xfrm_output.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. err = x->type->output(x, skb);
  83. if (err == -EINPROGRESS)
  84. goto out;
  85. resume:
  86. if (err) {
  87. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
  88. goto error_nolock;
  89. }
  90. dst = skb_dst_pop(skb);
  91. if (!dst) {
  92. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
  93. err = -EHOSTUNREACH;
  94. goto error_nolock;
  95. }
  96. skb_dst_set(skb, dst);
  97. x = dst->xfrm;
  98. } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
  99. return 0;
  100. error:
  101. spin_unlock_bh(&x->lock);
  102. error_nolock:
  103. kfree_skb(skb);
  104. out:
  105. return err;
  106. }
  107. int xfrm_output_resume(struct sk_buff *skb, int err)
  108. {
  109. struct net *net = xs_net(skb_dst(skb)->xfrm);
  110. while (likely((err = xfrm_output_one(skb, err)) == 0)) {
  111. nf_reset(skb);
  112. err = skb_dst(skb)->ops->local_out(net, skb->sk, skb);
  113. if (unlikely(err != 1))
  114. goto out;
  115. if (!skb_dst(skb)->xfrm)
  116. return dst_output(net, skb->sk, skb);
  117. err = nf_hook(skb_dst(skb)->ops->family,
  118. NF_INET_POST_ROUTING, net, skb->sk, skb,
  119. NULL, skb_dst(skb)->dev, xfrm_output2);
  120. if (unlikely(err != 1))
  121. goto out;
  122. }
  123. if (err == -EINPROGRESS)
  124. err = 0;
  125. out:
  126. return err;
  127. }
  128. EXPORT_SYMBOL_GPL(xfrm_output_resume);
  129. static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
  130. {
  131. return xfrm_output_resume(skb, 1);
  132. }
  133. static int xfrm_output_gso(struct net *net, struct sock *sk, struct sk_buff *skb)
  134. {
  135. struct sk_buff *segs;
  136. BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_SGO_CB_OFFSET);
  137. BUILD_BUG_ON(sizeof(*IP6CB(skb)) > SKB_SGO_CB_OFFSET);
  138. segs = skb_gso_segment(skb, 0);
  139. kfree_skb(skb);
  140. if (IS_ERR(segs))
  141. return PTR_ERR(segs);
  142. if (segs == NULL)
  143. return -EINVAL;
  144. do {
  145. struct sk_buff *nskb = segs->next;
  146. int err;
  147. segs->next = NULL;
  148. err = xfrm_output2(net, sk, segs);
  149. if (unlikely(err)) {
  150. kfree_skb_list(nskb);
  151. return err;
  152. }
  153. segs = nskb;
  154. } while (segs);
  155. return 0;
  156. }
  157. int xfrm_output(struct sock *sk, struct sk_buff *skb)
  158. {
  159. struct net *net = dev_net(skb_dst(skb)->dev);
  160. int err;
  161. if (skb_is_gso(skb))
  162. return xfrm_output_gso(net, sk, skb);
  163. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  164. err = skb_checksum_help(skb);
  165. if (err) {
  166. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
  167. kfree_skb(skb);
  168. return err;
  169. }
  170. }
  171. return xfrm_output2(net, sk, skb);
  172. }
  173. EXPORT_SYMBOL_GPL(xfrm_output);
  174. int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
  175. {
  176. struct xfrm_mode *inner_mode;
  177. if (x->sel.family == AF_UNSPEC)
  178. inner_mode = xfrm_ip2inner_mode(x,
  179. xfrm_af2proto(skb_dst(skb)->ops->family));
  180. else
  181. inner_mode = x->inner_mode;
  182. if (inner_mode == NULL)
  183. return -EAFNOSUPPORT;
  184. return inner_mode->afinfo->extract_output(x, skb);
  185. }
  186. EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);
  187. void xfrm_local_error(struct sk_buff *skb, int mtu)
  188. {
  189. unsigned int proto;
  190. struct xfrm_state_afinfo *afinfo;
  191. if (skb->protocol == htons(ETH_P_IP))
  192. proto = AF_INET;
  193. else if (skb->protocol == htons(ETH_P_IPV6))
  194. proto = AF_INET6;
  195. else
  196. return;
  197. afinfo = xfrm_state_get_afinfo(proto);
  198. if (!afinfo)
  199. return;
  200. afinfo->local_error(skb, mtu);
  201. xfrm_state_put_afinfo(afinfo);
  202. }
  203. EXPORT_SYMBOL_GPL(xfrm_local_error);