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 ah4_input(struct mbuf **mp, int *offp, int proto)
256 ipsec_common_input(m, off, offsetof(struct ip, ip_p),
257 AF_INET, IPPROTO_AH);
258 return (IPPROTO_DONE);
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 **mp, int *offp, int proto)
278 ipsec_common_input(m, off, offsetof(struct ip, ip_p),
279 AF_INET, IPPROTO_ESP);
280 return (IPPROTO_DONE);
284 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
286 if (sa->sa_family == AF_INET &&
287 sa->sa_len == sizeof(struct sockaddr_in))
288 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
292 ipcomp4_input(struct mbuf **mp, int *offp, int proto)
301 ipsec_common_input(m, off, offsetof(struct ip, ip_p),
302 AF_INET, IPPROTO_IPCOMP);
303 return (IPPROTO_DONE);
307 * IPsec input callback for INET protocols.
308 * This routine is called as the transform callback.
309 * Takes care of filtering and other sanity checks on
310 * the processed packet.
313 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
316 char buf[INET6_ADDRSTRLEN];
317 int prot, af, sproto, isr_prot;
320 struct tdb_ident *tdbi;
321 struct secasindex *saidx;
325 char ip6buf[INET6_ADDRSTRLEN];
329 IPSEC_ASSERT(m != NULL, ("null mbuf"));
330 IPSEC_ASSERT(sav != NULL, ("null SA"));
331 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
332 saidx = &sav->sah->saidx;
333 af = saidx->dst.sa.sa_family;
334 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
335 sproto = saidx->proto;
336 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
337 sproto == IPPROTO_IPCOMP,
338 ("unexpected security protocol %u", sproto));
342 DPRINTF(("%s: null mbuf", __func__));
343 IPSEC_ISTAT(sproto, badkcr);
351 * XXXGL: do we need this entire block?
353 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
354 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
355 __func__, ipsec_address(&sav->sah->saidx.dst,
356 buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
357 IPSEC_ISTAT(sproto, hdrops);
362 ip = mtod(m, struct ip *);
363 ip->ip_len = htons(m->m_pkthdr.len);
365 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
367 ip = mtod(m, struct ip *);
372 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
373 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
375 /* Pass the mbuf to enc0 for bpf and pfil. */
376 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
377 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
379 ip = mtod(m, struct ip *);
382 /* IP-in-IP encapsulation */
383 if (prot == IPPROTO_IPIP &&
384 saidx->mode != IPSEC_MODE_TRANSPORT) {
386 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
387 IPSEC_ISTAT(sproto, hdrops);
391 /* enc0: strip outer IPv4 header */
392 m_striphdr(m, 0, ip->ip_hl << 2);
395 /* XXX PROXY address isn't recorded in SAH */
397 * Check that the inner source address is the same as
398 * the proxy address, if available.
400 if ((saidx->proxy.sa.sa_family == AF_INET &&
401 saidx->proxy.sin.sin_addr.s_addr !=
404 saidx->proxy.sin.sin_addr.s_addr) ||
405 (saidx->proxy.sa.sa_family != AF_INET &&
406 saidx->proxy.sa.sa_family != 0)) {
408 DPRINTF(("%s: inner source address %s doesn't "
409 "correspond to expected proxy source %s, "
410 "SA %s/%08lx\n", __func__,
411 inet_ntoa4(ipn.ip_src),
412 ipsp_address(saidx->proxy),
413 ipsp_address(saidx->dst),
414 (u_long) ntohl(sav->spi)));
416 IPSEC_ISTAT(sproto, pdrops);
423 /* IPv6-in-IP encapsulation. */
424 else if (prot == IPPROTO_IPV6 &&
425 saidx->mode != IPSEC_MODE_TRANSPORT) {
427 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
428 IPSEC_ISTAT(sproto, hdrops);
432 /* enc0: strip IPv4 header, keep IPv6 header only */
433 m_striphdr(m, 0, ip->ip_hl << 2);
436 * Check that the inner source address is the same as
437 * the proxy address, if available.
439 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
440 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
441 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
442 &saidx->proxy.sin6.sin6_addr)) ||
443 (saidx->proxy.sa.sa_family != AF_INET6 &&
444 saidx->proxy.sa.sa_family != 0)) {
446 DPRINTF(("%s: inner source address %s doesn't "
447 "correspond to expected proxy source %s, "
448 "SA %s/%08lx\n", __func__,
449 ip6_sprintf(ip6buf, &ip6n.ip6_src),
450 ipsec_address(&saidx->proxy),
451 ipsec_address(&saidx->dst),
452 (u_long) ntohl(sav->spi)));
454 IPSEC_ISTAT(sproto, pdrops);
461 else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
463 * When mode is wildcard, inner protocol is IPv6 and
464 * we have no INET6 support - drop this packet a bit later.
465 * In other cases we assume transport mode and outer
466 * header was already stripped in xform_xxx_cb.
472 * Record what we've done to the packet (under what SA it was
475 if (sproto != IPPROTO_IPCOMP) {
476 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
477 sizeof(struct tdb_ident), M_NOWAIT);
479 DPRINTF(("%s: failed to get tag\n", __func__));
480 IPSEC_ISTAT(sproto, hdrops);
485 tdbi = (struct tdb_ident *)(mtag + 1);
486 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
487 tdbi->proto = sproto;
488 tdbi->spi = sav->spi;
489 /* Cache those two for enc(4) in xform_ipip. */
490 tdbi->alg_auth = sav->alg_auth;
491 tdbi->alg_enc = sav->alg_enc;
493 m_tag_prepend(m, mtag);
496 key_sa_recordxfer(sav, m); /* record data transfer */
499 * In transport mode requeue decrypted mbuf back to IPv4 protocol
500 * handler. This is necessary to correctly expose rcvif.
502 if (saidx->mode == IPSEC_MODE_TRANSPORT)
506 * Pass the mbuf to enc0 for bpf and pfil.
508 if (prot == IPPROTO_IPIP)
509 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
511 if (prot == IPPROTO_IPV6)
512 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
515 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
520 * Re-dispatch via software interrupt.
525 isr_prot = NETISR_IP;
529 isr_prot = NETISR_IPV6;
533 DPRINTF(("%s: cannot handle inner ip proto %d\n",
535 IPSEC_ISTAT(sproto, nopf);
536 error = EPFNOSUPPORT;
540 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
542 IPSEC_ISTAT(sproto, qfull);
543 DPRINTF(("%s: queue full; proto %u packet dropped\n",
554 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
556 /* XXX nothing just yet */
561 /* IPv6 AH wrapper. */
563 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
569 if (*offp < sizeof(struct ip6_hdr)) {
570 DPRINTF(("%s: bad offset %u\n", __func__, *offp));
572 } else if (*offp == sizeof(struct ip6_hdr)) {
573 protoff = offsetof(struct ip6_hdr, ip6_nxt);
575 /* Chase down the header chain... */
576 protoff = sizeof(struct ip6_hdr);
580 m_copydata(*mp, protoff, sizeof(ip6e),
583 if (ip6e.ip6e_nxt == IPPROTO_AH)
584 l = (ip6e.ip6e_len + 2) << 2;
586 l = (ip6e.ip6e_len + 1) << 3;
587 IPSEC_ASSERT(l > 0, ("l went zero or negative"));
588 } while (protoff + l < *offp);
590 /* Malformed packet check */
591 if (protoff + l != *offp) {
592 DPRINTF(("%s: bad packet header chain, protoff %u, "
593 "l %u, off %u\n", __func__, protoff, l, *offp));
594 IPSEC_ISTAT(proto, hdrops);
599 protoff += offsetof(struct ip6_ext, ip6e_nxt);
601 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
606 * IPsec input callback, called by the transform callback. Takes care of
607 * filtering and other sanity checks on the processed packet.
610 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
613 char buf[INET6_ADDRSTRLEN];
614 int prot, af, sproto;
617 struct tdb_ident *tdbi;
618 struct secasindex *saidx;
623 char ip6buf[INET6_ADDRSTRLEN];
626 IPSEC_ASSERT(m != NULL, ("null mbuf"));
627 IPSEC_ASSERT(sav != NULL, ("null SA"));
628 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
629 saidx = &sav->sah->saidx;
630 af = saidx->dst.sa.sa_family;
631 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
632 sproto = saidx->proto;
633 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
634 sproto == IPPROTO_IPCOMP,
635 ("unexpected security protocol %u", sproto));
639 DPRINTF(("%s: null mbuf", __func__));
640 IPSEC_ISTAT(sproto, badkcr);
645 /* Fix IPv6 header */
646 if (m->m_len < sizeof(struct ip6_hdr) &&
647 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
649 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
650 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
651 sizeof(buf)), (u_long) ntohl(sav->spi)));
653 IPSEC_ISTAT(sproto, hdrops);
658 ip6 = mtod(m, struct ip6_hdr *);
659 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
662 m_copydata(m, protoff, 1, &nxt8);
666 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
667 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
669 /* Pass the mbuf to enc0 for bpf and pfil. */
670 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
671 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
675 /* IPv6-in-IP encapsulation */
676 if (prot == IPPROTO_IPV6 &&
677 saidx->mode != IPSEC_MODE_TRANSPORT) {
678 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
679 IPSEC_ISTAT(sproto, hdrops);
683 /* ip6n will now contain the inner IPv6 header. */
684 m_striphdr(m, 0, skip);
688 * Check that the inner source address is the same as
689 * the proxy address, if available.
691 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
692 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
693 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
694 &saidx->proxy.sin6.sin6_addr)) ||
695 (saidx->proxy.sa.sa_family != AF_INET6 &&
696 saidx->proxy.sa.sa_family != 0)) {
698 DPRINTF(("%s: inner source address %s doesn't "
699 "correspond to expected proxy source %s, "
700 "SA %s/%08lx\n", __func__,
701 ip6_sprintf(ip6buf, &ip6n.ip6_src),
702 ipsec_address(&saidx->proxy),
703 ipsec_address(&saidx->dst),
704 (u_long) ntohl(sav->spi)));
706 IPSEC_ISTAT(sproto, pdrops);
713 /* IP-in-IP encapsulation */
714 else if (prot == IPPROTO_IPIP &&
715 saidx->mode != IPSEC_MODE_TRANSPORT) {
716 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
717 IPSEC_ISTAT(sproto, hdrops);
721 /* ipn will now contain the inner IPv4 header */
722 m_striphdr(m, 0, skip);
726 * Check that the inner source address is the same as
727 * the proxy address, if available.
729 if ((saidx->proxy.sa.sa_family == AF_INET &&
730 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
731 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
732 (saidx->proxy.sa.sa_family != AF_INET &&
733 saidx->proxy.sa.sa_family != 0)) {
735 DPRINTF(("%s: inner source address %s doesn't "
736 "correspond to expected proxy source %s, "
737 "SA %s/%08lx\n", __func__,
738 inet_ntoa4(ipn.ip_src),
739 ipsec_address(&saidx->proxy),
740 ipsec_address(&saidx->dst),
741 (u_long) ntohl(sav->spi)));
743 IPSEC_ISTAT(sproto, pdrops);
751 prot = IPPROTO_IPV6; /* for correct BPF processing */
755 * Record what we've done to the packet (under what SA it was
758 if (sproto != IPPROTO_IPCOMP) {
759 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
760 sizeof(struct tdb_ident), M_NOWAIT);
762 DPRINTF(("%s: failed to get tag\n", __func__));
763 IPSEC_ISTAT(sproto, hdrops);
768 tdbi = (struct tdb_ident *)(mtag + 1);
769 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
770 tdbi->proto = sproto;
771 tdbi->spi = sav->spi;
772 /* Cache those two for enc(4) in xform_ipip. */
773 tdbi->alg_auth = sav->alg_auth;
774 tdbi->alg_enc = sav->alg_enc;
776 m_tag_prepend(m, mtag);
779 key_sa_recordxfer(sav, m);
783 * Pass the mbuf to enc0 for bpf and pfil.
786 if (prot == IPPROTO_IPIP)
787 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
789 if (prot == IPPROTO_IPV6)
790 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
792 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
797 * We stripped outer IPv6 header.
798 * Now we should requeue decrypted packet via netisr.
803 isr_prot = NETISR_IP;
807 isr_prot = NETISR_IPV6;
810 DPRINTF(("%s: cannot handle inner ip proto %d\n",
812 IPSEC_ISTAT(sproto, nopf);
813 error = EPFNOSUPPORT;
816 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
818 IPSEC_ISTAT(sproto, qfull);
819 DPRINTF(("%s: queue full; proto %u packet dropped\n",
825 * See the end of ip6_input for this logic.
826 * IPPROTO_IPV[46] case will be processed just like other ones
830 while (nxt != IPPROTO_DONE) {
831 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
832 IP6STAT_INC(ip6s_toomanyhdr);
838 * Protection against faulty packet - there should be
839 * more sanity checks in header chain processing.
841 if (m->m_pkthdr.len < skip) {
842 IP6STAT_INC(ip6s_tooshort);
843 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
848 * Enforce IPsec policy checking if we are seeing last header.
849 * note that we do not visit this with protocols with pcb layer
850 * code - like udp/tcp/raw ip.
852 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
853 ipsec6_in_reject(m, NULL)) {
857 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
867 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
869 struct ip6ctlparam *ip6cp = NULL;
870 struct mbuf *m = NULL;
874 if (sa->sa_family != AF_INET6 ||
875 sa->sa_len != sizeof(struct sockaddr_in6))
877 if ((unsigned)cmd >= PRC_NCMDS)
880 /* if the parameter is from icmp6, decode it. */
882 ip6cp = (struct ip6ctlparam *)d;
884 ip6 = ip6cp->ip6c_ip6;
885 off = ip6cp->ip6c_off;
889 off = 0; /* calm gcc */
894 struct ip6ctlparam ip6cp1;
897 * Notify the error to all possible sockets via pfctlinput2.
898 * Since the upper layer information (such as protocol type,
899 * source and destination ports) is embedded in the encrypted
900 * data and might have been cut, we can't directly call
901 * an upper layer ctlinput function. However, the pcbnotify
902 * function will consider source and destination addresses
903 * as well as the flow info value, and may be able to find
904 * some PCB that should be notified.
905 * Although pfctlinput2 will call esp6_ctlinput(), there is
906 * no possibility of an infinite loop of function calls,
907 * because we don't pass the inner IPv6 header.
909 bzero(&ip6cp1, sizeof(ip6cp1));
910 ip6cp1.ip6c_src = ip6cp->ip6c_src;
911 pfctlinput2(cmd, sa, (void *)&ip6cp1);
914 * Then go to special cases that need ESP header information.
915 * XXX: We assume that when ip6 is non NULL,
916 * M and OFF are valid.
919 if (cmd == PRC_MSGSIZE) {
920 struct secasvar *sav;
924 /* check header length before using m_copydata */
925 if (m->m_pkthdr.len < off + sizeof (struct esp))
927 m_copydata(m, off + offsetof(struct esp, esp_spi),
928 sizeof(u_int32_t), (caddr_t) &spi);
930 * Check to see if we have a valid SA corresponding to
931 * the address in the ICMP message payload.
933 sav = KEY_ALLOCSA((union sockaddr_union *)sa,
935 valid = (sav != NULL);
939 /* XXX Further validation? */
942 * Depending on whether the SA is "valid" and
943 * routing table size (mtudisc_{hi,lo}wat), we will:
944 * - recalcurate the new MTU and create the
945 * corresponding routing entry, or
946 * - ignore the MTU change notification.
948 icmp6_mtudisc_update(ip6cp, valid);
951 /* we normally notify any pcb here */