Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C)2002 USAGI/WIDE Project | |
1ab1457c | 3 | * |
1da177e4 LT |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
1ab1457c | 8 | * |
1da177e4 LT |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1ab1457c | 13 | * |
1da177e4 LT |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | * | |
18 | * Authors | |
19 | * | |
1ab1457c | 20 | * Mitsuru KANDA @USAGI : IPv6 Support |
1da177e4 LT |
21 | * Kazunori MIYAZAWA @USAGI : |
22 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> | |
1ab1457c | 23 | * |
1da177e4 LT |
24 | * This file is derived from net/ipv4/ah.c. |
25 | */ | |
26 | ||
8631e9bd | 27 | #include <crypto/hash.h> |
1da177e4 LT |
28 | #include <linux/module.h> |
29 | #include <net/ip.h> | |
30 | #include <net/ah.h> | |
31 | #include <linux/crypto.h> | |
32 | #include <linux/pfkeyv2.h> | |
33 | #include <linux/string.h> | |
8631e9bd | 34 | #include <linux/scatterlist.h> |
1da177e4 LT |
35 | #include <net/icmp.h> |
36 | #include <net/ipv6.h> | |
14c85021 | 37 | #include <net/protocol.h> |
1da177e4 | 38 | #include <net/xfrm.h> |
1da177e4 | 39 | |
8631e9bd SK |
40 | #define IPV6HDR_BASELEN 8 |
41 | ||
42 | struct tmp_ext { | |
43 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) | |
44 | struct in6_addr saddr; | |
45 | #endif | |
46 | struct in6_addr daddr; | |
47 | char hdrs[0]; | |
48 | }; | |
49 | ||
50 | struct ah_skb_cb { | |
51 | struct xfrm_skb_cb xfrm; | |
52 | void *tmp; | |
53 | }; | |
54 | ||
55 | #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0])) | |
56 | ||
57 | static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags, | |
58 | unsigned int size) | |
59 | { | |
60 | unsigned int len; | |
61 | ||
62 | len = size + crypto_ahash_digestsize(ahash) + | |
63 | (crypto_ahash_alignmask(ahash) & | |
64 | ~(crypto_tfm_ctx_alignment() - 1)); | |
65 | ||
66 | len = ALIGN(len, crypto_tfm_ctx_alignment()); | |
67 | ||
68 | len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash); | |
69 | len = ALIGN(len, __alignof__(struct scatterlist)); | |
70 | ||
71 | len += sizeof(struct scatterlist) * nfrags; | |
72 | ||
73 | return kmalloc(len, GFP_ATOMIC); | |
74 | } | |
75 | ||
76 | static inline struct tmp_ext *ah_tmp_ext(void *base) | |
77 | { | |
78 | return base + IPV6HDR_BASELEN; | |
79 | } | |
80 | ||
81 | static inline u8 *ah_tmp_auth(u8 *tmp, unsigned int offset) | |
82 | { | |
83 | return tmp + offset; | |
84 | } | |
85 | ||
86 | static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp, | |
87 | unsigned int offset) | |
88 | { | |
89 | return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1); | |
90 | } | |
91 | ||
92 | static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash, | |
93 | u8 *icv) | |
94 | { | |
95 | struct ahash_request *req; | |
96 | ||
97 | req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash), | |
98 | crypto_tfm_ctx_alignment()); | |
99 | ||
100 | ahash_request_set_tfm(req, ahash); | |
101 | ||
102 | return req; | |
103 | } | |
104 | ||
105 | static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash, | |
106 | struct ahash_request *req) | |
107 | { | |
108 | return (void *)ALIGN((unsigned long)(req + 1) + | |
109 | crypto_ahash_reqsize(ahash), | |
110 | __alignof__(struct scatterlist)); | |
111 | } | |
112 | ||
1da177e4 LT |
113 | static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr) |
114 | { | |
115 | u8 *opt = (u8 *)opthdr; | |
116 | int len = ipv6_optlen(opthdr); | |
117 | int off = 0; | |
118 | int optlen = 0; | |
119 | ||
120 | off += 2; | |
121 | len -= 2; | |
122 | ||
123 | while (len > 0) { | |
124 | ||
125 | switch (opt[off]) { | |
126 | ||
127 | case IPV6_TLV_PAD0: | |
128 | optlen = 1; | |
129 | break; | |
130 | default: | |
1ab1457c | 131 | if (len < 2) |
1da177e4 LT |
132 | goto bad; |
133 | optlen = opt[off+1]+2; | |
134 | if (len < optlen) | |
135 | goto bad; | |
136 | if (opt[off] & 0x20) | |
137 | memset(&opt[off+2], 0, opt[off+1]); | |
138 | break; | |
139 | } | |
140 | ||
141 | off += optlen; | |
142 | len -= optlen; | |
143 | } | |
144 | if (len == 0) | |
145 | return 1; | |
146 | ||
147 | bad: | |
148 | return 0; | |
149 | } | |
150 | ||
59fbb3a6 | 151 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
27637df9 MN |
152 | /** |
153 | * ipv6_rearrange_destopt - rearrange IPv6 destination options header | |
154 | * @iph: IPv6 header | |
155 | * @destopt: destionation options header | |
156 | */ | |
157 | static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) | |
158 | { | |
159 | u8 *opt = (u8 *)destopt; | |
160 | int len = ipv6_optlen(destopt); | |
161 | int off = 0; | |
162 | int optlen = 0; | |
163 | ||
164 | off += 2; | |
165 | len -= 2; | |
166 | ||
167 | while (len > 0) { | |
168 | ||
169 | switch (opt[off]) { | |
170 | ||
171 | case IPV6_TLV_PAD0: | |
172 | optlen = 1; | |
173 | break; | |
174 | default: | |
175 | if (len < 2) | |
176 | goto bad; | |
177 | optlen = opt[off+1]+2; | |
178 | if (len < optlen) | |
179 | goto bad; | |
180 | ||
181 | /* Rearrange the source address in @iph and the | |
182 | * addresses in home address option for final source. | |
183 | * See 11.3.2 of RFC 3775 for details. | |
184 | */ | |
185 | if (opt[off] == IPV6_TLV_HAO) { | |
186 | struct in6_addr final_addr; | |
187 | struct ipv6_destopt_hao *hao; | |
188 | ||
189 | hao = (struct ipv6_destopt_hao *)&opt[off]; | |
190 | if (hao->length != sizeof(hao->addr)) { | |
191 | if (net_ratelimit()) | |
192 | printk(KERN_WARNING "destopt hao: invalid header length: %u\n", hao->length); | |
193 | goto bad; | |
194 | } | |
195 | ipv6_addr_copy(&final_addr, &hao->addr); | |
196 | ipv6_addr_copy(&hao->addr, &iph->saddr); | |
197 | ipv6_addr_copy(&iph->saddr, &final_addr); | |
198 | } | |
199 | break; | |
200 | } | |
201 | ||
202 | off += optlen; | |
203 | len -= optlen; | |
204 | } | |
e731c248 | 205 | /* Note: ok if len == 0 */ |
27637df9 MN |
206 | bad: |
207 | return; | |
208 | } | |
136ebf08 MN |
209 | #else |
210 | static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {} | |
27637df9 MN |
211 | #endif |
212 | ||
1da177e4 LT |
213 | /** |
214 | * ipv6_rearrange_rthdr - rearrange IPv6 routing header | |
215 | * @iph: IPv6 header | |
216 | * @rthdr: routing header | |
217 | * | |
218 | * Rearrange the destination address in @iph and the addresses in @rthdr | |
219 | * so that they appear in the order they will at the final destination. | |
220 | * See Appendix A2 of RFC 2402 for details. | |
221 | */ | |
222 | static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr) | |
223 | { | |
224 | int segments, segments_left; | |
225 | struct in6_addr *addrs; | |
226 | struct in6_addr final_addr; | |
227 | ||
228 | segments_left = rthdr->segments_left; | |
229 | if (segments_left == 0) | |
230 | return; | |
1ab1457c | 231 | rthdr->segments_left = 0; |
1da177e4 LT |
232 | |
233 | /* The value of rthdr->hdrlen has been verified either by the system | |
234 | * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming | |
235 | * packets. So we can assume that it is even and that segments is | |
236 | * greater than or equal to segments_left. | |
237 | * | |
238 | * For the same reason we can assume that this option is of type 0. | |
239 | */ | |
240 | segments = rthdr->hdrlen >> 1; | |
241 | ||
242 | addrs = ((struct rt0_hdr *)rthdr)->addr; | |
243 | ipv6_addr_copy(&final_addr, addrs + segments - 1); | |
244 | ||
245 | addrs += segments - segments_left; | |
246 | memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs)); | |
247 | ||
248 | ipv6_addr_copy(addrs, &iph->daddr); | |
249 | ipv6_addr_copy(&iph->daddr, &final_addr); | |
250 | } | |
251 | ||
27637df9 | 252 | static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir) |
1da177e4 LT |
253 | { |
254 | union { | |
255 | struct ipv6hdr *iph; | |
256 | struct ipv6_opt_hdr *opth; | |
257 | struct ipv6_rt_hdr *rth; | |
258 | char *raw; | |
259 | } exthdr = { .iph = iph }; | |
260 | char *end = exthdr.raw + len; | |
261 | int nexthdr = iph->nexthdr; | |
262 | ||
263 | exthdr.iph++; | |
264 | ||
265 | while (exthdr.raw < end) { | |
266 | switch (nexthdr) { | |
27637df9 MN |
267 | case NEXTHDR_DEST: |
268 | if (dir == XFRM_POLICY_OUT) | |
269 | ipv6_rearrange_destopt(iph, exthdr.opth); | |
1da177e4 | 270 | case NEXTHDR_HOP: |
1da177e4 | 271 | if (!zero_out_mutable_opts(exthdr.opth)) { |
64ce2073 | 272 | LIMIT_NETDEBUG( |
1da177e4 LT |
273 | KERN_WARNING "overrun %sopts\n", |
274 | nexthdr == NEXTHDR_HOP ? | |
64ce2073 | 275 | "hop" : "dest"); |
1da177e4 LT |
276 | return -EINVAL; |
277 | } | |
278 | break; | |
279 | ||
280 | case NEXTHDR_ROUTING: | |
281 | ipv6_rearrange_rthdr(iph, exthdr.rth); | |
282 | break; | |
283 | ||
284 | default : | |
285 | return 0; | |
286 | } | |
287 | ||
288 | nexthdr = exthdr.opth->nexthdr; | |
289 | exthdr.raw += ipv6_optlen(exthdr.opth); | |
290 | } | |
291 | ||
292 | return 0; | |
293 | } | |
294 | ||
8631e9bd SK |
295 | static void ah6_output_done(struct crypto_async_request *base, int err) |
296 | { | |
297 | int extlen; | |
298 | u8 *iph_base; | |
299 | u8 *icv; | |
300 | struct sk_buff *skb = base->data; | |
301 | struct xfrm_state *x = skb_dst(skb)->xfrm; | |
302 | struct ah_data *ahp = x->data; | |
303 | struct ipv6hdr *top_iph = ipv6_hdr(skb); | |
304 | struct ip_auth_hdr *ah = ip_auth_hdr(skb); | |
305 | struct tmp_ext *iph_ext; | |
306 | ||
307 | extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr); | |
308 | if (extlen) | |
309 | extlen += sizeof(*iph_ext); | |
310 | ||
311 | iph_base = AH_SKB_CB(skb)->tmp; | |
312 | iph_ext = ah_tmp_ext(iph_base); | |
313 | icv = ah_tmp_icv(ahp->ahash, iph_ext, extlen); | |
314 | ||
315 | memcpy(ah->auth_data, icv, ahp->icv_trunc_len); | |
316 | memcpy(top_iph, iph_base, IPV6HDR_BASELEN); | |
317 | ||
318 | if (extlen) { | |
319 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) | |
320 | memcpy(&top_iph->saddr, iph_ext, extlen); | |
321 | #else | |
322 | memcpy(&top_iph->daddr, iph_ext, extlen); | |
323 | #endif | |
324 | } | |
325 | ||
326 | err = ah->nexthdr; | |
327 | ||
328 | kfree(AH_SKB_CB(skb)->tmp); | |
329 | xfrm_output_resume(skb, err); | |
330 | } | |
331 | ||
1da177e4 LT |
332 | static int ah6_output(struct xfrm_state *x, struct sk_buff *skb) |
333 | { | |
334 | int err; | |
8631e9bd | 335 | int nfrags; |
1da177e4 | 336 | int extlen; |
8631e9bd SK |
337 | u8 *iph_base; |
338 | u8 *icv; | |
339 | u8 nexthdr; | |
340 | struct sk_buff *trailer; | |
341 | struct crypto_ahash *ahash; | |
342 | struct ahash_request *req; | |
343 | struct scatterlist *sg; | |
1da177e4 LT |
344 | struct ipv6hdr *top_iph; |
345 | struct ip_auth_hdr *ah; | |
346 | struct ah_data *ahp; | |
8631e9bd SK |
347 | struct tmp_ext *iph_ext; |
348 | ||
349 | ahp = x->data; | |
350 | ahash = ahp->ahash; | |
351 | ||
352 | if ((err = skb_cow_data(skb, 0, &trailer)) < 0) | |
353 | goto out; | |
354 | nfrags = err; | |
1da177e4 | 355 | |
7b277b1a | 356 | skb_push(skb, -skb_network_offset(skb)); |
8631e9bd SK |
357 | extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr); |
358 | if (extlen) | |
359 | extlen += sizeof(*iph_ext); | |
360 | ||
361 | err = -ENOMEM; | |
362 | iph_base = ah_alloc_tmp(ahash, nfrags, IPV6HDR_BASELEN + extlen); | |
363 | if (!iph_base) | |
364 | goto out; | |
365 | ||
366 | iph_ext = ah_tmp_ext(iph_base); | |
367 | icv = ah_tmp_icv(ahash, iph_ext, extlen); | |
368 | req = ah_tmp_req(ahash, icv); | |
369 | sg = ah_req_sg(ahash, req); | |
370 | ||
371 | ah = ip_auth_hdr(skb); | |
372 | memset(ah->auth_data, 0, ahp->icv_trunc_len); | |
373 | ||
007f0211 | 374 | top_iph = ipv6_hdr(skb); |
1da177e4 LT |
375 | top_iph->payload_len = htons(skb->len - sizeof(*top_iph)); |
376 | ||
007f0211 HX |
377 | nexthdr = *skb_mac_header(skb); |
378 | *skb_mac_header(skb) = IPPROTO_AH; | |
1da177e4 LT |
379 | |
380 | /* When there are no extension headers, we only need to save the first | |
381 | * 8 bytes of the base IP header. | |
382 | */ | |
8631e9bd | 383 | memcpy(iph_base, top_iph, IPV6HDR_BASELEN); |
1da177e4 | 384 | |
1da177e4 | 385 | if (extlen) { |
59fbb3a6 | 386 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
8631e9bd | 387 | memcpy(iph_ext, &top_iph->saddr, extlen); |
27637df9 | 388 | #else |
8631e9bd | 389 | memcpy(iph_ext, &top_iph->daddr, extlen); |
e731c248 | 390 | #endif |
1da177e4 | 391 | err = ipv6_clear_mutable_options(top_iph, |
8631e9bd | 392 | extlen - sizeof(*iph_ext) + |
e731c248 YH |
393 | sizeof(*top_iph), |
394 | XFRM_POLICY_OUT); | |
1da177e4 | 395 | if (err) |
8631e9bd | 396 | goto out_free; |
1da177e4 LT |
397 | } |
398 | ||
1da177e4 LT |
399 | ah->nexthdr = nexthdr; |
400 | ||
401 | top_iph->priority = 0; | |
402 | top_iph->flow_lbl[0] = 0; | |
403 | top_iph->flow_lbl[1] = 0; | |
404 | top_iph->flow_lbl[2] = 0; | |
405 | top_iph->hop_limit = 0; | |
406 | ||
87bdc48d | 407 | ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2; |
1da177e4 LT |
408 | |
409 | ah->reserved = 0; | |
410 | ah->spi = x->id.spi; | |
b318e0e4 | 411 | ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output); |
b7c6538c | 412 | |
8631e9bd SK |
413 | sg_init_table(sg, nfrags); |
414 | skb_to_sgvec(skb, sg, 0, skb->len); | |
1da177e4 | 415 | |
8631e9bd SK |
416 | ahash_request_set_crypt(req, sg, icv, skb->len); |
417 | ahash_request_set_callback(req, 0, ah6_output_done, skb); | |
418 | ||
419 | AH_SKB_CB(skb)->tmp = iph_base; | |
1da177e4 | 420 | |
8631e9bd SK |
421 | err = crypto_ahash_digest(req); |
422 | if (err) { | |
423 | if (err == -EINPROGRESS) | |
424 | goto out; | |
425 | ||
426 | if (err == -EBUSY) | |
427 | err = NET_XMIT_DROP; | |
428 | goto out_free; | |
429 | } | |
430 | ||
431 | memcpy(ah->auth_data, icv, ahp->icv_trunc_len); | |
432 | memcpy(top_iph, iph_base, IPV6HDR_BASELEN); | |
433 | ||
434 | if (extlen) { | |
59fbb3a6 | 435 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
8631e9bd | 436 | memcpy(&top_iph->saddr, iph_ext, extlen); |
27637df9 | 437 | #else |
8631e9bd | 438 | memcpy(&top_iph->daddr, iph_ext, extlen); |
27637df9 | 439 | #endif |
1da177e4 LT |
440 | } |
441 | ||
8631e9bd SK |
442 | out_free: |
443 | kfree(iph_base); | |
444 | out: | |
1da177e4 LT |
445 | return err; |
446 | } | |
447 | ||
8631e9bd SK |
448 | static void ah6_input_done(struct crypto_async_request *base, int err) |
449 | { | |
450 | u8 *auth_data; | |
451 | u8 *icv; | |
452 | u8 *work_iph; | |
453 | struct sk_buff *skb = base->data; | |
454 | struct xfrm_state *x = xfrm_input_state(skb); | |
455 | struct ah_data *ahp = x->data; | |
456 | struct ip_auth_hdr *ah = ip_auth_hdr(skb); | |
457 | int hdr_len = skb_network_header_len(skb); | |
458 | int ah_hlen = (ah->hdrlen + 2) << 2; | |
459 | ||
460 | work_iph = AH_SKB_CB(skb)->tmp; | |
461 | auth_data = ah_tmp_auth(work_iph, hdr_len); | |
462 | icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len); | |
463 | ||
464 | err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0; | |
465 | if (err) | |
466 | goto out; | |
467 | ||
468 | skb->network_header += ah_hlen; | |
469 | memcpy(skb_network_header(skb), work_iph, hdr_len); | |
470 | __skb_pull(skb, ah_hlen + hdr_len); | |
471 | skb_set_transport_header(skb, -hdr_len); | |
472 | ||
473 | err = ah->nexthdr; | |
474 | out: | |
475 | kfree(AH_SKB_CB(skb)->tmp); | |
476 | xfrm_input_resume(skb, err); | |
477 | } | |
478 | ||
479 | ||
480 | ||
e695633e | 481 | static int ah6_input(struct xfrm_state *x, struct sk_buff *skb) |
1da177e4 LT |
482 | { |
483 | /* | |
484 | * Before process AH | |
485 | * [IPv6][Ext1][Ext2][AH][Dest][Payload] | |
486 | * |<-------------->| hdr_len | |
487 | * | |
488 | * To erase AH: | |
489 | * Keeping copy of cleared headers. After AH processing, | |
b0e380b1 ACM |
490 | * Moving the pointer of skb->network_header by using skb_pull as long |
491 | * as AH header length. Then copy back the copy as long as hdr_len | |
1da177e4 | 492 | * If destination header following AH exists, copy it into after [Ext2]. |
1ab1457c | 493 | * |
1da177e4 LT |
494 | * |<>|[IPv6][Ext1][Ext2][Dest][Payload] |
495 | * There is offset of AH before IPv6 header after the process. | |
496 | */ | |
497 | ||
8631e9bd SK |
498 | u8 *auth_data; |
499 | u8 *icv; | |
500 | u8 *work_iph; | |
501 | struct sk_buff *trailer; | |
502 | struct crypto_ahash *ahash; | |
503 | struct ahash_request *req; | |
504 | struct scatterlist *sg; | |
87bdc48d | 505 | struct ip_auth_hdr *ah; |
0660e03f | 506 | struct ipv6hdr *ip6h; |
1da177e4 | 507 | struct ah_data *ahp; |
1da177e4 LT |
508 | u16 hdr_len; |
509 | u16 ah_hlen; | |
510 | int nexthdr; | |
8631e9bd SK |
511 | int nfrags; |
512 | int err = -ENOMEM; | |
1da177e4 LT |
513 | |
514 | if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr))) | |
515 | goto out; | |
516 | ||
517 | /* We are going to _remove_ AH header to keep sockets happy, | |
518 | * so... Later this can change. */ | |
519 | if (skb_cloned(skb) && | |
520 | pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) | |
521 | goto out; | |
522 | ||
7aa68cb9 HX |
523 | skb->ip_summed = CHECKSUM_NONE; |
524 | ||
8631e9bd | 525 | hdr_len = skb_network_header_len(skb); |
87bdc48d | 526 | ah = (struct ip_auth_hdr *)skb->data; |
1da177e4 | 527 | ahp = x->data; |
8631e9bd SK |
528 | ahash = ahp->ahash; |
529 | ||
1da177e4 LT |
530 | nexthdr = ah->nexthdr; |
531 | ah_hlen = (ah->hdrlen + 2) << 2; | |
532 | ||
87bdc48d HX |
533 | if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) && |
534 | ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len)) | |
1ab1457c | 535 | goto out; |
1da177e4 LT |
536 | |
537 | if (!pskb_may_pull(skb, ah_hlen)) | |
538 | goto out; | |
539 | ||
0660e03f | 540 | ip6h = ipv6_hdr(skb); |
8631e9bd SK |
541 | |
542 | skb_push(skb, hdr_len); | |
543 | ||
544 | if ((err = skb_cow_data(skb, 0, &trailer)) < 0) | |
545 | goto out; | |
546 | nfrags = err; | |
547 | ||
548 | work_iph = ah_alloc_tmp(ahash, nfrags, hdr_len + ahp->icv_trunc_len); | |
549 | if (!work_iph) | |
550 | goto out; | |
551 | ||
552 | auth_data = ah_tmp_auth(work_iph, hdr_len); | |
553 | icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len); | |
554 | req = ah_tmp_req(ahash, icv); | |
555 | sg = ah_req_sg(ahash, req); | |
556 | ||
557 | memcpy(work_iph, ip6h, hdr_len); | |
558 | memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len); | |
559 | memset(ah->auth_data, 0, ahp->icv_trunc_len); | |
560 | ||
0660e03f | 561 | if (ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN)) |
8631e9bd SK |
562 | goto out_free; |
563 | ||
0660e03f ACM |
564 | ip6h->priority = 0; |
565 | ip6h->flow_lbl[0] = 0; | |
566 | ip6h->flow_lbl[1] = 0; | |
567 | ip6h->flow_lbl[2] = 0; | |
568 | ip6h->hop_limit = 0; | |
1da177e4 | 569 | |
8631e9bd SK |
570 | sg_init_table(sg, nfrags); |
571 | skb_to_sgvec(skb, sg, 0, skb->len); | |
1da177e4 | 572 | |
8631e9bd SK |
573 | ahash_request_set_crypt(req, sg, icv, skb->len); |
574 | ahash_request_set_callback(req, 0, ah6_input_done, skb); | |
575 | ||
576 | AH_SKB_CB(skb)->tmp = work_iph; | |
577 | ||
578 | err = crypto_ahash_digest(req); | |
579 | if (err) { | |
580 | if (err == -EINPROGRESS) | |
581 | goto out; | |
582 | ||
583 | if (err == -EBUSY) | |
584 | err = NET_XMIT_DROP; | |
585 | goto out_free; | |
1da177e4 | 586 | } |
0ebea8ef | 587 | |
8631e9bd | 588 | err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0; |
0ebea8ef | 589 | if (err) |
8631e9bd | 590 | goto out_free; |
1da177e4 | 591 | |
b0e380b1 | 592 | skb->network_header += ah_hlen; |
8631e9bd | 593 | memcpy(skb_network_header(skb), work_iph, hdr_len); |
b0e380b1 | 594 | skb->transport_header = skb->network_header; |
31a4ab93 | 595 | __skb_pull(skb, ah_hlen + hdr_len); |
1da177e4 | 596 | |
8631e9bd | 597 | err = nexthdr; |
1da177e4 | 598 | |
8631e9bd SK |
599 | out_free: |
600 | kfree(work_iph); | |
1da177e4 | 601 | out: |
07d4ee58 | 602 | return err; |
1da177e4 LT |
603 | } |
604 | ||
1ab1457c | 605 | static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
d5fdd6ba | 606 | u8 type, u8 code, int offset, __be32 info) |
1da177e4 | 607 | { |
4fb236ba | 608 | struct net *net = dev_net(skb->dev); |
1da177e4 LT |
609 | struct ipv6hdr *iph = (struct ipv6hdr*)skb->data; |
610 | struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset); | |
611 | struct xfrm_state *x; | |
612 | ||
613 | if (type != ICMPV6_DEST_UNREACH && | |
614 | type != ICMPV6_PKT_TOOBIG) | |
615 | return; | |
616 | ||
4fb236ba | 617 | x = xfrm_state_lookup(net, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6); |
1da177e4 LT |
618 | if (!x) |
619 | return; | |
620 | ||
5b095d98 | 621 | NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/%pI6\n", |
0c6ce78a | 622 | ntohl(ah->spi), &iph->daddr); |
1da177e4 LT |
623 | |
624 | xfrm_state_put(x); | |
625 | } | |
626 | ||
72cb6962 | 627 | static int ah6_init_state(struct xfrm_state *x) |
1da177e4 LT |
628 | { |
629 | struct ah_data *ahp = NULL; | |
630 | struct xfrm_algo_desc *aalg_desc; | |
8631e9bd | 631 | struct crypto_ahash *ahash; |
1da177e4 LT |
632 | |
633 | if (!x->aalg) | |
634 | goto error; | |
635 | ||
1da177e4 LT |
636 | if (x->encap) |
637 | goto error; | |
638 | ||
0c600eda | 639 | ahp = kzalloc(sizeof(*ahp), GFP_KERNEL); |
1da177e4 LT |
640 | if (ahp == NULL) |
641 | return -ENOMEM; | |
642 | ||
8631e9bd SK |
643 | ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0); |
644 | if (IS_ERR(ahash)) | |
07d4ee58 HX |
645 | goto error; |
646 | ||
8631e9bd SK |
647 | ahp->ahash = ahash; |
648 | if (crypto_ahash_setkey(ahash, x->aalg->alg_key, | |
bc31d3b2 | 649 | (x->aalg->alg_key_len + 7) / 8)) |
1da177e4 | 650 | goto error; |
1ab1457c | 651 | |
1da177e4 LT |
652 | /* |
653 | * Lookup the algorithm description maintained by xfrm_algo, | |
654 | * verify crypto transform properties, and store information | |
655 | * we need for AH processing. This lookup cannot fail here | |
07d4ee58 | 656 | * after a successful crypto_alloc_hash(). |
1da177e4 LT |
657 | */ |
658 | aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); | |
659 | BUG_ON(!aalg_desc); | |
660 | ||
661 | if (aalg_desc->uinfo.auth.icv_fullbits/8 != | |
8631e9bd | 662 | crypto_ahash_digestsize(ahash)) { |
1da177e4 | 663 | printk(KERN_INFO "AH: %s digestsize %u != %hu\n", |
8631e9bd | 664 | x->aalg->alg_name, crypto_ahash_digestsize(ahash), |
1da177e4 LT |
665 | aalg_desc->uinfo.auth.icv_fullbits/8); |
666 | goto error; | |
667 | } | |
1ab1457c | 668 | |
1da177e4 | 669 | ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; |
8f8a088c | 670 | ahp->icv_trunc_len = x->aalg->alg_trunc_len/8; |
1ab1457c | 671 | |
1da177e4 | 672 | BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN); |
1ab1457c | 673 | |
87bdc48d HX |
674 | x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + |
675 | ahp->icv_trunc_len); | |
ca68145f HX |
676 | switch (x->props.mode) { |
677 | case XFRM_MODE_BEET: | |
678 | case XFRM_MODE_TRANSPORT: | |
679 | break; | |
680 | case XFRM_MODE_TUNNEL: | |
1da177e4 | 681 | x->props.header_len += sizeof(struct ipv6hdr); |
ea2c47b4 | 682 | break; |
ca68145f HX |
683 | default: |
684 | goto error; | |
685 | } | |
1da177e4 LT |
686 | x->data = ahp; |
687 | ||
688 | return 0; | |
689 | ||
690 | error: | |
691 | if (ahp) { | |
8631e9bd | 692 | crypto_free_ahash(ahp->ahash); |
1da177e4 LT |
693 | kfree(ahp); |
694 | } | |
695 | return -EINVAL; | |
696 | } | |
697 | ||
698 | static void ah6_destroy(struct xfrm_state *x) | |
699 | { | |
700 | struct ah_data *ahp = x->data; | |
701 | ||
702 | if (!ahp) | |
703 | return; | |
704 | ||
8631e9bd | 705 | crypto_free_ahash(ahp->ahash); |
1da177e4 LT |
706 | kfree(ahp); |
707 | } | |
708 | ||
533cb5b0 | 709 | static const struct xfrm_type ah6_type = |
1da177e4 LT |
710 | { |
711 | .description = "AH6", | |
712 | .owner = THIS_MODULE, | |
713 | .proto = IPPROTO_AH, | |
436a0a40 | 714 | .flags = XFRM_TYPE_REPLAY_PROT, |
1da177e4 LT |
715 | .init_state = ah6_init_state, |
716 | .destructor = ah6_destroy, | |
717 | .input = ah6_input, | |
aee5adb4 MN |
718 | .output = ah6_output, |
719 | .hdr_offset = xfrm6_find_1stfragopt, | |
1da177e4 LT |
720 | }; |
721 | ||
41135cc8 | 722 | static const struct inet6_protocol ah6_protocol = { |
1da177e4 LT |
723 | .handler = xfrm6_rcv, |
724 | .err_handler = ah6_err, | |
725 | .flags = INET6_PROTO_NOPOLICY, | |
726 | }; | |
727 | ||
728 | static int __init ah6_init(void) | |
729 | { | |
730 | if (xfrm_register_type(&ah6_type, AF_INET6) < 0) { | |
731 | printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n"); | |
732 | return -EAGAIN; | |
733 | } | |
734 | ||
735 | if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) { | |
736 | printk(KERN_INFO "ipv6 ah init: can't add protocol\n"); | |
737 | xfrm_unregister_type(&ah6_type, AF_INET6); | |
738 | return -EAGAIN; | |
739 | } | |
740 | ||
741 | return 0; | |
742 | } | |
743 | ||
744 | static void __exit ah6_fini(void) | |
745 | { | |
746 | if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0) | |
747 | printk(KERN_INFO "ipv6 ah close: can't remove protocol\n"); | |
748 | ||
749 | if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0) | |
750 | printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n"); | |
751 | ||
752 | } | |
753 | ||
754 | module_init(ah6_init); | |
755 | module_exit(ah6_fini); | |
756 | ||
757 | MODULE_LICENSE("GPL"); | |
d3d6dd3a | 758 | MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH); |