blob: bc48ac20cae9c103367ac3ebdfdda8f7b8d32b46 [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
Bernie Innocenti196f1b82019-05-20 16:34:16 +090017#include <dirent.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <signal.h>
San Mehatd1830422010-01-15 08:02:39 -080021#include <stdio.h>
22#include <stdlib.h>
San Mehatd1830422010-01-15 08:02:39 -080023#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
San Mehat5c1b8af2010-01-21 15:37:10 -080026#include <sys/wait.h>
Bernie Innocenti196f1b82019-05-20 16:34:16 +090027#include <chrono>
28#include <cinttypes>
29#include <mutex>
San Mehatd1830422010-01-15 08:02:39 -080030
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
35#include <binder/IPCThreadState.h>
36#include <binder/IServiceManager.h>
Steven Morelande2349022020-01-10 15:38:03 -080037#include <libbpf_android.h>
Mike Yue7e332f2019-03-13 17:15:48 +080038#include <netdutils/Stopwatch.h>
San Mehatd1830422010-01-15 08:02:39 -080039
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"
San Mehatd1830422010-01-15 08:02:39 -080049
Luke Huangf29fe682019-03-26 15:15:44 +080050#include "netd_resolv/resolv.h"
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090051
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090052using android::IPCThreadState;
Hans Boehmccdecaa2019-08-26 17:36:42 -070053using android::sp;
Bernie Innocentia5161a02019-01-30 22:40:53 +090054using android::status_t;
Luke Huangf29fe682019-03-26 15:15:44 +080055using android::String16;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090056using android::net::FwmarkServer;
Luke Huangf29fe682019-03-26 15:15:44 +080057using android::net::gCtls;
58using android::net::gLog;
Bernie Innocentia5161a02019-01-30 22:40:53 +090059using android::net::makeNFLogListener;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070060using android::net::NetdHwService;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090061using android::net::NetdNativeService;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090062using android::net::NetlinkManager;
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090063using android::net::NFLogListener;
Mike Yue7e332f2019-03-13 17:15:48 +080064using android::netdutils::Stopwatch;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090065
Robert Greenwalt347f6932014-10-31 18:54:06 -070066const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
Mike Yu0ae31af2018-11-15 21:58:19 +080067constexpr const char DNSPROXYLISTENER_SOCKET_NAME[] = "dnsproxyd";
San Mehatd1830422010-01-15 08:02:39 -080068
Luke Huangd1ee4622018-06-29 13:49:58 +080069std::mutex android::net::gBigNetdLock;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090070
Luke Huangf29fe682019-03-26 15:15:44 +080071namespace {
72
73void getNetworkContextCallback(uint32_t netId, uint32_t uid, android_net_context* netcontext) {
74 gCtls->netCtrl.getNetworkContext(netId, uid, netcontext);
75}
76
77bool checkCallingPermissionCallback(const char* permission) {
78 return checkCallingPermission(String16(permission));
79}
80
81void logCallback(const char* msg) {
82 gLog.info(std::string(msg));
83}
84
Praveen Moongalam Thyagarajanf7b003d2019-09-04 15:08:22 -070085int tagSocketCallback(int sockFd, uint32_t tag, uid_t uid, pid_t) {
Maciej Żenczykowskie9d140b2020-07-02 04:08:59 -070086 // Workaround for secureVPN with VpnIsolation enabled, refer to b/159994981 for details.
87 if (tag == TAG_SYSTEM_DNS) uid = AID_DNS;
Sehee Parkb953a912019-07-04 13:53:45 +090088 return gCtls->trafficCtrl.tagSocket(sockFd, tag, uid, geteuid());
89}
90
Felipe Mosso Ferfogliafeafd182019-10-31 10:31:25 -030091bool evaluateDomainNameCallback(const android_net_context&, const char* /*name*/) {
92 return true;
93}
94
Luke Huangf29fe682019-03-26 15:15:44 +080095bool initDnsResolver() {
96 ResolverNetdCallbacks callbacks = {
Nick Desaulniers6b357502019-10-11 09:26:44 -070097 .check_calling_permission = &checkCallingPermissionCallback,
Luke Huangf29fe682019-03-26 15:15:44 +080098 .get_network_context = &getNetworkContextCallback,
99 .log = &logCallback,
Sehee Parkb953a912019-07-04 13:53:45 +0900100 .tagSocket = &tagSocketCallback,
Felipe Mosso Ferfogliafeafd182019-10-31 10:31:25 -0300101 .evaluate_domain_name = &evaluateDomainNameCallback,
Luke Huangf29fe682019-03-26 15:15:44 +0800102 };
Jooyung Han3e64aa12019-11-27 15:36:29 +0900103 return resolv_init(&callbacks);
Luke Huangf29fe682019-03-26 15:15:44 +0800104}
105
106} // namespace
107
San Mehatd1830422010-01-15 08:02:39 -0800108int main() {
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900109 Stopwatch s;
Erik Klineb31fd692018-06-06 20:50:11 +0900110 gLog.info("netd 1.0 starting");
San Mehatd1830422010-01-15 08:02:39 -0800111
Erik Kline85890042018-05-25 19:19:11 +0900112 android::net::process::removePidFile(PID_FILE_PATH);
Ken Chen66e860f2021-04-01 15:50:28 +0800113 gLog.info("Pid file removed");
Erik Kline85890042018-05-25 19:19:11 +0900114 android::net::process::blockSigPipe();
Ken Chen66e860f2021-04-01 15:50:28 +0800115 gLog.info("SIGPIPE is blocked");
San Mehat5c1b8af2010-01-21 15:37:10 -0800116
Lorenzo Colitti548bbd42017-08-28 23:05:12 +0900117 // Before we do anything that could fork, mark CLOEXEC the UNIX sockets that we get from init.
118 // FrameworkListener does this on initialization as well, but we only initialize these
119 // components after having initialized other subsystems that can fork.
Maciej Żenczykowski38758392019-05-09 05:47:59 +0000120 for (const auto& sock :
121 {DNSPROXYLISTENER_SOCKET_NAME, FwmarkServer::SOCKET_NAME, MDnsSdListener::SOCKET_NAME}) {
Lorenzo Colitti548bbd42017-08-28 23:05:12 +0900122 setCloseOnExec(sock);
Ken Chen66e860f2021-04-01 15:50:28 +0800123 gLog.info("setCloseOnExec(%s)", sock);
Lorenzo Colitti548bbd42017-08-28 23:05:12 +0900124 }
125
Joel Fernandesa03aced2019-01-10 11:24:11 -0500126 // Make sure BPF programs are loaded before doing anything
Steven Morelande2349022020-01-10 15:38:03 -0800127 android::bpf::waitForProgsLoaded();
Ken Chen66e860f2021-04-01 15:50:28 +0800128 gLog.info("BPF programs are loaded");
Joel Fernandesa03aced2019-01-10 11:24:11 -0500129
Pierre Imai1cfa5432016-02-24 18:00:03 +0900130 NetlinkManager *nm = NetlinkManager::Instance();
131 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +0000132 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -0800133 exit(1);
134 };
Ken Chen66e860f2021-04-01 15:50:28 +0800135 gLog.info("NetlinkManager instanced");
San Mehatd1830422010-01-15 08:02:39 -0800136
Pierre Imai1cfa5432016-02-24 18:00:03 +0900137 gCtls = new android::net::Controllers();
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900138 gCtls->init();
139
San Mehatd1830422010-01-15 08:02:39 -0800140 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000141 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800142 exit(1);
143 }
144
Joel Scherpelz685deb52017-06-14 10:27:47 +0900145 std::unique_ptr<NFLogListener> logListener;
146 {
147 auto result = makeNFLogListener();
148 if (!isOk(result)) {
149 ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
150 exit(1);
151 }
152 logListener = std::move(result.value());
153 auto status = gCtls->wakeupCtrl.init(logListener.get());
154 if (!isOk(result)) {
Erik Klineb31fd692018-06-06 20:50:11 +0900155 gLog.error("Unable to init WakeupController: %s", toString(result).c_str());
Joel Scherpelz685deb52017-06-14 10:27:47 +0900156 // We can still continue without wakeup packet logging.
157 }
158 }
159
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700160 // Set local DNS mode, to prevent bionic from proxying
161 // back to this service, recursively.
Mike Yu0ae31af2018-11-15 21:58:19 +0800162 // TODO: Check if we could remove it since resolver cache no loger
163 // checks this environment variable after aosp/838050.
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700164 setenv("ANDROID_DNS_MODE", "local", 1);
Luke Huangf29fe682019-03-26 15:15:44 +0800165 // Note that only call initDnsResolver after gCtls initializing.
166 if (!initDnsResolver()) {
Mike Yu0ae31af2018-11-15 21:58:19 +0800167 ALOGE("Unable to init resolver");
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700168 exit(1);
169 }
170
Pierre Imai1cfa5432016-02-24 18:00:03 +0900171 MDnsSdListener mdnsl;
172 if (mdnsl.startListener()) {
Robert Greenwalt745e09f2012-03-29 14:45:54 -0700173 ALOGE("Unable to start MDnsSdListener (%s)", strerror(errno));
174 exit(1);
175 }
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700176
Chenbo Feng9944ba82017-10-10 17:33:20 -0700177 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter, &gCtls->trafficCtrl);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900178 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700179 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
180 exit(1);
181 }
182
Luke Huangcfd04b22019-03-18 15:53:21 +0800183 Stopwatch subTime;
184 status_t ret;
185 if ((ret = NetdNativeService::start()) != android::OK) {
186 ALOGE("Unable to start NetdNativeService: %d", ret);
San Mehatd1830422010-01-15 08:02:39 -0800187 exit(1);
188 }
Bernie Innocenti196f1b82019-05-20 16:34:16 +0900189 gLog.info("Registering NetdNativeService: %" PRId64 "us", subTime.getTimeAndResetUs());
San Mehatd1830422010-01-15 08:02:39 -0800190
Erik Kline85890042018-05-25 19:19:11 +0900191 android::net::process::ScopedPidFile pidFile(PID_FILE_PATH);
Robert Greenwalt347f6932014-10-31 18:54:06 -0700192
Hans Boehmccdecaa2019-08-26 17:36:42 -0700193 // Now that netd is ready to process commands, advertise service availability for HAL clients.
194 sp<NetdHwService> mHwSvc(new NetdHwService());
195 if ((ret = mHwSvc->start()) != android::OK) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700196 ALOGE("Unable to start NetdHwService: %d", ret);
197 exit(1);
198 }
Bernie Innocenti196f1b82019-05-20 16:34:16 +0900199 gLog.info("Registering NetdHwService: %" PRId64 "us", subTime.getTimeAndResetUs());
200 gLog.info("Netd started in %" PRId64 "us", s.timeTakenUs());
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900201
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900202 IPCThreadState::self()->joinThreadPool();
San Mehatd1830422010-01-15 08:02:39 -0800203
Erik Klineb31fd692018-06-06 20:50:11 +0900204 gLog.info("netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900205
San Mehatd1830422010-01-15 08:02:39 -0800206 exit(0);
207}