blob: 16fc99f7b4f8235c03bbbe2162d647643d5477af [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 Ramachandranf4f6c8d2014-06-23 09:54:06 -070049const unsigned MIN_NET_ID = 10;
50const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070051
52} // namespace
53
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070054NetworkController::NetworkController() : mDefaultNetId(NETID_UNSET) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070055 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050056}
57
58unsigned NetworkController::getDefaultNetwork() const {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -070059 android::RWLock::AutoRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050060 return mDefaultNetId;
61}
62
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070063int NetworkController::setDefaultNetwork(unsigned netId) {
64 android::RWLock::AutoWLock lock(mRWLock);
65
66 if (netId == mDefaultNetId) {
67 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -070068 }
69
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070070 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070071 Network* network = getNetworkLocked(netId);
72 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070073 ALOGE("invalid netId %u", netId);
74 return -EINVAL;
75 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070076 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070077 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -070078 }
79 }
80
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070081 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070082 Network* network = getNetworkLocked(mDefaultNetId);
83 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070084 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
85 return -ESRCH;
86 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070087 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070088 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -070089 }
90 }
91
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070092 mDefaultNetId = netId;
93 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050094}
95
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070096uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050097 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070098 Fwmark fwmark;
99 fwmark.protectedFromVpn = true;
100 fwmark.permission = PERMISSION_SYSTEM;
101 if (canUserSelectNetworkLocked(uid, *netId)) {
102 // If a non-zero NetId was explicitly specified, and the user has permission for that
103 // network, use that network's DNS servers. Do not fall through to the default network even
104 // if the explicitly selected network is a split tunnel VPN or a VPN without DNS servers.
105 fwmark.explicitlySelected = true;
106 } else {
107 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
108 // (possibly falling through to the default network if the VPN doesn't provide a route to
109 // them). Otherwise, use the default network's DNS servers.
110 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
111 if (virtualNetwork && virtualNetwork->getHasDns()) {
112 *netId = virtualNetwork->getNetId();
113 } else {
114 *netId = mDefaultNetId;
115 }
116 }
117 fwmark.netId = *netId;
118 return fwmark.intValue;
119}
120
121// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
122// the VPN that applies to the UID if any; otherwise, the default network.
123unsigned NetworkController::getNetworkForUser(uid_t uid) const {
124 android::RWLock::AutoRLock lock(mRWLock);
125 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700126 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500127 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700128 return mDefaultNetId;
129}
130
131// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
132// applies to the user if any; otherwise, the default network.
133//
134// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
135// is a split-tunnel and disappears later, the socket continues working (since the default network's
136// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
137// high-priority routing rule that doesn't care what NetId the socket has.
138//
139// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
140// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
141// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
142// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
143// the fallthrough rules also go away), the socket that used to fallthrough to the default network
144// will stop working.
145unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
146 android::RWLock::AutoRLock lock(mRWLock);
147 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
148 if (virtualNetwork && !virtualNetwork->isSecure()) {
149 return virtualNetwork->getNetId();
150 }
151 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500152}
153
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700154unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700155 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700156 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700157 if (entry.second->hasInterface(interface)) {
158 return entry.first;
159 }
160 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400161 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500162}
163
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700164bool NetworkController::isVirtualNetwork(unsigned netId) const {
165 android::RWLock::AutoRLock lock(mRWLock);
166 Network* network = getNetworkLocked(netId);
167 return network && network->getType() == Network::VIRTUAL;
168}
169
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700170int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700171 if (netId < MIN_NET_ID || netId > MAX_NET_ID) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400172 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900173 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700174 }
175
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700176 if (isValidNetwork(netId)) {
177 ALOGE("duplicate netId %u", netId);
178 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700179 }
180
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700181 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId);
182 if (int ret = physicalNetwork->setPermission(permission)) {
183 ALOGE("inconceivable! setPermission cannot fail on an empty network");
184 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900185 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700186 }
187
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700188 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700189 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900190 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700191}
192
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700193int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700194 if (netId < MIN_NET_ID || netId > MAX_NET_ID) {
195 ALOGE("invalid netId %u", netId);
196 return -EINVAL;
197 }
198
199 if (isValidNetwork(netId)) {
200 ALOGE("duplicate netId %u", netId);
201 return -EEXIST;
202 }
203
204 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700205 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700206 return 0;
207}
208
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700209int NetworkController::destroyNetwork(unsigned netId) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700210 if (netId == LOCAL_NET_ID || !isValidNetwork(netId)) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700211 ALOGE("invalid netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700212 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700213 }
214
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700215 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700216
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700217 android::RWLock::AutoWLock lock(mRWLock);
218 Network* network = getNetworkLocked(netId);
219 if (int ret = network->clearInterfaces()) {
220 return ret;
221 }
222 if (mDefaultNetId == netId) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700223 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700224 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
225 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700226 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700227 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700228 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700229 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700230 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700231 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900232 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700233}
234
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700235int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700236 if (!isValidNetwork(netId)) {
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700237 ALOGE("invalid netId %u", netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900238 return -EINVAL;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700239 }
240
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700241 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700242 if (existingNetId != NETID_UNSET && existingNetId != netId) {
243 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
244 return -EBUSY;
245 }
246
247 android::RWLock::AutoWLock lock(mRWLock);
248 return getNetworkLocked(netId)->addInterface(interface);
249}
250
251int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
252 if (!isValidNetwork(netId)) {
253 ALOGE("invalid netId %u", netId);
254 return -EINVAL;
255 }
256
257 android::RWLock::AutoWLock lock(mRWLock);
258 return getNetworkLocked(netId)->removeInterface(interface);
259}
260
261Permission NetworkController::getPermissionForUser(uid_t uid) const {
262 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700263 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700264}
265
266void NetworkController::setPermissionForUsers(Permission permission,
267 const std::vector<uid_t>& uids) {
268 android::RWLock::AutoWLock lock(mRWLock);
269 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700270 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700271 }
272}
273
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700274bool NetworkController::canUserSelectNetwork(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700275 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700276 return canUserSelectNetworkLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700277}
278
279int NetworkController::setPermissionForNetworks(Permission permission,
280 const std::vector<unsigned>& netIds) {
281 android::RWLock::AutoWLock lock(mRWLock);
282 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700283 Network* network = getNetworkLocked(netId);
284 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700285 ALOGE("invalid netId %u", netId);
286 return -EINVAL;
287 }
288
289 // TODO: ioctl(SIOCKILLADDR, ...) to kill socets on the network that don't have permission.
290
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700291 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700292 return ret;
293 }
294 }
295 return 0;
296}
297
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700298int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
299 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700300 Network* network = getNetworkLocked(netId);
301 if (!network || network->getType() != Network::VIRTUAL) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700302 ALOGE("invalid netId %u", netId);
303 return -EINVAL;
304 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700305 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700306 return ret;
307 }
308 return 0;
309}
310
311int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
312 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700313 Network* network = getNetworkLocked(netId);
314 if (!network || network->getType() != Network::VIRTUAL) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700315 ALOGE("invalid netId %u", netId);
316 return -EINVAL;
317 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700318 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700319 return ret;
320 }
321 return 0;
322}
323
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700324int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
325 const char* nexthop, bool legacy, uid_t uid) {
326 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
327}
328
329int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
330 const char* nexthop, bool legacy, uid_t uid) {
331 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
332}
333
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700334bool NetworkController::canProtect(uid_t uid) const {
335 android::RWLock::AutoRLock lock(mRWLock);
336 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
337 mProtectableUsers.find(uid) != mProtectableUsers.end();
338}
339
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700340void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
341 android::RWLock::AutoWLock lock(mRWLock);
342 mProtectableUsers.insert(uids.begin(), uids.end());
343}
344
345void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
346 android::RWLock::AutoWLock lock(mRWLock);
347 for (uid_t uid : uids) {
348 mProtectableUsers.erase(uid);
349 }
350}
351
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700352bool NetworkController::isValidNetwork(unsigned netId) const {
353 android::RWLock::AutoRLock lock(mRWLock);
354 return getNetworkLocked(netId);
355}
356
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700357Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700358 auto iter = mNetworks.find(netId);
359 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700360}
361
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700362VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
363 for (const auto& entry : mNetworks) {
364 if (entry.second->getType() == Network::VIRTUAL) {
365 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
366 if (virtualNetwork->appliesToUser(uid)) {
367 return virtualNetwork;
368 }
369 }
370 }
371 return NULL;
372}
373
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700374Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
375 auto iter = mUsers.find(uid);
376 if (iter != mUsers.end()) {
377 return iter->second;
378 }
379 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
380}
381
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700382bool NetworkController::canUserSelectNetworkLocked(uid_t uid, unsigned netId) const {
383 Network* network = getNetworkLocked(netId);
384 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
385 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
386 if (!network || uid == INVALID_UID) {
387 return false;
388 }
389 Permission userPermission = getPermissionForUserLocked(uid);
390 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
391 return true;
392 }
393 if (network->getType() == Network::VIRTUAL) {
394 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid);
395 }
396 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
397 if (virtualNetwork && virtualNetwork->isSecure() &&
398 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
399 return false;
400 }
401 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
402 return (userPermission & networkPermission) == networkPermission;
403}
404
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700405int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
406 const char* nexthop, bool add, bool legacy, uid_t uid) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700407 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700408 if (netId == NETID_UNSET || existingNetId != netId) {
409 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900410 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700411 }
412
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700413 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700414 if (netId == LOCAL_NET_ID) {
415 tableType = RouteController::LOCAL_NETWORK;
416 } else if (legacy) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700417 if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700418 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700419 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700420 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700421 }
422 } else {
423 tableType = RouteController::INTERFACE;
424 }
425
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700426 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
427 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700428}