Home | History | Annotate | Line # | Download | only in netinet6
in6_l2tp.c revision 1.5.4.2
      1  1.5.4.2  bouyer /*	$NetBSD: in6_l2tp.c,v 1.5.4.2 2017/04/21 16:54:06 bouyer Exp $	*/
      2  1.5.4.2  bouyer 
      3  1.5.4.2  bouyer /*
      4  1.5.4.2  bouyer  * Copyright (c) 2017 Internet Initiative Japan Inc.
      5  1.5.4.2  bouyer  * All rights reserved.
      6  1.5.4.2  bouyer  *
      7  1.5.4.2  bouyer  * Redistribution and use in source and binary forms, with or without
      8  1.5.4.2  bouyer  * modification, are permitted provided that the following conditions
      9  1.5.4.2  bouyer  * are met:
     10  1.5.4.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     11  1.5.4.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     12  1.5.4.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.5.4.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     14  1.5.4.2  bouyer  *    documentation and/or other materials provided with the distribution.
     15  1.5.4.2  bouyer  *
     16  1.5.4.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.5.4.2  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.5.4.2  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.5.4.2  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.5.4.2  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.5.4.2  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.5.4.2  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.5.4.2  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.5.4.2  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.5.4.2  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.5.4.2  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     27  1.5.4.2  bouyer  */
     28  1.5.4.2  bouyer 
     29  1.5.4.2  bouyer #include <sys/cdefs.h>
     30  1.5.4.2  bouyer __KERNEL_RCSID(0, "$NetBSD: in6_l2tp.c,v 1.5.4.2 2017/04/21 16:54:06 bouyer Exp $");
     31  1.5.4.2  bouyer 
     32  1.5.4.2  bouyer #ifdef _KERNEL_OPT
     33  1.5.4.2  bouyer #include "opt_l2tp.h"
     34  1.5.4.2  bouyer #endif
     35  1.5.4.2  bouyer 
     36  1.5.4.2  bouyer #include <sys/param.h>
     37  1.5.4.2  bouyer #include <sys/systm.h>
     38  1.5.4.2  bouyer #include <sys/socket.h>
     39  1.5.4.2  bouyer #include <sys/sockio.h>
     40  1.5.4.2  bouyer #include <sys/mbuf.h>
     41  1.5.4.2  bouyer #include <sys/errno.h>
     42  1.5.4.2  bouyer #include <sys/ioctl.h>
     43  1.5.4.2  bouyer #include <sys/syslog.h>
     44  1.5.4.2  bouyer #include <sys/kernel.h>
     45  1.5.4.2  bouyer 
     46  1.5.4.2  bouyer #include <net/if.h>
     47  1.5.4.2  bouyer #include <net/route.h>
     48  1.5.4.2  bouyer #include <net/if_ether.h>
     49  1.5.4.2  bouyer 
     50  1.5.4.2  bouyer #include <netinet/in.h>
     51  1.5.4.2  bouyer #include <netinet/in_systm.h>
     52  1.5.4.2  bouyer #include <netinet/ip.h>
     53  1.5.4.2  bouyer #include <netinet/ip_var.h>
     54  1.5.4.2  bouyer #include <netinet/ip_private.h>
     55  1.5.4.2  bouyer #include <netinet/in_l2tp.h>
     56  1.5.4.2  bouyer #include <netinet/in_var.h>
     57  1.5.4.2  bouyer #include <netinet/ip_encap.h>
     58  1.5.4.2  bouyer 
     59  1.5.4.2  bouyer #include <netinet/ip6.h>
     60  1.5.4.2  bouyer #include <netinet6/ip6_var.h>
     61  1.5.4.2  bouyer #include <netinet6/in6_l2tp.h>
     62  1.5.4.2  bouyer 
     63  1.5.4.2  bouyer #ifdef ALTQ
     64  1.5.4.2  bouyer #include <altq/altq.h>
     65  1.5.4.2  bouyer #endif
     66  1.5.4.2  bouyer 
     67  1.5.4.2  bouyer /* TODO: IP_TCPMSS support */
     68  1.5.4.2  bouyer #undef IP_TCPMSS
     69  1.5.4.2  bouyer #ifdef IP_TCPMSS
     70  1.5.4.2  bouyer #include <netinet/ip_tcpmss.h>
     71  1.5.4.2  bouyer #endif
     72  1.5.4.2  bouyer 
     73  1.5.4.2  bouyer #include <net/if_l2tp.h>
     74  1.5.4.2  bouyer 
     75  1.5.4.2  bouyer #define L2TP_HLIM6		64
     76  1.5.4.2  bouyer int ip6_l2tp_hlim = L2TP_HLIM6;
     77  1.5.4.2  bouyer 
     78  1.5.4.2  bouyer static int in6_l2tp_input(struct mbuf **, int *, int);
     79  1.5.4.2  bouyer 
     80  1.5.4.2  bouyer static const struct encapsw in6_l2tp_encapsw = {
     81  1.5.4.2  bouyer 	.encapsw6 = {
     82  1.5.4.2  bouyer 		.pr_input	= in6_l2tp_input,
     83  1.5.4.2  bouyer 		.pr_ctlinput	= NULL,
     84  1.5.4.2  bouyer 	}
     85  1.5.4.2  bouyer };
     86  1.5.4.2  bouyer 
     87  1.5.4.2  bouyer static int in6_l2tp_match(struct mbuf *, int, int, void *);
     88  1.5.4.2  bouyer 
     89  1.5.4.2  bouyer int
     90  1.5.4.2  bouyer in6_l2tp_output(struct l2tp_variant *var, struct mbuf *m)
     91  1.5.4.2  bouyer {
     92  1.5.4.2  bouyer 	struct rtentry *rt;
     93  1.5.4.2  bouyer 	struct l2tp_ro *lro;
     94  1.5.4.2  bouyer 	struct l2tp_softc *sc;
     95  1.5.4.2  bouyer 	struct ifnet *ifp;
     96  1.5.4.2  bouyer 	struct sockaddr_in6 *sin6_src = satosin6(var->lv_psrc);
     97  1.5.4.2  bouyer 	struct sockaddr_in6 *sin6_dst = satosin6(var->lv_pdst);
     98  1.5.4.2  bouyer 	struct ip6_hdr ip6hdr;	/* capsule IP header, host byte ordered */
     99  1.5.4.2  bouyer 	int error;
    100  1.5.4.2  bouyer 	uint32_t sess_id;
    101  1.5.4.2  bouyer 
    102  1.5.4.2  bouyer 	KASSERT(var != NULL);
    103  1.5.4.2  bouyer 	KASSERT(l2tp_heldref_variant(var));
    104  1.5.4.2  bouyer 	KASSERT(sin6_src != NULL && sin6_dst != NULL);
    105  1.5.4.2  bouyer 	KASSERT(sin6_src->sin6_family == AF_INET6
    106  1.5.4.2  bouyer 	    && sin6_dst->sin6_family == AF_INET6);
    107  1.5.4.2  bouyer 
    108  1.5.4.2  bouyer 	sc = var->lv_softc;
    109  1.5.4.2  bouyer 	ifp = &sc->l2tp_ec.ec_if;
    110  1.5.4.2  bouyer 	error = l2tp_check_nesting(ifp, m);
    111  1.5.4.2  bouyer 	if (error)
    112  1.5.4.2  bouyer 		goto looped;
    113  1.5.4.2  bouyer 
    114  1.5.4.2  bouyer #ifdef NOTYET
    115  1.5.4.2  bouyer /* TODO: support ALTQ for innner frame */
    116  1.5.4.2  bouyer #ifdef ALTQ
    117  1.5.4.2  bouyer 	ALTQ_SAVE_PAYLOAD(m, AF_ETHER);
    118  1.5.4.2  bouyer #endif
    119  1.5.4.2  bouyer #endif
    120  1.5.4.2  bouyer 
    121  1.5.4.2  bouyer 	memset(&ip6hdr, 0, sizeof(ip6hdr));
    122  1.5.4.2  bouyer 	ip6hdr.ip6_src = sin6_src->sin6_addr;
    123  1.5.4.2  bouyer 	/* bidirectional configured tunnel mode */
    124  1.5.4.2  bouyer 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
    125  1.5.4.2  bouyer 		ip6hdr.ip6_dst = sin6_dst->sin6_addr;
    126  1.5.4.2  bouyer 	else {
    127  1.5.4.2  bouyer 		m_freem(m);
    128  1.5.4.2  bouyer 		if ((ifp->if_flags & IFF_DEBUG) != 0)
    129  1.5.4.2  bouyer 			log(LOG_DEBUG, "%s: ENETUNREACH\n", __func__);
    130  1.5.4.2  bouyer 		return ENETUNREACH;
    131  1.5.4.2  bouyer 	}
    132  1.5.4.2  bouyer 	/* unlike IPv4, IP version must be filled by caller of ip6_output() */
    133  1.5.4.2  bouyer 	ip6hdr.ip6_vfc  = 0x60;
    134  1.5.4.2  bouyer 	ip6hdr.ip6_nxt  = IPPROTO_L2TP;
    135  1.5.4.2  bouyer 	ip6hdr.ip6_hlim = ip6_l2tp_hlim;
    136  1.5.4.2  bouyer 	/* outer IP payload length */
    137  1.5.4.2  bouyer 	ip6hdr.ip6_plen = 0;
    138  1.5.4.2  bouyer 	/* session-id length */
    139  1.5.4.2  bouyer 	ip6hdr.ip6_plen += sizeof(uint32_t);
    140  1.5.4.2  bouyer 	if (var->lv_use_cookie == L2TP_COOKIE_ON) {
    141  1.5.4.2  bouyer 		/* cookie length */
    142  1.5.4.2  bouyer 		ip6hdr.ip6_plen += var->lv_peer_cookie_len;
    143  1.5.4.2  bouyer 	}
    144  1.5.4.2  bouyer 
    145  1.5.4.2  bouyer /* TODO: IP_TCPMSS support */
    146  1.5.4.2  bouyer #ifdef IP_TCPMSS
    147  1.5.4.2  bouyer 	m = l2tp_tcpmss_clamp(ifp, m);
    148  1.5.4.2  bouyer 	if (m == NULL)
    149  1.5.4.2  bouyer 		return EINVAL;
    150  1.5.4.2  bouyer #endif
    151  1.5.4.2  bouyer 
    152  1.5.4.2  bouyer 	/*
    153  1.5.4.2  bouyer 	 * payload length
    154  1.5.4.2  bouyer 	 *  NOTE: Payload length may be changed in ip_tcpmss().
    155  1.5.4.2  bouyer 	 *        Typical case is missing of TCP mss option in original
    156  1.5.4.2  bouyer 	 *        TCP header.
    157  1.5.4.2  bouyer 	 */
    158  1.5.4.2  bouyer 	ip6hdr.ip6_plen += m->m_pkthdr.len;
    159  1.5.4.2  bouyer 	HTONS(ip6hdr.ip6_plen);
    160  1.5.4.2  bouyer 
    161  1.5.4.2  bouyer 	if (var->lv_use_cookie == L2TP_COOKIE_ON) {
    162  1.5.4.2  bouyer 		/* prepend session cookie */
    163  1.5.4.2  bouyer 		uint32_t cookie_32;
    164  1.5.4.2  bouyer 		uint64_t cookie_64;
    165  1.5.4.2  bouyer 		M_PREPEND(m, var->lv_peer_cookie_len, M_DONTWAIT);
    166  1.5.4.2  bouyer 		if (m && m->m_len < var->lv_peer_cookie_len)
    167  1.5.4.2  bouyer 			m = m_pullup(m, var->lv_peer_cookie_len);
    168  1.5.4.2  bouyer 		if (m == NULL)
    169  1.5.4.2  bouyer 			return ENOBUFS;
    170  1.5.4.2  bouyer 		if (var->lv_peer_cookie_len == 4) {
    171  1.5.4.2  bouyer 			cookie_32 = htonl((uint32_t)var->lv_peer_cookie);
    172  1.5.4.2  bouyer 			memcpy(mtod(m, void *), &cookie_32,
    173  1.5.4.2  bouyer 			    sizeof(uint32_t));
    174  1.5.4.2  bouyer 		} else {
    175  1.5.4.2  bouyer 			cookie_64 = htobe64(var->lv_peer_cookie);
    176  1.5.4.2  bouyer 			memcpy(mtod(m, void *), &cookie_64,
    177  1.5.4.2  bouyer 			    sizeof(uint64_t));
    178  1.5.4.2  bouyer 		}
    179  1.5.4.2  bouyer 	}
    180  1.5.4.2  bouyer 
    181  1.5.4.2  bouyer 	/* prepend session-ID */
    182  1.5.4.2  bouyer 	sess_id = htonl(var->lv_peer_sess_id);
    183  1.5.4.2  bouyer 	M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
    184  1.5.4.2  bouyer 	if (m && m->m_len < sizeof(uint32_t))
    185  1.5.4.2  bouyer 		m = m_pullup(m, sizeof(uint32_t));
    186  1.5.4.2  bouyer 	if (m == NULL)
    187  1.5.4.2  bouyer 		return ENOBUFS;
    188  1.5.4.2  bouyer 	memcpy(mtod(m, uint32_t *), &sess_id, sizeof(uint32_t));
    189  1.5.4.2  bouyer 
    190  1.5.4.2  bouyer 	/* prepend new IP header */
    191  1.5.4.2  bouyer 	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
    192  1.5.4.2  bouyer 	if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
    193  1.5.4.2  bouyer 		if (m)
    194  1.5.4.2  bouyer 			m = m_copyup(m, sizeof(struct ip), 0);
    195  1.5.4.2  bouyer 	} else {
    196  1.5.4.2  bouyer 		if (m && m->m_len < sizeof(struct ip6_hdr))
    197  1.5.4.2  bouyer 			m = m_pullup(m, sizeof(struct ip6_hdr));
    198  1.5.4.2  bouyer 	}
    199  1.5.4.2  bouyer 	if (m == NULL)
    200  1.5.4.2  bouyer 		return ENOBUFS;
    201  1.5.4.2  bouyer 	memcpy(mtod(m, struct ip6_hdr *), &ip6hdr, sizeof(struct ip6_hdr));
    202  1.5.4.2  bouyer 
    203  1.5.4.2  bouyer 	lro = percpu_getref(sc->l2tp_ro_percpu);
    204  1.5.4.2  bouyer 	mutex_enter(&lro->lr_lock);
    205  1.5.4.2  bouyer 	if ((rt = rtcache_lookup(&lro->lr_ro, var->lv_pdst)) == NULL) {
    206  1.5.4.2  bouyer 		mutex_exit(&lro->lr_lock);
    207  1.5.4.2  bouyer 		percpu_putref(sc->l2tp_ro_percpu);
    208  1.5.4.2  bouyer 		m_freem(m);
    209  1.5.4.2  bouyer 		return ENETUNREACH;
    210  1.5.4.2  bouyer 	}
    211  1.5.4.2  bouyer 
    212  1.5.4.2  bouyer 	/* If the route constitutes infinite encapsulation, punt. */
    213  1.5.4.2  bouyer 	if (rt->rt_ifp == ifp) {
    214  1.5.4.2  bouyer 		rtcache_unref(rt, &lro->lr_ro);
    215  1.5.4.2  bouyer 		rtcache_free(&lro->lr_ro);
    216  1.5.4.2  bouyer 		mutex_exit(&lro->lr_lock);
    217  1.5.4.2  bouyer 		percpu_putref(sc->l2tp_ro_percpu);
    218  1.5.4.2  bouyer 		m_freem(m);
    219  1.5.4.2  bouyer 		return ENETUNREACH;	/* XXX */
    220  1.5.4.2  bouyer 	}
    221  1.5.4.2  bouyer 	rtcache_unref(rt, &lro->lr_ro);
    222  1.5.4.2  bouyer 
    223  1.5.4.2  bouyer 	/*
    224  1.5.4.2  bouyer 	 * To avoid inappropriate rewrite of checksum,
    225  1.5.4.2  bouyer 	 * clear csum flags.
    226  1.5.4.2  bouyer 	 */
    227  1.5.4.2  bouyer 	m->m_pkthdr.csum_flags  = 0;
    228  1.5.4.2  bouyer 
    229  1.5.4.2  bouyer 	error = ip6_output(m, 0, &lro->lr_ro, 0, NULL, NULL, NULL);
    230  1.5.4.2  bouyer 	mutex_exit(&lro->lr_lock);
    231  1.5.4.2  bouyer 	percpu_putref(sc->l2tp_ro_percpu);
    232  1.5.4.2  bouyer 	return(error);
    233  1.5.4.2  bouyer 
    234  1.5.4.2  bouyer looped:
    235  1.5.4.2  bouyer 	if (error)
    236  1.5.4.2  bouyer 		ifp->if_oerrors++;
    237  1.5.4.2  bouyer 
    238  1.5.4.2  bouyer 	return error;
    239  1.5.4.2  bouyer }
    240  1.5.4.2  bouyer 
    241  1.5.4.2  bouyer static int
    242  1.5.4.2  bouyer in6_l2tp_input(struct mbuf **mp, int *offp, int proto)
    243  1.5.4.2  bouyer {
    244  1.5.4.2  bouyer 	struct mbuf *m = *mp;
    245  1.5.4.2  bouyer 	int off = *offp;
    246  1.5.4.2  bouyer 
    247  1.5.4.2  bouyer 	struct ifnet *l2tpp = NULL;
    248  1.5.4.2  bouyer 	struct l2tp_softc *sc;
    249  1.5.4.2  bouyer 	struct l2tp_variant *var;
    250  1.5.4.2  bouyer 	uint32_t sess_id;
    251  1.5.4.2  bouyer 	uint32_t cookie_32;
    252  1.5.4.2  bouyer 	uint64_t cookie_64;
    253  1.5.4.2  bouyer 	struct psref psref;
    254  1.5.4.2  bouyer 
    255  1.5.4.2  bouyer 	if (m->m_len < off + sizeof(uint32_t)) {
    256  1.5.4.2  bouyer 		m = m_pullup(m, off + sizeof(uint32_t));
    257  1.5.4.2  bouyer 		if (!m) {
    258  1.5.4.2  bouyer 			/* if payload length < 4 octets */
    259  1.5.4.2  bouyer 			return IPPROTO_DONE;
    260  1.5.4.2  bouyer 		}
    261  1.5.4.2  bouyer 		*mp = m;
    262  1.5.4.2  bouyer         }
    263  1.5.4.2  bouyer 
    264  1.5.4.2  bouyer 	/* get L2TP session ID */
    265  1.5.4.2  bouyer 	m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id);
    266  1.5.4.2  bouyer 	NTOHL(sess_id);
    267  1.5.4.2  bouyer #ifdef L2TP_DEBUG
    268  1.5.4.2  bouyer 	log(LOG_DEBUG, "%s: sess_id = %" PRIu32 "\n", __func__, sess_id);
    269  1.5.4.2  bouyer #endif
    270  1.5.4.2  bouyer 	if (sess_id == 0) {
    271  1.5.4.2  bouyer 		/*
    272  1.5.4.2  bouyer 		 * L2TPv3 control packet received.
    273  1.5.4.2  bouyer 		 * userland daemon(l2tpd?) should process.
    274  1.5.4.2  bouyer 		 */
    275  1.5.4.2  bouyer 		return rip6_input(mp, offp, proto);
    276  1.5.4.2  bouyer 	}
    277  1.5.4.2  bouyer 
    278  1.5.4.2  bouyer 	var = l2tp_lookup_session_ref(sess_id, &psref);
    279  1.5.4.2  bouyer 	if (var == NULL) {
    280  1.5.4.2  bouyer 		m_freem(m);
    281  1.5.4.2  bouyer 		IP_STATINC(IP_STAT_NOL2TP);
    282  1.5.4.2  bouyer 		return IPPROTO_DONE;
    283  1.5.4.2  bouyer 	} else {
    284  1.5.4.2  bouyer 		sc = var->lv_softc;
    285  1.5.4.2  bouyer 		l2tpp = &(sc->l2tp_ec.ec_if);
    286  1.5.4.2  bouyer 
    287  1.5.4.2  bouyer 		if (l2tpp == NULL || (l2tpp->if_flags & IFF_UP) == 0) {
    288  1.5.4.2  bouyer #ifdef L2TP_DEBUG
    289  1.5.4.2  bouyer 			if (l2tpp == NULL)
    290  1.5.4.2  bouyer 				log(LOG_DEBUG, "%s: l2tpp is NULL\n", __func__);
    291  1.5.4.2  bouyer 			else
    292  1.5.4.2  bouyer 				log(LOG_DEBUG, "%s: l2tpp is down\n", __func__);
    293  1.5.4.2  bouyer #endif
    294  1.5.4.2  bouyer 			m_freem(m);
    295  1.5.4.2  bouyer 			IP_STATINC(IP_STAT_NOL2TP);
    296  1.5.4.2  bouyer 			goto out;
    297  1.5.4.2  bouyer 		}
    298  1.5.4.2  bouyer 		/* other CPU do l2tp_delete_tunnel */
    299  1.5.4.2  bouyer 		if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
    300  1.5.4.2  bouyer 			m_freem(m);
    301  1.5.4.2  bouyer 			ip_statinc(IP_STAT_NOL2TP);
    302  1.5.4.2  bouyer 			goto out;
    303  1.5.4.2  bouyer 		}
    304  1.5.4.2  bouyer 	}
    305  1.5.4.2  bouyer 
    306  1.5.4.2  bouyer 	if (var->lv_state != L2TP_STATE_UP) {
    307  1.5.4.2  bouyer 		m_freem(m);
    308  1.5.4.2  bouyer 		goto out;
    309  1.5.4.2  bouyer 	}
    310  1.5.4.2  bouyer 	m_adj(m, off + sizeof(uint32_t));
    311  1.5.4.2  bouyer 
    312  1.5.4.2  bouyer 	if (var->lv_use_cookie == L2TP_COOKIE_ON) {
    313  1.5.4.2  bouyer 		if (var->lv_my_cookie_len == 4) {
    314  1.5.4.2  bouyer 			m_copydata(m, 0, sizeof(uint32_t), (void *)&cookie_32);
    315  1.5.4.2  bouyer 			NTOHL(cookie_32);
    316  1.5.4.2  bouyer 			if (cookie_32 != var->lv_my_cookie) {
    317  1.5.4.2  bouyer 				m_freem(m);
    318  1.5.4.2  bouyer 				goto out;
    319  1.5.4.2  bouyer 			}
    320  1.5.4.2  bouyer 			m_adj(m, sizeof(uint32_t));
    321  1.5.4.2  bouyer 		} else {
    322  1.5.4.2  bouyer 			m_copydata(m, 0, sizeof(uint64_t), (void *)&cookie_64);
    323  1.5.4.2  bouyer 			BE64TOH(cookie_64);
    324  1.5.4.2  bouyer 			if (cookie_64 != var->lv_my_cookie) {
    325  1.5.4.2  bouyer 				m_freem(m);
    326  1.5.4.2  bouyer 				goto out;
    327  1.5.4.2  bouyer 			}
    328  1.5.4.2  bouyer 			m_adj(m, sizeof(uint64_t));
    329  1.5.4.2  bouyer 		}
    330  1.5.4.2  bouyer 	}
    331  1.5.4.2  bouyer 
    332  1.5.4.2  bouyer /* TODO: IP_TCPMSS support */
    333  1.5.4.2  bouyer #ifdef IP_TCPMSS
    334  1.5.4.2  bouyer 	m = l2tp_tcpmss_clamp(l2tpp, m);
    335  1.5.4.2  bouyer 	if (m == NULL)
    336  1.5.4.2  bouyer 		goto out;
    337  1.5.4.2  bouyer #endif
    338  1.5.4.2  bouyer 	l2tp_input(m, l2tpp);
    339  1.5.4.2  bouyer 
    340  1.5.4.2  bouyer out:
    341  1.5.4.2  bouyer 	l2tp_putref_variant(var, &psref);
    342  1.5.4.2  bouyer 	return IPPROTO_DONE;
    343  1.5.4.2  bouyer }
    344  1.5.4.2  bouyer 
    345  1.5.4.2  bouyer /*
    346  1.5.4.2  bouyer  * This function is used by encap6_lookup() to decide priority of the encaptab.
    347  1.5.4.2  bouyer  * This priority is compared to the match length between mbuf's source/destination
    348  1.5.4.2  bouyer  * IPv6 address pair and encaptab's one.
    349  1.5.4.2  bouyer  * l2tp(4) does not use address pairs to search matched encaptab, so this
    350  1.5.4.2  bouyer  * function must return the length bigger than or equals to IPv6 address pair to
    351  1.5.4.2  bouyer  * avoid wrong encaptab.
    352  1.5.4.2  bouyer  */
    353  1.5.4.2  bouyer static int
    354  1.5.4.2  bouyer in6_l2tp_match(struct mbuf *m, int off, int proto, void *arg)
    355  1.5.4.2  bouyer {
    356  1.5.4.2  bouyer 	struct l2tp_variant *var = arg;
    357  1.5.4.2  bouyer 	uint32_t sess_id;
    358  1.5.4.2  bouyer 
    359  1.5.4.2  bouyer 	KASSERT(proto == IPPROTO_L2TP);
    360  1.5.4.2  bouyer 
    361  1.5.4.2  bouyer 	if (m->m_len < off + sizeof(uint32_t)) {
    362  1.5.4.2  bouyer 		m = m_pullup(m, off + sizeof(uint32_t));
    363  1.5.4.2  bouyer 		if (!m) {
    364  1.5.4.2  bouyer 			/* if payload length < 4 octets */
    365  1.5.4.2  bouyer 			return 0;
    366  1.5.4.2  bouyer 		}
    367  1.5.4.2  bouyer         }
    368  1.5.4.2  bouyer 
    369  1.5.4.2  bouyer 	/* get L2TP session ID */
    370  1.5.4.2  bouyer 	m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id);
    371  1.5.4.2  bouyer 	NTOHL(sess_id);
    372  1.5.4.2  bouyer 	if (sess_id == 0) {
    373  1.5.4.2  bouyer 		/*
    374  1.5.4.2  bouyer 		 * L2TPv3 control packet received.
    375  1.5.4.2  bouyer 		 * userland daemon(l2tpd?) should process.
    376  1.5.4.2  bouyer 		 */
    377  1.5.4.2  bouyer 		return 128 * 2;
    378  1.5.4.2  bouyer 	} else if (sess_id == var->lv_my_sess_id)
    379  1.5.4.2  bouyer 		return 128 * 2;
    380  1.5.4.2  bouyer 	else
    381  1.5.4.2  bouyer 		return 0;
    382  1.5.4.2  bouyer }
    383  1.5.4.2  bouyer 
    384  1.5.4.2  bouyer int
    385  1.5.4.2  bouyer in6_l2tp_attach(struct l2tp_variant *var)
    386  1.5.4.2  bouyer {
    387  1.5.4.2  bouyer 
    388  1.5.4.2  bouyer 	var->lv_encap_cookie = encap_attach_func(AF_INET6, IPPROTO_L2TP,
    389  1.5.4.2  bouyer 	    in6_l2tp_match, &in6_l2tp_encapsw, var);
    390  1.5.4.2  bouyer 	if (var->lv_encap_cookie == NULL)
    391  1.5.4.2  bouyer 		return EEXIST;
    392  1.5.4.2  bouyer 
    393  1.5.4.2  bouyer 	return 0;
    394  1.5.4.2  bouyer }
    395  1.5.4.2  bouyer 
    396  1.5.4.2  bouyer int
    397  1.5.4.2  bouyer in6_l2tp_detach(struct l2tp_variant *var)
    398  1.5.4.2  bouyer {
    399  1.5.4.2  bouyer 	int error;
    400  1.5.4.2  bouyer 
    401  1.5.4.2  bouyer 	error = encap_detach(var->lv_encap_cookie);
    402  1.5.4.2  bouyer 	if (error == 0)
    403  1.5.4.2  bouyer 		var->lv_encap_cookie = NULL;
    404  1.5.4.2  bouyer 
    405  1.5.4.2  bouyer 	return error;
    406  1.5.4.2  bouyer }
    407