xfrm_output.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <linux/time.h>
  17. #include <net/dst.h>
  18. #include <net/xfrm.h>
  19. static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb)
  20. {
  21. int nhead = x->props.header_len + LL_RESERVED_SPACE(skb->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. spin_lock_bh(&x->lock);
  49. err = xfrm_state_check(x, skb);
  50. if (err)
  51. goto error;
  52. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  53. XFRM_SKB_CB(skb)->seq = ++x->replay.oseq;
  54. if (xfrm_aevent_is_on())
  55. xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
  56. }
  57. err = x->mode->output(x, 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. if (x->props.mode == XFRM_MODE_ROUTEOPTIMIZATION)
  66. x->lastused = get_seconds();
  67. spin_unlock_bh(&x->lock);
  68. skb_reset_network_header(skb);
  69. if (!(skb->dst = dst_pop(dst))) {
  70. err = -EHOSTUNREACH;
  71. goto error_nolock;
  72. }
  73. dst = skb->dst;
  74. x = dst->xfrm;
  75. } while (x && (x->props.mode != XFRM_MODE_TUNNEL));
  76. err = 0;
  77. error_nolock:
  78. return err;
  79. error:
  80. spin_unlock_bh(&x->lock);
  81. goto error_nolock;
  82. }
  83. EXPORT_SYMBOL_GPL(xfrm_output);