blob: f7cb8dd39554d686da2bb302e7a1c534ef691ffb [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
209 // network, use that network's DNS servers. Do not fall through to the default network even
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900210 // if the explicitly selected network is a split tunnel VPN: the explicitlySelected bit
211 // ensures that the VPN fallthrough rule does not match.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700212 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900213
214 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
215 // servers (through the default network). Otherwise, the query is guaranteed to fail.
216 // http://b/29498052
217 Network *network = getNetworkLocked(*netId);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900218 if (network && network->getType() == Network::VIRTUAL &&
219 !RESOLV_STUB.resolv_has_nameservers(*netId)) {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900220 *netId = mDefaultNetId;
221 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700222 } else {
223 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
224 // (possibly falling through to the default network if the VPN doesn't provide a route to
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900225 // them). Otherwise, use the default network's DNS servers. We cannot set the explicit bit
226 // because we need to be able to fall through a split tunnel to the default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700227 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900228 if (virtualNetwork && RESOLV_STUB.resolv_has_nameservers(virtualNetwork->getNetId())) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700229 *netId = virtualNetwork->getNetId();
230 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900231 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
232 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700233 *netId = mDefaultNetId;
234 }
235 }
236 fwmark.netId = *netId;
237 return fwmark.intValue;
238}
239
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900240uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800241 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900242 return getNetworkForDnsLocked(netId, uid);
243}
244
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700245// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
246// the VPN that applies to the UID if any; otherwise, the default network.
247unsigned NetworkController::getNetworkForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800248 ScopedRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700249 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700250 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500251 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700252 return mDefaultNetId;
253}
254
255// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
256// applies to the user if any; otherwise, the default network.
257//
258// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
259// is a split-tunnel and disappears later, the socket continues working (since the default network's
260// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
261// high-priority routing rule that doesn't care what NetId the socket has.
262//
263// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
264// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
265// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
266// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
267// the fallthrough rules also go away), the socket that used to fallthrough to the default network
268// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900269unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700270 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
271 if (virtualNetwork && !virtualNetwork->isSecure()) {
272 return virtualNetwork->getNetId();
273 }
274 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500275}
276
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900277unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800278 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900279 return getNetworkForConnectLocked(uid);
280}
281
Erik Klinecea2d342015-06-25 18:24:46 +0900282void NetworkController::getNetworkContext(
283 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800284 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900285
Erik Klinecea2d342015-06-25 18:24:46 +0900286 struct android_net_context nc = {
287 .app_netid = netId,
288 .app_mark = MARK_UNSET,
289 .dns_netid = netId,
290 .dns_mark = MARK_UNSET,
291 .uid = uid,
292 };
293
Erik Kline492ca5b2016-03-09 14:56:00 +0900294 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
295 // client process. This value is nonzero iff.:
296 //
297 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
298 // - [Java] android.net.Network#getAllByName()
299 // - [C/++] android_getaddrinfofornetwork()
300 // 2. The app specified a netid/nethandle to be used as a process default via:
301 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
302 // - [C/++] android_setprocnetwork()
303 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
304 //
305 // In all these cases (with the possible exception of #3), the right thing to do is to treat
306 // such cases as explicitlySelected.
307 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
308 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900309 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900310 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900311
Erik Klinecea2d342015-06-25 18:24:46 +0900312 Fwmark fwmark;
313 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900314 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900315 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
316 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900317 nc.app_mark = fwmark.intValue;
318
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900319 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900320
Erik Kline6d4669f2017-05-25 17:03:31 +0900321 if (DBG) {
322 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
323 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
324 }
325
Erik Klinecea2d342015-06-25 18:24:46 +0900326 if (netcontext) {
327 *netcontext = nc;
328 }
329}
330
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900331unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700332 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700333 if (entry.second->hasInterface(interface)) {
334 return entry.first;
335 }
336 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400337 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500338}
339
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900340unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800341 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900342 return getNetworkForInterfaceLocked(interface);
343}
344
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700345bool NetworkController::isVirtualNetwork(unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800346 ScopedRLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100347 return isVirtualNetworkLocked(netId);
348}
349
350bool NetworkController::isVirtualNetworkLocked(unsigned netId) const {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700351 Network* network = getNetworkLocked(netId);
352 return network && network->getType() == Network::VIRTUAL;
353}
354
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700355int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700356 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
357 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400358 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900359 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700360 }
361
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700362 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700363 ALOGE("duplicate netId %u", netId);
364 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700365 }
366
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700367 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700368 if (int ret = physicalNetwork->setPermission(permission)) {
369 ALOGE("inconceivable! setPermission cannot fail on an empty network");
370 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900371 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700372 }
373
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700374 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900375
376 updateTcpSocketMonitorPolling();
377
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900378 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700379}
380
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700381int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800382 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700383 return createPhysicalNetworkLocked(netId, permission);
384}
385
386int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700387 if (pNetId == nullptr) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700388 return -EINVAL;
389 }
390
Luke Huangd1ee4622018-06-29 13:49:58 +0800391 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700392 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
393 if (!isValidNetworkLocked(*pNetId)) {
394 break;
395 }
396 }
397
398 if (*pNetId > MAX_OEM_ID) {
399 ALOGE("No free network ID");
400 *pNetId = 0;
401 return -ENONET;
402 }
403
404 int ret = createPhysicalNetworkLocked(*pNetId, permission);
405 if (ret) {
406 *pNetId = 0;
407 }
408
409 return ret;
410}
411
cken67cd14c2018-12-05 17:26:59 +0900412int NetworkController::createVirtualNetwork(unsigned netId, bool secure) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800413 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900414
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700415 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700416 ALOGE("invalid netId %u", netId);
417 return -EINVAL;
418 }
419
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900420 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700421 ALOGE("duplicate netId %u", netId);
422 return -EEXIST;
423 }
424
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700425 if (int ret = modifyFallthroughLocked(netId, true)) {
426 return ret;
427 }
cken67cd14c2018-12-05 17:26:59 +0900428 mNetworks[netId] = new VirtualNetwork(netId, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700429 return 0;
430}
431
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700432int NetworkController::destroyNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800433 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900434
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900435 if (netId == LOCAL_NET_ID) {
436 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700437 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700438 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900439 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900440 ALOGE("no such netId %u", netId);
441 return -ENONET;
442 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700443
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700444 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700445
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700446 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900447
448 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
449 // other network code, ignore failures and attempt to clear out as much state as possible, even
450 // if we hit an error on the way. Return the first error that we see.
451 int ret = network->clearInterfaces();
452
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700453 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900454 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700455 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900456 if (!ret) {
457 ret = err;
458 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700459 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700460 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700461 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900462 if (int err = modifyFallthroughLocked(netId, false)) {
463 if (!ret) {
464 ret = err;
465 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700466 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700467 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700468 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700469 delete network;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900470
Rubin Xu6c00b612018-04-27 14:27:59 +0100471 for (auto iter = mIfindexToLastNetId.begin(); iter != mIfindexToLastNetId.end();) {
472 if (iter->second == netId) {
473 iter = mIfindexToLastNetId.erase(iter);
474 } else {
475 ++iter;
476 }
477 }
478
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900479 updateTcpSocketMonitorPolling();
480
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900481 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700482}
483
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700484int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800485 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900486
487 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900488 ALOGE("no such netId %u", netId);
489 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700490 }
491
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900492 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700493 if (existingNetId != NETID_UNSET && existingNetId != netId) {
494 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
495 return -EBUSY;
496 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100497 if (int ret = getNetworkLocked(netId)->addInterface(interface)) {
498 return ret;
499 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700500
Rubin Xu6c00b612018-04-27 14:27:59 +0100501 int ifIndex = RouteController::getIfIndex(interface);
502 if (ifIndex) {
503 mIfindexToLastNetId[ifIndex] = netId;
504 } else {
505 // Cannot happen, since addInterface() above will have failed.
506 ALOGE("inconceivable! added interface %s with no index", interface);
507 }
508 return 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700509}
510
511int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800512 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900513
514 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900515 ALOGE("no such netId %u", netId);
516 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700517 }
518
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700519 return getNetworkLocked(netId)->removeInterface(interface);
520}
521
522Permission NetworkController::getPermissionForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800523 ScopedRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700524 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700525}
526
527void NetworkController::setPermissionForUsers(Permission permission,
528 const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800529 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700530 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700531 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700532 }
533}
534
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900535int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800536 ScopedRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900537 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700538}
539
540int NetworkController::setPermissionForNetworks(Permission permission,
541 const std::vector<unsigned>& netIds) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800542 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700543 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700544 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900545 if (!network) {
546 ALOGE("no such netId %u", netId);
547 return -ENONET;
548 }
549 if (network->getType() != Network::PHYSICAL) {
550 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700551 return -EINVAL;
552 }
553
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700554 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700555 return ret;
556 }
557 }
558 return 0;
559}
560
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700561int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800562 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700563 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900564 if (!network) {
565 ALOGE("no such netId %u", netId);
566 return -ENONET;
567 }
568 if (network->getType() != Network::VIRTUAL) {
569 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700570 return -EINVAL;
571 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900572 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700573 return ret;
574 }
575 return 0;
576}
577
578int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800579 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700580 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900581 if (!network) {
582 ALOGE("no such netId %u", netId);
583 return -ENONET;
584 }
585 if (network->getType() != Network::VIRTUAL) {
586 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700587 return -EINVAL;
588 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900589 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
590 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700591 return ret;
592 }
593 return 0;
594}
595
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700596int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
597 const char* nexthop, bool legacy, uid_t uid) {
598 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
599}
600
601int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
602 const char* nexthop, bool legacy, uid_t uid) {
603 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
604}
605
Rubin Xu6c00b612018-04-27 14:27:59 +0100606void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800607 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100608 if (ifIndex == 0) {
609 ALOGE("Attempting to add address %s without ifindex", address);
610 return;
611 }
612 mAddressToIfindices[address].insert(ifIndex);
613}
614
615// Returns whether we should call SOCK_DESTROY on the removed address.
616bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800617 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100618 // First, update mAddressToIfindices map
619 auto ifindicesIter = mAddressToIfindices.find(address);
620 if (ifindicesIter == mAddressToIfindices.end()) {
621 ALOGE("Removing unknown address %s from ifindex %u", address, ifindex);
622 return true;
623 }
624 std::unordered_set<unsigned>& ifindices = ifindicesIter->second;
625 if (ifindices.erase(ifindex) > 0) {
626 if (ifindices.size() == 0) {
Bernie Innocentidd5a4f02018-07-19 18:06:52 +0900627 mAddressToIfindices.erase(ifindicesIter); // Invalidates ifindices
628 // The address is no longer configured on any interface.
629 return true;
Rubin Xu6c00b612018-04-27 14:27:59 +0100630 }
631 } else {
632 ALOGE("No record of address %s on interface %u", address, ifindex);
633 return true;
634 }
635 // Then, check for VPN handover condition
636 if (mIfindexToLastNetId.find(ifindex) == mIfindexToLastNetId.end()) {
637 ALOGE("Interface index %u was never in a currently-connected netId", ifindex);
638 return true;
639 }
640 unsigned lastNetId = mIfindexToLastNetId[ifindex];
641 for (unsigned idx : ifindices) {
642 unsigned activeNetId = mIfindexToLastNetId[idx];
643 // If this IP address is still assigned to another interface in the same network,
644 // then we don't need to destroy sockets on it because they are likely still valid.
645 // For now we do this only on VPNs.
646 // TODO: evaluate extending this to all network types.
647 if (lastNetId == activeNetId && isVirtualNetworkLocked(activeNetId)) {
648 return false;
649 }
650 }
651 return true;
652}
653
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900654bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700655 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
656 mProtectableUsers.find(uid) != mProtectableUsers.end();
657}
658
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900659bool NetworkController::canProtect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800660 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900661 return canProtectLocked(uid);
662}
663
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700664void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800665 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700666 mProtectableUsers.insert(uids.begin(), uids.end());
667}
668
669void NetworkController::denyProtect(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 for (uid_t uid : uids) {
672 mProtectableUsers.erase(uid);
673 }
674}
675
Erik Kline2d3a1632016-03-15 16:33:48 +0900676void NetworkController::dump(DumpWriter& dw) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800677 ScopedRLock lock(mRWLock);
Erik Kline2d3a1632016-03-15 16:33:48 +0900678
679 dw.incIndent();
680 dw.println("NetworkController");
681
682 dw.incIndent();
683 dw.println("Default network: %u", mDefaultNetId);
684
685 dw.blankline();
686 dw.println("Networks:");
687 dw.incIndent();
688 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900689 Network* network = i.second;
Erik Klineb31fd692018-06-06 20:50:11 +0900690 dw.println(network->toString());
Lorenzo Colittid1029652016-09-26 17:17:40 +0900691 if (network->getType() == Network::PHYSICAL) {
692 dw.incIndent();
693 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
694 dw.println("Required permission: %s", permissionToName(permission));
695 dw.decIndent();
696 }
Pierre Imai3a272072016-04-19 16:17:07 +0900697 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900698 }
699 dw.decIndent();
700
Rubin Xu6c00b612018-04-27 14:27:59 +0100701 dw.blankline();
702 dw.println("Interface <-> last network map:");
703 dw.incIndent();
704 for (const auto& i : mIfindexToLastNetId) {
705 dw.println("Ifindex: %u NetId: %u", i.first, i.second);
706 }
707 dw.decIndent();
708
709 dw.blankline();
710 dw.println("Interface addresses:");
711 dw.incIndent();
712 for (const auto& i : mAddressToIfindices) {
713 dw.println("address: %s ifindices: [%s]", i.first.c_str(),
714 android::base::Join(i.second, ", ").c_str());
715 }
716 dw.decIndent();
717
Erik Kline2d3a1632016-03-15 16:33:48 +0900718 dw.decIndent();
719
720 dw.decIndent();
721}
722
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700723bool NetworkController::isValidNetworkLocked(unsigned netId) const {
724 return getNetworkLocked(netId);
725}
726
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700727Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700728 auto iter = mNetworks.find(netId);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700729 return iter == mNetworks.end() ? nullptr : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700730}
731
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700732VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
733 for (const auto& entry : mNetworks) {
734 if (entry.second->getType() == Network::VIRTUAL) {
735 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
736 if (virtualNetwork->appliesToUser(uid)) {
737 return virtualNetwork;
738 }
739 }
740 }
Yi Kongbdfd57e2018-07-25 13:26:10 -0700741 return nullptr;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700742}
743
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700744Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
745 auto iter = mUsers.find(uid);
746 if (iter != mUsers.end()) {
747 return iter->second;
748 }
749 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
750}
751
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900752int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700753 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900754 if (!network) {
755 return -ENONET;
756 }
757
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700758 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
759 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900760 if (uid == INVALID_UID) {
761 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700762 }
763 Permission userPermission = getPermissionForUserLocked(uid);
764 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900765 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700766 }
767 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900768 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700769 }
770 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
771 if (virtualNetwork && virtualNetwork->isSecure() &&
772 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900773 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700774 }
775 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900776 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700777}
778
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700779int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
780 const char* nexthop, bool add, bool legacy, uid_t uid) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800781 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900782
783 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900784 ALOGE("no such netId %u", netId);
785 return -ENONET;
786 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900787 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900788 if (existingNetId == NETID_UNSET) {
789 ALOGE("interface %s not assigned to any netId", interface);
790 return -ENODEV;
791 }
792 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700793 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900794 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700795 }
796
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700797 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700798 if (netId == LOCAL_NET_ID) {
799 tableType = RouteController::LOCAL_NETWORK;
800 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900801 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700802 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700803 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700804 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700805 }
806 } else {
807 tableType = RouteController::INTERFACE;
808 }
809
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700810 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
811 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700812}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700813
814int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
815 if (mDefaultNetId == NETID_UNSET) {
816 return 0;
817 }
818 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900819 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700820 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
821 return -ESRCH;
822 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900823 if (network->getType() != Network::PHYSICAL) {
824 ALOGE("inconceivable! default network must be a physical network");
825 return -EINVAL;
826 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700827 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
828 for (const auto& physicalInterface : network->getInterfaces()) {
829 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
830 add)) {
831 return ret;
832 }
833 }
834 return 0;
835}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900836
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900837void NetworkController::updateTcpSocketMonitorPolling() {
838 bool physicalNetworkExists = false;
839 for (const auto& entry : mNetworks) {
840 const auto& network = entry.second;
841 if (network->getType() == Network::PHYSICAL && network->getNetId() >= MIN_NET_ID) {
842 physicalNetworkExists = true;
843 break;
844 }
845 }
846
847 if (physicalNetworkExists) {
848 android::net::gCtls->tcpSocketMonitor.resumePolling();
849 } else {
850 android::net::gCtls->tcpSocketMonitor.suspendPolling();
851 }
852}
853
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900854} // namespace net
855} // namespace android