if_l2tp.c revision 1.42 1 /* $NetBSD: if_l2tp.c,v 1.42 2020/02/01 02:58:05 riastradh 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.42 2020/02/01 02:58:05 riastradh 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 if_statinc(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 if_statinc(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 if_statinc(ifp, if_oerrors);
508 } else {
509 if_statadd2(ifp, if_opackets, 1, if_obytes, len);
510 }
511 }
512
513 static void
514 l2tpintr(struct l2tp_variant *var)
515 {
516 struct l2tp_softc *sc;
517 struct ifnet *ifp;
518 struct mbuf *m;
519 struct ifqueue *ifq;
520 u_int cpuid = cpu_index(curcpu());
521
522 KASSERT(psref_held(&var->lv_psref, lv_psref_class));
523
524 sc = var->lv_softc;
525 ifp = &sc->l2tp_ec.ec_if;
526
527 /* output processing */
528 if (var->lv_my_sess_id == 0 || var->lv_peer_sess_id == 0) {
529 ifq = l2tp_ifq_percpu_getref(sc->l2tp_ifq_percpu);
530 IF_PURGE(ifq);
531 l2tp_ifq_percpu_putref(sc->l2tp_ifq_percpu);
532 if (cpuid == 0)
533 IFQ_PURGE(&ifp->if_snd);
534 return;
535 }
536
537 /* Currently, l2tpintr() is always called in softint context. */
538 ifq = l2tp_ifq_percpu_getref(sc->l2tp_ifq_percpu);
539 for (;;) {
540 IF_DEQUEUE(ifq, m);
541 if (m != NULL)
542 l2tp_sendit(var, m);
543 else
544 break;
545 }
546 l2tp_ifq_percpu_putref(sc->l2tp_ifq_percpu);
547
548 if (cpuid == 0) {
549 for (;;) {
550 IFQ_DEQUEUE(&ifp->if_snd, m);
551 if (m != NULL)
552 l2tp_sendit(var, m);
553 else
554 break;
555 }
556 }
557 }
558
559 static void
560 l2tpintr_softint(void *arg)
561 {
562 struct l2tp_variant *var;
563 struct psref psref;
564 struct l2tp_softc *sc = arg;
565
566 var = l2tp_getref_variant(sc, &psref);
567 if (var == NULL)
568 return;
569
570 l2tpintr(var);
571 l2tp_putref_variant(var, &psref);
572 }
573
574 void
575 l2tp_input(struct mbuf *m, struct ifnet *ifp)
576 {
577 vaddr_t addr;
578
579 KASSERT(ifp != NULL);
580
581 /*
582 * Currently, l2tp(4) supports only ethernet as inner protocol.
583 */
584 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
585 m_freem(m);
586 return;
587 }
588
589 /*
590 * If the head of the payload is not aligned, align it.
591 */
592 addr = mtod(m, vaddr_t);
593 if ((addr & 0x03) != 0x2) {
594 /* copy and align head of payload */
595 struct mbuf *m_head;
596 int copy_length;
597 u_int pad = roundup(sizeof(struct ether_header), 4)
598 - sizeof(struct ether_header);
599
600 #define L2TP_COPY_LENGTH 60
601
602 if (m->m_pkthdr.len < L2TP_COPY_LENGTH) {
603 copy_length = m->m_pkthdr.len;
604 } else {
605 copy_length = L2TP_COPY_LENGTH;
606 }
607
608 if (m->m_len < copy_length) {
609 m = m_pullup(m, copy_length);
610 if (m == NULL)
611 return;
612 }
613
614 MGETHDR(m_head, M_DONTWAIT, MT_HEADER);
615 if (m_head == NULL) {
616 m_freem(m);
617 return;
618 }
619 m_move_pkthdr(m_head, m);
620
621 /*
622 * m_head should be:
623 * L2TP_COPY_LENGTH
624 * <- + roundup(pad, 4) - pad ->
625 * +-------+--------+-----+--------------+-------------+
626 * | m_hdr | pkthdr | ... | ether header | payload |
627 * +-------+--------+-----+--------------+-------------+
628 * ^ ^
629 * m_data 4 byte aligned
630 */
631 m_align(m_head, L2TP_COPY_LENGTH + roundup(pad, 4));
632 m_head->m_data += pad;
633
634 memcpy(mtod(m_head, void *), mtod(m, void *), copy_length);
635 m_head->m_len = copy_length;
636 m->m_data += copy_length;
637 m->m_len -= copy_length;
638
639 /* construct chain */
640 if (m->m_len == 0) {
641 m_head->m_next = m_free(m);
642 } else {
643 m_head->m_next = m;
644 }
645
646 /* override m */
647 m = m_head;
648 }
649
650 m_set_rcvif(m, ifp);
651
652 /*
653 * bpf_mtap() and ifp->if_ipackets++ is done in if_input()
654 *
655 * obytes is incremented at ether_output() or bridge_enqueue().
656 */
657 if_percpuq_enqueue(ifp->if_percpuq, m);
658 }
659
660 void
661 l2tp_start(struct ifnet *ifp)
662 {
663 struct psref psref;
664 struct l2tp_variant *var;
665 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
666 l2tp_ec.ec_if);
667
668 var = l2tp_getref_variant(sc, &psref);
669 if (var == NULL)
670 return;
671
672 if (var->lv_psrc == NULL || var->lv_pdst == NULL)
673 return;
674
675 kpreempt_disable();
676 softint_schedule(sc->l2tp_si);
677 kpreempt_enable();
678 l2tp_putref_variant(var, &psref);
679 }
680
681 int
682 l2tp_transmit(struct ifnet *ifp, struct mbuf *m)
683 {
684 int error;
685 struct psref psref;
686 struct l2tp_variant *var;
687 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
688 l2tp_ec.ec_if);
689
690 var = l2tp_getref_variant(sc, &psref);
691 if (var == NULL) {
692 m_freem(m);
693 return ENETDOWN;
694 }
695
696 if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
697 m_freem(m);
698 error = ENETDOWN;
699 goto out;
700 }
701
702 m->m_flags &= ~(M_BCAST|M_MCAST);
703
704 error = l2tp_tx_enqueue(var, m);
705 out:
706 l2tp_putref_variant(var, &psref);
707 return error;
708 }
709
710 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
711 int
712 l2tp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
713 {
714 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
715 l2tp_ec.ec_if);
716 struct l2tp_variant *var, *var_tmp;
717 struct ifreq *ifr = data;
718 int error = 0, size;
719 struct sockaddr *dst, *src;
720 struct l2tp_req l2tpr;
721 u_long mtu;
722 int bound;
723 struct psref psref;
724
725 switch (cmd) {
726 case SIOCSIFADDR:
727 ifp->if_flags |= IFF_UP;
728 break;
729
730 case SIOCSIFDSTADDR:
731 break;
732
733 case SIOCADDMULTI:
734 case SIOCDELMULTI:
735 switch (ifr->ifr_addr.sa_family) {
736 #ifdef INET
737 case AF_INET: /* IP supports Multicast */
738 break;
739 #endif /* INET */
740 #ifdef INET6
741 case AF_INET6: /* IP6 supports Multicast */
742 break;
743 #endif /* INET6 */
744 default: /* Other protocols doesn't support Multicast */
745 error = EAFNOSUPPORT;
746 break;
747 }
748 break;
749
750 case SIOCSIFMTU:
751 mtu = ifr->ifr_mtu;
752 if (mtu < L2TP_MTU_MIN || mtu > L2TP_MTU_MAX)
753 return (EINVAL);
754 ifp->if_mtu = mtu;
755 break;
756
757 #ifdef INET
758 case SIOCSIFPHYADDR:
759 src = (struct sockaddr *)
760 &(((struct in_aliasreq *)data)->ifra_addr);
761 dst = (struct sockaddr *)
762 &(((struct in_aliasreq *)data)->ifra_dstaddr);
763 if (src->sa_family != AF_INET || dst->sa_family != AF_INET)
764 return EAFNOSUPPORT;
765 else if (src->sa_len != sizeof(struct sockaddr_in)
766 || dst->sa_len != sizeof(struct sockaddr_in))
767 return EINVAL;
768
769 error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
770 break;
771
772 #endif /* INET */
773 #ifdef INET6
774 case SIOCSIFPHYADDR_IN6:
775 src = (struct sockaddr *)
776 &(((struct in6_aliasreq *)data)->ifra_addr);
777 dst = (struct sockaddr *)
778 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
779 if (src->sa_family != AF_INET6 || dst->sa_family != AF_INET6)
780 return EAFNOSUPPORT;
781 else if (src->sa_len != sizeof(struct sockaddr_in6)
782 || dst->sa_len != sizeof(struct sockaddr_in6))
783 return EINVAL;
784
785 error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
786 break;
787
788 #endif /* INET6 */
789 case SIOCSLIFPHYADDR:
790 src = (struct sockaddr *)
791 &(((struct if_laddrreq *)data)->addr);
792 dst = (struct sockaddr *)
793 &(((struct if_laddrreq *)data)->dstaddr);
794 if (src->sa_family != dst->sa_family)
795 return EINVAL;
796 else if (src->sa_family == AF_INET
797 && src->sa_len != sizeof(struct sockaddr_in))
798 return EINVAL;
799 else if (src->sa_family == AF_INET6
800 && src->sa_len != sizeof(struct sockaddr_in6))
801 return EINVAL;
802 else if (dst->sa_family == AF_INET
803 && dst->sa_len != sizeof(struct sockaddr_in))
804 return EINVAL;
805 else if (dst->sa_family == AF_INET6
806 && dst->sa_len != sizeof(struct sockaddr_in6))
807 return EINVAL;
808
809 error = l2tp_set_tunnel(&sc->l2tp_ec.ec_if, src, dst);
810 break;
811
812 case SIOCDIFPHYADDR:
813 l2tp_delete_tunnel(&sc->l2tp_ec.ec_if);
814 break;
815
816 case SIOCGIFPSRCADDR:
817 #ifdef INET6
818 case SIOCGIFPSRCADDR_IN6:
819 #endif /* INET6 */
820 bound = curlwp_bind();
821 var = l2tp_getref_variant(sc, &psref);
822 if (var == NULL) {
823 curlwp_bindx(bound);
824 error = EADDRNOTAVAIL;
825 goto bad;
826 }
827 if (var->lv_psrc == NULL) {
828 l2tp_putref_variant(var, &psref);
829 curlwp_bindx(bound);
830 error = EADDRNOTAVAIL;
831 goto bad;
832 }
833 src = var->lv_psrc;
834 switch (cmd) {
835 #ifdef INET
836 case SIOCGIFPSRCADDR:
837 dst = &ifr->ifr_addr;
838 size = sizeof(ifr->ifr_addr);
839 break;
840 #endif /* INET */
841 #ifdef INET6
842 case SIOCGIFPSRCADDR_IN6:
843 dst = (struct sockaddr *)
844 &(((struct in6_ifreq *)data)->ifr_addr);
845 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
846 break;
847 #endif /* INET6 */
848 default:
849 l2tp_putref_variant(var, &psref);
850 curlwp_bindx(bound);
851 error = EADDRNOTAVAIL;
852 goto bad;
853 }
854 if (src->sa_len > size) {
855 l2tp_putref_variant(var, &psref);
856 curlwp_bindx(bound);
857 return EINVAL;
858 }
859 sockaddr_copy(dst, src->sa_len, src);
860 l2tp_putref_variant(var, &psref);
861 curlwp_bindx(bound);
862 break;
863
864 case SIOCGIFPDSTADDR:
865 #ifdef INET6
866 case SIOCGIFPDSTADDR_IN6:
867 #endif /* INET6 */
868 bound = curlwp_bind();
869 var = l2tp_getref_variant(sc, &psref);
870 if (var == NULL) {
871 curlwp_bindx(bound);
872 error = EADDRNOTAVAIL;
873 goto bad;
874 }
875 if (var->lv_pdst == NULL) {
876 l2tp_putref_variant(var, &psref);
877 curlwp_bindx(bound);
878 error = EADDRNOTAVAIL;
879 goto bad;
880 }
881 src = var->lv_pdst;
882 switch (cmd) {
883 #ifdef INET
884 case SIOCGIFPDSTADDR:
885 dst = &ifr->ifr_addr;
886 size = sizeof(ifr->ifr_addr);
887 break;
888 #endif /* INET */
889 #ifdef INET6
890 case SIOCGIFPDSTADDR_IN6:
891 dst = (struct sockaddr *)
892 &(((struct in6_ifreq *)data)->ifr_addr);
893 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
894 break;
895 #endif /* INET6 */
896 default:
897 l2tp_putref_variant(var, &psref);
898 curlwp_bindx(bound);
899 error = EADDRNOTAVAIL;
900 goto bad;
901 }
902 if (src->sa_len > size) {
903 l2tp_putref_variant(var, &psref);
904 curlwp_bindx(bound);
905 return EINVAL;
906 }
907 sockaddr_copy(dst, src->sa_len, src);
908 l2tp_putref_variant(var, &psref);
909 curlwp_bindx(bound);
910 break;
911
912 case SIOCGLIFPHYADDR:
913 bound = curlwp_bind();
914 var = l2tp_getref_variant(sc, &psref);
915 if (var == NULL) {
916 curlwp_bindx(bound);
917 error = EADDRNOTAVAIL;
918 goto bad;
919 }
920 if (var->lv_psrc == NULL || var->lv_pdst == NULL) {
921 l2tp_putref_variant(var, &psref);
922 curlwp_bindx(bound);
923 error = EADDRNOTAVAIL;
924 goto bad;
925 }
926
927 /* copy src */
928 src = var->lv_psrc;
929 dst = (struct sockaddr *)
930 &(((struct if_laddrreq *)data)->addr);
931 size = sizeof(((struct if_laddrreq *)data)->addr);
932 if (src->sa_len > size) {
933 l2tp_putref_variant(var, &psref);
934 curlwp_bindx(bound);
935 return EINVAL;
936 }
937 sockaddr_copy(dst, src->sa_len, src);
938
939 /* copy dst */
940 src = var->lv_pdst;
941 dst = (struct sockaddr *)
942 &(((struct if_laddrreq *)data)->dstaddr);
943 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
944 if (src->sa_len > size) {
945 l2tp_putref_variant(var, &psref);
946 curlwp_bindx(bound);
947 return EINVAL;
948 }
949 sockaddr_copy(dst, src->sa_len, src);
950 l2tp_putref_variant(var, &psref);
951 curlwp_bindx(bound);
952 break;
953
954 case SIOCSL2TPSESSION:
955 if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
956 break;
957
958 /* session id must not zero */
959 if (l2tpr.my_sess_id == 0 || l2tpr.peer_sess_id == 0)
960 return EINVAL;
961
962 bound = curlwp_bind();
963 var_tmp = l2tp_lookup_session_ref(l2tpr.my_sess_id, &psref);
964 if (var_tmp != NULL) {
965 /* duplicate session id */
966 log(LOG_WARNING, "%s: duplicate session id %" PRIu32 " of %s\n",
967 sc->l2tp_ec.ec_if.if_xname, l2tpr.my_sess_id,
968 var_tmp->lv_softc->l2tp_ec.ec_if.if_xname);
969 psref_release(&psref, &var_tmp->lv_psref,
970 lv_psref_class);
971 curlwp_bindx(bound);
972 return EINVAL;
973 }
974 curlwp_bindx(bound);
975
976 error = l2tp_set_session(sc, l2tpr.my_sess_id, l2tpr.peer_sess_id);
977 break;
978 case SIOCDL2TPSESSION:
979 l2tp_clear_session(sc);
980 break;
981 case SIOCSL2TPCOOKIE:
982 if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
983 break;
984
985 error = l2tp_set_cookie(sc, l2tpr.my_cookie, l2tpr.my_cookie_len,
986 l2tpr.peer_cookie, l2tpr.peer_cookie_len);
987 break;
988 case SIOCDL2TPCOOKIE:
989 l2tp_clear_cookie(sc);
990 break;
991 case SIOCSL2TPSTATE:
992 if ((error = copyin(ifr->ifr_data, &l2tpr, sizeof(l2tpr))) != 0)
993 break;
994
995 l2tp_set_state(sc, l2tpr.state);
996 break;
997 case SIOCGL2TP:
998 /* get L2TPV3 session info */
999 memset(&l2tpr, 0, sizeof(l2tpr));
1000
1001 bound = curlwp_bind();
1002 var = l2tp_getref_variant(sc, &psref);
1003 if (var == NULL) {
1004 curlwp_bindx(bound);
1005 error = EADDRNOTAVAIL;
1006 goto bad;
1007 }
1008
1009 l2tpr.state = var->lv_state;
1010 l2tpr.my_sess_id = var->lv_my_sess_id;
1011 l2tpr.peer_sess_id = var->lv_peer_sess_id;
1012 l2tpr.my_cookie = var->lv_my_cookie;
1013 l2tpr.my_cookie_len = var->lv_my_cookie_len;
1014 l2tpr.peer_cookie = var->lv_peer_cookie;
1015 l2tpr.peer_cookie_len = var->lv_peer_cookie_len;
1016 l2tp_putref_variant(var, &psref);
1017 curlwp_bindx(bound);
1018
1019 error = copyout(&l2tpr, ifr->ifr_data, sizeof(l2tpr));
1020 break;
1021
1022 default:
1023 error = ifioctl_common(ifp, cmd, data);
1024 break;
1025 }
1026 bad:
1027 return error;
1028 }
1029
1030 static int
1031 l2tp_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
1032 {
1033 struct l2tp_softc *sc = container_of(ifp, struct l2tp_softc,
1034 l2tp_ec.ec_if);
1035 struct sockaddr *osrc, *odst;
1036 struct sockaddr *nsrc, *ndst;
1037 struct l2tp_variant *ovar, *nvar;
1038 int error;
1039
1040 nsrc = sockaddr_dup(src, M_WAITOK);
1041 ndst = sockaddr_dup(dst, M_WAITOK);
1042
1043 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1044
1045 error = encap_lock_enter();
1046 if (error)
1047 goto error;
1048
1049 mutex_enter(&sc->l2tp_lock);
1050
1051 ovar = sc->l2tp_var;
1052 osrc = ovar->lv_psrc;
1053 odst = ovar->lv_pdst;
1054 *nvar = *ovar;
1055 psref_target_init(&nvar->lv_psref, lv_psref_class);
1056 nvar->lv_psrc = nsrc;
1057 nvar->lv_pdst = ndst;
1058 error = l2tp_encap_attach(nvar);
1059 if (error) {
1060 mutex_exit(&sc->l2tp_lock);
1061 encap_lock_exit();
1062 goto error;
1063 }
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 l2tp_variant_update(sc, nvar);
1113
1114 mutex_exit(&sc->l2tp_lock);
1115
1116 (void)l2tp_encap_detach(ovar);
1117 encap_lock_exit();
1118
1119 if (osrc)
1120 sockaddr_free(osrc);
1121 if (odst)
1122 sockaddr_free(odst);
1123 kmem_free(ovar, sizeof(*ovar));
1124 }
1125
1126 static int
1127 id_hash_func(uint32_t id, u_long mask)
1128 {
1129 uint32_t hash;
1130
1131 hash = (id >> 16) ^ id;
1132 hash = (hash >> 4) ^ hash;
1133
1134 return hash & mask;
1135 }
1136
1137 static void
1138 l2tp_hash_init(void)
1139 {
1140
1141 l2tp_hash.lists = hashinit(L2TP_ID_HASH_SIZE, HASH_PSLIST, true,
1142 &l2tp_hash.mask);
1143 }
1144
1145 static int
1146 l2tp_hash_fini(void)
1147 {
1148 int i;
1149
1150 mutex_enter(&l2tp_hash.lock);
1151
1152 for (i = 0; i < l2tp_hash.mask + 1; i++) {
1153 if (PSLIST_WRITER_FIRST(&l2tp_hash.lists[i], struct l2tp_softc,
1154 l2tp_hash) != NULL) {
1155 mutex_exit(&l2tp_hash.lock);
1156 return EBUSY;
1157 }
1158 }
1159 for (i = 0; i < l2tp_hash.mask + 1; i++)
1160 PSLIST_DESTROY(&l2tp_hash.lists[i]);
1161
1162 mutex_exit(&l2tp_hash.lock);
1163
1164 hashdone(l2tp_hash.lists, HASH_PSLIST, l2tp_hash.mask);
1165
1166 return 0;
1167 }
1168
1169 static int
1170 l2tp_set_session(struct l2tp_softc *sc, uint32_t my_sess_id,
1171 uint32_t peer_sess_id)
1172 {
1173 uint32_t idx;
1174 struct l2tp_variant *nvar;
1175 struct l2tp_variant *ovar;
1176 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1177
1178 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1179
1180 mutex_enter(&sc->l2tp_lock);
1181 ovar = sc->l2tp_var;
1182 *nvar = *ovar;
1183 psref_target_init(&nvar->lv_psref, lv_psref_class);
1184 nvar->lv_my_sess_id = my_sess_id;
1185 nvar->lv_peer_sess_id = peer_sess_id;
1186
1187 mutex_enter(&l2tp_hash.lock);
1188 if (ovar->lv_my_sess_id > 0 && ovar->lv_peer_sess_id > 0) {
1189 PSLIST_WRITER_REMOVE(sc, l2tp_hash);
1190 pserialize_perform(l2tp_psz);
1191 }
1192 mutex_exit(&l2tp_hash.lock);
1193 PSLIST_ENTRY_DESTROY(sc, l2tp_hash);
1194
1195 l2tp_variant_update(sc, nvar);
1196 mutex_exit(&sc->l2tp_lock);
1197
1198 idx = id_hash_func(nvar->lv_my_sess_id, l2tp_hash.mask);
1199 if ((ifp->if_flags & IFF_DEBUG) != 0)
1200 log(LOG_DEBUG, "%s: add hash entry: sess_id=%" PRIu32 ", idx=%" PRIu32 "\n",
1201 sc->l2tp_ec.ec_if.if_xname, nvar->lv_my_sess_id, idx);
1202
1203 PSLIST_ENTRY_INIT(sc, l2tp_hash);
1204 mutex_enter(&l2tp_hash.lock);
1205 PSLIST_WRITER_INSERT_HEAD(&l2tp_hash.lists[idx], sc, l2tp_hash);
1206 mutex_exit(&l2tp_hash.lock);
1207
1208 kmem_free(ovar, sizeof(*ovar));
1209 return 0;
1210 }
1211
1212 static int
1213 l2tp_clear_session(struct l2tp_softc *sc)
1214 {
1215 struct l2tp_variant *nvar;
1216 struct l2tp_variant *ovar;
1217
1218 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1219
1220 mutex_enter(&sc->l2tp_lock);
1221 ovar = sc->l2tp_var;
1222 *nvar = *ovar;
1223 psref_target_init(&nvar->lv_psref, lv_psref_class);
1224 nvar->lv_my_sess_id = 0;
1225 nvar->lv_peer_sess_id = 0;
1226
1227 mutex_enter(&l2tp_hash.lock);
1228 if (ovar->lv_my_sess_id > 0 && ovar->lv_peer_sess_id > 0) {
1229 PSLIST_WRITER_REMOVE(sc, l2tp_hash);
1230 pserialize_perform(l2tp_psz);
1231 }
1232 mutex_exit(&l2tp_hash.lock);
1233
1234 l2tp_variant_update(sc, nvar);
1235 mutex_exit(&sc->l2tp_lock);
1236 kmem_free(ovar, sizeof(*ovar));
1237 return 0;
1238 }
1239
1240 struct l2tp_variant *
1241 l2tp_lookup_session_ref(uint32_t id, struct psref *psref)
1242 {
1243 int idx;
1244 int s;
1245 struct l2tp_softc *sc;
1246
1247 idx = id_hash_func(id, l2tp_hash.mask);
1248
1249 s = pserialize_read_enter();
1250 PSLIST_READER_FOREACH(sc, &l2tp_hash.lists[idx], struct l2tp_softc,
1251 l2tp_hash) {
1252 struct l2tp_variant *var = atomic_load_consume(&sc->l2tp_var);
1253 if (var == NULL)
1254 continue;
1255 if (var->lv_my_sess_id != id)
1256 continue;
1257 psref_acquire(psref, &var->lv_psref, lv_psref_class);
1258 pserialize_read_exit(s);
1259 return var;
1260 }
1261 pserialize_read_exit(s);
1262 return NULL;
1263 }
1264
1265 /*
1266 * l2tp_variant update API.
1267 *
1268 * Assumption:
1269 * reader side dereferences sc->l2tp_var in reader critical section only,
1270 * that is, all of reader sides do not reader the sc->l2tp_var after
1271 * pserialize_perform().
1272 */
1273 static void
1274 l2tp_variant_update(struct l2tp_softc *sc, struct l2tp_variant *nvar)
1275 {
1276 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1277 struct l2tp_variant *ovar = sc->l2tp_var;
1278
1279 KASSERT(mutex_owned(&sc->l2tp_lock));
1280
1281 atomic_store_release(&sc->l2tp_var, nvar);
1282 pserialize_perform(sc->l2tp_psz);
1283 psref_target_destroy(&ovar->lv_psref, lv_psref_class);
1284
1285 if (nvar != NULL) {
1286 if (nvar->lv_psrc != NULL && nvar->lv_pdst != NULL)
1287 ifp->if_flags |= IFF_RUNNING;
1288 else
1289 ifp->if_flags &= ~IFF_RUNNING;
1290 }
1291 }
1292
1293 static int
1294 l2tp_set_cookie(struct l2tp_softc *sc, uint64_t my_cookie, u_int my_cookie_len,
1295 uint64_t peer_cookie, u_int peer_cookie_len)
1296 {
1297 struct l2tp_variant *nvar;
1298
1299 if (my_cookie == 0 || peer_cookie == 0)
1300 return EINVAL;
1301
1302 if (my_cookie_len != 4 && my_cookie_len != 8
1303 && peer_cookie_len != 4 && peer_cookie_len != 8)
1304 return EINVAL;
1305
1306 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1307
1308 mutex_enter(&sc->l2tp_lock);
1309
1310 *nvar = *sc->l2tp_var;
1311 psref_target_init(&nvar->lv_psref, lv_psref_class);
1312 nvar->lv_my_cookie = my_cookie;
1313 nvar->lv_my_cookie_len = my_cookie_len;
1314 nvar->lv_peer_cookie = peer_cookie;
1315 nvar->lv_peer_cookie_len = peer_cookie_len;
1316 nvar->lv_use_cookie = L2TP_COOKIE_ON;
1317 l2tp_variant_update(sc, nvar);
1318
1319 mutex_exit(&sc->l2tp_lock);
1320
1321 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1322 if ((ifp->if_flags & IFF_DEBUG) != 0) {
1323 log(LOG_DEBUG,
1324 "%s: set cookie: "
1325 "local cookie_len=%u local cookie=%" PRIu64 ", "
1326 "remote cookie_len=%u remote cookie=%" PRIu64 "\n",
1327 ifp->if_xname, my_cookie_len, my_cookie,
1328 peer_cookie_len, peer_cookie);
1329 }
1330
1331 return 0;
1332 }
1333
1334 static void
1335 l2tp_clear_cookie(struct l2tp_softc *sc)
1336 {
1337 struct l2tp_variant *nvar;
1338
1339 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1340
1341 mutex_enter(&sc->l2tp_lock);
1342
1343 *nvar = *sc->l2tp_var;
1344 psref_target_init(&nvar->lv_psref, lv_psref_class);
1345 nvar->lv_my_cookie = 0;
1346 nvar->lv_my_cookie_len = 0;
1347 nvar->lv_peer_cookie = 0;
1348 nvar->lv_peer_cookie_len = 0;
1349 nvar->lv_use_cookie = L2TP_COOKIE_OFF;
1350 l2tp_variant_update(sc, nvar);
1351
1352 mutex_exit(&sc->l2tp_lock);
1353 }
1354
1355 static void
1356 l2tp_set_state(struct l2tp_softc *sc, int state)
1357 {
1358 struct ifnet *ifp = &sc->l2tp_ec.ec_if;
1359 struct l2tp_variant *nvar;
1360
1361 nvar = kmem_alloc(sizeof(*nvar), KM_SLEEP);
1362
1363 mutex_enter(&sc->l2tp_lock);
1364
1365 *nvar = *sc->l2tp_var;
1366 psref_target_init(&nvar->lv_psref, lv_psref_class);
1367 nvar->lv_state = state;
1368 l2tp_variant_update(sc, nvar);
1369
1370 if (nvar->lv_state == L2TP_STATE_UP) {
1371 ifp->if_link_state = LINK_STATE_UP;
1372 } else {
1373 ifp->if_link_state = LINK_STATE_DOWN;
1374 }
1375
1376 mutex_exit(&sc->l2tp_lock);
1377
1378 #ifdef NOTYET
1379 vlan_linkstate_notify(ifp, ifp->if_link_state);
1380 #endif
1381 }
1382
1383 static int
1384 l2tp_encap_attach(struct l2tp_variant *var)
1385 {
1386 int error;
1387
1388 if (var == NULL || var->lv_psrc == NULL)
1389 return EINVAL;
1390
1391 switch (var->lv_psrc->sa_family) {
1392 #ifdef INET
1393 case AF_INET:
1394 error = in_l2tp_attach(var);
1395 break;
1396 #endif
1397 #ifdef INET6
1398 case AF_INET6:
1399 error = in6_l2tp_attach(var);
1400 break;
1401 #endif
1402 default:
1403 error = EINVAL;
1404 break;
1405 }
1406
1407 return error;
1408 }
1409
1410 static int
1411 l2tp_encap_detach(struct l2tp_variant *var)
1412 {
1413 int error;
1414
1415 if (var == NULL || var->lv_psrc == NULL)
1416 return EINVAL;
1417
1418 switch (var->lv_psrc->sa_family) {
1419 #ifdef INET
1420 case AF_INET:
1421 error = in_l2tp_detach(var);
1422 break;
1423 #endif
1424 #ifdef INET6
1425 case AF_INET6:
1426 error = in6_l2tp_detach(var);
1427 break;
1428 #endif
1429 default:
1430 error = EINVAL;
1431 break;
1432 }
1433
1434 return error;
1435 }
1436
1437 int
1438 l2tp_check_nesting(struct ifnet *ifp, struct mbuf *m)
1439 {
1440
1441 return if_tunnel_check_nesting(ifp, m, max_l2tp_nesting);
1442 }
1443
1444 /*
1445 * Module infrastructure
1446 */
1447 #include "if_module.h"
1448
1449 IF_MODULE(MODULE_CLASS_DRIVER, l2tp, NULL)
1450
1451
1452 /* TODO: IP_TCPMSS support */
1453 #ifdef IP_TCPMSS
1454 static int l2tp_need_tcpmss_clamp(struct ifnet *);
1455 #ifdef INET
1456 static struct mbuf *l2tp_tcpmss4_clamp(struct ifnet *, struct mbuf *);
1457 #endif
1458 #ifdef INET6
1459 static struct mbuf *l2tp_tcpmss6_clamp(struct ifnet *, struct mbuf *);
1460 #endif
1461
1462 struct mbuf *
1463 l2tp_tcpmss_clamp(struct ifnet *ifp, struct mbuf *m)
1464 {
1465 struct ether_header *eh;
1466 struct ether_vlan_header evh;
1467
1468 if (!l2tp_need_tcpmss_clamp(ifp)) {
1469 return m;
1470 }
1471
1472 if (m->m_pkthdr.len < sizeof(evh)) {
1473 m_freem(m);
1474 return NULL;
1475 }
1476
1477 /* save ether header */
1478 m_copydata(m, 0, sizeof(evh), (void *)&evh);
1479 eh = (struct ether_header *)&evh;
1480
1481 switch (ntohs(eh->ether_type)) {
1482 case ETHERTYPE_VLAN: /* Ether + VLAN */
1483 if (m->m_pkthdr.len <= sizeof(struct ether_vlan_header))
1484 break;
1485 m_adj(m, sizeof(struct ether_vlan_header));
1486 switch (ntohs(evh.evl_proto)) {
1487 #ifdef INET
1488 case ETHERTYPE_IP: /* Ether + VLAN + IPv4 */
1489 m = l2tp_tcpmss4_clamp(ifp, m);
1490 if (m == NULL)
1491 return NULL;
1492 break;
1493 #endif /* INET */
1494 #ifdef INET6
1495 case ETHERTYPE_IPV6: /* Ether + VLAN + IPv6 */
1496 m = l2tp_tcpmss6_clamp(ifp, m);
1497 if (m == NULL)
1498 return NULL;
1499 break;
1500 #endif /* INET6 */
1501 default:
1502 break;
1503 }
1504
1505 /* restore ether header */
1506 M_PREPEND(m, sizeof(struct ether_vlan_header),
1507 M_DONTWAIT);
1508 if (m == NULL)
1509 return NULL;
1510 *mtod(m, struct ether_vlan_header *) = evh;
1511 break;
1512
1513 #ifdef INET
1514 case ETHERTYPE_IP: /* Ether + IPv4 */
1515 if (m->m_pkthdr.len <= sizeof(struct ether_header))
1516 break;
1517 m_adj(m, sizeof(struct ether_header));
1518 m = l2tp_tcpmss4_clamp(ifp, m);
1519 if (m == NULL)
1520 return NULL;
1521 /* restore ether header */
1522 M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
1523 if (m == NULL)
1524 return NULL;
1525 *mtod(m, struct ether_header *) = *eh;
1526 break;
1527 #endif /* INET */
1528
1529 #ifdef INET6
1530 case ETHERTYPE_IPV6: /* Ether + IPv6 */
1531 if (m->m_pkthdr.len <= sizeof(struct ether_header))
1532 break;
1533 m_adj(m, sizeof(struct ether_header));
1534 m = l2tp_tcpmss6_clamp(ifp, m);
1535 if (m == NULL)
1536 return NULL;
1537 /* restore ether header */
1538 M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
1539 if (m == NULL)
1540 return NULL;
1541 *mtod(m, struct ether_header *) = *eh;
1542 break;
1543 #endif /* INET6 */
1544
1545 default:
1546 break;
1547 }
1548
1549 return m;
1550 }
1551
1552 static int
1553 l2tp_need_tcpmss_clamp(struct ifnet *ifp)
1554 {
1555 int ret = 0;
1556
1557 #ifdef INET
1558 if (ifp->if_tcpmss != 0)
1559 ret = 1;
1560 #endif
1561
1562 #ifdef INET6
1563 if (ifp->if_tcpmss6 != 0)
1564 ret = 1;
1565 #endif
1566
1567 return ret;
1568 }
1569
1570 #ifdef INET
1571 static struct mbuf *
1572 l2tp_tcpmss4_clamp(struct ifnet *ifp, struct mbuf *m)
1573 {
1574
1575 if (ifp->if_tcpmss != 0) {
1576 return ip_tcpmss(m, (ifp->if_tcpmss < 0) ?
1577 ifp->if_mtu - IP_TCPMSS_EXTLEN :
1578 ifp->if_tcpmss);
1579 }
1580 return m;
1581 }
1582 #endif /* INET */
1583
1584 #ifdef INET6
1585 static struct mbuf *
1586 l2tp_tcpmss6_clamp(struct ifnet *ifp, struct mbuf *m)
1587 {
1588 int ip6hdrlen;
1589
1590 if (ifp->if_tcpmss6 != 0 &&
1591 ip6_tcpmss_applicable(m, &ip6hdrlen)) {
1592 return ip6_tcpmss(m, ip6hdrlen,
1593 (ifp->if_tcpmss6 < 0) ?
1594 ifp->if_mtu - IP6_TCPMSS_EXTLEN :
1595 ifp->if_tcpmss6);
1596 }
1597 return m;
1598 }
1599 #endif /* INET6 */
1600
1601 #endif /* IP_TCPMSS */
1602