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.
6 * Authors: Lotsa people, from code originally in tcp
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.
14 #ifndef _INET6_HASHTABLES_H
15 #define _INET6_HASHTABLES_H
17 #include <linux/config.h>
19 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
20 #include <linux/in6.h>
21 #include <linux/ipv6.h>
22 #include <linux/types.h>
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)
32 unsigned int hashent = (lport ^ fport);
34 hashent ^= (laddr->s6_addr32[3] ^ faddr->s6_addr32[3]);
35 hashent ^= hashent >> 16;
36 hashent ^= hashent >> 8;
40 static inline int inet6_sk_ehashfn(const struct sock *sk)
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);
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
55 * The sockhash lock must be held as a reader here.
57 static inline struct sock *
58 __inet6_lookup_established(struct inet_hashinfo *hashinfo,
59 const struct in6_addr *saddr,
61 const struct in6_addr *daddr,
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.
71 unsigned int hash = inet6_ehashfn(daddr, hnum, saddr, sport);
72 struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
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! */
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);
85 if(*((__u32 *)&(tw->tw_dport)) == ports &&
86 sk->sk_family == PF_INET6) {
87 const struct tcp6_timewait_sock *tcp6tw = tcp6_twsk(sk);
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))
95 read_unlock(&head->lock);
100 read_unlock(&head->lock);
104 extern struct sock *inet6_lookup_listener(struct inet_hashinfo *hashinfo,
105 const struct in6_addr *daddr,
106 const unsigned short hnum,
109 static inline struct sock *__inet6_lookup(struct inet_hashinfo *hashinfo,
110 const struct in6_addr *saddr,
112 const struct in6_addr *daddr,
116 struct sock *sk = __inet6_lookup_established(hashinfo, saddr, sport,
121 return inet6_lookup_listener(hashinfo, daddr, hnum, dif);
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,
128 #endif /* defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) */
129 #endif /* _INET6_HASHTABLES_H */