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