blob: 5e7af809f73a074fdb9bdf1f1111d1972d0c318e [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
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070020#include <android/multinetwork.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070021#include "NetdConstants.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070022#include "Permission.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070023
Sreeram Ramachandran72604072014-05-21 13:19:43 -070024#include "utils/RWLock.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070025
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050026#include <list>
27#include <map>
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070028#include <set>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070029#include <sys/types.h>
Rubin Xuacbb6b72018-04-27 14:27:59 +010030#include <unordered_map>
31#include <unordered_set>
Sreeram Ramachandran72604072014-05-21 13:19:43 -070032#include <vector>
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050033
Lorenzo Colitti7035f222017-02-13 18:29:00 +090034struct android_net_context;
35
36namespace android {
37namespace net {
38
Lorenzo Colittiae8da9a2017-09-05 11:58:27 +090039constexpr uint32_t kHandleMagic = 0xcafed00d;
40
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070041// Utility to convert from netId to net_handle_t. Doing this here as opposed to exporting
42// from net.c as it may have NDK implications. Besides no conversion available in net.c for
43// obtaining handle given netId.
44// TODO: Use getnetidfromhandle() in net.c.
45static inline unsigned netHandleToNetId(net_handle_t fromNetHandle) {
46 const uint32_t k32BitMask = 0xffffffff;
47 // This value MUST be kept in sync with the corresponding value in
48 // the android.net.Network#getNetworkHandle() implementation.
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070049
50 // Check for minimum acceptable version of the API in the low bits.
51 if (fromNetHandle != NETWORK_UNSPECIFIED &&
52 (fromNetHandle & k32BitMask) != kHandleMagic) {
53 return 0;
54 }
55
56 return ((fromNetHandle >> (CHAR_BIT * sizeof(k32BitMask))) & k32BitMask);
57}
58
59// Utility to convert from nethandle to netid, keep in sync with getNetworkHandle
60// in Network.java.
61static inline net_handle_t netIdToNetHandle(unsigned fromNetId) {
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070062 if (!fromNetId) {
63 return NETWORK_UNSPECIFIED;
64 }
Lorenzo Colittiae8da9a2017-09-05 11:58:27 +090065 return (((net_handle_t)fromNetId << 32) | kHandleMagic);
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070066}
67
Erik Kline2d3a1632016-03-15 16:33:48 +090068class DumpWriter;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070069class Network;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070070class UidRanges;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070071class VirtualNetwork;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070072
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050073/*
74 * Keeps track of default, per-pid, and per-uid-range network selection, as
75 * well as the mark associated with each network. Networks are identified
76 * by netid. In all set* commands netid == 0 means "unspecified" and is
77 * equivalent to clearing the mapping.
78 */
79class NetworkController {
80public:
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070081 static const unsigned MIN_OEM_ID;
82 static const unsigned MAX_OEM_ID;
83 static const unsigned LOCAL_NET_ID;
Lorenzo Colitti36679362015-02-25 10:26:19 +090084 static const unsigned DUMMY_NET_ID;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070085
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070086 NetworkController();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050087
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050088 unsigned getDefaultNetwork() const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070089 int setDefaultNetwork(unsigned netId) WARN_UNUSED_RESULT;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050090
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070091 // Sets |*netId| to an appropriate NetId to use for DNS for the given user. Call with |*netId|
92 // set to a non-NETID_UNSET value if the user already has indicated a preference. Returns the
93 // fwmark value to set on the socket when performing the DNS request.
94 uint32_t getNetworkForDns(unsigned* netId, uid_t uid) const;
95 unsigned getNetworkForUser(uid_t uid) const;
96 unsigned getNetworkForConnect(uid_t uid) const;
Erik Klinecea2d342015-06-25 18:24:46 +090097 void getNetworkContext(unsigned netId, uid_t uid, struct android_net_context* netcontext) const;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070098 unsigned getNetworkForInterface(const char* interface) const;
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -070099 bool isVirtualNetwork(unsigned netId) const;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500100
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700101 int createPhysicalNetwork(unsigned netId, Permission permission) WARN_UNUSED_RESULT;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700102 int createPhysicalOemNetwork(Permission permission, unsigned *netId) WARN_UNUSED_RESULT;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700103 int createVirtualNetwork(unsigned netId, bool hasDns, bool secure) WARN_UNUSED_RESULT;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700104 int destroyNetwork(unsigned netId) WARN_UNUSED_RESULT;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700105
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700106 int addInterfaceToNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
107 int removeInterfaceFromNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
108
109 Permission getPermissionForUser(uid_t uid) const;
110 void setPermissionForUsers(Permission permission, const std::vector<uid_t>& uids);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900111 int checkUserNetworkAccess(uid_t uid, unsigned netId) const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700112 int setPermissionForNetworks(Permission permission,
113 const std::vector<unsigned>& netIds) WARN_UNUSED_RESULT;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700114
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700115 int addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT;
116 int removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT;
117
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700118 // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900119 // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address.
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700120 //
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700121 // Routes are added to tables determined by the interface, so only |interface| is actually used.
122 // |netId| is given only to sanity check that the interface has the correct netId.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900123 int addRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700124 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900125 int removeRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700126 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700127
Rubin Xuacbb6b72018-04-27 14:27:59 +0100128 // Notes that the specified address has appeared on the specified interface.
129 void addInterfaceAddress(unsigned ifIndex, const char* address);
130 // Notes that the specified address has been removed from the specified interface.
131 // Returns true if we should destroy sockets on this address.
132 bool removeInterfaceAddress(unsigned ifIndex, const char* address);
133
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700134 bool canProtect(uid_t uid) const;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700135 void allowProtect(const std::vector<uid_t>& uids);
136 void denyProtect(const std::vector<uid_t>& uids);
137
Erik Kline2d3a1632016-03-15 16:33:48 +0900138 void dump(DumpWriter& dw);
139
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500140private:
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700141 bool isValidNetworkLocked(unsigned netId) const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700142 Network* getNetworkLocked(unsigned netId) const;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900143 uint32_t getNetworkForDnsLocked(unsigned* netId, uid_t uid) const;
144 unsigned getNetworkForUserLocked(uid_t uid) const;
145 unsigned getNetworkForConnectLocked(uid_t uid) const;
146 unsigned getNetworkForInterfaceLocked(const char* interface) const;
147 bool canProtectLocked(uid_t uid) const;
Rubin Xuacbb6b72018-04-27 14:27:59 +0100148 bool isVirtualNetworkLocked(unsigned netId) const;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900149
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700150 VirtualNetwork* getVirtualNetworkForUserLocked(uid_t uid) const;
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700151 Permission getPermissionForUserLocked(uid_t uid) const;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900152 int checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700153 int createPhysicalNetworkLocked(unsigned netId, Permission permission) WARN_UNUSED_RESULT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700154
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900155 int modifyRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700156 const char* nexthop, bool add, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700157 int modifyFallthroughLocked(unsigned vpnNetId, bool add) WARN_UNUSED_RESULT;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900158 void updateTcpSocketMonitorPolling();
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700159
160 class DelegateImpl;
161 DelegateImpl* const mDelegateImpl;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700162
Rubin Xuacbb6b72018-04-27 14:27:59 +0100163 // mRWLock guards all accesses to mDefaultNetId, mNetworks, mUsers, mProtectableUsers,
164 // mIfindexToLastNetId and mAddressToIfindices.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500165 mutable android::RWLock mRWLock;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500166 unsigned mDefaultNetId;
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700167 std::map<unsigned, Network*> mNetworks; // Map keys are NetIds.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700168 std::map<uid_t, Permission> mUsers;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700169 std::set<uid_t> mProtectableUsers;
Rubin Xuacbb6b72018-04-27 14:27:59 +0100170 // Map interface (ifIndex) to its current NetId, or the last NetId if the interface was removed
171 // from the network and not added to another network. This state facilitates the interface to
172 // NetId lookup during RTM_DELADDR (NetworkController::removeInterfaceAddress), when the
173 // interface in question might already have been removed from the network.
174 // An interface is added to this map when it is added to a network and removed from this map
175 // when its network is destroyed.
176 std::unordered_map<unsigned, unsigned> mIfindexToLastNetId;
177 // Map IP address to the list of active interfaces (ifIndex) that have that address.
178 // Also contains IP addresses configured on interfaces that have not been added to any network.
179 // TODO: Does not track IP addresses present when netd is started or restarts after a crash.
180 // This is not a problem for its intended use (tracking IP addresses on VPN interfaces), but
181 // we should fix it.
182 std::unordered_map<std::string, std::unordered_set<unsigned>> mAddressToIfindices;
183
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500184};
185
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900186} // namespace net
187} // namespace android
188
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700189#endif // NETD_SERVER_NETWORK_CONTROLLER_H