VirtualNetwork.cpp revision 5009d5ef3fbcdc69d772b528fd22184b7d605afa
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
24VirtualNetwork::VirtualNetwork(unsigned netId): Network(netId) {
25}
26
27VirtualNetwork::~VirtualNetwork() {
28}
29
30int VirtualNetwork::addInterface(const std::string& interface) {
31    if (hasInterface(interface)) {
32        return 0;
33    }
34    if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(),
35                                                                mUidRanges)) {
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
43int VirtualNetwork::removeInterface(const std::string& interface) {
44    if (!hasInterface(interface)) {
45        return 0;
46    }
47    if (int ret = RouteController::removeInterfaceFromVirtualNetwork(mNetId, interface.c_str(),
48                                                                     mUidRanges)) {
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}
55
56Network::Type VirtualNetwork::getType() const {
57    return VIRTUAL;
58}
59
60int VirtualNetwork::addUsers(const UidRanges& uidRanges) {
61    for (const std::string& interface : mInterfaces) {
62        if (int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(),
63                                                                uidRanges)) {
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
72int VirtualNetwork::removeUsers(const UidRanges& uidRanges) {
73    for (const std::string& interface : mInterfaces) {
74        if (int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(),
75                                                                     uidRanges)) {
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}
83