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" |
Lorenzo Colitti | b81584d | 2021-02-06 00:00:58 +0900 | [diff] [blame] | 29 | #include "bpf_tethering.h" |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 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 | |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 34 | // ----- 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 Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 70 | // ----- Tethering Error Counters ----- |
| 71 | |
| 72 | DEFINE_BPF_MAP_GRW(tether_error_map, ARRAY, uint32_t, uint32_t, BPF_TETHER_ERR__MAX, |
| 73 | AID_NETWORK_STACK) |
| 74 | |
Maciej Żenczykowski | 3f32a83 | 2021-03-17 19:27:23 -0700 | [diff] [blame] | 75 | #define COUNT_AND_RETURN(counter, ret) do { \ |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 76 | uint32_t code = BPF_TETHER_ERR_ ## counter; \ |
| 77 | uint32_t *count = bpf_tether_error_map_lookup_elem(&code); \ |
Maciej Żenczykowski | 3f32a83 | 2021-03-17 19:27:23 -0700 | [diff] [blame] | 78 | if (count) __sync_fetch_and_add(count, 1); \ |
| 79 | return ret; \ |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 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 Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 89 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 90 | // Tethering stats, indexed by upstream interface. |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 91 | 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] | 92 | |
| 93 | // Tethering data limit, indexed by upstream interface. |
| 94 | // (tethering allowed when stats[iif].rxBytes + stats[iif].txBytes < limit[iif]) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 95 | DEFINE_BPF_MAP_GRW(tether_limit_map, HASH, TetherLimitKey, TetherLimitValue, 16, AID_NETWORK_STACK) |
| 96 | |
| 97 | // ----- IPv6 Support ----- |
| 98 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 99 | DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 64, |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 100 | AID_NETWORK_STACK) |
| 101 | |
| 102 | DEFINE_BPF_MAP_GRW(tether_downstream64_map, HASH, TetherDownstream64Key, TetherDownstream64Value, |
Maciej Żenczykowski | 8549af9 | 2021-02-25 19:11:34 -0800 | [diff] [blame] | 103 | 1024, AID_NETWORK_STACK) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 104 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 105 | DEFINE_BPF_MAP_GRW(tether_upstream6_map, HASH, TetherUpstream6Key, Tether6Value, 64, |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 106 | AID_NETWORK_STACK) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 107 | |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 108 | 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] | 109 | const bool downstream) { |
| 110 | const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 111 | 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 Żenczykowski | 18552e8 | 2021-01-24 19:59:05 -0800 | [diff] [blame] | 116 | // 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 Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 119 | // 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 Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 129 | if (ip6->version != 6) TC_PUNT(INVALID_IP_VERSION); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 130 | |
| 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 Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 133 | if (ip6->hop_limit <= 1) TC_PUNT(LOW_TTL); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 134 | |
Maciej Żenczykowski | fc4f654 | 2021-01-22 22:19:45 -0800 | [diff] [blame] | 135 | // 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 Colitti | b81584d | 2021-02-06 00:00:58 +0900 | [diff] [blame] | 141 | if (data + l2_header_size + sizeof(*ip6) + sizeof(*tcph) > data_end) |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 142 | TC_PUNT(INVALID_TCP_HEADER); |
Maciej Żenczykowski | fc4f654 | 2021-01-22 22:19:45 -0800 | [diff] [blame] | 143 | |
| 144 | // Do not offload TCP packets with any one of the SYN/FIN/RST flags |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 145 | if (tcph->syn || tcph->fin || tcph->rst) TC_PUNT(TCP_CONTROL_PACKET); |
Maciej Żenczykowski | fc4f654 | 2021-01-22 22:19:45 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 148 | // 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 Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 152 | TC_PUNT(NON_GLOBAL_SRC); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 153 | |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 154 | // 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 Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 158 | TC_PUNT(NON_GLOBAL_DST); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 159 | |
| 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 Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 162 | TC_PUNT(LOCAL_SRC_DST); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 163 | |
| 164 | TetherDownstream6Key kd = { |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 165 | .iif = skb->ifindex, |
| 166 | .neigh6 = ip6->daddr, |
| 167 | }; |
| 168 | |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 169 | TetherUpstream6Key ku = { |
| 170 | .iif = skb->ifindex, |
| 171 | }; |
Maciej Żenczykowski | 62733f5 | 2021-04-01 21:51:41 -0700 | [diff] [blame^] | 172 | if (is_ethernet) __builtin_memcpy(downstream ? kd.dstMac : ku.dstMac, eth->h_dest, ETH_ALEN); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 173 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 174 | Tether6Value* v = downstream ? bpf_tether_downstream6_map_lookup_elem(&kd) |
| 175 | : bpf_tether_upstream6_map_lookup_elem(&ku); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 176 | |
| 177 | // 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] | 178 | if (!v) return TC_ACT_OK; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 179 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 180 | uint32_t stat_and_limit_k = downstream ? skb->ifindex : v->oif; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 181 | |
| 182 | TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k); |
| 183 | |
| 184 | // If we don't have anywhere to put stats, then abort... |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 185 | if (!stat_v) TC_PUNT(NO_STATS_ENTRY); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 186 | |
| 187 | uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k); |
| 188 | |
| 189 | // If we don't have a limit, then abort... |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 190 | if (!limit_v) TC_PUNT(NO_LIMIT_ENTRY); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 191 | |
| 192 | // Required IPv6 minimum mtu is 1280, below that not clear what we should do, abort... |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 193 | if (v->pmtu < IPV6_MIN_MTU) TC_PUNT(BELOW_IPV6_MTU); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 194 | |
| 195 | // Approximate handling of TCP/IPv6 overhead for incoming LRO/GRO packets: default |
| 196 | // outbound path mtu of 1500 is not necessarily correct, but worst case we simply |
| 197 | // undercount, which is still better then not accounting for this overhead at all. |
| 198 | // Note: this really shouldn't be device/path mtu at all, but rather should be |
| 199 | // derived from this particular connection's mss (ie. from gro segment size). |
| 200 | // This would require a much newer kernel with newer ebpf accessors. |
| 201 | // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header) |
| 202 | uint64_t packets = 1; |
| 203 | uint64_t bytes = skb->len; |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 204 | if (bytes > v->pmtu) { |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 205 | const int tcp_overhead = sizeof(struct ipv6hdr) + sizeof(struct tcphdr) + 12; |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 206 | const int mss = v->pmtu - tcp_overhead; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 207 | const uint64_t payload = bytes - tcp_overhead; |
| 208 | packets = (payload + mss - 1) / mss; |
| 209 | bytes = tcp_overhead * packets + payload; |
| 210 | } |
| 211 | |
| 212 | // Are we past the limit? If so, then abort... |
| 213 | // Note: will not overflow since u64 is 936 years even at 5Gbps. |
| 214 | // Do not drop here. Offload is just that, whenever we fail to handle |
| 215 | // a packet we let the core stack deal with things. |
| 216 | // (The core stack needs to handle limits correctly anyway, |
| 217 | // since we don't offload all traffic in both directions) |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 218 | if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) TC_PUNT(LIMIT_REACHED); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 219 | |
| 220 | if (!is_ethernet) { |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 221 | // Try to inject an ethernet header, and simply return if we fail. |
| 222 | // We do this even if TX interface is RAWIP and thus does not need an ethernet header, |
| 223 | // because this is easier and the kernel will strip extraneous ethernet header. |
| 224 | if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) { |
| 225 | __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1); |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 226 | TC_PUNT(CHANGE_HEAD_FAILED); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // bpf_skb_change_head() invalidates all pointers - reload them |
| 230 | data = (void*)(long)skb->data; |
| 231 | data_end = (void*)(long)skb->data_end; |
| 232 | eth = data; |
| 233 | ip6 = (void*)(eth + 1); |
| 234 | |
| 235 | // 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] | 236 | if (data + sizeof(struct ethhdr) + sizeof(*ip6) > data_end) { |
| 237 | __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1); |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 238 | TC_DROP(TOO_SHORT); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 239 | } |
| 240 | }; |
| 241 | |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 242 | // At this point we always have an ethernet header - which will get stripped by the |
| 243 | // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid. |
| 244 | // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct. |
| 245 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 246 | // CHECKSUM_COMPLETE is a 16-bit one's complement sum, |
| 247 | // thus corrections for it need to be done in 16-byte chunks at even offsets. |
| 248 | // IPv6 nexthdr is at offset 6, while hop limit is at offset 7 |
| 249 | uint8_t old_hl = ip6->hop_limit; |
| 250 | --ip6->hop_limit; |
| 251 | uint8_t new_hl = ip6->hop_limit; |
| 252 | |
| 253 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 254 | // (-ENOTSUPP) if it isn't. |
| 255 | bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl)); |
| 256 | |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 257 | __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets); |
| 258 | __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 259 | |
| 260 | // Overwrite any mac header with the new one |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 261 | // 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] | 262 | *eth = v->macHeader; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 263 | |
| 264 | // Redirect to forwarded interface. |
| 265 | // |
| 266 | // Note that bpf_redirect() cannot fail unless you pass invalid flags. |
| 267 | // The redirect actually happens after the ebpf program has already terminated, |
| 268 | // and can fail for example for mtu reasons at that point in time, but there's nothing |
| 269 | // we can do about it here. |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 270 | return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 271 | } |
| 272 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 273 | DEFINE_BPF_PROG("schedcls/tether_downstream6_ether", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | 770e0a7 | 2021-01-18 20:14:03 -0800 | [diff] [blame] | 274 | sched_cls_tether_downstream6_ether) |
Maciej Żenczykowski | 6b7829f | 2021-01-18 00:03:37 -0800 | [diff] [blame] | 275 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 276 | return do_forward6(skb, /* is_ethernet */ true, /* downstream */ true); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 279 | DEFINE_BPF_PROG("schedcls/tether_upstream6_ether", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 280 | sched_cls_tether_upstream6_ether) |
| 281 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 282 | return do_forward6(skb, /* is_ethernet */ true, /* downstream */ false); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // Note: section names must be unique to prevent programs from appending to each other, |
| 286 | // so instead the bpf loader will strip everything past the final $ symbol when actually |
| 287 | // pinning the program into the filesystem. |
| 288 | // |
| 289 | // bpf_skb_change_head() is only present on 4.14+ and 2 trivial kernel patches are needed: |
| 290 | // ANDROID: net: bpf: Allow TC programs to call BPF_FUNC_skb_change_head |
| 291 | // ANDROID: net: bpf: permit redirect from ingress L3 to egress L2 devices at near max mtu |
| 292 | // (the first of those has already been upstreamed) |
| 293 | // |
| 294 | // 5.4 kernel support was only added to Android Common Kernel in R, |
| 295 | // and thus a 5.4 kernel always supports this. |
| 296 | // |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 297 | // Hence, these mandatory (must load successfully) implementations for 5.4+ kernels: |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 298 | 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] | 299 | sched_cls_tether_downstream6_rawip_5_4, KVER(5, 4, 0)) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 300 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 301 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 302 | } |
| 303 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 304 | 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] | 305 | sched_cls_tether_upstream6_rawip_5_4, KVER(5, 4, 0)) |
| 306 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 307 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // 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] | 311 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$4_14", |
| 312 | AID_ROOT, AID_NETWORK_STACK, |
| 313 | sched_cls_tether_downstream6_rawip_4_14, |
| 314 | KVER(4, 14, 0), KVER(5, 4, 0)) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 315 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 316 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 317 | } |
| 318 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 319 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$4_14", |
| 320 | AID_ROOT, AID_NETWORK_STACK, |
| 321 | sched_cls_tether_upstream6_rawip_4_14, |
| 322 | KVER(4, 14, 0), KVER(5, 4, 0)) |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 323 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 324 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // 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] | 328 | // (if the above real 4.14+ program loaded successfully, then bpfloader will have already pinned |
| 329 | // 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] | 330 | 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] | 331 | sched_cls_tether_downstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0)) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 332 | (struct __sk_buff* skb) { |
| 333 | return TC_ACT_OK; |
| 334 | } |
| 335 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 336 | 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] | 337 | sched_cls_tether_upstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0)) |
| 338 | (struct __sk_buff* skb) { |
| 339 | return TC_ACT_OK; |
| 340 | } |
| 341 | |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 342 | // ----- IPv4 Support ----- |
| 343 | |
Maciej Żenczykowski | 8549af9 | 2021-02-25 19:11:34 -0800 | [diff] [blame] | 344 | DEFINE_BPF_MAP_GRW(tether_downstream4_map, HASH, Tether4Key, Tether4Value, 1024, AID_NETWORK_STACK) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 345 | |
Maciej Żenczykowski | 8549af9 | 2021-02-25 19:11:34 -0800 | [diff] [blame] | 346 | DEFINE_BPF_MAP_GRW(tether_upstream4_map, HASH, Tether4Key, Tether4Value, 1024, AID_NETWORK_STACK) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 347 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 348 | static inline __always_inline int do_forward4(struct __sk_buff* skb, const bool is_ethernet, |
Maciej Żenczykowski | aefa095 | 2021-02-14 23:15:19 -0800 | [diff] [blame] | 349 | const bool downstream, const bool updatetime) { |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 350 | const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0; |
| 351 | void* data = (void*)(long)skb->data; |
| 352 | const void* data_end = (void*)(long)skb->data_end; |
| 353 | struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet |
| 354 | struct iphdr* ip = is_ethernet ? (void*)(eth + 1) : data; |
| 355 | |
| 356 | // Require ethernet dst mac address to be our unicast address. |
| 357 | if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_OK; |
| 358 | |
| 359 | // Must be meta-ethernet IPv4 frame |
| 360 | if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_OK; |
| 361 | |
| 362 | // Must have (ethernet and) ipv4 header |
| 363 | if (data + l2_header_size + sizeof(*ip) > data_end) return TC_ACT_OK; |
| 364 | |
| 365 | // Ethertype - if present - must be IPv4 |
| 366 | if (is_ethernet && (eth->h_proto != htons(ETH_P_IP))) return TC_ACT_OK; |
| 367 | |
| 368 | // IP version must be 4 |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 369 | if (ip->version != 4) TC_PUNT(INVALID_IP_VERSION); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 370 | |
| 371 | // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 372 | if (ip->ihl != 5) TC_PUNT(HAS_IP_OPTIONS); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 373 | |
| 374 | // Calculate the IPv4 one's complement checksum of the IPv4 header. |
| 375 | __wsum sum4 = 0; |
| 376 | for (int i = 0; i < sizeof(*ip) / sizeof(__u16); ++i) { |
| 377 | sum4 += ((__u16*)ip)[i]; |
| 378 | } |
| 379 | // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4 |
| 380 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE |
| 381 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16 |
| 382 | // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 383 | if (sum4 != 0xFFFF) TC_PUNT(CHECKSUM); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 384 | |
| 385 | // Minimum IPv4 total length is the size of the header |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 386 | if (ntohs(ip->tot_len) < sizeof(*ip)) TC_PUNT(TRUNCATED_IPV4); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 387 | |
| 388 | // We are incapable of dealing with IPv4 fragments |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 389 | if (ip->frag_off & ~htons(IP_DF)) TC_PUNT(IS_IP_FRAG); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 390 | |
| 391 | // Cannot decrement during forward if already zero or would be zero, |
| 392 | // Let the kernel's stack handle these cases and generate appropriate ICMP errors. |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 393 | if (ip->ttl <= 1) TC_PUNT(LOW_TTL); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 394 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 395 | // If we cannot update the 'last_used' field due to lack of bpf_ktime_get_boot_ns() helper, |
| 396 | // then it is not safe to offload UDP due to the small conntrack timeouts, as such, |
| 397 | // in such a situation we can only support TCP. This also has the added nice benefit of |
| 398 | // using a separate error counter, and thus making it obvious which version of the program |
| 399 | // is loaded. |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 400 | if (!updatetime && ip->protocol != IPPROTO_TCP) TC_PUNT(NON_TCP); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 401 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 402 | // We do not support offloading anything besides IPv4 TCP and UDP, due to need for NAT, |
| 403 | // but no need to check this if !updatetime due to check immediately above. |
| 404 | if (updatetime && (ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP)) |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 405 | TC_PUNT(NON_TCP_UDP); |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 406 | |
| 407 | // We want to make sure that the compiler will, in the !updatetime case, entirely optimize |
| 408 | // out all the non-tcp logic. Also note that at this point is_udp === !is_tcp. |
| 409 | const bool is_tcp = !updatetime || (ip->protocol == IPPROTO_TCP); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 410 | |
Maciej Żenczykowski | 82ee26b | 2021-02-16 16:10:08 -0800 | [diff] [blame] | 411 | // This is a bit of a hack to make things easier on the bpf verifier. |
| 412 | // (In particular I believe the Linux 4.14 kernel's verifier can get confused later on about |
| 413 | // what offsets into the packet are valid and can spuriously reject the program, this is |
| 414 | // because it fails to realize that is_tcp && !is_tcp is impossible) |
| 415 | // |
| 416 | // For both TCP & UDP we'll need to read and modify the src/dst ports, which so happen to |
| 417 | // always be in the first 4 bytes of the L4 header. Additionally for UDP we'll need access |
| 418 | // to the checksum field which is in bytes 7 and 8. While for TCP we'll need to read the |
| 419 | // TCP flags (at offset 13) and access to the checksum field (2 bytes at offset 16). |
| 420 | // As such we *always* need access to at least 8 bytes. |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 421 | if (data + l2_header_size + sizeof(*ip) + 8 > data_end) TC_PUNT(SHORT_L4_HEADER); |
Maciej Żenczykowski | 82ee26b | 2021-02-16 16:10:08 -0800 | [diff] [blame] | 422 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 423 | struct tcphdr* tcph = is_tcp ? (void*)(ip + 1) : NULL; |
| 424 | struct udphdr* udph = is_tcp ? NULL : (void*)(ip + 1); |
| 425 | |
| 426 | if (is_tcp) { |
| 427 | // Make sure we can get at the tcp header |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 428 | if (data + l2_header_size + sizeof(*ip) + sizeof(*tcph) > data_end) |
| 429 | TC_PUNT(SHORT_TCP_HEADER); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 430 | |
| 431 | // If hardware offload is running and programming flows based on conntrack entries, try not |
| 432 | // to interfere with it, so do not offload TCP packets with any one of the SYN/FIN/RST flags |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 433 | if (tcph->syn || tcph->fin || tcph->rst) TC_PUNT(TCP_CONTROL_PACKET); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 434 | } else { // UDP |
| 435 | // Make sure we can get at the udp header |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 436 | if (data + l2_header_size + sizeof(*ip) + sizeof(*udph) > data_end) |
| 437 | TC_PUNT(SHORT_UDP_HEADER); |
Maciej Żenczykowski | e4a726a | 2021-02-16 17:27:34 -0800 | [diff] [blame] | 438 | |
| 439 | // Skip handling of CHECKSUM_COMPLETE packets with udp checksum zero due to need for |
| 440 | // additional updating of skb->csum (this could be fixed up manually with more effort). |
| 441 | // |
| 442 | // Note that the in-kernel implementation of 'int64_t bpf_csum_update(skb, u32 csum)' is: |
| 443 | // if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 444 | // return (skb->csum = csum_add(skb->csum, csum)); |
| 445 | // else |
| 446 | // return -ENOTSUPP; |
| 447 | // |
| 448 | // So this will punt any CHECKSUM_COMPLETE packet with a zero UDP checksum, |
| 449 | // and leave all other packets unaffected (since it just at most adds zero to skb->csum). |
| 450 | // |
| 451 | // In practice this should almost never trigger because most nics do not generate |
| 452 | // CHECKSUM_COMPLETE packets on receive - especially so for nics/drivers on a phone. |
| 453 | // |
| 454 | // Additionally since we're forwarding, in most cases the value of the skb->csum field |
| 455 | // shouldn't matter (it's not used by physical nic egress). |
| 456 | // |
| 457 | // It only matters if we're ingressing through a CHECKSUM_COMPLETE capable nic |
| 458 | // and egressing through a virtual interface looping back to the kernel itself |
| 459 | // (ie. something like veth) where the CHECKSUM_COMPLETE/skb->csum can get reused |
| 460 | // on ingress. |
| 461 | // |
| 462 | // If we were in the kernel we'd simply probably call |
| 463 | // void skb_checksum_complete_unset(struct sk_buff *skb) { |
| 464 | // if (skb->ip_summed == CHECKSUM_COMPLETE) skb->ip_summed = CHECKSUM_NONE; |
| 465 | // } |
| 466 | // here instead. Perhaps there should be a bpf helper for that? |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 467 | if (!udph->check && (bpf_csum_update(skb, 0) >= 0)) TC_PUNT(UDP_CSUM_ZERO); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 470 | Tether4Key k = { |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 471 | .iif = skb->ifindex, |
| 472 | .l4Proto = ip->protocol, |
| 473 | .src4.s_addr = ip->saddr, |
| 474 | .dst4.s_addr = ip->daddr, |
| 475 | .srcPort = is_tcp ? tcph->source : udph->source, |
| 476 | .dstPort = is_tcp ? tcph->dest : udph->dest, |
| 477 | }; |
Maciej Żenczykowski | 62733f5 | 2021-04-01 21:51:41 -0700 | [diff] [blame^] | 478 | if (is_ethernet) __builtin_memcpy(k.dstMac, eth->h_dest, ETH_ALEN); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 479 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 480 | Tether4Value* v = downstream ? bpf_tether_downstream4_map_lookup_elem(&k) |
| 481 | : bpf_tether_upstream4_map_lookup_elem(&k); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 482 | |
| 483 | // 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] | 484 | if (!v) return TC_ACT_OK; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 485 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 486 | uint32_t stat_and_limit_k = downstream ? skb->ifindex : v->oif; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 487 | |
| 488 | TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k); |
| 489 | |
| 490 | // If we don't have anywhere to put stats, then abort... |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 491 | if (!stat_v) TC_PUNT(NO_STATS_ENTRY); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 492 | |
| 493 | uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k); |
| 494 | |
| 495 | // If we don't have a limit, then abort... |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 496 | if (!limit_v) TC_PUNT(NO_LIMIT_ENTRY); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 497 | |
| 498 | // Required IPv4 minimum mtu is 68, below that not clear what we should do, abort... |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 499 | if (v->pmtu < 68) TC_PUNT(BELOW_IPV4_MTU); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 500 | |
| 501 | // Approximate handling of TCP/IPv4 overhead for incoming LRO/GRO packets: default |
| 502 | // outbound path mtu of 1500 is not necessarily correct, but worst case we simply |
| 503 | // undercount, which is still better then not accounting for this overhead at all. |
| 504 | // Note: this really shouldn't be device/path mtu at all, but rather should be |
| 505 | // derived from this particular connection's mss (ie. from gro segment size). |
| 506 | // This would require a much newer kernel with newer ebpf accessors. |
| 507 | // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header) |
| 508 | uint64_t packets = 1; |
| 509 | uint64_t bytes = skb->len; |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 510 | if (bytes > v->pmtu) { |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 511 | const int tcp_overhead = sizeof(struct iphdr) + sizeof(struct tcphdr) + 12; |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 512 | const int mss = v->pmtu - tcp_overhead; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 513 | const uint64_t payload = bytes - tcp_overhead; |
| 514 | packets = (payload + mss - 1) / mss; |
| 515 | bytes = tcp_overhead * packets + payload; |
| 516 | } |
| 517 | |
| 518 | // Are we past the limit? If so, then abort... |
| 519 | // Note: will not overflow since u64 is 936 years even at 5Gbps. |
| 520 | // Do not drop here. Offload is just that, whenever we fail to handle |
| 521 | // a packet we let the core stack deal with things. |
| 522 | // (The core stack needs to handle limits correctly anyway, |
| 523 | // since we don't offload all traffic in both directions) |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 524 | if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) TC_PUNT(LIMIT_REACHED); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 525 | |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 526 | if (!is_ethernet) { |
| 527 | // Try to inject an ethernet header, and simply return if we fail. |
| 528 | // We do this even if TX interface is RAWIP and thus does not need an ethernet header, |
| 529 | // because this is easier and the kernel will strip extraneous ethernet header. |
| 530 | if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) { |
| 531 | __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1); |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 532 | TC_PUNT(CHANGE_HEAD_FAILED); |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // bpf_skb_change_head() invalidates all pointers - reload them |
| 536 | data = (void*)(long)skb->data; |
| 537 | data_end = (void*)(long)skb->data_end; |
| 538 | eth = data; |
| 539 | ip = (void*)(eth + 1); |
| 540 | tcph = is_tcp ? (void*)(ip + 1) : NULL; |
| 541 | udph = is_tcp ? NULL : (void*)(ip + 1); |
| 542 | |
| 543 | // I do not believe this can ever happen, but keep the verifier happy... |
| 544 | if (data + sizeof(struct ethhdr) + sizeof(*ip) + (is_tcp ? sizeof(*tcph) : sizeof(*udph)) > data_end) { |
| 545 | __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1); |
Maciej Żenczykowski | e982f09 | 2021-02-04 20:26:26 -0800 | [diff] [blame] | 546 | TC_DROP(TOO_SHORT); |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 547 | } |
| 548 | }; |
| 549 | |
| 550 | // At this point we always have an ethernet header - which will get stripped by the |
| 551 | // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid. |
| 552 | // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct. |
| 553 | |
| 554 | // Overwrite any mac header with the new one |
| 555 | // For a rawip tx interface it will simply be a bunch of zeroes and later stripped. |
| 556 | *eth = v->macHeader; |
| 557 | |
Maciej Żenczykowski | e4a726a | 2021-02-16 17:27:34 -0800 | [diff] [blame] | 558 | const int l4_offs_csum = is_tcp ? ETH_IP4_TCP_OFFSET(check) : ETH_IP4_UDP_OFFSET(check); |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 559 | const int sz4 = sizeof(__be32); |
Maciej Żenczykowski | e4a726a | 2021-02-16 17:27:34 -0800 | [diff] [blame] | 560 | // UDP 0 is special and stored as FFFF (this flag also causes a csum of 0 to be unmodified) |
| 561 | const int l4_flags = is_tcp ? 0 : BPF_F_MARK_MANGLED_0; |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 562 | const __be32 old_daddr = k.dst4.s_addr; |
| 563 | const __be32 old_saddr = k.src4.s_addr; |
| 564 | const __be32 new_daddr = v->dst46.s6_addr32[3]; |
| 565 | const __be32 new_saddr = v->src46.s6_addr32[3]; |
| 566 | |
Maciej Żenczykowski | e4a726a | 2021-02-16 17:27:34 -0800 | [diff] [blame] | 567 | bpf_l4_csum_replace(skb, l4_offs_csum, old_daddr, new_daddr, sz4 | BPF_F_PSEUDO_HDR | l4_flags); |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 568 | bpf_l3_csum_replace(skb, ETH_IP4_OFFSET(check), old_daddr, new_daddr, sz4); |
| 569 | bpf_skb_store_bytes(skb, ETH_IP4_OFFSET(daddr), &new_daddr, sz4, 0); |
| 570 | |
Maciej Żenczykowski | e4a726a | 2021-02-16 17:27:34 -0800 | [diff] [blame] | 571 | bpf_l4_csum_replace(skb, l4_offs_csum, old_saddr, new_saddr, sz4 | BPF_F_PSEUDO_HDR | l4_flags); |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 572 | bpf_l3_csum_replace(skb, ETH_IP4_OFFSET(check), old_saddr, new_saddr, sz4); |
| 573 | bpf_skb_store_bytes(skb, ETH_IP4_OFFSET(saddr), &new_saddr, sz4, 0); |
| 574 | |
| 575 | const int sz2 = sizeof(__be16); |
Maciej Żenczykowski | e4a726a | 2021-02-16 17:27:34 -0800 | [diff] [blame] | 576 | // The offsets for TCP and UDP ports: source (u16 @ L4 offset 0) & dest (u16 @ L4 offset 2) are |
| 577 | // actually the same, so the compiler should just optimize them both down to a constant. |
| 578 | bpf_l4_csum_replace(skb, l4_offs_csum, k.srcPort, v->srcPort, sz2 | l4_flags); |
| 579 | bpf_skb_store_bytes(skb, is_tcp ? ETH_IP4_TCP_OFFSET(source) : ETH_IP4_UDP_OFFSET(source), |
| 580 | &v->srcPort, sz2, 0); |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 581 | |
Maciej Żenczykowski | e4a726a | 2021-02-16 17:27:34 -0800 | [diff] [blame] | 582 | bpf_l4_csum_replace(skb, l4_offs_csum, k.dstPort, v->dstPort, sz2 | l4_flags); |
| 583 | bpf_skb_store_bytes(skb, is_tcp ? ETH_IP4_TCP_OFFSET(dest) : ETH_IP4_UDP_OFFSET(dest), |
| 584 | &v->dstPort, sz2, 0); |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 585 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 586 | // TEMP HACK: lack of TTL decrement |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 587 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 588 | // This requires the bpf_ktime_get_boot_ns() helper which was added in 5.8, |
| 589 | // and backported to all Android Common Kernel 4.14+ trees. |
Maciej Żenczykowski | aefa095 | 2021-02-14 23:15:19 -0800 | [diff] [blame] | 590 | if (updatetime) v->last_used = bpf_ktime_get_boot_ns(); |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 591 | |
| 592 | __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 593 | __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes); |
| 594 | |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 595 | // Redirect to forwarded interface. |
| 596 | // |
| 597 | // Note that bpf_redirect() cannot fail unless you pass invalid flags. |
| 598 | // The redirect actually happens after the ebpf program has already terminated, |
| 599 | // and can fail for example for mtu reasons at that point in time, but there's nothing |
| 600 | // we can do about it here. |
| 601 | return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Maciej Żenczykowski | acddd4f | 2021-03-09 21:43:48 -0800 | [diff] [blame] | 604 | // Full featured (required) implementations for 5.8+ kernels (these are S+ by definition) |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 605 | |
Maciej Żenczykowski | b0ac41f | 2021-02-14 21:34:03 -0800 | [diff] [blame] | 606 | DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_rawip$5_8", AID_ROOT, AID_NETWORK_STACK, |
| 607 | sched_cls_tether_downstream4_rawip_5_8, KVER(5, 8, 0)) |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 608 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | aefa095 | 2021-02-14 23:15:19 -0800 | [diff] [blame] | 609 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true, /* updatetime */ true); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Maciej Żenczykowski | b0ac41f | 2021-02-14 21:34:03 -0800 | [diff] [blame] | 612 | DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_rawip$5_8", AID_ROOT, AID_NETWORK_STACK, |
| 613 | sched_cls_tether_upstream4_rawip_5_8, KVER(5, 8, 0)) |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 614 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | aefa095 | 2021-02-14 23:15:19 -0800 | [diff] [blame] | 615 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false, /* updatetime */ true); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Maciej Żenczykowski | 2278aed | 2021-03-09 21:19:52 -0800 | [diff] [blame] | 618 | DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_ether$5_8", AID_ROOT, AID_NETWORK_STACK, |
| 619 | sched_cls_tether_downstream4_ether_5_8, KVER(5, 8, 0)) |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 620 | (struct __sk_buff* skb) { |
| 621 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true, /* updatetime */ true); |
| 622 | } |
| 623 | |
Maciej Żenczykowski | 2278aed | 2021-03-09 21:19:52 -0800 | [diff] [blame] | 624 | DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_ether$5_8", AID_ROOT, AID_NETWORK_STACK, |
| 625 | sched_cls_tether_upstream4_ether_5_8, KVER(5, 8, 0)) |
| 626 | (struct __sk_buff* skb) { |
| 627 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false, /* updatetime */ true); |
| 628 | } |
| 629 | |
Maciej Żenczykowski | acddd4f | 2021-03-09 21:43:48 -0800 | [diff] [blame] | 630 | // Full featured (optional) implementations for 4.14-S, 4.19-S & 5.4-S kernels |
| 631 | // (optional, because we need to be able to fallback for 4.14/4.19/5.4 pre-S kernels) |
Maciej Żenczykowski | 2278aed | 2021-03-09 21:19:52 -0800 | [diff] [blame] | 632 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 633 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$opt", |
| 634 | AID_ROOT, AID_NETWORK_STACK, |
| 635 | sched_cls_tether_downstream4_rawip_opt, |
| 636 | KVER(4, 14, 0), KVER(5, 8, 0)) |
| 637 | (struct __sk_buff* skb) { |
| 638 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true, /* updatetime */ true); |
| 639 | } |
| 640 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 641 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$opt", |
| 642 | AID_ROOT, AID_NETWORK_STACK, |
| 643 | sched_cls_tether_upstream4_rawip_opt, |
| 644 | KVER(4, 14, 0), KVER(5, 8, 0)) |
| 645 | (struct __sk_buff* skb) { |
| 646 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false, /* updatetime */ true); |
| 647 | } |
| 648 | |
Maciej Żenczykowski | 2278aed | 2021-03-09 21:19:52 -0800 | [diff] [blame] | 649 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$opt", |
| 650 | AID_ROOT, AID_NETWORK_STACK, |
| 651 | sched_cls_tether_downstream4_ether_opt, |
| 652 | KVER(4, 14, 0), KVER(5, 8, 0)) |
| 653 | (struct __sk_buff* skb) { |
| 654 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true, /* updatetime */ true); |
| 655 | } |
| 656 | |
| 657 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$opt", |
| 658 | AID_ROOT, AID_NETWORK_STACK, |
| 659 | sched_cls_tether_upstream4_ether_opt, |
| 660 | KVER(4, 14, 0), KVER(5, 8, 0)) |
| 661 | (struct __sk_buff* skb) { |
| 662 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false, /* updatetime */ true); |
| 663 | } |
| 664 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 665 | // Partial (TCP-only: will not update 'last_used' field) implementations for 4.14+ kernels. |
Maciej Żenczykowski | acddd4f | 2021-03-09 21:43:48 -0800 | [diff] [blame] | 666 | // These will be loaded only if the above optional ones failed (loading of *these* must succeed |
| 667 | // for 5.4+, since that is always an R patched kernel). |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 668 | // |
| 669 | // [Note: as a result TCP connections will not have their conntrack timeout refreshed, however, |
| 670 | // since /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established defaults to 432000 (seconds), |
| 671 | // this in practice means they'll break only after 5 days. This seems an acceptable trade-off. |
| 672 | // |
| 673 | // Additionally kernel/tests change "net-test: add bpf_ktime_get_ns / bpf_ktime_get_boot_ns tests" |
| 674 | // which enforces and documents the required kernel cherrypicks will make it pretty unlikely that |
| 675 | // many devices upgrading to S will end up relying on these fallback programs. |
| 676 | |
Maciej Żenczykowski | acddd4f | 2021-03-09 21:43:48 -0800 | [diff] [blame] | 677 | // RAWIP: Required for 5.4-R kernels -- which always support bpf_skb_change_head(). |
| 678 | |
| 679 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$5_4", AID_ROOT, AID_NETWORK_STACK, |
| 680 | sched_cls_tether_downstream4_rawip_5_4, KVER(5, 4, 0), KVER(5, 8, 0)) |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 681 | (struct __sk_buff* skb) { |
| 682 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true, /* updatetime */ false); |
| 683 | } |
| 684 | |
Maciej Żenczykowski | acddd4f | 2021-03-09 21:43:48 -0800 | [diff] [blame] | 685 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$5_4", AID_ROOT, AID_NETWORK_STACK, |
| 686 | sched_cls_tether_upstream4_rawip_5_4, KVER(5, 4, 0), KVER(5, 8, 0)) |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame] | 687 | (struct __sk_buff* skb) { |
| 688 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false, /* updatetime */ false); |
| 689 | } |
| 690 | |
Maciej Żenczykowski | acddd4f | 2021-03-09 21:43:48 -0800 | [diff] [blame] | 691 | // RAWIP: Optional for 4.14/4.19 (R) kernels -- which support bpf_skb_change_head(). |
| 692 | // [Note: fallback for 4.14/4.19 (P/Q) kernels is below in stub section] |
| 693 | |
| 694 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$4_14", |
| 695 | AID_ROOT, AID_NETWORK_STACK, |
| 696 | sched_cls_tether_downstream4_rawip_4_14, |
| 697 | KVER(4, 14, 0), KVER(5, 4, 0)) |
| 698 | (struct __sk_buff* skb) { |
| 699 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true, /* updatetime */ false); |
| 700 | } |
| 701 | |
| 702 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$4_14", |
| 703 | AID_ROOT, AID_NETWORK_STACK, |
| 704 | sched_cls_tether_upstream4_rawip_4_14, |
| 705 | KVER(4, 14, 0), KVER(5, 4, 0)) |
| 706 | (struct __sk_buff* skb) { |
| 707 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false, /* updatetime */ false); |
| 708 | } |
| 709 | |
| 710 | // ETHER: Required for 4.14-Q/R, 4.19-Q/R & 5.4-R kernels. |
| 711 | |
Maciej Żenczykowski | 2278aed | 2021-03-09 21:19:52 -0800 | [diff] [blame] | 712 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$4_14", AID_ROOT, AID_NETWORK_STACK, |
| 713 | sched_cls_tether_downstream4_ether_4_14, KVER(4, 14, 0), KVER(5, 8, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 714 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | 2278aed | 2021-03-09 21:19:52 -0800 | [diff] [blame] | 715 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true, /* updatetime */ false); |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Maciej Żenczykowski | 2278aed | 2021-03-09 21:19:52 -0800 | [diff] [blame] | 718 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$4_14", AID_ROOT, AID_NETWORK_STACK, |
| 719 | sched_cls_tether_upstream4_ether_4_14, KVER(4, 14, 0), KVER(5, 8, 0)) |
| 720 | (struct __sk_buff* skb) { |
| 721 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false, /* updatetime */ false); |
| 722 | } |
| 723 | |
Maciej Żenczykowski | acddd4f | 2021-03-09 21:43:48 -0800 | [diff] [blame] | 724 | // Placeholder (no-op) implementations for older Q kernels |
| 725 | |
| 726 | // RAWIP: 4.9-P/Q, 4.14-P/Q & 4.19-Q kernels -- without bpf_skb_change_head() for tc programs |
Maciej Żenczykowski | 2278aed | 2021-03-09 21:19:52 -0800 | [diff] [blame] | 727 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 728 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | acddd4f | 2021-03-09 21:43:48 -0800 | [diff] [blame] | 729 | sched_cls_tether_downstream4_rawip_stub, KVER_NONE, KVER(5, 4, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 730 | (struct __sk_buff* skb) { |
| 731 | return TC_ACT_OK; |
| 732 | } |
| 733 | |
Maciej Żenczykowski | 2278aed | 2021-03-09 21:19:52 -0800 | [diff] [blame] | 734 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | acddd4f | 2021-03-09 21:43:48 -0800 | [diff] [blame] | 735 | sched_cls_tether_upstream4_rawip_stub, KVER_NONE, KVER(5, 4, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 736 | (struct __sk_buff* skb) { |
| 737 | return TC_ACT_OK; |
| 738 | } |
| 739 | |
Maciej Żenczykowski | acddd4f | 2021-03-09 21:43:48 -0800 | [diff] [blame] | 740 | // ETHER: 4.9-P/Q kernel |
| 741 | |
Maciej Żenczykowski | 2278aed | 2021-03-09 21:19:52 -0800 | [diff] [blame] | 742 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK, |
| 743 | sched_cls_tether_downstream4_ether_stub, KVER_NONE, KVER(4, 14, 0)) |
| 744 | (struct __sk_buff* skb) { |
| 745 | return TC_ACT_OK; |
| 746 | } |
| 747 | |
| 748 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK, |
| 749 | sched_cls_tether_upstream4_ether_stub, KVER_NONE, KVER(4, 14, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 750 | (struct __sk_buff* skb) { |
| 751 | return TC_ACT_OK; |
| 752 | } |
| 753 | |
Maciej Żenczykowski | b199742 | 2021-01-20 14:31:50 -0800 | [diff] [blame] | 754 | // ----- XDP Support ----- |
| 755 | |
Maciej Żenczykowski | db2cff5 | 2021-03-01 21:22:49 -0800 | [diff] [blame] | 756 | DEFINE_BPF_MAP_GRW(tether_xdp_devmap, DEVMAP_HASH, uint32_t, uint32_t, 64, |
| 757 | AID_NETWORK_STACK) |
| 758 | |
Maciej Żenczykowski | 90b81ac | 2021-03-07 06:48:26 -0800 | [diff] [blame] | 759 | static inline __always_inline int do_xdp_forward6(struct xdp_md *ctx, const bool is_ethernet, |
| 760 | const bool downstream) { |
| 761 | return XDP_PASS; |
| 762 | } |
| 763 | |
| 764 | static inline __always_inline int do_xdp_forward4(struct xdp_md *ctx, const bool is_ethernet, |
| 765 | const bool downstream) { |
| 766 | return XDP_PASS; |
| 767 | } |
| 768 | |
| 769 | static inline __always_inline int do_xdp_forward_ether(struct xdp_md *ctx, const bool downstream) { |
| 770 | const void* data = (void*)(long)ctx->data; |
| 771 | const void* data_end = (void*)(long)ctx->data_end; |
| 772 | const struct ethhdr* eth = data; |
| 773 | |
| 774 | // Make sure we actually have an ethernet header |
| 775 | if ((void*)(eth + 1) > data_end) return XDP_PASS; |
| 776 | |
| 777 | if (eth->h_proto == htons(ETH_P_IPV6)) |
| 778 | return do_xdp_forward6(ctx, /* is_ethernet */ true, downstream); |
| 779 | if (eth->h_proto == htons(ETH_P_IP)) |
| 780 | return do_xdp_forward4(ctx, /* is_ethernet */ true, downstream); |
| 781 | |
| 782 | // Anything else we don't know how to handle... |
| 783 | return XDP_PASS; |
| 784 | } |
| 785 | |
| 786 | static inline __always_inline int do_xdp_forward_rawip(struct xdp_md *ctx, const bool downstream) { |
| 787 | const void* data = (void*)(long)ctx->data; |
| 788 | const void* data_end = (void*)(long)ctx->data_end; |
| 789 | |
| 790 | // The top nibble of both IPv4 and IPv6 headers is the IP version. |
| 791 | if (data_end - data < 1) return XDP_PASS; |
| 792 | const uint8_t v = (*(uint8_t*)data) >> 4; |
| 793 | |
| 794 | if (v == 6) return do_xdp_forward6(ctx, /* is_ethernet */ false, downstream); |
| 795 | if (v == 4) return do_xdp_forward4(ctx, /* is_ethernet */ false, downstream); |
| 796 | |
| 797 | // Anything else we don't know how to handle... |
| 798 | return XDP_PASS; |
| 799 | } |
| 800 | |
Maciej Żenczykowski | b199742 | 2021-01-20 14:31:50 -0800 | [diff] [blame] | 801 | #define DEFINE_XDP_PROG(str, func) \ |
| 802 | DEFINE_BPF_PROG_KVER(str, AID_ROOT, AID_NETWORK_STACK, func, KVER(5, 9, 0))(struct xdp_md *ctx) |
| 803 | |
| 804 | DEFINE_XDP_PROG("xdp/tether_downstream_ether", |
| 805 | xdp_tether_downstream_ether) { |
Maciej Żenczykowski | 90b81ac | 2021-03-07 06:48:26 -0800 | [diff] [blame] | 806 | return do_xdp_forward_ether(ctx, /* downstream */ true); |
Maciej Żenczykowski | b199742 | 2021-01-20 14:31:50 -0800 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | DEFINE_XDP_PROG("xdp/tether_downstream_rawip", |
| 810 | xdp_tether_downstream_rawip) { |
Maciej Żenczykowski | 90b81ac | 2021-03-07 06:48:26 -0800 | [diff] [blame] | 811 | return do_xdp_forward_rawip(ctx, /* downstream */ true); |
Maciej Żenczykowski | b199742 | 2021-01-20 14:31:50 -0800 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | DEFINE_XDP_PROG("xdp/tether_upstream_ether", |
| 815 | xdp_tether_upstream_ether) { |
Maciej Żenczykowski | 90b81ac | 2021-03-07 06:48:26 -0800 | [diff] [blame] | 816 | return do_xdp_forward_ether(ctx, /* downstream */ false); |
Maciej Żenczykowski | b199742 | 2021-01-20 14:31:50 -0800 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | DEFINE_XDP_PROG("xdp/tether_upstream_rawip", |
| 820 | xdp_tether_upstream_rawip) { |
Maciej Żenczykowski | 90b81ac | 2021-03-07 06:48:26 -0800 | [diff] [blame] | 821 | return do_xdp_forward_rawip(ctx, /* downstream */ false); |
Maciej Żenczykowski | b199742 | 2021-01-20 14:31:50 -0800 | [diff] [blame] | 822 | } |
| 823 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 824 | LICENSE("Apache 2.0"); |
Maciej Żenczykowski | 607d6dd | 2021-02-25 19:09:47 -0800 | [diff] [blame] | 825 | CRITICAL("tethering"); |