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