blob: ce2dd233d69d90699439876aa6a6d777d6503e7c [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>
Ben Schwartz4204ecf2017-10-02 12:35:48 -040020#include <set>
Erik Kline38e51f12018-09-06 20:14:44 +090021#include <tuple>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090022#include <vector>
23
Chenbo Fengf5663d82018-11-08 16:10:48 -080024#include <android-base/file.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090025#include <android-base/stringprintf.h>
Ben Schwartz4204ecf2017-10-02 12:35:48 -040026#include <android-base/strings.h>
Robin Lee2cf56172016-09-13 18:55:42 +090027#include <cutils/properties.h>
Logan Chien3f461482018-04-23 14:31:32 +080028#include <log/log.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090029#include <utils/Errors.h>
Pierre Imaibeedec32016-04-13 06:44:51 +090030#include <utils/String16.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090031
32#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
34#include "android/net/BnNetd.h"
35
Ben Schwartze7601812017-04-28 16:38:29 -040036#include <openssl/base64.h>
37
Lorenzo Colitti89faa342016-02-26 11:38:47 +090038#include "Controllers.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090039#include "DumpWriter.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"
Luke Huangb670d162018-08-23 20:01:13 +080043#include "Permission.h"
Erik Kline85890042018-05-25 19:19:11 +090044#include "Process.h"
Robin Leeb8087362016-03-30 18:43:08 +010045#include "RouteController.h"
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090046#include "SockDiag.h"
Robin Leeb8087362016-03-30 18:43:08 +010047#include "UidRanges.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090048#include "netid_client.h" // NETID_UNSET
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090049
50using android::base::StringPrintf;
Chenbo Fengf5663d82018-11-08 16:10:48 -080051using android::base::WriteStringToFile;
Luke Huangcaebcbb2018-09-27 20:37:14 +080052using android::net::TetherStatsParcel;
Luke Huang94658ac2018-10-18 19:35:12 +090053using android::net::UidRangeParcel;
Luke Huange203a152018-11-23 11:47:28 +080054using android::os::ParcelFileDescriptor;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090055
56namespace android {
57namespace net {
58
59namespace {
60
61const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090062const char NETWORK_STACK[] = "android.permission.NETWORK_STACK";
Erik Kline2d3a1632016-03-15 16:33:48 +090063const char DUMP[] = "android.permission.DUMP";
Erik Klineb31fd692018-06-06 20:50:11 +090064const char OPT_SHORT[] = "--short";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090065
66binder::Status checkPermission(const char *permission) {
Luke Huanga38b65c2018-09-26 16:31:03 +080067 pid_t pid = IPCThreadState::self()->getCallingPid();
68 uid_t uid = IPCThreadState::self()->getCallingUid();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090069
Luke Huanga38b65c2018-09-26 16:31:03 +080070 // If the caller is the system UID, don't check permissions.
71 // Otherwise, if the system server's binder thread pool is full, and all the threads are
72 // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492
73 //
74 // From a security perspective, there is currently no difference, because:
75 // 1. The only permissions we check in netd's binder interface are CONNECTIVITY_INTERNAL
76 // and NETWORK_STACK, which the system server will always need to have.
77 // 2. AID_SYSTEM always has all permissions. See ActivityManager#checkComponentPermission.
78 if (uid == AID_SYSTEM || checkPermission(String16(permission), pid, uid)) {
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090079 return binder::Status::ok();
80 } else {
81 auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
82 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
83 }
84}
85
Robin Lee2cf56172016-09-13 18:55:42 +090086#define ENFORCE_DEBUGGABLE() { \
87 char value[PROPERTY_VALUE_MAX + 1]; \
Yi Kongbdfd57e2018-07-25 13:26:10 -070088 if (property_get("ro.debuggable", value, nullptr) != 1 \
Robin Lee2cf56172016-09-13 18:55:42 +090089 || value[0] != '1') { \
90 return binder::Status::fromExceptionCode( \
91 binder::Status::EX_SECURITY, \
92 String8("Not available in production builds.") \
93 ); \
94 } \
95}
96
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090097#define ENFORCE_PERMISSION(permission) { \
98 binder::Status status = checkPermission((permission)); \
99 if (!status.isOk()) { \
100 return status; \
101 } \
102}
103
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900104#define NETD_LOCKING_RPC(permission, lock) \
105 ENFORCE_PERMISSION(permission); \
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900106 std::lock_guard _lock(lock);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900107
108#define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900109
Luke Huangf7782042018-08-08 13:13:04 +0800110#define RETURN_BINDER_STATUS_IF_NOT_OK(logEntry, res) \
111 do { \
112 if (!isOk((res))) { \
113 logErrorStatus((logEntry), (res)); \
114 return asBinderStatus((res)); \
115 } \
116 } while (0)
117
118void logErrorStatus(netdutils::LogEntry& logEntry, const netdutils::Status& status) {
119 gLog.log(logEntry.returns(status.code()).withAutomaticDuration());
120}
121
Bernie Innocenti97f388f2018-10-16 19:17:08 +0900122binder::Status asBinderStatus(const netdutils::Status& status) {
123 if (isOk(status)) {
124 return binder::Status::ok();
125 }
126 return binder::Status::fromServiceSpecificError(status.code(), status.msg().c_str());
127}
128
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900129inline binder::Status statusFromErrcode(int ret) {
130 if (ret) {
131 return binder::Status::fromServiceSpecificError(-ret, strerror(-ret));
132 }
133 return binder::Status::ok();
134}
135
Erik Klineb31fd692018-06-06 20:50:11 +0900136bool contains(const Vector<String16>& words, const String16& word) {
137 for (const auto& w : words) {
138 if (w == word) return true;
139 }
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900140
Erik Klineb31fd692018-06-06 20:50:11 +0900141 return false;
142}
143
144} // namespace
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900145
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900146status_t NetdNativeService::start() {
147 IPCThreadState::self()->disableBackgroundScheduling(true);
Erik Klineb31fd692018-06-06 20:50:11 +0900148 const status_t ret = BinderService<NetdNativeService>::publish();
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900149 if (ret != android::OK) {
150 return ret;
151 }
152 sp<ProcessState> ps(ProcessState::self());
153 ps->startThreadPool();
154 ps->giveThreadPoolName();
155 return android::OK;
156}
157
Hugo Benichi7b314e12018-01-15 21:54:00 +0900158status_t NetdNativeService::dump(int fd, const Vector<String16> &args) {
Erik Kline2d3a1632016-03-15 16:33:48 +0900159 const binder::Status dump_permission = checkPermission(DUMP);
160 if (!dump_permission.isOk()) {
161 const String8 msg(dump_permission.toString8());
162 write(fd, msg.string(), msg.size());
163 return PERMISSION_DENIED;
164 }
165
166 // This method does not grab any locks. If individual classes need locking
167 // their dump() methods MUST handle locking appropriately.
Hugo Benichi7b314e12018-01-15 21:54:00 +0900168
Erik Kline2d3a1632016-03-15 16:33:48 +0900169 DumpWriter dw(fd);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900170
171 if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) {
172 dw.blankline();
173 gCtls->tcpSocketMonitor.dump(dw);
174 dw.blankline();
175 return NO_ERROR;
176 }
177
Chenbo Fengef297172018-03-26 10:53:33 -0700178 if (!args.isEmpty() && args[0] == TrafficController::DUMP_KEYWORD) {
179 dw.blankline();
180 gCtls->trafficCtrl.dump(dw, true);
181 dw.blankline();
182 return NO_ERROR;
183 }
184
Erik Kline85890042018-05-25 19:19:11 +0900185 process::dump(dw);
Erik Kline2d3a1632016-03-15 16:33:48 +0900186 dw.blankline();
187 gCtls->netCtrl.dump(dw);
188 dw.blankline();
189
Chenbo Fengef297172018-03-26 10:53:33 -0700190 gCtls->trafficCtrl.dump(dw, false);
191 dw.blankline();
192
Benedict Wongaf855432018-05-10 17:07:37 -0700193 gCtls->xfrmCtrl.dump(dw);
194 dw.blankline();
195
Erik Klineb31fd692018-06-06 20:50:11 +0900196 {
197 ScopedIndent indentLog(dw);
198 if (contains(args, String16(OPT_SHORT))) {
199 dw.println("Log: <omitted>");
200 } else {
201 dw.println("Log:");
202 ScopedIndent indentLogEntries(dw);
203 gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); });
204 }
205 dw.blankline();
206 }
207
Luke Huang528af602018-08-29 19:06:05 +0800208 {
209 ScopedIndent indentLog(dw);
210 if (contains(args, String16(OPT_SHORT))) {
211 dw.println("UnsolicitedLog: <omitted>");
212 } else {
213 dw.println("UnsolicitedLog:");
214 ScopedIndent indentLogEntries(dw);
215 gUnsolicitedLog.forEachEntry(
216 [&dw](const std::string& entry) mutable { dw.println(entry); });
217 }
218 dw.blankline();
219 }
220
Erik Kline2d3a1632016-03-15 16:33:48 +0900221 return NO_ERROR;
222}
223
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900224binder::Status NetdNativeService::isAlive(bool *alive) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900225 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900226 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900227
228 *alive = true;
Erik Klineb31fd692018-06-06 20:50:11 +0900229
230 gLog.log(entry.returns(*alive));
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900231 return binder::Status::ok();
232}
233
Erik Klinef52d4522018-03-14 15:01:46 +0900234binder::Status NetdNativeService::firewallReplaceUidChain(const std::string& chainName,
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900235 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
236 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock);
Erik Klineb31fd692018-06-06 20:50:11 +0900237 auto entry = gLog.newEntry()
238 .prettyFunction(__PRETTY_FUNCTION__)
239 .arg(chainName)
240 .arg(isWhitelist)
241 .arg(uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900242
Erik Klinef52d4522018-03-14 15:01:46 +0900243 int err = gCtls->firewallCtrl.replaceUidChain(chainName, isWhitelist, uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900244 *ret = (err == 0);
Erik Klineb31fd692018-06-06 20:50:11 +0900245
246 gLog.log(entry.returns(*ret).withAutomaticDuration());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900247 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900248}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900249
250binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
251 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->bandwidthCtrl.lock);
Erik Klineb31fd692018-06-06 20:50:11 +0900252 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(enable);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900253
254 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
255 *ret = (err == 0);
Erik Klineb31fd692018-06-06 20:50:11 +0900256 gLog.log(entry.returns(*ret).withAutomaticDuration());
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900257 return binder::Status::ok();
258}
259
Luke Huang531f5d32018-08-03 15:19:05 +0800260binder::Status NetdNativeService::bandwidthSetInterfaceQuota(const std::string& ifName,
261 int64_t bytes) {
262 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
263 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName).arg(bytes);
264
265 int res = gCtls->bandwidthCtrl.setInterfaceQuota(ifName, bytes);
266
267 gLog.log(entry.returns(res).withAutomaticDuration());
268 return statusFromErrcode(res);
269}
270
271binder::Status NetdNativeService::bandwidthRemoveInterfaceQuota(const std::string& ifName) {
272 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
273 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
274
275 int res = gCtls->bandwidthCtrl.removeInterfaceQuota(ifName);
276
277 gLog.log(entry.returns(res).withAutomaticDuration());
278 return statusFromErrcode(res);
279}
280
281binder::Status NetdNativeService::bandwidthSetInterfaceAlert(const std::string& ifName,
282 int64_t bytes) {
283 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
284 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName).arg(bytes);
285
286 int res = gCtls->bandwidthCtrl.setInterfaceAlert(ifName, bytes);
287
288 gLog.log(entry.returns(res).withAutomaticDuration());
289 return statusFromErrcode(res);
290}
291
292binder::Status NetdNativeService::bandwidthRemoveInterfaceAlert(const std::string& ifName) {
293 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
294 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
295
296 int res = gCtls->bandwidthCtrl.removeInterfaceAlert(ifName);
297
298 gLog.log(entry.returns(res).withAutomaticDuration());
299 return statusFromErrcode(res);
300}
301
302binder::Status NetdNativeService::bandwidthSetGlobalAlert(int64_t bytes) {
303 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
304 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(bytes);
305
306 int res = gCtls->bandwidthCtrl.setGlobalAlert(bytes);
307
308 gLog.log(entry.returns(res).withAutomaticDuration());
309 return statusFromErrcode(res);
310}
311
312binder::Status NetdNativeService::bandwidthAddNaughtyApp(int32_t uid) {
313 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
314 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
315
316 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
317 int res = gCtls->bandwidthCtrl.addNaughtyApps(appStrUids);
318
319 gLog.log(entry.returns(res).withAutomaticDuration());
320 return statusFromErrcode(res);
321}
322
323binder::Status NetdNativeService::bandwidthRemoveNaughtyApp(int32_t uid) {
324 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
325 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
326
327 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
328 int res = gCtls->bandwidthCtrl.removeNaughtyApps(appStrUids);
329
330 gLog.log(entry.returns(res).withAutomaticDuration());
331 return statusFromErrcode(res);
332}
333
334binder::Status NetdNativeService::bandwidthAddNiceApp(int32_t uid) {
335 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
336 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
337
338 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
339 int res = gCtls->bandwidthCtrl.addNiceApps(appStrUids);
340
341 gLog.log(entry.returns(res).withAutomaticDuration());
342 return statusFromErrcode(res);
343}
344
345binder::Status NetdNativeService::bandwidthRemoveNiceApp(int32_t uid) {
346 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
347 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
348
349 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
350 int res = gCtls->bandwidthCtrl.removeNiceApps(appStrUids);
351
352 gLog.log(entry.returns(res).withAutomaticDuration());
353 return statusFromErrcode(res);
354}
355
Luke Huangb670d162018-08-23 20:01:13 +0800356binder::Status NetdNativeService::networkCreatePhysical(int32_t netId, int32_t permission) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900357 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900358 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(permission);
Luke Huangb670d162018-08-23 20:01:13 +0800359 int ret = gCtls->netCtrl.createPhysicalNetwork(netId, convertPermission(permission));
Erik Klineb31fd692018-06-06 20:50:11 +0900360 gLog.log(entry.returns(ret).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900361 return statusFromErrcode(ret);
362}
363
cken67cd14c2018-12-05 17:26:59 +0900364binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool secure) {
Ken Chen87f1ec92018-12-18 20:15:28 +0800365 ENFORCE_PERMISSION(NETWORK_STACK);
cken67cd14c2018-12-05 17:26:59 +0900366 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(netId, secure);
367 int ret = gCtls->netCtrl.createVirtualNetwork(netId, secure);
368 gLog.log(entry.returns(ret).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900369 return statusFromErrcode(ret);
370}
371
372binder::Status NetdNativeService::networkDestroy(int32_t netId) {
Erik Klinec8b6a9c2018-01-15 17:06:48 +0900373 ENFORCE_PERMISSION(NETWORK_STACK);
374 // Both of these functions manage their own locking internally.
375 const int ret = gCtls->netCtrl.destroyNetwork(netId);
376 gCtls->resolverCtrl.clearDnsServers(netId);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900377 return statusFromErrcode(ret);
378}
379
380binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
381 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
382 int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
383 return statusFromErrcode(ret);
384}
385
386binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
387 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
388 int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
389 return statusFromErrcode(ret);
390}
391
Luke Huang94658ac2018-10-18 19:35:12 +0900392namespace {
393
394std::string uidRangeParcelVecToString(const std::vector<UidRangeParcel>& uidRangeArray) {
395 std::vector<std::string> result;
396 result.reserve(uidRangeArray.size());
397 for (const auto& uidRange : uidRangeArray) {
398 result.push_back(StringPrintf("%d-%d", uidRange.start, uidRange.stop));
399 }
400
401 return base::Join(result, ", ");
402}
403
404} // namespace
405
406binder::Status NetdNativeService::networkAddUidRanges(
407 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900408 // NetworkController::addUsersToNetwork is thread-safe.
409 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Luke Huang94658ac2018-10-18 19:35:12 +0900410 auto entry = gLog.newEntry()
411 .prettyFunction(__PRETTY_FUNCTION__)
412 .args(netId, uidRangeParcelVecToString(uidRangeArray));
413
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900414 int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray));
Luke Huang94658ac2018-10-18 19:35:12 +0900415 gLog.log(entry.returns(ret).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900416 return statusFromErrcode(ret);
417}
418
Luke Huang94658ac2018-10-18 19:35:12 +0900419binder::Status NetdNativeService::networkRemoveUidRanges(
420 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900421 // NetworkController::removeUsersFromNetwork is thread-safe.
422 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Luke Huang94658ac2018-10-18 19:35:12 +0900423 auto entry = gLog.newEntry()
424 .prettyFunction(__PRETTY_FUNCTION__)
425 .args(netId, uidRangeParcelVecToString(uidRangeArray));
426
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900427 int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray));
Luke Huang94658ac2018-10-18 19:35:12 +0900428 gLog.log(entry.returns(ret).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900429 return statusFromErrcode(ret);
430}
431
Luke Huang94658ac2018-10-18 19:35:12 +0900432binder::Status NetdNativeService::networkRejectNonSecureVpn(
433 bool add, const std::vector<UidRangeParcel>& uidRangeArray) {
Robin Leeb8087362016-03-30 18:43:08 +0100434 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
435 // it should be possible to use the same lock as NetworkController. However, every call through
436 // the CommandListener "network" command will need to hold this lock too, not just the ones that
437 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
438 // look at routes, but it's not enough here).
439 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
Luke Huang94658ac2018-10-18 19:35:12 +0900440 auto entry = gLog.newEntry()
441 .prettyFunction(__PRETTY_FUNCTION__)
442 .args(add, uidRangeParcelVecToString(uidRangeArray));
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900443 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100444
445 int err;
446 if (add) {
447 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
448 } else {
449 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
450 }
Luke Huang94658ac2018-10-18 19:35:12 +0900451 gLog.log(entry.returns(err).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900452 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100453}
454
Luke Huang94658ac2018-10-18 19:35:12 +0900455binder::Status NetdNativeService::socketDestroy(const std::vector<UidRangeParcel>& uids,
456 const std::vector<int32_t>& skipUids) {
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900457 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
458
Luke Huang94658ac2018-10-18 19:35:12 +0900459 auto entry = gLog.newEntry()
460 .prettyFunction(__PRETTY_FUNCTION__)
461 .arg(uidRangeParcelVecToString(uids));
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900462 SockDiag sd;
463 if (!sd.open()) {
464 return binder::Status::fromServiceSpecificError(EIO,
465 String8("Could not open SOCK_DIAG socket"));
466 }
467
468 UidRanges uidRanges(uids);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +0900469 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
470 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900471
Luke Huang94658ac2018-10-18 19:35:12 +0900472 gLog.log(entry.returns(err).withAutomaticDuration());
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900473 if (err) {
474 return binder::Status::fromServiceSpecificError(-err,
475 String8::format("destroySockets: %s", strerror(-err)));
476 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900477 return binder::Status::ok();
478}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900479
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400480// Parse a base64 encoded string into a vector of bytes.
481// On failure, return an empty vector.
482static std::vector<uint8_t> parseBase64(const std::string& input) {
483 std::vector<uint8_t> decoded;
484 size_t out_len;
485 if (EVP_DecodedLength(&out_len, input.size()) != 1) {
486 return decoded;
487 }
488 // out_len is now an upper bound on the output length.
489 decoded.resize(out_len);
490 if (EVP_DecodeBase64(decoded.data(), &out_len, decoded.size(),
491 reinterpret_cast<const uint8_t*>(input.data()), input.size()) == 1) {
492 // Possibly shrink the vector if the actual output was smaller than the bound.
493 decoded.resize(out_len);
494 } else {
495 decoded.clear();
496 }
497 if (out_len != SHA256_SIZE) {
498 decoded.clear();
499 }
500 return decoded;
501}
502
Pierre Imaibeedec32016-04-13 06:44:51 +0900503binder::Status NetdNativeService::setResolverConfiguration(int32_t netId,
504 const std::vector<std::string>& servers, const std::vector<std::string>& domains,
Erik Klinea1476fb2018-03-04 21:01:56 +0900505 const std::vector<int32_t>& params, const std::string& tlsName,
506 const std::vector<std::string>& tlsServers,
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400507 const std::vector<std::string>& tlsFingerprints) {
Pierre Imaibeedec32016-04-13 06:44:51 +0900508 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
509 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900510 auto entry = gLog.newEntry()
511 .prettyFunction(__PRETTY_FUNCTION__)
512 .arg(netId)
513 .arg(servers)
514 .arg(domains)
515 .arg(params)
516 .arg(tlsName)
517 .arg(tlsServers)
518 .arg(tlsFingerprints);
Pierre Imaibeedec32016-04-13 06:44:51 +0900519
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400520 std::set<std::vector<uint8_t>> decoded_fingerprints;
521 for (const std::string& fingerprint : tlsFingerprints) {
522 std::vector<uint8_t> decoded = parseBase64(fingerprint);
523 if (decoded.empty()) {
524 return binder::Status::fromServiceSpecificError(EINVAL,
525 String8::format("ResolverController error: bad fingerprint"));
526 }
527 decoded_fingerprints.emplace(decoded);
528 }
529
530 int err = gCtls->resolverCtrl.setResolverConfiguration(netId, servers, domains, params,
Erik Klinea1476fb2018-03-04 21:01:56 +0900531 tlsName, tlsServers, decoded_fingerprints);
Erik Klineb31fd692018-06-06 20:50:11 +0900532 gLog.log(entry.returns(err).withAutomaticDuration());
Pierre Imaibeedec32016-04-13 06:44:51 +0900533 if (err != 0) {
534 return binder::Status::fromServiceSpecificError(-err,
535 String8::format("ResolverController error: %s", strerror(-err)));
536 }
537 return binder::Status::ok();
538}
539
Mike Yuda77e8e2018-11-26 13:26:21 +0900540binder::Status NetdNativeService::getResolverInfo(int32_t netId, std::vector<std::string>* servers,
541 std::vector<std::string>* domains,
542 std::vector<std::string>* tlsServers,
543 std::vector<int32_t>* params,
544 std::vector<int32_t>* stats) {
Pierre Imaibeedec32016-04-13 06:44:51 +0900545 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
Mike Yuda77e8e2018-11-26 13:26:21 +0900546 ENFORCE_PERMISSION(NETWORK_STACK);
Pierre Imaibeedec32016-04-13 06:44:51 +0900547
Mike Yuda77e8e2018-11-26 13:26:21 +0900548 int err =
549 gCtls->resolverCtrl.getResolverInfo(netId, servers, domains, tlsServers, params, stats);
Pierre Imaibeedec32016-04-13 06:44:51 +0900550 if (err != 0) {
551 return binder::Status::fromServiceSpecificError(-err,
552 String8::format("ResolverController error: %s", strerror(-err)));
553 }
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900554 return binder::Status::ok();
555}
556
Erik Klinef48e4dd2016-07-18 04:02:07 +0900557binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800558 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
Erik Klinef48e4dd2016-07-18 04:02:07 +0900559
560 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
561 return binder::Status::ok();
562}
563
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900564namespace {
565
Luke Huangcaebcbb2018-09-27 20:37:14 +0800566void tetherAddStatsByInterface(TetherController::TetherStats* tetherStatsParcel,
567 const TetherController::TetherStats& tetherStats) {
568 if (tetherStatsParcel->extIface == tetherStats.extIface) {
569 tetherStatsParcel->rxBytes += tetherStats.rxBytes;
570 tetherStatsParcel->rxPackets += tetherStats.rxPackets;
571 tetherStatsParcel->txBytes += tetherStats.txBytes;
572 tetherStatsParcel->txPackets += tetherStats.txPackets;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900573 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800574}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900575
Luke Huangcaebcbb2018-09-27 20:37:14 +0800576TetherStatsParcel toTetherStatsParcel(const TetherController::TetherStats& stats) {
577 TetherStatsParcel result;
578 result.iface = stats.extIface;
579 result.rxBytes = stats.rxBytes;
580 result.rxPackets = stats.rxPackets;
581 result.txBytes = stats.txBytes;
582 result.txPackets = stats.txPackets;
583 return result;
584}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900585
Luke Huangcaebcbb2018-09-27 20:37:14 +0800586void setTetherStatsParcelVecByInterface(std::vector<TetherStatsParcel>* tetherStatsVec,
587 const TetherController::TetherStatsList& statsList) {
588 std::map<std::string, TetherController::TetherStats> statsMap;
589 for (const auto& stats : statsList) {
590 auto iter = statsMap.find(stats.extIface);
591 if (iter != statsMap.end()) {
592 tetherAddStatsByInterface(&(iter->second), stats);
593 } else {
594 statsMap.insert(
595 std::pair<std::string, TetherController::TetherStats>(stats.extIface, stats));
596 }
597 }
598 for (auto iter = statsMap.begin(); iter != statsMap.end(); iter++) {
599 tetherStatsVec->push_back(toTetherStatsParcel(iter->second));
600 }
601}
602
603std::vector<std::string> tetherStatsParcelVecToStringVec(std::vector<TetherStatsParcel>* tVec) {
604 std::vector<std::string> result;
605 for (const auto& t : *tVec) {
606 result.push_back(StringPrintf("%s:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64,
607 t.iface.c_str(), t.rxBytes, t.rxPackets, t.txBytes,
608 t.txPackets));
609 }
610 return result;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900611}
612
613} // namespace
614
Luke Huangcaebcbb2018-09-27 20:37:14 +0800615binder::Status NetdNativeService::tetherGetStats(
616 std::vector<TetherStatsParcel>* tetherStatsParcelVec) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800617 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900618
Luke Huangcaebcbb2018-09-27 20:37:14 +0800619 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
620
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900621 const auto& statsList = gCtls->tetherCtrl.getTetherStats();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900622 if (!isOk(statsList)) {
Nathan Harold28ccfef2018-03-16 19:02:47 -0700623 return asBinderStatus(statsList);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900624 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800625 setTetherStatsParcelVecByInterface(tetherStatsParcelVec, statsList.value());
626 auto statsResults = tetherStatsParcelVecToStringVec(tetherStatsParcelVec);
627 gLog.log(entry.returns(base::Join(statsResults, ";")).withAutomaticDuration());
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900628 return binder::Status::ok();
629}
630
Erik Kline53c20882016-08-02 15:22:53 +0900631binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
632 const std::string &addrString, int prefixLength) {
633 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
634
635 const int err = InterfaceController::addAddress(
636 ifName.c_str(), addrString.c_str(), prefixLength);
637 if (err != 0) {
638 return binder::Status::fromServiceSpecificError(-err,
639 String8::format("InterfaceController error: %s", strerror(-err)));
640 }
641 return binder::Status::ok();
642}
643
644binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
645 const std::string &addrString, int prefixLength) {
646 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
647
648 const int err = InterfaceController::delAddress(
649 ifName.c_str(), addrString.c_str(), prefixLength);
650 if (err != 0) {
651 return binder::Status::fromServiceSpecificError(-err,
652 String8::format("InterfaceController error: %s", strerror(-err)));
653 }
654 return binder::Status::ok();
655}
656
Erik Kline38e51f12018-09-06 20:14:44 +0900657namespace {
Erik Kline55b06f82016-07-04 09:57:18 +0900658
Erik Kline38e51f12018-09-06 20:14:44 +0900659std::tuple<binder::Status, const char*, const char*> getPathComponents(int32_t ipversion,
660 int32_t category) {
661 const char* ipversionStr = nullptr;
662 switch (ipversion) {
Erik Kline55b06f82016-07-04 09:57:18 +0900663 case INetd::IPV4:
Erik Kline38e51f12018-09-06 20:14:44 +0900664 ipversionStr = "ipv4";
Erik Kline55b06f82016-07-04 09:57:18 +0900665 break;
666 case INetd::IPV6:
Erik Kline38e51f12018-09-06 20:14:44 +0900667 ipversionStr = "ipv6";
Erik Kline55b06f82016-07-04 09:57:18 +0900668 break;
669 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900670 return {binder::Status::fromServiceSpecificError(EAFNOSUPPORT, "Bad IP version"),
671 nullptr, nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900672 }
673
Erik Kline38e51f12018-09-06 20:14:44 +0900674 const char* whichStr = nullptr;
675 switch (category) {
Erik Kline55b06f82016-07-04 09:57:18 +0900676 case INetd::CONF:
677 whichStr = "conf";
678 break;
679 case INetd::NEIGH:
680 whichStr = "neigh";
681 break;
682 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900683 return {binder::Status::fromServiceSpecificError(EINVAL, "Bad category"), nullptr,
684 nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900685 }
686
Erik Kline38e51f12018-09-06 20:14:44 +0900687 return {binder::Status::ok(), ipversionStr, whichStr};
688}
689
690} // namespace
691
692binder::Status NetdNativeService::getProcSysNet(int32_t ipversion, int32_t which,
693 const std::string& ifname,
694 const std::string& parameter, std::string* value) {
695 ENFORCE_PERMISSION(NETWORK_STACK);
696 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__)
697 .args(ipversion, which, ifname, parameter);
698
699 const auto pathParts = getPathComponents(ipversion, which);
700 const auto& pathStatus = std::get<0>(pathParts);
701 if (!pathStatus.isOk()) {
702 gLog.log(entry.returns(pathStatus.exceptionCode()).withAutomaticDuration());
703 return pathStatus;
Erik Kline55b06f82016-07-04 09:57:18 +0900704 }
Erik Kline38e51f12018-09-06 20:14:44 +0900705
706 const int err = InterfaceController::getParameter(std::get<1>(pathParts),
707 std::get<2>(pathParts), ifname.c_str(),
708 parameter.c_str(), value);
709 entry.returns(err);
710 if (err == 0) entry.returns(*value);
711 gLog.log(entry.withAutomaticDuration());
712 return statusFromErrcode(err);
713}
714
715binder::Status NetdNativeService::setProcSysNet(int32_t ipversion, int32_t which,
716 const std::string& ifname,
717 const std::string& parameter,
718 const std::string& value) {
719 ENFORCE_PERMISSION(NETWORK_STACK);
720 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__)
721 .args(ipversion, which, ifname, parameter, value);
722
723 const auto pathParts = getPathComponents(ipversion, which);
724 const auto& pathStatus = std::get<0>(pathParts);
725 if (!pathStatus.isOk()) {
726 gLog.log(entry.returns(pathStatus.exceptionCode()).withAutomaticDuration());
727 return pathStatus;
728 }
729
730 const int err = InterfaceController::setParameter(std::get<1>(pathParts),
731 std::get<2>(pathParts), ifname.c_str(),
732 parameter.c_str(), value.c_str());
733 gLog.log(entry.returns(err).withAutomaticDuration());
734 return statusFromErrcode(err);
Erik Kline55b06f82016-07-04 09:57:18 +0900735}
736
Luke Huange203a152018-11-23 11:47:28 +0800737binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const ParcelFileDescriptor& socket,
738 int newUid) {
Benedict Wongb2daefb2017-12-06 22:05:46 -0800739 ENFORCE_PERMISSION(NETWORK_STACK)
Erik Klineb31fd692018-06-06 20:50:11 +0900740 gLog.log("ipSecSetEncapSocketOwner()");
Benedict Wongb2daefb2017-12-06 22:05:46 -0800741
742 uid_t callerUid = IPCThreadState::self()->getCallingUid();
Luke Huange203a152018-11-23 11:47:28 +0800743 return asBinderStatus(
744 gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket.get(), newUid, callerUid));
Benedict Wongb2daefb2017-12-06 22:05:46 -0800745}
746
Nathan Harold1a371532017-01-30 12:30:48 -0800747binder::Status NetdNativeService::ipSecAllocateSpi(
748 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800749 const std::string& sourceAddress,
750 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800751 int32_t inSpi,
752 int32_t* outSpi) {
753 // Necessary locking done in IpSecService and kernel
754 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900755 gLog.log("ipSecAllocateSpi()");
ludi6e8eccd2017-08-14 14:40:37 -0700756 return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi(
Nathan Harold1a371532017-01-30 12:30:48 -0800757 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800758 sourceAddress,
759 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800760 inSpi,
761 outSpi));
762}
763
764binder::Status NetdNativeService::ipSecAddSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700765 int32_t transformId, int32_t mode, const std::string& sourceAddress,
766 const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi,
767 int32_t markValue, int32_t markMask, const std::string& authAlgo,
768 const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo,
769 const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo,
770 const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType,
771 int32_t encapLocalPort, int32_t encapRemotePort, int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800772 // Necessary locking done in IpSecService and kernel
773 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900774 gLog.log("ipSecAddSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700775 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation(
Benedict Wongad600cb2018-05-14 17:22:35 -0700776 transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue,
777 markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits,
Benedict Wonga450e722018-05-07 10:29:02 -0700778 aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort,
779 interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800780}
781
782binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700783 int32_t transformId, const std::string& sourceAddress,
784 const std::string& destinationAddress, int32_t spi, int32_t markValue, int32_t markMask,
785 int32_t interfaceId) {
Nathan Harold1a371532017-01-30 12:30:48 -0800786 // Necessary locking done in IpSecService and kernel
787 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900788 gLog.log("ipSecDeleteSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700789 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
Benedict Wonga450e722018-05-07 10:29:02 -0700790 transformId, sourceAddress, destinationAddress, spi, markValue, markMask, interfaceId));
Nathan Harold1a371532017-01-30 12:30:48 -0800791}
792
793binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800794 const ParcelFileDescriptor& socket, int32_t transformId, int32_t direction,
795 const std::string& sourceAddress, const std::string& destinationAddress, int32_t spi) {
Nathan Harold1a371532017-01-30 12:30:48 -0800796 // Necessary locking done in IpSecService and kernel
797 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900798 gLog.log("ipSecApplyTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700799 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800800 socket.get(), transformId, direction, sourceAddress, destinationAddress, spi));
Nathan Harold1a371532017-01-30 12:30:48 -0800801}
802
803binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
Luke Huange203a152018-11-23 11:47:28 +0800804 const ParcelFileDescriptor& socket) {
Nathan Harold1a371532017-01-30 12:30:48 -0800805 // Necessary locking done in IpSecService and kernel
806 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900807 gLog.log("ipSecRemoveTransportModeTransform()");
Luke Huange203a152018-11-23 11:47:28 +0800808 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(socket.get()));
Nathan Harold1a371532017-01-30 12:30:48 -0800809}
810
Benedict Wonga04ffa72018-05-09 21:42:42 -0700811binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
812 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700813 const std::string& tmplSrcAddress,
814 const std::string& tmplDstAddress,
815 int32_t spi, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700816 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800817 // Necessary locking done in IpSecService and kernel
818 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900819 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800820 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700821 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700822 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800823}
824
Benedict Wonga450e722018-05-07 10:29:02 -0700825binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(
826 int32_t transformId, int32_t selAddrFamily, int32_t direction,
827 const std::string& tmplSrcAddress, const std::string& tmplDstAddress, int32_t spi,
828 int32_t markValue, int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800829 // Necessary locking done in IpSecService and kernel
830 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900831 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800832 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700833 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700834 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800835}
836
Benedict Wonga04ffa72018-05-09 21:42:42 -0700837binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId,
838 int32_t selAddrFamily,
839 int32_t direction, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700840 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800841 // Necessary locking done in IpSecService and kernel
842 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900843 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800844 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
Benedict Wonga450e722018-05-07 10:29:02 -0700845 transformId, selAddrFamily, direction, markValue, markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800846}
847
Benedict Wong319f17e2018-05-15 17:06:44 -0700848binder::Status NetdNativeService::ipSecAddTunnelInterface(const std::string& deviceName,
849 const std::string& localAddress,
850 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700851 int32_t iKey, int32_t oKey,
852 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800853 // Necessary locking done in IpSecService and kernel
854 ENFORCE_PERMISSION(NETWORK_STACK);
Benedict Wong319f17e2018-05-15 17:06:44 -0700855 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
manojboopathi8707f232018-01-02 14:45:47 -0800856
Benedict Wong319f17e2018-05-15 17:06:44 -0700857 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700858 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, false);
Benedict Wong319f17e2018-05-15 17:06:44 -0700859 RETURN_BINDER_STATUS_IF_NOT_OK(entry, result);
860
861 gLog.log(entry.returns(result).withAutomaticDuration());
862 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800863}
864
Benedict Wong319f17e2018-05-15 17:06:44 -0700865binder::Status NetdNativeService::ipSecUpdateTunnelInterface(const std::string& deviceName,
866 const std::string& localAddress,
867 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700868 int32_t iKey, int32_t oKey,
869 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800870 // Necessary locking done in IpSecService and kernel
871 ENFORCE_PERMISSION(NETWORK_STACK);
Benedict Wong319f17e2018-05-15 17:06:44 -0700872 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
manojboopathi8707f232018-01-02 14:45:47 -0800873
Benedict Wong319f17e2018-05-15 17:06:44 -0700874 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700875 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, true);
Benedict Wong319f17e2018-05-15 17:06:44 -0700876 RETURN_BINDER_STATUS_IF_NOT_OK(entry, result);
877
878 gLog.log(entry.returns(result).withAutomaticDuration());
879 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800880}
881
Benedict Wong319f17e2018-05-15 17:06:44 -0700882binder::Status NetdNativeService::ipSecRemoveTunnelInterface(const std::string& deviceName) {
manojboopathi8707f232018-01-02 14:45:47 -0800883 // Necessary locking done in IpSecService and kernel
884 ENFORCE_PERMISSION(NETWORK_STACK);
Benedict Wong319f17e2018-05-15 17:06:44 -0700885 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
manojboopathi8707f232018-01-02 14:45:47 -0800886
Benedict Wong319f17e2018-05-15 17:06:44 -0700887 netdutils::Status result = gCtls->xfrmCtrl.ipSecRemoveTunnelInterface(deviceName);
888 RETURN_BINDER_STATUS_IF_NOT_OK(entry, result);
889
890 gLog.log(entry.returns(result).withAutomaticDuration());
891 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800892}
893
Joel Scherpelzde937962017-06-01 13:20:21 +0900894binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
895 int32_t mode) {
896 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700897 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900898}
899
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900900binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName,
901 const std::string& prefix, int32_t mark,
902 int32_t mask) {
903 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700904 return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900905}
906
907binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
908 const std::string& prefix, int32_t mark,
909 int32_t mask) {
910 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700911 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900912}
913
Chenbo Feng07d43fe2017-12-21 14:38:51 -0800914binder::Status NetdNativeService::trafficCheckBpfStatsEnable(bool* ret) {
915 ENFORCE_PERMISSION(NETWORK_STACK);
916 *ret = gCtls->trafficCtrl.checkBpfStatsEnable();
917 return binder::Status::ok();
918}
919
Luke Huang0051a622018-07-23 20:30:16 +0800920binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout,
921 const std::string& classLabel) {
922 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock);
923 auto entry = gLog.newEntry()
924 .prettyFunction(__PRETTY_FUNCTION__)
925 .arg(ifName)
926 .arg(timeout)
927 .arg(classLabel);
928 int res =
929 gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str());
930 gLog.log(entry.returns(res).withAutomaticDuration());
931 return statusFromErrcode(res);
932}
933
934binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName,
935 int32_t timeout,
936 const std::string& classLabel) {
937 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock);
938 auto entry = gLog.newEntry()
939 .prettyFunction(__PRETTY_FUNCTION__)
940 .arg(ifName)
941 .arg(timeout)
942 .arg(classLabel);
943 int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout,
944 classLabel.c_str());
945 gLog.log(entry.returns(res).withAutomaticDuration());
946 return statusFromErrcode(res);
947}
Luke Huanga67dd562018-07-17 19:58:25 +0800948
949binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) {
950 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->strictCtrl.lock);
951 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid).arg(policyPenalty);
952 StrictPenalty penalty;
953 switch (policyPenalty) {
954 case INetd::PENALTY_POLICY_REJECT:
955 penalty = REJECT;
956 break;
957 case INetd::PENALTY_POLICY_LOG:
958 penalty = LOG;
959 break;
960 case INetd::PENALTY_POLICY_ACCEPT:
961 penalty = ACCEPT;
962 break;
963 default:
964 return statusFromErrcode(-EINVAL);
965 break;
966 }
967 int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty);
968 gLog.log(entry.returns(res).withAutomaticDuration());
969 return statusFromErrcode(res);
970}
Luke Huange64fa382018-07-24 16:38:22 +0800971
Luke Huang6d301232018-08-01 14:05:18 +0800972binder::Status NetdNativeService::clatdStart(const std::string& ifName) {
973 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex);
974 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
975 int res = gCtls->clatdCtrl.startClatd(ifName.c_str());
976 gLog.log(entry.returns(res).withAutomaticDuration());
977 return statusFromErrcode(res);
978}
979
980binder::Status NetdNativeService::clatdStop(const std::string& ifName) {
981 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex);
982 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
983 int res = gCtls->clatdCtrl.stopClatd(ifName.c_str());
984 gLog.log(entry.returns(res).withAutomaticDuration());
985 return statusFromErrcode(res);
986}
Luke Huanga67dd562018-07-17 19:58:25 +0800987
Luke Huang457d4702018-08-16 15:39:15 +0800988binder::Status NetdNativeService::ipfwdEnabled(bool* status) {
989 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
990 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
991 *status = (gCtls->tetherCtrl.forwardingRequestCount() > 0) ? true : false;
992 gLog.log(entry.returns(*status).withAutomaticDuration());
993 return binder::Status::ok();
994}
995
996binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) {
997 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
998 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester);
999 int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
1000 gLog.log(entry.returns(res).withAutomaticDuration());
1001 return statusFromErrcode(res);
1002}
1003
1004binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) {
1005 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1006 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester);
1007 int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
1008 gLog.log(entry.returns(res).withAutomaticDuration());
1009 return statusFromErrcode(res);
1010}
1011
1012binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface,
1013 const std::string& toIface) {
1014 ENFORCE_PERMISSION(NETWORK_STACK);
1015 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface);
1016 int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str());
1017 gLog.log(entry.returns(res).withAutomaticDuration());
1018 return statusFromErrcode(res);
1019}
1020
1021binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface,
1022 const std::string& toIface) {
1023 ENFORCE_PERMISSION(NETWORK_STACK);
1024 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface);
1025 int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str());
1026 gLog.log(entry.returns(res).withAutomaticDuration());
1027 return statusFromErrcode(res);
1028}
1029
Luke Huangf7782042018-08-08 13:13:04 +08001030namespace {
1031std::string addSquareBrackets(const std::string& s) {
1032 return "[" + s + "]";
1033}
1034
1035std::string addCurlyBrackets(const std::string& s) {
1036 return "{" + s + "}";
1037}
1038
1039} // namespace
1040binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) {
1041 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1042 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1043
1044 const auto& ifaceList = InterfaceController::getIfaceNames();
1045 RETURN_BINDER_STATUS_IF_NOT_OK(entry, ifaceList);
1046
1047 interfaceListResult->clear();
1048 interfaceListResult->reserve(ifaceList.value().size());
1049 interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()),
1050 end(ifaceList.value()));
1051
1052 gLog.log(entry.returns(addSquareBrackets(base::Join(*interfaceListResult, ", ")))
1053 .withAutomaticDuration());
1054 return binder::Status::ok();
1055}
1056
1057std::string interfaceConfigurationParcelToString(const InterfaceConfigurationParcel& cfg) {
1058 std::vector<std::string> result{cfg.ifName, cfg.hwAddr, cfg.ipv4Addr,
1059 std::to_string(cfg.prefixLength)};
1060 result.insert(end(result), begin(cfg.flags), end(cfg.flags));
1061 return addCurlyBrackets(base::Join(result, ", "));
1062}
1063
1064binder::Status NetdNativeService::interfaceGetCfg(
1065 const std::string& ifName, InterfaceConfigurationParcel* interfaceGetCfgResult) {
1066 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1067 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1068
1069 const auto& cfgRes = InterfaceController::getCfg(ifName);
1070 RETURN_BINDER_STATUS_IF_NOT_OK(entry, cfgRes);
1071
1072 *interfaceGetCfgResult = cfgRes.value();
1073 gLog.log(entry.returns(interfaceConfigurationParcelToString(*interfaceGetCfgResult))
1074 .withAutomaticDuration());
1075 return binder::Status::ok();
1076}
1077
1078binder::Status NetdNativeService::interfaceSetCfg(const InterfaceConfigurationParcel& cfg) {
1079 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1080 auto entry = gLog.newEntry()
1081 .prettyFunction(__PRETTY_FUNCTION__)
1082 .arg(interfaceConfigurationParcelToString(cfg));
1083
1084 const auto& res = InterfaceController::setCfg(cfg);
1085 RETURN_BINDER_STATUS_IF_NOT_OK(entry, res);
1086
1087 gLog.log(entry.withAutomaticDuration());
1088 return binder::Status::ok();
1089}
1090
1091binder::Status NetdNativeService::interfaceSetIPv6PrivacyExtensions(const std::string& ifName,
1092 bool enable) {
1093 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1094 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(ifName, enable);
1095 int res = InterfaceController::setIPv6PrivacyExtensions(ifName.c_str(), enable);
1096 gLog.log(entry.returns(res).withAutomaticDuration());
1097 return statusFromErrcode(res);
1098}
1099
1100binder::Status NetdNativeService::interfaceClearAddrs(const std::string& ifName) {
1101 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1102 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1103 int res = InterfaceController::clearAddrs(ifName.c_str());
1104 gLog.log(entry.returns(res).withAutomaticDuration());
1105 return statusFromErrcode(res);
1106}
1107
1108binder::Status NetdNativeService::interfaceSetEnableIPv6(const std::string& ifName, bool enable) {
1109 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1110 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(ifName, enable);
1111 int res = InterfaceController::setEnableIPv6(ifName.c_str(), enable);
1112 gLog.log(entry.returns(res).withAutomaticDuration());
1113 return statusFromErrcode(res);
1114}
1115
1116binder::Status NetdNativeService::interfaceSetMtu(const std::string& ifName, int32_t mtuValue) {
1117 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1118 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(ifName, mtuValue);
1119 std::string mtu = std::to_string(mtuValue);
1120 int res = InterfaceController::setMtu(ifName.c_str(), mtu.c_str());
1121 gLog.log(entry.returns(res).withAutomaticDuration());
1122 return statusFromErrcode(res);
1123}
1124
Luke Huangb5733d72018-08-21 17:17:19 +08001125binder::Status NetdNativeService::tetherStart(const std::vector<std::string>& dhcpRanges) {
1126 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1127 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(dhcpRanges);
1128 if (dhcpRanges.size() % 2 == 1) {
1129 return statusFromErrcode(-EINVAL);
1130 }
1131 int res = gCtls->tetherCtrl.startTethering(dhcpRanges);
1132 gLog.log(entry.returns(res).withAutomaticDuration());
1133 return statusFromErrcode(res);
1134}
1135
1136binder::Status NetdNativeService::tetherStop() {
1137 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1138 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1139 int res = gCtls->tetherCtrl.stopTethering();
1140 gLog.log(entry.returns(res).withAutomaticDuration());
1141 return statusFromErrcode(res);
1142}
1143
1144binder::Status NetdNativeService::tetherIsEnabled(bool* enabled) {
1145 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1146 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1147 *enabled = gCtls->tetherCtrl.isTetheringStarted();
1148 gLog.log(entry.returns(*enabled).withAutomaticDuration());
1149 return binder::Status::ok();
1150}
1151
1152binder::Status NetdNativeService::tetherInterfaceAdd(const std::string& ifName) {
1153 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1154 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1155 int res = gCtls->tetherCtrl.tetherInterface(ifName.c_str());
1156 gLog.log(entry.returns(res).withAutomaticDuration());
1157 return statusFromErrcode(res);
1158}
1159
1160binder::Status NetdNativeService::tetherInterfaceRemove(const std::string& ifName) {
1161 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1162 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1163 int res = gCtls->tetherCtrl.untetherInterface(ifName.c_str());
1164 gLog.log(entry.returns(res).withAutomaticDuration());
1165 return statusFromErrcode(res);
1166}
1167
1168binder::Status NetdNativeService::tetherInterfaceList(std::vector<std::string>* ifList) {
1169 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1170 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1171 for (const auto& ifname : gCtls->tetherCtrl.getTetheredInterfaceList()) {
1172 ifList->push_back(ifname);
1173 }
1174 gLog.log(entry.returns(true).withAutomaticDuration());
1175 return binder::Status::ok();
1176}
1177
1178binder::Status NetdNativeService::tetherDnsSet(int32_t netId,
1179 const std::vector<std::string>& dnsAddrs) {
1180 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1181 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(dnsAddrs);
1182 int res = gCtls->tetherCtrl.setDnsForwarders(netId, dnsAddrs);
1183 gLog.log(entry.returns(res).withAutomaticDuration());
1184 return statusFromErrcode(res);
1185}
1186
1187binder::Status NetdNativeService::tetherDnsList(std::vector<std::string>* dnsList) {
1188 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1189 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1190 for (const auto& fwdr : gCtls->tetherCtrl.getDnsForwarders()) {
1191 dnsList->push_back(fwdr);
1192 }
1193 gLog.log(entry.returns(true).withAutomaticDuration());
1194 return binder::Status::ok();
1195}
1196
Luke Huangb670d162018-08-23 20:01:13 +08001197binder::Status NetdNativeService::networkAddRoute(int32_t netId, const std::string& ifName,
1198 const std::string& destination,
1199 const std::string& nextHop) {
1200 // Public methods of NetworkController are thread-safe.
1201 ENFORCE_PERMISSION(NETWORK_STACK);
1202 auto entry = gLog.newEntry()
1203 .prettyFunction(__PRETTY_FUNCTION__)
1204 .arg(netId)
1205 .arg(ifName)
1206 .arg(destination)
1207 .arg(nextHop);
1208 bool legacy = false;
1209 uid_t uid = 0; // UID is only meaningful for legacy routes.
1210 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1211 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
1212 gLog.log(entry.returns(res).withAutomaticDuration());
1213 return statusFromErrcode(res);
1214}
1215
1216binder::Status NetdNativeService::networkRemoveRoute(int32_t netId, const std::string& ifName,
1217 const std::string& destination,
1218 const std::string& nextHop) {
1219 ENFORCE_PERMISSION(NETWORK_STACK);
1220 auto entry = gLog.newEntry()
1221 .prettyFunction(__PRETTY_FUNCTION__)
1222 .arg(netId)
1223 .arg(ifName)
1224 .arg(destination)
1225 .arg(nextHop);
1226 bool legacy = false;
1227 uid_t uid = 0; // UID is only meaningful for legacy routes.
1228 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1229 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
1230 gLog.log(entry.returns(res).withAutomaticDuration());
1231 return statusFromErrcode(res);
1232}
1233
1234binder::Status NetdNativeService::networkAddLegacyRoute(int32_t netId, const std::string& ifName,
1235 const std::string& destination,
1236 const std::string& nextHop, int32_t uid) {
1237 ENFORCE_PERMISSION(NETWORK_STACK);
1238 auto entry = gLog.newEntry()
1239 .prettyFunction(__PRETTY_FUNCTION__)
1240 .arg(netId)
1241 .arg(ifName)
1242 .arg(destination)
1243 .arg(nextHop)
1244 .arg(uid);
1245 bool legacy = true;
1246 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1247 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1248 (uid_t) uid);
1249 gLog.log(entry.returns(res).withAutomaticDuration());
1250 return statusFromErrcode(res);
1251}
1252
1253binder::Status NetdNativeService::networkRemoveLegacyRoute(int32_t netId, const std::string& ifName,
1254 const std::string& destination,
1255 const std::string& nextHop,
1256 int32_t uid) {
1257 ENFORCE_PERMISSION(NETWORK_STACK);
1258 auto entry = gLog.newEntry()
1259 .prettyFunction(__PRETTY_FUNCTION__)
1260 .arg(netId)
1261 .arg(ifName)
1262 .arg(destination)
1263 .arg(nextHop)
1264 .arg(uid);
1265 bool legacy = true;
1266 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1267 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1268 (uid_t) uid);
1269 gLog.log(entry.returns(res).withAutomaticDuration());
1270 return statusFromErrcode(res);
1271}
1272
1273binder::Status NetdNativeService::networkGetDefault(int32_t* netId) {
1274 ENFORCE_PERMISSION(NETWORK_STACK);
1275 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1276 *netId = gCtls->netCtrl.getDefaultNetwork();
1277 gLog.log(entry.returns(*netId).withAutomaticDuration());
1278 return binder::Status::ok();
1279}
1280
1281binder::Status NetdNativeService::networkSetDefault(int32_t netId) {
1282 ENFORCE_PERMISSION(NETWORK_STACK);
1283 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId);
1284 int res = gCtls->netCtrl.setDefaultNetwork(netId);
1285 gLog.log(entry.returns(res).withAutomaticDuration());
1286 return statusFromErrcode(res);
1287}
1288
1289binder::Status NetdNativeService::networkClearDefault() {
1290 ENFORCE_PERMISSION(NETWORK_STACK);
1291 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1292 unsigned netId = NETID_UNSET;
1293 int res = gCtls->netCtrl.setDefaultNetwork(netId);
1294 gLog.log(entry.returns(res).withAutomaticDuration());
1295 return statusFromErrcode(res);
1296}
1297
1298std::vector<uid_t> NetdNativeService::intsToUids(const std::vector<int32_t>& intUids) {
1299 return {begin(intUids), end(intUids)};
1300}
1301
1302Permission NetdNativeService::convertPermission(int32_t permission) {
1303 switch (permission) {
1304 case INetd::PERMISSION_NETWORK:
1305 return Permission::PERMISSION_NETWORK;
1306 case INetd::PERMISSION_SYSTEM:
1307 return Permission::PERMISSION_SYSTEM;
1308 default:
1309 return Permission::PERMISSION_NONE;
1310 }
1311}
1312
1313binder::Status NetdNativeService::networkSetPermissionForNetwork(int32_t netId,
1314 int32_t permission) {
1315 ENFORCE_PERMISSION(NETWORK_STACK);
1316 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(permission);
1317 std::vector<unsigned> netIds = {(unsigned) netId};
1318 int res = gCtls->netCtrl.setPermissionForNetworks(convertPermission(permission), netIds);
1319 gLog.log(entry.returns(res).withAutomaticDuration());
1320 return statusFromErrcode(res);
1321}
1322
1323binder::Status NetdNativeService::networkSetPermissionForUser(int32_t permission,
1324 const std::vector<int32_t>& uids) {
1325 ENFORCE_PERMISSION(NETWORK_STACK);
1326 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(permission).arg(uids);
1327 gCtls->netCtrl.setPermissionForUsers(convertPermission(permission), intsToUids(uids));
1328 gLog.log(entry.withAutomaticDuration());
1329 return binder::Status::ok();
1330}
1331
1332binder::Status NetdNativeService::networkClearPermissionForUser(const std::vector<int32_t>& uids) {
1333 ENFORCE_PERMISSION(NETWORK_STACK);
1334 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uids);
1335 Permission permission = Permission::PERMISSION_NONE;
1336 gCtls->netCtrl.setPermissionForUsers(permission, intsToUids(uids));
1337 gLog.log(entry.withAutomaticDuration());
1338 return binder::Status::ok();
1339}
1340
1341binder::Status NetdNativeService::NetdNativeService::networkSetProtectAllow(int32_t uid) {
1342 ENFORCE_PERMISSION(NETWORK_STACK);
1343 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
1344 std::vector<uid_t> uids = {(uid_t) uid};
1345 gCtls->netCtrl.allowProtect(uids);
1346 gLog.log(entry.withAutomaticDuration());
1347 return binder::Status::ok();
1348}
1349
1350binder::Status NetdNativeService::networkSetProtectDeny(int32_t uid) {
1351 ENFORCE_PERMISSION(NETWORK_STACK);
1352 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
1353 std::vector<uid_t> uids = {(uid_t) uid};
1354 gCtls->netCtrl.denyProtect(uids);
1355 gLog.log(entry.withAutomaticDuration());
1356 return binder::Status::ok();
1357}
1358
1359binder::Status NetdNativeService::networkCanProtect(int32_t uid, bool* ret) {
1360 ENFORCE_PERMISSION(NETWORK_STACK);
1361 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
1362 *ret = gCtls->netCtrl.canProtect((uid_t) uid);
1363 gLog.log(entry.returns(*ret).withAutomaticDuration());
1364 return binder::Status::ok();
1365}
1366
Chenbo Feng48eaed32018-12-26 17:40:21 -08001367binder::Status NetdNativeService::trafficSetNetPermForUids(int32_t permission,
1368 const std::vector<int32_t>& uids) {
1369 ENFORCE_PERMISSION(NETWORK_STACK);
1370 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(permission).arg(uids);
1371 gCtls->trafficCtrl.setPermissionForUids(permission, intsToUids(uids));
1372 gLog.log(entry.withAutomaticDuration());
1373 return binder::Status::ok();
1374}
1375
Luke Huange64fa382018-07-24 16:38:22 +08001376namespace {
1377std::string ruleToString(int32_t rule) {
1378 switch (rule) {
1379 case INetd::FIREWALL_RULE_DENY:
1380 return "DENY";
1381 case INetd::FIREWALL_RULE_ALLOW:
1382 return "ALLOW";
1383 default:
1384 return "INVALID";
1385 }
1386}
1387
1388std::string typeToString(int32_t type) {
1389 switch (type) {
1390 case INetd::FIREWALL_WHITELIST:
1391 return "WHITELIST";
1392 case INetd::FIREWALL_BLACKLIST:
1393 return "BLACKLIST";
1394 default:
1395 return "INVALID";
1396 }
1397}
1398
1399std::string chainToString(int32_t chain) {
1400 switch (chain) {
1401 case INetd::FIREWALL_CHAIN_NONE:
1402 return "NONE";
1403 case INetd::FIREWALL_CHAIN_DOZABLE:
1404 return "DOZABLE";
1405 case INetd::FIREWALL_CHAIN_STANDBY:
1406 return "STANDBY";
1407 case INetd::FIREWALL_CHAIN_POWERSAVE:
1408 return "POWERSAVE";
1409 default:
1410 return "INVALID";
1411 }
1412}
1413
1414} // namespace
1415
1416binder::Status NetdNativeService::firewallSetFirewallType(int32_t firewallType) {
1417 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1418 auto entry =
1419 gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(typeToString(firewallType));
1420 auto type = static_cast<FirewallType>(firewallType);
1421
1422 int res = gCtls->firewallCtrl.setFirewallType(type);
1423 gLog.log(entry.returns(res).withAutomaticDuration());
1424 return statusFromErrcode(res);
1425}
1426
1427binder::Status NetdNativeService::firewallSetInterfaceRule(const std::string& ifName,
1428 int32_t firewallRule) {
1429 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1430 auto entry = gLog.newEntry()
1431 .prettyFunction(__PRETTY_FUNCTION__)
1432 .args(ifName, ruleToString(firewallRule));
1433 auto rule = static_cast<FirewallRule>(firewallRule);
1434
1435 int res = gCtls->firewallCtrl.setInterfaceRule(ifName.c_str(), rule);
1436 gLog.log(entry.returns(res).withAutomaticDuration());
1437 return statusFromErrcode(res);
1438}
1439
1440binder::Status NetdNativeService::firewallSetUidRule(int32_t childChain, int32_t uid,
1441 int32_t firewallRule) {
1442 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1443 auto entry = gLog.newEntry()
1444 .prettyFunction(__PRETTY_FUNCTION__)
1445 .args(chainToString(childChain), uid, ruleToString(firewallRule));
1446 auto chain = static_cast<ChildChain>(childChain);
1447 auto rule = static_cast<FirewallRule>(firewallRule);
1448
1449 int res = gCtls->firewallCtrl.setUidRule(chain, uid, rule);
1450 gLog.log(entry.returns(res).withAutomaticDuration());
1451 return statusFromErrcode(res);
1452}
1453
1454binder::Status NetdNativeService::firewallEnableChildChain(int32_t childChain, bool enable) {
1455 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1456 auto entry = gLog.newEntry()
1457 .prettyFunction(__PRETTY_FUNCTION__)
1458 .args(chainToString(childChain), enable);
1459 auto chain = static_cast<ChildChain>(childChain);
1460
1461 int res = gCtls->firewallCtrl.enableChildChains(chain, enable);
1462 gLog.log(entry.returns(res).withAutomaticDuration());
1463 return statusFromErrcode(res);
1464}
1465
Luke Huang19b49c52018-10-22 12:12:05 +09001466binder::Status NetdNativeService::tetherAddForward(const std::string& intIface,
1467 const std::string& extIface) {
1468 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1469 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(intIface, extIface);
1470
1471 int res = gCtls->tetherCtrl.enableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001472 gLog.log(entry.returns(res).withAutomaticDuration());
1473 return statusFromErrcode(res);
1474}
1475
1476binder::Status NetdNativeService::tetherRemoveForward(const std::string& intIface,
1477 const std::string& extIface) {
1478 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
Luke Huang19b49c52018-10-22 12:12:05 +09001479 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(intIface, extIface);
1480
Luke Huangae038f82018-11-05 11:17:31 +09001481 int res = gCtls->tetherCtrl.disableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001482 gLog.log(entry.returns(res).withAutomaticDuration());
1483 return statusFromErrcode(res);
1484}
1485
Chenbo Fengf5663d82018-11-08 16:10:48 -08001486binder::Status NetdNativeService::setTcpRWmemorySize(const std::string& rmemValues,
1487 const std::string& wmemValues) {
1488 ENFORCE_PERMISSION(NETWORK_STACK);
1489 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(rmemValues, wmemValues);
1490 if (!WriteStringToFile(rmemValues, TCP_RMEM_PROC_FILE)) {
1491 int ret = -errno;
1492 gLog.log(entry.returns(ret).withAutomaticDuration());
1493 return statusFromErrcode(ret);
1494 }
1495
1496 if (!WriteStringToFile(wmemValues, TCP_WMEM_PROC_FILE)) {
1497 int ret = -errno;
1498 gLog.log(entry.returns(ret).withAutomaticDuration());
1499 return statusFromErrcode(ret);
1500 }
1501 gLog.log(entry.withAutomaticDuration());
1502 return binder::Status::ok();
1503}
1504
nuccachenf52f7a52018-07-17 18:07:23 +08001505binder::Status NetdNativeService::getPrefix64(int netId, std::string* _aidl_return) {
1506 ENFORCE_PERMISSION(NETWORK_STACK);
1507
1508 netdutils::IPPrefix prefix{};
1509 int err = gCtls->resolverCtrl.getPrefix64(netId, &prefix);
1510 if (err != 0) {
1511 return binder::Status::fromServiceSpecificError(
1512 -err, String8::format("ResolverController error: %s", strerror(-err)));
1513 }
1514 *_aidl_return = prefix.toString();
1515 return binder::Status::ok();
1516}
1517
Luke Huang528af602018-08-29 19:06:05 +08001518binder::Status NetdNativeService::registerUnsolicitedEventListener(
1519 const android::sp<android::net::INetdUnsolicitedEventListener>& listener) {
1520 ENFORCE_PERMISSION(NETWORK_STACK);
Bernie Innocenti9ee088d2019-02-01 17:14:32 +09001521 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1522 gCtls->eventReporter.registerUnsolEventListener(listener);
Luke Huang528af602018-08-29 19:06:05 +08001523 gLog.log(entry.withAutomaticDuration());
1524 return binder::Status::ok();
1525}
1526
Lorenzo Colittie4d626e2016-02-02 17:19:04 +09001527} // namespace net
1528} // namespace android