blob: 6236e45d7cec1aee78dbc2a4f9358a677451a5fc [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>
Erik Kline38e51f12018-09-06 20:14:44 +090022#include <tuple>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090023#include <vector>
24
Chenbo Fengf5663d82018-11-08 16:10:48 -080025#include <android-base/file.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090026#include <android-base/stringprintf.h>
Ben Schwartz4204ecf2017-10-02 12:35:48 -040027#include <android-base/strings.h>
Robin Lee2cf56172016-09-13 18:55:42 +090028#include <cutils/properties.h>
Logan Chien3f461482018-04-23 14:31:32 +080029#include <log/log.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090030#include <utils/Errors.h>
Pierre Imaibeedec32016-04-13 06:44:51 +090031#include <utils/String16.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090032
33#include <binder/IPCThreadState.h>
34#include <binder/IServiceManager.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090035
Xiao Ma33d562a2018-12-16 16:27:38 +090036#include <json/value.h>
Jeongik Cha374ca5b2019-03-22 12:11:25 +090037#include <json/writer.h>
Ben Schwartze7601812017-04-28 16:38:29 -040038
Lorenzo Colitti89faa342016-02-26 11:38:47 +090039#include "Controllers.h"
Erik Kline55b06f82016-07-04 09:57:18 +090040#include "InterfaceController.h"
Mike Yu5ae61542018-10-19 22:11:43 +080041#include "NetdConstants.h" // SHA256_SIZE
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090042#include "NetdNativeService.h"
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090043#include "NetdPermissions.h"
Luke Huangb670d162018-08-23 20:01:13 +080044#include "Permission.h"
Erik Kline85890042018-05-25 19:19:11 +090045#include "Process.h"
Robin Leeb8087362016-03-30 18:43:08 +010046#include "RouteController.h"
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090047#include "SockDiag.h"
Robin Leeb8087362016-03-30 18:43:08 +010048#include "UidRanges.h"
Xiao Ma33d562a2018-12-16 16:27:38 +090049#include "android/net/BnNetd.h"
Luke Huangb257d612019-03-14 21:19:13 +080050#include "netdutils/DumpWriter.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;
Luke Huangcaebcbb2018-09-27 20:37:14 +080055using android::net::TetherStatsParcel;
Luke Huang94658ac2018-10-18 19:35:12 +090056using android::net::UidRangeParcel;
Luke Huangb257d612019-03-14 21:19:13 +080057using android::netdutils::DumpWriter;
58using android::netdutils::ScopedIndent;
Luke Huange203a152018-11-23 11:47:28 +080059using android::os::ParcelFileDescriptor;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090060
61namespace android {
62namespace net {
63
64namespace {
Erik Klineb31fd692018-06-06 20:50:11 +090065const char OPT_SHORT[] = "--short";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090066
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090067binder::Status checkAnyPermission(const std::vector<const char*>& permissions) {
Luke Huanga38b65c2018-09-26 16:31:03 +080068 pid_t pid = IPCThreadState::self()->getCallingPid();
69 uid_t uid = IPCThreadState::self()->getCallingUid();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090070
Luke Huanga38b65c2018-09-26 16:31:03 +080071 // If the caller is the system UID, don't check permissions.
72 // Otherwise, if the system server's binder thread pool is full, and all the threads are
73 // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492
74 //
75 // From a security perspective, there is currently no difference, because:
76 // 1. The only permissions we check in netd's binder interface are CONNECTIVITY_INTERNAL
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090077 // and NETWORK_STACK, which the system server always has (or MAINLINE_NETWORK_STACK, which
78 // is equivalent to having both CONNECTIVITY_INTERNAL and NETWORK_STACK).
Luke Huanga38b65c2018-09-26 16:31:03 +080079 // 2. AID_SYSTEM always has all permissions. See ActivityManager#checkComponentPermission.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090080 if (uid == AID_SYSTEM) {
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090081 return binder::Status::ok();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090082 }
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090083
84 for (const char* permission : permissions) {
85 if (checkPermission(String16(permission), pid, uid)) {
86 return binder::Status::ok();
87 }
88 }
89
90 auto err = StringPrintf("UID %d / PID %d does not have any of the following permissions: %s",
91 uid, pid, android::base::Join(permissions, ',').c_str());
92 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, err.c_str());
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090093}
94
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +090095#define ENFORCE_ANY_PERMISSION(...) \
96 do { \
97 binder::Status status = checkAnyPermission({__VA_ARGS__}); \
98 if (!status.isOk()) { \
99 return status; \
100 } \
101 } while (0)
Robin Lee2cf56172016-09-13 18:55:42 +0900102
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900103#define NETD_LOCKING_RPC(lock, ... /* permissions */) \
104 ENFORCE_ANY_PERMISSION(__VA_ARGS__); \
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900105 std::lock_guard _lock(lock);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900106
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900107#define NETD_BIG_LOCK_RPC(... /* permissions */) NETD_LOCKING_RPC(gBigNetdLock, __VA_ARGS__)
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900108
Luke Huangf7782042018-08-08 13:13:04 +0800109#define RETURN_BINDER_STATUS_IF_NOT_OK(logEntry, res) \
110 do { \
111 if (!isOk((res))) { \
112 logErrorStatus((logEntry), (res)); \
113 return asBinderStatus((res)); \
114 } \
115 } while (0)
116
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900117#define ENFORCE_INTERNAL_PERMISSIONS() \
118 ENFORCE_ANY_PERMISSION(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK)
119
120#define ENFORCE_NETWORK_STACK_PERMISSIONS() \
121 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK)
122
Luke Huangf7782042018-08-08 13:13:04 +0800123void logErrorStatus(netdutils::LogEntry& logEntry, const netdutils::Status& status) {
124 gLog.log(logEntry.returns(status.code()).withAutomaticDuration());
125}
126
Bernie Innocenti97f388f2018-10-16 19:17:08 +0900127binder::Status asBinderStatus(const netdutils::Status& status) {
128 if (isOk(status)) {
129 return binder::Status::ok();
130 }
131 return binder::Status::fromServiceSpecificError(status.code(), status.msg().c_str());
132}
133
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900134inline binder::Status statusFromErrcode(int ret) {
135 if (ret) {
136 return binder::Status::fromServiceSpecificError(-ret, strerror(-ret));
137 }
138 return binder::Status::ok();
139}
140
Erik Klineb31fd692018-06-06 20:50:11 +0900141bool contains(const Vector<String16>& words, const String16& word) {
142 for (const auto& w : words) {
143 if (w == word) return true;
144 }
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900145
Erik Klineb31fd692018-06-06 20:50:11 +0900146 return false;
147}
148
149} // namespace
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900150
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900151status_t NetdNativeService::start() {
152 IPCThreadState::self()->disableBackgroundScheduling(true);
Erik Klineb31fd692018-06-06 20:50:11 +0900153 const status_t ret = BinderService<NetdNativeService>::publish();
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900154 if (ret != android::OK) {
155 return ret;
156 }
157 sp<ProcessState> ps(ProcessState::self());
158 ps->startThreadPool();
159 ps->giveThreadPoolName();
Xiao Ma33d562a2018-12-16 16:27:38 +0900160
161 // register log callback to BnNetd::logFunc
162 BnNetd::logFunc = [](const Json::Value& logTransaction) {
163 bool hasReturnArgs;
164 std::string output;
165 const Json::Value& inputArgs = logTransaction["input_args"];
166 const Json::Value& returnArgs = logTransaction["_aidl_return"];
Xiao Ma33d562a2018-12-16 16:27:38 +0900167
168 hasReturnArgs = !returnArgs.empty();
169 output.append(logTransaction["method_name"].asString().c_str() + std::string("("));
170
171 // input args
Jeongik Cha374ca5b2019-03-22 12:11:25 +0900172 Json::FastWriter fastWriter;
173 fastWriter.omitEndingLineFeed();
Jeongik Chab7775e52019-04-02 12:11:36 +0900174 for (Json::Value::ArrayIndex i = 0; i != inputArgs.size(); i++) {
175 std::string value = fastWriter.write(inputArgs[i]["value"]);
Xiao Ma33d562a2018-12-16 16:27:38 +0900176 if (value.empty()) value = std::string("null");
177 output.append(value);
Jeongik Chab7775e52019-04-02 12:11:36 +0900178 if (i != inputArgs.size() - 1) {
Xiao Ma33d562a2018-12-16 16:27:38 +0900179 output.append(", ");
180 }
181 }
182 output.append(std::string(")"));
183 // return args
184 if (hasReturnArgs) {
Jeongik Cha374ca5b2019-03-22 12:11:25 +0900185 output.append(StringPrintf(" -> (%s)", fastWriter.write(returnArgs).c_str()));
Xiao Ma33d562a2018-12-16 16:27:38 +0900186 }
187 // duration time
188 output.append(StringPrintf(" <%sms>", logTransaction["duration_ms"].asString().c_str()));
189 gLog.info("%s", output.c_str());
190 };
191
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900192 return android::OK;
193}
194
Hugo Benichi7b314e12018-01-15 21:54:00 +0900195status_t NetdNativeService::dump(int fd, const Vector<String16> &args) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900196 const binder::Status dump_permission = checkAnyPermission({PERM_DUMP});
Erik Kline2d3a1632016-03-15 16:33:48 +0900197 if (!dump_permission.isOk()) {
198 const String8 msg(dump_permission.toString8());
199 write(fd, msg.string(), msg.size());
200 return PERMISSION_DENIED;
201 }
202
203 // This method does not grab any locks. If individual classes need locking
204 // their dump() methods MUST handle locking appropriately.
Hugo Benichi7b314e12018-01-15 21:54:00 +0900205
Erik Kline2d3a1632016-03-15 16:33:48 +0900206 DumpWriter dw(fd);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900207
208 if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) {
209 dw.blankline();
210 gCtls->tcpSocketMonitor.dump(dw);
211 dw.blankline();
212 return NO_ERROR;
213 }
214
Chenbo Fengef297172018-03-26 10:53:33 -0700215 if (!args.isEmpty() && args[0] == TrafficController::DUMP_KEYWORD) {
216 dw.blankline();
217 gCtls->trafficCtrl.dump(dw, true);
218 dw.blankline();
219 return NO_ERROR;
220 }
221
Erik Kline85890042018-05-25 19:19:11 +0900222 process::dump(dw);
Erik Kline2d3a1632016-03-15 16:33:48 +0900223 dw.blankline();
224 gCtls->netCtrl.dump(dw);
225 dw.blankline();
226
Chenbo Fengef297172018-03-26 10:53:33 -0700227 gCtls->trafficCtrl.dump(dw, false);
228 dw.blankline();
229
Benedict Wongaf855432018-05-10 17:07:37 -0700230 gCtls->xfrmCtrl.dump(dw);
231 dw.blankline();
232
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700233 gCtls->clatdCtrl.dump(dw);
234 dw.blankline();
235
Erik Klineb31fd692018-06-06 20:50:11 +0900236 {
237 ScopedIndent indentLog(dw);
238 if (contains(args, String16(OPT_SHORT))) {
239 dw.println("Log: <omitted>");
240 } else {
241 dw.println("Log:");
242 ScopedIndent indentLogEntries(dw);
243 gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); });
244 }
245 dw.blankline();
246 }
247
Luke Huang528af602018-08-29 19:06:05 +0800248 {
249 ScopedIndent indentLog(dw);
250 if (contains(args, String16(OPT_SHORT))) {
251 dw.println("UnsolicitedLog: <omitted>");
252 } else {
253 dw.println("UnsolicitedLog:");
254 ScopedIndent indentLogEntries(dw);
255 gUnsolicitedLog.forEachEntry(
256 [&dw](const std::string& entry) mutable { dw.println(entry); });
257 }
258 dw.blankline();
259 }
260
Erik Kline2d3a1632016-03-15 16:33:48 +0900261 return NO_ERROR;
262}
263
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900264binder::Status NetdNativeService::isAlive(bool *alive) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900265 NETD_BIG_LOCK_RPC(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900266
267 *alive = true;
Erik Klineb31fd692018-06-06 20:50:11 +0900268
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900269 return binder::Status::ok();
270}
271
Erik Klinef52d4522018-03-14 15:01:46 +0900272binder::Status NetdNativeService::firewallReplaceUidChain(const std::string& chainName,
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900273 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900274 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_CONNECTIVITY_INTERNAL,
275 PERM_MAINLINE_NETWORK_STACK);
Erik Klinef52d4522018-03-14 15:01:46 +0900276 int err = gCtls->firewallCtrl.replaceUidChain(chainName, isWhitelist, uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900277 *ret = (err == 0);
278 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900279}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900280
281binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900282 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_CONNECTIVITY_INTERNAL,
283 PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900284 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
285 *ret = (err == 0);
286 return binder::Status::ok();
287}
288
Luke Huang531f5d32018-08-03 15:19:05 +0800289binder::Status NetdNativeService::bandwidthSetInterfaceQuota(const std::string& ifName,
290 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900291 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800292 int res = gCtls->bandwidthCtrl.setInterfaceQuota(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800293 return statusFromErrcode(res);
294}
295
296binder::Status NetdNativeService::bandwidthRemoveInterfaceQuota(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900297 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800298 int res = gCtls->bandwidthCtrl.removeInterfaceQuota(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800299 return statusFromErrcode(res);
300}
301
302binder::Status NetdNativeService::bandwidthSetInterfaceAlert(const std::string& ifName,
303 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900304 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800305 int res = gCtls->bandwidthCtrl.setInterfaceAlert(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800306 return statusFromErrcode(res);
307}
308
309binder::Status NetdNativeService::bandwidthRemoveInterfaceAlert(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900310 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800311 int res = gCtls->bandwidthCtrl.removeInterfaceAlert(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800312 return statusFromErrcode(res);
313}
314
315binder::Status NetdNativeService::bandwidthSetGlobalAlert(int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900316 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800317 int res = gCtls->bandwidthCtrl.setGlobalAlert(bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800318 return statusFromErrcode(res);
319}
320
321binder::Status NetdNativeService::bandwidthAddNaughtyApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900322 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800323 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
324 int res = gCtls->bandwidthCtrl.addNaughtyApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800325 return statusFromErrcode(res);
326}
327
328binder::Status NetdNativeService::bandwidthRemoveNaughtyApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900329 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800330 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
331 int res = gCtls->bandwidthCtrl.removeNaughtyApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800332 return statusFromErrcode(res);
333}
334
335binder::Status NetdNativeService::bandwidthAddNiceApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900336 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800337 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
338 int res = gCtls->bandwidthCtrl.addNiceApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800339 return statusFromErrcode(res);
340}
341
342binder::Status NetdNativeService::bandwidthRemoveNiceApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900343 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800344 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
345 int res = gCtls->bandwidthCtrl.removeNiceApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800346 return statusFromErrcode(res);
347}
348
Luke Huangb670d162018-08-23 20:01:13 +0800349binder::Status NetdNativeService::networkCreatePhysical(int32_t netId, int32_t permission) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900350 ENFORCE_INTERNAL_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +0800351 int ret = gCtls->netCtrl.createPhysicalNetwork(netId, convertPermission(permission));
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900352 return statusFromErrcode(ret);
353}
354
cken67cd14c2018-12-05 17:26:59 +0900355binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool secure) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900356 ENFORCE_NETWORK_STACK_PERMISSIONS();
cken67cd14c2018-12-05 17:26:59 +0900357 int ret = gCtls->netCtrl.createVirtualNetwork(netId, secure);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900358 return statusFromErrcode(ret);
359}
360
361binder::Status NetdNativeService::networkDestroy(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900362 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangd33a8f42019-03-14 16:10:04 +0800363 // NetworkController::destroyNetwork is thread-safe.
Mike Yu4fe1b9c2019-01-29 17:50:19 +0800364 const int ret = gCtls->netCtrl.destroyNetwork(netId);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900365 return statusFromErrcode(ret);
366}
367
368binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900369 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900370 int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
371 return statusFromErrcode(ret);
372}
373
374binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900375 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900376 int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
377 return statusFromErrcode(ret);
378}
379
Luke Huang94658ac2018-10-18 19:35:12 +0900380binder::Status NetdNativeService::networkAddUidRanges(
381 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900382 // NetworkController::addUsersToNetwork is thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900383 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900384 int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray));
385 return statusFromErrcode(ret);
386}
387
Luke Huang94658ac2018-10-18 19:35:12 +0900388binder::Status NetdNativeService::networkRemoveUidRanges(
389 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900390 // NetworkController::removeUsersFromNetwork is thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900391 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900392 int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray));
393 return statusFromErrcode(ret);
394}
395
Luke Huang94658ac2018-10-18 19:35:12 +0900396binder::Status NetdNativeService::networkRejectNonSecureVpn(
397 bool add, const std::vector<UidRangeParcel>& uidRangeArray) {
Robin Leeb8087362016-03-30 18:43:08 +0100398 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
399 // it should be possible to use the same lock as NetworkController. However, every call through
400 // the CommandListener "network" command will need to hold this lock too, not just the ones that
401 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
402 // look at routes, but it's not enough here).
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900403 NETD_BIG_LOCK_RPC(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900404 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100405
406 int err;
407 if (add) {
408 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
409 } else {
410 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
411 }
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900412 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100413}
414
Luke Huang94658ac2018-10-18 19:35:12 +0900415binder::Status NetdNativeService::socketDestroy(const std::vector<UidRangeParcel>& uids,
416 const std::vector<int32_t>& skipUids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900417 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900418
419 SockDiag sd;
420 if (!sd.open()) {
421 return binder::Status::fromServiceSpecificError(EIO,
422 String8("Could not open SOCK_DIAG socket"));
423 }
424
425 UidRanges uidRanges(uids);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +0900426 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
427 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900428 if (err) {
429 return binder::Status::fromServiceSpecificError(-err,
430 String8::format("destroySockets: %s", strerror(-err)));
431 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900432 return binder::Status::ok();
433}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900434
Erik Klinef48e4dd2016-07-18 04:02:07 +0900435binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900436 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Erik Klinef48e4dd2016-07-18 04:02:07 +0900437 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
438 return binder::Status::ok();
439}
440
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900441namespace {
442
Luke Huangcaebcbb2018-09-27 20:37:14 +0800443void tetherAddStatsByInterface(TetherController::TetherStats* tetherStatsParcel,
444 const TetherController::TetherStats& tetherStats) {
445 if (tetherStatsParcel->extIface == tetherStats.extIface) {
446 tetherStatsParcel->rxBytes += tetherStats.rxBytes;
447 tetherStatsParcel->rxPackets += tetherStats.rxPackets;
448 tetherStatsParcel->txBytes += tetherStats.txBytes;
449 tetherStatsParcel->txPackets += tetherStats.txPackets;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900450 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800451}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900452
Luke Huangcaebcbb2018-09-27 20:37:14 +0800453TetherStatsParcel toTetherStatsParcel(const TetherController::TetherStats& stats) {
454 TetherStatsParcel result;
455 result.iface = stats.extIface;
456 result.rxBytes = stats.rxBytes;
457 result.rxPackets = stats.rxPackets;
458 result.txBytes = stats.txBytes;
459 result.txPackets = stats.txPackets;
460 return result;
461}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900462
Luke Huangcaebcbb2018-09-27 20:37:14 +0800463void setTetherStatsParcelVecByInterface(std::vector<TetherStatsParcel>* tetherStatsVec,
464 const TetherController::TetherStatsList& statsList) {
465 std::map<std::string, TetherController::TetherStats> statsMap;
466 for (const auto& stats : statsList) {
467 auto iter = statsMap.find(stats.extIface);
468 if (iter != statsMap.end()) {
469 tetherAddStatsByInterface(&(iter->second), stats);
470 } else {
471 statsMap.insert(
472 std::pair<std::string, TetherController::TetherStats>(stats.extIface, stats));
473 }
474 }
475 for (auto iter = statsMap.begin(); iter != statsMap.end(); iter++) {
476 tetherStatsVec->push_back(toTetherStatsParcel(iter->second));
477 }
478}
479
480std::vector<std::string> tetherStatsParcelVecToStringVec(std::vector<TetherStatsParcel>* tVec) {
481 std::vector<std::string> result;
482 for (const auto& t : *tVec) {
483 result.push_back(StringPrintf("%s:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64,
484 t.iface.c_str(), t.rxBytes, t.rxPackets, t.txBytes,
485 t.txPackets));
486 }
487 return result;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900488}
489
490} // namespace
491
Luke Huangcaebcbb2018-09-27 20:37:14 +0800492binder::Status NetdNativeService::tetherGetStats(
493 std::vector<TetherStatsParcel>* tetherStatsParcelVec) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900494 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900495 const auto& statsList = gCtls->tetherCtrl.getTetherStats();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900496 if (!isOk(statsList)) {
Nathan Harold28ccfef2018-03-16 19:02:47 -0700497 return asBinderStatus(statsList);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900498 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800499 setTetherStatsParcelVecByInterface(tetherStatsParcelVec, statsList.value());
500 auto statsResults = tetherStatsParcelVecToStringVec(tetherStatsParcelVec);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900501 return binder::Status::ok();
502}
503
Erik Kline53c20882016-08-02 15:22:53 +0900504binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
505 const std::string &addrString, int prefixLength) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900506 ENFORCE_INTERNAL_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900507 const int err = InterfaceController::addAddress(
508 ifName.c_str(), addrString.c_str(), prefixLength);
509 if (err != 0) {
510 return binder::Status::fromServiceSpecificError(-err,
511 String8::format("InterfaceController error: %s", strerror(-err)));
512 }
513 return binder::Status::ok();
514}
515
516binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
517 const std::string &addrString, int prefixLength) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900518 ENFORCE_INTERNAL_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900519 const int err = InterfaceController::delAddress(
520 ifName.c_str(), addrString.c_str(), prefixLength);
521 if (err != 0) {
522 return binder::Status::fromServiceSpecificError(-err,
523 String8::format("InterfaceController error: %s", strerror(-err)));
524 }
525 return binder::Status::ok();
526}
527
Erik Kline38e51f12018-09-06 20:14:44 +0900528namespace {
Erik Kline55b06f82016-07-04 09:57:18 +0900529
Erik Kline38e51f12018-09-06 20:14:44 +0900530std::tuple<binder::Status, const char*, const char*> getPathComponents(int32_t ipversion,
531 int32_t category) {
532 const char* ipversionStr = nullptr;
533 switch (ipversion) {
Erik Kline55b06f82016-07-04 09:57:18 +0900534 case INetd::IPV4:
Erik Kline38e51f12018-09-06 20:14:44 +0900535 ipversionStr = "ipv4";
Erik Kline55b06f82016-07-04 09:57:18 +0900536 break;
537 case INetd::IPV6:
Erik Kline38e51f12018-09-06 20:14:44 +0900538 ipversionStr = "ipv6";
Erik Kline55b06f82016-07-04 09:57:18 +0900539 break;
540 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900541 return {binder::Status::fromServiceSpecificError(EAFNOSUPPORT, "Bad IP version"),
542 nullptr, nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900543 }
544
Erik Kline38e51f12018-09-06 20:14:44 +0900545 const char* whichStr = nullptr;
546 switch (category) {
Erik Kline55b06f82016-07-04 09:57:18 +0900547 case INetd::CONF:
548 whichStr = "conf";
549 break;
550 case INetd::NEIGH:
551 whichStr = "neigh";
552 break;
553 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900554 return {binder::Status::fromServiceSpecificError(EINVAL, "Bad category"), nullptr,
555 nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900556 }
557
Erik Kline38e51f12018-09-06 20:14:44 +0900558 return {binder::Status::ok(), ipversionStr, whichStr};
559}
560
561} // namespace
562
563binder::Status NetdNativeService::getProcSysNet(int32_t ipversion, int32_t which,
564 const std::string& ifname,
565 const std::string& parameter, std::string* value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900566 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900567 const auto pathParts = getPathComponents(ipversion, which);
568 const auto& pathStatus = std::get<0>(pathParts);
569 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900570 return pathStatus;
Erik Kline55b06f82016-07-04 09:57:18 +0900571 }
Erik Kline38e51f12018-09-06 20:14:44 +0900572
573 const int err = InterfaceController::getParameter(std::get<1>(pathParts),
574 std::get<2>(pathParts), ifname.c_str(),
575 parameter.c_str(), value);
Erik Kline38e51f12018-09-06 20:14:44 +0900576 return statusFromErrcode(err);
577}
578
579binder::Status NetdNativeService::setProcSysNet(int32_t ipversion, int32_t which,
580 const std::string& ifname,
581 const std::string& parameter,
582 const std::string& value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900583 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900584 const auto pathParts = getPathComponents(ipversion, which);
585 const auto& pathStatus = std::get<0>(pathParts);
586 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900587 return pathStatus;
588 }
589
590 const int err = InterfaceController::setParameter(std::get<1>(pathParts),
591 std::get<2>(pathParts), ifname.c_str(),
592 parameter.c_str(), value.c_str());
Erik Kline38e51f12018-09-06 20:14:44 +0900593 return statusFromErrcode(err);
Erik Kline55b06f82016-07-04 09:57:18 +0900594}
595
Luke Huange203a152018-11-23 11:47:28 +0800596binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const ParcelFileDescriptor& socket,
597 int newUid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900598 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900599 gLog.log("ipSecSetEncapSocketOwner()");
Benedict Wongb2daefb2017-12-06 22:05:46 -0800600
601 uid_t callerUid = IPCThreadState::self()->getCallingUid();
Luke Huange203a152018-11-23 11:47:28 +0800602 return asBinderStatus(
603 gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket.get(), newUid, callerUid));
Benedict Wongb2daefb2017-12-06 22:05:46 -0800604}
605
Nathan Harold1a371532017-01-30 12:30:48 -0800606binder::Status NetdNativeService::ipSecAllocateSpi(
607 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800608 const std::string& sourceAddress,
609 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800610 int32_t inSpi,
611 int32_t* outSpi) {
612 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900613 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900614 gLog.log("ipSecAllocateSpi()");
ludi6e8eccd2017-08-14 14:40:37 -0700615 return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi(
Nathan Harold1a371532017-01-30 12:30:48 -0800616 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800617 sourceAddress,
618 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800619 inSpi,
620 outSpi));
621}
622
623binder::Status NetdNativeService::ipSecAddSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700624 int32_t transformId, int32_t mode, const std::string& sourceAddress,
625 const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi,
626 int32_t markValue, int32_t markMask, const std::string& authAlgo,
627 const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo,
628 const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo,
629 const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType,
630 int32_t encapLocalPort, int32_t encapRemotePort, int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800631 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900632 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900633 gLog.log("ipSecAddSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700634 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation(
Benedict Wongad600cb2018-05-14 17:22:35 -0700635 transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue,
636 markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits,
Benedict Wonga450e722018-05-07 10:29:02 -0700637 aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort,
638 interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800639}
640
641binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700642 int32_t transformId, const std::string& sourceAddress,
643 const std::string& destinationAddress, int32_t spi, int32_t markValue, int32_t markMask,
644 int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800645 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900646 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900647 gLog.log("ipSecDeleteSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700648 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700649 transformId, sourceAddress, destinationAddress, spi, markValue, markMask, interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800650}
651
652binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800653 const ParcelFileDescriptor& socket, int32_t transformId, int32_t direction,
654 const std::string& sourceAddress, const std::string& destinationAddress, int32_t spi) {
Nathan Harold1a371532017-01-30 12:30:48 -0800655 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900656 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900657 gLog.log("ipSecApplyTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700658 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800659 socket.get(), transformId, direction, sourceAddress, destinationAddress, spi));
Nathan Harold1a371532017-01-30 12:30:48 -0800660}
661
662binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800663 const ParcelFileDescriptor& socket) {
Nathan Harold1a371532017-01-30 12:30:48 -0800664 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900665 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900666 gLog.log("ipSecRemoveTransportModeTransform()");
Luke Huange203a152018-11-23 11:47:28 +0800667 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(socket.get()));
Nathan Harold1a371532017-01-30 12:30:48 -0800668}
669
Benedict Wonga04ffa72018-05-09 21:42:42 -0700670binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
671 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700672 const std::string& tmplSrcAddress,
673 const std::string& tmplDstAddress,
674 int32_t spi, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700675 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800676 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900677 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900678 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800679 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700680 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700681 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800682}
683
Benedict Wonga450e722018-05-07 10:29:02 -0700684binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(
685 int32_t transformId, int32_t selAddrFamily, int32_t direction,
686 const std::string& tmplSrcAddress, const std::string& tmplDstAddress, int32_t spi,
687 int32_t markValue, int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800688 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900689 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900690 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800691 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700692 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700693 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800694}
695
Benedict Wonga04ffa72018-05-09 21:42:42 -0700696binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId,
697 int32_t selAddrFamily,
698 int32_t direction, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700699 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800700 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900701 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900702 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800703 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
Benedict Wonga450e722018-05-07 10:29:02 -0700704 transformId, selAddrFamily, direction, markValue, markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800705}
706
Benedict Wong319f17e2018-05-15 17:06:44 -0700707binder::Status NetdNativeService::ipSecAddTunnelInterface(const std::string& deviceName,
708 const std::string& localAddress,
709 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700710 int32_t iKey, int32_t oKey,
711 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800712 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900713 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700714 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700715 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, false);
Benedict Wong319f17e2018-05-15 17:06:44 -0700716 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800717}
718
Benedict Wong319f17e2018-05-15 17:06:44 -0700719binder::Status NetdNativeService::ipSecUpdateTunnelInterface(const std::string& deviceName,
720 const std::string& localAddress,
721 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700722 int32_t iKey, int32_t oKey,
723 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800724 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900725 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700726 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700727 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, true);
Benedict Wong319f17e2018-05-15 17:06:44 -0700728 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800729}
730
Benedict Wong319f17e2018-05-15 17:06:44 -0700731binder::Status NetdNativeService::ipSecRemoveTunnelInterface(const std::string& deviceName) {
manojboopathi8707f232018-01-02 14:45:47 -0800732 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900733 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700734 netdutils::Status result = gCtls->xfrmCtrl.ipSecRemoveTunnelInterface(deviceName);
Benedict Wong319f17e2018-05-15 17:06:44 -0700735 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800736}
737
Joel Scherpelzde937962017-06-01 13:20:21 +0900738binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
739 int32_t mode) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900740 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700741 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900742}
743
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900744binder::Status NetdNativeService::wakeupAddInterface(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.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900749}
750
751binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
752 const std::string& prefix, int32_t mark,
753 int32_t mask) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900754 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700755 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900756}
757
Luke Huang0051a622018-07-23 20:30:16 +0800758binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout,
759 const std::string& classLabel) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900760 NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang0051a622018-07-23 20:30:16 +0800761 int res =
762 gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str());
Luke Huang0051a622018-07-23 20:30:16 +0800763 return statusFromErrcode(res);
764}
765
766binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName,
767 int32_t timeout,
768 const std::string& classLabel) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900769 NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang0051a622018-07-23 20:30:16 +0800770 int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout,
771 classLabel.c_str());
Luke Huang0051a622018-07-23 20:30:16 +0800772 return statusFromErrcode(res);
773}
Luke Huanga67dd562018-07-17 19:58:25 +0800774
775binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900776 NETD_LOCKING_RPC(gCtls->strictCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huanga67dd562018-07-17 19:58:25 +0800777 StrictPenalty penalty;
778 switch (policyPenalty) {
779 case INetd::PENALTY_POLICY_REJECT:
780 penalty = REJECT;
781 break;
782 case INetd::PENALTY_POLICY_LOG:
783 penalty = LOG;
784 break;
785 case INetd::PENALTY_POLICY_ACCEPT:
786 penalty = ACCEPT;
787 break;
788 default:
789 return statusFromErrcode(-EINVAL);
790 break;
791 }
792 int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty);
Luke Huanga67dd562018-07-17 19:58:25 +0800793 return statusFromErrcode(res);
794}
Luke Huange64fa382018-07-24 16:38:22 +0800795
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900796binder::Status NetdNativeService::clatdStart(const std::string& ifName,
797 const std::string& nat64Prefix, std::string* v6Addr) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900798 NETD_LOCKING_RPC(gCtls->clatdCtrl.mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900799 int res = gCtls->clatdCtrl.startClatd(ifName.c_str(), nat64Prefix, v6Addr);
Luke Huang6d301232018-08-01 14:05:18 +0800800 return statusFromErrcode(res);
801}
802
803binder::Status NetdNativeService::clatdStop(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900804 NETD_LOCKING_RPC(gCtls->clatdCtrl.mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang6d301232018-08-01 14:05:18 +0800805 int res = gCtls->clatdCtrl.stopClatd(ifName.c_str());
Luke Huang6d301232018-08-01 14:05:18 +0800806 return statusFromErrcode(res);
807}
Luke Huanga67dd562018-07-17 19:58:25 +0800808
Luke Huang457d4702018-08-16 15:39:15 +0800809binder::Status NetdNativeService::ipfwdEnabled(bool* status) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900810 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang728cf4c2019-03-14 19:43:02 +0800811 *status = (gCtls->tetherCtrl.getIpfwdRequesterList().size() > 0) ? true : false;
812 return binder::Status::ok();
813}
814
815binder::Status NetdNativeService::ipfwdGetRequesterList(std::vector<std::string>* requesterList) {
816 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
817 for (const auto& requester : gCtls->tetherCtrl.getIpfwdRequesterList()) {
818 requesterList->push_back(requester);
819 }
Luke Huang457d4702018-08-16 15:39:15 +0800820 return binder::Status::ok();
821}
822
823binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900824 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang457d4702018-08-16 15:39:15 +0800825 int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
Luke Huang457d4702018-08-16 15:39:15 +0800826 return statusFromErrcode(res);
827}
828
829binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900830 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang457d4702018-08-16 15:39:15 +0800831 int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
Luke Huang457d4702018-08-16 15:39:15 +0800832 return statusFromErrcode(res);
833}
834
835binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface,
836 const std::string& toIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900837 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang457d4702018-08-16 15:39:15 +0800838 int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str());
Luke Huang457d4702018-08-16 15:39:15 +0800839 return statusFromErrcode(res);
840}
841
842binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface,
843 const std::string& toIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900844 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang457d4702018-08-16 15:39:15 +0800845 int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str());
Luke Huang457d4702018-08-16 15:39:15 +0800846 return statusFromErrcode(res);
847}
848
Luke Huangf7782042018-08-08 13:13:04 +0800849namespace {
850std::string addSquareBrackets(const std::string& s) {
851 return "[" + s + "]";
852}
853
854std::string addCurlyBrackets(const std::string& s) {
855 return "{" + s + "}";
856}
857
858} // namespace
Xiao Ma33d562a2018-12-16 16:27:38 +0900859
Luke Huangf7782042018-08-08 13:13:04 +0800860binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900861 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800862 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
863
864 const auto& ifaceList = InterfaceController::getIfaceNames();
865 RETURN_BINDER_STATUS_IF_NOT_OK(entry, ifaceList);
866
867 interfaceListResult->clear();
868 interfaceListResult->reserve(ifaceList.value().size());
869 interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()),
870 end(ifaceList.value()));
871
872 gLog.log(entry.returns(addSquareBrackets(base::Join(*interfaceListResult, ", ")))
873 .withAutomaticDuration());
874 return binder::Status::ok();
875}
876
877std::string interfaceConfigurationParcelToString(const InterfaceConfigurationParcel& cfg) {
878 std::vector<std::string> result{cfg.ifName, cfg.hwAddr, cfg.ipv4Addr,
879 std::to_string(cfg.prefixLength)};
880 result.insert(end(result), begin(cfg.flags), end(cfg.flags));
881 return addCurlyBrackets(base::Join(result, ", "));
882}
883
884binder::Status NetdNativeService::interfaceGetCfg(
885 const std::string& ifName, InterfaceConfigurationParcel* interfaceGetCfgResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900886 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800887 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
888
889 const auto& cfgRes = InterfaceController::getCfg(ifName);
890 RETURN_BINDER_STATUS_IF_NOT_OK(entry, cfgRes);
891
892 *interfaceGetCfgResult = cfgRes.value();
893 gLog.log(entry.returns(interfaceConfigurationParcelToString(*interfaceGetCfgResult))
894 .withAutomaticDuration());
895 return binder::Status::ok();
896}
897
898binder::Status NetdNativeService::interfaceSetCfg(const InterfaceConfigurationParcel& cfg) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900899 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800900 auto entry = gLog.newEntry()
901 .prettyFunction(__PRETTY_FUNCTION__)
902 .arg(interfaceConfigurationParcelToString(cfg));
903
904 const auto& res = InterfaceController::setCfg(cfg);
905 RETURN_BINDER_STATUS_IF_NOT_OK(entry, res);
906
907 gLog.log(entry.withAutomaticDuration());
908 return binder::Status::ok();
909}
910
911binder::Status NetdNativeService::interfaceSetIPv6PrivacyExtensions(const std::string& ifName,
912 bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900913 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800914 int res = InterfaceController::setIPv6PrivacyExtensions(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800915 return statusFromErrcode(res);
916}
917
918binder::Status NetdNativeService::interfaceClearAddrs(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900919 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800920 int res = InterfaceController::clearAddrs(ifName.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800921 return statusFromErrcode(res);
922}
923
924binder::Status NetdNativeService::interfaceSetEnableIPv6(const std::string& ifName, bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900925 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800926 int res = InterfaceController::setEnableIPv6(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800927 return statusFromErrcode(res);
928}
929
930binder::Status NetdNativeService::interfaceSetMtu(const std::string& ifName, int32_t mtuValue) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900931 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800932 std::string mtu = std::to_string(mtuValue);
933 int res = InterfaceController::setMtu(ifName.c_str(), mtu.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800934 return statusFromErrcode(res);
935}
936
Luke Huangb5733d72018-08-21 17:17:19 +0800937binder::Status NetdNativeService::tetherStart(const std::vector<std::string>& dhcpRanges) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900938 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800939 if (dhcpRanges.size() % 2 == 1) {
940 return statusFromErrcode(-EINVAL);
941 }
942 int res = gCtls->tetherCtrl.startTethering(dhcpRanges);
Luke Huangb5733d72018-08-21 17:17:19 +0800943 return statusFromErrcode(res);
944}
945
946binder::Status NetdNativeService::tetherStop() {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900947 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800948 int res = gCtls->tetherCtrl.stopTethering();
Luke Huangb5733d72018-08-21 17:17:19 +0800949 return statusFromErrcode(res);
950}
951
952binder::Status NetdNativeService::tetherIsEnabled(bool* enabled) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900953 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800954 *enabled = gCtls->tetherCtrl.isTetheringStarted();
Luke Huangb5733d72018-08-21 17:17:19 +0800955 return binder::Status::ok();
956}
957
958binder::Status NetdNativeService::tetherInterfaceAdd(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900959 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800960 int res = gCtls->tetherCtrl.tetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800961 return statusFromErrcode(res);
962}
963
964binder::Status NetdNativeService::tetherInterfaceRemove(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900965 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800966 int res = gCtls->tetherCtrl.untetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800967 return statusFromErrcode(res);
968}
969
970binder::Status NetdNativeService::tetherInterfaceList(std::vector<std::string>* ifList) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900971 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800972 for (const auto& ifname : gCtls->tetherCtrl.getTetheredInterfaceList()) {
973 ifList->push_back(ifname);
974 }
Luke Huangb5733d72018-08-21 17:17:19 +0800975 return binder::Status::ok();
976}
977
978binder::Status NetdNativeService::tetherDnsSet(int32_t netId,
979 const std::vector<std::string>& dnsAddrs) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900980 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800981 int res = gCtls->tetherCtrl.setDnsForwarders(netId, dnsAddrs);
Luke Huangb5733d72018-08-21 17:17:19 +0800982 return statusFromErrcode(res);
983}
984
985binder::Status NetdNativeService::tetherDnsList(std::vector<std::string>* dnsList) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900986 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800987 for (const auto& fwdr : gCtls->tetherCtrl.getDnsForwarders()) {
988 dnsList->push_back(fwdr);
989 }
Luke Huangb5733d72018-08-21 17:17:19 +0800990 return binder::Status::ok();
991}
992
Luke Huangb670d162018-08-23 20:01:13 +0800993binder::Status NetdNativeService::networkAddRoute(int32_t netId, const std::string& ifName,
994 const std::string& destination,
995 const std::string& nextHop) {
996 // Public methods of NetworkController are thread-safe.
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.addRoute(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::networkRemoveRoute(int32_t netId, const std::string& ifName,
1006 const std::string& destination,
1007 const std::string& nextHop) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001008 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001009 bool legacy = false;
1010 uid_t uid = 0; // UID is only meaningful for legacy routes.
1011 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1012 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
Luke Huangb670d162018-08-23 20:01:13 +08001013 return statusFromErrcode(res);
1014}
1015
1016binder::Status NetdNativeService::networkAddLegacyRoute(int32_t netId, const std::string& ifName,
1017 const std::string& destination,
1018 const std::string& nextHop, int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001019 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001020 bool legacy = true;
1021 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1022 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1023 (uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001024 return statusFromErrcode(res);
1025}
1026
1027binder::Status NetdNativeService::networkRemoveLegacyRoute(int32_t netId, const std::string& ifName,
1028 const std::string& destination,
1029 const std::string& nextHop,
1030 int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001031 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001032 bool legacy = true;
1033 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1034 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1035 (uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001036 return statusFromErrcode(res);
1037}
1038
1039binder::Status NetdNativeService::networkGetDefault(int32_t* netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001040 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001041 *netId = gCtls->netCtrl.getDefaultNetwork();
Luke Huangb670d162018-08-23 20:01:13 +08001042 return binder::Status::ok();
1043}
1044
1045binder::Status NetdNativeService::networkSetDefault(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001046 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001047 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001048 return statusFromErrcode(res);
1049}
1050
1051binder::Status NetdNativeService::networkClearDefault() {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001052 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001053 unsigned netId = NETID_UNSET;
1054 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001055 return statusFromErrcode(res);
1056}
1057
1058std::vector<uid_t> NetdNativeService::intsToUids(const std::vector<int32_t>& intUids) {
1059 return {begin(intUids), end(intUids)};
1060}
1061
1062Permission NetdNativeService::convertPermission(int32_t permission) {
1063 switch (permission) {
1064 case INetd::PERMISSION_NETWORK:
1065 return Permission::PERMISSION_NETWORK;
1066 case INetd::PERMISSION_SYSTEM:
1067 return Permission::PERMISSION_SYSTEM;
1068 default:
1069 return Permission::PERMISSION_NONE;
1070 }
1071}
1072
1073binder::Status NetdNativeService::networkSetPermissionForNetwork(int32_t netId,
1074 int32_t permission) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001075 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001076 std::vector<unsigned> netIds = {(unsigned) netId};
1077 int res = gCtls->netCtrl.setPermissionForNetworks(convertPermission(permission), netIds);
Luke Huangb670d162018-08-23 20:01:13 +08001078 return statusFromErrcode(res);
1079}
1080
1081binder::Status NetdNativeService::networkSetPermissionForUser(int32_t permission,
1082 const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001083 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001084 gCtls->netCtrl.setPermissionForUsers(convertPermission(permission), intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001085 return binder::Status::ok();
1086}
1087
1088binder::Status NetdNativeService::networkClearPermissionForUser(const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001089 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001090 Permission permission = Permission::PERMISSION_NONE;
1091 gCtls->netCtrl.setPermissionForUsers(permission, intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001092 return binder::Status::ok();
1093}
1094
1095binder::Status NetdNativeService::NetdNativeService::networkSetProtectAllow(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001096 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001097 std::vector<uid_t> uids = {(uid_t) uid};
1098 gCtls->netCtrl.allowProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001099 return binder::Status::ok();
1100}
1101
1102binder::Status NetdNativeService::networkSetProtectDeny(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001103 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001104 std::vector<uid_t> uids = {(uid_t) uid};
1105 gCtls->netCtrl.denyProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001106 return binder::Status::ok();
1107}
1108
1109binder::Status NetdNativeService::networkCanProtect(int32_t uid, bool* ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001110 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001111 *ret = gCtls->netCtrl.canProtect((uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001112 return binder::Status::ok();
1113}
1114
Chenbo Feng48eaed32018-12-26 17:40:21 -08001115binder::Status NetdNativeService::trafficSetNetPermForUids(int32_t permission,
1116 const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001117 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chenbo Feng48eaed32018-12-26 17:40:21 -08001118 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(permission).arg(uids);
1119 gCtls->trafficCtrl.setPermissionForUids(permission, intsToUids(uids));
1120 gLog.log(entry.withAutomaticDuration());
1121 return binder::Status::ok();
1122}
1123
Luke Huange64fa382018-07-24 16:38:22 +08001124binder::Status NetdNativeService::firewallSetFirewallType(int32_t firewallType) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001125 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001126 auto type = static_cast<FirewallType>(firewallType);
1127
1128 int res = gCtls->firewallCtrl.setFirewallType(type);
Luke Huange64fa382018-07-24 16:38:22 +08001129 return statusFromErrcode(res);
1130}
1131
1132binder::Status NetdNativeService::firewallSetInterfaceRule(const std::string& ifName,
1133 int32_t firewallRule) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001134 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001135 auto rule = static_cast<FirewallRule>(firewallRule);
1136
1137 int res = gCtls->firewallCtrl.setInterfaceRule(ifName.c_str(), rule);
Luke Huange64fa382018-07-24 16:38:22 +08001138 return statusFromErrcode(res);
1139}
1140
1141binder::Status NetdNativeService::firewallSetUidRule(int32_t childChain, int32_t uid,
1142 int32_t firewallRule) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001143 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001144 auto chain = static_cast<ChildChain>(childChain);
1145 auto rule = static_cast<FirewallRule>(firewallRule);
1146
1147 int res = gCtls->firewallCtrl.setUidRule(chain, uid, rule);
Luke Huange64fa382018-07-24 16:38:22 +08001148 return statusFromErrcode(res);
1149}
1150
1151binder::Status NetdNativeService::firewallEnableChildChain(int32_t childChain, bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001152 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001153 auto chain = static_cast<ChildChain>(childChain);
1154
1155 int res = gCtls->firewallCtrl.enableChildChains(chain, enable);
Luke Huange64fa382018-07-24 16:38:22 +08001156 return statusFromErrcode(res);
1157}
1158
Luke Huang19b49c52018-10-22 12:12:05 +09001159binder::Status NetdNativeService::tetherAddForward(const std::string& intIface,
1160 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001161 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang19b49c52018-10-22 12:12:05 +09001162
1163 int res = gCtls->tetherCtrl.enableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001164 return statusFromErrcode(res);
1165}
1166
1167binder::Status NetdNativeService::tetherRemoveForward(const std::string& intIface,
1168 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001169 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangae038f82018-11-05 11:17:31 +09001170 int res = gCtls->tetherCtrl.disableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001171 return statusFromErrcode(res);
1172}
1173
Chenbo Fengf5663d82018-11-08 16:10:48 -08001174binder::Status NetdNativeService::setTcpRWmemorySize(const std::string& rmemValues,
1175 const std::string& wmemValues) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001176 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chenbo Fengf5663d82018-11-08 16:10:48 -08001177 if (!WriteStringToFile(rmemValues, TCP_RMEM_PROC_FILE)) {
1178 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001179 return statusFromErrcode(ret);
1180 }
1181
1182 if (!WriteStringToFile(wmemValues, TCP_WMEM_PROC_FILE)) {
1183 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001184 return statusFromErrcode(ret);
1185 }
Chenbo Fengf5663d82018-11-08 16:10:48 -08001186 return binder::Status::ok();
1187}
1188
Luke Huang528af602018-08-29 19:06:05 +08001189binder::Status NetdNativeService::registerUnsolicitedEventListener(
1190 const android::sp<android::net::INetdUnsolicitedEventListener>& listener) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001191 ENFORCE_NETWORK_STACK_PERMISSIONS();
Bernie Innocenti9ee088d2019-02-01 17:14:32 +09001192 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1193 gCtls->eventReporter.registerUnsolEventListener(listener);
Luke Huang528af602018-08-29 19:06:05 +08001194 gLog.log(entry.withAutomaticDuration());
1195 return binder::Status::ok();
1196}
1197
Lorenzo Colittie4d626e2016-02-02 17:19:04 +09001198} // namespace net
1199} // namespace android