xfrm_output.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. nf_reset(skb);
  97. err = skb->dst->ops->local_out(skb);
  98. if (unlikely(err != 1))
  99. goto out;
  100. if (!skb->dst->xfrm)
  101. return dst_output(skb);
  102. err = nf_hook(skb->dst->ops->family,
  103. NF_INET_POST_ROUTING, skb,
  104. NULL, skb->dst->dev, xfrm_output2);
  105. if (unlikely(err != 1))
  106. goto out;
  107. }
  108. if (err == -EINPROGRESS)
  109. err = 0;
  110. out:
  111. return err;
  112. }
  113. EXPORT_SYMBOL_GPL(xfrm_output_resume);
  114. static int xfrm_output2(struct sk_buff *skb)
  115. {
  116. return xfrm_output_resume(skb, 1);
  117. }
  118. static int xfrm_output_gso(struct sk_buff *skb)
  119. {
  120. struct sk_buff *segs;
  121. segs = skb_gso_segment(skb, 0);
  122. kfree_skb(skb);
  123. if (IS_ERR(segs))
  124. return PTR_ERR(segs);
  125. do {
  126. struct sk_buff *nskb = segs->next;
  127. int err;
  128. segs->next = NULL;
  129. err = xfrm_output2(segs);
  130. if (unlikely(err)) {
  131. while ((segs = nskb)) {
  132. nskb = segs->next;
  133. segs->next = NULL;
  134. kfree_skb(segs);
  135. }
  136. return err;
  137. }
  138. segs = nskb;
  139. } while (segs);
  140. return 0;
  141. }
  142. int xfrm_output(struct sk_buff *skb)
  143. {
  144. int err;
  145. if (skb_is_gso(skb))
  146. return xfrm_output_gso(skb);
  147. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  148. err = skb_checksum_help(skb);
  149. if (err) {
  150. XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
  151. kfree_skb(skb);
  152. return err;
  153. }
  154. }
  155. return xfrm_output2(skb);
  156. }
  157. int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
  158. {
  159. struct xfrm_mode *inner_mode;
  160. if (x->sel.family == AF_UNSPEC)
  161. inner_mode = xfrm_ip2inner_mode(x,
  162. xfrm_af2proto(skb->dst->ops->family));
  163. else
  164. inner_mode = x->inner_mode;
  165. if (inner_mode == NULL)
  166. return -EAFNOSUPPORT;
  167. return inner_mode->afinfo->extract_output(x, skb);
  168. }
  169. EXPORT_SYMBOL_GPL(xfrm_output);
  170. EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);