blob: 49418eb2b4b35677a78f3d63f3a0e862bc48c647 [file] [log] [blame]
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001/*
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
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090017#include <set>
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070018#include "VirtualNetwork.h"
19
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090020#include "SockDiag.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070021#include "RouteController.h"
22
23#define LOG_TAG "Netd"
24#include "log/log.h"
25
Lorenzo Colitti7035f222017-02-13 18:29:00 +090026namespace android {
27namespace net {
28
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070029VirtualNetwork::VirtualNetwork(unsigned netId, bool hasDns, bool secure) :
30 Network(netId), mHasDns(hasDns), mSecure(secure) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070031}
32
33VirtualNetwork::~VirtualNetwork() {
34}
35
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070036bool VirtualNetwork::getHasDns() const {
37 return mHasDns;
38}
39
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070040bool VirtualNetwork::isSecure() const {
41 return mSecure;
42}
43
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070044bool VirtualNetwork::appliesToUser(uid_t uid) const {
45 return mUidRanges.hasUid(uid);
46}
47
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090048
49int VirtualNetwork::maybeCloseSockets(bool add, const UidRanges& uidRanges,
50 const std::set<uid_t>& protectableUsers) {
51 if (!mSecure) {
52 return 0;
53 }
54
55 SockDiag sd;
56 if (!sd.open()) {
57 return -EBADFD;
58 }
59
Lorenzo Colitti0726fec2016-07-26 17:53:50 +090060 if (int ret = sd.destroySockets(uidRanges, protectableUsers, true /* excludeLoopback */)) {
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090061 ALOGE("Failed to close sockets while %s %s to network %d: %s",
62 add ? "adding" : "removing", uidRanges.toString().c_str(), mNetId, strerror(-ret));
63 return ret;
64 }
65
66 return 0;
67}
68
69int VirtualNetwork::addUsers(const UidRanges& uidRanges, const std::set<uid_t>& protectableUsers) {
70 maybeCloseSockets(true, uidRanges, protectableUsers);
71
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070072 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070073 if (int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(), mSecure,
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070074 uidRanges)) {
75 ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId);
76 return ret;
77 }
78 }
79 mUidRanges.add(uidRanges);
80 return 0;
81}
82
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090083int VirtualNetwork::removeUsers(const UidRanges& uidRanges,
84 const std::set<uid_t>& protectableUsers) {
85 maybeCloseSockets(false, uidRanges, protectableUsers);
86
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070087 for (const std::string& interface : mInterfaces) {
88 if (int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(),
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070089 mSecure, uidRanges)) {
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070090 ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId);
91 return ret;
92 }
93 }
94 mUidRanges.remove(uidRanges);
95 return 0;
96}
97
98Network::Type VirtualNetwork::getType() const {
99 return VIRTUAL;
100}
101
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700102int VirtualNetwork::addInterface(const std::string& interface) {
103 if (hasInterface(interface)) {
104 return 0;
105 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700106 if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(), mSecure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700107 mUidRanges)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700108 ALOGE("failed to add interface %s to VPN netId %u", interface.c_str(), mNetId);
109 return ret;
110 }
111 mInterfaces.insert(interface);
112 return 0;
113}
114
115int VirtualNetwork::removeInterface(const std::string& interface) {
116 if (!hasInterface(interface)) {
117 return 0;
118 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700119 if (int ret = RouteController::removeInterfaceFromVirtualNetwork(mNetId, interface.c_str(),
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700120 mSecure, mUidRanges)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700121 ALOGE("failed to remove interface %s from VPN netId %u", interface.c_str(), mNetId);
122 return ret;
123 }
124 mInterfaces.erase(interface);
125 return 0;
126}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900127
128} // namespace net
129} // namespace android