1 /*- 2 * Copyright 1998 Massachusetts Institute of Technology 3 * 4 * Permission to use, copy, modify, and distribute this software and 5 * its documentation for any purpose and without fee is hereby 6 * granted, provided that both the above copyright notice and this 7 * permission notice appear in all copies, that both the above 8 * copyright notice and this permission notice appear in all 9 * supporting documentation, and that the name of M.I.T. not be used 10 * in advertising or publicity pertaining to distribution of the 11 * software without specific, written prior permission. M.I.T. makes 12 * no representations about the suitability of this software for any 13 * purpose. It is provided "as is" without express or implied 14 * warranty. 15 * 16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 32 * Might be extended some day to also handle IEEE 802.1p priority 33 * tagging. This is sort of sneaky in the implementation, since 34 * we need to pretend to be enough of an Ethernet implementation 35 * to make arp work. The way we do this is by telling everyone 36 * that we are an Ethernet, and then catch the packets that 37 * ether_output() left on our output queue when it calls 38 * if_start(), rewrite them for use by the real outgoing interface, 39 * and ask it to send them. 40 */ 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include "opt_vlan.h" 46 47 #include <sys/param.h> 48 #include <sys/kernel.h> 49 #include <sys/lock.h> 50 #include <sys/malloc.h> 51 #include <sys/mbuf.h> 52 #include <sys/module.h> 53 #include <sys/rwlock.h> 54 #include <sys/queue.h> 55 #include <sys/socket.h> 56 #include <sys/sockio.h> 57 #include <sys/sysctl.h> 58 #include <sys/systm.h> 59 60 #include <net/bpf.h> 61 #include <net/ethernet.h> 62 #include <net/if.h> 63 #include <net/if_clone.h> 64 #include <net/if_dl.h> 65 #include <net/if_types.h> 66 #include <net/if_vlan_var.h> 67 #include <net/vnet.h> 68 69 #define VLANNAME "vlan" 70 #define VLAN_DEF_HWIDTH 4 71 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 72 73 #define UP_AND_RUNNING(ifp) \ 74 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 75 76 LIST_HEAD(ifvlanhead, ifvlan); 77 78 struct ifvlantrunk { 79 struct ifnet *parent; /* parent interface of this trunk */ 80 struct rwlock rw; 81 #ifdef VLAN_ARRAY 82 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 83 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 84 #else 85 struct ifvlanhead *hash; /* dynamic hash-list table */ 86 uint16_t hmask; 87 uint16_t hwidth; 88 #endif 89 int refcnt; 90 }; 91 92 struct vlan_mc_entry { 93 struct ether_addr mc_addr; 94 SLIST_ENTRY(vlan_mc_entry) mc_entries; 95 }; 96 97 struct ifvlan { 98 struct ifvlantrunk *ifv_trunk; 99 struct ifnet *ifv_ifp; 100 #define TRUNK(ifv) ((ifv)->ifv_trunk) 101 #define PARENT(ifv) ((ifv)->ifv_trunk->parent) 102 int ifv_pflags; /* special flags we have set on parent */ 103 struct ifv_linkmib { 104 int ifvm_encaplen; /* encapsulation length */ 105 int ifvm_mtufudge; /* MTU fudged by this much */ 106 int ifvm_mintu; /* min transmission unit */ 107 uint16_t ifvm_proto; /* encapsulation ethertype */ 108 uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 109 } ifv_mib; 110 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 111 #ifndef VLAN_ARRAY 112 LIST_ENTRY(ifvlan) ifv_list; 113 #endif 114 }; 115 #define ifv_proto ifv_mib.ifvm_proto 116 #define ifv_tag ifv_mib.ifvm_tag 117 #define ifv_encaplen ifv_mib.ifvm_encaplen 118 #define ifv_mtufudge ifv_mib.ifvm_mtufudge 119 #define ifv_mintu ifv_mib.ifvm_mintu 120 121 /* Special flags we should propagate to parent. */ 122 static struct { 123 int flag; 124 int (*func)(struct ifnet *, int); 125 } vlan_pflags[] = { 126 {IFF_PROMISC, ifpromisc}, 127 {IFF_ALLMULTI, if_allmulti}, 128 {0, NULL} 129 }; 130 131 SYSCTL_DECL(_net_link); 132 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 133 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 134 135 static int soft_pad = 0; 136 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0, 137 "pad short frames before tagging"); 138 139 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface"); 140 141 static eventhandler_tag ifdetach_tag; 142 static eventhandler_tag iflladdr_tag; 143 144 /* 145 * We have a global mutex, that is used to serialize configuration 146 * changes and isn't used in normal packet delivery. 147 * 148 * We also have a per-trunk rwlock, that is locked shared on packet 149 * processing and exclusive when configuration is changed. 150 * 151 * The VLAN_ARRAY substitutes the dynamic hash with a static array 152 * with 4096 entries. In theory this can give a boost in processing, 153 * however on practice it does not. Probably this is because array 154 * is too big to fit into CPU cache. 155 */ 156 static struct mtx ifv_mtx; 157 #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF) 158 #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx) 159 #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED) 160 #define VLAN_LOCK() mtx_lock(&ifv_mtx) 161 #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx) 162 #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME) 163 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw) 164 #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw) 165 #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw) 166 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED) 167 #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw) 168 #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw) 169 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED) 170 171 #ifndef VLAN_ARRAY 172 static void vlan_inithash(struct ifvlantrunk *trunk); 173 static void vlan_freehash(struct ifvlantrunk *trunk); 174 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 175 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 176 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 177 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 178 uint16_t tag); 179 #endif 180 static void trunk_destroy(struct ifvlantrunk *trunk); 181 182 static void vlan_start(struct ifnet *ifp); 183 static void vlan_init(void *foo); 184 static void vlan_input(struct ifnet *ifp, struct mbuf *m); 185 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 186 static int vlan_setflag(struct ifnet *ifp, int flag, int status, 187 int (*func)(struct ifnet *, int)); 188 static int vlan_setflags(struct ifnet *ifp, int status); 189 static int vlan_setmulti(struct ifnet *ifp); 190 static void vlan_unconfig(struct ifnet *ifp); 191 static void vlan_unconfig_locked(struct ifnet *ifp); 192 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag); 193 static void vlan_link_state(struct ifnet *ifp); 194 static void vlan_capabilities(struct ifvlan *ifv); 195 static void vlan_trunk_capabilities(struct ifnet *ifp); 196 197 static struct ifnet *vlan_clone_match_ethertag(struct if_clone *, 198 const char *, int *); 199 static int vlan_clone_match(struct if_clone *, const char *); 200 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 201 static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 202 203 static void vlan_ifdetach(void *arg, struct ifnet *ifp); 204 static void vlan_iflladdr(void *arg, struct ifnet *ifp); 205 206 static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL, 207 IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy); 208 209 #ifndef VLAN_ARRAY 210 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 211 212 static void 213 vlan_inithash(struct ifvlantrunk *trunk) 214 { 215 int i, n; 216 217 /* 218 * The trunk must not be locked here since we call malloc(M_WAITOK). 219 * It is OK in case this function is called before the trunk struct 220 * gets hooked up and becomes visible from other threads. 221 */ 222 223 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 224 ("%s: hash already initialized", __func__)); 225 226 trunk->hwidth = VLAN_DEF_HWIDTH; 227 n = 1 << trunk->hwidth; 228 trunk->hmask = n - 1; 229 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 230 for (i = 0; i < n; i++) 231 LIST_INIT(&trunk->hash[i]); 232 } 233 234 static void 235 vlan_freehash(struct ifvlantrunk *trunk) 236 { 237 #ifdef INVARIANTS 238 int i; 239 240 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 241 for (i = 0; i < (1 << trunk->hwidth); i++) 242 KASSERT(LIST_EMPTY(&trunk->hash[i]), 243 ("%s: hash table not empty", __func__)); 244 #endif 245 free(trunk->hash, M_VLAN); 246 trunk->hash = NULL; 247 trunk->hwidth = trunk->hmask = 0; 248 } 249 250 static int 251 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 252 { 253 int i, b; 254 struct ifvlan *ifv2; 255 256 TRUNK_LOCK_ASSERT(trunk); 257 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 258 259 b = 1 << trunk->hwidth; 260 i = HASH(ifv->ifv_tag, trunk->hmask); 261 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 262 if (ifv->ifv_tag == ifv2->ifv_tag) 263 return (EEXIST); 264 265 /* 266 * Grow the hash when the number of vlans exceeds half of the number of 267 * hash buckets squared. This will make the average linked-list length 268 * buckets/2. 269 */ 270 if (trunk->refcnt > (b * b) / 2) { 271 vlan_growhash(trunk, 1); 272 i = HASH(ifv->ifv_tag, trunk->hmask); 273 } 274 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 275 trunk->refcnt++; 276 277 return (0); 278 } 279 280 static int 281 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 282 { 283 int i, b; 284 struct ifvlan *ifv2; 285 286 TRUNK_LOCK_ASSERT(trunk); 287 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 288 289 b = 1 << trunk->hwidth; 290 i = HASH(ifv->ifv_tag, trunk->hmask); 291 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 292 if (ifv2 == ifv) { 293 trunk->refcnt--; 294 LIST_REMOVE(ifv2, ifv_list); 295 if (trunk->refcnt < (b * b) / 2) 296 vlan_growhash(trunk, -1); 297 return (0); 298 } 299 300 panic("%s: vlan not found\n", __func__); 301 return (ENOENT); /*NOTREACHED*/ 302 } 303 304 /* 305 * Grow the hash larger or smaller if memory permits. 306 */ 307 static void 308 vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 309 { 310 struct ifvlan *ifv; 311 struct ifvlanhead *hash2; 312 int hwidth2, i, j, n, n2; 313 314 TRUNK_LOCK_ASSERT(trunk); 315 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 316 317 if (howmuch == 0) { 318 /* Harmless yet obvious coding error */ 319 printf("%s: howmuch is 0\n", __func__); 320 return; 321 } 322 323 hwidth2 = trunk->hwidth + howmuch; 324 n = 1 << trunk->hwidth; 325 n2 = 1 << hwidth2; 326 /* Do not shrink the table below the default */ 327 if (hwidth2 < VLAN_DEF_HWIDTH) 328 return; 329 330 /* M_NOWAIT because we're called with trunk mutex held */ 331 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT); 332 if (hash2 == NULL) { 333 printf("%s: out of memory -- hash size not changed\n", 334 __func__); 335 return; /* We can live with the old hash table */ 336 } 337 for (j = 0; j < n2; j++) 338 LIST_INIT(&hash2[j]); 339 for (i = 0; i < n; i++) 340 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) { 341 LIST_REMOVE(ifv, ifv_list); 342 j = HASH(ifv->ifv_tag, n2 - 1); 343 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 344 } 345 free(trunk->hash, M_VLAN); 346 trunk->hash = hash2; 347 trunk->hwidth = hwidth2; 348 trunk->hmask = n2 - 1; 349 350 if (bootverbose) 351 if_printf(trunk->parent, 352 "VLAN hash table resized from %d to %d buckets\n", n, n2); 353 } 354 355 static __inline struct ifvlan * 356 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag) 357 { 358 struct ifvlan *ifv; 359 360 TRUNK_LOCK_RASSERT(trunk); 361 362 LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list) 363 if (ifv->ifv_tag == tag) 364 return (ifv); 365 return (NULL); 366 } 367 368 #if 0 369 /* Debugging code to view the hashtables. */ 370 static void 371 vlan_dumphash(struct ifvlantrunk *trunk) 372 { 373 int i; 374 struct ifvlan *ifv; 375 376 for (i = 0; i < (1 << trunk->hwidth); i++) { 377 printf("%d: ", i); 378 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 379 printf("%s ", ifv->ifv_ifp->if_xname); 380 printf("\n"); 381 } 382 } 383 #endif /* 0 */ 384 #endif /* !VLAN_ARRAY */ 385 386 static void 387 trunk_destroy(struct ifvlantrunk *trunk) 388 { 389 VLAN_LOCK_ASSERT(); 390 391 TRUNK_LOCK(trunk); 392 #ifndef VLAN_ARRAY 393 vlan_freehash(trunk); 394 #endif 395 trunk->parent->if_vlantrunk = NULL; 396 TRUNK_UNLOCK(trunk); 397 TRUNK_LOCK_DESTROY(trunk); 398 free(trunk, M_VLAN); 399 } 400 401 /* 402 * Program our multicast filter. What we're actually doing is 403 * programming the multicast filter of the parent. This has the 404 * side effect of causing the parent interface to receive multicast 405 * traffic that it doesn't really want, which ends up being discarded 406 * later by the upper protocol layers. Unfortunately, there's no way 407 * to avoid this: there really is only one physical interface. 408 * 409 * XXX: There is a possible race here if more than one thread is 410 * modifying the multicast state of the vlan interface at the same time. 411 */ 412 static int 413 vlan_setmulti(struct ifnet *ifp) 414 { 415 struct ifnet *ifp_p; 416 struct ifmultiaddr *ifma, *rifma = NULL; 417 struct ifvlan *sc; 418 struct vlan_mc_entry *mc; 419 struct sockaddr_dl sdl; 420 int error; 421 422 /*VLAN_LOCK_ASSERT();*/ 423 424 /* Find the parent. */ 425 sc = ifp->if_softc; 426 ifp_p = PARENT(sc); 427 428 CURVNET_SET_QUIET(ifp_p->if_vnet); 429 430 bzero((char *)&sdl, sizeof(sdl)); 431 sdl.sdl_len = sizeof(sdl); 432 sdl.sdl_family = AF_LINK; 433 sdl.sdl_index = ifp_p->if_index; 434 sdl.sdl_type = IFT_ETHER; 435 sdl.sdl_alen = ETHER_ADDR_LEN; 436 437 /* First, remove any existing filter entries. */ 438 while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) { 439 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 440 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl); 441 if (error) 442 return (error); 443 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 444 free(mc, M_VLAN); 445 } 446 447 /* Now program new ones. */ 448 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 449 if (ifma->ifma_addr->sa_family != AF_LINK) 450 continue; 451 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 452 if (mc == NULL) 453 return (ENOMEM); 454 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 455 (char *)&mc->mc_addr, ETHER_ADDR_LEN); 456 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 457 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 458 LLADDR(&sdl), ETHER_ADDR_LEN); 459 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 460 if (error) 461 return (error); 462 } 463 464 CURVNET_RESTORE(); 465 return (0); 466 } 467 468 /* 469 * A handler for parent interface link layer address changes. 470 * If the parent interface link layer address is changed we 471 * should also change it on all children vlans. 472 */ 473 static void 474 vlan_iflladdr(void *arg __unused, struct ifnet *ifp) 475 { 476 struct ifvlan *ifv; 477 #ifndef VLAN_ARRAY 478 struct ifvlan *next; 479 #endif 480 int i; 481 482 /* 483 * Check if it's a trunk interface first of all 484 * to avoid needless locking. 485 */ 486 if (ifp->if_vlantrunk == NULL) 487 return; 488 489 VLAN_LOCK(); 490 /* 491 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it. 492 */ 493 #ifdef VLAN_ARRAY 494 for (i = 0; i < VLAN_ARRAY_SIZE; i++) 495 if ((ifv = ifp->if_vlantrunk->vlans[i])) { 496 #else /* VLAN_ARRAY */ 497 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 498 LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) { 499 #endif /* VLAN_ARRAY */ 500 VLAN_UNLOCK(); 501 if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN); 502 VLAN_LOCK(); 503 } 504 VLAN_UNLOCK(); 505 506 } 507 508 /* 509 * A handler for network interface departure events. 510 * Track departure of trunks here so that we don't access invalid 511 * pointers or whatever if a trunk is ripped from under us, e.g., 512 * by ejecting its hot-plug card. However, if an ifnet is simply 513 * being renamed, then there's no need to tear down the state. 514 */ 515 static void 516 vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 517 { 518 struct ifvlan *ifv; 519 int i; 520 521 /* 522 * Check if it's a trunk interface first of all 523 * to avoid needless locking. 524 */ 525 if (ifp->if_vlantrunk == NULL) 526 return; 527 528 /* If the ifnet is just being renamed, don't do anything. */ 529 if (ifp->if_flags & IFF_RENAMING) 530 return; 531 532 VLAN_LOCK(); 533 /* 534 * OK, it's a trunk. Loop over and detach all vlan's on it. 535 * Check trunk pointer after each vlan_unconfig() as it will 536 * free it and set to NULL after the last vlan was detached. 537 */ 538 #ifdef VLAN_ARRAY 539 for (i = 0; i < VLAN_ARRAY_SIZE; i++) 540 if ((ifv = ifp->if_vlantrunk->vlans[i])) { 541 vlan_unconfig_locked(ifv->ifv_ifp); 542 if (ifp->if_vlantrunk == NULL) 543 break; 544 } 545 #else /* VLAN_ARRAY */ 546 restart: 547 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 548 if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) { 549 vlan_unconfig_locked(ifv->ifv_ifp); 550 if (ifp->if_vlantrunk) 551 goto restart; /* trunk->hwidth can change */ 552 else 553 break; 554 } 555 #endif /* VLAN_ARRAY */ 556 /* Trunk should have been destroyed in vlan_unconfig(). */ 557 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 558 VLAN_UNLOCK(); 559 } 560 561 /* 562 * VLAN support can be loaded as a module. The only place in the 563 * system that's intimately aware of this is ether_input. We hook 564 * into this code through vlan_input_p which is defined there and 565 * set here. Noone else in the system should be aware of this so 566 * we use an explicit reference here. 567 */ 568 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 569 570 /* For if_link_state_change() eyes only... */ 571 extern void (*vlan_link_state_p)(struct ifnet *); 572 573 static int 574 vlan_modevent(module_t mod, int type, void *data) 575 { 576 577 switch (type) { 578 case MOD_LOAD: 579 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 580 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 581 if (ifdetach_tag == NULL) 582 return (ENOMEM); 583 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event, 584 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 585 if (iflladdr_tag == NULL) 586 return (ENOMEM); 587 VLAN_LOCK_INIT(); 588 vlan_input_p = vlan_input; 589 vlan_link_state_p = vlan_link_state; 590 vlan_trunk_cap_p = vlan_trunk_capabilities; 591 if_clone_attach(&vlan_cloner); 592 if (bootverbose) 593 printf("vlan: initialized, using " 594 #ifdef VLAN_ARRAY 595 "full-size arrays" 596 #else 597 "hash tables with chaining" 598 #endif 599 600 "\n"); 601 break; 602 case MOD_UNLOAD: 603 if_clone_detach(&vlan_cloner); 604 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 605 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag); 606 vlan_input_p = NULL; 607 vlan_link_state_p = NULL; 608 vlan_trunk_cap_p = NULL; 609 VLAN_LOCK_DESTROY(); 610 if (bootverbose) 611 printf("vlan: unloaded\n"); 612 break; 613 default: 614 return (EOPNOTSUPP); 615 } 616 return (0); 617 } 618 619 static moduledata_t vlan_mod = { 620 "if_vlan", 621 vlan_modevent, 622 0 623 }; 624 625 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 626 MODULE_VERSION(if_vlan, 3); 627 628 static struct ifnet * 629 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag) 630 { 631 const char *cp; 632 struct ifnet *ifp; 633 int t; 634 635 /* Check for <etherif>.<vlan> style interface names. */ 636 IFNET_RLOCK_NOSLEEP(); 637 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 638 if (ifp->if_type != IFT_ETHER) 639 continue; 640 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0) 641 continue; 642 cp = name + strlen(ifp->if_xname); 643 if (*cp++ != '.') 644 continue; 645 if (*cp == '\0') 646 continue; 647 t = 0; 648 for(; *cp >= '0' && *cp <= '9'; cp++) 649 t = (t * 10) + (*cp - '0'); 650 if (*cp != '\0') 651 continue; 652 if (tag != NULL) 653 *tag = t; 654 break; 655 } 656 IFNET_RUNLOCK_NOSLEEP(); 657 658 return (ifp); 659 } 660 661 static int 662 vlan_clone_match(struct if_clone *ifc, const char *name) 663 { 664 const char *cp; 665 666 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL) 667 return (1); 668 669 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0) 670 return (0); 671 for (cp = name + 4; *cp != '\0'; cp++) { 672 if (*cp < '0' || *cp > '9') 673 return (0); 674 } 675 676 return (1); 677 } 678 679 static int 680 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 681 { 682 char *dp; 683 int wildcard; 684 int unit; 685 int error; 686 int tag; 687 int ethertag; 688 struct ifvlan *ifv; 689 struct ifnet *ifp; 690 struct ifnet *p; 691 struct ifaddr *ifa; 692 struct sockaddr_dl *sdl; 693 struct vlanreq vlr; 694 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 695 696 /* 697 * There are 3 (ugh) ways to specify the cloned device: 698 * o pass a parameter block with the clone request. 699 * o specify parameters in the text of the clone device name 700 * o specify no parameters and get an unattached device that 701 * must be configured separately. 702 * The first technique is preferred; the latter two are 703 * supported for backwards compatibilty. 704 */ 705 if (params) { 706 error = copyin(params, &vlr, sizeof(vlr)); 707 if (error) 708 return error; 709 p = ifunit(vlr.vlr_parent); 710 if (p == NULL) 711 return ENXIO; 712 /* 713 * Don't let the caller set up a VLAN tag with 714 * anything except VLID bits. 715 */ 716 if (vlr.vlr_tag & ~EVL_VLID_MASK) 717 return (EINVAL); 718 error = ifc_name2unit(name, &unit); 719 if (error != 0) 720 return (error); 721 722 ethertag = 1; 723 tag = vlr.vlr_tag; 724 wildcard = (unit < 0); 725 } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) { 726 ethertag = 1; 727 unit = -1; 728 wildcard = 0; 729 730 /* 731 * Don't let the caller set up a VLAN tag with 732 * anything except VLID bits. 733 */ 734 if (tag & ~EVL_VLID_MASK) 735 return (EINVAL); 736 } else { 737 ethertag = 0; 738 739 error = ifc_name2unit(name, &unit); 740 if (error != 0) 741 return (error); 742 743 wildcard = (unit < 0); 744 } 745 746 error = ifc_alloc_unit(ifc, &unit); 747 if (error != 0) 748 return (error); 749 750 /* In the wildcard case, we need to update the name. */ 751 if (wildcard) { 752 for (dp = name; *dp != '\0'; dp++); 753 if (snprintf(dp, len - (dp-name), "%d", unit) > 754 len - (dp-name) - 1) { 755 panic("%s: interface name too long", __func__); 756 } 757 } 758 759 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 760 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 761 if (ifp == NULL) { 762 ifc_free_unit(ifc, unit); 763 free(ifv, M_VLAN); 764 return (ENOSPC); 765 } 766 SLIST_INIT(&ifv->vlan_mc_listhead); 767 768 ifp->if_softc = ifv; 769 /* 770 * Set the name manually rather than using if_initname because 771 * we don't conform to the default naming convention for interfaces. 772 */ 773 strlcpy(ifp->if_xname, name, IFNAMSIZ); 774 ifp->if_dname = ifc->ifc_name; 775 ifp->if_dunit = unit; 776 /* NB: flags are not set here */ 777 ifp->if_linkmib = &ifv->ifv_mib; 778 ifp->if_linkmiblen = sizeof(ifv->ifv_mib); 779 /* NB: mtu is not set here */ 780 781 ifp->if_init = vlan_init; 782 ifp->if_start = vlan_start; 783 ifp->if_ioctl = vlan_ioctl; 784 ifp->if_snd.ifq_maxlen = ifqmaxlen; 785 ifp->if_flags = VLAN_IFFLAGS; 786 ether_ifattach(ifp, eaddr); 787 /* Now undo some of the damage... */ 788 ifp->if_baudrate = 0; 789 ifp->if_type = IFT_L2VLAN; 790 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 791 ifa = ifp->if_addr; 792 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 793 sdl->sdl_type = IFT_L2VLAN; 794 795 if (ethertag) { 796 error = vlan_config(ifv, p, tag); 797 if (error != 0) { 798 /* 799 * Since we've partialy failed, we need to back 800 * out all the way, otherwise userland could get 801 * confused. Thus, we destroy the interface. 802 */ 803 ether_ifdetach(ifp); 804 vlan_unconfig(ifp); 805 if_free_type(ifp, IFT_ETHER); 806 ifc_free_unit(ifc, unit); 807 free(ifv, M_VLAN); 808 809 return (error); 810 } 811 812 /* Update flags on the parent, if necessary. */ 813 vlan_setflags(ifp, 1); 814 } 815 816 return (0); 817 } 818 819 static int 820 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 821 { 822 struct ifvlan *ifv = ifp->if_softc; 823 int unit = ifp->if_dunit; 824 825 ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 826 vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 827 if_free_type(ifp, IFT_ETHER); 828 free(ifv, M_VLAN); 829 ifc_free_unit(ifc, unit); 830 831 return (0); 832 } 833 834 /* 835 * The ifp->if_init entry point for vlan(4) is a no-op. 836 */ 837 static void 838 vlan_init(void *foo __unused) 839 { 840 } 841 842 /* 843 * The if_start method for vlan(4) interface. It doesn't 844 * raises the IFF_DRV_OACTIVE flag, since it is called 845 * only from IFQ_HANDOFF() macro in ether_output_frame(). 846 * If the interface queue is full, and vlan_start() is 847 * not called, the queue would never get emptied and 848 * interface would stall forever. 849 */ 850 static void 851 vlan_start(struct ifnet *ifp) 852 { 853 struct ifvlan *ifv; 854 struct ifnet *p; 855 struct mbuf *m; 856 int error; 857 858 ifv = ifp->if_softc; 859 p = PARENT(ifv); 860 861 for (;;) { 862 IF_DEQUEUE(&ifp->if_snd, m); 863 if (m == NULL) 864 break; 865 BPF_MTAP(ifp, m); 866 867 /* 868 * Do not run parent's if_start() if the parent is not up, 869 * or parent's driver will cause a system crash. 870 */ 871 if (!UP_AND_RUNNING(p)) { 872 m_freem(m); 873 ifp->if_collisions++; 874 continue; 875 } 876 877 /* 878 * Pad the frame to the minimum size allowed if told to. 879 * This option is in accord with IEEE Std 802.1Q, 2003 Ed., 880 * paragraph C.4.4.3.b. It can help to work around buggy 881 * bridges that violate paragraph C.4.4.3.a from the same 882 * document, i.e., fail to pad short frames after untagging. 883 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but 884 * untagging it will produce a 62-byte frame, which is a runt 885 * and requires padding. There are VLAN-enabled network 886 * devices that just discard such runts instead or mishandle 887 * them somehow. 888 */ 889 if (soft_pad) { 890 static char pad[8]; /* just zeros */ 891 int n; 892 893 for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len; 894 n > 0; n -= sizeof(pad)) 895 if (!m_append(m, min(n, sizeof(pad)), pad)) 896 break; 897 898 if (n > 0) { 899 if_printf(ifp, "cannot pad short frame\n"); 900 ifp->if_oerrors++; 901 m_freem(m); 902 continue; 903 } 904 } 905 906 /* 907 * If underlying interface can do VLAN tag insertion itself, 908 * just pass the packet along. However, we need some way to 909 * tell the interface where the packet came from so that it 910 * knows how to find the VLAN tag to use, so we attach a 911 * packet tag that holds it. 912 */ 913 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) { 914 m->m_pkthdr.ether_vtag = ifv->ifv_tag; 915 m->m_flags |= M_VLANTAG; 916 } else { 917 m = ether_vlanencap(m, ifv->ifv_tag); 918 if (m == NULL) { 919 if_printf(ifp, 920 "unable to prepend VLAN header\n"); 921 ifp->if_oerrors++; 922 continue; 923 } 924 } 925 926 /* 927 * Send it, precisely as ether_output() would have. 928 * We are already running at splimp. 929 */ 930 error = (p->if_transmit)(p, m); 931 if (!error) 932 ifp->if_opackets++; 933 else 934 ifp->if_oerrors++; 935 } 936 } 937 938 static void 939 vlan_input(struct ifnet *ifp, struct mbuf *m) 940 { 941 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 942 struct ifvlan *ifv; 943 uint16_t tag; 944 945 KASSERT(trunk != NULL, ("%s: no trunk", __func__)); 946 947 if (m->m_flags & M_VLANTAG) { 948 /* 949 * Packet is tagged, but m contains a normal 950 * Ethernet frame; the tag is stored out-of-band. 951 */ 952 tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag); 953 m->m_flags &= ~M_VLANTAG; 954 } else { 955 struct ether_vlan_header *evl; 956 957 /* 958 * Packet is tagged in-band as specified by 802.1q. 959 */ 960 switch (ifp->if_type) { 961 case IFT_ETHER: 962 if (m->m_len < sizeof(*evl) && 963 (m = m_pullup(m, sizeof(*evl))) == NULL) { 964 if_printf(ifp, "cannot pullup VLAN header\n"); 965 return; 966 } 967 evl = mtod(m, struct ether_vlan_header *); 968 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 969 970 /* 971 * Remove the 802.1q header by copying the Ethernet 972 * addresses over it and adjusting the beginning of 973 * the data in the mbuf. The encapsulated Ethernet 974 * type field is already in place. 975 */ 976 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 977 ETHER_HDR_LEN - ETHER_TYPE_LEN); 978 m_adj(m, ETHER_VLAN_ENCAP_LEN); 979 break; 980 981 default: 982 #ifdef INVARIANTS 983 panic("%s: %s has unsupported if_type %u", 984 __func__, ifp->if_xname, ifp->if_type); 985 #endif 986 m_freem(m); 987 ifp->if_noproto++; 988 return; 989 } 990 } 991 992 TRUNK_RLOCK(trunk); 993 #ifdef VLAN_ARRAY 994 ifv = trunk->vlans[tag]; 995 #else 996 ifv = vlan_gethash(trunk, tag); 997 #endif 998 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 999 TRUNK_RUNLOCK(trunk); 1000 m_freem(m); 1001 ifp->if_noproto++; 1002 return; 1003 } 1004 TRUNK_RUNLOCK(trunk); 1005 1006 m->m_pkthdr.rcvif = ifv->ifv_ifp; 1007 ifv->ifv_ifp->if_ipackets++; 1008 1009 /* Pass it back through the parent's input routine. */ 1010 (*ifp->if_input)(ifv->ifv_ifp, m); 1011 } 1012 1013 static int 1014 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag) 1015 { 1016 struct ifvlantrunk *trunk; 1017 struct ifnet *ifp; 1018 int error = 0; 1019 1020 /* VID numbers 0x0 and 0xFFF are reserved */ 1021 if (tag == 0 || tag == 0xFFF) 1022 return (EINVAL); 1023 if (p->if_type != IFT_ETHER) 1024 return (EPROTONOSUPPORT); 1025 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 1026 return (EPROTONOSUPPORT); 1027 if (ifv->ifv_trunk) 1028 return (EBUSY); 1029 1030 if (p->if_vlantrunk == NULL) { 1031 trunk = malloc(sizeof(struct ifvlantrunk), 1032 M_VLAN, M_WAITOK | M_ZERO); 1033 #ifndef VLAN_ARRAY 1034 vlan_inithash(trunk); 1035 #endif 1036 VLAN_LOCK(); 1037 if (p->if_vlantrunk != NULL) { 1038 /* A race that that is very unlikely to be hit. */ 1039 #ifndef VLAN_ARRAY 1040 vlan_freehash(trunk); 1041 #endif 1042 free(trunk, M_VLAN); 1043 goto exists; 1044 } 1045 TRUNK_LOCK_INIT(trunk); 1046 TRUNK_LOCK(trunk); 1047 p->if_vlantrunk = trunk; 1048 trunk->parent = p; 1049 } else { 1050 VLAN_LOCK(); 1051 exists: 1052 trunk = p->if_vlantrunk; 1053 TRUNK_LOCK(trunk); 1054 } 1055 1056 ifv->ifv_tag = tag; /* must set this before vlan_inshash() */ 1057 #ifdef VLAN_ARRAY 1058 if (trunk->vlans[tag] != NULL) { 1059 error = EEXIST; 1060 goto done; 1061 } 1062 trunk->vlans[tag] = ifv; 1063 trunk->refcnt++; 1064 #else 1065 error = vlan_inshash(trunk, ifv); 1066 if (error) 1067 goto done; 1068 #endif 1069 ifv->ifv_proto = ETHERTYPE_VLAN; 1070 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1071 ifv->ifv_mintu = ETHERMIN; 1072 ifv->ifv_pflags = 0; 1073 1074 /* 1075 * If the parent supports the VLAN_MTU capability, 1076 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1077 * use it. 1078 */ 1079 if (p->if_capenable & IFCAP_VLAN_MTU) { 1080 /* 1081 * No need to fudge the MTU since the parent can 1082 * handle extended frames. 1083 */ 1084 ifv->ifv_mtufudge = 0; 1085 } else { 1086 /* 1087 * Fudge the MTU by the encapsulation size. This 1088 * makes us incompatible with strictly compliant 1089 * 802.1Q implementations, but allows us to use 1090 * the feature with other NetBSD implementations, 1091 * which might still be useful. 1092 */ 1093 ifv->ifv_mtufudge = ifv->ifv_encaplen; 1094 } 1095 1096 ifv->ifv_trunk = trunk; 1097 ifp = ifv->ifv_ifp; 1098 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 1099 ifp->if_baudrate = p->if_baudrate; 1100 /* 1101 * Copy only a selected subset of flags from the parent. 1102 * Other flags are none of our business. 1103 */ 1104 #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 1105 ifp->if_flags &= ~VLAN_COPY_FLAGS; 1106 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 1107 #undef VLAN_COPY_FLAGS 1108 1109 ifp->if_link_state = p->if_link_state; 1110 1111 vlan_capabilities(ifv); 1112 1113 /* 1114 * Set up our ``Ethernet address'' to reflect the underlying 1115 * physical interface's. 1116 */ 1117 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN); 1118 1119 /* 1120 * Configure multicast addresses that may already be 1121 * joined on the vlan device. 1122 */ 1123 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */ 1124 1125 /* We are ready for operation now. */ 1126 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1127 done: 1128 TRUNK_UNLOCK(trunk); 1129 if (error == 0) 1130 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag); 1131 VLAN_UNLOCK(); 1132 1133 return (error); 1134 } 1135 1136 static void 1137 vlan_unconfig(struct ifnet *ifp) 1138 { 1139 1140 VLAN_LOCK(); 1141 vlan_unconfig_locked(ifp); 1142 VLAN_UNLOCK(); 1143 } 1144 1145 static void 1146 vlan_unconfig_locked(struct ifnet *ifp) 1147 { 1148 struct ifvlantrunk *trunk; 1149 struct vlan_mc_entry *mc; 1150 struct ifvlan *ifv; 1151 struct ifnet *parent; 1152 1153 VLAN_LOCK_ASSERT(); 1154 1155 ifv = ifp->if_softc; 1156 trunk = ifv->ifv_trunk; 1157 parent = NULL; 1158 1159 if (trunk != NULL) { 1160 struct sockaddr_dl sdl; 1161 1162 TRUNK_LOCK(trunk); 1163 parent = trunk->parent; 1164 1165 /* 1166 * Since the interface is being unconfigured, we need to 1167 * empty the list of multicast groups that we may have joined 1168 * while we were alive from the parent's list. 1169 */ 1170 bzero((char *)&sdl, sizeof(sdl)); 1171 sdl.sdl_len = sizeof(sdl); 1172 sdl.sdl_family = AF_LINK; 1173 sdl.sdl_index = parent->if_index; 1174 sdl.sdl_type = IFT_ETHER; 1175 sdl.sdl_alen = ETHER_ADDR_LEN; 1176 1177 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 1178 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), 1179 ETHER_ADDR_LEN); 1180 1181 /* 1182 * This may fail if the parent interface is 1183 * being detached. Regardless, we should do a 1184 * best effort to free this interface as much 1185 * as possible as all callers expect vlan 1186 * destruction to succeed. 1187 */ 1188 (void)if_delmulti(parent, (struct sockaddr *)&sdl); 1189 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 1190 free(mc, M_VLAN); 1191 } 1192 1193 vlan_setflags(ifp, 0); /* clear special flags on parent */ 1194 #ifdef VLAN_ARRAY 1195 trunk->vlans[ifv->ifv_tag] = NULL; 1196 trunk->refcnt--; 1197 #else 1198 vlan_remhash(trunk, ifv); 1199 #endif 1200 ifv->ifv_trunk = NULL; 1201 1202 /* 1203 * Check if we were the last. 1204 */ 1205 if (trunk->refcnt == 0) { 1206 trunk->parent->if_vlantrunk = NULL; 1207 /* 1208 * XXXGL: If some ithread has already entered 1209 * vlan_input() and is now blocked on the trunk 1210 * lock, then it should preempt us right after 1211 * unlock and finish its work. Then we will acquire 1212 * lock again in trunk_destroy(). 1213 */ 1214 TRUNK_UNLOCK(trunk); 1215 trunk_destroy(trunk); 1216 } else 1217 TRUNK_UNLOCK(trunk); 1218 } 1219 1220 /* Disconnect from parent. */ 1221 if (ifv->ifv_pflags) 1222 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 1223 ifp->if_mtu = ETHERMTU; 1224 ifp->if_link_state = LINK_STATE_UNKNOWN; 1225 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1226 1227 /* 1228 * Only dispatch an event if vlan was 1229 * attached, otherwise there is nothing 1230 * to cleanup anyway. 1231 */ 1232 if (parent != NULL) 1233 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag); 1234 } 1235 1236 /* Handle a reference counted flag that should be set on the parent as well */ 1237 static int 1238 vlan_setflag(struct ifnet *ifp, int flag, int status, 1239 int (*func)(struct ifnet *, int)) 1240 { 1241 struct ifvlan *ifv; 1242 int error; 1243 1244 /* XXX VLAN_LOCK_ASSERT(); */ 1245 1246 ifv = ifp->if_softc; 1247 status = status ? (ifp->if_flags & flag) : 0; 1248 /* Now "status" contains the flag value or 0 */ 1249 1250 /* 1251 * See if recorded parent's status is different from what 1252 * we want it to be. If it is, flip it. We record parent's 1253 * status in ifv_pflags so that we won't clear parent's flag 1254 * we haven't set. In fact, we don't clear or set parent's 1255 * flags directly, but get or release references to them. 1256 * That's why we can be sure that recorded flags still are 1257 * in accord with actual parent's flags. 1258 */ 1259 if (status != (ifv->ifv_pflags & flag)) { 1260 error = (*func)(PARENT(ifv), status); 1261 if (error) 1262 return (error); 1263 ifv->ifv_pflags &= ~flag; 1264 ifv->ifv_pflags |= status; 1265 } 1266 return (0); 1267 } 1268 1269 /* 1270 * Handle IFF_* flags that require certain changes on the parent: 1271 * if "status" is true, update parent's flags respective to our if_flags; 1272 * if "status" is false, forcedly clear the flags set on parent. 1273 */ 1274 static int 1275 vlan_setflags(struct ifnet *ifp, int status) 1276 { 1277 int error, i; 1278 1279 for (i = 0; vlan_pflags[i].flag; i++) { 1280 error = vlan_setflag(ifp, vlan_pflags[i].flag, 1281 status, vlan_pflags[i].func); 1282 if (error) 1283 return (error); 1284 } 1285 return (0); 1286 } 1287 1288 /* Inform all vlans that their parent has changed link state */ 1289 static void 1290 vlan_link_state(struct ifnet *ifp) 1291 { 1292 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1293 struct ifvlan *ifv; 1294 int i; 1295 1296 TRUNK_LOCK(trunk); 1297 #ifdef VLAN_ARRAY 1298 for (i = 0; i < VLAN_ARRAY_SIZE; i++) 1299 if (trunk->vlans[i] != NULL) { 1300 ifv = trunk->vlans[i]; 1301 #else 1302 for (i = 0; i < (1 << trunk->hwidth); i++) 1303 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) { 1304 #endif 1305 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1306 if_link_state_change(ifv->ifv_ifp, 1307 trunk->parent->if_link_state); 1308 } 1309 TRUNK_UNLOCK(trunk); 1310 } 1311 1312 static void 1313 vlan_capabilities(struct ifvlan *ifv) 1314 { 1315 struct ifnet *p = PARENT(ifv); 1316 struct ifnet *ifp = ifv->ifv_ifp; 1317 1318 TRUNK_LOCK_ASSERT(TRUNK(ifv)); 1319 1320 /* 1321 * If the parent interface can do checksum offloading 1322 * on VLANs, then propagate its hardware-assisted 1323 * checksumming flags. Also assert that checksum 1324 * offloading requires hardware VLAN tagging. 1325 */ 1326 if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1327 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM; 1328 1329 if (p->if_capenable & IFCAP_VLAN_HWCSUM && 1330 p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1331 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM; 1332 ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP | 1333 CSUM_UDP | CSUM_SCTP | CSUM_IP_FRAGS | CSUM_FRAGMENT); 1334 } else { 1335 ifp->if_capenable = 0; 1336 ifp->if_hwassist = 0; 1337 } 1338 /* 1339 * If the parent interface can do TSO on VLANs then 1340 * propagate the hardware-assisted flag. TSO on VLANs 1341 * does not necessarily require hardware VLAN tagging. 1342 */ 1343 if (p->if_capabilities & IFCAP_VLAN_HWTSO) 1344 ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO; 1345 if (p->if_capenable & IFCAP_VLAN_HWTSO) { 1346 ifp->if_capenable |= p->if_capenable & IFCAP_TSO; 1347 ifp->if_hwassist |= p->if_hwassist & CSUM_TSO; 1348 } else { 1349 ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO); 1350 ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO); 1351 } 1352 } 1353 1354 static void 1355 vlan_trunk_capabilities(struct ifnet *ifp) 1356 { 1357 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1358 struct ifvlan *ifv; 1359 int i; 1360 1361 TRUNK_LOCK(trunk); 1362 #ifdef VLAN_ARRAY 1363 for (i = 0; i < VLAN_ARRAY_SIZE; i++) 1364 if (trunk->vlans[i] != NULL) { 1365 ifv = trunk->vlans[i]; 1366 #else 1367 for (i = 0; i < (1 << trunk->hwidth); i++) { 1368 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 1369 #endif 1370 vlan_capabilities(ifv); 1371 } 1372 TRUNK_UNLOCK(trunk); 1373 } 1374 1375 static int 1376 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1377 { 1378 struct ifnet *p; 1379 struct ifreq *ifr; 1380 struct ifvlan *ifv; 1381 struct vlanreq vlr; 1382 int error = 0; 1383 1384 ifr = (struct ifreq *)data; 1385 ifv = ifp->if_softc; 1386 1387 switch (cmd) { 1388 case SIOCGIFMEDIA: 1389 VLAN_LOCK(); 1390 if (TRUNK(ifv) != NULL) { 1391 p = PARENT(ifv); 1392 VLAN_UNLOCK(); 1393 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data); 1394 /* Limit the result to the parent's current config. */ 1395 if (error == 0) { 1396 struct ifmediareq *ifmr; 1397 1398 ifmr = (struct ifmediareq *)data; 1399 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1400 ifmr->ifm_count = 1; 1401 error = copyout(&ifmr->ifm_current, 1402 ifmr->ifm_ulist, 1403 sizeof(int)); 1404 } 1405 } 1406 } else { 1407 VLAN_UNLOCK(); 1408 error = EINVAL; 1409 } 1410 break; 1411 1412 case SIOCSIFMEDIA: 1413 error = EINVAL; 1414 break; 1415 1416 case SIOCSIFMTU: 1417 /* 1418 * Set the interface MTU. 1419 */ 1420 VLAN_LOCK(); 1421 if (TRUNK(ifv) != NULL) { 1422 if (ifr->ifr_mtu > 1423 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1424 ifr->ifr_mtu < 1425 (ifv->ifv_mintu - ifv->ifv_mtufudge)) 1426 error = EINVAL; 1427 else 1428 ifp->if_mtu = ifr->ifr_mtu; 1429 } else 1430 error = EINVAL; 1431 VLAN_UNLOCK(); 1432 break; 1433 1434 case SIOCSETVLAN: 1435 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr)); 1436 if (error) 1437 break; 1438 if (vlr.vlr_parent[0] == '\0') { 1439 vlan_unconfig(ifp); 1440 break; 1441 } 1442 p = ifunit(vlr.vlr_parent); 1443 if (p == NULL) { 1444 error = ENOENT; 1445 break; 1446 } 1447 /* 1448 * Don't let the caller set up a VLAN tag with 1449 * anything except VLID bits. 1450 */ 1451 if (vlr.vlr_tag & ~EVL_VLID_MASK) { 1452 error = EINVAL; 1453 break; 1454 } 1455 error = vlan_config(ifv, p, vlr.vlr_tag); 1456 if (error) 1457 break; 1458 1459 /* Update flags on the parent, if necessary. */ 1460 vlan_setflags(ifp, 1); 1461 break; 1462 1463 case SIOCGETVLAN: 1464 bzero(&vlr, sizeof(vlr)); 1465 VLAN_LOCK(); 1466 if (TRUNK(ifv) != NULL) { 1467 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 1468 sizeof(vlr.vlr_parent)); 1469 vlr.vlr_tag = ifv->ifv_tag; 1470 } 1471 VLAN_UNLOCK(); 1472 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 1473 break; 1474 1475 case SIOCSIFFLAGS: 1476 /* 1477 * We should propagate selected flags to the parent, 1478 * e.g., promiscuous mode. 1479 */ 1480 if (TRUNK(ifv) != NULL) 1481 error = vlan_setflags(ifp, 1); 1482 break; 1483 1484 case SIOCADDMULTI: 1485 case SIOCDELMULTI: 1486 /* 1487 * If we don't have a parent, just remember the membership for 1488 * when we do. 1489 */ 1490 if (TRUNK(ifv) != NULL) 1491 error = vlan_setmulti(ifp); 1492 break; 1493 1494 default: 1495 error = ether_ioctl(ifp, cmd, data); 1496 } 1497 1498 return (error); 1499 } 1500