xfrm_output.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  55. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID);
  56. err = -EINVAL;
  57. goto error;
  58. }
  59. err = xfrm_state_check_expire(x);
  60. if (err) {
  61. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
  62. goto error;
  63. }
  64. err = x->repl->overflow(x, skb);
  65. if (err) {
  66. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
  67. goto error;
  68. }
  69. x->curlft.bytes += skb->len;
  70. x->curlft.packets++;
  71. spin_unlock_bh(&x->lock);
  72. skb_dst_force(skb);
  73. err = x->type->output(x, skb);
  74. if (err == -EINPROGRESS)
  75. goto out_exit;
  76. resume:
  77. if (err) {
  78. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
  79. goto error_nolock;
  80. }
  81. dst = skb_dst_pop(skb);
  82. if (!dst) {
  83. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
  84. err = -EHOSTUNREACH;
  85. goto error_nolock;
  86. }
  87. skb_dst_set(skb, dst);
  88. x = dst->xfrm;
  89. } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
  90. err = 0;
  91. out_exit:
  92. return err;
  93. error:
  94. spin_unlock_bh(&x->lock);
  95. error_nolock:
  96. kfree_skb(skb);
  97. goto out_exit;
  98. }
  99. int xfrm_output_resume(struct sk_buff *skb, int err)
  100. {
  101. while (likely((err = xfrm_output_one(skb, err)) == 0)) {
  102. nf_reset(skb);
  103. err = skb_dst(skb)->ops->local_out(skb);
  104. if (unlikely(err != 1))
  105. goto out;
  106. if (!skb_dst(skb)->xfrm)
  107. return dst_output(skb);
  108. err = nf_hook(skb_dst(skb)->ops->family,
  109. NF_INET_POST_ROUTING, skb,
  110. NULL, skb_dst(skb)->dev, xfrm_output2);
  111. if (unlikely(err != 1))
  112. goto out;
  113. }
  114. if (err == -EINPROGRESS)
  115. err = 0;
  116. out:
  117. return err;
  118. }
  119. EXPORT_SYMBOL_GPL(xfrm_output_resume);
  120. static int xfrm_output2(struct sk_buff *skb)
  121. {
  122. return xfrm_output_resume(skb, 1);
  123. }
  124. static int xfrm_output_gso(struct sk_buff *skb)
  125. {
  126. struct sk_buff *segs;
  127. segs = skb_gso_segment(skb, 0);
  128. kfree_skb(skb);
  129. if (IS_ERR(segs))
  130. return PTR_ERR(segs);
  131. if (segs == NULL)
  132. return -EINVAL;
  133. do {
  134. struct sk_buff *nskb = segs->next;
  135. int err;
  136. segs->next = NULL;
  137. err = xfrm_output2(segs);
  138. if (unlikely(err)) {
  139. while ((segs = nskb)) {
  140. nskb = segs->next;
  141. segs->next = NULL;
  142. kfree_skb(segs);
  143. }
  144. return err;
  145. }
  146. segs = nskb;
  147. } while (segs);
  148. return 0;
  149. }
  150. int xfrm_output(struct sk_buff *skb)
  151. {
  152. struct net *net = dev_net(skb_dst(skb)->dev);
  153. int err;
  154. if (skb_is_gso(skb))
  155. return xfrm_output_gso(skb);
  156. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  157. err = skb_checksum_help(skb);
  158. if (err) {
  159. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
  160. kfree_skb(skb);
  161. return err;
  162. }
  163. }
  164. return xfrm_output2(skb);
  165. }
  166. int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
  167. {
  168. struct xfrm_mode *inner_mode;
  169. if (x->sel.family == AF_UNSPEC)
  170. inner_mode = xfrm_ip2inner_mode(x,
  171. xfrm_af2proto(skb_dst(skb)->ops->family));
  172. else
  173. inner_mode = x->inner_mode;
  174. if (inner_mode == NULL)
  175. return -EAFNOSUPPORT;
  176. return inner_mode->afinfo->extract_output(x, skb);
  177. }
  178. EXPORT_SYMBOL_GPL(xfrm_output);
  179. EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);