inet6_hashtables.c 7.5 KB

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