blob: efda228479426d932331c71b32f2f2288538d44c [file] [log] [blame]
Lorenzo Colitti734b14e2021-02-05 23:56:09 +09001/*
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#pragma once
18
Lorenzo Colitti56be03e2021-02-24 00:10:44 +090019#include <linux/if.h>
20#include <linux/if_ether.h>
21#include <linux/in.h>
22#include <linux/in6.h>
23
Lorenzo Colitti734b14e2021-02-05 23:56:09 +090024// Common definitions for BPF code in the tethering mainline module.
25// These definitions are available to:
26// - The BPF programs in Tethering/bpf_progs/
27// - JNI code that depends on the bpf_tethering_headers library.
Lorenzo Colittib81584d2021-02-06 00:00:58 +090028
29#define BPF_TETHER_ERRORS \
30 ERR(INVALID_IP_VERSION) \
31 ERR(LOW_TTL) \
32 ERR(INVALID_TCP_HEADER) \
33 ERR(TCP_CONTROL_PACKET) \
34 ERR(NON_GLOBAL_SRC) \
35 ERR(NON_GLOBAL_DST) \
36 ERR(LOCAL_SRC_DST) \
37 ERR(NO_STATS_ENTRY) \
38 ERR(NO_LIMIT_ENTRY) \
Lorenzo Colittid561b7f2021-02-09 13:20:29 +090039 ERR(BELOW_IPV4_MTU) \
Lorenzo Colittib81584d2021-02-06 00:00:58 +090040 ERR(BELOW_IPV6_MTU) \
41 ERR(LIMIT_REACHED) \
42 ERR(CHANGE_HEAD_FAILED) \
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +090043 ERR(TOO_SHORT) \
Lorenzo Colittid561b7f2021-02-09 13:20:29 +090044 ERR(HAS_IP_OPTIONS) \
45 ERR(IS_IP_FRAG) \
46 ERR(CHECKSUM) \
47 ERR(NON_TCP_UDP) \
Maciej Żenczykowski36867352021-02-15 01:53:17 -080048 ERR(NON_TCP) \
Maciej Żenczykowski82ee26b2021-02-16 16:10:08 -080049 ERR(SHORT_L4_HEADER) \
Lorenzo Colittid561b7f2021-02-09 13:20:29 +090050 ERR(SHORT_TCP_HEADER) \
51 ERR(SHORT_UDP_HEADER) \
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -080052 ERR(UDP_CSUM_ZERO) \
Lorenzo Colittid561b7f2021-02-09 13:20:29 +090053 ERR(TRUNCATED_IPV4) \
Lorenzo Colittib81584d2021-02-06 00:00:58 +090054 ERR(_MAX)
55
56#define ERR(x) BPF_TETHER_ERR_ ##x,
57enum {
58 BPF_TETHER_ERRORS
59};
60#undef ERR
61
62#define ERR(x) #x,
63static const char *bpf_tether_errors[] = {
64 BPF_TETHER_ERRORS
65};
66#undef ERR
Lorenzo Colitti56be03e2021-02-24 00:10:44 +090067
68// This header file is shared by eBPF kernel programs (C) and netd (C++) and
69// some of the maps are also accessed directly from Java mainline module code.
70//
71// Hence: explicitly pad all relevant structures and assert that their size
72// is the sum of the sizes of their fields.
73#define STRUCT_SIZE(name, size) _Static_assert(sizeof(name) == (size), "Incorrect struct size.")
74
75
76#define BPF_PATH_TETHER BPF_PATH "tethering/"
77
78#define TETHER_STATS_MAP_PATH BPF_PATH_TETHER "map_offload_tether_stats_map"
79
80typedef uint32_t TetherStatsKey; // upstream ifindex
81
82typedef struct {
83 uint64_t rxPackets;
84 uint64_t rxBytes;
85 uint64_t rxErrors;
86 uint64_t txPackets;
87 uint64_t txBytes;
88 uint64_t txErrors;
89} TetherStatsValue;
90STRUCT_SIZE(TetherStatsValue, 6 * 8); // 48
91
92#define TETHER_LIMIT_MAP_PATH BPF_PATH_TETHER "map_offload_tether_limit_map"
93
94typedef uint32_t TetherLimitKey; // upstream ifindex
95typedef uint64_t TetherLimitValue; // in bytes
96
97#define TETHER_DOWNSTREAM6_TC_PROG_RAWIP_NAME "prog_offload_schedcls_tether_downstream6_rawip"
98#define TETHER_DOWNSTREAM6_TC_PROG_ETHER_NAME "prog_offload_schedcls_tether_downstream6_ether"
99
100#define TETHER_DOWNSTREAM6_TC_PROG_RAWIP_PATH BPF_PATH_TETHER TETHER_DOWNSTREAM6_TC_PROG_RAWIP_NAME
101#define TETHER_DOWNSTREAM6_TC_PROG_ETHER_PATH BPF_PATH_TETHER TETHER_DOWNSTREAM6_TC_PROG_ETHER_NAME
102
103#define TETHER_DOWNSTREAM6_MAP_PATH BPF_PATH_TETHER "map_offload_tether_downstream6_map"
104
105// For now tethering offload only needs to support downstreams that use 6-byte MAC addresses,
106// because all downstream types that are currently supported (WiFi, USB, Bluetooth and
107// Ethernet) have 6-byte MAC addresses.
108
109typedef struct {
110 uint32_t iif; // The input interface index
111 // TODO: extend this to include dstMac
112 struct in6_addr neigh6; // The destination IPv6 address
113} TetherDownstream6Key;
114STRUCT_SIZE(TetherDownstream6Key, 4 + 16); // 20
115
116typedef struct {
117 uint32_t oif; // The output interface to redirect to
118 struct ethhdr macHeader; // includes dst/src mac and ethertype (zeroed iff rawip egress)
119 uint16_t pmtu; // The maximum L3 output path/route mtu
120} Tether6Value;
121STRUCT_SIZE(Tether6Value, 4 + 14 + 2); // 20
122
123#define TETHER_DOWNSTREAM64_MAP_PATH BPF_PATH_TETHER "map_offload_tether_downstream64_map"
124
125typedef struct {
126 uint32_t iif; // The input interface index
127 uint8_t dstMac[ETH_ALEN]; // destination ethernet mac address (zeroed iff rawip ingress)
128 uint16_t l4Proto; // IPPROTO_TCP/UDP/...
129 struct in6_addr src6; // source &
130 struct in6_addr dst6; // destination IPv6 addresses
131 __be16 srcPort; // source &
132 __be16 dstPort; // destination tcp/udp/... ports
133} TetherDownstream64Key;
134STRUCT_SIZE(TetherDownstream64Key, 4 + 6 + 2 + 16 + 16 + 2 + 2); // 48
135
136typedef struct {
137 uint32_t oif; // The output interface to redirect to
138 struct ethhdr macHeader; // includes dst/src mac and ethertype (zeroed iff rawip egress)
139 uint16_t pmtu; // The maximum L3 output path/route mtu
140 struct in_addr src4; // source &
141 struct in_addr dst4; // destination IPv4 addresses
142 __be16 srcPort; // source &
143 __be16 outPort; // destination tcp/udp/... ports
144 uint64_t lastUsed; // Kernel updates on each use with bpf_ktime_get_boot_ns()
145} TetherDownstream64Value;
146STRUCT_SIZE(TetherDownstream64Value, 4 + 14 + 2 + 4 + 4 + 2 + 2 + 8); // 40
147
148#define TETHER_UPSTREAM6_TC_PROG_RAWIP_NAME "prog_offload_schedcls_tether_upstream6_rawip"
149#define TETHER_UPSTREAM6_TC_PROG_ETHER_NAME "prog_offload_schedcls_tether_upstream6_ether"
150
151#define TETHER_UPSTREAM6_TC_PROG_RAWIP_PATH BPF_PATH_TETHER TETHER_UPSTREAM6_TC_PROG_RAWIP_NAME
152#define TETHER_UPSTREAM6_TC_PROG_ETHER_PATH BPF_PATH_TETHER TETHER_UPSTREAM6_TC_PROG_ETHER_NAME
153
154#define TETHER_UPSTREAM6_MAP_PATH BPF_PATH_TETHER "map_offload_tether_upstream6_map"
155
156typedef struct {
157 uint32_t iif; // The input interface index
158 // TODO: extend this to include dstMac and src ip /64 subnet
159} TetherUpstream6Key;
160STRUCT_SIZE(TetherUpstream6Key, 4);
161
162#define TETHER_DOWNSTREAM4_TC_PROG_RAWIP_NAME "prog_offload_schedcls_tether_downstream4_rawip"
163#define TETHER_DOWNSTREAM4_TC_PROG_ETHER_NAME "prog_offload_schedcls_tether_downstream4_ether"
164
165#define TETHER_DOWNSTREAM4_TC_PROG_RAWIP_PATH BPF_PATH_TETHER TETHER_DOWNSTREAM4_TC_PROG_RAWIP_NAME
166#define TETHER_DOWNSTREAM4_TC_PROG_ETHER_PATH BPF_PATH_TETHER TETHER_DOWNSTREAM4_TC_PROG_ETHER_NAME
167
168#define TETHER_DOWNSTREAM4_MAP_PATH BPF_PATH_TETHER "map_offload_tether_downstream4_map"
169
170
171#define TETHER_UPSTREAM4_TC_PROG_RAWIP_NAME "prog_offload_schedcls_tether_upstream4_rawip"
172#define TETHER_UPSTREAM4_TC_PROG_ETHER_NAME "prog_offload_schedcls_tether_upstream4_ether"
173
174#define TETHER_UPSTREAM4_TC_PROG_RAWIP_PATH BPF_PATH_TETHER TETHER_UPSTREAM4_TC_PROG_RAWIP_NAME
175#define TETHER_UPSTREAM4_TC_PROG_ETHER_PATH BPF_PATH_TETHER TETHER_UPSTREAM4_TC_PROG_ETHER_NAME
176
177#define TETHER_UPSTREAM4_MAP_PATH BPF_PATH_TETHER "map_offload_tether_upstream4_map"
178
179typedef struct {
180 uint32_t iif; // The input interface index
181 uint8_t dstMac[ETH_ALEN]; // destination ethernet mac address (zeroed iff rawip ingress)
182 uint16_t l4Proto; // IPPROTO_TCP/UDP/...
183 struct in_addr src4; // source &
184 struct in_addr dst4; // destination IPv4 addresses
185 __be16 srcPort; // source &
186 __be16 dstPort; // destination TCP/UDP/... ports
187} Tether4Key;
188STRUCT_SIZE(Tether4Key, 4 + 6 + 2 + 4 + 4 + 2 + 2); // 24
189
190typedef struct {
191 uint32_t oif; // The output interface to redirect to
192 struct ethhdr macHeader; // includes dst/src mac and ethertype (zeroed iff rawip egress)
193 uint16_t pmtu; // Maximum L3 output path/route mtu
194 struct in6_addr src46; // source & (always IPv4 mapped for downstream)
195 struct in6_addr dst46; // destination IP addresses (may be IPv4 mapped or IPv6 for upstream)
196 __be16 srcPort; // source &
197 __be16 dstPort; // destination tcp/udp/... ports
198 uint64_t last_used; // Kernel updates on each use with bpf_ktime_get_boot_ns()
199} Tether4Value;
200STRUCT_SIZE(Tether4Value, 4 + 14 + 2 + 16 + 16 + 2 + 2 + 8); // 64
201
202#define TETHER_DOWNSTREAM_XDP_PROG_RAWIP_NAME "prog_offload_xdp_tether_downstream_rawip"
203#define TETHER_DOWNSTREAM_XDP_PROG_ETHER_NAME "prog_offload_xdp_tether_downstream_ether"
204
205#define TETHER_DOWNSTREAM_XDP_PROG_RAWIP_PATH BPF_PATH_TETHER TETHER_DOWNSTREAM_XDP_PROG_RAWIP_NAME
206#define TETHER_DOWNSTREAM_XDP_PROG_ETHER_PATH BPF_PATH_TETHER TETHER_DOWNSTREAM_XDP_PROG_ETHER_NAME
207
208#define TETHER_UPSTREAM_XDP_PROG_RAWIP_NAME "prog_offload_xdp_tether_upstream_rawip"
209#define TETHER_UPSTREAM_XDP_PROG_ETHER_NAME "prog_offload_xdp_tether_upstream_ether"
210
211#define TETHER_UPSTREAM_XDP_PROG_RAWIP_PATH BPF_PATH_TETHER TETHER_UPSTREAM_XDP_PROG_RAWIP_NAME
212#define TETHER_UPSTREAM_XDP_PROG_ETHER_PATH BPF_PATH_TETHER TETHER_UPSTREAM_XDP_PROG_ETHER_NAME
213
214#undef STRUCT_SIZE