Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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/bpf.h> |
| 18 | #include <linux/if.h> |
| 19 | #include <linux/if_ether.h> |
| 20 | #include <linux/in.h> |
| 21 | #include <linux/in6.h> |
| 22 | #include <linux/ip.h> |
| 23 | #include <linux/ipv6.h> |
| 24 | #include <linux/pkt_cls.h> |
| 25 | #include <linux/swab.h> |
| 26 | #include <stdbool.h> |
| 27 | #include <stdint.h> |
| 28 | |
| 29 | // bionic kernel uapi linux/udp.h header is munged... |
| 30 | #define __kernel_udphdr udphdr |
| 31 | #include <linux/udp.h> |
| 32 | |
Maciej Żenczykowski | c5af9f2 | 2023-04-25 15:06:49 -0700 | [diff] [blame] | 33 | // The resulting .o needs to load on the Android T bpfloader |
| 34 | #define BPFLOADER_MIN_VER BPFLOADER_T_VERSION |
Maciej Żenczykowski | acebffb | 2022-05-16 16:05:15 -0700 | [diff] [blame] | 35 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 36 | #include "bpf_helpers.h" |
| 37 | #include "bpf_net_helpers.h" |
Maciej Żenczykowski | 7b452a1 | 2022-12-08 13:10:29 +0000 | [diff] [blame] | 38 | #include "clatd.h" |
Maciej Żenczykowski | ce9108f | 2022-06-15 02:02:21 -0700 | [diff] [blame] | 39 | #include "clat_mark.h" |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 40 | |
Maciej Żenczykowski | e0ddc47 | 2022-10-24 02:43:21 -0700 | [diff] [blame] | 41 | // IP flags. (from kernel's include/net/ip.h) |
| 42 | #define IP_CE 0x8000 // Flag: "Congestion" (really reserved 'evil bit') |
| 43 | #define IP_DF 0x4000 // Flag: "Don't Fragment" |
| 44 | #define IP_MF 0x2000 // Flag: "More Fragments" |
| 45 | #define IP_OFFSET 0x1FFF // "Fragment Offset" part |
| 46 | |
| 47 | // from kernel's include/net/ipv6.h |
| 48 | struct frag_hdr { |
| 49 | __u8 nexthdr; |
| 50 | __u8 reserved; // always zero |
| 51 | __be16 frag_off; // 13 bit offset, 2 bits zero, 1 bit "More Fragments" |
| 52 | __be32 identification; |
| 53 | }; |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 54 | |
| 55 | DEFINE_BPF_MAP_GRW(clat_ingress6_map, HASH, ClatIngress6Key, ClatIngress6Value, 16, AID_SYSTEM) |
| 56 | |
Maciej Żenczykowski | 9b23952 | 2023-01-03 14:59:54 +0000 | [diff] [blame] | 57 | static inline __always_inline int nat64(struct __sk_buff* skb, |
| 58 | const bool is_ethernet, |
| 59 | const unsigned kver) { |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 60 | // Require ethernet dst mac address to be our unicast address. |
| 61 | if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_PIPE; |
| 62 | |
| 63 | // Must be meta-ethernet IPv6 frame |
| 64 | if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_PIPE; |
| 65 | |
Maciej Żenczykowski | 4b1e0f8 | 2022-04-07 16:53:02 -0700 | [diff] [blame] | 66 | const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0; |
| 67 | |
| 68 | // Not clear if this is actually necessary considering we use DPA (Direct Packet Access), |
| 69 | // but we need to make sure we can read the IPv6 header reliably so that we can set |
| 70 | // skb->mark = 0xDeadC1a7 for packets we fail to offload. |
Maciej Żenczykowski | 824fb29 | 2022-04-11 23:29:46 -0700 | [diff] [blame] | 71 | try_make_writable(skb, l2_header_size + sizeof(struct ipv6hdr)); |
Maciej Żenczykowski | 4b1e0f8 | 2022-04-07 16:53:02 -0700 | [diff] [blame] | 72 | |
| 73 | void* data = (void*)(long)skb->data; |
| 74 | const void* data_end = (void*)(long)skb->data_end; |
| 75 | const struct ethhdr* const eth = is_ethernet ? data : NULL; // used iff is_ethernet |
| 76 | const struct ipv6hdr* const ip6 = is_ethernet ? (void*)(eth + 1) : data; |
| 77 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 78 | // Must have (ethernet and) ipv6 header |
| 79 | if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_PIPE; |
| 80 | |
| 81 | // Ethertype - if present - must be IPv6 |
| 82 | if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_PIPE; |
| 83 | |
| 84 | // IP version must be 6 |
| 85 | if (ip6->version != 6) return TC_ACT_PIPE; |
| 86 | |
| 87 | // Maximum IPv6 payload length that can be translated to IPv4 |
Maciej Żenczykowski | bdcb896 | 2023-03-17 18:15:34 +0000 | [diff] [blame] | 88 | // Note: technically this check is too strict for an IPv6 fragment, |
| 89 | // which by virtue of stripping the extra 8 byte fragment extension header, |
| 90 | // could thus be 8 bytes larger and still fit in an ipv4 packet post |
| 91 | // translation. However... who ever heard of receiving ~64KB frags... |
| 92 | // fragments are kind of by definition smaller than ingress device mtu, |
| 93 | // and thus, on the internet, very very unlikely to exceed 1500 bytes. |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 94 | if (ntohs(ip6->payload_len) > 0xFFFF - sizeof(struct iphdr)) return TC_ACT_PIPE; |
| 95 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 96 | ClatIngress6Key k = { |
| 97 | .iif = skb->ifindex, |
| 98 | .pfx96.in6_u.u6_addr32 = |
| 99 | { |
| 100 | ip6->saddr.in6_u.u6_addr32[0], |
| 101 | ip6->saddr.in6_u.u6_addr32[1], |
| 102 | ip6->saddr.in6_u.u6_addr32[2], |
| 103 | }, |
| 104 | .local6 = ip6->daddr, |
| 105 | }; |
| 106 | |
| 107 | ClatIngress6Value* v = bpf_clat_ingress6_map_lookup_elem(&k); |
| 108 | |
| 109 | if (!v) return TC_ACT_PIPE; |
| 110 | |
Maciej Żenczykowski | 321321c | 2022-10-24 03:08:06 -0700 | [diff] [blame] | 111 | __u8 proto = ip6->nexthdr; |
| 112 | __be16 ip_id = 0; |
| 113 | __be16 frag_off = htons(IP_DF); |
Maciej Żenczykowski | 9b23952 | 2023-01-03 14:59:54 +0000 | [diff] [blame] | 114 | __u16 tot_len = ntohs(ip6->payload_len) + sizeof(struct iphdr); // cannot overflow, see above |
| 115 | |
| 116 | if (proto == IPPROTO_FRAGMENT) { |
| 117 | // Fragment handling requires bpf_skb_adjust_room which is 4.14+ |
| 118 | if (kver < KVER_4_14) return TC_ACT_PIPE; |
| 119 | |
| 120 | // Must have (ethernet and) ipv6 header and ipv6 fragment extension header |
| 121 | if (data + l2_header_size + sizeof(*ip6) + sizeof(struct frag_hdr) > data_end) |
| 122 | return TC_ACT_PIPE; |
| 123 | const struct frag_hdr *frag = (const struct frag_hdr *)(ip6 + 1); |
| 124 | proto = frag->nexthdr; |
| 125 | // RFC6145: use bottom 16-bits of network endian 32-bit IPv6 ID field for 16-bit IPv4 field. |
| 126 | // this is equivalent to: ip_id = htons(ntohl(frag->identification)); |
| 127 | ip_id = frag->identification >> 16; |
| 128 | // Conversion of 16-bit IPv6 frag offset to 16-bit IPv4 frag offset field. |
| 129 | // IPv6 is '13 bits of offset in multiples of 8' + 2 zero bits + more fragment bit |
| 130 | // IPv4 is zero bit + don't frag bit + more frag bit + '13 bits of offset in multiples of 8' |
| 131 | frag_off = ntohs(frag->frag_off); |
| 132 | frag_off = ((frag_off & 1) << 13) | (frag_off >> 3); |
| 133 | frag_off = htons(frag_off); |
| 134 | // Note that by construction tot_len is guaranteed to not underflow here |
| 135 | tot_len -= sizeof(struct frag_hdr); |
| 136 | // This is a badly formed IPv6 packet with less payload than the size of an IPv6 Frag EH |
| 137 | if (tot_len < sizeof(struct iphdr)) return TC_ACT_PIPE; |
| 138 | } |
Maciej Żenczykowski | 321321c | 2022-10-24 03:08:06 -0700 | [diff] [blame] | 139 | |
| 140 | switch (proto) { |
Maciej Żenczykowski | 43d31bb | 2023-07-25 16:16:46 +0000 | [diff] [blame^] | 141 | case IPPROTO_TCP: // For TCP, UDP & UDPLITE the checksum neutrality of the chosen |
| 142 | case IPPROTO_UDP: // IPv6 address means there is no need to update their checksums. |
| 143 | case IPPROTO_UDPLITE: // |
| 144 | case IPPROTO_GRE: // We do not need to bother looking at GRE/ESP headers, |
| 145 | case IPPROTO_ESP: // since there is never a checksum to update. |
Hungming Chen | 6c0b1e8 | 2022-04-01 19:51:56 +0800 | [diff] [blame] | 146 | break; |
| 147 | |
| 148 | default: // do not know how to handle anything else |
| 149 | // Mark ingress non-offloaded clat packet for dropping in ip6tables bw_raw_PREROUTING. |
| 150 | // Non-offloaded clat packet is going to be handled by clat daemon and ip6tables. The |
| 151 | // duplicate one in ip6tables is not necessary. |
| 152 | skb->mark = CLAT_MARK; |
| 153 | return TC_ACT_PIPE; |
| 154 | } |
| 155 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 156 | struct ethhdr eth2; // used iff is_ethernet |
| 157 | if (is_ethernet) { |
| 158 | eth2 = *eth; // Copy over the ethernet header (src/dst mac) |
| 159 | eth2.h_proto = htons(ETH_P_IP); // But replace the ethertype |
| 160 | } |
| 161 | |
| 162 | struct iphdr ip = { |
| 163 | .version = 4, // u4 |
| 164 | .ihl = sizeof(struct iphdr) / sizeof(__u32), // u4 |
| 165 | .tos = (ip6->priority << 4) + (ip6->flow_lbl[0] >> 4), // u8 |
Maciej Żenczykowski | 9b23952 | 2023-01-03 14:59:54 +0000 | [diff] [blame] | 166 | .tot_len = htons(tot_len), // be16 |
Maciej Żenczykowski | 321321c | 2022-10-24 03:08:06 -0700 | [diff] [blame] | 167 | .id = ip_id, // be16 |
| 168 | .frag_off = frag_off, // be16 |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 169 | .ttl = ip6->hop_limit, // u8 |
Maciej Żenczykowski | 321321c | 2022-10-24 03:08:06 -0700 | [diff] [blame] | 170 | .protocol = proto, // u8 |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 171 | .check = 0, // u16 |
Maciej Żenczykowski | 321321c | 2022-10-24 03:08:06 -0700 | [diff] [blame] | 172 | .saddr = ip6->saddr.in6_u.u6_addr32[3], // be32 |
| 173 | .daddr = v->local4.s_addr, // be32 |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | // Calculate the IPv4 one's complement checksum of the IPv4 header. |
| 177 | __wsum sum4 = 0; |
| 178 | for (int i = 0; i < sizeof(ip) / sizeof(__u16); ++i) { |
| 179 | sum4 += ((__u16*)&ip)[i]; |
| 180 | } |
| 181 | // Note that sum4 is guaranteed to be non-zero by virtue of ip.version == 4 |
| 182 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE |
| 183 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16 |
| 184 | ip.check = (__u16)~sum4; // sum4 cannot be zero, so this is never 0xFFFF |
| 185 | |
| 186 | // Calculate the *negative* IPv6 16-bit one's complement checksum of the IPv6 header. |
| 187 | __wsum sum6 = 0; |
| 188 | // We'll end up with a non-zero sum due to ip6->version == 6 (which has '0' bits) |
| 189 | for (int i = 0; i < sizeof(*ip6) / sizeof(__u16); ++i) { |
| 190 | sum6 += ~((__u16*)ip6)[i]; // note the bitwise negation |
| 191 | } |
| 192 | |
| 193 | // Note that there is no L4 checksum update: we are relying on the checksum neutrality |
| 194 | // of the ipv6 address chosen by netd's ClatdController. |
| 195 | |
| 196 | // Packet mutations begin - point of no return, but if this first modification fails |
| 197 | // the packet is probably still pristine, so let clatd handle it. |
Hungming Chen | 6c0b1e8 | 2022-04-01 19:51:56 +0800 | [diff] [blame] | 198 | if (bpf_skb_change_proto(skb, htons(ETH_P_IP), 0)) { |
| 199 | // Mark ingress non-offloaded clat packet for dropping in ip6tables bw_raw_PREROUTING. |
| 200 | // Non-offloaded clat packet is going to be handled by clat daemon and ip6tables. The |
| 201 | // duplicate one in ip6tables is not necessary. |
| 202 | skb->mark = CLAT_MARK; |
| 203 | return TC_ACT_PIPE; |
| 204 | } |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 205 | |
| 206 | // This takes care of updating the skb->csum field for a CHECKSUM_COMPLETE packet. |
| 207 | // |
| 208 | // In such a case, skb->csum is a 16-bit one's complement sum of the entire payload, |
| 209 | // thus we need to subtract out the ipv6 header's sum, and add in the ipv4 header's sum. |
| 210 | // However, by construction of ip.check above the checksum of an ipv4 header is zero. |
| 211 | // Thus we only need to subtract the ipv6 header's sum, which is the same as adding |
| 212 | // in the sum of the bitwise negation of the ipv6 header. |
| 213 | // |
| 214 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 215 | // (-ENOTSUPP) if it isn't. So we just ignore the return code. |
| 216 | // |
| 217 | // if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 218 | // return (skb->csum = csum_add(skb->csum, csum)); |
| 219 | // else |
| 220 | // return -ENOTSUPP; |
| 221 | bpf_csum_update(skb, sum6); |
| 222 | |
Maciej Żenczykowski | 9b23952 | 2023-01-03 14:59:54 +0000 | [diff] [blame] | 223 | // Technically 'kver < KVER_4_14' already implies 'frag_off == htons(IP_DF)' due to logic above, |
| 224 | // thus the initial 'kver >= KVER_4_14' check here is entirely superfluous. |
| 225 | // |
| 226 | // However, we *need* the compiler (when compiling the program for 4.9) to entirely |
| 227 | // optimize out the call to bpf_skb_adjust_room() bpf helper: it's not enough for it to emit |
| 228 | // an unreachable call to it, it must *not* emit it at all (otherwise the 4.9 kernel's |
| 229 | // bpf verifier will refuse to load a program with an unknown bpf helper call) |
| 230 | // |
| 231 | // This is easiest to achieve by being very explicit in the if clause, |
| 232 | // better safe than sorry... |
| 233 | // |
| 234 | // Note: we currently have no TreeHugger coverage for 4.9-T devices (there are no such |
| 235 | // Pixel or cuttlefish devices), so likely you won't notice for months if this breaks... |
| 236 | if (kver >= KVER_4_14 && frag_off != htons(IP_DF)) { |
| 237 | // If we're converting an IPv6 Fragment, we need to trim off 8 more bytes |
| 238 | // We're beyond recovery on error here... but hard to imagine how this could fail. |
| 239 | if (bpf_skb_adjust_room(skb, -(__s32)sizeof(struct frag_hdr), BPF_ADJ_ROOM_NET, /*flags*/0)) |
| 240 | return TC_ACT_SHOT; |
| 241 | } |
| 242 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 243 | // bpf_skb_change_proto() invalidates all pointers - reload them. |
| 244 | data = (void*)(long)skb->data; |
| 245 | data_end = (void*)(long)skb->data_end; |
| 246 | |
| 247 | // I cannot think of any valid way for this error condition to trigger, however I do |
| 248 | // believe the explicit check is required to keep the in kernel ebpf verifier happy. |
| 249 | if (data + l2_header_size + sizeof(struct iphdr) > data_end) return TC_ACT_SHOT; |
| 250 | |
| 251 | if (is_ethernet) { |
| 252 | struct ethhdr* new_eth = data; |
| 253 | |
| 254 | // Copy over the updated ethernet header |
| 255 | *new_eth = eth2; |
| 256 | |
| 257 | // Copy over the new ipv4 header. |
| 258 | *(struct iphdr*)(new_eth + 1) = ip; |
| 259 | } else { |
| 260 | // Copy over the new ipv4 header without an ethernet header. |
| 261 | *(struct iphdr*)data = ip; |
| 262 | } |
| 263 | |
| 264 | // Redirect, possibly back to same interface, so tcpdump sees packet twice. |
| 265 | if (v->oif) return bpf_redirect(v->oif, BPF_F_INGRESS); |
| 266 | |
| 267 | // Just let it through, tcpdump will not see IPv4 packet. |
| 268 | return TC_ACT_PIPE; |
| 269 | } |
| 270 | |
Maciej Żenczykowski | 9b23952 | 2023-01-03 14:59:54 +0000 | [diff] [blame] | 271 | DEFINE_BPF_PROG_KVER("schedcls/ingress6/clat_ether$4_14", AID_ROOT, AID_SYSTEM, sched_cls_ingress6_clat_ether_4_14, KVER_4_14) |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 272 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | 9b23952 | 2023-01-03 14:59:54 +0000 | [diff] [blame] | 273 | return nat64(skb, ETHER, KVER_4_14); |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Maciej Żenczykowski | 9b23952 | 2023-01-03 14:59:54 +0000 | [diff] [blame] | 276 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/ingress6/clat_ether$4_9", AID_ROOT, AID_SYSTEM, sched_cls_ingress6_clat_ether_4_9, KVER_NONE, KVER_4_14) |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 277 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | 9b23952 | 2023-01-03 14:59:54 +0000 | [diff] [blame] | 278 | return nat64(skb, ETHER, KVER_NONE); |
| 279 | } |
| 280 | |
| 281 | DEFINE_BPF_PROG_KVER("schedcls/ingress6/clat_rawip$4_14", AID_ROOT, AID_SYSTEM, sched_cls_ingress6_clat_rawip_4_14, KVER_4_14) |
| 282 | (struct __sk_buff* skb) { |
| 283 | return nat64(skb, RAWIP, KVER_4_14); |
| 284 | } |
| 285 | |
| 286 | DEFINE_BPF_PROG_KVER_RANGE("schedcls/ingress6/clat_rawip$4_9", AID_ROOT, AID_SYSTEM, sched_cls_ingress6_clat_rawip_4_9, KVER_NONE, KVER_4_14) |
| 287 | (struct __sk_buff* skb) { |
| 288 | return nat64(skb, RAWIP, KVER_NONE); |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | DEFINE_BPF_MAP_GRW(clat_egress4_map, HASH, ClatEgress4Key, ClatEgress4Value, 16, AID_SYSTEM) |
| 292 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 293 | DEFINE_BPF_PROG("schedcls/egress4/clat_rawip", AID_ROOT, AID_SYSTEM, sched_cls_egress4_clat_rawip) |
| 294 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | 4b1e0f8 | 2022-04-07 16:53:02 -0700 | [diff] [blame] | 295 | // Must be meta-ethernet IPv4 frame |
| 296 | if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_PIPE; |
| 297 | |
| 298 | // Possibly not needed, but for consistency with nat64 up above |
Maciej Żenczykowski | 824fb29 | 2022-04-11 23:29:46 -0700 | [diff] [blame] | 299 | try_make_writable(skb, sizeof(struct iphdr)); |
Maciej Żenczykowski | 4b1e0f8 | 2022-04-07 16:53:02 -0700 | [diff] [blame] | 300 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 301 | void* data = (void*)(long)skb->data; |
| 302 | const void* data_end = (void*)(long)skb->data_end; |
| 303 | const struct iphdr* const ip4 = data; |
| 304 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 305 | // Must have ipv4 header |
| 306 | if (data + sizeof(*ip4) > data_end) return TC_ACT_PIPE; |
| 307 | |
| 308 | // IP version must be 4 |
| 309 | if (ip4->version != 4) return TC_ACT_PIPE; |
| 310 | |
| 311 | // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header |
| 312 | if (ip4->ihl != 5) return TC_ACT_PIPE; |
| 313 | |
| 314 | // Calculate the IPv4 one's complement checksum of the IPv4 header. |
| 315 | __wsum sum4 = 0; |
| 316 | for (int i = 0; i < sizeof(*ip4) / sizeof(__u16); ++i) { |
| 317 | sum4 += ((__u16*)ip4)[i]; |
| 318 | } |
| 319 | // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4 |
| 320 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE |
| 321 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16 |
| 322 | // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF |
| 323 | if (sum4 != 0xFFFF) return TC_ACT_PIPE; |
| 324 | |
| 325 | // Minimum IPv4 total length is the size of the header |
| 326 | if (ntohs(ip4->tot_len) < sizeof(*ip4)) return TC_ACT_PIPE; |
| 327 | |
| 328 | // We are incapable of dealing with IPv4 fragments |
| 329 | if (ip4->frag_off & ~htons(IP_DF)) return TC_ACT_PIPE; |
| 330 | |
| 331 | switch (ip4->protocol) { |
Maciej Żenczykowski | 43d31bb | 2023-07-25 16:16:46 +0000 | [diff] [blame^] | 332 | case IPPROTO_TCP: // For TCP, UDP & UDPLITE the checksum neutrality of the chosen |
| 333 | case IPPROTO_UDPLITE: // IPv6 address means there is no need to update their checksums. |
| 334 | case IPPROTO_GRE: // We do not need to bother looking at GRE/ESP headers, |
| 335 | case IPPROTO_ESP: // since there is never a checksum to update. |
| 336 | break; |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 337 | |
Maciej Żenczykowski | 43d31bb | 2023-07-25 16:16:46 +0000 | [diff] [blame^] | 338 | case IPPROTO_UDP: // See above comment, but must also have UDP header... |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 339 | if (data + sizeof(*ip4) + sizeof(struct udphdr) > data_end) return TC_ACT_PIPE; |
| 340 | const struct udphdr* uh = (const struct udphdr*)(ip4 + 1); |
| 341 | // If IPv4/UDP checksum is 0 then fallback to clatd so it can calculate the |
| 342 | // checksum. Otherwise the network or more likely the NAT64 gateway might |
| 343 | // drop the packet because in most cases IPv6/UDP packets with a zero checksum |
| 344 | // are invalid. See RFC 6935. TODO: calculate checksum via bpf_csum_diff() |
| 345 | if (!uh->check) return TC_ACT_PIPE; |
| 346 | break; |
| 347 | |
| 348 | default: // do not know how to handle anything else |
| 349 | return TC_ACT_PIPE; |
| 350 | } |
| 351 | |
| 352 | ClatEgress4Key k = { |
| 353 | .iif = skb->ifindex, |
| 354 | .local4.s_addr = ip4->saddr, |
| 355 | }; |
| 356 | |
| 357 | ClatEgress4Value* v = bpf_clat_egress4_map_lookup_elem(&k); |
| 358 | |
| 359 | if (!v) return TC_ACT_PIPE; |
| 360 | |
| 361 | // Translating without redirecting doesn't make sense. |
| 362 | if (!v->oif) return TC_ACT_PIPE; |
| 363 | |
| 364 | // This implementation is currently limited to rawip. |
| 365 | if (v->oifIsEthernet) return TC_ACT_PIPE; |
| 366 | |
| 367 | struct ipv6hdr ip6 = { |
| 368 | .version = 6, // __u8:4 |
| 369 | .priority = ip4->tos >> 4, // __u8:4 |
| 370 | .flow_lbl = {(ip4->tos & 0xF) << 4, 0, 0}, // __u8[3] |
| 371 | .payload_len = htons(ntohs(ip4->tot_len) - 20), // __be16 |
| 372 | .nexthdr = ip4->protocol, // __u8 |
| 373 | .hop_limit = ip4->ttl, // __u8 |
| 374 | .saddr = v->local6, // struct in6_addr |
| 375 | .daddr = v->pfx96, // struct in6_addr |
| 376 | }; |
| 377 | ip6.daddr.in6_u.u6_addr32[3] = ip4->daddr; |
| 378 | |
| 379 | // Calculate the IPv6 16-bit one's complement checksum of the IPv6 header. |
| 380 | __wsum sum6 = 0; |
| 381 | // We'll end up with a non-zero sum due to ip6.version == 6 |
| 382 | for (int i = 0; i < sizeof(ip6) / sizeof(__u16); ++i) { |
| 383 | sum6 += ((__u16*)&ip6)[i]; |
| 384 | } |
| 385 | |
| 386 | // Note that there is no L4 checksum update: we are relying on the checksum neutrality |
| 387 | // of the ipv6 address chosen by netd's ClatdController. |
| 388 | |
| 389 | // Packet mutations begin - point of no return, but if this first modification fails |
| 390 | // the packet is probably still pristine, so let clatd handle it. |
| 391 | if (bpf_skb_change_proto(skb, htons(ETH_P_IPV6), 0)) return TC_ACT_PIPE; |
| 392 | |
| 393 | // This takes care of updating the skb->csum field for a CHECKSUM_COMPLETE packet. |
| 394 | // |
| 395 | // In such a case, skb->csum is a 16-bit one's complement sum of the entire payload, |
| 396 | // thus we need to subtract out the ipv4 header's sum, and add in the ipv6 header's sum. |
| 397 | // However, we've already verified the ipv4 checksum is correct and thus 0. |
| 398 | // Thus we only need to add the ipv6 header's sum. |
| 399 | // |
| 400 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 401 | // (-ENOTSUPP) if it isn't. So we just ignore the return code (see above for more details). |
| 402 | bpf_csum_update(skb, sum6); |
| 403 | |
| 404 | // bpf_skb_change_proto() invalidates all pointers - reload them. |
| 405 | data = (void*)(long)skb->data; |
| 406 | data_end = (void*)(long)skb->data_end; |
| 407 | |
| 408 | // I cannot think of any valid way for this error condition to trigger, however I do |
| 409 | // believe the explicit check is required to keep the in kernel ebpf verifier happy. |
| 410 | if (data + sizeof(ip6) > data_end) return TC_ACT_SHOT; |
| 411 | |
| 412 | // Copy over the new ipv6 header without an ethernet header. |
| 413 | *(struct ipv6hdr*)data = ip6; |
| 414 | |
| 415 | // Redirect to non v4-* interface. Tcpdump only sees packet after this redirect. |
| 416 | return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); |
| 417 | } |
| 418 | |
| 419 | LICENSE("Apache 2.0"); |
Maciej Żenczykowski | c41e35d | 2022-08-04 13:58:46 +0000 | [diff] [blame] | 420 | CRITICAL("Connectivity"); |
Maciej Żenczykowski | de1342a | 2023-06-09 05:45:57 +0000 | [diff] [blame] | 421 | DISABLE_BTF_ON_USER_BUILDS(); |