xref: /netbsd-src/sys/netinet/in_l2tp.c (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
1 /*	$NetBSD: in_l2tp.c,v 1.18 2020/01/29 04:37:24 thorpej 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.18 2020/01/29 04:37:24 thorpej 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 #include <sys/socketvar.h> /* For softnet_lock */
46 
47 #include <net/if.h>
48 #include <net/route.h>
49 #include <net/if_ether.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/ip_private.h>
56 #include <netinet/in_l2tp.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip_encap.h>
59 
60 #ifdef ALTQ
61 #include <altq/altq.h>
62 #endif
63 
64 /* TODO: IP_TCPMSS support */
65 #undef IP_TCPMSS
66 #ifdef IP_TCPMSS
67 #include <netinet/ip_tcpmss.h>
68 #endif
69 
70 #include <net/if_l2tp.h>
71 
72 int ip_l2tp_ttl = L2TP_TTL;
73 
74 static void in_l2tp_input(struct mbuf *, int, int, void *);
75 
76 static const struct encapsw in_l2tp_encapsw = {
77 	.encapsw4 = {
78 		.pr_input	= in_l2tp_input,
79 		.pr_ctlinput	= NULL,
80 	}
81 };
82 
83 static int in_l2tp_match(struct mbuf *, int, int, void *);
84 
85 int
86 in_l2tp_output(struct l2tp_variant *var, struct mbuf *m)
87 {
88 	struct l2tp_softc *sc;
89 	struct ifnet *ifp;
90 	struct sockaddr_in *sin_src = satosin(var->lv_psrc);
91 	struct sockaddr_in *sin_dst = satosin(var->lv_pdst);
92 	struct ip iphdr;	/* capsule IP header, host byte ordered */
93 	struct rtentry *rt;
94 	struct route *ro_pc;
95 	kmutex_t *lock_pc;
96 
97 	int error;
98 	uint32_t sess_id;
99 
100 	KASSERT(var != NULL);
101 	KASSERT(l2tp_heldref_variant(var));
102 	KASSERT(sin_src != NULL && sin_dst != NULL);
103 	KASSERT(sin_src->sin_family == AF_INET
104 	    && sin_dst->sin_family == AF_INET);
105 
106 	sc = var->lv_softc;
107 	ifp = &sc->l2tp_ec.ec_if;
108 	error = l2tp_check_nesting(ifp, m);
109 	if (error) {
110 		m_freem(m);
111 		goto looped;
112 	}
113 
114 	/* bidirectional configured tunnel mode */
115 	if (sin_dst->sin_addr.s_addr == INADDR_ANY) {
116 		m_freem(m);
117 		if ((ifp->if_flags & IFF_DEBUG) != 0)
118 			log(LOG_DEBUG, "%s: ENETUNREACH\n", __func__);
119 		error = ENETUNREACH;
120 		goto out;
121 	}
122 
123 #ifdef NOTYET
124 /* TODO: support ALTQ for innner frame */
125 #ifdef ALTQ
126 	ALTQ_SAVE_PAYLOAD(m, AF_ETHER);
127 #endif
128 #endif
129 
130 	memset(&iphdr, 0, sizeof(iphdr));
131 	iphdr.ip_src = sin_src->sin_addr;
132 	iphdr.ip_dst = sin_dst->sin_addr;
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 	/*
155 	 * Payload length.
156 	 *
157 	 * NOTE: payload length may be changed in ip_tcpmss(). Typical case
158 	 * is missing of TCP mss option in original TCP header.
159 	 */
160 	iphdr.ip_len += m->m_pkthdr.len;
161 	HTONS(iphdr.ip_len);
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 			error = ENOBUFS;
172 			goto out;
173 		}
174 		if (var->lv_peer_cookie_len == 4) {
175 			cookie_32 = htonl((uint32_t)var->lv_peer_cookie);
176 			memcpy(mtod(m, void *), &cookie_32, sizeof(uint32_t));
177 		} else {
178 			cookie_64 = htobe64(var->lv_peer_cookie);
179 			memcpy(mtod(m, void *), &cookie_64, 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 		error = ENOBUFS;
190 		goto out;
191 	}
192 	memcpy(mtod(m, uint32_t *), &sess_id, sizeof(uint32_t));
193 
194 	/* prepend new IP header */
195 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
196 	if (m == NULL) {
197 		error = ENOBUFS;
198 		goto out;
199 	}
200 	if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
201 		m = m_copyup(m, sizeof(struct ip), 0);
202 	} else {
203 		if (m->m_len < sizeof(struct ip))
204 			m = m_pullup(m, sizeof(struct ip));
205 	}
206 	if (m == NULL) {
207 		error = ENOBUFS;
208 		goto out;
209 	}
210 	memcpy(mtod(m, struct ip *), &iphdr, sizeof(struct ip));
211 
212 	if_tunnel_get_ro(sc->l2tp_ro_percpu, &ro_pc, &lock_pc);
213 	if ((rt = rtcache_lookup(ro_pc, var->lv_pdst)) == NULL) {
214 		if_tunnel_put_ro(sc->l2tp_ro_percpu, lock_pc);
215 		m_freem(m);
216 		error = ENETUNREACH;
217 		goto out;
218 	}
219 
220 	if (rt->rt_ifp == ifp) {
221 		rtcache_unref(rt, ro_pc);
222 		rtcache_free(ro_pc);
223 		if_tunnel_put_ro(sc->l2tp_ro_percpu, lock_pc);
224 		m_freem(m);
225 		error = ENETUNREACH;	/*XXX*/
226 		goto out;
227 	}
228 	rtcache_unref(rt, ro_pc);
229 
230 	/*
231 	 * To avoid inappropriate rewrite of checksum,
232 	 * clear csum flags.
233 	 */
234 	m->m_pkthdr.csum_flags  = 0;
235 
236 	error = ip_output(m, NULL, ro_pc, 0, NULL, NULL);
237 	if_tunnel_put_ro(sc->l2tp_ro_percpu, lock_pc);
238 	return error;
239 
240 looped:
241 	if (error)
242 		if_statinc(ifp, if_oerrors);
243 
244 out:
245 	return error;
246 }
247 
248 static void
249 in_l2tp_input(struct mbuf *m, int off, int proto, void *eparg __unused)
250 {
251 	struct ifnet *l2tpp = NULL;
252 	struct l2tp_softc *sc;
253 	uint32_t sess_id;
254 	uint32_t cookie_32;
255 	uint64_t cookie_64;
256 	struct psref psref;
257 	struct l2tp_variant *var;
258 
259 	KASSERT((m->m_flags & M_PKTHDR) != 0);
260 
261 	if (m->m_pkthdr.len < off + sizeof(uint32_t)) {
262 		m_freem(m);
263 		return;
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 		SOFTNET_LOCK_IF_NET_MPSAFE();
278 		rip_input(m, off, proto);
279 		SOFTNET_UNLOCK_IF_NET_MPSAFE();
280 		return;
281 	}
282 
283 	var = l2tp_lookup_session_ref(sess_id, &psref);
284 	if (var == NULL) {
285 		m_freem(m);
286 		ip_statinc(IP_STAT_NOL2TP);
287 		return;
288 	}
289 
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 did 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 	if (var->lv_state != L2TP_STATE_UP) {
313 		m_freem(m);
314 		goto out;
315 	}
316 
317 	m_adj(m, off + sizeof(uint32_t));
318 
319 	if (var->lv_use_cookie == L2TP_COOKIE_ON) {
320 		if (m->m_pkthdr.len < var->lv_my_cookie_len) {
321 			m_freem(m);
322 			goto out;
323 		}
324 		if (var->lv_my_cookie_len == 4) {
325 			m_copydata(m, 0, sizeof(uint32_t), (void *)&cookie_32);
326 			NTOHL(cookie_32);
327 			if (cookie_32 != var->lv_my_cookie) {
328 				m_freem(m);
329 				goto out;
330 			}
331 			m_adj(m, sizeof(uint32_t));
332 		} else {
333 			m_copydata(m, 0, sizeof(uint64_t), (void *)&cookie_64);
334 			BE64TOH(cookie_64);
335 			if (cookie_64 != var->lv_my_cookie) {
336 				m_freem(m);
337 				goto out;
338 			}
339 			m_adj(m, sizeof(uint64_t));
340 		}
341 	}
342 
343 /* TODO: IP_TCPMSS support */
344 #ifdef IP_TCPMSS
345 	m = l2tp_tcpmss_clamp(l2tpp, m);
346 	if (m == NULL)
347 		goto out;
348 #endif
349 	l2tp_input(m, l2tpp);
350 
351 out:
352 	l2tp_putref_variant(var, &psref);
353 	return;
354 }
355 
356 /*
357  * This function is used by encap4_lookup() to decide priority of the encaptab.
358  * This priority is compared to the match length between mbuf's source/destination
359  * IPv4 address pair and encaptab's one.
360  * l2tp(4) does not use address pairs to search matched encaptab, so this
361  * function must return the length bigger than or equals to IPv4 address pair to
362  * avoid wrong encaptab.
363  */
364 static int
365 in_l2tp_match(struct mbuf *m, int off, int proto, void *arg)
366 {
367 	struct l2tp_softc *sc = arg;
368 	struct l2tp_variant *var;
369 	struct psref psref;
370 	uint32_t sess_id;
371 	int rv = 0;
372 
373 	KASSERT(proto == IPPROTO_L2TP);
374 
375 	var = l2tp_getref_variant(sc, &psref);
376 	if (__predict_false(var == NULL))
377 		return rv;
378 
379 	/*
380 	 * If the packet contains no session ID it cannot match
381 	 */
382 	if (m_length(m) < off + sizeof(uint32_t)) {
383 		rv = 0;
384 		goto out;
385 	}
386 
387 	/* get L2TP session ID */
388 	m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id);
389 	NTOHL(sess_id);
390 	if (sess_id == 0) {
391 		/*
392 		 * L2TPv3 control packet received.
393 		 * userland daemon(l2tpd?) should process.
394 		 */
395 		rv = 32 * 2;
396 	} else if (sess_id == var->lv_my_sess_id)
397 		rv = 32 * 2;
398 	else
399 		rv = 0;
400 
401 out:
402 	l2tp_putref_variant(var, &psref);
403 	return rv;
404 }
405 
406 int
407 in_l2tp_attach(struct l2tp_variant *var)
408 {
409 	struct l2tp_softc *sc = var->lv_softc;
410 
411 	if (sc == NULL)
412 		return EINVAL;
413 	var->lv_encap_cookie = encap_attach_func(AF_INET, IPPROTO_L2TP,
414 	    in_l2tp_match, &in_l2tp_encapsw, sc);
415 	if (var->lv_encap_cookie == NULL)
416 		return EEXIST;
417 
418 	return 0;
419 }
420 
421 int
422 in_l2tp_detach(struct l2tp_variant *var)
423 {
424 	int error;
425 
426 	error = encap_detach(var->lv_encap_cookie);
427 	if (error == 0)
428 		var->lv_encap_cookie = NULL;
429 
430 	return error;
431 }
432