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