blob: 9d38ed2575177a348967a4cb882478f623a87909 [file] [log] [blame]
San Mehatd1830422010-01-15 08:02:39 -08001/*
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
Joel Fernandesa03aced2019-01-10 11:24:11 -050017#include <chrono>
San Mehatd1830422010-01-15 08:02:39 -080018#include <stdio.h>
19#include <stdlib.h>
San Mehat5c1b8af2010-01-21 15:37:10 -080020#include <signal.h>
San Mehatd1830422010-01-15 08:02:39 -080021#include <errno.h>
22#include <string.h>
Luke Huangd1ee4622018-06-29 13:49:58 +080023#include <mutex>
San Mehatd1830422010-01-15 08:02:39 -080024#include <sys/stat.h>
25#include <sys/types.h>
San Mehat5c1b8af2010-01-21 15:37:10 -080026#include <sys/wait.h>
San Mehatd1830422010-01-15 08:02:39 -080027
28#include <fcntl.h>
29#include <dirent.h>
30
31#define LOG_TAG "Netd"
32
Logan Chien3f461482018-04-23 14:31:32 +080033#include "log/log.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090034
Joel Fernandesa03aced2019-01-10 11:24:11 -050035#include <android-base/properties.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090036#include <binder/IPCThreadState.h>
37#include <binder/IServiceManager.h>
Mike Yue7e332f2019-03-13 17:15:48 +080038#include <netdutils/Stopwatch.h>
San Mehatd1830422010-01-15 08:02:39 -080039
40#include "CommandListener.h"
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090041#include "Controllers.h"
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090042#include "FwmarkServer.h"
43#include "MDnsSdListener.h"
44#include "NFLogListener.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090045#include "NetdConstants.h"
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070046#include "NetdHwService.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090047#include "NetdNativeService.h"
San Mehatd1830422010-01-15 08:02:39 -080048#include "NetlinkManager.h"
Erik Kline85890042018-05-25 19:19:11 +090049#include "Process.h"
San Mehatd1830422010-01-15 08:02:39 -080050
Luke Huangf29fe682019-03-26 15:15:44 +080051#include "netd_resolv/resolv.h"
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090052#include "netd_resolv/resolv_stub.h"
53
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090054using android::IPCThreadState;
Bernie Innocentia5161a02019-01-30 22:40:53 +090055using android::status_t;
Luke Huangf29fe682019-03-26 15:15:44 +080056using android::String16;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090057using android::net::CommandListener;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090058using android::net::FwmarkServer;
Luke Huangf29fe682019-03-26 15:15:44 +080059using android::net::gCtls;
60using android::net::gLog;
Bernie Innocentia5161a02019-01-30 22:40:53 +090061using android::net::makeNFLogListener;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070062using android::net::NetdHwService;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090063using android::net::NetdNativeService;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090064using android::net::NetlinkManager;
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090065using android::net::NFLogListener;
Mike Yue7e332f2019-03-13 17:15:48 +080066using android::netdutils::Stopwatch;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090067
Robert Greenwalt347f6932014-10-31 18:54:06 -070068const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
Mike Yu0ae31af2018-11-15 21:58:19 +080069constexpr const char DNSPROXYLISTENER_SOCKET_NAME[] = "dnsproxyd";
San Mehatd1830422010-01-15 08:02:39 -080070
Luke Huangd1ee4622018-06-29 13:49:58 +080071std::mutex android::net::gBigNetdLock;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090072
Luke Huangf29fe682019-03-26 15:15:44 +080073namespace {
74
75void getNetworkContextCallback(uint32_t netId, uint32_t uid, android_net_context* netcontext) {
76 gCtls->netCtrl.getNetworkContext(netId, uid, netcontext);
77}
78
79bool checkCallingPermissionCallback(const char* permission) {
80 return checkCallingPermission(String16(permission));
81}
82
83void logCallback(const char* msg) {
84 gLog.info(std::string(msg));
85}
86
87bool initDnsResolver() {
88 ResolverNetdCallbacks callbacks = {
89 .get_network_context = &getNetworkContextCallback,
90 .log = &logCallback,
91 .check_calling_permission = &checkCallingPermissionCallback,
92 };
93 return RESOLV_STUB.resolv_init(callbacks);
94}
95
96} // namespace
97
San Mehatd1830422010-01-15 08:02:39 -080098int main() {
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090099 Stopwatch s;
Erik Klineb31fd692018-06-06 20:50:11 +0900100 gLog.info("netd 1.0 starting");
San Mehatd1830422010-01-15 08:02:39 -0800101
Erik Kline85890042018-05-25 19:19:11 +0900102 android::net::process::removePidFile(PID_FILE_PATH);
103 android::net::process::blockSigPipe();
San Mehat5c1b8af2010-01-21 15:37:10 -0800104
Lorenzo Colitti548bbd42017-08-28 23:05:12 +0900105 // Before we do anything that could fork, mark CLOEXEC the UNIX sockets that we get from init.
106 // FrameworkListener does this on initialization as well, but we only initialize these
107 // components after having initialized other subsystems that can fork.
108 for (const auto& sock : { CommandListener::SOCKET_NAME,
Mike Yu0ae31af2018-11-15 21:58:19 +0800109 DNSPROXYLISTENER_SOCKET_NAME,
Lorenzo Colitti548bbd42017-08-28 23:05:12 +0900110 FwmarkServer::SOCKET_NAME,
111 MDnsSdListener::SOCKET_NAME }) {
112 setCloseOnExec(sock);
113 }
114
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900115 // Before we start any threads, populate the resolver stub pointers.
116 resolv_stub_init();
117
Joel Fernandesa03aced2019-01-10 11:24:11 -0500118 // Make sure BPF programs are loaded before doing anything
119 while (!android::base::WaitForProperty("bpf.progs_loaded", "1",
120 std::chrono::seconds(5))) {
121 ALOGD("netd waited 5s for bpf.progs_loaded, still waiting...");
122 }
123
Pierre Imai1cfa5432016-02-24 18:00:03 +0900124 NetlinkManager *nm = NetlinkManager::Instance();
125 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +0000126 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -0800127 exit(1);
128 };
129
Pierre Imai1cfa5432016-02-24 18:00:03 +0900130 gCtls = new android::net::Controllers();
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900131 gCtls->init();
132
Pierre Imai1cfa5432016-02-24 18:00:03 +0900133 CommandListener cl;
134 nm->setBroadcaster((SocketListener *) &cl);
San Mehatd1830422010-01-15 08:02:39 -0800135
136 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000137 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800138 exit(1);
139 }
140
Joel Scherpelz685deb52017-06-14 10:27:47 +0900141 std::unique_ptr<NFLogListener> logListener;
142 {
143 auto result = makeNFLogListener();
144 if (!isOk(result)) {
145 ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
146 exit(1);
147 }
148 logListener = std::move(result.value());
149 auto status = gCtls->wakeupCtrl.init(logListener.get());
150 if (!isOk(result)) {
Erik Klineb31fd692018-06-06 20:50:11 +0900151 gLog.error("Unable to init WakeupController: %s", toString(result).c_str());
Joel Scherpelz685deb52017-06-14 10:27:47 +0900152 // We can still continue without wakeup packet logging.
153 }
154 }
155
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700156 // Set local DNS mode, to prevent bionic from proxying
157 // back to this service, recursively.
Mike Yu0ae31af2018-11-15 21:58:19 +0800158 // TODO: Check if we could remove it since resolver cache no loger
159 // checks this environment variable after aosp/838050.
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700160 setenv("ANDROID_DNS_MODE", "local", 1);
Luke Huangf29fe682019-03-26 15:15:44 +0800161 // Note that only call initDnsResolver after gCtls initializing.
162 if (!initDnsResolver()) {
Mike Yu0ae31af2018-11-15 21:58:19 +0800163 ALOGE("Unable to init resolver");
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700164 exit(1);
165 }
166
Pierre Imai1cfa5432016-02-24 18:00:03 +0900167 MDnsSdListener mdnsl;
168 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700169 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
170 exit(1);
171 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700172
Chenbo Feng9944ba82017-10-10 17:33:20 -0700173 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter, &gCtls->trafficCtrl);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900174 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700175 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
176 exit(1);
177 }
178
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900179 Stopwatch subTime;
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900180 status_t ret;
181 if ((ret = NetdNativeService::start()) != android::OK) {
182 ALOGE("Unable to start NetdNativeService: %d", ret);
183 exit(1);
184 }
Erik Klineb31fd692018-06-06 20:50:11 +0900185 gLog.info("Registering NetdNativeService: %.1fms", subTime.getTimeAndReset());
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900186
San Mehatd1830422010-01-15 08:02:39 -0800187 /*
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900188 * Now that we're up, we can respond to commands. Starting the listener also tells
189 * NetworkManagementService that we are up and that our binder interface is ready.
San Mehatd1830422010-01-15 08:02:39 -0800190 */
Pierre Imai1cfa5432016-02-24 18:00:03 +0900191 if (cl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000192 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800193 exit(1);
194 }
Erik Klineb31fd692018-06-06 20:50:11 +0900195 gLog.info("Starting CommandListener: %.1fms", subTime.getTimeAndReset());
San Mehatd1830422010-01-15 08:02:39 -0800196
Erik Kline85890042018-05-25 19:19:11 +0900197 android::net::process::ScopedPidFile pidFile(PID_FILE_PATH);
Robert Greenwalt347f6932014-10-31 18:54:06 -0700198
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700199 // Now that netd is ready to process commands, advertise service
200 // availability for HAL clients.
201 NetdHwService mHwSvc;
202 if ((ret = mHwSvc.start()) != android::OK) {
203 ALOGE("Unable to start NetdHwService: %d", ret);
204 exit(1);
205 }
Erik Klineb31fd692018-06-06 20:50:11 +0900206 gLog.info("Registering NetdHwService: %.1fms", subTime.getTimeAndReset());
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700207
Erik Klineb31fd692018-06-06 20:50:11 +0900208 gLog.info("Netd started in %dms", static_cast<int>(s.timeTaken()));
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900209
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900210 IPCThreadState::self()->joinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800211
Erik Klineb31fd692018-06-06 20:50:11 +0900212 gLog.info("netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900213
San Mehatd1830422010-01-15 08:02:39 -0800214 exit(0);
215}