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);
198 /* We keep addresses in SADB without embedded scope id */
199 if (IN6_IS_SCOPE_LINKLOCAL(&dst_address.sin6.sin6_addr)) {
200 /* XXX: sa6_recoverscope() */
201 dst_address.sin6.sin6_scope_id =
202 ntohs(dst_address.sin6.sin6_addr.s6_addr16[1]);
203 dst_address.sin6.sin6_addr.s6_addr16[1] = 0;
208 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
210 IPSEC_ISTAT(sproto, nopf);
214 /* NB: only pass dst since key_allocsa follows RFC2401 */
215 sav = KEY_ALLOCSA(&dst_address, sproto, spi);
217 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
218 __func__, ipsec_address(&dst_address),
219 (u_long) ntohl(spi), sproto));
220 IPSEC_ISTAT(sproto, notdb);
225 if (sav->tdb_xform == NULL) {
226 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
227 __func__, ipsec_address(&dst_address),
228 (u_long) ntohl(spi), sproto));
229 IPSEC_ISTAT(sproto, noxform);
236 * Call appropriate transform and return -- callback takes care of
239 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
246 * Common input handler for IPv4 AH, ESP, and IPCOMP.
249 ipsec4_common_input(struct mbuf *m, ...)
255 off = va_arg(ap, int);
256 nxt = va_arg(ap, int);
259 return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
264 ah4_input(struct mbuf **mp, int *offp, int proto)
273 ipsec4_common_input(m, off, IPPROTO_AH);
274 return (IPPROTO_DONE);
277 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
279 if (sa->sa_family == AF_INET &&
280 sa->sa_len == sizeof(struct sockaddr_in))
281 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
285 esp4_input(struct mbuf **mp, int *offp, int proto)
294 ipsec4_common_input(m, off, IPPROTO_ESP);
295 return (IPPROTO_DONE);
299 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
301 if (sa->sa_family == AF_INET &&
302 sa->sa_len == sizeof(struct sockaddr_in))
303 ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
307 ipcomp4_input(struct mbuf **mp, int *offp, int proto)
316 ipsec4_common_input(m, off, IPPROTO_IPCOMP);
317 return (IPPROTO_DONE);
321 * IPsec input callback for INET protocols.
322 * This routine is called as the transform callback.
323 * Takes care of filtering and other sanity checks on
324 * the processed packet.
327 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
330 int prot, af, sproto, isr_prot;
333 struct tdb_ident *tdbi;
334 struct secasindex *saidx;
338 char ip6buf[INET6_ADDRSTRLEN];
342 IPSEC_ASSERT(m != NULL, ("null mbuf"));
343 IPSEC_ASSERT(sav != NULL, ("null SA"));
344 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
345 saidx = &sav->sah->saidx;
346 af = saidx->dst.sa.sa_family;
347 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
348 sproto = saidx->proto;
349 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
350 sproto == IPPROTO_IPCOMP,
351 ("unexpected security protocol %u", sproto));
355 DPRINTF(("%s: null mbuf", __func__));
356 IPSEC_ISTAT(sproto, badkcr);
364 * XXXGL: do we need this entire block?
366 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
367 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
368 __func__, ipsec_address(&sav->sah->saidx.dst),
369 (u_long) ntohl(sav->spi)));
370 IPSEC_ISTAT(sproto, hdrops);
375 ip = mtod(m, struct ip *);
376 ip->ip_len = htons(m->m_pkthdr.len);
378 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
380 ip = mtod(m, struct ip *);
385 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
386 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
388 /* Pass the mbuf to enc0 for bpf and pfil. */
389 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
390 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
394 /* IP-in-IP encapsulation */
395 if (prot == IPPROTO_IPIP &&
396 saidx->mode != IPSEC_MODE_TRANSPORT) {
398 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
399 IPSEC_ISTAT(sproto, hdrops);
403 /* enc0: strip outer IPv4 header */
404 m_striphdr(m, 0, ip->ip_hl << 2);
407 /* XXX PROXY address isn't recorded in SAH */
409 * Check that the inner source address is the same as
410 * the proxy address, if available.
412 if ((saidx->proxy.sa.sa_family == AF_INET &&
413 saidx->proxy.sin.sin_addr.s_addr !=
416 saidx->proxy.sin.sin_addr.s_addr) ||
417 (saidx->proxy.sa.sa_family != AF_INET &&
418 saidx->proxy.sa.sa_family != 0)) {
420 DPRINTF(("%s: inner source address %s doesn't "
421 "correspond to expected proxy source %s, "
422 "SA %s/%08lx\n", __func__,
423 inet_ntoa4(ipn.ip_src),
424 ipsp_address(saidx->proxy),
425 ipsp_address(saidx->dst),
426 (u_long) ntohl(sav->spi)));
428 IPSEC_ISTAT(sproto, pdrops);
435 /* IPv6-in-IP encapsulation. */
436 else if (prot == IPPROTO_IPV6 &&
437 saidx->mode != IPSEC_MODE_TRANSPORT) {
439 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
440 IPSEC_ISTAT(sproto, hdrops);
444 /* enc0: strip IPv4 header, keep IPv6 header only */
445 m_striphdr(m, 0, ip->ip_hl << 2);
448 * Check that the inner source address is the same as
449 * the proxy address, if available.
451 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
452 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
453 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
454 &saidx->proxy.sin6.sin6_addr)) ||
455 (saidx->proxy.sa.sa_family != AF_INET6 &&
456 saidx->proxy.sa.sa_family != 0)) {
458 DPRINTF(("%s: inner source address %s doesn't "
459 "correspond to expected proxy source %s, "
460 "SA %s/%08lx\n", __func__,
461 ip6_sprintf(ip6buf, &ip6n.ip6_src),
462 ipsec_address(&saidx->proxy),
463 ipsec_address(&saidx->dst),
464 (u_long) ntohl(sav->spi)));
466 IPSEC_ISTAT(sproto, pdrops);
473 else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
475 * When mode is wildcard, inner protocol is IPv6 and
476 * we have no INET6 support - drop this packet a bit later.
477 * In other cases we assume transport mode and outer
478 * header was already stripped in xform_xxx_cb.
484 * Record what we've done to the packet (under what SA it was
487 if (sproto != IPPROTO_IPCOMP) {
488 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
489 sizeof(struct tdb_ident), M_NOWAIT);
491 DPRINTF(("%s: failed to get tag\n", __func__));
492 IPSEC_ISTAT(sproto, hdrops);
497 tdbi = (struct tdb_ident *)(mtag + 1);
498 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
499 tdbi->proto = sproto;
500 tdbi->spi = sav->spi;
501 /* Cache those two for enc(4) in xform_ipip. */
502 tdbi->alg_auth = sav->alg_auth;
503 tdbi->alg_enc = sav->alg_enc;
505 m_tag_prepend(m, mtag);
508 key_sa_recordxfer(sav, m); /* record data transfer */
511 * In transport mode requeue decrypted mbuf back to IPv4 protocol
512 * handler. This is necessary to correctly expose rcvif.
514 if (saidx->mode == IPSEC_MODE_TRANSPORT)
518 * Pass the mbuf to enc0 for bpf and pfil.
520 if (prot == IPPROTO_IPIP)
521 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
523 if (prot == IPPROTO_IPV6)
524 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
527 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
532 * Re-dispatch via software interrupt.
537 isr_prot = NETISR_IP;
541 isr_prot = NETISR_IPV6;
545 DPRINTF(("%s: cannot handle inner ip proto %d\n",
547 IPSEC_ISTAT(sproto, nopf);
548 error = EPFNOSUPPORT;
552 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
554 IPSEC_ISTAT(sproto, qfull);
555 DPRINTF(("%s: queue full; proto %u packet dropped\n",
566 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
568 /* XXX nothing just yet */
573 /* IPv6 AH wrapper. */
575 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
581 if (*offp < sizeof(struct ip6_hdr)) {
582 DPRINTF(("%s: bad offset %u\n", __func__, *offp));
584 } else if (*offp == sizeof(struct ip6_hdr)) {
585 protoff = offsetof(struct ip6_hdr, ip6_nxt);
587 /* Chase down the header chain... */
588 protoff = sizeof(struct ip6_hdr);
592 m_copydata(*mp, protoff, sizeof(ip6e),
595 if (ip6e.ip6e_nxt == IPPROTO_AH)
596 l = (ip6e.ip6e_len + 2) << 2;
598 l = (ip6e.ip6e_len + 1) << 3;
599 IPSEC_ASSERT(l > 0, ("l went zero or negative"));
600 } while (protoff + l < *offp);
602 /* Malformed packet check */
603 if (protoff + l != *offp) {
604 DPRINTF(("%s: bad packet header chain, protoff %u, "
605 "l %u, off %u\n", __func__, protoff, l, *offp));
606 IPSEC_ISTAT(proto, hdrops);
611 protoff += offsetof(struct ip6_ext, ip6e_nxt);
613 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
618 * IPsec input callback, called by the transform callback. Takes care of
619 * filtering and other sanity checks on the processed packet.
622 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
625 int prot, af, sproto;
628 struct tdb_ident *tdbi;
629 struct secasindex *saidx;
634 char ip6buf[INET6_ADDRSTRLEN];
637 IPSEC_ASSERT(m != NULL, ("null mbuf"));
638 IPSEC_ASSERT(sav != NULL, ("null SA"));
639 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
640 saidx = &sav->sah->saidx;
641 af = saidx->dst.sa.sa_family;
642 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
643 sproto = saidx->proto;
644 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
645 sproto == IPPROTO_IPCOMP,
646 ("unexpected security protocol %u", sproto));
650 DPRINTF(("%s: null mbuf", __func__));
651 IPSEC_ISTAT(sproto, badkcr);
656 /* Fix IPv6 header */
657 if (m->m_len < sizeof(struct ip6_hdr) &&
658 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
660 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
661 __func__, ipsec_address(&sav->sah->saidx.dst),
662 (u_long) ntohl(sav->spi)));
664 IPSEC_ISTAT(sproto, hdrops);
669 ip6 = mtod(m, struct ip6_hdr *);
670 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
673 m_copydata(m, protoff, 1, &nxt8);
677 if_inc_counter(encif, IFCOUNTER_IPACKETS, 1);
678 if_inc_counter(encif, IFCOUNTER_IBYTES, m->m_pkthdr.len);
680 /* Pass the mbuf to enc0 for bpf and pfil. */
681 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
682 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
686 /* IPv6-in-IP encapsulation */
687 if (prot == IPPROTO_IPV6 &&
688 saidx->mode != IPSEC_MODE_TRANSPORT) {
689 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
690 IPSEC_ISTAT(sproto, hdrops);
694 /* ip6n will now contain the inner IPv6 header. */
695 m_striphdr(m, 0, skip);
699 * Check that the inner source address is the same as
700 * the proxy address, if available.
702 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
703 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
704 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
705 &saidx->proxy.sin6.sin6_addr)) ||
706 (saidx->proxy.sa.sa_family != AF_INET6 &&
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 ip6_sprintf(ip6buf, &ip6n.ip6_src),
713 ipsec_address(&saidx->proxy),
714 ipsec_address(&saidx->dst),
715 (u_long) ntohl(sav->spi)));
717 IPSEC_ISTAT(sproto, pdrops);
724 /* IP-in-IP encapsulation */
725 else if (prot == IPPROTO_IPIP &&
726 saidx->mode != IPSEC_MODE_TRANSPORT) {
727 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
728 IPSEC_ISTAT(sproto, hdrops);
732 /* ipn will now contain the inner IPv4 header */
733 m_striphdr(m, 0, skip);
737 * Check that the inner source address is the same as
738 * the proxy address, if available.
740 if ((saidx->proxy.sa.sa_family == AF_INET &&
741 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
742 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
743 (saidx->proxy.sa.sa_family != AF_INET &&
744 saidx->proxy.sa.sa_family != 0)) {
746 DPRINTF(("%s: inner source address %s doesn't "
747 "correspond to expected proxy source %s, "
748 "SA %s/%08lx\n", __func__,
749 inet_ntoa4(ipn.ip_src),
750 ipsec_address(&saidx->proxy),
751 ipsec_address(&saidx->dst),
752 (u_long) ntohl(sav->spi)));
754 IPSEC_ISTAT(sproto, pdrops);
762 prot = IPPROTO_IPV6; /* for correct BPF processing */
766 * Record what we've done to the packet (under what SA it was
769 if (sproto != IPPROTO_IPCOMP) {
770 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
771 sizeof(struct tdb_ident), M_NOWAIT);
773 DPRINTF(("%s: failed to get tag\n", __func__));
774 IPSEC_ISTAT(sproto, hdrops);
779 tdbi = (struct tdb_ident *)(mtag + 1);
780 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
781 tdbi->proto = sproto;
782 tdbi->spi = sav->spi;
783 /* Cache those two for enc(4) in xform_ipip. */
784 tdbi->alg_auth = sav->alg_auth;
785 tdbi->alg_enc = sav->alg_enc;
787 m_tag_prepend(m, mtag);
790 key_sa_recordxfer(sav, m);
794 * Pass the mbuf to enc0 for bpf and pfil.
797 if (prot == IPPROTO_IPIP)
798 ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_AFTER);
800 if (prot == IPPROTO_IPV6)
801 ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_AFTER);
803 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER)) != 0)
807 * See the end of ip6_input for this logic.
808 * IPPROTO_IPV[46] case will be processed just like other ones
812 while (nxt != IPPROTO_DONE) {
813 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
814 IP6STAT_INC(ip6s_toomanyhdr);
820 * Protection against faulty packet - there should be
821 * more sanity checks in header chain processing.
823 if (m->m_pkthdr.len < skip) {
824 IP6STAT_INC(ip6s_tooshort);
825 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
830 * Enforce IPsec policy checking if we are seeing last header.
831 * note that we do not visit this with protocols with pcb layer
832 * code - like udp/tcp/raw ip.
834 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
835 ipsec6_in_reject(m, NULL)) {
839 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
849 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
851 struct ip6ctlparam *ip6cp = NULL;
852 struct mbuf *m = NULL;
856 if (sa->sa_family != AF_INET6 ||
857 sa->sa_len != sizeof(struct sockaddr_in6))
859 if ((unsigned)cmd >= PRC_NCMDS)
862 /* if the parameter is from icmp6, decode it. */
864 ip6cp = (struct ip6ctlparam *)d;
866 ip6 = ip6cp->ip6c_ip6;
867 off = ip6cp->ip6c_off;
871 off = 0; /* calm gcc */
876 struct ip6ctlparam ip6cp1;
879 * Notify the error to all possible sockets via pfctlinput2.
880 * Since the upper layer information (such as protocol type,
881 * source and destination ports) is embedded in the encrypted
882 * data and might have been cut, we can't directly call
883 * an upper layer ctlinput function. However, the pcbnotify
884 * function will consider source and destination addresses
885 * as well as the flow info value, and may be able to find
886 * some PCB that should be notified.
887 * Although pfctlinput2 will call esp6_ctlinput(), there is
888 * no possibility of an infinite loop of function calls,
889 * because we don't pass the inner IPv6 header.
891 bzero(&ip6cp1, sizeof(ip6cp1));
892 ip6cp1.ip6c_src = ip6cp->ip6c_src;
893 pfctlinput2(cmd, sa, (void *)&ip6cp1);
896 * Then go to special cases that need ESP header information.
897 * XXX: We assume that when ip6 is non NULL,
898 * M and OFF are valid.
901 if (cmd == PRC_MSGSIZE) {
902 struct secasvar *sav;
906 /* check header length before using m_copydata */
907 if (m->m_pkthdr.len < off + sizeof (struct esp))
909 m_copydata(m, off + offsetof(struct esp, esp_spi),
910 sizeof(u_int32_t), (caddr_t) &spi);
912 * Check to see if we have a valid SA corresponding to
913 * the address in the ICMP message payload.
915 sav = KEY_ALLOCSA((union sockaddr_union *)sa,
917 valid = (sav != NULL);
921 /* XXX Further validation? */
924 * Depending on whether the SA is "valid" and
925 * routing table size (mtudisc_{hi,lo}wat), we will:
926 * - recalcurate the new MTU and create the
927 * corresponding routing entry, or
928 * - ignore the MTU change notification.
930 icmp6_mtudisc_update(ip6cp, valid);
933 /* we normally notify any pcb here */