blob: dc50f1e71f76f86487ebded9991cb20bc4e46ef9 [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
Herbert Xud01dbeb2008-09-30 02:03:19 -070030 if (nhead <= 0) {
31 if (ntail <= 0)
32 return 0;
33 nhead = 0;
34 } else if (ntail < 0)
35 ntail = 0;
Herbert Xu83815de2007-10-08 17:25:08 -070036
Herbert Xud01dbeb2008-09-30 02:03:19 -070037 return pskb_expand_head(skb, nhead, ntail, GFP_ATOMIC);
Herbert Xu83815de2007-10-08 17:25:08 -070038}
39
Herbert Xuc6581a42007-11-13 21:43:43 -080040static int xfrm_output_one(struct sk_buff *skb, int err)
Herbert Xu406ef772007-10-08 17:16:30 -070041{
42 struct dst_entry *dst = skb->dst;
43 struct xfrm_state *x = dst->xfrm;
Herbert Xu406ef772007-10-08 17:16:30 -070044
Herbert Xuc6581a42007-11-13 21:43:43 -080045 if (err <= 0)
46 goto resume;
Herbert Xu406ef772007-10-08 17:16:30 -070047
48 do {
Herbert Xu910ef702007-12-18 22:14:25 -080049 err = xfrm_state_check_space(x, skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080050 if (err) {
51 XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
Herbert Xu910ef702007-12-18 22:14:25 -080052 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080053 }
Herbert Xu910ef702007-12-18 22:14:25 -080054
Herbert Xua2deb6d22007-11-13 21:39:38 -080055 err = x->outer_mode->output(x, skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080056 if (err) {
57 XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEMODEERROR);
Herbert Xu910ef702007-12-18 22:14:25 -080058 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080059 }
Herbert Xua2deb6d22007-11-13 21:39:38 -080060
Herbert Xu406ef772007-10-08 17:16:30 -070061 spin_lock_bh(&x->lock);
Herbert Xu910ef702007-12-18 22:14:25 -080062 err = xfrm_state_check_expire(x);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080063 if (err) {
64 XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEEXPIRED);
Herbert Xu406ef772007-10-08 17:16:30 -070065 goto error;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -080066 }
Herbert Xu406ef772007-10-08 17:16:30 -070067
Herbert Xu436a0a42007-10-08 17:25:53 -070068 if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
Herbert Xub318e0e2008-02-12 22:50:35 -080069 XFRM_SKB_CB(skb)->seq.output = ++x->replay.oseq;
Paul Mooree1af9f22007-12-21 14:59:08 -080070 if (unlikely(x->replay.oseq == 0)) {
Masahide NAKAMURA9472c9e2008-01-31 17:14:58 -080071 XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATESEQERROR);
Paul Mooree1af9f22007-12-21 14:59:08 -080072 x->replay.oseq--;
Paul Mooreafeb14b2007-12-21 14:58:11 -080073 xfrm_audit_state_replay_overflow(x, skb);
Herbert Xudbb1db82008-01-01 23:52:59 -080074 err = -EOVERFLOW;
Paul Mooree1af9f22007-12-21 14:59:08 -080075 goto error;
76 }
Herbert Xucdf7e662007-10-08 17:26:34 -070077 if (xfrm_aevent_is_on())
78 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
Herbert Xu436a0a42007-10-08 17:25:53 -070079 }
80
Herbert Xu406ef772007-10-08 17:16:30 -070081 x->curlft.bytes += skb->len;
82 x->curlft.packets++;
83
Herbert Xu406ef772007-10-08 17:16:30 -070084 spin_unlock_bh(&x->lock);
85
Herbert Xub7c65382007-10-09 13:33:35 -070086 err = x->type->output(x, skb);
Herbert Xufcb8c152007-12-30 21:09:38 -080087 if (err == -EINPROGRESS)
88 goto out_exit;
Herbert Xuc6581a42007-11-13 21:43:43 -080089
90resume:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080091 if (err) {
92 XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c65382007-10-09 13:33:35 -070093 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080094 }
Herbert Xub7c65382007-10-09 13:33:35 -070095
Herbert Xu406ef772007-10-08 17:16:30 -070096 if (!(skb->dst = dst_pop(dst))) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080097 XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -070098 err = -EHOSTUNREACH;
99 goto error_nolock;
100 }
101 dst = skb->dst;
102 x = dst->xfrm;
Herbert Xu13996372007-10-17 21:35:51 -0700103 } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -0700104
105 err = 0;
106
Herbert Xu862b82c2007-11-13 21:43:11 -0800107out_exit:
Herbert Xu406ef772007-10-08 17:16:30 -0700108 return err;
109error:
110 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -0800111error_nolock:
112 kfree_skb(skb);
113 goto out_exit;
114}
115
Herbert Xuc6581a42007-11-13 21:43:43 -0800116int xfrm_output_resume(struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -0800117{
Herbert Xuc6581a42007-11-13 21:43:43 -0800118 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Herbert Xu862b82c2007-11-13 21:43:11 -0800119 nf_reset(skb);
120
121 err = skb->dst->ops->local_out(skb);
122 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800123 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800124
Jean-Christophe DUBOISc1e24df2008-08-13 13:35:37 -0700125 if (!skb->dst->xfrm)
Herbert Xu862b82c2007-11-13 21:43:11 -0800126 return dst_output(skb);
127
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700128 err = nf_hook(skb->dst->ops->family,
David S. Miller294b4ba2007-11-14 01:57:47 -0800129 NF_INET_POST_ROUTING, skb,
Herbert Xu862b82c2007-11-13 21:43:11 -0800130 NULL, skb->dst->dev, xfrm_output2);
131 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800132 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800133 }
134
Herbert Xuc6581a42007-11-13 21:43:43 -0800135 if (err == -EINPROGRESS)
136 err = 0;
137
138out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800139 return err;
140}
Herbert Xuc6581a42007-11-13 21:43:43 -0800141EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800142
Herbert Xuc6581a42007-11-13 21:43:43 -0800143static int xfrm_output2(struct sk_buff *skb)
144{
145 return xfrm_output_resume(skb, 1);
146}
147
148static int xfrm_output_gso(struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800149{
150 struct sk_buff *segs;
151
Herbert Xu862b82c2007-11-13 21:43:11 -0800152 segs = skb_gso_segment(skb, 0);
153 kfree_skb(skb);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700154 if (IS_ERR(segs))
Herbert Xu862b82c2007-11-13 21:43:11 -0800155 return PTR_ERR(segs);
156
157 do {
158 struct sk_buff *nskb = segs->next;
159 int err;
160
161 segs->next = NULL;
162 err = xfrm_output2(segs);
163
164 if (unlikely(err)) {
165 while ((segs = nskb)) {
166 nskb = segs->next;
167 segs->next = NULL;
168 kfree_skb(segs);
169 }
170 return err;
171 }
172
173 segs = nskb;
174 } while (segs);
175
176 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700177}
Herbert Xuc6581a42007-11-13 21:43:43 -0800178
179int xfrm_output(struct sk_buff *skb)
180{
181 int err;
182
183 if (skb_is_gso(skb))
184 return xfrm_output_gso(skb);
185
186 if (skb->ip_summed == CHECKSUM_PARTIAL) {
187 err = skb_checksum_help(skb);
188 if (err) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800189 XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800190 kfree_skb(skb);
191 return err;
192 }
193 }
194
195 return xfrm_output2(skb);
196}
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700197
198int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
199{
200 struct xfrm_mode *inner_mode;
201 if (x->sel.family == AF_UNSPEC)
202 inner_mode = xfrm_ip2inner_mode(x,
203 xfrm_af2proto(skb->dst->ops->family));
204 else
205 inner_mode = x->inner_mode;
206
207 if (inner_mode == NULL)
208 return -EAFNOSUPPORT;
209 return inner_mode->afinfo->extract_output(x, skb);
210}
211
Herbert Xu406ef772007-10-08 17:16:30 -0700212EXPORT_SYMBOL_GPL(xfrm_output);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700213EXPORT_SYMBOL_GPL(xfrm_inner_extract_output);