1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2017 Facebook 3 #include <stddef.h> 4 #include <stdbool.h> 5 #include <string.h> 6 #include <linux/pkt_cls.h> 7 #include <linux/bpf.h> 8 #include <linux/in.h> 9 #include <linux/if_ether.h> 10 #include <linux/ip.h> 11 #include <linux/ipv6.h> 12 #include <linux/icmp.h> 13 #include <linux/icmpv6.h> 14 #include <linux/tcp.h> 15 #include <linux/udp.h> 16 #include "bpf_helpers.h" 17 18 static __u32 rol32(__u32 word, unsigned int shift) 19 { 20 return (word << shift) | (word >> ((-shift) & 31)); 21 } 22 23 /* copy paste of jhash from kernel sources to make sure llvm 24 * can compile it into valid sequence of bpf instructions 25 */ 26 #define __jhash_mix(a, b, c) \ 27 { \ 28 a -= c; a ^= rol32(c, 4); c += b; \ 29 b -= a; b ^= rol32(a, 6); a += c; \ 30 c -= b; c ^= rol32(b, 8); b += a; \ 31 a -= c; a ^= rol32(c, 16); c += b; \ 32 b -= a; b ^= rol32(a, 19); a += c; \ 33 c -= b; c ^= rol32(b, 4); b += a; \ 34 } 35 36 #define __jhash_final(a, b, c) \ 37 { \ 38 c ^= b; c -= rol32(b, 14); \ 39 a ^= c; a -= rol32(c, 11); \ 40 b ^= a; b -= rol32(a, 25); \ 41 c ^= b; c -= rol32(b, 16); \ 42 a ^= c; a -= rol32(c, 4); \ 43 b ^= a; b -= rol32(a, 14); \ 44 c ^= b; c -= rol32(b, 24); \ 45 } 46 47 #define JHASH_INITVAL 0xdeadbeef 48 49 typedef unsigned int u32; 50 51 static __attribute__ ((noinline)) 52 u32 jhash(const void *key, u32 length, u32 initval) 53 { 54 u32 a, b, c; 55 const unsigned char *k = key; 56 57 a = b = c = JHASH_INITVAL + length + initval; 58 59 while (length > 12) { 60 a += *(u32 *)(k); 61 b += *(u32 *)(k + 4); 62 c += *(u32 *)(k + 8); 63 __jhash_mix(a, b, c); 64 length -= 12; 65 k += 12; 66 } 67 switch (length) { 68 case 12: c += (u32)k[11]<<24; 69 case 11: c += (u32)k[10]<<16; 70 case 10: c += (u32)k[9]<<8; 71 case 9: c += k[8]; 72 case 8: b += (u32)k[7]<<24; 73 case 7: b += (u32)k[6]<<16; 74 case 6: b += (u32)k[5]<<8; 75 case 5: b += k[4]; 76 case 4: a += (u32)k[3]<<24; 77 case 3: a += (u32)k[2]<<16; 78 case 2: a += (u32)k[1]<<8; 79 case 1: a += k[0]; 80 __jhash_final(a, b, c); 81 case 0: /* Nothing left to add */ 82 break; 83 } 84 85 return c; 86 } 87 88 static __attribute__ ((noinline)) 89 u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval) 90 { 91 a += initval; 92 b += initval; 93 c += initval; 94 __jhash_final(a, b, c); 95 return c; 96 } 97 98 static __attribute__ ((noinline)) 99 u32 jhash_2words(u32 a, u32 b, u32 initval) 100 { 101 return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2)); 102 } 103 104 struct flow_key { 105 union { 106 __be32 src; 107 __be32 srcv6[4]; 108 }; 109 union { 110 __be32 dst; 111 __be32 dstv6[4]; 112 }; 113 union { 114 __u32 ports; 115 __u16 port16[2]; 116 }; 117 __u8 proto; 118 }; 119 120 struct packet_description { 121 struct flow_key flow; 122 __u8 flags; 123 }; 124 125 struct ctl_value { 126 union { 127 __u64 value; 128 __u32 ifindex; 129 __u8 mac[6]; 130 }; 131 }; 132 133 struct vip_definition { 134 union { 135 __be32 vip; 136 __be32 vipv6[4]; 137 }; 138 __u16 port; 139 __u16 family; 140 __u8 proto; 141 }; 142 143 struct vip_meta { 144 __u32 flags; 145 __u32 vip_num; 146 }; 147 148 struct real_pos_lru { 149 __u32 pos; 150 __u64 atime; 151 }; 152 153 struct real_definition { 154 union { 155 __be32 dst; 156 __be32 dstv6[4]; 157 }; 158 __u8 flags; 159 }; 160 161 struct lb_stats { 162 __u64 v2; 163 __u64 v1; 164 }; 165 166 struct { 167 __u32 type; 168 __u32 max_entries; 169 struct vip_definition *key; 170 struct vip_meta *value; 171 } vip_map SEC(".maps") = { 172 .type = BPF_MAP_TYPE_HASH, 173 .max_entries = 512, 174 }; 175 176 struct { 177 __u32 type; 178 __u32 max_entries; 179 __u32 map_flags; 180 struct flow_key *key; 181 struct real_pos_lru *value; 182 } lru_cache SEC(".maps") = { 183 .type = BPF_MAP_TYPE_LRU_HASH, 184 .max_entries = 300, 185 .map_flags = 1U << 1, 186 }; 187 188 struct { 189 __u32 type; 190 __u32 max_entries; 191 __u32 *key; 192 __u32 *value; 193 } ch_rings SEC(".maps") = { 194 .type = BPF_MAP_TYPE_ARRAY, 195 .max_entries = 12 * 655, 196 }; 197 198 struct { 199 __u32 type; 200 __u32 max_entries; 201 __u32 *key; 202 struct real_definition *value; 203 } reals SEC(".maps") = { 204 .type = BPF_MAP_TYPE_ARRAY, 205 .max_entries = 40, 206 }; 207 208 struct { 209 __u32 type; 210 __u32 max_entries; 211 __u32 *key; 212 struct lb_stats *value; 213 } stats SEC(".maps") = { 214 .type = BPF_MAP_TYPE_PERCPU_ARRAY, 215 .max_entries = 515, 216 }; 217 218 struct { 219 __u32 type; 220 __u32 max_entries; 221 __u32 *key; 222 struct ctl_value *value; 223 } ctl_array SEC(".maps") = { 224 .type = BPF_MAP_TYPE_ARRAY, 225 .max_entries = 16, 226 }; 227 228 struct eth_hdr { 229 unsigned char eth_dest[6]; 230 unsigned char eth_source[6]; 231 unsigned short eth_proto; 232 }; 233 234 static inline __u64 calc_offset(bool is_ipv6, bool is_icmp) 235 { 236 __u64 off = sizeof(struct eth_hdr); 237 if (is_ipv6) { 238 off += sizeof(struct ipv6hdr); 239 if (is_icmp) 240 off += sizeof(struct icmp6hdr) + sizeof(struct ipv6hdr); 241 } else { 242 off += sizeof(struct iphdr); 243 if (is_icmp) 244 off += sizeof(struct icmphdr) + sizeof(struct iphdr); 245 } 246 return off; 247 } 248 249 static __attribute__ ((noinline)) 250 bool parse_udp(void *data, void *data_end, 251 bool is_ipv6, struct packet_description *pckt) 252 { 253 254 bool is_icmp = !((pckt->flags & (1 << 0)) == 0); 255 __u64 off = calc_offset(is_ipv6, is_icmp); 256 struct udphdr *udp; 257 udp = data + off; 258 259 if (udp + 1 > data_end) 260 return 0; 261 if (!is_icmp) { 262 pckt->flow.port16[0] = udp->source; 263 pckt->flow.port16[1] = udp->dest; 264 } else { 265 pckt->flow.port16[0] = udp->dest; 266 pckt->flow.port16[1] = udp->source; 267 } 268 return 1; 269 } 270 271 static __attribute__ ((noinline)) 272 bool parse_tcp(void *data, void *data_end, 273 bool is_ipv6, struct packet_description *pckt) 274 { 275 276 bool is_icmp = !((pckt->flags & (1 << 0)) == 0); 277 __u64 off = calc_offset(is_ipv6, is_icmp); 278 struct tcphdr *tcp; 279 280 tcp = data + off; 281 if (tcp + 1 > data_end) 282 return 0; 283 if (tcp->syn) 284 pckt->flags |= (1 << 1); 285 if (!is_icmp) { 286 pckt->flow.port16[0] = tcp->source; 287 pckt->flow.port16[1] = tcp->dest; 288 } else { 289 pckt->flow.port16[0] = tcp->dest; 290 pckt->flow.port16[1] = tcp->source; 291 } 292 return 1; 293 } 294 295 static __attribute__ ((noinline)) 296 bool encap_v6(struct xdp_md *xdp, struct ctl_value *cval, 297 struct packet_description *pckt, 298 struct real_definition *dst, __u32 pkt_bytes) 299 { 300 struct eth_hdr *new_eth; 301 struct eth_hdr *old_eth; 302 struct ipv6hdr *ip6h; 303 __u32 ip_suffix; 304 void *data_end; 305 void *data; 306 307 if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct ipv6hdr))) 308 return 0; 309 data = (void *)(long)xdp->data; 310 data_end = (void *)(long)xdp->data_end; 311 new_eth = data; 312 ip6h = data + sizeof(struct eth_hdr); 313 old_eth = data + sizeof(struct ipv6hdr); 314 if (new_eth + 1 > data_end || 315 old_eth + 1 > data_end || ip6h + 1 > data_end) 316 return 0; 317 memcpy(new_eth->eth_dest, cval->mac, 6); 318 memcpy(new_eth->eth_source, old_eth->eth_dest, 6); 319 new_eth->eth_proto = 56710; 320 ip6h->version = 6; 321 ip6h->priority = 0; 322 memset(ip6h->flow_lbl, 0, sizeof(ip6h->flow_lbl)); 323 324 ip6h->nexthdr = IPPROTO_IPV6; 325 ip_suffix = pckt->flow.srcv6[3] ^ pckt->flow.port16[0]; 326 ip6h->payload_len = 327 __builtin_bswap16(pkt_bytes + sizeof(struct ipv6hdr)); 328 ip6h->hop_limit = 4; 329 330 ip6h->saddr.in6_u.u6_addr32[0] = 1; 331 ip6h->saddr.in6_u.u6_addr32[1] = 2; 332 ip6h->saddr.in6_u.u6_addr32[2] = 3; 333 ip6h->saddr.in6_u.u6_addr32[3] = ip_suffix; 334 memcpy(ip6h->daddr.in6_u.u6_addr32, dst->dstv6, 16); 335 return 1; 336 } 337 338 static __attribute__ ((noinline)) 339 bool encap_v4(struct xdp_md *xdp, struct ctl_value *cval, 340 struct packet_description *pckt, 341 struct real_definition *dst, __u32 pkt_bytes) 342 { 343 344 __u32 ip_suffix = __builtin_bswap16(pckt->flow.port16[0]); 345 struct eth_hdr *new_eth; 346 struct eth_hdr *old_eth; 347 __u16 *next_iph_u16; 348 struct iphdr *iph; 349 __u32 csum = 0; 350 void *data_end; 351 void *data; 352 353 ip_suffix <<= 15; 354 ip_suffix ^= pckt->flow.src; 355 if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct iphdr))) 356 return 0; 357 data = (void *)(long)xdp->data; 358 data_end = (void *)(long)xdp->data_end; 359 new_eth = data; 360 iph = data + sizeof(struct eth_hdr); 361 old_eth = data + sizeof(struct iphdr); 362 if (new_eth + 1 > data_end || 363 old_eth + 1 > data_end || iph + 1 > data_end) 364 return 0; 365 memcpy(new_eth->eth_dest, cval->mac, 6); 366 memcpy(new_eth->eth_source, old_eth->eth_dest, 6); 367 new_eth->eth_proto = 8; 368 iph->version = 4; 369 iph->ihl = 5; 370 iph->frag_off = 0; 371 iph->protocol = IPPROTO_IPIP; 372 iph->check = 0; 373 iph->tos = 1; 374 iph->tot_len = __builtin_bswap16(pkt_bytes + sizeof(struct iphdr)); 375 /* don't update iph->daddr, since it will overwrite old eth_proto 376 * and multiple iterations of bpf_prog_run() will fail 377 */ 378 379 iph->saddr = ((0xFFFF0000 & ip_suffix) | 4268) ^ dst->dst; 380 iph->ttl = 4; 381 382 next_iph_u16 = (__u16 *) iph; 383 #pragma clang loop unroll(full) 384 for (int i = 0; i < sizeof(struct iphdr) >> 1; i++) 385 csum += *next_iph_u16++; 386 iph->check = ~((csum & 0xffff) + (csum >> 16)); 387 if (bpf_xdp_adjust_head(xdp, (int)sizeof(struct iphdr))) 388 return 0; 389 return 1; 390 } 391 392 static __attribute__ ((noinline)) 393 bool decap_v6(struct xdp_md *xdp, void **data, void **data_end, bool inner_v4) 394 { 395 struct eth_hdr *new_eth; 396 struct eth_hdr *old_eth; 397 398 old_eth = *data; 399 new_eth = *data + sizeof(struct ipv6hdr); 400 memcpy(new_eth->eth_source, old_eth->eth_source, 6); 401 memcpy(new_eth->eth_dest, old_eth->eth_dest, 6); 402 if (inner_v4) 403 new_eth->eth_proto = 8; 404 else 405 new_eth->eth_proto = 56710; 406 if (bpf_xdp_adjust_head(xdp, (int)sizeof(struct ipv6hdr))) 407 return 0; 408 *data = (void *)(long)xdp->data; 409 *data_end = (void *)(long)xdp->data_end; 410 return 1; 411 } 412 413 static __attribute__ ((noinline)) 414 bool decap_v4(struct xdp_md *xdp, void **data, void **data_end) 415 { 416 struct eth_hdr *new_eth; 417 struct eth_hdr *old_eth; 418 419 old_eth = *data; 420 new_eth = *data + sizeof(struct iphdr); 421 memcpy(new_eth->eth_source, old_eth->eth_source, 6); 422 memcpy(new_eth->eth_dest, old_eth->eth_dest, 6); 423 new_eth->eth_proto = 8; 424 if (bpf_xdp_adjust_head(xdp, (int)sizeof(struct iphdr))) 425 return 0; 426 *data = (void *)(long)xdp->data; 427 *data_end = (void *)(long)xdp->data_end; 428 return 1; 429 } 430 431 static __attribute__ ((noinline)) 432 int swap_mac_and_send(void *data, void *data_end) 433 { 434 unsigned char tmp_mac[6]; 435 struct eth_hdr *eth; 436 437 eth = data; 438 memcpy(tmp_mac, eth->eth_source, 6); 439 memcpy(eth->eth_source, eth->eth_dest, 6); 440 memcpy(eth->eth_dest, tmp_mac, 6); 441 return XDP_TX; 442 } 443 444 static __attribute__ ((noinline)) 445 int send_icmp_reply(void *data, void *data_end) 446 { 447 struct icmphdr *icmp_hdr; 448 __u16 *next_iph_u16; 449 __u32 tmp_addr = 0; 450 struct iphdr *iph; 451 __u32 csum1 = 0; 452 __u32 csum = 0; 453 __u64 off = 0; 454 455 if (data + sizeof(struct eth_hdr) 456 + sizeof(struct iphdr) + sizeof(struct icmphdr) > data_end) 457 return XDP_DROP; 458 off += sizeof(struct eth_hdr); 459 iph = data + off; 460 off += sizeof(struct iphdr); 461 icmp_hdr = data + off; 462 icmp_hdr->type = 0; 463 icmp_hdr->checksum += 0x0007; 464 iph->ttl = 4; 465 tmp_addr = iph->daddr; 466 iph->daddr = iph->saddr; 467 iph->saddr = tmp_addr; 468 iph->check = 0; 469 next_iph_u16 = (__u16 *) iph; 470 #pragma clang loop unroll(full) 471 for (int i = 0; i < sizeof(struct iphdr) >> 1; i++) 472 csum += *next_iph_u16++; 473 iph->check = ~((csum & 0xffff) + (csum >> 16)); 474 return swap_mac_and_send(data, data_end); 475 } 476 477 static __attribute__ ((noinline)) 478 int send_icmp6_reply(void *data, void *data_end) 479 { 480 struct icmp6hdr *icmp_hdr; 481 struct ipv6hdr *ip6h; 482 __be32 tmp_addr[4]; 483 __u64 off = 0; 484 485 if (data + sizeof(struct eth_hdr) 486 + sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr) > data_end) 487 return XDP_DROP; 488 off += sizeof(struct eth_hdr); 489 ip6h = data + off; 490 off += sizeof(struct ipv6hdr); 491 icmp_hdr = data + off; 492 icmp_hdr->icmp6_type = 129; 493 icmp_hdr->icmp6_cksum -= 0x0001; 494 ip6h->hop_limit = 4; 495 memcpy(tmp_addr, ip6h->saddr.in6_u.u6_addr32, 16); 496 memcpy(ip6h->saddr.in6_u.u6_addr32, ip6h->daddr.in6_u.u6_addr32, 16); 497 memcpy(ip6h->daddr.in6_u.u6_addr32, tmp_addr, 16); 498 return swap_mac_and_send(data, data_end); 499 } 500 501 static __attribute__ ((noinline)) 502 int parse_icmpv6(void *data, void *data_end, __u64 off, 503 struct packet_description *pckt) 504 { 505 struct icmp6hdr *icmp_hdr; 506 struct ipv6hdr *ip6h; 507 508 icmp_hdr = data + off; 509 if (icmp_hdr + 1 > data_end) 510 return XDP_DROP; 511 if (icmp_hdr->icmp6_type == 128) 512 return send_icmp6_reply(data, data_end); 513 if (icmp_hdr->icmp6_type != 3) 514 return XDP_PASS; 515 off += sizeof(struct icmp6hdr); 516 ip6h = data + off; 517 if (ip6h + 1 > data_end) 518 return XDP_DROP; 519 pckt->flow.proto = ip6h->nexthdr; 520 pckt->flags |= (1 << 0); 521 memcpy(pckt->flow.srcv6, ip6h->daddr.in6_u.u6_addr32, 16); 522 memcpy(pckt->flow.dstv6, ip6h->saddr.in6_u.u6_addr32, 16); 523 return -1; 524 } 525 526 static __attribute__ ((noinline)) 527 int parse_icmp(void *data, void *data_end, __u64 off, 528 struct packet_description *pckt) 529 { 530 struct icmphdr *icmp_hdr; 531 struct iphdr *iph; 532 533 icmp_hdr = data + off; 534 if (icmp_hdr + 1 > data_end) 535 return XDP_DROP; 536 if (icmp_hdr->type == 8) 537 return send_icmp_reply(data, data_end); 538 if ((icmp_hdr->type != 3) || (icmp_hdr->code != 4)) 539 return XDP_PASS; 540 off += sizeof(struct icmphdr); 541 iph = data + off; 542 if (iph + 1 > data_end) 543 return XDP_DROP; 544 if (iph->ihl != 5) 545 return XDP_DROP; 546 pckt->flow.proto = iph->protocol; 547 pckt->flags |= (1 << 0); 548 pckt->flow.src = iph->daddr; 549 pckt->flow.dst = iph->saddr; 550 return -1; 551 } 552 553 static __attribute__ ((noinline)) 554 __u32 get_packet_hash(struct packet_description *pckt, 555 bool hash_16bytes) 556 { 557 if (hash_16bytes) 558 return jhash_2words(jhash(pckt->flow.srcv6, 16, 12), 559 pckt->flow.ports, 24); 560 else 561 return jhash_2words(pckt->flow.src, pckt->flow.ports, 562 24); 563 } 564 565 __attribute__ ((noinline)) 566 static bool get_packet_dst(struct real_definition **real, 567 struct packet_description *pckt, 568 struct vip_meta *vip_info, 569 bool is_ipv6, void *lru_map) 570 { 571 struct real_pos_lru new_dst_lru = { }; 572 bool hash_16bytes = is_ipv6; 573 __u32 *real_pos, hash, key; 574 __u64 cur_time; 575 576 if (vip_info->flags & (1 << 2)) 577 hash_16bytes = 1; 578 if (vip_info->flags & (1 << 3)) { 579 pckt->flow.port16[0] = pckt->flow.port16[1]; 580 memset(pckt->flow.srcv6, 0, 16); 581 } 582 hash = get_packet_hash(pckt, hash_16bytes); 583 if (hash != 0x358459b7 /* jhash of ipv4 packet */ && 584 hash != 0x2f4bc6bb /* jhash of ipv6 packet */) 585 return 0; 586 key = 2 * vip_info->vip_num + hash % 2; 587 real_pos = bpf_map_lookup_elem(&ch_rings, &key); 588 if (!real_pos) 589 return 0; 590 key = *real_pos; 591 *real = bpf_map_lookup_elem(&reals, &key); 592 if (!(*real)) 593 return 0; 594 if (!(vip_info->flags & (1 << 1))) { 595 __u32 conn_rate_key = 512 + 2; 596 struct lb_stats *conn_rate_stats = 597 bpf_map_lookup_elem(&stats, &conn_rate_key); 598 599 if (!conn_rate_stats) 600 return 1; 601 cur_time = bpf_ktime_get_ns(); 602 if ((cur_time - conn_rate_stats->v2) >> 32 > 0xffFFFF) { 603 conn_rate_stats->v1 = 1; 604 conn_rate_stats->v2 = cur_time; 605 } else { 606 conn_rate_stats->v1 += 1; 607 if (conn_rate_stats->v1 >= 1) 608 return 1; 609 } 610 if (pckt->flow.proto == IPPROTO_UDP) 611 new_dst_lru.atime = cur_time; 612 new_dst_lru.pos = key; 613 bpf_map_update_elem(lru_map, &pckt->flow, &new_dst_lru, 0); 614 } 615 return 1; 616 } 617 618 __attribute__ ((noinline)) 619 static void connection_table_lookup(struct real_definition **real, 620 struct packet_description *pckt, 621 void *lru_map) 622 { 623 624 struct real_pos_lru *dst_lru; 625 __u64 cur_time; 626 __u32 key; 627 628 dst_lru = bpf_map_lookup_elem(lru_map, &pckt->flow); 629 if (!dst_lru) 630 return; 631 if (pckt->flow.proto == IPPROTO_UDP) { 632 cur_time = bpf_ktime_get_ns(); 633 if (cur_time - dst_lru->atime > 300000) 634 return; 635 dst_lru->atime = cur_time; 636 } 637 key = dst_lru->pos; 638 *real = bpf_map_lookup_elem(&reals, &key); 639 } 640 641 /* don't believe your eyes! 642 * below function has 6 arguments whereas bpf and llvm allow maximum of 5 643 * but since it's _static_ llvm can optimize one argument away 644 */ 645 __attribute__ ((noinline)) 646 static int process_l3_headers_v6(struct packet_description *pckt, 647 __u8 *protocol, __u64 off, 648 __u16 *pkt_bytes, void *data, 649 void *data_end) 650 { 651 struct ipv6hdr *ip6h; 652 __u64 iph_len; 653 int action; 654 655 ip6h = data + off; 656 if (ip6h + 1 > data_end) 657 return XDP_DROP; 658 iph_len = sizeof(struct ipv6hdr); 659 *protocol = ip6h->nexthdr; 660 pckt->flow.proto = *protocol; 661 *pkt_bytes = __builtin_bswap16(ip6h->payload_len); 662 off += iph_len; 663 if (*protocol == 45) { 664 return XDP_DROP; 665 } else if (*protocol == 59) { 666 action = parse_icmpv6(data, data_end, off, pckt); 667 if (action >= 0) 668 return action; 669 } else { 670 memcpy(pckt->flow.srcv6, ip6h->saddr.in6_u.u6_addr32, 16); 671 memcpy(pckt->flow.dstv6, ip6h->daddr.in6_u.u6_addr32, 16); 672 } 673 return -1; 674 } 675 676 __attribute__ ((noinline)) 677 static int process_l3_headers_v4(struct packet_description *pckt, 678 __u8 *protocol, __u64 off, 679 __u16 *pkt_bytes, void *data, 680 void *data_end) 681 { 682 struct iphdr *iph; 683 __u64 iph_len; 684 int action; 685 686 iph = data + off; 687 if (iph + 1 > data_end) 688 return XDP_DROP; 689 if (iph->ihl != 5) 690 return XDP_DROP; 691 *protocol = iph->protocol; 692 pckt->flow.proto = *protocol; 693 *pkt_bytes = __builtin_bswap16(iph->tot_len); 694 off += 20; 695 if (iph->frag_off & 65343) 696 return XDP_DROP; 697 if (*protocol == IPPROTO_ICMP) { 698 action = parse_icmp(data, data_end, off, pckt); 699 if (action >= 0) 700 return action; 701 } else { 702 pckt->flow.src = iph->saddr; 703 pckt->flow.dst = iph->daddr; 704 } 705 return -1; 706 } 707 708 __attribute__ ((noinline)) 709 static int process_packet(void *data, __u64 off, void *data_end, 710 bool is_ipv6, struct xdp_md *xdp) 711 { 712 713 struct real_definition *dst = NULL; 714 struct packet_description pckt = { }; 715 struct vip_definition vip = { }; 716 struct lb_stats *data_stats; 717 struct eth_hdr *eth = data; 718 void *lru_map = &lru_cache; 719 struct vip_meta *vip_info; 720 __u32 lru_stats_key = 513; 721 __u32 mac_addr_pos = 0; 722 __u32 stats_key = 512; 723 struct ctl_value *cval; 724 __u16 pkt_bytes; 725 __u64 iph_len; 726 __u8 protocol; 727 __u32 vip_num; 728 int action; 729 730 if (is_ipv6) 731 action = process_l3_headers_v6(&pckt, &protocol, off, 732 &pkt_bytes, data, data_end); 733 else 734 action = process_l3_headers_v4(&pckt, &protocol, off, 735 &pkt_bytes, data, data_end); 736 if (action >= 0) 737 return action; 738 protocol = pckt.flow.proto; 739 if (protocol == IPPROTO_TCP) { 740 if (!parse_tcp(data, data_end, is_ipv6, &pckt)) 741 return XDP_DROP; 742 } else if (protocol == IPPROTO_UDP) { 743 if (!parse_udp(data, data_end, is_ipv6, &pckt)) 744 return XDP_DROP; 745 } else { 746 return XDP_TX; 747 } 748 749 if (is_ipv6) 750 memcpy(vip.vipv6, pckt.flow.dstv6, 16); 751 else 752 vip.vip = pckt.flow.dst; 753 vip.port = pckt.flow.port16[1]; 754 vip.proto = pckt.flow.proto; 755 vip_info = bpf_map_lookup_elem(&vip_map, &vip); 756 if (!vip_info) { 757 vip.port = 0; 758 vip_info = bpf_map_lookup_elem(&vip_map, &vip); 759 if (!vip_info) 760 return XDP_PASS; 761 if (!(vip_info->flags & (1 << 4))) 762 pckt.flow.port16[1] = 0; 763 } 764 if (data_end - data > 1400) 765 return XDP_DROP; 766 data_stats = bpf_map_lookup_elem(&stats, &stats_key); 767 if (!data_stats) 768 return XDP_DROP; 769 data_stats->v1 += 1; 770 if (!dst) { 771 if (vip_info->flags & (1 << 0)) 772 pckt.flow.port16[0] = 0; 773 if (!(pckt.flags & (1 << 1)) && !(vip_info->flags & (1 << 1))) 774 connection_table_lookup(&dst, &pckt, lru_map); 775 if (dst) 776 goto out; 777 if (pckt.flow.proto == IPPROTO_TCP) { 778 struct lb_stats *lru_stats = 779 bpf_map_lookup_elem(&stats, &lru_stats_key); 780 781 if (!lru_stats) 782 return XDP_DROP; 783 if (pckt.flags & (1 << 1)) 784 lru_stats->v1 += 1; 785 else 786 lru_stats->v2 += 1; 787 } 788 if (!get_packet_dst(&dst, &pckt, vip_info, is_ipv6, lru_map)) 789 return XDP_DROP; 790 data_stats->v2 += 1; 791 } 792 out: 793 cval = bpf_map_lookup_elem(&ctl_array, &mac_addr_pos); 794 if (!cval) 795 return XDP_DROP; 796 if (dst->flags & (1 << 0)) { 797 if (!encap_v6(xdp, cval, &pckt, dst, pkt_bytes)) 798 return XDP_DROP; 799 } else { 800 if (!encap_v4(xdp, cval, &pckt, dst, pkt_bytes)) 801 return XDP_DROP; 802 } 803 vip_num = vip_info->vip_num; 804 data_stats = bpf_map_lookup_elem(&stats, &vip_num); 805 if (!data_stats) 806 return XDP_DROP; 807 data_stats->v1 += 1; 808 data_stats->v2 += pkt_bytes; 809 810 data = (void *)(long)xdp->data; 811 data_end = (void *)(long)xdp->data_end; 812 if (data + 4 > data_end) 813 return XDP_DROP; 814 *(u32 *)data = dst->dst; 815 return XDP_DROP; 816 } 817 818 __attribute__ ((section("xdp-test"), used)) 819 int balancer_ingress(struct xdp_md *ctx) 820 { 821 void *data = (void *)(long)ctx->data; 822 void *data_end = (void *)(long)ctx->data_end; 823 struct eth_hdr *eth = data; 824 __u32 eth_proto; 825 __u32 nh_off; 826 827 nh_off = sizeof(struct eth_hdr); 828 if (data + nh_off > data_end) 829 return XDP_DROP; 830 eth_proto = eth->eth_proto; 831 if (eth_proto == 8) 832 return process_packet(data, nh_off, data_end, 0, ctx); 833 else if (eth_proto == 56710) 834 return process_packet(data, nh_off, data_end, 1, ctx); 835 else 836 return XDP_DROP; 837 } 838 839 char _license[] __attribute__ ((section("license"), used)) = "GPL"; 840 int _version __attribute__ ((section("version"), used)) = 1; 841