if_l2tp.c revision 1.40 1 /* $NetBSD: if_l2tp.c,v 1.40 2019/10/16 06:53:34 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.40 2019/10/16 06:53:34 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 kpreempt_disable();
677 softint_schedule(sc->l2tp_si);
678 kpreempt_enable();
679 l2tp_putref_variant(var, &psref);
680 }
681
682 int
683 l2tp_transmit(struct ifnet *ifp, struct mbuf *m)
684 {
685 int error;
686 struct psref psref;
687 struct l2tp_variant *var;
688 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
689 l2tp_ec.ec_if);
690
691 var = l2tp_getref_variant(sc, &psref);
692 if (var == NULL) {
693 m_freem(m);
694 return ENETDOWN;
695 }
696
697 if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
698 m_freem(m);
699 error = ENETDOWN;
700 goto out;
701 }
702
703 m->m_flags &= ~(M_BCAST|M_MCAST);
704
705 error = l2tp_tx_enqueue(var, m);
706 out:
707 l2tp_putref_variant(var, &psref);
708 return error;
709 }
710
711 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
712 int
713 l2tp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
714 {
715 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
716 l2tp_ec.ec_if);
717 struct l2tp_variant *var, *var_tmp;
718 struct ifreq *ifr = data;
719 int error = 0, size;
720 struct sockaddr *dst, *src;
721 struct l2tp_req l2tpr;
722 u_long mtu;
723 int bound;
724 struct psref psref;
725
726 switch (cmd) {
727 case SIOCSIFADDR:
728 ifp->if_flags |= IFF_UP;
729 break;
730
731 case SIOCSIFDSTADDR:
732 break;
733
734 case SIOCADDMULTI:
735 case SIOCDELMULTI:
736 switch (ifr->ifr_addr.sa_family) {
737 #ifdef INET
738 case AF_INET: /* IP supports Multicast */
739 break;
740 #endif /* INET */
741 #ifdef INET6
742 case AF_INET6: /* IP6 supports Multicast */
743 break;
744 #endif /* INET6 */
745 default: /* Other protocols doesn't support Multicast */
746 error = EAFNOSUPPORT;
747 break;
748 }
749 break;
750
751 case SIOCSIFMTU:
752 mtu = ifr->ifr_mtu;
753 if (mtu < L2TP_MTU_MIN || mtu > L2TP_MTU_MAX)
754 return (EINVAL);
755 ifp->if_mtu = mtu;
756 break;
757
758 #ifdef INET
759 case SIOCSIFPHYADDR:
760 src = (struct sockaddr *)
761 &(((struct in_aliasreq *)data)->ifra_addr);
762 dst = (struct sockaddr *)
763 &(((struct in_aliasreq *)data)->ifra_dstaddr);
764 if (src->sa_family != AF_INET || dst->sa_family != AF_INET)
765 return EAFNOSUPPORT;
766 else if (src->sa_len != sizeof(struct sockaddr_in)
767 || dst->sa_len != sizeof(struct sockaddr_in))
768 return EINVAL;
769
770 error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
771 break;
772
773 #endif /* INET */
774 #ifdef INET6
775 case SIOCSIFPHYADDR_IN6:
776 src = (struct sockaddr *)
777 &(((struct in6_aliasreq *)data)->ifra_addr);
778 dst = (struct sockaddr *)
779 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
780 if (src->sa_family != AF_INET6 || dst->sa_family != AF_INET6)
781 return EAFNOSUPPORT;
782 else if (src->sa_len != sizeof(struct sockaddr_in6)
783 || dst->sa_len != sizeof(struct sockaddr_in6))
784 return EINVAL;
785
786 error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
787 break;
788
789 #endif /* INET6 */
790 case SIOCSLIFPHYADDR:
791 src = (struct sockaddr *)
792 &(((struct if_laddrreq *)data)->addr);
793 dst = (struct sockaddr *)
794 &(((struct if_laddrreq *)data)->dstaddr);
795 if (src->sa_family != dst->sa_family)
796 return EINVAL;
797 else if (src->sa_family == AF_INET
798 && src->sa_len != sizeof(struct sockaddr_in))
799 return EINVAL;
800 else if (src->sa_family == AF_INET6
801 && src->sa_len != sizeof(struct sockaddr_in6))
802 return EINVAL;
803 else if (dst->sa_family == AF_INET
804 && dst->sa_len != sizeof(struct sockaddr_in))
805 return EINVAL;
806 else if (dst->sa_family == AF_INET6
807 && dst->sa_len != sizeof(struct sockaddr_in6))
808 return EINVAL;
809
810 error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
811 break;
812
813 case SIOCDIFPHYADDR:
814 l2tp_delete_tunnel(&sc->l2tp_ec.ec_if);
815 break;
816
817 case SIOCGIFPSRCADDR:
818 #ifdef INET6
819 case SIOCGIFPSRCADDR_IN6:
820 #endif /* INET6 */
821 bound = curlwp_bind();
822 var = l2tp_getref_variant(sc, &psref);
823 if (var == NULL) {
824 curlwp_bindx(bound);
825 error = EADDRNOTAVAIL;
826 goto bad;
827 }
828 if (var->lv_psrc == NULL) {
829 l2tp_putref_variant(var, &psref);
830 curlwp_bindx(bound);
831 error = EADDRNOTAVAIL;
832 goto bad;
833 }
834 src = var->lv_psrc;
835 switch (cmd) {
836 #ifdef INET
837 case SIOCGIFPSRCADDR:
838 dst = &ifr->ifr_addr;
839 size = sizeof(ifr->ifr_addr);
840 break;
841 #endif /* INET */
842 #ifdef INET6
843 case SIOCGIFPSRCADDR_IN6:
844 dst = (struct sockaddr *)
845 &(((struct in6_ifreq *)data)->ifr_addr);
846 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
847 break;
848 #endif /* INET6 */
849 default:
850 l2tp_putref_variant(var, &psref);
851 curlwp_bindx(bound);
852 error = EADDRNOTAVAIL;
853 goto bad;
854 }
855 if (src->sa_len > size) {
856 l2tp_putref_variant(var, &psref);
857 curlwp_bindx(bound);
858 return EINVAL;
859 }
860 sockaddr_copy(dst, src->sa_len, src);
861 l2tp_putref_variant(var, &psref);
862 curlwp_bindx(bound);
863 break;
864
865 case SIOCGIFPDSTADDR:
866 #ifdef INET6
867 case SIOCGIFPDSTADDR_IN6:
868 #endif /* INET6 */
869 bound = curlwp_bind();
870 var = l2tp_getref_variant(sc, &psref);
871 if (var == NULL) {
872 curlwp_bindx(bound);
873 error = EADDRNOTAVAIL;
874 goto bad;
875 }
876 if (var->lv_pdst == NULL) {
877 l2tp_putref_variant(var, &psref);
878 curlwp_bindx(bound);
879 error = EADDRNOTAVAIL;
880 goto bad;
881 }
882 src = var->lv_pdst;
883 switch (cmd) {
884 #ifdef INET
885 case SIOCGIFPDSTADDR:
886 dst = &ifr->ifr_addr;
887 size = sizeof(ifr->ifr_addr);
888 break;
889 #endif /* INET */
890 #ifdef INET6
891 case SIOCGIFPDSTADDR_IN6:
892 dst = (struct sockaddr *)
893 &(((struct in6_ifreq *)data)->ifr_addr);
894 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
895 break;
896 #endif /* INET6 */
897 default:
898 l2tp_putref_variant(var, &psref);
899 curlwp_bindx(bound);
900 error = EADDRNOTAVAIL;
901 goto bad;
902 }
903 if (src->sa_len > size) {
904 l2tp_putref_variant(var, &psref);
905 curlwp_bindx(bound);
906 return EINVAL;
907 }
908 sockaddr_copy(dst, src->sa_len, src);
909 l2tp_putref_variant(var, &psref);
910 curlwp_bindx(bound);
911 break;
912
913 case SIOCGLIFPHYADDR:
914 bound = curlwp_bind();
915 var = l2tp_getref_variant(sc, &psref);
916 if (var == NULL) {
917 curlwp_bindx(bound);
918 error = EADDRNOTAVAIL;
919 goto bad;
920 }
921 if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
922 l2tp_putref_variant(var, &psref);
923 curlwp_bindx(bound);
924 error = EADDRNOTAVAIL;
925 goto bad;
926 }
927
928 /* copy src */
929 src = var->lv_psrc;
930 dst = (struct sockaddr *)
931 &(((struct if_laddrreq *)data)->addr);
932 size = sizeof(((struct if_laddrreq *)data)->addr);
933 if (src->sa_len > size) {
934 l2tp_putref_variant(var, &psref);
935 curlwp_bindx(bound);
936 return EINVAL;
937 }
938 sockaddr_copy(dst, src->sa_len, src);
939
940 /* copy dst */
941 src = var->lv_pdst;
942 dst = (struct sockaddr *)
943 &(((struct if_laddrreq *)data)->dstaddr);
944 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
945 if (src->sa_len > size) {
946 l2tp_putref_variant(var, &psref);
947 curlwp_bindx(bound);
948 return EINVAL;
949 }
950 sockaddr_copy(dst, src->sa_len, src);
951 l2tp_putref_variant(var, &psref);
952 curlwp_bindx(bound);
953 break;
954
955 case SIOCSL2TPSESSION:
956 if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
957 break;
958
959 /* session id must not zero */
960 if (l2tpr.my_sess_id == 0 || l2tpr.peer_sess_id == 0)
961 return EINVAL;
962
963 bound = curlwp_bind();
964 var_tmp = l2tp_lookup_session_ref(l2tpr.my_sess_id, &psref);
965 if (var_tmp != NULL) {
966 /* duplicate session id */
967 log(LOG_WARNING, "%s: duplicate session id %" PRIu32 " of %s\n",
968 sc->l2tp_ec.ec_if.if_xname, l2tpr.my_sess_id,
969 var_tmp->lv_softc->l2tp_ec.ec_if.if_xname);
970 psref_release(&psref, &var_tmp->lv_psref,
971 lv_psref_class);
972 curlwp_bindx(bound);
973 return EINVAL;
974 }
975 curlwp_bindx(bound);
976
977 error = l2tp_set_session(sc, l2tpr.my_sess_id, l2tpr.peer_sess_id);
978 break;
979 case SIOCDL2TPSESSION:
980 l2tp_clear_session(sc);
981 break;
982 case SIOCSL2TPCOOKIE:
983 if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
984 break;
985
986 error = l2tp_set_cookie(sc, l2tpr.my_cookie, l2tpr.my_cookie_len,
987 l2tpr.peer_cookie, l2tpr.peer_cookie_len);
988 break;
989 case SIOCDL2TPCOOKIE:
990 l2tp_clear_cookie(sc);
991 break;
992 case SIOCSL2TPSTATE:
993 if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
994 break;
995
996 l2tp_set_state(sc, l2tpr.state);
997 break;
998 case SIOCGL2TP:
999 /* get L2TPV3 session info */
1000 memset(&l2tpr, 0, sizeof(l2tpr));
1001
1002 bound = curlwp_bind();
1003 var = l2tp_getref_variant(sc, &psref);
1004 if (var == NULL) {
1005 curlwp_bindx(bound);
1006 error = EADDRNOTAVAIL;
1007 goto bad;
1008 }
1009
1010 l2tpr.state = var->lv_state;
1011 l2tpr.my_sess_id = var->lv_my_sess_id;
1012 l2tpr.peer_sess_id = var->lv_peer_sess_id;
1013 l2tpr.my_cookie = var->lv_my_cookie;
1014 l2tpr.my_cookie_len = var->lv_my_cookie_len;
1015 l2tpr.peer_cookie = var->lv_peer_cookie;
1016 l2tpr.peer_cookie_len = var->lv_peer_cookie_len;
1017 l2tp_putref_variant(var, &psref);
1018 curlwp_bindx(bound);
1019
1020 error = copyout(&l2tpr, ifr->ifr_data, sizeof(l2tpr));
1021 break;
1022
1023 default:
1024 error = ifioctl_common(ifp, cmd, data);
1025 break;
1026 }
1027 bad:
1028 return error;
1029 }
1030
1031 static int
1032 l2tp_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
1033 {
1034 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
1035 l2tp_ec.ec_if);
1036 struct sockaddr *osrc, *odst;
1037 struct sockaddr *nsrc, *ndst;
1038 struct l2tp_variant *ovar, *nvar;
1039 int error;
1040
1041 nsrc = sockaddr_dup(src, M_WAITOK);
1042 ndst = sockaddr_dup(dst, M_WAITOK);
1043
1044 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1045
1046 error = encap_lock_enter();
1047 if (error)
1048 goto error;
1049
1050 mutex_enter(&sc->l2tp_lock);
1051
1052 ovar = sc->l2tp_var;
1053 osrc = ovar->lv_psrc;
1054 odst = ovar->lv_pdst;
1055 *nvar = *ovar;
1056 psref_target_init(&nvar->lv_psref, lv_psref_class);
1057 nvar->lv_psrc = nsrc;
1058 nvar->lv_pdst = ndst;
1059 error = l2tp_encap_attach(nvar);
1060 if (error) {
1061 mutex_exit(&sc->l2tp_lock);
1062 encap_lock_exit();
1063 goto error;
1064 }
1065 membar_producer();
1066 l2tp_variant_update(sc, nvar);
1067
1068 mutex_exit(&sc->l2tp_lock);
1069
1070 (void)l2tp_encap_detach(ovar);
1071 encap_lock_exit();
1072
1073 if (osrc)
1074 sockaddr_free(osrc);
1075 if (odst)
1076 sockaddr_free(odst);
1077 kmem_free(ovar, sizeof(*ovar));
1078
1079 return 0;
1080
1081 error:
1082 sockaddr_free(nsrc);
1083 sockaddr_free(ndst);
1084 kmem_free(nvar, sizeof(*nvar));
1085
1086 return error;
1087 }
1088
1089 static void
1090 l2tp_delete_tunnel(struct ifnet *ifp)
1091 {
1092 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
1093 l2tp_ec.ec_if);
1094 struct sockaddr *osrc, *odst;
1095 struct l2tp_variant *ovar, *nvar;
1096 int error;
1097
1098 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1099
1100 error = encap_lock_enter();
1101 if (error) {
1102 kmem_free(nvar, sizeof(*nvar));
1103 return;
1104 }
1105 mutex_enter(&sc->l2tp_lock);
1106
1107 ovar = sc->l2tp_var;
1108 osrc = ovar->lv_psrc;
1109 odst = ovar->lv_pdst;
1110 *nvar = *ovar;
1111 psref_target_init(&nvar->lv_psref, lv_psref_class);
1112 nvar->lv_psrc = NULL;
1113 nvar->lv_pdst = NULL;
1114 membar_producer();
1115 l2tp_variant_update(sc, nvar);
1116
1117 mutex_exit(&sc->l2tp_lock);
1118
1119 (void)l2tp_encap_detach(ovar);
1120 encap_lock_exit();
1121
1122 if (osrc)
1123 sockaddr_free(osrc);
1124 if (odst)
1125 sockaddr_free(odst);
1126 kmem_free(ovar, sizeof(*ovar));
1127 }
1128
1129 static int
1130 id_hash_func(uint32_t id, u_long mask)
1131 {
1132 uint32_t hash;
1133
1134 hash = (id >> 16) ^ id;
1135 hash = (hash >> 4) ^ hash;
1136
1137 return hash & mask;
1138 }
1139
1140 static void
1141 l2tp_hash_init(void)
1142 {
1143
1144 l2tp_hash.lists = hashinit(L2TP_ID_HASH_SIZE, HASH_PSLIST, true,
1145 &l2tp_hash.mask);
1146 }
1147
1148 static int
1149 l2tp_hash_fini(void)
1150 {
1151 int i;
1152
1153 mutex_enter(&l2tp_hash.lock);
1154
1155 for (i = 0; i < l2tp_hash.mask + 1; i++) {
1156 if (PSLIST_WRITER_FIRST(&l2tp_hash.lists[i], struct l2tp_softc,
1157 l2tp_hash) != NULL) {
1158 mutex_exit(&l2tp_hash.lock);
1159 return EBUSY;
1160 }
1161 }
1162 for (i = 0; i < l2tp_hash.mask + 1; i++)
1163 PSLIST_DESTROY(&l2tp_hash.lists[i]);
1164
1165 mutex_exit(&l2tp_hash.lock);
1166
1167 hashdone(l2tp_hash.lists, HASH_PSLIST, l2tp_hash.mask);
1168
1169 return 0;
1170 }
1171
1172 static int
1173 l2tp_set_session(struct l2tp_softc *sc, uint32_t my_sess_id,
1174 uint32_t peer_sess_id)
1175 {
1176 uint32_t idx;
1177 struct l2tp_variant *nvar;
1178 struct l2tp_variant *ovar;
1179 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1180
1181 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1182
1183 mutex_enter(&sc->l2tp_lock);
1184 ovar = sc->l2tp_var;
1185 *nvar = *ovar;
1186 psref_target_init(&nvar->lv_psref, lv_psref_class);
1187 nvar->lv_my_sess_id = my_sess_id;
1188 nvar->lv_peer_sess_id = peer_sess_id;
1189 membar_producer();
1190
1191 mutex_enter(&l2tp_hash.lock);
1192 if (ovar->lv_my_sess_id > 0 && ovar->lv_peer_sess_id > 0) {
1193 PSLIST_WRITER_REMOVE(sc, l2tp_hash);
1194 pserialize_perform(l2tp_psz);
1195 }
1196 mutex_exit(&l2tp_hash.lock);
1197 PSLIST_ENTRY_DESTROY(sc, l2tp_hash);
1198
1199 l2tp_variant_update(sc, nvar);
1200 mutex_exit(&sc->l2tp_lock);
1201
1202 idx = id_hash_func(nvar->lv_my_sess_id, l2tp_hash.mask);
1203 if ((ifp->if_flags & IFF_DEBUG) != 0)
1204 log(LOG_DEBUG, "%s: add hash entry: sess_id=%" PRIu32 ", idx=%" PRIu32 "\n",
1205 sc->l2tp_ec.ec_if.if_xname, nvar->lv_my_sess_id, idx);
1206
1207 PSLIST_ENTRY_INIT(sc, l2tp_hash);
1208 mutex_enter(&l2tp_hash.lock);
1209 PSLIST_WRITER_INSERT_HEAD(&l2tp_hash.lists[idx], sc, l2tp_hash);
1210 mutex_exit(&l2tp_hash.lock);
1211
1212 kmem_free(ovar, sizeof(*ovar));
1213 return 0;
1214 }
1215
1216 static int
1217 l2tp_clear_session(struct l2tp_softc *sc)
1218 {
1219 struct l2tp_variant *nvar;
1220 struct l2tp_variant *ovar;
1221
1222 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1223
1224 mutex_enter(&sc->l2tp_lock);
1225 ovar = sc->l2tp_var;
1226 *nvar = *ovar;
1227 psref_target_init(&nvar->lv_psref, lv_psref_class);
1228 nvar->lv_my_sess_id = 0;
1229 nvar->lv_peer_sess_id = 0;
1230 membar_producer();
1231
1232 mutex_enter(&l2tp_hash.lock);
1233 if (ovar->lv_my_sess_id > 0 && ovar->lv_peer_sess_id > 0) {
1234 PSLIST_WRITER_REMOVE(sc, l2tp_hash);
1235 pserialize_perform(l2tp_psz);
1236 }
1237 mutex_exit(&l2tp_hash.lock);
1238
1239 l2tp_variant_update(sc, nvar);
1240 mutex_exit(&sc->l2tp_lock);
1241 kmem_free(ovar, sizeof(*ovar));
1242 return 0;
1243 }
1244
1245 struct l2tp_variant *
1246 l2tp_lookup_session_ref(uint32_t id, struct psref *psref)
1247 {
1248 int idx;
1249 int s;
1250 struct l2tp_softc *sc;
1251
1252 idx = id_hash_func(id, l2tp_hash.mask);
1253
1254 s = pserialize_read_enter();
1255 PSLIST_READER_FOREACH(sc, &l2tp_hash.lists[idx], struct l2tp_softc,
1256 l2tp_hash) {
1257 struct l2tp_variant *var = sc->l2tp_var;
1258 if (var == NULL)
1259 continue;
1260 if (var->lv_my_sess_id != id)
1261 continue;
1262 psref_acquire(psref, &var->lv_psref, lv_psref_class);
1263 pserialize_read_exit(s);
1264 return var;
1265 }
1266 pserialize_read_exit(s);
1267 return NULL;
1268 }
1269
1270 /*
1271 * l2tp_variant update API.
1272 *
1273 * Assumption:
1274 * reader side dereferences sc->l2tp_var in reader critical section only,
1275 * that is, all of reader sides do not reader the sc->l2tp_var after
1276 * pserialize_perform().
1277 */
1278 static void
1279 l2tp_variant_update(struct l2tp_softc *sc, struct l2tp_variant *nvar)
1280 {
1281 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1282 struct l2tp_variant *ovar = sc->l2tp_var;
1283
1284 KASSERT(mutex_owned(&sc->l2tp_lock));
1285
1286 sc->l2tp_var = nvar;
1287 pserialize_perform(sc->l2tp_psz);
1288 psref_target_destroy(&ovar->lv_psref, lv_psref_class);
1289
1290 /*
1291 * In the manual of atomic_swap_ptr(3), there is no mention if 2nd
1292 * argument is rewrite or not. So, use sc->l2tp_var instead of nvar.
1293 */
1294 if (sc->l2tp_var != NULL) {
1295 if (sc->l2tp_var->lv_psrc != NULL
1296 && sc->l2tp_var->lv_pdst != NULL)
1297 ifp->if_flags |= IFF_RUNNING;
1298 else
1299 ifp->if_flags &= ~IFF_RUNNING;
1300 }
1301 }
1302
1303 static int
1304 l2tp_set_cookie(struct l2tp_softc *sc, uint64_t my_cookie, u_int my_cookie_len,
1305 uint64_t peer_cookie, u_int peer_cookie_len)
1306 {
1307 struct l2tp_variant *nvar;
1308
1309 if (my_cookie == 0 || peer_cookie == 0)
1310 return EINVAL;
1311
1312 if (my_cookie_len != 4 && my_cookie_len != 8
1313 && peer_cookie_len != 4 && peer_cookie_len != 8)
1314 return EINVAL;
1315
1316 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1317
1318 mutex_enter(&sc->l2tp_lock);
1319
1320 *nvar = *sc->l2tp_var;
1321 psref_target_init(&nvar->lv_psref, lv_psref_class);
1322 nvar->lv_my_cookie = my_cookie;
1323 nvar->lv_my_cookie_len = my_cookie_len;
1324 nvar->lv_peer_cookie = peer_cookie;
1325 nvar->lv_peer_cookie_len = peer_cookie_len;
1326 nvar->lv_use_cookie = L2TP_COOKIE_ON;
1327 membar_producer();
1328 l2tp_variant_update(sc, nvar);
1329
1330 mutex_exit(&sc->l2tp_lock);
1331
1332 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1333 if ((ifp->if_flags & IFF_DEBUG) != 0) {
1334 log(LOG_DEBUG,
1335 "%s: set cookie: "
1336 "local cookie_len=%u local cookie=%" PRIu64 ", "
1337 "remote cookie_len=%u remote cookie=%" PRIu64 "\n",
1338 ifp->if_xname, my_cookie_len, my_cookie,
1339 peer_cookie_len, peer_cookie);
1340 }
1341
1342 return 0;
1343 }
1344
1345 static void
1346 l2tp_clear_cookie(struct l2tp_softc *sc)
1347 {
1348 struct l2tp_variant *nvar;
1349
1350 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1351
1352 mutex_enter(&sc->l2tp_lock);
1353
1354 *nvar = *sc->l2tp_var;
1355 psref_target_init(&nvar->lv_psref, lv_psref_class);
1356 nvar->lv_my_cookie = 0;
1357 nvar->lv_my_cookie_len = 0;
1358 nvar->lv_peer_cookie = 0;
1359 nvar->lv_peer_cookie_len = 0;
1360 nvar->lv_use_cookie = L2TP_COOKIE_OFF;
1361 membar_producer();
1362 l2tp_variant_update(sc, nvar);
1363
1364 mutex_exit(&sc->l2tp_lock);
1365 }
1366
1367 static void
1368 l2tp_set_state(struct l2tp_softc *sc, int state)
1369 {
1370 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1371 struct l2tp_variant *nvar;
1372
1373 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1374
1375 mutex_enter(&sc->l2tp_lock);
1376
1377 *nvar = *sc->l2tp_var;
1378 psref_target_init(&nvar->lv_psref, lv_psref_class);
1379 nvar->lv_state = state;
1380 membar_producer();
1381 l2tp_variant_update(sc, nvar);
1382
1383 if (nvar->lv_state == L2TP_STATE_UP) {
1384 ifp->if_link_state = LINK_STATE_UP;
1385 } else {
1386 ifp->if_link_state = LINK_STATE_DOWN;
1387 }
1388
1389 mutex_exit(&sc->l2tp_lock);
1390
1391 #ifdef NOTYET
1392 vlan_linkstate_notify(ifp, ifp->if_link_state);
1393 #endif
1394 }
1395
1396 static int
1397 l2tp_encap_attach(struct l2tp_variant *var)
1398 {
1399 int error;
1400
1401 if (var == NULL || var->lv_psrc == NULL)
1402 return EINVAL;
1403
1404 switch (var->lv_psrc->sa_family) {
1405 #ifdef INET
1406 case AF_INET:
1407 error = in_l2tp_attach(var);
1408 break;
1409 #endif
1410 #ifdef INET6
1411 case AF_INET6:
1412 error = in6_l2tp_attach(var);
1413 break;
1414 #endif
1415 default:
1416 error = EINVAL;
1417 break;
1418 }
1419
1420 return error;
1421 }
1422
1423 static int
1424 l2tp_encap_detach(struct l2tp_variant *var)
1425 {
1426 int error;
1427
1428 if (var == NULL || var->lv_psrc == NULL)
1429 return EINVAL;
1430
1431 switch (var->lv_psrc->sa_family) {
1432 #ifdef INET
1433 case AF_INET:
1434 error = in_l2tp_detach(var);
1435 break;
1436 #endif
1437 #ifdef INET6
1438 case AF_INET6:
1439 error = in6_l2tp_detach(var);
1440 break;
1441 #endif
1442 default:
1443 error = EINVAL;
1444 break;
1445 }
1446
1447 return error;
1448 }
1449
1450 int
1451 l2tp_check_nesting(struct ifnet *ifp, struct mbuf *m)
1452 {
1453
1454 return if_tunnel_check_nesting(ifp, m, max_l2tp_nesting);
1455 }
1456
1457 /*
1458 * Module infrastructure
1459 */
1460 #include "if_module.h"
1461
1462 IF_MODULE(MODULE_CLASS_DRIVER, l2tp, NULL)
1463
1464
1465 /* TODO: IP_TCPMSS support */
1466 #ifdef IP_TCPMSS
1467 static int l2tp_need_tcpmss_clamp(struct ifnet *);
1468 #ifdef INET
1469 static struct mbuf *l2tp_tcpmss4_clamp(struct ifnet *, struct mbuf *);
1470 #endif
1471 #ifdef INET6
1472 static struct mbuf *l2tp_tcpmss6_clamp(struct ifnet *, struct mbuf *);
1473 #endif
1474
1475 struct mbuf *
1476 l2tp_tcpmss_clamp(struct ifnet *ifp, struct mbuf *m)
1477 {
1478 struct ether_header *eh;
1479 struct ether_vlan_header evh;
1480
1481 if (!l2tp_need_tcpmss_clamp(ifp)) {
1482 return m;
1483 }
1484
1485 if (m->m_pkthdr.len < sizeof(evh)) {
1486 m_freem(m);
1487 return NULL;
1488 }
1489
1490 /* save ether header */
1491 m_copydata(m, 0, sizeof(evh), (void *)&evh);
1492 eh = (struct ether_header *)&evh;
1493
1494 switch (ntohs(eh->ether_type)) {
1495 case ETHERTYPE_VLAN: /* Ether + VLAN */
1496 if (m->m_pkthdr.len <= sizeof(struct ether_vlan_header))
1497 break;
1498 m_adj(m, sizeof(struct ether_vlan_header));
1499 switch (ntohs(evh.evl_proto)) {
1500 #ifdef INET
1501 case ETHERTYPE_IP: /* Ether + VLAN + IPv4 */
1502 m = l2tp_tcpmss4_clamp(ifp, m);
1503 if (m == NULL)
1504 return NULL;
1505 break;
1506 #endif /* INET */
1507 #ifdef INET6
1508 case ETHERTYPE_IPV6: /* Ether + VLAN + IPv6 */
1509 m = l2tp_tcpmss6_clamp(ifp, m);
1510 if (m == NULL)
1511 return NULL;
1512 break;
1513 #endif /* INET6 */
1514 default:
1515 break;
1516 }
1517
1518 /* restore ether header */
1519 M_PREPEND(m, sizeof(struct ether_vlan_header),
1520 M_DONTWAIT);
1521 if (m == NULL)
1522 return NULL;
1523 *mtod(m, struct ether_vlan_header *) = evh;
1524 break;
1525
1526 #ifdef INET
1527 case ETHERTYPE_IP: /* Ether + IPv4 */
1528 if (m->m_pkthdr.len <= sizeof(struct ether_header))
1529 break;
1530 m_adj(m, sizeof(struct ether_header));
1531 m = l2tp_tcpmss4_clamp(ifp, m);
1532 if (m == NULL)
1533 return NULL;
1534 /* restore ether header */
1535 M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
1536 if (m == NULL)
1537 return NULL;
1538 *mtod(m, struct ether_header *) = *eh;
1539 break;
1540 #endif /* INET */
1541
1542 #ifdef INET6
1543 case ETHERTYPE_IPV6: /* Ether + IPv6 */
1544 if (m->m_pkthdr.len <= sizeof(struct ether_header))
1545 break;
1546 m_adj(m, sizeof(struct ether_header));
1547 m = l2tp_tcpmss6_clamp(ifp, m);
1548 if (m == NULL)
1549 return NULL;
1550 /* restore ether header */
1551 M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
1552 if (m == NULL)
1553 return NULL;
1554 *mtod(m, struct ether_header *) = *eh;
1555 break;
1556 #endif /* INET6 */
1557
1558 default:
1559 break;
1560 }
1561
1562 return m;
1563 }
1564
1565 static int
1566 l2tp_need_tcpmss_clamp(struct ifnet *ifp)
1567 {
1568 int ret = 0;
1569
1570 #ifdef INET
1571 if (ifp->if_tcpmss != 0)
1572 ret = 1;
1573 #endif
1574
1575 #ifdef INET6
1576 if (ifp->if_tcpmss6 != 0)
1577 ret = 1;
1578 #endif
1579
1580 return ret;
1581 }
1582
1583 #ifdef INET
1584 static struct mbuf *
1585 l2tp_tcpmss4_clamp(struct ifnet *ifp, struct mbuf *m)
1586 {
1587
1588 if (ifp->if_tcpmss != 0) {
1589 return ip_tcpmss(m, (ifp->if_tcpmss < 0) ?
1590 ifp->if_mtu - IP_TCPMSS_EXTLEN :
1591 ifp->if_tcpmss);
1592 }
1593 return m;
1594 }
1595 #endif /* INET */
1596
1597 #ifdef INET6
1598 static struct mbuf *
1599 l2tp_tcpmss6_clamp(struct ifnet *ifp, struct mbuf *m)
1600 {
1601 int ip6hdrlen;
1602
1603 if (ifp->if_tcpmss6 != 0 &&
1604 ip6_tcpmss_applicable(m, &ip6hdrlen)) {
1605 return ip6_tcpmss(m, ip6hdrlen,
1606 (ifp->if_tcpmss6 < 0) ?
1607 ifp->if_mtu - IP6_TCPMSS_EXTLEN :
1608 ifp->if_tcpmss6);
1609 }
1610 return m;
1611 }
1612 #endif /* INET6 */
1613
1614 #endif /* IP_TCPMSS */
1615