blob: 49a91c5f5623b80298001b11927eb763883c1553 [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>
David Stevensad0081e2010-12-17 11:42:42 +000020#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/xfrm.h>
22
Masahide NAKAMURAaee5adb2006-08-23 17:57:28 -070023int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
24 u8 **prevhdr)
25{
26 return ip6_find_1stfragopt(skb, prevhdr);
27}
28
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +090029EXPORT_SYMBOL(xfrm6_find_1stfragopt);
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int xfrm6_tunnel_check_size(struct sk_buff *skb)
32{
33 int mtu, ret = 0;
Eric Dumazetadf30902009-06-02 05:19:30 +000034 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 mtu = dst_mtu(dst);
37 if (mtu < IPV6_MIN_MTU)
38 mtu = IPV6_MIN_MTU;
39
Herbert Xu28a89452008-02-12 18:07:27 -080040 if (!skb->local_df && skb->len > mtu) {
Herbert Xu180e4252005-05-23 13:11:07 -070041 skb->dev = dst->dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +000042 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 ret = -EMSGSIZE;
44 }
45
46 return ret;
47}
48
Herbert Xu36cf9ac2007-11-13 21:40:52 -080049int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090052
Herbert Xu36cf9ac2007-11-13 21:40:52 -080053 err = xfrm6_tunnel_check_size(skb);
54 if (err)
55 return err;
56
Herbert Xu60d5fcf2007-11-19 18:47:58 -080057 XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
58
Herbert Xu36cf9ac2007-11-13 21:40:52 -080059 return xfrm6_extract_header(skb);
60}
61
62int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
63{
64 int err;
65
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -070066 err = xfrm_inner_extract_output(x, skb);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080067 if (err)
68 return err;
69
70 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
Herbert Xu862b82c2007-11-13 21:43:11 -080071#ifdef CONFIG_NETFILTER
72 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
73#endif
Herbert Xu36cf9ac2007-11-13 21:40:52 -080074
75 skb->protocol = htons(ETH_P_IPV6);
Steffen Klassertd1d88e5d2009-04-06 03:58:50 +000076 skb->local_df = 1;
Herbert Xu36cf9ac2007-11-13 21:40:52 -080077
78 return x->outer_mode->output2(x, skb);
79}
80EXPORT_SYMBOL(xfrm6_prepare_output);
81
Steffen Klassert43a4dea2011-05-09 19:36:38 +000082int xfrm6_output_finish(struct sk_buff *skb)
Herbert Xu09b8f7a2006-06-22 03:08:03 -070083{
Herbert Xu862b82c2007-11-13 21:43:11 -080084#ifdef CONFIG_NETFILTER
85 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
86#endif
Herbert Xu09b8f7a2006-06-22 03:08:03 -070087
Patrick McHardy679e8982006-07-30 20:19:11 -070088 skb->protocol = htons(ETH_P_IPV6);
Herbert Xu862b82c2007-11-13 21:43:11 -080089 return xfrm_output(skb);
Herbert Xu09b8f7a2006-06-22 03:08:03 -070090}
91
David Stevensad0081e2010-12-17 11:42:42 +000092static int __xfrm6_output(struct sk_buff *skb)
93{
94 struct dst_entry *dst = skb_dst(skb);
95 struct xfrm_state *x = dst->xfrm;
96
97 if ((x && x->props.mode == XFRM_MODE_TUNNEL) &&
98 ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
99 dst_allfrag(skb_dst(skb)))) {
Steffen Klassert43a4dea2011-05-09 19:36:38 +0000100 return ip6_fragment(skb, x->outer_mode->afinfo->output_finish);
David Stevensad0081e2010-12-17 11:42:42 +0000101 }
Steffen Klassert43a4dea2011-05-09 19:36:38 +0000102 return x->outer_mode->afinfo->output_finish(skb);
David Stevensad0081e2010-12-17 11:42:42 +0000103}
104
Patrick McHardy16a66772006-01-06 23:01:48 -0800105int xfrm6_output(struct sk_buff *skb)
106{
Jan Engelhardtb2e0b382010-03-23 04:09:07 +0100107 return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL,
David Stevensad0081e2010-12-17 11:42:42 +0000108 skb_dst(skb)->dev, __xfrm6_output);
Patrick McHardy16a66772006-01-06 23:01:48 -0800109}