blob: 388b6b59146dbed5a34e89ca1fdada7c6113408e [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
Lorenzo Colitti89faa342016-02-26 11:38:47 +090019#include <vector>
20
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090021#include <android-base/stringprintf.h>
22#include <cutils/log.h>
23#include <utils/Errors.h>
24
25#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
27#include "android/net/BnNetd.h"
28
Lorenzo Colitti89faa342016-02-26 11:38:47 +090029#include "Controllers.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090030#include "DumpWriter.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090031#include "NetdConstants.h"
32#include "NetdNativeService.h"
Robin Leeb8087362016-03-30 18:43:08 +010033#include "RouteController.h"
34#include "UidRanges.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090035
36using android::base::StringPrintf;
37
38namespace android {
39namespace net {
40
41namespace {
42
43const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
Erik Kline2d3a1632016-03-15 16:33:48 +090044const char DUMP[] = "android.permission.DUMP";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090045
46binder::Status checkPermission(const char *permission) {
47 pid_t pid;
48 uid_t uid;
49
50 if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) {
51 return binder::Status::ok();
52 } else {
53 auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
54 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
55 }
56}
57
58#define ENFORCE_PERMISSION(permission) { \
59 binder::Status status = checkPermission((permission)); \
60 if (!status.isOk()) { \
61 return status; \
62 } \
63}
64
Lorenzo Colitti89faa342016-02-26 11:38:47 +090065#define NETD_LOCKING_RPC(permission, lock) \
66 ENFORCE_PERMISSION(permission); \
67 android::RWLock::AutoWLock _lock(lock);
68
69#define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090070
71} // namespace
72
73
Lorenzo Colittie4851de2016-03-17 13:23:28 +090074status_t NetdNativeService::start() {
75 IPCThreadState::self()->disableBackgroundScheduling(true);
76 status_t ret = BinderService<NetdNativeService>::publish();
77 if (ret != android::OK) {
78 return ret;
79 }
80 sp<ProcessState> ps(ProcessState::self());
81 ps->startThreadPool();
82 ps->giveThreadPoolName();
83 return android::OK;
84}
85
Erik Kline2d3a1632016-03-15 16:33:48 +090086status_t NetdNativeService::dump(int fd, const Vector<String16> & /* args */) {
87 const binder::Status dump_permission = checkPermission(DUMP);
88 if (!dump_permission.isOk()) {
89 const String8 msg(dump_permission.toString8());
90 write(fd, msg.string(), msg.size());
91 return PERMISSION_DENIED;
92 }
93
94 // This method does not grab any locks. If individual classes need locking
95 // their dump() methods MUST handle locking appropriately.
96 DumpWriter dw(fd);
97 dw.blankline();
98 gCtls->netCtrl.dump(dw);
99 dw.blankline();
100
101 return NO_ERROR;
102}
103
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900104binder::Status NetdNativeService::isAlive(bool *alive) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900105 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900106
107 *alive = true;
108 return binder::Status::ok();
109}
110
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900111binder::Status NetdNativeService::firewallReplaceUidChain(const android::String16& chainName,
112 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
113 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock);
114
115 android::String8 name = android::String8(chainName);
116 int err = gCtls->firewallCtrl.replaceUidChain(name.string(), isWhitelist, uids);
117 *ret = (err == 0);
118 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900119}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900120
121binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
122 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->bandwidthCtrl.lock);
123
124 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
125 *ret = (err == 0);
126 return binder::Status::ok();
127}
128
Robin Leeb8087362016-03-30 18:43:08 +0100129binder::Status NetdNativeService::networkRejectNonSecureVpn(bool add,
130 const std::vector<UidRange>& uidRangeArray) {
131 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
132 // it should be possible to use the same lock as NetworkController. However, every call through
133 // the CommandListener "network" command will need to hold this lock too, not just the ones that
134 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
135 // look at routes, but it's not enough here).
136 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
137
138 UidRanges uidRanges;
139 uidRanges.createFrom(uidRangeArray);
140
141 int err;
142 if (add) {
143 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
144 } else {
145 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
146 }
147
148 if (err != 0) {
149 return binder::Status::fromServiceSpecificError(-err,
150 String8::format("RouteController error: %s", strerror(-err)));
151 }
152 return binder::Status::ok();
153}
154
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900155} // namespace net
156} // namespace android