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