blob: e490a9f00569255dd1422dede457302d8be25971 [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
238// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
239// the VPN that applies to the UID if any; otherwise, the default network.
240unsigned NetworkController::getNetworkForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800241 ScopedRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700242 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700243 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500244 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700245 return mDefaultNetId;
246}
247
248// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
249// applies to the user if any; otherwise, the default network.
250//
251// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
252// is a split-tunnel and disappears later, the socket continues working (since the default network's
253// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
254// high-priority routing rule that doesn't care what NetId the socket has.
255//
256// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
257// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
258// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
259// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
260// the fallthrough rules also go away), the socket that used to fallthrough to the default network
261// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900262unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700263 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
264 if (virtualNetwork && !virtualNetwork->isSecure()) {
265 return virtualNetwork->getNetId();
266 }
267 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500268}
269
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900270unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800271 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900272 return getNetworkForConnectLocked(uid);
273}
274
Erik Klinecea2d342015-06-25 18:24:46 +0900275void NetworkController::getNetworkContext(
276 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800277 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900278
Erik Klinecea2d342015-06-25 18:24:46 +0900279 struct android_net_context nc = {
280 .app_netid = netId,
281 .app_mark = MARK_UNSET,
282 .dns_netid = netId,
283 .dns_mark = MARK_UNSET,
284 .uid = uid,
285 };
286
Erik Kline492ca5b2016-03-09 14:56:00 +0900287 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
288 // client process. This value is nonzero iff.:
289 //
290 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
291 // - [Java] android.net.Network#getAllByName()
292 // - [C/++] android_getaddrinfofornetwork()
293 // 2. The app specified a netid/nethandle to be used as a process default via:
294 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
295 // - [C/++] android_setprocnetwork()
296 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
297 //
298 // In all these cases (with the possible exception of #3), the right thing to do is to treat
299 // such cases as explicitlySelected.
300 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
301 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900302 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900303 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900304
Erik Klinecea2d342015-06-25 18:24:46 +0900305 Fwmark fwmark;
306 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900307 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900308 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
309 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900310 nc.app_mark = fwmark.intValue;
311
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900312 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900313
Erik Kline6d4669f2017-05-25 17:03:31 +0900314 if (DBG) {
315 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
316 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
317 }
318
Erik Klinecea2d342015-06-25 18:24:46 +0900319 if (netcontext) {
320 *netcontext = nc;
321 }
322}
323
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900324unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700325 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700326 if (entry.second->hasInterface(interface)) {
327 return entry.first;
328 }
329 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400330 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500331}
332
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900333unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800334 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900335 return getNetworkForInterfaceLocked(interface);
336}
337
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700338bool NetworkController::isVirtualNetwork(unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800339 ScopedRLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100340 return isVirtualNetworkLocked(netId);
341}
342
343bool NetworkController::isVirtualNetworkLocked(unsigned netId) const {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700344 Network* network = getNetworkLocked(netId);
345 return network && network->getType() == Network::VIRTUAL;
346}
347
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700348int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700349 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
350 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400351 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900352 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700353 }
354
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700355 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700356 ALOGE("duplicate netId %u", netId);
357 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700358 }
359
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700360 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700361 if (int ret = physicalNetwork->setPermission(permission)) {
362 ALOGE("inconceivable! setPermission cannot fail on an empty network");
363 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900364 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700365 }
366
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700367 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900368
369 updateTcpSocketMonitorPolling();
370
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900371 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700372}
373
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700374int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800375 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700376 return createPhysicalNetworkLocked(netId, permission);
377}
378
379int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700380 if (pNetId == nullptr) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700381 return -EINVAL;
382 }
383
Luke Huangd1ee4622018-06-29 13:49:58 +0800384 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700385 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
386 if (!isValidNetworkLocked(*pNetId)) {
387 break;
388 }
389 }
390
391 if (*pNetId > MAX_OEM_ID) {
392 ALOGE("No free network ID");
393 *pNetId = 0;
394 return -ENONET;
395 }
396
397 int ret = createPhysicalNetworkLocked(*pNetId, permission);
398 if (ret) {
399 *pNetId = 0;
400 }
401
402 return ret;
403}
404
cken67cd14c2018-12-05 17:26:59 +0900405int NetworkController::createVirtualNetwork(unsigned netId, bool secure) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800406 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900407
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700408 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700409 ALOGE("invalid netId %u", netId);
410 return -EINVAL;
411 }
412
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900413 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700414 ALOGE("duplicate netId %u", netId);
415 return -EEXIST;
416 }
417
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700418 if (int ret = modifyFallthroughLocked(netId, true)) {
419 return ret;
420 }
cken67cd14c2018-12-05 17:26:59 +0900421 mNetworks[netId] = new VirtualNetwork(netId, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700422 return 0;
423}
424
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700425int NetworkController::destroyNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800426 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900427
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900428 if (netId == LOCAL_NET_ID) {
429 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700430 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700431 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900432 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900433 ALOGE("no such netId %u", netId);
434 return -ENONET;
435 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700436
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700437 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700438
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700439 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900440
441 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
442 // other network code, ignore failures and attempt to clear out as much state as possible, even
443 // if we hit an error on the way. Return the first error that we see.
444 int ret = network->clearInterfaces();
445
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700446 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900447 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700448 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900449 if (!ret) {
450 ret = err;
451 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700452 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700453 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700454 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900455 if (int err = modifyFallthroughLocked(netId, false)) {
456 if (!ret) {
457 ret = err;
458 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700459 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700460 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700461 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700462 delete network;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900463
Rubin Xu6c00b612018-04-27 14:27:59 +0100464 for (auto iter = mIfindexToLastNetId.begin(); iter != mIfindexToLastNetId.end();) {
465 if (iter->second == netId) {
466 iter = mIfindexToLastNetId.erase(iter);
467 } else {
468 ++iter;
469 }
470 }
471
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900472 updateTcpSocketMonitorPolling();
473
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900474 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700475}
476
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700477int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800478 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900479
480 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900481 ALOGE("no such netId %u", netId);
482 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700483 }
484
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900485 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700486 if (existingNetId != NETID_UNSET && existingNetId != netId) {
487 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
488 return -EBUSY;
489 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100490 if (int ret = getNetworkLocked(netId)->addInterface(interface)) {
491 return ret;
492 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700493
Rubin Xu6c00b612018-04-27 14:27:59 +0100494 int ifIndex = RouteController::getIfIndex(interface);
495 if (ifIndex) {
496 mIfindexToLastNetId[ifIndex] = netId;
497 } else {
498 // Cannot happen, since addInterface() above will have failed.
499 ALOGE("inconceivable! added interface %s with no index", interface);
500 }
501 return 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700502}
503
504int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800505 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900506
507 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900508 ALOGE("no such netId %u", netId);
509 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700510 }
511
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700512 return getNetworkLocked(netId)->removeInterface(interface);
513}
514
515Permission NetworkController::getPermissionForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800516 ScopedRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700517 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700518}
519
520void NetworkController::setPermissionForUsers(Permission permission,
521 const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800522 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700523 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700524 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700525 }
526}
527
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900528int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800529 ScopedRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900530 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700531}
532
533int NetworkController::setPermissionForNetworks(Permission permission,
534 const std::vector<unsigned>& netIds) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800535 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700536 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700537 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900538 if (!network) {
539 ALOGE("no such netId %u", netId);
540 return -ENONET;
541 }
542 if (network->getType() != Network::PHYSICAL) {
543 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700544 return -EINVAL;
545 }
546
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700547 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700548 return ret;
549 }
550 }
551 return 0;
552}
553
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700554int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800555 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700556 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900557 if (!network) {
558 ALOGE("no such netId %u", netId);
559 return -ENONET;
560 }
561 if (network->getType() != Network::VIRTUAL) {
562 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700563 return -EINVAL;
564 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900565 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700566 return ret;
567 }
568 return 0;
569}
570
571int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800572 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700573 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900574 if (!network) {
575 ALOGE("no such netId %u", netId);
576 return -ENONET;
577 }
578 if (network->getType() != Network::VIRTUAL) {
579 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700580 return -EINVAL;
581 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900582 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
583 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700584 return ret;
585 }
586 return 0;
587}
588
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700589int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
590 const char* nexthop, bool legacy, uid_t uid) {
591 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
592}
593
594int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
595 const char* nexthop, bool legacy, uid_t uid) {
596 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
597}
598
Rubin Xu6c00b612018-04-27 14:27:59 +0100599void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800600 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100601 if (ifIndex == 0) {
602 ALOGE("Attempting to add address %s without ifindex", address);
603 return;
604 }
605 mAddressToIfindices[address].insert(ifIndex);
606}
607
608// Returns whether we should call SOCK_DESTROY on the removed address.
609bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800610 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100611 // First, update mAddressToIfindices map
612 auto ifindicesIter = mAddressToIfindices.find(address);
613 if (ifindicesIter == mAddressToIfindices.end()) {
614 ALOGE("Removing unknown address %s from ifindex %u", address, ifindex);
615 return true;
616 }
617 std::unordered_set<unsigned>& ifindices = ifindicesIter->second;
618 if (ifindices.erase(ifindex) > 0) {
619 if (ifindices.size() == 0) {
Bernie Innocentidd5a4f02018-07-19 18:06:52 +0900620 mAddressToIfindices.erase(ifindicesIter); // Invalidates ifindices
621 // The address is no longer configured on any interface.
622 return true;
Rubin Xu6c00b612018-04-27 14:27:59 +0100623 }
624 } else {
625 ALOGE("No record of address %s on interface %u", address, ifindex);
626 return true;
627 }
628 // Then, check for VPN handover condition
629 if (mIfindexToLastNetId.find(ifindex) == mIfindexToLastNetId.end()) {
630 ALOGE("Interface index %u was never in a currently-connected netId", ifindex);
631 return true;
632 }
633 unsigned lastNetId = mIfindexToLastNetId[ifindex];
634 for (unsigned idx : ifindices) {
635 unsigned activeNetId = mIfindexToLastNetId[idx];
636 // If this IP address is still assigned to another interface in the same network,
637 // then we don't need to destroy sockets on it because they are likely still valid.
638 // For now we do this only on VPNs.
639 // TODO: evaluate extending this to all network types.
640 if (lastNetId == activeNetId && isVirtualNetworkLocked(activeNetId)) {
641 return false;
642 }
643 }
644 return true;
645}
646
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900647bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700648 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
649 mProtectableUsers.find(uid) != mProtectableUsers.end();
650}
651
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900652bool NetworkController::canProtect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800653 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900654 return canProtectLocked(uid);
655}
656
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700657void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800658 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700659 mProtectableUsers.insert(uids.begin(), uids.end());
660}
661
662void NetworkController::denyProtect(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 for (uid_t uid : uids) {
665 mProtectableUsers.erase(uid);
666 }
667}
668
Erik Kline2d3a1632016-03-15 16:33:48 +0900669void NetworkController::dump(DumpWriter& dw) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800670 ScopedRLock lock(mRWLock);
Erik Kline2d3a1632016-03-15 16:33:48 +0900671
672 dw.incIndent();
673 dw.println("NetworkController");
674
675 dw.incIndent();
676 dw.println("Default network: %u", mDefaultNetId);
677
678 dw.blankline();
679 dw.println("Networks:");
680 dw.incIndent();
681 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900682 Network* network = i.second;
Erik Klineb31fd692018-06-06 20:50:11 +0900683 dw.println(network->toString());
Lorenzo Colittid1029652016-09-26 17:17:40 +0900684 if (network->getType() == Network::PHYSICAL) {
685 dw.incIndent();
686 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
687 dw.println("Required permission: %s", permissionToName(permission));
688 dw.decIndent();
689 }
Pierre Imai3a272072016-04-19 16:17:07 +0900690 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900691 }
692 dw.decIndent();
693
Rubin Xu6c00b612018-04-27 14:27:59 +0100694 dw.blankline();
695 dw.println("Interface <-> last network map:");
696 dw.incIndent();
697 for (const auto& i : mIfindexToLastNetId) {
698 dw.println("Ifindex: %u NetId: %u", i.first, i.second);
699 }
700 dw.decIndent();
701
702 dw.blankline();
703 dw.println("Interface addresses:");
704 dw.incIndent();
705 for (const auto& i : mAddressToIfindices) {
706 dw.println("address: %s ifindices: [%s]", i.first.c_str(),
707 android::base::Join(i.second, ", ").c_str());
708 }
709 dw.decIndent();
710
Erik Kline2d3a1632016-03-15 16:33:48 +0900711 dw.decIndent();
712
713 dw.decIndent();
714}
715
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700716bool NetworkController::isValidNetworkLocked(unsigned netId) const {
717 return getNetworkLocked(netId);
718}
719
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700720Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700721 auto iter = mNetworks.find(netId);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700722 return iter == mNetworks.end() ? nullptr : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700723}
724
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700725VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
726 for (const auto& entry : mNetworks) {
727 if (entry.second->getType() == Network::VIRTUAL) {
728 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
729 if (virtualNetwork->appliesToUser(uid)) {
730 return virtualNetwork;
731 }
732 }
733 }
Yi Kongbdfd57e2018-07-25 13:26:10 -0700734 return nullptr;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700735}
736
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700737Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
738 auto iter = mUsers.find(uid);
739 if (iter != mUsers.end()) {
740 return iter->second;
741 }
742 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
743}
744
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900745int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700746 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900747 if (!network) {
748 return -ENONET;
749 }
750
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700751 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
752 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900753 if (uid == INVALID_UID) {
754 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700755 }
756 Permission userPermission = getPermissionForUserLocked(uid);
757 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900758 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700759 }
760 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900761 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700762 }
763 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
764 if (virtualNetwork && virtualNetwork->isSecure() &&
765 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900766 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700767 }
768 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900769 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700770}
771
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700772int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
773 const char* nexthop, bool add, bool legacy, uid_t uid) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800774 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900775
776 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900777 ALOGE("no such netId %u", netId);
778 return -ENONET;
779 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900780 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900781 if (existingNetId == NETID_UNSET) {
782 ALOGE("interface %s not assigned to any netId", interface);
783 return -ENODEV;
784 }
785 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700786 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900787 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700788 }
789
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700790 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700791 if (netId == LOCAL_NET_ID) {
792 tableType = RouteController::LOCAL_NETWORK;
793 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900794 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700795 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700796 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700797 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700798 }
799 } else {
800 tableType = RouteController::INTERFACE;
801 }
802
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700803 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
804 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700805}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700806
807int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
808 if (mDefaultNetId == NETID_UNSET) {
809 return 0;
810 }
811 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900812 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700813 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
814 return -ESRCH;
815 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900816 if (network->getType() != Network::PHYSICAL) {
817 ALOGE("inconceivable! default network must be a physical network");
818 return -EINVAL;
819 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700820 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
821 for (const auto& physicalInterface : network->getInterfaces()) {
822 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
823 add)) {
824 return ret;
825 }
826 }
827 return 0;
828}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900829
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900830void NetworkController::updateTcpSocketMonitorPolling() {
831 bool physicalNetworkExists = false;
832 for (const auto& entry : mNetworks) {
833 const auto& network = entry.second;
834 if (network->getType() == Network::PHYSICAL && network->getNetId() >= MIN_NET_ID) {
835 physicalNetworkExists = true;
836 break;
837 }
838 }
839
840 if (physicalNetworkExists) {
841 android::net::gCtls->tcpSocketMonitor.resumePolling();
842 } else {
843 android::net::gCtls->tcpSocketMonitor.suspendPolling();
844 }
845}
846
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900847} // namespace android::net