blob: 565bd5536b9afa312b4e2b289d5f7c9f88396b44 [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 Ramachandrane09b20a2014-07-05 17:15:14 -070024VirtualNetwork::VirtualNetwork(unsigned netId, bool hasDns): Network(netId), mHasDns(hasDns) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070025}
26
27VirtualNetwork::~VirtualNetwork() {
28}
29
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070030bool VirtualNetwork::getHasDns() const {
31 return mHasDns;
32}
33
34bool VirtualNetwork::appliesToUser(uid_t uid) const {
35 return mUidRanges.hasUid(uid);
36}
37
38int VirtualNetwork::addUsers(const UidRanges& uidRanges) {
39 for (const std::string& interface : mInterfaces) {
40 if (int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(),
41 uidRanges)) {
42 ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId);
43 return ret;
44 }
45 }
46 mUidRanges.add(uidRanges);
47 return 0;
48}
49
50int VirtualNetwork::removeUsers(const UidRanges& uidRanges) {
51 for (const std::string& interface : mInterfaces) {
52 if (int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(),
53 uidRanges)) {
54 ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId);
55 return ret;
56 }
57 }
58 mUidRanges.remove(uidRanges);
59 return 0;
60}
61
62Network::Type VirtualNetwork::getType() const {
63 return VIRTUAL;
64}
65
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070066int VirtualNetwork::addInterface(const std::string& interface) {
67 if (hasInterface(interface)) {
68 return 0;
69 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070070 if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(),
71 mUidRanges)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070072 ALOGE("failed to add interface %s to VPN netId %u", interface.c_str(), mNetId);
73 return ret;
74 }
75 mInterfaces.insert(interface);
76 return 0;
77}
78
79int VirtualNetwork::removeInterface(const std::string& interface) {
80 if (!hasInterface(interface)) {
81 return 0;
82 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070083 if (int ret = RouteController::removeInterfaceFromVirtualNetwork(mNetId, interface.c_str(),
84 mUidRanges)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070085 ALOGE("failed to remove interface %s from VPN netId %u", interface.c_str(), mNetId);
86 return ret;
87 }
88 mInterfaces.erase(interface);
89 return 0;
90}