if_vlan.c revision 155493
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 155493 2006-02-09 22:11:58Z emaste $ 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 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 230 for (i = 0; i < (1 << trunk->hwidth); i++) 231 KASSERT(LIST_EMPTY(&trunk->hash[i]), 232 ("%s: hash table not empty", __func__)); 233#endif 234 free(trunk->hash, M_VLAN); 235 trunk->hash = NULL; 236 trunk->hwidth = trunk->hmask = 0; 237} 238 239static int 240vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 241{ 242 int i, b; 243 struct ifvlan *ifv2; 244 245 TRUNK_LOCK_ASSERT(trunk); 246 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 247 248 b = 1 << trunk->hwidth; 249 i = HASH(ifv->ifv_tag, trunk->hmask); 250 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 251 if (ifv->ifv_tag == ifv2->ifv_tag) 252 return (EEXIST); 253 254 /* 255 * Grow the hash when the number of vlans exceeds half of the number of 256 * hash buckets squared. This will make the average linked-list length 257 * buckets/2. 258 */ 259 if (trunk->refcnt > (b * b) / 2) { 260 vlan_growhash(trunk, 1); 261 i = HASH(ifv->ifv_tag, trunk->hmask); 262 } 263 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list); 264 trunk->refcnt++; 265 266 return (0); 267} 268 269static int 270vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv) 271{ 272 int i, b; 273 struct ifvlan *ifv2; 274 275 TRUNK_LOCK_ASSERT(trunk); 276 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 277 278 b = 1 << trunk->hwidth; 279 i = HASH(ifv->ifv_tag, trunk->hmask); 280 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list) 281 if (ifv2 == ifv) { 282 trunk->refcnt--; 283 LIST_REMOVE(ifv2, ifv_list); 284 if (trunk->refcnt < (b * b) / 2) 285 vlan_growhash(trunk, -1); 286 return (0); 287 } 288 289 panic("%s: vlan not found\n", __func__); 290 return (ENOENT); /*NOTREACHED*/ 291} 292 293/* 294 * Grow the hash larger or smaller if memory permits. 295 */ 296static void 297vlan_growhash(struct ifvlantrunk *trunk, int howmuch) 298{ 299 300 struct ifvlan *ifv; 301 struct ifvlanhead *hash2; 302 int hwidth2, i, j, n, n2; 303 304 TRUNK_LOCK_ASSERT(trunk); 305 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__)); 306 307 if (howmuch == 0) { 308 /* Harmless yet obvious coding error */ 309 printf("%s: howmuch is 0\n", __func__); 310 return; 311 } 312 313 hwidth2 = trunk->hwidth + howmuch; 314 n = 1 << trunk->hwidth; 315 n2 = 1 << hwidth2; 316 /* Do not shrink the table below the default */ 317 if (hwidth2 < VLAN_DEF_HWIDTH) 318 return; 319 320 /* M_NOWAIT because we're called with trunk mutex held */ 321 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT); 322 if (hash2 == NULL) { 323 printf("%s: out of memory -- hash size not changed\n", 324 __func__); 325 return; /* We can live with the old hash table */ 326 } 327 for (j = 0; j < n2; j++) 328 LIST_INIT(&hash2[j]); 329 for (i = 0; i < n; i++) 330 while (!LIST_EMPTY(&trunk->hash[i])) { 331 ifv = LIST_FIRST(&trunk->hash[i]); 332 LIST_REMOVE(ifv, ifv_list); 333 j = HASH(ifv->ifv_tag, n2 - 1); 334 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list); 335 } 336 free(trunk->hash, M_VLAN); 337 trunk->hash = hash2; 338 trunk->hwidth = hwidth2; 339 trunk->hmask = n2 - 1; 340} 341 342static __inline struct ifvlan * 343vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag) 344{ 345 struct ifvlan *ifv; 346 347 TRUNK_LOCK_RASSERT(trunk); 348 349 LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list) 350 if (ifv->ifv_tag == tag) 351 return (ifv); 352 return (NULL); 353} 354 355#if 0 356/* Debugging code to view the hashtables. */ 357static void 358vlan_dumphash(struct ifvlantrunk *trunk) 359{ 360 int i; 361 struct ifvlan *ifv; 362 363 for (i = 0; i < (1 << trunk->hwidth); i++) { 364 printf("%d: ", i); 365 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 366 printf("%s ", ifv->ifv_ifp->if_xname); 367 printf("\n"); 368 } 369} 370#endif /* 0 */ 371#endif /* !VLAN_ARRAY */ 372 373static void 374trunk_destroy(struct ifvlantrunk *trunk) 375{ 376 VLAN_LOCK_ASSERT(); 377 378 TRUNK_LOCK(trunk); 379#ifndef VLAN_ARRAY 380 vlan_freehash(trunk); 381#endif 382 TRUNK_LOCK_DESTROY(trunk); 383 LIST_REMOVE(trunk, trunk_entry); 384 trunk->parent->if_vlantrunk = NULL; 385 free(trunk, M_VLAN); 386} 387 388/* 389 * Program our multicast filter. What we're actually doing is 390 * programming the multicast filter of the parent. This has the 391 * side effect of causing the parent interface to receive multicast 392 * traffic that it doesn't really want, which ends up being discarded 393 * later by the upper protocol layers. Unfortunately, there's no way 394 * to avoid this: there really is only one physical interface. 395 * 396 * XXX: There is a possible race here if more than one thread is 397 * modifying the multicast state of the vlan interface at the same time. 398 */ 399static int 400vlan_setmulti(struct ifnet *ifp) 401{ 402 struct ifnet *ifp_p; 403 struct ifmultiaddr *ifma, *rifma = NULL; 404 struct ifvlan *sc; 405 struct vlan_mc_entry *mc = NULL; 406 struct sockaddr_dl sdl; 407 int error; 408 409 /*VLAN_LOCK_ASSERT();*/ 410 411 /* Find the parent. */ 412 sc = ifp->if_softc; 413 ifp_p = PARENT(sc); 414 415 bzero((char *)&sdl, sizeof(sdl)); 416 sdl.sdl_len = sizeof(sdl); 417 sdl.sdl_family = AF_LINK; 418 sdl.sdl_index = ifp_p->if_index; 419 sdl.sdl_type = IFT_ETHER; 420 sdl.sdl_alen = ETHER_ADDR_LEN; 421 422 /* First, remove any existing filter entries. */ 423 while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) { 424 mc = SLIST_FIRST(&sc->vlan_mc_listhead); 425 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 426 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl); 427 if (error) 428 return (error); 429 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 430 free(mc, M_VLAN); 431 } 432 433 /* Now program new ones. */ 434 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 435 if (ifma->ifma_addr->sa_family != AF_LINK) 436 continue; 437 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT); 438 if (mc == NULL) 439 return (ENOMEM); 440 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 441 (char *)&mc->mc_addr, ETHER_ADDR_LEN); 442 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 443 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 444 LLADDR(&sdl), ETHER_ADDR_LEN); 445 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 446 if (error) 447 return (error); 448 } 449 450 return (0); 451} 452 453/* 454 * VLAN support can be loaded as a module. The only place in the 455 * system that's intimately aware of this is ether_input. We hook 456 * into this code through vlan_input_p which is defined there and 457 * set here. Noone else in the system should be aware of this so 458 * we use an explicit reference here. 459 */ 460extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 461 462/* For if_link_state_change() eyes only... */ 463extern void (*vlan_link_state_p)(struct ifnet *, int); 464 465static int 466vlan_modevent(module_t mod, int type, void *data) 467{ 468 469 switch (type) { 470 case MOD_LOAD: 471 LIST_INIT(&trunk_list); 472 VLAN_LOCK_INIT(); 473 vlan_input_p = vlan_input; 474 vlan_link_state_p = vlan_link_state; 475 vlan_trunk_cap_p = vlan_trunk_capabilities; 476 if_clone_attach(&vlan_cloner); 477 break; 478 case MOD_UNLOAD: 479 { 480 struct ifvlantrunk *trunk, *trunk1; 481 482 if_clone_detach(&vlan_cloner); 483 vlan_input_p = NULL; 484 vlan_link_state_p = NULL; 485 vlan_trunk_cap_p = NULL; 486 VLAN_LOCK(); 487 LIST_FOREACH_SAFE(trunk, &trunk_list, trunk_entry, trunk1) 488 trunk_destroy(trunk); 489 VLAN_UNLOCK(); 490 VLAN_LOCK_DESTROY(); 491 break; 492 } 493 default: 494 return (EOPNOTSUPP); 495 } 496 return (0); 497} 498 499static moduledata_t vlan_mod = { 500 "if_vlan", 501 vlan_modevent, 502 0 503}; 504 505DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 506MODULE_VERSION(if_vlan, 1); 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#ifndef VLAN_ARRAY 917 vlan_inithash(trunk); 918#endif 919 VLAN_LOCK(); 920 if (p->if_vlantrunk != NULL) { 921 /* A race that that is very unlikely to be hit. */ 922#ifndef VLAN_ARRAY 923 vlan_freehash(trunk); 924#endif 925 free(trunk, M_VLAN); 926 goto exists; 927 } 928 TRUNK_LOCK_INIT(trunk); 929 LIST_INSERT_HEAD(&trunk_list, trunk, trunk_entry); 930 TRUNK_LOCK(trunk); 931 p->if_vlantrunk = trunk; 932 trunk->parent = p; 933 } else { 934 VLAN_LOCK(); 935exists: 936 trunk = p->if_vlantrunk; 937 TRUNK_LOCK(trunk); 938 } 939 940 ifv->ifv_tag = tag; 941#ifdef VLAN_ARRAY 942 if (trunk->vlans[tag] != NULL) 943 error = EEXIST; 944#else 945 error = vlan_inshash(trunk, ifv); 946#endif 947 if (error) 948 goto done; 949 950 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 951 ifv->ifv_mintu = ETHERMIN; 952 ifv->ifv_pflags = 0; 953 954 /* 955 * If the parent supports the VLAN_MTU capability, 956 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 957 * use it. 958 */ 959 if (p->if_capenable & IFCAP_VLAN_MTU) { 960 /* 961 * No need to fudge the MTU since the parent can 962 * handle extended frames. 963 */ 964 ifv->ifv_mtufudge = 0; 965 } else { 966 /* 967 * Fudge the MTU by the encapsulation size. This 968 * makes us incompatible with strictly compliant 969 * 802.1Q implementations, but allows us to use 970 * the feature with other NetBSD implementations, 971 * which might still be useful. 972 */ 973 ifv->ifv_mtufudge = ifv->ifv_encaplen; 974 } 975 976 ifv->ifv_trunk = trunk; 977 ifp = ifv->ifv_ifp; 978 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge; 979 ifp->if_baudrate = p->if_baudrate; 980 /* 981 * Copy only a selected subset of flags from the parent. 982 * Other flags are none of our business. 983 */ 984#define VLAN_COPY_FLAGS (IFF_SIMPLEX) 985 ifp->if_flags &= ~VLAN_COPY_FLAGS; 986 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS; 987#undef VLAN_COPY_FLAGS 988 989 ifp->if_link_state = p->if_link_state; 990 991 vlan_capabilities(ifv); 992 993 /* 994 * Set up our ``Ethernet address'' to reflect the underlying 995 * physical interface's. 996 */ 997 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN); 998 999 /* 1000 * Configure multicast addresses that may already be 1001 * joined on the vlan device. 1002 */ 1003 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */ 1004 1005#ifdef VLAN_ARRAY 1006 atomic_store_rel_ptr((uintptr_t *)&trunk->vlans[tag], (uintptr_t)ifv); 1007 trunk->refcnt++; 1008#endif 1009done: 1010 TRUNK_UNLOCK(trunk); 1011 VLAN_UNLOCK(); 1012 1013 return (error); 1014} 1015 1016static int 1017vlan_unconfig(struct ifnet *ifp) 1018{ 1019 struct ifvlantrunk *trunk; 1020 struct vlan_mc_entry *mc; 1021 struct ifvlan *ifv; 1022 int error; 1023 1024 VLAN_LOCK(); 1025 1026 ifv = ifp->if_softc; 1027 trunk = ifv->ifv_trunk; 1028 1029 if (trunk) { 1030 struct sockaddr_dl sdl; 1031 struct ifnet *p = trunk->parent; 1032 1033 TRUNK_LOCK(trunk); 1034#ifdef VLAN_ARRAY 1035 atomic_store_rel_ptr((uintptr_t *)&trunk->vlans[ifv->ifv_tag], 1036 (uintptr_t)NULL); 1037 trunk->refcnt--; 1038#endif 1039 1040 /* 1041 * Since the interface is being unconfigured, we need to 1042 * empty the list of multicast groups that we may have joined 1043 * while we were alive from the parent's list. 1044 */ 1045 bzero((char *)&sdl, sizeof(sdl)); 1046 sdl.sdl_len = sizeof(sdl); 1047 sdl.sdl_family = AF_LINK; 1048 sdl.sdl_index = p->if_index; 1049 sdl.sdl_type = IFT_ETHER; 1050 sdl.sdl_alen = ETHER_ADDR_LEN; 1051 1052 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) { 1053 mc = SLIST_FIRST(&ifv->vlan_mc_listhead); 1054 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), 1055 ETHER_ADDR_LEN); 1056 error = if_delmulti(p, (struct sockaddr *)&sdl); 1057 if (error) 1058 return (error); 1059 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 1060 free(mc, M_VLAN); 1061 } 1062 1063 vlan_setflags(ifp, 0); /* clear special flags on parent */ 1064#ifndef VLAN_ARRAY 1065 vlan_remhash(trunk, ifv); 1066#endif 1067 ifv->ifv_trunk = NULL; 1068 1069 /* 1070 * Check if we were the last. 1071 */ 1072 if (trunk->refcnt == 0) { 1073 atomic_store_rel_ptr((uintptr_t *) 1074 &trunk->parent->if_vlantrunk, 1075 (uintptr_t)NULL); 1076 /* 1077 * XXXGL: If some ithread has already entered 1078 * vlan_input() and is now blocked on the trunk 1079 * lock, then it should preempt us right after 1080 * unlock and finish its work. Then we will acquire 1081 * lock again in trunk_destroy(). 1082 * XXX: not true in case of VLAN_ARRAY 1083 */ 1084 TRUNK_UNLOCK(trunk); 1085 trunk_destroy(trunk); 1086 } else 1087 TRUNK_UNLOCK(trunk); 1088 } 1089 1090 /* Disconnect from parent. */ 1091 if (ifv->ifv_pflags) 1092 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__); 1093 ifv->ifv_ifp->if_mtu = ETHERMTU; /* XXX why not 0? */ 1094 ifv->ifv_ifp->if_link_state = LINK_STATE_UNKNOWN; 1095 1096 /* Clear our MAC address. */ 1097 bzero(IF_LLADDR(ifv->ifv_ifp), ETHER_ADDR_LEN); 1098 1099 VLAN_UNLOCK(); 1100 1101 return (0); 1102} 1103 1104/* Handle a reference counted flag that should be set on the parent as well */ 1105static int 1106vlan_setflag(struct ifnet *ifp, int flag, int status, 1107 int (*func)(struct ifnet *, int)) 1108{ 1109 struct ifvlan *ifv; 1110 int error; 1111 1112 /* XXX VLAN_LOCK_ASSERT(); */ 1113 1114 ifv = ifp->if_softc; 1115 status = status ? (ifp->if_flags & flag) : 0; 1116 /* Now "status" contains the flag value or 0 */ 1117 1118 /* 1119 * See if recorded parent's status is different from what 1120 * we want it to be. If it is, flip it. We record parent's 1121 * status in ifv_pflags so that we won't clear parent's flag 1122 * we haven't set. In fact, we don't clear or set parent's 1123 * flags directly, but get or release references to them. 1124 * That's why we can be sure that recorded flags still are 1125 * in accord with actual parent's flags. 1126 */ 1127 if (status != (ifv->ifv_pflags & flag)) { 1128 error = (*func)(PARENT(ifv), status); 1129 if (error) 1130 return (error); 1131 ifv->ifv_pflags &= ~flag; 1132 ifv->ifv_pflags |= status; 1133 } 1134 return (0); 1135} 1136 1137/* 1138 * Handle IFF_* flags that require certain changes on the parent: 1139 * if "status" is true, update parent's flags respective to our if_flags; 1140 * if "status" is false, forcedly clear the flags set on parent. 1141 */ 1142static int 1143vlan_setflags(struct ifnet *ifp, int status) 1144{ 1145 int error, i; 1146 1147 for (i = 0; vlan_pflags[i].flag; i++) { 1148 error = vlan_setflag(ifp, vlan_pflags[i].flag, 1149 status, vlan_pflags[i].func); 1150 if (error) 1151 return (error); 1152 } 1153 return (0); 1154} 1155 1156/* Inform all vlans that their parent has changed link state */ 1157static void 1158vlan_link_state(struct ifnet *ifp, int link) 1159{ 1160 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1161 struct ifvlan *ifv; 1162 int i; 1163 1164 TRUNK_LOCK(trunk); 1165#ifdef VLAN_ARRAY 1166 for (i = 0; i < EVL_VLID_MASK+1; i++) 1167 if (trunk->vlans[i] != NULL) { 1168 ifv = trunk->vlans[i]; 1169#else 1170 for (i = 0; i < (1 << trunk->hwidth); i++) { 1171 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 1172#endif 1173 if_link_state_change(ifv->ifv_ifp, 1174 trunk->parent->if_link_state); 1175 } 1176 TRUNK_UNLOCK(trunk); 1177} 1178 1179static void 1180vlan_capabilities(struct ifvlan *ifv) 1181{ 1182 struct ifnet *p = PARENT(ifv); 1183 struct ifnet *ifp = ifv->ifv_ifp; 1184 1185 TRUNK_LOCK_ASSERT(TRUNK(ifv)); 1186 1187 /* 1188 * If the parent interface can do checksum offloading 1189 * on VLANs, then propagate its hardware-assisted 1190 * checksumming flags. Also assert that checksum 1191 * offloading requires hardware VLAN tagging. 1192 */ 1193 if (p->if_capabilities & IFCAP_VLAN_HWCSUM) 1194 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM; 1195 1196 if (p->if_capenable & IFCAP_VLAN_HWCSUM && 1197 p->if_capenable & IFCAP_VLAN_HWTAGGING) { 1198 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM; 1199 ifp->if_hwassist = p->if_hwassist; 1200 } else { 1201 ifp->if_capenable = 0; 1202 ifp->if_hwassist = 0; 1203 } 1204} 1205 1206static void 1207vlan_trunk_capabilities(struct ifnet *ifp) 1208{ 1209 struct ifvlantrunk *trunk = ifp->if_vlantrunk; 1210 struct ifvlan *ifv; 1211 int i; 1212 1213 TRUNK_LOCK(trunk); 1214#ifdef VLAN_ARRAY 1215 for (i = 0; i < EVL_VLID_MASK+1; i++) 1216 if (trunk->vlans[i] != NULL) { 1217 ifv = trunk->vlans[i]; 1218#else 1219 for (i = 0; i < (1 << trunk->hwidth); i++) { 1220 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) 1221#endif 1222 vlan_capabilities(ifv); 1223 } 1224 TRUNK_UNLOCK(trunk); 1225} 1226 1227static int 1228vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1229{ 1230 struct ifaddr *ifa; 1231 struct ifnet *p; 1232 struct ifreq *ifr; 1233 struct ifvlan *ifv; 1234 struct vlanreq vlr; 1235 int error = 0; 1236 1237 ifr = (struct ifreq *)data; 1238 ifa = (struct ifaddr *)data; 1239 ifv = ifp->if_softc; 1240 1241 switch (cmd) { 1242 case SIOCSIFADDR: 1243 ifp->if_flags |= IFF_UP; 1244 1245 switch (ifa->ifa_addr->sa_family) { 1246#ifdef INET 1247 case AF_INET: 1248 arp_ifinit(ifv->ifv_ifp, ifa); 1249 break; 1250#endif 1251 default: 1252 break; 1253 } 1254 break; 1255 1256 case SIOCGIFADDR: 1257 { 1258 struct sockaddr *sa; 1259 1260 sa = (struct sockaddr *) &ifr->ifr_data; 1261 bcopy(IF_LLADDR(ifp), (caddr_t)sa->sa_data, 1262 ETHER_ADDR_LEN); 1263 } 1264 break; 1265 1266 case SIOCGIFMEDIA: 1267 VLAN_LOCK(); 1268 if (TRUNK(ifv) != NULL) { 1269 error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv), 1270 SIOCGIFMEDIA, data); 1271 VLAN_UNLOCK(); 1272 /* Limit the result to the parent's current config. */ 1273 if (error == 0) { 1274 struct ifmediareq *ifmr; 1275 1276 ifmr = (struct ifmediareq *)data; 1277 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 1278 ifmr->ifm_count = 1; 1279 error = copyout(&ifmr->ifm_current, 1280 ifmr->ifm_ulist, 1281 sizeof(int)); 1282 } 1283 } 1284 } else { 1285 VLAN_UNLOCK(); 1286 error = EINVAL; 1287 } 1288 break; 1289 1290 case SIOCSIFMEDIA: 1291 error = EINVAL; 1292 break; 1293 1294 case SIOCSIFMTU: 1295 /* 1296 * Set the interface MTU. 1297 */ 1298 VLAN_LOCK(); 1299 if (TRUNK(ifv) != NULL) { 1300 if (ifr->ifr_mtu > 1301 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) || 1302 ifr->ifr_mtu < 1303 (ifv->ifv_mintu - ifv->ifv_mtufudge)) 1304 error = EINVAL; 1305 else 1306 ifp->if_mtu = ifr->ifr_mtu; 1307 } else 1308 error = EINVAL; 1309 VLAN_UNLOCK(); 1310 break; 1311 1312 case SIOCSETVLAN: 1313 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr)); 1314 if (error) 1315 break; 1316 if (vlr.vlr_parent[0] == '\0') { 1317 VLAN_LOCK(); 1318 vlan_unconfig(ifp); 1319 if (ifp->if_flags & IFF_UP) 1320 if_down(ifp); 1321 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1322 VLAN_UNLOCK(); 1323 break; 1324 } 1325 p = ifunit(vlr.vlr_parent); 1326 if (p == 0) { 1327 error = ENOENT; 1328 break; 1329 } 1330 /* 1331 * Don't let the caller set up a VLAN tag with 1332 * anything except VLID bits. 1333 */ 1334 if (vlr.vlr_tag & ~EVL_VLID_MASK) { 1335 error = EINVAL; 1336 break; 1337 } 1338 error = vlan_config(ifv, p, vlr.vlr_tag); 1339 if (error) 1340 break; 1341 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1342 1343 /* Update flags on the parent, if necessary. */ 1344 vlan_setflags(ifp, 1); 1345 break; 1346 1347 case SIOCGETVLAN: 1348 bzero(&vlr, sizeof(vlr)); 1349 VLAN_LOCK(); 1350 if (TRUNK(ifv) != NULL) { 1351 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname, 1352 sizeof(vlr.vlr_parent)); 1353 vlr.vlr_tag = ifv->ifv_tag; 1354 } 1355 VLAN_UNLOCK(); 1356 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 1357 break; 1358 1359 case SIOCSIFFLAGS: 1360 /* 1361 * We should propagate selected flags to the parent, 1362 * e.g., promiscuous mode. 1363 */ 1364 if (TRUNK(ifv) != NULL) 1365 error = vlan_setflags(ifp, 1); 1366 break; 1367 1368 case SIOCADDMULTI: 1369 case SIOCDELMULTI: 1370 /* 1371 * If we don't have a parent, just remember the membership for 1372 * when we do. 1373 */ 1374 if (TRUNK(ifv) != NULL) 1375 error = vlan_setmulti(ifp); 1376 break; 1377 1378 default: 1379 error = EINVAL; 1380 } 1381 1382 return (error); 1383} 1384