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>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/in_var.h>
70 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
74 #include <netinet/in_pcb.h>
76 #include <netinet/icmp6.h>
79 #include <netipsec/ipsec.h>
81 #include <netipsec/ipsec6.h>
83 #include <netipsec/ah_var.h>
84 #include <netipsec/esp.h>
85 #include <netipsec/esp_var.h>
86 #include <netipsec/ipcomp_var.h>
88 #include <netipsec/key.h>
89 #include <netipsec/keydb.h>
91 #include <netipsec/xform.h>
92 #include <netinet6/ip6protosw.h>
94 #include <machine/in_cksum.h>
95 #include <machine/stdarg.h>
98 #include <net/if_enc.h>
102 #define IPSEC_ISTAT(p,x,y,z) ((p) == IPPROTO_ESP ? (x)++ : \
103 (p) == IPPROTO_AH ? (y)++ : (z)++)
106 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
110 * ipsec_common_input gets called when an IPsec-protected packet
111 * is received by IPv4 or IPv6. It's job is to find the right SA
112 * and call the appropriate transform. The transform callback
113 * takes care of further processing (like ingress filtering).
116 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
118 union sockaddr_union dst_address;
119 struct secasvar *sav;
126 IPSEC_ISTAT(sproto, V_espstat.esps_input, V_ahstat.ahs_input,
127 V_ipcompstat.ipcomps_input);
129 IPSEC_ASSERT(m != NULL, ("null packet"));
131 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
132 sproto == IPPROTO_IPCOMP,
133 ("unexpected security protocol %u", sproto));
135 if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
136 (sproto == IPPROTO_AH && !V_ah_enable) ||
137 (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
139 IPSEC_ISTAT(sproto, V_espstat.esps_pdrops, V_ahstat.ahs_pdrops,
140 V_ipcompstat.ipcomps_pdrops);
144 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
146 IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops,
147 V_ipcompstat.ipcomps_hdrops);
148 DPRINTF(("%s: packet too small\n", __func__));
152 /* Retrieve the SPI from the relevant IPsec header */
153 if (sproto == IPPROTO_ESP)
154 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
155 else if (sproto == IPPROTO_AH)
156 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
158 else if (sproto == IPPROTO_IPCOMP) {
160 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
162 spi = ntohl(htons(cpi));
166 * Find the SA and (indirectly) call the appropriate
167 * kernel crypto routine. The resulting mbuf chain is a valid
168 * IP packet ready to go through input processing.
170 bzero(&dst_address, sizeof (dst_address));
171 dst_address.sa.sa_family = af;
175 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
176 m_copydata(m, offsetof(struct ip, ip_dst),
177 sizeof(struct in_addr),
178 (caddr_t) &dst_address.sin.sin_addr);
180 /* Find the source port for NAT-T; see udp*_espdecap. */
181 tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
183 dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1];
184 #endif /* IPSEC_NAT_T */
189 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
190 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
191 sizeof(struct in6_addr),
192 (caddr_t) &dst_address.sin6.sin6_addr);
196 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
198 IPSEC_ISTAT(sproto, V_espstat.esps_nopf, V_ahstat.ahs_nopf,
199 V_ipcompstat.ipcomps_nopf);
203 /* NB: only pass dst since key_allocsa follows RFC2401 */
204 sav = KEY_ALLOCSA(&dst_address, sproto, spi);
206 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
207 __func__, ipsec_address(&dst_address),
208 (u_long) ntohl(spi), sproto));
209 IPSEC_ISTAT(sproto, V_espstat.esps_notdb, V_ahstat.ahs_notdb,
210 V_ipcompstat.ipcomps_notdb);
215 if (sav->tdb_xform == NULL) {
216 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
217 __func__, ipsec_address(&dst_address),
218 (u_long) ntohl(spi), sproto));
219 IPSEC_ISTAT(sproto, V_espstat.esps_noxform, V_ahstat.ahs_noxform,
220 V_ipcompstat.ipcomps_noxform);
227 * Call appropriate transform and return -- callback takes care of
230 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
237 * Common input handler for IPv4 AH, ESP, and IPCOMP.
240 ipsec4_common_input(struct mbuf *m, ...)
246 off = va_arg(ap, int);
247 nxt = va_arg(ap, int);
250 return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
255 ah4_input(struct mbuf *m, int off)
257 ipsec4_common_input(m, off, IPPROTO_AH);
260 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
262 if (sa->sa_family == AF_INET &&
263 sa->sa_len == sizeof(struct sockaddr_in))
264 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
268 esp4_input(struct mbuf *m, int off)
270 ipsec4_common_input(m, off, IPPROTO_ESP);
273 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
275 if (sa->sa_family == AF_INET &&
276 sa->sa_len == sizeof(struct sockaddr_in))
277 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
281 ipcomp4_input(struct mbuf *m, int off)
283 ipsec4_common_input(m, off, IPPROTO_IPCOMP);
287 * IPsec input callback for INET protocols.
288 * This routine is called as the transform callback.
289 * Takes care of filtering and other sanity checks on
290 * the processed packet.
293 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
294 int skip, int protoff, struct m_tag *mt)
296 int prot, af, sproto;
299 struct tdb_ident *tdbi;
300 struct secasindex *saidx;
304 char ip6buf[INET6_ADDRSTRLEN];
308 IPSEC_ASSERT(m != NULL, ("null mbuf"));
309 IPSEC_ASSERT(sav != NULL, ("null SA"));
310 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
311 saidx = &sav->sah->saidx;
312 af = saidx->dst.sa.sa_family;
313 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
314 sproto = saidx->proto;
315 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
316 sproto == IPPROTO_IPCOMP,
317 ("unexpected security protocol %u", sproto));
321 DPRINTF(("%s: null mbuf", __func__));
322 IPSEC_ISTAT(sproto, V_espstat.esps_badkcr, V_ahstat.ahs_badkcr,
323 V_ipcompstat.ipcomps_badkcr);
329 /* Fix IPv4 header */
330 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
331 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
332 __func__, ipsec_address(&sav->sah->saidx.dst),
333 (u_long) ntohl(sav->spi)));
334 IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops,
335 V_ipcompstat.ipcomps_hdrops);
340 ip = mtod(m, struct ip *);
341 ip->ip_len = htons(m->m_pkthdr.len);
342 ip->ip_off = htons(ip->ip_off);
344 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
346 ip = mtod(m, struct ip *);
351 /* IP-in-IP encapsulation */
352 if (prot == IPPROTO_IPIP) {
355 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
356 IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
358 V_ipcompstat.ipcomps_hdrops);
362 /* ipn will now contain the inner IPv4 header */
363 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip),
366 /* XXX PROXY address isn't recorded in SAH */
368 * Check that the inner source address is the same as
369 * the proxy address, if available.
371 if ((saidx->proxy.sa.sa_family == AF_INET &&
372 saidx->proxy.sin.sin_addr.s_addr !=
375 saidx->proxy.sin.sin_addr.s_addr) ||
376 (saidx->proxy.sa.sa_family != AF_INET &&
377 saidx->proxy.sa.sa_family != 0)) {
379 DPRINTF(("%s: inner source address %s doesn't "
380 "correspond to expected proxy source %s, "
381 "SA %s/%08lx\n", __func__,
382 inet_ntoa4(ipn.ip_src),
383 ipsp_address(saidx->proxy),
384 ipsp_address(saidx->dst),
385 (u_long) ntohl(sav->spi)));
387 IPSEC_ISTAT(sproto, V_espstat.esps_pdrops,
389 V_ipcompstat.ipcomps_pdrops);
395 /* IPv6-in-IP encapsulation. */
396 if (prot == IPPROTO_IPV6) {
399 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
400 IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
402 V_ipcompstat.ipcomps_hdrops);
406 /* ip6n will now contain the inner IPv6 header. */
407 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr),
411 * Check that the inner source address is the same as
412 * the proxy address, if available.
414 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
415 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
416 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
417 &saidx->proxy.sin6.sin6_addr)) ||
418 (saidx->proxy.sa.sa_family != AF_INET6 &&
419 saidx->proxy.sa.sa_family != 0)) {
421 DPRINTF(("%s: inner source address %s doesn't "
422 "correspond to expected proxy source %s, "
423 "SA %s/%08lx\n", __func__,
424 ip6_sprintf(ip6buf, &ip6n.ip6_src),
425 ipsec_address(&saidx->proxy),
426 ipsec_address(&saidx->dst),
427 (u_long) ntohl(sav->spi)));
429 IPSEC_ISTAT(sproto, V_espstat.esps_pdrops,
431 V_ipcompstat.ipcomps_pdrops);
440 * Record what we've done to the packet (under what SA it was
441 * processed). If we've been passed an mtag, it means the packet
442 * was already processed by an ethernet/crypto combo card and
443 * thus has a tag attached with all the right information, but
444 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
445 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
447 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
448 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
449 sizeof(struct tdb_ident), M_NOWAIT);
451 DPRINTF(("%s: failed to get tag\n", __func__));
452 IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
453 V_ahstat.ahs_hdrops, V_ipcompstat.ipcomps_hdrops);
458 tdbi = (struct tdb_ident *)(mtag + 1);
459 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
460 tdbi->proto = sproto;
461 tdbi->spi = sav->spi;
462 /* Cache those two for enc(4) in xform_ipip. */
463 tdbi->alg_auth = sav->alg_auth;
464 tdbi->alg_enc = sav->alg_enc;
466 m_tag_prepend(m, mtag);
467 } else if (mt != NULL) {
468 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
469 /* XXX do we need to mark m_flags??? */
472 key_sa_recordxfer(sav, m); /* record data transfer */
475 encif->if_ipackets++;
476 encif->if_ibytes += m->m_pkthdr.len;
479 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
480 * packet later after it has been decapsulated.
482 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
484 if (prot != IPPROTO_IPIP)
485 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
490 * Re-dispatch via software interrupt.
492 if ((error = netisr_queue_src(NETISR_IP, (uintptr_t)sav->spi, m))) {
493 IPSEC_ISTAT(sproto, V_espstat.esps_qfull, V_ahstat.ahs_qfull,
494 V_ipcompstat.ipcomps_qfull);
496 DPRINTF(("%s: queue full; proto %u packet dropped\n",
507 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
509 /* XXX nothing just yet */
514 /* IPv6 AH wrapper. */
516 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
522 if (*offp < sizeof(struct ip6_hdr)) {
523 DPRINTF(("%s: bad offset %u\n", __func__, *offp));
525 } else if (*offp == sizeof(struct ip6_hdr)) {
526 protoff = offsetof(struct ip6_hdr, ip6_nxt);
528 /* Chase down the header chain... */
529 protoff = sizeof(struct ip6_hdr);
533 m_copydata(*mp, protoff, sizeof(ip6e),
536 if (ip6e.ip6e_nxt == IPPROTO_AH)
537 l = (ip6e.ip6e_len + 2) << 2;
539 l = (ip6e.ip6e_len + 1) << 3;
540 IPSEC_ASSERT(l > 0, ("l went zero or negative"));
541 } while (protoff + l < *offp);
543 /* Malformed packet check */
544 if (protoff + l != *offp) {
545 DPRINTF(("%s: bad packet header chain, protoff %u, "
546 "l %u, off %u\n", __func__, protoff, l, *offp));
547 IPSEC_ISTAT(proto, V_espstat.esps_hdrops,
549 V_ipcompstat.ipcomps_hdrops);
554 protoff += offsetof(struct ip6_ext, ip6e_nxt);
556 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
561 * IPsec input callback, called by the transform callback. Takes care of
562 * filtering and other sanity checks on the processed packet.
565 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
568 int prot, af, sproto;
571 struct tdb_ident *tdbi;
572 struct secasindex *saidx;
577 char ip6buf[INET6_ADDRSTRLEN];
580 IPSEC_ASSERT(m != NULL, ("null mbuf"));
581 IPSEC_ASSERT(sav != NULL, ("null SA"));
582 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
583 saidx = &sav->sah->saidx;
584 af = saidx->dst.sa.sa_family;
585 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
586 sproto = saidx->proto;
587 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
588 sproto == IPPROTO_IPCOMP,
589 ("unexpected security protocol %u", sproto));
593 DPRINTF(("%s: null mbuf", __func__));
594 IPSEC_ISTAT(sproto, V_espstat.esps_badkcr, V_ahstat.ahs_badkcr,
595 V_ipcompstat.ipcomps_badkcr);
600 /* Fix IPv6 header */
601 if (m->m_len < sizeof(struct ip6_hdr) &&
602 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
604 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
605 __func__, ipsec_address(&sav->sah->saidx.dst),
606 (u_long) ntohl(sav->spi)));
608 IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops,
609 V_ipcompstat.ipcomps_hdrops);
614 ip6 = mtod(m, struct ip6_hdr *);
615 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
619 m_copydata(m, protoff, 1, (unsigned char *) &prot);
623 /* IP-in-IP encapsulation */
624 if (prot == IPPROTO_IPIP) {
627 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
628 IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
630 V_ipcompstat.ipcomps_hdrops);
634 /* ipn will now contain the inner IPv4 header */
635 m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn);
638 * Check that the inner source address is the same as
639 * the proxy address, if available.
641 if ((saidx->proxy.sa.sa_family == AF_INET &&
642 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
643 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
644 (saidx->proxy.sa.sa_family != AF_INET &&
645 saidx->proxy.sa.sa_family != 0)) {
647 DPRINTF(("%s: inner source address %s doesn't "
648 "correspond to expected proxy source %s, "
649 "SA %s/%08lx\n", __func__,
650 inet_ntoa4(ipn.ip_src),
651 ipsec_address(&saidx->proxy),
652 ipsec_address(&saidx->dst),
653 (u_long) ntohl(sav->spi)));
655 IPSEC_ISTATsproto, (V_espstat.esps_pdrops,
656 V_ahstat.ahs_pdrops, V_ipcompstat.ipcomps_pdrops);
663 /* IPv6-in-IP encapsulation */
664 if (prot == IPPROTO_IPV6) {
667 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
668 IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
670 V_ipcompstat.ipcomps_hdrops);
674 /* ip6n will now contain the inner IPv6 header. */
675 m_copydata(m, skip, sizeof(struct ip6_hdr),
679 * Check that the inner source address is the same as
680 * the proxy address, if available.
682 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
683 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
684 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
685 &saidx->proxy.sin6.sin6_addr)) ||
686 (saidx->proxy.sa.sa_family != AF_INET6 &&
687 saidx->proxy.sa.sa_family != 0)) {
689 DPRINTF(("%s: inner source address %s doesn't "
690 "correspond to expected proxy source %s, "
691 "SA %s/%08lx\n", __func__,
692 ip6_sprintf(ip6buf, &ip6n.ip6_src),
693 ipsec_address(&saidx->proxy),
694 ipsec_address(&saidx->dst),
695 (u_long) ntohl(sav->spi)));
697 IPSEC_ISTAT(sproto, V_espstat.esps_pdrops,
698 V_ahstat.ahs_pdrops, V_ipcompstat.ipcomps_pdrops);
706 * Record what we've done to the packet (under what SA it was
707 * processed). If we've been passed an mtag, it means the packet
708 * was already processed by an ethernet/crypto combo card and
709 * thus has a tag attached with all the right information, but
710 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
711 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
713 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
714 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
715 sizeof(struct tdb_ident), M_NOWAIT);
717 DPRINTF(("%s: failed to get tag\n", __func__));
718 IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
719 V_ahstat.ahs_hdrops, V_ipcompstat.ipcomps_hdrops);
724 tdbi = (struct tdb_ident *)(mtag + 1);
725 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
726 tdbi->proto = sproto;
727 tdbi->spi = sav->spi;
728 /* Cache those two for enc(4) in xform_ipip. */
729 tdbi->alg_auth = sav->alg_auth;
730 tdbi->alg_enc = sav->alg_enc;
732 m_tag_prepend(m, mtag);
735 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
736 /* XXX do we need to mark m_flags??? */
739 key_sa_recordxfer(sav, m);
742 encif->if_ipackets++;
743 encif->if_ibytes += m->m_pkthdr.len;
746 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
747 * packet later after it has been decapsulated.
749 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
751 /* XXX-BZ does not make sense. */
752 if (prot != IPPROTO_IPIP)
753 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
757 /* Retrieve new protocol */
758 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &nxt8);
761 * See the end of ip6_input for this logic.
762 * IPPROTO_IPV[46] case will be processed just like other ones
766 while (nxt != IPPROTO_DONE) {
767 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
768 V_ip6stat.ip6s_toomanyhdr++;
774 * Protection against faulty packet - there should be
775 * more sanity checks in header chain processing.
777 if (m->m_pkthdr.len < skip) {
778 V_ip6stat.ip6s_tooshort++;
779 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
784 * Enforce IPsec policy checking if we are seeing last header.
785 * note that we do not visit this with protocols with pcb layer
786 * code - like udp/tcp/raw ip.
788 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
789 ipsec6_in_reject(m, NULL)) {
793 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
803 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
805 struct ip6ctlparam *ip6cp = NULL;
806 struct mbuf *m = NULL;
810 if (sa->sa_family != AF_INET6 ||
811 sa->sa_len != sizeof(struct sockaddr_in6))
813 if ((unsigned)cmd >= PRC_NCMDS)
816 /* if the parameter is from icmp6, decode it. */
818 ip6cp = (struct ip6ctlparam *)d;
820 ip6 = ip6cp->ip6c_ip6;
821 off = ip6cp->ip6c_off;
825 off = 0; /* calm gcc */
830 struct ip6ctlparam ip6cp1;
833 * Notify the error to all possible sockets via pfctlinput2.
834 * Since the upper layer information (such as protocol type,
835 * source and destination ports) is embedded in the encrypted
836 * data and might have been cut, we can't directly call
837 * an upper layer ctlinput function. However, the pcbnotify
838 * function will consider source and destination addresses
839 * as well as the flow info value, and may be able to find
840 * some PCB that should be notified.
841 * Although pfctlinput2 will call esp6_ctlinput(), there is
842 * no possibility of an infinite loop of function calls,
843 * because we don't pass the inner IPv6 header.
845 bzero(&ip6cp1, sizeof(ip6cp1));
846 ip6cp1.ip6c_src = ip6cp->ip6c_src;
847 pfctlinput2(cmd, sa, (void *)&ip6cp1);
850 * Then go to special cases that need ESP header information.
851 * XXX: We assume that when ip6 is non NULL,
852 * M and OFF are valid.
855 if (cmd == PRC_MSGSIZE) {
856 struct secasvar *sav;
860 /* check header length before using m_copydata */
861 if (m->m_pkthdr.len < off + sizeof (struct esp))
863 m_copydata(m, off + offsetof(struct esp, esp_spi),
864 sizeof(u_int32_t), (caddr_t) &spi);
866 * Check to see if we have a valid SA corresponding to
867 * the address in the ICMP message payload.
869 sav = KEY_ALLOCSA((union sockaddr_union *)sa,
871 valid = (sav != NULL);
875 /* XXX Further validation? */
878 * Depending on whether the SA is "valid" and
879 * routing table size (mtudisc_{hi,lo}wat), we will:
880 * - recalcurate the new MTU and create the
881 * corresponding routing entry, or
882 * - ignore the MTU change notification.
884 icmp6_mtudisc_update(ip6cp, valid);
887 /* we normally notify any pcb here */