if_vlan.c revision 155114
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: head/sys/net/if_vlan.c 155114 2006-01-31 16:41:05Z yar $ 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_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 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_arp.h> 65#include <net/if_dl.h> 66#include <net/if_types.h> 67#include <net/if_vlan_var.h> 68 69#ifdef INET 70#include <netinet/in.h> 71#include <netinet/if_ether.h> 72#endif 73 74#define VLANNAME "vlan" 75#define VLAN_DEF_HWIDTH 4 76#define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST) 77 78LIST_HEAD(ifvlanhead, ifvlan); 79 80struct ifvlantrunk { 81 struct ifnet *parent; /* parent interface of this trunk */ 82 struct rwlock rw; 83#ifdef VLAN_ARRAY 84 struct ifvlan *vlans[EVL_VLID_MASK+1]; /* static table */ 85#else 86 struct ifvlanhead *hash; /* dynamic hash-list table */ 87 uint16_t hmask; 88 uint16_t hwidth; 89#endif 90 int refcnt; 91 LIST_ENTRY(ifvlantrunk) trunk_entry; 92}; 93static LIST_HEAD(, ifvlantrunk) trunk_list; 94 95struct vlan_mc_entry { 96 struct ether_addr mc_addr; 97 SLIST_ENTRY(vlan_mc_entry) mc_entries; 98}; 99 100struct ifvlan { 101 struct ifvlantrunk *ifv_trunk; 102 struct ifnet *ifv_ifp; 103#define TRUNK(ifv) ((ifv)->ifv_trunk) 104#define PARENT(ifv) ((ifv)->ifv_trunk->parent) 105 int ifv_pflags; /* special flags we have set on parent */ 106 struct ifv_linkmib { 107 int ifvm_parent; 108 int ifvm_encaplen; /* encapsulation length */ 109 int ifvm_mtufudge; /* MTU fudged by this much */ 110 int ifvm_mintu; /* min transmission unit */ 111 uint16_t ifvm_proto; /* encapsulation ethertype */ 112 uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 113 } ifv_mib; 114 SLIST_HEAD(__vlan_mchead, vlan_mc_entry) vlan_mc_listhead; 115 LIST_ENTRY(ifvlan) ifv_list; 116}; 117#define ifv_tag ifv_mib.ifvm_tag 118#define ifv_encaplen ifv_mib.ifvm_encaplen 119#define ifv_mtufudge ifv_mib.ifvm_mtufudge 120#define ifv_mintu ifv_mib.ifvm_mintu 121 122/* Special flags we should propagate to parent. */ 123static struct { 124 int flag; 125 int (*func)(struct ifnet *, int); 126} vlan_pflags[] = { 127 {IFF_PROMISC, ifpromisc}, 128 {IFF_ALLMULTI, if_allmulti}, 129 {0, NULL} 130}; 131 132SYSCTL_DECL(_net_link); 133SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 134SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 135 136static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface"); 137 138/* 139 * We have a global mutex, that is used to serialize configuration 140 * changes and isn't used in normal packet delivery. 141 * 142 * We also have a per-trunk rwlock, that is locked shared on packet 143 * processing and exclusive when configuration is changed. 144 * 145 * The VLAN_ARRAY substitutes the dynamic hash with a static array 146 * with 4096 entries. In theory this can give a boots in processing, 147 * however on practice it does not. Probably this is because array 148 * is too big to fit into CPU cache. 149 */ 150static struct mtx ifv_mtx; 151#define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF) 152#define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx) 153#define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED) 154#define VLAN_LOCK() mtx_lock(&ifv_mtx) 155#define VLAN_UNLOCK() mtx_unlock(&ifv_mtx) 156#define TRUNK_LOCK_INIT(trunk) rw_init(&(trunk)->rw, VLANNAME) 157#define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw) 158#define TRUNK_LOCK(trunk) rw_wlock(&(trunk)->rw) 159#define TRUNK_UNLOCK(trunk) rw_wunlock(&(trunk)->rw) 160#define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED) 161#define TRUNK_RLOCK(trunk) rw_rlock(&(trunk)->rw) 162#define TRUNK_RUNLOCK(trunk) rw_runlock(&(trunk)->rw) 163#define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED) 164 165#ifndef VLAN_ARRAY 166static void vlan_inithash(struct ifvlantrunk *trunk); 167static void vlan_freehash(struct ifvlantrunk *trunk); 168static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 169static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv); 170static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch); 171static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk, 172 uint16_t tag); 173#endif 174static void trunk_destroy(struct ifvlantrunk *trunk); 175 176static void vlan_start(struct ifnet *ifp); 177static void vlan_ifinit(void *foo); 178static void vlan_input(struct ifnet *ifp, struct mbuf *m); 179static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 180static int vlan_setflag(struct ifnet *ifp, int flag, int status, 181 int (*func)(struct ifnet *, int)); 182static int vlan_setflags(struct ifnet *ifp, int status); 183static int vlan_setmulti(struct ifnet *ifp); 184static int vlan_unconfig(struct ifnet *ifp); 185static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag); 186static void vlan_link_state(struct ifnet *ifp, int link); 187static void vlan_capabilities(struct ifvlan *ifv); 188static void vlan_trunk_capabilities(struct ifnet *ifp); 189 190static struct ifnet *vlan_clone_match_ethertag(struct if_clone *, 191 const char *, int *); 192static int vlan_clone_match(struct if_clone *, const char *); 193static int vlan_clone_create(struct if_clone *, char *, size_t); 194static int vlan_clone_destroy(struct if_clone *, struct ifnet *); 195 196static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL, 197 IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy); 198 199#ifndef VLAN_ARRAY 200#define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m)) 201static void 202vlan_inithash(struct ifvlantrunk *trunk) 203{ 204 int i, n; 205 206 /* 207 * The trunk must not be locked here since we call malloc(M_WAITOK). 208 * It is OK in case this function is called before the trunk struct 209 * gets hooked up and becomes visible from other threads. 210 */ 211 212 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL, 213 ("%s: hash already initialized", __func__)); 214 215 trunk->hwidth = VLAN_DEF_HWIDTH; 216 n = 1 << trunk->hwidth; 217 trunk->hmask = n - 1; 218 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK); 219 for (i = 0; i < n; i++) 220 LIST_INIT(&trunk->hash[i]); 221} 222 223static void 224vlan_freehash(struct ifvlantrunk *trunk) 225{ 226#ifdef INVARIANTS 227 int i; 228 229 TRUNK_LOCK_ASSERT(trunk); /* XXX just unhook trunk first? */ 230 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 231 for (i = 0; i < (1 << trunk->hwidth); i++) 232 KASSERT(LIST_EMPTY(&trunk->hash[i]), 233 ("%s: hash table not empty", __func__)); 234#endif 235 free(trunk->hash, M_VLAN); 236 trunk->hash = NULL; 237 trunk->hwidth = trunk->hmask = 0; 238} 239 240static int 241vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 242{ 243 int i, b; 244 struct ifvlan *ifv2; 245 246 TRUNK_LOCK_ASSERT(trunk); 247 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 248 249 b = 1 << trunk->hwidth; 250 i = HASH(ifv->ifv_tag, trunk->hmask); 251 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 252 if (ifv->ifv_tag == ifv2->ifv_tag) 253 return (EEXIST); 254 255 /* 256 * Grow the hash when the number of vlans exceeds half of the number of 257 * hash buckets squared. This will make the average linked-list length 258 * buckets/2. 259 */ 260 if (trunk->refcnt > (b * b) / 2) { 261 vlan_growhash(trunk, 1); 262 i = HASH(ifv->ifv_tag, trunk->hmask); 263 } 264 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 265 trunk->refcnt++; 266 267 return (0); 268} 269 270static int 271vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 272{ 273 int i, b; 274 struct ifvlan *ifv2; 275 276 TRUNK_LOCK_ASSERT(trunk); 277 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 278 279 b = 1 << trunk->hwidth; 280 i = HASH(ifv->ifv_tag, trunk->hmask); 281 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 282 if (ifv2 == ifv) { 283 trunk->refcnt--; 284 LIST_REMOVE(ifv2, ifv_list); 285 if (trunk->refcnt < (b * b) / 2) 286 vlan_growhash(trunk, -1); 287 return (0); 288 } 289 290 panic("%s: vlan not found\n", __func__); 291 return (ENOENT); /*NOTREACHED*/ 292} 293 294/* 295 * Grow the hash larger or smaller if memory permits. 296 */ 297static void 298vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 299{ 300 301 struct ifvlan *ifv; 302 struct ifvlanhead *hash2; 303 int hwidth2, i, j, n, n2; 304 305 TRUNK_LOCK_ASSERT(trunk); 306 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 307 308 if (howmuch == 0) { 309 /* Harmless yet obvious coding error */ 310 printf("%s: howmuch is 0\n", __func__); 311 return; 312 } 313 314 hwidth2 = trunk->hwidth + howmuch; 315 n = 1 << trunk->hwidth; 316 n2 = 1 << hwidth2; 317 /* Do not shrink the table below the default */ 318 if (hwidth2 < VLAN_DEF_HWIDTH) 319 return; 320 321 /* M_NOWAIT because we're called with trunk mutex held */ 322 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT); 323 if (hash2 == NULL) { 324 printf("%s: out of memory -- hash size not changed\n", 325 __func__); 326 return; /* We can live with the old hash table */ 327 } 328 for (j = 0; j < n2; j++) 329 LIST_INIT(&hash2[j]); 330 for (i = 0; i < n; i++) 331 while (!LIST_EMPTY(&trunk->hash[i])) { 332 ifv = LIST_FIRST(&trunk->hash[i]); 333 LIST_REMOVE(ifv, ifv_list); 334 j = HASH(ifv->ifv_tag, n2 - 1); 335 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 336 } 337 free(trunk->hash, M_VLAN); 338 trunk->hash = hash2; 339 trunk->hwidth = hwidth2; 340 trunk->hmask = n2 - 1; 341} 342 343static __inline struct ifvlan * 344vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag) 345{ 346 struct ifvlan *ifv; 347 348 TRUNK_LOCK_RASSERT(trunk); 349 350 LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list) 351 if (ifv->ifv_tag == tag) 352 return (ifv); 353 return (NULL); 354} 355 356#if 0 357/* Debugging code to view the hashtables. */ 358static void 359vlan_dumphash(struct ifvlantrunk *trunk) 360{ 361 int i; 362 struct ifvlan *ifv; 363 364 for (i = 0; i < (1 << trunk->hwidth); i++) { 365 printf("%d: ", i); 366 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 367 printf("%s ", ifv->ifv_ifp->if_xname); 368 printf("\n"); 369 } 370} 371#endif /* 0 */ 372#endif /* !VLAN_ARRAY */ 373 374static void 375trunk_destroy(struct ifvlantrunk *trunk) 376{ 377 VLAN_LOCK_ASSERT(); 378 379 TRUNK_LOCK(trunk); 380#ifndef VLAN_ARRAY 381 vlan_freehash(trunk); 382#endif 383 TRUNK_LOCK_DESTROY(trunk); 384 LIST_REMOVE(trunk, trunk_entry); 385 trunk->parent->if_vlantrunk = NULL; 386 free(trunk, M_VLAN); 387} 388 389/* 390 * Program our multicast filter. What we're actually doing is 391 * programming the multicast filter of the parent. This has the 392 * side effect of causing the parent interface to receive multicast 393 * traffic that it doesn't really want, which ends up being discarded 394 * later by the upper protocol layers. Unfortunately, there's no way 395 * to avoid this: there really is only one physical interface. 396 * 397 * XXX: There is a possible race here if more than one thread is 398 * modifying the multicast state of the vlan interface at the same time. 399 */ 400static int 401vlan_setmulti(struct ifnet *ifp) 402{ 403 struct ifnet *ifp_p; 404 struct ifmultiaddr *ifma, *rifma = NULL; 405 struct ifvlan *sc; 406 struct vlan_mc_entry *mc = NULL; 407 struct sockaddr_dl sdl; 408 int error; 409 410 /*VLAN_LOCK_ASSERT();*/ 411 412 /* Find the parent. */ 413 sc = ifp->if_softc; 414 ifp_p = PARENT(sc); 415 416 bzero((char *)&sdl, sizeof(sdl)); 417 sdl.sdl_len = sizeof(sdl); 418 sdl.sdl_family = AF_LINK; 419 sdl.sdl_index = ifp_p->if_index; 420 sdl.sdl_type = IFT_ETHER; 421 sdl.sdl_alen = ETHER_ADDR_LEN; 422 423 /* First, remove any existing filter entries. */ 424 while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) { 425 mc = SLIST_FIRST(&sc->vlan_mc_listhead); 426 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 427 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl); 428 if (error) 429 return (error); 430 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 431 free(mc, M_VLAN); 432 } 433 434 /* Now program new ones. */ 435 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 436 if (ifma->ifma_addr->sa_family != AF_LINK) 437 continue; 438 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 439 if (mc == NULL) 440 return (ENOMEM); 441 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 442 (char *)&mc->mc_addr, ETHER_ADDR_LEN); 443 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 444 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 445 LLADDR(&sdl), ETHER_ADDR_LEN); 446 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 447 if (error) 448 return (error); 449 } 450 451 return (0); 452} 453 454/* 455 * VLAN support can be loaded as a module. The only place in the 456 * system that's intimately aware of this is ether_input. We hook 457 * into this code through vlan_input_p which is defined there and 458 * set here. Noone else in the system should be aware of this so 459 * we use an explicit reference here. 460 */ 461extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 462 463/* For if_link_state_change() eyes only... */ 464extern void (*vlan_link_state_p)(struct ifnet *, int); 465 466static int 467vlan_modevent(module_t mod, int type, void *data) 468{ 469 470 switch (type) { 471 case MOD_LOAD: 472 LIST_INIT(&trunk_list); 473 VLAN_LOCK_INIT(); 474 vlan_input_p = vlan_input; 475 vlan_link_state_p = vlan_link_state; 476 vlan_trunk_cap_p = vlan_trunk_capabilities; 477 if_clone_attach(&vlan_cloner); 478 break; 479 case MOD_UNLOAD: 480 { 481 struct ifvlantrunk *trunk, *trunk1; 482 483 if_clone_detach(&vlan_cloner); 484 vlan_input_p = NULL; 485 vlan_link_state_p = NULL; 486 vlan_trunk_cap_p = NULL; 487 VLAN_LOCK(); 488 LIST_FOREACH_SAFE(trunk, &trunk_list, trunk_entry, trunk1) 489 trunk_destroy(trunk); 490 VLAN_UNLOCK(); 491 VLAN_LOCK_DESTROY(); 492 break; 493 } 494 default: 495 return (EOPNOTSUPP); 496 } 497 return (0); 498} 499 500static moduledata_t vlan_mod = { 501 "if_vlan", 502 vlan_modevent, 503 0 504}; 505 506DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 507MODULE_DEPEND(if_vlan, miibus, 1, 1, 1); 508 509static struct ifnet * 510vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag) 511{ 512 const char *cp; 513 struct ifnet *ifp; 514 int t = 0; 515 516 /* Check for <etherif>.<vlan> style interface names. */ 517 IFNET_RLOCK(); 518 TAILQ_FOREACH(ifp, &ifnet, if_link) { 519 if (ifp->if_type != IFT_ETHER) 520 continue; 521 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0) 522 continue; 523 cp = name + strlen(ifp->if_xname); 524 if (*cp != '.') 525 continue; 526 for(; *cp != '\0'; cp++) { 527 if (*cp < '0' || *cp > '9') 528 continue; 529 t = (t * 10) + (*cp - '0'); 530 } 531 if (tag != NULL) 532 *tag = t; 533 break; 534 } 535 IFNET_RUNLOCK(); 536 537 return (ifp); 538} 539 540static int 541vlan_clone_match(struct if_clone *ifc, const char *name) 542{ 543 const char *cp; 544 545 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL) 546 return (1); 547 548 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0) 549 return (0); 550 for (cp = name + 4; *cp != '\0'; cp++) { 551 if (*cp < '0' || *cp > '9') 552 return (0); 553 } 554 555 return (1); 556} 557 558static int 559vlan_clone_create(struct if_clone *ifc, char *name, size_t len) 560{ 561 char *dp; 562 int wildcard; 563 int unit; 564 int error; 565 int tag; 566 int ethertag; 567 struct ifvlan *ifv; 568 struct ifnet *ifp; 569 struct ifnet *p; 570 u_char eaddr[6] = {0,0,0,0,0,0}; 571 572 if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) { 573 ethertag = 1; 574 unit = -1; 575 wildcard = 0; 576 577 /* 578 * Don't let the caller set up a VLAN tag with 579 * anything except VLID bits. 580 */ 581 if (tag & ~EVL_VLID_MASK) 582 return (EINVAL); 583 } else { 584 ethertag = 0; 585 586 error = ifc_name2unit(name, &unit); 587 if (error != 0) 588 return (error); 589 590 wildcard = (unit < 0); 591 } 592 593 error = ifc_alloc_unit(ifc, &unit); 594 if (error != 0) 595 return (error); 596 597 /* In the wildcard case, we need to update the name. */ 598 if (wildcard) { 599 for (dp = name; *dp != '\0'; dp++); 600 if (snprintf(dp, len - (dp-name), "%d", unit) > 601 len - (dp-name) - 1) { 602 panic("%s: interface name too long", __func__); 603 } 604 } 605 606 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 607 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER); 608 if (ifp == NULL) { 609 ifc_free_unit(ifc, unit); 610 free(ifv, M_VLAN); 611 return (ENOSPC); 612 } 613 SLIST_INIT(&ifv->vlan_mc_listhead); 614 615 ifp->if_softc = ifv; 616 /* 617 * Set the name manually rather than using if_initname because 618 * we don't conform to the default naming convention for interfaces. 619 */ 620 strlcpy(ifp->if_xname, name, IFNAMSIZ); 621 ifp->if_dname = ifc->ifc_name; 622 ifp->if_dunit = unit; 623 /* NB: flags are not set here */ 624 ifp->if_linkmib = &ifv->ifv_mib; 625 ifp->if_linkmiblen = sizeof(ifv->ifv_mib); 626 /* NB: mtu is not set here */ 627 628 ifp->if_init = vlan_ifinit; 629 ifp->if_start = vlan_start; 630 ifp->if_ioctl = vlan_ioctl; 631 ifp->if_snd.ifq_maxlen = ifqmaxlen; 632 ifp->if_flags = VLAN_IFFLAGS; 633 ether_ifattach(ifp, eaddr); 634 /* Now undo some of the damage... */ 635 ifp->if_baudrate = 0; 636 ifp->if_type = IFT_L2VLAN; 637 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 638 639 if (ethertag) { 640 error = vlan_config(ifv, p, tag); 641 if (error != 0) { 642 /* 643 * Since we've partialy failed, we need to back 644 * out all the way, otherwise userland could get 645 * confused. Thus, we destroy the interface. 646 */ 647 vlan_unconfig(ifp); 648 ether_ifdetach(ifp); 649 if_free_type(ifp, IFT_ETHER); 650 free(ifv, M_VLAN); 651 652 return (error); 653 } 654 ifp->if_drv_flags |= IFF_DRV_RUNNING; 655 656 /* Update flags on the parent, if necessary. */ 657 vlan_setflags(ifp, 1); 658 } 659 660 return (0); 661} 662 663static int 664vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 665{ 666 int unit; 667 struct ifvlan *ifv = ifp->if_softc; 668 669 unit = ifp->if_dunit; 670 671 vlan_unconfig(ifp); 672 673 ether_ifdetach(ifp); 674 if_free_type(ifp, IFT_ETHER); 675 676 free(ifv, M_VLAN); 677 678 ifc_free_unit(ifc, unit); 679 680 return (0); 681} 682 683/* 684 * The ifp->if_init entry point for vlan(4) is a no-op. 685 */ 686static void 687vlan_ifinit(void *foo) 688{ 689 690} 691 692/* 693 * The if_start method for vlan(4) interface. It doesn't 694 * raises the IFF_DRV_OACTIVE flag, since it is called 695 * only from IFQ_HANDOFF() macro in ether_output_frame(). 696 * If the interface queue is full, and vlan_start() is 697 * not called, the queue would never get emptied and 698 * interface would stall forever. 699 */ 700static void 701vlan_start(struct ifnet *ifp) 702{ 703 struct ifvlan *ifv; 704 struct ifnet *p; 705 struct mbuf *m; 706 int error; 707 708 ifv = ifp->if_softc; 709 p = PARENT(ifv); 710 711 for (;;) { 712 IF_DEQUEUE(&ifp->if_snd, m); 713 if (m == 0) 714 break; 715 BPF_MTAP(ifp, m); 716 717 /* 718 * Do not run parent's if_start() if the parent is not up, 719 * or parent's driver will cause a system crash. 720 */ 721 if (!((p->if_flags & IFF_UP) && 722 (p->if_drv_flags & IFF_DRV_RUNNING))) { 723 m_freem(m); 724 ifp->if_collisions++; 725 continue; 726 } 727 728 /* 729 * If underlying interface can do VLAN tag insertion itself, 730 * just pass the packet along. However, we need some way to 731 * tell the interface where the packet came from so that it 732 * knows how to find the VLAN tag to use, so we attach a 733 * packet tag that holds it. 734 */ 735 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) { 736 struct m_tag *mtag = (struct m_tag *) 737 uma_zalloc(zone_mtag_vlan, M_NOWAIT); 738 if (mtag == NULL) { 739 ifp->if_oerrors++; 740 m_freem(m); 741 continue; 742 } 743 VLAN_TAG_VALUE(mtag) = ifv->ifv_tag; 744 m_tag_prepend(m, mtag); 745 m->m_flags |= M_VLANTAG; 746 } else { 747 struct ether_vlan_header *evl; 748 749 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT); 750 if (m == NULL) { 751 if_printf(ifp, 752 "unable to prepend VLAN header\n"); 753 ifp->if_oerrors++; 754 continue; 755 } 756 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 757 758 if (m->m_len < sizeof(*evl)) { 759 m = m_pullup(m, sizeof(*evl)); 760 if (m == NULL) { 761 if_printf(ifp, 762 "cannot pullup VLAN header\n"); 763 ifp->if_oerrors++; 764 continue; 765 } 766 } 767 768 /* 769 * Transform the Ethernet header into an Ethernet header 770 * with 802.1Q encapsulation. 771 */ 772 bcopy(mtod(m, char *) + ifv->ifv_encaplen, 773 mtod(m, char *), ETHER_HDR_LEN); 774 evl = mtod(m, struct ether_vlan_header *); 775 evl->evl_proto = evl->evl_encap_proto; 776 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 777 evl->evl_tag = htons(ifv->ifv_tag); 778#ifdef DEBUG 779 printf("%s: %*D\n", __func__, (int)sizeof(*evl), 780 (unsigned char *)evl, ":"); 781#endif 782 } 783 784 /* 785 * Send it, precisely as ether_output() would have. 786 * We are already running at splimp. 787 */ 788 IFQ_HANDOFF(p, m, error); 789 if (!error) 790 ifp->if_opackets++; 791 else 792 ifp->if_oerrors++; 793 } 794} 795 796static void 797vlan_input(struct ifnet *ifp, struct mbuf *m) 798{ 799 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 800 struct ifvlan *ifv; 801 struct m_tag *mtag; 802 uint16_t tag; 803 804 KASSERT(trunk != NULL, ("%s: no trunk", __func__)); 805 806 if (m->m_flags & M_VLANTAG) { 807 /* 808 * Packet is tagged, but m contains a normal 809 * Ethernet frame; the tag is stored out-of-band. 810 */ 811 mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL); 812 KASSERT(mtag != NULL, 813 ("%s: M_VLANTAG without m_tag", __func__)); 814 tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)); 815 m_tag_delete(m, mtag); 816 m->m_flags &= ~M_VLANTAG; 817 } else { 818 struct ether_vlan_header *evl; 819 820 /* 821 * Packet is tagged in-band as specified by 802.1q. 822 */ 823 mtag = NULL; 824 switch (ifp->if_type) { 825 case IFT_ETHER: 826 if (m->m_len < sizeof(*evl) && 827 (m = m_pullup(m, sizeof(*evl))) == NULL) { 828 if_printf(ifp, "cannot pullup VLAN header\n"); 829 return; 830 } 831 evl = mtod(m, struct ether_vlan_header *); 832 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN, 833 ("%s: bad encapsulation protocol (%u)", 834 __func__, ntohs(evl->evl_encap_proto))); 835 836 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 837 838 /* 839 * Restore the original ethertype. We'll remove 840 * the encapsulation after we've found the vlan 841 * interface corresponding to the tag. 842 */ 843 evl->evl_encap_proto = evl->evl_proto; 844 break; 845 default: 846 tag = (uint16_t) -1; 847#ifdef INVARIANTS 848 panic("%s: unsupported if_type (%u)", 849 __func__, ifp->if_type); 850#endif 851 break; 852 } 853 } 854 855 /* 856 * In VLAN_ARRAY case we proceed completely lockless. 857 */ 858#ifdef VLAN_ARRAY 859 ifv = trunk->vlans[tag]; 860 if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) { 861 m_freem(m); 862 ifp->if_noproto++; 863 return; 864 } 865#else 866 TRUNK_RLOCK(trunk); 867 ifv = vlan_gethash(trunk, tag); 868 if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) { 869 TRUNK_RUNLOCK(trunk); 870 m_freem(m); 871 ifp->if_noproto++; 872 return; 873 } 874 TRUNK_RUNLOCK(trunk); 875#endif 876 877 if (mtag == NULL) { 878 /* 879 * Packet had an in-line encapsulation header; 880 * remove it. The original header has already 881 * been fixed up above. 882 */ 883 bcopy(mtod(m, caddr_t), 884 mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN, 885 ETHER_HDR_LEN); 886 m_adj(m, ETHER_VLAN_ENCAP_LEN); 887 } 888 889 m->m_pkthdr.rcvif = ifv->ifv_ifp; 890 ifv->ifv_ifp->if_ipackets++; 891 892 /* Pass it back through the parent's input routine. */ 893 (*ifp->if_input)(ifv->ifv_ifp, m); 894} 895 896static int 897vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag) 898{ 899 struct ifvlantrunk *trunk; 900 struct ifnet *ifp; 901 int error = 0; 902 903 /* VID numbers 0x0 and 0xFFF are reserved */ 904 if (tag == 0 || tag == 0xFFF) 905 return (EINVAL); 906 if (p->if_type != IFT_ETHER) 907 return (EPROTONOSUPPORT); 908 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS) 909 return (EPROTONOSUPPORT); 910 if (ifv->ifv_trunk) 911 return (EBUSY); 912 913 if (p->if_vlantrunk == NULL) { 914 trunk = malloc(sizeof(struct ifvlantrunk), 915 M_VLAN, M_WAITOK | M_ZERO); 916 VLAN_LOCK(); 917 if (p->if_vlantrunk != NULL) { 918 /* A race that that is very unlikely to be hit. */ 919 free(trunk, M_VLAN); 920 goto exists; 921 } 922#ifndef VLAN_ARRAY 923 vlan_inithash(trunk); 924#endif 925 TRUNK_LOCK_INIT(trunk); 926 LIST_INSERT_HEAD(&trunk_list, trunk, trunk_entry); 927 TRUNK_LOCK(trunk); 928 p->if_vlantrunk = trunk; 929 trunk->parent = p; 930 } else { 931 VLAN_LOCK(); 932exists: 933 trunk = p->if_vlantrunk; 934 TRUNK_LOCK(trunk); 935 } 936 937 ifv->ifv_tag = tag; 938#ifdef VLAN_ARRAY 939 if (trunk->vlans[tag] != NULL) 940 error = EEXIST; 941#else 942 error = vlan_inshash(trunk, ifv); 943#endif 944 if (error) 945 goto done; 946 947 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 948 ifv->ifv_mintu = ETHERMIN; 949 ifv->ifv_pflags = 0; 950 951 /* 952 * If the parent supports the VLAN_MTU capability, 953 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 954 * use it. 955 */ 956 if (p->if_capenable & IFCAP_VLAN_MTU) { 957 /* 958 * No need to fudge the MTU since the parent can 959 * handle extended frames. 960 */ 961 ifv->ifv_mtufudge = 0; 962 } else { 963 /* 964 * Fudge the MTU by the encapsulation size. This 965 * makes us incompatible with strictly compliant 966 * 802.1Q implementations, but allows us to use 967 * the feature with other NetBSD implementations, 968 * which might still be useful. 969 */ 970 ifv->ifv_mtufudge = ifv->ifv_encaplen; 971 } 972 973 ifv->ifv_trunk = trunk; 974 ifp = ifv->ifv_ifp; 975 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 976 ifp->if_baudrate = p->if_baudrate; 977 /* 978 * Copy only a selected subset of flags from the parent. 979 * Other flags are none of our business. 980 */ 981#define VLAN_COPY_FLAGS (IFF_SIMPLEX) 982 ifp->if_flags &= ~VLAN_COPY_FLAGS; 983 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 984#undef VLAN_COPY_FLAGS 985 986 ifp->if_link_state = p->if_link_state; 987 988 vlan_capabilities(ifv); 989 990 /* 991 * Set up our ``Ethernet address'' to reflect the underlying 992 * physical interface's. 993 */ 994 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN); 995 996 /* 997 * Configure multicast addresses that may already be 998 * joined on the vlan device. 999 */ 1000 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */ 1001 1002#ifdef VLAN_ARRAY 1003 atomic_store_rel_ptr((uintptr_t *)&trunk->vlans[tag], (uintptr_t)ifv); 1004 trunk->refcnt++; 1005#endif 1006done: 1007 TRUNK_UNLOCK(trunk); 1008 VLAN_UNLOCK(); 1009 1010 return (error); 1011} 1012 1013static int 1014vlan_unconfig(struct ifnet *ifp) 1015{ 1016 struct ifvlantrunk *trunk; 1017 struct vlan_mc_entry *mc; 1018 struct ifvlan *ifv; 1019 int error; 1020 1021 VLAN_LOCK(); 1022 1023 ifv = ifp->if_softc; 1024 trunk = ifv->ifv_trunk; 1025 1026 if (trunk) { 1027 struct sockaddr_dl sdl; 1028 struct ifnet *p = trunk->parent; 1029 1030 TRUNK_LOCK(trunk); 1031#ifdef VLAN_ARRAY 1032 atomic_store_rel_ptr((uintptr_t *)&trunk->vlans[ifv->ifv_tag], 1033 (uintptr_t)NULL); 1034 trunk->refcnt--; 1035#endif 1036 1037 /* 1038 * Since the interface is being unconfigured, we need to 1039 * empty the list of multicast groups that we may have joined 1040 * while we were alive from the parent's list. 1041 */ 1042 bzero((char *)&sdl, sizeof(sdl)); 1043 sdl.sdl_len = sizeof(sdl); 1044 sdl.sdl_family = AF_LINK; 1045 sdl.sdl_index = p->if_index; 1046 sdl.sdl_type = IFT_ETHER; 1047 sdl.sdl_alen = ETHER_ADDR_LEN; 1048 1049 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) { 1050 mc = SLIST_FIRST(&ifv->vlan_mc_listhead); 1051 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), 1052 ETHER_ADDR_LEN); 1053 error = if_delmulti(p, (struct sockaddr *)&sdl); 1054 if (error) 1055 return (error); 1056 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 1057 free(mc, M_VLAN); 1058 } 1059 1060 vlan_setflags(ifp, 0); /* clear special flags on parent */ 1061#ifndef VLAN_ARRAY 1062 vlan_remhash(trunk, ifv); 1063#endif 1064 ifv->ifv_trunk = NULL; 1065 1066 /* 1067 * Check if we were the last. 1068 */ 1069 if (trunk->refcnt == 0) { 1070 atomic_store_rel_ptr((uintptr_t *) 1071 &trunk->parent->if_vlantrunk, 1072 (uintptr_t)NULL); 1073 /* 1074 * XXXGL: If some ithread has already entered 1075 * vlan_input() and is now blocked on the trunk 1076 * lock, then it should preempt us right after 1077 * unlock and finish its work. Then we will acquire 1078 * lock again in trunk_destroy(). 1079 * XXX: not true in case of VLAN_ARRAY 1080 */ 1081 TRUNK_UNLOCK(trunk); 1082 trunk_destroy(trunk); 1083 } else 1084 TRUNK_UNLOCK(trunk); 1085 } 1086 1087 /* Disconnect from parent. */ 1088 if (ifv->ifv_pflags) 1089 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 1090 ifv->ifv_ifp->if_mtu = ETHERMTU; /* XXX why not 0? */ 1091 ifv->ifv_ifp->if_link_state = LINK_STATE_UNKNOWN; 1092 1093 /* Clear our MAC address. */ 1094 bzero(IF_LLADDR(ifv->ifv_ifp), ETHER_ADDR_LEN); 1095 1096 VLAN_UNLOCK(); 1097 1098 return (0); 1099} 1100 1101/* Handle a reference counted flag that should be set on the parent as well */ 1102static int 1103vlan_setflag(struct ifnet *ifp, int flag, int status, 1104 int (*func)(struct ifnet *, int)) 1105{ 1106 struct ifvlan *ifv; 1107 int error; 1108 1109 /* XXX VLAN_LOCK_ASSERT(); */ 1110 1111 ifv = ifp->if_softc; 1112 status = status ? (ifp->if_flags & flag) : 0; 1113 /* Now "status" contains the flag value or 0 */ 1114 1115 /* 1116 * See if recorded parent's status is different from what 1117 * we want it to be. If it is, flip it. We record parent's 1118 * status in ifv_pflags so that we won't clear parent's flag 1119 * we haven't set. In fact, we don't clear or set parent's 1120 * flags directly, but get or release references to them. 1121 * That's why we can be sure that recorded flags still are 1122 * in accord with actual parent's flags. 1123 */ 1124 if (status != (ifv->ifv_pflags & flag)) { 1125 error = (*func)(PARENT(ifv), status); 1126 if (error) 1127 return (error); 1128 ifv->ifv_pflags &= ~flag; 1129 ifv->ifv_pflags |= status; 1130 } 1131 return (0); 1132} 1133 1134/* 1135 * Handle IFF_* flags that require certain changes on the parent: 1136 * if "status" is true, update parent's flags respective to our if_flags; 1137 * if "status" is false, forcedly clear the flags set on parent. 1138 */ 1139static int 1140vlan_setflags(struct ifnet *ifp, int status) 1141{ 1142 int error, i; 1143 1144 for (i = 0; vlan_pflags[i].flag; i++) { 1145 error = vlan_setflag(ifp, vlan_pflags[i].flag, 1146 status, vlan_pflags[i].func); 1147 if (error) 1148 return (error); 1149 } 1150 return (0); 1151} 1152 1153/* Inform all vlans that their parent has changed link state */ 1154static void 1155vlan_link_state(struct ifnet *ifp, int link) 1156{ 1157 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1158 struct ifvlan *ifv; 1159 int i; 1160 1161 TRUNK_LOCK(trunk); 1162#ifdef VLAN_ARRAY 1163 for (i = 0; i < EVL_VLID_MASK+1; i++) 1164 if (trunk->vlans[i] != NULL) { 1165 ifv = trunk->vlans[i]; 1166#else 1167 for (i = 0; i < (1 << trunk->hwidth); i++) { 1168 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 1169#endif 1170 if_link_state_change(ifv->ifv_ifp, 1171 trunk->parent->if_link_state); 1172 } 1173 TRUNK_UNLOCK(trunk); 1174} 1175 1176static void 1177vlan_capabilities(struct ifvlan *ifv) 1178{ 1179 struct ifnet *p = PARENT(ifv); 1180 struct ifnet *ifp = ifv->ifv_ifp; 1181 1182 TRUNK_LOCK_ASSERT(TRUNK(ifv)); 1183 1184 /* 1185 * If the parent interface can do checksum offloading 1186 * on VLANs, then propagate its hardware-assisted 1187 * checksumming flags. Also assert that checksum 1188 * offloading requires hardware VLAN tagging. 1189 */ 1190 if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1191 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM; 1192 1193 if (p->if_capenable & IFCAP_VLAN_HWCSUM && 1194 p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1195 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM; 1196 ifp->if_hwassist = p->if_hwassist; 1197 } else { 1198 ifp->if_capenable = 0; 1199 ifp->if_hwassist = 0; 1200 } 1201} 1202 1203static void 1204vlan_trunk_capabilities(struct ifnet *ifp) 1205{ 1206 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1207 struct ifvlan *ifv; 1208 int i; 1209 1210 TRUNK_LOCK(trunk); 1211#ifdef VLAN_ARRAY 1212 for (i = 0; i < EVL_VLID_MASK+1; i++) 1213 if (trunk->vlans[i] != NULL) { 1214 ifv = trunk->vlans[i]; 1215#else 1216 for (i = 0; i < (1 << trunk->hwidth); i++) { 1217 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 1218#endif 1219 vlan_capabilities(ifv); 1220 } 1221 TRUNK_UNLOCK(trunk); 1222} 1223 1224static int 1225vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1226{ 1227 struct ifaddr *ifa; 1228 struct ifnet *p; 1229 struct ifreq *ifr; 1230 struct ifvlan *ifv; 1231 struct vlanreq vlr; 1232 int error = 0; 1233 1234 ifr = (struct ifreq *)data; 1235 ifa = (struct ifaddr *)data; 1236 ifv = ifp->if_softc; 1237 1238 switch (cmd) { 1239 case SIOCSIFADDR: 1240 ifp->if_flags |= IFF_UP; 1241 1242 switch (ifa->ifa_addr->sa_family) { 1243#ifdef INET 1244 case AF_INET: 1245 arp_ifinit(ifv->ifv_ifp, ifa); 1246 break; 1247#endif 1248 default: 1249 break; 1250 } 1251 break; 1252 1253 case SIOCGIFADDR: 1254 { 1255 struct sockaddr *sa; 1256 1257 sa = (struct sockaddr *) &ifr->ifr_data; 1258 bcopy(IF_LLADDR(ifp), (caddr_t)sa->sa_data, 1259 ETHER_ADDR_LEN); 1260 } 1261 break; 1262 1263 case SIOCGIFMEDIA: 1264 VLAN_LOCK(); 1265 if (TRUNK(ifv) != NULL) { 1266 error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv), 1267 SIOCGIFMEDIA, data); 1268 VLAN_UNLOCK(); 1269 /* Limit the result to the parent's current config. */ 1270 if (error == 0) { 1271 struct ifmediareq *ifmr; 1272 1273 ifmr = (struct ifmediareq *)data; 1274 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1275 ifmr->ifm_count = 1; 1276 error = copyout(&ifmr->ifm_current, 1277 ifmr->ifm_ulist, 1278 sizeof(int)); 1279 } 1280 } 1281 } else { 1282 VLAN_UNLOCK(); 1283 error = EINVAL; 1284 } 1285 break; 1286 1287 case SIOCSIFMEDIA: 1288 error = EINVAL; 1289 break; 1290 1291 case SIOCSIFMTU: 1292 /* 1293 * Set the interface MTU. 1294 */ 1295 VLAN_LOCK(); 1296 if (TRUNK(ifv) != NULL) { 1297 if (ifr->ifr_mtu > 1298 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1299 ifr->ifr_mtu < 1300 (ifv->ifv_mintu - ifv->ifv_mtufudge)) 1301 error = EINVAL; 1302 else 1303 ifp->if_mtu = ifr->ifr_mtu; 1304 } else 1305 error = EINVAL; 1306 VLAN_UNLOCK(); 1307 break; 1308 1309 case SIOCSETVLAN: 1310 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr)); 1311 if (error) 1312 break; 1313 if (vlr.vlr_parent[0] == '\0') { 1314 VLAN_LOCK(); 1315 vlan_unconfig(ifp); 1316 if (ifp->if_flags & IFF_UP) 1317 if_down(ifp); 1318 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1319 VLAN_UNLOCK(); 1320 break; 1321 } 1322 p = ifunit(vlr.vlr_parent); 1323 if (p == 0) { 1324 error = ENOENT; 1325 break; 1326 } 1327 /* 1328 * Don't let the caller set up a VLAN tag with 1329 * anything except VLID bits. 1330 */ 1331 if (vlr.vlr_tag & ~EVL_VLID_MASK) { 1332 error = EINVAL; 1333 break; 1334 } 1335 error = vlan_config(ifv, p, vlr.vlr_tag); 1336 if (error) 1337 break; 1338 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1339 1340 /* Update flags on the parent, if necessary. */ 1341 vlan_setflags(ifp, 1); 1342 break; 1343 1344 case SIOCGETVLAN: 1345 bzero(&vlr, sizeof(vlr)); 1346 VLAN_LOCK(); 1347 if (TRUNK(ifv) != NULL) { 1348 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 1349 sizeof(vlr.vlr_parent)); 1350 vlr.vlr_tag = ifv->ifv_tag; 1351 } 1352 VLAN_UNLOCK(); 1353 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 1354 break; 1355 1356 case SIOCSIFFLAGS: 1357 /* 1358 * We should propagate selected flags to the parent, 1359 * e.g., promiscuous mode. 1360 */ 1361 if (TRUNK(ifv) != NULL) 1362 error = vlan_setflags(ifp, 1); 1363 break; 1364 1365 case SIOCADDMULTI: 1366 case SIOCDELMULTI: 1367 /* 1368 * If we don't have a parent, just remember the membership for 1369 * when we do. 1370 */ 1371 if (TRUNK(ifv) != NULL) 1372 error = vlan_setmulti(ifp); 1373 break; 1374 1375 default: 1376 error = EINVAL; 1377 } 1378 1379 return (error); 1380} 1381