Commit | Line | Data |
---|---|---|
0a69452c DB |
1 | /* |
2 | * xfrm4_mode_beet.c - BEET mode encapsulation for IPv4. | |
3 | * | |
4 | * Copyright (c) 2006 Diego Beltrami <diego.beltrami@gmail.com> | |
5 | * Miika Komu <miika@iki.fi> | |
6 | * Herbert Xu <herbert@gondor.apana.org.au> | |
7 | * Abhinav Pathak <abhinav.pathak@hiit.fi> | |
8 | * Jeff Ahrenholz <ahrenholz@gmail.com> | |
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. | |
0a69452c DB |
23 | */ |
24 | static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb) | |
25 | { | |
37fedd3a | 26 | struct ip_beet_phdr *ph; |
ea2f10a3 | 27 | struct iphdr *iph, *top_iph; |
0a69452c DB |
28 | int hdrlen, optlen; |
29 | ||
eddc9ec5 | 30 | iph = ip_hdr(skb); |
0a69452c DB |
31 | |
32 | hdrlen = 0; | |
33 | optlen = iph->ihl * 4 - sizeof(*iph); | |
34 | if (unlikely(optlen)) | |
35 | hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4); | |
36 | ||
7b277b1a HX |
37 | skb_set_network_header(skb, IPV4_BEET_PHMAXLEN - x->props.header_len - |
38 | hdrlen); | |
37fedd3a HX |
39 | skb->mac_header = skb->network_header + |
40 | offsetof(struct iphdr, protocol); | |
41 | skb->transport_header = skb->network_header + sizeof(*iph); | |
42 | ||
43 | ph = (struct ip_beet_phdr *)__skb_pull(skb, sizeof(*iph) - hdrlen); | |
0a69452c | 44 | |
37fedd3a | 45 | top_iph = ip_hdr(skb); |
c5027c9a | 46 | memmove(top_iph, iph, sizeof(*iph)); |
0a69452c | 47 | if (unlikely(optlen)) { |
0a69452c DB |
48 | BUG_ON(optlen < 0); |
49 | ||
0a69452c | 50 | ph->padlen = 4 - (optlen & 4); |
05d22446 | 51 | ph->hdrlen = optlen / 8; |
0a69452c | 52 | ph->nexthdr = top_iph->protocol; |
04fef989 PM |
53 | if (ph->padlen) |
54 | memset(ph + 1, IPOPT_NOP, ph->padlen); | |
0a69452c DB |
55 | |
56 | top_iph->protocol = IPPROTO_BEETPH; | |
57 | top_iph->ihl = sizeof(struct iphdr) / 4; | |
58 | } | |
59 | ||
60 | top_iph->saddr = x->props.saddr.a4; | |
61 | top_iph->daddr = x->id.daddr.a4; | |
62 | ||
63 | return 0; | |
64 | } | |
65 | ||
66 | static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb) | |
67 | { | |
eddc9ec5 | 68 | struct iphdr *iph = ip_hdr(skb); |
0a69452c DB |
69 | int phlen = 0; |
70 | int optlen = 0; | |
ea2f10a3 | 71 | u8 ph_nexthdr = 0; |
0a69452c DB |
72 | int err = -EINVAL; |
73 | ||
0a69452c | 74 | if (unlikely(iph->protocol == IPPROTO_BEETPH)) { |
254d0d24 | 75 | struct ip_beet_phdr *ph; |
0a69452c DB |
76 | |
77 | if (!pskb_may_pull(skb, sizeof(*ph))) | |
78 | goto out; | |
b0061ce4 | 79 | ph = (struct ip_beet_phdr *)(ipip_hdr(skb) + 1); |
0a69452c | 80 | |
d4b1e840 | 81 | phlen = sizeof(*ph) + ph->padlen; |
05d22446 | 82 | optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen); |
0a69452c DB |
83 | if (optlen < 0 || optlen & 3 || optlen > 250) |
84 | goto out; | |
85 | ||
d4b1e840 | 86 | if (!pskb_may_pull(skb, phlen + optlen)) |
0a69452c | 87 | goto out; |
254d0d24 | 88 | skb->len -= phlen + optlen; |
0a69452c DB |
89 | |
90 | ph_nexthdr = ph->nexthdr; | |
91 | } | |
92 | ||
b0061ce4 | 93 | skb_set_network_header(skb, phlen - sizeof(*iph)); |
d56f90a7 | 94 | memmove(skb_network_header(skb), iph, sizeof(*iph)); |
b0061ce4 | 95 | skb_set_transport_header(skb, phlen + optlen); |
239254fe | 96 | skb->data = skb_transport_header(skb); |
0a69452c | 97 | |
eddc9ec5 | 98 | iph = ip_hdr(skb); |
0a69452c | 99 | iph->ihl = (sizeof(*iph) + optlen) / 4; |
d4b1e840 | 100 | iph->tot_len = htons(skb->len + iph->ihl * 4); |
0a69452c DB |
101 | iph->daddr = x->sel.daddr.a4; |
102 | iph->saddr = x->sel.saddr.a4; | |
103 | if (ph_nexthdr) | |
104 | iph->protocol = ph_nexthdr; | |
0a69452c | 105 | iph->check = 0; |
d56f90a7 | 106 | iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl); |
0a69452c DB |
107 | err = 0; |
108 | out: | |
109 | return err; | |
110 | } | |
111 | ||
112 | static struct xfrm_mode xfrm4_beet_mode = { | |
113 | .input = xfrm4_beet_input, | |
114 | .output = xfrm4_beet_output, | |
115 | .owner = THIS_MODULE, | |
116 | .encap = XFRM_MODE_BEET, | |
117 | }; | |
118 | ||
119 | static int __init xfrm4_beet_init(void) | |
120 | { | |
121 | return xfrm_register_mode(&xfrm4_beet_mode, AF_INET); | |
122 | } | |
123 | ||
124 | static void __exit xfrm4_beet_exit(void) | |
125 | { | |
126 | int err; | |
127 | ||
128 | err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET); | |
129 | BUG_ON(err); | |
130 | } | |
131 | ||
132 | module_init(xfrm4_beet_init); | |
133 | module_exit(xfrm4_beet_exit); | |
134 | MODULE_LICENSE("GPL"); | |
135 | MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET); |