Home | History | Annotate | Line # | Download | only in netinet
in_l2tp.c revision 1.1
      1 /*	$NetBSD: in_l2tp.c,v 1.1 2017/02/16 08:23:35 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: in_l2tp.c,v 1.1 2017/02/16 08:23:35 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 #ifdef ALTQ
     60 #include <altq/altq.h>
     61 #endif
     62 
     63 /* TODO: IP_TCPMSS support */
     64 #undef IP_TCPMSS
     65 #ifdef IP_TCPMSS
     66 #include <netinet/ip_tcpmss.h>
     67 #endif
     68 
     69 #include <net/if_l2tp.h>
     70 
     71 #include <net/net_osdep.h>
     72 
     73 int ip_l2tp_ttl = L2TP_TTL;
     74 
     75 static void in_l2tp_input(struct mbuf *, int, int);
     76 
     77 static const struct encapsw in_l2tp_encapsw = {
     78 	.encapsw4 = {
     79 		.pr_input	= in_l2tp_input,
     80 		.pr_ctlinput	= NULL,
     81 	}
     82 };
     83 
     84 static int in_l2tp_match(struct mbuf *, int, int, void *);
     85 
     86 int
     87 in_l2tp_output(struct l2tp_variant *var, struct mbuf *m)
     88 {
     89 	struct l2tp_softc *sc;
     90 	struct ifnet *ifp;
     91 	struct sockaddr_in *sin_src = satosin(var->lv_psrc);
     92 	struct sockaddr_in *sin_dst = satosin(var->lv_pdst);
     93 	struct ip iphdr;	/* capsule IP header, host byte ordered */
     94 	struct rtentry *rt;
     95 	struct l2tp_ro *lro;
     96 	int error;
     97 	uint32_t sess_id;
     98 
     99 	KASSERT(var != NULL);
    100 	KASSERT(l2tp_heldref_variant(var));
    101 	KASSERT(sin_src != NULL && sin_dst != NULL);
    102 	KASSERT(sin_src->sin_family == AF_INET
    103 	    && sin_dst->sin_family == AF_INET);
    104 
    105 	sc = var->lv_softc;
    106 	if (sc == NULL)
    107 		return ENETUNREACH;
    108 
    109 	ifp = &sc->l2tp_ec.ec_if;
    110 	error = l2tp_check_nesting(ifp, m);
    111 	if (error)
    112 		goto looped;
    113 
    114 #ifdef NETYET
    115 /* TODO: support ALTQ for innner frame */
    116 #ifdef ALTQ
    117 	ALTQ_SAVE_PAYLOAD(m, AF_ETHER);
    118 #endif
    119 #endif
    120 
    121 	memset(&iphdr, 0, sizeof(iphdr));
    122 	iphdr.ip_src = sin_src->sin_addr;
    123 	/* bidirectional configured tunnel mode */
    124 	if (sin_dst->sin_addr.s_addr != INADDR_ANY)
    125 		iphdr.ip_dst = sin_dst->sin_addr;
    126 	else {
    127 		m_freem(m);
    128 		if ((ifp->if_flags & IFF_DEBUG) != 0)
    129 			log(LOG_DEBUG, "%s: ENETUNREACH\n", __func__);
    130 		error = ENETUNREACH;
    131 		goto out;
    132 	}
    133 	iphdr.ip_p = IPPROTO_L2TP;
    134 	/* version will be set in ip_output() */
    135 	iphdr.ip_ttl = ip_l2tp_ttl;
    136 	/* outer IP header length */
    137 	iphdr.ip_len = sizeof(struct ip);
    138 	/* session-id length */
    139 	iphdr.ip_len += sizeof(uint32_t);
    140 	if (var->lv_use_cookie == L2TP_COOKIE_ON) {
    141 		/* cookie length */
    142 		iphdr.ip_len += var->lv_peer_cookie_len;
    143 	}
    144 
    145 /* TODO: IP_TCPMSS support */
    146 #ifdef IP_TCPMSS
    147 	m = l2tp_tcpmss_clamp(ifp, m);
    148 	if (m == NULL) {
    149 		error = EINVAL;
    150 		goto out;
    151 	}
    152 #endif
    153 	/*
    154 	 * payload length
    155 	 *  NOTE: Payload length may be changed in ip_tcpmss().
    156 	 *        Typical case is missing of TCP mss option in original
    157 	 *        TCP header.
    158 	 */
    159 	iphdr.ip_len += m->m_pkthdr.len;
    160 	HTONS(iphdr.ip_len);
    161 
    162 	if (var->lv_use_cookie == L2TP_COOKIE_ON) {
    163 		/* prepend session cookie */
    164 		uint32_t cookie_32;
    165 		uint64_t cookie_64;
    166 		M_PREPEND(m, var->lv_peer_cookie_len, M_DONTWAIT);
    167 		if (m && m->m_len < var->lv_peer_cookie_len)
    168 			m = m_pullup(m, var->lv_peer_cookie_len);
    169 		if (m == NULL) {
    170 			error = ENOBUFS;
    171 			goto out;
    172 		}
    173 		if (var->lv_peer_cookie_len == 4) {
    174 			cookie_32 = htonl((uint32_t)var->lv_peer_cookie);
    175 			memcpy(mtod(m, void *), &cookie_32,
    176 			    sizeof(uint32_t));
    177 		} else {
    178 			cookie_64 = htobe64(var->lv_peer_cookie);
    179 			memcpy(mtod(m, void *), &cookie_64,
    180 			    sizeof(uint64_t));
    181 		}
    182 	}
    183 
    184 	/* prepend session-ID */
    185 	sess_id = htonl(var->lv_peer_sess_id);
    186 	M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
    187 	if (m && m->m_len < sizeof(uint32_t))
    188 		m = m_pullup(m, sizeof(uint32_t));
    189 	if (m == NULL) {
    190 		error = ENOBUFS;
    191 		goto out;
    192 	}
    193 	memcpy(mtod(m, uint32_t *), &sess_id, sizeof(uint32_t));
    194 
    195 	/* prepend new IP header */
    196 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    197 	if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
    198 		if (m)
    199 			m = m_copyup(m, sizeof(struct ip), 0);
    200 	} else {
    201 		if (m && m->m_len < sizeof(struct ip))
    202 			m = m_pullup(m, sizeof(struct ip));
    203 	}
    204 	if (m == NULL) {
    205 		error = ENOBUFS;
    206 		goto out;
    207 	}
    208 	memcpy(mtod(m, struct ip *), &iphdr, sizeof(struct ip));
    209 
    210 	lro = percpu_getref(sc->l2tp_ro_percpu);
    211 	mutex_enter(&lro->lr_lock);
    212 	if ((rt = rtcache_lookup(&lro->lr_ro, var->lv_pdst)) == NULL) {
    213 		mutex_exit(&lro->lr_lock);
    214 		percpu_putref(sc->l2tp_ro_percpu);
    215 		m_freem(m);
    216 		error = ENETUNREACH;
    217 		goto out;
    218 	}
    219 
    220 	if (rt->rt_ifp == ifp) {
    221 		rtcache_unref(rt, &lro->lr_ro);
    222 		rtcache_free(&lro->lr_ro);
    223 		mutex_exit(&lro->lr_lock);
    224 		percpu_putref(sc->l2tp_ro_percpu);
    225 		m_freem(m);
    226 		error = ENETUNREACH;	/*XXX*/
    227 		goto out;
    228 	}
    229 	rtcache_unref(rt, &lro->lr_ro);
    230 
    231 	/*
    232 	 * To avoid inappropriate rewrite of checksum,
    233 	 * clear csum flags.
    234 	 */
    235 	m->m_pkthdr.csum_flags  = 0;
    236 
    237 	error = ip_output(m, NULL, &lro->lr_ro, 0, NULL, NULL);
    238 	mutex_exit(&lro->lr_lock);
    239 	percpu_putref(sc->l2tp_ro_percpu);
    240 	return error;
    241 
    242 looped:
    243 	if (error)
    244 		ifp->if_oerrors++;
    245 
    246 out:
    247 	return error;
    248 }
    249 
    250 static void
    251 in_l2tp_input(struct mbuf *m, int off, int proto)
    252 {
    253 	struct ifnet *l2tpp = NULL;
    254 	struct l2tp_softc *sc;
    255 	uint32_t sess_id;
    256 	uint32_t cookie_32;
    257 	uint64_t cookie_64;
    258 	struct psref psref;
    259 	struct l2tp_variant *var;
    260 
    261 	if (m->m_len < off + sizeof(uint32_t)) {
    262 		m = m_pullup(m, off + sizeof(uint32_t));
    263 		if (!m) {
    264 			/* if payload length < 4 octets */
    265 			return;
    266 		}
    267         }
    268 
    269 	/* get L2TP session ID */
    270 	m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id);
    271 	NTOHL(sess_id);
    272 #ifdef L2TP_DEBUG
    273 	log(LOG_DEBUG, "%s: sess_id = %" PRIu32 "\n", __func__, sess_id);
    274 #endif
    275 	if (sess_id == 0) {
    276 		/*
    277 		 * L2TPv3 control packet received.
    278 		 * userland daemon(l2tpd?) should process.
    279 		 */
    280 		rip_input(m, off, proto);
    281 		return;
    282 	}
    283 
    284 	var = l2tp_lookup_session_ref(sess_id, &psref);
    285 	if (var == NULL) {
    286 		m_freem(m);
    287 		ip_statinc(IP_STAT_NOL2TP);
    288 		return;
    289 	} else {
    290 		sc = var->lv_softc;
    291 		l2tpp = &(sc->l2tp_ec.ec_if);
    292 
    293 		if (l2tpp == NULL || (l2tpp->if_flags & IFF_UP) == 0) {
    294 #ifdef L2TP_DEBUG
    295 			if (l2tpp == NULL)
    296 				log(LOG_DEBUG, "%s: l2tpp is NULL\n", __func__);
    297 			else
    298 				log(LOG_DEBUG, "%s: l2tpp is down\n", __func__);
    299 #endif
    300 			m_freem(m);
    301 			ip_statinc(IP_STAT_NOL2TP);
    302 			goto out;
    303 		}
    304 
    305 		/* other CPU do l2tp_delete_tunnel */
    306 		if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
    307 			m_freem(m);
    308 			ip_statinc(IP_STAT_NOL2TP);
    309 			goto out;
    310 		}
    311 	}
    312 
    313 	if (var->lv_state != L2TP_STATE_UP) {
    314 		m_freem(m);
    315 		goto out;
    316 	}
    317 
    318 	if (sess_id != var->lv_my_sess_id) {
    319 		m_freem(m);
    320 		goto out;
    321 	}
    322 
    323 	m_adj(m, off + sizeof(uint32_t));
    324 
    325 	if (var->lv_use_cookie == L2TP_COOKIE_ON) {
    326 		if (var->lv_my_cookie_len == 4) {
    327 			m_copydata(m, 0, sizeof(uint32_t), (void *)&cookie_32);
    328 			NTOHL(cookie_32);
    329 			if (cookie_32 != var->lv_my_cookie) {
    330 				m_freem(m);
    331 				goto out;
    332 			}
    333 			m_adj(m, sizeof(uint32_t));
    334 		} else {
    335 			m_copydata(m, 0, sizeof(uint64_t), (void *)&cookie_64);
    336 			BE64TOH(cookie_64);
    337 			if (cookie_64 != var->lv_my_cookie) {
    338 				m_freem(m);
    339 				goto out;
    340 			}
    341 			m_adj(m, sizeof(uint64_t));
    342 		}
    343 	}
    344 
    345 /* TODO: IP_TCPMSS support */
    346 #ifdef IP_TCPMSS
    347 	m = l2tp_tcpmss_clamp(l2tpp, m);
    348 	if (m == NULL)
    349 		goto out;
    350 #endif
    351 	l2tp_input(m, l2tpp);
    352 
    353 out:
    354 	l2tp_putref_variant(var, &psref);
    355 	return;
    356 }
    357 
    358 /*
    359  * This function is used by encap4_lookup() to decide priority of the encaptab.
    360  * This priority is compared to the match length between mbuf's source/destination
    361  * IPv4 address pair and encaptab's one.
    362  * l2tp(4) does not use address pairs to search matched encaptab, so this
    363  * function must return the length bigger than or equals to IPv4 address pair to
    364  * avoid wrong encaptab.
    365  */
    366 static int
    367 in_l2tp_match(struct mbuf *m, int off, int proto, void *arg)
    368 {
    369 	struct l2tp_variant *var = arg;
    370 	uint32_t sess_id;
    371 
    372 	KASSERT(proto == IPPROTO_L2TP);
    373 
    374 	if (m->m_len < off + sizeof(uint32_t)) {
    375 		m = m_pullup(m, off + sizeof(uint32_t));
    376 		if (!m) {
    377 			/* if payload length < 4 octets */
    378 			return 0;
    379 		}
    380         }
    381 
    382 	/* get L2TP session ID */
    383 	m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id);
    384 	NTOHL(sess_id);
    385 	if (sess_id == 0) {
    386 		/*
    387 		 * L2TPv3 control packet received.
    388 		 * userland daemon(l2tpd?) should process.
    389 		 */
    390 		return 32 * 2;
    391 	} else if (sess_id == var->lv_my_sess_id)
    392 		return 32 * 2;
    393 	else
    394 		return 0;
    395 }
    396 
    397 int
    398 in_l2tp_attach(struct l2tp_variant *var)
    399 {
    400 
    401 	var->lv_encap_cookie = encap_attach_func(AF_INET, IPPROTO_L2TP,
    402 	    in_l2tp_match, &in_l2tp_encapsw, var);
    403 	if (var->lv_encap_cookie == NULL)
    404 		return EEXIST;
    405 
    406 	return 0;
    407 }
    408 
    409 int
    410 in_l2tp_detach(struct l2tp_variant *var)
    411 {
    412 	int error;
    413 
    414 	error = encap_detach(var->lv_encap_cookie);
    415 	if (error == 0)
    416 		var->lv_encap_cookie = NULL;
    417 
    418 	return error;
    419 }
    420