inet6_connection_sock.c 6.5 KB

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