blob: 7f9754d89630055250373623b9d4c3845debd359 [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
17#include <linux/if.h>
18#include <linux/ip.h>
19#include <linux/ipv6.h>
20#include <linux/pkt_cls.h>
21#include <linux/tcp.h>
22
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -080023// bionic kernel uapi linux/udp.h header is munged...
24#define __kernel_udphdr udphdr
25#include <linux/udp.h>
26
Hungming Chen56c632c2020-09-10 15:42:58 +080027#include "bpf_helpers.h"
28#include "bpf_net_helpers.h"
Lorenzo Colittib81584d2021-02-06 00:00:58 +090029#include "bpf_tethering.h"
Hungming Chen56c632c2020-09-10 15:42:58 +080030
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -080031// From kernel:include/net/ip.h
32#define IP_DF 0x4000 // Flag: "Don't Fragment"
33
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -080034// ----- Helper functions for offsets to fields -----
35
36// They all assume simple IP packets:
37// - no VLAN ethernet tags
38// - no IPv4 options (see IPV4_HLEN/TCP4_OFFSET/UDP4_OFFSET)
39// - no IPv6 extension headers
40// - no TCP options (see TCP_HLEN)
41
42//#define ETH_HLEN sizeof(struct ethhdr)
43#define IP4_HLEN sizeof(struct iphdr)
44#define IP6_HLEN sizeof(struct ipv6hdr)
45#define TCP_HLEN sizeof(struct tcphdr)
46#define UDP_HLEN sizeof(struct udphdr)
47
48// Offsets from beginning of L4 (TCP/UDP) header
49#define TCP_OFFSET(field) offsetof(struct tcphdr, field)
50#define UDP_OFFSET(field) offsetof(struct udphdr, field)
51
52// Offsets from beginning of L3 (IPv4) header
53#define IP4_OFFSET(field) offsetof(struct iphdr, field)
54#define IP4_TCP_OFFSET(field) (IP4_HLEN + TCP_OFFSET(field))
55#define IP4_UDP_OFFSET(field) (IP4_HLEN + UDP_OFFSET(field))
56
57// Offsets from beginning of L3 (IPv6) header
58#define IP6_OFFSET(field) offsetof(struct ipv6hdr, field)
59#define IP6_TCP_OFFSET(field) (IP6_HLEN + TCP_OFFSET(field))
60#define IP6_UDP_OFFSET(field) (IP6_HLEN + UDP_OFFSET(field))
61
62// Offsets from beginning of L2 (ie. Ethernet) header (which must be present)
63#define ETH_IP4_OFFSET(field) (ETH_HLEN + IP4_OFFSET(field))
64#define ETH_IP4_TCP_OFFSET(field) (ETH_HLEN + IP4_TCP_OFFSET(field))
65#define ETH_IP4_UDP_OFFSET(field) (ETH_HLEN + IP4_UDP_OFFSET(field))
66#define ETH_IP6_OFFSET(field) (ETH_HLEN + IP6_OFFSET(field))
67#define ETH_IP6_TCP_OFFSET(field) (ETH_HLEN + IP6_TCP_OFFSET(field))
68#define ETH_IP6_UDP_OFFSET(field) (ETH_HLEN + IP6_UDP_OFFSET(field))
69
Maciej Żenczykowskie982f092021-02-04 20:26:26 -080070// ----- Tethering Error Counters -----
71
72DEFINE_BPF_MAP_GRW(tether_error_map, ARRAY, uint32_t, uint32_t, BPF_TETHER_ERR__MAX,
73 AID_NETWORK_STACK)
74
75#define COUNT_AND_RETURN(counter, ret) do { \
76 uint32_t code = BPF_TETHER_ERR_ ## counter; \
77 uint32_t *count = bpf_tether_error_map_lookup_elem(&code); \
78 if (count) __sync_fetch_and_add(count, 1); \
79 return ret; \
80} while(0)
81
82#define TC_DROP(counter) COUNT_AND_RETURN(counter, TC_ACT_SHOT)
83#define TC_PUNT(counter) COUNT_AND_RETURN(counter, TC_ACT_OK)
84
85#define XDP_DROP(counter) COUNT_AND_RETURN(counter, XDP_DROP)
86#define XDP_PUNT(counter) COUNT_AND_RETURN(counter, XDP_PASS)
87
88// ----- Tethering Data Stats and Limits -----
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -080089
Hungming Chen56c632c2020-09-10 15:42:58 +080090// Tethering stats, indexed by upstream interface.
Maciej Żenczykowski088fe192021-01-20 13:34:17 -080091DEFINE_BPF_MAP_GRW(tether_stats_map, HASH, TetherStatsKey, TetherStatsValue, 16, AID_NETWORK_STACK)
Hungming Chen56c632c2020-09-10 15:42:58 +080092
93// Tethering data limit, indexed by upstream interface.
94// (tethering allowed when stats[iif].rxBytes + stats[iif].txBytes < limit[iif])
Maciej Żenczykowski088fe192021-01-20 13:34:17 -080095DEFINE_BPF_MAP_GRW(tether_limit_map, HASH, TetherLimitKey, TetherLimitValue, 16, AID_NETWORK_STACK)
96
97// ----- IPv6 Support -----
98
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -080099DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 64,
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800100 AID_NETWORK_STACK)
101
102DEFINE_BPF_MAP_GRW(tether_downstream64_map, HASH, TetherDownstream64Key, TetherDownstream64Value,
Maciej Żenczykowski8549af92021-02-25 19:11:34 -0800103 1024, AID_NETWORK_STACK)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800104
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800105DEFINE_BPF_MAP_GRW(tether_upstream6_map, HASH, TetherUpstream6Key, Tether6Value, 64,
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800106 AID_NETWORK_STACK)
Hungming Chen56c632c2020-09-10 15:42:58 +0800107
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800108static inline __always_inline int do_forward6(struct __sk_buff* skb, const bool is_ethernet,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800109 const bool downstream) {
110 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
Hungming Chen56c632c2020-09-10 15:42:58 +0800111 void* data = (void*)(long)skb->data;
112 const void* data_end = (void*)(long)skb->data_end;
113 struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet
114 struct ipv6hdr* ip6 = is_ethernet ? (void*)(eth + 1) : data;
115
Maciej Żenczykowski18552e82021-01-24 19:59:05 -0800116 // Require ethernet dst mac address to be our unicast address.
117 if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_OK;
118
Hungming Chen56c632c2020-09-10 15:42:58 +0800119 // Must be meta-ethernet IPv6 frame
120 if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_OK;
121
122 // Must have (ethernet and) ipv6 header
123 if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_OK;
124
125 // Ethertype - if present - must be IPv6
126 if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_OK;
127
128 // IP version must be 6
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800129 if (ip6->version != 6) TC_PUNT(INVALID_IP_VERSION);
Hungming Chen56c632c2020-09-10 15:42:58 +0800130
131 // Cannot decrement during forward if already zero or would be zero,
132 // Let the kernel's stack handle these cases and generate appropriate ICMP errors.
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800133 if (ip6->hop_limit <= 1) TC_PUNT(LOW_TTL);
Hungming Chen56c632c2020-09-10 15:42:58 +0800134
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -0800135 // If hardware offload is running and programming flows based on conntrack entries,
136 // try not to interfere with it.
137 if (ip6->nexthdr == IPPROTO_TCP) {
138 struct tcphdr* tcph = (void*)(ip6 + 1);
139
140 // Make sure we can get at the tcp header
Lorenzo Colittib81584d2021-02-06 00:00:58 +0900141 if (data + l2_header_size + sizeof(*ip6) + sizeof(*tcph) > data_end)
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800142 TC_PUNT(INVALID_TCP_HEADER);
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -0800143
144 // Do not offload TCP packets with any one of the SYN/FIN/RST flags
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800145 if (tcph->syn || tcph->fin || tcph->rst) TC_PUNT(TCP_CONTROL_PACKET);
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -0800146 }
147
Hungming Chen56c632c2020-09-10 15:42:58 +0800148 // Protect against forwarding packets sourced from ::1 or fe80::/64 or other weirdness.
149 __be32 src32 = ip6->saddr.s6_addr32[0];
150 if (src32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
151 (src32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800152 TC_PUNT(NON_GLOBAL_SRC);
Hungming Chen56c632c2020-09-10 15:42:58 +0800153
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800154 // Protect against forwarding packets destined to ::1 or fe80::/64 or other weirdness.
155 __be32 dst32 = ip6->daddr.s6_addr32[0];
156 if (dst32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
157 (dst32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800158 TC_PUNT(NON_GLOBAL_DST);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800159
160 // In the upstream direction do not forward traffic within the same /64 subnet.
161 if (!downstream && (src32 == dst32) && (ip6->saddr.s6_addr32[1] == ip6->daddr.s6_addr32[1]))
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800162 TC_PUNT(LOCAL_SRC_DST);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800163
164 TetherDownstream6Key kd = {
Hungming Chen56c632c2020-09-10 15:42:58 +0800165 .iif = skb->ifindex,
166 .neigh6 = ip6->daddr,
167 };
168
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800169 TetherUpstream6Key ku = {
170 .iif = skb->ifindex,
171 };
172
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800173 Tether6Value* v = downstream ? bpf_tether_downstream6_map_lookup_elem(&kd)
174 : bpf_tether_upstream6_map_lookup_elem(&ku);
Hungming Chen56c632c2020-09-10 15:42:58 +0800175
176 // If we don't find any offload information then simply let the core stack handle it...
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800177 if (!v) return TC_ACT_OK;
Hungming Chen56c632c2020-09-10 15:42:58 +0800178
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800179 uint32_t stat_and_limit_k = downstream ? skb->ifindex : v->oif;
Hungming Chen56c632c2020-09-10 15:42:58 +0800180
181 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
182
183 // If we don't have anywhere to put stats, then abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800184 if (!stat_v) TC_PUNT(NO_STATS_ENTRY);
Hungming Chen56c632c2020-09-10 15:42:58 +0800185
186 uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k);
187
188 // If we don't have a limit, then abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800189 if (!limit_v) TC_PUNT(NO_LIMIT_ENTRY);
Hungming Chen56c632c2020-09-10 15:42:58 +0800190
191 // Required IPv6 minimum mtu is 1280, below that not clear what we should do, abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800192 if (v->pmtu < IPV6_MIN_MTU) TC_PUNT(BELOW_IPV6_MTU);
Hungming Chen56c632c2020-09-10 15:42:58 +0800193
194 // Approximate handling of TCP/IPv6 overhead for incoming LRO/GRO packets: default
195 // outbound path mtu of 1500 is not necessarily correct, but worst case we simply
196 // undercount, which is still better then not accounting for this overhead at all.
197 // Note: this really shouldn't be device/path mtu at all, but rather should be
198 // derived from this particular connection's mss (ie. from gro segment size).
199 // This would require a much newer kernel with newer ebpf accessors.
200 // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header)
201 uint64_t packets = 1;
202 uint64_t bytes = skb->len;
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800203 if (bytes > v->pmtu) {
Hungming Chen56c632c2020-09-10 15:42:58 +0800204 const int tcp_overhead = sizeof(struct ipv6hdr) + sizeof(struct tcphdr) + 12;
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800205 const int mss = v->pmtu - tcp_overhead;
Hungming Chen56c632c2020-09-10 15:42:58 +0800206 const uint64_t payload = bytes - tcp_overhead;
207 packets = (payload + mss - 1) / mss;
208 bytes = tcp_overhead * packets + payload;
209 }
210
211 // Are we past the limit? If so, then abort...
212 // Note: will not overflow since u64 is 936 years even at 5Gbps.
213 // Do not drop here. Offload is just that, whenever we fail to handle
214 // a packet we let the core stack deal with things.
215 // (The core stack needs to handle limits correctly anyway,
216 // since we don't offload all traffic in both directions)
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800217 if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) TC_PUNT(LIMIT_REACHED);
Hungming Chen56c632c2020-09-10 15:42:58 +0800218
219 if (!is_ethernet) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800220 // Try to inject an ethernet header, and simply return if we fail.
221 // We do this even if TX interface is RAWIP and thus does not need an ethernet header,
222 // because this is easier and the kernel will strip extraneous ethernet header.
223 if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) {
224 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800225 TC_PUNT(CHANGE_HEAD_FAILED);
Hungming Chen56c632c2020-09-10 15:42:58 +0800226 }
227
228 // bpf_skb_change_head() invalidates all pointers - reload them
229 data = (void*)(long)skb->data;
230 data_end = (void*)(long)skb->data_end;
231 eth = data;
232 ip6 = (void*)(eth + 1);
233
234 // I do not believe this can ever happen, but keep the verifier happy...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800235 if (data + sizeof(struct ethhdr) + sizeof(*ip6) > data_end) {
236 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800237 TC_DROP(TOO_SHORT);
Hungming Chen56c632c2020-09-10 15:42:58 +0800238 }
239 };
240
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800241 // At this point we always have an ethernet header - which will get stripped by the
242 // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid.
243 // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct.
244
Hungming Chen56c632c2020-09-10 15:42:58 +0800245 // CHECKSUM_COMPLETE is a 16-bit one's complement sum,
246 // thus corrections for it need to be done in 16-byte chunks at even offsets.
247 // IPv6 nexthdr is at offset 6, while hop limit is at offset 7
248 uint8_t old_hl = ip6->hop_limit;
249 --ip6->hop_limit;
250 uint8_t new_hl = ip6->hop_limit;
251
252 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
253 // (-ENOTSUPP) if it isn't.
254 bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl));
255
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800256 __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets);
257 __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes);
Hungming Chen56c632c2020-09-10 15:42:58 +0800258
259 // Overwrite any mac header with the new one
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800260 // For a rawip tx interface it will simply be a bunch of zeroes and later stripped.
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800261 *eth = v->macHeader;
Hungming Chen56c632c2020-09-10 15:42:58 +0800262
263 // Redirect to forwarded interface.
264 //
265 // Note that bpf_redirect() cannot fail unless you pass invalid flags.
266 // The redirect actually happens after the ebpf program has already terminated,
267 // and can fail for example for mtu reasons at that point in time, but there's nothing
268 // we can do about it here.
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800269 return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */);
Hungming Chen56c632c2020-09-10 15:42:58 +0800270}
271
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800272DEFINE_BPF_PROG("schedcls/tether_downstream6_ether", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800273 sched_cls_tether_downstream6_ether)
Maciej Żenczykowski6b7829f2021-01-18 00:03:37 -0800274(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800275 return do_forward6(skb, /* is_ethernet */ true, /* downstream */ true);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800276}
277
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800278DEFINE_BPF_PROG("schedcls/tether_upstream6_ether", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800279 sched_cls_tether_upstream6_ether)
280(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800281 return do_forward6(skb, /* is_ethernet */ true, /* downstream */ false);
Hungming Chen56c632c2020-09-10 15:42:58 +0800282}
283
284// Note: section names must be unique to prevent programs from appending to each other,
285// so instead the bpf loader will strip everything past the final $ symbol when actually
286// pinning the program into the filesystem.
287//
288// bpf_skb_change_head() is only present on 4.14+ and 2 trivial kernel patches are needed:
289// ANDROID: net: bpf: Allow TC programs to call BPF_FUNC_skb_change_head
290// ANDROID: net: bpf: permit redirect from ingress L3 to egress L2 devices at near max mtu
291// (the first of those has already been upstreamed)
292//
293// 5.4 kernel support was only added to Android Common Kernel in R,
294// and thus a 5.4 kernel always supports this.
295//
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800296// Hence, these mandatory (must load successfully) implementations for 5.4+ kernels:
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800297DEFINE_BPF_PROG_KVER("schedcls/tether_downstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800298 sched_cls_tether_downstream6_rawip_5_4, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800299(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800300 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800301}
302
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800303DEFINE_BPF_PROG_KVER("schedcls/tether_upstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800304 sched_cls_tether_upstream6_rawip_5_4, KVER(5, 4, 0))
305(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800306 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800307}
308
309// and these identical optional (may fail to load) implementations for [4.14..5.4) patched kernels:
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800310DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$4_14",
311 AID_ROOT, AID_NETWORK_STACK,
312 sched_cls_tether_downstream6_rawip_4_14,
313 KVER(4, 14, 0), KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800314(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800315 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800316}
317
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800318DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$4_14",
319 AID_ROOT, AID_NETWORK_STACK,
320 sched_cls_tether_upstream6_rawip_4_14,
321 KVER(4, 14, 0), KVER(5, 4, 0))
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800322(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800323 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800324}
325
326// and define no-op stubs for [4.9,4.14) and unpatched [4.14,5.4) kernels.
Hungming Chen56c632c2020-09-10 15:42:58 +0800327// (if the above real 4.14+ program loaded successfully, then bpfloader will have already pinned
328// it at the same location this one would be pinned at and will thus skip loading this stub)
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800329DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800330 sched_cls_tether_downstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800331(struct __sk_buff* skb) {
332 return TC_ACT_OK;
333}
334
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800335DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800336 sched_cls_tether_upstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
337(struct __sk_buff* skb) {
338 return TC_ACT_OK;
339}
340
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800341// ----- IPv4 Support -----
342
Maciej Żenczykowski8549af92021-02-25 19:11:34 -0800343DEFINE_BPF_MAP_GRW(tether_downstream4_map, HASH, Tether4Key, Tether4Value, 1024, AID_NETWORK_STACK)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800344
Maciej Żenczykowski8549af92021-02-25 19:11:34 -0800345DEFINE_BPF_MAP_GRW(tether_upstream4_map, HASH, Tether4Key, Tether4Value, 1024, AID_NETWORK_STACK)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800346
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800347static inline __always_inline int do_forward4(struct __sk_buff* skb, const bool is_ethernet,
Maciej Żenczykowskiaefa0952021-02-14 23:15:19 -0800348 const bool downstream, const bool updatetime) {
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800349 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
350 void* data = (void*)(long)skb->data;
351 const void* data_end = (void*)(long)skb->data_end;
352 struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet
353 struct iphdr* ip = is_ethernet ? (void*)(eth + 1) : data;
354
355 // Require ethernet dst mac address to be our unicast address.
356 if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_OK;
357
358 // Must be meta-ethernet IPv4 frame
359 if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_OK;
360
361 // Must have (ethernet and) ipv4 header
362 if (data + l2_header_size + sizeof(*ip) > data_end) return TC_ACT_OK;
363
364 // Ethertype - if present - must be IPv4
365 if (is_ethernet && (eth->h_proto != htons(ETH_P_IP))) return TC_ACT_OK;
366
367 // IP version must be 4
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800368 if (ip->version != 4) TC_PUNT(INVALID_IP_VERSION);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800369
370 // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800371 if (ip->ihl != 5) TC_PUNT(HAS_IP_OPTIONS);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800372
373 // Calculate the IPv4 one's complement checksum of the IPv4 header.
374 __wsum sum4 = 0;
375 for (int i = 0; i < sizeof(*ip) / sizeof(__u16); ++i) {
376 sum4 += ((__u16*)ip)[i];
377 }
378 // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4
379 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE
380 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16
381 // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800382 if (sum4 != 0xFFFF) TC_PUNT(CHECKSUM);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800383
384 // Minimum IPv4 total length is the size of the header
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800385 if (ntohs(ip->tot_len) < sizeof(*ip)) TC_PUNT(TRUNCATED_IPV4);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800386
387 // We are incapable of dealing with IPv4 fragments
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800388 if (ip->frag_off & ~htons(IP_DF)) TC_PUNT(IS_IP_FRAG);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800389
390 // Cannot decrement during forward if already zero or would be zero,
391 // Let the kernel's stack handle these cases and generate appropriate ICMP errors.
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800392 if (ip->ttl <= 1) TC_PUNT(LOW_TTL);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800393
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800394 // If we cannot update the 'last_used' field due to lack of bpf_ktime_get_boot_ns() helper,
395 // then it is not safe to offload UDP due to the small conntrack timeouts, as such,
396 // in such a situation we can only support TCP. This also has the added nice benefit of
397 // using a separate error counter, and thus making it obvious which version of the program
398 // is loaded.
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800399 if (!updatetime && ip->protocol != IPPROTO_TCP) TC_PUNT(NON_TCP);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800400
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800401 // We do not support offloading anything besides IPv4 TCP and UDP, due to need for NAT,
402 // but no need to check this if !updatetime due to check immediately above.
403 if (updatetime && (ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800404 TC_PUNT(NON_TCP_UDP);
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800405
406 // We want to make sure that the compiler will, in the !updatetime case, entirely optimize
407 // out all the non-tcp logic. Also note that at this point is_udp === !is_tcp.
408 const bool is_tcp = !updatetime || (ip->protocol == IPPROTO_TCP);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800409
Maciej Żenczykowski82ee26b2021-02-16 16:10:08 -0800410 // This is a bit of a hack to make things easier on the bpf verifier.
411 // (In particular I believe the Linux 4.14 kernel's verifier can get confused later on about
412 // what offsets into the packet are valid and can spuriously reject the program, this is
413 // because it fails to realize that is_tcp && !is_tcp is impossible)
414 //
415 // For both TCP & UDP we'll need to read and modify the src/dst ports, which so happen to
416 // always be in the first 4 bytes of the L4 header. Additionally for UDP we'll need access
417 // to the checksum field which is in bytes 7 and 8. While for TCP we'll need to read the
418 // TCP flags (at offset 13) and access to the checksum field (2 bytes at offset 16).
419 // As such we *always* need access to at least 8 bytes.
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800420 if (data + l2_header_size + sizeof(*ip) + 8 > data_end) TC_PUNT(SHORT_L4_HEADER);
Maciej Żenczykowski82ee26b2021-02-16 16:10:08 -0800421
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800422 struct tcphdr* tcph = is_tcp ? (void*)(ip + 1) : NULL;
423 struct udphdr* udph = is_tcp ? NULL : (void*)(ip + 1);
424
425 if (is_tcp) {
426 // Make sure we can get at the tcp header
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800427 if (data + l2_header_size + sizeof(*ip) + sizeof(*tcph) > data_end)
428 TC_PUNT(SHORT_TCP_HEADER);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800429
430 // If hardware offload is running and programming flows based on conntrack entries, try not
431 // to interfere with it, so do not offload TCP packets with any one of the SYN/FIN/RST flags
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800432 if (tcph->syn || tcph->fin || tcph->rst) TC_PUNT(TCP_CONTROL_PACKET);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800433 } else { // UDP
434 // Make sure we can get at the udp header
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800435 if (data + l2_header_size + sizeof(*ip) + sizeof(*udph) > data_end)
436 TC_PUNT(SHORT_UDP_HEADER);
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800437
438 // Skip handling of CHECKSUM_COMPLETE packets with udp checksum zero due to need for
439 // additional updating of skb->csum (this could be fixed up manually with more effort).
440 //
441 // Note that the in-kernel implementation of 'int64_t bpf_csum_update(skb, u32 csum)' is:
442 // if (skb->ip_summed == CHECKSUM_COMPLETE)
443 // return (skb->csum = csum_add(skb->csum, csum));
444 // else
445 // return -ENOTSUPP;
446 //
447 // So this will punt any CHECKSUM_COMPLETE packet with a zero UDP checksum,
448 // and leave all other packets unaffected (since it just at most adds zero to skb->csum).
449 //
450 // In practice this should almost never trigger because most nics do not generate
451 // CHECKSUM_COMPLETE packets on receive - especially so for nics/drivers on a phone.
452 //
453 // Additionally since we're forwarding, in most cases the value of the skb->csum field
454 // shouldn't matter (it's not used by physical nic egress).
455 //
456 // It only matters if we're ingressing through a CHECKSUM_COMPLETE capable nic
457 // and egressing through a virtual interface looping back to the kernel itself
458 // (ie. something like veth) where the CHECKSUM_COMPLETE/skb->csum can get reused
459 // on ingress.
460 //
461 // If we were in the kernel we'd simply probably call
462 // void skb_checksum_complete_unset(struct sk_buff *skb) {
463 // if (skb->ip_summed == CHECKSUM_COMPLETE) skb->ip_summed = CHECKSUM_NONE;
464 // }
465 // here instead. Perhaps there should be a bpf helper for that?
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800466 if (!udph->check && (bpf_csum_update(skb, 0) >= 0)) TC_PUNT(UDP_CSUM_ZERO);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800467 }
468
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800469 Tether4Key k = {
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800470 .iif = skb->ifindex,
471 .l4Proto = ip->protocol,
472 .src4.s_addr = ip->saddr,
473 .dst4.s_addr = ip->daddr,
474 .srcPort = is_tcp ? tcph->source : udph->source,
475 .dstPort = is_tcp ? tcph->dest : udph->dest,
476 };
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800477 if (is_ethernet) for (int i = 0; i < ETH_ALEN; ++i) k.dstMac[i] = eth->h_dest[i];
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800478
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800479 Tether4Value* v = downstream ? bpf_tether_downstream4_map_lookup_elem(&k)
480 : bpf_tether_upstream4_map_lookup_elem(&k);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800481
482 // If we don't find any offload information then simply let the core stack handle it...
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800483 if (!v) return TC_ACT_OK;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800484
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800485 uint32_t stat_and_limit_k = downstream ? skb->ifindex : v->oif;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800486
487 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
488
489 // If we don't have anywhere to put stats, then abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800490 if (!stat_v) TC_PUNT(NO_STATS_ENTRY);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800491
492 uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k);
493
494 // If we don't have a limit, then abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800495 if (!limit_v) TC_PUNT(NO_LIMIT_ENTRY);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800496
497 // Required IPv4 minimum mtu is 68, below that not clear what we should do, abort...
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800498 if (v->pmtu < 68) TC_PUNT(BELOW_IPV4_MTU);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800499
500 // Approximate handling of TCP/IPv4 overhead for incoming LRO/GRO packets: default
501 // outbound path mtu of 1500 is not necessarily correct, but worst case we simply
502 // undercount, which is still better then not accounting for this overhead at all.
503 // Note: this really shouldn't be device/path mtu at all, but rather should be
504 // derived from this particular connection's mss (ie. from gro segment size).
505 // This would require a much newer kernel with newer ebpf accessors.
506 // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header)
507 uint64_t packets = 1;
508 uint64_t bytes = skb->len;
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800509 if (bytes > v->pmtu) {
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800510 const int tcp_overhead = sizeof(struct iphdr) + sizeof(struct tcphdr) + 12;
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800511 const int mss = v->pmtu - tcp_overhead;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800512 const uint64_t payload = bytes - tcp_overhead;
513 packets = (payload + mss - 1) / mss;
514 bytes = tcp_overhead * packets + payload;
515 }
516
517 // Are we past the limit? If so, then abort...
518 // Note: will not overflow since u64 is 936 years even at 5Gbps.
519 // Do not drop here. Offload is just that, whenever we fail to handle
520 // a packet we let the core stack deal with things.
521 // (The core stack needs to handle limits correctly anyway,
522 // since we don't offload all traffic in both directions)
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800523 if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) TC_PUNT(LIMIT_REACHED);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800524
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800525 if (!is_ethernet) {
526 // Try to inject an ethernet header, and simply return if we fail.
527 // We do this even if TX interface is RAWIP and thus does not need an ethernet header,
528 // because this is easier and the kernel will strip extraneous ethernet header.
529 if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) {
530 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800531 TC_PUNT(CHANGE_HEAD_FAILED);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800532 }
533
534 // bpf_skb_change_head() invalidates all pointers - reload them
535 data = (void*)(long)skb->data;
536 data_end = (void*)(long)skb->data_end;
537 eth = data;
538 ip = (void*)(eth + 1);
539 tcph = is_tcp ? (void*)(ip + 1) : NULL;
540 udph = is_tcp ? NULL : (void*)(ip + 1);
541
542 // I do not believe this can ever happen, but keep the verifier happy...
543 if (data + sizeof(struct ethhdr) + sizeof(*ip) + (is_tcp ? sizeof(*tcph) : sizeof(*udph)) > data_end) {
544 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Maciej Żenczykowskie982f092021-02-04 20:26:26 -0800545 TC_DROP(TOO_SHORT);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800546 }
547 };
548
549 // At this point we always have an ethernet header - which will get stripped by the
550 // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid.
551 // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct.
552
553 // Overwrite any mac header with the new one
554 // For a rawip tx interface it will simply be a bunch of zeroes and later stripped.
555 *eth = v->macHeader;
556
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800557 const int l4_offs_csum = is_tcp ? ETH_IP4_TCP_OFFSET(check) : ETH_IP4_UDP_OFFSET(check);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800558 const int sz4 = sizeof(__be32);
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800559 // UDP 0 is special and stored as FFFF (this flag also causes a csum of 0 to be unmodified)
560 const int l4_flags = is_tcp ? 0 : BPF_F_MARK_MANGLED_0;
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800561 const __be32 old_daddr = k.dst4.s_addr;
562 const __be32 old_saddr = k.src4.s_addr;
563 const __be32 new_daddr = v->dst46.s6_addr32[3];
564 const __be32 new_saddr = v->src46.s6_addr32[3];
565
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800566 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 -0800567 bpf_l3_csum_replace(skb, ETH_IP4_OFFSET(check), old_daddr, new_daddr, sz4);
568 bpf_skb_store_bytes(skb, ETH_IP4_OFFSET(daddr), &new_daddr, sz4, 0);
569
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800570 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 -0800571 bpf_l3_csum_replace(skb, ETH_IP4_OFFSET(check), old_saddr, new_saddr, sz4);
572 bpf_skb_store_bytes(skb, ETH_IP4_OFFSET(saddr), &new_saddr, sz4, 0);
573
574 const int sz2 = sizeof(__be16);
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800575 // The offsets for TCP and UDP ports: source (u16 @ L4 offset 0) & dest (u16 @ L4 offset 2) are
576 // actually the same, so the compiler should just optimize them both down to a constant.
577 bpf_l4_csum_replace(skb, l4_offs_csum, k.srcPort, v->srcPort, sz2 | l4_flags);
578 bpf_skb_store_bytes(skb, is_tcp ? ETH_IP4_TCP_OFFSET(source) : ETH_IP4_UDP_OFFSET(source),
579 &v->srcPort, sz2, 0);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800580
Maciej Żenczykowskie4a726a2021-02-16 17:27:34 -0800581 bpf_l4_csum_replace(skb, l4_offs_csum, k.dstPort, v->dstPort, sz2 | l4_flags);
582 bpf_skb_store_bytes(skb, is_tcp ? ETH_IP4_TCP_OFFSET(dest) : ETH_IP4_UDP_OFFSET(dest),
583 &v->dstPort, sz2, 0);
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800584
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800585 // TEMP HACK: lack of TTL decrement
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800586
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800587 // This requires the bpf_ktime_get_boot_ns() helper which was added in 5.8,
588 // and backported to all Android Common Kernel 4.14+ trees.
Maciej Żenczykowskiaefa0952021-02-14 23:15:19 -0800589 if (updatetime) v->last_used = bpf_ktime_get_boot_ns();
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800590
591 __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800592 __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes);
593
Maciej Żenczykowskiec5f67d2021-01-25 02:32:01 -0800594 // Redirect to forwarded interface.
595 //
596 // Note that bpf_redirect() cannot fail unless you pass invalid flags.
597 // The redirect actually happens after the ebpf program has already terminated,
598 // and can fail for example for mtu reasons at that point in time, but there's nothing
599 // we can do about it here.
600 return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800601}
602
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800603// Full featured (required) implementations for 5.8+ kernels (these are S+ by definition)
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800604
Maciej Żenczykowskib0ac41f2021-02-14 21:34:03 -0800605DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_rawip$5_8", AID_ROOT, AID_NETWORK_STACK,
606 sched_cls_tether_downstream4_rawip_5_8, KVER(5, 8, 0))
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800607(struct __sk_buff* skb) {
Maciej Żenczykowskiaefa0952021-02-14 23:15:19 -0800608 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true, /* updatetime */ true);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800609}
610
Maciej Żenczykowskib0ac41f2021-02-14 21:34:03 -0800611DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_rawip$5_8", AID_ROOT, AID_NETWORK_STACK,
612 sched_cls_tether_upstream4_rawip_5_8, KVER(5, 8, 0))
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800613(struct __sk_buff* skb) {
Maciej Żenczykowskiaefa0952021-02-14 23:15:19 -0800614 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false, /* updatetime */ true);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800615}
616
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800617DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_ether$5_8", AID_ROOT, AID_NETWORK_STACK,
618 sched_cls_tether_downstream4_ether_5_8, KVER(5, 8, 0))
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800619(struct __sk_buff* skb) {
620 return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true, /* updatetime */ true);
621}
622
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800623DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_ether$5_8", AID_ROOT, AID_NETWORK_STACK,
624 sched_cls_tether_upstream4_ether_5_8, KVER(5, 8, 0))
625(struct __sk_buff* skb) {
626 return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false, /* updatetime */ true);
627}
628
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800629// Full featured (optional) implementations for 4.14-S, 4.19-S & 5.4-S kernels
630// (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 -0800631
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800632DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$opt",
633 AID_ROOT, AID_NETWORK_STACK,
634 sched_cls_tether_downstream4_rawip_opt,
635 KVER(4, 14, 0), KVER(5, 8, 0))
636(struct __sk_buff* skb) {
637 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true, /* updatetime */ true);
638}
639
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800640DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$opt",
641 AID_ROOT, AID_NETWORK_STACK,
642 sched_cls_tether_upstream4_rawip_opt,
643 KVER(4, 14, 0), KVER(5, 8, 0))
644(struct __sk_buff* skb) {
645 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false, /* updatetime */ true);
646}
647
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800648DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$opt",
649 AID_ROOT, AID_NETWORK_STACK,
650 sched_cls_tether_downstream4_ether_opt,
651 KVER(4, 14, 0), KVER(5, 8, 0))
652(struct __sk_buff* skb) {
653 return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true, /* updatetime */ true);
654}
655
656DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$opt",
657 AID_ROOT, AID_NETWORK_STACK,
658 sched_cls_tether_upstream4_ether_opt,
659 KVER(4, 14, 0), KVER(5, 8, 0))
660(struct __sk_buff* skb) {
661 return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false, /* updatetime */ true);
662}
663
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800664// Partial (TCP-only: will not update 'last_used' field) implementations for 4.14+ kernels.
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800665// These will be loaded only if the above optional ones failed (loading of *these* must succeed
666// for 5.4+, since that is always an R patched kernel).
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800667//
668// [Note: as a result TCP connections will not have their conntrack timeout refreshed, however,
669// since /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established defaults to 432000 (seconds),
670// this in practice means they'll break only after 5 days. This seems an acceptable trade-off.
671//
672// Additionally kernel/tests change "net-test: add bpf_ktime_get_ns / bpf_ktime_get_boot_ns tests"
673// which enforces and documents the required kernel cherrypicks will make it pretty unlikely that
674// many devices upgrading to S will end up relying on these fallback programs.
675
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800676// RAWIP: Required for 5.4-R kernels -- which always support bpf_skb_change_head().
677
678DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
679 sched_cls_tether_downstream4_rawip_5_4, KVER(5, 4, 0), KVER(5, 8, 0))
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800680(struct __sk_buff* skb) {
681 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true, /* updatetime */ false);
682}
683
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800684DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
685 sched_cls_tether_upstream4_rawip_5_4, KVER(5, 4, 0), KVER(5, 8, 0))
Maciej Żenczykowski36867352021-02-15 01:53:17 -0800686(struct __sk_buff* skb) {
687 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false, /* updatetime */ false);
688}
689
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800690// RAWIP: Optional for 4.14/4.19 (R) kernels -- which support bpf_skb_change_head().
691// [Note: fallback for 4.14/4.19 (P/Q) kernels is below in stub section]
692
693DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$4_14",
694 AID_ROOT, AID_NETWORK_STACK,
695 sched_cls_tether_downstream4_rawip_4_14,
696 KVER(4, 14, 0), KVER(5, 4, 0))
697(struct __sk_buff* skb) {
698 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true, /* updatetime */ false);
699}
700
701DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$4_14",
702 AID_ROOT, AID_NETWORK_STACK,
703 sched_cls_tether_upstream4_rawip_4_14,
704 KVER(4, 14, 0), KVER(5, 4, 0))
705(struct __sk_buff* skb) {
706 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false, /* updatetime */ false);
707}
708
709// ETHER: Required for 4.14-Q/R, 4.19-Q/R & 5.4-R kernels.
710
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800711DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$4_14", AID_ROOT, AID_NETWORK_STACK,
712 sched_cls_tether_downstream4_ether_4_14, KVER(4, 14, 0), KVER(5, 8, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800713(struct __sk_buff* skb) {
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800714 return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true, /* updatetime */ false);
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800715}
716
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800717DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$4_14", AID_ROOT, AID_NETWORK_STACK,
718 sched_cls_tether_upstream4_ether_4_14, KVER(4, 14, 0), KVER(5, 8, 0))
719(struct __sk_buff* skb) {
720 return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false, /* updatetime */ false);
721}
722
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800723// Placeholder (no-op) implementations for older Q kernels
724
725// 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 -0800726
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800727DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800728 sched_cls_tether_downstream4_rawip_stub, KVER_NONE, KVER(5, 4, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800729(struct __sk_buff* skb) {
730 return TC_ACT_OK;
731}
732
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800733DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800734 sched_cls_tether_upstream4_rawip_stub, KVER_NONE, KVER(5, 4, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800735(struct __sk_buff* skb) {
736 return TC_ACT_OK;
737}
738
Maciej Żenczykowskiacddd4f2021-03-09 21:43:48 -0800739// ETHER: 4.9-P/Q kernel
740
Maciej Żenczykowski2278aed2021-03-09 21:19:52 -0800741DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK,
742 sched_cls_tether_downstream4_ether_stub, KVER_NONE, KVER(4, 14, 0))
743(struct __sk_buff* skb) {
744 return TC_ACT_OK;
745}
746
747DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK,
748 sched_cls_tether_upstream4_ether_stub, KVER_NONE, KVER(4, 14, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800749(struct __sk_buff* skb) {
750 return TC_ACT_OK;
751}
752
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800753// ----- XDP Support -----
754
Maciej Żenczykowskidb2cff52021-03-01 21:22:49 -0800755DEFINE_BPF_MAP_GRW(tether_xdp_devmap, DEVMAP_HASH, uint32_t, uint32_t, 64,
756 AID_NETWORK_STACK)
757
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800758static inline __always_inline int do_xdp_forward6(struct xdp_md *ctx, const bool is_ethernet,
759 const bool downstream) {
760 return XDP_PASS;
761}
762
763static inline __always_inline int do_xdp_forward4(struct xdp_md *ctx, const bool is_ethernet,
764 const bool downstream) {
765 return XDP_PASS;
766}
767
768static inline __always_inline int do_xdp_forward_ether(struct xdp_md *ctx, const bool downstream) {
769 const void* data = (void*)(long)ctx->data;
770 const void* data_end = (void*)(long)ctx->data_end;
771 const struct ethhdr* eth = data;
772
773 // Make sure we actually have an ethernet header
774 if ((void*)(eth + 1) > data_end) return XDP_PASS;
775
776 if (eth->h_proto == htons(ETH_P_IPV6))
777 return do_xdp_forward6(ctx, /* is_ethernet */ true, downstream);
778 if (eth->h_proto == htons(ETH_P_IP))
779 return do_xdp_forward4(ctx, /* is_ethernet */ true, downstream);
780
781 // Anything else we don't know how to handle...
782 return XDP_PASS;
783}
784
785static inline __always_inline int do_xdp_forward_rawip(struct xdp_md *ctx, const bool downstream) {
786 const void* data = (void*)(long)ctx->data;
787 const void* data_end = (void*)(long)ctx->data_end;
788
789 // The top nibble of both IPv4 and IPv6 headers is the IP version.
790 if (data_end - data < 1) return XDP_PASS;
791 const uint8_t v = (*(uint8_t*)data) >> 4;
792
793 if (v == 6) return do_xdp_forward6(ctx, /* is_ethernet */ false, downstream);
794 if (v == 4) return do_xdp_forward4(ctx, /* is_ethernet */ false, downstream);
795
796 // Anything else we don't know how to handle...
797 return XDP_PASS;
798}
799
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800800#define DEFINE_XDP_PROG(str, func) \
801 DEFINE_BPF_PROG_KVER(str, AID_ROOT, AID_NETWORK_STACK, func, KVER(5, 9, 0))(struct xdp_md *ctx)
802
803DEFINE_XDP_PROG("xdp/tether_downstream_ether",
804 xdp_tether_downstream_ether) {
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800805 return do_xdp_forward_ether(ctx, /* downstream */ true);
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800806}
807
808DEFINE_XDP_PROG("xdp/tether_downstream_rawip",
809 xdp_tether_downstream_rawip) {
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800810 return do_xdp_forward_rawip(ctx, /* downstream */ true);
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800811}
812
813DEFINE_XDP_PROG("xdp/tether_upstream_ether",
814 xdp_tether_upstream_ether) {
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800815 return do_xdp_forward_ether(ctx, /* downstream */ false);
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800816}
817
818DEFINE_XDP_PROG("xdp/tether_upstream_rawip",
819 xdp_tether_upstream_rawip) {
Maciej Żenczykowski90b81ac2021-03-07 06:48:26 -0800820 return do_xdp_forward_rawip(ctx, /* downstream */ false);
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800821}
822
Hungming Chen56c632c2020-09-10 15:42:58 +0800823LICENSE("Apache 2.0");
Maciej Żenczykowski607d6dd2021-02-25 19:09:47 -0800824CRITICAL("tethering");