blob: 3a3291d6c9424e66e5a3f17c555ac5798bc8db75 [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"
29#include "netdbpf/bpf_shared.h"
30
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -080031// From kernel:include/net/ip.h
32#define IP_DF 0x4000 // Flag: "Don't Fragment"
33
Hungming Chen56c632c2020-09-10 15:42:58 +080034// Tethering stats, indexed by upstream interface.
Maciej Żenczykowski088fe192021-01-20 13:34:17 -080035DEFINE_BPF_MAP_GRW(tether_stats_map, HASH, TetherStatsKey, TetherStatsValue, 16, AID_NETWORK_STACK)
Hungming Chen56c632c2020-09-10 15:42:58 +080036
37// Tethering data limit, indexed by upstream interface.
38// (tethering allowed when stats[iif].rxBytes + stats[iif].txBytes < limit[iif])
Maciej Żenczykowski088fe192021-01-20 13:34:17 -080039DEFINE_BPF_MAP_GRW(tether_limit_map, HASH, TetherLimitKey, TetherLimitValue, 16, AID_NETWORK_STACK)
40
41// ----- IPv6 Support -----
42
43DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, TetherDownstream6Value, 64,
44 AID_NETWORK_STACK)
45
46DEFINE_BPF_MAP_GRW(tether_downstream64_map, HASH, TetherDownstream64Key, TetherDownstream64Value,
47 64, AID_NETWORK_STACK)
48
49DEFINE_BPF_MAP_GRW(tether_upstream6_map, HASH, TetherUpstream6Key, TetherUpstream6Value, 64,
50 AID_NETWORK_STACK)
Hungming Chen56c632c2020-09-10 15:42:58 +080051
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -080052static inline __always_inline int do_forward6(struct __sk_buff* skb, const bool is_ethernet,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080053 const bool downstream) {
54 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
Hungming Chen56c632c2020-09-10 15:42:58 +080055 void* data = (void*)(long)skb->data;
56 const void* data_end = (void*)(long)skb->data_end;
57 struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet
58 struct ipv6hdr* ip6 = is_ethernet ? (void*)(eth + 1) : data;
59
Maciej Żenczykowski18552e82021-01-24 19:59:05 -080060 // Require ethernet dst mac address to be our unicast address.
61 if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_OK;
62
Hungming Chen56c632c2020-09-10 15:42:58 +080063 // Must be meta-ethernet IPv6 frame
64 if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_OK;
65
66 // Must have (ethernet and) ipv6 header
67 if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_OK;
68
69 // Ethertype - if present - must be IPv6
70 if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_OK;
71
72 // IP version must be 6
73 if (ip6->version != 6) return TC_ACT_OK;
74
75 // Cannot decrement during forward if already zero or would be zero,
76 // Let the kernel's stack handle these cases and generate appropriate ICMP errors.
77 if (ip6->hop_limit <= 1) return TC_ACT_OK;
78
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -080079 // If hardware offload is running and programming flows based on conntrack entries,
80 // try not to interfere with it.
81 if (ip6->nexthdr == IPPROTO_TCP) {
82 struct tcphdr* tcph = (void*)(ip6 + 1);
83
84 // Make sure we can get at the tcp header
85 if (data + l2_header_size + sizeof(*ip6) + sizeof(*tcph) > data_end) return TC_ACT_OK;
86
87 // Do not offload TCP packets with any one of the SYN/FIN/RST flags
88 if (tcph->syn || tcph->fin || tcph->rst) return TC_ACT_OK;
89 }
90
Hungming Chen56c632c2020-09-10 15:42:58 +080091 // Protect against forwarding packets sourced from ::1 or fe80::/64 or other weirdness.
92 __be32 src32 = ip6->saddr.s6_addr32[0];
93 if (src32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
94 (src32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
95 return TC_ACT_OK;
96
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080097 // Protect against forwarding packets destined to ::1 or fe80::/64 or other weirdness.
98 __be32 dst32 = ip6->daddr.s6_addr32[0];
99 if (dst32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
100 (dst32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
101 return TC_ACT_OK;
102
103 // In the upstream direction do not forward traffic within the same /64 subnet.
104 if (!downstream && (src32 == dst32) && (ip6->saddr.s6_addr32[1] == ip6->daddr.s6_addr32[1]))
105 return TC_ACT_OK;
106
107 TetherDownstream6Key kd = {
Hungming Chen56c632c2020-09-10 15:42:58 +0800108 .iif = skb->ifindex,
109 .neigh6 = ip6->daddr,
110 };
111
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800112 TetherUpstream6Key ku = {
113 .iif = skb->ifindex,
114 };
115
116 TetherDownstream6Value* vd = downstream ? bpf_tether_downstream6_map_lookup_elem(&kd) : NULL;
117 TetherUpstream6Value* vu = downstream ? NULL : bpf_tether_upstream6_map_lookup_elem(&ku);
Hungming Chen56c632c2020-09-10 15:42:58 +0800118
119 // If we don't find any offload information then simply let the core stack handle it...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800120 if (downstream && !vd) return TC_ACT_OK;
121 if (!downstream && !vu) return TC_ACT_OK;
Hungming Chen56c632c2020-09-10 15:42:58 +0800122
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800123 uint32_t stat_and_limit_k = downstream ? skb->ifindex : vu->oif;
Hungming Chen56c632c2020-09-10 15:42:58 +0800124
125 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
126
127 // If we don't have anywhere to put stats, then abort...
128 if (!stat_v) return TC_ACT_OK;
129
130 uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k);
131
132 // If we don't have a limit, then abort...
133 if (!limit_v) return TC_ACT_OK;
134
135 // Required IPv6 minimum mtu is 1280, below that not clear what we should do, abort...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800136 const int pmtu = downstream ? vd->pmtu : vu->pmtu;
Hungming Chen56c632c2020-09-10 15:42:58 +0800137 if (pmtu < IPV6_MIN_MTU) return TC_ACT_OK;
138
139 // Approximate handling of TCP/IPv6 overhead for incoming LRO/GRO packets: default
140 // outbound path mtu of 1500 is not necessarily correct, but worst case we simply
141 // undercount, which is still better then not accounting for this overhead at all.
142 // Note: this really shouldn't be device/path mtu at all, but rather should be
143 // derived from this particular connection's mss (ie. from gro segment size).
144 // This would require a much newer kernel with newer ebpf accessors.
145 // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header)
146 uint64_t packets = 1;
147 uint64_t bytes = skb->len;
148 if (bytes > pmtu) {
149 const int tcp_overhead = sizeof(struct ipv6hdr) + sizeof(struct tcphdr) + 12;
150 const int mss = pmtu - tcp_overhead;
151 const uint64_t payload = bytes - tcp_overhead;
152 packets = (payload + mss - 1) / mss;
153 bytes = tcp_overhead * packets + payload;
154 }
155
156 // Are we past the limit? If so, then abort...
157 // Note: will not overflow since u64 is 936 years even at 5Gbps.
158 // Do not drop here. Offload is just that, whenever we fail to handle
159 // a packet we let the core stack deal with things.
160 // (The core stack needs to handle limits correctly anyway,
161 // since we don't offload all traffic in both directions)
162 if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) return TC_ACT_OK;
163
164 if (!is_ethernet) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800165 // Try to inject an ethernet header, and simply return if we fail.
166 // We do this even if TX interface is RAWIP and thus does not need an ethernet header,
167 // because this is easier and the kernel will strip extraneous ethernet header.
168 if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) {
169 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Hungming Chen56c632c2020-09-10 15:42:58 +0800170 return TC_ACT_OK;
171 }
172
173 // bpf_skb_change_head() invalidates all pointers - reload them
174 data = (void*)(long)skb->data;
175 data_end = (void*)(long)skb->data_end;
176 eth = data;
177 ip6 = (void*)(eth + 1);
178
179 // I do not believe this can ever happen, but keep the verifier happy...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800180 if (data + sizeof(struct ethhdr) + sizeof(*ip6) > data_end) {
181 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Hungming Chen56c632c2020-09-10 15:42:58 +0800182 return TC_ACT_SHOT;
183 }
184 };
185
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800186 // At this point we always have an ethernet header - which will get stripped by the
187 // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid.
188 // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct.
189
Hungming Chen56c632c2020-09-10 15:42:58 +0800190 // CHECKSUM_COMPLETE is a 16-bit one's complement sum,
191 // thus corrections for it need to be done in 16-byte chunks at even offsets.
192 // IPv6 nexthdr is at offset 6, while hop limit is at offset 7
193 uint8_t old_hl = ip6->hop_limit;
194 --ip6->hop_limit;
195 uint8_t new_hl = ip6->hop_limit;
196
197 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
198 // (-ENOTSUPP) if it isn't.
199 bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl));
200
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800201 __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets);
202 __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes);
Hungming Chen56c632c2020-09-10 15:42:58 +0800203
204 // Overwrite any mac header with the new one
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800205 // For a rawip tx interface it will simply be a bunch of zeroes and later stripped.
206 *eth = downstream ? vd->macHeader : vu->macHeader;
Hungming Chen56c632c2020-09-10 15:42:58 +0800207
208 // Redirect to forwarded interface.
209 //
210 // Note that bpf_redirect() cannot fail unless you pass invalid flags.
211 // The redirect actually happens after the ebpf program has already terminated,
212 // and can fail for example for mtu reasons at that point in time, but there's nothing
213 // we can do about it here.
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800214 return bpf_redirect(downstream ? vd->oif : vu->oif, 0 /* this is effectively BPF_F_EGRESS */);
Hungming Chen56c632c2020-09-10 15:42:58 +0800215}
216
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800217DEFINE_BPF_PROG("schedcls/tether_downstream6_ether", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800218 sched_cls_tether_downstream6_ether)
Maciej Żenczykowski6b7829f2021-01-18 00:03:37 -0800219(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800220 return do_forward6(skb, /* is_ethernet */ true, /* downstream */ true);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800221}
222
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800223DEFINE_BPF_PROG("schedcls/tether_upstream6_ether", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800224 sched_cls_tether_upstream6_ether)
225(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800226 return do_forward6(skb, /* is_ethernet */ true, /* downstream */ false);
Hungming Chen56c632c2020-09-10 15:42:58 +0800227}
228
229// Note: section names must be unique to prevent programs from appending to each other,
230// so instead the bpf loader will strip everything past the final $ symbol when actually
231// pinning the program into the filesystem.
232//
233// bpf_skb_change_head() is only present on 4.14+ and 2 trivial kernel patches are needed:
234// ANDROID: net: bpf: Allow TC programs to call BPF_FUNC_skb_change_head
235// ANDROID: net: bpf: permit redirect from ingress L3 to egress L2 devices at near max mtu
236// (the first of those has already been upstreamed)
237//
238// 5.4 kernel support was only added to Android Common Kernel in R,
239// and thus a 5.4 kernel always supports this.
240//
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800241// Hence, these mandatory (must load successfully) implementations for 5.4+ kernels:
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800242DEFINE_BPF_PROG_KVER("schedcls/tether_downstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800243 sched_cls_tether_downstream6_rawip_5_4, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800244(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800245 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800246}
247
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800248DEFINE_BPF_PROG_KVER("schedcls/tether_upstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800249 sched_cls_tether_upstream6_rawip_5_4, KVER(5, 4, 0))
250(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800251 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800252}
253
254// and these identical optional (may fail to load) implementations for [4.14..5.4) patched kernels:
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800255DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$4_14",
256 AID_ROOT, AID_NETWORK_STACK,
257 sched_cls_tether_downstream6_rawip_4_14,
258 KVER(4, 14, 0), KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800259(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800260 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800261}
262
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800263DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$4_14",
264 AID_ROOT, AID_NETWORK_STACK,
265 sched_cls_tether_upstream6_rawip_4_14,
266 KVER(4, 14, 0), KVER(5, 4, 0))
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800267(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800268 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800269}
270
271// and define no-op stubs for [4.9,4.14) and unpatched [4.14,5.4) kernels.
Hungming Chen56c632c2020-09-10 15:42:58 +0800272// (if the above real 4.14+ program loaded successfully, then bpfloader will have already pinned
273// 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 -0800274DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800275 sched_cls_tether_downstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800276(struct __sk_buff* skb) {
277 return TC_ACT_OK;
278}
279
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800280DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800281 sched_cls_tether_upstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
282(struct __sk_buff* skb) {
283 return TC_ACT_OK;
284}
285
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800286// ----- IPv4 Support -----
287
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800288DEFINE_BPF_MAP_GRW(tether_downstream4_map, HASH, Tether4Key, Tether4Value, 64, AID_NETWORK_STACK)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800289
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800290DEFINE_BPF_MAP_GRW(tether_upstream4_map, HASH, Tether4Key, Tether4Value, 64, AID_NETWORK_STACK)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800291
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800292static inline __always_inline int do_forward4(struct __sk_buff* skb, const bool is_ethernet,
293 const bool downstream) {
294 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
295 void* data = (void*)(long)skb->data;
296 const void* data_end = (void*)(long)skb->data_end;
297 struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet
298 struct iphdr* ip = is_ethernet ? (void*)(eth + 1) : data;
299
300 // Require ethernet dst mac address to be our unicast address.
301 if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_OK;
302
303 // Must be meta-ethernet IPv4 frame
304 if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_OK;
305
306 // Must have (ethernet and) ipv4 header
307 if (data + l2_header_size + sizeof(*ip) > data_end) return TC_ACT_OK;
308
309 // Ethertype - if present - must be IPv4
310 if (is_ethernet && (eth->h_proto != htons(ETH_P_IP))) return TC_ACT_OK;
311
312 // IP version must be 4
313 if (ip->version != 4) return TC_ACT_OK;
314
315 // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header
316 if (ip->ihl != 5) return TC_ACT_OK;
317
318 // Calculate the IPv4 one's complement checksum of the IPv4 header.
319 __wsum sum4 = 0;
320 for (int i = 0; i < sizeof(*ip) / sizeof(__u16); ++i) {
321 sum4 += ((__u16*)ip)[i];
322 }
323 // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4
324 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE
325 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16
326 // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF
327 if (sum4 != 0xFFFF) return TC_ACT_OK;
328
329 // Minimum IPv4 total length is the size of the header
330 if (ntohs(ip->tot_len) < sizeof(*ip)) return TC_ACT_OK;
331
332 // We are incapable of dealing with IPv4 fragments
333 if (ip->frag_off & ~htons(IP_DF)) return TC_ACT_OK;
334
335 // Cannot decrement during forward if already zero or would be zero,
336 // Let the kernel's stack handle these cases and generate appropriate ICMP errors.
337 if (ip->ttl <= 1) return TC_ACT_OK;
338
339 const bool is_tcp = (ip->protocol == IPPROTO_TCP);
340
341 // We do not support anything besides TCP and UDP
342 if (!is_tcp && (ip->protocol != IPPROTO_UDP)) return TC_ACT_OK;
343
344 struct tcphdr* tcph = is_tcp ? (void*)(ip + 1) : NULL;
345 struct udphdr* udph = is_tcp ? NULL : (void*)(ip + 1);
346
347 if (is_tcp) {
348 // Make sure we can get at the tcp header
349 if (data + l2_header_size + sizeof(*ip) + sizeof(*tcph) > data_end) return TC_ACT_OK;
350
351 // If hardware offload is running and programming flows based on conntrack entries, try not
352 // to interfere with it, so do not offload TCP packets with any one of the SYN/FIN/RST flags
353 if (tcph->syn || tcph->fin || tcph->rst) return TC_ACT_OK;
354 } else { // UDP
355 // Make sure we can get at the udp header
356 if (data + l2_header_size + sizeof(*ip) + sizeof(*udph) > data_end) return TC_ACT_OK;
357 }
358
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800359 Tether4Key k = {
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800360 .iif = skb->ifindex,
361 .l4Proto = ip->protocol,
362 .src4.s_addr = ip->saddr,
363 .dst4.s_addr = ip->daddr,
364 .srcPort = is_tcp ? tcph->source : udph->source,
365 .dstPort = is_tcp ? tcph->dest : udph->dest,
366 };
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800367 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 -0800368
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800369 Tether4Value* v = downstream ? bpf_tether_downstream4_map_lookup_elem(&k)
370 : bpf_tether_upstream4_map_lookup_elem(&k);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800371
372 // If we don't find any offload information then simply let the core stack handle it...
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800373 if (!v) return TC_ACT_OK;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800374
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800375 uint32_t stat_and_limit_k = downstream ? skb->ifindex : v->oif;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800376
377 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
378
379 // If we don't have anywhere to put stats, then abort...
380 if (!stat_v) return TC_ACT_OK;
381
382 uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k);
383
384 // If we don't have a limit, then abort...
385 if (!limit_v) return TC_ACT_OK;
386
387 // Required IPv4 minimum mtu is 68, below that not clear what we should do, abort...
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800388 if (v->pmtu < 68) return TC_ACT_OK;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800389
390 // Approximate handling of TCP/IPv4 overhead for incoming LRO/GRO packets: default
391 // outbound path mtu of 1500 is not necessarily correct, but worst case we simply
392 // undercount, which is still better then not accounting for this overhead at all.
393 // Note: this really shouldn't be device/path mtu at all, but rather should be
394 // derived from this particular connection's mss (ie. from gro segment size).
395 // This would require a much newer kernel with newer ebpf accessors.
396 // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header)
397 uint64_t packets = 1;
398 uint64_t bytes = skb->len;
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800399 if (bytes > v->pmtu) {
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800400 const int tcp_overhead = sizeof(struct iphdr) + sizeof(struct tcphdr) + 12;
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800401 const int mss = v->pmtu - tcp_overhead;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800402 const uint64_t payload = bytes - tcp_overhead;
403 packets = (payload + mss - 1) / mss;
404 bytes = tcp_overhead * packets + payload;
405 }
406
407 // Are we past the limit? If so, then abort...
408 // Note: will not overflow since u64 is 936 years even at 5Gbps.
409 // Do not drop here. Offload is just that, whenever we fail to handle
410 // a packet we let the core stack deal with things.
411 // (The core stack needs to handle limits correctly anyway,
412 // since we don't offload all traffic in both directions)
413 if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) return TC_ACT_OK;
414
415 // TODO: replace Errors with Packets once implemented
416 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, packets);
417 __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes);
418
419 // TODO: not actually implemented yet
420 return TC_ACT_OK;
421}
422
423// Real implementations for 5.9+ kernels
424
425DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_ether$5_9", AID_ROOT, AID_NETWORK_STACK,
426 sched_cls_tether_downstream4_ether_5_9, KVER(5, 9, 0))
427(struct __sk_buff* skb) {
428 return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true);
429}
430
431DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_rawip$5_9", AID_ROOT, AID_NETWORK_STACK,
432 sched_cls_tether_downstream4_rawip_5_9, KVER(5, 9, 0))
433(struct __sk_buff* skb) {
434 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true);
435}
436
437DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_ether$5_9", AID_ROOT, AID_NETWORK_STACK,
438 sched_cls_tether_upstream4_ether_5_9, KVER(5, 9, 0))
439(struct __sk_buff* skb) {
440 return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false);
441}
442
443DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_rawip$5_9", AID_ROOT, AID_NETWORK_STACK,
444 sched_cls_tether_upstream4_rawip_5_9, KVER(5, 9, 0))
445(struct __sk_buff* skb) {
446 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false);
447}
448
449// Placeholder implementations for older pre-5.9 kernels
450
451DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK,
452 sched_cls_tether_downstream4_ether_stub, KVER_NONE, KVER(5, 9, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800453(struct __sk_buff* skb) {
454 return TC_ACT_OK;
455}
456
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800457DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
458 sched_cls_tether_downstream4_rawip_stub, KVER_NONE, KVER(5, 9, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800459(struct __sk_buff* skb) {
460 return TC_ACT_OK;
461}
462
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800463DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK,
464 sched_cls_tether_upstream4_ether_stub, KVER_NONE, KVER(5, 9, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800465(struct __sk_buff* skb) {
466 return TC_ACT_OK;
467}
468
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800469DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
470 sched_cls_tether_upstream4_rawip_stub, KVER_NONE, KVER(5, 9, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800471(struct __sk_buff* skb) {
472 return TC_ACT_OK;
473}
474
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800475// ----- XDP Support -----
476
477#define DEFINE_XDP_PROG(str, func) \
478 DEFINE_BPF_PROG_KVER(str, AID_ROOT, AID_NETWORK_STACK, func, KVER(5, 9, 0))(struct xdp_md *ctx)
479
480DEFINE_XDP_PROG("xdp/tether_downstream_ether",
481 xdp_tether_downstream_ether) {
482 return XDP_PASS;
483}
484
485DEFINE_XDP_PROG("xdp/tether_downstream_rawip",
486 xdp_tether_downstream_rawip) {
487 return XDP_PASS;
488}
489
490DEFINE_XDP_PROG("xdp/tether_upstream_ether",
491 xdp_tether_upstream_ether) {
492 return XDP_PASS;
493}
494
495DEFINE_XDP_PROG("xdp/tether_upstream_rawip",
496 xdp_tether_upstream_rawip) {
497 return XDP_PASS;
498}
499
Hungming Chen56c632c2020-09-10 15:42:58 +0800500LICENSE("Apache 2.0");
501CRITICAL("netd");