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 | |
Ken Chen | 1a3a327 | 2020-12-04 04:03:08 +0800 | [diff] [blame] | 30 | VirtualNetwork::VirtualNetwork(unsigned netId, bool secure) : Network(netId, secure) {} |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 31 | |
cken | 67cd14c | 2018-12-05 17:26:59 +0900 | [diff] [blame] | 32 | VirtualNetwork::~VirtualNetwork() {} |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 33 | |
Ken Chen | d9aa98a | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 34 | int VirtualNetwork::addUsers(const UidRanges& uidRanges, uint32_t subPriority) { |
| 35 | if (!isValidSubPriority(subPriority) || !canAddUidRanges(uidRanges, subPriority)) { |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 36 | return -EINVAL; |
| 37 | } |
| 38 | |
| 39 | for (const std::string& interface : mInterfaces) { |
| 40 | int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(), mSecure, |
Ken Chen | d9aa98a | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 41 | {{subPriority, uidRanges}}); |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 42 | if (ret) { |
| 43 | ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId); |
| 44 | return ret; |
| 45 | } |
| 46 | } |
Ken Chen | d9aa98a | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 47 | addToUidRangeMap(uidRanges, subPriority); |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | |
Ken Chen | d9aa98a | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 51 | int VirtualNetwork::removeUsers(const UidRanges& uidRanges, uint32_t subPriority) { |
| 52 | if (!isValidSubPriority(subPriority)) return -EINVAL; |
| 53 | |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 54 | for (const std::string& interface : mInterfaces) { |
| 55 | int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(), mSecure, |
Ken Chen | d9aa98a | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 56 | {{subPriority, uidRanges}}); |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 57 | if (ret) { |
| 58 | ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId); |
| 59 | return ret; |
| 60 | } |
| 61 | } |
Ken Chen | d9aa98a | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 62 | removeFromUidRangeMap(uidRanges, subPriority); |
Ken Chen | c929e8a | 2021-03-22 11:37:17 +0800 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 66 | int VirtualNetwork::addInterface(const std::string& interface) { |
| 67 | if (hasInterface(interface)) { |
| 68 | return 0; |
| 69 | } |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 70 | if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(), mSecure, |
Ken Chen | d9aa98a | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 71 | mUidRangeMap)) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 72 | 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 | |
| 79 | int VirtualNetwork::removeInterface(const std::string& interface) { |
| 80 | if (!hasInterface(interface)) { |
| 81 | return 0; |
| 82 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 83 | if (int ret = RouteController::removeInterfaceFromVirtualNetwork(mNetId, interface.c_str(), |
Ken Chen | d9aa98a | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 84 | mSecure, mUidRangeMap)) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 85 | 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 Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 91 | |
Ken Chen | d9aa98a | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 92 | bool VirtualNetwork::isValidSubPriority(uint32_t priority) { |
| 93 | // Only supports default subsidiary permissions. |
| 94 | return priority == UidRanges::DEFAULT_SUB_PRIORITY; |
| 95 | } |
| 96 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 97 | } // namespace net |
| 98 | } // namespace android |