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 MODULE_DEPEND(if_vlan, miibus, 1, 1, 1); 249 250 static int 251 vlan_clone_create(struct if_clone *ifc, int unit) 252 { 253 struct ifvlan *ifv; 254 struct ifnet *ifp; 255 256 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 257 ifp = &ifv->ifv_if; 258 SLIST_INIT(&ifv->vlan_mc_listhead); 259 260 ifp->if_softc = ifv; 261 if_initname(ifp, ifc->ifc_name, unit); 262 /* NB: flags are not set here */ 263 ifp->if_linkmib = &ifv->ifv_mib; 264 ifp->if_linkmiblen = sizeof ifv->ifv_mib; 265 /* NB: mtu is not set here */ 266 267 ifp->if_init = vlan_ifinit; 268 ifp->if_start = vlan_start; 269 ifp->if_ioctl = vlan_ioctl; 270 ifp->if_snd.ifq_maxlen = ifqmaxlen; 271 ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr); 272 /* Now undo some of the damage... */ 273 ifp->if_baudrate = 0; 274 ifp->if_type = IFT_L2VLAN; 275 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 276 277 VLAN_LOCK(); 278 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list); 279 VLAN_UNLOCK(); 280 281 return (0); 282 } 283 284 static void 285 vlan_clone_destroy(struct ifnet *ifp) 286 { 287 struct ifvlan *ifv = ifp->if_softc; 288 289 VLAN_LOCK(); 290 LIST_REMOVE(ifv, ifv_list); 291 vlan_unconfig(ifp); 292 VLAN_UNLOCK(); 293 294 ether_ifdetach(ifp); 295 296 free(ifv, M_VLAN); 297 } 298 299 static void 300 vlan_ifinit(void *foo) 301 { 302 return; 303 } 304 305 static void 306 vlan_start(struct ifnet *ifp) 307 { 308 struct ifvlan *ifv; 309 struct ifnet *p; 310 struct ether_vlan_header *evl; 311 struct mbuf *m; 312 313 ifv = ifp->if_softc; 314 p = ifv->ifv_p; 315 316 ifp->if_flags |= IFF_OACTIVE; 317 for (;;) { 318 IF_DEQUEUE(&ifp->if_snd, m); 319 if (m == 0) 320 break; 321 BPF_MTAP(ifp, m); 322 323 /* 324 * Do not run parent's if_start() if the parent is not up, 325 * or parent's driver will cause a system crash. 326 */ 327 if ((p->if_flags & (IFF_UP | IFF_RUNNING)) != 328 (IFF_UP | IFF_RUNNING)) { 329 m_freem(m); 330 ifp->if_collisions++; 331 continue; 332 } 333 334 /* 335 * If underlying interface can do VLAN tag insertion itself, 336 * just pass the packet along. However, we need some way to 337 * tell the interface where the packet came from so that it 338 * knows how to find the VLAN tag to use, so we attach a 339 * packet tag that holds it. 340 */ 341 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) { 342 struct m_tag *mtag = m_tag_alloc(MTAG_VLAN, 343 MTAG_VLAN_TAG, 344 sizeof (u_int), 345 M_NOWAIT); 346 if (mtag == NULL) { 347 ifp->if_oerrors++; 348 m_freem(m); 349 continue; 350 } 351 *(u_int*)(mtag+1) = ifv->ifv_tag; 352 m_tag_prepend(m, mtag); 353 } else { 354 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT); 355 if (m == NULL) { 356 if_printf(ifp, "unable to prepend VLAN header"); 357 ifp->if_oerrors++; 358 continue; 359 } 360 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 361 362 if (m->m_len < sizeof(*evl)) { 363 m = m_pullup(m, sizeof(*evl)); 364 if (m == NULL) { 365 if_printf(ifp, 366 "cannot pullup VLAN header"); 367 ifp->if_oerrors++; 368 continue; 369 } 370 } 371 372 /* 373 * Transform the Ethernet header into an Ethernet header 374 * with 802.1Q encapsulation. 375 */ 376 bcopy(mtod(m, char *) + ifv->ifv_encaplen, 377 mtod(m, char *), ETHER_HDR_LEN); 378 evl = mtod(m, struct ether_vlan_header *); 379 evl->evl_proto = evl->evl_encap_proto; 380 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 381 evl->evl_tag = htons(ifv->ifv_tag); 382 #ifdef DEBUG 383 printf("vlan_start: %*D\n", (int)sizeof *evl, 384 (unsigned char *)evl, ":"); 385 #endif 386 } 387 388 /* 389 * Send it, precisely as ether_output() would have. 390 * We are already running at splimp. 391 */ 392 if (IF_HANDOFF(&p->if_snd, m, p)) 393 ifp->if_opackets++; 394 else 395 ifp->if_oerrors++; 396 } 397 ifp->if_flags &= ~IFF_OACTIVE; 398 399 return; 400 } 401 402 static void 403 vlan_input(struct ifnet *ifp, struct mbuf *m) 404 { 405 struct ether_vlan_header *evl; 406 struct ifvlan *ifv; 407 struct m_tag *mtag; 408 u_int tag; 409 410 mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL); 411 if (mtag != NULL) { 412 /* 413 * Packet is tagged, m contains a normal 414 * Ethernet frame; the tag is stored out-of-band. 415 */ 416 tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)); 417 m_tag_delete(m, mtag); 418 } else { 419 switch (ifp->if_type) { 420 case IFT_ETHER: 421 if (m->m_len < sizeof (*evl) && 422 (m = m_pullup(m, sizeof (*evl))) == NULL) { 423 if_printf(ifp, "cannot pullup VLAN header\n"); 424 return; 425 } 426 evl = mtod(m, struct ether_vlan_header *); 427 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN, 428 ("vlan_input: bad encapsulated protocols (%u)", 429 ntohs(evl->evl_encap_proto))); 430 431 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 432 433 /* 434 * Restore the original ethertype. We'll remove 435 * the encapsulation after we've found the vlan 436 * interface corresponding to the tag. 437 */ 438 evl->evl_encap_proto = evl->evl_proto; 439 break; 440 default: 441 tag = (u_int) -1; 442 #ifdef DIAGNOSTIC 443 panic("vlan_input: unsupported if type %u", ifp->if_type); 444 #endif 445 break; 446 } 447 } 448 449 VLAN_LOCK(); 450 LIST_FOREACH(ifv, &ifv_list, ifv_list) 451 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag) 452 break; 453 454 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 455 VLAN_UNLOCK(); 456 m_freem(m); 457 ifp->if_noproto++; 458 return; 459 } 460 VLAN_UNLOCK(); /* XXX extend below? */ 461 462 if (mtag == NULL) { 463 /* 464 * Packet had an in-line encapsulation header; 465 * remove it. The original header has already 466 * been fixed up above. 467 */ 468 bcopy(mtod(m, caddr_t), 469 mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN, 470 ETHER_HDR_LEN); 471 m_adj(m, ETHER_VLAN_ENCAP_LEN); 472 } 473 474 m->m_pkthdr.rcvif = &ifv->ifv_if; 475 ifv->ifv_if.if_ipackets++; 476 477 /* Pass it back through the parent's input routine. */ 478 (*ifp->if_input)(&ifv->ifv_if, m); 479 } 480 481 static int 482 vlan_config(struct ifvlan *ifv, struct ifnet *p) 483 { 484 struct ifaddr *ifa1, *ifa2; 485 struct sockaddr_dl *sdl1, *sdl2; 486 487 VLAN_LOCK_ASSERT(); 488 489 if (p->if_data.ifi_type != IFT_ETHER) 490 return EPROTONOSUPPORT; 491 if (ifv->ifv_p) 492 return EBUSY; 493 494 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 495 ifv->ifv_mintu = ETHERMIN; 496 ifv->ifv_flags = 0; 497 498 /* 499 * If the parent supports the VLAN_MTU capability, 500 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 501 * use it. 502 * First of all, enable Tx/Rx of such extended frames on the 503 * parent if it's disabled and we're the first to attach. 504 */ 505 p->if_nvlans++; 506 if (p->if_nvlans == 1 && 507 (p->if_capabilities & IFCAP_VLAN_MTU) && 508 (p->if_capenable & IFCAP_VLAN_MTU) == 0) { 509 struct ifreq ifr; 510 int error; 511 512 ifr.ifr_reqcap = p->if_capenable | IFCAP_VLAN_MTU; 513 error = (*p->if_ioctl)(p, SIOCSIFCAP, (caddr_t) &ifr); 514 if (error) { 515 p->if_nvlans--; 516 return (error); 517 } 518 } 519 if (p->if_capenable & IFCAP_VLAN_MTU) { 520 /* 521 * No need to fudge the MTU since the parent can 522 * handle extended frames. 523 */ 524 ifv->ifv_mtufudge = 0; 525 } else { 526 /* 527 * Fudge the MTU by the encapsulation size. This 528 * makes us incompatible with strictly compliant 529 * 802.1Q implementations, but allows us to use 530 * the feature with other NetBSD implementations, 531 * which might still be useful. 532 */ 533 ifv->ifv_mtufudge = ifv->ifv_encaplen; 534 } 535 536 ifv->ifv_p = p; 537 ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge; 538 /* 539 * Copy only a selected subset of flags from the parent. 540 * Other flags are none of our business. 541 */ 542 ifv->ifv_if.if_flags = (p->if_flags & 543 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT)); 544 ifv->ifv_if.if_link_state = p->if_link_state; 545 546 #if 0 547 /* 548 * Not ready yet. We need notification from the parent 549 * when hw checksumming flags in its if_capenable change. 550 * Flags set in if_capabilities only are useless. 551 */ 552 /* 553 * If the parent interface can do hardware-assisted 554 * VLAN encapsulation, then propagate its hardware- 555 * assisted checksumming flags. 556 */ 557 if (p->if_capabilities & IFCAP_VLAN_HWTAGGING) 558 ifv->ifv_if.if_capabilities |= p->if_capabilities & IFCAP_HWCSUM; 559 #endif 560 561 /* 562 * Set up our ``Ethernet address'' to reflect the underlying 563 * physical interface's. 564 */ 565 ifa1 = ifaddr_byindex(ifv->ifv_if.if_index); 566 ifa2 = ifaddr_byindex(p->if_index); 567 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr; 568 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr; 569 sdl1->sdl_type = IFT_ETHER; 570 sdl1->sdl_alen = ETHER_ADDR_LEN; 571 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN); 572 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 573 574 /* 575 * Configure multicast addresses that may already be 576 * joined on the vlan device. 577 */ 578 (void)vlan_setmulti(&ifv->ifv_if); 579 580 return 0; 581 } 582 583 static int 584 vlan_unconfig(struct ifnet *ifp) 585 { 586 struct ifaddr *ifa; 587 struct sockaddr_dl *sdl; 588 struct vlan_mc_entry *mc; 589 struct ifvlan *ifv; 590 struct ifnet *p; 591 int error; 592 593 VLAN_LOCK_ASSERT(); 594 595 ifv = ifp->if_softc; 596 p = ifv->ifv_p; 597 598 if (p) { 599 struct sockaddr_dl sdl; 600 601 /* 602 * Since the interface is being unconfigured, we need to 603 * empty the list of multicast groups that we may have joined 604 * while we were alive from the parent's list. 605 */ 606 bzero((char *)&sdl, sizeof sdl); 607 sdl.sdl_len = sizeof sdl; 608 sdl.sdl_family = AF_LINK; 609 sdl.sdl_index = p->if_index; 610 sdl.sdl_type = IFT_ETHER; 611 sdl.sdl_alen = ETHER_ADDR_LEN; 612 613 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) { 614 mc = SLIST_FIRST(&ifv->vlan_mc_listhead); 615 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 616 error = if_delmulti(p, (struct sockaddr *)&sdl); 617 if (error) 618 return(error); 619 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 620 free(mc, M_VLAN); 621 } 622 623 p->if_nvlans--; 624 if (p->if_nvlans == 0) { 625 struct ifreq ifr; 626 627 /* 628 * Try to disable Tx/Rx of VLAN-sized frames. 629 * This may have no effect for some interfaces, 630 * but only the parent driver knows that. 631 */ 632 ifr.ifr_reqcap = p->if_capenable & ~IFCAP_VLAN_MTU; 633 (*p->if_ioctl)(p, SIOCSIFCAP, (caddr_t) &ifr); 634 } 635 } 636 637 /* Disconnect from parent. */ 638 ifv->ifv_p = NULL; 639 ifv->ifv_if.if_mtu = ETHERMTU; /* XXX why not 0? */ 640 ifv->ifv_flags = 0; 641 ifv->ifv_if.if_link_state = LINK_STATE_UNKNOWN; 642 643 /* Clear our MAC address. */ 644 ifa = ifaddr_byindex(ifv->ifv_if.if_index); 645 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 646 sdl->sdl_type = IFT_ETHER; 647 sdl->sdl_alen = ETHER_ADDR_LEN; 648 bzero(LLADDR(sdl), ETHER_ADDR_LEN); 649 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 650 651 return 0; 652 } 653 654 static int 655 vlan_set_promisc(struct ifnet *ifp) 656 { 657 struct ifvlan *ifv = ifp->if_softc; 658 int error = 0; 659 660 if ((ifp->if_flags & IFF_PROMISC) != 0) { 661 if ((ifv->ifv_flags & IFVF_PROMISC) == 0) { 662 error = ifpromisc(ifv->ifv_p, 1); 663 if (error == 0) 664 ifv->ifv_flags |= IFVF_PROMISC; 665 } 666 } else { 667 if ((ifv->ifv_flags & IFVF_PROMISC) != 0) { 668 error = ifpromisc(ifv->ifv_p, 0); 669 if (error == 0) 670 ifv->ifv_flags &= ~IFVF_PROMISC; 671 } 672 } 673 674 return (error); 675 } 676 677 /* Inform all vlans that their parent has changed link state */ 678 static void 679 vlan_link_state(struct ifnet *ifp, int link) 680 { 681 struct ifvlan *ifv; 682 683 VLAN_LOCK(); 684 LIST_FOREACH(ifv, &ifv_list, ifv_list) { 685 if (ifv->ifv_p == ifp) { 686 ifv->ifv_if.if_link_state = ifv->ifv_p->if_link_state; 687 rt_ifmsg(&(ifv->ifv_if)); 688 KNOTE(&ifp->if_klist, link); 689 } 690 } 691 VLAN_UNLOCK(); 692 } 693 694 static int 695 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 696 { 697 struct ifaddr *ifa; 698 struct ifnet *p; 699 struct ifreq *ifr; 700 struct ifvlan *ifv; 701 struct vlanreq vlr; 702 int error = 0; 703 704 ifr = (struct ifreq *)data; 705 ifa = (struct ifaddr *)data; 706 ifv = ifp->if_softc; 707 708 switch (cmd) { 709 case SIOCSIFADDR: 710 ifp->if_flags |= IFF_UP; 711 712 switch (ifa->ifa_addr->sa_family) { 713 #ifdef INET 714 case AF_INET: 715 arp_ifinit(&ifv->ifv_if, ifa); 716 break; 717 #endif 718 default: 719 break; 720 } 721 break; 722 723 case SIOCGIFADDR: 724 { 725 struct sockaddr *sa; 726 727 sa = (struct sockaddr *) &ifr->ifr_data; 728 bcopy(IFP2AC(ifp)->ac_enaddr, 729 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 730 } 731 break; 732 733 case SIOCGIFMEDIA: 734 VLAN_LOCK(); 735 if (ifv->ifv_p != NULL) { 736 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, 737 SIOCGIFMEDIA, data); 738 VLAN_UNLOCK(); 739 /* Limit the result to the parent's current config. */ 740 if (error == 0) { 741 struct ifmediareq *ifmr; 742 743 ifmr = (struct ifmediareq *) data; 744 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 745 ifmr->ifm_count = 1; 746 error = copyout(&ifmr->ifm_current, 747 ifmr->ifm_ulist, 748 sizeof(int)); 749 } 750 } 751 } else { 752 VLAN_UNLOCK(); 753 error = EINVAL; 754 } 755 break; 756 757 case SIOCSIFMEDIA: 758 error = EINVAL; 759 break; 760 761 case SIOCSIFMTU: 762 /* 763 * Set the interface MTU. 764 */ 765 VLAN_LOCK(); 766 if (ifv->ifv_p != NULL) { 767 if (ifr->ifr_mtu > 768 (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) || 769 ifr->ifr_mtu < 770 (ifv->ifv_mintu - ifv->ifv_mtufudge)) 771 error = EINVAL; 772 else 773 ifp->if_mtu = ifr->ifr_mtu; 774 } else 775 error = EINVAL; 776 VLAN_UNLOCK(); 777 break; 778 779 case SIOCSETVLAN: 780 error = copyin(ifr->ifr_data, &vlr, sizeof vlr); 781 if (error) 782 break; 783 if (vlr.vlr_parent[0] == '\0') { 784 VLAN_LOCK(); 785 vlan_unconfig(ifp); 786 if (ifp->if_flags & IFF_UP) 787 if_down(ifp); 788 ifp->if_flags &= ~IFF_RUNNING; 789 VLAN_UNLOCK(); 790 break; 791 } 792 p = ifunit(vlr.vlr_parent); 793 if (p == 0) { 794 error = ENOENT; 795 break; 796 } 797 /* 798 * Don't let the caller set up a VLAN tag with 799 * anything except VLID bits. 800 */ 801 if (vlr.vlr_tag & ~EVL_VLID_MASK) { 802 error = EINVAL; 803 break; 804 } 805 VLAN_LOCK(); 806 error = vlan_config(ifv, p); 807 if (error) { 808 VLAN_UNLOCK(); 809 break; 810 } 811 ifv->ifv_tag = vlr.vlr_tag; 812 ifp->if_flags |= IFF_RUNNING; 813 VLAN_UNLOCK(); 814 815 /* Update promiscuous mode, if necessary. */ 816 vlan_set_promisc(ifp); 817 break; 818 819 case SIOCGETVLAN: 820 bzero(&vlr, sizeof vlr); 821 VLAN_LOCK(); 822 if (ifv->ifv_p) { 823 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname, 824 sizeof(vlr.vlr_parent)); 825 vlr.vlr_tag = ifv->ifv_tag; 826 } 827 VLAN_UNLOCK(); 828 error = copyout(&vlr, ifr->ifr_data, sizeof vlr); 829 break; 830 831 case SIOCSIFFLAGS: 832 /* 833 * For promiscuous mode, we enable promiscuous mode on 834 * the parent if we need promiscuous on the VLAN interface. 835 */ 836 if (ifv->ifv_p != NULL) 837 error = vlan_set_promisc(ifp); 838 break; 839 840 case SIOCADDMULTI: 841 case SIOCDELMULTI: 842 error = vlan_setmulti(ifp); 843 break; 844 default: 845 error = EINVAL; 846 } 847 return error; 848 } 849