blob: e0f60407c15ee8b5c21c40f9906e7bb504eb203d [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
Chiachang Wang2b0abee2022-01-12 10:03:17 +080030VirtualNetwork::VirtualNetwork(unsigned netId, bool secure, bool excludeLocalRoutes)
31 : Network(netId, secure), mExcludeLocalRoutes(excludeLocalRoutes) {}
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070032
cken67cd14c2018-12-05 17:26:59 +090033VirtualNetwork::~VirtualNetwork() {}
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070034
Ken Chen53360bf2021-12-10 02:41:05 +080035int VirtualNetwork::addUsers(const UidRanges& uidRanges, int32_t subPriority) {
chiachangwang65bc4ea2022-09-07 08:10:30 +000036 if (!isValidSubPriority(subPriority) || !canAddUidRanges(uidRanges)) {
Ken Chenc929e8a2021-03-22 11:37:17 +080037 return -EINVAL;
38 }
39
40 for (const std::string& interface : mInterfaces) {
41 int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(), mSecure,
Chiachang Wang2e9443f2022-01-14 22:55:36 +080042 {{subPriority, uidRanges}},
43 mExcludeLocalRoutes);
Ken Chenc929e8a2021-03-22 11:37:17 +080044 if (ret) {
45 ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId);
46 return ret;
47 }
48 }
Ken Chen4ea88462021-05-23 14:56:43 +080049 addToUidRangeMap(uidRanges, subPriority);
Ken Chenc929e8a2021-03-22 11:37:17 +080050 return 0;
51}
52
Ken Chen53360bf2021-12-10 02:41:05 +080053int VirtualNetwork::removeUsers(const UidRanges& uidRanges, int32_t subPriority) {
Ken Chen4ea88462021-05-23 14:56:43 +080054 if (!isValidSubPriority(subPriority)) return -EINVAL;
55
Ken Chenc929e8a2021-03-22 11:37:17 +080056 for (const std::string& interface : mInterfaces) {
57 int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(), mSecure,
Chiachang Wang2e9443f2022-01-14 22:55:36 +080058 {{subPriority, uidRanges}},
59 mExcludeLocalRoutes);
Ken Chenc929e8a2021-03-22 11:37:17 +080060 if (ret) {
61 ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId);
62 return ret;
63 }
64 }
Ken Chen4ea88462021-05-23 14:56:43 +080065 removeFromUidRangeMap(uidRanges, subPriority);
Ken Chenc929e8a2021-03-22 11:37:17 +080066 return 0;
67}
68
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070069int VirtualNetwork::addInterface(const std::string& interface) {
70 if (hasInterface(interface)) {
71 return 0;
72 }
Chiachang Wang2e9443f2022-01-14 22:55:36 +080073 if (int ret = RouteController::addInterfaceToVirtualNetwork(
74 mNetId, interface.c_str(), mSecure, mUidRangeMap, mExcludeLocalRoutes)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070075 ALOGE("failed to add interface %s to VPN netId %u", interface.c_str(), mNetId);
76 return ret;
77 }
78 mInterfaces.insert(interface);
79 return 0;
80}
81
82int VirtualNetwork::removeInterface(const std::string& interface) {
83 if (!hasInterface(interface)) {
84 return 0;
85 }
Chiachang Wang2e9443f2022-01-14 22:55:36 +080086 if (int ret = RouteController::removeInterfaceFromVirtualNetwork(
87 mNetId, interface.c_str(), mSecure, mUidRangeMap, mExcludeLocalRoutes)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070088 ALOGE("failed to remove interface %s from VPN netId %u", interface.c_str(), mNetId);
89 return ret;
90 }
91 mInterfaces.erase(interface);
92 return 0;
93}
Lorenzo Colitti7035f222017-02-13 18:29:00 +090094
Ken Chen53360bf2021-12-10 02:41:05 +080095bool VirtualNetwork::isValidSubPriority(int32_t priority) {
Ken Chen4ea88462021-05-23 14:56:43 +080096 // Only supports default subsidiary permissions.
Patrick Rohre2f1b5a2022-01-25 21:36:50 +010097 return priority == UidRanges::SUB_PRIORITY_HIGHEST;
Ken Chen4ea88462021-05-23 14:56:43 +080098}
99
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900100} // namespace net
101} // namespace android