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