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 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 12 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/skbuff.h> |
| 14 | #include <linux/spinlock.h> |
| 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 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 46 | static 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; |
| 50 | int err; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 51 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 52 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 53 | err = skb_checksum_help(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | if (err) |
| 55 | goto error_nolock; |
| 56 | } |
| 57 | |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 58 | if (x->props.mode == XFRM_MODE_TUNNEL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | err = xfrm6_tunnel_check_size(skb); |
| 60 | if (err) |
| 61 | goto error_nolock; |
| 62 | } |
| 63 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 64 | do { |
| 65 | spin_lock_bh(&x->lock); |
| 66 | err = xfrm_state_check(x, skb); |
| 67 | if (err) |
| 68 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Jamal Hadi Salim | eb878e8 | 2006-08-31 17:42:59 -0700 | [diff] [blame] | 70 | err = x->mode->output(x, skb); |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 71 | if (err) |
| 72 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 74 | err = x->type->output(x, skb); |
| 75 | if (err) |
| 76 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 78 | x->curlft.bytes += skb->len; |
| 79 | x->curlft.packets++; |
Masahide NAKAMURA | 9afaca0 | 2006-08-23 18:20:16 -0700 | [diff] [blame] | 80 | if (x->props.mode == XFRM_MODE_ROUTEOPTIMIZATION) |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 81 | x->lastused = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 83 | spin_unlock_bh(&x->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 85 | skb_reset_network_header(skb); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 86 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 87 | if (!(skb->dst = dst_pop(dst))) { |
| 88 | err = -EHOSTUNREACH; |
| 89 | goto error_nolock; |
| 90 | } |
| 91 | dst = skb->dst; |
| 92 | x = dst->xfrm; |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 93 | } while (x && (x->props.mode != XFRM_MODE_TUNNEL)); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 94 | |
Patrick McHardy | 3e3850e | 2006-01-06 23:04:54 -0800 | [diff] [blame] | 95 | IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED; |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 96 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | out_exit: |
| 99 | return err; |
| 100 | error: |
| 101 | spin_unlock_bh(&x->lock); |
| 102 | error_nolock: |
| 103 | kfree_skb(skb); |
| 104 | goto out_exit; |
| 105 | } |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 106 | |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 107 | static int xfrm6_output_finish2(struct sk_buff *skb) |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 108 | { |
| 109 | int err; |
| 110 | |
| 111 | while (likely((err = xfrm6_output_one(skb)) == 0)) { |
| 112 | nf_reset(skb); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 113 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 114 | err = nf_hook(PF_INET6, NF_IP6_LOCAL_OUT, &skb, NULL, |
| 115 | skb->dst->dev, dst_output); |
| 116 | if (unlikely(err != 1)) |
| 117 | break; |
| 118 | |
| 119 | if (!skb->dst->xfrm) |
| 120 | return dst_output(skb); |
| 121 | |
| 122 | err = nf_hook(PF_INET6, NF_IP6_POST_ROUTING, &skb, NULL, |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 123 | skb->dst->dev, xfrm6_output_finish2); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 124 | if (unlikely(err != 1)) |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | return err; |
| 129 | } |
| 130 | |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 131 | static int xfrm6_output_finish(struct sk_buff *skb) |
| 132 | { |
| 133 | struct sk_buff *segs; |
| 134 | |
Herbert Xu | 89114af | 2006-07-08 13:34:32 -0700 | [diff] [blame] | 135 | if (!skb_is_gso(skb)) |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 136 | return xfrm6_output_finish2(skb); |
| 137 | |
Patrick McHardy | 679e898 | 2006-07-30 20:19:11 -0700 | [diff] [blame] | 138 | skb->protocol = htons(ETH_P_IPV6); |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 139 | segs = skb_gso_segment(skb, 0); |
| 140 | kfree_skb(skb); |
| 141 | if (unlikely(IS_ERR(segs))) |
| 142 | return PTR_ERR(segs); |
| 143 | |
| 144 | do { |
| 145 | struct sk_buff *nskb = segs->next; |
| 146 | int err; |
| 147 | |
| 148 | segs->next = NULL; |
| 149 | err = xfrm6_output_finish2(segs); |
| 150 | |
| 151 | if (unlikely(err)) { |
| 152 | while ((segs = nskb)) { |
| 153 | nskb = segs->next; |
| 154 | segs->next = NULL; |
| 155 | kfree_skb(segs); |
| 156 | } |
| 157 | return err; |
| 158 | } |
| 159 | |
| 160 | segs = nskb; |
| 161 | } while (segs); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 166 | int xfrm6_output(struct sk_buff *skb) |
| 167 | { |
| 168 | return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dst->dev, |
| 169 | xfrm6_output_finish); |
| 170 | } |