xfrm_output.c 6.1 KB

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