blob: 3758991175274d6b5dedce2abccd8fa8dd09573d [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
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090024#include <android-base/stringprintf.h>
Ben Schwartz4204ecf2017-10-02 12:35:48 -040025#include <android-base/strings.h>
Robin Lee2cf56172016-09-13 18:55:42 +090026#include <cutils/properties.h>
Logan Chien3f461482018-04-23 14:31:32 +080027#include <log/log.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090028#include <utils/Errors.h>
Pierre Imaibeedec32016-04-13 06:44:51 +090029#include <utils/String16.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090030
31#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
33#include "android/net/BnNetd.h"
34
Ben Schwartze7601812017-04-28 16:38:29 -040035#include <openssl/base64.h>
36
Lorenzo Colitti89faa342016-02-26 11:38:47 +090037#include "Controllers.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090038#include "DumpWriter.h"
Michal Karpinskid5440112016-10-06 16:56:04 +010039#include "EventReporter.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;
Luke Huangcaebcbb2018-09-27 20:37:14 +080051using android::net::TetherStatsParcel;
Luke Huang94658ac2018-10-18 19:35:12 +090052using android::net::UidRangeParcel;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090053
54namespace android {
55namespace net {
56
57namespace {
58
59const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090060const char NETWORK_STACK[] = "android.permission.NETWORK_STACK";
Erik Kline2d3a1632016-03-15 16:33:48 +090061const char DUMP[] = "android.permission.DUMP";
Erik Klineb31fd692018-06-06 20:50:11 +090062const char OPT_SHORT[] = "--short";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090063
64binder::Status checkPermission(const char *permission) {
Luke Huanga38b65c2018-09-26 16:31:03 +080065 pid_t pid = IPCThreadState::self()->getCallingPid();
66 uid_t uid = IPCThreadState::self()->getCallingUid();
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090067
Luke Huanga38b65c2018-09-26 16:31:03 +080068 // If the caller is the system UID, don't check permissions.
69 // Otherwise, if the system server's binder thread pool is full, and all the threads are
70 // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492
71 //
72 // From a security perspective, there is currently no difference, because:
73 // 1. The only permissions we check in netd's binder interface are CONNECTIVITY_INTERNAL
74 // and NETWORK_STACK, which the system server will always need to have.
75 // 2. AID_SYSTEM always has all permissions. See ActivityManager#checkComponentPermission.
76 if (uid == AID_SYSTEM || checkPermission(String16(permission), pid, uid)) {
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090077 return binder::Status::ok();
78 } else {
79 auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
80 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
81 }
82}
83
Robin Lee2cf56172016-09-13 18:55:42 +090084#define ENFORCE_DEBUGGABLE() { \
85 char value[PROPERTY_VALUE_MAX + 1]; \
Yi Kongbdfd57e2018-07-25 13:26:10 -070086 if (property_get("ro.debuggable", value, nullptr) != 1 \
Robin Lee2cf56172016-09-13 18:55:42 +090087 || value[0] != '1') { \
88 return binder::Status::fromExceptionCode( \
89 binder::Status::EX_SECURITY, \
90 String8("Not available in production builds.") \
91 ); \
92 } \
93}
94
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090095#define ENFORCE_PERMISSION(permission) { \
96 binder::Status status = checkPermission((permission)); \
97 if (!status.isOk()) { \
98 return status; \
99 } \
100}
101
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900102#define NETD_LOCKING_RPC(permission, lock) \
103 ENFORCE_PERMISSION(permission); \
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900104 std::lock_guard _lock(lock);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900105
106#define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900107
Luke Huangf7782042018-08-08 13:13:04 +0800108#define RETURN_BINDER_STATUS_IF_NOT_OK(logEntry, res) \
109 do { \
110 if (!isOk((res))) { \
111 logErrorStatus((logEntry), (res)); \
112 return asBinderStatus((res)); \
113 } \
114 } while (0)
115
116void logErrorStatus(netdutils::LogEntry& logEntry, const netdutils::Status& status) {
117 gLog.log(logEntry.returns(status.code()).withAutomaticDuration());
118}
119
Bernie Innocenti97f388f2018-10-16 19:17:08 +0900120binder::Status asBinderStatus(const netdutils::Status& status) {
121 if (isOk(status)) {
122 return binder::Status::ok();
123 }
124 return binder::Status::fromServiceSpecificError(status.code(), status.msg().c_str());
125}
126
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900127inline binder::Status statusFromErrcode(int ret) {
128 if (ret) {
129 return binder::Status::fromServiceSpecificError(-ret, strerror(-ret));
130 }
131 return binder::Status::ok();
132}
133
Erik Klineb31fd692018-06-06 20:50:11 +0900134bool contains(const Vector<String16>& words, const String16& word) {
135 for (const auto& w : words) {
136 if (w == word) return true;
137 }
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900138
Erik Klineb31fd692018-06-06 20:50:11 +0900139 return false;
140}
141
142} // namespace
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900143
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900144status_t NetdNativeService::start() {
145 IPCThreadState::self()->disableBackgroundScheduling(true);
Erik Klineb31fd692018-06-06 20:50:11 +0900146 const status_t ret = BinderService<NetdNativeService>::publish();
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900147 if (ret != android::OK) {
148 return ret;
149 }
150 sp<ProcessState> ps(ProcessState::self());
151 ps->startThreadPool();
152 ps->giveThreadPoolName();
153 return android::OK;
154}
155
Hugo Benichi7b314e12018-01-15 21:54:00 +0900156status_t NetdNativeService::dump(int fd, const Vector<String16> &args) {
Erik Kline2d3a1632016-03-15 16:33:48 +0900157 const binder::Status dump_permission = checkPermission(DUMP);
158 if (!dump_permission.isOk()) {
159 const String8 msg(dump_permission.toString8());
160 write(fd, msg.string(), msg.size());
161 return PERMISSION_DENIED;
162 }
163
164 // This method does not grab any locks. If individual classes need locking
165 // their dump() methods MUST handle locking appropriately.
Hugo Benichi7b314e12018-01-15 21:54:00 +0900166
Erik Kline2d3a1632016-03-15 16:33:48 +0900167 DumpWriter dw(fd);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900168
169 if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) {
170 dw.blankline();
171 gCtls->tcpSocketMonitor.dump(dw);
172 dw.blankline();
173 return NO_ERROR;
174 }
175
Chenbo Fengef297172018-03-26 10:53:33 -0700176 if (!args.isEmpty() && args[0] == TrafficController::DUMP_KEYWORD) {
177 dw.blankline();
178 gCtls->trafficCtrl.dump(dw, true);
179 dw.blankline();
180 return NO_ERROR;
181 }
182
Erik Kline85890042018-05-25 19:19:11 +0900183 process::dump(dw);
Erik Kline2d3a1632016-03-15 16:33:48 +0900184 dw.blankline();
185 gCtls->netCtrl.dump(dw);
186 dw.blankline();
187
Chenbo Fengef297172018-03-26 10:53:33 -0700188 gCtls->trafficCtrl.dump(dw, false);
189 dw.blankline();
190
Benedict Wongaf855432018-05-10 17:07:37 -0700191 gCtls->xfrmCtrl.dump(dw);
192 dw.blankline();
193
Erik Klineb31fd692018-06-06 20:50:11 +0900194 {
195 ScopedIndent indentLog(dw);
196 if (contains(args, String16(OPT_SHORT))) {
197 dw.println("Log: <omitted>");
198 } else {
199 dw.println("Log:");
200 ScopedIndent indentLogEntries(dw);
201 gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); });
202 }
203 dw.blankline();
204 }
205
Erik Kline2d3a1632016-03-15 16:33:48 +0900206 return NO_ERROR;
207}
208
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900209binder::Status NetdNativeService::isAlive(bool *alive) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900210 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900211 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900212
213 *alive = true;
Erik Klineb31fd692018-06-06 20:50:11 +0900214
215 gLog.log(entry.returns(*alive));
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900216 return binder::Status::ok();
217}
218
Erik Klinef52d4522018-03-14 15:01:46 +0900219binder::Status NetdNativeService::firewallReplaceUidChain(const std::string& chainName,
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900220 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
221 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock);
Erik Klineb31fd692018-06-06 20:50:11 +0900222 auto entry = gLog.newEntry()
223 .prettyFunction(__PRETTY_FUNCTION__)
224 .arg(chainName)
225 .arg(isWhitelist)
226 .arg(uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900227
Erik Klinef52d4522018-03-14 15:01:46 +0900228 int err = gCtls->firewallCtrl.replaceUidChain(chainName, isWhitelist, uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900229 *ret = (err == 0);
Erik Klineb31fd692018-06-06 20:50:11 +0900230
231 gLog.log(entry.returns(*ret).withAutomaticDuration());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900232 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900233}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900234
235binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
236 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->bandwidthCtrl.lock);
Erik Klineb31fd692018-06-06 20:50:11 +0900237 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(enable);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900238
239 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
240 *ret = (err == 0);
Erik Klineb31fd692018-06-06 20:50:11 +0900241 gLog.log(entry.returns(*ret).withAutomaticDuration());
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900242 return binder::Status::ok();
243}
244
Luke Huang531f5d32018-08-03 15:19:05 +0800245binder::Status NetdNativeService::bandwidthSetInterfaceQuota(const std::string& ifName,
246 int64_t bytes) {
247 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
248 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName).arg(bytes);
249
250 int res = gCtls->bandwidthCtrl.setInterfaceQuota(ifName, bytes);
251
252 gLog.log(entry.returns(res).withAutomaticDuration());
253 return statusFromErrcode(res);
254}
255
256binder::Status NetdNativeService::bandwidthRemoveInterfaceQuota(const std::string& ifName) {
257 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
258 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
259
260 int res = gCtls->bandwidthCtrl.removeInterfaceQuota(ifName);
261
262 gLog.log(entry.returns(res).withAutomaticDuration());
263 return statusFromErrcode(res);
264}
265
266binder::Status NetdNativeService::bandwidthSetInterfaceAlert(const std::string& ifName,
267 int64_t bytes) {
268 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
269 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName).arg(bytes);
270
271 int res = gCtls->bandwidthCtrl.setInterfaceAlert(ifName, bytes);
272
273 gLog.log(entry.returns(res).withAutomaticDuration());
274 return statusFromErrcode(res);
275}
276
277binder::Status NetdNativeService::bandwidthRemoveInterfaceAlert(const std::string& ifName) {
278 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
279 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
280
281 int res = gCtls->bandwidthCtrl.removeInterfaceAlert(ifName);
282
283 gLog.log(entry.returns(res).withAutomaticDuration());
284 return statusFromErrcode(res);
285}
286
287binder::Status NetdNativeService::bandwidthSetGlobalAlert(int64_t bytes) {
288 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
289 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(bytes);
290
291 int res = gCtls->bandwidthCtrl.setGlobalAlert(bytes);
292
293 gLog.log(entry.returns(res).withAutomaticDuration());
294 return statusFromErrcode(res);
295}
296
297binder::Status NetdNativeService::bandwidthAddNaughtyApp(int32_t uid) {
298 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
299 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
300
301 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
302 int res = gCtls->bandwidthCtrl.addNaughtyApps(appStrUids);
303
304 gLog.log(entry.returns(res).withAutomaticDuration());
305 return statusFromErrcode(res);
306}
307
308binder::Status NetdNativeService::bandwidthRemoveNaughtyApp(int32_t uid) {
309 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
310 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
311
312 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
313 int res = gCtls->bandwidthCtrl.removeNaughtyApps(appStrUids);
314
315 gLog.log(entry.returns(res).withAutomaticDuration());
316 return statusFromErrcode(res);
317}
318
319binder::Status NetdNativeService::bandwidthAddNiceApp(int32_t uid) {
320 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
321 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
322
323 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
324 int res = gCtls->bandwidthCtrl.addNiceApps(appStrUids);
325
326 gLog.log(entry.returns(res).withAutomaticDuration());
327 return statusFromErrcode(res);
328}
329
330binder::Status NetdNativeService::bandwidthRemoveNiceApp(int32_t uid) {
331 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
332 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
333
334 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
335 int res = gCtls->bandwidthCtrl.removeNiceApps(appStrUids);
336
337 gLog.log(entry.returns(res).withAutomaticDuration());
338 return statusFromErrcode(res);
339}
340
Luke Huangb670d162018-08-23 20:01:13 +0800341binder::Status NetdNativeService::networkCreatePhysical(int32_t netId, int32_t permission) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900342 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900343 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(permission);
Luke Huangb670d162018-08-23 20:01:13 +0800344 int ret = gCtls->netCtrl.createPhysicalNetwork(netId, convertPermission(permission));
Erik Klineb31fd692018-06-06 20:50:11 +0900345 gLog.log(entry.returns(ret).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900346 return statusFromErrcode(ret);
347}
348
349binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool hasDns, bool secure) {
350 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
351 int ret = gCtls->netCtrl.createVirtualNetwork(netId, hasDns, secure);
352 return statusFromErrcode(ret);
353}
354
355binder::Status NetdNativeService::networkDestroy(int32_t netId) {
Erik Klinec8b6a9c2018-01-15 17:06:48 +0900356 ENFORCE_PERMISSION(NETWORK_STACK);
357 // Both of these functions manage their own locking internally.
358 const int ret = gCtls->netCtrl.destroyNetwork(netId);
359 gCtls->resolverCtrl.clearDnsServers(netId);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900360 return statusFromErrcode(ret);
361}
362
363binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
364 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
365 int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
366 return statusFromErrcode(ret);
367}
368
369binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
370 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
371 int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
372 return statusFromErrcode(ret);
373}
374
Luke Huang94658ac2018-10-18 19:35:12 +0900375namespace {
376
377std::string uidRangeParcelVecToString(const std::vector<UidRangeParcel>& uidRangeArray) {
378 std::vector<std::string> result;
379 result.reserve(uidRangeArray.size());
380 for (const auto& uidRange : uidRangeArray) {
381 result.push_back(StringPrintf("%d-%d", uidRange.start, uidRange.stop));
382 }
383
384 return base::Join(result, ", ");
385}
386
387} // namespace
388
389binder::Status NetdNativeService::networkAddUidRanges(
390 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900391 // NetworkController::addUsersToNetwork is thread-safe.
392 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Luke Huang94658ac2018-10-18 19:35:12 +0900393 auto entry = gLog.newEntry()
394 .prettyFunction(__PRETTY_FUNCTION__)
395 .args(netId, uidRangeParcelVecToString(uidRangeArray));
396
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900397 int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray));
Luke Huang94658ac2018-10-18 19:35:12 +0900398 gLog.log(entry.returns(ret).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900399 return statusFromErrcode(ret);
400}
401
Luke Huang94658ac2018-10-18 19:35:12 +0900402binder::Status NetdNativeService::networkRemoveUidRanges(
403 int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900404 // NetworkController::removeUsersFromNetwork is thread-safe.
405 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Luke Huang94658ac2018-10-18 19:35:12 +0900406 auto entry = gLog.newEntry()
407 .prettyFunction(__PRETTY_FUNCTION__)
408 .args(netId, uidRangeParcelVecToString(uidRangeArray));
409
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900410 int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray));
Luke Huang94658ac2018-10-18 19:35:12 +0900411 gLog.log(entry.returns(ret).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900412 return statusFromErrcode(ret);
413}
414
Luke Huang94658ac2018-10-18 19:35:12 +0900415binder::Status NetdNativeService::networkRejectNonSecureVpn(
416 bool add, const std::vector<UidRangeParcel>& uidRangeArray) {
Robin Leeb8087362016-03-30 18:43:08 +0100417 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
418 // it should be possible to use the same lock as NetworkController. However, every call through
419 // the CommandListener "network" command will need to hold this lock too, not just the ones that
420 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
421 // look at routes, but it's not enough here).
422 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
Luke Huang94658ac2018-10-18 19:35:12 +0900423 auto entry = gLog.newEntry()
424 .prettyFunction(__PRETTY_FUNCTION__)
425 .args(add, uidRangeParcelVecToString(uidRangeArray));
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900426 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100427
428 int err;
429 if (add) {
430 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
431 } else {
432 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
433 }
Luke Huang94658ac2018-10-18 19:35:12 +0900434 gLog.log(entry.returns(err).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900435 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100436}
437
Luke Huang94658ac2018-10-18 19:35:12 +0900438binder::Status NetdNativeService::socketDestroy(const std::vector<UidRangeParcel>& uids,
439 const std::vector<int32_t>& skipUids) {
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900440 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
441
Luke Huang94658ac2018-10-18 19:35:12 +0900442 auto entry = gLog.newEntry()
443 .prettyFunction(__PRETTY_FUNCTION__)
444 .arg(uidRangeParcelVecToString(uids));
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900445 SockDiag sd;
446 if (!sd.open()) {
447 return binder::Status::fromServiceSpecificError(EIO,
448 String8("Could not open SOCK_DIAG socket"));
449 }
450
451 UidRanges uidRanges(uids);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +0900452 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
453 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900454
Luke Huang94658ac2018-10-18 19:35:12 +0900455 gLog.log(entry.returns(err).withAutomaticDuration());
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900456 if (err) {
457 return binder::Status::fromServiceSpecificError(-err,
458 String8::format("destroySockets: %s", strerror(-err)));
459 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900460 return binder::Status::ok();
461}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900462
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400463// Parse a base64 encoded string into a vector of bytes.
464// On failure, return an empty vector.
465static std::vector<uint8_t> parseBase64(const std::string& input) {
466 std::vector<uint8_t> decoded;
467 size_t out_len;
468 if (EVP_DecodedLength(&out_len, input.size()) != 1) {
469 return decoded;
470 }
471 // out_len is now an upper bound on the output length.
472 decoded.resize(out_len);
473 if (EVP_DecodeBase64(decoded.data(), &out_len, decoded.size(),
474 reinterpret_cast<const uint8_t*>(input.data()), input.size()) == 1) {
475 // Possibly shrink the vector if the actual output was smaller than the bound.
476 decoded.resize(out_len);
477 } else {
478 decoded.clear();
479 }
480 if (out_len != SHA256_SIZE) {
481 decoded.clear();
482 }
483 return decoded;
484}
485
Pierre Imaibeedec32016-04-13 06:44:51 +0900486binder::Status NetdNativeService::setResolverConfiguration(int32_t netId,
487 const std::vector<std::string>& servers, const std::vector<std::string>& domains,
Erik Klinea1476fb2018-03-04 21:01:56 +0900488 const std::vector<int32_t>& params, const std::string& tlsName,
489 const std::vector<std::string>& tlsServers,
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400490 const std::vector<std::string>& tlsFingerprints) {
Pierre Imaibeedec32016-04-13 06:44:51 +0900491 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
492 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900493 auto entry = gLog.newEntry()
494 .prettyFunction(__PRETTY_FUNCTION__)
495 .arg(netId)
496 .arg(servers)
497 .arg(domains)
498 .arg(params)
499 .arg(tlsName)
500 .arg(tlsServers)
501 .arg(tlsFingerprints);
Pierre Imaibeedec32016-04-13 06:44:51 +0900502
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400503 std::set<std::vector<uint8_t>> decoded_fingerprints;
504 for (const std::string& fingerprint : tlsFingerprints) {
505 std::vector<uint8_t> decoded = parseBase64(fingerprint);
506 if (decoded.empty()) {
507 return binder::Status::fromServiceSpecificError(EINVAL,
508 String8::format("ResolverController error: bad fingerprint"));
509 }
510 decoded_fingerprints.emplace(decoded);
511 }
512
513 int err = gCtls->resolverCtrl.setResolverConfiguration(netId, servers, domains, params,
Erik Klinea1476fb2018-03-04 21:01:56 +0900514 tlsName, tlsServers, decoded_fingerprints);
Erik Klineb31fd692018-06-06 20:50:11 +0900515 gLog.log(entry.returns(err).withAutomaticDuration());
Pierre Imaibeedec32016-04-13 06:44:51 +0900516 if (err != 0) {
517 return binder::Status::fromServiceSpecificError(-err,
518 String8::format("ResolverController error: %s", strerror(-err)));
519 }
520 return binder::Status::ok();
521}
522
523binder::Status NetdNativeService::getResolverInfo(int32_t netId,
524 std::vector<std::string>* servers, std::vector<std::string>* domains,
525 std::vector<int32_t>* params, std::vector<int32_t>* stats) {
526 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
527 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
528
529 int err = gCtls->resolverCtrl.getResolverInfo(netId, servers, domains, params, stats);
530 if (err != 0) {
531 return binder::Status::fromServiceSpecificError(-err,
532 String8::format("ResolverController error: %s", strerror(-err)));
533 }
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900534 return binder::Status::ok();
535}
536
Erik Klinef48e4dd2016-07-18 04:02:07 +0900537binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800538 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
Erik Klinef48e4dd2016-07-18 04:02:07 +0900539
540 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
541 return binder::Status::ok();
542}
543
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900544namespace {
545
Luke Huangcaebcbb2018-09-27 20:37:14 +0800546void tetherAddStatsByInterface(TetherController::TetherStats* tetherStatsParcel,
547 const TetherController::TetherStats& tetherStats) {
548 if (tetherStatsParcel->extIface == tetherStats.extIface) {
549 tetherStatsParcel->rxBytes += tetherStats.rxBytes;
550 tetherStatsParcel->rxPackets += tetherStats.rxPackets;
551 tetherStatsParcel->txBytes += tetherStats.txBytes;
552 tetherStatsParcel->txPackets += tetherStats.txPackets;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900553 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800554}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900555
Luke Huangcaebcbb2018-09-27 20:37:14 +0800556TetherStatsParcel toTetherStatsParcel(const TetherController::TetherStats& stats) {
557 TetherStatsParcel result;
558 result.iface = stats.extIface;
559 result.rxBytes = stats.rxBytes;
560 result.rxPackets = stats.rxPackets;
561 result.txBytes = stats.txBytes;
562 result.txPackets = stats.txPackets;
563 return result;
564}
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900565
Luke Huangcaebcbb2018-09-27 20:37:14 +0800566void setTetherStatsParcelVecByInterface(std::vector<TetherStatsParcel>* tetherStatsVec,
567 const TetherController::TetherStatsList& statsList) {
568 std::map<std::string, TetherController::TetherStats> statsMap;
569 for (const auto& stats : statsList) {
570 auto iter = statsMap.find(stats.extIface);
571 if (iter != statsMap.end()) {
572 tetherAddStatsByInterface(&(iter->second), stats);
573 } else {
574 statsMap.insert(
575 std::pair<std::string, TetherController::TetherStats>(stats.extIface, stats));
576 }
577 }
578 for (auto iter = statsMap.begin(); iter != statsMap.end(); iter++) {
579 tetherStatsVec->push_back(toTetherStatsParcel(iter->second));
580 }
581}
582
583std::vector<std::string> tetherStatsParcelVecToStringVec(std::vector<TetherStatsParcel>* tVec) {
584 std::vector<std::string> result;
585 for (const auto& t : *tVec) {
586 result.push_back(StringPrintf("%s:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64,
587 t.iface.c_str(), t.rxBytes, t.rxPackets, t.txBytes,
588 t.txPackets));
589 }
590 return result;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900591}
592
593} // namespace
594
Luke Huangcaebcbb2018-09-27 20:37:14 +0800595binder::Status NetdNativeService::tetherGetStats(
596 std::vector<TetherStatsParcel>* tetherStatsParcelVec) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800597 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900598
Luke Huangcaebcbb2018-09-27 20:37:14 +0800599 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
600
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900601 const auto& statsList = gCtls->tetherCtrl.getTetherStats();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900602 if (!isOk(statsList)) {
Nathan Harold28ccfef2018-03-16 19:02:47 -0700603 return asBinderStatus(statsList);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900604 }
Luke Huangcaebcbb2018-09-27 20:37:14 +0800605 setTetherStatsParcelVecByInterface(tetherStatsParcelVec, statsList.value());
606 auto statsResults = tetherStatsParcelVecToStringVec(tetherStatsParcelVec);
607 gLog.log(entry.returns(base::Join(statsResults, ";")).withAutomaticDuration());
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900608 return binder::Status::ok();
609}
610
Erik Kline53c20882016-08-02 15:22:53 +0900611binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
612 const std::string &addrString, int prefixLength) {
613 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
614
615 const int err = InterfaceController::addAddress(
616 ifName.c_str(), addrString.c_str(), prefixLength);
617 if (err != 0) {
618 return binder::Status::fromServiceSpecificError(-err,
619 String8::format("InterfaceController error: %s", strerror(-err)));
620 }
621 return binder::Status::ok();
622}
623
624binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
625 const std::string &addrString, int prefixLength) {
626 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
627
628 const int err = InterfaceController::delAddress(
629 ifName.c_str(), addrString.c_str(), prefixLength);
630 if (err != 0) {
631 return binder::Status::fromServiceSpecificError(-err,
632 String8::format("InterfaceController error: %s", strerror(-err)));
633 }
634 return binder::Status::ok();
635}
636
Erik Kline38e51f12018-09-06 20:14:44 +0900637namespace {
Erik Kline55b06f82016-07-04 09:57:18 +0900638
Erik Kline38e51f12018-09-06 20:14:44 +0900639std::tuple<binder::Status, const char*, const char*> getPathComponents(int32_t ipversion,
640 int32_t category) {
641 const char* ipversionStr = nullptr;
642 switch (ipversion) {
Erik Kline55b06f82016-07-04 09:57:18 +0900643 case INetd::IPV4:
Erik Kline38e51f12018-09-06 20:14:44 +0900644 ipversionStr = "ipv4";
Erik Kline55b06f82016-07-04 09:57:18 +0900645 break;
646 case INetd::IPV6:
Erik Kline38e51f12018-09-06 20:14:44 +0900647 ipversionStr = "ipv6";
Erik Kline55b06f82016-07-04 09:57:18 +0900648 break;
649 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900650 return {binder::Status::fromServiceSpecificError(EAFNOSUPPORT, "Bad IP version"),
651 nullptr, nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900652 }
653
Erik Kline38e51f12018-09-06 20:14:44 +0900654 const char* whichStr = nullptr;
655 switch (category) {
Erik Kline55b06f82016-07-04 09:57:18 +0900656 case INetd::CONF:
657 whichStr = "conf";
658 break;
659 case INetd::NEIGH:
660 whichStr = "neigh";
661 break;
662 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900663 return {binder::Status::fromServiceSpecificError(EINVAL, "Bad category"), nullptr,
664 nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900665 }
666
Erik Kline38e51f12018-09-06 20:14:44 +0900667 return {binder::Status::ok(), ipversionStr, whichStr};
668}
669
670} // namespace
671
672binder::Status NetdNativeService::getProcSysNet(int32_t ipversion, int32_t which,
673 const std::string& ifname,
674 const std::string& parameter, std::string* value) {
675 ENFORCE_PERMISSION(NETWORK_STACK);
676 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__)
677 .args(ipversion, which, ifname, parameter);
678
679 const auto pathParts = getPathComponents(ipversion, which);
680 const auto& pathStatus = std::get<0>(pathParts);
681 if (!pathStatus.isOk()) {
682 gLog.log(entry.returns(pathStatus.exceptionCode()).withAutomaticDuration());
683 return pathStatus;
Erik Kline55b06f82016-07-04 09:57:18 +0900684 }
Erik Kline38e51f12018-09-06 20:14:44 +0900685
686 const int err = InterfaceController::getParameter(std::get<1>(pathParts),
687 std::get<2>(pathParts), ifname.c_str(),
688 parameter.c_str(), value);
689 entry.returns(err);
690 if (err == 0) entry.returns(*value);
691 gLog.log(entry.withAutomaticDuration());
692 return statusFromErrcode(err);
693}
694
695binder::Status NetdNativeService::setProcSysNet(int32_t ipversion, int32_t which,
696 const std::string& ifname,
697 const std::string& parameter,
698 const std::string& value) {
699 ENFORCE_PERMISSION(NETWORK_STACK);
700 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__)
701 .args(ipversion, which, ifname, parameter, value);
702
703 const auto pathParts = getPathComponents(ipversion, which);
704 const auto& pathStatus = std::get<0>(pathParts);
705 if (!pathStatus.isOk()) {
706 gLog.log(entry.returns(pathStatus.exceptionCode()).withAutomaticDuration());
707 return pathStatus;
708 }
709
710 const int err = InterfaceController::setParameter(std::get<1>(pathParts),
711 std::get<2>(pathParts), ifname.c_str(),
712 parameter.c_str(), value.c_str());
713 gLog.log(entry.returns(err).withAutomaticDuration());
714 return statusFromErrcode(err);
Erik Kline55b06f82016-07-04 09:57:18 +0900715}
716
Robin Lee2cf56172016-09-13 18:55:42 +0900717binder::Status NetdNativeService::getMetricsReportingLevel(int *reportingLevel) {
718 // This function intentionally does not lock, since the only thing it does is one read from an
719 // atomic_int.
720 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
721 ENFORCE_DEBUGGABLE();
722
Michal Karpinskid5440112016-10-06 16:56:04 +0100723 *reportingLevel = gCtls->eventReporter.getMetricsReportingLevel();
Robin Lee2cf56172016-09-13 18:55:42 +0900724 return binder::Status::ok();
725}
726
727binder::Status NetdNativeService::setMetricsReportingLevel(const int reportingLevel) {
728 // This function intentionally does not lock, since the only thing it does is one write to an
729 // atomic_int.
730 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
731 ENFORCE_DEBUGGABLE();
732
Michal Karpinskid5440112016-10-06 16:56:04 +0100733 return (gCtls->eventReporter.setMetricsReportingLevel(reportingLevel) == 0)
734 ? binder::Status::ok()
735 : binder::Status::fromExceptionCode(binder::Status::EX_ILLEGAL_ARGUMENT);
Robin Lee2cf56172016-09-13 18:55:42 +0900736}
737
Benedict Wongb2daefb2017-12-06 22:05:46 -0800738binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const android::base::unique_fd& socket,
739 int newUid) {
740 ENFORCE_PERMISSION(NETWORK_STACK)
Erik Klineb31fd692018-06-06 20:50:11 +0900741 gLog.log("ipSecSetEncapSocketOwner()");
Benedict Wongb2daefb2017-12-06 22:05:46 -0800742
743 uid_t callerUid = IPCThreadState::self()->getCallingUid();
744 return asBinderStatus(gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket, newUid, callerUid));
745}
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(
794 const android::base::unique_fd& socket,
795 int32_t transformId,
796 int32_t direction,
Nathan Haroldda54f122018-01-09 16:42:57 -0800797 const std::string& sourceAddress,
798 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800799 int32_t spi) {
800 // Necessary locking done in IpSecService and kernel
801 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900802 gLog.log("ipSecApplyTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700803 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Nathan Harold1a371532017-01-30 12:30:48 -0800804 socket,
805 transformId,
806 direction,
Nathan Haroldda54f122018-01-09 16:42:57 -0800807 sourceAddress,
808 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800809 spi));
810}
811
812binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
813 const android::base::unique_fd& socket) {
814 // Necessary locking done in IpSecService and kernel
815 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900816 gLog.log("ipSecRemoveTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700817 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(
Nathan Harold1a371532017-01-30 12:30:48 -0800818 socket));
819}
820
Benedict Wonga04ffa72018-05-09 21:42:42 -0700821binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
822 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700823 const std::string& tmplSrcAddress,
824 const std::string& tmplDstAddress,
825 int32_t spi, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700826 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800827 // Necessary locking done in IpSecService and kernel
828 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900829 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800830 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700831 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700832 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800833}
834
Benedict Wonga450e722018-05-07 10:29:02 -0700835binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(
836 int32_t transformId, int32_t selAddrFamily, int32_t direction,
837 const std::string& tmplSrcAddress, const std::string& tmplDstAddress, int32_t spi,
838 int32_t markValue, int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800839 // Necessary locking done in IpSecService and kernel
840 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900841 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800842 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700843 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700844 markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800845}
846
Benedict Wonga04ffa72018-05-09 21:42:42 -0700847binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId,
848 int32_t selAddrFamily,
849 int32_t direction, int32_t markValue,
Benedict Wonga450e722018-05-07 10:29:02 -0700850 int32_t markMask, int32_t interfaceId) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800851 // Necessary locking done in IpSecService and kernel
852 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900853 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800854 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
Benedict Wonga450e722018-05-07 10:29:02 -0700855 transformId, selAddrFamily, direction, markValue, markMask, interfaceId));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800856}
857
Benedict Wong319f17e2018-05-15 17:06:44 -0700858binder::Status NetdNativeService::ipSecAddTunnelInterface(const std::string& deviceName,
859 const std::string& localAddress,
860 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700861 int32_t iKey, int32_t oKey,
862 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800863 // Necessary locking done in IpSecService and kernel
864 ENFORCE_PERMISSION(NETWORK_STACK);
Benedict Wong319f17e2018-05-15 17:06:44 -0700865 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
manojboopathi8707f232018-01-02 14:45:47 -0800866
Benedict Wong319f17e2018-05-15 17:06:44 -0700867 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700868 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, false);
Benedict Wong319f17e2018-05-15 17:06:44 -0700869 RETURN_BINDER_STATUS_IF_NOT_OK(entry, result);
870
871 gLog.log(entry.returns(result).withAutomaticDuration());
872 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800873}
874
Benedict Wong319f17e2018-05-15 17:06:44 -0700875binder::Status NetdNativeService::ipSecUpdateTunnelInterface(const std::string& deviceName,
876 const std::string& localAddress,
877 const std::string& remoteAddress,
Benedict Wonga450e722018-05-07 10:29:02 -0700878 int32_t iKey, int32_t oKey,
879 int32_t interfaceId) {
manojboopathi8707f232018-01-02 14:45:47 -0800880 // Necessary locking done in IpSecService and kernel
881 ENFORCE_PERMISSION(NETWORK_STACK);
Benedict Wong319f17e2018-05-15 17:06:44 -0700882 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
manojboopathi8707f232018-01-02 14:45:47 -0800883
Benedict Wong319f17e2018-05-15 17:06:44 -0700884 netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface(
Benedict Wonga450e722018-05-07 10:29:02 -0700885 deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, true);
Benedict Wong319f17e2018-05-15 17:06:44 -0700886 RETURN_BINDER_STATUS_IF_NOT_OK(entry, result);
887
888 gLog.log(entry.returns(result).withAutomaticDuration());
889 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800890}
891
Benedict Wong319f17e2018-05-15 17:06:44 -0700892binder::Status NetdNativeService::ipSecRemoveTunnelInterface(const std::string& deviceName) {
manojboopathi8707f232018-01-02 14:45:47 -0800893 // Necessary locking done in IpSecService and kernel
894 ENFORCE_PERMISSION(NETWORK_STACK);
Benedict Wong319f17e2018-05-15 17:06:44 -0700895 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
manojboopathi8707f232018-01-02 14:45:47 -0800896
Benedict Wong319f17e2018-05-15 17:06:44 -0700897 netdutils::Status result = gCtls->xfrmCtrl.ipSecRemoveTunnelInterface(deviceName);
898 RETURN_BINDER_STATUS_IF_NOT_OK(entry, result);
899
900 gLog.log(entry.returns(result).withAutomaticDuration());
901 return binder::Status::ok();
manojboopathi8707f232018-01-02 14:45:47 -0800902}
903
Joel Scherpelzde937962017-06-01 13:20:21 +0900904binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
905 int32_t mode) {
906 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700907 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900908}
909
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900910binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName,
911 const std::string& prefix, int32_t mark,
912 int32_t mask) {
913 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700914 return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900915}
916
917binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
918 const std::string& prefix, int32_t mark,
919 int32_t mask) {
920 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700921 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900922}
923
Chenbo Feng07d43fe2017-12-21 14:38:51 -0800924binder::Status NetdNativeService::trafficCheckBpfStatsEnable(bool* ret) {
925 ENFORCE_PERMISSION(NETWORK_STACK);
926 *ret = gCtls->trafficCtrl.checkBpfStatsEnable();
927 return binder::Status::ok();
928}
929
Luke Huang0051a622018-07-23 20:30:16 +0800930binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout,
931 const std::string& classLabel) {
932 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock);
933 auto entry = gLog.newEntry()
934 .prettyFunction(__PRETTY_FUNCTION__)
935 .arg(ifName)
936 .arg(timeout)
937 .arg(classLabel);
938 int res =
939 gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str());
940 gLog.log(entry.returns(res).withAutomaticDuration());
941 return statusFromErrcode(res);
942}
943
944binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName,
945 int32_t timeout,
946 const std::string& classLabel) {
947 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock);
948 auto entry = gLog.newEntry()
949 .prettyFunction(__PRETTY_FUNCTION__)
950 .arg(ifName)
951 .arg(timeout)
952 .arg(classLabel);
953 int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout,
954 classLabel.c_str());
955 gLog.log(entry.returns(res).withAutomaticDuration());
956 return statusFromErrcode(res);
957}
Luke Huanga67dd562018-07-17 19:58:25 +0800958
959binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) {
960 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->strictCtrl.lock);
961 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid).arg(policyPenalty);
962 StrictPenalty penalty;
963 switch (policyPenalty) {
964 case INetd::PENALTY_POLICY_REJECT:
965 penalty = REJECT;
966 break;
967 case INetd::PENALTY_POLICY_LOG:
968 penalty = LOG;
969 break;
970 case INetd::PENALTY_POLICY_ACCEPT:
971 penalty = ACCEPT;
972 break;
973 default:
974 return statusFromErrcode(-EINVAL);
975 break;
976 }
977 int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty);
978 gLog.log(entry.returns(res).withAutomaticDuration());
979 return statusFromErrcode(res);
980}
Luke Huange64fa382018-07-24 16:38:22 +0800981
Luke Huang6d301232018-08-01 14:05:18 +0800982binder::Status NetdNativeService::clatdStart(const std::string& ifName) {
983 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex);
984 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
985 int res = gCtls->clatdCtrl.startClatd(ifName.c_str());
986 gLog.log(entry.returns(res).withAutomaticDuration());
987 return statusFromErrcode(res);
988}
989
990binder::Status NetdNativeService::clatdStop(const std::string& ifName) {
991 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex);
992 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
993 int res = gCtls->clatdCtrl.stopClatd(ifName.c_str());
994 gLog.log(entry.returns(res).withAutomaticDuration());
995 return statusFromErrcode(res);
996}
Luke Huanga67dd562018-07-17 19:58:25 +0800997
Luke Huang457d4702018-08-16 15:39:15 +0800998binder::Status NetdNativeService::ipfwdEnabled(bool* status) {
999 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1000 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1001 *status = (gCtls->tetherCtrl.forwardingRequestCount() > 0) ? true : false;
1002 gLog.log(entry.returns(*status).withAutomaticDuration());
1003 return binder::Status::ok();
1004}
1005
1006binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) {
1007 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1008 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester);
1009 int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
1010 gLog.log(entry.returns(res).withAutomaticDuration());
1011 return statusFromErrcode(res);
1012}
1013
1014binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) {
1015 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1016 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester);
1017 int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
1018 gLog.log(entry.returns(res).withAutomaticDuration());
1019 return statusFromErrcode(res);
1020}
1021
1022binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface,
1023 const std::string& toIface) {
1024 ENFORCE_PERMISSION(NETWORK_STACK);
1025 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface);
1026 int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str());
1027 gLog.log(entry.returns(res).withAutomaticDuration());
1028 return statusFromErrcode(res);
1029}
1030
1031binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface,
1032 const std::string& toIface) {
1033 ENFORCE_PERMISSION(NETWORK_STACK);
1034 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface);
1035 int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str());
1036 gLog.log(entry.returns(res).withAutomaticDuration());
1037 return statusFromErrcode(res);
1038}
1039
Luke Huangf7782042018-08-08 13:13:04 +08001040namespace {
1041std::string addSquareBrackets(const std::string& s) {
1042 return "[" + s + "]";
1043}
1044
1045std::string addCurlyBrackets(const std::string& s) {
1046 return "{" + s + "}";
1047}
1048
1049} // namespace
1050binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) {
1051 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1052 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1053
1054 const auto& ifaceList = InterfaceController::getIfaceNames();
1055 RETURN_BINDER_STATUS_IF_NOT_OK(entry, ifaceList);
1056
1057 interfaceListResult->clear();
1058 interfaceListResult->reserve(ifaceList.value().size());
1059 interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()),
1060 end(ifaceList.value()));
1061
1062 gLog.log(entry.returns(addSquareBrackets(base::Join(*interfaceListResult, ", ")))
1063 .withAutomaticDuration());
1064 return binder::Status::ok();
1065}
1066
1067std::string interfaceConfigurationParcelToString(const InterfaceConfigurationParcel& cfg) {
1068 std::vector<std::string> result{cfg.ifName, cfg.hwAddr, cfg.ipv4Addr,
1069 std::to_string(cfg.prefixLength)};
1070 result.insert(end(result), begin(cfg.flags), end(cfg.flags));
1071 return addCurlyBrackets(base::Join(result, ", "));
1072}
1073
1074binder::Status NetdNativeService::interfaceGetCfg(
1075 const std::string& ifName, InterfaceConfigurationParcel* interfaceGetCfgResult) {
1076 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1077 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1078
1079 const auto& cfgRes = InterfaceController::getCfg(ifName);
1080 RETURN_BINDER_STATUS_IF_NOT_OK(entry, cfgRes);
1081
1082 *interfaceGetCfgResult = cfgRes.value();
1083 gLog.log(entry.returns(interfaceConfigurationParcelToString(*interfaceGetCfgResult))
1084 .withAutomaticDuration());
1085 return binder::Status::ok();
1086}
1087
1088binder::Status NetdNativeService::interfaceSetCfg(const InterfaceConfigurationParcel& cfg) {
1089 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1090 auto entry = gLog.newEntry()
1091 .prettyFunction(__PRETTY_FUNCTION__)
1092 .arg(interfaceConfigurationParcelToString(cfg));
1093
1094 const auto& res = InterfaceController::setCfg(cfg);
1095 RETURN_BINDER_STATUS_IF_NOT_OK(entry, res);
1096
1097 gLog.log(entry.withAutomaticDuration());
1098 return binder::Status::ok();
1099}
1100
1101binder::Status NetdNativeService::interfaceSetIPv6PrivacyExtensions(const std::string& ifName,
1102 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::setIPv6PrivacyExtensions(ifName.c_str(), enable);
1106 gLog.log(entry.returns(res).withAutomaticDuration());
1107 return statusFromErrcode(res);
1108}
1109
1110binder::Status NetdNativeService::interfaceClearAddrs(const std::string& ifName) {
1111 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1112 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1113 int res = InterfaceController::clearAddrs(ifName.c_str());
1114 gLog.log(entry.returns(res).withAutomaticDuration());
1115 return statusFromErrcode(res);
1116}
1117
1118binder::Status NetdNativeService::interfaceSetEnableIPv6(const std::string& ifName, bool enable) {
1119 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1120 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(ifName, enable);
1121 int res = InterfaceController::setEnableIPv6(ifName.c_str(), enable);
1122 gLog.log(entry.returns(res).withAutomaticDuration());
1123 return statusFromErrcode(res);
1124}
1125
1126binder::Status NetdNativeService::interfaceSetMtu(const std::string& ifName, int32_t mtuValue) {
1127 NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex);
1128 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(ifName, mtuValue);
1129 std::string mtu = std::to_string(mtuValue);
1130 int res = InterfaceController::setMtu(ifName.c_str(), mtu.c_str());
1131 gLog.log(entry.returns(res).withAutomaticDuration());
1132 return statusFromErrcode(res);
1133}
1134
Luke Huangb5733d72018-08-21 17:17:19 +08001135binder::Status NetdNativeService::tetherStart(const std::vector<std::string>& dhcpRanges) {
1136 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1137 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(dhcpRanges);
1138 if (dhcpRanges.size() % 2 == 1) {
1139 return statusFromErrcode(-EINVAL);
1140 }
1141 int res = gCtls->tetherCtrl.startTethering(dhcpRanges);
1142 gLog.log(entry.returns(res).withAutomaticDuration());
1143 return statusFromErrcode(res);
1144}
1145
1146binder::Status NetdNativeService::tetherStop() {
1147 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1148 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1149 int res = gCtls->tetherCtrl.stopTethering();
1150 gLog.log(entry.returns(res).withAutomaticDuration());
1151 return statusFromErrcode(res);
1152}
1153
1154binder::Status NetdNativeService::tetherIsEnabled(bool* enabled) {
1155 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1156 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1157 *enabled = gCtls->tetherCtrl.isTetheringStarted();
1158 gLog.log(entry.returns(*enabled).withAutomaticDuration());
1159 return binder::Status::ok();
1160}
1161
1162binder::Status NetdNativeService::tetherInterfaceAdd(const std::string& ifName) {
1163 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1164 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1165 int res = gCtls->tetherCtrl.tetherInterface(ifName.c_str());
1166 gLog.log(entry.returns(res).withAutomaticDuration());
1167 return statusFromErrcode(res);
1168}
1169
1170binder::Status NetdNativeService::tetherInterfaceRemove(const std::string& ifName) {
1171 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1172 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
1173 int res = gCtls->tetherCtrl.untetherInterface(ifName.c_str());
1174 gLog.log(entry.returns(res).withAutomaticDuration());
1175 return statusFromErrcode(res);
1176}
1177
1178binder::Status NetdNativeService::tetherInterfaceList(std::vector<std::string>* ifList) {
1179 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1180 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1181 for (const auto& ifname : gCtls->tetherCtrl.getTetheredInterfaceList()) {
1182 ifList->push_back(ifname);
1183 }
1184 gLog.log(entry.returns(true).withAutomaticDuration());
1185 return binder::Status::ok();
1186}
1187
1188binder::Status NetdNativeService::tetherDnsSet(int32_t netId,
1189 const std::vector<std::string>& dnsAddrs) {
1190 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1191 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(dnsAddrs);
1192 int res = gCtls->tetherCtrl.setDnsForwarders(netId, dnsAddrs);
1193 gLog.log(entry.returns(res).withAutomaticDuration());
1194 return statusFromErrcode(res);
1195}
1196
1197binder::Status NetdNativeService::tetherDnsList(std::vector<std::string>* dnsList) {
1198 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1199 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1200 for (const auto& fwdr : gCtls->tetherCtrl.getDnsForwarders()) {
1201 dnsList->push_back(fwdr);
1202 }
1203 gLog.log(entry.returns(true).withAutomaticDuration());
1204 return binder::Status::ok();
1205}
1206
Luke Huangb670d162018-08-23 20:01:13 +08001207binder::Status NetdNativeService::networkAddRoute(int32_t netId, const std::string& ifName,
1208 const std::string& destination,
1209 const std::string& nextHop) {
1210 // Public methods of NetworkController are thread-safe.
1211 ENFORCE_PERMISSION(NETWORK_STACK);
1212 auto entry = gLog.newEntry()
1213 .prettyFunction(__PRETTY_FUNCTION__)
1214 .arg(netId)
1215 .arg(ifName)
1216 .arg(destination)
1217 .arg(nextHop);
1218 bool legacy = false;
1219 uid_t uid = 0; // UID is only meaningful for legacy routes.
1220 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1221 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
1222 gLog.log(entry.returns(res).withAutomaticDuration());
1223 return statusFromErrcode(res);
1224}
1225
1226binder::Status NetdNativeService::networkRemoveRoute(int32_t netId, const std::string& ifName,
1227 const std::string& destination,
1228 const std::string& nextHop) {
1229 ENFORCE_PERMISSION(NETWORK_STACK);
1230 auto entry = gLog.newEntry()
1231 .prettyFunction(__PRETTY_FUNCTION__)
1232 .arg(netId)
1233 .arg(ifName)
1234 .arg(destination)
1235 .arg(nextHop);
1236 bool legacy = false;
1237 uid_t uid = 0; // UID is only meaningful for legacy routes.
1238 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1239 nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
1240 gLog.log(entry.returns(res).withAutomaticDuration());
1241 return statusFromErrcode(res);
1242}
1243
1244binder::Status NetdNativeService::networkAddLegacyRoute(int32_t netId, const std::string& ifName,
1245 const std::string& destination,
1246 const std::string& nextHop, int32_t uid) {
1247 ENFORCE_PERMISSION(NETWORK_STACK);
1248 auto entry = gLog.newEntry()
1249 .prettyFunction(__PRETTY_FUNCTION__)
1250 .arg(netId)
1251 .arg(ifName)
1252 .arg(destination)
1253 .arg(nextHop)
1254 .arg(uid);
1255 bool legacy = true;
1256 int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1257 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1258 (uid_t) uid);
1259 gLog.log(entry.returns(res).withAutomaticDuration());
1260 return statusFromErrcode(res);
1261}
1262
1263binder::Status NetdNativeService::networkRemoveLegacyRoute(int32_t netId, const std::string& ifName,
1264 const std::string& destination,
1265 const std::string& nextHop,
1266 int32_t uid) {
1267 ENFORCE_PERMISSION(NETWORK_STACK);
1268 auto entry = gLog.newEntry()
1269 .prettyFunction(__PRETTY_FUNCTION__)
1270 .arg(netId)
1271 .arg(ifName)
1272 .arg(destination)
1273 .arg(nextHop)
1274 .arg(uid);
1275 bool legacy = true;
1276 int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1277 nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1278 (uid_t) uid);
1279 gLog.log(entry.returns(res).withAutomaticDuration());
1280 return statusFromErrcode(res);
1281}
1282
1283binder::Status NetdNativeService::networkGetDefault(int32_t* netId) {
1284 ENFORCE_PERMISSION(NETWORK_STACK);
1285 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1286 *netId = gCtls->netCtrl.getDefaultNetwork();
1287 gLog.log(entry.returns(*netId).withAutomaticDuration());
1288 return binder::Status::ok();
1289}
1290
1291binder::Status NetdNativeService::networkSetDefault(int32_t netId) {
1292 ENFORCE_PERMISSION(NETWORK_STACK);
1293 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId);
1294 int res = gCtls->netCtrl.setDefaultNetwork(netId);
1295 gLog.log(entry.returns(res).withAutomaticDuration());
1296 return statusFromErrcode(res);
1297}
1298
1299binder::Status NetdNativeService::networkClearDefault() {
1300 ENFORCE_PERMISSION(NETWORK_STACK);
1301 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
1302 unsigned netId = NETID_UNSET;
1303 int res = gCtls->netCtrl.setDefaultNetwork(netId);
1304 gLog.log(entry.returns(res).withAutomaticDuration());
1305 return statusFromErrcode(res);
1306}
1307
1308std::vector<uid_t> NetdNativeService::intsToUids(const std::vector<int32_t>& intUids) {
1309 return {begin(intUids), end(intUids)};
1310}
1311
1312Permission NetdNativeService::convertPermission(int32_t permission) {
1313 switch (permission) {
1314 case INetd::PERMISSION_NETWORK:
1315 return Permission::PERMISSION_NETWORK;
1316 case INetd::PERMISSION_SYSTEM:
1317 return Permission::PERMISSION_SYSTEM;
1318 default:
1319 return Permission::PERMISSION_NONE;
1320 }
1321}
1322
1323binder::Status NetdNativeService::networkSetPermissionForNetwork(int32_t netId,
1324 int32_t permission) {
1325 ENFORCE_PERMISSION(NETWORK_STACK);
1326 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(permission);
1327 std::vector<unsigned> netIds = {(unsigned) netId};
1328 int res = gCtls->netCtrl.setPermissionForNetworks(convertPermission(permission), netIds);
1329 gLog.log(entry.returns(res).withAutomaticDuration());
1330 return statusFromErrcode(res);
1331}
1332
1333binder::Status NetdNativeService::networkSetPermissionForUser(int32_t permission,
1334 const std::vector<int32_t>& uids) {
1335 ENFORCE_PERMISSION(NETWORK_STACK);
1336 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(permission).arg(uids);
1337 gCtls->netCtrl.setPermissionForUsers(convertPermission(permission), intsToUids(uids));
1338 gLog.log(entry.withAutomaticDuration());
1339 return binder::Status::ok();
1340}
1341
1342binder::Status NetdNativeService::networkClearPermissionForUser(const std::vector<int32_t>& uids) {
1343 ENFORCE_PERMISSION(NETWORK_STACK);
1344 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uids);
1345 Permission permission = Permission::PERMISSION_NONE;
1346 gCtls->netCtrl.setPermissionForUsers(permission, intsToUids(uids));
1347 gLog.log(entry.withAutomaticDuration());
1348 return binder::Status::ok();
1349}
1350
1351binder::Status NetdNativeService::NetdNativeService::networkSetProtectAllow(int32_t uid) {
1352 ENFORCE_PERMISSION(NETWORK_STACK);
1353 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
1354 std::vector<uid_t> uids = {(uid_t) uid};
1355 gCtls->netCtrl.allowProtect(uids);
1356 gLog.log(entry.withAutomaticDuration());
1357 return binder::Status::ok();
1358}
1359
1360binder::Status NetdNativeService::networkSetProtectDeny(int32_t uid) {
1361 ENFORCE_PERMISSION(NETWORK_STACK);
1362 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
1363 std::vector<uid_t> uids = {(uid_t) uid};
1364 gCtls->netCtrl.denyProtect(uids);
1365 gLog.log(entry.withAutomaticDuration());
1366 return binder::Status::ok();
1367}
1368
1369binder::Status NetdNativeService::networkCanProtect(int32_t uid, bool* ret) {
1370 ENFORCE_PERMISSION(NETWORK_STACK);
1371 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
1372 *ret = gCtls->netCtrl.canProtect((uid_t) uid);
1373 gLog.log(entry.returns(*ret).withAutomaticDuration());
1374 return binder::Status::ok();
1375}
1376
Luke Huange64fa382018-07-24 16:38:22 +08001377namespace {
1378std::string ruleToString(int32_t rule) {
1379 switch (rule) {
1380 case INetd::FIREWALL_RULE_DENY:
1381 return "DENY";
1382 case INetd::FIREWALL_RULE_ALLOW:
1383 return "ALLOW";
1384 default:
1385 return "INVALID";
1386 }
1387}
1388
1389std::string typeToString(int32_t type) {
1390 switch (type) {
1391 case INetd::FIREWALL_WHITELIST:
1392 return "WHITELIST";
1393 case INetd::FIREWALL_BLACKLIST:
1394 return "BLACKLIST";
1395 default:
1396 return "INVALID";
1397 }
1398}
1399
1400std::string chainToString(int32_t chain) {
1401 switch (chain) {
1402 case INetd::FIREWALL_CHAIN_NONE:
1403 return "NONE";
1404 case INetd::FIREWALL_CHAIN_DOZABLE:
1405 return "DOZABLE";
1406 case INetd::FIREWALL_CHAIN_STANDBY:
1407 return "STANDBY";
1408 case INetd::FIREWALL_CHAIN_POWERSAVE:
1409 return "POWERSAVE";
1410 default:
1411 return "INVALID";
1412 }
1413}
1414
1415} // namespace
1416
1417binder::Status NetdNativeService::firewallSetFirewallType(int32_t firewallType) {
1418 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1419 auto entry =
1420 gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(typeToString(firewallType));
1421 auto type = static_cast<FirewallType>(firewallType);
1422
1423 int res = gCtls->firewallCtrl.setFirewallType(type);
1424 gLog.log(entry.returns(res).withAutomaticDuration());
1425 return statusFromErrcode(res);
1426}
1427
1428binder::Status NetdNativeService::firewallSetInterfaceRule(const std::string& ifName,
1429 int32_t firewallRule) {
1430 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1431 auto entry = gLog.newEntry()
1432 .prettyFunction(__PRETTY_FUNCTION__)
1433 .args(ifName, ruleToString(firewallRule));
1434 auto rule = static_cast<FirewallRule>(firewallRule);
1435
1436 int res = gCtls->firewallCtrl.setInterfaceRule(ifName.c_str(), rule);
1437 gLog.log(entry.returns(res).withAutomaticDuration());
1438 return statusFromErrcode(res);
1439}
1440
1441binder::Status NetdNativeService::firewallSetUidRule(int32_t childChain, int32_t uid,
1442 int32_t firewallRule) {
1443 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1444 auto entry = gLog.newEntry()
1445 .prettyFunction(__PRETTY_FUNCTION__)
1446 .args(chainToString(childChain), uid, ruleToString(firewallRule));
1447 auto chain = static_cast<ChildChain>(childChain);
1448 auto rule = static_cast<FirewallRule>(firewallRule);
1449
1450 int res = gCtls->firewallCtrl.setUidRule(chain, uid, rule);
1451 gLog.log(entry.returns(res).withAutomaticDuration());
1452 return statusFromErrcode(res);
1453}
1454
1455binder::Status NetdNativeService::firewallEnableChildChain(int32_t childChain, bool enable) {
1456 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock);
1457 auto entry = gLog.newEntry()
1458 .prettyFunction(__PRETTY_FUNCTION__)
1459 .args(chainToString(childChain), enable);
1460 auto chain = static_cast<ChildChain>(childChain);
1461
1462 int res = gCtls->firewallCtrl.enableChildChains(chain, enable);
1463 gLog.log(entry.returns(res).withAutomaticDuration());
1464 return statusFromErrcode(res);
1465}
1466
Luke Huang19b49c52018-10-22 12:12:05 +09001467binder::Status NetdNativeService::tetherAddForward(const std::string& intIface,
1468 const std::string& extIface) {
1469 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
1470 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(intIface, extIface);
1471
1472 int res = gCtls->tetherCtrl.enableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001473 gLog.log(entry.returns(res).withAutomaticDuration());
1474 return statusFromErrcode(res);
1475}
1476
1477binder::Status NetdNativeService::tetherRemoveForward(const std::string& intIface,
1478 const std::string& extIface) {
1479 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
Luke Huang19b49c52018-10-22 12:12:05 +09001480 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(intIface, extIface);
1481
Luke Huangae038f82018-11-05 11:17:31 +09001482 int res = gCtls->tetherCtrl.disableNat(intIface.c_str(), extIface.c_str());
Luke Huang19b49c52018-10-22 12:12:05 +09001483 gLog.log(entry.returns(res).withAutomaticDuration());
1484 return statusFromErrcode(res);
1485}
1486
Lorenzo Colittie4d626e2016-02-02 17:19:04 +09001487} // namespace net
1488} // namespace android