]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/ah_input.c
xnu-344.tar.gz
[apple/xnu.git] / bsd / netinet6 / ah_input.c
CommitLineData
9bccf70c
A
1/* $FreeBSD: src/sys/netinet6/ah_input.c,v 1.1.2.4 2001/07/03 11:01:49 ume Exp $ */
2/* $KAME: ah_input.c,v 1.59 2001/05/16 04:01:27 jinmei Exp $ */
3
1c79356b
A
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * RFC1826/2402 authentication header.
35 */
36
1c79356b
A
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/mbuf.h>
41#include <sys/domain.h>
42#include <sys/protosw.h>
43#include <sys/socket.h>
44#include <sys/errno.h>
45#include <sys/time.h>
46#include <sys/kernel.h>
47#include <sys/syslog.h>
48
49#include <net/if.h>
50#include <net/route.h>
51#include <net/netisr.h>
52#include <kern/cpu_number.h>
53
54#include <netinet/in.h>
55#include <netinet/in_systm.h>
56#include <netinet/in_var.h>
57#include <netinet/ip.h>
58#include <netinet/ip_var.h>
59#include <netinet/ip_ecn.h>
9bccf70c
A
60#include <netinet/in_pcb.h>
61#if INET6
62#include <netinet6/ip6_ecn.h>
63#endif
1c79356b
A
64
65#if INET6
66#include <netinet/ip6.h>
67#include <netinet6/ip6_var.h>
9bccf70c 68#include <netinet6/in6_pcb.h>
1c79356b 69#include <netinet/icmp6.h>
9bccf70c 70#include <netinet6/ip6protosw.h>
1c79356b
A
71#endif
72
73#include <netinet6/ipsec.h>
9bccf70c
A
74#if INET6
75#include <netinet6/ipsec6.h>
76#endif
1c79356b 77#include <netinet6/ah.h>
9bccf70c
A
78#if INET6
79#include <netinet6/ah6.h>
80#endif
1c79356b
A
81#include <netkey/key.h>
82#include <netkey/keydb.h>
9bccf70c 83#if IPSEC_DEBUG
1c79356b 84#include <netkey/key_debug.h>
9bccf70c
A
85#else
86#define KEYDEBUG(lev,arg)
87#endif
1c79356b
A
88
89
90#include <net/net_osdep.h>
91
92#define IPLEN_FLIPPED
93
94#if INET
95extern struct protosw inetsw[];
1c79356b
A
96
97void
98ah4_input(struct mbuf *m, int off)
99{
100 struct ip *ip;
101 struct ah *ah;
102 u_int32_t spi;
9bccf70c 103 const struct ah_algorithm *algo;
1c79356b
A
104 size_t siz;
105 size_t siz1;
106 u_char *cksum;
107 struct secasvar *sav = NULL;
108 u_int16_t nxt;
109 size_t hlen;
110 int s;
9bccf70c
A
111 size_t stripsiz = 0;
112
1c79356b
A
113
114#ifndef PULLDOWN_TEST
115 if (m->m_len < off + sizeof(struct newah)) {
116 m = m_pullup(m, off + sizeof(struct newah));
117 if (!m) {
118 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;"
119 "dropping the packet for simplicity\n"));
120 ipsecstat.in_inval++;
121 goto fail;
122 }
123 }
124
125 ip = mtod(m, struct ip *);
126 ah = (struct ah *)(((caddr_t)ip) + off);
127#else
128 ip = mtod(m, struct ip *);
129 IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah));
130 if (ah == NULL) {
131 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;"
132 "dropping the packet for simplicity\n"));
133 ipsecstat.in_inval++;
134 goto fail;
135 }
136#endif
137 nxt = ah->ah_nxt;
138#ifdef _IP_VHL
139 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
140#else
141 hlen = ip->ip_hl << 2;
142#endif
143
144 /* find the sassoc. */
145 spi = ah->ah_spi;
146
147 if ((sav = key_allocsa(AF_INET,
148 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst,
149 IPPROTO_AH, spi)) == 0) {
150 ipseclog((LOG_WARNING,
151 "IPv4 AH input: no key association found for spi %u\n",
152 (u_int32_t)ntohl(spi)));
153 ipsecstat.in_nosa++;
154 goto fail;
155 }
156 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
157 printf("DP ah4_input called to allocate SA:%p\n", sav));
158 if (sav->state != SADB_SASTATE_MATURE
159 && sav->state != SADB_SASTATE_DYING) {
160 ipseclog((LOG_DEBUG,
161 "IPv4 AH input: non-mature/dying SA found for spi %u\n",
162 (u_int32_t)ntohl(spi)));
163 ipsecstat.in_badspi++;
164 goto fail;
165 }
9bccf70c
A
166
167 algo = ah_algorithm_lookup(sav->alg_auth);
168 if (!algo) {
1c79356b 169 ipseclog((LOG_DEBUG, "IPv4 AH input: "
9bccf70c 170 "unsupported authentication algorithm for spi %u\n",
1c79356b
A
171 (u_int32_t)ntohl(spi)));
172 ipsecstat.in_badspi++;
173 goto fail;
174 }
175
1c79356b
A
176 siz = (*algo->sumsiz)(sav);
177 siz1 = ((siz + 3) & ~(4 - 1));
178
179 /*
180 * sanity checks for header, 1.
181 */
182 {
183 int sizoff;
184
185 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
186
9bccf70c
A
187 /*
188 * Here, we do not do "siz1 == siz". This is because the way
189 * RFC240[34] section 2 is written. They do not require truncation
190 * to 96 bits.
191 * For example, Microsoft IPsec stack attaches 160 bits of
192 * authentication data for both hmac-md5 and hmac-sha1. For hmac-sha1,
193 * 32 bits of padding is attached.
194 *
195 * There are two downsides to this specification.
196 * They have no real harm, however, they leave us fuzzy feeling.
197 * - if we attach more than 96 bits of authentication data onto AH,
198 * we will never notice about possible modification by rogue
199 * intermediate nodes.
200 * Since extra bits in AH checksum is never used, this constitutes
201 * no real issue, however, it is wacky.
202 * - even if the peer attaches big authentication data, we will never
203 * notice the difference, since longer authentication data will just
204 * work.
205 *
206 * We may need some clarification in the spec.
207 */
208 if (siz1 < siz) {
209 ipseclog((LOG_NOTICE, "sum length too short in IPv4 AH input "
210 "(%lu, should be at least %lu): %s\n",
211 (u_long)siz1, (u_long)siz,
212 ipsec4_logpacketstr(ip, spi)));
213 ipsecstat.in_inval++;
214 goto fail;
215 }
1c79356b
A
216 if ((ah->ah_len << 2) - sizoff != siz1) {
217 ipseclog((LOG_NOTICE, "sum length mismatch in IPv4 AH input "
9bccf70c
A
218 "(%d should be %lu): %s\n",
219 (ah->ah_len << 2) - sizoff, (u_long)siz1,
220 ipsec4_logpacketstr(ip, spi)));
1c79356b
A
221 ipsecstat.in_inval++;
222 goto fail;
223 }
224
225#ifndef PULLDOWN_TEST
226 if (m->m_len < off + sizeof(struct ah) + sizoff + siz1) {
227 m = m_pullup(m, off + sizeof(struct ah) + sizoff + siz1);
228 if (!m) {
229 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n"));
230 ipsecstat.in_inval++;
231 goto fail;
232 }
233
234 ip = mtod(m, struct ip *);
235 ah = (struct ah *)(((caddr_t)ip) + off);
236 }
237#else
238 IP6_EXTHDR_GET(ah, struct ah *, m, off,
239 sizeof(struct ah) + sizoff + siz1);
240 if (ah == NULL) {
241 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n"));
242 ipsecstat.in_inval++;
243 goto fail;
244 }
245#endif
246 }
247
248 /*
249 * check for sequence number.
250 */
251 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
252 if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav))
253 ; /*okey*/
254 else {
255 ipsecstat.in_ahreplay++;
256 ipseclog((LOG_WARNING,
257 "replay packet in IPv4 AH input: %s %s\n",
258 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
259 goto fail;
260 }
261 }
262
263 /*
264 * alright, it seems sane. now we are going to check the
265 * cryptographic checksum.
266 */
267 cksum = _MALLOC(siz1, M_TEMP, M_NOWAIT);
268 if (!cksum) {
269 ipseclog((LOG_DEBUG, "IPv4 AH input: "
270 "couldn't alloc temporary region for cksum\n"));
271 ipsecstat.in_inval++;
272 goto fail;
273 }
274
1c79356b
A
275 /*
276 * some of IP header fields are flipped to the host endian.
277 * convert them back to network endian. VERY stupid.
278 */
1c79356b 279 ip->ip_len = htons(ip->ip_len + hlen);
1c79356b 280 ip->ip_off = htons(ip->ip_off);
9bccf70c
A
281 if (ah4_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) {
282 FREE(cksum, M_TEMP);
1c79356b
A
283 ipsecstat.in_inval++;
284 goto fail;
285 }
286 ipsecstat.in_ahhist[sav->alg_auth]++;
1c79356b
A
287 /*
288 * flip them back.
289 */
1c79356b 290 ip->ip_len = ntohs(ip->ip_len) - hlen;
1c79356b 291 ip->ip_off = ntohs(ip->ip_off);
1c79356b
A
292
293 {
294 caddr_t sumpos = NULL;
295
296 if (sav->flags & SADB_X_EXT_OLD) {
297 /* RFC 1826 */
298 sumpos = (caddr_t)(ah + 1);
299 } else {
300 /* RFC 2402 */
301 sumpos = (caddr_t)(((struct newah *)ah) + 1);
302 }
303
304 if (bcmp(sumpos, cksum, siz) != 0) {
305 ipseclog((LOG_WARNING,
306 "checksum mismatch in IPv4 AH input: %s %s\n",
307 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
9bccf70c 308 FREE(cksum, M_TEMP);
1c79356b
A
309 ipsecstat.in_ahauthfail++;
310 goto fail;
311 }
312 }
313
9bccf70c 314 FREE(cksum, M_TEMP);
1c79356b
A
315
316 m->m_flags |= M_AUTHIPHDR;
317 m->m_flags |= M_AUTHIPDGM;
318
319#if 0
320 /*
321 * looks okey, but we need more sanity check.
322 * XXX should elaborate.
323 */
324 if (ah->ah_nxt == IPPROTO_IPIP || ah->ah_nxt == IPPROTO_IP) {
325 struct ip *nip;
326 size_t sizoff;
327
328 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
329
330 if (m->m_len < off + sizeof(struct ah) + sizoff + siz1 + hlen) {
331 m = m_pullup(m, off + sizeof(struct ah)
332 + sizoff + siz1 + hlen);
333 if (!m) {
334 ipseclog((LOG_DEBUG,
335 "IPv4 AH input: can't pullup\n"));
336 ipsecstat.in_inval++;
337 goto fail;
338 }
339 }
340
341 nip = (struct ip *)((u_char *)(ah + 1) + sizoff + siz1);
342 if (nip->ip_src.s_addr != ip->ip_src.s_addr
343 || nip->ip_dst.s_addr != ip->ip_dst.s_addr) {
344 m->m_flags &= ~M_AUTHIPHDR;
345 m->m_flags &= ~M_AUTHIPDGM;
346 }
347 }
348#if INET6
349 else if (ah->ah_nxt == IPPROTO_IPV6) {
350 m->m_flags &= ~M_AUTHIPHDR;
351 m->m_flags &= ~M_AUTHIPDGM;
352 }
353#endif /*INET6*/
354#endif /*0*/
355
356 if (m->m_flags & M_AUTHIPHDR
357 && m->m_flags & M_AUTHIPDGM) {
358#if 0
359 ipseclog((LOG_DEBUG,
360 "IPv4 AH input: authentication succeess\n"));
361#endif
362 ipsecstat.in_ahauthsucc++;
363 } else {
364 ipseclog((LOG_WARNING,
365 "authentication failed in IPv4 AH input: %s %s\n",
366 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
367 ipsecstat.in_ahauthfail++;
368 goto fail;
369 }
370
371 /*
372 * update sequence number.
373 */
374 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
375 if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) {
376 ipsecstat.in_ahreplay++;
377 goto fail;
378 }
379 }
380
381 /* was it transmitted over the IPsec tunnel SA? */
9bccf70c
A
382 if (sav->flags & SADB_X_EXT_OLD) {
383 /* RFC 1826 */
384 stripsiz = sizeof(struct ah) + siz1;
385 } else {
386 /* RFC 2402 */
387 stripsiz = sizeof(struct newah) + siz1;
388 }
389 if (ipsec4_tunnel_validate(m, off + stripsiz, nxt, sav)) {
1c79356b
A
390 /*
391 * strip off all the headers that precedes AH.
392 * IP xx AH IP' payload -> IP' payload
393 *
394 * XXX more sanity checks
395 * XXX relationship with gif?
396 */
1c79356b
A
397 u_int8_t tos;
398
399 tos = ip->ip_tos;
1c79356b
A
400 m_adj(m, off + stripsiz);
401 if (m->m_len < sizeof(*ip)) {
402 m = m_pullup(m, sizeof(*ip));
403 if (!m) {
404 ipsecstat.in_inval++;
405 goto fail;
406 }
407 }
408 ip = mtod(m, struct ip *);
409 /* ECN consideration. */
410 ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
411 if (!key_checktunnelsanity(sav, AF_INET,
412 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
413 ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch "
414 "in IPv4 AH input: %s %s\n",
415 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
416 ipsecstat.in_inval++;
417 goto fail;
418 }
419
420#if 0 /* XXX should we call ipfw rather than ipsec_in_reject? */
421 /* drop it if it does not match the default policy */
422 if (ipsec4_in_reject(m, NULL)) {
423 ipsecstat.in_polvio++;
424 goto fail;
425 }
426#endif
427
428#if 1
429 /*
430 * Should the inner packet be considered authentic?
431 * My current answer is: NO.
432 *
433 * host1 -- gw1 === gw2 -- host2
434 * In this case, gw2 can trust the authenticity of the
435 * outer packet, but NOT inner. Packet may be altered
436 * between host1 and gw1.
437 *
438 * host1 -- gw1 === host2
439 * This case falls into the same scenario as above.
440 *
441 * host1 === host2
442 * This case is the only case when we may be able to leave
443 * M_AUTHIPHDR and M_AUTHIPDGM set.
444 * However, if host1 is wrongly configured, and allows
445 * attacker to inject some packet with src=host1 and
446 * dst=host2, you are in risk.
447 */
448 m->m_flags &= ~M_AUTHIPHDR;
449 m->m_flags &= ~M_AUTHIPDGM;
450#endif
451
452 key_sa_recordxfer(sav, m);
9bccf70c
A
453 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
454 ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
455 ipsecstat.in_nomem++;
456 goto fail;
457 }
1c79356b
A
458
459 s = splimp();
460 if (IF_QFULL(&ipintrq)) {
461 ipsecstat.in_inval++;
9bccf70c 462 splx(s);
1c79356b
A
463 goto fail;
464 }
465 IF_ENQUEUE(&ipintrq, m);
466 m = NULL;
467 schednetisr(NETISR_IP); /*can be skipped but to make sure*/
468 splx(s);
469 nxt = IPPROTO_DONE;
470 } else {
471 /*
472 * strip off AH.
1c79356b 473 */
1c79356b
A
474
475 ip = mtod(m, struct ip *);
476#ifndef PULLDOWN_TEST
9bccf70c
A
477 /*
478 * We do deep-copy since KAME requires that
479 * the packet is placed in a single external mbuf.
480 */
1c79356b
A
481 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
482 m->m_data += stripsiz;
483 m->m_len -= stripsiz;
484 m->m_pkthdr.len -= stripsiz;
485#else
486 /*
487 * even in m_pulldown case, we need to strip off AH so that
488 * we can compute checksum for multiple AH correctly.
489 */
490 if (m->m_len >= stripsiz + off) {
491 ovbcopy((caddr_t)ip, ((caddr_t)ip) + stripsiz, off);
492 m->m_data += stripsiz;
493 m->m_len -= stripsiz;
494 m->m_pkthdr.len -= stripsiz;
495 } else {
496 /*
497 * this comes with no copy if the boundary is on
498 * cluster
499 */
500 struct mbuf *n;
501
502 n = m_split(m, off, M_DONTWAIT);
503 if (n == NULL) {
504 /* m is retained by m_split */
505 goto fail;
506 }
507 m_adj(n, stripsiz);
508 m_cat(m, n);
509 /* m_cat does not update m_pkthdr.len */
510 m->m_pkthdr.len += n->m_pkthdr.len;
511 }
512#endif
513
514 if (m->m_len < sizeof(*ip)) {
515 m = m_pullup(m, sizeof(*ip));
516 if (m == NULL) {
517 ipsecstat.in_inval++;
518 goto fail;
519 }
520 }
521 ip = mtod(m, struct ip *);
522#ifdef IPLEN_FLIPPED
523 ip->ip_len = ip->ip_len - stripsiz;
524#else
525 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
526#endif
527 ip->ip_p = nxt;
528 /* forget about IP hdr checksum, the check has already been passed */
529
530 key_sa_recordxfer(sav, m);
9bccf70c
A
531 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
532 ipsecstat.in_nomem++;
533 goto fail;
534 }
1c79356b 535
9bccf70c
A
536 if (nxt != IPPROTO_DONE) {
537 if ((ip_protox[nxt]->pr_flags & PR_LASTHDR) != 0 &&
538 ipsec4_in_reject(m, NULL)) {
539 ipsecstat.in_polvio++;
540 goto fail;
541 }
1c79356b 542 (*ip_protox[nxt]->pr_input)(m, off);
9bccf70c 543 } else
1c79356b
A
544 m_freem(m);
545 m = NULL;
546 }
547
548 if (sav) {
549 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
550 printf("DP ah4_input call free SA:%p\n", sav));
551 key_freesav(sav);
552 }
553 ipsecstat.in_success++;
554 return;
555
556fail:
557 if (sav) {
558 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
559 printf("DP ah4_input call free SA:%p\n", sav));
560 key_freesav(sav);
561 }
562 if (m)
563 m_freem(m);
564 return;
565}
566#endif /* INET */
567
568#if INET6
569int
570ah6_input(mp, offp, proto)
571 struct mbuf **mp;
572 int *offp, proto;
573{
574 struct mbuf *m = *mp;
575 int off = *offp;
576 struct ip6_hdr *ip6;
577 struct ah *ah;
578 u_int32_t spi;
9bccf70c 579 const struct ah_algorithm *algo;
1c79356b
A
580 size_t siz;
581 size_t siz1;
582 u_char *cksum;
583 struct secasvar *sav = NULL;
584 u_int16_t nxt;
585 int s;
9bccf70c 586 size_t stripsiz = 0;
1c79356b
A
587
588#ifndef PULLDOWN_TEST
589 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah), IPPROTO_DONE);
590 ah = (struct ah *)(mtod(m, caddr_t) + off);
591#else
592 IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah));
593 if (ah == NULL) {
594 ipseclog((LOG_DEBUG, "IPv6 AH input: can't pullup\n"));
9bccf70c 595 ipsec6stat.in_inval++;
1c79356b
A
596 return IPPROTO_DONE;
597 }
598#endif
599 ip6 = mtod(m, struct ip6_hdr *);
600 nxt = ah->ah_nxt;
601
602 /* find the sassoc. */
603 spi = ah->ah_spi;
604
605 if (ntohs(ip6->ip6_plen) == 0) {
606 ipseclog((LOG_ERR, "IPv6 AH input: "
607 "AH with IPv6 jumbogram is not supported.\n"));
608 ipsec6stat.in_inval++;
609 goto fail;
610 }
611
612 if ((sav = key_allocsa(AF_INET6,
613 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
614 IPPROTO_AH, spi)) == 0) {
615 ipseclog((LOG_WARNING,
616 "IPv6 AH input: no key association found for spi %u\n",
617 (u_int32_t)ntohl(spi)));
618 ipsec6stat.in_nosa++;
619 goto fail;
620 }
621 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
622 printf("DP ah6_input called to allocate SA:%p\n", sav));
623 if (sav->state != SADB_SASTATE_MATURE
624 && sav->state != SADB_SASTATE_DYING) {
625 ipseclog((LOG_DEBUG,
626 "IPv6 AH input: non-mature/dying SA found for spi %u; ",
627 (u_int32_t)ntohl(spi)));
628 ipsec6stat.in_badspi++;
629 goto fail;
630 }
9bccf70c
A
631
632 algo = ah_algorithm_lookup(sav->alg_auth);
633 if (!algo) {
1c79356b 634 ipseclog((LOG_DEBUG, "IPv6 AH input: "
9bccf70c 635 "unsupported authentication algorithm for spi %u\n",
1c79356b
A
636 (u_int32_t)ntohl(spi)));
637 ipsec6stat.in_badspi++;
638 goto fail;
639 }
640
1c79356b
A
641 siz = (*algo->sumsiz)(sav);
642 siz1 = ((siz + 3) & ~(4 - 1));
643
644 /*
645 * sanity checks for header, 1.
646 */
647 {
648 int sizoff;
649
650 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
651
9bccf70c
A
652 /*
653 * Here, we do not do "siz1 == siz". See ah4_input() for complete
654 * description.
655 */
656 if (siz1 < siz) {
657 ipseclog((LOG_NOTICE, "sum length too short in IPv6 AH input "
658 "(%lu, should be at least %lu): %s\n",
659 (u_long)siz1, (u_long)siz,
660 ipsec6_logpacketstr(ip6, spi)));
661 ipsec6stat.in_inval++;
662 goto fail;
663 }
1c79356b
A
664 if ((ah->ah_len << 2) - sizoff != siz1) {
665 ipseclog((LOG_NOTICE, "sum length mismatch in IPv6 AH input "
9bccf70c
A
666 "(%d should be %lu): %s\n",
667 (ah->ah_len << 2) - sizoff, (u_long)siz1,
668 ipsec6_logpacketstr(ip6, spi)));
1c79356b
A
669 ipsec6stat.in_inval++;
670 goto fail;
671 }
672#ifndef PULLDOWN_TEST
673 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1, IPPROTO_DONE);
674#else
675 IP6_EXTHDR_GET(ah, struct ah *, m, off,
676 sizeof(struct ah) + sizoff + siz1);
677 if (ah == NULL) {
678 ipseclog((LOG_NOTICE, "couldn't pullup gather IPv6 AH checksum part"));
9bccf70c 679 ipsec6stat.in_inval++;
1c79356b
A
680 m = NULL;
681 goto fail;
682 }
683#endif
684 }
685
686 /*
687 * check for sequence number.
688 */
689 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
690 if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav))
691 ; /*okey*/
692 else {
693 ipsec6stat.in_ahreplay++;
694 ipseclog((LOG_WARNING,
695 "replay packet in IPv6 AH input: %s %s\n",
696 ipsec6_logpacketstr(ip6, spi),
697 ipsec_logsastr(sav)));
698 goto fail;
699 }
700 }
701
702 /*
703 * alright, it seems sane. now we are going to check the
704 * cryptographic checksum.
705 */
706 cksum = _MALLOC(siz1, M_TEMP, M_NOWAIT);
707 if (!cksum) {
708 ipseclog((LOG_DEBUG, "IPv6 AH input: "
709 "couldn't alloc temporary region for cksum\n"));
710 ipsec6stat.in_inval++;
711 goto fail;
712 }
713
9bccf70c
A
714 if (ah6_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) {
715 FREE(cksum, M_TEMP);
1c79356b
A
716 ipsec6stat.in_inval++;
717 goto fail;
718 }
719 ipsec6stat.in_ahhist[sav->alg_auth]++;
720
721 {
722 caddr_t sumpos = NULL;
723
724 if (sav->flags & SADB_X_EXT_OLD) {
725 /* RFC 1826 */
726 sumpos = (caddr_t)(ah + 1);
727 } else {
728 /* RFC 2402 */
729 sumpos = (caddr_t)(((struct newah *)ah) + 1);
730 }
731
732 if (bcmp(sumpos, cksum, siz) != 0) {
733 ipseclog((LOG_WARNING,
734 "checksum mismatch in IPv6 AH input: %s %s\n",
735 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
9bccf70c 736 FREE(cksum, M_TEMP);
1c79356b
A
737 ipsec6stat.in_ahauthfail++;
738 goto fail;
739 }
740 }
741
9bccf70c 742 FREE(cksum, M_TEMP);
1c79356b
A
743
744 m->m_flags |= M_AUTHIPHDR;
745 m->m_flags |= M_AUTHIPDGM;
746
747#if 0
748 /*
749 * looks okey, but we need more sanity check.
750 * XXX should elaborate.
751 */
752 if (ah->ah_nxt == IPPROTO_IPV6) {
753 struct ip6_hdr *nip6;
754 size_t sizoff;
755
756 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
757
758 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1
759 + sizeof(struct ip6_hdr), IPPROTO_DONE);
760
761 nip6 = (struct ip6_hdr *)((u_char *)(ah + 1) + sizoff + siz1);
762 if (!IN6_ARE_ADDR_EQUAL(&nip6->ip6_src, &ip6->ip6_src)
763 || !IN6_ARE_ADDR_EQUAL(&nip6->ip6_dst, &ip6->ip6_dst)) {
764 m->m_flags &= ~M_AUTHIPHDR;
765 m->m_flags &= ~M_AUTHIPDGM;
766 }
767 } else if (ah->ah_nxt == IPPROTO_IPIP) {
768 m->m_flags &= ~M_AUTHIPHDR;
769 m->m_flags &= ~M_AUTHIPDGM;
770 } else if (ah->ah_nxt == IPPROTO_IP) {
771 m->m_flags &= ~M_AUTHIPHDR;
772 m->m_flags &= ~M_AUTHIPDGM;
773 }
774#endif
775
776 if (m->m_flags & M_AUTHIPHDR
777 && m->m_flags & M_AUTHIPDGM) {
778#if 0
779 ipseclog((LOG_DEBUG,
780 "IPv6 AH input: authentication succeess\n"));
781#endif
782 ipsec6stat.in_ahauthsucc++;
783 } else {
784 ipseclog((LOG_WARNING,
785 "authentication failed in IPv6 AH input: %s %s\n",
786 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
787 ipsec6stat.in_ahauthfail++;
788 goto fail;
789 }
790
791 /*
792 * update sequence number.
793 */
794 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
795 if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) {
796 ipsec6stat.in_ahreplay++;
797 goto fail;
798 }
799 }
800
801 /* was it transmitted over the IPsec tunnel SA? */
9bccf70c
A
802 if (sav->flags & SADB_X_EXT_OLD) {
803 /* RFC 1826 */
804 stripsiz = sizeof(struct ah) + siz1;
805 } else {
806 /* RFC 2402 */
807 stripsiz = sizeof(struct newah) + siz1;
808 }
809 if (ipsec6_tunnel_validate(m, off + stripsiz, nxt, sav)) {
1c79356b
A
810 /*
811 * strip off all the headers that precedes AH.
812 * IP6 xx AH IP6' payload -> IP6' payload
813 *
814 * XXX more sanity checks
815 * XXX relationship with gif?
816 */
1c79356b
A
817 u_int32_t flowinfo; /*net endian*/
818
819 flowinfo = ip6->ip6_flow;
1c79356b
A
820 m_adj(m, off + stripsiz);
821 if (m->m_len < sizeof(*ip6)) {
822 /*
823 * m_pullup is prohibited in KAME IPv6 input processing
824 * but there's no other way!
825 */
826 m = m_pullup(m, sizeof(*ip6));
827 if (!m) {
828 ipsec6stat.in_inval++;
829 goto fail;
830 }
831 }
832 ip6 = mtod(m, struct ip6_hdr *);
833 /* ECN consideration. */
834 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
835 if (!key_checktunnelsanity(sav, AF_INET6,
836 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
837 ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch "
838 "in IPv6 AH input: %s %s\n",
839 ipsec6_logpacketstr(ip6, spi),
840 ipsec_logsastr(sav)));
841 ipsec6stat.in_inval++;
842 goto fail;
843 }
844
845#if 0 /* XXX should we call ipfw rather than ipsec_in_reject? */
846 /* drop it if it does not match the default policy */
847 if (ipsec6_in_reject(m, NULL)) {
848 ipsec6stat.in_polvio++;
849 goto fail;
850 }
851#endif
852
853#if 1
854 /*
855 * should the inner packet be considered authentic?
856 * see comment in ah4_input().
857 */
858 m->m_flags &= ~M_AUTHIPHDR;
859 m->m_flags &= ~M_AUTHIPDGM;
860#endif
861
862 key_sa_recordxfer(sav, m);
9bccf70c
A
863 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
864 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
865 ipsec6stat.in_nomem++;
866 goto fail;
867 }
1c79356b
A
868
869 s = splimp();
870 if (IF_QFULL(&ip6intrq)) {
871 ipsec6stat.in_inval++;
9bccf70c 872 splx(s);
1c79356b
A
873 goto fail;
874 }
875 IF_ENQUEUE(&ip6intrq, m);
876 m = NULL;
877 schednetisr(NETISR_IPV6); /*can be skipped but to make sure*/
878 splx(s);
879 nxt = IPPROTO_DONE;
880 } else {
881 /*
882 * strip off AH.
1c79356b 883 */
1c79356b
A
884 char *prvnxtp;
885
886 /*
887 * Copy the value of the next header field of AH to the
888 * next header field of the previous header.
889 * This is necessary because AH will be stripped off below.
890 */
891 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
892 *prvnxtp = nxt;
893
1c79356b
A
894 ip6 = mtod(m, struct ip6_hdr *);
895#ifndef PULLDOWN_TEST
9bccf70c
A
896 /*
897 * We do deep-copy since KAME requires that
898 * the packet is placed in a single mbuf.
899 */
1c79356b
A
900 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
901 m->m_data += stripsiz;
902 m->m_len -= stripsiz;
903 m->m_pkthdr.len -= stripsiz;
904#else
905 /*
906 * even in m_pulldown case, we need to strip off AH so that
907 * we can compute checksum for multiple AH correctly.
908 */
909 if (m->m_len >= stripsiz + off) {
910 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
911 m->m_data += stripsiz;
912 m->m_len -= stripsiz;
913 m->m_pkthdr.len -= stripsiz;
914 } else {
915 /*
916 * this comes with no copy if the boundary is on
917 * cluster
918 */
919 struct mbuf *n;
920
921 n = m_split(m, off, M_DONTWAIT);
922 if (n == NULL) {
923 /* m is retained by m_split */
924 goto fail;
925 }
926 m_adj(n, stripsiz);
927 m_cat(m, n);
928 /* m_cat does not update m_pkthdr.len */
929 m->m_pkthdr.len += n->m_pkthdr.len;
930 }
931#endif
932 ip6 = mtod(m, struct ip6_hdr *);
933 /* XXX jumbogram */
934 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
935
936 key_sa_recordxfer(sav, m);
9bccf70c
A
937 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
938 ipsec6stat.in_nomem++;
939 goto fail;
940 }
1c79356b
A
941 }
942
943 *offp = off;
944 *mp = m;
945
946 if (sav) {
947 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
948 printf("DP ah6_input call free SA:%p\n", sav));
949 key_freesav(sav);
950 }
951 ipsec6stat.in_success++;
952 return nxt;
953
954fail:
955 if (sav) {
956 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
957 printf("DP ah6_input call free SA:%p\n", sav));
958 key_freesav(sav);
959 }
960 if (m)
961 m_freem(m);
962 return IPPROTO_DONE;
963}
9bccf70c
A
964
965void
966ah6_ctlinput(cmd, sa, d)
967 int cmd;
968 struct sockaddr *sa;
969 void *d;
970{
971 const struct newah *ahp;
972 struct newah ah;
973 struct secasvar *sav;
974 struct ip6_hdr *ip6;
975 struct mbuf *m;
976 struct ip6ctlparam *ip6cp = NULL;
977 int off;
978 struct sockaddr_in6 sa6_src, sa6_dst;
979
980 if (sa->sa_family != AF_INET6 ||
981 sa->sa_len != sizeof(struct sockaddr_in6))
982 return;
983 if ((unsigned)cmd >= PRC_NCMDS)
984 return;
985
986 /* if the parameter is from icmp6, decode it. */
987 if (d != NULL) {
988 ip6cp = (struct ip6ctlparam *)d;
989 m = ip6cp->ip6c_m;
990 ip6 = ip6cp->ip6c_ip6;
991 off = ip6cp->ip6c_off;
992 } else {
993 m = NULL;
994 ip6 = NULL;
995 }
996
997 if (ip6) {
998 /*
999 * XXX: We assume that when ip6 is non NULL,
1000 * M and OFF are valid.
1001 */
1002
1003 /* check if we can safely examine src and dst ports */
1004 if (m->m_pkthdr.len < off + sizeof(ah))
1005 return;
1006
1007 if (m->m_len < off + sizeof(ah)) {
1008 /*
1009 * this should be rare case,
1010 * so we compromise on this copy...
1011 */
1012 m_copydata(m, off, sizeof(ah), (caddr_t)&ah);
1013 ahp = &ah;
1014 } else
1015 ahp = (struct newah *)(mtod(m, caddr_t) + off);
1016
1017 if (cmd == PRC_MSGSIZE) {
1018 int valid = 0;
1019
1020 /*
1021 * Check to see if we have a valid SA corresponding to
1022 * the address in the ICMP message payload.
1023 */
1024 sav = key_allocsa(AF_INET6,
1025 (caddr_t)&sa6_src.sin6_addr,
1026 (caddr_t)&sa6_dst.sin6_addr,
1027 IPPROTO_AH, ahp->ah_spi);
1028 if (sav) {
1029 if (sav->state == SADB_SASTATE_MATURE ||
1030 sav->state == SADB_SASTATE_DYING)
1031 valid++;
1032 key_freesav(sav);
1033 }
1034
1035 /* XXX Further validation? */
1036
1037 /*
1038 * Depending on the value of "valid" and routing table
1039 * size (mtudisc_{hi,lo}wat), we will:
1040 * - recalcurate the new MTU and create the
1041 * corresponding routing entry, or
1042 * - ignore the MTU change notification.
1043 */
1044 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1045 }
1046
1047 /* we normally notify single pcb here */
1048 } else {
1049 /* we normally notify any pcb here */
1050 }
1051}
1c79356b 1052#endif /* INET6 */