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