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$ 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 46 #include <sys/param.h> 47 #include <sys/kernel.h> 48 #include <sys/malloc.h> 49 #include <sys/mbuf.h> 50 #include <sys/module.h> 51 #include <sys/queue.h> 52 #include <sys/socket.h> 53 #include <sys/sockio.h> 54 #include <sys/sysctl.h> 55 #include <sys/systm.h> 56 57 #include <net/bpf.h> 58 #include <net/ethernet.h> 59 #include <net/if.h> 60 #include <net/if_arp.h> 61 #include <net/if_dl.h> 62 #include <net/if_types.h> 63 #include <net/if_vlan_var.h> 64 #include <net/route.h> 65 66 #ifdef INET 67 #include <netinet/in.h> 68 #include <netinet/if_ether.h> 69 #endif 70 71 #define VLANNAME "vlan" 72 73 struct vlan_mc_entry { 74 struct ether_addr mc_addr; 75 SLIST_ENTRY(vlan_mc_entry) mc_entries; 76 }; 77 78 struct ifvlan { 79 struct arpcom ifv_ac; /* make this an interface */ 80 struct ifnet *ifv_p; /* parent inteface of this vlan */ 81 struct ifv_linkmib { 82 int ifvm_parent; 83 int ifvm_encaplen; /* encapsulation length */ 84 int ifvm_mtufudge; /* MTU fudged by this much */ 85 int ifvm_mintu; /* min transmission unit */ 86 u_int16_t ifvm_proto; /* encapsulation ethertype */ 87 u_int16_t ifvm_tag; /* tag to apply on packets leaving if */ 88 } ifv_mib; 89 SLIST_HEAD(__vlan_mchead, vlan_mc_entry) vlan_mc_listhead; 90 LIST_ENTRY(ifvlan) ifv_list; 91 int ifv_flags; 92 }; 93 #define ifv_if ifv_ac.ac_if 94 #define ifv_tag ifv_mib.ifvm_tag 95 #define ifv_encaplen ifv_mib.ifvm_encaplen 96 #define ifv_mtufudge ifv_mib.ifvm_mtufudge 97 #define ifv_mintu ifv_mib.ifvm_mintu 98 99 #define IFVF_PROMISC 0x01 /* promiscuous mode enabled */ 100 101 SYSCTL_DECL(_net_link); 102 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 103 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 104 105 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface"); 106 static LIST_HEAD(, ifvlan) ifv_list; 107 108 /* 109 * Locking: one lock is used to guard both the ifv_list and modification 110 * to vlan data structures. We are rather conservative here; probably 111 * more than necessary. 112 */ 113 static struct mtx ifv_mtx; 114 #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, VLANNAME, NULL, MTX_DEF) 115 #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx) 116 #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED) 117 #define VLAN_LOCK() mtx_lock(&ifv_mtx) 118 #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx) 119 120 static int vlan_clone_create(struct if_clone *, int); 121 static void vlan_clone_destroy(struct ifnet *); 122 static void vlan_start(struct ifnet *ifp); 123 static void vlan_ifinit(void *foo); 124 static void vlan_input(struct ifnet *ifp, struct mbuf *m); 125 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 126 static int vlan_setmulti(struct ifnet *ifp); 127 static int vlan_unconfig(struct ifnet *ifp); 128 static int vlan_config(struct ifvlan *ifv, struct ifnet *p); 129 static void vlan_link_state(struct ifnet *ifp, int link); 130 131 struct if_clone vlan_cloner = IF_CLONE_INITIALIZER(VLANNAME, 132 vlan_clone_create, vlan_clone_destroy, 0, IF_MAXUNIT); 133 134 /* 135 * Program our multicast filter. What we're actually doing is 136 * programming the multicast filter of the parent. This has the 137 * side effect of causing the parent interface to receive multicast 138 * traffic that it doesn't really want, which ends up being discarded 139 * later by the upper protocol layers. Unfortunately, there's no way 140 * to avoid this: there really is only one physical interface. 141 */ 142 static int 143 vlan_setmulti(struct ifnet *ifp) 144 { 145 struct ifnet *ifp_p; 146 struct ifmultiaddr *ifma, *rifma = NULL; 147 struct ifvlan *sc; 148 struct vlan_mc_entry *mc = NULL; 149 struct sockaddr_dl sdl; 150 int error; 151 152 /* Find the parent. */ 153 sc = ifp->if_softc; 154 ifp_p = sc->ifv_p; 155 156 /* 157 * If we don't have a parent, just remember the membership for 158 * when we do. 159 */ 160 if (ifp_p == NULL) 161 return(0); 162 163 bzero((char *)&sdl, sizeof sdl); 164 sdl.sdl_len = sizeof sdl; 165 sdl.sdl_family = AF_LINK; 166 sdl.sdl_index = ifp_p->if_index; 167 sdl.sdl_type = IFT_ETHER; 168 sdl.sdl_alen = ETHER_ADDR_LEN; 169 170 /* First, remove any existing filter entries. */ 171 while(SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) { 172 mc = SLIST_FIRST(&sc->vlan_mc_listhead); 173 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 174 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl); 175 if (error) 176 return(error); 177 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 178 free(mc, M_VLAN); 179 } 180 181 /* Now program new ones. */ 182 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 183 if (ifma->ifma_addr->sa_family != AF_LINK) 184 continue; 185 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK); 186 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 187 (char *)&mc->mc_addr, ETHER_ADDR_LEN); 188 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 189 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 190 LLADDR(&sdl), ETHER_ADDR_LEN); 191 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 192 if (error) 193 return(error); 194 } 195 196 return(0); 197 } 198 199 /* 200 * VLAN support can be loaded as a module. The only place in the 201 * system that's intimately aware of this is ether_input. We hook 202 * into this code through vlan_input_p which is defined there and 203 * set here. Noone else in the system should be aware of this so 204 * we use an explicit reference here. 205 * 206 * NB: Noone should ever need to check if vlan_input_p is null or 207 * not. This is because interfaces have a count of the number 208 * of active vlans (if_nvlans) and this should never be bumped 209 * except by vlan_config--which is in this module so therefore 210 * the module must be loaded and vlan_input_p must be non-NULL. 211 */ 212 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 213 214 /* For MII eyes only... */ 215 extern void (*vlan_link_state_p)(struct ifnet *, int); 216 217 static int 218 vlan_modevent(module_t mod, int type, void *data) 219 { 220 221 switch (type) { 222 case MOD_LOAD: 223 LIST_INIT(&ifv_list); 224 VLAN_LOCK_INIT(); 225 vlan_input_p = vlan_input; 226 vlan_link_state_p = vlan_link_state; 227 if_clone_attach(&vlan_cloner); 228 break; 229 case MOD_UNLOAD: 230 if_clone_detach(&vlan_cloner); 231 vlan_input_p = NULL; 232 vlan_link_state_p = NULL; 233 while (!LIST_EMPTY(&ifv_list)) 234 vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if); 235 VLAN_LOCK_DESTROY(); 236 break; 237 } 238 return 0; 239 } 240 241 static moduledata_t vlan_mod = { 242 "if_vlan", 243 vlan_modevent, 244 0 245 }; 246 247 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 248 249 static int 250 vlan_clone_create(struct if_clone *ifc, int unit) 251 { 252 struct ifvlan *ifv; 253 struct ifnet *ifp; 254 255 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 256 ifp = &ifv->ifv_if; 257 SLIST_INIT(&ifv->vlan_mc_listhead); 258 259 ifp->if_softc = ifv; 260 if_initname(ifp, ifc->ifc_name, unit); 261 /* NB: flags are not set here */ 262 ifp->if_linkmib = &ifv->ifv_mib; 263 ifp->if_linkmiblen = sizeof ifv->ifv_mib; 264 /* NB: mtu is not set here */ 265 266 ifp->if_init = vlan_ifinit; 267 ifp->if_start = vlan_start; 268 ifp->if_ioctl = vlan_ioctl; 269 ifp->if_snd.ifq_maxlen = ifqmaxlen; 270 ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr); 271 /* Now undo some of the damage... */ 272 ifp->if_baudrate = 0; 273 ifp->if_type = IFT_L2VLAN; 274 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 275 276 VLAN_LOCK(); 277 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list); 278 VLAN_UNLOCK(); 279 280 return (0); 281 } 282 283 static void 284 vlan_clone_destroy(struct ifnet *ifp) 285 { 286 struct ifvlan *ifv = ifp->if_softc; 287 288 VLAN_LOCK(); 289 LIST_REMOVE(ifv, ifv_list); 290 vlan_unconfig(ifp); 291 VLAN_UNLOCK(); 292 293 ether_ifdetach(ifp); 294 295 free(ifv, M_VLAN); 296 } 297 298 static void 299 vlan_ifinit(void *foo) 300 { 301 return; 302 } 303 304 static void 305 vlan_start(struct ifnet *ifp) 306 { 307 struct ifvlan *ifv; 308 struct ifnet *p; 309 struct ether_vlan_header *evl; 310 struct mbuf *m; 311 312 ifv = ifp->if_softc; 313 p = ifv->ifv_p; 314 315 ifp->if_flags |= IFF_OACTIVE; 316 for (;;) { 317 IF_DEQUEUE(&ifp->if_snd, m); 318 if (m == 0) 319 break; 320 BPF_MTAP(ifp, m); 321 322 /* 323 * Do not run parent's if_start() if the parent is not up, 324 * or parent's driver will cause a system crash. 325 */ 326 if ((p->if_flags & (IFF_UP | IFF_RUNNING)) != 327 (IFF_UP | IFF_RUNNING)) { 328 m_freem(m); 329 ifp->if_collisions++; 330 continue; 331 } 332 333 /* 334 * If underlying interface can do VLAN tag insertion itself, 335 * just pass the packet along. However, we need some way to 336 * tell the interface where the packet came from so that it 337 * knows how to find the VLAN tag to use, so we attach a 338 * packet tag that holds it. 339 */ 340 if (p->if_capabilities & IFCAP_VLAN_HWTAGGING) { 341 struct m_tag *mtag = m_tag_alloc(MTAG_VLAN, 342 MTAG_VLAN_TAG, 343 sizeof (u_int), 344 M_NOWAIT); 345 if (mtag == NULL) { 346 ifp->if_oerrors++; 347 m_freem(m); 348 continue; 349 } 350 *(u_int*)(mtag+1) = ifv->ifv_tag; 351 m_tag_prepend(m, mtag); 352 } else { 353 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT); 354 if (m == NULL) { 355 if_printf(ifp, "unable to prepend VLAN header"); 356 ifp->if_oerrors++; 357 continue; 358 } 359 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 360 361 if (m->m_len < sizeof(*evl)) { 362 m = m_pullup(m, sizeof(*evl)); 363 if (m == NULL) { 364 if_printf(ifp, 365 "cannot pullup VLAN header"); 366 ifp->if_oerrors++; 367 continue; 368 } 369 } 370 371 /* 372 * Transform the Ethernet header into an Ethernet header 373 * with 802.1Q encapsulation. 374 */ 375 bcopy(mtod(m, char *) + ifv->ifv_encaplen, 376 mtod(m, char *), ETHER_HDR_LEN); 377 evl = mtod(m, struct ether_vlan_header *); 378 evl->evl_proto = evl->evl_encap_proto; 379 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 380 evl->evl_tag = htons(ifv->ifv_tag); 381 #ifdef DEBUG 382 printf("vlan_start: %*D\n", (int)sizeof *evl, 383 (unsigned char *)evl, ":"); 384 #endif 385 } 386 387 /* 388 * Send it, precisely as ether_output() would have. 389 * We are already running at splimp. 390 */ 391 if (IF_HANDOFF(&p->if_snd, m, p)) 392 ifp->if_opackets++; 393 else 394 ifp->if_oerrors++; 395 } 396 ifp->if_flags &= ~IFF_OACTIVE; 397 398 return; 399 } 400 401 static void 402 vlan_input(struct ifnet *ifp, struct mbuf *m) 403 { 404 struct ether_vlan_header *evl; 405 struct ifvlan *ifv; 406 struct m_tag *mtag; 407 u_int tag; 408 409 mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL); 410 if (mtag != NULL) { 411 /* 412 * Packet is tagged, m contains a normal 413 * Ethernet frame; the tag is stored out-of-band. 414 */ 415 tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)); 416 m_tag_delete(m, mtag); 417 } else { 418 switch (ifp->if_type) { 419 case IFT_ETHER: 420 if (m->m_len < sizeof (*evl) && 421 (m = m_pullup(m, sizeof (*evl))) == NULL) { 422 if_printf(ifp, "cannot pullup VLAN header\n"); 423 return; 424 } 425 evl = mtod(m, struct ether_vlan_header *); 426 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN, 427 ("vlan_input: bad encapsulated protocols (%u)", 428 ntohs(evl->evl_encap_proto))); 429 430 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 431 432 /* 433 * Restore the original ethertype. We'll remove 434 * the encapsulation after we've found the vlan 435 * interface corresponding to the tag. 436 */ 437 evl->evl_encap_proto = evl->evl_proto; 438 break; 439 default: 440 tag = (u_int) -1; 441 #ifdef DIAGNOSTIC 442 panic("vlan_input: unsupported if type %u", ifp->if_type); 443 #endif 444 break; 445 } 446 } 447 448 VLAN_LOCK(); 449 LIST_FOREACH(ifv, &ifv_list, ifv_list) 450 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag) 451 break; 452 453 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 454 VLAN_UNLOCK(); 455 m_freem(m); 456 ifp->if_noproto++; 457 return; 458 } 459 VLAN_UNLOCK(); /* XXX extend below? */ 460 461 if (mtag == NULL) { 462 /* 463 * Packet had an in-line encapsulation header; 464 * remove it. The original header has already 465 * been fixed up above. 466 */ 467 bcopy(mtod(m, caddr_t), 468 mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN, 469 ETHER_HDR_LEN); 470 m_adj(m, ETHER_VLAN_ENCAP_LEN); 471 } 472 473 m->m_pkthdr.rcvif = &ifv->ifv_if; 474 ifv->ifv_if.if_ipackets++; 475 476 /* Pass it back through the parent's input routine. */ 477 (*ifp->if_input)(&ifv->ifv_if, m); 478 } 479 480 static int 481 vlan_config(struct ifvlan *ifv, struct ifnet *p) 482 { 483 struct ifaddr *ifa1, *ifa2; 484 struct sockaddr_dl *sdl1, *sdl2; 485 486 VLAN_LOCK_ASSERT(); 487 488 if (p->if_data.ifi_type != IFT_ETHER) 489 return EPROTONOSUPPORT; 490 if (ifv->ifv_p) 491 return EBUSY; 492 493 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 494 ifv->ifv_mintu = ETHERMIN; 495 ifv->ifv_flags = 0; 496 497 /* 498 * If the parent supports the VLAN_MTU capability, 499 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 500 * enable it. 501 */ 502 p->if_nvlans++; 503 if (p->if_nvlans == 1 && (p->if_capabilities & IFCAP_VLAN_MTU) != 0) { 504 /* 505 * Enable Tx/Rx of VLAN-sized frames. 506 */ 507 p->if_capenable |= IFCAP_VLAN_MTU; 508 if (p->if_flags & IFF_UP) { 509 struct ifreq ifr; 510 int error; 511 512 ifr.ifr_flags = p->if_flags; 513 error = (*p->if_ioctl)(p, SIOCSIFFLAGS, 514 (caddr_t) &ifr); 515 if (error) { 516 p->if_nvlans--; 517 if (p->if_nvlans == 0) 518 p->if_capenable &= ~IFCAP_VLAN_MTU; 519 return (error); 520 } 521 } 522 ifv->ifv_mtufudge = 0; 523 } else if ((p->if_capabilities & IFCAP_VLAN_MTU) == 0) { 524 /* 525 * Fudge the MTU by the encapsulation size. This 526 * makes us incompatible with strictly compliant 527 * 802.1Q implementations, but allows us to use 528 * the feature with other NetBSD implementations, 529 * which might still be useful. 530 */ 531 ifv->ifv_mtufudge = ifv->ifv_encaplen; 532 } 533 534 ifv->ifv_p = p; 535 ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge; 536 /* 537 * Copy only a selected subset of flags from the parent. 538 * Other flags are none of our business. 539 */ 540 ifv->ifv_if.if_flags = (p->if_flags & 541 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT)); 542 ifv->ifv_if.if_link_state = p->if_link_state; 543 544 /* 545 * If the parent interface can do hardware-assisted 546 * VLAN encapsulation, then propagate its hardware- 547 * assisted checksumming flags. 548 */ 549 if (p->if_capabilities & IFCAP_VLAN_HWTAGGING) 550 ifv->ifv_if.if_capabilities |= p->if_capabilities & IFCAP_HWCSUM; 551 552 /* 553 * Set up our ``Ethernet address'' to reflect the underlying 554 * physical interface's. 555 */ 556 ifa1 = ifaddr_byindex(ifv->ifv_if.if_index); 557 ifa2 = ifaddr_byindex(p->if_index); 558 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr; 559 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr; 560 sdl1->sdl_type = IFT_ETHER; 561 sdl1->sdl_alen = ETHER_ADDR_LEN; 562 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN); 563 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 564 565 /* 566 * Configure multicast addresses that may already be 567 * joined on the vlan device. 568 */ 569 (void)vlan_setmulti(&ifv->ifv_if); 570 571 return 0; 572 } 573 574 static int 575 vlan_unconfig(struct ifnet *ifp) 576 { 577 struct ifaddr *ifa; 578 struct sockaddr_dl *sdl; 579 struct vlan_mc_entry *mc; 580 struct ifvlan *ifv; 581 struct ifnet *p; 582 int error; 583 584 VLAN_LOCK_ASSERT(); 585 586 ifv = ifp->if_softc; 587 p = ifv->ifv_p; 588 589 if (p) { 590 struct sockaddr_dl sdl; 591 592 /* 593 * Since the interface is being unconfigured, we need to 594 * empty the list of multicast groups that we may have joined 595 * while we were alive from the parent's list. 596 */ 597 bzero((char *)&sdl, sizeof sdl); 598 sdl.sdl_len = sizeof sdl; 599 sdl.sdl_family = AF_LINK; 600 sdl.sdl_index = p->if_index; 601 sdl.sdl_type = IFT_ETHER; 602 sdl.sdl_alen = ETHER_ADDR_LEN; 603 604 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) { 605 mc = SLIST_FIRST(&ifv->vlan_mc_listhead); 606 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 607 error = if_delmulti(p, (struct sockaddr *)&sdl); 608 if (error) 609 return(error); 610 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 611 free(mc, M_VLAN); 612 } 613 614 p->if_nvlans--; 615 if (p->if_nvlans == 0) { 616 /* 617 * Disable Tx/Rx of VLAN-sized frames. 618 */ 619 p->if_capenable &= ~IFCAP_VLAN_MTU; 620 if (p->if_flags & IFF_UP) { 621 struct ifreq ifr; 622 623 ifr.ifr_flags = p->if_flags; 624 (*p->if_ioctl)(p, SIOCSIFFLAGS, (caddr_t) &ifr); 625 } 626 } 627 } 628 629 /* Disconnect from parent. */ 630 ifv->ifv_p = NULL; 631 ifv->ifv_if.if_mtu = ETHERMTU; /* XXX why not 0? */ 632 ifv->ifv_flags = 0; 633 ifv->ifv_if.if_link_state = LINK_STATE_UNKNOWN; 634 635 /* Clear our MAC address. */ 636 ifa = ifaddr_byindex(ifv->ifv_if.if_index); 637 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 638 sdl->sdl_type = IFT_ETHER; 639 sdl->sdl_alen = ETHER_ADDR_LEN; 640 bzero(LLADDR(sdl), ETHER_ADDR_LEN); 641 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 642 643 return 0; 644 } 645 646 static int 647 vlan_set_promisc(struct ifnet *ifp) 648 { 649 struct ifvlan *ifv = ifp->if_softc; 650 int error = 0; 651 652 if ((ifp->if_flags & IFF_PROMISC) != 0) { 653 if ((ifv->ifv_flags & IFVF_PROMISC) == 0) { 654 error = ifpromisc(ifv->ifv_p, 1); 655 if (error == 0) 656 ifv->ifv_flags |= IFVF_PROMISC; 657 } 658 } else { 659 if ((ifv->ifv_flags & IFVF_PROMISC) != 0) { 660 error = ifpromisc(ifv->ifv_p, 0); 661 if (error == 0) 662 ifv->ifv_flags &= ~IFVF_PROMISC; 663 } 664 } 665 666 return (error); 667 } 668 669 /* Inform all vlans that their parent has changed link state */ 670 static void 671 vlan_link_state(struct ifnet *ifp, int link) 672 { 673 struct ifvlan *ifv; 674 675 VLAN_LOCK(); 676 LIST_FOREACH(ifv, &ifv_list, ifv_list) { 677 if (ifv->ifv_p == ifp) { 678 ifv->ifv_if.if_link_state = ifv->ifv_p->if_link_state; 679 rt_ifmsg(&(ifv->ifv_if)); 680 KNOTE(&ifp->if_klist, link); 681 } 682 } 683 VLAN_UNLOCK(); 684 } 685 686 static int 687 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 688 { 689 struct ifaddr *ifa; 690 struct ifnet *p; 691 struct ifreq *ifr; 692 struct ifvlan *ifv; 693 struct vlanreq vlr; 694 int error = 0; 695 696 ifr = (struct ifreq *)data; 697 ifa = (struct ifaddr *)data; 698 ifv = ifp->if_softc; 699 700 switch (cmd) { 701 case SIOCSIFADDR: 702 ifp->if_flags |= IFF_UP; 703 704 switch (ifa->ifa_addr->sa_family) { 705 #ifdef INET 706 case AF_INET: 707 arp_ifinit(&ifv->ifv_if, ifa); 708 break; 709 #endif 710 default: 711 break; 712 } 713 break; 714 715 case SIOCGIFADDR: 716 { 717 struct sockaddr *sa; 718 719 sa = (struct sockaddr *) &ifr->ifr_data; 720 bcopy(IFP2AC(ifp)->ac_enaddr, 721 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 722 } 723 break; 724 725 case SIOCGIFMEDIA: 726 VLAN_LOCK(); 727 if (ifv->ifv_p != NULL) { 728 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, 729 SIOCGIFMEDIA, data); 730 VLAN_UNLOCK(); 731 /* Limit the result to the parent's current config. */ 732 if (error == 0) { 733 struct ifmediareq *ifmr; 734 735 ifmr = (struct ifmediareq *) data; 736 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 737 ifmr->ifm_count = 1; 738 error = copyout(&ifmr->ifm_current, 739 ifmr->ifm_ulist, 740 sizeof(int)); 741 } 742 } 743 } else { 744 VLAN_UNLOCK(); 745 error = EINVAL; 746 } 747 break; 748 749 case SIOCSIFMEDIA: 750 error = EINVAL; 751 break; 752 753 case SIOCSIFMTU: 754 /* 755 * Set the interface MTU. 756 */ 757 VLAN_LOCK(); 758 if (ifv->ifv_p != NULL) { 759 if (ifr->ifr_mtu > 760 (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) || 761 ifr->ifr_mtu < 762 (ifv->ifv_mintu - ifv->ifv_mtufudge)) 763 error = EINVAL; 764 else 765 ifp->if_mtu = ifr->ifr_mtu; 766 } else 767 error = EINVAL; 768 VLAN_UNLOCK(); 769 break; 770 771 case SIOCSETVLAN: 772 error = copyin(ifr->ifr_data, &vlr, sizeof vlr); 773 if (error) 774 break; 775 if (vlr.vlr_parent[0] == '\0') { 776 VLAN_LOCK(); 777 vlan_unconfig(ifp); 778 if (ifp->if_flags & IFF_UP) 779 if_down(ifp); 780 ifp->if_flags &= ~IFF_RUNNING; 781 VLAN_UNLOCK(); 782 break; 783 } 784 p = ifunit(vlr.vlr_parent); 785 if (p == 0) { 786 error = ENOENT; 787 break; 788 } 789 /* 790 * Don't let the caller set up a VLAN tag with 791 * anything except VLID bits. 792 */ 793 if (vlr.vlr_tag & ~EVL_VLID_MASK) { 794 error = EINVAL; 795 break; 796 } 797 VLAN_LOCK(); 798 error = vlan_config(ifv, p); 799 if (error) { 800 VLAN_UNLOCK(); 801 break; 802 } 803 ifv->ifv_tag = vlr.vlr_tag; 804 ifp->if_flags |= IFF_RUNNING; 805 VLAN_UNLOCK(); 806 807 /* Update promiscuous mode, if necessary. */ 808 vlan_set_promisc(ifp); 809 break; 810 811 case SIOCGETVLAN: 812 bzero(&vlr, sizeof vlr); 813 VLAN_LOCK(); 814 if (ifv->ifv_p) { 815 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname, 816 sizeof(vlr.vlr_parent)); 817 vlr.vlr_tag = ifv->ifv_tag; 818 } 819 VLAN_UNLOCK(); 820 error = copyout(&vlr, ifr->ifr_data, sizeof vlr); 821 break; 822 823 case SIOCSIFFLAGS: 824 /* 825 * For promiscuous mode, we enable promiscuous mode on 826 * the parent if we need promiscuous on the VLAN interface. 827 */ 828 if (ifv->ifv_p != NULL) 829 error = vlan_set_promisc(ifp); 830 break; 831 832 case SIOCADDMULTI: 833 case SIOCDELMULTI: 834 error = vlan_setmulti(ifp); 835 break; 836 default: 837 error = EINVAL; 838 } 839 return error; 840 } 841