1 /* $NetBSD: if_vlan.c,v 1.136 2019/05/15 02:59:18 ozaki-r Exp $ */ 2 3 /* 4 * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran, and by Jason R. Thorpe of Zembu Labs, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright 1998 Massachusetts Institute of Technology 34 * 35 * Permission to use, copy, modify, and distribute this software and 36 * its documentation for any purpose and without fee is hereby 37 * granted, provided that both the above copyright notice and this 38 * permission notice appear in all copies, that both the above 39 * copyright notice and this permission notice appear in all 40 * supporting documentation, and that the name of M.I.T. not be used 41 * in advertising or publicity pertaining to distribution of the 42 * software without specific, written prior permission. M.I.T. makes 43 * no representations about the suitability of this software for any 44 * purpose. It is provided "as is" without express or implied 45 * warranty. 46 * 47 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 48 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 49 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 50 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 51 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 53 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 54 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 55 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 56 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 57 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp 61 * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp 62 */ 63 64 /* 65 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. Might be 66 * extended some day to also handle IEEE 802.1P priority tagging. This is 67 * sort of sneaky in the implementation, since we need to pretend to be 68 * enough of an Ethernet implementation to make ARP work. The way we do 69 * this is by telling everyone that we are an Ethernet interface, and then 70 * catch the packets that ether_output() left on our output queue when it 71 * calls if_start(), rewrite them for use by the real outgoing interface, 72 * and ask it to send them. 73 * 74 * TODO: 75 * 76 * - Need some way to notify vlan interfaces when the parent 77 * interface changes MTU. 78 */ 79 80 #include <sys/cdefs.h> 81 __KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.136 2019/05/15 02:59:18 ozaki-r Exp $"); 82 83 #ifdef _KERNEL_OPT 84 #include "opt_inet.h" 85 #include "opt_net_mpsafe.h" 86 #endif 87 88 #include <sys/param.h> 89 #include <sys/systm.h> 90 #include <sys/kernel.h> 91 #include <sys/mbuf.h> 92 #include <sys/queue.h> 93 #include <sys/socket.h> 94 #include <sys/sockio.h> 95 #include <sys/systm.h> 96 #include <sys/proc.h> 97 #include <sys/kauth.h> 98 #include <sys/mutex.h> 99 #include <sys/kmem.h> 100 #include <sys/cpu.h> 101 #include <sys/pserialize.h> 102 #include <sys/psref.h> 103 #include <sys/pslist.h> 104 #include <sys/atomic.h> 105 #include <sys/device.h> 106 #include <sys/module.h> 107 108 #include <net/bpf.h> 109 #include <net/if.h> 110 #include <net/if_dl.h> 111 #include <net/if_types.h> 112 #include <net/if_ether.h> 113 #include <net/if_vlanvar.h> 114 115 #ifdef INET 116 #include <netinet/in.h> 117 #include <netinet/if_inarp.h> 118 #endif 119 #ifdef INET6 120 #include <netinet6/in6_ifattach.h> 121 #include <netinet6/in6_var.h> 122 #endif 123 124 #include "ioconf.h" 125 126 struct vlan_mc_entry { 127 LIST_ENTRY(vlan_mc_entry) mc_entries; 128 /* 129 * A key to identify this entry. The mc_addr below can't be 130 * used since multiple sockaddr may mapped into the same 131 * ether_multi (e.g., AF_UNSPEC). 132 */ 133 union { 134 struct ether_multi *mcu_enm; 135 } mc_u; 136 struct sockaddr_storage mc_addr; 137 }; 138 139 #define mc_enm mc_u.mcu_enm 140 141 142 struct ifvlan_linkmib { 143 struct ifvlan *ifvm_ifvlan; 144 const struct vlan_multisw *ifvm_msw; 145 int ifvm_encaplen; /* encapsulation length */ 146 int ifvm_mtufudge; /* MTU fudged by this much */ 147 int ifvm_mintu; /* min transmission unit */ 148 uint16_t ifvm_proto; /* encapsulation ethertype */ 149 uint16_t ifvm_tag; /* tag to apply on packets */ 150 struct ifnet *ifvm_p; /* parent interface of this vlan */ 151 152 struct psref_target ifvm_psref; 153 }; 154 155 struct ifvlan { 156 union { 157 struct ethercom ifvu_ec; 158 } ifv_u; 159 struct ifvlan_linkmib *ifv_mib; /* 160 * reader must use vlan_getref_linkmib() 161 * instead of direct dereference 162 */ 163 kmutex_t ifv_lock; /* writer lock for ifv_mib */ 164 pserialize_t ifv_psz; 165 166 LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead; 167 LIST_ENTRY(ifvlan) ifv_list; 168 struct pslist_entry ifv_hash; 169 int ifv_flags; 170 }; 171 172 #define IFVF_PROMISC 0x01 /* promiscuous mode enabled */ 173 174 #define ifv_ec ifv_u.ifvu_ec 175 176 #define ifv_if ifv_ec.ec_if 177 178 #define ifv_msw ifv_mib.ifvm_msw 179 #define ifv_encaplen ifv_mib.ifvm_encaplen 180 #define ifv_mtufudge ifv_mib.ifvm_mtufudge 181 #define ifv_mintu ifv_mib.ifvm_mintu 182 #define ifv_tag ifv_mib.ifvm_tag 183 184 struct vlan_multisw { 185 int (*vmsw_addmulti)(struct ifvlan *, struct ifreq *); 186 int (*vmsw_delmulti)(struct ifvlan *, struct ifreq *); 187 void (*vmsw_purgemulti)(struct ifvlan *); 188 }; 189 190 static int vlan_ether_addmulti(struct ifvlan *, struct ifreq *); 191 static int vlan_ether_delmulti(struct ifvlan *, struct ifreq *); 192 static void vlan_ether_purgemulti(struct ifvlan *); 193 194 const struct vlan_multisw vlan_ether_multisw = { 195 .vmsw_addmulti = vlan_ether_addmulti, 196 .vmsw_delmulti = vlan_ether_delmulti, 197 .vmsw_purgemulti = vlan_ether_purgemulti, 198 }; 199 200 static int vlan_clone_create(struct if_clone *, int); 201 static int vlan_clone_destroy(struct ifnet *); 202 static int vlan_config(struct ifvlan *, struct ifnet *, 203 uint16_t); 204 static int vlan_ioctl(struct ifnet *, u_long, void *); 205 static void vlan_start(struct ifnet *); 206 static int vlan_transmit(struct ifnet *, struct mbuf *); 207 static void vlan_unconfig(struct ifnet *); 208 static int vlan_unconfig_locked(struct ifvlan *, 209 struct ifvlan_linkmib *); 210 static void vlan_hash_init(void); 211 static int vlan_hash_fini(void); 212 static int vlan_tag_hash(uint16_t, u_long); 213 static struct ifvlan_linkmib* vlan_getref_linkmib(struct ifvlan *, 214 struct psref *); 215 static void vlan_putref_linkmib(struct ifvlan_linkmib *, 216 struct psref *); 217 static void vlan_linkmib_update(struct ifvlan *, 218 struct ifvlan_linkmib *); 219 static struct ifvlan_linkmib* vlan_lookup_tag_psref(struct ifnet *, 220 uint16_t, struct psref *); 221 222 LIST_HEAD(vlan_ifvlist, ifvlan); 223 static struct { 224 kmutex_t lock; 225 struct vlan_ifvlist list; 226 } ifv_list __cacheline_aligned; 227 228 229 #if !defined(VLAN_TAG_HASH_SIZE) 230 #define VLAN_TAG_HASH_SIZE 32 231 #endif 232 static struct { 233 kmutex_t lock; 234 struct pslist_head *lists; 235 u_long mask; 236 } ifv_hash __cacheline_aligned = { 237 .lists = NULL, 238 .mask = 0, 239 }; 240 241 pserialize_t vlan_psz __read_mostly; 242 static struct psref_class *ifvm_psref_class __read_mostly; 243 244 struct if_clone vlan_cloner = 245 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy); 246 247 /* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */ 248 static char vlan_zero_pad_buff[ETHER_MIN_LEN]; 249 250 static inline int 251 vlan_safe_ifpromisc(struct ifnet *ifp, int pswitch) 252 { 253 int e; 254 255 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 256 e = ifpromisc(ifp, pswitch); 257 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 258 259 return e; 260 } 261 262 static inline int 263 vlan_safe_ifpromisc_locked(struct ifnet *ifp, int pswitch) 264 { 265 int e; 266 267 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 268 e = ifpromisc_locked(ifp, pswitch); 269 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 270 271 return e; 272 } 273 274 void 275 vlanattach(int n) 276 { 277 278 /* 279 * Nothing to do here, initialization is handled by the 280 * module initialization code in vlaninit() below. 281 */ 282 } 283 284 static void 285 vlaninit(void) 286 { 287 mutex_init(&ifv_list.lock, MUTEX_DEFAULT, IPL_NONE); 288 LIST_INIT(&ifv_list.list); 289 290 mutex_init(&ifv_hash.lock, MUTEX_DEFAULT, IPL_NONE); 291 vlan_psz = pserialize_create(); 292 ifvm_psref_class = psref_class_create("vlanlinkmib", IPL_SOFTNET); 293 if_clone_attach(&vlan_cloner); 294 295 vlan_hash_init(); 296 MODULE_HOOK_SET(if_vlan_vlan_input_hook, "vlan_inp", vlan_input); 297 } 298 299 static int 300 vlandetach(void) 301 { 302 bool is_empty; 303 int error; 304 305 mutex_enter(&ifv_list.lock); 306 is_empty = LIST_EMPTY(&ifv_list.list); 307 mutex_exit(&ifv_list.lock); 308 309 if (!is_empty) 310 return EBUSY; 311 312 error = vlan_hash_fini(); 313 if (error != 0) 314 return error; 315 316 if_clone_detach(&vlan_cloner); 317 psref_class_destroy(ifvm_psref_class); 318 pserialize_destroy(vlan_psz); 319 mutex_destroy(&ifv_hash.lock); 320 mutex_destroy(&ifv_list.lock); 321 322 MODULE_HOOK_UNSET(if_vlan_vlan_input_hook); 323 return 0; 324 } 325 326 static void 327 vlan_reset_linkname(struct ifnet *ifp) 328 { 329 330 /* 331 * We start out with a "802.1Q VLAN" type and zero-length 332 * addresses. When we attach to a parent interface, we 333 * inherit its type, address length, address, and data link 334 * type. 335 */ 336 337 ifp->if_type = IFT_L2VLAN; 338 ifp->if_addrlen = 0; 339 ifp->if_dlt = DLT_NULL; 340 if_alloc_sadl(ifp); 341 } 342 343 static int 344 vlan_clone_create(struct if_clone *ifc, int unit) 345 { 346 struct ifvlan *ifv; 347 struct ifnet *ifp; 348 struct ifvlan_linkmib *mib; 349 int rv; 350 351 ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK|M_ZERO); 352 mib = kmem_zalloc(sizeof(struct ifvlan_linkmib), KM_SLEEP); 353 ifp = &ifv->ifv_if; 354 LIST_INIT(&ifv->ifv_mc_listhead); 355 356 mib->ifvm_ifvlan = ifv; 357 mib->ifvm_p = NULL; 358 psref_target_init(&mib->ifvm_psref, ifvm_psref_class); 359 360 mutex_init(&ifv->ifv_lock, MUTEX_DEFAULT, IPL_NONE); 361 ifv->ifv_psz = pserialize_create(); 362 ifv->ifv_mib = mib; 363 364 mutex_enter(&ifv_list.lock); 365 LIST_INSERT_HEAD(&ifv_list.list, ifv, ifv_list); 366 mutex_exit(&ifv_list.lock); 367 368 if_initname(ifp, ifc->ifc_name, unit); 369 ifp->if_softc = ifv; 370 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 371 ifp->if_extflags = IFEF_NO_LINK_STATE_CHANGE; 372 #ifdef NET_MPSAFE 373 ifp->if_extflags |= IFEF_MPSAFE; 374 #endif 375 ifp->if_start = vlan_start; 376 ifp->if_transmit = vlan_transmit; 377 ifp->if_ioctl = vlan_ioctl; 378 IFQ_SET_READY(&ifp->if_snd); 379 380 rv = if_initialize(ifp); 381 if (rv != 0) { 382 aprint_error("%s: if_initialize failed(%d)\n", ifp->if_xname, 383 rv); 384 goto fail; 385 } 386 387 vlan_reset_linkname(ifp); 388 if_register(ifp); 389 return 0; 390 391 fail: 392 mutex_enter(&ifv_list.lock); 393 LIST_REMOVE(ifv, ifv_list); 394 mutex_exit(&ifv_list.lock); 395 396 mutex_destroy(&ifv->ifv_lock); 397 psref_target_destroy(&ifv->ifv_mib->ifvm_psref, ifvm_psref_class); 398 kmem_free(ifv->ifv_mib, sizeof(struct ifvlan_linkmib)); 399 free(ifv, M_DEVBUF); 400 401 return rv; 402 } 403 404 static int 405 vlan_clone_destroy(struct ifnet *ifp) 406 { 407 struct ifvlan *ifv = ifp->if_softc; 408 409 mutex_enter(&ifv_list.lock); 410 LIST_REMOVE(ifv, ifv_list); 411 mutex_exit(&ifv_list.lock); 412 413 IFNET_LOCK(ifp); 414 vlan_unconfig(ifp); 415 IFNET_UNLOCK(ifp); 416 if_detach(ifp); 417 418 psref_target_destroy(&ifv->ifv_mib->ifvm_psref, ifvm_psref_class); 419 kmem_free(ifv->ifv_mib, sizeof(struct ifvlan_linkmib)); 420 pserialize_destroy(ifv->ifv_psz); 421 mutex_destroy(&ifv->ifv_lock); 422 free(ifv, M_DEVBUF); 423 424 return 0; 425 } 426 427 /* 428 * Configure a VLAN interface. 429 */ 430 static int 431 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag) 432 { 433 struct ifnet *ifp = &ifv->ifv_if; 434 struct ifvlan_linkmib *nmib = NULL; 435 struct ifvlan_linkmib *omib = NULL; 436 struct ifvlan_linkmib *checkmib; 437 struct psref_target *nmib_psref = NULL; 438 const uint16_t vid = EVL_VLANOFTAG(tag); 439 int error = 0; 440 int idx; 441 bool omib_cleanup = false; 442 struct psref psref; 443 444 /* VLAN ID 0 and 4095 are reserved in the spec */ 445 if ((vid == 0) || (vid == 0xfff)) 446 return EINVAL; 447 448 nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP); 449 mutex_enter(&ifv->ifv_lock); 450 omib = ifv->ifv_mib; 451 452 if (omib->ifvm_p != NULL) { 453 error = EBUSY; 454 goto done; 455 } 456 457 /* Duplicate check */ 458 checkmib = vlan_lookup_tag_psref(p, vid, &psref); 459 if (checkmib != NULL) { 460 vlan_putref_linkmib(checkmib, &psref); 461 error = EEXIST; 462 goto done; 463 } 464 465 *nmib = *omib; 466 nmib_psref = &nmib->ifvm_psref; 467 468 psref_target_init(nmib_psref, ifvm_psref_class); 469 470 switch (p->if_type) { 471 case IFT_ETHER: 472 { 473 struct ethercom *ec = (void *)p; 474 nmib->ifvm_msw = &vlan_ether_multisw; 475 nmib->ifvm_encaplen = ETHER_VLAN_ENCAP_LEN; 476 nmib->ifvm_mintu = ETHERMIN; 477 478 if (ec->ec_nvlans++ == 0) { 479 IFNET_LOCK(p); 480 error = ether_enable_vlan_mtu(p); 481 IFNET_UNLOCK(p); 482 if (error >= 0) { 483 if (error) { 484 ec->ec_nvlans--; 485 goto done; 486 } 487 nmib->ifvm_mtufudge = 0; 488 } else { 489 /* 490 * Fudge the MTU by the encapsulation size. This 491 * makes us incompatible with strictly compliant 492 * 802.1Q implementations, but allows us to use 493 * the feature with other NetBSD 494 * implementations, which might still be useful. 495 */ 496 nmib->ifvm_mtufudge = nmib->ifvm_encaplen; 497 } 498 error = 0; 499 } 500 501 /* 502 * If the parent interface can do hardware-assisted 503 * VLAN encapsulation, then propagate its hardware- 504 * assisted checksumming flags and tcp segmentation 505 * offload. 506 */ 507 if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) { 508 ec->ec_capenable |= ETHERCAP_VLAN_HWTAGGING; 509 ifp->if_capabilities = p->if_capabilities & 510 (IFCAP_TSOv4 | IFCAP_TSOv6 | 511 IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx| 512 IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx| 513 IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx| 514 IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx| 515 IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx); 516 } 517 518 /* 519 * We inherit the parent's Ethernet address. 520 */ 521 ether_ifattach(ifp, CLLADDR(p->if_sadl)); 522 ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */ 523 break; 524 } 525 526 default: 527 error = EPROTONOSUPPORT; 528 goto done; 529 } 530 531 nmib->ifvm_p = p; 532 nmib->ifvm_tag = vid; 533 ifv->ifv_if.if_mtu = p->if_mtu - nmib->ifvm_mtufudge; 534 ifv->ifv_if.if_flags = p->if_flags & 535 (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 536 537 /* 538 * Inherit the if_type from the parent. This allows us 539 * to participate in bridges of that type. 540 */ 541 ifv->ifv_if.if_type = p->if_type; 542 543 PSLIST_ENTRY_INIT(ifv, ifv_hash); 544 idx = vlan_tag_hash(vid, ifv_hash.mask); 545 546 mutex_enter(&ifv_hash.lock); 547 PSLIST_WRITER_INSERT_HEAD(&ifv_hash.lists[idx], ifv, ifv_hash); 548 mutex_exit(&ifv_hash.lock); 549 550 vlan_linkmib_update(ifv, nmib); 551 nmib = NULL; 552 nmib_psref = NULL; 553 omib_cleanup = true; 554 555 done: 556 mutex_exit(&ifv->ifv_lock); 557 558 if (nmib_psref) 559 psref_target_destroy(nmib_psref, ifvm_psref_class); 560 if (nmib) 561 kmem_free(nmib, sizeof(*nmib)); 562 if (omib_cleanup) 563 kmem_free(omib, sizeof(*omib)); 564 565 return error; 566 } 567 568 /* 569 * Unconfigure a VLAN interface. 570 */ 571 static void 572 vlan_unconfig(struct ifnet *ifp) 573 { 574 struct ifvlan *ifv = ifp->if_softc; 575 struct ifvlan_linkmib *nmib = NULL; 576 int error; 577 578 KASSERT(IFNET_LOCKED(ifp)); 579 580 nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP); 581 582 mutex_enter(&ifv->ifv_lock); 583 error = vlan_unconfig_locked(ifv, nmib); 584 mutex_exit(&ifv->ifv_lock); 585 586 if (error) 587 kmem_free(nmib, sizeof(*nmib)); 588 } 589 static int 590 vlan_unconfig_locked(struct ifvlan *ifv, struct ifvlan_linkmib *nmib) 591 { 592 struct ifnet *p; 593 struct ifnet *ifp = &ifv->ifv_if; 594 struct psref_target *nmib_psref = NULL; 595 struct ifvlan_linkmib *omib; 596 int error = 0; 597 598 KASSERT(IFNET_LOCKED(ifp)); 599 KASSERT(mutex_owned(&ifv->ifv_lock)); 600 601 ifp->if_flags &= ~(IFF_UP|IFF_RUNNING); 602 603 omib = ifv->ifv_mib; 604 p = omib->ifvm_p; 605 606 if (p == NULL) { 607 error = -1; 608 goto done; 609 } 610 611 *nmib = *omib; 612 nmib_psref = &nmib->ifvm_psref; 613 psref_target_init(nmib_psref, ifvm_psref_class); 614 615 /* 616 * Since the interface is being unconfigured, we need to empty the 617 * list of multicast groups that we may have joined while we were 618 * alive and remove them from the parent's list also. 619 */ 620 (*nmib->ifvm_msw->vmsw_purgemulti)(ifv); 621 622 /* Disconnect from parent. */ 623 switch (p->if_type) { 624 case IFT_ETHER: 625 { 626 struct ethercom *ec = (void *)p; 627 if (--ec->ec_nvlans == 0) { 628 IFNET_LOCK(p); 629 (void) ether_disable_vlan_mtu(p); 630 IFNET_UNLOCK(p); 631 } 632 633 /* XXX ether_ifdetach must not be called with IFNET_LOCK */ 634 mutex_exit(&ifv->ifv_lock); 635 IFNET_UNLOCK(ifp); 636 ether_ifdetach(ifp); 637 IFNET_LOCK(ifp); 638 mutex_enter(&ifv->ifv_lock); 639 640 /* if_free_sadl must be called with IFNET_LOCK */ 641 if_free_sadl(ifp, 1); 642 643 /* Restore vlan_ioctl overwritten by ether_ifdetach */ 644 ifp->if_ioctl = vlan_ioctl; 645 vlan_reset_linkname(ifp); 646 break; 647 } 648 649 default: 650 panic("%s: impossible", __func__); 651 } 652 653 nmib->ifvm_p = NULL; 654 ifv->ifv_if.if_mtu = 0; 655 ifv->ifv_flags = 0; 656 657 mutex_enter(&ifv_hash.lock); 658 PSLIST_WRITER_REMOVE(ifv, ifv_hash); 659 pserialize_perform(vlan_psz); 660 mutex_exit(&ifv_hash.lock); 661 PSLIST_ENTRY_DESTROY(ifv, ifv_hash); 662 663 vlan_linkmib_update(ifv, nmib); 664 665 mutex_exit(&ifv->ifv_lock); 666 667 nmib_psref = NULL; 668 kmem_free(omib, sizeof(*omib)); 669 670 #ifdef INET6 671 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 672 /* To delete v6 link local addresses */ 673 if (in6_present) 674 in6_ifdetach(ifp); 675 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 676 #endif 677 678 if ((ifp->if_flags & IFF_PROMISC) != 0) 679 vlan_safe_ifpromisc_locked(ifp, 0); 680 if_down_locked(ifp); 681 ifp->if_capabilities = 0; 682 mutex_enter(&ifv->ifv_lock); 683 done: 684 685 if (nmib_psref) 686 psref_target_destroy(nmib_psref, ifvm_psref_class); 687 688 return error; 689 } 690 691 static void 692 vlan_hash_init(void) 693 { 694 695 ifv_hash.lists = hashinit(VLAN_TAG_HASH_SIZE, HASH_PSLIST, true, 696 &ifv_hash.mask); 697 } 698 699 static int 700 vlan_hash_fini(void) 701 { 702 int i; 703 704 mutex_enter(&ifv_hash.lock); 705 706 for (i = 0; i < ifv_hash.mask + 1; i++) { 707 if (PSLIST_WRITER_FIRST(&ifv_hash.lists[i], struct ifvlan, 708 ifv_hash) != NULL) { 709 mutex_exit(&ifv_hash.lock); 710 return EBUSY; 711 } 712 } 713 714 for (i = 0; i < ifv_hash.mask + 1; i++) 715 PSLIST_DESTROY(&ifv_hash.lists[i]); 716 717 mutex_exit(&ifv_hash.lock); 718 719 hashdone(ifv_hash.lists, HASH_PSLIST, ifv_hash.mask); 720 721 ifv_hash.lists = NULL; 722 ifv_hash.mask = 0; 723 724 return 0; 725 } 726 727 static int 728 vlan_tag_hash(uint16_t tag, u_long mask) 729 { 730 uint32_t hash; 731 732 hash = (tag >> 8) ^ tag; 733 hash = (hash >> 2) ^ hash; 734 735 return hash & mask; 736 } 737 738 static struct ifvlan_linkmib * 739 vlan_getref_linkmib(struct ifvlan *sc, struct psref *psref) 740 { 741 struct ifvlan_linkmib *mib; 742 int s; 743 744 s = pserialize_read_enter(); 745 mib = sc->ifv_mib; 746 if (mib == NULL) { 747 pserialize_read_exit(s); 748 return NULL; 749 } 750 membar_datadep_consumer(); 751 psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class); 752 pserialize_read_exit(s); 753 754 return mib; 755 } 756 757 static void 758 vlan_putref_linkmib(struct ifvlan_linkmib *mib, struct psref *psref) 759 { 760 if (mib == NULL) 761 return; 762 psref_release(psref, &mib->ifvm_psref, ifvm_psref_class); 763 } 764 765 static struct ifvlan_linkmib * 766 vlan_lookup_tag_psref(struct ifnet *ifp, uint16_t tag, struct psref *psref) 767 { 768 int idx; 769 int s; 770 struct ifvlan *sc; 771 772 idx = vlan_tag_hash(tag, ifv_hash.mask); 773 774 s = pserialize_read_enter(); 775 PSLIST_READER_FOREACH(sc, &ifv_hash.lists[idx], struct ifvlan, 776 ifv_hash) { 777 struct ifvlan_linkmib *mib = sc->ifv_mib; 778 if (mib == NULL) 779 continue; 780 if (mib->ifvm_tag != tag) 781 continue; 782 if (mib->ifvm_p != ifp) 783 continue; 784 785 psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class); 786 pserialize_read_exit(s); 787 return mib; 788 } 789 pserialize_read_exit(s); 790 return NULL; 791 } 792 793 static void 794 vlan_linkmib_update(struct ifvlan *ifv, struct ifvlan_linkmib *nmib) 795 { 796 struct ifvlan_linkmib *omib = ifv->ifv_mib; 797 798 KASSERT(mutex_owned(&ifv->ifv_lock)); 799 800 membar_producer(); 801 ifv->ifv_mib = nmib; 802 803 pserialize_perform(ifv->ifv_psz); 804 psref_target_destroy(&omib->ifvm_psref, ifvm_psref_class); 805 } 806 807 /* 808 * Called when a parent interface is detaching; destroy any VLAN 809 * configuration for the parent interface. 810 */ 811 void 812 vlan_ifdetach(struct ifnet *p) 813 { 814 struct ifvlan *ifv; 815 struct ifvlan_linkmib *mib, **nmibs; 816 struct psref psref; 817 int error; 818 int bound; 819 int i, cnt = 0; 820 821 bound = curlwp_bind(); 822 823 mutex_enter(&ifv_list.lock); 824 LIST_FOREACH(ifv, &ifv_list.list, ifv_list) { 825 mib = vlan_getref_linkmib(ifv, &psref); 826 if (mib == NULL) 827 continue; 828 829 if (mib->ifvm_p == p) 830 cnt++; 831 832 vlan_putref_linkmib(mib, &psref); 833 } 834 mutex_exit(&ifv_list.lock); 835 836 if (cnt == 0) { 837 curlwp_bindx(bound); 838 return; 839 } 840 841 /* 842 * The value of "cnt" does not increase while ifv_list.lock 843 * and ifv->ifv_lock are released here, because the parent 844 * interface is detaching. 845 */ 846 nmibs = kmem_alloc(sizeof(*nmibs) * cnt, KM_SLEEP); 847 for (i = 0; i < cnt; i++) { 848 nmibs[i] = kmem_alloc(sizeof(*nmibs[i]), KM_SLEEP); 849 } 850 851 mutex_enter(&ifv_list.lock); 852 853 i = 0; 854 LIST_FOREACH(ifv, &ifv_list.list, ifv_list) { 855 struct ifnet *ifp = &ifv->ifv_if; 856 857 /* IFNET_LOCK must be held before ifv_lock. */ 858 IFNET_LOCK(ifp); 859 mutex_enter(&ifv->ifv_lock); 860 861 /* XXX ifv_mib = NULL? */ 862 if (ifv->ifv_mib->ifvm_p == p) { 863 KASSERTMSG(i < cnt, "no memory for unconfig, parent=%s", 864 p->if_xname); 865 error = vlan_unconfig_locked(ifv, nmibs[i]); 866 if (!error) { 867 nmibs[i] = NULL; 868 i++; 869 } 870 871 } 872 873 mutex_exit(&ifv->ifv_lock); 874 IFNET_UNLOCK(ifp); 875 } 876 877 mutex_exit(&ifv_list.lock); 878 879 curlwp_bindx(bound); 880 881 for (i = 0; i < cnt; i++) { 882 if (nmibs[i]) 883 kmem_free(nmibs[i], sizeof(*nmibs[i])); 884 } 885 886 kmem_free(nmibs, sizeof(*nmibs) * cnt); 887 888 return; 889 } 890 891 static int 892 vlan_set_promisc(struct ifnet *ifp) 893 { 894 struct ifvlan *ifv = ifp->if_softc; 895 struct ifvlan_linkmib *mib; 896 struct psref psref; 897 int error = 0; 898 int bound; 899 900 bound = curlwp_bind(); 901 mib = vlan_getref_linkmib(ifv, &psref); 902 if (mib == NULL) { 903 curlwp_bindx(bound); 904 return EBUSY; 905 } 906 907 if ((ifp->if_flags & IFF_PROMISC) != 0) { 908 if ((ifv->ifv_flags & IFVF_PROMISC) == 0) { 909 error = vlan_safe_ifpromisc(mib->ifvm_p, 1); 910 if (error == 0) 911 ifv->ifv_flags |= IFVF_PROMISC; 912 } 913 } else { 914 if ((ifv->ifv_flags & IFVF_PROMISC) != 0) { 915 error = vlan_safe_ifpromisc(mib->ifvm_p, 0); 916 if (error == 0) 917 ifv->ifv_flags &= ~IFVF_PROMISC; 918 } 919 } 920 vlan_putref_linkmib(mib, &psref); 921 curlwp_bindx(bound); 922 923 return error; 924 } 925 926 static int 927 vlan_ioctl(struct ifnet *ifp, u_long cmd, void *data) 928 { 929 struct lwp *l = curlwp; 930 struct ifvlan *ifv = ifp->if_softc; 931 struct ifaddr *ifa = (struct ifaddr *) data; 932 struct ifreq *ifr = (struct ifreq *) data; 933 struct ifnet *pr; 934 struct ifcapreq *ifcr; 935 struct vlanreq vlr; 936 struct ifvlan_linkmib *mib; 937 struct psref psref; 938 int error = 0; 939 int bound; 940 941 switch (cmd) { 942 case SIOCSIFMTU: 943 bound = curlwp_bind(); 944 mib = vlan_getref_linkmib(ifv, &psref); 945 if (mib == NULL) { 946 curlwp_bindx(bound); 947 error = EBUSY; 948 break; 949 } 950 951 if (mib->ifvm_p == NULL) { 952 vlan_putref_linkmib(mib, &psref); 953 curlwp_bindx(bound); 954 error = EINVAL; 955 } else if ( 956 ifr->ifr_mtu > (mib->ifvm_p->if_mtu - mib->ifvm_mtufudge) || 957 ifr->ifr_mtu < (mib->ifvm_mintu - mib->ifvm_mtufudge)) { 958 vlan_putref_linkmib(mib, &psref); 959 curlwp_bindx(bound); 960 error = EINVAL; 961 } else { 962 vlan_putref_linkmib(mib, &psref); 963 curlwp_bindx(bound); 964 965 error = ifioctl_common(ifp, cmd, data); 966 if (error == ENETRESET) 967 error = 0; 968 } 969 970 break; 971 972 case SIOCSETVLAN: 973 if ((error = kauth_authorize_network(l->l_cred, 974 KAUTH_NETWORK_INTERFACE, 975 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 976 NULL)) != 0) 977 break; 978 if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0) 979 break; 980 981 if (vlr.vlr_parent[0] == '\0') { 982 bound = curlwp_bind(); 983 mib = vlan_getref_linkmib(ifv, &psref); 984 if (mib == NULL) { 985 curlwp_bindx(bound); 986 error = EBUSY; 987 break; 988 } 989 990 if (mib->ifvm_p != NULL && 991 (ifp->if_flags & IFF_PROMISC) != 0) 992 error = vlan_safe_ifpromisc(mib->ifvm_p, 0); 993 994 vlan_putref_linkmib(mib, &psref); 995 curlwp_bindx(bound); 996 997 vlan_unconfig(ifp); 998 break; 999 } 1000 if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) { 1001 error = EINVAL; /* check for valid tag */ 1002 break; 1003 } 1004 if ((pr = ifunit(vlr.vlr_parent)) == NULL) { 1005 error = ENOENT; 1006 break; 1007 } 1008 error = vlan_config(ifv, pr, vlr.vlr_tag); 1009 if (error != 0) { 1010 break; 1011 } 1012 1013 /* Update promiscuous mode, if necessary. */ 1014 vlan_set_promisc(ifp); 1015 1016 ifp->if_flags |= IFF_RUNNING; 1017 break; 1018 1019 case SIOCGETVLAN: 1020 memset(&vlr, 0, sizeof(vlr)); 1021 bound = curlwp_bind(); 1022 mib = vlan_getref_linkmib(ifv, &psref); 1023 if (mib == NULL) { 1024 curlwp_bindx(bound); 1025 error = EBUSY; 1026 break; 1027 } 1028 if (mib->ifvm_p != NULL) { 1029 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s", 1030 mib->ifvm_p->if_xname); 1031 vlr.vlr_tag = mib->ifvm_tag; 1032 } 1033 vlan_putref_linkmib(mib, &psref); 1034 curlwp_bindx(bound); 1035 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 1036 break; 1037 1038 case SIOCSIFFLAGS: 1039 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 1040 break; 1041 /* 1042 * For promiscuous mode, we enable promiscuous mode on 1043 * the parent if we need promiscuous on the VLAN interface. 1044 */ 1045 bound = curlwp_bind(); 1046 mib = vlan_getref_linkmib(ifv, &psref); 1047 if (mib == NULL) { 1048 curlwp_bindx(bound); 1049 error = EBUSY; 1050 break; 1051 } 1052 1053 if (mib->ifvm_p != NULL) 1054 error = vlan_set_promisc(ifp); 1055 vlan_putref_linkmib(mib, &psref); 1056 curlwp_bindx(bound); 1057 break; 1058 1059 case SIOCADDMULTI: 1060 mutex_enter(&ifv->ifv_lock); 1061 mib = ifv->ifv_mib; 1062 if (mib == NULL) { 1063 error = EBUSY; 1064 mutex_exit(&ifv->ifv_lock); 1065 break; 1066 } 1067 1068 error = (mib->ifvm_p != NULL) ? 1069 (*mib->ifvm_msw->vmsw_addmulti)(ifv, ifr) : EINVAL; 1070 mib = NULL; 1071 mutex_exit(&ifv->ifv_lock); 1072 break; 1073 1074 case SIOCDELMULTI: 1075 mutex_enter(&ifv->ifv_lock); 1076 mib = ifv->ifv_mib; 1077 if (mib == NULL) { 1078 error = EBUSY; 1079 mutex_exit(&ifv->ifv_lock); 1080 break; 1081 } 1082 error = (mib->ifvm_p != NULL) ? 1083 (*mib->ifvm_msw->vmsw_delmulti)(ifv, ifr) : EINVAL; 1084 mib = NULL; 1085 mutex_exit(&ifv->ifv_lock); 1086 break; 1087 1088 case SIOCSIFCAP: 1089 ifcr = data; 1090 /* make sure caps are enabled on parent */ 1091 bound = curlwp_bind(); 1092 mib = vlan_getref_linkmib(ifv, &psref); 1093 if (mib == NULL) { 1094 curlwp_bindx(bound); 1095 error = EBUSY; 1096 break; 1097 } 1098 1099 if (mib->ifvm_p == NULL) { 1100 vlan_putref_linkmib(mib, &psref); 1101 curlwp_bindx(bound); 1102 error = EINVAL; 1103 break; 1104 } 1105 if ((mib->ifvm_p->if_capenable & ifcr->ifcr_capenable) != 1106 ifcr->ifcr_capenable) { 1107 vlan_putref_linkmib(mib, &psref); 1108 curlwp_bindx(bound); 1109 error = EINVAL; 1110 break; 1111 } 1112 1113 vlan_putref_linkmib(mib, &psref); 1114 curlwp_bindx(bound); 1115 1116 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET) 1117 error = 0; 1118 break; 1119 case SIOCINITIFADDR: 1120 bound = curlwp_bind(); 1121 mib = vlan_getref_linkmib(ifv, &psref); 1122 if (mib == NULL) { 1123 curlwp_bindx(bound); 1124 error = EBUSY; 1125 break; 1126 } 1127 1128 if (mib->ifvm_p == NULL) { 1129 error = EINVAL; 1130 vlan_putref_linkmib(mib, &psref); 1131 curlwp_bindx(bound); 1132 break; 1133 } 1134 vlan_putref_linkmib(mib, &psref); 1135 curlwp_bindx(bound); 1136 1137 ifp->if_flags |= IFF_UP; 1138 #ifdef INET 1139 if (ifa->ifa_addr->sa_family == AF_INET) 1140 arp_ifinit(ifp, ifa); 1141 #endif 1142 break; 1143 1144 default: 1145 error = ether_ioctl(ifp, cmd, data); 1146 } 1147 1148 return error; 1149 } 1150 1151 static int 1152 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr) 1153 { 1154 const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr); 1155 struct vlan_mc_entry *mc; 1156 uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN]; 1157 struct ifvlan_linkmib *mib; 1158 int error; 1159 1160 KASSERT(mutex_owned(&ifv->ifv_lock)); 1161 1162 if (sa->sa_len > sizeof(struct sockaddr_storage)) 1163 return EINVAL; 1164 1165 error = ether_addmulti(sa, &ifv->ifv_ec); 1166 if (error != ENETRESET) 1167 return error; 1168 1169 /* 1170 * This is a new multicast address. We have to tell parent 1171 * about it. Also, remember this multicast address so that 1172 * we can delete it on unconfigure. 1173 */ 1174 mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT); 1175 if (mc == NULL) { 1176 error = ENOMEM; 1177 goto alloc_failed; 1178 } 1179 1180 /* 1181 * Since ether_addmulti() returned ENETRESET, the following two 1182 * statements shouldn't fail. Here ifv_ec is implicitly protected 1183 * by the ifv_lock lock. 1184 */ 1185 error = ether_multiaddr(sa, addrlo, addrhi); 1186 KASSERT(error == 0); 1187 1188 ETHER_LOCK(&ifv->ifv_ec); 1189 mc->mc_enm = ether_lookup_multi(addrlo, addrhi, &ifv->ifv_ec); 1190 ETHER_UNLOCK(&ifv->ifv_ec); 1191 1192 KASSERT(mc->mc_enm != NULL); 1193 1194 memcpy(&mc->mc_addr, sa, sa->sa_len); 1195 LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries); 1196 1197 mib = ifv->ifv_mib; 1198 1199 KERNEL_LOCK_UNLESS_IFP_MPSAFE(mib->ifvm_p); 1200 error = if_mcast_op(mib->ifvm_p, SIOCADDMULTI, sa); 1201 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(mib->ifvm_p); 1202 1203 if (error != 0) 1204 goto ioctl_failed; 1205 return error; 1206 1207 ioctl_failed: 1208 LIST_REMOVE(mc, mc_entries); 1209 free(mc, M_DEVBUF); 1210 1211 alloc_failed: 1212 (void)ether_delmulti(sa, &ifv->ifv_ec); 1213 return error; 1214 } 1215 1216 static int 1217 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr) 1218 { 1219 const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr); 1220 struct ether_multi *enm; 1221 struct vlan_mc_entry *mc; 1222 struct ifvlan_linkmib *mib; 1223 uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN]; 1224 int error; 1225 1226 KASSERT(mutex_owned(&ifv->ifv_lock)); 1227 1228 /* 1229 * Find a key to lookup vlan_mc_entry. We have to do this 1230 * before calling ether_delmulti for obvious reasons. 1231 */ 1232 if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0) 1233 return error; 1234 1235 ETHER_LOCK(&ifv->ifv_ec); 1236 enm = ether_lookup_multi(addrlo, addrhi, &ifv->ifv_ec); 1237 ETHER_UNLOCK(&ifv->ifv_ec); 1238 if (enm == NULL) 1239 return EINVAL; 1240 1241 LIST_FOREACH(mc, &ifv->ifv_mc_listhead, mc_entries) { 1242 if (mc->mc_enm == enm) 1243 break; 1244 } 1245 1246 /* We woun't delete entries we didn't add */ 1247 if (mc == NULL) 1248 return EINVAL; 1249 1250 error = ether_delmulti(sa, &ifv->ifv_ec); 1251 if (error != ENETRESET) 1252 return error; 1253 1254 /* We no longer use this multicast address. Tell parent so. */ 1255 mib = ifv->ifv_mib; 1256 error = if_mcast_op(mib->ifvm_p, SIOCDELMULTI, sa); 1257 1258 if (error == 0) { 1259 /* And forget about this address. */ 1260 LIST_REMOVE(mc, mc_entries); 1261 free(mc, M_DEVBUF); 1262 } else { 1263 (void)ether_addmulti(sa, &ifv->ifv_ec); 1264 } 1265 1266 return error; 1267 } 1268 1269 /* 1270 * Delete any multicast address we have asked to add from parent 1271 * interface. Called when the vlan is being unconfigured. 1272 */ 1273 static void 1274 vlan_ether_purgemulti(struct ifvlan *ifv) 1275 { 1276 struct vlan_mc_entry *mc; 1277 struct ifvlan_linkmib *mib; 1278 1279 KASSERT(mutex_owned(&ifv->ifv_lock)); 1280 mib = ifv->ifv_mib; 1281 if (mib == NULL) { 1282 return; 1283 } 1284 1285 while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) { 1286 (void)if_mcast_op(mib->ifvm_p, SIOCDELMULTI, 1287 sstocsa(&mc->mc_addr)); 1288 LIST_REMOVE(mc, mc_entries); 1289 free(mc, M_DEVBUF); 1290 } 1291 } 1292 1293 static void 1294 vlan_start(struct ifnet *ifp) 1295 { 1296 struct ifvlan *ifv = ifp->if_softc; 1297 struct ifnet *p; 1298 struct ethercom *ec; 1299 struct mbuf *m; 1300 struct ifvlan_linkmib *mib; 1301 struct psref psref; 1302 int error; 1303 1304 mib = vlan_getref_linkmib(ifv, &psref); 1305 if (mib == NULL) 1306 return; 1307 p = mib->ifvm_p; 1308 ec = (void *)mib->ifvm_p; 1309 1310 ifp->if_flags |= IFF_OACTIVE; 1311 1312 for (;;) { 1313 IFQ_DEQUEUE(&ifp->if_snd, m); 1314 if (m == NULL) 1315 break; 1316 1317 #ifdef ALTQ 1318 /* 1319 * KERNEL_LOCK is required for ALTQ even if NET_MPSAFE is 1320 * defined. 1321 */ 1322 KERNEL_LOCK(1, NULL); 1323 /* 1324 * If ALTQ is enabled on the parent interface, do 1325 * classification; the queueing discipline might 1326 * not require classification, but might require 1327 * the address family/header pointer in the pktattr. 1328 */ 1329 if (ALTQ_IS_ENABLED(&p->if_snd)) { 1330 switch (p->if_type) { 1331 case IFT_ETHER: 1332 altq_etherclassify(&p->if_snd, m); 1333 break; 1334 default: 1335 panic("%s: impossible (altq)", __func__); 1336 } 1337 } 1338 KERNEL_UNLOCK_ONE(NULL); 1339 #endif /* ALTQ */ 1340 1341 bpf_mtap(ifp, m, BPF_D_OUT); 1342 /* 1343 * If the parent can insert the tag itself, just mark 1344 * the tag in the mbuf header. 1345 */ 1346 if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) { 1347 vlan_set_tag(m, mib->ifvm_tag); 1348 } else { 1349 /* 1350 * insert the tag ourselves 1351 */ 1352 M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT); 1353 if (m == NULL) { 1354 printf("%s: unable to prepend encap header", 1355 p->if_xname); 1356 ifp->if_oerrors++; 1357 continue; 1358 } 1359 1360 switch (p->if_type) { 1361 case IFT_ETHER: 1362 { 1363 struct ether_vlan_header *evl; 1364 1365 if (m->m_len < sizeof(struct ether_vlan_header)) 1366 m = m_pullup(m, 1367 sizeof(struct ether_vlan_header)); 1368 if (m == NULL) { 1369 printf("%s: unable to pullup encap " 1370 "header", p->if_xname); 1371 ifp->if_oerrors++; 1372 continue; 1373 } 1374 1375 /* 1376 * Transform the Ethernet header into an 1377 * Ethernet header with 802.1Q encapsulation. 1378 */ 1379 memmove(mtod(m, void *), 1380 mtod(m, char *) + mib->ifvm_encaplen, 1381 sizeof(struct ether_header)); 1382 evl = mtod(m, struct ether_vlan_header *); 1383 evl->evl_proto = evl->evl_encap_proto; 1384 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 1385 evl->evl_tag = htons(mib->ifvm_tag); 1386 1387 /* 1388 * To cater for VLAN-aware layer 2 ethernet 1389 * switches which may need to strip the tag 1390 * before forwarding the packet, make sure 1391 * the packet+tag is at least 68 bytes long. 1392 * This is necessary because our parent will 1393 * only pad to 64 bytes (ETHER_MIN_LEN) and 1394 * some switches will not pad by themselves 1395 * after deleting a tag. 1396 */ 1397 const size_t min_data_len = ETHER_MIN_LEN - 1398 ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN; 1399 if (m->m_pkthdr.len < min_data_len) { 1400 m_copyback(m, m->m_pkthdr.len, 1401 min_data_len - m->m_pkthdr.len, 1402 vlan_zero_pad_buff); 1403 } 1404 break; 1405 } 1406 1407 default: 1408 panic("%s: impossible", __func__); 1409 } 1410 } 1411 1412 if ((p->if_flags & IFF_RUNNING) == 0) { 1413 m_freem(m); 1414 continue; 1415 } 1416 1417 error = if_transmit_lock(p, m); 1418 if (error) { 1419 /* mbuf is already freed */ 1420 ifp->if_oerrors++; 1421 continue; 1422 } 1423 ifp->if_opackets++; 1424 } 1425 1426 ifp->if_flags &= ~IFF_OACTIVE; 1427 1428 /* Remove reference to mib before release */ 1429 vlan_putref_linkmib(mib, &psref); 1430 } 1431 1432 static int 1433 vlan_transmit(struct ifnet *ifp, struct mbuf *m) 1434 { 1435 struct ifvlan *ifv = ifp->if_softc; 1436 struct ifnet *p; 1437 struct ethercom *ec; 1438 struct ifvlan_linkmib *mib; 1439 struct psref psref; 1440 int error; 1441 size_t pktlen = m->m_pkthdr.len; 1442 bool mcast = (m->m_flags & M_MCAST) != 0; 1443 1444 mib = vlan_getref_linkmib(ifv, &psref); 1445 if (mib == NULL) { 1446 m_freem(m); 1447 return ENETDOWN; 1448 } 1449 1450 p = mib->ifvm_p; 1451 ec = (void *)mib->ifvm_p; 1452 1453 bpf_mtap(ifp, m, BPF_D_OUT); 1454 1455 if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0) 1456 goto out; 1457 if (m == NULL) 1458 goto out; 1459 1460 /* 1461 * If the parent can insert the tag itself, just mark 1462 * the tag in the mbuf header. 1463 */ 1464 if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) { 1465 vlan_set_tag(m, mib->ifvm_tag); 1466 } else { 1467 /* 1468 * insert the tag ourselves 1469 */ 1470 M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT); 1471 if (m == NULL) { 1472 printf("%s: unable to prepend encap header", 1473 p->if_xname); 1474 ifp->if_oerrors++; 1475 error = ENOBUFS; 1476 goto out; 1477 } 1478 1479 switch (p->if_type) { 1480 case IFT_ETHER: 1481 { 1482 struct ether_vlan_header *evl; 1483 1484 if (m->m_len < sizeof(struct ether_vlan_header)) 1485 m = m_pullup(m, 1486 sizeof(struct ether_vlan_header)); 1487 if (m == NULL) { 1488 printf("%s: unable to pullup encap " 1489 "header", p->if_xname); 1490 ifp->if_oerrors++; 1491 error = ENOBUFS; 1492 goto out; 1493 } 1494 1495 /* 1496 * Transform the Ethernet header into an 1497 * Ethernet header with 802.1Q encapsulation. 1498 */ 1499 memmove(mtod(m, void *), 1500 mtod(m, char *) + mib->ifvm_encaplen, 1501 sizeof(struct ether_header)); 1502 evl = mtod(m, struct ether_vlan_header *); 1503 evl->evl_proto = evl->evl_encap_proto; 1504 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 1505 evl->evl_tag = htons(mib->ifvm_tag); 1506 1507 /* 1508 * To cater for VLAN-aware layer 2 ethernet 1509 * switches which may need to strip the tag 1510 * before forwarding the packet, make sure 1511 * the packet+tag is at least 68 bytes long. 1512 * This is necessary because our parent will 1513 * only pad to 64 bytes (ETHER_MIN_LEN) and 1514 * some switches will not pad by themselves 1515 * after deleting a tag. 1516 */ 1517 const size_t min_data_len = ETHER_MIN_LEN - 1518 ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN; 1519 if (m->m_pkthdr.len < min_data_len) { 1520 m_copyback(m, m->m_pkthdr.len, 1521 min_data_len - m->m_pkthdr.len, 1522 vlan_zero_pad_buff); 1523 } 1524 break; 1525 } 1526 1527 default: 1528 panic("%s: impossible", __func__); 1529 } 1530 } 1531 1532 if ((p->if_flags & IFF_RUNNING) == 0) { 1533 m_freem(m); 1534 error = ENETDOWN; 1535 goto out; 1536 } 1537 1538 error = if_transmit_lock(p, m); 1539 if (error) { 1540 /* mbuf is already freed */ 1541 ifp->if_oerrors++; 1542 } else { 1543 1544 ifp->if_opackets++; 1545 ifp->if_obytes += pktlen; 1546 if (mcast) 1547 ifp->if_omcasts++; 1548 } 1549 1550 out: 1551 /* Remove reference to mib before release */ 1552 vlan_putref_linkmib(mib, &psref); 1553 return error; 1554 } 1555 1556 /* 1557 * Given an Ethernet frame, find a valid vlan interface corresponding to the 1558 * given source interface and tag, then run the real packet through the 1559 * parent's input routine. 1560 */ 1561 void 1562 vlan_input(struct ifnet *ifp, struct mbuf *m) 1563 { 1564 struct ifvlan *ifv; 1565 uint16_t vid; 1566 struct ifvlan_linkmib *mib; 1567 struct psref psref; 1568 bool have_vtag; 1569 1570 have_vtag = vlan_has_tag(m); 1571 if (have_vtag) { 1572 vid = EVL_VLANOFTAG(vlan_get_tag(m)); 1573 m->m_flags &= ~M_VLANTAG; 1574 } else { 1575 struct ether_vlan_header *evl; 1576 1577 if (ifp->if_type != IFT_ETHER) { 1578 panic("%s: impossible", __func__); 1579 } 1580 1581 if (m->m_len < sizeof(struct ether_vlan_header) && 1582 (m = m_pullup(m, 1583 sizeof(struct ether_vlan_header))) == NULL) { 1584 printf("%s: no memory for VLAN header, " 1585 "dropping packet.\n", ifp->if_xname); 1586 return; 1587 } 1588 evl = mtod(m, struct ether_vlan_header *); 1589 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN); 1590 1591 vid = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 1592 1593 /* 1594 * Restore the original ethertype. We'll remove 1595 * the encapsulation after we've found the vlan 1596 * interface corresponding to the tag. 1597 */ 1598 evl->evl_encap_proto = evl->evl_proto; 1599 } 1600 1601 mib = vlan_lookup_tag_psref(ifp, vid, &psref); 1602 if (mib == NULL) { 1603 m_freem(m); 1604 ifp->if_noproto++; 1605 return; 1606 } 1607 KASSERT(mib->ifvm_encaplen == ETHER_VLAN_ENCAP_LEN); 1608 1609 ifv = mib->ifvm_ifvlan; 1610 if ((ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) != 1611 (IFF_UP|IFF_RUNNING)) { 1612 m_freem(m); 1613 ifp->if_noproto++; 1614 goto out; 1615 } 1616 1617 /* 1618 * Now, remove the encapsulation header. The original 1619 * header has already been fixed up above. 1620 */ 1621 if (!have_vtag) { 1622 memmove(mtod(m, char *) + mib->ifvm_encaplen, 1623 mtod(m, void *), sizeof(struct ether_header)); 1624 m_adj(m, mib->ifvm_encaplen); 1625 } 1626 1627 m_set_rcvif(m, &ifv->ifv_if); 1628 ifv->ifv_if.if_ipackets++; 1629 1630 if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0) 1631 goto out; 1632 if (m == NULL) 1633 goto out; 1634 1635 m->m_flags &= ~M_PROMISC; 1636 if_input(&ifv->ifv_if, m); 1637 out: 1638 vlan_putref_linkmib(mib, &psref); 1639 } 1640 1641 /* 1642 * Module infrastructure 1643 */ 1644 #include "if_module.h" 1645 1646 IF_MODULE(MODULE_CLASS_DRIVER, vlan, NULL) 1647