1 /*-
2  * Copyright (c) 2010-2011 Juniper Networks, Inc.
3  * All rights reserved.
4  *
5  * This software was developed by Robert N. M. Watson under contract
6  * to Juniper Networks, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 
32 __FBSDID("$FreeBSD: stable/10/sys/netinet6/in6_pcbgroup.c 222748 2011-06-06 12:55:02Z rwatson $");
33 
34 #include "opt_inet6.h"
35 
36 #include <sys/param.h>
37 #include <sys/mbuf.h>
38 
39 #include <netinet/in.h>
40 #include <netinet/in_pcb.h>
41 #ifdef INET6
42 #include <netinet6/in6_pcb.h>
43 #endif /* INET6 */
44 
45 /*
46  * Given a hash of whatever the covered tuple might be, return a pcbgroup
47  * index.
48  */
49 static __inline u_int
in6_pcbgroup_getbucket(struct inpcbinfo * pcbinfo,uint32_t hash)50 in6_pcbgroup_getbucket(struct inpcbinfo *pcbinfo, uint32_t hash)
51 {
52 
53 	return (hash % pcbinfo->ipi_npcbgroups);
54 }
55 
56 /*
57  * Map a (hashtype, hash) tuple into a connection group, or NULL if the hash
58  * information is insufficient to identify the pcbgroup.
59  */
60 struct inpcbgroup *
in6_pcbgroup_byhash(struct inpcbinfo * pcbinfo,u_int hashtype,uint32_t hash)61 in6_pcbgroup_byhash(struct inpcbinfo *pcbinfo, u_int hashtype, uint32_t hash)
62 {
63 
64 	return (NULL);
65 }
66 
67 struct inpcbgroup *
in6_pcbgroup_bymbuf(struct inpcbinfo * pcbinfo,struct mbuf * m)68 in6_pcbgroup_bymbuf(struct inpcbinfo *pcbinfo, struct mbuf *m)
69 {
70 
71 	return (in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
72 	    m->m_pkthdr.flowid));
73 }
74 
75 struct inpcbgroup *
in6_pcbgroup_bytuple(struct inpcbinfo * pcbinfo,const struct in6_addr * laddrp,u_short lport,const struct in6_addr * faddrp,u_short fport)76 in6_pcbgroup_bytuple(struct inpcbinfo *pcbinfo, const struct in6_addr *laddrp,
77     u_short lport, const struct in6_addr *faddrp, u_short fport)
78 {
79 	uint32_t hash;
80 
81 	switch (pcbinfo->ipi_hashfields) {
82 	case IPI_HASHFIELDS_4TUPLE:
83 		hash = faddrp->s6_addr32[3] ^ fport;
84 		break;
85 
86 	case IPI_HASHFIELDS_2TUPLE:
87 		hash = faddrp->s6_addr32[3] ^ laddrp->s6_addr32[3];
88 		break;
89 
90 	default:
91 		hash = 0;
92 	}
93 	return (&pcbinfo->ipi_pcbgroups[in6_pcbgroup_getbucket(pcbinfo,
94 	    hash)]);
95 }
96 
97 struct inpcbgroup *
in6_pcbgroup_byinpcb(struct inpcb * inp)98 in6_pcbgroup_byinpcb(struct inpcb *inp)
99 {
100 
101 	return (in6_pcbgroup_bytuple(inp->inp_pcbinfo, &inp->in6p_laddr,
102 	    inp->inp_lport, &inp->in6p_faddr, inp->inp_fport));
103 }
104