• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <linux/if_ether.h>
18 #include <linux/in.h>
19 #include <linux/ip.h>
20 
21 #ifdef MAINLINE
22 // BTF is incompatible with bpfloaders < v0.10, hence for S (v0.2) we must
23 // ship a different file than for later versions, but we need bpfloader v0.25+
24 // for obj@ver.o support
25 #define BPFLOADER_MIN_VER BPFLOADER_MAINLINE_T_VERSION
26 #else /* MAINLINE */
27 // The resulting .o needs to load on the Android S bpfloader
28 #define BPFLOADER_MIN_VER BPFLOADER_S_VERSION
29 #define BPFLOADER_MAX_VER BPFLOADER_T_VERSION
30 #endif /* MAINLINE */
31 
32 // Warning: values other than AID_ROOT don't work for map uid on BpfLoader < v0.21
33 #define TETHERING_UID AID_ROOT
34 
35 #define TETHERING_GID AID_NETWORK_STACK
36 
37 // This is non production code, only used for testing
38 // Needed because the bitmap array definition is non-kosher for pre-T OS devices.
39 #define THIS_BPF_PROGRAM_IS_FOR_TEST_PURPOSES_ONLY
40 
41 #include "bpf_helpers.h"
42 #include "bpf_net_helpers.h"
43 #include "offload.h"
44 
45 // Used only by TetheringPrivilegedTests, not by production code.
46 DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 16,
47                    TETHERING_GID)
48 DEFINE_BPF_MAP_GRW(tether2_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 16,
49                    TETHERING_GID)
50 DEFINE_BPF_MAP_GRW(tether3_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 16,
51                    TETHERING_GID)
52 // Used only by BpfBitmapTest, not by production code.
53 DEFINE_BPF_MAP_GRW(bitmap, ARRAY, int, uint64_t, 2, TETHERING_GID)
54 
55 DEFINE_BPF_PROG_KVER("xdp/drop_ipv4_udp_ether", TETHERING_UID, TETHERING_GID,
56                       xdp_test, KVER_5_9)
57 (struct xdp_md *ctx) {
58     void *data = (void *)(long)ctx->data;
59     void *data_end = (void *)(long)ctx->data_end;
60 
61     struct ethhdr *eth = data;
62     int hsize = sizeof(*eth);
63 
64     struct iphdr *ip = data + hsize;
65     hsize += sizeof(struct iphdr);
66 
67     if (data + hsize > data_end) return XDP_PASS;
68     if (eth->h_proto != htons(ETH_P_IP)) return XDP_PASS;
69     if (ip->protocol == IPPROTO_UDP) return XDP_DROP;
70     return XDP_PASS;
71 }
72 
73 LICENSE("Apache 2.0");
74 DISABLE_BTF_ON_USER_BUILDS();
75