xref: /linux/include/net/inet6_hashtables.h (revision 54a8a2220c936a47840c9a3d74910c5a56fae2ed)
1 /*
2  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3  *		operating system.  INET is implemented using the BSD Socket
4  *		interface as the means of communication with the user level.
5  *
6  * Authors:	Lotsa people, from code originally in tcp
7  *
8  *	This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13 
14 #ifndef _INET6_HASHTABLES_H
15 #define _INET6_HASHTABLES_H
16 
17 #include <linux/config.h>
18 
19 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
20 #include <linux/in6.h>
21 #include <linux/ipv6.h>
22 #include <linux/types.h>
23 
24 #include <net/ipv6.h>
25 
26 struct inet_hashinfo;
27 
28 /* I have no idea if this is a good hash for v6 or not. -DaveM */
29 static inline unsigned int inet6_ehashfn(const struct in6_addr *laddr, const u16 lport,
30 				const struct in6_addr *faddr, const u16 fport)
31 {
32 	unsigned int hashent = (lport ^ fport);
33 
34 	hashent ^= (laddr->s6_addr32[3] ^ faddr->s6_addr32[3]);
35 	hashent ^= hashent >> 16;
36 	hashent ^= hashent >> 8;
37 	return hashent;
38 }
39 
40 static inline int inet6_sk_ehashfn(const struct sock *sk)
41 {
42 	const struct inet_sock *inet = inet_sk(sk);
43 	const struct ipv6_pinfo *np = inet6_sk(sk);
44 	const struct in6_addr *laddr = &np->rcv_saddr;
45 	const struct in6_addr *faddr = &np->daddr;
46 	const __u16 lport = inet->num;
47 	const __u16 fport = inet->dport;
48 	return inet6_ehashfn(laddr, lport, faddr, fport);
49 }
50 
51 /*
52  * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
53  * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
54  *
55  * The sockhash lock must be held as a reader here.
56  */
57 static inline struct sock *
58 		__inet6_lookup_established(struct inet_hashinfo *hashinfo,
59 					   const struct in6_addr *saddr,
60 					   const u16 sport,
61 					   const struct in6_addr *daddr,
62 					   const u16 hnum,
63 					   const int dif)
64 {
65 	struct sock *sk;
66 	const struct hlist_node *node;
67 	const __u32 ports = INET_COMBINED_PORTS(sport, hnum);
68 	/* Optimize here for direct hit, only listening connections can
69 	 * have wildcards anyways.
70 	 */
71 	unsigned int hash = inet6_ehashfn(daddr, hnum, saddr, sport);
72 	struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
73 
74 	prefetch(head->chain.first);
75 	read_lock(&head->lock);
76 	sk_for_each(sk, node, &head->chain) {
77 		/* For IPV6 do the cheaper port and family tests first. */
78 		if (INET6_MATCH(sk, hash, saddr, daddr, ports, dif))
79 			goto hit; /* You sunk my battleship! */
80 	}
81 	/* Must check for a TIME_WAIT'er before going to listener hash. */
82 	sk_for_each(sk, node, &(head + hashinfo->ehash_size)->chain) {
83 		const struct inet_timewait_sock *tw = inet_twsk(sk);
84 
85 		if(*((__u32 *)&(tw->tw_dport))	== ports	&&
86 		   sk->sk_family		== PF_INET6) {
87 			const struct tcp6_timewait_sock *tcp6tw = tcp6_twsk(sk);
88 
89 			if (ipv6_addr_equal(&tcp6tw->tw_v6_daddr, saddr)	&&
90 			    ipv6_addr_equal(&tcp6tw->tw_v6_rcv_saddr, daddr)	&&
91 			    (!sk->sk_bound_dev_if || sk->sk_bound_dev_if == dif))
92 				goto hit;
93 		}
94 	}
95 	read_unlock(&head->lock);
96 	return NULL;
97 
98 hit:
99 	sock_hold(sk);
100 	read_unlock(&head->lock);
101 	return sk;
102 }
103 
104 extern struct sock *inet6_lookup_listener(struct inet_hashinfo *hashinfo,
105 					  const struct in6_addr *daddr,
106 					  const unsigned short hnum,
107 					  const int dif);
108 
109 static inline struct sock *__inet6_lookup(struct inet_hashinfo *hashinfo,
110 					  const struct in6_addr *saddr,
111 					  const u16 sport,
112 					  const struct in6_addr *daddr,
113 					  const u16 hnum,
114 					  const int dif)
115 {
116 	struct sock *sk = __inet6_lookup_established(hashinfo, saddr, sport,
117 						     daddr, hnum, dif);
118 	if (sk)
119 		return sk;
120 
121 	return inet6_lookup_listener(hashinfo, daddr, hnum, dif);
122 }
123 
124 extern struct sock *inet6_lookup(struct inet_hashinfo *hashinfo,
125 				 const struct in6_addr *saddr, const u16 sport,
126 				 const struct in6_addr *daddr, const u16 dport,
127 				 const int dif);
128 #endif /* defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) */
129 #endif /* _INET6_HASHTABLES_H */
130