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