2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "VirtualNetwork.h"
19 #include "RouteController.h"
21 #define LOG_TAG "Netd"
24 VirtualNetwork::VirtualNetwork(unsigned netId, bool hasDns, bool secure) :
25 Network(netId), mHasDns(hasDns), mSecure(secure) {
28 VirtualNetwork::~VirtualNetwork() {
31 bool VirtualNetwork::getHasDns() const {
35 bool VirtualNetwork::isSecure() const {
39 bool VirtualNetwork::appliesToUser(uid_t uid) const {
40 return mUidRanges.hasUid(uid);
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,
47 ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId);
51 mUidRanges.add(uidRanges);
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);
63 mUidRanges.remove(uidRanges);
67 Network::Type VirtualNetwork::getType() const {
71 int VirtualNetwork::addInterface(const std::string& interface) {
72 if (hasInterface(interface)) {
75 if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(), mSecure,
77 ALOGE("failed to add interface %s to VPN netId %u", interface.c_str(), mNetId);
80 mInterfaces.insert(interface);
84 int VirtualNetwork::removeInterface(const std::string& interface) {
85 if (!hasInterface(interface)) {
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);
93 mInterfaces.erase(interface);