blob: b3297bb63a631625b78d0afd86f3b64cd008ef5b [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 Innocenti34de3ba2019-02-19 18:08:36 +090032#include <cutils/misc.h> // FIRST_APPLICATION_UID
Bernie Innocenti189eb502018-10-01 23:10:18 +090033#include <netd_resolv/resolv.h>
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090034#include <netd_resolv/resolv_stub.h>
Luke Huangc3252cc2018-10-16 15:43:23 +080035#include "android/net/INetd.h"
Rubin Xu6c00b612018-04-27 14:27:59 +010036
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);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900223 if (network && network->getType() == Network::VIRTUAL &&
224 !RESOLV_STUB.resolv_has_nameservers(*netId)) {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900225 *netId = mDefaultNetId;
226 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700227 } else {
228 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
229 // (possibly falling through to the default network if the VPN doesn't provide a route to
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900230 // them). Otherwise, use the default network's DNS servers. We cannot set the explicit bit
231 // because we need to be able to fall through a split tunnel to the default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700232 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900233 if (virtualNetwork && RESOLV_STUB.resolv_has_nameservers(virtualNetwork->getNetId())) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700234 *netId = virtualNetwork->getNetId();
235 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900236 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
237 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700238 *netId = mDefaultNetId;
239 }
240 }
241 fwmark.netId = *netId;
242 return fwmark.intValue;
243}
244
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900245uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800246 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900247 return getNetworkForDnsLocked(netId, uid);
248}
249
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700250// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
251// the VPN that applies to the UID if any; otherwise, the default network.
252unsigned NetworkController::getNetworkForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800253 ScopedRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700254 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700255 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500256 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700257 return mDefaultNetId;
258}
259
260// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
261// applies to the user if any; otherwise, the default network.
262//
263// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
264// is a split-tunnel and disappears later, the socket continues working (since the default network's
265// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
266// high-priority routing rule that doesn't care what NetId the socket has.
267//
268// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
269// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
270// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
271// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
272// the fallthrough rules also go away), the socket that used to fallthrough to the default network
273// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900274unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700275 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
276 if (virtualNetwork && !virtualNetwork->isSecure()) {
277 return virtualNetwork->getNetId();
278 }
279 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500280}
281
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900282unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800283 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900284 return getNetworkForConnectLocked(uid);
285}
286
Erik Klinecea2d342015-06-25 18:24:46 +0900287void NetworkController::getNetworkContext(
288 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800289 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900290
Erik Klinecea2d342015-06-25 18:24:46 +0900291 struct android_net_context nc = {
292 .app_netid = netId,
293 .app_mark = MARK_UNSET,
294 .dns_netid = netId,
295 .dns_mark = MARK_UNSET,
296 .uid = uid,
297 };
298
Erik Kline492ca5b2016-03-09 14:56:00 +0900299 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
300 // client process. This value is nonzero iff.:
301 //
302 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
303 // - [Java] android.net.Network#getAllByName()
304 // - [C/++] android_getaddrinfofornetwork()
305 // 2. The app specified a netid/nethandle to be used as a process default via:
306 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
307 // - [C/++] android_setprocnetwork()
308 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
309 //
310 // In all these cases (with the possible exception of #3), the right thing to do is to treat
311 // such cases as explicitlySelected.
312 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
313 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900314 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900315 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900316
Erik Klinecea2d342015-06-25 18:24:46 +0900317 Fwmark fwmark;
318 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900319 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900320 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
321 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900322 nc.app_mark = fwmark.intValue;
323
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900324 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900325
Erik Kline6d4669f2017-05-25 17:03:31 +0900326 if (DBG) {
327 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
328 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
329 }
330
Erik Klinecea2d342015-06-25 18:24:46 +0900331 if (netcontext) {
332 *netcontext = nc;
333 }
334}
335
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900336unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700337 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700338 if (entry.second->hasInterface(interface)) {
339 return entry.first;
340 }
341 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400342 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500343}
344
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900345unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800346 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900347 return getNetworkForInterfaceLocked(interface);
348}
349
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700350bool NetworkController::isVirtualNetwork(unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800351 ScopedRLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100352 return isVirtualNetworkLocked(netId);
353}
354
355bool NetworkController::isVirtualNetworkLocked(unsigned netId) const {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700356 Network* network = getNetworkLocked(netId);
357 return network && network->getType() == Network::VIRTUAL;
358}
359
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700360int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700361 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
362 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400363 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900364 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700365 }
366
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700367 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700368 ALOGE("duplicate netId %u", netId);
369 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700370 }
371
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700372 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700373 if (int ret = physicalNetwork->setPermission(permission)) {
374 ALOGE("inconceivable! setPermission cannot fail on an empty network");
375 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900376 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700377 }
378
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700379 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900380
381 updateTcpSocketMonitorPolling();
382
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900383 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700384}
385
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700386int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800387 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700388 return createPhysicalNetworkLocked(netId, permission);
389}
390
391int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700392 if (pNetId == nullptr) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700393 return -EINVAL;
394 }
395
Luke Huangd1ee4622018-06-29 13:49:58 +0800396 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700397 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
398 if (!isValidNetworkLocked(*pNetId)) {
399 break;
400 }
401 }
402
403 if (*pNetId > MAX_OEM_ID) {
404 ALOGE("No free network ID");
405 *pNetId = 0;
406 return -ENONET;
407 }
408
409 int ret = createPhysicalNetworkLocked(*pNetId, permission);
410 if (ret) {
411 *pNetId = 0;
412 }
413
414 return ret;
415}
416
cken67cd14c2018-12-05 17:26:59 +0900417int NetworkController::createVirtualNetwork(unsigned netId, bool secure) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800418 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900419
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700420 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700421 ALOGE("invalid netId %u", netId);
422 return -EINVAL;
423 }
424
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900425 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700426 ALOGE("duplicate netId %u", netId);
427 return -EEXIST;
428 }
429
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700430 if (int ret = modifyFallthroughLocked(netId, true)) {
431 return ret;
432 }
cken67cd14c2018-12-05 17:26:59 +0900433 mNetworks[netId] = new VirtualNetwork(netId, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700434 return 0;
435}
436
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700437int NetworkController::destroyNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800438 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900439
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900440 if (netId == LOCAL_NET_ID) {
441 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700442 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700443 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900444 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900445 ALOGE("no such netId %u", netId);
446 return -ENONET;
447 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700448
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700449 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700450
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700451 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900452
453 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
454 // other network code, ignore failures and attempt to clear out as much state as possible, even
455 // if we hit an error on the way. Return the first error that we see.
456 int ret = network->clearInterfaces();
457
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700458 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900459 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700460 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900461 if (!ret) {
462 ret = err;
463 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700464 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700465 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700466 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900467 if (int err = modifyFallthroughLocked(netId, false)) {
468 if (!ret) {
469 ret = err;
470 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700471 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700472 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700473 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700474 delete network;
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900475 RESOLV_STUB.resolv_delete_cache_for_net(netId);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900476
Rubin Xu6c00b612018-04-27 14:27:59 +0100477 for (auto iter = mIfindexToLastNetId.begin(); iter != mIfindexToLastNetId.end();) {
478 if (iter->second == netId) {
479 iter = mIfindexToLastNetId.erase(iter);
480 } else {
481 ++iter;
482 }
483 }
484
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900485 updateTcpSocketMonitorPolling();
486
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900487 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700488}
489
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700490int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800491 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900492
493 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900494 ALOGE("no such netId %u", netId);
495 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700496 }
497
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900498 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700499 if (existingNetId != NETID_UNSET && existingNetId != netId) {
500 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
501 return -EBUSY;
502 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100503 if (int ret = getNetworkLocked(netId)->addInterface(interface)) {
504 return ret;
505 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700506
Rubin Xu6c00b612018-04-27 14:27:59 +0100507 int ifIndex = RouteController::getIfIndex(interface);
508 if (ifIndex) {
509 mIfindexToLastNetId[ifIndex] = netId;
510 } else {
511 // Cannot happen, since addInterface() above will have failed.
512 ALOGE("inconceivable! added interface %s with no index", interface);
513 }
514 return 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700515}
516
517int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800518 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900519
520 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900521 ALOGE("no such netId %u", netId);
522 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700523 }
524
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700525 return getNetworkLocked(netId)->removeInterface(interface);
526}
527
528Permission NetworkController::getPermissionForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800529 ScopedRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700530 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700531}
532
533void NetworkController::setPermissionForUsers(Permission permission,
534 const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800535 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700536 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700537 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700538 }
539}
540
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900541int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800542 ScopedRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900543 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700544}
545
546int NetworkController::setPermissionForNetworks(Permission permission,
547 const std::vector<unsigned>& netIds) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800548 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700549 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700550 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900551 if (!network) {
552 ALOGE("no such netId %u", netId);
553 return -ENONET;
554 }
555 if (network->getType() != Network::PHYSICAL) {
556 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700557 return -EINVAL;
558 }
559
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700560 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700561 return ret;
562 }
563 }
564 return 0;
565}
566
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700567int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800568 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700569 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900570 if (!network) {
571 ALOGE("no such netId %u", netId);
572 return -ENONET;
573 }
574 if (network->getType() != Network::VIRTUAL) {
575 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700576 return -EINVAL;
577 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900578 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700579 return ret;
580 }
581 return 0;
582}
583
584int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800585 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700586 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900587 if (!network) {
588 ALOGE("no such netId %u", netId);
589 return -ENONET;
590 }
591 if (network->getType() != Network::VIRTUAL) {
592 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700593 return -EINVAL;
594 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900595 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
596 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700597 return ret;
598 }
599 return 0;
600}
601
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700602int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
603 const char* nexthop, bool legacy, uid_t uid) {
604 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
605}
606
607int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
608 const char* nexthop, bool legacy, uid_t uid) {
609 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
610}
611
Rubin Xu6c00b612018-04-27 14:27:59 +0100612void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800613 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100614 if (ifIndex == 0) {
615 ALOGE("Attempting to add address %s without ifindex", address);
616 return;
617 }
618 mAddressToIfindices[address].insert(ifIndex);
619}
620
621// Returns whether we should call SOCK_DESTROY on the removed address.
622bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800623 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100624 // First, update mAddressToIfindices map
625 auto ifindicesIter = mAddressToIfindices.find(address);
626 if (ifindicesIter == mAddressToIfindices.end()) {
627 ALOGE("Removing unknown address %s from ifindex %u", address, ifindex);
628 return true;
629 }
630 std::unordered_set<unsigned>& ifindices = ifindicesIter->second;
631 if (ifindices.erase(ifindex) > 0) {
632 if (ifindices.size() == 0) {
Bernie Innocentidd5a4f02018-07-19 18:06:52 +0900633 mAddressToIfindices.erase(ifindicesIter); // Invalidates ifindices
634 // The address is no longer configured on any interface.
635 return true;
Rubin Xu6c00b612018-04-27 14:27:59 +0100636 }
637 } else {
638 ALOGE("No record of address %s on interface %u", address, ifindex);
639 return true;
640 }
641 // Then, check for VPN handover condition
642 if (mIfindexToLastNetId.find(ifindex) == mIfindexToLastNetId.end()) {
643 ALOGE("Interface index %u was never in a currently-connected netId", ifindex);
644 return true;
645 }
646 unsigned lastNetId = mIfindexToLastNetId[ifindex];
647 for (unsigned idx : ifindices) {
648 unsigned activeNetId = mIfindexToLastNetId[idx];
649 // If this IP address is still assigned to another interface in the same network,
650 // then we don't need to destroy sockets on it because they are likely still valid.
651 // For now we do this only on VPNs.
652 // TODO: evaluate extending this to all network types.
653 if (lastNetId == activeNetId && isVirtualNetworkLocked(activeNetId)) {
654 return false;
655 }
656 }
657 return true;
658}
659
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900660bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700661 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
662 mProtectableUsers.find(uid) != mProtectableUsers.end();
663}
664
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900665bool NetworkController::canProtect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800666 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900667 return canProtectLocked(uid);
668}
669
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700670void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800671 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700672 mProtectableUsers.insert(uids.begin(), uids.end());
673}
674
675void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800676 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700677 for (uid_t uid : uids) {
678 mProtectableUsers.erase(uid);
679 }
680}
681
Erik Kline2d3a1632016-03-15 16:33:48 +0900682void NetworkController::dump(DumpWriter& dw) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800683 ScopedRLock lock(mRWLock);
Erik Kline2d3a1632016-03-15 16:33:48 +0900684
685 dw.incIndent();
686 dw.println("NetworkController");
687
688 dw.incIndent();
689 dw.println("Default network: %u", mDefaultNetId);
690
691 dw.blankline();
692 dw.println("Networks:");
693 dw.incIndent();
694 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900695 Network* network = i.second;
Erik Klineb31fd692018-06-06 20:50:11 +0900696 dw.println(network->toString());
Lorenzo Colittid1029652016-09-26 17:17:40 +0900697 if (network->getType() == Network::PHYSICAL) {
698 dw.incIndent();
699 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
700 dw.println("Required permission: %s", permissionToName(permission));
701 dw.decIndent();
702 }
Pierre Imai3a272072016-04-19 16:17:07 +0900703 android::net::gCtls->resolverCtrl.dump(dw, i.first);
704 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900705 }
706 dw.decIndent();
707
Rubin Xu6c00b612018-04-27 14:27:59 +0100708 dw.blankline();
709 dw.println("Interface <-> last network map:");
710 dw.incIndent();
711 for (const auto& i : mIfindexToLastNetId) {
712 dw.println("Ifindex: %u NetId: %u", i.first, i.second);
713 }
714 dw.decIndent();
715
716 dw.blankline();
717 dw.println("Interface addresses:");
718 dw.incIndent();
719 for (const auto& i : mAddressToIfindices) {
720 dw.println("address: %s ifindices: [%s]", i.first.c_str(),
721 android::base::Join(i.second, ", ").c_str());
722 }
723 dw.decIndent();
724
Erik Kline2d3a1632016-03-15 16:33:48 +0900725 dw.decIndent();
726
727 dw.decIndent();
728}
729
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700730bool NetworkController::isValidNetworkLocked(unsigned netId) const {
731 return getNetworkLocked(netId);
732}
733
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700734Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700735 auto iter = mNetworks.find(netId);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700736 return iter == mNetworks.end() ? nullptr : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700737}
738
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700739VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
740 for (const auto& entry : mNetworks) {
741 if (entry.second->getType() == Network::VIRTUAL) {
742 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
743 if (virtualNetwork->appliesToUser(uid)) {
744 return virtualNetwork;
745 }
746 }
747 }
Yi Kongbdfd57e2018-07-25 13:26:10 -0700748 return nullptr;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700749}
750
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700751Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
752 auto iter = mUsers.find(uid);
753 if (iter != mUsers.end()) {
754 return iter->second;
755 }
756 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
757}
758
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900759int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700760 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900761 if (!network) {
762 return -ENONET;
763 }
764
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700765 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
766 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900767 if (uid == INVALID_UID) {
768 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700769 }
770 Permission userPermission = getPermissionForUserLocked(uid);
771 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900772 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700773 }
774 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900775 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700776 }
777 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
778 if (virtualNetwork && virtualNetwork->isSecure() &&
779 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900780 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700781 }
782 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900783 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700784}
785
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700786int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
787 const char* nexthop, bool add, bool legacy, uid_t uid) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800788 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900789
790 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900791 ALOGE("no such netId %u", netId);
792 return -ENONET;
793 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900794 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900795 if (existingNetId == NETID_UNSET) {
796 ALOGE("interface %s not assigned to any netId", interface);
797 return -ENODEV;
798 }
799 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700800 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900801 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700802 }
803
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700804 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700805 if (netId == LOCAL_NET_ID) {
806 tableType = RouteController::LOCAL_NETWORK;
807 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900808 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700809 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700810 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700811 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700812 }
813 } else {
814 tableType = RouteController::INTERFACE;
815 }
816
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700817 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
818 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700819}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700820
821int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
822 if (mDefaultNetId == NETID_UNSET) {
823 return 0;
824 }
825 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900826 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700827 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
828 return -ESRCH;
829 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900830 if (network->getType() != Network::PHYSICAL) {
831 ALOGE("inconceivable! default network must be a physical network");
832 return -EINVAL;
833 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700834 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
835 for (const auto& physicalInterface : network->getInterfaces()) {
836 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
837 add)) {
838 return ret;
839 }
840 }
841 return 0;
842}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900843
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900844void NetworkController::updateTcpSocketMonitorPolling() {
845 bool physicalNetworkExists = false;
846 for (const auto& entry : mNetworks) {
847 const auto& network = entry.second;
848 if (network->getType() == Network::PHYSICAL && network->getNetId() >= MIN_NET_ID) {
849 physicalNetworkExists = true;
850 break;
851 }
852 }
853
854 if (physicalNetworkExists) {
855 android::net::gCtls->tcpSocketMonitor.resumePolling();
856 } else {
857 android::net::gCtls->tcpSocketMonitor.suspendPolling();
858 }
859}
860
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900861} // namespace net
862} // namespace android