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: src/sys/net/if_vlan.c,v 1.15.2.13 2003/02/14 22:25:58 fenner Exp $ 30 * $DragonFly: src/sys/net/vlan/if_vlan.c,v 1.38 2008/08/17 06:26:45 sephe Exp $ 31 */ 32 33 /* 34 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 35 * Might be extended some day to also handle IEEE 802.1p priority 36 * tagging. This is sort of sneaky in the implementation, since 37 * we need to pretend to be enough of an Ethernet implementation 38 * to make arp work. The way we do this is by telling everyone 39 * that we are an Ethernet, and then catch the packets that 40 * ether_output() left on our output queue queue when it calls 41 * if_start(), rewrite them for use by the real outgoing interface, 42 * and ask it to send them. 43 */ 44 45 #ifndef NVLAN 46 #include "use_vlan.h" 47 #endif 48 #include "opt_inet.h" 49 #include "opt_ethernet.h" 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/kernel.h> 54 #include <sys/malloc.h> 55 #include <sys/mbuf.h> 56 #include <sys/module.h> 57 #include <sys/queue.h> 58 #include <sys/socket.h> 59 #include <sys/sockio.h> 60 #include <sys/sysctl.h> 61 #include <sys/bus.h> 62 #include <sys/thread2.h> 63 64 #include <net/bpf.h> 65 #include <net/ethernet.h> 66 #include <net/if.h> 67 #include <net/if_arp.h> 68 #include <net/if_dl.h> 69 #include <net/if_types.h> 70 #include <net/ifq_var.h> 71 #include <net/if_clone.h> 72 #include <net/netmsg2.h> 73 74 #ifdef INET 75 #include <netinet/in.h> 76 #include <netinet/if_ether.h> 77 #endif 78 79 #include <net/vlan/if_vlan_var.h> 80 #include <net/vlan/if_vlan_ether.h> 81 82 struct ifvlan; 83 84 struct vlan_mc_entry { 85 struct ether_addr mc_addr; 86 SLIST_ENTRY(vlan_mc_entry) mc_entries; 87 }; 88 89 struct vlan_entry { 90 struct ifvlan *ifv; 91 LIST_ENTRY(vlan_entry) ifv_link; 92 }; 93 94 struct ifvlan { 95 struct arpcom ifv_ac; /* make this an interface */ 96 struct ifnet *ifv_p; /* parent inteface of this vlan */ 97 struct ifv_linkmib { 98 int ifvm_parent; 99 uint16_t ifvm_proto; /* encapsulation ethertype */ 100 uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 101 } ifv_mib; 102 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 103 LIST_ENTRY(ifvlan) ifv_list; 104 struct vlan_entry ifv_entries[1]; 105 }; 106 #define ifv_if ifv_ac.ac_if 107 #define ifv_tag ifv_mib.ifvm_tag 108 109 struct vlan_trunk { 110 LIST_HEAD(, vlan_entry) vlan_list; 111 }; 112 113 struct netmsg_vlan { 114 struct netmsg nv_nmsg; 115 struct ifvlan *nv_ifv; 116 struct ifnet *nv_ifp_p; 117 const char *nv_parent_name; 118 uint16_t nv_vlantag; 119 }; 120 121 #define VLANNAME "vlan" 122 123 SYSCTL_DECL(_net_link); 124 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 125 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 126 127 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface"); 128 static LIST_HEAD(, ifvlan) ifv_list; 129 130 static int vlan_clone_create(struct if_clone *, int); 131 static void vlan_clone_destroy(struct ifnet *); 132 static void vlan_ifdetach(void *, struct ifnet *); 133 134 static void vlan_init(void *); 135 static void vlan_start(struct ifnet *); 136 static int vlan_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *); 137 static void vlan_input2(struct mbuf *); 138 139 static void vlan_clrmulti(struct ifvlan *, struct ifnet *); 140 static int vlan_setmulti(struct ifvlan *, struct ifnet *); 141 static int vlan_config_multi(struct ifvlan *); 142 static int vlan_config(struct ifvlan *, const char *, uint16_t); 143 static int vlan_unconfig(struct ifvlan *); 144 static void vlan_link(struct ifvlan *, struct ifnet *); 145 static void vlan_unlink(struct ifvlan *, struct ifnet *); 146 147 static void vlan_config_dispatch(struct netmsg *); 148 static void vlan_unconfig_dispatch(struct netmsg *); 149 static void vlan_link_dispatch(struct netmsg *); 150 static void vlan_unlink_dispatch(struct netmsg *); 151 static void vlan_multi_dispatch(struct netmsg *); 152 static void vlan_ifdetach_dispatch(struct netmsg *); 153 154 static eventhandler_tag vlan_ifdetach_cookie; 155 static struct if_clone vlan_cloner = 156 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy, 157 NVLAN, IF_MAXUNIT); 158 159 /* 160 * Program our multicast filter. What we're actually doing is 161 * programming the multicast filter of the parent. This has the 162 * side effect of causing the parent interface to receive multicast 163 * traffic that it doesn't really want, which ends up being discarded 164 * later by the upper protocol layers. Unfortunately, there's no way 165 * to avoid this: there really is only one physical interface. 166 */ 167 static int 168 vlan_setmulti(struct ifvlan *ifv, struct ifnet *ifp_p) 169 { 170 struct ifmultiaddr *ifma, *rifma = NULL; 171 struct vlan_mc_entry *mc = NULL; 172 struct sockaddr_dl sdl; 173 struct ifnet *ifp = &ifv->ifv_if; 174 175 ASSERT_NOT_SERIALIZED(ifp->if_serializer); 176 177 /* 178 * First, remove any existing filter entries. 179 */ 180 vlan_clrmulti(ifv, ifp_p); 181 182 /* 183 * Now program new ones. 184 */ 185 bzero(&sdl, sizeof(sdl)); 186 sdl.sdl_len = sizeof(sdl); 187 sdl.sdl_family = AF_LINK; 188 sdl.sdl_index = ifp_p->if_index; 189 sdl.sdl_type = IFT_ETHER; 190 sdl.sdl_alen = ETHER_ADDR_LEN; 191 192 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 193 int error; 194 195 if (ifma->ifma_addr->sa_family != AF_LINK) 196 continue; 197 198 /* Save a copy */ 199 mc = kmalloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK); 200 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 201 &mc->mc_addr, ETHER_ADDR_LEN); 202 SLIST_INSERT_HEAD(&ifv->vlan_mc_listhead, mc, mc_entries); 203 204 /* Program the parent multicast filter */ 205 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 206 LLADDR(&sdl), ETHER_ADDR_LEN); 207 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 208 if (error) 209 return error; 210 } 211 return 0; 212 } 213 214 static void 215 vlan_clrmulti(struct ifvlan *ifv, struct ifnet *ifp_p) 216 { 217 struct vlan_mc_entry *mc; 218 struct sockaddr_dl sdl; 219 220 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer); 221 222 bzero(&sdl, sizeof(sdl)); 223 sdl.sdl_len = sizeof(sdl); 224 sdl.sdl_family = AF_LINK; 225 sdl.sdl_index = ifp_p->if_index; 226 sdl.sdl_type = IFT_ETHER; 227 sdl.sdl_alen = ETHER_ADDR_LEN; 228 229 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 230 bcopy(&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 231 if_delmulti(ifp_p, (struct sockaddr *)&sdl); /* ignore error */ 232 233 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 234 kfree(mc, M_VLAN); 235 } 236 } 237 238 static int 239 vlan_modevent(module_t mod, int type, void *data) 240 { 241 switch (type) { 242 case MOD_LOAD: 243 LIST_INIT(&ifv_list); 244 vlan_input2_p = vlan_input2; 245 vlan_ifdetach_cookie = 246 EVENTHANDLER_REGISTER(ifnet_detach_event, 247 vlan_ifdetach, NULL, 248 EVENTHANDLER_PRI_ANY); 249 if_clone_attach(&vlan_cloner); 250 break; 251 252 case MOD_UNLOAD: 253 if_clone_detach(&vlan_cloner); 254 vlan_input2_p = NULL; 255 EVENTHANDLER_DEREGISTER(ifnet_detach_event, 256 vlan_ifdetach_cookie); 257 while (!LIST_EMPTY(&ifv_list)) 258 vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if); 259 break; 260 } 261 return 0; 262 } 263 264 static moduledata_t vlan_mod = { 265 "if_vlan", 266 vlan_modevent, 267 0 268 }; 269 270 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 271 272 static void 273 vlan_ifdetach_dispatch(struct netmsg *nmsg) 274 { 275 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg; 276 struct ifnet *ifp_p = vmsg->nv_ifp_p; 277 struct vlan_trunk *vlantrunks, *trunk; 278 struct vlan_entry *ifve; 279 280 vlantrunks = ifp_p->if_vlantrunks; 281 if (vlantrunks == NULL) 282 goto reply; 283 trunk = &vlantrunks[mycpuid]; 284 285 while (ifp_p->if_vlantrunks && 286 (ifve = LIST_FIRST(&trunk->vlan_list)) != NULL) 287 vlan_unconfig(ifve->ifv); 288 reply: 289 lwkt_replymsg(&nmsg->nm_lmsg, 0); 290 } 291 292 static void 293 vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 294 { 295 struct netmsg_vlan vmsg; 296 struct netmsg *nmsg; 297 298 ASSERT_NOT_SERIALIZED(ifp->if_serializer); 299 300 bzero(&vmsg, sizeof(vmsg)); 301 nmsg = &vmsg.nv_nmsg; 302 303 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_ifdetach_dispatch); 304 vmsg.nv_ifp_p = ifp; 305 306 lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0); 307 } 308 309 static int 310 vlan_clone_create(struct if_clone *ifc, int unit) 311 { 312 struct ifvlan *ifv; 313 struct ifnet *ifp; 314 int vlan_size, i; 315 316 vlan_size = sizeof(struct ifvlan) 317 + ((ncpus - 1) * sizeof(struct vlan_entry)); 318 ifv = kmalloc(vlan_size, M_VLAN, M_WAITOK | M_ZERO); 319 SLIST_INIT(&ifv->vlan_mc_listhead); 320 for (i = 0; i < ncpus; ++i) 321 ifv->ifv_entries[i].ifv = ifv; 322 323 crit_enter(); /* XXX not MP safe */ 324 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list); 325 crit_exit(); 326 327 ifp = &ifv->ifv_if; 328 ifp->if_softc = ifv; 329 if_initname(ifp, "vlan", unit); 330 /* NB: flags are not set here */ 331 ifp->if_linkmib = &ifv->ifv_mib; 332 ifp->if_linkmiblen = sizeof ifv->ifv_mib; 333 /* NB: mtu is not set here */ 334 335 ifp->if_init = vlan_init; 336 ifp->if_start = vlan_start; 337 ifp->if_ioctl = vlan_ioctl; 338 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); 339 ifq_set_ready(&ifp->if_snd); 340 ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr, NULL); 341 /* Now undo some of the damage... */ 342 ifp->if_data.ifi_type = IFT_L2VLAN; 343 ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN; 344 345 return (0); 346 } 347 348 static void 349 vlan_clone_destroy(struct ifnet *ifp) 350 { 351 struct ifvlan *ifv = ifp->if_softc; 352 353 crit_enter(); /* XXX not MP safe */ 354 LIST_REMOVE(ifv, ifv_list); 355 crit_exit(); 356 357 vlan_unconfig(ifv); 358 ether_ifdetach(ifp); 359 360 kfree(ifv, M_VLAN); 361 } 362 363 static void 364 vlan_init(void *xsc) 365 { 366 struct ifvlan *ifv = xsc; 367 struct ifnet *ifp = &ifv->ifv_if; 368 369 ASSERT_SERIALIZED(ifp->if_serializer); 370 371 if (ifv->ifv_p != NULL) 372 ifp->if_flags |= IFF_RUNNING; 373 } 374 375 static void 376 vlan_start(struct ifnet *ifp) 377 { 378 struct ifvlan *ifv = ifp->if_softc; 379 struct ifnet *ifp_p = ifv->ifv_p; 380 struct mbuf *m; 381 382 ASSERT_SERIALIZED(ifp->if_serializer); 383 384 if (ifp_p == NULL) { 385 ifq_purge(&ifp->if_snd); 386 return; 387 } 388 389 if ((ifp->if_flags & IFF_RUNNING) == 0) 390 return; 391 392 for (;;) { 393 struct netmsg_packet *nmp; 394 struct netmsg *nmsg; 395 struct lwkt_port *port; 396 397 m = ifq_dequeue(&ifp->if_snd, NULL); 398 if (m == NULL) 399 break; 400 BPF_MTAP(ifp, m); 401 402 /* 403 * Do not run parent's if_start() if the parent is not up, 404 * or parent's driver will cause a system crash. 405 */ 406 if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) != 407 (IFF_UP | IFF_RUNNING)) { 408 m_freem(m); 409 ifp->if_data.ifi_collisions++; 410 continue; 411 } 412 413 /* 414 * We need some way to tell the interface where the packet 415 * came from so that it knows how to find the VLAN tag to 416 * use, so we set the ether_vlantag in the mbuf packet header 417 * to our vlan tag. We also set the M_VLANTAG flag in the 418 * mbuf to let the parent driver know that the ether_vlantag 419 * is really valid. 420 */ 421 m->m_pkthdr.ether_vlantag = ifv->ifv_tag; 422 m->m_flags |= M_VLANTAG; 423 424 nmp = &m->m_hdr.mh_netmsg; 425 nmsg = &nmp->nm_netmsg; 426 427 netmsg_init(nmsg, &netisr_apanic_rport, 0, vlan_start_dispatch); 428 nmp->nm_packet = m; 429 nmsg->nm_lmsg.u.ms_resultp = ifp_p; 430 431 port = cpu_portfn(ifp_p->if_index % ncpus /* XXX */); 432 lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg); 433 ifp->if_opackets++; 434 } 435 } 436 437 static void 438 vlan_input2(struct mbuf *m) 439 { 440 struct ifvlan *ifv = NULL; 441 struct ifnet *rcvif, *ifp; 442 struct vlan_trunk *vlantrunks; 443 struct vlan_entry *entry; 444 445 rcvif = m->m_pkthdr.rcvif; 446 KKASSERT(m->m_flags & M_VLANTAG); 447 448 vlantrunks = rcvif->if_vlantrunks; 449 if (vlantrunks == NULL) { 450 rcvif->if_noproto++; 451 m_freem(m); 452 return; 453 } 454 455 crit_enter(); /* XXX Necessary? */ 456 LIST_FOREACH(entry, &vlantrunks[mycpuid].vlan_list, ifv_link) { 457 if (entry->ifv->ifv_tag == 458 EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag)) { 459 ifv = entry->ifv; 460 break; 461 } 462 } 463 crit_exit(); 464 465 /* 466 * Packet is discarded if: 467 * - no corresponding vlan(4) interface 468 * - vlan(4) interface has not been completely set up yet, 469 * or is being destroyed (ifv->ifv_p != rcvif) 470 * - vlan(4) interface is not brought up 471 */ 472 if (ifv == NULL || ifv->ifv_p != rcvif || 473 (ifv->ifv_if.if_flags & IFF_UP) == 0) { 474 rcvif->if_noproto++; 475 m_freem(m); 476 return; 477 } 478 ifp = &ifv->ifv_if; 479 480 /* 481 * Clear M_VLANTAG, before the packet is handed to 482 * vlan(4) interface 483 */ 484 m->m_flags &= ~M_VLANTAG; 485 486 /* Change receiving interface */ 487 m->m_pkthdr.rcvif = ifp; 488 489 /* Update statistics */ 490 ifp->if_ipackets++; 491 ifp->if_ibytes += m->m_pkthdr.len; 492 if (m->m_flags & (M_MCAST | M_BCAST)) 493 ifp->if_imcasts++; 494 495 BPF_MTAP(ifp, m); 496 497 if (ifp->if_flags & IFF_MONITOR) { 498 /* 499 * Interface marked for monitoring; discard packet. 500 */ 501 m_freem(m); 502 return; 503 } 504 ether_input_oncpu(ifp, m); 505 } 506 507 static void 508 vlan_link_dispatch(struct netmsg *nmsg) 509 { 510 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg; 511 struct ifvlan *ifv = vmsg->nv_ifv; 512 struct ifnet *ifp_p = vmsg->nv_ifp_p; 513 struct vlan_entry *entry; 514 struct vlan_trunk *vlantrunks, *trunk; 515 int cpu = mycpuid; 516 517 vlantrunks = ifp_p->if_vlantrunks; 518 KASSERT(vlantrunks != NULL, 519 ("vlan trunk has not been initialized yet\n")); 520 521 entry = &ifv->ifv_entries[cpu]; 522 trunk = &vlantrunks[cpu]; 523 524 crit_enter(); 525 LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link); 526 crit_exit(); 527 528 ifnet_forwardmsg(&nmsg->nm_lmsg, cpu + 1); 529 } 530 531 static void 532 vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p) 533 { 534 struct netmsg_vlan vmsg; 535 struct netmsg *nmsg; 536 537 /* Assert in netisr0 */ 538 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer); 539 540 if (ifp_p->if_vlantrunks == NULL) { 541 struct vlan_trunk *vlantrunks; 542 int i; 543 544 vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN, 545 M_WAITOK | M_ZERO); 546 for (i = 0; i < ncpus; ++i) 547 LIST_INIT(&vlantrunks[i].vlan_list); 548 549 ifp_p->if_vlantrunks = vlantrunks; 550 } 551 552 bzero(&vmsg, sizeof(vmsg)); 553 nmsg = &vmsg.nv_nmsg; 554 555 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_link_dispatch); 556 vmsg.nv_ifv = ifv; 557 vmsg.nv_ifp_p = ifp_p; 558 559 ifnet_domsg(&nmsg->nm_lmsg, 0); 560 } 561 562 static void 563 vlan_config_dispatch(struct netmsg *nmsg) 564 { 565 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg; 566 struct ifvlan *ifv; 567 struct ifnet *ifp_p, *ifp; 568 struct sockaddr_dl *sdl1, *sdl2; 569 int error; 570 571 /* Assert in netisr0 */ 572 573 ifp_p = ifunit(vmsg->nv_parent_name); 574 if (ifp_p == NULL) { 575 error = ENOENT; 576 goto reply; 577 } 578 579 if (ifp_p->if_data.ifi_type != IFT_ETHER) { 580 error = EPROTONOSUPPORT; 581 goto reply; 582 } 583 584 ifv = vmsg->nv_ifv; 585 ifp = &ifv->ifv_if; 586 587 if (ifv->ifv_p) { 588 error = EBUSY; 589 goto reply; 590 } 591 592 /* Link vlan into parent's vlantrunk */ 593 vlan_link(ifv, ifp_p); 594 595 lwkt_serialize_enter(ifp->if_serializer); 596 597 ifv->ifv_tag = vmsg->nv_vlantag; 598 if (ifp_p->if_capenable & IFCAP_VLAN_MTU) 599 ifp->if_mtu = ifp_p->if_mtu; 600 else 601 ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN; 602 603 /* 604 * Copy only a selected subset of flags from the parent. 605 * Other flags are none of our business. 606 */ 607 ifp->if_flags = (ifp_p->if_flags & 608 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT)); 609 610 /* 611 * Set up our ``Ethernet address'' to reflect the underlying 612 * physical interface's. 613 */ 614 sdl1 = IF_LLSOCKADDR(ifp); 615 sdl2 = IF_LLSOCKADDR(ifp_p); 616 sdl1->sdl_type = IFT_ETHER; 617 sdl1->sdl_alen = ETHER_ADDR_LEN; 618 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN); 619 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 620 621 /* 622 * Release vlan's serializer before reprogramming parent's 623 * multicast filter to avoid possible dead lock. 624 */ 625 lwkt_serialize_exit(ifp->if_serializer); 626 627 /* 628 * Configure multicast addresses that may already be 629 * joined on the vlan device. 630 */ 631 vlan_setmulti(ifv, ifp_p); 632 633 /* 634 * Connect to parent after everything have been set up, 635 * so input/output could know that vlan is ready to go 636 */ 637 ifv->ifv_p = ifp_p; 638 error = 0; 639 reply: 640 lwkt_replymsg(&nmsg->nm_lmsg, error); 641 } 642 643 static int 644 vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag) 645 { 646 struct netmsg_vlan vmsg; 647 struct netmsg *nmsg; 648 649 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer); 650 651 bzero(&vmsg, sizeof(vmsg)); 652 nmsg = &vmsg.nv_nmsg; 653 654 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_config_dispatch); 655 vmsg.nv_ifv = ifv; 656 vmsg.nv_parent_name = parent_name; 657 vmsg.nv_vlantag = vlantag; 658 659 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0); 660 } 661 662 static void 663 vlan_unlink_dispatch(struct netmsg *nmsg) 664 { 665 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg; 666 struct ifvlan *ifv = vmsg->nv_ifv; 667 struct vlan_entry *entry; 668 int cpu = mycpuid; 669 670 KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL, 671 ("vlan trunk has not been initialized yet\n")); 672 entry = &ifv->ifv_entries[cpu]; 673 674 crit_enter(); 675 LIST_REMOVE(entry, ifv_link); 676 crit_exit(); 677 678 ifnet_forwardmsg(&nmsg->nm_lmsg, cpu + 1); 679 } 680 681 static void 682 vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p) 683 { 684 struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks; 685 struct netmsg_vlan vmsg; 686 struct netmsg *nmsg; 687 688 /* Assert in netisr0 */ 689 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer); 690 691 KASSERT(ifp_p->if_vlantrunks != NULL, 692 ("vlan trunk has not been initialized yet\n")); 693 694 bzero(&vmsg, sizeof(vmsg)); 695 nmsg = &vmsg.nv_nmsg; 696 697 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_unlink_dispatch); 698 vmsg.nv_ifv = ifv; 699 vmsg.nv_ifp_p = ifp_p; 700 701 ifnet_domsg(&nmsg->nm_lmsg, 0); 702 703 crit_enter(); 704 if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) { 705 ifp_p->if_vlantrunks = NULL; 706 707 /* 708 * Make that all protocol threads see if_vlantrunks change. 709 */ 710 netmsg_service_sync(); 711 kfree(vlantrunks, M_VLAN); 712 } 713 crit_exit(); 714 } 715 716 static void 717 vlan_unconfig_dispatch(struct netmsg *nmsg) 718 { 719 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg; 720 struct sockaddr_dl *sdl; 721 struct ifvlan *ifv; 722 struct ifnet *ifp_p, *ifp; 723 int error; 724 725 /* Assert in netisr0 */ 726 727 ifv = vmsg->nv_ifv; 728 ifp = &ifv->ifv_if; 729 730 if (ifp->if_flags & IFF_UP) 731 if_down(ifp); 732 733 lwkt_serialize_enter(ifp->if_serializer); 734 735 ifp->if_flags &= ~IFF_RUNNING; 736 737 /* 738 * Save parent ifnet pointer and disconnect from parent. 739 * 740 * This is done early in this function, so input/output could 741 * know that we are disconnecting. 742 */ 743 ifp_p = ifv->ifv_p; 744 ifv->ifv_p = NULL; 745 746 /* 747 * Release vlan's serializer before reprogramming parent's 748 * multicast filter to avoid possible dead lock. 749 */ 750 lwkt_serialize_exit(ifp->if_serializer); 751 752 if (ifp_p) { 753 /* 754 * Since the interface is being unconfigured, we need to 755 * empty the list of multicast groups that we may have joined 756 * while we were alive from the parent's list. 757 */ 758 vlan_clrmulti(ifv, ifp_p); 759 } 760 761 lwkt_serialize_enter(ifp->if_serializer); 762 763 ifp->if_mtu = ETHERMTU; 764 765 /* Clear our MAC address. */ 766 sdl = IF_LLSOCKADDR(ifp); 767 sdl->sdl_type = IFT_ETHER; 768 sdl->sdl_alen = ETHER_ADDR_LEN; 769 bzero(LLADDR(sdl), ETHER_ADDR_LEN); 770 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 771 772 lwkt_serialize_exit(ifp->if_serializer); 773 774 /* Unlink vlan from parent's vlantrunk */ 775 if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL) 776 vlan_unlink(ifv, ifp_p); 777 778 error = 0; 779 lwkt_replymsg(&nmsg->nm_lmsg, error); 780 } 781 782 static int 783 vlan_unconfig(struct ifvlan *ifv) 784 { 785 struct netmsg_vlan vmsg; 786 struct netmsg *nmsg; 787 788 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer); 789 790 bzero(&vmsg, sizeof(vmsg)); 791 nmsg = &vmsg.nv_nmsg; 792 793 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_unconfig_dispatch); 794 vmsg.nv_ifv = ifv; 795 796 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0); 797 } 798 799 static int 800 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 801 { 802 struct ifvlan *ifv = ifp->if_softc; 803 struct ifreq *ifr = (struct ifreq *)data; 804 struct ifnet *ifp_p; 805 struct vlanreq vlr; 806 int error = 0; 807 808 ASSERT_SERIALIZED(ifp->if_serializer); 809 810 switch (cmd) { 811 case SIOCGIFMEDIA: 812 ifp_p = ifv->ifv_p; 813 if (ifp_p != NULL) { 814 /* 815 * Release vlan interface's serializer to void 816 * possible dead lock. 817 */ 818 lwkt_serialize_exit(ifp->if_serializer); 819 820 lwkt_serialize_enter(ifp_p->if_serializer); 821 error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr); 822 lwkt_serialize_exit(ifp_p->if_serializer); 823 824 lwkt_serialize_enter(ifp->if_serializer); 825 826 if (ifv->ifv_p == NULL && ifv->ifv_p != ifp_p) { 827 /* 828 * We are disconnected from the original 829 * parent interface or the parent interface 830 * is changed, after vlan interface's 831 * serializer is released. 832 */ 833 error = EINVAL; 834 } 835 836 /* Limit the result to the parent's current config. */ 837 if (error == 0) { 838 struct ifmediareq *ifmr; 839 840 ifmr = (struct ifmediareq *) data; 841 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 842 ifmr->ifm_count = 1; 843 error = copyout(&ifmr->ifm_current, 844 ifmr->ifm_ulist, 845 sizeof(int)); 846 } 847 } 848 } else { 849 error = EINVAL; 850 } 851 break; 852 853 case SIOCSIFMEDIA: 854 error = EINVAL; 855 break; 856 857 case SIOCSETVLAN: 858 error = copyin(ifr->ifr_data, &vlr, sizeof vlr); 859 if (error) 860 break; 861 862 lwkt_serialize_exit(ifp->if_serializer); 863 if (vlr.vlr_parent[0] == '\0') 864 error = vlan_unconfig(ifv); 865 else 866 error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag); 867 lwkt_serialize_enter(ifp->if_serializer); 868 break; 869 870 case SIOCGETVLAN: 871 bzero(&vlr, sizeof(vlr)); 872 if (ifv->ifv_p) { 873 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname, 874 sizeof(vlr.vlr_parent)); 875 vlr.vlr_tag = ifv->ifv_tag; 876 } 877 error = copyout(&vlr, ifr->ifr_data, sizeof vlr); 878 break; 879 880 case SIOCSIFFLAGS: 881 if (ifp->if_flags & IFF_UP) 882 ifp->if_init(ifp); 883 else 884 ifp->if_flags &= ~IFF_RUNNING; 885 886 /* 887 * We don't support promiscuous mode 888 * right now because it would require help from the 889 * underlying drivers, which hasn't been implemented. 890 */ 891 if (ifr->ifr_flags & IFF_PROMISC) { 892 ifp->if_flags &= ~IFF_PROMISC; 893 error = EINVAL; 894 } 895 break; 896 897 case SIOCADDMULTI: 898 case SIOCDELMULTI: 899 lwkt_serialize_exit(ifp->if_serializer); 900 error = vlan_config_multi(ifv); 901 lwkt_serialize_enter(ifp->if_serializer); 902 break; 903 904 default: 905 error = ether_ioctl(ifp, cmd, data); 906 break; 907 } 908 return error; 909 } 910 911 static void 912 vlan_multi_dispatch(struct netmsg *nmsg) 913 { 914 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)nmsg; 915 struct ifvlan *ifv = vmsg->nv_ifv; 916 int error = 0; 917 918 /* 919 * If we don't have a parent, just remember the membership for 920 * when we do. 921 */ 922 if (ifv->ifv_p != NULL) 923 error = vlan_setmulti(ifv, ifv->ifv_p); 924 lwkt_replymsg(&nmsg->nm_lmsg, error); 925 } 926 927 static int 928 vlan_config_multi(struct ifvlan *ifv) 929 { 930 struct netmsg_vlan vmsg; 931 struct netmsg *nmsg; 932 933 ASSERT_NOT_SERIALIZED(ifv->ifv_if.if_serializer); 934 935 bzero(&vmsg, sizeof(vmsg)); 936 nmsg = &vmsg.nv_nmsg; 937 938 netmsg_init(nmsg, &curthread->td_msgport, 0, vlan_multi_dispatch); 939 vmsg.nv_ifv = ifv; 940 941 return lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0); 942 } 943