blob: f36ed772a018fddcc9215b110be4a001f8d2fb6d [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
31#include "cutils/misc.h"
32#include "resolv_netid.h"
33
Pierre Imai3a272072016-04-19 16:17:07 +090034#include "Controllers.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090035#include "DummyNetwork.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090036#include "DumpWriter.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"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070042
Erik Kline6d4669f2017-05-25 17:03:31 +090043#define DBG 0
44
Lorenzo Colitti7035f222017-02-13 18:29:00 +090045namespace android {
46namespace net {
47
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070048namespace {
49
50// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070051const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070052const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070053
54} // namespace
55
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070056const unsigned NetworkController::MIN_OEM_ID = 1;
57const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090058const unsigned NetworkController::DUMMY_NET_ID = 51;
59// NetIds 52..98 are reserved for future use.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070060const unsigned NetworkController::LOCAL_NET_ID = 99;
61
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070062// All calls to methods here are made while holding a write lock on mRWLock.
63class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
64public:
65 explicit DelegateImpl(NetworkController* networkController);
66 virtual ~DelegateImpl();
67
68 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
69 Permission permission, bool add) WARN_UNUSED_RESULT;
70
71private:
72 int addFallthrough(const std::string& physicalInterface,
73 Permission permission) override WARN_UNUSED_RESULT;
74 int removeFallthrough(const std::string& physicalInterface,
75 Permission permission) override WARN_UNUSED_RESULT;
76
77 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
78 bool add) WARN_UNUSED_RESULT;
79
80 NetworkController* const mNetworkController;
81};
82
83NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
84 mNetworkController(networkController) {
85}
86
87NetworkController::DelegateImpl::~DelegateImpl() {
88}
89
90int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
91 const std::string& physicalInterface,
92 Permission permission, bool add) {
93 if (add) {
94 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
95 physicalInterface.c_str(),
96 permission)) {
97 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
98 vpnNetId);
99 return ret;
100 }
101 } else {
102 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
103 physicalInterface.c_str(),
104 permission)) {
105 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
106 vpnNetId);
107 return ret;
108 }
109 }
110 return 0;
111}
112
113int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
114 Permission permission) {
115 return modifyFallthrough(physicalInterface, permission, true);
116}
117
118int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
119 Permission permission) {
120 return modifyFallthrough(physicalInterface, permission, false);
121}
122
123int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
124 Permission permission, bool add) {
125 for (const auto& entry : mNetworkController->mNetworks) {
126 if (entry.second->getType() == Network::VIRTUAL) {
127 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
128 return ret;
129 }
130 }
131 }
132 return 0;
133}
134
135NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900136 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
137 mProtectableUsers({AID_VPN}) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700138 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900139 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500140}
141
142unsigned NetworkController::getDefaultNetwork() const {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700143 android::RWLock::AutoRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500144 return mDefaultNetId;
145}
146
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700147int NetworkController::setDefaultNetwork(unsigned netId) {
148 android::RWLock::AutoWLock lock(mRWLock);
149
150 if (netId == mDefaultNetId) {
151 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700152 }
153
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700154 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700155 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900156 if (!network) {
157 ALOGE("no such netId %u", netId);
158 return -ENONET;
159 }
160 if (network->getType() != Network::PHYSICAL) {
161 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700162 return -EINVAL;
163 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700164 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700165 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700166 }
167 }
168
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700169 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700170 Network* network = getNetworkLocked(mDefaultNetId);
171 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700172 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
173 return -ESRCH;
174 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700175 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700176 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700177 }
178 }
179
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700180 mDefaultNetId = netId;
181 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500182}
183
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900184uint32_t NetworkController::getNetworkForDnsLocked(unsigned* netId, uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700185 Fwmark fwmark;
186 fwmark.protectedFromVpn = true;
187 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900188 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700189 // If a non-zero NetId was explicitly specified, and the user has permission for that
190 // network, use that network's DNS servers. Do not fall through to the default network even
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900191 // if the explicitly selected network is a split tunnel VPN: the explicitlySelected bit
192 // ensures that the VPN fallthrough rule does not match.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700193 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900194
195 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
196 // servers (through the default network). Otherwise, the query is guaranteed to fail.
197 // http://b/29498052
198 Network *network = getNetworkLocked(*netId);
199 if (network && network->getType() == Network::VIRTUAL &&
200 !static_cast<VirtualNetwork *>(network)->getHasDns()) {
201 *netId = mDefaultNetId;
202 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700203 } else {
204 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
205 // (possibly falling through to the default network if the VPN doesn't provide a route to
206 // them). Otherwise, use the default network's DNS servers.
207 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
208 if (virtualNetwork && virtualNetwork->getHasDns()) {
209 *netId = virtualNetwork->getNetId();
210 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900211 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
212 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700213 *netId = mDefaultNetId;
214 }
215 }
216 fwmark.netId = *netId;
217 return fwmark.intValue;
218}
219
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900220uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
221 android::RWLock::AutoRLock lock(mRWLock);
222 return getNetworkForDnsLocked(netId, uid);
223}
224
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700225// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
226// the VPN that applies to the UID if any; otherwise, the default network.
227unsigned NetworkController::getNetworkForUser(uid_t uid) const {
228 android::RWLock::AutoRLock lock(mRWLock);
229 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700230 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500231 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700232 return mDefaultNetId;
233}
234
235// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
236// applies to the user if any; otherwise, the default network.
237//
238// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
239// is a split-tunnel and disappears later, the socket continues working (since the default network's
240// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
241// high-priority routing rule that doesn't care what NetId the socket has.
242//
243// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
244// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
245// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
246// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
247// the fallthrough rules also go away), the socket that used to fallthrough to the default network
248// will stop working.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900249unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700250 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
251 if (virtualNetwork && !virtualNetwork->isSecure()) {
252 return virtualNetwork->getNetId();
253 }
254 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500255}
256
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900257unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
258 android::RWLock::AutoRLock lock(mRWLock);
259 return getNetworkForConnectLocked(uid);
260}
261
Erik Klinecea2d342015-06-25 18:24:46 +0900262void NetworkController::getNetworkContext(
263 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900264 android::RWLock::AutoRLock lock(mRWLock);
265
Erik Klinecea2d342015-06-25 18:24:46 +0900266 struct android_net_context nc = {
267 .app_netid = netId,
268 .app_mark = MARK_UNSET,
269 .dns_netid = netId,
270 .dns_mark = MARK_UNSET,
271 .uid = uid,
272 };
273
Erik Kline492ca5b2016-03-09 14:56:00 +0900274 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
275 // client process. This value is nonzero iff.:
276 //
277 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
278 // - [Java] android.net.Network#getAllByName()
279 // - [C/++] android_getaddrinfofornetwork()
280 // 2. The app specified a netid/nethandle to be used as a process default via:
281 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
282 // - [C/++] android_setprocnetwork()
283 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
284 //
285 // In all these cases (with the possible exception of #3), the right thing to do is to treat
286 // such cases as explicitlySelected.
287 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
288 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900289 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900290 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900291
Erik Klinecea2d342015-06-25 18:24:46 +0900292 Fwmark fwmark;
293 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900294 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900295 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
296 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900297 nc.app_mark = fwmark.intValue;
298
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900299 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900300
Erik Kline6d4669f2017-05-25 17:03:31 +0900301 if (DBG) {
302 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
303 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
304 }
305
Erik Klinecea2d342015-06-25 18:24:46 +0900306 if (netcontext) {
307 *netcontext = nc;
308 }
309}
310
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900311unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700312 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700313 if (entry.second->hasInterface(interface)) {
314 return entry.first;
315 }
316 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400317 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500318}
319
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900320unsigned NetworkController::getNetworkForInterface(const char* interface) const {
321 android::RWLock::AutoRLock lock(mRWLock);
322 return getNetworkForInterfaceLocked(interface);
323}
324
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700325bool NetworkController::isVirtualNetwork(unsigned netId) const {
326 android::RWLock::AutoRLock lock(mRWLock);
327 Network* network = getNetworkLocked(netId);
328 return network && network->getType() == Network::VIRTUAL;
329}
330
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700331int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700332 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
333 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400334 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900335 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700336 }
337
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700338 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700339 ALOGE("duplicate netId %u", netId);
340 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700341 }
342
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700343 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700344 if (int ret = physicalNetwork->setPermission(permission)) {
345 ALOGE("inconceivable! setPermission cannot fail on an empty network");
346 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900347 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700348 }
349
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700350 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900351 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700352}
353
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700354int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
355 android::RWLock::AutoWLock lock(mRWLock);
356 return createPhysicalNetworkLocked(netId, permission);
357}
358
359int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
360 if (pNetId == NULL) {
361 return -EINVAL;
362 }
363
364 android::RWLock::AutoWLock lock(mRWLock);
365 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
366 if (!isValidNetworkLocked(*pNetId)) {
367 break;
368 }
369 }
370
371 if (*pNetId > MAX_OEM_ID) {
372 ALOGE("No free network ID");
373 *pNetId = 0;
374 return -ENONET;
375 }
376
377 int ret = createPhysicalNetworkLocked(*pNetId, permission);
378 if (ret) {
379 *pNetId = 0;
380 }
381
382 return ret;
383}
384
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700385int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900386 android::RWLock::AutoWLock lock(mRWLock);
387
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700388 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700389 ALOGE("invalid netId %u", netId);
390 return -EINVAL;
391 }
392
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900393 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700394 ALOGE("duplicate netId %u", netId);
395 return -EEXIST;
396 }
397
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700398 if (int ret = modifyFallthroughLocked(netId, true)) {
399 return ret;
400 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700401 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700402 return 0;
403}
404
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700405int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900406 android::RWLock::AutoWLock lock(mRWLock);
407
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900408 if (netId == LOCAL_NET_ID) {
409 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700410 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700411 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900412 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900413 ALOGE("no such netId %u", netId);
414 return -ENONET;
415 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700416
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700417 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700418
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700419 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900420
421 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
422 // other network code, ignore failures and attempt to clear out as much state as possible, even
423 // if we hit an error on the way. Return the first error that we see.
424 int ret = network->clearInterfaces();
425
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700426 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900427 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700428 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900429 if (!ret) {
430 ret = err;
431 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700432 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700433 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700434 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900435 if (int err = modifyFallthroughLocked(netId, false)) {
436 if (!ret) {
437 ret = err;
438 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700439 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700440 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700441 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700442 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700443 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900444 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700445}
446
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700447int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900448 android::RWLock::AutoWLock lock(mRWLock);
449
450 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900451 ALOGE("no such netId %u", netId);
452 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700453 }
454
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900455 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700456 if (existingNetId != NETID_UNSET && existingNetId != netId) {
457 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
458 return -EBUSY;
459 }
460
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700461 return getNetworkLocked(netId)->addInterface(interface);
462}
463
464int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900465 android::RWLock::AutoWLock lock(mRWLock);
466
467 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900468 ALOGE("no such netId %u", netId);
469 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700470 }
471
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700472 return getNetworkLocked(netId)->removeInterface(interface);
473}
474
475Permission NetworkController::getPermissionForUser(uid_t uid) const {
476 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700477 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700478}
479
480void NetworkController::setPermissionForUsers(Permission permission,
481 const std::vector<uid_t>& uids) {
482 android::RWLock::AutoWLock lock(mRWLock);
483 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700484 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700485 }
486}
487
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900488int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700489 android::RWLock::AutoRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900490 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700491}
492
493int NetworkController::setPermissionForNetworks(Permission permission,
494 const std::vector<unsigned>& netIds) {
495 android::RWLock::AutoWLock lock(mRWLock);
496 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700497 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900498 if (!network) {
499 ALOGE("no such netId %u", netId);
500 return -ENONET;
501 }
502 if (network->getType() != Network::PHYSICAL) {
503 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700504 return -EINVAL;
505 }
506
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700507 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700508 return ret;
509 }
510 }
511 return 0;
512}
513
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700514int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
515 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700516 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900517 if (!network) {
518 ALOGE("no such netId %u", netId);
519 return -ENONET;
520 }
521 if (network->getType() != Network::VIRTUAL) {
522 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700523 return -EINVAL;
524 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900525 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700526 return ret;
527 }
528 return 0;
529}
530
531int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
532 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700533 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900534 if (!network) {
535 ALOGE("no such netId %u", netId);
536 return -ENONET;
537 }
538 if (network->getType() != Network::VIRTUAL) {
539 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700540 return -EINVAL;
541 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900542 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
543 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700544 return ret;
545 }
546 return 0;
547}
548
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700549int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
550 const char* nexthop, bool legacy, uid_t uid) {
551 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
552}
553
554int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
555 const char* nexthop, bool legacy, uid_t uid) {
556 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
557}
558
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900559bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700560 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
561 mProtectableUsers.find(uid) != mProtectableUsers.end();
562}
563
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900564bool NetworkController::canProtect(uid_t uid) const {
565 android::RWLock::AutoRLock lock(mRWLock);
566 return canProtectLocked(uid);
567}
568
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700569void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
570 android::RWLock::AutoWLock lock(mRWLock);
571 mProtectableUsers.insert(uids.begin(), uids.end());
572}
573
574void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
575 android::RWLock::AutoWLock lock(mRWLock);
576 for (uid_t uid : uids) {
577 mProtectableUsers.erase(uid);
578 }
579}
580
Erik Kline2d3a1632016-03-15 16:33:48 +0900581void NetworkController::dump(DumpWriter& dw) {
582 android::RWLock::AutoRLock lock(mRWLock);
583
584 dw.incIndent();
585 dw.println("NetworkController");
586
587 dw.incIndent();
588 dw.println("Default network: %u", mDefaultNetId);
589
590 dw.blankline();
591 dw.println("Networks:");
592 dw.incIndent();
593 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900594 Network* network = i.second;
595 dw.println(network->toString().c_str());
596 if (network->getType() == Network::PHYSICAL) {
597 dw.incIndent();
598 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
599 dw.println("Required permission: %s", permissionToName(permission));
600 dw.decIndent();
601 }
Pierre Imai3a272072016-04-19 16:17:07 +0900602 android::net::gCtls->resolverCtrl.dump(dw, i.first);
603 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900604 }
605 dw.decIndent();
606
607 dw.decIndent();
608
609 dw.decIndent();
610}
611
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700612bool NetworkController::isValidNetworkLocked(unsigned netId) const {
613 return getNetworkLocked(netId);
614}
615
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700616Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700617 auto iter = mNetworks.find(netId);
618 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700619}
620
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700621VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
622 for (const auto& entry : mNetworks) {
623 if (entry.second->getType() == Network::VIRTUAL) {
624 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
625 if (virtualNetwork->appliesToUser(uid)) {
626 return virtualNetwork;
627 }
628 }
629 }
630 return NULL;
631}
632
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700633Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
634 auto iter = mUsers.find(uid);
635 if (iter != mUsers.end()) {
636 return iter->second;
637 }
638 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
639}
640
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900641int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700642 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900643 if (!network) {
644 return -ENONET;
645 }
646
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700647 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
648 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900649 if (uid == INVALID_UID) {
650 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700651 }
652 Permission userPermission = getPermissionForUserLocked(uid);
653 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900654 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700655 }
656 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900657 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700658 }
659 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
660 if (virtualNetwork && virtualNetwork->isSecure() &&
661 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900662 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700663 }
664 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900665 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700666}
667
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700668int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
669 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900670 android::RWLock::AutoRLock lock(mRWLock);
671
672 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900673 ALOGE("no such netId %u", netId);
674 return -ENONET;
675 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900676 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900677 if (existingNetId == NETID_UNSET) {
678 ALOGE("interface %s not assigned to any netId", interface);
679 return -ENODEV;
680 }
681 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700682 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900683 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700684 }
685
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700686 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700687 if (netId == LOCAL_NET_ID) {
688 tableType = RouteController::LOCAL_NETWORK;
689 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900690 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700691 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700692 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700693 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700694 }
695 } else {
696 tableType = RouteController::INTERFACE;
697 }
698
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700699 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
700 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700701}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700702
703int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
704 if (mDefaultNetId == NETID_UNSET) {
705 return 0;
706 }
707 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900708 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700709 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
710 return -ESRCH;
711 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900712 if (network->getType() != Network::PHYSICAL) {
713 ALOGE("inconceivable! default network must be a physical network");
714 return -EINVAL;
715 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700716 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
717 for (const auto& physicalInterface : network->getInterfaces()) {
718 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
719 add)) {
720 return ret;
721 }
722 }
723 return 0;
724}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900725
726} // namespace net
727} // namespace android