blob: 43a55bdd475bf9a717e24187fcd973ae530cf76c [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//
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +090022// Public functions accessible by external callers should be thread-safe and are responsible for
23// acquiring the lock. Private functions in this file should call xxxLocked() methods and access
24// internal state directly.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050025
26#include "NetworkController.h"
27
Erik Kline2d3a1632016-03-15 16:33:48 +090028#define LOG_TAG "Netd"
29#include "log/log.h"
30
31#include "cutils/misc.h"
32#include "resolv_netid.h"
33
Pierre Imai3a272072016-04-19 16:17:07 +090034#include "Controllers.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090035#include "DummyNetwork.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090036#include "DumpWriter.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070037#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070038#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070039#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070040#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070041#include "VirtualNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070042
Erik Kline6d4669f2017-05-25 17:03:31 +090043#define DBG 0
44
Lorenzo Colitti7035f222017-02-13 18:29:00 +090045namespace android {
46namespace net {
47
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070048namespace {
49
50// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070051const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070052const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070053
54} // namespace
55
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070056const unsigned NetworkController::MIN_OEM_ID = 1;
57const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090058const unsigned NetworkController::DUMMY_NET_ID = 51;
59// NetIds 52..98 are reserved for future use.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070060const unsigned NetworkController::LOCAL_NET_ID = 99;
61
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070062// All calls to methods here are made while holding a write lock on mRWLock.
Lorenzo Colitti2d014e72018-01-10 22:12:29 +090063// They are mostly not called directly from this class, but from methods in PhysicalNetwork.cpp.
64// However, we're the only user of that class, so all calls to those methods come from here and are
65// made under lock.
66// For example, PhysicalNetwork::setPermission ends up calling addFallthrough and removeFallthrough,
67// but it's only called from here under lock (specifically, from createPhysicalNetworkLocked and
68// setPermissionForNetworks).
69// TODO: use std::mutex and GUARDED_BY instead of manual inspection.
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070070class 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
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900191uint32_t NetworkController::getNetworkForDnsLocked(unsigned* netId, uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700192 Fwmark fwmark;
193 fwmark.protectedFromVpn = true;
194 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900195 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700196 // If a non-zero NetId was explicitly specified, and the user has permission for that
197 // network, use that network's DNS servers. Do not fall through to the default network even
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900198 // if the explicitly selected network is a split tunnel VPN: the explicitlySelected bit
199 // ensures that the VPN fallthrough rule does not match.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700200 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900201
202 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
203 // servers (through the default network). Otherwise, the query is guaranteed to fail.
204 // http://b/29498052
205 Network *network = getNetworkLocked(*netId);
206 if (network && network->getType() == Network::VIRTUAL &&
207 !static_cast<VirtualNetwork *>(network)->getHasDns()) {
208 *netId = mDefaultNetId;
209 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700210 } else {
211 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
212 // (possibly falling through to the default network if the VPN doesn't provide a route to
213 // them). Otherwise, use the default network's DNS servers.
214 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
215 if (virtualNetwork && virtualNetwork->getHasDns()) {
216 *netId = virtualNetwork->getNetId();
217 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900218 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
219 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700220 *netId = mDefaultNetId;
221 }
222 }
223 fwmark.netId = *netId;
224 return fwmark.intValue;
225}
226
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900227uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
228 android::RWLock::AutoRLock lock(mRWLock);
229 return getNetworkForDnsLocked(netId, uid);
230}
231
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700232// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
233// the VPN that applies to the UID if any; otherwise, the default network.
234unsigned NetworkController::getNetworkForUser(uid_t uid) const {
235 android::RWLock::AutoRLock lock(mRWLock);
236 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700237 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500238 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700239 return mDefaultNetId;
240}
241
242// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
243// applies to the user if any; otherwise, the default network.
244//
245// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
246// is a split-tunnel and disappears later, the socket continues working (since the default network's
247// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
248// high-priority routing rule that doesn't care what NetId the socket has.
249//
250// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
251// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
252// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
253// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
254// the fallthrough rules also go away), the socket that used to fallthrough to the default network
255// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900256unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700257 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
258 if (virtualNetwork && !virtualNetwork->isSecure()) {
259 return virtualNetwork->getNetId();
260 }
261 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500262}
263
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900264unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
265 android::RWLock::AutoRLock lock(mRWLock);
266 return getNetworkForConnectLocked(uid);
267}
268
Erik Klinecea2d342015-06-25 18:24:46 +0900269void NetworkController::getNetworkContext(
270 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900271 android::RWLock::AutoRLock lock(mRWLock);
272
Erik Klinecea2d342015-06-25 18:24:46 +0900273 struct android_net_context nc = {
274 .app_netid = netId,
275 .app_mark = MARK_UNSET,
276 .dns_netid = netId,
277 .dns_mark = MARK_UNSET,
278 .uid = uid,
279 };
280
Erik Kline492ca5b2016-03-09 14:56:00 +0900281 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
282 // client process. This value is nonzero iff.:
283 //
284 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
285 // - [Java] android.net.Network#getAllByName()
286 // - [C/++] android_getaddrinfofornetwork()
287 // 2. The app specified a netid/nethandle to be used as a process default via:
288 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
289 // - [C/++] android_setprocnetwork()
290 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
291 //
292 // In all these cases (with the possible exception of #3), the right thing to do is to treat
293 // such cases as explicitlySelected.
294 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
295 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900296 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900297 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900298
Erik Klinecea2d342015-06-25 18:24:46 +0900299 Fwmark fwmark;
300 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900301 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900302 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
303 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900304 nc.app_mark = fwmark.intValue;
305
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900306 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900307
Erik Kline6d4669f2017-05-25 17:03:31 +0900308 if (DBG) {
309 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
310 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
311 }
312
Erik Klinecea2d342015-06-25 18:24:46 +0900313 if (netcontext) {
314 *netcontext = nc;
315 }
316}
317
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900318unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700319 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700320 if (entry.second->hasInterface(interface)) {
321 return entry.first;
322 }
323 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400324 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500325}
326
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900327unsigned NetworkController::getNetworkForInterface(const char* interface) const {
328 android::RWLock::AutoRLock lock(mRWLock);
329 return getNetworkForInterfaceLocked(interface);
330}
331
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700332bool NetworkController::isVirtualNetwork(unsigned netId) const {
333 android::RWLock::AutoRLock lock(mRWLock);
334 Network* network = getNetworkLocked(netId);
335 return network && network->getType() == Network::VIRTUAL;
336}
337
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700338int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700339 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
340 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400341 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900342 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700343 }
344
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700345 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700346 ALOGE("duplicate netId %u", netId);
347 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700348 }
349
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700350 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700351 if (int ret = physicalNetwork->setPermission(permission)) {
352 ALOGE("inconceivable! setPermission cannot fail on an empty network");
353 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900354 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700355 }
356
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700357 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900358 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700359}
360
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700361int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
362 android::RWLock::AutoWLock lock(mRWLock);
363 return createPhysicalNetworkLocked(netId, permission);
364}
365
366int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
367 if (pNetId == NULL) {
368 return -EINVAL;
369 }
370
371 android::RWLock::AutoWLock lock(mRWLock);
372 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
373 if (!isValidNetworkLocked(*pNetId)) {
374 break;
375 }
376 }
377
378 if (*pNetId > MAX_OEM_ID) {
379 ALOGE("No free network ID");
380 *pNetId = 0;
381 return -ENONET;
382 }
383
384 int ret = createPhysicalNetworkLocked(*pNetId, permission);
385 if (ret) {
386 *pNetId = 0;
387 }
388
389 return ret;
390}
391
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700392int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900393 android::RWLock::AutoWLock lock(mRWLock);
394
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700395 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700396 ALOGE("invalid netId %u", netId);
397 return -EINVAL;
398 }
399
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900400 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700401 ALOGE("duplicate netId %u", netId);
402 return -EEXIST;
403 }
404
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700405 if (int ret = modifyFallthroughLocked(netId, true)) {
406 return ret;
407 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700408 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700409 return 0;
410}
411
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700412int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900413 android::RWLock::AutoWLock lock(mRWLock);
414
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900415 if (netId == LOCAL_NET_ID) {
416 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700417 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700418 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900419 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900420 ALOGE("no such netId %u", netId);
421 return -ENONET;
422 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700423
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700424 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700425
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700426 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900427
428 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
429 // other network code, ignore failures and attempt to clear out as much state as possible, even
430 // if we hit an error on the way. Return the first error that we see.
431 int ret = network->clearInterfaces();
432
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700433 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900434 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700435 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900436 if (!ret) {
437 ret = err;
438 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700439 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700440 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700441 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900442 if (int err = modifyFallthroughLocked(netId, false)) {
443 if (!ret) {
444 ret = err;
445 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700446 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700447 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700448 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700449 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700450 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900451 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700452}
453
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700454int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900455 android::RWLock::AutoWLock lock(mRWLock);
456
457 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900458 ALOGE("no such netId %u", netId);
459 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700460 }
461
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900462 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700463 if (existingNetId != NETID_UNSET && existingNetId != netId) {
464 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
465 return -EBUSY;
466 }
467
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700468 return getNetworkLocked(netId)->addInterface(interface);
469}
470
471int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900472 android::RWLock::AutoWLock lock(mRWLock);
473
474 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900475 ALOGE("no such netId %u", netId);
476 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700477 }
478
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700479 return getNetworkLocked(netId)->removeInterface(interface);
480}
481
482Permission NetworkController::getPermissionForUser(uid_t uid) const {
483 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700484 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700485}
486
487void NetworkController::setPermissionForUsers(Permission permission,
488 const std::vector<uid_t>& uids) {
489 android::RWLock::AutoWLock lock(mRWLock);
490 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700491 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700492 }
493}
494
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900495int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700496 android::RWLock::AutoRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900497 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700498}
499
500int NetworkController::setPermissionForNetworks(Permission permission,
501 const std::vector<unsigned>& netIds) {
502 android::RWLock::AutoWLock lock(mRWLock);
503 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700504 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900505 if (!network) {
506 ALOGE("no such netId %u", netId);
507 return -ENONET;
508 }
509 if (network->getType() != Network::PHYSICAL) {
510 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700511 return -EINVAL;
512 }
513
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700514 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700515 return ret;
516 }
517 }
518 return 0;
519}
520
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700521int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
522 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700523 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900524 if (!network) {
525 ALOGE("no such netId %u", netId);
526 return -ENONET;
527 }
528 if (network->getType() != Network::VIRTUAL) {
529 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700530 return -EINVAL;
531 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900532 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700533 return ret;
534 }
535 return 0;
536}
537
538int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
539 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700540 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900541 if (!network) {
542 ALOGE("no such netId %u", netId);
543 return -ENONET;
544 }
545 if (network->getType() != Network::VIRTUAL) {
546 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700547 return -EINVAL;
548 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900549 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
550 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700551 return ret;
552 }
553 return 0;
554}
555
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700556int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
557 const char* nexthop, bool legacy, uid_t uid) {
558 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
559}
560
561int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
562 const char* nexthop, bool legacy, uid_t uid) {
563 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
564}
565
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900566bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700567 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
568 mProtectableUsers.find(uid) != mProtectableUsers.end();
569}
570
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900571bool NetworkController::canProtect(uid_t uid) const {
572 android::RWLock::AutoRLock lock(mRWLock);
573 return canProtectLocked(uid);
574}
575
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700576void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
577 android::RWLock::AutoWLock lock(mRWLock);
578 mProtectableUsers.insert(uids.begin(), uids.end());
579}
580
581void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
582 android::RWLock::AutoWLock lock(mRWLock);
583 for (uid_t uid : uids) {
584 mProtectableUsers.erase(uid);
585 }
586}
587
Erik Kline2d3a1632016-03-15 16:33:48 +0900588void NetworkController::dump(DumpWriter& dw) {
589 android::RWLock::AutoRLock lock(mRWLock);
590
591 dw.incIndent();
592 dw.println("NetworkController");
593
594 dw.incIndent();
595 dw.println("Default network: %u", mDefaultNetId);
596
597 dw.blankline();
598 dw.println("Networks:");
599 dw.incIndent();
600 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900601 Network* network = i.second;
602 dw.println(network->toString().c_str());
603 if (network->getType() == Network::PHYSICAL) {
604 dw.incIndent();
605 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
606 dw.println("Required permission: %s", permissionToName(permission));
607 dw.decIndent();
608 }
Pierre Imai3a272072016-04-19 16:17:07 +0900609 android::net::gCtls->resolverCtrl.dump(dw, i.first);
610 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900611 }
612 dw.decIndent();
613
614 dw.decIndent();
615
616 dw.decIndent();
617}
618
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700619bool NetworkController::isValidNetworkLocked(unsigned netId) const {
620 return getNetworkLocked(netId);
621}
622
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700623Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700624 auto iter = mNetworks.find(netId);
625 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700626}
627
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700628VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
629 for (const auto& entry : mNetworks) {
630 if (entry.second->getType() == Network::VIRTUAL) {
631 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
632 if (virtualNetwork->appliesToUser(uid)) {
633 return virtualNetwork;
634 }
635 }
636 }
637 return NULL;
638}
639
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700640Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
641 auto iter = mUsers.find(uid);
642 if (iter != mUsers.end()) {
643 return iter->second;
644 }
645 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
646}
647
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900648int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700649 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900650 if (!network) {
651 return -ENONET;
652 }
653
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700654 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
655 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900656 if (uid == INVALID_UID) {
657 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700658 }
659 Permission userPermission = getPermissionForUserLocked(uid);
660 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900661 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700662 }
663 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900664 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700665 }
666 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
667 if (virtualNetwork && virtualNetwork->isSecure() &&
668 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900669 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700670 }
671 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900672 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700673}
674
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700675int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
676 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900677 android::RWLock::AutoRLock lock(mRWLock);
678
679 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900680 ALOGE("no such netId %u", netId);
681 return -ENONET;
682 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900683 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900684 if (existingNetId == NETID_UNSET) {
685 ALOGE("interface %s not assigned to any netId", interface);
686 return -ENODEV;
687 }
688 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700689 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900690 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700691 }
692
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700693 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700694 if (netId == LOCAL_NET_ID) {
695 tableType = RouteController::LOCAL_NETWORK;
696 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900697 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700698 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700699 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700700 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700701 }
702 } else {
703 tableType = RouteController::INTERFACE;
704 }
705
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700706 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
707 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700708}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700709
710int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
711 if (mDefaultNetId == NETID_UNSET) {
712 return 0;
713 }
714 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900715 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700716 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
717 return -ESRCH;
718 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900719 if (network->getType() != Network::PHYSICAL) {
720 ALOGE("inconceivable! default network must be a physical network");
721 return -EINVAL;
722 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700723 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
724 for (const auto& physicalInterface : network->getInterfaces()) {
725 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
726 add)) {
727 return ret;
728 }
729 }
730 return 0;
731}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900732
733} // namespace net
734} // namespace android