inet_hashtables.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/config.h>
  16. #include <linux/module.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/wait.h>
  20. #include <net/inet_hashtables.h>
  21. /*
  22. * Allocate and initialize a new local port bind bucket.
  23. * The bindhash mutex for snum's hash chain must be held here.
  24. */
  25. struct inet_bind_bucket *inet_bind_bucket_create(kmem_cache_t *cachep,
  26. struct inet_bind_hashbucket *head,
  27. const unsigned short snum)
  28. {
  29. struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, SLAB_ATOMIC);
  30. if (tb != NULL) {
  31. tb->port = snum;
  32. tb->fastreuse = 0;
  33. INIT_HLIST_HEAD(&tb->owners);
  34. hlist_add_head(&tb->node, &head->chain);
  35. }
  36. return tb;
  37. }
  38. EXPORT_SYMBOL(inet_bind_bucket_create);
  39. /*
  40. * Caller must hold hashbucket lock for this tb with local BH disabled
  41. */
  42. void inet_bind_bucket_destroy(kmem_cache_t *cachep, struct inet_bind_bucket *tb)
  43. {
  44. if (hlist_empty(&tb->owners)) {
  45. __hlist_del(&tb->node);
  46. kmem_cache_free(cachep, tb);
  47. }
  48. }
  49. void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  50. const unsigned short snum)
  51. {
  52. struct inet_sock *inet = inet_sk(sk);
  53. inet->num = snum;
  54. sk_add_bind_node(sk, &tb->owners);
  55. inet->bind_hash = tb;
  56. }
  57. EXPORT_SYMBOL(inet_bind_hash);
  58. /*
  59. * Get rid of any references to a local port held by the given sock.
  60. */
  61. static void __inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
  62. {
  63. struct inet_sock *inet = inet_sk(sk);
  64. const int bhash = inet_bhashfn(inet->num, hashinfo->bhash_size);
  65. struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
  66. struct inet_bind_bucket *tb;
  67. spin_lock(&head->lock);
  68. tb = inet->bind_hash;
  69. __sk_del_bind_node(sk);
  70. inet->bind_hash = NULL;
  71. inet->num = 0;
  72. inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
  73. spin_unlock(&head->lock);
  74. }
  75. void inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
  76. {
  77. local_bh_disable();
  78. __inet_put_port(hashinfo, sk);
  79. local_bh_enable();
  80. }
  81. EXPORT_SYMBOL(inet_put_port);
  82. /*
  83. * This lock without WQ_FLAG_EXCLUSIVE is good on UP and it can be very bad on SMP.
  84. * Look, when several writers sleep and reader wakes them up, all but one
  85. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  86. * this, _but_ remember, it adds useless work on UP machines (wake up each
  87. * exclusive lock release). It should be ifdefed really.
  88. */
  89. void inet_listen_wlock(struct inet_hashinfo *hashinfo)
  90. {
  91. write_lock(&hashinfo->lhash_lock);
  92. if (atomic_read(&hashinfo->lhash_users)) {
  93. DEFINE_WAIT(wait);
  94. for (;;) {
  95. prepare_to_wait_exclusive(&hashinfo->lhash_wait,
  96. &wait, TASK_UNINTERRUPTIBLE);
  97. if (!atomic_read(&hashinfo->lhash_users))
  98. break;
  99. write_unlock_bh(&hashinfo->lhash_lock);
  100. schedule();
  101. write_lock_bh(&hashinfo->lhash_lock);
  102. }
  103. finish_wait(&hashinfo->lhash_wait, &wait);
  104. }
  105. }
  106. EXPORT_SYMBOL(inet_listen_wlock);
  107. /*
  108. * Don't inline this cruft. Here are some nice properties to exploit here. The
  109. * BSD API does not allow a listening sock to specify the remote port nor the
  110. * remote address for the connection. So always assume those are both
  111. * wildcarded during the search since they can never be otherwise.
  112. */
  113. struct sock *__inet_lookup_listener(const struct hlist_head *head, const u32 daddr,
  114. const unsigned short hnum, const int dif)
  115. {
  116. struct sock *result = NULL, *sk;
  117. const struct hlist_node *node;
  118. int hiscore = -1;
  119. sk_for_each(sk, node, head) {
  120. const struct inet_sock *inet = inet_sk(sk);
  121. if (inet->num == hnum && !ipv6_only_sock(sk)) {
  122. const __u32 rcv_saddr = inet->rcv_saddr;
  123. int score = sk->sk_family == PF_INET ? 1 : 0;
  124. if (rcv_saddr) {
  125. if (rcv_saddr != daddr)
  126. continue;
  127. score += 2;
  128. }
  129. if (sk->sk_bound_dev_if) {
  130. if (sk->sk_bound_dev_if != dif)
  131. continue;
  132. score += 2;
  133. }
  134. if (score == 5)
  135. return sk;
  136. if (score > hiscore) {
  137. hiscore = score;
  138. result = sk;
  139. }
  140. }
  141. }
  142. return result;
  143. }
  144. EXPORT_SYMBOL_GPL(__inet_lookup_listener);