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> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/time.h> |
| 18 | #include <net/dst.h> |
| 19 | #include <net/xfrm.h> |
| 20 | |
Herbert Xu | 83815de | 2007-10-08 17:25:08 -0700 | [diff] [blame] | 21 | static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb) |
| 22 | { |
| 23 | int nhead = x->props.header_len + LL_RESERVED_SPACE(skb->dst->dev) |
| 24 | - skb_headroom(skb); |
| 25 | |
| 26 | if (nhead > 0) |
| 27 | return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC); |
| 28 | |
| 29 | /* Check tail too... */ |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | static int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb) |
| 34 | { |
| 35 | int err = xfrm_state_check_expire(x); |
| 36 | if (err < 0) |
| 37 | goto err; |
| 38 | err = xfrm_state_check_space(x, skb); |
| 39 | err: |
| 40 | return err; |
| 41 | } |
| 42 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 43 | int xfrm_output(struct sk_buff *skb) |
| 44 | { |
| 45 | struct dst_entry *dst = skb->dst; |
| 46 | struct xfrm_state *x = dst->xfrm; |
| 47 | int err; |
| 48 | |
| 49 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 50 | err = skb_checksum_help(skb); |
| 51 | if (err) |
| 52 | goto error_nolock; |
| 53 | } |
| 54 | |
| 55 | do { |
| 56 | spin_lock_bh(&x->lock); |
| 57 | err = xfrm_state_check(x, skb); |
| 58 | if (err) |
| 59 | goto error; |
| 60 | |
Herbert Xu | 436a0a4 | 2007-10-08 17:25:53 -0700 | [diff] [blame] | 61 | if (x->type->flags & XFRM_TYPE_REPLAY_PROT) { |
| 62 | XFRM_SKB_CB(skb)->seq = ++x->replay.oseq; |
Herbert Xu | cdf7e66 | 2007-10-08 17:26:34 -0700 | [diff] [blame^] | 63 | if (xfrm_aevent_is_on()) |
| 64 | xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); |
Herbert Xu | 436a0a4 | 2007-10-08 17:25:53 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 67 | err = x->mode->output(x, skb); |
| 68 | if (err) |
| 69 | goto error; |
| 70 | |
| 71 | err = x->type->output(x, skb); |
| 72 | if (err) |
| 73 | goto error; |
| 74 | |
| 75 | x->curlft.bytes += skb->len; |
| 76 | x->curlft.packets++; |
| 77 | |
| 78 | if (x->props.mode == XFRM_MODE_ROUTEOPTIMIZATION) |
| 79 | x->lastused = get_seconds(); |
| 80 | |
| 81 | spin_unlock_bh(&x->lock); |
| 82 | |
| 83 | skb_reset_network_header(skb); |
| 84 | |
| 85 | if (!(skb->dst = dst_pop(dst))) { |
| 86 | err = -EHOSTUNREACH; |
| 87 | goto error_nolock; |
| 88 | } |
| 89 | dst = skb->dst; |
| 90 | x = dst->xfrm; |
| 91 | } while (x && (x->props.mode != XFRM_MODE_TUNNEL)); |
| 92 | |
| 93 | err = 0; |
| 94 | |
| 95 | error_nolock: |
| 96 | return err; |
| 97 | error: |
| 98 | spin_unlock_bh(&x->lock); |
| 99 | goto error_nolock; |
| 100 | } |
| 101 | EXPORT_SYMBOL_GPL(xfrm_output); |