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 24 VirtualNetwork::VirtualNetwork(unsigned netId, bool hasDns, bool secure) : 25 Network(netId), mHasDns(hasDns), mSecure(secure) { 26 } 27 28 VirtualNetwork::~VirtualNetwork() { 29 } 30 31 bool VirtualNetwork::getHasDns() const { 32 return mHasDns; 33 } 34 35 bool VirtualNetwork::isSecure() const { 36 return mSecure; 37 } 38 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) { 45 if (int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(), mSecure, 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(), 58 mSecure, uidRanges)) { 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 71 int VirtualNetwork::addInterface(const std::string& interface) { 72 if (hasInterface(interface)) { 73 return 0; 74 } 75 if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(), mSecure, 76 mUidRanges)) { 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 } 88 if (int ret = RouteController::removeInterfaceFromVirtualNetwork(mNetId, interface.c_str(), 89 mSecure, mUidRanges)) { 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 } 96