inet_hashtables.c 35 KB

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