blob: 73826179b22b2fe897c1fda5533674e33268c15b [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
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070035#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070036#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070037#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070038#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070039#include "VirtualNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070040
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -070041#include "cutils/misc.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070042#define LOG_TAG "Netd"
43#include "log/log.h"
Sreeram Ramachandran72604072014-05-21 13:19:43 -070044#include "resolv_netid.h"
45
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070046namespace {
47
48// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070049const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070050const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070051
52} // namespace
53
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070054const unsigned NetworkController::MIN_OEM_ID = 1;
55const unsigned NetworkController::MAX_OEM_ID = 50;
56// NetIds 51..98 are reserved for future use.
57const unsigned NetworkController::LOCAL_NET_ID = 99;
58
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070059// All calls to methods here are made while holding a write lock on mRWLock.
60class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
61public:
62 explicit DelegateImpl(NetworkController* networkController);
63 virtual ~DelegateImpl();
64
65 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
66 Permission permission, bool add) WARN_UNUSED_RESULT;
67
68private:
69 int addFallthrough(const std::string& physicalInterface,
70 Permission permission) override WARN_UNUSED_RESULT;
71 int removeFallthrough(const std::string& physicalInterface,
72 Permission permission) override WARN_UNUSED_RESULT;
73
74 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
75 bool add) WARN_UNUSED_RESULT;
76
77 NetworkController* const mNetworkController;
78};
79
80NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
81 mNetworkController(networkController) {
82}
83
84NetworkController::DelegateImpl::~DelegateImpl() {
85}
86
87int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
88 const std::string& physicalInterface,
89 Permission permission, bool add) {
90 if (add) {
91 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
92 physicalInterface.c_str(),
93 permission)) {
94 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
95 vpnNetId);
96 return ret;
97 }
98 } else {
99 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
100 physicalInterface.c_str(),
101 permission)) {
102 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
103 vpnNetId);
104 return ret;
105 }
106 }
107 return 0;
108}
109
110int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
111 Permission permission) {
112 return modifyFallthrough(physicalInterface, permission, true);
113}
114
115int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
116 Permission permission) {
117 return modifyFallthrough(physicalInterface, permission, false);
118}
119
120int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
121 Permission permission, bool add) {
122 for (const auto& entry : mNetworkController->mNetworks) {
123 if (entry.second->getType() == Network::VIRTUAL) {
124 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
125 return ret;
126 }
127 }
128 }
129 return 0;
130}
131
132NetworkController::NetworkController() :
133 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700134 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500135}
136
137unsigned NetworkController::getDefaultNetwork() const {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700138 android::RWLock::AutoRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500139 return mDefaultNetId;
140}
141
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700142int NetworkController::setDefaultNetwork(unsigned netId) {
143 android::RWLock::AutoWLock lock(mRWLock);
144
145 if (netId == mDefaultNetId) {
146 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700147 }
148
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700149 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700150 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900151 if (!network) {
152 ALOGE("no such netId %u", netId);
153 return -ENONET;
154 }
155 if (network->getType() != Network::PHYSICAL) {
156 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700157 return -EINVAL;
158 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700159 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700160 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700161 }
162 }
163
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700164 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700165 Network* network = getNetworkLocked(mDefaultNetId);
166 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700167 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
168 return -ESRCH;
169 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700170 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700171 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700172 }
173 }
174
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700175 mDefaultNetId = netId;
176 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500177}
178
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700179uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500180 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700181 Fwmark fwmark;
182 fwmark.protectedFromVpn = true;
183 fwmark.permission = PERMISSION_SYSTEM;
184 if (canUserSelectNetworkLocked(uid, *netId)) {
185 // If a non-zero NetId was explicitly specified, and the user has permission for that
186 // network, use that network's DNS servers. Do not fall through to the default network even
187 // if the explicitly selected network is a split tunnel VPN or a VPN without DNS servers.
188 fwmark.explicitlySelected = true;
189 } else {
190 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
191 // (possibly falling through to the default network if the VPN doesn't provide a route to
192 // them). Otherwise, use the default network's DNS servers.
193 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
194 if (virtualNetwork && virtualNetwork->getHasDns()) {
195 *netId = virtualNetwork->getNetId();
196 } else {
197 *netId = mDefaultNetId;
198 }
199 }
200 fwmark.netId = *netId;
201 return fwmark.intValue;
202}
203
204// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
205// the VPN that applies to the UID if any; otherwise, the default network.
206unsigned NetworkController::getNetworkForUser(uid_t uid) const {
207 android::RWLock::AutoRLock lock(mRWLock);
208 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700209 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500210 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700211 return mDefaultNetId;
212}
213
214// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
215// applies to the user if any; otherwise, the default network.
216//
217// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
218// is a split-tunnel and disappears later, the socket continues working (since the default network's
219// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
220// high-priority routing rule that doesn't care what NetId the socket has.
221//
222// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
223// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
224// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
225// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
226// the fallthrough rules also go away), the socket that used to fallthrough to the default network
227// will stop working.
228unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
229 android::RWLock::AutoRLock lock(mRWLock);
230 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
231 if (virtualNetwork && !virtualNetwork->isSecure()) {
232 return virtualNetwork->getNetId();
233 }
234 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500235}
236
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700237unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700238 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700239 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700240 if (entry.second->hasInterface(interface)) {
241 return entry.first;
242 }
243 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400244 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500245}
246
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700247bool NetworkController::isVirtualNetwork(unsigned netId) const {
248 android::RWLock::AutoRLock lock(mRWLock);
249 Network* network = getNetworkLocked(netId);
250 return network && network->getType() == Network::VIRTUAL;
251}
252
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700253int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700254 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
255 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400256 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900257 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700258 }
259
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700260 if (isValidNetwork(netId)) {
261 ALOGE("duplicate netId %u", netId);
262 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700263 }
264
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700265 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700266 if (int ret = physicalNetwork->setPermission(permission)) {
267 ALOGE("inconceivable! setPermission cannot fail on an empty network");
268 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900269 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700270 }
271
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700272 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700273 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900274 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700275}
276
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700277int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700278 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700279 ALOGE("invalid netId %u", netId);
280 return -EINVAL;
281 }
282
283 if (isValidNetwork(netId)) {
284 ALOGE("duplicate netId %u", netId);
285 return -EEXIST;
286 }
287
288 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700289 if (int ret = modifyFallthroughLocked(netId, true)) {
290 return ret;
291 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700292 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700293 return 0;
294}
295
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700296int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900297 if (netId == LOCAL_NET_ID) {
298 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700299 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700300 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900301 if (!isValidNetwork(netId)) {
302 ALOGE("no such netId %u", netId);
303 return -ENONET;
304 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700305
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700306 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700307
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700308 android::RWLock::AutoWLock lock(mRWLock);
309 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900310
311 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
312 // other network code, ignore failures and attempt to clear out as much state as possible, even
313 // if we hit an error on the way. Return the first error that we see.
314 int ret = network->clearInterfaces();
315
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700316 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900317 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700318 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900319 if (!ret) {
320 ret = err;
321 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700322 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700323 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700324 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900325 if (int err = modifyFallthroughLocked(netId, false)) {
326 if (!ret) {
327 ret = err;
328 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700329 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700330 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700331 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700332 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700333 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900334 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700335}
336
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700337int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700338 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900339 ALOGE("no such netId %u", netId);
340 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700341 }
342
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700343 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700344 if (existingNetId != NETID_UNSET && existingNetId != netId) {
345 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
346 return -EBUSY;
347 }
348
349 android::RWLock::AutoWLock lock(mRWLock);
350 return getNetworkLocked(netId)->addInterface(interface);
351}
352
353int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
354 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900355 ALOGE("no such netId %u", netId);
356 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700357 }
358
359 android::RWLock::AutoWLock lock(mRWLock);
360 return getNetworkLocked(netId)->removeInterface(interface);
361}
362
363Permission NetworkController::getPermissionForUser(uid_t uid) const {
364 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700365 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700366}
367
368void NetworkController::setPermissionForUsers(Permission permission,
369 const std::vector<uid_t>& uids) {
370 android::RWLock::AutoWLock lock(mRWLock);
371 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700372 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700373 }
374}
375
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700376bool NetworkController::canUserSelectNetwork(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700377 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700378 return canUserSelectNetworkLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700379}
380
381int NetworkController::setPermissionForNetworks(Permission permission,
382 const std::vector<unsigned>& netIds) {
383 android::RWLock::AutoWLock lock(mRWLock);
384 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700385 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900386 if (!network) {
387 ALOGE("no such netId %u", netId);
388 return -ENONET;
389 }
390 if (network->getType() != Network::PHYSICAL) {
391 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700392 return -EINVAL;
393 }
394
395 // TODO: ioctl(SIOCKILLADDR, ...) to kill socets on the network that don't have permission.
396
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700397 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700398 return ret;
399 }
400 }
401 return 0;
402}
403
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700404int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
405 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700406 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900407 if (!network) {
408 ALOGE("no such netId %u", netId);
409 return -ENONET;
410 }
411 if (network->getType() != Network::VIRTUAL) {
412 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700413 return -EINVAL;
414 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700415 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700416 return ret;
417 }
418 return 0;
419}
420
421int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
422 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700423 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900424 if (!network) {
425 ALOGE("no such netId %u", netId);
426 return -ENONET;
427 }
428 if (network->getType() != Network::VIRTUAL) {
429 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700430 return -EINVAL;
431 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700432 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700433 return ret;
434 }
435 return 0;
436}
437
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700438int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
439 const char* nexthop, bool legacy, uid_t uid) {
440 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
441}
442
443int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
444 const char* nexthop, bool legacy, uid_t uid) {
445 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
446}
447
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700448bool NetworkController::canProtect(uid_t uid) const {
449 android::RWLock::AutoRLock lock(mRWLock);
450 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
451 mProtectableUsers.find(uid) != mProtectableUsers.end();
452}
453
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700454void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
455 android::RWLock::AutoWLock lock(mRWLock);
456 mProtectableUsers.insert(uids.begin(), uids.end());
457}
458
459void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
460 android::RWLock::AutoWLock lock(mRWLock);
461 for (uid_t uid : uids) {
462 mProtectableUsers.erase(uid);
463 }
464}
465
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700466bool NetworkController::isValidNetwork(unsigned netId) const {
467 android::RWLock::AutoRLock lock(mRWLock);
468 return getNetworkLocked(netId);
469}
470
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700471Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700472 auto iter = mNetworks.find(netId);
473 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700474}
475
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700476VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
477 for (const auto& entry : mNetworks) {
478 if (entry.second->getType() == Network::VIRTUAL) {
479 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
480 if (virtualNetwork->appliesToUser(uid)) {
481 return virtualNetwork;
482 }
483 }
484 }
485 return NULL;
486}
487
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700488Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
489 auto iter = mUsers.find(uid);
490 if (iter != mUsers.end()) {
491 return iter->second;
492 }
493 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
494}
495
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700496bool NetworkController::canUserSelectNetworkLocked(uid_t uid, unsigned netId) const {
497 Network* network = getNetworkLocked(netId);
498 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
499 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
500 if (!network || uid == INVALID_UID) {
501 return false;
502 }
503 Permission userPermission = getPermissionForUserLocked(uid);
504 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
505 return true;
506 }
507 if (network->getType() == Network::VIRTUAL) {
508 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid);
509 }
510 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
511 if (virtualNetwork && virtualNetwork->isSecure() &&
512 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
513 return false;
514 }
515 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
516 return (userPermission & networkPermission) == networkPermission;
517}
518
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700519int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
520 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900521 if (!isValidNetwork(netId)) {
522 ALOGE("no such netId %u", netId);
523 return -ENONET;
524 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700525 unsigned existingNetId = getNetworkForInterface(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900526 if (existingNetId == NETID_UNSET) {
527 ALOGE("interface %s not assigned to any netId", interface);
528 return -ENODEV;
529 }
530 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700531 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900532 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700533 }
534
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700535 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700536 if (netId == LOCAL_NET_ID) {
537 tableType = RouteController::LOCAL_NETWORK;
538 } else if (legacy) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700539 if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700540 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700541 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700542 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700543 }
544 } else {
545 tableType = RouteController::INTERFACE;
546 }
547
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700548 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
549 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700550}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700551
552int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
553 if (mDefaultNetId == NETID_UNSET) {
554 return 0;
555 }
556 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900557 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700558 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
559 return -ESRCH;
560 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900561 if (network->getType() != Network::PHYSICAL) {
562 ALOGE("inconceivable! default network must be a physical network");
563 return -EINVAL;
564 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700565 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
566 for (const auto& physicalInterface : network->getInterfaces()) {
567 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
568 add)) {
569 return ret;
570 }
571 }
572 return 0;
573}