inet_hashtables.c 4.4 KB

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