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