blob: fc58547a3100471a2610cd62cbeabd5ed3f17b0b [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>
San Mehatd1830422010-01-15 08:02:39 -080038
39#include "CommandListener.h"
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090040#include "Controllers.h"
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090041#include "FwmarkServer.h"
42#include "MDnsSdListener.h"
43#include "NFLogListener.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090044#include "NetdConstants.h"
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070045#include "NetdHwService.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090046#include "NetdNativeService.h"
San Mehatd1830422010-01-15 08:02:39 -080047#include "NetlinkManager.h"
Erik Kline85890042018-05-25 19:19:11 +090048#include "Process.h"
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090049#include "Stopwatch.h"
San Mehatd1830422010-01-15 08:02:39 -080050
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090051#include "netd_resolv/resolv_stub.h"
52
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090053using android::IPCThreadState;
Bernie Innocentia5161a02019-01-30 22:40:53 +090054using android::status_t;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090055using android::net::CommandListener;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090056using android::net::FwmarkServer;
Bernie Innocentia5161a02019-01-30 22:40:53 +090057using android::net::makeNFLogListener;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070058using android::net::NetdHwService;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090059using android::net::NetdNativeService;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090060using android::net::NetlinkManager;
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090061using android::net::NFLogListener;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090062
Robert Greenwalt347f6932014-10-31 18:54:06 -070063const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
Mike Yu0ae31af2018-11-15 21:58:19 +080064constexpr const char DNSPROXYLISTENER_SOCKET_NAME[] = "dnsproxyd";
San Mehatd1830422010-01-15 08:02:39 -080065
Luke Huangd1ee4622018-06-29 13:49:58 +080066std::mutex android::net::gBigNetdLock;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090067
San Mehatd1830422010-01-15 08:02:39 -080068int main() {
Pierre Imai1cfa5432016-02-24 18:00:03 +090069 using android::net::gCtls;
Bernie Innocentia5161a02019-01-30 22:40:53 +090070 using android::net::gLog;
Lorenzo Colitti4362bb22017-01-21 15:00:36 +090071 Stopwatch s;
Erik Klineb31fd692018-06-06 20:50:11 +090072 gLog.info("netd 1.0 starting");
San Mehatd1830422010-01-15 08:02:39 -080073
Erik Kline85890042018-05-25 19:19:11 +090074 android::net::process::removePidFile(PID_FILE_PATH);
75 android::net::process::blockSigPipe();
San Mehat5c1b8af2010-01-21 15:37:10 -080076
Lorenzo Colitti548bbd42017-08-28 23:05:12 +090077 // Before we do anything that could fork, mark CLOEXEC the UNIX sockets that we get from init.
78 // FrameworkListener does this on initialization as well, but we only initialize these
79 // components after having initialized other subsystems that can fork.
80 for (const auto& sock : { CommandListener::SOCKET_NAME,
Mike Yu0ae31af2018-11-15 21:58:19 +080081 DNSPROXYLISTENER_SOCKET_NAME,
Lorenzo Colitti548bbd42017-08-28 23:05:12 +090082 FwmarkServer::SOCKET_NAME,
83 MDnsSdListener::SOCKET_NAME }) {
84 setCloseOnExec(sock);
85 }
86
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090087 // Before we start any threads, populate the resolver stub pointers.
88 resolv_stub_init();
89
Joel Fernandesa03aced2019-01-10 11:24:11 -050090 // Make sure BPF programs are loaded before doing anything
91 while (!android::base::WaitForProperty("bpf.progs_loaded", "1",
92 std::chrono::seconds(5))) {
93 ALOGD("netd waited 5s for bpf.progs_loaded, still waiting...");
94 }
95
Pierre Imai1cfa5432016-02-24 18:00:03 +090096 NetlinkManager *nm = NetlinkManager::Instance();
97 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +000098 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -080099 exit(1);
100 };
101
Pierre Imai1cfa5432016-02-24 18:00:03 +0900102 gCtls = new android::net::Controllers();
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900103 gCtls->init();
104
Pierre Imai1cfa5432016-02-24 18:00:03 +0900105 CommandListener cl;
106 nm->setBroadcaster((SocketListener *) &cl);
San Mehatd1830422010-01-15 08:02:39 -0800107
108 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000109 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800110 exit(1);
111 }
112
Joel Scherpelz685deb52017-06-14 10:27:47 +0900113 std::unique_ptr<NFLogListener> logListener;
114 {
115 auto result = makeNFLogListener();
116 if (!isOk(result)) {
117 ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
118 exit(1);
119 }
120 logListener = std::move(result.value());
121 auto status = gCtls->wakeupCtrl.init(logListener.get());
122 if (!isOk(result)) {
Erik Klineb31fd692018-06-06 20:50:11 +0900123 gLog.error("Unable to init WakeupController: %s", toString(result).c_str());
Joel Scherpelz685deb52017-06-14 10:27:47 +0900124 // We can still continue without wakeup packet logging.
125 }
126 }
127
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700128 // Set local DNS mode, to prevent bionic from proxying
129 // back to this service, recursively.
Mike Yu0ae31af2018-11-15 21:58:19 +0800130 // TODO: Check if we could remove it since resolver cache no loger
131 // checks this environment variable after aosp/838050.
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700132 setenv("ANDROID_DNS_MODE", "local", 1);
Mike Yu0ae31af2018-11-15 21:58:19 +0800133 if (!gCtls->resolverCtrl.initResolver()) {
134 ALOGE("Unable to init resolver");
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700135 exit(1);
136 }
137
Pierre Imai1cfa5432016-02-24 18:00:03 +0900138 MDnsSdListener mdnsl;
139 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700140 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
141 exit(1);
142 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700143
Chenbo Feng9944ba82017-10-10 17:33:20 -0700144 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter, &gCtls->trafficCtrl);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900145 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700146 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
147 exit(1);
148 }
149
Lorenzo Colittif91e8ed2017-03-27 05:48:33 +0900150 Stopwatch subTime;
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900151 status_t ret;
152 if ((ret = NetdNativeService::start()) != android::OK) {
153 ALOGE("Unable to start NetdNativeService: %d", ret);
154 exit(1);
155 }
Erik Klineb31fd692018-06-06 20:50:11 +0900156 gLog.info("Registering NetdNativeService: %.1fms", subTime.getTimeAndReset());
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900157
San Mehatd1830422010-01-15 08:02:39 -0800158 /*
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900159 * Now that we're up, we can respond to commands. Starting the listener also tells
160 * NetworkManagementService that we are up and that our binder interface is ready.
San Mehatd1830422010-01-15 08:02:39 -0800161 */
Pierre Imai1cfa5432016-02-24 18:00:03 +0900162 if (cl.startListener()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000163 ALOGE("Unable to start CommandListener (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800164 exit(1);
165 }
Erik Klineb31fd692018-06-06 20:50:11 +0900166 gLog.info("Starting CommandListener: %.1fms", subTime.getTimeAndReset());
San Mehatd1830422010-01-15 08:02:39 -0800167
Erik Kline85890042018-05-25 19:19:11 +0900168 android::net::process::ScopedPidFile pidFile(PID_FILE_PATH);
Robert Greenwalt347f6932014-10-31 18:54:06 -0700169
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700170 // Now that netd is ready to process commands, advertise service
171 // availability for HAL clients.
172 NetdHwService mHwSvc;
173 if ((ret = mHwSvc.start()) != android::OK) {
174 ALOGE("Unable to start NetdHwService: %d", ret);
175 exit(1);
176 }
Erik Klineb31fd692018-06-06 20:50:11 +0900177 gLog.info("Registering NetdHwService: %.1fms", subTime.getTimeAndReset());
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700178
Erik Klineb31fd692018-06-06 20:50:11 +0900179 gLog.info("Netd started in %dms", static_cast<int>(s.timeTaken()));
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900180
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900181 IPCThreadState::self()->joinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800182
Erik Klineb31fd692018-06-06 20:50:11 +0900183 gLog.info("netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900184
San Mehatd1830422010-01-15 08:02:39 -0800185 exit(0);
186}