blob: 2ea71e29c2bb3723f541532e7e5e706b85eff9b5 [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 Ramachandran48e19b02014-07-22 22:23:20 -070054// All calls to methods here are made while holding a write lock on mRWLock.
55class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
56public:
57 explicit DelegateImpl(NetworkController* networkController);
58 virtual ~DelegateImpl();
59
60 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
61 Permission permission, bool add) WARN_UNUSED_RESULT;
62
63private:
64 int addFallthrough(const std::string& physicalInterface,
65 Permission permission) override WARN_UNUSED_RESULT;
66 int removeFallthrough(const std::string& physicalInterface,
67 Permission permission) override WARN_UNUSED_RESULT;
68
69 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
70 bool add) WARN_UNUSED_RESULT;
71
72 NetworkController* const mNetworkController;
73};
74
75NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
76 mNetworkController(networkController) {
77}
78
79NetworkController::DelegateImpl::~DelegateImpl() {
80}
81
82int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
83 const std::string& physicalInterface,
84 Permission permission, bool add) {
85 if (add) {
86 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
87 physicalInterface.c_str(),
88 permission)) {
89 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
90 vpnNetId);
91 return ret;
92 }
93 } else {
94 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
95 physicalInterface.c_str(),
96 permission)) {
97 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
98 vpnNetId);
99 return ret;
100 }
101 }
102 return 0;
103}
104
105int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
106 Permission permission) {
107 return modifyFallthrough(physicalInterface, permission, true);
108}
109
110int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
111 Permission permission) {
112 return modifyFallthrough(physicalInterface, permission, false);
113}
114
115int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
116 Permission permission, bool add) {
117 for (const auto& entry : mNetworkController->mNetworks) {
118 if (entry.second->getType() == Network::VIRTUAL) {
119 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
120 return ret;
121 }
122 }
123 }
124 return 0;
125}
126
127NetworkController::NetworkController() :
128 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700129 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500130}
131
132unsigned NetworkController::getDefaultNetwork() const {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700133 android::RWLock::AutoRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500134 return mDefaultNetId;
135}
136
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700137int NetworkController::setDefaultNetwork(unsigned netId) {
138 android::RWLock::AutoWLock lock(mRWLock);
139
140 if (netId == mDefaultNetId) {
141 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700142 }
143
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700144 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700145 Network* network = getNetworkLocked(netId);
146 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700147 ALOGE("invalid netId %u", netId);
148 return -EINVAL;
149 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700150 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700151 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700152 }
153 }
154
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700155 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700156 Network* network = getNetworkLocked(mDefaultNetId);
157 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700158 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
159 return -ESRCH;
160 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700161 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700162 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700163 }
164 }
165
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700166 mDefaultNetId = netId;
167 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500168}
169
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700170uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500171 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700172 Fwmark fwmark;
173 fwmark.protectedFromVpn = true;
174 fwmark.permission = PERMISSION_SYSTEM;
175 if (canUserSelectNetworkLocked(uid, *netId)) {
176 // If a non-zero NetId was explicitly specified, and the user has permission for that
177 // network, use that network's DNS servers. Do not fall through to the default network even
178 // if the explicitly selected network is a split tunnel VPN or a VPN without DNS servers.
179 fwmark.explicitlySelected = true;
180 } else {
181 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
182 // (possibly falling through to the default network if the VPN doesn't provide a route to
183 // them). Otherwise, use the default network's DNS servers.
184 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
185 if (virtualNetwork && virtualNetwork->getHasDns()) {
186 *netId = virtualNetwork->getNetId();
187 } else {
188 *netId = mDefaultNetId;
189 }
190 }
191 fwmark.netId = *netId;
192 return fwmark.intValue;
193}
194
195// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
196// the VPN that applies to the UID if any; otherwise, the default network.
197unsigned NetworkController::getNetworkForUser(uid_t uid) const {
198 android::RWLock::AutoRLock lock(mRWLock);
199 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700200 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500201 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700202 return mDefaultNetId;
203}
204
205// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
206// applies to the user if any; otherwise, the default network.
207//
208// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
209// is a split-tunnel and disappears later, the socket continues working (since the default network's
210// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
211// high-priority routing rule that doesn't care what NetId the socket has.
212//
213// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
214// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
215// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
216// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
217// the fallthrough rules also go away), the socket that used to fallthrough to the default network
218// will stop working.
219unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
220 android::RWLock::AutoRLock lock(mRWLock);
221 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
222 if (virtualNetwork && !virtualNetwork->isSecure()) {
223 return virtualNetwork->getNetId();
224 }
225 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500226}
227
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700228unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700229 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700230 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700231 if (entry.second->hasInterface(interface)) {
232 return entry.first;
233 }
234 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400235 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500236}
237
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700238bool NetworkController::isVirtualNetwork(unsigned netId) const {
239 android::RWLock::AutoRLock lock(mRWLock);
240 Network* network = getNetworkLocked(netId);
241 return network && network->getType() == Network::VIRTUAL;
242}
243
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700244int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700245 if (netId < MIN_NET_ID || netId > MAX_NET_ID) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400246 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900247 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700248 }
249
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700250 if (isValidNetwork(netId)) {
251 ALOGE("duplicate netId %u", netId);
252 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700253 }
254
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700255 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700256 if (int ret = physicalNetwork->setPermission(permission)) {
257 ALOGE("inconceivable! setPermission cannot fail on an empty network");
258 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900259 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700260 }
261
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700262 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700263 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900264 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700265}
266
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700267int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700268 if (netId < MIN_NET_ID || netId > MAX_NET_ID) {
269 ALOGE("invalid netId %u", netId);
270 return -EINVAL;
271 }
272
273 if (isValidNetwork(netId)) {
274 ALOGE("duplicate netId %u", netId);
275 return -EEXIST;
276 }
277
278 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700279 if (int ret = modifyFallthroughLocked(netId, true)) {
280 return ret;
281 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700282 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700283 return 0;
284}
285
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700286int NetworkController::destroyNetwork(unsigned netId) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700287 if (netId == LOCAL_NET_ID || !isValidNetwork(netId)) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700288 ALOGE("invalid netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700289 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700290 }
291
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700292 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700293
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700294 android::RWLock::AutoWLock lock(mRWLock);
295 Network* network = getNetworkLocked(netId);
296 if (int ret = network->clearInterfaces()) {
297 return ret;
298 }
299 if (mDefaultNetId == netId) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700300 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700301 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
302 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700303 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700304 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700305 } else if (network->getType() == Network::VIRTUAL) {
306 if (int ret = modifyFallthroughLocked(netId, false)) {
307 return ret;
308 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700309 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700310 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700311 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700312 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900313 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700314}
315
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700316int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700317 if (!isValidNetwork(netId)) {
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700318 ALOGE("invalid netId %u", netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900319 return -EINVAL;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700320 }
321
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700322 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700323 if (existingNetId != NETID_UNSET && existingNetId != netId) {
324 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
325 return -EBUSY;
326 }
327
328 android::RWLock::AutoWLock lock(mRWLock);
329 return getNetworkLocked(netId)->addInterface(interface);
330}
331
332int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
333 if (!isValidNetwork(netId)) {
334 ALOGE("invalid netId %u", netId);
335 return -EINVAL;
336 }
337
338 android::RWLock::AutoWLock lock(mRWLock);
339 return getNetworkLocked(netId)->removeInterface(interface);
340}
341
342Permission NetworkController::getPermissionForUser(uid_t uid) const {
343 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700344 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700345}
346
347void NetworkController::setPermissionForUsers(Permission permission,
348 const std::vector<uid_t>& uids) {
349 android::RWLock::AutoWLock lock(mRWLock);
350 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700351 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700352 }
353}
354
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700355bool NetworkController::canUserSelectNetwork(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700356 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700357 return canUserSelectNetworkLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700358}
359
360int NetworkController::setPermissionForNetworks(Permission permission,
361 const std::vector<unsigned>& netIds) {
362 android::RWLock::AutoWLock lock(mRWLock);
363 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700364 Network* network = getNetworkLocked(netId);
365 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700366 ALOGE("invalid netId %u", netId);
367 return -EINVAL;
368 }
369
370 // TODO: ioctl(SIOCKILLADDR, ...) to kill socets on the network that don't have permission.
371
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700372 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700373 return ret;
374 }
375 }
376 return 0;
377}
378
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700379int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
380 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700381 Network* network = getNetworkLocked(netId);
382 if (!network || network->getType() != Network::VIRTUAL) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700383 ALOGE("invalid netId %u", netId);
384 return -EINVAL;
385 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700386 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700387 return ret;
388 }
389 return 0;
390}
391
392int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
393 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700394 Network* network = getNetworkLocked(netId);
395 if (!network || network->getType() != Network::VIRTUAL) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700396 ALOGE("invalid netId %u", netId);
397 return -EINVAL;
398 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700399 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700400 return ret;
401 }
402 return 0;
403}
404
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700405int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
406 const char* nexthop, bool legacy, uid_t uid) {
407 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
408}
409
410int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
411 const char* nexthop, bool legacy, uid_t uid) {
412 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
413}
414
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700415bool NetworkController::canProtect(uid_t uid) const {
416 android::RWLock::AutoRLock lock(mRWLock);
417 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
418 mProtectableUsers.find(uid) != mProtectableUsers.end();
419}
420
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700421void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
422 android::RWLock::AutoWLock lock(mRWLock);
423 mProtectableUsers.insert(uids.begin(), uids.end());
424}
425
426void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
427 android::RWLock::AutoWLock lock(mRWLock);
428 for (uid_t uid : uids) {
429 mProtectableUsers.erase(uid);
430 }
431}
432
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700433bool NetworkController::isValidNetwork(unsigned netId) const {
434 android::RWLock::AutoRLock lock(mRWLock);
435 return getNetworkLocked(netId);
436}
437
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700438Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700439 auto iter = mNetworks.find(netId);
440 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700441}
442
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700443VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
444 for (const auto& entry : mNetworks) {
445 if (entry.second->getType() == Network::VIRTUAL) {
446 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
447 if (virtualNetwork->appliesToUser(uid)) {
448 return virtualNetwork;
449 }
450 }
451 }
452 return NULL;
453}
454
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700455Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
456 auto iter = mUsers.find(uid);
457 if (iter != mUsers.end()) {
458 return iter->second;
459 }
460 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
461}
462
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700463bool NetworkController::canUserSelectNetworkLocked(uid_t uid, unsigned netId) const {
464 Network* network = getNetworkLocked(netId);
465 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
466 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
467 if (!network || uid == INVALID_UID) {
468 return false;
469 }
470 Permission userPermission = getPermissionForUserLocked(uid);
471 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
472 return true;
473 }
474 if (network->getType() == Network::VIRTUAL) {
475 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid);
476 }
477 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
478 if (virtualNetwork && virtualNetwork->isSecure() &&
479 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
480 return false;
481 }
482 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
483 return (userPermission & networkPermission) == networkPermission;
484}
485
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700486int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
487 const char* nexthop, bool add, bool legacy, uid_t uid) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700488 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700489 if (netId == NETID_UNSET || existingNetId != netId) {
490 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900491 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700492 }
493
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700494 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700495 if (netId == LOCAL_NET_ID) {
496 tableType = RouteController::LOCAL_NETWORK;
497 } else if (legacy) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700498 if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700499 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700500 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700501 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700502 }
503 } else {
504 tableType = RouteController::INTERFACE;
505 }
506
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700507 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
508 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700509}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700510
511int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
512 if (mDefaultNetId == NETID_UNSET) {
513 return 0;
514 }
515 Network* network = getNetworkLocked(mDefaultNetId);
516 if (!network || network->getType() != Network::PHYSICAL) {
517 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
518 return -ESRCH;
519 }
520 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
521 for (const auto& physicalInterface : network->getInterfaces()) {
522 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
523 add)) {
524 return ret;
525 }
526 }
527 return 0;
528}