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 | ||
227620e2 HX |
20 | static void xfrm4_beet_make_header(struct sk_buff *skb) |
21 | { | |
22 | struct iphdr *iph = ip_hdr(skb); | |
23 | ||
24 | iph->ihl = 5; | |
25 | iph->version = 4; | |
26 | ||
27 | iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol; | |
28 | iph->tos = XFRM_MODE_SKB_CB(skb)->tos; | |
29 | ||
30 | iph->id = XFRM_MODE_SKB_CB(skb)->id; | |
31 | iph->frag_off = XFRM_MODE_SKB_CB(skb)->frag_off; | |
32 | iph->ttl = XFRM_MODE_SKB_CB(skb)->ttl; | |
33 | } | |
34 | ||
0a69452c DB |
35 | /* Add encapsulation header. |
36 | * | |
37 | * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt. | |
0a69452c DB |
38 | */ |
39 | static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb) | |
40 | { | |
37fedd3a | 41 | struct ip_beet_phdr *ph; |
732c8bd5 | 42 | struct iphdr *top_iph; |
0a69452c DB |
43 | int hdrlen, optlen; |
44 | ||
0a69452c | 45 | hdrlen = 0; |
732c8bd5 | 46 | optlen = XFRM_MODE_SKB_CB(skb)->optlen; |
0a69452c DB |
47 | if (unlikely(optlen)) |
48 | hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4); | |
49 | ||
7b277b1a HX |
50 | skb_set_network_header(skb, IPV4_BEET_PHMAXLEN - x->props.header_len - |
51 | hdrlen); | |
37fedd3a HX |
52 | skb->mac_header = skb->network_header + |
53 | offsetof(struct iphdr, protocol); | |
732c8bd5 | 54 | skb->transport_header = skb->network_header + sizeof(*top_iph); |
37fedd3a | 55 | |
227620e2 HX |
56 | xfrm4_beet_make_header(skb); |
57 | ||
732c8bd5 HX |
58 | ph = (struct ip_beet_phdr *) |
59 | __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdrlen); | |
0a69452c | 60 | |
37fedd3a | 61 | top_iph = ip_hdr(skb); |
36cf9acf | 62 | |
0a69452c | 63 | if (unlikely(optlen)) { |
0a69452c DB |
64 | BUG_ON(optlen < 0); |
65 | ||
0a69452c | 66 | ph->padlen = 4 - (optlen & 4); |
05d22446 | 67 | ph->hdrlen = optlen / 8; |
0a69452c | 68 | ph->nexthdr = top_iph->protocol; |
04fef989 PM |
69 | if (ph->padlen) |
70 | memset(ph + 1, IPOPT_NOP, ph->padlen); | |
0a69452c DB |
71 | |
72 | top_iph->protocol = IPPROTO_BEETPH; | |
73 | top_iph->ihl = sizeof(struct iphdr) / 4; | |
74 | } | |
75 | ||
76 | top_iph->saddr = x->props.saddr.a4; | |
77 | top_iph->daddr = x->id.daddr.a4; | |
78 | ||
79 | return 0; | |
80 | } | |
81 | ||
82 | static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb) | |
83 | { | |
227620e2 | 84 | struct iphdr *iph; |
0a69452c | 85 | int optlen = 0; |
0a69452c DB |
86 | int err = -EINVAL; |
87 | ||
227620e2 | 88 | if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) { |
254d0d24 | 89 | struct ip_beet_phdr *ph; |
227620e2 | 90 | int phlen; |
0a69452c DB |
91 | |
92 | if (!pskb_may_pull(skb, sizeof(*ph))) | |
93 | goto out; | |
227620e2 HX |
94 | |
95 | ph = (struct ip_beet_phdr *)skb->data; | |
0a69452c | 96 | |
d4b1e840 | 97 | phlen = sizeof(*ph) + ph->padlen; |
05d22446 | 98 | optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen); |
0a69452c DB |
99 | if (optlen < 0 || optlen & 3 || optlen > 250) |
100 | goto out; | |
101 | ||
227620e2 | 102 | XFRM_MODE_SKB_CB(skb)->protocol = ph->nexthdr; |
0a69452c | 103 | |
322c8a3c | 104 | if (!pskb_may_pull(skb, phlen)) |
227620e2 HX |
105 | goto out; |
106 | __skb_pull(skb, phlen); | |
0a69452c DB |
107 | } |
108 | ||
227620e2 HX |
109 | skb_push(skb, sizeof(*iph)); |
110 | skb_reset_network_header(skb); | |
111 | ||
112 | memmove(skb->data - skb->mac_len, skb_mac_header(skb), | |
113 | skb->mac_len); | |
114 | skb_set_mac_header(skb, -skb->mac_len); | |
115 | ||
116 | xfrm4_beet_make_header(skb); | |
0a69452c | 117 | |
eddc9ec5 | 118 | iph = ip_hdr(skb); |
227620e2 HX |
119 | |
120 | iph->ihl += optlen / 4; | |
121 | iph->tot_len = htons(skb->len); | |
0a69452c DB |
122 | iph->daddr = x->sel.daddr.a4; |
123 | iph->saddr = x->sel.saddr.a4; | |
0a69452c | 124 | iph->check = 0; |
d56f90a7 | 125 | iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl); |
0a69452c DB |
126 | err = 0; |
127 | out: | |
128 | return err; | |
129 | } | |
130 | ||
131 | static struct xfrm_mode xfrm4_beet_mode = { | |
227620e2 HX |
132 | .input2 = xfrm4_beet_input, |
133 | .input = xfrm_prepare_input, | |
36cf9acf HX |
134 | .output2 = xfrm4_beet_output, |
135 | .output = xfrm4_prepare_output, | |
0a69452c DB |
136 | .owner = THIS_MODULE, |
137 | .encap = XFRM_MODE_BEET, | |
1bfcb10f | 138 | .flags = XFRM_MODE_FLAG_TUNNEL, |
0a69452c DB |
139 | }; |
140 | ||
141 | static int __init xfrm4_beet_init(void) | |
142 | { | |
143 | return xfrm_register_mode(&xfrm4_beet_mode, AF_INET); | |
144 | } | |
145 | ||
146 | static void __exit xfrm4_beet_exit(void) | |
147 | { | |
148 | int err; | |
149 | ||
150 | err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET); | |
151 | BUG_ON(err); | |
152 | } | |
153 | ||
154 | module_init(xfrm4_beet_init); | |
155 | module_exit(xfrm4_beet_exit); | |
156 | MODULE_LICENSE("GPL"); | |
157 | MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET); |