2 /* $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $ */
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).
8 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17 * Additional features in 1999 by Angelos D. Keromytis.
19 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
20 * Angelos D. Keromytis and Niels Provos.
21 * Copyright (c) 2001, Angelos D. Keromytis.
23 * Permission to use, copy, and modify this software with or without fee
24 * is hereby granted, provided that this entire notice is included in
25 * all copies of any software which is or includes a copy or
26 * modification of this software.
27 * You may use this code under the GNU public license if you so wish. Please
28 * contribute changes back to the authors under this freer than GPL license
29 * so that we may further the use of strong encryption without limitations to
32 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
33 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
34 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
35 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
40 * IPsec input processing.
44 #include "opt_inet6.h"
45 #include "opt_ipsec.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
59 #include <net/if_var.h>
61 #include <net/route.h>
62 #include <net/netisr.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip_var.h>
69 #include <netinet/in_var.h>
71 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
75 #include <netinet/in_pcb.h>
77 #include <netinet/icmp6.h>
80 #include <netipsec/ipsec.h>
82 #include <netipsec/ipsec6.h>
84 #include <netipsec/ah_var.h>
85 #include <netipsec/esp.h>
86 #include <netipsec/esp_var.h>
87 #include <netipsec/ipcomp_var.h>
89 #include <netipsec/key.h>
90 #include <netipsec/keydb.h>
92 #include <netipsec/xform.h>
93 #include <netinet6/ip6protosw.h>
95 #include <machine/in_cksum.h>
96 #include <machine/stdarg.h>
99 #include <net/if_enc.h>
103 #define IPSEC_ISTAT(proto, name) do { \
104 if ((proto) == IPPROTO_ESP) \
105 ESPSTAT_INC(esps_##name); \
106 else if ((proto) == IPPROTO_AH) \
107 AHSTAT_INC(ahs_##name); \
109 IPCOMPSTAT_INC(ipcomps_##name); \
113 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
117 * ipsec_common_input gets called when an IPsec-protected packet
118 * is received by IPv4 or IPv6. Its job is to find the right SA
119 * and call the appropriate transform. The transform callback
120 * takes care of further processing (like ingress filtering).
123 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
125 union sockaddr_union dst_address;
126 struct secasvar *sav;
135 IPSEC_ISTAT(sproto, input);
137 IPSEC_ASSERT(m != NULL, ("null packet"));
139 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
140 sproto == IPPROTO_IPCOMP,
141 ("unexpected security protocol %u", sproto));
143 if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
144 (sproto == IPPROTO_AH && !V_ah_enable) ||
145 (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
147 IPSEC_ISTAT(sproto, pdrops);
151 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
153 IPSEC_ISTAT(sproto, hdrops);
154 DPRINTF(("%s: packet too small\n", __func__));
158 /* Retrieve the SPI from the relevant IPsec header */
159 if (sproto == IPPROTO_ESP)
160 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
161 else if (sproto == IPPROTO_AH)
162 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
164 else if (sproto == IPPROTO_IPCOMP) {
166 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
168 spi = ntohl(htons(cpi));
172 * Find the SA and (indirectly) call the appropriate
173 * kernel crypto routine. The resulting mbuf chain is a valid
174 * IP packet ready to go through input processing.
176 bzero(&dst_address, sizeof (dst_address));
177 dst_address.sa.sa_family = af;
181 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
182 m_copydata(m, offsetof(struct ip, ip_dst),
183 sizeof(struct in_addr),
184 (caddr_t) &dst_address.sin.sin_addr);
186 /* Find the source port for NAT-T; see udp*_espdecap. */
187 tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
189 dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1];
190 #endif /* IPSEC_NAT_T */
195 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
196 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
197 sizeof(struct in6_addr),
198 (caddr_t) &dst_address.sin6.sin6_addr);
202 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
204 IPSEC_ISTAT(sproto, nopf);
208 /* NB: only pass dst since key_allocsa follows RFC2401 */
209 sav = KEY_ALLOCSA(&dst_address, sproto, spi);
211 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
212 __func__, ipsec_address(&dst_address),
213 (u_long) ntohl(spi), sproto));
214 IPSEC_ISTAT(sproto, notdb);
219 if (sav->tdb_xform == NULL) {
220 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
221 __func__, ipsec_address(&dst_address),
222 (u_long) ntohl(spi), sproto));
223 IPSEC_ISTAT(sproto, noxform);
230 * Call appropriate transform and return -- callback takes care of
233 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
240 * Common input handler for IPv4 AH, ESP, and IPCOMP.
243 ipsec4_common_input(struct mbuf *m, ...)
249 off = va_arg(ap, int);
250 nxt = va_arg(ap, int);
253 return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
258 ah4_input(struct mbuf **mp, int *offp, int proto)
267 ipsec4_common_input(m, off, IPPROTO_AH);
268 return (IPPROTO_DONE);
271 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
273 if (sa->sa_family == AF_INET &&
274 sa->sa_len == sizeof(struct sockaddr_in))
275 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
279 esp4_input(struct mbuf **mp, int *offp, int proto)
288 ipsec4_common_input(m, off, IPPROTO_ESP);
289 return (IPPROTO_DONE);
293 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
295 if (sa->sa_family == AF_INET &&
296 sa->sa_len == sizeof(struct sockaddr_in))
297 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
301 ipcomp4_input(struct mbuf **mp, int *offp, int proto)
310 ipsec4_common_input(m, off, IPPROTO_IPCOMP);
311 return (IPPROTO_DONE);
315 * IPsec input callback for INET protocols.
316 * This routine is called as the transform callback.
317 * Takes care of filtering and other sanity checks on
318 * the processed packet.
321 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
322 int skip, int protoff, struct m_tag *mt)
324 int prot, af, sproto, isr_prot;
327 struct tdb_ident *tdbi;
328 struct secasindex *saidx;
332 char ip6buf[INET6_ADDRSTRLEN];
336 IPSEC_ASSERT(m != NULL, ("null mbuf"));
337 IPSEC_ASSERT(sav != NULL, ("null SA"));
338 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
339 saidx = &sav->sah->saidx;
340 af = saidx->dst.sa.sa_family;
341 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
342 sproto = saidx->proto;
343 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
344 sproto == IPPROTO_IPCOMP,
345 ("unexpected security protocol %u", sproto));
349 DPRINTF(("%s: null mbuf", __func__));
350 IPSEC_ISTAT(sproto, badkcr);
358 * XXXGL: do we need this entire block?
360 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
361 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
362 __func__, ipsec_address(&sav->sah->saidx.dst),
363 (u_long) ntohl(sav->spi)));
364 IPSEC_ISTAT(sproto, hdrops);
369 ip = mtod(m, struct ip *);
370 ip->ip_len = htons(m->m_pkthdr.len);
372 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
374 ip = mtod(m, struct ip *);
379 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
380 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
383 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
384 * packet later after it has been decapsulated.
386 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
388 if (prot != IPPROTO_IPIP)
389 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
393 /* IP-in-IP encapsulation */
394 if (prot == IPPROTO_IPIP &&
395 saidx->mode != IPSEC_MODE_TRANSPORT) {
397 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
398 IPSEC_ISTAT(sproto, hdrops);
402 /* enc0: strip outer IPv4 header */
403 m_striphdr(m, 0, ip->ip_hl << 2);
406 /* XXX PROXY address isn't recorded in SAH */
408 * Check that the inner source address is the same as
409 * the proxy address, if available.
411 if ((saidx->proxy.sa.sa_family == AF_INET &&
412 saidx->proxy.sin.sin_addr.s_addr !=
415 saidx->proxy.sin.sin_addr.s_addr) ||
416 (saidx->proxy.sa.sa_family != AF_INET &&
417 saidx->proxy.sa.sa_family != 0)) {
419 DPRINTF(("%s: inner source address %s doesn't "
420 "correspond to expected proxy source %s, "
421 "SA %s/%08lx\n", __func__,
422 inet_ntoa4(ipn.ip_src),
423 ipsp_address(saidx->proxy),
424 ipsp_address(saidx->dst),
425 (u_long) ntohl(sav->spi)));
427 IPSEC_ISTAT(sproto, pdrops);
434 /* IPv6-in-IP encapsulation. */
435 if (prot == IPPROTO_IPV6 &&
436 saidx->mode != IPSEC_MODE_TRANSPORT) {
438 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
439 IPSEC_ISTAT(sproto, hdrops);
443 /* enc0: strip IPv4 header, keep IPv6 header only */
444 m_striphdr(m, 0, ip->ip_hl << 2);
447 * Check that the inner source address is the same as
448 * the proxy address, if available.
450 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
451 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
452 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
453 &saidx->proxy.sin6.sin6_addr)) ||
454 (saidx->proxy.sa.sa_family != AF_INET6 &&
455 saidx->proxy.sa.sa_family != 0)) {
457 DPRINTF(("%s: inner source address %s doesn't "
458 "correspond to expected proxy source %s, "
459 "SA %s/%08lx\n", __func__,
460 ip6_sprintf(ip6buf, &ip6n.ip6_src),
461 ipsec_address(&saidx->proxy),
462 ipsec_address(&saidx->dst),
463 (u_long) ntohl(sav->spi)));
465 IPSEC_ISTAT(sproto, pdrops);
474 * Record what we've done to the packet (under what SA it was
475 * processed). If we've been passed an mtag, it means the packet
476 * was already processed by an ethernet/crypto combo card and
477 * thus has a tag attached with all the right information, but
478 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
479 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
481 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
482 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
483 sizeof(struct tdb_ident), M_NOWAIT);
485 DPRINTF(("%s: failed to get tag\n", __func__));
486 IPSEC_ISTAT(sproto, hdrops);
491 tdbi = (struct tdb_ident *)(mtag + 1);
492 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
493 tdbi->proto = sproto;
494 tdbi->spi = sav->spi;
495 /* Cache those two for enc(4) in xform_ipip. */
496 tdbi->alg_auth = sav->alg_auth;
497 tdbi->alg_enc = sav->alg_enc;
499 m_tag_prepend(m, mtag);
500 } else if (mt != NULL) {
501 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
502 /* XXX do we need to mark m_flags??? */
505 key_sa_recordxfer(sav, m); /* record data transfer */
508 * In transport mode requeue decrypted mbuf back to IPv4 protocol
509 * handler. This is necessary to correctly expose rcvif.
511 if (saidx->mode == IPSEC_MODE_TRANSPORT)
515 * Pass the mbuf to enc0 for bpf and pfil.
517 if (prot == IPPROTO_IPIP)
518 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
520 if (prot == IPPROTO_IPV6)
521 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
524 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
529 * Re-dispatch via software interrupt.
534 isr_prot = NETISR_IP;
538 isr_prot = NETISR_IPV6;
542 DPRINTF(("%s: cannot handle inner ip proto %d\n",
544 IPSEC_ISTAT(sproto, nopf);
545 error = EPFNOSUPPORT;
549 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
551 IPSEC_ISTAT(sproto, qfull);
552 DPRINTF(("%s: queue full; proto %u packet dropped\n",
563 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
565 /* XXX nothing just yet */
570 /* IPv6 AH wrapper. */
572 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
578 if (*offp < sizeof(struct ip6_hdr)) {
579 DPRINTF(("%s: bad offset %u\n", __func__, *offp));
581 } else if (*offp == sizeof(struct ip6_hdr)) {
582 protoff = offsetof(struct ip6_hdr, ip6_nxt);
584 /* Chase down the header chain... */
585 protoff = sizeof(struct ip6_hdr);
589 m_copydata(*mp, protoff, sizeof(ip6e),
592 if (ip6e.ip6e_nxt == IPPROTO_AH)
593 l = (ip6e.ip6e_len + 2) << 2;
595 l = (ip6e.ip6e_len + 1) << 3;
596 IPSEC_ASSERT(l > 0, ("l went zero or negative"));
597 } while (protoff + l < *offp);
599 /* Malformed packet check */
600 if (protoff + l != *offp) {
601 DPRINTF(("%s: bad packet header chain, protoff %u, "
602 "l %u, off %u\n", __func__, protoff, l, *offp));
603 IPSEC_ISTAT(proto, hdrops);
608 protoff += offsetof(struct ip6_ext, ip6e_nxt);
610 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
615 * IPsec input callback, called by the transform callback. Takes care of
616 * filtering and other sanity checks on the processed packet.
619 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
622 int prot, af, sproto;
625 struct tdb_ident *tdbi;
626 struct secasindex *saidx;
631 char ip6buf[INET6_ADDRSTRLEN];
634 IPSEC_ASSERT(m != NULL, ("null mbuf"));
635 IPSEC_ASSERT(sav != NULL, ("null SA"));
636 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
637 saidx = &sav->sah->saidx;
638 af = saidx->dst.sa.sa_family;
639 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
640 sproto = saidx->proto;
641 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
642 sproto == IPPROTO_IPCOMP,
643 ("unexpected security protocol %u", sproto));
647 DPRINTF(("%s: null mbuf", __func__));
648 IPSEC_ISTAT(sproto, badkcr);
653 /* Fix IPv6 header */
654 if (m->m_len < sizeof(struct ip6_hdr) &&
655 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
657 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
658 __func__, ipsec_address(&sav->sah->saidx.dst),
659 (u_long) ntohl(sav->spi)));
661 IPSEC_ISTAT(sproto, hdrops);
666 ip6 = mtod(m, struct ip6_hdr *);
667 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
671 m_copydata(m, protoff, 1, (unsigned char *) &prot);
674 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
675 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
678 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
679 * packet later after it has been decapsulated.
681 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
683 /* XXX-BZ does not make sense. */
684 if (prot != IPPROTO_IPIP)
685 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
690 /* IP-in-IP encapsulation */
691 if (prot == IPPROTO_IPIP) {
692 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
693 IPSEC_ISTAT(sproto, hdrops);
697 /* ipn will now contain the inner IPv4 header */
698 m_striphdr(m, 0, skip);
702 * Check that the inner source address is the same as
703 * the proxy address, if available.
705 if ((saidx->proxy.sa.sa_family == AF_INET &&
706 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
707 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
708 (saidx->proxy.sa.sa_family != AF_INET &&
709 saidx->proxy.sa.sa_family != 0)) {
711 DPRINTF(("%s: inner source address %s doesn't "
712 "correspond to expected proxy source %s, "
713 "SA %s/%08lx\n", __func__,
714 inet_ntoa4(ipn.ip_src),
715 ipsec_address(&saidx->proxy),
716 ipsec_address(&saidx->dst),
717 (u_long) ntohl(sav->spi)));
719 IPSEC_ISTAT(sproto, pdrops);
726 /* IPv6-in-IP encapsulation */
727 if (prot == IPPROTO_IPV6) {
728 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
729 IPSEC_ISTAT(sproto, hdrops);
733 /* ip6n will now contain the inner IPv6 header. */
734 m_striphdr(m, 0, skip);
738 * Check that the inner source address is the same as
739 * the proxy address, if available.
741 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
742 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
743 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
744 &saidx->proxy.sin6.sin6_addr)) ||
745 (saidx->proxy.sa.sa_family != AF_INET6 &&
746 saidx->proxy.sa.sa_family != 0)) {
748 DPRINTF(("%s: inner source address %s doesn't "
749 "correspond to expected proxy source %s, "
750 "SA %s/%08lx\n", __func__,
751 ip6_sprintf(ip6buf, &ip6n.ip6_src),
752 ipsec_address(&saidx->proxy),
753 ipsec_address(&saidx->dst),
754 (u_long) ntohl(sav->spi)));
756 IPSEC_ISTAT(sproto, pdrops);
764 * Record what we've done to the packet (under what SA it was
765 * processed). If we've been passed an mtag, it means the packet
766 * was already processed by an ethernet/crypto combo card and
767 * thus has a tag attached with all the right information, but
768 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
769 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
771 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
772 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
773 sizeof(struct tdb_ident), M_NOWAIT);
775 DPRINTF(("%s: failed to get tag\n", __func__));
776 IPSEC_ISTAT(sproto, hdrops);
781 tdbi = (struct tdb_ident *)(mtag + 1);
782 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
783 tdbi->proto = sproto;
784 tdbi->spi = sav->spi;
785 /* Cache those two for enc(4) in xform_ipip. */
786 tdbi->alg_auth = sav->alg_auth;
787 tdbi->alg_enc = sav->alg_enc;
789 m_tag_prepend(m, mtag);
792 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
793 /* XXX do we need to mark m_flags??? */
796 key_sa_recordxfer(sav, m);
800 * Pass the mbuf to enc0 for bpf and pfil.
803 if (prot == IPPROTO_IPIP)
804 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
806 if (prot == IPPROTO_IPV6)
807 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
809 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
812 /* Retrieve new protocol */
813 /* We have stripped the IP6 header from the mbuf, we have to use the backuped proto value instead */
817 * See the end of ip6_input for this logic.
818 * IPPROTO_IPV[46] case will be processed just like other ones
822 while (nxt != IPPROTO_DONE) {
823 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
824 IP6STAT_INC(ip6s_toomanyhdr);
830 * Protection against faulty packet - there should be
831 * more sanity checks in header chain processing.
833 if (m->m_pkthdr.len < skip) {
834 IP6STAT_INC(ip6s_tooshort);
835 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
840 * Enforce IPsec policy checking if we are seeing last header.
841 * note that we do not visit this with protocols with pcb layer
842 * code - like udp/tcp/raw ip.
844 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
845 ipsec6_in_reject(m, NULL)) {
849 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
859 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
861 struct ip6ctlparam *ip6cp = NULL;
862 struct mbuf *m = NULL;
866 if (sa->sa_family != AF_INET6 ||
867 sa->sa_len != sizeof(struct sockaddr_in6))
869 if ((unsigned)cmd >= PRC_NCMDS)
872 /* if the parameter is from icmp6, decode it. */
874 ip6cp = (struct ip6ctlparam *)d;
876 ip6 = ip6cp->ip6c_ip6;
877 off = ip6cp->ip6c_off;
881 off = 0; /* calm gcc */
886 struct ip6ctlparam ip6cp1;
889 * Notify the error to all possible sockets via pfctlinput2.
890 * Since the upper layer information (such as protocol type,
891 * source and destination ports) is embedded in the encrypted
892 * data and might have been cut, we can't directly call
893 * an upper layer ctlinput function. However, the pcbnotify
894 * function will consider source and destination addresses
895 * as well as the flow info value, and may be able to find
896 * some PCB that should be notified.
897 * Although pfctlinput2 will call esp6_ctlinput(), there is
898 * no possibility of an infinite loop of function calls,
899 * because we don't pass the inner IPv6 header.
901 bzero(&ip6cp1, sizeof(ip6cp1));
902 ip6cp1.ip6c_src = ip6cp->ip6c_src;
903 pfctlinput2(cmd, sa, (void *)&ip6cp1);
906 * Then go to special cases that need ESP header information.
907 * XXX: We assume that when ip6 is non NULL,
908 * M and OFF are valid.
911 if (cmd == PRC_MSGSIZE) {
912 struct secasvar *sav;
916 /* check header length before using m_copydata */
917 if (m->m_pkthdr.len < off + sizeof (struct esp))
919 m_copydata(m, off + offsetof(struct esp, esp_spi),
920 sizeof(u_int32_t), (caddr_t) &spi);
922 * Check to see if we have a valid SA corresponding to
923 * the address in the ICMP message payload.
925 sav = KEY_ALLOCSA((union sockaddr_union *)sa,
927 valid = (sav != NULL);
931 /* XXX Further validation? */
934 * Depending on whether the SA is "valid" and
935 * routing table size (mtudisc_{hi,lo}wat), we will:
936 * - recalcurate the new MTU and create the
937 * corresponding routing entry, or
938 * - ignore the MTU change notification.
940 icmp6_mtudisc_update(ip6cp, valid);
943 /* we normally notify any pcb here */