blob: f1441397d1b8fd8178f3aa1cc6562638b8bc8c72 [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"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070039#include "PhysicalNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070040#include "RouteController.h"
Patrick Rohr70d42502021-10-12 18:24:10 +020041#include "TcUtils.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"
waynema5851b032021-11-24 17:08:25 +080045#include "netdutils/Utils.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090046#include "netid_client.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070047
Erik Kline6d4669f2017-05-25 17:03:31 +090048#define DBG 0
49
Luke Huangb257d612019-03-14 21:19:13 +080050using android::netdutils::DumpWriter;
waynema5851b032021-11-24 17:08:25 +080051using android::netdutils::getIfaceNames;
Luke Huangb257d612019-03-14 21:19:13 +080052
Bernie Innocenti762dcf42019-06-14 19:52:49 +090053namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090054
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070055namespace {
56
57// Keep these in sync with ConnectivityService.java.
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070058const unsigned MIN_NET_ID = 100;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070059const unsigned MAX_NET_ID = 65535;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070060
61} // namespace
62
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070063// All calls to methods here are made while holding a write lock on mRWLock.
Lorenzo Colitti2d014e72018-01-10 22:12:29 +090064// They are mostly not called directly from this class, but from methods in PhysicalNetwork.cpp.
65// However, we're the only user of that class, so all calls to those methods come from here and are
66// made under lock.
67// For example, PhysicalNetwork::setPermission ends up calling addFallthrough and removeFallthrough,
68// but it's only called from here under lock (specifically, from createPhysicalNetworkLocked and
69// setPermissionForNetworks).
70// TODO: use std::mutex and GUARDED_BY instead of manual inspection.
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070071class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate {
Bernie Innocenti762dcf42019-06-14 19:52:49 +090072 public:
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070073 explicit DelegateImpl(NetworkController* networkController);
74 virtual ~DelegateImpl();
75
Bernie Innocenti762dcf42019-06-14 19:52:49 +090076 [[nodiscard]] int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface,
77 Permission permission, bool add);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070078
Bernie Innocenti762dcf42019-06-14 19:52:49 +090079 private:
80 [[nodiscard]] int addFallthrough(const std::string& physicalInterface,
81 Permission permission) override;
82 [[nodiscard]] int removeFallthrough(const std::string& physicalInterface,
83 Permission permission) override;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070084
Bernie Innocenti762dcf42019-06-14 19:52:49 +090085 [[nodiscard]] int modifyFallthrough(const std::string& physicalInterface, Permission permission,
86 bool add);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070087
88 NetworkController* const mNetworkController;
89};
90
91NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) :
92 mNetworkController(networkController) {
93}
94
95NetworkController::DelegateImpl::~DelegateImpl() {
96}
97
98int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId,
99 const std::string& physicalInterface,
100 Permission permission, bool add) {
101 if (add) {
102 if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId,
103 physicalInterface.c_str(),
104 permission)) {
105 ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
106 vpnNetId);
107 return ret;
108 }
109 } else {
110 if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId,
111 physicalInterface.c_str(),
112 permission)) {
113 ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(),
114 vpnNetId);
115 return ret;
116 }
117 }
118 return 0;
119}
120
121int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface,
122 Permission permission) {
123 return modifyFallthrough(physicalInterface, permission, true);
124}
125
126int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface,
127 Permission permission) {
128 return modifyFallthrough(physicalInterface, permission, false);
129}
130
131int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface,
132 Permission permission, bool add) {
133 for (const auto& entry : mNetworkController->mNetworks) {
Ken Chen7ad712f2021-01-05 01:14:20 +0800134 if (entry.second->isVirtual()) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700135 if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) {
136 return ret;
137 }
138 }
139 }
140 return 0;
141}
142
143NetworkController::NetworkController() :
Pierre Imai6be56192016-05-16 16:32:17 +0900144 mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET),
145 mProtectableUsers({AID_VPN}) {
Ken Chen66e860f2021-04-01 15:50:28 +0800146 gLog.info("enter NetworkController ctor");
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700147 mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900148 mNetworks[DUMMY_NET_ID] = new DummyNetwork(DUMMY_NET_ID);
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800149 mNetworks[UNREACHABLE_NET_ID] = new UnreachableNetwork(UNREACHABLE_NET_ID);
Hungming Chen228cffa2020-02-18 17:11:18 +0800150
151 // Clear all clsact stubs on all interfaces.
152 // TODO: perhaps only remove the clsact on the interface which is added by
153 // RouteController::addInterfaceToPhysicalNetwork. Currently, the netd only
154 // attach the clsact to the interface for the physical network.
waynema5851b032021-11-24 17:08:25 +0800155 const auto& ifaces = getIfaceNames();
Maciej Żenczykowski0e5d26f2021-01-17 03:14:20 -0800156 if (isOk(ifaces)) {
157 for (const std::string& iface : ifaces.value()) {
158 if (int ifIndex = if_nametoindex(iface.c_str())) {
159 // Ignore the error because the interface might not have a clsact.
160 tcQdiscDelDevClsact(ifIndex);
Hungming Chen228cffa2020-02-18 17:11:18 +0800161 }
162 }
163 }
Ken Chen66e860f2021-04-01 15:50:28 +0800164 gLog.info("leave NetworkController ctor");
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500165}
166
167unsigned NetworkController::getDefaultNetwork() const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800168 ScopedRLock lock(mRWLock);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500169 return mDefaultNetId;
170}
171
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700172int NetworkController::setDefaultNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800173 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700174
175 if (netId == mDefaultNetId) {
176 return 0;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700177 }
178
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700179 if (netId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700180 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900181 if (!network) {
182 ALOGE("no such netId %u", netId);
183 return -ENONET;
184 }
Ken Chen7ad712f2021-01-05 01:14:20 +0800185 if (!network->isPhysical()) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900186 ALOGE("cannot set default to non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700187 return -EINVAL;
188 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700189 if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700190 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700191 }
192 }
193
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700194 if (mDefaultNetId != NETID_UNSET) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700195 Network* network = getNetworkLocked(mDefaultNetId);
Ken Chen7ad712f2021-01-05 01:14:20 +0800196 if (!network || !network->isPhysical()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700197 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
198 return -ESRCH;
199 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700200 if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700201 return ret;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700202 }
203 }
204
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700205 mDefaultNetId = netId;
206 return 0;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500207}
208
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900209uint32_t NetworkController::getNetworkForDnsLocked(unsigned* netId, uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700210 Fwmark fwmark;
211 fwmark.protectedFromVpn = true;
212 fwmark.permission = PERMISSION_SYSTEM;
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900213
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800214 Network* appDefaultNetwork = getPhysicalOrUnreachableNetworkForUserLocked(uid);
Ken Chen8738e1c2020-11-24 11:38:54 +0800215 unsigned defaultNetId = appDefaultNetwork ? appDefaultNetwork->getNetId() : mDefaultNetId;
216
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900217 // Common case: there is no VPN that applies to the user, and the query did not specify a netId.
218 // Therefore, it is safe to set the explicit bit on this query and skip all the complex logic
219 // below. While this looks like a special case, it is actually the one that handles the vast
220 // majority of DNS queries.
221 // TODO: untangle this code.
222 if (*netId == NETID_UNSET && getVirtualNetworkForUserLocked(uid) == nullptr) {
Ken Chen8738e1c2020-11-24 11:38:54 +0800223 *netId = defaultNetId;
Lorenzo Colitti95f1bcb2018-05-30 16:14:18 +0900224 fwmark.netId = *netId;
225 fwmark.explicitlySelected = true;
226 return fwmark.intValue;
227 }
228
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900229 if (checkUserNetworkAccessLocked(uid, *netId) == 0) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700230 // If a non-zero NetId was explicitly specified, and the user has permission for that
Luke Huangd2861982019-05-17 19:47:28 +0800231 // network, use that network's DNS servers. (possibly falling through the to the default
232 // network if the VPN doesn't provide a route to them).
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700233 fwmark.explicitlySelected = true;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900234
235 // If the network is a VPN and it doesn't have DNS servers, use the default network's DNS
236 // servers (through the default network). Otherwise, the query is guaranteed to fail.
237 // http://b/29498052
238 Network *network = getNetworkLocked(*netId);
Ken Chen7ad712f2021-01-05 01:14:20 +0800239 if (network && network->isVirtual() && !resolv_has_nameservers(*netId)) {
Ken Chen8738e1c2020-11-24 11:38:54 +0800240 *netId = defaultNetId;
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900241 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700242 } else {
243 // If the user is subject to a VPN and the VPN provides DNS servers, use those servers
244 // (possibly falling through to the default network if the VPN doesn't provide a route to
Luke Huangd2861982019-05-17 19:47:28 +0800245 // them). Otherwise, use the default network's DNS servers.
246 // TODO: Consider if we should set the explicit bit here.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700247 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
Jooyung Han3e64aa12019-11-27 15:36:29 +0900248 if (virtualNetwork && resolv_has_nameservers(virtualNetwork->getNetId())) {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700249 *netId = virtualNetwork->getNetId();
250 } else {
Lorenzo Colittic63059c2016-06-21 23:54:12 +0900251 // TODO: return an error instead of silently doing the DNS lookup on the wrong network.
252 // http://b/27560555
Ken Chen8738e1c2020-11-24 11:38:54 +0800253 *netId = defaultNetId;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700254 }
255 }
256 fwmark.netId = *netId;
257 return fwmark.intValue;
258}
259
260// Returns the NetId that a given UID would use if no network is explicitly selected. Specifically,
Ken Chen55db3802021-03-30 13:47:49 +0800261// the VPN that applies to the UID if any; Otherwise, the default network for UID; Otherwise the
262// unreachable network that applies to the UID; lastly, the default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700263unsigned NetworkController::getNetworkForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800264 ScopedRLock lock(mRWLock);
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700265 if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700266 return virtualNetwork->getNetId();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500267 }
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800268 if (Network* network = getPhysicalOrUnreachableNetworkForUserLocked(uid)) {
269 return network->getNetId();
Ken Chen8738e1c2020-11-24 11:38:54 +0800270 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700271 return mDefaultNetId;
272}
273
274// 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 +0800275// applies to the user if any; otherwise, the default network that applies to user if any; lastly,
276// the default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700277//
278// In general, we prefer to always set the default network's NetId in connect(), so that if the VPN
279// is a split-tunnel and disappears later, the socket continues working (since the default network's
280// NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a
281// high-priority routing rule that doesn't care what NetId the socket has.
282//
283// But bypassable VPNs have a very low priority rule, so we need to mark the socket with the
284// bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a
285// split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough
286// traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus
287// the fallthrough rules also go away), the socket that used to fallthrough to the default network
288// will stop working.
Ken Chen8738e1c2020-11-24 11:38:54 +0800289//
290// Per-app physical default networks behave the same as bypassable VPNs: when a socket is connected
291// on one of these networks, we mark the socket with the netId of the network. This ensures that if
292// the per-app default network changes, sockets established on the previous network are still
293// routed to that network, assuming the network's UID ranges still apply to the UID. While this
294// means that fallthrough to the default network does not work, physical networks not expected
295// ever to be split tunnels.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900296unsigned NetworkController::getNetworkForConnectLocked(uid_t uid) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700297 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
298 if (virtualNetwork && !virtualNetwork->isSecure()) {
299 return virtualNetwork->getNetId();
300 }
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800301 if (Network* network = getPhysicalOrUnreachableNetworkForUserLocked(uid)) {
302 return network->getNetId();
Ken Chen8738e1c2020-11-24 11:38:54 +0800303 }
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700304 return mDefaultNetId;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500305}
306
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900307unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800308 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900309 return getNetworkForConnectLocked(uid);
310}
311
Erik Klinecea2d342015-06-25 18:24:46 +0900312void NetworkController::getNetworkContext(
313 unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800314 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900315
Erik Klinecea2d342015-06-25 18:24:46 +0900316 struct android_net_context nc = {
317 .app_netid = netId,
318 .app_mark = MARK_UNSET,
319 .dns_netid = netId,
320 .dns_mark = MARK_UNSET,
321 .uid = uid,
322 };
323
Erik Kline492ca5b2016-03-09 14:56:00 +0900324 // |netId| comes directly (via dnsproxyd) from the value returned by netIdForResolv() in the
325 // client process. This value is nonzero iff.:
326 //
327 // 1. The app specified a netid/nethandle to a DNS resolution method such as:
328 // - [Java] android.net.Network#getAllByName()
329 // - [C/++] android_getaddrinfofornetwork()
330 // 2. The app specified a netid/nethandle to be used as a process default via:
331 // - [Java] android.net.ConnectivityManager#bindProcessToNetwork()
332 // - [C/++] android_setprocnetwork()
333 // 3. The app called android.net.ConnectivityManager#startUsingNetworkFeature().
334 //
335 // In all these cases (with the possible exception of #3), the right thing to do is to treat
336 // such cases as explicitlySelected.
337 const bool explicitlySelected = (nc.app_netid != NETID_UNSET);
338 if (!explicitlySelected) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900339 nc.app_netid = getNetworkForConnectLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900340 }
Erik Kline492ca5b2016-03-09 14:56:00 +0900341
Erik Klinecea2d342015-06-25 18:24:46 +0900342 Fwmark fwmark;
343 fwmark.netId = nc.app_netid;
Erik Kline492ca5b2016-03-09 14:56:00 +0900344 fwmark.explicitlySelected = explicitlySelected;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900345 fwmark.protectedFromVpn = explicitlySelected && canProtectLocked(uid);
346 fwmark.permission = getPermissionForUserLocked(uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900347 nc.app_mark = fwmark.intValue;
348
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900349 nc.dns_mark = getNetworkForDnsLocked(&(nc.dns_netid), uid);
Erik Klinecea2d342015-06-25 18:24:46 +0900350
Erik Kline6d4669f2017-05-25 17:03:31 +0900351 if (DBG) {
352 ALOGD("app_netid:0x%x app_mark:0x%x dns_netid:0x%x dns_mark:0x%x uid:%d",
353 nc.app_netid, nc.app_mark, nc.dns_netid, nc.dns_mark, uid);
354 }
355
Erik Klinecea2d342015-06-25 18:24:46 +0900356 if (netcontext) {
357 *netcontext = nc;
358 }
359}
360
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900361unsigned NetworkController::getNetworkForInterfaceLocked(const char* interface) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700362 for (const auto& entry : mNetworks) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700363 if (entry.second->hasInterface(interface)) {
364 return entry.first;
365 }
366 }
Paul Jensen35c77e32014-04-10 14:57:54 -0400367 return NETID_UNSET;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500368}
369
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900370unsigned NetworkController::getNetworkForInterface(const char* interface) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800371 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900372 return getNetworkForInterfaceLocked(interface);
373}
374
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700375bool NetworkController::isVirtualNetwork(unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800376 ScopedRLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100377 return isVirtualNetworkLocked(netId);
378}
379
380bool NetworkController::isVirtualNetworkLocked(unsigned netId) const {
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700381 Network* network = getNetworkLocked(netId);
Ken Chen7ad712f2021-01-05 01:14:20 +0800382 return network && network->isVirtual();
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700383}
384
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700385int NetworkController::createPhysicalNetworkLocked(unsigned netId, Permission permission) {
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700386 if (!((MIN_NET_ID <= netId && netId <= MAX_NET_ID) ||
387 (MIN_OEM_ID <= netId && netId <= MAX_OEM_ID))) {
Paul Jensenae37e8a2014-04-28 10:35:51 -0400388 ALOGE("invalid netId %u", netId);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900389 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700390 }
391
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700392 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700393 ALOGE("duplicate netId %u", netId);
394 return -EEXIST;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700395 }
396
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700397 PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700398 if (int ret = physicalNetwork->setPermission(permission)) {
399 ALOGE("inconceivable! setPermission cannot fail on an empty network");
400 delete physicalNetwork;
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900401 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700402 }
403
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700404 mNetworks[netId] = physicalNetwork;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900405
406 updateTcpSocketMonitorPolling();
407
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900408 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700409}
410
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700411int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800412 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700413 return createPhysicalNetworkLocked(netId, permission);
414}
415
416int NetworkController::createPhysicalOemNetwork(Permission permission, unsigned *pNetId) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700417 if (pNetId == nullptr) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700418 return -EINVAL;
419 }
420
Luke Huangd1ee4622018-06-29 13:49:58 +0800421 ScopedWLock lock(mRWLock);
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700422 for (*pNetId = MIN_OEM_ID; *pNetId <= MAX_OEM_ID; (*pNetId)++) {
423 if (!isValidNetworkLocked(*pNetId)) {
424 break;
425 }
426 }
427
428 if (*pNetId > MAX_OEM_ID) {
429 ALOGE("No free network ID");
430 *pNetId = 0;
431 return -ENONET;
432 }
433
434 int ret = createPhysicalNetworkLocked(*pNetId, permission);
435 if (ret) {
436 *pNetId = 0;
437 }
438
439 return ret;
440}
441
Chiachang Wang2b0abee2022-01-12 10:03:17 +0800442int NetworkController::createVirtualNetwork(unsigned netId, bool secure, NativeVpnType vpnType,
443 bool excludeLocalRoutes) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800444 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900445
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -0700446 if (!(MIN_NET_ID <= netId && netId <= MAX_NET_ID)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700447 ALOGE("invalid netId %u", netId);
448 return -EINVAL;
449 }
450
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900451 if (isValidNetworkLocked(netId)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700452 ALOGE("duplicate netId %u", netId);
453 return -EEXIST;
454 }
455
Ken Chenab5f3472021-04-04 11:28:06 +0800456 if (vpnType < NativeVpnType::SERVICE || NativeVpnType::OEM < vpnType) {
457 ALOGE("invalid vpnType %d", static_cast<int>(vpnType));
458 return -EINVAL;
459 }
460
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700461 if (int ret = modifyFallthroughLocked(netId, true)) {
462 return ret;
463 }
Chiachang Wang2b0abee2022-01-12 10:03:17 +0800464 mNetworks[netId] = new VirtualNetwork(netId, secure, excludeLocalRoutes);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700465 return 0;
466}
467
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700468int NetworkController::destroyNetwork(unsigned netId) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800469 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900470
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800471 if (netId == LOCAL_NET_ID || netId == UNREACHABLE_NET_ID) {
472 ALOGE("cannot destroy local or unreachable network");
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700473 return -EINVAL;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700474 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900475 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900476 ALOGE("no such netId %u", netId);
477 return -ENONET;
478 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700479
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700480 // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700481
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700482 Network* network = getNetworkLocked(netId);
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900483
484 // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the
485 // other network code, ignore failures and attempt to clear out as much state as possible, even
486 // if we hit an error on the way. Return the first error that we see.
487 int ret = network->clearInterfaces();
488
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700489 if (mDefaultNetId == netId) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900490 if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700491 ALOGE("inconceivable! removeAsDefault cannot fail on an empty network");
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900492 if (!ret) {
493 ret = err;
494 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700495 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700496 mDefaultNetId = NETID_UNSET;
Ken Chen7ad712f2021-01-05 01:14:20 +0800497 } else if (network->isVirtual()) {
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900498 if (int err = modifyFallthroughLocked(netId, false)) {
499 if (!ret) {
500 ret = err;
501 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700502 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700503 }
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700504 mNetworks.erase(netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700505 delete network;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900506
Rubin Xu6c00b612018-04-27 14:27:59 +0100507 for (auto iter = mIfindexToLastNetId.begin(); iter != mIfindexToLastNetId.end();) {
508 if (iter->second == netId) {
509 iter = mIfindexToLastNetId.erase(iter);
510 } else {
511 ++iter;
512 }
513 }
514
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900515 updateTcpSocketMonitorPolling();
516
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900517 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700518}
519
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700520int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800521 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900522
523 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900524 ALOGE("no such netId %u", netId);
525 return -ENONET;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700526 }
527
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900528 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700529 if (existingNetId != NETID_UNSET && existingNetId != netId) {
530 ALOGE("interface %s already assigned to netId %u", interface, existingNetId);
531 return -EBUSY;
532 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100533 if (int ret = getNetworkLocked(netId)->addInterface(interface)) {
534 return ret;
535 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700536
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +0900537 // Only populate mIfindexToLastNetId for non-local networks, because for these getIfIndex will
538 // return 0. That's fine though, because that map is only used to prevent force-closing sockets
539 // when the same IP address is handed over from one interface to another interface that is in
540 // the same network but not in the same netId (for now this is done only on VPNs). That is not
541 // useful for the local network because IP addresses in the local network are always assigned by
542 // the device itself and never meaningful on any other network.
543 if (netId != LOCAL_NET_ID) {
544 int ifIndex = RouteController::getIfIndex(interface);
545 if (ifIndex) {
546 mIfindexToLastNetId[ifIndex] = netId;
547 } else {
548 // Cannot happen, since addInterface() above will have failed.
549 ALOGE("inconceivable! added interface %s with no index", interface);
550 }
Rubin Xu6c00b612018-04-27 14:27:59 +0100551 }
552 return 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700553}
554
555int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800556 ScopedWLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900557
558 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900559 ALOGE("no such netId %u", netId);
560 return -ENONET;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700561 }
562
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700563 return getNetworkLocked(netId)->removeInterface(interface);
564}
565
566Permission NetworkController::getPermissionForUser(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800567 ScopedRLock lock(mRWLock);
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700568 return getPermissionForUserLocked(uid);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700569}
570
571void NetworkController::setPermissionForUsers(Permission permission,
572 const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800573 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700574 for (uid_t uid : uids) {
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700575 mUsers[uid] = permission;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700576 }
577}
578
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900579int NetworkController::checkUserNetworkAccess(uid_t uid, unsigned netId) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800580 ScopedRLock lock(mRWLock);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900581 return checkUserNetworkAccessLocked(uid, netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700582}
583
584int NetworkController::setPermissionForNetworks(Permission permission,
585 const std::vector<unsigned>& netIds) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800586 ScopedWLock lock(mRWLock);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700587 for (unsigned netId : netIds) {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700588 Network* network = getNetworkLocked(netId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900589 if (!network) {
590 ALOGE("no such netId %u", netId);
591 return -ENONET;
592 }
Ken Chen7ad712f2021-01-05 01:14:20 +0800593 if (!network->isPhysical()) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900594 ALOGE("cannot set permissions on non-physical network with netId %u", netId);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700595 return -EINVAL;
596 }
597
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700598 if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700599 return ret;
600 }
601 }
602 return 0;
603}
604
Ken Chene99b8cb2020-12-09 07:33:06 +0800605namespace {
606
607int isWrongNetworkForUidRanges(unsigned netId, Network* network) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900608 if (!network) {
609 ALOGE("no such netId %u", netId);
610 return -ENONET;
611 }
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800612 if (!network->canAddUsers()) {
Ken Chen2f661522021-03-30 19:41:49 +0800613 ALOGE("cannot add/remove users to/from %s network %u", network->getTypeString().c_str(),
614 netId);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700615 return -EINVAL;
616 }
Ken Chene99b8cb2020-12-09 07:33:06 +0800617 return 0;
618}
619
620} // namespace
621
Ken Chen4ea88462021-05-23 14:56:43 +0800622int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges,
Ken Chen53360bf2021-12-10 02:41:05 +0800623 int32_t subPriority) {
Ken Chene99b8cb2020-12-09 07:33:06 +0800624 ScopedWLock lock(mRWLock);
625 Network* network = getNetworkLocked(netId);
626 if (int ret = isWrongNetworkForUidRanges(netId, network)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700627 return ret;
628 }
Ken Chen4ea88462021-05-23 14:56:43 +0800629 return network->addUsers(uidRanges, subPriority);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700630}
631
Ken Chen4ea88462021-05-23 14:56:43 +0800632int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges,
Ken Chen53360bf2021-12-10 02:41:05 +0800633 int32_t subPriority) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800634 ScopedWLock lock(mRWLock);
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700635 Network* network = getNetworkLocked(netId);
Ken Chene99b8cb2020-12-09 07:33:06 +0800636 if (int ret = isWrongNetworkForUidRanges(netId, network)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700637 return ret;
638 }
Ken Chen4ea88462021-05-23 14:56:43 +0800639 return network->removeUsers(uidRanges, subPriority);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700640}
641
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700642int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination,
Tyler Wearfa94a272019-12-05 15:01:48 -0800643 const char* nexthop, bool legacy, uid_t uid, int mtu) {
644 return modifyRoute(netId, interface, destination, nexthop, ROUTE_ADD, legacy, uid, mtu);
645}
646
647int NetworkController::updateRoute(unsigned netId, const char* interface, const char* destination,
648 const char* nexthop, bool legacy, uid_t uid, int mtu) {
649 return modifyRoute(netId, interface, destination, nexthop, ROUTE_UPDATE, legacy, uid, mtu);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700650}
651
652int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination,
653 const char* nexthop, bool legacy, uid_t uid) {
Tyler Wearfa94a272019-12-05 15:01:48 -0800654 return modifyRoute(netId, interface, destination, nexthop, ROUTE_REMOVE, legacy, uid, 0);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700655}
656
Rubin Xu6c00b612018-04-27 14:27:59 +0100657void NetworkController::addInterfaceAddress(unsigned ifIndex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800658 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100659 if (ifIndex == 0) {
660 ALOGE("Attempting to add address %s without ifindex", address);
661 return;
662 }
663 mAddressToIfindices[address].insert(ifIndex);
664}
665
666// Returns whether we should call SOCK_DESTROY on the removed address.
667bool NetworkController::removeInterfaceAddress(unsigned ifindex, const char* address) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800668 ScopedWLock lock(mRWLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100669 // First, update mAddressToIfindices map
670 auto ifindicesIter = mAddressToIfindices.find(address);
671 if (ifindicesIter == mAddressToIfindices.end()) {
672 ALOGE("Removing unknown address %s from ifindex %u", address, ifindex);
673 return true;
674 }
675 std::unordered_set<unsigned>& ifindices = ifindicesIter->second;
676 if (ifindices.erase(ifindex) > 0) {
677 if (ifindices.size() == 0) {
Bernie Innocentidd5a4f02018-07-19 18:06:52 +0900678 mAddressToIfindices.erase(ifindicesIter); // Invalidates ifindices
679 // The address is no longer configured on any interface.
680 return true;
Rubin Xu6c00b612018-04-27 14:27:59 +0100681 }
682 } else {
683 ALOGE("No record of address %s on interface %u", address, ifindex);
684 return true;
685 }
686 // Then, check for VPN handover condition
687 if (mIfindexToLastNetId.find(ifindex) == mIfindexToLastNetId.end()) {
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +0900688 ALOGW("Interface index %u was never in a currently-connected non-local netId", ifindex);
Rubin Xu6c00b612018-04-27 14:27:59 +0100689 return true;
690 }
691 unsigned lastNetId = mIfindexToLastNetId[ifindex];
692 for (unsigned idx : ifindices) {
693 unsigned activeNetId = mIfindexToLastNetId[idx];
694 // If this IP address is still assigned to another interface in the same network,
695 // then we don't need to destroy sockets on it because they are likely still valid.
696 // For now we do this only on VPNs.
697 // TODO: evaluate extending this to all network types.
698 if (lastNetId == activeNetId && isVirtualNetworkLocked(activeNetId)) {
699 return false;
700 }
701 }
702 return true;
703}
704
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900705bool NetworkController::canProtectLocked(uid_t uid) const {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700706 return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) ||
707 mProtectableUsers.find(uid) != mProtectableUsers.end();
708}
709
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900710bool NetworkController::canProtect(uid_t uid) const {
Luke Huangd1ee4622018-06-29 13:49:58 +0800711 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900712 return canProtectLocked(uid);
713}
714
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700715void NetworkController::allowProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800716 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700717 mProtectableUsers.insert(uids.begin(), uids.end());
718}
719
720void NetworkController::denyProtect(const std::vector<uid_t>& uids) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800721 ScopedWLock lock(mRWLock);
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700722 for (uid_t uid : uids) {
723 mProtectableUsers.erase(uid);
724 }
725}
726
Erik Kline2d3a1632016-03-15 16:33:48 +0900727void NetworkController::dump(DumpWriter& dw) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800728 ScopedRLock lock(mRWLock);
Erik Kline2d3a1632016-03-15 16:33:48 +0900729
730 dw.incIndent();
731 dw.println("NetworkController");
732
733 dw.incIndent();
734 dw.println("Default network: %u", mDefaultNetId);
735
736 dw.blankline();
737 dw.println("Networks:");
738 dw.incIndent();
739 for (const auto& i : mNetworks) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900740 Network* network = i.second;
Erik Klineb31fd692018-06-06 20:50:11 +0900741 dw.println(network->toString());
Ken Chen7ad712f2021-01-05 01:14:20 +0800742 if (network->isPhysical()) {
Lorenzo Colittid1029652016-09-26 17:17:40 +0900743 dw.incIndent();
744 Permission permission = reinterpret_cast<PhysicalNetwork*>(network)->getPermission();
745 dw.println("Required permission: %s", permissionToName(permission));
746 dw.decIndent();
747 }
Ken Chen8e0ba5a2021-06-11 03:29:45 +0800748 if (const auto& str = network->uidRangesToString(); !str.empty()) {
749 dw.incIndent();
750 dw.println(str);
751 dw.decIndent();
752 }
Pierre Imai3a272072016-04-19 16:17:07 +0900753 dw.blankline();
Erik Kline2d3a1632016-03-15 16:33:48 +0900754 }
755 dw.decIndent();
756
Rubin Xu6c00b612018-04-27 14:27:59 +0100757 dw.blankline();
758 dw.println("Interface <-> last network map:");
759 dw.incIndent();
760 for (const auto& i : mIfindexToLastNetId) {
761 dw.println("Ifindex: %u NetId: %u", i.first, i.second);
762 }
763 dw.decIndent();
764
765 dw.blankline();
766 dw.println("Interface addresses:");
767 dw.incIndent();
768 for (const auto& i : mAddressToIfindices) {
769 dw.println("address: %s ifindices: [%s]", i.first.c_str(),
770 android::base::Join(i.second, ", ").c_str());
771 }
772 dw.decIndent();
773
paulhu28582b72021-10-27 13:55:27 +0800774 dw.blankline();
775 dw.println("Permission of users:");
776 dw.incIndent();
777 std::vector<uid_t> systemUids;
778 std::vector<uid_t> networkUids;
779 for (const auto& [uid, permission] : mUsers) {
780 if ((permission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
781 systemUids.push_back(uid);
782 } else if ((permission & PERMISSION_NETWORK) == PERMISSION_NETWORK) {
783 networkUids.push_back(uid);
784 }
785 }
786 dw.println("NETWORK: %s", android::base::Join(networkUids, ", ").c_str());
787 dw.println("SYSTEM: %s", android::base::Join(systemUids, ", ").c_str());
788 dw.decIndent();
789
Erik Kline2d3a1632016-03-15 16:33:48 +0900790 dw.decIndent();
791
792 dw.decIndent();
793}
794
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700795bool NetworkController::isValidNetworkLocked(unsigned netId) const {
796 return getNetworkLocked(netId);
797}
798
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700799Network* NetworkController::getNetworkLocked(unsigned netId) const {
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700800 auto iter = mNetworks.find(netId);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700801 return iter == mNetworks.end() ? nullptr : iter->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700802}
803
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700804VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const {
Ken Chen53360bf2021-12-10 02:41:05 +0800805 int32_t subPriority;
Ken Chend15bcfc2020-12-04 00:08:54 +0800806 for (const auto& [_, network] : mNetworks) {
Ken Chen4ea88462021-05-23 14:56:43 +0800807 if (network->isVirtual() && network->appliesToUser(uid, &subPriority)) {
Ken Chen7ad712f2021-01-05 01:14:20 +0800808 return static_cast<VirtualNetwork*>(network);
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700809 }
810 }
Yi Kongbdfd57e2018-07-25 13:26:10 -0700811 return nullptr;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700812}
813
Patrick Rohre6f198c2022-01-25 13:50:31 +0100814// Returns the default network with the highest subsidiary priority among physical and unreachable
815// networks that applies to uid. For a single subsidiary priority, an uid should belong to only one
816// network. If the uid apply to different network with the same priority at the same time, the
817// behavior is undefined. That is a configuration error.
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800818Network* NetworkController::getPhysicalOrUnreachableNetworkForUserLocked(uid_t uid) const {
Ken Chen4ea88462021-05-23 14:56:43 +0800819 Network* bestNetwork = nullptr;
Patrick Rohre6f198c2022-01-25 13:50:31 +0100820
821 // In this function, appliesToUser() is used to figure out if this network is the user's default
822 // network (not just if the user has access to this network). Rules at SUB_PRIORITY_NO_DEFAULT
823 // "apply to the user" but do not include a default network rule. Since their subpriority (999)
824 // is greater than SUB_PRIORITY_LOWEST (998), these rules never trump any subpriority that
825 // includes a default network rule (appliesToUser returns the "highest" (=lowest value)
826 // subPriority that includes the uid), and they get filtered out in the if-statement below.
827 int32_t bestSubPriority = UidRanges::SUB_PRIORITY_NO_DEFAULT;
Ken Chen4ea88462021-05-23 14:56:43 +0800828 for (const auto& [netId, network] : mNetworks) {
Ken Chen53360bf2021-12-10 02:41:05 +0800829 int32_t subPriority;
Ken Chen4ea88462021-05-23 14:56:43 +0800830 if (!network->isPhysical() && !network->isUnreachable()) continue;
831 if (!network->appliesToUser(uid, &subPriority)) continue;
Patrick Rohre6f198c2022-01-25 13:50:31 +0100832 if (subPriority == UidRanges::SUB_PRIORITY_NO_DEFAULT) continue;
833
Ken Chen4ea88462021-05-23 14:56:43 +0800834 if (subPriority < bestSubPriority) {
835 bestNetwork = network;
836 bestSubPriority = subPriority;
Ken Chen8738e1c2020-11-24 11:38:54 +0800837 }
838 }
Ken Chen4ea88462021-05-23 14:56:43 +0800839 return bestNetwork;
Ken Chen8738e1c2020-11-24 11:38:54 +0800840}
841
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700842Permission NetworkController::getPermissionForUserLocked(uid_t uid) const {
843 auto iter = mUsers.find(uid);
844 if (iter != mUsers.end()) {
845 return iter->second;
846 }
847 return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE;
848}
849
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900850int NetworkController::checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const {
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700851 Network* network = getNetworkLocked(netId);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900852 if (!network) {
853 return -ENONET;
854 }
855
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700856 // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer
857 // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid.
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900858 if (uid == INVALID_UID) {
859 return -EREMOTEIO;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700860 }
Ken Chenf875b522020-12-03 19:05:02 +0800861 // If the UID has PERMISSION_SYSTEM, it can use whatever network it wants.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700862 Permission userPermission = getPermissionForUserLocked(uid);
863 if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900864 return 0;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700865 }
Ken Chenf875b522020-12-03 19:05:02 +0800866 // If the UID wants to use a VPN, it can do so if and only if the VPN applies to the UID.
Ken Chen53360bf2021-12-10 02:41:05 +0800867 int32_t subPriority;
Ken Chen7ad712f2021-01-05 01:14:20 +0800868 if (network->isVirtual()) {
Ken Chen4ea88462021-05-23 14:56:43 +0800869 return network->appliesToUser(uid, &subPriority) ? 0 : -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700870 }
Ken Chenf875b522020-12-03 19:05:02 +0800871 // If a VPN applies to the UID, and the VPN is secure (i.e., not bypassable), then the UID can
872 // only select a different network if it has the ability to protect its sockets.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700873 VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid);
874 if (virtualNetwork && virtualNetwork->isSecure() &&
875 mProtectableUsers.find(uid) == mProtectableUsers.end()) {
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900876 return -EPERM;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700877 }
Ken Chen8738e1c2020-11-24 11:38:54 +0800878 // If the UID wants to use a physical network and it has a UID range that includes the UID, the
879 // UID has permission to use it regardless of whether the permission bits match.
Ken Chen4ea88462021-05-23 14:56:43 +0800880 if (network->isPhysical() && network->appliesToUser(uid, &subPriority)) {
Ken Chen8738e1c2020-11-24 11:38:54 +0800881 return 0;
882 }
Ken Chen55db3802021-03-30 13:47:49 +0800883 // Only apps that are configured as "no default network" can use the unreachable network.
884 if (network->isUnreachable()) {
Ken Chen4ea88462021-05-23 14:56:43 +0800885 return network->appliesToUser(uid, &subPriority) ? 0 : -EPERM;
Ken Chen55db3802021-03-30 13:47:49 +0800886 }
Ken Chenf875b522020-12-03 19:05:02 +0800887 // Check whether the UID's permission bits are sufficient to use the network.
Ken Chen8738e1c2020-11-24 11:38:54 +0800888 // Because the permission of the system default network is PERMISSION_NONE(0x0), apps can always
889 // pass the check here when using the system default network.
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700890 Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission();
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900891 return ((userPermission & networkPermission) == networkPermission) ? 0 : -EACCES;
Sreeram Ramachandran1011b492014-07-24 19:04:32 -0700892}
893
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700894int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination,
Tyler Wearfa94a272019-12-05 15:01:48 -0800895 const char* nexthop, enum RouteOperation op, bool legacy,
896 uid_t uid, int mtu) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800897 ScopedRLock lock(mRWLock);
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900898
899 if (!isValidNetworkLocked(netId)) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900900 ALOGE("no such netId %u", netId);
901 return -ENONET;
902 }
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900903 unsigned existingNetId = getNetworkForInterfaceLocked(interface);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900904 if (existingNetId == NETID_UNSET) {
905 ALOGE("interface %s not assigned to any netId", interface);
906 return -ENODEV;
907 }
908 if (existingNetId != netId) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700909 ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900910 return -ENOENT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700911 }
912
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700913 RouteController::TableType tableType;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700914 if (netId == LOCAL_NET_ID) {
915 tableType = RouteController::LOCAL_NETWORK;
916 } else if (legacy) {
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900917 if ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700918 tableType = RouteController::LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700919 } else {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700920 tableType = RouteController::LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700921 }
922 } else {
923 tableType = RouteController::INTERFACE;
924 }
925
Tyler Wearfa94a272019-12-05 15:01:48 -0800926 switch (op) {
927 case ROUTE_ADD:
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200928 return RouteController::addRoute(interface, destination, nexthop, tableType, mtu,
929 0 /* priority */);
Tyler Wearfa94a272019-12-05 15:01:48 -0800930 case ROUTE_UPDATE:
931 return RouteController::updateRoute(interface, destination, nexthop, tableType, mtu);
932 case ROUTE_REMOVE:
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200933 return RouteController::removeRoute(interface, destination, nexthop, tableType,
934 0 /* priority */);
Tyler Wearfa94a272019-12-05 15:01:48 -0800935 }
936 return -EINVAL;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700937}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700938
939int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) {
940 if (mDefaultNetId == NETID_UNSET) {
941 return 0;
942 }
943 Network* network = getNetworkLocked(mDefaultNetId);
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900944 if (!network) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700945 ALOGE("cannot find previously set default network with netId %u", mDefaultNetId);
946 return -ESRCH;
947 }
Ken Chen7ad712f2021-01-05 01:14:20 +0800948 if (!network->isPhysical()) {
Lorenzo Colitti738c93e2014-07-30 17:46:08 +0900949 ALOGE("inconceivable! default network must be a physical network");
950 return -EINVAL;
951 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700952 Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission();
953 for (const auto& physicalInterface : network->getInterfaces()) {
954 if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission,
955 add)) {
956 return ret;
957 }
958 }
959 return 0;
960}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900961
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900962void NetworkController::updateTcpSocketMonitorPolling() {
963 bool physicalNetworkExists = false;
964 for (const auto& entry : mNetworks) {
965 const auto& network = entry.second;
Ken Chen7ad712f2021-01-05 01:14:20 +0800966 if (network->isPhysical() && network->getNetId() >= MIN_NET_ID) {
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900967 physicalNetworkExists = true;
968 break;
969 }
970 }
971
972 if (physicalNetworkExists) {
973 android::net::gCtls->tcpSocketMonitor.resumePolling();
974 } else {
975 android::net::gCtls->tcpSocketMonitor.suspendPolling();
976 }
977}
978
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900979} // namespace android::net