blob: c0a98945a42bb42ce4831802da35e58cc5aa7794 [file] [log] [blame]
Lorenzo Colittie4d626e2016-02-02 17:19:04 +09001/**
2 * Copyright (c) 2016, 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#define LOG_TAG "Netd"
18
Luke Huangcaebcbb2018-09-27 20:37:14 +080019#include <cinttypes>
Xiao Ma33d562a2018-12-16 16:27:38 +090020#include <numeric>
Ben Schwartz4204ecf2017-10-02 12:35:48 -040021#include <set>
Xiao Ma64b15b72019-03-20 11:33:15 +090022#include <string>
Erik Kline38e51f12018-09-06 20:14:44 +090023#include <tuple>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090024#include <vector>
25
Chenbo Fengf5663d82018-11-08 16:10:48 -080026#include <android-base/file.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090027#include <android-base/stringprintf.h>
Ben Schwartz4204ecf2017-10-02 12:35:48 -040028#include <android-base/strings.h>
Luke Huange3f11812019-05-02 18:10:15 +080029#include <binder/IPCThreadState.h>
30#include <binder/IServiceManager.h>
31#include <binder/Status.h>
Robin Lee2cf56172016-09-13 18:55:42 +090032#include <cutils/properties.h>
Logan Chien3f461482018-04-23 14:31:32 +080033#include <log/log.h>
Luke Huange3f11812019-05-02 18:10:15 +080034#include <netdutils/DumpWriter.h>
waynema5851b032021-11-24 17:08:25 +080035#include <netdutils/Utils.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090036#include <utils/Errors.h>
Pierre Imaibeedec32016-04-13 06:44:51 +090037#include <utils/String16.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090038
Lorenzo Colitti89faa342016-02-26 11:38:47 +090039#include "Controllers.h"
Chiachang Wang00fc62f2019-12-04 20:38:26 +080040#include "Fwmark.h"
Erik Kline55b06f82016-07-04 09:57:18 +090041#include "InterfaceController.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090042#include "NetdNativeService.h"
Luke Huang0e5e69d2019-03-06 15:42:38 +080043#include "OemNetdListener.h"
Luke Huangb670d162018-08-23 20:01:13 +080044#include "Permission.h"
Erik Kline85890042018-05-25 19:19:11 +090045#include "Process.h"
Robin Leeb8087362016-03-30 18:43:08 +010046#include "RouteController.h"
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090047#include "SockDiag.h"
Robin Leeb8087362016-03-30 18:43:08 +010048#include "UidRanges.h"
Xiao Ma33d562a2018-12-16 16:27:38 +090049#include "android/net/BnNetd.h"
Luke Huang743e0312020-05-26 17:21:28 +080050#include "binder_utils/BinderUtil.h"
51#include "binder_utils/NetdPermissions.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090052#include "netid_client.h" // NETID_UNSET
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090053
54using android::base::StringPrintf;
Chenbo Fengf5663d82018-11-08 16:10:48 -080055using android::base::WriteStringToFile;
Luke Huangcaebcbb2018-09-27 20:37:14 +080056using android::net::TetherStatsParcel;
Luke Huang94658ac2018-10-18 19:35:12 +090057using android::net::UidRangeParcel;
Ken Chen4ea88462021-05-23 14:56:43 +080058using android::net::netd::aidl::NativeUidRangeConfig;
Luke Huangb257d612019-03-14 21:19:13 +080059using android::netdutils::DumpWriter;
waynema5851b032021-11-24 17:08:25 +080060using android::netdutils::getIfaceNames;
Luke Huangb257d612019-03-14 21:19:13 +080061using android::netdutils::ScopedIndent;
Luke Huange203a152018-11-23 11:47:28 +080062using android::os::ParcelFileDescriptor;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090063
64namespace android {
65namespace net {
66
67namespace {
Erik Klineb31fd692018-06-06 20:50:11 +090068const char OPT_SHORT[] = "--short";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090069
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090070#define ENFORCE_ANY_PERMISSION(...) \
71 do { \
72 binder::Status status = checkAnyPermission({__VA_ARGS__}); \
73 if (!status.isOk()) { \
74 return status; \
75 } \
76 } while (0)
Robin Lee2cf56172016-09-13 18:55:42 +090077
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090078#define NETD_LOCKING_RPC(lock, ... /* permissions */) \
79 ENFORCE_ANY_PERMISSION(__VA_ARGS__); \
Bernie Innocentiabf8a342018-08-10 15:17:16 +090080 std::lock_guard _lock(lock);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090081
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090082#define NETD_BIG_LOCK_RPC(... /* permissions */) NETD_LOCKING_RPC(gBigNetdLock, __VA_ARGS__)
Lorenzo Colittid33e96d2016-12-15 23:59:01 +090083
Luke Huangf7782042018-08-08 13:13:04 +080084#define RETURN_BINDER_STATUS_IF_NOT_OK(logEntry, res) \
85 do { \
86 if (!isOk((res))) { \
87 logErrorStatus((logEntry), (res)); \
88 return asBinderStatus((res)); \
89 } \
90 } while (0)
91
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090092#define ENFORCE_NETWORK_STACK_PERMISSIONS() \
93 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK)
94
Luke Huangf7782042018-08-08 13:13:04 +080095void logErrorStatus(netdutils::LogEntry& logEntry, const netdutils::Status& status) {
96 gLog.log(logEntry.returns(status.code()).withAutomaticDuration());
97}
98
Bernie Innocenti97f388f2018-10-16 19:17:08 +090099binder::Status asBinderStatus(const netdutils::Status& status) {
100 if (isOk(status)) {
101 return binder::Status::ok();
102 }
103 return binder::Status::fromServiceSpecificError(status.code(), status.msg().c_str());
104}
105
Lorenzo Colittie801d3c2020-02-18 00:00:35 +0900106template <typename T>
107binder::Status asBinderStatus(const base::Result<T> result) {
108 if (result.ok()) return binder::Status::ok();
109
110 return binder::Status::fromServiceSpecificError(result.error().code(),
111 result.error().message().c_str());
112}
113
Erik Klineb31fd692018-06-06 20:50:11 +0900114bool contains(const Vector<String16>& words, const String16& word) {
115 for (const auto& w : words) {
116 if (w == word) return true;
117 }
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900118
Erik Klineb31fd692018-06-06 20:50:11 +0900119 return false;
120}
121
122} // namespace
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900123
Luke Huange3f11812019-05-02 18:10:15 +0800124NetdNativeService::NetdNativeService() {
125 // register log callback to BnNetd::logFunc
Jooyung Hanf2422582020-11-04 14:21:46 +0900126 BnNetd::logFunc = [](const auto& log) {
127 binderCallLogFn(log, [](const std::string& msg) { gLog.info("%s", msg.c_str()); });
128 };
Luke Huange3f11812019-05-02 18:10:15 +0800129}
130
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900131status_t NetdNativeService::start() {
132 IPCThreadState::self()->disableBackgroundScheduling(true);
Erik Klineb31fd692018-06-06 20:50:11 +0900133 const status_t ret = BinderService<NetdNativeService>::publish();
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900134 if (ret != android::OK) {
135 return ret;
136 }
137 sp<ProcessState> ps(ProcessState::self());
138 ps->startThreadPool();
139 ps->giveThreadPoolName();
Xiao Ma33d562a2018-12-16 16:27:38 +0900140
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900141 return android::OK;
142}
143
Hugo Benichi7b314e12018-01-15 21:54:00 +0900144status_t NetdNativeService::dump(int fd, const Vector<String16> &args) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900145 const binder::Status dump_permission = checkAnyPermission({PERM_DUMP});
Erik Kline2d3a1632016-03-15 16:33:48 +0900146 if (!dump_permission.isOk()) {
147 const String8 msg(dump_permission.toString8());
148 write(fd, msg.string(), msg.size());
149 return PERMISSION_DENIED;
150 }
151
152 // This method does not grab any locks. If individual classes need locking
153 // their dump() methods MUST handle locking appropriately.
Hugo Benichi7b314e12018-01-15 21:54:00 +0900154
Erik Kline2d3a1632016-03-15 16:33:48 +0900155 DumpWriter dw(fd);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900156
157 if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) {
158 dw.blankline();
159 gCtls->tcpSocketMonitor.dump(dw);
160 dw.blankline();
161 return NO_ERROR;
162 }
163
Erik Kline85890042018-05-25 19:19:11 +0900164 process::dump(dw);
Erik Kline2d3a1632016-03-15 16:33:48 +0900165 dw.blankline();
166 gCtls->netCtrl.dump(dw);
167 dw.blankline();
168
Benedict Wongaf855432018-05-10 17:07:37 -0700169 gCtls->xfrmCtrl.dump(dw);
170 dw.blankline();
171
Lorenzo Colitti52db3912020-02-17 23:59:45 +0900172 gCtls->tetherCtrl.dump(dw);
173 dw.blankline();
174
Erik Klineb31fd692018-06-06 20:50:11 +0900175 {
176 ScopedIndent indentLog(dw);
177 if (contains(args, String16(OPT_SHORT))) {
178 dw.println("Log: <omitted>");
179 } else {
180 dw.println("Log:");
181 ScopedIndent indentLogEntries(dw);
182 gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); });
183 }
184 dw.blankline();
185 }
186
Luke Huang528af602018-08-29 19:06:05 +0800187 {
188 ScopedIndent indentLog(dw);
189 if (contains(args, String16(OPT_SHORT))) {
190 dw.println("UnsolicitedLog: <omitted>");
191 } else {
192 dw.println("UnsolicitedLog:");
193 ScopedIndent indentLogEntries(dw);
194 gUnsolicitedLog.forEachEntry(
195 [&dw](const std::string& entry) mutable { dw.println(entry); });
196 }
197 dw.blankline();
198 }
199
Erik Kline2d3a1632016-03-15 16:33:48 +0900200 return NO_ERROR;
201}
202
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900203binder::Status NetdNativeService::isAlive(bool *alive) {
Luke Huangeedd6a52020-03-04 16:43:12 +0800204 NETD_BIG_LOCK_RPC(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900205
206 *alive = true;
Erik Klineb31fd692018-06-06 20:50:11 +0900207
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900208 return binder::Status::ok();
209}
210
Patrick Rohrc92891f2022-02-01 22:13:27 +0100211binder::Status NetdNativeService::firewallReplaceUidChain(const std::string&, bool,
212 const std::vector<int32_t>&, bool*) {
213 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900214}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900215
216binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
Luke Huangeedd6a52020-03-04 16:43:12 +0800217 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900218 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
219 *ret = (err == 0);
220 return binder::Status::ok();
221}
222
Luke Huang531f5d32018-08-03 15:19:05 +0800223binder::Status NetdNativeService::bandwidthSetInterfaceQuota(const std::string& ifName,
224 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900225 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800226 int res = gCtls->bandwidthCtrl.setInterfaceQuota(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800227 return statusFromErrcode(res);
228}
229
230binder::Status NetdNativeService::bandwidthRemoveInterfaceQuota(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900231 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800232 int res = gCtls->bandwidthCtrl.removeInterfaceQuota(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800233 return statusFromErrcode(res);
234}
235
236binder::Status NetdNativeService::bandwidthSetInterfaceAlert(const std::string& ifName,
237 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900238 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800239 int res = gCtls->bandwidthCtrl.setInterfaceAlert(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800240 return statusFromErrcode(res);
241}
242
243binder::Status NetdNativeService::bandwidthRemoveInterfaceAlert(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900244 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800245 int res = gCtls->bandwidthCtrl.removeInterfaceAlert(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800246 return statusFromErrcode(res);
247}
248
249binder::Status NetdNativeService::bandwidthSetGlobalAlert(int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900250 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800251 int res = gCtls->bandwidthCtrl.setGlobalAlert(bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800252 return statusFromErrcode(res);
253}
254
Patrick Rohrc92891f2022-02-01 22:13:27 +0100255binder::Status NetdNativeService::bandwidthAddNaughtyApp(int32_t) {
256 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
waynemadc07cd42021-11-03 10:11:59 +0800257}
258
Patrick Rohrc92891f2022-02-01 22:13:27 +0100259binder::Status NetdNativeService::bandwidthRemoveNaughtyApp(int32_t) {
260 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
waynemadc07cd42021-11-03 10:11:59 +0800261}
262
Patrick Rohrc92891f2022-02-01 22:13:27 +0100263binder::Status NetdNativeService::bandwidthAddNiceApp(int32_t) {
264 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
waynemadc07cd42021-11-03 10:11:59 +0800265}
266
Patrick Rohrc92891f2022-02-01 22:13:27 +0100267binder::Status NetdNativeService::bandwidthRemoveNiceApp(int32_t) {
268 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Luke Huang531f5d32018-08-03 15:19:05 +0800269}
270
Ken Chenab5f3472021-04-04 11:28:06 +0800271// TODO: Remove this function when there are no users. Currently, it is still used by DNS resolver
272// tests.
Luke Huangb670d162018-08-23 20:01:13 +0800273binder::Status NetdNativeService::networkCreatePhysical(int32_t netId, int32_t permission) {
Luke Huangeedd6a52020-03-04 16:43:12 +0800274 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chalard Jean04845142022-12-02 19:39:36 +0900275 int ret = gCtls->netCtrl.createPhysicalNetwork(netId, convertPermission(permission),
276 false /* local */);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900277 return statusFromErrcode(ret);
278}
279
Ken Chenab5f3472021-04-04 11:28:06 +0800280// TODO: Remove this function when there are no users. Currently, it is still used by DNS resolver
281// tests.
cken67cd14c2018-12-05 17:26:59 +0900282binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool secure) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900283 ENFORCE_NETWORK_STACK_PERMISSIONS();
Ken Chenab5f3472021-04-04 11:28:06 +0800284 // The value of vpnType does not matter here, because it is not used in AOSP and is only
285 // implemented by OEMs. Also, the RPC is going to deprecate. Just pick a value defined in INetd
286 // as default.
Chiachang Wang2b0abee2022-01-12 10:03:17 +0800287 int ret = gCtls->netCtrl.createVirtualNetwork(netId, secure, NativeVpnType::LEGACY,
288 false /* excludeLocalRoutes */);
Ken Chenab5f3472021-04-04 11:28:06 +0800289 return statusFromErrcode(ret);
290}
291
292binder::Status NetdNativeService::networkCreate(const NativeNetworkConfig& config) {
293 ENFORCE_NETWORK_STACK_PERMISSIONS();
294 int ret = -EINVAL;
295 if (config.networkType == NativeNetworkType::PHYSICAL) {
Chalard Jean04845142022-12-02 19:39:36 +0900296 ret = gCtls->netCtrl.createPhysicalNetwork(
297 config.netId, convertPermission(config.permission), false /* isLocalNetwork */);
298 } else if (config.networkType == NativeNetworkType::PHYSICAL_LOCAL) {
299 ret = gCtls->netCtrl.createPhysicalNetwork(
300 config.netId, convertPermission(config.permission), true /* isLocalNetwork */);
Ken Chenab5f3472021-04-04 11:28:06 +0800301 } else if (config.networkType == NativeNetworkType::VIRTUAL) {
Chiachang Wang2b0abee2022-01-12 10:03:17 +0800302 ret = gCtls->netCtrl.createVirtualNetwork(config.netId, config.secure, config.vpnType,
303 config.excludeLocalRoutes);
Ken Chenab5f3472021-04-04 11:28:06 +0800304 }
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900305 return statusFromErrcode(ret);
306}
307
308binder::Status NetdNativeService::networkDestroy(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900309 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangd33a8f42019-03-14 16:10:04 +0800310 // NetworkController::destroyNetwork is thread-safe.
Mike Yu4fe1b9c2019-01-29 17:50:19 +0800311 const int ret = gCtls->netCtrl.destroyNetwork(netId);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900312 return statusFromErrcode(ret);
313}
314
315binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
Luke Huangeedd6a52020-03-04 16:43:12 +0800316 ENFORCE_NETWORK_STACK_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900317 int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
318 return statusFromErrcode(ret);
319}
320
321binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
Luke Huangeedd6a52020-03-04 16:43:12 +0800322 ENFORCE_NETWORK_STACK_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900323 int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
324 return statusFromErrcode(ret);
325}
326
Luke Huang94658ac2018-10-18 19:35:12 +0900327binder::Status NetdNativeService::networkAddUidRanges(
328 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900329 // NetworkController::addUsersToNetwork is thread-safe.
Luke Huangeedd6a52020-03-04 16:43:12 +0800330 ENFORCE_NETWORK_STACK_PERMISSIONS();
Ken Chen4ea88462021-05-23 14:56:43 +0800331 int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray),
Patrick Rohre2f1b5a2022-01-25 21:36:50 +0100332 UidRanges::SUB_PRIORITY_HIGHEST);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900333 return statusFromErrcode(ret);
334}
335
Luke Huang94658ac2018-10-18 19:35:12 +0900336binder::Status NetdNativeService::networkRemoveUidRanges(
337 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900338 // NetworkController::removeUsersFromNetwork is thread-safe.
Luke Huangeedd6a52020-03-04 16:43:12 +0800339 ENFORCE_NETWORK_STACK_PERMISSIONS();
Ken Chen4ea88462021-05-23 14:56:43 +0800340 int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray),
Patrick Rohre2f1b5a2022-01-25 21:36:50 +0100341 UidRanges::SUB_PRIORITY_HIGHEST);
Ken Chen4ea88462021-05-23 14:56:43 +0800342 return statusFromErrcode(ret);
343}
344
345binder::Status NetdNativeService::networkAddUidRangesParcel(const NativeUidRangeConfig& config) {
346 ENFORCE_NETWORK_STACK_PERMISSIONS();
347 int ret = gCtls->netCtrl.addUsersToNetwork(config.netId, UidRanges(config.uidRanges),
348 config.subPriority);
349 return statusFromErrcode(ret);
350}
351
352binder::Status NetdNativeService::networkRemoveUidRangesParcel(const NativeUidRangeConfig& config) {
353 ENFORCE_NETWORK_STACK_PERMISSIONS();
354 int ret = gCtls->netCtrl.removeUsersFromNetwork(config.netId, UidRanges(config.uidRanges),
355 config.subPriority);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900356 return statusFromErrcode(ret);
357}
358
Luke Huang94658ac2018-10-18 19:35:12 +0900359binder::Status NetdNativeService::networkRejectNonSecureVpn(
360 bool add, const std::vector<UidRangeParcel>& uidRangeArray) {
Robin Leeb8087362016-03-30 18:43:08 +0100361 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
362 // it should be possible to use the same lock as NetworkController. However, every call through
363 // the CommandListener "network" command will need to hold this lock too, not just the ones that
364 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
365 // look at routes, but it's not enough here).
Luke Huangeedd6a52020-03-04 16:43:12 +0800366 NETD_BIG_LOCK_RPC(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900367 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100368
369 int err;
370 if (add) {
371 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
372 } else {
373 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
374 }
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900375 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100376}
377
Luke Huang94658ac2018-10-18 19:35:12 +0900378binder::Status NetdNativeService::socketDestroy(const std::vector<UidRangeParcel>& uids,
379 const std::vector<int32_t>& skipUids) {
Luke Huangeedd6a52020-03-04 16:43:12 +0800380 ENFORCE_NETWORK_STACK_PERMISSIONS();
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900381
382 SockDiag sd;
383 if (!sd.open()) {
384 return binder::Status::fromServiceSpecificError(EIO,
385 String8("Could not open SOCK_DIAG socket"));
386 }
387
388 UidRanges uidRanges(uids);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +0900389 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
390 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900391 if (err) {
392 return binder::Status::fromServiceSpecificError(-err,
393 String8::format("destroySockets: %s", strerror(-err)));
394 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900395 return binder::Status::ok();
396}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900397
Erik Klinef48e4dd2016-07-18 04:02:07 +0900398binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900399 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Erik Klinef48e4dd2016-07-18 04:02:07 +0900400 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
401 return binder::Status::ok();
402}
403
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900404namespace {
405
Hungming Chenf40dc092020-03-12 16:21:03 +0800406constexpr const int UNUSED_IFINDEX = 0;
407
Luke Huangcaebcbb2018-09-27 20:37:14 +0800408void tetherAddStatsByInterface(TetherController::TetherStats* tetherStatsParcel,
409 const TetherController::TetherStats& tetherStats) {
410 if (tetherStatsParcel->extIface == tetherStats.extIface) {
411 tetherStatsParcel->rxBytes += tetherStats.rxBytes;
412 tetherStatsParcel->rxPackets += tetherStats.rxPackets;
413 tetherStatsParcel->txBytes += tetherStats.txBytes;
414 tetherStatsParcel->txPackets += tetherStats.txPackets;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900415 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800416}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900417
Luke Huangcaebcbb2018-09-27 20:37:14 +0800418TetherStatsParcel toTetherStatsParcel(const TetherController::TetherStats& stats) {
419 TetherStatsParcel result;
420 result.iface = stats.extIface;
421 result.rxBytes = stats.rxBytes;
422 result.rxPackets = stats.rxPackets;
423 result.txBytes = stats.txBytes;
424 result.txPackets = stats.txPackets;
Hungming Chenf40dc092020-03-12 16:21:03 +0800425 result.ifIndex = UNUSED_IFINDEX;
Luke Huangcaebcbb2018-09-27 20:37:14 +0800426 return result;
427}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900428
Luke Huangcaebcbb2018-09-27 20:37:14 +0800429void setTetherStatsParcelVecByInterface(std::vector<TetherStatsParcel>* tetherStatsVec,
430 const TetherController::TetherStatsList& statsList) {
431 std::map<std::string, TetherController::TetherStats> statsMap;
432 for (const auto& stats : statsList) {
433 auto iter = statsMap.find(stats.extIface);
434 if (iter != statsMap.end()) {
435 tetherAddStatsByInterface(&(iter->second), stats);
436 } else {
437 statsMap.insert(
438 std::pair<std::string, TetherController::TetherStats>(stats.extIface, stats));
439 }
440 }
441 for (auto iter = statsMap.begin(); iter != statsMap.end(); iter++) {
442 tetherStatsVec->push_back(toTetherStatsParcel(iter->second));
443 }
444}
445
446std::vector<std::string> tetherStatsParcelVecToStringVec(std::vector<TetherStatsParcel>* tVec) {
447 std::vector<std::string> result;
448 for (const auto& t : *tVec) {
449 result.push_back(StringPrintf("%s:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64,
450 t.iface.c_str(), t.rxBytes, t.rxPackets, t.txBytes,
451 t.txPackets));
452 }
453 return result;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900454}
455
456} // namespace
457
Luke Huangcaebcbb2018-09-27 20:37:14 +0800458binder::Status NetdNativeService::tetherGetStats(
459 std::vector<TetherStatsParcel>* tetherStatsParcelVec) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900460 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900461 const auto& statsList = gCtls->tetherCtrl.getTetherStats();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900462 if (!isOk(statsList)) {
Nathan Harold28ccfef2018-03-16 19:02:47 -0700463 return asBinderStatus(statsList);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900464 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800465 setTetherStatsParcelVecByInterface(tetherStatsParcelVec, statsList.value());
466 auto statsResults = tetherStatsParcelVecToStringVec(tetherStatsParcelVec);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900467 return binder::Status::ok();
468}
469
Erik Kline53c20882016-08-02 15:22:53 +0900470binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
471 const std::string &addrString, int prefixLength) {
Luke Huangeedd6a52020-03-04 16:43:12 +0800472 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900473 const int err = InterfaceController::addAddress(
474 ifName.c_str(), addrString.c_str(), prefixLength);
475 if (err != 0) {
476 return binder::Status::fromServiceSpecificError(-err,
477 String8::format("InterfaceController error: %s", strerror(-err)));
478 }
479 return binder::Status::ok();
480}
481
482binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
483 const std::string &addrString, int prefixLength) {
Luke Huangeedd6a52020-03-04 16:43:12 +0800484 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900485 const int err = InterfaceController::delAddress(
486 ifName.c_str(), addrString.c_str(), prefixLength);
487 if (err != 0) {
488 return binder::Status::fromServiceSpecificError(-err,
489 String8::format("InterfaceController error: %s", strerror(-err)));
490 }
491 return binder::Status::ok();
492}
493
Erik Kline38e51f12018-09-06 20:14:44 +0900494namespace {
Erik Kline55b06f82016-07-04 09:57:18 +0900495
Erik Kline38e51f12018-09-06 20:14:44 +0900496std::tuple<binder::Status, const char*, const char*> getPathComponents(int32_t ipversion,
497 int32_t category) {
498 const char* ipversionStr = nullptr;
499 switch (ipversion) {
Erik Kline55b06f82016-07-04 09:57:18 +0900500 case INetd::IPV4:
Erik Kline38e51f12018-09-06 20:14:44 +0900501 ipversionStr = "ipv4";
Erik Kline55b06f82016-07-04 09:57:18 +0900502 break;
503 case INetd::IPV6:
Erik Kline38e51f12018-09-06 20:14:44 +0900504 ipversionStr = "ipv6";
Erik Kline55b06f82016-07-04 09:57:18 +0900505 break;
506 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900507 return {binder::Status::fromServiceSpecificError(EAFNOSUPPORT, "Bad IP version"),
508 nullptr, nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900509 }
510
Erik Kline38e51f12018-09-06 20:14:44 +0900511 const char* whichStr = nullptr;
512 switch (category) {
Erik Kline55b06f82016-07-04 09:57:18 +0900513 case INetd::CONF:
514 whichStr = "conf";
515 break;
516 case INetd::NEIGH:
517 whichStr = "neigh";
518 break;
519 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900520 return {binder::Status::fromServiceSpecificError(EINVAL, "Bad category"), nullptr,
521 nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900522 }
523
Erik Kline38e51f12018-09-06 20:14:44 +0900524 return {binder::Status::ok(), ipversionStr, whichStr};
525}
526
527} // namespace
528
529binder::Status NetdNativeService::getProcSysNet(int32_t ipversion, int32_t which,
530 const std::string& ifname,
531 const std::string& parameter, std::string* value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900532 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900533 const auto pathParts = getPathComponents(ipversion, which);
534 const auto& pathStatus = std::get<0>(pathParts);
535 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900536 return pathStatus;
Erik Kline55b06f82016-07-04 09:57:18 +0900537 }
Erik Kline38e51f12018-09-06 20:14:44 +0900538
539 const int err = InterfaceController::getParameter(std::get<1>(pathParts),
540 std::get<2>(pathParts), ifname.c_str(),
541 parameter.c_str(), value);
Erik Kline38e51f12018-09-06 20:14:44 +0900542 return statusFromErrcode(err);
543}
544
545binder::Status NetdNativeService::setProcSysNet(int32_t ipversion, int32_t which,
546 const std::string& ifname,
547 const std::string& parameter,
548 const std::string& value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900549 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900550 const auto pathParts = getPathComponents(ipversion, which);
551 const auto& pathStatus = std::get<0>(pathParts);
552 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900553 return pathStatus;
554 }
555
556 const int err = InterfaceController::setParameter(std::get<1>(pathParts),
557 std::get<2>(pathParts), ifname.c_str(),
558 parameter.c_str(), value.c_str());
Erik Kline38e51f12018-09-06 20:14:44 +0900559 return statusFromErrcode(err);
Erik Kline55b06f82016-07-04 09:57:18 +0900560}
561
Luke Huange203a152018-11-23 11:47:28 +0800562binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const ParcelFileDescriptor& socket,
563 int newUid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900564 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wongb2daefb2017-12-06 22:05:46 -0800565
566 uid_t callerUid = IPCThreadState::self()->getCallingUid();
Luke Huange203a152018-11-23 11:47:28 +0800567 return asBinderStatus(
568 gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket.get(), newUid, callerUid));
Benedict Wongb2daefb2017-12-06 22:05:46 -0800569}
570
Nathan Harold1a371532017-01-30 12:30:48 -0800571binder::Status NetdNativeService::ipSecAllocateSpi(
572 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800573 const std::string& sourceAddress,
574 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800575 int32_t inSpi,
576 int32_t* outSpi) {
577 // Necessary locking done in IpSecService and kernel
Luke Huangeedd6a52020-03-04 16:43:12 +0800578 ENFORCE_NETWORK_STACK_PERMISSIONS();
ludi6e8eccd2017-08-14 14:40:37 -0700579 return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi(
Nathan Harold1a371532017-01-30 12:30:48 -0800580 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800581 sourceAddress,
582 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800583 inSpi,
584 outSpi));
585}
586
587binder::Status NetdNativeService::ipSecAddSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700588 int32_t transformId, int32_t mode, const std::string& sourceAddress,
589 const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi,
590 int32_t markValue, int32_t markMask, const std::string& authAlgo,
591 const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo,
592 const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo,
593 const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType,
594 int32_t encapLocalPort, int32_t encapRemotePort, int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800595 // Necessary locking done in IpSecService and kernel
Luke Huangeedd6a52020-03-04 16:43:12 +0800596 ENFORCE_NETWORK_STACK_PERMISSIONS();
ludi6e8eccd2017-08-14 14:40:37 -0700597 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation(
Benedict Wongad600cb2018-05-14 17:22:35 -0700598 transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue,
599 markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits,
Benedict Wonga450e722018-05-07 10:29:02 -0700600 aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort,
601 interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800602}
603
604binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700605 int32_t transformId, const std::string& sourceAddress,
606 const std::string& destinationAddress, int32_t spi, int32_t markValue, int32_t markMask,
607 int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800608 // Necessary locking done in IpSecService and kernel
Luke Huangeedd6a52020-03-04 16:43:12 +0800609 ENFORCE_NETWORK_STACK_PERMISSIONS();
ludi6e8eccd2017-08-14 14:40:37 -0700610 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700611 transformId, sourceAddress, destinationAddress, spi, markValue, markMask, interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800612}
613
614binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800615 const ParcelFileDescriptor& socket, int32_t transformId, int32_t direction,
616 const std::string& sourceAddress, const std::string& destinationAddress, int32_t spi) {
Nathan Harold1a371532017-01-30 12:30:48 -0800617 // Necessary locking done in IpSecService and kernel
Luke Huangeedd6a52020-03-04 16:43:12 +0800618 ENFORCE_NETWORK_STACK_PERMISSIONS();
ludi6e8eccd2017-08-14 14:40:37 -0700619 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800620 socket.get(), transformId, direction, sourceAddress, destinationAddress, spi));
Nathan Harold1a371532017-01-30 12:30:48 -0800621}
622
623binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800624 const ParcelFileDescriptor& socket) {
Nathan Harold1a371532017-01-30 12:30:48 -0800625 // Necessary locking done in IpSecService and kernel
Luke Huangeedd6a52020-03-04 16:43:12 +0800626 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huange203a152018-11-23 11:47:28 +0800627 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(socket.get()));
Nathan Harold1a371532017-01-30 12:30:48 -0800628}
629
Benedict Wonga04ffa72018-05-09 21:42:42 -0700630binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
631 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700632 const std::string& tmplSrcAddress,
633 const std::string& tmplDstAddress,
634 int32_t spi, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700635 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800636 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900637 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong84a8dca2018-01-19 12:12:17 -0800638 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700639 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700640 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800641}
642
Benedict Wonga450e722018-05-07 10:29:02 -0700643binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(
644 int32_t transformId, int32_t selAddrFamily, int32_t direction,
645 const std::string& tmplSrcAddress, const std::string& tmplDstAddress, int32_t spi,
646 int32_t markValue, int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800647 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900648 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong84a8dca2018-01-19 12:12:17 -0800649 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700650 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700651 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800652}
653
Benedict Wonga04ffa72018-05-09 21:42:42 -0700654binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId,
655 int32_t selAddrFamily,
656 int32_t direction, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700657 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800658 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900659 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong84a8dca2018-01-19 12:12:17 -0800660 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
Benedict Wonga450e722018-05-07 10:29:02 -0700661 transformId, selAddrFamily, direction, markValue, markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800662}
663
Benedict Wong319f17e2018-05-15 17:06:44 -0700664binder::Status NetdNativeService::ipSecAddTunnelInterface(const std::string& deviceName,
665 const std::string& localAddress,
666 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700667 int32_t iKey, int32_t oKey,
668 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800669 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900670 ENFORCE_NETWORK_STACK_PERMISSIONS();
Xiao Mace45dcc2022-10-13 17:23:25 +0900671 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddTunnelInterface(
672 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, false));
manojboopathi8707f232018-01-02 14:45:47 -0800673}
674
Benedict Wong319f17e2018-05-15 17:06:44 -0700675binder::Status NetdNativeService::ipSecUpdateTunnelInterface(const std::string& deviceName,
676 const std::string& localAddress,
677 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700678 int32_t iKey, int32_t oKey,
679 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800680 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900681 ENFORCE_NETWORK_STACK_PERMISSIONS();
Xiao Mace45dcc2022-10-13 17:23:25 +0900682 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddTunnelInterface(
683 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, true));
manojboopathi8707f232018-01-02 14:45:47 -0800684}
685
Benedict Wong319f17e2018-05-15 17:06:44 -0700686binder::Status NetdNativeService::ipSecRemoveTunnelInterface(const std::string& deviceName) {
manojboopathi8707f232018-01-02 14:45:47 -0800687 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900688 ENFORCE_NETWORK_STACK_PERMISSIONS();
Xiao Mace45dcc2022-10-13 17:23:25 +0900689 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTunnelInterface(deviceName));
manojboopathi8707f232018-01-02 14:45:47 -0800690}
691
Yan Yanf9c46792022-10-13 00:25:43 +0000692binder::Status NetdNativeService::ipSecMigrate(const IpSecMigrateInfoParcel& migrateInfo) {
693 // Necessary locking done in IpSecService and kernel
694 ENFORCE_NETWORK_STACK_PERMISSIONS();
695 return asBinderStatus(gCtls->xfrmCtrl.ipSecMigrate(
696 migrateInfo.requestId, migrateInfo.selAddrFamily, migrateInfo.direction,
697 migrateInfo.oldSourceAddress, migrateInfo.oldDestinationAddress,
698 migrateInfo.newSourceAddress, migrateInfo.newDestinationAddress,
699 migrateInfo.interfaceId));
700}
701
Joel Scherpelzde937962017-06-01 13:20:21 +0900702binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
703 int32_t mode) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900704 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700705 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900706}
707
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900708binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName,
709 const std::string& prefix, int32_t mark,
710 int32_t mask) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900711 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700712 return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900713}
714
715binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
716 const std::string& prefix, int32_t mark,
717 int32_t mask) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900718 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700719 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900720}
721
Chenbo Feng873ae142019-04-10 12:26:06 -0700722binder::Status NetdNativeService::trafficSwapActiveStatsMap() {
Patrick Rohrc92891f2022-02-01 22:13:27 +0100723 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Chenbo Feng873ae142019-04-10 12:26:06 -0700724}
725
Luke Huang0051a622018-07-23 20:30:16 +0800726binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout,
727 const std::string& classLabel) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900728 NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang0051a622018-07-23 20:30:16 +0800729 int res =
730 gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str());
Luke Huang0051a622018-07-23 20:30:16 +0800731 return statusFromErrcode(res);
732}
733
734binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName,
735 int32_t timeout,
736 const std::string& classLabel) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900737 NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang0051a622018-07-23 20:30:16 +0800738 int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout,
739 classLabel.c_str());
Luke Huang0051a622018-07-23 20:30:16 +0800740 return statusFromErrcode(res);
741}
Luke Huanga67dd562018-07-17 19:58:25 +0800742
743binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900744 NETD_LOCKING_RPC(gCtls->strictCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huanga67dd562018-07-17 19:58:25 +0800745 StrictPenalty penalty;
746 switch (policyPenalty) {
747 case INetd::PENALTY_POLICY_REJECT:
748 penalty = REJECT;
749 break;
750 case INetd::PENALTY_POLICY_LOG:
751 penalty = LOG;
752 break;
753 case INetd::PENALTY_POLICY_ACCEPT:
754 penalty = ACCEPT;
755 break;
756 default:
757 return statusFromErrcode(-EINVAL);
758 break;
759 }
760 int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty);
Luke Huanga67dd562018-07-17 19:58:25 +0800761 return statusFromErrcode(res);
762}
Luke Huange64fa382018-07-24 16:38:22 +0800763
Hungming Chenf0ac3df2022-04-22 15:25:23 +0800764// TODO: remark @deprecated in INetd.aidl.
765binder::Status NetdNativeService::clatdStart(const std::string& /* ifName */,
766 const std::string& /* nat64Prefix */,
767 std::string* /* v6Addr */) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700768 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Hungming Chenf0ac3df2022-04-22 15:25:23 +0800769 // deprecated
770 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Luke Huang6d301232018-08-01 14:05:18 +0800771}
772
Hungming Chenf0ac3df2022-04-22 15:25:23 +0800773// TODO: remark @deprecated in INetd.aidl.
774binder::Status NetdNativeService::clatdStop(const std::string& /* ifName */) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700775 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Hungming Chenf0ac3df2022-04-22 15:25:23 +0800776 // deprecated
777 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Luke Huang6d301232018-08-01 14:05:18 +0800778}
Luke Huanga67dd562018-07-17 19:58:25 +0800779
Luke Huang457d4702018-08-16 15:39:15 +0800780binder::Status NetdNativeService::ipfwdEnabled(bool* status) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900781 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang728cf4c2019-03-14 19:43:02 +0800782 *status = (gCtls->tetherCtrl.getIpfwdRequesterList().size() > 0) ? true : false;
783 return binder::Status::ok();
784}
785
786binder::Status NetdNativeService::ipfwdGetRequesterList(std::vector<std::string>* requesterList) {
787 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
788 for (const auto& requester : gCtls->tetherCtrl.getIpfwdRequesterList()) {
789 requesterList->push_back(requester);
790 }
Luke Huang457d4702018-08-16 15:39:15 +0800791 return binder::Status::ok();
792}
793
794binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900795 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang457d4702018-08-16 15:39:15 +0800796 int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
Luke Huang457d4702018-08-16 15:39:15 +0800797 return statusFromErrcode(res);
798}
799
800binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900801 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang457d4702018-08-16 15:39:15 +0800802 int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
Luke Huang457d4702018-08-16 15:39:15 +0800803 return statusFromErrcode(res);
804}
805
806binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface,
807 const std::string& toIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900808 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang457d4702018-08-16 15:39:15 +0800809 int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str());
Luke Huang457d4702018-08-16 15:39:15 +0800810 return statusFromErrcode(res);
811}
812
813binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface,
814 const std::string& toIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900815 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang457d4702018-08-16 15:39:15 +0800816 int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str());
Luke Huang457d4702018-08-16 15:39:15 +0800817 return statusFromErrcode(res);
818}
819
Luke Huangf7782042018-08-08 13:13:04 +0800820namespace {
Luke Huangeedd6a52020-03-04 16:43:12 +0800821
Luke Huangf7782042018-08-08 13:13:04 +0800822std::string addCurlyBrackets(const std::string& s) {
823 return "{" + s + "}";
824}
825
826} // namespace
Xiao Ma33d562a2018-12-16 16:27:38 +0900827
Luke Huangf7782042018-08-08 13:13:04 +0800828binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900829 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
waynema5851b032021-11-24 17:08:25 +0800830 const auto& ifaceList = getIfaceNames();
Luke Huangf7782042018-08-08 13:13:04 +0800831
832 interfaceListResult->clear();
833 interfaceListResult->reserve(ifaceList.value().size());
834 interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()),
835 end(ifaceList.value()));
836
Luke Huangf7782042018-08-08 13:13:04 +0800837 return binder::Status::ok();
838}
839
840std::string interfaceConfigurationParcelToString(const InterfaceConfigurationParcel& cfg) {
841 std::vector<std::string> result{cfg.ifName, cfg.hwAddr, cfg.ipv4Addr,
842 std::to_string(cfg.prefixLength)};
843 result.insert(end(result), begin(cfg.flags), end(cfg.flags));
844 return addCurlyBrackets(base::Join(result, ", "));
845}
846
847binder::Status NetdNativeService::interfaceGetCfg(
848 const std::string& ifName, InterfaceConfigurationParcel* interfaceGetCfgResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900849 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800850 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
851
852 const auto& cfgRes = InterfaceController::getCfg(ifName);
853 RETURN_BINDER_STATUS_IF_NOT_OK(entry, cfgRes);
854
855 *interfaceGetCfgResult = cfgRes.value();
856 gLog.log(entry.returns(interfaceConfigurationParcelToString(*interfaceGetCfgResult))
857 .withAutomaticDuration());
858 return binder::Status::ok();
859}
860
861binder::Status NetdNativeService::interfaceSetCfg(const InterfaceConfigurationParcel& cfg) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900862 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800863 auto entry = gLog.newEntry()
864 .prettyFunction(__PRETTY_FUNCTION__)
865 .arg(interfaceConfigurationParcelToString(cfg));
866
867 const auto& res = InterfaceController::setCfg(cfg);
868 RETURN_BINDER_STATUS_IF_NOT_OK(entry, res);
869
870 gLog.log(entry.withAutomaticDuration());
871 return binder::Status::ok();
872}
873
874binder::Status NetdNativeService::interfaceSetIPv6PrivacyExtensions(const std::string& ifName,
875 bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900876 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800877 int res = InterfaceController::setIPv6PrivacyExtensions(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800878 return statusFromErrcode(res);
879}
880
881binder::Status NetdNativeService::interfaceClearAddrs(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900882 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800883 int res = InterfaceController::clearAddrs(ifName.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800884 return statusFromErrcode(res);
885}
886
887binder::Status NetdNativeService::interfaceSetEnableIPv6(const std::string& ifName, bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900888 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800889 int res = InterfaceController::setEnableIPv6(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800890 return statusFromErrcode(res);
891}
892
893binder::Status NetdNativeService::interfaceSetMtu(const std::string& ifName, int32_t mtuValue) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900894 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800895 std::string mtu = std::to_string(mtuValue);
896 int res = InterfaceController::setMtu(ifName.c_str(), mtu.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800897 return statusFromErrcode(res);
898}
899
Luke Huangb5733d72018-08-21 17:17:19 +0800900binder::Status NetdNativeService::tetherStart(const std::vector<std::string>& dhcpRanges) {
Chiachang Wang08cb2112019-12-10 09:53:24 +0800901 TetherConfigParcel config;
902 config.usingLegacyDnsProxy = true;
903 config.dhcpRanges = dhcpRanges;
904 return tetherStartWithConfiguration(config);
Luke Huang91bd3e12019-08-20 11:33:52 +0800905}
906
Chiachang Wang08cb2112019-12-10 09:53:24 +0800907binder::Status NetdNativeService::tetherStartWithConfiguration(const TetherConfigParcel& config) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900908 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Chiachang Wang08cb2112019-12-10 09:53:24 +0800909 if (config.dhcpRanges.size() % 2 == 1) {
Luke Huangb5733d72018-08-21 17:17:19 +0800910 return statusFromErrcode(-EINVAL);
911 }
Chiachang Wang08cb2112019-12-10 09:53:24 +0800912 // TODO: Pass TetherConfigParcel directly.
913 int res = gCtls->tetherCtrl.startTethering(config.usingLegacyDnsProxy, config.dhcpRanges);
Luke Huangb5733d72018-08-21 17:17:19 +0800914 return statusFromErrcode(res);
915}
916
917binder::Status NetdNativeService::tetherStop() {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900918 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800919 int res = gCtls->tetherCtrl.stopTethering();
Luke Huangb5733d72018-08-21 17:17:19 +0800920 return statusFromErrcode(res);
921}
922
923binder::Status NetdNativeService::tetherIsEnabled(bool* enabled) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900924 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800925 *enabled = gCtls->tetherCtrl.isTetheringStarted();
Luke Huangb5733d72018-08-21 17:17:19 +0800926 return binder::Status::ok();
927}
928
929binder::Status NetdNativeService::tetherInterfaceAdd(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900930 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800931 int res = gCtls->tetherCtrl.tetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800932 return statusFromErrcode(res);
933}
934
935binder::Status NetdNativeService::tetherInterfaceRemove(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900936 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800937 int res = gCtls->tetherCtrl.untetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800938 return statusFromErrcode(res);
939}
940
941binder::Status NetdNativeService::tetherInterfaceList(std::vector<std::string>* ifList) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900942 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800943 for (const auto& ifname : gCtls->tetherCtrl.getTetheredInterfaceList()) {
944 ifList->push_back(ifname);
945 }
Luke Huangb5733d72018-08-21 17:17:19 +0800946 return binder::Status::ok();
947}
948
949binder::Status NetdNativeService::tetherDnsSet(int32_t netId,
950 const std::vector<std::string>& dnsAddrs) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900951 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800952 int res = gCtls->tetherCtrl.setDnsForwarders(netId, dnsAddrs);
Luke Huangb5733d72018-08-21 17:17:19 +0800953 return statusFromErrcode(res);
954}
955
956binder::Status NetdNativeService::tetherDnsList(std::vector<std::string>* dnsList) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900957 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800958 for (const auto& fwdr : gCtls->tetherCtrl.getDnsForwarders()) {
959 dnsList->push_back(fwdr);
960 }
Luke Huangb5733d72018-08-21 17:17:19 +0800961 return binder::Status::ok();
962}
963
Tyler Wearfa94a272019-12-05 15:01:48 -0800964binder::Status NetdNativeService::networkAddRouteParcel(int32_t netId,
965 const RouteInfoParcel& route) {
966 // Public methods of NetworkController are thread-safe.
967 ENFORCE_NETWORK_STACK_PERMISSIONS();
968 bool legacy = false;
969 uid_t uid = 0; // UID is only meaningful for legacy routes.
970
971 // convert Parcel to parameters
972 int res = gCtls->netCtrl.addRoute(netId, route.ifName.c_str(), route.destination.c_str(),
973 route.nextHop.empty() ? nullptr : route.nextHop.c_str(),
974 legacy, uid, route.mtu);
975 return statusFromErrcode(res);
976}
977
978binder::Status NetdNativeService::networkUpdateRouteParcel(int32_t netId,
979 const RouteInfoParcel& route) {
980 // Public methods of NetworkController are thread-safe.
981 ENFORCE_NETWORK_STACK_PERMISSIONS();
982 bool legacy = false;
983 uid_t uid = 0; // UID is only meaningful for legacy routes.
984
985 // convert Parcel to parameters
986 int res = gCtls->netCtrl.updateRoute(netId, route.ifName.c_str(), route.destination.c_str(),
987 route.nextHop.empty() ? nullptr : route.nextHop.c_str(),
988 legacy, uid, route.mtu);
989 return statusFromErrcode(res);
990}
991
992binder::Status NetdNativeService::networkRemoveRouteParcel(int32_t netId,
993 const RouteInfoParcel& route) {
994 return networkRemoveRoute(netId, route.ifName, route.destination, route.nextHop);
995}
996
Luke Huangb670d162018-08-23 20:01:13 +0800997binder::Status NetdNativeService::networkAddRoute(int32_t netId, const std::string& ifName,
998 const std::string& destination,
999 const std::string& nextHop) {
1000 // Public methods of NetworkController are thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001001 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001002 bool legacy = false;
1003 uid_t uid = 0; // UID is only meaningful for legacy routes.
1004 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
Tyler Wearfa94a272019-12-05 15:01:48 -08001005 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid, 0);
Luke Huangb670d162018-08-23 20:01:13 +08001006 return statusFromErrcode(res);
1007}
1008
1009binder::Status NetdNativeService::networkRemoveRoute(int32_t netId, const std::string& ifName,
1010 const std::string& destination,
1011 const std::string& nextHop) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001012 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001013 bool legacy = false;
1014 uid_t uid = 0; // UID is only meaningful for legacy routes.
1015 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1016 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
Luke Huangb670d162018-08-23 20:01:13 +08001017 return statusFromErrcode(res);
1018}
1019
1020binder::Status NetdNativeService::networkAddLegacyRoute(int32_t netId, const std::string& ifName,
1021 const std::string& destination,
1022 const std::string& nextHop, int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001023 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001024 bool legacy = true;
1025 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1026 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
Tyler Wearfa94a272019-12-05 15:01:48 -08001027 (uid_t)uid, 0);
Luke Huangb670d162018-08-23 20:01:13 +08001028 return statusFromErrcode(res);
1029}
1030
1031binder::Status NetdNativeService::networkRemoveLegacyRoute(int32_t netId, const std::string& ifName,
1032 const std::string& destination,
1033 const std::string& nextHop,
1034 int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001035 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001036 bool legacy = true;
1037 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1038 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1039 (uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001040 return statusFromErrcode(res);
1041}
1042
1043binder::Status NetdNativeService::networkGetDefault(int32_t* netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001044 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001045 *netId = gCtls->netCtrl.getDefaultNetwork();
Luke Huangb670d162018-08-23 20:01:13 +08001046 return binder::Status::ok();
1047}
1048
1049binder::Status NetdNativeService::networkSetDefault(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001050 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001051 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001052 return statusFromErrcode(res);
1053}
1054
1055binder::Status NetdNativeService::networkClearDefault() {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001056 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001057 unsigned netId = NETID_UNSET;
1058 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001059 return statusFromErrcode(res);
1060}
1061
1062std::vector<uid_t> NetdNativeService::intsToUids(const std::vector<int32_t>& intUids) {
1063 return {begin(intUids), end(intUids)};
1064}
1065
1066Permission NetdNativeService::convertPermission(int32_t permission) {
1067 switch (permission) {
1068 case INetd::PERMISSION_NETWORK:
1069 return Permission::PERMISSION_NETWORK;
1070 case INetd::PERMISSION_SYSTEM:
1071 return Permission::PERMISSION_SYSTEM;
1072 default:
1073 return Permission::PERMISSION_NONE;
1074 }
1075}
1076
1077binder::Status NetdNativeService::networkSetPermissionForNetwork(int32_t netId,
1078 int32_t permission) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001079 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001080 std::vector<unsigned> netIds = {(unsigned) netId};
1081 int res = gCtls->netCtrl.setPermissionForNetworks(convertPermission(permission), netIds);
Luke Huangb670d162018-08-23 20:01:13 +08001082 return statusFromErrcode(res);
1083}
1084
1085binder::Status NetdNativeService::networkSetPermissionForUser(int32_t permission,
1086 const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001087 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001088 gCtls->netCtrl.setPermissionForUsers(convertPermission(permission), intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001089 return binder::Status::ok();
1090}
1091
1092binder::Status NetdNativeService::networkClearPermissionForUser(const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001093 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001094 Permission permission = Permission::PERMISSION_NONE;
1095 gCtls->netCtrl.setPermissionForUsers(permission, intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001096 return binder::Status::ok();
1097}
1098
Ken Chen155fb6a2020-12-10 18:50:19 +08001099binder::Status NetdNativeService::networkSetProtectAllow(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001100 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001101 std::vector<uid_t> uids = {(uid_t) uid};
1102 gCtls->netCtrl.allowProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001103 return binder::Status::ok();
1104}
1105
1106binder::Status NetdNativeService::networkSetProtectDeny(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001107 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001108 std::vector<uid_t> uids = {(uid_t) uid};
1109 gCtls->netCtrl.denyProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001110 return binder::Status::ok();
1111}
1112
1113binder::Status NetdNativeService::networkCanProtect(int32_t uid, bool* ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001114 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001115 *ret = gCtls->netCtrl.canProtect((uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001116 return binder::Status::ok();
1117}
1118
Patrick Rohrc92891f2022-02-01 22:13:27 +01001119binder::Status NetdNativeService::trafficSetNetPermForUids(int32_t, const std::vector<int32_t>&) {
1120 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Chenbo Feng48eaed32018-12-26 17:40:21 -08001121}
1122
Luke Huange64fa382018-07-24 16:38:22 +08001123binder::Status NetdNativeService::firewallSetFirewallType(int32_t firewallType) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001124 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001125 auto type = static_cast<FirewallType>(firewallType);
1126
1127 int res = gCtls->firewallCtrl.setFirewallType(type);
Luke Huange64fa382018-07-24 16:38:22 +08001128 return statusFromErrcode(res);
1129}
1130
1131binder::Status NetdNativeService::firewallSetInterfaceRule(const std::string& ifName,
1132 int32_t firewallRule) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001133 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001134 auto rule = static_cast<FirewallRule>(firewallRule);
1135
1136 int res = gCtls->firewallCtrl.setInterfaceRule(ifName.c_str(), rule);
Luke Huange64fa382018-07-24 16:38:22 +08001137 return statusFromErrcode(res);
1138}
1139
Patrick Rohrc92891f2022-02-01 22:13:27 +01001140binder::Status NetdNativeService::firewallSetUidRule(int32_t, int32_t, int32_t) {
1141 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Luke Huange64fa382018-07-24 16:38:22 +08001142}
1143
Patrick Rohrc92891f2022-02-01 22:13:27 +01001144binder::Status NetdNativeService::firewallEnableChildChain(int32_t, bool) {
1145 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Luke Huange64fa382018-07-24 16:38:22 +08001146}
1147
Patrick Rohrc92891f2022-02-01 22:13:27 +01001148binder::Status NetdNativeService::firewallAddUidInterfaceRules(const std::string&,
1149 const std::vector<int32_t>&) {
1150 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Rubin Xuec27ff22019-01-08 21:33:03 +00001151}
1152
Patrick Rohrc92891f2022-02-01 22:13:27 +01001153binder::Status NetdNativeService::firewallRemoveUidInterfaceRules(const std::vector<int32_t>&) {
1154 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Rubin Xuec27ff22019-01-08 21:33:03 +00001155}
1156
Luke Huang19b49c52018-10-22 12:12:05 +09001157binder::Status NetdNativeService::tetherAddForward(const std::string& intIface,
1158 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001159 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang19b49c52018-10-22 12:12:05 +09001160
1161 int res = gCtls->tetherCtrl.enableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001162 return statusFromErrcode(res);
1163}
1164
1165binder::Status NetdNativeService::tetherRemoveForward(const std::string& intIface,
1166 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001167 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangae038f82018-11-05 11:17:31 +09001168 int res = gCtls->tetherCtrl.disableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001169 return statusFromErrcode(res);
1170}
1171
Chenbo Fengf5663d82018-11-08 16:10:48 -08001172binder::Status NetdNativeService::setTcpRWmemorySize(const std::string& rmemValues,
1173 const std::string& wmemValues) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001174 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chenbo Fengf5663d82018-11-08 16:10:48 -08001175 if (!WriteStringToFile(rmemValues, TCP_RMEM_PROC_FILE)) {
1176 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001177 return statusFromErrcode(ret);
1178 }
1179
1180 if (!WriteStringToFile(wmemValues, TCP_WMEM_PROC_FILE)) {
1181 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001182 return statusFromErrcode(ret);
1183 }
Chenbo Fengf5663d82018-11-08 16:10:48 -08001184 return binder::Status::ok();
1185}
1186
Luke Huang528af602018-08-29 19:06:05 +08001187binder::Status NetdNativeService::registerUnsolicitedEventListener(
1188 const android::sp<android::net::INetdUnsolicitedEventListener>& listener) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001189 ENFORCE_NETWORK_STACK_PERMISSIONS();
Bernie Innocenti9ee088d2019-02-01 17:14:32 +09001190 gCtls->eventReporter.registerUnsolEventListener(listener);
Luke Huang528af602018-08-29 19:06:05 +08001191 return binder::Status::ok();
1192}
1193
Luke Huange60bfd82019-04-26 11:39:31 +08001194binder::Status NetdNativeService::getOemNetd(android::sp<android::IBinder>* listener) {
1195 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang0e5e69d2019-03-06 15:42:38 +08001196 *listener = com::android::internal::net::OemNetdListener::getListener();
1197
Luke Huange60bfd82019-04-26 11:39:31 +08001198 return binder::Status::ok();
1199}
1200
Chiachang Wang00fc62f2019-12-04 20:38:26 +08001201binder::Status NetdNativeService::getFwmarkForNetwork(int32_t netId, MarkMaskParcel* markMask) {
1202 ENFORCE_NETWORK_STACK_PERMISSIONS();
1203
1204 Fwmark fwmark;
1205 fwmark.netId = netId;
1206 markMask->mask = FWMARK_NET_ID_MASK;
1207 markMask->mark = fwmark.intValue;
1208 return binder::Status::ok();
1209}
1210
Hungming Chenea8b2302021-02-04 20:31:52 +08001211// TODO: remark @deprecated in INetd.aidl.
1212binder::Status NetdNativeService::tetherOffloadRuleAdd(const TetherOffloadRuleParcel& /* rule */) {
1213 // deprecated
Lorenzo Colittie801d3c2020-02-18 00:00:35 +09001214 ENFORCE_NETWORK_STACK_PERMISSIONS();
Hungming Chenea8b2302021-02-04 20:31:52 +08001215 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Lorenzo Colittie801d3c2020-02-18 00:00:35 +09001216}
1217
Hungming Chenea8b2302021-02-04 20:31:52 +08001218// TODO: remark @deprecated in INetd.aidl.
1219binder::Status NetdNativeService::tetherOffloadRuleRemove(
1220 const TetherOffloadRuleParcel& /* rule */) {
1221 // deprecated
Lorenzo Colittie801d3c2020-02-18 00:00:35 +09001222 ENFORCE_NETWORK_STACK_PERMISSIONS();
Hungming Chenea8b2302021-02-04 20:31:52 +08001223 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Lorenzo Colittie801d3c2020-02-18 00:00:35 +09001224}
1225
Hungming Chenea8b2302021-02-04 20:31:52 +08001226// TODO: remark @deprecated in INetd.aidl.
Hungming Chenf40dc092020-03-12 16:21:03 +08001227binder::Status NetdNativeService::tetherOffloadGetStats(
Hungming Chenea8b2302021-02-04 20:31:52 +08001228 std::vector<TetherStatsParcel>* /* tetherStatsParcelVec */) {
1229 // deprecated
Hungming Chenf40dc092020-03-12 16:21:03 +08001230 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Hungming Chenea8b2302021-02-04 20:31:52 +08001231 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Hungming Chenf40dc092020-03-12 16:21:03 +08001232}
1233
Hungming Chenea8b2302021-02-04 20:31:52 +08001234// TODO: remark @deprecated in INetd.aidl.
1235binder::Status NetdNativeService::tetherOffloadSetInterfaceQuota(int /* ifIndex */,
1236 int64_t /* quotaBytes */) {
1237 // deprecated
Hungming Chen0c476712020-03-16 13:53:19 +08001238 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Hungming Chenea8b2302021-02-04 20:31:52 +08001239 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Hungming Chen0c476712020-03-16 13:53:19 +08001240}
1241
Hungming Chenea8b2302021-02-04 20:31:52 +08001242// TODO: remark @deprecated in INetd.aidl.
Hungming Chen468a20c2020-04-17 20:00:27 +08001243binder::Status NetdNativeService::tetherOffloadGetAndClearStats(
Hungming Chenea8b2302021-02-04 20:31:52 +08001244 int /* ifIndex */, android::net::TetherStatsParcel* /* tetherStats */) {
1245 // deprecated
Hungming Chen468a20c2020-04-17 20:00:27 +08001246 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Hungming Chenea8b2302021-02-04 20:31:52 +08001247 return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION);
Hungming Chen468a20c2020-04-17 20:00:27 +08001248}
1249
Ken Chen0c209f82022-12-22 15:11:39 +08001250binder::Status NetdNativeService::setNetworkAllowlist(
Ken Chen04ee6092022-12-26 17:35:33 +08001251 const std::vector<NativeUidRangeConfig>& rangeConfigs) {
Ken Chen0c209f82022-12-22 15:11:39 +08001252 ENFORCE_NETWORK_STACK_PERMISSIONS();
Ken Chen04ee6092022-12-26 17:35:33 +08001253 return statusFromErrcode(gCtls->netCtrl.setNetworkAllowlist(rangeConfigs));
Ken Chen0c209f82022-12-22 15:11:39 +08001254}
1255
Lorenzo Colittie4d626e2016-02-02 17:19:04 +09001256} // namespace net
1257} // namespace android