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
26 * $FreeBSD: src/sys/netipsec/ipsec_output.c,v 1.3.2.2 2003/03/28 20:32:53 sam Exp $
27 * $DragonFly: src/sys/netproto/ipsec/ipsec_output.c,v 1.3 2003/08/07 21:17:37 dillon Exp $
31 * IPsec output processing.
34 #include "opt_inet6.h"
35 #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>
47 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip_ecn.h>
56 #include <netinet6/ip6_ecn.h>
59 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
63 #include <netinet/in_pcb.h>
65 #include <netinet/icmp6.h>
74 #include "ipcomp_var.h"
80 #include "key_debug.h"
82 #include <machine/in_cksum.h>
85 ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
87 struct tdb_ident *tdbi;
90 struct secasindex *saidx;
93 SPLASSERT(net, "ipsec_process_done");
95 KASSERT(m != NULL, ("ipsec_process_done: null mbuf"));
96 KASSERT(isr != NULL, ("ipsec_process_done: null ISR"));
98 KASSERT(sav != NULL, ("ipsec_process_done: null SA"));
99 KASSERT(sav->sah != NULL, ("ipsec_process_done: null SAH"));
101 saidx = &sav->sah->saidx;
102 switch (saidx->dst.sa.sa_family) {
105 /* Fix the header length, for AH processing. */
106 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
111 /* Fix the header length, for AH processing. */
112 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
116 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
117 /* No jumbogram support. */
121 mtod(m, struct ip6_hdr *)->ip6_plen =
122 htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
126 DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
127 saidx->dst.sa.sa_family));
133 * Add a record of what we've done or what needs to be done to the
136 mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
137 sizeof(struct tdb_ident), M_NOWAIT);
139 DPRINTF(("ipsec_process_done: could not get packet tag\n"));
144 tdbi = (struct tdb_ident *)(mtag + 1);
145 tdbi->dst = saidx->dst;
146 tdbi->proto = saidx->proto;
147 tdbi->spi = sav->spi;
148 m_tag_prepend(m, mtag);
151 * If there's another (bundled) SA to apply, do so.
152 * Note that this puts a burden on the kernel stack size.
153 * If this is a problem we'll need to introduce a queue
154 * to set the packet on so we can unwind the stack before
155 * doing further processing.
158 newipsecstat.ips_out_bundlesa++;
159 return ipsec4_process_packet(m, isr->next, 0, 0);
163 * We're done with IPsec processing, transmit the packet using the
164 * appropriate network protocol (IP or IPv6). SPD lookup will be
165 * performed again there.
167 switch (saidx->dst.sa.sa_family) {
171 ip = mtod(m, struct ip *);
172 ip->ip_len = ntohs(ip->ip_len);
173 ip->ip_off = ntohs(ip->ip_off);
175 return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
180 * We don't need massage, IPv6 header fields are always in
183 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
186 panic("ipsec_process_done");
193 static struct ipsecrequest *
196 struct ipsecrequest *isr,
198 struct secasindex *saidx,
202 #define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \
203 isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++)
204 struct secasvar *sav;
206 SPLASSERT(net, "ipsec_nextisr");
207 KASSERT(af == AF_INET || af == AF_INET6,
208 ("ipsec_nextisr: invalid address family %u", af));
211 * Craft SA index to search for proper SA. Note that
212 * we only fillin unspecified SA peers for transport
213 * mode; for tunnel mode they must already be filled in.
216 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
217 /* Fillin unspecified SA peers only for transport mode */
219 struct sockaddr_in *sin;
220 struct ip *ip = mtod(m, struct ip *);
222 if (saidx->src.sa.sa_len == 0) {
223 sin = &saidx->src.sin;
224 sin->sin_len = sizeof(*sin);
225 sin->sin_family = AF_INET;
226 sin->sin_port = IPSEC_PORT_ANY;
227 sin->sin_addr = ip->ip_src;
229 if (saidx->dst.sa.sa_len == 0) {
230 sin = &saidx->dst.sin;
231 sin->sin_len = sizeof(*sin);
232 sin->sin_family = AF_INET;
233 sin->sin_port = IPSEC_PORT_ANY;
234 sin->sin_addr = ip->ip_dst;
237 struct sockaddr_in6 *sin6;
238 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
240 if (saidx->src.sin6.sin6_len == 0) {
241 sin6 = (struct sockaddr_in6 *)&saidx->src;
242 sin6->sin6_len = sizeof(*sin6);
243 sin6->sin6_family = AF_INET6;
244 sin6->sin6_port = IPSEC_PORT_ANY;
245 sin6->sin6_addr = ip6->ip6_src;
246 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
247 /* fix scope id for comparing SPD */
248 sin6->sin6_addr.s6_addr16[1] = 0;
249 sin6->sin6_scope_id =
250 ntohs(ip6->ip6_src.s6_addr16[1]);
253 if (saidx->dst.sin6.sin6_len == 0) {
254 sin6 = (struct sockaddr_in6 *)&saidx->dst;
255 sin6->sin6_len = sizeof(*sin6);
256 sin6->sin6_family = AF_INET6;
257 sin6->sin6_port = IPSEC_PORT_ANY;
258 sin6->sin6_addr = ip6->ip6_dst;
259 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
260 /* fix scope id for comparing SPD */
261 sin6->sin6_addr.s6_addr16[1] = 0;
262 sin6->sin6_scope_id =
263 ntohs(ip6->ip6_dst.s6_addr16[1]);
270 * Lookup SA and validate it.
272 *error = key_checkrequest(isr, saidx);
275 * IPsec processing is required, but no SA found.
276 * I assume that key_acquire() had been called
277 * to get/establish the SA. Here I discard
278 * this packet because it is responsibility for
279 * upper layer to retransmit the packet.
281 newipsecstat.ips_out_nosa++;
285 if (sav == NULL) { /* XXX valid return */
286 KASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
287 ("ipsec_nextisr: no SA found, but required; level %u",
288 ipsec_get_reqlevel(isr)));
292 *error = EINVAL; /*XXX*/
299 * Check system global policy controls.
301 if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
302 (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
303 (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
304 DPRINTF(("ipsec_nextisr: IPsec outbound packet dropped due"
305 " to policy (check your sysctls)\n"));
306 IPSEC_OSTAT(espstat.esps_pdrops, ahstat.ahs_pdrops,
307 ipcompstat.ipcomps_pdrops);
308 *error = EHOSTUNREACH;
313 * Sanity check the SA contents for the caller
314 * before they invoke the xform output method.
316 if (sav->tdb_xform == NULL) {
317 DPRINTF(("ipsec_nextisr: no transform for SA\n"));
318 IPSEC_OSTAT(espstat.esps_noxform, ahstat.ahs_noxform,
319 ipcompstat.ipcomps_noxform);
320 *error = EHOSTUNREACH;
325 KASSERT(*error != 0, ("ipsec_nextisr: error return w/ no error code"));
332 * IPsec output logic for IPv4.
335 ipsec4_process_packet(
337 struct ipsecrequest *isr,
341 struct secasindex saidx;
342 struct secasvar *sav;
344 int s, error, i, off;
346 KASSERT(m != NULL, ("ipsec4_process_packet: null mbuf"));
347 KASSERT(isr != NULL, ("ipsec4_process_packet: null isr"));
349 s = splnet(); /* insure SA contents don't change */
351 isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
357 union sockaddr_union *dst = &sav->sah->saidx.dst;
361 * Collect IP_DF state from the outer header.
363 if (dst->sa.sa_family == AF_INET) {
364 if (m->m_len < sizeof (struct ip) &&
365 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
369 ip = mtod(m, struct ip *);
370 /* Honor system-wide control of how to handle IP_DF */
371 switch (ip4_ipsec_dfbit) {
372 case 0: /* clear in outer header */
373 case 1: /* set in outer header */
374 setdf = ip4_ipsec_dfbit;
376 default: /* propagate to outer header */
377 setdf = ntohs(ip->ip_off & IP_DF);
381 ip = NULL; /* keep compiler happy */
384 /* Do the appropriate encapsulation, if necessary */
385 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
386 dst->sa.sa_family != AF_INET || /* PF mismatch */
388 (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
389 sav->tdb_xform->xf_type == XF_IP4 || /* ditto */
391 (dst->sa.sa_family == AF_INET && /* Proxy */
392 dst->sin.sin_addr.s_addr != INADDR_ANY &&
393 dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
396 /* Fix IPv4 header checksum and length */
397 if (m->m_len < sizeof (struct ip) &&
398 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
402 ip = mtod(m, struct ip *);
403 ip->ip_len = htons(m->m_pkthdr.len);
406 if (ip->ip_vhl == IP_VHL_BORING)
407 ip->ip_sum = in_cksum_hdr(ip);
409 ip->ip_sum = in_cksum(m,
410 _IP_VHL_HL(ip->ip_vhl) << 2);
412 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
415 /* Encapsulate the packet */
416 error = ipip_output(m, isr, &mp, 0, 0);
417 if (mp == NULL && !error) {
418 /* Should never happen. */
419 DPRINTF(("ipsec4_process_packet: ipip_output "
420 "returns no mbuf and no error!"));
430 * ipip_output clears IP_DF in the new header. If
431 * we need to propagate IP_DF from the outer header,
432 * then we have to do it here.
434 * XXX shouldn't assume what ipip_output does.
436 if (dst->sa.sa_family == AF_INET && setdf) {
437 if (m->m_len < sizeof (struct ip) &&
438 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
442 ip = mtod(m, struct ip *);
443 ip->ip_off = ntohs(ip->ip_off);
445 ip->ip_off = htons(ip->ip_off);
451 * Dispatch to the appropriate IPsec transform logic. The
452 * packet will be returned for transmission after crypto
453 * processing, etc. are completed. For encapsulation we
454 * bypass this call because of the explicit call done above
455 * (necessary to deal with IP_DF handling for IPv4).
457 * NB: m & sav are ``passed to caller'' who's reponsible for
458 * for reclaiming their resources.
460 if (sav->tdb_xform->xf_type != XF_IP4) {
461 ip = mtod(m, struct ip *);
463 off = offsetof(struct ip, ip_p);
464 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
466 error = ipsec_process_done(m, isr);
480 * Chop IP6 header from the payload.
483 ipsec6_splithdr(struct mbuf *m)
489 KASSERT(m->m_len >= sizeof (struct ip6_hdr),
490 ("ipsec6_splithdr: first mbuf too short, len %u", m->m_len));
491 ip6 = mtod(m, struct ip6_hdr *);
492 hlen = sizeof(struct ip6_hdr);
493 if (m->m_len > hlen) {
494 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
499 M_MOVE_PKTHDR(mh, m);
506 bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
507 } else if (m->m_len < hlen) {
508 m = m_pullup(m, hlen);
516 * IPsec output logic for IPv6, transport mode.
520 struct ipsec_output_state *state,
523 struct secpolicy *sp,
527 struct ipsecrequest *isr;
528 struct secasindex saidx;
532 KASSERT(state != NULL, ("ipsec6_output: null state"));
533 KASSERT(state->m != NULL, ("ipsec6_output: null m"));
534 KASSERT(nexthdrp != NULL, ("ipsec6_output: null nexthdrp"));
535 KASSERT(mprev != NULL, ("ipsec6_output: null mprev"));
536 KASSERT(sp != NULL, ("ipsec6_output: null sp"));
537 KASSERT(tun != NULL, ("ipsec6_output: null tun"));
539 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
540 printf("ipsec6_output_trans: applyed SP\n");
541 kdebug_secpolicy(sp));
544 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
545 /* the rest will be handled by ipsec6_output_tunnel() */
546 *tun = 1; /* need tunnel-mode processing */
553 isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
556 /* XXX should notification be done for all errors ? */
558 * Notify the fact that the packet is discarded
559 * to ourselves. I believe this is better than
560 * just silently discarding. (jinmei@kame.net)
561 * XXX: should we restrict the error to TCP packets?
562 * XXX: should we directly notify sockets via
565 icmp6_error(m, ICMP6_DST_UNREACH,
566 ICMP6_DST_UNREACH_ADMIN, 0);
567 m = NULL; /* NB: icmp6_error frees mbuf */
572 return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
573 sizeof (struct ip6_hdr),
574 offsetof(struct ip6_hdr, ip6_nxt));
583 ipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
585 struct ip6_hdr *oip6;
589 /* can't tunnel between different AFs */
590 if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
591 sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
595 KASSERT(m->m_len != sizeof (struct ip6_hdr),
596 ("ipsec6_encapsulate: mbuf wrong size; len %u", m->m_len));
600 * grow the mbuf to accomodate the new IPv6 header.
602 plen = m->m_pkthdr.len;
603 if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
605 MGET(n, M_DONTWAIT, MT_DATA);
610 n->m_len = sizeof(struct ip6_hdr);
611 n->m_next = m->m_next;
613 m->m_pkthdr.len += sizeof(struct ip6_hdr);
614 oip6 = mtod(n, struct ip6_hdr *);
616 m->m_next->m_len += sizeof(struct ip6_hdr);
617 m->m_next->m_data -= sizeof(struct ip6_hdr);
618 m->m_pkthdr.len += sizeof(struct ip6_hdr);
619 oip6 = mtod(m->m_next, struct ip6_hdr *);
621 ip6 = mtod(m, struct ip6_hdr *);
622 ovbcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
624 /* Fake link-local scope-class addresses */
625 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
626 oip6->ip6_src.s6_addr16[1] = 0;
627 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
628 oip6->ip6_dst.s6_addr16[1] = 0;
630 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
631 /* ECN consideration. */
632 ip6_ecn_ingress(ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
633 if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
634 ip6->ip6_plen = htons(plen);
636 /* ip6->ip6_plen will be updated in ip6_output() */
638 ip6->ip6_nxt = IPPROTO_IPV6;
639 sav->sah->saidx.src.sin6.sin6_addr = ip6->ip6_src;
640 sav->sah->saidx.dst.sin6.sin6_addr = ip6->ip6_dst;
641 ip6->ip6_hlim = IPV6_DEFHLIM;
643 /* XXX Should ip6_src be updated later ? */
649 * IPsec output logic for IPv6, tunnel mode.
652 ipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
655 struct ipsecrequest *isr;
656 struct secasindex saidx;
658 struct sockaddr_in6* dst6;
661 KASSERT(state != NULL, ("ipsec6_output: null state"));
662 KASSERT(state->m != NULL, ("ipsec6_output: null m"));
663 KASSERT(sp != NULL, ("ipsec6_output: null sp"));
665 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
666 printf("ipsec6_output_tunnel: applyed SP\n");
667 kdebug_secpolicy(sp));
671 * transport mode ipsec (before the 1st tunnel mode) is already
672 * processed by ipsec6_output_trans().
674 for (isr = sp->req; isr; isr = isr->next) {
675 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
678 isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
683 * There may be the case that SA status will be changed when
684 * we are refering to one. So calling splsoftnet().
686 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
688 * build IPsec tunnel.
690 /* XXX should be processed with other familiy */
691 if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
692 ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
693 "family mismatched between inner and outer, spi=%lu\n",
694 ntohl(isr->sav->spi)));
695 newipsecstat.ips_out_inval++;
696 error = EAFNOSUPPORT;
700 m = ipsec6_splithdr(m);
702 newipsecstat.ips_out_nomem++;
706 error = ipsec6_encapsulate(m, isr->sav);
711 ip6 = mtod(m, struct ip6_hdr *);
713 state->ro = &isr->sav->sah->sa_route;
714 state->dst = (struct sockaddr *)&state->ro->ro_dst;
715 dst6 = (struct sockaddr_in6 *)state->dst;
717 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
718 || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
719 RTFREE(state->ro->ro_rt);
720 state->ro->ro_rt = NULL;
722 if (state->ro->ro_rt == 0) {
723 bzero(dst6, sizeof(*dst6));
724 dst6->sin6_family = AF_INET6;
725 dst6->sin6_len = sizeof(*dst6);
726 dst6->sin6_addr = ip6->ip6_dst;
729 if (state->ro->ro_rt == 0) {
730 ip6stat.ip6s_noroute++;
731 newipsecstat.ips_out_noroute++;
732 error = EHOSTUNREACH;
736 /* adjust state->dst if tunnel endpoint is offlink */
737 if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
738 state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
739 dst6 = (struct sockaddr_in6 *)state->dst;
743 m = ipsec6_splithdr(m);
745 newipsecstat.ips_out_nomem++;
749 ip6 = mtod(m, struct ip6_hdr *);
750 return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
751 sizeof (struct ip6_hdr),
752 offsetof(struct ip6_hdr, ip6_nxt));