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