1bd4aed0eSJiong Wang /* Copyright (c) 2017 Facebook 2bd4aed0eSJiong Wang * 3bd4aed0eSJiong Wang * This program is free software; you can redistribute it and/or 4bd4aed0eSJiong Wang * modify it under the terms of version 2 of the GNU General Public 5bd4aed0eSJiong Wang * License as published by the Free Software Foundation. 6bd4aed0eSJiong Wang */ 7bd4aed0eSJiong Wang #include <stddef.h> 8bd4aed0eSJiong Wang #include <stdbool.h> 9bd4aed0eSJiong Wang #include <string.h> 10bd4aed0eSJiong Wang #include <linux/pkt_cls.h> 11bd4aed0eSJiong Wang #include <linux/bpf.h> 12bd4aed0eSJiong Wang #include <linux/in.h> 13bd4aed0eSJiong Wang #include <linux/if_ether.h> 14bd4aed0eSJiong Wang #include <linux/ip.h> 15bd4aed0eSJiong Wang #include <linux/ipv6.h> 16bd4aed0eSJiong Wang #include <linux/icmp.h> 17bd4aed0eSJiong Wang #include <linux/icmpv6.h> 18bd4aed0eSJiong Wang #include <linux/tcp.h> 19bd4aed0eSJiong Wang #include <linux/udp.h> 20bd4aed0eSJiong Wang #include "bpf_helpers.h" 21bd4aed0eSJiong Wang #include "test_iptunnel_common.h" 22bd4aed0eSJiong Wang #include "bpf_endian.h" 23bd4aed0eSJiong Wang 24bd4aed0eSJiong Wang int _version SEC("version") = 1; 25bd4aed0eSJiong Wang 26bd4aed0eSJiong Wang static inline __u32 rol32(__u32 word, unsigned int shift) 27bd4aed0eSJiong Wang { 28bd4aed0eSJiong Wang return (word << shift) | (word >> ((-shift) & 31)); 29bd4aed0eSJiong Wang } 30bd4aed0eSJiong Wang 31bd4aed0eSJiong Wang /* copy paste of jhash from kernel sources to make sure llvm 32bd4aed0eSJiong Wang * can compile it into valid sequence of bpf instructions 33bd4aed0eSJiong Wang */ 34bd4aed0eSJiong Wang #define __jhash_mix(a, b, c) \ 35bd4aed0eSJiong Wang { \ 36bd4aed0eSJiong Wang a -= c; a ^= rol32(c, 4); c += b; \ 37bd4aed0eSJiong Wang b -= a; b ^= rol32(a, 6); a += c; \ 38bd4aed0eSJiong Wang c -= b; c ^= rol32(b, 8); b += a; \ 39bd4aed0eSJiong Wang a -= c; a ^= rol32(c, 16); c += b; \ 40bd4aed0eSJiong Wang b -= a; b ^= rol32(a, 19); a += c; \ 41bd4aed0eSJiong Wang c -= b; c ^= rol32(b, 4); b += a; \ 42bd4aed0eSJiong Wang } 43bd4aed0eSJiong Wang 44bd4aed0eSJiong Wang #define __jhash_final(a, b, c) \ 45bd4aed0eSJiong Wang { \ 46bd4aed0eSJiong Wang c ^= b; c -= rol32(b, 14); \ 47bd4aed0eSJiong Wang a ^= c; a -= rol32(c, 11); \ 48bd4aed0eSJiong Wang b ^= a; b -= rol32(a, 25); \ 49bd4aed0eSJiong Wang c ^= b; c -= rol32(b, 16); \ 50bd4aed0eSJiong Wang a ^= c; a -= rol32(c, 4); \ 51bd4aed0eSJiong Wang b ^= a; b -= rol32(a, 14); \ 52bd4aed0eSJiong Wang c ^= b; c -= rol32(b, 24); \ 53bd4aed0eSJiong Wang } 54bd4aed0eSJiong Wang 55bd4aed0eSJiong Wang #define JHASH_INITVAL 0xdeadbeef 56bd4aed0eSJiong Wang 57bd4aed0eSJiong Wang typedef unsigned int u32; 58bd4aed0eSJiong Wang 59bd4aed0eSJiong Wang static inline u32 jhash(const void *key, u32 length, u32 initval) 60bd4aed0eSJiong Wang { 61bd4aed0eSJiong Wang u32 a, b, c; 62bd4aed0eSJiong Wang const unsigned char *k = key; 63bd4aed0eSJiong Wang 64bd4aed0eSJiong Wang a = b = c = JHASH_INITVAL + length + initval; 65bd4aed0eSJiong Wang 66bd4aed0eSJiong Wang while (length > 12) { 67bd4aed0eSJiong Wang a += *(u32 *)(k); 68bd4aed0eSJiong Wang b += *(u32 *)(k + 4); 69bd4aed0eSJiong Wang c += *(u32 *)(k + 8); 70bd4aed0eSJiong Wang __jhash_mix(a, b, c); 71bd4aed0eSJiong Wang length -= 12; 72bd4aed0eSJiong Wang k += 12; 73bd4aed0eSJiong Wang } 74bd4aed0eSJiong Wang switch (length) { 75bd4aed0eSJiong Wang case 12: c += (u32)k[11]<<24; 76bd4aed0eSJiong Wang case 11: c += (u32)k[10]<<16; 77bd4aed0eSJiong Wang case 10: c += (u32)k[9]<<8; 78bd4aed0eSJiong Wang case 9: c += k[8]; 79bd4aed0eSJiong Wang case 8: b += (u32)k[7]<<24; 80bd4aed0eSJiong Wang case 7: b += (u32)k[6]<<16; 81bd4aed0eSJiong Wang case 6: b += (u32)k[5]<<8; 82bd4aed0eSJiong Wang case 5: b += k[4]; 83bd4aed0eSJiong Wang case 4: a += (u32)k[3]<<24; 84bd4aed0eSJiong Wang case 3: a += (u32)k[2]<<16; 85bd4aed0eSJiong Wang case 2: a += (u32)k[1]<<8; 86bd4aed0eSJiong Wang case 1: a += k[0]; 87bd4aed0eSJiong Wang __jhash_final(a, b, c); 88bd4aed0eSJiong Wang case 0: /* Nothing left to add */ 89bd4aed0eSJiong Wang break; 90bd4aed0eSJiong Wang } 91bd4aed0eSJiong Wang 92bd4aed0eSJiong Wang return c; 93bd4aed0eSJiong Wang } 94bd4aed0eSJiong Wang 95bd4aed0eSJiong Wang static inline u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval) 96bd4aed0eSJiong Wang { 97bd4aed0eSJiong Wang a += initval; 98bd4aed0eSJiong Wang b += initval; 99bd4aed0eSJiong Wang c += initval; 100bd4aed0eSJiong Wang __jhash_final(a, b, c); 101bd4aed0eSJiong Wang return c; 102bd4aed0eSJiong Wang } 103bd4aed0eSJiong Wang 104bd4aed0eSJiong Wang static inline u32 jhash_2words(u32 a, u32 b, u32 initval) 105bd4aed0eSJiong Wang { 106bd4aed0eSJiong Wang return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2)); 107bd4aed0eSJiong Wang } 108bd4aed0eSJiong Wang 109bd4aed0eSJiong Wang #define PCKT_FRAGMENTED 65343 110bd4aed0eSJiong Wang #define IPV4_HDR_LEN_NO_OPT 20 111bd4aed0eSJiong Wang #define IPV4_PLUS_ICMP_HDR 28 112bd4aed0eSJiong Wang #define IPV6_PLUS_ICMP_HDR 48 113bd4aed0eSJiong Wang #define RING_SIZE 2 114bd4aed0eSJiong Wang #define MAX_VIPS 12 115bd4aed0eSJiong Wang #define MAX_REALS 5 116bd4aed0eSJiong Wang #define CTL_MAP_SIZE 16 117bd4aed0eSJiong Wang #define CH_RINGS_SIZE (MAX_VIPS * RING_SIZE) 118bd4aed0eSJiong Wang #define F_IPV6 (1 << 0) 119bd4aed0eSJiong Wang #define F_HASH_NO_SRC_PORT (1 << 0) 120bd4aed0eSJiong Wang #define F_ICMP (1 << 0) 121bd4aed0eSJiong Wang #define F_SYN_SET (1 << 1) 122bd4aed0eSJiong Wang 123bd4aed0eSJiong Wang struct packet_description { 124bd4aed0eSJiong Wang union { 125bd4aed0eSJiong Wang __be32 src; 126bd4aed0eSJiong Wang __be32 srcv6[4]; 127bd4aed0eSJiong Wang }; 128bd4aed0eSJiong Wang union { 129bd4aed0eSJiong Wang __be32 dst; 130bd4aed0eSJiong Wang __be32 dstv6[4]; 131bd4aed0eSJiong Wang }; 132bd4aed0eSJiong Wang union { 133bd4aed0eSJiong Wang __u32 ports; 134bd4aed0eSJiong Wang __u16 port16[2]; 135bd4aed0eSJiong Wang }; 136bd4aed0eSJiong Wang __u8 proto; 137bd4aed0eSJiong Wang __u8 flags; 138bd4aed0eSJiong Wang }; 139bd4aed0eSJiong Wang 140bd4aed0eSJiong Wang struct ctl_value { 141bd4aed0eSJiong Wang union { 142bd4aed0eSJiong Wang __u64 value; 143bd4aed0eSJiong Wang __u32 ifindex; 144bd4aed0eSJiong Wang __u8 mac[6]; 145bd4aed0eSJiong Wang }; 146bd4aed0eSJiong Wang }; 147bd4aed0eSJiong Wang 148bd4aed0eSJiong Wang struct vip_meta { 149bd4aed0eSJiong Wang __u32 flags; 150bd4aed0eSJiong Wang __u32 vip_num; 151bd4aed0eSJiong Wang }; 152bd4aed0eSJiong Wang 153bd4aed0eSJiong Wang struct real_definition { 154bd4aed0eSJiong Wang union { 155bd4aed0eSJiong Wang __be32 dst; 156bd4aed0eSJiong Wang __be32 dstv6[4]; 157bd4aed0eSJiong Wang }; 158bd4aed0eSJiong Wang __u8 flags; 159bd4aed0eSJiong Wang }; 160bd4aed0eSJiong Wang 161bd4aed0eSJiong Wang struct vip_stats { 162bd4aed0eSJiong Wang __u64 bytes; 163bd4aed0eSJiong Wang __u64 pkts; 164bd4aed0eSJiong Wang }; 165bd4aed0eSJiong Wang 166bd4aed0eSJiong Wang struct eth_hdr { 167bd4aed0eSJiong Wang unsigned char eth_dest[ETH_ALEN]; 168bd4aed0eSJiong Wang unsigned char eth_source[ETH_ALEN]; 169bd4aed0eSJiong Wang unsigned short eth_proto; 170bd4aed0eSJiong Wang }; 171bd4aed0eSJiong Wang 172*df0b7792SAndrii Nakryiko struct { 173*df0b7792SAndrii Nakryiko __u32 type; 174*df0b7792SAndrii Nakryiko __u32 max_entries; 175*df0b7792SAndrii Nakryiko struct vip *key; 176*df0b7792SAndrii Nakryiko struct vip_meta *value; 177*df0b7792SAndrii Nakryiko } vip_map SEC(".maps") = { 178bd4aed0eSJiong Wang .type = BPF_MAP_TYPE_HASH, 179bd4aed0eSJiong Wang .max_entries = MAX_VIPS, 180bd4aed0eSJiong Wang }; 181bd4aed0eSJiong Wang 182*df0b7792SAndrii Nakryiko struct { 183*df0b7792SAndrii Nakryiko __u32 type; 184*df0b7792SAndrii Nakryiko __u32 max_entries; 185*df0b7792SAndrii Nakryiko __u32 *key; 186*df0b7792SAndrii Nakryiko __u32 *value; 187*df0b7792SAndrii Nakryiko } ch_rings SEC(".maps") = { 188bd4aed0eSJiong Wang .type = BPF_MAP_TYPE_ARRAY, 189bd4aed0eSJiong Wang .max_entries = CH_RINGS_SIZE, 190bd4aed0eSJiong Wang }; 191bd4aed0eSJiong Wang 192*df0b7792SAndrii Nakryiko struct { 193*df0b7792SAndrii Nakryiko __u32 type; 194*df0b7792SAndrii Nakryiko __u32 max_entries; 195*df0b7792SAndrii Nakryiko __u32 *key; 196*df0b7792SAndrii Nakryiko struct real_definition *value; 197*df0b7792SAndrii Nakryiko } reals SEC(".maps") = { 198bd4aed0eSJiong Wang .type = BPF_MAP_TYPE_ARRAY, 199bd4aed0eSJiong Wang .max_entries = MAX_REALS, 200bd4aed0eSJiong Wang }; 201bd4aed0eSJiong Wang 202*df0b7792SAndrii Nakryiko struct { 203*df0b7792SAndrii Nakryiko __u32 type; 204*df0b7792SAndrii Nakryiko __u32 max_entries; 205*df0b7792SAndrii Nakryiko __u32 *key; 206*df0b7792SAndrii Nakryiko struct vip_stats *value; 207*df0b7792SAndrii Nakryiko } stats SEC(".maps") = { 208bd4aed0eSJiong Wang .type = BPF_MAP_TYPE_PERCPU_ARRAY, 209bd4aed0eSJiong Wang .max_entries = MAX_VIPS, 210bd4aed0eSJiong Wang }; 211bd4aed0eSJiong Wang 212*df0b7792SAndrii Nakryiko struct { 213*df0b7792SAndrii Nakryiko __u32 type; 214*df0b7792SAndrii Nakryiko __u32 max_entries; 215*df0b7792SAndrii Nakryiko __u32 *key; 216*df0b7792SAndrii Nakryiko struct ctl_value *value; 217*df0b7792SAndrii Nakryiko } ctl_array SEC(".maps") = { 218bd4aed0eSJiong Wang .type = BPF_MAP_TYPE_ARRAY, 219bd4aed0eSJiong Wang .max_entries = CTL_MAP_SIZE, 220bd4aed0eSJiong Wang }; 221bd4aed0eSJiong Wang 222bd4aed0eSJiong Wang static __always_inline __u32 get_packet_hash(struct packet_description *pckt, 223bd4aed0eSJiong Wang bool ipv6) 224bd4aed0eSJiong Wang { 225bd4aed0eSJiong Wang if (ipv6) 226bd4aed0eSJiong Wang return jhash_2words(jhash(pckt->srcv6, 16, MAX_VIPS), 227bd4aed0eSJiong Wang pckt->ports, CH_RINGS_SIZE); 228bd4aed0eSJiong Wang else 229bd4aed0eSJiong Wang return jhash_2words(pckt->src, pckt->ports, CH_RINGS_SIZE); 230bd4aed0eSJiong Wang } 231bd4aed0eSJiong Wang 232bd4aed0eSJiong Wang static __always_inline bool get_packet_dst(struct real_definition **real, 233bd4aed0eSJiong Wang struct packet_description *pckt, 234bd4aed0eSJiong Wang struct vip_meta *vip_info, 235bd4aed0eSJiong Wang bool is_ipv6) 236bd4aed0eSJiong Wang { 237bd4aed0eSJiong Wang __u32 hash = get_packet_hash(pckt, is_ipv6) % RING_SIZE; 238bd4aed0eSJiong Wang __u32 key = RING_SIZE * vip_info->vip_num + hash; 239bd4aed0eSJiong Wang __u32 *real_pos; 240bd4aed0eSJiong Wang 241bd4aed0eSJiong Wang real_pos = bpf_map_lookup_elem(&ch_rings, &key); 242bd4aed0eSJiong Wang if (!real_pos) 243bd4aed0eSJiong Wang return false; 244bd4aed0eSJiong Wang key = *real_pos; 245bd4aed0eSJiong Wang *real = bpf_map_lookup_elem(&reals, &key); 246bd4aed0eSJiong Wang if (!(*real)) 247bd4aed0eSJiong Wang return false; 248bd4aed0eSJiong Wang return true; 249bd4aed0eSJiong Wang } 250bd4aed0eSJiong Wang 251bd4aed0eSJiong Wang static __always_inline int parse_icmpv6(void *data, void *data_end, __u64 off, 252bd4aed0eSJiong Wang struct packet_description *pckt) 253bd4aed0eSJiong Wang { 254bd4aed0eSJiong Wang struct icmp6hdr *icmp_hdr; 255bd4aed0eSJiong Wang struct ipv6hdr *ip6h; 256bd4aed0eSJiong Wang 257bd4aed0eSJiong Wang icmp_hdr = data + off; 258bd4aed0eSJiong Wang if (icmp_hdr + 1 > data_end) 259bd4aed0eSJiong Wang return TC_ACT_SHOT; 260bd4aed0eSJiong Wang if (icmp_hdr->icmp6_type != ICMPV6_PKT_TOOBIG) 261bd4aed0eSJiong Wang return TC_ACT_OK; 262bd4aed0eSJiong Wang off += sizeof(struct icmp6hdr); 263bd4aed0eSJiong Wang ip6h = data + off; 264bd4aed0eSJiong Wang if (ip6h + 1 > data_end) 265bd4aed0eSJiong Wang return TC_ACT_SHOT; 266bd4aed0eSJiong Wang pckt->proto = ip6h->nexthdr; 267bd4aed0eSJiong Wang pckt->flags |= F_ICMP; 268bd4aed0eSJiong Wang memcpy(pckt->srcv6, ip6h->daddr.s6_addr32, 16); 269bd4aed0eSJiong Wang memcpy(pckt->dstv6, ip6h->saddr.s6_addr32, 16); 270bd4aed0eSJiong Wang return TC_ACT_UNSPEC; 271bd4aed0eSJiong Wang } 272bd4aed0eSJiong Wang 273bd4aed0eSJiong Wang static __always_inline int parse_icmp(void *data, void *data_end, __u64 off, 274bd4aed0eSJiong Wang struct packet_description *pckt) 275bd4aed0eSJiong Wang { 276bd4aed0eSJiong Wang struct icmphdr *icmp_hdr; 277bd4aed0eSJiong Wang struct iphdr *iph; 278bd4aed0eSJiong Wang 279bd4aed0eSJiong Wang icmp_hdr = data + off; 280bd4aed0eSJiong Wang if (icmp_hdr + 1 > data_end) 281bd4aed0eSJiong Wang return TC_ACT_SHOT; 282bd4aed0eSJiong Wang if (icmp_hdr->type != ICMP_DEST_UNREACH || 283bd4aed0eSJiong Wang icmp_hdr->code != ICMP_FRAG_NEEDED) 284bd4aed0eSJiong Wang return TC_ACT_OK; 285bd4aed0eSJiong Wang off += sizeof(struct icmphdr); 286bd4aed0eSJiong Wang iph = data + off; 287bd4aed0eSJiong Wang if (iph + 1 > data_end) 288bd4aed0eSJiong Wang return TC_ACT_SHOT; 289bd4aed0eSJiong Wang if (iph->ihl != 5) 290bd4aed0eSJiong Wang return TC_ACT_SHOT; 291bd4aed0eSJiong Wang pckt->proto = iph->protocol; 292bd4aed0eSJiong Wang pckt->flags |= F_ICMP; 293bd4aed0eSJiong Wang pckt->src = iph->daddr; 294bd4aed0eSJiong Wang pckt->dst = iph->saddr; 295bd4aed0eSJiong Wang return TC_ACT_UNSPEC; 296bd4aed0eSJiong Wang } 297bd4aed0eSJiong Wang 298bd4aed0eSJiong Wang static __always_inline bool parse_udp(void *data, __u64 off, void *data_end, 299bd4aed0eSJiong Wang struct packet_description *pckt) 300bd4aed0eSJiong Wang { 301bd4aed0eSJiong Wang struct udphdr *udp; 302bd4aed0eSJiong Wang udp = data + off; 303bd4aed0eSJiong Wang 304bd4aed0eSJiong Wang if (udp + 1 > data_end) 305bd4aed0eSJiong Wang return false; 306bd4aed0eSJiong Wang 307bd4aed0eSJiong Wang if (!(pckt->flags & F_ICMP)) { 308bd4aed0eSJiong Wang pckt->port16[0] = udp->source; 309bd4aed0eSJiong Wang pckt->port16[1] = udp->dest; 310bd4aed0eSJiong Wang } else { 311bd4aed0eSJiong Wang pckt->port16[0] = udp->dest; 312bd4aed0eSJiong Wang pckt->port16[1] = udp->source; 313bd4aed0eSJiong Wang } 314bd4aed0eSJiong Wang return true; 315bd4aed0eSJiong Wang } 316bd4aed0eSJiong Wang 317bd4aed0eSJiong Wang static __always_inline bool parse_tcp(void *data, __u64 off, void *data_end, 318bd4aed0eSJiong Wang struct packet_description *pckt) 319bd4aed0eSJiong Wang { 320bd4aed0eSJiong Wang struct tcphdr *tcp; 321bd4aed0eSJiong Wang 322bd4aed0eSJiong Wang tcp = data + off; 323bd4aed0eSJiong Wang if (tcp + 1 > data_end) 324bd4aed0eSJiong Wang return false; 325bd4aed0eSJiong Wang 326bd4aed0eSJiong Wang if (tcp->syn) 327bd4aed0eSJiong Wang pckt->flags |= F_SYN_SET; 328bd4aed0eSJiong Wang 329bd4aed0eSJiong Wang if (!(pckt->flags & F_ICMP)) { 330bd4aed0eSJiong Wang pckt->port16[0] = tcp->source; 331bd4aed0eSJiong Wang pckt->port16[1] = tcp->dest; 332bd4aed0eSJiong Wang } else { 333bd4aed0eSJiong Wang pckt->port16[0] = tcp->dest; 334bd4aed0eSJiong Wang pckt->port16[1] = tcp->source; 335bd4aed0eSJiong Wang } 336bd4aed0eSJiong Wang return true; 337bd4aed0eSJiong Wang } 338bd4aed0eSJiong Wang 339bd4aed0eSJiong Wang static __always_inline int process_packet(void *data, __u64 off, void *data_end, 340bd4aed0eSJiong Wang bool is_ipv6, struct __sk_buff *skb) 341bd4aed0eSJiong Wang { 342bd4aed0eSJiong Wang void *pkt_start = (void *)(long)skb->data; 343bd4aed0eSJiong Wang struct packet_description pckt = {}; 344bd4aed0eSJiong Wang struct eth_hdr *eth = pkt_start; 345bd4aed0eSJiong Wang struct bpf_tunnel_key tkey = {}; 346bd4aed0eSJiong Wang struct vip_stats *data_stats; 347bd4aed0eSJiong Wang struct real_definition *dst; 348bd4aed0eSJiong Wang struct vip_meta *vip_info; 349bd4aed0eSJiong Wang struct ctl_value *cval; 350bd4aed0eSJiong Wang __u32 v4_intf_pos = 1; 351bd4aed0eSJiong Wang __u32 v6_intf_pos = 2; 352bd4aed0eSJiong Wang struct ipv6hdr *ip6h; 353bd4aed0eSJiong Wang struct vip vip = {}; 354bd4aed0eSJiong Wang struct iphdr *iph; 355bd4aed0eSJiong Wang int tun_flag = 0; 356bd4aed0eSJiong Wang __u16 pkt_bytes; 357bd4aed0eSJiong Wang __u64 iph_len; 358bd4aed0eSJiong Wang __u32 ifindex; 359bd4aed0eSJiong Wang __u8 protocol; 360bd4aed0eSJiong Wang __u32 vip_num; 361bd4aed0eSJiong Wang int action; 362bd4aed0eSJiong Wang 363bd4aed0eSJiong Wang tkey.tunnel_ttl = 64; 364bd4aed0eSJiong Wang if (is_ipv6) { 365bd4aed0eSJiong Wang ip6h = data + off; 366bd4aed0eSJiong Wang if (ip6h + 1 > data_end) 367bd4aed0eSJiong Wang return TC_ACT_SHOT; 368bd4aed0eSJiong Wang 369bd4aed0eSJiong Wang iph_len = sizeof(struct ipv6hdr); 370bd4aed0eSJiong Wang protocol = ip6h->nexthdr; 371bd4aed0eSJiong Wang pckt.proto = protocol; 372bd4aed0eSJiong Wang pkt_bytes = bpf_ntohs(ip6h->payload_len); 373bd4aed0eSJiong Wang off += iph_len; 374bd4aed0eSJiong Wang if (protocol == IPPROTO_FRAGMENT) { 375bd4aed0eSJiong Wang return TC_ACT_SHOT; 376bd4aed0eSJiong Wang } else if (protocol == IPPROTO_ICMPV6) { 377bd4aed0eSJiong Wang action = parse_icmpv6(data, data_end, off, &pckt); 378bd4aed0eSJiong Wang if (action >= 0) 379bd4aed0eSJiong Wang return action; 380bd4aed0eSJiong Wang off += IPV6_PLUS_ICMP_HDR; 381bd4aed0eSJiong Wang } else { 382bd4aed0eSJiong Wang memcpy(pckt.srcv6, ip6h->saddr.s6_addr32, 16); 383bd4aed0eSJiong Wang memcpy(pckt.dstv6, ip6h->daddr.s6_addr32, 16); 384bd4aed0eSJiong Wang } 385bd4aed0eSJiong Wang } else { 386bd4aed0eSJiong Wang iph = data + off; 387bd4aed0eSJiong Wang if (iph + 1 > data_end) 388bd4aed0eSJiong Wang return TC_ACT_SHOT; 389bd4aed0eSJiong Wang if (iph->ihl != 5) 390bd4aed0eSJiong Wang return TC_ACT_SHOT; 391bd4aed0eSJiong Wang 392bd4aed0eSJiong Wang protocol = iph->protocol; 393bd4aed0eSJiong Wang pckt.proto = protocol; 394bd4aed0eSJiong Wang pkt_bytes = bpf_ntohs(iph->tot_len); 395bd4aed0eSJiong Wang off += IPV4_HDR_LEN_NO_OPT; 396bd4aed0eSJiong Wang 397bd4aed0eSJiong Wang if (iph->frag_off & PCKT_FRAGMENTED) 398bd4aed0eSJiong Wang return TC_ACT_SHOT; 399bd4aed0eSJiong Wang if (protocol == IPPROTO_ICMP) { 400bd4aed0eSJiong Wang action = parse_icmp(data, data_end, off, &pckt); 401bd4aed0eSJiong Wang if (action >= 0) 402bd4aed0eSJiong Wang return action; 403bd4aed0eSJiong Wang off += IPV4_PLUS_ICMP_HDR; 404bd4aed0eSJiong Wang } else { 405bd4aed0eSJiong Wang pckt.src = iph->saddr; 406bd4aed0eSJiong Wang pckt.dst = iph->daddr; 407bd4aed0eSJiong Wang } 408bd4aed0eSJiong Wang } 409bd4aed0eSJiong Wang protocol = pckt.proto; 410bd4aed0eSJiong Wang 411bd4aed0eSJiong Wang if (protocol == IPPROTO_TCP) { 412bd4aed0eSJiong Wang if (!parse_tcp(data, off, data_end, &pckt)) 413bd4aed0eSJiong Wang return TC_ACT_SHOT; 414bd4aed0eSJiong Wang } else if (protocol == IPPROTO_UDP) { 415bd4aed0eSJiong Wang if (!parse_udp(data, off, data_end, &pckt)) 416bd4aed0eSJiong Wang return TC_ACT_SHOT; 417bd4aed0eSJiong Wang } else { 418bd4aed0eSJiong Wang return TC_ACT_SHOT; 419bd4aed0eSJiong Wang } 420bd4aed0eSJiong Wang 421bd4aed0eSJiong Wang if (is_ipv6) 422bd4aed0eSJiong Wang memcpy(vip.daddr.v6, pckt.dstv6, 16); 423bd4aed0eSJiong Wang else 424bd4aed0eSJiong Wang vip.daddr.v4 = pckt.dst; 425bd4aed0eSJiong Wang 426bd4aed0eSJiong Wang vip.dport = pckt.port16[1]; 427bd4aed0eSJiong Wang vip.protocol = pckt.proto; 428bd4aed0eSJiong Wang vip_info = bpf_map_lookup_elem(&vip_map, &vip); 429bd4aed0eSJiong Wang if (!vip_info) { 430bd4aed0eSJiong Wang vip.dport = 0; 431bd4aed0eSJiong Wang vip_info = bpf_map_lookup_elem(&vip_map, &vip); 432bd4aed0eSJiong Wang if (!vip_info) 433bd4aed0eSJiong Wang return TC_ACT_SHOT; 434bd4aed0eSJiong Wang pckt.port16[1] = 0; 435bd4aed0eSJiong Wang } 436bd4aed0eSJiong Wang 437bd4aed0eSJiong Wang if (vip_info->flags & F_HASH_NO_SRC_PORT) 438bd4aed0eSJiong Wang pckt.port16[0] = 0; 439bd4aed0eSJiong Wang 440bd4aed0eSJiong Wang if (!get_packet_dst(&dst, &pckt, vip_info, is_ipv6)) 441bd4aed0eSJiong Wang return TC_ACT_SHOT; 442bd4aed0eSJiong Wang 443bd4aed0eSJiong Wang if (dst->flags & F_IPV6) { 444bd4aed0eSJiong Wang cval = bpf_map_lookup_elem(&ctl_array, &v6_intf_pos); 445bd4aed0eSJiong Wang if (!cval) 446bd4aed0eSJiong Wang return TC_ACT_SHOT; 447bd4aed0eSJiong Wang ifindex = cval->ifindex; 448bd4aed0eSJiong Wang memcpy(tkey.remote_ipv6, dst->dstv6, 16); 449bd4aed0eSJiong Wang tun_flag = BPF_F_TUNINFO_IPV6; 450bd4aed0eSJiong Wang } else { 451bd4aed0eSJiong Wang cval = bpf_map_lookup_elem(&ctl_array, &v4_intf_pos); 452bd4aed0eSJiong Wang if (!cval) 453bd4aed0eSJiong Wang return TC_ACT_SHOT; 454bd4aed0eSJiong Wang ifindex = cval->ifindex; 455bd4aed0eSJiong Wang tkey.remote_ipv4 = dst->dst; 456bd4aed0eSJiong Wang } 457bd4aed0eSJiong Wang vip_num = vip_info->vip_num; 458bd4aed0eSJiong Wang data_stats = bpf_map_lookup_elem(&stats, &vip_num); 459bd4aed0eSJiong Wang if (!data_stats) 460bd4aed0eSJiong Wang return TC_ACT_SHOT; 461bd4aed0eSJiong Wang data_stats->pkts++; 462bd4aed0eSJiong Wang data_stats->bytes += pkt_bytes; 463bd4aed0eSJiong Wang bpf_skb_set_tunnel_key(skb, &tkey, sizeof(tkey), tun_flag); 464bd4aed0eSJiong Wang *(u32 *)eth->eth_dest = tkey.remote_ipv4; 465bd4aed0eSJiong Wang return bpf_redirect(ifindex, 0); 466bd4aed0eSJiong Wang } 467bd4aed0eSJiong Wang 468bd4aed0eSJiong Wang SEC("l4lb-demo") 469bd4aed0eSJiong Wang int balancer_ingress(struct __sk_buff *ctx) 470bd4aed0eSJiong Wang { 471bd4aed0eSJiong Wang void *data_end = (void *)(long)ctx->data_end; 472bd4aed0eSJiong Wang void *data = (void *)(long)ctx->data; 473bd4aed0eSJiong Wang struct eth_hdr *eth = data; 474bd4aed0eSJiong Wang __u32 eth_proto; 475bd4aed0eSJiong Wang __u32 nh_off; 476bd4aed0eSJiong Wang 477bd4aed0eSJiong Wang nh_off = sizeof(struct eth_hdr); 478bd4aed0eSJiong Wang if (data + nh_off > data_end) 479bd4aed0eSJiong Wang return TC_ACT_SHOT; 480bd4aed0eSJiong Wang eth_proto = eth->eth_proto; 481bd4aed0eSJiong Wang if (eth_proto == bpf_htons(ETH_P_IP)) 482bd4aed0eSJiong Wang return process_packet(data, nh_off, data_end, false, ctx); 483bd4aed0eSJiong Wang else if (eth_proto == bpf_htons(ETH_P_IPV6)) 484bd4aed0eSJiong Wang return process_packet(data, nh_off, data_end, true, ctx); 485bd4aed0eSJiong Wang else 486bd4aed0eSJiong Wang return TC_ACT_SHOT; 487bd4aed0eSJiong Wang } 488bd4aed0eSJiong Wang char _license[] SEC("license") = "GPL"; 489