blob: 15df7dad53be0fd6098caea193cdda55c9fef6da [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"];
167 Json::Value::Members member = inputArgs.getMemberNames();
168
169 hasReturnArgs = !returnArgs.empty();
170 output.append(logTransaction["method_name"].asString().c_str() + std::string("("));
171
172 // input args
Jeongik Cha374ca5b2019-03-22 12:11:25 +0900173 Json::FastWriter fastWriter;
174 fastWriter.omitEndingLineFeed();
Xiao Ma33d562a2018-12-16 16:27:38 +0900175 for (Json::Value::Members::iterator iter = member.begin(); iter != member.end(); ++iter) {
Jeongik Cha374ca5b2019-03-22 12:11:25 +0900176 std::string value = fastWriter.write(inputArgs[(*iter).c_str()]);
Xiao Ma33d562a2018-12-16 16:27:38 +0900177 if (value.empty()) value = std::string("null");
178 output.append(value);
179 if (iter != member.end() - 1) {
180 output.append(", ");
181 }
182 }
183 output.append(std::string(")"));
184 // return args
185 if (hasReturnArgs) {
Jeongik Cha374ca5b2019-03-22 12:11:25 +0900186 output.append(StringPrintf(" -> (%s)", fastWriter.write(returnArgs).c_str()));
Xiao Ma33d562a2018-12-16 16:27:38 +0900187 }
188 // duration time
189 output.append(StringPrintf(" <%sms>", logTransaction["duration_ms"].asString().c_str()));
190 gLog.info("%s", output.c_str());
191 };
192
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900193 return android::OK;
194}
195
Hugo Benichi7b314e12018-01-15 21:54:00 +0900196status_t NetdNativeService::dump(int fd, const Vector<String16> &args) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900197 const binder::Status dump_permission = checkAnyPermission({PERM_DUMP});
Erik Kline2d3a1632016-03-15 16:33:48 +0900198 if (!dump_permission.isOk()) {
199 const String8 msg(dump_permission.toString8());
200 write(fd, msg.string(), msg.size());
201 return PERMISSION_DENIED;
202 }
203
204 // This method does not grab any locks. If individual classes need locking
205 // their dump() methods MUST handle locking appropriately.
Hugo Benichi7b314e12018-01-15 21:54:00 +0900206
Erik Kline2d3a1632016-03-15 16:33:48 +0900207 DumpWriter dw(fd);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900208
209 if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) {
210 dw.blankline();
211 gCtls->tcpSocketMonitor.dump(dw);
212 dw.blankline();
213 return NO_ERROR;
214 }
215
Chenbo Fengef297172018-03-26 10:53:33 -0700216 if (!args.isEmpty() && args[0] == TrafficController::DUMP_KEYWORD) {
217 dw.blankline();
218 gCtls->trafficCtrl.dump(dw, true);
219 dw.blankline();
220 return NO_ERROR;
221 }
222
Erik Kline85890042018-05-25 19:19:11 +0900223 process::dump(dw);
Erik Kline2d3a1632016-03-15 16:33:48 +0900224 dw.blankline();
225 gCtls->netCtrl.dump(dw);
226 dw.blankline();
227
Chenbo Fengef297172018-03-26 10:53:33 -0700228 gCtls->trafficCtrl.dump(dw, false);
229 dw.blankline();
230
Benedict Wongaf855432018-05-10 17:07:37 -0700231 gCtls->xfrmCtrl.dump(dw);
232 dw.blankline();
233
Erik Klineb31fd692018-06-06 20:50:11 +0900234 {
235 ScopedIndent indentLog(dw);
236 if (contains(args, String16(OPT_SHORT))) {
237 dw.println("Log: <omitted>");
238 } else {
239 dw.println("Log:");
240 ScopedIndent indentLogEntries(dw);
241 gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); });
242 }
243 dw.blankline();
244 }
245
Luke Huang528af602018-08-29 19:06:05 +0800246 {
247 ScopedIndent indentLog(dw);
248 if (contains(args, String16(OPT_SHORT))) {
249 dw.println("UnsolicitedLog: <omitted>");
250 } else {
251 dw.println("UnsolicitedLog:");
252 ScopedIndent indentLogEntries(dw);
253 gUnsolicitedLog.forEachEntry(
254 [&dw](const std::string& entry) mutable { dw.println(entry); });
255 }
256 dw.blankline();
257 }
258
Erik Kline2d3a1632016-03-15 16:33:48 +0900259 return NO_ERROR;
260}
261
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900262binder::Status NetdNativeService::isAlive(bool *alive) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900263 NETD_BIG_LOCK_RPC(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900264
265 *alive = true;
Erik Klineb31fd692018-06-06 20:50:11 +0900266
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900267 return binder::Status::ok();
268}
269
Erik Klinef52d4522018-03-14 15:01:46 +0900270binder::Status NetdNativeService::firewallReplaceUidChain(const std::string& chainName,
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900271 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900272 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_CONNECTIVITY_INTERNAL,
273 PERM_MAINLINE_NETWORK_STACK);
Erik Klinef52d4522018-03-14 15:01:46 +0900274 int err = gCtls->firewallCtrl.replaceUidChain(chainName, isWhitelist, uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900275 *ret = (err == 0);
276 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900277}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900278
279binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900280 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_CONNECTIVITY_INTERNAL,
281 PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900282 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
283 *ret = (err == 0);
284 return binder::Status::ok();
285}
286
Luke Huang531f5d32018-08-03 15:19:05 +0800287binder::Status NetdNativeService::bandwidthSetInterfaceQuota(const std::string& ifName,
288 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900289 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800290 int res = gCtls->bandwidthCtrl.setInterfaceQuota(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800291 return statusFromErrcode(res);
292}
293
294binder::Status NetdNativeService::bandwidthRemoveInterfaceQuota(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900295 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800296 int res = gCtls->bandwidthCtrl.removeInterfaceQuota(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800297 return statusFromErrcode(res);
298}
299
300binder::Status NetdNativeService::bandwidthSetInterfaceAlert(const std::string& ifName,
301 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900302 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800303 int res = gCtls->bandwidthCtrl.setInterfaceAlert(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800304 return statusFromErrcode(res);
305}
306
307binder::Status NetdNativeService::bandwidthRemoveInterfaceAlert(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900308 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800309 int res = gCtls->bandwidthCtrl.removeInterfaceAlert(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800310 return statusFromErrcode(res);
311}
312
313binder::Status NetdNativeService::bandwidthSetGlobalAlert(int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900314 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800315 int res = gCtls->bandwidthCtrl.setGlobalAlert(bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800316 return statusFromErrcode(res);
317}
318
319binder::Status NetdNativeService::bandwidthAddNaughtyApp(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.addNaughtyApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800323 return statusFromErrcode(res);
324}
325
326binder::Status NetdNativeService::bandwidthRemoveNaughtyApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900327 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800328 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
329 int res = gCtls->bandwidthCtrl.removeNaughtyApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800330 return statusFromErrcode(res);
331}
332
333binder::Status NetdNativeService::bandwidthAddNiceApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900334 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800335 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
336 int res = gCtls->bandwidthCtrl.addNiceApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800337 return statusFromErrcode(res);
338}
339
340binder::Status NetdNativeService::bandwidthRemoveNiceApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900341 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800342 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
343 int res = gCtls->bandwidthCtrl.removeNiceApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800344 return statusFromErrcode(res);
345}
346
Luke Huangb670d162018-08-23 20:01:13 +0800347binder::Status NetdNativeService::networkCreatePhysical(int32_t netId, int32_t permission) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900348 ENFORCE_INTERNAL_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +0800349 int ret = gCtls->netCtrl.createPhysicalNetwork(netId, convertPermission(permission));
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900350 return statusFromErrcode(ret);
351}
352
cken67cd14c2018-12-05 17:26:59 +0900353binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool secure) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900354 ENFORCE_NETWORK_STACK_PERMISSIONS();
cken67cd14c2018-12-05 17:26:59 +0900355 int ret = gCtls->netCtrl.createVirtualNetwork(netId, secure);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900356 return statusFromErrcode(ret);
357}
358
359binder::Status NetdNativeService::networkDestroy(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900360 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangd33a8f42019-03-14 16:10:04 +0800361 // NetworkController::destroyNetwork is thread-safe.
Mike Yu4fe1b9c2019-01-29 17:50:19 +0800362 const int ret = gCtls->netCtrl.destroyNetwork(netId);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900363 return statusFromErrcode(ret);
364}
365
366binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900367 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900368 int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
369 return statusFromErrcode(ret);
370}
371
372binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900373 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900374 int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
375 return statusFromErrcode(ret);
376}
377
Luke Huang94658ac2018-10-18 19:35:12 +0900378binder::Status NetdNativeService::networkAddUidRanges(
379 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900380 // NetworkController::addUsersToNetwork is thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900381 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900382 int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray));
383 return statusFromErrcode(ret);
384}
385
Luke Huang94658ac2018-10-18 19:35:12 +0900386binder::Status NetdNativeService::networkRemoveUidRanges(
387 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900388 // NetworkController::removeUsersFromNetwork is thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900389 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900390 int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray));
391 return statusFromErrcode(ret);
392}
393
Luke Huang94658ac2018-10-18 19:35:12 +0900394binder::Status NetdNativeService::networkRejectNonSecureVpn(
395 bool add, const std::vector<UidRangeParcel>& uidRangeArray) {
Robin Leeb8087362016-03-30 18:43:08 +0100396 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
397 // it should be possible to use the same lock as NetworkController. However, every call through
398 // the CommandListener "network" command will need to hold this lock too, not just the ones that
399 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
400 // look at routes, but it's not enough here).
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900401 NETD_BIG_LOCK_RPC(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900402 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100403
404 int err;
405 if (add) {
406 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
407 } else {
408 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
409 }
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900410 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100411}
412
Luke Huang94658ac2018-10-18 19:35:12 +0900413binder::Status NetdNativeService::socketDestroy(const std::vector<UidRangeParcel>& uids,
414 const std::vector<int32_t>& skipUids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900415 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900416
417 SockDiag sd;
418 if (!sd.open()) {
419 return binder::Status::fromServiceSpecificError(EIO,
420 String8("Could not open SOCK_DIAG socket"));
421 }
422
423 UidRanges uidRanges(uids);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +0900424 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
425 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900426 if (err) {
427 return binder::Status::fromServiceSpecificError(-err,
428 String8::format("destroySockets: %s", strerror(-err)));
429 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900430 return binder::Status::ok();
431}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900432
Erik Klinef48e4dd2016-07-18 04:02:07 +0900433binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900434 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Erik Klinef48e4dd2016-07-18 04:02:07 +0900435 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
436 return binder::Status::ok();
437}
438
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900439namespace {
440
Luke Huangcaebcbb2018-09-27 20:37:14 +0800441void tetherAddStatsByInterface(TetherController::TetherStats* tetherStatsParcel,
442 const TetherController::TetherStats& tetherStats) {
443 if (tetherStatsParcel->extIface == tetherStats.extIface) {
444 tetherStatsParcel->rxBytes += tetherStats.rxBytes;
445 tetherStatsParcel->rxPackets += tetherStats.rxPackets;
446 tetherStatsParcel->txBytes += tetherStats.txBytes;
447 tetherStatsParcel->txPackets += tetherStats.txPackets;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900448 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800449}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900450
Luke Huangcaebcbb2018-09-27 20:37:14 +0800451TetherStatsParcel toTetherStatsParcel(const TetherController::TetherStats& stats) {
452 TetherStatsParcel result;
453 result.iface = stats.extIface;
454 result.rxBytes = stats.rxBytes;
455 result.rxPackets = stats.rxPackets;
456 result.txBytes = stats.txBytes;
457 result.txPackets = stats.txPackets;
458 return result;
459}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900460
Luke Huangcaebcbb2018-09-27 20:37:14 +0800461void setTetherStatsParcelVecByInterface(std::vector<TetherStatsParcel>* tetherStatsVec,
462 const TetherController::TetherStatsList& statsList) {
463 std::map<std::string, TetherController::TetherStats> statsMap;
464 for (const auto& stats : statsList) {
465 auto iter = statsMap.find(stats.extIface);
466 if (iter != statsMap.end()) {
467 tetherAddStatsByInterface(&(iter->second), stats);
468 } else {
469 statsMap.insert(
470 std::pair<std::string, TetherController::TetherStats>(stats.extIface, stats));
471 }
472 }
473 for (auto iter = statsMap.begin(); iter != statsMap.end(); iter++) {
474 tetherStatsVec->push_back(toTetherStatsParcel(iter->second));
475 }
476}
477
478std::vector<std::string> tetherStatsParcelVecToStringVec(std::vector<TetherStatsParcel>* tVec) {
479 std::vector<std::string> result;
480 for (const auto& t : *tVec) {
481 result.push_back(StringPrintf("%s:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64,
482 t.iface.c_str(), t.rxBytes, t.rxPackets, t.txBytes,
483 t.txPackets));
484 }
485 return result;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900486}
487
488} // namespace
489
Luke Huangcaebcbb2018-09-27 20:37:14 +0800490binder::Status NetdNativeService::tetherGetStats(
491 std::vector<TetherStatsParcel>* tetherStatsParcelVec) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900492 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900493 const auto& statsList = gCtls->tetherCtrl.getTetherStats();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900494 if (!isOk(statsList)) {
Nathan Harold28ccfef2018-03-16 19:02:47 -0700495 return asBinderStatus(statsList);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900496 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800497 setTetherStatsParcelVecByInterface(tetherStatsParcelVec, statsList.value());
498 auto statsResults = tetherStatsParcelVecToStringVec(tetherStatsParcelVec);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900499 return binder::Status::ok();
500}
501
Erik Kline53c20882016-08-02 15:22:53 +0900502binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
503 const std::string &addrString, int prefixLength) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900504 ENFORCE_INTERNAL_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900505 const int err = InterfaceController::addAddress(
506 ifName.c_str(), addrString.c_str(), prefixLength);
507 if (err != 0) {
508 return binder::Status::fromServiceSpecificError(-err,
509 String8::format("InterfaceController error: %s", strerror(-err)));
510 }
511 return binder::Status::ok();
512}
513
514binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
515 const std::string &addrString, int prefixLength) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900516 ENFORCE_INTERNAL_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900517 const int err = InterfaceController::delAddress(
518 ifName.c_str(), addrString.c_str(), prefixLength);
519 if (err != 0) {
520 return binder::Status::fromServiceSpecificError(-err,
521 String8::format("InterfaceController error: %s", strerror(-err)));
522 }
523 return binder::Status::ok();
524}
525
Erik Kline38e51f12018-09-06 20:14:44 +0900526namespace {
Erik Kline55b06f82016-07-04 09:57:18 +0900527
Erik Kline38e51f12018-09-06 20:14:44 +0900528std::tuple<binder::Status, const char*, const char*> getPathComponents(int32_t ipversion,
529 int32_t category) {
530 const char* ipversionStr = nullptr;
531 switch (ipversion) {
Erik Kline55b06f82016-07-04 09:57:18 +0900532 case INetd::IPV4:
Erik Kline38e51f12018-09-06 20:14:44 +0900533 ipversionStr = "ipv4";
Erik Kline55b06f82016-07-04 09:57:18 +0900534 break;
535 case INetd::IPV6:
Erik Kline38e51f12018-09-06 20:14:44 +0900536 ipversionStr = "ipv6";
Erik Kline55b06f82016-07-04 09:57:18 +0900537 break;
538 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900539 return {binder::Status::fromServiceSpecificError(EAFNOSUPPORT, "Bad IP version"),
540 nullptr, nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900541 }
542
Erik Kline38e51f12018-09-06 20:14:44 +0900543 const char* whichStr = nullptr;
544 switch (category) {
Erik Kline55b06f82016-07-04 09:57:18 +0900545 case INetd::CONF:
546 whichStr = "conf";
547 break;
548 case INetd::NEIGH:
549 whichStr = "neigh";
550 break;
551 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900552 return {binder::Status::fromServiceSpecificError(EINVAL, "Bad category"), nullptr,
553 nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900554 }
555
Erik Kline38e51f12018-09-06 20:14:44 +0900556 return {binder::Status::ok(), ipversionStr, whichStr};
557}
558
559} // namespace
560
561binder::Status NetdNativeService::getProcSysNet(int32_t ipversion, int32_t which,
562 const std::string& ifname,
563 const std::string& parameter, std::string* value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900564 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900565 const auto pathParts = getPathComponents(ipversion, which);
566 const auto& pathStatus = std::get<0>(pathParts);
567 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900568 return pathStatus;
Erik Kline55b06f82016-07-04 09:57:18 +0900569 }
Erik Kline38e51f12018-09-06 20:14:44 +0900570
571 const int err = InterfaceController::getParameter(std::get<1>(pathParts),
572 std::get<2>(pathParts), ifname.c_str(),
573 parameter.c_str(), value);
Erik Kline38e51f12018-09-06 20:14:44 +0900574 return statusFromErrcode(err);
575}
576
577binder::Status NetdNativeService::setProcSysNet(int32_t ipversion, int32_t which,
578 const std::string& ifname,
579 const std::string& parameter,
580 const std::string& value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900581 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900582 const auto pathParts = getPathComponents(ipversion, which);
583 const auto& pathStatus = std::get<0>(pathParts);
584 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900585 return pathStatus;
586 }
587
588 const int err = InterfaceController::setParameter(std::get<1>(pathParts),
589 std::get<2>(pathParts), ifname.c_str(),
590 parameter.c_str(), value.c_str());
Erik Kline38e51f12018-09-06 20:14:44 +0900591 return statusFromErrcode(err);
Erik Kline55b06f82016-07-04 09:57:18 +0900592}
593
Luke Huange203a152018-11-23 11:47:28 +0800594binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const ParcelFileDescriptor& socket,
595 int newUid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900596 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900597 gLog.log("ipSecSetEncapSocketOwner()");
Benedict Wongb2daefb2017-12-06 22:05:46 -0800598
599 uid_t callerUid = IPCThreadState::self()->getCallingUid();
Luke Huange203a152018-11-23 11:47:28 +0800600 return asBinderStatus(
601 gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket.get(), newUid, callerUid));
Benedict Wongb2daefb2017-12-06 22:05:46 -0800602}
603
Nathan Harold1a371532017-01-30 12:30:48 -0800604binder::Status NetdNativeService::ipSecAllocateSpi(
605 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800606 const std::string& sourceAddress,
607 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800608 int32_t inSpi,
609 int32_t* outSpi) {
610 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900611 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900612 gLog.log("ipSecAllocateSpi()");
ludi6e8eccd2017-08-14 14:40:37 -0700613 return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi(
Nathan Harold1a371532017-01-30 12:30:48 -0800614 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800615 sourceAddress,
616 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800617 inSpi,
618 outSpi));
619}
620
621binder::Status NetdNativeService::ipSecAddSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700622 int32_t transformId, int32_t mode, const std::string& sourceAddress,
623 const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi,
624 int32_t markValue, int32_t markMask, const std::string& authAlgo,
625 const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo,
626 const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo,
627 const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType,
628 int32_t encapLocalPort, int32_t encapRemotePort, int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800629 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900630 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900631 gLog.log("ipSecAddSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700632 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation(
Benedict Wongad600cb2018-05-14 17:22:35 -0700633 transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue,
634 markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits,
Benedict Wonga450e722018-05-07 10:29:02 -0700635 aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort,
636 interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800637}
638
639binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700640 int32_t transformId, const std::string& sourceAddress,
641 const std::string& destinationAddress, int32_t spi, int32_t markValue, int32_t markMask,
642 int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800643 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900644 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900645 gLog.log("ipSecDeleteSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700646 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700647 transformId, sourceAddress, destinationAddress, spi, markValue, markMask, interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800648}
649
650binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800651 const ParcelFileDescriptor& socket, int32_t transformId, int32_t direction,
652 const std::string& sourceAddress, const std::string& destinationAddress, int32_t spi) {
Nathan Harold1a371532017-01-30 12:30:48 -0800653 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900654 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900655 gLog.log("ipSecApplyTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700656 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800657 socket.get(), transformId, direction, sourceAddress, destinationAddress, spi));
Nathan Harold1a371532017-01-30 12:30:48 -0800658}
659
660binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800661 const ParcelFileDescriptor& socket) {
Nathan Harold1a371532017-01-30 12:30:48 -0800662 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900663 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900664 gLog.log("ipSecRemoveTransportModeTransform()");
Luke Huange203a152018-11-23 11:47:28 +0800665 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(socket.get()));
Nathan Harold1a371532017-01-30 12:30:48 -0800666}
667
Benedict Wonga04ffa72018-05-09 21:42:42 -0700668binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
669 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700670 const std::string& tmplSrcAddress,
671 const std::string& tmplDstAddress,
672 int32_t spi, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700673 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800674 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900675 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900676 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800677 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700678 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700679 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800680}
681
Benedict Wonga450e722018-05-07 10:29:02 -0700682binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(
683 int32_t transformId, int32_t selAddrFamily, int32_t direction,
684 const std::string& tmplSrcAddress, const std::string& tmplDstAddress, int32_t spi,
685 int32_t markValue, int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800686 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900687 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900688 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800689 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700690 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700691 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800692}
693
Benedict Wonga04ffa72018-05-09 21:42:42 -0700694binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId,
695 int32_t selAddrFamily,
696 int32_t direction, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700697 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800698 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900699 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900700 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800701 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
Benedict Wonga450e722018-05-07 10:29:02 -0700702 transformId, selAddrFamily, direction, markValue, markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800703}
704
Benedict Wong319f17e2018-05-15 17:06:44 -0700705binder::Status NetdNativeService::ipSecAddTunnelInterface(const std::string& deviceName,
706 const std::string& localAddress,
707 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700708 int32_t iKey, int32_t oKey,
709 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800710 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900711 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700712 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700713 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, false);
Benedict Wong319f17e2018-05-15 17:06:44 -0700714 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800715}
716
Benedict Wong319f17e2018-05-15 17:06:44 -0700717binder::Status NetdNativeService::ipSecUpdateTunnelInterface(const std::string& deviceName,
718 const std::string& localAddress,
719 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700720 int32_t iKey, int32_t oKey,
721 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800722 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900723 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700724 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700725 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, true);
Benedict Wong319f17e2018-05-15 17:06:44 -0700726 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800727}
728
Benedict Wong319f17e2018-05-15 17:06:44 -0700729binder::Status NetdNativeService::ipSecRemoveTunnelInterface(const std::string& deviceName) {
manojboopathi8707f232018-01-02 14:45:47 -0800730 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900731 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700732 netdutils::Status result = gCtls->xfrmCtrl.ipSecRemoveTunnelInterface(deviceName);
Benedict Wong319f17e2018-05-15 17:06:44 -0700733 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800734}
735
Joel Scherpelzde937962017-06-01 13:20:21 +0900736binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
737 int32_t mode) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900738 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700739 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900740}
741
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900742binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName,
743 const std::string& prefix, int32_t mark,
744 int32_t mask) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900745 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700746 return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900747}
748
749binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
750 const std::string& prefix, int32_t mark,
751 int32_t mask) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900752 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700753 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900754}
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) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900796 NETD_LOCKING_RPC(gCtls->clatdCtrl.mutex, 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) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900802 NETD_LOCKING_RPC(gCtls->clatdCtrl.mutex, 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 {
848std::string addSquareBrackets(const std::string& s) {
849 return "[" + s + "]";
850}
851
852std::string addCurlyBrackets(const std::string& s) {
853 return "{" + s + "}";
854}
855
856} // namespace
Xiao Ma33d562a2018-12-16 16:27:38 +0900857
Luke Huangf7782042018-08-08 13:13:04 +0800858binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900859 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800860 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
861
862 const auto& ifaceList = InterfaceController::getIfaceNames();
863 RETURN_BINDER_STATUS_IF_NOT_OK(entry, ifaceList);
864
865 interfaceListResult->clear();
866 interfaceListResult->reserve(ifaceList.value().size());
867 interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()),
868 end(ifaceList.value()));
869
870 gLog.log(entry.returns(addSquareBrackets(base::Join(*interfaceListResult, ", ")))
871 .withAutomaticDuration());
872 return binder::Status::ok();
873}
874
875std::string interfaceConfigurationParcelToString(const InterfaceConfigurationParcel& cfg) {
876 std::vector<std::string> result{cfg.ifName, cfg.hwAddr, cfg.ipv4Addr,
877 std::to_string(cfg.prefixLength)};
878 result.insert(end(result), begin(cfg.flags), end(cfg.flags));
879 return addCurlyBrackets(base::Join(result, ", "));
880}
881
882binder::Status NetdNativeService::interfaceGetCfg(
883 const std::string& ifName, InterfaceConfigurationParcel* interfaceGetCfgResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900884 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800885 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
886
887 const auto& cfgRes = InterfaceController::getCfg(ifName);
888 RETURN_BINDER_STATUS_IF_NOT_OK(entry, cfgRes);
889
890 *interfaceGetCfgResult = cfgRes.value();
891 gLog.log(entry.returns(interfaceConfigurationParcelToString(*interfaceGetCfgResult))
892 .withAutomaticDuration());
893 return binder::Status::ok();
894}
895
896binder::Status NetdNativeService::interfaceSetCfg(const InterfaceConfigurationParcel& cfg) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900897 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800898 auto entry = gLog.newEntry()
899 .prettyFunction(__PRETTY_FUNCTION__)
900 .arg(interfaceConfigurationParcelToString(cfg));
901
902 const auto& res = InterfaceController::setCfg(cfg);
903 RETURN_BINDER_STATUS_IF_NOT_OK(entry, res);
904
905 gLog.log(entry.withAutomaticDuration());
906 return binder::Status::ok();
907}
908
909binder::Status NetdNativeService::interfaceSetIPv6PrivacyExtensions(const std::string& ifName,
910 bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900911 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800912 int res = InterfaceController::setIPv6PrivacyExtensions(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800913 return statusFromErrcode(res);
914}
915
916binder::Status NetdNativeService::interfaceClearAddrs(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900917 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800918 int res = InterfaceController::clearAddrs(ifName.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800919 return statusFromErrcode(res);
920}
921
922binder::Status NetdNativeService::interfaceSetEnableIPv6(const std::string& ifName, bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900923 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800924 int res = InterfaceController::setEnableIPv6(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800925 return statusFromErrcode(res);
926}
927
928binder::Status NetdNativeService::interfaceSetMtu(const std::string& ifName, int32_t mtuValue) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900929 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800930 std::string mtu = std::to_string(mtuValue);
931 int res = InterfaceController::setMtu(ifName.c_str(), mtu.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800932 return statusFromErrcode(res);
933}
934
Luke Huangb5733d72018-08-21 17:17:19 +0800935binder::Status NetdNativeService::tetherStart(const std::vector<std::string>& dhcpRanges) {
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 if (dhcpRanges.size() % 2 == 1) {
938 return statusFromErrcode(-EINVAL);
939 }
940 int res = gCtls->tetherCtrl.startTethering(dhcpRanges);
Luke Huangb5733d72018-08-21 17:17:19 +0800941 return statusFromErrcode(res);
942}
943
944binder::Status NetdNativeService::tetherStop() {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900945 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800946 int res = gCtls->tetherCtrl.stopTethering();
Luke Huangb5733d72018-08-21 17:17:19 +0800947 return statusFromErrcode(res);
948}
949
950binder::Status NetdNativeService::tetherIsEnabled(bool* enabled) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900951 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800952 *enabled = gCtls->tetherCtrl.isTetheringStarted();
Luke Huangb5733d72018-08-21 17:17:19 +0800953 return binder::Status::ok();
954}
955
956binder::Status NetdNativeService::tetherInterfaceAdd(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900957 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800958 int res = gCtls->tetherCtrl.tetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800959 return statusFromErrcode(res);
960}
961
962binder::Status NetdNativeService::tetherInterfaceRemove(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900963 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800964 int res = gCtls->tetherCtrl.untetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800965 return statusFromErrcode(res);
966}
967
968binder::Status NetdNativeService::tetherInterfaceList(std::vector<std::string>* ifList) {
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 for (const auto& ifname : gCtls->tetherCtrl.getTetheredInterfaceList()) {
971 ifList->push_back(ifname);
972 }
Luke Huangb5733d72018-08-21 17:17:19 +0800973 return binder::Status::ok();
974}
975
976binder::Status NetdNativeService::tetherDnsSet(int32_t netId,
977 const std::vector<std::string>& dnsAddrs) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900978 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800979 int res = gCtls->tetherCtrl.setDnsForwarders(netId, dnsAddrs);
Luke Huangb5733d72018-08-21 17:17:19 +0800980 return statusFromErrcode(res);
981}
982
983binder::Status NetdNativeService::tetherDnsList(std::vector<std::string>* dnsList) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900984 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800985 for (const auto& fwdr : gCtls->tetherCtrl.getDnsForwarders()) {
986 dnsList->push_back(fwdr);
987 }
Luke Huangb5733d72018-08-21 17:17:19 +0800988 return binder::Status::ok();
989}
990
Luke Huangb670d162018-08-23 20:01:13 +0800991binder::Status NetdNativeService::networkAddRoute(int32_t netId, const std::string& ifName,
992 const std::string& destination,
993 const std::string& nextHop) {
994 // Public methods of NetworkController are thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900995 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +0800996 bool legacy = false;
997 uid_t uid = 0; // UID is only meaningful for legacy routes.
998 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
999 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
Luke Huangb670d162018-08-23 20:01:13 +08001000 return statusFromErrcode(res);
1001}
1002
1003binder::Status NetdNativeService::networkRemoveRoute(int32_t netId, const std::string& ifName,
1004 const std::string& destination,
1005 const std::string& nextHop) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001006 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001007 bool legacy = false;
1008 uid_t uid = 0; // UID is only meaningful for legacy routes.
1009 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1010 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
Luke Huangb670d162018-08-23 20:01:13 +08001011 return statusFromErrcode(res);
1012}
1013
1014binder::Status NetdNativeService::networkAddLegacyRoute(int32_t netId, const std::string& ifName,
1015 const std::string& destination,
1016 const std::string& nextHop, int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001017 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001018 bool legacy = true;
1019 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1020 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1021 (uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001022 return statusFromErrcode(res);
1023}
1024
1025binder::Status NetdNativeService::networkRemoveLegacyRoute(int32_t netId, const std::string& ifName,
1026 const std::string& destination,
1027 const std::string& nextHop,
1028 int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001029 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001030 bool legacy = true;
1031 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1032 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1033 (uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001034 return statusFromErrcode(res);
1035}
1036
1037binder::Status NetdNativeService::networkGetDefault(int32_t* netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001038 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001039 *netId = gCtls->netCtrl.getDefaultNetwork();
Luke Huangb670d162018-08-23 20:01:13 +08001040 return binder::Status::ok();
1041}
1042
1043binder::Status NetdNativeService::networkSetDefault(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001044 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001045 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001046 return statusFromErrcode(res);
1047}
1048
1049binder::Status NetdNativeService::networkClearDefault() {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001050 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001051 unsigned netId = NETID_UNSET;
1052 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001053 return statusFromErrcode(res);
1054}
1055
1056std::vector<uid_t> NetdNativeService::intsToUids(const std::vector<int32_t>& intUids) {
1057 return {begin(intUids), end(intUids)};
1058}
1059
1060Permission NetdNativeService::convertPermission(int32_t permission) {
1061 switch (permission) {
1062 case INetd::PERMISSION_NETWORK:
1063 return Permission::PERMISSION_NETWORK;
1064 case INetd::PERMISSION_SYSTEM:
1065 return Permission::PERMISSION_SYSTEM;
1066 default:
1067 return Permission::PERMISSION_NONE;
1068 }
1069}
1070
1071binder::Status NetdNativeService::networkSetPermissionForNetwork(int32_t netId,
1072 int32_t permission) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001073 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001074 std::vector<unsigned> netIds = {(unsigned) netId};
1075 int res = gCtls->netCtrl.setPermissionForNetworks(convertPermission(permission), netIds);
Luke Huangb670d162018-08-23 20:01:13 +08001076 return statusFromErrcode(res);
1077}
1078
1079binder::Status NetdNativeService::networkSetPermissionForUser(int32_t permission,
1080 const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001081 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001082 gCtls->netCtrl.setPermissionForUsers(convertPermission(permission), intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001083 return binder::Status::ok();
1084}
1085
1086binder::Status NetdNativeService::networkClearPermissionForUser(const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001087 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001088 Permission permission = Permission::PERMISSION_NONE;
1089 gCtls->netCtrl.setPermissionForUsers(permission, intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001090 return binder::Status::ok();
1091}
1092
1093binder::Status NetdNativeService::NetdNativeService::networkSetProtectAllow(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001094 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001095 std::vector<uid_t> uids = {(uid_t) uid};
1096 gCtls->netCtrl.allowProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001097 return binder::Status::ok();
1098}
1099
1100binder::Status NetdNativeService::networkSetProtectDeny(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001101 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001102 std::vector<uid_t> uids = {(uid_t) uid};
1103 gCtls->netCtrl.denyProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001104 return binder::Status::ok();
1105}
1106
1107binder::Status NetdNativeService::networkCanProtect(int32_t uid, bool* ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001108 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001109 *ret = gCtls->netCtrl.canProtect((uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001110 return binder::Status::ok();
1111}
1112
Chenbo Feng48eaed32018-12-26 17:40:21 -08001113binder::Status NetdNativeService::trafficSetNetPermForUids(int32_t permission,
1114 const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001115 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chenbo Feng48eaed32018-12-26 17:40:21 -08001116 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(permission).arg(uids);
1117 gCtls->trafficCtrl.setPermissionForUids(permission, intsToUids(uids));
1118 gLog.log(entry.withAutomaticDuration());
1119 return binder::Status::ok();
1120}
1121
Luke Huange64fa382018-07-24 16:38:22 +08001122binder::Status NetdNativeService::firewallSetFirewallType(int32_t firewallType) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001123 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001124 auto type = static_cast<FirewallType>(firewallType);
1125
1126 int res = gCtls->firewallCtrl.setFirewallType(type);
Luke Huange64fa382018-07-24 16:38:22 +08001127 return statusFromErrcode(res);
1128}
1129
1130binder::Status NetdNativeService::firewallSetInterfaceRule(const std::string& ifName,
1131 int32_t firewallRule) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001132 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001133 auto rule = static_cast<FirewallRule>(firewallRule);
1134
1135 int res = gCtls->firewallCtrl.setInterfaceRule(ifName.c_str(), rule);
Luke Huange64fa382018-07-24 16:38:22 +08001136 return statusFromErrcode(res);
1137}
1138
1139binder::Status NetdNativeService::firewallSetUidRule(int32_t childChain, int32_t uid,
1140 int32_t firewallRule) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001141 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001142 auto chain = static_cast<ChildChain>(childChain);
1143 auto rule = static_cast<FirewallRule>(firewallRule);
1144
1145 int res = gCtls->firewallCtrl.setUidRule(chain, uid, rule);
Luke Huange64fa382018-07-24 16:38:22 +08001146 return statusFromErrcode(res);
1147}
1148
1149binder::Status NetdNativeService::firewallEnableChildChain(int32_t childChain, bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001150 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001151 auto chain = static_cast<ChildChain>(childChain);
1152
1153 int res = gCtls->firewallCtrl.enableChildChains(chain, enable);
Luke Huange64fa382018-07-24 16:38:22 +08001154 return statusFromErrcode(res);
1155}
1156
Luke Huang19b49c52018-10-22 12:12:05 +09001157binder::Status NetdNativeService::tetherAddForward(const std::string& intIface,
1158 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001159 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang19b49c52018-10-22 12:12:05 +09001160
1161 int res = gCtls->tetherCtrl.enableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001162 return statusFromErrcode(res);
1163}
1164
1165binder::Status NetdNativeService::tetherRemoveForward(const std::string& intIface,
1166 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001167 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangae038f82018-11-05 11:17:31 +09001168 int res = gCtls->tetherCtrl.disableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001169 return statusFromErrcode(res);
1170}
1171
Chenbo Fengf5663d82018-11-08 16:10:48 -08001172binder::Status NetdNativeService::setTcpRWmemorySize(const std::string& rmemValues,
1173 const std::string& wmemValues) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001174 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chenbo Fengf5663d82018-11-08 16:10:48 -08001175 if (!WriteStringToFile(rmemValues, TCP_RMEM_PROC_FILE)) {
1176 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001177 return statusFromErrcode(ret);
1178 }
1179
1180 if (!WriteStringToFile(wmemValues, TCP_WMEM_PROC_FILE)) {
1181 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001182 return statusFromErrcode(ret);
1183 }
Chenbo Fengf5663d82018-11-08 16:10:48 -08001184 return binder::Status::ok();
1185}
1186
Luke Huang528af602018-08-29 19:06:05 +08001187binder::Status NetdNativeService::registerUnsolicitedEventListener(
1188 const android::sp<android::net::INetdUnsolicitedEventListener>& listener) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001189 ENFORCE_NETWORK_STACK_PERMISSIONS();
Bernie Innocenti9ee088d2019-02-01 17:14:32 +09001190 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1191 gCtls->eventReporter.registerUnsolEventListener(listener);
Luke Huang528af602018-08-29 19:06:05 +08001192 gLog.log(entry.withAutomaticDuration());
1193 return binder::Status::ok();
1194}
1195
Lorenzo Colittie4d626e2016-02-02 17:19:04 +09001196} // namespace net
1197} // namespace android