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>
60 #include <net/route.h>
61 #include <net/netisr.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/in_var.h>
69 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
73 #include <netinet/in_pcb.h>
75 #include <netinet/icmp6.h>
78 #include <netipsec/ipsec.h>
80 #include <netipsec/ipsec6.h>
82 #include <netipsec/ah_var.h>
83 #include <netipsec/esp.h>
84 #include <netipsec/esp_var.h>
85 #include <netipsec/ipcomp_var.h>
87 #include <netipsec/key.h>
88 #include <netipsec/keydb.h>
90 #include <netipsec/xform.h>
91 #include <netinet6/ip6protosw.h>
93 #include <machine/in_cksum.h>
94 #include <machine/stdarg.h>
97 #include <net/if_enc.h>
101 #define IPSEC_ISTAT(p,x,y,z) ((p) == IPPROTO_ESP ? (x)++ : \
102 (p) == IPPROTO_AH ? (y)++ : (z)++)
104 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
107 * ipsec_common_input gets called when an IPsec-protected packet
108 * is received by IPv4 or IPv6. It's job is to find the right SA
109 * and call the appropriate transform. The transform callback
110 * takes care of further processing (like ingress filtering).
113 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
115 union sockaddr_union dst_address;
116 struct secasvar *sav;
120 IPSEC_ISTAT(sproto, espstat.esps_input, ahstat.ahs_input,
121 ipcompstat.ipcomps_input);
123 IPSEC_ASSERT(m != NULL, ("null packet"));
125 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
126 sproto == IPPROTO_IPCOMP,
127 ("unexpected security protocol %u", sproto));
129 if ((sproto == IPPROTO_ESP && !esp_enable) ||
130 (sproto == IPPROTO_AH && !ah_enable) ||
131 (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
133 IPSEC_ISTAT(sproto, espstat.esps_pdrops, ahstat.ahs_pdrops,
134 ipcompstat.ipcomps_pdrops);
138 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
140 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
141 ipcompstat.ipcomps_hdrops);
142 DPRINTF(("%s: packet too small\n", __func__));
146 /* Retrieve the SPI from the relevant IPsec header */
147 if (sproto == IPPROTO_ESP)
148 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
149 else if (sproto == IPPROTO_AH)
150 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
152 else if (sproto == IPPROTO_IPCOMP) {
154 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
156 spi = ntohl(htons(cpi));
160 * Find the SA and (indirectly) call the appropriate
161 * kernel crypto routine. The resulting mbuf chain is a valid
162 * IP packet ready to go through input processing.
164 bzero(&dst_address, sizeof (dst_address));
165 dst_address.sa.sa_family = af;
169 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
170 m_copydata(m, offsetof(struct ip, ip_dst),
171 sizeof(struct in_addr),
172 (caddr_t) &dst_address.sin.sin_addr);
177 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
178 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
179 sizeof(struct in6_addr),
180 (caddr_t) &dst_address.sin6.sin6_addr);
184 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
186 IPSEC_ISTAT(sproto, espstat.esps_nopf, ahstat.ahs_nopf,
187 ipcompstat.ipcomps_nopf);
191 /* NB: only pass dst since key_allocsa follows RFC2401 */
192 sav = KEY_ALLOCSA(&dst_address, sproto, spi);
194 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
195 __func__, ipsec_address(&dst_address),
196 (u_long) ntohl(spi), sproto));
197 IPSEC_ISTAT(sproto, espstat.esps_notdb, ahstat.ahs_notdb,
198 ipcompstat.ipcomps_notdb);
203 if (sav->tdb_xform == NULL) {
204 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
205 __func__, ipsec_address(&dst_address),
206 (u_long) ntohl(spi), sproto));
207 IPSEC_ISTAT(sproto, espstat.esps_noxform, ahstat.ahs_noxform,
208 ipcompstat.ipcomps_noxform);
215 * Call appropriate transform and return -- callback takes care of
218 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
225 * Common input handler for IPv4 AH, ESP, and IPCOMP.
228 ipsec4_common_input(struct mbuf *m, ...)
234 off = va_arg(ap, int);
235 nxt = va_arg(ap, int);
238 return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
243 ah4_input(struct mbuf *m, int off)
245 ipsec4_common_input(m, off, IPPROTO_AH);
248 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
250 if (sa->sa_family == AF_INET &&
251 sa->sa_len == sizeof(struct sockaddr_in))
252 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
256 esp4_input(struct mbuf *m, int off)
258 ipsec4_common_input(m, off, IPPROTO_ESP);
261 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
263 if (sa->sa_family == AF_INET &&
264 sa->sa_len == sizeof(struct sockaddr_in))
265 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
269 ipcomp4_input(struct mbuf *m, int off)
271 ipsec4_common_input(m, off, IPPROTO_IPCOMP);
275 * IPsec input callback for INET protocols.
276 * This routine is called as the transform callback.
277 * Takes care of filtering and other sanity checks on
278 * the processed packet.
281 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
282 int skip, int protoff, struct m_tag *mt)
284 int prot, af, sproto;
287 struct tdb_ident *tdbi;
288 struct secasindex *saidx;
292 char ip6buf[INET6_ADDRSTRLEN];
296 IPSEC_SPLASSERT_SOFTNET(__func__);
298 IPSEC_ASSERT(m != NULL, ("null mbuf"));
299 IPSEC_ASSERT(sav != NULL, ("null SA"));
300 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
301 saidx = &sav->sah->saidx;
302 af = saidx->dst.sa.sa_family;
303 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
304 sproto = saidx->proto;
305 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
306 sproto == IPPROTO_IPCOMP,
307 ("unexpected security protocol %u", sproto));
311 DPRINTF(("%s: null mbuf", __func__));
312 IPSEC_ISTAT(sproto, espstat.esps_badkcr, ahstat.ahs_badkcr,
313 ipcompstat.ipcomps_badkcr);
319 /* Fix IPv4 header */
320 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
321 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
322 __func__, ipsec_address(&sav->sah->saidx.dst),
323 (u_long) ntohl(sav->spi)));
324 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
325 ipcompstat.ipcomps_hdrops);
330 ip = mtod(m, struct ip *);
331 ip->ip_len = htons(m->m_pkthdr.len);
332 ip->ip_off = htons(ip->ip_off);
334 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
336 ip = mtod(m, struct ip *);
341 /* IP-in-IP encapsulation */
342 if (prot == IPPROTO_IPIP) {
345 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
346 IPSEC_ISTAT(sproto, espstat.esps_hdrops,
348 ipcompstat.ipcomps_hdrops);
352 /* ipn will now contain the inner IPv4 header */
353 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip),
356 /* XXX PROXY address isn't recorded in SAH */
358 * Check that the inner source address is the same as
359 * the proxy address, if available.
361 if ((saidx->proxy.sa.sa_family == AF_INET &&
362 saidx->proxy.sin.sin_addr.s_addr !=
365 saidx->proxy.sin.sin_addr.s_addr) ||
366 (saidx->proxy.sa.sa_family != AF_INET &&
367 saidx->proxy.sa.sa_family != 0)) {
369 DPRINTF(("%s: inner source address %s doesn't "
370 "correspond to expected proxy source %s, "
371 "SA %s/%08lx\n", __func__,
372 inet_ntoa4(ipn.ip_src),
373 ipsp_address(saidx->proxy),
374 ipsp_address(saidx->dst),
375 (u_long) ntohl(sav->spi)));
377 IPSEC_ISTAT(sproto, espstat.esps_pdrops,
379 ipcompstat.ipcomps_pdrops);
385 /* IPv6-in-IP encapsulation. */
386 if (prot == IPPROTO_IPV6) {
389 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
390 IPSEC_ISTAT(sproto, espstat.esps_hdrops,
392 ipcompstat.ipcomps_hdrops);
396 /* ip6n will now contain the inner IPv6 header. */
397 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr),
401 * Check that the inner source address is the same as
402 * the proxy address, if available.
404 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
405 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
406 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
407 &saidx->proxy.sin6.sin6_addr)) ||
408 (saidx->proxy.sa.sa_family != AF_INET6 &&
409 saidx->proxy.sa.sa_family != 0)) {
411 DPRINTF(("%s: inner source address %s doesn't "
412 "correspond to expected proxy source %s, "
413 "SA %s/%08lx\n", __func__,
414 ip6_sprintf(ip6buf, &ip6n.ip6_src),
415 ipsec_address(&saidx->proxy),
416 ipsec_address(&saidx->dst),
417 (u_long) ntohl(sav->spi)));
419 IPSEC_ISTAT(sproto, espstat.esps_pdrops,
421 ipcompstat.ipcomps_pdrops);
430 * Record what we've done to the packet (under what SA it was
431 * processed). If we've been passed an mtag, it means the packet
432 * was already processed by an ethernet/crypto combo card and
433 * thus has a tag attached with all the right information, but
434 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
435 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
437 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
438 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
439 sizeof(struct tdb_ident), M_NOWAIT);
441 DPRINTF(("%s: failed to get tag\n", __func__));
442 IPSEC_ISTAT(sproto, espstat.esps_hdrops,
443 ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops);
448 tdbi = (struct tdb_ident *)(mtag + 1);
449 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
450 tdbi->proto = sproto;
451 tdbi->spi = sav->spi;
452 /* Cache those two for enc(4) in xform_ipip. */
453 tdbi->alg_auth = sav->alg_auth;
454 tdbi->alg_enc = sav->alg_enc;
456 m_tag_prepend(m, mtag);
457 } else if (mt != NULL) {
458 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
459 /* XXX do we need to mark m_flags??? */
462 key_sa_recordxfer(sav, m); /* record data transfer */
465 encif->if_ipackets++;
466 encif->if_ibytes += m->m_pkthdr.len;
469 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
470 * packet later after it has been decapsulated.
472 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
474 if (prot != IPPROTO_IPIP)
475 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
480 * Re-dispatch via software interrupt.
482 if ((error = netisr_queue(NETISR_IP, m))) {
483 IPSEC_ISTAT(sproto, espstat.esps_qfull, ahstat.ahs_qfull,
484 ipcompstat.ipcomps_qfull);
486 DPRINTF(("%s: queue full; proto %u packet dropped\n",
497 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
499 /* XXX nothing just yet */
504 /* IPv6 AH wrapper. */
506 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
512 if (*offp < sizeof(struct ip6_hdr)) {
513 DPRINTF(("%s: bad offset %u\n", __func__, *offp));
515 } else if (*offp == sizeof(struct ip6_hdr)) {
516 protoff = offsetof(struct ip6_hdr, ip6_nxt);
518 /* Chase down the header chain... */
519 protoff = sizeof(struct ip6_hdr);
523 m_copydata(*mp, protoff, sizeof(ip6e),
526 if (ip6e.ip6e_nxt == IPPROTO_AH)
527 l = (ip6e.ip6e_len + 2) << 2;
529 l = (ip6e.ip6e_len + 1) << 3;
530 IPSEC_ASSERT(l > 0, ("l went zero or negative"));
531 } while (protoff + l < *offp);
533 /* Malformed packet check */
534 if (protoff + l != *offp) {
535 DPRINTF(("%s: bad packet header chain, protoff %u, "
536 "l %u, off %u\n", __func__, protoff, l, *offp));
537 IPSEC_ISTAT(proto, espstat.esps_hdrops,
539 ipcompstat.ipcomps_hdrops);
544 protoff += offsetof(struct ip6_ext, ip6e_nxt);
546 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
551 * IPsec input callback, called by the transform callback. Takes care of
552 * filtering and other sanity checks on the processed packet.
555 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
558 int prot, af, sproto;
561 struct tdb_ident *tdbi;
562 struct secasindex *saidx;
567 char ip6buf[INET6_ADDRSTRLEN];
570 IPSEC_ASSERT(m != NULL, ("null mbuf"));
571 IPSEC_ASSERT(sav != NULL, ("null SA"));
572 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
573 saidx = &sav->sah->saidx;
574 af = saidx->dst.sa.sa_family;
575 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
576 sproto = saidx->proto;
577 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
578 sproto == IPPROTO_IPCOMP,
579 ("unexpected security protocol %u", sproto));
583 DPRINTF(("%s: null mbuf", __func__));
584 IPSEC_ISTAT(sproto, espstat.esps_badkcr, ahstat.ahs_badkcr,
585 ipcompstat.ipcomps_badkcr);
590 /* Fix IPv6 header */
591 if (m->m_len < sizeof(struct ip6_hdr) &&
592 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
594 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
595 __func__, ipsec_address(&sav->sah->saidx.dst),
596 (u_long) ntohl(sav->spi)));
598 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
599 ipcompstat.ipcomps_hdrops);
604 ip6 = mtod(m, struct ip6_hdr *);
605 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
608 m_copydata(m, protoff, 1, (unsigned char *) &prot);
612 /* IP-in-IP encapsulation */
613 if (prot == IPPROTO_IPIP) {
616 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
617 IPSEC_ISTAT(sproto, espstat.esps_hdrops,
619 ipcompstat.ipcomps_hdrops);
623 /* ipn will now contain the inner IPv4 header */
624 m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn);
627 * Check that the inner source address is the same as
628 * the proxy address, if available.
630 if ((saidx->proxy.sa.sa_family == AF_INET &&
631 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
632 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
633 (saidx->proxy.sa.sa_family != AF_INET &&
634 saidx->proxy.sa.sa_family != 0)) {
636 DPRINTF(("%s: inner source address %s doesn't "
637 "correspond to expected proxy source %s, "
638 "SA %s/%08lx\n", __func__,
639 inet_ntoa4(ipn.ip_src),
640 ipsec_address(&saidx->proxy),
641 ipsec_address(&saidx->dst),
642 (u_long) ntohl(sav->spi)));
644 IPSEC_ISTATsproto, (espstat.esps_pdrops,
645 ahstat.ahs_pdrops, ipcompstat.ipcomps_pdrops);
652 /* IPv6-in-IP encapsulation */
653 if (prot == IPPROTO_IPV6) {
656 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
657 IPSEC_ISTAT(sproto, espstat.esps_hdrops,
659 ipcompstat.ipcomps_hdrops);
663 /* ip6n will now contain the inner IPv6 header. */
664 m_copydata(m, skip, sizeof(struct ip6_hdr),
668 * Check that the inner source address is the same as
669 * the proxy address, if available.
671 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
672 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
673 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
674 &saidx->proxy.sin6.sin6_addr)) ||
675 (saidx->proxy.sa.sa_family != AF_INET6 &&
676 saidx->proxy.sa.sa_family != 0)) {
678 DPRINTF(("%s: inner source address %s doesn't "
679 "correspond to expected proxy source %s, "
680 "SA %s/%08lx\n", __func__,
681 ip6_sprintf(ip6buf, &ip6n.ip6_src),
682 ipsec_address(&saidx->proxy),
683 ipsec_address(&saidx->dst),
684 (u_long) ntohl(sav->spi)));
686 IPSEC_ISTAT(sproto, espstat.esps_pdrops,
687 ahstat.ahs_pdrops, ipcompstat.ipcomps_pdrops);
695 * Record what we've done to the packet (under what SA it was
696 * processed). If we've been passed an mtag, it means the packet
697 * was already processed by an ethernet/crypto combo card and
698 * thus has a tag attached with all the right information, but
699 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
700 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
702 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
703 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
704 sizeof(struct tdb_ident), M_NOWAIT);
706 DPRINTF(("%s: failed to get tag\n", __func__));
707 IPSEC_ISTAT(sproto, espstat.esps_hdrops,
708 ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops);
713 tdbi = (struct tdb_ident *)(mtag + 1);
714 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
715 tdbi->proto = sproto;
716 tdbi->spi = sav->spi;
717 /* Cache those two for enc(4) in xform_ipip. */
718 tdbi->alg_auth = sav->alg_auth;
719 tdbi->alg_enc = sav->alg_enc;
721 m_tag_prepend(m, mtag);
724 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
725 /* XXX do we need to mark m_flags??? */
728 key_sa_recordxfer(sav, m);
732 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
733 * packet later after it has been decapsulated.
735 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
737 /* XXX-BZ does not make sense. */
738 if (prot != IPPROTO_IPIP)
739 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
743 /* Retrieve new protocol */
744 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &nxt8);
747 * See the end of ip6_input for this logic.
748 * IPPROTO_IPV[46] case will be processed just like other ones
752 while (nxt != IPPROTO_DONE) {
753 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
754 ip6stat.ip6s_toomanyhdr++;
760 * Protection against faulty packet - there should be
761 * more sanity checks in header chain processing.
763 if (m->m_pkthdr.len < skip) {
764 ip6stat.ip6s_tooshort++;
765 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
770 * Enforce IPsec policy checking if we are seeing last header.
771 * note that we do not visit this with protocols with pcb layer
772 * code - like udp/tcp/raw ip.
774 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
775 ipsec6_in_reject(m, NULL)) {
779 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
789 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
791 struct ip6ctlparam *ip6cp = NULL;
792 struct mbuf *m = NULL;
796 if (sa->sa_family != AF_INET6 ||
797 sa->sa_len != sizeof(struct sockaddr_in6))
799 if ((unsigned)cmd >= PRC_NCMDS)
802 /* if the parameter is from icmp6, decode it. */
804 ip6cp = (struct ip6ctlparam *)d;
806 ip6 = ip6cp->ip6c_ip6;
807 off = ip6cp->ip6c_off;
811 off = 0; /* calm gcc */
816 struct ip6ctlparam ip6cp1;
819 * Notify the error to all possible sockets via pfctlinput2.
820 * Since the upper layer information (such as protocol type,
821 * source and destination ports) is embedded in the encrypted
822 * data and might have been cut, we can't directly call
823 * an upper layer ctlinput function. However, the pcbnotify
824 * function will consider source and destination addresses
825 * as well as the flow info value, and may be able to find
826 * some PCB that should be notified.
827 * Although pfctlinput2 will call esp6_ctlinput(), there is
828 * no possibility of an infinite loop of function calls,
829 * because we don't pass the inner IPv6 header.
831 bzero(&ip6cp1, sizeof(ip6cp1));
832 ip6cp1.ip6c_src = ip6cp->ip6c_src;
833 pfctlinput2(cmd, sa, (void *)&ip6cp1);
836 * Then go to special cases that need ESP header information.
837 * XXX: We assume that when ip6 is non NULL,
838 * M and OFF are valid.
841 if (cmd == PRC_MSGSIZE) {
842 struct secasvar *sav;
846 /* check header length before using m_copydata */
847 if (m->m_pkthdr.len < off + sizeof (struct esp))
849 m_copydata(m, off + offsetof(struct esp, esp_spi),
850 sizeof(u_int32_t), (caddr_t) &spi);
852 * Check to see if we have a valid SA corresponding to
853 * the address in the ICMP message payload.
855 sav = KEY_ALLOCSA((union sockaddr_union *)sa,
857 valid = (sav != NULL);
861 /* XXX Further validation? */
864 * Depending on whether the SA is "valid" and
865 * routing table size (mtudisc_{hi,lo}wat), we will:
866 * - recalcurate the new MTU and create the
867 * corresponding routing entry, or
868 * - ignore the MTU change notification.
870 icmp6_mtudisc_update(ip6cp, valid);
873 /* we normally notify any pcb here */