blob: f60706941ec6c4c2898b5434001ed52a0ba03c4a [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.
Mike Yu4fe1b9c2019-01-29 17:50:19 +0800375 // Clear DNS servers before delete the cache to avoid the cache being created again.
Erik Klinec8b6a9c2018-01-15 17:06:48 +0900376 gCtls->resolverCtrl.clearDnsServers(netId);
Mike Yu4fe1b9c2019-01-29 17:50:19 +0800377 const int ret = gCtls->netCtrl.destroyNetwork(netId);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900378 return statusFromErrcode(ret);
379}
380
381binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
382 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
383 int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
384 return statusFromErrcode(ret);
385}
386
387binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
388 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
389 int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
390 return statusFromErrcode(ret);
391}
392
Luke Huang94658ac2018-10-18 19:35:12 +0900393namespace {
394
395std::string uidRangeParcelVecToString(const std::vector<UidRangeParcel>& uidRangeArray) {
396 std::vector<std::string> result;
397 result.reserve(uidRangeArray.size());
398 for (const auto& uidRange : uidRangeArray) {
399 result.push_back(StringPrintf("%d-%d", uidRange.start, uidRange.stop));
400 }
401
402 return base::Join(result, ", ");
403}
404
405} // namespace
406
407binder::Status NetdNativeService::networkAddUidRanges(
408 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900409 // NetworkController::addUsersToNetwork is thread-safe.
410 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Luke Huang94658ac2018-10-18 19:35:12 +0900411 auto entry = gLog.newEntry()
412 .prettyFunction(__PRETTY_FUNCTION__)
413 .args(netId, uidRangeParcelVecToString(uidRangeArray));
414
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900415 int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray));
Luke Huang94658ac2018-10-18 19:35:12 +0900416 gLog.log(entry.returns(ret).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900417 return statusFromErrcode(ret);
418}
419
Luke Huang94658ac2018-10-18 19:35:12 +0900420binder::Status NetdNativeService::networkRemoveUidRanges(
421 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900422 // NetworkController::removeUsersFromNetwork is thread-safe.
423 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Luke Huang94658ac2018-10-18 19:35:12 +0900424 auto entry = gLog.newEntry()
425 .prettyFunction(__PRETTY_FUNCTION__)
426 .args(netId, uidRangeParcelVecToString(uidRangeArray));
427
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900428 int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray));
Luke Huang94658ac2018-10-18 19:35:12 +0900429 gLog.log(entry.returns(ret).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900430 return statusFromErrcode(ret);
431}
432
Luke Huang94658ac2018-10-18 19:35:12 +0900433binder::Status NetdNativeService::networkRejectNonSecureVpn(
434 bool add, const std::vector<UidRangeParcel>& uidRangeArray) {
Robin Leeb8087362016-03-30 18:43:08 +0100435 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
436 // it should be possible to use the same lock as NetworkController. However, every call through
437 // the CommandListener "network" command will need to hold this lock too, not just the ones that
438 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
439 // look at routes, but it's not enough here).
440 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
Luke Huang94658ac2018-10-18 19:35:12 +0900441 auto entry = gLog.newEntry()
442 .prettyFunction(__PRETTY_FUNCTION__)
443 .args(add, uidRangeParcelVecToString(uidRangeArray));
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900444 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100445
446 int err;
447 if (add) {
448 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
449 } else {
450 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
451 }
Luke Huang94658ac2018-10-18 19:35:12 +0900452 gLog.log(entry.returns(err).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900453 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100454}
455
Luke Huang94658ac2018-10-18 19:35:12 +0900456binder::Status NetdNativeService::socketDestroy(const std::vector<UidRangeParcel>& uids,
457 const std::vector<int32_t>& skipUids) {
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900458 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
459
Luke Huang94658ac2018-10-18 19:35:12 +0900460 auto entry = gLog.newEntry()
461 .prettyFunction(__PRETTY_FUNCTION__)
462 .arg(uidRangeParcelVecToString(uids));
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900463 SockDiag sd;
464 if (!sd.open()) {
465 return binder::Status::fromServiceSpecificError(EIO,
466 String8("Could not open SOCK_DIAG socket"));
467 }
468
469 UidRanges uidRanges(uids);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +0900470 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
471 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900472
Luke Huang94658ac2018-10-18 19:35:12 +0900473 gLog.log(entry.returns(err).withAutomaticDuration());
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900474 if (err) {
475 return binder::Status::fromServiceSpecificError(-err,
476 String8::format("destroySockets: %s", strerror(-err)));
477 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900478 return binder::Status::ok();
479}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900480
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400481// Parse a base64 encoded string into a vector of bytes.
482// On failure, return an empty vector.
483static std::vector<uint8_t> parseBase64(const std::string& input) {
484 std::vector<uint8_t> decoded;
485 size_t out_len;
486 if (EVP_DecodedLength(&out_len, input.size()) != 1) {
487 return decoded;
488 }
489 // out_len is now an upper bound on the output length.
490 decoded.resize(out_len);
491 if (EVP_DecodeBase64(decoded.data(), &out_len, decoded.size(),
492 reinterpret_cast<const uint8_t*>(input.data()), input.size()) == 1) {
493 // Possibly shrink the vector if the actual output was smaller than the bound.
494 decoded.resize(out_len);
495 } else {
496 decoded.clear();
497 }
498 if (out_len != SHA256_SIZE) {
499 decoded.clear();
500 }
501 return decoded;
502}
503
Pierre Imaibeedec32016-04-13 06:44:51 +0900504binder::Status NetdNativeService::setResolverConfiguration(int32_t netId,
505 const std::vector<std::string>& servers, const std::vector<std::string>& domains,
Erik Klinea1476fb2018-03-04 21:01:56 +0900506 const std::vector<int32_t>& params, const std::string& tlsName,
507 const std::vector<std::string>& tlsServers,
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400508 const std::vector<std::string>& tlsFingerprints) {
Pierre Imaibeedec32016-04-13 06:44:51 +0900509 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
510 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900511 auto entry = gLog.newEntry()
512 .prettyFunction(__PRETTY_FUNCTION__)
513 .arg(netId)
514 .arg(servers)
515 .arg(domains)
516 .arg(params)
517 .arg(tlsName)
518 .arg(tlsServers)
519 .arg(tlsFingerprints);
Pierre Imaibeedec32016-04-13 06:44:51 +0900520
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400521 std::set<std::vector<uint8_t>> decoded_fingerprints;
522 for (const std::string& fingerprint : tlsFingerprints) {
523 std::vector<uint8_t> decoded = parseBase64(fingerprint);
524 if (decoded.empty()) {
525 return binder::Status::fromServiceSpecificError(EINVAL,
526 String8::format("ResolverController error: bad fingerprint"));
527 }
528 decoded_fingerprints.emplace(decoded);
529 }
530
531 int err = gCtls->resolverCtrl.setResolverConfiguration(netId, servers, domains, params,
Erik Klinea1476fb2018-03-04 21:01:56 +0900532 tlsName, tlsServers, decoded_fingerprints);
Erik Klineb31fd692018-06-06 20:50:11 +0900533 gLog.log(entry.returns(err).withAutomaticDuration());
Pierre Imaibeedec32016-04-13 06:44:51 +0900534 if (err != 0) {
535 return binder::Status::fromServiceSpecificError(-err,
536 String8::format("ResolverController error: %s", strerror(-err)));
537 }
538 return binder::Status::ok();
539}
540
Ken Chen2a429352018-12-22 21:46:55 +0800541binder::Status NetdNativeService::getResolverInfo(
542 int32_t netId, std::vector<std::string>* servers, std::vector<std::string>* domains,
543 std::vector<std::string>* tlsServers, std::vector<int32_t>* params,
544 std::vector<int32_t>* stats, std::vector<int32_t>* wait_for_pending_req_timeout_count) {
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
Ken Chen2a429352018-12-22 21:46:55 +0800548 int err = gCtls->resolverCtrl.getResolverInfo(netId, servers, domains, tlsServers, params,
549 stats, wait_for_pending_req_timeout_count);
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
Luke Huang0051a622018-07-23 20:30:16 +0800914binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout,
915 const std::string& classLabel) {
916 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock);
917 auto entry = gLog.newEntry()
918 .prettyFunction(__PRETTY_FUNCTION__)
919 .arg(ifName)
920 .arg(timeout)
921 .arg(classLabel);
922 int res =
923 gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str());
924 gLog.log(entry.returns(res).withAutomaticDuration());
925 return statusFromErrcode(res);
926}
927
928binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName,
929 int32_t timeout,
930 const std::string& classLabel) {
931 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock);
932 auto entry = gLog.newEntry()
933 .prettyFunction(__PRETTY_FUNCTION__)
934 .arg(ifName)
935 .arg(timeout)
936 .arg(classLabel);
937 int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout,
938 classLabel.c_str());
939 gLog.log(entry.returns(res).withAutomaticDuration());
940 return statusFromErrcode(res);
941}
Luke Huanga67dd562018-07-17 19:58:25 +0800942
943binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) {
944 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->strictCtrl.lock);
945 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid).arg(policyPenalty);
946 StrictPenalty penalty;
947 switch (policyPenalty) {
948 case INetd::PENALTY_POLICY_REJECT:
949 penalty = REJECT;
950 break;
951 case INetd::PENALTY_POLICY_LOG:
952 penalty = LOG;
953 break;
954 case INetd::PENALTY_POLICY_ACCEPT:
955 penalty = ACCEPT;
956 break;
957 default:
958 return statusFromErrcode(-EINVAL);
959 break;
960 }
961 int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty);
962 gLog.log(entry.returns(res).withAutomaticDuration());
963 return statusFromErrcode(res);
964}
Luke Huange64fa382018-07-24 16:38:22 +0800965
Luke Huang6d301232018-08-01 14:05:18 +0800966binder::Status NetdNativeService::clatdStart(const std::string& ifName) {
967 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex);
968 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
969 int res = gCtls->clatdCtrl.startClatd(ifName.c_str());
970 gLog.log(entry.returns(res).withAutomaticDuration());
971 return statusFromErrcode(res);
972}
973
974binder::Status NetdNativeService::clatdStop(const std::string& ifName) {
975 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex);
976 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
977 int res = gCtls->clatdCtrl.stopClatd(ifName.c_str());
978 gLog.log(entry.returns(res).withAutomaticDuration());
979 return statusFromErrcode(res);
980}
Luke Huanga67dd562018-07-17 19:58:25 +0800981
Luke Huang457d4702018-08-16 15:39:15 +0800982binder::Status NetdNativeService::ipfwdEnabled(bool* status) {
983 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
984 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
985 *status = (gCtls->tetherCtrl.forwardingRequestCount() > 0) ? true : false;
986 gLog.log(entry.returns(*status).withAutomaticDuration());
987 return binder::Status::ok();
988}
989
990binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) {
991 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
992 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester);
993 int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
994 gLog.log(entry.returns(res).withAutomaticDuration());
995 return statusFromErrcode(res);
996}
997
998binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) {
999 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1000 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester);
1001 int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
1002 gLog.log(entry.returns(res).withAutomaticDuration());
1003 return statusFromErrcode(res);
1004}
1005
1006binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface,
1007 const std::string& toIface) {
1008 ENFORCE_PERMISSION(NETWORK_STACK);
1009 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface);
1010 int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str());
1011 gLog.log(entry.returns(res).withAutomaticDuration());
1012 return statusFromErrcode(res);
1013}
1014
1015binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface,
1016 const std::string& toIface) {
1017 ENFORCE_PERMISSION(NETWORK_STACK);
1018 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface);
1019 int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str());
1020 gLog.log(entry.returns(res).withAutomaticDuration());
1021 return statusFromErrcode(res);
1022}
1023
Luke Huangf7782042018-08-08 13:13:04 +08001024namespace {
1025std::string addSquareBrackets(const std::string& s) {
1026 return "[" + s + "]";
1027}
1028
1029std::string addCurlyBrackets(const std::string& s) {
1030 return "{" + s + "}";
1031}
1032
1033} // namespace
1034binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) {
1035 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1036 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1037
1038 const auto& ifaceList = InterfaceController::getIfaceNames();
1039 RETURN_BINDER_STATUS_IF_NOT_OK(entry, ifaceList);
1040
1041 interfaceListResult->clear();
1042 interfaceListResult->reserve(ifaceList.value().size());
1043 interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()),
1044 end(ifaceList.value()));
1045
1046 gLog.log(entry.returns(addSquareBrackets(base::Join(*interfaceListResult, ", ")))
1047 .withAutomaticDuration());
1048 return binder::Status::ok();
1049}
1050
1051std::string interfaceConfigurationParcelToString(const InterfaceConfigurationParcel& cfg) {
1052 std::vector<std::string> result{cfg.ifName, cfg.hwAddr, cfg.ipv4Addr,
1053 std::to_string(cfg.prefixLength)};
1054 result.insert(end(result), begin(cfg.flags), end(cfg.flags));
1055 return addCurlyBrackets(base::Join(result, ", "));
1056}
1057
1058binder::Status NetdNativeService::interfaceGetCfg(
1059 const std::string& ifName, InterfaceConfigurationParcel* interfaceGetCfgResult) {
1060 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1061 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1062
1063 const auto& cfgRes = InterfaceController::getCfg(ifName);
1064 RETURN_BINDER_STATUS_IF_NOT_OK(entry, cfgRes);
1065
1066 *interfaceGetCfgResult = cfgRes.value();
1067 gLog.log(entry.returns(interfaceConfigurationParcelToString(*interfaceGetCfgResult))
1068 .withAutomaticDuration());
1069 return binder::Status::ok();
1070}
1071
1072binder::Status NetdNativeService::interfaceSetCfg(const InterfaceConfigurationParcel& cfg) {
1073 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1074 auto entry = gLog.newEntry()
1075 .prettyFunction(__PRETTY_FUNCTION__)
1076 .arg(interfaceConfigurationParcelToString(cfg));
1077
1078 const auto& res = InterfaceController::setCfg(cfg);
1079 RETURN_BINDER_STATUS_IF_NOT_OK(entry, res);
1080
1081 gLog.log(entry.withAutomaticDuration());
1082 return binder::Status::ok();
1083}
1084
1085binder::Status NetdNativeService::interfaceSetIPv6PrivacyExtensions(const std::string& ifName,
1086 bool enable) {
1087 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1088 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(ifName, enable);
1089 int res = InterfaceController::setIPv6PrivacyExtensions(ifName.c_str(), enable);
1090 gLog.log(entry.returns(res).withAutomaticDuration());
1091 return statusFromErrcode(res);
1092}
1093
1094binder::Status NetdNativeService::interfaceClearAddrs(const std::string& ifName) {
1095 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1096 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1097 int res = InterfaceController::clearAddrs(ifName.c_str());
1098 gLog.log(entry.returns(res).withAutomaticDuration());
1099 return statusFromErrcode(res);
1100}
1101
1102binder::Status NetdNativeService::interfaceSetEnableIPv6(const std::string& ifName, bool enable) {
1103 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1104 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(ifName, enable);
1105 int res = InterfaceController::setEnableIPv6(ifName.c_str(), enable);
1106 gLog.log(entry.returns(res).withAutomaticDuration());
1107 return statusFromErrcode(res);
1108}
1109
1110binder::Status NetdNativeService::interfaceSetMtu(const std::string& ifName, int32_t mtuValue) {
1111 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1112 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(ifName, mtuValue);
1113 std::string mtu = std::to_string(mtuValue);
1114 int res = InterfaceController::setMtu(ifName.c_str(), mtu.c_str());
1115 gLog.log(entry.returns(res).withAutomaticDuration());
1116 return statusFromErrcode(res);
1117}
1118
Luke Huangb5733d72018-08-21 17:17:19 +08001119binder::Status NetdNativeService::tetherStart(const std::vector<std::string>& dhcpRanges) {
1120 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1121 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(dhcpRanges);
1122 if (dhcpRanges.size() % 2 == 1) {
1123 return statusFromErrcode(-EINVAL);
1124 }
1125 int res = gCtls->tetherCtrl.startTethering(dhcpRanges);
1126 gLog.log(entry.returns(res).withAutomaticDuration());
1127 return statusFromErrcode(res);
1128}
1129
1130binder::Status NetdNativeService::tetherStop() {
1131 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1132 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1133 int res = gCtls->tetherCtrl.stopTethering();
1134 gLog.log(entry.returns(res).withAutomaticDuration());
1135 return statusFromErrcode(res);
1136}
1137
1138binder::Status NetdNativeService::tetherIsEnabled(bool* enabled) {
1139 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1140 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1141 *enabled = gCtls->tetherCtrl.isTetheringStarted();
1142 gLog.log(entry.returns(*enabled).withAutomaticDuration());
1143 return binder::Status::ok();
1144}
1145
1146binder::Status NetdNativeService::tetherInterfaceAdd(const std::string& ifName) {
1147 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1148 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1149 int res = gCtls->tetherCtrl.tetherInterface(ifName.c_str());
1150 gLog.log(entry.returns(res).withAutomaticDuration());
1151 return statusFromErrcode(res);
1152}
1153
1154binder::Status NetdNativeService::tetherInterfaceRemove(const std::string& ifName) {
1155 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1156 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1157 int res = gCtls->tetherCtrl.untetherInterface(ifName.c_str());
1158 gLog.log(entry.returns(res).withAutomaticDuration());
1159 return statusFromErrcode(res);
1160}
1161
1162binder::Status NetdNativeService::tetherInterfaceList(std::vector<std::string>* ifList) {
1163 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1164 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1165 for (const auto& ifname : gCtls->tetherCtrl.getTetheredInterfaceList()) {
1166 ifList->push_back(ifname);
1167 }
1168 gLog.log(entry.returns(true).withAutomaticDuration());
1169 return binder::Status::ok();
1170}
1171
1172binder::Status NetdNativeService::tetherDnsSet(int32_t netId,
1173 const std::vector<std::string>& dnsAddrs) {
1174 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1175 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(dnsAddrs);
1176 int res = gCtls->tetherCtrl.setDnsForwarders(netId, dnsAddrs);
1177 gLog.log(entry.returns(res).withAutomaticDuration());
1178 return statusFromErrcode(res);
1179}
1180
1181binder::Status NetdNativeService::tetherDnsList(std::vector<std::string>* dnsList) {
1182 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1183 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1184 for (const auto& fwdr : gCtls->tetherCtrl.getDnsForwarders()) {
1185 dnsList->push_back(fwdr);
1186 }
1187 gLog.log(entry.returns(true).withAutomaticDuration());
1188 return binder::Status::ok();
1189}
1190
Luke Huangb670d162018-08-23 20:01:13 +08001191binder::Status NetdNativeService::networkAddRoute(int32_t netId, const std::string& ifName,
1192 const std::string& destination,
1193 const std::string& nextHop) {
1194 // Public methods of NetworkController are thread-safe.
1195 ENFORCE_PERMISSION(NETWORK_STACK);
1196 auto entry = gLog.newEntry()
1197 .prettyFunction(__PRETTY_FUNCTION__)
1198 .arg(netId)
1199 .arg(ifName)
1200 .arg(destination)
1201 .arg(nextHop);
1202 bool legacy = false;
1203 uid_t uid = 0; // UID is only meaningful for legacy routes.
1204 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1205 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
1206 gLog.log(entry.returns(res).withAutomaticDuration());
1207 return statusFromErrcode(res);
1208}
1209
1210binder::Status NetdNativeService::networkRemoveRoute(int32_t netId, const std::string& ifName,
1211 const std::string& destination,
1212 const std::string& nextHop) {
1213 ENFORCE_PERMISSION(NETWORK_STACK);
1214 auto entry = gLog.newEntry()
1215 .prettyFunction(__PRETTY_FUNCTION__)
1216 .arg(netId)
1217 .arg(ifName)
1218 .arg(destination)
1219 .arg(nextHop);
1220 bool legacy = false;
1221 uid_t uid = 0; // UID is only meaningful for legacy routes.
1222 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1223 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
1224 gLog.log(entry.returns(res).withAutomaticDuration());
1225 return statusFromErrcode(res);
1226}
1227
1228binder::Status NetdNativeService::networkAddLegacyRoute(int32_t netId, const std::string& ifName,
1229 const std::string& destination,
1230 const std::string& nextHop, int32_t uid) {
1231 ENFORCE_PERMISSION(NETWORK_STACK);
1232 auto entry = gLog.newEntry()
1233 .prettyFunction(__PRETTY_FUNCTION__)
1234 .arg(netId)
1235 .arg(ifName)
1236 .arg(destination)
1237 .arg(nextHop)
1238 .arg(uid);
1239 bool legacy = true;
1240 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1241 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1242 (uid_t) uid);
1243 gLog.log(entry.returns(res).withAutomaticDuration());
1244 return statusFromErrcode(res);
1245}
1246
1247binder::Status NetdNativeService::networkRemoveLegacyRoute(int32_t netId, const std::string& ifName,
1248 const std::string& destination,
1249 const std::string& nextHop,
1250 int32_t uid) {
1251 ENFORCE_PERMISSION(NETWORK_STACK);
1252 auto entry = gLog.newEntry()
1253 .prettyFunction(__PRETTY_FUNCTION__)
1254 .arg(netId)
1255 .arg(ifName)
1256 .arg(destination)
1257 .arg(nextHop)
1258 .arg(uid);
1259 bool legacy = true;
1260 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1261 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1262 (uid_t) uid);
1263 gLog.log(entry.returns(res).withAutomaticDuration());
1264 return statusFromErrcode(res);
1265}
1266
1267binder::Status NetdNativeService::networkGetDefault(int32_t* netId) {
1268 ENFORCE_PERMISSION(NETWORK_STACK);
1269 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1270 *netId = gCtls->netCtrl.getDefaultNetwork();
1271 gLog.log(entry.returns(*netId).withAutomaticDuration());
1272 return binder::Status::ok();
1273}
1274
1275binder::Status NetdNativeService::networkSetDefault(int32_t netId) {
1276 ENFORCE_PERMISSION(NETWORK_STACK);
1277 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId);
1278 int res = gCtls->netCtrl.setDefaultNetwork(netId);
1279 gLog.log(entry.returns(res).withAutomaticDuration());
1280 return statusFromErrcode(res);
1281}
1282
1283binder::Status NetdNativeService::networkClearDefault() {
1284 ENFORCE_PERMISSION(NETWORK_STACK);
1285 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1286 unsigned netId = NETID_UNSET;
1287 int res = gCtls->netCtrl.setDefaultNetwork(netId);
1288 gLog.log(entry.returns(res).withAutomaticDuration());
1289 return statusFromErrcode(res);
1290}
1291
1292std::vector<uid_t> NetdNativeService::intsToUids(const std::vector<int32_t>& intUids) {
1293 return {begin(intUids), end(intUids)};
1294}
1295
1296Permission NetdNativeService::convertPermission(int32_t permission) {
1297 switch (permission) {
1298 case INetd::PERMISSION_NETWORK:
1299 return Permission::PERMISSION_NETWORK;
1300 case INetd::PERMISSION_SYSTEM:
1301 return Permission::PERMISSION_SYSTEM;
1302 default:
1303 return Permission::PERMISSION_NONE;
1304 }
1305}
1306
1307binder::Status NetdNativeService::networkSetPermissionForNetwork(int32_t netId,
1308 int32_t permission) {
1309 ENFORCE_PERMISSION(NETWORK_STACK);
1310 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(permission);
1311 std::vector<unsigned> netIds = {(unsigned) netId};
1312 int res = gCtls->netCtrl.setPermissionForNetworks(convertPermission(permission), netIds);
1313 gLog.log(entry.returns(res).withAutomaticDuration());
1314 return statusFromErrcode(res);
1315}
1316
1317binder::Status NetdNativeService::networkSetPermissionForUser(int32_t permission,
1318 const std::vector<int32_t>& uids) {
1319 ENFORCE_PERMISSION(NETWORK_STACK);
1320 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(permission).arg(uids);
1321 gCtls->netCtrl.setPermissionForUsers(convertPermission(permission), intsToUids(uids));
1322 gLog.log(entry.withAutomaticDuration());
1323 return binder::Status::ok();
1324}
1325
1326binder::Status NetdNativeService::networkClearPermissionForUser(const std::vector<int32_t>& uids) {
1327 ENFORCE_PERMISSION(NETWORK_STACK);
1328 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uids);
1329 Permission permission = Permission::PERMISSION_NONE;
1330 gCtls->netCtrl.setPermissionForUsers(permission, intsToUids(uids));
1331 gLog.log(entry.withAutomaticDuration());
1332 return binder::Status::ok();
1333}
1334
1335binder::Status NetdNativeService::NetdNativeService::networkSetProtectAllow(int32_t uid) {
1336 ENFORCE_PERMISSION(NETWORK_STACK);
1337 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
1338 std::vector<uid_t> uids = {(uid_t) uid};
1339 gCtls->netCtrl.allowProtect(uids);
1340 gLog.log(entry.withAutomaticDuration());
1341 return binder::Status::ok();
1342}
1343
1344binder::Status NetdNativeService::networkSetProtectDeny(int32_t uid) {
1345 ENFORCE_PERMISSION(NETWORK_STACK);
1346 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
1347 std::vector<uid_t> uids = {(uid_t) uid};
1348 gCtls->netCtrl.denyProtect(uids);
1349 gLog.log(entry.withAutomaticDuration());
1350 return binder::Status::ok();
1351}
1352
1353binder::Status NetdNativeService::networkCanProtect(int32_t uid, bool* ret) {
1354 ENFORCE_PERMISSION(NETWORK_STACK);
1355 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
1356 *ret = gCtls->netCtrl.canProtect((uid_t) uid);
1357 gLog.log(entry.returns(*ret).withAutomaticDuration());
1358 return binder::Status::ok();
1359}
1360
Chenbo Feng48eaed32018-12-26 17:40:21 -08001361binder::Status NetdNativeService::trafficSetNetPermForUids(int32_t permission,
1362 const std::vector<int32_t>& uids) {
1363 ENFORCE_PERMISSION(NETWORK_STACK);
1364 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(permission).arg(uids);
1365 gCtls->trafficCtrl.setPermissionForUids(permission, intsToUids(uids));
1366 gLog.log(entry.withAutomaticDuration());
1367 return binder::Status::ok();
1368}
1369
Luke Huange64fa382018-07-24 16:38:22 +08001370namespace {
1371std::string ruleToString(int32_t rule) {
1372 switch (rule) {
1373 case INetd::FIREWALL_RULE_DENY:
1374 return "DENY";
1375 case INetd::FIREWALL_RULE_ALLOW:
1376 return "ALLOW";
1377 default:
1378 return "INVALID";
1379 }
1380}
1381
1382std::string typeToString(int32_t type) {
1383 switch (type) {
1384 case INetd::FIREWALL_WHITELIST:
1385 return "WHITELIST";
1386 case INetd::FIREWALL_BLACKLIST:
1387 return "BLACKLIST";
1388 default:
1389 return "INVALID";
1390 }
1391}
1392
1393std::string chainToString(int32_t chain) {
1394 switch (chain) {
1395 case INetd::FIREWALL_CHAIN_NONE:
1396 return "NONE";
1397 case INetd::FIREWALL_CHAIN_DOZABLE:
1398 return "DOZABLE";
1399 case INetd::FIREWALL_CHAIN_STANDBY:
1400 return "STANDBY";
1401 case INetd::FIREWALL_CHAIN_POWERSAVE:
1402 return "POWERSAVE";
1403 default:
1404 return "INVALID";
1405 }
1406}
1407
1408} // namespace
1409
1410binder::Status NetdNativeService::firewallSetFirewallType(int32_t firewallType) {
1411 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1412 auto entry =
1413 gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(typeToString(firewallType));
1414 auto type = static_cast<FirewallType>(firewallType);
1415
1416 int res = gCtls->firewallCtrl.setFirewallType(type);
1417 gLog.log(entry.returns(res).withAutomaticDuration());
1418 return statusFromErrcode(res);
1419}
1420
1421binder::Status NetdNativeService::firewallSetInterfaceRule(const std::string& ifName,
1422 int32_t firewallRule) {
1423 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1424 auto entry = gLog.newEntry()
1425 .prettyFunction(__PRETTY_FUNCTION__)
1426 .args(ifName, ruleToString(firewallRule));
1427 auto rule = static_cast<FirewallRule>(firewallRule);
1428
1429 int res = gCtls->firewallCtrl.setInterfaceRule(ifName.c_str(), rule);
1430 gLog.log(entry.returns(res).withAutomaticDuration());
1431 return statusFromErrcode(res);
1432}
1433
1434binder::Status NetdNativeService::firewallSetUidRule(int32_t childChain, int32_t uid,
1435 int32_t firewallRule) {
1436 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1437 auto entry = gLog.newEntry()
1438 .prettyFunction(__PRETTY_FUNCTION__)
1439 .args(chainToString(childChain), uid, ruleToString(firewallRule));
1440 auto chain = static_cast<ChildChain>(childChain);
1441 auto rule = static_cast<FirewallRule>(firewallRule);
1442
1443 int res = gCtls->firewallCtrl.setUidRule(chain, uid, rule);
1444 gLog.log(entry.returns(res).withAutomaticDuration());
1445 return statusFromErrcode(res);
1446}
1447
1448binder::Status NetdNativeService::firewallEnableChildChain(int32_t childChain, bool enable) {
1449 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1450 auto entry = gLog.newEntry()
1451 .prettyFunction(__PRETTY_FUNCTION__)
1452 .args(chainToString(childChain), enable);
1453 auto chain = static_cast<ChildChain>(childChain);
1454
1455 int res = gCtls->firewallCtrl.enableChildChains(chain, enable);
1456 gLog.log(entry.returns(res).withAutomaticDuration());
1457 return statusFromErrcode(res);
1458}
1459
Luke Huang19b49c52018-10-22 12:12:05 +09001460binder::Status NetdNativeService::tetherAddForward(const std::string& intIface,
1461 const std::string& extIface) {
1462 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1463 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(intIface, extIface);
1464
1465 int res = gCtls->tetherCtrl.enableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001466 gLog.log(entry.returns(res).withAutomaticDuration());
1467 return statusFromErrcode(res);
1468}
1469
1470binder::Status NetdNativeService::tetherRemoveForward(const std::string& intIface,
1471 const std::string& extIface) {
1472 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
Luke Huang19b49c52018-10-22 12:12:05 +09001473 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(intIface, extIface);
1474
Luke Huangae038f82018-11-05 11:17:31 +09001475 int res = gCtls->tetherCtrl.disableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001476 gLog.log(entry.returns(res).withAutomaticDuration());
1477 return statusFromErrcode(res);
1478}
1479
Chenbo Fengf5663d82018-11-08 16:10:48 -08001480binder::Status NetdNativeService::setTcpRWmemorySize(const std::string& rmemValues,
1481 const std::string& wmemValues) {
1482 ENFORCE_PERMISSION(NETWORK_STACK);
1483 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(rmemValues, wmemValues);
1484 if (!WriteStringToFile(rmemValues, TCP_RMEM_PROC_FILE)) {
1485 int ret = -errno;
1486 gLog.log(entry.returns(ret).withAutomaticDuration());
1487 return statusFromErrcode(ret);
1488 }
1489
1490 if (!WriteStringToFile(wmemValues, TCP_WMEM_PROC_FILE)) {
1491 int ret = -errno;
1492 gLog.log(entry.returns(ret).withAutomaticDuration());
1493 return statusFromErrcode(ret);
1494 }
1495 gLog.log(entry.withAutomaticDuration());
1496 return binder::Status::ok();
1497}
1498
nuccachenf52f7a52018-07-17 18:07:23 +08001499binder::Status NetdNativeService::getPrefix64(int netId, std::string* _aidl_return) {
1500 ENFORCE_PERMISSION(NETWORK_STACK);
1501
1502 netdutils::IPPrefix prefix{};
1503 int err = gCtls->resolverCtrl.getPrefix64(netId, &prefix);
1504 if (err != 0) {
1505 return binder::Status::fromServiceSpecificError(
1506 -err, String8::format("ResolverController error: %s", strerror(-err)));
1507 }
1508 *_aidl_return = prefix.toString();
1509 return binder::Status::ok();
1510}
1511
Luke Huang528af602018-08-29 19:06:05 +08001512binder::Status NetdNativeService::registerUnsolicitedEventListener(
1513 const android::sp<android::net::INetdUnsolicitedEventListener>& listener) {
1514 ENFORCE_PERMISSION(NETWORK_STACK);
Bernie Innocenti9ee088d2019-02-01 17:14:32 +09001515 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1516 gCtls->eventReporter.registerUnsolEventListener(listener);
Luke Huang528af602018-08-29 19:06:05 +08001517 gLog.log(entry.withAutomaticDuration());
1518 return binder::Status::ok();
1519}
1520
Lorenzo Colittie4d626e2016-02-02 17:19:04 +09001521} // namespace net
1522} // namespace android