blob: bcfda8921b5bf944d38ac3e087eb1cccfac327fe [file] [log] [blame]
Herbert Xu406ef772007-10-08 17:16:30 -07001/*
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
12#include <linux/errno.h>
13#include <linux/module.h>
14#include <linux/netdevice.h>
Herbert Xu862b82c2007-11-13 21:43:11 -080015#include <linux/netfilter.h>
Herbert Xu406ef772007-10-08 17:16:30 -070016#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Herbert Xu406ef772007-10-08 17:16:30 -070018#include <linux/spinlock.h>
Herbert Xu406ef772007-10-08 17:16:30 -070019#include <net/dst.h>
20#include <net/xfrm.h>
21
Herbert Xuc6581a42007-11-13 21:43:43 -080022static int xfrm_output2(struct sk_buff *skb);
23
Steffen Klassert26b20722012-03-21 23:32:39 +000024static int xfrm_skb_check_space(struct sk_buff *skb)
Herbert Xu83815de2007-10-08 17:25:08 -070025{
Eric Dumazetadf30902009-06-02 05:19:30 +000026 struct dst_entry *dst = skb_dst(skb);
Herbert Xu550ade82007-11-13 21:33:01 -080027 int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
Herbert Xu83815de2007-10-08 17:25:08 -070028 - skb_headroom(skb);
Johannes Bergf5184d22008-05-12 20:48:31 -070029 int ntail = dst->dev->needed_tailroom - skb_tailroom(skb);
Herbert Xu83815de2007-10-08 17:25:08 -070030
Herbert Xud01dbeb2008-09-30 02:03:19 -070031 if (nhead <= 0) {
32 if (ntail <= 0)
33 return 0;
34 nhead = 0;
35 } else if (ntail < 0)
36 ntail = 0;
Herbert Xu83815de2007-10-08 17:25:08 -070037
Herbert Xud01dbeb2008-09-30 02:03:19 -070038 return pskb_expand_head(skb, nhead, ntail, GFP_ATOMIC);
Herbert Xu83815de2007-10-08 17:25:08 -070039}
40
Herbert Xuc6581a42007-11-13 21:43:43 -080041static int xfrm_output_one(struct sk_buff *skb, int err)
Herbert Xu406ef772007-10-08 17:16:30 -070042{
Eric Dumazetadf30902009-06-02 05:19:30 +000043 struct dst_entry *dst = skb_dst(skb);
Herbert Xu406ef772007-10-08 17:16:30 -070044 struct xfrm_state *x = dst->xfrm;
Alexey Dobriyana6483b72008-11-25 17:38:20 -080045 struct net *net = xs_net(x);
Herbert Xu406ef772007-10-08 17:16:30 -070046
Herbert Xuc6581a42007-11-13 21:43:43 -080047 if (err <= 0)
48 goto resume;
Herbert Xu406ef772007-10-08 17:16:30 -070049
50 do {
Steffen Klassert26b20722012-03-21 23:32:39 +000051 err = xfrm_skb_check_space(skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080052 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080053 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu910ef702007-12-18 22:14:25 -080054 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080055 }
Herbert Xu910ef702007-12-18 22:14:25 -080056
Herbert Xua2deb6d22007-11-13 21:39:38 -080057 err = x->outer_mode->output(x, skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080058 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080059 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Herbert Xu910ef702007-12-18 22:14:25 -080060 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080061 }
Herbert Xua2deb6d22007-11-13 21:39:38 -080062
Herbert Xu406ef772007-10-08 17:16:30 -070063 spin_lock_bh(&x->lock);
Li RongQingbb65a9c2012-12-28 16:06:28 +080064
65 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
66 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID);
Li RongQingfa8599d2013-02-01 13:17:16 +080067 goto error;
Li RongQingbb65a9c2012-12-28 16:06:28 +080068 }
69
Herbert Xu910ef702007-12-18 22:14:25 -080070 err = xfrm_state_check_expire(x);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080071 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080072 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
Herbert Xu406ef772007-10-08 17:16:30 -070073 goto error;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080074 }
Herbert Xu406ef772007-10-08 17:16:30 -070075
Steffen Klassert9fdc4882011-03-08 00:08:32 +000076 err = x->repl->overflow(x, skb);
77 if (err) {
78 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
79 goto error;
Herbert Xu436a0a42007-10-08 17:25:53 -070080 }
81
Herbert Xu406ef772007-10-08 17:16:30 -070082 x->curlft.bytes += skb->len;
83 x->curlft.packets++;
84
Herbert Xu406ef772007-10-08 17:16:30 -070085 spin_unlock_bh(&x->lock);
86
Steffen Klassert3bc07322011-03-15 21:08:28 +000087 skb_dst_force(skb);
88
Herbert Xub7c65382007-10-09 13:33:35 -070089 err = x->type->output(x, skb);
Herbert Xufcb8c152007-12-30 21:09:38 -080090 if (err == -EINPROGRESS)
91 goto out_exit;
Herbert Xuc6581a42007-11-13 21:43:43 -080092
93resume:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080094 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080095 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c65382007-10-09 13:33:35 -070096 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080097 }
Herbert Xub7c65382007-10-09 13:33:35 -070098
Steffen Klassert8764ab22010-06-04 01:57:38 +000099 dst = skb_dst_pop(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000100 if (!dst) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800101 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -0700102 err = -EHOSTUNREACH;
103 goto error_nolock;
104 }
Steffen Klasserte4334302011-03-15 21:09:32 +0000105 skb_dst_set(skb, dst);
Herbert Xu406ef772007-10-08 17:16:30 -0700106 x = dst->xfrm;
Herbert Xu13996372007-10-17 21:35:51 -0700107 } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -0700108
109 err = 0;
110
Herbert Xu862b82c2007-11-13 21:43:11 -0800111out_exit:
Herbert Xu406ef772007-10-08 17:16:30 -0700112 return err;
113error:
114 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -0800115error_nolock:
116 kfree_skb(skb);
117 goto out_exit;
118}
119
Herbert Xuc6581a42007-11-13 21:43:43 -0800120int xfrm_output_resume(struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -0800121{
Herbert Xuc6581a42007-11-13 21:43:43 -0800122 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Herbert Xu862b82c2007-11-13 21:43:11 -0800123 nf_reset(skb);
124
Eric Dumazetadf30902009-06-02 05:19:30 +0000125 err = skb_dst(skb)->ops->local_out(skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800126 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800127 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800128
Eric Dumazetadf30902009-06-02 05:19:30 +0000129 if (!skb_dst(skb)->xfrm)
Herbert Xu862b82c2007-11-13 21:43:11 -0800130 return dst_output(skb);
131
Eric Dumazetadf30902009-06-02 05:19:30 +0000132 err = nf_hook(skb_dst(skb)->ops->family,
David S. Miller294b4ba2007-11-14 01:57:47 -0800133 NF_INET_POST_ROUTING, skb,
Eric Dumazetadf30902009-06-02 05:19:30 +0000134 NULL, skb_dst(skb)->dev, xfrm_output2);
Herbert Xu862b82c2007-11-13 21:43:11 -0800135 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800136 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800137 }
138
Herbert Xuc6581a42007-11-13 21:43:43 -0800139 if (err == -EINPROGRESS)
140 err = 0;
141
142out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800143 return err;
144}
Herbert Xuc6581a42007-11-13 21:43:43 -0800145EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800146
Herbert Xuc6581a42007-11-13 21:43:43 -0800147static int xfrm_output2(struct sk_buff *skb)
148{
149 return xfrm_output_resume(skb, 1);
150}
151
152static int xfrm_output_gso(struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800153{
154 struct sk_buff *segs;
155
Herbert Xu862b82c2007-11-13 21:43:11 -0800156 segs = skb_gso_segment(skb, 0);
157 kfree_skb(skb);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700158 if (IS_ERR(segs))
Herbert Xu862b82c2007-11-13 21:43:11 -0800159 return PTR_ERR(segs);
160
161 do {
162 struct sk_buff *nskb = segs->next;
163 int err;
164
165 segs->next = NULL;
166 err = xfrm_output2(segs);
167
168 if (unlikely(err)) {
169 while ((segs = nskb)) {
170 nskb = segs->next;
171 segs->next = NULL;
172 kfree_skb(segs);
173 }
174 return err;
175 }
176
177 segs = nskb;
178 } while (segs);
179
180 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700181}
Herbert Xuc6581a42007-11-13 21:43:43 -0800182
183int xfrm_output(struct sk_buff *skb)
184{
Eric Dumazetadf30902009-06-02 05:19:30 +0000185 struct net *net = dev_net(skb_dst(skb)->dev);
Herbert Xuc6581a42007-11-13 21:43:43 -0800186 int err;
187
188 if (skb_is_gso(skb))
189 return xfrm_output_gso(skb);
190
191 if (skb->ip_summed == CHECKSUM_PARTIAL) {
192 err = skb_checksum_help(skb);
193 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800194 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800195 kfree_skb(skb);
196 return err;
197 }
198 }
199
200 return xfrm_output2(skb);
201}
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700202
203int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
204{
205 struct xfrm_mode *inner_mode;
206 if (x->sel.family == AF_UNSPEC)
207 inner_mode = xfrm_ip2inner_mode(x,
Eric Dumazetadf30902009-06-02 05:19:30 +0000208 xfrm_af2proto(skb_dst(skb)->ops->family));
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700209 else
210 inner_mode = x->inner_mode;
211
212 if (inner_mode == NULL)
213 return -EAFNOSUPPORT;
214 return inner_mode->afinfo->extract_output(x, skb);
215}
216
Herbert Xu406ef772007-10-08 17:16:30 -0700217EXPORT_SYMBOL_GPL(xfrm_output);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700218EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);