inet6_connection_sock.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/config.h>
  16. #include <linux/module.h>
  17. #include <linux/in6.h>
  18. #include <linux/ipv6.h>
  19. #include <linux/jhash.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. sk_for_each_bound(sk2, node, &tb->owners) {
  34. if (sk != sk2 &&
  35. (!sk->sk_bound_dev_if ||
  36. !sk2->sk_bound_dev_if ||
  37. sk->sk_bound_dev_if == sk2->sk_bound_dev_if) &&
  38. (!sk->sk_reuse || !sk2->sk_reuse ||
  39. sk2->sk_state == TCP_LISTEN) &&
  40. ipv6_rcv_saddr_equal(sk, sk2))
  41. break;
  42. }
  43. return node != NULL;
  44. }
  45. EXPORT_SYMBOL_GPL(inet6_csk_bind_conflict);
  46. /*
  47. * request_sock (formerly open request) hash tables.
  48. */
  49. static u32 inet6_synq_hash(const struct in6_addr *raddr, const u16 rport,
  50. const u32 rnd, const u16 synq_hsize)
  51. {
  52. u32 a = raddr->s6_addr32[0];
  53. u32 b = raddr->s6_addr32[1];
  54. u32 c = raddr->s6_addr32[2];
  55. a += JHASH_GOLDEN_RATIO;
  56. b += JHASH_GOLDEN_RATIO;
  57. c += rnd;
  58. __jhash_mix(a, b, c);
  59. a += raddr->s6_addr32[3];
  60. b += (u32)rport;
  61. __jhash_mix(a, b, c);
  62. return c & (synq_hsize - 1);
  63. }
  64. struct request_sock *inet6_csk_search_req(const struct sock *sk,
  65. struct request_sock ***prevp,
  66. const __u16 rport,
  67. const struct in6_addr *raddr,
  68. const struct in6_addr *laddr,
  69. const int iif)
  70. {
  71. const struct inet_connection_sock *icsk = inet_csk(sk);
  72. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  73. struct request_sock *req, **prev;
  74. for (prev = &lopt->syn_table[inet6_synq_hash(raddr, rport,
  75. lopt->hash_rnd,
  76. lopt->nr_table_entries)];
  77. (req = *prev) != NULL;
  78. prev = &req->dl_next) {
  79. const struct inet6_request_sock *treq = inet6_rsk(req);
  80. if (inet_rsk(req)->rmt_port == rport &&
  81. req->rsk_ops->family == AF_INET6 &&
  82. ipv6_addr_equal(&treq->rmt_addr, raddr) &&
  83. ipv6_addr_equal(&treq->loc_addr, laddr) &&
  84. (!treq->iif || treq->iif == iif)) {
  85. BUG_TRAP(req->sk == NULL);
  86. *prevp = prev;
  87. return req;
  88. }
  89. }
  90. return NULL;
  91. }
  92. EXPORT_SYMBOL_GPL(inet6_csk_search_req);
  93. void inet6_csk_reqsk_queue_hash_add(struct sock *sk,
  94. struct request_sock *req,
  95. const unsigned long timeout)
  96. {
  97. struct inet_connection_sock *icsk = inet_csk(sk);
  98. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  99. const u32 h = inet6_synq_hash(&inet6_rsk(req)->rmt_addr,
  100. inet_rsk(req)->rmt_port,
  101. lopt->hash_rnd, lopt->nr_table_entries);
  102. reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
  103. inet_csk_reqsk_queue_added(sk, timeout);
  104. }
  105. EXPORT_SYMBOL_GPL(inet6_csk_reqsk_queue_hash_add);
  106. void inet6_csk_addr2sockaddr(struct sock *sk, struct sockaddr * uaddr)
  107. {
  108. struct ipv6_pinfo *np = inet6_sk(sk);
  109. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) uaddr;
  110. sin6->sin6_family = AF_INET6;
  111. ipv6_addr_copy(&sin6->sin6_addr, &np->daddr);
  112. sin6->sin6_port = inet_sk(sk)->dport;
  113. /* We do not store received flowlabel for TCP */
  114. sin6->sin6_flowinfo = 0;
  115. sin6->sin6_scope_id = 0;
  116. if (sk->sk_bound_dev_if &&
  117. ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  118. sin6->sin6_scope_id = sk->sk_bound_dev_if;
  119. }
  120. EXPORT_SYMBOL_GPL(inet6_csk_addr2sockaddr);
  121. int inet6_csk_xmit(struct sk_buff *skb, int ipfragok)
  122. {
  123. struct sock *sk = skb->sk;
  124. struct inet_sock *inet = inet_sk(sk);
  125. struct ipv6_pinfo *np = inet6_sk(sk);
  126. struct flowi fl;
  127. struct dst_entry *dst;
  128. struct in6_addr *final_p = NULL, final;
  129. memset(&fl, 0, sizeof(fl));
  130. fl.proto = sk->sk_protocol;
  131. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  132. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  133. fl.fl6_flowlabel = np->flow_label;
  134. IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
  135. fl.oif = sk->sk_bound_dev_if;
  136. fl.fl_ip_sport = inet->sport;
  137. fl.fl_ip_dport = inet->dport;
  138. if (np->opt && np->opt->srcrt) {
  139. struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
  140. ipv6_addr_copy(&final, &fl.fl6_dst);
  141. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  142. final_p = &final;
  143. }
  144. dst = __sk_dst_check(sk, np->dst_cookie);
  145. if (dst == NULL) {
  146. int err = ip6_dst_lookup(sk, &dst, &fl);
  147. if (err) {
  148. sk->sk_err_soft = -err;
  149. return err;
  150. }
  151. if (final_p)
  152. ipv6_addr_copy(&fl.fl6_dst, final_p);
  153. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
  154. sk->sk_route_caps = 0;
  155. return err;
  156. }
  157. ip6_dst_store(sk, dst, NULL);
  158. sk->sk_route_caps = dst->dev->features &
  159. ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
  160. }
  161. skb->dst = dst_clone(dst);
  162. /* Restore final destination back after routing done */
  163. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  164. return ip6_xmit(sk, skb, &fl, np->opt, 0);
  165. }
  166. EXPORT_SYMBOL_GPL(inet6_csk_xmit);