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/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(proto, name) do { \
103 if ((proto) == IPPROTO_ESP) \
104 ESPSTAT_INC(esps_##name); \
105 else if ((proto) == IPPROTO_AH) \
106 AHSTAT_INC(ahs_##name); \
108 IPCOMPSTAT_INC(ipcomps_##name); \
112 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
116 * ipsec_common_input gets called when an IPsec-protected packet
117 * is received by IPv4 or IPv6. Its job is to find the right SA
118 * and call the appropriate transform. The transform callback
119 * takes care of further processing (like ingress filtering).
122 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
124 char buf[INET6_ADDRSTRLEN];
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);
199 /* We keep addresses in SADB without embedded scope id */
200 if (IN6_IS_SCOPE_LINKLOCAL(&dst_address.sin6.sin6_addr)) {
201 /* XXX: sa6_recoverscope() */
202 dst_address.sin6.sin6_scope_id =
203 ntohs(dst_address.sin6.sin6_addr.s6_addr16[1]);
204 dst_address.sin6.sin6_addr.s6_addr16[1] = 0;
209 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
211 IPSEC_ISTAT(sproto, nopf);
215 /* NB: only pass dst since key_allocsa follows RFC2401 */
216 sav = KEY_ALLOCSA(&dst_address, sproto, spi);
218 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
219 __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
220 (u_long) ntohl(spi), sproto));
221 IPSEC_ISTAT(sproto, notdb);
226 if (sav->tdb_xform == NULL) {
227 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
228 __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
229 (u_long) ntohl(spi), sproto));
230 IPSEC_ISTAT(sproto, noxform);
237 * Call appropriate transform and return -- callback takes care of
240 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
247 * Common input handler for IPv4 AH, ESP, and IPCOMP.
250 ipsec4_common_input(struct mbuf *m, ...)
256 off = va_arg(ap, int);
257 nxt = va_arg(ap, int);
260 return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
265 ah4_input(struct mbuf **mp, int *offp, int proto)
274 ipsec4_common_input(m, off, IPPROTO_AH);
275 return (IPPROTO_DONE);
278 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
280 if (sa->sa_family == AF_INET &&
281 sa->sa_len == sizeof(struct sockaddr_in))
282 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
286 esp4_input(struct mbuf **mp, int *offp, int proto)
295 ipsec4_common_input(m, off, IPPROTO_ESP);
296 return (IPPROTO_DONE);
300 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
302 if (sa->sa_family == AF_INET &&
303 sa->sa_len == sizeof(struct sockaddr_in))
304 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
308 ipcomp4_input(struct mbuf **mp, int *offp, int proto)
317 ipsec4_common_input(m, off, IPPROTO_IPCOMP);
318 return (IPPROTO_DONE);
322 * IPsec input callback for INET protocols.
323 * This routine is called as the transform callback.
324 * Takes care of filtering and other sanity checks on
325 * the processed packet.
328 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
331 char buf[INET6_ADDRSTRLEN];
332 int prot, af, sproto, isr_prot;
335 struct tdb_ident *tdbi;
336 struct secasindex *saidx;
340 char ip6buf[INET6_ADDRSTRLEN];
344 IPSEC_ASSERT(m != NULL, ("null mbuf"));
345 IPSEC_ASSERT(sav != NULL, ("null SA"));
346 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
347 saidx = &sav->sah->saidx;
348 af = saidx->dst.sa.sa_family;
349 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
350 sproto = saidx->proto;
351 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
352 sproto == IPPROTO_IPCOMP,
353 ("unexpected security protocol %u", sproto));
357 DPRINTF(("%s: null mbuf", __func__));
358 IPSEC_ISTAT(sproto, badkcr);
366 * XXXGL: do we need this entire block?
368 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
369 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
370 __func__, ipsec_address(&sav->sah->saidx.dst,
371 buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
372 IPSEC_ISTAT(sproto, hdrops);
377 ip = mtod(m, struct ip *);
378 ip->ip_len = htons(m->m_pkthdr.len);
380 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
382 ip = mtod(m, struct ip *);
387 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
388 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
390 /* Pass the mbuf to enc0 for bpf and pfil. */
391 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
392 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
396 /* IP-in-IP encapsulation */
397 if (prot == IPPROTO_IPIP &&
398 saidx->mode != IPSEC_MODE_TRANSPORT) {
400 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
401 IPSEC_ISTAT(sproto, hdrops);
405 /* enc0: strip outer IPv4 header */
406 m_striphdr(m, 0, ip->ip_hl << 2);
409 /* XXX PROXY address isn't recorded in SAH */
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_INET &&
415 saidx->proxy.sin.sin_addr.s_addr !=
418 saidx->proxy.sin.sin_addr.s_addr) ||
419 (saidx->proxy.sa.sa_family != AF_INET &&
420 saidx->proxy.sa.sa_family != 0)) {
422 DPRINTF(("%s: inner source address %s doesn't "
423 "correspond to expected proxy source %s, "
424 "SA %s/%08lx\n", __func__,
425 inet_ntoa4(ipn.ip_src),
426 ipsp_address(saidx->proxy),
427 ipsp_address(saidx->dst),
428 (u_long) ntohl(sav->spi)));
430 IPSEC_ISTAT(sproto, pdrops);
437 /* IPv6-in-IP encapsulation. */
438 else if (prot == IPPROTO_IPV6 &&
439 saidx->mode != IPSEC_MODE_TRANSPORT) {
441 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
442 IPSEC_ISTAT(sproto, hdrops);
446 /* enc0: strip IPv4 header, keep IPv6 header only */
447 m_striphdr(m, 0, ip->ip_hl << 2);
450 * Check that the inner source address is the same as
451 * the proxy address, if available.
453 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
454 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
455 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
456 &saidx->proxy.sin6.sin6_addr)) ||
457 (saidx->proxy.sa.sa_family != AF_INET6 &&
458 saidx->proxy.sa.sa_family != 0)) {
460 DPRINTF(("%s: inner source address %s doesn't "
461 "correspond to expected proxy source %s, "
462 "SA %s/%08lx\n", __func__,
463 ip6_sprintf(ip6buf, &ip6n.ip6_src),
464 ipsec_address(&saidx->proxy),
465 ipsec_address(&saidx->dst),
466 (u_long) ntohl(sav->spi)));
468 IPSEC_ISTAT(sproto, pdrops);
475 else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
477 * When mode is wildcard, inner protocol is IPv6 and
478 * we have no INET6 support - drop this packet a bit later.
479 * In other cases we assume transport mode and outer
480 * header was already stripped in xform_xxx_cb.
486 * Record what we've done to the packet (under what SA it was
489 if (sproto != IPPROTO_IPCOMP) {
490 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
491 sizeof(struct tdb_ident), M_NOWAIT);
493 DPRINTF(("%s: failed to get tag\n", __func__));
494 IPSEC_ISTAT(sproto, hdrops);
499 tdbi = (struct tdb_ident *)(mtag + 1);
500 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
501 tdbi->proto = sproto;
502 tdbi->spi = sav->spi;
503 /* Cache those two for enc(4) in xform_ipip. */
504 tdbi->alg_auth = sav->alg_auth;
505 tdbi->alg_enc = sav->alg_enc;
507 m_tag_prepend(m, mtag);
510 key_sa_recordxfer(sav, m); /* record data transfer */
513 * In transport mode requeue decrypted mbuf back to IPv4 protocol
514 * handler. This is necessary to correctly expose rcvif.
516 if (saidx->mode == IPSEC_MODE_TRANSPORT)
520 * Pass the mbuf to enc0 for bpf and pfil.
522 if (prot == IPPROTO_IPIP)
523 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
525 if (prot == IPPROTO_IPV6)
526 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
529 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
534 * Re-dispatch via software interrupt.
539 isr_prot = NETISR_IP;
543 isr_prot = NETISR_IPV6;
547 DPRINTF(("%s: cannot handle inner ip proto %d\n",
549 IPSEC_ISTAT(sproto, nopf);
550 error = EPFNOSUPPORT;
554 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
556 IPSEC_ISTAT(sproto, qfull);
557 DPRINTF(("%s: queue full; proto %u packet dropped\n",
568 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
570 /* XXX nothing just yet */
575 /* IPv6 AH wrapper. */
577 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
583 if (*offp < sizeof(struct ip6_hdr)) {
584 DPRINTF(("%s: bad offset %u\n", __func__, *offp));
586 } else if (*offp == sizeof(struct ip6_hdr)) {
587 protoff = offsetof(struct ip6_hdr, ip6_nxt);
589 /* Chase down the header chain... */
590 protoff = sizeof(struct ip6_hdr);
594 m_copydata(*mp, protoff, sizeof(ip6e),
597 if (ip6e.ip6e_nxt == IPPROTO_AH)
598 l = (ip6e.ip6e_len + 2) << 2;
600 l = (ip6e.ip6e_len + 1) << 3;
601 IPSEC_ASSERT(l > 0, ("l went zero or negative"));
602 } while (protoff + l < *offp);
604 /* Malformed packet check */
605 if (protoff + l != *offp) {
606 DPRINTF(("%s: bad packet header chain, protoff %u, "
607 "l %u, off %u\n", __func__, protoff, l, *offp));
608 IPSEC_ISTAT(proto, hdrops);
613 protoff += offsetof(struct ip6_ext, ip6e_nxt);
615 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
620 * IPsec input callback, called by the transform callback. Takes care of
621 * filtering and other sanity checks on the processed packet.
624 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
627 char buf[INET6_ADDRSTRLEN];
628 int prot, af, sproto;
631 struct tdb_ident *tdbi;
632 struct secasindex *saidx;
637 char ip6buf[INET6_ADDRSTRLEN];
640 IPSEC_ASSERT(m != NULL, ("null mbuf"));
641 IPSEC_ASSERT(sav != NULL, ("null SA"));
642 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
643 saidx = &sav->sah->saidx;
644 af = saidx->dst.sa.sa_family;
645 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
646 sproto = saidx->proto;
647 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
648 sproto == IPPROTO_IPCOMP,
649 ("unexpected security protocol %u", sproto));
653 DPRINTF(("%s: null mbuf", __func__));
654 IPSEC_ISTAT(sproto, badkcr);
659 /* Fix IPv6 header */
660 if (m->m_len < sizeof(struct ip6_hdr) &&
661 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
663 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
664 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
665 sizeof(buf)), (u_long) ntohl(sav->spi)));
667 IPSEC_ISTAT(sproto, hdrops);
672 ip6 = mtod(m, struct ip6_hdr *);
673 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
676 m_copydata(m, protoff, 1, &nxt8);
680 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
681 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
683 /* Pass the mbuf to enc0 for bpf and pfil. */
684 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
685 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
689 /* IPv6-in-IP encapsulation */
690 if (prot == IPPROTO_IPV6 &&
691 saidx->mode != IPSEC_MODE_TRANSPORT) {
692 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
693 IPSEC_ISTAT(sproto, hdrops);
697 /* ip6n will now contain the inner IPv6 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_INET6 &&
706 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
707 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
708 &saidx->proxy.sin6.sin6_addr)) ||
709 (saidx->proxy.sa.sa_family != AF_INET6 &&
710 saidx->proxy.sa.sa_family != 0)) {
712 DPRINTF(("%s: inner source address %s doesn't "
713 "correspond to expected proxy source %s, "
714 "SA %s/%08lx\n", __func__,
715 ip6_sprintf(ip6buf, &ip6n.ip6_src),
716 ipsec_address(&saidx->proxy),
717 ipsec_address(&saidx->dst),
718 (u_long) ntohl(sav->spi)));
720 IPSEC_ISTAT(sproto, pdrops);
727 /* IP-in-IP encapsulation */
728 else if (prot == IPPROTO_IPIP &&
729 saidx->mode != IPSEC_MODE_TRANSPORT) {
730 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
731 IPSEC_ISTAT(sproto, hdrops);
735 /* ipn will now contain the inner IPv4 header */
736 m_striphdr(m, 0, skip);
740 * Check that the inner source address is the same as
741 * the proxy address, if available.
743 if ((saidx->proxy.sa.sa_family == AF_INET &&
744 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
745 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
746 (saidx->proxy.sa.sa_family != AF_INET &&
747 saidx->proxy.sa.sa_family != 0)) {
749 DPRINTF(("%s: inner source address %s doesn't "
750 "correspond to expected proxy source %s, "
751 "SA %s/%08lx\n", __func__,
752 inet_ntoa4(ipn.ip_src),
753 ipsec_address(&saidx->proxy),
754 ipsec_address(&saidx->dst),
755 (u_long) ntohl(sav->spi)));
757 IPSEC_ISTAT(sproto, pdrops);
765 prot = IPPROTO_IPV6; /* for correct BPF processing */
769 * Record what we've done to the packet (under what SA it was
772 if (sproto != IPPROTO_IPCOMP) {
773 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
774 sizeof(struct tdb_ident), M_NOWAIT);
776 DPRINTF(("%s: failed to get tag\n", __func__));
777 IPSEC_ISTAT(sproto, hdrops);
782 tdbi = (struct tdb_ident *)(mtag + 1);
783 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
784 tdbi->proto = sproto;
785 tdbi->spi = sav->spi;
786 /* Cache those two for enc(4) in xform_ipip. */
787 tdbi->alg_auth = sav->alg_auth;
788 tdbi->alg_enc = sav->alg_enc;
790 m_tag_prepend(m, mtag);
793 key_sa_recordxfer(sav, m);
797 * Pass the mbuf to enc0 for bpf and pfil.
800 if (prot == IPPROTO_IPIP)
801 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
803 if (prot == IPPROTO_IPV6)
804 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
806 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
811 * We stripped outer IPv6 header.
812 * Now we should requeue decrypted packet via netisr.
817 isr_prot = NETISR_IP;
821 isr_prot = NETISR_IPV6;
824 DPRINTF(("%s: cannot handle inner ip proto %d\n",
826 IPSEC_ISTAT(sproto, nopf);
827 error = EPFNOSUPPORT;
830 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
832 IPSEC_ISTAT(sproto, qfull);
833 DPRINTF(("%s: queue full; proto %u packet dropped\n",
839 * See the end of ip6_input for this logic.
840 * IPPROTO_IPV[46] case will be processed just like other ones
844 while (nxt != IPPROTO_DONE) {
845 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
846 IP6STAT_INC(ip6s_toomanyhdr);
852 * Protection against faulty packet - there should be
853 * more sanity checks in header chain processing.
855 if (m->m_pkthdr.len < skip) {
856 IP6STAT_INC(ip6s_tooshort);
857 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
862 * Enforce IPsec policy checking if we are seeing last header.
863 * note that we do not visit this with protocols with pcb layer
864 * code - like udp/tcp/raw ip.
866 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
867 ipsec6_in_reject(m, NULL)) {
871 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
881 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
883 struct ip6ctlparam *ip6cp = NULL;
884 struct mbuf *m = NULL;
888 if (sa->sa_family != AF_INET6 ||
889 sa->sa_len != sizeof(struct sockaddr_in6))
891 if ((unsigned)cmd >= PRC_NCMDS)
894 /* if the parameter is from icmp6, decode it. */
896 ip6cp = (struct ip6ctlparam *)d;
898 ip6 = ip6cp->ip6c_ip6;
899 off = ip6cp->ip6c_off;
903 off = 0; /* calm gcc */
908 struct ip6ctlparam ip6cp1;
911 * Notify the error to all possible sockets via pfctlinput2.
912 * Since the upper layer information (such as protocol type,
913 * source and destination ports) is embedded in the encrypted
914 * data and might have been cut, we can't directly call
915 * an upper layer ctlinput function. However, the pcbnotify
916 * function will consider source and destination addresses
917 * as well as the flow info value, and may be able to find
918 * some PCB that should be notified.
919 * Although pfctlinput2 will call esp6_ctlinput(), there is
920 * no possibility of an infinite loop of function calls,
921 * because we don't pass the inner IPv6 header.
923 bzero(&ip6cp1, sizeof(ip6cp1));
924 ip6cp1.ip6c_src = ip6cp->ip6c_src;
925 pfctlinput2(cmd, sa, (void *)&ip6cp1);
928 * Then go to special cases that need ESP header information.
929 * XXX: We assume that when ip6 is non NULL,
930 * M and OFF are valid.
933 if (cmd == PRC_MSGSIZE) {
934 struct secasvar *sav;
938 /* check header length before using m_copydata */
939 if (m->m_pkthdr.len < off + sizeof (struct esp))
941 m_copydata(m, off + offsetof(struct esp, esp_spi),
942 sizeof(u_int32_t), (caddr_t) &spi);
944 * Check to see if we have a valid SA corresponding to
945 * the address in the ICMP message payload.
947 sav = KEY_ALLOCSA((union sockaddr_union *)sa,
949 valid = (sav != NULL);
953 /* XXX Further validation? */
956 * Depending on whether the SA is "valid" and
957 * routing table size (mtudisc_{hi,lo}wat), we will:
958 * - recalcurate the new MTU and create the
959 * corresponding routing entry, or
960 * - ignore the MTU change notification.
962 icmp6_mtudisc_update(ip6cp, valid);
965 /* we normally notify any pcb here */