]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/ah_input.c
xnu-517.12.7.tar.gz
[apple/xnu.git] / bsd / netinet6 / ah_input.c
CommitLineData
55e303ae
A
1/* $FreeBSD: src/sys/netinet6/ah_input.c,v 1.1.2.6 2002/04/28 05:40:26 suz Exp $ */
2/* $KAME: ah_input.c,v 1.67 2002/01/07 11:39:56 kjc Exp $ */
9bccf70c 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
1c79356b
A
420#if 1
421 /*
422 * Should the inner packet be considered authentic?
423 * My current answer is: NO.
424 *
425 * host1 -- gw1 === gw2 -- host2
426 * In this case, gw2 can trust the authenticity of the
427 * outer packet, but NOT inner. Packet may be altered
428 * between host1 and gw1.
429 *
430 * host1 -- gw1 === host2
431 * This case falls into the same scenario as above.
432 *
433 * host1 === host2
434 * This case is the only case when we may be able to leave
435 * M_AUTHIPHDR and M_AUTHIPDGM set.
436 * However, if host1 is wrongly configured, and allows
437 * attacker to inject some packet with src=host1 and
438 * dst=host2, you are in risk.
439 */
440 m->m_flags &= ~M_AUTHIPHDR;
441 m->m_flags &= ~M_AUTHIPDGM;
442#endif
443
444 key_sa_recordxfer(sav, m);
9bccf70c
A
445 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
446 ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
447 ipsecstat.in_nomem++;
448 goto fail;
449 }
1c79356b
A
450
451 s = splimp();
452 if (IF_QFULL(&ipintrq)) {
453 ipsecstat.in_inval++;
9bccf70c 454 splx(s);
1c79356b
A
455 goto fail;
456 }
457 IF_ENQUEUE(&ipintrq, m);
458 m = NULL;
459 schednetisr(NETISR_IP); /*can be skipped but to make sure*/
460 splx(s);
461 nxt = IPPROTO_DONE;
462 } else {
463 /*
464 * strip off AH.
1c79356b 465 */
1c79356b
A
466
467 ip = mtod(m, struct ip *);
468#ifndef PULLDOWN_TEST
9bccf70c
A
469 /*
470 * We do deep-copy since KAME requires that
471 * the packet is placed in a single external mbuf.
472 */
1c79356b
A
473 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
474 m->m_data += stripsiz;
475 m->m_len -= stripsiz;
476 m->m_pkthdr.len -= stripsiz;
477#else
478 /*
479 * even in m_pulldown case, we need to strip off AH so that
480 * we can compute checksum for multiple AH correctly.
481 */
482 if (m->m_len >= stripsiz + off) {
483 ovbcopy((caddr_t)ip, ((caddr_t)ip) + stripsiz, off);
484 m->m_data += stripsiz;
485 m->m_len -= stripsiz;
486 m->m_pkthdr.len -= stripsiz;
487 } else {
488 /*
489 * this comes with no copy if the boundary is on
490 * cluster
491 */
492 struct mbuf *n;
493
494 n = m_split(m, off, M_DONTWAIT);
495 if (n == NULL) {
496 /* m is retained by m_split */
497 goto fail;
498 }
499 m_adj(n, stripsiz);
1c79356b
A
500 /* m_cat does not update m_pkthdr.len */
501 m->m_pkthdr.len += n->m_pkthdr.len;
55e303ae 502 m_cat(m, n);
1c79356b
A
503 }
504#endif
505
506 if (m->m_len < sizeof(*ip)) {
507 m = m_pullup(m, sizeof(*ip));
508 if (m == NULL) {
509 ipsecstat.in_inval++;
510 goto fail;
511 }
512 }
513 ip = mtod(m, struct ip *);
514#ifdef IPLEN_FLIPPED
515 ip->ip_len = ip->ip_len - stripsiz;
516#else
517 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
518#endif
519 ip->ip_p = nxt;
520 /* forget about IP hdr checksum, the check has already been passed */
521
522 key_sa_recordxfer(sav, m);
9bccf70c
A
523 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
524 ipsecstat.in_nomem++;
525 goto fail;
526 }
1c79356b 527
9bccf70c
A
528 if (nxt != IPPROTO_DONE) {
529 if ((ip_protox[nxt]->pr_flags & PR_LASTHDR) != 0 &&
530 ipsec4_in_reject(m, NULL)) {
531 ipsecstat.in_polvio++;
532 goto fail;
533 }
1c79356b 534 (*ip_protox[nxt]->pr_input)(m, off);
9bccf70c 535 } else
1c79356b
A
536 m_freem(m);
537 m = NULL;
538 }
539
540 if (sav) {
541 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
542 printf("DP ah4_input call free SA:%p\n", sav));
543 key_freesav(sav);
544 }
545 ipsecstat.in_success++;
546 return;
547
548fail:
549 if (sav) {
550 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
551 printf("DP ah4_input call free SA:%p\n", sav));
552 key_freesav(sav);
553 }
554 if (m)
555 m_freem(m);
556 return;
557}
558#endif /* INET */
559
560#if INET6
561int
55e303ae 562ah6_input(mp, offp)
1c79356b 563 struct mbuf **mp;
55e303ae 564 int *offp;
1c79356b
A
565{
566 struct mbuf *m = *mp;
567 int off = *offp;
568 struct ip6_hdr *ip6;
569 struct ah *ah;
570 u_int32_t spi;
9bccf70c 571 const struct ah_algorithm *algo;
1c79356b
A
572 size_t siz;
573 size_t siz1;
574 u_char *cksum;
575 struct secasvar *sav = NULL;
576 u_int16_t nxt;
577 int s;
9bccf70c 578 size_t stripsiz = 0;
1c79356b
A
579
580#ifndef PULLDOWN_TEST
581 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah), IPPROTO_DONE);
582 ah = (struct ah *)(mtod(m, caddr_t) + off);
583#else
584 IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah));
585 if (ah == NULL) {
586 ipseclog((LOG_DEBUG, "IPv6 AH input: can't pullup\n"));
9bccf70c 587 ipsec6stat.in_inval++;
1c79356b
A
588 return IPPROTO_DONE;
589 }
590#endif
591 ip6 = mtod(m, struct ip6_hdr *);
592 nxt = ah->ah_nxt;
593
594 /* find the sassoc. */
595 spi = ah->ah_spi;
596
597 if (ntohs(ip6->ip6_plen) == 0) {
598 ipseclog((LOG_ERR, "IPv6 AH input: "
599 "AH with IPv6 jumbogram is not supported.\n"));
600 ipsec6stat.in_inval++;
601 goto fail;
602 }
603
604 if ((sav = key_allocsa(AF_INET6,
605 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
606 IPPROTO_AH, spi)) == 0) {
607 ipseclog((LOG_WARNING,
608 "IPv6 AH input: no key association found for spi %u\n",
609 (u_int32_t)ntohl(spi)));
610 ipsec6stat.in_nosa++;
611 goto fail;
612 }
613 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
614 printf("DP ah6_input called to allocate SA:%p\n", sav));
615 if (sav->state != SADB_SASTATE_MATURE
616 && sav->state != SADB_SASTATE_DYING) {
617 ipseclog((LOG_DEBUG,
618 "IPv6 AH input: non-mature/dying SA found for spi %u; ",
619 (u_int32_t)ntohl(spi)));
620 ipsec6stat.in_badspi++;
621 goto fail;
622 }
9bccf70c
A
623
624 algo = ah_algorithm_lookup(sav->alg_auth);
625 if (!algo) {
1c79356b 626 ipseclog((LOG_DEBUG, "IPv6 AH input: "
9bccf70c 627 "unsupported authentication algorithm for spi %u\n",
1c79356b
A
628 (u_int32_t)ntohl(spi)));
629 ipsec6stat.in_badspi++;
630 goto fail;
631 }
632
1c79356b
A
633 siz = (*algo->sumsiz)(sav);
634 siz1 = ((siz + 3) & ~(4 - 1));
635
636 /*
637 * sanity checks for header, 1.
638 */
639 {
640 int sizoff;
641
642 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
643
9bccf70c
A
644 /*
645 * Here, we do not do "siz1 == siz". See ah4_input() for complete
646 * description.
647 */
648 if (siz1 < siz) {
649 ipseclog((LOG_NOTICE, "sum length too short in IPv6 AH input "
650 "(%lu, should be at least %lu): %s\n",
651 (u_long)siz1, (u_long)siz,
652 ipsec6_logpacketstr(ip6, spi)));
653 ipsec6stat.in_inval++;
654 goto fail;
655 }
1c79356b
A
656 if ((ah->ah_len << 2) - sizoff != siz1) {
657 ipseclog((LOG_NOTICE, "sum length mismatch in IPv6 AH input "
9bccf70c
A
658 "(%d should be %lu): %s\n",
659 (ah->ah_len << 2) - sizoff, (u_long)siz1,
660 ipsec6_logpacketstr(ip6, spi)));
1c79356b
A
661 ipsec6stat.in_inval++;
662 goto fail;
663 }
664#ifndef PULLDOWN_TEST
665 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1, IPPROTO_DONE);
666#else
667 IP6_EXTHDR_GET(ah, struct ah *, m, off,
668 sizeof(struct ah) + sizoff + siz1);
669 if (ah == NULL) {
670 ipseclog((LOG_NOTICE, "couldn't pullup gather IPv6 AH checksum part"));
9bccf70c 671 ipsec6stat.in_inval++;
1c79356b
A
672 m = NULL;
673 goto fail;
674 }
675#endif
676 }
677
678 /*
679 * check for sequence number.
680 */
681 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
682 if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav))
683 ; /*okey*/
684 else {
685 ipsec6stat.in_ahreplay++;
686 ipseclog((LOG_WARNING,
687 "replay packet in IPv6 AH input: %s %s\n",
688 ipsec6_logpacketstr(ip6, spi),
689 ipsec_logsastr(sav)));
690 goto fail;
691 }
692 }
693
694 /*
695 * alright, it seems sane. now we are going to check the
696 * cryptographic checksum.
697 */
698 cksum = _MALLOC(siz1, M_TEMP, M_NOWAIT);
699 if (!cksum) {
700 ipseclog((LOG_DEBUG, "IPv6 AH input: "
701 "couldn't alloc temporary region for cksum\n"));
702 ipsec6stat.in_inval++;
703 goto fail;
704 }
705
9bccf70c
A
706 if (ah6_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) {
707 FREE(cksum, M_TEMP);
1c79356b
A
708 ipsec6stat.in_inval++;
709 goto fail;
710 }
711 ipsec6stat.in_ahhist[sav->alg_auth]++;
712
713 {
714 caddr_t sumpos = NULL;
715
716 if (sav->flags & SADB_X_EXT_OLD) {
717 /* RFC 1826 */
718 sumpos = (caddr_t)(ah + 1);
719 } else {
720 /* RFC 2402 */
721 sumpos = (caddr_t)(((struct newah *)ah) + 1);
722 }
723
724 if (bcmp(sumpos, cksum, siz) != 0) {
725 ipseclog((LOG_WARNING,
726 "checksum mismatch in IPv6 AH input: %s %s\n",
727 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
9bccf70c 728 FREE(cksum, M_TEMP);
1c79356b
A
729 ipsec6stat.in_ahauthfail++;
730 goto fail;
731 }
732 }
733
9bccf70c 734 FREE(cksum, M_TEMP);
1c79356b
A
735
736 m->m_flags |= M_AUTHIPHDR;
737 m->m_flags |= M_AUTHIPDGM;
738
739#if 0
740 /*
741 * looks okey, but we need more sanity check.
742 * XXX should elaborate.
743 */
744 if (ah->ah_nxt == IPPROTO_IPV6) {
745 struct ip6_hdr *nip6;
746 size_t sizoff;
747
748 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
749
750 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1
751 + sizeof(struct ip6_hdr), IPPROTO_DONE);
752
753 nip6 = (struct ip6_hdr *)((u_char *)(ah + 1) + sizoff + siz1);
754 if (!IN6_ARE_ADDR_EQUAL(&nip6->ip6_src, &ip6->ip6_src)
755 || !IN6_ARE_ADDR_EQUAL(&nip6->ip6_dst, &ip6->ip6_dst)) {
756 m->m_flags &= ~M_AUTHIPHDR;
757 m->m_flags &= ~M_AUTHIPDGM;
758 }
759 } else if (ah->ah_nxt == IPPROTO_IPIP) {
760 m->m_flags &= ~M_AUTHIPHDR;
761 m->m_flags &= ~M_AUTHIPDGM;
762 } else if (ah->ah_nxt == IPPROTO_IP) {
763 m->m_flags &= ~M_AUTHIPHDR;
764 m->m_flags &= ~M_AUTHIPDGM;
765 }
766#endif
767
768 if (m->m_flags & M_AUTHIPHDR
769 && m->m_flags & M_AUTHIPDGM) {
770#if 0
771 ipseclog((LOG_DEBUG,
772 "IPv6 AH input: authentication succeess\n"));
773#endif
774 ipsec6stat.in_ahauthsucc++;
775 } else {
776 ipseclog((LOG_WARNING,
777 "authentication failed in IPv6 AH input: %s %s\n",
778 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
779 ipsec6stat.in_ahauthfail++;
780 goto fail;
781 }
782
783 /*
784 * update sequence number.
785 */
786 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
787 if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) {
788 ipsec6stat.in_ahreplay++;
789 goto fail;
790 }
791 }
792
793 /* was it transmitted over the IPsec tunnel SA? */
9bccf70c
A
794 if (sav->flags & SADB_X_EXT_OLD) {
795 /* RFC 1826 */
796 stripsiz = sizeof(struct ah) + siz1;
797 } else {
798 /* RFC 2402 */
799 stripsiz = sizeof(struct newah) + siz1;
800 }
801 if (ipsec6_tunnel_validate(m, off + stripsiz, nxt, sav)) {
1c79356b
A
802 /*
803 * strip off all the headers that precedes AH.
804 * IP6 xx AH IP6' payload -> IP6' payload
805 *
806 * XXX more sanity checks
807 * XXX relationship with gif?
808 */
1c79356b
A
809 u_int32_t flowinfo; /*net endian*/
810
811 flowinfo = ip6->ip6_flow;
1c79356b
A
812 m_adj(m, off + stripsiz);
813 if (m->m_len < sizeof(*ip6)) {
814 /*
815 * m_pullup is prohibited in KAME IPv6 input processing
816 * but there's no other way!
817 */
818 m = m_pullup(m, sizeof(*ip6));
819 if (!m) {
820 ipsec6stat.in_inval++;
821 goto fail;
822 }
823 }
824 ip6 = mtod(m, struct ip6_hdr *);
825 /* ECN consideration. */
826 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
827 if (!key_checktunnelsanity(sav, AF_INET6,
828 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
829 ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch "
830 "in IPv6 AH input: %s %s\n",
831 ipsec6_logpacketstr(ip6, spi),
832 ipsec_logsastr(sav)));
833 ipsec6stat.in_inval++;
834 goto fail;
835 }
836
1c79356b
A
837#if 1
838 /*
839 * should the inner packet be considered authentic?
840 * see comment in ah4_input().
841 */
842 m->m_flags &= ~M_AUTHIPHDR;
843 m->m_flags &= ~M_AUTHIPDGM;
844#endif
845
846 key_sa_recordxfer(sav, m);
9bccf70c
A
847 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
848 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
849 ipsec6stat.in_nomem++;
850 goto fail;
851 }
1c79356b
A
852
853 s = splimp();
854 if (IF_QFULL(&ip6intrq)) {
855 ipsec6stat.in_inval++;
9bccf70c 856 splx(s);
1c79356b
A
857 goto fail;
858 }
859 IF_ENQUEUE(&ip6intrq, m);
860 m = NULL;
55e303ae 861 schednetisr(NETISR_IPV6); /* can be skipped but to make sure */
1c79356b
A
862 splx(s);
863 nxt = IPPROTO_DONE;
864 } else {
865 /*
866 * strip off AH.
1c79356b 867 */
1c79356b
A
868 char *prvnxtp;
869
870 /*
871 * Copy the value of the next header field of AH to the
872 * next header field of the previous header.
873 * This is necessary because AH will be stripped off below.
874 */
875 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
876 *prvnxtp = nxt;
877
1c79356b
A
878 ip6 = mtod(m, struct ip6_hdr *);
879#ifndef PULLDOWN_TEST
9bccf70c
A
880 /*
881 * We do deep-copy since KAME requires that
882 * the packet is placed in a single mbuf.
883 */
1c79356b
A
884 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
885 m->m_data += stripsiz;
886 m->m_len -= stripsiz;
887 m->m_pkthdr.len -= stripsiz;
888#else
889 /*
890 * even in m_pulldown case, we need to strip off AH so that
891 * we can compute checksum for multiple AH correctly.
892 */
893 if (m->m_len >= stripsiz + off) {
894 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
895 m->m_data += stripsiz;
896 m->m_len -= stripsiz;
897 m->m_pkthdr.len -= stripsiz;
898 } else {
899 /*
900 * this comes with no copy if the boundary is on
901 * cluster
902 */
903 struct mbuf *n;
904
905 n = m_split(m, off, M_DONTWAIT);
906 if (n == NULL) {
907 /* m is retained by m_split */
908 goto fail;
909 }
910 m_adj(n, stripsiz);
1c79356b
A
911 /* m_cat does not update m_pkthdr.len */
912 m->m_pkthdr.len += n->m_pkthdr.len;
55e303ae 913 m_cat(m, n);
1c79356b
A
914 }
915#endif
916 ip6 = mtod(m, struct ip6_hdr *);
917 /* XXX jumbogram */
918 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
919
920 key_sa_recordxfer(sav, m);
9bccf70c
A
921 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
922 ipsec6stat.in_nomem++;
923 goto fail;
924 }
1c79356b
A
925 }
926
927 *offp = off;
928 *mp = m;
929
930 if (sav) {
931 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
932 printf("DP ah6_input call free SA:%p\n", sav));
933 key_freesav(sav);
934 }
935 ipsec6stat.in_success++;
936 return nxt;
937
938fail:
939 if (sav) {
940 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
941 printf("DP ah6_input call free SA:%p\n", sav));
942 key_freesav(sav);
943 }
944 if (m)
945 m_freem(m);
946 return IPPROTO_DONE;
947}
9bccf70c
A
948
949void
950ah6_ctlinput(cmd, sa, d)
951 int cmd;
952 struct sockaddr *sa;
953 void *d;
954{
955 const struct newah *ahp;
956 struct newah ah;
957 struct secasvar *sav;
958 struct ip6_hdr *ip6;
959 struct mbuf *m;
960 struct ip6ctlparam *ip6cp = NULL;
961 int off;
55e303ae 962 struct sockaddr_in6 *sa6_src, *sa6_dst;
9bccf70c
A
963
964 if (sa->sa_family != AF_INET6 ||
965 sa->sa_len != sizeof(struct sockaddr_in6))
966 return;
967 if ((unsigned)cmd >= PRC_NCMDS)
968 return;
969
970 /* if the parameter is from icmp6, decode it. */
971 if (d != NULL) {
972 ip6cp = (struct ip6ctlparam *)d;
973 m = ip6cp->ip6c_m;
974 ip6 = ip6cp->ip6c_ip6;
975 off = ip6cp->ip6c_off;
976 } else {
977 m = NULL;
978 ip6 = NULL;
979 }
980
981 if (ip6) {
982 /*
983 * XXX: We assume that when ip6 is non NULL,
984 * M and OFF are valid.
985 */
986
987 /* check if we can safely examine src and dst ports */
988 if (m->m_pkthdr.len < off + sizeof(ah))
989 return;
990
991 if (m->m_len < off + sizeof(ah)) {
992 /*
993 * this should be rare case,
994 * so we compromise on this copy...
995 */
996 m_copydata(m, off, sizeof(ah), (caddr_t)&ah);
997 ahp = &ah;
998 } else
999 ahp = (struct newah *)(mtod(m, caddr_t) + off);
1000
1001 if (cmd == PRC_MSGSIZE) {
1002 int valid = 0;
1003
1004 /*
1005 * Check to see if we have a valid SA corresponding to
1006 * the address in the ICMP message payload.
1007 */
55e303ae
A
1008 sa6_src = ip6cp->ip6c_src;
1009 sa6_dst = (struct sockaddr_in6 *)sa;
9bccf70c 1010 sav = key_allocsa(AF_INET6,
55e303ae
A
1011 (caddr_t)&sa6_src->sin6_addr,
1012 (caddr_t)&sa6_dst->sin6_addr,
9bccf70c
A
1013 IPPROTO_AH, ahp->ah_spi);
1014 if (sav) {
1015 if (sav->state == SADB_SASTATE_MATURE ||
1016 sav->state == SADB_SASTATE_DYING)
1017 valid++;
1018 key_freesav(sav);
1019 }
1020
1021 /* XXX Further validation? */
1022
1023 /*
1024 * Depending on the value of "valid" and routing table
1025 * size (mtudisc_{hi,lo}wat), we will:
1026 * - recalcurate the new MTU and create the
1027 * corresponding routing entry, or
1028 * - ignore the MTU change notification.
1029 */
1030 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1031 }
1032
1033 /* we normally notify single pcb here */
1034 } else {
1035 /* we normally notify any pcb here */
1036 }
1037}
1c79356b 1038#endif /* INET6 */