blob: 7e9e36352021c58a11b31d396ee0e2e72f3a6ca6 [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 Ma492f38f2019-04-08 19:07:05 -070022#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 Huang363587c2019-05-08 21:54:28 -070029#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>
Luke Huang363587c2019-05-08 21:54:28 -070033#include <json/value.h>
34#include <json/writer.h>
Logan Chien3f461482018-04-23 14:31:32 +080035#include <log/log.h>
Luke Huang363587c2019-05-08 21:54:28 -070036#include <netdutils/DumpWriter.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090037#include <utils/Errors.h>
Pierre Imaibeedec32016-04-13 06:44:51 +090038#include <utils/String16.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090039
Luke Huang363587c2019-05-08 21:54:28 -070040#include "BinderUtil.h"
Lorenzo Colitti89faa342016-02-26 11:38:47 +090041#include "Controllers.h"
Erik Kline55b06f82016-07-04 09:57:18 +090042#include "InterfaceController.h"
Mike Yu5ae61542018-10-19 22:11:43 +080043#include "NetdConstants.h" // SHA256_SIZE
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090044#include "NetdNativeService.h"
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090045#include "NetdPermissions.h"
Luke Huangbc8a80f2019-04-29 18:08:29 -070046#include "OemNetdListener.h"
Luke Huangb670d162018-08-23 20:01:13 +080047#include "Permission.h"
Erik Kline85890042018-05-25 19:19:11 +090048#include "Process.h"
Robin Leeb8087362016-03-30 18:43:08 +010049#include "RouteController.h"
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090050#include "SockDiag.h"
Robin Leeb8087362016-03-30 18:43:08 +010051#include "UidRanges.h"
Xiao Ma33d562a2018-12-16 16:27:38 +090052#include "android/net/BnNetd.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090053#include "netid_client.h" // NETID_UNSET
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090054
55using android::base::StringPrintf;
Chenbo Fengf5663d82018-11-08 16:10:48 -080056using android::base::WriteStringToFile;
Luke Huangcaebcbb2018-09-27 20:37:14 +080057using android::net::TetherStatsParcel;
Luke Huang94658ac2018-10-18 19:35:12 +090058using android::net::UidRangeParcel;
Luke Huangb257d612019-03-14 21:19:13 +080059using android::netdutils::DumpWriter;
60using android::netdutils::ScopedIndent;
Luke Huange203a152018-11-23 11:47:28 +080061using android::os::ParcelFileDescriptor;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090062
63namespace android {
64namespace net {
65
66namespace {
Erik Klineb31fd692018-06-06 20:50:11 +090067const char OPT_SHORT[] = "--short";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090068
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090069binder::Status checkAnyPermission(const std::vector<const char*>& permissions) {
Luke Huanga38b65c2018-09-26 16:31:03 +080070 pid_t pid = IPCThreadState::self()->getCallingPid();
71 uid_t uid = IPCThreadState::self()->getCallingUid();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090072
Luke Huanga38b65c2018-09-26 16:31:03 +080073 // If the caller is the system UID, don't check permissions.
74 // Otherwise, if the system server's binder thread pool is full, and all the threads are
75 // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492
76 //
77 // From a security perspective, there is currently no difference, because:
78 // 1. The only permissions we check in netd's binder interface are CONNECTIVITY_INTERNAL
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090079 // and NETWORK_STACK, which the system server always has (or MAINLINE_NETWORK_STACK, which
80 // is equivalent to having both CONNECTIVITY_INTERNAL and NETWORK_STACK).
Luke Huanga38b65c2018-09-26 16:31:03 +080081 // 2. AID_SYSTEM always has all permissions. See ActivityManager#checkComponentPermission.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090082 if (uid == AID_SYSTEM) {
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090083 return binder::Status::ok();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090084 }
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090085
86 for (const char* permission : permissions) {
87 if (checkPermission(String16(permission), pid, uid)) {
88 return binder::Status::ok();
89 }
90 }
91
92 auto err = StringPrintf("UID %d / PID %d does not have any of the following permissions: %s",
93 uid, pid, android::base::Join(permissions, ',').c_str());
94 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, err.c_str());
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090095}
96
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090097#define ENFORCE_ANY_PERMISSION(...) \
98 do { \
99 binder::Status status = checkAnyPermission({__VA_ARGS__}); \
100 if (!status.isOk()) { \
101 return status; \
102 } \
103 } while (0)
Robin Lee2cf56172016-09-13 18:55:42 +0900104
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900105#define NETD_LOCKING_RPC(lock, ... /* permissions */) \
106 ENFORCE_ANY_PERMISSION(__VA_ARGS__); \
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900107 std::lock_guard _lock(lock);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900108
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900109#define NETD_BIG_LOCK_RPC(... /* permissions */) NETD_LOCKING_RPC(gBigNetdLock, __VA_ARGS__)
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900110
Luke Huangf7782042018-08-08 13:13:04 +0800111#define RETURN_BINDER_STATUS_IF_NOT_OK(logEntry, res) \
112 do { \
113 if (!isOk((res))) { \
114 logErrorStatus((logEntry), (res)); \
115 return asBinderStatus((res)); \
116 } \
117 } while (0)
118
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900119#define ENFORCE_INTERNAL_PERMISSIONS() \
120 ENFORCE_ANY_PERMISSION(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK)
121
122#define ENFORCE_NETWORK_STACK_PERMISSIONS() \
123 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK)
124
Luke Huangf7782042018-08-08 13:13:04 +0800125void logErrorStatus(netdutils::LogEntry& logEntry, const netdutils::Status& status) {
126 gLog.log(logEntry.returns(status.code()).withAutomaticDuration());
127}
128
Bernie Innocenti97f388f2018-10-16 19:17:08 +0900129binder::Status asBinderStatus(const netdutils::Status& status) {
130 if (isOk(status)) {
131 return binder::Status::ok();
132 }
133 return binder::Status::fromServiceSpecificError(status.code(), status.msg().c_str());
134}
135
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900136inline binder::Status statusFromErrcode(int ret) {
137 if (ret) {
138 return binder::Status::fromServiceSpecificError(-ret, strerror(-ret));
139 }
140 return binder::Status::ok();
141}
142
Erik Klineb31fd692018-06-06 20:50:11 +0900143bool contains(const Vector<String16>& words, const String16& word) {
144 for (const auto& w : words) {
145 if (w == word) return true;
146 }
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900147
Erik Klineb31fd692018-06-06 20:50:11 +0900148 return false;
149}
150
151} // namespace
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900152
Luke Huang363587c2019-05-08 21:54:28 -0700153NetdNativeService::NetdNativeService() {
154 // register log callback to BnNetd::logFunc
155 BnNetd::logFunc = std::bind(binderCallLogFn, std::placeholders::_1,
156 [](const std::string& msg) { gLog.info("%s", msg.c_str()); });
157}
158
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900159status_t NetdNativeService::start() {
160 IPCThreadState::self()->disableBackgroundScheduling(true);
Erik Klineb31fd692018-06-06 20:50:11 +0900161 const status_t ret = BinderService<NetdNativeService>::publish();
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900162 if (ret != android::OK) {
163 return ret;
164 }
165 sp<ProcessState> ps(ProcessState::self());
166 ps->startThreadPool();
167 ps->giveThreadPoolName();
Xiao Ma33d562a2018-12-16 16:27:38 +0900168
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900169 return android::OK;
170}
171
Hugo Benichi7b314e12018-01-15 21:54:00 +0900172status_t NetdNativeService::dump(int fd, const Vector<String16> &args) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900173 const binder::Status dump_permission = checkAnyPermission({PERM_DUMP});
Erik Kline2d3a1632016-03-15 16:33:48 +0900174 if (!dump_permission.isOk()) {
175 const String8 msg(dump_permission.toString8());
176 write(fd, msg.string(), msg.size());
177 return PERMISSION_DENIED;
178 }
179
180 // This method does not grab any locks. If individual classes need locking
181 // their dump() methods MUST handle locking appropriately.
Hugo Benichi7b314e12018-01-15 21:54:00 +0900182
Erik Kline2d3a1632016-03-15 16:33:48 +0900183 DumpWriter dw(fd);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900184
185 if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) {
186 dw.blankline();
187 gCtls->tcpSocketMonitor.dump(dw);
188 dw.blankline();
189 return NO_ERROR;
190 }
191
Chenbo Fengef297172018-03-26 10:53:33 -0700192 if (!args.isEmpty() && args[0] == TrafficController::DUMP_KEYWORD) {
193 dw.blankline();
194 gCtls->trafficCtrl.dump(dw, true);
195 dw.blankline();
196 return NO_ERROR;
197 }
198
Erik Kline85890042018-05-25 19:19:11 +0900199 process::dump(dw);
Erik Kline2d3a1632016-03-15 16:33:48 +0900200 dw.blankline();
201 gCtls->netCtrl.dump(dw);
202 dw.blankline();
203
Chenbo Fengef297172018-03-26 10:53:33 -0700204 gCtls->trafficCtrl.dump(dw, false);
205 dw.blankline();
206
Benedict Wongaf855432018-05-10 17:07:37 -0700207 gCtls->xfrmCtrl.dump(dw);
208 dw.blankline();
209
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700210 gCtls->clatdCtrl.dump(dw);
211 dw.blankline();
212
Erik Klineb31fd692018-06-06 20:50:11 +0900213 {
214 ScopedIndent indentLog(dw);
215 if (contains(args, String16(OPT_SHORT))) {
216 dw.println("Log: <omitted>");
217 } else {
218 dw.println("Log:");
219 ScopedIndent indentLogEntries(dw);
220 gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); });
221 }
222 dw.blankline();
223 }
224
Luke Huang528af602018-08-29 19:06:05 +0800225 {
226 ScopedIndent indentLog(dw);
227 if (contains(args, String16(OPT_SHORT))) {
228 dw.println("UnsolicitedLog: <omitted>");
229 } else {
230 dw.println("UnsolicitedLog:");
231 ScopedIndent indentLogEntries(dw);
232 gUnsolicitedLog.forEachEntry(
233 [&dw](const std::string& entry) mutable { dw.println(entry); });
234 }
235 dw.blankline();
236 }
237
Erik Kline2d3a1632016-03-15 16:33:48 +0900238 return NO_ERROR;
239}
240
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900241binder::Status NetdNativeService::isAlive(bool *alive) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900242 NETD_BIG_LOCK_RPC(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900243
244 *alive = true;
Erik Klineb31fd692018-06-06 20:50:11 +0900245
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900246 return binder::Status::ok();
247}
248
Erik Klinef52d4522018-03-14 15:01:46 +0900249binder::Status NetdNativeService::firewallReplaceUidChain(const std::string& chainName,
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900250 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900251 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_CONNECTIVITY_INTERNAL,
252 PERM_MAINLINE_NETWORK_STACK);
Erik Klinef52d4522018-03-14 15:01:46 +0900253 int err = gCtls->firewallCtrl.replaceUidChain(chainName, isWhitelist, uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900254 *ret = (err == 0);
255 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900256}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900257
258binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900259 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_CONNECTIVITY_INTERNAL,
260 PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900261 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
262 *ret = (err == 0);
263 return binder::Status::ok();
264}
265
Luke Huang531f5d32018-08-03 15:19:05 +0800266binder::Status NetdNativeService::bandwidthSetInterfaceQuota(const std::string& ifName,
267 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900268 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800269 int res = gCtls->bandwidthCtrl.setInterfaceQuota(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800270 return statusFromErrcode(res);
271}
272
273binder::Status NetdNativeService::bandwidthRemoveInterfaceQuota(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900274 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800275 int res = gCtls->bandwidthCtrl.removeInterfaceQuota(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800276 return statusFromErrcode(res);
277}
278
279binder::Status NetdNativeService::bandwidthSetInterfaceAlert(const std::string& ifName,
280 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900281 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800282 int res = gCtls->bandwidthCtrl.setInterfaceAlert(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800283 return statusFromErrcode(res);
284}
285
286binder::Status NetdNativeService::bandwidthRemoveInterfaceAlert(const std::string& ifName) {
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.removeInterfaceAlert(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800289 return statusFromErrcode(res);
290}
291
292binder::Status NetdNativeService::bandwidthSetGlobalAlert(int64_t bytes) {
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.setGlobalAlert(bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800295 return statusFromErrcode(res);
296}
297
298binder::Status NetdNativeService::bandwidthAddNaughtyApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900299 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800300 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
301 int res = gCtls->bandwidthCtrl.addNaughtyApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800302 return statusFromErrcode(res);
303}
304
305binder::Status NetdNativeService::bandwidthRemoveNaughtyApp(int32_t uid) {
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 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
308 int res = gCtls->bandwidthCtrl.removeNaughtyApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800309 return statusFromErrcode(res);
310}
311
312binder::Status NetdNativeService::bandwidthAddNiceApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900313 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800314 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
315 int res = gCtls->bandwidthCtrl.addNiceApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800316 return statusFromErrcode(res);
317}
318
319binder::Status NetdNativeService::bandwidthRemoveNiceApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900320 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800321 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
322 int res = gCtls->bandwidthCtrl.removeNiceApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800323 return statusFromErrcode(res);
324}
325
Uldiniad9748e942018-03-01 08:54:00 -0500326binder::Status NetdNativeService::bandwidthAddRestrictAppOnInterface(const std::string& usecase,
327 const std::string& ifName, int32_t uid) {
328 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
329 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
330 int res = gCtls->bandwidthCtrl.addRestrictAppsOnInterface(usecase, ifName, appStrUids);
331 return statusFromErrcode(res);
332}
333
334binder::Status NetdNativeService::bandwidthRemoveRestrictAppOnInterface(const std::string& usecase,
335 const std::string& ifName, int32_t uid) {
336 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
337 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
338 int res = gCtls->bandwidthCtrl.removeRestrictAppsOnInterface(usecase, ifName, appStrUids);
339 return statusFromErrcode(res);
340}
341
Luke Huangb670d162018-08-23 20:01:13 +0800342binder::Status NetdNativeService::networkCreatePhysical(int32_t netId, int32_t permission) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900343 ENFORCE_INTERNAL_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +0800344 int ret = gCtls->netCtrl.createPhysicalNetwork(netId, convertPermission(permission));
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900345 return statusFromErrcode(ret);
346}
347
cken67cd14c2018-12-05 17:26:59 +0900348binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool secure) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900349 ENFORCE_NETWORK_STACK_PERMISSIONS();
cken67cd14c2018-12-05 17:26:59 +0900350 int ret = gCtls->netCtrl.createVirtualNetwork(netId, secure);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900351 return statusFromErrcode(ret);
352}
353
354binder::Status NetdNativeService::networkDestroy(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900355 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangd33a8f42019-03-14 16:10:04 +0800356 // NetworkController::destroyNetwork is thread-safe.
Mike Yu4fe1b9c2019-01-29 17:50:19 +0800357 const int ret = gCtls->netCtrl.destroyNetwork(netId);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900358 return statusFromErrcode(ret);
359}
360
361binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900362 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900363 int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
364 return statusFromErrcode(ret);
365}
366
367binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900368 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900369 int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
370 return statusFromErrcode(ret);
371}
372
Luke Huang94658ac2018-10-18 19:35:12 +0900373binder::Status NetdNativeService::networkAddUidRanges(
374 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900375 // NetworkController::addUsersToNetwork is thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900376 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900377 int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray));
378 return statusFromErrcode(ret);
379}
380
Luke Huang94658ac2018-10-18 19:35:12 +0900381binder::Status NetdNativeService::networkRemoveUidRanges(
382 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900383 // NetworkController::removeUsersFromNetwork is thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900384 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900385 int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray));
386 return statusFromErrcode(ret);
387}
388
Luke Huang94658ac2018-10-18 19:35:12 +0900389binder::Status NetdNativeService::networkRejectNonSecureVpn(
390 bool add, const std::vector<UidRangeParcel>& uidRangeArray) {
Robin Leeb8087362016-03-30 18:43:08 +0100391 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
392 // it should be possible to use the same lock as NetworkController. However, every call through
393 // the CommandListener "network" command will need to hold this lock too, not just the ones that
394 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
395 // look at routes, but it's not enough here).
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900396 NETD_BIG_LOCK_RPC(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900397 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100398
399 int err;
400 if (add) {
401 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
402 } else {
403 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
404 }
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900405 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100406}
407
Luke Huang94658ac2018-10-18 19:35:12 +0900408binder::Status NetdNativeService::socketDestroy(const std::vector<UidRangeParcel>& uids,
409 const std::vector<int32_t>& skipUids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900410 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900411
412 SockDiag sd;
413 if (!sd.open()) {
414 return binder::Status::fromServiceSpecificError(EIO,
415 String8("Could not open SOCK_DIAG socket"));
416 }
417
418 UidRanges uidRanges(uids);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +0900419 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
420 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900421 if (err) {
422 return binder::Status::fromServiceSpecificError(-err,
423 String8::format("destroySockets: %s", strerror(-err)));
424 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900425 return binder::Status::ok();
426}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900427
Erik Klinef48e4dd2016-07-18 04:02:07 +0900428binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900429 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Erik Klinef48e4dd2016-07-18 04:02:07 +0900430 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
431 return binder::Status::ok();
432}
433
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900434namespace {
435
Luke Huangcaebcbb2018-09-27 20:37:14 +0800436void tetherAddStatsByInterface(TetherController::TetherStats* tetherStatsParcel,
437 const TetherController::TetherStats& tetherStats) {
438 if (tetherStatsParcel->extIface == tetherStats.extIface) {
439 tetherStatsParcel->rxBytes += tetherStats.rxBytes;
440 tetherStatsParcel->rxPackets += tetherStats.rxPackets;
441 tetherStatsParcel->txBytes += tetherStats.txBytes;
442 tetherStatsParcel->txPackets += tetherStats.txPackets;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900443 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800444}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900445
Luke Huangcaebcbb2018-09-27 20:37:14 +0800446TetherStatsParcel toTetherStatsParcel(const TetherController::TetherStats& stats) {
447 TetherStatsParcel result;
448 result.iface = stats.extIface;
449 result.rxBytes = stats.rxBytes;
450 result.rxPackets = stats.rxPackets;
451 result.txBytes = stats.txBytes;
452 result.txPackets = stats.txPackets;
453 return result;
454}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900455
Luke Huangcaebcbb2018-09-27 20:37:14 +0800456void setTetherStatsParcelVecByInterface(std::vector<TetherStatsParcel>* tetherStatsVec,
457 const TetherController::TetherStatsList& statsList) {
458 std::map<std::string, TetherController::TetherStats> statsMap;
459 for (const auto& stats : statsList) {
460 auto iter = statsMap.find(stats.extIface);
461 if (iter != statsMap.end()) {
462 tetherAddStatsByInterface(&(iter->second), stats);
463 } else {
464 statsMap.insert(
465 std::pair<std::string, TetherController::TetherStats>(stats.extIface, stats));
466 }
467 }
468 for (auto iter = statsMap.begin(); iter != statsMap.end(); iter++) {
469 tetherStatsVec->push_back(toTetherStatsParcel(iter->second));
470 }
471}
472
473std::vector<std::string> tetherStatsParcelVecToStringVec(std::vector<TetherStatsParcel>* tVec) {
474 std::vector<std::string> result;
475 for (const auto& t : *tVec) {
476 result.push_back(StringPrintf("%s:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64,
477 t.iface.c_str(), t.rxBytes, t.rxPackets, t.txBytes,
478 t.txPackets));
479 }
480 return result;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900481}
482
483} // namespace
484
Luke Huangcaebcbb2018-09-27 20:37:14 +0800485binder::Status NetdNativeService::tetherGetStats(
486 std::vector<TetherStatsParcel>* tetherStatsParcelVec) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900487 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900488 const auto& statsList = gCtls->tetherCtrl.getTetherStats();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900489 if (!isOk(statsList)) {
Nathan Harold28ccfef2018-03-16 19:02:47 -0700490 return asBinderStatus(statsList);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900491 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800492 setTetherStatsParcelVecByInterface(tetherStatsParcelVec, statsList.value());
493 auto statsResults = tetherStatsParcelVecToStringVec(tetherStatsParcelVec);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900494 return binder::Status::ok();
495}
496
Erik Kline53c20882016-08-02 15:22:53 +0900497binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
498 const std::string &addrString, int prefixLength) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900499 ENFORCE_INTERNAL_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900500 const int err = InterfaceController::addAddress(
501 ifName.c_str(), addrString.c_str(), prefixLength);
502 if (err != 0) {
503 return binder::Status::fromServiceSpecificError(-err,
504 String8::format("InterfaceController error: %s", strerror(-err)));
505 }
506 return binder::Status::ok();
507}
508
509binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
510 const std::string &addrString, int prefixLength) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900511 ENFORCE_INTERNAL_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900512 const int err = InterfaceController::delAddress(
513 ifName.c_str(), addrString.c_str(), prefixLength);
514 if (err != 0) {
515 return binder::Status::fromServiceSpecificError(-err,
516 String8::format("InterfaceController error: %s", strerror(-err)));
517 }
518 return binder::Status::ok();
519}
520
Erik Kline38e51f12018-09-06 20:14:44 +0900521namespace {
Erik Kline55b06f82016-07-04 09:57:18 +0900522
Erik Kline38e51f12018-09-06 20:14:44 +0900523std::tuple<binder::Status, const char*, const char*> getPathComponents(int32_t ipversion,
524 int32_t category) {
525 const char* ipversionStr = nullptr;
526 switch (ipversion) {
Erik Kline55b06f82016-07-04 09:57:18 +0900527 case INetd::IPV4:
Erik Kline38e51f12018-09-06 20:14:44 +0900528 ipversionStr = "ipv4";
Erik Kline55b06f82016-07-04 09:57:18 +0900529 break;
530 case INetd::IPV6:
Erik Kline38e51f12018-09-06 20:14:44 +0900531 ipversionStr = "ipv6";
Erik Kline55b06f82016-07-04 09:57:18 +0900532 break;
533 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900534 return {binder::Status::fromServiceSpecificError(EAFNOSUPPORT, "Bad IP version"),
535 nullptr, nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900536 }
537
Erik Kline38e51f12018-09-06 20:14:44 +0900538 const char* whichStr = nullptr;
539 switch (category) {
Erik Kline55b06f82016-07-04 09:57:18 +0900540 case INetd::CONF:
541 whichStr = "conf";
542 break;
543 case INetd::NEIGH:
544 whichStr = "neigh";
545 break;
546 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900547 return {binder::Status::fromServiceSpecificError(EINVAL, "Bad category"), nullptr,
548 nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900549 }
550
Erik Kline38e51f12018-09-06 20:14:44 +0900551 return {binder::Status::ok(), ipversionStr, whichStr};
552}
553
554} // namespace
555
556binder::Status NetdNativeService::getProcSysNet(int32_t ipversion, int32_t which,
557 const std::string& ifname,
558 const std::string& parameter, std::string* value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900559 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900560 const auto pathParts = getPathComponents(ipversion, which);
561 const auto& pathStatus = std::get<0>(pathParts);
562 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900563 return pathStatus;
Erik Kline55b06f82016-07-04 09:57:18 +0900564 }
Erik Kline38e51f12018-09-06 20:14:44 +0900565
566 const int err = InterfaceController::getParameter(std::get<1>(pathParts),
567 std::get<2>(pathParts), ifname.c_str(),
568 parameter.c_str(), value);
Erik Kline38e51f12018-09-06 20:14:44 +0900569 return statusFromErrcode(err);
570}
571
572binder::Status NetdNativeService::setProcSysNet(int32_t ipversion, int32_t which,
573 const std::string& ifname,
574 const std::string& parameter,
575 const std::string& value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900576 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900577 const auto pathParts = getPathComponents(ipversion, which);
578 const auto& pathStatus = std::get<0>(pathParts);
579 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900580 return pathStatus;
581 }
582
583 const int err = InterfaceController::setParameter(std::get<1>(pathParts),
584 std::get<2>(pathParts), ifname.c_str(),
585 parameter.c_str(), value.c_str());
Erik Kline38e51f12018-09-06 20:14:44 +0900586 return statusFromErrcode(err);
Erik Kline55b06f82016-07-04 09:57:18 +0900587}
588
Luke Huange203a152018-11-23 11:47:28 +0800589binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const ParcelFileDescriptor& socket,
590 int newUid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900591 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900592 gLog.log("ipSecSetEncapSocketOwner()");
Benedict Wongb2daefb2017-12-06 22:05:46 -0800593
594 uid_t callerUid = IPCThreadState::self()->getCallingUid();
Luke Huange203a152018-11-23 11:47:28 +0800595 return asBinderStatus(
596 gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket.get(), newUid, callerUid));
Benedict Wongb2daefb2017-12-06 22:05:46 -0800597}
598
Nathan Harold1a371532017-01-30 12:30:48 -0800599binder::Status NetdNativeService::ipSecAllocateSpi(
600 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800601 const std::string& sourceAddress,
602 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800603 int32_t inSpi,
604 int32_t* outSpi) {
605 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900606 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900607 gLog.log("ipSecAllocateSpi()");
ludi6e8eccd2017-08-14 14:40:37 -0700608 return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi(
Nathan Harold1a371532017-01-30 12:30:48 -0800609 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800610 sourceAddress,
611 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800612 inSpi,
613 outSpi));
614}
615
616binder::Status NetdNativeService::ipSecAddSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700617 int32_t transformId, int32_t mode, const std::string& sourceAddress,
618 const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi,
619 int32_t markValue, int32_t markMask, const std::string& authAlgo,
620 const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo,
621 const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo,
622 const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType,
623 int32_t encapLocalPort, int32_t encapRemotePort, int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800624 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900625 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900626 gLog.log("ipSecAddSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700627 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation(
Benedict Wongad600cb2018-05-14 17:22:35 -0700628 transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue,
629 markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits,
Benedict Wonga450e722018-05-07 10:29:02 -0700630 aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort,
631 interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800632}
633
634binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700635 int32_t transformId, const std::string& sourceAddress,
636 const std::string& destinationAddress, int32_t spi, int32_t markValue, int32_t markMask,
637 int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800638 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900639 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900640 gLog.log("ipSecDeleteSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700641 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700642 transformId, sourceAddress, destinationAddress, spi, markValue, markMask, interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800643}
644
645binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800646 const ParcelFileDescriptor& socket, int32_t transformId, int32_t direction,
647 const std::string& sourceAddress, const std::string& destinationAddress, int32_t spi) {
Nathan Harold1a371532017-01-30 12:30:48 -0800648 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900649 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900650 gLog.log("ipSecApplyTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700651 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800652 socket.get(), transformId, direction, sourceAddress, destinationAddress, spi));
Nathan Harold1a371532017-01-30 12:30:48 -0800653}
654
655binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800656 const ParcelFileDescriptor& socket) {
Nathan Harold1a371532017-01-30 12:30:48 -0800657 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900658 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900659 gLog.log("ipSecRemoveTransportModeTransform()");
Luke Huange203a152018-11-23 11:47:28 +0800660 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(socket.get()));
Nathan Harold1a371532017-01-30 12:30:48 -0800661}
662
Benedict Wonga04ffa72018-05-09 21:42:42 -0700663binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
664 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700665 const std::string& tmplSrcAddress,
666 const std::string& tmplDstAddress,
667 int32_t spi, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700668 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800669 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900670 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900671 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800672 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700673 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700674 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800675}
676
Benedict Wonga450e722018-05-07 10:29:02 -0700677binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(
678 int32_t transformId, int32_t selAddrFamily, int32_t direction,
679 const std::string& tmplSrcAddress, const std::string& tmplDstAddress, int32_t spi,
680 int32_t markValue, int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800681 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900682 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900683 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800684 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700685 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700686 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800687}
688
Benedict Wonga04ffa72018-05-09 21:42:42 -0700689binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId,
690 int32_t selAddrFamily,
691 int32_t direction, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700692 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800693 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900694 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900695 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800696 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
Benedict Wonga450e722018-05-07 10:29:02 -0700697 transformId, selAddrFamily, direction, markValue, markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800698}
699
Benedict Wong319f17e2018-05-15 17:06:44 -0700700binder::Status NetdNativeService::ipSecAddTunnelInterface(const std::string& deviceName,
701 const std::string& localAddress,
702 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700703 int32_t iKey, int32_t oKey,
704 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800705 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900706 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700707 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700708 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, false);
Benedict Wong319f17e2018-05-15 17:06:44 -0700709 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800710}
711
Benedict Wong319f17e2018-05-15 17:06:44 -0700712binder::Status NetdNativeService::ipSecUpdateTunnelInterface(const std::string& deviceName,
713 const std::string& localAddress,
714 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700715 int32_t iKey, int32_t oKey,
716 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800717 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900718 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700719 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700720 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, true);
Benedict Wong319f17e2018-05-15 17:06:44 -0700721 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800722}
723
Benedict Wong319f17e2018-05-15 17:06:44 -0700724binder::Status NetdNativeService::ipSecRemoveTunnelInterface(const std::string& deviceName) {
manojboopathi8707f232018-01-02 14:45:47 -0800725 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900726 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700727 netdutils::Status result = gCtls->xfrmCtrl.ipSecRemoveTunnelInterface(deviceName);
Benedict Wong319f17e2018-05-15 17:06:44 -0700728 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800729}
730
Joel Scherpelzde937962017-06-01 13:20:21 +0900731binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
732 int32_t mode) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900733 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700734 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900735}
736
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900737binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName,
738 const std::string& prefix, int32_t mark,
739 int32_t mask) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900740 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700741 return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900742}
743
744binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
745 const std::string& prefix, int32_t mark,
746 int32_t mask) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900747 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700748 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900749}
750
Chenbo Fengf9fbfff2019-04-10 12:26:06 -0700751binder::Status NetdNativeService::trafficSwapActiveStatsMap() {
752 ENFORCE_NETWORK_STACK_PERMISSIONS();
753 return asBinderStatus(gCtls->trafficCtrl.swapActiveStatsMap());
754}
755
Luke Huang0051a622018-07-23 20:30:16 +0800756binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout,
757 const std::string& classLabel) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900758 NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang0051a622018-07-23 20:30:16 +0800759 int res =
760 gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str());
Luke Huang0051a622018-07-23 20:30:16 +0800761 return statusFromErrcode(res);
762}
763
764binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName,
765 int32_t timeout,
766 const std::string& classLabel) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900767 NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang0051a622018-07-23 20:30:16 +0800768 int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout,
769 classLabel.c_str());
Luke Huang0051a622018-07-23 20:30:16 +0800770 return statusFromErrcode(res);
771}
Luke Huanga67dd562018-07-17 19:58:25 +0800772
773binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900774 NETD_LOCKING_RPC(gCtls->strictCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huanga67dd562018-07-17 19:58:25 +0800775 StrictPenalty penalty;
776 switch (policyPenalty) {
777 case INetd::PENALTY_POLICY_REJECT:
778 penalty = REJECT;
779 break;
780 case INetd::PENALTY_POLICY_LOG:
781 penalty = LOG;
782 break;
783 case INetd::PENALTY_POLICY_ACCEPT:
784 penalty = ACCEPT;
785 break;
786 default:
787 return statusFromErrcode(-EINVAL);
788 break;
789 }
790 int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty);
Luke Huanga67dd562018-07-17 19:58:25 +0800791 return statusFromErrcode(res);
792}
Luke Huange64fa382018-07-24 16:38:22 +0800793
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900794binder::Status NetdNativeService::clatdStart(const std::string& ifName,
795 const std::string& nat64Prefix, std::string* v6Addr) {
Maciej Żenczykowski2c460752019-03-30 03:32:51 -0700796 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900797 int res = gCtls->clatdCtrl.startClatd(ifName.c_str(), nat64Prefix, v6Addr);
Luke Huang6d301232018-08-01 14:05:18 +0800798 return statusFromErrcode(res);
799}
800
801binder::Status NetdNativeService::clatdStop(const std::string& ifName) {
Maciej Żenczykowski2c460752019-03-30 03:32:51 -0700802 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang6d301232018-08-01 14:05:18 +0800803 int res = gCtls->clatdCtrl.stopClatd(ifName.c_str());
Luke Huang6d301232018-08-01 14:05:18 +0800804 return statusFromErrcode(res);
805}
Luke Huanga67dd562018-07-17 19:58:25 +0800806
Luke Huang457d4702018-08-16 15:39:15 +0800807binder::Status NetdNativeService::ipfwdEnabled(bool* status) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900808 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang728cf4c2019-03-14 19:43:02 +0800809 *status = (gCtls->tetherCtrl.getIpfwdRequesterList().size() > 0) ? true : false;
810 return binder::Status::ok();
811}
812
813binder::Status NetdNativeService::ipfwdGetRequesterList(std::vector<std::string>* requesterList) {
814 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
815 for (const auto& requester : gCtls->tetherCtrl.getIpfwdRequesterList()) {
816 requesterList->push_back(requester);
817 }
Luke Huang457d4702018-08-16 15:39:15 +0800818 return binder::Status::ok();
819}
820
821binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900822 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang457d4702018-08-16 15:39:15 +0800823 int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
Luke Huang457d4702018-08-16 15:39:15 +0800824 return statusFromErrcode(res);
825}
826
827binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900828 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang457d4702018-08-16 15:39:15 +0800829 int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
Luke Huang457d4702018-08-16 15:39:15 +0800830 return statusFromErrcode(res);
831}
832
833binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface,
834 const std::string& toIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900835 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang457d4702018-08-16 15:39:15 +0800836 int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str());
Luke Huang457d4702018-08-16 15:39:15 +0800837 return statusFromErrcode(res);
838}
839
840binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface,
841 const std::string& toIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900842 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang457d4702018-08-16 15:39:15 +0800843 int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str());
Luke Huang457d4702018-08-16 15:39:15 +0800844 return statusFromErrcode(res);
845}
846
Luke Huangf7782042018-08-08 13:13:04 +0800847namespace {
Luke Huangf7782042018-08-08 13:13:04 +0800848std::string addCurlyBrackets(const std::string& s) {
849 return "{" + s + "}";
850}
851
852} // namespace
Xiao Ma33d562a2018-12-16 16:27:38 +0900853
Luke Huangf7782042018-08-08 13:13:04 +0800854binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900855 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800856 const auto& ifaceList = InterfaceController::getIfaceNames();
Luke Huangf7782042018-08-08 13:13:04 +0800857
858 interfaceListResult->clear();
859 interfaceListResult->reserve(ifaceList.value().size());
860 interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()),
861 end(ifaceList.value()));
862
Luke Huangf7782042018-08-08 13:13:04 +0800863 return binder::Status::ok();
864}
865
866std::string interfaceConfigurationParcelToString(const InterfaceConfigurationParcel& cfg) {
867 std::vector<std::string> result{cfg.ifName, cfg.hwAddr, cfg.ipv4Addr,
868 std::to_string(cfg.prefixLength)};
869 result.insert(end(result), begin(cfg.flags), end(cfg.flags));
870 return addCurlyBrackets(base::Join(result, ", "));
871}
872
873binder::Status NetdNativeService::interfaceGetCfg(
874 const std::string& ifName, InterfaceConfigurationParcel* interfaceGetCfgResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900875 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800876 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
877
878 const auto& cfgRes = InterfaceController::getCfg(ifName);
879 RETURN_BINDER_STATUS_IF_NOT_OK(entry, cfgRes);
880
881 *interfaceGetCfgResult = cfgRes.value();
882 gLog.log(entry.returns(interfaceConfigurationParcelToString(*interfaceGetCfgResult))
883 .withAutomaticDuration());
884 return binder::Status::ok();
885}
886
887binder::Status NetdNativeService::interfaceSetCfg(const InterfaceConfigurationParcel& cfg) {
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 auto entry = gLog.newEntry()
890 .prettyFunction(__PRETTY_FUNCTION__)
891 .arg(interfaceConfigurationParcelToString(cfg));
892
893 const auto& res = InterfaceController::setCfg(cfg);
894 RETURN_BINDER_STATUS_IF_NOT_OK(entry, res);
895
896 gLog.log(entry.withAutomaticDuration());
897 return binder::Status::ok();
898}
899
900binder::Status NetdNativeService::interfaceSetIPv6PrivacyExtensions(const std::string& ifName,
901 bool enable) {
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 int res = InterfaceController::setIPv6PrivacyExtensions(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800904 return statusFromErrcode(res);
905}
906
907binder::Status NetdNativeService::interfaceClearAddrs(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900908 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800909 int res = InterfaceController::clearAddrs(ifName.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800910 return statusFromErrcode(res);
911}
912
913binder::Status NetdNativeService::interfaceSetEnableIPv6(const std::string& ifName, bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900914 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800915 int res = InterfaceController::setEnableIPv6(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800916 return statusFromErrcode(res);
917}
918
919binder::Status NetdNativeService::interfaceSetMtu(const std::string& ifName, int32_t mtuValue) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900920 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800921 std::string mtu = std::to_string(mtuValue);
922 int res = InterfaceController::setMtu(ifName.c_str(), mtu.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800923 return statusFromErrcode(res);
924}
925
Luke Huangb5733d72018-08-21 17:17:19 +0800926binder::Status NetdNativeService::tetherStart(const std::vector<std::string>& dhcpRanges) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900927 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800928 if (dhcpRanges.size() % 2 == 1) {
929 return statusFromErrcode(-EINVAL);
930 }
931 int res = gCtls->tetherCtrl.startTethering(dhcpRanges);
Luke Huangb5733d72018-08-21 17:17:19 +0800932 return statusFromErrcode(res);
933}
934
935binder::Status NetdNativeService::tetherStop() {
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.stopTethering();
Luke Huangb5733d72018-08-21 17:17:19 +0800938 return statusFromErrcode(res);
939}
940
941binder::Status NetdNativeService::tetherIsEnabled(bool* enabled) {
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 *enabled = gCtls->tetherCtrl.isTetheringStarted();
Luke Huangb5733d72018-08-21 17:17:19 +0800944 return binder::Status::ok();
945}
946
947binder::Status NetdNativeService::tetherInterfaceAdd(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900948 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800949 int res = gCtls->tetherCtrl.tetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800950 return statusFromErrcode(res);
951}
952
953binder::Status NetdNativeService::tetherInterfaceRemove(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900954 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800955 int res = gCtls->tetherCtrl.untetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800956 return statusFromErrcode(res);
957}
958
959binder::Status NetdNativeService::tetherInterfaceList(std::vector<std::string>* ifList) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900960 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800961 for (const auto& ifname : gCtls->tetherCtrl.getTetheredInterfaceList()) {
962 ifList->push_back(ifname);
963 }
Luke Huangb5733d72018-08-21 17:17:19 +0800964 return binder::Status::ok();
965}
966
967binder::Status NetdNativeService::tetherDnsSet(int32_t netId,
968 const std::vector<std::string>& dnsAddrs) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900969 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800970 int res = gCtls->tetherCtrl.setDnsForwarders(netId, dnsAddrs);
Luke Huangb5733d72018-08-21 17:17:19 +0800971 return statusFromErrcode(res);
972}
973
974binder::Status NetdNativeService::tetherDnsList(std::vector<std::string>* dnsList) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900975 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800976 for (const auto& fwdr : gCtls->tetherCtrl.getDnsForwarders()) {
977 dnsList->push_back(fwdr);
978 }
Luke Huangb5733d72018-08-21 17:17:19 +0800979 return binder::Status::ok();
980}
981
Luke Huangb670d162018-08-23 20:01:13 +0800982binder::Status NetdNativeService::networkAddRoute(int32_t netId, const std::string& ifName,
983 const std::string& destination,
984 const std::string& nextHop) {
985 // Public methods of NetworkController are thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900986 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +0800987 bool legacy = false;
988 uid_t uid = 0; // UID is only meaningful for legacy routes.
989 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
990 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
Luke Huangb670d162018-08-23 20:01:13 +0800991 return statusFromErrcode(res);
992}
993
994binder::Status NetdNativeService::networkRemoveRoute(int32_t netId, const std::string& ifName,
995 const std::string& destination,
996 const std::string& nextHop) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900997 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +0800998 bool legacy = false;
999 uid_t uid = 0; // UID is only meaningful for legacy routes.
1000 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1001 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
Luke Huangb670d162018-08-23 20:01:13 +08001002 return statusFromErrcode(res);
1003}
1004
1005binder::Status NetdNativeService::networkAddLegacyRoute(int32_t netId, const std::string& ifName,
1006 const std::string& destination,
1007 const std::string& nextHop, int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001008 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001009 bool legacy = true;
1010 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1011 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1012 (uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001013 return statusFromErrcode(res);
1014}
1015
1016binder::Status NetdNativeService::networkRemoveLegacyRoute(int32_t netId, const std::string& ifName,
1017 const std::string& destination,
1018 const std::string& nextHop,
1019 int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001020 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001021 bool legacy = true;
1022 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1023 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1024 (uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001025 return statusFromErrcode(res);
1026}
1027
1028binder::Status NetdNativeService::networkGetDefault(int32_t* netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001029 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001030 *netId = gCtls->netCtrl.getDefaultNetwork();
Luke Huangb670d162018-08-23 20:01:13 +08001031 return binder::Status::ok();
1032}
1033
1034binder::Status NetdNativeService::networkSetDefault(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001035 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001036 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001037 return statusFromErrcode(res);
1038}
1039
1040binder::Status NetdNativeService::networkClearDefault() {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001041 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001042 unsigned netId = NETID_UNSET;
1043 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001044 return statusFromErrcode(res);
1045}
1046
1047std::vector<uid_t> NetdNativeService::intsToUids(const std::vector<int32_t>& intUids) {
1048 return {begin(intUids), end(intUids)};
1049}
1050
1051Permission NetdNativeService::convertPermission(int32_t permission) {
1052 switch (permission) {
1053 case INetd::PERMISSION_NETWORK:
1054 return Permission::PERMISSION_NETWORK;
1055 case INetd::PERMISSION_SYSTEM:
1056 return Permission::PERMISSION_SYSTEM;
1057 default:
1058 return Permission::PERMISSION_NONE;
1059 }
1060}
1061
1062binder::Status NetdNativeService::networkSetPermissionForNetwork(int32_t netId,
1063 int32_t permission) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001064 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001065 std::vector<unsigned> netIds = {(unsigned) netId};
1066 int res = gCtls->netCtrl.setPermissionForNetworks(convertPermission(permission), netIds);
Luke Huangb670d162018-08-23 20:01:13 +08001067 return statusFromErrcode(res);
1068}
1069
1070binder::Status NetdNativeService::networkSetPermissionForUser(int32_t permission,
1071 const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001072 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001073 gCtls->netCtrl.setPermissionForUsers(convertPermission(permission), intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001074 return binder::Status::ok();
1075}
1076
1077binder::Status NetdNativeService::networkClearPermissionForUser(const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001078 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001079 Permission permission = Permission::PERMISSION_NONE;
1080 gCtls->netCtrl.setPermissionForUsers(permission, intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001081 return binder::Status::ok();
1082}
1083
1084binder::Status NetdNativeService::NetdNativeService::networkSetProtectAllow(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001085 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001086 std::vector<uid_t> uids = {(uid_t) uid};
1087 gCtls->netCtrl.allowProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001088 return binder::Status::ok();
1089}
1090
1091binder::Status NetdNativeService::networkSetProtectDeny(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001092 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001093 std::vector<uid_t> uids = {(uid_t) uid};
1094 gCtls->netCtrl.denyProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001095 return binder::Status::ok();
1096}
1097
1098binder::Status NetdNativeService::networkCanProtect(int32_t uid, bool* ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001099 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001100 *ret = gCtls->netCtrl.canProtect((uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001101 return binder::Status::ok();
1102}
1103
Chenbo Feng48eaed32018-12-26 17:40:21 -08001104binder::Status NetdNativeService::trafficSetNetPermForUids(int32_t permission,
1105 const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001106 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chenbo Feng48eaed32018-12-26 17:40:21 -08001107 gCtls->trafficCtrl.setPermissionForUids(permission, intsToUids(uids));
Chenbo Feng48eaed32018-12-26 17:40:21 -08001108 return binder::Status::ok();
1109}
1110
Luke Huange64fa382018-07-24 16:38:22 +08001111binder::Status NetdNativeService::firewallSetFirewallType(int32_t firewallType) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001112 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001113 auto type = static_cast<FirewallType>(firewallType);
1114
1115 int res = gCtls->firewallCtrl.setFirewallType(type);
Luke Huange64fa382018-07-24 16:38:22 +08001116 return statusFromErrcode(res);
1117}
1118
1119binder::Status NetdNativeService::firewallSetInterfaceRule(const std::string& ifName,
1120 int32_t firewallRule) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001121 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001122 auto rule = static_cast<FirewallRule>(firewallRule);
1123
1124 int res = gCtls->firewallCtrl.setInterfaceRule(ifName.c_str(), rule);
Luke Huange64fa382018-07-24 16:38:22 +08001125 return statusFromErrcode(res);
1126}
1127
1128binder::Status NetdNativeService::firewallSetUidRule(int32_t childChain, int32_t uid,
1129 int32_t firewallRule) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001130 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001131 auto chain = static_cast<ChildChain>(childChain);
1132 auto rule = static_cast<FirewallRule>(firewallRule);
1133
1134 int res = gCtls->firewallCtrl.setUidRule(chain, uid, rule);
Luke Huange64fa382018-07-24 16:38:22 +08001135 return statusFromErrcode(res);
1136}
1137
1138binder::Status NetdNativeService::firewallEnableChildChain(int32_t childChain, bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001139 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001140 auto chain = static_cast<ChildChain>(childChain);
1141
1142 int res = gCtls->firewallCtrl.enableChildChains(chain, enable);
Luke Huange64fa382018-07-24 16:38:22 +08001143 return statusFromErrcode(res);
1144}
1145
Rubin Xu68654512019-04-11 11:40:09 -07001146binder::Status NetdNativeService::firewallAddUidInterfaceRules(const std::string& ifName,
1147 const std::vector<int32_t>& uids) {
1148 ENFORCE_NETWORK_STACK_PERMISSIONS();
1149
1150 return asBinderStatus(gCtls->trafficCtrl.addUidInterfaceRules(
1151 RouteController::getIfIndex(ifName.c_str()), uids));
1152}
1153
1154binder::Status NetdNativeService::firewallRemoveUidInterfaceRules(
1155 const std::vector<int32_t>& uids) {
1156 ENFORCE_NETWORK_STACK_PERMISSIONS();
1157
1158 return asBinderStatus(gCtls->trafficCtrl.removeUidInterfaceRules(uids));
1159}
1160
Luke Huang19b49c52018-10-22 12:12:05 +09001161binder::Status NetdNativeService::tetherAddForward(const std::string& intIface,
1162 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001163 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang19b49c52018-10-22 12:12:05 +09001164
1165 int res = gCtls->tetherCtrl.enableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001166 return statusFromErrcode(res);
1167}
1168
1169binder::Status NetdNativeService::tetherRemoveForward(const std::string& intIface,
1170 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001171 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangae038f82018-11-05 11:17:31 +09001172 int res = gCtls->tetherCtrl.disableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001173 return statusFromErrcode(res);
1174}
1175
Chenbo Fengf5663d82018-11-08 16:10:48 -08001176binder::Status NetdNativeService::setTcpRWmemorySize(const std::string& rmemValues,
1177 const std::string& wmemValues) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001178 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chenbo Fengf5663d82018-11-08 16:10:48 -08001179 if (!WriteStringToFile(rmemValues, TCP_RMEM_PROC_FILE)) {
1180 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001181 return statusFromErrcode(ret);
1182 }
1183
1184 if (!WriteStringToFile(wmemValues, TCP_WMEM_PROC_FILE)) {
1185 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001186 return statusFromErrcode(ret);
1187 }
Chenbo Fengf5663d82018-11-08 16:10:48 -08001188 return binder::Status::ok();
1189}
1190
Luke Huang528af602018-08-29 19:06:05 +08001191binder::Status NetdNativeService::registerUnsolicitedEventListener(
1192 const android::sp<android::net::INetdUnsolicitedEventListener>& listener) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001193 ENFORCE_NETWORK_STACK_PERMISSIONS();
Bernie Innocenti9ee088d2019-02-01 17:14:32 +09001194 gCtls->eventReporter.registerUnsolEventListener(listener);
Luke Huang528af602018-08-29 19:06:05 +08001195 return binder::Status::ok();
1196}
1197
Luke Huang72a4b7b2019-04-26 09:12:46 -07001198binder::Status NetdNativeService::getOemNetd(android::sp<android::IBinder>* listener) {
1199 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangbc8a80f2019-04-29 18:08:29 -07001200 *listener = com::android::internal::net::OemNetdListener::getListener();
1201
Luke Huang72a4b7b2019-04-26 09:12:46 -07001202 return binder::Status::ok();
1203}
1204
Lorenzo Colittie4d626e2016-02-02 17:19:04 +09001205} // namespace net
1206} // namespace android