in6_l2tp.c revision 1.17 1 /* $NetBSD: in6_l2tp.c,v 1.17 2018/09/03 02:33:31 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.17 2018/09/03 02:33:31 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 int rv;
271 /*
272 * L2TPv3 control packet received.
273 * userland daemon(l2tpd?) should process.
274 */
275 SOFTNET_LOCK_IF_NET_MPSAFE();
276 rv = rip6_input(mp, offp, proto);
277 SOFTNET_UNLOCK_IF_NET_MPSAFE();
278 return rv;
279 }
280
281 var = l2tp_lookup_session_ref(sess_id, &psref);
282 if (var == NULL) {
283 m_freem(m);
284 IP_STATINC(IP_STAT_NOL2TP);
285 return IPPROTO_DONE;
286 }
287
288 sc = var->lv_softc;
289 l2tpp = &(sc->l2tp_ec.ec_if);
290
291 if (l2tpp == NULL || (l2tpp->if_flags & IFF_UP) == 0) {
292 #ifdef L2TP_DEBUG
293 if (l2tpp == NULL)
294 log(LOG_DEBUG, "%s: l2tpp is NULL\n", __func__);
295 else
296 log(LOG_DEBUG, "%s: l2tpp is down\n", __func__);
297 #endif
298 m_freem(m);
299 IP_STATINC(IP_STAT_NOL2TP);
300 goto out;
301 }
302
303 /* other CPU did l2tp_delete_tunnel */
304 if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
305 m_freem(m);
306 ip_statinc(IP_STAT_NOL2TP);
307 goto out;
308 }
309
310 if (var->lv_state != L2TP_STATE_UP) {
311 m_freem(m);
312 goto out;
313 }
314 m_adj(m, off + sizeof(uint32_t));
315
316 if (var->lv_use_cookie == L2TP_COOKIE_ON) {
317 if (m->m_pkthdr.len < var->lv_my_cookie_len) {
318 m_freem(m);
319 goto out;
320 }
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_softc *sc = arg;
365 struct l2tp_variant *var;
366 struct psref psref;
367 uint32_t sess_id;
368 int rv = 0;
369
370 KASSERT(proto == IPPROTO_L2TP);
371
372 var = l2tp_getref_variant(sc, &psref);
373 if (__predict_false(var == NULL))
374 return rv;
375
376 /*
377 * If the packet contains no session ID it cannot match
378 */
379 if (m_length(m) < off + sizeof(uint32_t)) {
380 rv = 0 ;
381 goto out;
382 }
383
384 /* get L2TP session ID */
385 m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id);
386 NTOHL(sess_id);
387 if (sess_id == 0) {
388 /*
389 * L2TPv3 control packet received.
390 * userland daemon(l2tpd?) should process.
391 */
392 rv = 128 * 2;
393 } else if (sess_id == var->lv_my_sess_id)
394 rv = 128 * 2;
395 else
396 rv = 0;
397
398 out:
399 l2tp_putref_variant(var, &psref);
400 return rv;
401 }
402
403 int
404 in6_l2tp_attach(struct l2tp_variant *var)
405 {
406 struct l2tp_softc *sc = var->lv_softc;
407
408 if (sc == NULL)
409 return EINVAL;
410 var->lv_encap_cookie = encap_attach_func(AF_INET6, IPPROTO_L2TP,
411 in6_l2tp_match, &in6_l2tp_encapsw, sc);
412 if (var->lv_encap_cookie == NULL)
413 return EEXIST;
414
415 return 0;
416 }
417
418 int
419 in6_l2tp_detach(struct l2tp_variant *var)
420 {
421 int error;
422
423 error = encap_detach(var->lv_encap_cookie);
424 if (error == 0)
425 var->lv_encap_cookie = NULL;
426
427 return error;
428 }
429