blob: ccac32333b2ca2672e6fe8fd5f957f6aa20fdb14 [file] [log] [blame]
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -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
17#include "PhysicalNetwork.h"
18
19#include "RouteController.h"
Lorenzo Colittic6201c32016-09-14 02:25:05 +090020#include "SockDiag.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070021
22#define LOG_TAG "Netd"
23#include "log/log.h"
24
Lorenzo Colitti7035f222017-02-13 18:29:00 +090025namespace android {
26namespace net {
27
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070028namespace {
29
30WARN_UNUSED_RESULT int addToDefault(unsigned netId, const std::string& interface,
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070031 Permission permission, PhysicalNetwork::Delegate* delegate) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070032 if (int ret = RouteController::addInterfaceToDefaultNetwork(interface.c_str(), permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070033 ALOGE("failed to add interface %s to default netId %u", interface.c_str(), netId);
34 return ret;
35 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070036 if (int ret = delegate->addFallthrough(interface, permission)) {
37 return ret;
38 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070039 return 0;
40}
41
42WARN_UNUSED_RESULT int removeFromDefault(unsigned netId, const std::string& interface,
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070043 Permission permission,
44 PhysicalNetwork::Delegate* delegate) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070045 if (int ret = RouteController::removeInterfaceFromDefaultNetwork(interface.c_str(),
46 permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070047 ALOGE("failed to remove interface %s from default netId %u", interface.c_str(), netId);
48 return ret;
49 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070050 if (int ret = delegate->removeFallthrough(interface, permission)) {
51 return ret;
52 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070053 return 0;
54}
55
56} // namespace
57
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070058PhysicalNetwork::Delegate::~Delegate() {
59}
60
61PhysicalNetwork::PhysicalNetwork(unsigned netId, PhysicalNetwork::Delegate* delegate) :
62 Network(netId), mDelegate(delegate), mPermission(PERMISSION_NONE), mIsDefault(false) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070063}
64
65PhysicalNetwork::~PhysicalNetwork() {
66}
67
68Permission PhysicalNetwork::getPermission() const {
69 return mPermission;
70}
71
Lorenzo Colittic6201c32016-09-14 02:25:05 +090072int PhysicalNetwork::destroySocketsLackingPermission(Permission permission) {
73 if (permission == PERMISSION_NONE) return 0;
74
75 SockDiag sd;
76 if (!sd.open()) {
77 ALOGE("Error closing sockets for netId %d permission change", mNetId);
78 return -EBADFD;
79 }
80 if (int ret = sd.destroySocketsLackingPermission(mNetId, permission,
81 true /* excludeLoopback */)) {
82 ALOGE("Failed to close sockets changing netId %d to permission %d: %s",
83 mNetId, permission, strerror(-ret));
84 return ret;
85 }
86 return 0;
87}
88
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070089int PhysicalNetwork::setPermission(Permission permission) {
90 if (permission == mPermission) {
91 return 0;
92 }
Lorenzo Colittic6201c32016-09-14 02:25:05 +090093 if (mInterfaces.empty()) {
94 mPermission = permission;
95 return 0;
96 }
97
98 destroySocketsLackingPermission(permission);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070099 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700100 if (int ret = RouteController::modifyPhysicalNetworkPermission(mNetId, interface.c_str(),
101 mPermission, permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700102 ALOGE("failed to change permission on interface %s of netId %u from %x to %x",
103 interface.c_str(), mNetId, mPermission, permission);
104 return ret;
105 }
106 }
107 if (mIsDefault) {
108 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700109 if (int ret = addToDefault(mNetId, interface, permission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700110 return ret;
111 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700112 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700113 return ret;
114 }
115 }
116 }
Lorenzo Colittic6201c32016-09-14 02:25:05 +0900117 // Destroy sockets again in case any were opened after we called destroySocketsLackingPermission
118 // above and before we changed the permissions. These sockets won't be able to send any RST
119 // packets because they are now no longer routed, but at least the apps will get errors.
120 destroySocketsLackingPermission(permission);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700121 mPermission = permission;
122 return 0;
123}
124
125int PhysicalNetwork::addAsDefault() {
126 if (mIsDefault) {
127 return 0;
128 }
129 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700130 if (int ret = addToDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700131 return ret;
132 }
133 }
134 mIsDefault = true;
135 return 0;
136}
137
138int PhysicalNetwork::removeAsDefault() {
139 if (!mIsDefault) {
140 return 0;
141 }
142 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700143 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700144 return ret;
145 }
146 }
147 mIsDefault = false;
148 return 0;
149}
150
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700151Network::Type PhysicalNetwork::getType() const {
152 return PHYSICAL;
153}
154
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700155int PhysicalNetwork::addInterface(const std::string& interface) {
156 if (hasInterface(interface)) {
157 return 0;
158 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700159 if (int ret = RouteController::addInterfaceToPhysicalNetwork(mNetId, interface.c_str(),
160 mPermission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700161 ALOGE("failed to add interface %s to netId %u", interface.c_str(), mNetId);
162 return ret;
163 }
164 if (mIsDefault) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700165 if (int ret = addToDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700166 return ret;
167 }
168 }
169 mInterfaces.insert(interface);
170 return 0;
171}
172
173int PhysicalNetwork::removeInterface(const std::string& interface) {
174 if (!hasInterface(interface)) {
175 return 0;
176 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700177 if (mIsDefault) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700178 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700179 return ret;
180 }
181 }
Paul Jensen6d7e6232014-08-01 10:54:03 -0400182 // This step will flush the interface index from the cache in RouteController so it must be
183 // done last as further requests to the RouteController regarding this interface will fail
184 // to find the interface index in the cache in cases where the interface is already gone
185 // (e.g. bt-pan).
186 if (int ret = RouteController::removeInterfaceFromPhysicalNetwork(mNetId, interface.c_str(),
187 mPermission)) {
188 ALOGE("failed to remove interface %s from netId %u", interface.c_str(), mNetId);
189 return ret;
190 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700191 mInterfaces.erase(interface);
192 return 0;
193}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900194
195} // namespace net
196} // namespace android