Home | History | Annotate | Line # | Download | only in net
if_l2tp.c revision 1.27
      1 /*	$NetBSD: if_l2tp.c,v 1.27 2018/05/08 06:08:19 maxv 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 /*
     30  * L2TPv3 kernel interface
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: if_l2tp.c,v 1.27 2018/05/08 06:08:19 maxv 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/syslog.h>
     51 #include <sys/proc.h>
     52 #include <sys/conf.h>
     53 #include <sys/kauth.h>
     54 #include <sys/cpu.h>
     55 #include <sys/cprng.h>
     56 #include <sys/intr.h>
     57 #include <sys/kmem.h>
     58 #include <sys/mutex.h>
     59 #include <sys/atomic.h>
     60 #include <sys/pserialize.h>
     61 #include <sys/device.h>
     62 #include <sys/module.h>
     63 
     64 #include <net/if.h>
     65 #include <net/if_dl.h>
     66 #include <net/if_ether.h>
     67 #include <net/if_types.h>
     68 #include <net/netisr.h>
     69 #include <net/route.h>
     70 #include <net/bpf.h>
     71 #include <net/if_vlanvar.h>
     72 
     73 #include <netinet/in.h>
     74 #include <netinet/in_systm.h>
     75 #include <netinet/ip.h>
     76 #include <netinet/ip_encap.h>
     77 #ifdef	INET
     78 #include <netinet/in_var.h>
     79 #include <netinet/in_l2tp.h>
     80 #endif	/* INET */
     81 #ifdef INET6
     82 #include <netinet6/in6_l2tp.h>
     83 #endif
     84 
     85 #include <net/if_l2tp.h>
     86 
     87 #include <net/if_vlanvar.h>
     88 
     89 /* TODO: IP_TCPMSS support */
     90 #undef IP_TCPMSS
     91 #ifdef IP_TCPMSS
     92 #include <netinet/ip_tcpmss.h>
     93 #endif
     94 
     95 #include <net/bpf.h>
     96 
     97 /*
     98  * l2tp global variable definitions
     99  */
    100 LIST_HEAD(l2tp_sclist, l2tp_softc);
    101 static struct {
    102 	struct l2tp_sclist list;
    103 	kmutex_t lock;
    104 } l2tp_softcs __cacheline_aligned;
    105 
    106 
    107 #if !defined(L2TP_ID_HASH_SIZE)
    108 #define L2TP_ID_HASH_SIZE 64
    109 #endif
    110 static struct {
    111 	kmutex_t lock;
    112 	struct pslist_head *lists;
    113 	u_long mask;
    114 } l2tp_hash __cacheline_aligned = {
    115 	.lists = NULL,
    116 };
    117 
    118 pserialize_t l2tp_psz __read_mostly;
    119 struct psref_class *lv_psref_class __read_mostly;
    120 
    121 static void	l2tp_ro_init_pc(void *, void *, struct cpu_info *);
    122 static void	l2tp_ro_fini_pc(void *, void *, struct cpu_info *);
    123 
    124 static int	l2tp_clone_create(struct if_clone *, int);
    125 static int	l2tp_clone_destroy(struct ifnet *);
    126 
    127 struct if_clone l2tp_cloner =
    128     IF_CLONE_INITIALIZER("l2tp", l2tp_clone_create, l2tp_clone_destroy);
    129 
    130 static int	l2tp_output(struct ifnet *, struct mbuf *,
    131 		    const struct sockaddr *, const struct rtentry *);
    132 static void	l2tpintr(struct l2tp_variant *);
    133 
    134 static void	l2tp_hash_init(void);
    135 static int	l2tp_hash_fini(void);
    136 
    137 static void	l2tp_start(struct ifnet *);
    138 static int	l2tp_transmit(struct ifnet *, struct mbuf *);
    139 
    140 static int	l2tp_set_tunnel(struct ifnet *, struct sockaddr *,
    141 		    struct sockaddr *);
    142 static void	l2tp_delete_tunnel(struct ifnet *);
    143 
    144 static int	id_hash_func(uint32_t, u_long);
    145 
    146 static void	l2tp_variant_update(struct l2tp_softc *, struct l2tp_variant *);
    147 static int	l2tp_set_session(struct l2tp_softc *, uint32_t, uint32_t);
    148 static int	l2tp_clear_session(struct l2tp_softc *);
    149 static int	l2tp_set_cookie(struct l2tp_softc *, uint64_t, u_int, uint64_t, u_int);
    150 static void	l2tp_clear_cookie(struct l2tp_softc *);
    151 static void	l2tp_set_state(struct l2tp_softc *, int);
    152 static int	l2tp_encap_attach(struct l2tp_variant *);
    153 static int	l2tp_encap_detach(struct l2tp_variant *);
    154 
    155 #ifndef MAX_L2TP_NEST
    156 /*
    157  * This macro controls the upper limitation on nesting of l2tp tunnels.
    158  * Since, setting a large value to this macro with a careless configuration
    159  * may introduce system crash, we don't allow any nestings by default.
    160  * If you need to configure nested l2tp tunnels, you can define this macro
    161  * in your kernel configuration file.  However, if you do so, please be
    162  * careful to configure the tunnels so that it won't make a loop.
    163  */
    164 /*
    165  * XXX
    166  * Currently, if in_l2tp_output recursively calls, it causes locking against
    167  * myself of struct l2tp_ro->lr_lock. So, nested l2tp tunnels is prohibited.
    168  */
    169 #define MAX_L2TP_NEST 0
    170 #endif
    171 
    172 static int max_l2tp_nesting = MAX_L2TP_NEST;
    173 
    174 /* ARGSUSED */
    175 void
    176 l2tpattach(int count)
    177 {
    178 	/*
    179 	 * Nothing to do here, initialization is handled by the
    180 	 * module initialization code in l2tpinit() below).
    181 	 */
    182 }
    183 
    184 static void
    185 l2tpinit(void)
    186 {
    187 
    188 	mutex_init(&l2tp_softcs.lock, MUTEX_DEFAULT, IPL_NONE);
    189 	LIST_INIT(&l2tp_softcs.list);
    190 
    191 	mutex_init(&l2tp_hash.lock, MUTEX_DEFAULT, IPL_NONE);
    192 	l2tp_psz = pserialize_create();
    193 	lv_psref_class = psref_class_create("l2tpvar", IPL_SOFTNET);
    194 	if_clone_attach(&l2tp_cloner);
    195 
    196 	l2tp_hash_init();
    197 }
    198 
    199 static int
    200 l2tpdetach(void)
    201 {
    202 	int error;
    203 
    204 	mutex_enter(&l2tp_softcs.lock);
    205 	if (!LIST_EMPTY(&l2tp_softcs.list)) {
    206 		mutex_exit(&l2tp_softcs.lock);
    207 		return EBUSY;
    208 	}
    209 	mutex_exit(&l2tp_softcs.lock);
    210 
    211 	error = l2tp_hash_fini();
    212 	if (error)
    213 		return error;
    214 
    215 	if_clone_detach(&l2tp_cloner);
    216 	psref_class_destroy(lv_psref_class);
    217 	pserialize_destroy(l2tp_psz);
    218 	mutex_destroy(&l2tp_hash.lock);
    219 
    220 	mutex_destroy(&l2tp_softcs.lock);
    221 
    222 	return error;
    223 }
    224 
    225 static int
    226 l2tp_clone_create(struct if_clone *ifc, int unit)
    227 {
    228 	struct l2tp_softc *sc;
    229 	struct l2tp_variant *var;
    230 	int rv;
    231 
    232 	sc = kmem_zalloc(sizeof(struct l2tp_softc), KM_SLEEP);
    233 	if_initname(&sc->l2tp_ec.ec_if, ifc->ifc_name, unit);
    234 	rv = l2tpattach0(sc);
    235 	if (rv != 0) {
    236 		kmem_free(sc, sizeof(struct l2tp_softc));
    237 		return rv;
    238 	}
    239 
    240 	var = kmem_zalloc(sizeof(struct l2tp_variant), KM_SLEEP);
    241 	var->lv_softc = sc;
    242 	var->lv_state = L2TP_STATE_DOWN;
    243 	var->lv_use_cookie = L2TP_COOKIE_OFF;
    244 	psref_target_init(&var->lv_psref, lv_psref_class);
    245 
    246 	sc->l2tp_var = var;
    247 	mutex_init(&sc->l2tp_lock, MUTEX_DEFAULT, IPL_NONE);
    248 	PSLIST_ENTRY_INIT(sc, l2tp_hash);
    249 
    250 	sc->l2tp_ro_percpu = percpu_alloc(sizeof(struct l2tp_ro));
    251 	percpu_foreach(sc->l2tp_ro_percpu, l2tp_ro_init_pc, NULL);
    252 
    253 	mutex_enter(&l2tp_softcs.lock);
    254 	LIST_INSERT_HEAD(&l2tp_softcs.list, sc, l2tp_list);
    255 	mutex_exit(&l2tp_softcs.lock);
    256 
    257 	return (0);
    258 }
    259 
    260 int
    261 l2tpattach0(struct l2tp_softc *sc)
    262 {
    263 	int rv;
    264 
    265 	sc->l2tp_ec.ec_if.if_addrlen = 0;
    266 	sc->l2tp_ec.ec_if.if_mtu    = L2TP_MTU;
    267 	sc->l2tp_ec.ec_if.if_flags  = IFF_POINTOPOINT|IFF_MULTICAST|IFF_SIMPLEX;
    268 	sc->l2tp_ec.ec_if.if_extflags = IFEF_NO_LINK_STATE_CHANGE;
    269 #ifdef NET_MPSAFE
    270 	sc->l2tp_ec.ec_if.if_extflags |= IFEF_MPSAFE;
    271 #endif
    272 	sc->l2tp_ec.ec_if.if_ioctl  = l2tp_ioctl;
    273 	sc->l2tp_ec.ec_if.if_output = l2tp_output;
    274 	sc->l2tp_ec.ec_if.if_type   = IFT_L2TP;
    275 	sc->l2tp_ec.ec_if.if_dlt    = DLT_NULL;
    276 	sc->l2tp_ec.ec_if.if_start  = l2tp_start;
    277 	sc->l2tp_ec.ec_if.if_transmit = l2tp_transmit;
    278 	sc->l2tp_ec.ec_if._if_input = ether_input;
    279 	IFQ_SET_READY(&sc->l2tp_ec.ec_if.if_snd);
    280 	/* XXX
    281 	 * It may improve performance to use if_initialize()/if_register()
    282 	 * so that l2tp_input() calls if_input() instead of
    283 	 * if_percpuq_enqueue(). However, that causes recursive softnet_lock
    284 	 * when NET_MPSAFE is not set.
    285 	 */
    286 	rv = if_attach(&sc->l2tp_ec.ec_if);
    287 	if (rv != 0)
    288 		return rv;
    289 	if_alloc_sadl(&sc->l2tp_ec.ec_if);
    290 	bpf_attach(&sc->l2tp_ec.ec_if, DLT_EN10MB, sizeof(struct ether_header));
    291 
    292 	return 0;
    293 }
    294 
    295 void
    296 l2tp_ro_init_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
    297 {
    298 	struct l2tp_ro *lro = p;
    299 
    300 	lro->lr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    301 }
    302 
    303 void
    304 l2tp_ro_fini_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
    305 {
    306 	struct l2tp_ro *lro = p;
    307 
    308 	rtcache_free(&lro->lr_ro);
    309 
    310 	mutex_obj_free(lro->lr_lock);
    311 }
    312 
    313 static int
    314 l2tp_clone_destroy(struct ifnet *ifp)
    315 {
    316 	struct l2tp_variant *var;
    317 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    318 	    l2tp_ec.ec_if);
    319 
    320 	l2tp_clear_session(sc);
    321 	l2tp_delete_tunnel(&sc->l2tp_ec.ec_if);
    322 	/*
    323 	 * To avoid for l2tp_transmit() to access sc->l2tp_var after free it.
    324 	 */
    325 	mutex_enter(&sc->l2tp_lock);
    326 	var = sc->l2tp_var;
    327 	l2tp_variant_update(sc, NULL);
    328 	mutex_exit(&sc->l2tp_lock);
    329 
    330 	mutex_enter(&l2tp_softcs.lock);
    331 	LIST_REMOVE(sc, l2tp_list);
    332 	mutex_exit(&l2tp_softcs.lock);
    333 
    334 	bpf_detach(ifp);
    335 
    336 	if_detach(ifp);
    337 
    338 	percpu_foreach(sc->l2tp_ro_percpu, l2tp_ro_fini_pc, NULL);
    339 	percpu_free(sc->l2tp_ro_percpu, sizeof(struct l2tp_ro));
    340 
    341 	kmem_free(var, sizeof(struct l2tp_variant));
    342 	mutex_destroy(&sc->l2tp_lock);
    343 	kmem_free(sc, sizeof(struct l2tp_softc));
    344 
    345 	return 0;
    346 }
    347 
    348 static int
    349 l2tp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    350     const struct rtentry *rt)
    351 {
    352 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    353 	    l2tp_ec.ec_if);
    354 	struct l2tp_variant *var;
    355 	struct psref psref;
    356 	int error = 0;
    357 
    358 	var = l2tp_getref_variant(sc, &psref);
    359 	if (var == NULL) {
    360 		m_freem(m);
    361 		return ENETDOWN;
    362 	}
    363 
    364 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
    365 
    366 	m->m_flags &= ~(M_BCAST|M_MCAST);
    367 
    368 	if ((ifp->if_flags & IFF_UP) == 0) {
    369 		m_freem(m);
    370 		error = ENETDOWN;
    371 		goto end;
    372 	}
    373 
    374 	if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
    375 		m_freem(m);
    376 		error = ENETDOWN;
    377 		goto end;
    378 	}
    379 
    380 	/* XXX should we check if our outer source is legal? */
    381 
    382 	/* use DLT_NULL encapsulation here to pass inner af type */
    383 	M_PREPEND(m, sizeof(int), M_DONTWAIT);
    384 	if (!m) {
    385 		error = ENOBUFS;
    386 		goto end;
    387 	}
    388 	*mtod(m, int *) = dst->sa_family;
    389 
    390 	IFQ_ENQUEUE(&ifp->if_snd, m, error);
    391 	if (error)
    392 		goto end;
    393 
    394 	/*
    395 	 * direct call to avoid infinite loop at l2tpintr()
    396 	 */
    397 	l2tpintr(var);
    398 
    399 	error = 0;
    400 
    401 end:
    402 	l2tp_putref_variant(var, &psref);
    403 	if (error)
    404 		ifp->if_oerrors++;
    405 
    406 	return error;
    407 }
    408 
    409 static void
    410 l2tpintr(struct l2tp_variant *var)
    411 {
    412 	struct l2tp_softc *sc;
    413 	struct ifnet *ifp;
    414 	struct mbuf *m;
    415 	int error;
    416 
    417 	KASSERT(psref_held(&var->lv_psref, lv_psref_class));
    418 
    419 	sc = var->lv_softc;
    420 	ifp = &sc->l2tp_ec.ec_if;
    421 
    422 	/* output processing */
    423 	if (var->lv_my_sess_id == 0 || var->lv_peer_sess_id == 0) {
    424 		IFQ_PURGE(&ifp->if_snd);
    425 		return;
    426 	}
    427 
    428 	for (;;) {
    429 		IFQ_DEQUEUE(&ifp->if_snd, m);
    430 		if (m == NULL)
    431 			break;
    432 		m->m_flags &= ~(M_BCAST|M_MCAST);
    433 		bpf_mtap(ifp, m);
    434 		switch (var->lv_psrc->sa_family) {
    435 #ifdef INET
    436 		case AF_INET:
    437 			error = in_l2tp_output(var, m);
    438 			break;
    439 #endif
    440 #ifdef INET6
    441 		case AF_INET6:
    442 			error = in6_l2tp_output(var, m);
    443 			break;
    444 #endif
    445 		default:
    446 			m_freem(m);
    447 			error = ENETDOWN;
    448 			break;
    449 		}
    450 
    451 		if (error)
    452 			ifp->if_oerrors++;
    453 		else {
    454 			ifp->if_opackets++;
    455 			/*
    456 			 * obytes is incremented at ether_output() or
    457 			 * bridge_enqueue().
    458 			 */
    459 		}
    460 	}
    461 
    462 }
    463 
    464 void
    465 l2tp_input(struct mbuf *m, struct ifnet *ifp)
    466 {
    467 	vaddr_t addr;
    468 
    469 	KASSERT(ifp != NULL);
    470 
    471 	/*
    472 	 * Currently, l2tp(4) supports only ethernet as inner protocol.
    473 	 */
    474 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
    475 		m_freem(m);
    476 		return;
    477 	}
    478 
    479 	/*
    480 	 * If the head of the payload is not aligned, align it.
    481 	 */
    482 	addr = mtod(m, vaddr_t);
    483 	if ((addr & 0x03) != 0x2) {
    484 		/* copy and align head of payload */
    485 		struct mbuf *m_head;
    486 		int copy_length;
    487 		u_int pad = roundup(sizeof(struct ether_header), 4)
    488 			- sizeof(struct ether_header);
    489 
    490 #define L2TP_COPY_LENGTH		60
    491 
    492 		if (m->m_pkthdr.len < L2TP_COPY_LENGTH) {
    493 			copy_length = m->m_pkthdr.len;
    494 		} else {
    495 			copy_length = L2TP_COPY_LENGTH;
    496 		}
    497 
    498 		if (m->m_len < copy_length) {
    499 			m = m_pullup(m, copy_length);
    500 			if (m == NULL)
    501 				return;
    502 		}
    503 
    504 		MGETHDR(m_head, M_DONTWAIT, MT_HEADER);
    505 		if (m_head == NULL) {
    506 			m_freem(m);
    507 			return;
    508 		}
    509 		M_MOVE_PKTHDR(m_head, m);
    510 
    511 		/*
    512 		 * m_head should be:
    513 		 *                             L2TP_COPY_LENGTH
    514 		 *                          <-  + roundup(pad, 4) - pad ->
    515 		 *   +-------+--------+-----+--------------+-------------+
    516 		 *   | m_hdr | pkthdr | ... | ether header |   payload   |
    517 		 *   +-------+--------+-----+--------------+-------------+
    518 		 *                          ^              ^
    519 		 *                          m_data         4 byte aligned
    520 		 */
    521 		MH_ALIGN(m_head, L2TP_COPY_LENGTH + roundup(pad, 4));
    522 		m_head->m_data += pad;
    523 
    524 		memcpy(mtod(m_head, void *), mtod(m, void *), copy_length);
    525 		m_head->m_len = copy_length;
    526 		m->m_data += copy_length;
    527 		m->m_len -= copy_length;
    528 
    529 		/* construct chain */
    530 		if (m->m_len == 0) {
    531 			m_head->m_next = m_free(m);
    532 		} else {
    533 			m_head->m_next = m;
    534 		}
    535 
    536 		/* override m */
    537 		m = m_head;
    538 	}
    539 
    540 	m_set_rcvif(m, ifp);
    541 
    542 	/*
    543 	 * bpf_mtap() and ifp->if_ipackets++ is done in if_input()
    544 	 *
    545 	 * obytes is incremented at ether_output() or bridge_enqueue().
    546 	 */
    547 	if_percpuq_enqueue(ifp->if_percpuq, m);
    548 }
    549 
    550 void
    551 l2tp_start(struct ifnet *ifp)
    552 {
    553 	struct psref psref;
    554 	struct l2tp_variant *var;
    555 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    556 	    l2tp_ec.ec_if);
    557 
    558 	var = l2tp_getref_variant(sc, &psref);
    559 	if (var == NULL)
    560 		return;
    561 
    562 	if (var->lv_psrc == NULL || var->lv_pdst == NULL)
    563 		return;
    564 
    565 	l2tpintr(var);
    566 	l2tp_putref_variant(var, &psref);
    567 }
    568 
    569 int
    570 l2tp_transmit(struct ifnet *ifp, struct mbuf *m)
    571 {
    572 	int error;
    573 	struct psref psref;
    574 	struct l2tp_variant *var;
    575 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    576 	    l2tp_ec.ec_if);
    577 
    578 	var = l2tp_getref_variant(sc, &psref);
    579 	if (var == NULL) {
    580 		m_freem(m);
    581 		return ENETDOWN;
    582 	}
    583 
    584 	if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
    585 		m_freem(m);
    586 		error = ENETDOWN;
    587 		goto out;
    588 	}
    589 
    590 	m->m_flags &= ~(M_BCAST|M_MCAST);
    591 	bpf_mtap(ifp, m);
    592 	switch (var->lv_psrc->sa_family) {
    593 #ifdef INET
    594 	case AF_INET:
    595 		error = in_l2tp_output(var, m);
    596 		break;
    597 #endif
    598 #ifdef INET6
    599 	case AF_INET6:
    600 		error = in6_l2tp_output(var, m);
    601 		break;
    602 #endif
    603 	default:
    604 		m_freem(m);
    605 		error = ENETDOWN;
    606 		break;
    607 	}
    608 
    609 	if (error)
    610 		ifp->if_oerrors++;
    611 	else {
    612 		ifp->if_opackets++;
    613 		/*
    614 		 * obytes is incremented at ether_output() or bridge_enqueue().
    615 		 */
    616 	}
    617 
    618 out:
    619 	l2tp_putref_variant(var, &psref);
    620 	return error;
    621 }
    622 
    623 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
    624 int
    625 l2tp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    626 {
    627 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    628 	    l2tp_ec.ec_if);
    629 	struct l2tp_variant *var, *var_tmp;
    630 	struct ifreq     *ifr = data;
    631 	int error = 0, size;
    632 	struct sockaddr *dst, *src;
    633 	struct l2tp_req l2tpr;
    634 	u_long mtu;
    635 	int bound;
    636 	struct psref psref;
    637 
    638 	switch (cmd) {
    639 	case SIOCSIFADDR:
    640 		ifp->if_flags |= IFF_UP;
    641 		break;
    642 
    643 	case SIOCSIFDSTADDR:
    644 		break;
    645 
    646 	case SIOCADDMULTI:
    647 	case SIOCDELMULTI:
    648 		switch (ifr->ifr_addr.sa_family) {
    649 #ifdef INET
    650 		case AF_INET:	/* IP supports Multicast */
    651 			break;
    652 #endif /* INET */
    653 #ifdef INET6
    654 		case AF_INET6:	/* IP6 supports Multicast */
    655 			break;
    656 #endif /* INET6 */
    657 		default:  /* Other protocols doesn't support Multicast */
    658 			error = EAFNOSUPPORT;
    659 			break;
    660 		}
    661 		break;
    662 
    663 	case SIOCSIFMTU:
    664 		mtu = ifr->ifr_mtu;
    665 		if (mtu < L2TP_MTU_MIN || mtu > L2TP_MTU_MAX)
    666 			return (EINVAL);
    667 		ifp->if_mtu = mtu;
    668 		break;
    669 
    670 #ifdef INET
    671 	case SIOCSIFPHYADDR:
    672 		src = (struct sockaddr *)
    673 			&(((struct in_aliasreq *)data)->ifra_addr);
    674 		dst = (struct sockaddr *)
    675 			&(((struct in_aliasreq *)data)->ifra_dstaddr);
    676 		if (src->sa_family != AF_INET || dst->sa_family != AF_INET)
    677 			return EAFNOSUPPORT;
    678 		else if (src->sa_len != sizeof(struct sockaddr_in)
    679 		    || dst->sa_len != sizeof(struct sockaddr_in))
    680 			return EINVAL;
    681 
    682 		error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
    683 		break;
    684 
    685 #endif /* INET */
    686 #ifdef INET6
    687 	case SIOCSIFPHYADDR_IN6:
    688 		src = (struct sockaddr *)
    689 			&(((struct in6_aliasreq *)data)->ifra_addr);
    690 		dst = (struct sockaddr *)
    691 			&(((struct in6_aliasreq *)data)->ifra_dstaddr);
    692 		if (src->sa_family != AF_INET6 || dst->sa_family != AF_INET6)
    693 			return EAFNOSUPPORT;
    694 		else if (src->sa_len != sizeof(struct sockaddr_in6)
    695 		    || dst->sa_len != sizeof(struct sockaddr_in6))
    696 			return EINVAL;
    697 
    698 		error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
    699 		break;
    700 
    701 #endif /* INET6 */
    702 	case SIOCSLIFPHYADDR:
    703 		src = (struct sockaddr *)
    704 			&(((struct if_laddrreq *)data)->addr);
    705 		dst = (struct sockaddr *)
    706 			&(((struct if_laddrreq *)data)->dstaddr);
    707 		if (src->sa_family != dst->sa_family)
    708 			return EINVAL;
    709 		else if (src->sa_family == AF_INET
    710 		    && src->sa_len != sizeof(struct sockaddr_in))
    711 			return EINVAL;
    712 		else if (src->sa_family == AF_INET6
    713 		    && src->sa_len != sizeof(struct sockaddr_in6))
    714 			return EINVAL;
    715 		else if (dst->sa_family == AF_INET
    716 		    && dst->sa_len != sizeof(struct sockaddr_in))
    717 			return EINVAL;
    718 		else if (dst->sa_family == AF_INET6
    719 		    && dst->sa_len != sizeof(struct sockaddr_in6))
    720 			return EINVAL;
    721 
    722 		error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
    723 		break;
    724 
    725 	case SIOCDIFPHYADDR:
    726 		l2tp_delete_tunnel(&sc->l2tp_ec.ec_if);
    727 		break;
    728 
    729 	case SIOCGIFPSRCADDR:
    730 #ifdef INET6
    731 	case SIOCGIFPSRCADDR_IN6:
    732 #endif /* INET6 */
    733 		bound = curlwp_bind();
    734 		var = l2tp_getref_variant(sc, &psref);
    735 		if (var == NULL) {
    736 			curlwp_bindx(bound);
    737 			error = EADDRNOTAVAIL;
    738 			goto bad;
    739 		}
    740 		if (var->lv_psrc == NULL) {
    741 			l2tp_putref_variant(var, &psref);
    742 			curlwp_bindx(bound);
    743 			error = EADDRNOTAVAIL;
    744 			goto bad;
    745 		}
    746 		src = var->lv_psrc;
    747 		switch (cmd) {
    748 #ifdef INET
    749 		case SIOCGIFPSRCADDR:
    750 			dst = &ifr->ifr_addr;
    751 			size = sizeof(ifr->ifr_addr);
    752 			break;
    753 #endif /* INET */
    754 #ifdef INET6
    755 		case SIOCGIFPSRCADDR_IN6:
    756 			dst = (struct sockaddr *)
    757 				&(((struct in6_ifreq *)data)->ifr_addr);
    758 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    759 			break;
    760 #endif /* INET6 */
    761 		default:
    762 			l2tp_putref_variant(var, &psref);
    763 			curlwp_bindx(bound);
    764 			error = EADDRNOTAVAIL;
    765 			goto bad;
    766 		}
    767 		if (src->sa_len > size) {
    768 			l2tp_putref_variant(var, &psref);
    769 			curlwp_bindx(bound);
    770 			return EINVAL;
    771 		}
    772 		sockaddr_copy(dst, src->sa_len, src);
    773 		l2tp_putref_variant(var, &psref);
    774 		curlwp_bindx(bound);
    775 		break;
    776 
    777 	case SIOCGIFPDSTADDR:
    778 #ifdef INET6
    779 	case SIOCGIFPDSTADDR_IN6:
    780 #endif /* INET6 */
    781 		bound = curlwp_bind();
    782 		var = l2tp_getref_variant(sc, &psref);
    783 		if (var == NULL) {
    784 			curlwp_bindx(bound);
    785 			error = EADDRNOTAVAIL;
    786 			goto bad;
    787 		}
    788 		if (var->lv_pdst == NULL) {
    789 			l2tp_putref_variant(var, &psref);
    790 			curlwp_bindx(bound);
    791 			error = EADDRNOTAVAIL;
    792 			goto bad;
    793 		}
    794 		src = var->lv_pdst;
    795 		switch (cmd) {
    796 #ifdef INET
    797 		case SIOCGIFPDSTADDR:
    798 			dst = &ifr->ifr_addr;
    799 			size = sizeof(ifr->ifr_addr);
    800 			break;
    801 #endif /* INET */
    802 #ifdef INET6
    803 		case SIOCGIFPDSTADDR_IN6:
    804 			dst = (struct sockaddr *)
    805 				&(((struct in6_ifreq *)data)->ifr_addr);
    806 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    807 			break;
    808 #endif /* INET6 */
    809 		default:
    810 			l2tp_putref_variant(var, &psref);
    811 			curlwp_bindx(bound);
    812 			error = EADDRNOTAVAIL;
    813 			goto bad;
    814 		}
    815 		if (src->sa_len > size) {
    816 			l2tp_putref_variant(var, &psref);
    817 			curlwp_bindx(bound);
    818 			return EINVAL;
    819 		}
    820 		sockaddr_copy(dst, src->sa_len, src);
    821 		l2tp_putref_variant(var, &psref);
    822 		curlwp_bindx(bound);
    823 		break;
    824 
    825 	case SIOCGLIFPHYADDR:
    826 		bound = curlwp_bind();
    827 		var = l2tp_getref_variant(sc, &psref);
    828 		if (var == NULL) {
    829 			curlwp_bindx(bound);
    830 			error = EADDRNOTAVAIL;
    831 			goto bad;
    832 		}
    833 		if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
    834 			l2tp_putref_variant(var, &psref);
    835 			curlwp_bindx(bound);
    836 			error = EADDRNOTAVAIL;
    837 			goto bad;
    838 		}
    839 
    840 		/* copy src */
    841 		src = var->lv_psrc;
    842 		dst = (struct sockaddr *)
    843 			&(((struct if_laddrreq *)data)->addr);
    844 		size = sizeof(((struct if_laddrreq *)data)->addr);
    845 		if (src->sa_len > size) {
    846 			l2tp_putref_variant(var, &psref);
    847 			curlwp_bindx(bound);
    848 			return EINVAL;
    849                 }
    850 		sockaddr_copy(dst, src->sa_len, src);
    851 
    852 		/* copy dst */
    853 		src = var->lv_pdst;
    854 		dst = (struct sockaddr *)
    855 			&(((struct if_laddrreq *)data)->dstaddr);
    856 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
    857 		if (src->sa_len > size) {
    858 			l2tp_putref_variant(var, &psref);
    859 			curlwp_bindx(bound);
    860 			return EINVAL;
    861                 }
    862 		sockaddr_copy(dst, src->sa_len, src);
    863 		l2tp_putref_variant(var, &psref);
    864 		curlwp_bindx(bound);
    865 		break;
    866 
    867 	case SIOCSL2TPSESSION:
    868 		if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
    869 			break;
    870 
    871 		/* session id must not zero */
    872 		if (l2tpr.my_sess_id == 0 || l2tpr.peer_sess_id == 0)
    873 			return EINVAL;
    874 
    875 		bound = curlwp_bind();
    876 		var_tmp = l2tp_lookup_session_ref(l2tpr.my_sess_id, &psref);
    877 		if (var_tmp != NULL) {
    878 			/* duplicate session id */
    879 			log(LOG_WARNING, "%s: duplicate session id %" PRIu32 " of %s\n",
    880 				sc->l2tp_ec.ec_if.if_xname, l2tpr.my_sess_id,
    881 				var_tmp->lv_softc->l2tp_ec.ec_if.if_xname);
    882 			psref_release(&psref, &var_tmp->lv_psref,
    883 			    lv_psref_class);
    884 			curlwp_bindx(bound);
    885 			return EINVAL;
    886 		}
    887 		curlwp_bindx(bound);
    888 
    889 		error = l2tp_set_session(sc, l2tpr.my_sess_id, l2tpr.peer_sess_id);
    890 		break;
    891 	case SIOCDL2TPSESSION:
    892 		l2tp_clear_session(sc);
    893 		break;
    894 	case SIOCSL2TPCOOKIE:
    895 		if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
    896 			break;
    897 
    898 		error = l2tp_set_cookie(sc, l2tpr.my_cookie, l2tpr.my_cookie_len,
    899 		    l2tpr.peer_cookie, l2tpr.peer_cookie_len);
    900 		break;
    901 	case SIOCDL2TPCOOKIE:
    902 		l2tp_clear_cookie(sc);
    903 		break;
    904 	case SIOCSL2TPSTATE:
    905 		if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
    906 			break;
    907 
    908 		l2tp_set_state(sc, l2tpr.state);
    909 		break;
    910 	case SIOCGL2TP:
    911 		/* get L2TPV3 session info */
    912 		memset(&l2tpr, 0, sizeof(l2tpr));
    913 
    914 		bound = curlwp_bind();
    915 		var = l2tp_getref_variant(sc, &psref);
    916 		if (var == NULL) {
    917 			curlwp_bindx(bound);
    918 			error = EADDRNOTAVAIL;
    919 			goto bad;
    920 		}
    921 
    922 		l2tpr.state = var->lv_state;
    923 		l2tpr.my_sess_id = var->lv_my_sess_id;
    924 		l2tpr.peer_sess_id = var->lv_peer_sess_id;
    925 		l2tpr.my_cookie = var->lv_my_cookie;
    926 		l2tpr.my_cookie_len = var->lv_my_cookie_len;
    927 		l2tpr.peer_cookie = var->lv_peer_cookie;
    928 		l2tpr.peer_cookie_len = var->lv_peer_cookie_len;
    929 		l2tp_putref_variant(var, &psref);
    930 		curlwp_bindx(bound);
    931 
    932 		error = copyout(&l2tpr, ifr->ifr_data, sizeof(l2tpr));
    933 		break;
    934 
    935 	default:
    936 		error =	ifioctl_common(ifp, cmd, data);
    937 		break;
    938 	}
    939  bad:
    940 	return error;
    941 }
    942 
    943 static int
    944 l2tp_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
    945 {
    946 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    947 	    l2tp_ec.ec_if);
    948 	struct sockaddr *osrc, *odst;
    949 	struct sockaddr *nsrc, *ndst;
    950 	struct l2tp_variant *ovar, *nvar;
    951 	int error;
    952 
    953 	nsrc = sockaddr_dup(src, M_WAITOK);
    954 	ndst = sockaddr_dup(dst, M_WAITOK);
    955 
    956 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
    957 
    958 	error = encap_lock_enter();
    959 	if (error)
    960 		goto error;
    961 
    962 	mutex_enter(&sc->l2tp_lock);
    963 
    964 	ovar = sc->l2tp_var;
    965 	osrc = ovar->lv_psrc;
    966 	odst = ovar->lv_pdst;
    967 	*nvar = *ovar;
    968 	psref_target_init(&nvar->lv_psref, lv_psref_class);
    969 	nvar->lv_psrc = nsrc;
    970 	nvar->lv_pdst = ndst;
    971 	error = l2tp_encap_attach(nvar);
    972 	if (error) {
    973 		mutex_exit(&sc->l2tp_lock);
    974 		encap_lock_exit();
    975 		goto error;
    976 	}
    977 	membar_producer();
    978 	l2tp_variant_update(sc, nvar);
    979 
    980 	mutex_exit(&sc->l2tp_lock);
    981 
    982 	(void)l2tp_encap_detach(ovar);
    983 	encap_lock_exit();
    984 
    985 	if (osrc)
    986 		sockaddr_free(osrc);
    987 	if (odst)
    988 		sockaddr_free(odst);
    989 	kmem_free(ovar, sizeof(*ovar));
    990 
    991 	return 0;
    992 
    993 error:
    994 	sockaddr_free(nsrc);
    995 	sockaddr_free(ndst);
    996 	kmem_free(nvar, sizeof(*nvar));
    997 
    998 	return error;
    999 }
   1000 
   1001 static void
   1002 l2tp_delete_tunnel(struct ifnet *ifp)
   1003 {
   1004 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
   1005 	    l2tp_ec.ec_if);
   1006 	struct sockaddr *osrc, *odst;
   1007 	struct l2tp_variant *ovar, *nvar;
   1008 	int error;
   1009 
   1010 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
   1011 
   1012 	error = encap_lock_enter();
   1013 	if (error) {
   1014 		kmem_free(nvar, sizeof(*nvar));
   1015 		return;
   1016 	}
   1017 	mutex_enter(&sc->l2tp_lock);
   1018 
   1019 	ovar = sc->l2tp_var;
   1020 	osrc = ovar->lv_psrc;
   1021 	odst = ovar->lv_pdst;
   1022 	*nvar = *ovar;
   1023 	psref_target_init(&nvar->lv_psref, lv_psref_class);
   1024 	nvar->lv_psrc = NULL;
   1025 	nvar->lv_pdst = NULL;
   1026 	membar_producer();
   1027 	l2tp_variant_update(sc, nvar);
   1028 
   1029 	mutex_exit(&sc->l2tp_lock);
   1030 
   1031 	(void)l2tp_encap_detach(ovar);
   1032 	encap_lock_exit();
   1033 
   1034 	if (osrc)
   1035 		sockaddr_free(osrc);
   1036 	if (odst)
   1037 		sockaddr_free(odst);
   1038 	kmem_free(ovar, sizeof(*ovar));
   1039 }
   1040 
   1041 static int
   1042 id_hash_func(uint32_t id, u_long mask)
   1043 {
   1044 	uint32_t hash;
   1045 
   1046 	hash = (id >> 16) ^ id;
   1047 	hash = (hash >> 4) ^ hash;
   1048 
   1049 	return hash & mask;
   1050 }
   1051 
   1052 static void
   1053 l2tp_hash_init(void)
   1054 {
   1055 
   1056 	l2tp_hash.lists = hashinit(L2TP_ID_HASH_SIZE, HASH_PSLIST, true,
   1057 	    &l2tp_hash.mask);
   1058 }
   1059 
   1060 static int
   1061 l2tp_hash_fini(void)
   1062 {
   1063 	int i;
   1064 
   1065 	mutex_enter(&l2tp_hash.lock);
   1066 
   1067 	for (i = 0; i < l2tp_hash.mask + 1; i++) {
   1068 		if (PSLIST_WRITER_FIRST(&l2tp_hash.lists[i], struct l2tp_softc,
   1069 			l2tp_hash) != NULL) {
   1070 			mutex_exit(&l2tp_hash.lock);
   1071 			return EBUSY;
   1072 		}
   1073 	}
   1074 	for (i = 0; i < l2tp_hash.mask + 1; i++)
   1075 		PSLIST_DESTROY(&l2tp_hash.lists[i]);
   1076 
   1077 	mutex_exit(&l2tp_hash.lock);
   1078 
   1079 	hashdone(l2tp_hash.lists, HASH_PSLIST, l2tp_hash.mask);
   1080 
   1081 	return 0;
   1082 }
   1083 
   1084 static int
   1085 l2tp_set_session(struct l2tp_softc *sc, uint32_t my_sess_id,
   1086     uint32_t peer_sess_id)
   1087 {
   1088 	uint32_t idx;
   1089 	struct l2tp_variant *nvar;
   1090 	struct l2tp_variant *ovar;
   1091 	struct ifnet *ifp = &sc->l2tp_ec.ec_if;
   1092 
   1093 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
   1094 
   1095 	mutex_enter(&sc->l2tp_lock);
   1096 	ovar = sc->l2tp_var;
   1097 	*nvar = *ovar;
   1098 	psref_target_init(&nvar->lv_psref, lv_psref_class);
   1099 	nvar->lv_my_sess_id = my_sess_id;
   1100 	nvar->lv_peer_sess_id = peer_sess_id;
   1101 	membar_producer();
   1102 
   1103 	mutex_enter(&l2tp_hash.lock);
   1104 	if (ovar->lv_my_sess_id > 0 && ovar->lv_peer_sess_id > 0) {
   1105 		PSLIST_WRITER_REMOVE(sc, l2tp_hash);
   1106 		pserialize_perform(l2tp_psz);
   1107 	}
   1108 	mutex_exit(&l2tp_hash.lock);
   1109 	PSLIST_ENTRY_DESTROY(sc, l2tp_hash);
   1110 
   1111 	l2tp_variant_update(sc, nvar);
   1112 	mutex_exit(&sc->l2tp_lock);
   1113 
   1114 	idx = id_hash_func(nvar->lv_my_sess_id, l2tp_hash.mask);
   1115 	if ((ifp->if_flags & IFF_DEBUG) != 0)
   1116 		log(LOG_DEBUG, "%s: add hash entry: sess_id=%" PRIu32 ", idx=%" PRIu32 "\n",
   1117 		    sc->l2tp_ec.ec_if.if_xname, nvar->lv_my_sess_id, idx);
   1118 
   1119 	PSLIST_ENTRY_INIT(sc, l2tp_hash);
   1120 	mutex_enter(&l2tp_hash.lock);
   1121 	PSLIST_WRITER_INSERT_HEAD(&l2tp_hash.lists[idx], sc, l2tp_hash);
   1122 	mutex_exit(&l2tp_hash.lock);
   1123 
   1124 	kmem_free(ovar, sizeof(*ovar));
   1125 	return 0;
   1126 }
   1127 
   1128 static int
   1129 l2tp_clear_session(struct l2tp_softc *sc)
   1130 {
   1131 	struct l2tp_variant *nvar;
   1132 	struct l2tp_variant *ovar;
   1133 
   1134 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
   1135 
   1136 	mutex_enter(&sc->l2tp_lock);
   1137 	ovar = sc->l2tp_var;
   1138 	*nvar = *ovar;
   1139 	psref_target_init(&nvar->lv_psref, lv_psref_class);
   1140 	nvar->lv_my_sess_id = 0;
   1141 	nvar->lv_peer_sess_id = 0;
   1142 	membar_producer();
   1143 
   1144 	mutex_enter(&l2tp_hash.lock);
   1145 	if (ovar->lv_my_sess_id > 0 && ovar->lv_peer_sess_id > 0) {
   1146 		PSLIST_WRITER_REMOVE(sc, l2tp_hash);
   1147 		pserialize_perform(l2tp_psz);
   1148 	}
   1149 	mutex_exit(&l2tp_hash.lock);
   1150 
   1151 	l2tp_variant_update(sc, nvar);
   1152 	mutex_exit(&sc->l2tp_lock);
   1153 	kmem_free(ovar, sizeof(*ovar));
   1154 	return 0;
   1155 }
   1156 
   1157 struct l2tp_variant *
   1158 l2tp_lookup_session_ref(uint32_t id, struct psref *psref)
   1159 {
   1160 	int idx;
   1161 	int s;
   1162 	struct l2tp_softc *sc;
   1163 
   1164 	idx = id_hash_func(id, l2tp_hash.mask);
   1165 
   1166 	s = pserialize_read_enter();
   1167 	PSLIST_READER_FOREACH(sc, &l2tp_hash.lists[idx], struct l2tp_softc,
   1168 	    l2tp_hash) {
   1169 		struct l2tp_variant *var = sc->l2tp_var;
   1170 		if (var == NULL)
   1171 			continue;
   1172 		if (var->lv_my_sess_id != id)
   1173 			continue;
   1174 		psref_acquire(psref, &var->lv_psref, lv_psref_class);
   1175 		pserialize_read_exit(s);
   1176 		return var;
   1177 	}
   1178 	pserialize_read_exit(s);
   1179 	return NULL;
   1180 }
   1181 
   1182 /*
   1183  * l2tp_variant update API.
   1184  *
   1185  * Assumption:
   1186  * reader side dereferences sc->l2tp_var in reader critical section only,
   1187  * that is, all of reader sides do not reader the sc->l2tp_var after
   1188  * pserialize_perform().
   1189  */
   1190 static void
   1191 l2tp_variant_update(struct l2tp_softc *sc, struct l2tp_variant *nvar)
   1192 {
   1193 	struct ifnet *ifp = &sc->l2tp_ec.ec_if;
   1194 	struct l2tp_variant *ovar = sc->l2tp_var;
   1195 
   1196 	KASSERT(mutex_owned(&sc->l2tp_lock));
   1197 
   1198 	sc->l2tp_var = nvar;
   1199 	pserialize_perform(l2tp_psz);
   1200 	psref_target_destroy(&ovar->lv_psref, lv_psref_class);
   1201 
   1202 	/*
   1203 	 * In the manual of atomic_swap_ptr(3), there is no mention if 2nd
   1204 	 * argument is rewrite or not. So, use sc->l2tp_var instead of nvar.
   1205 	 */
   1206 	if (sc->l2tp_var != NULL) {
   1207 		if (sc->l2tp_var->lv_psrc != NULL
   1208 		    && sc->l2tp_var->lv_pdst != NULL)
   1209 			ifp->if_flags |= IFF_RUNNING;
   1210 		else
   1211 			ifp->if_flags &= ~IFF_RUNNING;
   1212 	}
   1213 }
   1214 
   1215 static int
   1216 l2tp_set_cookie(struct l2tp_softc *sc, uint64_t my_cookie, u_int my_cookie_len,
   1217     uint64_t peer_cookie, u_int peer_cookie_len)
   1218 {
   1219 	struct l2tp_variant *nvar;
   1220 
   1221 	if (my_cookie == 0 || peer_cookie == 0)
   1222 		return EINVAL;
   1223 
   1224 	if (my_cookie_len != 4 && my_cookie_len != 8
   1225 	    && peer_cookie_len != 4 && peer_cookie_len != 8)
   1226 		return EINVAL;
   1227 
   1228 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
   1229 
   1230 	mutex_enter(&sc->l2tp_lock);
   1231 
   1232 	*nvar = *sc->l2tp_var;
   1233 	psref_target_init(&nvar->lv_psref, lv_psref_class);
   1234 	nvar->lv_my_cookie = my_cookie;
   1235 	nvar->lv_my_cookie_len = my_cookie_len;
   1236 	nvar->lv_peer_cookie = peer_cookie;
   1237 	nvar->lv_peer_cookie_len = peer_cookie_len;
   1238 	nvar->lv_use_cookie = L2TP_COOKIE_ON;
   1239 	membar_producer();
   1240 	l2tp_variant_update(sc, nvar);
   1241 
   1242 	mutex_exit(&sc->l2tp_lock);
   1243 
   1244 	struct ifnet *ifp = &sc->l2tp_ec.ec_if;
   1245 	if ((ifp->if_flags & IFF_DEBUG) != 0) {
   1246 		log(LOG_DEBUG,
   1247 		    "%s: set cookie: "
   1248 		    "local cookie_len=%u local cookie=%" PRIu64 ", "
   1249 		    "remote cookie_len=%u remote cookie=%" PRIu64 "\n",
   1250 		    ifp->if_xname, my_cookie_len, my_cookie,
   1251 		    peer_cookie_len, peer_cookie);
   1252 	}
   1253 
   1254 	return 0;
   1255 }
   1256 
   1257 static void
   1258 l2tp_clear_cookie(struct l2tp_softc *sc)
   1259 {
   1260 	struct l2tp_variant *nvar;
   1261 
   1262 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
   1263 
   1264 	mutex_enter(&sc->l2tp_lock);
   1265 
   1266 	*nvar = *sc->l2tp_var;
   1267 	psref_target_init(&nvar->lv_psref, lv_psref_class);
   1268 	nvar->lv_my_cookie = 0;
   1269 	nvar->lv_my_cookie_len = 0;
   1270 	nvar->lv_peer_cookie = 0;
   1271 	nvar->lv_peer_cookie_len = 0;
   1272 	nvar->lv_use_cookie = L2TP_COOKIE_OFF;
   1273 	membar_producer();
   1274 	l2tp_variant_update(sc, nvar);
   1275 
   1276 	mutex_exit(&sc->l2tp_lock);
   1277 }
   1278 
   1279 static void
   1280 l2tp_set_state(struct l2tp_softc *sc, int state)
   1281 {
   1282 	struct ifnet *ifp = &sc->l2tp_ec.ec_if;
   1283 	struct l2tp_variant *nvar;
   1284 
   1285 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
   1286 
   1287 	mutex_enter(&sc->l2tp_lock);
   1288 
   1289 	*nvar = *sc->l2tp_var;
   1290 	psref_target_init(&nvar->lv_psref, lv_psref_class);
   1291 	nvar->lv_state = state;
   1292 	membar_producer();
   1293 	l2tp_variant_update(sc, nvar);
   1294 
   1295 	if (nvar->lv_state == L2TP_STATE_UP) {
   1296 		ifp->if_link_state = LINK_STATE_UP;
   1297 	} else {
   1298 		ifp->if_link_state = LINK_STATE_DOWN;
   1299 	}
   1300 
   1301 	mutex_exit(&sc->l2tp_lock);
   1302 
   1303 #ifdef NOTYET
   1304 	vlan_linkstate_notify(ifp, ifp->if_link_state);
   1305 #endif
   1306 }
   1307 
   1308 static int
   1309 l2tp_encap_attach(struct l2tp_variant *var)
   1310 {
   1311 	int error;
   1312 
   1313 	if (var == NULL || var->lv_psrc == NULL)
   1314 		return EINVAL;
   1315 
   1316 	switch (var->lv_psrc->sa_family) {
   1317 #ifdef INET
   1318 	case AF_INET:
   1319 		error = in_l2tp_attach(var);
   1320 		break;
   1321 #endif
   1322 #ifdef INET6
   1323 	case AF_INET6:
   1324 		error = in6_l2tp_attach(var);
   1325 		break;
   1326 #endif
   1327 	default:
   1328 		error = EINVAL;
   1329 		break;
   1330 	}
   1331 
   1332 	return error;
   1333 }
   1334 
   1335 static int
   1336 l2tp_encap_detach(struct l2tp_variant *var)
   1337 {
   1338 	int error;
   1339 
   1340 	if (var == NULL || var->lv_psrc == NULL)
   1341 		return EINVAL;
   1342 
   1343 	switch (var->lv_psrc->sa_family) {
   1344 #ifdef INET
   1345 	case AF_INET:
   1346 		error = in_l2tp_detach(var);
   1347 		break;
   1348 #endif
   1349 #ifdef INET6
   1350 	case AF_INET6:
   1351 		error = in6_l2tp_detach(var);
   1352 		break;
   1353 #endif
   1354 	default:
   1355 		error = EINVAL;
   1356 		break;
   1357 	}
   1358 
   1359 	return error;
   1360 }
   1361 
   1362 int
   1363 l2tp_check_nesting(struct ifnet *ifp, struct mbuf *m)
   1364 {
   1365 
   1366 	return if_tunnel_check_nesting(ifp, m, max_l2tp_nesting);
   1367 }
   1368 
   1369 /*
   1370  * Module infrastructure
   1371  */
   1372 #include "if_module.h"
   1373 
   1374 IF_MODULE(MODULE_CLASS_DRIVER, l2tp, "")
   1375 
   1376 
   1377 /* TODO: IP_TCPMSS support */
   1378 #ifdef IP_TCPMSS
   1379 static int l2tp_need_tcpmss_clamp(struct ifnet *);
   1380 #ifdef INET
   1381 static struct mbuf *l2tp_tcpmss4_clamp(struct ifnet *, struct mbuf *);
   1382 #endif
   1383 #ifdef INET6
   1384 static struct mbuf *l2tp_tcpmss6_clamp(struct ifnet *, struct mbuf *);
   1385 #endif
   1386 
   1387 struct mbuf *
   1388 l2tp_tcpmss_clamp(struct ifnet *ifp, struct mbuf *m)
   1389 {
   1390 	struct ether_header *eh;
   1391 	struct ether_vlan_header evh;
   1392 
   1393 	if (!l2tp_need_tcpmss_clamp(ifp)) {
   1394 		return m;
   1395 	}
   1396 
   1397 	if (m->m_pkthdr.len < sizeof(evh)) {
   1398 		m_freem(m);
   1399 		return NULL;
   1400 	}
   1401 
   1402 	/* save ether header */
   1403 	m_copydata(m, 0, sizeof(evh), (void *)&evh);
   1404 	eh = (struct ether_header *)&evh;
   1405 
   1406 	switch (ntohs(eh->ether_type)) {
   1407 	case ETHERTYPE_VLAN: /* Ether + VLAN */
   1408 		if (m->m_pkthdr.len <= sizeof(struct ether_vlan_header))
   1409 			break;
   1410 		m_adj(m, sizeof(struct ether_vlan_header));
   1411 		switch (ntohs(evh.evl_proto)) {
   1412 #ifdef INET
   1413 		case ETHERTYPE_IP: /* Ether + VLAN + IPv4 */
   1414 			m = l2tp_tcpmss4_clamp(ifp, m);
   1415 			if (m == NULL)
   1416 				return NULL;
   1417 			break;
   1418 #endif /* INET */
   1419 #ifdef INET6
   1420 		case ETHERTYPE_IPV6: /* Ether + VLAN + IPv6 */
   1421 			m = l2tp_tcpmss6_clamp(ifp, m);
   1422 			if (m == NULL)
   1423 				return NULL;
   1424 			break;
   1425 #endif /* INET6 */
   1426 		default:
   1427 			break;
   1428 		}
   1429 
   1430 		/* restore ether header */
   1431 		M_PREPEND(m, sizeof(struct ether_vlan_header),
   1432 		    M_DONTWAIT);
   1433 		if (m == NULL)
   1434 			return NULL;
   1435 		*mtod(m, struct ether_vlan_header *) = evh;
   1436 		break;
   1437 
   1438 #ifdef INET
   1439 	case ETHERTYPE_IP: /* Ether + IPv4 */
   1440 		if (m->m_pkthdr.len <= sizeof(struct ether_header))
   1441 			break;
   1442 		m_adj(m, sizeof(struct ether_header));
   1443 		m = l2tp_tcpmss4_clamp(ifp, m);
   1444 		if (m == NULL)
   1445 			return NULL;
   1446 		/* restore ether header */
   1447 		M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
   1448 		if (m == NULL)
   1449 			return NULL;
   1450 		*mtod(m, struct ether_header *) = *eh;
   1451 		break;
   1452 #endif /* INET */
   1453 
   1454 #ifdef INET6
   1455 	case ETHERTYPE_IPV6: /* Ether + IPv6 */
   1456 		if (m->m_pkthdr.len <= sizeof(struct ether_header))
   1457 			break;
   1458 		m_adj(m, sizeof(struct ether_header));
   1459 		m = l2tp_tcpmss6_clamp(ifp, m);
   1460 		if (m == NULL)
   1461 			return NULL;
   1462 		/* restore ether header */
   1463 		M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
   1464 		if (m == NULL)
   1465 			return NULL;
   1466 		*mtod(m, struct ether_header *) = *eh;
   1467 		break;
   1468 #endif /* INET6 */
   1469 
   1470 	default:
   1471 		break;
   1472 	}
   1473 
   1474 	return m;
   1475 }
   1476 
   1477 static int
   1478 l2tp_need_tcpmss_clamp(struct ifnet *ifp)
   1479 {
   1480 	int ret = 0;
   1481 
   1482 #ifdef INET
   1483 	if (ifp->if_tcpmss != 0)
   1484 		ret = 1;
   1485 #endif
   1486 
   1487 #ifdef INET6
   1488 	if (ifp->if_tcpmss6 != 0)
   1489 		ret = 1;
   1490 #endif
   1491 
   1492 	return ret;
   1493 }
   1494 
   1495 #ifdef INET
   1496 static struct mbuf *
   1497 l2tp_tcpmss4_clamp(struct ifnet *ifp, struct mbuf *m)
   1498 {
   1499 
   1500 	if (ifp->if_tcpmss != 0) {
   1501 		return ip_tcpmss(m, (ifp->if_tcpmss < 0) ?
   1502 			ifp->if_mtu - IP_TCPMSS_EXTLEN :
   1503 			ifp->if_tcpmss);
   1504 	}
   1505 	return m;
   1506 }
   1507 #endif /* INET */
   1508 
   1509 #ifdef INET6
   1510 static struct mbuf *
   1511 l2tp_tcpmss6_clamp(struct ifnet *ifp, struct mbuf *m)
   1512 {
   1513 	int ip6hdrlen;
   1514 
   1515 	if (ifp->if_tcpmss6 != 0 &&
   1516 	    ip6_tcpmss_applicable(m, &ip6hdrlen)) {
   1517 		return ip6_tcpmss(m, ip6hdrlen,
   1518 			(ifp->if_tcpmss6 < 0) ?
   1519 			ifp->if_mtu - IP6_TCPMSS_EXTLEN :
   1520 			ifp->if_tcpmss6);
   1521 	}
   1522 	return m;
   1523 }
   1524 #endif /* INET6 */
   1525 
   1526 #endif /* IP_TCPMSS */
   1527