blob: 3c6b0d52e32bc449eb9adc785da8f5103af81bca [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
Devin Moorec304b472022-04-08 21:42:50 +000035#include <android/binder_manager.h>
36#include <android/binder_process.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090037#include <binder/IPCThreadState.h>
38#include <binder/IServiceManager.h>
Devin Moorec304b472022-04-08 21:42:50 +000039#include <hidl/HidlTransportSupport.h>
Mike Yue7e332f2019-03-13 17:15:48 +080040#include <netdutils/Stopwatch.h>
Ken Chenebdeba82021-10-28 09:54:46 +080041#include <processgroup/processgroup.h>
San Mehatd1830422010-01-15 08:02:39 -080042
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090043#include "Controllers.h"
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090044#include "FwmarkServer.h"
45#include "MDnsSdListener.h"
paulhuc8a58ff2022-02-09 18:37:27 +080046#include "MDnsService.h"
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090047#include "NFLogListener.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090048#include "NetdConstants.h"
Devin Moorec304b472022-04-08 21:42:50 +000049#include "NetdHwAidlService.h"
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070050#include "NetdHwService.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090051#include "NetdNativeService.h"
San Mehatd1830422010-01-15 08:02:39 -080052#include "NetlinkManager.h"
Erik Kline85890042018-05-25 19:19:11 +090053#include "Process.h"
San Mehatd1830422010-01-15 08:02:39 -080054
Ken Chenebdeba82021-10-28 09:54:46 +080055#include "NetdUpdatablePublic.h"
Luke Huangf29fe682019-03-26 15:15:44 +080056#include "netd_resolv/resolv.h"
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090057
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090058using android::IPCThreadState;
Hans Boehmccdecaa2019-08-26 17:36:42 -070059using android::sp;
Bernie Innocentia5161a02019-01-30 22:40:53 +090060using android::status_t;
Luke Huangf29fe682019-03-26 15:15:44 +080061using android::String16;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090062using android::net::FwmarkServer;
Luke Huangf29fe682019-03-26 15:15:44 +080063using android::net::gCtls;
64using android::net::gLog;
Bernie Innocentia5161a02019-01-30 22:40:53 +090065using android::net::makeNFLogListener;
paulhuc8a58ff2022-02-09 18:37:27 +080066using android::net::MDnsService;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070067using android::net::NetdHwService;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090068using android::net::NetdNativeService;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090069using android::net::NetlinkManager;
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090070using android::net::NFLogListener;
Devin Moorec304b472022-04-08 21:42:50 +000071using android::net::aidl::NetdHwAidlService;
Mike Yue7e332f2019-03-13 17:15:48 +080072using android::netdutils::Stopwatch;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090073
Robert Greenwalt347f6932014-10-31 18:54:06 -070074const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
Mike Yu0ae31af2018-11-15 21:58:19 +080075constexpr const char DNSPROXYLISTENER_SOCKET_NAME[] = "dnsproxyd";
San Mehatd1830422010-01-15 08:02:39 -080076
Luke Huangd1ee4622018-06-29 13:49:58 +080077std::mutex android::net::gBigNetdLock;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090078
Luke Huangf29fe682019-03-26 15:15:44 +080079namespace {
80
81void getNetworkContextCallback(uint32_t netId, uint32_t uid, android_net_context* netcontext) {
82 gCtls->netCtrl.getNetworkContext(netId, uid, netcontext);
83}
84
85bool checkCallingPermissionCallback(const char* permission) {
86 return checkCallingPermission(String16(permission));
87}
88
89void logCallback(const char* msg) {
90 gLog.info(std::string(msg));
91}
92
Praveen Moongalam Thyagarajanf7b003d2019-09-04 15:08:22 -070093int tagSocketCallback(int sockFd, uint32_t tag, uid_t uid, pid_t) {
Maciej Żenczykowskie9d140b2020-07-02 04:08:59 -070094 // Workaround for secureVPN with VpnIsolation enabled, refer to b/159994981 for details.
95 if (tag == TAG_SYSTEM_DNS) uid = AID_DNS;
Ken Chenebdeba82021-10-28 09:54:46 +080096 return libnetd_updatable_tagSocket(sockFd, tag, uid, AID_DNS);
Sehee Parkb953a912019-07-04 13:53:45 +090097}
98
Ken Chene22ac8d2022-12-24 09:24:56 +080099bool evaluateDomainNameCallback(const android_net_context& netcontext, const char* /*name*/) {
100 // OEMs should NOT modify IF statement, or DNS control provided by mainline modules may break.
101 if (!gCtls->netCtrl.isUidAllowed(netcontext.app_netid, netcontext.uid)) {
102 ALOGI("uid %d is not allowed to use netid %u", netcontext.uid, netcontext.app_netid);
103 return false;
104 }
105
106 // Add OEM customization from here
107 // ...
Felipe Mosso Ferfogliafeafd182019-10-31 10:31:25 -0300108 return true;
109}
110
Luke Huangf29fe682019-03-26 15:15:44 +0800111bool initDnsResolver() {
112 ResolverNetdCallbacks callbacks = {
Nick Desaulniers6b357502019-10-11 09:26:44 -0700113 .check_calling_permission = &checkCallingPermissionCallback,
Luke Huangf29fe682019-03-26 15:15:44 +0800114 .get_network_context = &getNetworkContextCallback,
115 .log = &logCallback,
Sehee Parkb953a912019-07-04 13:53:45 +0900116 .tagSocket = &tagSocketCallback,
Felipe Mosso Ferfogliafeafd182019-10-31 10:31:25 -0300117 .evaluate_domain_name = &evaluateDomainNameCallback,
Luke Huangf29fe682019-03-26 15:15:44 +0800118 };
Jooyung Han3e64aa12019-11-27 15:36:29 +0900119 return resolv_init(&callbacks);
Luke Huangf29fe682019-03-26 15:15:44 +0800120}
121
122} // namespace
123
San Mehatd1830422010-01-15 08:02:39 -0800124int main() {
Lorenzo Colitti4362bb22017-01-21 15:00:36 +0900125 Stopwatch s;
Erik Klineb31fd692018-06-06 20:50:11 +0900126 gLog.info("netd 1.0 starting");
San Mehatd1830422010-01-15 08:02:39 -0800127
Erik Kline85890042018-05-25 19:19:11 +0900128 android::net::process::removePidFile(PID_FILE_PATH);
Ken Chen66e860f2021-04-01 15:50:28 +0800129 gLog.info("Pid file removed");
Erik Kline85890042018-05-25 19:19:11 +0900130 android::net::process::blockSigPipe();
Ken Chen66e860f2021-04-01 15:50:28 +0800131 gLog.info("SIGPIPE is blocked");
San Mehat5c1b8af2010-01-21 15:37:10 -0800132
Lorenzo Colitti548bbd42017-08-28 23:05:12 +0900133 // Before we do anything that could fork, mark CLOEXEC the UNIX sockets that we get from init.
134 // FrameworkListener does this on initialization as well, but we only initialize these
135 // components after having initialized other subsystems that can fork.
Maciej Żenczykowski38758392019-05-09 05:47:59 +0000136 for (const auto& sock :
137 {DNSPROXYLISTENER_SOCKET_NAME, FwmarkServer::SOCKET_NAME, MDnsSdListener::SOCKET_NAME}) {
Lorenzo Colitti548bbd42017-08-28 23:05:12 +0900138 setCloseOnExec(sock);
Ken Chen66e860f2021-04-01 15:50:28 +0800139 gLog.info("setCloseOnExec(%s)", sock);
Lorenzo Colitti548bbd42017-08-28 23:05:12 +0900140 }
141
Ken Chenebdeba82021-10-28 09:54:46 +0800142 std::string cg2_path;
143 if (!CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path)) {
144 ALOGE("Failed to find cgroup v2 root %s", strerror(errno));
145 exit(1);
146 }
147
148 if (libnetd_updatable_init(cg2_path.c_str())) {
149 ALOGE("libnetd_updatable_init failed");
150 exit(1);
151 }
152 gLog.info("libnetd_updatable_init success");
Joel Fernandesa03aced2019-01-10 11:24:11 -0500153
Pierre Imai1cfa5432016-02-24 18:00:03 +0900154 NetlinkManager *nm = NetlinkManager::Instance();
155 if (nm == nullptr) {
Steve Block5ea0c052012-01-06 19:18:11 +0000156 ALOGE("Unable to create NetlinkManager");
San Mehatd1830422010-01-15 08:02:39 -0800157 exit(1);
158 };
Ken Chen66e860f2021-04-01 15:50:28 +0800159 gLog.info("NetlinkManager instanced");
San Mehatd1830422010-01-15 08:02:39 -0800160
Pierre Imai1cfa5432016-02-24 18:00:03 +0900161 gCtls = new android::net::Controllers();
Lorenzo Colitti1ed96e22017-02-02 12:21:56 +0900162 gCtls->init();
163
San Mehatd1830422010-01-15 08:02:39 -0800164 if (nm->start()) {
Steve Block5ea0c052012-01-06 19:18:11 +0000165 ALOGE("Unable to start NetlinkManager (%s)", strerror(errno));
San Mehatd1830422010-01-15 08:02:39 -0800166 exit(1);
167 }
168
Joel Scherpelz685deb52017-06-14 10:27:47 +0900169 std::unique_ptr<NFLogListener> logListener;
170 {
171 auto result = makeNFLogListener();
172 if (!isOk(result)) {
173 ALOGE("Unable to create NFLogListener: %s", toString(result).c_str());
174 exit(1);
175 }
176 logListener = std::move(result.value());
177 auto status = gCtls->wakeupCtrl.init(logListener.get());
Christian Koestlin002e5fa2021-06-03 00:11:02 +0200178 if (!isOk(status)) {
179 gLog.error("Unable to init WakeupController: %s", toString(status).c_str());
Joel Scherpelz685deb52017-06-14 10:27:47 +0900180 // We can still continue without wakeup packet logging.
181 }
182 }
183
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700184 // Set local DNS mode, to prevent bionic from proxying
185 // back to this service, recursively.
Mike Yu0ae31af2018-11-15 21:58:19 +0800186 // TODO: Check if we could remove it since resolver cache no loger
187 // checks this environment variable after aosp/838050.
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700188 setenv("ANDROID_DNS_MODE", "local", 1);
Luke Huangf29fe682019-03-26 15:15:44 +0800189 // Note that only call initDnsResolver after gCtls initializing.
190 if (!initDnsResolver()) {
Mike Yu0ae31af2018-11-15 21:58:19 +0800191 ALOGE("Unable to init resolver");
Brad Fitzpatrick007e9872010-10-27 11:39:52 -0700192 exit(1);
193 }
194
Patrick Rohrb371bc32022-02-01 22:43:23 +0100195 FwmarkServer fwmarkServer(&gCtls->netCtrl, &gCtls->eventReporter);
Pierre Imai1cfa5432016-02-24 18:00:03 +0900196 if (fwmarkServer.startListener()) {
Sreeram Ramachandran030b36e2014-05-11 21:04:03 -0700197 ALOGE("Unable to start FwmarkServer (%s)", strerror(errno));
198 exit(1);
199 }
200
Luke Huangcfd04b22019-03-18 15:53:21 +0800201 Stopwatch subTime;
202 status_t ret;
203 if ((ret = NetdNativeService::start()) != android::OK) {
204 ALOGE("Unable to start NetdNativeService: %d", ret);
San Mehatd1830422010-01-15 08:02:39 -0800205 exit(1);
206 }
Bernie Innocenti196f1b82019-05-20 16:34:16 +0900207 gLog.info("Registering NetdNativeService: %" PRId64 "us", subTime.getTimeAndResetUs());
San Mehatd1830422010-01-15 08:02:39 -0800208
paulhuc8a58ff2022-02-09 18:37:27 +0800209 if ((ret = MDnsService::start()) != android::OK) {
210 ALOGE("Unable to start MDnsService: %d", ret);
211 exit(1);
212 }
213 gLog.info("Registering MDnsService: %" PRId64 "us", subTime.getTimeAndResetUs());
214
Erik Kline85890042018-05-25 19:19:11 +0900215 android::net::process::ScopedPidFile pidFile(PID_FILE_PATH);
Robert Greenwalt347f6932014-10-31 18:54:06 -0700216
Hans Boehmccdecaa2019-08-26 17:36:42 -0700217 // Now that netd is ready to process commands, advertise service availability for HAL clients.
Devin Moorec304b472022-04-08 21:42:50 +0000218 // Usage of this HAL is anticipated to be thin; one thread per HAL service should suffice,
219 // AIDL and HIDL.
220 android::hardware::configureRpcThreadpool(2, true /* callerWillJoin */);
221 IPCThreadState::self()->disableBackgroundScheduling(true);
222
223 std::thread aidlService = std::thread(NetdHwAidlService::run);
224
Hans Boehmccdecaa2019-08-26 17:36:42 -0700225 sp<NetdHwService> mHwSvc(new NetdHwService());
Devin Moorec304b472022-04-08 21:42:50 +0000226 bool startedHidlService = true;
Hans Boehmccdecaa2019-08-26 17:36:42 -0700227 if ((ret = mHwSvc->start()) != android::OK) {
Devin Moorec304b472022-04-08 21:42:50 +0000228 ALOGE("Unable to start HIDL NetdHwService: %d", ret);
229 startedHidlService = false;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700230 }
Devin Moorec304b472022-04-08 21:42:50 +0000231
Bernie Innocenti196f1b82019-05-20 16:34:16 +0900232 gLog.info("Registering NetdHwService: %" PRId64 "us", subTime.getTimeAndResetUs());
233 gLog.info("Netd started in %" PRId64 "us", s.timeTakenUs());
Devin Moorec304b472022-04-08 21:42:50 +0000234 if (startedHidlService) {
235 IPCThreadState::self()->joinThreadPool();
236 }
237 aidlService.join();
Erik Klineb31fd692018-06-06 20:50:11 +0900238 gLog.info("netd exiting");
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900239
San Mehatd1830422010-01-15 08:02:39 -0800240 exit(0);
241}