inet6_hashtables.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 INET6 transport hashtables
  7. *
  8. * Authors: Lotsa people, from code originally in tcp, generalised here
  9. * by Arnaldo Carvalho de Melo <acme@mandriva.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/random.h>
  18. #include <net/inet_connection_sock.h>
  19. #include <net/inet_hashtables.h>
  20. #include <net/inet6_hashtables.h>
  21. #include <net/ip.h>
  22. void __inet6_hash(struct sock *sk)
  23. {
  24. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  25. struct hlist_head *list;
  26. rwlock_t *lock;
  27. BUG_TRAP(sk_unhashed(sk));
  28. if (sk->sk_state == TCP_LISTEN) {
  29. list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  30. lock = &hashinfo->lhash_lock;
  31. inet_listen_wlock(hashinfo);
  32. } else {
  33. unsigned int hash;
  34. sk->sk_hash = hash = inet6_sk_ehashfn(sk);
  35. list = &inet_ehash_bucket(hashinfo, hash)->chain;
  36. lock = inet_ehash_lockp(hashinfo, hash);
  37. write_lock(lock);
  38. }
  39. __sk_add_node(sk, list);
  40. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  41. write_unlock(lock);
  42. }
  43. EXPORT_SYMBOL(__inet6_hash);
  44. /*
  45. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
  46. * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
  47. *
  48. * The sockhash lock must be held as a reader here.
  49. */
  50. struct sock *__inet6_lookup_established(struct net *net,
  51. struct inet_hashinfo *hashinfo,
  52. const struct in6_addr *saddr,
  53. const __be16 sport,
  54. const struct in6_addr *daddr,
  55. const u16 hnum,
  56. const int dif)
  57. {
  58. struct sock *sk;
  59. const struct hlist_node *node;
  60. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  61. /* Optimize here for direct hit, only listening connections can
  62. * have wildcards anyways.
  63. */
  64. unsigned int hash = inet6_ehashfn(daddr, hnum, saddr, sport);
  65. struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
  66. rwlock_t *lock = inet_ehash_lockp(hashinfo, hash);
  67. prefetch(head->chain.first);
  68. read_lock(lock);
  69. sk_for_each(sk, node, &head->chain) {
  70. /* For IPV6 do the cheaper port and family tests first. */
  71. if (INET6_MATCH(sk, net, hash, saddr, daddr, ports, dif))
  72. goto hit; /* You sunk my battleship! */
  73. }
  74. /* Must check for a TIME_WAIT'er before going to listener hash. */
  75. sk_for_each(sk, node, &head->twchain) {
  76. if (INET6_TW_MATCH(sk, net, hash, saddr, daddr, ports, dif))
  77. goto hit;
  78. }
  79. read_unlock(lock);
  80. return NULL;
  81. hit:
  82. sock_hold(sk);
  83. read_unlock(lock);
  84. return sk;
  85. }
  86. EXPORT_SYMBOL(__inet6_lookup_established);
  87. struct sock *inet6_lookup_listener(struct net *net,
  88. struct inet_hashinfo *hashinfo, const struct in6_addr *daddr,
  89. const unsigned short hnum, const int dif)
  90. {
  91. struct sock *sk;
  92. const struct hlist_node *node;
  93. struct sock *result = NULL;
  94. int score, hiscore = 0;
  95. read_lock(&hashinfo->lhash_lock);
  96. sk_for_each(sk, node, &hashinfo->listening_hash[inet_lhashfn(hnum)]) {
  97. if (net_eq(sock_net(sk), net) && inet_sk(sk)->num == hnum &&
  98. sk->sk_family == PF_INET6) {
  99. const struct ipv6_pinfo *np = inet6_sk(sk);
  100. score = 1;
  101. if (!ipv6_addr_any(&np->rcv_saddr)) {
  102. if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
  103. continue;
  104. score++;
  105. }
  106. if (sk->sk_bound_dev_if) {
  107. if (sk->sk_bound_dev_if != dif)
  108. continue;
  109. score++;
  110. }
  111. if (score == 3) {
  112. result = sk;
  113. break;
  114. }
  115. if (score > hiscore) {
  116. hiscore = score;
  117. result = sk;
  118. }
  119. }
  120. }
  121. if (result)
  122. sock_hold(result);
  123. read_unlock(&hashinfo->lhash_lock);
  124. return result;
  125. }
  126. EXPORT_SYMBOL_GPL(inet6_lookup_listener);
  127. struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo,
  128. const struct in6_addr *saddr, const __be16 sport,
  129. const struct in6_addr *daddr, const __be16 dport,
  130. const int dif)
  131. {
  132. struct sock *sk;
  133. local_bh_disable();
  134. sk = __inet6_lookup(net, hashinfo, saddr, sport, daddr, ntohs(dport), dif);
  135. local_bh_enable();
  136. return sk;
  137. }
  138. EXPORT_SYMBOL_GPL(inet6_lookup);
  139. static int __inet6_check_established(struct inet_timewait_death_row *death_row,
  140. struct sock *sk, const __u16 lport,
  141. struct inet_timewait_sock **twp)
  142. {
  143. struct inet_hashinfo *hinfo = death_row->hashinfo;
  144. struct inet_sock *inet = inet_sk(sk);
  145. const struct ipv6_pinfo *np = inet6_sk(sk);
  146. const struct in6_addr *daddr = &np->rcv_saddr;
  147. const struct in6_addr *saddr = &np->daddr;
  148. const int dif = sk->sk_bound_dev_if;
  149. const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport);
  150. const unsigned int hash = inet6_ehashfn(daddr, lport, saddr,
  151. inet->dport);
  152. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  153. rwlock_t *lock = inet_ehash_lockp(hinfo, hash);
  154. struct sock *sk2;
  155. const struct hlist_node *node;
  156. struct inet_timewait_sock *tw;
  157. struct net *net = sock_net(sk);
  158. prefetch(head->chain.first);
  159. write_lock(lock);
  160. /* Check TIME-WAIT sockets first. */
  161. sk_for_each(sk2, node, &head->twchain) {
  162. tw = inet_twsk(sk2);
  163. if (INET6_TW_MATCH(sk2, net, hash, saddr, daddr, ports, dif)) {
  164. if (twsk_unique(sk, sk2, twp))
  165. goto unique;
  166. else
  167. goto not_unique;
  168. }
  169. }
  170. tw = NULL;
  171. /* And established part... */
  172. sk_for_each(sk2, node, &head->chain) {
  173. if (INET6_MATCH(sk2, net, hash, saddr, daddr, ports, dif))
  174. goto not_unique;
  175. }
  176. unique:
  177. /* Must record num and sport now. Otherwise we will see
  178. * in hash table socket with a funny identity. */
  179. inet->num = lport;
  180. inet->sport = htons(lport);
  181. BUG_TRAP(sk_unhashed(sk));
  182. __sk_add_node(sk, &head->chain);
  183. sk->sk_hash = hash;
  184. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  185. write_unlock(lock);
  186. if (twp != NULL) {
  187. *twp = tw;
  188. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  189. } else if (tw != NULL) {
  190. /* Silly. Should hash-dance instead... */
  191. inet_twsk_deschedule(tw, death_row);
  192. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  193. inet_twsk_put(tw);
  194. }
  195. return 0;
  196. not_unique:
  197. write_unlock(lock);
  198. return -EADDRNOTAVAIL;
  199. }
  200. static inline u32 inet6_sk_port_offset(const struct sock *sk)
  201. {
  202. const struct inet_sock *inet = inet_sk(sk);
  203. const struct ipv6_pinfo *np = inet6_sk(sk);
  204. return secure_ipv6_port_ephemeral(np->rcv_saddr.s6_addr32,
  205. np->daddr.s6_addr32,
  206. inet->dport);
  207. }
  208. int inet6_hash_connect(struct inet_timewait_death_row *death_row,
  209. struct sock *sk)
  210. {
  211. return __inet_hash_connect(death_row, sk, inet6_sk_port_offset(sk),
  212. __inet6_check_established, __inet6_hash);
  213. }
  214. EXPORT_SYMBOL_GPL(inet6_hash_connect);