inet_hashtables.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Generic INET transport hashtables
  7. *
  8. * Authors: Lotsa people, from code originally in tcp
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/random.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/wait.h>
  20. #include <net/inet_connection_sock.h>
  21. #include <net/inet_hashtables.h>
  22. #include <net/ip.h>
  23. /*
  24. * Allocate and initialize a new local port bind bucket.
  25. * The bindhash mutex for snum's hash chain must be held here.
  26. */
  27. struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
  28. struct net *net,
  29. struct inet_bind_hashbucket *head,
  30. const unsigned short snum)
  31. {
  32. struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
  33. if (tb != NULL) {
  34. write_pnet(&tb->ib_net, hold_net(net));
  35. tb->port = snum;
  36. tb->fastreuse = 0;
  37. INIT_HLIST_HEAD(&tb->owners);
  38. hlist_add_head(&tb->node, &head->chain);
  39. }
  40. return tb;
  41. }
  42. /*
  43. * Caller must hold hashbucket lock for this tb with local BH disabled
  44. */
  45. void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
  46. {
  47. if (hlist_empty(&tb->owners)) {
  48. __hlist_del(&tb->node);
  49. release_net(ib_net(tb));
  50. kmem_cache_free(cachep, tb);
  51. }
  52. }
  53. void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  54. const unsigned short snum)
  55. {
  56. inet_sk(sk)->num = snum;
  57. sk_add_bind_node(sk, &tb->owners);
  58. inet_csk(sk)->icsk_bind_hash = tb;
  59. }
  60. /*
  61. * Get rid of any references to a local port held by the given sock.
  62. */
  63. static void __inet_put_port(struct sock *sk)
  64. {
  65. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  66. const int bhash = inet_bhashfn(sock_net(sk), inet_sk(sk)->num,
  67. hashinfo->bhash_size);
  68. struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
  69. struct inet_bind_bucket *tb;
  70. spin_lock(&head->lock);
  71. tb = inet_csk(sk)->icsk_bind_hash;
  72. __sk_del_bind_node(sk);
  73. inet_csk(sk)->icsk_bind_hash = NULL;
  74. inet_sk(sk)->num = 0;
  75. inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
  76. spin_unlock(&head->lock);
  77. }
  78. void inet_put_port(struct sock *sk)
  79. {
  80. local_bh_disable();
  81. __inet_put_port(sk);
  82. local_bh_enable();
  83. }
  84. EXPORT_SYMBOL(inet_put_port);
  85. void __inet_inherit_port(struct sock *sk, struct sock *child)
  86. {
  87. struct inet_hashinfo *table = sk->sk_prot->h.hashinfo;
  88. const int bhash = inet_bhashfn(sock_net(sk), inet_sk(child)->num,
  89. table->bhash_size);
  90. struct inet_bind_hashbucket *head = &table->bhash[bhash];
  91. struct inet_bind_bucket *tb;
  92. spin_lock(&head->lock);
  93. tb = inet_csk(sk)->icsk_bind_hash;
  94. sk_add_bind_node(child, &tb->owners);
  95. inet_csk(child)->icsk_bind_hash = tb;
  96. spin_unlock(&head->lock);
  97. }
  98. EXPORT_SYMBOL_GPL(__inet_inherit_port);
  99. static inline int compute_score(struct sock *sk, struct net *net,
  100. const unsigned short hnum, const __be32 daddr,
  101. const int dif)
  102. {
  103. int score = -1;
  104. struct inet_sock *inet = inet_sk(sk);
  105. if (net_eq(sock_net(sk), net) && inet->num == hnum &&
  106. !ipv6_only_sock(sk)) {
  107. __be32 rcv_saddr = inet->rcv_saddr;
  108. score = sk->sk_family == PF_INET ? 1 : 0;
  109. if (rcv_saddr) {
  110. if (rcv_saddr != daddr)
  111. return -1;
  112. score += 2;
  113. }
  114. if (sk->sk_bound_dev_if) {
  115. if (sk->sk_bound_dev_if != dif)
  116. return -1;
  117. score += 2;
  118. }
  119. }
  120. return score;
  121. }
  122. /*
  123. * Don't inline this cruft. Here are some nice properties to exploit here. The
  124. * BSD API does not allow a listening sock to specify the remote port nor the
  125. * remote address for the connection. So always assume those are both
  126. * wildcarded during the search since they can never be otherwise.
  127. */
  128. struct sock *__inet_lookup_listener(struct net *net,
  129. struct inet_hashinfo *hashinfo,
  130. const __be32 daddr, const unsigned short hnum,
  131. const int dif)
  132. {
  133. struct sock *sk, *result;
  134. struct hlist_nulls_node *node;
  135. unsigned int hash = inet_lhashfn(net, hnum);
  136. struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];
  137. int score, hiscore;
  138. rcu_read_lock();
  139. begin:
  140. result = NULL;
  141. hiscore = -1;
  142. sk_nulls_for_each_rcu(sk, node, &ilb->head) {
  143. score = compute_score(sk, net, hnum, daddr, dif);
  144. if (score > hiscore) {
  145. result = sk;
  146. hiscore = score;
  147. }
  148. }
  149. /*
  150. * if the nulls value we got at the end of this lookup is
  151. * not the expected one, we must restart lookup.
  152. * We probably met an item that was moved to another chain.
  153. */
  154. if (get_nulls_value(node) != hash + LISTENING_NULLS_BASE)
  155. goto begin;
  156. if (result) {
  157. if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
  158. result = NULL;
  159. else if (unlikely(compute_score(result, net, hnum, daddr,
  160. dif) < hiscore)) {
  161. sock_put(result);
  162. goto begin;
  163. }
  164. }
  165. rcu_read_unlock();
  166. return result;
  167. }
  168. EXPORT_SYMBOL_GPL(__inet_lookup_listener);
  169. struct sock * __inet_lookup_established(struct net *net,
  170. struct inet_hashinfo *hashinfo,
  171. const __be32 saddr, const __be16 sport,
  172. const __be32 daddr, const u16 hnum,
  173. const int dif)
  174. {
  175. INET_ADDR_COOKIE(acookie, saddr, daddr)
  176. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  177. struct sock *sk;
  178. const struct hlist_nulls_node *node;
  179. /* Optimize here for direct hit, only listening connections can
  180. * have wildcards anyways.
  181. */
  182. unsigned int hash = inet_ehashfn(net, daddr, hnum, saddr, sport);
  183. unsigned int slot = hash & (hashinfo->ehash_size - 1);
  184. struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
  185. rcu_read_lock();
  186. begin:
  187. sk_nulls_for_each_rcu(sk, node, &head->chain) {
  188. if (INET_MATCH(sk, net, hash, acookie,
  189. saddr, daddr, ports, dif)) {
  190. if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt)))
  191. goto begintw;
  192. if (unlikely(!INET_MATCH(sk, net, hash, acookie,
  193. saddr, daddr, ports, dif))) {
  194. sock_put(sk);
  195. goto begin;
  196. }
  197. goto out;
  198. }
  199. }
  200. /*
  201. * if the nulls value we got at the end of this lookup is
  202. * not the expected one, we must restart lookup.
  203. * We probably met an item that was moved to another chain.
  204. */
  205. if (get_nulls_value(node) != slot)
  206. goto begin;
  207. begintw:
  208. /* Must check for a TIME_WAIT'er before going to listener hash. */
  209. sk_nulls_for_each_rcu(sk, node, &head->twchain) {
  210. if (INET_TW_MATCH(sk, net, hash, acookie,
  211. saddr, daddr, ports, dif)) {
  212. if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt))) {
  213. sk = NULL;
  214. goto out;
  215. }
  216. if (unlikely(!INET_TW_MATCH(sk, net, hash, acookie,
  217. saddr, daddr, ports, dif))) {
  218. sock_put(sk);
  219. goto begintw;
  220. }
  221. goto out;
  222. }
  223. }
  224. /*
  225. * if the nulls value we got at the end of this lookup is
  226. * not the expected one, we must restart lookup.
  227. * We probably met an item that was moved to another chain.
  228. */
  229. if (get_nulls_value(node) != slot)
  230. goto begintw;
  231. sk = NULL;
  232. out:
  233. rcu_read_unlock();
  234. return sk;
  235. }
  236. EXPORT_SYMBOL_GPL(__inet_lookup_established);
  237. /* called with local bh disabled */
  238. static int __inet_check_established(struct inet_timewait_death_row *death_row,
  239. struct sock *sk, __u16 lport,
  240. struct inet_timewait_sock **twp)
  241. {
  242. struct inet_hashinfo *hinfo = death_row->hashinfo;
  243. struct inet_sock *inet = inet_sk(sk);
  244. __be32 daddr = inet->rcv_saddr;
  245. __be32 saddr = inet->daddr;
  246. int dif = sk->sk_bound_dev_if;
  247. INET_ADDR_COOKIE(acookie, saddr, daddr)
  248. const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport);
  249. struct net *net = sock_net(sk);
  250. unsigned int hash = inet_ehashfn(net, daddr, lport, saddr, inet->dport);
  251. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  252. spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
  253. struct sock *sk2;
  254. const struct hlist_nulls_node *node;
  255. struct inet_timewait_sock *tw;
  256. spin_lock(lock);
  257. /* Check TIME-WAIT sockets first. */
  258. sk_nulls_for_each(sk2, node, &head->twchain) {
  259. tw = inet_twsk(sk2);
  260. if (INET_TW_MATCH(sk2, net, hash, acookie,
  261. saddr, daddr, ports, dif)) {
  262. if (twsk_unique(sk, sk2, twp))
  263. goto unique;
  264. else
  265. goto not_unique;
  266. }
  267. }
  268. tw = NULL;
  269. /* And established part... */
  270. sk_nulls_for_each(sk2, node, &head->chain) {
  271. if (INET_MATCH(sk2, net, hash, acookie,
  272. saddr, daddr, ports, dif))
  273. goto not_unique;
  274. }
  275. unique:
  276. /* Must record num and sport now. Otherwise we will see
  277. * in hash table socket with a funny identity. */
  278. inet->num = lport;
  279. inet->sport = htons(lport);
  280. sk->sk_hash = hash;
  281. WARN_ON(!sk_unhashed(sk));
  282. __sk_nulls_add_node_rcu(sk, &head->chain);
  283. spin_unlock(lock);
  284. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  285. if (twp) {
  286. *twp = tw;
  287. NET_INC_STATS_BH(net, LINUX_MIB_TIMEWAITRECYCLED);
  288. } else if (tw) {
  289. /* Silly. Should hash-dance instead... */
  290. inet_twsk_deschedule(tw, death_row);
  291. NET_INC_STATS_BH(net, LINUX_MIB_TIMEWAITRECYCLED);
  292. inet_twsk_put(tw);
  293. }
  294. return 0;
  295. not_unique:
  296. spin_unlock(lock);
  297. return -EADDRNOTAVAIL;
  298. }
  299. static inline u32 inet_sk_port_offset(const struct sock *sk)
  300. {
  301. const struct inet_sock *inet = inet_sk(sk);
  302. return secure_ipv4_port_ephemeral(inet->rcv_saddr, inet->daddr,
  303. inet->dport);
  304. }
  305. void __inet_hash_nolisten(struct sock *sk)
  306. {
  307. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  308. struct hlist_nulls_head *list;
  309. spinlock_t *lock;
  310. struct inet_ehash_bucket *head;
  311. WARN_ON(!sk_unhashed(sk));
  312. sk->sk_hash = inet_sk_ehashfn(sk);
  313. head = inet_ehash_bucket(hashinfo, sk->sk_hash);
  314. list = &head->chain;
  315. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  316. spin_lock(lock);
  317. __sk_nulls_add_node_rcu(sk, list);
  318. spin_unlock(lock);
  319. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  320. }
  321. EXPORT_SYMBOL_GPL(__inet_hash_nolisten);
  322. static void __inet_hash(struct sock *sk)
  323. {
  324. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  325. struct inet_listen_hashbucket *ilb;
  326. if (sk->sk_state != TCP_LISTEN) {
  327. __inet_hash_nolisten(sk);
  328. return;
  329. }
  330. WARN_ON(!sk_unhashed(sk));
  331. ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  332. spin_lock(&ilb->lock);
  333. __sk_nulls_add_node_rcu(sk, &ilb->head);
  334. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  335. spin_unlock(&ilb->lock);
  336. }
  337. void inet_hash(struct sock *sk)
  338. {
  339. if (sk->sk_state != TCP_CLOSE) {
  340. local_bh_disable();
  341. __inet_hash(sk);
  342. local_bh_enable();
  343. }
  344. }
  345. EXPORT_SYMBOL_GPL(inet_hash);
  346. void inet_unhash(struct sock *sk)
  347. {
  348. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  349. spinlock_t *lock;
  350. int done;
  351. if (sk_unhashed(sk))
  352. return;
  353. if (sk->sk_state == TCP_LISTEN)
  354. lock = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)].lock;
  355. else
  356. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  357. spin_lock_bh(lock);
  358. done =__sk_nulls_del_node_init_rcu(sk);
  359. if (done)
  360. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  361. spin_unlock_bh(lock);
  362. }
  363. EXPORT_SYMBOL_GPL(inet_unhash);
  364. int __inet_hash_connect(struct inet_timewait_death_row *death_row,
  365. struct sock *sk, u32 port_offset,
  366. int (*check_established)(struct inet_timewait_death_row *,
  367. struct sock *, __u16, struct inet_timewait_sock **),
  368. void (*hash)(struct sock *sk))
  369. {
  370. struct inet_hashinfo *hinfo = death_row->hashinfo;
  371. const unsigned short snum = inet_sk(sk)->num;
  372. struct inet_bind_hashbucket *head;
  373. struct inet_bind_bucket *tb;
  374. int ret;
  375. struct net *net = sock_net(sk);
  376. if (!snum) {
  377. int i, remaining, low, high, port;
  378. static u32 hint;
  379. u32 offset = hint + port_offset;
  380. struct hlist_node *node;
  381. struct inet_timewait_sock *tw = NULL;
  382. inet_get_local_port_range(&low, &high);
  383. remaining = (high - low) + 1;
  384. local_bh_disable();
  385. for (i = 1; i <= remaining; i++) {
  386. port = low + (i + offset) % remaining;
  387. head = &hinfo->bhash[inet_bhashfn(net, port,
  388. hinfo->bhash_size)];
  389. spin_lock(&head->lock);
  390. /* Does not bother with rcv_saddr checks,
  391. * because the established check is already
  392. * unique enough.
  393. */
  394. inet_bind_bucket_for_each(tb, node, &head->chain) {
  395. if (ib_net(tb) == net && tb->port == port) {
  396. WARN_ON(hlist_empty(&tb->owners));
  397. if (tb->fastreuse >= 0)
  398. goto next_port;
  399. if (!check_established(death_row, sk,
  400. port, &tw))
  401. goto ok;
  402. goto next_port;
  403. }
  404. }
  405. tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
  406. net, head, port);
  407. if (!tb) {
  408. spin_unlock(&head->lock);
  409. break;
  410. }
  411. tb->fastreuse = -1;
  412. goto ok;
  413. next_port:
  414. spin_unlock(&head->lock);
  415. }
  416. local_bh_enable();
  417. return -EADDRNOTAVAIL;
  418. ok:
  419. hint += i;
  420. /* Head lock still held and bh's disabled */
  421. inet_bind_hash(sk, tb, port);
  422. if (sk_unhashed(sk)) {
  423. inet_sk(sk)->sport = htons(port);
  424. hash(sk);
  425. }
  426. spin_unlock(&head->lock);
  427. if (tw) {
  428. inet_twsk_deschedule(tw, death_row);
  429. inet_twsk_put(tw);
  430. }
  431. ret = 0;
  432. goto out;
  433. }
  434. head = &hinfo->bhash[inet_bhashfn(net, snum, hinfo->bhash_size)];
  435. tb = inet_csk(sk)->icsk_bind_hash;
  436. spin_lock_bh(&head->lock);
  437. if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
  438. hash(sk);
  439. spin_unlock_bh(&head->lock);
  440. return 0;
  441. } else {
  442. spin_unlock(&head->lock);
  443. /* No definite answer... Walk to established hash table */
  444. ret = check_established(death_row, sk, snum, NULL);
  445. out:
  446. local_bh_enable();
  447. return ret;
  448. }
  449. }
  450. /*
  451. * Bind a port for a connect operation and hash it.
  452. */
  453. int inet_hash_connect(struct inet_timewait_death_row *death_row,
  454. struct sock *sk)
  455. {
  456. return __inet_hash_connect(death_row, sk, inet_sk_port_offset(sk),
  457. __inet_check_established, __inet_hash_nolisten);
  458. }
  459. EXPORT_SYMBOL_GPL(inet_hash_connect);
  460. void inet_hashinfo_init(struct inet_hashinfo *h)
  461. {
  462. int i;
  463. for (i = 0; i < INET_LHTABLE_SIZE; i++) {
  464. spin_lock_init(&h->listening_hash[i].lock);
  465. INIT_HLIST_NULLS_HEAD(&h->listening_hash[i].head,
  466. i + LISTENING_NULLS_BASE);
  467. }
  468. }
  469. EXPORT_SYMBOL_GPL(inet_hashinfo_init);