inet6_connection_sock.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. * Support for INET6 connection oriented protocols.
  7. *
  8. * Authors: See the TCPv6 sources
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or(at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/in6.h>
  17. #include <linux/ipv6.h>
  18. #include <linux/jhash.h>
  19. #include <net/addrconf.h>
  20. #include <net/inet_connection_sock.h>
  21. #include <net/inet_ecn.h>
  22. #include <net/inet_hashtables.h>
  23. #include <net/ip6_route.h>
  24. #include <net/sock.h>
  25. #include <net/inet6_connection_sock.h>
  26. int inet6_csk_bind_conflict(const struct sock *sk,
  27. const struct inet_bind_bucket *tb)
  28. {
  29. const struct sock *sk2;
  30. const struct hlist_node *node;
  31. /* We must walk the whole port owner list in this case. -DaveM */
  32. /*
  33. * See comment in inet_csk_bind_conflict about sock lookup
  34. * vs net namespaces issues.
  35. */
  36. sk_for_each_bound(sk2, node, &tb->owners) {
  37. if (sk != sk2 &&
  38. (!sk->sk_bound_dev_if ||
  39. !sk2->sk_bound_dev_if ||
  40. sk->sk_bound_dev_if == sk2->sk_bound_dev_if) &&
  41. (!sk->sk_reuse || !sk2->sk_reuse ||
  42. sk2->sk_state == TCP_LISTEN) &&
  43. ipv6_rcv_saddr_equal(sk, sk2))
  44. break;
  45. }
  46. return node != NULL;
  47. }
  48. EXPORT_SYMBOL_GPL(inet6_csk_bind_conflict);
  49. /*
  50. * request_sock (formerly open request) hash tables.
  51. */
  52. static u32 inet6_synq_hash(const struct in6_addr *raddr, const __be16 rport,
  53. const u32 rnd, const u16 synq_hsize)
  54. {
  55. u32 a = (__force u32)raddr->s6_addr32[0];
  56. u32 b = (__force u32)raddr->s6_addr32[1];
  57. u32 c = (__force u32)raddr->s6_addr32[2];
  58. a += JHASH_GOLDEN_RATIO;
  59. b += JHASH_GOLDEN_RATIO;
  60. c += rnd;
  61. __jhash_mix(a, b, c);
  62. a += (__force u32)raddr->s6_addr32[3];
  63. b += (__force u32)rport;
  64. __jhash_mix(a, b, c);
  65. return c & (synq_hsize - 1);
  66. }
  67. struct request_sock *inet6_csk_search_req(const struct sock *sk,
  68. struct request_sock ***prevp,
  69. const __be16 rport,
  70. const struct in6_addr *raddr,
  71. const struct in6_addr *laddr,
  72. const int iif)
  73. {
  74. const struct inet_connection_sock *icsk = inet_csk(sk);
  75. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  76. struct request_sock *req, **prev;
  77. for (prev = &lopt->syn_table[inet6_synq_hash(raddr, rport,
  78. lopt->hash_rnd,
  79. lopt->nr_table_entries)];
  80. (req = *prev) != NULL;
  81. prev = &req->dl_next) {
  82. const struct inet6_request_sock *treq = inet6_rsk(req);
  83. if (inet_rsk(req)->rmt_port == rport &&
  84. req->rsk_ops->family == AF_INET6 &&
  85. ipv6_addr_equal(&treq->rmt_addr, raddr) &&
  86. ipv6_addr_equal(&treq->loc_addr, laddr) &&
  87. (!treq->iif || treq->iif == iif)) {
  88. WARN_ON(req->sk != NULL);
  89. *prevp = prev;
  90. return req;
  91. }
  92. }
  93. return NULL;
  94. }
  95. EXPORT_SYMBOL_GPL(inet6_csk_search_req);
  96. void inet6_csk_reqsk_queue_hash_add(struct sock *sk,
  97. struct request_sock *req,
  98. const unsigned long timeout)
  99. {
  100. struct inet_connection_sock *icsk = inet_csk(sk);
  101. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  102. const u32 h = inet6_synq_hash(&inet6_rsk(req)->rmt_addr,
  103. inet_rsk(req)->rmt_port,
  104. lopt->hash_rnd, lopt->nr_table_entries);
  105. reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
  106. inet_csk_reqsk_queue_added(sk, timeout);
  107. }
  108. EXPORT_SYMBOL_GPL(inet6_csk_reqsk_queue_hash_add);
  109. void inet6_csk_addr2sockaddr(struct sock *sk, struct sockaddr * uaddr)
  110. {
  111. struct ipv6_pinfo *np = inet6_sk(sk);
  112. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) uaddr;
  113. sin6->sin6_family = AF_INET6;
  114. ipv6_addr_copy(&sin6->sin6_addr, &np->daddr);
  115. sin6->sin6_port = inet_sk(sk)->dport;
  116. /* We do not store received flowlabel for TCP */
  117. sin6->sin6_flowinfo = 0;
  118. sin6->sin6_scope_id = 0;
  119. if (sk->sk_bound_dev_if &&
  120. ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  121. sin6->sin6_scope_id = sk->sk_bound_dev_if;
  122. }
  123. EXPORT_SYMBOL_GPL(inet6_csk_addr2sockaddr);
  124. static inline
  125. void __inet6_csk_dst_store(struct sock *sk, struct dst_entry *dst,
  126. struct in6_addr *daddr, struct in6_addr *saddr)
  127. {
  128. __ip6_dst_store(sk, dst, daddr, saddr);
  129. #ifdef CONFIG_XFRM
  130. {
  131. struct rt6_info *rt = (struct rt6_info *)dst;
  132. rt->rt6i_flow_cache_genid = atomic_read(&flow_cache_genid);
  133. }
  134. #endif
  135. }
  136. static inline
  137. struct dst_entry *__inet6_csk_dst_check(struct sock *sk, u32 cookie)
  138. {
  139. struct dst_entry *dst;
  140. dst = __sk_dst_check(sk, cookie);
  141. #ifdef CONFIG_XFRM
  142. if (dst) {
  143. struct rt6_info *rt = (struct rt6_info *)dst;
  144. if (rt->rt6i_flow_cache_genid != atomic_read(&flow_cache_genid)) {
  145. sk->sk_dst_cache = NULL;
  146. dst_release(dst);
  147. dst = NULL;
  148. }
  149. }
  150. #endif
  151. return dst;
  152. }
  153. int inet6_csk_xmit(struct sk_buff *skb, int ipfragok)
  154. {
  155. struct sock *sk = skb->sk;
  156. struct inet_sock *inet = inet_sk(sk);
  157. struct ipv6_pinfo *np = inet6_sk(sk);
  158. struct flowi fl;
  159. struct dst_entry *dst;
  160. struct in6_addr *final_p = NULL, final;
  161. memset(&fl, 0, sizeof(fl));
  162. fl.proto = sk->sk_protocol;
  163. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  164. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  165. fl.fl6_flowlabel = np->flow_label;
  166. IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
  167. fl.oif = sk->sk_bound_dev_if;
  168. fl.fl_ip_sport = inet->sport;
  169. fl.fl_ip_dport = inet->dport;
  170. security_sk_classify_flow(sk, &fl);
  171. if (np->opt && np->opt->srcrt) {
  172. struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
  173. ipv6_addr_copy(&final, &fl.fl6_dst);
  174. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  175. final_p = &final;
  176. }
  177. dst = __inet6_csk_dst_check(sk, np->dst_cookie);
  178. if (dst == NULL) {
  179. int err = ip6_dst_lookup(sk, &dst, &fl);
  180. if (err) {
  181. sk->sk_err_soft = -err;
  182. kfree_skb(skb);
  183. return err;
  184. }
  185. if (final_p)
  186. ipv6_addr_copy(&fl.fl6_dst, final_p);
  187. if ((err = xfrm_lookup(sock_net(sk), &dst, &fl, sk, 0)) < 0) {
  188. sk->sk_route_caps = 0;
  189. kfree_skb(skb);
  190. return err;
  191. }
  192. __inet6_csk_dst_store(sk, dst, NULL, NULL);
  193. }
  194. skb_dst_set(skb, dst_clone(dst));
  195. /* Restore final destination back after routing done */
  196. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  197. return ip6_xmit(sk, skb, &fl, np->opt, 0);
  198. }
  199. EXPORT_SYMBOL_GPL(inet6_csk_xmit);