]>
Commit | Line | Data |
---|---|---|
0a69452c DB |
1 | /* |
2 | * xfrm4_mode_beet.c - BEET mode encapsulation for IPv4. | |
3 | * | |
4 | * Copyright (c) 2006 Diego Beltrami <[email protected]> | |
5 | * Miika Komu <[email protected]> | |
6 | * Herbert Xu <[email protected]> | |
7 | * Abhinav Pathak <[email protected]> | |
8 | * Jeff Ahrenholz <[email protected]> | |
9 | */ | |
10 | ||
11 | #include <linux/init.h> | |
12 | #include <linux/kernel.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/stringify.h> | |
16 | #include <net/dst.h> | |
17 | #include <net/ip.h> | |
18 | #include <net/xfrm.h> | |
19 | ||
20 | /* Add encapsulation header. | |
21 | * | |
22 | * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt. | |
23 | * The following fields in it shall be filled in by x->type->output: | |
24 | * tot_len | |
25 | * check | |
26 | * | |
27 | * On exit, skb->h will be set to the start of the payload to be processed | |
28 | * by x->type->output and skb->nh will be set to the top IP header. | |
29 | */ | |
30 | static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb) | |
31 | { | |
ea2f10a3 | 32 | struct iphdr *iph, *top_iph; |
0a69452c DB |
33 | int hdrlen, optlen; |
34 | ||
eddc9ec5 | 35 | iph = ip_hdr(skb); |
b0e380b1 | 36 | skb->transport_header = skb->network_header; |
0a69452c DB |
37 | |
38 | hdrlen = 0; | |
39 | optlen = iph->ihl * 4 - sizeof(*iph); | |
40 | if (unlikely(optlen)) | |
41 | hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4); | |
42 | ||
ac758e3c | 43 | skb_push(skb, x->props.header_len - IPV4_BEET_PHMAXLEN + hdrlen); |
e2d1bca7 | 44 | skb_reset_network_header(skb); |
eddc9ec5 | 45 | top_iph = ip_hdr(skb); |
b0e380b1 | 46 | skb->transport_header += sizeof(*iph) - hdrlen; |
0a69452c | 47 | |
c5027c9a | 48 | memmove(top_iph, iph, sizeof(*iph)); |
0a69452c DB |
49 | if (unlikely(optlen)) { |
50 | struct ip_beet_phdr *ph; | |
51 | ||
52 | BUG_ON(optlen < 0); | |
53 | ||
9c70220b | 54 | ph = (struct ip_beet_phdr *)skb_transport_header(skb); |
0a69452c | 55 | ph->padlen = 4 - (optlen & 4); |
05d22446 | 56 | ph->hdrlen = optlen / 8; |
0a69452c | 57 | ph->nexthdr = top_iph->protocol; |
04fef989 PM |
58 | if (ph->padlen) |
59 | memset(ph + 1, IPOPT_NOP, ph->padlen); | |
0a69452c DB |
60 | |
61 | top_iph->protocol = IPPROTO_BEETPH; | |
62 | top_iph->ihl = sizeof(struct iphdr) / 4; | |
63 | } | |
64 | ||
65 | top_iph->saddr = x->props.saddr.a4; | |
66 | top_iph->daddr = x->id.daddr.a4; | |
67 | ||
68 | return 0; | |
69 | } | |
70 | ||
71 | static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb) | |
72 | { | |
eddc9ec5 | 73 | struct iphdr *iph = ip_hdr(skb); |
0a69452c DB |
74 | int phlen = 0; |
75 | int optlen = 0; | |
ea2f10a3 | 76 | u8 ph_nexthdr = 0; |
0a69452c DB |
77 | int err = -EINVAL; |
78 | ||
0a69452c | 79 | if (unlikely(iph->protocol == IPPROTO_BEETPH)) { |
254d0d24 | 80 | struct ip_beet_phdr *ph; |
0a69452c DB |
81 | |
82 | if (!pskb_may_pull(skb, sizeof(*ph))) | |
83 | goto out; | |
b0061ce4 | 84 | ph = (struct ip_beet_phdr *)(ipip_hdr(skb) + 1); |
0a69452c | 85 | |
d4b1e840 | 86 | phlen = sizeof(*ph) + ph->padlen; |
05d22446 | 87 | optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen); |
0a69452c DB |
88 | if (optlen < 0 || optlen & 3 || optlen > 250) |
89 | goto out; | |
90 | ||
d4b1e840 | 91 | if (!pskb_may_pull(skb, phlen + optlen)) |
0a69452c | 92 | goto out; |
254d0d24 | 93 | skb->len -= phlen + optlen; |
0a69452c DB |
94 | |
95 | ph_nexthdr = ph->nexthdr; | |
96 | } | |
97 | ||
b0061ce4 | 98 | skb_set_network_header(skb, phlen - sizeof(*iph)); |
d56f90a7 | 99 | memmove(skb_network_header(skb), iph, sizeof(*iph)); |
b0061ce4 | 100 | skb_set_transport_header(skb, phlen + optlen); |
239254fe | 101 | skb->data = skb_transport_header(skb); |
0a69452c | 102 | |
eddc9ec5 | 103 | iph = ip_hdr(skb); |
0a69452c | 104 | iph->ihl = (sizeof(*iph) + optlen) / 4; |
d4b1e840 | 105 | iph->tot_len = htons(skb->len + iph->ihl * 4); |
0a69452c DB |
106 | iph->daddr = x->sel.daddr.a4; |
107 | iph->saddr = x->sel.saddr.a4; | |
108 | if (ph_nexthdr) | |
109 | iph->protocol = ph_nexthdr; | |
0a69452c | 110 | iph->check = 0; |
d56f90a7 | 111 | iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl); |
0a69452c DB |
112 | err = 0; |
113 | out: | |
114 | return err; | |
115 | } | |
116 | ||
117 | static struct xfrm_mode xfrm4_beet_mode = { | |
118 | .input = xfrm4_beet_input, | |
119 | .output = xfrm4_beet_output, | |
120 | .owner = THIS_MODULE, | |
121 | .encap = XFRM_MODE_BEET, | |
122 | }; | |
123 | ||
124 | static int __init xfrm4_beet_init(void) | |
125 | { | |
126 | return xfrm_register_mode(&xfrm4_beet_mode, AF_INET); | |
127 | } | |
128 | ||
129 | static void __exit xfrm4_beet_exit(void) | |
130 | { | |
131 | int err; | |
132 | ||
133 | err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET); | |
134 | BUG_ON(err); | |
135 | } | |
136 | ||
137 | module_init(xfrm4_beet_init); | |
138 | module_exit(xfrm4_beet_exit); | |
139 | MODULE_LICENSE("GPL"); | |
140 | MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET); |