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> |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 13 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/icmpv6.h> |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 16 | #include <linux/netfilter_ipv6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <net/ipv6.h> |
| 18 | #include <net/xfrm.h> |
| 19 | |
Masahide NAKAMURA | aee5adb | 2006-08-23 17:57:28 -0700 | [diff] [blame] | 20 | int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb, |
| 21 | u8 **prevhdr) |
| 22 | { |
| 23 | return ip6_find_1stfragopt(skb, prevhdr); |
| 24 | } |
| 25 | |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 26 | EXPORT_SYMBOL(xfrm6_find_1stfragopt); |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static int xfrm6_tunnel_check_size(struct sk_buff *skb) |
| 29 | { |
| 30 | int mtu, ret = 0; |
| 31 | struct dst_entry *dst = skb->dst; |
| 32 | |
| 33 | mtu = dst_mtu(dst); |
| 34 | if (mtu < IPV6_MIN_MTU) |
| 35 | mtu = IPV6_MIN_MTU; |
| 36 | |
| 37 | if (skb->len > mtu) { |
Herbert Xu | 180e425 | 2005-05-23 13:11:07 -0700 | [diff] [blame] | 38 | skb->dev = dst->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev); |
| 40 | ret = -EMSGSIZE; |
| 41 | } |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 46 | static inline int xfrm6_output_one(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | struct dst_entry *dst = skb->dst; |
| 49 | struct xfrm_state *x = dst->xfrm; |
Herbert Xu | ceb1eec | 2007-10-10 15:45:52 -0700 | [diff] [blame] | 50 | struct ipv6hdr *iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | int err; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 52 | |
Herbert Xu | 1399637 | 2007-10-17 21:35:51 -0700 | [diff] [blame] | 53 | if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | err = xfrm6_tunnel_check_size(skb); |
| 55 | if (err) |
| 56 | goto error_nolock; |
| 57 | } |
| 58 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 59 | err = xfrm_output(skb); |
| 60 | if (err) |
| 61 | goto error_nolock; |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 62 | |
Herbert Xu | ceb1eec | 2007-10-10 15:45:52 -0700 | [diff] [blame] | 63 | iph = ipv6_hdr(skb); |
| 64 | iph->payload_len = htons(skb->len - sizeof(*iph)); |
| 65 | |
Patrick McHardy | 3e3850e | 2006-01-06 23:04:54 -0800 | [diff] [blame] | 66 | IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED; |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 67 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | out_exit: |
| 70 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | error_nolock: |
| 72 | kfree_skb(skb); |
| 73 | goto out_exit; |
| 74 | } |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 75 | |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 76 | static int xfrm6_output_finish2(struct sk_buff *skb) |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 77 | { |
| 78 | int err; |
| 79 | |
| 80 | while (likely((err = xfrm6_output_one(skb)) == 0)) { |
| 81 | nf_reset(skb); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 82 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 83 | err = nf_hook(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 84 | skb->dst->dev, dst_output); |
| 85 | if (unlikely(err != 1)) |
| 86 | break; |
| 87 | |
| 88 | if (!skb->dst->xfrm) |
| 89 | return dst_output(skb); |
| 90 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 91 | err = nf_hook(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 92 | skb->dst->dev, xfrm6_output_finish2); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 93 | if (unlikely(err != 1)) |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | return err; |
| 98 | } |
| 99 | |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 100 | static int xfrm6_output_finish(struct sk_buff *skb) |
| 101 | { |
| 102 | struct sk_buff *segs; |
| 103 | |
Herbert Xu | 89114af | 2006-07-08 13:34:32 -0700 | [diff] [blame] | 104 | if (!skb_is_gso(skb)) |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 105 | return xfrm6_output_finish2(skb); |
| 106 | |
Patrick McHardy | 679e898 | 2006-07-30 20:19:11 -0700 | [diff] [blame] | 107 | skb->protocol = htons(ETH_P_IPV6); |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 108 | segs = skb_gso_segment(skb, 0); |
| 109 | kfree_skb(skb); |
| 110 | if (unlikely(IS_ERR(segs))) |
| 111 | return PTR_ERR(segs); |
| 112 | |
| 113 | do { |
| 114 | struct sk_buff *nskb = segs->next; |
| 115 | int err; |
| 116 | |
| 117 | segs->next = NULL; |
| 118 | err = xfrm6_output_finish2(segs); |
| 119 | |
| 120 | if (unlikely(err)) { |
| 121 | while ((segs = nskb)) { |
| 122 | nskb = segs->next; |
| 123 | segs->next = NULL; |
| 124 | kfree_skb(segs); |
| 125 | } |
| 126 | return err; |
| 127 | } |
| 128 | |
| 129 | segs = nskb; |
| 130 | } while (segs); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 135 | int xfrm6_output(struct sk_buff *skb) |
| 136 | { |
| 137 | return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dst->dev, |
| 138 | xfrm6_output_finish); |
| 139 | } |