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