blob: 7c2a82664a26c7839dfcf0caa10f35b73e334683 [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
Lorenzo Colitti36679362015-02-25 10:26:19 +090041#include "DummyNetwork.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090042#include "DumpWriter.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070043#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070044#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070045#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070046#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070047#include "VirtualNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070048
49namespace {
50
51// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070052const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070053const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070054
55} // namespace
56
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070057const unsigned NetworkController::MIN_OEM_ID = 1;
58const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090059const unsigned NetworkController::DUMMY_NET_ID = 51;
60// NetIds 52..98 are reserved for future use.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070061const unsigned NetworkController::LOCAL_NET_ID = 99;
62
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070063// All calls to methods here are made while holding a write lock on mRWLock.
64class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
65public:
66 explicit DelegateImpl(NetworkController* networkController);
67 virtual ~DelegateImpl();
68
69 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
70 Permission permission, bool add) WARN_UNUSED_RESULT;
71
72private:
73 int addFallthrough(const std::string& physicalInterface,
74 Permission permission) override WARN_UNUSED_RESULT;
75 int removeFallthrough(const std::string& physicalInterface,
76 Permission permission) override WARN_UNUSED_RESULT;
77
78 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
79 bool add) WARN_UNUSED_RESULT;
80
81 NetworkController* const mNetworkController;
82};
83
84NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
85 mNetworkController(networkController) {
86}
87
88NetworkController::DelegateImpl::~DelegateImpl() {
89}
90
91int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
92 const std::string& physicalInterface,
93 Permission permission, bool add) {
94 if (add) {
95 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
96 physicalInterface.c_str(),
97 permission)) {
98 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
99 vpnNetId);
100 return ret;
101 }
102 } else {
103 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
104 physicalInterface.c_str(),
105 permission)) {
106 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
107 vpnNetId);
108 return ret;
109 }
110 }
111 return 0;
112}
113
114int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
115 Permission permission) {
116 return modifyFallthrough(physicalInterface, permission, true);
117}
118
119int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
120 Permission permission) {
121 return modifyFallthrough(physicalInterface, permission, false);
122}
123
124int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
125 Permission permission, bool add) {
126 for (const auto& entry : mNetworkController->mNetworks) {
127 if (entry.second->getType() == Network::VIRTUAL) {
128 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
129 return ret;
130 }
131 }
132 }
133 return 0;
134}
135
136NetworkController::NetworkController() :
137 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET) {
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
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700184uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500185 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700186 Fwmark fwmark;
187 fwmark.protectedFromVpn = true;
188 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900189 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700190 // If a non-zero NetId was explicitly specified, and the user has permission for that
191 // network, use that network's DNS servers. Do not fall through to the default network even
192 // if the explicitly selected network is a split tunnel VPN or a VPN without DNS servers.
193 fwmark.explicitlySelected = true;
194 } else {
195 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
196 // (possibly falling through to the default network if the VPN doesn't provide a route to
197 // them). Otherwise, use the default network's DNS servers.
198 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
199 if (virtualNetwork && virtualNetwork->getHasDns()) {
200 *netId = virtualNetwork->getNetId();
201 } else {
202 *netId = mDefaultNetId;
203 }
204 }
205 fwmark.netId = *netId;
206 return fwmark.intValue;
207}
208
209// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
210// the VPN that applies to the UID if any; otherwise, the default network.
211unsigned NetworkController::getNetworkForUser(uid_t uid) const {
212 android::RWLock::AutoRLock lock(mRWLock);
213 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700214 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500215 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700216 return mDefaultNetId;
217}
218
219// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
220// applies to the user if any; otherwise, the default network.
221//
222// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
223// is a split-tunnel and disappears later, the socket continues working (since the default network's
224// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
225// high-priority routing rule that doesn't care what NetId the socket has.
226//
227// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
228// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
229// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
230// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
231// the fallthrough rules also go away), the socket that used to fallthrough to the default network
232// will stop working.
233unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
234 android::RWLock::AutoRLock lock(mRWLock);
235 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
236 if (virtualNetwork && !virtualNetwork->isSecure()) {
237 return virtualNetwork->getNetId();
238 }
239 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500240}
241
Erik Klinecea2d342015-06-25 18:24:46 +0900242void NetworkController::getNetworkContext(
243 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
244 struct android_net_context nc = {
245 .app_netid = netId,
246 .app_mark = MARK_UNSET,
247 .dns_netid = netId,
248 .dns_mark = MARK_UNSET,
249 .uid = uid,
250 };
251
Erik Kline492ca5b2016-03-09 14:56:00 +0900252 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
253 // client process. This value is nonzero iff.:
254 //
255 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
256 // - [Java] android.net.Network#getAllByName()
257 // - [C/++] android_getaddrinfofornetwork()
258 // 2. The app specified a netid/nethandle to be used as a process default via:
259 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
260 // - [C/++] android_setprocnetwork()
261 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
262 //
263 // In all these cases (with the possible exception of #3), the right thing to do is to treat
264 // such cases as explicitlySelected.
265 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
266 if (!explicitlySelected) {
Erik Klinecea2d342015-06-25 18:24:46 +0900267 nc.app_netid = getNetworkForConnect(uid);
268 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900269
Erik Klinecea2d342015-06-25 18:24:46 +0900270 Fwmark fwmark;
271 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900272 fwmark.explicitlySelected = explicitlySelected;
273 fwmark.protectedFromVpn = canProtect(uid);
274 fwmark.permission = getPermissionForUser(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900275 nc.app_mark = fwmark.intValue;
276
277 nc.dns_mark = getNetworkForDns(&(nc.dns_netid), uid);
278
279 if (netcontext) {
280 *netcontext = nc;
281 }
282}
283
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700284unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700285 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700286 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700287 if (entry.second->hasInterface(interface)) {
288 return entry.first;
289 }
290 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400291 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500292}
293
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700294bool NetworkController::isVirtualNetwork(unsigned netId) const {
295 android::RWLock::AutoRLock lock(mRWLock);
296 Network* network = getNetworkLocked(netId);
297 return network && network->getType() == Network::VIRTUAL;
298}
299
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700300int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700301 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
302 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400303 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900304 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700305 }
306
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700307 if (isValidNetwork(netId)) {
308 ALOGE("duplicate netId %u", netId);
309 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700310 }
311
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700312 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700313 if (int ret = physicalNetwork->setPermission(permission)) {
314 ALOGE("inconceivable! setPermission cannot fail on an empty network");
315 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900316 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700317 }
318
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700319 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700320 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900321 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700322}
323
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700324int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700325 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700326 ALOGE("invalid netId %u", netId);
327 return -EINVAL;
328 }
329
330 if (isValidNetwork(netId)) {
331 ALOGE("duplicate netId %u", netId);
332 return -EEXIST;
333 }
334
335 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700336 if (int ret = modifyFallthroughLocked(netId, true)) {
337 return ret;
338 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700339 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700340 return 0;
341}
342
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700343int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900344 if (netId == LOCAL_NET_ID) {
345 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700346 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700347 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900348 if (!isValidNetwork(netId)) {
349 ALOGE("no such netId %u", netId);
350 return -ENONET;
351 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700352
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700353 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700354
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700355 android::RWLock::AutoWLock lock(mRWLock);
356 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900357
358 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
359 // other network code, ignore failures and attempt to clear out as much state as possible, even
360 // if we hit an error on the way. Return the first error that we see.
361 int ret = network->clearInterfaces();
362
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700363 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900364 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700365 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900366 if (!ret) {
367 ret = err;
368 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700369 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700370 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700371 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900372 if (int err = modifyFallthroughLocked(netId, false)) {
373 if (!ret) {
374 ret = err;
375 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700376 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700377 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700378 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700379 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700380 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900381 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700382}
383
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700384int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700385 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900386 ALOGE("no such netId %u", netId);
387 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700388 }
389
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700390 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700391 if (existingNetId != NETID_UNSET && existingNetId != netId) {
392 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
393 return -EBUSY;
394 }
395
396 android::RWLock::AutoWLock lock(mRWLock);
397 return getNetworkLocked(netId)->addInterface(interface);
398}
399
400int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
401 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900402 ALOGE("no such netId %u", netId);
403 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700404 }
405
406 android::RWLock::AutoWLock lock(mRWLock);
407 return getNetworkLocked(netId)->removeInterface(interface);
408}
409
410Permission NetworkController::getPermissionForUser(uid_t uid) const {
411 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700412 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700413}
414
415void NetworkController::setPermissionForUsers(Permission permission,
416 const std::vector<uid_t>& uids) {
417 android::RWLock::AutoWLock lock(mRWLock);
418 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700419 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700420 }
421}
422
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900423int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700424 android::RWLock::AutoRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900425 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700426}
427
428int NetworkController::setPermissionForNetworks(Permission permission,
429 const std::vector<unsigned>& netIds) {
430 android::RWLock::AutoWLock lock(mRWLock);
431 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700432 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900433 if (!network) {
434 ALOGE("no such netId %u", netId);
435 return -ENONET;
436 }
437 if (network->getType() != Network::PHYSICAL) {
438 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700439 return -EINVAL;
440 }
441
442 // TODO: ioctl(SIOCKILLADDR, ...) to kill socets on the network that don't have permission.
443
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700444 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700445 return ret;
446 }
447 }
448 return 0;
449}
450
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700451int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
452 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700453 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900454 if (!network) {
455 ALOGE("no such netId %u", netId);
456 return -ENONET;
457 }
458 if (network->getType() != Network::VIRTUAL) {
459 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700460 return -EINVAL;
461 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900462 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700463 return ret;
464 }
465 return 0;
466}
467
468int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
469 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700470 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900471 if (!network) {
472 ALOGE("no such netId %u", netId);
473 return -ENONET;
474 }
475 if (network->getType() != Network::VIRTUAL) {
476 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700477 return -EINVAL;
478 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900479 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
480 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700481 return ret;
482 }
483 return 0;
484}
485
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700486int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
487 const char* nexthop, bool legacy, uid_t uid) {
488 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
489}
490
491int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
492 const char* nexthop, bool legacy, uid_t uid) {
493 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
494}
495
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700496bool NetworkController::canProtect(uid_t uid) const {
497 android::RWLock::AutoRLock lock(mRWLock);
498 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
499 mProtectableUsers.find(uid) != mProtectableUsers.end();
500}
501
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700502void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
503 android::RWLock::AutoWLock lock(mRWLock);
504 mProtectableUsers.insert(uids.begin(), uids.end());
505}
506
507void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
508 android::RWLock::AutoWLock lock(mRWLock);
509 for (uid_t uid : uids) {
510 mProtectableUsers.erase(uid);
511 }
512}
513
Erik Kline2d3a1632016-03-15 16:33:48 +0900514void NetworkController::dump(DumpWriter& dw) {
515 android::RWLock::AutoRLock lock(mRWLock);
516
517 dw.incIndent();
518 dw.println("NetworkController");
519
520 dw.incIndent();
521 dw.println("Default network: %u", mDefaultNetId);
522
523 dw.blankline();
524 dw.println("Networks:");
525 dw.incIndent();
526 for (const auto& i : mNetworks) {
527 dw.println(i.second->toString().c_str());
528 }
529 dw.decIndent();
530
531 dw.decIndent();
532
533 dw.decIndent();
534}
535
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700536bool NetworkController::isValidNetwork(unsigned netId) const {
537 android::RWLock::AutoRLock lock(mRWLock);
538 return getNetworkLocked(netId);
539}
540
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700541Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700542 auto iter = mNetworks.find(netId);
543 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700544}
545
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700546VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
547 for (const auto& entry : mNetworks) {
548 if (entry.second->getType() == Network::VIRTUAL) {
549 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
550 if (virtualNetwork->appliesToUser(uid)) {
551 return virtualNetwork;
552 }
553 }
554 }
555 return NULL;
556}
557
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700558Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
559 auto iter = mUsers.find(uid);
560 if (iter != mUsers.end()) {
561 return iter->second;
562 }
563 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
564}
565
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900566int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700567 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900568 if (!network) {
569 return -ENONET;
570 }
571
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700572 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
573 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900574 if (uid == INVALID_UID) {
575 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700576 }
577 Permission userPermission = getPermissionForUserLocked(uid);
578 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900579 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700580 }
581 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900582 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700583 }
584 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
585 if (virtualNetwork && virtualNetwork->isSecure() &&
586 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900587 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700588 }
589 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900590 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700591}
592
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700593int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
594 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900595 if (!isValidNetwork(netId)) {
596 ALOGE("no such netId %u", netId);
597 return -ENONET;
598 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700599 unsigned existingNetId = getNetworkForInterface(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900600 if (existingNetId == NETID_UNSET) {
601 ALOGE("interface %s not assigned to any netId", interface);
602 return -ENODEV;
603 }
604 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700605 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900606 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700607 }
608
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700609 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700610 if (netId == LOCAL_NET_ID) {
611 tableType = RouteController::LOCAL_NETWORK;
612 } else if (legacy) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700613 if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700614 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700615 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700616 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700617 }
618 } else {
619 tableType = RouteController::INTERFACE;
620 }
621
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700622 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
623 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700624}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700625
626int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
627 if (mDefaultNetId == NETID_UNSET) {
628 return 0;
629 }
630 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900631 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700632 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
633 return -ESRCH;
634 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900635 if (network->getType() != Network::PHYSICAL) {
636 ALOGE("inconceivable! default network must be a physical network");
637 return -EINVAL;
638 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700639 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
640 for (const auto& physicalInterface : network->getInterfaces()) {
641 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
642 add)) {
643 return ret;
644 }
645 }
646 return 0;
647}