xfrm_output.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/netfilter.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/spinlock.h>
  17. #include <net/dst.h>
  18. #include <net/xfrm.h>
  19. static int xfrm_output2(struct sk_buff *skb);
  20. static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb)
  21. {
  22. struct dst_entry *dst = skb->dst;
  23. int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
  24. - skb_headroom(skb);
  25. int ntail = dst->dev->needed_tailroom - skb_tailroom(skb);
  26. if (nhead > 0 || ntail > 0)
  27. return pskb_expand_head(skb, nhead, ntail, GFP_ATOMIC);
  28. return 0;
  29. }
  30. static int xfrm_output_one(struct sk_buff *skb, int err)
  31. {
  32. struct dst_entry *dst = skb->dst;
  33. struct xfrm_state *x = dst->xfrm;
  34. if (err <= 0)
  35. goto resume;
  36. do {
  37. err = xfrm_state_check_space(x, skb);
  38. if (err) {
  39. XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
  40. goto error_nolock;
  41. }
  42. err = x->outer_mode->output(x, skb);
  43. if (err) {
  44. XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEMODEERROR);
  45. goto error_nolock;
  46. }
  47. spin_lock_bh(&x->lock);
  48. err = xfrm_state_check_expire(x);
  49. if (err) {
  50. XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEEXPIRED);
  51. goto error;
  52. }
  53. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  54. XFRM_SKB_CB(skb)->seq.output = ++x->replay.oseq;
  55. if (unlikely(x->replay.oseq == 0)) {
  56. XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATESEQERROR);
  57. x->replay.oseq--;
  58. xfrm_audit_state_replay_overflow(x, skb);
  59. err = -EOVERFLOW;
  60. goto error;
  61. }
  62. if (xfrm_aevent_is_on())
  63. xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
  64. }
  65. x->curlft.bytes += skb->len;
  66. x->curlft.packets++;
  67. spin_unlock_bh(&x->lock);
  68. err = x->type->output(x, skb);
  69. if (err == -EINPROGRESS)
  70. goto out_exit;
  71. resume:
  72. if (err) {
  73. XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEPROTOERROR);
  74. goto error_nolock;
  75. }
  76. if (!(skb->dst = dst_pop(dst))) {
  77. XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
  78. err = -EHOSTUNREACH;
  79. goto error_nolock;
  80. }
  81. dst = skb->dst;
  82. x = dst->xfrm;
  83. } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
  84. err = 0;
  85. out_exit:
  86. return err;
  87. error:
  88. spin_unlock_bh(&x->lock);
  89. error_nolock:
  90. kfree_skb(skb);
  91. goto out_exit;
  92. }
  93. int xfrm_output_resume(struct sk_buff *skb, int err)
  94. {
  95. while (likely((err = xfrm_output_one(skb, err)) == 0)) {
  96. struct xfrm_state *x;
  97. nf_reset(skb);
  98. err = skb->dst->ops->local_out(skb);
  99. if (unlikely(err != 1))
  100. goto out;
  101. x = skb->dst->xfrm;
  102. if (!x)
  103. return dst_output(skb);
  104. err = nf_hook(skb->dst->ops->family,
  105. NF_INET_POST_ROUTING, skb,
  106. NULL, skb->dst->dev, xfrm_output2);
  107. if (unlikely(err != 1))
  108. goto out;
  109. }
  110. if (err == -EINPROGRESS)
  111. err = 0;
  112. out:
  113. return err;
  114. }
  115. EXPORT_SYMBOL_GPL(xfrm_output_resume);
  116. static int xfrm_output2(struct sk_buff *skb)
  117. {
  118. return xfrm_output_resume(skb, 1);
  119. }
  120. static int xfrm_output_gso(struct sk_buff *skb)
  121. {
  122. struct sk_buff *segs;
  123. segs = skb_gso_segment(skb, 0);
  124. kfree_skb(skb);
  125. if (IS_ERR(segs))
  126. return PTR_ERR(segs);
  127. do {
  128. struct sk_buff *nskb = segs->next;
  129. int err;
  130. segs->next = NULL;
  131. err = xfrm_output2(segs);
  132. if (unlikely(err)) {
  133. while ((segs = nskb)) {
  134. nskb = segs->next;
  135. segs->next = NULL;
  136. kfree_skb(segs);
  137. }
  138. return err;
  139. }
  140. segs = nskb;
  141. } while (segs);
  142. return 0;
  143. }
  144. int xfrm_output(struct sk_buff *skb)
  145. {
  146. int err;
  147. if (skb_is_gso(skb))
  148. return xfrm_output_gso(skb);
  149. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  150. err = skb_checksum_help(skb);
  151. if (err) {
  152. XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
  153. kfree_skb(skb);
  154. return err;
  155. }
  156. }
  157. return xfrm_output2(skb);
  158. }
  159. int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
  160. {
  161. struct xfrm_mode *inner_mode;
  162. if (x->sel.family == AF_UNSPEC)
  163. inner_mode = xfrm_ip2inner_mode(x,
  164. xfrm_af2proto(skb->dst->ops->family));
  165. else
  166. inner_mode = x->inner_mode;
  167. if (inner_mode == NULL)
  168. return -EAFNOSUPPORT;
  169. return inner_mode->afinfo->extract_output(x, skb);
  170. }
  171. EXPORT_SYMBOL_GPL(xfrm_output);
  172. EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);