[INET6_HASHTABLES]: Move inet6_lookup functions to net/ipv6/inet6_hashtables.c
[safe/jmp/linux-2.6] / include / net / inet6_hashtables.h
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 int inet6_ehashfn(const struct in6_addr *laddr, const u16 lport,
30                                 const struct in6_addr *faddr, const u16 fport,
31                                 const int ehash_size)
32 {
33         int hashent = (lport ^ fport);
34
35         hashent ^= (laddr->s6_addr32[3] ^ faddr->s6_addr32[3]);
36         hashent ^= hashent >> 16;
37         hashent ^= hashent >> 8;
38         return (hashent & (ehash_size - 1));
39 }
40
41 static inline int inet6_sk_ehashfn(const struct sock *sk, const int ehash_size)
42 {
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);
50 }
51
52 /*
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
55  *
56  * The sockhash lock must be held as a reader here.
57  */
58 static inline struct sock *
59                 __inet6_lookup_established(struct inet_hashinfo *hashinfo,
60                                            const struct in6_addr *saddr,
61                                            const u16 sport,
62                                            const struct in6_addr *daddr,
63                                            const u16 hnum,
64                                            const int dif)
65 {
66         struct sock *sk;
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.
71          */
72         const int hash = inet6_ehashfn(daddr, hnum, saddr, sport,
73                                        hashinfo->ehash_size);
74         struct inet_ehash_bucket *head = &hashinfo->ehash[hash];
75
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! */
81         }
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);
85
86                 if(*((__u32 *)&(tw->tw_dport))  == ports        &&
87                    sk->sk_family                == PF_INET6) {
88                         const struct tcp6_timewait_sock *tcp6tw = tcp6_twsk(sk);
89
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))
93                                 goto hit;
94                 }
95         }
96         read_unlock(&head->lock);
97         return NULL;
98
99 hit:
100         sock_hold(sk);
101         read_unlock(&head->lock);
102         return sk;
103 }
104
105 extern struct sock *inet6_lookup_listener(struct inet_hashinfo *hashinfo,
106                                           const struct in6_addr *daddr,
107                                           const unsigned short hnum,
108                                           const int dif);
109
110 static inline struct sock *__inet6_lookup(struct inet_hashinfo *hashinfo,
111                                           const struct in6_addr *saddr,
112                                           const u16 sport,
113                                           const struct in6_addr *daddr,
114                                           const u16 hnum,
115                                           const int dif)
116 {
117         struct sock *sk = __inet6_lookup_established(hashinfo, saddr, sport,
118                                                      daddr, hnum, dif);
119         if (sk)
120                 return sk;
121
122         return inet6_lookup_listener(hashinfo, daddr, hnum, dif);
123 }
124
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,
128                                  const int dif);
129 #endif /* defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) */
130 #endif /* _INET6_HASHTABLES_H */