blob: 0e2dba936b95e57c60e19ea4603ffb9c46e02105 [file] [log] [blame]
Tyler Wearb37f5512021-10-01 13:22:00 -07001/*
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 Żenczykowski11141da2024-03-15 18:21:33 -070017// The resulting .o needs to load on Android T+
Maciej Żenczykowski4e4f8722024-06-15 06:38:08 -070018#define BPFLOADER_MIN_VER BPFLOADER_MAINLINE_T_VERSION
Maciej Żenczykowskiacebffb2022-05-16 16:05:15 -070019
Maciej Żenczykowski1745e542024-08-16 15:52:55 -070020#include "bpf_net_helpers.h"
Tyler Wearb37f5512021-10-01 13:22:00 -070021
22DEFINE_BPF_MAP_GRW(blocked_ports_map, ARRAY, int, uint64_t,
23 1024 /* 64K ports -> 1024 u64s */, AID_SYSTEM)
24
25static inline __always_inline int block_port(struct bpf_sock_addr *ctx) {
Maciej Żenczykowski95ab8c72024-08-16 15:10:02 -070026 if (!ctx->user_port) return BPF_ALLOW;
Tyler Wearb37f5512021-10-01 13:22:00 -070027
28 switch (ctx->protocol) {
29 case IPPROTO_TCP:
30 case IPPROTO_MPTCP:
31 case IPPROTO_UDP:
32 case IPPROTO_UDPLITE:
33 case IPPROTO_DCCP:
34 case IPPROTO_SCTP:
35 break;
36 default:
Maciej Żenczykowski95ab8c72024-08-16 15:10:02 -070037 return BPF_ALLOW; // unknown protocols are allowed
Tyler Wearb37f5512021-10-01 13:22:00 -070038 }
39
40 int key = ctx->user_port >> 6;
41 int shift = ctx->user_port & 63;
42
43 uint64_t *val = bpf_blocked_ports_map_lookup_elem(&key);
44 // Lookup should never fail in reality, but if it does return here to keep the
45 // BPF verifier happy.
Maciej Żenczykowski95ab8c72024-08-16 15:10:02 -070046 if (!val) return BPF_ALLOW;
Tyler Wearb37f5512021-10-01 13:22:00 -070047
Maciej Żenczykowski95ab8c72024-08-16 15:10:02 -070048 if ((*val >> shift) & 1) return BPF_DISALLOW;
49 return BPF_ALLOW;
Tyler Wearb37f5512021-10-01 13:22:00 -070050}
51
Maciej Żenczykowski3cb494f2023-10-04 21:35:41 +000052// the program need to be accessible/loadable by netd (from netd updatable plugin)
53#define DEFINE_NETD_RO_BPF_PROG(SECTION_NAME, the_prog, min_kver) \
54 DEFINE_BPF_PROG_EXT(SECTION_NAME, AID_ROOT, AID_ROOT, the_prog, min_kver, KVER_INF, \
55 BPFLOADER_MIN_VER, BPFLOADER_MAX_VER, MANDATORY, \
56 "", "netd_readonly/", LOAD_ON_ENG, LOAD_ON_USER, LOAD_ON_USERDEBUG)
57
Maciej Żenczykowski901c7102023-10-06 15:47:46 -070058DEFINE_NETD_RO_BPF_PROG("bind4/block_port", bind4_block_port, KVER_4_19)
Tyler Wearb37f5512021-10-01 13:22:00 -070059(struct bpf_sock_addr *ctx) {
60 return block_port(ctx);
61}
62
Maciej Żenczykowski901c7102023-10-06 15:47:46 -070063DEFINE_NETD_RO_BPF_PROG("bind6/block_port", bind6_block_port, KVER_4_19)
Tyler Wearb37f5512021-10-01 13:22:00 -070064(struct bpf_sock_addr *ctx) {
65 return block_port(ctx);
66}
67
68LICENSE("Apache 2.0");
69CRITICAL("ConnectivityNative");