Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | #define LOG_TAG "Netd" |
| 18 | |
| 19 | #include "UnreachableNetwork.h" |
| 20 | |
| 21 | #include "RouteController.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace net { |
| 25 | |
| 26 | // The unreachable network is used to reject traffic. It is used for system purposes only. |
| 27 | UnreachableNetwork::UnreachableNetwork(unsigned netId) : Network(netId) {} |
| 28 | |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 29 | int UnreachableNetwork::addUsers(const UidRanges& uidRanges, int32_t subPriority) { |
chiachangwang | 65bc4ea | 2022-09-07 08:10:30 +0000 | [diff] [blame] | 30 | if (!isValidSubPriority(subPriority) || !canAddUidRanges(uidRanges)) { |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 31 | return -EINVAL; |
| 32 | } |
| 33 | |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 34 | int ret = RouteController::addUsersToUnreachableNetwork(mNetId, {{subPriority, uidRanges}}); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 35 | if (ret) { |
| 36 | ALOGE("failed to add users to unreachable network"); |
| 37 | return ret; |
| 38 | } |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 39 | addToUidRangeMap(uidRanges, subPriority); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 40 | return 0; |
| 41 | } |
| 42 | |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 43 | int UnreachableNetwork::removeUsers(const UidRanges& uidRanges, int32_t subPriority) { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 44 | if (!isValidSubPriority(subPriority)) return -EINVAL; |
| 45 | |
| 46 | int ret = |
| 47 | RouteController::removeUsersFromUnreachableNetwork(mNetId, {{subPriority, uidRanges}}); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 48 | if (ret) { |
| 49 | ALOGE("failed to remove users from unreachable network"); |
| 50 | return ret; |
| 51 | } |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 52 | removeFromUidRangeMap(uidRanges, subPriority); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 56 | bool UnreachableNetwork::isValidSubPriority(int32_t priority) { |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 57 | return priority >= UidRanges::SUB_PRIORITY_HIGHEST && |
| 58 | priority <= UidRanges::SUB_PRIORITY_LOWEST; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 59 | } |
| 60 | |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 61 | } // namespace net |
| 62 | } // namespace android |