in6_l2tp.c revision 1.5.4.2 1 /* $NetBSD: in6_l2tp.c,v 1.5.4.2 2017/04/21 16:54:06 bouyer 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.5.4.2 2017/04/21 16:54:06 bouyer 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);
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 goto looped;
113
114 #ifdef NOTYET
115 /* TODO: support ALTQ for innner frame */
116 #ifdef ALTQ
117 ALTQ_SAVE_PAYLOAD(m, AF_ETHER);
118 #endif
119 #endif
120
121 memset(&ip6hdr, 0, sizeof(ip6hdr));
122 ip6hdr.ip6_src = sin6_src->sin6_addr;
123 /* bidirectional configured tunnel mode */
124 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
125 ip6hdr.ip6_dst = sin6_dst->sin6_addr;
126 else {
127 m_freem(m);
128 if ((ifp->if_flags & IFF_DEBUG) != 0)
129 log(LOG_DEBUG, "%s: ENETUNREACH\n", __func__);
130 return ENETUNREACH;
131 }
132 /* unlike IPv4, IP version must be filled by caller of ip6_output() */
133 ip6hdr.ip6_vfc = 0x60;
134 ip6hdr.ip6_nxt = IPPROTO_L2TP;
135 ip6hdr.ip6_hlim = ip6_l2tp_hlim;
136 /* outer IP payload length */
137 ip6hdr.ip6_plen = 0;
138 /* session-id length */
139 ip6hdr.ip6_plen += sizeof(uint32_t);
140 if (var->lv_use_cookie == L2TP_COOKIE_ON) {
141 /* cookie length */
142 ip6hdr.ip6_plen += 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 return EINVAL;
150 #endif
151
152 /*
153 * payload length
154 * NOTE: Payload length may be changed in ip_tcpmss().
155 * Typical case is missing of TCP mss option in original
156 * TCP header.
157 */
158 ip6hdr.ip6_plen += m->m_pkthdr.len;
159 HTONS(ip6hdr.ip6_plen);
160
161 if (var->lv_use_cookie == L2TP_COOKIE_ON) {
162 /* prepend session cookie */
163 uint32_t cookie_32;
164 uint64_t cookie_64;
165 M_PREPEND(m, var->lv_peer_cookie_len, M_DONTWAIT);
166 if (m && m->m_len < var->lv_peer_cookie_len)
167 m = m_pullup(m, var->lv_peer_cookie_len);
168 if (m == NULL)
169 return ENOBUFS;
170 if (var->lv_peer_cookie_len == 4) {
171 cookie_32 = htonl((uint32_t)var->lv_peer_cookie);
172 memcpy(mtod(m, void *), &cookie_32,
173 sizeof(uint32_t));
174 } else {
175 cookie_64 = htobe64(var->lv_peer_cookie);
176 memcpy(mtod(m, void *), &cookie_64,
177 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 (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
193 if (m)
194 m = m_copyup(m, sizeof(struct ip), 0);
195 } else {
196 if (m && m->m_len < sizeof(struct ip6_hdr))
197 m = m_pullup(m, sizeof(struct ip6_hdr));
198 }
199 if (m == NULL)
200 return ENOBUFS;
201 memcpy(mtod(m, struct ip6_hdr *), &ip6hdr, sizeof(struct ip6_hdr));
202
203 lro = percpu_getref(sc->l2tp_ro_percpu);
204 mutex_enter(&lro->lr_lock);
205 if ((rt = rtcache_lookup(&lro->lr_ro, var->lv_pdst)) == NULL) {
206 mutex_exit(&lro->lr_lock);
207 percpu_putref(sc->l2tp_ro_percpu);
208 m_freem(m);
209 return ENETUNREACH;
210 }
211
212 /* If the route constitutes infinite encapsulation, punt. */
213 if (rt->rt_ifp == ifp) {
214 rtcache_unref(rt, &lro->lr_ro);
215 rtcache_free(&lro->lr_ro);
216 mutex_exit(&lro->lr_lock);
217 percpu_putref(sc->l2tp_ro_percpu);
218 m_freem(m);
219 return ENETUNREACH; /* XXX */
220 }
221 rtcache_unref(rt, &lro->lr_ro);
222
223 /*
224 * To avoid inappropriate rewrite of checksum,
225 * clear csum flags.
226 */
227 m->m_pkthdr.csum_flags = 0;
228
229 error = ip6_output(m, 0, &lro->lr_ro, 0, NULL, NULL, NULL);
230 mutex_exit(&lro->lr_lock);
231 percpu_putref(sc->l2tp_ro_percpu);
232 return(error);
233
234 looped:
235 if (error)
236 ifp->if_oerrors++;
237
238 return error;
239 }
240
241 static int
242 in6_l2tp_input(struct mbuf **mp, int *offp, int proto)
243 {
244 struct mbuf *m = *mp;
245 int off = *offp;
246
247 struct ifnet *l2tpp = NULL;
248 struct l2tp_softc *sc;
249 struct l2tp_variant *var;
250 uint32_t sess_id;
251 uint32_t cookie_32;
252 uint64_t cookie_64;
253 struct psref psref;
254
255 if (m->m_len < off + sizeof(uint32_t)) {
256 m = m_pullup(m, off + sizeof(uint32_t));
257 if (!m) {
258 /* if payload length < 4 octets */
259 return IPPROTO_DONE;
260 }
261 *mp = m;
262 }
263
264 /* get L2TP session ID */
265 m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id);
266 NTOHL(sess_id);
267 #ifdef L2TP_DEBUG
268 log(LOG_DEBUG, "%s: sess_id = %" PRIu32 "\n", __func__, sess_id);
269 #endif
270 if (sess_id == 0) {
271 /*
272 * L2TPv3 control packet received.
273 * userland daemon(l2tpd?) should process.
274 */
275 return rip6_input(mp, offp, proto);
276 }
277
278 var = l2tp_lookup_session_ref(sess_id, &psref);
279 if (var == NULL) {
280 m_freem(m);
281 IP_STATINC(IP_STAT_NOL2TP);
282 return IPPROTO_DONE;
283 } else {
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 /* other CPU do l2tp_delete_tunnel */
299 if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
300 m_freem(m);
301 ip_statinc(IP_STAT_NOL2TP);
302 goto out;
303 }
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 (var->lv_my_cookie_len == 4) {
314 m_copydata(m, 0, sizeof(uint32_t), (void *)&cookie_32);
315 NTOHL(cookie_32);
316 if (cookie_32 != var->lv_my_cookie) {
317 m_freem(m);
318 goto out;
319 }
320 m_adj(m, sizeof(uint32_t));
321 } else {
322 m_copydata(m, 0, sizeof(uint64_t), (void *)&cookie_64);
323 BE64TOH(cookie_64);
324 if (cookie_64 != var->lv_my_cookie) {
325 m_freem(m);
326 goto out;
327 }
328 m_adj(m, sizeof(uint64_t));
329 }
330 }
331
332 /* TODO: IP_TCPMSS support */
333 #ifdef IP_TCPMSS
334 m = l2tp_tcpmss_clamp(l2tpp, m);
335 if (m == NULL)
336 goto out;
337 #endif
338 l2tp_input(m, l2tpp);
339
340 out:
341 l2tp_putref_variant(var, &psref);
342 return IPPROTO_DONE;
343 }
344
345 /*
346 * This function is used by encap6_lookup() to decide priority of the encaptab.
347 * This priority is compared to the match length between mbuf's source/destination
348 * IPv6 address pair and encaptab's one.
349 * l2tp(4) does not use address pairs to search matched encaptab, so this
350 * function must return the length bigger than or equals to IPv6 address pair to
351 * avoid wrong encaptab.
352 */
353 static int
354 in6_l2tp_match(struct mbuf *m, int off, int proto, void *arg)
355 {
356 struct l2tp_variant *var = arg;
357 uint32_t sess_id;
358
359 KASSERT(proto == IPPROTO_L2TP);
360
361 if (m->m_len < off + sizeof(uint32_t)) {
362 m = m_pullup(m, off + sizeof(uint32_t));
363 if (!m) {
364 /* if payload length < 4 octets */
365 return 0;
366 }
367 }
368
369 /* get L2TP session ID */
370 m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id);
371 NTOHL(sess_id);
372 if (sess_id == 0) {
373 /*
374 * L2TPv3 control packet received.
375 * userland daemon(l2tpd?) should process.
376 */
377 return 128 * 2;
378 } else if (sess_id == var->lv_my_sess_id)
379 return 128 * 2;
380 else
381 return 0;
382 }
383
384 int
385 in6_l2tp_attach(struct l2tp_variant *var)
386 {
387
388 var->lv_encap_cookie = encap_attach_func(AF_INET6, IPPROTO_L2TP,
389 in6_l2tp_match, &in6_l2tp_encapsw, var);
390 if (var->lv_encap_cookie == NULL)
391 return EEXIST;
392
393 return 0;
394 }
395
396 int
397 in6_l2tp_detach(struct l2tp_variant *var)
398 {
399 int error;
400
401 error = encap_detach(var->lv_encap_cookie);
402 if (error == 0)
403 var->lv_encap_cookie = NULL;
404
405 return error;
406 }
407