blob: 038ca160fe2c4c67e3ba17fd39f3361fd31fa795 [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;
25 struct iphdr *iph = skb->nh.iph;
26
27 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
28 goto out;
29
30 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 if (!(iph->frag_off & htons(IP_DF)) || skb->local_df)
33 goto out;
34
35 dst = skb->dst;
36 mtu = dst_mtu(dst);
37 if (skb->len > mtu) {
38 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
39 ret = -EMSGSIZE;
40 }
41out:
42 return ret;
43}
44
Patrick McHardy16a66772006-01-06 23:01:48 -080045static int xfrm4_output_one(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 struct dst_entry *dst = skb->dst;
48 struct xfrm_state *x = dst->xfrm;
49 int err;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090050
Patrick McHardy84fa7932006-08-29 16:44:56 -070051 if (skb->ip_summed == CHECKSUM_PARTIAL) {
52 err = skb_checksum_help(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (err)
54 goto error_nolock;
55 }
56
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -070057 if (x->props.mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 err = xfrm4_tunnel_check_size(skb);
59 if (err)
60 goto error_nolock;
61 }
62
Patrick McHardy16a66772006-01-06 23:01:48 -080063 do {
64 spin_lock_bh(&x->lock);
65 err = xfrm_state_check(x, skb);
66 if (err)
67 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jamal Hadi Salimeb878e82006-08-31 17:42:59 -070069 err = x->mode->output(x, skb);
Herbert Xub59f45d2006-05-27 23:05:54 -070070 if (err)
71 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Patrick McHardy16a66772006-01-06 23:01:48 -080073 err = x->type->output(x, skb);
74 if (err)
75 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Patrick McHardy16a66772006-01-06 23:01:48 -080077 x->curlft.bytes += skb->len;
78 x->curlft.packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Patrick McHardy16a66772006-01-06 23:01:48 -080080 spin_unlock_bh(&x->lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090081
Patrick McHardy16a66772006-01-06 23:01:48 -080082 if (!(skb->dst = dst_pop(dst))) {
83 err = -EHOSTUNREACH;
84 goto error_nolock;
85 }
86 dst = skb->dst;
87 x = dst->xfrm;
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -070088 } while (x && (x->props.mode != XFRM_MODE_TUNNEL));
Patrick McHardy16a66772006-01-06 23:01:48 -080089
Patrick McHardy3e3850e2006-01-06 23:04:54 -080090 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
Patrick McHardy16a66772006-01-06 23:01:48 -080091 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93out_exit:
94 return err;
95error:
96 spin_unlock_bh(&x->lock);
97error_nolock:
98 kfree_skb(skb);
99 goto out_exit;
100}
Patrick McHardy16a66772006-01-06 23:01:48 -0800101
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700102static int xfrm4_output_finish2(struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -0800103{
104 int err;
105
106 while (likely((err = xfrm4_output_one(skb)) == 0)) {
107 nf_reset(skb);
108
109 err = nf_hook(PF_INET, NF_IP_LOCAL_OUT, &skb, NULL,
110 skb->dst->dev, dst_output);
111 if (unlikely(err != 1))
112 break;
113
114 if (!skb->dst->xfrm)
115 return dst_output(skb);
116
117 err = nf_hook(PF_INET, NF_IP_POST_ROUTING, &skb, NULL,
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700118 skb->dst->dev, xfrm4_output_finish2);
Patrick McHardy16a66772006-01-06 23:01:48 -0800119 if (unlikely(err != 1))
120 break;
121 }
122
123 return err;
124}
125
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700126static int xfrm4_output_finish(struct sk_buff *skb)
127{
128 struct sk_buff *segs;
129
130#ifdef CONFIG_NETFILTER
131 if (!skb->dst->xfrm) {
132 IPCB(skb)->flags |= IPSKB_REROUTED;
133 return dst_output(skb);
134 }
135#endif
136
Herbert Xu89114af2006-07-08 13:34:32 -0700137 if (!skb_is_gso(skb))
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700138 return xfrm4_output_finish2(skb);
139
140 skb->protocol = htons(ETH_P_IP);
141 segs = skb_gso_segment(skb, 0);
142 kfree_skb(skb);
143 if (unlikely(IS_ERR(segs)))
144 return PTR_ERR(segs);
145
146 do {
147 struct sk_buff *nskb = segs->next;
148 int err;
149
150 segs->next = NULL;
151 err = xfrm4_output_finish2(segs);
152
153 if (unlikely(err)) {
154 while ((segs = nskb)) {
155 nskb = segs->next;
156 segs->next = NULL;
157 kfree_skb(segs);
158 }
159 return err;
160 }
161
162 segs = nskb;
163 } while (segs);
164
165 return 0;
166}
167
Patrick McHardy16a66772006-01-06 23:01:48 -0800168int xfrm4_output(struct sk_buff *skb)
169{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800170 return NF_HOOK_COND(PF_INET, NF_IP_POST_ROUTING, skb, NULL, skb->dst->dev,
171 xfrm4_output_finish,
172 !(IPCB(skb)->flags & IPSKB_REROUTED));
Patrick McHardy16a66772006-01-06 23:01:48 -0800173}