blob: 3364577cc3e7bcb8b6f5b668015eed4a3415366d [file] [log] [blame]
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -05001/*
2 * Copyright (C) 2014 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
Sreeram Ramachandran72604072014-05-21 13:19:43 -070017// THREAD-SAFETY
18// -------------
19// The methods in this file are called from multiple threads (from CommandListener, FwmarkServer
20// and DnsProxyListener). So, all accesses to shared state are guarded by a lock.
21//
22// In some cases, a single non-const method acquires and releases the lock several times, like so:
23// if (isValidNetwork(...)) { // isValidNetwork() acquires and releases the lock.
24// setDefaultNetwork(...); // setDefaultNetwork() also acquires and releases the lock.
25//
26// It might seem that this allows races where the state changes between the two statements, but in
27// fact there are no races because:
28// 1. This pattern only occurs in non-const methods (i.e., those that mutate state).
29// 2. Only CommandListener calls these non-const methods. The others call only const methods.
30// 3. CommandListener only processes one command at a time. I.e., it's serialized.
31// Thus, no other mutation can occur in between the two statements above.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050032
33#include "NetworkController.h"
34
Erik Kline2d3a1632016-03-15 16:33:48 +090035#define LOG_TAG "Netd"
36#include "log/log.h"
37
38#include "cutils/misc.h"
39#include "resolv_netid.h"
40
Pierre Imai3a272072016-04-19 16:17:07 +090041#include "Controllers.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090042#include "DummyNetwork.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090043#include "DumpWriter.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070044#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070045#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070046#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070047#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070048#include "VirtualNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070049
50namespace {
51
52// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070053const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070054const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070055
56} // namespace
57
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070058const unsigned NetworkController::MIN_OEM_ID = 1;
59const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090060const unsigned NetworkController::DUMMY_NET_ID = 51;
61// NetIds 52..98 are reserved for future use.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070062const unsigned NetworkController::LOCAL_NET_ID = 99;
63
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070064// All calls to methods here are made while holding a write lock on mRWLock.
65class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
66public:
67 explicit DelegateImpl(NetworkController* networkController);
68 virtual ~DelegateImpl();
69
70 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
71 Permission permission, bool add) WARN_UNUSED_RESULT;
72
73private:
74 int addFallthrough(const std::string& physicalInterface,
75 Permission permission) override WARN_UNUSED_RESULT;
76 int removeFallthrough(const std::string& physicalInterface,
77 Permission permission) override WARN_UNUSED_RESULT;
78
79 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
80 bool add) WARN_UNUSED_RESULT;
81
82 NetworkController* const mNetworkController;
83};
84
85NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
86 mNetworkController(networkController) {
87}
88
89NetworkController::DelegateImpl::~DelegateImpl() {
90}
91
92int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
93 const std::string& physicalInterface,
94 Permission permission, bool add) {
95 if (add) {
96 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
97 physicalInterface.c_str(),
98 permission)) {
99 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
100 vpnNetId);
101 return ret;
102 }
103 } else {
104 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
105 physicalInterface.c_str(),
106 permission)) {
107 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
108 vpnNetId);
109 return ret;
110 }
111 }
112 return 0;
113}
114
115int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
116 Permission permission) {
117 return modifyFallthrough(physicalInterface, permission, true);
118}
119
120int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
121 Permission permission) {
122 return modifyFallthrough(physicalInterface, permission, false);
123}
124
125int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
126 Permission permission, bool add) {
127 for (const auto& entry : mNetworkController->mNetworks) {
128 if (entry.second->getType() == Network::VIRTUAL) {
129 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
130 return ret;
131 }
132 }
133 }
134 return 0;
135}
136
137NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900138 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
139 mProtectableUsers({AID_VPN}) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700140 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900141 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500142}
143
144unsigned NetworkController::getDefaultNetwork() const {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700145 android::RWLock::AutoRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500146 return mDefaultNetId;
147}
148
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700149int NetworkController::setDefaultNetwork(unsigned netId) {
150 android::RWLock::AutoWLock lock(mRWLock);
151
152 if (netId == mDefaultNetId) {
153 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700154 }
155
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700156 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700157 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900158 if (!network) {
159 ALOGE("no such netId %u", netId);
160 return -ENONET;
161 }
162 if (network->getType() != Network::PHYSICAL) {
163 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700164 return -EINVAL;
165 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700166 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700167 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700168 }
169 }
170
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700171 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700172 Network* network = getNetworkLocked(mDefaultNetId);
173 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700174 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
175 return -ESRCH;
176 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700177 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700178 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700179 }
180 }
181
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700182 mDefaultNetId = netId;
183 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500184}
185
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700186uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500187 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700188 Fwmark fwmark;
189 fwmark.protectedFromVpn = true;
190 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900191 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700192 // If a non-zero NetId was explicitly specified, and the user has permission for that
193 // network, use that network's DNS servers. Do not fall through to the default network even
194 // if the explicitly selected network is a split tunnel VPN or a VPN without DNS servers.
195 fwmark.explicitlySelected = true;
196 } else {
197 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
198 // (possibly falling through to the default network if the VPN doesn't provide a route to
199 // them). Otherwise, use the default network's DNS servers.
200 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
201 if (virtualNetwork && virtualNetwork->getHasDns()) {
202 *netId = virtualNetwork->getNetId();
203 } else {
204 *netId = mDefaultNetId;
205 }
206 }
207 fwmark.netId = *netId;
208 return fwmark.intValue;
209}
210
211// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
212// the VPN that applies to the UID if any; otherwise, the default network.
213unsigned NetworkController::getNetworkForUser(uid_t uid) const {
214 android::RWLock::AutoRLock lock(mRWLock);
215 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700216 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500217 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700218 return mDefaultNetId;
219}
220
221// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
222// applies to the user if any; otherwise, the default network.
223//
224// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
225// is a split-tunnel and disappears later, the socket continues working (since the default network's
226// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
227// high-priority routing rule that doesn't care what NetId the socket has.
228//
229// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
230// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
231// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
232// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
233// the fallthrough rules also go away), the socket that used to fallthrough to the default network
234// will stop working.
235unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
236 android::RWLock::AutoRLock lock(mRWLock);
237 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
238 if (virtualNetwork && !virtualNetwork->isSecure()) {
239 return virtualNetwork->getNetId();
240 }
241 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500242}
243
Erik Klinecea2d342015-06-25 18:24:46 +0900244void NetworkController::getNetworkContext(
245 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
246 struct android_net_context nc = {
247 .app_netid = netId,
248 .app_mark = MARK_UNSET,
249 .dns_netid = netId,
250 .dns_mark = MARK_UNSET,
251 .uid = uid,
252 };
253
Erik Kline492ca5b2016-03-09 14:56:00 +0900254 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
255 // client process. This value is nonzero iff.:
256 //
257 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
258 // - [Java] android.net.Network#getAllByName()
259 // - [C/++] android_getaddrinfofornetwork()
260 // 2. The app specified a netid/nethandle to be used as a process default via:
261 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
262 // - [C/++] android_setprocnetwork()
263 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
264 //
265 // In all these cases (with the possible exception of #3), the right thing to do is to treat
266 // such cases as explicitlySelected.
267 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
268 if (!explicitlySelected) {
Erik Klinecea2d342015-06-25 18:24:46 +0900269 nc.app_netid = getNetworkForConnect(uid);
270 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900271
Erik Klinecea2d342015-06-25 18:24:46 +0900272 Fwmark fwmark;
273 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900274 fwmark.explicitlySelected = explicitlySelected;
275 fwmark.protectedFromVpn = canProtect(uid);
276 fwmark.permission = getPermissionForUser(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900277 nc.app_mark = fwmark.intValue;
278
279 nc.dns_mark = getNetworkForDns(&(nc.dns_netid), uid);
280
281 if (netcontext) {
282 *netcontext = nc;
283 }
284}
285
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700286unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700287 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700288 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700289 if (entry.second->hasInterface(interface)) {
290 return entry.first;
291 }
292 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400293 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500294}
295
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700296bool NetworkController::isVirtualNetwork(unsigned netId) const {
297 android::RWLock::AutoRLock lock(mRWLock);
298 Network* network = getNetworkLocked(netId);
299 return network && network->getType() == Network::VIRTUAL;
300}
301
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700302int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700303 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
304 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400305 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900306 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700307 }
308
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700309 if (isValidNetwork(netId)) {
310 ALOGE("duplicate netId %u", netId);
311 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700312 }
313
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700314 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700315 if (int ret = physicalNetwork->setPermission(permission)) {
316 ALOGE("inconceivable! setPermission cannot fail on an empty network");
317 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900318 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700319 }
320
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700321 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700322 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900323 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700324}
325
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700326int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700327 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700328 ALOGE("invalid netId %u", netId);
329 return -EINVAL;
330 }
331
332 if (isValidNetwork(netId)) {
333 ALOGE("duplicate netId %u", netId);
334 return -EEXIST;
335 }
336
337 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700338 if (int ret = modifyFallthroughLocked(netId, true)) {
339 return ret;
340 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700341 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700342 return 0;
343}
344
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700345int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900346 if (netId == LOCAL_NET_ID) {
347 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700348 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700349 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900350 if (!isValidNetwork(netId)) {
351 ALOGE("no such netId %u", netId);
352 return -ENONET;
353 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700354
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700355 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700356
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700357 android::RWLock::AutoWLock lock(mRWLock);
358 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900359
360 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
361 // other network code, ignore failures and attempt to clear out as much state as possible, even
362 // if we hit an error on the way. Return the first error that we see.
363 int ret = network->clearInterfaces();
364
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700365 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900366 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700367 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900368 if (!ret) {
369 ret = err;
370 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700371 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700372 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700373 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900374 if (int err = modifyFallthroughLocked(netId, false)) {
375 if (!ret) {
376 ret = err;
377 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700378 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700379 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700380 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700381 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700382 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900383 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700384}
385
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700386int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700387 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900388 ALOGE("no such netId %u", netId);
389 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700390 }
391
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700392 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700393 if (existingNetId != NETID_UNSET && existingNetId != netId) {
394 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
395 return -EBUSY;
396 }
397
398 android::RWLock::AutoWLock lock(mRWLock);
399 return getNetworkLocked(netId)->addInterface(interface);
400}
401
402int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
403 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900404 ALOGE("no such netId %u", netId);
405 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700406 }
407
408 android::RWLock::AutoWLock lock(mRWLock);
409 return getNetworkLocked(netId)->removeInterface(interface);
410}
411
412Permission NetworkController::getPermissionForUser(uid_t uid) const {
413 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700414 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700415}
416
417void NetworkController::setPermissionForUsers(Permission permission,
418 const std::vector<uid_t>& uids) {
419 android::RWLock::AutoWLock lock(mRWLock);
420 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700421 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700422 }
423}
424
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900425int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700426 android::RWLock::AutoRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900427 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700428}
429
430int NetworkController::setPermissionForNetworks(Permission permission,
431 const std::vector<unsigned>& netIds) {
432 android::RWLock::AutoWLock lock(mRWLock);
433 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700434 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900435 if (!network) {
436 ALOGE("no such netId %u", netId);
437 return -ENONET;
438 }
439 if (network->getType() != Network::PHYSICAL) {
440 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700441 return -EINVAL;
442 }
443
444 // TODO: ioctl(SIOCKILLADDR, ...) to kill socets on the network that don't have permission.
445
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700446 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700447 return ret;
448 }
449 }
450 return 0;
451}
452
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700453int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
454 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700455 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900456 if (!network) {
457 ALOGE("no such netId %u", netId);
458 return -ENONET;
459 }
460 if (network->getType() != Network::VIRTUAL) {
461 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700462 return -EINVAL;
463 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900464 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700465 return ret;
466 }
467 return 0;
468}
469
470int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
471 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700472 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900473 if (!network) {
474 ALOGE("no such netId %u", netId);
475 return -ENONET;
476 }
477 if (network->getType() != Network::VIRTUAL) {
478 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700479 return -EINVAL;
480 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900481 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
482 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700483 return ret;
484 }
485 return 0;
486}
487
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700488int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
489 const char* nexthop, bool legacy, uid_t uid) {
490 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
491}
492
493int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
494 const char* nexthop, bool legacy, uid_t uid) {
495 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
496}
497
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700498bool NetworkController::canProtect(uid_t uid) const {
499 android::RWLock::AutoRLock lock(mRWLock);
500 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
501 mProtectableUsers.find(uid) != mProtectableUsers.end();
502}
503
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700504void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
505 android::RWLock::AutoWLock lock(mRWLock);
506 mProtectableUsers.insert(uids.begin(), uids.end());
507}
508
509void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
510 android::RWLock::AutoWLock lock(mRWLock);
511 for (uid_t uid : uids) {
512 mProtectableUsers.erase(uid);
513 }
514}
515
Erik Kline2d3a1632016-03-15 16:33:48 +0900516void NetworkController::dump(DumpWriter& dw) {
517 android::RWLock::AutoRLock lock(mRWLock);
518
519 dw.incIndent();
520 dw.println("NetworkController");
521
522 dw.incIndent();
523 dw.println("Default network: %u", mDefaultNetId);
524
525 dw.blankline();
526 dw.println("Networks:");
527 dw.incIndent();
528 for (const auto& i : mNetworks) {
529 dw.println(i.second->toString().c_str());
Pierre Imai3a272072016-04-19 16:17:07 +0900530 android::net::gCtls->resolverCtrl.dump(dw, i.first);
531 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900532 }
533 dw.decIndent();
534
535 dw.decIndent();
536
537 dw.decIndent();
538}
539
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700540bool NetworkController::isValidNetwork(unsigned netId) const {
541 android::RWLock::AutoRLock lock(mRWLock);
542 return getNetworkLocked(netId);
543}
544
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700545Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700546 auto iter = mNetworks.find(netId);
547 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700548}
549
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700550VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
551 for (const auto& entry : mNetworks) {
552 if (entry.second->getType() == Network::VIRTUAL) {
553 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
554 if (virtualNetwork->appliesToUser(uid)) {
555 return virtualNetwork;
556 }
557 }
558 }
559 return NULL;
560}
561
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700562Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
563 auto iter = mUsers.find(uid);
564 if (iter != mUsers.end()) {
565 return iter->second;
566 }
567 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
568}
569
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900570int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700571 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900572 if (!network) {
573 return -ENONET;
574 }
575
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700576 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
577 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900578 if (uid == INVALID_UID) {
579 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700580 }
581 Permission userPermission = getPermissionForUserLocked(uid);
582 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900583 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700584 }
585 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900586 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700587 }
588 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
589 if (virtualNetwork && virtualNetwork->isSecure() &&
590 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900591 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700592 }
593 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900594 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700595}
596
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700597int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
598 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900599 if (!isValidNetwork(netId)) {
600 ALOGE("no such netId %u", netId);
601 return -ENONET;
602 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700603 unsigned existingNetId = getNetworkForInterface(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900604 if (existingNetId == NETID_UNSET) {
605 ALOGE("interface %s not assigned to any netId", interface);
606 return -ENODEV;
607 }
608 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700609 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900610 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700611 }
612
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700613 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700614 if (netId == LOCAL_NET_ID) {
615 tableType = RouteController::LOCAL_NETWORK;
616 } else if (legacy) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700617 if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700618 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700619 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700620 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700621 }
622 } else {
623 tableType = RouteController::INTERFACE;
624 }
625
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700626 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
627 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700628}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700629
630int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
631 if (mDefaultNetId == NETID_UNSET) {
632 return 0;
633 }
634 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900635 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700636 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
637 return -ESRCH;
638 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900639 if (network->getType() != Network::PHYSICAL) {
640 ALOGE("inconceivable! default network must be a physical network");
641 return -EINVAL;
642 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700643 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
644 for (const auto& physicalInterface : network->getInterfaces()) {
645 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
646 add)) {
647 return ret;
648 }
649 }
650 return 0;
651}