blob: 602639cb1f2c34b95505e64fe831afbe48125f4d [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//
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +090022// Public functions accessible by external callers should be thread-safe and are responsible for
23// acquiring the lock. Private functions in this file should call xxxLocked() methods and access
24// internal state directly.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050025
Erik Kline2d3a1632016-03-15 16:33:48 +090026#define LOG_TAG "Netd"
Luke Huangcfd04b22019-03-18 15:53:21 +080027
28#include "NetworkController.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090029
Rubin Xu6c00b612018-04-27 14:27:59 +010030#include <android-base/strings.h>
Bernie Innocenti34de3ba2019-02-19 18:08:36 +090031#include <cutils/misc.h> // FIRST_APPLICATION_UID
Bernie Innocenti189eb502018-10-01 23:10:18 +090032#include <netd_resolv/resolv.h>
Luke Huangcfd04b22019-03-18 15:53:21 +080033#include "log/log.h"
Rubin Xu6c00b612018-04-27 14:27:59 +010034
Pierre Imai3a272072016-04-19 16:17:07 +090035#include "Controllers.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090036#include "DummyNetwork.h"
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070037#include "Fwmark.h"
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070038#include "LocalNetwork.h"
Hungming Chen228cffa2020-02-18 17:11:18 +080039#include "OffloadUtils.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070040#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070041#include "RouteController.h"
Ken Chen4e8ef9b2021-03-17 01:57:19 +080042#include "UnreachableNetwork.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070043#include "VirtualNetwork.h"
Luke Huangb257d612019-03-14 21:19:13 +080044#include "netdutils/DumpWriter.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090045#include "netid_client.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070046
Erik Kline6d4669f2017-05-25 17:03:31 +090047#define DBG 0
48
Luke Huangb257d612019-03-14 21:19:13 +080049using android::netdutils::DumpWriter;
50
Bernie Innocenti762dcf42019-06-14 19:52:49 +090051namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090052
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 Ramachandran48e19b02014-07-22 22:23:20 -070061// All calls to methods here are made while holding a write lock on mRWLock.
Lorenzo Colitti2d014e72018-01-10 22:12:29 +090062// They are mostly not called directly from this class, but from methods in PhysicalNetwork.cpp.
63// However, we're the only user of that class, so all calls to those methods come from here and are
64// made under lock.
65// For example, PhysicalNetwork::setPermission ends up calling addFallthrough and removeFallthrough,
66// but it's only called from here under lock (specifically, from createPhysicalNetworkLocked and
67// setPermissionForNetworks).
68// TODO: use std::mutex and GUARDED_BY instead of manual inspection.
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070069class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
Bernie Innocenti762dcf42019-06-14 19:52:49 +090070 public:
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070071 explicit DelegateImpl(NetworkController* networkController);
72 virtual ~DelegateImpl();
73
Bernie Innocenti762dcf42019-06-14 19:52:49 +090074 [[nodiscard]] int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
75 Permission permission, bool add);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070076
Bernie Innocenti762dcf42019-06-14 19:52:49 +090077 private:
78 [[nodiscard]] int addFallthrough(const std::string& physicalInterface,
79 Permission permission) override;
80 [[nodiscard]] int removeFallthrough(const std::string& physicalInterface,
81 Permission permission) override;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070082
Bernie Innocenti762dcf42019-06-14 19:52:49 +090083 [[nodiscard]] int modifyFallthrough(const std::string& physicalInterface, Permission permission,
84 bool add);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070085
86 NetworkController* const mNetworkController;
87};
88
89NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
90 mNetworkController(networkController) {
91}
92
93NetworkController::DelegateImpl::~DelegateImpl() {
94}
95
96int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
97 const std::string& physicalInterface,
98 Permission permission, bool add) {
99 if (add) {
100 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
101 physicalInterface.c_str(),
102 permission)) {
103 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
104 vpnNetId);
105 return ret;
106 }
107 } else {
108 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
109 physicalInterface.c_str(),
110 permission)) {
111 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
112 vpnNetId);
113 return ret;
114 }
115 }
116 return 0;
117}
118
119int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
120 Permission permission) {
121 return modifyFallthrough(physicalInterface, permission, true);
122}
123
124int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
125 Permission permission) {
126 return modifyFallthrough(physicalInterface, permission, false);
127}
128
129int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
130 Permission permission, bool add) {
131 for (const auto& entry : mNetworkController->mNetworks) {
Ken Chen7ad712f2021-01-05 01:14:20 +0800132 if (entry.second->isVirtual()) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700133 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
134 return ret;
135 }
136 }
137 }
138 return 0;
139}
140
141NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900142 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
143 mProtectableUsers({AID_VPN}) {
Ken Chen66e860f2021-04-01 15:50:28 +0800144 gLog.info("enter NetworkController ctor");
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700145 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900146 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800147 mNetworks[UNREACHABLE_NET_ID] = new UnreachableNetwork(UNREACHABLE_NET_ID);
Hungming Chen228cffa2020-02-18 17:11:18 +0800148
149 // Clear all clsact stubs on all interfaces.
150 // TODO: perhaps only remove the clsact on the interface which is added by
151 // RouteController::addInterfaceToPhysicalNetwork. Currently, the netd only
152 // attach the clsact to the interface for the physical network.
Maciej Żenczykowski0e5d26f2021-01-17 03:14:20 -0800153 const auto& ifaces = InterfaceController::getIfaceNames();
154 if (isOk(ifaces)) {
155 for (const std::string& iface : ifaces.value()) {
156 if (int ifIndex = if_nametoindex(iface.c_str())) {
157 // Ignore the error because the interface might not have a clsact.
158 tcQdiscDelDevClsact(ifIndex);
Hungming Chen228cffa2020-02-18 17:11:18 +0800159 }
160 }
161 }
Ken Chen66e860f2021-04-01 15:50:28 +0800162 gLog.info("leave NetworkController ctor");
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500163}
164
165unsigned NetworkController::getDefaultNetwork() const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800166 ScopedRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500167 return mDefaultNetId;
168}
169
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700170int NetworkController::setDefaultNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800171 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700172
173 if (netId == mDefaultNetId) {
174 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700175 }
176
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700177 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700178 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900179 if (!network) {
180 ALOGE("no such netId %u", netId);
181 return -ENONET;
182 }
Ken Chen7ad712f2021-01-05 01:14:20 +0800183 if (!network->isPhysical()) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900184 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700185 return -EINVAL;
186 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700187 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700188 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700189 }
190 }
191
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700192 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700193 Network* network = getNetworkLocked(mDefaultNetId);
Ken Chen7ad712f2021-01-05 01:14:20 +0800194 if (!network || !network->isPhysical()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700195 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
196 return -ESRCH;
197 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700198 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700199 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700200 }
201 }
202
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700203 mDefaultNetId = netId;
204 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500205}
206
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900207uint32_t NetworkController::getNetworkForDnsLocked(unsigned* netId, uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700208 Fwmark fwmark;
209 fwmark.protectedFromVpn = true;
210 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900211
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800212 Network* appDefaultNetwork = getPhysicalOrUnreachableNetworkForUserLocked(uid);
Ken Chen8738e1c2020-11-24 11:38:54 +0800213 unsigned defaultNetId = appDefaultNetwork ? appDefaultNetwork->getNetId() : mDefaultNetId;
214
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900215 // Common case: there is no VPN that applies to the user, and the query did not specify a netId.
216 // Therefore, it is safe to set the explicit bit on this query and skip all the complex logic
217 // below. While this looks like a special case, it is actually the one that handles the vast
218 // majority of DNS queries.
219 // TODO: untangle this code.
220 if (*netId == NETID_UNSET && getVirtualNetworkForUserLocked(uid) == nullptr) {
Ken Chen8738e1c2020-11-24 11:38:54 +0800221 *netId = defaultNetId;
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900222 fwmark.netId = *netId;
223 fwmark.explicitlySelected = true;
224 return fwmark.intValue;
225 }
226
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900227 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700228 // If a non-zero NetId was explicitly specified, and the user has permission for that
Luke Huangd2861982019-05-17 19:47:28 +0800229 // network, use that network's DNS servers. (possibly falling through the to the default
230 // network if the VPN doesn't provide a route to them).
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700231 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900232
233 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
234 // servers (through the default network). Otherwise, the query is guaranteed to fail.
235 // http://b/29498052
236 Network *network = getNetworkLocked(*netId);
Ken Chen7ad712f2021-01-05 01:14:20 +0800237 if (network && network->isVirtual() && !resolv_has_nameservers(*netId)) {
Ken Chen8738e1c2020-11-24 11:38:54 +0800238 *netId = defaultNetId;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900239 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700240 } else {
241 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
242 // (possibly falling through to the default network if the VPN doesn't provide a route to
Luke Huangd2861982019-05-17 19:47:28 +0800243 // them). Otherwise, use the default network's DNS servers.
244 // TODO: Consider if we should set the explicit bit here.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700245 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
Jooyung Han3e64aa12019-11-27 15:36:29 +0900246 if (virtualNetwork && resolv_has_nameservers(virtualNetwork->getNetId())) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700247 *netId = virtualNetwork->getNetId();
248 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900249 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
250 // http://b/27560555
Ken Chen8738e1c2020-11-24 11:38:54 +0800251 *netId = defaultNetId;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700252 }
253 }
254 fwmark.netId = *netId;
255 return fwmark.intValue;
256}
257
258// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
Ken Chen55db3802021-03-30 13:47:49 +0800259// the VPN that applies to the UID if any; Otherwise, the default network for UID; Otherwise the
260// unreachable network that applies to the UID; lastly, the default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700261unsigned NetworkController::getNetworkForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800262 ScopedRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700263 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700264 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500265 }
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800266 if (Network* network = getPhysicalOrUnreachableNetworkForUserLocked(uid)) {
267 return network->getNetId();
Ken Chen8738e1c2020-11-24 11:38:54 +0800268 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700269 return mDefaultNetId;
270}
271
272// Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that
Ken Chen8738e1c2020-11-24 11:38:54 +0800273// applies to the user if any; otherwise, the default network that applies to user if any; lastly,
274// the default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700275//
276// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
277// is a split-tunnel and disappears later, the socket continues working (since the default network's
278// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
279// high-priority routing rule that doesn't care what NetId the socket has.
280//
281// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
282// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
283// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
284// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
285// the fallthrough rules also go away), the socket that used to fallthrough to the default network
286// will stop working.
Ken Chen8738e1c2020-11-24 11:38:54 +0800287//
288// Per-app physical default networks behave the same as bypassable VPNs: when a socket is connected
289// on one of these networks, we mark the socket with the netId of the network. This ensures that if
290// the per-app default network changes, sockets established on the previous network are still
291// routed to that network, assuming the network's UID ranges still apply to the UID. While this
292// means that fallthrough to the default network does not work, physical networks not expected
293// ever to be split tunnels.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900294unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700295 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
296 if (virtualNetwork && !virtualNetwork->isSecure()) {
297 return virtualNetwork->getNetId();
298 }
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800299 if (Network* network = getPhysicalOrUnreachableNetworkForUserLocked(uid)) {
300 return network->getNetId();
Ken Chen8738e1c2020-11-24 11:38:54 +0800301 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700302 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500303}
304
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900305unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800306 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900307 return getNetworkForConnectLocked(uid);
308}
309
Erik Klinecea2d342015-06-25 18:24:46 +0900310void NetworkController::getNetworkContext(
311 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800312 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900313
Erik Klinecea2d342015-06-25 18:24:46 +0900314 struct android_net_context nc = {
315 .app_netid = netId,
316 .app_mark = MARK_UNSET,
317 .dns_netid = netId,
318 .dns_mark = MARK_UNSET,
319 .uid = uid,
320 };
321
Erik Kline492ca5b2016-03-09 14:56:00 +0900322 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
323 // client process. This value is nonzero iff.:
324 //
325 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
326 // - [Java] android.net.Network#getAllByName()
327 // - [C/++] android_getaddrinfofornetwork()
328 // 2. The app specified a netid/nethandle to be used as a process default via:
329 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
330 // - [C/++] android_setprocnetwork()
331 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
332 //
333 // In all these cases (with the possible exception of #3), the right thing to do is to treat
334 // such cases as explicitlySelected.
335 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
336 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900337 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900338 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900339
Erik Klinecea2d342015-06-25 18:24:46 +0900340 Fwmark fwmark;
341 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900342 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900343 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
344 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900345 nc.app_mark = fwmark.intValue;
346
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900347 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900348
Erik Kline6d4669f2017-05-25 17:03:31 +0900349 if (DBG) {
350 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
351 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
352 }
353
Erik Klinecea2d342015-06-25 18:24:46 +0900354 if (netcontext) {
355 *netcontext = nc;
356 }
357}
358
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900359unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700360 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700361 if (entry.second->hasInterface(interface)) {
362 return entry.first;
363 }
364 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400365 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500366}
367
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900368unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800369 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900370 return getNetworkForInterfaceLocked(interface);
371}
372
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700373bool NetworkController::isVirtualNetwork(unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800374 ScopedRLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100375 return isVirtualNetworkLocked(netId);
376}
377
378bool NetworkController::isVirtualNetworkLocked(unsigned netId) const {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700379 Network* network = getNetworkLocked(netId);
Ken Chen7ad712f2021-01-05 01:14:20 +0800380 return network && network->isVirtual();
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700381}
382
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700383int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700384 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
385 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400386 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900387 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700388 }
389
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700390 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700391 ALOGE("duplicate netId %u", netId);
392 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700393 }
394
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700395 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700396 if (int ret = physicalNetwork->setPermission(permission)) {
397 ALOGE("inconceivable! setPermission cannot fail on an empty network");
398 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900399 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700400 }
401
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700402 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900403
404 updateTcpSocketMonitorPolling();
405
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900406 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700407}
408
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700409int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800410 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700411 return createPhysicalNetworkLocked(netId, permission);
412}
413
414int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700415 if (pNetId == nullptr) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700416 return -EINVAL;
417 }
418
Luke Huangd1ee4622018-06-29 13:49:58 +0800419 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700420 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
421 if (!isValidNetworkLocked(*pNetId)) {
422 break;
423 }
424 }
425
426 if (*pNetId > MAX_OEM_ID) {
427 ALOGE("No free network ID");
428 *pNetId = 0;
429 return -ENONET;
430 }
431
432 int ret = createPhysicalNetworkLocked(*pNetId, permission);
433 if (ret) {
434 *pNetId = 0;
435 }
436
437 return ret;
438}
439
Ken Chenab5f3472021-04-04 11:28:06 +0800440int NetworkController::createVirtualNetwork(unsigned netId, bool secure, NativeVpnType vpnType) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800441 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900442
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700443 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700444 ALOGE("invalid netId %u", netId);
445 return -EINVAL;
446 }
447
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900448 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700449 ALOGE("duplicate netId %u", netId);
450 return -EEXIST;
451 }
452
Ken Chenab5f3472021-04-04 11:28:06 +0800453 if (vpnType < NativeVpnType::SERVICE || NativeVpnType::OEM < vpnType) {
454 ALOGE("invalid vpnType %d", static_cast<int>(vpnType));
455 return -EINVAL;
456 }
457
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700458 if (int ret = modifyFallthroughLocked(netId, true)) {
459 return ret;
460 }
cken67cd14c2018-12-05 17:26:59 +0900461 mNetworks[netId] = new VirtualNetwork(netId, secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700462 return 0;
463}
464
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700465int NetworkController::destroyNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800466 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900467
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800468 if (netId == LOCAL_NET_ID || netId == UNREACHABLE_NET_ID) {
469 ALOGE("cannot destroy local or unreachable network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700470 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700471 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900472 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900473 ALOGE("no such netId %u", netId);
474 return -ENONET;
475 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700476
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700477 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700478
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700479 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900480
481 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
482 // other network code, ignore failures and attempt to clear out as much state as possible, even
483 // if we hit an error on the way. Return the first error that we see.
484 int ret = network->clearInterfaces();
485
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700486 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900487 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700488 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900489 if (!ret) {
490 ret = err;
491 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700492 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700493 mDefaultNetId = NETID_UNSET;
Ken Chen7ad712f2021-01-05 01:14:20 +0800494 } else if (network->isVirtual()) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900495 if (int err = modifyFallthroughLocked(netId, false)) {
496 if (!ret) {
497 ret = err;
498 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700499 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700500 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700501 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700502 delete network;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900503
Rubin Xu6c00b612018-04-27 14:27:59 +0100504 for (auto iter = mIfindexToLastNetId.begin(); iter != mIfindexToLastNetId.end();) {
505 if (iter->second == netId) {
506 iter = mIfindexToLastNetId.erase(iter);
507 } else {
508 ++iter;
509 }
510 }
511
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900512 updateTcpSocketMonitorPolling();
513
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900514 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700515}
516
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700517int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800518 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900519
520 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900521 ALOGE("no such netId %u", netId);
522 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700523 }
524
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900525 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700526 if (existingNetId != NETID_UNSET && existingNetId != netId) {
527 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
528 return -EBUSY;
529 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100530 if (int ret = getNetworkLocked(netId)->addInterface(interface)) {
531 return ret;
532 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700533
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +0900534 // Only populate mIfindexToLastNetId for non-local networks, because for these getIfIndex will
535 // return 0. That's fine though, because that map is only used to prevent force-closing sockets
536 // when the same IP address is handed over from one interface to another interface that is in
537 // the same network but not in the same netId (for now this is done only on VPNs). That is not
538 // useful for the local network because IP addresses in the local network are always assigned by
539 // the device itself and never meaningful on any other network.
540 if (netId != LOCAL_NET_ID) {
541 int ifIndex = RouteController::getIfIndex(interface);
542 if (ifIndex) {
543 mIfindexToLastNetId[ifIndex] = netId;
544 } else {
545 // Cannot happen, since addInterface() above will have failed.
546 ALOGE("inconceivable! added interface %s with no index", interface);
547 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100548 }
549 return 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700550}
551
552int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800553 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900554
555 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900556 ALOGE("no such netId %u", netId);
557 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700558 }
559
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700560 return getNetworkLocked(netId)->removeInterface(interface);
561}
562
563Permission NetworkController::getPermissionForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800564 ScopedRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700565 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700566}
567
568void NetworkController::setPermissionForUsers(Permission permission,
569 const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800570 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700571 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700572 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700573 }
574}
575
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900576int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800577 ScopedRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900578 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700579}
580
581int NetworkController::setPermissionForNetworks(Permission permission,
582 const std::vector<unsigned>& netIds) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800583 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700584 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700585 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900586 if (!network) {
587 ALOGE("no such netId %u", netId);
588 return -ENONET;
589 }
Ken Chen7ad712f2021-01-05 01:14:20 +0800590 if (!network->isPhysical()) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900591 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700592 return -EINVAL;
593 }
594
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700595 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700596 return ret;
597 }
598 }
599 return 0;
600}
601
Ken Chene99b8cb2020-12-09 07:33:06 +0800602namespace {
603
604int isWrongNetworkForUidRanges(unsigned netId, Network* network) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900605 if (!network) {
606 ALOGE("no such netId %u", netId);
607 return -ENONET;
608 }
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800609 if (!network->canAddUsers()) {
Ken Chen2f661522021-03-30 19:41:49 +0800610 ALOGE("cannot add/remove users to/from %s network %u", network->getTypeString().c_str(),
611 netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700612 return -EINVAL;
613 }
Ken Chene99b8cb2020-12-09 07:33:06 +0800614 return 0;
615}
616
617} // namespace
618
Ken Chend9aa98a2021-05-23 14:56:43 +0800619int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges,
620 uint32_t subPriority) {
Ken Chene99b8cb2020-12-09 07:33:06 +0800621 ScopedWLock lock(mRWLock);
622 Network* network = getNetworkLocked(netId);
623 if (int ret = isWrongNetworkForUidRanges(netId, network)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700624 return ret;
625 }
Ken Chend9aa98a2021-05-23 14:56:43 +0800626 return network->addUsers(uidRanges, subPriority);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700627}
628
Ken Chend9aa98a2021-05-23 14:56:43 +0800629int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges,
630 uint32_t subPriority) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800631 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700632 Network* network = getNetworkLocked(netId);
Ken Chene99b8cb2020-12-09 07:33:06 +0800633 if (int ret = isWrongNetworkForUidRanges(netId, network)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700634 return ret;
635 }
Ken Chend9aa98a2021-05-23 14:56:43 +0800636 return network->removeUsers(uidRanges, subPriority);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700637}
638
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700639int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
Tyler Wearfa94a272019-12-05 15:01:48 -0800640 const char* nexthop, bool legacy, uid_t uid, int mtu) {
641 return modifyRoute(netId, interface, destination, nexthop, ROUTE_ADD, legacy, uid, mtu);
642}
643
644int NetworkController::updateRoute(unsigned netId, const char* interface, const char* destination,
645 const char* nexthop, bool legacy, uid_t uid, int mtu) {
646 return modifyRoute(netId, interface, destination, nexthop, ROUTE_UPDATE, legacy, uid, mtu);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700647}
648
649int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
650 const char* nexthop, bool legacy, uid_t uid) {
Tyler Wearfa94a272019-12-05 15:01:48 -0800651 return modifyRoute(netId, interface, destination, nexthop, ROUTE_REMOVE, legacy, uid, 0);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700652}
653
Rubin Xu6c00b612018-04-27 14:27:59 +0100654void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800655 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100656 if (ifIndex == 0) {
657 ALOGE("Attempting to add address %s without ifindex", address);
658 return;
659 }
660 mAddressToIfindices[address].insert(ifIndex);
661}
662
663// Returns whether we should call SOCK_DESTROY on the removed address.
664bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800665 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100666 // First, update mAddressToIfindices map
667 auto ifindicesIter = mAddressToIfindices.find(address);
668 if (ifindicesIter == mAddressToIfindices.end()) {
669 ALOGE("Removing unknown address %s from ifindex %u", address, ifindex);
670 return true;
671 }
672 std::unordered_set<unsigned>& ifindices = ifindicesIter->second;
673 if (ifindices.erase(ifindex) > 0) {
674 if (ifindices.size() == 0) {
Bernie Innocentidd5a4f02018-07-19 18:06:52 +0900675 mAddressToIfindices.erase(ifindicesIter); // Invalidates ifindices
676 // The address is no longer configured on any interface.
677 return true;
Rubin Xu6c00b612018-04-27 14:27:59 +0100678 }
679 } else {
680 ALOGE("No record of address %s on interface %u", address, ifindex);
681 return true;
682 }
683 // Then, check for VPN handover condition
684 if (mIfindexToLastNetId.find(ifindex) == mIfindexToLastNetId.end()) {
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +0900685 ALOGW("Interface index %u was never in a currently-connected non-local netId", ifindex);
Rubin Xu6c00b612018-04-27 14:27:59 +0100686 return true;
687 }
688 unsigned lastNetId = mIfindexToLastNetId[ifindex];
689 for (unsigned idx : ifindices) {
690 unsigned activeNetId = mIfindexToLastNetId[idx];
691 // If this IP address is still assigned to another interface in the same network,
692 // then we don't need to destroy sockets on it because they are likely still valid.
693 // For now we do this only on VPNs.
694 // TODO: evaluate extending this to all network types.
695 if (lastNetId == activeNetId && isVirtualNetworkLocked(activeNetId)) {
696 return false;
697 }
698 }
699 return true;
700}
701
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900702bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700703 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
704 mProtectableUsers.find(uid) != mProtectableUsers.end();
705}
706
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900707bool NetworkController::canProtect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800708 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900709 return canProtectLocked(uid);
710}
711
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700712void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800713 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700714 mProtectableUsers.insert(uids.begin(), uids.end());
715}
716
717void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800718 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700719 for (uid_t uid : uids) {
720 mProtectableUsers.erase(uid);
721 }
722}
723
Erik Kline2d3a1632016-03-15 16:33:48 +0900724void NetworkController::dump(DumpWriter& dw) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800725 ScopedRLock lock(mRWLock);
Erik Kline2d3a1632016-03-15 16:33:48 +0900726
727 dw.incIndent();
728 dw.println("NetworkController");
729
730 dw.incIndent();
731 dw.println("Default network: %u", mDefaultNetId);
732
733 dw.blankline();
734 dw.println("Networks:");
735 dw.incIndent();
736 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900737 Network* network = i.second;
Erik Klineb31fd692018-06-06 20:50:11 +0900738 dw.println(network->toString());
Ken Chen7ad712f2021-01-05 01:14:20 +0800739 if (network->isPhysical()) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900740 dw.incIndent();
741 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
742 dw.println("Required permission: %s", permissionToName(permission));
743 dw.decIndent();
744 }
Ken Chene88e2cb2021-06-11 03:29:45 +0800745 if (const auto& str = network->uidRangesToString(); !str.empty()) {
746 dw.incIndent();
747 dw.println(str);
748 dw.decIndent();
749 }
Pierre Imai3a272072016-04-19 16:17:07 +0900750 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900751 }
752 dw.decIndent();
753
Rubin Xu6c00b612018-04-27 14:27:59 +0100754 dw.blankline();
755 dw.println("Interface <-> last network map:");
756 dw.incIndent();
757 for (const auto& i : mIfindexToLastNetId) {
758 dw.println("Ifindex: %u NetId: %u", i.first, i.second);
759 }
760 dw.decIndent();
761
762 dw.blankline();
763 dw.println("Interface addresses:");
764 dw.incIndent();
765 for (const auto& i : mAddressToIfindices) {
766 dw.println("address: %s ifindices: [%s]", i.first.c_str(),
767 android::base::Join(i.second, ", ").c_str());
768 }
769 dw.decIndent();
770
Erik Kline2d3a1632016-03-15 16:33:48 +0900771 dw.decIndent();
772
773 dw.decIndent();
774}
775
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700776bool NetworkController::isValidNetworkLocked(unsigned netId) const {
777 return getNetworkLocked(netId);
778}
779
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700780Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700781 auto iter = mNetworks.find(netId);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700782 return iter == mNetworks.end() ? nullptr : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700783}
784
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700785VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
Ken Chend9aa98a2021-05-23 14:56:43 +0800786 uint32_t subPriority;
Ken Chend15bcfc2020-12-04 00:08:54 +0800787 for (const auto& [_, network] : mNetworks) {
Ken Chend9aa98a2021-05-23 14:56:43 +0800788 if (network->isVirtual() && network->appliesToUser(uid, &subPriority)) {
Ken Chen7ad712f2021-01-05 01:14:20 +0800789 return static_cast<VirtualNetwork*>(network);
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700790 }
791 }
Yi Kongbdfd57e2018-07-25 13:26:10 -0700792 return nullptr;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700793}
794
Ken Chend9aa98a2021-05-23 14:56:43 +0800795// Returns a network with the highest subsidiary priority among physical and unreachable networks
796// that applies to uid. For a single subsidiary priority, an uid should belong to only one network.
797// If the uid apply to different network with the same priority at the same time, the behavior is
798// undefined. That is a configuration error.
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800799Network* NetworkController::getPhysicalOrUnreachableNetworkForUserLocked(uid_t uid) const {
Ken Chend9aa98a2021-05-23 14:56:43 +0800800 Network* bestNetwork = nullptr;
801 unsigned bestSubPriority = UidRanges::LOWEST_SUB_PRIORITY + 1;
802 for (const auto& [netId, network] : mNetworks) {
803 uint32_t subPriority;
804 if (!network->isPhysical() && !network->isUnreachable()) continue;
805 if (!network->appliesToUser(uid, &subPriority)) continue;
806 if (subPriority < bestSubPriority) {
807 bestNetwork = network;
808 bestSubPriority = subPriority;
Ken Chen8738e1c2020-11-24 11:38:54 +0800809 }
810 }
Ken Chend9aa98a2021-05-23 14:56:43 +0800811 return bestNetwork;
Ken Chen8738e1c2020-11-24 11:38:54 +0800812}
813
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700814Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
815 auto iter = mUsers.find(uid);
816 if (iter != mUsers.end()) {
817 return iter->second;
818 }
819 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
820}
821
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900822int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700823 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900824 if (!network) {
825 return -ENONET;
826 }
827
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700828 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
829 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900830 if (uid == INVALID_UID) {
831 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700832 }
Ken Chenf875b522020-12-03 19:05:02 +0800833 // If the UID has PERMISSION_SYSTEM, it can use whatever network it wants.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700834 Permission userPermission = getPermissionForUserLocked(uid);
835 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900836 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700837 }
Ken Chenf875b522020-12-03 19:05:02 +0800838 // If the UID wants to use a VPN, it can do so if and only if the VPN applies to the UID.
Ken Chend9aa98a2021-05-23 14:56:43 +0800839 uint32_t subPriority;
Ken Chen7ad712f2021-01-05 01:14:20 +0800840 if (network->isVirtual()) {
Ken Chend9aa98a2021-05-23 14:56:43 +0800841 return network->appliesToUser(uid, &subPriority) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700842 }
Ken Chenf875b522020-12-03 19:05:02 +0800843 // If a VPN applies to the UID, and the VPN is secure (i.e., not bypassable), then the UID can
844 // only select a different network if it has the ability to protect its sockets.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700845 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
846 if (virtualNetwork && virtualNetwork->isSecure() &&
847 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900848 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700849 }
Ken Chen8738e1c2020-11-24 11:38:54 +0800850 // If the UID wants to use a physical network and it has a UID range that includes the UID, the
851 // UID has permission to use it regardless of whether the permission bits match.
Ken Chend9aa98a2021-05-23 14:56:43 +0800852 if (network->isPhysical() && network->appliesToUser(uid, &subPriority)) {
Ken Chen8738e1c2020-11-24 11:38:54 +0800853 return 0;
854 }
Ken Chen55db3802021-03-30 13:47:49 +0800855 // Only apps that are configured as "no default network" can use the unreachable network.
856 if (network->isUnreachable()) {
Ken Chend9aa98a2021-05-23 14:56:43 +0800857 return network->appliesToUser(uid, &subPriority) ? 0 : -EPERM;
Ken Chen55db3802021-03-30 13:47:49 +0800858 }
Ken Chenf875b522020-12-03 19:05:02 +0800859 // Check whether the UID's permission bits are sufficient to use the network.
Ken Chen8738e1c2020-11-24 11:38:54 +0800860 // Because the permission of the system default network is PERMISSION_NONE(0x0), apps can always
861 // pass the check here when using the system default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700862 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900863 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700864}
865
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700866int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
Tyler Wearfa94a272019-12-05 15:01:48 -0800867 const char* nexthop, enum RouteOperation op, bool legacy,
868 uid_t uid, int mtu) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800869 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900870
871 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900872 ALOGE("no such netId %u", netId);
873 return -ENONET;
874 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900875 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900876 if (existingNetId == NETID_UNSET) {
877 ALOGE("interface %s not assigned to any netId", interface);
878 return -ENODEV;
879 }
880 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700881 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900882 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700883 }
884
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700885 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700886 if (netId == LOCAL_NET_ID) {
887 tableType = RouteController::LOCAL_NETWORK;
888 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900889 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700890 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700891 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700892 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700893 }
894 } else {
895 tableType = RouteController::INTERFACE;
896 }
897
Tyler Wearfa94a272019-12-05 15:01:48 -0800898 switch (op) {
899 case ROUTE_ADD:
900 return RouteController::addRoute(interface, destination, nexthop, tableType, mtu);
901 case ROUTE_UPDATE:
902 return RouteController::updateRoute(interface, destination, nexthop, tableType, mtu);
903 case ROUTE_REMOVE:
904 return RouteController::removeRoute(interface, destination, nexthop, tableType);
905 }
906 return -EINVAL;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700907}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700908
909int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
910 if (mDefaultNetId == NETID_UNSET) {
911 return 0;
912 }
913 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900914 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700915 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
916 return -ESRCH;
917 }
Ken Chen7ad712f2021-01-05 01:14:20 +0800918 if (!network->isPhysical()) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900919 ALOGE("inconceivable! default network must be a physical network");
920 return -EINVAL;
921 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700922 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
923 for (const auto& physicalInterface : network->getInterfaces()) {
924 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
925 add)) {
926 return ret;
927 }
928 }
929 return 0;
930}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900931
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900932void NetworkController::updateTcpSocketMonitorPolling() {
933 bool physicalNetworkExists = false;
934 for (const auto& entry : mNetworks) {
935 const auto& network = entry.second;
Ken Chen7ad712f2021-01-05 01:14:20 +0800936 if (network->isPhysical() && network->getNetId() >= MIN_NET_ID) {
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900937 physicalNetworkExists = true;
938 break;
939 }
940 }
941
942 if (physicalNetworkExists) {
943 android::net::gCtls->tcpSocketMonitor.resumePolling();
944 } else {
945 android::net::gCtls->tcpSocketMonitor.suspendPolling();
946 }
947}
948
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900949} // namespace android::net