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 <dirent.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <chrono>
28 #include <cinttypes>
29 #include <mutex>
30
31 #define LOG_TAG "Netd"
32
33 #include "log/log.h"
34
35 #include <binder/IPCThreadState.h>
36 #include <binder/IServiceManager.h>
37 #include <netdutils/Stopwatch.h>
38 #include <processgroup/processgroup.h>
39
40 #include "Controllers.h"
41 #include "FwmarkServer.h"
42 #include "MDnsSdListener.h"
43 #include "MDnsService.h"
44 #include "NFLogListener.h"
45 #include "NetdConstants.h"
46 #include "NetdHwService.h"
47 #include "NetdNativeService.h"
48 #include "NetlinkManager.h"
49 #include "Process.h"
50
51 #include "NetdUpdatablePublic.h"
52 #include "netd_resolv/resolv.h"
53
54 using android::IPCThreadState;
55 using android::sp;
56 using android::status_t;
57 using android::String16;
58 using android::net::FwmarkServer;
59 using android::net::gCtls;
60 using android::net::gLog;
61 using android::net::makeNFLogListener;
62 using android::net::MDnsService;
63 using android::net::NetdHwService;
64 using android::net::NetdNativeService;
65 using android::net::NetlinkManager;
66 using android::net::NFLogListener;
67 using android::netdutils::Stopwatch;
68
69 const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
70 constexpr const char DNSPROXYLISTENER_SOCKET_NAME[] = "dnsproxyd";
71
72 std::mutex android::net::gBigNetdLock;
73
74 namespace {
75
getNetworkContextCallback(uint32_t netId,uint32_t uid,android_net_context * netcontext)76 void getNetworkContextCallback(uint32_t netId, uint32_t uid, android_net_context* netcontext) {
77 gCtls->netCtrl.getNetworkContext(netId, uid, netcontext);
78 }
79
checkCallingPermissionCallback(const char * permission)80 bool checkCallingPermissionCallback(const char* permission) {
81 return checkCallingPermission(String16(permission));
82 }
83
logCallback(const char * msg)84 void logCallback(const char* msg) {
85 gLog.info(std::string(msg));
86 }
87
tagSocketCallback(int sockFd,uint32_t tag,uid_t uid,pid_t)88 int tagSocketCallback(int sockFd, uint32_t tag, uid_t uid, pid_t) {
89 // Workaround for secureVPN with VpnIsolation enabled, refer to b/159994981 for details.
90 if (tag == TAG_SYSTEM_DNS) uid = AID_DNS;
91 return libnetd_updatable_tagSocket(sockFd, tag, uid, AID_DNS);
92 }
93
evaluateDomainNameCallback(const android_net_context &,const char *)94 bool evaluateDomainNameCallback(const android_net_context&, const char* /*name*/) {
95 return true;
96 }
97
initDnsResolver()98 bool initDnsResolver() {
99 ResolverNetdCallbacks callbacks = {
100 .check_calling_permission = &checkCallingPermissionCallback,
101 .get_network_context = &getNetworkContextCallback,
102 .log = &logCallback,
103 .tagSocket = &tagSocketCallback,
104 .evaluate_domain_name = &evaluateDomainNameCallback,
105 };
106 return resolv_init(&callbacks);
107 }
108
109 } // namespace
110
main()111 int main() {
112 Stopwatch s;
113 gLog.info("netd 1.0 starting");
114
115 android::net::process::removePidFile(PID_FILE_PATH);
116 gLog.info("Pid file removed");
117 android::net::process::blockSigPipe();
118 gLog.info("SIGPIPE is blocked");
119
120 // Before we do anything that could fork, mark CLOEXEC the UNIX sockets that we get from init.
121 // FrameworkListener does this on initialization as well, but we only initialize these
122 // components after having initialized other subsystems that can fork.
123 for (const auto& sock :
124 {DNSPROXYLISTENER_SOCKET_NAME, FwmarkServer::SOCKET_NAME, MDnsSdListener::SOCKET_NAME}) {
125 setCloseOnExec(sock);
126 gLog.info("setCloseOnExec(%s)", sock);
127 }
128
129 std::string cg2_path;
130 if (!CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path)) {
131 ALOGE("Failed to find cgroup v2 root %s", strerror(errno));
132 exit(1);
133 }
134
135 if (libnetd_updatable_init(cg2_path.c_str())) {
136 ALOGE("libnetd_updatable_init failed");
137 exit(1);
138 }
139 gLog.info("libnetd_updatable_init success");
140
141 NetlinkManager *nm = NetlinkManager::Instance();
142 if (nm == nullptr) {
143 ALOGE("Unable to create NetlinkManager");
144 exit(1);
145 };
146 gLog.info("NetlinkManager instanced");
147
148 gCtls = new android::net::Controllers();
149 gCtls->init();
150
151 if (nm->start()) {
152 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
153 exit(1);
154 }
155
156 std::unique_ptr<NFLogListener> logListener;
157 {
158 auto result = makeNFLogListener();
159 if (!isOk(result)) {
160 ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
161 exit(1);
162 }
163 logListener = std::move(result.value());
164 auto status = gCtls->wakeupCtrl.init(logListener.get());
165 if (!isOk(result)) {
166 gLog.error("Unable to init WakeupController: %s", toString(result).c_str());
167 // We can still continue without wakeup packet logging.
168 }
169 }
170
171 // Set local DNS mode, to prevent bionic from proxying
172 // back to this service, recursively.
173 // TODO: Check if we could remove it since resolver cache no loger
174 // checks this environment variable after aosp/838050.
175 setenv("ANDROID_DNS_MODE", "local", 1);
176 // Note that only call initDnsResolver after gCtls initializing.
177 if (!initDnsResolver()) {
178 ALOGE("Unable to init resolver");
179 exit(1);
180 }
181
182 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter);
183 if (fwmarkServer.startListener()) {
184 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
185 exit(1);
186 }
187
188 Stopwatch subTime;
189 status_t ret;
190 if ((ret = NetdNativeService::start()) != android::OK) {
191 ALOGE("Unable to start NetdNativeService: %d", ret);
192 exit(1);
193 }
194 gLog.info("Registering NetdNativeService: %" PRId64 "us", subTime.getTimeAndResetUs());
195
196 if ((ret = MDnsService::start()) != android::OK) {
197 ALOGE("Unable to start MDnsService: %d", ret);
198 exit(1);
199 }
200 gLog.info("Registering MDnsService: %" PRId64 "us", subTime.getTimeAndResetUs());
201
202 android::net::process::ScopedPidFile pidFile(PID_FILE_PATH);
203
204 // Now that netd is ready to process commands, advertise service availability for HAL clients.
205 sp<NetdHwService> mHwSvc(new NetdHwService());
206 if ((ret = mHwSvc->start()) != android::OK) {
207 ALOGE("Unable to start NetdHwService: %d", ret);
208 exit(1);
209 }
210 gLog.info("Registering NetdHwService: %" PRId64 "us", subTime.getTimeAndResetUs());
211 gLog.info("Netd started in %" PRId64 "us", s.timeTakenUs());
212
213 IPCThreadState::self()->joinThreadPool();
214
215 gLog.info("netd exiting");
216
217 exit(0);
218 }
219