xfrm6_output.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. proto = sk->sk_protocol;
  33. if (proto == IPPROTO_UDP || proto == IPPROTO_RAW)
  34. return inet6_sk(sk)->dontfrag;
  35. }
  36. return 0;
  37. }
  38. static void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu)
  39. {
  40. struct flowi6 fl6;
  41. struct sock *sk = skb->sk;
  42. fl6.flowi6_oif = sk->sk_bound_dev_if;
  43. fl6.daddr = ipv6_hdr(skb)->daddr;
  44. ipv6_local_rxpmtu(sk, &fl6, mtu);
  45. }
  46. static void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
  47. {
  48. struct flowi6 fl6;
  49. struct sock *sk = skb->sk;
  50. fl6.fl6_dport = inet_sk(sk)->inet_dport;
  51. fl6.daddr = ipv6_hdr(skb)->daddr;
  52. ipv6_local_error(sk, EMSGSIZE, &fl6, mtu);
  53. }
  54. static int xfrm6_tunnel_check_size(struct sk_buff *skb)
  55. {
  56. int mtu, ret = 0;
  57. struct dst_entry *dst = skb_dst(skb);
  58. mtu = dst_mtu(dst);
  59. if (mtu < IPV6_MIN_MTU)
  60. mtu = IPV6_MIN_MTU;
  61. if (!skb->local_df && skb->len > mtu) {
  62. skb->dev = dst->dev;
  63. if (xfrm6_local_dontfrag(skb))
  64. xfrm6_local_rxpmtu(skb, mtu);
  65. else if (skb->sk)
  66. xfrm6_local_error(skb, mtu);
  67. else
  68. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  69. ret = -EMSGSIZE;
  70. }
  71. return ret;
  72. }
  73. int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
  74. {
  75. int err;
  76. err = xfrm6_tunnel_check_size(skb);
  77. if (err)
  78. return err;
  79. XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
  80. return xfrm6_extract_header(skb);
  81. }
  82. int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
  83. {
  84. int err;
  85. err = xfrm_inner_extract_output(x, skb);
  86. if (err)
  87. return err;
  88. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  89. #ifdef CONFIG_NETFILTER
  90. IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
  91. #endif
  92. skb->protocol = htons(ETH_P_IPV6);
  93. skb->local_df = 1;
  94. return x->outer_mode->output2(x, skb);
  95. }
  96. EXPORT_SYMBOL(xfrm6_prepare_output);
  97. int xfrm6_output_finish(struct sk_buff *skb)
  98. {
  99. #ifdef CONFIG_NETFILTER
  100. IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
  101. #endif
  102. skb->protocol = htons(ETH_P_IPV6);
  103. return xfrm_output(skb);
  104. }
  105. static int __xfrm6_output(struct sk_buff *skb)
  106. {
  107. struct dst_entry *dst = skb_dst(skb);
  108. struct xfrm_state *x = dst->xfrm;
  109. int mtu = ip6_skb_dst_mtu(skb);
  110. bool toobig;
  111. if (x->props.mode != XFRM_MODE_TUNNEL)
  112. goto skip_frag;
  113. toobig = skb->len > mtu && !skb_is_gso(skb);
  114. if (toobig && xfrm6_local_dontfrag(skb)) {
  115. xfrm6_local_rxpmtu(skb, mtu);
  116. return -EMSGSIZE;
  117. } else if (!skb->local_df && toobig && skb->sk) {
  118. xfrm6_local_error(skb, mtu);
  119. return -EMSGSIZE;
  120. }
  121. if (toobig || dst_allfrag(skb_dst(skb)))
  122. return ip6_fragment(skb, x->outer_mode->afinfo->output_finish);
  123. skip_frag:
  124. return x->outer_mode->afinfo->output_finish(skb);
  125. }
  126. int xfrm6_output(struct sk_buff *skb)
  127. {
  128. return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL,
  129. skb_dst(skb)->dev, __xfrm6_output);
  130. }