blob: 8b1f84e57888dee3b64622e958f8488018b4fbc3 [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
50namespace {
51
52// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070053const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070054const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070055
56} // namespace
57
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070058const unsigned NetworkController::MIN_OEM_ID = 1;
59const unsigned NetworkController::MAX_OEM_ID = 50;
Lorenzo Colitti36679362015-02-25 10:26:19 +090060const unsigned NetworkController::DUMMY_NET_ID = 51;
61// NetIds 52..98 are reserved for future use.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070062const unsigned NetworkController::LOCAL_NET_ID = 99;
63
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070064// All calls to methods here are made while holding a write lock on mRWLock.
65class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
66public:
67 explicit DelegateImpl(NetworkController* networkController);
68 virtual ~DelegateImpl();
69
70 int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
71 Permission permission, bool add) WARN_UNUSED_RESULT;
72
73private:
74 int addFallthrough(const std::string& physicalInterface,
75 Permission permission) override WARN_UNUSED_RESULT;
76 int removeFallthrough(const std::string& physicalInterface,
77 Permission permission) override WARN_UNUSED_RESULT;
78
79 int modifyFallthrough(const std::string& physicalInterface, Permission permission,
80 bool add) WARN_UNUSED_RESULT;
81
82 NetworkController* const mNetworkController;
83};
84
85NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
86 mNetworkController(networkController) {
87}
88
89NetworkController::DelegateImpl::~DelegateImpl() {
90}
91
92int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
93 const std::string& physicalInterface,
94 Permission permission, bool add) {
95 if (add) {
96 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
97 physicalInterface.c_str(),
98 permission)) {
99 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
100 vpnNetId);
101 return ret;
102 }
103 } else {
104 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
105 physicalInterface.c_str(),
106 permission)) {
107 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
108 vpnNetId);
109 return ret;
110 }
111 }
112 return 0;
113}
114
115int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
116 Permission permission) {
117 return modifyFallthrough(physicalInterface, permission, true);
118}
119
120int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
121 Permission permission) {
122 return modifyFallthrough(physicalInterface, permission, false);
123}
124
125int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
126 Permission permission, bool add) {
127 for (const auto& entry : mNetworkController->mNetworks) {
128 if (entry.second->getType() == Network::VIRTUAL) {
129 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
130 return ret;
131 }
132 }
133 }
134 return 0;
135}
136
137NetworkController::NetworkController() :
138 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700139 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900140 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500141}
142
143unsigned NetworkController::getDefaultNetwork() const {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700144 android::RWLock::AutoRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500145 return mDefaultNetId;
146}
147
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700148int NetworkController::setDefaultNetwork(unsigned netId) {
149 android::RWLock::AutoWLock lock(mRWLock);
150
151 if (netId == mDefaultNetId) {
152 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700153 }
154
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700155 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700156 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900157 if (!network) {
158 ALOGE("no such netId %u", netId);
159 return -ENONET;
160 }
161 if (network->getType() != Network::PHYSICAL) {
162 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700163 return -EINVAL;
164 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700165 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700166 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700167 }
168 }
169
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700170 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700171 Network* network = getNetworkLocked(mDefaultNetId);
172 if (!network || network->getType() != Network::PHYSICAL) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700173 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
174 return -ESRCH;
175 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700176 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700177 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700178 }
179 }
180
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700181 mDefaultNetId = netId;
182 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500183}
184
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700185uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const {
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500186 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700187 Fwmark fwmark;
188 fwmark.protectedFromVpn = true;
189 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900190 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700191 // If a non-zero NetId was explicitly specified, and the user has permission for that
192 // network, use that network's DNS servers. Do not fall through to the default network even
193 // if the explicitly selected network is a split tunnel VPN or a VPN without DNS servers.
194 fwmark.explicitlySelected = true;
195 } else {
196 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
197 // (possibly falling through to the default network if the VPN doesn't provide a route to
198 // them). Otherwise, use the default network's DNS servers.
199 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
200 if (virtualNetwork && virtualNetwork->getHasDns()) {
201 *netId = virtualNetwork->getNetId();
202 } else {
203 *netId = mDefaultNetId;
204 }
205 }
206 fwmark.netId = *netId;
207 return fwmark.intValue;
208}
209
210// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
211// the VPN that applies to the UID if any; otherwise, the default network.
212unsigned NetworkController::getNetworkForUser(uid_t uid) const {
213 android::RWLock::AutoRLock lock(mRWLock);
214 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700215 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500216 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700217 return mDefaultNetId;
218}
219
220// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
221// applies to the user if any; otherwise, the default network.
222//
223// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
224// is a split-tunnel and disappears later, the socket continues working (since the default network's
225// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
226// high-priority routing rule that doesn't care what NetId the socket has.
227//
228// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
229// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
230// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
231// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
232// the fallthrough rules also go away), the socket that used to fallthrough to the default network
233// will stop working.
234unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
235 android::RWLock::AutoRLock lock(mRWLock);
236 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
237 if (virtualNetwork && !virtualNetwork->isSecure()) {
238 return virtualNetwork->getNetId();
239 }
240 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500241}
242
Erik Klinecea2d342015-06-25 18:24:46 +0900243void NetworkController::getNetworkContext(
244 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
245 struct android_net_context nc = {
246 .app_netid = netId,
247 .app_mark = MARK_UNSET,
248 .dns_netid = netId,
249 .dns_mark = MARK_UNSET,
250 .uid = uid,
251 };
252
Erik Kline492ca5b2016-03-09 14:56:00 +0900253 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
254 // client process. This value is nonzero iff.:
255 //
256 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
257 // - [Java] android.net.Network#getAllByName()
258 // - [C/++] android_getaddrinfofornetwork()
259 // 2. The app specified a netid/nethandle to be used as a process default via:
260 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
261 // - [C/++] android_setprocnetwork()
262 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
263 //
264 // In all these cases (with the possible exception of #3), the right thing to do is to treat
265 // such cases as explicitlySelected.
266 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
267 if (!explicitlySelected) {
Erik Klinecea2d342015-06-25 18:24:46 +0900268 nc.app_netid = getNetworkForConnect(uid);
269 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900270
Erik Klinecea2d342015-06-25 18:24:46 +0900271 Fwmark fwmark;
272 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900273 fwmark.explicitlySelected = explicitlySelected;
274 fwmark.protectedFromVpn = canProtect(uid);
275 fwmark.permission = getPermissionForUser(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900276 nc.app_mark = fwmark.intValue;
277
278 nc.dns_mark = getNetworkForDns(&(nc.dns_netid), uid);
279
280 if (netcontext) {
281 *netcontext = nc;
282 }
283}
284
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700285unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700286 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700287 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700288 if (entry.second->hasInterface(interface)) {
289 return entry.first;
290 }
291 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400292 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500293}
294
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700295bool NetworkController::isVirtualNetwork(unsigned netId) const {
296 android::RWLock::AutoRLock lock(mRWLock);
297 Network* network = getNetworkLocked(netId);
298 return network && network->getType() == Network::VIRTUAL;
299}
300
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700301int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700302 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
303 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400304 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900305 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700306 }
307
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700308 if (isValidNetwork(netId)) {
309 ALOGE("duplicate netId %u", netId);
310 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700311 }
312
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700313 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700314 if (int ret = physicalNetwork->setPermission(permission)) {
315 ALOGE("inconceivable! setPermission cannot fail on an empty network");
316 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900317 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700318 }
319
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700320 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700321 mNetworks[netId] = physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900322 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700323}
324
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700325int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700326 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700327 ALOGE("invalid netId %u", netId);
328 return -EINVAL;
329 }
330
331 if (isValidNetwork(netId)) {
332 ALOGE("duplicate netId %u", netId);
333 return -EEXIST;
334 }
335
336 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700337 if (int ret = modifyFallthroughLocked(netId, true)) {
338 return ret;
339 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700340 mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700341 return 0;
342}
343
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700344int NetworkController::destroyNetwork(unsigned netId) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900345 if (netId == LOCAL_NET_ID) {
346 ALOGE("cannot destroy local network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700347 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700348 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900349 if (!isValidNetwork(netId)) {
350 ALOGE("no such netId %u", netId);
351 return -ENONET;
352 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700353
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700354 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700355
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700356 android::RWLock::AutoWLock lock(mRWLock);
357 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900358
359 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
360 // other network code, ignore failures and attempt to clear out as much state as possible, even
361 // if we hit an error on the way. Return the first error that we see.
362 int ret = network->clearInterfaces();
363
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700364 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900365 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700366 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900367 if (!ret) {
368 ret = err;
369 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700370 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700371 mDefaultNetId = NETID_UNSET;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700372 } else if (network->getType() == Network::VIRTUAL) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900373 if (int err = modifyFallthroughLocked(netId, false)) {
374 if (!ret) {
375 ret = err;
376 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700377 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700378 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700379 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700380 delete network;
Sreeram Ramachandran3ced0692014-04-18 09:49:13 -0700381 _resolv_delete_cache_for_net(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900382 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700383}
384
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700385int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700386 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900387 ALOGE("no such netId %u", netId);
388 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700389 }
390
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700391 unsigned existingNetId = getNetworkForInterface(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700392 if (existingNetId != NETID_UNSET && existingNetId != netId) {
393 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
394 return -EBUSY;
395 }
396
397 android::RWLock::AutoWLock lock(mRWLock);
398 return getNetworkLocked(netId)->addInterface(interface);
399}
400
401int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
402 if (!isValidNetwork(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900403 ALOGE("no such netId %u", netId);
404 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700405 }
406
407 android::RWLock::AutoWLock lock(mRWLock);
408 return getNetworkLocked(netId)->removeInterface(interface);
409}
410
411Permission NetworkController::getPermissionForUser(uid_t uid) const {
412 android::RWLock::AutoRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700413 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700414}
415
416void NetworkController::setPermissionForUsers(Permission permission,
417 const std::vector<uid_t>& uids) {
418 android::RWLock::AutoWLock lock(mRWLock);
419 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700420 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700421 }
422}
423
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900424int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700425 android::RWLock::AutoRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900426 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700427}
428
429int NetworkController::setPermissionForNetworks(Permission permission,
430 const std::vector<unsigned>& netIds) {
431 android::RWLock::AutoWLock lock(mRWLock);
432 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700433 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900434 if (!network) {
435 ALOGE("no such netId %u", netId);
436 return -ENONET;
437 }
438 if (network->getType() != Network::PHYSICAL) {
439 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700440 return -EINVAL;
441 }
442
443 // TODO: ioctl(SIOCKILLADDR, ...) to kill socets on the network that don't have permission.
444
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700445 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700446 return ret;
447 }
448 }
449 return 0;
450}
451
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700452int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) {
453 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700454 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900455 if (!network) {
456 ALOGE("no such netId %u", netId);
457 return -ENONET;
458 }
459 if (network->getType() != Network::VIRTUAL) {
460 ALOGE("cannot add users to non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700461 return -EINVAL;
462 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900463 if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges, mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700464 return ret;
465 }
466 return 0;
467}
468
469int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) {
470 android::RWLock::AutoWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700471 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900472 if (!network) {
473 ALOGE("no such netId %u", netId);
474 return -ENONET;
475 }
476 if (network->getType() != Network::VIRTUAL) {
477 ALOGE("cannot remove users from non-virtual network with netId %u", netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700478 return -EINVAL;
479 }
Lorenzo Colittifff4bd32016-04-14 00:56:01 +0900480 if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges,
481 mProtectableUsers)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700482 return ret;
483 }
484 return 0;
485}
486
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700487int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
488 const char* nexthop, bool legacy, uid_t uid) {
489 return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid);
490}
491
492int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
493 const char* nexthop, bool legacy, uid_t uid) {
494 return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid);
495}
496
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700497bool NetworkController::canProtect(uid_t uid) const {
498 android::RWLock::AutoRLock lock(mRWLock);
499 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
500 mProtectableUsers.find(uid) != mProtectableUsers.end();
501}
502
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700503void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
504 android::RWLock::AutoWLock lock(mRWLock);
505 mProtectableUsers.insert(uids.begin(), uids.end());
506}
507
508void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
509 android::RWLock::AutoWLock lock(mRWLock);
510 for (uid_t uid : uids) {
511 mProtectableUsers.erase(uid);
512 }
513}
514
Erik Kline2d3a1632016-03-15 16:33:48 +0900515void NetworkController::dump(DumpWriter& dw) {
516 android::RWLock::AutoRLock lock(mRWLock);
517
518 dw.incIndent();
519 dw.println("NetworkController");
520
521 dw.incIndent();
522 dw.println("Default network: %u", mDefaultNetId);
523
524 dw.blankline();
525 dw.println("Networks:");
526 dw.incIndent();
527 for (const auto& i : mNetworks) {
528 dw.println(i.second->toString().c_str());
Pierre Imai3a272072016-04-19 16:17:07 +0900529 android::net::gCtls->resolverCtrl.dump(dw, i.first);
530 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900531 }
532 dw.decIndent();
533
534 dw.decIndent();
535
536 dw.decIndent();
537}
538
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700539bool NetworkController::isValidNetwork(unsigned netId) const {
540 android::RWLock::AutoRLock lock(mRWLock);
541 return getNetworkLocked(netId);
542}
543
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700544Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700545 auto iter = mNetworks.find(netId);
546 return iter == mNetworks.end() ? NULL : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700547}
548
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700549VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
550 for (const auto& entry : mNetworks) {
551 if (entry.second->getType() == Network::VIRTUAL) {
552 VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second);
553 if (virtualNetwork->appliesToUser(uid)) {
554 return virtualNetwork;
555 }
556 }
557 }
558 return NULL;
559}
560
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700561Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
562 auto iter = mUsers.find(uid);
563 if (iter != mUsers.end()) {
564 return iter->second;
565 }
566 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
567}
568
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900569int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700570 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900571 if (!network) {
572 return -ENONET;
573 }
574
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700575 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
576 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900577 if (uid == INVALID_UID) {
578 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700579 }
580 Permission userPermission = getPermissionForUserLocked(uid);
581 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900582 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700583 }
584 if (network->getType() == Network::VIRTUAL) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900585 return static_cast<VirtualNetwork*>(network)->appliesToUser(uid) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700586 }
587 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
588 if (virtualNetwork && virtualNetwork->isSecure() &&
589 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900590 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700591 }
592 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900593 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700594}
595
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700596int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
597 const char* nexthop, bool add, bool legacy, uid_t uid) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900598 if (!isValidNetwork(netId)) {
599 ALOGE("no such netId %u", netId);
600 return -ENONET;
601 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700602 unsigned existingNetId = getNetworkForInterface(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900603 if (existingNetId == NETID_UNSET) {
604 ALOGE("interface %s not assigned to any netId", interface);
605 return -ENODEV;
606 }
607 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700608 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900609 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700610 }
611
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700612 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700613 if (netId == LOCAL_NET_ID) {
614 tableType = RouteController::LOCAL_NETWORK;
615 } else if (legacy) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700616 if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700617 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700618 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700619 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700620 }
621 } else {
622 tableType = RouteController::INTERFACE;
623 }
624
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700625 return add ? RouteController::addRoute(interface, destination, nexthop, tableType) :
626 RouteController::removeRoute(interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700627}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700628
629int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
630 if (mDefaultNetId == NETID_UNSET) {
631 return 0;
632 }
633 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900634 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700635 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
636 return -ESRCH;
637 }
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900638 if (network->getType() != Network::PHYSICAL) {
639 ALOGE("inconceivable! default network must be a physical network");
640 return -EINVAL;
641 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700642 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
643 for (const auto& physicalInterface : network->getInterfaces()) {
644 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
645 add)) {
646 return ret;
647 }
648 }
649 return 0;
650}