xform_ah.c revision 1.1 1 /* $NetBSD: xform_ah.c,v 1.1 2003/08/13 20:06:51 jonathan Exp $ */
2 /* $FreeBSD: src/sys/netipsec/xform_ah.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $ */
3 /* $OpenBSD: ip_ah.c,v 1.63 2001/06/26 06:18:58 angelos Exp $ */
4 /*
5 * The authors of this code are John Ioannidis (ji (at) tla.org),
6 * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
7 * Niels Provos (provos (at) physnet.uni-hamburg.de).
8 *
9 * The original version of this code was written by John Ioannidis
10 * for BSD/OS in Athens, Greece, in November 1995.
11 *
12 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
13 * by Angelos D. Keromytis.
14 *
15 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
16 * and Niels Provos.
17 *
18 * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist.
19 *
20 * Copyright (c) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
21 * Angelos D. Keromytis and Niels Provos.
22 * Copyright (c) 1999 Niklas Hallqvist.
23 * Copyright (c) 2001 Angelos D. Keromytis.
24 *
25 * Permission to use, copy, and modify this software with or without fee
26 * is hereby granted, provided that this entire notice is included in
27 * all copies of any software which is or includes a copy or
28 * modification of this software.
29 * You may use this code under the GNU public license if you so wish. Please
30 * contribute changes back to the authors under this freer than GPL license
31 * so that we may further the use of strong encryption without limitations to
32 * all.
33 *
34 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
35 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
36 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
37 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
38 * PURPOSE.
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: xform_ah.c,v 1.1 2003/08/13 20:06:51 jonathan Exp $");
43
44 #include "opt_inet.h"
45 #include "opt_inet6.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/socket.h>
51 #include <sys/syslog.h>
52 #include <sys/kernel.h>
53 #include <sys/sysctl.h>
54
55 #include <net/if.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_ecn.h>
61 #include <netinet/ip6.h>
62
63 #include <net/route.h>
64 #include <netipsec/ipsec.h>
65 #include <netipsec/ah.h>
66 #include <netipsec/ah_var.h>
67 #include <netipsec/xform.h>
68
69 #ifdef INET6
70 #include <netinet6/ip6_var.h>
71 #include <netipsec/ipsec6.h>
72 #include <netinet6/ip6_ecn.h>
73 #endif
74
75 #include <netipsec/key.h>
76 #include <netipsec/key_debug.h>
77 #include <netipsec/ipsec_osdep.h>
78
79 #include <opencrypto/cryptodev.h>
80
81 /*
82 * Return header size in bytes. The old protocol did not support
83 * the replay counter; the new protocol always includes the counter.
84 */
85 #define HDRSIZE(sav) \
86 (((sav)->flags & SADB_X_EXT_OLD) ? \
87 sizeof (struct ah) : sizeof (struct ah) + sizeof (u_int32_t))
88 /*
89 * Return authenticator size in bytes. The old protocol is known
90 * to use a fixed 16-byte authenticator. The new algorithm gets
91 * this size from the xform but is (currently) always 12.
92 */
93 #define AUTHSIZE(sav) \
94 ((sav->flags & SADB_X_EXT_OLD) ? 16 : (sav)->tdb_authalgxform->authsize)
95
96 int ah_enable = 1; /* control flow of packets with AH */
97 int ah_cleartos = 1; /* clear ip_tos when doing AH calc */
98 struct ahstat ahstat;
99
100 #ifdef __FreeBSD__
101 SYSCTL_DECL(_net_inet_ah);
102 SYSCTL_INT(_net_inet_ah, OID_AUTO,
103 ah_enable, CTLFLAG_RW, &ah_enable, 0, "");
104 SYSCTL_INT(_net_inet_ah, OID_AUTO,
105 ah_cleartos, CTLFLAG_RW, &ah_cleartos, 0, "");
106 SYSCTL_STRUCT(_net_inet_ah, IPSECCTL_STATS,
107 stats, CTLFLAG_RD, &ahstat, ahstat, "");
108
109 #endif __FreeBSD__
110
111 static unsigned char ipseczeroes[256]; /* larger than an ip6 extension hdr */
112
113 static int ah_input_cb(struct cryptop*);
114 static int ah_output_cb(struct cryptop*);
115
116 /*
117 * NB: this is public for use by the PF_KEY support.
118 */
119 struct auth_hash *
120 ah_algorithm_lookup(int alg)
121 {
122 if (alg >= AH_ALG_MAX)
123 return NULL;
124 switch (alg) {
125 case SADB_X_AALG_NULL:
126 return &auth_hash_null;
127 case SADB_AALG_MD5HMAC:
128 return &auth_hash_hmac_md5_96;
129 case SADB_AALG_SHA1HMAC:
130 return &auth_hash_hmac_sha1_96;
131 case SADB_X_AALG_RIPEMD160HMAC:
132 return &auth_hash_hmac_ripemd_160_96;
133 case SADB_X_AALG_MD5:
134 return &auth_hash_key_md5;
135 case SADB_X_AALG_SHA:
136 return &auth_hash_key_sha1;
137 case SADB_X_AALG_SHA2_256:
138 return &auth_hash_hmac_sha2_256;
139 case SADB_X_AALG_SHA2_384:
140 return &auth_hash_hmac_sha2_384;
141 case SADB_X_AALG_SHA2_512:
142 return &auth_hash_hmac_sha2_512;
143 }
144 return NULL;
145 }
146
147 size_t
148 ah_hdrsiz(struct secasvar *sav)
149 {
150 size_t size;
151
152 if (sav != NULL) {
153 int authsize;
154 IPSEC_ASSERT(sav->tdb_authalgxform != NULL,
155 ("ah_hdrsiz: null xform"));
156 /*XXX not right for null algorithm--does it matter??*/
157 authsize = AUTHSIZE(sav);
158 size = roundup(authsize, sizeof (u_int32_t)) + HDRSIZE(sav);
159 } else {
160 /* default guess */
161 size = sizeof (struct ah) + sizeof (u_int32_t) + 16;
162 }
163 return size;
164 }
165
166 /*
167 * NB: public for use by esp_init.
168 */
169 int
170 ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria)
171 {
172 struct auth_hash *thash;
173 int keylen;
174
175 thash = ah_algorithm_lookup(sav->alg_auth);
176 if (thash == NULL) {
177 DPRINTF(("ah_init: unsupported authentication algorithm %u\n",
178 sav->alg_auth));
179 return EINVAL;
180 }
181 /*
182 * Verify the replay state block allocation is consistent with
183 * the protocol type. We check here so we can make assumptions
184 * later during protocol processing.
185 */
186 /* NB: replay state is setup elsewhere (sigh) */
187 if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) {
188 DPRINTF(("ah_init: replay state block inconsistency, "
189 "%s algorithm %s replay state\n",
190 (sav->flags & SADB_X_EXT_OLD) ? "old" : "new",
191 sav->replay == NULL ? "without" : "with"));
192 return EINVAL;
193 }
194 if (sav->key_auth == NULL) {
195 DPRINTF(("ah_init: no authentication key for %s "
196 "algorithm\n", thash->name));
197 return EINVAL;
198 }
199 keylen = _KEYLEN(sav->key_auth);
200 if (keylen != thash->keysize && thash->keysize != 0) {
201 DPRINTF(("ah_init: invalid keylength %d, algorithm "
202 "%s requires keysize %d\n",
203 keylen, thash->name, thash->keysize));
204 return EINVAL;
205 }
206
207 sav->tdb_xform = xsp;
208 sav->tdb_authalgxform = thash;
209
210 /* Initialize crypto session. */
211 bzero(cria, sizeof (*cria));
212 cria->cri_alg = sav->tdb_authalgxform->type;
213 cria->cri_klen = _KEYBITS(sav->key_auth);
214 cria->cri_key = _KEYBUF(sav->key_auth);
215
216 return 0;
217 }
218
219 /*
220 * ah_init() is called when an SPI is being set up.
221 */
222 static int
223 ah_init(struct secasvar *sav, struct xformsw *xsp)
224 {
225 struct cryptoini cria;
226 int error;
227
228 error = ah_init0(sav, xsp, &cria);
229 return error ? error :
230 crypto_newsession(&sav->tdb_cryptoid, &cria, crypto_support);
231 }
232
233 /*
234 * Paranoia.
235 *
236 * NB: public for use by esp_zeroize (XXX).
237 */
238 int
239 ah_zeroize(struct secasvar *sav)
240 {
241 int err;
242
243 if (sav->key_auth)
244 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
245
246 err = crypto_freesession(sav->tdb_cryptoid);
247 sav->tdb_cryptoid = 0;
248 sav->tdb_authalgxform = NULL;
249 sav->tdb_xform = NULL;
250 return err;
251 }
252
253 /*
254 * Massage IPv4/IPv6 headers for AH processing.
255 */
256 static int
257 ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out)
258 {
259 struct mbuf *m = *m0;
260 unsigned char *ptr;
261 int off, count;
262
263 #ifdef INET
264 struct ip *ip;
265 #endif /* INET */
266
267 #ifdef INET6
268 struct ip6_ext *ip6e;
269 struct ip6_hdr ip6;
270 int alloc, len, ad;
271 #endif /* INET6 */
272
273 switch (proto) {
274 #ifdef INET
275 case AF_INET:
276 /*
277 * This is the least painful way of dealing with IPv4 header
278 * and option processing -- just make sure they're in
279 * contiguous memory.
280 */
281 *m0 = m = m_pullup(m, skip);
282 if (m == NULL) {
283 DPRINTF(("ah_massage_headers: m_pullup failed\n"));
284 return ENOBUFS;
285 }
286
287 /* Fix the IP header */
288 ip = mtod(m, struct ip *);
289 if (ah_cleartos)
290 ip->ip_tos = 0;
291 ip->ip_ttl = 0;
292 ip->ip_sum = 0;
293
294 /*
295 * On input, fix ip_len which has been byte-swapped
296 * at ip_input().
297 */
298 /* On FreeBSD, ip_off and ip_len assumed in host endian. */
299 #ifdef __FreeBSD__
300 #define TOHOST(x) (x)
301 #else
302 #define TOHOST(x) (ntohs(x))
303 #endif
304 if (!out) {
305 u_int16_t inlen = ip->ip_len;
306
307 ip->ip_len = htons(TOHOST(ip->ip_len) + skip);
308 DPRINTF(("ip len: skip %d, "
309 "in %d host %d: new: raw %d host %d\n",
310 skip,
311 inlen, TOHOST(inlen),
312 ip->ip_len, ntohs(ip->ip_len)));
313
314
315 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
316 ip->ip_off = htons(TOHOST(ip->ip_off) & IP_DF);
317 else
318 ip->ip_off = 0;
319 } else {
320 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
321 ip->ip_off = htons(ntohs(ip->ip_off) & IP_DF);
322 else
323 ip->ip_off = 0;
324 }
325
326 ptr = mtod(m, unsigned char *) + sizeof(struct ip);
327
328 /* IPv4 option processing */
329 for (off = sizeof(struct ip); off < skip;) {
330 if (ptr[off] == IPOPT_EOL || ptr[off] == IPOPT_NOP ||
331 off + 1 < skip)
332 ;
333 else {
334 DPRINTF(("ah_massage_headers: illegal IPv4 "
335 "option length for option %d\n",
336 ptr[off]));
337
338 m_freem(m);
339 return EINVAL;
340 }
341
342 switch (ptr[off]) {
343 case IPOPT_EOL:
344 off = skip; /* End the loop. */
345 break;
346
347 case IPOPT_NOP:
348 off++;
349 break;
350
351 case IPOPT_SECURITY: /* 0x82 */
352 case 0x85: /* Extended security. */
353 case 0x86: /* Commercial security. */
354 case 0x94: /* Router alert */
355 case 0x95: /* RFC1770 */
356 /* Sanity check for option length. */
357 if (ptr[off + 1] < 2) {
358 DPRINTF(("ah_massage_headers: "
359 "illegal IPv4 option length for "
360 "option %d\n", ptr[off]));
361
362 m_freem(m);
363 return EINVAL;
364 }
365
366 off += ptr[off + 1];
367 break;
368
369 case IPOPT_LSRR:
370 case IPOPT_SSRR:
371 /* Sanity check for option length. */
372 if (ptr[off + 1] < 2) {
373 DPRINTF(("ah_massage_headers: "
374 "illegal IPv4 option length for "
375 "option %d\n", ptr[off]));
376
377 m_freem(m);
378 return EINVAL;
379 }
380
381 /*
382 * On output, if we have either of the
383 * source routing options, we should
384 * swap the destination address of the
385 * IP header with the last address
386 * specified in the option, as that is
387 * what the destination's IP header
388 * will look like.
389 */
390 if (out)
391 bcopy(ptr + off + ptr[off + 1] -
392 sizeof(struct in_addr),
393 &(ip->ip_dst), sizeof(struct in_addr));
394
395 /* Fall through */
396 default:
397 /* Sanity check for option length. */
398 if (ptr[off + 1] < 2) {
399 DPRINTF(("ah_massage_headers: "
400 "illegal IPv4 option length for "
401 "option %d\n", ptr[off]));
402 m_freem(m);
403 return EINVAL;
404 }
405
406 /* Zeroize all other options. */
407 count = ptr[off + 1];
408 bcopy(ipseczeroes, ptr, count);
409 off += count;
410 break;
411 }
412
413 /* Sanity check. */
414 if (off > skip) {
415 DPRINTF(("ah_massage_headers(): malformed "
416 "IPv4 options header\n"));
417
418 m_freem(m);
419 return EINVAL;
420 }
421 }
422
423 break;
424 #endif /* INET */
425
426 #ifdef INET6
427 case AF_INET6: /* Ugly... */
428 /* Copy and "cook" the IPv6 header. */
429 m_copydata(m, 0, sizeof(ip6), (caddr_t) &ip6);
430
431 /* We don't do IPv6 Jumbograms. */
432 if (ip6.ip6_plen == 0) {
433 DPRINTF(("ah_massage_headers: unsupported IPv6 jumbogram\n"));
434 m_freem(m);
435 return EMSGSIZE;
436 }
437
438 ip6.ip6_flow = 0;
439 ip6.ip6_hlim = 0;
440 ip6.ip6_vfc &= ~IPV6_VERSION_MASK;
441 ip6.ip6_vfc |= IPV6_VERSION;
442
443 /* Scoped address handling. */
444 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src))
445 ip6.ip6_src.s6_addr16[1] = 0;
446 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst))
447 ip6.ip6_dst.s6_addr16[1] = 0;
448
449 /* Done with IPv6 header. */
450 m_copyback(m, 0, sizeof(struct ip6_hdr), (caddr_t) &ip6);
451
452 /* Let's deal with the remaining headers (if any). */
453 if (skip - sizeof(struct ip6_hdr) > 0) {
454 if (m->m_len <= skip) {
455 ptr = (unsigned char *) malloc(
456 skip - sizeof(struct ip6_hdr),
457 M_XDATA, M_NOWAIT);
458 if (ptr == NULL) {
459 DPRINTF(("ah_massage_headers: failed "
460 "to allocate memory for IPv6 "
461 "headers\n"));
462 m_freem(m);
463 return ENOBUFS;
464 }
465
466 /*
467 * Copy all the protocol headers after
468 * the IPv6 header.
469 */
470 m_copydata(m, sizeof(struct ip6_hdr),
471 skip - sizeof(struct ip6_hdr), ptr);
472 alloc = 1;
473 } else {
474 /* No need to allocate memory. */
475 ptr = mtod(m, unsigned char *) +
476 sizeof(struct ip6_hdr);
477 alloc = 0;
478 }
479 } else
480 break;
481
482 off = ip6.ip6_nxt & 0xff; /* Next header type. */
483
484 for (len = 0; len < skip - sizeof(struct ip6_hdr);)
485 switch (off) {
486 case IPPROTO_HOPOPTS:
487 case IPPROTO_DSTOPTS:
488 ip6e = (struct ip6_ext *) (ptr + len);
489
490 /*
491 * Process the mutable/immutable
492 * options -- borrows heavily from the
493 * KAME code.
494 */
495 for (count = len + sizeof(struct ip6_ext);
496 count < len + ((ip6e->ip6e_len + 1) << 3);) {
497 if (ptr[count] == IP6OPT_PAD1) {
498 count++;
499 continue; /* Skip padding. */
500 }
501
502 /* Sanity check. */
503 if (count > len +
504 ((ip6e->ip6e_len + 1) << 3)) {
505 m_freem(m);
506
507 /* Free, if we allocated. */
508 if (alloc)
509 FREE(ptr, M_XDATA);
510 return EINVAL;
511 }
512
513 ad = ptr[count + 1];
514
515 /* If mutable option, zeroize. */
516 if (ptr[count] & IP6OPT_MUTABLE)
517 bcopy(ipseczeroes, ptr + count,
518 ptr[count + 1]);
519
520 count += ad;
521
522 /* Sanity check. */
523 if (count >
524 skip - sizeof(struct ip6_hdr)) {
525 m_freem(m);
526
527 /* Free, if we allocated. */
528 if (alloc)
529 FREE(ptr, M_XDATA);
530 return EINVAL;
531 }
532 }
533
534 /* Advance. */
535 len += ((ip6e->ip6e_len + 1) << 3);
536 off = ip6e->ip6e_nxt;
537 break;
538
539 case IPPROTO_ROUTING:
540 /*
541 * Always include routing headers in
542 * computation.
543 */
544 ip6e = (struct ip6_ext *) (ptr + len);
545 len += ((ip6e->ip6e_len + 1) << 3);
546 off = ip6e->ip6e_nxt;
547 break;
548
549 default:
550 DPRINTF(("ah_massage_headers: unexpected "
551 "IPv6 header type %d", off));
552 if (alloc)
553 FREE(ptr, M_XDATA);
554 m_freem(m);
555 return EINVAL;
556 }
557
558 /* Copyback and free, if we allocated. */
559 if (alloc) {
560 m_copyback(m, sizeof(struct ip6_hdr),
561 skip - sizeof(struct ip6_hdr), ptr);
562 free(ptr, M_XDATA);
563 }
564
565 break;
566 #endif /* INET6 */
567 }
568
569 return 0;
570 }
571
572 /*
573 * ah_input() gets called to verify that an input packet
574 * passes authentication.
575 */
576 static int
577 ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
578 {
579 struct auth_hash *ahx;
580 struct tdb_ident *tdbi;
581 struct tdb_crypto *tc;
582 struct m_tag *mtag;
583 struct newah *ah;
584 int hl, rplen, authsize;
585
586 struct cryptodesc *crda;
587 struct cryptop *crp;
588
589 IPSEC_SPLASSERT_SOFTNET("ah_input");
590
591 IPSEC_ASSERT(sav != NULL, ("ah_input: null SA"));
592 IPSEC_ASSERT(sav->key_auth != NULL,
593 ("ah_input: null authentication key"));
594 IPSEC_ASSERT(sav->tdb_authalgxform != NULL,
595 ("ah_input: null authentication xform"));
596
597 /* Figure out header size. */
598 rplen = HDRSIZE(sav);
599
600 /* XXX don't pullup, just copy header */
601 IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen);
602 if (ah == NULL) {
603 DPRINTF(("ah_input: cannot pullup header\n"));
604 ahstat.ahs_hdrops++; /*XXX*/
605 m_freem(m);
606 return ENOBUFS;
607 }
608
609 /* Check replay window, if applicable. */
610 if (sav->replay && !ipsec_chkreplay(ntohl(ah->ah_seq), sav)) {
611 ahstat.ahs_replay++;
612 DPRINTF(("ah_input: packet replay failure: %s\n",
613 ipsec_logsastr(sav)));
614 m_freem(m);
615 return ENOBUFS;
616 }
617
618 /* Verify AH header length. */
619 hl = ah->ah_len * sizeof (u_int32_t);
620 ahx = sav->tdb_authalgxform;
621 authsize = AUTHSIZE(sav);
622 if (hl != authsize + rplen - sizeof (struct ah)) {
623 DPRINTF(("ah_input: bad authenticator length %u (expecting %lu)"
624 " for packet in SA %s/%08lx\n",
625 hl, (u_long) (authsize + rplen - sizeof (struct ah)),
626 ipsec_address(&sav->sah->saidx.dst),
627 (u_long) ntohl(sav->spi)));
628 ahstat.ahs_badauthl++;
629 m_freem(m);
630 return EACCES;
631 }
632 ahstat.ahs_ibytes += m->m_pkthdr.len - skip - hl;
633 DPRINTF(("ah_input skip %d poff %d\n"
634 "len: hl %d authsize %d rpl %d expect %d\n",
635 skip, protoff,
636 hl, authsize, rplen,
637 authsize + rplen - sizeof(struct ah)));
638
639 /* Get crypto descriptors. */
640 crp = crypto_getreq(1);
641 if (crp == NULL) {
642 DPRINTF(("ah_input: failed to acquire crypto descriptor\n"));
643 ahstat.ahs_crypto++;
644 m_freem(m);
645 return ENOBUFS;
646 }
647
648 crda = crp->crp_desc;
649 IPSEC_ASSERT(crda != NULL, ("ah_input: null crypto descriptor"));
650
651 crda->crd_skip = 0;
652 crda->crd_len = m->m_pkthdr.len;
653 crda->crd_inject = skip + rplen;
654
655 /* Authentication operation. */
656 crda->crd_alg = ahx->type;
657 crda->crd_key = _KEYBUF(sav->key_auth);
658 crda->crd_klen = _KEYBITS(sav->key_auth);
659
660 /* Find out if we've already done crypto. */
661 for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL);
662 mtag != NULL;
663 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) {
664 tdbi = (struct tdb_ident *) (mtag + 1);
665 if (tdbi->proto == sav->sah->saidx.proto &&
666 tdbi->spi == sav->spi &&
667 !bcmp(&tdbi->dst, &sav->sah->saidx.dst,
668 sizeof (union sockaddr_union)))
669 break;
670 }
671
672 /* Allocate IPsec-specific opaque crypto info. */
673 if (mtag == NULL) {
674 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto) +
675 skip + rplen + authsize, M_XDATA, M_NOWAIT|M_ZERO);
676 } else {
677 /* Hash verification has already been done successfully. */
678 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto),
679 M_XDATA, M_NOWAIT|M_ZERO);
680 }
681 if (tc == NULL) {
682 DPRINTF(("ah_input: failed to allocate tdb_crypto\n"));
683 ahstat.ahs_crypto++;
684 crypto_freereq(crp);
685 m_freem(m);
686 return ENOBUFS;
687 }
688
689 /* Only save information if crypto processing is needed. */
690 if (mtag == NULL) {
691 int error;
692
693 /*
694 * Save the authenticator, the skipped portion of the packet,
695 * and the AH header.
696 */
697 m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(tc+1));
698
699 {
700 u_int8_t *pppp = ((caddr_t)(tc+1))+skip+rplen;
701 DPRINTF(("ah_input: zeroing %d bytes of authent " \
702 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
703 authsize,
704 pppp[0], pppp[1], pppp[2], pppp[3],
705 pppp[4], pppp[5], pppp[6], pppp[7],
706 pppp[8], pppp[9], pppp[10], pppp[11]));
707 }
708
709 /* Zeroize the authenticator on the packet. */
710 m_copyback(m, skip + rplen, authsize, ipseczeroes);
711
712 /* "Massage" the packet headers for crypto processing. */
713 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
714 skip, ahx->type, 0);
715 if (error != 0) {
716 /* NB: mbuf is free'd by ah_massage_headers */
717 ahstat.ahs_hdrops++;
718 free(tc, M_XDATA);
719 crypto_freereq(crp);
720 return error;
721 }
722 }
723
724 /* Crypto operation descriptor. */
725 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
726 crp->crp_flags = CRYPTO_F_IMBUF;
727 crp->crp_buf = (caddr_t) m;
728 crp->crp_callback = ah_input_cb;
729 crp->crp_sid = sav->tdb_cryptoid;
730 crp->crp_opaque = (caddr_t) tc;
731
732 /* These are passed as-is to the callback. */
733 tc->tc_spi = sav->spi;
734 tc->tc_dst = sav->sah->saidx.dst;
735 tc->tc_proto = sav->sah->saidx.proto;
736 tc->tc_nxt = ah->ah_nxt;
737 tc->tc_protoff = protoff;
738 tc->tc_skip = skip;
739 tc->tc_ptr = (caddr_t) mtag; /* Save the mtag we've identified. */
740
741 DPRINTF(("ah: hash over %d bytes, skip %d: "
742 "crda len %d skip %d inject %d\n",
743 crp->crp_ilen, tc->tc_skip,
744 crda->crd_len, crda->crd_skip, crda->crd_inject));
745
746 if (mtag == NULL)
747 return crypto_dispatch(crp);
748 else
749 return ah_input_cb(crp);
750 }
751
752 #ifdef INET6
753 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
754 if (saidx->dst.sa.sa_family == AF_INET6) { \
755 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
756 } else { \
757 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
758 } \
759 } while (0)
760 #else
761 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
762 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
763 #endif
764
765 /*
766 * AH input callback from the crypto driver.
767 */
768 static int
769 ah_input_cb(struct cryptop *crp)
770 {
771 int rplen, error, skip, protoff;
772 unsigned char calc[AH_ALEN_MAX];
773 struct mbuf *m;
774 struct cryptodesc *crd;
775 struct auth_hash *ahx;
776 struct tdb_crypto *tc;
777 struct m_tag *mtag;
778 struct secasvar *sav;
779 struct secasindex *saidx;
780 u_int8_t nxt;
781 caddr_t ptr;
782 int s, authsize;
783
784 crd = crp->crp_desc;
785
786 tc = (struct tdb_crypto *) crp->crp_opaque;
787 IPSEC_ASSERT(tc != NULL, ("ah_input_cb: null opaque crypto data area!"));
788 skip = tc->tc_skip;
789 nxt = tc->tc_nxt;
790 protoff = tc->tc_protoff;
791 mtag = (struct m_tag *) tc->tc_ptr;
792 m = (struct mbuf *) crp->crp_buf;
793
794 s = splsoftnet();
795
796 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
797 if (sav == NULL) {
798 ahstat.ahs_notdb++;
799 DPRINTF(("ah_input_cb: SA expired while in crypto\n"));
800 error = ENOBUFS; /*XXX*/
801 goto bad;
802 }
803
804 saidx = &sav->sah->saidx;
805 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
806 saidx->dst.sa.sa_family == AF_INET6,
807 ("ah_input_cb: unexpected protocol family %u",
808 saidx->dst.sa.sa_family));
809
810 ahx = (struct auth_hash *) sav->tdb_authalgxform;
811
812 /* Check for crypto errors. */
813 if (crp->crp_etype) {
814 if (sav->tdb_cryptoid != 0)
815 sav->tdb_cryptoid = crp->crp_sid;
816
817 if (crp->crp_etype == EAGAIN)
818 return crypto_dispatch(crp);
819
820 ahstat.ahs_noxform++;
821 DPRINTF(("ah_input_cb: crypto error %d\n", crp->crp_etype));
822 error = crp->crp_etype;
823 goto bad;
824 } else {
825 ahstat.ahs_hist[sav->alg_auth]++;
826 crypto_freereq(crp); /* No longer needed. */
827 crp = NULL;
828 }
829
830 /* Shouldn't happen... */
831 if (m == NULL) {
832 ahstat.ahs_crypto++;
833 DPRINTF(("ah_input_cb: bogus returned buffer from crypto\n"));
834 error = EINVAL;
835 goto bad;
836 }
837
838 /* Figure out header size. */
839 rplen = HDRSIZE(sav);
840 authsize = AUTHSIZE(sav);
841
842 if (ipsec_debug)
843 bzero(calc, sizeof(calc));
844
845 /* Copy authenticator off the packet. */
846 m_copydata(m, skip + rplen, authsize, calc);
847
848 /*
849 * If we have an mtag, we don't need to verify the authenticator --
850 * it has been verified by an IPsec-aware NIC.
851 */
852 if (mtag == NULL) {
853 ptr = (caddr_t) (tc + 1);
854
855 /* Verify authenticator. */
856 if (bcmp(ptr + skip + rplen, calc, authsize)) {
857 u_int8_t *pppp = ptr + skip+rplen;
858 DPRINTF(("ah_input: authentication hash mismatch " \
859 "over %d bytes " \
860 "for packet in SA %s/%08lx:\n" \
861 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, " \
862 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
863 authsize,
864 ipsec_address(&saidx->dst),
865 (u_long) ntohl(sav->spi),
866 calc[0], calc[1], calc[2], calc[3],
867 calc[4], calc[5], calc[6], calc[7],
868 calc[8], calc[9], calc[10], calc[11],
869 pppp[0], pppp[1], pppp[2], pppp[3],
870 pppp[4], pppp[5], pppp[6], pppp[7],
871 pppp[8], pppp[9], pppp[10], pppp[11]
872 ));
873 ahstat.ahs_badauth++;
874 error = EACCES;
875 goto bad;
876 }
877
878 /* Fix the Next Protocol field. */
879 ((u_int8_t *) ptr)[protoff] = nxt;
880
881 /* Copyback the saved (uncooked) network headers. */
882 m_copyback(m, 0, skip, ptr);
883 } else {
884 /* Fix the Next Protocol field. */
885 m_copyback(m, protoff, sizeof(u_int8_t), &nxt);
886 }
887
888 free(tc, M_XDATA), tc = NULL; /* No longer needed */
889
890 /*
891 * Header is now authenticated.
892 */
893 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM;
894
895 /*
896 * Update replay sequence number, if appropriate.
897 */
898 if (sav->replay) {
899 u_int32_t seq;
900
901 m_copydata(m, skip + offsetof(struct newah, ah_seq),
902 sizeof (seq), (caddr_t) &seq);
903 if (ipsec_updatereplay(ntohl(seq), sav)) {
904 ahstat.ahs_replay++;
905 error = ENOBUFS; /*XXX as above*/
906 goto bad;
907 }
908 }
909
910 /*
911 * Remove the AH header and authenticator from the mbuf.
912 */
913 error = m_striphdr(m, skip, rplen + authsize);
914 if (error) {
915 DPRINTF(("ah_input_cb: mangled mbuf chain for SA %s/%08lx\n",
916 ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
917
918 ahstat.ahs_hdrops++;
919 goto bad;
920 }
921
922 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag);
923
924 KEY_FREESAV(&sav);
925 splx(s);
926 return error;
927 bad:
928 if (sav)
929 KEY_FREESAV(&sav);
930 splx(s);
931 if (m != NULL)
932 m_freem(m);
933 if (tc != NULL)
934 free(tc, M_XDATA);
935 if (crp != NULL)
936 crypto_freereq(crp);
937 return error;
938 }
939
940 /*
941 * AH output routine, called by ipsec[46]_process_packet().
942 */
943 static int
944 ah_output(
945 struct mbuf *m,
946 struct ipsecrequest *isr,
947 struct mbuf **mp,
948 int skip,
949 int protoff)
950 {
951 struct secasvar *sav;
952 struct auth_hash *ahx;
953 struct cryptodesc *crda;
954 struct tdb_crypto *tc;
955 struct mbuf *mi;
956 struct cryptop *crp;
957 u_int16_t iplen;
958 int error, rplen, authsize, maxpacketsize, roff;
959 u_int8_t prot;
960 struct newah *ah;
961
962 IPSEC_SPLASSERT_SOFTNET("ah_output");
963
964 sav = isr->sav;
965 IPSEC_ASSERT(sav != NULL, ("ah_output: null SA"));
966 ahx = sav->tdb_authalgxform;
967 IPSEC_ASSERT(ahx != NULL, ("ah_output: null authentication xform"));
968
969 ahstat.ahs_output++;
970
971 /* Figure out header size. */
972 rplen = HDRSIZE(sav);
973
974 /* Check for maximum packet size violations. */
975 switch (sav->sah->saidx.dst.sa.sa_family) {
976 #ifdef INET
977 case AF_INET:
978 maxpacketsize = IP_MAXPACKET;
979 break;
980 #endif /* INET */
981 #ifdef INET6
982 case AF_INET6:
983 maxpacketsize = IPV6_MAXPACKET;
984 break;
985 #endif /* INET6 */
986 default:
987 DPRINTF(("ah_output: unknown/unsupported protocol "
988 "family %u, SA %s/%08lx\n",
989 sav->sah->saidx.dst.sa.sa_family,
990 ipsec_address(&sav->sah->saidx.dst),
991 (u_long) ntohl(sav->spi)));
992 ahstat.ahs_nopf++;
993 error = EPFNOSUPPORT;
994 goto bad;
995 }
996 authsize = AUTHSIZE(sav);
997 if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) {
998 DPRINTF(("ah_output: packet in SA %s/%08lx got too big "
999 "(len %u, max len %u)\n",
1000 ipsec_address(&sav->sah->saidx.dst),
1001 (u_long) ntohl(sav->spi),
1002 rplen + authsize + m->m_pkthdr.len, maxpacketsize));
1003 ahstat.ahs_toobig++;
1004 error = EMSGSIZE;
1005 goto bad;
1006 }
1007
1008 /* Update the counters. */
1009 ahstat.ahs_obytes += m->m_pkthdr.len - skip;
1010
1011 m = m_clone(m);
1012 if (m == NULL) {
1013 DPRINTF(("ah_output: cannot clone mbuf chain, SA %s/%08lx\n",
1014 ipsec_address(&sav->sah->saidx.dst),
1015 (u_long) ntohl(sav->spi)));
1016 ahstat.ahs_hdrops++;
1017 error = ENOBUFS;
1018 goto bad;
1019 }
1020
1021 /* Inject AH header. */
1022 mi = m_makespace(m, skip, rplen + authsize, &roff);
1023 if (mi == NULL) {
1024 DPRINTF(("ah_output: failed to inject %u byte AH header for SA "
1025 "%s/%08lx\n",
1026 rplen + authsize,
1027 ipsec_address(&sav->sah->saidx.dst),
1028 (u_long) ntohl(sav->spi)));
1029 ahstat.ahs_hdrops++; /*XXX differs from openbsd */
1030 error = ENOBUFS;
1031 goto bad;
1032 }
1033
1034 /*
1035 * The AH header is guaranteed by m_makespace() to be in
1036 * contiguous memory, at roff bytes offset into the returned mbuf.
1037 */
1038 ah = (struct newah *)(mtod(mi, caddr_t) + roff);
1039
1040 /* Initialize the AH header. */
1041 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt);
1042 ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(u_int32_t);
1043 ah->ah_reserve = 0;
1044 ah->ah_spi = sav->spi;
1045
1046 /* Zeroize authenticator. */
1047 m_copyback(m, skip + rplen, authsize, ipseczeroes);
1048
1049 /* Insert packet replay counter, as requested. */
1050 if (sav->replay) {
1051 if (sav->replay->count == ~0 &&
1052 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
1053 DPRINTF(("ah_output: replay counter wrapped for SA "
1054 "%s/%08lx\n",
1055 ipsec_address(&sav->sah->saidx.dst),
1056 (u_long) ntohl(sav->spi)));
1057 ahstat.ahs_wrap++;
1058 error = EINVAL;
1059 goto bad;
1060 }
1061 sav->replay->count++;
1062 ah->ah_seq = htonl(sav->replay->count);
1063 }
1064
1065 /* Get crypto descriptors. */
1066 crp = crypto_getreq(1);
1067 if (crp == NULL) {
1068 DPRINTF(("ah_output: failed to acquire crypto descriptors\n"));
1069 ahstat.ahs_crypto++;
1070 error = ENOBUFS;
1071 goto bad;
1072 }
1073
1074 crda = crp->crp_desc;
1075
1076 crda->crd_skip = 0;
1077 crda->crd_inject = skip + rplen;
1078 crda->crd_len = m->m_pkthdr.len;
1079
1080 /* Authentication operation. */
1081 crda->crd_alg = ahx->type;
1082 crda->crd_key = _KEYBUF(sav->key_auth);
1083 crda->crd_klen = _KEYBITS(sav->key_auth);
1084
1085 /* Allocate IPsec-specific opaque crypto info. */
1086 tc = (struct tdb_crypto *) malloc(
1087 sizeof(struct tdb_crypto) + skip, M_XDATA, M_NOWAIT|M_ZERO);
1088 if (tc == NULL) {
1089 crypto_freereq(crp);
1090 DPRINTF(("ah_output: failed to allocate tdb_crypto\n"));
1091 ahstat.ahs_crypto++;
1092 error = ENOBUFS;
1093 goto bad;
1094 }
1095
1096 /* Save the skipped portion of the packet. */
1097 m_copydata(m, 0, skip, (caddr_t) (tc + 1));
1098
1099 /*
1100 * Fix IP header length on the header used for
1101 * authentication. We don't need to fix the original
1102 * header length as it will be fixed by our caller.
1103 */
1104 switch (sav->sah->saidx.dst.sa.sa_family) {
1105 #ifdef INET
1106 case AF_INET:
1107 bcopy(((caddr_t)(tc + 1)) +
1108 offsetof(struct ip, ip_len),
1109 (caddr_t) &iplen, sizeof(u_int16_t));
1110 iplen = htons(ntohs(iplen) + rplen + authsize);
1111 m_copyback(m, offsetof(struct ip, ip_len),
1112 sizeof(u_int16_t), (caddr_t) &iplen);
1113 break;
1114 #endif /* INET */
1115
1116 #ifdef INET6
1117 case AF_INET6:
1118 bcopy(((caddr_t)(tc + 1)) +
1119 offsetof(struct ip6_hdr, ip6_plen),
1120 (caddr_t) &iplen, sizeof(u_int16_t));
1121 iplen = htons(ntohs(iplen) + rplen + authsize);
1122 m_copyback(m, offsetof(struct ip6_hdr, ip6_plen),
1123 sizeof(u_int16_t), (caddr_t) &iplen);
1124 break;
1125 #endif /* INET6 */
1126 }
1127
1128 /* Fix the Next Header field in saved header. */
1129 ((u_int8_t *) (tc + 1))[protoff] = IPPROTO_AH;
1130
1131 /* Update the Next Protocol field in the IP header. */
1132 prot = IPPROTO_AH;
1133 m_copyback(m, protoff, sizeof(u_int8_t), (caddr_t) &prot);
1134
1135 /* "Massage" the packet headers for crypto processing. */
1136 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
1137 skip, ahx->type, 1);
1138 if (error != 0) {
1139 m = NULL; /* mbuf was free'd by ah_massage_headers. */
1140 free(tc, M_XDATA);
1141 crypto_freereq(crp);
1142 goto bad;
1143 }
1144
1145 /* Crypto operation descriptor. */
1146 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
1147 crp->crp_flags = CRYPTO_F_IMBUF;
1148 crp->crp_buf = (caddr_t) m;
1149 crp->crp_callback = ah_output_cb;
1150 crp->crp_sid = sav->tdb_cryptoid;
1151 crp->crp_opaque = (caddr_t) tc;
1152
1153 /* These are passed as-is to the callback. */
1154 tc->tc_isr = isr;
1155 tc->tc_spi = sav->spi;
1156 tc->tc_dst = sav->sah->saidx.dst;
1157 tc->tc_proto = sav->sah->saidx.proto;
1158 tc->tc_skip = skip;
1159 tc->tc_protoff = protoff;
1160
1161 return crypto_dispatch(crp);
1162 bad:
1163 if (m)
1164 m_freem(m);
1165 return (error);
1166 }
1167
1168 /*
1169 * AH output callback from the crypto driver.
1170 */
1171 static int
1172 ah_output_cb(struct cryptop *crp)
1173 {
1174 int skip, protoff, error;
1175 struct tdb_crypto *tc;
1176 struct ipsecrequest *isr;
1177 struct secasvar *sav;
1178 struct mbuf *m;
1179 caddr_t ptr;
1180 int s, err;
1181
1182 tc = (struct tdb_crypto *) crp->crp_opaque;
1183 IPSEC_ASSERT(tc != NULL, ("ah_output_cb: null opaque data area!"));
1184 skip = tc->tc_skip;
1185 protoff = tc->tc_protoff;
1186 ptr = (caddr_t) (tc + 1);
1187 m = (struct mbuf *) crp->crp_buf;
1188
1189 s = splsoftnet();
1190
1191 isr = tc->tc_isr;
1192 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
1193 if (sav == NULL) {
1194 ahstat.ahs_notdb++;
1195 DPRINTF(("ah_output_cb: SA expired while in crypto\n"));
1196 error = ENOBUFS; /*XXX*/
1197 goto bad;
1198 }
1199 IPSEC_ASSERT(isr->sav == sav, ("ah_output_cb: SA changed\n"));
1200
1201 /* Check for crypto errors. */
1202 if (crp->crp_etype) {
1203 if (sav->tdb_cryptoid != 0)
1204 sav->tdb_cryptoid = crp->crp_sid;
1205
1206 if (crp->crp_etype == EAGAIN) {
1207 KEY_FREESAV(&sav);
1208 splx(s);
1209 return crypto_dispatch(crp);
1210 }
1211
1212 ahstat.ahs_noxform++;
1213 DPRINTF(("ah_output_cb: crypto error %d\n", crp->crp_etype));
1214 error = crp->crp_etype;
1215 goto bad;
1216 }
1217
1218 /* Shouldn't happen... */
1219 if (m == NULL) {
1220 ahstat.ahs_crypto++;
1221 DPRINTF(("ah_output_cb: bogus returned buffer from crypto\n"));
1222 error = EINVAL;
1223 goto bad;
1224 }
1225 ahstat.ahs_hist[sav->alg_auth]++;
1226
1227 /*
1228 * Copy original headers (with the new protocol number) back
1229 * in place.
1230 */
1231 m_copyback(m, 0, skip, ptr);
1232
1233 /* No longer needed. */
1234 free(tc, M_XDATA);
1235 crypto_freereq(crp);
1236
1237 /* NB: m is reclaimed by ipsec_process_done. */
1238 err = ipsec_process_done(m, isr);
1239 KEY_FREESAV(&sav);
1240 splx(s);
1241 return err;
1242 bad:
1243 if (sav)
1244 KEY_FREESAV(&sav);
1245 splx(s);
1246 if (m)
1247 m_freem(m);
1248 free(tc, M_XDATA);
1249 crypto_freereq(crp);
1250 return error;
1251 }
1252
1253 static struct xformsw ah_xformsw = {
1254 XF_AH, XFT_AUTH, "IPsec AH",
1255 ah_init, ah_zeroize, ah_input, ah_output,
1256 };
1257
1258 INITFN void
1259 ah_attach(void)
1260 {
1261 xform_register(&ah_xformsw);
1262 }
1263
1264 #ifdef __FreeBSD__
1265 SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ah_attach, NULL);
1266 #endif
1267