inet6_hashtables.c 9.5 KB

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