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 | |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 17 | #ifndef NETD_SERVER_NETWORK_CONTROLLER_H |
| 18 | #define NETD_SERVER_NETWORK_CONTROLLER_H |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 19 | |
Niranjan Pendharkar | 4c18bd9 | 2017-07-24 09:54:07 -0700 | [diff] [blame] | 20 | #include <android/multinetwork.h> |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 21 | #include "NetdConstants.h" |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 22 | #include "Permission.h" |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 23 | |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 24 | #include "utils/RWLock.h" |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 25 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 26 | #include <list> |
| 27 | #include <map> |
Sreeram Ramachandran | 89dad01 | 2014-07-02 10:09:49 -0700 | [diff] [blame] | 28 | #include <set> |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 29 | #include <sys/types.h> |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 30 | #include <vector> |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 31 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 32 | struct android_net_context; |
| 33 | |
| 34 | namespace android { |
| 35 | namespace net { |
| 36 | |
Lorenzo Colitti | ae8da9a | 2017-09-05 11:58:27 +0900 | [diff] [blame^] | 37 | constexpr uint32_t kHandleMagic = 0xcafed00d; |
| 38 | |
Niranjan Pendharkar | 4c18bd9 | 2017-07-24 09:54:07 -0700 | [diff] [blame] | 39 | // Utility to convert from netId to net_handle_t. Doing this here as opposed to exporting |
| 40 | // from net.c as it may have NDK implications. Besides no conversion available in net.c for |
| 41 | // obtaining handle given netId. |
| 42 | // TODO: Use getnetidfromhandle() in net.c. |
| 43 | static inline unsigned netHandleToNetId(net_handle_t fromNetHandle) { |
| 44 | const uint32_t k32BitMask = 0xffffffff; |
| 45 | // This value MUST be kept in sync with the corresponding value in |
| 46 | // the android.net.Network#getNetworkHandle() implementation. |
Niranjan Pendharkar | 4c18bd9 | 2017-07-24 09:54:07 -0700 | [diff] [blame] | 47 | |
| 48 | // Check for minimum acceptable version of the API in the low bits. |
| 49 | if (fromNetHandle != NETWORK_UNSPECIFIED && |
| 50 | (fromNetHandle & k32BitMask) != kHandleMagic) { |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | return ((fromNetHandle >> (CHAR_BIT * sizeof(k32BitMask))) & k32BitMask); |
| 55 | } |
| 56 | |
| 57 | // Utility to convert from nethandle to netid, keep in sync with getNetworkHandle |
| 58 | // in Network.java. |
| 59 | static inline net_handle_t netIdToNetHandle(unsigned fromNetId) { |
Niranjan Pendharkar | 4c18bd9 | 2017-07-24 09:54:07 -0700 | [diff] [blame] | 60 | if (!fromNetId) { |
| 61 | return NETWORK_UNSPECIFIED; |
| 62 | } |
Lorenzo Colitti | ae8da9a | 2017-09-05 11:58:27 +0900 | [diff] [blame^] | 63 | return (((net_handle_t)fromNetId << 32) | kHandleMagic); |
Niranjan Pendharkar | 4c18bd9 | 2017-07-24 09:54:07 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 66 | class DumpWriter; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 67 | class Network; |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 68 | class UidRanges; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 69 | class VirtualNetwork; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 70 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 71 | /* |
| 72 | * Keeps track of default, per-pid, and per-uid-range network selection, as |
| 73 | * well as the mark associated with each network. Networks are identified |
| 74 | * by netid. In all set* commands netid == 0 means "unspecified" and is |
| 75 | * equivalent to clearing the mapping. |
| 76 | */ |
| 77 | class NetworkController { |
| 78 | public: |
Sreeram Ramachandran | bbdde99 | 2014-09-05 16:05:03 -0700 | [diff] [blame] | 79 | static const unsigned MIN_OEM_ID; |
| 80 | static const unsigned MAX_OEM_ID; |
| 81 | static const unsigned LOCAL_NET_ID; |
Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 82 | static const unsigned DUMMY_NET_ID; |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 83 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 84 | NetworkController(); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 85 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 86 | unsigned getDefaultNetwork() const; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 87 | int setDefaultNetwork(unsigned netId) WARN_UNUSED_RESULT; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 88 | |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 89 | // Sets |*netId| to an appropriate NetId to use for DNS for the given user. Call with |*netId| |
| 90 | // set to a non-NETID_UNSET value if the user already has indicated a preference. Returns the |
| 91 | // fwmark value to set on the socket when performing the DNS request. |
| 92 | uint32_t getNetworkForDns(unsigned* netId, uid_t uid) const; |
| 93 | unsigned getNetworkForUser(uid_t uid) const; |
| 94 | unsigned getNetworkForConnect(uid_t uid) const; |
Erik Kline | cea2d34 | 2015-06-25 18:24:46 +0900 | [diff] [blame] | 95 | 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] | 96 | unsigned getNetworkForInterface(const char* interface) const; |
Sreeram Ramachandran | 070b2d2 | 2014-07-11 17:06:12 -0700 | [diff] [blame] | 97 | bool isVirtualNetwork(unsigned netId) const; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 98 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 99 | int createPhysicalNetwork(unsigned netId, Permission permission) WARN_UNUSED_RESULT; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 100 | int createPhysicalOemNetwork(Permission permission, unsigned *netId) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 101 | int createVirtualNetwork(unsigned netId, bool hasDns, bool secure) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 102 | int destroyNetwork(unsigned netId) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 103 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 104 | int addInterfaceToNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT; |
| 105 | int removeInterfaceFromNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT; |
| 106 | |
| 107 | Permission getPermissionForUser(uid_t uid) const; |
| 108 | void setPermissionForUsers(Permission permission, const std::vector<uid_t>& uids); |
Lorenzo Colitti | a1067c8 | 2014-10-02 22:47:41 +0900 | [diff] [blame] | 109 | int checkUserNetworkAccess(uid_t uid, unsigned netId) const; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 110 | int setPermissionForNetworks(Permission permission, |
| 111 | const std::vector<unsigned>& netIds) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 112 | |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 113 | int addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT; |
| 114 | int removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT; |
| 115 | |
Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 116 | // |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] | 117 | // 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] | 118 | // |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 119 | // Routes are added to tables determined by the interface, so only |interface| is actually used. |
| 120 | // |netId| is given only to sanity check that the interface has the correct netId. |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 121 | int addRoute(unsigned netId, const char* interface, const char* destination, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 122 | const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT; |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 123 | int removeRoute(unsigned netId, const char* interface, const char* destination, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 124 | const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 125 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 126 | bool canProtect(uid_t uid) const; |
Sreeram Ramachandran | 89dad01 | 2014-07-02 10:09:49 -0700 | [diff] [blame] | 127 | void allowProtect(const std::vector<uid_t>& uids); |
| 128 | void denyProtect(const std::vector<uid_t>& uids); |
| 129 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 130 | void dump(DumpWriter& dw); |
| 131 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 132 | private: |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 133 | bool isValidNetworkLocked(unsigned netId) const; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 134 | Network* getNetworkLocked(unsigned netId) const; |
Lorenzo Colitti | bbd0aff | 2016-12-15 22:53:24 +0900 | [diff] [blame] | 135 | uint32_t getNetworkForDnsLocked(unsigned* netId, uid_t uid) const; |
| 136 | unsigned getNetworkForUserLocked(uid_t uid) const; |
| 137 | unsigned getNetworkForConnectLocked(uid_t uid) const; |
| 138 | unsigned getNetworkForInterfaceLocked(const char* interface) const; |
| 139 | bool canProtectLocked(uid_t uid) const; |
| 140 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 141 | VirtualNetwork* getVirtualNetworkForUserLocked(uid_t uid) const; |
Sreeram Ramachandran | ed4bd1f | 2014-07-05 12:31:05 -0700 | [diff] [blame] | 142 | Permission getPermissionForUserLocked(uid_t uid) const; |
Lorenzo Colitti | a1067c8 | 2014-10-02 22:47:41 +0900 | [diff] [blame] | 143 | int checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 144 | int createPhysicalNetworkLocked(unsigned netId, Permission permission) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 145 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 146 | int modifyRoute(unsigned netId, const char* interface, const char* destination, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 147 | const char* nexthop, bool add, bool legacy, uid_t uid) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 148 | int modifyFallthroughLocked(unsigned vpnNetId, bool add) WARN_UNUSED_RESULT; |
| 149 | |
| 150 | class DelegateImpl; |
| 151 | DelegateImpl* const mDelegateImpl; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 152 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 153 | // mRWLock guards all accesses to mDefaultNetId, mNetworks, mUsers and mProtectableUsers. |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 154 | mutable android::RWLock mRWLock; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 155 | unsigned mDefaultNetId; |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 156 | std::map<unsigned, Network*> mNetworks; // Map keys are NetIds. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 157 | std::map<uid_t, Permission> mUsers; |
Sreeram Ramachandran | 89dad01 | 2014-07-02 10:09:49 -0700 | [diff] [blame] | 158 | std::set<uid_t> mProtectableUsers; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 159 | }; |
| 160 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 161 | } // namespace net |
| 162 | } // namespace android |
| 163 | |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 164 | #endif // NETD_SERVER_NETWORK_CONTROLLER_H |