blob: ecb4ceeda3d233ba4b56e3361ae1de782b7195e9 [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
Erik Kline6d4669f2017-05-25 17:03:31 +090050#define DBG 0
51
Lorenzo Colitti7035f222017-02-13 18:29:00 +090052namespace android {
53namespace net {
54
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070055namespace {
56
57// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070058const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070059const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070060
61} // namespace
62
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070063const unsigned NetworkController::MIN_OEM_ID = 1;
64const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090065const unsigned NetworkController::DUMMY_NET_ID = 51;
66// NetIds 52..98 are reserved for future use.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070067const unsigned NetworkController::LOCAL_NET_ID = 99;
68
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070069// All calls to methods here are made while holding a write lock on mRWLock.
70class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
71public:
72 explicit DelegateImpl(NetworkController* networkController);
73 virtual ~DelegateImpl();
74
75 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
76 Permission permission, bool add) WARN_UNUSED_RESULT;
77
78private:
79 int addFallthrough(const std::string& physicalInterface,
80 Permission permission) override WARN_UNUSED_RESULT;
81 int removeFallthrough(const std::string& physicalInterface,
82 Permission permission) override WARN_UNUSED_RESULT;
83
84 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
85 bool add) WARN_UNUSED_RESULT;
86
87 NetworkController* const mNetworkController;
88};
89
90NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
91 mNetworkController(networkController) {
92}
93
94NetworkController::DelegateImpl::~DelegateImpl() {
95}
96
97int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
98 const std::string& physicalInterface,
99 Permission permission, bool add) {
100 if (add) {
101 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
102 physicalInterface.c_str(),
103 permission)) {
104 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
105 vpnNetId);
106 return ret;
107 }
108 } else {
109 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
110 physicalInterface.c_str(),
111 permission)) {
112 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
113 vpnNetId);
114 return ret;
115 }
116 }
117 return 0;
118}
119
120int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
121 Permission permission) {
122 return modifyFallthrough(physicalInterface, permission, true);
123}
124
125int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
126 Permission permission) {
127 return modifyFallthrough(physicalInterface, permission, false);
128}
129
130int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
131 Permission permission, bool add) {
132 for (const auto& entry : mNetworkController->mNetworks) {
133 if (entry.second->getType() == Network::VIRTUAL) {
134 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
135 return ret;
136 }
137 }
138 }
139 return 0;
140}
141
142NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900143 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
144 mProtectableUsers({AID_VPN}) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700145 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900146 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500147}
148
149unsigned NetworkController::getDefaultNetwork() const {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700150 android::RWLock::AutoRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500151 return mDefaultNetId;
152}
153
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700154int NetworkController::setDefaultNetwork(unsigned netId) {
155 android::RWLock::AutoWLock lock(mRWLock);
156
157 if (netId == mDefaultNetId) {
158 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700159 }
160
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700161 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700162 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900163 if (!network) {
164 ALOGE("no such netId %u", netId);
165 return -ENONET;
166 }
167 if (network->getType() != Network::PHYSICAL) {
168 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700169 return -EINVAL;
170 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700171 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700172 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700173 }
174 }
175
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700176 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700177 Network* network = getNetworkLocked(mDefaultNetId);
178 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700179 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
180 return -ESRCH;
181 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700182 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700183 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700184 }
185 }
186
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700187 mDefaultNetId = netId;
188 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500189}
190
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700191uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500192 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700193 Fwmark fwmark;
194 fwmark.protectedFromVpn = true;
195 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900196 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700197 // If a non-zero NetId was explicitly specified, and the user has permission for that
198 // network, use that network's DNS servers. Do not fall through to the default network even
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900199 // if the explicitly selected network is a split tunnel VPN: the explicitlySelected bit
200 // ensures that the VPN fallthrough rule does not match.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700201 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900202
203 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
204 // servers (through the default network). Otherwise, the query is guaranteed to fail.
205 // http://b/29498052
206 Network *network = getNetworkLocked(*netId);
207 if (network && network->getType() == Network::VIRTUAL &&
208 !static_cast<VirtualNetwork *>(network)->getHasDns()) {
209 *netId = mDefaultNetId;
210 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700211 } else {
212 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
213 // (possibly falling through to the default network if the VPN doesn't provide a route to
214 // them). Otherwise, use the default network's DNS servers.
215 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
216 if (virtualNetwork && virtualNetwork->getHasDns()) {
217 *netId = virtualNetwork->getNetId();
218 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900219 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
220 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700221 *netId = mDefaultNetId;
222 }
223 }
224 fwmark.netId = *netId;
225 return fwmark.intValue;
226}
227
228// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
229// the VPN that applies to the UID if any; otherwise, the default network.
230unsigned NetworkController::getNetworkForUser(uid_t uid) const {
231 android::RWLock::AutoRLock lock(mRWLock);
232 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700233 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500234 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700235 return mDefaultNetId;
236}
237
238// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
239// applies to the user if any; otherwise, the default network.
240//
241// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
242// is a split-tunnel and disappears later, the socket continues working (since the default network's
243// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
244// high-priority routing rule that doesn't care what NetId the socket has.
245//
246// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
247// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
248// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
249// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
250// the fallthrough rules also go away), the socket that used to fallthrough to the default network
251// will stop working.
252unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
253 android::RWLock::AutoRLock lock(mRWLock);
254 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
255 if (virtualNetwork && !virtualNetwork->isSecure()) {
256 return virtualNetwork->getNetId();
257 }
258 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500259}
260
Erik Klinecea2d342015-06-25 18:24:46 +0900261void NetworkController::getNetworkContext(
262 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
263 struct android_net_context nc = {
264 .app_netid = netId,
265 .app_mark = MARK_UNSET,
266 .dns_netid = netId,
267 .dns_mark = MARK_UNSET,
268 .uid = uid,
269 };
270
Erik Kline492ca5b2016-03-09 14:56:00 +0900271 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
272 // client process. This value is nonzero iff.:
273 //
274 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
275 // - [Java] android.net.Network#getAllByName()
276 // - [C/++] android_getaddrinfofornetwork()
277 // 2. The app specified a netid/nethandle to be used as a process default via:
278 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
279 // - [C/++] android_setprocnetwork()
280 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
281 //
282 // In all these cases (with the possible exception of #3), the right thing to do is to treat
283 // such cases as explicitlySelected.
284 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
285 if (!explicitlySelected) {
Erik Klinecea2d342015-06-25 18:24:46 +0900286 nc.app_netid = getNetworkForConnect(uid);
287 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900288
Erik Klinecea2d342015-06-25 18:24:46 +0900289 Fwmark fwmark;
290 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900291 fwmark.explicitlySelected = explicitlySelected;
Erik Kline6d4669f2017-05-25 17:03:31 +0900292 fwmark.protectedFromVpn = explicitlySelected && canProtect(uid);
Erik Kline492ca5b2016-03-09 14:56:00 +0900293 fwmark.permission = getPermissionForUser(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900294 nc.app_mark = fwmark.intValue;
295
296 nc.dns_mark = getNetworkForDns(&(nc.dns_netid), uid);
297
Erik Kline6d4669f2017-05-25 17:03:31 +0900298 if (DBG) {
299 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
300 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
301 }
302
Erik Klinecea2d342015-06-25 18:24:46 +0900303 if (netcontext) {
304 *netcontext = nc;
305 }
306}
307
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700308unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700309 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700310 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700311 if (entry.second->hasInterface(interface)) {
312 return entry.first;
313 }
314 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400315 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500316}
317
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700318bool NetworkController::isVirtualNetwork(unsigned netId) const {
319 android::RWLock::AutoRLock lock(mRWLock);
320 Network* network = getNetworkLocked(netId);
321 return network && network->getType() == Network::VIRTUAL;
322}
323
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700324int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700325 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
326 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400327 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900328 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700329 }
330
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700331 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700332 ALOGE("duplicate netId %u", netId);
333 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700334 }
335
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700336 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700337 if (int ret = physicalNetwork->setPermission(permission)) {
338 ALOGE("inconceivable! setPermission cannot fail on an empty network");
339 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900340 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700341 }
342
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700343 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900344 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700345}
346
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700347int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
348 android::RWLock::AutoWLock lock(mRWLock);
349 return createPhysicalNetworkLocked(netId, permission);
350}
351
352int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
353 if (pNetId == NULL) {
354 return -EINVAL;
355 }
356
357 android::RWLock::AutoWLock lock(mRWLock);
358 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
359 if (!isValidNetworkLocked(*pNetId)) {
360 break;
361 }
362 }
363
364 if (*pNetId > MAX_OEM_ID) {
365 ALOGE("No free network ID");
366 *pNetId = 0;
367 return -ENONET;
368 }
369
370 int ret = createPhysicalNetworkLocked(*pNetId, permission);
371 if (ret) {
372 *pNetId = 0;
373 }
374
375 return ret;
376}
377
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700378int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700379 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700380 ALOGE("invalid netId %u", netId);
381 return -EINVAL;
382 }
383
384 if (isValidNetwork(netId)) {
385 ALOGE("duplicate netId %u", netId);
386 return -EEXIST;
387 }
388
389 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700390 if (int ret = modifyFallthroughLocked(netId, true)) {
391 return ret;
392 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700393 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700394 return 0;
395}
396
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700397int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900398 if (netId == LOCAL_NET_ID) {
399 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700400 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700401 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900402 if (!isValidNetwork(netId)) {
403 ALOGE("no such netId %u", netId);
404 return -ENONET;
405 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700406
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700407 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700408
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700409 android::RWLock::AutoWLock lock(mRWLock);
410 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900411
412 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
413 // other network code, ignore failures and attempt to clear out as much state as possible, even
414 // if we hit an error on the way. Return the first error that we see.
415 int ret = network->clearInterfaces();
416
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700417 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900418 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700419 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900420 if (!ret) {
421 ret = err;
422 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700423 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700424 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700425 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900426 if (int err = modifyFallthroughLocked(netId, false)) {
427 if (!ret) {
428 ret = err;
429 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700430 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700431 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700432 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700433 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700434 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900435 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700436}
437
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700438int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700439 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900440 ALOGE("no such netId %u", netId);
441 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700442 }
443
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700444 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700445 if (existingNetId != NETID_UNSET && existingNetId != netId) {
446 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
447 return -EBUSY;
448 }
449
450 android::RWLock::AutoWLock lock(mRWLock);
451 return getNetworkLocked(netId)->addInterface(interface);
452}
453
454int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
455 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900456 ALOGE("no such netId %u", netId);
457 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700458 }
459
460 android::RWLock::AutoWLock lock(mRWLock);
461 return getNetworkLocked(netId)->removeInterface(interface);
462}
463
464Permission NetworkController::getPermissionForUser(uid_t uid) const {
465 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700466 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700467}
468
469void NetworkController::setPermissionForUsers(Permission permission,
470 const std::vector<uid_t>& uids) {
471 android::RWLock::AutoWLock lock(mRWLock);
472 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700473 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700474 }
475}
476
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900477int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700478 android::RWLock::AutoRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900479 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700480}
481
482int NetworkController::setPermissionForNetworks(Permission permission,
483 const std::vector<unsigned>& netIds) {
484 android::RWLock::AutoWLock lock(mRWLock);
485 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700486 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900487 if (!network) {
488 ALOGE("no such netId %u", netId);
489 return -ENONET;
490 }
491 if (network->getType() != Network::PHYSICAL) {
492 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700493 return -EINVAL;
494 }
495
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700496 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700497 return ret;
498 }
499 }
500 return 0;
501}
502
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700503int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
504 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700505 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900506 if (!network) {
507 ALOGE("no such netId %u", netId);
508 return -ENONET;
509 }
510 if (network->getType() != Network::VIRTUAL) {
511 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700512 return -EINVAL;
513 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900514 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700515 return ret;
516 }
517 return 0;
518}
519
520int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
521 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700522 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900523 if (!network) {
524 ALOGE("no such netId %u", netId);
525 return -ENONET;
526 }
527 if (network->getType() != Network::VIRTUAL) {
528 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700529 return -EINVAL;
530 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900531 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
532 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700533 return ret;
534 }
535 return 0;
536}
537
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700538int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
539 const char* nexthop, bool legacy, uid_t uid) {
540 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
541}
542
543int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
544 const char* nexthop, bool legacy, uid_t uid) {
545 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
546}
547
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700548bool NetworkController::canProtect(uid_t uid) const {
549 android::RWLock::AutoRLock lock(mRWLock);
550 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
551 mProtectableUsers.find(uid) != mProtectableUsers.end();
552}
553
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700554void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
555 android::RWLock::AutoWLock lock(mRWLock);
556 mProtectableUsers.insert(uids.begin(), uids.end());
557}
558
559void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
560 android::RWLock::AutoWLock lock(mRWLock);
561 for (uid_t uid : uids) {
562 mProtectableUsers.erase(uid);
563 }
564}
565
Erik Kline2d3a1632016-03-15 16:33:48 +0900566void NetworkController::dump(DumpWriter& dw) {
567 android::RWLock::AutoRLock lock(mRWLock);
568
569 dw.incIndent();
570 dw.println("NetworkController");
571
572 dw.incIndent();
573 dw.println("Default network: %u", mDefaultNetId);
574
575 dw.blankline();
576 dw.println("Networks:");
577 dw.incIndent();
578 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900579 Network* network = i.second;
580 dw.println(network->toString().c_str());
581 if (network->getType() == Network::PHYSICAL) {
582 dw.incIndent();
583 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
584 dw.println("Required permission: %s", permissionToName(permission));
585 dw.decIndent();
586 }
Pierre Imai3a272072016-04-19 16:17:07 +0900587 android::net::gCtls->resolverCtrl.dump(dw, i.first);
588 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900589 }
590 dw.decIndent();
591
592 dw.decIndent();
593
594 dw.decIndent();
595}
596
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700597bool NetworkController::isValidNetworkLocked(unsigned netId) const {
598 return getNetworkLocked(netId);
599}
600
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700601bool NetworkController::isValidNetwork(unsigned netId) const {
602 android::RWLock::AutoRLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700603 return isValidNetworkLocked(netId);
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700604}
605
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700606Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700607 auto iter = mNetworks.find(netId);
608 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700609}
610
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700611VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
612 for (const auto& entry : mNetworks) {
613 if (entry.second->getType() == Network::VIRTUAL) {
614 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
615 if (virtualNetwork->appliesToUser(uid)) {
616 return virtualNetwork;
617 }
618 }
619 }
620 return NULL;
621}
622
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700623Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
624 auto iter = mUsers.find(uid);
625 if (iter != mUsers.end()) {
626 return iter->second;
627 }
628 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
629}
630
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900631int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700632 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900633 if (!network) {
634 return -ENONET;
635 }
636
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700637 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
638 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900639 if (uid == INVALID_UID) {
640 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700641 }
642 Permission userPermission = getPermissionForUserLocked(uid);
643 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900644 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700645 }
646 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900647 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700648 }
649 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
650 if (virtualNetwork && virtualNetwork->isSecure() &&
651 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900652 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700653 }
654 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900655 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700656}
657
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700658int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
659 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900660 if (!isValidNetwork(netId)) {
661 ALOGE("no such netId %u", netId);
662 return -ENONET;
663 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700664 unsigned existingNetId = getNetworkForInterface(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900665 if (existingNetId == NETID_UNSET) {
666 ALOGE("interface %s not assigned to any netId", interface);
667 return -ENODEV;
668 }
669 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700670 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900671 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700672 }
673
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700674 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700675 if (netId == LOCAL_NET_ID) {
676 tableType = RouteController::LOCAL_NETWORK;
677 } else if (legacy) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700678 if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700679 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700680 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700681 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700682 }
683 } else {
684 tableType = RouteController::INTERFACE;
685 }
686
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700687 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
688 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700689}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700690
691int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
692 if (mDefaultNetId == NETID_UNSET) {
693 return 0;
694 }
695 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900696 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700697 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
698 return -ESRCH;
699 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900700 if (network->getType() != Network::PHYSICAL) {
701 ALOGE("inconceivable! default network must be a physical network");
702 return -EINVAL;
703 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700704 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
705 for (const auto& physicalInterface : network->getInterfaces()) {
706 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
707 add)) {
708 return ret;
709 }
710 }
711 return 0;
712}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900713
714} // namespace net
715} // namespace android