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
17 #include <set>
18
19 #define LOG_TAG "Netd"
20
21 #include "VirtualNetwork.h"
22
23 #include "RouteController.h"
24
25 #include "log/log.h"
26
27 namespace android {
28 namespace net {
29
VirtualNetwork(unsigned netId,bool secure,bool excludeLocalRoutes)30 VirtualNetwork::VirtualNetwork(unsigned netId, bool secure, bool excludeLocalRoutes)
31 : Network(netId, secure), mExcludeLocalRoutes(excludeLocalRoutes) {}
32
~VirtualNetwork()33 VirtualNetwork::~VirtualNetwork() {}
34
addUsers(const UidRanges & uidRanges,int32_t subPriority)35 int VirtualNetwork::addUsers(const UidRanges& uidRanges, int32_t subPriority) {
36 if (!isValidSubPriority(subPriority) || !canAddUidRanges(uidRanges)) {
37 return -EINVAL;
38 }
39
40 for (const std::string& interface : mInterfaces) {
41 int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(), mSecure,
42 {{subPriority, uidRanges}},
43 mExcludeLocalRoutes);
44 if (ret) {
45 ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId);
46 return ret;
47 }
48 }
49 addToUidRangeMap(uidRanges, subPriority);
50 return 0;
51 }
52
removeUsers(const UidRanges & uidRanges,int32_t subPriority)53 int VirtualNetwork::removeUsers(const UidRanges& uidRanges, int32_t subPriority) {
54 if (!isValidSubPriority(subPriority)) return -EINVAL;
55
56 for (const std::string& interface : mInterfaces) {
57 int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(), mSecure,
58 {{subPriority, uidRanges}},
59 mExcludeLocalRoutes);
60 if (ret) {
61 ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId);
62 return ret;
63 }
64 }
65 removeFromUidRangeMap(uidRanges, subPriority);
66 return 0;
67 }
68
addInterface(const std::string & interface)69 int VirtualNetwork::addInterface(const std::string& interface) {
70 if (hasInterface(interface)) {
71 return 0;
72 }
73 if (int ret = RouteController::addInterfaceToVirtualNetwork(
74 mNetId, interface.c_str(), mSecure, mUidRangeMap, mExcludeLocalRoutes)) {
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
removeInterface(const std::string & interface)82 int VirtualNetwork::removeInterface(const std::string& interface) {
83 if (!hasInterface(interface)) {
84 return 0;
85 }
86 if (int ret = RouteController::removeInterfaceFromVirtualNetwork(
87 mNetId, interface.c_str(), mSecure, mUidRangeMap, mExcludeLocalRoutes)) {
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 }
94
isValidSubPriority(int32_t priority)95 bool VirtualNetwork::isValidSubPriority(int32_t priority) {
96 // Only supports default subsidiary permissions.
97 return priority == UidRanges::SUB_PRIORITY_HIGHEST;
98 }
99
100 } // namespace net
101 } // namespace android
102