inet_hashtables.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 bhash2;
  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 addr_type;
  91. struct in6_addr v6_rcv_saddr;
  92. #define rcv_saddr v6_rcv_saddr.s6_addr32[3]
  93. #else
  94. __be32 rcv_saddr;
  95. #endif
  96. /* Node in the bhash2 inet_bind_hashbucket chain */
  97. struct hlist_node node;
  98. struct hlist_node bhash_node;
  99. /* List of sockets hashed to this bucket */
  100. struct hlist_head owners;
  101. };
  102. static inline struct net *ib_net(const struct inet_bind_bucket *ib)
  103. {
  104. return read_pnet(&ib->ib_net);
  105. }
  106. static inline struct net *ib2_net(const struct inet_bind2_bucket *ib)
  107. {
  108. return read_pnet(&ib->ib_net);
  109. }
  110. #define inet_bind_bucket_for_each(tb, head) \
  111. hlist_for_each_entry(tb, head, node)
  112. struct inet_bind_hashbucket {
  113. spinlock_t lock;
  114. struct hlist_head chain;
  115. };
  116. /* Sockets can be hashed in established or listening table.
  117. * We must use different 'nulls' end-of-chain value for all hash buckets :
  118. * A socket might transition from ESTABLISH to LISTEN state without
  119. * RCU grace period. A lookup in ehash table needs to handle this case.
  120. */
  121. #define LISTENING_NULLS_BASE (1U << 29)
  122. struct inet_listen_hashbucket {
  123. spinlock_t lock;
  124. struct hlist_nulls_head nulls_head;
  125. };
  126. /* This is for listening sockets, thus all sockets which possess wildcards. */
  127. #define INET_LHTABLE_SIZE 32 /* Yes, really, this is all you need. */
  128. struct inet_hashinfo {
  129. /* This is for sockets with full identity only. Sockets here will
  130. * always be without wildcards and will have the following invariant:
  131. *
  132. * TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
  133. *
  134. */
  135. struct inet_ehash_bucket *ehash;
  136. spinlock_t *ehash_locks;
  137. unsigned int ehash_mask;
  138. unsigned int ehash_locks_mask;
  139. /* Ok, let's try this, I give up, we do need a local binding
  140. * TCP hash as well as the others for fast bind/connect.
  141. */
  142. struct kmem_cache *bind_bucket_cachep;
  143. /* This bind table is hashed by local port */
  144. struct inet_bind_hashbucket *bhash;
  145. struct kmem_cache *bind2_bucket_cachep;
  146. /* This bind table is hashed by local port and sk->sk_rcv_saddr (ipv4)
  147. * or sk->sk_v6_rcv_saddr (ipv6). This 2nd bind table is used
  148. * primarily for expediting bind conflict resolution.
  149. */
  150. struct inet_bind_hashbucket *bhash2;
  151. unsigned int bhash_size;
  152. /* The 2nd listener table hashed by local port and address */
  153. unsigned int lhash2_mask;
  154. struct inet_listen_hashbucket *lhash2;
  155. bool pernet;
  156. } ____cacheline_aligned_in_smp;
  157. static inline struct inet_hashinfo *tcp_or_dccp_get_hashinfo(const struct sock *sk)
  158. {
  159. #if IS_ENABLED(CONFIG_IP_DCCP)
  160. return sk->sk_prot->h.hashinfo ? :
  161. sock_net(sk)->ipv4.tcp_death_row.hashinfo;
  162. #else
  163. return sock_net(sk)->ipv4.tcp_death_row.hashinfo;
  164. #endif
  165. }
  166. static inline struct inet_listen_hashbucket *
  167. inet_lhash2_bucket(struct inet_hashinfo *h, u32 hash)
  168. {
  169. return &h->lhash2[hash & h->lhash2_mask];
  170. }
  171. static inline struct inet_ehash_bucket *inet_ehash_bucket(
  172. struct inet_hashinfo *hashinfo,
  173. unsigned int hash)
  174. {
  175. return &hashinfo->ehash[hash & hashinfo->ehash_mask];
  176. }
  177. static inline spinlock_t *inet_ehash_lockp(
  178. struct inet_hashinfo *hashinfo,
  179. unsigned int hash)
  180. {
  181. return &hashinfo->ehash_locks[hash & hashinfo->ehash_locks_mask];
  182. }
  183. int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo);
  184. static inline void inet_hashinfo2_free_mod(struct inet_hashinfo *h)
  185. {
  186. kfree(h->lhash2);
  187. h->lhash2 = NULL;
  188. }
  189. static inline void inet_ehash_locks_free(struct inet_hashinfo *hashinfo)
  190. {
  191. kvfree(hashinfo->ehash_locks);
  192. hashinfo->ehash_locks = NULL;
  193. }
  194. struct inet_hashinfo *inet_pernet_hashinfo_alloc(struct inet_hashinfo *hashinfo,
  195. unsigned int ehash_entries);
  196. void inet_pernet_hashinfo_free(struct inet_hashinfo *hashinfo);
  197. struct inet_bind_bucket *
  198. inet_bind_bucket_create(struct kmem_cache *cachep, struct net *net,
  199. struct inet_bind_hashbucket *head,
  200. const unsigned short snum, int l3mdev);
  201. void inet_bind_bucket_destroy(struct kmem_cache *cachep,
  202. struct inet_bind_bucket *tb);
  203. bool inet_bind_bucket_match(const struct inet_bind_bucket *tb,
  204. const struct net *net, unsigned short port,
  205. int l3mdev);
  206. struct inet_bind2_bucket *
  207. inet_bind2_bucket_create(struct kmem_cache *cachep, struct net *net,
  208. struct inet_bind_hashbucket *head,
  209. struct inet_bind_bucket *tb,
  210. const struct sock *sk);
  211. void inet_bind2_bucket_destroy(struct kmem_cache *cachep,
  212. struct inet_bind2_bucket *tb);
  213. struct inet_bind2_bucket *
  214. inet_bind2_bucket_find(const struct inet_bind_hashbucket *head,
  215. const struct net *net,
  216. unsigned short port, int l3mdev,
  217. const struct sock *sk);
  218. bool inet_bind2_bucket_match_addr_any(const struct inet_bind2_bucket *tb,
  219. const struct net *net, unsigned short port,
  220. int l3mdev, const struct sock *sk);
  221. static inline u32 inet_bhashfn(const struct net *net, const __u16 lport,
  222. const u32 bhash_size)
  223. {
  224. return (lport + net_hash_mix(net)) & (bhash_size - 1);
  225. }
  226. static inline struct inet_bind_hashbucket *
  227. inet_bhashfn_portaddr(const struct inet_hashinfo *hinfo, const struct sock *sk,
  228. const struct net *net, unsigned short port)
  229. {
  230. u32 hash;
  231. #if IS_ENABLED(CONFIG_IPV6)
  232. if (sk->sk_family == AF_INET6)
  233. hash = ipv6_portaddr_hash(net, &sk->sk_v6_rcv_saddr, port);
  234. else
  235. #endif
  236. hash = ipv4_portaddr_hash(net, sk->sk_rcv_saddr, port);
  237. return &hinfo->bhash2[hash & (hinfo->bhash_size - 1)];
  238. }
  239. struct inet_bind_hashbucket *
  240. inet_bhash2_addr_any_hashbucket(const struct sock *sk, const struct net *net, int port);
  241. /* This should be called whenever a socket's sk_rcv_saddr (ipv4) or
  242. * sk_v6_rcv_saddr (ipv6) changes after it has been binded. The socket's
  243. * rcv_saddr field should already have been updated when this is called.
  244. */
  245. int inet_bhash2_update_saddr(struct sock *sk, void *saddr, int family);
  246. void inet_bhash2_reset_saddr(struct sock *sk);
  247. void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  248. struct inet_bind2_bucket *tb2, unsigned short port);
  249. /* Caller must disable local BH processing. */
  250. int __inet_inherit_port(const struct sock *sk, struct sock *child);
  251. void inet_put_port(struct sock *sk);
  252. void inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
  253. unsigned long numentries, int scale,
  254. unsigned long low_limit,
  255. unsigned long high_limit);
  256. int inet_hashinfo2_init_mod(struct inet_hashinfo *h);
  257. bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk);
  258. bool inet_ehash_nolisten(struct sock *sk, struct sock *osk,
  259. bool *found_dup_sk);
  260. int __inet_hash(struct sock *sk, struct sock *osk);
  261. int inet_hash(struct sock *sk);
  262. void inet_unhash(struct sock *sk);
  263. struct sock *__inet_lookup_listener(const struct net *net,
  264. struct inet_hashinfo *hashinfo,
  265. struct sk_buff *skb, int doff,
  266. const __be32 saddr, const __be16 sport,
  267. const __be32 daddr,
  268. const unsigned short hnum,
  269. const int dif, const int sdif);
  270. static inline struct sock *inet_lookup_listener(struct net *net,
  271. struct inet_hashinfo *hashinfo,
  272. struct sk_buff *skb, int doff,
  273. __be32 saddr, __be16 sport,
  274. __be32 daddr, __be16 dport, int dif, int sdif)
  275. {
  276. return __inet_lookup_listener(net, hashinfo, skb, doff, saddr, sport,
  277. daddr, ntohs(dport), dif, sdif);
  278. }
  279. /* Socket demux engine toys. */
  280. /* What happens here is ugly; there's a pair of adjacent fields in
  281. struct inet_sock; __be16 dport followed by __u16 num. We want to
  282. search by pair, so we combine the keys into a single 32bit value
  283. and compare with 32bit value read from &...->dport. Let's at least
  284. make sure that it's not mixed with anything else...
  285. On 64bit targets we combine comparisons with pair of adjacent __be32
  286. fields in the same way.
  287. */
  288. #ifdef __BIG_ENDIAN
  289. #define INET_COMBINED_PORTS(__sport, __dport) \
  290. ((__force __portpair)(((__force __u32)(__be16)(__sport) << 16) | (__u32)(__dport)))
  291. #else /* __LITTLE_ENDIAN */
  292. #define INET_COMBINED_PORTS(__sport, __dport) \
  293. ((__force __portpair)(((__u32)(__dport) << 16) | (__force __u32)(__be16)(__sport)))
  294. #endif
  295. #ifdef __BIG_ENDIAN
  296. #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
  297. const __addrpair __name = (__force __addrpair) ( \
  298. (((__force __u64)(__be32)(__saddr)) << 32) | \
  299. ((__force __u64)(__be32)(__daddr)))
  300. #else /* __LITTLE_ENDIAN */
  301. #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
  302. const __addrpair __name = (__force __addrpair) ( \
  303. (((__force __u64)(__be32)(__daddr)) << 32) | \
  304. ((__force __u64)(__be32)(__saddr)))
  305. #endif /* __BIG_ENDIAN */
  306. static inline bool inet_match(const struct net *net, const struct sock *sk,
  307. const __addrpair cookie, const __portpair ports,
  308. int dif, int sdif)
  309. {
  310. if (!net_eq(sock_net(sk), net) ||
  311. sk->sk_portpair != ports ||
  312. sk->sk_addrpair != cookie)
  313. return false;
  314. /* READ_ONCE() paired with WRITE_ONCE() in sock_bindtoindex_locked() */
  315. return inet_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif,
  316. sdif);
  317. }
  318. /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need
  319. * not check it for lookups anymore, thanks Alexey. -DaveM
  320. */
  321. struct sock *__inet_lookup_established(const struct net *net,
  322. struct inet_hashinfo *hashinfo,
  323. const __be32 saddr, const __be16 sport,
  324. const __be32 daddr, const u16 hnum,
  325. const int dif, const int sdif);
  326. typedef u32 (inet_ehashfn_t)(const struct net *net,
  327. const __be32 laddr, const __u16 lport,
  328. const __be32 faddr, const __be16 fport);
  329. inet_ehashfn_t inet_ehashfn;
  330. INDIRECT_CALLABLE_DECLARE(inet_ehashfn_t udp_ehashfn);
  331. struct sock *inet_lookup_reuseport(const struct net *net, struct sock *sk,
  332. struct sk_buff *skb, int doff,
  333. __be32 saddr, __be16 sport,
  334. __be32 daddr, unsigned short hnum,
  335. inet_ehashfn_t *ehashfn);
  336. struct sock *inet_lookup_run_sk_lookup(const struct net *net,
  337. int protocol,
  338. struct sk_buff *skb, int doff,
  339. __be32 saddr, __be16 sport,
  340. __be32 daddr, u16 hnum, const int dif,
  341. inet_ehashfn_t *ehashfn);
  342. static inline struct sock *
  343. inet_lookup_established(struct net *net, struct inet_hashinfo *hashinfo,
  344. const __be32 saddr, const __be16 sport,
  345. const __be32 daddr, const __be16 dport,
  346. const int dif)
  347. {
  348. return __inet_lookup_established(net, hashinfo, saddr, sport, daddr,
  349. ntohs(dport), dif, 0);
  350. }
  351. static inline struct sock *__inet_lookup(struct net *net,
  352. struct inet_hashinfo *hashinfo,
  353. struct sk_buff *skb, int doff,
  354. const __be32 saddr, const __be16 sport,
  355. const __be32 daddr, const __be16 dport,
  356. const int dif, const int sdif,
  357. bool *refcounted)
  358. {
  359. u16 hnum = ntohs(dport);
  360. struct sock *sk;
  361. sk = __inet_lookup_established(net, hashinfo, saddr, sport,
  362. daddr, hnum, dif, sdif);
  363. *refcounted = true;
  364. if (sk)
  365. return sk;
  366. *refcounted = false;
  367. return __inet_lookup_listener(net, hashinfo, skb, doff, saddr,
  368. sport, daddr, hnum, dif, sdif);
  369. }
  370. static inline struct sock *inet_lookup(struct net *net,
  371. struct inet_hashinfo *hashinfo,
  372. struct sk_buff *skb, int doff,
  373. const __be32 saddr, const __be16 sport,
  374. const __be32 daddr, const __be16 dport,
  375. const int dif)
  376. {
  377. struct sock *sk;
  378. bool refcounted;
  379. sk = __inet_lookup(net, hashinfo, skb, doff, saddr, sport, daddr,
  380. dport, dif, 0, &refcounted);
  381. if (sk && !refcounted && !refcount_inc_not_zero(&sk->sk_refcnt))
  382. sk = NULL;
  383. return sk;
  384. }
  385. static inline
  386. struct sock *inet_steal_sock(struct net *net, struct sk_buff *skb, int doff,
  387. const __be32 saddr, const __be16 sport,
  388. const __be32 daddr, const __be16 dport,
  389. bool *refcounted, inet_ehashfn_t *ehashfn)
  390. {
  391. struct sock *sk, *reuse_sk;
  392. bool prefetched;
  393. sk = skb_steal_sock(skb, refcounted, &prefetched);
  394. if (!sk)
  395. return NULL;
  396. if (!prefetched || !sk_fullsock(sk))
  397. return sk;
  398. if (sk->sk_protocol == IPPROTO_TCP) {
  399. if (sk->sk_state != TCP_LISTEN)
  400. return sk;
  401. } else if (sk->sk_protocol == IPPROTO_UDP) {
  402. if (sk->sk_state != TCP_CLOSE)
  403. return sk;
  404. } else {
  405. return sk;
  406. }
  407. reuse_sk = inet_lookup_reuseport(net, sk, skb, doff,
  408. saddr, sport, daddr, ntohs(dport),
  409. ehashfn);
  410. if (!reuse_sk)
  411. return sk;
  412. /* We've chosen a new reuseport sock which is never refcounted. This
  413. * implies that sk also isn't refcounted.
  414. */
  415. WARN_ON_ONCE(*refcounted);
  416. return reuse_sk;
  417. }
  418. static inline struct sock *__inet_lookup_skb(struct inet_hashinfo *hashinfo,
  419. struct sk_buff *skb,
  420. int doff,
  421. const __be16 sport,
  422. const __be16 dport,
  423. const int sdif,
  424. bool *refcounted)
  425. {
  426. struct net *net = skb_dst_dev_net_rcu(skb);
  427. const struct iphdr *iph = ip_hdr(skb);
  428. struct sock *sk;
  429. sk = inet_steal_sock(net, skb, doff, iph->saddr, sport, iph->daddr, dport,
  430. refcounted, inet_ehashfn);
  431. if (IS_ERR(sk))
  432. return NULL;
  433. if (sk)
  434. return sk;
  435. return __inet_lookup(net, hashinfo, skb,
  436. doff, iph->saddr, sport,
  437. iph->daddr, dport, inet_iif(skb), sdif,
  438. refcounted);
  439. }
  440. static inline void sk_daddr_set(struct sock *sk, __be32 addr)
  441. {
  442. sk->sk_daddr = addr; /* alias of inet_daddr */
  443. #if IS_ENABLED(CONFIG_IPV6)
  444. ipv6_addr_set_v4mapped(addr, &sk->sk_v6_daddr);
  445. #endif
  446. }
  447. static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr)
  448. {
  449. sk->sk_rcv_saddr = addr; /* alias of inet_rcv_saddr */
  450. #if IS_ENABLED(CONFIG_IPV6)
  451. ipv6_addr_set_v4mapped(addr, &sk->sk_v6_rcv_saddr);
  452. #endif
  453. }
  454. int __inet_hash_connect(struct inet_timewait_death_row *death_row,
  455. struct sock *sk, u64 port_offset,
  456. int (*check_established)(struct inet_timewait_death_row *,
  457. struct sock *, __u16,
  458. struct inet_timewait_sock **));
  459. int inet_hash_connect(struct inet_timewait_death_row *death_row,
  460. struct sock *sk);
  461. #endif /* _INET_HASHTABLES_H */