2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
5 * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * IPsec output processing.
36 #include "opt_inet6.h"
37 #include "opt_ipsec.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/errno.h>
47 #include <sys/hhook.h>
48 #include <sys/syslog.h>
51 #include <net/if_enc.h>
52 #include <net/if_var.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_var.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip_ecn.h>
62 #include <netinet6/ip6_ecn.h>
64 #include <netinet/ip_icmp.h>
65 #include <netinet/tcp_var.h>
67 #include <netinet/ip6.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/scope6_var.h>
72 #include <netinet/in_pcb.h>
74 #include <netinet/icmp6.h>
76 #if defined(SCTP) || defined(SCTP_SUPPORT)
77 #include <netinet/sctp_crc32.h>
80 #include <netinet/udp.h>
81 #include <netipsec/ah.h>
82 #include <netipsec/esp.h>
83 #include <netipsec/ipsec.h>
85 #include <netipsec/ipsec6.h>
87 #include <netipsec/ah_var.h>
88 #include <netipsec/esp_var.h>
89 #include <netipsec/ipcomp_var.h>
91 #include <netipsec/xform.h>
93 #include <netipsec/key.h>
94 #include <netipsec/keydb.h>
95 #include <netipsec/key_debug.h>
97 #include <machine/in_cksum.h>
99 #define IPSEC_OSTAT_INC(proto, name) do { \
100 if ((proto) == IPPROTO_ESP) \
101 ESPSTAT_INC(esps_##name); \
102 else if ((proto) == IPPROTO_AH)\
103 AHSTAT_INC(ahs_##name); \
105 IPCOMPSTAT_INC(ipcomps_##name); \
108 static int ipsec_encap(struct mbuf **mp, struct secasindex *saidx);
111 static struct secasvar *
112 ipsec4_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
114 struct secasindex *saidx, tmpsaidx;
115 struct ipsecrequest *isr;
116 struct sockaddr_in *sin;
117 struct secasvar *sav;
121 * Check system global policy controls.
124 isr = sp->req[*pidx];
125 if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
126 (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
127 (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
128 DPRINTF(("%s: IPsec outbound packet dropped due"
129 " to policy (check your sysctls)\n", __func__));
130 IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
131 *error = EHOSTUNREACH;
135 * Craft SA index to search for proper SA. Note that
136 * we only initialize unspecified SA peers for transport
137 * mode; for tunnel mode they must already be filled in.
139 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
142 ip = mtod(m, struct ip *);
143 if (saidx->src.sa.sa_len == 0) {
144 sin = &saidx->src.sin;
145 sin->sin_len = sizeof(*sin);
146 sin->sin_family = AF_INET;
147 sin->sin_port = IPSEC_PORT_ANY;
148 sin->sin_addr = ip->ip_src;
150 if (saidx->dst.sa.sa_len == 0) {
151 sin = &saidx->dst.sin;
152 sin->sin_len = sizeof(*sin);
153 sin->sin_family = AF_INET;
154 sin->sin_port = IPSEC_PORT_ANY;
155 sin->sin_addr = ip->ip_dst;
158 saidx = &sp->req[*pidx]->saidx;
160 * Lookup SA and validate it.
162 sav = key_allocsa_policy(sp, saidx, error);
164 IPSECSTAT_INC(ips_out_nosa);
167 if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
169 * We have no SA and policy that doesn't require
170 * this IPsec transform, thus we can continue w/o
171 * IPsec processing, i.e. return EJUSTRETURN.
172 * But first check if there is some bundled transform.
174 if (sp->tcount > ++(*pidx))
176 *error = EJUSTRETURN;
180 IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
185 * IPsec output logic for IPv4.
188 ipsec4_perform_request(struct mbuf *m, struct secpolicy *sp,
189 struct inpcb *inp, u_int idx)
191 struct ipsec_ctx_data ctx;
192 union sockaddr_union *dst;
193 struct secasvar *sav;
197 IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
200 * We hold the reference to SP. Content of SP couldn't be changed.
201 * Craft secasindex and do lookup for suitable SA.
202 * Then do encapsulation if needed and call xform's output.
203 * We need to store SP in the xform callback parameters.
204 * In xform callback we will extract SP and it can be used to
205 * determine next transform. At the end of transform we can
206 * release reference to SP.
208 sav = ipsec4_allocsa(m, sp, &idx, &error);
210 if (error == EJUSTRETURN) { /* No IPsec required */
217 * XXXAE: most likely ip_sum at this point is wrong.
219 IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET, IPSEC_ENC_BEFORE);
220 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
223 ip = mtod(m, struct ip *);
224 dst = &sav->sah->saidx.dst;
225 /* Do the appropriate encapsulation, if necessary */
226 if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
227 dst->sa.sa_family != AF_INET || /* PF mismatch */
228 (dst->sa.sa_family == AF_INET && /* Proxy */
229 dst->sin.sin_addr.s_addr != INADDR_ANY &&
230 dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
231 /* Fix IPv4 header checksum and length */
232 ip->ip_len = htons(m->m_pkthdr.len);
234 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
235 error = ipsec_encap(&m, &sav->sah->saidx);
237 DPRINTF(("%s: encapsulation for SPI 0x%08x failed "
238 "with error %d\n", __func__, ntohl(sav->spi),
240 /* XXXAE: IPSEC_OSTAT_INC(tunnel); */
246 IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
247 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
251 * Dispatch to the appropriate IPsec transform logic. The
252 * packet will be returned for transmission after crypto
253 * processing, etc. are completed.
255 * NB: m & sav are ``passed to caller'' who's responsible for
256 * reclaiming their resources.
258 switch(dst->sa.sa_family) {
260 ip = mtod(m, struct ip *);
262 off = offsetof(struct ip, ip_p);
266 i = sizeof(struct ip6_hdr);
267 off = offsetof(struct ip6_hdr, ip6_nxt);
271 DPRINTF(("%s: unsupported protocol family %u\n",
272 __func__, dst->sa.sa_family));
273 error = EPFNOSUPPORT;
274 IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
277 error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
280 IPSECSTAT_INC(ips_out_inval);
290 ipsec4_process_packet(struct mbuf *m, struct secpolicy *sp,
294 return (ipsec4_perform_request(m, sp, inp, 0));
298 ipsec4_check_pmtu(struct mbuf *m, struct secpolicy *sp, int forwarding)
300 union sockaddr_union *dst;
301 struct in_conninfo inc;
302 struct secasvar *sav;
308 /* Don't check PMTU if the frame won't have DF bit set. */
309 if (!V_ip4_ipsec_dfbit)
311 if (V_ip4_ipsec_dfbit == 1)
314 /* V_ip4_ipsec_dfbit > 1 - we will copy it from inner header. */
315 ip = mtod(m, struct ip *);
316 if (!(ip->ip_off & htons(IP_DF)))
320 idx = sp->tcount - 1;
321 sav = ipsec4_allocsa(m, sp, &idx, &error);
325 * No matching SA was found and SADB_ACQUIRE message was generated.
326 * Since we have matched a SP to this packet drop it silently.
330 if (error != EJUSTRETURN)
336 dst = &sav->sah->saidx.dst;
337 memset(&inc, 0, sizeof(inc));
338 switch (dst->sa.sa_family) {
340 inc.inc_faddr = satosin(&dst->sa)->sin_addr;
344 inc.inc6_faddr = satosin6(&dst->sa)->sin6_addr;
345 inc.inc_flags |= INC_ISIPV6;
354 pmtu = tcp_hc_getmtu(&inc);
355 /* No entry in hostcache. */
359 hlen = ipsec_hdrsiz_internal(sp);
360 if (m_length(m, NULL) + hlen > pmtu) {
362 * If we're forwarding generate ICMP message here,
363 * so that it contains pmtu and not link mtu.
364 * Set error to EINPROGRESS, in order for the frame
365 * to be dropped silently.
369 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
375 return (EINPROGRESS); /* Pretend that we consumed it. */
387 ipsec4_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
389 struct secpolicy *sp;
392 /* Lookup for the corresponding outbound security policy */
393 sp = ipsec4_checkpolicy(m, inp, &error, !forwarding);
395 if (error == -EINVAL) {
396 /* Discarded by policy. */
400 return (0); /* No IPsec required. */
404 * Usually we have to have tunnel mode IPsec security policy
405 * when we are forwarding a packet. Otherwise we could not handle
406 * encrypted replies, because they are not destined for us. But
407 * some users are doing source address translation for forwarded
408 * packets, and thus, even if they are forwarded, the replies will
413 * Do delayed checksums now because we send before
414 * this is done in the normal processing path.
416 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
417 m = mb_unmapped_to_ext(m);
419 IPSECSTAT_INC(ips_out_nomem);
424 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
426 #if defined(SCTP) || defined(SCTP_SUPPORT)
427 if (m->m_pkthdr.csum_flags & CSUM_SCTP) {
430 m = mb_unmapped_to_ext(m);
432 IPSECSTAT_INC(ips_out_nomem);
436 ip = mtod(m, struct ip *);
437 sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
438 m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
442 /* NB: callee frees mbuf and releases reference to SP */
443 error = ipsec4_check_pmtu(m, sp, forwarding);
445 if (error == EJUSTRETURN)
451 error = ipsec4_process_packet(m, sp, inp);
452 if (error == EJUSTRETURN) {
454 * We had a SP with a level of 'use' and no SA. We
455 * will just continue to process the packet without
456 * IPsec processing and return without error.
461 return (EINPROGRESS); /* consumed by IPsec */
466 * IPSEC_OUTPUT() method implementation for IPv4.
467 * 0 - no IPsec handling needed
468 * other values - mbuf consumed by IPsec.
471 ipsec4_output(struct mbuf *m, struct inpcb *inp)
475 * If the packet is resubmitted to ip_output (e.g. after
476 * AH, ESP, etc. processing), there will be a tag to bypass
477 * the lookup and related policy checking.
479 if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
482 return (ipsec4_common_output(m, inp, 0));
486 * IPSEC_FORWARD() method implementation for IPv4.
487 * 0 - no IPsec handling needed
488 * other values - mbuf consumed by IPsec.
491 ipsec4_forward(struct mbuf *m)
495 * Check if this packet has an active inbound SP and needs to be
496 * dropped instead of forwarded.
498 if (ipsec4_in_reject(m, NULL) != 0) {
502 return (ipsec4_common_output(m, NULL, 1));
508 in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa,
509 const struct in6_addr *ia)
513 if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) {
514 memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
515 ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
516 return (IN6_ARE_ADDR_EQUAL(ia, &ia2));
518 return (IN6_ARE_ADDR_EQUAL(&sa->sin6_addr, ia));
521 static struct secasvar *
522 ipsec6_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
524 struct secasindex *saidx, tmpsaidx;
525 struct ipsecrequest *isr;
526 struct sockaddr_in6 *sin6;
527 struct secasvar *sav;
531 * Check system global policy controls.
534 isr = sp->req[*pidx];
535 if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
536 (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
537 (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
538 DPRINTF(("%s: IPsec outbound packet dropped due"
539 " to policy (check your sysctls)\n", __func__));
540 IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
541 *error = EHOSTUNREACH;
545 * Craft SA index to search for proper SA. Note that
546 * we only fillin unspecified SA peers for transport
547 * mode; for tunnel mode they must already be filled in.
549 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
552 ip6 = mtod(m, struct ip6_hdr *);
553 if (saidx->src.sin6.sin6_len == 0) {
554 sin6 = (struct sockaddr_in6 *)&saidx->src;
555 sin6->sin6_len = sizeof(*sin6);
556 sin6->sin6_family = AF_INET6;
557 sin6->sin6_port = IPSEC_PORT_ANY;
558 sin6->sin6_addr = ip6->ip6_src;
559 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
560 /* fix scope id for comparing SPD */
561 sin6->sin6_addr.s6_addr16[1] = 0;
562 sin6->sin6_scope_id =
563 ntohs(ip6->ip6_src.s6_addr16[1]);
566 if (saidx->dst.sin6.sin6_len == 0) {
567 sin6 = (struct sockaddr_in6 *)&saidx->dst;
568 sin6->sin6_len = sizeof(*sin6);
569 sin6->sin6_family = AF_INET6;
570 sin6->sin6_port = IPSEC_PORT_ANY;
571 sin6->sin6_addr = ip6->ip6_dst;
572 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
573 /* fix scope id for comparing SPD */
574 sin6->sin6_addr.s6_addr16[1] = 0;
575 sin6->sin6_scope_id =
576 ntohs(ip6->ip6_dst.s6_addr16[1]);
580 saidx = &sp->req[*pidx]->saidx;
582 * Lookup SA and validate it.
584 sav = key_allocsa_policy(sp, saidx, error);
586 IPSEC6STAT_INC(ips_out_nosa);
589 if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
591 * We have no SA and policy that doesn't require
592 * this IPsec transform, thus we can continue w/o
593 * IPsec processing, i.e. return EJUSTRETURN.
594 * But first check if there is some bundled transform.
596 if (sp->tcount > ++(*pidx))
598 *error = EJUSTRETURN;
602 IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
607 * IPsec output logic for IPv6.
610 ipsec6_perform_request(struct mbuf *m, struct secpolicy *sp,
611 struct inpcb *inp, u_int idx)
613 struct ipsec_ctx_data ctx;
614 union sockaddr_union *dst;
615 struct secasvar *sav;
619 IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
621 sav = ipsec6_allocsa(m, sp, &idx, &error);
623 if (error == EJUSTRETURN) { /* No IPsec required */
630 /* Fix IP length in case if it is not set yet. */
631 ip6 = mtod(m, struct ip6_hdr *);
632 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
634 IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET6, IPSEC_ENC_BEFORE);
635 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
638 ip6 = mtod(m, struct ip6_hdr *); /* pfil can change mbuf */
639 dst = &sav->sah->saidx.dst;
641 /* Do the appropriate encapsulation, if necessary */
642 if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
643 dst->sa.sa_family != AF_INET6 || /* PF mismatch */
644 ((dst->sa.sa_family == AF_INET6) &&
645 (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
646 (!in6_sa_equal_addrwithscope(&dst->sin6, &ip6->ip6_dst)))) {
647 if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
648 /* No jumbogram support. */
649 error = ENXIO; /*XXX*/
652 error = ipsec_encap(&m, &sav->sah->saidx);
654 DPRINTF(("%s: encapsulation for SPI 0x%08x failed "
655 "with error %d\n", __func__, ntohl(sav->spi),
657 /* XXXAE: IPSEC_OSTAT_INC(tunnel); */
663 IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
664 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
667 switch(dst->sa.sa_family) {
672 ip = mtod(m, struct ip *);
674 off = offsetof(struct ip, ip_p);
679 i = sizeof(struct ip6_hdr);
680 off = offsetof(struct ip6_hdr, ip6_nxt);
683 DPRINTF(("%s: unsupported protocol family %u\n",
684 __func__, dst->sa.sa_family));
685 error = EPFNOSUPPORT;
686 IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
689 error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
692 IPSEC6STAT_INC(ips_out_inval);
702 ipsec6_process_packet(struct mbuf *m, struct secpolicy *sp,
706 return (ipsec6_perform_request(m, sp, inp, 0));
710 ipsec6_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
712 struct secpolicy *sp;
715 /* Lookup for the corresponding outbound security policy */
716 sp = ipsec6_checkpolicy(m, inp, &error, !forwarding);
718 if (error == -EINVAL) {
719 /* Discarded by policy. */
723 return (0); /* No IPsec required. */
728 * Do delayed checksums now because we send before
729 * this is done in the normal processing path.
731 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
732 m = mb_unmapped_to_ext(m);
734 IPSEC6STAT_INC(ips_out_nomem);
738 in6_delayed_cksum(m, m->m_pkthdr.len -
739 sizeof(struct ip6_hdr), sizeof(struct ip6_hdr));
740 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
742 #if defined(SCTP) || defined(SCTP_SUPPORT)
743 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
744 m = mb_unmapped_to_ext(m);
746 IPSEC6STAT_INC(ips_out_nomem);
750 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
751 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
755 /* NB: callee frees mbuf and releases reference to SP */
756 error = ipsec6_process_packet(m, sp, inp);
757 if (error == EJUSTRETURN) {
759 * We had a SP with a level of 'use' and no SA. We
760 * will just continue to process the packet without
761 * IPsec processing and return without error.
766 return (EINPROGRESS); /* consumed by IPsec */
771 * IPSEC_OUTPUT() method implementation for IPv6.
772 * 0 - no IPsec handling needed
773 * other values - mbuf consumed by IPsec.
776 ipsec6_output(struct mbuf *m, struct inpcb *inp)
780 * If the packet is resubmitted to ip_output (e.g. after
781 * AH, ESP, etc. processing), there will be a tag to bypass
782 * the lookup and related policy checking.
784 if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
787 return (ipsec6_common_output(m, inp, 0));
791 * IPSEC_FORWARD() method implementation for IPv6.
792 * 0 - no IPsec handling needed
793 * other values - mbuf consumed by IPsec.
796 ipsec6_forward(struct mbuf *m)
800 * Check if this packet has an active inbound SP and needs to be
801 * dropped instead of forwarded.
803 if (ipsec6_in_reject(m, NULL) != 0) {
807 return (ipsec6_common_output(m, NULL, 1));
812 ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
815 struct epoch_tracker et;
816 struct xform_history *xh;
817 struct secasindex *saidx;
821 saidx = &sav->sah->saidx;
822 switch (saidx->dst.sa.sa_family) {
825 /* Fix the header length, for AH processing. */
826 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
831 /* Fix the header length, for AH processing. */
832 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
836 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
837 /* No jumbogram support. */
841 mtod(m, struct ip6_hdr *)->ip6_plen =
842 htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
846 DPRINTF(("%s: unknown protocol family %u\n", __func__,
847 saidx->dst.sa.sa_family));
853 * Add a record of what we've done to the packet.
855 mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, sizeof(*xh), M_NOWAIT);
857 DPRINTF(("%s: could not get packet tag\n", __func__));
862 xh = (struct xform_history *)(mtag + 1);
863 xh->dst = saidx->dst;
864 xh->proto = saidx->proto;
865 xh->mode = saidx->mode;
867 m_tag_prepend(m, mtag);
869 key_sa_recordxfer(sav, m); /* record data transfer */
872 * If there's another (bundled) SA to apply, do so.
873 * Note that this puts a burden on the kernel stack size.
874 * If this is a problem we'll need to introduce a queue
875 * to set the packet on so we can unwind the stack before
876 * doing further processing.
878 if (++idx < sp->tcount) {
879 switch (saidx->dst.sa.sa_family) {
883 IPSECSTAT_INC(ips_out_bundlesa);
884 return (ipsec4_perform_request(m, sp, NULL, idx));
890 IPSEC6STAT_INC(ips_out_bundlesa);
891 return (ipsec6_perform_request(m, sp, NULL, idx));
895 DPRINTF(("%s: unknown protocol family %u\n", __func__,
896 saidx->dst.sa.sa_family));
897 error = EPFNOSUPPORT;
902 key_freesp(&sp), sp = NULL; /* Release reference to SP */
905 * Do UDP encapsulation if SA requires it.
907 if (sav->natt != NULL) {
908 error = udp_ipsec_output(m, sav);
914 * We're done with IPsec processing, transmit the packet using the
915 * appropriate network protocol (IP or IPv6).
918 switch (saidx->dst.sa.sa_family) {
922 error = ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
928 error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
932 panic("ipsec_process_done");
945 * ipsec_prepend() is optimized version of M_PREPEND().
946 * ipsec_encap() is called by IPsec output routine for tunnel mode SA.
947 * It is expected that after IP encapsulation some IPsec transform will
948 * be performed. Each IPsec transform inserts its variable length header
949 * just after outer IP header using m_makespace(). If given mbuf has not
950 * enough free space at the beginning, we allocate new mbuf and reserve
951 * some space at the beginning and at the end.
952 * This helps avoid allocating of new mbuf and data copying in m_makespace(),
953 * we place outer header in the middle of mbuf's data with reserved leading
954 * and trailing space:
955 * [ LEADINGSPACE ][ Outer IP header ][ TRAILINGSPACE ]
956 * LEADINGSPACE will be used to add ethernet header, TRAILINGSPACE will
957 * be used to inject AH/ESP/IPCOMP header.
959 #define IPSEC_TRAILINGSPACE (sizeof(struct udphdr) +/* NAT-T */ \
960 max(sizeof(struct newesp) + EALG_MAX_BLOCK_LEN, /* ESP + IV */ \
961 sizeof(struct newah) + HASH_MAX_LEN /* AH + ICV */))
963 ipsec_prepend(struct mbuf *m, int len, int how)
968 IPSEC_ASSERT(len < MHLEN, ("wrong length"));
969 if (M_LEADINGSPACE(m) >= len) {
970 /* No need to allocate new mbuf. */
973 m->m_pkthdr.len += len;
976 n = m_gethdr(how, m->m_type);
983 if (len + IPSEC_TRAILINGSPACE < M_SIZE(n))
984 m_align(n, len + IPSEC_TRAILINGSPACE);
986 n->m_pkthdr.len += len;
991 ipsec_encap(struct mbuf **mp, struct secasindex *saidx)
1000 ip = mtod(*mp, struct ip *);
1004 proto = IPPROTO_IPIP;
1006 * Collect IP_DF state from the inner header
1007 * and honor system-wide control of how to handle it.
1009 switch (V_ip4_ipsec_dfbit) {
1010 case 0: /* clear in outer header */
1011 case 1: /* set in outer header */
1012 setdf = V_ip4_ipsec_dfbit;
1014 default:/* propagate to outer header */
1015 setdf = (ip->ip_off & htons(IP_DF)) != 0;
1021 case (IPV6_VERSION >> 4):
1022 proto = IPPROTO_IPV6;
1023 ip6 = mtod(*mp, struct ip6_hdr *);
1024 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
1025 setdf = V_ip4_ipsec_dfbit ? 1: 0;
1026 /* scoped address handling */
1027 in6_clearscope(&ip6->ip6_src);
1028 in6_clearscope(&ip6->ip6_dst);
1032 return (EAFNOSUPPORT);
1034 switch (saidx->dst.sa.sa_family) {
1037 if (saidx->src.sa.sa_family != AF_INET ||
1038 saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
1039 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY)
1041 *mp = ipsec_prepend(*mp, sizeof(struct ip), M_NOWAIT);
1044 ip = mtod(*mp, struct ip *);
1045 ip->ip_v = IPVERSION;
1046 ip->ip_hl = sizeof(struct ip) >> 2;
1048 ip->ip_len = htons((*mp)->m_pkthdr.len);
1049 ip->ip_ttl = V_ip_defttl;
1051 ip->ip_off = setdf ? htons(IP_DF): 0;
1052 ip->ip_src = saidx->src.sin.sin_addr;
1053 ip->ip_dst = saidx->dst.sin.sin_addr;
1054 ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos);
1060 if (saidx->src.sa.sa_family != AF_INET6 ||
1061 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) ||
1062 IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr))
1064 *mp = ipsec_prepend(*mp, sizeof(struct ip6_hdr), M_NOWAIT);
1067 ip6 = mtod(*mp, struct ip6_hdr *);
1069 ip6->ip6_vfc = IPV6_VERSION;
1070 ip6->ip6_hlim = V_ip6_defhlim;
1071 ip6->ip6_nxt = proto;
1072 ip6->ip6_dst = saidx->dst.sin6.sin6_addr;
1073 /* For link-local address embed scope zone id */
1074 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
1075 ip6->ip6_dst.s6_addr16[1] =
1076 htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
1077 ip6->ip6_src = saidx->src.sin6.sin6_addr;
1078 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
1079 ip6->ip6_src.s6_addr16[1] =
1080 htons(saidx->src.sin6.sin6_scope_id & 0xffff);
1081 ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6));
1082 ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos);
1083 ip6->ip6_flow |= htonl((uint32_t)proto << 20);
1087 return (EAFNOSUPPORT);
1089 (*mp)->m_flags &= ~(M_BCAST | M_MCAST);