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