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/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(proto, name) do { \
102 if ((proto) == IPPROTO_ESP) \
103 ESPSTAT_INC(esps_##name); \
104 else if ((proto) == IPPROTO_AH) \
105 AHSTAT_INC(ahs_##name); \
107 IPCOMPSTAT_INC(ipcomps_##name); \
111 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
115 * ipsec_common_input gets called when an IPsec-protected packet
116 * is received by IPv4 or IPv6. Its job is to find the right SA
117 * and call the appropriate transform. The transform callback
118 * takes care of further processing (like ingress filtering).
121 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
123 union sockaddr_union dst_address;
124 struct secasvar *sav;
133 IPSEC_ISTAT(sproto, input);
135 IPSEC_ASSERT(m != NULL, ("null packet"));
137 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
138 sproto == IPPROTO_IPCOMP,
139 ("unexpected security protocol %u", sproto));
141 if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
142 (sproto == IPPROTO_AH && !V_ah_enable) ||
143 (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
145 IPSEC_ISTAT(sproto, pdrops);
149 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
151 IPSEC_ISTAT(sproto, hdrops);
152 DPRINTF(("%s: packet too small\n", __func__));
156 /* Retrieve the SPI from the relevant IPsec header */
157 if (sproto == IPPROTO_ESP)
158 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
159 else if (sproto == IPPROTO_AH)
160 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
162 else if (sproto == IPPROTO_IPCOMP) {
164 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
166 spi = ntohl(htons(cpi));
170 * Find the SA and (indirectly) call the appropriate
171 * kernel crypto routine. The resulting mbuf chain is a valid
172 * IP packet ready to go through input processing.
174 bzero(&dst_address, sizeof (dst_address));
175 dst_address.sa.sa_family = af;
179 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
180 m_copydata(m, offsetof(struct ip, ip_dst),
181 sizeof(struct in_addr),
182 (caddr_t) &dst_address.sin.sin_addr);
184 /* Find the source port for NAT-T; see udp*_espdecap. */
185 tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
187 dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1];
188 #endif /* IPSEC_NAT_T */
193 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
194 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
195 sizeof(struct in6_addr),
196 (caddr_t) &dst_address.sin6.sin6_addr);
200 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
202 IPSEC_ISTAT(sproto, nopf);
206 /* NB: only pass dst since key_allocsa follows RFC2401 */
207 sav = KEY_ALLOCSA(&dst_address, sproto, spi);
209 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
210 __func__, ipsec_address(&dst_address),
211 (u_long) ntohl(spi), sproto));
212 IPSEC_ISTAT(sproto, notdb);
217 if (sav->tdb_xform == NULL) {
218 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
219 __func__, ipsec_address(&dst_address),
220 (u_long) ntohl(spi), sproto));
221 IPSEC_ISTAT(sproto, noxform);
228 * Call appropriate transform and return -- callback takes care of
231 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
238 * Common input handler for IPv4 AH, ESP, and IPCOMP.
241 ipsec4_common_input(struct mbuf *m, ...)
247 off = va_arg(ap, int);
248 nxt = va_arg(ap, int);
251 return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
256 ah4_input(struct mbuf *m, int off)
258 ipsec4_common_input(m, off, IPPROTO_AH);
261 ah4_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_AH);
269 esp4_input(struct mbuf *m, int off)
271 ipsec4_common_input(m, off, IPPROTO_ESP);
274 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
276 if (sa->sa_family == AF_INET &&
277 sa->sa_len == sizeof(struct sockaddr_in))
278 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
282 ipcomp4_input(struct mbuf *m, int off)
284 ipsec4_common_input(m, off, IPPROTO_IPCOMP);
288 * IPsec input callback for INET protocols.
289 * This routine is called as the transform callback.
290 * Takes care of filtering and other sanity checks on
291 * the processed packet.
294 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
295 int skip, int protoff, struct m_tag *mt)
297 int prot, af, sproto, isr_prot;
300 struct tdb_ident *tdbi;
301 struct secasindex *saidx;
305 char ip6buf[INET6_ADDRSTRLEN];
309 IPSEC_ASSERT(m != NULL, ("null mbuf"));
310 IPSEC_ASSERT(sav != NULL, ("null SA"));
311 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
312 saidx = &sav->sah->saidx;
313 af = saidx->dst.sa.sa_family;
314 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
315 sproto = saidx->proto;
316 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
317 sproto == IPPROTO_IPCOMP,
318 ("unexpected security protocol %u", sproto));
322 DPRINTF(("%s: null mbuf", __func__));
323 IPSEC_ISTAT(sproto, badkcr);
331 * XXXGL: do we need this entire block?
333 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
334 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
335 __func__, ipsec_address(&sav->sah->saidx.dst),
336 (u_long) ntohl(sav->spi)));
337 IPSEC_ISTAT(sproto, hdrops);
342 ip = mtod(m, struct ip *);
343 ip->ip_len = htons(m->m_pkthdr.len);
345 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
347 ip = mtod(m, struct ip *);
352 encif->if_ipackets++;
353 encif->if_ibytes += m->m_pkthdr.len;
355 /* Pass the mbuf to enc0 for bpf and pfil. */
356 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
357 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
361 /* IP-in-IP encapsulation */
362 if (prot == IPPROTO_IPIP &&
363 saidx->mode != IPSEC_MODE_TRANSPORT) {
365 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
366 IPSEC_ISTAT(sproto, hdrops);
370 /* enc0: strip outer IPv4 header */
371 m_striphdr(m, 0, ip->ip_hl << 2);
374 /* XXX PROXY address isn't recorded in SAH */
376 * Check that the inner source address is the same as
377 * the proxy address, if available.
379 if ((saidx->proxy.sa.sa_family == AF_INET &&
380 saidx->proxy.sin.sin_addr.s_addr !=
383 saidx->proxy.sin.sin_addr.s_addr) ||
384 (saidx->proxy.sa.sa_family != AF_INET &&
385 saidx->proxy.sa.sa_family != 0)) {
387 DPRINTF(("%s: inner source address %s doesn't "
388 "correspond to expected proxy source %s, "
389 "SA %s/%08lx\n", __func__,
390 inet_ntoa4(ipn.ip_src),
391 ipsp_address(saidx->proxy),
392 ipsp_address(saidx->dst),
393 (u_long) ntohl(sav->spi)));
395 IPSEC_ISTAT(sproto, pdrops);
402 /* IPv6-in-IP encapsulation. */
403 else if (prot == IPPROTO_IPV6 &&
404 saidx->mode != IPSEC_MODE_TRANSPORT) {
406 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
407 IPSEC_ISTAT(sproto, hdrops);
411 /* enc0: strip IPv4 header, keep IPv6 header only */
412 m_striphdr(m, 0, ip->ip_hl << 2);
415 * Check that the inner source address is the same as
416 * the proxy address, if available.
418 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
419 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
420 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
421 &saidx->proxy.sin6.sin6_addr)) ||
422 (saidx->proxy.sa.sa_family != AF_INET6 &&
423 saidx->proxy.sa.sa_family != 0)) {
425 DPRINTF(("%s: inner source address %s doesn't "
426 "correspond to expected proxy source %s, "
427 "SA %s/%08lx\n", __func__,
428 ip6_sprintf(ip6buf, &ip6n.ip6_src),
429 ipsec_address(&saidx->proxy),
430 ipsec_address(&saidx->dst),
431 (u_long) ntohl(sav->spi)));
433 IPSEC_ISTAT(sproto, pdrops);
440 else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
442 * When mode is wildcard, inner protocol is IPv6 and
443 * we have no INET6 support - drop this packet a bit later.
444 * In other cases we assume transport mode and outer
445 * header was already stripped in xform_xxx_cb.
451 * Record what we've done to the packet (under what SA it was
452 * processed). If we've been passed an mtag, it means the packet
453 * was already processed by an ethernet/crypto combo card and
454 * thus has a tag attached with all the right information, but
455 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
456 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
458 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
459 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
460 sizeof(struct tdb_ident), M_NOWAIT);
462 DPRINTF(("%s: failed to get tag\n", __func__));
463 IPSEC_ISTAT(sproto, hdrops);
468 tdbi = (struct tdb_ident *)(mtag + 1);
469 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
470 tdbi->proto = sproto;
471 tdbi->spi = sav->spi;
472 /* Cache those two for enc(4) in xform_ipip. */
473 tdbi->alg_auth = sav->alg_auth;
474 tdbi->alg_enc = sav->alg_enc;
476 m_tag_prepend(m, mtag);
477 } else if (mt != NULL) {
478 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
479 /* XXX do we need to mark m_flags??? */
482 key_sa_recordxfer(sav, m); /* record data transfer */
485 * In transport mode requeue decrypted mbuf back to IPv4 protocol
486 * handler. This is necessary to correctly expose rcvif.
488 if (saidx->mode == IPSEC_MODE_TRANSPORT)
492 * Pass the mbuf to enc0 for bpf and pfil.
494 if (prot == IPPROTO_IPIP)
495 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
497 if (prot == IPPROTO_IPV6)
498 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
501 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
506 * Re-dispatch via software interrupt.
511 isr_prot = NETISR_IP;
515 isr_prot = NETISR_IPV6;
519 DPRINTF(("%s: cannot handle inner ip proto %d\n",
521 IPSEC_ISTAT(sproto, nopf);
522 error = EPFNOSUPPORT;
526 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
528 IPSEC_ISTAT(sproto, qfull);
529 DPRINTF(("%s: queue full; proto %u packet dropped\n",
540 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
542 /* XXX nothing just yet */
547 /* IPv6 AH wrapper. */
549 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
555 if (*offp < sizeof(struct ip6_hdr)) {
556 DPRINTF(("%s: bad offset %u\n", __func__, *offp));
558 } else if (*offp == sizeof(struct ip6_hdr)) {
559 protoff = offsetof(struct ip6_hdr, ip6_nxt);
561 /* Chase down the header chain... */
562 protoff = sizeof(struct ip6_hdr);
566 m_copydata(*mp, protoff, sizeof(ip6e),
569 if (ip6e.ip6e_nxt == IPPROTO_AH)
570 l = (ip6e.ip6e_len + 2) << 2;
572 l = (ip6e.ip6e_len + 1) << 3;
573 IPSEC_ASSERT(l > 0, ("l went zero or negative"));
574 } while (protoff + l < *offp);
576 /* Malformed packet check */
577 if (protoff + l != *offp) {
578 DPRINTF(("%s: bad packet header chain, protoff %u, "
579 "l %u, off %u\n", __func__, protoff, l, *offp));
580 IPSEC_ISTAT(proto, hdrops);
585 protoff += offsetof(struct ip6_ext, ip6e_nxt);
587 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
592 * IPsec input callback, called by the transform callback. Takes care of
593 * filtering and other sanity checks on the processed packet.
596 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
599 int prot, af, sproto;
602 struct tdb_ident *tdbi;
603 struct secasindex *saidx;
608 char ip6buf[INET6_ADDRSTRLEN];
611 IPSEC_ASSERT(m != NULL, ("null mbuf"));
612 IPSEC_ASSERT(sav != NULL, ("null SA"));
613 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
614 saidx = &sav->sah->saidx;
615 af = saidx->dst.sa.sa_family;
616 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
617 sproto = saidx->proto;
618 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
619 sproto == IPPROTO_IPCOMP,
620 ("unexpected security protocol %u", sproto));
624 DPRINTF(("%s: null mbuf", __func__));
625 IPSEC_ISTAT(sproto, badkcr);
630 /* Fix IPv6 header */
631 if (m->m_len < sizeof(struct ip6_hdr) &&
632 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
634 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
635 __func__, ipsec_address(&sav->sah->saidx.dst),
636 (u_long) ntohl(sav->spi)));
638 IPSEC_ISTAT(sproto, hdrops);
643 ip6 = mtod(m, struct ip6_hdr *);
644 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
647 m_copydata(m, protoff, 1, &nxt8);
651 encif->if_ipackets++;
652 encif->if_ibytes += m->m_pkthdr.len;
654 /* Pass the mbuf to enc0 for bpf and pfil. */
655 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
656 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
660 /* IPv6-in-IP encapsulation */
661 if (prot == IPPROTO_IPV6 &&
662 saidx->mode != IPSEC_MODE_TRANSPORT) {
663 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
664 IPSEC_ISTAT(sproto, hdrops);
668 /* ip6n will now contain the inner IPv6 header. */
669 m_striphdr(m, 0, skip);
673 * Check that the inner source address is the same as
674 * the proxy address, if available.
676 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
677 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
678 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
679 &saidx->proxy.sin6.sin6_addr)) ||
680 (saidx->proxy.sa.sa_family != AF_INET6 &&
681 saidx->proxy.sa.sa_family != 0)) {
683 DPRINTF(("%s: inner source address %s doesn't "
684 "correspond to expected proxy source %s, "
685 "SA %s/%08lx\n", __func__,
686 ip6_sprintf(ip6buf, &ip6n.ip6_src),
687 ipsec_address(&saidx->proxy),
688 ipsec_address(&saidx->dst),
689 (u_long) ntohl(sav->spi)));
691 IPSEC_ISTAT(sproto, pdrops);
698 /* IP-in-IP encapsulation */
699 else if (prot == IPPROTO_IPIP &&
700 saidx->mode != IPSEC_MODE_TRANSPORT) {
701 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
702 IPSEC_ISTAT(sproto, hdrops);
706 /* ipn will now contain the inner IPv4 header */
707 m_striphdr(m, 0, skip);
711 * Check that the inner source address is the same as
712 * the proxy address, if available.
714 if ((saidx->proxy.sa.sa_family == AF_INET &&
715 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
716 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
717 (saidx->proxy.sa.sa_family != AF_INET &&
718 saidx->proxy.sa.sa_family != 0)) {
720 DPRINTF(("%s: inner source address %s doesn't "
721 "correspond to expected proxy source %s, "
722 "SA %s/%08lx\n", __func__,
723 inet_ntoa4(ipn.ip_src),
724 ipsec_address(&saidx->proxy),
725 ipsec_address(&saidx->dst),
726 (u_long) ntohl(sav->spi)));
728 IPSEC_ISTAT(sproto, pdrops);
736 prot = IPPROTO_IPV6; /* for correct BPF processing */
740 * Record what we've done to the packet (under what SA it was
741 * processed). If we've been passed an mtag, it means the packet
742 * was already processed by an ethernet/crypto combo card and
743 * thus has a tag attached with all the right information, but
744 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
745 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
747 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
748 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
749 sizeof(struct tdb_ident), M_NOWAIT);
751 DPRINTF(("%s: failed to get tag\n", __func__));
752 IPSEC_ISTAT(sproto, hdrops);
757 tdbi = (struct tdb_ident *)(mtag + 1);
758 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
759 tdbi->proto = sproto;
760 tdbi->spi = sav->spi;
761 /* Cache those two for enc(4) in xform_ipip. */
762 tdbi->alg_auth = sav->alg_auth;
763 tdbi->alg_enc = sav->alg_enc;
765 m_tag_prepend(m, mtag);
768 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
769 /* XXX do we need to mark m_flags??? */
772 key_sa_recordxfer(sav, m);
776 * Pass the mbuf to enc0 for bpf and pfil.
779 if (prot == IPPROTO_IPIP)
780 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
782 if (prot == IPPROTO_IPV6)
783 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
785 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
789 * See the end of ip6_input for this logic.
790 * IPPROTO_IPV[46] case will be processed just like other ones
794 while (nxt != IPPROTO_DONE) {
795 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
796 IP6STAT_INC(ip6s_toomanyhdr);
802 * Protection against faulty packet - there should be
803 * more sanity checks in header chain processing.
805 if (m->m_pkthdr.len < skip) {
806 IP6STAT_INC(ip6s_tooshort);
807 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
812 * Enforce IPsec policy checking if we are seeing last header.
813 * note that we do not visit this with protocols with pcb layer
814 * code - like udp/tcp/raw ip.
816 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
817 ipsec6_in_reject(m, NULL)) {
821 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
831 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
833 struct ip6ctlparam *ip6cp = NULL;
834 struct mbuf *m = NULL;
838 if (sa->sa_family != AF_INET6 ||
839 sa->sa_len != sizeof(struct sockaddr_in6))
841 if ((unsigned)cmd >= PRC_NCMDS)
844 /* if the parameter is from icmp6, decode it. */
846 ip6cp = (struct ip6ctlparam *)d;
848 ip6 = ip6cp->ip6c_ip6;
849 off = ip6cp->ip6c_off;
853 off = 0; /* calm gcc */
858 struct ip6ctlparam ip6cp1;
861 * Notify the error to all possible sockets via pfctlinput2.
862 * Since the upper layer information (such as protocol type,
863 * source and destination ports) is embedded in the encrypted
864 * data and might have been cut, we can't directly call
865 * an upper layer ctlinput function. However, the pcbnotify
866 * function will consider source and destination addresses
867 * as well as the flow info value, and may be able to find
868 * some PCB that should be notified.
869 * Although pfctlinput2 will call esp6_ctlinput(), there is
870 * no possibility of an infinite loop of function calls,
871 * because we don't pass the inner IPv6 header.
873 bzero(&ip6cp1, sizeof(ip6cp1));
874 ip6cp1.ip6c_src = ip6cp->ip6c_src;
875 pfctlinput2(cmd, sa, (void *)&ip6cp1);
878 * Then go to special cases that need ESP header information.
879 * XXX: We assume that when ip6 is non NULL,
880 * M and OFF are valid.
883 if (cmd == PRC_MSGSIZE) {
884 struct secasvar *sav;
888 /* check header length before using m_copydata */
889 if (m->m_pkthdr.len < off + sizeof (struct esp))
891 m_copydata(m, off + offsetof(struct esp, esp_spi),
892 sizeof(u_int32_t), (caddr_t) &spi);
894 * Check to see if we have a valid SA corresponding to
895 * the address in the ICMP message payload.
897 sav = KEY_ALLOCSA((union sockaddr_union *)sa,
899 valid = (sav != NULL);
903 /* XXX Further validation? */
906 * Depending on whether the SA is "valid" and
907 * routing table size (mtudisc_{hi,lo}wat), we will:
908 * - recalcurate the new MTU and create the
909 * corresponding routing entry, or
910 * - ignore the MTU change notification.
912 icmp6_mtudisc_update(ip6cp, valid);
915 /* we normally notify any pcb here */