Home | History | Annotate | Line # | Download | only in net
if_gif.c revision 1.151
      1 /*	$NetBSD: if_gif.c,v 1.151 2020/01/29 04:18:34 thorpej Exp $	*/
      2 /*	$KAME: if_gif.c,v 1.76 2001/08/20 02:01:02 kjc Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: if_gif.c,v 1.151 2020/01/29 04:18:34 thorpej Exp $");
     35 
     36 #ifdef _KERNEL_OPT
     37 #include "opt_inet.h"
     38 #include "opt_net_mpsafe.h"
     39 #endif
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/socket.h>
     46 #include <sys/sockio.h>
     47 #include <sys/errno.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/time.h>
     50 #include <sys/socketvar.h>
     51 #include <sys/syslog.h>
     52 #include <sys/proc.h>
     53 #include <sys/cpu.h>
     54 #include <sys/intr.h>
     55 #include <sys/kmem.h>
     56 #include <sys/sysctl.h>
     57 #include <sys/xcall.h>
     58 #include <sys/device.h>
     59 #include <sys/module.h>
     60 #include <sys/mutex.h>
     61 #include <sys/pserialize.h>
     62 #include <sys/psref.h>
     63 
     64 #include <net/if.h>
     65 #include <net/if_types.h>
     66 #include <net/netisr.h>
     67 #include <net/route.h>
     68 #include <net/bpf.h>
     69 
     70 #include <netinet/in.h>
     71 #include <netinet/in_systm.h>
     72 #include <netinet/ip.h>
     73 #ifdef	INET
     74 #include <netinet/in_var.h>
     75 #endif	/* INET */
     76 #include <netinet/in_gif.h>
     77 
     78 #ifdef INET6
     79 #ifndef INET
     80 #include <netinet/in.h>
     81 #endif
     82 #include <netinet6/in6_var.h>
     83 #include <netinet/ip6.h>
     84 #include <netinet6/ip6_var.h>
     85 #include <netinet6/in6_gif.h>
     86 #endif /* INET6 */
     87 
     88 #include <netinet/ip_encap.h>
     89 #include <net/if_gif.h>
     90 
     91 #include "ioconf.h"
     92 
     93 #ifdef NET_MPSAFE
     94 #define GIF_MPSAFE	1
     95 #endif
     96 
     97 /*
     98  * gif global variable definitions
     99  */
    100 static struct {
    101 	LIST_HEAD(gif_sclist, gif_softc) list;
    102 	kmutex_t lock;
    103 } gif_softcs __cacheline_aligned;
    104 
    105 struct psref_class *gv_psref_class __read_mostly;
    106 
    107 static int	gifattach0(struct gif_softc *);
    108 static int	gif_output(struct ifnet *, struct mbuf *,
    109 			   const struct sockaddr *, const struct rtentry *);
    110 static void	gif_start(struct ifnet *);
    111 static int	gif_transmit(struct ifnet *, struct mbuf *);
    112 static int	gif_transmit_direct(struct gif_variant *, struct mbuf *);
    113 static int	gif_ioctl(struct ifnet *, u_long, void *);
    114 static int	gif_set_tunnel(struct ifnet *, struct sockaddr *,
    115 			       struct sockaddr *);
    116 static void	gif_delete_tunnel(struct ifnet *);
    117 
    118 static int	gif_clone_create(struct if_clone *, int);
    119 static int	gif_clone_destroy(struct ifnet *);
    120 static int	gif_check_nesting(struct ifnet *, struct mbuf *);
    121 
    122 static int	gif_encap_attach(struct gif_variant *);
    123 static int	gif_encap_detach(struct gif_variant *);
    124 
    125 static void	gif_update_variant(struct gif_softc *, struct gif_variant *);
    126 
    127 static struct if_clone gif_cloner =
    128     IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
    129 
    130 #ifndef MAX_GIF_NEST
    131 /*
    132  * This macro controls the upper limitation on nesting of gif tunnels.
    133  * Since, setting a large value to this macro with a careless configuration
    134  * may introduce system crash, we don't allow any nestings by default.
    135  * If you need to configure nested gif tunnels, you can define this macro
    136  * in your kernel configuration file.  However, if you do so, please be
    137  * careful to configure the tunnels so that it won't make a loop.
    138  */
    139 #define MAX_GIF_NEST 1
    140 #endif
    141 static int max_gif_nesting = MAX_GIF_NEST;
    142 
    143 static struct sysctllog *gif_sysctl;
    144 
    145 #ifdef INET6
    146 static int
    147 sysctl_gif_pmtu_global(SYSCTLFN_ARGS)
    148 {
    149 	int error, pmtu;
    150 	struct sysctlnode node = *rnode;
    151 
    152 	pmtu = ip6_gif_pmtu;
    153 	node.sysctl_data = &pmtu;
    154 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    155 	if (error || newp == NULL)
    156 		return error;
    157 
    158 	switch (pmtu) {
    159 	case GIF_PMTU_MINMTU:
    160 	case GIF_PMTU_OUTERMTU:
    161 		ip6_gif_pmtu = pmtu;
    162 		break;
    163 	default:
    164 		return EINVAL;
    165 	}
    166 
    167 	return 0;
    168 }
    169 
    170 static int
    171 sysctl_gif_pmtu_perif(SYSCTLFN_ARGS)
    172 {
    173 	int error, pmtu;
    174 	struct sysctlnode node = *rnode;
    175 	struct gif_softc *sc = (struct gif_softc *)node.sysctl_data;
    176 
    177 	pmtu = sc->gif_pmtu;
    178 	node.sysctl_data = &pmtu;
    179 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    180 	if (error || newp == NULL)
    181 		return error;
    182 
    183 	switch (pmtu) {
    184 	case GIF_PMTU_SYSDEFAULT:
    185 	case GIF_PMTU_MINMTU:
    186 	case GIF_PMTU_OUTERMTU:
    187 		sc->gif_pmtu = pmtu;
    188 		break;
    189 	default:
    190 		return EINVAL;
    191 	}
    192 
    193 	return 0;
    194 }
    195 #endif
    196 
    197 static void
    198 gif_sysctl_setup(void)
    199 {
    200 	gif_sysctl = NULL;
    201 
    202 #ifdef INET
    203 	/*
    204 	 * Previously create "net.inet.ip" entry to avoid sysctl_createv error.
    205 	 */
    206 	sysctl_createv(NULL, 0, NULL, NULL,
    207 		       CTLFLAG_PERMANENT,
    208 		       CTLTYPE_NODE, "inet",
    209 		       SYSCTL_DESCR("PF_INET related settings"),
    210 		       NULL, 0, NULL, 0,
    211 		       CTL_NET, PF_INET, CTL_EOL);
    212 	sysctl_createv(NULL, 0, NULL, NULL,
    213 		       CTLFLAG_PERMANENT,
    214 		       CTLTYPE_NODE, "ip",
    215 		       SYSCTL_DESCR("IPv4 related settings"),
    216 		       NULL, 0, NULL, 0,
    217 		       CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
    218 
    219 	sysctl_createv(&gif_sysctl, 0, NULL, NULL,
    220 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    221 		       CTLTYPE_INT, "gifttl",
    222 		       SYSCTL_DESCR("Default TTL for a gif tunnel datagram"),
    223 		       NULL, 0, &ip_gif_ttl, 0,
    224 		       CTL_NET, PF_INET, IPPROTO_IP,
    225 		       IPCTL_GIF_TTL, CTL_EOL);
    226 #endif
    227 #ifdef INET6
    228 	/*
    229 	 * Previously create "net.inet6.ip6" entry to avoid sysctl_createv error.
    230 	 */
    231 	sysctl_createv(NULL, 0, NULL, NULL,
    232 		       CTLFLAG_PERMANENT,
    233 		       CTLTYPE_NODE, "inet6",
    234 		       SYSCTL_DESCR("PF_INET6 related settings"),
    235 		       NULL, 0, NULL, 0,
    236 		       CTL_NET, PF_INET6, CTL_EOL);
    237 	sysctl_createv(NULL, 0, NULL, NULL,
    238 		       CTLFLAG_PERMANENT,
    239 		       CTLTYPE_NODE, "ip6",
    240 		       SYSCTL_DESCR("IPv6 related settings"),
    241 		       NULL, 0, NULL, 0,
    242 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
    243 
    244 	sysctl_createv(&gif_sysctl, 0, NULL, NULL,
    245 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    246 		       CTLTYPE_INT, "gifhlim",
    247 		       SYSCTL_DESCR("Default hop limit for a gif tunnel datagram"),
    248 		       NULL, 0, &ip6_gif_hlim, 0,
    249 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
    250 		       IPV6CTL_GIF_HLIM, CTL_EOL);
    251 
    252 	sysctl_createv(&gif_sysctl, 0, NULL, NULL,
    253 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    254 		       CTLTYPE_INT, "gifpmtu",
    255 		       SYSCTL_DESCR("Default Path MTU setting for gif tunnels"),
    256 		       sysctl_gif_pmtu_global, 0, NULL, 0,
    257 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
    258 		       IPV6CTL_GIF_PMTU, CTL_EOL);
    259 #endif
    260 }
    261 
    262 static void
    263 gif_perif_sysctl_setup(struct sysctllog **clog, struct gif_softc *sc)
    264 {
    265 #ifdef INET6
    266 	const struct sysctlnode *cnode, *rnode;
    267 	struct ifnet *ifp = &sc->gif_if;
    268 	const char *ifname = ifp->if_xname;
    269 	int rv;
    270 
    271 	/*
    272 	 * Already created in sysctl_sndq_setup().
    273 	 */
    274 	sysctl_createv(clog, 0, NULL, &rnode,
    275 		       CTLFLAG_PERMANENT,
    276 		       CTLTYPE_NODE, "interfaces",
    277 		       SYSCTL_DESCR("Per-interface controls"),
    278 		       NULL, 0, NULL, 0,
    279 		       CTL_NET, CTL_CREATE, CTL_EOL);
    280 	sysctl_createv(clog, 0, &rnode, &rnode,
    281 		       CTLFLAG_PERMANENT,
    282 		       CTLTYPE_NODE, ifname,
    283 		       SYSCTL_DESCR("Interface controls"),
    284 		       NULL, 0, NULL, 0,
    285 		       CTL_CREATE, CTL_EOL);
    286 
    287 	rv = sysctl_createv(clog, 0, &rnode, &cnode,
    288 			    CTLFLAG_PERMANENT,
    289 			    CTLTYPE_INT, "pmtu",
    290 			    SYSCTL_DESCR("Path MTU setting for this gif tunnel"),
    291 			    sysctl_gif_pmtu_perif, 0, (void *)sc, 0,
    292 			    CTL_CREATE, CTL_EOL);
    293 	if (rv != 0)
    294 		log(LOG_WARNING, "%s: could not attach sysctl node pmtu\n", ifname);
    295 
    296 	sc->gif_pmtu = GIF_PMTU_SYSDEFAULT;
    297 #endif
    298 }
    299 
    300 /* ARGSUSED */
    301 void
    302 gifattach(int count)
    303 {
    304 	/*
    305 	 * Nothing to do here, initialization is handled by the
    306 	 * module initialization code in gifinit() below).
    307 	 */
    308 }
    309 
    310 static void
    311 gifinit(void)
    312 {
    313 
    314 	mutex_init(&gif_softcs.lock, MUTEX_DEFAULT, IPL_NONE);
    315 	LIST_INIT(&gif_softcs.list);
    316 	if_clone_attach(&gif_cloner);
    317 
    318 	gv_psref_class = psref_class_create("gifvar", IPL_SOFTNET);
    319 
    320 	gif_sysctl_setup();
    321 }
    322 
    323 static int
    324 gifdetach(void)
    325 {
    326 	int error = 0;
    327 
    328 	mutex_enter(&gif_softcs.lock);
    329 	if (!LIST_EMPTY(&gif_softcs.list)) {
    330 		mutex_exit(&gif_softcs.lock);
    331 		error = EBUSY;
    332 	}
    333 
    334 	if (error == 0) {
    335 		psref_class_destroy(gv_psref_class);
    336 
    337 		if_clone_detach(&gif_cloner);
    338 		sysctl_teardown(&gif_sysctl);
    339 	}
    340 
    341 	return error;
    342 }
    343 
    344 static int
    345 gif_clone_create(struct if_clone *ifc, int unit)
    346 {
    347 	struct gif_softc *sc;
    348 	struct gif_variant *var;
    349 	struct ifnet *ifp;
    350 	int rv;
    351 
    352 	sc = kmem_zalloc(sizeof(struct gif_softc), KM_SLEEP);
    353 
    354 	if_initname(&sc->gif_if, ifc->ifc_name, unit);
    355 
    356 	rv = gifattach0(sc);
    357 	if (rv != 0) {
    358 		kmem_free(sc, sizeof(struct gif_softc));
    359 		return rv;
    360 	}
    361 
    362 	ifp = &sc->gif_if;
    363 	gif_perif_sysctl_setup(&ifp->if_sysctl_log, sc);
    364 
    365 	var = kmem_zalloc(sizeof(*var), KM_SLEEP);
    366 	var->gv_softc = sc;
    367 	psref_target_init(&var->gv_psref, gv_psref_class);
    368 
    369 	sc->gif_var = var;
    370 	mutex_init(&sc->gif_lock, MUTEX_DEFAULT, IPL_NONE);
    371 	sc->gif_psz = pserialize_create();
    372 
    373 	sc->gif_ro_percpu = if_tunnel_alloc_ro_percpu();
    374 	mutex_enter(&gif_softcs.lock);
    375 	LIST_INSERT_HEAD(&gif_softcs.list, sc, gif_list);
    376 	mutex_exit(&gif_softcs.lock);
    377 	return 0;
    378 }
    379 
    380 static int
    381 gifattach0(struct gif_softc *sc)
    382 {
    383 	int rv;
    384 
    385 	sc->gif_if.if_addrlen = 0;
    386 	sc->gif_if.if_mtu    = GIF_MTU;
    387 	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
    388 	sc->gif_if.if_extflags  = IFEF_NO_LINK_STATE_CHANGE;
    389 #ifdef GIF_MPSAFE
    390 	sc->gif_if.if_extflags  |= IFEF_MPSAFE;
    391 #endif
    392 	sc->gif_if.if_ioctl  = gif_ioctl;
    393 	sc->gif_if.if_output = gif_output;
    394 	sc->gif_if.if_start = gif_start;
    395 	sc->gif_if.if_transmit = gif_transmit;
    396 	sc->gif_if.if_type   = IFT_GIF;
    397 	sc->gif_if.if_dlt    = DLT_NULL;
    398 	sc->gif_if.if_softc  = sc;
    399 	IFQ_SET_READY(&sc->gif_if.if_snd);
    400 	rv = if_initialize(&sc->gif_if);
    401 	if (rv != 0)
    402 		return rv;
    403 
    404 	if_alloc_sadl(&sc->gif_if);
    405 	bpf_attach(&sc->gif_if, DLT_NULL, sizeof(u_int));
    406 	if_register(&sc->gif_if);
    407 	return 0;
    408 }
    409 
    410 static int
    411 gif_clone_destroy(struct ifnet *ifp)
    412 {
    413 	struct gif_softc *sc = (void *) ifp;
    414 	struct gif_variant *var;
    415 
    416 	LIST_REMOVE(sc, gif_list);
    417 
    418 	gif_delete_tunnel(&sc->gif_if);
    419 	bpf_detach(ifp);
    420 	if_detach(ifp);
    421 
    422 	if_tunnel_free_ro_percpu(sc->gif_ro_percpu);
    423 
    424 	pserialize_destroy(sc->gif_psz);
    425 	mutex_destroy(&sc->gif_lock);
    426 
    427 	var = sc->gif_var;
    428 	kmem_free(var, sizeof(*var));
    429 	kmem_free(sc, sizeof(struct gif_softc));
    430 
    431 	return 0;
    432 }
    433 
    434 #ifdef GIF_ENCAPCHECK
    435 int
    436 gif_encapcheck(struct mbuf *m, int off, int proto, void *arg)
    437 {
    438 	struct ip ip;
    439 	struct gif_softc *sc;
    440 	struct gif_variant *var;
    441 	struct psref psref;
    442 	int ret = 0;
    443 
    444 	sc = arg;
    445 	if (sc == NULL)
    446 		return 0;
    447 
    448 	if ((sc->gif_if.if_flags & IFF_UP) == 0)
    449 		return 0;
    450 
    451 	var = gif_getref_variant(sc, &psref);
    452 	/* no physical address */
    453 	if (var->gv_psrc == NULL || var->gv_pdst == NULL)
    454 		goto out;
    455 
    456 	switch (proto) {
    457 #ifdef INET
    458 	case IPPROTO_IPV4:
    459 		break;
    460 #endif
    461 #ifdef INET6
    462 	case IPPROTO_IPV6:
    463 		break;
    464 #endif
    465 	default:
    466 		goto out;
    467 	}
    468 
    469 	/* Bail on short packets */
    470 	KASSERT(m->m_flags & M_PKTHDR);
    471 	if (m->m_pkthdr.len < sizeof(ip))
    472 		goto  out;
    473 
    474 	m_copydata(m, 0, sizeof(ip), &ip);
    475 
    476 	switch (ip.ip_v) {
    477 #ifdef INET
    478 	case 4:
    479 		if (var->gv_psrc->sa_family != AF_INET ||
    480 		    var->gv_pdst->sa_family != AF_INET)
    481 			goto out;
    482 		ret = gif_encapcheck4(m, off, proto, var);
    483 		break;
    484 #endif
    485 #ifdef INET6
    486 	case 6:
    487 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
    488 			goto out;
    489 		if (var->gv_psrc->sa_family != AF_INET6 ||
    490 		    var->gv_pdst->sa_family != AF_INET6)
    491 			goto out;
    492 		ret = gif_encapcheck6(m, off, proto, var);
    493 		break;
    494 #endif
    495 	default:
    496 		goto out;
    497 	}
    498 
    499 out:
    500 	gif_putref_variant(var, &psref);
    501 	return ret;
    502 }
    503 #endif
    504 
    505 /*
    506  * gif may cause infinite recursion calls when misconfigured.
    507  * We'll prevent this by introducing upper limit.
    508  */
    509 static int
    510 gif_check_nesting(struct ifnet *ifp, struct mbuf *m)
    511 {
    512 
    513 	return if_tunnel_check_nesting(ifp, m, max_gif_nesting);
    514 }
    515 
    516 static int
    517 gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    518     const struct rtentry *rt)
    519 {
    520 	struct gif_softc *sc = ifp->if_softc;
    521 	struct gif_variant *var = NULL;
    522 	struct psref psref;
    523 	int error = 0;
    524 
    525 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
    526 
    527 	if ((error = gif_check_nesting(ifp, m)) != 0) {
    528 		m_freem(m);
    529 		goto end;
    530 	}
    531 
    532 	if ((ifp->if_flags & IFF_UP) == 0) {
    533 		m_freem(m);
    534 		error = ENETDOWN;
    535 		goto end;
    536 	}
    537 
    538 	var = gif_getref_variant(sc, &psref);
    539 	if (var->gv_psrc == NULL || var->gv_pdst == NULL) {
    540 		m_freem(m);
    541 		error = ENETDOWN;
    542 		goto end;
    543 	}
    544 	/* XXX should we check if our outer source is legal? */
    545 
    546 	m->m_flags &= ~(M_BCAST | M_MCAST);
    547 
    548 	/* use DLT_NULL encapsulation here to pass inner af type */
    549 	M_PREPEND(m, sizeof(int), M_DONTWAIT);
    550 	if (!m) {
    551 		error = ENOBUFS;
    552 		goto end;
    553 	}
    554 	*mtod(m, int *) = dst->sa_family;
    555 
    556 	/* Clear checksum-offload flags. */
    557 	m->m_pkthdr.csum_flags = 0;
    558 	m->m_pkthdr.csum_data = 0;
    559 
    560 	error = if_transmit_lock(ifp, m);
    561 
    562 end:
    563 	if (var != NULL)
    564 		gif_putref_variant(var, &psref);
    565 	if (error)
    566 		if_statinc(ifp, if_oerrors);
    567 	return error;
    568 }
    569 
    570 static void
    571 gif_start(struct ifnet *ifp)
    572 {
    573 	struct gif_softc *sc;
    574 	struct gif_variant *var;
    575 	struct mbuf *m;
    576 	struct psref psref;
    577 	int family;
    578 	int len;
    579 	int error;
    580 
    581 	sc = ifp->if_softc;
    582 	var = gif_getref_variant(sc, &psref);
    583 
    584 	KASSERT(var->gv_output != NULL);
    585 
    586 	/* output processing */
    587 	while (1) {
    588 		IFQ_DEQUEUE(&sc->gif_if.if_snd, m);
    589 		if (m == NULL)
    590 			break;
    591 
    592 		/* grab and chop off inner af type */
    593 		if (sizeof(int) > m->m_len) {
    594 			m = m_pullup(m, sizeof(int));
    595 			if (!m) {
    596 				if_statinc(ifp, if_oerrors);
    597 				continue;
    598 			}
    599 		}
    600 		family = *mtod(m, int *);
    601 		bpf_mtap(ifp, m, BPF_D_OUT);
    602 		m_adj(m, sizeof(int));
    603 
    604 		len = m->m_pkthdr.len;
    605 
    606 		error = var->gv_output(var, family, m);
    607 		if (error)
    608 			if_statinc(ifp, if_oerrors);
    609 		else
    610 			if_statadd2(ifp, if_opackets, 1, if_obytes, len);
    611 	}
    612 
    613 	gif_putref_variant(var, &psref);
    614 }
    615 
    616 static int
    617 gif_transmit(struct ifnet *ifp, struct mbuf *m)
    618 {
    619 	struct gif_softc *sc;
    620 	struct gif_variant *var;
    621 	struct psref psref;
    622 	int error;
    623 
    624 	sc = ifp->if_softc;
    625 
    626 	/* output processing */
    627 	if (m == NULL)
    628 		return EINVAL;
    629 
    630 	var = gif_getref_variant(sc, &psref);
    631 	error = gif_transmit_direct(var, m);
    632 	gif_putref_variant(var, &psref);
    633 
    634 	return error;
    635 }
    636 
    637 static int
    638 gif_transmit_direct(struct gif_variant *var, struct mbuf *m)
    639 {
    640 	struct ifnet *ifp = &var->gv_softc->gif_if;
    641 	int error;
    642 	int family;
    643 	int len;
    644 
    645 	KASSERT(gif_heldref_variant(var));
    646 	KASSERT(var->gv_output != NULL);
    647 
    648 	/* grab and chop off inner af type */
    649 	if (sizeof(int) > m->m_len) {
    650 		m = m_pullup(m, sizeof(int));
    651 		if (!m) {
    652 			if_statinc(ifp, if_oerrors);
    653 			return ENOBUFS;
    654 		}
    655 	}
    656 	family = *mtod(m, int *);
    657 	bpf_mtap(ifp, m, BPF_D_OUT);
    658 	m_adj(m, sizeof(int));
    659 
    660 	len = m->m_pkthdr.len;
    661 
    662 	error = var->gv_output(var, family, m);
    663 	if (error)
    664 		if_statinc(ifp, if_oerrors);
    665 	else
    666 		if_statadd2(ifp, if_opackets, 1, if_obytes, len);
    667 
    668 	return error;
    669 }
    670 
    671 void
    672 gif_input(struct mbuf *m, int af, struct ifnet *ifp)
    673 {
    674 	pktqueue_t *pktq;
    675 	size_t pktlen;
    676 
    677 	if (ifp == NULL) {
    678 		/* just in case */
    679 		m_freem(m);
    680 		return;
    681 	}
    682 
    683 	m_set_rcvif(m, ifp);
    684 	pktlen = m->m_pkthdr.len;
    685 
    686 	bpf_mtap_af(ifp, af, m, BPF_D_IN);
    687 
    688 	/*
    689 	 * Put the packet to the network layer input queue according to the
    690 	 * specified address family.  Note: we avoid direct call to the
    691 	 * input function of the network layer in order to avoid recursion.
    692 	 * This may be revisited in the future.
    693 	 */
    694 	switch (af) {
    695 #ifdef INET
    696 	case AF_INET:
    697 		pktq = ip_pktq;
    698 		break;
    699 #endif
    700 #ifdef INET6
    701 	case AF_INET6:
    702 		pktq = ip6_pktq;
    703 		break;
    704 #endif
    705 	default:
    706 		m_freem(m);
    707 		return;
    708 	}
    709 
    710 #ifdef GIF_MPSAFE
    711 	const u_int h = curcpu()->ci_index;
    712 #else
    713 	const uint32_t h = pktq_rps_hash(m);
    714 #endif
    715 	if (__predict_true(pktq_enqueue(pktq, m, h))) {
    716 		if_statadd2(ifp, if_ibytes, pktlen, if_ipackets, 1);
    717 	} else {
    718 		m_freem(m);
    719 	}
    720 }
    721 
    722 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
    723 static int
    724 gif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    725 {
    726 	struct gif_softc *sc  = ifp->if_softc;
    727 	struct ifreq     *ifr = (struct ifreq*)data;
    728 	struct ifaddr    *ifa = (struct ifaddr*)data;
    729 	int error = 0, size, bound;
    730 	struct sockaddr *dst, *src;
    731 	struct gif_variant *var;
    732 	struct psref psref;
    733 
    734 	switch (cmd) {
    735 	case SIOCINITIFADDR:
    736 		ifp->if_flags |= IFF_UP;
    737 		ifa->ifa_rtrequest = p2p_rtrequest;
    738 		break;
    739 
    740 	case SIOCADDMULTI:
    741 	case SIOCDELMULTI:
    742 		switch (ifr->ifr_addr.sa_family) {
    743 #ifdef INET
    744 		case AF_INET:	/* IP supports Multicast */
    745 			break;
    746 #endif /* INET */
    747 #ifdef INET6
    748 		case AF_INET6:	/* IP6 supports Multicast */
    749 			break;
    750 #endif /* INET6 */
    751 		default:  /* Other protocols doesn't support Multicast */
    752 			error = EAFNOSUPPORT;
    753 			break;
    754 		}
    755 		break;
    756 
    757 	case SIOCSIFMTU:
    758 		if (ifr->ifr_mtu < GIF_MTU_MIN || ifr->ifr_mtu > GIF_MTU_MAX)
    759 			return EINVAL;
    760 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    761 			error = 0;
    762 		break;
    763 
    764 #ifdef INET
    765 	case SIOCSIFPHYADDR:
    766 #endif
    767 #ifdef INET6
    768 	case SIOCSIFPHYADDR_IN6:
    769 #endif /* INET6 */
    770 	case SIOCSLIFPHYADDR:
    771 		switch (cmd) {
    772 #ifdef INET
    773 		case SIOCSIFPHYADDR:
    774 			src = (struct sockaddr *)
    775 				&(((struct in_aliasreq *)data)->ifra_addr);
    776 			dst = (struct sockaddr *)
    777 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
    778 			break;
    779 #endif
    780 #ifdef INET6
    781 		case SIOCSIFPHYADDR_IN6:
    782 			src = (struct sockaddr *)
    783 				&(((struct in6_aliasreq *)data)->ifra_addr);
    784 			dst = (struct sockaddr *)
    785 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
    786 			break;
    787 #endif
    788 		case SIOCSLIFPHYADDR:
    789 			src = (struct sockaddr *)
    790 				&(((struct if_laddrreq *)data)->addr);
    791 			dst = (struct sockaddr *)
    792 				&(((struct if_laddrreq *)data)->dstaddr);
    793 			break;
    794 		default:
    795 			return EINVAL;
    796 		}
    797 
    798 		/* sa_family must be equal */
    799 		if (src->sa_family != dst->sa_family)
    800 			return EINVAL;
    801 
    802 		/* validate sa_len */
    803 		switch (src->sa_family) {
    804 #ifdef INET
    805 		case AF_INET:
    806 			if (src->sa_len != sizeof(struct sockaddr_in))
    807 				return EINVAL;
    808 			break;
    809 #endif
    810 #ifdef INET6
    811 		case AF_INET6:
    812 			if (src->sa_len != sizeof(struct sockaddr_in6))
    813 				return EINVAL;
    814 			break;
    815 #endif
    816 		default:
    817 			return EAFNOSUPPORT;
    818 		}
    819 		switch (dst->sa_family) {
    820 #ifdef INET
    821 		case AF_INET:
    822 			if (dst->sa_len != sizeof(struct sockaddr_in))
    823 				return EINVAL;
    824 			break;
    825 #endif
    826 #ifdef INET6
    827 		case AF_INET6:
    828 			if (dst->sa_len != sizeof(struct sockaddr_in6))
    829 				return EINVAL;
    830 			break;
    831 #endif
    832 		default:
    833 			return EAFNOSUPPORT;
    834 		}
    835 
    836 		/* check sa_family looks sane for the cmd */
    837 		switch (cmd) {
    838 		case SIOCSIFPHYADDR:
    839 			if (src->sa_family == AF_INET)
    840 				break;
    841 			return EAFNOSUPPORT;
    842 #ifdef INET6
    843 		case SIOCSIFPHYADDR_IN6:
    844 			if (src->sa_family == AF_INET6)
    845 				break;
    846 			return EAFNOSUPPORT;
    847 #endif /* INET6 */
    848 		case SIOCSLIFPHYADDR:
    849 			/* checks done in the above */
    850 			break;
    851 		}
    852 		/*
    853 		 * calls gif_getref_variant() for other softcs to check
    854 		 * address pair duplicattion
    855 		 */
    856 		bound = curlwp_bind();
    857 		error = gif_set_tunnel(&sc->gif_if, src, dst);
    858 		curlwp_bindx(bound);
    859 		break;
    860 
    861 #ifdef SIOCDIFPHYADDR
    862 	case SIOCDIFPHYADDR:
    863 		bound = curlwp_bind();
    864 		gif_delete_tunnel(&sc->gif_if);
    865 		curlwp_bindx(bound);
    866 		break;
    867 #endif
    868 
    869 	case SIOCGIFPSRCADDR:
    870 #ifdef INET6
    871 	case SIOCGIFPSRCADDR_IN6:
    872 #endif /* INET6 */
    873 		bound = curlwp_bind();
    874 		var = gif_getref_variant(sc, &psref);
    875 		if (var->gv_psrc == NULL) {
    876 			gif_putref_variant(var, &psref);
    877 			curlwp_bindx(bound);
    878 			error = EADDRNOTAVAIL;
    879 			goto bad;
    880 		}
    881 		src = var->gv_psrc;
    882 		switch (cmd) {
    883 #ifdef INET
    884 		case SIOCGIFPSRCADDR:
    885 			dst = &ifr->ifr_addr;
    886 			size = sizeof(ifr->ifr_addr);
    887 			break;
    888 #endif /* INET */
    889 #ifdef INET6
    890 		case SIOCGIFPSRCADDR_IN6:
    891 			dst = (struct sockaddr *)
    892 				&(((struct in6_ifreq *)data)->ifr_addr);
    893 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    894 			break;
    895 #endif /* INET6 */
    896 		default:
    897 			gif_putref_variant(var, &psref);
    898 			curlwp_bindx(bound);
    899 			error = EADDRNOTAVAIL;
    900 			goto bad;
    901 		}
    902 		if (src->sa_len > size) {
    903 			gif_putref_variant(var, &psref);
    904 			curlwp_bindx(bound);
    905 			return EINVAL;
    906 		}
    907 		memcpy(dst, src, src->sa_len);
    908 		gif_putref_variant(var, &psref);
    909 		curlwp_bindx(bound);
    910 		break;
    911 
    912 	case SIOCGIFPDSTADDR:
    913 #ifdef INET6
    914 	case SIOCGIFPDSTADDR_IN6:
    915 #endif /* INET6 */
    916 		bound = curlwp_bind();
    917 		var = gif_getref_variant(sc, &psref);
    918 		if (var->gv_pdst == NULL) {
    919 			gif_putref_variant(var, &psref);
    920 			curlwp_bindx(bound);
    921 			error = EADDRNOTAVAIL;
    922 			goto bad;
    923 		}
    924 		src = var->gv_pdst;
    925 		switch (cmd) {
    926 #ifdef INET
    927 		case SIOCGIFPDSTADDR:
    928 			dst = &ifr->ifr_addr;
    929 			size = sizeof(ifr->ifr_addr);
    930 			break;
    931 #endif /* INET */
    932 #ifdef INET6
    933 		case SIOCGIFPDSTADDR_IN6:
    934 			dst = (struct sockaddr *)
    935 				&(((struct in6_ifreq *)data)->ifr_addr);
    936 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    937 			break;
    938 #endif /* INET6 */
    939 		default:
    940 			gif_putref_variant(var, &psref);
    941 			curlwp_bindx(bound);
    942 			error = EADDRNOTAVAIL;
    943 			goto bad;
    944 		}
    945 		if (src->sa_len > size) {
    946 			gif_putref_variant(var, &psref);
    947 			curlwp_bindx(bound);
    948 			return EINVAL;
    949 		}
    950 		memcpy(dst, src, src->sa_len);
    951 		gif_putref_variant(var, &psref);
    952 		curlwp_bindx(bound);
    953 		break;
    954 
    955 	case SIOCGLIFPHYADDR:
    956 		bound = curlwp_bind();
    957 		var = gif_getref_variant(sc, &psref);
    958 		if (var->gv_psrc == NULL || var->gv_pdst == NULL) {
    959 			gif_putref_variant(var, &psref);
    960 			curlwp_bindx(bound);
    961 			error = EADDRNOTAVAIL;
    962 			goto bad;
    963 		}
    964 
    965 		/* copy src */
    966 		src = var->gv_psrc;
    967 		dst = (struct sockaddr *)
    968 			&(((struct if_laddrreq *)data)->addr);
    969 		size = sizeof(((struct if_laddrreq *)data)->addr);
    970 		if (src->sa_len > size) {
    971 			gif_putref_variant(var, &psref);
    972 			curlwp_bindx(bound);
    973 			return EINVAL;
    974 		}
    975 		memcpy(dst, src, src->sa_len);
    976 
    977 		/* copy dst */
    978 		src = var->gv_pdst;
    979 		dst = (struct sockaddr *)
    980 			&(((struct if_laddrreq *)data)->dstaddr);
    981 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
    982 		if (src->sa_len > size) {
    983 			gif_putref_variant(var, &psref);
    984 			curlwp_bindx(bound);
    985 			return EINVAL;
    986 		}
    987 		memcpy(dst, src, src->sa_len);
    988 		gif_putref_variant(var, &psref);
    989 		curlwp_bindx(bound);
    990 		break;
    991 
    992 	default:
    993 		return ifioctl_common(ifp, cmd, data);
    994 	}
    995  bad:
    996 	return error;
    997 }
    998 
    999 static int
   1000 gif_encap_attach(struct gif_variant *var)
   1001 {
   1002 	int error;
   1003 
   1004 	if (var == NULL || var->gv_psrc == NULL)
   1005 		return EINVAL;
   1006 
   1007 	switch (var->gv_psrc->sa_family) {
   1008 #ifdef INET
   1009 	case AF_INET:
   1010 		error = in_gif_attach(var);
   1011 		break;
   1012 #endif
   1013 #ifdef INET6
   1014 	case AF_INET6:
   1015 		error = in6_gif_attach(var);
   1016 		break;
   1017 #endif
   1018 	default:
   1019 		error = EINVAL;
   1020 		break;
   1021 	}
   1022 
   1023 	return error;
   1024 }
   1025 
   1026 static int
   1027 gif_encap_detach(struct gif_variant *var)
   1028 {
   1029 	int error;
   1030 
   1031 	if (var == NULL || var->gv_psrc == NULL)
   1032 		return EINVAL;
   1033 
   1034 	switch (var->gv_psrc->sa_family) {
   1035 #ifdef INET
   1036 	case AF_INET:
   1037 		error = in_gif_detach(var);
   1038 		break;
   1039 #endif
   1040 #ifdef INET6
   1041 	case AF_INET6:
   1042 		error = in6_gif_detach(var);
   1043 		break;
   1044 #endif
   1045 	default:
   1046 		error = EINVAL;
   1047 		break;
   1048 	}
   1049 
   1050 	return error;
   1051 }
   1052 
   1053 static int
   1054 gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
   1055 {
   1056 	struct gif_softc *sc = ifp->if_softc;
   1057 	struct gif_softc *sc2;
   1058 	struct gif_variant *ovar, *nvar;
   1059 	struct sockaddr *osrc, *odst;
   1060 	struct sockaddr *nsrc, *ndst;
   1061 	int error;
   1062 #ifndef GIF_MPSAFE
   1063 	int s;
   1064 
   1065 	s = splsoftnet();
   1066 #endif
   1067 	error = encap_lock_enter();
   1068 	if (error) {
   1069 #ifndef GIF_MPSAFE
   1070 		splx(s);
   1071 #endif
   1072 		return error;
   1073 	}
   1074 
   1075 	nsrc = sockaddr_dup(src, M_WAITOK);
   1076 	ndst = sockaddr_dup(dst, M_WAITOK);
   1077 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
   1078 
   1079 	mutex_enter(&sc->gif_lock);
   1080 
   1081 	ovar = sc->gif_var;
   1082 
   1083 	if ((ovar->gv_pdst && sockaddr_cmp(ovar->gv_pdst, dst) == 0) &&
   1084 	    (ovar->gv_psrc && sockaddr_cmp(ovar->gv_psrc, src) == 0)) {
   1085 		/* address and port pair not changed. */
   1086 		error = 0;
   1087 		goto out;
   1088 	}
   1089 
   1090 	mutex_enter(&gif_softcs.lock);
   1091 	LIST_FOREACH(sc2, &gif_softcs.list, gif_list) {
   1092 		struct gif_variant *var2;
   1093 		struct psref psref;
   1094 
   1095 		if (sc2 == sc)
   1096 			continue;
   1097 		var2 = gif_getref_variant(sc2, &psref);
   1098 		if (!var2->gv_pdst || !var2->gv_psrc) {
   1099 			gif_putref_variant(var2, &psref);
   1100 			continue;
   1101 		}
   1102 		/* can't configure same pair of address onto two gifs */
   1103 		if (sockaddr_cmp(var2->gv_pdst, dst) == 0 &&
   1104 		    sockaddr_cmp(var2->gv_psrc, src) == 0) {
   1105 			/* continue to use the old configuration. */
   1106 			gif_putref_variant(var2, &psref);
   1107 			mutex_exit(&gif_softcs.lock);
   1108 			error =  EADDRNOTAVAIL;
   1109 			goto out;
   1110 		}
   1111 		gif_putref_variant(var2, &psref);
   1112 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
   1113 	}
   1114 	mutex_exit(&gif_softcs.lock);
   1115 
   1116 	osrc = ovar->gv_psrc;
   1117 	odst = ovar->gv_pdst;
   1118 
   1119 	*nvar = *ovar;
   1120 	nvar->gv_psrc = nsrc;
   1121 	nvar->gv_pdst = ndst;
   1122 	nvar->gv_encap_cookie4 = NULL;
   1123 	nvar->gv_encap_cookie6 = NULL;
   1124 	error = gif_encap_attach(nvar);
   1125 	if (error)
   1126 		goto out;
   1127 	psref_target_init(&nvar->gv_psref, gv_psref_class);
   1128 	membar_producer();
   1129 	gif_update_variant(sc, nvar);
   1130 
   1131 	mutex_exit(&sc->gif_lock);
   1132 
   1133 	(void)gif_encap_detach(ovar);
   1134 	encap_lock_exit();
   1135 
   1136 	if (osrc)
   1137 		sockaddr_free(osrc);
   1138 	if (odst)
   1139 		sockaddr_free(odst);
   1140 	kmem_free(ovar, sizeof(*ovar));
   1141 
   1142 #ifndef GIF_MPSAFE
   1143 	splx(s);
   1144 #endif
   1145 	return 0;
   1146 
   1147  out:
   1148 	sockaddr_free(nsrc);
   1149 	sockaddr_free(ndst);
   1150 	kmem_free(nvar, sizeof(*nvar));
   1151 
   1152 	mutex_exit(&sc->gif_lock);
   1153 	encap_lock_exit();
   1154 #ifndef GIF_MPSAFE
   1155 	splx(s);
   1156 #endif
   1157 	return error;
   1158 }
   1159 
   1160 static void
   1161 gif_delete_tunnel(struct ifnet *ifp)
   1162 {
   1163 	struct gif_softc *sc = ifp->if_softc;
   1164 	struct gif_variant *ovar, *nvar;
   1165 	struct sockaddr *osrc, *odst;
   1166 	int error;
   1167 #ifndef GIF_MPSAFE
   1168 	int s;
   1169 
   1170 	s = splsoftnet();
   1171 #endif
   1172 	error = encap_lock_enter();
   1173 	if (error) {
   1174 #ifndef GIF_MPSAFE
   1175 		splx(s);
   1176 #endif
   1177 		return;
   1178 	}
   1179 
   1180 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
   1181 
   1182 	mutex_enter(&sc->gif_lock);
   1183 
   1184 	ovar = sc->gif_var;
   1185 	osrc = ovar->gv_psrc;
   1186 	odst = ovar->gv_pdst;
   1187 	if (osrc == NULL || odst == NULL) {
   1188 		/* address pair not changed. */
   1189 		mutex_exit(&sc->gif_lock);
   1190 		encap_lock_exit();
   1191 		kmem_free(nvar, sizeof(*nvar));
   1192 #ifndef GIF_MPSAFE
   1193 		splx(s);
   1194 #endif
   1195 		return;
   1196 	}
   1197 
   1198 	*nvar = *ovar;
   1199 	nvar->gv_psrc = NULL;
   1200 	nvar->gv_pdst = NULL;
   1201 	nvar->gv_encap_cookie4 = NULL;
   1202 	nvar->gv_encap_cookie6 = NULL;
   1203 	nvar->gv_output = NULL;
   1204 	psref_target_init(&nvar->gv_psref, gv_psref_class);
   1205 	membar_producer();
   1206 	gif_update_variant(sc, nvar);
   1207 
   1208 	mutex_exit(&sc->gif_lock);
   1209 
   1210 	gif_encap_detach(ovar);
   1211 	encap_lock_exit();
   1212 
   1213 	sockaddr_free(osrc);
   1214 	sockaddr_free(odst);
   1215 	kmem_free(ovar, sizeof(*ovar));
   1216 
   1217 #ifndef GIF_MPSAFE
   1218 	splx(s);
   1219 #endif
   1220 }
   1221 
   1222 /*
   1223  * gif_variant update API.
   1224  *
   1225  * Assumption:
   1226  * reader side dereferences sc->gif_var in reader critical section only,
   1227  * that is, all of reader sides do not reader the sc->gif_var after
   1228  * pserialize_perform().
   1229  */
   1230 static void
   1231 gif_update_variant(struct gif_softc *sc, struct gif_variant *nvar)
   1232 {
   1233 	struct ifnet *ifp = &sc->gif_if;
   1234 	struct gif_variant *ovar = sc->gif_var;
   1235 
   1236 	KASSERT(mutex_owned(&sc->gif_lock));
   1237 
   1238 	sc->gif_var = nvar;
   1239 	pserialize_perform(sc->gif_psz);
   1240 	psref_target_destroy(&ovar->gv_psref, gv_psref_class);
   1241 
   1242 	if (nvar->gv_psrc != NULL && nvar->gv_pdst != NULL)
   1243 		ifp->if_flags |= IFF_RUNNING;
   1244 	else
   1245 		ifp->if_flags &= ~IFF_RUNNING;
   1246 }
   1247 
   1248 /*
   1249  * Module infrastructure
   1250  */
   1251 #include "if_module.h"
   1252 
   1253 IF_MODULE(MODULE_CLASS_DRIVER, gif, "ip_ecn")
   1254