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