inet6_hashtables.c 8.6 KB

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