blob: a4558765558176d1e935a9f0a33dce10f28658cb [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
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700234 gCtls->clatdCtrl.dump(dw);
235 dw.blankline();
236
Erik Klineb31fd692018-06-06 20:50:11 +0900237 {
238 ScopedIndent indentLog(dw);
239 if (contains(args, String16(OPT_SHORT))) {
240 dw.println("Log: <omitted>");
241 } else {
242 dw.println("Log:");
243 ScopedIndent indentLogEntries(dw);
244 gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); });
245 }
246 dw.blankline();
247 }
248
Luke Huang528af602018-08-29 19:06:05 +0800249 {
250 ScopedIndent indentLog(dw);
251 if (contains(args, String16(OPT_SHORT))) {
252 dw.println("UnsolicitedLog: <omitted>");
253 } else {
254 dw.println("UnsolicitedLog:");
255 ScopedIndent indentLogEntries(dw);
256 gUnsolicitedLog.forEachEntry(
257 [&dw](const std::string& entry) mutable { dw.println(entry); });
258 }
259 dw.blankline();
260 }
261
Erik Kline2d3a1632016-03-15 16:33:48 +0900262 return NO_ERROR;
263}
264
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900265binder::Status NetdNativeService::isAlive(bool *alive) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900266 NETD_BIG_LOCK_RPC(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900267
268 *alive = true;
Erik Klineb31fd692018-06-06 20:50:11 +0900269
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900270 return binder::Status::ok();
271}
272
Erik Klinef52d4522018-03-14 15:01:46 +0900273binder::Status NetdNativeService::firewallReplaceUidChain(const std::string& chainName,
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900274 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900275 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_CONNECTIVITY_INTERNAL,
276 PERM_MAINLINE_NETWORK_STACK);
Erik Klinef52d4522018-03-14 15:01:46 +0900277 int err = gCtls->firewallCtrl.replaceUidChain(chainName, isWhitelist, uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900278 *ret = (err == 0);
279 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900280}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900281
282binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900283 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_CONNECTIVITY_INTERNAL,
284 PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900285 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
286 *ret = (err == 0);
287 return binder::Status::ok();
288}
289
Luke Huang531f5d32018-08-03 15:19:05 +0800290binder::Status NetdNativeService::bandwidthSetInterfaceQuota(const std::string& ifName,
291 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900292 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800293 int res = gCtls->bandwidthCtrl.setInterfaceQuota(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800294 return statusFromErrcode(res);
295}
296
297binder::Status NetdNativeService::bandwidthRemoveInterfaceQuota(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900298 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800299 int res = gCtls->bandwidthCtrl.removeInterfaceQuota(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800300 return statusFromErrcode(res);
301}
302
303binder::Status NetdNativeService::bandwidthSetInterfaceAlert(const std::string& ifName,
304 int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900305 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800306 int res = gCtls->bandwidthCtrl.setInterfaceAlert(ifName, bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800307 return statusFromErrcode(res);
308}
309
310binder::Status NetdNativeService::bandwidthRemoveInterfaceAlert(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900311 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800312 int res = gCtls->bandwidthCtrl.removeInterfaceAlert(ifName);
Luke Huang531f5d32018-08-03 15:19:05 +0800313 return statusFromErrcode(res);
314}
315
316binder::Status NetdNativeService::bandwidthSetGlobalAlert(int64_t bytes) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900317 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800318 int res = gCtls->bandwidthCtrl.setGlobalAlert(bytes);
Luke Huang531f5d32018-08-03 15:19:05 +0800319 return statusFromErrcode(res);
320}
321
322binder::Status NetdNativeService::bandwidthAddNaughtyApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900323 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800324 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
325 int res = gCtls->bandwidthCtrl.addNaughtyApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800326 return statusFromErrcode(res);
327}
328
329binder::Status NetdNativeService::bandwidthRemoveNaughtyApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900330 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800331 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
332 int res = gCtls->bandwidthCtrl.removeNaughtyApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800333 return statusFromErrcode(res);
334}
335
336binder::Status NetdNativeService::bandwidthAddNiceApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900337 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800338 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
339 int res = gCtls->bandwidthCtrl.addNiceApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800340 return statusFromErrcode(res);
341}
342
343binder::Status NetdNativeService::bandwidthRemoveNiceApp(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900344 NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang531f5d32018-08-03 15:19:05 +0800345 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
346 int res = gCtls->bandwidthCtrl.removeNiceApps(appStrUids);
Luke Huang531f5d32018-08-03 15:19:05 +0800347 return statusFromErrcode(res);
348}
349
Luke Huangb670d162018-08-23 20:01:13 +0800350binder::Status NetdNativeService::networkCreatePhysical(int32_t netId, int32_t permission) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900351 ENFORCE_INTERNAL_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +0800352 int ret = gCtls->netCtrl.createPhysicalNetwork(netId, convertPermission(permission));
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900353 return statusFromErrcode(ret);
354}
355
cken67cd14c2018-12-05 17:26:59 +0900356binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool secure) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900357 ENFORCE_NETWORK_STACK_PERMISSIONS();
cken67cd14c2018-12-05 17:26:59 +0900358 int ret = gCtls->netCtrl.createVirtualNetwork(netId, secure);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900359 return statusFromErrcode(ret);
360}
361
362binder::Status NetdNativeService::networkDestroy(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900363 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangd33a8f42019-03-14 16:10:04 +0800364 // NetworkController::destroyNetwork is thread-safe.
Mike Yu4fe1b9c2019-01-29 17:50:19 +0800365 const int ret = gCtls->netCtrl.destroyNetwork(netId);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900366 return statusFromErrcode(ret);
367}
368
369binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900370 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900371 int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
372 return statusFromErrcode(ret);
373}
374
375binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900376 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900377 int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
378 return statusFromErrcode(ret);
379}
380
Luke Huang94658ac2018-10-18 19:35:12 +0900381binder::Status NetdNativeService::networkAddUidRanges(
382 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900383 // NetworkController::addUsersToNetwork is thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900384 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900385 int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray));
386 return statusFromErrcode(ret);
387}
388
Luke Huang94658ac2018-10-18 19:35:12 +0900389binder::Status NetdNativeService::networkRemoveUidRanges(
390 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900391 // NetworkController::removeUsersFromNetwork is thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900392 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900393 int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray));
394 return statusFromErrcode(ret);
395}
396
Luke Huang94658ac2018-10-18 19:35:12 +0900397binder::Status NetdNativeService::networkRejectNonSecureVpn(
398 bool add, const std::vector<UidRangeParcel>& uidRangeArray) {
Robin Leeb8087362016-03-30 18:43:08 +0100399 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
400 // it should be possible to use the same lock as NetworkController. However, every call through
401 // the CommandListener "network" command will need to hold this lock too, not just the ones that
402 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
403 // look at routes, but it's not enough here).
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900404 NETD_BIG_LOCK_RPC(PERM_CONNECTIVITY_INTERNAL, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900405 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100406
407 int err;
408 if (add) {
409 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
410 } else {
411 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
412 }
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900413 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100414}
415
Luke Huang94658ac2018-10-18 19:35:12 +0900416binder::Status NetdNativeService::socketDestroy(const std::vector<UidRangeParcel>& uids,
417 const std::vector<int32_t>& skipUids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900418 ENFORCE_INTERNAL_PERMISSIONS();
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900419
420 SockDiag sd;
421 if (!sd.open()) {
422 return binder::Status::fromServiceSpecificError(EIO,
423 String8("Could not open SOCK_DIAG socket"));
424 }
425
426 UidRanges uidRanges(uids);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +0900427 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
428 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900429 if (err) {
430 return binder::Status::fromServiceSpecificError(-err,
431 String8::format("destroySockets: %s", strerror(-err)));
432 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900433 return binder::Status::ok();
434}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900435
Erik Klinef48e4dd2016-07-18 04:02:07 +0900436binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900437 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Erik Klinef48e4dd2016-07-18 04:02:07 +0900438 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
439 return binder::Status::ok();
440}
441
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900442namespace {
443
Luke Huangcaebcbb2018-09-27 20:37:14 +0800444void tetherAddStatsByInterface(TetherController::TetherStats* tetherStatsParcel,
445 const TetherController::TetherStats& tetherStats) {
446 if (tetherStatsParcel->extIface == tetherStats.extIface) {
447 tetherStatsParcel->rxBytes += tetherStats.rxBytes;
448 tetherStatsParcel->rxPackets += tetherStats.rxPackets;
449 tetherStatsParcel->txBytes += tetherStats.txBytes;
450 tetherStatsParcel->txPackets += tetherStats.txPackets;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900451 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800452}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900453
Luke Huangcaebcbb2018-09-27 20:37:14 +0800454TetherStatsParcel toTetherStatsParcel(const TetherController::TetherStats& stats) {
455 TetherStatsParcel result;
456 result.iface = stats.extIface;
457 result.rxBytes = stats.rxBytes;
458 result.rxPackets = stats.rxPackets;
459 result.txBytes = stats.txBytes;
460 result.txPackets = stats.txPackets;
461 return result;
462}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900463
Luke Huangcaebcbb2018-09-27 20:37:14 +0800464void setTetherStatsParcelVecByInterface(std::vector<TetherStatsParcel>* tetherStatsVec,
465 const TetherController::TetherStatsList& statsList) {
466 std::map<std::string, TetherController::TetherStats> statsMap;
467 for (const auto& stats : statsList) {
468 auto iter = statsMap.find(stats.extIface);
469 if (iter != statsMap.end()) {
470 tetherAddStatsByInterface(&(iter->second), stats);
471 } else {
472 statsMap.insert(
473 std::pair<std::string, TetherController::TetherStats>(stats.extIface, stats));
474 }
475 }
476 for (auto iter = statsMap.begin(); iter != statsMap.end(); iter++) {
477 tetherStatsVec->push_back(toTetherStatsParcel(iter->second));
478 }
479}
480
481std::vector<std::string> tetherStatsParcelVecToStringVec(std::vector<TetherStatsParcel>* tVec) {
482 std::vector<std::string> result;
483 for (const auto& t : *tVec) {
484 result.push_back(StringPrintf("%s:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64,
485 t.iface.c_str(), t.rxBytes, t.rxPackets, t.txBytes,
486 t.txPackets));
487 }
488 return result;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900489}
490
491} // namespace
492
Luke Huangcaebcbb2018-09-27 20:37:14 +0800493binder::Status NetdNativeService::tetherGetStats(
494 std::vector<TetherStatsParcel>* tetherStatsParcelVec) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900495 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900496 const auto& statsList = gCtls->tetherCtrl.getTetherStats();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900497 if (!isOk(statsList)) {
Nathan Harold28ccfef2018-03-16 19:02:47 -0700498 return asBinderStatus(statsList);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900499 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800500 setTetherStatsParcelVecByInterface(tetherStatsParcelVec, statsList.value());
501 auto statsResults = tetherStatsParcelVecToStringVec(tetherStatsParcelVec);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900502 return binder::Status::ok();
503}
504
Erik Kline53c20882016-08-02 15:22:53 +0900505binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
506 const std::string &addrString, int prefixLength) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900507 ENFORCE_INTERNAL_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900508 const int err = InterfaceController::addAddress(
509 ifName.c_str(), addrString.c_str(), prefixLength);
510 if (err != 0) {
511 return binder::Status::fromServiceSpecificError(-err,
512 String8::format("InterfaceController error: %s", strerror(-err)));
513 }
514 return binder::Status::ok();
515}
516
517binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
518 const std::string &addrString, int prefixLength) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900519 ENFORCE_INTERNAL_PERMISSIONS();
Erik Kline53c20882016-08-02 15:22:53 +0900520 const int err = InterfaceController::delAddress(
521 ifName.c_str(), addrString.c_str(), prefixLength);
522 if (err != 0) {
523 return binder::Status::fromServiceSpecificError(-err,
524 String8::format("InterfaceController error: %s", strerror(-err)));
525 }
526 return binder::Status::ok();
527}
528
Erik Kline38e51f12018-09-06 20:14:44 +0900529namespace {
Erik Kline55b06f82016-07-04 09:57:18 +0900530
Erik Kline38e51f12018-09-06 20:14:44 +0900531std::tuple<binder::Status, const char*, const char*> getPathComponents(int32_t ipversion,
532 int32_t category) {
533 const char* ipversionStr = nullptr;
534 switch (ipversion) {
Erik Kline55b06f82016-07-04 09:57:18 +0900535 case INetd::IPV4:
Erik Kline38e51f12018-09-06 20:14:44 +0900536 ipversionStr = "ipv4";
Erik Kline55b06f82016-07-04 09:57:18 +0900537 break;
538 case INetd::IPV6:
Erik Kline38e51f12018-09-06 20:14:44 +0900539 ipversionStr = "ipv6";
Erik Kline55b06f82016-07-04 09:57:18 +0900540 break;
541 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900542 return {binder::Status::fromServiceSpecificError(EAFNOSUPPORT, "Bad IP version"),
543 nullptr, nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900544 }
545
Erik Kline38e51f12018-09-06 20:14:44 +0900546 const char* whichStr = nullptr;
547 switch (category) {
Erik Kline55b06f82016-07-04 09:57:18 +0900548 case INetd::CONF:
549 whichStr = "conf";
550 break;
551 case INetd::NEIGH:
552 whichStr = "neigh";
553 break;
554 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900555 return {binder::Status::fromServiceSpecificError(EINVAL, "Bad category"), nullptr,
556 nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900557 }
558
Erik Kline38e51f12018-09-06 20:14:44 +0900559 return {binder::Status::ok(), ipversionStr, whichStr};
560}
561
562} // namespace
563
564binder::Status NetdNativeService::getProcSysNet(int32_t ipversion, int32_t which,
565 const std::string& ifname,
566 const std::string& parameter, std::string* value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900567 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900568 const auto pathParts = getPathComponents(ipversion, which);
569 const auto& pathStatus = std::get<0>(pathParts);
570 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900571 return pathStatus;
Erik Kline55b06f82016-07-04 09:57:18 +0900572 }
Erik Kline38e51f12018-09-06 20:14:44 +0900573
574 const int err = InterfaceController::getParameter(std::get<1>(pathParts),
575 std::get<2>(pathParts), ifname.c_str(),
576 parameter.c_str(), value);
Erik Kline38e51f12018-09-06 20:14:44 +0900577 return statusFromErrcode(err);
578}
579
580binder::Status NetdNativeService::setProcSysNet(int32_t ipversion, int32_t which,
581 const std::string& ifname,
582 const std::string& parameter,
583 const std::string& value) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900584 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Kline38e51f12018-09-06 20:14:44 +0900585 const auto pathParts = getPathComponents(ipversion, which);
586 const auto& pathStatus = std::get<0>(pathParts);
587 if (!pathStatus.isOk()) {
Erik Kline38e51f12018-09-06 20:14:44 +0900588 return pathStatus;
589 }
590
591 const int err = InterfaceController::setParameter(std::get<1>(pathParts),
592 std::get<2>(pathParts), ifname.c_str(),
593 parameter.c_str(), value.c_str());
Erik Kline38e51f12018-09-06 20:14:44 +0900594 return statusFromErrcode(err);
Erik Kline55b06f82016-07-04 09:57:18 +0900595}
596
Luke Huange203a152018-11-23 11:47:28 +0800597binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const ParcelFileDescriptor& socket,
598 int newUid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900599 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900600 gLog.log("ipSecSetEncapSocketOwner()");
Benedict Wongb2daefb2017-12-06 22:05:46 -0800601
602 uid_t callerUid = IPCThreadState::self()->getCallingUid();
Luke Huange203a152018-11-23 11:47:28 +0800603 return asBinderStatus(
604 gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket.get(), newUid, callerUid));
Benedict Wongb2daefb2017-12-06 22:05:46 -0800605}
606
Nathan Harold1a371532017-01-30 12:30:48 -0800607binder::Status NetdNativeService::ipSecAllocateSpi(
608 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800609 const std::string& sourceAddress,
610 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800611 int32_t inSpi,
612 int32_t* outSpi) {
613 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900614 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900615 gLog.log("ipSecAllocateSpi()");
ludi6e8eccd2017-08-14 14:40:37 -0700616 return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi(
Nathan Harold1a371532017-01-30 12:30:48 -0800617 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800618 sourceAddress,
619 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800620 inSpi,
621 outSpi));
622}
623
624binder::Status NetdNativeService::ipSecAddSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700625 int32_t transformId, int32_t mode, const std::string& sourceAddress,
626 const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi,
627 int32_t markValue, int32_t markMask, const std::string& authAlgo,
628 const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo,
629 const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo,
630 const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType,
631 int32_t encapLocalPort, int32_t encapRemotePort, int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800632 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900633 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900634 gLog.log("ipSecAddSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700635 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation(
Benedict Wongad600cb2018-05-14 17:22:35 -0700636 transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue,
637 markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits,
Benedict Wonga450e722018-05-07 10:29:02 -0700638 aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort,
639 interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800640}
641
642binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700643 int32_t transformId, const std::string& sourceAddress,
644 const std::string& destinationAddress, int32_t spi, int32_t markValue, int32_t markMask,
645 int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800646 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900647 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900648 gLog.log("ipSecDeleteSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700649 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700650 transformId, sourceAddress, destinationAddress, spi, markValue, markMask, interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800651}
652
653binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800654 const ParcelFileDescriptor& socket, int32_t transformId, int32_t direction,
655 const std::string& sourceAddress, const std::string& destinationAddress, int32_t spi) {
Nathan Harold1a371532017-01-30 12:30:48 -0800656 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900657 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900658 gLog.log("ipSecApplyTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700659 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800660 socket.get(), transformId, direction, sourceAddress, destinationAddress, spi));
Nathan Harold1a371532017-01-30 12:30:48 -0800661}
662
663binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800664 const ParcelFileDescriptor& socket) {
Nathan Harold1a371532017-01-30 12:30:48 -0800665 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900666 ENFORCE_INTERNAL_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900667 gLog.log("ipSecRemoveTransportModeTransform()");
Luke Huange203a152018-11-23 11:47:28 +0800668 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(socket.get()));
Nathan Harold1a371532017-01-30 12:30:48 -0800669}
670
Benedict Wonga04ffa72018-05-09 21:42:42 -0700671binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
672 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700673 const std::string& tmplSrcAddress,
674 const std::string& tmplDstAddress,
675 int32_t spi, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700676 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800677 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900678 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900679 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800680 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700681 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700682 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800683}
684
Benedict Wonga450e722018-05-07 10:29:02 -0700685binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(
686 int32_t transformId, int32_t selAddrFamily, int32_t direction,
687 const std::string& tmplSrcAddress, const std::string& tmplDstAddress, int32_t spi,
688 int32_t markValue, int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800689 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900690 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900691 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800692 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700693 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700694 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800695}
696
Benedict Wonga04ffa72018-05-09 21:42:42 -0700697binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId,
698 int32_t selAddrFamily,
699 int32_t direction, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700700 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800701 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900702 ENFORCE_NETWORK_STACK_PERMISSIONS();
Erik Klineb31fd692018-06-06 20:50:11 +0900703 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800704 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
Benedict Wonga450e722018-05-07 10:29:02 -0700705 transformId, selAddrFamily, direction, markValue, markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800706}
707
Benedict Wong319f17e2018-05-15 17:06:44 -0700708binder::Status NetdNativeService::ipSecAddTunnelInterface(const std::string& deviceName,
709 const std::string& localAddress,
710 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700711 int32_t iKey, int32_t oKey,
712 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800713 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900714 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700715 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700716 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, false);
Benedict Wong319f17e2018-05-15 17:06:44 -0700717 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800718}
719
Benedict Wong319f17e2018-05-15 17:06:44 -0700720binder::Status NetdNativeService::ipSecUpdateTunnelInterface(const std::string& deviceName,
721 const std::string& localAddress,
722 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700723 int32_t iKey, int32_t oKey,
724 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800725 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900726 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700727 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700728 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, true);
Benedict Wong319f17e2018-05-15 17:06:44 -0700729 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800730}
731
Benedict Wong319f17e2018-05-15 17:06:44 -0700732binder::Status NetdNativeService::ipSecRemoveTunnelInterface(const std::string& deviceName) {
manojboopathi8707f232018-01-02 14:45:47 -0800733 // Necessary locking done in IpSecService and kernel
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900734 ENFORCE_NETWORK_STACK_PERMISSIONS();
Benedict Wong319f17e2018-05-15 17:06:44 -0700735 netdutils::Status result = gCtls->xfrmCtrl.ipSecRemoveTunnelInterface(deviceName);
Benedict Wong319f17e2018-05-15 17:06:44 -0700736 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800737}
738
Joel Scherpelzde937962017-06-01 13:20:21 +0900739binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
740 int32_t mode) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900741 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700742 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900743}
744
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900745binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName,
746 const std::string& prefix, int32_t mark,
747 int32_t mask) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900748 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700749 return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900750}
751
752binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
753 const std::string& prefix, int32_t mark,
754 int32_t mask) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900755 ENFORCE_NETWORK_STACK_PERMISSIONS();
Nathan Harold28ccfef2018-03-16 19:02:47 -0700756 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900757}
758
Luke Huang0051a622018-07-23 20:30:16 +0800759binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout,
760 const std::string& classLabel) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900761 NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang0051a622018-07-23 20:30:16 +0800762 int res =
763 gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str());
Luke Huang0051a622018-07-23 20:30:16 +0800764 return statusFromErrcode(res);
765}
766
767binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName,
768 int32_t timeout,
769 const std::string& classLabel) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900770 NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang0051a622018-07-23 20:30:16 +0800771 int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout,
772 classLabel.c_str());
Luke Huang0051a622018-07-23 20:30:16 +0800773 return statusFromErrcode(res);
774}
Luke Huanga67dd562018-07-17 19:58:25 +0800775
776binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900777 NETD_LOCKING_RPC(gCtls->strictCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huanga67dd562018-07-17 19:58:25 +0800778 StrictPenalty penalty;
779 switch (policyPenalty) {
780 case INetd::PENALTY_POLICY_REJECT:
781 penalty = REJECT;
782 break;
783 case INetd::PENALTY_POLICY_LOG:
784 penalty = LOG;
785 break;
786 case INetd::PENALTY_POLICY_ACCEPT:
787 penalty = ACCEPT;
788 break;
789 default:
790 return statusFromErrcode(-EINVAL);
791 break;
792 }
793 int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty);
Luke Huanga67dd562018-07-17 19:58:25 +0800794 return statusFromErrcode(res);
795}
Luke Huange64fa382018-07-24 16:38:22 +0800796
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900797binder::Status NetdNativeService::clatdStart(const std::string& ifName,
798 const std::string& nat64Prefix, std::string* v6Addr) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900799 NETD_LOCKING_RPC(gCtls->clatdCtrl.mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900800 int res = gCtls->clatdCtrl.startClatd(ifName.c_str(), nat64Prefix, v6Addr);
Luke Huang6d301232018-08-01 14:05:18 +0800801 return statusFromErrcode(res);
802}
803
804binder::Status NetdNativeService::clatdStop(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900805 NETD_LOCKING_RPC(gCtls->clatdCtrl.mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang6d301232018-08-01 14:05:18 +0800806 int res = gCtls->clatdCtrl.stopClatd(ifName.c_str());
Luke Huang6d301232018-08-01 14:05:18 +0800807 return statusFromErrcode(res);
808}
Luke Huanga67dd562018-07-17 19:58:25 +0800809
Luke Huang457d4702018-08-16 15:39:15 +0800810binder::Status NetdNativeService::ipfwdEnabled(bool* status) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900811 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang728cf4c2019-03-14 19:43:02 +0800812 *status = (gCtls->tetherCtrl.getIpfwdRequesterList().size() > 0) ? true : false;
813 return binder::Status::ok();
814}
815
816binder::Status NetdNativeService::ipfwdGetRequesterList(std::vector<std::string>* requesterList) {
817 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
818 for (const auto& requester : gCtls->tetherCtrl.getIpfwdRequesterList()) {
819 requesterList->push_back(requester);
820 }
Luke Huang457d4702018-08-16 15:39:15 +0800821 return binder::Status::ok();
822}
823
824binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900825 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang457d4702018-08-16 15:39:15 +0800826 int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
Luke Huang457d4702018-08-16 15:39:15 +0800827 return statusFromErrcode(res);
828}
829
830binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900831 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang457d4702018-08-16 15:39:15 +0800832 int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
Luke Huang457d4702018-08-16 15:39:15 +0800833 return statusFromErrcode(res);
834}
835
836binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface,
837 const std::string& toIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900838 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang457d4702018-08-16 15:39:15 +0800839 int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str());
Luke Huang457d4702018-08-16 15:39:15 +0800840 return statusFromErrcode(res);
841}
842
843binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface,
844 const std::string& toIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900845 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huang457d4702018-08-16 15:39:15 +0800846 int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str());
Luke Huang457d4702018-08-16 15:39:15 +0800847 return statusFromErrcode(res);
848}
849
Luke Huangf7782042018-08-08 13:13:04 +0800850namespace {
851std::string addSquareBrackets(const std::string& s) {
852 return "[" + s + "]";
853}
854
855std::string addCurlyBrackets(const std::string& s) {
856 return "{" + s + "}";
857}
858
859} // namespace
Xiao Ma33d562a2018-12-16 16:27:38 +0900860
Luke Huangf7782042018-08-08 13:13:04 +0800861binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900862 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800863 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
864
865 const auto& ifaceList = InterfaceController::getIfaceNames();
866 RETURN_BINDER_STATUS_IF_NOT_OK(entry, ifaceList);
867
868 interfaceListResult->clear();
869 interfaceListResult->reserve(ifaceList.value().size());
870 interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()),
871 end(ifaceList.value()));
872
873 gLog.log(entry.returns(addSquareBrackets(base::Join(*interfaceListResult, ", ")))
874 .withAutomaticDuration());
875 return binder::Status::ok();
876}
877
878std::string interfaceConfigurationParcelToString(const InterfaceConfigurationParcel& cfg) {
879 std::vector<std::string> result{cfg.ifName, cfg.hwAddr, cfg.ipv4Addr,
880 std::to_string(cfg.prefixLength)};
881 result.insert(end(result), begin(cfg.flags), end(cfg.flags));
882 return addCurlyBrackets(base::Join(result, ", "));
883}
884
885binder::Status NetdNativeService::interfaceGetCfg(
886 const std::string& ifName, InterfaceConfigurationParcel* interfaceGetCfgResult) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900887 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800888 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
889
890 const auto& cfgRes = InterfaceController::getCfg(ifName);
891 RETURN_BINDER_STATUS_IF_NOT_OK(entry, cfgRes);
892
893 *interfaceGetCfgResult = cfgRes.value();
894 gLog.log(entry.returns(interfaceConfigurationParcelToString(*interfaceGetCfgResult))
895 .withAutomaticDuration());
896 return binder::Status::ok();
897}
898
899binder::Status NetdNativeService::interfaceSetCfg(const InterfaceConfigurationParcel& cfg) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900900 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800901 auto entry = gLog.newEntry()
902 .prettyFunction(__PRETTY_FUNCTION__)
903 .arg(interfaceConfigurationParcelToString(cfg));
904
905 const auto& res = InterfaceController::setCfg(cfg);
906 RETURN_BINDER_STATUS_IF_NOT_OK(entry, res);
907
908 gLog.log(entry.withAutomaticDuration());
909 return binder::Status::ok();
910}
911
912binder::Status NetdNativeService::interfaceSetIPv6PrivacyExtensions(const std::string& ifName,
913 bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900914 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800915 int res = InterfaceController::setIPv6PrivacyExtensions(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800916 return statusFromErrcode(res);
917}
918
919binder::Status NetdNativeService::interfaceClearAddrs(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900920 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800921 int res = InterfaceController::clearAddrs(ifName.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800922 return statusFromErrcode(res);
923}
924
925binder::Status NetdNativeService::interfaceSetEnableIPv6(const std::string& ifName, bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900926 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800927 int res = InterfaceController::setEnableIPv6(ifName.c_str(), enable);
Luke Huangf7782042018-08-08 13:13:04 +0800928 return statusFromErrcode(res);
929}
930
931binder::Status NetdNativeService::interfaceSetMtu(const std::string& ifName, int32_t mtuValue) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900932 NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangf7782042018-08-08 13:13:04 +0800933 std::string mtu = std::to_string(mtuValue);
934 int res = InterfaceController::setMtu(ifName.c_str(), mtu.c_str());
Luke Huangf7782042018-08-08 13:13:04 +0800935 return statusFromErrcode(res);
936}
937
Luke Huangb5733d72018-08-21 17:17:19 +0800938binder::Status NetdNativeService::tetherStart(const std::vector<std::string>& dhcpRanges) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900939 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800940 if (dhcpRanges.size() % 2 == 1) {
941 return statusFromErrcode(-EINVAL);
942 }
943 int res = gCtls->tetherCtrl.startTethering(dhcpRanges);
Luke Huangb5733d72018-08-21 17:17:19 +0800944 return statusFromErrcode(res);
945}
946
947binder::Status NetdNativeService::tetherStop() {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900948 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800949 int res = gCtls->tetherCtrl.stopTethering();
Luke Huangb5733d72018-08-21 17:17:19 +0800950 return statusFromErrcode(res);
951}
952
953binder::Status NetdNativeService::tetherIsEnabled(bool* enabled) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900954 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800955 *enabled = gCtls->tetherCtrl.isTetheringStarted();
Luke Huangb5733d72018-08-21 17:17:19 +0800956 return binder::Status::ok();
957}
958
959binder::Status NetdNativeService::tetherInterfaceAdd(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900960 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800961 int res = gCtls->tetherCtrl.tetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800962 return statusFromErrcode(res);
963}
964
965binder::Status NetdNativeService::tetherInterfaceRemove(const std::string& ifName) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900966 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800967 int res = gCtls->tetherCtrl.untetherInterface(ifName.c_str());
Luke Huangb5733d72018-08-21 17:17:19 +0800968 return statusFromErrcode(res);
969}
970
971binder::Status NetdNativeService::tetherInterfaceList(std::vector<std::string>* ifList) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900972 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800973 for (const auto& ifname : gCtls->tetherCtrl.getTetheredInterfaceList()) {
974 ifList->push_back(ifname);
975 }
Luke Huangb5733d72018-08-21 17:17:19 +0800976 return binder::Status::ok();
977}
978
979binder::Status NetdNativeService::tetherDnsSet(int32_t netId,
980 const std::vector<std::string>& dnsAddrs) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900981 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800982 int res = gCtls->tetherCtrl.setDnsForwarders(netId, dnsAddrs);
Luke Huangb5733d72018-08-21 17:17:19 +0800983 return statusFromErrcode(res);
984}
985
986binder::Status NetdNativeService::tetherDnsList(std::vector<std::string>* dnsList) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900987 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangb5733d72018-08-21 17:17:19 +0800988 for (const auto& fwdr : gCtls->tetherCtrl.getDnsForwarders()) {
989 dnsList->push_back(fwdr);
990 }
Luke Huangb5733d72018-08-21 17:17:19 +0800991 return binder::Status::ok();
992}
993
Luke Huangb670d162018-08-23 20:01:13 +0800994binder::Status NetdNativeService::networkAddRoute(int32_t netId, const std::string& ifName,
995 const std::string& destination,
996 const std::string& nextHop) {
997 // Public methods of NetworkController are thread-safe.
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +0900998 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +0800999 bool legacy = false;
1000 uid_t uid = 0; // UID is only meaningful for legacy routes.
1001 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1002 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
Luke Huangb670d162018-08-23 20:01:13 +08001003 return statusFromErrcode(res);
1004}
1005
1006binder::Status NetdNativeService::networkRemoveRoute(int32_t netId, const std::string& ifName,
1007 const std::string& destination,
1008 const std::string& nextHop) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001009 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001010 bool legacy = false;
1011 uid_t uid = 0; // UID is only meaningful for legacy routes.
1012 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1013 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
Luke Huangb670d162018-08-23 20:01:13 +08001014 return statusFromErrcode(res);
1015}
1016
1017binder::Status NetdNativeService::networkAddLegacyRoute(int32_t netId, const std::string& ifName,
1018 const std::string& destination,
1019 const std::string& nextHop, int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001020 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001021 bool legacy = true;
1022 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1023 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1024 (uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001025 return statusFromErrcode(res);
1026}
1027
1028binder::Status NetdNativeService::networkRemoveLegacyRoute(int32_t netId, const std::string& ifName,
1029 const std::string& destination,
1030 const std::string& nextHop,
1031 int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001032 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001033 bool legacy = true;
1034 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1035 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1036 (uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001037 return statusFromErrcode(res);
1038}
1039
1040binder::Status NetdNativeService::networkGetDefault(int32_t* netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001041 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001042 *netId = gCtls->netCtrl.getDefaultNetwork();
Luke Huangb670d162018-08-23 20:01:13 +08001043 return binder::Status::ok();
1044}
1045
1046binder::Status NetdNativeService::networkSetDefault(int32_t netId) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001047 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001048 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001049 return statusFromErrcode(res);
1050}
1051
1052binder::Status NetdNativeService::networkClearDefault() {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001053 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001054 unsigned netId = NETID_UNSET;
1055 int res = gCtls->netCtrl.setDefaultNetwork(netId);
Luke Huangb670d162018-08-23 20:01:13 +08001056 return statusFromErrcode(res);
1057}
1058
1059std::vector<uid_t> NetdNativeService::intsToUids(const std::vector<int32_t>& intUids) {
1060 return {begin(intUids), end(intUids)};
1061}
1062
1063Permission NetdNativeService::convertPermission(int32_t permission) {
1064 switch (permission) {
1065 case INetd::PERMISSION_NETWORK:
1066 return Permission::PERMISSION_NETWORK;
1067 case INetd::PERMISSION_SYSTEM:
1068 return Permission::PERMISSION_SYSTEM;
1069 default:
1070 return Permission::PERMISSION_NONE;
1071 }
1072}
1073
1074binder::Status NetdNativeService::networkSetPermissionForNetwork(int32_t netId,
1075 int32_t permission) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001076 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001077 std::vector<unsigned> netIds = {(unsigned) netId};
1078 int res = gCtls->netCtrl.setPermissionForNetworks(convertPermission(permission), netIds);
Luke Huangb670d162018-08-23 20:01:13 +08001079 return statusFromErrcode(res);
1080}
1081
1082binder::Status NetdNativeService::networkSetPermissionForUser(int32_t permission,
1083 const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001084 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001085 gCtls->netCtrl.setPermissionForUsers(convertPermission(permission), intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001086 return binder::Status::ok();
1087}
1088
1089binder::Status NetdNativeService::networkClearPermissionForUser(const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001090 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001091 Permission permission = Permission::PERMISSION_NONE;
1092 gCtls->netCtrl.setPermissionForUsers(permission, intsToUids(uids));
Luke Huangb670d162018-08-23 20:01:13 +08001093 return binder::Status::ok();
1094}
1095
1096binder::Status NetdNativeService::NetdNativeService::networkSetProtectAllow(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001097 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001098 std::vector<uid_t> uids = {(uid_t) uid};
1099 gCtls->netCtrl.allowProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001100 return binder::Status::ok();
1101}
1102
1103binder::Status NetdNativeService::networkSetProtectDeny(int32_t uid) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001104 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001105 std::vector<uid_t> uids = {(uid_t) uid};
1106 gCtls->netCtrl.denyProtect(uids);
Luke Huangb670d162018-08-23 20:01:13 +08001107 return binder::Status::ok();
1108}
1109
1110binder::Status NetdNativeService::networkCanProtect(int32_t uid, bool* ret) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001111 ENFORCE_NETWORK_STACK_PERMISSIONS();
Luke Huangb670d162018-08-23 20:01:13 +08001112 *ret = gCtls->netCtrl.canProtect((uid_t) uid);
Luke Huangb670d162018-08-23 20:01:13 +08001113 return binder::Status::ok();
1114}
1115
Chenbo Feng48eaed32018-12-26 17:40:21 -08001116binder::Status NetdNativeService::trafficSetNetPermForUids(int32_t permission,
1117 const std::vector<int32_t>& uids) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001118 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chenbo Feng48eaed32018-12-26 17:40:21 -08001119 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(permission).arg(uids);
1120 gCtls->trafficCtrl.setPermissionForUids(permission, intsToUids(uids));
1121 gLog.log(entry.withAutomaticDuration());
1122 return binder::Status::ok();
1123}
1124
Luke Huange64fa382018-07-24 16:38:22 +08001125binder::Status NetdNativeService::firewallSetFirewallType(int32_t firewallType) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001126 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001127 auto type = static_cast<FirewallType>(firewallType);
1128
1129 int res = gCtls->firewallCtrl.setFirewallType(type);
Luke Huange64fa382018-07-24 16:38:22 +08001130 return statusFromErrcode(res);
1131}
1132
1133binder::Status NetdNativeService::firewallSetInterfaceRule(const std::string& ifName,
1134 int32_t firewallRule) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001135 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001136 auto rule = static_cast<FirewallRule>(firewallRule);
1137
1138 int res = gCtls->firewallCtrl.setInterfaceRule(ifName.c_str(), rule);
Luke Huange64fa382018-07-24 16:38:22 +08001139 return statusFromErrcode(res);
1140}
1141
1142binder::Status NetdNativeService::firewallSetUidRule(int32_t childChain, int32_t uid,
1143 int32_t firewallRule) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001144 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001145 auto chain = static_cast<ChildChain>(childChain);
1146 auto rule = static_cast<FirewallRule>(firewallRule);
1147
1148 int res = gCtls->firewallCtrl.setUidRule(chain, uid, rule);
Luke Huange64fa382018-07-24 16:38:22 +08001149 return statusFromErrcode(res);
1150}
1151
1152binder::Status NetdNativeService::firewallEnableChildChain(int32_t childChain, bool enable) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001153 NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huange64fa382018-07-24 16:38:22 +08001154 auto chain = static_cast<ChildChain>(childChain);
1155
1156 int res = gCtls->firewallCtrl.enableChildChains(chain, enable);
Luke Huange64fa382018-07-24 16:38:22 +08001157 return statusFromErrcode(res);
1158}
1159
Luke Huang19b49c52018-10-22 12:12:05 +09001160binder::Status NetdNativeService::tetherAddForward(const std::string& intIface,
1161 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001162 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huang19b49c52018-10-22 12:12:05 +09001163
1164 int res = gCtls->tetherCtrl.enableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001165 return statusFromErrcode(res);
1166}
1167
1168binder::Status NetdNativeService::tetherRemoveForward(const std::string& intIface,
1169 const std::string& extIface) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001170 NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
Luke Huangae038f82018-11-05 11:17:31 +09001171 int res = gCtls->tetherCtrl.disableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001172 return statusFromErrcode(res);
1173}
1174
Chenbo Fengf5663d82018-11-08 16:10:48 -08001175binder::Status NetdNativeService::setTcpRWmemorySize(const std::string& rmemValues,
1176 const std::string& wmemValues) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001177 ENFORCE_NETWORK_STACK_PERMISSIONS();
Chenbo Fengf5663d82018-11-08 16:10:48 -08001178 if (!WriteStringToFile(rmemValues, TCP_RMEM_PROC_FILE)) {
1179 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001180 return statusFromErrcode(ret);
1181 }
1182
1183 if (!WriteStringToFile(wmemValues, TCP_WMEM_PROC_FILE)) {
1184 int ret = -errno;
Chenbo Fengf5663d82018-11-08 16:10:48 -08001185 return statusFromErrcode(ret);
1186 }
Chenbo Fengf5663d82018-11-08 16:10:48 -08001187 return binder::Status::ok();
1188}
1189
Luke Huang528af602018-08-29 19:06:05 +08001190binder::Status NetdNativeService::registerUnsolicitedEventListener(
1191 const android::sp<android::net::INetdUnsolicitedEventListener>& listener) {
Remi NGUYEN VANa686ebc2019-02-07 12:25:23 +09001192 ENFORCE_NETWORK_STACK_PERMISSIONS();
Bernie Innocenti9ee088d2019-02-01 17:14:32 +09001193 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1194 gCtls->eventReporter.registerUnsolEventListener(listener);
Luke Huang528af602018-08-29 19:06:05 +08001195 gLog.log(entry.withAutomaticDuration());
1196 return binder::Status::ok();
1197}
1198
Lorenzo Colittie4d626e2016-02-02 17:19:04 +09001199} // namespace net
1200} // namespace android