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