Home | History | Annotate | Line # | Download | only in agr
      1 /*	$NetBSD: if_agrether_hash.c,v 1.4 2017/09/26 07:42:06 knakahara Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2005 YAMAMOTO Takashi,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: if_agrether_hash.c,v 1.4 2017/09/26 07:42:06 knakahara Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/mbuf.h>
     34 #include <sys/hash.h>
     35 
     36 #include <net/if.h>
     37 #include <net/if_dl.h>
     38 #include <net/if_ether.h>
     39 #include <net/if_vlanvar.h>
     40 
     41 #include <net/agr/if_agrethervar.h>
     42 
     43 #include <netinet/in.h>
     44 #include <netinet/in_systm.h>
     45 #include <netinet/ip.h>
     46 #include <netinet/ip6.h>
     47 
     48 #define	HASH(p, l, h)	hash32_buf((p), (l), (h))
     49 
     50 static const void *agr_m_extract(struct mbuf *, int, int, void *);
     51 
     52 static const void *
     53 agr_m_extract(struct mbuf *m, int off, int len, void *buf)
     54 {
     55 	const void *result;
     56 
     57 	KASSERT((m->m_flags & M_PKTHDR) != 0);
     58 
     59 	if (m->m_pkthdr.len < off + len) {
     60 		result = NULL;
     61 	} else if (m->m_len >= off + len) {
     62 		result = mtod(m, char *) + off;
     63 	} else {
     64 		m_copydata(m, off, len, buf);
     65 		result = buf;
     66 	}
     67 
     68 	return result;
     69 }
     70 
     71 uint32_t
     72 agrether_hashmbuf(struct agr_softc *sc, struct mbuf *m)
     73 {
     74 	struct ether_header eh_store;
     75 	const struct ether_header *eh;
     76 	uint32_t hash = HASH32_BUF_INIT;
     77 	int off = 0;
     78 	uint16_t tci;
     79 	uint16_t etype;
     80 
     81 	eh = agr_m_extract(m, off, sizeof(*eh), &eh_store);
     82 	if (eh == NULL) {
     83 		return hash;
     84 	}
     85 
     86 	hash = HASH(&eh->ether_dhost, sizeof(eh->ether_dhost), hash);
     87 	hash = HASH(&eh->ether_shost, sizeof(eh->ether_shost), hash);
     88 	etype = eh->ether_type;
     89 
     90 	if (etype == htobe16(ETHERTYPE_VLAN)) {
     91 		struct ether_vlan_header vlanhdr_store;
     92 		const struct ether_vlan_header *vlanhdr;
     93 
     94 		vlanhdr = agr_m_extract(m, off, sizeof(*vlanhdr),
     95 		    &vlanhdr_store);
     96 		if (vlanhdr == NULL) {
     97 			return hash;
     98 		}
     99 
    100 		tci = vlanhdr->evl_tag;
    101 		etype = vlanhdr->evl_proto;
    102 		off += sizeof(*vlanhdr) - sizeof(*eh);
    103 	} else if (vlan_has_tag(m)) {
    104 		tci = htole16(vlan_get_tag(m));
    105 	} else {
    106 		tci = 0;
    107 	}
    108 	hash = HASH(&tci, sizeof(tci), hash);
    109 
    110 	off += sizeof(*eh);
    111 	if (etype == htobe16(ETHERTYPE_IP)) {
    112 		struct ip ip_store;
    113 		const struct ip *ip;
    114 
    115 		ip = agr_m_extract(m, off, sizeof(*ip), &ip_store);
    116 		if (ip == NULL) {
    117 			return hash;
    118 		}
    119 
    120 		hash = HASH(&ip->ip_src, sizeof(ip->ip_src), hash);
    121 		hash = HASH(&ip->ip_dst, sizeof(ip->ip_dst), hash);
    122 		hash = HASH(&ip->ip_p, sizeof(ip->ip_p), hash);
    123 
    124 		/* use port numbers for tcp and udp? */
    125 
    126 	} else if (etype == htobe16(ETHERTYPE_IPV6)) {
    127 		struct ip6_hdr ip6_store;
    128 		const struct ip6_hdr *ip6;
    129 		uint32_t flowlabel;
    130 
    131 		ip6 = agr_m_extract(m, off, sizeof(*ip6), &ip6_store);
    132 		if (ip6 == NULL) {
    133 			return hash;
    134 		}
    135 
    136 		hash = HASH(&ip6->ip6_src, sizeof(ip6->ip6_src), hash);
    137 		hash = HASH(&ip6->ip6_dst, sizeof(ip6->ip6_dst), hash);
    138 		/* hash = HASH(&ip6->ip6_nxt, sizeof(ip6->ip6_nxt), hash); */
    139 
    140 		flowlabel = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
    141 		hash = HASH(&flowlabel, sizeof(flowlabel), hash);
    142 
    143 		/* use port numbers for tcp and udp? */
    144 	}
    145 
    146 	return hash;
    147 }
    148