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 | |
| 17 | #include "VirtualNetwork.h" |
| 18 | |
| 19 | #include "RouteController.h" |
| 20 | |
| 21 | #define LOG_TAG "Netd" |
| 22 | #include "log/log.h" |
| 23 | |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame^] | 24 | VirtualNetwork::VirtualNetwork(unsigned netId, bool hasDns, bool secure) : |
| 25 | Network(netId), mHasDns(hasDns), mSecure(secure) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | VirtualNetwork::~VirtualNetwork() { |
| 29 | } |
| 30 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 31 | bool VirtualNetwork::getHasDns() const { |
| 32 | return mHasDns; |
| 33 | } |
| 34 | |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame^] | 35 | bool VirtualNetwork::isSecure() const { |
| 36 | return mSecure; |
| 37 | } |
| 38 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 39 | bool VirtualNetwork::appliesToUser(uid_t uid) const { |
| 40 | return mUidRanges.hasUid(uid); |
| 41 | } |
| 42 | |
| 43 | int VirtualNetwork::addUsers(const UidRanges& uidRanges) { |
| 44 | for (const std::string& interface : mInterfaces) { |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame^] | 45 | if (int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(), mSecure, |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 46 | uidRanges)) { |
| 47 | ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId); |
| 48 | return ret; |
| 49 | } |
| 50 | } |
| 51 | mUidRanges.add(uidRanges); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | int VirtualNetwork::removeUsers(const UidRanges& uidRanges) { |
| 56 | for (const std::string& interface : mInterfaces) { |
| 57 | if (int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(), |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame^] | 58 | mSecure, uidRanges)) { |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 59 | ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId); |
| 60 | return ret; |
| 61 | } |
| 62 | } |
| 63 | mUidRanges.remove(uidRanges); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | Network::Type VirtualNetwork::getType() const { |
| 68 | return VIRTUAL; |
| 69 | } |
| 70 | |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 71 | int VirtualNetwork::addInterface(const std::string& interface) { |
| 72 | if (hasInterface(interface)) { |
| 73 | return 0; |
| 74 | } |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame^] | 75 | if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(), mSecure, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 76 | mUidRanges)) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 77 | ALOGE("failed to add interface %s to VPN netId %u", interface.c_str(), mNetId); |
| 78 | return ret; |
| 79 | } |
| 80 | mInterfaces.insert(interface); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | int VirtualNetwork::removeInterface(const std::string& interface) { |
| 85 | if (!hasInterface(interface)) { |
| 86 | return 0; |
| 87 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 88 | if (int ret = RouteController::removeInterfaceFromVirtualNetwork(mNetId, interface.c_str(), |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame^] | 89 | mSecure, mUidRanges)) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 90 | ALOGE("failed to remove interface %s from VPN netId %u", interface.c_str(), mNetId); |
| 91 | return ret; |
| 92 | } |
| 93 | mInterfaces.erase(interface); |
| 94 | return 0; |
| 95 | } |