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