xref: /netbsd-src/sys/netinet6/in6_l2tp.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /*	$NetBSD: in6_l2tp.c,v 1.15 2018/04/27 09:55:28 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.15 2018/04/27 09:55:28 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 /* TODO: IP_TCPMSS support */
68 #undef IP_TCPMSS
69 #ifdef IP_TCPMSS
70 #include <netinet/ip_tcpmss.h>
71 #endif
72 
73 #include <net/if_l2tp.h>
74 
75 #define L2TP_HLIM6		64
76 int ip6_l2tp_hlim = L2TP_HLIM6;
77 
78 static int in6_l2tp_input(struct mbuf **, int *, int, void *);
79 
80 static const struct encapsw in6_l2tp_encapsw = {
81 	.encapsw6 = {
82 		.pr_input	= in6_l2tp_input,
83 		.pr_ctlinput	= NULL,
84 	}
85 };
86 
87 static int in6_l2tp_match(struct mbuf *, int, int, void *);
88 
89 int
90 in6_l2tp_output(struct l2tp_variant *var, struct mbuf *m)
91 {
92 	struct rtentry *rt;
93 	struct l2tp_ro *lro;
94 	struct l2tp_softc *sc;
95 	struct ifnet *ifp;
96 	struct sockaddr_in6 *sin6_src = satosin6(var->lv_psrc);
97 	struct sockaddr_in6 *sin6_dst = satosin6(var->lv_pdst);
98 	struct ip6_hdr ip6hdr;	/* capsule IP header, host byte ordered */
99 	int error;
100 	uint32_t sess_id;
101 
102 	KASSERT(var != NULL);
103 	KASSERT(l2tp_heldref_variant(var));
104 	KASSERT(sin6_src != NULL && sin6_dst != NULL);
105 	KASSERT(sin6_src->sin6_family == AF_INET6
106 	    && sin6_dst->sin6_family == AF_INET6);
107 
108 	sc = var->lv_softc;
109 	ifp = &sc->l2tp_ec.ec_if;
110 	error = l2tp_check_nesting(ifp, m);
111 	if (error) {
112 		m_freem(m);
113 		goto looped;
114 	}
115 
116 	/* bidirectional configured tunnel mode */
117 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) {
118 		m_freem(m);
119 		if ((ifp->if_flags & IFF_DEBUG) != 0)
120 			log(LOG_DEBUG, "%s: ENETUNREACH\n", __func__);
121 		return ENETUNREACH;
122 	}
123 
124 #ifdef NOTYET
125 /* TODO: support ALTQ for innner frame */
126 #ifdef ALTQ
127 	ALTQ_SAVE_PAYLOAD(m, AF_ETHER);
128 #endif
129 #endif
130 
131 	memset(&ip6hdr, 0, sizeof(ip6hdr));
132 	ip6hdr.ip6_src = sin6_src->sin6_addr;
133 	ip6hdr.ip6_dst = sin6_dst->sin6_addr;
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 	 *
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 	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, sizeof(uint32_t));
175 		} else {
176 			cookie_64 = htobe64(var->lv_peer_cookie);
177 			memcpy(mtod(m, void *), &cookie_64, sizeof(uint64_t));
178 		}
179 	}
180 
181 	/* prepend session-ID */
182 	sess_id = htonl(var->lv_peer_sess_id);
183 	M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
184 	if (m && m->m_len < sizeof(uint32_t))
185 		m = m_pullup(m, sizeof(uint32_t));
186 	if (m == NULL)
187 		return ENOBUFS;
188 	memcpy(mtod(m, uint32_t *), &sess_id, sizeof(uint32_t));
189 
190 	/* prepend new IP header */
191 	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
192 	if (m == NULL)
193 		return ENOBUFS;
194 	if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
195 		m = m_copyup(m, sizeof(struct ip), 0);
196 	} else {
197 		if (m->m_len < sizeof(struct ip6_hdr))
198 			m = m_pullup(m, sizeof(struct ip6_hdr));
199 	}
200 	if (m == NULL)
201 		return ENOBUFS;
202 	memcpy(mtod(m, struct ip6_hdr *), &ip6hdr, sizeof(struct ip6_hdr));
203 
204 	lro = percpu_getref(sc->l2tp_ro_percpu);
205 	mutex_enter(lro->lr_lock);
206 	if ((rt = rtcache_lookup(&lro->lr_ro, var->lv_pdst)) == NULL) {
207 		mutex_exit(lro->lr_lock);
208 		percpu_putref(sc->l2tp_ro_percpu);
209 		m_freem(m);
210 		return ENETUNREACH;
211 	}
212 
213 	/* If the route constitutes infinite encapsulation, punt. */
214 	if (rt->rt_ifp == ifp) {
215 		rtcache_unref(rt, &lro->lr_ro);
216 		rtcache_free(&lro->lr_ro);
217 		mutex_exit(lro->lr_lock);
218 		percpu_putref(sc->l2tp_ro_percpu);
219 		m_freem(m);
220 		return ENETUNREACH;	/* XXX */
221 	}
222 	rtcache_unref(rt, &lro->lr_ro);
223 
224 	/*
225 	 * To avoid inappropriate rewrite of checksum,
226 	 * clear csum flags.
227 	 */
228 	m->m_pkthdr.csum_flags  = 0;
229 
230 	error = ip6_output(m, 0, &lro->lr_ro, 0, NULL, NULL, NULL);
231 	mutex_exit(lro->lr_lock);
232 	percpu_putref(sc->l2tp_ro_percpu);
233 	return(error);
234 
235 looped:
236 	if (error)
237 		ifp->if_oerrors++;
238 
239 	return error;
240 }
241 
242 static int
243 in6_l2tp_input(struct mbuf **mp, int *offp, int proto, void *eparg __unused)
244 {
245 	struct mbuf *m = *mp;
246 	int off = *offp;
247 
248 	struct ifnet *l2tpp = NULL;
249 	struct l2tp_softc *sc;
250 	struct l2tp_variant *var;
251 	uint32_t sess_id;
252 	uint32_t cookie_32;
253 	uint64_t cookie_64;
254 	struct psref psref;
255 
256 	KASSERT((m->m_flags & M_PKTHDR) != 0);
257 
258 	if (m->m_pkthdr.len < off + sizeof(uint32_t)) {
259 		m_freem(m);
260 		return IPPROTO_DONE;
261 	}
262 
263 	/* get L2TP session ID */
264 	m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id);
265 	NTOHL(sess_id);
266 #ifdef L2TP_DEBUG
267 	log(LOG_DEBUG, "%s: sess_id = %" PRIu32 "\n", __func__, sess_id);
268 #endif
269 	if (sess_id == 0) {
270 		/*
271 		 * L2TPv3 control packet received.
272 		 * userland daemon(l2tpd?) should process.
273 		 */
274 		return rip6_input(mp, offp, proto);
275 	}
276 
277 	var = l2tp_lookup_session_ref(sess_id, &psref);
278 	if (var == NULL) {
279 		m_freem(m);
280 		IP_STATINC(IP_STAT_NOL2TP);
281 		return IPPROTO_DONE;
282 	}
283 
284 	sc = var->lv_softc;
285 	l2tpp = &(sc->l2tp_ec.ec_if);
286 
287 	if (l2tpp == NULL || (l2tpp->if_flags & IFF_UP) == 0) {
288 #ifdef L2TP_DEBUG
289 		if (l2tpp == NULL)
290 			log(LOG_DEBUG, "%s: l2tpp is NULL\n", __func__);
291 		else
292 			log(LOG_DEBUG, "%s: l2tpp is down\n", __func__);
293 #endif
294 		m_freem(m);
295 		IP_STATINC(IP_STAT_NOL2TP);
296 		goto out;
297 	}
298 
299 	/* other CPU did l2tp_delete_tunnel */
300 	if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
301 		m_freem(m);
302 		ip_statinc(IP_STAT_NOL2TP);
303 		goto out;
304 	}
305 
306 	if (var->lv_state != L2TP_STATE_UP) {
307 		m_freem(m);
308 		goto out;
309 	}
310 	m_adj(m, off + sizeof(uint32_t));
311 
312 	if (var->lv_use_cookie == L2TP_COOKIE_ON) {
313 		if (m->m_pkthdr.len < var->lv_my_cookie_len) {
314 			m_freem(m);
315 			goto out;
316 		}
317 		if (var->lv_my_cookie_len == 4) {
318 			m_copydata(m, 0, sizeof(uint32_t), (void *)&cookie_32);
319 			NTOHL(cookie_32);
320 			if (cookie_32 != var->lv_my_cookie) {
321 				m_freem(m);
322 				goto out;
323 			}
324 			m_adj(m, sizeof(uint32_t));
325 		} else {
326 			m_copydata(m, 0, sizeof(uint64_t), (void *)&cookie_64);
327 			BE64TOH(cookie_64);
328 			if (cookie_64 != var->lv_my_cookie) {
329 				m_freem(m);
330 				goto out;
331 			}
332 			m_adj(m, sizeof(uint64_t));
333 		}
334 	}
335 
336 /* TODO: IP_TCPMSS support */
337 #ifdef IP_TCPMSS
338 	m = l2tp_tcpmss_clamp(l2tpp, m);
339 	if (m == NULL)
340 		goto out;
341 #endif
342 	l2tp_input(m, l2tpp);
343 
344 out:
345 	l2tp_putref_variant(var, &psref);
346 	return IPPROTO_DONE;
347 }
348 
349 /*
350  * This function is used by encap6_lookup() to decide priority of the encaptab.
351  * This priority is compared to the match length between mbuf's source/destination
352  * IPv6 address pair and encaptab's one.
353  * l2tp(4) does not use address pairs to search matched encaptab, so this
354  * function must return the length bigger than or equals to IPv6 address pair to
355  * avoid wrong encaptab.
356  */
357 static int
358 in6_l2tp_match(struct mbuf *m, int off, int proto, void *arg)
359 {
360 	struct l2tp_variant *var = arg;
361 	uint32_t sess_id;
362 
363 	KASSERT(proto == IPPROTO_L2TP);
364 
365 	/*
366 	 * If the packet contains no session ID it cannot match
367 	 */
368 	if (m_length(m) < off + sizeof(uint32_t))
369 		return 0;
370 
371 	/* get L2TP session ID */
372 	m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id);
373 	NTOHL(sess_id);
374 	if (sess_id == 0) {
375 		/*
376 		 * L2TPv3 control packet received.
377 		 * userland daemon(l2tpd?) should process.
378 		 */
379 		return 128 * 2;
380 	} else if (sess_id == var->lv_my_sess_id)
381 		return 128 * 2;
382 	else
383 		return 0;
384 }
385 
386 int
387 in6_l2tp_attach(struct l2tp_variant *var)
388 {
389 
390 	var->lv_encap_cookie = encap_attach_func(AF_INET6, IPPROTO_L2TP,
391 	    in6_l2tp_match, &in6_l2tp_encapsw, var);
392 	if (var->lv_encap_cookie == NULL)
393 		return EEXIST;
394 
395 	return 0;
396 }
397 
398 int
399 in6_l2tp_detach(struct l2tp_variant *var)
400 {
401 	int error;
402 
403 	error = encap_detach(var->lv_encap_cookie);
404 	if (error == 0)
405 		var->lv_encap_cookie = NULL;
406 
407 	return error;
408 }
409