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