xfrm6_output.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_tunnel_check_size(struct sk_buff *skb)
  28. {
  29. int mtu, ret = 0;
  30. struct dst_entry *dst = skb_dst(skb);
  31. mtu = dst_mtu(dst);
  32. if (mtu < IPV6_MIN_MTU)
  33. mtu = IPV6_MIN_MTU;
  34. if (!skb->local_df && skb->len > mtu) {
  35. skb->dev = dst->dev;
  36. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  37. ret = -EMSGSIZE;
  38. }
  39. return ret;
  40. }
  41. int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
  42. {
  43. int err;
  44. err = xfrm6_tunnel_check_size(skb);
  45. if (err)
  46. return err;
  47. XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
  48. return xfrm6_extract_header(skb);
  49. }
  50. int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
  51. {
  52. int err;
  53. err = xfrm_inner_extract_output(x, skb);
  54. if (err)
  55. return err;
  56. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  57. #ifdef CONFIG_NETFILTER
  58. IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
  59. #endif
  60. skb->protocol = htons(ETH_P_IPV6);
  61. skb->local_df = 1;
  62. return x->outer_mode->output2(x, skb);
  63. }
  64. EXPORT_SYMBOL(xfrm6_prepare_output);
  65. int xfrm6_output_finish(struct sk_buff *skb)
  66. {
  67. #ifdef CONFIG_NETFILTER
  68. IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
  69. #endif
  70. skb->protocol = htons(ETH_P_IPV6);
  71. return xfrm_output(skb);
  72. }
  73. static int __xfrm6_output(struct sk_buff *skb)
  74. {
  75. struct dst_entry *dst = skb_dst(skb);
  76. struct xfrm_state *x = dst->xfrm;
  77. if ((x && x->props.mode == XFRM_MODE_TUNNEL) &&
  78. ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
  79. dst_allfrag(skb_dst(skb)))) {
  80. return ip6_fragment(skb, x->outer_mode->afinfo->output_finish);
  81. }
  82. return x->outer_mode->afinfo->output_finish(skb);
  83. }
  84. int xfrm6_output(struct sk_buff *skb)
  85. {
  86. return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL,
  87. skb_dst(skb)->dev, __xfrm6_output);
  88. }