inet_hashtables.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * Authors: Lotsa people, from code originally in tcp
  8. */
  9. #ifndef _INET_HASHTABLES_H
  10. #define _INET_HASHTABLES_H
  11. #include <linux/interrupt.h>
  12. #include <linux/ip.h>
  13. #include <linux/ipv6.h>
  14. #include <linux/list.h>
  15. #include <linux/slab.h>
  16. #include <linux/socket.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/types.h>
  19. #include <linux/wait.h>
  20. #include <net/inet_connection_sock.h>
  21. #include <net/inet_sock.h>
  22. #include <net/ip.h>
  23. #include <net/sock.h>
  24. #include <net/route.h>
  25. #include <net/tcp_states.h>
  26. #include <net/netns/hash.h>
  27. #include <linux/refcount.h>
  28. #include <asm/byteorder.h>
  29. /* This is for all connections with a full identity, no wildcards.
  30. * The 'e' prefix stands for Establish, but we really put all sockets
  31. * but LISTEN ones.
  32. */
  33. struct inet_ehash_bucket {
  34. struct hlist_nulls_head chain;
  35. };
  36. /* There are a few simple rules, which allow for local port reuse by
  37. * an application. In essence:
  38. *
  39. * 1) Sockets bound to different interfaces may share a local port.
  40. * Failing that, goto test 2.
  41. * 2) If all sockets have sk->sk_reuse set, and none of them are in
  42. * TCP_LISTEN state, the port may be shared.
  43. * Failing that, goto test 3.
  44. * 3) If all sockets are bound to a specific inet_sk(sk)->rcv_saddr local
  45. * address, and none of them are the same, the port may be
  46. * shared.
  47. * Failing this, the port cannot be shared.
  48. *
  49. * The interesting point, is test #2. This is what an FTP server does
  50. * all day. To optimize this case we use a specific flag bit defined
  51. * below. As we add sockets to a bind bucket list, we perform a
  52. * check of: (newsk->sk_reuse && (newsk->sk_state != TCP_LISTEN))
  53. * As long as all sockets added to a bind bucket pass this test,
  54. * the flag bit will be set.
  55. * The resulting situation is that tcp_v[46]_verify_bind() can just check
  56. * for this flag bit, if it is set and the socket trying to bind has
  57. * sk->sk_reuse set, we don't even have to walk the owners list at all,
  58. * we return that it is ok to bind this socket to the requested local port.
  59. *
  60. * Sounds like a lot of work, but it is worth it. In a more naive
  61. * implementation (ie. current FreeBSD etc.) the entire list of ports
  62. * must be walked for each data port opened by an ftp server. Needless
  63. * to say, this does not scale at all. With a couple thousand FTP
  64. * users logged onto your box, isn't it nice to know that new data
  65. * ports are created in O(1) time? I thought so. ;-) -DaveM
  66. */
  67. #define FASTREUSEPORT_ANY 1
  68. #define FASTREUSEPORT_STRICT 2
  69. struct inet_bind_bucket {
  70. possible_net_t ib_net;
  71. int l3mdev;
  72. unsigned short port;
  73. signed char fastreuse;
  74. signed char fastreuseport;
  75. kuid_t fastuid;
  76. #if IS_ENABLED(CONFIG_IPV6)
  77. struct in6_addr fast_v6_rcv_saddr;
  78. #endif
  79. __be32 fast_rcv_saddr;
  80. unsigned short fast_sk_family;
  81. bool fast_ipv6_only;
  82. struct hlist_node node;
  83. struct hlist_head owners;
  84. };
  85. struct inet_bind2_bucket {
  86. possible_net_t ib_net;
  87. int l3mdev;
  88. unsigned short port;
  89. #if IS_ENABLED(CONFIG_IPV6)
  90. unsigned short family;
  91. #endif
  92. union {
  93. #if IS_ENABLED(CONFIG_IPV6)
  94. struct in6_addr v6_rcv_saddr;
  95. #endif
  96. __be32 rcv_saddr;
  97. };
  98. /* Node in the bhash2 inet_bind_hashbucket chain */
  99. struct hlist_node node;
  100. /* List of sockets hashed to this bucket */
  101. struct hlist_head owners;
  102. /* bhash has twsk in owners, but bhash2 has twsk in
  103. * deathrow not to add a member in struct sock_common.
  104. */
  105. struct hlist_head deathrow;
  106. };
  107. static inline struct net *ib_net(const struct inet_bind_bucket *ib)
  108. {
  109. return read_pnet(&ib->ib_net);
  110. }
  111. static inline struct net *ib2_net(const struct inet_bind2_bucket *ib)
  112. {
  113. return read_pnet(&ib->ib_net);
  114. }
  115. #define inet_bind_bucket_for_each(tb, head) \
  116. hlist_for_each_entry(tb, head, node)
  117. struct inet_bind_hashbucket {
  118. spinlock_t lock;
  119. struct hlist_head chain;
  120. };
  121. /* Sockets can be hashed in established or listening table.
  122. * We must use different 'nulls' end-of-chain value for all hash buckets :
  123. * A socket might transition from ESTABLISH to LISTEN state without
  124. * RCU grace period. A lookup in ehash table needs to handle this case.
  125. */
  126. #define LISTENING_NULLS_BASE (1U << 29)
  127. struct inet_listen_hashbucket {
  128. spinlock_t lock;
  129. struct hlist_nulls_head nulls_head;
  130. };
  131. /* This is for listening sockets, thus all sockets which possess wildcards. */
  132. #define INET_LHTABLE_SIZE 32 /* Yes, really, this is all you need. */
  133. struct inet_hashinfo {
  134. /* This is for sockets with full identity only. Sockets here will
  135. * always be without wildcards and will have the following invariant:
  136. *
  137. * TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
  138. *
  139. */
  140. struct inet_ehash_bucket *ehash;
  141. spinlock_t *ehash_locks;
  142. unsigned int ehash_mask;
  143. unsigned int ehash_locks_mask;
  144. /* Ok, let's try this, I give up, we do need a local binding
  145. * TCP hash as well as the others for fast bind/connect.
  146. */
  147. struct kmem_cache *bind_bucket_cachep;
  148. /* This bind table is hashed by local port */
  149. struct inet_bind_hashbucket *bhash;
  150. struct kmem_cache *bind2_bucket_cachep;
  151. /* This bind table is hashed by local port and sk->sk_rcv_saddr (ipv4)
  152. * or sk->sk_v6_rcv_saddr (ipv6). This 2nd bind table is used
  153. * primarily for expediting bind conflict resolution.
  154. */
  155. struct inet_bind_hashbucket *bhash2;
  156. unsigned int bhash_size;
  157. /* The 2nd listener table hashed by local port and address */
  158. unsigned int lhash2_mask;
  159. struct inet_listen_hashbucket *lhash2;
  160. bool pernet;
  161. };
  162. static inline struct inet_hashinfo *tcp_or_dccp_get_hashinfo(const struct sock *sk)
  163. {
  164. #if IS_ENABLED(CONFIG_IP_DCCP)
  165. return sk->sk_prot->h.hashinfo ? :
  166. sock_net(sk)->ipv4.tcp_death_row.hashinfo;
  167. #else
  168. return sock_net(sk)->ipv4.tcp_death_row.hashinfo;
  169. #endif
  170. }
  171. static inline struct inet_listen_hashbucket *
  172. inet_lhash2_bucket(struct inet_hashinfo *h, u32 hash)
  173. {
  174. return &h->lhash2[hash & h->lhash2_mask];
  175. }
  176. static inline struct inet_ehash_bucket *inet_ehash_bucket(
  177. struct inet_hashinfo *hashinfo,
  178. unsigned int hash)
  179. {
  180. return &hashinfo->ehash[hash & hashinfo->ehash_mask];
  181. }
  182. static inline spinlock_t *inet_ehash_lockp(
  183. struct inet_hashinfo *hashinfo,
  184. unsigned int hash)
  185. {
  186. return &hashinfo->ehash_locks[hash & hashinfo->ehash_locks_mask];
  187. }
  188. int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo);
  189. static inline void inet_hashinfo2_free_mod(struct inet_hashinfo *h)
  190. {
  191. kfree(h->lhash2);
  192. h->lhash2 = NULL;
  193. }
  194. static inline void inet_ehash_locks_free(struct inet_hashinfo *hashinfo)
  195. {
  196. kvfree(hashinfo->ehash_locks);
  197. hashinfo->ehash_locks = NULL;
  198. }
  199. struct inet_hashinfo *inet_pernet_hashinfo_alloc(struct inet_hashinfo *hashinfo,
  200. unsigned int ehash_entries);
  201. void inet_pernet_hashinfo_free(struct inet_hashinfo *hashinfo);
  202. struct inet_bind_bucket *
  203. inet_bind_bucket_create(struct kmem_cache *cachep, struct net *net,
  204. struct inet_bind_hashbucket *head,
  205. const unsigned short snum, int l3mdev);
  206. void inet_bind_bucket_destroy(struct kmem_cache *cachep,
  207. struct inet_bind_bucket *tb);
  208. bool inet_bind_bucket_match(const struct inet_bind_bucket *tb,
  209. const struct net *net, unsigned short port,
  210. int l3mdev);
  211. struct inet_bind2_bucket *
  212. inet_bind2_bucket_create(struct kmem_cache *cachep, struct net *net,
  213. struct inet_bind_hashbucket *head,
  214. unsigned short port, int l3mdev,
  215. const struct sock *sk);
  216. void inet_bind2_bucket_destroy(struct kmem_cache *cachep,
  217. struct inet_bind2_bucket *tb);
  218. struct inet_bind2_bucket *
  219. inet_bind2_bucket_find(const struct inet_bind_hashbucket *head,
  220. const struct net *net,
  221. unsigned short port, int l3mdev,
  222. const struct sock *sk);
  223. bool inet_bind2_bucket_match_addr_any(const struct inet_bind2_bucket *tb,
  224. const struct net *net, unsigned short port,
  225. int l3mdev, const struct sock *sk);
  226. static inline u32 inet_bhashfn(const struct net *net, const __u16 lport,
  227. const u32 bhash_size)
  228. {
  229. return (lport + net_hash_mix(net)) & (bhash_size - 1);
  230. }
  231. static inline struct inet_bind_hashbucket *
  232. inet_bhashfn_portaddr(const struct inet_hashinfo *hinfo, const struct sock *sk,
  233. const struct net *net, unsigned short port)
  234. {
  235. u32 hash;
  236. #if IS_ENABLED(CONFIG_IPV6)
  237. if (sk->sk_family == AF_INET6)
  238. hash = ipv6_portaddr_hash(net, &sk->sk_v6_rcv_saddr, port);
  239. else
  240. #endif
  241. hash = ipv4_portaddr_hash(net, sk->sk_rcv_saddr, port);
  242. return &hinfo->bhash2[hash & (hinfo->bhash_size - 1)];
  243. }
  244. struct inet_bind_hashbucket *
  245. inet_bhash2_addr_any_hashbucket(const struct sock *sk, const struct net *net, int port);
  246. /* This should be called whenever a socket's sk_rcv_saddr (ipv4) or
  247. * sk_v6_rcv_saddr (ipv6) changes after it has been binded. The socket's
  248. * rcv_saddr field should already have been updated when this is called.
  249. */
  250. int inet_bhash2_update_saddr(struct sock *sk, void *saddr, int family);
  251. void inet_bhash2_reset_saddr(struct sock *sk);
  252. void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  253. struct inet_bind2_bucket *tb2, unsigned short port);
  254. /* Caller must disable local BH processing. */
  255. int __inet_inherit_port(const struct sock *sk, struct sock *child);
  256. void inet_put_port(struct sock *sk);
  257. void inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
  258. unsigned long numentries, int scale,
  259. unsigned long low_limit,
  260. unsigned long high_limit);
  261. int inet_hashinfo2_init_mod(struct inet_hashinfo *h);
  262. bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk);
  263. bool inet_ehash_nolisten(struct sock *sk, struct sock *osk,
  264. bool *found_dup_sk);
  265. int __inet_hash(struct sock *sk, struct sock *osk);
  266. int inet_hash(struct sock *sk);
  267. void inet_unhash(struct sock *sk);
  268. struct sock *__inet_lookup_listener(struct net *net,
  269. struct inet_hashinfo *hashinfo,
  270. struct sk_buff *skb, int doff,
  271. const __be32 saddr, const __be16 sport,
  272. const __be32 daddr,
  273. const unsigned short hnum,
  274. const int dif, const int sdif);
  275. static inline struct sock *inet_lookup_listener(struct net *net,
  276. struct inet_hashinfo *hashinfo,
  277. struct sk_buff *skb, int doff,
  278. __be32 saddr, __be16 sport,
  279. __be32 daddr, __be16 dport, int dif, int sdif)
  280. {
  281. return __inet_lookup_listener(net, hashinfo, skb, doff, saddr, sport,
  282. daddr, ntohs(dport), dif, sdif);
  283. }
  284. /* Socket demux engine toys. */
  285. /* What happens here is ugly; there's a pair of adjacent fields in
  286. struct inet_sock; __be16 dport followed by __u16 num. We want to
  287. search by pair, so we combine the keys into a single 32bit value
  288. and compare with 32bit value read from &...->dport. Let's at least
  289. make sure that it's not mixed with anything else...
  290. On 64bit targets we combine comparisons with pair of adjacent __be32
  291. fields in the same way.
  292. */
  293. #ifdef __BIG_ENDIAN
  294. #define INET_COMBINED_PORTS(__sport, __dport) \
  295. ((__force __portpair)(((__force __u32)(__be16)(__sport) << 16) | (__u32)(__dport)))
  296. #else /* __LITTLE_ENDIAN */
  297. #define INET_COMBINED_PORTS(__sport, __dport) \
  298. ((__force __portpair)(((__u32)(__dport) << 16) | (__force __u32)(__be16)(__sport)))
  299. #endif
  300. #ifdef __BIG_ENDIAN
  301. #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
  302. const __addrpair __name = (__force __addrpair) ( \
  303. (((__force __u64)(__be32)(__saddr)) << 32) | \
  304. ((__force __u64)(__be32)(__daddr)))
  305. #else /* __LITTLE_ENDIAN */
  306. #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
  307. const __addrpair __name = (__force __addrpair) ( \
  308. (((__force __u64)(__be32)(__daddr)) << 32) | \
  309. ((__force __u64)(__be32)(__saddr)))
  310. #endif /* __BIG_ENDIAN */
  311. static inline bool inet_match(struct net *net, const struct sock *sk,
  312. const __addrpair cookie, const __portpair ports,
  313. int dif, int sdif)
  314. {
  315. if (!net_eq(sock_net(sk), net) ||
  316. sk->sk_portpair != ports ||
  317. sk->sk_addrpair != cookie)
  318. return false;
  319. /* READ_ONCE() paired with WRITE_ONCE() in sock_bindtoindex_locked() */
  320. return inet_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif,
  321. sdif);
  322. }
  323. /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need
  324. * not check it for lookups anymore, thanks Alexey. -DaveM
  325. */
  326. struct sock *__inet_lookup_established(struct net *net,
  327. struct inet_hashinfo *hashinfo,
  328. const __be32 saddr, const __be16 sport,
  329. const __be32 daddr, const u16 hnum,
  330. const int dif, const int sdif);
  331. static inline struct sock *
  332. inet_lookup_established(struct net *net, struct inet_hashinfo *hashinfo,
  333. const __be32 saddr, const __be16 sport,
  334. const __be32 daddr, const __be16 dport,
  335. const int dif)
  336. {
  337. return __inet_lookup_established(net, hashinfo, saddr, sport, daddr,
  338. ntohs(dport), dif, 0);
  339. }
  340. static inline struct sock *__inet_lookup(struct net *net,
  341. struct inet_hashinfo *hashinfo,
  342. struct sk_buff *skb, int doff,
  343. const __be32 saddr, const __be16 sport,
  344. const __be32 daddr, const __be16 dport,
  345. const int dif, const int sdif,
  346. bool *refcounted)
  347. {
  348. u16 hnum = ntohs(dport);
  349. struct sock *sk;
  350. sk = __inet_lookup_established(net, hashinfo, saddr, sport,
  351. daddr, hnum, dif, sdif);
  352. *refcounted = true;
  353. if (sk)
  354. return sk;
  355. *refcounted = false;
  356. return __inet_lookup_listener(net, hashinfo, skb, doff, saddr,
  357. sport, daddr, hnum, dif, sdif);
  358. }
  359. static inline struct sock *inet_lookup(struct net *net,
  360. struct inet_hashinfo *hashinfo,
  361. struct sk_buff *skb, int doff,
  362. const __be32 saddr, const __be16 sport,
  363. const __be32 daddr, const __be16 dport,
  364. const int dif)
  365. {
  366. struct sock *sk;
  367. bool refcounted;
  368. sk = __inet_lookup(net, hashinfo, skb, doff, saddr, sport, daddr,
  369. dport, dif, 0, &refcounted);
  370. if (sk && !refcounted && !refcount_inc_not_zero(&sk->sk_refcnt))
  371. sk = NULL;
  372. return sk;
  373. }
  374. static inline struct sock *__inet_lookup_skb(struct inet_hashinfo *hashinfo,
  375. struct sk_buff *skb,
  376. int doff,
  377. const __be16 sport,
  378. const __be16 dport,
  379. const int sdif,
  380. bool *refcounted)
  381. {
  382. struct sock *sk = skb_steal_sock(skb, refcounted);
  383. const struct iphdr *iph = ip_hdr(skb);
  384. if (sk)
  385. return sk;
  386. return __inet_lookup(dev_net(skb_dst(skb)->dev), hashinfo, skb,
  387. doff, iph->saddr, sport,
  388. iph->daddr, dport, inet_iif(skb), sdif,
  389. refcounted);
  390. }
  391. u32 inet6_ehashfn(const struct net *net,
  392. const struct in6_addr *laddr, const u16 lport,
  393. const struct in6_addr *faddr, const __be16 fport);
  394. static inline void sk_daddr_set(struct sock *sk, __be32 addr)
  395. {
  396. sk->sk_daddr = addr; /* alias of inet_daddr */
  397. #if IS_ENABLED(CONFIG_IPV6)
  398. ipv6_addr_set_v4mapped(addr, &sk->sk_v6_daddr);
  399. #endif
  400. }
  401. static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr)
  402. {
  403. sk->sk_rcv_saddr = addr; /* alias of inet_rcv_saddr */
  404. #if IS_ENABLED(CONFIG_IPV6)
  405. ipv6_addr_set_v4mapped(addr, &sk->sk_v6_rcv_saddr);
  406. #endif
  407. }
  408. int __inet_hash_connect(struct inet_timewait_death_row *death_row,
  409. struct sock *sk, u64 port_offset,
  410. int (*check_established)(struct inet_timewait_death_row *,
  411. struct sock *, __u16,
  412. struct inet_timewait_sock **));
  413. int inet_hash_connect(struct inet_timewait_death_row *death_row,
  414. struct sock *sk);
  415. #endif /* _INET_HASHTABLES_H */