1 /* $OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $ */
4 * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
24 #include "opt_inet6.h"
26 #include <sys/param.h>
28 #include <sys/fnv_hash.h>
30 #include <net/ethernet.h>
32 #if defined(INET) || defined(INET6)
33 #include <netinet/in.h>
37 #include <netinet/ip.h>
41 #include <netinet/ip6.h>
45 m_ether_tcpip_hash_gethdr(const struct mbuf *m, const u_int off,
46 const u_int len, void *buf)
49 if (m->m_pkthdr.len < (off + len)) {
51 } else if (m->m_len < (off + len)) {
52 m_copydata(m, off, len, buf);
55 return (mtod(m, char *) + off);
59 m_ether_tcpip_hash_init(void)
64 return (fnv_32_buf(&seed, sizeof(seed), FNV1_32_INIT));
68 m_ether_tcpip_hash(const uint32_t flags, const struct mbuf *m,
78 struct ether_vlan_header vlan;
81 struct ether_header *eh;
82 const struct ether_vlan_header *vlan;
87 const struct ip6_hdr *ip6;
97 eh = mtod(m, struct ether_header *);
98 etype = ntohs(eh->ether_type);
99 if (flags & MBUF_HASHFLAG_L2) {
100 p = fnv_32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
101 p = fnv_32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
103 /* Special handling for encapsulating VLAN frames */
104 if ((m->m_flags & M_VLANTAG) && (flags & MBUF_HASHFLAG_L2)) {
105 p = fnv_32_buf(&m->m_pkthdr.ether_vtag,
106 sizeof(m->m_pkthdr.ether_vtag), p);
107 } else if (etype == ETHERTYPE_VLAN) {
108 vlan = m_ether_tcpip_hash_gethdr(m, off, sizeof(*vlan), &buf);
112 if (flags & MBUF_HASHFLAG_L2)
113 p = fnv_32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
114 etype = ntohs(vlan->evl_proto);
115 off += sizeof(*vlan) - sizeof(*eh);
120 ip = m_ether_tcpip_hash_gethdr(m, off, sizeof(*ip), &buf);
123 if (flags & MBUF_HASHFLAG_L3) {
124 p = fnv_32_buf(&ip->ip_src, sizeof(struct in_addr), p);
125 p = fnv_32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
127 if (flags & MBUF_HASHFLAG_L4) {
128 const uint32_t *ports;
135 iphlen = ip->ip_hl << 2;
136 if (iphlen < sizeof(*ip))
139 ports = m_ether_tcpip_hash_gethdr(m,
140 off, sizeof(*ports), &buf);
143 p = fnv_32_buf(ports, sizeof(*ports), p);
153 ip6 = m_ether_tcpip_hash_gethdr(m, off, sizeof(*ip6), &buf);
156 if (flags & MBUF_HASHFLAG_L3) {
157 p = fnv_32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
158 p = fnv_32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
160 if (flags & MBUF_HASHFLAG_L4) {
163 /* IPv6 flow label */
164 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
165 p = fnv_32_buf(&flow, sizeof(flow), p);