Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * xfrm_input.c | |
3 | * | |
4 | * Changes: | |
5 | * YOSHIFUJI Hideaki @USAGI | |
6 | * Split up af-specific portion | |
a716c119 | 7 | * |
1da177e4 LT |
8 | */ |
9 | ||
10 | #include <linux/slab.h> | |
11 | #include <linux/module.h> | |
12 | #include <net/ip.h> | |
13 | #include <net/xfrm.h> | |
14 | ||
e18b890b | 15 | static struct kmem_cache *secpath_cachep __read_mostly; |
1da177e4 LT |
16 | |
17 | void __secpath_destroy(struct sec_path *sp) | |
18 | { | |
19 | int i; | |
20 | for (i = 0; i < sp->len; i++) | |
dbe5b4aa | 21 | xfrm_state_put(sp->xvec[i]); |
1da177e4 LT |
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 | ||
54e6ecb2 | 30 | sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC); |
1da177e4 LT |
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++) | |
dbe5b4aa | 40 | xfrm_state_hold(sp->xvec[i]); |
1da177e4 LT |
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 | ||
6067b2ba | 49 | int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq) |
1da177e4 LT |
50 | { |
51 | int offset, offset_seq; | |
44072500 | 52 | int hlen; |
1da177e4 LT |
53 | |
54 | switch (nexthdr) { | |
55 | case IPPROTO_AH: | |
44072500 | 56 | hlen = sizeof(struct ip_auth_hdr); |
1da177e4 LT |
57 | offset = offsetof(struct ip_auth_hdr, spi); |
58 | offset_seq = offsetof(struct ip_auth_hdr, seq_no); | |
59 | break; | |
60 | case IPPROTO_ESP: | |
44072500 | 61 | hlen = sizeof(struct ip_esp_hdr); |
1da177e4 LT |
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; | |
9c70220b | 68 | *spi = htonl(ntohs(*(__be16*)(skb_transport_header(skb) + 2))); |
1da177e4 LT |
69 | *seq = 0; |
70 | return 0; | |
71 | default: | |
72 | return 1; | |
73 | } | |
74 | ||
44072500 | 75 | if (!pskb_may_pull(skb, hlen)) |
1da177e4 LT |
76 | return -EINVAL; |
77 | ||
9c70220b ACM |
78 | *spi = *(__be32*)(skb_transport_header(skb) + offset); |
79 | *seq = *(__be32*)(skb_transport_header(skb) + offset_seq); | |
1da177e4 LT |
80 | return 0; |
81 | } | |
82 | EXPORT_SYMBOL(xfrm_parse_spi); | |
83 | ||
84 | void __init xfrm_input_init(void) | |
85 | { | |
86 | secpath_cachep = kmem_cache_create("secpath_cache", | |
87 | sizeof(struct sec_path), | |
e5d679f3 | 88 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
20c2df83 | 89 | NULL); |
1da177e4 | 90 | } |