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