1 /* $FreeBSD: releng/8.2/sys/netipsec/xform_ah.c 199583 2009-11-20 15:27:52Z jhb $ */
2 /* $OpenBSD: ip_ah.c,v 1.63 2001/06/26 06:18:58 angelos Exp $ */
3 /*-
4 * The authors of this code are John Ioannidis (ji@tla.org),
5 * Angelos D. Keromytis (kermit@csd.uch.gr) and
6 * Niels Provos (provos@physnet.uni-hamburg.de).
7 *
8 * The original version of this code was written by John Ioannidis
9 * for BSD/OS in Athens, Greece, in November 1995.
10 *
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
13 *
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15 * and Niels Provos.
16 *
17 * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist.
18 *
19 * Copyright (c) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
20 * Angelos D. Keromytis and Niels Provos.
21 * Copyright (c) 1999 Niklas Hallqvist.
22 * Copyright (c) 2001 Angelos D. Keromytis.
23 *
24 * Permission to use, copy, and modify this software with or without fee
25 * is hereby granted, provided that this entire notice is included in
26 * all copies of any software which is or includes a copy or
27 * modification of this software.
28 * You may use this code under the GNU public license if you so wish. Please
29 * contribute changes back to the authors under this freer than GPL license
30 * so that we may further the use of strong encryption without limitations to
31 * all.
32 *
33 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
34 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
35 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
36 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
37 * PURPOSE.
38 */
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/syslog.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49
50 #include <net/if.h>
51 #include <net/vnet.h>
52
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_ecn.h>
57 #include <netinet/ip6.h>
58
59 #include <net/route.h>
60 #include <netipsec/ipsec.h>
61 #include <netipsec/ah.h>
62 #include <netipsec/ah_var.h>
63 #include <netipsec/xform.h>
64
65 #ifdef INET6
66 #include <netinet6/ip6_var.h>
67 #include <netipsec/ipsec6.h>
68 #include <netinet6/ip6_ecn.h>
69 #endif
70
71 #include <netipsec/key.h>
72 #include <netipsec/key_debug.h>
73
74 #include <opencrypto/cryptodev.h>
75
76 /*
77 * Return header size in bytes. The old protocol did not support
78 * the replay counter; the new protocol always includes the counter.
79 */
80 #define HDRSIZE(sav) \
81 (((sav)->flags & SADB_X_EXT_OLD) ? \
82 sizeof (struct ah) : sizeof (struct ah) + sizeof (u_int32_t))
83 /*
84 * Return authenticator size in bytes. The old protocol is known
85 * to use a fixed 16-byte authenticator. The new algorithm use 12-byte
86 * authenticator.
87 */
88 #define AUTHSIZE(sav) \
89 ((sav->flags & SADB_X_EXT_OLD) ? 16 : AH_HMAC_HASHLEN)
90
91 VNET_DEFINE(int, ah_enable) = 1; /* control flow of packets with AH */
92 VNET_DEFINE(int, ah_cleartos) = 1; /* clear ip_tos when doing AH calc */
93 VNET_DEFINE(struct ahstat, ahstat);
94
95 SYSCTL_DECL(_net_inet_ah);
96 SYSCTL_VNET_INT(_net_inet_ah, OID_AUTO,
97 ah_enable, CTLFLAG_RW, &VNET_NAME(ah_enable), 0, "");
98 SYSCTL_VNET_INT(_net_inet_ah, OID_AUTO,
99 ah_cleartos, CTLFLAG_RW, &VNET_NAME(ah_cleartos), 0, "");
100 SYSCTL_VNET_STRUCT(_net_inet_ah, IPSECCTL_STATS,
101 stats, CTLFLAG_RD, &VNET_NAME(ahstat), ahstat, "");
102
103 static unsigned char ipseczeroes[256]; /* larger than an ip6 extension hdr */
104
105 static int ah_input_cb(struct cryptop*);
106 static int ah_output_cb(struct cryptop*);
107
108 /*
109 * NB: this is public for use by the PF_KEY support.
110 */
111 struct auth_hash *
112 ah_algorithm_lookup(int alg)
113 {
114 if (alg > SADB_AALG_MAX)
115 return NULL;
116 switch (alg) {
117 case SADB_X_AALG_NULL:
118 return &auth_hash_null;
119 case SADB_AALG_MD5HMAC:
120 return &auth_hash_hmac_md5;
121 case SADB_AALG_SHA1HMAC:
122 return &auth_hash_hmac_sha1;
123 case SADB_X_AALG_RIPEMD160HMAC:
124 return &auth_hash_hmac_ripemd_160;
125 case SADB_X_AALG_MD5:
126 return &auth_hash_key_md5;
127 case SADB_X_AALG_SHA:
128 return &auth_hash_key_sha1;
129 case SADB_X_AALG_SHA2_256:
130 return &auth_hash_hmac_sha2_256;
131 case SADB_X_AALG_SHA2_384:
132 return &auth_hash_hmac_sha2_384;
133 case SADB_X_AALG_SHA2_512:
134 return &auth_hash_hmac_sha2_512;
135 }
136 return NULL;
137 }
138
139 size_t
140 ah_hdrsiz(struct secasvar *sav)
141 {
142 size_t size;
143
144 if (sav != NULL) {
145 int authsize;
146 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, ("null xform"));
147 /*XXX not right for null algorithm--does it matter??*/
148 authsize = AUTHSIZE(sav);
149 size = roundup(authsize, sizeof (u_int32_t)) + HDRSIZE(sav);
150 } else {
151 /* default guess */
152 size = sizeof (struct ah) + sizeof (u_int32_t) + 16;
153 }
154 return size;
155 }
156
157 /*
158 * NB: public for use by esp_init.
159 */
160 int
161 ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria)
162 {
163 struct auth_hash *thash;
164 int keylen;
165
166 thash = ah_algorithm_lookup(sav->alg_auth);
167 if (thash == NULL) {
168 DPRINTF(("%s: unsupported authentication algorithm %u\n",
169 __func__, sav->alg_auth));
170 return EINVAL;
171 }
172 /*
173 * Verify the replay state block allocation is consistent with
174 * the protocol type. We check here so we can make assumptions
175 * later during protocol processing.
176 */
177 /* NB: replay state is setup elsewhere (sigh) */
178 if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) {
179 DPRINTF(("%s: replay state block inconsistency, "
180 "%s algorithm %s replay state\n", __func__,
181 (sav->flags & SADB_X_EXT_OLD) ? "old" : "new",
182 sav->replay == NULL ? "without" : "with"));
183 return EINVAL;
184 }
185 if (sav->key_auth == NULL) {
186 DPRINTF(("%s: no authentication key for %s algorithm\n",
187 __func__, thash->name));
188 return EINVAL;
189 }
190 keylen = _KEYLEN(sav->key_auth);
191 if (keylen != thash->keysize && thash->keysize != 0) {
192 DPRINTF(("%s: invalid keylength %d, algorithm %s requires "
193 "keysize %d\n", __func__,
194 keylen, thash->name, thash->keysize));
195 return EINVAL;
196 }
197
198 sav->tdb_xform = xsp;
199 sav->tdb_authalgxform = thash;
200
201 /* Initialize crypto session. */
202 bzero(cria, sizeof (*cria));
203 cria->cri_alg = sav->tdb_authalgxform->type;
204 cria->cri_klen = _KEYBITS(sav->key_auth);
205 cria->cri_key = sav->key_auth->key_data;
206 cria->cri_mlen = AUTHSIZE(sav);
207
208 return 0;
209 }
210
211 /*
212 * ah_init() is called when an SPI is being set up.
213 */
214 static int
215 ah_init(struct secasvar *sav, struct xformsw *xsp)
216 {
217 struct cryptoini cria;
218 int error;
219
220 error = ah_init0(sav, xsp, &cria);
221 return error ? error :
222 crypto_newsession(&sav->tdb_cryptoid, &cria, V_crypto_support);
223 }
224
225 /*
226 * Paranoia.
227 *
228 * NB: public for use by esp_zeroize (XXX).
229 */
230 int
231 ah_zeroize(struct secasvar *sav)
232 {
233 int err;
234
235 if (sav->key_auth)
236 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
237
238 err = crypto_freesession(sav->tdb_cryptoid);
239 sav->tdb_cryptoid = 0;
240 sav->tdb_authalgxform = NULL;
241 sav->tdb_xform = NULL;
242 return err;
243 }
244
245 /*
246 * Massage IPv4/IPv6 headers for AH processing.
247 */
248 static int
249 ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out)
250 {
251 struct mbuf *m = *m0;
252 unsigned char *ptr;
253 int off, count;
254
255 #ifdef INET
256 struct ip *ip;
257 #endif /* INET */
258
259 #ifdef INET6
260 struct ip6_ext *ip6e;
261 struct ip6_hdr ip6;
262 int alloc, len, ad;
263 #endif /* INET6 */
264
265 switch (proto) {
266 #ifdef INET
267 case AF_INET:
268 /*
269 * This is the least painful way of dealing with IPv4 header
270 * and option processing -- just make sure they're in
271 * contiguous memory.
272 */
273 *m0 = m = m_pullup(m, skip);
274 if (m == NULL) {
275 DPRINTF(("%s: m_pullup failed\n", __func__));
276 return ENOBUFS;
277 }
278
279 /* Fix the IP header */
280 ip = mtod(m, struct ip *);
281 if (V_ah_cleartos)
282 ip->ip_tos = 0;
283 ip->ip_ttl = 0;
284 ip->ip_sum = 0;
285
286 /*
287 * On input, fix ip_len which has been byte-swapped
288 * at ip_input().
289 */
290 if (!out) {
291 ip->ip_len = htons(ip->ip_len + skip);
292
293 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
294 ip->ip_off = htons(ip->ip_off & IP_DF);
295 else
296 ip->ip_off = 0;
297 } else {
298 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
299 ip->ip_off = htons(ntohs(ip->ip_off) & IP_DF);
300 else
301 ip->ip_off = 0;
302 }
303
304 ptr = mtod(m, unsigned char *) + sizeof(struct ip);
305
306 /* IPv4 option processing */
307 for (off = sizeof(struct ip); off < skip;) {
308 if (ptr[off] == IPOPT_EOL || ptr[off] == IPOPT_NOP ||
309 off + 1 < skip)
310 ;
311 else {
312 DPRINTF(("%s: illegal IPv4 option length for "
313 "option %d\n", __func__, ptr[off]));
314
315 m_freem(m);
316 return EINVAL;
317 }
318
319 switch (ptr[off]) {
320 case IPOPT_EOL:
321 off = skip; /* End the loop. */
322 break;
323
324 case IPOPT_NOP:
325 off++;
326 break;
327
328 case IPOPT_SECURITY: /* 0x82 */
329 case 0x85: /* Extended security. */
330 case 0x86: /* Commercial security. */
331 case 0x94: /* Router alert */
332 case 0x95: /* RFC1770 */
333 /* Sanity check for option length. */
334 if (ptr[off + 1] < 2) {
335 DPRINTF(("%s: illegal IPv4 option "
336 "length for option %d\n",
337 __func__, ptr[off]));
338
339 m_freem(m);
340 return EINVAL;
341 }
342
343 off += ptr[off + 1];
344 break;
345
346 case IPOPT_LSRR:
347 case IPOPT_SSRR:
348 /* Sanity check for option length. */
349 if (ptr[off + 1] < 2) {
350 DPRINTF(("%s: illegal IPv4 option "
351 "length for option %d\n",
352 __func__, ptr[off]));
353
354 m_freem(m);
355 return EINVAL;
356 }
357
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 bcopy(ptr + off + ptr[off + 1] -
369 sizeof(struct in_addr),
370 &(ip->ip_dst), sizeof(struct in_addr));
371
372 /* Fall through */
373 default:
374 /* Sanity check for option length. */
375 if (ptr[off + 1] < 2) {
376 DPRINTF(("%s: illegal IPv4 option "
377 "length for option %d\n",
378 __func__, ptr[off]));
379 m_freem(m);
380 return EINVAL;
381 }
382
383 /* Zeroize all other options. */
384 count = ptr[off + 1];
385 bcopy(ipseczeroes, ptr, count);
386 off += count;
387 break;
388 }
389
390 /* Sanity check. */
391 if (off > skip) {
392 DPRINTF(("%s: malformed IPv4 options header\n",
393 __func__));
394
395 m_freem(m);
396 return EINVAL;
397 }
398 }
399
400 break;
401 #endif /* INET */
402
403 #ifdef INET6
404 case AF_INET6: /* Ugly... */
405 /* Copy and "cook" the IPv6 header. */
406 m_copydata(m, 0, sizeof(ip6), (caddr_t) &ip6);
407
408 /* We don't do IPv6 Jumbograms. */
409 if (ip6.ip6_plen == 0) {
410 DPRINTF(("%s: unsupported IPv6 jumbogram\n", __func__));
411 m_freem(m);
412 return EMSGSIZE;
413 }
414
415 ip6.ip6_flow = 0;
416 ip6.ip6_hlim = 0;
417 ip6.ip6_vfc &= ~IPV6_VERSION_MASK;
418 ip6.ip6_vfc |= IPV6_VERSION;
419
420 /* Scoped address handling. */
421 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src))
422 ip6.ip6_src.s6_addr16[1] = 0;
423 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst))
424 ip6.ip6_dst.s6_addr16[1] = 0;
425
426 /* Done with IPv6 header. */
427 m_copyback(m, 0, sizeof(struct ip6_hdr), (caddr_t) &ip6);
428
429 /* Let's deal with the remaining headers (if any). */
430 if (skip - sizeof(struct ip6_hdr) > 0) {
431 if (m->m_len <= skip) {
432 ptr = (unsigned char *) malloc(
433 skip - sizeof(struct ip6_hdr),
434 M_XDATA, M_NOWAIT);
435 if (ptr == NULL) {
436 DPRINTF(("%s: failed to allocate memory"
437 "for IPv6 headers\n",__func__));
438 m_freem(m);
439 return ENOBUFS;
440 }
441
442 /*
443 * Copy all the protocol headers after
444 * the IPv6 header.
445 */
446 m_copydata(m, sizeof(struct ip6_hdr),
447 skip - sizeof(struct ip6_hdr), ptr);
448 alloc = 1;
449 } else {
450 /* No need to allocate memory. */
451 ptr = mtod(m, unsigned char *) +
452 sizeof(struct ip6_hdr);
453 alloc = 0;
454 }
455 } else
456 break;
457
458 off = ip6.ip6_nxt & 0xff; /* Next header type. */
459
460 for (len = 0; len < skip - sizeof(struct ip6_hdr);)
461 switch (off) {
462 case IPPROTO_HOPOPTS:
463 case IPPROTO_DSTOPTS:
464 ip6e = (struct ip6_ext *) (ptr + len);
465
466 /*
467 * Process the mutable/immutable
468 * options -- borrows heavily from the
469 * KAME code.
470 */
471 for (count = len + sizeof(struct ip6_ext);
472 count < len + ((ip6e->ip6e_len + 1) << 3);) {
473 if (ptr[count] == IP6OPT_PAD1) {
474 count++;
475 continue; /* Skip padding. */
476 }
477
478 /* Sanity check. */
479 if (count > len +
480 ((ip6e->ip6e_len + 1) << 3)) {
481 m_freem(m);
482
483 /* Free, if we allocated. */
484 if (alloc)
485 free(ptr, M_XDATA);
486 return EINVAL;
487 }
488
489 ad = ptr[count + 1];
490
491 /* If mutable option, zeroize. */
492 if (ptr[count] & IP6OPT_MUTABLE)
493 bcopy(ipseczeroes, ptr + count,
494 ptr[count + 1]);
495
496 count += ad;
497
498 /* Sanity check. */
499 if (count >
500 skip - sizeof(struct ip6_hdr)) {
501 m_freem(m);
502
503 /* Free, if we allocated. */
504 if (alloc)
505 free(ptr, M_XDATA);
506 return EINVAL;
507 }
508 }
509
510 /* Advance. */
511 len += ((ip6e->ip6e_len + 1) << 3);
512 off = ip6e->ip6e_nxt;
513 break;
514
515 case IPPROTO_ROUTING:
516 /*
517 * Always include routing headers in
518 * computation.
519 */
520 ip6e = (struct ip6_ext *) (ptr + len);
521 len += ((ip6e->ip6e_len + 1) << 3);
522 off = ip6e->ip6e_nxt;
523 break;
524
525 default:
526 DPRINTF(("%s: unexpected IPv6 header type %d",
527 __func__, off));
528 if (alloc)
529 free(ptr, M_XDATA);
530 m_freem(m);
531 return EINVAL;
532 }
533
534 /* Copyback and free, if we allocated. */
535 if (alloc) {
536 m_copyback(m, sizeof(struct ip6_hdr),
537 skip - sizeof(struct ip6_hdr), ptr);
538 free(ptr, M_XDATA);
539 }
540
541 break;
542 #endif /* INET6 */
543 }
544
545 return 0;
546 }
547
548 /*
549 * ah_input() gets called to verify that an input packet
550 * passes authentication.
551 */
552 static int
553 ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
554 {
555 struct auth_hash *ahx;
556 struct tdb_ident *tdbi;
557 struct tdb_crypto *tc;
558 struct m_tag *mtag;
559 struct newah *ah;
560 int hl, rplen, authsize;
561
562 struct cryptodesc *crda;
563 struct cryptop *crp;
564
565 IPSEC_ASSERT(sav != NULL, ("null SA"));
566 IPSEC_ASSERT(sav->key_auth != NULL, ("null authentication key"));
567 IPSEC_ASSERT(sav->tdb_authalgxform != NULL,
568 ("null authentication xform"));
569
570 /* Figure out header size. */
571 rplen = HDRSIZE(sav);
572
573 /* XXX don't pullup, just copy header */
574 IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen);
575 if (ah == NULL) {
576 DPRINTF(("ah_input: cannot pullup header\n"));
577 V_ahstat.ahs_hdrops++; /*XXX*/
578 m_freem(m);
579 return ENOBUFS;
580 }
581
582 /* Check replay window, if applicable. */
583 if (sav->replay && !ipsec_chkreplay(ntohl(ah->ah_seq), sav)) {
584 V_ahstat.ahs_replay++;
585 DPRINTF(("%s: packet replay failure: %s\n", __func__,
586 ipsec_logsastr(sav)));
587 m_freem(m);
588 return ENOBUFS;
589 }
590
591 /* Verify AH header length. */
592 hl = ah->ah_len * sizeof (u_int32_t);
593 ahx = sav->tdb_authalgxform;
594 authsize = AUTHSIZE(sav);
595 if (hl != authsize + rplen - sizeof (struct ah)) {
596 DPRINTF(("%s: bad authenticator length %u (expecting %lu)"
597 " for packet in SA %s/%08lx\n", __func__,
598 hl, (u_long) (authsize + rplen - sizeof (struct ah)),
599 ipsec_address(&sav->sah->saidx.dst),
600 (u_long) ntohl(sav->spi)));
601 V_ahstat.ahs_badauthl++;
602 m_freem(m);
603 return EACCES;
604 }
605 V_ahstat.ahs_ibytes += m->m_pkthdr.len - skip - hl;
606
607 /* Get crypto descriptors. */
608 crp = crypto_getreq(1);
609 if (crp == NULL) {
610 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
611 V_ahstat.ahs_crypto++;
612 m_freem(m);
613 return ENOBUFS;
614 }
615
616 crda = crp->crp_desc;
617 IPSEC_ASSERT(crda != NULL, ("null crypto descriptor"));
618
619 crda->crd_skip = 0;
620 crda->crd_len = m->m_pkthdr.len;
621 crda->crd_inject = skip + rplen;
622
623 /* Authentication operation. */
624 crda->crd_alg = ahx->type;
625 crda->crd_klen = _KEYBITS(sav->key_auth);
626 crda->crd_key = sav->key_auth->key_data;
627
628 /* Find out if we've already done crypto. */
629 for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL);
630 mtag != NULL;
631 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) {
632 tdbi = (struct tdb_ident *) (mtag + 1);
633 if (tdbi->proto == sav->sah->saidx.proto &&
634 tdbi->spi == sav->spi &&
635 !bcmp(&tdbi->dst, &sav->sah->saidx.dst,
636 sizeof (union sockaddr_union)))
637 break;
638 }
639
640 /* Allocate IPsec-specific opaque crypto info. */
641 if (mtag == NULL) {
642 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto) +
643 skip + rplen + authsize, M_XDATA, M_NOWAIT|M_ZERO);
644 } else {
645 /* Hash verification has already been done successfully. */
646 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto),
647 M_XDATA, M_NOWAIT|M_ZERO);
648 }
649 if (tc == NULL) {
650 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
651 V_ahstat.ahs_crypto++;
652 crypto_freereq(crp);
653 m_freem(m);
654 return ENOBUFS;
655 }
656
657 /* Only save information if crypto processing is needed. */
658 if (mtag == NULL) {
659 int error;
660
661 /*
662 * Save the authenticator, the skipped portion of the packet,
663 * and the AH header.
664 */
665 m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(tc+1));
666
667 /* Zeroize the authenticator on the packet. */
668 m_copyback(m, skip + rplen, authsize, ipseczeroes);
669
670 /* "Massage" the packet headers for crypto processing. */
671 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
672 skip, ahx->type, 0);
673 if (error != 0) {
674 /* NB: mbuf is free'd by ah_massage_headers */
675 V_ahstat.ahs_hdrops++;
676 free(tc, M_XDATA);
677 crypto_freereq(crp);
678 return error;
679 }
680 }
681
682 /* Crypto operation descriptor. */
683 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
684 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
685 crp->crp_buf = (caddr_t) m;
686 crp->crp_callback = ah_input_cb;
687 crp->crp_sid = sav->tdb_cryptoid;
688 crp->crp_opaque = (caddr_t) tc;
689
690 /* These are passed as-is to the callback. */
691 tc->tc_spi = sav->spi;
692 tc->tc_dst = sav->sah->saidx.dst;
693 tc->tc_proto = sav->sah->saidx.proto;
694 tc->tc_nxt = ah->ah_nxt;
695 tc->tc_protoff = protoff;
696 tc->tc_skip = skip;
697 tc->tc_ptr = (caddr_t) mtag; /* Save the mtag we've identified. */
698
699 if (mtag == NULL)
700 return crypto_dispatch(crp);
701 else
702 return ah_input_cb(crp);
703 }
704
705 #ifdef INET6
706 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
707 if (saidx->dst.sa.sa_family == AF_INET6) { \
708 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
709 } else { \
710 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
711 } \
712 } while (0)
713 #else
714 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
715 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
716 #endif
717
718 /*
719 * AH input callback from the crypto driver.
720 */
721 static int
722 ah_input_cb(struct cryptop *crp)
723 {
724 int rplen, error, skip, protoff;
725 unsigned char calc[AH_ALEN_MAX];
726 struct mbuf *m;
727 struct cryptodesc *crd;
728 struct auth_hash *ahx;
729 struct tdb_crypto *tc;
730 struct m_tag *mtag;
731 struct secasvar *sav;
732 struct secasindex *saidx;
733 u_int8_t nxt;
734 caddr_t ptr;
735 int authsize;
736
737 crd = crp->crp_desc;
738
739 tc = (struct tdb_crypto *) crp->crp_opaque;
740 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
741 skip = tc->tc_skip;
742 nxt = tc->tc_nxt;
743 protoff = tc->tc_protoff;
744 mtag = (struct m_tag *) tc->tc_ptr;
745 m = (struct mbuf *) crp->crp_buf;
746
747 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
748 if (sav == NULL) {
749 V_ahstat.ahs_notdb++;
750 DPRINTF(("%s: SA expired while in crypto\n", __func__));
751 error = ENOBUFS; /*XXX*/
752 goto bad;
753 }
754
755 saidx = &sav->sah->saidx;
756 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
757 saidx->dst.sa.sa_family == AF_INET6,
758 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
759
760 ahx = (struct auth_hash *) sav->tdb_authalgxform;
761
762 /* Check for crypto errors. */
763 if (crp->crp_etype) {
764 if (sav->tdb_cryptoid != 0)
765 sav->tdb_cryptoid = crp->crp_sid;
766
767 if (crp->crp_etype == EAGAIN) {
768 error = crypto_dispatch(crp);
769 return error;
770 }
771
772 V_ahstat.ahs_noxform++;
773 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
774 error = crp->crp_etype;
775 goto bad;
776 } else {
777 V_ahstat.ahs_hist[sav->alg_auth]++;
778 crypto_freereq(crp); /* No longer needed. */
779 crp = NULL;
780 }
781
782 /* Shouldn't happen... */
783 if (m == NULL) {
784 V_ahstat.ahs_crypto++;
785 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
786 error = EINVAL;
787 goto bad;
788 }
789
790 /* Figure out header size. */
791 rplen = HDRSIZE(sav);
792 authsize = AUTHSIZE(sav);
793
794 /* Copy authenticator off the packet. */
795 m_copydata(m, skip + rplen, authsize, calc);
796
797 /*
798 * If we have an mtag, we don't need to verify the authenticator --
799 * it has been verified by an IPsec-aware NIC.
800 */
801 if (mtag == NULL) {
802 ptr = (caddr_t) (tc + 1);
803
804 /* Verify authenticator. */
805 if (bcmp(ptr + skip + rplen, calc, authsize)) {
806 DPRINTF(("%s: authentication hash mismatch for packet "
807 "in SA %s/%08lx\n", __func__,
808 ipsec_address(&saidx->dst),
809 (u_long) ntohl(sav->spi)));
810 V_ahstat.ahs_badauth++;
811 error = EACCES;
812 goto bad;
813 }
814
815 /* Fix the Next Protocol field. */
816 ((u_int8_t *) ptr)[protoff] = nxt;
817
818 /* Copyback the saved (uncooked) network headers. */
819 m_copyback(m, 0, skip, ptr);
820 } else {
821 /* Fix the Next Protocol field. */
822 m_copyback(m, protoff, sizeof(u_int8_t), &nxt);
823 }
824
825 free(tc, M_XDATA), tc = NULL; /* No longer needed */
826
827 /*
828 * Header is now authenticated.
829 */
830 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM;
831
832 /*
833 * Update replay sequence number, if appropriate.
834 */
835 if (sav->replay) {
836 u_int32_t seq;
837
838 m_copydata(m, skip + offsetof(struct newah, ah_seq),
839 sizeof (seq), (caddr_t) &seq);
840 if (ipsec_updatereplay(ntohl(seq), sav)) {
841 V_ahstat.ahs_replay++;
842 error = ENOBUFS; /*XXX as above*/
843 goto bad;
844 }
845 }
846
847 /*
848 * Remove the AH header and authenticator from the mbuf.
849 */
850 error = m_striphdr(m, skip, rplen + authsize);
851 if (error) {
852 DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__,
853 ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
854
855 V_ahstat.ahs_hdrops++;
856 goto bad;
857 }
858
859 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag);
860
861 KEY_FREESAV(&sav);
862 return error;
863 bad:
864 if (sav)
865 KEY_FREESAV(&sav);
866 if (m != NULL)
867 m_freem(m);
868 if (tc != NULL)
869 free(tc, M_XDATA);
870 if (crp != NULL)
871 crypto_freereq(crp);
872 return error;
873 }
874
875 /*
876 * AH output routine, called by ipsec[46]_process_packet().
877 */
878 static int
879 ah_output(
880 struct mbuf *m,
881 struct ipsecrequest *isr,
882 struct mbuf **mp,
883 int skip,
884 int protoff)
885 {
886 struct secasvar *sav;
887 struct auth_hash *ahx;
888 struct cryptodesc *crda;
889 struct tdb_crypto *tc;
890 struct mbuf *mi;
891 struct cryptop *crp;
892 u_int16_t iplen;
893 int error, rplen, authsize, maxpacketsize, roff;
894 u_int8_t prot;
895 struct newah *ah;
896
897 sav = isr->sav;
898 IPSEC_ASSERT(sav != NULL, ("null SA"));
899 ahx = sav->tdb_authalgxform;
900 IPSEC_ASSERT(ahx != NULL, ("null authentication xform"));
901
902 V_ahstat.ahs_output++;
903
904 /* Figure out header size. */
905 rplen = HDRSIZE(sav);
906
907 /* Check for maximum packet size violations. */
908 switch (sav->sah->saidx.dst.sa.sa_family) {
909 #ifdef INET
910 case AF_INET:
911 maxpacketsize = IP_MAXPACKET;
912 break;
913 #endif /* INET */
914 #ifdef INET6
915 case AF_INET6:
916 maxpacketsize = IPV6_MAXPACKET;
917 break;
918 #endif /* INET6 */
919 default:
920 DPRINTF(("%s: unknown/unsupported protocol family %u, "
921 "SA %s/%08lx\n", __func__,
922 sav->sah->saidx.dst.sa.sa_family,
923 ipsec_address(&sav->sah->saidx.dst),
924 (u_long) ntohl(sav->spi)));
925 V_ahstat.ahs_nopf++;
926 error = EPFNOSUPPORT;
927 goto bad;
928 }
929 authsize = AUTHSIZE(sav);
930 if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) {
931 DPRINTF(("%s: packet in SA %s/%08lx got too big "
932 "(len %u, max len %u)\n", __func__,
933 ipsec_address(&sav->sah->saidx.dst),
934 (u_long) ntohl(sav->spi),
935 rplen + authsize + m->m_pkthdr.len, maxpacketsize));
936 V_ahstat.ahs_toobig++;
937 error = EMSGSIZE;
938 goto bad;
939 }
940
941 /* Update the counters. */
942 V_ahstat.ahs_obytes += m->m_pkthdr.len - skip;
943
944 m = m_unshare(m, M_NOWAIT);
945 if (m == NULL) {
946 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
947 ipsec_address(&sav->sah->saidx.dst),
948 (u_long) ntohl(sav->spi)));
949 V_ahstat.ahs_hdrops++;
950 error = ENOBUFS;
951 goto bad;
952 }
953
954 /* Inject AH header. */
955 mi = m_makespace(m, skip, rplen + authsize, &roff);
956 if (mi == NULL) {
957 DPRINTF(("%s: failed to inject %u byte AH header for SA "
958 "%s/%08lx\n", __func__,
959 rplen + authsize,
960 ipsec_address(&sav->sah->saidx.dst),
961 (u_long) ntohl(sav->spi)));
962 V_ahstat.ahs_hdrops++; /*XXX differs from openbsd */
963 error = ENOBUFS;
964 goto bad;
965 }
966
967 /*
968 * The AH header is guaranteed by m_makespace() to be in
969 * contiguous memory, at roff bytes offset into the returned mbuf.
970 */
971 ah = (struct newah *)(mtod(mi, caddr_t) + roff);
972
973 /* Initialize the AH header. */
974 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt);
975 ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(u_int32_t);
976 ah->ah_reserve = 0;
977 ah->ah_spi = sav->spi;
978
979 /* Zeroize authenticator. */
980 m_copyback(m, skip + rplen, authsize, ipseczeroes);
981
982 /* Insert packet replay counter, as requested. */
983 if (sav->replay) {
984 if (sav->replay->count == ~0 &&
985 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
986 DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n",
987 __func__,
988 ipsec_address(&sav->sah->saidx.dst),
989 (u_long) ntohl(sav->spi)));
990 V_ahstat.ahs_wrap++;
991 error = EINVAL;
992 goto bad;
993 }
994 #ifdef REGRESSION
995 /* Emulate replay attack when ipsec_replay is TRUE. */
996 if (!V_ipsec_replay)
997 #endif
998 sav->replay->count++;
999 ah->ah_seq = htonl(sav->replay->count);
1000 }
1001
1002 /* Get crypto descriptors. */
1003 crp = crypto_getreq(1);
1004 if (crp == NULL) {
1005 DPRINTF(("%s: failed to acquire crypto descriptors\n",
1006 __func__));
1007 V_ahstat.ahs_crypto++;
1008 error = ENOBUFS;
1009 goto bad;
1010 }
1011
1012 crda = crp->crp_desc;
1013
1014 crda->crd_skip = 0;
1015 crda->crd_inject = skip + rplen;
1016 crda->crd_len = m->m_pkthdr.len;
1017
1018 /* Authentication operation. */
1019 crda->crd_alg = ahx->type;
1020 crda->crd_key = sav->key_auth->key_data;
1021 crda->crd_klen = _KEYBITS(sav->key_auth);
1022
1023 /* Allocate IPsec-specific opaque crypto info. */
1024 tc = (struct tdb_crypto *) malloc(
1025 sizeof(struct tdb_crypto) + skip, M_XDATA, M_NOWAIT|M_ZERO);
1026 if (tc == NULL) {
1027 crypto_freereq(crp);
1028 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
1029 V_ahstat.ahs_crypto++;
1030 error = ENOBUFS;
1031 goto bad;
1032 }
1033
1034 /* Save the skipped portion of the packet. */
1035 m_copydata(m, 0, skip, (caddr_t) (tc + 1));
1036
1037 /*
1038 * Fix IP header length on the header used for
1039 * authentication. We don't need to fix the original
1040 * header length as it will be fixed by our caller.
1041 */
1042 switch (sav->sah->saidx.dst.sa.sa_family) {
1043 #ifdef INET
1044 case AF_INET:
1045 bcopy(((caddr_t)(tc + 1)) +
1046 offsetof(struct ip, ip_len),
1047 (caddr_t) &iplen, sizeof(u_int16_t));
1048 iplen = htons(ntohs(iplen) + rplen + authsize);
1049 m_copyback(m, offsetof(struct ip, ip_len),
1050 sizeof(u_int16_t), (caddr_t) &iplen);
1051 break;
1052 #endif /* INET */
1053
1054 #ifdef INET6
1055 case AF_INET6:
1056 bcopy(((caddr_t)(tc + 1)) +
1057 offsetof(struct ip6_hdr, ip6_plen),
1058 (caddr_t) &iplen, sizeof(u_int16_t));
1059 iplen = htons(ntohs(iplen) + rplen + authsize);
1060 m_copyback(m, offsetof(struct ip6_hdr, ip6_plen),
1061 sizeof(u_int16_t), (caddr_t) &iplen);
1062 break;
1063 #endif /* INET6 */
1064 }
1065
1066 /* Fix the Next Header field in saved header. */
1067 ((u_int8_t *) (tc + 1))[protoff] = IPPROTO_AH;
1068
1069 /* Update the Next Protocol field in the IP header. */
1070 prot = IPPROTO_AH;
1071 m_copyback(m, protoff, sizeof(u_int8_t), (caddr_t) &prot);
1072
1073 /* "Massage" the packet headers for crypto processing. */
1074 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
1075 skip, ahx->type, 1);
1076 if (error != 0) {
1077 m = NULL; /* mbuf was free'd by ah_massage_headers. */
1078 free(tc, M_XDATA);
1079 crypto_freereq(crp);
1080 goto bad;
1081 }
1082
1083 /* Crypto operation descriptor. */
1084 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
1085 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
1086 crp->crp_buf = (caddr_t) m;
1087 crp->crp_callback = ah_output_cb;
1088 crp->crp_sid = sav->tdb_cryptoid;
1089 crp->crp_opaque = (caddr_t) tc;
1090
1091 /* These are passed as-is to the callback. */
1092 tc->tc_isr = isr;
1093 tc->tc_spi = sav->spi;
1094 tc->tc_dst = sav->sah->saidx.dst;
1095 tc->tc_proto = sav->sah->saidx.proto;
1096 tc->tc_skip = skip;
1097 tc->tc_protoff = protoff;
1098
1099 return crypto_dispatch(crp);
1100 bad:
1101 if (m)
1102 m_freem(m);
1103 return (error);
1104 }
1105
1106 /*
1107 * AH output callback from the crypto driver.
1108 */
1109 static int
1110 ah_output_cb(struct cryptop *crp)
1111 {
1112 int skip, protoff, error;
1113 struct tdb_crypto *tc;
1114 struct ipsecrequest *isr;
1115 struct secasvar *sav;
1116 struct mbuf *m;
1117 caddr_t ptr;
1118 int err;
1119
1120 tc = (struct tdb_crypto *) crp->crp_opaque;
1121 IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
1122 skip = tc->tc_skip;
1123 protoff = tc->tc_protoff;
1124 ptr = (caddr_t) (tc + 1);
1125 m = (struct mbuf *) crp->crp_buf;
1126
1127 isr = tc->tc_isr;
1128 IPSECREQUEST_LOCK(isr);
1129 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
1130 if (sav == NULL) {
1131 V_ahstat.ahs_notdb++;
1132 DPRINTF(("%s: SA expired while in crypto\n", __func__));
1133 error = ENOBUFS; /*XXX*/
1134 goto bad;
1135 }
1136 IPSEC_ASSERT(isr->sav == sav, ("SA changed\n"));
1137
1138 /* Check for crypto errors. */
1139 if (crp->crp_etype) {
1140 if (sav->tdb_cryptoid != 0)
1141 sav->tdb_cryptoid = crp->crp_sid;
1142
1143 if (crp->crp_etype == EAGAIN) {
1144 KEY_FREESAV(&sav);
1145 IPSECREQUEST_UNLOCK(isr);
1146 error = crypto_dispatch(crp);
1147 return error;
1148 }
1149
1150 V_ahstat.ahs_noxform++;
1151 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
1152 error = crp->crp_etype;
1153 goto bad;
1154 }
1155
1156 /* Shouldn't happen... */
1157 if (m == NULL) {
1158 V_ahstat.ahs_crypto++;
1159 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
1160 error = EINVAL;
1161 goto bad;
1162 }
1163 V_ahstat.ahs_hist[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 /* No longer needed. */
1172 free(tc, M_XDATA);
1173 crypto_freereq(crp);
1174
1175 #ifdef REGRESSION
1176 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
1177 if (V_ipsec_integrity) {
1178 int alen;
1179
1180 /*
1181 * Corrupt HMAC if we want to test integrity verification of
1182 * the other side.
1183 */
1184 alen = AUTHSIZE(sav);
1185 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes);
1186 }
1187 #endif
1188
1189 /* NB: m is reclaimed by ipsec_process_done. */
1190 err = ipsec_process_done(m, isr);
1191 KEY_FREESAV(&sav);
1192 IPSECREQUEST_UNLOCK(isr);
1193 return err;
1194 bad:
1195 if (sav)
1196 KEY_FREESAV(&sav);
1197 IPSECREQUEST_UNLOCK(isr);
1198 if (m)
1199 m_freem(m);
1200 free(tc, M_XDATA);
1201 crypto_freereq(crp);
1202 return error;
1203 }
1204
1205 static struct xformsw ah_xformsw = {
1206 XF_AH, XFT_AUTH, "IPsec AH",
1207 ah_init, ah_zeroize, ah_input, ah_output,
1208 };
1209
1210 static void
1211 ah_attach(void)
1212 {
1213
1214 xform_register(&ah_xformsw);
1215 }
1216
1217 SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ah_attach, NULL);
Cache object: 0a8ad41ac90b62174ab598727df436a9
|