blob: 95a338c89f99d87af24ffa97faf17e1a7ef700e8 [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 Xua2deb6d2007-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 Xua2deb6d2007-11-13 21:39:38 -080062
Herbert Xu406ef772007-10-08 17:16:30 -070063 spin_lock_bh(&x->lock);
Herbert Xu910ef702007-12-18 22:14:25 -080064 err = xfrm_state_check_expire(x);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080065 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080066 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
Herbert Xu406ef772007-10-08 17:16:30 -070067 goto error;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080068 }
Herbert Xu406ef772007-10-08 17:16:30 -070069
Steffen Klassert9fdc4882011-03-08 00:08:32 +000070 err = x->repl->overflow(x, skb);
71 if (err) {
72 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
73 goto error;
Herbert Xu436a0a42007-10-08 17:25:53 -070074 }
75
Herbert Xu406ef772007-10-08 17:16:30 -070076 x->curlft.bytes += skb->len;
77 x->curlft.packets++;
78
Herbert Xu406ef772007-10-08 17:16:30 -070079 spin_unlock_bh(&x->lock);
80
Steffen Klassert3bc07322011-03-15 21:08:28 +000081 skb_dst_force(skb);
82
Herbert Xub7c65382007-10-09 13:33:35 -070083 err = x->type->output(x, skb);
Herbert Xufcb8c152007-12-30 21:09:38 -080084 if (err == -EINPROGRESS)
85 goto out_exit;
Herbert Xuc6581a42007-11-13 21:43:43 -080086
87resume:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080088 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080089 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c65382007-10-09 13:33:35 -070090 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080091 }
Herbert Xub7c65382007-10-09 13:33:35 -070092
Steffen Klassert8764ab22010-06-04 01:57:38 +000093 dst = skb_dst_pop(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +000094 if (!dst) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080095 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -070096 err = -EHOSTUNREACH;
97 goto error_nolock;
98 }
Steffen Klasserte4334302011-03-15 21:09:32 +000099 skb_dst_set(skb, dst);
Herbert Xu406ef772007-10-08 17:16:30 -0700100 x = dst->xfrm;
Herbert Xu13996372007-10-17 21:35:51 -0700101 } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -0700102
103 err = 0;
104
Herbert Xu862b82c2007-11-13 21:43:11 -0800105out_exit:
Herbert Xu406ef772007-10-08 17:16:30 -0700106 return err;
107error:
108 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -0800109error_nolock:
110 kfree_skb(skb);
111 goto out_exit;
112}
113
Herbert Xuc6581a42007-11-13 21:43:43 -0800114int xfrm_output_resume(struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -0800115{
Herbert Xuc6581a42007-11-13 21:43:43 -0800116 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Herbert Xu862b82c2007-11-13 21:43:11 -0800117 nf_reset(skb);
118
Eric Dumazetadf30902009-06-02 05:19:30 +0000119 err = skb_dst(skb)->ops->local_out(skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800120 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800121 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800122
Eric Dumazetadf30902009-06-02 05:19:30 +0000123 if (!skb_dst(skb)->xfrm)
Herbert Xu862b82c2007-11-13 21:43:11 -0800124 return dst_output(skb);
125
Eric Dumazetadf30902009-06-02 05:19:30 +0000126 err = nf_hook(skb_dst(skb)->ops->family,
David S. Miller294b4ba2007-11-14 01:57:47 -0800127 NF_INET_POST_ROUTING, skb,
Eric Dumazetadf30902009-06-02 05:19:30 +0000128 NULL, skb_dst(skb)->dev, xfrm_output2);
Herbert Xu862b82c2007-11-13 21:43:11 -0800129 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800130 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800131 }
132
Herbert Xuc6581a42007-11-13 21:43:43 -0800133 if (err == -EINPROGRESS)
134 err = 0;
135
136out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800137 return err;
138}
Herbert Xuc6581a42007-11-13 21:43:43 -0800139EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800140
Herbert Xuc6581a42007-11-13 21:43:43 -0800141static int xfrm_output2(struct sk_buff *skb)
142{
143 return xfrm_output_resume(skb, 1);
144}
145
146static int xfrm_output_gso(struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800147{
148 struct sk_buff *segs;
149
Herbert Xu862b82c2007-11-13 21:43:11 -0800150 segs = skb_gso_segment(skb, 0);
151 kfree_skb(skb);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700152 if (IS_ERR(segs))
Herbert Xu862b82c2007-11-13 21:43:11 -0800153 return PTR_ERR(segs);
154
155 do {
156 struct sk_buff *nskb = segs->next;
157 int err;
158
159 segs->next = NULL;
160 err = xfrm_output2(segs);
161
162 if (unlikely(err)) {
163 while ((segs = nskb)) {
164 nskb = segs->next;
165 segs->next = NULL;
166 kfree_skb(segs);
167 }
168 return err;
169 }
170
171 segs = nskb;
172 } while (segs);
173
174 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700175}
Herbert Xuc6581a42007-11-13 21:43:43 -0800176
177int xfrm_output(struct sk_buff *skb)
178{
Eric Dumazetadf30902009-06-02 05:19:30 +0000179 struct net *net = dev_net(skb_dst(skb)->dev);
Herbert Xuc6581a42007-11-13 21:43:43 -0800180 int err;
181
182 if (skb_is_gso(skb))
183 return xfrm_output_gso(skb);
184
185 if (skb->ip_summed == CHECKSUM_PARTIAL) {
186 err = skb_checksum_help(skb);
187 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800188 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800189 kfree_skb(skb);
190 return err;
191 }
192 }
193
194 return xfrm_output2(skb);
195}
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700196
197int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
198{
199 struct xfrm_mode *inner_mode;
200 if (x->sel.family == AF_UNSPEC)
201 inner_mode = xfrm_ip2inner_mode(x,
Eric Dumazetadf30902009-06-02 05:19:30 +0000202 xfrm_af2proto(skb_dst(skb)->ops->family));
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700203 else
204 inner_mode = x->inner_mode;
205
206 if (inner_mode == NULL)
207 return -EAFNOSUPPORT;
208 return inner_mode->afinfo->extract_output(x, skb);
209}
210
Herbert Xu406ef772007-10-08 17:16:30 -0700211EXPORT_SYMBOL_GPL(xfrm_output);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700212EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);