xfrm_output.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <net/dst.h>
  19. #include <net/xfrm.h>
  20. static int xfrm_output2(struct sk_buff *skb);
  21. static int xfrm_skb_check_space(struct sk_buff *skb)
  22. {
  23. struct dst_entry *dst = skb_dst(skb);
  24. int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
  25. - skb_headroom(skb);
  26. int ntail = dst->dev->needed_tailroom - skb_tailroom(skb);
  27. if (nhead <= 0) {
  28. if (ntail <= 0)
  29. return 0;
  30. nhead = 0;
  31. } else if (ntail < 0)
  32. ntail = 0;
  33. return pskb_expand_head(skb, nhead, ntail, GFP_ATOMIC);
  34. }
  35. static int xfrm_output_one(struct sk_buff *skb, int err)
  36. {
  37. struct dst_entry *dst = skb_dst(skb);
  38. struct xfrm_state *x = dst->xfrm;
  39. struct net *net = xs_net(x);
  40. if (err <= 0)
  41. goto resume;
  42. do {
  43. err = xfrm_skb_check_space(skb);
  44. if (err) {
  45. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
  46. goto error_nolock;
  47. }
  48. err = x->outer_mode->output(x, skb);
  49. if (err) {
  50. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
  51. goto error_nolock;
  52. }
  53. spin_lock_bh(&x->lock);
  54. err = xfrm_state_check_expire(x);
  55. if (err) {
  56. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
  57. goto error;
  58. }
  59. err = x->repl->overflow(x, skb);
  60. if (err) {
  61. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
  62. goto error;
  63. }
  64. x->curlft.bytes += skb->len;
  65. x->curlft.packets++;
  66. spin_unlock_bh(&x->lock);
  67. skb_dst_force(skb);
  68. err = x->type->output(x, skb);
  69. if (err == -EINPROGRESS)
  70. goto out_exit;
  71. resume:
  72. if (err) {
  73. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
  74. goto error_nolock;
  75. }
  76. dst = skb_dst_pop(skb);
  77. if (!dst) {
  78. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
  79. err = -EHOSTUNREACH;
  80. goto error_nolock;
  81. }
  82. skb_dst_set(skb, dst);
  83. x = dst->xfrm;
  84. } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
  85. err = 0;
  86. out_exit:
  87. return err;
  88. error:
  89. spin_unlock_bh(&x->lock);
  90. error_nolock:
  91. kfree_skb(skb);
  92. goto out_exit;
  93. }
  94. int xfrm_output_resume(struct sk_buff *skb, int err)
  95. {
  96. while (likely((err = xfrm_output_one(skb, err)) == 0)) {
  97. nf_reset(skb);
  98. err = skb_dst(skb)->ops->local_out(skb);
  99. if (unlikely(err != 1))
  100. goto out;
  101. if (!skb_dst(skb)->xfrm)
  102. return dst_output(skb);
  103. err = nf_hook(skb_dst(skb)->ops->family,
  104. NF_INET_POST_ROUTING, skb,
  105. NULL, skb_dst(skb)->dev, xfrm_output2);
  106. if (unlikely(err != 1))
  107. goto out;
  108. }
  109. if (err == -EINPROGRESS)
  110. err = 0;
  111. out:
  112. return err;
  113. }
  114. EXPORT_SYMBOL_GPL(xfrm_output_resume);
  115. static int xfrm_output2(struct sk_buff *skb)
  116. {
  117. return xfrm_output_resume(skb, 1);
  118. }
  119. static int xfrm_output_gso(struct sk_buff *skb)
  120. {
  121. struct sk_buff *segs;
  122. segs = skb_gso_segment(skb, 0);
  123. kfree_skb(skb);
  124. if (IS_ERR(segs))
  125. return PTR_ERR(segs);
  126. if (segs == NULL)
  127. return -EINVAL;
  128. do {
  129. struct sk_buff *nskb = segs->next;
  130. int err;
  131. segs->next = NULL;
  132. err = xfrm_output2(segs);
  133. if (unlikely(err)) {
  134. while ((segs = nskb)) {
  135. nskb = segs->next;
  136. segs->next = NULL;
  137. kfree_skb(segs);
  138. }
  139. return err;
  140. }
  141. segs = nskb;
  142. } while (segs);
  143. return 0;
  144. }
  145. int xfrm_output(struct sk_buff *skb)
  146. {
  147. struct net *net = dev_net(skb_dst(skb)->dev);
  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(net, 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(skb)->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);