blob: 83de531a9e6ba9c8eb35ae957a3a0540b01ccae0 [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
Erik Kline2d3a1632016-03-15 16:33:48 +090026#define LOG_TAG "Netd"
Luke Huangcfd04b22019-03-18 15:53:21 +080027
28#include "NetworkController.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090029
Rubin Xu6c00b612018-04-27 14:27:59 +010030#include <android-base/strings.h>
Bernie Innocenti34de3ba2019-02-19 18:08:36 +090031#include <cutils/misc.h> // FIRST_APPLICATION_UID
Bernie Innocenti189eb502018-10-01 23:10:18 +090032#include <netd_resolv/resolv.h>
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090033#include <netd_resolv/resolv_stub.h>
Luke Huangcfd04b22019-03-18 15:53:21 +080034#include "log/log.h"
Rubin Xu6c00b612018-04-27 14:27:59 +010035
Pierre Imai3a272072016-04-19 16:17:07 +090036#include "Controllers.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090037#include "DummyNetwork.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070038#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070039#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070040#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070041#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070042#include "VirtualNetwork.h"
Luke Huangb257d612019-03-14 21:19:13 +080043#include "netdutils/DumpWriter.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090044#include "netid_client.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070045
Erik Kline6d4669f2017-05-25 17:03:31 +090046#define DBG 0
47
Luke Huangb257d612019-03-14 21:19:13 +080048using android::netdutils::DumpWriter;
49
Lorenzo Colitti7035f222017-02-13 18:29:00 +090050namespace android {
51namespace net {
52
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070053namespace {
54
55// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070056const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070057const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070058
59} // namespace
60
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070061// All calls to methods here are made while holding a write lock on mRWLock.
Lorenzo Colitti2d014e72018-01-10 22:12:29 +090062// They are mostly not called directly from this class, but from methods in PhysicalNetwork.cpp.
63// However, we're the only user of that class, so all calls to those methods come from here and are
64// made under lock.
65// For example, PhysicalNetwork::setPermission ends up calling addFallthrough and removeFallthrough,
66// but it's only called from here under lock (specifically, from createPhysicalNetworkLocked and
67// setPermissionForNetworks).
68// TODO: use std::mutex and GUARDED_BY instead of manual inspection.
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070069class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
70public:
71 explicit DelegateImpl(NetworkController* networkController);
72 virtual ~DelegateImpl();
73
74 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
75 Permission permission, bool add) WARN_UNUSED_RESULT;
76
77private:
78 int addFallthrough(const std::string& physicalInterface,
79 Permission permission) override WARN_UNUSED_RESULT;
80 int removeFallthrough(const std::string& physicalInterface,
81 Permission permission) override WARN_UNUSED_RESULT;
82
83 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
84 bool add) WARN_UNUSED_RESULT;
85
86 NetworkController* const mNetworkController;
87};
88
89NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
90 mNetworkController(networkController) {
91}
92
93NetworkController::DelegateImpl::~DelegateImpl() {
94}
95
96int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
97 const std::string& physicalInterface,
98 Permission permission, bool add) {
99 if (add) {
100 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
101 physicalInterface.c_str(),
102 permission)) {
103 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
104 vpnNetId);
105 return ret;
106 }
107 } else {
108 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
109 physicalInterface.c_str(),
110 permission)) {
111 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
112 vpnNetId);
113 return ret;
114 }
115 }
116 return 0;
117}
118
119int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
120 Permission permission) {
121 return modifyFallthrough(physicalInterface, permission, true);
122}
123
124int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
125 Permission permission) {
126 return modifyFallthrough(physicalInterface, permission, false);
127}
128
129int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
130 Permission permission, bool add) {
131 for (const auto& entry : mNetworkController->mNetworks) {
132 if (entry.second->getType() == Network::VIRTUAL) {
133 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
134 return ret;
135 }
136 }
137 }
138 return 0;
139}
140
141NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900142 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
143 mProtectableUsers({AID_VPN}) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700144 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900145 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500146}
147
148unsigned NetworkController::getDefaultNetwork() const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800149 ScopedRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500150 return mDefaultNetId;
151}
152
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700153int NetworkController::setDefaultNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800154 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700155
156 if (netId == mDefaultNetId) {
157 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700158 }
159
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700160 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700161 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900162 if (!network) {
163 ALOGE("no such netId %u", netId);
164 return -ENONET;
165 }
166 if (network->getType() != Network::PHYSICAL) {
167 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700168 return -EINVAL;
169 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700170 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700171 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700172 }
173 }
174
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700175 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700176 Network* network = getNetworkLocked(mDefaultNetId);
177 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700178 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
179 return -ESRCH;
180 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700181 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700182 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700183 }
184 }
185
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700186 mDefaultNetId = netId;
187 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500188}
189
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900190uint32_t NetworkController::getNetworkForDnsLocked(unsigned* netId, uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700191 Fwmark fwmark;
192 fwmark.protectedFromVpn = true;
193 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900194
195 // Common case: there is no VPN that applies to the user, and the query did not specify a netId.
196 // Therefore, it is safe to set the explicit bit on this query and skip all the complex logic
197 // below. While this looks like a special case, it is actually the one that handles the vast
198 // majority of DNS queries.
199 // TODO: untangle this code.
200 if (*netId == NETID_UNSET && getVirtualNetworkForUserLocked(uid) == nullptr) {
201 *netId = mDefaultNetId;
202 fwmark.netId = *netId;
203 fwmark.explicitlySelected = true;
204 return fwmark.intValue;
205 }
206
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900207 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700208 // If a non-zero NetId was explicitly specified, and the user has permission for that
Luke Huangd2861982019-05-17 19:47:28 +0800209 // network, use that network's DNS servers. (possibly falling through the to the default
210 // network if the VPN doesn't provide a route to them).
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700211 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900212
213 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
214 // servers (through the default network). Otherwise, the query is guaranteed to fail.
215 // http://b/29498052
216 Network *network = getNetworkLocked(*netId);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900217 if (network && network->getType() == Network::VIRTUAL &&
218 !RESOLV_STUB.resolv_has_nameservers(*netId)) {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900219 *netId = mDefaultNetId;
220 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700221 } else {
222 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
223 // (possibly falling through to the default network if the VPN doesn't provide a route to
Luke Huangd2861982019-05-17 19:47:28 +0800224 // them). Otherwise, use the default network's DNS servers.
225 // TODO: Consider if we should set the explicit bit here.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700226 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900227 if (virtualNetwork && RESOLV_STUB.resolv_has_nameservers(virtualNetwork->getNetId())) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700228 *netId = virtualNetwork->getNetId();
229 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900230 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
231 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700232 *netId = mDefaultNetId;
233 }
234 }
235 fwmark.netId = *netId;
236 return fwmark.intValue;
237}
238
239// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
240// the VPN that applies to the UID if any; otherwise, the default network.
241unsigned NetworkController::getNetworkForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800242 ScopedRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700243 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700244 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500245 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700246 return mDefaultNetId;
247}
248
249// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
250// applies to the user if any; otherwise, the default network.
251//
252// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
253// is a split-tunnel and disappears later, the socket continues working (since the default network's
254// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
255// high-priority routing rule that doesn't care what NetId the socket has.
256//
257// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
258// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
259// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
260// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
261// the fallthrough rules also go away), the socket that used to fallthrough to the default network
262// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900263unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700264 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
265 if (virtualNetwork && !virtualNetwork->isSecure()) {
266 return virtualNetwork->getNetId();
267 }
268 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500269}
270
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900271unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800272 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900273 return getNetworkForConnectLocked(uid);
274}
275
Erik Klinecea2d342015-06-25 18:24:46 +0900276void NetworkController::getNetworkContext(
277 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800278 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900279
Erik Klinecea2d342015-06-25 18:24:46 +0900280 struct android_net_context nc = {
281 .app_netid = netId,
282 .app_mark = MARK_UNSET,
283 .dns_netid = netId,
284 .dns_mark = MARK_UNSET,
285 .uid = uid,
286 };
287
Erik Kline492ca5b2016-03-09 14:56:00 +0900288 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
289 // client process. This value is nonzero iff.:
290 //
291 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
292 // - [Java] android.net.Network#getAllByName()
293 // - [C/++] android_getaddrinfofornetwork()
294 // 2. The app specified a netid/nethandle to be used as a process default via:
295 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
296 // - [C/++] android_setprocnetwork()
297 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
298 //
299 // In all these cases (with the possible exception of #3), the right thing to do is to treat
300 // such cases as explicitlySelected.
301 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
302 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900303 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900304 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900305
Erik Klinecea2d342015-06-25 18:24:46 +0900306 Fwmark fwmark;
307 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900308 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900309 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
310 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900311 nc.app_mark = fwmark.intValue;
312
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900313 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900314
Erik Kline6d4669f2017-05-25 17:03:31 +0900315 if (DBG) {
316 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
317 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
318 }
319
Erik Klinecea2d342015-06-25 18:24:46 +0900320 if (netcontext) {
321 *netcontext = nc;
322 }
323}
324
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900325unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700326 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700327 if (entry.second->hasInterface(interface)) {
328 return entry.first;
329 }
330 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400331 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500332}
333
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900334unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800335 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900336 return getNetworkForInterfaceLocked(interface);
337}
338
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700339bool NetworkController::isVirtualNetwork(unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800340 ScopedRLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100341 return isVirtualNetworkLocked(netId);
342}
343
344bool NetworkController::isVirtualNetworkLocked(unsigned netId) const {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700345 Network* network = getNetworkLocked(netId);
346 return network && network->getType() == Network::VIRTUAL;
347}
348
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700349int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700350 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
351 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400352 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900353 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700354 }
355
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700356 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700357 ALOGE("duplicate netId %u", netId);
358 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700359 }
360
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700361 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700362 if (int ret = physicalNetwork->setPermission(permission)) {
363 ALOGE("inconceivable! setPermission cannot fail on an empty network");
364 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900365 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700366 }
367
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700368 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900369
370 updateTcpSocketMonitorPolling();
371
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900372 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700373}
374
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700375int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800376 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700377 return createPhysicalNetworkLocked(netId, permission);
378}
379
380int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700381 if (pNetId == nullptr) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700382 return -EINVAL;
383 }
384
Luke Huangd1ee4622018-06-29 13:49:58 +0800385 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700386 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
387 if (!isValidNetworkLocked(*pNetId)) {
388 break;
389 }
390 }
391
392 if (*pNetId > MAX_OEM_ID) {
393 ALOGE("No free network ID");
394 *pNetId = 0;
395 return -ENONET;
396 }
397
398 int ret = createPhysicalNetworkLocked(*pNetId, permission);
399 if (ret) {
400 *pNetId = 0;
401 }
402
403 return ret;
404}
405
cken67cd14c2018-12-05 17:26:59 +0900406int NetworkController::createVirtualNetwork(unsigned netId, bool secure) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800407 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900408
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700409 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700410 ALOGE("invalid netId %u", netId);
411 return -EINVAL;
412 }
413
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900414 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700415 ALOGE("duplicate netId %u", netId);
416 return -EEXIST;
417 }
418
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700419 if (int ret = modifyFallthroughLocked(netId, true)) {
420 return ret;
421 }
cken67cd14c2018-12-05 17:26:59 +0900422 mNetworks[netId] = new VirtualNetwork(netId, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700423 return 0;
424}
425
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700426int NetworkController::destroyNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800427 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900428
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900429 if (netId == LOCAL_NET_ID) {
430 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700431 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700432 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900433 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900434 ALOGE("no such netId %u", netId);
435 return -ENONET;
436 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700437
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700438 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700439
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700440 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900441
442 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
443 // other network code, ignore failures and attempt to clear out as much state as possible, even
444 // if we hit an error on the way. Return the first error that we see.
445 int ret = network->clearInterfaces();
446
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700447 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900448 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700449 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900450 if (!ret) {
451 ret = err;
452 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700453 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700454 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700455 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900456 if (int err = modifyFallthroughLocked(netId, false)) {
457 if (!ret) {
458 ret = err;
459 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700460 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700461 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700462 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700463 delete network;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900464
Rubin Xu6c00b612018-04-27 14:27:59 +0100465 for (auto iter = mIfindexToLastNetId.begin(); iter != mIfindexToLastNetId.end();) {
466 if (iter->second == netId) {
467 iter = mIfindexToLastNetId.erase(iter);
468 } else {
469 ++iter;
470 }
471 }
472
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900473 updateTcpSocketMonitorPolling();
474
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900475 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700476}
477
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700478int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800479 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900480
481 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900482 ALOGE("no such netId %u", netId);
483 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700484 }
485
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900486 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700487 if (existingNetId != NETID_UNSET && existingNetId != netId) {
488 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
489 return -EBUSY;
490 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100491 if (int ret = getNetworkLocked(netId)->addInterface(interface)) {
492 return ret;
493 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700494
Rubin Xu6c00b612018-04-27 14:27:59 +0100495 int ifIndex = RouteController::getIfIndex(interface);
496 if (ifIndex) {
497 mIfindexToLastNetId[ifIndex] = netId;
498 } else {
499 // Cannot happen, since addInterface() above will have failed.
500 ALOGE("inconceivable! added interface %s with no index", interface);
501 }
502 return 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700503}
504
505int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800506 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900507
508 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900509 ALOGE("no such netId %u", netId);
510 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700511 }
512
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700513 return getNetworkLocked(netId)->removeInterface(interface);
514}
515
516Permission NetworkController::getPermissionForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800517 ScopedRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700518 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700519}
520
521void NetworkController::setPermissionForUsers(Permission permission,
522 const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800523 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700524 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700525 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700526 }
527}
528
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900529int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800530 ScopedRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900531 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700532}
533
534int NetworkController::setPermissionForNetworks(Permission permission,
535 const std::vector<unsigned>& netIds) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800536 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700537 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700538 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900539 if (!network) {
540 ALOGE("no such netId %u", netId);
541 return -ENONET;
542 }
543 if (network->getType() != Network::PHYSICAL) {
544 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700545 return -EINVAL;
546 }
547
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700548 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700549 return ret;
550 }
551 }
552 return 0;
553}
554
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700555int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800556 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700557 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900558 if (!network) {
559 ALOGE("no such netId %u", netId);
560 return -ENONET;
561 }
562 if (network->getType() != Network::VIRTUAL) {
563 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700564 return -EINVAL;
565 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900566 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700567 return ret;
568 }
569 return 0;
570}
571
572int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800573 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700574 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900575 if (!network) {
576 ALOGE("no such netId %u", netId);
577 return -ENONET;
578 }
579 if (network->getType() != Network::VIRTUAL) {
580 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700581 return -EINVAL;
582 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900583 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
584 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700585 return ret;
586 }
587 return 0;
588}
589
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700590int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
591 const char* nexthop, bool legacy, uid_t uid) {
592 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
593}
594
595int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
596 const char* nexthop, bool legacy, uid_t uid) {
597 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
598}
599
Rubin Xu6c00b612018-04-27 14:27:59 +0100600void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800601 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100602 if (ifIndex == 0) {
603 ALOGE("Attempting to add address %s without ifindex", address);
604 return;
605 }
606 mAddressToIfindices[address].insert(ifIndex);
607}
608
609// Returns whether we should call SOCK_DESTROY on the removed address.
610bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800611 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100612 // First, update mAddressToIfindices map
613 auto ifindicesIter = mAddressToIfindices.find(address);
614 if (ifindicesIter == mAddressToIfindices.end()) {
615 ALOGE("Removing unknown address %s from ifindex %u", address, ifindex);
616 return true;
617 }
618 std::unordered_set<unsigned>& ifindices = ifindicesIter->second;
619 if (ifindices.erase(ifindex) > 0) {
620 if (ifindices.size() == 0) {
Bernie Innocentidd5a4f02018-07-19 18:06:52 +0900621 mAddressToIfindices.erase(ifindicesIter); // Invalidates ifindices
622 // The address is no longer configured on any interface.
623 return true;
Rubin Xu6c00b612018-04-27 14:27:59 +0100624 }
625 } else {
626 ALOGE("No record of address %s on interface %u", address, ifindex);
627 return true;
628 }
629 // Then, check for VPN handover condition
630 if (mIfindexToLastNetId.find(ifindex) == mIfindexToLastNetId.end()) {
631 ALOGE("Interface index %u was never in a currently-connected netId", ifindex);
632 return true;
633 }
634 unsigned lastNetId = mIfindexToLastNetId[ifindex];
635 for (unsigned idx : ifindices) {
636 unsigned activeNetId = mIfindexToLastNetId[idx];
637 // If this IP address is still assigned to another interface in the same network,
638 // then we don't need to destroy sockets on it because they are likely still valid.
639 // For now we do this only on VPNs.
640 // TODO: evaluate extending this to all network types.
641 if (lastNetId == activeNetId && isVirtualNetworkLocked(activeNetId)) {
642 return false;
643 }
644 }
645 return true;
646}
647
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900648bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700649 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
650 mProtectableUsers.find(uid) != mProtectableUsers.end();
651}
652
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900653bool NetworkController::canProtect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800654 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900655 return canProtectLocked(uid);
656}
657
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700658void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800659 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700660 mProtectableUsers.insert(uids.begin(), uids.end());
661}
662
663void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800664 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700665 for (uid_t uid : uids) {
666 mProtectableUsers.erase(uid);
667 }
668}
669
Erik Kline2d3a1632016-03-15 16:33:48 +0900670void NetworkController::dump(DumpWriter& dw) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800671 ScopedRLock lock(mRWLock);
Erik Kline2d3a1632016-03-15 16:33:48 +0900672
673 dw.incIndent();
674 dw.println("NetworkController");
675
676 dw.incIndent();
677 dw.println("Default network: %u", mDefaultNetId);
678
679 dw.blankline();
680 dw.println("Networks:");
681 dw.incIndent();
682 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900683 Network* network = i.second;
Erik Klineb31fd692018-06-06 20:50:11 +0900684 dw.println(network->toString());
Lorenzo Colittid1029652016-09-26 17:17:40 +0900685 if (network->getType() == Network::PHYSICAL) {
686 dw.incIndent();
687 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
688 dw.println("Required permission: %s", permissionToName(permission));
689 dw.decIndent();
690 }
Pierre Imai3a272072016-04-19 16:17:07 +0900691 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900692 }
693 dw.decIndent();
694
Rubin Xu6c00b612018-04-27 14:27:59 +0100695 dw.blankline();
696 dw.println("Interface <-> last network map:");
697 dw.incIndent();
698 for (const auto& i : mIfindexToLastNetId) {
699 dw.println("Ifindex: %u NetId: %u", i.first, i.second);
700 }
701 dw.decIndent();
702
703 dw.blankline();
704 dw.println("Interface addresses:");
705 dw.incIndent();
706 for (const auto& i : mAddressToIfindices) {
707 dw.println("address: %s ifindices: [%s]", i.first.c_str(),
708 android::base::Join(i.second, ", ").c_str());
709 }
710 dw.decIndent();
711
Erik Kline2d3a1632016-03-15 16:33:48 +0900712 dw.decIndent();
713
714 dw.decIndent();
715}
716
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700717bool NetworkController::isValidNetworkLocked(unsigned netId) const {
718 return getNetworkLocked(netId);
719}
720
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700721Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700722 auto iter = mNetworks.find(netId);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700723 return iter == mNetworks.end() ? nullptr : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700724}
725
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700726VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
727 for (const auto& entry : mNetworks) {
728 if (entry.second->getType() == Network::VIRTUAL) {
729 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
730 if (virtualNetwork->appliesToUser(uid)) {
731 return virtualNetwork;
732 }
733 }
734 }
Yi Kongbdfd57e2018-07-25 13:26:10 -0700735 return nullptr;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700736}
737
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700738Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
739 auto iter = mUsers.find(uid);
740 if (iter != mUsers.end()) {
741 return iter->second;
742 }
743 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
744}
745
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900746int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700747 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900748 if (!network) {
749 return -ENONET;
750 }
751
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700752 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
753 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900754 if (uid == INVALID_UID) {
755 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700756 }
757 Permission userPermission = getPermissionForUserLocked(uid);
758 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900759 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700760 }
761 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900762 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700763 }
764 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
765 if (virtualNetwork && virtualNetwork->isSecure() &&
766 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900767 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700768 }
769 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900770 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700771}
772
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700773int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
774 const char* nexthop, bool add, bool legacy, uid_t uid) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800775 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900776
777 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900778 ALOGE("no such netId %u", netId);
779 return -ENONET;
780 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900781 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900782 if (existingNetId == NETID_UNSET) {
783 ALOGE("interface %s not assigned to any netId", interface);
784 return -ENODEV;
785 }
786 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700787 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900788 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700789 }
790
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700791 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700792 if (netId == LOCAL_NET_ID) {
793 tableType = RouteController::LOCAL_NETWORK;
794 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900795 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700796 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700797 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700798 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700799 }
800 } else {
801 tableType = RouteController::INTERFACE;
802 }
803
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700804 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
805 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700806}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700807
808int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
809 if (mDefaultNetId == NETID_UNSET) {
810 return 0;
811 }
812 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900813 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700814 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
815 return -ESRCH;
816 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900817 if (network->getType() != Network::PHYSICAL) {
818 ALOGE("inconceivable! default network must be a physical network");
819 return -EINVAL;
820 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700821 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
822 for (const auto& physicalInterface : network->getInterfaces()) {
823 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
824 add)) {
825 return ret;
826 }
827 }
828 return 0;
829}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900830
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900831void NetworkController::updateTcpSocketMonitorPolling() {
832 bool physicalNetworkExists = false;
833 for (const auto& entry : mNetworks) {
834 const auto& network = entry.second;
835 if (network->getType() == Network::PHYSICAL && network->getNetId() >= MIN_NET_ID) {
836 physicalNetworkExists = true;
837 break;
838 }
839 }
840
841 if (physicalNetworkExists) {
842 android::net::gCtls->tcpSocketMonitor.resumePolling();
843 } else {
844 android::net::gCtls->tcpSocketMonitor.suspendPolling();
845 }
846}
847
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900848} // namespace net
849} // namespace android