blob: c11c35843b9b5e9f352a5b0636accf755840f24e [file] [log] [blame]
Maciej Żenczykowskie9810ff2021-01-14 20:02:08 -08001/*
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 Żenczykowski8c7cd342021-01-18 00:02:19 -080017#include <linux/if_ether.h>
18#include <linux/in.h>
19#include <linux/ip.h>
20
Maciej Żenczykowski07d30132022-04-23 12:33:32 -070021#ifdef BTF
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_OBJ_AT_VER_VERSION
26#else /* BTF */
Maciej Żenczykowskif7699522022-05-24 15:56:03 -070027// The resulting .o needs to load on the Android S bpfloader
28#define BPFLOADER_MIN_VER BPFLOADER_S_VERSION
Maciej Żenczykowski07d30132022-04-23 12:33:32 -070029#define BPFLOADER_MAX_VER BPFLOADER_OBJ_AT_VER_VERSION
30#endif /* BTF */
Maciej Żenczykowskia457bf72021-10-22 21:41:25 -070031
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -070032// Warning: values other than AID_ROOT don't work for map uid on BpfLoader < v0.21
33#define TETHERING_UID AID_ROOT
34
35#ifdef INPROCESS
36#define DEFAULT_BPF_MAP_SELINUX_CONTEXT "fs_bpf_net_shared"
37#define DEFAULT_BPF_PROG_SELINUX_CONTEXT "fs_bpf_net_shared"
38#define TETHERING_GID AID_SYSTEM
39#else
40#define TETHERING_GID AID_NETWORK_STACK
41#endif
42
Maciej Żenczykowskic8e40c12022-10-21 00:01:39 +000043// This is non production code, only used for testing
44// Needed because the bitmap array definition is non-kosher for pre-T OS devices.
45#define THIS_BPF_PROGRAM_IS_FOR_TEST_PURPOSES_ONLY
46
Maciej Żenczykowskie9810ff2021-01-14 20:02:08 -080047#include "bpf_helpers.h"
48#include "bpf_net_helpers.h"
Lorenzo Colitti56be03e2021-02-24 00:10:44 +090049#include "bpf_tethering.h"
Maciej Żenczykowskie9810ff2021-01-14 20:02:08 -080050
51// Used only by TetheringPrivilegedTests, not by production code.
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -080052DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 16,
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -070053 TETHERING_GID)
Tyler Wearc23ffbd2021-12-01 16:25:00 -080054// Used only by BpfBitmapTest, not by production code.
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -070055DEFINE_BPF_MAP_GRW(bitmap, ARRAY, int, uint64_t, 2, TETHERING_GID)
Maciej Żenczykowskie9810ff2021-01-14 20:02:08 -080056
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -070057DEFINE_BPF_PROG_KVER("xdp/drop_ipv4_udp_ether", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski8c7cd342021-01-18 00:02:19 -080058 xdp_test, KVER(5, 9, 0))
59(struct xdp_md *ctx) {
60 void *data = (void *)(long)ctx->data;
61 void *data_end = (void *)(long)ctx->data_end;
62
63 struct ethhdr *eth = data;
64 int hsize = sizeof(*eth);
65
66 struct iphdr *ip = data + hsize;
67 hsize += sizeof(struct iphdr);
68
69 if (data + hsize > data_end) return XDP_PASS;
70 if (eth->h_proto != htons(ETH_P_IP)) return XDP_PASS;
71 if (ip->protocol == IPPROTO_UDP) return XDP_DROP;
72 return XDP_PASS;
73}
74
Maciej Żenczykowskie9810ff2021-01-14 20:02:08 -080075LICENSE("Apache 2.0");