blob: 10629ef15ed4e11ab6e81c1eba0d86688bea103e [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>
Pierre Imaibeedec32016-04-13 06:44:51 +090024#include <utils/String16.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090025
26#include <binder/IPCThreadState.h>
27#include <binder/IServiceManager.h>
28#include "android/net/BnNetd.h"
29
Lorenzo Colitti89faa342016-02-26 11:38:47 +090030#include "Controllers.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090031#include "DumpWriter.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090032#include "NetdConstants.h"
33#include "NetdNativeService.h"
Robin Leeb8087362016-03-30 18:43:08 +010034#include "RouteController.h"
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090035#include "SockDiag.h"
Robin Leeb8087362016-03-30 18:43:08 +010036#include "UidRanges.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090037
38using android::base::StringPrintf;
39
40namespace android {
41namespace net {
42
43namespace {
44
45const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
Erik Kline2d3a1632016-03-15 16:33:48 +090046const char DUMP[] = "android.permission.DUMP";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090047
48binder::Status checkPermission(const char *permission) {
49 pid_t pid;
50 uid_t uid;
51
52 if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) {
53 return binder::Status::ok();
54 } else {
55 auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
56 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
57 }
58}
59
60#define ENFORCE_PERMISSION(permission) { \
61 binder::Status status = checkPermission((permission)); \
62 if (!status.isOk()) { \
63 return status; \
64 } \
65}
66
Lorenzo Colitti89faa342016-02-26 11:38:47 +090067#define NETD_LOCKING_RPC(permission, lock) \
68 ENFORCE_PERMISSION(permission); \
69 android::RWLock::AutoWLock _lock(lock);
70
71#define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090072} // namespace
73
74
Lorenzo Colittie4851de2016-03-17 13:23:28 +090075status_t NetdNativeService::start() {
76 IPCThreadState::self()->disableBackgroundScheduling(true);
77 status_t ret = BinderService<NetdNativeService>::publish();
78 if (ret != android::OK) {
79 return ret;
80 }
81 sp<ProcessState> ps(ProcessState::self());
82 ps->startThreadPool();
83 ps->giveThreadPoolName();
84 return android::OK;
85}
86
Erik Kline2d3a1632016-03-15 16:33:48 +090087status_t NetdNativeService::dump(int fd, const Vector<String16> & /* args */) {
88 const binder::Status dump_permission = checkPermission(DUMP);
89 if (!dump_permission.isOk()) {
90 const String8 msg(dump_permission.toString8());
91 write(fd, msg.string(), msg.size());
92 return PERMISSION_DENIED;
93 }
94
95 // This method does not grab any locks. If individual classes need locking
96 // their dump() methods MUST handle locking appropriately.
97 DumpWriter dw(fd);
98 dw.blankline();
99 gCtls->netCtrl.dump(dw);
100 dw.blankline();
101
102 return NO_ERROR;
103}
104
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900105binder::Status NetdNativeService::isAlive(bool *alive) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900106 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900107
108 *alive = true;
109 return binder::Status::ok();
110}
111
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900112binder::Status NetdNativeService::firewallReplaceUidChain(const android::String16& chainName,
113 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
114 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock);
115
116 android::String8 name = android::String8(chainName);
117 int err = gCtls->firewallCtrl.replaceUidChain(name.string(), isWhitelist, uids);
118 *ret = (err == 0);
119 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900120}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900121
122binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
123 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->bandwidthCtrl.lock);
124
125 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
126 *ret = (err == 0);
127 return binder::Status::ok();
128}
129
Robin Leeb8087362016-03-30 18:43:08 +0100130binder::Status NetdNativeService::networkRejectNonSecureVpn(bool add,
131 const std::vector<UidRange>& uidRangeArray) {
132 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
133 // it should be possible to use the same lock as NetworkController. However, every call through
134 // the CommandListener "network" command will need to hold this lock too, not just the ones that
135 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
136 // look at routes, but it's not enough here).
137 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
138
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900139 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100140
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 Colitti563d98b2016-04-24 13:13:14 +0900155binder::Status NetdNativeService::socketDestroy(const std::vector<UidRange>& uids,
156 const std::vector<int32_t>& skipUids) {
157
158 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
159
160 SockDiag sd;
161 if (!sd.open()) {
162 return binder::Status::fromServiceSpecificError(EIO,
163 String8("Could not open SOCK_DIAG socket"));
164 }
165
166 UidRanges uidRanges(uids);
167 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()));
168
169 if (err) {
170 return binder::Status::fromServiceSpecificError(-err,
171 String8::format("destroySockets: %s", strerror(-err)));
172 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900173 return binder::Status::ok();
174}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900175
Pierre Imaibeedec32016-04-13 06:44:51 +0900176binder::Status NetdNativeService::setResolverConfiguration(int32_t netId,
177 const std::vector<std::string>& servers, const std::vector<std::string>& domains,
178 const std::vector<int32_t>& params) {
179 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
180 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
181
182 int err = gCtls->resolverCtrl.setResolverConfiguration(netId, servers, domains, params);
183 if (err != 0) {
184 return binder::Status::fromServiceSpecificError(-err,
185 String8::format("ResolverController error: %s", strerror(-err)));
186 }
187 return binder::Status::ok();
188}
189
190binder::Status NetdNativeService::getResolverInfo(int32_t netId,
191 std::vector<std::string>* servers, std::vector<std::string>* domains,
192 std::vector<int32_t>* params, std::vector<int32_t>* stats) {
193 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
194 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
195
196 int err = gCtls->resolverCtrl.getResolverInfo(netId, servers, domains, params, stats);
197 if (err != 0) {
198 return binder::Status::fromServiceSpecificError(-err,
199 String8::format("ResolverController error: %s", strerror(-err)));
200 }
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900201 return binder::Status::ok();
202}
203
Erik Klinef48e4dd2016-07-18 04:02:07 +0900204binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
205 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
206
207 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
208 return binder::Status::ok();
209}
210
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900211} // namespace net
212} // namespace android