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