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