blob: a77a547d8f0b1174b2c65c5c45e9bc82ad04b67c [file] [log] [blame]
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -05001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sreeram Ramachandran72604072014-05-21 13:19:43 -070017// THREAD-SAFETY
18// -------------
19// The methods in this file are called from multiple threads (from CommandListener, FwmarkServer
20// and DnsProxyListener). So, all accesses to shared state are guarded by a lock.
21//
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +090022// Public functions accessible by external callers should be thread-safe and are responsible for
23// acquiring the lock. Private functions in this file should call xxxLocked() methods and access
24// internal state directly.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050025
26#include "NetworkController.h"
27
Erik Kline2d3a1632016-03-15 16:33:48 +090028#define LOG_TAG "Netd"
29#include "log/log.h"
30
Rubin Xu6c00b612018-04-27 14:27:59 +010031#include <android-base/strings.h>
Bernie Innocenti34de3ba2019-02-19 18:08:36 +090032#include <cutils/misc.h> // FIRST_APPLICATION_UID
Bernie Innocenti189eb502018-10-01 23:10:18 +090033#include <netd_resolv/resolv.h>
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090034#include <netd_resolv/resolv_stub.h>
Luke Huangc3252cc2018-10-16 15:43:23 +080035#include "android/net/INetd.h"
Rubin Xu6c00b612018-04-27 14:27:59 +010036
Pierre Imai3a272072016-04-19 16:17:07 +090037#include "Controllers.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090038#include "DummyNetwork.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070039#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070040#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070041#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070042#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070043#include "VirtualNetwork.h"
Luke Huangb257d612019-03-14 21:19:13 +080044#include "netdutils/DumpWriter.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090045#include "netid_client.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070046
Erik Kline6d4669f2017-05-25 17:03:31 +090047#define DBG 0
48
Luke Huangb257d612019-03-14 21:19:13 +080049using android::netdutils::DumpWriter;
50
Lorenzo Colitti7035f222017-02-13 18:29:00 +090051namespace android {
52namespace net {
53
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070054namespace {
55
56// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070057const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070058const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070059
60} // namespace
61
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070062const unsigned NetworkController::MIN_OEM_ID = 1;
63const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090064const unsigned NetworkController::DUMMY_NET_ID = 51;
65// NetIds 52..98 are reserved for future use.
Luke Huangc3252cc2018-10-16 15:43:23 +080066const unsigned NetworkController::LOCAL_NET_ID = INetd::LOCAL_NET_ID;
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070067
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070068// All calls to methods here are made while holding a write lock on mRWLock.
Lorenzo Colitti2d014e72018-01-10 22:12:29 +090069// They are mostly not called directly from this class, but from methods in PhysicalNetwork.cpp.
70// However, we're the only user of that class, so all calls to those methods come from here and are
71// made under lock.
72// For example, PhysicalNetwork::setPermission ends up calling addFallthrough and removeFallthrough,
73// but it's only called from here under lock (specifically, from createPhysicalNetworkLocked and
74// setPermissionForNetworks).
75// TODO: use std::mutex and GUARDED_BY instead of manual inspection.
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070076class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
77public:
78 explicit DelegateImpl(NetworkController* networkController);
79 virtual ~DelegateImpl();
80
81 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
82 Permission permission, bool add) WARN_UNUSED_RESULT;
83
84private:
85 int addFallthrough(const std::string& physicalInterface,
86 Permission permission) override WARN_UNUSED_RESULT;
87 int removeFallthrough(const std::string& physicalInterface,
88 Permission permission) override WARN_UNUSED_RESULT;
89
90 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
91 bool add) WARN_UNUSED_RESULT;
92
93 NetworkController* const mNetworkController;
94};
95
96NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
97 mNetworkController(networkController) {
98}
99
100NetworkController::DelegateImpl::~DelegateImpl() {
101}
102
103int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
104 const std::string& physicalInterface,
105 Permission permission, bool add) {
106 if (add) {
107 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
108 physicalInterface.c_str(),
109 permission)) {
110 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
111 vpnNetId);
112 return ret;
113 }
114 } else {
115 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
116 physicalInterface.c_str(),
117 permission)) {
118 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
119 vpnNetId);
120 return ret;
121 }
122 }
123 return 0;
124}
125
126int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
127 Permission permission) {
128 return modifyFallthrough(physicalInterface, permission, true);
129}
130
131int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
132 Permission permission) {
133 return modifyFallthrough(physicalInterface, permission, false);
134}
135
136int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
137 Permission permission, bool add) {
138 for (const auto& entry : mNetworkController->mNetworks) {
139 if (entry.second->getType() == Network::VIRTUAL) {
140 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
141 return ret;
142 }
143 }
144 }
145 return 0;
146}
147
148NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900149 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
150 mProtectableUsers({AID_VPN}) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700151 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900152 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500153}
154
155unsigned NetworkController::getDefaultNetwork() const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800156 ScopedRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500157 return mDefaultNetId;
158}
159
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700160int NetworkController::setDefaultNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800161 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700162
163 if (netId == mDefaultNetId) {
164 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700165 }
166
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700167 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700168 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900169 if (!network) {
170 ALOGE("no such netId %u", netId);
171 return -ENONET;
172 }
173 if (network->getType() != Network::PHYSICAL) {
174 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700175 return -EINVAL;
176 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700177 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700178 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700179 }
180 }
181
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700182 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700183 Network* network = getNetworkLocked(mDefaultNetId);
184 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700185 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
186 return -ESRCH;
187 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700188 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700189 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700190 }
191 }
192
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700193 mDefaultNetId = netId;
194 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500195}
196
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900197uint32_t NetworkController::getNetworkForDnsLocked(unsigned* netId, uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700198 Fwmark fwmark;
199 fwmark.protectedFromVpn = true;
200 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900201
202 // Common case: there is no VPN that applies to the user, and the query did not specify a netId.
203 // Therefore, it is safe to set the explicit bit on this query and skip all the complex logic
204 // below. While this looks like a special case, it is actually the one that handles the vast
205 // majority of DNS queries.
206 // TODO: untangle this code.
207 if (*netId == NETID_UNSET && getVirtualNetworkForUserLocked(uid) == nullptr) {
208 *netId = mDefaultNetId;
209 fwmark.netId = *netId;
210 fwmark.explicitlySelected = true;
211 return fwmark.intValue;
212 }
213
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900214 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700215 // If a non-zero NetId was explicitly specified, and the user has permission for that
216 // network, use that network's DNS servers. Do not fall through to the default network even
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900217 // if the explicitly selected network is a split tunnel VPN: the explicitlySelected bit
218 // ensures that the VPN fallthrough rule does not match.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700219 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900220
221 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
222 // servers (through the default network). Otherwise, the query is guaranteed to fail.
223 // http://b/29498052
224 Network *network = getNetworkLocked(*netId);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900225 if (network && network->getType() == Network::VIRTUAL &&
226 !RESOLV_STUB.resolv_has_nameservers(*netId)) {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900227 *netId = mDefaultNetId;
228 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700229 } else {
230 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
231 // (possibly falling through to the default network if the VPN doesn't provide a route to
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900232 // them). Otherwise, use the default network's DNS servers. We cannot set the explicit bit
233 // because we need to be able to fall through a split tunnel to the default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700234 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900235 if (virtualNetwork && RESOLV_STUB.resolv_has_nameservers(virtualNetwork->getNetId())) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700236 *netId = virtualNetwork->getNetId();
237 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900238 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
239 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700240 *netId = mDefaultNetId;
241 }
242 }
243 fwmark.netId = *netId;
244 return fwmark.intValue;
245}
246
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900247uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800248 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900249 return getNetworkForDnsLocked(netId, uid);
250}
251
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700252// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
253// the VPN that applies to the UID if any; otherwise, the default network.
254unsigned NetworkController::getNetworkForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800255 ScopedRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700256 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700257 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500258 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700259 return mDefaultNetId;
260}
261
262// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
263// applies to the user if any; otherwise, the default network.
264//
265// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
266// is a split-tunnel and disappears later, the socket continues working (since the default network's
267// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
268// high-priority routing rule that doesn't care what NetId the socket has.
269//
270// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
271// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
272// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
273// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
274// the fallthrough rules also go away), the socket that used to fallthrough to the default network
275// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900276unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700277 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
278 if (virtualNetwork && !virtualNetwork->isSecure()) {
279 return virtualNetwork->getNetId();
280 }
281 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500282}
283
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900284unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800285 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900286 return getNetworkForConnectLocked(uid);
287}
288
Erik Klinecea2d342015-06-25 18:24:46 +0900289void NetworkController::getNetworkContext(
290 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800291 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900292
Erik Klinecea2d342015-06-25 18:24:46 +0900293 struct android_net_context nc = {
294 .app_netid = netId,
295 .app_mark = MARK_UNSET,
296 .dns_netid = netId,
297 .dns_mark = MARK_UNSET,
298 .uid = uid,
299 };
300
Erik Kline492ca5b2016-03-09 14:56:00 +0900301 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
302 // client process. This value is nonzero iff.:
303 //
304 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
305 // - [Java] android.net.Network#getAllByName()
306 // - [C/++] android_getaddrinfofornetwork()
307 // 2. The app specified a netid/nethandle to be used as a process default via:
308 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
309 // - [C/++] android_setprocnetwork()
310 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
311 //
312 // In all these cases (with the possible exception of #3), the right thing to do is to treat
313 // such cases as explicitlySelected.
314 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
315 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900316 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900317 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900318
Erik Klinecea2d342015-06-25 18:24:46 +0900319 Fwmark fwmark;
320 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900321 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900322 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
323 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900324 nc.app_mark = fwmark.intValue;
325
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900326 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900327
Erik Kline6d4669f2017-05-25 17:03:31 +0900328 if (DBG) {
329 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
330 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
331 }
332
Erik Klinecea2d342015-06-25 18:24:46 +0900333 if (netcontext) {
334 *netcontext = nc;
335 }
336}
337
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900338unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700339 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700340 if (entry.second->hasInterface(interface)) {
341 return entry.first;
342 }
343 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400344 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500345}
346
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900347unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800348 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900349 return getNetworkForInterfaceLocked(interface);
350}
351
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700352bool NetworkController::isVirtualNetwork(unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800353 ScopedRLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100354 return isVirtualNetworkLocked(netId);
355}
356
357bool NetworkController::isVirtualNetworkLocked(unsigned netId) const {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700358 Network* network = getNetworkLocked(netId);
359 return network && network->getType() == Network::VIRTUAL;
360}
361
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700362int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700363 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
364 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400365 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900366 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700367 }
368
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700369 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700370 ALOGE("duplicate netId %u", netId);
371 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700372 }
373
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700374 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700375 if (int ret = physicalNetwork->setPermission(permission)) {
376 ALOGE("inconceivable! setPermission cannot fail on an empty network");
377 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900378 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700379 }
380
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700381 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900382
383 updateTcpSocketMonitorPolling();
384
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900385 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700386}
387
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700388int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800389 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700390 return createPhysicalNetworkLocked(netId, permission);
391}
392
393int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700394 if (pNetId == nullptr) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700395 return -EINVAL;
396 }
397
Luke Huangd1ee4622018-06-29 13:49:58 +0800398 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700399 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
400 if (!isValidNetworkLocked(*pNetId)) {
401 break;
402 }
403 }
404
405 if (*pNetId > MAX_OEM_ID) {
406 ALOGE("No free network ID");
407 *pNetId = 0;
408 return -ENONET;
409 }
410
411 int ret = createPhysicalNetworkLocked(*pNetId, permission);
412 if (ret) {
413 *pNetId = 0;
414 }
415
416 return ret;
417}
418
cken67cd14c2018-12-05 17:26:59 +0900419int NetworkController::createVirtualNetwork(unsigned netId, bool secure) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800420 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900421
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700422 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700423 ALOGE("invalid netId %u", netId);
424 return -EINVAL;
425 }
426
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900427 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700428 ALOGE("duplicate netId %u", netId);
429 return -EEXIST;
430 }
431
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700432 if (int ret = modifyFallthroughLocked(netId, true)) {
433 return ret;
434 }
cken67cd14c2018-12-05 17:26:59 +0900435 mNetworks[netId] = new VirtualNetwork(netId, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700436 return 0;
437}
438
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700439int NetworkController::destroyNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800440 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900441
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900442 if (netId == LOCAL_NET_ID) {
443 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700444 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700445 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900446 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900447 ALOGE("no such netId %u", netId);
448 return -ENONET;
449 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700450
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700451 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700452
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700453 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900454
455 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
456 // other network code, ignore failures and attempt to clear out as much state as possible, even
457 // if we hit an error on the way. Return the first error that we see.
458 int ret = network->clearInterfaces();
459
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700460 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900461 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700462 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900463 if (!ret) {
464 ret = err;
465 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700466 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700467 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700468 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900469 if (int err = modifyFallthroughLocked(netId, false)) {
470 if (!ret) {
471 ret = err;
472 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700473 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700474 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700475 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700476 delete network;
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900477 RESOLV_STUB.resolv_delete_cache_for_net(netId);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900478
Rubin Xu6c00b612018-04-27 14:27:59 +0100479 for (auto iter = mIfindexToLastNetId.begin(); iter != mIfindexToLastNetId.end();) {
480 if (iter->second == netId) {
481 iter = mIfindexToLastNetId.erase(iter);
482 } else {
483 ++iter;
484 }
485 }
486
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900487 updateTcpSocketMonitorPolling();
488
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900489 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700490}
491
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700492int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800493 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900494
495 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900496 ALOGE("no such netId %u", netId);
497 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700498 }
499
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900500 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700501 if (existingNetId != NETID_UNSET && existingNetId != netId) {
502 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
503 return -EBUSY;
504 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100505 if (int ret = getNetworkLocked(netId)->addInterface(interface)) {
506 return ret;
507 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700508
Rubin Xu6c00b612018-04-27 14:27:59 +0100509 int ifIndex = RouteController::getIfIndex(interface);
510 if (ifIndex) {
511 mIfindexToLastNetId[ifIndex] = netId;
512 } else {
513 // Cannot happen, since addInterface() above will have failed.
514 ALOGE("inconceivable! added interface %s with no index", interface);
515 }
516 return 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700517}
518
519int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800520 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900521
522 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900523 ALOGE("no such netId %u", netId);
524 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700525 }
526
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700527 return getNetworkLocked(netId)->removeInterface(interface);
528}
529
530Permission NetworkController::getPermissionForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800531 ScopedRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700532 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700533}
534
535void NetworkController::setPermissionForUsers(Permission permission,
536 const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800537 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700538 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700539 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700540 }
541}
542
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900543int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800544 ScopedRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900545 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700546}
547
548int NetworkController::setPermissionForNetworks(Permission permission,
549 const std::vector<unsigned>& netIds) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800550 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700551 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700552 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900553 if (!network) {
554 ALOGE("no such netId %u", netId);
555 return -ENONET;
556 }
557 if (network->getType() != Network::PHYSICAL) {
558 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700559 return -EINVAL;
560 }
561
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700562 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700563 return ret;
564 }
565 }
566 return 0;
567}
568
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700569int NetworkController::addUsersToNetwork(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 add users to 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)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700581 return ret;
582 }
583 return 0;
584}
585
586int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800587 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700588 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900589 if (!network) {
590 ALOGE("no such netId %u", netId);
591 return -ENONET;
592 }
593 if (network->getType() != Network::VIRTUAL) {
594 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700595 return -EINVAL;
596 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900597 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
598 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700599 return ret;
600 }
601 return 0;
602}
603
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700604int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
605 const char* nexthop, bool legacy, uid_t uid) {
606 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
607}
608
609int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
610 const char* nexthop, bool legacy, uid_t uid) {
611 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
612}
613
Rubin Xu6c00b612018-04-27 14:27:59 +0100614void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800615 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100616 if (ifIndex == 0) {
617 ALOGE("Attempting to add address %s without ifindex", address);
618 return;
619 }
620 mAddressToIfindices[address].insert(ifIndex);
621}
622
623// Returns whether we should call SOCK_DESTROY on the removed address.
624bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800625 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100626 // First, update mAddressToIfindices map
627 auto ifindicesIter = mAddressToIfindices.find(address);
628 if (ifindicesIter == mAddressToIfindices.end()) {
629 ALOGE("Removing unknown address %s from ifindex %u", address, ifindex);
630 return true;
631 }
632 std::unordered_set<unsigned>& ifindices = ifindicesIter->second;
633 if (ifindices.erase(ifindex) > 0) {
634 if (ifindices.size() == 0) {
Bernie Innocentidd5a4f02018-07-19 18:06:52 +0900635 mAddressToIfindices.erase(ifindicesIter); // Invalidates ifindices
636 // The address is no longer configured on any interface.
637 return true;
Rubin Xu6c00b612018-04-27 14:27:59 +0100638 }
639 } else {
640 ALOGE("No record of address %s on interface %u", address, ifindex);
641 return true;
642 }
643 // Then, check for VPN handover condition
644 if (mIfindexToLastNetId.find(ifindex) == mIfindexToLastNetId.end()) {
645 ALOGE("Interface index %u was never in a currently-connected netId", ifindex);
646 return true;
647 }
648 unsigned lastNetId = mIfindexToLastNetId[ifindex];
649 for (unsigned idx : ifindices) {
650 unsigned activeNetId = mIfindexToLastNetId[idx];
651 // If this IP address is still assigned to another interface in the same network,
652 // then we don't need to destroy sockets on it because they are likely still valid.
653 // For now we do this only on VPNs.
654 // TODO: evaluate extending this to all network types.
655 if (lastNetId == activeNetId && isVirtualNetworkLocked(activeNetId)) {
656 return false;
657 }
658 }
659 return true;
660}
661
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900662bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700663 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
664 mProtectableUsers.find(uid) != mProtectableUsers.end();
665}
666
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900667bool NetworkController::canProtect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800668 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900669 return canProtectLocked(uid);
670}
671
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700672void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800673 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700674 mProtectableUsers.insert(uids.begin(), uids.end());
675}
676
677void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800678 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700679 for (uid_t uid : uids) {
680 mProtectableUsers.erase(uid);
681 }
682}
683
Erik Kline2d3a1632016-03-15 16:33:48 +0900684void NetworkController::dump(DumpWriter& dw) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800685 ScopedRLock lock(mRWLock);
Erik Kline2d3a1632016-03-15 16:33:48 +0900686
687 dw.incIndent();
688 dw.println("NetworkController");
689
690 dw.incIndent();
691 dw.println("Default network: %u", mDefaultNetId);
692
693 dw.blankline();
694 dw.println("Networks:");
695 dw.incIndent();
696 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900697 Network* network = i.second;
Erik Klineb31fd692018-06-06 20:50:11 +0900698 dw.println(network->toString());
Lorenzo Colittid1029652016-09-26 17:17:40 +0900699 if (network->getType() == Network::PHYSICAL) {
700 dw.incIndent();
701 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
702 dw.println("Required permission: %s", permissionToName(permission));
703 dw.decIndent();
704 }
Pierre Imai3a272072016-04-19 16:17:07 +0900705 android::net::gCtls->resolverCtrl.dump(dw, i.first);
706 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900707 }
708 dw.decIndent();
709
Rubin Xu6c00b612018-04-27 14:27:59 +0100710 dw.blankline();
711 dw.println("Interface <-> last network map:");
712 dw.incIndent();
713 for (const auto& i : mIfindexToLastNetId) {
714 dw.println("Ifindex: %u NetId: %u", i.first, i.second);
715 }
716 dw.decIndent();
717
718 dw.blankline();
719 dw.println("Interface addresses:");
720 dw.incIndent();
721 for (const auto& i : mAddressToIfindices) {
722 dw.println("address: %s ifindices: [%s]", i.first.c_str(),
723 android::base::Join(i.second, ", ").c_str());
724 }
725 dw.decIndent();
726
Erik Kline2d3a1632016-03-15 16:33:48 +0900727 dw.decIndent();
728
729 dw.decIndent();
730}
731
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700732bool NetworkController::isValidNetworkLocked(unsigned netId) const {
733 return getNetworkLocked(netId);
734}
735
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700736Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700737 auto iter = mNetworks.find(netId);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700738 return iter == mNetworks.end() ? nullptr : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700739}
740
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700741VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
742 for (const auto& entry : mNetworks) {
743 if (entry.second->getType() == Network::VIRTUAL) {
744 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
745 if (virtualNetwork->appliesToUser(uid)) {
746 return virtualNetwork;
747 }
748 }
749 }
Yi Kongbdfd57e2018-07-25 13:26:10 -0700750 return nullptr;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700751}
752
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700753Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
754 auto iter = mUsers.find(uid);
755 if (iter != mUsers.end()) {
756 return iter->second;
757 }
758 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
759}
760
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900761int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700762 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900763 if (!network) {
764 return -ENONET;
765 }
766
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700767 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
768 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900769 if (uid == INVALID_UID) {
770 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700771 }
772 Permission userPermission = getPermissionForUserLocked(uid);
773 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900774 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700775 }
776 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900777 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700778 }
779 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
780 if (virtualNetwork && virtualNetwork->isSecure() &&
781 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900782 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700783 }
784 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900785 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700786}
787
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700788int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
789 const char* nexthop, bool add, bool legacy, uid_t uid) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800790 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900791
792 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900793 ALOGE("no such netId %u", netId);
794 return -ENONET;
795 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900796 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900797 if (existingNetId == NETID_UNSET) {
798 ALOGE("interface %s not assigned to any netId", interface);
799 return -ENODEV;
800 }
801 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700802 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900803 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700804 }
805
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700806 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700807 if (netId == LOCAL_NET_ID) {
808 tableType = RouteController::LOCAL_NETWORK;
809 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900810 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700811 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700812 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700813 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700814 }
815 } else {
816 tableType = RouteController::INTERFACE;
817 }
818
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700819 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
820 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700821}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700822
823int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
824 if (mDefaultNetId == NETID_UNSET) {
825 return 0;
826 }
827 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900828 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700829 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
830 return -ESRCH;
831 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900832 if (network->getType() != Network::PHYSICAL) {
833 ALOGE("inconceivable! default network must be a physical network");
834 return -EINVAL;
835 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700836 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
837 for (const auto& physicalInterface : network->getInterfaces()) {
838 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
839 add)) {
840 return ret;
841 }
842 }
843 return 0;
844}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900845
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900846void NetworkController::updateTcpSocketMonitorPolling() {
847 bool physicalNetworkExists = false;
848 for (const auto& entry : mNetworks) {
849 const auto& network = entry.second;
850 if (network->getType() == Network::PHYSICAL && network->getNetId() >= MIN_NET_ID) {
851 physicalNetworkExists = true;
852 break;
853 }
854 }
855
856 if (physicalNetworkExists) {
857 android::net::gCtls->tcpSocketMonitor.resumePolling();
858 } else {
859 android::net::gCtls->tcpSocketMonitor.suspendPolling();
860 }
861}
862
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900863} // namespace net
864} // namespace android