GNU Linux-libre 4.9.309-gnu1
[releases.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         int                     num_owners;
84         struct hlist_node       node;
85         struct hlist_head       owners;
86 };
87
88 static inline struct net *ib_net(struct inet_bind_bucket *ib)
89 {
90         return read_pnet(&ib->ib_net);
91 }
92
93 #define inet_bind_bucket_for_each(tb, head) \
94         hlist_for_each_entry(tb, head, node)
95
96 struct inet_bind_hashbucket {
97         spinlock_t              lock;
98         struct hlist_head       chain;
99 };
100
101 /* Sockets can be hashed in established or listening table.
102  * We must use different 'nulls' end-of-chain value for all hash buckets :
103  * A socket might transition from ESTABLISH to LISTEN state without
104  * RCU grace period. A lookup in ehash table needs to handle this case.
105  */
106 #define LISTENING_NULLS_BASE (1U << 29)
107 struct inet_listen_hashbucket {
108         spinlock_t              lock;
109         union {
110                 struct hlist_head       head;
111                 struct hlist_nulls_head nulls_head;
112         };
113 };
114
115 /* This is for listening sockets, thus all sockets which possess wildcards. */
116 #define INET_LHTABLE_SIZE       32      /* Yes, really, this is all you need. */
117
118 struct inet_hashinfo {
119         /* This is for sockets with full identity only.  Sockets here will
120          * always be without wildcards and will have the following invariant:
121          *
122          *          TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
123          *
124          */
125         struct inet_ehash_bucket        *ehash;
126         spinlock_t                      *ehash_locks;
127         unsigned int                    ehash_mask;
128         unsigned int                    ehash_locks_mask;
129
130         /* Ok, let's try this, I give up, we do need a local binding
131          * TCP hash as well as the others for fast bind/connect.
132          */
133         struct inet_bind_hashbucket     *bhash;
134
135         unsigned int                    bhash_size;
136         /* 4 bytes hole on 64 bit */
137
138         struct kmem_cache               *bind_bucket_cachep;
139
140         /* All the above members are written once at bootup and
141          * never written again _or_ are predominantly read-access.
142          *
143          * Now align to a new cache line as all the following members
144          * might be often dirty.
145          */
146         /* All sockets in TCP_LISTEN state will be in here.  This is the only
147          * table where wildcard'd TCP sockets can exist.  Hash function here
148          * is just local port number.
149          */
150         struct inet_listen_hashbucket   listening_hash[INET_LHTABLE_SIZE]
151                                         ____cacheline_aligned_in_smp;
152 };
153
154 static inline struct inet_ehash_bucket *inet_ehash_bucket(
155         struct inet_hashinfo *hashinfo,
156         unsigned int hash)
157 {
158         return &hashinfo->ehash[hash & hashinfo->ehash_mask];
159 }
160
161 static inline spinlock_t *inet_ehash_lockp(
162         struct inet_hashinfo *hashinfo,
163         unsigned int hash)
164 {
165         return &hashinfo->ehash_locks[hash & hashinfo->ehash_locks_mask];
166 }
167
168 int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo);
169
170 static inline void inet_ehash_locks_free(struct inet_hashinfo *hashinfo)
171 {
172         kvfree(hashinfo->ehash_locks);
173         hashinfo->ehash_locks = NULL;
174 }
175
176 struct inet_bind_bucket *
177 inet_bind_bucket_create(struct kmem_cache *cachep, struct net *net,
178                         struct inet_bind_hashbucket *head,
179                         const unsigned short snum);
180 void inet_bind_bucket_destroy(struct kmem_cache *cachep,
181                               struct inet_bind_bucket *tb);
182
183 static inline u32 inet_bhashfn(const struct net *net, const __u16 lport,
184                                const u32 bhash_size)
185 {
186         return (lport + net_hash_mix(net)) & (bhash_size - 1);
187 }
188
189 void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
190                     const unsigned short snum);
191
192 /* These can have wildcards, don't try too hard. */
193 static inline u32 inet_lhashfn(const struct net *net, const unsigned short num)
194 {
195         return (num + net_hash_mix(net)) & (INET_LHTABLE_SIZE - 1);
196 }
197
198 static inline int inet_sk_listen_hashfn(const struct sock *sk)
199 {
200         return inet_lhashfn(sock_net(sk), inet_sk(sk)->inet_num);
201 }
202
203 /* Caller must disable local BH processing. */
204 int __inet_inherit_port(const struct sock *sk, struct sock *child);
205
206 void inet_put_port(struct sock *sk);
207
208 void inet_hashinfo_init(struct inet_hashinfo *h);
209
210 bool inet_ehash_insert(struct sock *sk, struct sock *osk);
211 bool inet_ehash_nolisten(struct sock *sk, struct sock *osk);
212 int __inet_hash(struct sock *sk, struct sock *osk,
213                 int (*saddr_same)(const struct sock *sk1,
214                                   const struct sock *sk2,
215                                   bool match_wildcard));
216 int inet_hash(struct sock *sk);
217 void inet_unhash(struct sock *sk);
218
219 struct sock *__inet_lookup_listener(struct net *net,
220                                     struct inet_hashinfo *hashinfo,
221                                     struct sk_buff *skb, int doff,
222                                     const __be32 saddr, const __be16 sport,
223                                     const __be32 daddr,
224                                     const unsigned short hnum,
225                                     const int dif);
226
227 static inline struct sock *inet_lookup_listener(struct net *net,
228                 struct inet_hashinfo *hashinfo,
229                 struct sk_buff *skb, int doff,
230                 __be32 saddr, __be16 sport,
231                 __be32 daddr, __be16 dport, int dif)
232 {
233         return __inet_lookup_listener(net, hashinfo, skb, doff, saddr, sport,
234                                       daddr, ntohs(dport), dif);
235 }
236
237 /* Socket demux engine toys. */
238 /* What happens here is ugly; there's a pair of adjacent fields in
239    struct inet_sock; __be16 dport followed by __u16 num.  We want to
240    search by pair, so we combine the keys into a single 32bit value
241    and compare with 32bit value read from &...->dport.  Let's at least
242    make sure that it's not mixed with anything else...
243    On 64bit targets we combine comparisons with pair of adjacent __be32
244    fields in the same way.
245 */
246 #ifdef __BIG_ENDIAN
247 #define INET_COMBINED_PORTS(__sport, __dport) \
248         ((__force __portpair)(((__force __u32)(__be16)(__sport) << 16) | (__u32)(__dport)))
249 #else /* __LITTLE_ENDIAN */
250 #define INET_COMBINED_PORTS(__sport, __dport) \
251         ((__force __portpair)(((__u32)(__dport) << 16) | (__force __u32)(__be16)(__sport)))
252 #endif
253
254 #if (BITS_PER_LONG == 64)
255 #ifdef __BIG_ENDIAN
256 #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
257         const __addrpair __name = (__force __addrpair) ( \
258                                    (((__force __u64)(__be32)(__saddr)) << 32) | \
259                                    ((__force __u64)(__be32)(__daddr)))
260 #else /* __LITTLE_ENDIAN */
261 #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
262         const __addrpair __name = (__force __addrpair) ( \
263                                    (((__force __u64)(__be32)(__daddr)) << 32) | \
264                                    ((__force __u64)(__be32)(__saddr)))
265 #endif /* __BIG_ENDIAN */
266 #define INET_MATCH(__sk, __net, __cookie, __saddr, __daddr, __ports, __dif)     \
267         (((__sk)->sk_portpair == (__ports))                     &&      \
268          ((__sk)->sk_addrpair == (__cookie))                    &&      \
269          (!(__sk)->sk_bound_dev_if      ||                              \
270            ((__sk)->sk_bound_dev_if == (__dif)))                &&      \
271          net_eq(sock_net(__sk), (__net)))
272 #else /* 32-bit arch */
273 #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
274         const int __name __deprecated __attribute__((unused))
275
276 #define INET_MATCH(__sk, __net, __cookie, __saddr, __daddr, __ports, __dif) \
277         (((__sk)->sk_portpair == (__ports))             &&              \
278          ((__sk)->sk_daddr      == (__saddr))           &&              \
279          ((__sk)->sk_rcv_saddr  == (__daddr))           &&              \
280          (!(__sk)->sk_bound_dev_if      ||                              \
281            ((__sk)->sk_bound_dev_if == (__dif)))        &&              \
282          net_eq(sock_net(__sk), (__net)))
283 #endif /* 64-bit arch */
284
285 /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need
286  * not check it for lookups anymore, thanks Alexey. -DaveM
287  */
288 struct sock *__inet_lookup_established(struct net *net,
289                                        struct inet_hashinfo *hashinfo,
290                                        const __be32 saddr, const __be16 sport,
291                                        const __be32 daddr, const u16 hnum,
292                                        const int dif);
293
294 static inline struct sock *
295         inet_lookup_established(struct net *net, struct inet_hashinfo *hashinfo,
296                                 const __be32 saddr, const __be16 sport,
297                                 const __be32 daddr, const __be16 dport,
298                                 const int dif)
299 {
300         return __inet_lookup_established(net, hashinfo, saddr, sport, daddr,
301                                          ntohs(dport), dif);
302 }
303
304 static inline struct sock *__inet_lookup(struct net *net,
305                                          struct inet_hashinfo *hashinfo,
306                                          struct sk_buff *skb, int doff,
307                                          const __be32 saddr, const __be16 sport,
308                                          const __be32 daddr, const __be16 dport,
309                                          const int dif,
310                                          bool *refcounted)
311 {
312         u16 hnum = ntohs(dport);
313         struct sock *sk;
314
315         sk = __inet_lookup_established(net, hashinfo, saddr, sport,
316                                        daddr, hnum, dif);
317         *refcounted = true;
318         if (sk)
319                 return sk;
320         *refcounted = false;
321         return __inet_lookup_listener(net, hashinfo, skb, doff, saddr,
322                                       sport, daddr, hnum, dif);
323 }
324
325 static inline struct sock *inet_lookup(struct net *net,
326                                        struct inet_hashinfo *hashinfo,
327                                        struct sk_buff *skb, int doff,
328                                        const __be32 saddr, const __be16 sport,
329                                        const __be32 daddr, const __be16 dport,
330                                        const int dif)
331 {
332         struct sock *sk;
333         bool refcounted;
334
335         sk = __inet_lookup(net, hashinfo, skb, doff, saddr, sport, daddr,
336                            dport, dif, &refcounted);
337
338         if (sk && !refcounted && !atomic_inc_not_zero(&sk->sk_refcnt))
339                 sk = NULL;
340         return sk;
341 }
342
343 static inline struct sock *__inet_lookup_skb(struct inet_hashinfo *hashinfo,
344                                              struct sk_buff *skb,
345                                              int doff,
346                                              const __be16 sport,
347                                              const __be16 dport,
348                                              bool *refcounted)
349 {
350         struct sock *sk = skb_steal_sock(skb);
351         const struct iphdr *iph = ip_hdr(skb);
352
353         *refcounted = true;
354         if (sk)
355                 return sk;
356
357         return __inet_lookup(dev_net(skb_dst(skb)->dev), hashinfo, skb,
358                              doff, iph->saddr, sport,
359                              iph->daddr, dport, inet_iif(skb),
360                              refcounted);
361 }
362
363 u32 sk_ehashfn(const struct sock *sk);
364 u32 inet6_ehashfn(const struct net *net,
365                   const struct in6_addr *laddr, const u16 lport,
366                   const struct in6_addr *faddr, const __be16 fport);
367
368 static inline void sk_daddr_set(struct sock *sk, __be32 addr)
369 {
370         sk->sk_daddr = addr; /* alias of inet_daddr */
371 #if IS_ENABLED(CONFIG_IPV6)
372         ipv6_addr_set_v4mapped(addr, &sk->sk_v6_daddr);
373 #endif
374 }
375
376 static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr)
377 {
378         sk->sk_rcv_saddr = addr; /* alias of inet_rcv_saddr */
379 #if IS_ENABLED(CONFIG_IPV6)
380         ipv6_addr_set_v4mapped(addr, &sk->sk_v6_rcv_saddr);
381 #endif
382 }
383
384 int __inet_hash_connect(struct inet_timewait_death_row *death_row,
385                         struct sock *sk, u32 port_offset,
386                         int (*check_established)(struct inet_timewait_death_row *,
387                                                  struct sock *, __u16,
388                                                  struct inet_timewait_sock **));
389
390 int inet_hash_connect(struct inet_timewait_death_row *death_row,
391                       struct sock *sk);
392 #endif /* _INET_HASHTABLES_H */