Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm_input.c |
| 3 | * |
| 4 | * Changes: |
| 5 | * YOSHIFUJI Hideaki @USAGI |
| 6 | * Split up af-specific portion |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <net/ip.h> |
| 13 | #include <net/xfrm.h> |
| 14 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 15 | static struct kmem_cache *secpath_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | void __secpath_destroy(struct sec_path *sp) |
| 18 | { |
| 19 | int i; |
| 20 | for (i = 0; i < sp->len; i++) |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 21 | xfrm_state_put(sp->xvec[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | kmem_cache_free(secpath_cachep, sp); |
| 23 | } |
| 24 | EXPORT_SYMBOL(__secpath_destroy); |
| 25 | |
| 26 | struct sec_path *secpath_dup(struct sec_path *src) |
| 27 | { |
| 28 | struct sec_path *sp; |
| 29 | |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 30 | sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | if (!sp) |
| 32 | return NULL; |
| 33 | |
| 34 | sp->len = 0; |
| 35 | if (src) { |
| 36 | int i; |
| 37 | |
| 38 | memcpy(sp, src, sizeof(*sp)); |
| 39 | for (i = 0; i < sp->len; i++) |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 40 | xfrm_state_hold(sp->xvec[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
| 42 | atomic_set(&sp->refcnt, 1); |
| 43 | return sp; |
| 44 | } |
| 45 | EXPORT_SYMBOL(secpath_dup); |
| 46 | |
| 47 | /* Fetch spi and seq from ipsec header */ |
| 48 | |
Al Viro | 6067b2b | 2006-09-27 18:47:59 -0700 | [diff] [blame] | 49 | int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | int offset, offset_seq; |
Herbert Xu | 4407250 | 2007-10-17 21:30:34 -0700 | [diff] [blame] | 52 | int hlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | switch (nexthdr) { |
| 55 | case IPPROTO_AH: |
Herbert Xu | 4407250 | 2007-10-17 21:30:34 -0700 | [diff] [blame] | 56 | hlen = sizeof(struct ip_auth_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | offset = offsetof(struct ip_auth_hdr, spi); |
| 58 | offset_seq = offsetof(struct ip_auth_hdr, seq_no); |
| 59 | break; |
| 60 | case IPPROTO_ESP: |
Herbert Xu | 4407250 | 2007-10-17 21:30:34 -0700 | [diff] [blame] | 61 | hlen = sizeof(struct ip_esp_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | offset = offsetof(struct ip_esp_hdr, spi); |
| 63 | offset_seq = offsetof(struct ip_esp_hdr, seq_no); |
| 64 | break; |
| 65 | case IPPROTO_COMP: |
| 66 | if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr))) |
| 67 | return -EINVAL; |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 68 | *spi = htonl(ntohs(*(__be16*)(skb_transport_header(skb) + 2))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | *seq = 0; |
| 70 | return 0; |
| 71 | default: |
| 72 | return 1; |
| 73 | } |
| 74 | |
Herbert Xu | 4407250 | 2007-10-17 21:30:34 -0700 | [diff] [blame] | 75 | if (!pskb_may_pull(skb, hlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | return -EINVAL; |
| 77 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 78 | *spi = *(__be32*)(skb_transport_header(skb) + offset); |
| 79 | *seq = *(__be32*)(skb_transport_header(skb) + offset_seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | EXPORT_SYMBOL(xfrm_parse_spi); |
| 83 | |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame^] | 84 | int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb) |
| 85 | { |
| 86 | int err; |
| 87 | |
| 88 | err = x->outer_mode->afinfo->extract_input(x, skb); |
| 89 | if (err) |
| 90 | return err; |
| 91 | |
| 92 | skb->protocol = x->inner_mode->afinfo->eth_proto; |
| 93 | return x->inner_mode->input2(x, skb); |
| 94 | } |
| 95 | EXPORT_SYMBOL(xfrm_prepare_input); |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | void __init xfrm_input_init(void) |
| 98 | { |
| 99 | secpath_cachep = kmem_cache_create("secpath_cache", |
| 100 | sizeof(struct sec_path), |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 101 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 102 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |