blob: 6a11f92829c99855f4ef366431d279a9af322d94 [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>
Luke Huangcfd04b22019-03-18 15:53:21 +080033#include "log/log.h"
Rubin Xu6c00b612018-04-27 14:27:59 +010034
Pierre Imai3a272072016-04-19 16:17:07 +090035#include "Controllers.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090036#include "DummyNetwork.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070037#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070038#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070039#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070040#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070041#include "VirtualNetwork.h"
Luke Huangb257d612019-03-14 21:19:13 +080042#include "netdutils/DumpWriter.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090043#include "netid_client.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070044
Erik Kline6d4669f2017-05-25 17:03:31 +090045#define DBG 0
46
Luke Huangb257d612019-03-14 21:19:13 +080047using android::netdutils::DumpWriter;
48
Bernie Innocenti762dcf42019-06-14 19:52:49 +090049namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090050
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070051namespace {
52
53// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070054const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070055const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070056
57} // namespace
58
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070059// All calls to methods here are made while holding a write lock on mRWLock.
Lorenzo Colitti2d014e72018-01-10 22:12:29 +090060// They are mostly not called directly from this class, but from methods in PhysicalNetwork.cpp.
61// However, we're the only user of that class, so all calls to those methods come from here and are
62// made under lock.
63// For example, PhysicalNetwork::setPermission ends up calling addFallthrough and removeFallthrough,
64// but it's only called from here under lock (specifically, from createPhysicalNetworkLocked and
65// setPermissionForNetworks).
66// TODO: use std::mutex and GUARDED_BY instead of manual inspection.
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070067class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
Bernie Innocenti762dcf42019-06-14 19:52:49 +090068 public:
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070069 explicit DelegateImpl(NetworkController* networkController);
70 virtual ~DelegateImpl();
71
Bernie Innocenti762dcf42019-06-14 19:52:49 +090072 [[nodiscard]] int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
73 Permission permission, bool add);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070074
Bernie Innocenti762dcf42019-06-14 19:52:49 +090075 private:
76 [[nodiscard]] int addFallthrough(const std::string& physicalInterface,
77 Permission permission) override;
78 [[nodiscard]] int removeFallthrough(const std::string& physicalInterface,
79 Permission permission) override;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070080
Bernie Innocenti762dcf42019-06-14 19:52:49 +090081 [[nodiscard]] int modifyFallthrough(const std::string& physicalInterface, Permission permission,
82 bool add);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070083
84 NetworkController* const mNetworkController;
85};
86
87NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
88 mNetworkController(networkController) {
89}
90
91NetworkController::DelegateImpl::~DelegateImpl() {
92}
93
94int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
95 const std::string& physicalInterface,
96 Permission permission, bool add) {
97 if (add) {
98 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
99 physicalInterface.c_str(),
100 permission)) {
101 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
102 vpnNetId);
103 return ret;
104 }
105 } else {
106 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
107 physicalInterface.c_str(),
108 permission)) {
109 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
110 vpnNetId);
111 return ret;
112 }
113 }
114 return 0;
115}
116
117int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
118 Permission permission) {
119 return modifyFallthrough(physicalInterface, permission, true);
120}
121
122int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
123 Permission permission) {
124 return modifyFallthrough(physicalInterface, permission, false);
125}
126
127int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
128 Permission permission, bool add) {
129 for (const auto& entry : mNetworkController->mNetworks) {
130 if (entry.second->getType() == Network::VIRTUAL) {
131 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
132 return ret;
133 }
134 }
135 }
136 return 0;
137}
138
139NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900140 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
141 mProtectableUsers({AID_VPN}) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700142 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900143 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500144}
145
146unsigned NetworkController::getDefaultNetwork() const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800147 ScopedRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500148 return mDefaultNetId;
149}
150
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700151int NetworkController::setDefaultNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800152 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700153
154 if (netId == mDefaultNetId) {
155 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700156 }
157
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700158 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700159 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900160 if (!network) {
161 ALOGE("no such netId %u", netId);
162 return -ENONET;
163 }
164 if (network->getType() != Network::PHYSICAL) {
165 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700166 return -EINVAL;
167 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700168 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700169 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700170 }
171 }
172
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700173 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700174 Network* network = getNetworkLocked(mDefaultNetId);
175 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700176 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
177 return -ESRCH;
178 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700179 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700180 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700181 }
182 }
183
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700184 mDefaultNetId = netId;
185 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500186}
187
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900188uint32_t NetworkController::getNetworkForDnsLocked(unsigned* netId, uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700189 Fwmark fwmark;
190 fwmark.protectedFromVpn = true;
191 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900192
193 // Common case: there is no VPN that applies to the user, and the query did not specify a netId.
194 // Therefore, it is safe to set the explicit bit on this query and skip all the complex logic
195 // below. While this looks like a special case, it is actually the one that handles the vast
196 // majority of DNS queries.
197 // TODO: untangle this code.
198 if (*netId == NETID_UNSET && getVirtualNetworkForUserLocked(uid) == nullptr) {
199 *netId = mDefaultNetId;
200 fwmark.netId = *netId;
201 fwmark.explicitlySelected = true;
202 return fwmark.intValue;
203 }
204
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900205 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700206 // If a non-zero NetId was explicitly specified, and the user has permission for that
Luke Huangd2861982019-05-17 19:47:28 +0800207 // network, use that network's DNS servers. (possibly falling through the to the default
208 // network if the VPN doesn't provide a route to them).
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700209 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900210
211 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
212 // servers (through the default network). Otherwise, the query is guaranteed to fail.
213 // http://b/29498052
214 Network *network = getNetworkLocked(*netId);
Jooyung Han3e64aa12019-11-27 15:36:29 +0900215 if (network && network->getType() == Network::VIRTUAL && !resolv_has_nameservers(*netId)) {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900216 *netId = mDefaultNetId;
217 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700218 } else {
219 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
220 // (possibly falling through to the default network if the VPN doesn't provide a route to
Luke Huangd2861982019-05-17 19:47:28 +0800221 // them). Otherwise, use the default network's DNS servers.
222 // TODO: Consider if we should set the explicit bit here.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700223 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
Jooyung Han3e64aa12019-11-27 15:36:29 +0900224 if (virtualNetwork && resolv_has_nameservers(virtualNetwork->getNetId())) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700225 *netId = virtualNetwork->getNetId();
226 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900227 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
228 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700229 *netId = mDefaultNetId;
230 }
231 }
232 fwmark.netId = *netId;
233 return fwmark.intValue;
234}
235
236// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
237// the VPN that applies to the UID if any; otherwise, the default network.
238unsigned NetworkController::getNetworkForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800239 ScopedRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700240 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700241 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500242 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700243 return mDefaultNetId;
244}
245
246// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
247// applies to the user if any; otherwise, the default network.
248//
249// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
250// is a split-tunnel and disappears later, the socket continues working (since the default network's
251// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
252// high-priority routing rule that doesn't care what NetId the socket has.
253//
254// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
255// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
256// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
257// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
258// the fallthrough rules also go away), the socket that used to fallthrough to the default network
259// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900260unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700261 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
262 if (virtualNetwork && !virtualNetwork->isSecure()) {
263 return virtualNetwork->getNetId();
264 }
265 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500266}
267
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900268unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800269 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900270 return getNetworkForConnectLocked(uid);
271}
272
Erik Klinecea2d342015-06-25 18:24:46 +0900273void NetworkController::getNetworkContext(
274 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800275 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900276
Erik Klinecea2d342015-06-25 18:24:46 +0900277 struct android_net_context nc = {
278 .app_netid = netId,
279 .app_mark = MARK_UNSET,
280 .dns_netid = netId,
281 .dns_mark = MARK_UNSET,
282 .uid = uid,
283 };
284
Erik Kline492ca5b2016-03-09 14:56:00 +0900285 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
286 // client process. This value is nonzero iff.:
287 //
288 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
289 // - [Java] android.net.Network#getAllByName()
290 // - [C/++] android_getaddrinfofornetwork()
291 // 2. The app specified a netid/nethandle to be used as a process default via:
292 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
293 // - [C/++] android_setprocnetwork()
294 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
295 //
296 // In all these cases (with the possible exception of #3), the right thing to do is to treat
297 // such cases as explicitlySelected.
298 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
299 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900300 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900301 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900302
Erik Klinecea2d342015-06-25 18:24:46 +0900303 Fwmark fwmark;
304 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900305 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900306 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
307 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900308 nc.app_mark = fwmark.intValue;
309
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900310 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900311
Erik Kline6d4669f2017-05-25 17:03:31 +0900312 if (DBG) {
313 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
314 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
315 }
316
Erik Klinecea2d342015-06-25 18:24:46 +0900317 if (netcontext) {
318 *netcontext = nc;
319 }
320}
321
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900322unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700323 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700324 if (entry.second->hasInterface(interface)) {
325 return entry.first;
326 }
327 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400328 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500329}
330
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900331unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800332 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900333 return getNetworkForInterfaceLocked(interface);
334}
335
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700336bool NetworkController::isVirtualNetwork(unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800337 ScopedRLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100338 return isVirtualNetworkLocked(netId);
339}
340
341bool NetworkController::isVirtualNetworkLocked(unsigned netId) const {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700342 Network* network = getNetworkLocked(netId);
343 return network && network->getType() == Network::VIRTUAL;
344}
345
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700346int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700347 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
348 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400349 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900350 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700351 }
352
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700353 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700354 ALOGE("duplicate netId %u", netId);
355 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700356 }
357
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700358 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700359 if (int ret = physicalNetwork->setPermission(permission)) {
360 ALOGE("inconceivable! setPermission cannot fail on an empty network");
361 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900362 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700363 }
364
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700365 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900366
367 updateTcpSocketMonitorPolling();
368
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900369 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700370}
371
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700372int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800373 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700374 return createPhysicalNetworkLocked(netId, permission);
375}
376
377int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700378 if (pNetId == nullptr) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700379 return -EINVAL;
380 }
381
Luke Huangd1ee4622018-06-29 13:49:58 +0800382 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700383 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
384 if (!isValidNetworkLocked(*pNetId)) {
385 break;
386 }
387 }
388
389 if (*pNetId > MAX_OEM_ID) {
390 ALOGE("No free network ID");
391 *pNetId = 0;
392 return -ENONET;
393 }
394
395 int ret = createPhysicalNetworkLocked(*pNetId, permission);
396 if (ret) {
397 *pNetId = 0;
398 }
399
400 return ret;
401}
402
cken67cd14c2018-12-05 17:26:59 +0900403int NetworkController::createVirtualNetwork(unsigned netId, bool secure) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800404 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900405
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700406 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700407 ALOGE("invalid netId %u", netId);
408 return -EINVAL;
409 }
410
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900411 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700412 ALOGE("duplicate netId %u", netId);
413 return -EEXIST;
414 }
415
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700416 if (int ret = modifyFallthroughLocked(netId, true)) {
417 return ret;
418 }
cken67cd14c2018-12-05 17:26:59 +0900419 mNetworks[netId] = new VirtualNetwork(netId, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700420 return 0;
421}
422
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700423int NetworkController::destroyNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800424 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900425
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900426 if (netId == LOCAL_NET_ID) {
427 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700428 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700429 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900430 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900431 ALOGE("no such netId %u", netId);
432 return -ENONET;
433 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700434
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700435 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700436
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700437 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900438
439 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
440 // other network code, ignore failures and attempt to clear out as much state as possible, even
441 // if we hit an error on the way. Return the first error that we see.
442 int ret = network->clearInterfaces();
443
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700444 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900445 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700446 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900447 if (!ret) {
448 ret = err;
449 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700450 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700451 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700452 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900453 if (int err = modifyFallthroughLocked(netId, false)) {
454 if (!ret) {
455 ret = err;
456 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700457 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700458 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700459 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700460 delete network;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900461
Rubin Xu6c00b612018-04-27 14:27:59 +0100462 for (auto iter = mIfindexToLastNetId.begin(); iter != mIfindexToLastNetId.end();) {
463 if (iter->second == netId) {
464 iter = mIfindexToLastNetId.erase(iter);
465 } else {
466 ++iter;
467 }
468 }
469
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900470 updateTcpSocketMonitorPolling();
471
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900472 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700473}
474
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700475int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800476 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900477
478 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900479 ALOGE("no such netId %u", netId);
480 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700481 }
482
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900483 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700484 if (existingNetId != NETID_UNSET && existingNetId != netId) {
485 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
486 return -EBUSY;
487 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100488 if (int ret = getNetworkLocked(netId)->addInterface(interface)) {
489 return ret;
490 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700491
Rubin Xu6c00b612018-04-27 14:27:59 +0100492 int ifIndex = RouteController::getIfIndex(interface);
493 if (ifIndex) {
494 mIfindexToLastNetId[ifIndex] = netId;
495 } else {
496 // Cannot happen, since addInterface() above will have failed.
497 ALOGE("inconceivable! added interface %s with no index", interface);
498 }
499 return 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700500}
501
502int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800503 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900504
505 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900506 ALOGE("no such netId %u", netId);
507 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700508 }
509
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700510 return getNetworkLocked(netId)->removeInterface(interface);
511}
512
513Permission NetworkController::getPermissionForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800514 ScopedRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700515 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700516}
517
518void NetworkController::setPermissionForUsers(Permission permission,
519 const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800520 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700521 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700522 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700523 }
524}
525
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900526int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800527 ScopedRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900528 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700529}
530
531int NetworkController::setPermissionForNetworks(Permission permission,
532 const std::vector<unsigned>& netIds) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800533 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700534 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700535 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900536 if (!network) {
537 ALOGE("no such netId %u", netId);
538 return -ENONET;
539 }
540 if (network->getType() != Network::PHYSICAL) {
541 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700542 return -EINVAL;
543 }
544
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700545 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700546 return ret;
547 }
548 }
549 return 0;
550}
551
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700552int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800553 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700554 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900555 if (!network) {
556 ALOGE("no such netId %u", netId);
557 return -ENONET;
558 }
559 if (network->getType() != Network::VIRTUAL) {
560 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700561 return -EINVAL;
562 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900563 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700564 return ret;
565 }
566 return 0;
567}
568
569int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800570 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700571 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900572 if (!network) {
573 ALOGE("no such netId %u", netId);
574 return -ENONET;
575 }
576 if (network->getType() != Network::VIRTUAL) {
577 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700578 return -EINVAL;
579 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900580 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
581 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700582 return ret;
583 }
584 return 0;
585}
586
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700587int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
Tyler Wearfa94a272019-12-05 15:01:48 -0800588 const char* nexthop, bool legacy, uid_t uid, int mtu) {
589 return modifyRoute(netId, interface, destination, nexthop, ROUTE_ADD, legacy, uid, mtu);
590}
591
592int NetworkController::updateRoute(unsigned netId, const char* interface, const char* destination,
593 const char* nexthop, bool legacy, uid_t uid, int mtu) {
594 return modifyRoute(netId, interface, destination, nexthop, ROUTE_UPDATE, legacy, uid, mtu);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700595}
596
597int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
598 const char* nexthop, bool legacy, uid_t uid) {
Tyler Wearfa94a272019-12-05 15:01:48 -0800599 return modifyRoute(netId, interface, destination, nexthop, ROUTE_REMOVE, legacy, uid, 0);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700600}
601
Rubin Xu6c00b612018-04-27 14:27:59 +0100602void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800603 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100604 if (ifIndex == 0) {
605 ALOGE("Attempting to add address %s without ifindex", address);
606 return;
607 }
608 mAddressToIfindices[address].insert(ifIndex);
609}
610
611// Returns whether we should call SOCK_DESTROY on the removed address.
612bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800613 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100614 // First, update mAddressToIfindices map
615 auto ifindicesIter = mAddressToIfindices.find(address);
616 if (ifindicesIter == mAddressToIfindices.end()) {
617 ALOGE("Removing unknown address %s from ifindex %u", address, ifindex);
618 return true;
619 }
620 std::unordered_set<unsigned>& ifindices = ifindicesIter->second;
621 if (ifindices.erase(ifindex) > 0) {
622 if (ifindices.size() == 0) {
Bernie Innocentidd5a4f02018-07-19 18:06:52 +0900623 mAddressToIfindices.erase(ifindicesIter); // Invalidates ifindices
624 // The address is no longer configured on any interface.
625 return true;
Rubin Xu6c00b612018-04-27 14:27:59 +0100626 }
627 } else {
628 ALOGE("No record of address %s on interface %u", address, ifindex);
629 return true;
630 }
631 // Then, check for VPN handover condition
632 if (mIfindexToLastNetId.find(ifindex) == mIfindexToLastNetId.end()) {
633 ALOGE("Interface index %u was never in a currently-connected netId", ifindex);
634 return true;
635 }
636 unsigned lastNetId = mIfindexToLastNetId[ifindex];
637 for (unsigned idx : ifindices) {
638 unsigned activeNetId = mIfindexToLastNetId[idx];
639 // If this IP address is still assigned to another interface in the same network,
640 // then we don't need to destroy sockets on it because they are likely still valid.
641 // For now we do this only on VPNs.
642 // TODO: evaluate extending this to all network types.
643 if (lastNetId == activeNetId && isVirtualNetworkLocked(activeNetId)) {
644 return false;
645 }
646 }
647 return true;
648}
649
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900650bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700651 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
652 mProtectableUsers.find(uid) != mProtectableUsers.end();
653}
654
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900655bool NetworkController::canProtect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800656 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900657 return canProtectLocked(uid);
658}
659
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700660void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800661 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700662 mProtectableUsers.insert(uids.begin(), uids.end());
663}
664
665void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800666 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700667 for (uid_t uid : uids) {
668 mProtectableUsers.erase(uid);
669 }
670}
671
Erik Kline2d3a1632016-03-15 16:33:48 +0900672void NetworkController::dump(DumpWriter& dw) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800673 ScopedRLock lock(mRWLock);
Erik Kline2d3a1632016-03-15 16:33:48 +0900674
675 dw.incIndent();
676 dw.println("NetworkController");
677
678 dw.incIndent();
679 dw.println("Default network: %u", mDefaultNetId);
680
681 dw.blankline();
682 dw.println("Networks:");
683 dw.incIndent();
684 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900685 Network* network = i.second;
Erik Klineb31fd692018-06-06 20:50:11 +0900686 dw.println(network->toString());
Lorenzo Colittid1029652016-09-26 17:17:40 +0900687 if (network->getType() == Network::PHYSICAL) {
688 dw.incIndent();
689 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
690 dw.println("Required permission: %s", permissionToName(permission));
691 dw.decIndent();
692 }
Pierre Imai3a272072016-04-19 16:17:07 +0900693 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900694 }
695 dw.decIndent();
696
Rubin Xu6c00b612018-04-27 14:27:59 +0100697 dw.blankline();
698 dw.println("Interface <-> last network map:");
699 dw.incIndent();
700 for (const auto& i : mIfindexToLastNetId) {
701 dw.println("Ifindex: %u NetId: %u", i.first, i.second);
702 }
703 dw.decIndent();
704
705 dw.blankline();
706 dw.println("Interface addresses:");
707 dw.incIndent();
708 for (const auto& i : mAddressToIfindices) {
709 dw.println("address: %s ifindices: [%s]", i.first.c_str(),
710 android::base::Join(i.second, ", ").c_str());
711 }
712 dw.decIndent();
713
Erik Kline2d3a1632016-03-15 16:33:48 +0900714 dw.decIndent();
715
716 dw.decIndent();
717}
718
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700719bool NetworkController::isValidNetworkLocked(unsigned netId) const {
720 return getNetworkLocked(netId);
721}
722
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700723Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700724 auto iter = mNetworks.find(netId);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700725 return iter == mNetworks.end() ? nullptr : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700726}
727
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700728VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
729 for (const auto& entry : mNetworks) {
730 if (entry.second->getType() == Network::VIRTUAL) {
731 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
732 if (virtualNetwork->appliesToUser(uid)) {
733 return virtualNetwork;
734 }
735 }
736 }
Yi Kongbdfd57e2018-07-25 13:26:10 -0700737 return nullptr;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700738}
739
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700740Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
741 auto iter = mUsers.find(uid);
742 if (iter != mUsers.end()) {
743 return iter->second;
744 }
745 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
746}
747
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900748int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700749 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900750 if (!network) {
751 return -ENONET;
752 }
753
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700754 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
755 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900756 if (uid == INVALID_UID) {
757 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700758 }
759 Permission userPermission = getPermissionForUserLocked(uid);
760 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900761 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700762 }
763 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900764 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700765 }
766 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
767 if (virtualNetwork && virtualNetwork->isSecure() &&
768 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900769 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700770 }
771 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900772 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700773}
774
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700775int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
Tyler Wearfa94a272019-12-05 15:01:48 -0800776 const char* nexthop, enum RouteOperation op, bool legacy,
777 uid_t uid, int mtu) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800778 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900779
780 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900781 ALOGE("no such netId %u", netId);
782 return -ENONET;
783 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900784 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900785 if (existingNetId == NETID_UNSET) {
786 ALOGE("interface %s not assigned to any netId", interface);
787 return -ENODEV;
788 }
789 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700790 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900791 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700792 }
793
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700794 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700795 if (netId == LOCAL_NET_ID) {
796 tableType = RouteController::LOCAL_NETWORK;
797 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900798 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700799 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700800 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700801 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700802 }
803 } else {
804 tableType = RouteController::INTERFACE;
805 }
806
Tyler Wearfa94a272019-12-05 15:01:48 -0800807 switch (op) {
808 case ROUTE_ADD:
809 return RouteController::addRoute(interface, destination, nexthop, tableType, mtu);
810 case ROUTE_UPDATE:
811 return RouteController::updateRoute(interface, destination, nexthop, tableType, mtu);
812 case ROUTE_REMOVE:
813 return RouteController::removeRoute(interface, destination, nexthop, tableType);
814 }
815 return -EINVAL;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700816}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700817
818int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
819 if (mDefaultNetId == NETID_UNSET) {
820 return 0;
821 }
822 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900823 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700824 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
825 return -ESRCH;
826 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900827 if (network->getType() != Network::PHYSICAL) {
828 ALOGE("inconceivable! default network must be a physical network");
829 return -EINVAL;
830 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700831 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
832 for (const auto& physicalInterface : network->getInterfaces()) {
833 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
834 add)) {
835 return ret;
836 }
837 }
838 return 0;
839}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900840
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900841void NetworkController::updateTcpSocketMonitorPolling() {
842 bool physicalNetworkExists = false;
843 for (const auto& entry : mNetworks) {
844 const auto& network = entry.second;
845 if (network->getType() == Network::PHYSICAL && network->getNetId() >= MIN_NET_ID) {
846 physicalNetworkExists = true;
847 break;
848 }
849 }
850
851 if (physicalNetworkExists) {
852 android::net::gCtls->tcpSocketMonitor.resumePolling();
853 } else {
854 android::net::gCtls->tcpSocketMonitor.suspendPolling();
855 }
856}
857
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900858} // namespace android::net