blob: c1ba63efd7cbcf3a786050b2c9bc01ff818690ea [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);
28
29 if (nhead > 0)
30 return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
31
32 /* Check tail too... */
33 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) {
65 XFRM_SKB_CB(skb)->seq = ++x->replay.oseq;
Paul Mooree1af9f22007-12-21 14:59:08 -080066 if (unlikely(x->replay.oseq == 0)) {
67 x->replay.oseq--;
Paul Mooreafeb14b2007-12-21 14:58:11 -080068 xfrm_audit_state_replay_overflow(x, skb);
Paul Mooree1af9f22007-12-21 14:59:08 -080069 goto error;
70 }
Herbert Xucdf7e662007-10-08 17:26:34 -070071 if (xfrm_aevent_is_on())
72 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
Herbert Xu436a0a42007-10-08 17:25:53 -070073 }
74
Herbert Xu406ef772007-10-08 17:16:30 -070075 x->curlft.bytes += skb->len;
76 x->curlft.packets++;
77
Herbert Xu406ef772007-10-08 17:16:30 -070078 spin_unlock_bh(&x->lock);
79
Herbert Xub7c6538c2007-10-09 13:33:35 -070080 err = x->type->output(x, skb);
Herbert Xuc6581a42007-11-13 21:43:43 -080081
82resume:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080083 if (err) {
84 XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c6538c2007-10-09 13:33:35 -070085 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080086 }
Herbert Xub7c6538c2007-10-09 13:33:35 -070087
Herbert Xu406ef772007-10-08 17:16:30 -070088 if (!(skb->dst = dst_pop(dst))) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080089 XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -070090 err = -EHOSTUNREACH;
91 goto error_nolock;
92 }
93 dst = skb->dst;
94 x = dst->xfrm;
Herbert Xu13996372007-10-17 21:35:51 -070095 } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -070096
97 err = 0;
98
Herbert Xu862b82c2007-11-13 21:43:11 -080099out_exit:
Herbert Xu406ef772007-10-08 17:16:30 -0700100 return err;
101error:
102 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -0800103error_nolock:
104 kfree_skb(skb);
105 goto out_exit;
106}
107
Herbert Xuc6581a42007-11-13 21:43:43 -0800108int xfrm_output_resume(struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -0800109{
Herbert Xuc6581a42007-11-13 21:43:43 -0800110 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Herbert Xu862b82c2007-11-13 21:43:11 -0800111 struct xfrm_state *x;
112
113 nf_reset(skb);
114
115 err = skb->dst->ops->local_out(skb);
116 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800117 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800118
119 x = skb->dst->xfrm;
120 if (!x)
121 return dst_output(skb);
122
123 err = nf_hook(x->inner_mode->afinfo->family,
David S. Miller294b4ba2007-11-14 01:57:47 -0800124 NF_INET_POST_ROUTING, skb,
Herbert Xu862b82c2007-11-13 21:43:11 -0800125 NULL, skb->dst->dev, xfrm_output2);
126 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800127 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800128 }
129
Herbert Xuc6581a42007-11-13 21:43:43 -0800130 if (err == -EINPROGRESS)
131 err = 0;
132
133out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800134 return err;
135}
Herbert Xuc6581a42007-11-13 21:43:43 -0800136EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800137
Herbert Xuc6581a42007-11-13 21:43:43 -0800138static int xfrm_output2(struct sk_buff *skb)
139{
140 return xfrm_output_resume(skb, 1);
141}
142
143static int xfrm_output_gso(struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800144{
145 struct sk_buff *segs;
146
Herbert Xu862b82c2007-11-13 21:43:11 -0800147 segs = skb_gso_segment(skb, 0);
148 kfree_skb(skb);
149 if (unlikely(IS_ERR(segs)))
150 return PTR_ERR(segs);
151
152 do {
153 struct sk_buff *nskb = segs->next;
154 int err;
155
156 segs->next = NULL;
157 err = xfrm_output2(segs);
158
159 if (unlikely(err)) {
160 while ((segs = nskb)) {
161 nskb = segs->next;
162 segs->next = NULL;
163 kfree_skb(segs);
164 }
165 return err;
166 }
167
168 segs = nskb;
169 } while (segs);
170
171 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700172}
Herbert Xuc6581a42007-11-13 21:43:43 -0800173
174int xfrm_output(struct sk_buff *skb)
175{
176 int err;
177
178 if (skb_is_gso(skb))
179 return xfrm_output_gso(skb);
180
181 if (skb->ip_summed == CHECKSUM_PARTIAL) {
182 err = skb_checksum_help(skb);
183 if (err) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800184 XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800185 kfree_skb(skb);
186 return err;
187 }
188 }
189
190 return xfrm_output2(skb);
191}
Herbert Xu406ef772007-10-08 17:16:30 -0700192EXPORT_SYMBOL_GPL(xfrm_output);