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