Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 1 | /* |
| 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 Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 23 | // bionic kernel uapi linux/udp.h header is munged... |
| 24 | #define __kernel_udphdr udphdr |
| 25 | #include <linux/udp.h> |
| 26 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 27 | #include "bpf_helpers.h" |
| 28 | #include "bpf_net_helpers.h" |
| 29 | #include "netdbpf/bpf_shared.h" |
| 30 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 31 | // From kernel:include/net/ip.h |
| 32 | #define IP_DF 0x4000 // Flag: "Don't Fragment" |
| 33 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 34 | // Tethering stats, indexed by upstream interface. |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 35 | DEFINE_BPF_MAP_GRW(tether_stats_map, HASH, TetherStatsKey, TetherStatsValue, 16, AID_NETWORK_STACK) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 36 | |
| 37 | // Tethering data limit, indexed by upstream interface. |
| 38 | // (tethering allowed when stats[iif].rxBytes + stats[iif].txBytes < limit[iif]) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 39 | DEFINE_BPF_MAP_GRW(tether_limit_map, HASH, TetherLimitKey, TetherLimitValue, 16, AID_NETWORK_STACK) |
| 40 | |
| 41 | // ----- IPv6 Support ----- |
| 42 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 43 | DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 64, |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 44 | AID_NETWORK_STACK) |
| 45 | |
| 46 | DEFINE_BPF_MAP_GRW(tether_downstream64_map, HASH, TetherDownstream64Key, TetherDownstream64Value, |
| 47 | 64, AID_NETWORK_STACK) |
| 48 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 49 | DEFINE_BPF_MAP_GRW(tether_upstream6_map, HASH, TetherUpstream6Key, Tether6Value, 64, |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 50 | AID_NETWORK_STACK) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 51 | |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 52 | static inline __always_inline int do_forward6(struct __sk_buff* skb, const bool is_ethernet, |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 53 | const bool downstream) { |
| 54 | const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 55 | 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 Żenczykowski | 18552e8 | 2021-01-24 19:59:05 -0800 | [diff] [blame] | 60 | // 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 Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 63 | // 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 Żenczykowski | fc4f654 | 2021-01-22 22:19:45 -0800 | [diff] [blame] | 79 | // 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 Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 91 | // 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 Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 97 | // 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 Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 108 | .iif = skb->ifindex, |
| 109 | .neigh6 = ip6->daddr, |
| 110 | }; |
| 111 | |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 112 | TetherUpstream6Key ku = { |
| 113 | .iif = skb->ifindex, |
| 114 | }; |
| 115 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 116 | Tether6Value* v = downstream ? bpf_tether_downstream6_map_lookup_elem(&kd) |
| 117 | : bpf_tether_upstream6_map_lookup_elem(&ku); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 118 | |
| 119 | // If we don't find any offload information then simply let the core stack handle it... |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 120 | if (!v) return TC_ACT_OK; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 121 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 122 | uint32_t stat_and_limit_k = downstream ? skb->ifindex : v->oif; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 123 | |
| 124 | TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k); |
| 125 | |
| 126 | // If we don't have anywhere to put stats, then abort... |
| 127 | if (!stat_v) return TC_ACT_OK; |
| 128 | |
| 129 | uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k); |
| 130 | |
| 131 | // If we don't have a limit, then abort... |
| 132 | if (!limit_v) return TC_ACT_OK; |
| 133 | |
| 134 | // Required IPv6 minimum mtu is 1280, below that not clear what we should do, abort... |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 135 | if (v->pmtu < IPV6_MIN_MTU) return TC_ACT_OK; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 136 | |
| 137 | // Approximate handling of TCP/IPv6 overhead for incoming LRO/GRO packets: default |
| 138 | // outbound path mtu of 1500 is not necessarily correct, but worst case we simply |
| 139 | // undercount, which is still better then not accounting for this overhead at all. |
| 140 | // Note: this really shouldn't be device/path mtu at all, but rather should be |
| 141 | // derived from this particular connection's mss (ie. from gro segment size). |
| 142 | // This would require a much newer kernel with newer ebpf accessors. |
| 143 | // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header) |
| 144 | uint64_t packets = 1; |
| 145 | uint64_t bytes = skb->len; |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 146 | if (bytes > v->pmtu) { |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 147 | const int tcp_overhead = sizeof(struct ipv6hdr) + sizeof(struct tcphdr) + 12; |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 148 | const int mss = v->pmtu - tcp_overhead; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 149 | const uint64_t payload = bytes - tcp_overhead; |
| 150 | packets = (payload + mss - 1) / mss; |
| 151 | bytes = tcp_overhead * packets + payload; |
| 152 | } |
| 153 | |
| 154 | // Are we past the limit? If so, then abort... |
| 155 | // Note: will not overflow since u64 is 936 years even at 5Gbps. |
| 156 | // Do not drop here. Offload is just that, whenever we fail to handle |
| 157 | // a packet we let the core stack deal with things. |
| 158 | // (The core stack needs to handle limits correctly anyway, |
| 159 | // since we don't offload all traffic in both directions) |
| 160 | if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) return TC_ACT_OK; |
| 161 | |
| 162 | if (!is_ethernet) { |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 163 | // Try to inject an ethernet header, and simply return if we fail. |
| 164 | // We do this even if TX interface is RAWIP and thus does not need an ethernet header, |
| 165 | // because this is easier and the kernel will strip extraneous ethernet header. |
| 166 | if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) { |
| 167 | __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 168 | return TC_ACT_OK; |
| 169 | } |
| 170 | |
| 171 | // bpf_skb_change_head() invalidates all pointers - reload them |
| 172 | data = (void*)(long)skb->data; |
| 173 | data_end = (void*)(long)skb->data_end; |
| 174 | eth = data; |
| 175 | ip6 = (void*)(eth + 1); |
| 176 | |
| 177 | // I do not believe this can ever happen, but keep the verifier happy... |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 178 | if (data + sizeof(struct ethhdr) + sizeof(*ip6) > data_end) { |
| 179 | __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 180 | return TC_ACT_SHOT; |
| 181 | } |
| 182 | }; |
| 183 | |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 184 | // At this point we always have an ethernet header - which will get stripped by the |
| 185 | // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid. |
| 186 | // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct. |
| 187 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 188 | // CHECKSUM_COMPLETE is a 16-bit one's complement sum, |
| 189 | // thus corrections for it need to be done in 16-byte chunks at even offsets. |
| 190 | // IPv6 nexthdr is at offset 6, while hop limit is at offset 7 |
| 191 | uint8_t old_hl = ip6->hop_limit; |
| 192 | --ip6->hop_limit; |
| 193 | uint8_t new_hl = ip6->hop_limit; |
| 194 | |
| 195 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 196 | // (-ENOTSUPP) if it isn't. |
| 197 | bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl)); |
| 198 | |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 199 | __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets); |
| 200 | __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 201 | |
| 202 | // Overwrite any mac header with the new one |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 203 | // For a rawip tx interface it will simply be a bunch of zeroes and later stripped. |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 204 | *eth = v->macHeader; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 205 | |
| 206 | // Redirect to forwarded interface. |
| 207 | // |
| 208 | // Note that bpf_redirect() cannot fail unless you pass invalid flags. |
| 209 | // The redirect actually happens after the ebpf program has already terminated, |
| 210 | // and can fail for example for mtu reasons at that point in time, but there's nothing |
| 211 | // we can do about it here. |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 212 | return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 213 | } |
| 214 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 215 | DEFINE_BPF_PROG("schedcls/tether_downstream6_ether", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | 770e0a7 | 2021-01-18 20:14:03 -0800 | [diff] [blame] | 216 | sched_cls_tether_downstream6_ether) |
Maciej Żenczykowski | 6b7829f | 2021-01-18 00:03:37 -0800 | [diff] [blame] | 217 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 218 | return do_forward6(skb, /* is_ethernet */ true, /* downstream */ true); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 221 | DEFINE_BPF_PROG("schedcls/tether_upstream6_ether", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 222 | sched_cls_tether_upstream6_ether) |
| 223 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 224 | return do_forward6(skb, /* is_ethernet */ true, /* downstream */ false); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // Note: section names must be unique to prevent programs from appending to each other, |
| 228 | // so instead the bpf loader will strip everything past the final $ symbol when actually |
| 229 | // pinning the program into the filesystem. |
| 230 | // |
| 231 | // bpf_skb_change_head() is only present on 4.14+ and 2 trivial kernel patches are needed: |
| 232 | // ANDROID: net: bpf: Allow TC programs to call BPF_FUNC_skb_change_head |
| 233 | // ANDROID: net: bpf: permit redirect from ingress L3 to egress L2 devices at near max mtu |
| 234 | // (the first of those has already been upstreamed) |
| 235 | // |
| 236 | // 5.4 kernel support was only added to Android Common Kernel in R, |
| 237 | // and thus a 5.4 kernel always supports this. |
| 238 | // |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 239 | // Hence, these mandatory (must load successfully) implementations for 5.4+ kernels: |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 240 | DEFINE_BPF_PROG_KVER("schedcls/tether_downstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | 770e0a7 | 2021-01-18 20:14:03 -0800 | [diff] [blame] | 241 | sched_cls_tether_downstream6_rawip_5_4, KVER(5, 4, 0)) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 242 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 243 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 244 | } |
| 245 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 246 | DEFINE_BPF_PROG_KVER("schedcls/tether_upstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 247 | sched_cls_tether_upstream6_rawip_5_4, KVER(5, 4, 0)) |
| 248 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 249 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // and these identical optional (may fail to load) implementations for [4.14..5.4) patched kernels: |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 253 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$4_14", |
| 254 | AID_ROOT, AID_NETWORK_STACK, |
| 255 | sched_cls_tether_downstream6_rawip_4_14, |
| 256 | KVER(4, 14, 0), KVER(5, 4, 0)) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 257 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 258 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 259 | } |
| 260 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 261 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$4_14", |
| 262 | AID_ROOT, AID_NETWORK_STACK, |
| 263 | sched_cls_tether_upstream6_rawip_4_14, |
| 264 | KVER(4, 14, 0), KVER(5, 4, 0)) |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 265 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 266 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // and define no-op stubs for [4.9,4.14) and unpatched [4.14,5.4) kernels. |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 270 | // (if the above real 4.14+ program loaded successfully, then bpfloader will have already pinned |
| 271 | // it at the same location this one would be pinned at and will thus skip loading this stub) |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 272 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | 770e0a7 | 2021-01-18 20:14:03 -0800 | [diff] [blame] | 273 | sched_cls_tether_downstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0)) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 274 | (struct __sk_buff* skb) { |
| 275 | return TC_ACT_OK; |
| 276 | } |
| 277 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 278 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 279 | sched_cls_tether_upstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0)) |
| 280 | (struct __sk_buff* skb) { |
| 281 | return TC_ACT_OK; |
| 282 | } |
| 283 | |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 284 | // ----- IPv4 Support ----- |
| 285 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 286 | DEFINE_BPF_MAP_GRW(tether_downstream4_map, HASH, Tether4Key, Tether4Value, 64, AID_NETWORK_STACK) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 287 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 288 | DEFINE_BPF_MAP_GRW(tether_upstream4_map, HASH, Tether4Key, Tether4Value, 64, AID_NETWORK_STACK) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 289 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 290 | static inline __always_inline int do_forward4(struct __sk_buff* skb, const bool is_ethernet, |
| 291 | const bool downstream) { |
| 292 | const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0; |
| 293 | void* data = (void*)(long)skb->data; |
| 294 | const void* data_end = (void*)(long)skb->data_end; |
| 295 | struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet |
| 296 | struct iphdr* ip = is_ethernet ? (void*)(eth + 1) : data; |
| 297 | |
| 298 | // Require ethernet dst mac address to be our unicast address. |
| 299 | if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_OK; |
| 300 | |
| 301 | // Must be meta-ethernet IPv4 frame |
| 302 | if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_OK; |
| 303 | |
| 304 | // Must have (ethernet and) ipv4 header |
| 305 | if (data + l2_header_size + sizeof(*ip) > data_end) return TC_ACT_OK; |
| 306 | |
| 307 | // Ethertype - if present - must be IPv4 |
| 308 | if (is_ethernet && (eth->h_proto != htons(ETH_P_IP))) return TC_ACT_OK; |
| 309 | |
| 310 | // IP version must be 4 |
| 311 | if (ip->version != 4) return TC_ACT_OK; |
| 312 | |
| 313 | // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header |
| 314 | if (ip->ihl != 5) return TC_ACT_OK; |
| 315 | |
| 316 | // Calculate the IPv4 one's complement checksum of the IPv4 header. |
| 317 | __wsum sum4 = 0; |
| 318 | for (int i = 0; i < sizeof(*ip) / sizeof(__u16); ++i) { |
| 319 | sum4 += ((__u16*)ip)[i]; |
| 320 | } |
| 321 | // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4 |
| 322 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE |
| 323 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16 |
| 324 | // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF |
| 325 | if (sum4 != 0xFFFF) return TC_ACT_OK; |
| 326 | |
| 327 | // Minimum IPv4 total length is the size of the header |
| 328 | if (ntohs(ip->tot_len) < sizeof(*ip)) return TC_ACT_OK; |
| 329 | |
| 330 | // We are incapable of dealing with IPv4 fragments |
| 331 | if (ip->frag_off & ~htons(IP_DF)) return TC_ACT_OK; |
| 332 | |
| 333 | // Cannot decrement during forward if already zero or would be zero, |
| 334 | // Let the kernel's stack handle these cases and generate appropriate ICMP errors. |
| 335 | if (ip->ttl <= 1) return TC_ACT_OK; |
| 336 | |
| 337 | const bool is_tcp = (ip->protocol == IPPROTO_TCP); |
| 338 | |
| 339 | // We do not support anything besides TCP and UDP |
| 340 | if (!is_tcp && (ip->protocol != IPPROTO_UDP)) return TC_ACT_OK; |
| 341 | |
| 342 | struct tcphdr* tcph = is_tcp ? (void*)(ip + 1) : NULL; |
| 343 | struct udphdr* udph = is_tcp ? NULL : (void*)(ip + 1); |
| 344 | |
| 345 | if (is_tcp) { |
| 346 | // Make sure we can get at the tcp header |
| 347 | if (data + l2_header_size + sizeof(*ip) + sizeof(*tcph) > data_end) return TC_ACT_OK; |
| 348 | |
| 349 | // If hardware offload is running and programming flows based on conntrack entries, try not |
| 350 | // to interfere with it, so do not offload TCP packets with any one of the SYN/FIN/RST flags |
| 351 | if (tcph->syn || tcph->fin || tcph->rst) return TC_ACT_OK; |
| 352 | } else { // UDP |
| 353 | // Make sure we can get at the udp header |
| 354 | if (data + l2_header_size + sizeof(*ip) + sizeof(*udph) > data_end) return TC_ACT_OK; |
| 355 | } |
| 356 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 357 | Tether4Key k = { |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 358 | .iif = skb->ifindex, |
| 359 | .l4Proto = ip->protocol, |
| 360 | .src4.s_addr = ip->saddr, |
| 361 | .dst4.s_addr = ip->daddr, |
| 362 | .srcPort = is_tcp ? tcph->source : udph->source, |
| 363 | .dstPort = is_tcp ? tcph->dest : udph->dest, |
| 364 | }; |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 365 | if (is_ethernet) for (int i = 0; i < ETH_ALEN; ++i) k.dstMac[i] = eth->h_dest[i]; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 366 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 367 | Tether4Value* v = downstream ? bpf_tether_downstream4_map_lookup_elem(&k) |
| 368 | : bpf_tether_upstream4_map_lookup_elem(&k); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 369 | |
| 370 | // If we don't find any offload information then simply let the core stack handle it... |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 371 | if (!v) return TC_ACT_OK; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 372 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 373 | uint32_t stat_and_limit_k = downstream ? skb->ifindex : v->oif; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 374 | |
| 375 | TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k); |
| 376 | |
| 377 | // If we don't have anywhere to put stats, then abort... |
| 378 | if (!stat_v) return TC_ACT_OK; |
| 379 | |
| 380 | uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k); |
| 381 | |
| 382 | // If we don't have a limit, then abort... |
| 383 | if (!limit_v) return TC_ACT_OK; |
| 384 | |
| 385 | // Required IPv4 minimum mtu is 68, below that not clear what we should do, abort... |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 386 | if (v->pmtu < 68) return TC_ACT_OK; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 387 | |
| 388 | // Approximate handling of TCP/IPv4 overhead for incoming LRO/GRO packets: default |
| 389 | // outbound path mtu of 1500 is not necessarily correct, but worst case we simply |
| 390 | // undercount, which is still better then not accounting for this overhead at all. |
| 391 | // Note: this really shouldn't be device/path mtu at all, but rather should be |
| 392 | // derived from this particular connection's mss (ie. from gro segment size). |
| 393 | // This would require a much newer kernel with newer ebpf accessors. |
| 394 | // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header) |
| 395 | uint64_t packets = 1; |
| 396 | uint64_t bytes = skb->len; |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 397 | if (bytes > v->pmtu) { |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 398 | const int tcp_overhead = sizeof(struct iphdr) + sizeof(struct tcphdr) + 12; |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 399 | const int mss = v->pmtu - tcp_overhead; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 400 | const uint64_t payload = bytes - tcp_overhead; |
| 401 | packets = (payload + mss - 1) / mss; |
| 402 | bytes = tcp_overhead * packets + payload; |
| 403 | } |
| 404 | |
| 405 | // Are we past the limit? If so, then abort... |
| 406 | // Note: will not overflow since u64 is 936 years even at 5Gbps. |
| 407 | // Do not drop here. Offload is just that, whenever we fail to handle |
| 408 | // a packet we let the core stack deal with things. |
| 409 | // (The core stack needs to handle limits correctly anyway, |
| 410 | // since we don't offload all traffic in both directions) |
| 411 | if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) return TC_ACT_OK; |
| 412 | |
| 413 | // TODO: replace Errors with Packets once implemented |
| 414 | __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, packets); |
| 415 | __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes); |
| 416 | |
| 417 | // TODO: not actually implemented yet |
| 418 | return TC_ACT_OK; |
| 419 | } |
| 420 | |
| 421 | // Real implementations for 5.9+ kernels |
| 422 | |
| 423 | DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_ether$5_9", AID_ROOT, AID_NETWORK_STACK, |
| 424 | sched_cls_tether_downstream4_ether_5_9, KVER(5, 9, 0)) |
| 425 | (struct __sk_buff* skb) { |
| 426 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true); |
| 427 | } |
| 428 | |
| 429 | DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_rawip$5_9", AID_ROOT, AID_NETWORK_STACK, |
| 430 | sched_cls_tether_downstream4_rawip_5_9, KVER(5, 9, 0)) |
| 431 | (struct __sk_buff* skb) { |
| 432 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true); |
| 433 | } |
| 434 | |
| 435 | DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_ether$5_9", AID_ROOT, AID_NETWORK_STACK, |
| 436 | sched_cls_tether_upstream4_ether_5_9, KVER(5, 9, 0)) |
| 437 | (struct __sk_buff* skb) { |
| 438 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false); |
| 439 | } |
| 440 | |
| 441 | DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_rawip$5_9", AID_ROOT, AID_NETWORK_STACK, |
| 442 | sched_cls_tether_upstream4_rawip_5_9, KVER(5, 9, 0)) |
| 443 | (struct __sk_buff* skb) { |
| 444 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false); |
| 445 | } |
| 446 | |
| 447 | // Placeholder implementations for older pre-5.9 kernels |
| 448 | |
| 449 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK, |
| 450 | sched_cls_tether_downstream4_ether_stub, KVER_NONE, KVER(5, 9, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 451 | (struct __sk_buff* skb) { |
| 452 | return TC_ACT_OK; |
| 453 | } |
| 454 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 455 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK, |
| 456 | sched_cls_tether_downstream4_rawip_stub, KVER_NONE, KVER(5, 9, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 457 | (struct __sk_buff* skb) { |
| 458 | return TC_ACT_OK; |
| 459 | } |
| 460 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 461 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK, |
| 462 | sched_cls_tether_upstream4_ether_stub, KVER_NONE, KVER(5, 9, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 463 | (struct __sk_buff* skb) { |
| 464 | return TC_ACT_OK; |
| 465 | } |
| 466 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 467 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK, |
| 468 | sched_cls_tether_upstream4_rawip_stub, KVER_NONE, KVER(5, 9, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 469 | (struct __sk_buff* skb) { |
| 470 | return TC_ACT_OK; |
| 471 | } |
| 472 | |
Maciej Żenczykowski | b199742 | 2021-01-20 14:31:50 -0800 | [diff] [blame] | 473 | // ----- XDP Support ----- |
| 474 | |
| 475 | #define DEFINE_XDP_PROG(str, func) \ |
| 476 | DEFINE_BPF_PROG_KVER(str, AID_ROOT, AID_NETWORK_STACK, func, KVER(5, 9, 0))(struct xdp_md *ctx) |
| 477 | |
| 478 | DEFINE_XDP_PROG("xdp/tether_downstream_ether", |
| 479 | xdp_tether_downstream_ether) { |
| 480 | return XDP_PASS; |
| 481 | } |
| 482 | |
| 483 | DEFINE_XDP_PROG("xdp/tether_downstream_rawip", |
| 484 | xdp_tether_downstream_rawip) { |
| 485 | return XDP_PASS; |
| 486 | } |
| 487 | |
| 488 | DEFINE_XDP_PROG("xdp/tether_upstream_ether", |
| 489 | xdp_tether_upstream_ether) { |
| 490 | return XDP_PASS; |
| 491 | } |
| 492 | |
| 493 | DEFINE_XDP_PROG("xdp/tether_upstream_rawip", |
| 494 | xdp_tether_upstream_rawip) { |
| 495 | return XDP_PASS; |
| 496 | } |
| 497 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 498 | LICENSE("Apache 2.0"); |
| 499 | CRITICAL("netd"); |