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