blob: dd6318c0dfe9df02d2c5bc476c0c6c4b386a0d80 [file] [log] [blame]
Ken Chen4e8ef9b2021-03-17 01:57:19 +08001/*
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
23namespace android {
24namespace net {
25
26// The unreachable network is used to reject traffic. It is used for system purposes only.
27UnreachableNetwork::UnreachableNetwork(unsigned netId) : Network(netId) {}
28
Ken Chen53360bf2021-12-10 02:41:05 +080029int UnreachableNetwork::addUsers(const UidRanges& uidRanges, int32_t subPriority) {
chiachangwang65bc4ea2022-09-07 08:10:30 +000030 if (!isValidSubPriority(subPriority) || !canAddUidRanges(uidRanges)) {
Ken Chen4e8ef9b2021-03-17 01:57:19 +080031 return -EINVAL;
32 }
33
Ken Chen4ea88462021-05-23 14:56:43 +080034 int ret = RouteController::addUsersToUnreachableNetwork(mNetId, {{subPriority, uidRanges}});
Ken Chen4e8ef9b2021-03-17 01:57:19 +080035 if (ret) {
36 ALOGE("failed to add users to unreachable network");
37 return ret;
38 }
Ken Chen4ea88462021-05-23 14:56:43 +080039 addToUidRangeMap(uidRanges, subPriority);
Ken Chen4e8ef9b2021-03-17 01:57:19 +080040 return 0;
41}
42
Ken Chen53360bf2021-12-10 02:41:05 +080043int UnreachableNetwork::removeUsers(const UidRanges& uidRanges, int32_t subPriority) {
Ken Chen4ea88462021-05-23 14:56:43 +080044 if (!isValidSubPriority(subPriority)) return -EINVAL;
45
46 int ret =
47 RouteController::removeUsersFromUnreachableNetwork(mNetId, {{subPriority, uidRanges}});
Ken Chen4e8ef9b2021-03-17 01:57:19 +080048 if (ret) {
49 ALOGE("failed to remove users from unreachable network");
50 return ret;
51 }
Ken Chen4ea88462021-05-23 14:56:43 +080052 removeFromUidRangeMap(uidRanges, subPriority);
Ken Chen4e8ef9b2021-03-17 01:57:19 +080053 return 0;
54}
55
Ken Chen53360bf2021-12-10 02:41:05 +080056bool UnreachableNetwork::isValidSubPriority(int32_t priority) {
Patrick Rohre2f1b5a2022-01-25 21:36:50 +010057 return priority >= UidRanges::SUB_PRIORITY_HIGHEST &&
58 priority <= UidRanges::SUB_PRIORITY_LOWEST;
Ken Chen4ea88462021-05-23 14:56:43 +080059}
60
Ken Chen4e8ef9b2021-03-17 01:57:19 +080061} // namespace net
62} // namespace android