blob: 76e4a6af8c6a71cfe2dab733c93014edf0974779 [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
Lorenzo Colitti36679362015-02-25 10:26:19 +090035#include "DummyNetwork.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070036#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070037#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070038#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070039#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070040#include "VirtualNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070041
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -070042#include "cutils/misc.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070043#define LOG_TAG "Netd"
44#include "log/log.h"
Sreeram Ramachandran72604072014-05-21 13:19:43 -070045#include "resolv_netid.h"
46
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070047namespace {
48
49// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070050const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070051const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070052
53} // namespace
54
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070055const unsigned NetworkController::MIN_OEM_ID = 1;
56const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090057const unsigned NetworkController::DUMMY_NET_ID = 51;
58// NetIds 52..98 are reserved for future use.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070059const unsigned NetworkController::LOCAL_NET_ID = 99;
60
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070061// All calls to methods here are made while holding a write lock on mRWLock.
62class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
63public:
64 explicit DelegateImpl(NetworkController* networkController);
65 virtual ~DelegateImpl();
66
67 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
68 Permission permission, bool add) WARN_UNUSED_RESULT;
69
70private:
71 int addFallthrough(const std::string& physicalInterface,
72 Permission permission) override WARN_UNUSED_RESULT;
73 int removeFallthrough(const std::string& physicalInterface,
74 Permission permission) override WARN_UNUSED_RESULT;
75
76 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
77 bool add) WARN_UNUSED_RESULT;
78
79 NetworkController* const mNetworkController;
80};
81
82NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
83 mNetworkController(networkController) {
84}
85
86NetworkController::DelegateImpl::~DelegateImpl() {
87}
88
89int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
90 const std::string& physicalInterface,
91 Permission permission, bool add) {
92 if (add) {
93 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
94 physicalInterface.c_str(),
95 permission)) {
96 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
97 vpnNetId);
98 return ret;
99 }
100 } else {
101 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
102 physicalInterface.c_str(),
103 permission)) {
104 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
105 vpnNetId);
106 return ret;
107 }
108 }
109 return 0;
110}
111
112int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
113 Permission permission) {
114 return modifyFallthrough(physicalInterface, permission, true);
115}
116
117int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
118 Permission permission) {
119 return modifyFallthrough(physicalInterface, permission, false);
120}
121
122int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
123 Permission permission, bool add) {
124 for (const auto& entry : mNetworkController->mNetworks) {
125 if (entry.second->getType() == Network::VIRTUAL) {
126 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
127 return ret;
128 }
129 }
130 }
131 return 0;
132}
133
134NetworkController::NetworkController() :
135 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700136 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900137 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500138}
139
140unsigned NetworkController::getDefaultNetwork() const {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700141 android::RWLock::AutoRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500142 return mDefaultNetId;
143}
144
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700145int NetworkController::setDefaultNetwork(unsigned netId) {
146 android::RWLock::AutoWLock lock(mRWLock);
147
148 if (netId == mDefaultNetId) {
149 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700150 }
151
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700152 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700153 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900154 if (!network) {
155 ALOGE("no such netId %u", netId);
156 return -ENONET;
157 }
158 if (network->getType() != Network::PHYSICAL) {
159 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700160 return -EINVAL;
161 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700162 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700163 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700164 }
165 }
166
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700167 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700168 Network* network = getNetworkLocked(mDefaultNetId);
169 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700170 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
171 return -ESRCH;
172 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700173 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700174 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700175 }
176 }
177
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700178 mDefaultNetId = netId;
179 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500180}
181
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700182uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500183 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700184 Fwmark fwmark;
185 fwmark.protectedFromVpn = true;
186 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900187 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700188 // If a non-zero NetId was explicitly specified, and the user has permission for that
189 // network, use that network's DNS servers. Do not fall through to the default network even
190 // if the explicitly selected network is a split tunnel VPN or a VPN without DNS servers.
191 fwmark.explicitlySelected = true;
192 } else {
193 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
194 // (possibly falling through to the default network if the VPN doesn't provide a route to
195 // them). Otherwise, use the default network's DNS servers.
196 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
197 if (virtualNetwork && virtualNetwork->getHasDns()) {
198 *netId = virtualNetwork->getNetId();
199 } else {
200 *netId = mDefaultNetId;
201 }
202 }
203 fwmark.netId = *netId;
204 return fwmark.intValue;
205}
206
207// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
208// the VPN that applies to the UID if any; otherwise, the default network.
209unsigned NetworkController::getNetworkForUser(uid_t uid) const {
210 android::RWLock::AutoRLock lock(mRWLock);
211 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700212 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500213 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700214 return mDefaultNetId;
215}
216
217// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
218// applies to the user if any; otherwise, the default network.
219//
220// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
221// is a split-tunnel and disappears later, the socket continues working (since the default network's
222// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
223// high-priority routing rule that doesn't care what NetId the socket has.
224//
225// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
226// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
227// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
228// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
229// the fallthrough rules also go away), the socket that used to fallthrough to the default network
230// will stop working.
231unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
232 android::RWLock::AutoRLock lock(mRWLock);
233 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
234 if (virtualNetwork && !virtualNetwork->isSecure()) {
235 return virtualNetwork->getNetId();
236 }
237 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500238}
239
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700240unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700241 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700242 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700243 if (entry.second->hasInterface(interface)) {
244 return entry.first;
245 }
246 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400247 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500248}
249
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700250bool NetworkController::isVirtualNetwork(unsigned netId) const {
251 android::RWLock::AutoRLock lock(mRWLock);
252 Network* network = getNetworkLocked(netId);
253 return network && network->getType() == Network::VIRTUAL;
254}
255
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700256int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700257 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
258 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400259 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900260 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700261 }
262
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700263 if (isValidNetwork(netId)) {
264 ALOGE("duplicate netId %u", netId);
265 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700266 }
267
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700268 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700269 if (int ret = physicalNetwork->setPermission(permission)) {
270 ALOGE("inconceivable! setPermission cannot fail on an empty network");
271 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900272 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700273 }
274
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700275 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700276 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900277 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700278}
279
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700280int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700281 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700282 ALOGE("invalid netId %u", netId);
283 return -EINVAL;
284 }
285
286 if (isValidNetwork(netId)) {
287 ALOGE("duplicate netId %u", netId);
288 return -EEXIST;
289 }
290
291 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700292 if (int ret = modifyFallthroughLocked(netId, true)) {
293 return ret;
294 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700295 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700296 return 0;
297}
298
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700299int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900300 if (netId == LOCAL_NET_ID) {
301 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700302 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700303 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900304 if (!isValidNetwork(netId)) {
305 ALOGE("no such netId %u", netId);
306 return -ENONET;
307 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700308
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700309 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700310
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700311 android::RWLock::AutoWLock lock(mRWLock);
312 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900313
314 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
315 // other network code, ignore failures and attempt to clear out as much state as possible, even
316 // if we hit an error on the way. Return the first error that we see.
317 int ret = network->clearInterfaces();
318
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700319 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900320 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700321 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900322 if (!ret) {
323 ret = err;
324 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700325 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700326 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700327 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900328 if (int err = modifyFallthroughLocked(netId, false)) {
329 if (!ret) {
330 ret = err;
331 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700332 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700333 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700334 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700335 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700336 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900337 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700338}
339
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700340int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700341 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900342 ALOGE("no such netId %u", netId);
343 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700344 }
345
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700346 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700347 if (existingNetId != NETID_UNSET && existingNetId != netId) {
348 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
349 return -EBUSY;
350 }
351
352 android::RWLock::AutoWLock lock(mRWLock);
353 return getNetworkLocked(netId)->addInterface(interface);
354}
355
356int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
357 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900358 ALOGE("no such netId %u", netId);
359 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700360 }
361
362 android::RWLock::AutoWLock lock(mRWLock);
363 return getNetworkLocked(netId)->removeInterface(interface);
364}
365
366Permission NetworkController::getPermissionForUser(uid_t uid) const {
367 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700368 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700369}
370
371void NetworkController::setPermissionForUsers(Permission permission,
372 const std::vector<uid_t>& uids) {
373 android::RWLock::AutoWLock lock(mRWLock);
374 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700375 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700376 }
377}
378
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900379int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700380 android::RWLock::AutoRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900381 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700382}
383
384int NetworkController::setPermissionForNetworks(Permission permission,
385 const std::vector<unsigned>& netIds) {
386 android::RWLock::AutoWLock lock(mRWLock);
387 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700388 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900389 if (!network) {
390 ALOGE("no such netId %u", netId);
391 return -ENONET;
392 }
393 if (network->getType() != Network::PHYSICAL) {
394 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700395 return -EINVAL;
396 }
397
398 // TODO: ioctl(SIOCKILLADDR, ...) to kill socets on the network that don't have permission.
399
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700400 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700401 return ret;
402 }
403 }
404 return 0;
405}
406
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700407int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
408 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700409 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900410 if (!network) {
411 ALOGE("no such netId %u", netId);
412 return -ENONET;
413 }
414 if (network->getType() != Network::VIRTUAL) {
415 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700416 return -EINVAL;
417 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700418 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700419 return ret;
420 }
421 return 0;
422}
423
424int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
425 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700426 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900427 if (!network) {
428 ALOGE("no such netId %u", netId);
429 return -ENONET;
430 }
431 if (network->getType() != Network::VIRTUAL) {
432 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700433 return -EINVAL;
434 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700435 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700436 return ret;
437 }
438 return 0;
439}
440
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700441int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
442 const char* nexthop, bool legacy, uid_t uid) {
443 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
444}
445
446int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
447 const char* nexthop, bool legacy, uid_t uid) {
448 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
449}
450
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700451bool NetworkController::canProtect(uid_t uid) const {
452 android::RWLock::AutoRLock lock(mRWLock);
453 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
454 mProtectableUsers.find(uid) != mProtectableUsers.end();
455}
456
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700457void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
458 android::RWLock::AutoWLock lock(mRWLock);
459 mProtectableUsers.insert(uids.begin(), uids.end());
460}
461
462void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
463 android::RWLock::AutoWLock lock(mRWLock);
464 for (uid_t uid : uids) {
465 mProtectableUsers.erase(uid);
466 }
467}
468
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700469bool NetworkController::isValidNetwork(unsigned netId) const {
470 android::RWLock::AutoRLock lock(mRWLock);
471 return getNetworkLocked(netId);
472}
473
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700474Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700475 auto iter = mNetworks.find(netId);
476 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700477}
478
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700479VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
480 for (const auto& entry : mNetworks) {
481 if (entry.second->getType() == Network::VIRTUAL) {
482 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
483 if (virtualNetwork->appliesToUser(uid)) {
484 return virtualNetwork;
485 }
486 }
487 }
488 return NULL;
489}
490
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700491Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
492 auto iter = mUsers.find(uid);
493 if (iter != mUsers.end()) {
494 return iter->second;
495 }
496 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
497}
498
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900499int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700500 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900501 if (!network) {
502 return -ENONET;
503 }
504
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700505 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
506 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900507 if (uid == INVALID_UID) {
508 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700509 }
510 Permission userPermission = getPermissionForUserLocked(uid);
511 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900512 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700513 }
514 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900515 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700516 }
517 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
518 if (virtualNetwork && virtualNetwork->isSecure() &&
519 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900520 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700521 }
522 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900523 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700524}
525
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700526int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
527 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900528 if (!isValidNetwork(netId)) {
529 ALOGE("no such netId %u", netId);
530 return -ENONET;
531 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700532 unsigned existingNetId = getNetworkForInterface(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900533 if (existingNetId == NETID_UNSET) {
534 ALOGE("interface %s not assigned to any netId", interface);
535 return -ENODEV;
536 }
537 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700538 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900539 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700540 }
541
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700542 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700543 if (netId == LOCAL_NET_ID) {
544 tableType = RouteController::LOCAL_NETWORK;
545 } else if (legacy) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700546 if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700547 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700548 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700549 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700550 }
551 } else {
552 tableType = RouteController::INTERFACE;
553 }
554
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700555 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
556 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700557}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700558
559int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
560 if (mDefaultNetId == NETID_UNSET) {
561 return 0;
562 }
563 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900564 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700565 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
566 return -ESRCH;
567 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900568 if (network->getType() != Network::PHYSICAL) {
569 ALOGE("inconceivable! default network must be a physical network");
570 return -EINVAL;
571 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700572 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
573 for (const auto& physicalInterface : network->getInterfaces()) {
574 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
575 add)) {
576 return ret;
577 }
578 }
579 return 0;
580}