Home | History | Annotate | Line # | Download | only in netipsec
ipsecif.c revision 1.2.2.8
      1 /*	$NetBSD: ipsecif.c,v 1.2.2.8 2019/01/18 08:50:58 pgoyette Exp $  */
      2 
      3 /*
      4  * Copyright (c) 2017 Internet Initiative Japan Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: ipsecif.c,v 1.2.2.8 2019/01/18 08:50:58 pgoyette Exp $");
     31 
     32 #ifdef _KERNEL_OPT
     33 #include "opt_inet.h"
     34 #include "opt_ipsec.h"
     35 #endif
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/socket.h>
     40 #include <sys/sockio.h>
     41 #include <sys/mbuf.h>
     42 #include <sys/errno.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/syslog.h>
     45 #include <sys/kernel.h>
     46 
     47 #include <net/if.h>
     48 #include <net/route.h>
     49 
     50 #include <netinet/in.h>
     51 #include <netinet/in_systm.h>
     52 #include <netinet/ip.h>
     53 #include <netinet/ip_var.h>
     54 #include <netinet/in_var.h>
     55 #include <netinet/ip_encap.h>
     56 #include <netinet/ip_ecn.h>
     57 #include <netinet/ip_private.h>
     58 #include <netinet/udp.h>
     59 
     60 #ifdef INET6
     61 #include <netinet/ip6.h>
     62 #include <netinet6/ip6_var.h>
     63 #include <netinet6/ip6_private.h>
     64 #include <netinet6/in6_var.h>
     65 #include <netinet6/ip6protosw.h> /* for struct ip6ctlparam */
     66 #include <netinet/ip_ecn.h>
     67 #endif
     68 
     69 #include <netipsec/key.h>
     70 #include <netipsec/ipsecif.h>
     71 
     72 #include <net/if_ipsec.h>
     73 
     74 static int ipsecif_set_natt_ports(struct ipsec_variant *, struct mbuf *);
     75 static void ipsecif4_input(struct mbuf *, int, int, void *);
     76 static int ipsecif4_output(struct ipsec_variant *, int, struct mbuf *);
     77 static int ipsecif4_filter4(const struct ip *, struct ipsec_variant *,
     78 	struct ifnet *);
     79 
     80 #ifdef INET6
     81 static int ipsecif6_input(struct mbuf **, int *, int, void *);
     82 static int ipsecif6_output(struct ipsec_variant *, int, struct mbuf *);
     83 static int ipsecif6_filter6(const struct ip6_hdr *, struct ipsec_variant *,
     84 	struct ifnet *);
     85 #endif
     86 
     87 static int ip_ipsec_ttl = IPSEC_TTL;
     88 static int ip_ipsec_copy_tos = 0;
     89 #ifdef INET6
     90 static int ip6_ipsec_hlim = IPSEC_HLIM;
     91 static int ip6_ipsec_pmtu = 0; /* XXX: per interface configuration?? */
     92 static int ip6_ipsec_copy_tos = 0;
     93 #endif
     94 
     95 static const struct encapsw ipsecif4_encapsw = {
     96 	.encapsw4 = {
     97 		.pr_input = ipsecif4_input,
     98 		.pr_ctlinput = NULL,
     99 	}
    100 };
    101 
    102 #ifdef INET6
    103 static const struct encapsw ipsecif6_encapsw;
    104 #endif
    105 
    106 static int
    107 ipsecif_set_natt_ports(struct ipsec_variant *var, struct mbuf *m)
    108 {
    109 
    110 	KASSERT(if_ipsec_heldref_variant(var));
    111 
    112 	if (var->iv_sport || var->iv_dport) {
    113 		struct m_tag *mtag;
    114 
    115 		mtag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
    116 		    sizeof(uint16_t) + sizeof(uint16_t), M_DONTWAIT);
    117 		if (mtag) {
    118 			uint16_t *natt_port;
    119 
    120 			natt_port = (uint16_t *)(mtag + 1);
    121 			natt_port[0] = var->iv_dport;
    122 			natt_port[1] = var->iv_sport;
    123 			m_tag_prepend(m, mtag);
    124 		} else {
    125 			return ENOBUFS;
    126 		}
    127 	}
    128 
    129 	return 0;
    130 }
    131 
    132 static struct mbuf *
    133 ipsecif4_prepend_hdr(struct ipsec_variant *var, struct mbuf *m,
    134     uint8_t proto, uint8_t tos)
    135 {
    136 	struct ip *ip;
    137 	struct sockaddr_in *src, *dst;
    138 
    139 	src = satosin(var->iv_psrc);
    140 	dst = satosin(var->iv_pdst);
    141 
    142 	if (in_nullhost(src->sin_addr) || in_nullhost(src->sin_addr) ||
    143 	    src->sin_addr.s_addr == INADDR_BROADCAST ||
    144 	    dst->sin_addr.s_addr == INADDR_BROADCAST) {
    145 		m_freem(m);
    146 		return NULL;
    147 	}
    148 	m->m_flags &= ~M_BCAST;
    149 
    150 	if (IN_MULTICAST(src->sin_addr.s_addr) ||
    151 	    IN_MULTICAST(dst->sin_addr.s_addr)) {
    152 		m_freem(m);
    153 		return NULL;
    154 	}
    155 
    156 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    157 	if (m && M_UNWRITABLE(m, sizeof(struct ip)))
    158 		m = m_pullup(m, sizeof(struct ip));
    159 	if (m == NULL)
    160 		return NULL;
    161 
    162 	ip = mtod(m, struct ip *);
    163 	ip->ip_v = IPVERSION;
    164 	ip->ip_off = htons(0);
    165 	if (m->m_pkthdr.len < IP_MINFRAGSIZE)
    166 		ip->ip_id = 0;
    167 	else
    168 		ip->ip_id = ip_newid(NULL);
    169 	ip->ip_hl = sizeof(*ip) >> 2;
    170 	if (ip_ipsec_copy_tos)
    171 		ip->ip_tos = tos;
    172 	else
    173 		ip->ip_tos = 0;
    174 	ip->ip_sum = 0;
    175 	ip->ip_src = src->sin_addr;
    176 	ip->ip_dst = dst->sin_addr;
    177 	ip->ip_p = proto;
    178 	ip->ip_ttl = ip_ipsec_ttl;
    179 	ip->ip_len = htons(m->m_pkthdr.len);
    180 #ifndef IPSEC_TX_TOS_CLEAR
    181 	struct ifnet *ifp = &var->iv_softc->ipsec_if;
    182 	if (ifp->if_flags & IFF_ECN)
    183 		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
    184 	else
    185 		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
    186 #endif
    187 
    188 	return m;
    189 }
    190 
    191 static int
    192 ipsecif4_needfrag(struct mbuf *m, struct ipsecrequest *isr)
    193 {
    194 	struct ip ip0;
    195 	struct ip *ip;
    196 	int mtu;
    197 	struct secasvar *sav;
    198 
    199 	sav = key_lookup_sa_bysaidx(&isr->saidx);
    200 	if (sav == NULL)
    201 		return 0;
    202 
    203 	if (!(sav->natt_type & UDP_ENCAP_ESPINUDP)) {
    204 		mtu = 0;
    205 		goto out;
    206 	}
    207 
    208 	if (m->m_len < sizeof(struct ip)) {
    209 		m_copydata(m, 0, sizeof(ip0), &ip0);
    210 		ip = &ip0;
    211 	} else {
    212 		ip = mtod(m, struct ip *);
    213 	}
    214 	mtu = sav->esp_frag;
    215 	if (ntohs(ip->ip_len) <= mtu)
    216 		mtu = 0;
    217 
    218 out:
    219 	KEY_SA_UNREF(&sav);
    220 	return mtu;
    221 }
    222 
    223 static struct mbuf *
    224 ipsecif4_flowinfo(struct mbuf *m, int family, int *proto0, u_int8_t *tos0)
    225 {
    226 	const struct ip *ip;
    227 	int proto;
    228 	int tos;
    229 
    230 	KASSERT(proto0 != NULL);
    231 	KASSERT(tos0 != NULL);
    232 
    233 	switch (family) {
    234 	case AF_INET:
    235 		proto = IPPROTO_IPV4;
    236 		if (m->m_len < sizeof(*ip)) {
    237 			m = m_pullup(m, sizeof(*ip));
    238 			if (m == NULL) {
    239 				*tos0 = 0;
    240 				*proto0 = 0;
    241 				return NULL;
    242 			}
    243 		}
    244 		ip = mtod(m, const struct ip *);
    245 		tos = ip->ip_tos;
    246 		/* TODO: support ALTQ for innner packet */
    247 		break;
    248 #ifdef INET6
    249 	case AF_INET6: {
    250 		const struct ip6_hdr *ip6;
    251 		proto = IPPROTO_IPV6;
    252 		if (m->m_len < sizeof(*ip6)) {
    253 			m = m_pullup(m, sizeof(*ip6));
    254 			if (m == NULL) {
    255 				*tos0 = 0;
    256 				*proto0 = 0;
    257 				return NULL;
    258 			}
    259 		}
    260 		ip6 = mtod(m, const struct ip6_hdr *);
    261 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    262 		/* TODO: support ALTQ for innner packet */
    263 		break;
    264 	}
    265 #endif /* INET6 */
    266 	default:
    267 		*tos0 = 0;
    268 		*proto0 = 0;
    269 		return NULL;
    270 	}
    271 
    272 	*proto0 = proto;
    273 	*tos0 = tos;
    274 	return m;
    275 }
    276 
    277 static int
    278 ipsecif4_fragout(struct ipsec_variant *var, int family, struct mbuf *m, int mtu)
    279 {
    280 	struct ifnet *ifp = &var->iv_softc->ipsec_if;
    281 	struct mbuf *next;
    282 	struct m_tag *mtag;
    283 	int error;
    284 
    285 	KASSERT(if_ipsec_heldref_variant(var));
    286 
    287 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS);
    288 	if (mtag)
    289 		m_tag_delete(m, mtag);
    290 
    291 	/* consider new IP header prepended in ipsecif4_output() */
    292 	if (mtu <= sizeof(struct ip)) {
    293 		m_freem(m);
    294 		return ENETUNREACH;
    295 	}
    296 	m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
    297 	error = ip_fragment(m, ifp, mtu - sizeof(struct ip));
    298 	if (error)
    299 		return error;
    300 
    301 	for (error = 0; m; m = next) {
    302 		next = m->m_nextpkt;
    303 		m->m_nextpkt = NULL;
    304 		if (error) {
    305 			m_freem(m);
    306 			continue;
    307 		}
    308 
    309 		error = ipsecif4_output(var, family, m);
    310 	}
    311 	if (error == 0)
    312 		IP_STATINC(IP_STAT_FRAGMENTED);
    313 
    314 	return error;
    315 }
    316 
    317 int
    318 ipsecif4_encap_func(struct mbuf *m, struct ip *ip, struct ipsec_variant *var)
    319 {
    320 	struct m_tag *mtag;
    321 	struct sockaddr_in *src, *dst;
    322 	u_int16_t src_port = 0;
    323 	u_int16_t dst_port = 0;
    324 
    325 	KASSERT(var != NULL);
    326 
    327 	src = satosin(var->iv_psrc);
    328 	dst = satosin(var->iv_pdst);
    329 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS);
    330 	if (mtag) {
    331 		u_int16_t *ports;
    332 
    333 		ports = (u_int16_t *)(mtag + 1);
    334 		src_port = ports[0];
    335 		dst_port = ports[1];
    336 	}
    337 
    338 	/* address match */
    339 	if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
    340 	    dst->sin_addr.s_addr != ip->ip_src.s_addr)
    341 		return 0;
    342 
    343 	/* UDP encap? */
    344 	if (mtag == NULL && var->iv_sport == 0 && var->iv_dport == 0)
    345 		goto match;
    346 
    347 	/* port match */
    348 	if (src_port != var->iv_dport ||
    349 	    dst_port != var->iv_sport) {
    350 #ifdef DEBUG
    351 		printf("%s: port mismatch: pkt(%u, %u), if(%u, %u)\n",
    352 		    __func__, ntohs(src_port), ntohs(dst_port),
    353 		    ntohs(var->iv_sport), ntohs(var->iv_dport));
    354 #endif
    355 		return 0;
    356 	}
    357 
    358 match:
    359 	/*
    360 	 * hide NAT-T information from encapsulated traffics.
    361 	 * they don't know about IPsec.
    362 	 */
    363 	if (mtag)
    364 		m_tag_delete(m, mtag);
    365 	return sizeof(src->sin_addr) + sizeof(dst->sin_addr);
    366 }
    367 
    368 static int
    369 ipsecif4_output(struct ipsec_variant *var, int family, struct mbuf *m)
    370 {
    371 	struct secpolicy *sp = NULL;
    372 	u_int8_t tos;
    373 	int proto;
    374 	int error;
    375 	int mtu;
    376 	u_long sa_mtu = 0;
    377 
    378 	KASSERT(if_ipsec_heldref_variant(var));
    379 	KASSERT(if_ipsec_variant_is_configured(var));
    380 	KASSERT(var->iv_psrc->sa_family == AF_INET);
    381 	KASSERT(var->iv_pdst->sa_family == AF_INET);
    382 
    383 	sp = IV_SP_OUT(var);
    384 	KASSERT(sp != NULL);
    385 	/*
    386 	 * The SPs in ipsec_variant are prevented from freed by
    387 	 * ipsec_variant->iv_psref. So, KEY_SP_REF() is unnecessary here.
    388 	 */
    389 
    390 	KASSERT(sp->policy != IPSEC_POLICY_NONE);
    391 	KASSERT(sp->policy != IPSEC_POLICY_ENTRUST);
    392 	KASSERT(sp->policy != IPSEC_POLICY_BYPASS);
    393 	if (sp->policy != IPSEC_POLICY_IPSEC) {
    394 		m_freem(m);
    395 		error = ENETUNREACH;
    396 		goto done;
    397 	}
    398 
    399 	/* get flowinfo */
    400 	m = ipsecif4_flowinfo(m, family, &proto, &tos);
    401 	if (m == NULL) {
    402 		error = ENETUNREACH;
    403 		goto done;
    404 	}
    405 
    406 	/* prepend new IP header */
    407 	m = ipsecif4_prepend_hdr(var, m, proto, tos);
    408 	if (m == NULL) {
    409 		error = ENETUNREACH;
    410 		goto done;
    411 	}
    412 
    413 	/*
    414 	 * Normal netipsec's NAT-T fragmentation is done in ip_output().
    415 	 * See "natt_frag" processing.
    416 	 * However, ipsec(4) interface's one is not done in the same way,
    417 	 * so we must do NAT-T fragmentation by own code.
    418 	 */
    419 	/* NAT-T ESP fragmentation */
    420 	mtu = ipsecif4_needfrag(m, sp->req);
    421 	if (mtu > 0)
    422 		return ipsecif4_fragout(var, family, m, mtu);
    423 
    424 	/* set NAT-T ports */
    425 	error = ipsecif_set_natt_ports(var, m);
    426 	if (error) {
    427 		m_freem(m);
    428 		goto done;
    429 	}
    430 
    431 	/* IPsec output */
    432 	IP_STATINC(IP_STAT_LOCALOUT);
    433 	error = ipsec4_process_packet(m, sp->req, &sa_mtu);
    434 	if (error == ENOENT)
    435 		error = 0;
    436 	/*
    437 	 * frangmentation is already done in ipsecif4_fragout(),
    438 	 * so ipsec4_process_packet() must not do fragmentation here.
    439 	 */
    440 	KASSERT(sa_mtu == 0);
    441 
    442 done:
    443 	return error;
    444 }
    445 
    446 #ifdef INET6
    447 int
    448 ipsecif6_encap_func(struct mbuf *m, struct ip6_hdr *ip6, struct ipsec_variant *var)
    449 {
    450 	struct m_tag *mtag;
    451 	struct sockaddr_in6 *src, *dst;
    452 	u_int16_t src_port = 0;
    453 	u_int16_t dst_port = 0;
    454 
    455 	KASSERT(var != NULL);
    456 
    457 	src = satosin6(var->iv_psrc);
    458 	dst = satosin6(var->iv_pdst);
    459 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS);
    460 	if (mtag) {
    461 		u_int16_t *ports;
    462 
    463 		ports = (u_int16_t *)(mtag + 1);
    464 		src_port = ports[0];
    465 		dst_port = ports[1];
    466 	}
    467 
    468 	/* address match */
    469 	if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
    470 	    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
    471 		return 0;
    472 
    473 	/* UDP encap? */
    474 	if (mtag == NULL && var->iv_sport == 0 && var->iv_dport == 0)
    475 		goto match;
    476 
    477 	/* port match */
    478 	if (src_port != var->iv_dport ||
    479 	    dst_port != var->iv_sport) {
    480 #ifdef DEBUG
    481 		printf("%s: port mismatch: pkt(%u, %u), if(%u, %u)\n",
    482 		    __func__, ntohs(src_port), ntohs(dst_port),
    483 		    ntohs(var->iv_sport), ntohs(var->iv_dport));
    484 #endif
    485 		return 0;
    486 	}
    487 
    488 match:
    489 	/*
    490 	 * hide NAT-T information from encapsulated traffics.
    491 	 * they don't know about IPsec.
    492 	 */
    493 	if (mtag)
    494 		m_tag_delete(m, mtag);
    495 	return sizeof(src->sin6_addr) + sizeof(dst->sin6_addr);
    496 }
    497 
    498 static int
    499 ipsecif6_output(struct ipsec_variant *var, int family, struct mbuf *m)
    500 {
    501 	struct ifnet *ifp = &var->iv_softc->ipsec_if;
    502 	struct ipsec_softc *sc = ifp->if_softc;
    503 	struct ipsec_ro *iro;
    504 	struct rtentry *rt;
    505 	struct sockaddr_in6 *sin6_src;
    506 	struct sockaddr_in6 *sin6_dst;
    507 	struct ip6_hdr *ip6;
    508 	int proto, error;
    509 	u_int8_t itos, otos;
    510 	union {
    511 		struct sockaddr		dst;
    512 		struct sockaddr_in6	dst6;
    513 	} u;
    514 
    515 	KASSERT(if_ipsec_heldref_variant(var));
    516 	KASSERT(if_ipsec_variant_is_configured(var));
    517 
    518 	sin6_src = satosin6(var->iv_psrc);
    519 	sin6_dst = satosin6(var->iv_pdst);
    520 
    521 	KASSERT(sin6_src->sin6_family == AF_INET6);
    522 	KASSERT(sin6_dst->sin6_family == AF_INET6);
    523 
    524 	switch (family) {
    525 #ifdef INET
    526 	case AF_INET:
    527 	    {
    528 		struct ip *ip;
    529 
    530 		proto = IPPROTO_IPV4;
    531 		if (m->m_len < sizeof(*ip)) {
    532 			m = m_pullup(m, sizeof(*ip));
    533 			if (m == NULL)
    534 				return ENOBUFS;
    535 		}
    536 		ip = mtod(m, struct ip *);
    537 		itos = ip->ip_tos;
    538 		/* TODO: support ALTQ for innner packet */
    539 		break;
    540 	    }
    541 #endif /* INET */
    542 	case AF_INET6:
    543 	    {
    544 		struct ip6_hdr *xip6;
    545 		proto = IPPROTO_IPV6;
    546 		if (m->m_len < sizeof(*xip6)) {
    547 			m = m_pullup(m, sizeof(*xip6));
    548 			if (m == NULL)
    549 				return ENOBUFS;
    550 		}
    551 		xip6 = mtod(m, struct ip6_hdr *);
    552 		itos = (ntohl(xip6->ip6_flow) >> 20) & 0xff;
    553 		/* TODO: support ALTQ for innner packet */
    554 		break;
    555 	    }
    556 	default:
    557 		m_freem(m);
    558 		return EAFNOSUPPORT;
    559 	}
    560 
    561 	/* prepend new IP header */
    562 	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
    563 	if (m && M_UNWRITABLE(m, sizeof(struct ip6_hdr)))
    564 		m = m_pullup(m, sizeof(struct ip6_hdr));
    565 	if (m == NULL)
    566 		return ENOBUFS;
    567 
    568 	ip6 = mtod(m, struct ip6_hdr *);
    569 	ip6->ip6_flow	= 0;
    570 	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
    571 	ip6->ip6_vfc	|= IPV6_VERSION;
    572 #if 0	/* ip6->ip6_plen will be filled by ip6_output */
    573 	ip6->ip6_plen	= htons((u_short)m->m_pkthdr.len - sizeof(*ip6));
    574 #endif
    575 	ip6->ip6_nxt	= proto;
    576 	ip6->ip6_hlim	= ip6_ipsec_hlim;
    577 	ip6->ip6_src	= sin6_src->sin6_addr;
    578 	/* bidirectional configured tunnel mode */
    579 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) {
    580 		ip6->ip6_dst = sin6_dst->sin6_addr;
    581 	} else  {
    582 		m_freem(m);
    583 		return ENETUNREACH;
    584 	}
    585 #ifndef IPSEC_TX_TOS_CLEAR
    586 	if (ifp->if_flags & IFF_ECN)
    587 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
    588 	else
    589 		ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
    590 
    591 	if (!ip6_ipsec_copy_tos)
    592 		otos = 0;
    593 #else
    594 	if (ip6_ipsec_copy_tos)
    595 		otos = itos;
    596 	else
    597 		otos = 0;
    598 #endif
    599 	ip6->ip6_flow &= ~ntohl(0xff00000);
    600 	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
    601 
    602 	sockaddr_in6_init(&u.dst6, &sin6_dst->sin6_addr, 0, 0, 0);
    603 
    604 	iro = percpu_getref(sc->ipsec_ro_percpu);
    605 	mutex_enter(iro->ir_lock);
    606 	if ((rt = rtcache_lookup(&iro->ir_ro, &u.dst)) == NULL) {
    607 		mutex_exit(iro->ir_lock);
    608 		percpu_putref(sc->ipsec_ro_percpu);
    609 		m_freem(m);
    610 		return ENETUNREACH;
    611 	}
    612 
    613 	if (rt->rt_ifp == ifp) {
    614 		rtcache_unref(rt, &iro->ir_ro);
    615 		rtcache_free(&iro->ir_ro);
    616 		mutex_exit(iro->ir_lock);
    617 		percpu_putref(sc->ipsec_ro_percpu);
    618 		m_freem(m);
    619 		return ENETUNREACH;
    620 	}
    621 	rtcache_unref(rt, &iro->ir_ro);
    622 
    623 	/* set NAT-T ports */
    624 	error = ipsecif_set_natt_ports(var, m);
    625 	if (error) {
    626 		m_freem(m);
    627 		goto out;
    628 	}
    629 
    630 	/*
    631 	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
    632 	 * it is too painful to ask for resend of inner packet, to achieve
    633 	 * path MTU discovery for encapsulated packets.
    634 	 */
    635 	error = ip6_output(m, 0, &iro->ir_ro,
    636 	    ip6_ipsec_pmtu ? 0 : IPV6_MINMTU, 0, NULL, NULL);
    637 
    638 out:
    639 	if (error)
    640 		rtcache_free(&iro->ir_ro);
    641 	mutex_exit(iro->ir_lock);
    642 	percpu_putref(sc->ipsec_ro_percpu);
    643 
    644 	return error;
    645 }
    646 #endif /* INET6 */
    647 
    648 static void
    649 ipsecif4_input(struct mbuf *m, int off, int proto, void *eparg)
    650 {
    651 	struct ifnet *ipsecp;
    652 	struct ipsec_softc *sc = eparg;
    653 	struct ipsec_variant *var;
    654 	const struct ip *ip;
    655 	int af;
    656 #ifndef IPSEC_TX_TOS_CLEAR
    657 	u_int8_t otos;
    658 #endif
    659 	struct psref psref_rcvif;
    660 	struct psref psref_var;
    661 	struct ifnet *rcvif;
    662 
    663 	KASSERT(sc != NULL);
    664 
    665 	ipsecp = &sc->ipsec_if;
    666 	if ((ipsecp->if_flags & IFF_UP) == 0) {
    667 		m_freem(m);
    668 		ip_statinc(IP_STAT_NOIPSEC);
    669 		return;
    670 	}
    671 
    672 	var = if_ipsec_getref_variant(sc, &psref_var);
    673 	if (if_ipsec_variant_is_unconfigured(var)) {
    674 		if_ipsec_putref_variant(var, &psref_var);
    675 		m_freem(m);
    676 		ip_statinc(IP_STAT_NOIPSEC);
    677 		return;
    678 	}
    679 
    680 	ip = mtod(m, const struct ip *);
    681 
    682 	rcvif = m_get_rcvif_psref(m, &psref_rcvif);
    683 	if (rcvif == NULL || !ipsecif4_filter4(ip, var, rcvif)) {
    684 		m_put_rcvif_psref(rcvif, &psref_rcvif);
    685 		if_ipsec_putref_variant(var, &psref_var);
    686 		m_freem(m);
    687 		ip_statinc(IP_STAT_NOIPSEC);
    688 		return;
    689 	}
    690 	m_put_rcvif_psref(rcvif, &psref_rcvif);
    691 	if_ipsec_putref_variant(var, &psref_var);
    692 #ifndef IPSEC_TX_TOS_CLEAR
    693 	otos = ip->ip_tos;
    694 #endif
    695 	m_adj(m, off);
    696 
    697 	switch (proto) {
    698 	case IPPROTO_IPV4:
    699 	    {
    700 		struct ip *xip;
    701 		af = AF_INET;
    702 		if (M_UNWRITABLE(m, sizeof(*xip))) {
    703 			m = m_pullup(m, sizeof(*xip));
    704 			if (m == NULL)
    705 				return;
    706 		}
    707 		xip = mtod(m, struct ip *);
    708 #ifndef IPSEC_TX_TOS_CLEAR
    709 		if (ipsecp->if_flags & IFF_ECN)
    710 			ip_ecn_egress(ECN_ALLOWED, &otos, &xip->ip_tos);
    711 		else
    712 			ip_ecn_egress(ECN_NOCARE, &otos, &xip->ip_tos);
    713 #endif
    714 		break;
    715 	    }
    716 #ifdef INET6
    717 	case IPPROTO_IPV6:
    718 	    {
    719 		struct ip6_hdr *ip6;
    720 		u_int8_t itos;
    721 		af = AF_INET6;
    722 		if (M_UNWRITABLE(m, sizeof(*ip6))) {
    723 			m = m_pullup(m, sizeof(*ip6));
    724 			if (m == NULL)
    725 				return;
    726 		}
    727 		ip6 = mtod(m, struct ip6_hdr *);
    728 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    729 #ifndef IPSEC_TX_TOS_CLEAR
    730 		if (ipsecp->if_flags & IFF_ECN)
    731 			ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
    732 		else
    733 			ip_ecn_egress(ECN_NOCARE, &otos, &itos);
    734 #endif
    735 		ip6->ip6_flow &= ~htonl(0xff << 20);
    736 		ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
    737 		break;
    738 	    }
    739 #endif /* INET6 */
    740 	default:
    741 		ip_statinc(IP_STAT_NOIPSEC);
    742 		m_freem(m);
    743 		return;
    744 	}
    745 	if_ipsec_input(m, af, ipsecp);
    746 
    747 	return;
    748 }
    749 
    750 /*
    751  * validate and filter the pakcet
    752  */
    753 static int
    754 ipsecif4_filter4(const struct ip *ip, struct ipsec_variant *var,
    755     struct ifnet *ifp)
    756 {
    757 	struct sockaddr_in *src, *dst;
    758 
    759 	src = satosin(var->iv_psrc);
    760 	dst = satosin(var->iv_pdst);
    761 
    762 	return in_tunnel_validate(ip, src->sin_addr, dst->sin_addr);
    763 }
    764 
    765 #ifdef INET6
    766 static int
    767 ipsecif6_input(struct mbuf **mp, int *offp, int proto, void *eparg)
    768 {
    769 	struct mbuf *m = *mp;
    770 	struct ifnet *ipsecp;
    771 	struct ipsec_softc *sc = eparg;
    772 	struct ipsec_variant *var;
    773 	struct ip6_hdr *ip6;
    774 	int af = 0;
    775 #ifndef IPSEC_TX_TOS_CLEAR
    776 	u_int32_t otos;
    777 #endif
    778 	struct psref psref_rcvif;
    779 	struct psref psref_var;
    780 	struct ifnet *rcvif;
    781 
    782 	KASSERT(eparg != NULL);
    783 
    784 	ipsecp = &sc->ipsec_if;
    785 	if ((ipsecp->if_flags & IFF_UP) == 0) {
    786 		m_freem(m);
    787 		IP6_STATINC(IP6_STAT_NOIPSEC);
    788 		return IPPROTO_DONE;
    789 	}
    790 
    791 	var = if_ipsec_getref_variant(sc, &psref_var);
    792 	if (if_ipsec_variant_is_unconfigured(var)) {
    793 		if_ipsec_putref_variant(var, &psref_var);
    794 		m_freem(m);
    795 		IP6_STATINC(IP6_STAT_NOIPSEC);
    796 		return IPPROTO_DONE;
    797 	}
    798 
    799 	ip6 = mtod(m, struct ip6_hdr *);
    800 
    801 	rcvif = m_get_rcvif_psref(m, &psref_rcvif);
    802 	if (rcvif == NULL || !ipsecif6_filter6(ip6, var, rcvif)) {
    803 		m_put_rcvif_psref(rcvif, &psref_rcvif);
    804 		if_ipsec_putref_variant(var, &psref_var);
    805 		m_freem(m);
    806 		IP6_STATINC(IP6_STAT_NOIPSEC);
    807 		return IPPROTO_DONE;
    808 	}
    809 	m_put_rcvif_psref(rcvif, &psref_rcvif);
    810 	if_ipsec_putref_variant(var, &psref_var);
    811 
    812 #ifndef IPSEC_TX_TOS_CLEAR
    813 	otos = ip6->ip6_flow;
    814 #endif
    815 	m_adj(m, *offp);
    816 
    817 	switch (proto) {
    818 #ifdef INET
    819 	case IPPROTO_IPV4:
    820 	    {
    821 		af = AF_INET;
    822 #ifndef IPSEC_TX_TOS_CLEAR
    823 		struct ip *ip;
    824 		u_int8_t otos8;
    825 		otos8 = (ntohl(otos) >> 20) & 0xff;
    826 
    827 		if (M_UNWRITABLE(m, sizeof(*ip))) {
    828 			m = m_pullup(m, sizeof(*ip));
    829 			if (m == NULL)
    830 				return IPPROTO_DONE;
    831 		}
    832 		ip = mtod(m, struct ip *);
    833 		if (ipsecp->if_flags & IFF_ECN)
    834 			ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos);
    835 		else
    836 			ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
    837 #endif
    838 		break;
    839 	    }
    840 #endif /* INET */
    841 	case IPPROTO_IPV6:
    842 	    {
    843 		af = AF_INET6;
    844 #ifndef IPSEC_TX_TOS_CLEAR
    845 		struct ip6_hdr *xip6;
    846 
    847 		if (M_UNWRITABLE(m, sizeof(*xip6))) {
    848 			m = m_pullup(m, sizeof(*xip6));
    849 			if (m == NULL)
    850 				return IPPROTO_DONE;
    851 		}
    852 		xip6 = mtod(m, struct ip6_hdr *);
    853 		if (ipsecp->if_flags & IFF_ECN)
    854 			ip6_ecn_egress(ECN_ALLOWED, &otos, &xip6->ip6_flow);
    855 		else
    856 			ip6_ecn_egress(ECN_NOCARE, &otos, &xip6->ip6_flow);
    857 		break;
    858 #endif
    859 	    }
    860 	default:
    861 		IP6_STATINC(IP6_STAT_NOIPSEC);
    862 		m_freem(m);
    863 		return IPPROTO_DONE;
    864 	}
    865 
    866 	if_ipsec_input(m, af, ipsecp);
    867 	return IPPROTO_DONE;
    868 }
    869 
    870 /*
    871  * validate and filter the packet.
    872  */
    873 static int
    874 ipsecif6_filter6(const struct ip6_hdr *ip6, struct ipsec_variant *var,
    875     struct ifnet *ifp)
    876 {
    877 	struct sockaddr_in6 *src, *dst;
    878 
    879 	src = satosin6(var->iv_psrc);
    880 	dst = satosin6(var->iv_pdst);
    881 
    882 	return in6_tunnel_validate(ip6, &src->sin6_addr, &dst->sin6_addr);
    883 }
    884 #endif /* INET6 */
    885 
    886 int
    887 ipsecif4_attach(struct ipsec_variant *var)
    888 {
    889 	struct ipsec_softc *sc = var->iv_softc;
    890 
    891 	KASSERT(if_ipsec_variant_is_configured(var));
    892 
    893 	if (var->iv_encap_cookie4 != NULL)
    894 		return EALREADY;
    895 	var->iv_encap_cookie4 = encap_attach_func(AF_INET, -1, if_ipsec_encap_func,
    896 	    &ipsecif4_encapsw, sc);
    897 	if (var->iv_encap_cookie4 == NULL)
    898 		return EEXIST;
    899 
    900 	var->iv_output = ipsecif4_output;
    901 	return 0;
    902 }
    903 
    904 int
    905 ipsecif4_detach(struct ipsec_variant *var)
    906 {
    907 	int error;
    908 
    909 	if (var->iv_encap_cookie4 == NULL)
    910 		return 0;
    911 
    912 	var->iv_output = NULL;
    913 	error = encap_detach(var->iv_encap_cookie4);
    914 	if (error == 0)
    915 		var->iv_encap_cookie4 = NULL;
    916 
    917 	return error;
    918 }
    919 
    920 #ifdef INET6
    921 int
    922 ipsecif6_attach(struct ipsec_variant *var)
    923 {
    924 	struct sockaddr_in6 mask6;
    925 	struct ipsec_softc *sc = var->iv_softc;
    926 
    927 	KASSERT(if_ipsec_variant_is_configured(var));
    928 	KASSERT(var->iv_encap_cookie6 == NULL);
    929 
    930 	memset(&mask6, 0, sizeof(mask6));
    931 	mask6.sin6_len = sizeof(struct sockaddr_in6);
    932 	mask6.sin6_addr.s6_addr32[0] = mask6.sin6_addr.s6_addr32[1] =
    933 	mask6.sin6_addr.s6_addr32[2] = mask6.sin6_addr.s6_addr32[3] = ~0;
    934 
    935 	var->iv_encap_cookie6 = encap_attach_func(AF_INET6, -1, if_ipsec_encap_func,
    936 	    &ipsecif6_encapsw, sc);
    937 	if (var->iv_encap_cookie6 == NULL)
    938 		return EEXIST;
    939 
    940 	var->iv_output = ipsecif6_output;
    941 	return 0;
    942 }
    943 
    944 static void
    945 ipsecif6_rtcache_free_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
    946 {
    947 	struct ipsec_ro *iro = p;
    948 
    949 	mutex_enter(iro->ir_lock);
    950 	rtcache_free(&iro->ir_ro);
    951 	mutex_exit(iro->ir_lock);
    952 }
    953 
    954 int
    955 ipsecif6_detach(struct ipsec_variant *var)
    956 {
    957 	struct ipsec_softc *sc = var->iv_softc;
    958 	int error;
    959 
    960 	KASSERT(var->iv_encap_cookie6 != NULL);
    961 
    962 	percpu_foreach(sc->ipsec_ro_percpu, ipsecif6_rtcache_free_pc, NULL);
    963 
    964 	var->iv_output = NULL;
    965 	error = encap_detach(var->iv_encap_cookie6);
    966 	if (error == 0)
    967 		var->iv_encap_cookie6 = NULL;
    968 	return error;
    969 }
    970 
    971 void *
    972 ipsecif6_ctlinput(int cmd, const struct sockaddr *sa, void *d, void *eparg)
    973 {
    974 	struct ipsec_softc *sc = eparg;
    975 	struct ip6ctlparam *ip6cp = NULL;
    976 	struct ip6_hdr *ip6;
    977 	const struct sockaddr_in6 *dst6;
    978 	struct ipsec_ro *iro;
    979 
    980 	if (sa->sa_family != AF_INET6 ||
    981 	    sa->sa_len != sizeof(struct sockaddr_in6))
    982 		return NULL;
    983 
    984 	if ((unsigned)cmd >= PRC_NCMDS)
    985 		return NULL;
    986 	if (cmd == PRC_HOSTDEAD)
    987 		d = NULL;
    988 	else if (inet6ctlerrmap[cmd] == 0)
    989 		return NULL;
    990 
    991 	/* if the parameter is from icmp6, decode it. */
    992 	if (d != NULL) {
    993 		ip6cp = (struct ip6ctlparam *)d;
    994 		ip6 = ip6cp->ip6c_ip6;
    995 	} else {
    996 		ip6 = NULL;
    997 	}
    998 
    999 	if (!ip6)
   1000 		return NULL;
   1001 
   1002 	iro = percpu_getref(sc->ipsec_ro_percpu);
   1003 	mutex_enter(iro->ir_lock);
   1004 	dst6 = satocsin6(rtcache_getdst(&iro->ir_ro));
   1005 	/* XXX scope */
   1006 	if (dst6 == NULL)
   1007 		;
   1008 	else if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst6->sin6_addr))
   1009 		/* flush route cache */
   1010 		rtcache_free(&iro->ir_ro);
   1011 
   1012 	mutex_exit(iro->ir_lock);
   1013 	percpu_putref(sc->ipsec_ro_percpu);
   1014 
   1015 	return NULL;
   1016 }
   1017 
   1018 ENCAP_PR_WRAP_CTLINPUT(ipsecif6_ctlinput)
   1019 #define	ipsecif6_ctlinput	ipsecif6_ctlinput_wrapper
   1020 
   1021 static const struct encapsw ipsecif6_encapsw = {
   1022 	.encapsw6 = {
   1023 		.pr_input = ipsecif6_input,
   1024 		.pr_ctlinput = ipsecif6_ctlinput,
   1025 	}
   1026 };
   1027 #endif /* INET6 */
   1028