blob: 3739d299a3029a34e93afef5cf31c8da86ed7ab2 [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#ifndef NETD_SERVER_NETWORK_CONTROLLER_H
18#define NETD_SERVER_NETWORK_CONTROLLER_H
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050019
Luke Huangd1ee4622018-06-29 13:49:58 +080020#include <android-base/thread_annotations.h>
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070021#include <android/multinetwork.h>
Luke Huangd1ee4622018-06-29 13:49:58 +080022
Luke Huangcfd04b22019-03-18 15:53:21 +080023
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070024#include "NetdConstants.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070025#include "Permission.h"
Luke Huangcfd04b22019-03-18 15:53:21 +080026#include "android/net/INetd.h"
Luke Huangb257d612019-03-14 21:19:13 +080027#include "netdutils/DumpWriter.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070028
Luke Huangd1ee4622018-06-29 13:49:58 +080029#include <sys/types.h>
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050030#include <list>
31#include <map>
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070032#include <set>
Luke Huangd1ee4622018-06-29 13:49:58 +080033#include <shared_mutex>
Rubin Xu6c00b612018-04-27 14:27:59 +010034#include <unordered_map>
35#include <unordered_set>
Sreeram Ramachandran72604072014-05-21 13:19:43 -070036#include <vector>
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050037
Lorenzo Colitti7035f222017-02-13 18:29:00 +090038struct android_net_context;
39
40namespace android {
41namespace net {
42
Luke Huangd1ee4622018-06-29 13:49:58 +080043typedef std::shared_lock<std::shared_mutex> ScopedRLock;
44typedef std::lock_guard<std::shared_mutex> ScopedWLock;
45
Lorenzo Colittiae8da9a2017-09-05 11:58:27 +090046constexpr uint32_t kHandleMagic = 0xcafed00d;
47
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070048// Utility to convert from netId to net_handle_t. Doing this here as opposed to exporting
49// from net.c as it may have NDK implications. Besides no conversion available in net.c for
50// obtaining handle given netId.
51// TODO: Use getnetidfromhandle() in net.c.
52static inline unsigned netHandleToNetId(net_handle_t fromNetHandle) {
53 const uint32_t k32BitMask = 0xffffffff;
54 // This value MUST be kept in sync with the corresponding value in
55 // the android.net.Network#getNetworkHandle() implementation.
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070056
57 // Check for minimum acceptable version of the API in the low bits.
58 if (fromNetHandle != NETWORK_UNSPECIFIED &&
59 (fromNetHandle & k32BitMask) != kHandleMagic) {
60 return 0;
61 }
62
63 return ((fromNetHandle >> (CHAR_BIT * sizeof(k32BitMask))) & k32BitMask);
64}
65
66// Utility to convert from nethandle to netid, keep in sync with getNetworkHandle
67// in Network.java.
68static inline net_handle_t netIdToNetHandle(unsigned fromNetId) {
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070069 if (!fromNetId) {
70 return NETWORK_UNSPECIFIED;
71 }
Lorenzo Colittiae8da9a2017-09-05 11:58:27 +090072 return (((net_handle_t)fromNetId << 32) | kHandleMagic);
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070073}
74
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070075class Network;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070076class UidRanges;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070077class VirtualNetwork;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070078
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050079/*
80 * Keeps track of default, per-pid, and per-uid-range network selection, as
81 * well as the mark associated with each network. Networks are identified
82 * by netid. In all set* commands netid == 0 means "unspecified" and is
83 * equivalent to clearing the mapping.
84 */
85class NetworkController {
86public:
Luke Huangcfd04b22019-03-18 15:53:21 +080087 // NetIds 52..98 are reserved for future use.
Luke Huangbbfd3832019-04-25 20:23:02 +080088 static constexpr int MIN_OEM_ID = 1;
89 static constexpr int MAX_OEM_ID = 50;
90 static constexpr int LOCAL_NET_ID = INetd::LOCAL_NET_ID;
91 static constexpr int DUMMY_NET_ID = 51;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070092
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070093 NetworkController();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050094
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050095 unsigned getDefaultNetwork() const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070096 int setDefaultNetwork(unsigned netId) WARN_UNUSED_RESULT;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050097
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070098 unsigned getNetworkForUser(uid_t uid) const;
99 unsigned getNetworkForConnect(uid_t uid) const;
Erik Klinecea2d342015-06-25 18:24:46 +0900100 void getNetworkContext(unsigned netId, uid_t uid, struct android_net_context* netcontext) const;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700101 unsigned getNetworkForInterface(const char* interface) const;
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700102 bool isVirtualNetwork(unsigned netId) const;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500103
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700104 int createPhysicalNetwork(unsigned netId, Permission permission) WARN_UNUSED_RESULT;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700105 int createPhysicalOemNetwork(Permission permission, unsigned *netId) WARN_UNUSED_RESULT;
cken67cd14c2018-12-05 17:26:59 +0900106 int createVirtualNetwork(unsigned netId, bool secure) WARN_UNUSED_RESULT;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700107 int destroyNetwork(unsigned netId) WARN_UNUSED_RESULT;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700108
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700109 int addInterfaceToNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
110 int removeInterfaceFromNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
111
112 Permission getPermissionForUser(uid_t uid) const;
113 void setPermissionForUsers(Permission permission, const std::vector<uid_t>& uids);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900114 int checkUserNetworkAccess(uid_t uid, unsigned netId) const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700115 int setPermissionForNetworks(Permission permission,
116 const std::vector<unsigned>& netIds) WARN_UNUSED_RESULT;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700117
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700118 int addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT;
119 int removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT;
120
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700121 // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900122 // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address.
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700123 //
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700124 // Routes are added to tables determined by the interface, so only |interface| is actually used.
125 // |netId| is given only to sanity check that the interface has the correct netId.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900126 int addRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700127 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900128 int removeRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700129 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700130
Rubin Xu6c00b612018-04-27 14:27:59 +0100131 // Notes that the specified address has appeared on the specified interface.
132 void addInterfaceAddress(unsigned ifIndex, const char* address);
133 // Notes that the specified address has been removed from the specified interface.
134 // Returns true if we should destroy sockets on this address.
135 bool removeInterfaceAddress(unsigned ifIndex, const char* address);
136
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700137 bool canProtect(uid_t uid) const;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700138 void allowProtect(const std::vector<uid_t>& uids);
139 void denyProtect(const std::vector<uid_t>& uids);
140
Luke Huangb257d612019-03-14 21:19:13 +0800141 void dump(netdutils::DumpWriter& dw);
Erik Kline2d3a1632016-03-15 16:33:48 +0900142
Luke Huangb257d612019-03-14 21:19:13 +0800143 private:
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700144 bool isValidNetworkLocked(unsigned netId) const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700145 Network* getNetworkLocked(unsigned netId) const;
Bernie Innocenti51e917e2019-08-21 12:47:32 +0900146
147 // Sets |*netId| to an appropriate NetId to use for DNS for the given user. Call with |*netId|
148 // set to a non-NETID_UNSET value if the user already has indicated a preference. Returns the
149 // fwmark value to set on the socket when performing the DNS request.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900150 uint32_t getNetworkForDnsLocked(unsigned* netId, uid_t uid) const;
Bernie Innocenti51e917e2019-08-21 12:47:32 +0900151
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900152 unsigned getNetworkForUserLocked(uid_t uid) const;
153 unsigned getNetworkForConnectLocked(uid_t uid) const;
154 unsigned getNetworkForInterfaceLocked(const char* interface) const;
155 bool canProtectLocked(uid_t uid) const;
Rubin Xu6c00b612018-04-27 14:27:59 +0100156 bool isVirtualNetworkLocked(unsigned netId) const;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700157 VirtualNetwork* getVirtualNetworkForUserLocked(uid_t uid) const;
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700158 Permission getPermissionForUserLocked(uid_t uid) const;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900159 int checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700160 int createPhysicalNetworkLocked(unsigned netId, Permission permission) WARN_UNUSED_RESULT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700161
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900162 int modifyRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700163 const char* nexthop, bool add, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700164 int modifyFallthroughLocked(unsigned vpnNetId, bool add) WARN_UNUSED_RESULT;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900165 void updateTcpSocketMonitorPolling();
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700166
167 class DelegateImpl;
168 DelegateImpl* const mDelegateImpl;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700169
Rubin Xu6c00b612018-04-27 14:27:59 +0100170 // mRWLock guards all accesses to mDefaultNetId, mNetworks, mUsers, mProtectableUsers,
171 // mIfindexToLastNetId and mAddressToIfindices.
Luke Huangd1ee4622018-06-29 13:49:58 +0800172 mutable std::shared_mutex mRWLock;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500173 unsigned mDefaultNetId;
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700174 std::map<unsigned, Network*> mNetworks; // Map keys are NetIds.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700175 std::map<uid_t, Permission> mUsers;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700176 std::set<uid_t> mProtectableUsers;
Rubin Xu6c00b612018-04-27 14:27:59 +0100177 // Map interface (ifIndex) to its current NetId, or the last NetId if the interface was removed
178 // from the network and not added to another network. This state facilitates the interface to
179 // NetId lookup during RTM_DELADDR (NetworkController::removeInterfaceAddress), when the
180 // interface in question might already have been removed from the network.
181 // An interface is added to this map when it is added to a network and removed from this map
182 // when its network is destroyed.
183 std::unordered_map<unsigned, unsigned> mIfindexToLastNetId;
184 // Map IP address to the list of active interfaces (ifIndex) that have that address.
185 // Also contains IP addresses configured on interfaces that have not been added to any network.
186 // TODO: Does not track IP addresses present when netd is started or restarts after a crash.
187 // This is not a problem for its intended use (tracking IP addresses on VPN interfaces), but
188 // we should fix it.
189 std::unordered_map<std::string, std::unordered_set<unsigned>> mAddressToIfindices;
190
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500191};
192
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900193} // namespace net
194} // namespace android
195
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700196#endif // NETD_SERVER_NETWORK_CONTROLLER_H