2 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * IPsec output processing.
33 #include "opt_inet6.h"
34 #include "opt_ipsec.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/domain.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/errno.h>
44 #include <sys/syslog.h>
48 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip_ecn.h>
58 #include <netinet6/ip6_ecn.h>
61 #include <netinet/ip6.h>
63 #include <netinet6/ip6_var.h>
65 #include <netinet/in_pcb.h>
67 #include <netinet/icmp6.h>
70 #include <netipsec/ipsec.h>
72 #include <netipsec/ipsec6.h>
74 #include <netipsec/ah_var.h>
75 #include <netipsec/esp_var.h>
76 #include <netipsec/ipcomp_var.h>
78 #include <netipsec/xform.h>
80 #include <netipsec/key.h>
81 #include <netipsec/keydb.h>
82 #include <netipsec/key_debug.h>
84 #include <machine/in_cksum.h>
87 #include <netinet/udp.h>
91 #include <net/if_enc.h>
96 ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
98 struct tdb_ident *tdbi;
100 struct secasvar *sav;
101 struct secasindex *saidx;
104 IPSEC_ASSERT(m != NULL, ("null mbuf"));
105 IPSEC_ASSERT(isr != NULL, ("null ISR"));
107 IPSEC_ASSERT(sav != NULL, ("null SA"));
108 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
110 saidx = &sav->sah->saidx;
111 switch (saidx->dst.sa.sa_family) {
114 /* Fix the header length, for AH processing. */
115 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
120 /* Fix the header length, for AH processing. */
121 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
125 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
126 /* No jumbogram support. */
130 mtod(m, struct ip6_hdr *)->ip6_plen =
131 htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
135 DPRINTF(("%s: unknown protocol family %u\n", __func__,
136 saidx->dst.sa.sa_family));
142 * Add a record of what we've done or what needs to be done to the
145 mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
146 sizeof(struct tdb_ident), M_NOWAIT);
148 DPRINTF(("%s: could not get packet tag\n", __func__));
153 tdbi = (struct tdb_ident *)(mtag + 1);
154 tdbi->dst = saidx->dst;
155 tdbi->proto = saidx->proto;
156 tdbi->spi = sav->spi;
157 m_tag_prepend(m, mtag);
160 * If there's another (bundled) SA to apply, do so.
161 * Note that this puts a burden on the kernel stack size.
162 * If this is a problem we'll need to introduce a queue
163 * to set the packet on so we can unwind the stack before
164 * doing further processing.
167 V_ipsec4stat.ips_out_bundlesa++;
168 return ipsec4_process_packet(m, isr->next, 0, 0);
170 key_sa_recordxfer(sav, m); /* record data transfer */
173 * We're done with IPsec processing, transmit the packet using the
174 * appropriate network protocol (IP or IPv6). SPD lookup will be
175 * performed again there.
177 switch (saidx->dst.sa.sa_family) {
181 ip = mtod(m, struct ip *);
182 ip->ip_len = ntohs(ip->ip_len);
183 ip->ip_off = ntohs(ip->ip_off);
187 * If NAT-T is enabled, now that all IPsec processing is done
188 * insert UDP encapsulation header after IP header.
190 if (sav->natt_type) {
192 const int hlen = IP_VHL_HL(ip->ip_vhl);
194 const int hlen = (ip->ip_hl << 2);
200 size = sizeof(struct udphdr);
201 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
203 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
204 * draft-ietf-ipsec-udp-encaps-(00/)01.txt,
205 * ignoring possible AH mode
206 * non-IKE marker + non-ESP marker
207 * from draft-ietf-ipsec-udp-encaps-00.txt.
209 size += sizeof(u_int64_t);
211 mi = m_makespace(m, hlen, size, &off);
213 DPRINTF(("%s: m_makespace for udphdr failed\n",
219 udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
220 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
221 udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
224 KEY_PORTFROMSADDR(&sav->sah->saidx.src);
225 udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
227 udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
228 ip->ip_len = m->m_pkthdr.len;
229 ip->ip_p = IPPROTO_UDP;
231 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
232 *(u_int64_t *)(udp + 1) = 0;
234 #endif /* IPSEC_NAT_T */
236 return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
241 * We don't need massage, IPv6 header fields are always in
244 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
247 panic("ipsec_process_done");
253 static struct ipsecrequest *
256 struct ipsecrequest *isr,
258 struct secasindex *saidx,
262 #define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \
263 isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++)
264 struct secasvar *sav;
266 IPSECREQUEST_LOCK_ASSERT(isr);
268 IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
269 ("invalid address family %u", af));
272 * Craft SA index to search for proper SA. Note that
273 * we only fillin unspecified SA peers for transport
274 * mode; for tunnel mode they must already be filled in.
277 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
278 /* Fillin unspecified SA peers only for transport mode */
280 struct sockaddr_in *sin;
281 struct ip *ip = mtod(m, struct ip *);
283 if (saidx->src.sa.sa_len == 0) {
284 sin = &saidx->src.sin;
285 sin->sin_len = sizeof(*sin);
286 sin->sin_family = AF_INET;
287 sin->sin_port = IPSEC_PORT_ANY;
288 sin->sin_addr = ip->ip_src;
290 if (saidx->dst.sa.sa_len == 0) {
291 sin = &saidx->dst.sin;
292 sin->sin_len = sizeof(*sin);
293 sin->sin_family = AF_INET;
294 sin->sin_port = IPSEC_PORT_ANY;
295 sin->sin_addr = ip->ip_dst;
298 struct sockaddr_in6 *sin6;
299 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
301 if (saidx->src.sin6.sin6_len == 0) {
302 sin6 = (struct sockaddr_in6 *)&saidx->src;
303 sin6->sin6_len = sizeof(*sin6);
304 sin6->sin6_family = AF_INET6;
305 sin6->sin6_port = IPSEC_PORT_ANY;
306 sin6->sin6_addr = ip6->ip6_src;
307 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
308 /* fix scope id for comparing SPD */
309 sin6->sin6_addr.s6_addr16[1] = 0;
310 sin6->sin6_scope_id =
311 ntohs(ip6->ip6_src.s6_addr16[1]);
314 if (saidx->dst.sin6.sin6_len == 0) {
315 sin6 = (struct sockaddr_in6 *)&saidx->dst;
316 sin6->sin6_len = sizeof(*sin6);
317 sin6->sin6_family = AF_INET6;
318 sin6->sin6_port = IPSEC_PORT_ANY;
319 sin6->sin6_addr = ip6->ip6_dst;
320 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
321 /* fix scope id for comparing SPD */
322 sin6->sin6_addr.s6_addr16[1] = 0;
323 sin6->sin6_scope_id =
324 ntohs(ip6->ip6_dst.s6_addr16[1]);
331 * Lookup SA and validate it.
333 *error = key_checkrequest(isr, saidx);
336 * IPsec processing is required, but no SA found.
337 * I assume that key_acquire() had been called
338 * to get/establish the SA. Here I discard
339 * this packet because it is responsibility for
340 * upper layer to retransmit the packet.
342 V_ipsec4stat.ips_out_nosa++;
347 IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
348 ("no SA found, but required; level %u",
349 ipsec_get_reqlevel(isr)));
350 IPSECREQUEST_UNLOCK(isr);
353 * If isr is NULL, we found a 'use' policy w/o SA.
354 * Return w/o error and w/o isr so we can drop out
355 * and continue w/o IPsec processing.
359 IPSECREQUEST_LOCK(isr);
364 * Check system global policy controls.
366 if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
367 (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
368 (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
369 DPRINTF(("%s: IPsec outbound packet dropped due"
370 " to policy (check your sysctls)\n", __func__));
371 IPSEC_OSTAT(V_espstat.esps_pdrops, V_ahstat.ahs_pdrops,
372 V_ipcompstat.ipcomps_pdrops);
373 *error = EHOSTUNREACH;
378 * Sanity check the SA contents for the caller
379 * before they invoke the xform output method.
381 if (sav->tdb_xform == NULL) {
382 DPRINTF(("%s: no transform for SA\n", __func__));
383 IPSEC_OSTAT(V_espstat.esps_noxform, V_ahstat.ahs_noxform,
384 V_ipcompstat.ipcomps_noxform);
385 *error = EHOSTUNREACH;
390 IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
391 IPSECREQUEST_UNLOCK(isr);
398 * IPsec output logic for IPv4.
401 ipsec4_process_packet(
403 struct ipsecrequest *isr,
407 struct secasindex saidx;
408 struct secasvar *sav;
412 IPSEC_ASSERT(m != NULL, ("null mbuf"));
413 IPSEC_ASSERT(isr != NULL, ("null isr"));
415 IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */
417 isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
427 encif->if_opackets++;
428 encif->if_obytes += m->m_pkthdr.len;
430 /* pass the mbuf to enc0 for bpf processing */
431 ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
432 /* pass the mbuf to enc0 for packet filtering */
433 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
438 union sockaddr_union *dst = &sav->sah->saidx.dst;
442 * Collect IP_DF state from the outer header.
444 if (dst->sa.sa_family == AF_INET) {
445 if (m->m_len < sizeof (struct ip) &&
446 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
450 ip = mtod(m, struct ip *);
451 /* Honor system-wide control of how to handle IP_DF */
452 switch (V_ip4_ipsec_dfbit) {
453 case 0: /* clear in outer header */
454 case 1: /* set in outer header */
455 setdf = V_ip4_ipsec_dfbit;
457 default: /* propagate to outer header */
458 setdf = ntohs(ip->ip_off & IP_DF);
462 ip = NULL; /* keep compiler happy */
465 /* Do the appropriate encapsulation, if necessary */
466 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
467 dst->sa.sa_family != AF_INET || /* PF mismatch */
469 (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
470 sav->tdb_xform->xf_type == XF_IP4 || /* ditto */
472 (dst->sa.sa_family == AF_INET && /* Proxy */
473 dst->sin.sin_addr.s_addr != INADDR_ANY &&
474 dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
477 /* Fix IPv4 header checksum and length */
478 if (m->m_len < sizeof (struct ip) &&
479 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
483 ip = mtod(m, struct ip *);
484 ip->ip_len = htons(m->m_pkthdr.len);
487 if (ip->ip_vhl == IP_VHL_BORING)
488 ip->ip_sum = in_cksum_hdr(ip);
490 ip->ip_sum = in_cksum(m,
491 _IP_VHL_HL(ip->ip_vhl) << 2);
493 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
496 /* Encapsulate the packet */
497 error = ipip_output(m, isr, &mp, 0, 0);
498 if (mp == NULL && !error) {
499 /* Should never happen. */
500 DPRINTF(("%s: ipip_output returns no mbuf and "
501 "no error!", __func__));
506 /* XXX: Should never happen! */
509 m = NULL; /* ipip_output() already freed it */
514 * ipip_output clears IP_DF in the new header. If
515 * we need to propagate IP_DF from the outer header,
516 * then we have to do it here.
518 * XXX shouldn't assume what ipip_output does.
520 if (dst->sa.sa_family == AF_INET && setdf) {
521 if (m->m_len < sizeof (struct ip) &&
522 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
526 ip = mtod(m, struct ip *);
527 ip->ip_off = ntohs(ip->ip_off);
529 ip->ip_off = htons(ip->ip_off);
535 /* pass the mbuf to enc0 for bpf processing */
536 ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_AFTER);
537 /* pass the mbuf to enc0 for packet filtering */
538 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
543 * Dispatch to the appropriate IPsec transform logic. The
544 * packet will be returned for transmission after crypto
545 * processing, etc. are completed. For encapsulation we
546 * bypass this call because of the explicit call done above
547 * (necessary to deal with IP_DF handling for IPv4).
549 * NB: m & sav are ``passed to caller'' who's reponsible for
550 * for reclaiming their resources.
552 if (sav->tdb_xform->xf_type != XF_IP4) {
553 ip = mtod(m, struct ip *);
555 off = offsetof(struct ip, ip_p);
556 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
558 error = ipsec_process_done(m, isr);
560 IPSECREQUEST_UNLOCK(isr);
564 IPSECREQUEST_UNLOCK(isr);
573 * Chop IP6 header from the payload.
576 ipsec6_splithdr(struct mbuf *m)
582 IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr),
583 ("first mbuf too short, len %u", m->m_len));
584 ip6 = mtod(m, struct ip6_hdr *);
585 hlen = sizeof(struct ip6_hdr);
586 if (m->m_len > hlen) {
587 MGETHDR(mh, M_DONTWAIT, MT_DATA);
592 M_MOVE_PKTHDR(mh, m);
599 bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
600 } else if (m->m_len < hlen) {
601 m = m_pullup(m, hlen);
609 * IPsec output logic for IPv6, transport mode.
613 struct ipsec_output_state *state,
616 struct secpolicy *sp,
620 struct ipsecrequest *isr;
621 struct secasindex saidx;
625 IPSEC_ASSERT(state != NULL, ("null state"));
626 IPSEC_ASSERT(state->m != NULL, ("null m"));
627 IPSEC_ASSERT(nexthdrp != NULL, ("null nexthdrp"));
628 IPSEC_ASSERT(mprev != NULL, ("null mprev"));
629 IPSEC_ASSERT(sp != NULL, ("null sp"));
630 IPSEC_ASSERT(tun != NULL, ("null tun"));
632 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
633 printf("%s: applied SP\n", __func__);
634 kdebug_secpolicy(sp));
637 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
638 /* the rest will be handled by ipsec6_output_tunnel() */
639 *tun = 1; /* need tunnel-mode processing */
646 IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */
647 isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
651 /* XXX should notification be done for all errors ? */
653 * Notify the fact that the packet is discarded
654 * to ourselves. I believe this is better than
655 * just silently discarding. (jinmei@kame.net)
656 * XXX: should we restrict the error to TCP packets?
657 * XXX: should we directly notify sockets via
660 icmp6_error(m, ICMP6_DST_UNREACH,
661 ICMP6_DST_UNREACH_ADMIN, 0);
662 m = NULL; /* NB: icmp6_error frees mbuf */
669 error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
670 sizeof (struct ip6_hdr),
671 offsetof(struct ip6_hdr,
673 IPSECREQUEST_UNLOCK(isr);
677 IPSECREQUEST_UNLOCK(isr);
685 ipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
687 struct ip6_hdr *oip6;
691 /* can't tunnel between different AFs */
692 if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
693 sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
697 IPSEC_ASSERT(m->m_len == sizeof (struct ip6_hdr),
698 ("mbuf wrong size; len %u", m->m_len));
702 * grow the mbuf to accomodate the new IPv6 header.
704 plen = m->m_pkthdr.len;
705 if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
707 MGET(n, M_DONTWAIT, MT_DATA);
712 n->m_len = sizeof(struct ip6_hdr);
713 n->m_next = m->m_next;
715 m->m_pkthdr.len += sizeof(struct ip6_hdr);
716 oip6 = mtod(n, struct ip6_hdr *);
718 m->m_next->m_len += sizeof(struct ip6_hdr);
719 m->m_next->m_data -= sizeof(struct ip6_hdr);
720 m->m_pkthdr.len += sizeof(struct ip6_hdr);
721 oip6 = mtod(m->m_next, struct ip6_hdr *);
723 ip6 = mtod(m, struct ip6_hdr *);
724 bcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
726 /* Fake link-local scope-class addresses */
727 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
728 oip6->ip6_src.s6_addr16[1] = 0;
729 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
730 oip6->ip6_dst.s6_addr16[1] = 0;
732 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
733 /* ECN consideration. */
734 ip6_ecn_ingress(V_ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
735 if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
736 ip6->ip6_plen = htons(plen);
738 /* ip6->ip6_plen will be updated in ip6_output() */
740 ip6->ip6_nxt = IPPROTO_IPV6;
741 ip6->ip6_src = sav->sah->saidx.src.sin6.sin6_addr;
742 ip6->ip6_dst = sav->sah->saidx.dst.sin6.sin6_addr;
743 ip6->ip6_hlim = IPV6_DEFHLIM;
745 /* XXX Should ip6_src be updated later ? */
751 * IPsec output logic for IPv6, tunnel mode.
754 ipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
757 struct ipsecrequest *isr;
758 struct secasindex saidx;
760 struct sockaddr_in6 *dst6;
763 IPSEC_ASSERT(state != NULL, ("null state"));
764 IPSEC_ASSERT(state->m != NULL, ("null m"));
765 IPSEC_ASSERT(sp != NULL, ("null sp"));
767 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
768 printf("%s: applied SP\n", __func__);
769 kdebug_secpolicy(sp));
773 * transport mode ipsec (before the 1st tunnel mode) is already
774 * processed by ipsec6_output_trans().
776 for (isr = sp->req; isr; isr = isr->next) {
777 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
781 IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */
782 isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
790 encif->if_opackets++;
791 encif->if_obytes += m->m_pkthdr.len;
793 /* pass the mbuf to enc0 for bpf processing */
794 ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
795 /* pass the mbuf to enc0 for packet filtering */
796 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
801 * There may be the case that SA status will be changed when
802 * we are refering to one. So calling splsoftnet().
804 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
806 * build IPsec tunnel.
808 /* XXX should be processed with other familiy */
809 if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
810 ipseclog((LOG_ERR, "%s: family mismatched between "
811 "inner and outer, spi=%u\n", __func__,
812 ntohl(isr->sav->spi)));
813 V_ipsec6stat.ips_out_inval++;
814 error = EAFNOSUPPORT;
818 m = ipsec6_splithdr(m);
820 V_ipsec6stat.ips_out_nomem++;
824 error = ipsec6_encapsulate(m, isr->sav);
829 ip6 = mtod(m, struct ip6_hdr *);
832 (struct route *)&isr->sav->sah->route_cache.sin6_route;
833 state->dst = (struct sockaddr *)&state->ro->ro_dst;
834 dst6 = (struct sockaddr_in6 *)state->dst;
836 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
837 || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
838 RTFREE(state->ro->ro_rt);
839 state->ro->ro_rt = NULL;
841 if (state->ro->ro_rt == NULL) {
842 bzero(dst6, sizeof(*dst6));
843 dst6->sin6_family = AF_INET6;
844 dst6->sin6_len = sizeof(*dst6);
845 dst6->sin6_addr = ip6->ip6_dst;
846 rtalloc_ign_fib(state->ro, 0UL, M_GETFIB(m));
848 if (state->ro->ro_rt == NULL) {
849 V_ip6stat.ip6s_noroute++;
850 V_ipsec6stat.ips_out_noroute++;
851 error = EHOSTUNREACH;
855 /* adjust state->dst if tunnel endpoint is offlink */
856 if (state->ro->ro_rt->rt_flags & RTF_GATEWAY)
857 state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
860 m = ipsec6_splithdr(m);
862 V_ipsec6stat.ips_out_nomem++;
866 ip6 = mtod(m, struct ip6_hdr *);
869 /* pass the mbuf to enc0 for bpf processing */
870 ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_AFTER);
871 /* pass the mbuf to enc0 for packet filtering */
872 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
876 error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
877 sizeof (struct ip6_hdr),
878 offsetof(struct ip6_hdr, ip6_nxt));
879 IPSECREQUEST_UNLOCK(isr);
883 IPSECREQUEST_UNLOCK(isr);