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 | // THREAD-SAFETY |
| 18 | // ------------- |
| 19 | // The methods in this file are called from multiple threads (from CommandListener, FwmarkServer |
| 20 | // and DnsProxyListener). So, all accesses to shared state are guarded by a lock. |
| 21 | // |
| 22 | // In some cases, a single non-const method acquires and releases the lock several times, like so: |
| 23 | // if (isValidNetwork(...)) { // isValidNetwork() acquires and releases the lock. |
| 24 | // setDefaultNetwork(...); // setDefaultNetwork() also acquires and releases the lock. |
| 25 | // |
| 26 | // It might seem that this allows races where the state changes between the two statements, but in |
| 27 | // fact there are no races because: |
| 28 | // 1. This pattern only occurs in non-const methods (i.e., those that mutate state). |
| 29 | // 2. Only CommandListener calls these non-const methods. The others call only const methods. |
| 30 | // 3. CommandListener only processes one command at a time. I.e., it's serialized. |
| 31 | // Thus, no other mutation can occur in between the two statements above. |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 32 | |
| 33 | #include "NetworkController.h" |
| 34 | |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 35 | #include "Fwmark.h" |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 36 | #include "LocalNetwork.h" |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 37 | #include "PhysicalNetwork.h" |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 38 | #include "RouteController.h" |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 39 | #include "VirtualNetwork.h" |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 40 | |
Sreeram Ramachandran | ed4bd1f | 2014-07-05 12:31:05 -0700 | [diff] [blame] | 41 | #include "cutils/misc.h" |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 42 | #define LOG_TAG "Netd" |
| 43 | #include "log/log.h" |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 44 | #include "resolv_netid.h" |
| 45 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 46 | namespace { |
| 47 | |
| 48 | // Keep these in sync with ConnectivityService.java. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 49 | const unsigned MIN_NET_ID = 10; |
| 50 | const unsigned MAX_NET_ID = 65535; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 51 | |
| 52 | } // namespace |
| 53 | |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 54 | // All calls to methods here are made while holding a write lock on mRWLock. |
| 55 | class NetworkController::DelegateImpl : public PhysicalNetwork::Delegate { |
| 56 | public: |
| 57 | explicit DelegateImpl(NetworkController* networkController); |
| 58 | virtual ~DelegateImpl(); |
| 59 | |
| 60 | int modifyFallthrough(unsigned vpnNetId, const std::string& physicalInterface, |
| 61 | Permission permission, bool add) WARN_UNUSED_RESULT; |
| 62 | |
| 63 | private: |
| 64 | int addFallthrough(const std::string& physicalInterface, |
| 65 | Permission permission) override WARN_UNUSED_RESULT; |
| 66 | int removeFallthrough(const std::string& physicalInterface, |
| 67 | Permission permission) override WARN_UNUSED_RESULT; |
| 68 | |
| 69 | int modifyFallthrough(const std::string& physicalInterface, Permission permission, |
| 70 | bool add) WARN_UNUSED_RESULT; |
| 71 | |
| 72 | NetworkController* const mNetworkController; |
| 73 | }; |
| 74 | |
| 75 | NetworkController::DelegateImpl::DelegateImpl(NetworkController* networkController) : |
| 76 | mNetworkController(networkController) { |
| 77 | } |
| 78 | |
| 79 | NetworkController::DelegateImpl::~DelegateImpl() { |
| 80 | } |
| 81 | |
| 82 | int NetworkController::DelegateImpl::modifyFallthrough(unsigned vpnNetId, |
| 83 | const std::string& physicalInterface, |
| 84 | Permission permission, bool add) { |
| 85 | if (add) { |
| 86 | if (int ret = RouteController::addVirtualNetworkFallthrough(vpnNetId, |
| 87 | physicalInterface.c_str(), |
| 88 | permission)) { |
| 89 | ALOGE("failed to add fallthrough to %s for VPN netId %u", physicalInterface.c_str(), |
| 90 | vpnNetId); |
| 91 | return ret; |
| 92 | } |
| 93 | } else { |
| 94 | if (int ret = RouteController::removeVirtualNetworkFallthrough(vpnNetId, |
| 95 | physicalInterface.c_str(), |
| 96 | permission)) { |
| 97 | ALOGE("failed to remove fallthrough to %s for VPN netId %u", physicalInterface.c_str(), |
| 98 | vpnNetId); |
| 99 | return ret; |
| 100 | } |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | int NetworkController::DelegateImpl::addFallthrough(const std::string& physicalInterface, |
| 106 | Permission permission) { |
| 107 | return modifyFallthrough(physicalInterface, permission, true); |
| 108 | } |
| 109 | |
| 110 | int NetworkController::DelegateImpl::removeFallthrough(const std::string& physicalInterface, |
| 111 | Permission permission) { |
| 112 | return modifyFallthrough(physicalInterface, permission, false); |
| 113 | } |
| 114 | |
| 115 | int NetworkController::DelegateImpl::modifyFallthrough(const std::string& physicalInterface, |
| 116 | Permission permission, bool add) { |
| 117 | for (const auto& entry : mNetworkController->mNetworks) { |
| 118 | if (entry.second->getType() == Network::VIRTUAL) { |
| 119 | if (int ret = modifyFallthrough(entry.first, physicalInterface, permission, add)) { |
| 120 | return ret; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | NetworkController::NetworkController() : |
| 128 | mDelegateImpl(new NetworkController::DelegateImpl(this)), mDefaultNetId(NETID_UNSET) { |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 129 | mNetworks[LOCAL_NET_ID] = new LocalNetwork(LOCAL_NET_ID); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | unsigned NetworkController::getDefaultNetwork() const { |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 133 | android::RWLock::AutoRLock lock(mRWLock); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 134 | return mDefaultNetId; |
| 135 | } |
| 136 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 137 | int NetworkController::setDefaultNetwork(unsigned netId) { |
| 138 | android::RWLock::AutoWLock lock(mRWLock); |
| 139 | |
| 140 | if (netId == mDefaultNetId) { |
| 141 | return 0; |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 144 | if (netId != NETID_UNSET) { |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 145 | Network* network = getNetworkLocked(netId); |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 146 | if (!network) { |
| 147 | ALOGE("no such netId %u", netId); |
| 148 | return -ENONET; |
| 149 | } |
| 150 | if (network->getType() != Network::PHYSICAL) { |
| 151 | ALOGE("cannot set default to non-physical network with netId %u", netId); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 152 | return -EINVAL; |
| 153 | } |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 154 | if (int ret = static_cast<PhysicalNetwork*>(network)->addAsDefault()) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 155 | return ret; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 159 | if (mDefaultNetId != NETID_UNSET) { |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 160 | Network* network = getNetworkLocked(mDefaultNetId); |
| 161 | if (!network || network->getType() != Network::PHYSICAL) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 162 | ALOGE("cannot find previously set default network with netId %u", mDefaultNetId); |
| 163 | return -ESRCH; |
| 164 | } |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 165 | if (int ret = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 166 | return ret; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 170 | mDefaultNetId = netId; |
| 171 | return 0; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 174 | uint32_t NetworkController::getNetworkForDns(unsigned* netId, uid_t uid) const { |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 175 | android::RWLock::AutoRLock lock(mRWLock); |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 176 | Fwmark fwmark; |
| 177 | fwmark.protectedFromVpn = true; |
| 178 | fwmark.permission = PERMISSION_SYSTEM; |
| 179 | if (canUserSelectNetworkLocked(uid, *netId)) { |
| 180 | // If a non-zero NetId was explicitly specified, and the user has permission for that |
| 181 | // network, use that network's DNS servers. Do not fall through to the default network even |
| 182 | // if the explicitly selected network is a split tunnel VPN or a VPN without DNS servers. |
| 183 | fwmark.explicitlySelected = true; |
| 184 | } else { |
| 185 | // If the user is subject to a VPN and the VPN provides DNS servers, use those servers |
| 186 | // (possibly falling through to the default network if the VPN doesn't provide a route to |
| 187 | // them). Otherwise, use the default network's DNS servers. |
| 188 | VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid); |
| 189 | if (virtualNetwork && virtualNetwork->getHasDns()) { |
| 190 | *netId = virtualNetwork->getNetId(); |
| 191 | } else { |
| 192 | *netId = mDefaultNetId; |
| 193 | } |
| 194 | } |
| 195 | fwmark.netId = *netId; |
| 196 | return fwmark.intValue; |
| 197 | } |
| 198 | |
| 199 | // Returns the NetId that a given UID would use if no network is explicitly selected. Specifically, |
| 200 | // the VPN that applies to the UID if any; otherwise, the default network. |
| 201 | unsigned NetworkController::getNetworkForUser(uid_t uid) const { |
| 202 | android::RWLock::AutoRLock lock(mRWLock); |
| 203 | if (VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid)) { |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 204 | return virtualNetwork->getNetId(); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 205 | } |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 206 | return mDefaultNetId; |
| 207 | } |
| 208 | |
| 209 | // Returns the NetId that will be set when a socket connect()s. This is the bypassable VPN that |
| 210 | // applies to the user if any; otherwise, the default network. |
| 211 | // |
| 212 | // In general, we prefer to always set the default network's NetId in connect(), so that if the VPN |
| 213 | // is a split-tunnel and disappears later, the socket continues working (since the default network's |
| 214 | // NetId is still valid). Secure VPNs will correctly grab the socket's traffic since they have a |
| 215 | // high-priority routing rule that doesn't care what NetId the socket has. |
| 216 | // |
| 217 | // But bypassable VPNs have a very low priority rule, so we need to mark the socket with the |
| 218 | // bypassable VPN's NetId if we expect it to get any traffic at all. If the bypassable VPN is a |
| 219 | // split-tunnel, that's okay, because we have fallthrough rules that will direct the fallthrough |
| 220 | // traffic to the default network. But it does mean that if the bypassable VPN goes away (and thus |
| 221 | // the fallthrough rules also go away), the socket that used to fallthrough to the default network |
| 222 | // will stop working. |
| 223 | unsigned NetworkController::getNetworkForConnect(uid_t uid) const { |
| 224 | android::RWLock::AutoRLock lock(mRWLock); |
| 225 | VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid); |
| 226 | if (virtualNetwork && !virtualNetwork->isSecure()) { |
| 227 | return virtualNetwork->getNetId(); |
| 228 | } |
| 229 | return mDefaultNetId; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 230 | } |
| 231 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 232 | unsigned NetworkController::getNetworkForInterface(const char* interface) const { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 233 | android::RWLock::AutoRLock lock(mRWLock); |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 234 | for (const auto& entry : mNetworks) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 235 | if (entry.second->hasInterface(interface)) { |
| 236 | return entry.first; |
| 237 | } |
| 238 | } |
Paul Jensen | 35c77e3 | 2014-04-10 14:57:54 -0400 | [diff] [blame] | 239 | return NETID_UNSET; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 240 | } |
| 241 | |
Sreeram Ramachandran | 070b2d2 | 2014-07-11 17:06:12 -0700 | [diff] [blame] | 242 | bool NetworkController::isVirtualNetwork(unsigned netId) const { |
| 243 | android::RWLock::AutoRLock lock(mRWLock); |
| 244 | Network* network = getNetworkLocked(netId); |
| 245 | return network && network->getType() == Network::VIRTUAL; |
| 246 | } |
| 247 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 248 | int NetworkController::createPhysicalNetwork(unsigned netId, Permission permission) { |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 249 | if (netId < MIN_NET_ID || netId > MAX_NET_ID) { |
Paul Jensen | ae37e8a | 2014-04-28 10:35:51 -0400 | [diff] [blame] | 250 | ALOGE("invalid netId %u", netId); |
Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 251 | return -EINVAL; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 254 | if (isValidNetwork(netId)) { |
| 255 | ALOGE("duplicate netId %u", netId); |
| 256 | return -EEXIST; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 259 | PhysicalNetwork* physicalNetwork = new PhysicalNetwork(netId, mDelegateImpl); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 260 | if (int ret = physicalNetwork->setPermission(permission)) { |
| 261 | ALOGE("inconceivable! setPermission cannot fail on an empty network"); |
| 262 | delete physicalNetwork; |
Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 263 | return ret; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 266 | android::RWLock::AutoWLock lock(mRWLock); |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 267 | mNetworks[netId] = physicalNetwork; |
Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 268 | return 0; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 271 | int NetworkController::createVirtualNetwork(unsigned netId, bool hasDns, bool secure) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 272 | if (netId < MIN_NET_ID || netId > MAX_NET_ID) { |
| 273 | ALOGE("invalid netId %u", netId); |
| 274 | return -EINVAL; |
| 275 | } |
| 276 | |
| 277 | if (isValidNetwork(netId)) { |
| 278 | ALOGE("duplicate netId %u", netId); |
| 279 | return -EEXIST; |
| 280 | } |
| 281 | |
| 282 | android::RWLock::AutoWLock lock(mRWLock); |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 283 | if (int ret = modifyFallthroughLocked(netId, true)) { |
| 284 | return ret; |
| 285 | } |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 286 | mNetworks[netId] = new VirtualNetwork(netId, hasDns, secure); |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 290 | int NetworkController::destroyNetwork(unsigned netId) { |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 291 | if (netId == LOCAL_NET_ID) { |
| 292 | ALOGE("cannot destroy local network"); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 293 | return -EINVAL; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 294 | } |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 295 | if (!isValidNetwork(netId)) { |
| 296 | ALOGE("no such netId %u", netId); |
| 297 | return -ENONET; |
| 298 | } |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 299 | |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 300 | // TODO: ioctl(SIOCKILLADDR, ...) to kill all sockets on the old network. |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 301 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 302 | android::RWLock::AutoWLock lock(mRWLock); |
| 303 | Network* network = getNetworkLocked(netId); |
Lorenzo Colitti | 99286fe | 2014-08-12 15:08:00 +0900 | [diff] [blame^] | 304 | |
| 305 | // If we fail to destroy a network, things will get stuck badly. Therefore, unlike most of the |
| 306 | // other network code, ignore failures and attempt to clear out as much state as possible, even |
| 307 | // if we hit an error on the way. Return the first error that we see. |
| 308 | int ret = network->clearInterfaces(); |
| 309 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 310 | if (mDefaultNetId == netId) { |
Lorenzo Colitti | 99286fe | 2014-08-12 15:08:00 +0900 | [diff] [blame^] | 311 | if (int err = static_cast<PhysicalNetwork*>(network)->removeAsDefault()) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 312 | ALOGE("inconceivable! removeAsDefault cannot fail on an empty network"); |
Lorenzo Colitti | 99286fe | 2014-08-12 15:08:00 +0900 | [diff] [blame^] | 313 | if (!ret) { |
| 314 | ret = err; |
| 315 | } |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 316 | } |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 317 | mDefaultNetId = NETID_UNSET; |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 318 | } else if (network->getType() == Network::VIRTUAL) { |
Lorenzo Colitti | 99286fe | 2014-08-12 15:08:00 +0900 | [diff] [blame^] | 319 | if (int err = modifyFallthroughLocked(netId, false)) { |
| 320 | if (!ret) { |
| 321 | ret = err; |
| 322 | } |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 323 | } |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 324 | } |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 325 | mNetworks.erase(netId); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 326 | delete network; |
Sreeram Ramachandran | 3ced069 | 2014-04-18 09:49:13 -0700 | [diff] [blame] | 327 | _resolv_delete_cache_for_net(netId); |
Lorenzo Colitti | 99286fe | 2014-08-12 15:08:00 +0900 | [diff] [blame^] | 328 | return ret; |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 331 | int NetworkController::addInterfaceToNetwork(unsigned netId, const char* interface) { |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 332 | if (!isValidNetwork(netId)) { |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 333 | ALOGE("no such netId %u", netId); |
| 334 | return -ENONET; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 337 | unsigned existingNetId = getNetworkForInterface(interface); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 338 | if (existingNetId != NETID_UNSET && existingNetId != netId) { |
| 339 | ALOGE("interface %s already assigned to netId %u", interface, existingNetId); |
| 340 | return -EBUSY; |
| 341 | } |
| 342 | |
| 343 | android::RWLock::AutoWLock lock(mRWLock); |
| 344 | return getNetworkLocked(netId)->addInterface(interface); |
| 345 | } |
| 346 | |
| 347 | int NetworkController::removeInterfaceFromNetwork(unsigned netId, const char* interface) { |
| 348 | if (!isValidNetwork(netId)) { |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 349 | ALOGE("no such netId %u", netId); |
| 350 | return -ENONET; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | android::RWLock::AutoWLock lock(mRWLock); |
| 354 | return getNetworkLocked(netId)->removeInterface(interface); |
| 355 | } |
| 356 | |
| 357 | Permission NetworkController::getPermissionForUser(uid_t uid) const { |
| 358 | android::RWLock::AutoRLock lock(mRWLock); |
Sreeram Ramachandran | ed4bd1f | 2014-07-05 12:31:05 -0700 | [diff] [blame] | 359 | return getPermissionForUserLocked(uid); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void NetworkController::setPermissionForUsers(Permission permission, |
| 363 | const std::vector<uid_t>& uids) { |
| 364 | android::RWLock::AutoWLock lock(mRWLock); |
| 365 | for (uid_t uid : uids) { |
Sreeram Ramachandran | ed4bd1f | 2014-07-05 12:31:05 -0700 | [diff] [blame] | 366 | mUsers[uid] = permission; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 370 | bool NetworkController::canUserSelectNetwork(uid_t uid, unsigned netId) const { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 371 | android::RWLock::AutoRLock lock(mRWLock); |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 372 | return canUserSelectNetworkLocked(uid, netId); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | int NetworkController::setPermissionForNetworks(Permission permission, |
| 376 | const std::vector<unsigned>& netIds) { |
| 377 | android::RWLock::AutoWLock lock(mRWLock); |
| 378 | for (unsigned netId : netIds) { |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 379 | Network* network = getNetworkLocked(netId); |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 380 | if (!network) { |
| 381 | ALOGE("no such netId %u", netId); |
| 382 | return -ENONET; |
| 383 | } |
| 384 | if (network->getType() != Network::PHYSICAL) { |
| 385 | ALOGE("cannot set permissions on non-physical network with netId %u", netId); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 386 | return -EINVAL; |
| 387 | } |
| 388 | |
| 389 | // TODO: ioctl(SIOCKILLADDR, ...) to kill socets on the network that don't have permission. |
| 390 | |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 391 | if (int ret = static_cast<PhysicalNetwork*>(network)->setPermission(permission)) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 392 | return ret; |
| 393 | } |
| 394 | } |
| 395 | return 0; |
| 396 | } |
| 397 | |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 398 | int NetworkController::addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) { |
| 399 | android::RWLock::AutoWLock lock(mRWLock); |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 400 | Network* network = getNetworkLocked(netId); |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 401 | if (!network) { |
| 402 | ALOGE("no such netId %u", netId); |
| 403 | return -ENONET; |
| 404 | } |
| 405 | if (network->getType() != Network::VIRTUAL) { |
| 406 | ALOGE("cannot add users to non-virtual network with netId %u", netId); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 407 | return -EINVAL; |
| 408 | } |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 409 | if (int ret = static_cast<VirtualNetwork*>(network)->addUsers(uidRanges)) { |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 410 | return ret; |
| 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | int NetworkController::removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) { |
| 416 | android::RWLock::AutoWLock lock(mRWLock); |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 417 | Network* network = getNetworkLocked(netId); |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 418 | if (!network) { |
| 419 | ALOGE("no such netId %u", netId); |
| 420 | return -ENONET; |
| 421 | } |
| 422 | if (network->getType() != Network::VIRTUAL) { |
| 423 | ALOGE("cannot remove users from non-virtual network with netId %u", netId); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 424 | return -EINVAL; |
| 425 | } |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 426 | if (int ret = static_cast<VirtualNetwork*>(network)->removeUsers(uidRanges)) { |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 427 | return ret; |
| 428 | } |
| 429 | return 0; |
| 430 | } |
| 431 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 432 | int NetworkController::addRoute(unsigned netId, const char* interface, const char* destination, |
| 433 | const char* nexthop, bool legacy, uid_t uid) { |
| 434 | return modifyRoute(netId, interface, destination, nexthop, true, legacy, uid); |
| 435 | } |
| 436 | |
| 437 | int NetworkController::removeRoute(unsigned netId, const char* interface, const char* destination, |
| 438 | const char* nexthop, bool legacy, uid_t uid) { |
| 439 | return modifyRoute(netId, interface, destination, nexthop, false, legacy, uid); |
| 440 | } |
| 441 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 442 | bool NetworkController::canProtect(uid_t uid) const { |
| 443 | android::RWLock::AutoRLock lock(mRWLock); |
| 444 | return ((getPermissionForUserLocked(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) || |
| 445 | mProtectableUsers.find(uid) != mProtectableUsers.end(); |
| 446 | } |
| 447 | |
Sreeram Ramachandran | 89dad01 | 2014-07-02 10:09:49 -0700 | [diff] [blame] | 448 | void NetworkController::allowProtect(const std::vector<uid_t>& uids) { |
| 449 | android::RWLock::AutoWLock lock(mRWLock); |
| 450 | mProtectableUsers.insert(uids.begin(), uids.end()); |
| 451 | } |
| 452 | |
| 453 | void NetworkController::denyProtect(const std::vector<uid_t>& uids) { |
| 454 | android::RWLock::AutoWLock lock(mRWLock); |
| 455 | for (uid_t uid : uids) { |
| 456 | mProtectableUsers.erase(uid); |
| 457 | } |
| 458 | } |
| 459 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 460 | bool NetworkController::isValidNetwork(unsigned netId) const { |
| 461 | android::RWLock::AutoRLock lock(mRWLock); |
| 462 | return getNetworkLocked(netId); |
| 463 | } |
| 464 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 465 | Network* NetworkController::getNetworkLocked(unsigned netId) const { |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 466 | auto iter = mNetworks.find(netId); |
| 467 | return iter == mNetworks.end() ? NULL : iter->second; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 470 | VirtualNetwork* NetworkController::getVirtualNetworkForUserLocked(uid_t uid) const { |
| 471 | for (const auto& entry : mNetworks) { |
| 472 | if (entry.second->getType() == Network::VIRTUAL) { |
| 473 | VirtualNetwork* virtualNetwork = static_cast<VirtualNetwork*>(entry.second); |
| 474 | if (virtualNetwork->appliesToUser(uid)) { |
| 475 | return virtualNetwork; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | return NULL; |
| 480 | } |
| 481 | |
Sreeram Ramachandran | ed4bd1f | 2014-07-05 12:31:05 -0700 | [diff] [blame] | 482 | Permission NetworkController::getPermissionForUserLocked(uid_t uid) const { |
| 483 | auto iter = mUsers.find(uid); |
| 484 | if (iter != mUsers.end()) { |
| 485 | return iter->second; |
| 486 | } |
| 487 | return uid < FIRST_APPLICATION_UID ? PERMISSION_SYSTEM : PERMISSION_NONE; |
| 488 | } |
| 489 | |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 490 | bool NetworkController::canUserSelectNetworkLocked(uid_t uid, unsigned netId) const { |
| 491 | Network* network = getNetworkLocked(netId); |
| 492 | // If uid is INVALID_UID, this likely means that we were unable to retrieve the UID of the peer |
| 493 | // (using SO_PEERCRED). Be safe and deny access to the network, even if it's valid. |
| 494 | if (!network || uid == INVALID_UID) { |
| 495 | return false; |
| 496 | } |
| 497 | Permission userPermission = getPermissionForUserLocked(uid); |
| 498 | if ((userPermission & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) { |
| 499 | return true; |
| 500 | } |
| 501 | if (network->getType() == Network::VIRTUAL) { |
| 502 | return static_cast<VirtualNetwork*>(network)->appliesToUser(uid); |
| 503 | } |
| 504 | VirtualNetwork* virtualNetwork = getVirtualNetworkForUserLocked(uid); |
| 505 | if (virtualNetwork && virtualNetwork->isSecure() && |
| 506 | mProtectableUsers.find(uid) == mProtectableUsers.end()) { |
| 507 | return false; |
| 508 | } |
| 509 | Permission networkPermission = static_cast<PhysicalNetwork*>(network)->getPermission(); |
| 510 | return (userPermission & networkPermission) == networkPermission; |
| 511 | } |
| 512 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 513 | int NetworkController::modifyRoute(unsigned netId, const char* interface, const char* destination, |
| 514 | const char* nexthop, bool add, bool legacy, uid_t uid) { |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 515 | if (!isValidNetwork(netId)) { |
| 516 | ALOGE("no such netId %u", netId); |
| 517 | return -ENONET; |
| 518 | } |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 519 | unsigned existingNetId = getNetworkForInterface(interface); |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 520 | if (existingNetId == NETID_UNSET) { |
| 521 | ALOGE("interface %s not assigned to any netId", interface); |
| 522 | return -ENODEV; |
| 523 | } |
| 524 | if (existingNetId != netId) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 525 | ALOGE("interface %s assigned to netId %u, not %u", interface, existingNetId, netId); |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 526 | return -ENOENT; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 529 | RouteController::TableType tableType; |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 530 | if (netId == LOCAL_NET_ID) { |
| 531 | tableType = RouteController::LOCAL_NETWORK; |
| 532 | } else if (legacy) { |
Sreeram Ramachandran | ed4bd1f | 2014-07-05 12:31:05 -0700 | [diff] [blame] | 533 | if ((getPermissionForUser(uid) & PERMISSION_SYSTEM) == PERMISSION_SYSTEM) { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 534 | tableType = RouteController::LEGACY_SYSTEM; |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 535 | } else { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 536 | tableType = RouteController::LEGACY_NETWORK; |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 537 | } |
| 538 | } else { |
| 539 | tableType = RouteController::INTERFACE; |
| 540 | } |
| 541 | |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 542 | return add ? RouteController::addRoute(interface, destination, nexthop, tableType) : |
| 543 | RouteController::removeRoute(interface, destination, nexthop, tableType); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 544 | } |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 545 | |
| 546 | int NetworkController::modifyFallthroughLocked(unsigned vpnNetId, bool add) { |
| 547 | if (mDefaultNetId == NETID_UNSET) { |
| 548 | return 0; |
| 549 | } |
| 550 | Network* network = getNetworkLocked(mDefaultNetId); |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 551 | if (!network) { |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 552 | ALOGE("cannot find previously set default network with netId %u", mDefaultNetId); |
| 553 | return -ESRCH; |
| 554 | } |
Lorenzo Colitti | 738c93e | 2014-07-30 17:46:08 +0900 | [diff] [blame] | 555 | if (network->getType() != Network::PHYSICAL) { |
| 556 | ALOGE("inconceivable! default network must be a physical network"); |
| 557 | return -EINVAL; |
| 558 | } |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 559 | Permission permission = static_cast<PhysicalNetwork*>(network)->getPermission(); |
| 560 | for (const auto& physicalInterface : network->getInterfaces()) { |
| 561 | if (int ret = mDelegateImpl->modifyFallthrough(vpnNetId, physicalInterface, permission, |
| 562 | add)) { |
| 563 | return ret; |
| 564 | } |
| 565 | } |
| 566 | return 0; |
| 567 | } |