Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm_output.c - Common IPsec encapsulation code. |
| 3 | * |
| 4 | * Copyright (c) 2007 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 | |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/netdevice.h> |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 15 | #include <linux/netfilter.h> |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 16 | #include <linux/skbuff.h> |
| 17 | #include <linux/spinlock.h> |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 18 | #include <net/dst.h> |
| 19 | #include <net/xfrm.h> |
| 20 | |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 21 | static int xfrm_output2(struct sk_buff *skb); |
| 22 | |
Herbert Xu | 83815de | 2007-10-08 17:25:08 -0700 | [diff] [blame] | 23 | static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb) |
| 24 | { |
Herbert Xu | 550ade8 | 2007-11-13 21:33:01 -0800 | [diff] [blame] | 25 | struct dst_entry *dst = skb->dst; |
| 26 | int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev) |
Herbert Xu | 83815de | 2007-10-08 17:25:08 -0700 | [diff] [blame] | 27 | - skb_headroom(skb); |
| 28 | |
| 29 | if (nhead > 0) |
| 30 | return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC); |
| 31 | |
| 32 | /* Check tail too... */ |
| 33 | return 0; |
| 34 | } |
| 35 | |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 36 | static int xfrm_output_one(struct sk_buff *skb, int err) |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 37 | { |
| 38 | struct dst_entry *dst = skb->dst; |
| 39 | struct xfrm_state *x = dst->xfrm; |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 40 | |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 41 | if (err <= 0) |
| 42 | goto resume; |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 43 | |
| 44 | do { |
Herbert Xu | 910ef70 | 2007-12-18 22:14:25 -0800 | [diff] [blame] | 45 | err = xfrm_state_check_space(x, skb); |
| 46 | if (err) |
| 47 | goto error_nolock; |
| 48 | |
Herbert Xu | a2deb6d2 | 2007-11-13 21:39:38 -0800 | [diff] [blame] | 49 | err = x->outer_mode->output(x, skb); |
| 50 | if (err) |
Herbert Xu | 910ef70 | 2007-12-18 22:14:25 -0800 | [diff] [blame] | 51 | goto error_nolock; |
Herbert Xu | a2deb6d2 | 2007-11-13 21:39:38 -0800 | [diff] [blame] | 52 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 53 | spin_lock_bh(&x->lock); |
Herbert Xu | 910ef70 | 2007-12-18 22:14:25 -0800 | [diff] [blame] | 54 | err = xfrm_state_check_expire(x); |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 55 | if (err) |
| 56 | goto error; |
| 57 | |
Herbert Xu | 436a0a4 | 2007-10-08 17:25:53 -0700 | [diff] [blame] | 58 | if (x->type->flags & XFRM_TYPE_REPLAY_PROT) { |
| 59 | XFRM_SKB_CB(skb)->seq = ++x->replay.oseq; |
Herbert Xu | cdf7e66 | 2007-10-08 17:26:34 -0700 | [diff] [blame] | 60 | if (xfrm_aevent_is_on()) |
| 61 | xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); |
Herbert Xu | 436a0a4 | 2007-10-08 17:25:53 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 64 | x->curlft.bytes += skb->len; |
| 65 | x->curlft.packets++; |
| 66 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 67 | spin_unlock_bh(&x->lock); |
| 68 | |
Herbert Xu | b7c6538c | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 69 | err = x->type->output(x, skb); |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 70 | |
| 71 | resume: |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame^] | 72 | if (err) { |
| 73 | XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEPROTOERROR); |
Herbert Xu | b7c6538c | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 74 | goto error_nolock; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame^] | 75 | } |
Herbert Xu | b7c6538c | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 76 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 77 | if (!(skb->dst = dst_pop(dst))) { |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame^] | 78 | XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR); |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 79 | err = -EHOSTUNREACH; |
| 80 | goto error_nolock; |
| 81 | } |
| 82 | dst = skb->dst; |
| 83 | x = dst->xfrm; |
Herbert Xu | 1399637 | 2007-10-17 21:35:51 -0700 | [diff] [blame] | 84 | } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL)); |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 85 | |
| 86 | err = 0; |
| 87 | |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 88 | out_exit: |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 89 | return err; |
| 90 | error: |
| 91 | spin_unlock_bh(&x->lock); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 92 | error_nolock: |
| 93 | kfree_skb(skb); |
| 94 | goto out_exit; |
| 95 | } |
| 96 | |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 97 | int xfrm_output_resume(struct sk_buff *skb, int err) |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 98 | { |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 99 | while (likely((err = xfrm_output_one(skb, err)) == 0)) { |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 100 | struct xfrm_state *x; |
| 101 | |
| 102 | nf_reset(skb); |
| 103 | |
| 104 | err = skb->dst->ops->local_out(skb); |
| 105 | if (unlikely(err != 1)) |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 106 | goto out; |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 107 | |
| 108 | x = skb->dst->xfrm; |
| 109 | if (!x) |
| 110 | return dst_output(skb); |
| 111 | |
| 112 | err = nf_hook(x->inner_mode->afinfo->family, |
David S. Miller | 294b4ba | 2007-11-14 01:57:47 -0800 | [diff] [blame] | 113 | NF_INET_POST_ROUTING, skb, |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 114 | NULL, skb->dst->dev, xfrm_output2); |
| 115 | if (unlikely(err != 1)) |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 116 | goto out; |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 119 | if (err == -EINPROGRESS) |
| 120 | err = 0; |
| 121 | |
| 122 | out: |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 123 | return err; |
| 124 | } |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 125 | EXPORT_SYMBOL_GPL(xfrm_output_resume); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 126 | |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 127 | static int xfrm_output2(struct sk_buff *skb) |
| 128 | { |
| 129 | return xfrm_output_resume(skb, 1); |
| 130 | } |
| 131 | |
| 132 | static int xfrm_output_gso(struct sk_buff *skb) |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 133 | { |
| 134 | struct sk_buff *segs; |
| 135 | |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 136 | segs = skb_gso_segment(skb, 0); |
| 137 | kfree_skb(skb); |
| 138 | if (unlikely(IS_ERR(segs))) |
| 139 | return PTR_ERR(segs); |
| 140 | |
| 141 | do { |
| 142 | struct sk_buff *nskb = segs->next; |
| 143 | int err; |
| 144 | |
| 145 | segs->next = NULL; |
| 146 | err = xfrm_output2(segs); |
| 147 | |
| 148 | if (unlikely(err)) { |
| 149 | while ((segs = nskb)) { |
| 150 | nskb = segs->next; |
| 151 | segs->next = NULL; |
| 152 | kfree_skb(segs); |
| 153 | } |
| 154 | return err; |
| 155 | } |
| 156 | |
| 157 | segs = nskb; |
| 158 | } while (segs); |
| 159 | |
| 160 | return 0; |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 161 | } |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 162 | |
| 163 | int xfrm_output(struct sk_buff *skb) |
| 164 | { |
| 165 | int err; |
| 166 | |
| 167 | if (skb_is_gso(skb)) |
| 168 | return xfrm_output_gso(skb); |
| 169 | |
| 170 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 171 | err = skb_checksum_help(skb); |
| 172 | if (err) { |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame^] | 173 | XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR); |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 174 | kfree_skb(skb); |
| 175 | return err; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return xfrm_output2(skb); |
| 180 | } |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 181 | EXPORT_SYMBOL_GPL(xfrm_output); |