blob: bb3f653d88b785d563807becf46f9c876c32a498 [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
Luke Huang94658ac2018-10-18 19:35:12 +090017#define LOG_TAG "Netd"
18
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070019#include "PhysicalNetwork.h"
20
21#include "RouteController.h"
Lorenzo Colittic6201c32016-09-14 02:25:05 +090022#include "SockDiag.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070023
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070024#include "log/log.h"
25
Bernie Innocenti762dcf42019-06-14 19:52:49 +090026namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090027
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070028namespace {
29
Bernie Innocenti762dcf42019-06-14 19:52:49 +090030[[nodiscard]] int addToDefault(unsigned netId, const std::string& interface, Permission permission,
31 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
Bernie Innocenti762dcf42019-06-14 19:52:49 +090042[[nodiscard]] int removeFromDefault(unsigned netId, const std::string& interface,
43 Permission permission, PhysicalNetwork::Delegate* delegate) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070044 if (int ret = RouteController::removeInterfaceFromDefaultNetwork(interface.c_str(),
45 permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070046 ALOGE("failed to remove interface %s from default netId %u", interface.c_str(), netId);
47 return ret;
48 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070049 if (int ret = delegate->removeFallthrough(interface, permission)) {
50 return ret;
51 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070052 return 0;
53}
54
55} // namespace
56
Bernie Innocenti762dcf42019-06-14 19:52:49 +090057PhysicalNetwork::Delegate::~Delegate() {}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070058
59PhysicalNetwork::PhysicalNetwork(unsigned netId, PhysicalNetwork::Delegate* delegate) :
60 Network(netId), mDelegate(delegate), mPermission(PERMISSION_NONE), mIsDefault(false) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070061}
62
Bernie Innocenti762dcf42019-06-14 19:52:49 +090063PhysicalNetwork::~PhysicalNetwork() {}
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070064
65Permission PhysicalNetwork::getPermission() const {
66 return mPermission;
67}
68
Lorenzo Colittic6201c32016-09-14 02:25:05 +090069int PhysicalNetwork::destroySocketsLackingPermission(Permission permission) {
70 if (permission == PERMISSION_NONE) return 0;
71
72 SockDiag sd;
73 if (!sd.open()) {
74 ALOGE("Error closing sockets for netId %d permission change", mNetId);
75 return -EBADFD;
76 }
77 if (int ret = sd.destroySocketsLackingPermission(mNetId, permission,
78 true /* excludeLoopback */)) {
79 ALOGE("Failed to close sockets changing netId %d to permission %d: %s",
80 mNetId, permission, strerror(-ret));
81 return ret;
82 }
83 return 0;
84}
85
Lorenzo Colitti4662e162017-09-08 11:31:59 +090086void PhysicalNetwork::invalidateRouteCache(const std::string& interface) {
Taras Antoshchuk0cceda22021-09-24 18:40:03 +020087 // This method invalidates all socket destination cache entries in the kernel by creating and
88 // removing a low-priority route.
89 // This number is an arbitrary number that need to be higher than any other route created either
90 // by netd or by an IPv6 RouterAdvertisement.
91 int priority = 100000;
92
Lorenzo Colitti4662e162017-09-08 11:31:59 +090093 for (const auto& dst : { "0.0.0.0/0", "::/0" }) {
94 // If any of these operations fail, there's no point in logging because RouteController will
95 // have already logged a message. There's also no point returning an error since there's
96 // nothing we can do.
Tyler Wearfa94a272019-12-05 15:01:48 -080097 (void)RouteController::addRoute(interface.c_str(), dst, "throw", RouteController::INTERFACE,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +020098 0 /* mtu */, priority);
99 (void)RouteController::removeRoute(interface.c_str(), dst, "throw",
100 RouteController::INTERFACE, priority);
Lorenzo Colitti4662e162017-09-08 11:31:59 +0900101 }
102}
103
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700104int PhysicalNetwork::setPermission(Permission permission) {
105 if (permission == mPermission) {
106 return 0;
107 }
Lorenzo Colittic6201c32016-09-14 02:25:05 +0900108 if (mInterfaces.empty()) {
109 mPermission = permission;
110 return 0;
111 }
112
113 destroySocketsLackingPermission(permission);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700114 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700115 if (int ret = RouteController::modifyPhysicalNetworkPermission(mNetId, interface.c_str(),
116 mPermission, permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700117 ALOGE("failed to change permission on interface %s of netId %u from %x to %x",
118 interface.c_str(), mNetId, mPermission, permission);
119 return ret;
120 }
Lorenzo Colitti4662e162017-09-08 11:31:59 +0900121 invalidateRouteCache(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700122 }
123 if (mIsDefault) {
124 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700125 if (int ret = addToDefault(mNetId, interface, permission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700126 return ret;
127 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700128 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700129 return ret;
130 }
131 }
132 }
Lorenzo Colittic6201c32016-09-14 02:25:05 +0900133 // Destroy sockets again in case any were opened after we called destroySocketsLackingPermission
134 // above and before we changed the permissions. These sockets won't be able to send any RST
135 // packets because they are now no longer routed, but at least the apps will get errors.
136 destroySocketsLackingPermission(permission);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700137 mPermission = permission;
138 return 0;
139}
140
141int PhysicalNetwork::addAsDefault() {
142 if (mIsDefault) {
143 return 0;
144 }
145 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700146 if (int ret = addToDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700147 return ret;
148 }
149 }
150 mIsDefault = true;
151 return 0;
152}
153
154int PhysicalNetwork::removeAsDefault() {
155 if (!mIsDefault) {
156 return 0;
157 }
158 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700159 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700160 return ret;
161 }
162 }
163 mIsDefault = false;
164 return 0;
165}
166
Ken Chen53360bf2021-12-10 02:41:05 +0800167int PhysicalNetwork::addUsers(const UidRanges& uidRanges, int32_t subPriority) {
Ken Chen4ea88462021-05-23 14:56:43 +0800168 if (!isValidSubPriority(subPriority) || !canAddUidRanges(uidRanges, subPriority)) {
Ken Chenc929e8a2021-03-22 11:37:17 +0800169 return -EINVAL;
170 }
171
172 for (const std::string& interface : mInterfaces) {
Ken Chen4ea88462021-05-23 14:56:43 +0800173 int ret = RouteController::addUsersToPhysicalNetwork(mNetId, interface.c_str(),
174 {{subPriority, uidRanges}});
Ken Chenc929e8a2021-03-22 11:37:17 +0800175 if (ret) {
176 ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId);
177 return ret;
178 }
179 }
Ken Chen4ea88462021-05-23 14:56:43 +0800180 addToUidRangeMap(uidRanges, subPriority);
Ken Chenc929e8a2021-03-22 11:37:17 +0800181 return 0;
182}
183
Ken Chen53360bf2021-12-10 02:41:05 +0800184int PhysicalNetwork::removeUsers(const UidRanges& uidRanges, int32_t subPriority) {
Ken Chen4ea88462021-05-23 14:56:43 +0800185 if (!isValidSubPriority(subPriority)) return -EINVAL;
186
Ken Chenc929e8a2021-03-22 11:37:17 +0800187 for (const std::string& interface : mInterfaces) {
188 int ret = RouteController::removeUsersFromPhysicalNetwork(mNetId, interface.c_str(),
Ken Chen4ea88462021-05-23 14:56:43 +0800189 {{subPriority, uidRanges}});
Ken Chenc929e8a2021-03-22 11:37:17 +0800190 if (ret) {
191 ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId);
192 return ret;
193 }
194 }
Ken Chen4ea88462021-05-23 14:56:43 +0800195 removeFromUidRangeMap(uidRanges, subPriority);
Ken Chenc929e8a2021-03-22 11:37:17 +0800196 return 0;
197}
198
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700199int PhysicalNetwork::addInterface(const std::string& interface) {
200 if (hasInterface(interface)) {
201 return 0;
202 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700203 if (int ret = RouteController::addInterfaceToPhysicalNetwork(mNetId, interface.c_str(),
Ken Chen4ea88462021-05-23 14:56:43 +0800204 mPermission, mUidRangeMap)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700205 ALOGE("failed to add interface %s to netId %u", interface.c_str(), mNetId);
206 return ret;
207 }
208 if (mIsDefault) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700209 if (int ret = addToDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700210 return ret;
211 }
212 }
213 mInterfaces.insert(interface);
214 return 0;
215}
216
217int PhysicalNetwork::removeInterface(const std::string& interface) {
218 if (!hasInterface(interface)) {
219 return 0;
220 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700221 if (mIsDefault) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700222 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700223 return ret;
224 }
225 }
Paul Jensen6d7e6232014-08-01 10:54:03 -0400226 // This step will flush the interface index from the cache in RouteController so it must be
227 // done last as further requests to the RouteController regarding this interface will fail
228 // to find the interface index in the cache in cases where the interface is already gone
229 // (e.g. bt-pan).
230 if (int ret = RouteController::removeInterfaceFromPhysicalNetwork(mNetId, interface.c_str(),
Ken Chen4ea88462021-05-23 14:56:43 +0800231 mPermission, mUidRangeMap)) {
Paul Jensen6d7e6232014-08-01 10:54:03 -0400232 ALOGE("failed to remove interface %s from netId %u", interface.c_str(), mNetId);
233 return ret;
234 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700235 mInterfaces.erase(interface);
236 return 0;
237}
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900238
Ken Chen53360bf2021-12-10 02:41:05 +0800239bool PhysicalNetwork::isValidSubPriority(int32_t priority) {
Patrick Rohre6f198c2022-01-25 13:50:31 +0100240 // SUB_PRIORITY_NO_DEFAULT is a special value, see UidRanges.h.
241 return (priority >= UidRanges::SUB_PRIORITY_HIGHEST &&
242 priority <= UidRanges::SUB_PRIORITY_LOWEST) ||
243 priority == UidRanges::SUB_PRIORITY_NO_DEFAULT;
Ken Chen4ea88462021-05-23 14:56:43 +0800244}
245
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900246} // namespace android::net