blob: b90976b6b72d19d3da58da04ee1cf715c731c013 [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//
22// In some cases, a single non-const method acquires and releases the lock several times, like so:
23// if (isValidNetwork(...)) { // isValidNetwork() acquires and releases the lock.
24// setDefaultNetwork(...); // setDefaultNetwork() also acquires and releases the lock.
25//
26// It might seem that this allows races where the state changes between the two statements, but in
27// fact there are no races because:
28// 1. This pattern only occurs in non-const methods (i.e., those that mutate state).
29// 2. Only CommandListener calls these non-const methods. The others call only const methods.
30// 3. CommandListener only processes one command at a time. I.e., it's serialized.
31// Thus, no other mutation can occur in between the two statements above.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050032
33#include "NetworkController.h"
34
Erik Kline2d3a1632016-03-15 16:33:48 +090035#define LOG_TAG "Netd"
36#include "log/log.h"
37
38#include "cutils/misc.h"
39#include "resolv_netid.h"
40
Pierre Imai3a272072016-04-19 16:17:07 +090041#include "Controllers.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090042#include "DummyNetwork.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090043#include "DumpWriter.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070044#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070045#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070046#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070047#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070048#include "VirtualNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070049
Erik Kline6d4669f2017-05-25 17:03:31 +090050#define DBG 0
51
Lorenzo Colitti7035f222017-02-13 18:29:00 +090052namespace android {
53namespace net {
54
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070055namespace {
56
57// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070058const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070059const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070060
61} // namespace
62
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070063const unsigned NetworkController::MIN_OEM_ID = 1;
64const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090065const unsigned NetworkController::DUMMY_NET_ID = 51;
66// NetIds 52..98 are reserved for future use.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070067const unsigned NetworkController::LOCAL_NET_ID = 99;
68
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070069// All calls to methods here are made while holding a write lock on mRWLock.
70class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
71public:
72 explicit DelegateImpl(NetworkController* networkController);
73 virtual ~DelegateImpl();
74
75 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
76 Permission permission, bool add) WARN_UNUSED_RESULT;
77
78private:
79 int addFallthrough(const std::string& physicalInterface,
80 Permission permission) override WARN_UNUSED_RESULT;
81 int removeFallthrough(const std::string& physicalInterface,
82 Permission permission) override WARN_UNUSED_RESULT;
83
84 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
85 bool add) WARN_UNUSED_RESULT;
86
87 NetworkController* const mNetworkController;
88};
89
90NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
91 mNetworkController(networkController) {
92}
93
94NetworkController::DelegateImpl::~DelegateImpl() {
95}
96
97int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
98 const std::string& physicalInterface,
99 Permission permission, bool add) {
100 if (add) {
101 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
102 physicalInterface.c_str(),
103 permission)) {
104 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
105 vpnNetId);
106 return ret;
107 }
108 } else {
109 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
110 physicalInterface.c_str(),
111 permission)) {
112 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
113 vpnNetId);
114 return ret;
115 }
116 }
117 return 0;
118}
119
120int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
121 Permission permission) {
122 return modifyFallthrough(physicalInterface, permission, true);
123}
124
125int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
126 Permission permission) {
127 return modifyFallthrough(physicalInterface, permission, false);
128}
129
130int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
131 Permission permission, bool add) {
132 for (const auto& entry : mNetworkController->mNetworks) {
133 if (entry.second->getType() == Network::VIRTUAL) {
134 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
135 return ret;
136 }
137 }
138 }
139 return 0;
140}
141
142NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900143 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
144 mProtectableUsers({AID_VPN}) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700145 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900146 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500147}
148
149unsigned NetworkController::getDefaultNetwork() const {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700150 android::RWLock::AutoRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500151 return mDefaultNetId;
152}
153
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700154int NetworkController::setDefaultNetwork(unsigned netId) {
155 android::RWLock::AutoWLock lock(mRWLock);
156
157 if (netId == mDefaultNetId) {
158 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700159 }
160
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700161 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700162 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900163 if (!network) {
164 ALOGE("no such netId %u", netId);
165 return -ENONET;
166 }
167 if (network->getType() != Network::PHYSICAL) {
168 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700169 return -EINVAL;
170 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700171 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700172 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700173 }
174 }
175
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700176 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700177 Network* network = getNetworkLocked(mDefaultNetId);
178 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700179 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
180 return -ESRCH;
181 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700182 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700183 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700184 }
185 }
186
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700187 mDefaultNetId = netId;
188 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500189}
190
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700191uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500192 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700193 Fwmark fwmark;
194 fwmark.protectedFromVpn = true;
195 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900196 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700197 // If a non-zero NetId was explicitly specified, and the user has permission for that
198 // network, use that network's DNS servers. Do not fall through to the default network even
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900199 // if the explicitly selected network is a split tunnel VPN: the explicitlySelected bit
200 // ensures that the VPN fallthrough rule does not match.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700201 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900202
203 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
204 // servers (through the default network). Otherwise, the query is guaranteed to fail.
205 // http://b/29498052
206 Network *network = getNetworkLocked(*netId);
207 if (network && network->getType() == Network::VIRTUAL &&
208 !static_cast<VirtualNetwork *>(network)->getHasDns()) {
209 *netId = mDefaultNetId;
210 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700211 } else {
212 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
213 // (possibly falling through to the default network if the VPN doesn't provide a route to
214 // them). Otherwise, use the default network's DNS servers.
215 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
216 if (virtualNetwork && virtualNetwork->getHasDns()) {
217 *netId = virtualNetwork->getNetId();
218 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900219 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
220 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700221 *netId = mDefaultNetId;
222 }
223 }
224 fwmark.netId = *netId;
225 return fwmark.intValue;
226}
227
228// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
229// the VPN that applies to the UID if any; otherwise, the default network.
230unsigned NetworkController::getNetworkForUser(uid_t uid) const {
231 android::RWLock::AutoRLock lock(mRWLock);
232 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700233 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500234 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700235 return mDefaultNetId;
236}
237
238// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
239// applies to the user if any; otherwise, the default network.
240//
241// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
242// is a split-tunnel and disappears later, the socket continues working (since the default network's
243// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
244// high-priority routing rule that doesn't care what NetId the socket has.
245//
246// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
247// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
248// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
249// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
250// the fallthrough rules also go away), the socket that used to fallthrough to the default network
251// will stop working.
252unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
253 android::RWLock::AutoRLock lock(mRWLock);
254 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
255 if (virtualNetwork && !virtualNetwork->isSecure()) {
256 return virtualNetwork->getNetId();
257 }
258 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500259}
260
Erik Klinecea2d342015-06-25 18:24:46 +0900261void NetworkController::getNetworkContext(
262 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
263 struct android_net_context nc = {
264 .app_netid = netId,
265 .app_mark = MARK_UNSET,
266 .dns_netid = netId,
267 .dns_mark = MARK_UNSET,
268 .uid = uid,
269 };
270
Erik Kline492ca5b2016-03-09 14:56:00 +0900271 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
272 // client process. This value is nonzero iff.:
273 //
274 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
275 // - [Java] android.net.Network#getAllByName()
276 // - [C/++] android_getaddrinfofornetwork()
277 // 2. The app specified a netid/nethandle to be used as a process default via:
278 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
279 // - [C/++] android_setprocnetwork()
280 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
281 //
282 // In all these cases (with the possible exception of #3), the right thing to do is to treat
283 // such cases as explicitlySelected.
284 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
285 if (!explicitlySelected) {
Erik Klinecea2d342015-06-25 18:24:46 +0900286 nc.app_netid = getNetworkForConnect(uid);
287 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900288
Erik Klinecea2d342015-06-25 18:24:46 +0900289 Fwmark fwmark;
290 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900291 fwmark.explicitlySelected = explicitlySelected;
Erik Kline6d4669f2017-05-25 17:03:31 +0900292 fwmark.protectedFromVpn = explicitlySelected && canProtect(uid);
Erik Kline492ca5b2016-03-09 14:56:00 +0900293 fwmark.permission = getPermissionForUser(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900294 nc.app_mark = fwmark.intValue;
295
296 nc.dns_mark = getNetworkForDns(&(nc.dns_netid), uid);
297
Erik Kline6d4669f2017-05-25 17:03:31 +0900298 if (DBG) {
299 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
300 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
301 }
302
Erik Klinecea2d342015-06-25 18:24:46 +0900303 if (netcontext) {
304 *netcontext = nc;
305 }
306}
307
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700308unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700309 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700310 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700311 if (entry.second->hasInterface(interface)) {
312 return entry.first;
313 }
314 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400315 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500316}
317
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700318bool NetworkController::isVirtualNetwork(unsigned netId) const {
319 android::RWLock::AutoRLock lock(mRWLock);
320 Network* network = getNetworkLocked(netId);
321 return network && network->getType() == Network::VIRTUAL;
322}
323
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700324int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700325 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
326 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400327 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900328 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700329 }
330
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700331 if (isValidNetwork(netId)) {
332 ALOGE("duplicate netId %u", netId);
333 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700334 }
335
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700336 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700337 if (int ret = physicalNetwork->setPermission(permission)) {
338 ALOGE("inconceivable! setPermission cannot fail on an empty network");
339 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900340 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700341 }
342
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700343 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700344 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900345 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700346}
347
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700348int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700349 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700350 ALOGE("invalid netId %u", netId);
351 return -EINVAL;
352 }
353
354 if (isValidNetwork(netId)) {
355 ALOGE("duplicate netId %u", netId);
356 return -EEXIST;
357 }
358
359 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700360 if (int ret = modifyFallthroughLocked(netId, true)) {
361 return ret;
362 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700363 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700364 return 0;
365}
366
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700367int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900368 if (netId == LOCAL_NET_ID) {
369 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700370 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700371 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900372 if (!isValidNetwork(netId)) {
373 ALOGE("no such netId %u", netId);
374 return -ENONET;
375 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700376
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700377 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700378
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700379 android::RWLock::AutoWLock lock(mRWLock);
380 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900381
382 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
383 // other network code, ignore failures and attempt to clear out as much state as possible, even
384 // if we hit an error on the way. Return the first error that we see.
385 int ret = network->clearInterfaces();
386
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700387 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900388 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700389 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900390 if (!ret) {
391 ret = err;
392 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700393 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700394 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700395 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900396 if (int err = modifyFallthroughLocked(netId, false)) {
397 if (!ret) {
398 ret = err;
399 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700400 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700401 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700402 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700403 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700404 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900405 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700406}
407
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700408int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700409 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900410 ALOGE("no such netId %u", netId);
411 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700412 }
413
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700414 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700415 if (existingNetId != NETID_UNSET && existingNetId != netId) {
416 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
417 return -EBUSY;
418 }
419
420 android::RWLock::AutoWLock lock(mRWLock);
421 return getNetworkLocked(netId)->addInterface(interface);
422}
423
424int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
425 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900426 ALOGE("no such netId %u", netId);
427 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700428 }
429
430 android::RWLock::AutoWLock lock(mRWLock);
431 return getNetworkLocked(netId)->removeInterface(interface);
432}
433
434Permission NetworkController::getPermissionForUser(uid_t uid) const {
435 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700436 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700437}
438
439void NetworkController::setPermissionForUsers(Permission permission,
440 const std::vector<uid_t>& uids) {
441 android::RWLock::AutoWLock lock(mRWLock);
442 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700443 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700444 }
445}
446
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900447int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700448 android::RWLock::AutoRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900449 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700450}
451
452int NetworkController::setPermissionForNetworks(Permission permission,
453 const std::vector<unsigned>& netIds) {
454 android::RWLock::AutoWLock lock(mRWLock);
455 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700456 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900457 if (!network) {
458 ALOGE("no such netId %u", netId);
459 return -ENONET;
460 }
461 if (network->getType() != Network::PHYSICAL) {
462 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700463 return -EINVAL;
464 }
465
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700466 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700467 return ret;
468 }
469 }
470 return 0;
471}
472
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700473int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
474 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700475 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900476 if (!network) {
477 ALOGE("no such netId %u", netId);
478 return -ENONET;
479 }
480 if (network->getType() != Network::VIRTUAL) {
481 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700482 return -EINVAL;
483 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900484 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700485 return ret;
486 }
487 return 0;
488}
489
490int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
491 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700492 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900493 if (!network) {
494 ALOGE("no such netId %u", netId);
495 return -ENONET;
496 }
497 if (network->getType() != Network::VIRTUAL) {
498 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700499 return -EINVAL;
500 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900501 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
502 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700503 return ret;
504 }
505 return 0;
506}
507
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700508int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
509 const char* nexthop, bool legacy, uid_t uid) {
510 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
511}
512
513int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
514 const char* nexthop, bool legacy, uid_t uid) {
515 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
516}
517
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700518bool NetworkController::canProtect(uid_t uid) const {
519 android::RWLock::AutoRLock lock(mRWLock);
520 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
521 mProtectableUsers.find(uid) != mProtectableUsers.end();
522}
523
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700524void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
525 android::RWLock::AutoWLock lock(mRWLock);
526 mProtectableUsers.insert(uids.begin(), uids.end());
527}
528
529void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
530 android::RWLock::AutoWLock lock(mRWLock);
531 for (uid_t uid : uids) {
532 mProtectableUsers.erase(uid);
533 }
534}
535
Erik Kline2d3a1632016-03-15 16:33:48 +0900536void NetworkController::dump(DumpWriter& dw) {
537 android::RWLock::AutoRLock lock(mRWLock);
538
539 dw.incIndent();
540 dw.println("NetworkController");
541
542 dw.incIndent();
543 dw.println("Default network: %u", mDefaultNetId);
544
545 dw.blankline();
546 dw.println("Networks:");
547 dw.incIndent();
548 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900549 Network* network = i.second;
550 dw.println(network->toString().c_str());
551 if (network->getType() == Network::PHYSICAL) {
552 dw.incIndent();
553 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
554 dw.println("Required permission: %s", permissionToName(permission));
555 dw.decIndent();
556 }
Pierre Imai3a272072016-04-19 16:17:07 +0900557 android::net::gCtls->resolverCtrl.dump(dw, i.first);
558 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900559 }
560 dw.decIndent();
561
562 dw.decIndent();
563
564 dw.decIndent();
565}
566
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700567bool NetworkController::isValidNetwork(unsigned netId) const {
568 android::RWLock::AutoRLock lock(mRWLock);
569 return getNetworkLocked(netId);
570}
571
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700572Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700573 auto iter = mNetworks.find(netId);
574 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700575}
576
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700577VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
578 for (const auto& entry : mNetworks) {
579 if (entry.second->getType() == Network::VIRTUAL) {
580 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
581 if (virtualNetwork->appliesToUser(uid)) {
582 return virtualNetwork;
583 }
584 }
585 }
586 return NULL;
587}
588
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700589Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
590 auto iter = mUsers.find(uid);
591 if (iter != mUsers.end()) {
592 return iter->second;
593 }
594 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
595}
596
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900597int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700598 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900599 if (!network) {
600 return -ENONET;
601 }
602
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700603 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
604 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900605 if (uid == INVALID_UID) {
606 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700607 }
608 Permission userPermission = getPermissionForUserLocked(uid);
609 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900610 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700611 }
612 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900613 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700614 }
615 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
616 if (virtualNetwork && virtualNetwork->isSecure() &&
617 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900618 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700619 }
620 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900621 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700622}
623
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700624int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
625 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900626 if (!isValidNetwork(netId)) {
627 ALOGE("no such netId %u", netId);
628 return -ENONET;
629 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700630 unsigned existingNetId = getNetworkForInterface(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900631 if (existingNetId == NETID_UNSET) {
632 ALOGE("interface %s not assigned to any netId", interface);
633 return -ENODEV;
634 }
635 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700636 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900637 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700638 }
639
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700640 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700641 if (netId == LOCAL_NET_ID) {
642 tableType = RouteController::LOCAL_NETWORK;
643 } else if (legacy) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700644 if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700645 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700646 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700647 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700648 }
649 } else {
650 tableType = RouteController::INTERFACE;
651 }
652
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700653 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
654 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700655}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700656
657int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
658 if (mDefaultNetId == NETID_UNSET) {
659 return 0;
660 }
661 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900662 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700663 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
664 return -ESRCH;
665 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900666 if (network->getType() != Network::PHYSICAL) {
667 ALOGE("inconceivable! default network must be a physical network");
668 return -EINVAL;
669 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700670 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
671 for (const auto& physicalInterface : network->getInterfaces()) {
672 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
673 add)) {
674 return ret;
675 }
676 }
677 return 0;
678}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900679
680} // namespace net
681} // namespace android