blob: 754c841001224b6da1e560bb8a865bfc6c419b72 [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#include "netdbpf/bpf_shared.h"
31
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -080032// From kernel:include/net/ip.h
33#define IP_DF 0x4000 // Flag: "Don't Fragment"
34
Hungming Chen56c632c2020-09-10 15:42:58 +080035// Tethering stats, indexed by upstream interface.
Maciej Żenczykowski088fe192021-01-20 13:34:17 -080036DEFINE_BPF_MAP_GRW(tether_stats_map, HASH, TetherStatsKey, TetherStatsValue, 16, AID_NETWORK_STACK)
Hungming Chen56c632c2020-09-10 15:42:58 +080037
38// Tethering data limit, indexed by upstream interface.
39// (tethering allowed when stats[iif].rxBytes + stats[iif].txBytes < limit[iif])
Maciej Żenczykowski088fe192021-01-20 13:34:17 -080040DEFINE_BPF_MAP_GRW(tether_limit_map, HASH, TetherLimitKey, TetherLimitValue, 16, AID_NETWORK_STACK)
41
42// ----- IPv6 Support -----
43
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -080044DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 64,
Maciej Żenczykowski088fe192021-01-20 13:34:17 -080045 AID_NETWORK_STACK)
46
47DEFINE_BPF_MAP_GRW(tether_downstream64_map, HASH, TetherDownstream64Key, TetherDownstream64Value,
48 64, AID_NETWORK_STACK)
49
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -080050DEFINE_BPF_MAP_GRW(tether_upstream6_map, HASH, TetherUpstream6Key, Tether6Value, 64,
Maciej Żenczykowski088fe192021-01-20 13:34:17 -080051 AID_NETWORK_STACK)
Hungming Chen56c632c2020-09-10 15:42:58 +080052
Lorenzo Colittib81584d2021-02-06 00:00:58 +090053DEFINE_BPF_MAP_GRW(tether_error_map, ARRAY, __u32, __u32, BPF_TETHER_ERR__MAX,
54 AID_NETWORK_STACK)
55
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +090056#define COUNT_AND_RETURN(counter, ret) do { \
57 __u32 code = BPF_TETHER_ERR_ ## counter; \
58 __u32 *count = bpf_tether_error_map_lookup_elem(&code); \
59 if (count) __sync_fetch_and_add(count, 1); \
60 return ret; \
Lorenzo Colittib81584d2021-02-06 00:00:58 +090061} while(0)
62
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +090063#define DROP(counter) COUNT_AND_RETURN(counter, TC_ACT_SHOT)
64#define PUNT(counter) COUNT_AND_RETURN(counter, TC_ACT_OK)
65
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -080066static inline __always_inline int do_forward6(struct __sk_buff* skb, const bool is_ethernet,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -080067 const bool downstream) {
68 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
Hungming Chen56c632c2020-09-10 15:42:58 +080069 void* data = (void*)(long)skb->data;
70 const void* data_end = (void*)(long)skb->data_end;
71 struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet
72 struct ipv6hdr* ip6 = is_ethernet ? (void*)(eth + 1) : data;
73
Maciej Żenczykowski18552e82021-01-24 19:59:05 -080074 // Require ethernet dst mac address to be our unicast address.
75 if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_OK;
76
Hungming Chen56c632c2020-09-10 15:42:58 +080077 // Must be meta-ethernet IPv6 frame
78 if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_OK;
79
80 // Must have (ethernet and) ipv6 header
81 if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_OK;
82
83 // Ethertype - if present - must be IPv6
84 if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_OK;
85
86 // IP version must be 6
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +090087 if (ip6->version != 6) PUNT(INVALID_IP_VERSION);
Hungming Chen56c632c2020-09-10 15:42:58 +080088
89 // Cannot decrement during forward if already zero or would be zero,
90 // Let the kernel's stack handle these cases and generate appropriate ICMP errors.
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +090091 if (ip6->hop_limit <= 1) PUNT(LOW_TTL);
Hungming Chen56c632c2020-09-10 15:42:58 +080092
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -080093 // If hardware offload is running and programming flows based on conntrack entries,
94 // try not to interfere with it.
95 if (ip6->nexthdr == IPPROTO_TCP) {
96 struct tcphdr* tcph = (void*)(ip6 + 1);
97
98 // Make sure we can get at the tcp header
Lorenzo Colittib81584d2021-02-06 00:00:58 +090099 if (data + l2_header_size + sizeof(*ip6) + sizeof(*tcph) > data_end)
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +0900100 PUNT(INVALID_TCP_HEADER);
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -0800101
102 // Do not offload TCP packets with any one of the SYN/FIN/RST flags
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +0900103 if (tcph->syn || tcph->fin || tcph->rst) PUNT(TCP_CONTROL_PACKET);
Maciej Żenczykowskifc4f6542021-01-22 22:19:45 -0800104 }
105
Hungming Chen56c632c2020-09-10 15:42:58 +0800106 // Protect against forwarding packets sourced from ::1 or fe80::/64 or other weirdness.
107 __be32 src32 = ip6->saddr.s6_addr32[0];
108 if (src32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
109 (src32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +0900110 PUNT(NON_GLOBAL_SRC);
Hungming Chen56c632c2020-09-10 15:42:58 +0800111
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800112 // Protect against forwarding packets destined to ::1 or fe80::/64 or other weirdness.
113 __be32 dst32 = ip6->daddr.s6_addr32[0];
114 if (dst32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP
115 (dst32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +0900116 PUNT(NON_GLOBAL_DST);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800117
118 // In the upstream direction do not forward traffic within the same /64 subnet.
119 if (!downstream && (src32 == dst32) && (ip6->saddr.s6_addr32[1] == ip6->daddr.s6_addr32[1]))
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +0900120 PUNT(LOCAL_SRC_DST);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800121
122 TetherDownstream6Key kd = {
Hungming Chen56c632c2020-09-10 15:42:58 +0800123 .iif = skb->ifindex,
124 .neigh6 = ip6->daddr,
125 };
126
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800127 TetherUpstream6Key ku = {
128 .iif = skb->ifindex,
129 };
130
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800131 Tether6Value* v = downstream ? bpf_tether_downstream6_map_lookup_elem(&kd)
132 : bpf_tether_upstream6_map_lookup_elem(&ku);
Hungming Chen56c632c2020-09-10 15:42:58 +0800133
134 // If we don't find any offload information then simply let the core stack handle it...
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800135 if (!v) return TC_ACT_OK;
Hungming Chen56c632c2020-09-10 15:42:58 +0800136
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800137 uint32_t stat_and_limit_k = downstream ? skb->ifindex : v->oif;
Hungming Chen56c632c2020-09-10 15:42:58 +0800138
139 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
140
141 // If we don't have anywhere to put stats, then abort...
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +0900142 if (!stat_v) PUNT(NO_STATS_ENTRY);
Hungming Chen56c632c2020-09-10 15:42:58 +0800143
144 uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k);
145
146 // If we don't have a limit, then abort...
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +0900147 if (!limit_v) PUNT(NO_LIMIT_ENTRY);
Hungming Chen56c632c2020-09-10 15:42:58 +0800148
149 // Required IPv6 minimum mtu is 1280, below that not clear what we should do, abort...
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +0900150 if (v->pmtu < IPV6_MIN_MTU) PUNT(BELOW_IPV6_MTU);
Hungming Chen56c632c2020-09-10 15:42:58 +0800151
152 // Approximate handling of TCP/IPv6 overhead for incoming LRO/GRO packets: default
153 // outbound path mtu of 1500 is not necessarily correct, but worst case we simply
154 // undercount, which is still better then not accounting for this overhead at all.
155 // Note: this really shouldn't be device/path mtu at all, but rather should be
156 // derived from this particular connection's mss (ie. from gro segment size).
157 // This would require a much newer kernel with newer ebpf accessors.
158 // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header)
159 uint64_t packets = 1;
160 uint64_t bytes = skb->len;
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800161 if (bytes > v->pmtu) {
Hungming Chen56c632c2020-09-10 15:42:58 +0800162 const int tcp_overhead = sizeof(struct ipv6hdr) + sizeof(struct tcphdr) + 12;
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800163 const int mss = v->pmtu - tcp_overhead;
Hungming Chen56c632c2020-09-10 15:42:58 +0800164 const uint64_t payload = bytes - tcp_overhead;
165 packets = (payload + mss - 1) / mss;
166 bytes = tcp_overhead * packets + payload;
167 }
168
169 // Are we past the limit? If so, then abort...
170 // Note: will not overflow since u64 is 936 years even at 5Gbps.
171 // Do not drop here. Offload is just that, whenever we fail to handle
172 // a packet we let the core stack deal with things.
173 // (The core stack needs to handle limits correctly anyway,
174 // since we don't offload all traffic in both directions)
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +0900175 if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) PUNT(LIMIT_REACHED);
Hungming Chen56c632c2020-09-10 15:42:58 +0800176
177 if (!is_ethernet) {
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800178 // Try to inject an ethernet header, and simply return if we fail.
179 // We do this even if TX interface is RAWIP and thus does not need an ethernet header,
180 // because this is easier and the kernel will strip extraneous ethernet header.
181 if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) {
182 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +0900183 PUNT(CHANGE_HEAD_FAILED);
Hungming Chen56c632c2020-09-10 15:42:58 +0800184 }
185
186 // bpf_skb_change_head() invalidates all pointers - reload them
187 data = (void*)(long)skb->data;
188 data_end = (void*)(long)skb->data_end;
189 eth = data;
190 ip6 = (void*)(eth + 1);
191
192 // I do not believe this can ever happen, but keep the verifier happy...
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800193 if (data + sizeof(struct ethhdr) + sizeof(*ip6) > data_end) {
194 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1);
Lorenzo Colitti72ec3ba2021-02-09 11:47:46 +0900195 DROP(TOO_SHORT);
Hungming Chen56c632c2020-09-10 15:42:58 +0800196 }
197 };
198
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800199 // At this point we always have an ethernet header - which will get stripped by the
200 // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid.
201 // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct.
202
Hungming Chen56c632c2020-09-10 15:42:58 +0800203 // CHECKSUM_COMPLETE is a 16-bit one's complement sum,
204 // thus corrections for it need to be done in 16-byte chunks at even offsets.
205 // IPv6 nexthdr is at offset 6, while hop limit is at offset 7
206 uint8_t old_hl = ip6->hop_limit;
207 --ip6->hop_limit;
208 uint8_t new_hl = ip6->hop_limit;
209
210 // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error
211 // (-ENOTSUPP) if it isn't.
212 bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl));
213
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800214 __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets);
215 __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes);
Hungming Chen56c632c2020-09-10 15:42:58 +0800216
217 // Overwrite any mac header with the new one
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800218 // For a rawip tx interface it will simply be a bunch of zeroes and later stripped.
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800219 *eth = v->macHeader;
Hungming Chen56c632c2020-09-10 15:42:58 +0800220
221 // Redirect to forwarded interface.
222 //
223 // Note that bpf_redirect() cannot fail unless you pass invalid flags.
224 // The redirect actually happens after the ebpf program has already terminated,
225 // and can fail for example for mtu reasons at that point in time, but there's nothing
226 // we can do about it here.
Maciej Żenczykowski7dfbcf52021-01-26 16:08:57 -0800227 return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */);
Hungming Chen56c632c2020-09-10 15:42:58 +0800228}
229
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800230DEFINE_BPF_PROG("schedcls/tether_downstream6_ether", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800231 sched_cls_tether_downstream6_ether)
Maciej Żenczykowski6b7829f2021-01-18 00:03:37 -0800232(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800233 return do_forward6(skb, /* is_ethernet */ true, /* downstream */ true);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800234}
235
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800236DEFINE_BPF_PROG("schedcls/tether_upstream6_ether", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800237 sched_cls_tether_upstream6_ether)
238(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800239 return do_forward6(skb, /* is_ethernet */ true, /* downstream */ false);
Hungming Chen56c632c2020-09-10 15:42:58 +0800240}
241
242// Note: section names must be unique to prevent programs from appending to each other,
243// so instead the bpf loader will strip everything past the final $ symbol when actually
244// pinning the program into the filesystem.
245//
246// bpf_skb_change_head() is only present on 4.14+ and 2 trivial kernel patches are needed:
247// ANDROID: net: bpf: Allow TC programs to call BPF_FUNC_skb_change_head
248// ANDROID: net: bpf: permit redirect from ingress L3 to egress L2 devices at near max mtu
249// (the first of those has already been upstreamed)
250//
251// 5.4 kernel support was only added to Android Common Kernel in R,
252// and thus a 5.4 kernel always supports this.
253//
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800254// Hence, these mandatory (must load successfully) implementations for 5.4+ kernels:
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800255DEFINE_BPF_PROG_KVER("schedcls/tether_downstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800256 sched_cls_tether_downstream6_rawip_5_4, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800257(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800258 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800259}
260
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800261DEFINE_BPF_PROG_KVER("schedcls/tether_upstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800262 sched_cls_tether_upstream6_rawip_5_4, KVER(5, 4, 0))
263(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800264 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800265}
266
267// and these identical optional (may fail to load) implementations for [4.14..5.4) patched kernels:
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800268DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$4_14",
269 AID_ROOT, AID_NETWORK_STACK,
270 sched_cls_tether_downstream6_rawip_4_14,
271 KVER(4, 14, 0), KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800272(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800273 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true);
Hungming Chen56c632c2020-09-10 15:42:58 +0800274}
275
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800276DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$4_14",
277 AID_ROOT, AID_NETWORK_STACK,
278 sched_cls_tether_upstream6_rawip_4_14,
279 KVER(4, 14, 0), KVER(5, 4, 0))
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800280(struct __sk_buff* skb) {
Maciej Żenczykowskibf8ec1a2021-01-24 19:56:39 -0800281 return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false);
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800282}
283
284// and define no-op stubs for [4.9,4.14) and unpatched [4.14,5.4) kernels.
Hungming Chen56c632c2020-09-10 15:42:58 +0800285// (if the above real 4.14+ program loaded successfully, then bpfloader will have already pinned
286// 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 -0800287DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowski770e0a72021-01-18 20:14:03 -0800288 sched_cls_tether_downstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
Hungming Chen56c632c2020-09-10 15:42:58 +0800289(struct __sk_buff* skb) {
290 return TC_ACT_OK;
291}
292
Maciej Żenczykowski5b00fbd2021-01-19 23:37:51 -0800293DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
Maciej Żenczykowskibca0c852021-01-19 01:22:17 -0800294 sched_cls_tether_upstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0))
295(struct __sk_buff* skb) {
296 return TC_ACT_OK;
297}
298
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800299// ----- IPv4 Support -----
300
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800301DEFINE_BPF_MAP_GRW(tether_downstream4_map, HASH, Tether4Key, Tether4Value, 64, AID_NETWORK_STACK)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800302
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800303DEFINE_BPF_MAP_GRW(tether_upstream4_map, HASH, Tether4Key, Tether4Value, 64, AID_NETWORK_STACK)
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800304
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800305static inline __always_inline int do_forward4(struct __sk_buff* skb, const bool is_ethernet,
306 const bool downstream) {
307 const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0;
308 void* data = (void*)(long)skb->data;
309 const void* data_end = (void*)(long)skb->data_end;
310 struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet
311 struct iphdr* ip = is_ethernet ? (void*)(eth + 1) : data;
312
313 // Require ethernet dst mac address to be our unicast address.
314 if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_OK;
315
316 // Must be meta-ethernet IPv4 frame
317 if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_OK;
318
319 // Must have (ethernet and) ipv4 header
320 if (data + l2_header_size + sizeof(*ip) > data_end) return TC_ACT_OK;
321
322 // Ethertype - if present - must be IPv4
323 if (is_ethernet && (eth->h_proto != htons(ETH_P_IP))) return TC_ACT_OK;
324
325 // IP version must be 4
326 if (ip->version != 4) return TC_ACT_OK;
327
328 // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header
329 if (ip->ihl != 5) return TC_ACT_OK;
330
331 // Calculate the IPv4 one's complement checksum of the IPv4 header.
332 __wsum sum4 = 0;
333 for (int i = 0; i < sizeof(*ip) / sizeof(__u16); ++i) {
334 sum4 += ((__u16*)ip)[i];
335 }
336 // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4
337 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE
338 sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16
339 // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF
340 if (sum4 != 0xFFFF) return TC_ACT_OK;
341
342 // Minimum IPv4 total length is the size of the header
343 if (ntohs(ip->tot_len) < sizeof(*ip)) return TC_ACT_OK;
344
345 // We are incapable of dealing with IPv4 fragments
346 if (ip->frag_off & ~htons(IP_DF)) return TC_ACT_OK;
347
348 // Cannot decrement during forward if already zero or would be zero,
349 // Let the kernel's stack handle these cases and generate appropriate ICMP errors.
350 if (ip->ttl <= 1) return TC_ACT_OK;
351
352 const bool is_tcp = (ip->protocol == IPPROTO_TCP);
353
354 // We do not support anything besides TCP and UDP
355 if (!is_tcp && (ip->protocol != IPPROTO_UDP)) return TC_ACT_OK;
356
357 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
362 if (data + l2_header_size + sizeof(*ip) + sizeof(*tcph) > data_end) return TC_ACT_OK;
363
364 // If hardware offload is running and programming flows based on conntrack entries, try not
365 // to interfere with it, so do not offload TCP packets with any one of the SYN/FIN/RST flags
366 if (tcph->syn || tcph->fin || tcph->rst) return TC_ACT_OK;
367 } else { // UDP
368 // Make sure we can get at the udp header
369 if (data + l2_header_size + sizeof(*ip) + sizeof(*udph) > data_end) return TC_ACT_OK;
370 }
371
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800372 Tether4Key k = {
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800373 .iif = skb->ifindex,
374 .l4Proto = ip->protocol,
375 .src4.s_addr = ip->saddr,
376 .dst4.s_addr = ip->daddr,
377 .srcPort = is_tcp ? tcph->source : udph->source,
378 .dstPort = is_tcp ? tcph->dest : udph->dest,
379 };
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800380 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 -0800381
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800382 Tether4Value* v = downstream ? bpf_tether_downstream4_map_lookup_elem(&k)
383 : bpf_tether_upstream4_map_lookup_elem(&k);
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800384
385 // If we don't find any offload information then simply let the core stack handle it...
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800386 if (!v) return TC_ACT_OK;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800387
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800388 uint32_t stat_and_limit_k = downstream ? skb->ifindex : v->oif;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800389
390 TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k);
391
392 // If we don't have anywhere to put stats, then abort...
393 if (!stat_v) return TC_ACT_OK;
394
395 uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k);
396
397 // If we don't have a limit, then abort...
398 if (!limit_v) return TC_ACT_OK;
399
400 // Required IPv4 minimum mtu is 68, below that not clear what we should do, abort...
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800401 if (v->pmtu < 68) return TC_ACT_OK;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800402
403 // Approximate handling of TCP/IPv4 overhead for incoming LRO/GRO packets: default
404 // outbound path mtu of 1500 is not necessarily correct, but worst case we simply
405 // undercount, which is still better then not accounting for this overhead at all.
406 // Note: this really shouldn't be device/path mtu at all, but rather should be
407 // derived from this particular connection's mss (ie. from gro segment size).
408 // This would require a much newer kernel with newer ebpf accessors.
409 // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header)
410 uint64_t packets = 1;
411 uint64_t bytes = skb->len;
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800412 if (bytes > v->pmtu) {
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800413 const int tcp_overhead = sizeof(struct iphdr) + sizeof(struct tcphdr) + 12;
Maciej Żenczykowski1feb8b42021-01-25 12:01:31 -0800414 const int mss = v->pmtu - tcp_overhead;
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800415 const uint64_t payload = bytes - tcp_overhead;
416 packets = (payload + mss - 1) / mss;
417 bytes = tcp_overhead * packets + payload;
418 }
419
420 // Are we past the limit? If so, then abort...
421 // Note: will not overflow since u64 is 936 years even at 5Gbps.
422 // Do not drop here. Offload is just that, whenever we fail to handle
423 // a packet we let the core stack deal with things.
424 // (The core stack needs to handle limits correctly anyway,
425 // since we don't offload all traffic in both directions)
426 if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) return TC_ACT_OK;
427
428 // TODO: replace Errors with Packets once implemented
429 __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, packets);
430 __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes);
431
432 // TODO: not actually implemented yet
433 return TC_ACT_OK;
434}
435
436// Real implementations for 5.9+ kernels
437
438DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_ether$5_9", AID_ROOT, AID_NETWORK_STACK,
439 sched_cls_tether_downstream4_ether_5_9, KVER(5, 9, 0))
440(struct __sk_buff* skb) {
441 return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true);
442}
443
444DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_rawip$5_9", AID_ROOT, AID_NETWORK_STACK,
445 sched_cls_tether_downstream4_rawip_5_9, KVER(5, 9, 0))
446(struct __sk_buff* skb) {
447 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true);
448}
449
450DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_ether$5_9", AID_ROOT, AID_NETWORK_STACK,
451 sched_cls_tether_upstream4_ether_5_9, KVER(5, 9, 0))
452(struct __sk_buff* skb) {
453 return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false);
454}
455
456DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_rawip$5_9", AID_ROOT, AID_NETWORK_STACK,
457 sched_cls_tether_upstream4_rawip_5_9, KVER(5, 9, 0))
458(struct __sk_buff* skb) {
459 return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false);
460}
461
462// Placeholder implementations for older pre-5.9 kernels
463
464DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK,
465 sched_cls_tether_downstream4_ether_stub, KVER_NONE, KVER(5, 9, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800466(struct __sk_buff* skb) {
467 return TC_ACT_OK;
468}
469
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800470DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
471 sched_cls_tether_downstream4_rawip_stub, KVER_NONE, KVER(5, 9, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800472(struct __sk_buff* skb) {
473 return TC_ACT_OK;
474}
475
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800476DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK,
477 sched_cls_tether_upstream4_ether_stub, KVER_NONE, KVER(5, 9, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800478(struct __sk_buff* skb) {
479 return TC_ACT_OK;
480}
481
Maciej Żenczykowskic2b01462021-01-24 21:01:29 -0800482DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK,
483 sched_cls_tether_upstream4_rawip_stub, KVER_NONE, KVER(5, 9, 0))
Maciej Żenczykowski088fe192021-01-20 13:34:17 -0800484(struct __sk_buff* skb) {
485 return TC_ACT_OK;
486}
487
Maciej Żenczykowskib1997422021-01-20 14:31:50 -0800488// ----- XDP Support -----
489
490#define DEFINE_XDP_PROG(str, func) \
491 DEFINE_BPF_PROG_KVER(str, AID_ROOT, AID_NETWORK_STACK, func, KVER(5, 9, 0))(struct xdp_md *ctx)
492
493DEFINE_XDP_PROG("xdp/tether_downstream_ether",
494 xdp_tether_downstream_ether) {
495 return XDP_PASS;
496}
497
498DEFINE_XDP_PROG("xdp/tether_downstream_rawip",
499 xdp_tether_downstream_rawip) {
500 return XDP_PASS;
501}
502
503DEFINE_XDP_PROG("xdp/tether_upstream_ether",
504 xdp_tether_upstream_ether) {
505 return XDP_PASS;
506}
507
508DEFINE_XDP_PROG("xdp/tether_upstream_rawip",
509 xdp_tether_upstream_rawip) {
510 return XDP_PASS;
511}
512
Hungming Chen56c632c2020-09-10 15:42:58 +0800513LICENSE("Apache 2.0");
514CRITICAL("netd");