xform_ah.c revision 1.62 1 /* $NetBSD: xform_ah.c,v 1.62 2017/07/18 04:01:04 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.62 2017/07/18 04:01:04 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 /* Shouldn't happen... */
849 if (m == NULL) {
850 AH_STATINC(AH_STAT_CRYPTO);
851 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
852 error = EINVAL;
853 goto bad;
854 }
855
856 /* Figure out header size. */
857 rplen = HDRSIZE(sav);
858 authsize = AUTHSIZE(sav);
859
860 if (ipsec_debug)
861 memset(calc, 0, sizeof(calc));
862
863 /* Copy authenticator off the packet. */
864 m_copydata(m, skip + rplen, authsize, calc);
865
866 ptr = (char *)(tc + 1);
867 const uint8_t *pppp = ptr + skip + rplen;
868
869 /* Verify authenticator. */
870 if (!consttime_memequal(pppp, calc, authsize)) {
871 DPRINTF(("%s: authentication hash mismatch " \
872 "over %d bytes " \
873 "for packet in SA %s/%08lx:\n" \
874 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, " \
875 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
876 __func__, authsize,
877 ipsec_address(&saidx->dst, buf, sizeof(buf)),
878 (u_long) ntohl(sav->spi),
879 calc[0], calc[1], calc[2], calc[3],
880 calc[4], calc[5], calc[6], calc[7],
881 calc[8], calc[9], calc[10], calc[11],
882 pppp[0], pppp[1], pppp[2], pppp[3],
883 pppp[4], pppp[5], pppp[6], pppp[7],
884 pppp[8], pppp[9], pppp[10], pppp[11]
885 ));
886 AH_STATINC(AH_STAT_BADAUTH);
887 error = EACCES;
888 goto bad;
889 }
890
891 /* Fix the Next Protocol field. */
892 ptr[protoff] = nxt;
893
894 /* Copyback the saved (uncooked) network headers. */
895 m_copyback(m, 0, skip, ptr);
896
897 free(tc, M_XDATA), tc = NULL; /* No longer needed */
898
899 /*
900 * Header is now authenticated.
901 */
902 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM;
903
904 /*
905 * Update replay sequence number, if appropriate.
906 */
907 if (sav->replay) {
908 uint32_t seq;
909
910 m_copydata(m, skip + offsetof(struct newah, ah_seq),
911 sizeof(seq), &seq);
912 if (ipsec_updatereplay(ntohl(seq), sav)) {
913 AH_STATINC(AH_STAT_REPLAY);
914 error = ENOBUFS; /*XXX as above*/
915 goto bad;
916 }
917 }
918
919 /*
920 * Remove the AH header and authenticator from the mbuf.
921 */
922 error = m_striphdr(m, skip, rplen + authsize);
923 if (error) {
924 DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__,
925 ipsec_address(&saidx->dst, buf, sizeof(buf)),
926 (u_long) ntohl(sav->spi)));
927
928 AH_STATINC(AH_STAT_HDROPS);
929 goto bad;
930 }
931
932 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
933
934 KEY_FREESAV(&sav);
935 mutex_exit(softnet_lock);
936 splx(s);
937 return error;
938 bad:
939 if (sav)
940 KEY_FREESAV(&sav);
941 mutex_exit(softnet_lock);
942 splx(s);
943 if (m != NULL)
944 m_freem(m);
945 if (tc != NULL)
946 free(tc, M_XDATA);
947 if (crp != NULL)
948 crypto_freereq(crp);
949 return error;
950 }
951
952 /*
953 * AH output routine, called by ipsec[46]_process_packet().
954 */
955 static int
956 ah_output(
957 struct mbuf *m,
958 struct ipsecrequest *isr,
959 struct secasvar *sav,
960 struct mbuf **mp,
961 int skip,
962 int protoff
963 )
964 {
965 char buf[IPSEC_ADDRSTRLEN];
966 const struct auth_hash *ahx;
967 struct cryptodesc *crda;
968 struct tdb_crypto *tc;
969 struct mbuf *mi;
970 struct cryptop *crp;
971 uint16_t iplen;
972 int error, rplen, authsize, maxpacketsize, roff;
973 uint8_t prot;
974 struct newah *ah;
975
976 IPSEC_SPLASSERT_SOFTNET(__func__);
977
978 KASSERT(sav != NULL);
979 KASSERT(sav->tdb_authalgxform != NULL);
980 ahx = sav->tdb_authalgxform;
981
982 AH_STATINC(AH_STAT_OUTPUT);
983
984 /* Figure out header size. */
985 rplen = HDRSIZE(sav);
986
987 size_t ipoffs;
988 /* Check for maximum packet size violations. */
989 switch (sav->sah->saidx.dst.sa.sa_family) {
990 #ifdef INET
991 case AF_INET:
992 maxpacketsize = IP_MAXPACKET;
993 ipoffs = offsetof(struct ip, ip_len);
994 break;
995 #endif /* INET */
996 #ifdef INET6
997 case AF_INET6:
998 maxpacketsize = IPV6_MAXPACKET;
999 ipoffs = offsetof(struct ip6_hdr, ip6_plen);
1000 break;
1001 #endif /* INET6 */
1002 default:
1003 DPRINTF(("%s: unknown/unsupported protocol "
1004 "family %u, SA %s/%08lx\n", __func__,
1005 sav->sah->saidx.dst.sa.sa_family,
1006 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
1007 (u_long) ntohl(sav->spi)));
1008 AH_STATINC(AH_STAT_NOPF);
1009 error = EPFNOSUPPORT;
1010 goto bad;
1011 }
1012 authsize = AUTHSIZE(sav);
1013 if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) {
1014 DPRINTF(("%s: packet in SA %s/%08lx got too big "
1015 "(len %u, max len %u)\n", __func__,
1016 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
1017 (u_long) ntohl(sav->spi),
1018 rplen + authsize + m->m_pkthdr.len, maxpacketsize));
1019 AH_STATINC(AH_STAT_TOOBIG);
1020 error = EMSGSIZE;
1021 goto bad;
1022 }
1023
1024 /* Update the counters. */
1025 AH_STATADD(AH_STAT_OBYTES, m->m_pkthdr.len - skip);
1026
1027 m = m_clone(m);
1028 if (m == NULL) {
1029 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
1030 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
1031 (u_long) ntohl(sav->spi)));
1032 AH_STATINC(AH_STAT_HDROPS);
1033 error = ENOBUFS;
1034 goto bad;
1035 }
1036
1037 /* Inject AH header. */
1038 mi = m_makespace(m, skip, rplen + authsize, &roff);
1039 if (mi == NULL) {
1040 DPRINTF(("%s: failed to inject %u byte AH header for SA "
1041 "%s/%08lx\n", __func__,
1042 rplen + authsize,
1043 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
1044 (u_long) ntohl(sav->spi)));
1045 AH_STATINC(AH_STAT_HDROPS); /*XXX differs from openbsd */
1046 error = ENOBUFS;
1047 goto bad;
1048 }
1049
1050 /*
1051 * The AH header is guaranteed by m_makespace() to be in
1052 * contiguous memory, at roff bytes offset into the returned mbuf.
1053 */
1054 ah = (struct newah *)(mtod(mi, char *) + roff);
1055
1056 /* Initialize the AH header. */
1057 m_copydata(m, protoff, sizeof(uint8_t), &ah->ah_nxt);
1058 ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(uint32_t);
1059 ah->ah_reserve = 0;
1060 ah->ah_spi = sav->spi;
1061
1062 /* Zeroize authenticator. */
1063 m_copyback(m, skip + rplen, authsize, ipseczeroes);
1064
1065 /* Insert packet replay counter, as requested. */
1066 if (sav->replay) {
1067 if (sav->replay->count == ~0 &&
1068 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
1069 DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n",
1070 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
1071 sizeof(buf)), (u_long) ntohl(sav->spi)));
1072 AH_STATINC(AH_STAT_WRAP);
1073 error = EINVAL;
1074 goto bad;
1075 }
1076 #ifdef IPSEC_DEBUG
1077 /* Emulate replay attack when ipsec_replay is TRUE. */
1078 if (!ipsec_replay)
1079 #endif
1080 sav->replay->count++;
1081 ah->ah_seq = htonl(sav->replay->count);
1082 }
1083
1084 /* Get crypto descriptors. */
1085 crp = crypto_getreq(1);
1086 if (crp == NULL) {
1087 DPRINTF(("%s: failed to acquire crypto descriptors\n",
1088 __func__));
1089 AH_STATINC(AH_STAT_CRYPTO);
1090 error = ENOBUFS;
1091 goto bad;
1092 }
1093
1094 crda = crp->crp_desc;
1095
1096 crda->crd_skip = 0;
1097 crda->crd_inject = skip + rplen;
1098 crda->crd_len = m->m_pkthdr.len;
1099
1100 /* Authentication operation. */
1101 crda->crd_alg = ahx->type;
1102 crda->crd_key = _KEYBUF(sav->key_auth);
1103 crda->crd_klen = _KEYBITS(sav->key_auth);
1104
1105 /* Allocate IPsec-specific opaque crypto info. */
1106 tc = malloc(sizeof(*tc) + skip, M_XDATA, M_NOWAIT|M_ZERO);
1107 if (tc == NULL) {
1108 crypto_freereq(crp);
1109 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
1110 AH_STATINC(AH_STAT_CRYPTO);
1111 error = ENOBUFS;
1112 goto bad;
1113 }
1114
1115 uint8_t *pext = (char *)(tc + 1);
1116 /* Save the skipped portion of the packet. */
1117 m_copydata(m, 0, skip, pext);
1118
1119 /*
1120 * Fix IP header length on the header used for
1121 * authentication. We don't need to fix the original
1122 * header length as it will be fixed by our caller.
1123 */
1124 memcpy(&iplen, pext + ipoffs, sizeof(iplen));
1125 iplen = htons(ntohs(iplen) + rplen + authsize);
1126 m_copyback(m, ipoffs, sizeof(iplen), &iplen);
1127
1128 /* Fix the Next Header field in saved header. */
1129 pext[protoff] = IPPROTO_AH;
1130
1131 /* Update the Next Protocol field in the IP header. */
1132 prot = IPPROTO_AH;
1133 m_copyback(m, protoff, sizeof(prot), &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 = m;
1149 crp->crp_callback = ah_output_cb;
1150 crp->crp_sid = sav->tdb_cryptoid;
1151 crp->crp_opaque = 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 tc->tc_sav = sav;
1161 KEY_SA_REF(sav);
1162
1163 return crypto_dispatch(crp);
1164 bad:
1165 if (m)
1166 m_freem(m);
1167 return (error);
1168 }
1169
1170 /*
1171 * AH output callback from the crypto driver.
1172 */
1173 static int
1174 ah_output_cb(struct cryptop *crp)
1175 {
1176 int skip, error;
1177 struct tdb_crypto *tc;
1178 struct ipsecrequest *isr;
1179 struct secasvar *sav;
1180 struct mbuf *m;
1181 void *ptr;
1182 int s, err;
1183
1184 KASSERT(crp->crp_opaque != NULL);
1185 tc = crp->crp_opaque;
1186 skip = tc->tc_skip;
1187 ptr = (tc + 1);
1188 m = crp->crp_buf;
1189
1190 s = splsoftnet();
1191 mutex_enter(softnet_lock);
1192
1193 isr = tc->tc_isr;
1194 sav = tc->tc_sav;
1195 if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
1196 KEY_FREESAV(&sav);
1197 sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
1198 if (sav == NULL) {
1199 AH_STATINC(AH_STAT_NOTDB);
1200 DPRINTF(("%s: SA expired while in crypto\n", __func__));
1201 error = ENOBUFS; /*XXX*/
1202 goto bad;
1203 }
1204 }
1205
1206 /* Check for crypto errors. */
1207 if (crp->crp_etype) {
1208 if (sav->tdb_cryptoid != 0)
1209 sav->tdb_cryptoid = crp->crp_sid;
1210
1211 if (crp->crp_etype == EAGAIN) {
1212 KEY_FREESAV(&sav);
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 /* Shouldn't happen... */
1225 if (m == NULL) {
1226 AH_STATINC(AH_STAT_CRYPTO);
1227 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
1228 error = EINVAL;
1229 goto bad;
1230 }
1231 AH_STATINC(AH_STAT_HIST + ah_stats[sav->alg_auth]);
1232
1233 /*
1234 * Copy original headers (with the new protocol number) back
1235 * in place.
1236 */
1237 m_copyback(m, 0, skip, ptr);
1238
1239 /* No longer needed. */
1240 free(tc, M_XDATA);
1241 crypto_freereq(crp);
1242
1243 #ifdef IPSEC_DEBUG
1244 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
1245 if (ipsec_integrity) {
1246 int alen;
1247
1248 /*
1249 * Corrupt HMAC if we want to test integrity verification of
1250 * the other side.
1251 */
1252 alen = AUTHSIZE(sav);
1253 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes);
1254 }
1255 #endif
1256
1257 /* NB: m is reclaimed by ipsec_process_done. */
1258 err = ipsec_process_done(m, isr, sav);
1259 KEY_FREESAV(&sav);
1260 mutex_exit(softnet_lock);
1261 splx(s);
1262 return err;
1263 bad:
1264 if (sav)
1265 KEY_FREESAV(&sav);
1266 mutex_exit(softnet_lock);
1267 splx(s);
1268 if (m)
1269 m_freem(m);
1270 free(tc, M_XDATA);
1271 crypto_freereq(crp);
1272 return error;
1273 }
1274
1275 static struct xformsw ah_xformsw = {
1276 .xf_type = XF_AH,
1277 .xf_flags = XFT_AUTH,
1278 .xf_name = "IPsec AH",
1279 .xf_init = ah_init,
1280 .xf_zeroize = ah_zeroize,
1281 .xf_input = ah_input,
1282 .xf_output = ah_output,
1283 .xf_next = NULL,
1284 };
1285
1286 void
1287 ah_attach(void)
1288 {
1289 ahstat_percpu = percpu_alloc(sizeof(uint64_t) * AH_NSTATS);
1290
1291 #define MAXAUTHSIZE(name) \
1292 if ((auth_hash_ ## name).authsize > ah_max_authsize) \
1293 ah_max_authsize = (auth_hash_ ## name).authsize
1294
1295 ah_max_authsize = 0;
1296 MAXAUTHSIZE(null);
1297 MAXAUTHSIZE(md5);
1298 MAXAUTHSIZE(sha1);
1299 MAXAUTHSIZE(key_md5);
1300 MAXAUTHSIZE(key_sha1);
1301 MAXAUTHSIZE(hmac_md5);
1302 MAXAUTHSIZE(hmac_sha1);
1303 MAXAUTHSIZE(hmac_ripemd_160);
1304 MAXAUTHSIZE(hmac_md5_96);
1305 MAXAUTHSIZE(hmac_sha1_96);
1306 MAXAUTHSIZE(hmac_ripemd_160_96);
1307 MAXAUTHSIZE(hmac_sha2_256);
1308 MAXAUTHSIZE(hmac_sha2_384);
1309 MAXAUTHSIZE(hmac_sha2_512);
1310 MAXAUTHSIZE(aes_xcbc_mac_96);
1311 MAXAUTHSIZE(gmac_aes_128);
1312 MAXAUTHSIZE(gmac_aes_192);
1313 MAXAUTHSIZE(gmac_aes_256);
1314 IPSECLOG(LOG_DEBUG, "ah_max_authsize=%d\n", ah_max_authsize);
1315
1316 #undef MAXAUTHSIZE
1317
1318 xform_register(&ah_xformsw);
1319 }
1320