Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 17 | #pragma once |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 18 | |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 19 | #include <android-base/thread_annotations.h> |
Niranjan Pendharkar | 4c18bd9 | 2017-07-24 09:54:07 -0700 | [diff] [blame] | 20 | #include <android/multinetwork.h> |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 21 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 22 | #include "NetdConstants.h" |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 23 | #include "Permission.h" |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 24 | #include "PhysicalNetwork.h" |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 25 | #include "UnreachableNetwork.h" |
Luke Huang | cfd04b2 | 2019-03-18 15:53:21 +0800 | [diff] [blame] | 26 | #include "android/net/INetd.h" |
Luke Huang | b257d61 | 2019-03-14 21:19:13 +0800 | [diff] [blame] | 27 | #include "netdutils/DumpWriter.h" |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 28 | |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 29 | #include <sys/types.h> |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 30 | #include <list> |
| 31 | #include <map> |
Sreeram Ramachandran | 89dad01 | 2014-07-02 10:09:49 -0700 | [diff] [blame] | 32 | #include <set> |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 33 | #include <shared_mutex> |
Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 34 | #include <unordered_map> |
| 35 | #include <unordered_set> |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 36 | #include <vector> |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 37 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 38 | struct android_net_context; |
| 39 | |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 40 | namespace android::net { |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 41 | |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 42 | typedef std::shared_lock<std::shared_mutex> ScopedRLock; |
| 43 | typedef std::lock_guard<std::shared_mutex> ScopedWLock; |
| 44 | |
Lorenzo Colitti | ae8da9a | 2017-09-05 11:58:27 +0900 | [diff] [blame] | 45 | constexpr uint32_t kHandleMagic = 0xcafed00d; |
| 46 | |
Niranjan Pendharkar | 4c18bd9 | 2017-07-24 09:54:07 -0700 | [diff] [blame] | 47 | // Utility to convert from netId to net_handle_t. Doing this here as opposed to exporting |
| 48 | // from net.c as it may have NDK implications. Besides no conversion available in net.c for |
| 49 | // obtaining handle given netId. |
| 50 | // TODO: Use getnetidfromhandle() in net.c. |
| 51 | static inline unsigned netHandleToNetId(net_handle_t fromNetHandle) { |
| 52 | const uint32_t k32BitMask = 0xffffffff; |
| 53 | // This value MUST be kept in sync with the corresponding value in |
| 54 | // the android.net.Network#getNetworkHandle() implementation. |
Niranjan Pendharkar | 4c18bd9 | 2017-07-24 09:54:07 -0700 | [diff] [blame] | 55 | |
| 56 | // Check for minimum acceptable version of the API in the low bits. |
| 57 | if (fromNetHandle != NETWORK_UNSPECIFIED && |
| 58 | (fromNetHandle & k32BitMask) != kHandleMagic) { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | return ((fromNetHandle >> (CHAR_BIT * sizeof(k32BitMask))) & k32BitMask); |
| 63 | } |
| 64 | |
| 65 | // Utility to convert from nethandle to netid, keep in sync with getNetworkHandle |
| 66 | // in Network.java. |
| 67 | static inline net_handle_t netIdToNetHandle(unsigned fromNetId) { |
Niranjan Pendharkar | 4c18bd9 | 2017-07-24 09:54:07 -0700 | [diff] [blame] | 68 | if (!fromNetId) { |
| 69 | return NETWORK_UNSPECIFIED; |
| 70 | } |
Lorenzo Colitti | ae8da9a | 2017-09-05 11:58:27 +0900 | [diff] [blame] | 71 | return (((net_handle_t)fromNetId << 32) | kHandleMagic); |
Niranjan Pendharkar | 4c18bd9 | 2017-07-24 09:54:07 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 74 | class Network; |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 75 | class UidRanges; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 76 | class VirtualNetwork; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 77 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 78 | /* |
| 79 | * Keeps track of default, per-pid, and per-uid-range network selection, as |
| 80 | * well as the mark associated with each network. Networks are identified |
| 81 | * by netid. In all set* commands netid == 0 means "unspecified" and is |
| 82 | * equivalent to clearing the mapping. |
| 83 | */ |
| 84 | class NetworkController { |
| 85 | public: |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 86 | // NetIds 53..98 are reserved for future use. |
Luke Huang | bbfd383 | 2019-04-25 20:23:02 +0800 | [diff] [blame] | 87 | static constexpr int MIN_OEM_ID = 1; |
| 88 | static constexpr int MAX_OEM_ID = 50; |
| 89 | static constexpr int LOCAL_NET_ID = INetd::LOCAL_NET_ID; |
Chalard Jean | d96b0f2 | 2021-01-20 14:16:06 +0900 | [diff] [blame] | 90 | static constexpr int DUMMY_NET_ID = INetd::DUMMY_NET_ID; |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 91 | static constexpr int UNREACHABLE_NET_ID = INetd::UNREACHABLE_NET_ID; |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 92 | |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 93 | // Route mode for modify route |
| 94 | enum RouteOperation { ROUTE_ADD, ROUTE_UPDATE, ROUTE_REMOVE }; |
| 95 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 96 | NetworkController(); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 97 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 98 | unsigned getDefaultNetwork() const; |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 99 | [[nodiscard]] int setDefaultNetwork(unsigned netId); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 100 | |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 101 | unsigned getNetworkForUser(uid_t uid) const; |
| 102 | unsigned getNetworkForConnect(uid_t uid) const; |
Erik Kline | cea2d34 | 2015-06-25 18:24:46 +0900 | [diff] [blame] | 103 | void getNetworkContext(unsigned netId, uid_t uid, struct android_net_context* netcontext) const; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 104 | unsigned getNetworkForInterface(const char* interface) const; |
Sreeram Ramachandran | 070b2d2 | 2014-07-11 17:06:12 -0700 | [diff] [blame] | 105 | bool isVirtualNetwork(unsigned netId) const; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 106 | |
Chalard Jean | 0484514 | 2022-12-02 19:39:36 +0900 | [diff] [blame] | 107 | [[nodiscard]] int createPhysicalNetwork(unsigned netId, Permission permission, bool local); |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 108 | [[nodiscard]] int createPhysicalOemNetwork(Permission permission, unsigned* netId); |
Chiachang Wang | 2b0abee | 2022-01-12 10:03:17 +0800 | [diff] [blame] | 109 | [[nodiscard]] int createVirtualNetwork(unsigned netId, bool secure, NativeVpnType vpnType, |
| 110 | bool excludeLocalRoutes); |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 111 | [[nodiscard]] int destroyNetwork(unsigned netId); |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 112 | |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 113 | [[nodiscard]] int addInterfaceToNetwork(unsigned netId, const char* interface); |
| 114 | [[nodiscard]] int removeInterfaceFromNetwork(unsigned netId, const char* interface); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 115 | |
| 116 | Permission getPermissionForUser(uid_t uid) const; |
| 117 | void setPermissionForUsers(Permission permission, const std::vector<uid_t>& uids); |
Lorenzo Colitti | a1067c8 | 2014-10-02 22:47:41 +0900 | [diff] [blame] | 118 | int checkUserNetworkAccess(uid_t uid, unsigned netId) const; |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 119 | [[nodiscard]] int setPermissionForNetworks(Permission permission, |
| 120 | const std::vector<unsigned>& netIds); |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 121 | |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 122 | [[nodiscard]] int addUsersToNetwork(unsigned netId, const UidRanges& uidRanges, |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 123 | int32_t subPriority); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 124 | [[nodiscard]] int removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges, |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 125 | int32_t subPriority); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 126 | |
Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 127 | // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a |
Lorenzo Colitti | 4c95a12 | 2014-09-18 16:01:50 +0900 | [diff] [blame] | 128 | // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address. |
Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 129 | // |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 130 | // Routes are added to tables determined by the interface, so only |interface| is actually used. |
| 131 | // |netId| is given only to sanity check that the interface has the correct netId. |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 132 | [[nodiscard]] int addRoute(unsigned netId, const char* interface, const char* destination, |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 133 | const char* nexthop, bool legacy, uid_t uid, int mtu); |
| 134 | [[nodiscard]] int updateRoute(unsigned netId, const char* interface, const char* destination, |
| 135 | const char* nexthop, bool legacy, uid_t uid, int mtu); |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 136 | [[nodiscard]] int removeRoute(unsigned netId, const char* interface, const char* destination, |
| 137 | const char* nexthop, bool legacy, uid_t uid); |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 138 | |
Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 139 | // Notes that the specified address has appeared on the specified interface. |
| 140 | void addInterfaceAddress(unsigned ifIndex, const char* address); |
| 141 | // Notes that the specified address has been removed from the specified interface. |
| 142 | // Returns true if we should destroy sockets on this address. |
| 143 | bool removeInterfaceAddress(unsigned ifIndex, const char* address); |
| 144 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 145 | bool canProtect(uid_t uid) const; |
Sreeram Ramachandran | 89dad01 | 2014-07-02 10:09:49 -0700 | [diff] [blame] | 146 | void allowProtect(const std::vector<uid_t>& uids); |
| 147 | void denyProtect(const std::vector<uid_t>& uids); |
| 148 | |
Luke Huang | b257d61 | 2019-03-14 21:19:13 +0800 | [diff] [blame] | 149 | void dump(netdutils::DumpWriter& dw); |
Ken Chen | 04ee609 | 2022-12-26 17:35:33 +0800 | [diff] [blame] | 150 | int setNetworkAllowlist(const std::vector<netd::aidl::NativeUidRangeConfig>& rangeConfigs); |
Ken Chen | e22ac8d | 2022-12-24 09:24:56 +0800 | [diff] [blame] | 151 | bool isUidAllowed(unsigned netId, uid_t uid) const; |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 152 | |
Luke Huang | b257d61 | 2019-03-14 21:19:13 +0800 | [diff] [blame] | 153 | private: |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 154 | bool isValidNetworkLocked(unsigned netId) const; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 155 | Network* getNetworkLocked(unsigned netId) const; |
Bernie Innocenti | 51e917e | 2019-08-21 12:47:32 +0900 | [diff] [blame] | 156 | |
| 157 | // Sets |*netId| to an appropriate NetId to use for DNS for the given user. Call with |*netId| |
| 158 | // set to a non-NETID_UNSET value if the user already has indicated a preference. Returns the |
| 159 | // fwmark value to set on the socket when performing the DNS request. |
Lorenzo Colitti | bbd0aff | 2016-12-15 22:53:24 +0900 | [diff] [blame] | 160 | uint32_t getNetworkForDnsLocked(unsigned* netId, uid_t uid) const; |
Lorenzo Colitti | bbd0aff | 2016-12-15 22:53:24 +0900 | [diff] [blame] | 161 | unsigned getNetworkForConnectLocked(uid_t uid) const; |
| 162 | unsigned getNetworkForInterfaceLocked(const char* interface) const; |
| 163 | bool canProtectLocked(uid_t uid) const; |
Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 164 | bool isVirtualNetworkLocked(unsigned netId) const; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 165 | VirtualNetwork* getVirtualNetworkForUserLocked(uid_t uid) const; |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 166 | Network* getPhysicalOrUnreachableNetworkForUserLocked(uid_t uid) const; |
Sreeram Ramachandran | ed4bd1f | 2014-07-05 12:31:05 -0700 | [diff] [blame] | 167 | Permission getPermissionForUserLocked(uid_t uid) const; |
Lorenzo Colitti | a1067c8 | 2014-10-02 22:47:41 +0900 | [diff] [blame] | 168 | int checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const; |
Chalard Jean | 0484514 | 2022-12-02 19:39:36 +0900 | [diff] [blame] | 169 | [[nodiscard]] int createPhysicalNetworkLocked(unsigned netId, Permission permission, |
| 170 | bool local); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 171 | |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 172 | [[nodiscard]] int modifyRoute(unsigned netId, const char* interface, const char* destination, |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 173 | const char* nexthop, RouteOperation op, bool legacy, uid_t uid, |
| 174 | int mtu); |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 175 | [[nodiscard]] int modifyFallthroughLocked(unsigned vpnNetId, bool add); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 176 | void updateTcpSocketMonitorPolling(); |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 177 | void clearAllowedUidsForAllNetworksLocked(); |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 178 | |
| 179 | class DelegateImpl; |
| 180 | DelegateImpl* const mDelegateImpl; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 181 | |
Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 182 | // mRWLock guards all accesses to mDefaultNetId, mNetworks, mUsers, mProtectableUsers, |
| 183 | // mIfindexToLastNetId and mAddressToIfindices. |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 184 | mutable std::shared_mutex mRWLock; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 185 | unsigned mDefaultNetId; |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 186 | std::map<unsigned, Network*> mNetworks; // Map keys are NetIds. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 187 | std::map<uid_t, Permission> mUsers; |
Sreeram Ramachandran | 89dad01 | 2014-07-02 10:09:49 -0700 | [diff] [blame] | 188 | std::set<uid_t> mProtectableUsers; |
Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 189 | // Map interface (ifIndex) to its current NetId, or the last NetId if the interface was removed |
| 190 | // from the network and not added to another network. This state facilitates the interface to |
| 191 | // NetId lookup during RTM_DELADDR (NetworkController::removeInterfaceAddress), when the |
| 192 | // interface in question might already have been removed from the network. |
| 193 | // An interface is added to this map when it is added to a network and removed from this map |
| 194 | // when its network is destroyed. |
| 195 | std::unordered_map<unsigned, unsigned> mIfindexToLastNetId; |
| 196 | // Map IP address to the list of active interfaces (ifIndex) that have that address. |
| 197 | // Also contains IP addresses configured on interfaces that have not been added to any network. |
| 198 | // TODO: Does not track IP addresses present when netd is started or restarts after a crash. |
| 199 | // This is not a problem for its intended use (tracking IP addresses on VPN interfaces), but |
| 200 | // we should fix it. |
| 201 | std::unordered_map<std::string, std::unordered_set<unsigned>> mAddressToIfindices; |
| 202 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 203 | }; |
| 204 | |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 205 | } // namespace android::net |