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 * $FreeBSD$ 30 */ 31 32 /* 33 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 34 * Might be extended some day to also handle IEEE 802.1p priority 35 * tagging. This is sort of sneaky in the implementation, since 36 * we need to pretend to be enough of an Ethernet implementation 37 * to make arp work. The way we do this is by telling everyone 38 * that we are an Ethernet, and then catch the packets that 39 * ether_output() left on our output queue when it calls 40 * if_start(), rewrite them for use by the real outgoing interface, 41 * and ask it to send them. 42 */ 43 44 #include "opt_route.h" 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 #include <sys/vimage.h> 60 61 #include <net/bpf.h> 62 #include <net/ethernet.h> 63 #include <net/if.h> 64 #include <net/if_clone.h> 65 #include <net/if_dl.h> 66 #include <net/if_types.h> 67 #include <net/if_vlan_var.h> 68 #include <net/route.h> 69 #include <net/vnet.h> 70 71 #define VLANNAME "vlan" 72 #define VLAN_DEF_HWIDTH 4 73 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 74 75 #define UP_AND_RUNNING(ifp) \ 76 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING) 77 78 LIST_HEAD(ifvlanhead, ifvlan); 79 80 struct ifvlantrunk { 81 struct ifnet *parent; /* parent interface of this trunk */ 82 struct rwlock rw; 83 #ifdef VLAN_ARRAY 84 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1) 85 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */ 86 #else 87 struct ifvlanhead *hash; /* dynamic hash-list table */ 88 uint16_t hmask; 89 uint16_t hwidth; 90 #endif 91 int refcnt; 92 }; 93 94 struct vlan_mc_entry { 95 struct ether_addr mc_addr; 96 SLIST_ENTRY(vlan_mc_entry) mc_entries; 97 }; 98 99 struct ifvlan { 100 struct ifvlantrunk *ifv_trunk; 101 struct ifnet *ifv_ifp; 102 #define TRUNK(ifv) ((ifv)->ifv_trunk) 103 #define PARENT(ifv) ((ifv)->ifv_trunk->parent) 104 int ifv_pflags; /* special flags we have set on parent */ 105 struct ifv_linkmib { 106 int ifvm_encaplen; /* encapsulation length */ 107 int ifvm_mtufudge; /* MTU fudged by this much */ 108 int ifvm_mintu; /* min transmission unit */ 109 uint16_t ifvm_proto; /* encapsulation ethertype */ 110 uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 111 } ifv_mib; 112 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 113 #ifndef VLAN_ARRAY 114 LIST_ENTRY(ifvlan) ifv_list; 115 #endif 116 }; 117 #define ifv_proto ifv_mib.ifvm_proto 118 #define ifv_tag ifv_mib.ifvm_tag 119 #define ifv_encaplen ifv_mib.ifvm_encaplen 120 #define ifv_mtufudge ifv_mib.ifvm_mtufudge 121 #define ifv_mintu ifv_mib.ifvm_mintu 122 123 /* Special flags we should propagate to parent. */ 124 static struct { 125 int flag; 126 int (*func)(struct ifnet *, int); 127 } vlan_pflags[] = { 128 {IFF_PROMISC, ifpromisc}, 129 {IFF_ALLMULTI, if_allmulti}, 130 {0, NULL} 131 }; 132 133 SYSCTL_DECL(_net_link); 134 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 135 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 136 137 static int soft_pad = 0; 138 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0, 139 "pad short frames before tagging"); 140 141 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface"); 142 143 static eventhandler_tag ifdetach_tag; 144 145 /* 146 * We have a global mutex, that is used to serialize configuration 147 * changes and isn't used in normal packet delivery. 148 * 149 * We also have a per-trunk rwlock, that is locked shared on packet 150 * processing and exclusive when configuration is changed. 151 * 152 * The VLAN_ARRAY substitutes the dynamic hash with a static array 153 * with 4096 entries. In theory this can give a boost in processing, 154 * however on practice it does not. Probably this is because array 155 * is too big to fit into CPU cache. 156 */ 157 static struct mtx ifv_mtx; 158 #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF) 159 #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx) 160 #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED) 161 #define VLAN_LOCK() mtx_lock(&ifv_mtx) 162 #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx) 163 #define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME) 164 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw) 165 #define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw) 166 #define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw) 167 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED) 168 #define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw) 169 #define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw) 170 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED) 171 172 #ifndef VLAN_ARRAY 173 static void vlan_inithash(struct ifvlantrunk *trunk); 174 static void vlan_freehash(struct ifvlantrunk *trunk); 175 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 176 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 177 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 178 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 179 uint16_t tag); 180 #endif 181 static void trunk_destroy(struct ifvlantrunk *trunk); 182 183 static void vlan_start(struct ifnet *ifp); 184 static void vlan_init(void *foo); 185 static void vlan_input(struct ifnet *ifp, struct mbuf *m); 186 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 187 static int vlan_setflag(struct ifnet *ifp, int flag, int status, 188 int (*func)(struct ifnet *, int)); 189 static int vlan_setflags(struct ifnet *ifp, int status); 190 static int vlan_setmulti(struct ifnet *ifp); 191 static int vlan_unconfig(struct ifnet *ifp); 192 static int vlan_unconfig_locked(struct ifnet *ifp); 193 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag); 194 static void vlan_link_state(struct ifnet *ifp, int link); 195 static void vlan_capabilities(struct ifvlan *ifv); 196 static void vlan_trunk_capabilities(struct ifnet *ifp); 197 198 static struct ifnet *vlan_clone_match_ethertag(struct if_clone *, 199 const char *, int *); 200 static int vlan_clone_match(struct if_clone *, const char *); 201 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t); 202 static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 203 204 static void vlan_ifdetach(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 network interface departure events. 470 * Track departure of trunks here so that we don't access invalid 471 * pointers or whatever if a trunk is ripped from under us, e.g., 472 * by ejecting its hot-plug card. 473 */ 474 static void 475 vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 476 { 477 struct ifvlan *ifv; 478 int i; 479 480 /* 481 * Check if it's a trunk interface first of all 482 * to avoid needless locking. 483 */ 484 if (ifp->if_vlantrunk == NULL) 485 return; 486 487 VLAN_LOCK(); 488 /* 489 * OK, it's a trunk. Loop over and detach all vlan's on it. 490 * Check trunk pointer after each vlan_unconfig() as it will 491 * free it and set to NULL after the last vlan was detached. 492 */ 493 #ifdef VLAN_ARRAY 494 for (i = 0; i < VLAN_ARRAY_SIZE; i++) 495 if ((ifv = ifp->if_vlantrunk->vlans[i])) { 496 vlan_unconfig_locked(ifv->ifv_ifp); 497 if (ifp->if_vlantrunk == NULL) 498 break; 499 } 500 #else /* VLAN_ARRAY */ 501 restart: 502 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++) 503 if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) { 504 vlan_unconfig_locked(ifv->ifv_ifp); 505 if (ifp->if_vlantrunk) 506 goto restart; /* trunk->hwidth can change */ 507 else 508 break; 509 } 510 #endif /* VLAN_ARRAY */ 511 /* Trunk should have been destroyed in vlan_unconfig(). */ 512 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__)); 513 VLAN_UNLOCK(); 514 } 515 516 /* 517 * VLAN support can be loaded as a module. The only place in the 518 * system that's intimately aware of this is ether_input. We hook 519 * into this code through vlan_input_p which is defined there and 520 * set here. Noone else in the system should be aware of this so 521 * we use an explicit reference here. 522 */ 523 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 524 525 /* For if_link_state_change() eyes only... */ 526 extern void (*vlan_link_state_p)(struct ifnet *, int); 527 528 static int 529 vlan_modevent(module_t mod, int type, void *data) 530 { 531 532 switch (type) { 533 case MOD_LOAD: 534 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 535 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY); 536 if (ifdetach_tag == NULL) 537 return (ENOMEM); 538 VLAN_LOCK_INIT(); 539 vlan_input_p = vlan_input; 540 vlan_link_state_p = vlan_link_state; 541 vlan_trunk_cap_p = vlan_trunk_capabilities; 542 if_clone_attach(&vlan_cloner); 543 if (bootverbose) 544 printf("vlan: initialized, using " 545 #ifdef VLAN_ARRAY 546 "full-size arrays" 547 #else 548 "hash tables with chaining" 549 #endif 550 551 "\n"); 552 break; 553 case MOD_UNLOAD: 554 if_clone_detach(&vlan_cloner); 555 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag); 556 vlan_input_p = NULL; 557 vlan_link_state_p = NULL; 558 vlan_trunk_cap_p = NULL; 559 VLAN_LOCK_DESTROY(); 560 if (bootverbose) 561 printf("vlan: unloaded\n"); 562 break; 563 default: 564 return (EOPNOTSUPP); 565 } 566 return (0); 567 } 568 569 static moduledata_t vlan_mod = { 570 "if_vlan", 571 vlan_modevent, 572 0 573 }; 574 575 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 576 MODULE_VERSION(if_vlan, 3); 577 578 static struct ifnet * 579 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag) 580 { 581 INIT_VNET_NET(curvnet); 582 const char *cp; 583 struct ifnet *ifp; 584 int t = 0; 585 586 /* Check for <etherif>.<vlan> style interface names. */ 587 IFNET_RLOCK(); 588 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 589 if (ifp->if_type != IFT_ETHER) 590 continue; 591 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0) 592 continue; 593 cp = name + strlen(ifp->if_xname); 594 if (*cp != '.') 595 continue; 596 for(; *cp != '\0'; cp++) { 597 if (*cp < '0' || *cp > '9') 598 continue; 599 t = (t * 10) + (*cp - '0'); 600 } 601 if (tag != NULL) 602 *tag = t; 603 break; 604 } 605 IFNET_RUNLOCK(); 606 607 return (ifp); 608 } 609 610 static int 611 vlan_clone_match(struct if_clone *ifc, const char *name) 612 { 613 const char *cp; 614 615 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL) 616 return (1); 617 618 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0) 619 return (0); 620 for (cp = name + 4; *cp != '\0'; cp++) { 621 if (*cp < '0' || *cp > '9') 622 return (0); 623 } 624 625 return (1); 626 } 627 628 static int 629 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 630 { 631 char *dp; 632 int wildcard; 633 int unit; 634 int error; 635 int tag; 636 int ethertag; 637 struct ifvlan *ifv; 638 struct ifnet *ifp; 639 struct ifnet *p; 640 struct vlanreq vlr; 641 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */ 642 643 /* 644 * There are 3 (ugh) ways to specify the cloned device: 645 * o pass a parameter block with the clone request. 646 * o specify parameters in the text of the clone device name 647 * o specify no parameters and get an unattached device that 648 * must be configured separately. 649 * The first technique is preferred; the latter two are 650 * supported for backwards compatibilty. 651 */ 652 if (params) { 653 error = copyin(params, &vlr, sizeof(vlr)); 654 if (error) 655 return error; 656 p = ifunit(vlr.vlr_parent); 657 if (p == NULL) 658 return ENXIO; 659 /* 660 * Don't let the caller set up a VLAN tag with 661 * anything except VLID bits. 662 */ 663 if (vlr.vlr_tag & ~EVL_VLID_MASK) 664 return (EINVAL); 665 error = ifc_name2unit(name, &unit); 666 if (error != 0) 667 return (error); 668 669 ethertag = 1; 670 tag = vlr.vlr_tag; 671 wildcard = (unit < 0); 672 } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) { 673 ethertag = 1; 674 unit = -1; 675 wildcard = 0; 676 677 /* 678 * Don't let the caller set up a VLAN tag with 679 * anything except VLID bits. 680 */ 681 if (tag & ~EVL_VLID_MASK) 682 return (EINVAL); 683 } else { 684 ethertag = 0; 685 686 error = ifc_name2unit(name, &unit); 687 if (error != 0) 688 return (error); 689 690 wildcard = (unit < 0); 691 } 692 693 error = ifc_alloc_unit(ifc, &unit); 694 if (error != 0) 695 return (error); 696 697 /* In the wildcard case, we need to update the name. */ 698 if (wildcard) { 699 for (dp = name; *dp != '\0'; dp++); 700 if (snprintf(dp, len - (dp-name), "%d", unit) > 701 len - (dp-name) - 1) { 702 panic("%s: interface name too long", __func__); 703 } 704 } 705 706 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 707 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 708 if (ifp == NULL) { 709 ifc_free_unit(ifc, unit); 710 free(ifv, M_VLAN); 711 return (ENOSPC); 712 } 713 SLIST_INIT(&ifv->vlan_mc_listhead); 714 715 ifp->if_softc = ifv; 716 /* 717 * Set the name manually rather than using if_initname because 718 * we don't conform to the default naming convention for interfaces. 719 */ 720 strlcpy(ifp->if_xname, name, IFNAMSIZ); 721 ifp->if_dname = ifc->ifc_name; 722 ifp->if_dunit = unit; 723 /* NB: flags are not set here */ 724 ifp->if_linkmib = &ifv->ifv_mib; 725 ifp->if_linkmiblen = sizeof(ifv->ifv_mib); 726 /* NB: mtu is not set here */ 727 728 ifp->if_init = vlan_init; 729 ifp->if_start = vlan_start; 730 ifp->if_ioctl = vlan_ioctl; 731 ifp->if_snd.ifq_maxlen = ifqmaxlen; 732 ifp->if_flags = VLAN_IFFLAGS; 733 ether_ifattach(ifp, eaddr); 734 /* Now undo some of the damage... */ 735 ifp->if_baudrate = 0; 736 ifp->if_type = IFT_L2VLAN; 737 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 738 739 if (ethertag) { 740 error = vlan_config(ifv, p, tag); 741 if (error != 0) { 742 /* 743 * Since we've partialy failed, we need to back 744 * out all the way, otherwise userland could get 745 * confused. Thus, we destroy the interface. 746 */ 747 ether_ifdetach(ifp); 748 vlan_unconfig(ifp); 749 if_free_type(ifp, IFT_ETHER); 750 ifc_free_unit(ifc, unit); 751 free(ifv, M_VLAN); 752 753 return (error); 754 } 755 756 /* Update flags on the parent, if necessary. */ 757 vlan_setflags(ifp, 1); 758 } 759 760 return (0); 761 } 762 763 static int 764 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 765 { 766 struct ifvlan *ifv = ifp->if_softc; 767 int unit = ifp->if_dunit; 768 769 ether_ifdetach(ifp); /* first, remove it from system-wide lists */ 770 vlan_unconfig(ifp); /* now it can be unconfigured and freed */ 771 if_free_type(ifp, IFT_ETHER); 772 free(ifv, M_VLAN); 773 ifc_free_unit(ifc, unit); 774 775 return (0); 776 } 777 778 /* 779 * The ifp->if_init entry point for vlan(4) is a no-op. 780 */ 781 static void 782 vlan_init(void *foo __unused) 783 { 784 } 785 786 /* 787 * The if_start method for vlan(4) interface. It doesn't 788 * raises the IFF_DRV_OACTIVE flag, since it is called 789 * only from IFQ_HANDOFF() macro in ether_output_frame(). 790 * If the interface queue is full, and vlan_start() is 791 * not called, the queue would never get emptied and 792 * interface would stall forever. 793 */ 794 static void 795 vlan_start(struct ifnet *ifp) 796 { 797 struct ifvlan *ifv; 798 struct ifnet *p; 799 struct mbuf *m; 800 int error; 801 802 ifv = ifp->if_softc; 803 p = PARENT(ifv); 804 805 for (;;) { 806 IF_DEQUEUE(&ifp->if_snd, m); 807 if (m == NULL) 808 break; 809 BPF_MTAP(ifp, m); 810 811 /* 812 * Do not run parent's if_start() if the parent is not up, 813 * or parent's driver will cause a system crash. 814 */ 815 if (!UP_AND_RUNNING(p)) { 816 m_freem(m); 817 ifp->if_collisions++; 818 continue; 819 } 820 821 /* 822 * Pad the frame to the minimum size allowed if told to. 823 * This option is in accord with IEEE Std 802.1Q, 2003 Ed., 824 * paragraph C.4.4.3.b. It can help to work around buggy 825 * bridges that violate paragraph C.4.4.3.a from the same 826 * document, i.e., fail to pad short frames after untagging. 827 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but 828 * untagging it will produce a 62-byte frame, which is a runt 829 * and requires padding. There are VLAN-enabled network 830 * devices that just discard such runts instead or mishandle 831 * them somehow. 832 */ 833 if (soft_pad) { 834 static char pad[8]; /* just zeros */ 835 int n; 836 837 for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len; 838 n > 0; n -= sizeof(pad)) 839 if (!m_append(m, min(n, sizeof(pad)), pad)) 840 break; 841 842 if (n > 0) { 843 if_printf(ifp, "cannot pad short frame\n"); 844 ifp->if_oerrors++; 845 m_freem(m); 846 continue; 847 } 848 } 849 850 /* 851 * If underlying interface can do VLAN tag insertion itself, 852 * just pass the packet along. However, we need some way to 853 * tell the interface where the packet came from so that it 854 * knows how to find the VLAN tag to use, so we attach a 855 * packet tag that holds it. 856 */ 857 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) { 858 m->m_pkthdr.ether_vtag = ifv->ifv_tag; 859 m->m_flags |= M_VLANTAG; 860 } else { 861 m = ether_vlanencap(m, ifv->ifv_tag); 862 if (m == NULL) { 863 if_printf(ifp, 864 "unable to prepend VLAN header\n"); 865 ifp->if_oerrors++; 866 continue; 867 } 868 } 869 870 /* 871 * Send it, precisely as ether_output() would have. 872 * We are already running at splimp. 873 */ 874 error = (p->if_transmit)(p, m); 875 if (!error) 876 ifp->if_opackets++; 877 else 878 ifp->if_oerrors++; 879 } 880 } 881 882 static void 883 vlan_input(struct ifnet *ifp, struct mbuf *m) 884 { 885 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 886 struct ifvlan *ifv; 887 uint16_t tag; 888 889 KASSERT(trunk != NULL, ("%s: no trunk", __func__)); 890 891 if (m->m_flags & M_VLANTAG) { 892 /* 893 * Packet is tagged, but m contains a normal 894 * Ethernet frame; the tag is stored out-of-band. 895 */ 896 tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag); 897 m->m_flags &= ~M_VLANTAG; 898 } else { 899 struct ether_vlan_header *evl; 900 901 /* 902 * Packet is tagged in-band as specified by 802.1q. 903 */ 904 switch (ifp->if_type) { 905 case IFT_ETHER: 906 if (m->m_len < sizeof(*evl) && 907 (m = m_pullup(m, sizeof(*evl))) == NULL) { 908 if_printf(ifp, "cannot pullup VLAN header\n"); 909 return; 910 } 911 evl = mtod(m, struct ether_vlan_header *); 912 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 913 914 /* 915 * Remove the 802.1q header by copying the Ethernet 916 * addresses over it and adjusting the beginning of 917 * the data in the mbuf. The encapsulated Ethernet 918 * type field is already in place. 919 */ 920 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 921 ETHER_HDR_LEN - ETHER_TYPE_LEN); 922 m_adj(m, ETHER_VLAN_ENCAP_LEN); 923 break; 924 925 default: 926 #ifdef INVARIANTS 927 panic("%s: %s has unsupported if_type %u", 928 __func__, ifp->if_xname, ifp->if_type); 929 #endif 930 m_freem(m); 931 ifp->if_noproto++; 932 return; 933 } 934 } 935 936 TRUNK_RLOCK(trunk); 937 #ifdef VLAN_ARRAY 938 ifv = trunk->vlans[tag]; 939 #else 940 ifv = vlan_gethash(trunk, tag); 941 #endif 942 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) { 943 TRUNK_RUNLOCK(trunk); 944 m_freem(m); 945 ifp->if_noproto++; 946 return; 947 } 948 TRUNK_RUNLOCK(trunk); 949 950 m->m_pkthdr.rcvif = ifv->ifv_ifp; 951 ifv->ifv_ifp->if_ipackets++; 952 953 /* Pass it back through the parent's input routine. */ 954 (*ifp->if_input)(ifv->ifv_ifp, m); 955 } 956 957 static int 958 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag) 959 { 960 struct ifvlantrunk *trunk; 961 struct ifnet *ifp; 962 int error = 0; 963 964 /* VID numbers 0x0 and 0xFFF are reserved */ 965 if (tag == 0 || tag == 0xFFF) 966 return (EINVAL); 967 if (p->if_type != IFT_ETHER) 968 return (EPROTONOSUPPORT); 969 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 970 return (EPROTONOSUPPORT); 971 if (ifv->ifv_trunk) 972 return (EBUSY); 973 974 if (p->if_vlantrunk == NULL) { 975 trunk = malloc(sizeof(struct ifvlantrunk), 976 M_VLAN, M_WAITOK | M_ZERO); 977 #ifndef VLAN_ARRAY 978 vlan_inithash(trunk); 979 #endif 980 VLAN_LOCK(); 981 if (p->if_vlantrunk != NULL) { 982 /* A race that that is very unlikely to be hit. */ 983 #ifndef VLAN_ARRAY 984 vlan_freehash(trunk); 985 #endif 986 free(trunk, M_VLAN); 987 goto exists; 988 } 989 TRUNK_LOCK_INIT(trunk); 990 TRUNK_LOCK(trunk); 991 p->if_vlantrunk = trunk; 992 trunk->parent = p; 993 } else { 994 VLAN_LOCK(); 995 exists: 996 trunk = p->if_vlantrunk; 997 TRUNK_LOCK(trunk); 998 } 999 1000 ifv->ifv_tag = tag; /* must set this before vlan_inshash() */ 1001 #ifdef VLAN_ARRAY 1002 if (trunk->vlans[tag] != NULL) { 1003 error = EEXIST; 1004 goto done; 1005 } 1006 trunk->vlans[tag] = ifv; 1007 trunk->refcnt++; 1008 #else 1009 error = vlan_inshash(trunk, ifv); 1010 if (error) 1011 goto done; 1012 #endif 1013 ifv->ifv_proto = ETHERTYPE_VLAN; 1014 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 1015 ifv->ifv_mintu = ETHERMIN; 1016 ifv->ifv_pflags = 0; 1017 1018 /* 1019 * If the parent supports the VLAN_MTU capability, 1020 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 1021 * use it. 1022 */ 1023 if (p->if_capenable & IFCAP_VLAN_MTU) { 1024 /* 1025 * No need to fudge the MTU since the parent can 1026 * handle extended frames. 1027 */ 1028 ifv->ifv_mtufudge = 0; 1029 } else { 1030 /* 1031 * Fudge the MTU by the encapsulation size. This 1032 * makes us incompatible with strictly compliant 1033 * 802.1Q implementations, but allows us to use 1034 * the feature with other NetBSD implementations, 1035 * which might still be useful. 1036 */ 1037 ifv->ifv_mtufudge = ifv->ifv_encaplen; 1038 } 1039 1040 ifv->ifv_trunk = trunk; 1041 ifp = ifv->ifv_ifp; 1042 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 1043 ifp->if_baudrate = p->if_baudrate; 1044 /* 1045 * Copy only a selected subset of flags from the parent. 1046 * Other flags are none of our business. 1047 */ 1048 #define VLAN_COPY_FLAGS (IFF_SIMPLEX) 1049 ifp->if_flags &= ~VLAN_COPY_FLAGS; 1050 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 1051 #undef VLAN_COPY_FLAGS 1052 1053 ifp->if_link_state = p->if_link_state; 1054 1055 vlan_capabilities(ifv); 1056 1057 /* 1058 * Set up our ``Ethernet address'' to reflect the underlying 1059 * physical interface's. 1060 */ 1061 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN); 1062 1063 /* 1064 * Configure multicast addresses that may already be 1065 * joined on the vlan device. 1066 */ 1067 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */ 1068 1069 /* We are ready for operation now. */ 1070 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1071 done: 1072 TRUNK_UNLOCK(trunk); 1073 if (error == 0) 1074 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag); 1075 VLAN_UNLOCK(); 1076 1077 return (error); 1078 } 1079 1080 static int 1081 vlan_unconfig(struct ifnet *ifp) 1082 { 1083 int ret; 1084 1085 VLAN_LOCK(); 1086 ret = vlan_unconfig_locked(ifp); 1087 VLAN_UNLOCK(); 1088 return (ret); 1089 } 1090 1091 static int 1092 vlan_unconfig_locked(struct ifnet *ifp) 1093 { 1094 struct ifvlantrunk *trunk; 1095 struct vlan_mc_entry *mc; 1096 struct ifvlan *ifv; 1097 struct ifnet *parent; 1098 int error; 1099 1100 VLAN_LOCK_ASSERT(); 1101 1102 ifv = ifp->if_softc; 1103 trunk = ifv->ifv_trunk; 1104 parent = NULL; 1105 1106 if (trunk != NULL) { 1107 struct sockaddr_dl sdl; 1108 1109 TRUNK_LOCK(trunk); 1110 parent = trunk->parent; 1111 1112 /* 1113 * Since the interface is being unconfigured, we need to 1114 * empty the list of multicast groups that we may have joined 1115 * while we were alive from the parent's list. 1116 */ 1117 bzero((char *)&sdl, sizeof(sdl)); 1118 sdl.sdl_len = sizeof(sdl); 1119 sdl.sdl_family = AF_LINK; 1120 sdl.sdl_index = parent->if_index; 1121 sdl.sdl_type = IFT_ETHER; 1122 sdl.sdl_alen = ETHER_ADDR_LEN; 1123 1124 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 1125 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), 1126 ETHER_ADDR_LEN); 1127 error = if_delmulti(parent, (struct sockaddr *)&sdl); 1128 if (error) 1129 return (error); 1130 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 1131 free(mc, M_VLAN); 1132 } 1133 1134 vlan_setflags(ifp, 0); /* clear special flags on parent */ 1135 #ifdef VLAN_ARRAY 1136 trunk->vlans[ifv->ifv_tag] = NULL; 1137 trunk->refcnt--; 1138 #else 1139 vlan_remhash(trunk, ifv); 1140 #endif 1141 ifv->ifv_trunk = NULL; 1142 1143 /* 1144 * Check if we were the last. 1145 */ 1146 if (trunk->refcnt == 0) { 1147 trunk->parent->if_vlantrunk = NULL; 1148 /* 1149 * XXXGL: If some ithread has already entered 1150 * vlan_input() and is now blocked on the trunk 1151 * lock, then it should preempt us right after 1152 * unlock and finish its work. Then we will acquire 1153 * lock again in trunk_destroy(). 1154 */ 1155 TRUNK_UNLOCK(trunk); 1156 trunk_destroy(trunk); 1157 } else 1158 TRUNK_UNLOCK(trunk); 1159 } 1160 1161 /* Disconnect from parent. */ 1162 if (ifv->ifv_pflags) 1163 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 1164 ifp->if_mtu = ETHERMTU; 1165 ifp->if_link_state = LINK_STATE_UNKNOWN; 1166 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1167 1168 /* 1169 * Only dispatch an event if vlan was 1170 * attached, otherwise there is nothing 1171 * to cleanup anyway. 1172 */ 1173 if (parent != NULL) 1174 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag); 1175 1176 return (0); 1177 } 1178 1179 /* Handle a reference counted flag that should be set on the parent as well */ 1180 static int 1181 vlan_setflag(struct ifnet *ifp, int flag, int status, 1182 int (*func)(struct ifnet *, int)) 1183 { 1184 struct ifvlan *ifv; 1185 int error; 1186 1187 /* XXX VLAN_LOCK_ASSERT(); */ 1188 1189 ifv = ifp->if_softc; 1190 status = status ? (ifp->if_flags & flag) : 0; 1191 /* Now "status" contains the flag value or 0 */ 1192 1193 /* 1194 * See if recorded parent's status is different from what 1195 * we want it to be. If it is, flip it. We record parent's 1196 * status in ifv_pflags so that we won't clear parent's flag 1197 * we haven't set. In fact, we don't clear or set parent's 1198 * flags directly, but get or release references to them. 1199 * That's why we can be sure that recorded flags still are 1200 * in accord with actual parent's flags. 1201 */ 1202 if (status != (ifv->ifv_pflags & flag)) { 1203 error = (*func)(PARENT(ifv), status); 1204 if (error) 1205 return (error); 1206 ifv->ifv_pflags &= ~flag; 1207 ifv->ifv_pflags |= status; 1208 } 1209 return (0); 1210 } 1211 1212 /* 1213 * Handle IFF_* flags that require certain changes on the parent: 1214 * if "status" is true, update parent's flags respective to our if_flags; 1215 * if "status" is false, forcedly clear the flags set on parent. 1216 */ 1217 static int 1218 vlan_setflags(struct ifnet *ifp, int status) 1219 { 1220 int error, i; 1221 1222 for (i = 0; vlan_pflags[i].flag; i++) { 1223 error = vlan_setflag(ifp, vlan_pflags[i].flag, 1224 status, vlan_pflags[i].func); 1225 if (error) 1226 return (error); 1227 } 1228 return (0); 1229 } 1230 1231 /* Inform all vlans that their parent has changed link state */ 1232 static void 1233 vlan_link_state(struct ifnet *ifp, int link) 1234 { 1235 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1236 struct ifvlan *ifv; 1237 int i; 1238 1239 TRUNK_LOCK(trunk); 1240 #ifdef VLAN_ARRAY 1241 for (i = 0; i < VLAN_ARRAY_SIZE; i++) 1242 if (trunk->vlans[i] != NULL) { 1243 ifv = trunk->vlans[i]; 1244 #else 1245 for (i = 0; i < (1 << trunk->hwidth); i++) 1246 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) { 1247 #endif 1248 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate; 1249 if_link_state_change(ifv->ifv_ifp, 1250 trunk->parent->if_link_state); 1251 } 1252 TRUNK_UNLOCK(trunk); 1253 } 1254 1255 static void 1256 vlan_capabilities(struct ifvlan *ifv) 1257 { 1258 struct ifnet *p = PARENT(ifv); 1259 struct ifnet *ifp = ifv->ifv_ifp; 1260 1261 TRUNK_LOCK_ASSERT(TRUNK(ifv)); 1262 1263 /* 1264 * If the parent interface can do checksum offloading 1265 * on VLANs, then propagate its hardware-assisted 1266 * checksumming flags. Also assert that checksum 1267 * offloading requires hardware VLAN tagging. 1268 */ 1269 if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1270 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM; 1271 1272 if (p->if_capenable & IFCAP_VLAN_HWCSUM && 1273 p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1274 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM; 1275 ifp->if_hwassist = p->if_hwassist; 1276 } else { 1277 ifp->if_capenable = 0; 1278 ifp->if_hwassist = 0; 1279 } 1280 } 1281 1282 static void 1283 vlan_trunk_capabilities(struct ifnet *ifp) 1284 { 1285 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1286 struct ifvlan *ifv; 1287 int i; 1288 1289 TRUNK_LOCK(trunk); 1290 #ifdef VLAN_ARRAY 1291 for (i = 0; i < VLAN_ARRAY_SIZE; i++) 1292 if (trunk->vlans[i] != NULL) { 1293 ifv = trunk->vlans[i]; 1294 #else 1295 for (i = 0; i < (1 << trunk->hwidth); i++) { 1296 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 1297 #endif 1298 vlan_capabilities(ifv); 1299 } 1300 TRUNK_UNLOCK(trunk); 1301 } 1302 1303 static int 1304 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1305 { 1306 struct ifnet *p; 1307 struct ifreq *ifr; 1308 struct ifvlan *ifv; 1309 struct vlanreq vlr; 1310 int error = 0; 1311 1312 ifr = (struct ifreq *)data; 1313 ifv = ifp->if_softc; 1314 1315 switch (cmd) { 1316 case SIOCGIFMEDIA: 1317 VLAN_LOCK(); 1318 if (TRUNK(ifv) != NULL) { 1319 error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv), 1320 SIOCGIFMEDIA, data); 1321 VLAN_UNLOCK(); 1322 /* Limit the result to the parent's current config. */ 1323 if (error == 0) { 1324 struct ifmediareq *ifmr; 1325 1326 ifmr = (struct ifmediareq *)data; 1327 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1328 ifmr->ifm_count = 1; 1329 error = copyout(&ifmr->ifm_current, 1330 ifmr->ifm_ulist, 1331 sizeof(int)); 1332 } 1333 } 1334 } else { 1335 VLAN_UNLOCK(); 1336 error = EINVAL; 1337 } 1338 break; 1339 1340 case SIOCSIFMEDIA: 1341 error = EINVAL; 1342 break; 1343 1344 case SIOCSIFMTU: 1345 /* 1346 * Set the interface MTU. 1347 */ 1348 VLAN_LOCK(); 1349 if (TRUNK(ifv) != NULL) { 1350 if (ifr->ifr_mtu > 1351 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1352 ifr->ifr_mtu < 1353 (ifv->ifv_mintu - ifv->ifv_mtufudge)) 1354 error = EINVAL; 1355 else 1356 ifp->if_mtu = ifr->ifr_mtu; 1357 } else 1358 error = EINVAL; 1359 VLAN_UNLOCK(); 1360 break; 1361 1362 case SIOCSETVLAN: 1363 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr)); 1364 if (error) 1365 break; 1366 if (vlr.vlr_parent[0] == '\0') { 1367 vlan_unconfig(ifp); 1368 break; 1369 } 1370 p = ifunit(vlr.vlr_parent); 1371 if (p == 0) { 1372 error = ENOENT; 1373 break; 1374 } 1375 /* 1376 * Don't let the caller set up a VLAN tag with 1377 * anything except VLID bits. 1378 */ 1379 if (vlr.vlr_tag & ~EVL_VLID_MASK) { 1380 error = EINVAL; 1381 break; 1382 } 1383 error = vlan_config(ifv, p, vlr.vlr_tag); 1384 if (error) 1385 break; 1386 1387 /* Update flags on the parent, if necessary. */ 1388 vlan_setflags(ifp, 1); 1389 break; 1390 1391 case SIOCGETVLAN: 1392 bzero(&vlr, sizeof(vlr)); 1393 VLAN_LOCK(); 1394 if (TRUNK(ifv) != NULL) { 1395 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 1396 sizeof(vlr.vlr_parent)); 1397 vlr.vlr_tag = ifv->ifv_tag; 1398 } 1399 VLAN_UNLOCK(); 1400 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 1401 break; 1402 1403 case SIOCSIFFLAGS: 1404 /* 1405 * We should propagate selected flags to the parent, 1406 * e.g., promiscuous mode. 1407 */ 1408 if (TRUNK(ifv) != NULL) 1409 error = vlan_setflags(ifp, 1); 1410 break; 1411 1412 case SIOCADDMULTI: 1413 case SIOCDELMULTI: 1414 /* 1415 * If we don't have a parent, just remember the membership for 1416 * when we do. 1417 */ 1418 if (TRUNK(ifv) != NULL) 1419 error = vlan_setmulti(ifp); 1420 break; 1421 1422 default: 1423 error = ether_ioctl(ifp, cmd, data); 1424 } 1425 1426 return (error); 1427 } 1428