blob: c45050cfe72b27128d80d574a459d4ba5de12dd8 [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>
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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
Herbert Xu406ef772007-10-08 17:16:30 -070012#include <linux/if_ether.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -080013#include <linux/kernel.h>
14#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/icmpv6.h>
Patrick McHardy16a66772006-01-06 23:01:48 -080017#include <linux/netfilter_ipv6.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -080018#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/ipv6.h>
20#include <net/xfrm.h>
21
Masahide NAKAMURAaee5adb2006-08-23 17:57:28 -070022int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
23 u8 **prevhdr)
24{
25 return ip6_find_1stfragopt(skb, prevhdr);
26}
27
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +090028EXPORT_SYMBOL(xfrm6_find_1stfragopt);
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int xfrm6_tunnel_check_size(struct sk_buff *skb)
31{
32 int mtu, ret = 0;
33 struct dst_entry *dst = skb->dst;
34
35 mtu = dst_mtu(dst);
36 if (mtu < IPV6_MIN_MTU)
37 mtu = IPV6_MIN_MTU;
38
39 if (skb->len > mtu) {
Herbert Xu180e4252005-05-23 13:11:07 -070040 skb->dev = dst->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
42 ret = -EMSGSIZE;
43 }
44
45 return ret;
46}
47
Herbert Xu36cf9ac2007-11-13 21:40:52 -080048int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090051
Herbert Xu36cf9ac2007-11-13 21:40:52 -080052 err = xfrm6_tunnel_check_size(skb);
53 if (err)
54 return err;
55
Herbert Xu227620e2007-11-13 21:41:28 -080056 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080057 return xfrm6_extract_header(skb);
58}
59
60int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
61{
62 int err;
63
64 err = x->inner_mode->afinfo->extract_output(x, skb);
65 if (err)
66 return err;
67
68 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
69
70 skb->protocol = htons(ETH_P_IPV6);
71
72 return x->outer_mode->output2(x, skb);
73}
74EXPORT_SYMBOL(xfrm6_prepare_output);
75
76static inline int xfrm6_output_one(struct sk_buff *skb)
77{
78 struct ipv6hdr *iph;
79 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Herbert Xu406ef772007-10-08 17:16:30 -070081 err = xfrm_output(skb);
82 if (err)
83 goto error_nolock;
Patrick McHardy16a66772006-01-06 23:01:48 -080084
Herbert Xuceb1eec2007-10-10 15:45:52 -070085 iph = ipv6_hdr(skb);
86 iph->payload_len = htons(skb->len - sizeof(*iph));
87
Patrick McHardy3e3850e2006-01-06 23:04:54 -080088 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
Patrick McHardy16a66772006-01-06 23:01:48 -080089 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91out_exit:
92 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093error_nolock:
94 kfree_skb(skb);
95 goto out_exit;
96}
Patrick McHardy16a66772006-01-06 23:01:48 -080097
Herbert Xu09b8f7a2006-06-22 03:08:03 -070098static int xfrm6_output_finish2(struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -080099{
100 int err;
101
102 while (likely((err = xfrm6_output_one(skb)) == 0)) {
103 nf_reset(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900104
Herbert Xu3db05fe2007-10-15 00:53:15 -0700105 err = nf_hook(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
Patrick McHardy16a66772006-01-06 23:01:48 -0800106 skb->dst->dev, dst_output);
107 if (unlikely(err != 1))
108 break;
109
110 if (!skb->dst->xfrm)
111 return dst_output(skb);
112
Herbert Xu3db05fe2007-10-15 00:53:15 -0700113 err = nf_hook(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL,
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700114 skb->dst->dev, xfrm6_output_finish2);
Patrick McHardy16a66772006-01-06 23:01:48 -0800115 if (unlikely(err != 1))
116 break;
117 }
118
119 return err;
120}
121
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700122static int xfrm6_output_finish(struct sk_buff *skb)
123{
124 struct sk_buff *segs;
125
Herbert Xu89114af2006-07-08 13:34:32 -0700126 if (!skb_is_gso(skb))
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700127 return xfrm6_output_finish2(skb);
128
Patrick McHardy679e8982006-07-30 20:19:11 -0700129 skb->protocol = htons(ETH_P_IPV6);
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700130 segs = skb_gso_segment(skb, 0);
131 kfree_skb(skb);
132 if (unlikely(IS_ERR(segs)))
133 return PTR_ERR(segs);
134
135 do {
136 struct sk_buff *nskb = segs->next;
137 int err;
138
139 segs->next = NULL;
140 err = xfrm6_output_finish2(segs);
141
142 if (unlikely(err)) {
143 while ((segs = nskb)) {
144 nskb = segs->next;
145 segs->next = NULL;
146 kfree_skb(segs);
147 }
148 return err;
149 }
150
151 segs = nskb;
152 } while (segs);
153
154 return 0;
155}
156
Patrick McHardy16a66772006-01-06 23:01:48 -0800157int xfrm6_output(struct sk_buff *skb)
158{
159 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dst->dev,
160 xfrm6_output_finish);
161}