blob: 8e4c69dfd84ef58563c289e3edd80eb9d6413a6d [file] [log] [blame]
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -05001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sreeram Ramachandran72604072014-05-21 13:19:43 -070017// THREAD-SAFETY
18// -------------
19// The methods in this file are called from multiple threads (from CommandListener, FwmarkServer
20// and DnsProxyListener). So, all accesses to shared state are guarded by a lock.
21//
22// In some cases, a single non-const method acquires and releases the lock several times, like so:
23// if (isValidNetwork(...)) { // isValidNetwork() acquires and releases the lock.
24// setDefaultNetwork(...); // setDefaultNetwork() also acquires and releases the lock.
25//
26// It might seem that this allows races where the state changes between the two statements, but in
27// fact there are no races because:
28// 1. This pattern only occurs in non-const methods (i.e., those that mutate state).
29// 2. Only CommandListener calls these non-const methods. The others call only const methods.
30// 3. CommandListener only processes one command at a time. I.e., it's serialized.
31// Thus, no other mutation can occur in between the two statements above.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050032
33#include "NetworkController.h"
34
Erik Kline2d3a1632016-03-15 16:33:48 +090035#define LOG_TAG "Netd"
36#include "log/log.h"
37
38#include "cutils/misc.h"
39#include "resolv_netid.h"
40
Pierre Imai3a272072016-04-19 16:17:07 +090041#include "Controllers.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090042#include "DummyNetwork.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090043#include "DumpWriter.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070044#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070045#include "LocalNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070046#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070047#include "RouteController.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070048#include "VirtualNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070049
Lorenzo Colitti7035f222017-02-13 18:29:00 +090050namespace android {
51namespace net {
52
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070053namespace {
54
55// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070056const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070057const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070058
59} // namespace
60
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070061const unsigned NetworkController::MIN_OEM_ID = 1;
62const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090063const unsigned NetworkController::DUMMY_NET_ID = 51;
64// NetIds 52..98 are reserved for future use.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070065const unsigned NetworkController::LOCAL_NET_ID = 99;
66
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070067// All calls to methods here are made while holding a write lock on mRWLock.
68class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
69public:
70 explicit DelegateImpl(NetworkController* networkController);
71 virtual ~DelegateImpl();
72
73 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
74 Permission permission, bool add) WARN_UNUSED_RESULT;
75
76private:
77 int addFallthrough(const std::string& physicalInterface,
78 Permission permission) override WARN_UNUSED_RESULT;
79 int removeFallthrough(const std::string& physicalInterface,
80 Permission permission) override WARN_UNUSED_RESULT;
81
82 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
83 bool add) WARN_UNUSED_RESULT;
84
85 NetworkController* const mNetworkController;
86};
87
88NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
89 mNetworkController(networkController) {
90}
91
92NetworkController::DelegateImpl::~DelegateImpl() {
93}
94
95int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
96 const std::string& physicalInterface,
97 Permission permission, bool add) {
98 if (add) {
99 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
100 physicalInterface.c_str(),
101 permission)) {
102 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
103 vpnNetId);
104 return ret;
105 }
106 } else {
107 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
108 physicalInterface.c_str(),
109 permission)) {
110 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
111 vpnNetId);
112 return ret;
113 }
114 }
115 return 0;
116}
117
118int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
119 Permission permission) {
120 return modifyFallthrough(physicalInterface, permission, true);
121}
122
123int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
124 Permission permission) {
125 return modifyFallthrough(physicalInterface, permission, false);
126}
127
128int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
129 Permission permission, bool add) {
130 for (const auto& entry : mNetworkController->mNetworks) {
131 if (entry.second->getType() == Network::VIRTUAL) {
132 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
133 return ret;
134 }
135 }
136 }
137 return 0;
138}
139
140NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900141 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
142 mProtectableUsers({AID_VPN}) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700143 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900144 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500145}
146
147unsigned NetworkController::getDefaultNetwork() const {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700148 android::RWLock::AutoRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500149 return mDefaultNetId;
150}
151
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700152int NetworkController::setDefaultNetwork(unsigned netId) {
153 android::RWLock::AutoWLock lock(mRWLock);
154
155 if (netId == mDefaultNetId) {
156 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700157 }
158
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700159 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700160 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900161 if (!network) {
162 ALOGE("no such netId %u", netId);
163 return -ENONET;
164 }
165 if (network->getType() != Network::PHYSICAL) {
166 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700167 return -EINVAL;
168 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700169 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700170 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700171 }
172 }
173
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700174 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700175 Network* network = getNetworkLocked(mDefaultNetId);
176 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700177 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
178 return -ESRCH;
179 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700180 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700181 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700182 }
183 }
184
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700185 mDefaultNetId = netId;
186 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500187}
188
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700189uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500190 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700191 Fwmark fwmark;
192 fwmark.protectedFromVpn = true;
193 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900194 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700195 // If a non-zero NetId was explicitly specified, and the user has permission for that
196 // network, use that network's DNS servers. Do not fall through to the default network even
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900197 // if the explicitly selected network is a split tunnel VPN: the explicitlySelected bit
198 // ensures that the VPN fallthrough rule does not match.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700199 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900200
201 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
202 // servers (through the default network). Otherwise, the query is guaranteed to fail.
203 // http://b/29498052
204 Network *network = getNetworkLocked(*netId);
205 if (network && network->getType() == Network::VIRTUAL &&
206 !static_cast<VirtualNetwork *>(network)->getHasDns()) {
207 *netId = mDefaultNetId;
208 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700209 } else {
210 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
211 // (possibly falling through to the default network if the VPN doesn't provide a route to
212 // them). Otherwise, use the default network's DNS servers.
213 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
214 if (virtualNetwork && virtualNetwork->getHasDns()) {
215 *netId = virtualNetwork->getNetId();
216 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900217 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
218 // http://b/27560555
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700219 *netId = mDefaultNetId;
220 }
221 }
222 fwmark.netId = *netId;
223 return fwmark.intValue;
224}
225
226// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
227// the VPN that applies to the UID if any; otherwise, the default network.
228unsigned NetworkController::getNetworkForUser(uid_t uid) const {
229 android::RWLock::AutoRLock lock(mRWLock);
230 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700231 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500232 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700233 return mDefaultNetId;
234}
235
236// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
237// applies to the user if any; otherwise, the default network.
238//
239// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
240// is a split-tunnel and disappears later, the socket continues working (since the default network's
241// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
242// high-priority routing rule that doesn't care what NetId the socket has.
243//
244// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
245// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
246// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
247// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
248// the fallthrough rules also go away), the socket that used to fallthrough to the default network
249// will stop working.
250unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
251 android::RWLock::AutoRLock lock(mRWLock);
252 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
253 if (virtualNetwork && !virtualNetwork->isSecure()) {
254 return virtualNetwork->getNetId();
255 }
256 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500257}
258
Erik Klinecea2d342015-06-25 18:24:46 +0900259void NetworkController::getNetworkContext(
260 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
261 struct android_net_context nc = {
262 .app_netid = netId,
263 .app_mark = MARK_UNSET,
264 .dns_netid = netId,
265 .dns_mark = MARK_UNSET,
266 .uid = uid,
267 };
268
Erik Kline492ca5b2016-03-09 14:56:00 +0900269 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
270 // client process. This value is nonzero iff.:
271 //
272 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
273 // - [Java] android.net.Network#getAllByName()
274 // - [C/++] android_getaddrinfofornetwork()
275 // 2. The app specified a netid/nethandle to be used as a process default via:
276 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
277 // - [C/++] android_setprocnetwork()
278 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
279 //
280 // In all these cases (with the possible exception of #3), the right thing to do is to treat
281 // such cases as explicitlySelected.
282 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
283 if (!explicitlySelected) {
Erik Klinecea2d342015-06-25 18:24:46 +0900284 nc.app_netid = getNetworkForConnect(uid);
285 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900286
Erik Klinecea2d342015-06-25 18:24:46 +0900287 Fwmark fwmark;
288 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900289 fwmark.explicitlySelected = explicitlySelected;
290 fwmark.protectedFromVpn = canProtect(uid);
291 fwmark.permission = getPermissionForUser(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900292 nc.app_mark = fwmark.intValue;
293
294 nc.dns_mark = getNetworkForDns(&(nc.dns_netid), uid);
295
296 if (netcontext) {
297 *netcontext = nc;
298 }
299}
300
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700301unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700302 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700303 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700304 if (entry.second->hasInterface(interface)) {
305 return entry.first;
306 }
307 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400308 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500309}
310
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700311bool NetworkController::isVirtualNetwork(unsigned netId) const {
312 android::RWLock::AutoRLock lock(mRWLock);
313 Network* network = getNetworkLocked(netId);
314 return network && network->getType() == Network::VIRTUAL;
315}
316
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700317int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700318 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
319 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400320 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900321 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700322 }
323
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700324 if (isValidNetwork(netId)) {
325 ALOGE("duplicate netId %u", netId);
326 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700327 }
328
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700329 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700330 if (int ret = physicalNetwork->setPermission(permission)) {
331 ALOGE("inconceivable! setPermission cannot fail on an empty network");
332 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900333 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700334 }
335
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700336 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700337 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900338 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700339}
340
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700341int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700342 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700343 ALOGE("invalid netId %u", netId);
344 return -EINVAL;
345 }
346
347 if (isValidNetwork(netId)) {
348 ALOGE("duplicate netId %u", netId);
349 return -EEXIST;
350 }
351
352 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700353 if (int ret = modifyFallthroughLocked(netId, true)) {
354 return ret;
355 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700356 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700357 return 0;
358}
359
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700360int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900361 if (netId == LOCAL_NET_ID) {
362 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700363 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700364 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900365 if (!isValidNetwork(netId)) {
366 ALOGE("no such netId %u", netId);
367 return -ENONET;
368 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700369
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700370 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700371
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700372 android::RWLock::AutoWLock lock(mRWLock);
373 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900374
375 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
376 // other network code, ignore failures and attempt to clear out as much state as possible, even
377 // if we hit an error on the way. Return the first error that we see.
378 int ret = network->clearInterfaces();
379
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700380 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900381 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700382 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900383 if (!ret) {
384 ret = err;
385 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700386 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700387 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700388 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900389 if (int err = modifyFallthroughLocked(netId, false)) {
390 if (!ret) {
391 ret = err;
392 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700393 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700394 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700395 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700396 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700397 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900398 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700399}
400
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700401int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700402 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900403 ALOGE("no such netId %u", netId);
404 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700405 }
406
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700407 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700408 if (existingNetId != NETID_UNSET && existingNetId != netId) {
409 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
410 return -EBUSY;
411 }
412
413 android::RWLock::AutoWLock lock(mRWLock);
414 return getNetworkLocked(netId)->addInterface(interface);
415}
416
417int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
418 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900419 ALOGE("no such netId %u", netId);
420 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700421 }
422
423 android::RWLock::AutoWLock lock(mRWLock);
424 return getNetworkLocked(netId)->removeInterface(interface);
425}
426
427Permission NetworkController::getPermissionForUser(uid_t uid) const {
428 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700429 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700430}
431
432void NetworkController::setPermissionForUsers(Permission permission,
433 const std::vector<uid_t>& uids) {
434 android::RWLock::AutoWLock lock(mRWLock);
435 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700436 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700437 }
438}
439
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900440int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700441 android::RWLock::AutoRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900442 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700443}
444
445int NetworkController::setPermissionForNetworks(Permission permission,
446 const std::vector<unsigned>& netIds) {
447 android::RWLock::AutoWLock lock(mRWLock);
448 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700449 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900450 if (!network) {
451 ALOGE("no such netId %u", netId);
452 return -ENONET;
453 }
454 if (network->getType() != Network::PHYSICAL) {
455 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700456 return -EINVAL;
457 }
458
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700459 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700460 return ret;
461 }
462 }
463 return 0;
464}
465
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700466int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
467 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700468 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900469 if (!network) {
470 ALOGE("no such netId %u", netId);
471 return -ENONET;
472 }
473 if (network->getType() != Network::VIRTUAL) {
474 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700475 return -EINVAL;
476 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900477 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700478 return ret;
479 }
480 return 0;
481}
482
483int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
484 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700485 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900486 if (!network) {
487 ALOGE("no such netId %u", netId);
488 return -ENONET;
489 }
490 if (network->getType() != Network::VIRTUAL) {
491 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700492 return -EINVAL;
493 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900494 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
495 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700496 return ret;
497 }
498 return 0;
499}
500
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700501int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
502 const char* nexthop, bool legacy, uid_t uid) {
503 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
504}
505
506int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
507 const char* nexthop, bool legacy, uid_t uid) {
508 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
509}
510
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700511bool NetworkController::canProtect(uid_t uid) const {
512 android::RWLock::AutoRLock lock(mRWLock);
513 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
514 mProtectableUsers.find(uid) != mProtectableUsers.end();
515}
516
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700517void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
518 android::RWLock::AutoWLock lock(mRWLock);
519 mProtectableUsers.insert(uids.begin(), uids.end());
520}
521
522void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
523 android::RWLock::AutoWLock lock(mRWLock);
524 for (uid_t uid : uids) {
525 mProtectableUsers.erase(uid);
526 }
527}
528
Erik Kline2d3a1632016-03-15 16:33:48 +0900529void NetworkController::dump(DumpWriter& dw) {
530 android::RWLock::AutoRLock lock(mRWLock);
531
532 dw.incIndent();
533 dw.println("NetworkController");
534
535 dw.incIndent();
536 dw.println("Default network: %u", mDefaultNetId);
537
538 dw.blankline();
539 dw.println("Networks:");
540 dw.incIndent();
541 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900542 Network* network = i.second;
543 dw.println(network->toString().c_str());
544 if (network->getType() == Network::PHYSICAL) {
545 dw.incIndent();
546 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
547 dw.println("Required permission: %s", permissionToName(permission));
548 dw.decIndent();
549 }
Pierre Imai3a272072016-04-19 16:17:07 +0900550 android::net::gCtls->resolverCtrl.dump(dw, i.first);
551 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900552 }
553 dw.decIndent();
554
555 dw.decIndent();
556
557 dw.decIndent();
558}
559
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700560bool NetworkController::isValidNetwork(unsigned netId) const {
561 android::RWLock::AutoRLock lock(mRWLock);
562 return getNetworkLocked(netId);
563}
564
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700565Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700566 auto iter = mNetworks.find(netId);
567 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700568}
569
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700570VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
571 for (const auto& entry : mNetworks) {
572 if (entry.second->getType() == Network::VIRTUAL) {
573 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
574 if (virtualNetwork->appliesToUser(uid)) {
575 return virtualNetwork;
576 }
577 }
578 }
579 return NULL;
580}
581
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700582Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
583 auto iter = mUsers.find(uid);
584 if (iter != mUsers.end()) {
585 return iter->second;
586 }
587 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
588}
589
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900590int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700591 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900592 if (!network) {
593 return -ENONET;
594 }
595
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700596 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
597 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900598 if (uid == INVALID_UID) {
599 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700600 }
601 Permission userPermission = getPermissionForUserLocked(uid);
602 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900603 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700604 }
605 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900606 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700607 }
608 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
609 if (virtualNetwork && virtualNetwork->isSecure() &&
610 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900611 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700612 }
613 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900614 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700615}
616
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700617int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
618 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900619 if (!isValidNetwork(netId)) {
620 ALOGE("no such netId %u", netId);
621 return -ENONET;
622 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700623 unsigned existingNetId = getNetworkForInterface(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900624 if (existingNetId == NETID_UNSET) {
625 ALOGE("interface %s not assigned to any netId", interface);
626 return -ENODEV;
627 }
628 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700629 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900630 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700631 }
632
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700633 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700634 if (netId == LOCAL_NET_ID) {
635 tableType = RouteController::LOCAL_NETWORK;
636 } else if (legacy) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700637 if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700638 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700639 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700640 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700641 }
642 } else {
643 tableType = RouteController::INTERFACE;
644 }
645
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700646 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
647 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700648}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700649
650int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
651 if (mDefaultNetId == NETID_UNSET) {
652 return 0;
653 }
654 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900655 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700656 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
657 return -ESRCH;
658 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900659 if (network->getType() != Network::PHYSICAL) {
660 ALOGE("inconceivable! default network must be a physical network");
661 return -EINVAL;
662 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700663 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
664 for (const auto& physicalInterface : network->getInterfaces()) {
665 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
666 add)) {
667 return ret;
668 }
669 }
670 return 0;
671}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900672
673} // namespace net
674} // namespace android