blob: e0abde09bd86d153b17fd302d0faecc01d50b512 [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
Bernie Innocenti762dcf42019-06-14 19:52:49 +090017#pragma once
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050018
Luke Huangd1ee4622018-06-29 13:49:58 +080019#include <android-base/thread_annotations.h>
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070020#include <android/multinetwork.h>
Luke Huangd1ee4622018-06-29 13:49:58 +080021
Luke Huangcfd04b22019-03-18 15:53:21 +080022
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070023#include "NetdConstants.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070024#include "Permission.h"
Luke Huangcfd04b22019-03-18 15:53:21 +080025#include "android/net/INetd.h"
Luke Huangb257d612019-03-14 21:19:13 +080026#include "netdutils/DumpWriter.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070027
Luke Huangd1ee4622018-06-29 13:49:58 +080028#include <sys/types.h>
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050029#include <list>
30#include <map>
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070031#include <set>
Luke Huangd1ee4622018-06-29 13:49:58 +080032#include <shared_mutex>
Rubin Xu6c00b612018-04-27 14:27:59 +010033#include <unordered_map>
34#include <unordered_set>
Sreeram Ramachandran72604072014-05-21 13:19:43 -070035#include <vector>
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050036
Lorenzo Colitti7035f222017-02-13 18:29:00 +090037struct android_net_context;
38
Bernie Innocenti762dcf42019-06-14 19:52:49 +090039namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090040
Luke Huangd1ee4622018-06-29 13:49:58 +080041typedef std::shared_lock<std::shared_mutex> ScopedRLock;
42typedef std::lock_guard<std::shared_mutex> ScopedWLock;
43
Lorenzo Colittiae8da9a2017-09-05 11:58:27 +090044constexpr uint32_t kHandleMagic = 0xcafed00d;
45
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070046// Utility to convert from netId to net_handle_t. Doing this here as opposed to exporting
47// from net.c as it may have NDK implications. Besides no conversion available in net.c for
48// obtaining handle given netId.
49// TODO: Use getnetidfromhandle() in net.c.
50static inline unsigned netHandleToNetId(net_handle_t fromNetHandle) {
51 const uint32_t k32BitMask = 0xffffffff;
52 // This value MUST be kept in sync with the corresponding value in
53 // the android.net.Network#getNetworkHandle() implementation.
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070054
55 // Check for minimum acceptable version of the API in the low bits.
56 if (fromNetHandle != NETWORK_UNSPECIFIED &&
57 (fromNetHandle & k32BitMask) != kHandleMagic) {
58 return 0;
59 }
60
61 return ((fromNetHandle >> (CHAR_BIT * sizeof(k32BitMask))) & k32BitMask);
62}
63
64// Utility to convert from nethandle to netid, keep in sync with getNetworkHandle
65// in Network.java.
66static inline net_handle_t netIdToNetHandle(unsigned fromNetId) {
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070067 if (!fromNetId) {
68 return NETWORK_UNSPECIFIED;
69 }
Lorenzo Colittiae8da9a2017-09-05 11:58:27 +090070 return (((net_handle_t)fromNetId << 32) | kHandleMagic);
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070071}
72
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070073class Network;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070074class UidRanges;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070075class VirtualNetwork;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070076
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050077/*
78 * Keeps track of default, per-pid, and per-uid-range network selection, as
79 * well as the mark associated with each network. Networks are identified
80 * by netid. In all set* commands netid == 0 means "unspecified" and is
81 * equivalent to clearing the mapping.
82 */
83class NetworkController {
84public:
Luke Huangcfd04b22019-03-18 15:53:21 +080085 // NetIds 52..98 are reserved for future use.
Luke Huangbbfd3832019-04-25 20:23:02 +080086 static constexpr int MIN_OEM_ID = 1;
87 static constexpr int MAX_OEM_ID = 50;
88 static constexpr int LOCAL_NET_ID = INetd::LOCAL_NET_ID;
89 static constexpr int DUMMY_NET_ID = 51;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070090
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070091 NetworkController();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050092
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050093 unsigned getDefaultNetwork() const;
Bernie Innocenti762dcf42019-06-14 19:52:49 +090094 [[nodiscard]] int setDefaultNetwork(unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050095
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070096 unsigned getNetworkForUser(uid_t uid) const;
97 unsigned getNetworkForConnect(uid_t uid) const;
Erik Klinecea2d342015-06-25 18:24:46 +090098 void getNetworkContext(unsigned netId, uid_t uid, struct android_net_context* netcontext) const;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070099 unsigned getNetworkForInterface(const char* interface) const;
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -0700100 bool isVirtualNetwork(unsigned netId) const;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500101
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900102 [[nodiscard]] int createPhysicalNetwork(unsigned netId, Permission permission);
103 [[nodiscard]] int createPhysicalOemNetwork(Permission permission, unsigned* netId);
104 [[nodiscard]] int createVirtualNetwork(unsigned netId, bool secure);
105 [[nodiscard]] int destroyNetwork(unsigned netId);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700106
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900107 [[nodiscard]] int addInterfaceToNetwork(unsigned netId, const char* interface);
108 [[nodiscard]] int removeInterfaceFromNetwork(unsigned netId, const char* interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700109
110 Permission getPermissionForUser(uid_t uid) const;
111 void setPermissionForUsers(Permission permission, const std::vector<uid_t>& uids);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900112 int checkUserNetworkAccess(uid_t uid, unsigned netId) const;
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900113 [[nodiscard]] int setPermissionForNetworks(Permission permission,
114 const std::vector<unsigned>& netIds);
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700115
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900116 [[nodiscard]] int addUsersToNetwork(unsigned netId, const UidRanges& uidRanges);
117 [[nodiscard]] int removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700118
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700119 // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900120 // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address.
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700121 //
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700122 // Routes are added to tables determined by the interface, so only |interface| is actually used.
123 // |netId| is given only to sanity check that the interface has the correct netId.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900124 [[nodiscard]] int addRoute(unsigned netId, const char* interface, const char* destination,
125 const char* nexthop, bool legacy, uid_t uid);
126 [[nodiscard]] int removeRoute(unsigned netId, const char* interface, const char* destination,
127 const char* nexthop, bool legacy, uid_t uid);
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700128
Rubin Xu6c00b612018-04-27 14:27:59 +0100129 // Notes that the specified address has appeared on the specified interface.
130 void addInterfaceAddress(unsigned ifIndex, const char* address);
131 // Notes that the specified address has been removed from the specified interface.
132 // Returns true if we should destroy sockets on this address.
133 bool removeInterfaceAddress(unsigned ifIndex, const char* address);
134
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700135 bool canProtect(uid_t uid) const;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700136 void allowProtect(const std::vector<uid_t>& uids);
137 void denyProtect(const std::vector<uid_t>& uids);
138
Luke Huangb257d612019-03-14 21:19:13 +0800139 void dump(netdutils::DumpWriter& dw);
Erik Kline2d3a1632016-03-15 16:33:48 +0900140
Luke Huangb257d612019-03-14 21:19:13 +0800141 private:
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700142 bool isValidNetworkLocked(unsigned netId) const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700143 Network* getNetworkLocked(unsigned netId) const;
Bernie Innocenti51e917e2019-08-21 12:47:32 +0900144
145 // Sets |*netId| to an appropriate NetId to use for DNS for the given user. Call with |*netId|
146 // set to a non-NETID_UNSET value if the user already has indicated a preference. Returns the
147 // fwmark value to set on the socket when performing the DNS request.
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900148 uint32_t getNetworkForDnsLocked(unsigned* netId, uid_t uid) const;
Bernie Innocenti51e917e2019-08-21 12:47:32 +0900149
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900150 unsigned getNetworkForUserLocked(uid_t uid) const;
151 unsigned getNetworkForConnectLocked(uid_t uid) const;
152 unsigned getNetworkForInterfaceLocked(const char* interface) const;
153 bool canProtectLocked(uid_t uid) const;
Rubin Xu6c00b612018-04-27 14:27:59 +0100154 bool isVirtualNetworkLocked(unsigned netId) const;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700155 VirtualNetwork* getVirtualNetworkForUserLocked(uid_t uid) const;
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700156 Permission getPermissionForUserLocked(uid_t uid) const;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900157 int checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const;
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900158 [[nodiscard]] int createPhysicalNetworkLocked(unsigned netId, Permission permission);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700159
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900160 [[nodiscard]] int modifyRoute(unsigned netId, const char* interface, const char* destination,
161 const char* nexthop, bool add, bool legacy, uid_t uid);
162 [[nodiscard]] int modifyFallthroughLocked(unsigned vpnNetId, bool add);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +0900163 void updateTcpSocketMonitorPolling();
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700164
165 class DelegateImpl;
166 DelegateImpl* const mDelegateImpl;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700167
Rubin Xu6c00b612018-04-27 14:27:59 +0100168 // mRWLock guards all accesses to mDefaultNetId, mNetworks, mUsers, mProtectableUsers,
169 // mIfindexToLastNetId and mAddressToIfindices.
Luke Huangd1ee4622018-06-29 13:49:58 +0800170 mutable std::shared_mutex mRWLock;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500171 unsigned mDefaultNetId;
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700172 std::map<unsigned, Network*> mNetworks; // Map keys are NetIds.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700173 std::map<uid_t, Permission> mUsers;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700174 std::set<uid_t> mProtectableUsers;
Rubin Xu6c00b612018-04-27 14:27:59 +0100175 // Map interface (ifIndex) to its current NetId, or the last NetId if the interface was removed
176 // from the network and not added to another network. This state facilitates the interface to
177 // NetId lookup during RTM_DELADDR (NetworkController::removeInterfaceAddress), when the
178 // interface in question might already have been removed from the network.
179 // An interface is added to this map when it is added to a network and removed from this map
180 // when its network is destroyed.
181 std::unordered_map<unsigned, unsigned> mIfindexToLastNetId;
182 // Map IP address to the list of active interfaces (ifIndex) that have that address.
183 // Also contains IP addresses configured on interfaces that have not been added to any network.
184 // TODO: Does not track IP addresses present when netd is started or restarts after a crash.
185 // This is not a problem for its intended use (tracking IP addresses on VPN interfaces), but
186 // we should fix it.
187 std::unordered_map<std::string, std::unordered_set<unsigned>> mAddressToIfindices;
188
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500189};
190
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900191} // namespace android::net