xform_ah.c revision 1.65 1 /* $NetBSD: xform_ah.c,v 1.65 2017/07/19 10:26:09 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.65 2017/07/19 10:26:09 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;
622 struct newah *ah;
623 int hl, rplen, authsize, error;
624
625 struct cryptodesc *crda;
626 struct cryptop *crp;
627
628 IPSEC_SPLASSERT_SOFTNET(__func__);
629
630 KASSERT(sav != NULL);
631 KASSERT(sav->key_auth != NULL);
632 KASSERT(sav->tdb_authalgxform != NULL);
633
634 /* Figure out header size. */
635 rplen = HDRSIZE(sav);
636
637 /* XXX don't pullup, just copy header */
638 IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen);
639 if (ah == NULL) {
640 DPRINTF(("%s: cannot pullup header\n", __func__));
641 AH_STATINC(AH_STAT_HDROPS); /*XXX*/
642 m_freem(m);
643 return ENOBUFS;
644 }
645
646 /* Check replay window, if applicable. */
647 if (sav->replay && !ipsec_chkreplay(ntohl(ah->ah_seq), sav)) {
648 char buf[IPSEC_LOGSASTRLEN];
649 AH_STATINC(AH_STAT_REPLAY);
650 DPRINTF(("%s: packet replay failure: %s\n", __func__,
651 ipsec_logsastr(sav, buf, sizeof(buf))));
652 m_freem(m);
653 return ENOBUFS;
654 }
655
656 /* Verify AH header length. */
657 hl = ah->ah_len * sizeof(uint32_t);
658 ahx = sav->tdb_authalgxform;
659 authsize = AUTHSIZE(sav);
660 if (hl != authsize + rplen - sizeof(struct ah)) {
661 char buf[IPSEC_ADDRSTRLEN];
662 DPRINTF(("%s: bad authenticator length %u (expecting %lu)"
663 " for packet in SA %s/%08lx\n", __func__,
664 hl, (u_long) (authsize + rplen - sizeof(struct ah)),
665 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
666 (u_long) ntohl(sav->spi)));
667 AH_STATINC(AH_STAT_BADAUTHL);
668 m_freem(m);
669 return EACCES;
670 }
671 AH_STATADD(AH_STAT_IBYTES, m->m_pkthdr.len - skip - hl);
672
673 /* Get crypto descriptors. */
674 crp = crypto_getreq(1);
675 if (crp == NULL) {
676 DPRINTF(("%s: failed to acquire crypto descriptor\n", __func__));
677 AH_STATINC(AH_STAT_CRYPTO);
678 m_freem(m);
679 return ENOBUFS;
680 }
681
682 crda = crp->crp_desc;
683 KASSERT(crda != NULL);
684
685 crda->crd_skip = 0;
686 crda->crd_len = m->m_pkthdr.len;
687 crda->crd_inject = skip + rplen;
688
689 /* Authentication operation. */
690 crda->crd_alg = ahx->type;
691 crda->crd_key = _KEYBUF(sav->key_auth);
692 crda->crd_klen = _KEYBITS(sav->key_auth);
693
694 /* Allocate IPsec-specific opaque crypto info. */
695 size_t size = sizeof(*tc);
696 size_t extra = skip + rplen + authsize;
697 size += extra;
698
699 tc = malloc(size, M_XDATA, M_NOWAIT|M_ZERO);
700 if (tc == NULL) {
701 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
702 AH_STATINC(AH_STAT_CRYPTO);
703 crypto_freereq(crp);
704 m_freem(m);
705 return ENOBUFS;
706 }
707
708 error = m_makewritable(&m, 0, extra, M_NOWAIT);
709 if (error) {
710 m_freem(m);
711 DPRINTF(("%s: failed to copyback_cow\n", __func__));
712 AH_STATINC(AH_STAT_HDROPS);
713 free(tc, M_XDATA);
714 crypto_freereq(crp);
715 return error;
716 }
717
718 /*
719 * Save the authenticator, the skipped portion of the packet,
720 * and the AH header.
721 */
722 m_copydata(m, 0, extra, (tc + 1));
723 /* Zeroize the authenticator on the packet. */
724 m_copyback(m, skip + rplen, authsize, ipseczeroes);
725
726 /* "Massage" the packet headers for crypto processing. */
727 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
728 skip, ahx->type, 0);
729 if (error != 0) {
730 /* NB: mbuf is free'd by ah_massage_headers */
731 AH_STATINC(AH_STAT_HDROPS);
732 free(tc, M_XDATA);
733 crypto_freereq(crp);
734 return error;
735 }
736
737 /* Crypto operation descriptor. */
738 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
739 crp->crp_flags = CRYPTO_F_IMBUF;
740 crp->crp_buf = m;
741 crp->crp_callback = ah_input_cb;
742 crp->crp_sid = sav->tdb_cryptoid;
743 crp->crp_opaque = tc;
744
745 /* These are passed as-is to the callback. */
746 tc->tc_spi = sav->spi;
747 tc->tc_dst = sav->sah->saidx.dst;
748 tc->tc_proto = sav->sah->saidx.proto;
749 tc->tc_nxt = ah->ah_nxt;
750 tc->tc_protoff = protoff;
751 tc->tc_skip = skip;
752 tc->tc_sav = sav;
753 KEY_SA_REF(sav);
754
755 DPRINTF(("%s: hash over %d bytes, skip %d: "
756 "crda len %d skip %d inject %d\n", __func__,
757 crp->crp_ilen, tc->tc_skip,
758 crda->crd_len, crda->crd_skip, crda->crd_inject));
759
760 return crypto_dispatch(crp);
761 }
762
763 #ifdef INET6
764 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do { \
765 if (saidx->dst.sa.sa_family == AF_INET6) { \
766 error = ipsec6_common_input_cb(m, sav, skip, protoff); \
767 } else { \
768 error = ipsec4_common_input_cb(m, sav, skip, protoff); \
769 } \
770 } while (0)
771 #else
772 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) \
773 (error = ipsec4_common_input_cb(m, sav, skip, protoff))
774 #endif
775
776 /*
777 * AH input callback from the crypto driver.
778 */
779 static int
780 ah_input_cb(struct cryptop *crp)
781 {
782 char buf[IPSEC_ADDRSTRLEN];
783 int rplen, error, skip, protoff;
784 unsigned char calc[AH_ALEN_MAX];
785 struct mbuf *m;
786 struct tdb_crypto *tc;
787 struct secasvar *sav;
788 struct secasindex *saidx;
789 uint8_t nxt;
790 char *ptr;
791 int s, authsize;
792 uint16_t dport;
793 uint16_t sport;
794
795 KASSERT(crp->crp_opaque != NULL);
796 tc = crp->crp_opaque;
797 skip = tc->tc_skip;
798 nxt = tc->tc_nxt;
799 protoff = tc->tc_protoff;
800 m = crp->crp_buf;
801
802
803 /* find the source port for NAT-T */
804 nat_t_ports_get(m, &dport, &sport);
805
806 s = splsoftnet();
807 mutex_enter(softnet_lock);
808
809 sav = tc->tc_sav;
810 if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
811 KEY_FREESAV(&sav);
812 sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi,
813 sport, dport);
814 if (sav == NULL) {
815 AH_STATINC(AH_STAT_NOTDB);
816 DPRINTF(("%s: SA expired while in crypto\n", __func__));
817 error = ENOBUFS; /*XXX*/
818 goto bad;
819 }
820 }
821
822 saidx = &sav->sah->saidx;
823 KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
824 saidx->dst.sa.sa_family == AF_INET6,
825 "unexpected protocol family %u", saidx->dst.sa.sa_family);
826
827 /* Check for crypto errors. */
828 if (crp->crp_etype) {
829 if (sav->tdb_cryptoid != 0)
830 sav->tdb_cryptoid = crp->crp_sid;
831
832 if (crp->crp_etype == EAGAIN) {
833 mutex_exit(softnet_lock);
834 splx(s);
835 return crypto_dispatch(crp);
836 }
837
838 AH_STATINC(AH_STAT_NOXFORM);
839 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
840 error = crp->crp_etype;
841 goto bad;
842 } else {
843 AH_STATINC(AH_STAT_HIST + ah_stats[sav->alg_auth]);
844 crypto_freereq(crp); /* No longer needed. */
845 crp = NULL;
846 }
847
848 /* Figure out header size. */
849 rplen = HDRSIZE(sav);
850 authsize = AUTHSIZE(sav);
851
852 if (ipsec_debug)
853 memset(calc, 0, sizeof(calc));
854
855 /* Copy authenticator off the packet. */
856 m_copydata(m, skip + rplen, authsize, calc);
857
858 ptr = (char *)(tc + 1);
859 const uint8_t *pppp = ptr + skip + rplen;
860
861 /* Verify authenticator. */
862 if (!consttime_memequal(pppp, calc, authsize)) {
863 DPRINTF(("%s: authentication hash mismatch " \
864 "over %d bytes " \
865 "for packet in SA %s/%08lx:\n" \
866 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, " \
867 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
868 __func__, authsize,
869 ipsec_address(&saidx->dst, buf, sizeof(buf)),
870 (u_long) ntohl(sav->spi),
871 calc[0], calc[1], calc[2], calc[3],
872 calc[4], calc[5], calc[6], calc[7],
873 calc[8], calc[9], calc[10], calc[11],
874 pppp[0], pppp[1], pppp[2], pppp[3],
875 pppp[4], pppp[5], pppp[6], pppp[7],
876 pppp[8], pppp[9], pppp[10], pppp[11]
877 ));
878 AH_STATINC(AH_STAT_BADAUTH);
879 error = EACCES;
880 goto bad;
881 }
882
883 /* Fix the Next Protocol field. */
884 ptr[protoff] = nxt;
885
886 /* Copyback the saved (uncooked) network headers. */
887 m_copyback(m, 0, skip, ptr);
888
889 free(tc, M_XDATA), tc = NULL; /* No longer needed */
890
891 /*
892 * Header is now authenticated.
893 */
894 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM;
895
896 /*
897 * Update replay sequence number, if appropriate.
898 */
899 if (sav->replay) {
900 uint32_t seq;
901
902 m_copydata(m, skip + offsetof(struct newah, ah_seq),
903 sizeof(seq), &seq);
904 if (ipsec_updatereplay(ntohl(seq), sav)) {
905 AH_STATINC(AH_STAT_REPLAY);
906 error = ENOBUFS; /*XXX as above*/
907 goto bad;
908 }
909 }
910
911 /*
912 * Remove the AH header and authenticator from the mbuf.
913 */
914 error = m_striphdr(m, skip, rplen + authsize);
915 if (error) {
916 DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__,
917 ipsec_address(&saidx->dst, buf, sizeof(buf)),
918 (u_long) ntohl(sav->spi)));
919
920 AH_STATINC(AH_STAT_HDROPS);
921 goto bad;
922 }
923
924 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
925
926 KEY_FREESAV(&sav);
927 mutex_exit(softnet_lock);
928 splx(s);
929 return error;
930 bad:
931 if (sav)
932 KEY_FREESAV(&sav);
933 mutex_exit(softnet_lock);
934 splx(s);
935 if (m != NULL)
936 m_freem(m);
937 if (tc != NULL)
938 free(tc, M_XDATA);
939 if (crp != NULL)
940 crypto_freereq(crp);
941 return error;
942 }
943
944 /*
945 * AH output routine, called by ipsec[46]_process_packet().
946 */
947 static int
948 ah_output(
949 struct mbuf *m,
950 struct ipsecrequest *isr,
951 struct secasvar *sav,
952 struct mbuf **mp,
953 int skip,
954 int protoff
955 )
956 {
957 char buf[IPSEC_ADDRSTRLEN];
958 const struct auth_hash *ahx;
959 struct cryptodesc *crda;
960 struct tdb_crypto *tc;
961 struct mbuf *mi;
962 struct cryptop *crp;
963 uint16_t iplen;
964 int error, rplen, authsize, maxpacketsize, roff;
965 uint8_t prot;
966 struct newah *ah;
967
968 IPSEC_SPLASSERT_SOFTNET(__func__);
969
970 KASSERT(sav != NULL);
971 KASSERT(sav->tdb_authalgxform != NULL);
972 ahx = sav->tdb_authalgxform;
973
974 AH_STATINC(AH_STAT_OUTPUT);
975
976 /* Figure out header size. */
977 rplen = HDRSIZE(sav);
978
979 size_t ipoffs;
980 /* Check for maximum packet size violations. */
981 switch (sav->sah->saidx.dst.sa.sa_family) {
982 #ifdef INET
983 case AF_INET:
984 maxpacketsize = IP_MAXPACKET;
985 ipoffs = offsetof(struct ip, ip_len);
986 break;
987 #endif /* INET */
988 #ifdef INET6
989 case AF_INET6:
990 maxpacketsize = IPV6_MAXPACKET;
991 ipoffs = offsetof(struct ip6_hdr, ip6_plen);
992 break;
993 #endif /* INET6 */
994 default:
995 DPRINTF(("%s: unknown/unsupported protocol "
996 "family %u, SA %s/%08lx\n", __func__,
997 sav->sah->saidx.dst.sa.sa_family,
998 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
999 (u_long) ntohl(sav->spi)));
1000 AH_STATINC(AH_STAT_NOPF);
1001 error = EPFNOSUPPORT;
1002 goto bad;
1003 }
1004 authsize = AUTHSIZE(sav);
1005 if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) {
1006 DPRINTF(("%s: packet in SA %s/%08lx got too big "
1007 "(len %u, max len %u)\n", __func__,
1008 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
1009 (u_long) ntohl(sav->spi),
1010 rplen + authsize + m->m_pkthdr.len, maxpacketsize));
1011 AH_STATINC(AH_STAT_TOOBIG);
1012 error = EMSGSIZE;
1013 goto bad;
1014 }
1015
1016 /* Update the counters. */
1017 AH_STATADD(AH_STAT_OBYTES, m->m_pkthdr.len - skip);
1018
1019 m = m_clone(m);
1020 if (m == NULL) {
1021 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
1022 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
1023 (u_long) ntohl(sav->spi)));
1024 AH_STATINC(AH_STAT_HDROPS);
1025 error = ENOBUFS;
1026 goto bad;
1027 }
1028
1029 /* Inject AH header. */
1030 mi = m_makespace(m, skip, rplen + authsize, &roff);
1031 if (mi == NULL) {
1032 DPRINTF(("%s: failed to inject %u byte AH header for SA "
1033 "%s/%08lx\n", __func__,
1034 rplen + authsize,
1035 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
1036 (u_long) ntohl(sav->spi)));
1037 AH_STATINC(AH_STAT_HDROPS); /*XXX differs from openbsd */
1038 error = ENOBUFS;
1039 goto bad;
1040 }
1041
1042 /*
1043 * The AH header is guaranteed by m_makespace() to be in
1044 * contiguous memory, at roff bytes offset into the returned mbuf.
1045 */
1046 ah = (struct newah *)(mtod(mi, char *) + roff);
1047
1048 /* Initialize the AH header. */
1049 m_copydata(m, protoff, sizeof(uint8_t), &ah->ah_nxt);
1050 ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(uint32_t);
1051 ah->ah_reserve = 0;
1052 ah->ah_spi = sav->spi;
1053
1054 /* Zeroize authenticator. */
1055 m_copyback(m, skip + rplen, authsize, ipseczeroes);
1056
1057 /* Insert packet replay counter, as requested. */
1058 if (sav->replay) {
1059 if (sav->replay->count == ~0 &&
1060 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
1061 DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n",
1062 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
1063 sizeof(buf)), (u_long) ntohl(sav->spi)));
1064 AH_STATINC(AH_STAT_WRAP);
1065 error = EINVAL;
1066 goto bad;
1067 }
1068 #ifdef IPSEC_DEBUG
1069 /* Emulate replay attack when ipsec_replay is TRUE. */
1070 if (!ipsec_replay)
1071 #endif
1072 sav->replay->count++;
1073 ah->ah_seq = htonl(sav->replay->count);
1074 }
1075
1076 /* Get crypto descriptors. */
1077 crp = crypto_getreq(1);
1078 if (crp == NULL) {
1079 DPRINTF(("%s: failed to acquire crypto descriptors\n",
1080 __func__));
1081 AH_STATINC(AH_STAT_CRYPTO);
1082 error = ENOBUFS;
1083 goto bad;
1084 }
1085
1086 crda = crp->crp_desc;
1087
1088 crda->crd_skip = 0;
1089 crda->crd_inject = skip + rplen;
1090 crda->crd_len = m->m_pkthdr.len;
1091
1092 /* Authentication operation. */
1093 crda->crd_alg = ahx->type;
1094 crda->crd_key = _KEYBUF(sav->key_auth);
1095 crda->crd_klen = _KEYBITS(sav->key_auth);
1096
1097 /* Allocate IPsec-specific opaque crypto info. */
1098 tc = malloc(sizeof(*tc) + skip, M_XDATA, M_NOWAIT|M_ZERO);
1099 if (tc == NULL) {
1100 crypto_freereq(crp);
1101 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
1102 AH_STATINC(AH_STAT_CRYPTO);
1103 error = ENOBUFS;
1104 goto bad;
1105 }
1106
1107 uint8_t *pext = (char *)(tc + 1);
1108 /* Save the skipped portion of the packet. */
1109 m_copydata(m, 0, skip, pext);
1110
1111 /*
1112 * Fix IP header length on the header used for
1113 * authentication. We don't need to fix the original
1114 * header length as it will be fixed by our caller.
1115 */
1116 memcpy(&iplen, pext + ipoffs, sizeof(iplen));
1117 iplen = htons(ntohs(iplen) + rplen + authsize);
1118 m_copyback(m, ipoffs, sizeof(iplen), &iplen);
1119
1120 /* Fix the Next Header field in saved header. */
1121 pext[protoff] = IPPROTO_AH;
1122
1123 /* Update the Next Protocol field in the IP header. */
1124 prot = IPPROTO_AH;
1125 m_copyback(m, protoff, sizeof(prot), &prot);
1126
1127 /* "Massage" the packet headers for crypto processing. */
1128 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
1129 skip, ahx->type, 1);
1130 if (error != 0) {
1131 m = NULL; /* mbuf was free'd by ah_massage_headers. */
1132 free(tc, M_XDATA);
1133 crypto_freereq(crp);
1134 goto bad;
1135 }
1136
1137 /* Crypto operation descriptor. */
1138 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
1139 crp->crp_flags = CRYPTO_F_IMBUF;
1140 crp->crp_buf = m;
1141 crp->crp_callback = ah_output_cb;
1142 crp->crp_sid = sav->tdb_cryptoid;
1143 crp->crp_opaque = tc;
1144
1145 /* These are passed as-is to the callback. */
1146 tc->tc_isr = isr;
1147 KEY_SP_REF(isr->sp);
1148 tc->tc_spi = sav->spi;
1149 tc->tc_dst = sav->sah->saidx.dst;
1150 tc->tc_proto = sav->sah->saidx.proto;
1151 tc->tc_skip = skip;
1152 tc->tc_protoff = protoff;
1153 tc->tc_sav = sav;
1154 KEY_SA_REF(sav);
1155
1156 return crypto_dispatch(crp);
1157 bad:
1158 if (m)
1159 m_freem(m);
1160 return (error);
1161 }
1162
1163 /*
1164 * AH output callback from the crypto driver.
1165 */
1166 static int
1167 ah_output_cb(struct cryptop *crp)
1168 {
1169 int skip, error;
1170 struct tdb_crypto *tc;
1171 struct ipsecrequest *isr;
1172 struct secasvar *sav;
1173 struct mbuf *m;
1174 void *ptr;
1175 int s, err;
1176
1177 KASSERT(crp->crp_opaque != NULL);
1178 tc = crp->crp_opaque;
1179 skip = tc->tc_skip;
1180 ptr = (tc + 1);
1181 m = crp->crp_buf;
1182
1183 s = splsoftnet();
1184 mutex_enter(softnet_lock);
1185
1186 isr = tc->tc_isr;
1187 sav = tc->tc_sav;
1188 if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD)) {
1189 AH_STATINC(AH_STAT_NOTDB);
1190 IPSECLOG(LOG_DEBUG,
1191 "SP is being destroyed while in crypto (id=%u)\n",
1192 isr->sp->id);
1193 error = ENOENT;
1194 goto bad;
1195 }
1196 if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
1197 KEY_FREESAV(&sav);
1198 sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
1199 if (sav == NULL) {
1200 AH_STATINC(AH_STAT_NOTDB);
1201 DPRINTF(("%s: SA expired while in crypto\n", __func__));
1202 error = ENOBUFS; /*XXX*/
1203 goto bad;
1204 }
1205 }
1206
1207 /* Check for crypto errors. */
1208 if (crp->crp_etype) {
1209 if (sav->tdb_cryptoid != 0)
1210 sav->tdb_cryptoid = crp->crp_sid;
1211
1212 if (crp->crp_etype == EAGAIN) {
1213 mutex_exit(softnet_lock);
1214 splx(s);
1215 return crypto_dispatch(crp);
1216 }
1217
1218 AH_STATINC(AH_STAT_NOXFORM);
1219 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
1220 error = crp->crp_etype;
1221 goto bad;
1222 }
1223
1224 AH_STATINC(AH_STAT_HIST + ah_stats[sav->alg_auth]);
1225
1226 /*
1227 * Copy original headers (with the new protocol number) back
1228 * in place.
1229 */
1230 m_copyback(m, 0, skip, ptr);
1231
1232 /* No longer needed. */
1233 free(tc, M_XDATA);
1234 crypto_freereq(crp);
1235
1236 #ifdef IPSEC_DEBUG
1237 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
1238 if (ipsec_integrity) {
1239 int alen;
1240
1241 /*
1242 * Corrupt HMAC if we want to test integrity verification of
1243 * the other side.
1244 */
1245 alen = AUTHSIZE(sav);
1246 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes);
1247 }
1248 #endif
1249
1250 /* NB: m is reclaimed by ipsec_process_done. */
1251 err = ipsec_process_done(m, isr, sav);
1252 KEY_FREESAV(&sav);
1253 KEY_FREESP(&isr->sp);
1254 mutex_exit(softnet_lock);
1255 splx(s);
1256 return err;
1257 bad:
1258 if (sav)
1259 KEY_FREESAV(&sav);
1260 KEY_FREESP(&isr->sp);
1261 mutex_exit(softnet_lock);
1262 splx(s);
1263 if (m)
1264 m_freem(m);
1265 free(tc, M_XDATA);
1266 crypto_freereq(crp);
1267 return error;
1268 }
1269
1270 static struct xformsw ah_xformsw = {
1271 .xf_type = XF_AH,
1272 .xf_flags = XFT_AUTH,
1273 .xf_name = "IPsec AH",
1274 .xf_init = ah_init,
1275 .xf_zeroize = ah_zeroize,
1276 .xf_input = ah_input,
1277 .xf_output = ah_output,
1278 .xf_next = NULL,
1279 };
1280
1281 void
1282 ah_attach(void)
1283 {
1284 ahstat_percpu = percpu_alloc(sizeof(uint64_t) * AH_NSTATS);
1285
1286 #define MAXAUTHSIZE(name) \
1287 if ((auth_hash_ ## name).authsize > ah_max_authsize) \
1288 ah_max_authsize = (auth_hash_ ## name).authsize
1289
1290 ah_max_authsize = 0;
1291 MAXAUTHSIZE(null);
1292 MAXAUTHSIZE(md5);
1293 MAXAUTHSIZE(sha1);
1294 MAXAUTHSIZE(key_md5);
1295 MAXAUTHSIZE(key_sha1);
1296 MAXAUTHSIZE(hmac_md5);
1297 MAXAUTHSIZE(hmac_sha1);
1298 MAXAUTHSIZE(hmac_ripemd_160);
1299 MAXAUTHSIZE(hmac_md5_96);
1300 MAXAUTHSIZE(hmac_sha1_96);
1301 MAXAUTHSIZE(hmac_ripemd_160_96);
1302 MAXAUTHSIZE(hmac_sha2_256);
1303 MAXAUTHSIZE(hmac_sha2_384);
1304 MAXAUTHSIZE(hmac_sha2_512);
1305 MAXAUTHSIZE(aes_xcbc_mac_96);
1306 MAXAUTHSIZE(gmac_aes_128);
1307 MAXAUTHSIZE(gmac_aes_192);
1308 MAXAUTHSIZE(gmac_aes_256);
1309 IPSECLOG(LOG_DEBUG, "ah_max_authsize=%d\n", ah_max_authsize);
1310
1311 #undef MAXAUTHSIZE
1312
1313 xform_register(&ah_xformsw);
1314 }
1315