blob: b4628fbf8ff598013f484147286a98fbe62653ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm6_output.c - Common IPsec encapsulation code for IPv6.
3 * Copyright (C) 2002 USAGI/WIDE Project
4 * Copyright (c) 2004 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
Patrick McHardy16a66772006-01-06 23:01:48 -080012#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/skbuff.h>
14#include <linux/spinlock.h>
15#include <linux/icmpv6.h>
Patrick McHardy16a66772006-01-06 23:01:48 -080016#include <linux/netfilter_ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/ipv6.h>
18#include <net/xfrm.h>
19
Masahide NAKAMURAaee5adb2006-08-23 17:57:28 -070020int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
21 u8 **prevhdr)
22{
23 return ip6_find_1stfragopt(skb, prevhdr);
24}
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int xfrm6_tunnel_check_size(struct sk_buff *skb)
27{
28 int mtu, ret = 0;
29 struct dst_entry *dst = skb->dst;
30
31 mtu = dst_mtu(dst);
32 if (mtu < IPV6_MIN_MTU)
33 mtu = IPV6_MIN_MTU;
34
35 if (skb->len > mtu) {
Herbert Xu180e4252005-05-23 13:11:07 -070036 skb->dev = dst->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
38 ret = -EMSGSIZE;
39 }
40
41 return ret;
42}
43
Patrick McHardy16a66772006-01-06 23:01:48 -080044static int xfrm6_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;
49
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 = xfrm6_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
Herbert Xub59f45d2006-05-27 23:05:54 -070068 err = x->mode->output(skb);
69 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Patrick McHardy16a66772006-01-06 23:01:48 -080081 skb->nh.raw = skb->data;
82
83 if (!(skb->dst = dst_pop(dst))) {
84 err = -EHOSTUNREACH;
85 goto error_nolock;
86 }
87 dst = skb->dst;
88 x = dst->xfrm;
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -070089 } while (x && (x->props.mode != XFRM_MODE_TUNNEL));
Patrick McHardy16a66772006-01-06 23:01:48 -080090
Patrick McHardy3e3850e2006-01-06 23:04:54 -080091 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
Patrick McHardy16a66772006-01-06 23:01:48 -080092 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94out_exit:
95 return err;
96error:
97 spin_unlock_bh(&x->lock);
98error_nolock:
99 kfree_skb(skb);
100 goto out_exit;
101}
Patrick McHardy16a66772006-01-06 23:01:48 -0800102
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700103static int xfrm6_output_finish2(struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -0800104{
105 int err;
106
107 while (likely((err = xfrm6_output_one(skb)) == 0)) {
108 nf_reset(skb);
109
110 err = nf_hook(PF_INET6, NF_IP6_LOCAL_OUT, &skb, NULL,
111 skb->dst->dev, dst_output);
112 if (unlikely(err != 1))
113 break;
114
115 if (!skb->dst->xfrm)
116 return dst_output(skb);
117
118 err = nf_hook(PF_INET6, NF_IP6_POST_ROUTING, &skb, NULL,
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700119 skb->dst->dev, xfrm6_output_finish2);
Patrick McHardy16a66772006-01-06 23:01:48 -0800120 if (unlikely(err != 1))
121 break;
122 }
123
124 return err;
125}
126
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700127static int xfrm6_output_finish(struct sk_buff *skb)
128{
129 struct sk_buff *segs;
130
Herbert Xu89114af2006-07-08 13:34:32 -0700131 if (!skb_is_gso(skb))
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700132 return xfrm6_output_finish2(skb);
133
Patrick McHardy679e8982006-07-30 20:19:11 -0700134 skb->protocol = htons(ETH_P_IPV6);
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700135 segs = skb_gso_segment(skb, 0);
136 kfree_skb(skb);
137 if (unlikely(IS_ERR(segs)))
138 return PTR_ERR(segs);
139
140 do {
141 struct sk_buff *nskb = segs->next;
142 int err;
143
144 segs->next = NULL;
145 err = xfrm6_output_finish2(segs);
146
147 if (unlikely(err)) {
148 while ((segs = nskb)) {
149 nskb = segs->next;
150 segs->next = NULL;
151 kfree_skb(segs);
152 }
153 return err;
154 }
155
156 segs = nskb;
157 } while (segs);
158
159 return 0;
160}
161
Patrick McHardy16a66772006-01-06 23:01:48 -0800162int xfrm6_output(struct sk_buff *skb)
163{
164 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dst->dev,
165 xfrm6_output_finish);
166}