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);
109 static size_t ipsec_get_pmtu(struct secasvar *sav);
112 static struct secasvar *
113 ipsec4_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
115 struct secasindex *saidx, tmpsaidx;
116 struct ipsecrequest *isr;
117 struct sockaddr_in *sin;
118 struct secasvar *sav;
122 * Check system global policy controls.
125 isr = sp->req[*pidx];
126 if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
127 (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
128 (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
129 DPRINTF(("%s: IPsec outbound packet dropped due"
130 " to policy (check your sysctls)\n", __func__));
131 IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
132 *error = EHOSTUNREACH;
136 * Craft SA index to search for proper SA. Note that
137 * we only initialize unspecified SA peers for transport
138 * mode; for tunnel mode they must already be filled in.
140 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
143 ip = mtod(m, struct ip *);
144 if (saidx->src.sa.sa_len == 0) {
145 sin = &saidx->src.sin;
146 sin->sin_len = sizeof(*sin);
147 sin->sin_family = AF_INET;
148 sin->sin_port = IPSEC_PORT_ANY;
149 sin->sin_addr = ip->ip_src;
151 if (saidx->dst.sa.sa_len == 0) {
152 sin = &saidx->dst.sin;
153 sin->sin_len = sizeof(*sin);
154 sin->sin_family = AF_INET;
155 sin->sin_port = IPSEC_PORT_ANY;
156 sin->sin_addr = ip->ip_dst;
159 saidx = &sp->req[*pidx]->saidx;
161 * Lookup SA and validate it.
163 sav = key_allocsa_policy(sp, saidx, error);
165 IPSECSTAT_INC(ips_out_nosa);
168 if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
170 * We have no SA and policy that doesn't require
171 * this IPsec transform, thus we can continue w/o
172 * IPsec processing, i.e. return EJUSTRETURN.
173 * But first check if there is some bundled transform.
175 if (sp->tcount > ++(*pidx))
177 *error = EJUSTRETURN;
181 IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
186 * IPsec output logic for IPv4.
189 ipsec4_perform_request(struct mbuf *m, struct secpolicy *sp,
190 struct inpcb *inp, u_int idx)
192 struct ipsec_ctx_data ctx;
193 union sockaddr_union *dst;
194 struct secasvar *sav;
198 IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
201 * We hold the reference to SP. Content of SP couldn't be changed.
202 * Craft secasindex and do lookup for suitable SA.
203 * Then do encapsulation if needed and call xform's output.
204 * We need to store SP in the xform callback parameters.
205 * In xform callback we will extract SP and it can be used to
206 * determine next transform. At the end of transform we can
207 * release reference to SP.
209 sav = ipsec4_allocsa(m, sp, &idx, &error);
211 if (error == EJUSTRETURN) { /* No IPsec required */
218 * XXXAE: most likely ip_sum at this point is wrong.
220 IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET, IPSEC_ENC_BEFORE);
221 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
224 ip = mtod(m, struct ip *);
225 dst = &sav->sah->saidx.dst;
226 /* Do the appropriate encapsulation, if necessary */
227 if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
228 dst->sa.sa_family != AF_INET || /* PF mismatch */
229 (dst->sa.sa_family == AF_INET && /* Proxy */
230 dst->sin.sin_addr.s_addr != INADDR_ANY &&
231 dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
232 /* Fix IPv4 header checksum and length */
233 ip->ip_len = htons(m->m_pkthdr.len);
235 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
236 error = ipsec_encap(&m, &sav->sah->saidx);
238 DPRINTF(("%s: encapsulation for SPI 0x%08x failed "
239 "with error %d\n", __func__, ntohl(sav->spi),
241 /* XXXAE: IPSEC_OSTAT_INC(tunnel); */
247 IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
248 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
252 * Dispatch to the appropriate IPsec transform logic. The
253 * packet will be returned for transmission after crypto
254 * processing, etc. are completed.
256 * NB: m & sav are ``passed to caller'' who's responsible for
257 * reclaiming their resources.
259 switch(dst->sa.sa_family) {
261 ip = mtod(m, struct ip *);
263 off = offsetof(struct ip, ip_p);
267 i = sizeof(struct ip6_hdr);
268 off = offsetof(struct ip6_hdr, ip6_nxt);
272 DPRINTF(("%s: unsupported protocol family %u\n",
273 __func__, dst->sa.sa_family));
274 error = EPFNOSUPPORT;
275 IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
278 error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
281 IPSECSTAT_INC(ips_out_inval);
291 ipsec4_process_packet(struct mbuf *m, struct secpolicy *sp,
295 return (ipsec4_perform_request(m, sp, inp, 0));
299 ipsec4_check_pmtu(struct mbuf *m, struct secpolicy *sp, int forwarding)
301 struct secasvar *sav;
307 /* Don't check PMTU if the frame won't have DF bit set. */
308 if (!V_ip4_ipsec_dfbit)
310 if (V_ip4_ipsec_dfbit == 1)
313 /* V_ip4_ipsec_dfbit > 1 - we will copy it from inner header. */
314 ip = mtod(m, struct ip *);
315 if (!(ip->ip_off & htons(IP_DF)))
319 idx = sp->tcount - 1;
320 sav = ipsec4_allocsa(m, sp, &idx, &error);
324 * No matching SA was found and SADB_ACQUIRE message was generated.
325 * Since we have matched a SP to this packet drop it silently.
329 if (error != EJUSTRETURN)
335 pmtu = ipsec_get_pmtu(sav);
341 hlen = ipsec_hdrsiz_internal(sp);
344 if (m_length(m, NULL) + hlen > pmtu) {
346 * If we're forwarding generate ICMP message here,
347 * so that it contains pmtu substraced by header size.
348 * Set error to EINPROGRESS, in order for the frame
349 * to be dropped silently.
353 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
359 return (EINPROGRESS); /* Pretend that we consumed it. */
371 ipsec4_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
373 struct secpolicy *sp;
376 /* Lookup for the corresponding outbound security policy */
377 sp = ipsec4_checkpolicy(m, inp, &error, !forwarding);
379 if (error == -EINVAL) {
380 /* Discarded by policy. */
384 return (0); /* No IPsec required. */
388 * Usually we have to have tunnel mode IPsec security policy
389 * when we are forwarding a packet. Otherwise we could not handle
390 * encrypted replies, because they are not destined for us. But
391 * some users are doing source address translation for forwarded
392 * packets, and thus, even if they are forwarded, the replies will
397 * Do delayed checksums now because we send before
398 * this is done in the normal processing path.
400 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
401 m = mb_unmapped_to_ext(m);
403 IPSECSTAT_INC(ips_out_nomem);
408 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
410 #if defined(SCTP) || defined(SCTP_SUPPORT)
411 if (m->m_pkthdr.csum_flags & CSUM_SCTP) {
414 m = mb_unmapped_to_ext(m);
416 IPSECSTAT_INC(ips_out_nomem);
420 ip = mtod(m, struct ip *);
421 sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
422 m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
426 /* NB: callee frees mbuf and releases reference to SP */
427 error = ipsec4_check_pmtu(m, sp, forwarding);
429 if (error == EJUSTRETURN)
435 error = ipsec4_process_packet(m, sp, inp);
436 if (error == EJUSTRETURN) {
438 * We had a SP with a level of 'use' and no SA. We
439 * will just continue to process the packet without
440 * IPsec processing and return without error.
445 return (EINPROGRESS); /* consumed by IPsec */
450 * IPSEC_OUTPUT() method implementation for IPv4.
451 * 0 - no IPsec handling needed
452 * other values - mbuf consumed by IPsec.
455 ipsec4_output(struct mbuf *m, struct inpcb *inp)
459 * If the packet is resubmitted to ip_output (e.g. after
460 * AH, ESP, etc. processing), there will be a tag to bypass
461 * the lookup and related policy checking.
463 if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
466 return (ipsec4_common_output(m, inp, 0));
470 * IPSEC_FORWARD() method implementation for IPv4.
471 * 0 - no IPsec handling needed
472 * other values - mbuf consumed by IPsec.
475 ipsec4_forward(struct mbuf *m)
479 * Check if this packet has an active inbound SP and needs to be
480 * dropped instead of forwarded.
482 if (ipsec4_in_reject(m, NULL) != 0) {
486 return (ipsec4_common_output(m, NULL, 1));
492 in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa,
493 const struct in6_addr *ia)
497 if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) {
498 memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
499 ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
500 return (IN6_ARE_ADDR_EQUAL(ia, &ia2));
502 return (IN6_ARE_ADDR_EQUAL(&sa->sin6_addr, ia));
505 static struct secasvar *
506 ipsec6_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
508 struct secasindex *saidx, tmpsaidx;
509 struct ipsecrequest *isr;
510 struct sockaddr_in6 *sin6;
511 struct secasvar *sav;
515 * Check system global policy controls.
518 isr = sp->req[*pidx];
519 if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
520 (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
521 (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
522 DPRINTF(("%s: IPsec outbound packet dropped due"
523 " to policy (check your sysctls)\n", __func__));
524 IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
525 *error = EHOSTUNREACH;
529 * Craft SA index to search for proper SA. Note that
530 * we only fillin unspecified SA peers for transport
531 * mode; for tunnel mode they must already be filled in.
533 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
536 ip6 = mtod(m, struct ip6_hdr *);
537 if (saidx->src.sin6.sin6_len == 0) {
538 sin6 = (struct sockaddr_in6 *)&saidx->src;
539 sin6->sin6_len = sizeof(*sin6);
540 sin6->sin6_family = AF_INET6;
541 sin6->sin6_port = IPSEC_PORT_ANY;
542 sin6->sin6_addr = ip6->ip6_src;
543 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
544 /* fix scope id for comparing SPD */
545 sin6->sin6_addr.s6_addr16[1] = 0;
546 sin6->sin6_scope_id =
547 ntohs(ip6->ip6_src.s6_addr16[1]);
550 if (saidx->dst.sin6.sin6_len == 0) {
551 sin6 = (struct sockaddr_in6 *)&saidx->dst;
552 sin6->sin6_len = sizeof(*sin6);
553 sin6->sin6_family = AF_INET6;
554 sin6->sin6_port = IPSEC_PORT_ANY;
555 sin6->sin6_addr = ip6->ip6_dst;
556 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
557 /* fix scope id for comparing SPD */
558 sin6->sin6_addr.s6_addr16[1] = 0;
559 sin6->sin6_scope_id =
560 ntohs(ip6->ip6_dst.s6_addr16[1]);
564 saidx = &sp->req[*pidx]->saidx;
566 * Lookup SA and validate it.
568 sav = key_allocsa_policy(sp, saidx, error);
570 IPSEC6STAT_INC(ips_out_nosa);
573 if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
575 * We have no SA and policy that doesn't require
576 * this IPsec transform, thus we can continue w/o
577 * IPsec processing, i.e. return EJUSTRETURN.
578 * But first check if there is some bundled transform.
580 if (sp->tcount > ++(*pidx))
582 *error = EJUSTRETURN;
586 IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
591 * IPsec output logic for IPv6.
594 ipsec6_perform_request(struct mbuf *m, struct secpolicy *sp,
595 struct inpcb *inp, u_int idx)
597 struct ipsec_ctx_data ctx;
598 union sockaddr_union *dst;
599 struct secasvar *sav;
603 IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
605 sav = ipsec6_allocsa(m, sp, &idx, &error);
607 if (error == EJUSTRETURN) { /* No IPsec required */
614 /* Fix IP length in case if it is not set yet. */
615 ip6 = mtod(m, struct ip6_hdr *);
616 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
618 IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET6, IPSEC_ENC_BEFORE);
619 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
622 ip6 = mtod(m, struct ip6_hdr *); /* pfil can change mbuf */
623 dst = &sav->sah->saidx.dst;
625 /* Do the appropriate encapsulation, if necessary */
626 if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
627 dst->sa.sa_family != AF_INET6 || /* PF mismatch */
628 ((dst->sa.sa_family == AF_INET6) &&
629 (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
630 (!in6_sa_equal_addrwithscope(&dst->sin6, &ip6->ip6_dst)))) {
631 if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
632 /* No jumbogram support. */
633 error = ENXIO; /*XXX*/
636 error = ipsec_encap(&m, &sav->sah->saidx);
638 DPRINTF(("%s: encapsulation for SPI 0x%08x failed "
639 "with error %d\n", __func__, ntohl(sav->spi),
641 /* XXXAE: IPSEC_OSTAT_INC(tunnel); */
647 IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
648 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
651 switch(dst->sa.sa_family) {
656 ip = mtod(m, struct ip *);
658 off = offsetof(struct ip, ip_p);
663 i = sizeof(struct ip6_hdr);
664 off = offsetof(struct ip6_hdr, ip6_nxt);
667 DPRINTF(("%s: unsupported protocol family %u\n",
668 __func__, dst->sa.sa_family));
669 error = EPFNOSUPPORT;
670 IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
673 error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
676 IPSEC6STAT_INC(ips_out_inval);
686 ipsec6_process_packet(struct mbuf *m, struct secpolicy *sp,
690 return (ipsec6_perform_request(m, sp, inp, 0));
694 * IPv6 implementation is based on IPv4 implementation.
697 ipsec6_check_pmtu(struct mbuf *m, struct secpolicy *sp, int forwarding)
699 struct secasvar *sav;
705 * According to RFC8200 L3 fragmentation is supposed to be done only on
706 * locally generated packets. During L3 forwarding packets that are too
707 * big are always supposed to be dropped, with an ICMPv6 packet being
713 idx = sp->tcount - 1;
714 sav = ipsec6_allocsa(m, sp, &idx, &error);
718 * No matching SA was found and SADB_ACQUIRE message was generated.
719 * Since we have matched a SP to this packet drop it silently.
723 if (error != EJUSTRETURN)
729 pmtu = ipsec_get_pmtu(sav);
735 hlen = ipsec_hdrsiz_internal(sp);
738 if (m_length(m, NULL) + hlen > pmtu) {
740 * If we're forwarding generate ICMPv6 message here,
741 * so that it contains pmtu substracted by header size.
742 * Set error to EINPROGRESS, in order for the frame
743 * to be dropped silently.
747 icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, pmtu - hlen);
752 return (EINPROGRESS); /* Pretend that we consumed it. */
760 ipsec6_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
762 struct secpolicy *sp;
765 /* Lookup for the corresponding outbound security policy */
766 sp = ipsec6_checkpolicy(m, inp, &error, !forwarding);
768 if (error == -EINVAL) {
769 /* Discarded by policy. */
773 return (0); /* No IPsec required. */
778 * Do delayed checksums now because we send before
779 * this is done in the normal processing path.
781 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
782 m = mb_unmapped_to_ext(m);
784 IPSEC6STAT_INC(ips_out_nomem);
788 in6_delayed_cksum(m, m->m_pkthdr.len -
789 sizeof(struct ip6_hdr), sizeof(struct ip6_hdr));
790 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
792 #if defined(SCTP) || defined(SCTP_SUPPORT)
793 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
794 m = mb_unmapped_to_ext(m);
796 IPSEC6STAT_INC(ips_out_nomem);
800 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
801 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
806 error = ipsec6_check_pmtu(m, sp, forwarding);
808 if (error == EJUSTRETURN)
814 /* NB: callee frees mbuf and releases reference to SP */
815 error = ipsec6_process_packet(m, sp, inp);
816 if (error == EJUSTRETURN) {
818 * We had a SP with a level of 'use' and no SA. We
819 * will just continue to process the packet without
820 * IPsec processing and return without error.
825 return (EINPROGRESS); /* consumed by IPsec */
830 * IPSEC_OUTPUT() method implementation for IPv6.
831 * 0 - no IPsec handling needed
832 * other values - mbuf consumed by IPsec.
835 ipsec6_output(struct mbuf *m, struct inpcb *inp)
839 * If the packet is resubmitted to ip_output (e.g. after
840 * AH, ESP, etc. processing), there will be a tag to bypass
841 * the lookup and related policy checking.
843 if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
846 return (ipsec6_common_output(m, inp, 0));
850 * IPSEC_FORWARD() method implementation for IPv6.
851 * 0 - no IPsec handling needed
852 * other values - mbuf consumed by IPsec.
855 ipsec6_forward(struct mbuf *m)
859 * Check if this packet has an active inbound SP and needs to be
860 * dropped instead of forwarded.
862 if (ipsec6_in_reject(m, NULL) != 0) {
866 return (ipsec6_common_output(m, NULL, 1));
871 ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
874 struct epoch_tracker et;
875 struct xform_history *xh;
876 struct secasindex *saidx;
880 saidx = &sav->sah->saidx;
881 switch (saidx->dst.sa.sa_family) {
884 /* Fix the header length, for AH processing. */
885 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
890 /* Fix the header length, for AH processing. */
891 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
895 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
896 /* No jumbogram support. */
900 mtod(m, struct ip6_hdr *)->ip6_plen =
901 htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
905 DPRINTF(("%s: unknown protocol family %u\n", __func__,
906 saidx->dst.sa.sa_family));
912 * Add a record of what we've done to the packet.
914 mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, sizeof(*xh), M_NOWAIT);
916 DPRINTF(("%s: could not get packet tag\n", __func__));
921 xh = (struct xform_history *)(mtag + 1);
922 xh->dst = saidx->dst;
923 xh->proto = saidx->proto;
924 xh->mode = saidx->mode;
926 m_tag_prepend(m, mtag);
928 key_sa_recordxfer(sav, m); /* record data transfer */
931 * If there's another (bundled) SA to apply, do so.
932 * Note that this puts a burden on the kernel stack size.
933 * If this is a problem we'll need to introduce a queue
934 * to set the packet on so we can unwind the stack before
935 * doing further processing.
937 if (++idx < sp->tcount) {
938 switch (saidx->dst.sa.sa_family) {
942 IPSECSTAT_INC(ips_out_bundlesa);
943 return (ipsec4_perform_request(m, sp, NULL, idx));
949 IPSEC6STAT_INC(ips_out_bundlesa);
950 return (ipsec6_perform_request(m, sp, NULL, idx));
954 DPRINTF(("%s: unknown protocol family %u\n", __func__,
955 saidx->dst.sa.sa_family));
956 error = EPFNOSUPPORT;
961 key_freesp(&sp), sp = NULL; /* Release reference to SP */
964 * Do UDP encapsulation if SA requires it.
966 if (sav->natt != NULL) {
967 error = udp_ipsec_output(m, sav);
973 * We're done with IPsec processing, transmit the packet using the
974 * appropriate network protocol (IP or IPv6).
977 switch (saidx->dst.sa.sa_family) {
981 error = ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
987 error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
991 panic("ipsec_process_done");
1004 * ipsec_prepend() is optimized version of M_PREPEND().
1005 * ipsec_encap() is called by IPsec output routine for tunnel mode SA.
1006 * It is expected that after IP encapsulation some IPsec transform will
1007 * be performed. Each IPsec transform inserts its variable length header
1008 * just after outer IP header using m_makespace(). If given mbuf has not
1009 * enough free space at the beginning, we allocate new mbuf and reserve
1010 * some space at the beginning and at the end.
1011 * This helps avoid allocating of new mbuf and data copying in m_makespace(),
1012 * we place outer header in the middle of mbuf's data with reserved leading
1013 * and trailing space:
1014 * [ LEADINGSPACE ][ Outer IP header ][ TRAILINGSPACE ]
1015 * LEADINGSPACE will be used to add ethernet header, TRAILINGSPACE will
1016 * be used to inject AH/ESP/IPCOMP header.
1018 #define IPSEC_TRAILINGSPACE (sizeof(struct udphdr) +/* NAT-T */ \
1019 max(sizeof(struct newesp) + EALG_MAX_BLOCK_LEN, /* ESP + IV */ \
1020 sizeof(struct newah) + HASH_MAX_LEN /* AH + ICV */))
1021 static struct mbuf *
1022 ipsec_prepend(struct mbuf *m, int len, int how)
1027 IPSEC_ASSERT(len < MHLEN, ("wrong length"));
1028 if (M_LEADINGSPACE(m) >= len) {
1029 /* No need to allocate new mbuf. */
1032 m->m_pkthdr.len += len;
1035 n = m_gethdr(how, m->m_type);
1040 m_move_pkthdr(n, m);
1042 if (len + IPSEC_TRAILINGSPACE < M_SIZE(n))
1043 m_align(n, len + IPSEC_TRAILINGSPACE);
1045 n->m_pkthdr.len += len;
1050 ipsec_get_pmtu(struct secasvar *sav)
1052 union sockaddr_union *dst;
1053 struct in_conninfo inc;
1056 dst = &sav->sah->saidx.dst;
1057 memset(&inc, 0, sizeof(inc));
1059 switch (dst->sa.sa_family) {
1062 inc.inc_faddr = satosin(&dst->sa)->sin_addr;
1067 inc.inc6_faddr = satosin6(&dst->sa)->sin6_addr;
1068 inc.inc_flags |= INC_ISIPV6;
1075 pmtu = tcp_hc_getmtu(&inc);
1079 /* No entry in hostcache. Assume that PMTU is equal to link's MTU */
1080 switch (dst->sa.sa_family) {
1083 pmtu = tcp_maxmtu(&inc, NULL);
1088 pmtu = tcp_maxmtu6(&inc, NULL);
1097 tcp_hc_updatemtu(&inc, pmtu);
1103 ipsec_encap(struct mbuf **mp, struct secasindex *saidx)
1106 struct ip6_hdr *ip6;
1110 uint8_t itos, proto;
1112 ip = mtod(*mp, struct ip *);
1116 proto = IPPROTO_IPIP;
1118 * Collect IP_DF state from the inner header
1119 * and honor system-wide control of how to handle it.
1121 switch (V_ip4_ipsec_dfbit) {
1122 case 0: /* clear in outer header */
1123 case 1: /* set in outer header */
1124 setdf = V_ip4_ipsec_dfbit;
1126 default:/* propagate to outer header */
1127 setdf = (ip->ip_off & htons(IP_DF)) != 0;
1133 case (IPV6_VERSION >> 4):
1134 proto = IPPROTO_IPV6;
1135 ip6 = mtod(*mp, struct ip6_hdr *);
1136 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
1137 setdf = V_ip4_ipsec_dfbit ? 1: 0;
1138 /* scoped address handling */
1139 in6_clearscope(&ip6->ip6_src);
1140 in6_clearscope(&ip6->ip6_dst);
1144 return (EAFNOSUPPORT);
1146 switch (saidx->dst.sa.sa_family) {
1149 if (saidx->src.sa.sa_family != AF_INET ||
1150 saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
1151 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY)
1153 *mp = ipsec_prepend(*mp, sizeof(struct ip), M_NOWAIT);
1156 ip = mtod(*mp, struct ip *);
1157 ip->ip_v = IPVERSION;
1158 ip->ip_hl = sizeof(struct ip) >> 2;
1160 ip->ip_len = htons((*mp)->m_pkthdr.len);
1161 ip->ip_ttl = V_ip_defttl;
1163 ip->ip_off = setdf ? htons(IP_DF): 0;
1164 ip->ip_src = saidx->src.sin.sin_addr;
1165 ip->ip_dst = saidx->dst.sin.sin_addr;
1166 ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos);
1172 if (saidx->src.sa.sa_family != AF_INET6 ||
1173 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) ||
1174 IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr))
1176 *mp = ipsec_prepend(*mp, sizeof(struct ip6_hdr), M_NOWAIT);
1179 ip6 = mtod(*mp, struct ip6_hdr *);
1181 ip6->ip6_vfc = IPV6_VERSION;
1182 ip6->ip6_hlim = V_ip6_defhlim;
1183 ip6->ip6_nxt = proto;
1184 ip6->ip6_dst = saidx->dst.sin6.sin6_addr;
1185 /* For link-local address embed scope zone id */
1186 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
1187 ip6->ip6_dst.s6_addr16[1] =
1188 htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
1189 ip6->ip6_src = saidx->src.sin6.sin6_addr;
1190 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
1191 ip6->ip6_src.s6_addr16[1] =
1192 htons(saidx->src.sin6.sin6_scope_id & 0xffff);
1193 ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6));
1194 ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos);
1195 ip6->ip6_flow |= htonl((uint32_t)proto << 20);
1199 return (EAFNOSUPPORT);
1201 (*mp)->m_flags &= ~(M_BCAST | M_MCAST);