Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [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 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 17 | #include <set> |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame^] | 18 | |
| 19 | #define LOG_TAG "Netd" |
| 20 | |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 21 | #include "VirtualNetwork.h" |
| 22 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 23 | #include "SockDiag.h" |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 24 | #include "RouteController.h" |
| 25 | |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 26 | #include "log/log.h" |
| 27 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace net { |
| 30 | |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 31 | VirtualNetwork::VirtualNetwork(unsigned netId, bool hasDns, bool secure) : |
| 32 | Network(netId), mHasDns(hasDns), mSecure(secure) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | VirtualNetwork::~VirtualNetwork() { |
| 36 | } |
| 37 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 38 | bool VirtualNetwork::getHasDns() const { |
| 39 | return mHasDns; |
| 40 | } |
| 41 | |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 42 | bool VirtualNetwork::isSecure() const { |
| 43 | return mSecure; |
| 44 | } |
| 45 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 46 | bool VirtualNetwork::appliesToUser(uid_t uid) const { |
| 47 | return mUidRanges.hasUid(uid); |
| 48 | } |
| 49 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 50 | |
| 51 | int VirtualNetwork::maybeCloseSockets(bool add, const UidRanges& uidRanges, |
| 52 | const std::set<uid_t>& protectableUsers) { |
| 53 | if (!mSecure) { |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | SockDiag sd; |
| 58 | if (!sd.open()) { |
| 59 | return -EBADFD; |
| 60 | } |
| 61 | |
Lorenzo Colitti | 0726fec | 2016-07-26 17:53:50 +0900 | [diff] [blame] | 62 | if (int ret = sd.destroySockets(uidRanges, protectableUsers, true /* excludeLoopback */)) { |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 63 | ALOGE("Failed to close sockets while %s %s to network %d: %s", |
| 64 | add ? "adding" : "removing", uidRanges.toString().c_str(), mNetId, strerror(-ret)); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int VirtualNetwork::addUsers(const UidRanges& uidRanges, const std::set<uid_t>& protectableUsers) { |
| 72 | maybeCloseSockets(true, uidRanges, protectableUsers); |
| 73 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 74 | for (const std::string& interface : mInterfaces) { |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 75 | if (int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(), mSecure, |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 76 | uidRanges)) { |
| 77 | ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId); |
| 78 | return ret; |
| 79 | } |
| 80 | } |
| 81 | mUidRanges.add(uidRanges); |
| 82 | return 0; |
| 83 | } |
| 84 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 85 | int VirtualNetwork::removeUsers(const UidRanges& uidRanges, |
| 86 | const std::set<uid_t>& protectableUsers) { |
| 87 | maybeCloseSockets(false, uidRanges, protectableUsers); |
| 88 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 89 | for (const std::string& interface : mInterfaces) { |
| 90 | if (int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(), |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 91 | mSecure, uidRanges)) { |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 92 | ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId); |
| 93 | return ret; |
| 94 | } |
| 95 | } |
| 96 | mUidRanges.remove(uidRanges); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | Network::Type VirtualNetwork::getType() const { |
| 101 | return VIRTUAL; |
| 102 | } |
| 103 | |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 104 | int VirtualNetwork::addInterface(const std::string& interface) { |
| 105 | if (hasInterface(interface)) { |
| 106 | return 0; |
| 107 | } |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 108 | if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(), mSecure, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 109 | mUidRanges)) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 110 | ALOGE("failed to add interface %s to VPN netId %u", interface.c_str(), mNetId); |
| 111 | return ret; |
| 112 | } |
| 113 | mInterfaces.insert(interface); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int VirtualNetwork::removeInterface(const std::string& interface) { |
| 118 | if (!hasInterface(interface)) { |
| 119 | return 0; |
| 120 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 121 | if (int ret = RouteController::removeInterfaceFromVirtualNetwork(mNetId, interface.c_str(), |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 122 | mSecure, mUidRanges)) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 123 | ALOGE("failed to remove interface %s from VPN netId %u", interface.c_str(), mNetId); |
| 124 | return ret; |
| 125 | } |
| 126 | mInterfaces.erase(interface); |
| 127 | return 0; |
| 128 | } |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 129 | |
| 130 | } // namespace net |
| 131 | } // namespace android |