blob: 5db3645adc7e7bfebd74db96ab4c559852f425c2 [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 Ramachandran95684ba2014-07-23 13:27:31 -070024VirtualNetwork::VirtualNetwork(unsigned netId, bool hasDns, bool secure) :
25 Network(netId), mHasDns(hasDns), mSecure(secure) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070026}
27
28VirtualNetwork::~VirtualNetwork() {
29}
30
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070031bool VirtualNetwork::getHasDns() const {
32 return mHasDns;
33}
34
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070035bool VirtualNetwork::isSecure() const {
36 return mSecure;
37}
38
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070039bool VirtualNetwork::appliesToUser(uid_t uid) const {
40 return mUidRanges.hasUid(uid);
41}
42
43int VirtualNetwork::addUsers(const UidRanges& uidRanges) {
44 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070045 if (int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(), mSecure,
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070046 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
55int VirtualNetwork::removeUsers(const UidRanges& uidRanges) {
56 for (const std::string& interface : mInterfaces) {
57 if (int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(),
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070058 mSecure, uidRanges)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070059 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
67Network::Type VirtualNetwork::getType() const {
68 return VIRTUAL;
69}
70
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070071int VirtualNetwork::addInterface(const std::string& interface) {
72 if (hasInterface(interface)) {
73 return 0;
74 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070075 if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(), mSecure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070076 mUidRanges)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070077 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
84int VirtualNetwork::removeInterface(const std::string& interface) {
85 if (!hasInterface(interface)) {
86 return 0;
87 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070088 if (int ret = RouteController::removeInterfaceFromVirtualNetwork(mNetId, interface.c_str(),
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070089 mSecure, mUidRanges)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070090 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}