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 int inet6_ehashfn(const struct in6_addr
*laddr
, const u16 lport
,
30 const struct in6_addr
*faddr
, const u16 fport
,
33 int hashent
= (lport
^ fport
);
35 hashent
^= (laddr
->s6_addr32
[3] ^ faddr
->s6_addr32
[3]);
36 hashent
^= hashent
>> 16;
37 hashent
^= hashent
>> 8;
38 return (hashent
& (ehash_size
- 1));
41 static inline int inet6_sk_ehashfn(const struct sock
*sk
, const int ehash_size
)
43 const struct inet_sock
*inet
= inet_sk(sk
);
44 const struct ipv6_pinfo
*np
= inet6_sk(sk
);
45 const struct in6_addr
*laddr
= &np
->rcv_saddr
;
46 const struct in6_addr
*faddr
= &np
->daddr
;
47 const __u16 lport
= inet
->num
;
48 const __u16 fport
= inet
->dport
;
49 return inet6_ehashfn(laddr
, lport
, faddr
, fport
, ehash_size
);
53 * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
54 * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
56 * The sockhash lock must be held as a reader here.
58 static inline struct sock
*
59 __inet6_lookup_established(struct inet_hashinfo
*hashinfo
,
60 const struct in6_addr
*saddr
,
62 const struct in6_addr
*daddr
,
67 const struct hlist_node
*node
;
68 const __u32 ports
= INET_COMBINED_PORTS(sport
, hnum
);
69 /* Optimize here for direct hit, only listening connections can
70 * have wildcards anyways.
72 const int hash
= inet6_ehashfn(daddr
, hnum
, saddr
, sport
,
73 hashinfo
->ehash_size
);
74 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[hash
];
76 read_lock(&head
->lock
);
77 sk_for_each(sk
, node
, &head
->chain
) {
78 /* For IPV6 do the cheaper port and family tests first. */
79 if (INET6_MATCH(sk
, saddr
, daddr
, ports
, dif
))
80 goto hit
; /* You sunk my battleship! */
82 /* Must check for a TIME_WAIT'er before going to listener hash. */
83 sk_for_each(sk
, node
, &(head
+ hashinfo
->ehash_size
)->chain
) {
84 const struct inet_timewait_sock
*tw
= inet_twsk(sk
);
86 if(*((__u32
*)&(tw
->tw_dport
)) == ports
&&
87 sk
->sk_family
== PF_INET6
) {
88 const struct tcp6_timewait_sock
*tcp6tw
= tcp6_twsk(sk
);
90 if (ipv6_addr_equal(&tcp6tw
->tw_v6_daddr
, saddr
) &&
91 ipv6_addr_equal(&tcp6tw
->tw_v6_rcv_saddr
, daddr
) &&
92 (!sk
->sk_bound_dev_if
|| sk
->sk_bound_dev_if
== dif
))
96 read_unlock(&head
->lock
);
101 read_unlock(&head
->lock
);
105 extern struct sock
*inet6_lookup_listener(struct inet_hashinfo
*hashinfo
,
106 const struct in6_addr
*daddr
,
107 const unsigned short hnum
,
110 static inline struct sock
*__inet6_lookup(struct inet_hashinfo
*hashinfo
,
111 const struct in6_addr
*saddr
,
113 const struct in6_addr
*daddr
,
117 struct sock
*sk
= __inet6_lookup_established(hashinfo
, saddr
, sport
,
122 return inet6_lookup_listener(hashinfo
, daddr
, hnum
, dif
);
125 extern struct sock
*inet6_lookup(struct inet_hashinfo
*hashinfo
,
126 const struct in6_addr
*saddr
, const u16 sport
,
127 const struct in6_addr
*daddr
, const u16 dport
,
129 #endif /* defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) */
130 #endif /* _INET6_HASHTABLES_H */