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 _INET_HASHTABLES_H
15 #define _INET_HASHTABLES_H
18 #include <linux/interrupt.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>
28 #include <net/inet_connection_sock.h>
29 #include <net/inet_sock.h>
31 #include <net/route.h>
32 #include <net/tcp_states.h>
33 #include <net/netns/hash.h>
35 #include <linux/atomic.h>
36 #include <asm/byteorder.h>
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
42 struct inet_ehash_bucket {
43 struct hlist_nulls_head chain;
46 /* There are a few simple rules, which allow for local port reuse by
47 * an application. In essence:
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
57 * Failing this, the port cannot be shared.
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.
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
77 struct inet_bind_bucket {
78 possible_net_t ib_net;
80 signed char fastreuse;
81 signed char fastreuseport;
83 struct hlist_node node;
84 struct hlist_head owners;
87 static inline struct net *ib_net(struct inet_bind_bucket *ib)
89 return read_pnet(&ib->ib_net);
92 #define inet_bind_bucket_for_each(tb, head) \
93 hlist_for_each_entry(tb, head, node)
95 struct inet_bind_hashbucket {
97 struct hlist_head chain;
101 * Sockets can be hashed in established or listening table
103 struct inet_listen_hashbucket {
105 struct hlist_head head;
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. */
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:
115 * TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
118 struct inet_ehash_bucket *ehash;
119 spinlock_t *ehash_locks;
120 unsigned int ehash_mask;
121 unsigned int ehash_locks_mask;
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.
126 struct inet_bind_hashbucket *bhash;
128 unsigned int bhash_size;
129 /* 4 bytes hole on 64 bit */
131 struct kmem_cache *bind_bucket_cachep;
133 /* All the above members are written once at bootup and
134 * never written again _or_ are predominantly read-access.
136 * Now align to a new cache line as all the following members
137 * might be often dirty.
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.
143 struct inet_listen_hashbucket listening_hash[INET_LHTABLE_SIZE]
144 ____cacheline_aligned_in_smp;
147 static inline struct inet_ehash_bucket *inet_ehash_bucket(
148 struct inet_hashinfo *hashinfo,
151 return &hashinfo->ehash[hash & hashinfo->ehash_mask];
154 static inline spinlock_t *inet_ehash_lockp(
155 struct inet_hashinfo *hashinfo,
158 return &hashinfo->ehash_locks[hash & hashinfo->ehash_locks_mask];
161 int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo);
163 static inline void inet_ehash_locks_free(struct inet_hashinfo *hashinfo)
165 kvfree(hashinfo->ehash_locks);
166 hashinfo->ehash_locks = NULL;
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);
176 static inline u32 inet_bhashfn(const struct net *net, const __u16 lport,
177 const u32 bhash_size)
179 return (lport + net_hash_mix(net)) & (bhash_size - 1);
182 void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
183 const unsigned short snum);
185 /* These can have wildcards, don't try too hard. */
186 static inline u32 inet_lhashfn(const struct net *net, const unsigned short num)
188 return (num + net_hash_mix(net)) & (INET_LHTABLE_SIZE - 1);
191 static inline int inet_sk_listen_hashfn(const struct sock *sk)
193 return inet_lhashfn(sock_net(sk), inet_sk(sk)->inet_num);
196 /* Caller must disable local BH processing. */
197 int __inet_inherit_port(const struct sock *sk, struct sock *child);
199 void inet_put_port(struct sock *sk);
201 void inet_hashinfo_init(struct inet_hashinfo *h);
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);
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,
214 const unsigned short hnum,
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)
223 return __inet_lookup_listener(net, hashinfo, skb, doff, saddr, sport,
224 daddr, ntohs(dport), dif);
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.
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)))
244 #if (BITS_PER_LONG == 64)
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))
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 */
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
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,
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,
290 return __inet_lookup_established(net, hashinfo, saddr, sport, daddr,
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,
302 u16 hnum = ntohs(dport);
305 sk = __inet_lookup_established(net, hashinfo, saddr, sport,
311 return __inet_lookup_listener(net, hashinfo, skb, doff, saddr,
312 sport, daddr, hnum, dif);
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,
325 sk = __inet_lookup(net, hashinfo, skb, doff, saddr, sport, daddr,
326 dport, dif, &refcounted);
328 if (sk && !refcounted && !atomic_inc_not_zero(&sk->sk_refcnt))
333 static inline struct sock *__inet_lookup_skb(struct inet_hashinfo *hashinfo,
340 struct sock *sk = skb_steal_sock(skb);
341 const struct iphdr *iph = ip_hdr(skb);
347 return __inet_lookup(dev_net(skb_dst(skb)->dev), hashinfo, skb,
348 doff, iph->saddr, sport,
349 iph->daddr, dport, inet_iif(skb),
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);
358 static inline void sk_daddr_set(struct sock *sk, __be32 addr)
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);
366 static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr)
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);
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 **));
380 int inet_hash_connect(struct inet_timewait_death_row *death_row,
382 #endif /* _INET_HASHTABLES_H */