xfrm_output.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. int nhead = x->props.header_len + LL_RESERVED_SPACE(skb->dst->dev)
  21. - skb_headroom(skb);
  22. if (nhead > 0)
  23. return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
  24. /* Check tail too... */
  25. return 0;
  26. }
  27. static int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb)
  28. {
  29. int err = xfrm_state_check_expire(x);
  30. if (err < 0)
  31. goto err;
  32. err = xfrm_state_check_space(x, skb);
  33. err:
  34. return err;
  35. }
  36. int xfrm_output(struct sk_buff *skb)
  37. {
  38. struct dst_entry *dst = skb->dst;
  39. struct xfrm_state *x = dst->xfrm;
  40. int err;
  41. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  42. err = skb_checksum_help(skb);
  43. if (err)
  44. goto error_nolock;
  45. }
  46. do {
  47. spin_lock_bh(&x->lock);
  48. err = xfrm_state_check(x, skb);
  49. if (err)
  50. goto error;
  51. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  52. XFRM_SKB_CB(skb)->seq = ++x->replay.oseq;
  53. if (xfrm_aevent_is_on())
  54. xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
  55. }
  56. err = x->mode->output(x, skb);
  57. if (err)
  58. goto error;
  59. x->curlft.bytes += skb->len;
  60. x->curlft.packets++;
  61. spin_unlock_bh(&x->lock);
  62. err = x->type->output(x, skb);
  63. if (err)
  64. goto error_nolock;
  65. if (!(skb->dst = dst_pop(dst))) {
  66. err = -EHOSTUNREACH;
  67. goto error_nolock;
  68. }
  69. dst = skb->dst;
  70. x = dst->xfrm;
  71. } while (x && (x->props.mode != XFRM_MODE_TUNNEL));
  72. err = 0;
  73. error_nolock:
  74. return err;
  75. error:
  76. spin_unlock_bh(&x->lock);
  77. goto error_nolock;
  78. }
  79. EXPORT_SYMBOL_GPL(xfrm_output);