| 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 | 8c7cd34 | 2021-01-18 00:02:19 -0800 | [diff] [blame] | 17 | #include <linux/if_ether.h> | 
 | 18 | #include <linux/in.h> | 
 | 19 | #include <linux/ip.h> | 
 | 20 |  | 
| Maciej Żenczykowski | f769952 | 2022-05-24 15:56:03 -0700 | [diff] [blame] | 21 | // The resulting .o needs to load on the Android S bpfloader | 
 | 22 | #define BPFLOADER_MIN_VER BPFLOADER_S_VERSION | 
| Maciej Żenczykowski | a457bf7 | 2021-10-22 21:41:25 -0700 | [diff] [blame] | 23 |  | 
| Maciej Żenczykowski | e9810ff | 2021-01-14 20:02:08 -0800 | [diff] [blame] | 24 | #include "bpf_helpers.h" | 
 | 25 | #include "bpf_net_helpers.h" | 
| Lorenzo Colitti | 56be03e | 2021-02-24 00:10:44 +0900 | [diff] [blame] | 26 | #include "bpf_tethering.h" | 
| Maciej Żenczykowski | e9810ff | 2021-01-14 20:02:08 -0800 | [diff] [blame] | 27 |  | 
 | 28 | // Used only by TetheringPrivilegedTests, not by production code. | 
| Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 29 | DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 16, | 
| Maciej Żenczykowski | e9810ff | 2021-01-14 20:02:08 -0800 | [diff] [blame] | 30 |                    AID_NETWORK_STACK) | 
| Tyler Wear | c23ffbd | 2021-12-01 16:25:00 -0800 | [diff] [blame] | 31 | // Used only by BpfBitmapTest, not by production code. | 
 | 32 | DEFINE_BPF_MAP_GRW(bitmap, ARRAY, int, uint64_t, 2, | 
 | 33 |                    AID_NETWORK_STACK) | 
| Maciej Żenczykowski | e9810ff | 2021-01-14 20:02:08 -0800 | [diff] [blame] | 34 |  | 
| Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 35 | DEFINE_BPF_PROG_KVER("xdp/drop_ipv4_udp_ether", AID_ROOT, AID_NETWORK_STACK, | 
| Maciej Żenczykowski | 8c7cd34 | 2021-01-18 00:02:19 -0800 | [diff] [blame] | 36 |                       xdp_test, KVER(5, 9, 0)) | 
 | 37 | (struct xdp_md *ctx) { | 
 | 38 |     void *data = (void *)(long)ctx->data; | 
 | 39 |     void *data_end = (void *)(long)ctx->data_end; | 
 | 40 |  | 
 | 41 |     struct ethhdr *eth = data; | 
 | 42 |     int hsize = sizeof(*eth); | 
 | 43 |  | 
 | 44 |     struct iphdr *ip = data + hsize; | 
 | 45 |     hsize += sizeof(struct iphdr); | 
 | 46 |  | 
 | 47 |     if (data + hsize > data_end) return XDP_PASS; | 
 | 48 |     if (eth->h_proto != htons(ETH_P_IP)) return XDP_PASS; | 
 | 49 |     if (ip->protocol == IPPROTO_UDP) return XDP_DROP; | 
 | 50 |     return XDP_PASS; | 
 | 51 | } | 
 | 52 |  | 
| Maciej Żenczykowski | e9810ff | 2021-01-14 20:02:08 -0800 | [diff] [blame] | 53 | LICENSE("Apache 2.0"); |