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.
UnreachableNetwork(unsigned netId)27 UnreachableNetwork::UnreachableNetwork(unsigned netId) : Network(netId) {}
28
addUsers(const UidRanges & uidRanges,uint32_t subPriority)29 int UnreachableNetwork::addUsers(const UidRanges& uidRanges, uint32_t subPriority) {
30 if (!isValidSubPriority(subPriority) || !canAddUidRanges(uidRanges, subPriority)) {
31 return -EINVAL;
32 }
33
34 int ret = RouteController::addUsersToUnreachableNetwork(mNetId, {{subPriority, uidRanges}});
35 if (ret) {
36 ALOGE("failed to add users to unreachable network");
37 return ret;
38 }
39 addToUidRangeMap(uidRanges, subPriority);
40 return 0;
41 }
42
removeUsers(const UidRanges & uidRanges,uint32_t subPriority)43 int UnreachableNetwork::removeUsers(const UidRanges& uidRanges, uint32_t subPriority) {
44 if (!isValidSubPriority(subPriority)) return -EINVAL;
45
46 int ret =
47 RouteController::removeUsersFromUnreachableNetwork(mNetId, {{subPriority, uidRanges}});
48 if (ret) {
49 ALOGE("failed to remove users from unreachable network");
50 return ret;
51 }
52 removeFromUidRangeMap(uidRanges, subPriority);
53 return 0;
54 }
55
isValidSubPriority(uint32_t priority)56 bool UnreachableNetwork::isValidSubPriority(uint32_t priority) {
57 return priority >= UidRanges::DEFAULT_SUB_PRIORITY &&
58 priority <= UidRanges::LOWEST_SUB_PRIORITY;
59 }
60
61 } // namespace net
62 } // namespace android
63