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