blob: 1906e2086416c9666cca1985fcbd0968551606c1 [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
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090017#include <set>
Luke Huang94658ac2018-10-18 19:35:12 +090018
19#define LOG_TAG "Netd"
20
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070021#include "VirtualNetwork.h"
22
23#include "RouteController.h"
24
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070025#include "log/log.h"
26
Lorenzo Colitti7035f222017-02-13 18:29:00 +090027namespace android {
28namespace net {
29
Ken Chen1a3a3272020-12-04 04:03:08 +080030VirtualNetwork::VirtualNetwork(unsigned netId, bool secure) : Network(netId, secure) {}
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070031
cken67cd14c2018-12-05 17:26:59 +090032VirtualNetwork::~VirtualNetwork() {}
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070033
Ken Chend9aa98a2021-05-23 14:56:43 +080034int VirtualNetwork::addUsers(const UidRanges& uidRanges, uint32_t subPriority) {
35 if (!isValidSubPriority(subPriority) || !canAddUidRanges(uidRanges, subPriority)) {
Ken Chenc929e8a2021-03-22 11:37:17 +080036 return -EINVAL;
37 }
38
39 for (const std::string& interface : mInterfaces) {
40 int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(), mSecure,
Ken Chend9aa98a2021-05-23 14:56:43 +080041 {{subPriority, uidRanges}});
Ken Chenc929e8a2021-03-22 11:37:17 +080042 if (ret) {
43 ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId);
44 return ret;
45 }
46 }
Ken Chend9aa98a2021-05-23 14:56:43 +080047 addToUidRangeMap(uidRanges, subPriority);
Ken Chenc929e8a2021-03-22 11:37:17 +080048 return 0;
49}
50
Ken Chend9aa98a2021-05-23 14:56:43 +080051int VirtualNetwork::removeUsers(const UidRanges& uidRanges, uint32_t subPriority) {
52 if (!isValidSubPriority(subPriority)) return -EINVAL;
53
Ken Chenc929e8a2021-03-22 11:37:17 +080054 for (const std::string& interface : mInterfaces) {
55 int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(), mSecure,
Ken Chend9aa98a2021-05-23 14:56:43 +080056 {{subPriority, uidRanges}});
Ken Chenc929e8a2021-03-22 11:37:17 +080057 if (ret) {
58 ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId);
59 return ret;
60 }
61 }
Ken Chend9aa98a2021-05-23 14:56:43 +080062 removeFromUidRangeMap(uidRanges, subPriority);
Ken Chenc929e8a2021-03-22 11:37:17 +080063 return 0;
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 Ramachandran95684ba2014-07-23 13:27:31 -070070 if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(), mSecure,
Ken Chend9aa98a2021-05-23 14:56:43 +080071 mUidRangeMap)) {
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(),
Ken Chend9aa98a2021-05-23 14:56:43 +080084 mSecure, mUidRangeMap)) {
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}
Lorenzo Colitti7035f222017-02-13 18:29:00 +090091
Ken Chend9aa98a2021-05-23 14:56:43 +080092bool VirtualNetwork::isValidSubPriority(uint32_t priority) {
93 // Only supports default subsidiary permissions.
94 return priority == UidRanges::DEFAULT_SUB_PRIORITY;
95}
96
Lorenzo Colitti7035f222017-02-13 18:29:00 +090097} // namespace net
98} // namespace android