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