xfrm_output.c 6.0 KB

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