inet: kill smallest_size and smallest_port
[linux-2.6-block.git] / include / net / inet_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 _INET_HASHTABLES_H
15 #define _INET_HASHTABLES_H
16
17
18 #include <linux/interrupt.h>
19 #include <linux/ip.h>
20 #include <linux/ipv6.h>
21 #include <linux/list.h>
22 #include <linux/slab.h>
23 #include <linux/socket.h>
24 #include <linux/spinlock.h>
25 #include <linux/types.h>
26 #include <linux/wait.h>
27
28 #include <net/inet_connection_sock.h>
29 #include <net/inet_sock.h>
30 #include <net/sock.h>
31 #include <net/route.h>
32 #include <net/tcp_states.h>
33 #include <net/netns/hash.h>
34
35 #include <linux/atomic.h>
36 #include <asm/byteorder.h>
37
38 /* This is for all connections with a full identity, no wildcards.
39  * The 'e' prefix stands for Establish, but we really put all sockets
40  * but LISTEN ones.
41  */
42 struct inet_ehash_bucket {
43         struct hlist_nulls_head chain;
44 };
45
46 /* There are a few simple rules, which allow for local port reuse by
47  * an application.  In essence:
48  *
49  *      1) Sockets bound to different interfaces may share a local port.
50  *         Failing that, goto test 2.
51  *      2) If all sockets have sk->sk_reuse set, and none of them are in
52  *         TCP_LISTEN state, the port may be shared.
53  *         Failing that, goto test 3.
54  *      3) If all sockets are bound to a specific inet_sk(sk)->rcv_saddr local
55  *         address, and none of them are the same, the port may be
56  *         shared.
57  *         Failing this, the port cannot be shared.
58  *
59  * The interesting point, is test #2.  This is what an FTP server does
60  * all day.  To optimize this case we use a specific flag bit defined
61  * below.  As we add sockets to a bind bucket list, we perform a
62  * check of: (newsk->sk_reuse && (newsk->sk_state != TCP_LISTEN))
63  * As long as all sockets added to a bind bucket pass this test,
64  * the flag bit will be set.
65  * The resulting situation is that tcp_v[46]_verify_bind() can just check
66  * for this flag bit, if it is set and the socket trying to bind has
67  * sk->sk_reuse set, we don't even have to walk the owners list at all,
68  * we return that it is ok to bind this socket to the requested local port.
69  *
70  * Sounds like a lot of work, but it is worth it.  In a more naive
71  * implementation (ie. current FreeBSD etc.) the entire list of ports
72  * must be walked for each data port opened by an ftp server.  Needless
73  * to say, this does not scale at all.  With a couple thousand FTP
74  * users logged onto your box, isn't it nice to know that new data
75  * ports are created in O(1) time?  I thought so. ;-)   -DaveM
76  */
77 struct inet_bind_bucket {
78         possible_net_t          ib_net;
79         unsigned short          port;
80         signed char             fastreuse;
81         signed char             fastreuseport;
82         kuid_t                  fastuid;
83         struct hlist_node       node;
84         struct hlist_head       owners;
85 };
86
87 static inline struct net *ib_net(struct inet_bind_bucket *ib)
88 {
89         return read_pnet(&ib->ib_net);
90 }
91
92 #define inet_bind_bucket_for_each(tb, head) \
93         hlist_for_each_entry(tb, head, node)
94
95 struct inet_bind_hashbucket {
96         spinlock_t              lock;
97         struct hlist_head       chain;
98 };
99
100 /*
101  * Sockets can be hashed in established or listening table
102  */
103 struct inet_listen_hashbucket {
104         spinlock_t              lock;
105         struct hlist_head       head;
106 };
107
108 /* This is for listening sockets, thus all sockets which possess wildcards. */
109 #define INET_LHTABLE_SIZE       32      /* Yes, really, this is all you need. */
110
111 struct inet_hashinfo {
112         /* This is for sockets with full identity only.  Sockets here will
113          * always be without wildcards and will have the following invariant:
114          *
115          *          TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
116          *
117          */
118         struct inet_ehash_bucket        *ehash;
119         spinlock_t                      *ehash_locks;
120         unsigned int                    ehash_mask;
121         unsigned int                    ehash_locks_mask;
122
123         /* Ok, let's try this, I give up, we do need a local binding
124          * TCP hash as well as the others for fast bind/connect.
125          */
126         struct inet_bind_hashbucket     *bhash;
127
128         unsigned int                    bhash_size;
129         /* 4 bytes hole on 64 bit */
130
131         struct kmem_cache               *bind_bucket_cachep;
132
133         /* All the above members are written once at bootup and
134          * never written again _or_ are predominantly read-access.
135          *
136          * Now align to a new cache line as all the following members
137          * might be often dirty.
138          */
139         /* All sockets in TCP_LISTEN state will be in here.  This is the only
140          * table where wildcard'd TCP sockets can exist.  Hash function here
141          * is just local port number.
142          */
143         struct inet_listen_hashbucket   listening_hash[INET_LHTABLE_SIZE]
144                                         ____cacheline_aligned_in_smp;
145 };
146
147 static inline struct inet_ehash_bucket *inet_ehash_bucket(
148         struct inet_hashinfo *hashinfo,
149         unsigned int hash)
150 {
151         return &hashinfo->ehash[hash & hashinfo->ehash_mask];
152 }
153
154 static inline spinlock_t *inet_ehash_lockp(
155         struct inet_hashinfo *hashinfo,
156         unsigned int hash)
157 {
158         return &hashinfo->ehash_locks[hash & hashinfo->ehash_locks_mask];
159 }
160
161 int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo);
162
163 static inline void inet_ehash_locks_free(struct inet_hashinfo *hashinfo)
164 {
165         kvfree(hashinfo->ehash_locks);
166         hashinfo->ehash_locks = NULL;
167 }
168
169 struct inet_bind_bucket *
170 inet_bind_bucket_create(struct kmem_cache *cachep, struct net *net,
171                         struct inet_bind_hashbucket *head,
172                         const unsigned short snum);
173 void inet_bind_bucket_destroy(struct kmem_cache *cachep,
174                               struct inet_bind_bucket *tb);
175
176 static inline u32 inet_bhashfn(const struct net *net, const __u16 lport,
177                                const u32 bhash_size)
178 {
179         return (lport + net_hash_mix(net)) & (bhash_size - 1);
180 }
181
182 void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
183                     const unsigned short snum);
184
185 /* These can have wildcards, don't try too hard. */
186 static inline u32 inet_lhashfn(const struct net *net, const unsigned short num)
187 {
188         return (num + net_hash_mix(net)) & (INET_LHTABLE_SIZE - 1);
189 }
190
191 static inline int inet_sk_listen_hashfn(const struct sock *sk)
192 {
193         return inet_lhashfn(sock_net(sk), inet_sk(sk)->inet_num);
194 }
195
196 /* Caller must disable local BH processing. */
197 int __inet_inherit_port(const struct sock *sk, struct sock *child);
198
199 void inet_put_port(struct sock *sk);
200
201 void inet_hashinfo_init(struct inet_hashinfo *h);
202
203 bool inet_ehash_insert(struct sock *sk, struct sock *osk);
204 bool inet_ehash_nolisten(struct sock *sk, struct sock *osk);
205 int __inet_hash(struct sock *sk, struct sock *osk);
206 int inet_hash(struct sock *sk);
207 void inet_unhash(struct sock *sk);
208
209 struct sock *__inet_lookup_listener(struct net *net,
210                                     struct inet_hashinfo *hashinfo,
211                                     struct sk_buff *skb, int doff,
212                                     const __be32 saddr, const __be16 sport,
213                                     const __be32 daddr,
214                                     const unsigned short hnum,
215                                     const int dif);
216
217 static inline struct sock *inet_lookup_listener(struct net *net,
218                 struct inet_hashinfo *hashinfo,
219                 struct sk_buff *skb, int doff,
220                 __be32 saddr, __be16 sport,
221                 __be32 daddr, __be16 dport, int dif)
222 {
223         return __inet_lookup_listener(net, hashinfo, skb, doff, saddr, sport,
224                                       daddr, ntohs(dport), dif);
225 }
226
227 /* Socket demux engine toys. */
228 /* What happens here is ugly; there's a pair of adjacent fields in
229    struct inet_sock; __be16 dport followed by __u16 num.  We want to
230    search by pair, so we combine the keys into a single 32bit value
231    and compare with 32bit value read from &...->dport.  Let's at least
232    make sure that it's not mixed with anything else...
233    On 64bit targets we combine comparisons with pair of adjacent __be32
234    fields in the same way.
235 */
236 #ifdef __BIG_ENDIAN
237 #define INET_COMBINED_PORTS(__sport, __dport) \
238         ((__force __portpair)(((__force __u32)(__be16)(__sport) << 16) | (__u32)(__dport)))
239 #else /* __LITTLE_ENDIAN */
240 #define INET_COMBINED_PORTS(__sport, __dport) \
241         ((__force __portpair)(((__u32)(__dport) << 16) | (__force __u32)(__be16)(__sport)))
242 #endif
243
244 #if (BITS_PER_LONG == 64)
245 #ifdef __BIG_ENDIAN
246 #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
247         const __addrpair __name = (__force __addrpair) ( \
248                                    (((__force __u64)(__be32)(__saddr)) << 32) | \
249                                    ((__force __u64)(__be32)(__daddr)))
250 #else /* __LITTLE_ENDIAN */
251 #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
252         const __addrpair __name = (__force __addrpair) ( \
253                                    (((__force __u64)(__be32)(__daddr)) << 32) | \
254                                    ((__force __u64)(__be32)(__saddr)))
255 #endif /* __BIG_ENDIAN */
256 #define INET_MATCH(__sk, __net, __cookie, __saddr, __daddr, __ports, __dif)     \
257         (((__sk)->sk_portpair == (__ports))                     &&      \
258          ((__sk)->sk_addrpair == (__cookie))                    &&      \
259          (!(__sk)->sk_bound_dev_if      ||                              \
260            ((__sk)->sk_bound_dev_if == (__dif)))                &&      \
261          net_eq(sock_net(__sk), (__net)))
262 #else /* 32-bit arch */
263 #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
264         const int __name __deprecated __attribute__((unused))
265
266 #define INET_MATCH(__sk, __net, __cookie, __saddr, __daddr, __ports, __dif) \
267         (((__sk)->sk_portpair == (__ports))             &&              \
268          ((__sk)->sk_daddr      == (__saddr))           &&              \
269          ((__sk)->sk_rcv_saddr  == (__daddr))           &&              \
270          (!(__sk)->sk_bound_dev_if      ||                              \
271            ((__sk)->sk_bound_dev_if == (__dif)))        &&              \
272          net_eq(sock_net(__sk), (__net)))
273 #endif /* 64-bit arch */
274
275 /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need
276  * not check it for lookups anymore, thanks Alexey. -DaveM
277  */
278 struct sock *__inet_lookup_established(struct net *net,
279                                        struct inet_hashinfo *hashinfo,
280                                        const __be32 saddr, const __be16 sport,
281                                        const __be32 daddr, const u16 hnum,
282                                        const int dif);
283
284 static inline struct sock *
285         inet_lookup_established(struct net *net, struct inet_hashinfo *hashinfo,
286                                 const __be32 saddr, const __be16 sport,
287                                 const __be32 daddr, const __be16 dport,
288                                 const int dif)
289 {
290         return __inet_lookup_established(net, hashinfo, saddr, sport, daddr,
291                                          ntohs(dport), dif);
292 }
293
294 static inline struct sock *__inet_lookup(struct net *net,
295                                          struct inet_hashinfo *hashinfo,
296                                          struct sk_buff *skb, int doff,
297                                          const __be32 saddr, const __be16 sport,
298                                          const __be32 daddr, const __be16 dport,
299                                          const int dif,
300                                          bool *refcounted)
301 {
302         u16 hnum = ntohs(dport);
303         struct sock *sk;
304
305         sk = __inet_lookup_established(net, hashinfo, saddr, sport,
306                                        daddr, hnum, dif);
307         *refcounted = true;
308         if (sk)
309                 return sk;
310         *refcounted = false;
311         return __inet_lookup_listener(net, hashinfo, skb, doff, saddr,
312                                       sport, daddr, hnum, dif);
313 }
314
315 static inline struct sock *inet_lookup(struct net *net,
316                                        struct inet_hashinfo *hashinfo,
317                                        struct sk_buff *skb, int doff,
318                                        const __be32 saddr, const __be16 sport,
319                                        const __be32 daddr, const __be16 dport,
320                                        const int dif)
321 {
322         struct sock *sk;
323         bool refcounted;
324
325         sk = __inet_lookup(net, hashinfo, skb, doff, saddr, sport, daddr,
326                            dport, dif, &refcounted);
327
328         if (sk && !refcounted && !atomic_inc_not_zero(&sk->sk_refcnt))
329                 sk = NULL;
330         return sk;
331 }
332
333 static inline struct sock *__inet_lookup_skb(struct inet_hashinfo *hashinfo,
334                                              struct sk_buff *skb,
335                                              int doff,
336                                              const __be16 sport,
337                                              const __be16 dport,
338                                              bool *refcounted)
339 {
340         struct sock *sk = skb_steal_sock(skb);
341         const struct iphdr *iph = ip_hdr(skb);
342
343         *refcounted = true;
344         if (sk)
345                 return sk;
346
347         return __inet_lookup(dev_net(skb_dst(skb)->dev), hashinfo, skb,
348                              doff, iph->saddr, sport,
349                              iph->daddr, dport, inet_iif(skb),
350                              refcounted);
351 }
352
353 u32 sk_ehashfn(const struct sock *sk);
354 u32 inet6_ehashfn(const struct net *net,
355                   const struct in6_addr *laddr, const u16 lport,
356                   const struct in6_addr *faddr, const __be16 fport);
357
358 static inline void sk_daddr_set(struct sock *sk, __be32 addr)
359 {
360         sk->sk_daddr = addr; /* alias of inet_daddr */
361 #if IS_ENABLED(CONFIG_IPV6)
362         ipv6_addr_set_v4mapped(addr, &sk->sk_v6_daddr);
363 #endif
364 }
365
366 static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr)
367 {
368         sk->sk_rcv_saddr = addr; /* alias of inet_rcv_saddr */
369 #if IS_ENABLED(CONFIG_IPV6)
370         ipv6_addr_set_v4mapped(addr, &sk->sk_v6_rcv_saddr);
371 #endif
372 }
373
374 int __inet_hash_connect(struct inet_timewait_death_row *death_row,
375                         struct sock *sk, u32 port_offset,
376                         int (*check_established)(struct inet_timewait_death_row *,
377                                                  struct sock *, __u16,
378                                                  struct inet_timewait_sock **));
379
380 int inet_hash_connect(struct inet_timewait_death_row *death_row,
381                       struct sock *sk);
382 #endif /* _INET_HASHTABLES_H */