Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
afd46503 JP |
2 | #define pr_fmt(fmt) "IPsec: " fmt |
3 | ||
dff3bb06 | 4 | #include <crypto/hash.h> |
c66218cc | 5 | #include <crypto/utils.h> |
07d4ee58 | 6 | #include <linux/err.h> |
1da177e4 | 7 | #include <linux/module.h> |
5a0e3ad6 | 8 | #include <linux/slab.h> |
1da177e4 LT |
9 | #include <net/ip.h> |
10 | #include <net/xfrm.h> | |
11 | #include <net/ah.h> | |
12 | #include <linux/crypto.h> | |
13 | #include <linux/pfkeyv2.h> | |
dff3bb06 | 14 | #include <linux/scatterlist.h> |
1da177e4 | 15 | #include <net/icmp.h> |
14c85021 | 16 | #include <net/protocol.h> |
1da177e4 | 17 | |
dff3bb06 SK |
18 | struct ah_skb_cb { |
19 | struct xfrm_skb_cb xfrm; | |
20 | void *tmp; | |
21 | }; | |
22 | ||
23 | #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0])) | |
24 | ||
25 | static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags, | |
26 | unsigned int size) | |
27 | { | |
28 | unsigned int len; | |
29 | ||
e77f5dd7 | 30 | len = size + crypto_ahash_digestsize(ahash); |
dff3bb06 SK |
31 | |
32 | len = ALIGN(len, crypto_tfm_ctx_alignment()); | |
33 | ||
34 | len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash); | |
35 | len = ALIGN(len, __alignof__(struct scatterlist)); | |
36 | ||
37 | len += sizeof(struct scatterlist) * nfrags; | |
38 | ||
39 | return kmalloc(len, GFP_ATOMIC); | |
40 | } | |
41 | ||
42 | static inline u8 *ah_tmp_auth(void *tmp, unsigned int offset) | |
43 | { | |
44 | return tmp + offset; | |
45 | } | |
46 | ||
e77f5dd7 | 47 | static inline u8 *ah_tmp_icv(void *tmp, unsigned int offset) |
dff3bb06 | 48 | { |
e77f5dd7 | 49 | return tmp + offset; |
dff3bb06 SK |
50 | } |
51 | ||
52 | static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash, | |
53 | u8 *icv) | |
54 | { | |
55 | struct ahash_request *req; | |
56 | ||
57 | req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash), | |
58 | crypto_tfm_ctx_alignment()); | |
59 | ||
60 | ahash_request_set_tfm(req, ahash); | |
61 | ||
62 | return req; | |
63 | } | |
64 | ||
65 | static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash, | |
66 | struct ahash_request *req) | |
67 | { | |
68 | return (void *)ALIGN((unsigned long)(req + 1) + | |
69 | crypto_ahash_reqsize(ahash), | |
70 | __alignof__(struct scatterlist)); | |
71 | } | |
1da177e4 LT |
72 | |
73 | /* Clear mutable options and find final destination to substitute | |
74 | * into IP header for icv calculation. Options are already checked | |
75 | * for validity, so paranoia is not required. */ | |
76 | ||
b71d1d42 | 77 | static int ip_clear_mutable_options(const struct iphdr *iph, __be32 *daddr) |
1da177e4 | 78 | { |
5e73ea1a | 79 | unsigned char *optptr = (unsigned char *)(iph+1); |
1da177e4 LT |
80 | int l = iph->ihl*4 - sizeof(struct iphdr); |
81 | int optlen; | |
82 | ||
83 | while (l > 0) { | |
84 | switch (*optptr) { | |
85 | case IPOPT_END: | |
86 | return 0; | |
87 | case IPOPT_NOOP: | |
88 | l--; | |
89 | optptr++; | |
90 | continue; | |
91 | } | |
92 | optlen = optptr[1]; | |
93 | if (optlen<2 || optlen>l) | |
94 | return -EINVAL; | |
95 | switch (*optptr) { | |
96 | case IPOPT_SEC: | |
97 | case 0x85: /* Some "Extended Security" crap. */ | |
11a03f78 | 98 | case IPOPT_CIPSO: |
1da177e4 LT |
99 | case IPOPT_RA: |
100 | case 0x80|21: /* RFC1770 */ | |
101 | break; | |
102 | case IPOPT_LSRR: | |
103 | case IPOPT_SSRR: | |
104 | if (optlen < 6) | |
105 | return -EINVAL; | |
106 | memcpy(daddr, optptr+optlen-4, 4); | |
a8eceea8 | 107 | fallthrough; |
1da177e4 | 108 | default: |
96fe1c02 | 109 | memset(optptr, 0, optlen); |
1da177e4 LT |
110 | } |
111 | l -= optlen; | |
112 | optptr += optlen; | |
113 | } | |
114 | return 0; | |
115 | } | |
116 | ||
fd5dabf7 | 117 | static void ah_output_done(void *data, int err) |
dff3bb06 SK |
118 | { |
119 | u8 *icv; | |
120 | struct iphdr *iph; | |
fd5dabf7 | 121 | struct sk_buff *skb = data; |
dff3bb06 SK |
122 | struct xfrm_state *x = skb_dst(skb)->xfrm; |
123 | struct ah_data *ahp = x->data; | |
124 | struct iphdr *top_iph = ip_hdr(skb); | |
125 | struct ip_auth_hdr *ah = ip_auth_hdr(skb); | |
126 | int ihl = ip_hdrlen(skb); | |
127 | ||
128 | iph = AH_SKB_CB(skb)->tmp; | |
e77f5dd7 | 129 | icv = ah_tmp_icv(iph, ihl); |
dff3bb06 SK |
130 | memcpy(ah->auth_data, icv, ahp->icv_trunc_len); |
131 | ||
132 | top_iph->tos = iph->tos; | |
133 | top_iph->ttl = iph->ttl; | |
134 | top_iph->frag_off = iph->frag_off; | |
135 | if (top_iph->ihl != 5) { | |
136 | top_iph->daddr = iph->daddr; | |
137 | memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); | |
138 | } | |
139 | ||
dff3bb06 | 140 | kfree(AH_SKB_CB(skb)->tmp); |
9ab1265d | 141 | xfrm_output_resume(skb->sk, skb, err); |
dff3bb06 SK |
142 | } |
143 | ||
1da177e4 LT |
144 | static int ah_output(struct xfrm_state *x, struct sk_buff *skb) |
145 | { | |
146 | int err; | |
dff3bb06 SK |
147 | int nfrags; |
148 | int ihl; | |
149 | u8 *icv; | |
150 | struct sk_buff *trailer; | |
151 | struct crypto_ahash *ahash; | |
152 | struct ahash_request *req; | |
153 | struct scatterlist *sg; | |
1da177e4 LT |
154 | struct iphdr *iph, *top_iph; |
155 | struct ip_auth_hdr *ah; | |
156 | struct ah_data *ahp; | |
d4d573d0 FD |
157 | int seqhi_len = 0; |
158 | __be32 *seqhi; | |
159 | int sglists = 0; | |
160 | struct scatterlist *seqhisg; | |
dff3bb06 SK |
161 | |
162 | ahp = x->data; | |
163 | ahash = ahp->ahash; | |
164 | ||
165 | if ((err = skb_cow_data(skb, 0, &trailer)) < 0) | |
166 | goto out; | |
167 | nfrags = err; | |
1da177e4 | 168 | |
7b277b1a | 169 | skb_push(skb, -skb_network_offset(skb)); |
dff3bb06 SK |
170 | ah = ip_auth_hdr(skb); |
171 | ihl = ip_hdrlen(skb); | |
172 | ||
d4d573d0 FD |
173 | if (x->props.flags & XFRM_STATE_ESN) { |
174 | sglists = 1; | |
175 | seqhi_len = sizeof(*seqhi); | |
176 | } | |
dff3bb06 | 177 | err = -ENOMEM; |
d4d573d0 | 178 | iph = ah_alloc_tmp(ahash, nfrags + sglists, ihl + seqhi_len); |
dff3bb06 SK |
179 | if (!iph) |
180 | goto out; | |
d4d573d0 | 181 | seqhi = (__be32 *)((char *)iph + ihl); |
e77f5dd7 | 182 | icv = ah_tmp_icv(seqhi, seqhi_len); |
dff3bb06 SK |
183 | req = ah_tmp_req(ahash, icv); |
184 | sg = ah_req_sg(ahash, req); | |
d4d573d0 | 185 | seqhisg = sg + nfrags; |
dff3bb06 SK |
186 | |
187 | memset(ah->auth_data, 0, ahp->icv_trunc_len); | |
188 | ||
eddc9ec5 | 189 | top_iph = ip_hdr(skb); |
1da177e4 LT |
190 | |
191 | iph->tos = top_iph->tos; | |
192 | iph->ttl = top_iph->ttl; | |
193 | iph->frag_off = top_iph->frag_off; | |
194 | ||
195 | if (top_iph->ihl != 5) { | |
196 | iph->daddr = top_iph->daddr; | |
197 | memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); | |
198 | err = ip_clear_mutable_options(top_iph, &top_iph->daddr); | |
199 | if (err) | |
dff3bb06 | 200 | goto out_free; |
1da177e4 LT |
201 | } |
202 | ||
37fedd3a HX |
203 | ah->nexthdr = *skb_mac_header(skb); |
204 | *skb_mac_header(skb) = IPPROTO_AH; | |
1da177e4 LT |
205 | |
206 | top_iph->tos = 0; | |
207 | top_iph->tot_len = htons(skb->len); | |
208 | top_iph->frag_off = 0; | |
209 | top_iph->ttl = 0; | |
1da177e4 LT |
210 | top_iph->check = 0; |
211 | ||
fa9921e4 ND |
212 | if (x->props.flags & XFRM_STATE_ALIGN4) |
213 | ah->hdrlen = (XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2; | |
214 | else | |
215 | ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2; | |
1da177e4 LT |
216 | |
217 | ah->reserved = 0; | |
218 | ah->spi = x->id.spi; | |
1ce3644a | 219 | ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low); |
b7c6538c | 220 | |
d4d573d0 | 221 | sg_init_table(sg, nfrags + sglists); |
3f297707 JD |
222 | err = skb_to_sgvec_nomark(skb, sg, 0, skb->len); |
223 | if (unlikely(err < 0)) | |
224 | goto out_free; | |
b7c6538c | 225 | |
d4d573d0 FD |
226 | if (x->props.flags & XFRM_STATE_ESN) { |
227 | /* Attach seqhi sg right after packet payload */ | |
228 | *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi); | |
229 | sg_set_buf(seqhisg, seqhi, seqhi_len); | |
230 | } | |
231 | ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len); | |
dff3bb06 SK |
232 | ahash_request_set_callback(req, 0, ah_output_done, skb); |
233 | ||
234 | AH_SKB_CB(skb)->tmp = iph; | |
235 | ||
236 | err = crypto_ahash_digest(req); | |
237 | if (err) { | |
238 | if (err == -EINPROGRESS) | |
239 | goto out; | |
240 | ||
068c2e70 | 241 | if (err == -ENOSPC) |
dff3bb06 SK |
242 | err = NET_XMIT_DROP; |
243 | goto out_free; | |
244 | } | |
245 | ||
246 | memcpy(ah->auth_data, icv, ahp->icv_trunc_len); | |
1da177e4 LT |
247 | |
248 | top_iph->tos = iph->tos; | |
249 | top_iph->ttl = iph->ttl; | |
250 | top_iph->frag_off = iph->frag_off; | |
251 | if (top_iph->ihl != 5) { | |
252 | top_iph->daddr = iph->daddr; | |
253 | memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); | |
254 | } | |
255 | ||
dff3bb06 SK |
256 | out_free: |
257 | kfree(iph); | |
258 | out: | |
1da177e4 LT |
259 | return err; |
260 | } | |
261 | ||
fd5dabf7 | 262 | static void ah_input_done(void *data, int err) |
dff3bb06 SK |
263 | { |
264 | u8 *auth_data; | |
265 | u8 *icv; | |
266 | struct iphdr *work_iph; | |
fd5dabf7 | 267 | struct sk_buff *skb = data; |
dff3bb06 SK |
268 | struct xfrm_state *x = xfrm_input_state(skb); |
269 | struct ah_data *ahp = x->data; | |
270 | struct ip_auth_hdr *ah = ip_auth_hdr(skb); | |
271 | int ihl = ip_hdrlen(skb); | |
272 | int ah_hlen = (ah->hdrlen + 2) << 2; | |
273 | ||
ebd89a2d GBY |
274 | if (err) |
275 | goto out; | |
276 | ||
dff3bb06 SK |
277 | work_iph = AH_SKB_CB(skb)->tmp; |
278 | auth_data = ah_tmp_auth(work_iph, ihl); | |
e77f5dd7 | 279 | icv = ah_tmp_icv(auth_data, ahp->icv_trunc_len); |
dff3bb06 | 280 | |
67df58a3 | 281 | err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0; |
dff3bb06 SK |
282 | if (err) |
283 | goto out; | |
284 | ||
b7ea81a5 NB |
285 | err = ah->nexthdr; |
286 | ||
dff3bb06 SK |
287 | skb->network_header += ah_hlen; |
288 | memcpy(skb_network_header(skb), work_iph, ihl); | |
289 | __skb_pull(skb, ah_hlen + ihl); | |
7143dfac LR |
290 | |
291 | if (x->props.mode == XFRM_MODE_TUNNEL) | |
292 | skb_reset_transport_header(skb); | |
293 | else | |
294 | skb_set_transport_header(skb, -ihl); | |
dff3bb06 SK |
295 | out: |
296 | kfree(AH_SKB_CB(skb)->tmp); | |
297 | xfrm_input_resume(skb, err); | |
298 | } | |
299 | ||
e695633e | 300 | static int ah_input(struct xfrm_state *x, struct sk_buff *skb) |
1da177e4 LT |
301 | { |
302 | int ah_hlen; | |
31a4ab93 | 303 | int ihl; |
631a6698 | 304 | int nexthdr; |
dff3bb06 SK |
305 | int nfrags; |
306 | u8 *auth_data; | |
307 | u8 *icv; | |
308 | struct sk_buff *trailer; | |
309 | struct crypto_ahash *ahash; | |
310 | struct ahash_request *req; | |
311 | struct scatterlist *sg; | |
312 | struct iphdr *iph, *work_iph; | |
1da177e4 LT |
313 | struct ip_auth_hdr *ah; |
314 | struct ah_data *ahp; | |
dff3bb06 | 315 | int err = -ENOMEM; |
d8b2a860 FD |
316 | int seqhi_len = 0; |
317 | __be32 *seqhi; | |
318 | int sglists = 0; | |
319 | struct scatterlist *seqhisg; | |
1da177e4 | 320 | |
87bdc48d | 321 | if (!pskb_may_pull(skb, sizeof(*ah))) |
1da177e4 LT |
322 | goto out; |
323 | ||
87bdc48d | 324 | ah = (struct ip_auth_hdr *)skb->data; |
1da177e4 | 325 | ahp = x->data; |
dff3bb06 SK |
326 | ahash = ahp->ahash; |
327 | ||
631a6698 | 328 | nexthdr = ah->nexthdr; |
1da177e4 | 329 | ah_hlen = (ah->hdrlen + 2) << 2; |
e905a9ed | 330 | |
fa9921e4 ND |
331 | if (x->props.flags & XFRM_STATE_ALIGN4) { |
332 | if (ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_full_len) && | |
333 | ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len)) | |
334 | goto out; | |
335 | } else { | |
336 | if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) && | |
337 | ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len)) | |
338 | goto out; | |
339 | } | |
1da177e4 LT |
340 | |
341 | if (!pskb_may_pull(skb, ah_hlen)) | |
342 | goto out; | |
343 | ||
344 | /* We are going to _remove_ AH header to keep sockets happy, | |
345 | * so... Later this can change. */ | |
14bbd6a5 | 346 | if (skb_unclone(skb, GFP_ATOMIC)) |
1da177e4 LT |
347 | goto out; |
348 | ||
349 | skb->ip_summed = CHECKSUM_NONE; | |
350 | ||
dff3bb06 SK |
351 | |
352 | if ((err = skb_cow_data(skb, 0, &trailer)) < 0) | |
353 | goto out; | |
354 | nfrags = err; | |
355 | ||
4b0ef1f2 DH |
356 | ah = (struct ip_auth_hdr *)skb->data; |
357 | iph = ip_hdr(skb); | |
358 | ihl = ip_hdrlen(skb); | |
359 | ||
d8b2a860 FD |
360 | if (x->props.flags & XFRM_STATE_ESN) { |
361 | sglists = 1; | |
362 | seqhi_len = sizeof(*seqhi); | |
363 | } | |
364 | ||
365 | work_iph = ah_alloc_tmp(ahash, nfrags + sglists, ihl + | |
366 | ahp->icv_trunc_len + seqhi_len); | |
94c10f0e DM |
367 | if (!work_iph) { |
368 | err = -ENOMEM; | |
dff3bb06 | 369 | goto out; |
94c10f0e | 370 | } |
dff3bb06 | 371 | |
d8b2a860 FD |
372 | seqhi = (__be32 *)((char *)work_iph + ihl); |
373 | auth_data = ah_tmp_auth(seqhi, seqhi_len); | |
e77f5dd7 | 374 | icv = ah_tmp_icv(auth_data, ahp->icv_trunc_len); |
dff3bb06 SK |
375 | req = ah_tmp_req(ahash, icv); |
376 | sg = ah_req_sg(ahash, req); | |
d8b2a860 | 377 | seqhisg = sg + nfrags; |
1da177e4 | 378 | |
dff3bb06 SK |
379 | memcpy(work_iph, iph, ihl); |
380 | memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len); | |
381 | memset(ah->auth_data, 0, ahp->icv_trunc_len); | |
1da177e4 LT |
382 | |
383 | iph->ttl = 0; | |
384 | iph->tos = 0; | |
385 | iph->frag_off = 0; | |
386 | iph->check = 0; | |
31a4ab93 | 387 | if (ihl > sizeof(*iph)) { |
d5a0a1e3 | 388 | __be32 dummy; |
dff3bb06 SK |
389 | err = ip_clear_mutable_options(iph, &dummy); |
390 | if (err) | |
391 | goto out_free; | |
1da177e4 | 392 | } |
0ebea8ef | 393 | |
dff3bb06 | 394 | skb_push(skb, ihl); |
e905a9ed | 395 | |
d8b2a860 | 396 | sg_init_table(sg, nfrags + sglists); |
3f297707 JD |
397 | err = skb_to_sgvec_nomark(skb, sg, 0, skb->len); |
398 | if (unlikely(err < 0)) | |
399 | goto out_free; | |
dff3bb06 | 400 | |
d8b2a860 FD |
401 | if (x->props.flags & XFRM_STATE_ESN) { |
402 | /* Attach seqhi sg right after packet payload */ | |
403 | *seqhi = XFRM_SKB_CB(skb)->seq.input.hi; | |
404 | sg_set_buf(seqhisg, seqhi, seqhi_len); | |
405 | } | |
406 | ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len); | |
dff3bb06 SK |
407 | ahash_request_set_callback(req, 0, ah_input_done, skb); |
408 | ||
409 | AH_SKB_CB(skb)->tmp = work_iph; | |
410 | ||
411 | err = crypto_ahash_digest(req); | |
412 | if (err) { | |
413 | if (err == -EINPROGRESS) | |
414 | goto out; | |
415 | ||
dff3bb06 | 416 | goto out_free; |
1da177e4 | 417 | } |
0ebea8ef | 418 | |
67df58a3 | 419 | err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0; |
0ebea8ef | 420 | if (err) |
dff3bb06 | 421 | goto out_free; |
0ebea8ef | 422 | |
b0e380b1 | 423 | skb->network_header += ah_hlen; |
dff3bb06 | 424 | memcpy(skb_network_header(skb), work_iph, ihl); |
31a4ab93 | 425 | __skb_pull(skb, ah_hlen + ihl); |
7143dfac LR |
426 | if (x->props.mode == XFRM_MODE_TUNNEL) |
427 | skb_reset_transport_header(skb); | |
428 | else | |
429 | skb_set_transport_header(skb, -ihl); | |
1da177e4 | 430 | |
dff3bb06 | 431 | err = nexthdr; |
1da177e4 | 432 | |
dff3bb06 SK |
433 | out_free: |
434 | kfree (work_iph); | |
1da177e4 | 435 | out: |
07d4ee58 | 436 | return err; |
1da177e4 LT |
437 | } |
438 | ||
e5b56454 | 439 | static int ah4_err(struct sk_buff *skb, u32 info) |
1da177e4 | 440 | { |
4fb236ba | 441 | struct net *net = dev_net(skb->dev); |
b71d1d42 | 442 | const struct iphdr *iph = (const struct iphdr *)skb->data; |
d9319100 | 443 | struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2)); |
1da177e4 LT |
444 | struct xfrm_state *x; |
445 | ||
55be7a9c DM |
446 | switch (icmp_hdr(skb)->type) { |
447 | case ICMP_DEST_UNREACH: | |
448 | if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) | |
e5b56454 | 449 | return 0; |
79121184 | 450 | break; |
55be7a9c DM |
451 | case ICMP_REDIRECT: |
452 | break; | |
453 | default: | |
e5b56454 | 454 | return 0; |
55be7a9c | 455 | } |
1da177e4 | 456 | |
b71d1d42 ED |
457 | x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr, |
458 | ah->spi, IPPROTO_AH, AF_INET); | |
1da177e4 | 459 | if (!x) |
e5b56454 | 460 | return 0; |
55be7a9c | 461 | |
387aa65a | 462 | if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH) |
d888f396 | 463 | ipv4_update_pmtu(skb, net, info, 0, IPPROTO_AH); |
387aa65a | 464 | else |
1042caa7 | 465 | ipv4_redirect(skb, net, 0, IPPROTO_AH); |
1da177e4 | 466 | xfrm_state_put(x); |
e5b56454 SK |
467 | |
468 | return 0; | |
1da177e4 LT |
469 | } |
470 | ||
e1e10b44 | 471 | static int ah_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack) |
1da177e4 LT |
472 | { |
473 | struct ah_data *ahp = NULL; | |
474 | struct xfrm_algo_desc *aalg_desc; | |
dff3bb06 | 475 | struct crypto_ahash *ahash; |
1da177e4 | 476 | |
ef87a4f8 SD |
477 | if (!x->aalg) { |
478 | NL_SET_ERR_MSG(extack, "AH requires a state with an AUTH algorithm"); | |
1da177e4 | 479 | goto error; |
ef87a4f8 | 480 | } |
1da177e4 | 481 | |
ef87a4f8 SD |
482 | if (x->encap) { |
483 | NL_SET_ERR_MSG(extack, "AH is not compatible with encapsulation"); | |
1da177e4 | 484 | goto error; |
ef87a4f8 | 485 | } |
1da177e4 | 486 | |
0da974f4 | 487 | ahp = kzalloc(sizeof(*ahp), GFP_KERNEL); |
dff3bb06 | 488 | if (!ahp) |
1da177e4 LT |
489 | return -ENOMEM; |
490 | ||
dff3bb06 | 491 | ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0); |
ef87a4f8 SD |
492 | if (IS_ERR(ahash)) { |
493 | NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations"); | |
07d4ee58 | 494 | goto error; |
ef87a4f8 | 495 | } |
07d4ee58 | 496 | |
dff3bb06 SK |
497 | ahp->ahash = ahash; |
498 | if (crypto_ahash_setkey(ahash, x->aalg->alg_key, | |
ef87a4f8 SD |
499 | (x->aalg->alg_key_len + 7) / 8)) { |
500 | NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations"); | |
1da177e4 | 501 | goto error; |
ef87a4f8 | 502 | } |
e905a9ed | 503 | |
1da177e4 LT |
504 | /* |
505 | * Lookup the algorithm description maintained by xfrm_algo, | |
506 | * verify crypto transform properties, and store information | |
507 | * we need for AH processing. This lookup cannot fail here | |
dff3bb06 | 508 | * after a successful crypto_alloc_ahash(). |
1da177e4 LT |
509 | */ |
510 | aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); | |
511 | BUG_ON(!aalg_desc); | |
512 | ||
513 | if (aalg_desc->uinfo.auth.icv_fullbits/8 != | |
dff3bb06 | 514 | crypto_ahash_digestsize(ahash)) { |
ef87a4f8 | 515 | NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations"); |
1da177e4 LT |
516 | goto error; |
517 | } | |
e905a9ed | 518 | |
1da177e4 | 519 | ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; |
8f8a088c | 520 | ahp->icv_trunc_len = x->aalg->alg_trunc_len/8; |
e905a9ed | 521 | |
fa9921e4 ND |
522 | if (x->props.flags & XFRM_STATE_ALIGN4) |
523 | x->props.header_len = XFRM_ALIGN4(sizeof(struct ip_auth_hdr) + | |
524 | ahp->icv_trunc_len); | |
525 | else | |
526 | x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + | |
527 | ahp->icv_trunc_len); | |
7e49e6de | 528 | if (x->props.mode == XFRM_MODE_TUNNEL) |
1da177e4 LT |
529 | x->props.header_len += sizeof(struct iphdr); |
530 | x->data = ahp; | |
531 | ||
532 | return 0; | |
533 | ||
534 | error: | |
535 | if (ahp) { | |
dff3bb06 | 536 | crypto_free_ahash(ahp->ahash); |
1da177e4 LT |
537 | kfree(ahp); |
538 | } | |
539 | return -EINVAL; | |
540 | } | |
541 | ||
542 | static void ah_destroy(struct xfrm_state *x) | |
543 | { | |
544 | struct ah_data *ahp = x->data; | |
545 | ||
546 | if (!ahp) | |
547 | return; | |
548 | ||
dff3bb06 | 549 | crypto_free_ahash(ahp->ahash); |
1da177e4 LT |
550 | kfree(ahp); |
551 | } | |
552 | ||
e5b56454 SK |
553 | static int ah4_rcv_cb(struct sk_buff *skb, int err) |
554 | { | |
555 | return 0; | |
556 | } | |
1da177e4 | 557 | |
533cb5b0 | 558 | static const struct xfrm_type ah_type = |
1da177e4 | 559 | { |
1da177e4 LT |
560 | .owner = THIS_MODULE, |
561 | .proto = IPPROTO_AH, | |
436a0a40 | 562 | .flags = XFRM_TYPE_REPLAY_PROT, |
1da177e4 LT |
563 | .init_state = ah_init_state, |
564 | .destructor = ah_destroy, | |
565 | .input = ah_input, | |
566 | .output = ah_output | |
567 | }; | |
568 | ||
e5b56454 | 569 | static struct xfrm4_protocol ah4_protocol = { |
1da177e4 | 570 | .handler = xfrm4_rcv, |
e5b56454 SK |
571 | .input_handler = xfrm_input, |
572 | .cb_handler = ah4_rcv_cb, | |
1da177e4 | 573 | .err_handler = ah4_err, |
e5b56454 | 574 | .priority = 0, |
1da177e4 LT |
575 | }; |
576 | ||
577 | static int __init ah4_init(void) | |
578 | { | |
579 | if (xfrm_register_type(&ah_type, AF_INET) < 0) { | |
058bd4d2 | 580 | pr_info("%s: can't add xfrm type\n", __func__); |
1da177e4 LT |
581 | return -EAGAIN; |
582 | } | |
e5b56454 | 583 | if (xfrm4_protocol_register(&ah4_protocol, IPPROTO_AH) < 0) { |
058bd4d2 | 584 | pr_info("%s: can't add protocol\n", __func__); |
1da177e4 LT |
585 | xfrm_unregister_type(&ah_type, AF_INET); |
586 | return -EAGAIN; | |
587 | } | |
588 | return 0; | |
589 | } | |
590 | ||
591 | static void __exit ah4_fini(void) | |
592 | { | |
e5b56454 | 593 | if (xfrm4_protocol_deregister(&ah4_protocol, IPPROTO_AH) < 0) |
058bd4d2 | 594 | pr_info("%s: can't remove protocol\n", __func__); |
4f518e80 | 595 | xfrm_unregister_type(&ah_type, AF_INET); |
1da177e4 LT |
596 | } |
597 | ||
598 | module_init(ah4_init); | |
599 | module_exit(ah4_fini); | |
b058a5d2 | 600 | MODULE_DESCRIPTION("IPv4 AH transformation library"); |
1da177e4 | 601 | MODULE_LICENSE("GPL"); |
d3d6dd3a | 602 | MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH); |