blob: 44ef208a75cb175bfbff21cae090212a6d996b07 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm4_output.c - Common IPsec encapsulation code for IPv4.
3 * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 */
10
Patrick McHardy16a66772006-01-06 23:01:48 -080011#include <linux/compiler.h>
Herbert Xu09b8f7a2006-06-22 03:08:03 -070012#include <linux/if_ether.h>
13#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/skbuff.h>
15#include <linux/spinlock.h>
Patrick McHardy16a66772006-01-06 23:01:48 -080016#include <linux/netfilter_ipv4.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/ip.h>
18#include <net/xfrm.h>
19#include <net/icmp.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static int xfrm4_tunnel_check_size(struct sk_buff *skb)
22{
23 int mtu, ret = 0;
24 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
27 goto out;
28
29 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090030
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070031 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->local_df)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 goto out;
33
34 dst = skb->dst;
35 mtu = dst_mtu(dst);
36 if (skb->len > mtu) {
37 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
38 ret = -EMSGSIZE;
39 }
40out:
41 return ret;
42}
43
Patrick McHardy16a66772006-01-06 23:01:48 -080044static int xfrm4_output_one(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct dst_entry *dst = skb->dst;
47 struct xfrm_state *x = dst->xfrm;
48 int err;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090049
Patrick McHardy84fa7932006-08-29 16:44:56 -070050 if (skb->ip_summed == CHECKSUM_PARTIAL) {
51 err = skb_checksum_help(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (err)
53 goto error_nolock;
54 }
55
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -070056 if (x->props.mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 err = xfrm4_tunnel_check_size(skb);
58 if (err)
59 goto error_nolock;
60 }
61
Patrick McHardy16a66772006-01-06 23:01:48 -080062 do {
63 spin_lock_bh(&x->lock);
64 err = xfrm_state_check(x, skb);
65 if (err)
66 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Jamal Hadi Salimeb878e82006-08-31 17:42:59 -070068 err = x->mode->output(x, skb);
Herbert Xub59f45d2006-05-27 23:05:54 -070069 if (err)
70 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Patrick McHardy16a66772006-01-06 23:01:48 -080072 err = x->type->output(x, skb);
73 if (err)
74 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Patrick McHardy16a66772006-01-06 23:01:48 -080076 x->curlft.bytes += skb->len;
77 x->curlft.packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Patrick McHardy16a66772006-01-06 23:01:48 -080079 spin_unlock_bh(&x->lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090080
Patrick McHardy16a66772006-01-06 23:01:48 -080081 if (!(skb->dst = dst_pop(dst))) {
82 err = -EHOSTUNREACH;
83 goto error_nolock;
84 }
85 dst = skb->dst;
86 x = dst->xfrm;
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -070087 } while (x && (x->props.mode != XFRM_MODE_TUNNEL));
Patrick McHardy16a66772006-01-06 23:01:48 -080088
Patrick McHardy3e3850e2006-01-06 23:04:54 -080089 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
Patrick McHardy16a66772006-01-06 23:01:48 -080090 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92out_exit:
93 return err;
94error:
95 spin_unlock_bh(&x->lock);
96error_nolock:
97 kfree_skb(skb);
98 goto out_exit;
99}
Patrick McHardy16a66772006-01-06 23:01:48 -0800100
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700101static int xfrm4_output_finish2(struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -0800102{
103 int err;
104
105 while (likely((err = xfrm4_output_one(skb)) == 0)) {
106 nf_reset(skb);
107
108 err = nf_hook(PF_INET, NF_IP_LOCAL_OUT, &skb, NULL,
109 skb->dst->dev, dst_output);
110 if (unlikely(err != 1))
111 break;
112
113 if (!skb->dst->xfrm)
114 return dst_output(skb);
115
116 err = nf_hook(PF_INET, NF_IP_POST_ROUTING, &skb, NULL,
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700117 skb->dst->dev, xfrm4_output_finish2);
Patrick McHardy16a66772006-01-06 23:01:48 -0800118 if (unlikely(err != 1))
119 break;
120 }
121
122 return err;
123}
124
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700125static int xfrm4_output_finish(struct sk_buff *skb)
126{
127 struct sk_buff *segs;
128
129#ifdef CONFIG_NETFILTER
130 if (!skb->dst->xfrm) {
131 IPCB(skb)->flags |= IPSKB_REROUTED;
132 return dst_output(skb);
133 }
134#endif
135
Herbert Xu89114af2006-07-08 13:34:32 -0700136 if (!skb_is_gso(skb))
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700137 return xfrm4_output_finish2(skb);
138
139 skb->protocol = htons(ETH_P_IP);
140 segs = skb_gso_segment(skb, 0);
141 kfree_skb(skb);
142 if (unlikely(IS_ERR(segs)))
143 return PTR_ERR(segs);
144
145 do {
146 struct sk_buff *nskb = segs->next;
147 int err;
148
149 segs->next = NULL;
150 err = xfrm4_output_finish2(segs);
151
152 if (unlikely(err)) {
153 while ((segs = nskb)) {
154 nskb = segs->next;
155 segs->next = NULL;
156 kfree_skb(segs);
157 }
158 return err;
159 }
160
161 segs = nskb;
162 } while (segs);
163
164 return 0;
165}
166
Patrick McHardy16a66772006-01-06 23:01:48 -0800167int xfrm4_output(struct sk_buff *skb)
168{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800169 return NF_HOOK_COND(PF_INET, NF_IP_POST_ROUTING, skb, NULL, skb->dst->dev,
170 xfrm4_output_finish,
171 !(IPCB(skb)->flags & IPSKB_REROUTED));
Patrick McHardy16a66772006-01-06 23:01:48 -0800172}