blob: 575a0bc6b21734519490b4d942154bc7a88a9d0a [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
Rubin Xu6c00b612018-04-27 14:27:59 +010031#include <android-base/strings.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090032#include <netd_resolv/resolv.h>
Luke Huangc3252cc2018-10-16 15:43:23 +080033#include "android/net/INetd.h"
Rubin Xu6c00b612018-04-27 14:27:59 +010034
Erik Kline2d3a1632016-03-15 16:33:48 +090035#include "cutils/misc.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090036
Pierre Imai3a272072016-04-19 16:17:07 +090037#include "Controllers.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090038#include "DummyNetwork.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090039#include "DumpWriter.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070040#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070041#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070042#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070043#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070044#include "VirtualNetwork.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090045#include "netid_client.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070046
Erik Kline6d4669f2017-05-25 17:03:31 +090047#define DBG 0
48
Lorenzo Colitti7035f222017-02-13 18:29:00 +090049namespace android {
50namespace net {
51
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070052namespace {
53
54// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070055const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070056const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070057
58} // namespace
59
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070060const unsigned NetworkController::MIN_OEM_ID = 1;
61const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090062const unsigned NetworkController::DUMMY_NET_ID = 51;
63// NetIds 52..98 are reserved for future use.
Luke Huangc3252cc2018-10-16 15:43:23 +080064const unsigned NetworkController::LOCAL_NET_ID = INetd::LOCAL_NET_ID;
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070065
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070066// All calls to methods here are made while holding a write lock on mRWLock.
Lorenzo Colitti2d014e72018-01-10 22:12:29 +090067// They are mostly not called directly from this class, but from methods in PhysicalNetwork.cpp.
68// However, we're the only user of that class, so all calls to those methods come from here and are
69// made under lock.
70// For example, PhysicalNetwork::setPermission ends up calling addFallthrough and removeFallthrough,
71// but it's only called from here under lock (specifically, from createPhysicalNetworkLocked and
72// setPermissionForNetworks).
73// TODO: use std::mutex and GUARDED_BY instead of manual inspection.
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070074class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
75public:
76 explicit DelegateImpl(NetworkController* networkController);
77 virtual ~DelegateImpl();
78
79 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
80 Permission permission, bool add) WARN_UNUSED_RESULT;
81
82private:
83 int addFallthrough(const std::string& physicalInterface,
84 Permission permission) override WARN_UNUSED_RESULT;
85 int removeFallthrough(const std::string& physicalInterface,
86 Permission permission) override WARN_UNUSED_RESULT;
87
88 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
89 bool add) WARN_UNUSED_RESULT;
90
91 NetworkController* const mNetworkController;
92};
93
94NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
95 mNetworkController(networkController) {
96}
97
98NetworkController::DelegateImpl::~DelegateImpl() {
99}
100
101int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
102 const std::string& physicalInterface,
103 Permission permission, bool add) {
104 if (add) {
105 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
106 physicalInterface.c_str(),
107 permission)) {
108 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
109 vpnNetId);
110 return ret;
111 }
112 } else {
113 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
114 physicalInterface.c_str(),
115 permission)) {
116 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
117 vpnNetId);
118 return ret;
119 }
120 }
121 return 0;
122}
123
124int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
125 Permission permission) {
126 return modifyFallthrough(physicalInterface, permission, true);
127}
128
129int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
130 Permission permission) {
131 return modifyFallthrough(physicalInterface, permission, false);
132}
133
134int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
135 Permission permission, bool add) {
136 for (const auto& entry : mNetworkController->mNetworks) {
137 if (entry.second->getType() == Network::VIRTUAL) {
138 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
139 return ret;
140 }
141 }
142 }
143 return 0;
144}
145
146NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900147 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
148 mProtectableUsers({AID_VPN}) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700149 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900150 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500151}
152
153unsigned NetworkController::getDefaultNetwork() const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800154 ScopedRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500155 return mDefaultNetId;
156}
157
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700158int NetworkController::setDefaultNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800159 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700160
161 if (netId == mDefaultNetId) {
162 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700163 }
164
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700165 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700166 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900167 if (!network) {
168 ALOGE("no such netId %u", netId);
169 return -ENONET;
170 }
171 if (network->getType() != Network::PHYSICAL) {
172 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700173 return -EINVAL;
174 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700175 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700176 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700177 }
178 }
179
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700180 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700181 Network* network = getNetworkLocked(mDefaultNetId);
182 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700183 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
184 return -ESRCH;
185 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700186 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700187 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700188 }
189 }
190
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700191 mDefaultNetId = netId;
192 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500193}
194
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900195uint32_t NetworkController::getNetworkForDnsLocked(unsigned* netId, uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700196 Fwmark fwmark;
197 fwmark.protectedFromVpn = true;
198 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900199
200 // Common case: there is no VPN that applies to the user, and the query did not specify a netId.
201 // Therefore, it is safe to set the explicit bit on this query and skip all the complex logic
202 // below. While this looks like a special case, it is actually the one that handles the vast
203 // majority of DNS queries.
204 // TODO: untangle this code.
205 if (*netId == NETID_UNSET && getVirtualNetworkForUserLocked(uid) == nullptr) {
206 *netId = mDefaultNetId;
207 fwmark.netId = *netId;
208 fwmark.explicitlySelected = true;
209 return fwmark.intValue;
210 }
211
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900212 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700213 // If a non-zero NetId was explicitly specified, and the user has permission for that
214 // network, use that network's DNS servers. Do not fall through to the default network even
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900215 // if the explicitly selected network is a split tunnel VPN: the explicitlySelected bit
216 // ensures that the VPN fallthrough rule does not match.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700217 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900218
219 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
220 // servers (through the default network). Otherwise, the query is guaranteed to fail.
221 // http://b/29498052
222 Network *network = getNetworkLocked(*netId);
ckenb1a69a42018-12-01 17:45:18 +0900223 if (network && network->getType() == Network::VIRTUAL && !resolv_has_nameservers(*netId)) {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900224 *netId = mDefaultNetId;
225 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700226 } else {
227 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
228 // (possibly falling through to the default network if the VPN doesn't provide a route to
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900229 // them). Otherwise, use the default network's DNS servers. We cannot set the explicit bit
230 // because we need to be able to fall through a split tunnel to the default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700231 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
ckenb1a69a42018-12-01 17:45:18 +0900232 if (virtualNetwork && resolv_has_nameservers(virtualNetwork->getNetId())) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700233 *netId = virtualNetwork->getNetId();
234 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900235 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
236 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700237 *netId = mDefaultNetId;
238 }
239 }
240 fwmark.netId = *netId;
241 return fwmark.intValue;
242}
243
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900244uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800245 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900246 return getNetworkForDnsLocked(netId, uid);
247}
248
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700249// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
250// the VPN that applies to the UID if any; otherwise, the default network.
251unsigned NetworkController::getNetworkForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800252 ScopedRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700253 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700254 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500255 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700256 return mDefaultNetId;
257}
258
259// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
260// applies to the user if any; otherwise, the default network.
261//
262// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
263// is a split-tunnel and disappears later, the socket continues working (since the default network's
264// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
265// high-priority routing rule that doesn't care what NetId the socket has.
266//
267// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
268// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
269// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
270// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
271// the fallthrough rules also go away), the socket that used to fallthrough to the default network
272// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900273unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700274 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
275 if (virtualNetwork && !virtualNetwork->isSecure()) {
276 return virtualNetwork->getNetId();
277 }
278 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500279}
280
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900281unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800282 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900283 return getNetworkForConnectLocked(uid);
284}
285
Erik Klinecea2d342015-06-25 18:24:46 +0900286void NetworkController::getNetworkContext(
287 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800288 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900289
Erik Klinecea2d342015-06-25 18:24:46 +0900290 struct android_net_context nc = {
291 .app_netid = netId,
292 .app_mark = MARK_UNSET,
293 .dns_netid = netId,
294 .dns_mark = MARK_UNSET,
295 .uid = uid,
296 };
297
Erik Kline492ca5b2016-03-09 14:56:00 +0900298 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
299 // client process. This value is nonzero iff.:
300 //
301 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
302 // - [Java] android.net.Network#getAllByName()
303 // - [C/++] android_getaddrinfofornetwork()
304 // 2. The app specified a netid/nethandle to be used as a process default via:
305 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
306 // - [C/++] android_setprocnetwork()
307 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
308 //
309 // In all these cases (with the possible exception of #3), the right thing to do is to treat
310 // such cases as explicitlySelected.
311 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
312 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900313 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900314 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900315
Erik Klinecea2d342015-06-25 18:24:46 +0900316 Fwmark fwmark;
317 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900318 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900319 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
320 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900321 nc.app_mark = fwmark.intValue;
322
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900323 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900324
Erik Kline6d4669f2017-05-25 17:03:31 +0900325 if (DBG) {
326 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
327 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
328 }
329
Erik Klinecea2d342015-06-25 18:24:46 +0900330 if (netcontext) {
331 *netcontext = nc;
332 }
333}
334
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900335unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700336 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700337 if (entry.second->hasInterface(interface)) {
338 return entry.first;
339 }
340 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400341 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500342}
343
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900344unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800345 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900346 return getNetworkForInterfaceLocked(interface);
347}
348
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700349bool NetworkController::isVirtualNetwork(unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800350 ScopedRLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100351 return isVirtualNetworkLocked(netId);
352}
353
354bool NetworkController::isVirtualNetworkLocked(unsigned netId) const {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700355 Network* network = getNetworkLocked(netId);
356 return network && network->getType() == Network::VIRTUAL;
357}
358
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700359int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700360 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
361 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400362 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900363 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700364 }
365
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700366 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700367 ALOGE("duplicate netId %u", netId);
368 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700369 }
370
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700371 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700372 if (int ret = physicalNetwork->setPermission(permission)) {
373 ALOGE("inconceivable! setPermission cannot fail on an empty network");
374 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900375 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700376 }
377
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700378 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900379
380 updateTcpSocketMonitorPolling();
381
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900382 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700383}
384
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700385int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800386 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700387 return createPhysicalNetworkLocked(netId, permission);
388}
389
390int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700391 if (pNetId == nullptr) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700392 return -EINVAL;
393 }
394
Luke Huangd1ee4622018-06-29 13:49:58 +0800395 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700396 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
397 if (!isValidNetworkLocked(*pNetId)) {
398 break;
399 }
400 }
401
402 if (*pNetId > MAX_OEM_ID) {
403 ALOGE("No free network ID");
404 *pNetId = 0;
405 return -ENONET;
406 }
407
408 int ret = createPhysicalNetworkLocked(*pNetId, permission);
409 if (ret) {
410 *pNetId = 0;
411 }
412
413 return ret;
414}
415
cken67cd14c2018-12-05 17:26:59 +0900416int NetworkController::createVirtualNetwork(unsigned netId, bool secure) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800417 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900418
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700419 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700420 ALOGE("invalid netId %u", netId);
421 return -EINVAL;
422 }
423
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900424 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700425 ALOGE("duplicate netId %u", netId);
426 return -EEXIST;
427 }
428
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700429 if (int ret = modifyFallthroughLocked(netId, true)) {
430 return ret;
431 }
cken67cd14c2018-12-05 17:26:59 +0900432 mNetworks[netId] = new VirtualNetwork(netId, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700433 return 0;
434}
435
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700436int NetworkController::destroyNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800437 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900438
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900439 if (netId == LOCAL_NET_ID) {
440 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700441 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700442 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900443 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900444 ALOGE("no such netId %u", netId);
445 return -ENONET;
446 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700447
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700448 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700449
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700450 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900451
452 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
453 // other network code, ignore failures and attempt to clear out as much state as possible, even
454 // if we hit an error on the way. Return the first error that we see.
455 int ret = network->clearInterfaces();
456
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700457 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900458 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700459 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900460 if (!ret) {
461 ret = err;
462 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700463 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700464 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700465 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900466 if (int err = modifyFallthroughLocked(netId, false)) {
467 if (!ret) {
468 ret = err;
469 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700470 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700471 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700472 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700473 delete network;
Bernie Innocenti189eb502018-10-01 23:10:18 +0900474 resolv_delete_cache_for_net(netId);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900475
Rubin Xu6c00b612018-04-27 14:27:59 +0100476 for (auto iter = mIfindexToLastNetId.begin(); iter != mIfindexToLastNetId.end();) {
477 if (iter->second == netId) {
478 iter = mIfindexToLastNetId.erase(iter);
479 } else {
480 ++iter;
481 }
482 }
483
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900484 updateTcpSocketMonitorPolling();
485
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900486 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700487}
488
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700489int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800490 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900491
492 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900493 ALOGE("no such netId %u", netId);
494 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700495 }
496
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900497 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700498 if (existingNetId != NETID_UNSET && existingNetId != netId) {
499 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
500 return -EBUSY;
501 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100502 if (int ret = getNetworkLocked(netId)->addInterface(interface)) {
503 return ret;
504 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700505
Rubin Xu6c00b612018-04-27 14:27:59 +0100506 int ifIndex = RouteController::getIfIndex(interface);
507 if (ifIndex) {
508 mIfindexToLastNetId[ifIndex] = netId;
509 } else {
510 // Cannot happen, since addInterface() above will have failed.
511 ALOGE("inconceivable! added interface %s with no index", interface);
512 }
513 return 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700514}
515
516int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800517 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900518
519 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900520 ALOGE("no such netId %u", netId);
521 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700522 }
523
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700524 return getNetworkLocked(netId)->removeInterface(interface);
525}
526
527Permission NetworkController::getPermissionForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800528 ScopedRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700529 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700530}
531
532void NetworkController::setPermissionForUsers(Permission permission,
533 const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800534 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700535 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700536 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700537 }
538}
539
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900540int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800541 ScopedRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900542 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700543}
544
545int NetworkController::setPermissionForNetworks(Permission permission,
546 const std::vector<unsigned>& netIds) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800547 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700548 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700549 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900550 if (!network) {
551 ALOGE("no such netId %u", netId);
552 return -ENONET;
553 }
554 if (network->getType() != Network::PHYSICAL) {
555 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700556 return -EINVAL;
557 }
558
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700559 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700560 return ret;
561 }
562 }
563 return 0;
564}
565
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700566int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800567 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700568 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900569 if (!network) {
570 ALOGE("no such netId %u", netId);
571 return -ENONET;
572 }
573 if (network->getType() != Network::VIRTUAL) {
574 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700575 return -EINVAL;
576 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900577 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700578 return ret;
579 }
580 return 0;
581}
582
583int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800584 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700585 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900586 if (!network) {
587 ALOGE("no such netId %u", netId);
588 return -ENONET;
589 }
590 if (network->getType() != Network::VIRTUAL) {
591 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700592 return -EINVAL;
593 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900594 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
595 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700596 return ret;
597 }
598 return 0;
599}
600
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700601int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
602 const char* nexthop, bool legacy, uid_t uid) {
603 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
604}
605
606int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
607 const char* nexthop, bool legacy, uid_t uid) {
608 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
609}
610
Rubin Xu6c00b612018-04-27 14:27:59 +0100611void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800612 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100613 if (ifIndex == 0) {
614 ALOGE("Attempting to add address %s without ifindex", address);
615 return;
616 }
617 mAddressToIfindices[address].insert(ifIndex);
618}
619
620// Returns whether we should call SOCK_DESTROY on the removed address.
621bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800622 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100623 // First, update mAddressToIfindices map
624 auto ifindicesIter = mAddressToIfindices.find(address);
625 if (ifindicesIter == mAddressToIfindices.end()) {
626 ALOGE("Removing unknown address %s from ifindex %u", address, ifindex);
627 return true;
628 }
629 std::unordered_set<unsigned>& ifindices = ifindicesIter->second;
630 if (ifindices.erase(ifindex) > 0) {
631 if (ifindices.size() == 0) {
Bernie Innocentidd5a4f02018-07-19 18:06:52 +0900632 mAddressToIfindices.erase(ifindicesIter); // Invalidates ifindices
633 // The address is no longer configured on any interface.
634 return true;
Rubin Xu6c00b612018-04-27 14:27:59 +0100635 }
636 } else {
637 ALOGE("No record of address %s on interface %u", address, ifindex);
638 return true;
639 }
640 // Then, check for VPN handover condition
641 if (mIfindexToLastNetId.find(ifindex) == mIfindexToLastNetId.end()) {
642 ALOGE("Interface index %u was never in a currently-connected netId", ifindex);
643 return true;
644 }
645 unsigned lastNetId = mIfindexToLastNetId[ifindex];
646 for (unsigned idx : ifindices) {
647 unsigned activeNetId = mIfindexToLastNetId[idx];
648 // If this IP address is still assigned to another interface in the same network,
649 // then we don't need to destroy sockets on it because they are likely still valid.
650 // For now we do this only on VPNs.
651 // TODO: evaluate extending this to all network types.
652 if (lastNetId == activeNetId && isVirtualNetworkLocked(activeNetId)) {
653 return false;
654 }
655 }
656 return true;
657}
658
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900659bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700660 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
661 mProtectableUsers.find(uid) != mProtectableUsers.end();
662}
663
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900664bool NetworkController::canProtect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800665 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900666 return canProtectLocked(uid);
667}
668
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700669void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800670 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700671 mProtectableUsers.insert(uids.begin(), uids.end());
672}
673
674void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800675 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700676 for (uid_t uid : uids) {
677 mProtectableUsers.erase(uid);
678 }
679}
680
Erik Kline2d3a1632016-03-15 16:33:48 +0900681void NetworkController::dump(DumpWriter& dw) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800682 ScopedRLock lock(mRWLock);
Erik Kline2d3a1632016-03-15 16:33:48 +0900683
684 dw.incIndent();
685 dw.println("NetworkController");
686
687 dw.incIndent();
688 dw.println("Default network: %u", mDefaultNetId);
689
690 dw.blankline();
691 dw.println("Networks:");
692 dw.incIndent();
693 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900694 Network* network = i.second;
Erik Klineb31fd692018-06-06 20:50:11 +0900695 dw.println(network->toString());
Lorenzo Colittid1029652016-09-26 17:17:40 +0900696 if (network->getType() == Network::PHYSICAL) {
697 dw.incIndent();
698 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
699 dw.println("Required permission: %s", permissionToName(permission));
700 dw.decIndent();
701 }
Pierre Imai3a272072016-04-19 16:17:07 +0900702 android::net::gCtls->resolverCtrl.dump(dw, i.first);
703 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900704 }
705 dw.decIndent();
706
Rubin Xu6c00b612018-04-27 14:27:59 +0100707 dw.blankline();
708 dw.println("Interface <-> last network map:");
709 dw.incIndent();
710 for (const auto& i : mIfindexToLastNetId) {
711 dw.println("Ifindex: %u NetId: %u", i.first, i.second);
712 }
713 dw.decIndent();
714
715 dw.blankline();
716 dw.println("Interface addresses:");
717 dw.incIndent();
718 for (const auto& i : mAddressToIfindices) {
719 dw.println("address: %s ifindices: [%s]", i.first.c_str(),
720 android::base::Join(i.second, ", ").c_str());
721 }
722 dw.decIndent();
723
Erik Kline2d3a1632016-03-15 16:33:48 +0900724 dw.decIndent();
725
726 dw.decIndent();
727}
728
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700729bool NetworkController::isValidNetworkLocked(unsigned netId) const {
730 return getNetworkLocked(netId);
731}
732
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700733Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700734 auto iter = mNetworks.find(netId);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700735 return iter == mNetworks.end() ? nullptr : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700736}
737
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700738VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
739 for (const auto& entry : mNetworks) {
740 if (entry.second->getType() == Network::VIRTUAL) {
741 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
742 if (virtualNetwork->appliesToUser(uid)) {
743 return virtualNetwork;
744 }
745 }
746 }
Yi Kongbdfd57e2018-07-25 13:26:10 -0700747 return nullptr;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700748}
749
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700750Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
751 auto iter = mUsers.find(uid);
752 if (iter != mUsers.end()) {
753 return iter->second;
754 }
755 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
756}
757
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900758int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700759 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900760 if (!network) {
761 return -ENONET;
762 }
763
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700764 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
765 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900766 if (uid == INVALID_UID) {
767 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700768 }
769 Permission userPermission = getPermissionForUserLocked(uid);
770 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900771 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700772 }
773 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900774 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700775 }
776 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
777 if (virtualNetwork && virtualNetwork->isSecure() &&
778 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900779 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700780 }
781 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900782 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700783}
784
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700785int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
786 const char* nexthop, bool add, bool legacy, uid_t uid) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800787 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900788
789 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900790 ALOGE("no such netId %u", netId);
791 return -ENONET;
792 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900793 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900794 if (existingNetId == NETID_UNSET) {
795 ALOGE("interface %s not assigned to any netId", interface);
796 return -ENODEV;
797 }
798 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700799 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900800 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700801 }
802
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700803 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700804 if (netId == LOCAL_NET_ID) {
805 tableType = RouteController::LOCAL_NETWORK;
806 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900807 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700808 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700809 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700810 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700811 }
812 } else {
813 tableType = RouteController::INTERFACE;
814 }
815
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700816 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
817 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700818}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700819
820int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
821 if (mDefaultNetId == NETID_UNSET) {
822 return 0;
823 }
824 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900825 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700826 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
827 return -ESRCH;
828 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900829 if (network->getType() != Network::PHYSICAL) {
830 ALOGE("inconceivable! default network must be a physical network");
831 return -EINVAL;
832 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700833 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
834 for (const auto& physicalInterface : network->getInterfaces()) {
835 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
836 add)) {
837 return ret;
838 }
839 }
840 return 0;
841}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900842
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900843void NetworkController::updateTcpSocketMonitorPolling() {
844 bool physicalNetworkExists = false;
845 for (const auto& entry : mNetworks) {
846 const auto& network = entry.second;
847 if (network->getType() == Network::PHYSICAL && network->getNetId() >= MIN_NET_ID) {
848 physicalNetworkExists = true;
849 break;
850 }
851 }
852
853 if (physicalNetworkExists) {
854 android::net::gCtls->tcpSocketMonitor.resumePolling();
855 } else {
856 android::net::gCtls->tcpSocketMonitor.suspendPolling();
857 }
858}
859
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900860} // namespace net
861} // namespace android