xfrm6_output.c 3.2 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/compiler.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/icmpv6.h>
  15. #include <linux/netfilter_ipv6.h>
  16. #include <net/ipv6.h>
  17. #include <net/xfrm.h>
  18. int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
  19. u8 **prevhdr)
  20. {
  21. return ip6_find_1stfragopt(skb, prevhdr);
  22. }
  23. static int xfrm6_tunnel_check_size(struct sk_buff *skb)
  24. {
  25. int mtu, ret = 0;
  26. struct dst_entry *dst = skb->dst;
  27. mtu = dst_mtu(dst);
  28. if (mtu < IPV6_MIN_MTU)
  29. mtu = IPV6_MIN_MTU;
  30. if (skb->len > mtu) {
  31. skb->dev = dst->dev;
  32. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
  33. ret = -EMSGSIZE;
  34. }
  35. return ret;
  36. }
  37. static int xfrm6_output_one(struct sk_buff *skb)
  38. {
  39. struct dst_entry *dst = skb->dst;
  40. struct xfrm_state *x = dst->xfrm;
  41. int err;
  42. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  43. err = skb_checksum_help(skb);
  44. if (err)
  45. goto error_nolock;
  46. }
  47. if (x->props.mode == XFRM_MODE_TUNNEL) {
  48. err = xfrm6_tunnel_check_size(skb);
  49. if (err)
  50. goto error_nolock;
  51. }
  52. do {
  53. spin_lock_bh(&x->lock);
  54. err = xfrm_state_check(x, skb);
  55. if (err)
  56. goto error;
  57. err = x->mode->output(skb);
  58. if (err)
  59. goto error;
  60. err = x->type->output(x, skb);
  61. if (err)
  62. goto error;
  63. x->curlft.bytes += skb->len;
  64. x->curlft.packets++;
  65. spin_unlock_bh(&x->lock);
  66. skb->nh.raw = skb->data;
  67. if (!(skb->dst = dst_pop(dst))) {
  68. err = -EHOSTUNREACH;
  69. goto error_nolock;
  70. }
  71. dst = skb->dst;
  72. x = dst->xfrm;
  73. } while (x && (x->props.mode != XFRM_MODE_TUNNEL));
  74. IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
  75. err = 0;
  76. out_exit:
  77. return err;
  78. error:
  79. spin_unlock_bh(&x->lock);
  80. error_nolock:
  81. kfree_skb(skb);
  82. goto out_exit;
  83. }
  84. static int xfrm6_output_finish2(struct sk_buff *skb)
  85. {
  86. int err;
  87. while (likely((err = xfrm6_output_one(skb)) == 0)) {
  88. nf_reset(skb);
  89. err = nf_hook(PF_INET6, NF_IP6_LOCAL_OUT, &skb, NULL,
  90. skb->dst->dev, dst_output);
  91. if (unlikely(err != 1))
  92. break;
  93. if (!skb->dst->xfrm)
  94. return dst_output(skb);
  95. err = nf_hook(PF_INET6, NF_IP6_POST_ROUTING, &skb, NULL,
  96. skb->dst->dev, xfrm6_output_finish2);
  97. if (unlikely(err != 1))
  98. break;
  99. }
  100. return err;
  101. }
  102. static int xfrm6_output_finish(struct sk_buff *skb)
  103. {
  104. struct sk_buff *segs;
  105. if (!skb_is_gso(skb))
  106. return xfrm6_output_finish2(skb);
  107. skb->protocol = htons(ETH_P_IPV6);
  108. segs = skb_gso_segment(skb, 0);
  109. kfree_skb(skb);
  110. if (unlikely(IS_ERR(segs)))
  111. return PTR_ERR(segs);
  112. do {
  113. struct sk_buff *nskb = segs->next;
  114. int err;
  115. segs->next = NULL;
  116. err = xfrm6_output_finish2(segs);
  117. if (unlikely(err)) {
  118. while ((segs = nskb)) {
  119. nskb = segs->next;
  120. segs->next = NULL;
  121. kfree_skb(segs);
  122. }
  123. return err;
  124. }
  125. segs = nskb;
  126. } while (segs);
  127. return 0;
  128. }
  129. int xfrm6_output(struct sk_buff *skb)
  130. {
  131. return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dst->dev,
  132. xfrm6_output_finish);
  133. }