Maciej Żenczykowski | e9810ff | 2021-01-14 20:02:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 Żenczykowski | 561fb4a | 2023-10-24 18:48:54 -0700 | [diff] [blame] | 17 | #ifdef MAINLINE |
Maciej Żenczykowski | 07d3013 | 2022-04-23 12:33:32 -0700 | [diff] [blame] | 18 | // BTF is incompatible with bpfloaders < v0.10, hence for S (v0.2) we must |
| 19 | // ship a different file than for later versions, but we need bpfloader v0.25+ |
| 20 | // for obj@ver.o support |
Maciej Żenczykowski | 4e4f872 | 2024-06-15 06:38:08 -0700 | [diff] [blame] | 21 | #define BPFLOADER_MIN_VER BPFLOADER_MAINLINE_T_VERSION |
Maciej Żenczykowski | 561fb4a | 2023-10-24 18:48:54 -0700 | [diff] [blame] | 22 | #else /* MAINLINE */ |
Maciej Żenczykowski | 4e4f872 | 2024-06-15 06:38:08 -0700 | [diff] [blame] | 23 | // The resulting .o needs to load on the Android S bpfloader |
Maciej Żenczykowski | f769952 | 2022-05-24 15:56:03 -0700 | [diff] [blame] | 24 | #define BPFLOADER_MIN_VER BPFLOADER_S_VERSION |
Maciej Żenczykowski | 4e4f872 | 2024-06-15 06:38:08 -0700 | [diff] [blame] | 25 | #define BPFLOADER_MAX_VER BPFLOADER_T_VERSION |
Maciej Żenczykowski | 561fb4a | 2023-10-24 18:48:54 -0700 | [diff] [blame] | 26 | #endif /* MAINLINE */ |
Maciej Żenczykowski | a457bf7 | 2021-10-22 21:41:25 -0700 | [diff] [blame] | 27 | |
Maciej Żenczykowski | ccce4a3 | 2022-07-17 01:28:38 -0700 | [diff] [blame] | 28 | // Warning: values other than AID_ROOT don't work for map uid on BpfLoader < v0.21 |
| 29 | #define TETHERING_UID AID_ROOT |
| 30 | |
Maciej Żenczykowski | ccce4a3 | 2022-07-17 01:28:38 -0700 | [diff] [blame] | 31 | #define TETHERING_GID AID_NETWORK_STACK |
Maciej Żenczykowski | ccce4a3 | 2022-07-17 01:28:38 -0700 | [diff] [blame] | 32 | |
Maciej Żenczykowski | c8e40c1 | 2022-10-21 00:01:39 +0000 | [diff] [blame] | 33 | // This is non production code, only used for testing |
| 34 | // Needed because the bitmap array definition is non-kosher for pre-T OS devices. |
| 35 | #define THIS_BPF_PROGRAM_IS_FOR_TEST_PURPOSES_ONLY |
| 36 | |
Maciej Żenczykowski | e9810ff | 2021-01-14 20:02:08 -0800 | [diff] [blame] | 37 | #include "bpf_net_helpers.h" |
Maciej Żenczykowski | 4e3321e | 2022-12-08 12:59:23 +0000 | [diff] [blame] | 38 | #include "offload.h" |
Maciej Żenczykowski | e9810ff | 2021-01-14 20:02:08 -0800 | [diff] [blame] | 39 | |
| 40 | // Used only by TetheringPrivilegedTests, not by production code. |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 41 | DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 16, |
Maciej Żenczykowski | ccce4a3 | 2022-07-17 01:28:38 -0700 | [diff] [blame] | 42 | TETHERING_GID) |
Maciej Żenczykowski | 45e9372 | 2024-06-15 11:47:26 -0700 | [diff] [blame] | 43 | DEFINE_BPF_MAP_GRW(tether2_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 16, |
| 44 | TETHERING_GID) |
Maciej Żenczykowski | 878aae0 | 2024-06-15 11:54:36 -0700 | [diff] [blame] | 45 | DEFINE_BPF_MAP_GRW(tether3_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 16, |
| 46 | TETHERING_GID) |
Tyler Wear | c23ffbd | 2021-12-01 16:25:00 -0800 | [diff] [blame] | 47 | // Used only by BpfBitmapTest, not by production code. |
Maciej Żenczykowski | ccce4a3 | 2022-07-17 01:28:38 -0700 | [diff] [blame] | 48 | DEFINE_BPF_MAP_GRW(bitmap, ARRAY, int, uint64_t, 2, TETHERING_GID) |
Maciej Żenczykowski | e9810ff | 2021-01-14 20:02:08 -0800 | [diff] [blame] | 49 | |
Maciej Żenczykowski | ccce4a3 | 2022-07-17 01:28:38 -0700 | [diff] [blame] | 50 | DEFINE_BPF_PROG_KVER("xdp/drop_ipv4_udp_ether", TETHERING_UID, TETHERING_GID, |
Maciej Żenczykowski | 901c710 | 2023-10-06 15:47:46 -0700 | [diff] [blame] | 51 | xdp_test, KVER_5_9) |
Maciej Żenczykowski | 8c7cd34 | 2021-01-18 00:02:19 -0800 | [diff] [blame] | 52 | (struct xdp_md *ctx) { |
| 53 | void *data = (void *)(long)ctx->data; |
| 54 | void *data_end = (void *)(long)ctx->data_end; |
| 55 | |
| 56 | struct ethhdr *eth = data; |
| 57 | int hsize = sizeof(*eth); |
| 58 | |
| 59 | struct iphdr *ip = data + hsize; |
| 60 | hsize += sizeof(struct iphdr); |
| 61 | |
| 62 | if (data + hsize > data_end) return XDP_PASS; |
| 63 | if (eth->h_proto != htons(ETH_P_IP)) return XDP_PASS; |
| 64 | if (ip->protocol == IPPROTO_UDP) return XDP_DROP; |
| 65 | return XDP_PASS; |
| 66 | } |
| 67 | |
Maciej Żenczykowski | e9810ff | 2021-01-14 20:02:08 -0800 | [diff] [blame] | 68 | LICENSE("Apache 2.0"); |
Maciej Żenczykowski | de1342a | 2023-06-09 05:45:57 +0000 | [diff] [blame] | 69 | DISABLE_BTF_ON_USER_BUILDS(); |