blob: f87fc492ae387946d372a02fb1df7e84d412bb1a [file] [log] [blame]
Hungming Chen56c632c2020-09-10 15:42:58 +08001/*
2 * Copyright (C) 2020 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 Żenczykowski561fb4a2023-10-24 18:48:54 -070017#ifdef MAINLINE
Maciej Żenczykowski07d30132022-04-23 12:33:32 -070018// 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 Żenczykowski4e4f8722024-06-15 06:38:08 -070021#define BPFLOADER_MIN_VER BPFLOADER_MAINLINE_T_VERSION
Maciej Żenczykowski561fb4a2023-10-24 18:48:54 -070022#else /* MAINLINE */
Maciej Żenczykowski4e4f8722024-06-15 06:38:08 -070023// The resulting .o needs to load on the Android S bpfloader
Maciej Żenczykowskif7699522022-05-24 15:56:03 -070024#define BPFLOADER_MIN_VER BPFLOADER_S_VERSION
Maciej Żenczykowski4e4f8722024-06-15 06:38:08 -070025#define BPFLOADER_MAX_VER BPFLOADER_T_VERSION
Maciej Żenczykowski561fb4a2023-10-24 18:48:54 -070026#endif /* MAINLINE */
Maciej Żenczykowskia457bf72021-10-22 21:41:25 -070027
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -070028// Warning: values other than AID_ROOT don't work for map uid on BpfLoader < v0.21
29#define TETHERING_UID AID_ROOT
30
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -070031#define TETHERING_GID AID_NETWORK_STACK
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -070032
Hungming Chen56c632c2020-09-10 15:42:58 +080033#include "bpf_net_helpers.h"
Maciej Żenczykowski4e3321e2022-12-08 12:59:23 +000034#include "offload.h"
Hungming Chen56c632c2020-09-10 15:42:58 +080035
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -080036// From kernel:include/net/ip.h
37#define IP_DF 0x4000 // Flag: "Don't Fragment"
38
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -080039// ----- Helper functions for offsets to fields -----
40
41// They all assume simple IP packets:
42// - no VLAN ethernet tags
43// - no IPv4 options (see IPV4_HLEN/TCP4_OFFSET/UDP4_OFFSET)
44// - no IPv6 extension headers
45// - no TCP options (see TCP_HLEN)
46
47//#define ETH_HLEN sizeof(struct ethhdr)
48#define IP4_HLEN sizeof(struct iphdr)
49#define IP6_HLEN sizeof(struct ipv6hdr)
50#define TCP_HLEN sizeof(struct tcphdr)
51#define UDP_HLEN sizeof(struct udphdr)
52
53// Offsets from beginning of L4 (TCP/UDP) header
54#define TCP_OFFSET(field) offsetof(struct tcphdr, field)
55#define UDP_OFFSET(field) offsetof(struct udphdr, field)
56
57// Offsets from beginning of L3 (IPv4) header
58#define IP4_OFFSET(field) offsetof(struct iphdr, field)
59#define IP4_TCP_OFFSET(field) (IP4_HLEN + TCP_OFFSET(field))
60#define IP4_UDP_OFFSET(field) (IP4_HLEN + UDP_OFFSET(field))
61
62// Offsets from beginning of L3 (IPv6) header
63#define IP6_OFFSET(field) offsetof(struct ipv6hdr, field)
64#define IP6_TCP_OFFSET(field) (IP6_HLEN + TCP_OFFSET(field))
65#define IP6_UDP_OFFSET(field) (IP6_HLEN + UDP_OFFSET(field))
66
67// Offsets from beginning of L2 (ie. Ethernet) header (which must be present)
68#define ETH_IP4_OFFSET(field) (ETH_HLEN + IP4_OFFSET(field))
69#define ETH_IP4_TCP_OFFSET(field) (ETH_HLEN + IP4_TCP_OFFSET(field))
70#define ETH_IP4_UDP_OFFSET(field) (ETH_HLEN + IP4_UDP_OFFSET(field))
71#define ETH_IP6_OFFSET(field) (ETH_HLEN + IP6_OFFSET(field))
72#define ETH_IP6_TCP_OFFSET(field) (ETH_HLEN + IP6_TCP_OFFSET(field))
73#define ETH_IP6_UDP_OFFSET(field) (ETH_HLEN + IP6_UDP_OFFSET(field))
74
Maciej Żenczykowskie982f092021-02-04 20:26:26 -080075// ----- Tethering Error Counters -----
76
Maciej Żenczykowskibe25f962022-10-20 00:13:15 +000077// Note that pre-T devices with Mediatek chipsets may have a kernel bug (bad patch
78// "[ALPS05162612] bpf: fix ubsan error") making it impossible to write to non-zero
Maciej Żenczykowskif932a8d2022-12-03 10:30:24 +000079// offset of bpf map ARRAYs. This file (offload.o) loads on S+, but luckily this
Maciej Żenczykowskibe25f962022-10-20 00:13:15 +000080// array is only written by bpf code, and only read by userspace.
81DEFINE_BPF_MAP_RO(tether_error_map, ARRAY, uint32_t, uint32_t, BPF_TETHER_ERR__MAX, TETHERING_GID)
Maciej Żenczykowskie982f092021-02-04 20:26:26 -080082
Maciej Żenczykowski3f32a832021-03-17 19:27:23 -070083#define COUNT_AND_RETURN(counter, ret) do { \
Maciej Żenczykowskie982f092021-02-04 20:26:26 -080084 uint32_t code = BPF_TETHER_ERR_ ## counter; \
85 uint32_t *count = bpf_tether_error_map_lookup_elem(&code); \
Maciej Żenczykowski3f32a832021-03-17 19:27:23 -070086 if (count) __sync_fetch_and_add(count, 1); \
87 return ret; \
Maciej Żenczykowskie982f092021-02-04 20:26:26 -080088} while(0)
89
90#define TC_DROP(counter) COUNT_AND_RETURN(counter, TC_ACT_SHOT)
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -070091#define TC_PUNT(counter) COUNT_AND_RETURN(counter, TC_ACT_PIPE)
Maciej Żenczykowskie982f092021-02-04 20:26:26 -080092
93#define XDP_DROP(counter) COUNT_AND_RETURN(counter, XDP_DROP)
94#define XDP_PUNT(counter) COUNT_AND_RETURN(counter, XDP_PASS)
95
96// ----- Tethering Data Stats and Limits -----
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -080097
Hungming Chen56c632c2020-09-10 15:42:58 +080098// Tethering stats, indexed by upstream interface.
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -070099DEFINE_BPF_MAP_GRW(tether_stats_map, HASH, TetherStatsKey, TetherStatsValue, 16, TETHERING_GID)
Hungming Chen56c632c2020-09-10 15:42:58 +0800100
101// Tethering data limit, indexed by upstream interface.
102// (tethering allowed when stats[iif].rxBytes + stats[iif].txBytes < limit[iif])
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700103DEFINE_BPF_MAP_GRW(tether_limit_map, HASH, TetherLimitKey, TetherLimitValue, 16, TETHERING_GID)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800104
105// ----- IPv6 Support -----
106
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800107DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 64,
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700108 TETHERING_GID)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800109
110DEFINE_BPF_MAP_GRW(tether_downstream64_map, HASH, TetherDownstream64Key, TetherDownstream64Value,
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700111 1024, TETHERING_GID)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800112
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800113DEFINE_BPF_MAP_GRW(tether_upstream6_map, HASH, TetherUpstream6Key, Tether6Value, 64,
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700114 TETHERING_GID)
Hungming Chen56c632c2020-09-10 15:42:58 +0800115
Maciej Żenczykowski8d3bde72023-10-08 18:43:23 -0700116static inline __always_inline int do_forward6(struct __sk_buff* skb,
117 const struct rawip_bool rawip,
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700118 const struct stream_bool stream,
Maciej Żenczykowski2e0da9b2024-07-24 17:54:47 -0700119 __unused const struct kver_uint kver) {
Maciej Żenczykowski8d3bde72023-10-08 18:43:23 -0700120 const bool is_ethernet = !rawip.rawip;
121
Maciej Żenczykowski8e69ec12021-03-07 07:06:13 -0800122 // Must be meta-ethernet IPv6 frame
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700123 if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_PIPE;
Hungming Chen56c632c2020-09-10 15:42:58 +0800124
Maciej Żenczykowski18552e82021-01-24 19:59:05 -0800125 // Require ethernet dst mac address to be our unicast address.
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700126 if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_PIPE;
Maciej Żenczykowski18552e82021-01-24 19:59:05 -0800127
Maciej Żenczykowski8e69ec12021-03-07 07:06:13 -0800128 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
129
130 // Since the program never writes via DPA (direct packet access) auto-pull/unclone logic does
131 // not trigger and thus we need to manually make sure we can read packet headers via DPA.
132 // Note: this is a blind best effort pull, which may fail or pull less - this doesn't matter.
133 // It has to be done early cause it will invalidate any skb->data/data_end derived pointers.
Maciej Żenczykowski824fb292022-04-11 23:29:46 -0700134 try_make_writable(skb, l2_header_size + IP6_HLEN + TCP_HLEN);
Maciej Żenczykowski8e69ec12021-03-07 07:06:13 -0800135
136 void* data = (void*)(long)skb->data;
137 const void* data_end = (void*)(long)skb->data_end;
138 struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet
139 struct ipv6hdr* ip6 = is_ethernet ? (void*)(eth + 1) : data;
Hungming Chen56c632c2020-09-10 15:42:58 +0800140
141 // Must have (ethernet and) ipv6 header
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700142 if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_PIPE;
Hungming Chen56c632c2020-09-10 15:42:58 +0800143
144 // Ethertype - if present - must be IPv6
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700145 if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_PIPE;
Hungming Chen56c632c2020-09-10 15:42:58 +0800146
147 // IP version must be 6
Maciej Żenczykowskib82bf652022-08-10 19:28:16 +0000148 if (ip6->version != 6) TC_PUNT(INVALID_IPV6_VERSION);
Hungming Chen56c632c2020-09-10 15:42:58 +0800149
150 // Cannot decrement during forward if already zero or would be zero,
151 // Let the kernel's stack handle these cases and generate appropriate ICMP errors.
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800152 if (ip6->hop_limit <= 1) TC_PUNT(LOW_TTL);
Hungming Chen56c632c2020-09-10 15:42:58 +0800153
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -0800154 // If hardware offload is running and programming flows based on conntrack entries,
155 // try not to interfere with it.
156 if (ip6->nexthdr == IPPROTO_TCP) {
157 struct tcphdr* tcph = (void*)(ip6 + 1);
158
159 // Make sure we can get at the tcp header
Lorenzo Colittib81584d2021-02-06 00:00:58 +0900160 if (data + l2_header_size + sizeof(*ip6) + sizeof(*tcph) > data_end)
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800161 TC_PUNT(INVALID_TCP_HEADER);
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -0800162
163 // Do not offload TCP packets with any one of the SYN/FIN/RST flags
Maciej Żenczykowski0dd2bb32022-08-10 19:33:06 +0000164 if (tcph->syn || tcph->fin || tcph->rst) TC_PUNT(TCPV6_CONTROL_PACKET);
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -0800165 }
166
Hungming Chen56c632c2020-09-10 15:42:58 +0800167 // Protect against forwarding packets sourced from ::1 or fe80::/64 or other weirdness.
168 __be32 src32 = ip6->saddr.s6_addr32[0];
169 if (src32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
170 (src32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800171 TC_PUNT(NON_GLOBAL_SRC);
Hungming Chen56c632c2020-09-10 15:42:58 +0800172
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800173 // Protect against forwarding packets destined to ::1 or fe80::/64 or other weirdness.
174 __be32 dst32 = ip6->daddr.s6_addr32[0];
175 if (dst32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
176 (dst32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800177 TC_PUNT(NON_GLOBAL_DST);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800178
179 // In the upstream direction do not forward traffic within the same /64 subnet.
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700180 if (!stream.down && (src32 == dst32) && (ip6->saddr.s6_addr32[1] == ip6->daddr.s6_addr32[1]))
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800181 TC_PUNT(LOCAL_SRC_DST);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800182
183 TetherDownstream6Key kd = {
Hungming Chen56c632c2020-09-10 15:42:58 +0800184 .iif = skb->ifindex,
185 .neigh6 = ip6->daddr,
186 };
187
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800188 TetherUpstream6Key ku = {
189 .iif = skb->ifindex,
KH Shi3f738fc2023-05-23 22:37:17 +0800190 // Retrieve the first 64 bits of the source IPv6 address in network order
191 .src64 = *(uint64_t*)&(ip6->saddr.s6_addr32[0]),
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800192 };
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700193 if (is_ethernet) __builtin_memcpy(stream.down ? kd.dstMac : ku.dstMac, eth->h_dest, ETH_ALEN);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800194
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700195 Tether6Value* v = stream.down ? bpf_tether_downstream6_map_lookup_elem(&kd)
196 : bpf_tether_upstream6_map_lookup_elem(&ku);
Hungming Chen56c632c2020-09-10 15:42:58 +0800197
198 // If we don't find any offload information then simply let the core stack handle it...
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700199 if (!v) return TC_ACT_PIPE;
Hungming Chen56c632c2020-09-10 15:42:58 +0800200
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700201 uint32_t stat_and_limit_k = stream.down ? skb->ifindex : v->oif;
Hungming Chen56c632c2020-09-10 15:42:58 +0800202
203 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
204
205 // If we don't have anywhere to put stats, then abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800206 if (!stat_v) TC_PUNT(NO_STATS_ENTRY);
Hungming Chen56c632c2020-09-10 15:42:58 +0800207
208 uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k);
209
210 // If we don't have a limit, then abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800211 if (!limit_v) TC_PUNT(NO_LIMIT_ENTRY);
Hungming Chen56c632c2020-09-10 15:42:58 +0800212
213 // Required IPv6 minimum mtu is 1280, below that not clear what we should do, abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800214 if (v->pmtu < IPV6_MIN_MTU) TC_PUNT(BELOW_IPV6_MTU);
Hungming Chen56c632c2020-09-10 15:42:58 +0800215
216 // Approximate handling of TCP/IPv6 overhead for incoming LRO/GRO packets: default
217 // outbound path mtu of 1500 is not necessarily correct, but worst case we simply
218 // undercount, which is still better then not accounting for this overhead at all.
219 // Note: this really shouldn't be device/path mtu at all, but rather should be
220 // derived from this particular connection's mss (ie. from gro segment size).
221 // This would require a much newer kernel with newer ebpf accessors.
222 // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header)
223 uint64_t packets = 1;
Maciej Żenczykowskibab0c1a2022-12-29 11:18:35 +0000224 uint64_t L3_bytes = skb->len - l2_header_size;
225 if (L3_bytes > v->pmtu) {
226 const int tcp6_overhead = sizeof(struct ipv6hdr) + sizeof(struct tcphdr) + 12;
227 const int mss = v->pmtu - tcp6_overhead;
228 const uint64_t payload = L3_bytes - tcp6_overhead;
Hungming Chen56c632c2020-09-10 15:42:58 +0800229 packets = (payload + mss - 1) / mss;
Maciej Żenczykowskibab0c1a2022-12-29 11:18:35 +0000230 L3_bytes = tcp6_overhead * packets + payload;
Hungming Chen56c632c2020-09-10 15:42:58 +0800231 }
232
233 // Are we past the limit? If so, then abort...
234 // Note: will not overflow since u64 is 936 years even at 5Gbps.
235 // Do not drop here. Offload is just that, whenever we fail to handle
236 // a packet we let the core stack deal with things.
237 // (The core stack needs to handle limits correctly anyway,
238 // since we don't offload all traffic in both directions)
Maciej Żenczykowskibab0c1a2022-12-29 11:18:35 +0000239 if (stat_v->rxBytes + stat_v->txBytes + L3_bytes > *limit_v) TC_PUNT(LIMIT_REACHED);
Hungming Chen56c632c2020-09-10 15:42:58 +0800240
241 if (!is_ethernet) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800242 // Try to inject an ethernet header, and simply return if we fail.
243 // We do this even if TX interface is RAWIP and thus does not need an ethernet header,
244 // because this is easier and the kernel will strip extraneous ethernet header.
245 if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) {
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700246 __sync_fetch_and_add(stream.down ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800247 TC_PUNT(CHANGE_HEAD_FAILED);
Hungming Chen56c632c2020-09-10 15:42:58 +0800248 }
249
250 // bpf_skb_change_head() invalidates all pointers - reload them
251 data = (void*)(long)skb->data;
252 data_end = (void*)(long)skb->data_end;
253 eth = data;
254 ip6 = (void*)(eth + 1);
255
256 // I do not believe this can ever happen, but keep the verifier happy...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800257 if (data + sizeof(struct ethhdr) + sizeof(*ip6) > data_end) {
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700258 __sync_fetch_and_add(stream.down ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800259 TC_DROP(TOO_SHORT);
Hungming Chen56c632c2020-09-10 15:42:58 +0800260 }
261 };
262
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800263 // At this point we always have an ethernet header - which will get stripped by the
264 // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid.
265 // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct.
266
Hungming Chen56c632c2020-09-10 15:42:58 +0800267 // CHECKSUM_COMPLETE is a 16-bit one's complement sum,
268 // thus corrections for it need to be done in 16-byte chunks at even offsets.
269 // IPv6 nexthdr is at offset 6, while hop limit is at offset 7
270 uint8_t old_hl = ip6->hop_limit;
271 --ip6->hop_limit;
272 uint8_t new_hl = ip6->hop_limit;
273
274 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
275 // (-ENOTSUPP) if it isn't.
276 bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl));
277
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700278 __sync_fetch_and_add(stream.down ? &stat_v->rxPackets : &stat_v->txPackets, packets);
279 __sync_fetch_and_add(stream.down ? &stat_v->rxBytes : &stat_v->txBytes, L3_bytes);
Hungming Chen56c632c2020-09-10 15:42:58 +0800280
281 // Overwrite any mac header with the new one
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800282 // For a rawip tx interface it will simply be a bunch of zeroes and later stripped.
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800283 *eth = v->macHeader;
Hungming Chen56c632c2020-09-10 15:42:58 +0800284
285 // Redirect to forwarded interface.
286 //
287 // Note that bpf_redirect() cannot fail unless you pass invalid flags.
288 // The redirect actually happens after the ebpf program has already terminated,
289 // and can fail for example for mtu reasons at that point in time, but there's nothing
290 // we can do about it here.
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800291 return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */);
Hungming Chen56c632c2020-09-10 15:42:58 +0800292}
293
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700294DEFINE_BPF_PROG("schedcls/tether_downstream6_ether", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800295 sched_cls_tether_downstream6_ether)
Maciej Żenczykowski6b7829f2021-01-18 00:03:37 -0800296(struct __sk_buff* skb) {
Maciej Żenczykowski63fadd12023-04-19 16:39:57 -0700297 return do_forward6(skb, ETHER, DOWNSTREAM, KVER_NONE);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800298}
299
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700300DEFINE_BPF_PROG("schedcls/tether_upstream6_ether", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800301 sched_cls_tether_upstream6_ether)
302(struct __sk_buff* skb) {
Maciej Żenczykowski63fadd12023-04-19 16:39:57 -0700303 return do_forward6(skb, ETHER, UPSTREAM, KVER_NONE);
Hungming Chen56c632c2020-09-10 15:42:58 +0800304}
305
306// Note: section names must be unique to prevent programs from appending to each other,
307// so instead the bpf loader will strip everything past the final $ symbol when actually
308// pinning the program into the filesystem.
309//
310// bpf_skb_change_head() is only present on 4.14+ and 2 trivial kernel patches are needed:
311// ANDROID: net: bpf: Allow TC programs to call BPF_FUNC_skb_change_head
312// ANDROID: net: bpf: permit redirect from ingress L3 to egress L2 devices at near max mtu
313// (the first of those has already been upstreamed)
314//
Maciej Żenczykowskiefe862e2022-07-28 09:36:52 +0000315// These were added to 4.14+ Android Common Kernel in R (including the original release of ACK 5.4)
316// and there is a test in kernel/tests/net/test/bpf_test.py testSkbChangeHead()
317// and in system/netd/tests/binder_test.cpp NetdBinderTest TetherOffloadForwarding.
Hungming Chen56c632c2020-09-10 15:42:58 +0800318//
Maciej Żenczykowskiefe862e2022-07-28 09:36:52 +0000319// Hence, these mandatory (must load successfully) implementations for 4.14+ kernels:
320DEFINE_BPF_PROG_KVER("schedcls/tether_downstream6_rawip$4_14", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700321 sched_cls_tether_downstream6_rawip_4_14, KVER_4_14)
Hungming Chen56c632c2020-09-10 15:42:58 +0800322(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700323 return do_forward6(skb, RAWIP, DOWNSTREAM, KVER_4_14);
Hungming Chen56c632c2020-09-10 15:42:58 +0800324}
325
Maciej Żenczykowskiefe862e2022-07-28 09:36:52 +0000326DEFINE_BPF_PROG_KVER("schedcls/tether_upstream6_rawip$4_14", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700327 sched_cls_tether_upstream6_rawip_4_14, KVER_4_14)
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800328(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700329 return do_forward6(skb, RAWIP, UPSTREAM, KVER_4_14);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800330}
331
Maciej Żenczykowskiefe862e2022-07-28 09:36:52 +0000332// and define no-op stubs for pre-4.14 kernels.
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700333DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$stub", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700334 sched_cls_tether_downstream6_rawip_stub, KVER_NONE, KVER_4_14)
Maciej Żenczykowski2e0da9b2024-07-24 17:54:47 -0700335(__unused struct __sk_buff* skb) {
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700336 return TC_ACT_PIPE;
Hungming Chen56c632c2020-09-10 15:42:58 +0800337}
338
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700339DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$stub", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700340 sched_cls_tether_upstream6_rawip_stub, KVER_NONE, KVER_4_14)
Maciej Żenczykowski2e0da9b2024-07-24 17:54:47 -0700341(__unused struct __sk_buff* skb) {
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700342 return TC_ACT_PIPE;
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800343}
344
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800345// ----- IPv4 Support -----
346
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700347DEFINE_BPF_MAP_GRW(tether_downstream4_map, HASH, Tether4Key, Tether4Value, 1024, TETHERING_GID)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800348
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700349DEFINE_BPF_MAP_GRW(tether_upstream4_map, HASH, Tether4Key, Tether4Value, 1024, TETHERING_GID)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800350
Maciej Żenczykowskif72c8aa2022-04-28 02:02:45 -0700351static inline __always_inline int do_forward4_bottom(struct __sk_buff* skb,
352 const int l2_header_size, void* data, const void* data_end,
Maciej Żenczykowski8d3bde72023-10-08 18:43:23 -0700353 struct ethhdr* eth, struct iphdr* ip, const struct rawip_bool rawip,
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700354 const struct stream_bool stream, const struct updatetime_bool updatetime,
Maciej Żenczykowski2e0da9b2024-07-24 17:54:47 -0700355 const bool is_tcp, __unused const struct kver_uint kver) {
Maciej Żenczykowski8d3bde72023-10-08 18:43:23 -0700356 const bool is_ethernet = !rawip.rawip;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800357 struct tcphdr* tcph = is_tcp ? (void*)(ip + 1) : NULL;
358 struct udphdr* udph = is_tcp ? NULL : (void*)(ip + 1);
359
360 if (is_tcp) {
361 // Make sure we can get at the tcp header
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800362 if (data + l2_header_size + sizeof(*ip) + sizeof(*tcph) > data_end)
363 TC_PUNT(SHORT_TCP_HEADER);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800364
365 // If hardware offload is running and programming flows based on conntrack entries, try not
366 // to interfere with it, so do not offload TCP packets with any one of the SYN/FIN/RST flags
Maciej Żenczykowski0dd2bb32022-08-10 19:33:06 +0000367 if (tcph->syn || tcph->fin || tcph->rst) TC_PUNT(TCPV4_CONTROL_PACKET);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800368 } else { // UDP
369 // Make sure we can get at the udp header
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800370 if (data + l2_header_size + sizeof(*ip) + sizeof(*udph) > data_end)
371 TC_PUNT(SHORT_UDP_HEADER);
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800372
373 // Skip handling of CHECKSUM_COMPLETE packets with udp checksum zero due to need for
374 // additional updating of skb->csum (this could be fixed up manually with more effort).
375 //
376 // Note that the in-kernel implementation of 'int64_t bpf_csum_update(skb, u32 csum)' is:
377 // if (skb->ip_summed == CHECKSUM_COMPLETE)
378 // return (skb->csum = csum_add(skb->csum, csum));
379 // else
380 // return -ENOTSUPP;
381 //
382 // So this will punt any CHECKSUM_COMPLETE packet with a zero UDP checksum,
383 // and leave all other packets unaffected (since it just at most adds zero to skb->csum).
384 //
385 // In practice this should almost never trigger because most nics do not generate
386 // CHECKSUM_COMPLETE packets on receive - especially so for nics/drivers on a phone.
387 //
388 // Additionally since we're forwarding, in most cases the value of the skb->csum field
389 // shouldn't matter (it's not used by physical nic egress).
390 //
391 // It only matters if we're ingressing through a CHECKSUM_COMPLETE capable nic
392 // and egressing through a virtual interface looping back to the kernel itself
393 // (ie. something like veth) where the CHECKSUM_COMPLETE/skb->csum can get reused
394 // on ingress.
395 //
396 // If we were in the kernel we'd simply probably call
397 // void skb_checksum_complete_unset(struct sk_buff *skb) {
398 // if (skb->ip_summed == CHECKSUM_COMPLETE) skb->ip_summed = CHECKSUM_NONE;
399 // }
400 // here instead. Perhaps there should be a bpf helper for that?
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800401 if (!udph->check && (bpf_csum_update(skb, 0) >= 0)) TC_PUNT(UDP_CSUM_ZERO);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800402 }
403
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800404 Tether4Key k = {
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800405 .iif = skb->ifindex,
406 .l4Proto = ip->protocol,
407 .src4.s_addr = ip->saddr,
408 .dst4.s_addr = ip->daddr,
409 .srcPort = is_tcp ? tcph->source : udph->source,
410 .dstPort = is_tcp ? tcph->dest : udph->dest,
411 };
Maciej Żenczykowski62733f52021-04-01 21:51:41 -0700412 if (is_ethernet) __builtin_memcpy(k.dstMac, eth->h_dest, ETH_ALEN);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800413
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700414 Tether4Value* v = stream.down ? bpf_tether_downstream4_map_lookup_elem(&k)
415 : bpf_tether_upstream4_map_lookup_elem(&k);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800416
417 // If we don't find any offload information then simply let the core stack handle it...
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700418 if (!v) return TC_ACT_PIPE;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800419
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700420 uint32_t stat_and_limit_k = stream.down ? skb->ifindex : v->oif;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800421
422 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
423
424 // If we don't have anywhere to put stats, then abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800425 if (!stat_v) TC_PUNT(NO_STATS_ENTRY);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800426
427 uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k);
428
429 // If we don't have a limit, then abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800430 if (!limit_v) TC_PUNT(NO_LIMIT_ENTRY);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800431
432 // Required IPv4 minimum mtu is 68, below that not clear what we should do, abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800433 if (v->pmtu < 68) TC_PUNT(BELOW_IPV4_MTU);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800434
435 // Approximate handling of TCP/IPv4 overhead for incoming LRO/GRO packets: default
436 // outbound path mtu of 1500 is not necessarily correct, but worst case we simply
437 // undercount, which is still better then not accounting for this overhead at all.
438 // Note: this really shouldn't be device/path mtu at all, but rather should be
439 // derived from this particular connection's mss (ie. from gro segment size).
440 // This would require a much newer kernel with newer ebpf accessors.
441 // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header)
442 uint64_t packets = 1;
Maciej Żenczykowskibab0c1a2022-12-29 11:18:35 +0000443 uint64_t L3_bytes = skb->len - l2_header_size;
444 if (L3_bytes > v->pmtu) {
445 const int tcp4_overhead = sizeof(struct iphdr) + sizeof(struct tcphdr) + 12;
446 const int mss = v->pmtu - tcp4_overhead;
447 const uint64_t payload = L3_bytes - tcp4_overhead;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800448 packets = (payload + mss - 1) / mss;
Maciej Żenczykowskibab0c1a2022-12-29 11:18:35 +0000449 L3_bytes = tcp4_overhead * packets + payload;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800450 }
451
452 // Are we past the limit? If so, then abort...
453 // Note: will not overflow since u64 is 936 years even at 5Gbps.
454 // Do not drop here. Offload is just that, whenever we fail to handle
455 // a packet we let the core stack deal with things.
456 // (The core stack needs to handle limits correctly anyway,
457 // since we don't offload all traffic in both directions)
Maciej Żenczykowskibab0c1a2022-12-29 11:18:35 +0000458 if (stat_v->rxBytes + stat_v->txBytes + L3_bytes > *limit_v) TC_PUNT(LIMIT_REACHED);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800459
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800460 if (!is_ethernet) {
461 // Try to inject an ethernet header, and simply return if we fail.
462 // We do this even if TX interface is RAWIP and thus does not need an ethernet header,
463 // because this is easier and the kernel will strip extraneous ethernet header.
464 if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) {
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700465 __sync_fetch_and_add(stream.down ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800466 TC_PUNT(CHANGE_HEAD_FAILED);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800467 }
468
469 // bpf_skb_change_head() invalidates all pointers - reload them
470 data = (void*)(long)skb->data;
471 data_end = (void*)(long)skb->data_end;
472 eth = data;
473 ip = (void*)(eth + 1);
474 tcph = is_tcp ? (void*)(ip + 1) : NULL;
475 udph = is_tcp ? NULL : (void*)(ip + 1);
476
477 // I do not believe this can ever happen, but keep the verifier happy...
478 if (data + sizeof(struct ethhdr) + sizeof(*ip) + (is_tcp ? sizeof(*tcph) : sizeof(*udph)) > data_end) {
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700479 __sync_fetch_and_add(stream.down ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800480 TC_DROP(TOO_SHORT);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800481 }
482 };
483
484 // At this point we always have an ethernet header - which will get stripped by the
485 // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid.
486 // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct.
487
488 // Overwrite any mac header with the new one
489 // For a rawip tx interface it will simply be a bunch of zeroes and later stripped.
490 *eth = v->macHeader;
491
Maciej Żenczykowskic29af792021-07-02 01:54:04 -0700492 // Decrement the IPv4 TTL, we already know it's greater than 1.
493 // u8 TTL field is followed by u8 protocol to make a u16 for ipv4 header checksum update.
494 // Since we're keeping the ipv4 checksum valid (which means the checksum of the entire
495 // ipv4 header remains 0), the overall checksum of the entire packet does not change.
496 const int sz2 = sizeof(__be16);
497 const __be16 old_ttl_proto = *(__be16 *)&ip->ttl;
498 const __be16 new_ttl_proto = old_ttl_proto - htons(0x0100);
499 bpf_l3_csum_replace(skb, ETH_IP4_OFFSET(check), old_ttl_proto, new_ttl_proto, sz2);
500 bpf_skb_store_bytes(skb, ETH_IP4_OFFSET(ttl), &new_ttl_proto, sz2, 0);
501
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800502 const int l4_offs_csum = is_tcp ? ETH_IP4_TCP_OFFSET(check) : ETH_IP4_UDP_OFFSET(check);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800503 const int sz4 = sizeof(__be32);
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800504 // UDP 0 is special and stored as FFFF (this flag also causes a csum of 0 to be unmodified)
505 const int l4_flags = is_tcp ? 0 : BPF_F_MARK_MANGLED_0;
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800506 const __be32 old_daddr = k.dst4.s_addr;
507 const __be32 old_saddr = k.src4.s_addr;
508 const __be32 new_daddr = v->dst46.s6_addr32[3];
509 const __be32 new_saddr = v->src46.s6_addr32[3];
510
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800511 bpf_l4_csum_replace(skb, l4_offs_csum, old_daddr, new_daddr, sz4 | BPF_F_PSEUDO_HDR | l4_flags);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800512 bpf_l3_csum_replace(skb, ETH_IP4_OFFSET(check), old_daddr, new_daddr, sz4);
513 bpf_skb_store_bytes(skb, ETH_IP4_OFFSET(daddr), &new_daddr, sz4, 0);
514
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800515 bpf_l4_csum_replace(skb, l4_offs_csum, old_saddr, new_saddr, sz4 | BPF_F_PSEUDO_HDR | l4_flags);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800516 bpf_l3_csum_replace(skb, ETH_IP4_OFFSET(check), old_saddr, new_saddr, sz4);
517 bpf_skb_store_bytes(skb, ETH_IP4_OFFSET(saddr), &new_saddr, sz4, 0);
518
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800519 // The offsets for TCP and UDP ports: source (u16 @ L4 offset 0) & dest (u16 @ L4 offset 2) are
520 // actually the same, so the compiler should just optimize them both down to a constant.
521 bpf_l4_csum_replace(skb, l4_offs_csum, k.srcPort, v->srcPort, sz2 | l4_flags);
522 bpf_skb_store_bytes(skb, is_tcp ? ETH_IP4_TCP_OFFSET(source) : ETH_IP4_UDP_OFFSET(source),
523 &v->srcPort, sz2, 0);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800524
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800525 bpf_l4_csum_replace(skb, l4_offs_csum, k.dstPort, v->dstPort, sz2 | l4_flags);
526 bpf_skb_store_bytes(skb, is_tcp ? ETH_IP4_TCP_OFFSET(dest) : ETH_IP4_UDP_OFFSET(dest),
527 &v->dstPort, sz2, 0);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800528
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800529 // This requires the bpf_ktime_get_boot_ns() helper which was added in 5.8,
530 // and backported to all Android Common Kernel 4.14+ trees.
Maciej Żenczykowski8a6c6d52023-10-10 00:59:31 -0700531 if (updatetime.updatetime) v->last_used = bpf_ktime_get_boot_ns();
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800532
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700533 __sync_fetch_and_add(stream.down ? &stat_v->rxPackets : &stat_v->txPackets, packets);
534 __sync_fetch_and_add(stream.down ? &stat_v->rxBytes : &stat_v->txBytes, L3_bytes);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800535
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800536 // Redirect to forwarded interface.
537 //
538 // Note that bpf_redirect() cannot fail unless you pass invalid flags.
539 // The redirect actually happens after the ebpf program has already terminated,
540 // and can fail for example for mtu reasons at that point in time, but there's nothing
541 // we can do about it here.
542 return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800543}
544
Maciej Żenczykowski8d3bde72023-10-08 18:43:23 -0700545static inline __always_inline int do_forward4(struct __sk_buff* skb,
546 const struct rawip_bool rawip,
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700547 const struct stream_bool stream,
Maciej Żenczykowski8a6c6d52023-10-10 00:59:31 -0700548 const struct updatetime_bool updatetime,
Maciej Żenczykowski8d3bde72023-10-08 18:43:23 -0700549 const struct kver_uint kver) {
550 const bool is_ethernet = !rawip.rawip;
551
Maciej Żenczykowskif72c8aa2022-04-28 02:02:45 -0700552 // Require ethernet dst mac address to be our unicast address.
553 if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_PIPE;
554
555 // Must be meta-ethernet IPv4 frame
556 if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_PIPE;
557
558 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
559
560 // Since the program never writes via DPA (direct packet access) auto-pull/unclone logic does
561 // not trigger and thus we need to manually make sure we can read packet headers via DPA.
562 // Note: this is a blind best effort pull, which may fail or pull less - this doesn't matter.
563 // It has to be done early cause it will invalidate any skb->data/data_end derived pointers.
564 try_make_writable(skb, l2_header_size + IP4_HLEN + TCP_HLEN);
565
566 void* data = (void*)(long)skb->data;
567 const void* data_end = (void*)(long)skb->data_end;
568 struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet
569 struct iphdr* ip = is_ethernet ? (void*)(eth + 1) : data;
570
571 // Must have (ethernet and) ipv4 header
572 if (data + l2_header_size + sizeof(*ip) > data_end) return TC_ACT_PIPE;
573
574 // Ethertype - if present - must be IPv4
575 if (is_ethernet && (eth->h_proto != htons(ETH_P_IP))) return TC_ACT_PIPE;
576
577 // IP version must be 4
Maciej Żenczykowskib82bf652022-08-10 19:28:16 +0000578 if (ip->version != 4) TC_PUNT(INVALID_IPV4_VERSION);
Maciej Żenczykowskif72c8aa2022-04-28 02:02:45 -0700579
580 // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header
581 if (ip->ihl != 5) TC_PUNT(HAS_IP_OPTIONS);
582
583 // Calculate the IPv4 one's complement checksum of the IPv4 header.
584 __wsum sum4 = 0;
Maciej Żenczykowskic11dfd82024-07-24 17:54:41 -0700585 for (unsigned i = 0; i < sizeof(*ip) / sizeof(__u16); ++i) {
Maciej Żenczykowskif72c8aa2022-04-28 02:02:45 -0700586 sum4 += ((__u16*)ip)[i];
587 }
588 // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4
589 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE
590 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16
591 // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF
592 if (sum4 != 0xFFFF) TC_PUNT(CHECKSUM);
593
594 // Minimum IPv4 total length is the size of the header
595 if (ntohs(ip->tot_len) < sizeof(*ip)) TC_PUNT(TRUNCATED_IPV4);
596
597 // We are incapable of dealing with IPv4 fragments
598 if (ip->frag_off & ~htons(IP_DF)) TC_PUNT(IS_IP_FRAG);
599
600 // Cannot decrement during forward if already zero or would be zero,
601 // Let the kernel's stack handle these cases and generate appropriate ICMP errors.
602 if (ip->ttl <= 1) TC_PUNT(LOW_TTL);
603
604 // If we cannot update the 'last_used' field due to lack of bpf_ktime_get_boot_ns() helper,
605 // then it is not safe to offload UDP due to the small conntrack timeouts, as such,
606 // in such a situation we can only support TCP. This also has the added nice benefit of
607 // using a separate error counter, and thus making it obvious which version of the program
608 // is loaded.
Maciej Żenczykowski8a6c6d52023-10-10 00:59:31 -0700609 if (!updatetime.updatetime && ip->protocol != IPPROTO_TCP) TC_PUNT(NON_TCP);
Maciej Żenczykowskif72c8aa2022-04-28 02:02:45 -0700610
611 // We do not support offloading anything besides IPv4 TCP and UDP, due to need for NAT,
612 // but no need to check this if !updatetime due to check immediately above.
Maciej Żenczykowski8a6c6d52023-10-10 00:59:31 -0700613 if (updatetime.updatetime && (ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
Maciej Żenczykowskif72c8aa2022-04-28 02:02:45 -0700614 TC_PUNT(NON_TCP_UDP);
615
616 // We want to make sure that the compiler will, in the !updatetime case, entirely optimize
617 // out all the non-tcp logic. Also note that at this point is_udp === !is_tcp.
Maciej Żenczykowski8a6c6d52023-10-10 00:59:31 -0700618 const bool is_tcp = !updatetime.updatetime || (ip->protocol == IPPROTO_TCP);
Maciej Żenczykowskif72c8aa2022-04-28 02:02:45 -0700619
620 // This is a bit of a hack to make things easier on the bpf verifier.
621 // (In particular I believe the Linux 4.14 kernel's verifier can get confused later on about
622 // what offsets into the packet are valid and can spuriously reject the program, this is
623 // because it fails to realize that is_tcp && !is_tcp is impossible)
624 //
625 // For both TCP & UDP we'll need to read and modify the src/dst ports, which so happen to
626 // always be in the first 4 bytes of the L4 header. Additionally for UDP we'll need access
627 // to the checksum field which is in bytes 7 and 8. While for TCP we'll need to read the
628 // TCP flags (at offset 13) and access to the checksum field (2 bytes at offset 16).
629 // As such we *always* need access to at least 8 bytes.
630 if (data + l2_header_size + sizeof(*ip) + 8 > data_end) TC_PUNT(SHORT_L4_HEADER);
631
632 // We're forcing the compiler to emit two copies of the following code, optimized
633 // separately for is_tcp being true or false. This simplifies the resulting bpf
634 // byte code sufficiently that the 4.14 bpf verifier is able to keep track of things.
635 // Without this (updatetime == true) case would fail to bpf verify on 4.14 even
636 // if the underlying requisite kernel support (bpf_ktime_get_boot_ns) was backported.
637 if (is_tcp) {
638 return do_forward4_bottom(skb, l2_header_size, data, data_end, eth, ip,
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700639 rawip, stream, updatetime, /* is_tcp */ true, kver);
Maciej Żenczykowskif72c8aa2022-04-28 02:02:45 -0700640 } else {
641 return do_forward4_bottom(skb, l2_header_size, data, data_end, eth, ip,
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700642 rawip, stream, updatetime, /* is_tcp */ false, kver);
Maciej Żenczykowskif72c8aa2022-04-28 02:02:45 -0700643 }
644}
645
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800646// Full featured (required) implementations for 5.8+ kernels (these are S+ by definition)
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800647
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700648DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_rawip$5_8", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700649 sched_cls_tether_downstream4_rawip_5_8, KVER_5_8)
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800650(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700651 return do_forward4(skb, RAWIP, DOWNSTREAM, UPDATETIME, KVER_5_8);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800652}
653
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700654DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_rawip$5_8", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700655 sched_cls_tether_upstream4_rawip_5_8, KVER_5_8)
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800656(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700657 return do_forward4(skb, RAWIP, UPSTREAM, UPDATETIME, KVER_5_8);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800658}
659
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700660DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_ether$5_8", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700661 sched_cls_tether_downstream4_ether_5_8, KVER_5_8)
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800662(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700663 return do_forward4(skb, ETHER, DOWNSTREAM, UPDATETIME, KVER_5_8);
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800664}
665
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700666DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_ether$5_8", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700667 sched_cls_tether_upstream4_ether_5_8, KVER_5_8)
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800668(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700669 return do_forward4(skb, ETHER, UPSTREAM, UPDATETIME, KVER_5_8);
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800670}
671
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800672// Full featured (optional) implementations for 4.14-S, 4.19-S & 5.4-S kernels
673// (optional, because we need to be able to fallback for 4.14/4.19/5.4 pre-S kernels)
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800674
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800675DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$opt",
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700676 TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800677 sched_cls_tether_downstream4_rawip_opt,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700678 KVER_4_14, KVER_5_8)
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800679(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700680 return do_forward4(skb, RAWIP, DOWNSTREAM, UPDATETIME, KVER_4_14);
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800681}
682
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800683DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$opt",
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700684 TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800685 sched_cls_tether_upstream4_rawip_opt,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700686 KVER_4_14, KVER_5_8)
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800687(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700688 return do_forward4(skb, RAWIP, UPSTREAM, UPDATETIME, KVER_4_14);
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800689}
690
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800691DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$opt",
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700692 TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800693 sched_cls_tether_downstream4_ether_opt,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700694 KVER_4_14, KVER_5_8)
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800695(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700696 return do_forward4(skb, ETHER, DOWNSTREAM, UPDATETIME, KVER_4_14);
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800697}
698
699DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$opt",
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700700 TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800701 sched_cls_tether_upstream4_ether_opt,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700702 KVER_4_14, KVER_5_8)
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800703(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700704 return do_forward4(skb, ETHER, UPSTREAM, UPDATETIME, KVER_4_14);
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800705}
706
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800707// Partial (TCP-only: will not update 'last_used' field) implementations for 4.14+ kernels.
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800708// These will be loaded only if the above optional ones failed (loading of *these* must succeed
709// for 5.4+, since that is always an R patched kernel).
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800710//
711// [Note: as a result TCP connections will not have their conntrack timeout refreshed, however,
712// since /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established defaults to 432000 (seconds),
713// this in practice means they'll break only after 5 days. This seems an acceptable trade-off.
714//
715// Additionally kernel/tests change "net-test: add bpf_ktime_get_ns / bpf_ktime_get_boot_ns tests"
716// which enforces and documents the required kernel cherrypicks will make it pretty unlikely that
717// many devices upgrading to S will end up relying on these fallback programs.
718
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800719// RAWIP: Required for 5.4-R kernels -- which always support bpf_skb_change_head().
720
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700721DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$5_4", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700722 sched_cls_tether_downstream4_rawip_5_4, KVER_5_4, KVER_5_8)
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800723(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700724 return do_forward4(skb, RAWIP, DOWNSTREAM, NO_UPDATETIME, KVER_5_4);
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800725}
726
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700727DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$5_4", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700728 sched_cls_tether_upstream4_rawip_5_4, KVER_5_4, KVER_5_8)
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800729(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700730 return do_forward4(skb, RAWIP, UPSTREAM, NO_UPDATETIME, KVER_5_4);
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800731}
732
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800733// RAWIP: Optional for 4.14/4.19 (R) kernels -- which support bpf_skb_change_head().
734// [Note: fallback for 4.14/4.19 (P/Q) kernels is below in stub section]
735
736DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$4_14",
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700737 TETHERING_UID, TETHERING_GID,
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800738 sched_cls_tether_downstream4_rawip_4_14,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700739 KVER_4_14, KVER_5_4)
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800740(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700741 return do_forward4(skb, RAWIP, DOWNSTREAM, NO_UPDATETIME, KVER_4_14);
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800742}
743
744DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$4_14",
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700745 TETHERING_UID, TETHERING_GID,
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800746 sched_cls_tether_upstream4_rawip_4_14,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700747 KVER_4_14, KVER_5_4)
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800748(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700749 return do_forward4(skb, RAWIP, UPSTREAM, NO_UPDATETIME, KVER_4_14);
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800750}
751
752// ETHER: Required for 4.14-Q/R, 4.19-Q/R & 5.4-R kernels.
753
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700754DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$4_14", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700755 sched_cls_tether_downstream4_ether_4_14, KVER_4_14, KVER_5_8)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800756(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700757 return do_forward4(skb, ETHER, DOWNSTREAM, NO_UPDATETIME, KVER_4_14);
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800758}
759
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700760DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$4_14", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700761 sched_cls_tether_upstream4_ether_4_14, KVER_4_14, KVER_5_8)
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800762(struct __sk_buff* skb) {
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700763 return do_forward4(skb, ETHER, UPSTREAM, NO_UPDATETIME, KVER_4_14);
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800764}
765
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800766// Placeholder (no-op) implementations for older Q kernels
767
768// RAWIP: 4.9-P/Q, 4.14-P/Q & 4.19-Q kernels -- without bpf_skb_change_head() for tc programs
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800769
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700770DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$stub", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700771 sched_cls_tether_downstream4_rawip_stub, KVER_NONE, KVER_5_4)
Maciej Żenczykowski2e0da9b2024-07-24 17:54:47 -0700772(__unused struct __sk_buff* skb) {
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700773 return TC_ACT_PIPE;
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800774}
775
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700776DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$stub", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700777 sched_cls_tether_upstream4_rawip_stub, KVER_NONE, KVER_5_4)
Maciej Żenczykowski2e0da9b2024-07-24 17:54:47 -0700778(__unused struct __sk_buff* skb) {
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700779 return TC_ACT_PIPE;
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800780}
781
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800782// ETHER: 4.9-P/Q kernel
783
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700784DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$stub", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700785 sched_cls_tether_downstream4_ether_stub, KVER_NONE, KVER_4_14)
Maciej Żenczykowski2e0da9b2024-07-24 17:54:47 -0700786(__unused struct __sk_buff* skb) {
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700787 return TC_ACT_PIPE;
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800788}
789
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700790DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$stub", TETHERING_UID, TETHERING_GID,
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700791 sched_cls_tether_upstream4_ether_stub, KVER_NONE, KVER_4_14)
Maciej Żenczykowski2e0da9b2024-07-24 17:54:47 -0700792(__unused struct __sk_buff* skb) {
Maciej Żenczykowski6e66a362021-08-24 15:43:15 -0700793 return TC_ACT_PIPE;
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800794}
795
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800796// ----- XDP Support -----
797
Maciej Żenczykowskiccce4a32022-07-17 01:28:38 -0700798DEFINE_BPF_MAP_GRW(tether_dev_map, DEVMAP_HASH, uint32_t, uint32_t, 64, TETHERING_GID)
Maciej Żenczykowskidb2cff52021-03-01 21:22:49 -0800799
Maciej Żenczykowski2e0da9b2024-07-24 17:54:47 -0700800static inline __always_inline int do_xdp_forward6(__unused struct xdp_md *ctx,
801 __unused const struct rawip_bool rawip, __unused const struct stream_bool stream) {
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800802 return XDP_PASS;
803}
804
Maciej Żenczykowski2e0da9b2024-07-24 17:54:47 -0700805static inline __always_inline int do_xdp_forward4(__unused struct xdp_md *ctx,
806 __unused const struct rawip_bool rawip, __unused const struct stream_bool stream) {
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800807 return XDP_PASS;
808}
809
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700810static inline __always_inline int do_xdp_forward_ether(struct xdp_md *ctx,
811 const struct stream_bool stream) {
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800812 const void* data = (void*)(long)ctx->data;
813 const void* data_end = (void*)(long)ctx->data_end;
814 const struct ethhdr* eth = data;
815
816 // Make sure we actually have an ethernet header
817 if ((void*)(eth + 1) > data_end) return XDP_PASS;
818
819 if (eth->h_proto == htons(ETH_P_IPV6))
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700820 return do_xdp_forward6(ctx, ETHER, stream);
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800821 if (eth->h_proto == htons(ETH_P_IP))
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700822 return do_xdp_forward4(ctx, ETHER, stream);
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800823
824 // Anything else we don't know how to handle...
825 return XDP_PASS;
826}
827
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700828static inline __always_inline int do_xdp_forward_rawip(struct xdp_md *ctx,
829 const struct stream_bool stream) {
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800830 const void* data = (void*)(long)ctx->data;
831 const void* data_end = (void*)(long)ctx->data_end;
832
833 // The top nibble of both IPv4 and IPv6 headers is the IP version.
834 if (data_end - data < 1) return XDP_PASS;
835 const uint8_t v = (*(uint8_t*)data) >> 4;
836
Maciej Żenczykowskie1a615a2023-10-10 03:34:56 -0700837 if (v == 6) return do_xdp_forward6(ctx, RAWIP, stream);
838 if (v == 4) return do_xdp_forward4(ctx, RAWIP, stream);
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800839
840 // Anything else we don't know how to handle...
841 return XDP_PASS;
842}
843
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800844#define DEFINE_XDP_PROG(str, func) \
Maciej Żenczykowski901c7102023-10-06 15:47:46 -0700845 DEFINE_BPF_PROG_KVER(str, TETHERING_UID, TETHERING_GID, func, KVER_5_9)(struct xdp_md *ctx)
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800846
847DEFINE_XDP_PROG("xdp/tether_downstream_ether",
848 xdp_tether_downstream_ether) {
Maciej Żenczykowskicad569f2023-04-19 16:33:30 -0700849 return do_xdp_forward_ether(ctx, DOWNSTREAM);
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800850}
851
852DEFINE_XDP_PROG("xdp/tether_downstream_rawip",
853 xdp_tether_downstream_rawip) {
Maciej Żenczykowskicad569f2023-04-19 16:33:30 -0700854 return do_xdp_forward_rawip(ctx, DOWNSTREAM);
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800855}
856
857DEFINE_XDP_PROG("xdp/tether_upstream_ether",
858 xdp_tether_upstream_ether) {
Maciej Żenczykowski941ea032023-04-19 16:33:02 -0700859 return do_xdp_forward_ether(ctx, UPSTREAM);
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800860}
861
862DEFINE_XDP_PROG("xdp/tether_upstream_rawip",
863 xdp_tether_upstream_rawip) {
Maciej Żenczykowski941ea032023-04-19 16:33:02 -0700864 return do_xdp_forward_rawip(ctx, UPSTREAM);
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800865}
866
Hungming Chen56c632c2020-09-10 15:42:58 +0800867LICENSE("Apache 2.0");
Maciej Żenczykowski3dd052e2024-04-19 23:39:44 +0000868CRITICAL("Connectivity (Tethering)");
Maciej Żenczykowskide1342a2023-06-09 05:45:57 +0000869DISABLE_BTF_ON_USER_BUILDS();