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