inet6_hashtables.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. WARN_ON(!sk_unhashed(sk));
  26. if (sk->sk_state == TCP_LISTEN) {
  27. struct inet_listen_hashbucket *ilb;
  28. ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  29. spin_lock(&ilb->lock);
  30. __sk_nulls_add_node_rcu(sk, &ilb->head);
  31. spin_unlock(&ilb->lock);
  32. } else {
  33. unsigned int hash;
  34. struct hlist_nulls_head *list;
  35. spinlock_t *lock;
  36. sk->sk_hash = hash = inet6_sk_ehashfn(sk);
  37. list = &inet_ehash_bucket(hashinfo, hash)->chain;
  38. lock = inet_ehash_lockp(hashinfo, hash);
  39. spin_lock(lock);
  40. __sk_nulls_add_node_rcu(sk, list);
  41. spin_unlock(lock);
  42. }
  43. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  44. }
  45. EXPORT_SYMBOL(__inet6_hash);
  46. /*
  47. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
  48. * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
  49. *
  50. * The sockhash lock must be held as a reader here.
  51. */
  52. struct sock *__inet6_lookup_established(struct net *net,
  53. struct inet_hashinfo *hashinfo,
  54. const struct in6_addr *saddr,
  55. const __be16 sport,
  56. const struct in6_addr *daddr,
  57. const u16 hnum,
  58. const int dif)
  59. {
  60. struct sock *sk;
  61. const struct hlist_nulls_node *node;
  62. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  63. /* Optimize here for direct hit, only listening connections can
  64. * have wildcards anyways.
  65. */
  66. unsigned int hash = inet6_ehashfn(net, daddr, hnum, saddr, sport);
  67. unsigned int slot = hash & (hashinfo->ehash_size - 1);
  68. struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
  69. rcu_read_lock();
  70. begin:
  71. sk_nulls_for_each_rcu(sk, node, &head->chain) {
  72. /* For IPV6 do the cheaper port and family tests first. */
  73. if (INET6_MATCH(sk, net, hash, saddr, daddr, ports, dif)) {
  74. if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt)))
  75. goto begintw;
  76. if (!INET6_MATCH(sk, net, hash, saddr, daddr, ports, dif)) {
  77. sock_put(sk);
  78. goto begin;
  79. }
  80. goto out;
  81. }
  82. }
  83. if (get_nulls_value(node) != slot)
  84. goto begin;
  85. begintw:
  86. /* Must check for a TIME_WAIT'er before going to listener hash. */
  87. sk_nulls_for_each_rcu(sk, node, &head->twchain) {
  88. if (INET6_TW_MATCH(sk, net, hash, saddr, daddr, ports, dif)) {
  89. if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt))) {
  90. sk = NULL;
  91. goto out;
  92. }
  93. if (!INET6_TW_MATCH(sk, net, hash, saddr, daddr, ports, dif)) {
  94. sock_put(sk);
  95. goto begintw;
  96. }
  97. goto out;
  98. }
  99. }
  100. if (get_nulls_value(node) != slot)
  101. goto begintw;
  102. sk = NULL;
  103. out:
  104. rcu_read_unlock();
  105. return sk;
  106. }
  107. EXPORT_SYMBOL(__inet6_lookup_established);
  108. static int inline compute_score(struct sock *sk, struct net *net,
  109. const unsigned short hnum,
  110. const struct in6_addr *daddr,
  111. const int dif)
  112. {
  113. int score = -1;
  114. if (net_eq(sock_net(sk), net) && inet_sk(sk)->num == hnum &&
  115. sk->sk_family == PF_INET6) {
  116. const struct ipv6_pinfo *np = inet6_sk(sk);
  117. score = 1;
  118. if (!ipv6_addr_any(&np->rcv_saddr)) {
  119. if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
  120. return -1;
  121. score++;
  122. }
  123. if (sk->sk_bound_dev_if) {
  124. if (sk->sk_bound_dev_if != dif)
  125. return -1;
  126. score++;
  127. }
  128. }
  129. return score;
  130. }
  131. struct sock *inet6_lookup_listener(struct net *net,
  132. struct inet_hashinfo *hashinfo, const struct in6_addr *daddr,
  133. const unsigned short hnum, const int dif)
  134. {
  135. struct sock *sk;
  136. const struct hlist_nulls_node *node;
  137. struct sock *result;
  138. int score, hiscore;
  139. unsigned int hash = inet_lhashfn(net, hnum);
  140. struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];
  141. rcu_read_lock();
  142. begin:
  143. result = NULL;
  144. hiscore = -1;
  145. sk_nulls_for_each(sk, node, &ilb->head) {
  146. score = compute_score(sk, net, hnum, daddr, dif);
  147. if (score > hiscore) {
  148. hiscore = score;
  149. result = sk;
  150. }
  151. }
  152. /*
  153. * if the nulls value we got at the end of this lookup is
  154. * not the expected one, we must restart lookup.
  155. * We probably met an item that was moved to another chain.
  156. */
  157. if (get_nulls_value(node) != hash + LISTENING_NULLS_BASE)
  158. goto begin;
  159. if (result) {
  160. if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
  161. result = NULL;
  162. else if (unlikely(compute_score(result, net, hnum, daddr,
  163. dif) < hiscore)) {
  164. sock_put(result);
  165. goto begin;
  166. }
  167. }
  168. rcu_read_unlock();
  169. return result;
  170. }
  171. EXPORT_SYMBOL_GPL(inet6_lookup_listener);
  172. struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo,
  173. const struct in6_addr *saddr, const __be16 sport,
  174. const struct in6_addr *daddr, const __be16 dport,
  175. const int dif)
  176. {
  177. struct sock *sk;
  178. local_bh_disable();
  179. sk = __inet6_lookup(net, hashinfo, saddr, sport, daddr, ntohs(dport), dif);
  180. local_bh_enable();
  181. return sk;
  182. }
  183. EXPORT_SYMBOL_GPL(inet6_lookup);
  184. static int __inet6_check_established(struct inet_timewait_death_row *death_row,
  185. struct sock *sk, const __u16 lport,
  186. struct inet_timewait_sock **twp)
  187. {
  188. struct inet_hashinfo *hinfo = death_row->hashinfo;
  189. struct inet_sock *inet = inet_sk(sk);
  190. const struct ipv6_pinfo *np = inet6_sk(sk);
  191. const struct in6_addr *daddr = &np->rcv_saddr;
  192. const struct in6_addr *saddr = &np->daddr;
  193. const int dif = sk->sk_bound_dev_if;
  194. const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport);
  195. struct net *net = sock_net(sk);
  196. const unsigned int hash = inet6_ehashfn(net, daddr, lport, saddr,
  197. inet->dport);
  198. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  199. spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
  200. struct sock *sk2;
  201. const struct hlist_nulls_node *node;
  202. struct inet_timewait_sock *tw;
  203. spin_lock(lock);
  204. /* Check TIME-WAIT sockets first. */
  205. sk_nulls_for_each(sk2, node, &head->twchain) {
  206. tw = inet_twsk(sk2);
  207. if (INET6_TW_MATCH(sk2, net, hash, saddr, daddr, ports, dif)) {
  208. if (twsk_unique(sk, sk2, twp))
  209. goto unique;
  210. else
  211. goto not_unique;
  212. }
  213. }
  214. tw = NULL;
  215. /* And established part... */
  216. sk_nulls_for_each(sk2, node, &head->chain) {
  217. if (INET6_MATCH(sk2, net, hash, saddr, daddr, ports, dif))
  218. goto not_unique;
  219. }
  220. unique:
  221. /* Must record num and sport now. Otherwise we will see
  222. * in hash table socket with a funny identity. */
  223. inet->num = lport;
  224. inet->sport = htons(lport);
  225. WARN_ON(!sk_unhashed(sk));
  226. __sk_nulls_add_node_rcu(sk, &head->chain);
  227. sk->sk_hash = hash;
  228. spin_unlock(lock);
  229. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  230. if (twp != NULL) {
  231. *twp = tw;
  232. NET_INC_STATS_BH(net, LINUX_MIB_TIMEWAITRECYCLED);
  233. } else if (tw != NULL) {
  234. /* Silly. Should hash-dance instead... */
  235. inet_twsk_deschedule(tw, death_row);
  236. NET_INC_STATS_BH(net, LINUX_MIB_TIMEWAITRECYCLED);
  237. inet_twsk_put(tw);
  238. }
  239. return 0;
  240. not_unique:
  241. spin_unlock(lock);
  242. return -EADDRNOTAVAIL;
  243. }
  244. static inline u32 inet6_sk_port_offset(const struct sock *sk)
  245. {
  246. const struct inet_sock *inet = inet_sk(sk);
  247. const struct ipv6_pinfo *np = inet6_sk(sk);
  248. return secure_ipv6_port_ephemeral(np->rcv_saddr.s6_addr32,
  249. np->daddr.s6_addr32,
  250. inet->dport);
  251. }
  252. int inet6_hash_connect(struct inet_timewait_death_row *death_row,
  253. struct sock *sk)
  254. {
  255. return __inet_hash_connect(death_row, sk, inet6_sk_port_offset(sk),
  256. __inet6_check_established, __inet6_hash);
  257. }
  258. EXPORT_SYMBOL_GPL(inet6_hash_connect);