blob: 50e1275c80e2f16ec58169d861c8c439dc2b732c [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 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 Huangc3252cc2018-10-16 15:43:23 +080034#include "android/net/INetd.h"
Rubin Xu6c00b612018-04-27 14:27:59 +010035
Erik Kline2d3a1632016-03-15 16:33:48 +090036#include "cutils/misc.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090037
Pierre Imai3a272072016-04-19 16:17:07 +090038#include "Controllers.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090039#include "DummyNetwork.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090040#include "DumpWriter.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070041#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070042#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070043#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070044#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070045#include "VirtualNetwork.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090046#include "netid_client.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070047
Erik Kline6d4669f2017-05-25 17:03:31 +090048#define DBG 0
49
Lorenzo Colitti7035f222017-02-13 18:29:00 +090050namespace android {
51namespace net {
52
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070053namespace {
54
55// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070056const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070057const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070058
59} // namespace
60
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070061const unsigned NetworkController::MIN_OEM_ID = 1;
62const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090063const unsigned NetworkController::DUMMY_NET_ID = 51;
64// NetIds 52..98 are reserved for future use.
Luke Huangc3252cc2018-10-16 15:43:23 +080065const unsigned NetworkController::LOCAL_NET_ID = INetd::LOCAL_NET_ID;
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070066
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070067// All calls to methods here are made while holding a write lock on mRWLock.
Lorenzo Colitti2d014e72018-01-10 22:12:29 +090068// They are mostly not called directly from this class, but from methods in PhysicalNetwork.cpp.
69// However, we're the only user of that class, so all calls to those methods come from here and are
70// made under lock.
71// For example, PhysicalNetwork::setPermission ends up calling addFallthrough and removeFallthrough,
72// but it's only called from here under lock (specifically, from createPhysicalNetworkLocked and
73// setPermissionForNetworks).
74// TODO: use std::mutex and GUARDED_BY instead of manual inspection.
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070075class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
76public:
77 explicit DelegateImpl(NetworkController* networkController);
78 virtual ~DelegateImpl();
79
80 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
81 Permission permission, bool add) WARN_UNUSED_RESULT;
82
83private:
84 int addFallthrough(const std::string& physicalInterface,
85 Permission permission) override WARN_UNUSED_RESULT;
86 int removeFallthrough(const std::string& physicalInterface,
87 Permission permission) override WARN_UNUSED_RESULT;
88
89 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
90 bool add) WARN_UNUSED_RESULT;
91
92 NetworkController* const mNetworkController;
93};
94
95NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
96 mNetworkController(networkController) {
97}
98
99NetworkController::DelegateImpl::~DelegateImpl() {
100}
101
102int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
103 const std::string& physicalInterface,
104 Permission permission, bool add) {
105 if (add) {
106 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
107 physicalInterface.c_str(),
108 permission)) {
109 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
110 vpnNetId);
111 return ret;
112 }
113 } else {
114 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
115 physicalInterface.c_str(),
116 permission)) {
117 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
118 vpnNetId);
119 return ret;
120 }
121 }
122 return 0;
123}
124
125int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
126 Permission permission) {
127 return modifyFallthrough(physicalInterface, permission, true);
128}
129
130int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
131 Permission permission) {
132 return modifyFallthrough(physicalInterface, permission, false);
133}
134
135int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
136 Permission permission, bool add) {
137 for (const auto& entry : mNetworkController->mNetworks) {
138 if (entry.second->getType() == Network::VIRTUAL) {
139 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
140 return ret;
141 }
142 }
143 }
144 return 0;
145}
146
147NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900148 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
149 mProtectableUsers({AID_VPN}) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700150 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900151 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500152}
153
154unsigned NetworkController::getDefaultNetwork() const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800155 ScopedRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500156 return mDefaultNetId;
157}
158
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700159int NetworkController::setDefaultNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800160 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700161
162 if (netId == mDefaultNetId) {
163 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700164 }
165
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700166 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700167 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900168 if (!network) {
169 ALOGE("no such netId %u", netId);
170 return -ENONET;
171 }
172 if (network->getType() != Network::PHYSICAL) {
173 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700174 return -EINVAL;
175 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700176 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700177 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700178 }
179 }
180
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700181 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700182 Network* network = getNetworkLocked(mDefaultNetId);
183 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700184 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
185 return -ESRCH;
186 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700187 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700188 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700189 }
190 }
191
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700192 mDefaultNetId = netId;
193 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500194}
195
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900196uint32_t NetworkController::getNetworkForDnsLocked(unsigned* netId, uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700197 Fwmark fwmark;
198 fwmark.protectedFromVpn = true;
199 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900200
201 // Common case: there is no VPN that applies to the user, and the query did not specify a netId.
202 // Therefore, it is safe to set the explicit bit on this query and skip all the complex logic
203 // below. While this looks like a special case, it is actually the one that handles the vast
204 // majority of DNS queries.
205 // TODO: untangle this code.
206 if (*netId == NETID_UNSET && getVirtualNetworkForUserLocked(uid) == nullptr) {
207 *netId = mDefaultNetId;
208 fwmark.netId = *netId;
209 fwmark.explicitlySelected = true;
210 return fwmark.intValue;
211 }
212
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900213 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700214 // If a non-zero NetId was explicitly specified, and the user has permission for that
215 // network, use that network's DNS servers. Do not fall through to the default network even
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900216 // if the explicitly selected network is a split tunnel VPN: the explicitlySelected bit
217 // ensures that the VPN fallthrough rule does not match.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700218 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900219
220 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
221 // servers (through the default network). Otherwise, the query is guaranteed to fail.
222 // http://b/29498052
223 Network *network = getNetworkLocked(*netId);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900224 if (network && network->getType() == Network::VIRTUAL &&
225 !RESOLV_STUB.resolv_has_nameservers(*netId)) {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900226 *netId = mDefaultNetId;
227 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700228 } else {
229 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
230 // (possibly falling through to the default network if the VPN doesn't provide a route to
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900231 // them). Otherwise, use the default network's DNS servers. We cannot set the explicit bit
232 // because we need to be able to fall through a split tunnel to the default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700233 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900234 if (virtualNetwork && RESOLV_STUB.resolv_has_nameservers(virtualNetwork->getNetId())) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700235 *netId = virtualNetwork->getNetId();
236 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900237 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
238 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700239 *netId = mDefaultNetId;
240 }
241 }
242 fwmark.netId = *netId;
243 return fwmark.intValue;
244}
245
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900246uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800247 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900248 return getNetworkForDnsLocked(netId, uid);
249}
250
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700251// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
252// the VPN that applies to the UID if any; otherwise, the default network.
253unsigned NetworkController::getNetworkForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800254 ScopedRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700255 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700256 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500257 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700258 return mDefaultNetId;
259}
260
261// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
262// applies to the user if any; otherwise, the default network.
263//
264// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
265// is a split-tunnel and disappears later, the socket continues working (since the default network's
266// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
267// high-priority routing rule that doesn't care what NetId the socket has.
268//
269// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
270// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
271// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
272// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
273// the fallthrough rules also go away), the socket that used to fallthrough to the default network
274// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900275unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700276 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
277 if (virtualNetwork && !virtualNetwork->isSecure()) {
278 return virtualNetwork->getNetId();
279 }
280 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500281}
282
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900283unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800284 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900285 return getNetworkForConnectLocked(uid);
286}
287
Erik Klinecea2d342015-06-25 18:24:46 +0900288void NetworkController::getNetworkContext(
289 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800290 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900291
Erik Klinecea2d342015-06-25 18:24:46 +0900292 struct android_net_context nc = {
293 .app_netid = netId,
294 .app_mark = MARK_UNSET,
295 .dns_netid = netId,
296 .dns_mark = MARK_UNSET,
297 .uid = uid,
298 };
299
Erik Kline492ca5b2016-03-09 14:56:00 +0900300 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
301 // client process. This value is nonzero iff.:
302 //
303 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
304 // - [Java] android.net.Network#getAllByName()
305 // - [C/++] android_getaddrinfofornetwork()
306 // 2. The app specified a netid/nethandle to be used as a process default via:
307 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
308 // - [C/++] android_setprocnetwork()
309 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
310 //
311 // In all these cases (with the possible exception of #3), the right thing to do is to treat
312 // such cases as explicitlySelected.
313 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
314 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900315 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900316 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900317
Erik Klinecea2d342015-06-25 18:24:46 +0900318 Fwmark fwmark;
319 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900320 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900321 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
322 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900323 nc.app_mark = fwmark.intValue;
324
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900325 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900326
Erik Kline6d4669f2017-05-25 17:03:31 +0900327 if (DBG) {
328 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
329 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
330 }
331
Erik Klinecea2d342015-06-25 18:24:46 +0900332 if (netcontext) {
333 *netcontext = nc;
334 }
335}
336
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900337unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700338 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700339 if (entry.second->hasInterface(interface)) {
340 return entry.first;
341 }
342 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400343 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500344}
345
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900346unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800347 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900348 return getNetworkForInterfaceLocked(interface);
349}
350
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700351bool NetworkController::isVirtualNetwork(unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800352 ScopedRLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100353 return isVirtualNetworkLocked(netId);
354}
355
356bool NetworkController::isVirtualNetworkLocked(unsigned netId) const {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700357 Network* network = getNetworkLocked(netId);
358 return network && network->getType() == Network::VIRTUAL;
359}
360
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700361int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700362 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
363 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400364 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900365 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700366 }
367
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700368 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700369 ALOGE("duplicate netId %u", netId);
370 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700371 }
372
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700373 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700374 if (int ret = physicalNetwork->setPermission(permission)) {
375 ALOGE("inconceivable! setPermission cannot fail on an empty network");
376 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900377 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700378 }
379
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700380 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900381
382 updateTcpSocketMonitorPolling();
383
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900384 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700385}
386
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700387int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800388 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700389 return createPhysicalNetworkLocked(netId, permission);
390}
391
392int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700393 if (pNetId == nullptr) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700394 return -EINVAL;
395 }
396
Luke Huangd1ee4622018-06-29 13:49:58 +0800397 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700398 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
399 if (!isValidNetworkLocked(*pNetId)) {
400 break;
401 }
402 }
403
404 if (*pNetId > MAX_OEM_ID) {
405 ALOGE("No free network ID");
406 *pNetId = 0;
407 return -ENONET;
408 }
409
410 int ret = createPhysicalNetworkLocked(*pNetId, permission);
411 if (ret) {
412 *pNetId = 0;
413 }
414
415 return ret;
416}
417
cken67cd14c2018-12-05 17:26:59 +0900418int NetworkController::createVirtualNetwork(unsigned netId, bool secure) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800419 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900420
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700421 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700422 ALOGE("invalid netId %u", netId);
423 return -EINVAL;
424 }
425
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900426 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700427 ALOGE("duplicate netId %u", netId);
428 return -EEXIST;
429 }
430
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700431 if (int ret = modifyFallthroughLocked(netId, true)) {
432 return ret;
433 }
cken67cd14c2018-12-05 17:26:59 +0900434 mNetworks[netId] = new VirtualNetwork(netId, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700435 return 0;
436}
437
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700438int NetworkController::destroyNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800439 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900440
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900441 if (netId == LOCAL_NET_ID) {
442 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700443 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700444 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900445 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900446 ALOGE("no such netId %u", netId);
447 return -ENONET;
448 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700449
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700450 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700451
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700452 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900453
454 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
455 // other network code, ignore failures and attempt to clear out as much state as possible, even
456 // if we hit an error on the way. Return the first error that we see.
457 int ret = network->clearInterfaces();
458
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700459 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900460 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700461 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900462 if (!ret) {
463 ret = err;
464 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700465 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700466 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700467 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900468 if (int err = modifyFallthroughLocked(netId, false)) {
469 if (!ret) {
470 ret = err;
471 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700472 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700473 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700474 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700475 delete network;
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +0900476 RESOLV_STUB.resolv_delete_cache_for_net(netId);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900477
Rubin Xu6c00b612018-04-27 14:27:59 +0100478 for (auto iter = mIfindexToLastNetId.begin(); iter != mIfindexToLastNetId.end();) {
479 if (iter->second == netId) {
480 iter = mIfindexToLastNetId.erase(iter);
481 } else {
482 ++iter;
483 }
484 }
485
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900486 updateTcpSocketMonitorPolling();
487
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900488 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700489}
490
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700491int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800492 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900493
494 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900495 ALOGE("no such netId %u", netId);
496 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700497 }
498
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900499 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700500 if (existingNetId != NETID_UNSET && existingNetId != netId) {
501 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
502 return -EBUSY;
503 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100504 if (int ret = getNetworkLocked(netId)->addInterface(interface)) {
505 return ret;
506 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700507
Rubin Xu6c00b612018-04-27 14:27:59 +0100508 int ifIndex = RouteController::getIfIndex(interface);
509 if (ifIndex) {
510 mIfindexToLastNetId[ifIndex] = netId;
511 } else {
512 // Cannot happen, since addInterface() above will have failed.
513 ALOGE("inconceivable! added interface %s with no index", interface);
514 }
515 return 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700516}
517
518int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800519 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900520
521 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900522 ALOGE("no such netId %u", netId);
523 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700524 }
525
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700526 return getNetworkLocked(netId)->removeInterface(interface);
527}
528
529Permission NetworkController::getPermissionForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800530 ScopedRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700531 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700532}
533
534void NetworkController::setPermissionForUsers(Permission permission,
535 const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800536 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700537 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700538 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700539 }
540}
541
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900542int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800543 ScopedRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900544 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700545}
546
547int NetworkController::setPermissionForNetworks(Permission permission,
548 const std::vector<unsigned>& netIds) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800549 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700550 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700551 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900552 if (!network) {
553 ALOGE("no such netId %u", netId);
554 return -ENONET;
555 }
556 if (network->getType() != Network::PHYSICAL) {
557 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700558 return -EINVAL;
559 }
560
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700561 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700562 return ret;
563 }
564 }
565 return 0;
566}
567
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700568int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800569 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700570 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900571 if (!network) {
572 ALOGE("no such netId %u", netId);
573 return -ENONET;
574 }
575 if (network->getType() != Network::VIRTUAL) {
576 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700577 return -EINVAL;
578 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900579 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700580 return ret;
581 }
582 return 0;
583}
584
585int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800586 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700587 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900588 if (!network) {
589 ALOGE("no such netId %u", netId);
590 return -ENONET;
591 }
592 if (network->getType() != Network::VIRTUAL) {
593 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700594 return -EINVAL;
595 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900596 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
597 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700598 return ret;
599 }
600 return 0;
601}
602
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700603int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
604 const char* nexthop, bool legacy, uid_t uid) {
605 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
606}
607
608int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
609 const char* nexthop, bool legacy, uid_t uid) {
610 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
611}
612
Rubin Xu6c00b612018-04-27 14:27:59 +0100613void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800614 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100615 if (ifIndex == 0) {
616 ALOGE("Attempting to add address %s without ifindex", address);
617 return;
618 }
619 mAddressToIfindices[address].insert(ifIndex);
620}
621
622// Returns whether we should call SOCK_DESTROY on the removed address.
623bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800624 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100625 // First, update mAddressToIfindices map
626 auto ifindicesIter = mAddressToIfindices.find(address);
627 if (ifindicesIter == mAddressToIfindices.end()) {
628 ALOGE("Removing unknown address %s from ifindex %u", address, ifindex);
629 return true;
630 }
631 std::unordered_set<unsigned>& ifindices = ifindicesIter->second;
632 if (ifindices.erase(ifindex) > 0) {
633 if (ifindices.size() == 0) {
Bernie Innocentidd5a4f02018-07-19 18:06:52 +0900634 mAddressToIfindices.erase(ifindicesIter); // Invalidates ifindices
635 // The address is no longer configured on any interface.
636 return true;
Rubin Xu6c00b612018-04-27 14:27:59 +0100637 }
638 } else {
639 ALOGE("No record of address %s on interface %u", address, ifindex);
640 return true;
641 }
642 // Then, check for VPN handover condition
643 if (mIfindexToLastNetId.find(ifindex) == mIfindexToLastNetId.end()) {
644 ALOGE("Interface index %u was never in a currently-connected netId", ifindex);
645 return true;
646 }
647 unsigned lastNetId = mIfindexToLastNetId[ifindex];
648 for (unsigned idx : ifindices) {
649 unsigned activeNetId = mIfindexToLastNetId[idx];
650 // If this IP address is still assigned to another interface in the same network,
651 // then we don't need to destroy sockets on it because they are likely still valid.
652 // For now we do this only on VPNs.
653 // TODO: evaluate extending this to all network types.
654 if (lastNetId == activeNetId && isVirtualNetworkLocked(activeNetId)) {
655 return false;
656 }
657 }
658 return true;
659}
660
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900661bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700662 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
663 mProtectableUsers.find(uid) != mProtectableUsers.end();
664}
665
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900666bool NetworkController::canProtect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800667 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900668 return canProtectLocked(uid);
669}
670
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700671void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800672 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700673 mProtectableUsers.insert(uids.begin(), uids.end());
674}
675
676void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800677 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700678 for (uid_t uid : uids) {
679 mProtectableUsers.erase(uid);
680 }
681}
682
Erik Kline2d3a1632016-03-15 16:33:48 +0900683void NetworkController::dump(DumpWriter& dw) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800684 ScopedRLock lock(mRWLock);
Erik Kline2d3a1632016-03-15 16:33:48 +0900685
686 dw.incIndent();
687 dw.println("NetworkController");
688
689 dw.incIndent();
690 dw.println("Default network: %u", mDefaultNetId);
691
692 dw.blankline();
693 dw.println("Networks:");
694 dw.incIndent();
695 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900696 Network* network = i.second;
Erik Klineb31fd692018-06-06 20:50:11 +0900697 dw.println(network->toString());
Lorenzo Colittid1029652016-09-26 17:17:40 +0900698 if (network->getType() == Network::PHYSICAL) {
699 dw.incIndent();
700 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
701 dw.println("Required permission: %s", permissionToName(permission));
702 dw.decIndent();
703 }
Pierre Imai3a272072016-04-19 16:17:07 +0900704 android::net::gCtls->resolverCtrl.dump(dw, i.first);
705 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900706 }
707 dw.decIndent();
708
Rubin Xu6c00b612018-04-27 14:27:59 +0100709 dw.blankline();
710 dw.println("Interface <-> last network map:");
711 dw.incIndent();
712 for (const auto& i : mIfindexToLastNetId) {
713 dw.println("Ifindex: %u NetId: %u", i.first, i.second);
714 }
715 dw.decIndent();
716
717 dw.blankline();
718 dw.println("Interface addresses:");
719 dw.incIndent();
720 for (const auto& i : mAddressToIfindices) {
721 dw.println("address: %s ifindices: [%s]", i.first.c_str(),
722 android::base::Join(i.second, ", ").c_str());
723 }
724 dw.decIndent();
725
Erik Kline2d3a1632016-03-15 16:33:48 +0900726 dw.decIndent();
727
728 dw.decIndent();
729}
730
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700731bool NetworkController::isValidNetworkLocked(unsigned netId) const {
732 return getNetworkLocked(netId);
733}
734
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700735Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700736 auto iter = mNetworks.find(netId);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700737 return iter == mNetworks.end() ? nullptr : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700738}
739
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700740VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
741 for (const auto& entry : mNetworks) {
742 if (entry.second->getType() == Network::VIRTUAL) {
743 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
744 if (virtualNetwork->appliesToUser(uid)) {
745 return virtualNetwork;
746 }
747 }
748 }
Yi Kongbdfd57e2018-07-25 13:26:10 -0700749 return nullptr;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700750}
751
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700752Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
753 auto iter = mUsers.find(uid);
754 if (iter != mUsers.end()) {
755 return iter->second;
756 }
757 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
758}
759
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900760int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700761 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900762 if (!network) {
763 return -ENONET;
764 }
765
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700766 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
767 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900768 if (uid == INVALID_UID) {
769 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700770 }
771 Permission userPermission = getPermissionForUserLocked(uid);
772 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900773 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700774 }
775 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900776 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700777 }
778 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
779 if (virtualNetwork && virtualNetwork->isSecure() &&
780 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900781 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700782 }
783 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900784 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700785}
786
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700787int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
788 const char* nexthop, bool add, bool legacy, uid_t uid) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800789 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900790
791 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900792 ALOGE("no such netId %u", netId);
793 return -ENONET;
794 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900795 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900796 if (existingNetId == NETID_UNSET) {
797 ALOGE("interface %s not assigned to any netId", interface);
798 return -ENODEV;
799 }
800 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700801 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900802 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700803 }
804
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700805 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700806 if (netId == LOCAL_NET_ID) {
807 tableType = RouteController::LOCAL_NETWORK;
808 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900809 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700810 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700811 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700812 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700813 }
814 } else {
815 tableType = RouteController::INTERFACE;
816 }
817
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700818 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
819 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700820}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700821
822int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
823 if (mDefaultNetId == NETID_UNSET) {
824 return 0;
825 }
826 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900827 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700828 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
829 return -ESRCH;
830 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900831 if (network->getType() != Network::PHYSICAL) {
832 ALOGE("inconceivable! default network must be a physical network");
833 return -EINVAL;
834 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700835 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
836 for (const auto& physicalInterface : network->getInterfaces()) {
837 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
838 add)) {
839 return ret;
840 }
841 }
842 return 0;
843}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900844
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900845void NetworkController::updateTcpSocketMonitorPolling() {
846 bool physicalNetworkExists = false;
847 for (const auto& entry : mNetworks) {
848 const auto& network = entry.second;
849 if (network->getType() == Network::PHYSICAL && network->getNetId() >= MIN_NET_ID) {
850 physicalNetworkExists = true;
851 break;
852 }
853 }
854
855 if (physicalNetworkExists) {
856 android::net::gCtls->tcpSocketMonitor.resumePolling();
857 } else {
858 android::net::gCtls->tcpSocketMonitor.suspendPolling();
859 }
860}
861
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900862} // namespace net
863} // namespace android