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