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 | fa61d49 | 2022-05-16 16:05:15 -0700 | [diff] [blame] | 33 | // The resulting .o needs to load on the Android T bpfloader v0.12+ |
| 34 | #define BPFLOADER_MIN_VER 12u |
| 35 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 36 | #include "bpf_helpers.h" |
| 37 | #include "bpf_net_helpers.h" |
| 38 | #include "bpf_shared.h" |
| 39 | |
| 40 | // From kernel:include/net/ip.h |
| 41 | #define IP_DF 0x4000 // Flag: "Don't Fragment" |
| 42 | |
Hungming Chen | 6c0b1e8 | 2022-04-01 19:51:56 +0800 | [diff] [blame] | 43 | // Used for iptables drops ingress clat packet. Beware of clat mark change may break the device |
| 44 | // which is using the old clat mark in netd platform code. The reason is that the clat mark is a |
| 45 | // mainline constant since T+ but netd iptable rules (ex: bandwidth control, firewall, and so on) |
| 46 | // are set in stone. |
| 47 | #define CLAT_MARK 0xdeadc1a7 |
| 48 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 49 | DEFINE_BPF_MAP_GRW(clat_ingress6_map, HASH, ClatIngress6Key, ClatIngress6Value, 16, AID_SYSTEM) |
| 50 | |
| 51 | static inline __always_inline int nat64(struct __sk_buff* skb, bool is_ethernet) { |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 52 | // Require ethernet dst mac address to be our unicast address. |
| 53 | if (is_ethernet && (skb->pkt_type != PACKET_HOST)) return TC_ACT_PIPE; |
| 54 | |
| 55 | // Must be meta-ethernet IPv6 frame |
| 56 | if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_PIPE; |
| 57 | |
Maciej Żenczykowski | 4b1e0f8 | 2022-04-07 16:53:02 -0700 | [diff] [blame] | 58 | const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0; |
| 59 | |
| 60 | // Not clear if this is actually necessary considering we use DPA (Direct Packet Access), |
| 61 | // but we need to make sure we can read the IPv6 header reliably so that we can set |
| 62 | // skb->mark = 0xDeadC1a7 for packets we fail to offload. |
Maciej Żenczykowski | 824fb29 | 2022-04-11 23:29:46 -0700 | [diff] [blame] | 63 | try_make_writable(skb, l2_header_size + sizeof(struct ipv6hdr)); |
Maciej Żenczykowski | 4b1e0f8 | 2022-04-07 16:53:02 -0700 | [diff] [blame] | 64 | |
| 65 | void* data = (void*)(long)skb->data; |
| 66 | const void* data_end = (void*)(long)skb->data_end; |
| 67 | const struct ethhdr* const eth = is_ethernet ? data : NULL; // used iff is_ethernet |
| 68 | const struct ipv6hdr* const ip6 = is_ethernet ? (void*)(eth + 1) : data; |
| 69 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 70 | // Must have (ethernet and) ipv6 header |
| 71 | if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_PIPE; |
| 72 | |
| 73 | // Ethertype - if present - must be IPv6 |
| 74 | if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_PIPE; |
| 75 | |
| 76 | // IP version must be 6 |
| 77 | if (ip6->version != 6) return TC_ACT_PIPE; |
| 78 | |
| 79 | // Maximum IPv6 payload length that can be translated to IPv4 |
| 80 | if (ntohs(ip6->payload_len) > 0xFFFF - sizeof(struct iphdr)) return TC_ACT_PIPE; |
| 81 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 82 | ClatIngress6Key k = { |
| 83 | .iif = skb->ifindex, |
| 84 | .pfx96.in6_u.u6_addr32 = |
| 85 | { |
| 86 | ip6->saddr.in6_u.u6_addr32[0], |
| 87 | ip6->saddr.in6_u.u6_addr32[1], |
| 88 | ip6->saddr.in6_u.u6_addr32[2], |
| 89 | }, |
| 90 | .local6 = ip6->daddr, |
| 91 | }; |
| 92 | |
| 93 | ClatIngress6Value* v = bpf_clat_ingress6_map_lookup_elem(&k); |
| 94 | |
| 95 | if (!v) return TC_ACT_PIPE; |
| 96 | |
Hungming Chen | 6c0b1e8 | 2022-04-01 19:51:56 +0800 | [diff] [blame] | 97 | switch (ip6->nexthdr) { |
| 98 | case IPPROTO_TCP: // For TCP & UDP the checksum neutrality of the chosen IPv6 |
| 99 | case IPPROTO_UDP: // address means there is no need to update their checksums. |
| 100 | case IPPROTO_GRE: // We do not need to bother looking at GRE/ESP headers, |
| 101 | case IPPROTO_ESP: // since there is never a checksum to update. |
| 102 | break; |
| 103 | |
| 104 | default: // do not know how to handle anything else |
| 105 | // Mark ingress non-offloaded clat packet for dropping in ip6tables bw_raw_PREROUTING. |
| 106 | // Non-offloaded clat packet is going to be handled by clat daemon and ip6tables. The |
| 107 | // duplicate one in ip6tables is not necessary. |
| 108 | skb->mark = CLAT_MARK; |
| 109 | return TC_ACT_PIPE; |
| 110 | } |
| 111 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 112 | struct ethhdr eth2; // used iff is_ethernet |
| 113 | if (is_ethernet) { |
| 114 | eth2 = *eth; // Copy over the ethernet header (src/dst mac) |
| 115 | eth2.h_proto = htons(ETH_P_IP); // But replace the ethertype |
| 116 | } |
| 117 | |
| 118 | struct iphdr ip = { |
| 119 | .version = 4, // u4 |
| 120 | .ihl = sizeof(struct iphdr) / sizeof(__u32), // u4 |
| 121 | .tos = (ip6->priority << 4) + (ip6->flow_lbl[0] >> 4), // u8 |
| 122 | .tot_len = htons(ntohs(ip6->payload_len) + sizeof(struct iphdr)), // u16 |
| 123 | .id = 0, // u16 |
| 124 | .frag_off = htons(IP_DF), // u16 |
| 125 | .ttl = ip6->hop_limit, // u8 |
| 126 | .protocol = ip6->nexthdr, // u8 |
| 127 | .check = 0, // u16 |
| 128 | .saddr = ip6->saddr.in6_u.u6_addr32[3], // u32 |
| 129 | .daddr = v->local4.s_addr, // u32 |
| 130 | }; |
| 131 | |
| 132 | // Calculate the IPv4 one's complement checksum of the IPv4 header. |
| 133 | __wsum sum4 = 0; |
| 134 | for (int i = 0; i < sizeof(ip) / sizeof(__u16); ++i) { |
| 135 | sum4 += ((__u16*)&ip)[i]; |
| 136 | } |
| 137 | // Note that sum4 is guaranteed to be non-zero by virtue of ip.version == 4 |
| 138 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE |
| 139 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16 |
| 140 | ip.check = (__u16)~sum4; // sum4 cannot be zero, so this is never 0xFFFF |
| 141 | |
| 142 | // Calculate the *negative* IPv6 16-bit one's complement checksum of the IPv6 header. |
| 143 | __wsum sum6 = 0; |
| 144 | // We'll end up with a non-zero sum due to ip6->version == 6 (which has '0' bits) |
| 145 | for (int i = 0; i < sizeof(*ip6) / sizeof(__u16); ++i) { |
| 146 | sum6 += ~((__u16*)ip6)[i]; // note the bitwise negation |
| 147 | } |
| 148 | |
| 149 | // Note that there is no L4 checksum update: we are relying on the checksum neutrality |
| 150 | // of the ipv6 address chosen by netd's ClatdController. |
| 151 | |
| 152 | // Packet mutations begin - point of no return, but if this first modification fails |
| 153 | // the packet is probably still pristine, so let clatd handle it. |
Hungming Chen | 6c0b1e8 | 2022-04-01 19:51:56 +0800 | [diff] [blame] | 154 | if (bpf_skb_change_proto(skb, htons(ETH_P_IP), 0)) { |
| 155 | // Mark ingress non-offloaded clat packet for dropping in ip6tables bw_raw_PREROUTING. |
| 156 | // Non-offloaded clat packet is going to be handled by clat daemon and ip6tables. The |
| 157 | // duplicate one in ip6tables is not necessary. |
| 158 | skb->mark = CLAT_MARK; |
| 159 | return TC_ACT_PIPE; |
| 160 | } |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 161 | |
| 162 | // This takes care of updating the skb->csum field for a CHECKSUM_COMPLETE packet. |
| 163 | // |
| 164 | // In such a case, skb->csum is a 16-bit one's complement sum of the entire payload, |
| 165 | // thus we need to subtract out the ipv6 header's sum, and add in the ipv4 header's sum. |
| 166 | // However, by construction of ip.check above the checksum of an ipv4 header is zero. |
| 167 | // Thus we only need to subtract the ipv6 header's sum, which is the same as adding |
| 168 | // in the sum of the bitwise negation of the ipv6 header. |
| 169 | // |
| 170 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 171 | // (-ENOTSUPP) if it isn't. So we just ignore the return code. |
| 172 | // |
| 173 | // if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 174 | // return (skb->csum = csum_add(skb->csum, csum)); |
| 175 | // else |
| 176 | // return -ENOTSUPP; |
| 177 | bpf_csum_update(skb, sum6); |
| 178 | |
| 179 | // bpf_skb_change_proto() invalidates all pointers - reload them. |
| 180 | data = (void*)(long)skb->data; |
| 181 | data_end = (void*)(long)skb->data_end; |
| 182 | |
| 183 | // I cannot think of any valid way for this error condition to trigger, however I do |
| 184 | // believe the explicit check is required to keep the in kernel ebpf verifier happy. |
| 185 | if (data + l2_header_size + sizeof(struct iphdr) > data_end) return TC_ACT_SHOT; |
| 186 | |
| 187 | if (is_ethernet) { |
| 188 | struct ethhdr* new_eth = data; |
| 189 | |
| 190 | // Copy over the updated ethernet header |
| 191 | *new_eth = eth2; |
| 192 | |
| 193 | // Copy over the new ipv4 header. |
| 194 | *(struct iphdr*)(new_eth + 1) = ip; |
| 195 | } else { |
| 196 | // Copy over the new ipv4 header without an ethernet header. |
| 197 | *(struct iphdr*)data = ip; |
| 198 | } |
| 199 | |
| 200 | // Redirect, possibly back to same interface, so tcpdump sees packet twice. |
| 201 | if (v->oif) return bpf_redirect(v->oif, BPF_F_INGRESS); |
| 202 | |
| 203 | // Just let it through, tcpdump will not see IPv4 packet. |
| 204 | return TC_ACT_PIPE; |
| 205 | } |
| 206 | |
| 207 | DEFINE_BPF_PROG("schedcls/ingress6/clat_ether", AID_ROOT, AID_SYSTEM, sched_cls_ingress6_clat_ether) |
| 208 | (struct __sk_buff* skb) { |
| 209 | return nat64(skb, true); |
| 210 | } |
| 211 | |
| 212 | DEFINE_BPF_PROG("schedcls/ingress6/clat_rawip", AID_ROOT, AID_SYSTEM, sched_cls_ingress6_clat_rawip) |
| 213 | (struct __sk_buff* skb) { |
| 214 | return nat64(skb, false); |
| 215 | } |
| 216 | |
| 217 | DEFINE_BPF_MAP_GRW(clat_egress4_map, HASH, ClatEgress4Key, ClatEgress4Value, 16, AID_SYSTEM) |
| 218 | |
| 219 | DEFINE_BPF_PROG("schedcls/egress4/clat_ether", AID_ROOT, AID_SYSTEM, sched_cls_egress4_clat_ether) |
| 220 | (struct __sk_buff* skb) { |
| 221 | return TC_ACT_PIPE; |
| 222 | } |
| 223 | |
| 224 | DEFINE_BPF_PROG("schedcls/egress4/clat_rawip", AID_ROOT, AID_SYSTEM, sched_cls_egress4_clat_rawip) |
| 225 | (struct __sk_buff* skb) { |
Maciej Żenczykowski | 4b1e0f8 | 2022-04-07 16:53:02 -0700 | [diff] [blame] | 226 | // Must be meta-ethernet IPv4 frame |
| 227 | if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_PIPE; |
| 228 | |
| 229 | // Possibly not needed, but for consistency with nat64 up above |
Maciej Żenczykowski | 824fb29 | 2022-04-11 23:29:46 -0700 | [diff] [blame] | 230 | try_make_writable(skb, sizeof(struct iphdr)); |
Maciej Żenczykowski | 4b1e0f8 | 2022-04-07 16:53:02 -0700 | [diff] [blame] | 231 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 232 | void* data = (void*)(long)skb->data; |
| 233 | const void* data_end = (void*)(long)skb->data_end; |
| 234 | const struct iphdr* const ip4 = data; |
| 235 | |
Maciej Żenczykowski | 849b3aa | 2022-01-20 20:58:34 -0800 | [diff] [blame] | 236 | // Must have ipv4 header |
| 237 | if (data + sizeof(*ip4) > data_end) return TC_ACT_PIPE; |
| 238 | |
| 239 | // IP version must be 4 |
| 240 | if (ip4->version != 4) return TC_ACT_PIPE; |
| 241 | |
| 242 | // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header |
| 243 | if (ip4->ihl != 5) return TC_ACT_PIPE; |
| 244 | |
| 245 | // Calculate the IPv4 one's complement checksum of the IPv4 header. |
| 246 | __wsum sum4 = 0; |
| 247 | for (int i = 0; i < sizeof(*ip4) / sizeof(__u16); ++i) { |
| 248 | sum4 += ((__u16*)ip4)[i]; |
| 249 | } |
| 250 | // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4 |
| 251 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE |
| 252 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16 |
| 253 | // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF |
| 254 | if (sum4 != 0xFFFF) return TC_ACT_PIPE; |
| 255 | |
| 256 | // Minimum IPv4 total length is the size of the header |
| 257 | if (ntohs(ip4->tot_len) < sizeof(*ip4)) return TC_ACT_PIPE; |
| 258 | |
| 259 | // We are incapable of dealing with IPv4 fragments |
| 260 | if (ip4->frag_off & ~htons(IP_DF)) return TC_ACT_PIPE; |
| 261 | |
| 262 | switch (ip4->protocol) { |
| 263 | case IPPROTO_TCP: // For TCP & UDP the checksum neutrality of the chosen IPv6 |
| 264 | case IPPROTO_GRE: // address means there is no need to update their checksums. |
| 265 | case IPPROTO_ESP: // We do not need to bother looking at GRE/ESP headers, |
| 266 | break; // since there is never a checksum to update. |
| 267 | |
| 268 | case IPPROTO_UDP: // See above comment, but must also have UDP header... |
| 269 | if (data + sizeof(*ip4) + sizeof(struct udphdr) > data_end) return TC_ACT_PIPE; |
| 270 | const struct udphdr* uh = (const struct udphdr*)(ip4 + 1); |
| 271 | // If IPv4/UDP checksum is 0 then fallback to clatd so it can calculate the |
| 272 | // checksum. Otherwise the network or more likely the NAT64 gateway might |
| 273 | // drop the packet because in most cases IPv6/UDP packets with a zero checksum |
| 274 | // are invalid. See RFC 6935. TODO: calculate checksum via bpf_csum_diff() |
| 275 | if (!uh->check) return TC_ACT_PIPE; |
| 276 | break; |
| 277 | |
| 278 | default: // do not know how to handle anything else |
| 279 | return TC_ACT_PIPE; |
| 280 | } |
| 281 | |
| 282 | ClatEgress4Key k = { |
| 283 | .iif = skb->ifindex, |
| 284 | .local4.s_addr = ip4->saddr, |
| 285 | }; |
| 286 | |
| 287 | ClatEgress4Value* v = bpf_clat_egress4_map_lookup_elem(&k); |
| 288 | |
| 289 | if (!v) return TC_ACT_PIPE; |
| 290 | |
| 291 | // Translating without redirecting doesn't make sense. |
| 292 | if (!v->oif) return TC_ACT_PIPE; |
| 293 | |
| 294 | // This implementation is currently limited to rawip. |
| 295 | if (v->oifIsEthernet) return TC_ACT_PIPE; |
| 296 | |
| 297 | struct ipv6hdr ip6 = { |
| 298 | .version = 6, // __u8:4 |
| 299 | .priority = ip4->tos >> 4, // __u8:4 |
| 300 | .flow_lbl = {(ip4->tos & 0xF) << 4, 0, 0}, // __u8[3] |
| 301 | .payload_len = htons(ntohs(ip4->tot_len) - 20), // __be16 |
| 302 | .nexthdr = ip4->protocol, // __u8 |
| 303 | .hop_limit = ip4->ttl, // __u8 |
| 304 | .saddr = v->local6, // struct in6_addr |
| 305 | .daddr = v->pfx96, // struct in6_addr |
| 306 | }; |
| 307 | ip6.daddr.in6_u.u6_addr32[3] = ip4->daddr; |
| 308 | |
| 309 | // Calculate the IPv6 16-bit one's complement checksum of the IPv6 header. |
| 310 | __wsum sum6 = 0; |
| 311 | // We'll end up with a non-zero sum due to ip6.version == 6 |
| 312 | for (int i = 0; i < sizeof(ip6) / sizeof(__u16); ++i) { |
| 313 | sum6 += ((__u16*)&ip6)[i]; |
| 314 | } |
| 315 | |
| 316 | // Note that there is no L4 checksum update: we are relying on the checksum neutrality |
| 317 | // of the ipv6 address chosen by netd's ClatdController. |
| 318 | |
| 319 | // Packet mutations begin - point of no return, but if this first modification fails |
| 320 | // the packet is probably still pristine, so let clatd handle it. |
| 321 | if (bpf_skb_change_proto(skb, htons(ETH_P_IPV6), 0)) return TC_ACT_PIPE; |
| 322 | |
| 323 | // This takes care of updating the skb->csum field for a CHECKSUM_COMPLETE packet. |
| 324 | // |
| 325 | // In such a case, skb->csum is a 16-bit one's complement sum of the entire payload, |
| 326 | // thus we need to subtract out the ipv4 header's sum, and add in the ipv6 header's sum. |
| 327 | // However, we've already verified the ipv4 checksum is correct and thus 0. |
| 328 | // Thus we only need to add the ipv6 header's sum. |
| 329 | // |
| 330 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 331 | // (-ENOTSUPP) if it isn't. So we just ignore the return code (see above for more details). |
| 332 | bpf_csum_update(skb, sum6); |
| 333 | |
| 334 | // bpf_skb_change_proto() invalidates all pointers - reload them. |
| 335 | data = (void*)(long)skb->data; |
| 336 | data_end = (void*)(long)skb->data_end; |
| 337 | |
| 338 | // I cannot think of any valid way for this error condition to trigger, however I do |
| 339 | // believe the explicit check is required to keep the in kernel ebpf verifier happy. |
| 340 | if (data + sizeof(ip6) > data_end) return TC_ACT_SHOT; |
| 341 | |
| 342 | // Copy over the new ipv6 header without an ethernet header. |
| 343 | *(struct ipv6hdr*)data = ip6; |
| 344 | |
| 345 | // Redirect to non v4-* interface. Tcpdump only sees packet after this redirect. |
| 346 | return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); |
| 347 | } |
| 348 | |
| 349 | LICENSE("Apache 2.0"); |
| 350 | CRITICAL("netd"); |