if_l2tp.c revision 1.24 1 /* $NetBSD: if_l2tp.c,v 1.24 2018/04/27 09:55:27 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 /*
30 * L2TPv3 kernel interface
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_l2tp.c,v 1.24 2018/04/27 09:55:27 knakahara Exp $");
35
36 #ifdef _KERNEL_OPT
37 #include "opt_inet.h"
38 #include "opt_net_mpsafe.h"
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/errno.h>
48 #include <sys/ioctl.h>
49 #include <sys/time.h>
50 #include <sys/syslog.h>
51 #include <sys/proc.h>
52 #include <sys/conf.h>
53 #include <sys/kauth.h>
54 #include <sys/cpu.h>
55 #include <sys/cprng.h>
56 #include <sys/intr.h>
57 #include <sys/kmem.h>
58 #include <sys/mutex.h>
59 #include <sys/atomic.h>
60 #include <sys/pserialize.h>
61 #include <sys/device.h>
62 #include <sys/module.h>
63
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/if_ether.h>
67 #include <net/if_types.h>
68 #include <net/netisr.h>
69 #include <net/route.h>
70 #include <net/bpf.h>
71 #include <net/if_vlanvar.h>
72
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_encap.h>
77 #ifdef INET
78 #include <netinet/in_var.h>
79 #include <netinet/in_l2tp.h>
80 #endif /* INET */
81 #ifdef INET6
82 #include <netinet6/in6_l2tp.h>
83 #endif
84
85 #include <net/if_l2tp.h>
86
87 #include <net/if_vlanvar.h>
88
89 /* TODO: IP_TCPMSS support */
90 #undef IP_TCPMSS
91 #ifdef IP_TCPMSS
92 #include <netinet/ip_tcpmss.h>
93 #endif
94
95 #include <net/bpf.h>
96 #include <net/net_osdep.h>
97
98 /*
99 * l2tp global variable definitions
100 */
101 LIST_HEAD(l2tp_sclist, l2tp_softc);
102 static struct {
103 struct l2tp_sclist list;
104 kmutex_t lock;
105 } l2tp_softcs __cacheline_aligned;
106
107
108 #if !defined(L2TP_ID_HASH_SIZE)
109 #define L2TP_ID_HASH_SIZE 64
110 #endif
111 static struct {
112 kmutex_t lock;
113 struct pslist_head *lists;
114 u_long mask;
115 } l2tp_hash __cacheline_aligned = {
116 .lists = NULL,
117 };
118
119 pserialize_t l2tp_psz __read_mostly;
120 struct psref_class *lv_psref_class __read_mostly;
121
122 static void l2tp_ro_init_pc(void *, void *, struct cpu_info *);
123 static void l2tp_ro_fini_pc(void *, void *, struct cpu_info *);
124
125 static int l2tp_clone_create(struct if_clone *, int);
126 static int l2tp_clone_destroy(struct ifnet *);
127
128 struct if_clone l2tp_cloner =
129 IF_CLONE_INITIALIZER("l2tp", l2tp_clone_create, l2tp_clone_destroy);
130
131 static int l2tp_output(struct ifnet *, struct mbuf *,
132 const struct sockaddr *, const struct rtentry *);
133 static void l2tpintr(struct l2tp_variant *);
134
135 static void l2tp_hash_init(void);
136 static int l2tp_hash_fini(void);
137
138 static void l2tp_start(struct ifnet *);
139 static int l2tp_transmit(struct ifnet *, struct mbuf *);
140
141 static int l2tp_set_tunnel(struct ifnet *, struct sockaddr *,
142 struct sockaddr *);
143 static void l2tp_delete_tunnel(struct ifnet *);
144
145 static int id_hash_func(uint32_t, u_long);
146
147 static void l2tp_variant_update(struct l2tp_softc *, struct l2tp_variant *);
148 static int l2tp_set_session(struct l2tp_softc *, uint32_t, uint32_t);
149 static int l2tp_clear_session(struct l2tp_softc *);
150 static int l2tp_set_cookie(struct l2tp_softc *, uint64_t, u_int, uint64_t, u_int);
151 static void l2tp_clear_cookie(struct l2tp_softc *);
152 static void l2tp_set_state(struct l2tp_softc *, int);
153 static int l2tp_encap_attach(struct l2tp_variant *);
154 static int l2tp_encap_detach(struct l2tp_variant *);
155
156 #ifndef MAX_L2TP_NEST
157 /*
158 * This macro controls the upper limitation on nesting of l2tp tunnels.
159 * Since, setting a large value to this macro with a careless configuration
160 * may introduce system crash, we don't allow any nestings by default.
161 * If you need to configure nested l2tp tunnels, you can define this macro
162 * in your kernel configuration file. However, if you do so, please be
163 * careful to configure the tunnels so that it won't make a loop.
164 */
165 /*
166 * XXX
167 * Currently, if in_l2tp_output recursively calls, it causes locking against
168 * myself of struct l2tp_ro->lr_lock. So, nested l2tp tunnels is prohibited.
169 */
170 #define MAX_L2TP_NEST 0
171 #endif
172
173 static int max_l2tp_nesting = MAX_L2TP_NEST;
174
175 /* ARGSUSED */
176 void
177 l2tpattach(int count)
178 {
179 /*
180 * Nothing to do here, initialization is handled by the
181 * module initialization code in l2tpinit() below).
182 */
183 }
184
185 static void
186 l2tpinit(void)
187 {
188
189 mutex_init(&l2tp_softcs.lock, MUTEX_DEFAULT, IPL_NONE);
190 LIST_INIT(&l2tp_softcs.list);
191
192 mutex_init(&l2tp_hash.lock, MUTEX_DEFAULT, IPL_NONE);
193 l2tp_psz = pserialize_create();
194 lv_psref_class = psref_class_create("l2tpvar", IPL_SOFTNET);
195 if_clone_attach(&l2tp_cloner);
196
197 l2tp_hash_init();
198 }
199
200 static int
201 l2tpdetach(void)
202 {
203 int error;
204
205 mutex_enter(&l2tp_softcs.lock);
206 if (!LIST_EMPTY(&l2tp_softcs.list)) {
207 mutex_exit(&l2tp_softcs.lock);
208 return EBUSY;
209 }
210 mutex_exit(&l2tp_softcs.lock);
211
212 error = l2tp_hash_fini();
213 if (error)
214 return error;
215
216 if_clone_detach(&l2tp_cloner);
217 psref_class_destroy(lv_psref_class);
218 pserialize_destroy(l2tp_psz);
219 mutex_destroy(&l2tp_hash.lock);
220
221 mutex_destroy(&l2tp_softcs.lock);
222
223 return error;
224 }
225
226 static int
227 l2tp_clone_create(struct if_clone *ifc, int unit)
228 {
229 struct l2tp_softc *sc;
230 struct l2tp_variant *var;
231 int rv;
232
233 sc = kmem_zalloc(sizeof(struct l2tp_softc), KM_SLEEP);
234 if_initname(&sc->l2tp_ec.ec_if, ifc->ifc_name, unit);
235 rv = l2tpattach0(sc);
236 if (rv != 0) {
237 kmem_free(sc, sizeof(struct l2tp_softc));
238 return rv;
239 }
240
241 var = kmem_zalloc(sizeof(struct l2tp_variant), KM_SLEEP);
242 var->lv_softc = sc;
243 var->lv_state = L2TP_STATE_DOWN;
244 var->lv_use_cookie = L2TP_COOKIE_OFF;
245 psref_target_init(&var->lv_psref, lv_psref_class);
246
247 sc->l2tp_var = var;
248 mutex_init(&sc->l2tp_lock, MUTEX_DEFAULT, IPL_NONE);
249 PSLIST_ENTRY_INIT(sc, l2tp_hash);
250
251 sc->l2tp_ro_percpu = percpu_alloc(sizeof(struct l2tp_ro));
252 percpu_foreach(sc->l2tp_ro_percpu, l2tp_ro_init_pc, NULL);
253
254 mutex_enter(&l2tp_softcs.lock);
255 LIST_INSERT_HEAD(&l2tp_softcs.list, sc, l2tp_list);
256 mutex_exit(&l2tp_softcs.lock);
257
258 return (0);
259 }
260
261 int
262 l2tpattach0(struct l2tp_softc *sc)
263 {
264 int rv;
265
266 sc->l2tp_ec.ec_if.if_addrlen = 0;
267 sc->l2tp_ec.ec_if.if_mtu = L2TP_MTU;
268 sc->l2tp_ec.ec_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST|IFF_SIMPLEX;
269 sc->l2tp_ec.ec_if.if_extflags = IFEF_NO_LINK_STATE_CHANGE;
270 #ifdef NET_MPSAFE
271 sc->l2tp_ec.ec_if.if_extflags |= IFEF_MPSAFE;
272 #endif
273 sc->l2tp_ec.ec_if.if_ioctl = l2tp_ioctl;
274 sc->l2tp_ec.ec_if.if_output = l2tp_output;
275 sc->l2tp_ec.ec_if.if_type = IFT_L2TP;
276 sc->l2tp_ec.ec_if.if_dlt = DLT_NULL;
277 sc->l2tp_ec.ec_if.if_start = l2tp_start;
278 sc->l2tp_ec.ec_if.if_transmit = l2tp_transmit;
279 sc->l2tp_ec.ec_if._if_input = ether_input;
280 IFQ_SET_READY(&sc->l2tp_ec.ec_if.if_snd);
281 /* XXX
282 * It may improve performance to use if_initialize()/if_register()
283 * so that l2tp_input() calls if_input() instead of
284 * if_percpuq_enqueue(). However, that causes recursive softnet_lock
285 * when NET_MPSAFE is not set.
286 */
287 rv = if_attach(&sc->l2tp_ec.ec_if);
288 if (rv != 0)
289 return rv;
290 if_alloc_sadl(&sc->l2tp_ec.ec_if);
291 bpf_attach(&sc->l2tp_ec.ec_if, DLT_EN10MB, sizeof(struct ether_header));
292
293 return 0;
294 }
295
296 void
297 l2tp_ro_init_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
298 {
299 struct l2tp_ro *lro = p;
300
301 lro->lr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
302 }
303
304 void
305 l2tp_ro_fini_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
306 {
307 struct l2tp_ro *lro = p;
308
309 rtcache_free(&lro->lr_ro);
310
311 mutex_obj_free(lro->lr_lock);
312 }
313
314 static int
315 l2tp_clone_destroy(struct ifnet *ifp)
316 {
317 struct l2tp_variant *var;
318 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
319 l2tp_ec.ec_if);
320
321 l2tp_clear_session(sc);
322 l2tp_delete_tunnel(&sc->l2tp_ec.ec_if);
323 /*
324 * To avoid for l2tp_transmit() to access sc->l2tp_var after free it.
325 */
326 mutex_enter(&sc->l2tp_lock);
327 var = sc->l2tp_var;
328 l2tp_variant_update(sc, NULL);
329 mutex_exit(&sc->l2tp_lock);
330
331 mutex_enter(&l2tp_softcs.lock);
332 LIST_REMOVE(sc, l2tp_list);
333 mutex_exit(&l2tp_softcs.lock);
334
335 bpf_detach(ifp);
336
337 if_detach(ifp);
338
339 percpu_foreach(sc->l2tp_ro_percpu, l2tp_ro_fini_pc, NULL);
340 percpu_free(sc->l2tp_ro_percpu, sizeof(struct l2tp_ro));
341
342 kmem_free(var, sizeof(struct l2tp_variant));
343 mutex_destroy(&sc->l2tp_lock);
344 kmem_free(sc, sizeof(struct l2tp_softc));
345
346 return 0;
347 }
348
349 static int
350 l2tp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
351 const struct rtentry *rt)
352 {
353 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
354 l2tp_ec.ec_if);
355 struct l2tp_variant *var;
356 struct psref psref;
357 int error = 0;
358
359 var = l2tp_getref_variant(sc, &psref);
360 if (var == NULL) {
361 m_freem(m);
362 return ENETDOWN;
363 }
364
365 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
366
367 m->m_flags &= ~(M_BCAST|M_MCAST);
368
369 if ((ifp->if_flags & IFF_UP) == 0) {
370 m_freem(m);
371 error = ENETDOWN;
372 goto end;
373 }
374
375 if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
376 m_freem(m);
377 error = ENETDOWN;
378 goto end;
379 }
380
381 /* XXX should we check if our outer source is legal? */
382
383 /* use DLT_NULL encapsulation here to pass inner af type */
384 M_PREPEND(m, sizeof(int), M_DONTWAIT);
385 if (!m) {
386 error = ENOBUFS;
387 goto end;
388 }
389 *mtod(m, int *) = dst->sa_family;
390
391 IFQ_ENQUEUE(&ifp->if_snd, m, error);
392 if (error)
393 goto end;
394
395 /*
396 * direct call to avoid infinite loop at l2tpintr()
397 */
398 l2tpintr(var);
399
400 error = 0;
401
402 end:
403 l2tp_putref_variant(var, &psref);
404 if (error)
405 ifp->if_oerrors++;
406
407 return error;
408 }
409
410 static void
411 l2tpintr(struct l2tp_variant *var)
412 {
413 struct l2tp_softc *sc;
414 struct ifnet *ifp;
415 struct mbuf *m;
416 int error;
417
418 KASSERT(psref_held(&var->lv_psref, lv_psref_class));
419
420 sc = var->lv_softc;
421 ifp = &sc->l2tp_ec.ec_if;
422
423 /* output processing */
424 if (var->lv_my_sess_id == 0 || var->lv_peer_sess_id == 0) {
425 IFQ_PURGE(&ifp->if_snd);
426 return;
427 }
428
429 for (;;) {
430 IFQ_DEQUEUE(&ifp->if_snd, m);
431 if (m == NULL)
432 break;
433 m->m_flags &= ~(M_BCAST|M_MCAST);
434 bpf_mtap(ifp, m);
435 switch (var->lv_psrc->sa_family) {
436 #ifdef INET
437 case AF_INET:
438 error = in_l2tp_output(var, m);
439 break;
440 #endif
441 #ifdef INET6
442 case AF_INET6:
443 error = in6_l2tp_output(var, m);
444 break;
445 #endif
446 default:
447 m_freem(m);
448 error = ENETDOWN;
449 break;
450 }
451
452 if (error)
453 ifp->if_oerrors++;
454 else {
455 ifp->if_opackets++;
456 /*
457 * obytes is incremented at ether_output() or
458 * bridge_enqueue().
459 */
460 }
461 }
462
463 }
464
465 void
466 l2tp_input(struct mbuf *m, struct ifnet *ifp)
467 {
468 vaddr_t addr;
469
470 KASSERT(ifp != NULL);
471
472 /*
473 * Currently, l2tp(4) supports only ethernet as inner protocol.
474 */
475 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
476 m_freem(m);
477 return;
478 }
479
480 /*
481 * If the head of the payload is not aligned, align it.
482 */
483 addr = mtod(m, vaddr_t);
484 if ((addr & 0x03) != 0x2) {
485 /* copy and align head of payload */
486 struct mbuf *m_head;
487 int copy_length;
488 u_int pad = roundup(sizeof(struct ether_header), 4)
489 - sizeof(struct ether_header);
490
491 #define L2TP_COPY_LENGTH 60
492
493 if (m->m_pkthdr.len < L2TP_COPY_LENGTH) {
494 copy_length = m->m_pkthdr.len;
495 } else {
496 copy_length = L2TP_COPY_LENGTH;
497 }
498
499 if (m->m_len < copy_length) {
500 m = m_pullup(m, copy_length);
501 if (m == NULL)
502 return;
503 }
504
505 MGETHDR(m_head, M_DONTWAIT, MT_HEADER);
506 if (m_head == NULL) {
507 m_freem(m);
508 return;
509 }
510 M_COPY_PKTHDR(m_head, m);
511
512 /*
513 * m_head should be:
514 * L2TP_COPY_LENGTH
515 * <- + roundup(pad, 4) - pad ->
516 * +-------+--------+-----+--------------+-------------+
517 * | m_hdr | pkthdr | ... | ether header | payload |
518 * +-------+--------+-----+--------------+-------------+
519 * ^ ^
520 * m_data 4 byte aligned
521 */
522 MH_ALIGN(m_head, L2TP_COPY_LENGTH + roundup(pad, 4));
523 m_head->m_data += pad;
524
525 memcpy(mtod(m_head, void *), mtod(m, void *), copy_length);
526 m_head->m_len = copy_length;
527 m->m_data += copy_length;
528 m->m_len -= copy_length;
529
530 /* construct chain */
531 if (m->m_len == 0) {
532 m_head->m_next = m_free(m);
533 } else {
534 /*
535 * Already copied mtag with M_COPY_PKTHDR.
536 * but don't delete mtag in case cut off M_PKTHDR flag
537 */
538 m_tag_delete_chain(m, NULL);
539 m->m_flags &= ~M_PKTHDR;
540 m_head->m_next = m;
541 }
542
543 /* override m */
544 m = m_head;
545 }
546
547 m_set_rcvif(m, ifp);
548
549 /*
550 * bpf_mtap() and ifp->if_ipackets++ is done in if_input()
551 *
552 * obytes is incremented at ether_output() or bridge_enqueue().
553 */
554 if_percpuq_enqueue(ifp->if_percpuq, m);
555 }
556
557 void
558 l2tp_start(struct ifnet *ifp)
559 {
560 struct psref psref;
561 struct l2tp_variant *var;
562 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
563 l2tp_ec.ec_if);
564
565 var = l2tp_getref_variant(sc, &psref);
566 if (var == NULL)
567 return;
568
569 if (var->lv_psrc == NULL || var->lv_pdst == NULL)
570 return;
571
572 l2tpintr(var);
573 l2tp_putref_variant(var, &psref);
574 }
575
576 int
577 l2tp_transmit(struct ifnet *ifp, struct mbuf *m)
578 {
579 int error;
580 struct psref psref;
581 struct l2tp_variant *var;
582 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
583 l2tp_ec.ec_if);
584
585 var = l2tp_getref_variant(sc, &psref);
586 if (var == NULL) {
587 m_freem(m);
588 return ENETDOWN;
589 }
590
591 if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
592 m_freem(m);
593 error = ENETDOWN;
594 goto out;
595 }
596
597 m->m_flags &= ~(M_BCAST|M_MCAST);
598 bpf_mtap(ifp, m);
599 switch (var->lv_psrc->sa_family) {
600 #ifdef INET
601 case AF_INET:
602 error = in_l2tp_output(var, m);
603 break;
604 #endif
605 #ifdef INET6
606 case AF_INET6:
607 error = in6_l2tp_output(var, m);
608 break;
609 #endif
610 default:
611 m_freem(m);
612 error = ENETDOWN;
613 break;
614 }
615
616 if (error)
617 ifp->if_oerrors++;
618 else {
619 ifp->if_opackets++;
620 /*
621 * obytes is incremented at ether_output() or bridge_enqueue().
622 */
623 }
624
625 out:
626 l2tp_putref_variant(var, &psref);
627 return error;
628 }
629
630 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
631 int
632 l2tp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
633 {
634 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
635 l2tp_ec.ec_if);
636 struct l2tp_variant *var, *var_tmp;
637 struct ifreq *ifr = data;
638 int error = 0, size;
639 struct sockaddr *dst, *src;
640 struct l2tp_req l2tpr;
641 u_long mtu;
642 int bound;
643 struct psref psref;
644
645 switch (cmd) {
646 case SIOCSIFADDR:
647 ifp->if_flags |= IFF_UP;
648 break;
649
650 case SIOCSIFDSTADDR:
651 break;
652
653 case SIOCADDMULTI:
654 case SIOCDELMULTI:
655 switch (ifr->ifr_addr.sa_family) {
656 #ifdef INET
657 case AF_INET: /* IP supports Multicast */
658 break;
659 #endif /* INET */
660 #ifdef INET6
661 case AF_INET6: /* IP6 supports Multicast */
662 break;
663 #endif /* INET6 */
664 default: /* Other protocols doesn't support Multicast */
665 error = EAFNOSUPPORT;
666 break;
667 }
668 break;
669
670 case SIOCSIFMTU:
671 mtu = ifr->ifr_mtu;
672 if (mtu < L2TP_MTU_MIN || mtu > L2TP_MTU_MAX)
673 return (EINVAL);
674 ifp->if_mtu = mtu;
675 break;
676
677 #ifdef INET
678 case SIOCSIFPHYADDR:
679 src = (struct sockaddr *)
680 &(((struct in_aliasreq *)data)->ifra_addr);
681 dst = (struct sockaddr *)
682 &(((struct in_aliasreq *)data)->ifra_dstaddr);
683 if (src->sa_family != AF_INET || dst->sa_family != AF_INET)
684 return EAFNOSUPPORT;
685 else if (src->sa_len != sizeof(struct sockaddr_in)
686 || dst->sa_len != sizeof(struct sockaddr_in))
687 return EINVAL;
688
689 error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
690 break;
691
692 #endif /* INET */
693 #ifdef INET6
694 case SIOCSIFPHYADDR_IN6:
695 src = (struct sockaddr *)
696 &(((struct in6_aliasreq *)data)->ifra_addr);
697 dst = (struct sockaddr *)
698 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
699 if (src->sa_family != AF_INET6 || dst->sa_family != AF_INET6)
700 return EAFNOSUPPORT;
701 else if (src->sa_len != sizeof(struct sockaddr_in6)
702 || dst->sa_len != sizeof(struct sockaddr_in6))
703 return EINVAL;
704
705 error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
706 break;
707
708 #endif /* INET6 */
709 case SIOCSLIFPHYADDR:
710 src = (struct sockaddr *)
711 &(((struct if_laddrreq *)data)->addr);
712 dst = (struct sockaddr *)
713 &(((struct if_laddrreq *)data)->dstaddr);
714 if (src->sa_family != dst->sa_family)
715 return EINVAL;
716 else if (src->sa_family == AF_INET
717 && src->sa_len != sizeof(struct sockaddr_in))
718 return EINVAL;
719 else if (src->sa_family == AF_INET6
720 && src->sa_len != sizeof(struct sockaddr_in6))
721 return EINVAL;
722 else if (dst->sa_family == AF_INET
723 && dst->sa_len != sizeof(struct sockaddr_in))
724 return EINVAL;
725 else if (dst->sa_family == AF_INET6
726 && dst->sa_len != sizeof(struct sockaddr_in6))
727 return EINVAL;
728
729 error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
730 break;
731
732 case SIOCDIFPHYADDR:
733 l2tp_delete_tunnel(&sc->l2tp_ec.ec_if);
734 break;
735
736 case SIOCGIFPSRCADDR:
737 #ifdef INET6
738 case SIOCGIFPSRCADDR_IN6:
739 #endif /* INET6 */
740 bound = curlwp_bind();
741 var = l2tp_getref_variant(sc, &psref);
742 if (var == NULL) {
743 curlwp_bindx(bound);
744 error = EADDRNOTAVAIL;
745 goto bad;
746 }
747 if (var->lv_psrc == NULL) {
748 l2tp_putref_variant(var, &psref);
749 curlwp_bindx(bound);
750 error = EADDRNOTAVAIL;
751 goto bad;
752 }
753 src = var->lv_psrc;
754 switch (cmd) {
755 #ifdef INET
756 case SIOCGIFPSRCADDR:
757 dst = &ifr->ifr_addr;
758 size = sizeof(ifr->ifr_addr);
759 break;
760 #endif /* INET */
761 #ifdef INET6
762 case SIOCGIFPSRCADDR_IN6:
763 dst = (struct sockaddr *)
764 &(((struct in6_ifreq *)data)->ifr_addr);
765 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
766 break;
767 #endif /* INET6 */
768 default:
769 l2tp_putref_variant(var, &psref);
770 curlwp_bindx(bound);
771 error = EADDRNOTAVAIL;
772 goto bad;
773 }
774 if (src->sa_len > size) {
775 l2tp_putref_variant(var, &psref);
776 curlwp_bindx(bound);
777 return EINVAL;
778 }
779 sockaddr_copy(dst, src->sa_len, src);
780 l2tp_putref_variant(var, &psref);
781 curlwp_bindx(bound);
782 break;
783
784 case SIOCGIFPDSTADDR:
785 #ifdef INET6
786 case SIOCGIFPDSTADDR_IN6:
787 #endif /* INET6 */
788 bound = curlwp_bind();
789 var = l2tp_getref_variant(sc, &psref);
790 if (var == NULL) {
791 curlwp_bindx(bound);
792 error = EADDRNOTAVAIL;
793 goto bad;
794 }
795 if (var->lv_pdst == NULL) {
796 l2tp_putref_variant(var, &psref);
797 curlwp_bindx(bound);
798 error = EADDRNOTAVAIL;
799 goto bad;
800 }
801 src = var->lv_pdst;
802 switch (cmd) {
803 #ifdef INET
804 case SIOCGIFPDSTADDR:
805 dst = &ifr->ifr_addr;
806 size = sizeof(ifr->ifr_addr);
807 break;
808 #endif /* INET */
809 #ifdef INET6
810 case SIOCGIFPDSTADDR_IN6:
811 dst = (struct sockaddr *)
812 &(((struct in6_ifreq *)data)->ifr_addr);
813 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
814 break;
815 #endif /* INET6 */
816 default:
817 l2tp_putref_variant(var, &psref);
818 curlwp_bindx(bound);
819 error = EADDRNOTAVAIL;
820 goto bad;
821 }
822 if (src->sa_len > size) {
823 l2tp_putref_variant(var, &psref);
824 curlwp_bindx(bound);
825 return EINVAL;
826 }
827 sockaddr_copy(dst, src->sa_len, src);
828 l2tp_putref_variant(var, &psref);
829 curlwp_bindx(bound);
830 break;
831
832 case SIOCGLIFPHYADDR:
833 bound = curlwp_bind();
834 var = l2tp_getref_variant(sc, &psref);
835 if (var == NULL) {
836 curlwp_bindx(bound);
837 error = EADDRNOTAVAIL;
838 goto bad;
839 }
840 if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
841 l2tp_putref_variant(var, &psref);
842 curlwp_bindx(bound);
843 error = EADDRNOTAVAIL;
844 goto bad;
845 }
846
847 /* copy src */
848 src = var->lv_psrc;
849 dst = (struct sockaddr *)
850 &(((struct if_laddrreq *)data)->addr);
851 size = sizeof(((struct if_laddrreq *)data)->addr);
852 if (src->sa_len > size) {
853 l2tp_putref_variant(var, &psref);
854 curlwp_bindx(bound);
855 return EINVAL;
856 }
857 sockaddr_copy(dst, src->sa_len, src);
858
859 /* copy dst */
860 src = var->lv_pdst;
861 dst = (struct sockaddr *)
862 &(((struct if_laddrreq *)data)->dstaddr);
863 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
864 if (src->sa_len > size) {
865 l2tp_putref_variant(var, &psref);
866 curlwp_bindx(bound);
867 return EINVAL;
868 }
869 sockaddr_copy(dst, src->sa_len, src);
870 l2tp_putref_variant(var, &psref);
871 curlwp_bindx(bound);
872 break;
873
874 case SIOCSL2TPSESSION:
875 if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
876 break;
877
878 /* session id must not zero */
879 if (l2tpr.my_sess_id == 0 || l2tpr.peer_sess_id == 0)
880 return EINVAL;
881
882 bound = curlwp_bind();
883 var_tmp = l2tp_lookup_session_ref(l2tpr.my_sess_id, &psref);
884 if (var_tmp != NULL) {
885 /* duplicate session id */
886 log(LOG_WARNING, "%s: duplicate session id %" PRIu32 " of %s\n",
887 sc->l2tp_ec.ec_if.if_xname, l2tpr.my_sess_id,
888 var_tmp->lv_softc->l2tp_ec.ec_if.if_xname);
889 psref_release(&psref, &var_tmp->lv_psref,
890 lv_psref_class);
891 curlwp_bindx(bound);
892 return EINVAL;
893 }
894 curlwp_bindx(bound);
895
896 error = l2tp_set_session(sc, l2tpr.my_sess_id, l2tpr.peer_sess_id);
897 break;
898 case SIOCDL2TPSESSION:
899 l2tp_clear_session(sc);
900 break;
901 case SIOCSL2TPCOOKIE:
902 if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
903 break;
904
905 error = l2tp_set_cookie(sc, l2tpr.my_cookie, l2tpr.my_cookie_len,
906 l2tpr.peer_cookie, l2tpr.peer_cookie_len);
907 break;
908 case SIOCDL2TPCOOKIE:
909 l2tp_clear_cookie(sc);
910 break;
911 case SIOCSL2TPSTATE:
912 if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
913 break;
914
915 l2tp_set_state(sc, l2tpr.state);
916 break;
917 case SIOCGL2TP:
918 /* get L2TPV3 session info */
919 memset(&l2tpr, 0, sizeof(l2tpr));
920
921 bound = curlwp_bind();
922 var = l2tp_getref_variant(sc, &psref);
923 if (var == NULL) {
924 curlwp_bindx(bound);
925 error = EADDRNOTAVAIL;
926 goto bad;
927 }
928
929 l2tpr.state = var->lv_state;
930 l2tpr.my_sess_id = var->lv_my_sess_id;
931 l2tpr.peer_sess_id = var->lv_peer_sess_id;
932 l2tpr.my_cookie = var->lv_my_cookie;
933 l2tpr.my_cookie_len = var->lv_my_cookie_len;
934 l2tpr.peer_cookie = var->lv_peer_cookie;
935 l2tpr.peer_cookie_len = var->lv_peer_cookie_len;
936 l2tp_putref_variant(var, &psref);
937 curlwp_bindx(bound);
938
939 error = copyout(&l2tpr, ifr->ifr_data, sizeof(l2tpr));
940 break;
941
942 default:
943 error = ifioctl_common(ifp, cmd, data);
944 break;
945 }
946 bad:
947 return error;
948 }
949
950 static int
951 l2tp_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
952 {
953 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
954 l2tp_ec.ec_if);
955 struct sockaddr *osrc, *odst;
956 struct sockaddr *nsrc, *ndst;
957 struct l2tp_variant *ovar, *nvar;
958 int error;
959
960 nsrc = sockaddr_dup(src, M_WAITOK);
961 ndst = sockaddr_dup(dst, M_WAITOK);
962
963 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
964
965 error = encap_lock_enter();
966 if (error)
967 goto error;
968
969 mutex_enter(&sc->l2tp_lock);
970
971 ovar = sc->l2tp_var;
972 osrc = ovar->lv_psrc;
973 odst = ovar->lv_pdst;
974 *nvar = *ovar;
975 psref_target_init(&nvar->lv_psref, lv_psref_class);
976 nvar->lv_psrc = nsrc;
977 nvar->lv_pdst = ndst;
978 error = l2tp_encap_attach(nvar);
979 if (error) {
980 mutex_exit(&sc->l2tp_lock);
981 encap_lock_exit();
982 goto error;
983 }
984 membar_producer();
985 l2tp_variant_update(sc, nvar);
986
987 mutex_exit(&sc->l2tp_lock);
988
989 (void)l2tp_encap_detach(ovar);
990 encap_lock_exit();
991
992 if (osrc)
993 sockaddr_free(osrc);
994 if (odst)
995 sockaddr_free(odst);
996 kmem_free(ovar, sizeof(*ovar));
997
998 return 0;
999
1000 error:
1001 sockaddr_free(nsrc);
1002 sockaddr_free(ndst);
1003 kmem_free(nvar, sizeof(*nvar));
1004
1005 return error;
1006 }
1007
1008 static void
1009 l2tp_delete_tunnel(struct ifnet *ifp)
1010 {
1011 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
1012 l2tp_ec.ec_if);
1013 struct sockaddr *osrc, *odst;
1014 struct l2tp_variant *ovar, *nvar;
1015 int error;
1016
1017 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1018
1019 error = encap_lock_enter();
1020 if (error) {
1021 kmem_free(nvar, sizeof(*nvar));
1022 return;
1023 }
1024 mutex_enter(&sc->l2tp_lock);
1025
1026 ovar = sc->l2tp_var;
1027 osrc = ovar->lv_psrc;
1028 odst = ovar->lv_pdst;
1029 *nvar = *ovar;
1030 psref_target_init(&nvar->lv_psref, lv_psref_class);
1031 nvar->lv_psrc = NULL;
1032 nvar->lv_pdst = NULL;
1033 membar_producer();
1034 l2tp_variant_update(sc, nvar);
1035
1036 mutex_exit(&sc->l2tp_lock);
1037
1038 (void)l2tp_encap_detach(ovar);
1039 encap_lock_exit();
1040
1041 if (osrc)
1042 sockaddr_free(osrc);
1043 if (odst)
1044 sockaddr_free(odst);
1045 kmem_free(ovar, sizeof(*ovar));
1046 }
1047
1048 static int
1049 id_hash_func(uint32_t id, u_long mask)
1050 {
1051 uint32_t hash;
1052
1053 hash = (id >> 16) ^ id;
1054 hash = (hash >> 4) ^ hash;
1055
1056 return hash & mask;
1057 }
1058
1059 static void
1060 l2tp_hash_init(void)
1061 {
1062
1063 l2tp_hash.lists = hashinit(L2TP_ID_HASH_SIZE, HASH_PSLIST, true,
1064 &l2tp_hash.mask);
1065 }
1066
1067 static int
1068 l2tp_hash_fini(void)
1069 {
1070 int i;
1071
1072 mutex_enter(&l2tp_hash.lock);
1073
1074 for (i = 0; i < l2tp_hash.mask + 1; i++) {
1075 if (PSLIST_WRITER_FIRST(&l2tp_hash.lists[i], struct l2tp_softc,
1076 l2tp_hash) != NULL) {
1077 mutex_exit(&l2tp_hash.lock);
1078 return EBUSY;
1079 }
1080 }
1081 for (i = 0; i < l2tp_hash.mask + 1; i++)
1082 PSLIST_DESTROY(&l2tp_hash.lists[i]);
1083
1084 mutex_exit(&l2tp_hash.lock);
1085
1086 hashdone(l2tp_hash.lists, HASH_PSLIST, l2tp_hash.mask);
1087
1088 return 0;
1089 }
1090
1091 static int
1092 l2tp_set_session(struct l2tp_softc *sc, uint32_t my_sess_id,
1093 uint32_t peer_sess_id)
1094 {
1095 uint32_t idx;
1096 struct l2tp_variant *nvar;
1097 struct l2tp_variant *ovar;
1098 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1099
1100 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1101
1102 mutex_enter(&sc->l2tp_lock);
1103 ovar = sc->l2tp_var;
1104 *nvar = *ovar;
1105 psref_target_init(&nvar->lv_psref, lv_psref_class);
1106 nvar->lv_my_sess_id = my_sess_id;
1107 nvar->lv_peer_sess_id = peer_sess_id;
1108 membar_producer();
1109
1110 mutex_enter(&l2tp_hash.lock);
1111 if (ovar->lv_my_sess_id > 0 && ovar->lv_peer_sess_id > 0) {
1112 PSLIST_WRITER_REMOVE(sc, l2tp_hash);
1113 pserialize_perform(l2tp_psz);
1114 }
1115 mutex_exit(&l2tp_hash.lock);
1116 PSLIST_ENTRY_DESTROY(sc, l2tp_hash);
1117
1118 l2tp_variant_update(sc, nvar);
1119 mutex_exit(&sc->l2tp_lock);
1120
1121 idx = id_hash_func(nvar->lv_my_sess_id, l2tp_hash.mask);
1122 if ((ifp->if_flags & IFF_DEBUG) != 0)
1123 log(LOG_DEBUG, "%s: add hash entry: sess_id=%" PRIu32 ", idx=%" PRIu32 "\n",
1124 sc->l2tp_ec.ec_if.if_xname, nvar->lv_my_sess_id, idx);
1125
1126 PSLIST_ENTRY_INIT(sc, l2tp_hash);
1127 mutex_enter(&l2tp_hash.lock);
1128 PSLIST_WRITER_INSERT_HEAD(&l2tp_hash.lists[idx], sc, l2tp_hash);
1129 mutex_exit(&l2tp_hash.lock);
1130
1131 kmem_free(ovar, sizeof(*ovar));
1132 return 0;
1133 }
1134
1135 static int
1136 l2tp_clear_session(struct l2tp_softc *sc)
1137 {
1138 struct l2tp_variant *nvar;
1139 struct l2tp_variant *ovar;
1140
1141 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1142
1143 mutex_enter(&sc->l2tp_lock);
1144 ovar = sc->l2tp_var;
1145 *nvar = *ovar;
1146 psref_target_init(&nvar->lv_psref, lv_psref_class);
1147 nvar->lv_my_sess_id = 0;
1148 nvar->lv_peer_sess_id = 0;
1149 membar_producer();
1150
1151 mutex_enter(&l2tp_hash.lock);
1152 if (ovar->lv_my_sess_id > 0 && ovar->lv_peer_sess_id > 0) {
1153 PSLIST_WRITER_REMOVE(sc, l2tp_hash);
1154 pserialize_perform(l2tp_psz);
1155 }
1156 mutex_exit(&l2tp_hash.lock);
1157
1158 l2tp_variant_update(sc, nvar);
1159 mutex_exit(&sc->l2tp_lock);
1160 kmem_free(ovar, sizeof(*ovar));
1161 return 0;
1162 }
1163
1164 struct l2tp_variant *
1165 l2tp_lookup_session_ref(uint32_t id, struct psref *psref)
1166 {
1167 int idx;
1168 int s;
1169 struct l2tp_softc *sc;
1170
1171 idx = id_hash_func(id, l2tp_hash.mask);
1172
1173 s = pserialize_read_enter();
1174 PSLIST_READER_FOREACH(sc, &l2tp_hash.lists[idx], struct l2tp_softc,
1175 l2tp_hash) {
1176 struct l2tp_variant *var = sc->l2tp_var;
1177 if (var == NULL)
1178 continue;
1179 if (var->lv_my_sess_id != id)
1180 continue;
1181 psref_acquire(psref, &var->lv_psref, lv_psref_class);
1182 pserialize_read_exit(s);
1183 return var;
1184 }
1185 pserialize_read_exit(s);
1186 return NULL;
1187 }
1188
1189 /*
1190 * l2tp_variant update API.
1191 *
1192 * Assumption:
1193 * reader side dereferences sc->l2tp_var in reader critical section only,
1194 * that is, all of reader sides do not reader the sc->l2tp_var after
1195 * pserialize_perform().
1196 */
1197 static void
1198 l2tp_variant_update(struct l2tp_softc *sc, struct l2tp_variant *nvar)
1199 {
1200 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1201 struct l2tp_variant *ovar = sc->l2tp_var;
1202
1203 KASSERT(mutex_owned(&sc->l2tp_lock));
1204
1205 sc->l2tp_var = nvar;
1206 pserialize_perform(l2tp_psz);
1207 psref_target_destroy(&ovar->lv_psref, lv_psref_class);
1208
1209 /*
1210 * In the manual of atomic_swap_ptr(3), there is no mention if 2nd
1211 * argument is rewrite or not. So, use sc->l2tp_var instead of nvar.
1212 */
1213 if (sc->l2tp_var != NULL) {
1214 if (sc->l2tp_var->lv_psrc != NULL
1215 && sc->l2tp_var->lv_pdst != NULL)
1216 ifp->if_flags |= IFF_RUNNING;
1217 else
1218 ifp->if_flags &= ~IFF_RUNNING;
1219 }
1220 }
1221
1222 static int
1223 l2tp_set_cookie(struct l2tp_softc *sc, uint64_t my_cookie, u_int my_cookie_len,
1224 uint64_t peer_cookie, u_int peer_cookie_len)
1225 {
1226 struct l2tp_variant *nvar;
1227
1228 if (my_cookie == 0 || peer_cookie == 0)
1229 return EINVAL;
1230
1231 if (my_cookie_len != 4 && my_cookie_len != 8
1232 && peer_cookie_len != 4 && peer_cookie_len != 8)
1233 return EINVAL;
1234
1235 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1236
1237 mutex_enter(&sc->l2tp_lock);
1238
1239 *nvar = *sc->l2tp_var;
1240 psref_target_init(&nvar->lv_psref, lv_psref_class);
1241 nvar->lv_my_cookie = my_cookie;
1242 nvar->lv_my_cookie_len = my_cookie_len;
1243 nvar->lv_peer_cookie = peer_cookie;
1244 nvar->lv_peer_cookie_len = peer_cookie_len;
1245 nvar->lv_use_cookie = L2TP_COOKIE_ON;
1246 membar_producer();
1247 l2tp_variant_update(sc, nvar);
1248
1249 mutex_exit(&sc->l2tp_lock);
1250
1251 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1252 if ((ifp->if_flags & IFF_DEBUG) != 0) {
1253 log(LOG_DEBUG,
1254 "%s: set cookie: "
1255 "local cookie_len=%u local cookie=%" PRIu64 ", "
1256 "remote cookie_len=%u remote cookie=%" PRIu64 "\n",
1257 ifp->if_xname, my_cookie_len, my_cookie,
1258 peer_cookie_len, peer_cookie);
1259 }
1260
1261 return 0;
1262 }
1263
1264 static void
1265 l2tp_clear_cookie(struct l2tp_softc *sc)
1266 {
1267 struct l2tp_variant *nvar;
1268
1269 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1270
1271 mutex_enter(&sc->l2tp_lock);
1272
1273 *nvar = *sc->l2tp_var;
1274 psref_target_init(&nvar->lv_psref, lv_psref_class);
1275 nvar->lv_my_cookie = 0;
1276 nvar->lv_my_cookie_len = 0;
1277 nvar->lv_peer_cookie = 0;
1278 nvar->lv_peer_cookie_len = 0;
1279 nvar->lv_use_cookie = L2TP_COOKIE_OFF;
1280 membar_producer();
1281 l2tp_variant_update(sc, nvar);
1282
1283 mutex_exit(&sc->l2tp_lock);
1284 }
1285
1286 static void
1287 l2tp_set_state(struct l2tp_softc *sc, int state)
1288 {
1289 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1290 struct l2tp_variant *nvar;
1291
1292 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1293
1294 mutex_enter(&sc->l2tp_lock);
1295
1296 *nvar = *sc->l2tp_var;
1297 psref_target_init(&nvar->lv_psref, lv_psref_class);
1298 nvar->lv_state = state;
1299 membar_producer();
1300 l2tp_variant_update(sc, nvar);
1301
1302 if (nvar->lv_state == L2TP_STATE_UP) {
1303 ifp->if_link_state = LINK_STATE_UP;
1304 } else {
1305 ifp->if_link_state = LINK_STATE_DOWN;
1306 }
1307
1308 mutex_exit(&sc->l2tp_lock);
1309
1310 #ifdef NOTYET
1311 vlan_linkstate_notify(ifp, ifp->if_link_state);
1312 #endif
1313 }
1314
1315 static int
1316 l2tp_encap_attach(struct l2tp_variant *var)
1317 {
1318 int error;
1319
1320 if (var == NULL || var->lv_psrc == NULL)
1321 return EINVAL;
1322
1323 switch (var->lv_psrc->sa_family) {
1324 #ifdef INET
1325 case AF_INET:
1326 error = in_l2tp_attach(var);
1327 break;
1328 #endif
1329 #ifdef INET6
1330 case AF_INET6:
1331 error = in6_l2tp_attach(var);
1332 break;
1333 #endif
1334 default:
1335 error = EINVAL;
1336 break;
1337 }
1338
1339 return error;
1340 }
1341
1342 static int
1343 l2tp_encap_detach(struct l2tp_variant *var)
1344 {
1345 int error;
1346
1347 if (var == NULL || var->lv_psrc == NULL)
1348 return EINVAL;
1349
1350 switch (var->lv_psrc->sa_family) {
1351 #ifdef INET
1352 case AF_INET:
1353 error = in_l2tp_detach(var);
1354 break;
1355 #endif
1356 #ifdef INET6
1357 case AF_INET6:
1358 error = in6_l2tp_detach(var);
1359 break;
1360 #endif
1361 default:
1362 error = EINVAL;
1363 break;
1364 }
1365
1366 return error;
1367 }
1368
1369 int
1370 l2tp_check_nesting(struct ifnet *ifp, struct mbuf *m)
1371 {
1372
1373 return if_tunnel_check_nesting(ifp, m, max_l2tp_nesting);
1374 }
1375
1376 /*
1377 * Module infrastructure
1378 */
1379 #include "if_module.h"
1380
1381 IF_MODULE(MODULE_CLASS_DRIVER, l2tp, "")
1382
1383
1384 /* TODO: IP_TCPMSS support */
1385 #ifdef IP_TCPMSS
1386 static int l2tp_need_tcpmss_clamp(struct ifnet *);
1387 #ifdef INET
1388 static struct mbuf *l2tp_tcpmss4_clamp(struct ifnet *, struct mbuf *);
1389 #endif
1390 #ifdef INET6
1391 static struct mbuf *l2tp_tcpmss6_clamp(struct ifnet *, struct mbuf *);
1392 #endif
1393
1394 struct mbuf *
1395 l2tp_tcpmss_clamp(struct ifnet *ifp, struct mbuf *m)
1396 {
1397 struct ether_header *eh;
1398 struct ether_vlan_header evh;
1399
1400 if (!l2tp_need_tcpmss_clamp(ifp)) {
1401 return m;
1402 }
1403
1404 if (m->m_pkthdr.len < sizeof(evh)) {
1405 m_freem(m);
1406 return NULL;
1407 }
1408
1409 /* save ether header */
1410 m_copydata(m, 0, sizeof(evh), (void *)&evh);
1411 eh = (struct ether_header *)&evh;
1412
1413 switch (ntohs(eh->ether_type)) {
1414 case ETHERTYPE_VLAN: /* Ether + VLAN */
1415 if (m->m_pkthdr.len <= sizeof(struct ether_vlan_header))
1416 break;
1417 m_adj(m, sizeof(struct ether_vlan_header));
1418 switch (ntohs(evh.evl_proto)) {
1419 #ifdef INET
1420 case ETHERTYPE_IP: /* Ether + VLAN + IPv4 */
1421 m = l2tp_tcpmss4_clamp(ifp, m);
1422 if (m == NULL)
1423 return NULL;
1424 break;
1425 #endif /* INET */
1426 #ifdef INET6
1427 case ETHERTYPE_IPV6: /* Ether + VLAN + IPv6 */
1428 m = l2tp_tcpmss6_clamp(ifp, m);
1429 if (m == NULL)
1430 return NULL;
1431 break;
1432 #endif /* INET6 */
1433 default:
1434 break;
1435 }
1436
1437 /* restore ether header */
1438 M_PREPEND(m, sizeof(struct ether_vlan_header),
1439 M_DONTWAIT);
1440 if (m == NULL)
1441 return NULL;
1442 *mtod(m, struct ether_vlan_header *) = evh;
1443 break;
1444
1445 #ifdef INET
1446 case ETHERTYPE_IP: /* Ether + IPv4 */
1447 if (m->m_pkthdr.len <= sizeof(struct ether_header))
1448 break;
1449 m_adj(m, sizeof(struct ether_header));
1450 m = l2tp_tcpmss4_clamp(ifp, m);
1451 if (m == NULL)
1452 return NULL;
1453 /* restore ether header */
1454 M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
1455 if (m == NULL)
1456 return NULL;
1457 *mtod(m, struct ether_header *) = *eh;
1458 break;
1459 #endif /* INET */
1460
1461 #ifdef INET6
1462 case ETHERTYPE_IPV6: /* Ether + IPv6 */
1463 if (m->m_pkthdr.len <= sizeof(struct ether_header))
1464 break;
1465 m_adj(m, sizeof(struct ether_header));
1466 m = l2tp_tcpmss6_clamp(ifp, m);
1467 if (m == NULL)
1468 return NULL;
1469 /* restore ether header */
1470 M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
1471 if (m == NULL)
1472 return NULL;
1473 *mtod(m, struct ether_header *) = *eh;
1474 break;
1475 #endif /* INET6 */
1476
1477 default:
1478 break;
1479 }
1480
1481 return m;
1482 }
1483
1484 static int
1485 l2tp_need_tcpmss_clamp(struct ifnet *ifp)
1486 {
1487 int ret = 0;
1488
1489 #ifdef INET
1490 if (ifp->if_tcpmss != 0)
1491 ret = 1;
1492 #endif
1493
1494 #ifdef INET6
1495 if (ifp->if_tcpmss6 != 0)
1496 ret = 1;
1497 #endif
1498
1499 return ret;
1500 }
1501
1502 #ifdef INET
1503 static struct mbuf *
1504 l2tp_tcpmss4_clamp(struct ifnet *ifp, struct mbuf *m)
1505 {
1506
1507 if (ifp->if_tcpmss != 0) {
1508 return ip_tcpmss(m, (ifp->if_tcpmss < 0) ?
1509 ifp->if_mtu - IP_TCPMSS_EXTLEN :
1510 ifp->if_tcpmss);
1511 }
1512 return m;
1513 }
1514 #endif /* INET */
1515
1516 #ifdef INET6
1517 static struct mbuf *
1518 l2tp_tcpmss6_clamp(struct ifnet *ifp, struct mbuf *m)
1519 {
1520 int ip6hdrlen;
1521
1522 if (ifp->if_tcpmss6 != 0 &&
1523 ip6_tcpmss_applicable(m, &ip6hdrlen)) {
1524 return ip6_tcpmss(m, ip6hdrlen,
1525 (ifp->if_tcpmss6 < 0) ?
1526 ifp->if_mtu - IP6_TCPMSS_EXTLEN :
1527 ifp->if_tcpmss6);
1528 }
1529 return m;
1530 }
1531 #endif /* INET6 */
1532
1533 #endif /* IP_TCPMSS */
1534