xfrm_output.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 net *net, struct sock *sk, 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. /* Children define the path of the packet through the
  36. * Linux networking. Thus, destinations are stackable.
  37. */
  38. static struct dst_entry *skb_dst_pop(struct sk_buff *skb)
  39. {
  40. struct dst_entry *child = dst_clone(skb_dst(skb)->child);
  41. skb_dst_drop(skb);
  42. return child;
  43. }
  44. static int xfrm_output_one(struct sk_buff *skb, int err)
  45. {
  46. struct dst_entry *dst = skb_dst(skb);
  47. struct xfrm_state *x = dst->xfrm;
  48. struct net *net = xs_net(x);
  49. if (err <= 0)
  50. goto resume;
  51. do {
  52. err = xfrm_skb_check_space(skb);
  53. if (err) {
  54. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
  55. goto error_nolock;
  56. }
  57. err = x->outer_mode->output(x, skb);
  58. if (err) {
  59. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
  60. goto error_nolock;
  61. }
  62. spin_lock_bh(&x->lock);
  63. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  64. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID);
  65. err = -EINVAL;
  66. goto error;
  67. }
  68. err = xfrm_state_check_expire(x);
  69. if (err) {
  70. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
  71. goto error;
  72. }
  73. err = x->repl->overflow(x, skb);
  74. if (err) {
  75. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
  76. goto error;
  77. }
  78. x->curlft.bytes += skb->len;
  79. x->curlft.packets++;
  80. spin_unlock_bh(&x->lock);
  81. skb_dst_force(skb);
  82. /* Inner headers are invalid now. */
  83. skb->encapsulation = 0;
  84. err = x->type->output(x, skb);
  85. if (err == -EINPROGRESS)
  86. goto out;
  87. resume:
  88. if (err) {
  89. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
  90. goto error_nolock;
  91. }
  92. dst = skb_dst_pop(skb);
  93. if (!dst) {
  94. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
  95. err = -EHOSTUNREACH;
  96. goto error_nolock;
  97. }
  98. skb_dst_set(skb, dst);
  99. x = dst->xfrm;
  100. } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
  101. return 0;
  102. error:
  103. spin_unlock_bh(&x->lock);
  104. error_nolock:
  105. kfree_skb(skb);
  106. out:
  107. return err;
  108. }
  109. int xfrm_output_resume(struct sk_buff *skb, int err)
  110. {
  111. struct net *net = xs_net(skb_dst(skb)->xfrm);
  112. while (likely((err = xfrm_output_one(skb, err)) == 0)) {
  113. nf_reset(skb);
  114. err = skb_dst(skb)->ops->local_out(net, skb->sk, skb);
  115. if (unlikely(err != 1))
  116. goto out;
  117. if (!skb_dst(skb)->xfrm)
  118. return dst_output(net, skb->sk, skb);
  119. err = nf_hook(skb_dst(skb)->ops->family,
  120. NF_INET_POST_ROUTING, net, skb->sk, skb,
  121. NULL, skb_dst(skb)->dev, xfrm_output2);
  122. if (unlikely(err != 1))
  123. goto out;
  124. }
  125. if (err == -EINPROGRESS)
  126. err = 0;
  127. out:
  128. return err;
  129. }
  130. EXPORT_SYMBOL_GPL(xfrm_output_resume);
  131. static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
  132. {
  133. return xfrm_output_resume(skb, 1);
  134. }
  135. static int xfrm_output_gso(struct net *net, struct sock *sk, struct sk_buff *skb)
  136. {
  137. struct sk_buff *segs;
  138. BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_SGO_CB_OFFSET);
  139. BUILD_BUG_ON(sizeof(*IP6CB(skb)) > SKB_SGO_CB_OFFSET);
  140. segs = skb_gso_segment(skb, 0);
  141. kfree_skb(skb);
  142. if (IS_ERR(segs))
  143. return PTR_ERR(segs);
  144. if (segs == NULL)
  145. return -EINVAL;
  146. do {
  147. struct sk_buff *nskb = segs->next;
  148. int err;
  149. segs->next = NULL;
  150. err = xfrm_output2(net, sk, segs);
  151. if (unlikely(err)) {
  152. kfree_skb_list(nskb);
  153. return err;
  154. }
  155. segs = nskb;
  156. } while (segs);
  157. return 0;
  158. }
  159. int xfrm_output(struct sock *sk, struct sk_buff *skb)
  160. {
  161. struct net *net = dev_net(skb_dst(skb)->dev);
  162. int err;
  163. if (skb_is_gso(skb))
  164. return xfrm_output_gso(net, sk, skb);
  165. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  166. err = skb_checksum_help(skb);
  167. if (err) {
  168. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
  169. kfree_skb(skb);
  170. return err;
  171. }
  172. }
  173. return xfrm_output2(net, sk, skb);
  174. }
  175. EXPORT_SYMBOL_GPL(xfrm_output);
  176. int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
  177. {
  178. struct xfrm_mode *inner_mode;
  179. if (x->sel.family == AF_UNSPEC)
  180. inner_mode = xfrm_ip2inner_mode(x,
  181. xfrm_af2proto(skb_dst(skb)->ops->family));
  182. else
  183. inner_mode = x->inner_mode;
  184. if (inner_mode == NULL)
  185. return -EAFNOSUPPORT;
  186. return inner_mode->afinfo->extract_output(x, skb);
  187. }
  188. EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);
  189. void xfrm_local_error(struct sk_buff *skb, int mtu)
  190. {
  191. unsigned int proto;
  192. struct xfrm_state_afinfo *afinfo;
  193. if (skb->protocol == htons(ETH_P_IP))
  194. proto = AF_INET;
  195. else if (skb->protocol == htons(ETH_P_IPV6))
  196. proto = AF_INET6;
  197. else
  198. return;
  199. afinfo = xfrm_state_get_afinfo(proto);
  200. if (!afinfo)
  201. return;
  202. afinfo->local_error(skb, mtu);
  203. xfrm_state_put_afinfo(afinfo);
  204. }
  205. EXPORT_SYMBOL_GPL(xfrm_local_error);