blob: 867484a046af15ab4ccb7a32f7fa100cc2ceb0e5 [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:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080072 if (err) {
73 XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c6538c2007-10-09 13:33:35 -070074 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080075 }
Herbert Xub7c6538c2007-10-09 13:33:35 -070076
Herbert Xu406ef772007-10-08 17:16:30 -070077 if (!(skb->dst = dst_pop(dst))) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -080078 XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -070079 err = -EHOSTUNREACH;
80 goto error_nolock;
81 }
82 dst = skb->dst;
83 x = dst->xfrm;
Herbert Xu13996372007-10-17 21:35:51 -070084 } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -070085
86 err = 0;
87
Herbert Xu862b82c2007-11-13 21:43:11 -080088out_exit:
Herbert Xu406ef772007-10-08 17:16:30 -070089 return err;
90error:
91 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -080092error_nolock:
93 kfree_skb(skb);
94 goto out_exit;
95}
96
Herbert Xuc6581a42007-11-13 21:43:43 -080097int xfrm_output_resume(struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -080098{
Herbert Xuc6581a42007-11-13 21:43:43 -080099 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Herbert Xu862b82c2007-11-13 21:43:11 -0800100 struct xfrm_state *x;
101
102 nf_reset(skb);
103
104 err = skb->dst->ops->local_out(skb);
105 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800106 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800107
108 x = skb->dst->xfrm;
109 if (!x)
110 return dst_output(skb);
111
112 err = nf_hook(x->inner_mode->afinfo->family,
David S. Miller294b4ba2007-11-14 01:57:47 -0800113 NF_INET_POST_ROUTING, skb,
Herbert Xu862b82c2007-11-13 21:43:11 -0800114 NULL, skb->dst->dev, xfrm_output2);
115 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800116 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800117 }
118
Herbert Xuc6581a42007-11-13 21:43:43 -0800119 if (err == -EINPROGRESS)
120 err = 0;
121
122out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800123 return err;
124}
Herbert Xuc6581a42007-11-13 21:43:43 -0800125EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800126
Herbert Xuc6581a42007-11-13 21:43:43 -0800127static int xfrm_output2(struct sk_buff *skb)
128{
129 return xfrm_output_resume(skb, 1);
130}
131
132static int xfrm_output_gso(struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800133{
134 struct sk_buff *segs;
135
Herbert Xu862b82c2007-11-13 21:43:11 -0800136 segs = skb_gso_segment(skb, 0);
137 kfree_skb(skb);
138 if (unlikely(IS_ERR(segs)))
139 return PTR_ERR(segs);
140
141 do {
142 struct sk_buff *nskb = segs->next;
143 int err;
144
145 segs->next = NULL;
146 err = xfrm_output2(segs);
147
148 if (unlikely(err)) {
149 while ((segs = nskb)) {
150 nskb = segs->next;
151 segs->next = NULL;
152 kfree_skb(segs);
153 }
154 return err;
155 }
156
157 segs = nskb;
158 } while (segs);
159
160 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700161}
Herbert Xuc6581a42007-11-13 21:43:43 -0800162
163int xfrm_output(struct sk_buff *skb)
164{
165 int err;
166
167 if (skb_is_gso(skb))
168 return xfrm_output_gso(skb);
169
170 if (skb->ip_summed == CHECKSUM_PARTIAL) {
171 err = skb_checksum_help(skb);
172 if (err) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800173 XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800174 kfree_skb(skb);
175 return err;
176 }
177 }
178
179 return xfrm_output2(skb);
180}
Herbert Xu406ef772007-10-08 17:16:30 -0700181EXPORT_SYMBOL_GPL(xfrm_output);