blob: a8f5c3b21c130484ba3e2931e4be84f6940eedfc [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"
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090034#include "SockDiag.h"
Robin Leeb8087362016-03-30 18:43:08 +010035#include "UidRanges.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090036
37using android::base::StringPrintf;
38
39namespace android {
40namespace net {
41
42namespace {
43
44const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
Erik Kline2d3a1632016-03-15 16:33:48 +090045const char DUMP[] = "android.permission.DUMP";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090046
47binder::Status checkPermission(const char *permission) {
48 pid_t pid;
49 uid_t uid;
50
51 if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) {
52 return binder::Status::ok();
53 } else {
54 auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
55 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
56 }
57}
58
59#define ENFORCE_PERMISSION(permission) { \
60 binder::Status status = checkPermission((permission)); \
61 if (!status.isOk()) { \
62 return status; \
63 } \
64}
65
Lorenzo Colitti89faa342016-02-26 11:38:47 +090066#define NETD_LOCKING_RPC(permission, lock) \
67 ENFORCE_PERMISSION(permission); \
68 android::RWLock::AutoWLock _lock(lock);
69
70#define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090071} // 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
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900138 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100139
140 int err;
141 if (add) {
142 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
143 } else {
144 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
145 }
146
147 if (err != 0) {
148 return binder::Status::fromServiceSpecificError(-err,
149 String8::format("RouteController error: %s", strerror(-err)));
150 }
151 return binder::Status::ok();
152}
153
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900154binder::Status NetdNativeService::socketDestroy(const std::vector<UidRange>& uids,
155 const std::vector<int32_t>& skipUids) {
156
157 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
158
159 SockDiag sd;
160 if (!sd.open()) {
161 return binder::Status::fromServiceSpecificError(EIO,
162 String8("Could not open SOCK_DIAG socket"));
163 }
164
165 UidRanges uidRanges(uids);
166 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()));
167
168 if (err) {
169 return binder::Status::fromServiceSpecificError(-err,
170 String8::format("destroySockets: %s", strerror(-err)));
171 }
172
173 return binder::Status::ok();
174}
175
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900176} // namespace net
177} // namespace android