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"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
51 #include <sys/domain.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54 #include <sys/errno.h>
55 #include <sys/hhook.h>
56 #include <sys/syslog.h>
59 #include <net/if_var.h>
60 #include <net/if_enc.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 #define IPSEC_ISTAT(proto, name) do { \
99 if ((proto) == IPPROTO_ESP) \
100 ESPSTAT_INC(esps_##name); \
101 else if ((proto) == IPPROTO_AH) \
102 AHSTAT_INC(ahs_##name); \
104 IPCOMPSTAT_INC(ipcomps_##name); \
108 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
112 * ipsec_common_input gets called when an IPsec-protected packet
113 * is received by IPv4 or IPv6. Its job is to find the right SA
114 * and call the appropriate transform. The transform callback
115 * takes care of further processing (like ingress filtering).
118 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
120 char buf[INET6_ADDRSTRLEN];
121 union sockaddr_union dst_address;
122 struct secasvar *sav;
131 IPSEC_ISTAT(sproto, input);
133 IPSEC_ASSERT(m != NULL, ("null packet"));
135 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
136 sproto == IPPROTO_IPCOMP,
137 ("unexpected security protocol %u", sproto));
139 if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
140 (sproto == IPPROTO_AH && !V_ah_enable) ||
141 (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
143 IPSEC_ISTAT(sproto, pdrops);
147 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
149 IPSEC_ISTAT(sproto, hdrops);
150 DPRINTF(("%s: packet too small\n", __func__));
154 /* Retrieve the SPI from the relevant IPsec header */
155 if (sproto == IPPROTO_ESP)
156 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
157 else if (sproto == IPPROTO_AH)
158 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
160 else if (sproto == IPPROTO_IPCOMP) {
162 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
164 spi = ntohl(htons(cpi));
168 * Find the SA and (indirectly) call the appropriate
169 * kernel crypto routine. The resulting mbuf chain is a valid
170 * IP packet ready to go through input processing.
172 bzero(&dst_address, sizeof (dst_address));
173 dst_address.sa.sa_family = af;
177 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
178 m_copydata(m, offsetof(struct ip, ip_dst),
179 sizeof(struct in_addr),
180 (caddr_t) &dst_address.sin.sin_addr);
182 /* Find the source port for NAT-T; see udp*_espdecap. */
183 tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
185 dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1];
186 #endif /* IPSEC_NAT_T */
191 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
192 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
193 sizeof(struct in6_addr),
194 (caddr_t) &dst_address.sin6.sin6_addr);
195 /* We keep addresses in SADB without embedded scope id */
196 if (IN6_IS_SCOPE_LINKLOCAL(&dst_address.sin6.sin6_addr)) {
197 /* XXX: sa6_recoverscope() */
198 dst_address.sin6.sin6_scope_id =
199 ntohs(dst_address.sin6.sin6_addr.s6_addr16[1]);
200 dst_address.sin6.sin6_addr.s6_addr16[1] = 0;
205 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
207 IPSEC_ISTAT(sproto, nopf);
211 /* NB: only pass dst since key_allocsa follows RFC2401 */
212 sav = KEY_ALLOCSA(&dst_address, sproto, spi);
214 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
215 __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
216 (u_long) ntohl(spi), sproto));
217 IPSEC_ISTAT(sproto, notdb);
222 if (sav->tdb_xform == NULL) {
223 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
224 __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
225 (u_long) ntohl(spi), sproto));
226 IPSEC_ISTAT(sproto, noxform);
233 * Call appropriate transform and return -- callback takes care of
236 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
243 ah4_input(struct mbuf **mp, int *offp, int proto)
252 ipsec_common_input(m, off, offsetof(struct ip, ip_p),
253 AF_INET, IPPROTO_AH);
254 return (IPPROTO_DONE);
257 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
259 if (sa->sa_family == AF_INET &&
260 sa->sa_len == sizeof(struct sockaddr_in))
261 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
265 esp4_input(struct mbuf **mp, int *offp, int proto)
274 ipsec_common_input(m, off, offsetof(struct ip, ip_p),
275 AF_INET, IPPROTO_ESP);
276 return (IPPROTO_DONE);
280 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
282 if (sa->sa_family == AF_INET &&
283 sa->sa_len == sizeof(struct sockaddr_in))
284 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
288 ipcomp4_input(struct mbuf **mp, int *offp, int proto)
297 ipsec_common_input(m, off, offsetof(struct ip, ip_p),
298 AF_INET, IPPROTO_IPCOMP);
299 return (IPPROTO_DONE);
303 * IPsec input callback for INET protocols.
304 * This routine is called as the transform callback.
305 * Takes care of filtering and other sanity checks on
306 * the processed packet.
309 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
312 char buf[INET6_ADDRSTRLEN];
313 struct ipsec_ctx_data ctx;
314 int prot, af, sproto, isr_prot;
317 struct tdb_ident *tdbi;
318 struct secasindex *saidx;
322 char ip6buf[INET6_ADDRSTRLEN];
326 IPSEC_ASSERT(m != NULL, ("null mbuf"));
327 IPSEC_ASSERT(sav != NULL, ("null SA"));
328 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
329 saidx = &sav->sah->saidx;
330 af = saidx->dst.sa.sa_family;
331 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
332 sproto = saidx->proto;
333 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
334 sproto == IPPROTO_IPCOMP,
335 ("unexpected security protocol %u", sproto));
339 DPRINTF(("%s: null mbuf", __func__));
340 IPSEC_ISTAT(sproto, badkcr);
348 * XXXGL: do we need this entire block?
350 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
351 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
352 __func__, ipsec_address(&sav->sah->saidx.dst,
353 buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
354 IPSEC_ISTAT(sproto, hdrops);
359 ip = mtod(m, struct ip *);
360 ip->ip_len = htons(m->m_pkthdr.len);
362 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
364 ip = mtod(m, struct ip *);
368 IPSEC_INIT_CTX(&ctx, &m, sav, AF_INET, IPSEC_ENC_BEFORE);
369 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
371 ip = mtod(m, struct ip *);
373 /* IP-in-IP encapsulation */
374 if (prot == IPPROTO_IPIP &&
375 saidx->mode != IPSEC_MODE_TRANSPORT) {
377 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
378 IPSEC_ISTAT(sproto, hdrops);
382 /* enc0: strip outer IPv4 header */
383 m_striphdr(m, 0, ip->ip_hl << 2);
386 /* XXX PROXY address isn't recorded in SAH */
388 * Check that the inner source address is the same as
389 * the proxy address, if available.
391 if ((saidx->proxy.sa.sa_family == AF_INET &&
392 saidx->proxy.sin.sin_addr.s_addr !=
395 saidx->proxy.sin.sin_addr.s_addr) ||
396 (saidx->proxy.sa.sa_family != AF_INET &&
397 saidx->proxy.sa.sa_family != 0)) {
399 DPRINTF(("%s: inner source address %s doesn't "
400 "correspond to expected proxy source %s, "
401 "SA %s/%08lx\n", __func__,
402 inet_ntoa4(ipn.ip_src),
403 ipsp_address(saidx->proxy),
404 ipsp_address(saidx->dst),
405 (u_long) ntohl(sav->spi)));
407 IPSEC_ISTAT(sproto, pdrops);
414 /* IPv6-in-IP encapsulation. */
415 else if (prot == IPPROTO_IPV6 &&
416 saidx->mode != IPSEC_MODE_TRANSPORT) {
418 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
419 IPSEC_ISTAT(sproto, hdrops);
423 /* enc0: strip IPv4 header, keep IPv6 header only */
424 m_striphdr(m, 0, ip->ip_hl << 2);
427 * Check that the inner source address is the same as
428 * the proxy address, if available.
430 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
431 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
432 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
433 &saidx->proxy.sin6.sin6_addr)) ||
434 (saidx->proxy.sa.sa_family != AF_INET6 &&
435 saidx->proxy.sa.sa_family != 0)) {
437 DPRINTF(("%s: inner source address %s doesn't "
438 "correspond to expected proxy source %s, "
439 "SA %s/%08lx\n", __func__,
440 ip6_sprintf(ip6buf, &ip6n.ip6_src),
441 ipsec_address(&saidx->proxy),
442 ipsec_address(&saidx->dst),
443 (u_long) ntohl(sav->spi)));
445 IPSEC_ISTAT(sproto, pdrops);
452 else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
454 * When mode is wildcard, inner protocol is IPv6 and
455 * we have no INET6 support - drop this packet a bit later.
456 * In other cases we assume transport mode and outer
457 * header was already stripped in xform_xxx_cb.
463 * Record what we've done to the packet (under what SA it was
466 if (sproto != IPPROTO_IPCOMP) {
467 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
468 sizeof(struct tdb_ident), M_NOWAIT);
470 DPRINTF(("%s: failed to get tag\n", __func__));
471 IPSEC_ISTAT(sproto, hdrops);
476 tdbi = (struct tdb_ident *)(mtag + 1);
477 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
478 tdbi->proto = sproto;
479 tdbi->spi = sav->spi;
480 /* Cache those two for enc(4) in xform_ipip. */
481 tdbi->alg_auth = sav->alg_auth;
482 tdbi->alg_enc = sav->alg_enc;
484 m_tag_prepend(m, mtag);
487 key_sa_recordxfer(sav, m); /* record data transfer */
490 * In transport mode requeue decrypted mbuf back to IPv4 protocol
491 * handler. This is necessary to correctly expose rcvif.
493 if (saidx->mode == IPSEC_MODE_TRANSPORT)
496 * Re-dispatch via software interrupt.
500 isr_prot = NETISR_IP;
505 isr_prot = NETISR_IPV6;
510 DPRINTF(("%s: cannot handle inner ip proto %d\n",
512 IPSEC_ISTAT(sproto, nopf);
513 error = EPFNOSUPPORT;
517 IPSEC_INIT_CTX(&ctx, &m, sav, af, IPSEC_ENC_AFTER);
518 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
520 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
522 IPSEC_ISTAT(sproto, qfull);
523 DPRINTF(("%s: queue full; proto %u packet dropped\n",
534 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
536 /* XXX nothing just yet */
541 /* IPv6 AH wrapper. */
543 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
549 if (*offp < sizeof(struct ip6_hdr)) {
550 DPRINTF(("%s: bad offset %u\n", __func__, *offp));
552 } else if (*offp == sizeof(struct ip6_hdr)) {
553 protoff = offsetof(struct ip6_hdr, ip6_nxt);
555 /* Chase down the header chain... */
556 protoff = sizeof(struct ip6_hdr);
560 m_copydata(*mp, protoff, sizeof(ip6e),
563 if (ip6e.ip6e_nxt == IPPROTO_AH)
564 l = (ip6e.ip6e_len + 2) << 2;
566 l = (ip6e.ip6e_len + 1) << 3;
567 IPSEC_ASSERT(l > 0, ("l went zero or negative"));
568 } while (protoff + l < *offp);
570 /* Malformed packet check */
571 if (protoff + l != *offp) {
572 DPRINTF(("%s: bad packet header chain, protoff %u, "
573 "l %u, off %u\n", __func__, protoff, l, *offp));
574 IPSEC_ISTAT(proto, hdrops);
579 protoff += offsetof(struct ip6_ext, ip6e_nxt);
581 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
586 * IPsec input callback, called by the transform callback. Takes care of
587 * filtering and other sanity checks on the processed packet.
590 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
593 char buf[INET6_ADDRSTRLEN];
594 struct ipsec_ctx_data ctx;
595 int prot, af, sproto;
598 struct tdb_ident *tdbi;
599 struct secasindex *saidx;
604 char ip6buf[INET6_ADDRSTRLEN];
607 IPSEC_ASSERT(m != NULL, ("null mbuf"));
608 IPSEC_ASSERT(sav != NULL, ("null SA"));
609 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
610 saidx = &sav->sah->saidx;
611 af = saidx->dst.sa.sa_family;
612 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
613 sproto = saidx->proto;
614 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
615 sproto == IPPROTO_IPCOMP,
616 ("unexpected security protocol %u", sproto));
620 DPRINTF(("%s: null mbuf", __func__));
621 IPSEC_ISTAT(sproto, badkcr);
626 /* Fix IPv6 header */
627 if (m->m_len < sizeof(struct ip6_hdr) &&
628 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
630 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
631 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
632 sizeof(buf)), (u_long) ntohl(sav->spi)));
634 IPSEC_ISTAT(sproto, hdrops);
639 ip6 = mtod(m, struct ip6_hdr *);
640 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
642 IPSEC_INIT_CTX(&ctx, &m, sav, af, IPSEC_ENC_BEFORE);
643 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
646 m_copydata(m, protoff, 1, &nxt8);
649 /* IPv6-in-IP encapsulation */
650 if (prot == IPPROTO_IPV6 &&
651 saidx->mode != IPSEC_MODE_TRANSPORT) {
652 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
653 IPSEC_ISTAT(sproto, hdrops);
657 /* ip6n will now contain the inner IPv6 header. */
658 m_striphdr(m, 0, skip);
662 * Check that the inner source address is the same as
663 * the proxy address, if available.
665 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
666 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
667 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
668 &saidx->proxy.sin6.sin6_addr)) ||
669 (saidx->proxy.sa.sa_family != AF_INET6 &&
670 saidx->proxy.sa.sa_family != 0)) {
672 DPRINTF(("%s: inner source address %s doesn't "
673 "correspond to expected proxy source %s, "
674 "SA %s/%08lx\n", __func__,
675 ip6_sprintf(ip6buf, &ip6n.ip6_src),
676 ipsec_address(&saidx->proxy),
677 ipsec_address(&saidx->dst),
678 (u_long) ntohl(sav->spi)));
680 IPSEC_ISTAT(sproto, pdrops);
687 /* IP-in-IP encapsulation */
688 else if (prot == IPPROTO_IPIP &&
689 saidx->mode != IPSEC_MODE_TRANSPORT) {
690 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
691 IPSEC_ISTAT(sproto, hdrops);
695 /* ipn will now contain the inner IPv4 header */
696 m_striphdr(m, 0, skip);
700 * Check that the inner source address is the same as
701 * the proxy address, if available.
703 if ((saidx->proxy.sa.sa_family == AF_INET &&
704 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
705 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
706 (saidx->proxy.sa.sa_family != AF_INET &&
707 saidx->proxy.sa.sa_family != 0)) {
709 DPRINTF(("%s: inner source address %s doesn't "
710 "correspond to expected proxy source %s, "
711 "SA %s/%08lx\n", __func__,
712 inet_ntoa4(ipn.ip_src),
713 ipsec_address(&saidx->proxy),
714 ipsec_address(&saidx->dst),
715 (u_long) ntohl(sav->spi)));
717 IPSEC_ISTAT(sproto, pdrops);
725 prot = IPPROTO_IPV6; /* for correct BPF processing */
729 * Record what we've done to the packet (under what SA it was
732 if (sproto != IPPROTO_IPCOMP) {
733 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
734 sizeof(struct tdb_ident), M_NOWAIT);
736 DPRINTF(("%s: failed to get tag\n", __func__));
737 IPSEC_ISTAT(sproto, hdrops);
742 tdbi = (struct tdb_ident *)(mtag + 1);
743 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
744 tdbi->proto = sproto;
745 tdbi->spi = sav->spi;
746 /* Cache those two for enc(4) in xform_ipip. */
747 tdbi->alg_auth = sav->alg_auth;
748 tdbi->alg_enc = sav->alg_enc;
750 m_tag_prepend(m, mtag);
753 key_sa_recordxfer(sav, m);
757 if (prot == IPPROTO_IPIP)
762 IPSEC_INIT_CTX(&ctx, &m, sav, af, IPSEC_ENC_AFTER);
763 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
767 * We stripped outer IPv6 header.
768 * Now we should requeue decrypted packet via netisr.
773 isr_prot = NETISR_IP;
777 isr_prot = NETISR_IPV6;
780 DPRINTF(("%s: cannot handle inner ip proto %d\n",
782 IPSEC_ISTAT(sproto, nopf);
783 error = EPFNOSUPPORT;
786 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
788 IPSEC_ISTAT(sproto, qfull);
789 DPRINTF(("%s: queue full; proto %u packet dropped\n",
795 * See the end of ip6_input for this logic.
796 * IPPROTO_IPV[46] case will be processed just like other ones
800 while (nxt != IPPROTO_DONE) {
801 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
802 IP6STAT_INC(ip6s_toomanyhdr);
808 * Protection against faulty packet - there should be
809 * more sanity checks in header chain processing.
811 if (m->m_pkthdr.len < skip) {
812 IP6STAT_INC(ip6s_tooshort);
813 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
818 * Enforce IPsec policy checking if we are seeing last header.
819 * note that we do not visit this with protocols with pcb layer
820 * code - like udp/tcp/raw ip.
822 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
823 ipsec6_in_reject(m, NULL)) {
827 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
837 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
839 struct ip6ctlparam *ip6cp = NULL;
840 struct mbuf *m = NULL;
844 if (sa->sa_family != AF_INET6 ||
845 sa->sa_len != sizeof(struct sockaddr_in6))
847 if ((unsigned)cmd >= PRC_NCMDS)
850 /* if the parameter is from icmp6, decode it. */
852 ip6cp = (struct ip6ctlparam *)d;
854 ip6 = ip6cp->ip6c_ip6;
855 off = ip6cp->ip6c_off;
859 off = 0; /* calm gcc */
864 struct ip6ctlparam ip6cp1;
867 * Notify the error to all possible sockets via pfctlinput2.
868 * Since the upper layer information (such as protocol type,
869 * source and destination ports) is embedded in the encrypted
870 * data and might have been cut, we can't directly call
871 * an upper layer ctlinput function. However, the pcbnotify
872 * function will consider source and destination addresses
873 * as well as the flow info value, and may be able to find
874 * some PCB that should be notified.
875 * Although pfctlinput2 will call esp6_ctlinput(), there is
876 * no possibility of an infinite loop of function calls,
877 * because we don't pass the inner IPv6 header.
879 bzero(&ip6cp1, sizeof(ip6cp1));
880 ip6cp1.ip6c_src = ip6cp->ip6c_src;
881 pfctlinput2(cmd, sa, (void *)&ip6cp1);
884 * Then go to special cases that need ESP header information.
885 * XXX: We assume that when ip6 is non NULL,
886 * M and OFF are valid.
889 if (cmd == PRC_MSGSIZE) {
890 struct secasvar *sav;
894 /* check header length before using m_copydata */
895 if (m->m_pkthdr.len < off + sizeof (struct esp))
897 m_copydata(m, off + offsetof(struct esp, esp_spi),
898 sizeof(u_int32_t), (caddr_t) &spi);
900 * Check to see if we have a valid SA corresponding to
901 * the address in the ICMP message payload.
903 sav = KEY_ALLOCSA((union sockaddr_union *)sa,
905 valid = (sav != NULL);
909 /* XXX Further validation? */
912 * Depending on whether the SA is "valid" and
913 * routing table size (mtudisc_{hi,lo}wat), we will:
914 * - recalcurate the new MTU and create the
915 * corresponding routing entry, or
916 * - ignore the MTU change notification.
918 icmp6_mtudisc_update(ip6cp, valid);
921 /* we normally notify any pcb here */