blob: 5b2cd89284897196aeb70d2ac65088bfc103dc06 [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;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900358
359 updateTcpSocketMonitorPolling();
360
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900361 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700362}
363
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700364int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
365 android::RWLock::AutoWLock lock(mRWLock);
366 return createPhysicalNetworkLocked(netId, permission);
367}
368
369int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
370 if (pNetId == NULL) {
371 return -EINVAL;
372 }
373
374 android::RWLock::AutoWLock lock(mRWLock);
375 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
376 if (!isValidNetworkLocked(*pNetId)) {
377 break;
378 }
379 }
380
381 if (*pNetId > MAX_OEM_ID) {
382 ALOGE("No free network ID");
383 *pNetId = 0;
384 return -ENONET;
385 }
386
387 int ret = createPhysicalNetworkLocked(*pNetId, permission);
388 if (ret) {
389 *pNetId = 0;
390 }
391
392 return ret;
393}
394
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700395int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900396 android::RWLock::AutoWLock lock(mRWLock);
397
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700398 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700399 ALOGE("invalid netId %u", netId);
400 return -EINVAL;
401 }
402
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900403 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700404 ALOGE("duplicate netId %u", netId);
405 return -EEXIST;
406 }
407
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700408 if (int ret = modifyFallthroughLocked(netId, true)) {
409 return ret;
410 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700411 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700412 return 0;
413}
414
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700415int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900416 android::RWLock::AutoWLock lock(mRWLock);
417
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900418 if (netId == LOCAL_NET_ID) {
419 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700420 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700421 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900422 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900423 ALOGE("no such netId %u", netId);
424 return -ENONET;
425 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700426
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700427 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700428
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700429 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900430
431 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
432 // other network code, ignore failures and attempt to clear out as much state as possible, even
433 // if we hit an error on the way. Return the first error that we see.
434 int ret = network->clearInterfaces();
435
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700436 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900437 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700438 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900439 if (!ret) {
440 ret = err;
441 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700442 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700443 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700444 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900445 if (int err = modifyFallthroughLocked(netId, false)) {
446 if (!ret) {
447 ret = err;
448 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700449 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700450 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700451 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700452 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700453 _resolv_delete_cache_for_net(netId);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900454
455 updateTcpSocketMonitorPolling();
456
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900457 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700458}
459
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700460int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900461 android::RWLock::AutoWLock lock(mRWLock);
462
463 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900464 ALOGE("no such netId %u", netId);
465 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700466 }
467
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900468 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700469 if (existingNetId != NETID_UNSET && existingNetId != netId) {
470 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
471 return -EBUSY;
472 }
473
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700474 return getNetworkLocked(netId)->addInterface(interface);
475}
476
477int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900478 android::RWLock::AutoWLock lock(mRWLock);
479
480 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900481 ALOGE("no such netId %u", netId);
482 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700483 }
484
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700485 return getNetworkLocked(netId)->removeInterface(interface);
486}
487
488Permission NetworkController::getPermissionForUser(uid_t uid) const {
489 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700490 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700491}
492
493void NetworkController::setPermissionForUsers(Permission permission,
494 const std::vector<uid_t>& uids) {
495 android::RWLock::AutoWLock lock(mRWLock);
496 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700497 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700498 }
499}
500
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900501int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700502 android::RWLock::AutoRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900503 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700504}
505
506int NetworkController::setPermissionForNetworks(Permission permission,
507 const std::vector<unsigned>& netIds) {
508 android::RWLock::AutoWLock lock(mRWLock);
509 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700510 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900511 if (!network) {
512 ALOGE("no such netId %u", netId);
513 return -ENONET;
514 }
515 if (network->getType() != Network::PHYSICAL) {
516 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700517 return -EINVAL;
518 }
519
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700520 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700521 return ret;
522 }
523 }
524 return 0;
525}
526
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700527int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
528 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700529 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900530 if (!network) {
531 ALOGE("no such netId %u", netId);
532 return -ENONET;
533 }
534 if (network->getType() != Network::VIRTUAL) {
535 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700536 return -EINVAL;
537 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900538 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700539 return ret;
540 }
541 return 0;
542}
543
544int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
545 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700546 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900547 if (!network) {
548 ALOGE("no such netId %u", netId);
549 return -ENONET;
550 }
551 if (network->getType() != Network::VIRTUAL) {
552 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700553 return -EINVAL;
554 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900555 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
556 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700557 return ret;
558 }
559 return 0;
560}
561
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700562int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
563 const char* nexthop, bool legacy, uid_t uid) {
564 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
565}
566
567int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
568 const char* nexthop, bool legacy, uid_t uid) {
569 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
570}
571
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900572bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700573 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
574 mProtectableUsers.find(uid) != mProtectableUsers.end();
575}
576
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900577bool NetworkController::canProtect(uid_t uid) const {
578 android::RWLock::AutoRLock lock(mRWLock);
579 return canProtectLocked(uid);
580}
581
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700582void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
583 android::RWLock::AutoWLock lock(mRWLock);
584 mProtectableUsers.insert(uids.begin(), uids.end());
585}
586
587void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
588 android::RWLock::AutoWLock lock(mRWLock);
589 for (uid_t uid : uids) {
590 mProtectableUsers.erase(uid);
591 }
592}
593
Erik Kline2d3a1632016-03-15 16:33:48 +0900594void NetworkController::dump(DumpWriter& dw) {
595 android::RWLock::AutoRLock lock(mRWLock);
596
597 dw.incIndent();
598 dw.println("NetworkController");
599
600 dw.incIndent();
601 dw.println("Default network: %u", mDefaultNetId);
602
603 dw.blankline();
604 dw.println("Networks:");
605 dw.incIndent();
606 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900607 Network* network = i.second;
608 dw.println(network->toString().c_str());
609 if (network->getType() == Network::PHYSICAL) {
610 dw.incIndent();
611 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
612 dw.println("Required permission: %s", permissionToName(permission));
613 dw.decIndent();
614 }
Pierre Imai3a272072016-04-19 16:17:07 +0900615 android::net::gCtls->resolverCtrl.dump(dw, i.first);
616 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900617 }
618 dw.decIndent();
619
620 dw.decIndent();
621
622 dw.decIndent();
623}
624
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700625bool NetworkController::isValidNetworkLocked(unsigned netId) const {
626 return getNetworkLocked(netId);
627}
628
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700629Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700630 auto iter = mNetworks.find(netId);
631 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700632}
633
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700634VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
635 for (const auto& entry : mNetworks) {
636 if (entry.second->getType() == Network::VIRTUAL) {
637 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
638 if (virtualNetwork->appliesToUser(uid)) {
639 return virtualNetwork;
640 }
641 }
642 }
643 return NULL;
644}
645
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700646Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
647 auto iter = mUsers.find(uid);
648 if (iter != mUsers.end()) {
649 return iter->second;
650 }
651 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
652}
653
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900654int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700655 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900656 if (!network) {
657 return -ENONET;
658 }
659
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700660 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
661 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900662 if (uid == INVALID_UID) {
663 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700664 }
665 Permission userPermission = getPermissionForUserLocked(uid);
666 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900667 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700668 }
669 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900670 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700671 }
672 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
673 if (virtualNetwork && virtualNetwork->isSecure() &&
674 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900675 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700676 }
677 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900678 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700679}
680
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700681int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
682 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900683 android::RWLock::AutoRLock lock(mRWLock);
684
685 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900686 ALOGE("no such netId %u", netId);
687 return -ENONET;
688 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900689 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900690 if (existingNetId == NETID_UNSET) {
691 ALOGE("interface %s not assigned to any netId", interface);
692 return -ENODEV;
693 }
694 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700695 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900696 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700697 }
698
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700699 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700700 if (netId == LOCAL_NET_ID) {
701 tableType = RouteController::LOCAL_NETWORK;
702 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900703 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700704 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700705 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700706 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700707 }
708 } else {
709 tableType = RouteController::INTERFACE;
710 }
711
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700712 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
713 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700714}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700715
716int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
717 if (mDefaultNetId == NETID_UNSET) {
718 return 0;
719 }
720 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900721 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700722 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
723 return -ESRCH;
724 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900725 if (network->getType() != Network::PHYSICAL) {
726 ALOGE("inconceivable! default network must be a physical network");
727 return -EINVAL;
728 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700729 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
730 for (const auto& physicalInterface : network->getInterfaces()) {
731 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
732 add)) {
733 return ret;
734 }
735 }
736 return 0;
737}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900738
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900739void NetworkController::updateTcpSocketMonitorPolling() {
740 bool physicalNetworkExists = false;
741 for (const auto& entry : mNetworks) {
742 const auto& network = entry.second;
743 if (network->getType() == Network::PHYSICAL && network->getNetId() >= MIN_NET_ID) {
744 physicalNetworkExists = true;
745 break;
746 }
747 }
748
749 if (physicalNetworkExists) {
750 android::net::gCtls->tcpSocketMonitor.resumePolling();
751 } else {
752 android::net::gCtls->tcpSocketMonitor.suspendPolling();
753 }
754}
755
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900756} // namespace net
757} // namespace android