xfrm6_output.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * xfrm6_output.c - Common IPsec encapsulation code for IPv6.
  3. * Copyright (C) 2002 USAGI/WIDE Project
  4. * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/if_ether.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/icmpv6.h>
  16. #include <linux/netfilter_ipv6.h>
  17. #include <net/dst.h>
  18. #include <net/ipv6.h>
  19. #include <net/ip6_route.h>
  20. #include <net/xfrm.h>
  21. int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
  22. u8 **prevhdr)
  23. {
  24. return ip6_find_1stfragopt(skb, prevhdr);
  25. }
  26. EXPORT_SYMBOL(xfrm6_find_1stfragopt);
  27. static int xfrm6_local_dontfrag(struct sk_buff *skb)
  28. {
  29. int proto;
  30. struct sock *sk = skb->sk;
  31. if (sk) {
  32. if (sk->sk_family != AF_INET6)
  33. return 0;
  34. proto = sk->sk_protocol;
  35. if (proto == IPPROTO_UDP || proto == IPPROTO_RAW)
  36. return inet6_sk(sk)->dontfrag;
  37. }
  38. return 0;
  39. }
  40. static void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu)
  41. {
  42. struct flowi6 fl6;
  43. struct sock *sk = skb->sk;
  44. fl6.flowi6_oif = sk->sk_bound_dev_if;
  45. fl6.daddr = ipv6_hdr(skb)->daddr;
  46. ipv6_local_rxpmtu(sk, &fl6, mtu);
  47. }
  48. void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
  49. {
  50. struct flowi6 fl6;
  51. const struct ipv6hdr *hdr;
  52. struct sock *sk = skb->sk;
  53. hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
  54. fl6.fl6_dport = inet_sk(sk)->inet_dport;
  55. fl6.daddr = hdr->daddr;
  56. ipv6_local_error(sk, EMSGSIZE, &fl6, mtu);
  57. }
  58. static int xfrm6_tunnel_check_size(struct sk_buff *skb)
  59. {
  60. int mtu, ret = 0;
  61. struct dst_entry *dst = skb_dst(skb);
  62. mtu = dst_mtu(dst);
  63. if (mtu < IPV6_MIN_MTU)
  64. mtu = IPV6_MIN_MTU;
  65. if (!skb->ignore_df && skb->len > mtu) {
  66. skb->dev = dst->dev;
  67. skb->protocol = htons(ETH_P_IPV6);
  68. if (xfrm6_local_dontfrag(skb))
  69. xfrm6_local_rxpmtu(skb, mtu);
  70. else if (skb->sk)
  71. xfrm_local_error(skb, mtu);
  72. else
  73. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  74. ret = -EMSGSIZE;
  75. }
  76. return ret;
  77. }
  78. int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
  79. {
  80. int err;
  81. err = xfrm6_tunnel_check_size(skb);
  82. if (err)
  83. return err;
  84. XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
  85. return xfrm6_extract_header(skb);
  86. }
  87. int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
  88. {
  89. int err;
  90. err = xfrm_inner_extract_output(x, skb);
  91. if (err)
  92. return err;
  93. skb->ignore_df = 1;
  94. skb->protocol = htons(ETH_P_IPV6);
  95. return x->outer_mode->output2(x, skb);
  96. }
  97. EXPORT_SYMBOL(xfrm6_prepare_output);
  98. int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb)
  99. {
  100. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  101. #ifdef CONFIG_NETFILTER
  102. IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
  103. #endif
  104. return xfrm_output(sk, skb);
  105. }
  106. static int __xfrm6_output_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  107. {
  108. struct xfrm_state *x = skb_dst(skb)->xfrm;
  109. return x->outer_mode->afinfo->output_finish(sk, skb);
  110. }
  111. static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  112. {
  113. struct dst_entry *dst = skb_dst(skb);
  114. struct xfrm_state *x = dst->xfrm;
  115. int mtu;
  116. bool toobig;
  117. #ifdef CONFIG_NETFILTER
  118. if (!x) {
  119. IP6CB(skb)->flags |= IP6SKB_REROUTED;
  120. return dst_output(net, sk, skb);
  121. }
  122. #endif
  123. if (x->props.mode != XFRM_MODE_TUNNEL)
  124. goto skip_frag;
  125. if (skb->protocol == htons(ETH_P_IPV6))
  126. mtu = ip6_skb_dst_mtu(skb);
  127. else
  128. mtu = dst_mtu(skb_dst(skb));
  129. toobig = skb->len > mtu && !skb_is_gso(skb);
  130. if (toobig && xfrm6_local_dontfrag(skb)) {
  131. xfrm6_local_rxpmtu(skb, mtu);
  132. return -EMSGSIZE;
  133. } else if (!skb->ignore_df && toobig && skb->sk) {
  134. xfrm_local_error(skb, mtu);
  135. return -EMSGSIZE;
  136. }
  137. if (toobig || dst_allfrag(skb_dst(skb)))
  138. return ip6_fragment(net, sk, skb,
  139. __xfrm6_output_finish);
  140. skip_frag:
  141. return x->outer_mode->afinfo->output_finish(sk, skb);
  142. }
  143. int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  144. {
  145. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  146. net, sk, skb, NULL, skb_dst(skb)->dev,
  147. __xfrm6_output,
  148. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  149. }