blob: fbcedbe33190346a40fc148369757a6ef64a2106 [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
David Miller7026b1d2015-04-05 22:19:04 -040022static int xfrm_output2(struct sock *sk, struct sk_buff *skb);
Herbert Xuc6581a42007-11-13 21:43:43 -080023
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);
Timo Teräs497574c2013-05-22 01:40:47 +000067 err = -EINVAL;
Li RongQingfa8599d2013-02-01 13:17:16 +080068 goto error;
Li RongQingbb65a9c2012-12-28 16:06:28 +080069 }
70
Herbert Xu910ef702007-12-18 22:14:25 -080071 err = xfrm_state_check_expire(x);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080072 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080073 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
Herbert Xu406ef772007-10-08 17:16:30 -070074 goto error;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080075 }
Herbert Xu406ef772007-10-08 17:16:30 -070076
Steffen Klassert9fdc4882011-03-08 00:08:32 +000077 err = x->repl->overflow(x, skb);
78 if (err) {
79 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
80 goto error;
Herbert Xu436a0a42007-10-08 17:25:53 -070081 }
82
Herbert Xu406ef772007-10-08 17:16:30 -070083 x->curlft.bytes += skb->len;
84 x->curlft.packets++;
85
Herbert Xu406ef772007-10-08 17:16:30 -070086 spin_unlock_bh(&x->lock);
87
Steffen Klassert3bc07322011-03-15 21:08:28 +000088 skb_dst_force(skb);
89
Herbert Xub7c6538c2007-10-09 13:33:35 -070090 err = x->type->output(x, skb);
Herbert Xufcb8c152007-12-30 21:09:38 -080091 if (err == -EINPROGRESS)
Jean Sacrenebd46872013-06-01 16:23:15 +000092 goto out;
Herbert Xuc6581a42007-11-13 21:43:43 -080093
94resume:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080095 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080096 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c6538c2007-10-09 13:33:35 -070097 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080098 }
Herbert Xub7c6538c2007-10-09 13:33:35 -070099
Steffen Klassert8764ab22010-06-04 01:57:38 +0000100 dst = skb_dst_pop(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000101 if (!dst) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800102 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -0700103 err = -EHOSTUNREACH;
104 goto error_nolock;
105 }
Steffen Klasserte4334302011-03-15 21:09:32 +0000106 skb_dst_set(skb, dst);
Herbert Xu406ef772007-10-08 17:16:30 -0700107 x = dst->xfrm;
Herbert Xu13996372007-10-17 21:35:51 -0700108 } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -0700109
Jean Sacrenebd46872013-06-01 16:23:15 +0000110 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700111
Herbert Xu406ef772007-10-08 17:16:30 -0700112error:
113 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -0800114error_nolock:
115 kfree_skb(skb);
Jean Sacrenebd46872013-06-01 16:23:15 +0000116out:
117 return err;
Herbert Xu862b82c2007-11-13 21:43:11 -0800118}
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 Miller7026b1d2015-04-05 22:19:04 -0400133 NF_INET_POST_ROUTING, skb->sk, 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
David Miller7026b1d2015-04-05 22:19:04 -0400147static int xfrm_output2(struct sock *sk, struct sk_buff *skb)
Herbert Xuc6581a42007-11-13 21:43:43 -0800148{
149 return xfrm_output_resume(skb, 1);
150}
151
David Miller7026b1d2015-04-05 22:19:04 -0400152static int xfrm_output_gso(struct sock *sk, 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);
Florian Westphal330966e2014-10-20 13:49:17 +0200160 if (segs == NULL)
161 return -EINVAL;
Herbert Xu862b82c2007-11-13 21:43:11 -0800162
163 do {
164 struct sk_buff *nskb = segs->next;
165 int err;
166
167 segs->next = NULL;
David Miller7026b1d2015-04-05 22:19:04 -0400168 err = xfrm_output2(sk, segs);
Herbert Xu862b82c2007-11-13 21:43:11 -0800169
170 if (unlikely(err)) {
Florian Westphal46cfd722014-09-10 01:08:46 +0200171 kfree_skb_list(nskb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800172 return err;
173 }
174
175 segs = nskb;
176 } while (segs);
177
178 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700179}
Herbert Xuc6581a42007-11-13 21:43:43 -0800180
David Miller7026b1d2015-04-05 22:19:04 -0400181int xfrm_output(struct sock *sk, struct sk_buff *skb)
Herbert Xuc6581a42007-11-13 21:43:43 -0800182{
Eric Dumazetadf30902009-06-02 05:19:30 +0000183 struct net *net = dev_net(skb_dst(skb)->dev);
Herbert Xuc6581a42007-11-13 21:43:43 -0800184 int err;
185
186 if (skb_is_gso(skb))
David Miller7026b1d2015-04-05 22:19:04 -0400187 return xfrm_output_gso(sk, skb);
Herbert Xuc6581a42007-11-13 21:43:43 -0800188
189 if (skb->ip_summed == CHECKSUM_PARTIAL) {
190 err = skb_checksum_help(skb);
191 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800192 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800193 kfree_skb(skb);
194 return err;
195 }
196 }
197
David Miller7026b1d2015-04-05 22:19:04 -0400198 return xfrm_output2(sk, skb);
Herbert Xuc6581a42007-11-13 21:43:43 -0800199}
Fabian Frederickfc680862014-05-12 19:54:34 +0200200EXPORT_SYMBOL_GPL(xfrm_output);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700201
202int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
203{
204 struct xfrm_mode *inner_mode;
205 if (x->sel.family == AF_UNSPEC)
206 inner_mode = xfrm_ip2inner_mode(x,
Eric Dumazetadf30902009-06-02 05:19:30 +0000207 xfrm_af2proto(skb_dst(skb)->ops->family));
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700208 else
209 inner_mode = x->inner_mode;
210
211 if (inner_mode == NULL)
212 return -EAFNOSUPPORT;
213 return inner_mode->afinfo->extract_output(x, skb);
214}
Fabian Frederickfc680862014-05-12 19:54:34 +0200215EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700216
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200217void xfrm_local_error(struct sk_buff *skb, int mtu)
218{
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200219 unsigned int proto;
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200220 struct xfrm_state_afinfo *afinfo;
221
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200222 if (skb->protocol == htons(ETH_P_IP))
223 proto = AF_INET;
224 else if (skb->protocol == htons(ETH_P_IPV6))
225 proto = AF_INET6;
226 else
227 return;
228
229 afinfo = xfrm_state_get_afinfo(proto);
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200230 if (!afinfo)
231 return;
232
233 afinfo->local_error(skb, mtu);
234 xfrm_state_put_afinfo(afinfo);
235}
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200236EXPORT_SYMBOL_GPL(xfrm_local_error);