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 union sockaddr_union dst_address;
125 struct secasvar *sav;
134 IPSEC_ISTAT(sproto, input);
136 IPSEC_ASSERT(m != NULL, ("null packet"));
138 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
139 sproto == IPPROTO_IPCOMP,
140 ("unexpected security protocol %u", sproto));
142 if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
143 (sproto == IPPROTO_AH && !V_ah_enable) ||
144 (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
146 IPSEC_ISTAT(sproto, pdrops);
150 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
152 IPSEC_ISTAT(sproto, hdrops);
153 DPRINTF(("%s: packet too small\n", __func__));
157 /* Retrieve the SPI from the relevant IPsec header */
158 if (sproto == IPPROTO_ESP)
159 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
160 else if (sproto == IPPROTO_AH)
161 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
163 else if (sproto == IPPROTO_IPCOMP) {
165 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
167 spi = ntohl(htons(cpi));
171 * Find the SA and (indirectly) call the appropriate
172 * kernel crypto routine. The resulting mbuf chain is a valid
173 * IP packet ready to go through input processing.
175 bzero(&dst_address, sizeof (dst_address));
176 dst_address.sa.sa_family = af;
180 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
181 m_copydata(m, offsetof(struct ip, ip_dst),
182 sizeof(struct in_addr),
183 (caddr_t) &dst_address.sin.sin_addr);
185 /* Find the source port for NAT-T; see udp*_espdecap. */
186 tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
188 dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1];
189 #endif /* IPSEC_NAT_T */
194 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
195 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
196 sizeof(struct in6_addr),
197 (caddr_t) &dst_address.sin6.sin6_addr);
201 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
203 IPSEC_ISTAT(sproto, nopf);
207 /* NB: only pass dst since key_allocsa follows RFC2401 */
208 sav = KEY_ALLOCSA(&dst_address, sproto, spi);
210 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
211 __func__, ipsec_address(&dst_address),
212 (u_long) ntohl(spi), sproto));
213 IPSEC_ISTAT(sproto, notdb);
218 if (sav->tdb_xform == NULL) {
219 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
220 __func__, ipsec_address(&dst_address),
221 (u_long) ntohl(spi), sproto));
222 IPSEC_ISTAT(sproto, noxform);
229 * Call appropriate transform and return -- callback takes care of
232 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
239 * Common input handler for IPv4 AH, ESP, and IPCOMP.
242 ipsec4_common_input(struct mbuf *m, ...)
248 off = va_arg(ap, int);
249 nxt = va_arg(ap, int);
252 return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
257 ah4_input(struct mbuf **mp, int *offp, int proto)
266 ipsec4_common_input(m, off, IPPROTO_AH);
267 return (IPPROTO_DONE);
270 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
272 if (sa->sa_family == AF_INET &&
273 sa->sa_len == sizeof(struct sockaddr_in))
274 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
278 esp4_input(struct mbuf **mp, int *offp, int proto)
287 ipsec4_common_input(m, off, IPPROTO_ESP);
288 return (IPPROTO_DONE);
292 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
294 if (sa->sa_family == AF_INET &&
295 sa->sa_len == sizeof(struct sockaddr_in))
296 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
300 ipcomp4_input(struct mbuf **mp, int *offp, int proto)
309 ipsec4_common_input(m, off, IPPROTO_IPCOMP);
310 return (IPPROTO_DONE);
314 * IPsec input callback for INET protocols.
315 * This routine is called as the transform callback.
316 * Takes care of filtering and other sanity checks on
317 * the processed packet.
320 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
323 int prot, af, sproto, isr_prot;
326 struct tdb_ident *tdbi;
327 struct secasindex *saidx;
331 char ip6buf[INET6_ADDRSTRLEN];
335 IPSEC_ASSERT(m != NULL, ("null mbuf"));
336 IPSEC_ASSERT(sav != NULL, ("null SA"));
337 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
338 saidx = &sav->sah->saidx;
339 af = saidx->dst.sa.sa_family;
340 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
341 sproto = saidx->proto;
342 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
343 sproto == IPPROTO_IPCOMP,
344 ("unexpected security protocol %u", sproto));
348 DPRINTF(("%s: null mbuf", __func__));
349 IPSEC_ISTAT(sproto, badkcr);
357 * XXXGL: do we need this entire block?
359 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
360 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
361 __func__, ipsec_address(&sav->sah->saidx.dst),
362 (u_long) ntohl(sav->spi)));
363 IPSEC_ISTAT(sproto, hdrops);
368 ip = mtod(m, struct ip *);
369 ip->ip_len = htons(m->m_pkthdr.len);
371 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
373 ip = mtod(m, struct ip *);
378 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
379 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
381 /* Pass the mbuf to enc0 for bpf and pfil. */
382 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
383 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
387 /* IP-in-IP encapsulation */
388 if (prot == IPPROTO_IPIP &&
389 saidx->mode != IPSEC_MODE_TRANSPORT) {
391 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
392 IPSEC_ISTAT(sproto, hdrops);
396 /* enc0: strip outer IPv4 header */
397 m_striphdr(m, 0, ip->ip_hl << 2);
400 /* XXX PROXY address isn't recorded in SAH */
402 * Check that the inner source address is the same as
403 * the proxy address, if available.
405 if ((saidx->proxy.sa.sa_family == AF_INET &&
406 saidx->proxy.sin.sin_addr.s_addr !=
409 saidx->proxy.sin.sin_addr.s_addr) ||
410 (saidx->proxy.sa.sa_family != AF_INET &&
411 saidx->proxy.sa.sa_family != 0)) {
413 DPRINTF(("%s: inner source address %s doesn't "
414 "correspond to expected proxy source %s, "
415 "SA %s/%08lx\n", __func__,
416 inet_ntoa4(ipn.ip_src),
417 ipsp_address(saidx->proxy),
418 ipsp_address(saidx->dst),
419 (u_long) ntohl(sav->spi)));
421 IPSEC_ISTAT(sproto, pdrops);
428 /* IPv6-in-IP encapsulation. */
429 else if (prot == IPPROTO_IPV6 &&
430 saidx->mode != IPSEC_MODE_TRANSPORT) {
432 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
433 IPSEC_ISTAT(sproto, hdrops);
437 /* enc0: strip IPv4 header, keep IPv6 header only */
438 m_striphdr(m, 0, ip->ip_hl << 2);
441 * Check that the inner source address is the same as
442 * the proxy address, if available.
444 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
445 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
446 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
447 &saidx->proxy.sin6.sin6_addr)) ||
448 (saidx->proxy.sa.sa_family != AF_INET6 &&
449 saidx->proxy.sa.sa_family != 0)) {
451 DPRINTF(("%s: inner source address %s doesn't "
452 "correspond to expected proxy source %s, "
453 "SA %s/%08lx\n", __func__,
454 ip6_sprintf(ip6buf, &ip6n.ip6_src),
455 ipsec_address(&saidx->proxy),
456 ipsec_address(&saidx->dst),
457 (u_long) ntohl(sav->spi)));
459 IPSEC_ISTAT(sproto, pdrops);
466 else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
468 * When mode is wildcard, inner protocol is IPv6 and
469 * we have no INET6 support - drop this packet a bit later.
470 * In other cases we assume transport mode and outer
471 * header was already stripped in xform_xxx_cb.
477 * Record what we've done to the packet (under what SA it was
480 if (sproto != IPPROTO_IPCOMP) {
481 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
482 sizeof(struct tdb_ident), M_NOWAIT);
484 DPRINTF(("%s: failed to get tag\n", __func__));
485 IPSEC_ISTAT(sproto, hdrops);
490 tdbi = (struct tdb_ident *)(mtag + 1);
491 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
492 tdbi->proto = sproto;
493 tdbi->spi = sav->spi;
494 /* Cache those two for enc(4) in xform_ipip. */
495 tdbi->alg_auth = sav->alg_auth;
496 tdbi->alg_enc = sav->alg_enc;
498 m_tag_prepend(m, mtag);
501 key_sa_recordxfer(sav, m); /* record data transfer */
504 * In transport mode requeue decrypted mbuf back to IPv4 protocol
505 * handler. This is necessary to correctly expose rcvif.
507 if (saidx->mode == IPSEC_MODE_TRANSPORT)
511 * Pass the mbuf to enc0 for bpf and pfil.
513 if (prot == IPPROTO_IPIP)
514 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
516 if (prot == IPPROTO_IPV6)
517 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
520 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
525 * Re-dispatch via software interrupt.
530 isr_prot = NETISR_IP;
534 isr_prot = NETISR_IPV6;
538 DPRINTF(("%s: cannot handle inner ip proto %d\n",
540 IPSEC_ISTAT(sproto, nopf);
541 error = EPFNOSUPPORT;
545 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
547 IPSEC_ISTAT(sproto, qfull);
548 DPRINTF(("%s: queue full; proto %u packet dropped\n",
559 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
561 /* XXX nothing just yet */
566 /* IPv6 AH wrapper. */
568 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
574 if (*offp < sizeof(struct ip6_hdr)) {
575 DPRINTF(("%s: bad offset %u\n", __func__, *offp));
577 } else if (*offp == sizeof(struct ip6_hdr)) {
578 protoff = offsetof(struct ip6_hdr, ip6_nxt);
580 /* Chase down the header chain... */
581 protoff = sizeof(struct ip6_hdr);
585 m_copydata(*mp, protoff, sizeof(ip6e),
588 if (ip6e.ip6e_nxt == IPPROTO_AH)
589 l = (ip6e.ip6e_len + 2) << 2;
591 l = (ip6e.ip6e_len + 1) << 3;
592 IPSEC_ASSERT(l > 0, ("l went zero or negative"));
593 } while (protoff + l < *offp);
595 /* Malformed packet check */
596 if (protoff + l != *offp) {
597 DPRINTF(("%s: bad packet header chain, protoff %u, "
598 "l %u, off %u\n", __func__, protoff, l, *offp));
599 IPSEC_ISTAT(proto, hdrops);
604 protoff += offsetof(struct ip6_ext, ip6e_nxt);
606 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
611 * IPsec input callback, called by the transform callback. Takes care of
612 * filtering and other sanity checks on the processed packet.
615 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
618 int prot, af, sproto;
621 struct tdb_ident *tdbi;
622 struct secasindex *saidx;
627 char ip6buf[INET6_ADDRSTRLEN];
630 IPSEC_ASSERT(m != NULL, ("null mbuf"));
631 IPSEC_ASSERT(sav != NULL, ("null SA"));
632 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
633 saidx = &sav->sah->saidx;
634 af = saidx->dst.sa.sa_family;
635 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
636 sproto = saidx->proto;
637 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
638 sproto == IPPROTO_IPCOMP,
639 ("unexpected security protocol %u", sproto));
643 DPRINTF(("%s: null mbuf", __func__));
644 IPSEC_ISTAT(sproto, badkcr);
649 /* Fix IPv6 header */
650 if (m->m_len < sizeof(struct ip6_hdr) &&
651 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
653 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
654 __func__, ipsec_address(&sav->sah->saidx.dst),
655 (u_long) ntohl(sav->spi)));
657 IPSEC_ISTAT(sproto, hdrops);
662 ip6 = mtod(m, struct ip6_hdr *);
663 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
666 m_copydata(m, protoff, 1, &nxt8);
670 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
671 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
673 /* Pass the mbuf to enc0 for bpf and pfil. */
674 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
675 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
679 /* IPv6-in-IP encapsulation */
680 if (prot == IPPROTO_IPV6 &&
681 saidx->mode != IPSEC_MODE_TRANSPORT) {
682 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
683 IPSEC_ISTAT(sproto, hdrops);
687 /* ip6n will now contain the inner IPv6 header. */
688 m_striphdr(m, 0, skip);
692 * Check that the inner source address is the same as
693 * the proxy address, if available.
695 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
696 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
697 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
698 &saidx->proxy.sin6.sin6_addr)) ||
699 (saidx->proxy.sa.sa_family != AF_INET6 &&
700 saidx->proxy.sa.sa_family != 0)) {
702 DPRINTF(("%s: inner source address %s doesn't "
703 "correspond to expected proxy source %s, "
704 "SA %s/%08lx\n", __func__,
705 ip6_sprintf(ip6buf, &ip6n.ip6_src),
706 ipsec_address(&saidx->proxy),
707 ipsec_address(&saidx->dst),
708 (u_long) ntohl(sav->spi)));
710 IPSEC_ISTAT(sproto, pdrops);
717 /* IP-in-IP encapsulation */
718 else if (prot == IPPROTO_IPIP &&
719 saidx->mode != IPSEC_MODE_TRANSPORT) {
720 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
721 IPSEC_ISTAT(sproto, hdrops);
725 /* ipn will now contain the inner IPv4 header */
726 m_striphdr(m, 0, skip);
730 * Check that the inner source address is the same as
731 * the proxy address, if available.
733 if ((saidx->proxy.sa.sa_family == AF_INET &&
734 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
735 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
736 (saidx->proxy.sa.sa_family != AF_INET &&
737 saidx->proxy.sa.sa_family != 0)) {
739 DPRINTF(("%s: inner source address %s doesn't "
740 "correspond to expected proxy source %s, "
741 "SA %s/%08lx\n", __func__,
742 inet_ntoa4(ipn.ip_src),
743 ipsec_address(&saidx->proxy),
744 ipsec_address(&saidx->dst),
745 (u_long) ntohl(sav->spi)));
747 IPSEC_ISTAT(sproto, pdrops);
755 prot = IPPROTO_IPV6; /* for correct BPF processing */
759 * Record what we've done to the packet (under what SA it was
762 if (sproto != IPPROTO_IPCOMP) {
763 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
764 sizeof(struct tdb_ident), M_NOWAIT);
766 DPRINTF(("%s: failed to get tag\n", __func__));
767 IPSEC_ISTAT(sproto, hdrops);
772 tdbi = (struct tdb_ident *)(mtag + 1);
773 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
774 tdbi->proto = sproto;
775 tdbi->spi = sav->spi;
776 /* Cache those two for enc(4) in xform_ipip. */
777 tdbi->alg_auth = sav->alg_auth;
778 tdbi->alg_enc = sav->alg_enc;
780 m_tag_prepend(m, mtag);
783 key_sa_recordxfer(sav, m);
787 * Pass the mbuf to enc0 for bpf and pfil.
790 if (prot == IPPROTO_IPIP)
791 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
793 if (prot == IPPROTO_IPV6)
794 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
796 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
800 * See the end of ip6_input for this logic.
801 * IPPROTO_IPV[46] case will be processed just like other ones
805 while (nxt != IPPROTO_DONE) {
806 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
807 IP6STAT_INC(ip6s_toomanyhdr);
813 * Protection against faulty packet - there should be
814 * more sanity checks in header chain processing.
816 if (m->m_pkthdr.len < skip) {
817 IP6STAT_INC(ip6s_tooshort);
818 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
823 * Enforce IPsec policy checking if we are seeing last header.
824 * note that we do not visit this with protocols with pcb layer
825 * code - like udp/tcp/raw ip.
827 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
828 ipsec6_in_reject(m, NULL)) {
832 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
842 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
844 struct ip6ctlparam *ip6cp = NULL;
845 struct mbuf *m = NULL;
849 if (sa->sa_family != AF_INET6 ||
850 sa->sa_len != sizeof(struct sockaddr_in6))
852 if ((unsigned)cmd >= PRC_NCMDS)
855 /* if the parameter is from icmp6, decode it. */
857 ip6cp = (struct ip6ctlparam *)d;
859 ip6 = ip6cp->ip6c_ip6;
860 off = ip6cp->ip6c_off;
864 off = 0; /* calm gcc */
869 struct ip6ctlparam ip6cp1;
872 * Notify the error to all possible sockets via pfctlinput2.
873 * Since the upper layer information (such as protocol type,
874 * source and destination ports) is embedded in the encrypted
875 * data and might have been cut, we can't directly call
876 * an upper layer ctlinput function. However, the pcbnotify
877 * function will consider source and destination addresses
878 * as well as the flow info value, and may be able to find
879 * some PCB that should be notified.
880 * Although pfctlinput2 will call esp6_ctlinput(), there is
881 * no possibility of an infinite loop of function calls,
882 * because we don't pass the inner IPv6 header.
884 bzero(&ip6cp1, sizeof(ip6cp1));
885 ip6cp1.ip6c_src = ip6cp->ip6c_src;
886 pfctlinput2(cmd, sa, (void *)&ip6cp1);
889 * Then go to special cases that need ESP header information.
890 * XXX: We assume that when ip6 is non NULL,
891 * M and OFF are valid.
894 if (cmd == PRC_MSGSIZE) {
895 struct secasvar *sav;
899 /* check header length before using m_copydata */
900 if (m->m_pkthdr.len < off + sizeof (struct esp))
902 m_copydata(m, off + offsetof(struct esp, esp_spi),
903 sizeof(u_int32_t), (caddr_t) &spi);
905 * Check to see if we have a valid SA corresponding to
906 * the address in the ICMP message payload.
908 sav = KEY_ALLOCSA((union sockaddr_union *)sa,
910 valid = (sav != NULL);
914 /* XXX Further validation? */
917 * Depending on whether the SA is "valid" and
918 * routing table size (mtudisc_{hi,lo}wat), we will:
919 * - recalcurate the new MTU and create the
920 * corresponding routing entry, or
921 * - ignore the MTU change notification.
923 icmp6_mtudisc_update(ip6cp, valid);
926 /* we normally notify any pcb here */