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)30 VirtualNetwork::VirtualNetwork(unsigned netId, bool secure) : Network(netId, secure) {}
31 
~VirtualNetwork()32 VirtualNetwork::~VirtualNetwork() {}
33 
addUsers(const UidRanges & uidRanges,uint32_t subPriority)34 int VirtualNetwork::addUsers(const UidRanges& uidRanges, uint32_t subPriority) {
35     if (!isValidSubPriority(subPriority) || !canAddUidRanges(uidRanges, subPriority)) {
36         return -EINVAL;
37     }
38 
39     for (const std::string& interface : mInterfaces) {
40         int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(), mSecure,
41                                                             {{subPriority, uidRanges}});
42         if (ret) {
43             ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId);
44             return ret;
45         }
46     }
47     addToUidRangeMap(uidRanges, subPriority);
48     return 0;
49 }
50 
removeUsers(const UidRanges & uidRanges,uint32_t subPriority)51 int VirtualNetwork::removeUsers(const UidRanges& uidRanges, uint32_t subPriority) {
52     if (!isValidSubPriority(subPriority)) return -EINVAL;
53 
54     for (const std::string& interface : mInterfaces) {
55         int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(), mSecure,
56                                                                  {{subPriority, uidRanges}});
57         if (ret) {
58             ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId);
59             return ret;
60         }
61     }
62     removeFromUidRangeMap(uidRanges, subPriority);
63     return 0;
64 }
65 
addInterface(const std::string & interface)66 int VirtualNetwork::addInterface(const std::string& interface) {
67     if (hasInterface(interface)) {
68         return 0;
69     }
70     if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(), mSecure,
71                                                                 mUidRangeMap)) {
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 
removeInterface(const std::string & interface)79 int VirtualNetwork::removeInterface(const std::string& interface) {
80     if (!hasInterface(interface)) {
81         return 0;
82     }
83     if (int ret = RouteController::removeInterfaceFromVirtualNetwork(mNetId, interface.c_str(),
84                                                                      mSecure, mUidRangeMap)) {
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 }
91 
isValidSubPriority(uint32_t priority)92 bool VirtualNetwork::isValidSubPriority(uint32_t priority) {
93     // Only supports default subsidiary permissions.
94     return priority == UidRanges::DEFAULT_SUB_PRIORITY;
95 }
96 
97 }  // namespace net
98 }  // namespace android
99