/** * Parser + hash function for the IPv4 packet */ static uint32_t decode_ip_n_hash(struct iphdr *iph, uint8_t hash_split, uint8_t seed) { TRACE_PKTHASH_FUNC_START(); uint32_t rc = 0; if (hash_split == 2) { rc = sym_hash_fn(NTOHL(iph->saddr), NTOHL(iph->daddr), NTOHS(0xFFFD) + seed, NTOHS(0xFFFE) + seed); } else { struct tcphdr *tcph = NULL; struct udphdr *udph = NULL; switch (iph->protocol) { case IPPROTO_TCP: tcph = (struct tcphdr *)((uint8_t *)iph + (iph->ihl<<2)); rc = sym_hash_fn(NTOHL(iph->saddr), NTOHL(iph->daddr), NTOHS(tcph->source) + seed, NTOHS(tcph->dest) + seed); break; case IPPROTO_UDP: udph = (struct udphdr *)((uint8_t *)iph + (iph->ihl<<2)); rc = sym_hash_fn(NTOHL(iph->saddr), NTOHL(iph->daddr), NTOHS(udph->source) + seed, NTOHS(udph->dest) + seed); break; case IPPROTO_IPIP: /* tunneling */ rc = decode_ip_n_hash((struct iphdr *)((uint8_t *)iph + (iph->ihl<<2)), hash_split, seed); break; default: /* * the hash strength (although weaker but) should still hold * even with 2 fields */ rc = sym_hash_fn(NTOHL(iph->saddr), NTOHL(iph->daddr), NTOHS(0xFFFD) + seed, NTOHS(0xFFFE) + seed); break; } } TRACE_PKTHASH_FUNC_END(); return rc; }
/** * Parser + hash function for the IPv4 packet */ static uint32_t decode_ip_n_hash(struct ip *iph, uint8_t hash_split, uint8_t seed) { TRACE_PKTHASH_FUNC_START(); uint32_t rc = 0; if (hash_split == 2) { rc = sym_hash_fn(ntohl(iph->ip_src.s_addr), ntohl(iph->ip_dst.s_addr), ntohs(0xFFFD) + seed, ntohs(0xFFFE) + seed); } else { struct tcphdr *tcph = NULL; struct udphdr *udph = NULL; switch (iph->ip_p) { case IPPROTO_TCP: tcph = (struct tcphdr *)((uint8_t *)iph + (iph->ip_hl<<2)); rc = sym_hash_fn(ntohl(iph->ip_src.s_addr), ntohl(iph->ip_dst.s_addr), ntohs(tcph->th_sport) + seed, ntohs(tcph->th_dport) + seed); break; case IPPROTO_UDP: udph = (struct udphdr *)((uint8_t *)iph + (iph->ip_hl<<2)); rc = sym_hash_fn(ntohl(iph->ip_src.s_addr), ntohl(iph->ip_dst.s_addr), ntohs(udph->uh_sport) + seed, ntohs(udph->uh_dport) + seed); break; case IPPROTO_IPIP: /* tunneling */ rc = decode_ip_n_hash((struct ip *)((uint8_t *)iph + (iph->ip_hl<<2)), hash_split, seed); break; default: /* * the hash strength (although weaker but) should still hold * even with 2 fields */ rc = sym_hash_fn(ntohl(iph->ip_src.s_addr), ntohl(iph->ip_dst.s_addr), ntohs(0xFFFD) + seed, ntohs(0xFFFE) + seed); break; } } TRACE_PKTHASH_FUNC_END(); return rc; }
/** * A temp solution while hash for other protocols are filled... * (See decode_vlan_n_hash & pkt_hdr_hash functions). */ static uint32_t decode_others_n_hash(struct ether_header *ethh, uint8_t seed) { TRACE_PKTHASH_FUNC_START(); uint32_t saddr, daddr, rc; saddr = ethh->ether_shost[5] | (ethh->ether_shost[4] << 8) | (ethh->ether_shost[3] << 16) | (ethh->ether_shost[2] << 24); daddr = ethh->ether_dhost[5] | (ethh->ether_dhost[4] << 8) | (ethh->ether_dhost[3] << 16) | (ethh->ether_dhost[2] << 24); rc = sym_hash_fn(ntohl(saddr), ntohl(daddr), ntohs(0xFFFD) + seed, ntohs(0xFFFE) + seed); TRACE_PKTHASH_FUNC_END(); return rc; }
/** * A temp solution while hash for other protocols are filled... * (See decode_vlan_n_hash & pkt_hdr_hash functions). */ static uint32_t decode_others_n_hash(struct ethhdr *ethh, uint8_t seed) { TRACE_PKTHASH_FUNC_START(); uint32_t saddr, daddr, rc; saddr = ethh->h_source[5] | (ethh->h_source[4] << 8) | (ethh->h_source[3] << 16) | (ethh->h_source[2] << 24); daddr = ethh->h_dest[5] | (ethh->h_dest[4] << 8) | (ethh->h_dest[3] << 16) | (ethh->h_dest[2] << 24); rc = sym_hash_fn(NTOHL(saddr), NTOHL(daddr), NTOHS(0xFFFD) + seed, NTOHS(0xFFFE) + seed); TRACE_PKTHASH_FUNC_END(); return rc; }
/** * Parser + hash function for the IPv6 packet */ static uint32_t decode_ipv6_n_hash(struct ipv6hdr *ipv6h, uint8_t hash_split, uint8_t seed) { TRACE_PKTHASH_FUNC_START(); uint32_t saddr, daddr; uint32_t rc = 0; /* Get only the first 4 octets */ saddr = ipv6h->saddr.in6_u.u6_addr8[0] | (ipv6h->saddr.in6_u.u6_addr8[1] << 8) | (ipv6h->saddr.in6_u.u6_addr8[2] << 16) | (ipv6h->saddr.in6_u.u6_addr8[3] << 24); daddr = ipv6h->daddr.in6_u.u6_addr8[0] | (ipv6h->daddr.in6_u.u6_addr8[1] << 8) | (ipv6h->daddr.in6_u.u6_addr8[2] << 16) | (ipv6h->daddr.in6_u.u6_addr8[3] << 24); if (hash_split == 2) { rc = sym_hash_fn(NTOHL(saddr), NTOHL(daddr), NTOHS(0xFFFD) + seed, NTOHS(0xFFFE) + seed); } else { struct tcphdr *tcph = NULL; struct udphdr *udph = NULL; switch(NTOHS(ipv6h->nexthdr)) { case IPPROTO_TCP: tcph = (struct tcphdr *)(ipv6h + 1); rc = sym_hash_fn(NTOHL(saddr), NTOHL(daddr), NTOHS(tcph->source) + seed, NTOHS(tcph->dest) + seed); break; case IPPROTO_UDP: udph = (struct udphdr *)(ipv6h + 1); rc = sym_hash_fn(NTOHL(saddr), NTOHL(daddr), NTOHS(udph->source) + seed, NTOHS(udph->dest) + seed); break; case IPPROTO_IPIP: /* tunneling */ rc = decode_ip_n_hash((struct iphdr *)(ipv6h + 1), hash_split, seed); break; case IPPROTO_IPV6: /* tunneling */ rc = decode_ipv6_n_hash((struct ipv6hdr *)(ipv6h + 1), hash_split, seed); break; case IPPROTO_ICMP: case IPPROTO_GRE: case IPPROTO_ESP: case IPPROTO_PIM: case IPPROTO_IGMP: default: /* * the hash strength (although weaker but) * should still hold even with 2 fields */ rc = sym_hash_fn(NTOHL(saddr), NTOHL(daddr), NTOHS(0xFFFD) + seed, NTOHS(0xFFFE) + seed); } } TRACE_PKTHASH_FUNC_END(); return rc; }
/** * Parser + hash function for the IPv6 packet */ static uint32_t decode_ipv6_n_hash(struct ip6_hdr *ipv6h, uint8_t hash_split, uint8_t seed) { TRACE_PKTHASH_FUNC_START(); uint32_t saddr, daddr; uint32_t rc = 0; /* Get only the first 4 octets */ saddr = ipv6h->ip6_src.s6_addr[0] | (ipv6h->ip6_src.s6_addr[1] << 8) | (ipv6h->ip6_src.s6_addr[2] << 16) | (ipv6h->ip6_src.s6_addr[3] << 24); daddr = ipv6h->ip6_dst.s6_addr[0] | (ipv6h->ip6_dst.s6_addr[1] << 8) | (ipv6h->ip6_dst.s6_addr[2] << 16) | (ipv6h->ip6_dst.s6_addr[3] << 24); if (hash_split == 2) { rc = sym_hash_fn(ntohl(saddr), ntohl(daddr), ntohs(0xFFFD) + seed, ntohs(0xFFFE) + seed); } else { struct tcphdr *tcph = NULL; struct udphdr *udph = NULL; switch(ntohs(ipv6h->ip6_ctlun.ip6_un1.ip6_un1_nxt)) { case IPPROTO_TCP: tcph = (struct tcphdr *)(ipv6h + 1); rc = sym_hash_fn(ntohl(saddr), ntohl(daddr), ntohs(tcph->th_sport) + seed, ntohs(tcph->th_dport) + seed); break; case IPPROTO_UDP: udph = (struct udphdr *)(ipv6h + 1); rc = sym_hash_fn(ntohl(saddr), ntohl(daddr), ntohs(udph->uh_sport) + seed, ntohs(udph->uh_dport) + seed); break; case IPPROTO_IPIP: /* tunneling */ rc = decode_ip_n_hash((struct ip *)(ipv6h + 1), hash_split, seed); break; case IPPROTO_IPV6: /* tunneling */ rc = decode_ipv6_n_hash((struct ip6_hdr *)(ipv6h + 1), hash_split, seed); break; case IPPROTO_ICMP: case IPPROTO_GRE: case IPPROTO_ESP: case IPPROTO_PIM: case IPPROTO_IGMP: default: /* * the hash strength (although weaker but) should still hold * even with 2 fields */ rc = sym_hash_fn(ntohl(saddr), ntohl(daddr), ntohs(0xFFFD) + seed, ntohs(0xFFFE) + seed); } } TRACE_PKTHASH_FUNC_END(); return rc; }