• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <chrono>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <signal.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <mutex>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 
28 #include <fcntl.h>
29 #include <dirent.h>
30 
31 #define LOG_TAG "Netd"
32 
33 #include "log/log.h"
34 
35 #include <android-base/properties.h>
36 #include <binder/IPCThreadState.h>
37 #include <binder/IServiceManager.h>
38 #include <netdutils/Stopwatch.h>
39 
40 #include "Controllers.h"
41 #include "FwmarkServer.h"
42 #include "MDnsSdListener.h"
43 #include "NFLogListener.h"
44 #include "NetdConstants.h"
45 #include "NetdHwService.h"
46 #include "NetdNativeService.h"
47 #include "NetlinkManager.h"
48 #include "Process.h"
49 
50 #include "netd_resolv/resolv.h"
51 #include "netd_resolv/resolv_stub.h"
52 
53 using android::IPCThreadState;
54 using android::status_t;
55 using android::String16;
56 using android::net::FwmarkServer;
57 using android::net::gCtls;
58 using android::net::gLog;
59 using android::net::makeNFLogListener;
60 using android::net::NetdHwService;
61 using android::net::NetdNativeService;
62 using android::net::NetlinkManager;
63 using android::net::NFLogListener;
64 using android::netdutils::Stopwatch;
65 
66 const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
67 constexpr const char DNSPROXYLISTENER_SOCKET_NAME[] = "dnsproxyd";
68 
69 std::mutex android::net::gBigNetdLock;
70 
71 namespace {
72 
getNetworkContextCallback(uint32_t netId,uint32_t uid,android_net_context * netcontext)73 void getNetworkContextCallback(uint32_t netId, uint32_t uid, android_net_context* netcontext) {
74     gCtls->netCtrl.getNetworkContext(netId, uid, netcontext);
75 }
76 
checkCallingPermissionCallback(const char * permission)77 bool checkCallingPermissionCallback(const char* permission) {
78     return checkCallingPermission(String16(permission));
79 }
80 
logCallback(const char * msg)81 void logCallback(const char* msg) {
82     gLog.info(std::string(msg));
83 }
84 
initDnsResolver()85 bool initDnsResolver() {
86     ResolverNetdCallbacks callbacks = {
87             .get_network_context = &getNetworkContextCallback,
88             .log = &logCallback,
89             .check_calling_permission = &checkCallingPermissionCallback,
90     };
91     return RESOLV_STUB.resolv_init(callbacks);
92 }
93 
94 }  // namespace
95 
main()96 int main() {
97     Stopwatch s;
98     gLog.info("netd 1.0 starting");
99 
100     android::net::process::removePidFile(PID_FILE_PATH);
101     android::net::process::blockSigPipe();
102 
103     // Before we do anything that could fork, mark CLOEXEC the UNIX sockets that we get from init.
104     // FrameworkListener does this on initialization as well, but we only initialize these
105     // components after having initialized other subsystems that can fork.
106     for (const auto& sock :
107          {DNSPROXYLISTENER_SOCKET_NAME, FwmarkServer::SOCKET_NAME, MDnsSdListener::SOCKET_NAME}) {
108         setCloseOnExec(sock);
109     }
110 
111     // Before we start any threads, populate the resolver stub pointers.
112     resolv_stub_init();
113 
114     // Make sure BPF programs are loaded before doing anything
115     while (!android::base::WaitForProperty("bpf.progs_loaded", "1",
116            std::chrono::seconds(5))) {
117         ALOGD("netd waited 5s for bpf.progs_loaded, still waiting...");
118     }
119 
120     NetlinkManager *nm = NetlinkManager::Instance();
121     if (nm == nullptr) {
122         ALOGE("Unable to create NetlinkManager");
123         exit(1);
124     };
125 
126     gCtls = new android::net::Controllers();
127     gCtls->init();
128 
129     if (nm->start()) {
130         ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
131         exit(1);
132     }
133 
134     std::unique_ptr<NFLogListener> logListener;
135     {
136         auto result = makeNFLogListener();
137         if (!isOk(result)) {
138             ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
139             exit(1);
140         }
141         logListener = std::move(result.value());
142         auto status = gCtls->wakeupCtrl.init(logListener.get());
143         if (!isOk(result)) {
144             gLog.error("Unable to init WakeupController: %s", toString(result).c_str());
145             // We can still continue without wakeup packet logging.
146         }
147     }
148 
149     // Set local DNS mode, to prevent bionic from proxying
150     // back to this service, recursively.
151     // TODO: Check if we could remove it since resolver cache no loger
152     // checks this environment variable after aosp/838050.
153     setenv("ANDROID_DNS_MODE", "local", 1);
154     // Note that only call initDnsResolver after gCtls initializing.
155     if (!initDnsResolver()) {
156         ALOGE("Unable to init resolver");
157         exit(1);
158     }
159 
160     MDnsSdListener mdnsl;
161     if (mdnsl.startListener()) {
162         ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
163         exit(1);
164     }
165 
166     FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter, &gCtls->trafficCtrl);
167     if (fwmarkServer.startListener()) {
168         ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
169         exit(1);
170     }
171 
172     Stopwatch subTime;
173     status_t ret;
174     if ((ret = NetdNativeService::start()) != android::OK) {
175         ALOGE("Unable to start NetdNativeService: %d", ret);
176         exit(1);
177     }
178     gLog.info("Registering NetdNativeService: %.1fms", subTime.getTimeAndReset());
179 
180     android::net::process::ScopedPidFile pidFile(PID_FILE_PATH);
181 
182     // Now that netd is ready to process commands, advertise service
183     // availability for HAL clients.
184     NetdHwService mHwSvc;
185     if ((ret = mHwSvc.start()) != android::OK) {
186         ALOGE("Unable to start NetdHwService: %d", ret);
187         exit(1);
188     }
189     gLog.info("Registering NetdHwService: %.1fms", subTime.getTimeAndReset());
190 
191     gLog.info("Netd started in %dms", static_cast<int>(s.timeTaken()));
192 
193     IPCThreadState::self()->joinThreadPool();
194 
195     gLog.info("netd exiting");
196 
197     exit(0);
198 }
199