Home | History | Annotate | Line # | Download | only in net
if_l2tp.c revision 1.10
      1 /*	$NetBSD: if_l2tp.c,v 1.10 2017/04/20 09:11:58 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.10 2017/04/20 09:11:58 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 	KASSERTMSG(sc->l2tp_ro_percpu != NULL,
    249 	    "failed to allocate sc->l2tp_ro_percpu");
    250 	percpu_foreach(sc->l2tp_ro_percpu, l2tp_ro_init_pc, NULL);
    251 
    252 	mutex_enter(&l2tp_softcs.lock);
    253 	LIST_INSERT_HEAD(&l2tp_softcs.list, sc, l2tp_list);
    254 	mutex_exit(&l2tp_softcs.lock);
    255 
    256 	return (0);
    257 }
    258 
    259 void
    260 l2tpattach0(struct l2tp_softc *sc)
    261 {
    262 
    263 	sc->l2tp_ec.ec_if.if_addrlen = 0;
    264 	sc->l2tp_ec.ec_if.if_mtu    = L2TP_MTU;
    265 	sc->l2tp_ec.ec_if.if_flags  = IFF_POINTOPOINT|IFF_MULTICAST|IFF_SIMPLEX;
    266 	sc->l2tp_ec.ec_if.if_extflags  = IFEF_OUTPUT_MPSAFE|IFEF_START_MPSAFE;
    267 	sc->l2tp_ec.ec_if.if_ioctl  = l2tp_ioctl;
    268 	sc->l2tp_ec.ec_if.if_output = l2tp_output;
    269 	sc->l2tp_ec.ec_if.if_type   = IFT_L2TP;
    270 	sc->l2tp_ec.ec_if.if_dlt    = DLT_NULL;
    271 	sc->l2tp_ec.ec_if.if_start  = l2tp_start;
    272 	sc->l2tp_ec.ec_if.if_transmit = l2tp_transmit;
    273 	sc->l2tp_ec.ec_if._if_input = ether_input;
    274 	IFQ_SET_READY(&sc->l2tp_ec.ec_if.if_snd);
    275 	if_attach(&sc->l2tp_ec.ec_if);
    276 	if_alloc_sadl(&sc->l2tp_ec.ec_if);
    277 	bpf_attach(&sc->l2tp_ec.ec_if, DLT_EN10MB, sizeof(struct ether_header));
    278 }
    279 
    280 void
    281 l2tp_ro_init_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
    282 {
    283 	struct l2tp_ro *lro = p;
    284 
    285 	mutex_init(&lro->lr_lock, MUTEX_DEFAULT, IPL_NONE);
    286 }
    287 
    288 void
    289 l2tp_ro_fini_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
    290 {
    291 	struct l2tp_ro *lro = p;
    292 
    293 	rtcache_free(&lro->lr_ro);
    294 
    295 	mutex_destroy(&lro->lr_lock);
    296 }
    297 
    298 static int
    299 l2tp_clone_destroy(struct ifnet *ifp)
    300 {
    301 	struct l2tp_variant *var;
    302 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    303 	    l2tp_ec.ec_if);
    304 
    305 	l2tp_clear_session(sc);
    306 	l2tp_delete_tunnel(&sc->l2tp_ec.ec_if);
    307 	/*
    308 	 * To avoid for l2tp_transmit() to access sc->l2tp_var after free it.
    309 	 */
    310 	mutex_enter(&sc->l2tp_lock);
    311 	var = sc->l2tp_var;
    312 	l2tp_variant_update(sc, NULL);
    313 	mutex_exit(&sc->l2tp_lock);
    314 
    315 	mutex_enter(&l2tp_softcs.lock);
    316 	LIST_REMOVE(sc, l2tp_list);
    317 	mutex_exit(&l2tp_softcs.lock);
    318 
    319 	bpf_detach(ifp);
    320 
    321 	if_detach(ifp);
    322 
    323 	percpu_foreach(sc->l2tp_ro_percpu, l2tp_ro_fini_pc, NULL);
    324 	percpu_free(sc->l2tp_ro_percpu, sizeof(struct l2tp_ro));
    325 
    326 	kmem_free(var, sizeof(struct l2tp_variant));
    327 	mutex_destroy(&sc->l2tp_lock);
    328 	kmem_free(sc, sizeof(struct l2tp_softc));
    329 
    330 	return 0;
    331 }
    332 
    333 static int
    334 l2tp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    335     const struct rtentry *rt)
    336 {
    337 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    338 	    l2tp_ec.ec_if);
    339 	struct l2tp_variant *var;
    340 	struct psref psref;
    341 	int error = 0;
    342 
    343 	var = l2tp_getref_variant(sc, &psref);
    344 	if (var == NULL) {
    345 		m_freem(m);
    346 		return ENETDOWN;
    347 	}
    348 
    349 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
    350 
    351 	m->m_flags &= ~(M_BCAST|M_MCAST);
    352 
    353 	if ((ifp->if_flags & IFF_UP) == 0) {
    354 		m_freem(m);
    355 		error = ENETDOWN;
    356 		goto end;
    357 	}
    358 
    359 	if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
    360 		m_freem(m);
    361 		error = ENETDOWN;
    362 		goto end;
    363 	}
    364 
    365 	/* XXX should we check if our outer source is legal? */
    366 
    367 	/* use DLT_NULL encapsulation here to pass inner af type */
    368 	M_PREPEND(m, sizeof(int), M_DONTWAIT);
    369 	if (!m) {
    370 		error = ENOBUFS;
    371 		goto end;
    372 	}
    373 	*mtod(m, int *) = dst->sa_family;
    374 
    375 	IFQ_ENQUEUE(&ifp->if_snd, m, error);
    376 	if (error)
    377 		goto end;
    378 
    379 	/*
    380 	 * direct call to avoid infinite loop at l2tpintr()
    381 	 */
    382 	l2tpintr(var);
    383 
    384 	error = 0;
    385 
    386 end:
    387 	l2tp_putref_variant(var, &psref);
    388 	if (error)
    389 		ifp->if_oerrors++;
    390 
    391 	return error;
    392 }
    393 
    394 static void
    395 l2tpintr(struct l2tp_variant *var)
    396 {
    397 	struct l2tp_softc *sc;
    398 	struct ifnet *ifp;
    399 	struct mbuf *m;
    400 	int error;
    401 
    402 	KASSERT(psref_held(&var->lv_psref, lv_psref_class));
    403 
    404 	sc = var->lv_softc;
    405 	ifp = &sc->l2tp_ec.ec_if;
    406 
    407 	/* output processing */
    408 	if (var->lv_my_sess_id == 0 || var->lv_peer_sess_id == 0) {
    409 		IFQ_PURGE(&ifp->if_snd);
    410 		return;
    411 	}
    412 
    413 	for (;;) {
    414 		IFQ_DEQUEUE(&ifp->if_snd, m);
    415 		if (m == NULL)
    416 			break;
    417 		m->m_flags &= ~(M_BCAST|M_MCAST);
    418 		bpf_mtap(ifp, m);
    419 		switch (var->lv_psrc->sa_family) {
    420 #ifdef INET
    421 		case AF_INET:
    422 			error = in_l2tp_output(var, m);
    423 			break;
    424 #endif
    425 #ifdef INET6
    426 		case AF_INET6:
    427 			error = in6_l2tp_output(var, m);
    428 			break;
    429 #endif
    430 		default:
    431 			m_freem(m);
    432 			error = ENETDOWN;
    433 			break;
    434 		}
    435 
    436 		if (error)
    437 			ifp->if_oerrors++;
    438 		else {
    439 			ifp->if_opackets++;
    440 			/*
    441 			 * obytes is incremented at ether_output() or
    442 			 * bridge_enqueue().
    443 			 */
    444 		}
    445 	}
    446 
    447 }
    448 
    449 void
    450 l2tp_input(struct mbuf *m, struct ifnet *ifp)
    451 {
    452 
    453 	KASSERT(ifp != NULL);
    454 
    455 	if (0 == (mtod(m, u_long) & 0x03)) {
    456 		/* copy and align head of payload */
    457 		struct mbuf *m_head;
    458 		int copy_length;
    459 
    460 #define L2TP_COPY_LENGTH		60
    461 #define L2TP_LINK_HDR_ROOM	(MHLEN - L2TP_COPY_LENGTH - 4/*round4(2)*/)
    462 
    463 		if (m->m_pkthdr.len < L2TP_COPY_LENGTH) {
    464 			copy_length = m->m_pkthdr.len;
    465 		} else {
    466 			copy_length = L2TP_COPY_LENGTH;
    467 		}
    468 
    469 		if (m->m_len < copy_length) {
    470 			m = m_pullup(m, copy_length);
    471 			if (m == NULL)
    472 				return;
    473 		}
    474 
    475 		MGETHDR(m_head, M_DONTWAIT, MT_HEADER);
    476 		if (m_head == NULL) {
    477 			m_freem(m);
    478 			return;
    479 		}
    480 		M_COPY_PKTHDR(m_head, m);
    481 
    482 		m_head->m_data += 2 /* align */ + L2TP_LINK_HDR_ROOM;
    483 		memcpy(m_head->m_data, m->m_data, copy_length);
    484 		m_head->m_len = copy_length;
    485 		m->m_data += copy_length;
    486 		m->m_len -= copy_length;
    487 
    488 		/* construct chain */
    489 		if (m->m_len == 0) {
    490 			m_head->m_next = m_free(m); /* not m_freem */
    491 		} else {
    492 			/*
    493 			 * copyed mtag in previous call M_COPY_PKTHDR
    494 			 * but don't delete mtag in case cutt of M_PKTHDR flag
    495 			 */
    496 			m_tag_delete_chain(m, NULL);
    497 			m->m_flags &= ~M_PKTHDR;
    498 			m_head->m_next = m;
    499 		}
    500 
    501 		/* override m */
    502 		m = m_head;
    503 	}
    504 
    505 	m_set_rcvif(m, ifp);
    506 
    507 	/*
    508 	 * bpf_mtap() and ifp->if_ipackets++ is done in if_input()
    509 	 *
    510 	 * obytes is incremented at ether_output() or bridge_enqueue().
    511 	 */
    512 	if_percpuq_enqueue(ifp->if_percpuq, m);
    513 }
    514 
    515 void
    516 l2tp_start(struct ifnet *ifp)
    517 {
    518 	struct psref psref;
    519 	struct l2tp_variant *var;
    520 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    521 	    l2tp_ec.ec_if);
    522 
    523 	var = l2tp_getref_variant(sc, &psref);
    524 	if (var == NULL)
    525 		return;
    526 
    527 	if (var->lv_psrc == NULL || var->lv_pdst == NULL)
    528 		return;
    529 
    530 	l2tpintr(var);
    531 	l2tp_putref_variant(var, &psref);
    532 }
    533 
    534 int
    535 l2tp_transmit(struct ifnet *ifp, struct mbuf *m)
    536 {
    537 	int error;
    538 	struct psref psref;
    539 	struct l2tp_variant *var;
    540 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    541 	    l2tp_ec.ec_if);
    542 
    543 	var = l2tp_getref_variant(sc, &psref);
    544 	if (var == NULL) {
    545 		m_freem(m);
    546 		return ENETDOWN;
    547 	}
    548 
    549 	if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
    550 		m_freem(m);
    551 		error = ENETDOWN;
    552 		goto out;
    553 	}
    554 
    555 	m->m_flags &= ~(M_BCAST|M_MCAST);
    556 	bpf_mtap(ifp, m);
    557 	switch (var->lv_psrc->sa_family) {
    558 #ifdef INET
    559 	case AF_INET:
    560 		error = in_l2tp_output(var, m);
    561 		break;
    562 #endif
    563 #ifdef INET6
    564 	case AF_INET6:
    565 		error = in6_l2tp_output(var, m);
    566 		break;
    567 #endif
    568 	default:
    569 		m_freem(m);
    570 		error = ENETDOWN;
    571 		break;
    572 	}
    573 
    574 	if (error)
    575 		ifp->if_oerrors++;
    576 	else {
    577 		ifp->if_opackets++;
    578 		/*
    579 		 * obytes is incremented at ether_output() or bridge_enqueue().
    580 		 */
    581 	}
    582 
    583 out:
    584 	l2tp_putref_variant(var, &psref);
    585 	return error;
    586 }
    587 
    588 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
    589 int
    590 l2tp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    591 {
    592 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    593 	    l2tp_ec.ec_if);
    594 	struct l2tp_variant *var, *var_tmp;
    595 	struct ifreq     *ifr = data;
    596 	int error = 0, size;
    597 	struct sockaddr *dst, *src;
    598 	struct l2tp_req l2tpr;
    599 	u_long mtu;
    600 	int bound;
    601 	struct psref psref;
    602 
    603 	switch (cmd) {
    604 	case SIOCSIFADDR:
    605 		ifp->if_flags |= IFF_UP;
    606 		break;
    607 
    608 	case SIOCSIFDSTADDR:
    609 		break;
    610 
    611 	case SIOCADDMULTI:
    612 	case SIOCDELMULTI:
    613 		switch (ifr->ifr_addr.sa_family) {
    614 #ifdef INET
    615 		case AF_INET:	/* IP supports Multicast */
    616 			break;
    617 #endif /* INET */
    618 #ifdef INET6
    619 		case AF_INET6:	/* IP6 supports Multicast */
    620 			break;
    621 #endif /* INET6 */
    622 		default:  /* Other protocols doesn't support Multicast */
    623 			error = EAFNOSUPPORT;
    624 			break;
    625 		}
    626 		break;
    627 
    628 	case SIOCSIFMTU:
    629 		mtu = ifr->ifr_mtu;
    630 		if (mtu < L2TP_MTU_MIN || mtu > L2TP_MTU_MAX)
    631 			return (EINVAL);
    632 		ifp->if_mtu = mtu;
    633 		break;
    634 
    635 #ifdef INET
    636 	case SIOCSIFPHYADDR:
    637 		src = (struct sockaddr *)
    638 			&(((struct in_aliasreq *)data)->ifra_addr);
    639 		dst = (struct sockaddr *)
    640 			&(((struct in_aliasreq *)data)->ifra_dstaddr);
    641 		if (src->sa_family != AF_INET || dst->sa_family != AF_INET)
    642 			return EAFNOSUPPORT;
    643 		else if (src->sa_len != sizeof(struct sockaddr_in)
    644 		    || dst->sa_len != sizeof(struct sockaddr_in))
    645 			return EINVAL;
    646 
    647 		error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
    648 		break;
    649 
    650 #endif /* INET */
    651 #ifdef INET6
    652 	case SIOCSIFPHYADDR_IN6:
    653 		src = (struct sockaddr *)
    654 			&(((struct in6_aliasreq *)data)->ifra_addr);
    655 		dst = (struct sockaddr *)
    656 			&(((struct in6_aliasreq *)data)->ifra_dstaddr);
    657 		if (src->sa_family != AF_INET6 || dst->sa_family != AF_INET6)
    658 			return EAFNOSUPPORT;
    659 		else if (src->sa_len != sizeof(struct sockaddr_in6)
    660 		    || dst->sa_len != sizeof(struct sockaddr_in6))
    661 			return EINVAL;
    662 
    663 		error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
    664 		break;
    665 
    666 #endif /* INET6 */
    667 	case SIOCSLIFPHYADDR:
    668 		src = (struct sockaddr *)
    669 			&(((struct if_laddrreq *)data)->addr);
    670 		dst = (struct sockaddr *)
    671 			&(((struct if_laddrreq *)data)->dstaddr);
    672 		if (src->sa_family != dst->sa_family)
    673 			return EINVAL;
    674 		else if (src->sa_family == AF_INET
    675 		    && src->sa_len != sizeof(struct sockaddr_in))
    676 			return EINVAL;
    677 		else if (src->sa_family == AF_INET6
    678 		    && src->sa_len != sizeof(struct sockaddr_in6))
    679 			return EINVAL;
    680 		else if (dst->sa_family == AF_INET
    681 		    && dst->sa_len != sizeof(struct sockaddr_in))
    682 			return EINVAL;
    683 		else if (dst->sa_family == AF_INET6
    684 		    && dst->sa_len != sizeof(struct sockaddr_in6))
    685 			return EINVAL;
    686 
    687 		error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
    688 		break;
    689 
    690 	case SIOCDIFPHYADDR:
    691 		l2tp_delete_tunnel(&sc->l2tp_ec.ec_if);
    692 		break;
    693 
    694 	case SIOCGIFPSRCADDR:
    695 #ifdef INET6
    696 	case SIOCGIFPSRCADDR_IN6:
    697 #endif /* INET6 */
    698 		bound = curlwp_bind();
    699 		var = l2tp_getref_variant(sc, &psref);
    700 		if (var == NULL) {
    701 			curlwp_bindx(bound);
    702 			error = EADDRNOTAVAIL;
    703 			goto bad;
    704 		}
    705 		if (var->lv_psrc == NULL) {
    706 			l2tp_putref_variant(var, &psref);
    707 			curlwp_bindx(bound);
    708 			error = EADDRNOTAVAIL;
    709 			goto bad;
    710 		}
    711 		src = var->lv_psrc;
    712 		switch (cmd) {
    713 #ifdef INET
    714 		case SIOCGIFPSRCADDR:
    715 			dst = &ifr->ifr_addr;
    716 			size = sizeof(ifr->ifr_addr);
    717 			break;
    718 #endif /* INET */
    719 #ifdef INET6
    720 		case SIOCGIFPSRCADDR_IN6:
    721 			dst = (struct sockaddr *)
    722 				&(((struct in6_ifreq *)data)->ifr_addr);
    723 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    724 			break;
    725 #endif /* INET6 */
    726 		default:
    727 			l2tp_putref_variant(var, &psref);
    728 			curlwp_bindx(bound);
    729 			error = EADDRNOTAVAIL;
    730 			goto bad;
    731 		}
    732 		if (src->sa_len > size) {
    733 			l2tp_putref_variant(var, &psref);
    734 			curlwp_bindx(bound);
    735 			return EINVAL;
    736 		}
    737 		sockaddr_copy(dst, src->sa_len, src);
    738 		l2tp_putref_variant(var, &psref);
    739 		curlwp_bindx(bound);
    740 		break;
    741 
    742 	case SIOCGIFPDSTADDR:
    743 #ifdef INET6
    744 	case SIOCGIFPDSTADDR_IN6:
    745 #endif /* INET6 */
    746 		bound = curlwp_bind();
    747 		var = l2tp_getref_variant(sc, &psref);
    748 		if (var == NULL) {
    749 			curlwp_bindx(bound);
    750 			error = EADDRNOTAVAIL;
    751 			goto bad;
    752 		}
    753 		if (var->lv_pdst == NULL) {
    754 			l2tp_putref_variant(var, &psref);
    755 			curlwp_bindx(bound);
    756 			error = EADDRNOTAVAIL;
    757 			goto bad;
    758 		}
    759 		src = var->lv_pdst;
    760 		switch (cmd) {
    761 #ifdef INET
    762 		case SIOCGIFPDSTADDR:
    763 			dst = &ifr->ifr_addr;
    764 			size = sizeof(ifr->ifr_addr);
    765 			break;
    766 #endif /* INET */
    767 #ifdef INET6
    768 		case SIOCGIFPDSTADDR_IN6:
    769 			dst = (struct sockaddr *)
    770 				&(((struct in6_ifreq *)data)->ifr_addr);
    771 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    772 			break;
    773 #endif /* INET6 */
    774 		default:
    775 			l2tp_putref_variant(var, &psref);
    776 			curlwp_bindx(bound);
    777 			error = EADDRNOTAVAIL;
    778 			goto bad;
    779 		}
    780 		if (src->sa_len > size) {
    781 			l2tp_putref_variant(var, &psref);
    782 			curlwp_bindx(bound);
    783 			return EINVAL;
    784 		}
    785 		sockaddr_copy(dst, src->sa_len, src);
    786 		l2tp_putref_variant(var, &psref);
    787 		curlwp_bindx(bound);
    788 		break;
    789 
    790 	case SIOCGLIFPHYADDR:
    791 		bound = curlwp_bind();
    792 		var = l2tp_getref_variant(sc, &psref);
    793 		if (var == NULL) {
    794 			curlwp_bindx(bound);
    795 			error = EADDRNOTAVAIL;
    796 			goto bad;
    797 		}
    798 		if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
    799 			l2tp_putref_variant(var, &psref);
    800 			curlwp_bindx(bound);
    801 			error = EADDRNOTAVAIL;
    802 			goto bad;
    803 		}
    804 
    805 		/* copy src */
    806 		src = var->lv_psrc;
    807 		dst = (struct sockaddr *)
    808 			&(((struct if_laddrreq *)data)->addr);
    809 		size = sizeof(((struct if_laddrreq *)data)->addr);
    810 		if (src->sa_len > size) {
    811 			l2tp_putref_variant(var, &psref);
    812 			curlwp_bindx(bound);
    813 			return EINVAL;
    814                 }
    815 		sockaddr_copy(dst, src->sa_len, src);
    816 
    817 		/* copy dst */
    818 		src = var->lv_pdst;
    819 		dst = (struct sockaddr *)
    820 			&(((struct if_laddrreq *)data)->dstaddr);
    821 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
    822 		if (src->sa_len > size) {
    823 			l2tp_putref_variant(var, &psref);
    824 			curlwp_bindx(bound);
    825 			return EINVAL;
    826                 }
    827 		sockaddr_copy(dst, src->sa_len, src);
    828 		l2tp_putref_variant(var, &psref);
    829 		curlwp_bindx(bound);
    830 		break;
    831 
    832 	case SIOCSL2TPSESSION:
    833 		if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
    834 			break;
    835 
    836 		/* session id must not zero */
    837 		if (l2tpr.my_sess_id == 0 || l2tpr.peer_sess_id == 0)
    838 			return EINVAL;
    839 
    840 		bound = curlwp_bind();
    841 		var_tmp = l2tp_lookup_session_ref(l2tpr.my_sess_id, &psref);
    842 		if (var_tmp != NULL) {
    843 			/* duplicate session id */
    844 			log(LOG_WARNING, "%s: duplicate session id %" PRIu32 " of %s\n",
    845 				sc->l2tp_ec.ec_if.if_xname, l2tpr.my_sess_id,
    846 				var_tmp->lv_softc->l2tp_ec.ec_if.if_xname);
    847 			psref_release(&psref, &var_tmp->lv_psref,
    848 			    lv_psref_class);
    849 			curlwp_bindx(bound);
    850 			return EINVAL;
    851 		}
    852 		curlwp_bindx(bound);
    853 
    854 		error = l2tp_set_session(sc, l2tpr.my_sess_id, l2tpr.peer_sess_id);
    855 		break;
    856 	case SIOCDL2TPSESSION:
    857 		l2tp_clear_session(sc);
    858 		break;
    859 	case SIOCSL2TPCOOKIE:
    860 		if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
    861 			break;
    862 
    863 		error = l2tp_set_cookie(sc, l2tpr.my_cookie, l2tpr.my_cookie_len,
    864 		    l2tpr.peer_cookie, l2tpr.peer_cookie_len);
    865 		break;
    866 	case SIOCDL2TPCOOKIE:
    867 		l2tp_clear_cookie(sc);
    868 		break;
    869 	case SIOCSL2TPSTATE:
    870 		if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
    871 			break;
    872 
    873 		l2tp_set_state(sc, l2tpr.state);
    874 		break;
    875 	case SIOCGL2TP:
    876 		/* get L2TPV3 session info */
    877 		memset(&l2tpr, 0, sizeof(l2tpr));
    878 
    879 		bound = curlwp_bind();
    880 		var = l2tp_getref_variant(sc, &psref);
    881 		if (var == NULL) {
    882 			curlwp_bindx(bound);
    883 			error = EADDRNOTAVAIL;
    884 			goto bad;
    885 		}
    886 
    887 		l2tpr.state = var->lv_state;
    888 		l2tpr.my_sess_id = var->lv_my_sess_id;
    889 		l2tpr.peer_sess_id = var->lv_peer_sess_id;
    890 		l2tpr.my_cookie = var->lv_my_cookie;
    891 		l2tpr.my_cookie_len = var->lv_my_cookie_len;
    892 		l2tpr.peer_cookie = var->lv_peer_cookie;
    893 		l2tpr.peer_cookie_len = var->lv_peer_cookie_len;
    894 		l2tp_putref_variant(var, &psref);
    895 		curlwp_bindx(bound);
    896 
    897 		error = copyout(&l2tpr, ifr->ifr_data, sizeof(l2tpr));
    898 		break;
    899 
    900 	default:
    901 		error =	ifioctl_common(ifp, cmd, data);
    902 		break;
    903 	}
    904  bad:
    905 	return error;
    906 }
    907 
    908 static int
    909 l2tp_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
    910 {
    911 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    912 	    l2tp_ec.ec_if);
    913 	struct sockaddr *osrc, *odst;
    914 	struct sockaddr *nsrc, *ndst;
    915 	struct l2tp_variant *ovar, *nvar;
    916 	int error;
    917 
    918 	nsrc = sockaddr_dup(src, M_WAITOK);
    919 	ndst = sockaddr_dup(dst, M_WAITOK);
    920 
    921 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
    922 
    923 	error = encap_lock_enter();
    924 	if (error)
    925 		goto error;
    926 
    927 	mutex_enter(&sc->l2tp_lock);
    928 
    929 	ovar = sc->l2tp_var;
    930 	osrc = ovar->lv_psrc;
    931 	odst = ovar->lv_pdst;
    932 	*nvar = *ovar;
    933 	psref_target_init(&nvar->lv_psref, lv_psref_class);
    934 	nvar->lv_psrc = nsrc;
    935 	nvar->lv_pdst = ndst;
    936 	error = l2tp_encap_attach(nvar);
    937 	if (error) {
    938 		mutex_exit(&sc->l2tp_lock);
    939 		encap_lock_exit();
    940 		goto error;
    941 	}
    942 	membar_producer();
    943 	l2tp_variant_update(sc, nvar);
    944 
    945 	mutex_exit(&sc->l2tp_lock);
    946 
    947 	(void)l2tp_encap_detach(ovar);
    948 	encap_lock_exit();
    949 
    950 	if (osrc)
    951 		sockaddr_free(osrc);
    952 	if (odst)
    953 		sockaddr_free(odst);
    954 	kmem_free(ovar, sizeof(*ovar));
    955 
    956 	return 0;
    957 
    958 error:
    959 	sockaddr_free(nsrc);
    960 	sockaddr_free(ndst);
    961 	kmem_free(nvar, sizeof(*nvar));
    962 
    963 	return error;
    964 }
    965 
    966 static void
    967 l2tp_delete_tunnel(struct ifnet *ifp)
    968 {
    969 	struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
    970 	    l2tp_ec.ec_if);
    971 	struct sockaddr *osrc, *odst;
    972 	struct l2tp_variant *ovar, *nvar;
    973 	int error;
    974 
    975 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
    976 
    977 	error = encap_lock_enter();
    978 	if (error) {
    979 		kmem_free(nvar, sizeof(*nvar));
    980 		return;
    981 	}
    982 	mutex_enter(&sc->l2tp_lock);
    983 
    984 	ovar = sc->l2tp_var;
    985 	osrc = ovar->lv_psrc;
    986 	odst = ovar->lv_pdst;
    987 	*nvar = *ovar;
    988 	psref_target_init(&nvar->lv_psref, lv_psref_class);
    989 	nvar->lv_psrc = NULL;
    990 	nvar->lv_pdst = NULL;
    991 	membar_producer();
    992 	l2tp_variant_update(sc, nvar);
    993 
    994 	mutex_exit(&sc->l2tp_lock);
    995 
    996 	(void)l2tp_encap_detach(ovar);
    997 	encap_lock_exit();
    998 
    999 	if (osrc)
   1000 		sockaddr_free(osrc);
   1001 	if (odst)
   1002 		sockaddr_free(odst);
   1003 	kmem_free(ovar, sizeof(*ovar));
   1004 }
   1005 
   1006 static int
   1007 id_hash_func(uint32_t id, u_long mask)
   1008 {
   1009 	uint32_t hash;
   1010 
   1011 	hash = (id >> 16) ^ id;
   1012 	hash = (hash >> 4) ^ hash;
   1013 
   1014 	return hash & mask;
   1015 }
   1016 
   1017 static void
   1018 l2tp_hash_init(void)
   1019 {
   1020 
   1021 	l2tp_hash.lists = hashinit(L2TP_ID_HASH_SIZE, HASH_PSLIST, true,
   1022 	    &l2tp_hash.mask);
   1023 }
   1024 
   1025 static int
   1026 l2tp_hash_fini(void)
   1027 {
   1028 	int i;
   1029 
   1030 	mutex_enter(&l2tp_hash.lock);
   1031 
   1032 	for (i = 0; i < l2tp_hash.mask + 1; i++) {
   1033 		if (PSLIST_WRITER_FIRST(&l2tp_hash.lists[i], struct l2tp_softc,
   1034 			l2tp_hash) != NULL) {
   1035 			mutex_exit(&l2tp_hash.lock);
   1036 			return EBUSY;
   1037 		}
   1038 	}
   1039 	for (i = 0; i < l2tp_hash.mask + 1; i++)
   1040 		PSLIST_DESTROY(&l2tp_hash.lists[i]);
   1041 
   1042 	mutex_exit(&l2tp_hash.lock);
   1043 
   1044 	hashdone(l2tp_hash.lists, HASH_PSLIST, l2tp_hash.mask);
   1045 
   1046 	return 0;
   1047 }
   1048 
   1049 static int
   1050 l2tp_set_session(struct l2tp_softc *sc, uint32_t my_sess_id,
   1051     uint32_t peer_sess_id)
   1052 {
   1053 	uint32_t idx;
   1054 	struct l2tp_variant *nvar;
   1055 	struct l2tp_variant *ovar;
   1056 	struct ifnet *ifp = &sc->l2tp_ec.ec_if;
   1057 
   1058 	nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
   1059 
   1060 	mutex_enter(&sc->l2tp_lock);
   1061 	ovar = sc->l2tp_var;
   1062 	*nvar = *ovar;
   1063 	psref_target_init(&nvar->lv_psref, lv_psref_class);
   1064 	nvar->lv_my_sess_id = my_sess_id;
   1065 	nvar->lv_peer_sess_id = peer_sess_id;
   1066 	membar_producer();
   1067 
   1068 	mutex_enter(&l2tp_hash.lock);
   1069 	if (ovar->lv_my_sess_id > 0 && ovar->lv_peer_sess_id > 0) {
   1070 		PSLIST_WRITER_REMOVE(sc, l2tp_hash);
   1071 		pserialize_perform(l2tp_psz);
   1072 	}
   1073 	mutex_exit(&l2tp_hash.lock);
   1074 
   1075 	l2tp_variant_update(sc, nvar);
   1076 	mutex_exit(&sc->l2tp_lock);
   1077 
   1078 	idx = id_hash_func(nvar->lv_my_sess_id, l2tp_hash.mask);
   1079 	if ((ifp->if_flags & IFF_DEBUG) != 0)
   1080 		log(LOG_DEBUG, "%s: add hash entry: sess_id=%" PRIu32 ", idx=%" PRIu32 "\n",
   1081 		    sc->l2tp_ec.ec_if.if_xname, nvar->lv_my_sess_id, idx);
   1082 
   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