xfrm_output.c 5.4 KB

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