xfrm_output.c 4.3 KB

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