PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/net/ipv6/inet6_connection_sock.c

http://github.com/leemgs/samsung-s3c6410-android.1.0
C | 235 lines | 175 code | 40 blank | 20 comment | 25 complexity | f5d45bd9865eb91f9bcf19783adc904e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0, CC-BY-SA-3.0
  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. static inline
  121. void __inet6_csk_dst_store(struct sock *sk, struct dst_entry *dst,
  122. struct in6_addr *daddr, struct in6_addr *saddr)
  123. {
  124. __ip6_dst_store(sk, dst, daddr, saddr);
  125. #ifdef CONFIG_XFRM
  126. {
  127. struct rt6_info *rt = (struct rt6_info *)dst;
  128. rt->rt6i_flow_cache_genid = atomic_read(&flow_cache_genid);
  129. }
  130. #endif
  131. }
  132. static inline
  133. struct dst_entry *__inet6_csk_dst_check(struct sock *sk, u32 cookie)
  134. {
  135. struct dst_entry *dst;
  136. dst = __sk_dst_check(sk, cookie);
  137. #ifdef CONFIG_XFRM
  138. if (dst) {
  139. struct rt6_info *rt = (struct rt6_info *)dst;
  140. if (rt->rt6i_flow_cache_genid != atomic_read(&flow_cache_genid)) {
  141. sk->sk_dst_cache = NULL;
  142. dst_release(dst);
  143. dst = NULL;
  144. }
  145. }
  146. #endif
  147. return dst;
  148. }
  149. int inet6_csk_xmit(struct sk_buff *skb, int ipfragok)
  150. {
  151. struct sock *sk = skb->sk;
  152. struct inet_sock *inet = inet_sk(sk);
  153. struct ipv6_pinfo *np = inet6_sk(sk);
  154. struct flowi fl;
  155. struct dst_entry *dst;
  156. struct in6_addr *final_p = NULL, final;
  157. memset(&fl, 0, sizeof(fl));
  158. fl.proto = sk->sk_protocol;
  159. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  160. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  161. fl.fl6_flowlabel = np->flow_label;
  162. IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
  163. fl.oif = sk->sk_bound_dev_if;
  164. fl.fl_ip_sport = inet->sport;
  165. fl.fl_ip_dport = inet->dport;
  166. security_sk_classify_flow(sk, &fl);
  167. if (np->opt && np->opt->srcrt) {
  168. struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
  169. ipv6_addr_copy(&final, &fl.fl6_dst);
  170. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  171. final_p = &final;
  172. }
  173. dst = __inet6_csk_dst_check(sk, np->dst_cookie);
  174. if (dst == NULL) {
  175. int err = ip6_dst_lookup(sk, &dst, &fl);
  176. if (err) {
  177. sk->sk_err_soft = -err;
  178. kfree_skb(skb);
  179. return err;
  180. }
  181. if (final_p)
  182. ipv6_addr_copy(&fl.fl6_dst, final_p);
  183. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
  184. sk->sk_route_caps = 0;
  185. kfree_skb(skb);
  186. return err;
  187. }
  188. __inet6_csk_dst_store(sk, dst, NULL, NULL);
  189. }
  190. skb->dst = dst_clone(dst);
  191. /* Restore final destination back after routing done */
  192. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  193. return ip6_xmit(sk, skb, &fl, np->opt, 0);
  194. }
  195. EXPORT_SYMBOL_GPL(inet6_csk_xmit);