blob: 024d2cfa778d27693ad0ba9087952f4a6651c95c [file] [log] [blame]
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001/*
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 Ramachandran89dad012014-07-02 10:09:49 -070024VirtualNetwork::VirtualNetwork(unsigned netId): Network(netId) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070025}
26
27VirtualNetwork::~VirtualNetwork() {
28}
29
30int VirtualNetwork::addInterface(const std::string& interface) {
31 if (hasInterface(interface)) {
32 return 0;
33 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070034 if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(),
35 mUidRanges)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070036 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 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070047 if (int ret = RouteController::removeInterfaceFromVirtualNetwork(mNetId, interface.c_str(),
48 mUidRanges)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070049 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 Ramachandranb1425cc2014-06-23 18:54:27 -070055
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070056Network::Type VirtualNetwork::getType() const {
57 return VIRTUAL;
58}
59
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070060int VirtualNetwork::addUsers(const UidRanges& uidRanges) {
61 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070062 if (int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(),
63 uidRanges)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070064 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) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070074 if (int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(),
75 uidRanges)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070076 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}