inet_hashtables.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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. * Generic INET transport hashtables
  8. *
  9. * Authors: Lotsa people, from code originally in tcp
  10. */
  11. #include <linux/module.h>
  12. #include <linux/random.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/wait.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/memblock.h>
  18. #include <net/addrconf.h>
  19. #include <net/inet_connection_sock.h>
  20. #include <net/inet_hashtables.h>
  21. #if IS_ENABLED(CONFIG_IPV6)
  22. #include <net/inet6_hashtables.h>
  23. #endif
  24. #include <net/hotdata.h>
  25. #include <net/ip.h>
  26. #include <net/rps.h>
  27. #include <net/secure_seq.h>
  28. #include <net/sock_reuseport.h>
  29. #include <net/tcp.h>
  30. u32 inet_ehashfn(const struct net *net, const __be32 laddr,
  31. const __u16 lport, const __be32 faddr,
  32. const __be16 fport)
  33. {
  34. net_get_random_once(&inet_ehash_secret, sizeof(inet_ehash_secret));
  35. return lport + __inet_ehashfn(laddr, 0, faddr, fport,
  36. inet_ehash_secret + net_hash_mix(net));
  37. }
  38. EXPORT_SYMBOL_GPL(inet_ehashfn);
  39. /* This function handles inet_sock, but also timewait and request sockets
  40. * for IPv4/IPv6.
  41. */
  42. static u32 sk_ehashfn(const struct sock *sk)
  43. {
  44. #if IS_ENABLED(CONFIG_IPV6)
  45. if (sk->sk_family == AF_INET6 &&
  46. !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
  47. return inet6_ehashfn(sock_net(sk),
  48. &sk->sk_v6_rcv_saddr, sk->sk_num,
  49. &sk->sk_v6_daddr, sk->sk_dport);
  50. #endif
  51. return inet_ehashfn(sock_net(sk),
  52. sk->sk_rcv_saddr, sk->sk_num,
  53. sk->sk_daddr, sk->sk_dport);
  54. }
  55. static bool sk_is_connect_bind(const struct sock *sk)
  56. {
  57. if (sk->sk_state == TCP_TIME_WAIT)
  58. return inet_twsk(sk)->tw_connect_bind;
  59. else
  60. return sk->sk_userlocks & SOCK_CONNECT_BIND;
  61. }
  62. /*
  63. * Allocate and initialize a new local port bind bucket.
  64. * The bindhash mutex for snum's hash chain must be held here.
  65. */
  66. struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
  67. struct net *net,
  68. struct inet_bind_hashbucket *head,
  69. const unsigned short snum,
  70. int l3mdev)
  71. {
  72. struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
  73. if (tb) {
  74. write_pnet(&tb->ib_net, net);
  75. tb->l3mdev = l3mdev;
  76. tb->port = snum;
  77. tb->fastreuse = 0;
  78. tb->fastreuseport = 0;
  79. INIT_HLIST_HEAD(&tb->bhash2);
  80. hlist_add_head_rcu(&tb->node, &head->chain);
  81. }
  82. return tb;
  83. }
  84. /*
  85. * Caller must hold hashbucket lock for this tb with local BH disabled
  86. */
  87. void inet_bind_bucket_destroy(struct inet_bind_bucket *tb)
  88. {
  89. const struct inet_bind2_bucket *tb2;
  90. if (hlist_empty(&tb->bhash2)) {
  91. hlist_del_rcu(&tb->node);
  92. kfree_rcu(tb, rcu);
  93. return;
  94. }
  95. if (tb->fastreuse == -1 && tb->fastreuseport == -1)
  96. return;
  97. hlist_for_each_entry(tb2, &tb->bhash2, bhash_node) {
  98. if (tb2->fastreuse != -1 || tb2->fastreuseport != -1)
  99. return;
  100. }
  101. tb->fastreuse = -1;
  102. tb->fastreuseport = -1;
  103. }
  104. bool inet_bind_bucket_match(const struct inet_bind_bucket *tb, const struct net *net,
  105. unsigned short port, int l3mdev)
  106. {
  107. return net_eq(ib_net(tb), net) && tb->port == port &&
  108. tb->l3mdev == l3mdev;
  109. }
  110. static void inet_bind2_bucket_init(struct inet_bind2_bucket *tb2,
  111. struct net *net,
  112. struct inet_bind_hashbucket *head,
  113. struct inet_bind_bucket *tb,
  114. const struct sock *sk)
  115. {
  116. write_pnet(&tb2->ib_net, net);
  117. tb2->l3mdev = tb->l3mdev;
  118. tb2->port = tb->port;
  119. #if IS_ENABLED(CONFIG_IPV6)
  120. BUILD_BUG_ON(USHRT_MAX < (IPV6_ADDR_ANY | IPV6_ADDR_MAPPED));
  121. if (sk->sk_family == AF_INET6) {
  122. tb2->addr_type = ipv6_addr_type(&sk->sk_v6_rcv_saddr);
  123. tb2->v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  124. } else {
  125. tb2->addr_type = IPV6_ADDR_MAPPED;
  126. ipv6_addr_set_v4mapped(sk->sk_rcv_saddr, &tb2->v6_rcv_saddr);
  127. }
  128. #else
  129. tb2->rcv_saddr = sk->sk_rcv_saddr;
  130. #endif
  131. tb2->fastreuse = 0;
  132. tb2->fastreuseport = 0;
  133. INIT_HLIST_HEAD(&tb2->owners);
  134. hlist_add_head(&tb2->node, &head->chain);
  135. hlist_add_head(&tb2->bhash_node, &tb->bhash2);
  136. }
  137. struct inet_bind2_bucket *inet_bind2_bucket_create(struct kmem_cache *cachep,
  138. struct net *net,
  139. struct inet_bind_hashbucket *head,
  140. struct inet_bind_bucket *tb,
  141. const struct sock *sk)
  142. {
  143. struct inet_bind2_bucket *tb2 = kmem_cache_alloc(cachep, GFP_ATOMIC);
  144. if (tb2)
  145. inet_bind2_bucket_init(tb2, net, head, tb, sk);
  146. return tb2;
  147. }
  148. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  149. void inet_bind2_bucket_destroy(struct kmem_cache *cachep, struct inet_bind2_bucket *tb)
  150. {
  151. const struct sock *sk;
  152. if (hlist_empty(&tb->owners)) {
  153. __hlist_del(&tb->node);
  154. __hlist_del(&tb->bhash_node);
  155. kmem_cache_free(cachep, tb);
  156. return;
  157. }
  158. if (tb->fastreuse == -1 && tb->fastreuseport == -1)
  159. return;
  160. sk_for_each_bound(sk, &tb->owners) {
  161. if (!sk_is_connect_bind(sk))
  162. return;
  163. }
  164. tb->fastreuse = -1;
  165. tb->fastreuseport = -1;
  166. }
  167. static bool inet_bind2_bucket_addr_match(const struct inet_bind2_bucket *tb2,
  168. const struct sock *sk)
  169. {
  170. #if IS_ENABLED(CONFIG_IPV6)
  171. if (sk->sk_family == AF_INET6)
  172. return ipv6_addr_equal(&tb2->v6_rcv_saddr, &sk->sk_v6_rcv_saddr);
  173. if (tb2->addr_type != IPV6_ADDR_MAPPED)
  174. return false;
  175. #endif
  176. return tb2->rcv_saddr == sk->sk_rcv_saddr;
  177. }
  178. void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  179. struct inet_bind2_bucket *tb2, unsigned short port)
  180. {
  181. WRITE_ONCE(inet_sk(sk)->inet_num, port);
  182. inet_csk(sk)->icsk_bind_hash = tb;
  183. inet_csk(sk)->icsk_bind2_hash = tb2;
  184. sk_add_bind_node(sk, &tb2->owners);
  185. }
  186. /*
  187. * Get rid of any references to a local port held by the given sock.
  188. */
  189. static void __inet_put_port(struct sock *sk)
  190. {
  191. struct inet_hashinfo *hashinfo = tcp_get_hashinfo(sk);
  192. struct inet_bind_hashbucket *head, *head2;
  193. struct net *net = sock_net(sk);
  194. struct inet_bind_bucket *tb;
  195. int bhash;
  196. bhash = inet_bhashfn(net, inet_sk(sk)->inet_num, hashinfo->bhash_size);
  197. head = &hashinfo->bhash[bhash];
  198. head2 = inet_bhashfn_portaddr(hashinfo, sk, net, inet_sk(sk)->inet_num);
  199. spin_lock(&head->lock);
  200. tb = inet_csk(sk)->icsk_bind_hash;
  201. inet_csk(sk)->icsk_bind_hash = NULL;
  202. WRITE_ONCE(inet_sk(sk)->inet_num, 0);
  203. sk->sk_userlocks &= ~SOCK_CONNECT_BIND;
  204. spin_lock(&head2->lock);
  205. if (inet_csk(sk)->icsk_bind2_hash) {
  206. struct inet_bind2_bucket *tb2 = inet_csk(sk)->icsk_bind2_hash;
  207. __sk_del_bind_node(sk);
  208. inet_csk(sk)->icsk_bind2_hash = NULL;
  209. inet_bind2_bucket_destroy(hashinfo->bind2_bucket_cachep, tb2);
  210. }
  211. spin_unlock(&head2->lock);
  212. inet_bind_bucket_destroy(tb);
  213. spin_unlock(&head->lock);
  214. }
  215. void inet_put_port(struct sock *sk)
  216. {
  217. local_bh_disable();
  218. __inet_put_port(sk);
  219. local_bh_enable();
  220. }
  221. EXPORT_SYMBOL(inet_put_port);
  222. int __inet_inherit_port(const struct sock *sk, struct sock *child)
  223. {
  224. struct inet_hashinfo *table = tcp_get_hashinfo(sk);
  225. unsigned short port = inet_sk(child)->inet_num;
  226. struct inet_bind_hashbucket *head, *head2;
  227. bool created_inet_bind_bucket = false;
  228. struct net *net = sock_net(sk);
  229. bool update_fastreuse = false;
  230. struct inet_bind2_bucket *tb2;
  231. struct inet_bind_bucket *tb;
  232. int bhash, l3mdev;
  233. bhash = inet_bhashfn(net, port, table->bhash_size);
  234. head = &table->bhash[bhash];
  235. head2 = inet_bhashfn_portaddr(table, child, net, port);
  236. spin_lock(&head->lock);
  237. spin_lock(&head2->lock);
  238. tb = inet_csk(sk)->icsk_bind_hash;
  239. tb2 = inet_csk(sk)->icsk_bind2_hash;
  240. if (unlikely(!tb || !tb2)) {
  241. spin_unlock(&head2->lock);
  242. spin_unlock(&head->lock);
  243. return -ENOENT;
  244. }
  245. if (tb->port != port) {
  246. l3mdev = inet_sk_bound_l3mdev(sk);
  247. /* NOTE: using tproxy and redirecting skbs to a proxy
  248. * on a different listener port breaks the assumption
  249. * that the listener socket's icsk_bind_hash is the same
  250. * as that of the child socket. We have to look up or
  251. * create a new bind bucket for the child here. */
  252. inet_bind_bucket_for_each(tb, &head->chain) {
  253. if (inet_bind_bucket_match(tb, net, port, l3mdev))
  254. break;
  255. }
  256. if (!tb) {
  257. tb = inet_bind_bucket_create(table->bind_bucket_cachep,
  258. net, head, port, l3mdev);
  259. if (!tb) {
  260. spin_unlock(&head2->lock);
  261. spin_unlock(&head->lock);
  262. return -ENOMEM;
  263. }
  264. created_inet_bind_bucket = true;
  265. }
  266. update_fastreuse = true;
  267. goto bhash2_find;
  268. } else if (!inet_bind2_bucket_addr_match(tb2, child)) {
  269. l3mdev = inet_sk_bound_l3mdev(sk);
  270. bhash2_find:
  271. tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, child);
  272. if (!tb2) {
  273. tb2 = inet_bind2_bucket_create(table->bind2_bucket_cachep,
  274. net, head2, tb, child);
  275. if (!tb2)
  276. goto error;
  277. }
  278. }
  279. if (update_fastreuse)
  280. inet_csk_update_fastreuse(child, tb, tb2);
  281. inet_bind_hash(child, tb, tb2, port);
  282. spin_unlock(&head2->lock);
  283. spin_unlock(&head->lock);
  284. return 0;
  285. error:
  286. if (created_inet_bind_bucket)
  287. inet_bind_bucket_destroy(tb);
  288. spin_unlock(&head2->lock);
  289. spin_unlock(&head->lock);
  290. return -ENOMEM;
  291. }
  292. EXPORT_SYMBOL_GPL(__inet_inherit_port);
  293. static struct inet_listen_hashbucket *
  294. inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
  295. {
  296. u32 hash;
  297. #if IS_ENABLED(CONFIG_IPV6)
  298. if (sk->sk_family == AF_INET6)
  299. hash = ipv6_portaddr_hash(sock_net(sk),
  300. &sk->sk_v6_rcv_saddr,
  301. inet_sk(sk)->inet_num);
  302. else
  303. #endif
  304. hash = ipv4_portaddr_hash(sock_net(sk),
  305. inet_sk(sk)->inet_rcv_saddr,
  306. inet_sk(sk)->inet_num);
  307. return inet_lhash2_bucket(h, hash);
  308. }
  309. static inline int compute_score(struct sock *sk, const struct net *net,
  310. const unsigned short hnum, const __be32 daddr,
  311. const int dif, const int sdif)
  312. {
  313. int score = -1;
  314. if (net_eq(sock_net(sk), net) && READ_ONCE(sk->sk_num) == hnum &&
  315. !ipv6_only_sock(sk)) {
  316. if (sk->sk_rcv_saddr != daddr)
  317. return -1;
  318. if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
  319. return -1;
  320. score = sk->sk_bound_dev_if ? 2 : 1;
  321. if (sk->sk_family == PF_INET)
  322. score++;
  323. if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
  324. score++;
  325. }
  326. return score;
  327. }
  328. /**
  329. * inet_lookup_reuseport() - execute reuseport logic on AF_INET socket if necessary.
  330. * @net: network namespace.
  331. * @sk: AF_INET socket, must be in TCP_LISTEN state for TCP or TCP_CLOSE for UDP.
  332. * @skb: context for a potential SK_REUSEPORT program.
  333. * @doff: header offset.
  334. * @saddr: source address.
  335. * @sport: source port.
  336. * @daddr: destination address.
  337. * @hnum: destination port in host byte order.
  338. * @ehashfn: hash function used to generate the fallback hash.
  339. *
  340. * Return: NULL if sk doesn't have SO_REUSEPORT set, otherwise a pointer to
  341. * the selected sock or an error.
  342. */
  343. struct sock *inet_lookup_reuseport(const struct net *net, struct sock *sk,
  344. struct sk_buff *skb, int doff,
  345. __be32 saddr, __be16 sport,
  346. __be32 daddr, unsigned short hnum,
  347. inet_ehashfn_t *ehashfn)
  348. {
  349. struct sock *reuse_sk = NULL;
  350. u32 phash;
  351. if (sk->sk_reuseport) {
  352. phash = INDIRECT_CALL_2(ehashfn, udp_ehashfn, inet_ehashfn,
  353. net, daddr, hnum, saddr, sport);
  354. reuse_sk = reuseport_select_sock(sk, phash, skb, doff);
  355. }
  356. return reuse_sk;
  357. }
  358. EXPORT_SYMBOL_GPL(inet_lookup_reuseport);
  359. /*
  360. * Here are some nice properties to exploit here. The BSD API
  361. * does not allow a listening sock to specify the remote port nor the
  362. * remote address for the connection. So always assume those are both
  363. * wildcarded during the search since they can never be otherwise.
  364. */
  365. /* called with rcu_read_lock() : No refcount taken on the socket */
  366. static struct sock *inet_lhash2_lookup(const struct net *net,
  367. struct inet_listen_hashbucket *ilb2,
  368. struct sk_buff *skb, int doff,
  369. const __be32 saddr, __be16 sport,
  370. const __be32 daddr, const unsigned short hnum,
  371. const int dif, const int sdif)
  372. {
  373. struct sock *sk, *result = NULL;
  374. struct hlist_nulls_node *node;
  375. int score, hiscore = 0;
  376. sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) {
  377. score = compute_score(sk, net, hnum, daddr, dif, sdif);
  378. if (score > hiscore) {
  379. result = inet_lookup_reuseport(net, sk, skb, doff,
  380. saddr, sport, daddr, hnum, inet_ehashfn);
  381. if (result)
  382. return result;
  383. result = sk;
  384. hiscore = score;
  385. }
  386. }
  387. return result;
  388. }
  389. struct sock *inet_lookup_run_sk_lookup(const struct net *net,
  390. int protocol,
  391. struct sk_buff *skb, int doff,
  392. __be32 saddr, __be16 sport,
  393. __be32 daddr, u16 hnum, const int dif,
  394. inet_ehashfn_t *ehashfn)
  395. {
  396. struct sock *sk, *reuse_sk;
  397. bool no_reuseport;
  398. no_reuseport = bpf_sk_lookup_run_v4(net, protocol, saddr, sport,
  399. daddr, hnum, dif, &sk);
  400. if (no_reuseport || IS_ERR_OR_NULL(sk))
  401. return sk;
  402. reuse_sk = inet_lookup_reuseport(net, sk, skb, doff, saddr, sport, daddr, hnum,
  403. ehashfn);
  404. if (reuse_sk)
  405. sk = reuse_sk;
  406. return sk;
  407. }
  408. struct sock *__inet_lookup_listener(const struct net *net,
  409. struct sk_buff *skb, int doff,
  410. const __be32 saddr, __be16 sport,
  411. const __be32 daddr, const unsigned short hnum,
  412. const int dif, const int sdif)
  413. {
  414. struct inet_listen_hashbucket *ilb2;
  415. struct inet_hashinfo *hashinfo;
  416. struct sock *result = NULL;
  417. unsigned int hash2;
  418. /* Lookup redirect from BPF */
  419. if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
  420. result = inet_lookup_run_sk_lookup(net, IPPROTO_TCP, skb, doff,
  421. saddr, sport, daddr, hnum, dif,
  422. inet_ehashfn);
  423. if (result)
  424. goto done;
  425. }
  426. hashinfo = net->ipv4.tcp_death_row.hashinfo;
  427. hash2 = ipv4_portaddr_hash(net, daddr, hnum);
  428. ilb2 = inet_lhash2_bucket(hashinfo, hash2);
  429. result = inet_lhash2_lookup(net, ilb2, skb, doff,
  430. saddr, sport, daddr, hnum,
  431. dif, sdif);
  432. if (result)
  433. goto done;
  434. /* Lookup lhash2 with INADDR_ANY */
  435. hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
  436. ilb2 = inet_lhash2_bucket(hashinfo, hash2);
  437. result = inet_lhash2_lookup(net, ilb2, skb, doff,
  438. saddr, sport, htonl(INADDR_ANY), hnum,
  439. dif, sdif);
  440. done:
  441. if (IS_ERR(result))
  442. return NULL;
  443. return result;
  444. }
  445. EXPORT_SYMBOL_GPL(__inet_lookup_listener);
  446. /* All sockets share common refcount, but have different destructors */
  447. void sock_gen_put(struct sock *sk)
  448. {
  449. if (!refcount_dec_and_test(&sk->sk_refcnt))
  450. return;
  451. if (sk->sk_state == TCP_TIME_WAIT)
  452. inet_twsk_free(inet_twsk(sk));
  453. else if (sk->sk_state == TCP_NEW_SYN_RECV)
  454. reqsk_free(inet_reqsk(sk));
  455. else
  456. sk_free(sk);
  457. }
  458. EXPORT_SYMBOL_GPL(sock_gen_put);
  459. void sock_edemux(struct sk_buff *skb)
  460. {
  461. sock_gen_put(skb->sk);
  462. }
  463. EXPORT_SYMBOL(sock_edemux);
  464. struct sock *__inet_lookup_established(const struct net *net,
  465. const __be32 saddr, const __be16 sport,
  466. const __be32 daddr, const u16 hnum,
  467. const int dif, const int sdif)
  468. {
  469. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  470. INET_ADDR_COOKIE(acookie, saddr, daddr);
  471. const struct hlist_nulls_node *node;
  472. struct inet_ehash_bucket *head;
  473. struct inet_hashinfo *hashinfo;
  474. unsigned int hash, slot;
  475. struct sock *sk;
  476. hashinfo = net->ipv4.tcp_death_row.hashinfo;
  477. hash = inet_ehashfn(net, daddr, hnum, saddr, sport);
  478. slot = hash & hashinfo->ehash_mask;
  479. head = &hashinfo->ehash[slot];
  480. begin:
  481. sk_nulls_for_each_rcu(sk, node, &head->chain) {
  482. if (sk->sk_hash != hash)
  483. continue;
  484. if (likely(inet_match(net, sk, acookie, ports, dif, sdif))) {
  485. if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
  486. goto out;
  487. if (unlikely(!inet_match(net, sk, acookie,
  488. ports, dif, sdif))) {
  489. sock_gen_put(sk);
  490. goto begin;
  491. }
  492. goto found;
  493. }
  494. }
  495. /*
  496. * if the nulls value we got at the end of this lookup is
  497. * not the expected one, we must restart lookup.
  498. * We probably met an item that was moved to another chain.
  499. */
  500. if (get_nulls_value(node) != slot)
  501. goto begin;
  502. out:
  503. sk = NULL;
  504. found:
  505. return sk;
  506. }
  507. EXPORT_SYMBOL_GPL(__inet_lookup_established);
  508. /* called with local bh disabled */
  509. static int __inet_check_established(struct inet_timewait_death_row *death_row,
  510. struct sock *sk, __u16 lport,
  511. struct inet_timewait_sock **twp,
  512. bool rcu_lookup,
  513. u32 hash)
  514. {
  515. struct inet_hashinfo *hinfo = death_row->hashinfo;
  516. struct inet_sock *inet = inet_sk(sk);
  517. __be32 daddr = inet->inet_rcv_saddr;
  518. __be32 saddr = inet->inet_daddr;
  519. int dif = sk->sk_bound_dev_if;
  520. struct net *net = sock_net(sk);
  521. int sdif = l3mdev_master_ifindex_by_index(net, dif);
  522. INET_ADDR_COOKIE(acookie, saddr, daddr);
  523. const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
  524. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  525. struct inet_timewait_sock *tw = NULL;
  526. const struct hlist_nulls_node *node;
  527. struct sock *sk2;
  528. spinlock_t *lock;
  529. if (rcu_lookup) {
  530. sk_nulls_for_each(sk2, node, &head->chain) {
  531. if (sk2->sk_hash != hash ||
  532. !inet_match(net, sk2, acookie, ports, dif, sdif))
  533. continue;
  534. if (sk2->sk_state == TCP_TIME_WAIT)
  535. break;
  536. return -EADDRNOTAVAIL;
  537. }
  538. return 0;
  539. }
  540. lock = inet_ehash_lockp(hinfo, hash);
  541. spin_lock(lock);
  542. sk_nulls_for_each(sk2, node, &head->chain) {
  543. if (sk2->sk_hash != hash)
  544. continue;
  545. if (likely(inet_match(net, sk2, acookie, ports, dif, sdif))) {
  546. if (sk2->sk_state == TCP_TIME_WAIT) {
  547. tw = inet_twsk(sk2);
  548. if (tcp_twsk_unique(sk, sk2, twp))
  549. break;
  550. }
  551. goto not_unique;
  552. }
  553. }
  554. /* Must record num and sport now. Otherwise we will see
  555. * in hash table socket with a funny identity.
  556. */
  557. inet->inet_num = lport;
  558. inet->inet_sport = htons(lport);
  559. sk->sk_hash = hash;
  560. WARN_ON(!sk_unhashed(sk));
  561. __sk_nulls_add_node_rcu(sk, &head->chain);
  562. if (tw) {
  563. sk_nulls_del_node_init_rcu((struct sock *)tw);
  564. __NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
  565. }
  566. spin_unlock(lock);
  567. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  568. if (twp) {
  569. *twp = tw;
  570. } else if (tw) {
  571. /* Silly. Should hash-dance instead... */
  572. inet_twsk_deschedule_put(tw);
  573. }
  574. return 0;
  575. not_unique:
  576. spin_unlock(lock);
  577. return -EADDRNOTAVAIL;
  578. }
  579. static u64 inet_sk_port_offset(const struct sock *sk)
  580. {
  581. const struct inet_sock *inet = inet_sk(sk);
  582. return secure_ipv4_port_ephemeral(inet->inet_rcv_saddr,
  583. inet->inet_daddr,
  584. inet->inet_dport);
  585. }
  586. /* Searches for an exsiting socket in the ehash bucket list.
  587. * Returns true if found, false otherwise.
  588. */
  589. static bool inet_ehash_lookup_by_sk(struct sock *sk,
  590. struct hlist_nulls_head *list)
  591. {
  592. const __portpair ports = INET_COMBINED_PORTS(sk->sk_dport, sk->sk_num);
  593. const int sdif = sk->sk_bound_dev_if;
  594. const int dif = sk->sk_bound_dev_if;
  595. const struct hlist_nulls_node *node;
  596. struct net *net = sock_net(sk);
  597. struct sock *esk;
  598. INET_ADDR_COOKIE(acookie, sk->sk_daddr, sk->sk_rcv_saddr);
  599. sk_nulls_for_each_rcu(esk, node, list) {
  600. if (esk->sk_hash != sk->sk_hash)
  601. continue;
  602. if (sk->sk_family == AF_INET) {
  603. if (unlikely(inet_match(net, esk, acookie,
  604. ports, dif, sdif))) {
  605. return true;
  606. }
  607. }
  608. #if IS_ENABLED(CONFIG_IPV6)
  609. else if (sk->sk_family == AF_INET6) {
  610. if (unlikely(inet6_match(net, esk,
  611. &sk->sk_v6_daddr,
  612. &sk->sk_v6_rcv_saddr,
  613. ports, dif, sdif))) {
  614. return true;
  615. }
  616. }
  617. #endif
  618. }
  619. return false;
  620. }
  621. /* Insert a socket into ehash, and eventually remove another one
  622. * (The another one can be a SYN_RECV or TIMEWAIT)
  623. * If an existing socket already exists, socket sk is not inserted,
  624. * and sets found_dup_sk parameter to true.
  625. */
  626. bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk)
  627. {
  628. struct inet_hashinfo *hashinfo = tcp_get_hashinfo(sk);
  629. struct inet_ehash_bucket *head;
  630. struct hlist_nulls_head *list;
  631. spinlock_t *lock;
  632. bool ret = true;
  633. WARN_ON_ONCE(!sk_unhashed(sk));
  634. sk->sk_hash = sk_ehashfn(sk);
  635. head = inet_ehash_bucket(hashinfo, sk->sk_hash);
  636. list = &head->chain;
  637. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  638. spin_lock(lock);
  639. if (osk) {
  640. WARN_ON_ONCE(sk->sk_hash != osk->sk_hash);
  641. ret = sk_nulls_replace_node_init_rcu(osk, sk);
  642. goto unlock;
  643. }
  644. if (found_dup_sk) {
  645. *found_dup_sk = inet_ehash_lookup_by_sk(sk, list);
  646. if (*found_dup_sk)
  647. ret = false;
  648. }
  649. if (ret)
  650. __sk_nulls_add_node_rcu(sk, list);
  651. unlock:
  652. spin_unlock(lock);
  653. return ret;
  654. }
  655. bool inet_ehash_nolisten(struct sock *sk, struct sock *osk, bool *found_dup_sk)
  656. {
  657. bool ok = inet_ehash_insert(sk, osk, found_dup_sk);
  658. if (ok) {
  659. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  660. } else {
  661. tcp_orphan_count_inc();
  662. inet_sk_set_state(sk, TCP_CLOSE);
  663. sock_set_flag(sk, SOCK_DEAD);
  664. inet_csk_destroy_sock(sk);
  665. }
  666. return ok;
  667. }
  668. EXPORT_IPV6_MOD(inet_ehash_nolisten);
  669. static int inet_reuseport_add_sock(struct sock *sk,
  670. struct inet_listen_hashbucket *ilb)
  671. {
  672. struct inet_bind_bucket *tb = inet_csk(sk)->icsk_bind_hash;
  673. const struct hlist_nulls_node *node;
  674. kuid_t uid = sk_uid(sk);
  675. struct sock *sk2;
  676. sk_nulls_for_each_rcu(sk2, node, &ilb->nulls_head) {
  677. if (sk2 != sk &&
  678. sk2->sk_family == sk->sk_family &&
  679. ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
  680. sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
  681. inet_csk(sk2)->icsk_bind_hash == tb &&
  682. sk2->sk_reuseport && uid_eq(uid, sk_uid(sk2)) &&
  683. inet_rcv_saddr_equal(sk, sk2, false))
  684. return reuseport_add_sock(sk, sk2,
  685. inet_rcv_saddr_any(sk));
  686. }
  687. return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
  688. }
  689. int inet_hash(struct sock *sk)
  690. {
  691. struct inet_hashinfo *hashinfo = tcp_get_hashinfo(sk);
  692. struct inet_listen_hashbucket *ilb2;
  693. int err = 0;
  694. if (sk->sk_state == TCP_CLOSE)
  695. return 0;
  696. if (sk->sk_state != TCP_LISTEN) {
  697. local_bh_disable();
  698. inet_ehash_nolisten(sk, NULL, NULL);
  699. local_bh_enable();
  700. return 0;
  701. }
  702. WARN_ON(!sk_unhashed(sk));
  703. ilb2 = inet_lhash2_bucket_sk(hashinfo, sk);
  704. spin_lock(&ilb2->lock);
  705. if (sk->sk_reuseport) {
  706. err = inet_reuseport_add_sock(sk, ilb2);
  707. if (err)
  708. goto unlock;
  709. }
  710. sock_set_flag(sk, SOCK_RCU_FREE);
  711. if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
  712. sk->sk_family == AF_INET6)
  713. __sk_nulls_add_node_tail_rcu(sk, &ilb2->nulls_head);
  714. else
  715. __sk_nulls_add_node_rcu(sk, &ilb2->nulls_head);
  716. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  717. unlock:
  718. spin_unlock(&ilb2->lock);
  719. return err;
  720. }
  721. EXPORT_IPV6_MOD(inet_hash);
  722. void inet_unhash(struct sock *sk)
  723. {
  724. struct inet_hashinfo *hashinfo = tcp_get_hashinfo(sk);
  725. if (sk_unhashed(sk))
  726. return;
  727. sock_rps_delete_flow(sk);
  728. if (sk->sk_state == TCP_LISTEN) {
  729. struct inet_listen_hashbucket *ilb2;
  730. ilb2 = inet_lhash2_bucket_sk(hashinfo, sk);
  731. /* Don't disable bottom halves while acquiring the lock to
  732. * avoid circular locking dependency on PREEMPT_RT.
  733. */
  734. spin_lock(&ilb2->lock);
  735. if (rcu_access_pointer(sk->sk_reuseport_cb))
  736. reuseport_stop_listen_sock(sk);
  737. __sk_nulls_del_node_init_rcu(sk);
  738. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  739. spin_unlock(&ilb2->lock);
  740. } else {
  741. spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  742. spin_lock_bh(lock);
  743. __sk_nulls_del_node_init_rcu(sk);
  744. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  745. spin_unlock_bh(lock);
  746. }
  747. }
  748. EXPORT_IPV6_MOD(inet_unhash);
  749. static bool inet_bind2_bucket_match(const struct inet_bind2_bucket *tb,
  750. const struct net *net, unsigned short port,
  751. int l3mdev, const struct sock *sk)
  752. {
  753. if (!net_eq(ib2_net(tb), net) || tb->port != port ||
  754. tb->l3mdev != l3mdev)
  755. return false;
  756. return inet_bind2_bucket_addr_match(tb, sk);
  757. }
  758. bool inet_bind2_bucket_match_addr_any(const struct inet_bind2_bucket *tb, const struct net *net,
  759. unsigned short port, int l3mdev, const struct sock *sk)
  760. {
  761. if (!net_eq(ib2_net(tb), net) || tb->port != port ||
  762. tb->l3mdev != l3mdev)
  763. return false;
  764. #if IS_ENABLED(CONFIG_IPV6)
  765. if (tb->addr_type == IPV6_ADDR_ANY)
  766. return true;
  767. if (tb->addr_type != IPV6_ADDR_MAPPED)
  768. return false;
  769. if (sk->sk_family == AF_INET6 &&
  770. !ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
  771. return false;
  772. #endif
  773. return tb->rcv_saddr == 0;
  774. }
  775. /* The socket's bhash2 hashbucket spinlock must be held when this is called */
  776. struct inet_bind2_bucket *
  777. inet_bind2_bucket_find(const struct inet_bind_hashbucket *head, const struct net *net,
  778. unsigned short port, int l3mdev, const struct sock *sk)
  779. {
  780. struct inet_bind2_bucket *bhash2 = NULL;
  781. inet_bind_bucket_for_each(bhash2, &head->chain)
  782. if (inet_bind2_bucket_match(bhash2, net, port, l3mdev, sk))
  783. break;
  784. return bhash2;
  785. }
  786. struct inet_bind_hashbucket *
  787. inet_bhash2_addr_any_hashbucket(const struct sock *sk, const struct net *net, int port)
  788. {
  789. struct inet_hashinfo *hinfo = tcp_get_hashinfo(sk);
  790. u32 hash;
  791. #if IS_ENABLED(CONFIG_IPV6)
  792. if (sk->sk_family == AF_INET6)
  793. hash = ipv6_portaddr_hash(net, &in6addr_any, port);
  794. else
  795. #endif
  796. hash = ipv4_portaddr_hash(net, 0, port);
  797. return &hinfo->bhash2[hash & (hinfo->bhash_size - 1)];
  798. }
  799. static void inet_update_saddr(struct sock *sk, void *saddr, int family)
  800. {
  801. if (family == AF_INET) {
  802. inet_sk(sk)->inet_saddr = *(__be32 *)saddr;
  803. sk_rcv_saddr_set(sk, inet_sk(sk)->inet_saddr);
  804. }
  805. #if IS_ENABLED(CONFIG_IPV6)
  806. else {
  807. sk->sk_v6_rcv_saddr = *(struct in6_addr *)saddr;
  808. }
  809. #endif
  810. }
  811. static int __inet_bhash2_update_saddr(struct sock *sk, void *saddr, int family, bool reset)
  812. {
  813. struct inet_hashinfo *hinfo = tcp_get_hashinfo(sk);
  814. struct inet_bind_hashbucket *head, *head2;
  815. struct inet_bind2_bucket *tb2, *new_tb2;
  816. int l3mdev = inet_sk_bound_l3mdev(sk);
  817. int port = inet_sk(sk)->inet_num;
  818. struct net *net = sock_net(sk);
  819. int bhash;
  820. if (!inet_csk(sk)->icsk_bind2_hash) {
  821. /* Not bind()ed before. */
  822. if (reset)
  823. inet_reset_saddr(sk);
  824. else
  825. inet_update_saddr(sk, saddr, family);
  826. return 0;
  827. }
  828. /* Allocate a bind2 bucket ahead of time to avoid permanently putting
  829. * the bhash2 table in an inconsistent state if a new tb2 bucket
  830. * allocation fails.
  831. */
  832. new_tb2 = kmem_cache_alloc(hinfo->bind2_bucket_cachep, GFP_ATOMIC);
  833. if (!new_tb2) {
  834. if (reset) {
  835. /* The (INADDR_ANY, port) bucket might have already
  836. * been freed, then we cannot fixup icsk_bind2_hash,
  837. * so we give up and unlink sk from bhash/bhash2 not
  838. * to leave inconsistency in bhash2.
  839. */
  840. inet_put_port(sk);
  841. inet_reset_saddr(sk);
  842. }
  843. return -ENOMEM;
  844. }
  845. bhash = inet_bhashfn(net, port, hinfo->bhash_size);
  846. head = &hinfo->bhash[bhash];
  847. head2 = inet_bhashfn_portaddr(hinfo, sk, net, port);
  848. /* If we change saddr locklessly, another thread
  849. * iterating over bhash might see corrupted address.
  850. */
  851. spin_lock_bh(&head->lock);
  852. spin_lock(&head2->lock);
  853. __sk_del_bind_node(sk);
  854. inet_bind2_bucket_destroy(hinfo->bind2_bucket_cachep, inet_csk(sk)->icsk_bind2_hash);
  855. spin_unlock(&head2->lock);
  856. if (reset)
  857. inet_reset_saddr(sk);
  858. else
  859. inet_update_saddr(sk, saddr, family);
  860. head2 = inet_bhashfn_portaddr(hinfo, sk, net, port);
  861. spin_lock(&head2->lock);
  862. tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, sk);
  863. if (!tb2) {
  864. tb2 = new_tb2;
  865. inet_bind2_bucket_init(tb2, net, head2, inet_csk(sk)->icsk_bind_hash, sk);
  866. if (sk_is_connect_bind(sk)) {
  867. tb2->fastreuse = -1;
  868. tb2->fastreuseport = -1;
  869. }
  870. }
  871. inet_csk(sk)->icsk_bind2_hash = tb2;
  872. sk_add_bind_node(sk, &tb2->owners);
  873. spin_unlock(&head2->lock);
  874. spin_unlock_bh(&head->lock);
  875. if (tb2 != new_tb2)
  876. kmem_cache_free(hinfo->bind2_bucket_cachep, new_tb2);
  877. return 0;
  878. }
  879. int inet_bhash2_update_saddr(struct sock *sk, void *saddr, int family)
  880. {
  881. return __inet_bhash2_update_saddr(sk, saddr, family, false);
  882. }
  883. EXPORT_IPV6_MOD(inet_bhash2_update_saddr);
  884. void inet_bhash2_reset_saddr(struct sock *sk)
  885. {
  886. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  887. __inet_bhash2_update_saddr(sk, NULL, 0, true);
  888. }
  889. EXPORT_IPV6_MOD(inet_bhash2_reset_saddr);
  890. /* RFC 6056 3.3.4. Algorithm 4: Double-Hash Port Selection Algorithm
  891. * Note that we use 32bit integers (vs RFC 'short integers')
  892. * because 2^16 is not a multiple of num_ephemeral and this
  893. * property might be used by clever attacker.
  894. *
  895. * RFC claims using TABLE_LENGTH=10 buckets gives an improvement, though
  896. * attacks were since demonstrated, thus we use 65536 by default instead
  897. * to really give more isolation and privacy, at the expense of 256kB
  898. * of kernel memory.
  899. */
  900. #define INET_TABLE_PERTURB_SIZE (1 << CONFIG_INET_TABLE_PERTURB_ORDER)
  901. static u32 *table_perturb;
  902. int __inet_hash_connect(struct inet_timewait_death_row *death_row,
  903. struct sock *sk, u64 port_offset,
  904. u32 hash_port0,
  905. int (*check_established)(struct inet_timewait_death_row *,
  906. struct sock *, __u16, struct inet_timewait_sock **,
  907. bool rcu_lookup, u32 hash))
  908. {
  909. struct inet_hashinfo *hinfo = death_row->hashinfo;
  910. struct inet_bind_hashbucket *head, *head2;
  911. struct inet_timewait_sock *tw = NULL;
  912. int port = inet_sk(sk)->inet_num;
  913. struct net *net = sock_net(sk);
  914. struct inet_bind2_bucket *tb2;
  915. struct inet_bind_bucket *tb;
  916. bool tb_created = false;
  917. u32 remaining, offset;
  918. int ret, i, low, high;
  919. bool local_ports;
  920. int step, l3mdev;
  921. u32 index;
  922. if (port) {
  923. local_bh_disable();
  924. ret = check_established(death_row, sk, port, NULL, false,
  925. hash_port0 + port);
  926. local_bh_enable();
  927. return ret;
  928. }
  929. l3mdev = inet_sk_bound_l3mdev(sk);
  930. local_ports = inet_sk_get_local_port_range(sk, &low, &high);
  931. step = local_ports ? 1 : 2;
  932. high++; /* [32768, 60999] -> [32768, 61000[ */
  933. remaining = high - low;
  934. if (!local_ports && remaining > 1)
  935. remaining &= ~1U;
  936. get_random_sleepable_once(table_perturb,
  937. INET_TABLE_PERTURB_SIZE * sizeof(*table_perturb));
  938. index = port_offset & (INET_TABLE_PERTURB_SIZE - 1);
  939. offset = READ_ONCE(table_perturb[index]) + (port_offset >> 32);
  940. offset %= remaining;
  941. /* In first pass we try ports of @low parity.
  942. * inet_csk_get_port() does the opposite choice.
  943. */
  944. if (!local_ports)
  945. offset &= ~1U;
  946. other_parity_scan:
  947. port = low + offset;
  948. for (i = 0; i < remaining; i += step, port += step) {
  949. if (unlikely(port >= high))
  950. port -= remaining;
  951. if (inet_is_local_reserved_port(net, port))
  952. continue;
  953. head = &hinfo->bhash[inet_bhashfn(net, port,
  954. hinfo->bhash_size)];
  955. rcu_read_lock();
  956. hlist_for_each_entry_rcu(tb, &head->chain, node) {
  957. if (!inet_bind_bucket_match(tb, net, port, l3mdev))
  958. continue;
  959. if (tb->fastreuse >= 0 || tb->fastreuseport >= 0) {
  960. rcu_read_unlock();
  961. goto next_port;
  962. }
  963. if (!check_established(death_row, sk, port, &tw, true,
  964. hash_port0 + port))
  965. break;
  966. rcu_read_unlock();
  967. goto next_port;
  968. }
  969. rcu_read_unlock();
  970. spin_lock_bh(&head->lock);
  971. /* Does not bother with rcv_saddr checks, because
  972. * the established check is already unique enough.
  973. */
  974. inet_bind_bucket_for_each(tb, &head->chain) {
  975. if (inet_bind_bucket_match(tb, net, port, l3mdev)) {
  976. if (tb->fastreuse >= 0 ||
  977. tb->fastreuseport >= 0)
  978. goto next_port_unlock;
  979. WARN_ON(hlist_empty(&tb->bhash2));
  980. if (!check_established(death_row, sk,
  981. port, &tw, false,
  982. hash_port0 + port))
  983. goto ok;
  984. goto next_port_unlock;
  985. }
  986. }
  987. tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
  988. net, head, port, l3mdev);
  989. if (!tb) {
  990. spin_unlock_bh(&head->lock);
  991. return -ENOMEM;
  992. }
  993. tb_created = true;
  994. tb->fastreuse = -1;
  995. tb->fastreuseport = -1;
  996. goto ok;
  997. next_port_unlock:
  998. spin_unlock_bh(&head->lock);
  999. next_port:
  1000. cond_resched();
  1001. }
  1002. if (!local_ports) {
  1003. offset++;
  1004. if ((offset & 1) && remaining > 1)
  1005. goto other_parity_scan;
  1006. }
  1007. return -EADDRNOTAVAIL;
  1008. ok:
  1009. /* Find the corresponding tb2 bucket since we need to
  1010. * add the socket to the bhash2 table as well
  1011. */
  1012. head2 = inet_bhashfn_portaddr(hinfo, sk, net, port);
  1013. spin_lock(&head2->lock);
  1014. tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, sk);
  1015. if (!tb2) {
  1016. tb2 = inet_bind2_bucket_create(hinfo->bind2_bucket_cachep, net,
  1017. head2, tb, sk);
  1018. if (!tb2)
  1019. goto error;
  1020. tb2->fastreuse = -1;
  1021. tb2->fastreuseport = -1;
  1022. }
  1023. /* Here we want to add a little bit of randomness to the next source
  1024. * port that will be chosen. We use a max() with a random here so that
  1025. * on low contention the randomness is maximal and on high contention
  1026. * it may be inexistent.
  1027. */
  1028. i = max_t(int, i, get_random_u32_below(8) * step);
  1029. WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + step);
  1030. /* Head lock still held and bh's disabled */
  1031. inet_bind_hash(sk, tb, tb2, port);
  1032. sk->sk_userlocks |= SOCK_CONNECT_BIND;
  1033. if (sk_unhashed(sk)) {
  1034. inet_sk(sk)->inet_sport = htons(port);
  1035. inet_ehash_nolisten(sk, (struct sock *)tw, NULL);
  1036. }
  1037. if (tw)
  1038. inet_twsk_bind_unhash(tw, hinfo);
  1039. spin_unlock(&head2->lock);
  1040. spin_unlock(&head->lock);
  1041. if (tw)
  1042. inet_twsk_deschedule_put(tw);
  1043. local_bh_enable();
  1044. return 0;
  1045. error:
  1046. if (sk_hashed(sk)) {
  1047. spinlock_t *lock = inet_ehash_lockp(hinfo, sk->sk_hash);
  1048. sock_prot_inuse_add(net, sk->sk_prot, -1);
  1049. spin_lock(lock);
  1050. __sk_nulls_del_node_init_rcu(sk);
  1051. spin_unlock(lock);
  1052. sk->sk_hash = 0;
  1053. inet_sk(sk)->inet_sport = 0;
  1054. WRITE_ONCE(inet_sk(sk)->inet_num, 0);
  1055. if (tw)
  1056. inet_twsk_bind_unhash(tw, hinfo);
  1057. }
  1058. spin_unlock(&head2->lock);
  1059. if (tb_created)
  1060. inet_bind_bucket_destroy(tb);
  1061. spin_unlock(&head->lock);
  1062. if (tw)
  1063. inet_twsk_deschedule_put(tw);
  1064. local_bh_enable();
  1065. return -ENOMEM;
  1066. }
  1067. /*
  1068. * Bind a port for a connect operation and hash it.
  1069. */
  1070. int inet_hash_connect(struct inet_timewait_death_row *death_row,
  1071. struct sock *sk)
  1072. {
  1073. const struct inet_sock *inet = inet_sk(sk);
  1074. const struct net *net = sock_net(sk);
  1075. u64 port_offset = 0;
  1076. u32 hash_port0;
  1077. if (!inet_sk(sk)->inet_num)
  1078. port_offset = inet_sk_port_offset(sk);
  1079. hash_port0 = inet_ehashfn(net, inet->inet_rcv_saddr, 0,
  1080. inet->inet_daddr, inet->inet_dport);
  1081. return __inet_hash_connect(death_row, sk, port_offset, hash_port0,
  1082. __inet_check_established);
  1083. }
  1084. static void init_hashinfo_lhash2(struct inet_hashinfo *h)
  1085. {
  1086. int i;
  1087. for (i = 0; i <= h->lhash2_mask; i++) {
  1088. spin_lock_init(&h->lhash2[i].lock);
  1089. INIT_HLIST_NULLS_HEAD(&h->lhash2[i].nulls_head,
  1090. i + LISTENING_NULLS_BASE);
  1091. }
  1092. }
  1093. void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
  1094. unsigned long numentries, int scale,
  1095. unsigned long low_limit,
  1096. unsigned long high_limit)
  1097. {
  1098. h->lhash2 = alloc_large_system_hash(name,
  1099. sizeof(*h->lhash2),
  1100. numentries,
  1101. scale,
  1102. 0,
  1103. NULL,
  1104. &h->lhash2_mask,
  1105. low_limit,
  1106. high_limit);
  1107. init_hashinfo_lhash2(h);
  1108. /* this one is used for source ports of outgoing connections */
  1109. table_perturb = alloc_large_system_hash("Table-perturb",
  1110. sizeof(*table_perturb),
  1111. INET_TABLE_PERTURB_SIZE,
  1112. 0, 0, NULL, NULL,
  1113. INET_TABLE_PERTURB_SIZE,
  1114. INET_TABLE_PERTURB_SIZE);
  1115. }
  1116. int inet_hashinfo2_init_mod(struct inet_hashinfo *h)
  1117. {
  1118. h->lhash2 = kmalloc_objs(*h->lhash2, INET_LHTABLE_SIZE);
  1119. if (!h->lhash2)
  1120. return -ENOMEM;
  1121. h->lhash2_mask = INET_LHTABLE_SIZE - 1;
  1122. /* INET_LHTABLE_SIZE must be a power of 2 */
  1123. BUG_ON(INET_LHTABLE_SIZE & h->lhash2_mask);
  1124. init_hashinfo_lhash2(h);
  1125. return 0;
  1126. }
  1127. int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
  1128. {
  1129. unsigned int locksz = sizeof(spinlock_t);
  1130. unsigned int i, nblocks = 1;
  1131. spinlock_t *ptr = NULL;
  1132. if (locksz == 0)
  1133. goto set_mask;
  1134. /* Allocate 2 cache lines or at least one spinlock per cpu. */
  1135. nblocks = max(2U * L1_CACHE_BYTES / locksz, 1U) * num_possible_cpus();
  1136. /* At least one page per NUMA node. */
  1137. nblocks = max(nblocks, num_online_nodes() * PAGE_SIZE / locksz);
  1138. nblocks = roundup_pow_of_two(nblocks);
  1139. /* No more locks than number of hash buckets. */
  1140. nblocks = min(nblocks, hashinfo->ehash_mask + 1);
  1141. if (num_online_nodes() > 1) {
  1142. /* Use vmalloc() to allow NUMA policy to spread pages
  1143. * on all available nodes if desired.
  1144. */
  1145. ptr = vmalloc_array(nblocks, locksz);
  1146. }
  1147. if (!ptr) {
  1148. ptr = kvmalloc_array(nblocks, locksz, GFP_KERNEL);
  1149. if (!ptr)
  1150. return -ENOMEM;
  1151. }
  1152. for (i = 0; i < nblocks; i++)
  1153. spin_lock_init(&ptr[i]);
  1154. hashinfo->ehash_locks = ptr;
  1155. set_mask:
  1156. hashinfo->ehash_locks_mask = nblocks - 1;
  1157. return 0;
  1158. }
  1159. struct inet_hashinfo *inet_pernet_hashinfo_alloc(struct inet_hashinfo *hashinfo,
  1160. unsigned int ehash_entries)
  1161. {
  1162. struct inet_hashinfo *new_hashinfo;
  1163. int i;
  1164. new_hashinfo = kmemdup(hashinfo, sizeof(*hashinfo), GFP_KERNEL);
  1165. if (!new_hashinfo)
  1166. goto err;
  1167. new_hashinfo->ehash = vmalloc_huge(ehash_entries * sizeof(struct inet_ehash_bucket),
  1168. GFP_KERNEL_ACCOUNT);
  1169. if (!new_hashinfo->ehash)
  1170. goto free_hashinfo;
  1171. new_hashinfo->ehash_mask = ehash_entries - 1;
  1172. if (inet_ehash_locks_alloc(new_hashinfo))
  1173. goto free_ehash;
  1174. for (i = 0; i < ehash_entries; i++)
  1175. INIT_HLIST_NULLS_HEAD(&new_hashinfo->ehash[i].chain, i);
  1176. new_hashinfo->pernet = true;
  1177. return new_hashinfo;
  1178. free_ehash:
  1179. vfree(new_hashinfo->ehash);
  1180. free_hashinfo:
  1181. kfree(new_hashinfo);
  1182. err:
  1183. return NULL;
  1184. }
  1185. void inet_pernet_hashinfo_free(struct inet_hashinfo *hashinfo)
  1186. {
  1187. if (!hashinfo->pernet)
  1188. return;
  1189. inet_ehash_locks_free(hashinfo);
  1190. vfree(hashinfo->ehash);
  1191. kfree(hashinfo);
  1192. }