inet_hashtables.h 14 KB

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