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