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