blob: 6a329158bdfaa14de3891194e0c98ae70f4f1834 [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
Herbert Xu83815de2007-10-08 17:25:08 -070024static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb)
25{
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 {
Herbert Xu910ef702007-12-18 22:14:25 -080051 err = xfrm_state_check_space(x, 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);
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
Herbert Xu436a0a42007-10-08 17:25:53 -070070 if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
Herbert Xub318e0e2008-02-12 22:50:35 -080071 XFRM_SKB_CB(skb)->seq.output = ++x->replay.oseq;
Paul Mooree1af9f22007-12-21 14:59:08 -080072 if (unlikely(x->replay.oseq == 0)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080073 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
Paul Mooree1af9f22007-12-21 14:59:08 -080074 x->replay.oseq--;
Paul Mooreafeb14b2007-12-21 14:58:11 -080075 xfrm_audit_state_replay_overflow(x, skb);
Herbert Xudbb1db82008-01-01 23:52:59 -080076 err = -EOVERFLOW;
Paul Mooree1af9f22007-12-21 14:59:08 -080077 goto error;
78 }
Alexey Dobriyana6483b72008-11-25 17:38:20 -080079 if (xfrm_aevent_is_on(net))
Herbert Xucdf7e662007-10-08 17:26:34 -070080 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
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
Herbert Xub7c65382007-10-09 13:33:35 -070088 err = x->type->output(x, skb);
Herbert Xufcb8c152007-12-30 21:09:38 -080089 if (err == -EINPROGRESS)
90 goto out_exit;
Herbert Xuc6581a42007-11-13 21:43:43 -080091
92resume:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080093 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080094 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c65382007-10-09 13:33:35 -070095 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080096 }
Herbert Xub7c65382007-10-09 13:33:35 -070097
Eric Dumazetadf30902009-06-02 05:19:30 +000098 dst = dst_pop(dst);
99 if (!dst) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800100 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -0700101 err = -EHOSTUNREACH;
102 goto error_nolock;
103 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000104 skb_dst_set(skb, dst);
Herbert Xu406ef772007-10-08 17:16:30 -0700105 x = dst->xfrm;
Herbert Xu13996372007-10-17 21:35:51 -0700106 } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -0700107
108 err = 0;
109
Herbert Xu862b82c2007-11-13 21:43:11 -0800110out_exit:
Herbert Xu406ef772007-10-08 17:16:30 -0700111 return err;
112error:
113 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -0800114error_nolock:
115 kfree_skb(skb);
116 goto out_exit;
117}
118
Herbert Xuc6581a42007-11-13 21:43:43 -0800119int xfrm_output_resume(struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -0800120{
Herbert Xuc6581a42007-11-13 21:43:43 -0800121 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Herbert Xu862b82c2007-11-13 21:43:11 -0800122 nf_reset(skb);
123
Eric Dumazetadf30902009-06-02 05:19:30 +0000124 err = skb_dst(skb)->ops->local_out(skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800125 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800126 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800127
Eric Dumazetadf30902009-06-02 05:19:30 +0000128 if (!skb_dst(skb)->xfrm)
Herbert Xu862b82c2007-11-13 21:43:11 -0800129 return dst_output(skb);
130
Eric Dumazetadf30902009-06-02 05:19:30 +0000131 err = nf_hook(skb_dst(skb)->ops->family,
David S. Miller294b4ba2007-11-14 01:57:47 -0800132 NF_INET_POST_ROUTING, skb,
Eric Dumazetadf30902009-06-02 05:19:30 +0000133 NULL, skb_dst(skb)->dev, xfrm_output2);
Herbert Xu862b82c2007-11-13 21:43:11 -0800134 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800135 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800136 }
137
Herbert Xuc6581a42007-11-13 21:43:43 -0800138 if (err == -EINPROGRESS)
139 err = 0;
140
141out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800142 return err;
143}
Herbert Xuc6581a42007-11-13 21:43:43 -0800144EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800145
Herbert Xuc6581a42007-11-13 21:43:43 -0800146static int xfrm_output2(struct sk_buff *skb)
147{
148 return xfrm_output_resume(skb, 1);
149}
150
151static int xfrm_output_gso(struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800152{
153 struct sk_buff *segs;
154
Herbert Xu862b82c2007-11-13 21:43:11 -0800155 segs = skb_gso_segment(skb, 0);
156 kfree_skb(skb);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700157 if (IS_ERR(segs))
Herbert Xu862b82c2007-11-13 21:43:11 -0800158 return PTR_ERR(segs);
159
160 do {
161 struct sk_buff *nskb = segs->next;
162 int err;
163
164 segs->next = NULL;
165 err = xfrm_output2(segs);
166
167 if (unlikely(err)) {
168 while ((segs = nskb)) {
169 nskb = segs->next;
170 segs->next = NULL;
171 kfree_skb(segs);
172 }
173 return err;
174 }
175
176 segs = nskb;
177 } while (segs);
178
179 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700180}
Herbert Xuc6581a42007-11-13 21:43:43 -0800181
182int xfrm_output(struct sk_buff *skb)
183{
Eric Dumazetadf30902009-06-02 05:19:30 +0000184 struct net *net = dev_net(skb_dst(skb)->dev);
Herbert Xuc6581a42007-11-13 21:43:43 -0800185 int err;
186
187 if (skb_is_gso(skb))
188 return xfrm_output_gso(skb);
189
190 if (skb->ip_summed == CHECKSUM_PARTIAL) {
191 err = skb_checksum_help(skb);
192 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800193 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800194 kfree_skb(skb);
195 return err;
196 }
197 }
198
199 return xfrm_output2(skb);
200}
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}
215
Herbert Xu406ef772007-10-08 17:16:30 -0700216EXPORT_SYMBOL_GPL(xfrm_output);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700217EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);