blob: 3f964db908a71466e77949568d6ea672c63f281c [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>
17#include <linux/spinlock.h>
Herbert Xu406ef772007-10-08 17:16:30 -070018#include <net/dst.h>
19#include <net/xfrm.h>
20
Herbert Xuc6581a42007-11-13 21:43:43 -080021static int xfrm_output2(struct sk_buff *skb);
22
Herbert Xu83815de2007-10-08 17:25:08 -070023static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb)
24{
Herbert Xu550ade82007-11-13 21:33:01 -080025 struct dst_entry *dst = skb->dst;
26 int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
Herbert Xu83815de2007-10-08 17:25:08 -070027 - skb_headroom(skb);
Johannes Bergf5184d22008-05-12 20:48:31 -070028 int ntail = dst->dev->needed_tailroom - skb_tailroom(skb);
Herbert Xu83815de2007-10-08 17:25:08 -070029
Johannes Bergf5184d22008-05-12 20:48:31 -070030 if (nhead > 0 || ntail > 0)
31 return pskb_expand_head(skb, nhead, ntail, GFP_ATOMIC);
Herbert Xu83815de2007-10-08 17:25:08 -070032
Herbert Xu83815de2007-10-08 17:25:08 -070033 return 0;
34}
35
Herbert Xuc6581a42007-11-13 21:43:43 -080036static int xfrm_output_one(struct sk_buff *skb, int err)
Herbert Xu406ef772007-10-08 17:16:30 -070037{
38 struct dst_entry *dst = skb->dst;
39 struct xfrm_state *x = dst->xfrm;
Herbert Xu406ef772007-10-08 17:16:30 -070040
Herbert Xuc6581a42007-11-13 21:43:43 -080041 if (err <= 0)
42 goto resume;
Herbert Xu406ef772007-10-08 17:16:30 -070043
44 do {
Herbert Xu910ef702007-12-18 22:14:25 -080045 err = xfrm_state_check_space(x, skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080046 if (err) {
47 XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
Herbert Xu910ef702007-12-18 22:14:25 -080048 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080049 }
Herbert Xu910ef702007-12-18 22:14:25 -080050
Herbert Xua2deb6d22007-11-13 21:39:38 -080051 err = x->outer_mode->output(x, skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080052 if (err) {
53 XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEMODEERROR);
Herbert Xu910ef702007-12-18 22:14:25 -080054 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080055 }
Herbert Xua2deb6d22007-11-13 21:39:38 -080056
Herbert Xu406ef772007-10-08 17:16:30 -070057 spin_lock_bh(&x->lock);
Herbert Xu910ef702007-12-18 22:14:25 -080058 err = xfrm_state_check_expire(x);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080059 if (err) {
60 XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEEXPIRED);
Herbert Xu406ef772007-10-08 17:16:30 -070061 goto error;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080062 }
Herbert Xu406ef772007-10-08 17:16:30 -070063
Herbert Xu436a0a42007-10-08 17:25:53 -070064 if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
Herbert Xub318e0e2008-02-12 22:50:35 -080065 XFRM_SKB_CB(skb)->seq.output = ++x->replay.oseq;
Paul Mooree1af9f22007-12-21 14:59:08 -080066 if (unlikely(x->replay.oseq == 0)) {
Masahide NAKAMURA9472c9e2008-01-31 17:14:58 -080067 XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATESEQERROR);
Paul Mooree1af9f22007-12-21 14:59:08 -080068 x->replay.oseq--;
Paul Mooreafeb14b2007-12-21 14:58:11 -080069 xfrm_audit_state_replay_overflow(x, skb);
Herbert Xudbb1db82008-01-01 23:52:59 -080070 err = -EOVERFLOW;
Paul Mooree1af9f22007-12-21 14:59:08 -080071 goto error;
72 }
Herbert Xucdf7e662007-10-08 17:26:34 -070073 if (xfrm_aevent_is_on())
74 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
Herbert Xu436a0a42007-10-08 17:25:53 -070075 }
76
Herbert Xu406ef772007-10-08 17:16:30 -070077 x->curlft.bytes += skb->len;
78 x->curlft.packets++;
79
Herbert Xu406ef772007-10-08 17:16:30 -070080 spin_unlock_bh(&x->lock);
81
Herbert Xub7c65382007-10-09 13:33:35 -070082 err = x->type->output(x, skb);
Herbert Xufcb8c152007-12-30 21:09:38 -080083 if (err == -EINPROGRESS)
84 goto out_exit;
Herbert Xuc6581a42007-11-13 21:43:43 -080085
86resume:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080087 if (err) {
88 XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c65382007-10-09 13:33:35 -070089 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080090 }
Herbert Xub7c65382007-10-09 13:33:35 -070091
Herbert Xu406ef772007-10-08 17:16:30 -070092 if (!(skb->dst = dst_pop(dst))) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080093 XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -070094 err = -EHOSTUNREACH;
95 goto error_nolock;
96 }
97 dst = skb->dst;
98 x = dst->xfrm;
Herbert Xu13996372007-10-17 21:35:51 -070099 } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -0700100
101 err = 0;
102
Herbert Xu862b82c2007-11-13 21:43:11 -0800103out_exit:
Herbert Xu406ef772007-10-08 17:16:30 -0700104 return err;
105error:
106 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -0800107error_nolock:
108 kfree_skb(skb);
109 goto out_exit;
110}
111
Herbert Xuc6581a42007-11-13 21:43:43 -0800112int xfrm_output_resume(struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -0800113{
Herbert Xuc6581a42007-11-13 21:43:43 -0800114 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Herbert Xu862b82c2007-11-13 21:43:11 -0800115 struct xfrm_state *x;
116
117 nf_reset(skb);
118
119 err = skb->dst->ops->local_out(skb);
120 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800121 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800122
123 x = skb->dst->xfrm;
124 if (!x)
125 return dst_output(skb);
126
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700127 err = nf_hook(skb->dst->ops->family,
David S. Miller294b4ba2007-11-14 01:57:47 -0800128 NF_INET_POST_ROUTING, skb,
Herbert Xu862b82c2007-11-13 21:43:11 -0800129 NULL, skb->dst->dev, xfrm_output2);
130 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800131 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800132 }
133
Herbert Xuc6581a42007-11-13 21:43:43 -0800134 if (err == -EINPROGRESS)
135 err = 0;
136
137out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800138 return err;
139}
Herbert Xuc6581a42007-11-13 21:43:43 -0800140EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800141
Herbert Xuc6581a42007-11-13 21:43:43 -0800142static int xfrm_output2(struct sk_buff *skb)
143{
144 return xfrm_output_resume(skb, 1);
145}
146
147static int xfrm_output_gso(struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800148{
149 struct sk_buff *segs;
150
Herbert Xu862b82c2007-11-13 21:43:11 -0800151 segs = skb_gso_segment(skb, 0);
152 kfree_skb(skb);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700153 if (IS_ERR(segs))
Herbert Xu862b82c2007-11-13 21:43:11 -0800154 return PTR_ERR(segs);
155
156 do {
157 struct sk_buff *nskb = segs->next;
158 int err;
159
160 segs->next = NULL;
161 err = xfrm_output2(segs);
162
163 if (unlikely(err)) {
164 while ((segs = nskb)) {
165 nskb = segs->next;
166 segs->next = NULL;
167 kfree_skb(segs);
168 }
169 return err;
170 }
171
172 segs = nskb;
173 } while (segs);
174
175 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700176}
Herbert Xuc6581a42007-11-13 21:43:43 -0800177
178int xfrm_output(struct sk_buff *skb)
179{
180 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) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800188 XFRM_INC_STATS(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,
202 xfrm_af2proto(skb->dst->ops->family));
203 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);