Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 17 | #include <set> |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 18 | |
| 19 | #define LOG_TAG "Netd" |
| 20 | |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 21 | #include "VirtualNetwork.h" |
| 22 | |
| 23 | #include "RouteController.h" |
| 24 | |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 25 | #include "log/log.h" |
| 26 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace net { |
| 29 | |
Chiachang Wang | 2b0abee | 2022-01-12 10:03:17 +0800 | [diff] [blame] | 30 | VirtualNetwork::VirtualNetwork(unsigned netId, bool secure, bool excludeLocalRoutes) |
| 31 | : Network(netId, secure), mExcludeLocalRoutes(excludeLocalRoutes) {} |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 32 | |
cken | 67cd14c | 2018-12-05 17:26:59 +0900 | [diff] [blame] | 33 | VirtualNetwork::~VirtualNetwork() {} |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 34 | |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 35 | int VirtualNetwork::addUsers(const UidRanges& uidRanges, int32_t subPriority) { |
chiachangwang | 65bc4ea | 2022-09-07 08:10:30 +0000 | [diff] [blame] | 36 | if (!isValidSubPriority(subPriority) || !canAddUidRanges(uidRanges)) { |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 37 | return -EINVAL; |
| 38 | } |
| 39 | |
| 40 | for (const std::string& interface : mInterfaces) { |
| 41 | int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(), mSecure, |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 42 | {{subPriority, uidRanges}}, |
| 43 | mExcludeLocalRoutes); |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 44 | if (ret) { |
| 45 | ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId); |
| 46 | return ret; |
| 47 | } |
| 48 | } |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 49 | addToUidRangeMap(uidRanges, subPriority); |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 53 | int VirtualNetwork::removeUsers(const UidRanges& uidRanges, int32_t subPriority) { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 54 | if (!isValidSubPriority(subPriority)) return -EINVAL; |
| 55 | |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 56 | for (const std::string& interface : mInterfaces) { |
| 57 | int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(), mSecure, |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 58 | {{subPriority, uidRanges}}, |
| 59 | mExcludeLocalRoutes); |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 60 | if (ret) { |
| 61 | ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId); |
| 62 | return ret; |
| 63 | } |
| 64 | } |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 65 | removeFromUidRangeMap(uidRanges, subPriority); |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 69 | int VirtualNetwork::addInterface(const std::string& interface) { |
| 70 | if (hasInterface(interface)) { |
| 71 | return 0; |
| 72 | } |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 73 | if (int ret = RouteController::addInterfaceToVirtualNetwork( |
| 74 | mNetId, interface.c_str(), mSecure, mUidRangeMap, mExcludeLocalRoutes)) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 75 | 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 | |
| 82 | int VirtualNetwork::removeInterface(const std::string& interface) { |
| 83 | if (!hasInterface(interface)) { |
| 84 | return 0; |
| 85 | } |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 86 | if (int ret = RouteController::removeInterfaceFromVirtualNetwork( |
| 87 | mNetId, interface.c_str(), mSecure, mUidRangeMap, mExcludeLocalRoutes)) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 88 | 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 Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 94 | |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 95 | bool VirtualNetwork::isValidSubPriority(int32_t priority) { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 96 | // Only supports default subsidiary permissions. |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 97 | return priority == UidRanges::SUB_PRIORITY_HIGHEST; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 98 | } |
| 99 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 100 | } // namespace net |
| 101 | } // namespace android |