2 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
3 * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * IPsec output processing.
34 #include "opt_inet6.h"
35 #include "opt_ipsec.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/errno.h>
45 #include <sys/hhook.h>
46 #include <sys/syslog.h>
49 #include <net/if_enc.h>
50 #include <net/if_var.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip_ecn.h>
60 #include <netinet6/ip6_ecn.h>
63 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/scope6_var.h>
68 #include <netinet/in_pcb.h>
70 #include <netinet/icmp6.h>
73 #include <netinet/sctp_crc32.h>
76 #include <netinet/udp.h>
77 #include <netipsec/ah.h>
78 #include <netipsec/esp.h>
79 #include <netipsec/ipsec.h>
81 #include <netipsec/ipsec6.h>
83 #include <netipsec/ah_var.h>
84 #include <netipsec/esp_var.h>
85 #include <netipsec/ipcomp_var.h>
87 #include <netipsec/xform.h>
89 #include <netipsec/key.h>
90 #include <netipsec/keydb.h>
91 #include <netipsec/key_debug.h>
93 #include <machine/in_cksum.h>
95 #define IPSEC_OSTAT_INC(proto, name) do { \
96 if ((proto) == IPPROTO_ESP) \
97 ESPSTAT_INC(esps_##name); \
98 else if ((proto) == IPPROTO_AH)\
99 AHSTAT_INC(ahs_##name); \
101 IPCOMPSTAT_INC(ipcomps_##name); \
104 static int ipsec_encap(struct mbuf **mp, struct secasindex *saidx);
107 static struct secasvar *
108 ipsec4_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
110 struct secasindex *saidx, tmpsaidx;
111 struct ipsecrequest *isr;
112 struct sockaddr_in *sin;
113 struct secasvar *sav;
117 * Check system global policy controls.
120 isr = sp->req[*pidx];
121 if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
122 (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
123 (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
124 DPRINTF(("%s: IPsec outbound packet dropped due"
125 " to policy (check your sysctls)\n", __func__));
126 IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
127 *error = EHOSTUNREACH;
131 * Craft SA index to search for proper SA. Note that
132 * we only initialize unspecified SA peers for transport
133 * mode; for tunnel mode they must already be filled in.
135 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
138 ip = mtod(m, struct ip *);
139 if (saidx->src.sa.sa_len == 0) {
140 sin = &saidx->src.sin;
141 sin->sin_len = sizeof(*sin);
142 sin->sin_family = AF_INET;
143 sin->sin_port = IPSEC_PORT_ANY;
144 sin->sin_addr = ip->ip_src;
146 if (saidx->dst.sa.sa_len == 0) {
147 sin = &saidx->dst.sin;
148 sin->sin_len = sizeof(*sin);
149 sin->sin_family = AF_INET;
150 sin->sin_port = IPSEC_PORT_ANY;
151 sin->sin_addr = ip->ip_dst;
154 saidx = &sp->req[*pidx]->saidx;
156 * Lookup SA and validate it.
158 sav = key_allocsa_policy(sp, saidx, error);
160 IPSECSTAT_INC(ips_out_nosa);
163 if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
165 * We have no SA and policy that doesn't require
166 * this IPsec transform, thus we can continue w/o
167 * IPsec processing, i.e. return EJUSTRETURN.
168 * But first check if there is some bundled transform.
170 if (sp->tcount > ++(*pidx))
172 *error = EJUSTRETURN;
176 IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
181 * IPsec output logic for IPv4.
184 ipsec4_perform_request(struct mbuf *m, struct secpolicy *sp, u_int idx)
186 char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
187 struct ipsec_ctx_data ctx;
188 union sockaddr_union *dst;
189 struct secasvar *sav;
193 IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
196 * We hold the reference to SP. Content of SP couldn't be changed.
197 * Craft secasindex and do lookup for suitable SA.
198 * Then do encapsulation if needed and call xform's output.
199 * We need to store SP in the xform callback parameters.
200 * In xform callback we will extract SP and it can be used to
201 * determine next transform. At the end of transform we can
202 * release reference to SP.
204 sav = ipsec4_allocsa(m, sp, &idx, &error);
206 if (error == EJUSTRETURN) { /* No IPsec required */
213 * XXXAE: most likely ip_sum at this point is wrong.
215 IPSEC_INIT_CTX(&ctx, &m, sav, AF_INET, IPSEC_ENC_BEFORE);
216 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
219 ip = mtod(m, struct ip *);
220 dst = &sav->sah->saidx.dst;
221 /* Do the appropriate encapsulation, if necessary */
222 if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
223 dst->sa.sa_family != AF_INET || /* PF mismatch */
224 (dst->sa.sa_family == AF_INET && /* Proxy */
225 dst->sin.sin_addr.s_addr != INADDR_ANY &&
226 dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
227 /* Fix IPv4 header checksum and length */
228 ip->ip_len = htons(m->m_pkthdr.len);
230 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
231 error = ipsec_encap(&m, &sav->sah->saidx);
233 DPRINTF(("%s: encapsulation for SA %s->%s "
234 "SPI 0x%08x failed with error %d\n", __func__,
235 ipsec_address(&sav->sah->saidx.src, sbuf,
237 ipsec_address(&sav->sah->saidx.dst, dbuf,
238 sizeof(dbuf)), ntohl(sav->spi), error));
239 /* XXXAE: IPSEC_OSTAT_INC(tunnel); */
244 IPSEC_INIT_CTX(&ctx, &m, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
245 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
249 * Dispatch to the appropriate IPsec transform logic. The
250 * packet will be returned for transmission after crypto
251 * processing, etc. are completed.
253 * NB: m & sav are ``passed to caller'' who's responsible for
254 * reclaiming their resources.
256 switch(dst->sa.sa_family) {
258 ip = mtod(m, struct ip *);
260 off = offsetof(struct ip, ip_p);
264 i = sizeof(struct ip6_hdr);
265 off = offsetof(struct ip6_hdr, ip6_nxt);
269 DPRINTF(("%s: unsupported protocol family %u\n",
270 __func__, dst->sa.sa_family));
271 error = EPFNOSUPPORT;
272 IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
275 error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
282 IPSECSTAT_INC(ips_out_inval);
292 ipsec4_process_packet(struct mbuf *m, struct secpolicy *sp,
296 return (ipsec4_perform_request(m, sp, 0));
300 ipsec4_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
302 struct secpolicy *sp;
305 /* Lookup for the corresponding outbound security policy */
306 sp = ipsec4_checkpolicy(m, inp, &error);
308 if (error == -EINVAL) {
309 /* Discarded by policy. */
313 return (0); /* No IPsec required. */
317 * Usually we have to have tunnel mode IPsec security policy
318 * when we are forwarding a packet. Otherwise we could not handle
319 * encrypted replies, because they are not destined for us. But
320 * some users are doing source address translation for forwarded
321 * packets, and thus, even if they are forwarded, the replies will
326 * Do delayed checksums now because we send before
327 * this is done in the normal processing path.
329 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
331 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
334 if (m->m_pkthdr.csum_flags & CSUM_SCTP) {
335 struct ip *ip = mtod(m, struct ip *);
337 sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
338 m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
342 /* NB: callee frees mbuf and releases reference to SP */
343 error = ipsec4_process_packet(m, sp, inp);
344 if (error == EJUSTRETURN) {
346 * We had a SP with a level of 'use' and no SA. We
347 * will just continue to process the packet without
348 * IPsec processing and return without error.
353 return (EINPROGRESS); /* consumed by IPsec */
358 * IPSEC_OUTPUT() method implementation for IPv4.
359 * 0 - no IPsec handling needed
360 * other values - mbuf consumed by IPsec.
363 ipsec4_output(struct mbuf *m, struct inpcb *inp)
367 * If the packet is resubmitted to ip_output (e.g. after
368 * AH, ESP, etc. processing), there will be a tag to bypass
369 * the lookup and related policy checking.
371 if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
374 return (ipsec4_common_output(m, inp, 0));
378 * IPSEC_FORWARD() method implementation for IPv4.
379 * 0 - no IPsec handling needed
380 * other values - mbuf consumed by IPsec.
383 ipsec4_forward(struct mbuf *m)
387 * Check if this packet has an active inbound SP and needs to be
388 * dropped instead of forwarded.
390 if (ipsec4_in_reject(m, NULL) != 0) {
394 return (ipsec4_common_output(m, NULL, 1));
400 in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa,
401 const struct in6_addr *ia)
405 if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) {
406 memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
407 ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
408 return (IN6_ARE_ADDR_EQUAL(ia, &ia2));
410 return (IN6_ARE_ADDR_EQUAL(&sa->sin6_addr, ia));
413 static struct secasvar *
414 ipsec6_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
416 struct secasindex *saidx, tmpsaidx;
417 struct ipsecrequest *isr;
418 struct sockaddr_in6 *sin6;
419 struct secasvar *sav;
423 * Check system global policy controls.
426 isr = sp->req[*pidx];
427 if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
428 (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
429 (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
430 DPRINTF(("%s: IPsec outbound packet dropped due"
431 " to policy (check your sysctls)\n", __func__));
432 IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
433 *error = EHOSTUNREACH;
437 * Craft SA index to search for proper SA. Note that
438 * we only fillin unspecified SA peers for transport
439 * mode; for tunnel mode they must already be filled in.
441 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
444 ip6 = mtod(m, struct ip6_hdr *);
445 if (saidx->src.sin6.sin6_len == 0) {
446 sin6 = (struct sockaddr_in6 *)&saidx->src;
447 sin6->sin6_len = sizeof(*sin6);
448 sin6->sin6_family = AF_INET6;
449 sin6->sin6_port = IPSEC_PORT_ANY;
450 sin6->sin6_addr = ip6->ip6_src;
451 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
452 /* fix scope id for comparing SPD */
453 sin6->sin6_addr.s6_addr16[1] = 0;
454 sin6->sin6_scope_id =
455 ntohs(ip6->ip6_src.s6_addr16[1]);
458 if (saidx->dst.sin6.sin6_len == 0) {
459 sin6 = (struct sockaddr_in6 *)&saidx->dst;
460 sin6->sin6_len = sizeof(*sin6);
461 sin6->sin6_family = AF_INET6;
462 sin6->sin6_port = IPSEC_PORT_ANY;
463 sin6->sin6_addr = ip6->ip6_dst;
464 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
465 /* fix scope id for comparing SPD */
466 sin6->sin6_addr.s6_addr16[1] = 0;
467 sin6->sin6_scope_id =
468 ntohs(ip6->ip6_dst.s6_addr16[1]);
472 saidx = &sp->req[*pidx]->saidx;
474 * Lookup SA and validate it.
476 sav = key_allocsa_policy(sp, saidx, error);
478 IPSEC6STAT_INC(ips_out_nosa);
481 if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
483 * We have no SA and policy that doesn't require
484 * this IPsec transform, thus we can continue w/o
485 * IPsec processing, i.e. return EJUSTRETURN.
486 * But first check if there is some bundled transform.
488 if (sp->tcount > ++(*pidx))
490 *error = EJUSTRETURN;
494 IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
499 * IPsec output logic for IPv6.
502 ipsec6_perform_request(struct mbuf *m, struct secpolicy *sp, u_int idx)
504 char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
505 struct ipsec_ctx_data ctx;
506 union sockaddr_union *dst;
507 struct secasvar *sav;
511 IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
513 sav = ipsec6_allocsa(m, sp, &idx, &error);
515 if (error == EJUSTRETURN) { /* No IPsec required */
522 /* Fix IP length in case if it is not set yet. */
523 ip6 = mtod(m, struct ip6_hdr *);
524 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
526 IPSEC_INIT_CTX(&ctx, &m, sav, AF_INET6, IPSEC_ENC_BEFORE);
527 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
530 ip6 = mtod(m, struct ip6_hdr *); /* pfil can change mbuf */
531 dst = &sav->sah->saidx.dst;
533 /* Do the appropriate encapsulation, if necessary */
534 if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
535 dst->sa.sa_family != AF_INET6 || /* PF mismatch */
536 ((dst->sa.sa_family == AF_INET6) &&
537 (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
538 (!in6_sa_equal_addrwithscope(&dst->sin6, &ip6->ip6_dst)))) {
539 if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
540 /* No jumbogram support. */
541 error = ENXIO; /*XXX*/
544 error = ipsec_encap(&m, &sav->sah->saidx);
546 DPRINTF(("%s: encapsulation for SA %s->%s "
547 "SPI 0x%08x failed with error %d\n", __func__,
548 ipsec_address(&sav->sah->saidx.src, sbuf,
550 ipsec_address(&sav->sah->saidx.dst, dbuf,
551 sizeof(dbuf)), ntohl(sav->spi), error));
552 /* XXXAE: IPSEC_OSTAT_INC(tunnel); */
557 IPSEC_INIT_CTX(&ctx, &m, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
558 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
561 switch(dst->sa.sa_family) {
566 ip = mtod(m, struct ip *);
568 off = offsetof(struct ip, ip_p);
573 i = sizeof(struct ip6_hdr);
574 off = offsetof(struct ip6_hdr, ip6_nxt);
577 DPRINTF(("%s: unsupported protocol family %u\n",
578 __func__, dst->sa.sa_family));
579 error = EPFNOSUPPORT;
580 IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
583 error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
590 IPSEC6STAT_INC(ips_out_inval);
600 ipsec6_process_packet(struct mbuf *m, struct secpolicy *sp,
604 return (ipsec6_perform_request(m, sp, 0));
608 ipsec6_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
610 struct secpolicy *sp;
613 /* Lookup for the corresponding outbound security policy */
614 sp = ipsec6_checkpolicy(m, inp, &error);
616 if (error == -EINVAL) {
617 /* Discarded by policy. */
621 return (0); /* No IPsec required. */
626 * Do delayed checksums now because we send before
627 * this is done in the normal processing path.
629 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
630 in6_delayed_cksum(m, m->m_pkthdr.len -
631 sizeof(struct ip6_hdr), sizeof(struct ip6_hdr));
632 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
635 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
636 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
637 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
641 /* NB: callee frees mbuf and releases reference to SP */
642 error = ipsec6_process_packet(m, sp, inp);
643 if (error == EJUSTRETURN) {
645 * We had a SP with a level of 'use' and no SA. We
646 * will just continue to process the packet without
647 * IPsec processing and return without error.
652 return (EINPROGRESS); /* consumed by IPsec */
657 * IPSEC_OUTPUT() method implementation for IPv6.
658 * 0 - no IPsec handling needed
659 * other values - mbuf consumed by IPsec.
662 ipsec6_output(struct mbuf *m, struct inpcb *inp)
666 * If the packet is resubmitted to ip_output (e.g. after
667 * AH, ESP, etc. processing), there will be a tag to bypass
668 * the lookup and related policy checking.
670 if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
673 return (ipsec6_common_output(m, inp, 0));
677 * IPSEC_FORWARD() method implementation for IPv6.
678 * 0 - no IPsec handling needed
679 * other values - mbuf consumed by IPsec.
682 ipsec6_forward(struct mbuf *m)
686 * Check if this packet has an active inbound SP and needs to be
687 * dropped instead of forwarded.
689 if (ipsec6_in_reject(m, NULL) != 0) {
693 return (ipsec6_common_output(m, NULL, 1));
698 ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
701 struct xform_history *xh;
702 struct secasindex *saidx;
706 saidx = &sav->sah->saidx;
707 switch (saidx->dst.sa.sa_family) {
710 /* Fix the header length, for AH processing. */
711 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
716 /* Fix the header length, for AH processing. */
717 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
721 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
722 /* No jumbogram support. */
726 mtod(m, struct ip6_hdr *)->ip6_plen =
727 htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
731 DPRINTF(("%s: unknown protocol family %u\n", __func__,
732 saidx->dst.sa.sa_family));
738 * Add a record of what we've done to the packet.
740 mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, sizeof(*xh), M_NOWAIT);
742 DPRINTF(("%s: could not get packet tag\n", __func__));
747 xh = (struct xform_history *)(mtag + 1);
748 xh->dst = saidx->dst;
749 xh->proto = saidx->proto;
750 xh->mode = saidx->mode;
752 m_tag_prepend(m, mtag);
754 key_sa_recordxfer(sav, m); /* record data transfer */
757 * If there's another (bundled) SA to apply, do so.
758 * Note that this puts a burden on the kernel stack size.
759 * If this is a problem we'll need to introduce a queue
760 * to set the packet on so we can unwind the stack before
761 * doing further processing.
763 if (++idx < sp->tcount) {
764 switch (saidx->dst.sa.sa_family) {
768 IPSECSTAT_INC(ips_out_bundlesa);
769 return (ipsec4_perform_request(m, sp, idx));
775 IPSEC6STAT_INC(ips_out_bundlesa);
776 return (ipsec6_perform_request(m, sp, idx));
780 DPRINTF(("%s: unknown protocol family %u\n", __func__,
781 saidx->dst.sa.sa_family));
782 error = EPFNOSUPPORT;
787 key_freesp(&sp), sp = NULL; /* Release reference to SP */
790 * Do UDP encapsulation if SA requires it.
792 if (sav->natt != NULL) {
793 error = udp_ipsec_output(m, sav);
799 * We're done with IPsec processing, transmit the packet using the
800 * appropriate network protocol (IP or IPv6).
802 switch (saidx->dst.sa.sa_family) {
806 return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
811 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
814 panic("ipsec_process_done");
824 * ipsec_prepend() is optimized version of M_PREPEND().
825 * ipsec_encap() is called by IPsec output routine for tunnel mode SA.
826 * It is expected that after IP encapsulation some IPsec transform will
827 * be performed. Each IPsec transform inserts its variable length header
828 * just after outer IP header using m_makespace(). If given mbuf has not
829 * enough free space at the beginning, we allocate new mbuf and reserve
830 * some space at the beginning and at the end.
831 * This helps avoid allocating of new mbuf and data copying in m_makespace(),
832 * we place outer header in the middle of mbuf's data with reserved leading
833 * and trailing space:
834 * [ LEADINGSPACE ][ Outer IP header ][ TRAILINGSPACE ]
835 * LEADINGSPACE will be used to add ethernet header, TRAILINGSPACE will
836 * be used to inject AH/ESP/IPCOMP header.
838 #define IPSEC_TRAILINGSPACE (sizeof(struct udphdr) +/* NAT-T */ \
839 max(sizeof(struct newesp) + EALG_MAX_BLOCK_LEN, /* ESP + IV */ \
840 sizeof(struct newah) + HASH_MAX_LEN /* AH + ICV */))
842 ipsec_prepend(struct mbuf *m, int len, int how)
847 IPSEC_ASSERT(len < MHLEN, ("wrong length"));
848 if (M_LEADINGSPACE(m) >= len) {
849 /* No need to allocate new mbuf. */
852 m->m_pkthdr.len += len;
855 n = m_gethdr(how, m->m_type);
862 if (len + IPSEC_TRAILINGSPACE < M_SIZE(n))
863 m_align(n, len + IPSEC_TRAILINGSPACE);
865 n->m_pkthdr.len += len;
870 ipsec_encap(struct mbuf **mp, struct secasindex *saidx)
879 ip = mtod(*mp, struct ip *);
883 proto = IPPROTO_IPIP;
885 * Collect IP_DF state from the inner header
886 * and honor system-wide control of how to handle it.
888 switch (V_ip4_ipsec_dfbit) {
889 case 0: /* clear in outer header */
890 case 1: /* set in outer header */
891 setdf = V_ip4_ipsec_dfbit;
893 default:/* propagate to outer header */
894 setdf = (ip->ip_off & htons(IP_DF)) != 0;
900 case (IPV6_VERSION >> 4):
901 proto = IPPROTO_IPV6;
902 ip6 = mtod(*mp, struct ip6_hdr *);
903 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
904 setdf = V_ip4_ipsec_dfbit ? 1: 0;
905 /* scoped address handling */
906 in6_clearscope(&ip6->ip6_src);
907 in6_clearscope(&ip6->ip6_dst);
911 return (EAFNOSUPPORT);
913 switch (saidx->dst.sa.sa_family) {
916 if (saidx->src.sa.sa_family != AF_INET ||
917 saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
918 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY)
920 *mp = ipsec_prepend(*mp, sizeof(struct ip), M_NOWAIT);
923 ip = mtod(*mp, struct ip *);
924 ip->ip_v = IPVERSION;
925 ip->ip_hl = sizeof(struct ip) >> 2;
927 ip->ip_len = htons((*mp)->m_pkthdr.len);
928 ip->ip_ttl = V_ip_defttl;
930 ip->ip_off = setdf ? htons(IP_DF): 0;
931 ip->ip_src = saidx->src.sin.sin_addr;
932 ip->ip_dst = saidx->dst.sin.sin_addr;
933 ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos);
939 if (saidx->src.sa.sa_family != AF_INET6 ||
940 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) ||
941 IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr))
943 *mp = ipsec_prepend(*mp, sizeof(struct ip6_hdr), M_NOWAIT);
946 ip6 = mtod(*mp, struct ip6_hdr *);
948 ip6->ip6_vfc = IPV6_VERSION;
949 ip6->ip6_hlim = V_ip6_defhlim;
950 ip6->ip6_nxt = proto;
951 ip6->ip6_dst = saidx->dst.sin6.sin6_addr;
952 /* For link-local address embed scope zone id */
953 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
954 ip6->ip6_dst.s6_addr16[1] =
955 htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
956 ip6->ip6_src = saidx->src.sin6.sin6_addr;
957 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
958 ip6->ip6_src.s6_addr16[1] =
959 htons(saidx->src.sin6.sin6_scope_id & 0xffff);
960 ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6));
961 ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos);
962 ip6->ip6_flow |= htonl((uint32_t)proto << 20);
966 return (EAFNOSUPPORT);
968 (*mp)->m_flags &= ~(M_BCAST | M_MCAST);