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