xfrm_output.c 6.2 KB

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