blob: 26fa0cb78c9419b8f1e26f25463dad203e4c72cf [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);
46 if (err)
47 goto error_nolock;
48
Herbert Xua2deb6d22007-11-13 21:39:38 -080049 err = x->outer_mode->output(x, skb);
50 if (err)
Herbert Xu910ef702007-12-18 22:14:25 -080051 goto error_nolock;
Herbert Xua2deb6d22007-11-13 21:39:38 -080052
Herbert Xu406ef772007-10-08 17:16:30 -070053 spin_lock_bh(&x->lock);
Herbert Xu910ef702007-12-18 22:14:25 -080054 err = xfrm_state_check_expire(x);
Herbert Xu406ef772007-10-08 17:16:30 -070055 if (err)
56 goto error;
57
Herbert Xu436a0a42007-10-08 17:25:53 -070058 if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
59 XFRM_SKB_CB(skb)->seq = ++x->replay.oseq;
Herbert Xucdf7e662007-10-08 17:26:34 -070060 if (xfrm_aevent_is_on())
61 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
Herbert Xu436a0a42007-10-08 17:25:53 -070062 }
63
Herbert Xu406ef772007-10-08 17:16:30 -070064 x->curlft.bytes += skb->len;
65 x->curlft.packets++;
66
Herbert Xu406ef772007-10-08 17:16:30 -070067 spin_unlock_bh(&x->lock);
68
Herbert Xub7c6538c2007-10-09 13:33:35 -070069 err = x->type->output(x, skb);
Herbert Xuc6581a42007-11-13 21:43:43 -080070
71resume:
Herbert Xub7c6538c2007-10-09 13:33:35 -070072 if (err)
73 goto error_nolock;
74
Herbert Xu406ef772007-10-08 17:16:30 -070075 if (!(skb->dst = dst_pop(dst))) {
76 err = -EHOSTUNREACH;
77 goto error_nolock;
78 }
79 dst = skb->dst;
80 x = dst->xfrm;
Herbert Xu13996372007-10-17 21:35:51 -070081 } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -070082
83 err = 0;
84
Herbert Xu862b82c2007-11-13 21:43:11 -080085out_exit:
Herbert Xu406ef772007-10-08 17:16:30 -070086 return err;
87error:
88 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -080089error_nolock:
90 kfree_skb(skb);
91 goto out_exit;
92}
93
Herbert Xuc6581a42007-11-13 21:43:43 -080094int xfrm_output_resume(struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -080095{
Herbert Xuc6581a42007-11-13 21:43:43 -080096 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Herbert Xu862b82c2007-11-13 21:43:11 -080097 struct xfrm_state *x;
98
99 nf_reset(skb);
100
101 err = skb->dst->ops->local_out(skb);
102 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800103 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800104
105 x = skb->dst->xfrm;
106 if (!x)
107 return dst_output(skb);
108
109 err = nf_hook(x->inner_mode->afinfo->family,
David S. Miller294b4ba2007-11-14 01:57:47 -0800110 NF_INET_POST_ROUTING, skb,
Herbert Xu862b82c2007-11-13 21:43:11 -0800111 NULL, skb->dst->dev, xfrm_output2);
112 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800113 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800114 }
115
Herbert Xuc6581a42007-11-13 21:43:43 -0800116 if (err == -EINPROGRESS)
117 err = 0;
118
119out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800120 return err;
121}
Herbert Xuc6581a42007-11-13 21:43:43 -0800122EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800123
Herbert Xuc6581a42007-11-13 21:43:43 -0800124static int xfrm_output2(struct sk_buff *skb)
125{
126 return xfrm_output_resume(skb, 1);
127}
128
129static int xfrm_output_gso(struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800130{
131 struct sk_buff *segs;
132
Herbert Xu862b82c2007-11-13 21:43:11 -0800133 segs = skb_gso_segment(skb, 0);
134 kfree_skb(skb);
135 if (unlikely(IS_ERR(segs)))
136 return PTR_ERR(segs);
137
138 do {
139 struct sk_buff *nskb = segs->next;
140 int err;
141
142 segs->next = NULL;
143 err = xfrm_output2(segs);
144
145 if (unlikely(err)) {
146 while ((segs = nskb)) {
147 nskb = segs->next;
148 segs->next = NULL;
149 kfree_skb(segs);
150 }
151 return err;
152 }
153
154 segs = nskb;
155 } while (segs);
156
157 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700158}
Herbert Xuc6581a42007-11-13 21:43:43 -0800159
160int xfrm_output(struct sk_buff *skb)
161{
162 int err;
163
164 if (skb_is_gso(skb))
165 return xfrm_output_gso(skb);
166
167 if (skb->ip_summed == CHECKSUM_PARTIAL) {
168 err = skb_checksum_help(skb);
169 if (err) {
170 kfree_skb(skb);
171 return err;
172 }
173 }
174
175 return xfrm_output2(skb);
176}
Herbert Xu406ef772007-10-08 17:16:30 -0700177EXPORT_SYMBOL_GPL(xfrm_output);