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