xfrm6_output.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. if (skb->ignore_df)
  63. goto out;
  64. mtu = dst_mtu(dst);
  65. if (mtu < IPV6_MIN_MTU)
  66. mtu = IPV6_MIN_MTU;
  67. if ((!skb_is_gso(skb) && skb->len > mtu) ||
  68. (skb_is_gso(skb) &&
  69. !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) {
  70. skb->dev = dst->dev;
  71. skb->protocol = htons(ETH_P_IPV6);
  72. if (xfrm6_local_dontfrag(skb))
  73. xfrm6_local_rxpmtu(skb, mtu);
  74. else if (skb->sk)
  75. xfrm_local_error(skb, mtu);
  76. else
  77. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  78. ret = -EMSGSIZE;
  79. }
  80. out:
  81. return ret;
  82. }
  83. int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
  84. {
  85. int err;
  86. err = xfrm6_tunnel_check_size(skb);
  87. if (err)
  88. return err;
  89. XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
  90. return xfrm6_extract_header(skb);
  91. }
  92. int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
  93. {
  94. int err;
  95. err = xfrm_inner_extract_output(x, skb);
  96. if (err)
  97. return err;
  98. skb->ignore_df = 1;
  99. skb->protocol = htons(ETH_P_IPV6);
  100. return x->outer_mode->output2(x, skb);
  101. }
  102. EXPORT_SYMBOL(xfrm6_prepare_output);
  103. int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb)
  104. {
  105. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  106. #ifdef CONFIG_NETFILTER
  107. IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
  108. #endif
  109. return xfrm_output(sk, skb);
  110. }
  111. static int __xfrm6_output_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  112. {
  113. struct xfrm_state *x = skb_dst(skb)->xfrm;
  114. return x->outer_mode->afinfo->output_finish(sk, skb);
  115. }
  116. static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  117. {
  118. struct dst_entry *dst = skb_dst(skb);
  119. struct xfrm_state *x = dst->xfrm;
  120. int mtu;
  121. bool toobig;
  122. #ifdef CONFIG_NETFILTER
  123. if (!x) {
  124. IP6CB(skb)->flags |= IP6SKB_REROUTED;
  125. return dst_output(net, sk, skb);
  126. }
  127. #endif
  128. if (x->props.mode != XFRM_MODE_TUNNEL)
  129. goto skip_frag;
  130. if (skb->protocol == htons(ETH_P_IPV6))
  131. mtu = ip6_skb_dst_mtu(skb);
  132. else
  133. mtu = dst_mtu(skb_dst(skb));
  134. toobig = skb->len > mtu && !skb_is_gso(skb);
  135. if (toobig && xfrm6_local_dontfrag(skb)) {
  136. xfrm6_local_rxpmtu(skb, mtu);
  137. kfree_skb(skb);
  138. return -EMSGSIZE;
  139. } else if (!skb->ignore_df && toobig && skb->sk) {
  140. xfrm_local_error(skb, mtu);
  141. kfree_skb(skb);
  142. return -EMSGSIZE;
  143. }
  144. if (toobig || dst_allfrag(skb_dst(skb)))
  145. return ip6_fragment(net, sk, skb,
  146. __xfrm6_output_finish);
  147. skip_frag:
  148. return x->outer_mode->afinfo->output_finish(sk, skb);
  149. }
  150. int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  151. {
  152. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  153. net, sk, skb, NULL, skb_dst(skb)->dev,
  154. __xfrm6_output,
  155. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  156. }