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