Home | History | Annotate | Line # | Download | only in netipsec
ipsecif.c revision 1.4
      1 /*	$NetBSD: ipsecif.c,v 1.4 2018/03/09 11:05:21 knakahara 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.4 2018/03/09 11:05:21 knakahara 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 void ipsecif4_input(struct mbuf *, int, int, void *);
     75 static int ipsecif4_output(struct ipsec_variant *, int, struct mbuf *);
     76 static int ipsecif4_filter4(const struct ip *, struct ipsec_variant *,
     77 	struct ifnet *);
     78 
     79 #ifdef INET6
     80 static int ipsecif6_input(struct mbuf **, int *, int, void *);
     81 static int ipsecif6_output(struct ipsec_variant *, int, struct mbuf *);
     82 static int ipsecif6_filter6(const struct ip6_hdr *, struct ipsec_variant *,
     83 	struct ifnet *);
     84 #endif
     85 
     86 static int ip_ipsec_ttl = IPSEC_TTL;
     87 static int ip_ipsec_copy_tos = 0;
     88 #ifdef INET6
     89 static int ip6_ipsec_hlim = IPSEC_HLIM;
     90 static int ip6_ipsec_pmtu = 0; /* XXX: per interface configuration?? */
     91 static int ip6_ipsec_copy_tos = 0;
     92 #endif
     93 
     94 struct encapsw ipsecif4_encapsw = {
     95 	.encapsw4 = {
     96 		.pr_input = ipsecif4_input,
     97 		.pr_ctlinput = NULL,
     98 	}
     99 };
    100 
    101 #ifdef INET6
    102 static const struct encapsw ipsecif6_encapsw;
    103 #endif
    104 
    105 static struct mbuf *
    106 ipsecif4_prepend_hdr(struct ipsec_variant *var, struct mbuf *m,
    107     uint8_t proto, uint8_t tos)
    108 {
    109 	struct ip *ip;
    110 	struct sockaddr_in *src, *dst;
    111 
    112 	src = satosin(var->iv_psrc);
    113 	dst = satosin(var->iv_pdst);
    114 
    115 	if (in_nullhost(src->sin_addr) || in_nullhost(src->sin_addr) ||
    116 	    src->sin_addr.s_addr == INADDR_BROADCAST ||
    117 	    dst->sin_addr.s_addr == INADDR_BROADCAST) {
    118 		m_freem(m);
    119 		return NULL;
    120 	}
    121 	m->m_flags &= ~M_BCAST;
    122 
    123 	if (IN_MULTICAST(src->sin_addr.s_addr) ||
    124 	    IN_MULTICAST(dst->sin_addr.s_addr)) {
    125 		m_freem(m);
    126 		return NULL;
    127 	}
    128 
    129 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    130 	if (m && M_UNWRITABLE(m, sizeof(struct ip)))
    131 		m = m_pullup(m, sizeof(struct ip));
    132 	if (m == NULL)
    133 		return NULL;
    134 
    135 	ip = mtod(m, struct ip *);
    136 	ip->ip_v = IPVERSION;
    137 	ip->ip_off = htons(0);
    138 	ip->ip_id = 0;
    139 	ip->ip_hl = sizeof(*ip) >> 2;
    140 	if (ip_ipsec_copy_tos)
    141 		ip->ip_tos = tos;
    142 	else
    143 		ip->ip_tos = 0;
    144 	ip->ip_sum = 0;
    145 	ip->ip_src = src->sin_addr;
    146 	ip->ip_dst = dst->sin_addr;
    147 	ip->ip_p = proto;
    148 	ip->ip_ttl = ip_ipsec_ttl;
    149 	ip->ip_len = htons(m->m_pkthdr.len);
    150 #ifndef IPSEC_TX_TOS_CLEAR
    151 	struct ifnet *ifp = &var->iv_softc->ipsec_if;
    152 	if (ifp->if_flags & IFF_ECN)
    153 		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
    154 	else
    155 		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
    156 #endif
    157 
    158 	return m;
    159 }
    160 
    161 static int
    162 ipsecif4_needfrag(struct mbuf *m, struct ipsecrequest *isr)
    163 {
    164 	struct ip ip0;
    165 	struct ip *ip;
    166 	int mtu;
    167 	struct secasvar *sav;
    168 
    169 	sav = key_lookup_sa_bysaidx(&isr->saidx);
    170 	if (sav == NULL)
    171 		return 0;
    172 
    173 	if (!(sav->natt_type & UDP_ENCAP_ESPINUDP) &&
    174 	    !(sav->natt_type & UDP_ENCAP_ESPINUDP_NON_IKE)) {
    175 		mtu = 0;
    176 		goto out;
    177 	}
    178 
    179 	if (m->m_len < sizeof(struct ip)) {
    180 		m_copydata(m, 0, sizeof(ip0), &ip0);
    181 		ip = &ip0;
    182 	} else {
    183 		ip = mtod(m, struct ip *);
    184 	}
    185 	mtu = sav->esp_frag;
    186 	if (ntohs(ip->ip_len) <= mtu)
    187 		mtu = 0;
    188 
    189 out:
    190 	KEY_SA_UNREF(&sav);
    191 	return mtu;
    192 }
    193 
    194 static struct mbuf *
    195 ipsecif4_flowinfo(struct mbuf *m, int family, int *proto0, u_int8_t *tos0)
    196 {
    197 	const struct ip *ip;
    198 	int proto;
    199 	int tos;
    200 
    201 	KASSERT(proto0 != NULL);
    202 	KASSERT(tos0 != NULL);
    203 
    204 	switch (family) {
    205 	case AF_INET:
    206 		proto = IPPROTO_IPV4;
    207 		if (m->m_len < sizeof(*ip)) {
    208 			m = m_pullup(m, sizeof(*ip));
    209 			if (m == NULL) {
    210 				*tos0 = 0;
    211 				*proto0 = 0;
    212 				return NULL;
    213 			}
    214 		}
    215 		ip = mtod(m, const struct ip *);
    216 		tos = ip->ip_tos;
    217 		/* TODO: support ALTQ for innner packet */
    218 		break;
    219 #ifdef INET6
    220 	case AF_INET6: {
    221 		const struct ip6_hdr *ip6;
    222 		proto = IPPROTO_IPV6;
    223 		if (m->m_len < sizeof(*ip6)) {
    224 			m = m_pullup(m, sizeof(*ip6));
    225 			if (m == NULL) {
    226 				*tos0 = 0;
    227 				*proto0 = 0;
    228 				return NULL;
    229 			}
    230 		}
    231 		ip6 = mtod(m, const struct ip6_hdr *);
    232 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    233 		/* TODO: support ALTQ for innner packet */
    234 		break;
    235 	}
    236 #endif /* INET6 */
    237 	default:
    238 		*tos0 = 0;
    239 		*proto0 = 0;
    240 		return NULL;
    241 	}
    242 
    243 	*proto0 = proto;
    244 	*tos0 = tos;
    245 	return m;
    246 }
    247 
    248 static int
    249 ipsecif4_fragout(struct ipsec_variant *var, int family, struct mbuf *m, int mtu)
    250 {
    251 	struct ifnet *ifp = &var->iv_softc->ipsec_if;
    252 	struct mbuf *next;
    253 	struct m_tag *mtag;
    254 	int error;
    255 
    256 	KASSERT(if_ipsec_heldref_variant(var));
    257 
    258 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
    259 	if (mtag)
    260 		m_tag_delete(m, mtag);
    261 
    262 	/* consider new IP header prepended in ipsecif4_output() */
    263 	if (mtu <= sizeof(struct ip)) {
    264 		m_freem(m);
    265 		return ENETUNREACH;
    266 	}
    267 	m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
    268 	error = ip_fragment(m, ifp, mtu - sizeof(struct ip));
    269 	if (error)
    270 		return error;
    271 
    272 	for (error = 0; m; m = next) {
    273 		next = m->m_nextpkt;
    274 		m->m_nextpkt = NULL;
    275 		if (error) {
    276 			m_freem(m);
    277 			continue;
    278 		}
    279 
    280 		error = ipsecif4_output(var, family, m);
    281 	}
    282 	if (error == 0)
    283 		IP_STATINC(IP_STAT_FRAGMENTED);
    284 
    285 	return error;
    286 }
    287 
    288 int
    289 ipsecif4_encap_func(struct mbuf *m, struct ip *ip, struct ipsec_variant *var)
    290 {
    291 	struct m_tag *mtag;
    292 	struct sockaddr_in *src, *dst;
    293 	u_int16_t src_port = 0;
    294 	u_int16_t dst_port = 0;
    295 
    296 	KASSERT(var != NULL);
    297 
    298 	src = satosin(var->iv_psrc);
    299 	dst = satosin(var->iv_pdst);
    300 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
    301 	if (mtag) {
    302 		u_int16_t *ports;
    303 
    304 		ports = (u_int16_t *)(mtag + 1);
    305 		src_port = ports[0];
    306 		dst_port = ports[1];
    307 	}
    308 
    309 	/* address match */
    310 	if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
    311 	    dst->sin_addr.s_addr != ip->ip_src.s_addr)
    312 		return 0;
    313 
    314 	/* UDP encap? */
    315 	if (mtag == NULL && var->iv_sport == 0 && var->iv_dport == 0)
    316 		goto match;
    317 
    318 	/* port match */
    319 	if (src_port != var->iv_dport ||
    320 	    dst_port != var->iv_sport) {
    321 #ifdef DEBUG
    322 		printf("%s: port mismatch: pkt(%u, %u), if(%u, %u)\n",
    323 		    __func__, ntohs(src_port), ntohs(dst_port),
    324 		    ntohs(var->iv_sport), ntohs(var->iv_dport));
    325 #endif
    326 		return 0;
    327 	}
    328 
    329 match:
    330 	/*
    331 	 * hide NAT-T information from encapsulated traffics.
    332 	 * they don't know about IPsec.
    333 	 */
    334 	if (mtag)
    335 		m_tag_delete(m, mtag);
    336 	return sizeof(src->sin_addr) + sizeof(dst->sin_addr);
    337 }
    338 
    339 static int
    340 ipsecif4_output(struct ipsec_variant *var, int family, struct mbuf *m)
    341 {
    342 	struct secpolicy *sp = NULL;
    343 	u_int8_t tos;
    344 	int proto;
    345 	int error;
    346 	int mtu;
    347 	u_long sa_mtu = 0;
    348 
    349 	KASSERT(if_ipsec_heldref_variant(var));
    350 	KASSERT(if_ipsec_variant_is_configured(var));
    351 	KASSERT(var->iv_psrc->sa_family == AF_INET);
    352 	KASSERT(var->iv_pdst->sa_family == AF_INET);
    353 
    354 	sp = IV_SP_OUT(var);
    355 	KASSERT(sp != NULL);
    356 	/*
    357 	 * The SPs in ipsec_variant are prevented from freed by
    358 	 * ipsec_variant->iv_psref. So, KEY_SP_REF() is unnecessary here.
    359 	 */
    360 
    361 	KASSERT(sp->policy != IPSEC_POLICY_NONE);
    362 	KASSERT(sp->policy != IPSEC_POLICY_ENTRUST);
    363 	KASSERT(sp->policy != IPSEC_POLICY_BYPASS);
    364 	if (sp->policy != IPSEC_POLICY_IPSEC) {
    365 		struct ifnet *ifp = &var->iv_softc->ipsec_if;
    366 		m_freem(m);
    367 		IF_DROP(&ifp->if_snd);
    368 		return 0;
    369 	}
    370 
    371 	/* get flowinfo */
    372 	m = ipsecif4_flowinfo(m, family, &proto, &tos);
    373 	if (m == NULL) {
    374 		error = ENETUNREACH;
    375 		goto done;
    376 	}
    377 
    378 	/* prepend new IP header */
    379 	m = ipsecif4_prepend_hdr(var, m, proto, tos);
    380 	if (m == NULL) {
    381 		error = ENETUNREACH;
    382 		goto done;
    383 	}
    384 
    385 	/*
    386 	 * Normal netipsec's NAT-T fragmentation is done in ip_output().
    387 	 * See "natt_frag" processing.
    388 	 * However, ipsec(4) interface's one is not done in the same way,
    389 	 * so we must do NAT-T fragmentation by own code.
    390 	 */
    391 	/* NAT-T ESP fragmentation */
    392 	mtu = ipsecif4_needfrag(m, sp->req);
    393 	if (mtu > 0)
    394 		return ipsecif4_fragout(var, family, m, mtu);
    395 
    396 	/* IPsec output */
    397 	IP_STATINC(IP_STAT_LOCALOUT);
    398 	error = ipsec4_process_packet(m, sp->req, &sa_mtu);
    399 	if (error == ENOENT)
    400 		error = 0;
    401 	/*
    402 	 * frangmentation is already done in ipsecif4_fragout(),
    403 	 * so ipsec4_process_packet() must not do fragmentation here.
    404 	 */
    405 	KASSERT(sa_mtu == 0);
    406 
    407 done:
    408 	return error;
    409 }
    410 
    411 #ifdef INET6
    412 static int
    413 ipsecif6_output(struct ipsec_variant *var, int family, struct mbuf *m)
    414 {
    415 	struct ifnet *ifp = &var->iv_softc->ipsec_if;
    416 	struct ipsec_softc *sc = ifp->if_softc;
    417 	struct ipsec_ro *iro;
    418 	struct rtentry *rt;
    419 	struct sockaddr_in6 *sin6_src;
    420 	struct sockaddr_in6 *sin6_dst;
    421 	struct ip6_hdr *ip6;
    422 	int proto, error;
    423 	u_int8_t itos, otos;
    424 	union {
    425 		struct sockaddr		dst;
    426 		struct sockaddr_in6	dst6;
    427 	} u;
    428 
    429 	KASSERT(if_ipsec_heldref_variant(var));
    430 	KASSERT(if_ipsec_variant_is_configured(var));
    431 
    432 	sin6_src = satosin6(var->iv_psrc);
    433 	sin6_dst = satosin6(var->iv_pdst);
    434 
    435 	KASSERT(sin6_src->sin6_family == AF_INET6);
    436 	KASSERT(sin6_dst->sin6_family == AF_INET6);
    437 
    438 	switch (family) {
    439 #ifdef INET
    440 	case AF_INET:
    441 	    {
    442 		struct ip *ip;
    443 
    444 		proto = IPPROTO_IPV4;
    445 		if (m->m_len < sizeof(*ip)) {
    446 			m = m_pullup(m, sizeof(*ip));
    447 			if (m == NULL)
    448 				return ENOBUFS;
    449 		}
    450 		ip = mtod(m, struct ip *);
    451 		itos = ip->ip_tos;
    452 		/* TODO: support ALTQ for innner packet */
    453 		break;
    454 	    }
    455 #endif /* INET */
    456 	case AF_INET6:
    457 	    {
    458 		struct ip6_hdr *xip6;
    459 		proto = IPPROTO_IPV6;
    460 		if (m->m_len < sizeof(*xip6)) {
    461 			m = m_pullup(m, sizeof(*xip6));
    462 			if (m == NULL)
    463 				return ENOBUFS;
    464 		}
    465 		xip6 = mtod(m, struct ip6_hdr *);
    466 		itos = (ntohl(xip6->ip6_flow) >> 20) & 0xff;
    467 		/* TODO: support ALTQ for innner packet */
    468 		break;
    469 	    }
    470 	default:
    471 		m_freem(m);
    472 		return EAFNOSUPPORT;
    473 	}
    474 
    475 	/* prepend new IP header */
    476 	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
    477 	if (m && M_UNWRITABLE(m, sizeof(struct ip6_hdr)))
    478 		m = m_pullup(m, sizeof(struct ip6_hdr));
    479 	if (m == NULL)
    480 		return ENOBUFS;
    481 
    482 	ip6 = mtod(m, struct ip6_hdr *);
    483 	ip6->ip6_flow	= 0;
    484 	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
    485 	ip6->ip6_vfc	|= IPV6_VERSION;
    486 	ip6->ip6_plen	= htons((u_short)m->m_pkthdr.len);
    487 	ip6->ip6_nxt	= proto;
    488 	ip6->ip6_hlim	= ip6_ipsec_hlim;
    489 	ip6->ip6_src	= sin6_src->sin6_addr;
    490 	/* bidirectional configured tunnel mode */
    491 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) {
    492 		ip6->ip6_dst = sin6_dst->sin6_addr;
    493 	} else  {
    494 		m_freem(m);
    495 		return ENETUNREACH;
    496 	}
    497 #ifndef IPSEC_TX_TOS_CLEAR
    498 	if (ifp->if_flags & IFF_ECN)
    499 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
    500 	else
    501 		ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
    502 
    503 	if (!ip6_ipsec_copy_tos)
    504 		otos = 0;
    505 #else
    506 	if (ip6_ipsec_copy_tos)
    507 		otos = itos;
    508 	else
    509 		otos = 0;
    510 #endif
    511 	ip6->ip6_flow &= ~ntohl(0xff00000);
    512 	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
    513 
    514 	sockaddr_in6_init(&u.dst6, &sin6_dst->sin6_addr, 0, 0, 0);
    515 
    516 	iro = percpu_getref(sc->ipsec_ro_percpu);
    517 	mutex_enter(&iro->ir_lock);
    518 	if ((rt = rtcache_lookup(&iro->ir_ro, &u.dst)) == NULL) {
    519 		mutex_exit(&iro->ir_lock);
    520 		percpu_putref(sc->ipsec_ro_percpu);
    521 		m_freem(m);
    522 		return ENETUNREACH;
    523 	}
    524 
    525 	if (rt->rt_ifp == ifp) {
    526 		rtcache_unref(rt, &iro->ir_ro);
    527 		rtcache_free(&iro->ir_ro);
    528 		mutex_exit(&iro->ir_lock);
    529 		percpu_putref(sc->ipsec_ro_percpu);
    530 		m_freem(m);
    531 		return ENETUNREACH;
    532 	}
    533 	rtcache_unref(rt, &iro->ir_ro);
    534 
    535 	/*
    536 	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
    537 	 * it is too painful to ask for resend of inner packet, to achieve
    538 	 * path MTU discovery for encapsulated packets.
    539 	 */
    540 	error = ip6_output(m, 0, &iro->ir_ro,
    541 	    ip6_ipsec_pmtu ? 0 : IPV6_MINMTU, 0, NULL, NULL);
    542 	if (error)
    543 		rtcache_free(&iro->ir_ro);
    544 
    545 	mutex_exit(&iro->ir_lock);
    546 	percpu_putref(sc->ipsec_ro_percpu);
    547 
    548 	return error;
    549 }
    550 #endif /* INET6 */
    551 
    552 static void
    553 ipsecif4_input(struct mbuf *m, int off, int proto, void *eparg)
    554 {
    555 	struct ifnet *ipsecp;
    556 	struct ipsec_softc *sc = eparg;
    557 	struct ipsec_variant *var;
    558 	const struct ip *ip;
    559 	int af;
    560 #ifndef IPSEC_TX_TOS_CLEAR
    561 	u_int8_t otos;
    562 #endif
    563 	struct psref psref_rcvif;
    564 	struct psref psref_var;
    565 	struct ifnet *rcvif;
    566 
    567 	KASSERT(sc != NULL);
    568 
    569 	ipsecp = &sc->ipsec_if;
    570 	if ((ipsecp->if_flags & IFF_UP) == 0) {
    571 		m_freem(m);
    572 		ip_statinc(IP_STAT_NOIPSEC);
    573 		return;
    574 	}
    575 
    576 	var = if_ipsec_getref_variant(sc, &psref_var);
    577 	if (if_ipsec_variant_is_unconfigured(var)) {
    578 		if_ipsec_putref_variant(var, &psref_var);
    579 		m_freem(m);
    580 		ip_statinc(IP_STAT_NOIPSEC);
    581 		return;
    582 	}
    583 
    584 	ip = mtod(m, const struct ip *);
    585 
    586 	rcvif = m_get_rcvif_psref(m, &psref_rcvif);
    587 	if (rcvif == NULL || !ipsecif4_filter4(ip, var, rcvif)) {
    588 		m_put_rcvif_psref(rcvif, &psref_rcvif);
    589 		if_ipsec_putref_variant(var, &psref_var);
    590 		m_freem(m);
    591 		ip_statinc(IP_STAT_NOIPSEC);
    592 		return;
    593 	}
    594 	m_put_rcvif_psref(rcvif, &psref_rcvif);
    595 	if_ipsec_putref_variant(var, &psref_var);
    596 #ifndef IPSEC_TX_TOS_CLEAR
    597 	otos = ip->ip_tos;
    598 #endif
    599 	m_adj(m, off);
    600 
    601 	switch (proto) {
    602 	case IPPROTO_IPV4:
    603 	    {
    604 		struct ip *xip;
    605 		af = AF_INET;
    606 		if (M_UNWRITABLE(m, sizeof(*xip))) {
    607 			m = m_pullup(m, sizeof(*xip));
    608 			if (m == NULL)
    609 				return;
    610 		}
    611 		xip = mtod(m, struct ip *);
    612 #ifndef IPSEC_TX_TOS_CLEAR
    613 		if (ipsecp->if_flags & IFF_ECN)
    614 			ip_ecn_egress(ECN_ALLOWED, &otos, &xip->ip_tos);
    615 		else
    616 			ip_ecn_egress(ECN_NOCARE, &otos, &xip->ip_tos);
    617 #endif
    618 		break;
    619 	    }
    620 #ifdef INET6
    621 	case IPPROTO_IPV6:
    622 	    {
    623 		struct ip6_hdr *ip6;
    624 		u_int8_t itos;
    625 		af = AF_INET6;
    626 		if (M_UNWRITABLE(m, sizeof(*ip6))) {
    627 			m = m_pullup(m, sizeof(*ip6));
    628 			if (m == NULL)
    629 				return;
    630 		}
    631 		ip6 = mtod(m, struct ip6_hdr *);
    632 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    633 #ifndef IPSEC_TX_TOS_CLEAR
    634 		if (ipsecp->if_flags & IFF_ECN)
    635 			ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
    636 		else
    637 			ip_ecn_egress(ECN_NOCARE, &otos, &itos);
    638 #endif
    639 		ip6->ip6_flow &= ~htonl(0xff << 20);
    640 		ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
    641 		break;
    642 	    }
    643 #endif /* INET6 */
    644 	default:
    645 		ip_statinc(IP_STAT_NOIPSEC);
    646 		m_freem(m);
    647 		return;
    648 	}
    649 	if_ipsec_input(m, af, ipsecp);
    650 
    651 	return;
    652 }
    653 
    654 /*
    655  * validate and filter the pakcet
    656  */
    657 static int
    658 ipsecif4_filter4(const struct ip *ip, struct ipsec_variant *var,
    659     struct ifnet *ifp)
    660 {
    661 	struct sockaddr_in *src, *dst;
    662 
    663 	src = satosin(var->iv_psrc);
    664 	dst = satosin(var->iv_pdst);
    665 
    666 	return in_tunnel_validate(ip, src->sin_addr, dst->sin_addr);
    667 }
    668 
    669 #ifdef INET6
    670 static int
    671 ipsecif6_input(struct mbuf **mp, int *offp, int proto, void *eparg)
    672 {
    673 	struct mbuf *m = *mp;
    674 	struct ifnet *ipsecp;
    675 	struct ipsec_softc *sc = eparg;
    676 	struct ipsec_variant *var;
    677 	struct ip6_hdr *ip6;
    678 	int af = 0;
    679 #ifndef IPSEC_TX_TOS_CLEAR
    680 	u_int32_t otos;
    681 #endif
    682 	struct psref psref_rcvif;
    683 	struct psref psref_var;
    684 	struct ifnet *rcvif;
    685 
    686 	KASSERT(eparg != NULL);
    687 
    688 	ipsecp = &sc->ipsec_if;
    689 	if ((ipsecp->if_flags & IFF_UP) == 0) {
    690 		m_freem(m);
    691 		IP6_STATINC(IP6_STAT_NOIPSEC);
    692 		return IPPROTO_DONE;
    693 	}
    694 
    695 	var = if_ipsec_getref_variant(sc, &psref_var);
    696 	if (if_ipsec_variant_is_unconfigured(var)) {
    697 		if_ipsec_putref_variant(var, &psref_var);
    698 		m_freem(m);
    699 		IP6_STATINC(IP6_STAT_NOIPSEC);
    700 		return IPPROTO_DONE;
    701 	}
    702 
    703 	ip6 = mtod(m, struct ip6_hdr *);
    704 
    705 	rcvif = m_get_rcvif_psref(m, &psref_rcvif);
    706 	if (rcvif == NULL || !ipsecif6_filter6(ip6, var, rcvif)) {
    707 		m_put_rcvif_psref(rcvif, &psref_rcvif);
    708 		if_ipsec_putref_variant(var, &psref_var);
    709 		m_freem(m);
    710 		IP6_STATINC(IP6_STAT_NOIPSEC);
    711 		return IPPROTO_DONE;
    712 	}
    713 	m_put_rcvif_psref(rcvif, &psref_rcvif);
    714 	if_ipsec_putref_variant(var, &psref_var);
    715 
    716 #ifndef IPSEC_TX_TOS_CLEAR
    717 	otos = ip6->ip6_flow;
    718 #endif
    719 	m_adj(m, *offp);
    720 
    721 	switch (proto) {
    722 #ifdef INET
    723 	case IPPROTO_IPV4:
    724 	    {
    725 		af = AF_INET;
    726 #ifndef IPSEC_TX_TOS_CLEAR
    727 		struct ip *ip;
    728 		u_int8_t otos8;
    729 		otos8 = (ntohl(otos) >> 20) & 0xff;
    730 
    731 		if (M_UNWRITABLE(m, sizeof(*ip))) {
    732 			m = m_pullup(m, sizeof(*ip));
    733 			if (m == NULL)
    734 				return IPPROTO_DONE;
    735 		}
    736 		ip = mtod(m, struct ip *);
    737 		if (ipsecp->if_flags & IFF_ECN)
    738 			ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos);
    739 		else
    740 			ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
    741 #endif
    742 		break;
    743 	    }
    744 #endif /* INET */
    745 	case IPPROTO_IPV6:
    746 	    {
    747 		af = AF_INET6;
    748 #ifndef IPSEC_TX_TOS_CLEAR
    749 		struct ip6_hdr *xip6;
    750 
    751 		if (M_UNWRITABLE(m, sizeof(*xip6))) {
    752 			m = m_pullup(m, sizeof(*xip6));
    753 			if (m == NULL)
    754 				return IPPROTO_DONE;
    755 		}
    756 		xip6 = mtod(m, struct ip6_hdr *);
    757 		if (ipsecp->if_flags & IFF_ECN)
    758 			ip6_ecn_egress(ECN_ALLOWED, &otos, &xip6->ip6_flow);
    759 		else
    760 			ip6_ecn_egress(ECN_NOCARE, &otos, &xip6->ip6_flow);
    761 		break;
    762 #endif
    763 	    }
    764 	default:
    765 		IP6_STATINC(IP6_STAT_NOIPSEC);
    766 		m_freem(m);
    767 		return IPPROTO_DONE;
    768 	}
    769 
    770 	if_ipsec_input(m, af, ipsecp);
    771 	return IPPROTO_DONE;
    772 }
    773 
    774 /*
    775  * validate and filter the packet.
    776  */
    777 static int
    778 ipsecif6_filter6(const struct ip6_hdr *ip6, struct ipsec_variant *var,
    779     struct ifnet *ifp)
    780 {
    781 	struct sockaddr_in6 *src, *dst;
    782 
    783 	src = satosin6(var->iv_psrc);
    784 	dst = satosin6(var->iv_pdst);
    785 
    786 	return in6_tunnel_validate(ip6, &src->sin6_addr, &dst->sin6_addr);
    787 }
    788 #endif /* INET6 */
    789 
    790 int
    791 ipsecif4_attach(struct ipsec_variant *var)
    792 {
    793 	struct ipsec_softc *sc = var->iv_softc;
    794 
    795 	KASSERT(if_ipsec_variant_is_configured(var));
    796 
    797 	if (var->iv_encap_cookie4 != NULL)
    798 		return EALREADY;
    799 	var->iv_encap_cookie4 = encap_attach_func(AF_INET, -1, if_ipsec_encap_func,
    800 	    &ipsecif4_encapsw, sc);
    801 	if (var->iv_encap_cookie4 == NULL)
    802 		return EEXIST;
    803 
    804 	var->iv_output = ipsecif4_output;
    805 	return 0;
    806 }
    807 
    808 int
    809 ipsecif4_detach(struct ipsec_variant *var)
    810 {
    811 	int error;
    812 
    813 	if (var->iv_encap_cookie4 == NULL)
    814 		return 0;
    815 
    816 	var->iv_output = NULL;
    817 	error = encap_detach(var->iv_encap_cookie4);
    818 	if (error == 0)
    819 		var->iv_encap_cookie4 = NULL;
    820 
    821 	return error;
    822 }
    823 
    824 #ifdef INET6
    825 int
    826 ipsecif6_attach(struct ipsec_variant *var)
    827 {
    828 	struct sockaddr_in6 mask6;
    829 	struct ipsec_softc *sc = var->iv_softc;
    830 
    831 	KASSERT(if_ipsec_variant_is_configured(var));
    832 	KASSERT(var->iv_encap_cookie6 == NULL);
    833 
    834 	memset(&mask6, 0, sizeof(mask6));
    835 	mask6.sin6_len = sizeof(struct sockaddr_in6);
    836 	mask6.sin6_addr.s6_addr32[0] = mask6.sin6_addr.s6_addr32[1] =
    837 	mask6.sin6_addr.s6_addr32[2] = mask6.sin6_addr.s6_addr32[3] = ~0;
    838 
    839 	var->iv_encap_cookie6 = encap_attach(AF_INET6, -1,
    840 	    var->iv_psrc, (struct sockaddr *)&mask6,
    841 	    var->iv_pdst, (struct sockaddr *)&mask6,
    842 	    &ipsecif6_encapsw, sc);
    843 	if (var->iv_encap_cookie6 == NULL)
    844 		return EEXIST;
    845 
    846 	var->iv_output = ipsecif6_output;
    847 	return 0;
    848 }
    849 
    850 static void
    851 ipsecif6_rtcache_free_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
    852 {
    853 	struct ipsec_ro *iro = p;
    854 
    855 	mutex_enter(&iro->ir_lock);
    856 	rtcache_free(&iro->ir_ro);
    857 	mutex_exit(&iro->ir_lock);
    858 }
    859 
    860 int
    861 ipsecif6_detach(struct ipsec_variant *var)
    862 {
    863 	struct ipsec_softc *sc = var->iv_softc;
    864 	int error;
    865 
    866 	KASSERT(var->iv_encap_cookie6 != NULL);
    867 
    868 	percpu_foreach(sc->ipsec_ro_percpu, ipsecif6_rtcache_free_pc, NULL);
    869 
    870 	var->iv_output = NULL;
    871 	error = encap_detach(var->iv_encap_cookie6);
    872 	if (error == 0)
    873 		var->iv_encap_cookie6 = NULL;
    874 	return error;
    875 }
    876 
    877 void *
    878 ipsecif6_ctlinput(int cmd, const struct sockaddr *sa, void *d, void *eparg)
    879 {
    880 	struct ipsec_softc *sc = eparg;
    881 	struct ip6ctlparam *ip6cp = NULL;
    882 	struct ip6_hdr *ip6;
    883 	const struct sockaddr_in6 *dst6;
    884 	struct ipsec_ro *iro;
    885 
    886 	if (sa->sa_family != AF_INET6 ||
    887 	    sa->sa_len != sizeof(struct sockaddr_in6))
    888 		return NULL;
    889 
    890 	if ((unsigned)cmd >= PRC_NCMDS)
    891 		return NULL;
    892 	if (cmd == PRC_HOSTDEAD)
    893 		d = NULL;
    894 	else if (inet6ctlerrmap[cmd] == 0)
    895 		return NULL;
    896 
    897 	/* if the parameter is from icmp6, decode it. */
    898 	if (d != NULL) {
    899 		ip6cp = (struct ip6ctlparam *)d;
    900 		ip6 = ip6cp->ip6c_ip6;
    901 	} else {
    902 		ip6 = NULL;
    903 	}
    904 
    905 	if (!ip6)
    906 		return NULL;
    907 
    908 	iro = percpu_getref(sc->ipsec_ro_percpu);
    909 	mutex_enter(&iro->ir_lock);
    910 	dst6 = satocsin6(rtcache_getdst(&iro->ir_ro));
    911 	/* XXX scope */
    912 	if (dst6 == NULL)
    913 		;
    914 	else if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst6->sin6_addr))
    915 		/* flush route cache */
    916 		rtcache_free(&iro->ir_ro);
    917 
    918 	mutex_exit(&iro->ir_lock);
    919 	percpu_putref(sc->ipsec_ro_percpu);
    920 
    921 	return NULL;
    922 }
    923 
    924 ENCAP_PR_WRAP_CTLINPUT(ipsecif6_ctlinput)
    925 #define	ipsecif6_ctlinput	ipsecif6_ctlinput_wrapper
    926 
    927 static const struct encapsw ipsecif6_encapsw = {
    928 	.encapsw6 = {
    929 		.pr_input = ipsecif6_input,
    930 		.pr_ctlinput = ipsecif6_ctlinput,
    931 	}
    932 };
    933 #endif /* INET6 */
    934