Maciej Żenczykowski | dca6ce7 | 2019-03-07 16:54:12 -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 | |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 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> |
Maciej Żenczykowski | dca6ce7 | 2019-03-07 16:54:12 -0800 | [diff] [blame] | 24 | #include <linux/pkt_cls.h> |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 25 | #include <linux/swab.h> |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 26 | #include <stdbool.h> |
| 27 | #include <stdint.h> |
| 28 | |
Maciej Żenczykowski | dca6ce7 | 2019-03-07 16:54:12 -0800 | [diff] [blame] | 29 | #include "bpf_helpers.h" |
Maciej Żenczykowski | 3cfcdd6 | 2019-10-31 01:03:39 -0700 | [diff] [blame] | 30 | #include "bpf_net_helpers.h" |
Maciej Żenczykowski | dca6ce7 | 2019-03-07 16:54:12 -0800 | [diff] [blame] | 31 | #include "netdbpf/bpf_shared.h" |
| 32 | |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 33 | // From kernel:include/net/ip.h |
| 34 | #define IP_DF 0x4000 // Flag: "Don't Fragment" |
| 35 | |
Maciej Żenczykowski | b8a7be5 | 2019-04-19 21:47:08 -0700 | [diff] [blame] | 36 | DEFINE_BPF_MAP(clat_ingress_map, HASH, ClatIngressKey, ClatIngressValue, 16) |
Maciej Żenczykowski | dca6ce7 | 2019-03-07 16:54:12 -0800 | [diff] [blame] | 37 | |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 38 | static inline __always_inline int nat64(struct __sk_buff* skb, bool is_ethernet) { |
| 39 | const int l2_header_size = is_ethernet ? sizeof(struct ethhdr) : 0; |
| 40 | void* data = (void*)(long)skb->data; |
| 41 | const void* data_end = (void*)(long)skb->data_end; |
| 42 | const struct ethhdr* const eth = is_ethernet ? data : NULL; // used iff is_ethernet |
| 43 | const struct ipv6hdr* const ip6 = is_ethernet ? (void*)(eth + 1) : data; |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 44 | |
| 45 | // Must be meta-ethernet IPv6 frame |
| 46 | if (skb->protocol != htons(ETH_P_IPV6)) return TC_ACT_OK; |
| 47 | |
| 48 | // Must have (ethernet and) ipv6 header |
| 49 | if (data + l2_header_size + sizeof(*ip6) > data_end) return TC_ACT_OK; |
| 50 | |
| 51 | // Ethertype - if present - must be IPv6 |
| 52 | if (is_ethernet && (eth->h_proto != htons(ETH_P_IPV6))) return TC_ACT_OK; |
| 53 | |
| 54 | // IP version must be 6 |
| 55 | if (ip6->version != 6) return TC_ACT_OK; |
| 56 | |
| 57 | // Maximum IPv6 payload length that can be translated to IPv4 |
| 58 | if (ntohs(ip6->payload_len) > 0xFFFF - sizeof(struct iphdr)) return TC_ACT_OK; |
| 59 | |
| 60 | switch (ip6->nexthdr) { |
Maciej Żenczykowski | 3ed829e | 2019-09-07 06:53:20 -0700 | [diff] [blame] | 61 | case IPPROTO_TCP: // For TCP & UDP the checksum neutrality of the chosen IPv6 |
| 62 | case IPPROTO_UDP: // address means there is no need to update their checksums. |
| 63 | case IPPROTO_GRE: // We do not need to bother looking at GRE/ESP headers, |
Maciej Żenczykowski | 365ecc3 | 2019-08-20 18:00:02 -0700 | [diff] [blame] | 64 | case IPPROTO_ESP: // since there is never a checksum to update. |
| 65 | break; |
| 66 | |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 67 | default: // do not know how to handle anything else |
| 68 | return TC_ACT_OK; |
| 69 | } |
| 70 | |
Maciej Żenczykowski | b8a7be5 | 2019-04-19 21:47:08 -0700 | [diff] [blame] | 71 | ClatIngressKey k = { |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 72 | .iif = skb->ifindex, |
| 73 | .pfx96.in6_u.u6_addr32 = |
| 74 | { |
| 75 | ip6->saddr.in6_u.u6_addr32[0], |
| 76 | ip6->saddr.in6_u.u6_addr32[1], |
| 77 | ip6->saddr.in6_u.u6_addr32[2], |
| 78 | }, |
| 79 | .local6 = ip6->daddr, |
| 80 | }; |
| 81 | |
Maciej Żenczykowski | b8a7be5 | 2019-04-19 21:47:08 -0700 | [diff] [blame] | 82 | ClatIngressValue* v = bpf_clat_ingress_map_lookup_elem(&k); |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 83 | |
| 84 | if (!v) return TC_ACT_OK; |
| 85 | |
| 86 | struct ethhdr eth2; // used iff is_ethernet |
| 87 | if (is_ethernet) { |
| 88 | eth2 = *eth; // Copy over the ethernet header (src/dst mac) |
| 89 | eth2.h_proto = htons(ETH_P_IP); // But replace the ethertype |
| 90 | } |
| 91 | |
| 92 | struct iphdr ip = { |
| 93 | .version = 4, // u4 |
| 94 | .ihl = sizeof(struct iphdr) / sizeof(__u32), // u4 |
| 95 | .tos = (ip6->priority << 4) + (ip6->flow_lbl[0] >> 4), // u8 |
| 96 | .tot_len = htons(ntohs(ip6->payload_len) + sizeof(struct iphdr)), // u16 |
| 97 | .id = 0, // u16 |
| 98 | .frag_off = htons(IP_DF), // u16 |
| 99 | .ttl = ip6->hop_limit, // u8 |
| 100 | .protocol = ip6->nexthdr, // u8 |
| 101 | .check = 0, // u16 |
| 102 | .saddr = ip6->saddr.in6_u.u6_addr32[3], // u32 |
| 103 | .daddr = v->local4.s_addr, // u32 |
| 104 | }; |
| 105 | |
| 106 | // Calculate the IPv4 one's complement checksum of the IPv4 header. |
Maciej Żenczykowski | bb83a51 | 2019-11-18 11:33:19 -0800 | [diff] [blame] | 107 | __wsum sum4 = 0; |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 108 | for (int i = 0; i < sizeof(ip) / sizeof(__u16); ++i) { |
Maciej Żenczykowski | bb83a51 | 2019-11-18 11:33:19 -0800 | [diff] [blame] | 109 | sum4 += ((__u16*)&ip)[i]; |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 110 | } |
Maciej Żenczykowski | bb83a51 | 2019-11-18 11:33:19 -0800 | [diff] [blame] | 111 | // Note that sum4 is guaranteed to be non-zero by virtue of ip.version == 4 |
| 112 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE |
| 113 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16 |
| 114 | ip.check = (__u16)~sum4; // sum4 cannot be zero, so this is never 0xFFFF |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 115 | |
Maciej Żenczykowski | faf05a1 | 2019-11-13 01:35:17 -0800 | [diff] [blame] | 116 | // Calculate the *negative* IPv6 16-bit one's complement checksum of the IPv6 header. |
| 117 | __wsum sum6 = 0; |
| 118 | // We'll end up with a non-zero sum due to ip6->version == 6 (which has '0' bits) |
| 119 | for (int i = 0; i < sizeof(*ip6) / sizeof(__u16); ++i) { |
| 120 | sum6 += ~((__u16*)ip6)[i]; // note the bitwise negation |
| 121 | } |
| 122 | |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 123 | // Note that there is no L4 checksum update: we are relying on the checksum neutrality |
| 124 | // of the ipv6 address chosen by netd's ClatdController. |
| 125 | |
Maciej Żenczykowski | faa3abc | 2019-10-31 03:31:11 -0700 | [diff] [blame] | 126 | // Packet mutations begin - point of no return, but if this first modification fails |
| 127 | // the packet is probably still pristine, so let clatd handle it. |
| 128 | if (bpf_skb_change_proto(skb, htons(ETH_P_IP), 0)) return TC_ACT_OK; |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 129 | |
Maciej Żenczykowski | faf05a1 | 2019-11-13 01:35:17 -0800 | [diff] [blame] | 130 | // This takes care of updating the skb->csum field for a CHECKSUM_COMPLETE packet. |
| 131 | // |
| 132 | // In such a case, skb->csum is a 16-bit one's complement sum of the entire payload, |
| 133 | // thus we need to subtract out the ipv6 header's sum, and add in the ipv4 header's sum. |
| 134 | // However, by construction of ip.check above the checksum of an ipv4 header is zero. |
| 135 | // Thus we only need to subtract the ipv6 header's sum, which is the same as adding |
| 136 | // in the sum of the bitwise negation of the ipv6 header. |
| 137 | // |
| 138 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 139 | // (-ENOTSUPP) if it isn't. So we just ignore the return code. |
| 140 | // |
| 141 | // if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 142 | // return (skb->csum = csum_add(skb->csum, csum)); |
| 143 | // else |
| 144 | // return -ENOTSUPP; |
| 145 | bpf_csum_update(skb, sum6); |
| 146 | |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 147 | // bpf_skb_change_proto() invalidates all pointers - reload them. |
| 148 | data = (void*)(long)skb->data; |
| 149 | data_end = (void*)(long)skb->data_end; |
| 150 | |
| 151 | // I cannot think of any valid way for this error condition to trigger, however I do |
| 152 | // believe the explicit check is required to keep the in kernel ebpf verifier happy. |
| 153 | if (data + l2_header_size + sizeof(struct iphdr) > data_end) return TC_ACT_SHOT; |
| 154 | |
| 155 | if (is_ethernet) { |
| 156 | struct ethhdr* new_eth = data; |
| 157 | |
| 158 | // Copy over the updated ethernet header |
| 159 | *new_eth = eth2; |
| 160 | |
| 161 | // Copy over the new ipv4 header. |
| 162 | *(struct iphdr*)(new_eth + 1) = ip; |
| 163 | } else { |
| 164 | // Copy over the new ipv4 header without an ethernet header. |
| 165 | *(struct iphdr*)data = ip; |
| 166 | } |
| 167 | |
| 168 | // Redirect, possibly back to same interface, so tcpdump sees packet twice. |
| 169 | if (v->oif) return bpf_redirect(v->oif, BPF_F_INGRESS); |
| 170 | |
| 171 | // Just let it through, tcpdump will not see IPv4 packet. |
| 172 | return TC_ACT_OK; |
| 173 | } |
| 174 | |
Maciej Żenczykowski | dca6ce7 | 2019-03-07 16:54:12 -0800 | [diff] [blame] | 175 | SEC("schedcls/ingress/clat_ether") |
| 176 | int sched_cls_ingress_clat_ether(struct __sk_buff* skb) { |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 177 | return nat64(skb, true); |
Maciej Żenczykowski | dca6ce7 | 2019-03-07 16:54:12 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | SEC("schedcls/ingress/clat_rawip") |
| 181 | int sched_cls_ingress_clat_rawip(struct __sk_buff* skb) { |
Maciej Żenczykowski | c2237d8 | 2019-04-02 03:59:51 -0700 | [diff] [blame] | 182 | return nat64(skb, false); |
Maciej Żenczykowski | dca6ce7 | 2019-03-07 16:54:12 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Maciej Żenczykowski | 99bc7a9 | 2019-12-15 12:38:36 -0800 | [diff] [blame] | 185 | DEFINE_BPF_MAP(clat_egress_map, HASH, ClatEgressKey, ClatEgressValue, 16) |
| 186 | |
| 187 | SEC("schedcls/egress/clat_ether") |
| 188 | int sched_cls_egress_clat_ether(struct __sk_buff* skb) { |
| 189 | return TC_ACT_OK; |
| 190 | } |
| 191 | |
| 192 | SEC("schedcls/egress/clat_rawip") |
| 193 | int sched_cls_egress_clat_rawip(struct __sk_buff* skb) { |
Maciej Żenczykowski | 9400524 | 2019-12-17 10:26:22 -0800 | [diff] [blame^] | 194 | void* data = (void*)(long)skb->data; |
| 195 | const void* data_end = (void*)(long)skb->data_end; |
| 196 | const struct iphdr* const ip4 = data; |
| 197 | |
| 198 | // Must be meta-ethernet IPv4 frame |
| 199 | if (skb->protocol != htons(ETH_P_IP)) return TC_ACT_OK; |
| 200 | |
| 201 | // Must have ipv4 header |
| 202 | if (data + sizeof(*ip4) > data_end) return TC_ACT_OK; |
| 203 | |
| 204 | // IP version must be 4 |
| 205 | if (ip4->version != 4) return TC_ACT_OK; |
| 206 | |
| 207 | // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header |
| 208 | if (ip4->ihl != 5) return TC_ACT_OK; |
| 209 | |
| 210 | // Calculate the IPv4 one's complement checksum of the IPv4 header. |
| 211 | __wsum sum4 = 0; |
| 212 | for (int i = 0; i < sizeof(*ip4) / sizeof(__u16); ++i) { |
| 213 | sum4 += ((__u16*)ip4)[i]; |
| 214 | } |
| 215 | // Note that sum4 is guaranteed to be non-zero by virtue of ip4->version == 4 |
| 216 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse u32 into range 1 .. 0x1FFFE |
| 217 | sum4 = (sum4 & 0xFFFF) + (sum4 >> 16); // collapse any potential carry into u16 |
| 218 | // for a correct checksum we should get *a* zero, but sum4 must be positive, ie 0xFFFF |
| 219 | if (sum4 != 0xFFFF) return TC_ACT_OK; |
| 220 | |
| 221 | // Minimum IPv4 total length is the size of the header |
| 222 | if (ntohs(ip4->tot_len) < sizeof(*ip4)) return TC_ACT_OK; |
| 223 | |
| 224 | // We are incapable of dealing with IPv4 fragments |
| 225 | if (ip4->frag_off & ~htons(IP_DF)) return TC_ACT_OK; |
| 226 | |
| 227 | switch (ip4->protocol) { |
| 228 | case IPPROTO_TCP: // For TCP & UDP the checksum neutrality of the chosen IPv6 |
| 229 | case IPPROTO_UDP: // address means there is no need to update their checksums. |
| 230 | case IPPROTO_GRE: // We do not need to bother looking at GRE/ESP headers, |
| 231 | case IPPROTO_ESP: // since there is never a checksum to update. |
| 232 | break; |
| 233 | |
| 234 | default: // do not know how to handle anything else |
| 235 | return TC_ACT_OK; |
| 236 | } |
| 237 | |
| 238 | ClatEgressKey k = { |
| 239 | .iif = skb->ifindex, |
| 240 | .local4.s_addr = ip4->saddr, |
| 241 | }; |
| 242 | |
| 243 | ClatEgressValue* v = bpf_clat_egress_map_lookup_elem(&k); |
| 244 | |
| 245 | if (!v) return TC_ACT_OK; |
| 246 | |
| 247 | // Translating without redirecting doesn't make sense. |
| 248 | if (!v->oif) return TC_ACT_OK; |
| 249 | |
| 250 | // This implementation is currently limited to rawip. |
| 251 | if (v->oifIsEthernet) return TC_ACT_OK; |
| 252 | |
| 253 | struct ipv6hdr ip6 = { |
| 254 | .version = 6, // __u8:4 |
| 255 | .priority = ip4->tos >> 4, // __u8:4 |
| 256 | .flow_lbl = {(ip4->tos & 0xF) << 4, 0, 0}, // __u8[3] |
| 257 | .payload_len = htons(ntohs(ip4->tot_len) - 20), // __be16 |
| 258 | .nexthdr = ip4->protocol, // __u8 |
| 259 | .hop_limit = ip4->ttl, // __u8 |
| 260 | .saddr = v->local6, // struct in6_addr |
| 261 | .daddr = v->pfx96, // struct in6_addr |
| 262 | }; |
| 263 | ip6.daddr.in6_u.u6_addr32[3] = ip4->daddr; |
| 264 | |
| 265 | // Calculate the IPv6 16-bit one's complement checksum of the IPv6 header. |
| 266 | __wsum sum6 = 0; |
| 267 | // We'll end up with a non-zero sum due to ip6.version == 6 |
| 268 | for (int i = 0; i < sizeof(ip6) / sizeof(__u16); ++i) { |
| 269 | sum6 += ((__u16*)&ip6)[i]; |
| 270 | } |
| 271 | |
| 272 | // Note that there is no L4 checksum update: we are relying on the checksum neutrality |
| 273 | // of the ipv6 address chosen by netd's ClatdController. |
| 274 | |
| 275 | // Packet mutations begin - point of no return, but if this first modification fails |
| 276 | // the packet is probably still pristine, so let clatd handle it. |
| 277 | if (bpf_skb_change_proto(skb, htons(ETH_P_IPV6), 0)) return TC_ACT_OK; |
| 278 | |
| 279 | // This takes care of updating the skb->csum field for a CHECKSUM_COMPLETE packet. |
| 280 | // |
| 281 | // In such a case, skb->csum is a 16-bit one's complement sum of the entire payload, |
| 282 | // thus we need to subtract out the ipv4 header's sum, and add in the ipv6 header's sum. |
| 283 | // However, we've already verified the ipv4 checksum is correct and thus 0. |
| 284 | // Thus we only need to add the ipv6 header's sum. |
| 285 | // |
| 286 | // bpf_csum_update() always succeeds if the skb is CHECKSUM_COMPLETE and returns an error |
| 287 | // (-ENOTSUPP) if it isn't. So we just ignore the return code (see above for more details). |
| 288 | bpf_csum_update(skb, sum6); |
| 289 | |
| 290 | // bpf_skb_change_proto() invalidates all pointers - reload them. |
| 291 | data = (void*)(long)skb->data; |
| 292 | data_end = (void*)(long)skb->data_end; |
| 293 | |
| 294 | // I cannot think of any valid way for this error condition to trigger, however I do |
| 295 | // believe the explicit check is required to keep the in kernel ebpf verifier happy. |
| 296 | if (data + sizeof(ip6) > data_end) return TC_ACT_SHOT; |
| 297 | |
| 298 | // Copy over the new ipv6 header without an ethernet header. |
| 299 | *(struct ipv6hdr*)data = ip6; |
| 300 | |
| 301 | // Redirect to non v4-* interface. Tcpdump only sees packet after this redirect. |
| 302 | return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); |
Maciej Żenczykowski | 99bc7a9 | 2019-12-15 12:38:36 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Maciej Żenczykowski | dca6ce7 | 2019-03-07 16:54:12 -0800 | [diff] [blame] | 305 | char _license[] SEC("license") = "Apache 2.0"; |