blob: aa8408987dcb337887581796d878906fc75034b0 [file] [log] [blame]
Ken Chen1647f602021-10-05 21:55:22 +08001/**
2 * Copyright (c) 2022, 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
Maciej Żenczykowskia81daca2024-05-20 14:33:17 +000017#define LOG_TAG "NetdUpdatable"
Ken Chen1647f602021-10-05 21:55:22 +080018
19#include "BpfHandler.h"
20
21#include <linux/bpf.h>
Nick Wille5076a022023-06-01 18:39:25 +000022#include <inttypes.h>
Ken Chen1647f602021-10-05 21:55:22 +080023
24#include <android-base/unique_fd.h>
Maciej Żenczykowski65075bb2023-06-01 23:09:14 +000025#include <android-modules-utils/sdk_level.h>
Ken Chen1647f602021-10-05 21:55:22 +080026#include <bpf/WaitForProgsLoaded.h>
27#include <log/log.h>
28#include <netdutils/UidConstants.h>
29#include <private/android_filesystem_config.h>
30
31#include "BpfSyscallWrappers.h"
32
33namespace android {
34namespace net {
35
36using base::unique_fd;
Ken Chen1647f602021-10-05 21:55:22 +080037using bpf::getSocketCookie;
38using bpf::retrieveProgram;
39using netdutils::Status;
40using netdutils::statusFromErrno;
41
42constexpr int PER_UID_STATS_ENTRIES_LIMIT = 500;
43// At most 90% of the stats map may be used by tagged traffic entries. This ensures
44// that 10% of the map is always available to count untagged traffic, one entry per UID.
45// Otherwise, apps would be able to avoid data usage accounting entirely by filling up the
46// map with tagged traffic entries.
47constexpr int TOTAL_UID_STATS_ENTRIES_LIMIT = STATS_MAP_SIZE * 0.9;
48
49static_assert(STATS_MAP_SIZE - TOTAL_UID_STATS_ENTRIES_LIMIT > 100,
50 "The limit for stats map is to high, stats data may be lost due to overflow");
51
52static Status attachProgramToCgroup(const char* programPath, const unique_fd& cgroupFd,
53 bpf_attach_type type) {
54 unique_fd cgroupProg(retrieveProgram(programPath));
Maciej Żenczykowski25824452023-06-14 10:08:31 +000055 if (!cgroupProg.ok()) {
Ken Chend6ea75a2023-11-28 23:29:47 +080056 return statusFromErrno(errno, fmt::format("Failed to get program from {}", programPath));
Ken Chen1647f602021-10-05 21:55:22 +080057 }
58 if (android::bpf::attachProgram(type, cgroupProg, cgroupFd)) {
Ken Chend6ea75a2023-11-28 23:29:47 +080059 return statusFromErrno(errno, fmt::format("Program {} attach failed", programPath));
Ken Chen1647f602021-10-05 21:55:22 +080060 }
61 return netdutils::status::ok;
62}
63
Maciej Żenczykowskic576c0d2022-08-07 22:18:15 +000064static Status checkProgramAccessible(const char* programPath) {
65 unique_fd prog(retrieveProgram(programPath));
Maciej Żenczykowski25824452023-06-14 10:08:31 +000066 if (!prog.ok()) {
Ken Chend6ea75a2023-11-28 23:29:47 +080067 return statusFromErrno(errno, fmt::format("Failed to get program from {}", programPath));
Maciej Żenczykowskic576c0d2022-08-07 22:18:15 +000068 }
69 return netdutils::status::ok;
70}
71
Ken Chen1647f602021-10-05 21:55:22 +080072static Status initPrograms(const char* cg2_path) {
Ken Chend6ea75a2023-11-28 23:29:47 +080073 if (!cg2_path) return Status("cg2_path is NULL");
Ken Chen5d146492023-11-27 17:05:43 +080074
Maciej Żenczykowskic2dd01c2023-09-01 21:02:36 +000075 // This code was mainlined in T, so this should be trivially satisfied.
Ken Chend6ea75a2023-11-28 23:29:47 +080076 if (!modules::sdklevel::IsAtLeastT()) return Status("S- platform is unsupported");
Maciej Żenczykowskic2dd01c2023-09-01 21:02:36 +000077
78 // S requires eBPF support which was only added in 4.9, so this should be satisfied.
Ken Chen5d146492023-11-27 17:05:43 +080079 if (!bpf::isAtLeastKernelVersion(4, 9, 0)) {
Ken Chend6ea75a2023-11-28 23:29:47 +080080 return Status("kernel version < 4.9.0 is unsupported");
Ken Chen5d146492023-11-27 17:05:43 +080081 }
Maciej Żenczykowskic2dd01c2023-09-01 21:02:36 +000082
83 // U bumps the kernel requirement up to 4.14
Ken Chen5d146492023-11-27 17:05:43 +080084 if (modules::sdklevel::IsAtLeastU() && !bpf::isAtLeastKernelVersion(4, 14, 0)) {
Ken Chend6ea75a2023-11-28 23:29:47 +080085 return Status("U+ platform with kernel version < 4.14.0 is unsupported");
Ken Chen5d146492023-11-27 17:05:43 +080086 }
Maciej Żenczykowskic2dd01c2023-09-01 21:02:36 +000087
Maciej Żenczykowskic2dd01c2023-09-01 21:02:36 +000088 // U mandates this mount point (though it should also be the case on T)
Ken Chen5d146492023-11-27 17:05:43 +080089 if (modules::sdklevel::IsAtLeastU() && !!strcmp(cg2_path, "/sys/fs/cgroup")) {
Ken Chend6ea75a2023-11-28 23:29:47 +080090 return Status("U+ platform with cg2_path != /sys/fs/cgroup is unsupported");
Ken Chen5d146492023-11-27 17:05:43 +080091 }
Maciej Żenczykowski65075bb2023-06-01 23:09:14 +000092
Ken Chen1647f602021-10-05 21:55:22 +080093 unique_fd cg_fd(open(cg2_path, O_DIRECTORY | O_RDONLY | O_CLOEXEC));
Maciej Żenczykowski25824452023-06-14 10:08:31 +000094 if (!cg_fd.ok()) {
95 const int err = errno;
96 ALOGE("Failed to open the cgroup directory: %s", strerror(err));
97 return statusFromErrno(err, "Open the cgroup directory failed");
Ken Chen1647f602021-10-05 21:55:22 +080098 }
Maciej Żenczykowskic576c0d2022-08-07 22:18:15 +000099 RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_ALLOWLIST_PROG_PATH));
100 RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_DENYLIST_PROG_PATH));
101 RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_EGRESS_PROG_PATH));
102 RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_INGRESS_PROG_PATH));
Ken Chen1647f602021-10-05 21:55:22 +0800103 RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_EGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_EGRESS));
104 RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_INGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_INGRESS));
Lorenzo Colitti3505b582022-10-27 19:36:27 +0900105
106 // For the devices that support cgroup socket filter, the socket filter
107 // should be loaded successfully by bpfloader. So we attach the filter to
108 // cgroup if the program is pinned properly.
109 // TODO: delete the if statement once all devices should support cgroup
110 // socket filter (ie. the minimum kernel version required is 4.14).
Maciej Żenczykowski03093622023-02-22 22:09:25 +0000111 if (bpf::isAtLeastKernelVersion(4, 14, 0)) {
Lorenzo Colitti3505b582022-10-27 19:36:27 +0900112 RETURN_IF_NOT_OK(
113 attachProgramToCgroup(CGROUP_SOCKET_PROG_PATH, cg_fd, BPF_CGROUP_INET_SOCK_CREATE));
114 }
Maciej Żenczykowski5b2611d2023-10-04 00:44:56 +0000115
Maciej Żenczykowski5b2611d2023-10-04 00:44:56 +0000116 if (bpf::isAtLeastKernelVersion(4, 19, 0)) {
Maciej Żenczykowski3cb494f2023-10-04 21:35:41 +0000117 RETURN_IF_NOT_OK(attachProgramToCgroup(
118 "/sys/fs/bpf/netd_readonly/prog_block_bind4_block_port",
119 cg_fd, BPF_CGROUP_INET4_BIND));
120 RETURN_IF_NOT_OK(attachProgramToCgroup(
121 "/sys/fs/bpf/netd_readonly/prog_block_bind6_block_port",
122 cg_fd, BPF_CGROUP_INET6_BIND));
123
124 // This should trivially pass, since we just attached up above,
125 // but BPF_PROG_QUERY is only implemented on 4.19+ kernels.
Maciej Żenczykowski5b2611d2023-10-04 00:44:56 +0000126 if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET_EGRESS) <= 0) abort();
127 if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET_INGRESS) <= 0) abort();
128 if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET_SOCK_CREATE) <= 0) abort();
Maciej Żenczykowski3cb494f2023-10-04 21:35:41 +0000129 if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET4_BIND) <= 0) abort();
130 if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET6_BIND) <= 0) abort();
Maciej Żenczykowski5b2611d2023-10-04 00:44:56 +0000131 }
132
Ken Chen1647f602021-10-05 21:55:22 +0800133 return netdutils::status::ok;
134}
135
136BpfHandler::BpfHandler()
137 : mPerUidStatsEntriesLimit(PER_UID_STATS_ENTRIES_LIMIT),
138 mTotalUidStatsEntriesLimit(TOTAL_UID_STATS_ENTRIES_LIMIT) {}
139
140BpfHandler::BpfHandler(uint32_t perUidLimit, uint32_t totalLimit)
141 : mPerUidStatsEntriesLimit(perUidLimit), mTotalUidStatsEntriesLimit(totalLimit) {}
142
Maciej Żenczykowski732a1412024-03-14 00:17:18 -0700143// copied with minor changes from waitForProgsLoaded()
144// p/m/C's staticlibs/native/bpf_headers/include/bpf/WaitForProgsLoaded.h
145static inline void waitForNetProgsLoaded() {
146 // infinite loop until success with 5/10/20/40/60/60/60... delay
147 for (int delay = 5;; delay *= 2) {
148 if (delay > 60) delay = 60;
149 if (base::WaitForProperty("init.svc.bpfloader", "stopped", std::chrono::seconds(delay))
150 && !access("/sys/fs/bpf/netd_shared", F_OK))
151 return;
152 ALOGW("Waited %ds for init.svc.bpfloader=stopped, still waiting...", delay);
153 }
154}
155
Ken Chen1647f602021-10-05 21:55:22 +0800156Status BpfHandler::init(const char* cg2_path) {
Maciej Żenczykowski23d2c1e2024-03-28 22:54:01 -0700157 if (base::GetProperty("bpf.progs_loaded", "") != "1") {
158 // Make sure BPF programs are loaded before doing anything
159 ALOGI("Waiting for BPF programs");
Maciej Żenczykowski732a1412024-03-14 00:17:18 -0700160
Maciej Żenczykowski3218a812024-04-15 00:39:59 -0700161 // TODO: use !modules::sdklevel::IsAtLeastV() once api finalized
162 if (android_get_device_api_level() < __ANDROID_API_V__) {
Maciej Żenczykowski23d2c1e2024-03-28 22:54:01 -0700163 waitForNetProgsLoaded();
164 ALOGI("Networking BPF programs are loaded");
Maciej Żenczykowski732a1412024-03-14 00:17:18 -0700165
Maciej Żenczykowski23d2c1e2024-03-28 22:54:01 -0700166 if (!base::SetProperty("ctl.start", "mdnsd_loadbpf")) {
167 ALOGE("Failed to set property ctl.start=mdnsd_loadbpf, see dmesg for reason.");
168 abort();
169 }
170
171 ALOGI("Waiting for remaining BPF programs");
Maciej Żenczykowski732a1412024-03-14 00:17:18 -0700172 }
173
Maciej Żenczykowski23d2c1e2024-03-28 22:54:01 -0700174 android::bpf::waitForProgsLoaded();
Maciej Żenczykowski732a1412024-03-14 00:17:18 -0700175 }
Ken Chen1647f602021-10-05 21:55:22 +0800176 ALOGI("BPF programs are loaded");
177
178 RETURN_IF_NOT_OK(initPrograms(cg2_path));
179 RETURN_IF_NOT_OK(initMaps());
180
181 return netdutils::status::ok;
182}
183
Maciej Żenczykowski52018c82024-06-04 16:05:16 +0000184static void mapLockTest(void) {
185 // The maps must be R/W, and as yet unopened (or more specifically not yet lock'ed).
186 const char * const m1 = BPF_NETD_PATH "map_netd_lock_array_test_map";
187 const char * const m2 = BPF_NETD_PATH "map_netd_lock_hash_test_map";
188
189 unique_fd fd0(bpf::mapRetrieveExclusiveRW(m1)); if (!fd0.ok()) abort();
190
191 unique_fd fd1(bpf::mapRetrieveExclusiveRW(m2)); if (!fd1.ok()) abort(); // no conflict with fd0
192 unique_fd fd2(bpf::mapRetrieveExclusiveRW(m2)); if ( fd2.ok()) abort(); // busy due to fd1
193 unique_fd fd3(bpf::mapRetrieveRO(m2)); if (!fd3.ok()) abort(); // no lock taken
194 unique_fd fd4(bpf::mapRetrieveRW(m2)); if ( fd4.ok()) abort(); // busy due to fd1
195 fd1.reset(); // releases exclusive lock
196 unique_fd fd5(bpf::mapRetrieveRO(m2)); if (!fd5.ok()) abort(); // no lock taken
197 unique_fd fd6(bpf::mapRetrieveRW(m2)); if (!fd6.ok()) abort(); // now ok
198 unique_fd fd7(bpf::mapRetrieveRO(m2)); if (!fd7.ok()) abort(); // no lock taken
199 unique_fd fd8(bpf::mapRetrieveExclusiveRW(m2)); if ( fd8.ok()) abort(); // busy due to fd6
200}
201
Ken Chen1647f602021-10-05 21:55:22 +0800202Status BpfHandler::initMaps() {
Maciej Żenczykowski52018c82024-06-04 16:05:16 +0000203 mapLockTest();
204
Ken Chen1647f602021-10-05 21:55:22 +0800205 RETURN_IF_NOT_OK(mStatsMapA.init(STATS_MAP_A_PATH));
206 RETURN_IF_NOT_OK(mStatsMapB.init(STATS_MAP_B_PATH));
207 RETURN_IF_NOT_OK(mConfigurationMap.init(CONFIGURATION_MAP_PATH));
Ken Chen1647f602021-10-05 21:55:22 +0800208 RETURN_IF_NOT_OK(mUidPermissionMap.init(UID_PERMISSION_MAP_PATH));
Maciej Żenczykowskib5868a02022-08-31 04:21:01 +0000209 // initialized last so mCookieTagMap.isValid() implies everything else is valid too
210 RETURN_IF_NOT_OK(mCookieTagMap.init(COOKIE_TAG_MAP_PATH));
Ken Chen322ffcb2022-05-23 22:27:40 +0800211 ALOGI("%s successfully", __func__);
Ken Chen1647f602021-10-05 21:55:22 +0800212
213 return netdutils::status::ok;
214}
215
216bool BpfHandler::hasUpdateDeviceStatsPermission(uid_t uid) {
217 // This implementation is the same logic as method ActivityManager#checkComponentPermission.
218 // It implies that the real uid can never be the same as PER_USER_RANGE.
219 uint32_t appId = uid % PER_USER_RANGE;
220 auto permission = mUidPermissionMap.readValue(appId);
221 if (permission.ok() && (permission.value() & BPF_PERMISSION_UPDATE_DEVICE_STATS)) {
222 return true;
223 }
224 return ((appId == AID_ROOT) || (appId == AID_SYSTEM) || (appId == AID_DNS));
225}
226
227int BpfHandler::tagSocket(int sockFd, uint32_t tag, uid_t chargeUid, uid_t realUid) {
Maciej Żenczykowski4938d402022-08-14 14:36:20 +0000228 if (!mCookieTagMap.isValid()) return -EPERM;
229
230 if (chargeUid != realUid && !hasUpdateDeviceStatsPermission(realUid)) return -EPERM;
Ken Chen1647f602021-10-05 21:55:22 +0800231
Hungming Chen436547e2022-02-18 17:52:11 +0800232 // Note that tagging the socket to AID_CLAT is only implemented in JNI ClatCoordinator.
233 // The process is not allowed to tag socket to AID_CLAT via tagSocket() which would cause
234 // process data usage accounting to be bypassed. Tagging AID_CLAT is used for avoiding counting
235 // CLAT traffic data usage twice. See packages/modules/Connectivity/service/jni/
236 // com_android_server_connectivity_ClatCoordinator.cpp
Maciej Żenczykowski4938d402022-08-14 14:36:20 +0000237 if (chargeUid == AID_CLAT) return -EPERM;
Hungming Chen436547e2022-02-18 17:52:11 +0800238
Hungming Chen478c0eb2022-03-04 21:16:59 +0800239 // The socket destroy listener only monitors on the group {INET_TCP, INET_UDP, INET6_TCP,
240 // INET6_UDP}. Tagging listener unsupported socket causes that the tag can't be removed from
241 // tag map automatically. Eventually, the tag map may run out of space because of dead tag
Hungming Chenbcc0f5b2022-03-07 14:13:49 +0800242 // entries. Note that although tagSocket() of net client has already denied the family which
243 // is neither AF_INET nor AF_INET6, the family validation is still added here just in case.
244 // See tagSocket in system/netd/client/NetdClient.cpp and
245 // TrafficController::makeSkDestroyListener in
Hungming Chen478c0eb2022-03-04 21:16:59 +0800246 // packages/modules/Connectivity/service/native/TrafficController.cpp
247 // TODO: remove this once the socket destroy listener can detect more types of socket destroy.
Hungming Chenbcc0f5b2022-03-07 14:13:49 +0800248 int socketFamily;
249 socklen_t familyLen = sizeof(socketFamily);
250 if (getsockopt(sockFd, SOL_SOCKET, SO_DOMAIN, &socketFamily, &familyLen)) {
251 ALOGE("Failed to getsockopt SO_DOMAIN: %s, fd: %d", strerror(errno), sockFd);
Hungming Chen478c0eb2022-03-04 21:16:59 +0800252 return -errno;
Hungming Chenbcc0f5b2022-03-07 14:13:49 +0800253 }
254 if (socketFamily != AF_INET && socketFamily != AF_INET6) {
255 ALOGE("Unsupported family: %d", socketFamily);
256 return -EAFNOSUPPORT;
257 }
258
259 int socketProto;
260 socklen_t protoLen = sizeof(socketProto);
261 if (getsockopt(sockFd, SOL_SOCKET, SO_PROTOCOL, &socketProto, &protoLen)) {
262 ALOGE("Failed to getsockopt SO_PROTOCOL: %s, fd: %d", strerror(errno), sockFd);
263 return -errno;
264 }
265 if (socketProto != IPPROTO_UDP && socketProto != IPPROTO_TCP) {
266 ALOGE("Unsupported protocol: %d", socketProto);
267 return -EPROTONOSUPPORT;
Hungming Chen478c0eb2022-03-04 21:16:59 +0800268 }
269
Ken Chen1647f602021-10-05 21:55:22 +0800270 uint64_t sock_cookie = getSocketCookie(sockFd);
Maciej Żenczykowskid3fe1542023-02-23 03:56:45 +0000271 if (!sock_cookie) return -errno;
Maciej Żenczykowski4938d402022-08-14 14:36:20 +0000272
Ken Chen1647f602021-10-05 21:55:22 +0800273 UidTagValue newKey = {.uid = (uint32_t)chargeUid, .tag = tag};
274
275 uint32_t totalEntryCount = 0;
276 uint32_t perUidEntryCount = 0;
277 // Now we go through the stats map and count how many entries are associated
278 // with chargeUid. If the uid entry hit the limit for each chargeUid, we block
Maciej Żenczykowskib5868a02022-08-31 04:21:01 +0000279 // the request to prevent the map from overflow. Note though that it isn't really
280 // safe here to iterate over the map since it might be modified by the system server,
281 // which might toggle the live stats map and clean it.
Ken Chen1647f602021-10-05 21:55:22 +0800282 const auto countUidStatsEntries = [chargeUid, &totalEntryCount, &perUidEntryCount](
283 const StatsKey& key,
Maciej Żenczykowski7e2f53e2023-09-28 01:08:28 +0000284 const BpfMapRO<StatsKey, StatsValue>&) {
Ken Chen1647f602021-10-05 21:55:22 +0800285 if (key.uid == chargeUid) {
286 perUidEntryCount++;
287 }
288 totalEntryCount++;
289 return base::Result<void>();
290 };
291 auto configuration = mConfigurationMap.readValue(CURRENT_STATS_MAP_CONFIGURATION_KEY);
292 if (!configuration.ok()) {
Maciej Żenczykowski85d4a5e2023-08-20 16:41:50 +0000293 ALOGE("Failed to get current configuration: %s",
294 strerror(configuration.error().code()));
Ken Chen1647f602021-10-05 21:55:22 +0800295 return -configuration.error().code();
296 }
297 if (configuration.value() != SELECT_MAP_A && configuration.value() != SELECT_MAP_B) {
298 ALOGE("unknown configuration value: %d", configuration.value());
299 return -EINVAL;
300 }
301
Maciej Żenczykowski7e2f53e2023-09-28 01:08:28 +0000302 BpfMapRO<StatsKey, StatsValue>& currentMap =
Ken Chen1647f602021-10-05 21:55:22 +0800303 (configuration.value() == SELECT_MAP_A) ? mStatsMapA : mStatsMapB;
304 base::Result<void> res = currentMap.iterate(countUidStatsEntries);
305 if (!res.ok()) {
Maciej Żenczykowski85d4a5e2023-08-20 16:41:50 +0000306 ALOGE("Failed to count the stats entry in map: %s",
Ken Chen1647f602021-10-05 21:55:22 +0800307 strerror(res.error().code()));
308 return -res.error().code();
309 }
310
311 if (totalEntryCount > mTotalUidStatsEntriesLimit ||
312 perUidEntryCount > mPerUidStatsEntriesLimit) {
313 ALOGE("Too many stats entries in the map, total count: %u, chargeUid(%u) count: %u,"
314 " blocking tag request to prevent map overflow",
315 totalEntryCount, chargeUid, perUidEntryCount);
316 return -EMFILE;
317 }
318 // Update the tag information of a socket to the cookieUidMap. Use BPF_ANY
319 // flag so it will insert a new entry to the map if that value doesn't exist
Maciej Żenczykowskib5868a02022-08-31 04:21:01 +0000320 // yet and update the tag if there is already a tag stored. Since the eBPF
Ken Chen1647f602021-10-05 21:55:22 +0800321 // program in kernel only read this map, and is protected by rcu read lock. It
Maciej Żenczykowskib5868a02022-08-31 04:21:01 +0000322 // should be fine to concurrently update the map while eBPF program is running.
Ken Chen1647f602021-10-05 21:55:22 +0800323 res = mCookieTagMap.writeValue(sock_cookie, newKey, BPF_ANY);
324 if (!res.ok()) {
Maciej Żenczykowski85d4a5e2023-08-20 16:41:50 +0000325 ALOGE("Failed to tag the socket: %s", strerror(res.error().code()));
Ken Chen1647f602021-10-05 21:55:22 +0800326 return -res.error().code();
327 }
Nick Wille5076a022023-06-01 18:39:25 +0000328 ALOGD("Socket with cookie %" PRIu64 " tagged successfully with tag %" PRIu32 " uid %u "
329 "and real uid %u", sock_cookie, tag, chargeUid, realUid);
Ken Chen1647f602021-10-05 21:55:22 +0800330 return 0;
331}
332
333int BpfHandler::untagSocket(int sockFd) {
Maciej Żenczykowski4938d402022-08-14 14:36:20 +0000334 uint64_t sock_cookie = getSocketCookie(sockFd);
Maciej Żenczykowskid3fe1542023-02-23 03:56:45 +0000335 if (!sock_cookie) return -errno;
Maciej Żenczykowski4938d402022-08-14 14:36:20 +0000336
337 if (!mCookieTagMap.isValid()) return -EPERM;
Ken Chen1647f602021-10-05 21:55:22 +0800338 base::Result<void> res = mCookieTagMap.deleteValue(sock_cookie);
339 if (!res.ok()) {
Maciej Żenczykowskie0f58462022-05-17 13:59:22 -0700340 ALOGE("Failed to untag socket: %s", strerror(res.error().code()));
Ken Chen1647f602021-10-05 21:55:22 +0800341 return -res.error().code();
342 }
Nick Wille5076a022023-06-01 18:39:25 +0000343 ALOGD("Socket with cookie %" PRIu64 " untagged successfully.", sock_cookie);
Ken Chen1647f602021-10-05 21:55:22 +0800344 return 0;
345}
346
347} // namespace net
348} // namespace android