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