xfrm_output.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * xfrm_output.c - Common IPsec encapsulation code.
  3. *
  4. * Copyright (c) 2007 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/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/spinlock.h>
  16. #include <net/dst.h>
  17. #include <net/xfrm.h>
  18. static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb)
  19. {
  20. struct dst_entry *dst = skb->dst;
  21. int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
  22. - skb_headroom(skb);
  23. if (nhead > 0)
  24. return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
  25. /* Check tail too... */
  26. return 0;
  27. }
  28. static int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb)
  29. {
  30. int err = xfrm_state_check_expire(x);
  31. if (err < 0)
  32. goto err;
  33. err = xfrm_state_check_space(x, skb);
  34. err:
  35. return err;
  36. }
  37. int xfrm_output(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. do {
  48. err = x->outer_mode->output(x, skb);
  49. if (err)
  50. goto error;
  51. spin_lock_bh(&x->lock);
  52. err = xfrm_state_check(x, skb);
  53. if (err)
  54. goto error;
  55. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  56. XFRM_SKB_CB(skb)->seq = ++x->replay.oseq;
  57. if (xfrm_aevent_is_on())
  58. xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
  59. }
  60. x->curlft.bytes += skb->len;
  61. x->curlft.packets++;
  62. spin_unlock_bh(&x->lock);
  63. err = x->type->output(x, skb);
  64. if (err)
  65. goto error_nolock;
  66. if (!(skb->dst = dst_pop(dst))) {
  67. err = -EHOSTUNREACH;
  68. goto error_nolock;
  69. }
  70. dst = skb->dst;
  71. x = dst->xfrm;
  72. } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
  73. err = 0;
  74. error_nolock:
  75. return err;
  76. error:
  77. spin_unlock_bh(&x->lock);
  78. goto error_nolock;
  79. }
  80. EXPORT_SYMBOL_GPL(xfrm_output);