blob: 87ad1bd0749dfd756500cb775d5cac298c114d1e [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 Colitti95f1bcb2018-05-30 16:14:18 +0900195
196 // Common case: there is no VPN that applies to the user, and the query did not specify a netId.
197 // Therefore, it is safe to set the explicit bit on this query and skip all the complex logic
198 // below. While this looks like a special case, it is actually the one that handles the vast
199 // majority of DNS queries.
200 // TODO: untangle this code.
201 if (*netId == NETID_UNSET && getVirtualNetworkForUserLocked(uid) == nullptr) {
202 *netId = mDefaultNetId;
203 fwmark.netId = *netId;
204 fwmark.explicitlySelected = true;
205 return fwmark.intValue;
206 }
207
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900208 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700209 // If a non-zero NetId was explicitly specified, and the user has permission for that
210 // network, use that network's DNS servers. Do not fall through to the default network even
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900211 // if the explicitly selected network is a split tunnel VPN: the explicitlySelected bit
212 // ensures that the VPN fallthrough rule does not match.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700213 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900214
215 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
216 // servers (through the default network). Otherwise, the query is guaranteed to fail.
217 // http://b/29498052
218 Network *network = getNetworkLocked(*netId);
219 if (network && network->getType() == Network::VIRTUAL &&
220 !static_cast<VirtualNetwork *>(network)->getHasDns()) {
221 *netId = mDefaultNetId;
222 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700223 } else {
224 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
225 // (possibly falling through to the default network if the VPN doesn't provide a route to
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900226 // them). Otherwise, use the default network's DNS servers. We cannot set the explicit bit
227 // because we need to be able to fall through a split tunnel to the default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700228 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
229 if (virtualNetwork && virtualNetwork->getHasDns()) {
230 *netId = virtualNetwork->getNetId();
231 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900232 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
233 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700234 *netId = mDefaultNetId;
235 }
236 }
237 fwmark.netId = *netId;
238 return fwmark.intValue;
239}
240
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900241uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
242 android::RWLock::AutoRLock lock(mRWLock);
243 return getNetworkForDnsLocked(netId, uid);
244}
245
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700246// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
247// the VPN that applies to the UID if any; otherwise, the default network.
248unsigned NetworkController::getNetworkForUser(uid_t uid) const {
249 android::RWLock::AutoRLock lock(mRWLock);
250 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700251 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500252 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700253 return mDefaultNetId;
254}
255
256// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
257// applies to the user if any; otherwise, the default network.
258//
259// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
260// is a split-tunnel and disappears later, the socket continues working (since the default network's
261// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
262// high-priority routing rule that doesn't care what NetId the socket has.
263//
264// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
265// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
266// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
267// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
268// the fallthrough rules also go away), the socket that used to fallthrough to the default network
269// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900270unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700271 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
272 if (virtualNetwork && !virtualNetwork->isSecure()) {
273 return virtualNetwork->getNetId();
274 }
275 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500276}
277
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900278unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
279 android::RWLock::AutoRLock lock(mRWLock);
280 return getNetworkForConnectLocked(uid);
281}
282
Erik Klinecea2d342015-06-25 18:24:46 +0900283void NetworkController::getNetworkContext(
284 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900285 android::RWLock::AutoRLock lock(mRWLock);
286
Erik Klinecea2d342015-06-25 18:24:46 +0900287 struct android_net_context nc = {
288 .app_netid = netId,
289 .app_mark = MARK_UNSET,
290 .dns_netid = netId,
291 .dns_mark = MARK_UNSET,
292 .uid = uid,
293 };
294
Erik Kline492ca5b2016-03-09 14:56:00 +0900295 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
296 // client process. This value is nonzero iff.:
297 //
298 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
299 // - [Java] android.net.Network#getAllByName()
300 // - [C/++] android_getaddrinfofornetwork()
301 // 2. The app specified a netid/nethandle to be used as a process default via:
302 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
303 // - [C/++] android_setprocnetwork()
304 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
305 //
306 // In all these cases (with the possible exception of #3), the right thing to do is to treat
307 // such cases as explicitlySelected.
308 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
309 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900310 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900311 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900312
Erik Klinecea2d342015-06-25 18:24:46 +0900313 Fwmark fwmark;
314 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900315 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900316 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
317 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900318 nc.app_mark = fwmark.intValue;
319
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900320 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900321
Erik Kline6d4669f2017-05-25 17:03:31 +0900322 if (DBG) {
323 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
324 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
325 }
326
Erik Klinecea2d342015-06-25 18:24:46 +0900327 if (netcontext) {
328 *netcontext = nc;
329 }
330}
331
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900332unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700333 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700334 if (entry.second->hasInterface(interface)) {
335 return entry.first;
336 }
337 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400338 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500339}
340
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900341unsigned NetworkController::getNetworkForInterface(const char* interface) const {
342 android::RWLock::AutoRLock lock(mRWLock);
343 return getNetworkForInterfaceLocked(interface);
344}
345
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700346bool NetworkController::isVirtualNetwork(unsigned netId) const {
347 android::RWLock::AutoRLock lock(mRWLock);
348 Network* network = getNetworkLocked(netId);
349 return network && network->getType() == Network::VIRTUAL;
350}
351
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700352int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700353 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
354 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400355 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900356 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700357 }
358
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700359 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700360 ALOGE("duplicate netId %u", netId);
361 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700362 }
363
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700364 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700365 if (int ret = physicalNetwork->setPermission(permission)) {
366 ALOGE("inconceivable! setPermission cannot fail on an empty network");
367 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900368 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700369 }
370
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700371 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900372
373 updateTcpSocketMonitorPolling();
374
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900375 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700376}
377
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700378int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
379 android::RWLock::AutoWLock lock(mRWLock);
380 return createPhysicalNetworkLocked(netId, permission);
381}
382
383int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
384 if (pNetId == NULL) {
385 return -EINVAL;
386 }
387
388 android::RWLock::AutoWLock lock(mRWLock);
389 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
390 if (!isValidNetworkLocked(*pNetId)) {
391 break;
392 }
393 }
394
395 if (*pNetId > MAX_OEM_ID) {
396 ALOGE("No free network ID");
397 *pNetId = 0;
398 return -ENONET;
399 }
400
401 int ret = createPhysicalNetworkLocked(*pNetId, permission);
402 if (ret) {
403 *pNetId = 0;
404 }
405
406 return ret;
407}
408
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700409int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900410 android::RWLock::AutoWLock lock(mRWLock);
411
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700412 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700413 ALOGE("invalid netId %u", netId);
414 return -EINVAL;
415 }
416
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900417 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700418 ALOGE("duplicate netId %u", netId);
419 return -EEXIST;
420 }
421
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700422 if (int ret = modifyFallthroughLocked(netId, true)) {
423 return ret;
424 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700425 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700426 return 0;
427}
428
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700429int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900430 android::RWLock::AutoWLock lock(mRWLock);
431
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900432 if (netId == LOCAL_NET_ID) {
433 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700434 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700435 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900436 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900437 ALOGE("no such netId %u", netId);
438 return -ENONET;
439 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700440
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700441 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700442
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700443 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900444
445 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
446 // other network code, ignore failures and attempt to clear out as much state as possible, even
447 // if we hit an error on the way. Return the first error that we see.
448 int ret = network->clearInterfaces();
449
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700450 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900451 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700452 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900453 if (!ret) {
454 ret = err;
455 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700456 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700457 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700458 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900459 if (int err = modifyFallthroughLocked(netId, false)) {
460 if (!ret) {
461 ret = err;
462 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700463 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700464 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700465 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700466 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700467 _resolv_delete_cache_for_net(netId);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900468
469 updateTcpSocketMonitorPolling();
470
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900471 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700472}
473
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700474int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900475 android::RWLock::AutoWLock lock(mRWLock);
476
477 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900478 ALOGE("no such netId %u", netId);
479 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700480 }
481
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900482 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700483 if (existingNetId != NETID_UNSET && existingNetId != netId) {
484 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
485 return -EBUSY;
486 }
487
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700488 return getNetworkLocked(netId)->addInterface(interface);
489}
490
491int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900492 android::RWLock::AutoWLock lock(mRWLock);
493
494 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900495 ALOGE("no such netId %u", netId);
496 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700497 }
498
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700499 return getNetworkLocked(netId)->removeInterface(interface);
500}
501
502Permission NetworkController::getPermissionForUser(uid_t uid) const {
503 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700504 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700505}
506
507void NetworkController::setPermissionForUsers(Permission permission,
508 const std::vector<uid_t>& uids) {
509 android::RWLock::AutoWLock lock(mRWLock);
510 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700511 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700512 }
513}
514
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900515int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700516 android::RWLock::AutoRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900517 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700518}
519
520int NetworkController::setPermissionForNetworks(Permission permission,
521 const std::vector<unsigned>& netIds) {
522 android::RWLock::AutoWLock lock(mRWLock);
523 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700524 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900525 if (!network) {
526 ALOGE("no such netId %u", netId);
527 return -ENONET;
528 }
529 if (network->getType() != Network::PHYSICAL) {
530 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700531 return -EINVAL;
532 }
533
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700534 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700535 return ret;
536 }
537 }
538 return 0;
539}
540
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700541int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
542 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700543 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900544 if (!network) {
545 ALOGE("no such netId %u", netId);
546 return -ENONET;
547 }
548 if (network->getType() != Network::VIRTUAL) {
549 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700550 return -EINVAL;
551 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900552 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700553 return ret;
554 }
555 return 0;
556}
557
558int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
559 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700560 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900561 if (!network) {
562 ALOGE("no such netId %u", netId);
563 return -ENONET;
564 }
565 if (network->getType() != Network::VIRTUAL) {
566 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700567 return -EINVAL;
568 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900569 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
570 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700571 return ret;
572 }
573 return 0;
574}
575
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700576int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
577 const char* nexthop, bool legacy, uid_t uid) {
578 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
579}
580
581int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
582 const char* nexthop, bool legacy, uid_t uid) {
583 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
584}
585
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900586bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700587 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
588 mProtectableUsers.find(uid) != mProtectableUsers.end();
589}
590
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900591bool NetworkController::canProtect(uid_t uid) const {
592 android::RWLock::AutoRLock lock(mRWLock);
593 return canProtectLocked(uid);
594}
595
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700596void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
597 android::RWLock::AutoWLock lock(mRWLock);
598 mProtectableUsers.insert(uids.begin(), uids.end());
599}
600
601void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
602 android::RWLock::AutoWLock lock(mRWLock);
603 for (uid_t uid : uids) {
604 mProtectableUsers.erase(uid);
605 }
606}
607
Erik Kline2d3a1632016-03-15 16:33:48 +0900608void NetworkController::dump(DumpWriter& dw) {
609 android::RWLock::AutoRLock lock(mRWLock);
610
611 dw.incIndent();
612 dw.println("NetworkController");
613
614 dw.incIndent();
615 dw.println("Default network: %u", mDefaultNetId);
616
617 dw.blankline();
618 dw.println("Networks:");
619 dw.incIndent();
620 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900621 Network* network = i.second;
622 dw.println(network->toString().c_str());
623 if (network->getType() == Network::PHYSICAL) {
624 dw.incIndent();
625 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
626 dw.println("Required permission: %s", permissionToName(permission));
627 dw.decIndent();
628 }
Pierre Imai3a272072016-04-19 16:17:07 +0900629 android::net::gCtls->resolverCtrl.dump(dw, i.first);
630 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900631 }
632 dw.decIndent();
633
634 dw.decIndent();
635
636 dw.decIndent();
637}
638
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700639bool NetworkController::isValidNetworkLocked(unsigned netId) const {
640 return getNetworkLocked(netId);
641}
642
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700643Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700644 auto iter = mNetworks.find(netId);
645 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700646}
647
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700648VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
649 for (const auto& entry : mNetworks) {
650 if (entry.second->getType() == Network::VIRTUAL) {
651 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
652 if (virtualNetwork->appliesToUser(uid)) {
653 return virtualNetwork;
654 }
655 }
656 }
657 return NULL;
658}
659
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700660Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
661 auto iter = mUsers.find(uid);
662 if (iter != mUsers.end()) {
663 return iter->second;
664 }
665 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
666}
667
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900668int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700669 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900670 if (!network) {
671 return -ENONET;
672 }
673
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700674 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
675 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900676 if (uid == INVALID_UID) {
677 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700678 }
679 Permission userPermission = getPermissionForUserLocked(uid);
680 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900681 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700682 }
683 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900684 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700685 }
686 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
687 if (virtualNetwork && virtualNetwork->isSecure() &&
688 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900689 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700690 }
691 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900692 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700693}
694
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700695int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
696 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900697 android::RWLock::AutoRLock lock(mRWLock);
698
699 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900700 ALOGE("no such netId %u", netId);
701 return -ENONET;
702 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900703 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900704 if (existingNetId == NETID_UNSET) {
705 ALOGE("interface %s not assigned to any netId", interface);
706 return -ENODEV;
707 }
708 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700709 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900710 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700711 }
712
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700713 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700714 if (netId == LOCAL_NET_ID) {
715 tableType = RouteController::LOCAL_NETWORK;
716 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900717 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700718 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700719 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700720 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700721 }
722 } else {
723 tableType = RouteController::INTERFACE;
724 }
725
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700726 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
727 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700728}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700729
730int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
731 if (mDefaultNetId == NETID_UNSET) {
732 return 0;
733 }
734 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900735 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700736 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
737 return -ESRCH;
738 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900739 if (network->getType() != Network::PHYSICAL) {
740 ALOGE("inconceivable! default network must be a physical network");
741 return -EINVAL;
742 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700743 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
744 for (const auto& physicalInterface : network->getInterfaces()) {
745 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
746 add)) {
747 return ret;
748 }
749 }
750 return 0;
751}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900752
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900753void NetworkController::updateTcpSocketMonitorPolling() {
754 bool physicalNetworkExists = false;
755 for (const auto& entry : mNetworks) {
756 const auto& network = entry.second;
757 if (network->getType() == Network::PHYSICAL && network->getNetId() >= MIN_NET_ID) {
758 physicalNetworkExists = true;
759 break;
760 }
761 }
762
763 if (physicalNetworkExists) {
764 android::net::gCtls->tcpSocketMonitor.resumePolling();
765 } else {
766 android::net::gCtls->tcpSocketMonitor.suspendPolling();
767 }
768}
769
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900770} // namespace net
771} // namespace android