Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 12 | #include <linux/if_ether.h> |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/icmpv6.h> |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 17 | #include <linux/netfilter_ipv6.h> |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 18 | #include <net/dst.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <net/ipv6.h> |
| 20 | #include <net/xfrm.h> |
| 21 | |
Masahide NAKAMURA | aee5adb | 2006-08-23 17:57:28 -0700 | [diff] [blame] | 22 | int 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 Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 28 | EXPORT_SYMBOL(xfrm6_find_1stfragopt); |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static int xfrm6_tunnel_check_size(struct sk_buff *skb) |
| 31 | { |
| 32 | int mtu, ret = 0; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame^] | 33 | struct dst_entry *dst = skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | mtu = dst_mtu(dst); |
| 36 | if (mtu < IPV6_MIN_MTU) |
| 37 | mtu = IPV6_MIN_MTU; |
| 38 | |
Herbert Xu | 28a89453 | 2008-02-12 18:07:27 -0800 | [diff] [blame] | 39 | if (!skb->local_df && skb->len > mtu) { |
Herbert Xu | 180e425 | 2005-05-23 13:11:07 -0700 | [diff] [blame] | 40 | skb->dev = dst->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev); |
| 42 | ret = -EMSGSIZE; |
| 43 | } |
| 44 | |
| 45 | return ret; |
| 46 | } |
| 47 | |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 48 | int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | int err; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 51 | |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 52 | err = xfrm6_tunnel_check_size(skb); |
| 53 | if (err) |
| 54 | return err; |
| 55 | |
Herbert Xu | 60d5fcf | 2007-11-19 18:47:58 -0800 | [diff] [blame] | 56 | XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr; |
| 57 | |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 58 | return xfrm6_extract_header(skb); |
| 59 | } |
| 60 | |
| 61 | int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb) |
| 62 | { |
| 63 | int err; |
| 64 | |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 65 | err = xfrm_inner_extract_output(x, skb); |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 66 | if (err) |
| 67 | return err; |
| 68 | |
| 69 | memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 70 | #ifdef CONFIG_NETFILTER |
| 71 | IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED; |
| 72 | #endif |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 73 | |
| 74 | skb->protocol = htons(ETH_P_IPV6); |
Steffen Klassert | d1d88e5d | 2009-04-06 03:58:50 +0000 | [diff] [blame] | 75 | skb->local_df = 1; |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 76 | |
| 77 | return x->outer_mode->output2(x, skb); |
| 78 | } |
| 79 | EXPORT_SYMBOL(xfrm6_prepare_output); |
| 80 | |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 81 | static int xfrm6_output_finish(struct sk_buff *skb) |
| 82 | { |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 83 | #ifdef CONFIG_NETFILTER |
| 84 | IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED; |
| 85 | #endif |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 86 | |
Patrick McHardy | 679e898 | 2006-07-30 20:19:11 -0700 | [diff] [blame] | 87 | skb->protocol = htons(ETH_P_IPV6); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 88 | return xfrm_output(skb); |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 91 | int xfrm6_output(struct sk_buff *skb) |
| 92 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame^] | 93 | return NF_HOOK(PF_INET6, NF_INET_POST_ROUTING, skb, NULL, skb_dst(skb)->dev, |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 94 | xfrm6_output_finish); |
| 95 | } |