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