blob: 2c778301fcfed26d445d3dbd63ecfd97fac00808 [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
Bernie Innocenti762dcf42019-06-14 19:52:49 +090050namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090051
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 Ramachandran48e19b02014-07-22 22:23:20 -070060// All calls to methods here are made while holding a write lock on mRWLock.
Lorenzo Colitti2d014e72018-01-10 22:12:29 +090061// They are mostly not called directly from this class, but from methods in PhysicalNetwork.cpp.
62// However, we're the only user of that class, so all calls to those methods come from here and are
63// made under lock.
64// For example, PhysicalNetwork::setPermission ends up calling addFallthrough and removeFallthrough,
65// but it's only called from here under lock (specifically, from createPhysicalNetworkLocked and
66// setPermissionForNetworks).
67// TODO: use std::mutex and GUARDED_BY instead of manual inspection.
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070068class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
Bernie Innocenti762dcf42019-06-14 19:52:49 +090069 public:
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070070 explicit DelegateImpl(NetworkController* networkController);
71 virtual ~DelegateImpl();
72
Bernie Innocenti762dcf42019-06-14 19:52:49 +090073 [[nodiscard]] int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
74 Permission permission, bool add);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070075
Bernie Innocenti762dcf42019-06-14 19:52:49 +090076 private:
77 [[nodiscard]] int addFallthrough(const std::string& physicalInterface,
78 Permission permission) override;
79 [[nodiscard]] int removeFallthrough(const std::string& physicalInterface,
80 Permission permission) override;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070081
Bernie Innocenti762dcf42019-06-14 19:52:49 +090082 [[nodiscard]] int modifyFallthrough(const std::string& physicalInterface, Permission permission,
83 bool add);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070084
85 NetworkController* const mNetworkController;
86};
87
88NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
89 mNetworkController(networkController) {
90}
91
92NetworkController::DelegateImpl::~DelegateImpl() {
93}
94
95int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
96 const std::string& physicalInterface,
97 Permission permission, bool add) {
98 if (add) {
99 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
100 physicalInterface.c_str(),
101 permission)) {
102 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
103 vpnNetId);
104 return ret;
105 }
106 } else {
107 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
108 physicalInterface.c_str(),
109 permission)) {
110 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
111 vpnNetId);
112 return ret;
113 }
114 }
115 return 0;
116}
117
118int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
119 Permission permission) {
120 return modifyFallthrough(physicalInterface, permission, true);
121}
122
123int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
124 Permission permission) {
125 return modifyFallthrough(physicalInterface, permission, false);
126}
127
128int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
129 Permission permission, bool add) {
130 for (const auto& entry : mNetworkController->mNetworks) {
131 if (entry.second->getType() == Network::VIRTUAL) {
132 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
133 return ret;
134 }
135 }
136 }
137 return 0;
138}
139
140NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900141 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
142 mProtectableUsers({AID_VPN}) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700143 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900144 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500145}
146
147unsigned NetworkController::getDefaultNetwork() const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800148 ScopedRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500149 return mDefaultNetId;
150}
151
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700152int NetworkController::setDefaultNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800153 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700154
155 if (netId == mDefaultNetId) {
156 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700157 }
158
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700159 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700160 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900161 if (!network) {
162 ALOGE("no such netId %u", netId);
163 return -ENONET;
164 }
165 if (network->getType() != Network::PHYSICAL) {
166 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700167 return -EINVAL;
168 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700169 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700170 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700171 }
172 }
173
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700174 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700175 Network* network = getNetworkLocked(mDefaultNetId);
176 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700177 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
178 return -ESRCH;
179 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700180 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700181 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700182 }
183 }
184
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700185 mDefaultNetId = netId;
186 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500187}
188
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900189uint32_t NetworkController::getNetworkForDnsLocked(unsigned* netId, uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700190 Fwmark fwmark;
191 fwmark.protectedFromVpn = true;
192 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900193
194 // Common case: there is no VPN that applies to the user, and the query did not specify a netId.
195 // Therefore, it is safe to set the explicit bit on this query and skip all the complex logic
196 // below. While this looks like a special case, it is actually the one that handles the vast
197 // majority of DNS queries.
198 // TODO: untangle this code.
199 if (*netId == NETID_UNSET && getVirtualNetworkForUserLocked(uid) == nullptr) {
200 *netId = mDefaultNetId;
201 fwmark.netId = *netId;
202 fwmark.explicitlySelected = true;
203 return fwmark.intValue;
204 }
205
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900206 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700207 // If a non-zero NetId was explicitly specified, and the user has permission for that
Luke Huangd2861982019-05-17 19:47:28 +0800208 // network, use that network's DNS servers. (possibly falling through the to the default
209 // network if the VPN doesn't provide a route to them).
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700210 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900211
212 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
213 // servers (through the default network). Otherwise, the query is guaranteed to fail.
214 // http://b/29498052
215 Network *network = getNetworkLocked(*netId);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900216 if (network && network->getType() == Network::VIRTUAL &&
217 !RESOLV_STUB.resolv_has_nameservers(*netId)) {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900218 *netId = mDefaultNetId;
219 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700220 } else {
221 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
222 // (possibly falling through to the default network if the VPN doesn't provide a route to
Luke Huangd2861982019-05-17 19:47:28 +0800223 // them). Otherwise, use the default network's DNS servers.
224 // TODO: Consider if we should set the explicit bit here.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700225 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900226 if (virtualNetwork && RESOLV_STUB.resolv_has_nameservers(virtualNetwork->getNetId())) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700227 *netId = virtualNetwork->getNetId();
228 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900229 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
230 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700231 *netId = mDefaultNetId;
232 }
233 }
234 fwmark.netId = *netId;
235 return fwmark.intValue;
236}
237
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900238uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800239 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900240 return getNetworkForDnsLocked(netId, uid);
241}
242
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700243// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
244// the VPN that applies to the UID if any; otherwise, the default network.
245unsigned NetworkController::getNetworkForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800246 ScopedRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700247 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700248 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500249 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700250 return mDefaultNetId;
251}
252
253// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
254// applies to the user if any; otherwise, the default network.
255//
256// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
257// is a split-tunnel and disappears later, the socket continues working (since the default network's
258// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
259// high-priority routing rule that doesn't care what NetId the socket has.
260//
261// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
262// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
263// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
264// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
265// the fallthrough rules also go away), the socket that used to fallthrough to the default network
266// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900267unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700268 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
269 if (virtualNetwork && !virtualNetwork->isSecure()) {
270 return virtualNetwork->getNetId();
271 }
272 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500273}
274
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900275unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800276 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900277 return getNetworkForConnectLocked(uid);
278}
279
Erik Klinecea2d342015-06-25 18:24:46 +0900280void NetworkController::getNetworkContext(
281 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800282 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900283
Erik Klinecea2d342015-06-25 18:24:46 +0900284 struct android_net_context nc = {
285 .app_netid = netId,
286 .app_mark = MARK_UNSET,
287 .dns_netid = netId,
288 .dns_mark = MARK_UNSET,
289 .uid = uid,
290 };
291
Erik Kline492ca5b2016-03-09 14:56:00 +0900292 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
293 // client process. This value is nonzero iff.:
294 //
295 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
296 // - [Java] android.net.Network#getAllByName()
297 // - [C/++] android_getaddrinfofornetwork()
298 // 2. The app specified a netid/nethandle to be used as a process default via:
299 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
300 // - [C/++] android_setprocnetwork()
301 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
302 //
303 // In all these cases (with the possible exception of #3), the right thing to do is to treat
304 // such cases as explicitlySelected.
305 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
306 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900307 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900308 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900309
Erik Klinecea2d342015-06-25 18:24:46 +0900310 Fwmark fwmark;
311 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900312 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900313 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
314 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900315 nc.app_mark = fwmark.intValue;
316
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900317 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900318
Erik Kline6d4669f2017-05-25 17:03:31 +0900319 if (DBG) {
320 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
321 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
322 }
323
Erik Klinecea2d342015-06-25 18:24:46 +0900324 if (netcontext) {
325 *netcontext = nc;
326 }
327}
328
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900329unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700330 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700331 if (entry.second->hasInterface(interface)) {
332 return entry.first;
333 }
334 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400335 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500336}
337
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900338unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800339 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900340 return getNetworkForInterfaceLocked(interface);
341}
342
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700343bool NetworkController::isVirtualNetwork(unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800344 ScopedRLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100345 return isVirtualNetworkLocked(netId);
346}
347
348bool NetworkController::isVirtualNetworkLocked(unsigned netId) const {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700349 Network* network = getNetworkLocked(netId);
350 return network && network->getType() == Network::VIRTUAL;
351}
352
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700353int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700354 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
355 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400356 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900357 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700358 }
359
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700360 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700361 ALOGE("duplicate netId %u", netId);
362 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700363 }
364
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700365 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700366 if (int ret = physicalNetwork->setPermission(permission)) {
367 ALOGE("inconceivable! setPermission cannot fail on an empty network");
368 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900369 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700370 }
371
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700372 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900373
374 updateTcpSocketMonitorPolling();
375
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900376 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700377}
378
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700379int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800380 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700381 return createPhysicalNetworkLocked(netId, permission);
382}
383
384int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700385 if (pNetId == nullptr) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700386 return -EINVAL;
387 }
388
Luke Huangd1ee4622018-06-29 13:49:58 +0800389 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700390 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
391 if (!isValidNetworkLocked(*pNetId)) {
392 break;
393 }
394 }
395
396 if (*pNetId > MAX_OEM_ID) {
397 ALOGE("No free network ID");
398 *pNetId = 0;
399 return -ENONET;
400 }
401
402 int ret = createPhysicalNetworkLocked(*pNetId, permission);
403 if (ret) {
404 *pNetId = 0;
405 }
406
407 return ret;
408}
409
cken67cd14c2018-12-05 17:26:59 +0900410int NetworkController::createVirtualNetwork(unsigned netId, bool secure) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800411 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900412
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700413 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700414 ALOGE("invalid netId %u", netId);
415 return -EINVAL;
416 }
417
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900418 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700419 ALOGE("duplicate netId %u", netId);
420 return -EEXIST;
421 }
422
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700423 if (int ret = modifyFallthroughLocked(netId, true)) {
424 return ret;
425 }
cken67cd14c2018-12-05 17:26:59 +0900426 mNetworks[netId] = new VirtualNetwork(netId, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700427 return 0;
428}
429
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700430int NetworkController::destroyNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800431 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900432
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900433 if (netId == LOCAL_NET_ID) {
434 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700435 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700436 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900437 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900438 ALOGE("no such netId %u", netId);
439 return -ENONET;
440 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700441
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700442 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700443
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700444 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900445
446 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
447 // other network code, ignore failures and attempt to clear out as much state as possible, even
448 // if we hit an error on the way. Return the first error that we see.
449 int ret = network->clearInterfaces();
450
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700451 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900452 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700453 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900454 if (!ret) {
455 ret = err;
456 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700457 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700458 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700459 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900460 if (int err = modifyFallthroughLocked(netId, false)) {
461 if (!ret) {
462 ret = err;
463 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700464 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700465 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700466 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700467 delete network;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900468
Rubin Xu6c00b612018-04-27 14:27:59 +0100469 for (auto iter = mIfindexToLastNetId.begin(); iter != mIfindexToLastNetId.end();) {
470 if (iter->second == netId) {
471 iter = mIfindexToLastNetId.erase(iter);
472 } else {
473 ++iter;
474 }
475 }
476
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900477 updateTcpSocketMonitorPolling();
478
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900479 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700480}
481
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700482int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800483 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900484
485 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900486 ALOGE("no such netId %u", netId);
487 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700488 }
489
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900490 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700491 if (existingNetId != NETID_UNSET && existingNetId != netId) {
492 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
493 return -EBUSY;
494 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100495 if (int ret = getNetworkLocked(netId)->addInterface(interface)) {
496 return ret;
497 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700498
Rubin Xu6c00b612018-04-27 14:27:59 +0100499 int ifIndex = RouteController::getIfIndex(interface);
500 if (ifIndex) {
501 mIfindexToLastNetId[ifIndex] = netId;
502 } else {
503 // Cannot happen, since addInterface() above will have failed.
504 ALOGE("inconceivable! added interface %s with no index", interface);
505 }
506 return 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700507}
508
509int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800510 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900511
512 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900513 ALOGE("no such netId %u", netId);
514 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700515 }
516
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700517 return getNetworkLocked(netId)->removeInterface(interface);
518}
519
520Permission NetworkController::getPermissionForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800521 ScopedRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700522 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700523}
524
525void NetworkController::setPermissionForUsers(Permission permission,
526 const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800527 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700528 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700529 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700530 }
531}
532
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900533int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800534 ScopedRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900535 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700536}
537
538int NetworkController::setPermissionForNetworks(Permission permission,
539 const std::vector<unsigned>& netIds) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800540 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700541 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700542 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900543 if (!network) {
544 ALOGE("no such netId %u", netId);
545 return -ENONET;
546 }
547 if (network->getType() != Network::PHYSICAL) {
548 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700549 return -EINVAL;
550 }
551
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700552 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700553 return ret;
554 }
555 }
556 return 0;
557}
558
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700559int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800560 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700561 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900562 if (!network) {
563 ALOGE("no such netId %u", netId);
564 return -ENONET;
565 }
566 if (network->getType() != Network::VIRTUAL) {
567 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700568 return -EINVAL;
569 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900570 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700571 return ret;
572 }
573 return 0;
574}
575
576int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800577 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700578 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900579 if (!network) {
580 ALOGE("no such netId %u", netId);
581 return -ENONET;
582 }
583 if (network->getType() != Network::VIRTUAL) {
584 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700585 return -EINVAL;
586 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900587 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
588 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700589 return ret;
590 }
591 return 0;
592}
593
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700594int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
595 const char* nexthop, bool legacy, uid_t uid) {
596 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
597}
598
599int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
600 const char* nexthop, bool legacy, uid_t uid) {
601 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
602}
603
Rubin Xu6c00b612018-04-27 14:27:59 +0100604void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800605 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100606 if (ifIndex == 0) {
607 ALOGE("Attempting to add address %s without ifindex", address);
608 return;
609 }
610 mAddressToIfindices[address].insert(ifIndex);
611}
612
613// Returns whether we should call SOCK_DESTROY on the removed address.
614bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800615 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100616 // First, update mAddressToIfindices map
617 auto ifindicesIter = mAddressToIfindices.find(address);
618 if (ifindicesIter == mAddressToIfindices.end()) {
619 ALOGE("Removing unknown address %s from ifindex %u", address, ifindex);
620 return true;
621 }
622 std::unordered_set<unsigned>& ifindices = ifindicesIter->second;
623 if (ifindices.erase(ifindex) > 0) {
624 if (ifindices.size() == 0) {
Bernie Innocentidd5a4f02018-07-19 18:06:52 +0900625 mAddressToIfindices.erase(ifindicesIter); // Invalidates ifindices
626 // The address is no longer configured on any interface.
627 return true;
Rubin Xu6c00b612018-04-27 14:27:59 +0100628 }
629 } else {
630 ALOGE("No record of address %s on interface %u", address, ifindex);
631 return true;
632 }
633 // Then, check for VPN handover condition
634 if (mIfindexToLastNetId.find(ifindex) == mIfindexToLastNetId.end()) {
635 ALOGE("Interface index %u was never in a currently-connected netId", ifindex);
636 return true;
637 }
638 unsigned lastNetId = mIfindexToLastNetId[ifindex];
639 for (unsigned idx : ifindices) {
640 unsigned activeNetId = mIfindexToLastNetId[idx];
641 // If this IP address is still assigned to another interface in the same network,
642 // then we don't need to destroy sockets on it because they are likely still valid.
643 // For now we do this only on VPNs.
644 // TODO: evaluate extending this to all network types.
645 if (lastNetId == activeNetId && isVirtualNetworkLocked(activeNetId)) {
646 return false;
647 }
648 }
649 return true;
650}
651
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900652bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700653 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
654 mProtectableUsers.find(uid) != mProtectableUsers.end();
655}
656
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900657bool NetworkController::canProtect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800658 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900659 return canProtectLocked(uid);
660}
661
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700662void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800663 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700664 mProtectableUsers.insert(uids.begin(), uids.end());
665}
666
667void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800668 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700669 for (uid_t uid : uids) {
670 mProtectableUsers.erase(uid);
671 }
672}
673
Erik Kline2d3a1632016-03-15 16:33:48 +0900674void NetworkController::dump(DumpWriter& dw) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800675 ScopedRLock lock(mRWLock);
Erik Kline2d3a1632016-03-15 16:33:48 +0900676
677 dw.incIndent();
678 dw.println("NetworkController");
679
680 dw.incIndent();
681 dw.println("Default network: %u", mDefaultNetId);
682
683 dw.blankline();
684 dw.println("Networks:");
685 dw.incIndent();
686 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900687 Network* network = i.second;
Erik Klineb31fd692018-06-06 20:50:11 +0900688 dw.println(network->toString());
Lorenzo Colittid1029652016-09-26 17:17:40 +0900689 if (network->getType() == Network::PHYSICAL) {
690 dw.incIndent();
691 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
692 dw.println("Required permission: %s", permissionToName(permission));
693 dw.decIndent();
694 }
Pierre Imai3a272072016-04-19 16:17:07 +0900695 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900696 }
697 dw.decIndent();
698
Rubin Xu6c00b612018-04-27 14:27:59 +0100699 dw.blankline();
700 dw.println("Interface <-> last network map:");
701 dw.incIndent();
702 for (const auto& i : mIfindexToLastNetId) {
703 dw.println("Ifindex: %u NetId: %u", i.first, i.second);
704 }
705 dw.decIndent();
706
707 dw.blankline();
708 dw.println("Interface addresses:");
709 dw.incIndent();
710 for (const auto& i : mAddressToIfindices) {
711 dw.println("address: %s ifindices: [%s]", i.first.c_str(),
712 android::base::Join(i.second, ", ").c_str());
713 }
714 dw.decIndent();
715
Erik Kline2d3a1632016-03-15 16:33:48 +0900716 dw.decIndent();
717
718 dw.decIndent();
719}
720
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700721bool NetworkController::isValidNetworkLocked(unsigned netId) const {
722 return getNetworkLocked(netId);
723}
724
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700725Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700726 auto iter = mNetworks.find(netId);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700727 return iter == mNetworks.end() ? nullptr : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700728}
729
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700730VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
731 for (const auto& entry : mNetworks) {
732 if (entry.second->getType() == Network::VIRTUAL) {
733 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
734 if (virtualNetwork->appliesToUser(uid)) {
735 return virtualNetwork;
736 }
737 }
738 }
Yi Kongbdfd57e2018-07-25 13:26:10 -0700739 return nullptr;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700740}
741
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700742Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
743 auto iter = mUsers.find(uid);
744 if (iter != mUsers.end()) {
745 return iter->second;
746 }
747 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
748}
749
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900750int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700751 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900752 if (!network) {
753 return -ENONET;
754 }
755
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700756 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
757 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900758 if (uid == INVALID_UID) {
759 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700760 }
761 Permission userPermission = getPermissionForUserLocked(uid);
762 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900763 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700764 }
765 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900766 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700767 }
768 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
769 if (virtualNetwork && virtualNetwork->isSecure() &&
770 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900771 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700772 }
773 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900774 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700775}
776
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700777int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
778 const char* nexthop, bool add, bool legacy, uid_t uid) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800779 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900780
781 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900782 ALOGE("no such netId %u", netId);
783 return -ENONET;
784 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900785 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900786 if (existingNetId == NETID_UNSET) {
787 ALOGE("interface %s not assigned to any netId", interface);
788 return -ENODEV;
789 }
790 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700791 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900792 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700793 }
794
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700795 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700796 if (netId == LOCAL_NET_ID) {
797 tableType = RouteController::LOCAL_NETWORK;
798 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900799 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700800 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700801 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700802 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700803 }
804 } else {
805 tableType = RouteController::INTERFACE;
806 }
807
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700808 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
809 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700810}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700811
812int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
813 if (mDefaultNetId == NETID_UNSET) {
814 return 0;
815 }
816 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900817 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700818 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
819 return -ESRCH;
820 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900821 if (network->getType() != Network::PHYSICAL) {
822 ALOGE("inconceivable! default network must be a physical network");
823 return -EINVAL;
824 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700825 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
826 for (const auto& physicalInterface : network->getInterfaces()) {
827 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
828 add)) {
829 return ret;
830 }
831 }
832 return 0;
833}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900834
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900835void NetworkController::updateTcpSocketMonitorPolling() {
836 bool physicalNetworkExists = false;
837 for (const auto& entry : mNetworks) {
838 const auto& network = entry.second;
839 if (network->getType() == Network::PHYSICAL && network->getNetId() >= MIN_NET_ID) {
840 physicalNetworkExists = true;
841 break;
842 }
843 }
844
845 if (physicalNetworkExists) {
846 android::net::gCtls->tcpSocketMonitor.resumePolling();
847 } else {
848 android::net::gCtls->tcpSocketMonitor.suspendPolling();
849 }
850}
851
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900852} // namespace android::net