blob: 9af7efcdb10e7bd96c9e2307f68957c19e81d9bc [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>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090035#include <utils/Errors.h>
Pierre Imaibeedec32016-04-13 06:44:51 +090036#include <utils/String16.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090037
Lorenzo Colitti89faa342016-02-26 11:38:47 +090038#include "Controllers.h"
Chiachang Wang00fc62f2019-12-04 20:38:26 +080039#include "Fwmark.h"
Erik Kline55b06f82016-07-04 09:57:18 +090040#include "InterfaceController.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090041#include "NetdNativeService.h"
Luke Huang0e5e69d2019-03-06 15:42:38 +080042#include "OemNetdListener.h"
Luke Huangb670d162018-08-23 20:01:13 +080043#include "Permission.h"
Erik Kline85890042018-05-25 19:19:11 +090044#include "Process.h"
Robin Leeb8087362016-03-30 18:43:08 +010045#include "RouteController.h"
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090046#include "SockDiag.h"
Robin Leeb8087362016-03-30 18:43:08 +010047#include "UidRanges.h"
Xiao Ma33d562a2018-12-16 16:27:38 +090048#include "android/net/BnNetd.h"
Luke Huang3b56e5c2020-05-26 17:21:28 +080049#include "binder_utils/BinderUtil.h"
50#include "binder_utils/NetdPermissions.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090051#include "netid_client.h" // NETID_UNSET
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090052
53using android::base::StringPrintf;
Chenbo Fengf5663d82018-11-08 16:10:48 -080054using android::base::WriteStringToFile;
Lorenzo Colittif0e051c2020-04-06 09:19:57 +000055using android::net::TetherOffloadRuleParcel;
Luke Huangcaebcbb2018-09-27 20:37:14 +080056using android::net::TetherStatsParcel;
Luke Huang94658ac2018-10-18 19:35:12 +090057using android::net::UidRangeParcel;
Luke Huangb257d612019-03-14 21:19:13 +080058using android::netdutils::DumpWriter;
59using android::netdutils::ScopedIndent;
Luke Huange203a152018-11-23 11:47:28 +080060using android::os::ParcelFileDescriptor;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090061
62namespace android {
63namespace net {
64
65namespace {
Erik Klineb31fd692018-06-06 20:50:11 +090066const char OPT_SHORT[] = "--short";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090067
Automerger Merge Worker2b0f5862020-03-05 12:25:24 +000068// The input permissions should be equivalent that this function would return ok if any of them is
69// granted.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090070binder::Status checkAnyPermission(const std::vector<const char*>& permissions) {
Luke Huanga38b65c2018-09-26 16:31:03 +080071 pid_t pid = IPCThreadState::self()->getCallingPid();
72 uid_t uid = IPCThreadState::self()->getCallingUid();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090073
Automerger Merge Worker2b0f5862020-03-05 12:25:24 +000074 // TODO: Do the pure permission check in this function. Have another method
75 // (e.g. checkNetworkStackPermission) to wrap AID_SYSTEM and
76 // AID_NETWORK_STACK uid check.
Luke Huanga38b65c2018-09-26 16:31:03 +080077 // If the caller is the system UID, don't check permissions.
78 // Otherwise, if the system server's binder thread pool is full, and all the threads are
79 // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492
80 //
81 // From a security perspective, there is currently no difference, because:
Automerger Merge Workeraa65d712020-03-05 10:29:36 +000082 // 1. The system server has the NETWORK_STACK permission, which grants access to all the
83 // IPCs in this file.
Luke Huanga38b65c2018-09-26 16:31:03 +080084 // 2. AID_SYSTEM always has all permissions. See ActivityManager#checkComponentPermission.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090085 if (uid == AID_SYSTEM) {
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090086 return binder::Status::ok();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090087 }
Automerger Merge Worker2b0f5862020-03-05 12:25:24 +000088 // AID_NETWORK_STACK own MAINLINE_NETWORK_STACK permission, don't IPC to system server to check
89 // MAINLINE_NETWORK_STACK permission. Cross-process(netd, networkstack and system server)
90 // deadlock: http://b/149766727
91 if (uid == AID_NETWORK_STACK) {
92 for (const char* permission : permissions) {
93 if (std::strcmp(permission, PERM_MAINLINE_NETWORK_STACK) == 0) {
94 return binder::Status::ok();
95 }
96 }
97 }
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090098
99 for (const char* permission : permissions) {
100 if (checkPermission(String16(permission), pid, uid)) {
101 return binder::Status::ok();
102 }
103 }
104
105 auto err = StringPrintf("UID %d / PID %d does not have any of the following permissions: %s",
106 uid, pid, android::base::Join(permissions, ',').c_str());
107 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, err.c_str());
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900108}
109
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900110#define ENFORCE_ANY_PERMISSION(...) \
111 do { \
112 binder::Status status = checkAnyPermission({__VA_ARGS__}); \
113 if (!status.isOk()) { \
114 return status; \
115 } \
116 } while (0)
Robin Lee2cf56172016-09-13 18:55:42 +0900117
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900118#define NETD_LOCKING_RPC(lock, ... /* permissions */) \
119 ENFORCE_ANY_PERMISSION(__VA_ARGS__); \
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900120 std::lock_guard _lock(lock);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900121
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900122#define NETD_BIG_LOCK_RPC(... /* permissions */) NETD_LOCKING_RPC(gBigNetdLock, __VA_ARGS__)
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900123
Luke Huangf7782042018-08-08 13:13:04 +0800124#define RETURN_BINDER_STATUS_IF_NOT_OK(logEntry, res) \
125 do { \
126 if (!isOk((res))) { \
127 logErrorStatus((logEntry), (res)); \
128 return asBinderStatus((res)); \
129 } \
130 } while (0)
131
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900132#define ENFORCE_NETWORK_STACK_PERMISSIONS() \
133 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK)
134
Luke Huangf7782042018-08-08 13:13:04 +0800135void logErrorStatus(netdutils::LogEntry& logEntry, const netdutils::Status& status) {
136 gLog.log(logEntry.returns(status.code()).withAutomaticDuration());
137}
138
Bernie Innocenti97f388f2018-10-16 19:17:08 +0900139binder::Status asBinderStatus(const netdutils::Status& status) {
140 if (isOk(status)) {
141 return binder::Status::ok();
142 }
143 return binder::Status::fromServiceSpecificError(status.code(), status.msg().c_str());
144}
145
Lorenzo Colittie801d3c2020-02-18 00:00:35 +0900146template <typename T>
147binder::Status asBinderStatus(const base::Result<T> result) {
148 if (result.ok()) return binder::Status::ok();
149
150 return binder::Status::fromServiceSpecificError(result.error().code(),
151 result.error().message().c_str());
152}
153
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900154inline binder::Status statusFromErrcode(int ret) {
155 if (ret) {
156 return binder::Status::fromServiceSpecificError(-ret, strerror(-ret));
157 }
158 return binder::Status::ok();
159}
160
Erik Klineb31fd692018-06-06 20:50:11 +0900161bool contains(const Vector<String16>& words, const String16& word) {
162 for (const auto& w : words) {
163 if (w == word) return true;
164 }
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900165
Erik Klineb31fd692018-06-06 20:50:11 +0900166 return false;
167}
168
169} // namespace
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900170
Luke Huange3f11812019-05-02 18:10:15 +0800171NetdNativeService::NetdNativeService() {
172 // register log callback to BnNetd::logFunc
173 BnNetd::logFunc = std::bind(binderCallLogFn, std::placeholders::_1,
174 [](const std::string& msg) { gLog.info("%s", msg.c_str()); });
175}
176
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900177status_t NetdNativeService::start() {
178 IPCThreadState::self()->disableBackgroundScheduling(true);
Erik Klineb31fd692018-06-06 20:50:11 +0900179 const status_t ret = BinderService<NetdNativeService>::publish();
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900180 if (ret != android::OK) {
181 return ret;
182 }
183 sp<ProcessState> ps(ProcessState::self());
184 ps->startThreadPool();
185 ps->giveThreadPoolName();
Xiao Ma33d562a2018-12-16 16:27:38 +0900186
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900187 return android::OK;
188}
189
Hugo Benichi7b314e12018-01-15 21:54:00 +0900190status_t NetdNativeService::dump(int fd, const Vector<String16> &args) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900191 const binder::Status dump_permission = checkAnyPermission({PERM_DUMP});
Erik Kline2d3a1632016-03-15 16:33:48 +0900192 if (!dump_permission.isOk()) {
193 const String8 msg(dump_permission.toString8());
194 write(fd, msg.string(), msg.size());
195 return PERMISSION_DENIED;
196 }
197
198 // This method does not grab any locks. If individual classes need locking
199 // their dump() methods MUST handle locking appropriately.
Hugo Benichi7b314e12018-01-15 21:54:00 +0900200
Erik Kline2d3a1632016-03-15 16:33:48 +0900201 DumpWriter dw(fd);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900202
203 if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) {
204 dw.blankline();
205 gCtls->tcpSocketMonitor.dump(dw);
206 dw.blankline();
207 return NO_ERROR;
208 }
209
Chenbo Fengef297172018-03-26 10:53:33 -0700210 if (!args.isEmpty() && args[0] == TrafficController::DUMP_KEYWORD) {
211 dw.blankline();
212 gCtls->trafficCtrl.dump(dw, true);
213 dw.blankline();
214 return NO_ERROR;
215 }
216
Erik Kline85890042018-05-25 19:19:11 +0900217 process::dump(dw);
Erik Kline2d3a1632016-03-15 16:33:48 +0900218 dw.blankline();
219 gCtls->netCtrl.dump(dw);
220 dw.blankline();
221
Chenbo Fengef297172018-03-26 10:53:33 -0700222 gCtls->trafficCtrl.dump(dw, false);
223 dw.blankline();
224
Benedict Wongaf855432018-05-10 17:07:37 -0700225 gCtls->xfrmCtrl.dump(dw);
226 dw.blankline();
227
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700228 gCtls->clatdCtrl.dump(dw);
229 dw.blankline();
230
Lorenzo Colitti52db3912020-02-17 23:59:45 +0900231 gCtls->tetherCtrl.dump(dw);
232 dw.blankline();
233
Erik Klineb31fd692018-06-06 20:50:11 +0900234 {
235 ScopedIndent indentLog(dw);
236 if (contains(args, String16(OPT_SHORT))) {
237 dw.println("Log: <omitted>");
238 } else {
239 dw.println("Log:");
240 ScopedIndent indentLogEntries(dw);
241 gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); });
242 }
243 dw.blankline();
244 }
245
Luke Huang528af602018-08-29 19:06:05 +0800246 {
247 ScopedIndent indentLog(dw);
248 if (contains(args, String16(OPT_SHORT))) {
249 dw.println("UnsolicitedLog: <omitted>");
250 } else {
251 dw.println("UnsolicitedLog:");
252 ScopedIndent indentLogEntries(dw);
253 gUnsolicitedLog.forEachEntry(
254 [&dw](const std::string& entry) mutable { dw.println(entry); });
255 }
256 dw.blankline();
257 }
258
Erik Kline2d3a1632016-03-15 16:33:48 +0900259 return NO_ERROR;
260}
261
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900262binder::Status NetdNativeService::isAlive(bool *alive) {
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000263 NETD_BIG_LOCK_RPC(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900264
265 *alive = true;
Erik Klineb31fd692018-06-06 20:50:11 +0900266
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900267 return binder::Status::ok();
268}
269
Erik Klinef52d4522018-03-14 15:01:46 +0900270binder::Status NetdNativeService::firewallReplaceUidChain(const std::string& chainName,
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900271 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000272 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Erik Klinef52d4522018-03-14 15:01:46 +0900273 int err = gCtls->firewallCtrl.replaceUidChain(chainName, isWhitelist, uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900274 *ret = (err == 0);
275 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900276}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900277
278binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000279 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900280 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
281 *ret = (err == 0);
282 return binder::Status::ok();
283}
284
Luke Huang531f5d32018-08-03 15:19:05 +0800285binder::Status NetdNativeService::bandwidthSetInterfaceQuota(const std::string& ifName,
286 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900287 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800288 int res = gCtls->bandwidthCtrl.setInterfaceQuota(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800289 return statusFromErrcode(res);
290}
291
292binder::Status NetdNativeService::bandwidthRemoveInterfaceQuota(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900293 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800294 int res = gCtls->bandwidthCtrl.removeInterfaceQuota(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800295 return statusFromErrcode(res);
296}
297
298binder::Status NetdNativeService::bandwidthSetInterfaceAlert(const std::string& ifName,
299 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900300 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800301 int res = gCtls->bandwidthCtrl.setInterfaceAlert(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800302 return statusFromErrcode(res);
303}
304
305binder::Status NetdNativeService::bandwidthRemoveInterfaceAlert(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900306 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800307 int res = gCtls->bandwidthCtrl.removeInterfaceAlert(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800308 return statusFromErrcode(res);
309}
310
311binder::Status NetdNativeService::bandwidthSetGlobalAlert(int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900312 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800313 int res = gCtls->bandwidthCtrl.setGlobalAlert(bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800314 return statusFromErrcode(res);
315}
316
317binder::Status NetdNativeService::bandwidthAddNaughtyApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900318 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800319 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
320 int res = gCtls->bandwidthCtrl.addNaughtyApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800321 return statusFromErrcode(res);
322}
323
324binder::Status NetdNativeService::bandwidthRemoveNaughtyApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900325 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800326 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
327 int res = gCtls->bandwidthCtrl.removeNaughtyApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800328 return statusFromErrcode(res);
329}
330
331binder::Status NetdNativeService::bandwidthAddNiceApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900332 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800333 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
334 int res = gCtls->bandwidthCtrl.addNiceApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800335 return statusFromErrcode(res);
336}
337
338binder::Status NetdNativeService::bandwidthRemoveNiceApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900339 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800340 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
341 int res = gCtls->bandwidthCtrl.removeNiceApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800342 return statusFromErrcode(res);
343}
344
Uldiniadb198ec92018-03-01 08:54:00 -0500345binder::Status NetdNativeService::bandwidthAddRestrictAppOnInterface(const std::string& usecase,
346 const std::string& ifName, int32_t uid) {
347 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
348 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
349 int res = gCtls->bandwidthCtrl.addRestrictAppsOnInterface(usecase, ifName, appStrUids);
350 return statusFromErrcode(res);
351}
352
353binder::Status NetdNativeService::bandwidthRemoveRestrictAppOnInterface(const std::string& usecase,
354 const std::string& ifName, int32_t uid) {
355 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
356 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
357 int res = gCtls->bandwidthCtrl.removeRestrictAppsOnInterface(usecase, ifName, appStrUids);
358 return statusFromErrcode(res);
359}
360
Luke Huangb670d162018-08-23 20:01:13 +0800361binder::Status NetdNativeService::networkCreatePhysical(int32_t netId, int32_t permission) {
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000362 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +0800363 int ret = gCtls->netCtrl.createPhysicalNetwork(netId, convertPermission(permission));
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900364 return statusFromErrcode(ret);
365}
366
cken67cd14c2018-12-05 17:26:59 +0900367binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool secure) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900368 ENFORCE_NETWORK_STACK_PERMISSIONS();
cken67cd14c2018-12-05 17:26:59 +0900369 int ret = gCtls->netCtrl.createVirtualNetwork(netId, secure);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900370 return statusFromErrcode(ret);
371}
372
373binder::Status NetdNativeService::networkDestroy(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900374 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangd33a8f42019-03-14 16:10:04 +0800375 // NetworkController::destroyNetwork is thread-safe.
Mike Yu4fe1b9c2019-01-29 17:50:19 +0800376 const int ret = gCtls->netCtrl.destroyNetwork(netId);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900377 return statusFromErrcode(ret);
378}
379
380binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000381 ENFORCE_NETWORK_STACK_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900382 int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
383 return statusFromErrcode(ret);
384}
385
386binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000387 ENFORCE_NETWORK_STACK_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900388 int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
389 return statusFromErrcode(ret);
390}
391
Luke Huang94658ac2018-10-18 19:35:12 +0900392binder::Status NetdNativeService::networkAddUidRanges(
393 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900394 // NetworkController::addUsersToNetwork is thread-safe.
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000395 ENFORCE_NETWORK_STACK_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900396 int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray));
397 return statusFromErrcode(ret);
398}
399
Luke Huang94658ac2018-10-18 19:35:12 +0900400binder::Status NetdNativeService::networkRemoveUidRanges(
401 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900402 // NetworkController::removeUsersFromNetwork is thread-safe.
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000403 ENFORCE_NETWORK_STACK_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900404 int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray));
405 return statusFromErrcode(ret);
406}
407
Luke Huang94658ac2018-10-18 19:35:12 +0900408binder::Status NetdNativeService::networkRejectNonSecureVpn(
409 bool add, const std::vector<UidRangeParcel>& uidRangeArray) {
Robin Leeb8087362016-03-30 18:43:08 +0100410 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
411 // it should be possible to use the same lock as NetworkController. However, every call through
412 // the CommandListener "network" command will need to hold this lock too, not just the ones that
413 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
414 // look at routes, but it's not enough here).
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000415 NETD_BIG_LOCK_RPC(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900416 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100417
418 int err;
419 if (add) {
420 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
421 } else {
422 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
423 }
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900424 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100425}
426
Luke Huang94658ac2018-10-18 19:35:12 +0900427binder::Status NetdNativeService::socketDestroy(const std::vector<UidRangeParcel>& uids,
428 const std::vector<int32_t>& skipUids) {
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000429 ENFORCE_NETWORK_STACK_PERMISSIONS();
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900430
431 SockDiag sd;
432 if (!sd.open()) {
433 return binder::Status::fromServiceSpecificError(EIO,
434 String8("Could not open SOCK_DIAG socket"));
435 }
436
437 UidRanges uidRanges(uids);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +0900438 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
439 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900440 if (err) {
441 return binder::Status::fromServiceSpecificError(-err,
442 String8::format("destroySockets: %s", strerror(-err)));
443 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900444 return binder::Status::ok();
445}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900446
Erik Klinef48e4dd2016-07-18 04:02:07 +0900447binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900448 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Erik Klinef48e4dd2016-07-18 04:02:07 +0900449 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
450 return binder::Status::ok();
451}
452
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900453namespace {
454
Hungming Chen41b0ed92020-06-02 00:13:20 +0000455constexpr const int UNUSED_IFINDEX = 0;
456
Luke Huangcaebcbb2018-09-27 20:37:14 +0800457void tetherAddStatsByInterface(TetherController::TetherStats* tetherStatsParcel,
458 const TetherController::TetherStats& tetherStats) {
459 if (tetherStatsParcel->extIface == tetherStats.extIface) {
460 tetherStatsParcel->rxBytes += tetherStats.rxBytes;
461 tetherStatsParcel->rxPackets += tetherStats.rxPackets;
462 tetherStatsParcel->txBytes += tetherStats.txBytes;
463 tetherStatsParcel->txPackets += tetherStats.txPackets;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900464 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800465}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900466
Luke Huangcaebcbb2018-09-27 20:37:14 +0800467TetherStatsParcel toTetherStatsParcel(const TetherController::TetherStats& stats) {
468 TetherStatsParcel result;
469 result.iface = stats.extIface;
470 result.rxBytes = stats.rxBytes;
471 result.rxPackets = stats.rxPackets;
472 result.txBytes = stats.txBytes;
473 result.txPackets = stats.txPackets;
Hungming Chen41b0ed92020-06-02 00:13:20 +0000474 result.ifIndex = UNUSED_IFINDEX;
Luke Huangcaebcbb2018-09-27 20:37:14 +0800475 return result;
476}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900477
Luke Huangcaebcbb2018-09-27 20:37:14 +0800478void setTetherStatsParcelVecByInterface(std::vector<TetherStatsParcel>* tetherStatsVec,
479 const TetherController::TetherStatsList& statsList) {
480 std::map<std::string, TetherController::TetherStats> statsMap;
481 for (const auto& stats : statsList) {
482 auto iter = statsMap.find(stats.extIface);
483 if (iter != statsMap.end()) {
484 tetherAddStatsByInterface(&(iter->second), stats);
485 } else {
486 statsMap.insert(
487 std::pair<std::string, TetherController::TetherStats>(stats.extIface, stats));
488 }
489 }
490 for (auto iter = statsMap.begin(); iter != statsMap.end(); iter++) {
491 tetherStatsVec->push_back(toTetherStatsParcel(iter->second));
492 }
493}
494
495std::vector<std::string> tetherStatsParcelVecToStringVec(std::vector<TetherStatsParcel>* tVec) {
496 std::vector<std::string> result;
497 for (const auto& t : *tVec) {
498 result.push_back(StringPrintf("%s:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64,
499 t.iface.c_str(), t.rxBytes, t.rxPackets, t.txBytes,
500 t.txPackets));
501 }
502 return result;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900503}
504
505} // namespace
506
Luke Huangcaebcbb2018-09-27 20:37:14 +0800507binder::Status NetdNativeService::tetherGetStats(
508 std::vector<TetherStatsParcel>* tetherStatsParcelVec) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900509 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900510 const auto& statsList = gCtls->tetherCtrl.getTetherStats();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900511 if (!isOk(statsList)) {
Nathan Harold28ccfef2018-03-16 19:02:47 -0700512 return asBinderStatus(statsList);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900513 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800514 setTetherStatsParcelVecByInterface(tetherStatsParcelVec, statsList.value());
515 auto statsResults = tetherStatsParcelVecToStringVec(tetherStatsParcelVec);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900516 return binder::Status::ok();
517}
518
Erik Kline53c20882016-08-02 15:22:53 +0900519binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
520 const std::string &addrString, int prefixLength) {
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000521 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900522 const int err = InterfaceController::addAddress(
523 ifName.c_str(), addrString.c_str(), prefixLength);
524 if (err != 0) {
525 return binder::Status::fromServiceSpecificError(-err,
526 String8::format("InterfaceController error: %s", strerror(-err)));
527 }
528 return binder::Status::ok();
529}
530
531binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
532 const std::string &addrString, int prefixLength) {
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000533 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900534 const int err = InterfaceController::delAddress(
535 ifName.c_str(), addrString.c_str(), prefixLength);
536 if (err != 0) {
537 return binder::Status::fromServiceSpecificError(-err,
538 String8::format("InterfaceController error: %s", strerror(-err)));
539 }
540 return binder::Status::ok();
541}
542
Erik Kline38e51f12018-09-06 20:14:44 +0900543namespace {
Erik Kline55b06f82016-07-04 09:57:18 +0900544
Erik Kline38e51f12018-09-06 20:14:44 +0900545std::tuple<binder::Status, const char*, const char*> getPathComponents(int32_t ipversion,
546 int32_t category) {
547 const char* ipversionStr = nullptr;
548 switch (ipversion) {
Erik Kline55b06f82016-07-04 09:57:18 +0900549 case INetd::IPV4:
Erik Kline38e51f12018-09-06 20:14:44 +0900550 ipversionStr = "ipv4";
Erik Kline55b06f82016-07-04 09:57:18 +0900551 break;
552 case INetd::IPV6:
Erik Kline38e51f12018-09-06 20:14:44 +0900553 ipversionStr = "ipv6";
Erik Kline55b06f82016-07-04 09:57:18 +0900554 break;
555 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900556 return {binder::Status::fromServiceSpecificError(EAFNOSUPPORT, "Bad IP version"),
557 nullptr, nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900558 }
559
Erik Kline38e51f12018-09-06 20:14:44 +0900560 const char* whichStr = nullptr;
561 switch (category) {
Erik Kline55b06f82016-07-04 09:57:18 +0900562 case INetd::CONF:
563 whichStr = "conf";
564 break;
565 case INetd::NEIGH:
566 whichStr = "neigh";
567 break;
568 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900569 return {binder::Status::fromServiceSpecificError(EINVAL, "Bad category"), nullptr,
570 nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900571 }
572
Erik Kline38e51f12018-09-06 20:14:44 +0900573 return {binder::Status::ok(), ipversionStr, whichStr};
574}
575
576} // namespace
577
578binder::Status NetdNativeService::getProcSysNet(int32_t ipversion, int32_t which,
579 const std::string& ifname,
580 const std::string& parameter, std::string* value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900581 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900582 const auto pathParts = getPathComponents(ipversion, which);
583 const auto& pathStatus = std::get<0>(pathParts);
584 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900585 return pathStatus;
Erik Kline55b06f82016-07-04 09:57:18 +0900586 }
Erik Kline38e51f12018-09-06 20:14:44 +0900587
588 const int err = InterfaceController::getParameter(std::get<1>(pathParts),
589 std::get<2>(pathParts), ifname.c_str(),
590 parameter.c_str(), value);
Erik Kline38e51f12018-09-06 20:14:44 +0900591 return statusFromErrcode(err);
592}
593
594binder::Status NetdNativeService::setProcSysNet(int32_t ipversion, int32_t which,
595 const std::string& ifname,
596 const std::string& parameter,
597 const std::string& value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900598 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900599 const auto pathParts = getPathComponents(ipversion, which);
600 const auto& pathStatus = std::get<0>(pathParts);
601 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900602 return pathStatus;
603 }
604
605 const int err = InterfaceController::setParameter(std::get<1>(pathParts),
606 std::get<2>(pathParts), ifname.c_str(),
607 parameter.c_str(), value.c_str());
Erik Kline38e51f12018-09-06 20:14:44 +0900608 return statusFromErrcode(err);
Erik Kline55b06f82016-07-04 09:57:18 +0900609}
610
Luke Huange203a152018-11-23 11:47:28 +0800611binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const ParcelFileDescriptor& socket,
612 int newUid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900613 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wongb2daefb2017-12-06 22:05:46 -0800614
615 uid_t callerUid = IPCThreadState::self()->getCallingUid();
Luke Huange203a152018-11-23 11:47:28 +0800616 return asBinderStatus(
617 gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket.get(), newUid, callerUid));
Benedict Wongb2daefb2017-12-06 22:05:46 -0800618}
619
Nathan Harold1a371532017-01-30 12:30:48 -0800620binder::Status NetdNativeService::ipSecAllocateSpi(
621 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800622 const std::string& sourceAddress,
623 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800624 int32_t inSpi,
625 int32_t* outSpi) {
626 // Necessary locking done in IpSecService and kernel
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000627 ENFORCE_NETWORK_STACK_PERMISSIONS();
ludi6e8eccd2017-08-14 14:40:37 -0700628 return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi(
Nathan Harold1a371532017-01-30 12:30:48 -0800629 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800630 sourceAddress,
631 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800632 inSpi,
633 outSpi));
634}
635
636binder::Status NetdNativeService::ipSecAddSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700637 int32_t transformId, int32_t mode, const std::string& sourceAddress,
638 const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi,
639 int32_t markValue, int32_t markMask, const std::string& authAlgo,
640 const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo,
641 const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo,
642 const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType,
643 int32_t encapLocalPort, int32_t encapRemotePort, int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800644 // Necessary locking done in IpSecService and kernel
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000645 ENFORCE_NETWORK_STACK_PERMISSIONS();
ludi6e8eccd2017-08-14 14:40:37 -0700646 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation(
Benedict Wongad600cb2018-05-14 17:22:35 -0700647 transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue,
648 markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits,
Benedict Wonga450e722018-05-07 10:29:02 -0700649 aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort,
650 interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800651}
652
653binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700654 int32_t transformId, const std::string& sourceAddress,
655 const std::string& destinationAddress, int32_t spi, int32_t markValue, int32_t markMask,
656 int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800657 // Necessary locking done in IpSecService and kernel
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000658 ENFORCE_NETWORK_STACK_PERMISSIONS();
ludi6e8eccd2017-08-14 14:40:37 -0700659 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700660 transformId, sourceAddress, destinationAddress, spi, markValue, markMask, interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800661}
662
663binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800664 const ParcelFileDescriptor& socket, int32_t transformId, int32_t direction,
665 const std::string& sourceAddress, const std::string& destinationAddress, int32_t spi) {
Nathan Harold1a371532017-01-30 12:30:48 -0800666 // Necessary locking done in IpSecService and kernel
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000667 ENFORCE_NETWORK_STACK_PERMISSIONS();
ludi6e8eccd2017-08-14 14:40:37 -0700668 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800669 socket.get(), transformId, direction, sourceAddress, destinationAddress, spi));
Nathan Harold1a371532017-01-30 12:30:48 -0800670}
671
672binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800673 const ParcelFileDescriptor& socket) {
Nathan Harold1a371532017-01-30 12:30:48 -0800674 // Necessary locking done in IpSecService and kernel
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000675 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huange203a152018-11-23 11:47:28 +0800676 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(socket.get()));
Nathan Harold1a371532017-01-30 12:30:48 -0800677}
678
Benedict Wonga04ffa72018-05-09 21:42:42 -0700679binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
680 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700681 const std::string& tmplSrcAddress,
682 const std::string& tmplDstAddress,
683 int32_t spi, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700684 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800685 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900686 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong84a8dca2018-01-19 12:12:17 -0800687 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700688 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700689 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800690}
691
Benedict Wonga450e722018-05-07 10:29:02 -0700692binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(
693 int32_t transformId, int32_t selAddrFamily, int32_t direction,
694 const std::string& tmplSrcAddress, const std::string& tmplDstAddress, int32_t spi,
695 int32_t markValue, int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800696 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900697 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong84a8dca2018-01-19 12:12:17 -0800698 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700699 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700700 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800701}
702
Benedict Wonga04ffa72018-05-09 21:42:42 -0700703binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId,
704 int32_t selAddrFamily,
705 int32_t direction, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700706 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800707 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900708 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong84a8dca2018-01-19 12:12:17 -0800709 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
Benedict Wonga450e722018-05-07 10:29:02 -0700710 transformId, selAddrFamily, direction, markValue, markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800711}
712
Benedict Wong319f17e2018-05-15 17:06:44 -0700713binder::Status NetdNativeService::ipSecAddTunnelInterface(const std::string& deviceName,
714 const std::string& localAddress,
715 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700716 int32_t iKey, int32_t oKey,
717 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800718 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900719 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700720 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700721 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, false);
Benedict Wong319f17e2018-05-15 17:06:44 -0700722 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800723}
724
Benedict Wong319f17e2018-05-15 17:06:44 -0700725binder::Status NetdNativeService::ipSecUpdateTunnelInterface(const std::string& deviceName,
726 const std::string& localAddress,
727 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700728 int32_t iKey, int32_t oKey,
729 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800730 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900731 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700732 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700733 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, true);
Benedict Wong319f17e2018-05-15 17:06:44 -0700734 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800735}
736
Benedict Wong319f17e2018-05-15 17:06:44 -0700737binder::Status NetdNativeService::ipSecRemoveTunnelInterface(const std::string& deviceName) {
manojboopathi8707f232018-01-02 14:45:47 -0800738 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900739 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700740 netdutils::Status result = gCtls->xfrmCtrl.ipSecRemoveTunnelInterface(deviceName);
Benedict Wong319f17e2018-05-15 17:06:44 -0700741 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800742}
743
Joel Scherpelzde937962017-06-01 13:20:21 +0900744binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
745 int32_t mode) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900746 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700747 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900748}
749
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900750binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName,
751 const std::string& prefix, int32_t mark,
752 int32_t mask) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900753 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700754 return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900755}
756
757binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
758 const std::string& prefix, int32_t mark,
759 int32_t mask) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900760 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700761 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900762}
763
Chenbo Feng873ae142019-04-10 12:26:06 -0700764binder::Status NetdNativeService::trafficSwapActiveStatsMap() {
765 ENFORCE_NETWORK_STACK_PERMISSIONS();
766 return asBinderStatus(gCtls->trafficCtrl.swapActiveStatsMap());
767}
768
Luke Huang0051a622018-07-23 20:30:16 +0800769binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout,
770 const std::string& classLabel) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900771 NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang0051a622018-07-23 20:30:16 +0800772 int res =
773 gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str());
Luke Huang0051a622018-07-23 20:30:16 +0800774 return statusFromErrcode(res);
775}
776
777binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName,
778 int32_t timeout,
779 const std::string& classLabel) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900780 NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang0051a622018-07-23 20:30:16 +0800781 int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout,
782 classLabel.c_str());
Luke Huang0051a622018-07-23 20:30:16 +0800783 return statusFromErrcode(res);
784}
Luke Huanga67dd562018-07-17 19:58:25 +0800785
786binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900787 NETD_LOCKING_RPC(gCtls->strictCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huanga67dd562018-07-17 19:58:25 +0800788 StrictPenalty penalty;
789 switch (policyPenalty) {
790 case INetd::PENALTY_POLICY_REJECT:
791 penalty = REJECT;
792 break;
793 case INetd::PENALTY_POLICY_LOG:
794 penalty = LOG;
795 break;
796 case INetd::PENALTY_POLICY_ACCEPT:
797 penalty = ACCEPT;
798 break;
799 default:
800 return statusFromErrcode(-EINVAL);
801 break;
802 }
803 int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty);
Luke Huanga67dd562018-07-17 19:58:25 +0800804 return statusFromErrcode(res);
805}
Luke Huange64fa382018-07-24 16:38:22 +0800806
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900807binder::Status NetdNativeService::clatdStart(const std::string& ifName,
808 const std::string& nat64Prefix, std::string* v6Addr) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700809 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900810 int res = gCtls->clatdCtrl.startClatd(ifName.c_str(), nat64Prefix, v6Addr);
Luke Huang6d301232018-08-01 14:05:18 +0800811 return statusFromErrcode(res);
812}
813
814binder::Status NetdNativeService::clatdStop(const std::string& ifName) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700815 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang6d301232018-08-01 14:05:18 +0800816 int res = gCtls->clatdCtrl.stopClatd(ifName.c_str());
Luke Huang6d301232018-08-01 14:05:18 +0800817 return statusFromErrcode(res);
818}
Luke Huanga67dd562018-07-17 19:58:25 +0800819
Luke Huang457d4702018-08-16 15:39:15 +0800820binder::Status NetdNativeService::ipfwdEnabled(bool* status) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900821 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang728cf4c2019-03-14 19:43:02 +0800822 *status = (gCtls->tetherCtrl.getIpfwdRequesterList().size() > 0) ? true : false;
823 return binder::Status::ok();
824}
825
826binder::Status NetdNativeService::ipfwdGetRequesterList(std::vector<std::string>* requesterList) {
827 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
828 for (const auto& requester : gCtls->tetherCtrl.getIpfwdRequesterList()) {
829 requesterList->push_back(requester);
830 }
Luke Huang457d4702018-08-16 15:39:15 +0800831 return binder::Status::ok();
832}
833
834binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900835 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang457d4702018-08-16 15:39:15 +0800836 int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
Luke Huang457d4702018-08-16 15:39:15 +0800837 return statusFromErrcode(res);
838}
839
840binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900841 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang457d4702018-08-16 15:39:15 +0800842 int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
Luke Huang457d4702018-08-16 15:39:15 +0800843 return statusFromErrcode(res);
844}
845
846binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface,
847 const std::string& toIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900848 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang457d4702018-08-16 15:39:15 +0800849 int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str());
Luke Huang457d4702018-08-16 15:39:15 +0800850 return statusFromErrcode(res);
851}
852
853binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface,
854 const std::string& toIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900855 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang457d4702018-08-16 15:39:15 +0800856 int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str());
Luke Huang457d4702018-08-16 15:39:15 +0800857 return statusFromErrcode(res);
858}
859
Luke Huangf7782042018-08-08 13:13:04 +0800860namespace {
Automerger Merge Workeraa65d712020-03-05 10:29:36 +0000861
Luke Huangf7782042018-08-08 13:13:04 +0800862std::string addCurlyBrackets(const std::string& s) {
863 return "{" + s + "}";
864}
865
866} // namespace
Xiao Ma33d562a2018-12-16 16:27:38 +0900867
Luke Huangf7782042018-08-08 13:13:04 +0800868binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900869 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800870 const auto& ifaceList = InterfaceController::getIfaceNames();
Luke Huangf7782042018-08-08 13:13:04 +0800871
872 interfaceListResult->clear();
873 interfaceListResult->reserve(ifaceList.value().size());
874 interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()),
875 end(ifaceList.value()));
876
Luke Huangf7782042018-08-08 13:13:04 +0800877 return binder::Status::ok();
878}
879
880std::string interfaceConfigurationParcelToString(const InterfaceConfigurationParcel& cfg) {
881 std::vector<std::string> result{cfg.ifName, cfg.hwAddr, cfg.ipv4Addr,
882 std::to_string(cfg.prefixLength)};
883 result.insert(end(result), begin(cfg.flags), end(cfg.flags));
884 return addCurlyBrackets(base::Join(result, ", "));
885}
886
887binder::Status NetdNativeService::interfaceGetCfg(
888 const std::string& ifName, InterfaceConfigurationParcel* interfaceGetCfgResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900889 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800890 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
891
892 const auto& cfgRes = InterfaceController::getCfg(ifName);
893 RETURN_BINDER_STATUS_IF_NOT_OK(entry, cfgRes);
894
895 *interfaceGetCfgResult = cfgRes.value();
896 gLog.log(entry.returns(interfaceConfigurationParcelToString(*interfaceGetCfgResult))
897 .withAutomaticDuration());
898 return binder::Status::ok();
899}
900
901binder::Status NetdNativeService::interfaceSetCfg(const InterfaceConfigurationParcel& cfg) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900902 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800903 auto entry = gLog.newEntry()
904 .prettyFunction(__PRETTY_FUNCTION__)
905 .arg(interfaceConfigurationParcelToString(cfg));
906
907 const auto& res = InterfaceController::setCfg(cfg);
908 RETURN_BINDER_STATUS_IF_NOT_OK(entry, res);
909
910 gLog.log(entry.withAutomaticDuration());
911 return binder::Status::ok();
912}
913
914binder::Status NetdNativeService::interfaceSetIPv6PrivacyExtensions(const std::string& ifName,
915 bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900916 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800917 int res = InterfaceController::setIPv6PrivacyExtensions(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800918 return statusFromErrcode(res);
919}
920
921binder::Status NetdNativeService::interfaceClearAddrs(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900922 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800923 int res = InterfaceController::clearAddrs(ifName.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800924 return statusFromErrcode(res);
925}
926
927binder::Status NetdNativeService::interfaceSetEnableIPv6(const std::string& ifName, bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900928 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800929 int res = InterfaceController::setEnableIPv6(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800930 return statusFromErrcode(res);
931}
932
933binder::Status NetdNativeService::interfaceSetMtu(const std::string& ifName, int32_t mtuValue) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900934 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800935 std::string mtu = std::to_string(mtuValue);
936 int res = InterfaceController::setMtu(ifName.c_str(), mtu.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800937 return statusFromErrcode(res);
938}
939
Luke Huangb5733d72018-08-21 17:17:19 +0800940binder::Status NetdNativeService::tetherStart(const std::vector<std::string>& dhcpRanges) {
Chiachang Wang08cb2112019-12-10 09:53:24 +0800941 TetherConfigParcel config;
942 config.usingLegacyDnsProxy = true;
943 config.dhcpRanges = dhcpRanges;
944 return tetherStartWithConfiguration(config);
Luke Huang91bd3e12019-08-20 11:33:52 +0800945}
946
Chiachang Wang08cb2112019-12-10 09:53:24 +0800947binder::Status NetdNativeService::tetherStartWithConfiguration(const TetherConfigParcel& config) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900948 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Chiachang Wang08cb2112019-12-10 09:53:24 +0800949 if (config.dhcpRanges.size() % 2 == 1) {
Luke Huangb5733d72018-08-21 17:17:19 +0800950 return statusFromErrcode(-EINVAL);
951 }
Chiachang Wang08cb2112019-12-10 09:53:24 +0800952 // TODO: Pass TetherConfigParcel directly.
953 int res = gCtls->tetherCtrl.startTethering(config.usingLegacyDnsProxy, config.dhcpRanges);
Luke Huangb5733d72018-08-21 17:17:19 +0800954 return statusFromErrcode(res);
955}
956
957binder::Status NetdNativeService::tetherStop() {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900958 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800959 int res = gCtls->tetherCtrl.stopTethering();
Luke Huangb5733d72018-08-21 17:17:19 +0800960 return statusFromErrcode(res);
961}
962
963binder::Status NetdNativeService::tetherIsEnabled(bool* enabled) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900964 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800965 *enabled = gCtls->tetherCtrl.isTetheringStarted();
Luke Huangb5733d72018-08-21 17:17:19 +0800966 return binder::Status::ok();
967}
968
969binder::Status NetdNativeService::tetherInterfaceAdd(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900970 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800971 int res = gCtls->tetherCtrl.tetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800972 return statusFromErrcode(res);
973}
974
975binder::Status NetdNativeService::tetherInterfaceRemove(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900976 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800977 int res = gCtls->tetherCtrl.untetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800978 return statusFromErrcode(res);
979}
980
981binder::Status NetdNativeService::tetherInterfaceList(std::vector<std::string>* ifList) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900982 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800983 for (const auto& ifname : gCtls->tetherCtrl.getTetheredInterfaceList()) {
984 ifList->push_back(ifname);
985 }
Luke Huangb5733d72018-08-21 17:17:19 +0800986 return binder::Status::ok();
987}
988
989binder::Status NetdNativeService::tetherDnsSet(int32_t netId,
990 const std::vector<std::string>& dnsAddrs) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900991 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800992 int res = gCtls->tetherCtrl.setDnsForwarders(netId, dnsAddrs);
Luke Huangb5733d72018-08-21 17:17:19 +0800993 return statusFromErrcode(res);
994}
995
996binder::Status NetdNativeService::tetherDnsList(std::vector<std::string>* dnsList) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900997 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800998 for (const auto& fwdr : gCtls->tetherCtrl.getDnsForwarders()) {
999 dnsList->push_back(fwdr);
1000 }
Luke Huangb5733d72018-08-21 17:17:19 +08001001 return binder::Status::ok();
1002}
1003
Tyler Wearfa94a272019-12-05 15:01:48 -08001004binder::Status NetdNativeService::networkAddRouteParcel(int32_t netId,
1005 const RouteInfoParcel& route) {
1006 // Public methods of NetworkController are thread-safe.
1007 ENFORCE_NETWORK_STACK_PERMISSIONS();
1008 bool legacy = false;
1009 uid_t uid = 0; // UID is only meaningful for legacy routes.
1010
1011 // convert Parcel to parameters
1012 int res = gCtls->netCtrl.addRoute(netId, route.ifName.c_str(), route.destination.c_str(),
1013 route.nextHop.empty() ? nullptr : route.nextHop.c_str(),
1014 legacy, uid, route.mtu);
1015 return statusFromErrcode(res);
1016}
1017
1018binder::Status NetdNativeService::networkUpdateRouteParcel(int32_t netId,
1019 const RouteInfoParcel& route) {
1020 // Public methods of NetworkController are thread-safe.
1021 ENFORCE_NETWORK_STACK_PERMISSIONS();
1022 bool legacy = false;
1023 uid_t uid = 0; // UID is only meaningful for legacy routes.
1024
1025 // convert Parcel to parameters
1026 int res = gCtls->netCtrl.updateRoute(netId, route.ifName.c_str(), route.destination.c_str(),
1027 route.nextHop.empty() ? nullptr : route.nextHop.c_str(),
1028 legacy, uid, route.mtu);
1029 return statusFromErrcode(res);
1030}
1031
1032binder::Status NetdNativeService::networkRemoveRouteParcel(int32_t netId,
1033 const RouteInfoParcel& route) {
1034 return networkRemoveRoute(netId, route.ifName, route.destination, route.nextHop);
1035}
1036
Luke Huangb670d162018-08-23 20:01:13 +08001037binder::Status NetdNativeService::networkAddRoute(int32_t netId, const std::string& ifName,
1038 const std::string& destination,
1039 const std::string& nextHop) {
1040 // Public methods of NetworkController are thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001041 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001042 bool legacy = false;
1043 uid_t uid = 0; // UID is only meaningful for legacy routes.
1044 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
Tyler Wearfa94a272019-12-05 15:01:48 -08001045 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid, 0);
Luke Huangb670d162018-08-23 20:01:13 +08001046 return statusFromErrcode(res);
1047}
1048
1049binder::Status NetdNativeService::networkRemoveRoute(int32_t netId, const std::string& ifName,
1050 const std::string& destination,
1051 const std::string& nextHop) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001052 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001053 bool legacy = false;
1054 uid_t uid = 0; // UID is only meaningful for legacy routes.
1055 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1056 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
Luke Huangb670d162018-08-23 20:01:13 +08001057 return statusFromErrcode(res);
1058}
1059
1060binder::Status NetdNativeService::networkAddLegacyRoute(int32_t netId, const std::string& ifName,
1061 const std::string& destination,
1062 const std::string& nextHop, int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001063 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001064 bool legacy = true;
1065 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1066 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
Tyler Wearfa94a272019-12-05 15:01:48 -08001067 (uid_t)uid, 0);
Luke Huangb670d162018-08-23 20:01:13 +08001068 return statusFromErrcode(res);
1069}
1070
1071binder::Status NetdNativeService::networkRemoveLegacyRoute(int32_t netId, const std::string& ifName,
1072 const std::string& destination,
1073 const std::string& nextHop,
1074 int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001075 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001076 bool legacy = true;
1077 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1078 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1079 (uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001080 return statusFromErrcode(res);
1081}
1082
1083binder::Status NetdNativeService::networkGetDefault(int32_t* netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001084 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001085 *netId = gCtls->netCtrl.getDefaultNetwork();
Luke Huangb670d162018-08-23 20:01:13 +08001086 return binder::Status::ok();
1087}
1088
1089binder::Status NetdNativeService::networkSetDefault(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001090 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001091 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001092 return statusFromErrcode(res);
1093}
1094
1095binder::Status NetdNativeService::networkClearDefault() {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001096 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001097 unsigned netId = NETID_UNSET;
1098 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001099 return statusFromErrcode(res);
1100}
1101
1102std::vector<uid_t> NetdNativeService::intsToUids(const std::vector<int32_t>& intUids) {
1103 return {begin(intUids), end(intUids)};
1104}
1105
1106Permission NetdNativeService::convertPermission(int32_t permission) {
1107 switch (permission) {
1108 case INetd::PERMISSION_NETWORK:
1109 return Permission::PERMISSION_NETWORK;
1110 case INetd::PERMISSION_SYSTEM:
1111 return Permission::PERMISSION_SYSTEM;
1112 default:
1113 return Permission::PERMISSION_NONE;
1114 }
1115}
1116
1117binder::Status NetdNativeService::networkSetPermissionForNetwork(int32_t netId,
1118 int32_t permission) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001119 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001120 std::vector<unsigned> netIds = {(unsigned) netId};
1121 int res = gCtls->netCtrl.setPermissionForNetworks(convertPermission(permission), netIds);
Luke Huangb670d162018-08-23 20:01:13 +08001122 return statusFromErrcode(res);
1123}
1124
1125binder::Status NetdNativeService::networkSetPermissionForUser(int32_t permission,
1126 const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001127 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001128 gCtls->netCtrl.setPermissionForUsers(convertPermission(permission), intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001129 return binder::Status::ok();
1130}
1131
1132binder::Status NetdNativeService::networkClearPermissionForUser(const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001133 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001134 Permission permission = Permission::PERMISSION_NONE;
1135 gCtls->netCtrl.setPermissionForUsers(permission, intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001136 return binder::Status::ok();
1137}
1138
1139binder::Status NetdNativeService::NetdNativeService::networkSetProtectAllow(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001140 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001141 std::vector<uid_t> uids = {(uid_t) uid};
1142 gCtls->netCtrl.allowProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001143 return binder::Status::ok();
1144}
1145
1146binder::Status NetdNativeService::networkSetProtectDeny(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001147 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001148 std::vector<uid_t> uids = {(uid_t) uid};
1149 gCtls->netCtrl.denyProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001150 return binder::Status::ok();
1151}
1152
1153binder::Status NetdNativeService::networkCanProtect(int32_t uid, bool* ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001154 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001155 *ret = gCtls->netCtrl.canProtect((uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001156 return binder::Status::ok();
1157}
1158
Chenbo Feng48eaed32018-12-26 17:40:21 -08001159binder::Status NetdNativeService::trafficSetNetPermForUids(int32_t permission,
1160 const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001161 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chenbo Feng48eaed32018-12-26 17:40:21 -08001162 gCtls->trafficCtrl.setPermissionForUids(permission, intsToUids(uids));
Chenbo Feng48eaed32018-12-26 17:40:21 -08001163 return binder::Status::ok();
1164}
1165
Luke Huange64fa382018-07-24 16:38:22 +08001166binder::Status NetdNativeService::firewallSetFirewallType(int32_t firewallType) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001167 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001168 auto type = static_cast<FirewallType>(firewallType);
1169
1170 int res = gCtls->firewallCtrl.setFirewallType(type);
Luke Huange64fa382018-07-24 16:38:22 +08001171 return statusFromErrcode(res);
1172}
1173
1174binder::Status NetdNativeService::firewallSetInterfaceRule(const std::string& ifName,
1175 int32_t firewallRule) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001176 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001177 auto rule = static_cast<FirewallRule>(firewallRule);
1178
1179 int res = gCtls->firewallCtrl.setInterfaceRule(ifName.c_str(), rule);
Luke Huange64fa382018-07-24 16:38:22 +08001180 return statusFromErrcode(res);
1181}
1182
1183binder::Status NetdNativeService::firewallSetUidRule(int32_t childChain, int32_t uid,
1184 int32_t firewallRule) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001185 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001186 auto chain = static_cast<ChildChain>(childChain);
1187 auto rule = static_cast<FirewallRule>(firewallRule);
1188
1189 int res = gCtls->firewallCtrl.setUidRule(chain, uid, rule);
Luke Huange64fa382018-07-24 16:38:22 +08001190 return statusFromErrcode(res);
1191}
1192
cjybyjk7fd85352021-01-22 17:12:21 +08001193binder::Status NetdNativeService::firewallSetMACAddressRule(const std::string& macAddr,
1194 int32_t firewallRule) {
1195 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
1196 auto rule = static_cast<FirewallRule>(firewallRule);
1197
1198 int res = gCtls->firewallCtrl.setMACAddressRule(macAddr.c_str(), rule);
1199 return statusFromErrcode(res);
1200}
1201
Luke Huange64fa382018-07-24 16:38:22 +08001202binder::Status NetdNativeService::firewallEnableChildChain(int32_t childChain, bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001203 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001204 auto chain = static_cast<ChildChain>(childChain);
1205
1206 int res = gCtls->firewallCtrl.enableChildChains(chain, enable);
Luke Huange64fa382018-07-24 16:38:22 +08001207 return statusFromErrcode(res);
1208}
1209
Rubin Xuec27ff22019-01-08 21:33:03 +00001210binder::Status NetdNativeService::firewallAddUidInterfaceRules(const std::string& ifName,
1211 const std::vector<int32_t>& uids) {
1212 ENFORCE_NETWORK_STACK_PERMISSIONS();
1213
1214 return asBinderStatus(gCtls->trafficCtrl.addUidInterfaceRules(
1215 RouteController::getIfIndex(ifName.c_str()), uids));
1216}
1217
1218binder::Status NetdNativeService::firewallRemoveUidInterfaceRules(
1219 const std::vector<int32_t>& uids) {
1220 ENFORCE_NETWORK_STACK_PERMISSIONS();
1221
1222 return asBinderStatus(gCtls->trafficCtrl.removeUidInterfaceRules(uids));
1223}
1224
Luke Huang19b49c52018-10-22 12:12:05 +09001225binder::Status NetdNativeService::tetherAddForward(const std::string& intIface,
1226 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001227 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang19b49c52018-10-22 12:12:05 +09001228
1229 int res = gCtls->tetherCtrl.enableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001230 return statusFromErrcode(res);
1231}
1232
1233binder::Status NetdNativeService::tetherRemoveForward(const std::string& intIface,
1234 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001235 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangae038f82018-11-05 11:17:31 +09001236 int res = gCtls->tetherCtrl.disableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001237 return statusFromErrcode(res);
1238}
1239
Chenbo Fengf5663d82018-11-08 16:10:48 -08001240binder::Status NetdNativeService::setTcpRWmemorySize(const std::string& rmemValues,
1241 const std::string& wmemValues) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001242 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chenbo Fengf5663d82018-11-08 16:10:48 -08001243 if (!WriteStringToFile(rmemValues, TCP_RMEM_PROC_FILE)) {
1244 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001245 return statusFromErrcode(ret);
1246 }
1247
1248 if (!WriteStringToFile(wmemValues, TCP_WMEM_PROC_FILE)) {
1249 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001250 return statusFromErrcode(ret);
1251 }
Chenbo Fengf5663d82018-11-08 16:10:48 -08001252 return binder::Status::ok();
1253}
1254
Luke Huang528af602018-08-29 19:06:05 +08001255binder::Status NetdNativeService::registerUnsolicitedEventListener(
1256 const android::sp<android::net::INetdUnsolicitedEventListener>& listener) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001257 ENFORCE_NETWORK_STACK_PERMISSIONS();
Bernie Innocenti9ee088d2019-02-01 17:14:32 +09001258 gCtls->eventReporter.registerUnsolEventListener(listener);
Luke Huang528af602018-08-29 19:06:05 +08001259 return binder::Status::ok();
1260}
1261
Luke Huange60bfd82019-04-26 11:39:31 +08001262binder::Status NetdNativeService::getOemNetd(android::sp<android::IBinder>* listener) {
1263 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang0e5e69d2019-03-06 15:42:38 +08001264 *listener = com::android::internal::net::OemNetdListener::getListener();
1265
Luke Huange60bfd82019-04-26 11:39:31 +08001266 return binder::Status::ok();
1267}
1268
Chiachang Wang00fc62f2019-12-04 20:38:26 +08001269binder::Status NetdNativeService::getFwmarkForNetwork(int32_t netId, MarkMaskParcel* markMask) {
1270 ENFORCE_NETWORK_STACK_PERMISSIONS();
1271
1272 Fwmark fwmark;
1273 fwmark.netId = netId;
1274 markMask->mask = FWMARK_NET_ID_MASK;
1275 markMask->mark = fwmark.intValue;
1276 return binder::Status::ok();
1277}
1278
Lorenzo Colittif0e051c2020-04-06 09:19:57 +00001279binder::Status NetdNativeService::tetherOffloadRuleAdd(const TetherOffloadRuleParcel& rule) {
Lorenzo Colittie801d3c2020-02-18 00:00:35 +09001280 ENFORCE_NETWORK_STACK_PERMISSIONS();
1281
Lorenzo Colittif0e051c2020-04-06 09:19:57 +00001282 return asBinderStatus(gCtls->tetherCtrl.addOffloadRule(rule));
Lorenzo Colittie801d3c2020-02-18 00:00:35 +09001283}
1284
Lorenzo Colittif0e051c2020-04-06 09:19:57 +00001285binder::Status NetdNativeService::tetherOffloadRuleRemove(const TetherOffloadRuleParcel& rule) {
Lorenzo Colittie801d3c2020-02-18 00:00:35 +09001286 ENFORCE_NETWORK_STACK_PERMISSIONS();
1287
Lorenzo Colittif0e051c2020-04-06 09:19:57 +00001288 return asBinderStatus(gCtls->tetherCtrl.removeOffloadRule(rule));
Lorenzo Colittie801d3c2020-02-18 00:00:35 +09001289}
1290
Hungming Chen41b0ed92020-06-02 00:13:20 +00001291namespace {
1292
1293constexpr const char UNUSED_IFNAME[] = "";
1294
1295TetherStatsParcel toTetherStatsParcel(const TetherController::TetherOffloadStats& stats) {
1296 TetherStatsParcel result;
1297 result.iface = UNUSED_IFNAME;
1298 result.rxBytes = stats.rxBytes;
1299 result.rxPackets = stats.rxPackets;
1300 result.txBytes = stats.txBytes;
1301 result.txPackets = stats.txPackets;
1302 result.ifIndex = stats.ifIndex;
1303 return result;
1304}
1305
1306} // namespace
1307
1308binder::Status NetdNativeService::tetherOffloadGetStats(
1309 std::vector<TetherStatsParcel>* tetherStatsParcelVec) {
1310 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
1311
1312 tetherStatsParcelVec->clear();
1313 const auto& statsList = gCtls->tetherCtrl.getTetherOffloadStats();
1314 if (!isOk(statsList)) {
1315 return asBinderStatus(statsList);
1316 }
1317 for (const auto& stats : statsList.value()) {
1318 tetherStatsParcelVec->push_back(toTetherStatsParcel(stats));
1319 }
1320 return binder::Status::ok();
1321}
1322
Hungming Chen93a39d22020-03-16 13:53:19 +08001323binder::Status NetdNativeService::tetherOffloadSetInterfaceQuota(int ifIndex, int64_t quotaBytes) {
1324 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
1325 int res = gCtls->tetherCtrl.setTetherOffloadInterfaceQuota(ifIndex, quotaBytes);
1326 return statusFromErrcode(res);
1327}
1328
Hungming Chenaf0dbed2020-04-17 20:00:27 +08001329binder::Status NetdNativeService::tetherOffloadGetAndClearStats(
1330 int ifIndex, android::net::TetherStatsParcel* tetherStats) {
1331 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
1332 const auto& stats = gCtls->tetherCtrl.getAndClearTetherOffloadStats(ifIndex);
1333 if (!stats.ok()) {
1334 return asBinderStatus(stats);
1335 }
1336 *tetherStats = toTetherStatsParcel(stats.value());
1337 return binder::Status::ok();
1338}
1339
Lorenzo Colittie4d626e2016-02-02 17:19:04 +09001340} // namespace net
1341} // namespace android