Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/if.h> |
| 18 | #include <linux/ip.h> |
| 19 | #include <linux/ipv6.h> |
| 20 | #include <linux/pkt_cls.h> |
| 21 | #include <linux/tcp.h> |
| 22 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 23 | // bionic kernel uapi linux/udp.h header is munged... |
| 24 | #define __kernel_udphdr udphdr |
| 25 | #include <linux/udp.h> |
| 26 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 27 | #include "bpf_helpers.h" |
| 28 | #include "bpf_net_helpers.h" |
Lorenzo Colitti | b81584d | 2021-02-06 00:00:58 +0900 | [diff] [blame] | 29 | #include "bpf_tethering.h" |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 30 | #include "netdbpf/bpf_shared.h" |
| 31 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 32 | // From kernel:include/net/ip.h |
| 33 | #define IP_DF 0x4000 // Flag: "Don't Fragment" |
| 34 | |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 35 | // ----- Helper functions for offsets to fields ----- |
| 36 | |
| 37 | // They all assume simple IP packets: |
| 38 | // - no VLAN ethernet tags |
| 39 | // - no IPv4 options (see IPV4_HLEN/TCP4_OFFSET/UDP4_OFFSET) |
| 40 | // - no IPv6 extension headers |
| 41 | // - no TCP options (see TCP_HLEN) |
| 42 | |
| 43 | //#define ETH_HLEN sizeof(struct ethhdr) |
| 44 | #define IP4_HLEN sizeof(struct iphdr) |
| 45 | #define IP6_HLEN sizeof(struct ipv6hdr) |
| 46 | #define TCP_HLEN sizeof(struct tcphdr) |
| 47 | #define UDP_HLEN sizeof(struct udphdr) |
| 48 | |
| 49 | // Offsets from beginning of L4 (TCP/UDP) header |
| 50 | #define TCP_OFFSET(field) offsetof(struct tcphdr, field) |
| 51 | #define UDP_OFFSET(field) offsetof(struct udphdr, field) |
| 52 | |
| 53 | // Offsets from beginning of L3 (IPv4) header |
| 54 | #define IP4_OFFSET(field) offsetof(struct iphdr, field) |
| 55 | #define IP4_TCP_OFFSET(field) (IP4_HLEN + TCP_OFFSET(field)) |
| 56 | #define IP4_UDP_OFFSET(field) (IP4_HLEN + UDP_OFFSET(field)) |
| 57 | |
| 58 | // Offsets from beginning of L3 (IPv6) header |
| 59 | #define IP6_OFFSET(field) offsetof(struct ipv6hdr, field) |
| 60 | #define IP6_TCP_OFFSET(field) (IP6_HLEN + TCP_OFFSET(field)) |
| 61 | #define IP6_UDP_OFFSET(field) (IP6_HLEN + UDP_OFFSET(field)) |
| 62 | |
| 63 | // Offsets from beginning of L2 (ie. Ethernet) header (which must be present) |
| 64 | #define ETH_IP4_OFFSET(field) (ETH_HLEN + IP4_OFFSET(field)) |
| 65 | #define ETH_IP4_TCP_OFFSET(field) (ETH_HLEN + IP4_TCP_OFFSET(field)) |
| 66 | #define ETH_IP4_UDP_OFFSET(field) (ETH_HLEN + IP4_UDP_OFFSET(field)) |
| 67 | #define ETH_IP6_OFFSET(field) (ETH_HLEN + IP6_OFFSET(field)) |
| 68 | #define ETH_IP6_TCP_OFFSET(field) (ETH_HLEN + IP6_TCP_OFFSET(field)) |
| 69 | #define ETH_IP6_UDP_OFFSET(field) (ETH_HLEN + IP6_UDP_OFFSET(field)) |
| 70 | |
| 71 | // ----- Tethering stats and data limits ----- |
| 72 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 73 | // Tethering stats, indexed by upstream interface. |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 74 | 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] | 75 | |
| 76 | // Tethering data limit, indexed by upstream interface. |
| 77 | // (tethering allowed when stats[iif].rxBytes + stats[iif].txBytes < limit[iif]) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 78 | DEFINE_BPF_MAP_GRW(tether_limit_map, HASH, TetherLimitKey, TetherLimitValue, 16, AID_NETWORK_STACK) |
| 79 | |
| 80 | // ----- IPv6 Support ----- |
| 81 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 82 | DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, Tether6Value, 64, |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 83 | AID_NETWORK_STACK) |
| 84 | |
| 85 | DEFINE_BPF_MAP_GRW(tether_downstream64_map, HASH, TetherDownstream64Key, TetherDownstream64Value, |
| 86 | 64, AID_NETWORK_STACK) |
| 87 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 88 | DEFINE_BPF_MAP_GRW(tether_upstream6_map, HASH, TetherUpstream6Key, Tether6Value, 64, |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 89 | AID_NETWORK_STACK) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 90 | |
Lorenzo Colitti | b81584d | 2021-02-06 00:00:58 +0900 | [diff] [blame] | 91 | DEFINE_BPF_MAP_GRW(tether_error_map, ARRAY, __u32, __u32, BPF_TETHER_ERR__MAX, |
| 92 | AID_NETWORK_STACK) |
| 93 | |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 94 | #define COUNT_AND_RETURN(counter, ret) do { \ |
| 95 | __u32 code = BPF_TETHER_ERR_ ## counter; \ |
| 96 | __u32 *count = bpf_tether_error_map_lookup_elem(&code); \ |
| 97 | if (count) __sync_fetch_and_add(count, 1); \ |
| 98 | return ret; \ |
Lorenzo Colitti | b81584d | 2021-02-06 00:00:58 +0900 | [diff] [blame] | 99 | } while(0) |
| 100 | |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 101 | #define DROP(counter) COUNT_AND_RETURN(counter, TC_ACT_SHOT) |
| 102 | #define PUNT(counter) COUNT_AND_RETURN(counter, TC_ACT_OK) |
| 103 | |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 104 | 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] | 105 | const bool downstream) { |
| 106 | const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 107 | void* data = (void*)(long)skb->data; |
| 108 | const void* data_end = (void*)(long)skb->data_end; |
| 109 | struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet |
| 110 | struct ipv6hdr* ip6 = is_ethernet ? (void*)(eth + 1) : data; |
| 111 | |
Maciej Żenczykowski | 18552e8 | 2021-01-24 19:59:05 -0800 | [diff] [blame] | 112 | // Require ethernet dst mac address to be our unicast address. |
| 113 | if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_OK; |
| 114 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 115 | // Must be meta-ethernet IPv6 frame |
| 116 | if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_OK; |
| 117 | |
| 118 | // Must have (ethernet and) ipv6 header |
| 119 | if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_OK; |
| 120 | |
| 121 | // Ethertype - if present - must be IPv6 |
| 122 | if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_OK; |
| 123 | |
| 124 | // IP version must be 6 |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 125 | if (ip6->version != 6) PUNT(INVALID_IP_VERSION); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 126 | |
| 127 | // Cannot decrement during forward if already zero or would be zero, |
| 128 | // Let the kernel's stack handle these cases and generate appropriate ICMP errors. |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 129 | if (ip6->hop_limit <= 1) PUNT(LOW_TTL); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 130 | |
Maciej Żenczykowski | fc4f654 | 2021-01-22 22:19:45 -0800 | [diff] [blame] | 131 | // If hardware offload is running and programming flows based on conntrack entries, |
| 132 | // try not to interfere with it. |
| 133 | if (ip6->nexthdr == IPPROTO_TCP) { |
| 134 | struct tcphdr* tcph = (void*)(ip6 + 1); |
| 135 | |
| 136 | // Make sure we can get at the tcp header |
Lorenzo Colitti | b81584d | 2021-02-06 00:00:58 +0900 | [diff] [blame] | 137 | if (data + l2_header_size + sizeof(*ip6) + sizeof(*tcph) > data_end) |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 138 | PUNT(INVALID_TCP_HEADER); |
Maciej Żenczykowski | fc4f654 | 2021-01-22 22:19:45 -0800 | [diff] [blame] | 139 | |
| 140 | // Do not offload TCP packets with any one of the SYN/FIN/RST flags |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 141 | if (tcph->syn || tcph->fin || tcph->rst) PUNT(TCP_CONTROL_PACKET); |
Maciej Żenczykowski | fc4f654 | 2021-01-22 22:19:45 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 144 | // Protect against forwarding packets sourced from ::1 or fe80::/64 or other weirdness. |
| 145 | __be32 src32 = ip6->saddr.s6_addr32[0]; |
| 146 | if (src32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP |
| 147 | (src32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 148 | PUNT(NON_GLOBAL_SRC); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 149 | |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 150 | // Protect against forwarding packets destined to ::1 or fe80::/64 or other weirdness. |
| 151 | __be32 dst32 = ip6->daddr.s6_addr32[0]; |
| 152 | if (dst32 != htonl(0x0064ff9b) && // 64:ff9b:/32 incl. XLAT464 WKP |
| 153 | (dst32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 154 | PUNT(NON_GLOBAL_DST); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 155 | |
| 156 | // In the upstream direction do not forward traffic within the same /64 subnet. |
| 157 | if (!downstream && (src32 == dst32) && (ip6->saddr.s6_addr32[1] == ip6->daddr.s6_addr32[1])) |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 158 | PUNT(LOCAL_SRC_DST); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 159 | |
| 160 | TetherDownstream6Key kd = { |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 161 | .iif = skb->ifindex, |
| 162 | .neigh6 = ip6->daddr, |
| 163 | }; |
| 164 | |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 165 | TetherUpstream6Key ku = { |
| 166 | .iif = skb->ifindex, |
| 167 | }; |
| 168 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 169 | Tether6Value* v = downstream ? bpf_tether_downstream6_map_lookup_elem(&kd) |
| 170 | : bpf_tether_upstream6_map_lookup_elem(&ku); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 171 | |
| 172 | // If we don't find any offload information then simply let the core stack handle it... |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 173 | if (!v) return TC_ACT_OK; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 174 | |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 175 | uint32_t stat_and_limit_k = downstream ? skb->ifindex : v->oif; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 176 | |
| 177 | TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k); |
| 178 | |
| 179 | // If we don't have anywhere to put stats, then abort... |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 180 | if (!stat_v) PUNT(NO_STATS_ENTRY); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 181 | |
| 182 | uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k); |
| 183 | |
| 184 | // If we don't have a limit, then abort... |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 185 | if (!limit_v) PUNT(NO_LIMIT_ENTRY); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 186 | |
| 187 | // Required IPv6 minimum mtu is 1280, below that not clear what we should do, abort... |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 188 | if (v->pmtu < IPV6_MIN_MTU) PUNT(BELOW_IPV6_MTU); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 189 | |
| 190 | // Approximate handling of TCP/IPv6 overhead for incoming LRO/GRO packets: default |
| 191 | // outbound path mtu of 1500 is not necessarily correct, but worst case we simply |
| 192 | // undercount, which is still better then not accounting for this overhead at all. |
| 193 | // Note: this really shouldn't be device/path mtu at all, but rather should be |
| 194 | // derived from this particular connection's mss (ie. from gro segment size). |
| 195 | // This would require a much newer kernel with newer ebpf accessors. |
| 196 | // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header) |
| 197 | uint64_t packets = 1; |
| 198 | uint64_t bytes = skb->len; |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 199 | if (bytes > v->pmtu) { |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 200 | const int tcp_overhead = sizeof(struct ipv6hdr) + sizeof(struct tcphdr) + 12; |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 201 | const int mss = v->pmtu - tcp_overhead; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 202 | const uint64_t payload = bytes - tcp_overhead; |
| 203 | packets = (payload + mss - 1) / mss; |
| 204 | bytes = tcp_overhead * packets + payload; |
| 205 | } |
| 206 | |
| 207 | // Are we past the limit? If so, then abort... |
| 208 | // Note: will not overflow since u64 is 936 years even at 5Gbps. |
| 209 | // Do not drop here. Offload is just that, whenever we fail to handle |
| 210 | // a packet we let the core stack deal with things. |
| 211 | // (The core stack needs to handle limits correctly anyway, |
| 212 | // since we don't offload all traffic in both directions) |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 213 | if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) PUNT(LIMIT_REACHED); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 214 | |
| 215 | if (!is_ethernet) { |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 216 | // Try to inject an ethernet header, and simply return if we fail. |
| 217 | // We do this even if TX interface is RAWIP and thus does not need an ethernet header, |
| 218 | // because this is easier and the kernel will strip extraneous ethernet header. |
| 219 | if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) { |
| 220 | __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1); |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 221 | PUNT(CHANGE_HEAD_FAILED); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // bpf_skb_change_head() invalidates all pointers - reload them |
| 225 | data = (void*)(long)skb->data; |
| 226 | data_end = (void*)(long)skb->data_end; |
| 227 | eth = data; |
| 228 | ip6 = (void*)(eth + 1); |
| 229 | |
| 230 | // 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] | 231 | if (data + sizeof(struct ethhdr) + sizeof(*ip6) > data_end) { |
| 232 | __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1); |
Lorenzo Colitti | 72ec3ba | 2021-02-09 11:47:46 +0900 | [diff] [blame] | 233 | DROP(TOO_SHORT); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 234 | } |
| 235 | }; |
| 236 | |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 237 | // At this point we always have an ethernet header - which will get stripped by the |
| 238 | // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid. |
| 239 | // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct. |
| 240 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 241 | // CHECKSUM_COMPLETE is a 16-bit one's complement sum, |
| 242 | // thus corrections for it need to be done in 16-byte chunks at even offsets. |
| 243 | // IPv6 nexthdr is at offset 6, while hop limit is at offset 7 |
| 244 | uint8_t old_hl = ip6->hop_limit; |
| 245 | --ip6->hop_limit; |
| 246 | uint8_t new_hl = ip6->hop_limit; |
| 247 | |
| 248 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 249 | // (-ENOTSUPP) if it isn't. |
| 250 | bpf_csum_update(skb, 0xFFFF - ntohs(old_hl) + ntohs(new_hl)); |
| 251 | |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 252 | __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets); |
| 253 | __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 254 | |
| 255 | // Overwrite any mac header with the new one |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 256 | // 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] | 257 | *eth = v->macHeader; |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 258 | |
| 259 | // Redirect to forwarded interface. |
| 260 | // |
| 261 | // Note that bpf_redirect() cannot fail unless you pass invalid flags. |
| 262 | // The redirect actually happens after the ebpf program has already terminated, |
| 263 | // and can fail for example for mtu reasons at that point in time, but there's nothing |
| 264 | // we can do about it here. |
Maciej Żenczykowski | 7dfbcf5 | 2021-01-26 16:08:57 -0800 | [diff] [blame] | 265 | return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 266 | } |
| 267 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 268 | DEFINE_BPF_PROG("schedcls/tether_downstream6_ether", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | 770e0a7 | 2021-01-18 20:14:03 -0800 | [diff] [blame] | 269 | sched_cls_tether_downstream6_ether) |
Maciej Żenczykowski | 6b7829f | 2021-01-18 00:03:37 -0800 | [diff] [blame] | 270 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 271 | return do_forward6(skb, /* is_ethernet */ true, /* downstream */ true); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 274 | DEFINE_BPF_PROG("schedcls/tether_upstream6_ether", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 275 | sched_cls_tether_upstream6_ether) |
| 276 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 277 | return do_forward6(skb, /* is_ethernet */ true, /* downstream */ false); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Note: section names must be unique to prevent programs from appending to each other, |
| 281 | // so instead the bpf loader will strip everything past the final $ symbol when actually |
| 282 | // pinning the program into the filesystem. |
| 283 | // |
| 284 | // bpf_skb_change_head() is only present on 4.14+ and 2 trivial kernel patches are needed: |
| 285 | // ANDROID: net: bpf: Allow TC programs to call BPF_FUNC_skb_change_head |
| 286 | // ANDROID: net: bpf: permit redirect from ingress L3 to egress L2 devices at near max mtu |
| 287 | // (the first of those has already been upstreamed) |
| 288 | // |
| 289 | // 5.4 kernel support was only added to Android Common Kernel in R, |
| 290 | // and thus a 5.4 kernel always supports this. |
| 291 | // |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 292 | // Hence, these mandatory (must load successfully) implementations for 5.4+ kernels: |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 293 | DEFINE_BPF_PROG_KVER("schedcls/tether_downstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | 770e0a7 | 2021-01-18 20:14:03 -0800 | [diff] [blame] | 294 | sched_cls_tether_downstream6_rawip_5_4, KVER(5, 4, 0)) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 295 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 296 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 297 | } |
| 298 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 299 | DEFINE_BPF_PROG_KVER("schedcls/tether_upstream6_rawip$5_4", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 300 | sched_cls_tether_upstream6_rawip_5_4, KVER(5, 4, 0)) |
| 301 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 302 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // and these identical optional (may fail to load) implementations for [4.14..5.4) patched kernels: |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 306 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$4_14", |
| 307 | AID_ROOT, AID_NETWORK_STACK, |
| 308 | sched_cls_tether_downstream6_rawip_4_14, |
| 309 | KVER(4, 14, 0), KVER(5, 4, 0)) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 310 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 311 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ true); |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 312 | } |
| 313 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 314 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$4_14", |
| 315 | AID_ROOT, AID_NETWORK_STACK, |
| 316 | sched_cls_tether_upstream6_rawip_4_14, |
| 317 | KVER(4, 14, 0), KVER(5, 4, 0)) |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 318 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | bf8ec1a | 2021-01-24 19:56:39 -0800 | [diff] [blame] | 319 | return do_forward6(skb, /* is_ethernet */ false, /* downstream */ false); |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // and define no-op stubs for [4.9,4.14) and unpatched [4.14,5.4) kernels. |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 323 | // (if the above real 4.14+ program loaded successfully, then bpfloader will have already pinned |
| 324 | // it at the same location this one would be pinned at and will thus skip loading this stub) |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 325 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | 770e0a7 | 2021-01-18 20:14:03 -0800 | [diff] [blame] | 326 | sched_cls_tether_downstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0)) |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 327 | (struct __sk_buff* skb) { |
| 328 | return TC_ACT_OK; |
| 329 | } |
| 330 | |
Maciej Żenczykowski | 5b00fbd | 2021-01-19 23:37:51 -0800 | [diff] [blame] | 331 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream6_rawip$stub", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | bca0c85 | 2021-01-19 01:22:17 -0800 | [diff] [blame] | 332 | sched_cls_tether_upstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0)) |
| 333 | (struct __sk_buff* skb) { |
| 334 | return TC_ACT_OK; |
| 335 | } |
| 336 | |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 337 | // ----- IPv4 Support ----- |
| 338 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 339 | DEFINE_BPF_MAP_GRW(tether_downstream4_map, HASH, Tether4Key, Tether4Value, 64, AID_NETWORK_STACK) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 340 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 341 | DEFINE_BPF_MAP_GRW(tether_upstream4_map, HASH, Tether4Key, Tether4Value, 64, AID_NETWORK_STACK) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 342 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 343 | static inline __always_inline int do_forward4(struct __sk_buff* skb, const bool is_ethernet, |
Maciej Żenczykowski | aefa095 | 2021-02-14 23:15:19 -0800 | [diff] [blame] | 344 | const bool downstream, const bool updatetime) { |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 345 | const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0; |
| 346 | void* data = (void*)(long)skb->data; |
| 347 | const void* data_end = (void*)(long)skb->data_end; |
| 348 | struct ethhdr* eth = is_ethernet ? data : NULL; // used iff is_ethernet |
| 349 | struct iphdr* ip = is_ethernet ? (void*)(eth + 1) : data; |
| 350 | |
| 351 | // Require ethernet dst mac address to be our unicast address. |
| 352 | if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_OK; |
| 353 | |
| 354 | // Must be meta-ethernet IPv4 frame |
| 355 | if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_OK; |
| 356 | |
| 357 | // Must have (ethernet and) ipv4 header |
| 358 | if (data + l2_header_size + sizeof(*ip) > data_end) return TC_ACT_OK; |
| 359 | |
| 360 | // Ethertype - if present - must be IPv4 |
| 361 | if (is_ethernet && (eth->h_proto != htons(ETH_P_IP))) return TC_ACT_OK; |
| 362 | |
| 363 | // IP version must be 4 |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 364 | if (ip->version != 4) PUNT(INVALID_IP_VERSION); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 365 | |
| 366 | // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 367 | if (ip->ihl != 5) PUNT(HAS_IP_OPTIONS); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 368 | |
| 369 | // Calculate the IPv4 one's complement checksum of the IPv4 header. |
| 370 | __wsum sum4 = 0; |
| 371 | for (int i = 0; i < sizeof(*ip) / sizeof(__u16); ++i) { |
| 372 | sum4 += ((__u16*)ip)[i]; |
| 373 | } |
| 374 | // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4 |
| 375 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE |
| 376 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16 |
| 377 | // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 378 | if (sum4 != 0xFFFF) PUNT(CHECKSUM); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 379 | |
| 380 | // Minimum IPv4 total length is the size of the header |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 381 | if (ntohs(ip->tot_len) < sizeof(*ip)) PUNT(TRUNCATED_IPV4); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 382 | |
| 383 | // We are incapable of dealing with IPv4 fragments |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 384 | if (ip->frag_off & ~htons(IP_DF)) PUNT(IS_IP_FRAG); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 385 | |
| 386 | // Cannot decrement during forward if already zero or would be zero, |
| 387 | // Let the kernel's stack handle these cases and generate appropriate ICMP errors. |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 388 | if (ip->ttl <= 1) PUNT(LOW_TTL); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 389 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame^] | 390 | // If we cannot update the 'last_used' field due to lack of bpf_ktime_get_boot_ns() helper, |
| 391 | // then it is not safe to offload UDP due to the small conntrack timeouts, as such, |
| 392 | // in such a situation we can only support TCP. This also has the added nice benefit of |
| 393 | // using a separate error counter, and thus making it obvious which version of the program |
| 394 | // is loaded. |
| 395 | if (!updatetime && ip->protocol != IPPROTO_TCP) PUNT(NON_TCP); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 396 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame^] | 397 | // We do not support offloading anything besides IPv4 TCP and UDP, due to need for NAT, |
| 398 | // but no need to check this if !updatetime due to check immediately above. |
| 399 | if (updatetime && (ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP)) |
| 400 | PUNT(NON_TCP_UDP); |
| 401 | |
| 402 | // We want to make sure that the compiler will, in the !updatetime case, entirely optimize |
| 403 | // out all the non-tcp logic. Also note that at this point is_udp === !is_tcp. |
| 404 | const bool is_tcp = !updatetime || (ip->protocol == IPPROTO_TCP); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 405 | |
| 406 | struct tcphdr* tcph = is_tcp ? (void*)(ip + 1) : NULL; |
| 407 | struct udphdr* udph = is_tcp ? NULL : (void*)(ip + 1); |
| 408 | |
| 409 | if (is_tcp) { |
| 410 | // Make sure we can get at the tcp header |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 411 | if (data + l2_header_size + sizeof(*ip) + sizeof(*tcph) > data_end) PUNT(SHORT_TCP_HEADER); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 412 | |
| 413 | // If hardware offload is running and programming flows based on conntrack entries, try not |
| 414 | // to interfere with it, so do not offload TCP packets with any one of the SYN/FIN/RST flags |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 415 | if (tcph->syn || tcph->fin || tcph->rst) PUNT(TCP_CONTROL_PACKET); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 416 | } else { // UDP |
| 417 | // Make sure we can get at the udp header |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 418 | if (data + l2_header_size + sizeof(*ip) + sizeof(*udph) > data_end) PUNT(SHORT_UDP_HEADER); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 421 | Tether4Key k = { |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 422 | .iif = skb->ifindex, |
| 423 | .l4Proto = ip->protocol, |
| 424 | .src4.s_addr = ip->saddr, |
| 425 | .dst4.s_addr = ip->daddr, |
| 426 | .srcPort = is_tcp ? tcph->source : udph->source, |
| 427 | .dstPort = is_tcp ? tcph->dest : udph->dest, |
| 428 | }; |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 429 | if (is_ethernet) for (int i = 0; i < ETH_ALEN; ++i) k.dstMac[i] = eth->h_dest[i]; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 430 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 431 | Tether4Value* v = downstream ? bpf_tether_downstream4_map_lookup_elem(&k) |
| 432 | : bpf_tether_upstream4_map_lookup_elem(&k); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 433 | |
| 434 | // If we don't find any offload information then simply let the core stack handle it... |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 435 | if (!v) return TC_ACT_OK; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 436 | |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 437 | uint32_t stat_and_limit_k = downstream ? skb->ifindex : v->oif; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 438 | |
| 439 | TetherStatsValue* stat_v = bpf_tether_stats_map_lookup_elem(&stat_and_limit_k); |
| 440 | |
| 441 | // If we don't have anywhere to put stats, then abort... |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 442 | if (!stat_v) PUNT(NO_STATS_ENTRY); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 443 | |
| 444 | uint64_t* limit_v = bpf_tether_limit_map_lookup_elem(&stat_and_limit_k); |
| 445 | |
| 446 | // If we don't have a limit, then abort... |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 447 | if (!limit_v) PUNT(NO_LIMIT_ENTRY); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 448 | |
| 449 | // Required IPv4 minimum mtu is 68, below that not clear what we should do, abort... |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 450 | if (v->pmtu < 68) PUNT(BELOW_IPV4_MTU); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 451 | |
| 452 | // Approximate handling of TCP/IPv4 overhead for incoming LRO/GRO packets: default |
| 453 | // outbound path mtu of 1500 is not necessarily correct, but worst case we simply |
| 454 | // undercount, which is still better then not accounting for this overhead at all. |
| 455 | // Note: this really shouldn't be device/path mtu at all, but rather should be |
| 456 | // derived from this particular connection's mss (ie. from gro segment size). |
| 457 | // This would require a much newer kernel with newer ebpf accessors. |
| 458 | // (This is also blindly assuming 12 bytes of tcp timestamp option in tcp header) |
| 459 | uint64_t packets = 1; |
| 460 | uint64_t bytes = skb->len; |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 461 | if (bytes > v->pmtu) { |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 462 | const int tcp_overhead = sizeof(struct iphdr) + sizeof(struct tcphdr) + 12; |
Maciej Żenczykowski | 1feb8b4 | 2021-01-25 12:01:31 -0800 | [diff] [blame] | 463 | const int mss = v->pmtu - tcp_overhead; |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 464 | const uint64_t payload = bytes - tcp_overhead; |
| 465 | packets = (payload + mss - 1) / mss; |
| 466 | bytes = tcp_overhead * packets + payload; |
| 467 | } |
| 468 | |
| 469 | // Are we past the limit? If so, then abort... |
| 470 | // Note: will not overflow since u64 is 936 years even at 5Gbps. |
| 471 | // Do not drop here. Offload is just that, whenever we fail to handle |
| 472 | // a packet we let the core stack deal with things. |
| 473 | // (The core stack needs to handle limits correctly anyway, |
| 474 | // since we don't offload all traffic in both directions) |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 475 | if (stat_v->rxBytes + stat_v->txBytes + bytes > *limit_v) PUNT(LIMIT_REACHED); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 476 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame^] | 477 | if (!is_tcp) PUNT(NON_TCP); // TEMP HACK: will remove once UDP is supported. |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 478 | |
| 479 | if (!is_ethernet) { |
| 480 | // Try to inject an ethernet header, and simply return if we fail. |
| 481 | // We do this even if TX interface is RAWIP and thus does not need an ethernet header, |
| 482 | // because this is easier and the kernel will strip extraneous ethernet header. |
| 483 | if (bpf_skb_change_head(skb, sizeof(struct ethhdr), /*flags*/ 0)) { |
| 484 | __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1); |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 485 | PUNT(CHANGE_HEAD_FAILED); |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // bpf_skb_change_head() invalidates all pointers - reload them |
| 489 | data = (void*)(long)skb->data; |
| 490 | data_end = (void*)(long)skb->data_end; |
| 491 | eth = data; |
| 492 | ip = (void*)(eth + 1); |
| 493 | tcph = is_tcp ? (void*)(ip + 1) : NULL; |
| 494 | udph = is_tcp ? NULL : (void*)(ip + 1); |
| 495 | |
| 496 | // I do not believe this can ever happen, but keep the verifier happy... |
| 497 | if (data + sizeof(struct ethhdr) + sizeof(*ip) + (is_tcp ? sizeof(*tcph) : sizeof(*udph)) > data_end) { |
| 498 | __sync_fetch_and_add(downstream ? &stat_v->rxErrors : &stat_v->txErrors, 1); |
Lorenzo Colitti | d561b7f | 2021-02-09 13:20:29 +0900 | [diff] [blame] | 499 | DROP(TOO_SHORT); |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 500 | } |
| 501 | }; |
| 502 | |
| 503 | // At this point we always have an ethernet header - which will get stripped by the |
| 504 | // kernel during transmit through a rawip interface. ie. 'eth' pointer is valid. |
| 505 | // Additionally note that 'is_ethernet' and 'l2_header_size' are no longer correct. |
| 506 | |
| 507 | // Overwrite any mac header with the new one |
| 508 | // For a rawip tx interface it will simply be a bunch of zeroes and later stripped. |
| 509 | *eth = v->macHeader; |
| 510 | |
| 511 | const int sz4 = sizeof(__be32); |
| 512 | const __be32 old_daddr = k.dst4.s_addr; |
| 513 | const __be32 old_saddr = k.src4.s_addr; |
| 514 | const __be32 new_daddr = v->dst46.s6_addr32[3]; |
| 515 | const __be32 new_saddr = v->src46.s6_addr32[3]; |
| 516 | |
| 517 | bpf_l4_csum_replace(skb, ETH_IP4_TCP_OFFSET(check), old_daddr, new_daddr, sz4 | BPF_F_PSEUDO_HDR); |
| 518 | bpf_l3_csum_replace(skb, ETH_IP4_OFFSET(check), old_daddr, new_daddr, sz4); |
| 519 | bpf_skb_store_bytes(skb, ETH_IP4_OFFSET(daddr), &new_daddr, sz4, 0); |
| 520 | |
| 521 | bpf_l4_csum_replace(skb, ETH_IP4_TCP_OFFSET(check), old_saddr, new_saddr, sz4 | BPF_F_PSEUDO_HDR); |
| 522 | bpf_l3_csum_replace(skb, ETH_IP4_OFFSET(check), old_saddr, new_saddr, sz4); |
| 523 | bpf_skb_store_bytes(skb, ETH_IP4_OFFSET(saddr), &new_saddr, sz4, 0); |
| 524 | |
| 525 | const int sz2 = sizeof(__be16); |
| 526 | bpf_l4_csum_replace(skb, ETH_IP4_TCP_OFFSET(check), k.srcPort, v->srcPort, sz2); |
| 527 | bpf_skb_store_bytes(skb, ETH_IP4_TCP_OFFSET(source), &v->srcPort, sz2, 0); |
| 528 | |
| 529 | bpf_l4_csum_replace(skb, ETH_IP4_TCP_OFFSET(check), k.dstPort, v->dstPort, sz2); |
| 530 | bpf_skb_store_bytes(skb, ETH_IP4_TCP_OFFSET(dest), &v->dstPort, sz2, 0); |
| 531 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame^] | 532 | // TEMP HACK: lack of TTL decrement |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 533 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame^] | 534 | // This requires the bpf_ktime_get_boot_ns() helper which was added in 5.8, |
| 535 | // and backported to all Android Common Kernel 4.14+ trees. |
Maciej Żenczykowski | aefa095 | 2021-02-14 23:15:19 -0800 | [diff] [blame] | 536 | if (updatetime) v->last_used = bpf_ktime_get_boot_ns(); |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 537 | |
| 538 | __sync_fetch_and_add(downstream ? &stat_v->rxPackets : &stat_v->txPackets, packets); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 539 | __sync_fetch_and_add(downstream ? &stat_v->rxBytes : &stat_v->txBytes, bytes); |
| 540 | |
Maciej Żenczykowski | ec5f67d | 2021-01-25 02:32:01 -0800 | [diff] [blame] | 541 | // Redirect to forwarded interface. |
| 542 | // |
| 543 | // Note that bpf_redirect() cannot fail unless you pass invalid flags. |
| 544 | // The redirect actually happens after the ebpf program has already terminated, |
| 545 | // and can fail for example for mtu reasons at that point in time, but there's nothing |
| 546 | // we can do about it here. |
| 547 | return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame^] | 550 | // Full featured (required) implementations for 5.8+ kernels |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 551 | |
Maciej Żenczykowski | b0ac41f | 2021-02-14 21:34:03 -0800 | [diff] [blame] | 552 | DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_ether$5_8", AID_ROOT, AID_NETWORK_STACK, |
| 553 | sched_cls_tether_downstream4_ether_5_8, KVER(5, 8, 0)) |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 554 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | aefa095 | 2021-02-14 23:15:19 -0800 | [diff] [blame] | 555 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true, /* updatetime */ true); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Maciej Żenczykowski | b0ac41f | 2021-02-14 21:34:03 -0800 | [diff] [blame] | 558 | DEFINE_BPF_PROG_KVER("schedcls/tether_downstream4_rawip$5_8", AID_ROOT, AID_NETWORK_STACK, |
| 559 | sched_cls_tether_downstream4_rawip_5_8, KVER(5, 8, 0)) |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 560 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | aefa095 | 2021-02-14 23:15:19 -0800 | [diff] [blame] | 561 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true, /* updatetime */ true); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Maciej Żenczykowski | b0ac41f | 2021-02-14 21:34:03 -0800 | [diff] [blame] | 564 | DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_ether$5_8", AID_ROOT, AID_NETWORK_STACK, |
| 565 | sched_cls_tether_upstream4_ether_5_8, KVER(5, 8, 0)) |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 566 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | aefa095 | 2021-02-14 23:15:19 -0800 | [diff] [blame] | 567 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false, /* updatetime */ true); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Maciej Żenczykowski | b0ac41f | 2021-02-14 21:34:03 -0800 | [diff] [blame] | 570 | DEFINE_BPF_PROG_KVER("schedcls/tether_upstream4_rawip$5_8", AID_ROOT, AID_NETWORK_STACK, |
| 571 | sched_cls_tether_upstream4_rawip_5_8, KVER(5, 8, 0)) |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 572 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | aefa095 | 2021-02-14 23:15:19 -0800 | [diff] [blame] | 573 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false, /* updatetime */ true); |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame^] | 576 | // Full featured (optional) implementations for [4.14..5.8) kernels |
| 577 | |
| 578 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$opt", |
| 579 | AID_ROOT, AID_NETWORK_STACK, |
| 580 | sched_cls_tether_downstream4_ether_opt, |
| 581 | KVER(4, 14, 0), KVER(5, 8, 0)) |
| 582 | (struct __sk_buff* skb) { |
| 583 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true, /* updatetime */ true); |
| 584 | } |
| 585 | |
| 586 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$opt", |
| 587 | AID_ROOT, AID_NETWORK_STACK, |
| 588 | sched_cls_tether_downstream4_rawip_opt, |
| 589 | KVER(4, 14, 0), KVER(5, 8, 0)) |
| 590 | (struct __sk_buff* skb) { |
| 591 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true, /* updatetime */ true); |
| 592 | } |
| 593 | |
| 594 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$opt", |
| 595 | AID_ROOT, AID_NETWORK_STACK, |
| 596 | sched_cls_tether_upstream4_ether_opt, |
| 597 | KVER(4, 14, 0), KVER(5, 8, 0)) |
| 598 | (struct __sk_buff* skb) { |
| 599 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false, /* updatetime */ true); |
| 600 | } |
| 601 | |
| 602 | DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$opt", |
| 603 | AID_ROOT, AID_NETWORK_STACK, |
| 604 | sched_cls_tether_upstream4_rawip_opt, |
| 605 | KVER(4, 14, 0), KVER(5, 8, 0)) |
| 606 | (struct __sk_buff* skb) { |
| 607 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false, /* updatetime */ true); |
| 608 | } |
| 609 | |
| 610 | // Partial (TCP-only: will not update 'last_used' field) implementations for 4.14+ kernels. |
| 611 | // These will be loaded only if the above optional ones failed (loading of *these* must succeed). |
| 612 | // |
| 613 | // [Note: as a result TCP connections will not have their conntrack timeout refreshed, however, |
| 614 | // since /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established defaults to 432000 (seconds), |
| 615 | // this in practice means they'll break only after 5 days. This seems an acceptable trade-off. |
| 616 | // |
| 617 | // Additionally kernel/tests change "net-test: add bpf_ktime_get_ns / bpf_ktime_get_boot_ns tests" |
| 618 | // which enforces and documents the required kernel cherrypicks will make it pretty unlikely that |
| 619 | // many devices upgrading to S will end up relying on these fallback programs. |
| 620 | |
| 621 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$4_14", AID_ROOT, AID_NETWORK_STACK, |
| 622 | sched_cls_tether_downstream4_ether_4_14, KVER(4, 14, 0), KVER(5, 8, 0)) |
| 623 | (struct __sk_buff* skb) { |
| 624 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ true, /* updatetime */ false); |
| 625 | } |
| 626 | |
| 627 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$4_14", AID_ROOT, AID_NETWORK_STACK, |
| 628 | sched_cls_tether_downstream4_rawip_4_14, KVER(4, 14, 0), KVER(5, 8, 0)) |
| 629 | (struct __sk_buff* skb) { |
| 630 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ true, /* updatetime */ false); |
| 631 | } |
| 632 | |
| 633 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$4_14", AID_ROOT, AID_NETWORK_STACK, |
| 634 | sched_cls_tether_upstream4_ether_4_14, KVER(4, 14, 0), KVER(5, 8, 0)) |
| 635 | (struct __sk_buff* skb) { |
| 636 | return do_forward4(skb, /* is_ethernet */ true, /* downstream */ false, /* updatetime */ false); |
| 637 | } |
| 638 | |
| 639 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$4_14", AID_ROOT, AID_NETWORK_STACK, |
| 640 | sched_cls_tether_upstream4_rawip_4_14, KVER(4, 14, 0), KVER(5, 8, 0)) |
| 641 | (struct __sk_buff* skb) { |
| 642 | return do_forward4(skb, /* is_ethernet */ false, /* downstream */ false, /* updatetime */ false); |
| 643 | } |
| 644 | |
| 645 | // Placeholder (no-op) implementations for older pre-4.14 kernels |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 646 | |
| 647 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame^] | 648 | sched_cls_tether_downstream4_ether_stub, KVER_NONE, KVER(4, 14, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 649 | (struct __sk_buff* skb) { |
| 650 | return TC_ACT_OK; |
| 651 | } |
| 652 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 653 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame^] | 654 | sched_cls_tether_downstream4_rawip_stub, KVER_NONE, KVER(4, 14, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 655 | (struct __sk_buff* skb) { |
| 656 | return TC_ACT_OK; |
| 657 | } |
| 658 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 659 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_ether$stub", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame^] | 660 | sched_cls_tether_upstream4_ether_stub, KVER_NONE, KVER(4, 14, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 661 | (struct __sk_buff* skb) { |
| 662 | return TC_ACT_OK; |
| 663 | } |
| 664 | |
Maciej Żenczykowski | c2b0146 | 2021-01-24 21:01:29 -0800 | [diff] [blame] | 665 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_upstream4_rawip$stub", AID_ROOT, AID_NETWORK_STACK, |
Maciej Żenczykowski | 3686735 | 2021-02-15 01:53:17 -0800 | [diff] [blame^] | 666 | sched_cls_tether_upstream4_rawip_stub, KVER_NONE, KVER(4, 14, 0)) |
Maciej Żenczykowski | 088fe19 | 2021-01-20 13:34:17 -0800 | [diff] [blame] | 667 | (struct __sk_buff* skb) { |
| 668 | return TC_ACT_OK; |
| 669 | } |
| 670 | |
Maciej Żenczykowski | b199742 | 2021-01-20 14:31:50 -0800 | [diff] [blame] | 671 | // ----- XDP Support ----- |
| 672 | |
| 673 | #define DEFINE_XDP_PROG(str, func) \ |
| 674 | DEFINE_BPF_PROG_KVER(str, AID_ROOT, AID_NETWORK_STACK, func, KVER(5, 9, 0))(struct xdp_md *ctx) |
| 675 | |
| 676 | DEFINE_XDP_PROG("xdp/tether_downstream_ether", |
| 677 | xdp_tether_downstream_ether) { |
| 678 | return XDP_PASS; |
| 679 | } |
| 680 | |
| 681 | DEFINE_XDP_PROG("xdp/tether_downstream_rawip", |
| 682 | xdp_tether_downstream_rawip) { |
| 683 | return XDP_PASS; |
| 684 | } |
| 685 | |
| 686 | DEFINE_XDP_PROG("xdp/tether_upstream_ether", |
| 687 | xdp_tether_upstream_ether) { |
| 688 | return XDP_PASS; |
| 689 | } |
| 690 | |
| 691 | DEFINE_XDP_PROG("xdp/tether_upstream_rawip", |
| 692 | xdp_tether_upstream_rawip) { |
| 693 | return XDP_PASS; |
| 694 | } |
| 695 | |
Hungming Chen | 56c632c | 2020-09-10 15:42:58 +0800 | [diff] [blame] | 696 | LICENSE("Apache 2.0"); |
| 697 | CRITICAL("netd"); |