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
12#include <linux/errno.h>
13#include <linux/module.h>
14#include <linux/netdevice.h>
15#include <linux/netfilter.h>
16#include <linux/skbuff.h>
17#include <linux/slab.h>
18#include <linux/spinlock.h>
19#include <net/dst.h>
20#include <net/xfrm.h>
21
22static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb);
23
24static int xfrm_skb_check_space(struct sk_buff *skb)
25{
26	struct dst_entry *dst = skb_dst(skb);
27	int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
28		- skb_headroom(skb);
29	int ntail = dst->dev->needed_tailroom - skb_tailroom(skb);
30
31	if (nhead <= 0) {
32		if (ntail <= 0)
33			return 0;
34		nhead = 0;
35	} else if (ntail < 0)
36		ntail = 0;
37
38	return pskb_expand_head(skb, nhead, ntail, GFP_ATOMIC);
39}
40
41/* Children define the path of the packet through the
42 * Linux networking.  Thus, destinations are stackable.
43 */
44
45static struct dst_entry *skb_dst_pop(struct sk_buff *skb)
46{
47	struct dst_entry *child = dst_clone(skb_dst(skb)->child);
48
49	skb_dst_drop(skb);
50	return child;
51}
52
53static int xfrm_output_one(struct sk_buff *skb, int err)
54{
55	struct dst_entry *dst = skb_dst(skb);
56	struct xfrm_state *x = dst->xfrm;
57	struct net *net = xs_net(x);
58
59	if (err <= 0)
60		goto resume;
61
62	do {
63		err = xfrm_skb_check_space(skb);
64		if (err) {
65			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
66			goto error_nolock;
67		}
68
69		err = x->outer_mode->output(x, skb);
70		if (err) {
71			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
72			goto error_nolock;
73		}
74
75		spin_lock_bh(&x->lock);
76
77		if (unlikely(x->km.state != XFRM_STATE_VALID)) {
78			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID);
79			err = -EINVAL;
80			goto error;
81		}
82
83		err = xfrm_state_check_expire(x);
84		if (err) {
85			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
86			goto error;
87		}
88
89		err = x->repl->overflow(x, skb);
90		if (err) {
91			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
92			goto error;
93		}
94
95		x->curlft.bytes += skb->len;
96		x->curlft.packets++;
97
98		spin_unlock_bh(&x->lock);
99
100		skb_dst_force(skb);
101
102		err = x->type->output(x, skb);
103		if (err == -EINPROGRESS)
104			goto out;
105
106resume:
107		if (err) {
108			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
109			goto error_nolock;
110		}
111
112		dst = skb_dst_pop(skb);
113		if (!dst) {
114			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
115			err = -EHOSTUNREACH;
116			goto error_nolock;
117		}
118		skb_dst_set(skb, dst);
119		x = dst->xfrm;
120	} while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
121
122	return 0;
123
124error:
125	spin_unlock_bh(&x->lock);
126error_nolock:
127	kfree_skb(skb);
128out:
129	return err;
130}
131
132int xfrm_output_resume(struct sk_buff *skb, int err)
133{
134	struct net *net = xs_net(skb_dst(skb)->xfrm);
135
136	while (likely((err = xfrm_output_one(skb, err)) == 0)) {
137		nf_reset(skb);
138
139		err = skb_dst(skb)->ops->local_out(net, skb->sk, skb);
140		if (unlikely(err != 1))
141			goto out;
142
143		if (!skb_dst(skb)->xfrm)
144			return dst_output(net, skb->sk, skb);
145
146		err = nf_hook(skb_dst(skb)->ops->family,
147			      NF_INET_POST_ROUTING, net, skb->sk, skb,
148			      NULL, skb_dst(skb)->dev, xfrm_output2);
149		if (unlikely(err != 1))
150			goto out;
151	}
152
153	if (err == -EINPROGRESS)
154		err = 0;
155
156out:
157	return err;
158}
159EXPORT_SYMBOL_GPL(xfrm_output_resume);
160
161static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
162{
163	return xfrm_output_resume(skb, 1);
164}
165
166static int xfrm_output_gso(struct net *net, struct sock *sk, struct sk_buff *skb)
167{
168	struct sk_buff *segs;
169
170	BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_SGO_CB_OFFSET);
171	BUILD_BUG_ON(sizeof(*IP6CB(skb)) > SKB_SGO_CB_OFFSET);
172	segs = skb_gso_segment(skb, 0);
173	kfree_skb(skb);
174	if (IS_ERR(segs))
175		return PTR_ERR(segs);
176	if (segs == NULL)
177		return -EINVAL;
178
179	do {
180		struct sk_buff *nskb = segs->next;
181		int err;
182
183		segs->next = NULL;
184		err = xfrm_output2(net, sk, segs);
185
186		if (unlikely(err)) {
187			kfree_skb_list(nskb);
188			return err;
189		}
190
191		segs = nskb;
192	} while (segs);
193
194	return 0;
195}
196
197int xfrm_output(struct sock *sk, struct sk_buff *skb)
198{
199	struct net *net = dev_net(skb_dst(skb)->dev);
200	int err;
201
202	if (skb_is_gso(skb))
203		return xfrm_output_gso(net, sk, skb);
204
205	if (skb->ip_summed == CHECKSUM_PARTIAL) {
206		err = skb_checksum_help(skb);
207		if (err) {
208			XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
209			kfree_skb(skb);
210			return err;
211		}
212	}
213
214	return xfrm_output2(net, sk, skb);
215}
216EXPORT_SYMBOL_GPL(xfrm_output);
217
218int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
219{
220	struct xfrm_mode *inner_mode;
221	if (x->sel.family == AF_UNSPEC)
222		inner_mode = xfrm_ip2inner_mode(x,
223				xfrm_af2proto(skb_dst(skb)->ops->family));
224	else
225		inner_mode = x->inner_mode;
226
227	if (inner_mode == NULL)
228		return -EAFNOSUPPORT;
229	return inner_mode->afinfo->extract_output(x, skb);
230}
231EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);
232
233void xfrm_local_error(struct sk_buff *skb, int mtu)
234{
235	unsigned int proto;
236	struct xfrm_state_afinfo *afinfo;
237
238	if (skb->protocol == htons(ETH_P_IP))
239		proto = AF_INET;
240	else if (skb->protocol == htons(ETH_P_IPV6))
241		proto = AF_INET6;
242	else
243		return;
244
245	afinfo = xfrm_state_get_afinfo(proto);
246	if (!afinfo)
247		return;
248
249	afinfo->local_error(skb, mtu);
250	xfrm_state_put_afinfo(afinfo);
251}
252EXPORT_SYMBOL_GPL(xfrm_local_error);
253