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 */ 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 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 * Note about vlan's MP safe approach: 45 * 46 * - All configuration operation, e.g. config, unconfig and change flags, 47 * is serialized by netisr0; not by vlan's serializer 48 * 49 * - Parent interface's trunk and vlans are linked in the following 50 * fashion: 51 * CPU0 CPU1 CPU2 CPU3 52 * +--------------+--------+--------+--------+--------+ 53 * | parent ifnet |trunk[0]|trunk[1]|trunk[2]|trunk[3]| 54 * +--------------+--------+--------+--------+--------+ 55 * | | | | 56 * V V V V 57 * +--------------+--------+--------+--------+--------+ 58 * | vlan ifnet |entry[0]|entry[1]|entry[2]|entry[3]| 59 * +--------------+--------+--------+--------+--------+ 60 * | | | | 61 * V V V V 62 * +--------------+--------+--------+--------+--------+ 63 * | vlan ifnet |entry[0]|entry[1]|entry[2]|entry[3]| 64 * +--------------+--------+--------+--------+--------+ 65 * 66 * - Vlan is linked/unlinked onto parent interface's trunk using following 67 * way: 68 * 69 * CPU0 CPU1 CPU2 CPU3 70 * 71 * netisr0 <---------------------------------------------+ 72 * (config/unconfig) | 73 * | | 74 * | domsg | replymsg 75 * | | 76 * V fwdmsg fwdmsg fwdmsg | 77 * ifnet0 --------> ifnet1 --------> ifnet2 --------> ifnet3 78 * (link/unlink) (link/unlink) (link/unlink) (link/unlink) 79 * 80 * - Parent interface's trunk is destroyed in the following lockless way: 81 * 82 * old_trunk = ifp->if_vlantrunks; 83 * ifp->if_vlantrunks = NULL; 84 * netmsg_service_sync(); 85 * (*) 86 * free(old_trunk); 87 * 88 * Since all of the accessing of if_vlantrunks only happens in network 89 * threads (percpu netisr and ifnet threads), after netmsg_service_sync() 90 * the network threads are promised to see only NULL if_vlantrunks; we 91 * are safe to free the "to be destroyed" parent interface's trunk 92 * afterwards. 93 */ 94 95 #ifndef NVLAN 96 #include "use_vlan.h" 97 #endif 98 #include "opt_inet.h" 99 100 #include <sys/param.h> 101 #include <sys/systm.h> 102 #include <sys/kernel.h> 103 #include <sys/malloc.h> 104 #include <sys/mbuf.h> 105 #include <sys/module.h> 106 #include <sys/queue.h> 107 #include <sys/socket.h> 108 #include <sys/sockio.h> 109 #include <sys/sysctl.h> 110 #include <sys/bus.h> 111 #include <sys/thread2.h> 112 113 #include <net/bpf.h> 114 #include <net/ethernet.h> 115 #include <net/if.h> 116 #include <net/if_arp.h> 117 #include <net/if_dl.h> 118 #include <net/if_types.h> 119 #include <net/ifq_var.h> 120 #include <net/if_clone.h> 121 #include <net/netmsg2.h> 122 123 #ifdef INET 124 #include <netinet/in.h> 125 #include <netinet/if_ether.h> 126 #endif 127 128 #include <net/vlan/if_vlan_var.h> 129 #include <net/vlan/if_vlan_ether.h> 130 131 struct ifvlan; 132 133 struct vlan_mc_entry { 134 struct ether_addr mc_addr; 135 SLIST_ENTRY(vlan_mc_entry) mc_entries; 136 }; 137 138 struct vlan_entry { 139 struct ifvlan *ifv; 140 LIST_ENTRY(vlan_entry) ifv_link; 141 }; 142 143 struct ifvlan { 144 struct arpcom ifv_ac; /* make this an interface */ 145 struct ifnet *ifv_p; /* parent inteface of this vlan */ 146 int ifv_pflags; /* special flags we have set on parent */ 147 struct ifv_linkmib { 148 int ifvm_parent; 149 uint16_t ifvm_proto; /* encapsulation ethertype */ 150 uint16_t ifvm_tag; /* tag to apply on packets leaving if */ 151 } ifv_mib; 152 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead; 153 LIST_ENTRY(ifvlan) ifv_list; 154 struct vlan_entry ifv_entries[1]; 155 }; 156 #define ifv_if ifv_ac.ac_if 157 #define ifv_tag ifv_mib.ifvm_tag 158 159 struct vlan_trunk { 160 LIST_HEAD(, vlan_entry) vlan_list; 161 }; 162 163 struct netmsg_vlan { 164 struct netmsg_base base; 165 struct ifvlan *nv_ifv; 166 struct ifnet *nv_ifp_p; 167 const char *nv_parent_name; 168 uint16_t nv_vlantag; 169 }; 170 171 #define VLANNAME "vlan" 172 173 SYSCTL_DECL(_net_link); 174 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 175 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 176 177 static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface"); 178 static LIST_HEAD(, ifvlan) ifv_list; 179 180 static int vlan_clone_create(struct if_clone *, int, caddr_t); 181 static int vlan_clone_destroy(struct ifnet *); 182 static void vlan_ifdetach(void *, struct ifnet *); 183 184 static void vlan_init(void *); 185 static void vlan_start(struct ifnet *); 186 static int vlan_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *); 187 static void vlan_input(struct mbuf *); 188 189 static int vlan_setflags(struct ifvlan *, struct ifnet *, int); 190 static int vlan_setflag(struct ifvlan *, struct ifnet *, int, int, 191 int (*)(struct ifnet *, int)); 192 static int vlan_config_flags(struct ifvlan *ifv); 193 static void vlan_clrmulti(struct ifvlan *, struct ifnet *); 194 static int vlan_setmulti(struct ifvlan *, struct ifnet *); 195 static int vlan_config_multi(struct ifvlan *); 196 static int vlan_config(struct ifvlan *, const char *, uint16_t); 197 static int vlan_unconfig(struct ifvlan *); 198 static void vlan_link(struct ifvlan *, struct ifnet *); 199 static void vlan_unlink(struct ifvlan *, struct ifnet *); 200 201 static void vlan_config_dispatch(netmsg_t); 202 static void vlan_unconfig_dispatch(netmsg_t); 203 static void vlan_link_dispatch(netmsg_t); 204 static void vlan_unlink_dispatch(netmsg_t); 205 static void vlan_multi_dispatch(netmsg_t); 206 static void vlan_flags_dispatch(netmsg_t); 207 static void vlan_ifdetach_dispatch(netmsg_t); 208 209 /* Special flags we should propagate to parent */ 210 static struct { 211 int flag; 212 int (*func)(struct ifnet *, int); 213 } vlan_pflags[] = { 214 { IFF_PROMISC, ifpromisc }, 215 { IFF_ALLMULTI, if_allmulti }, 216 { 0, NULL } 217 }; 218 219 static eventhandler_tag vlan_ifdetach_cookie; 220 static struct if_clone vlan_cloner = 221 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy, 222 NVLAN, IF_MAXUNIT); 223 224 /* 225 * Handle IFF_* flags that require certain changes on the parent: 226 * if "set" is true, update parent's flags respective to our if_flags; 227 * if "set" is false, forcedly clear the flags set on parent. 228 */ 229 static int 230 vlan_setflags(struct ifvlan *ifv, struct ifnet *ifp_p, int set) 231 { 232 int error, i; 233 234 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if); 235 236 for (i = 0; vlan_pflags[i].func != NULL; i++) { 237 error = vlan_setflag(ifv, ifp_p, vlan_pflags[i].flag, 238 set, vlan_pflags[i].func); 239 if (error) 240 return error; 241 } 242 return 0; 243 } 244 245 /* Handle a reference counted flag that should be set on the parent as well */ 246 static int 247 vlan_setflag(struct ifvlan *ifv, struct ifnet *ifp_p, int flag, int set, 248 int (*func)(struct ifnet *, int)) 249 { 250 struct ifnet *ifp = &ifv->ifv_if; 251 int error, ifv_flag; 252 253 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp); 254 255 ifv_flag = set ? (ifp->if_flags & flag) : 0; 256 257 /* 258 * See if recorded parent's status is different from what 259 * we want it to be. If it is, flip it. We record parent's 260 * status in ifv_pflags so that we won't clear parent's flag 261 * we haven't set. In fact, we don't clear or set parent's 262 * flags directly, but get or release references to them. 263 * That's why we can be sure that recorded flags still are 264 * in accord with actual parent's flags. 265 */ 266 if (ifv_flag != (ifv->ifv_pflags & flag)) { 267 error = func(ifp_p, ifv_flag); 268 if (error) 269 return error; 270 ifv->ifv_pflags &= ~flag; 271 ifv->ifv_pflags |= ifv_flag; 272 } 273 return 0; 274 } 275 276 /* 277 * Program our multicast filter. What we're actually doing is 278 * programming the multicast filter of the parent. This has the 279 * side effect of causing the parent interface to receive multicast 280 * traffic that it doesn't really want, which ends up being discarded 281 * later by the upper protocol layers. Unfortunately, there's no way 282 * to avoid this: there really is only one physical interface. 283 */ 284 static int 285 vlan_setmulti(struct ifvlan *ifv, struct ifnet *ifp_p) 286 { 287 struct ifmultiaddr *ifma, *rifma = NULL; 288 struct vlan_mc_entry *mc = NULL; 289 struct sockaddr_dl sdl; 290 struct ifnet *ifp = &ifv->ifv_if; 291 292 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp); 293 294 /* 295 * First, remove any existing filter entries. 296 */ 297 vlan_clrmulti(ifv, ifp_p); 298 299 /* 300 * Now program new ones. 301 */ 302 bzero(&sdl, sizeof(sdl)); 303 sdl.sdl_len = sizeof(sdl); 304 sdl.sdl_family = AF_LINK; 305 sdl.sdl_index = ifp_p->if_index; 306 sdl.sdl_type = IFT_ETHER; 307 sdl.sdl_alen = ETHER_ADDR_LEN; 308 309 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 310 int error; 311 312 if (ifma->ifma_addr->sa_family != AF_LINK) 313 continue; 314 315 /* Save a copy */ 316 mc = kmalloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK); 317 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 318 &mc->mc_addr, ETHER_ADDR_LEN); 319 SLIST_INSERT_HEAD(&ifv->vlan_mc_listhead, mc, mc_entries); 320 321 /* Program the parent multicast filter */ 322 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 323 LLADDR(&sdl), ETHER_ADDR_LEN); 324 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 325 if (error) 326 return error; 327 } 328 return 0; 329 } 330 331 static void 332 vlan_clrmulti(struct ifvlan *ifv, struct ifnet *ifp_p) 333 { 334 struct vlan_mc_entry *mc; 335 struct sockaddr_dl sdl; 336 337 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if); 338 339 bzero(&sdl, sizeof(sdl)); 340 sdl.sdl_len = sizeof(sdl); 341 sdl.sdl_family = AF_LINK; 342 sdl.sdl_index = ifp_p->if_index; 343 sdl.sdl_type = IFT_ETHER; 344 sdl.sdl_alen = ETHER_ADDR_LEN; 345 346 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) { 347 bcopy(&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 348 if_delmulti(ifp_p, (struct sockaddr *)&sdl); /* ignore error */ 349 350 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 351 kfree(mc, M_VLAN); 352 } 353 } 354 355 static int 356 vlan_modevent(module_t mod, int type, void *data) 357 { 358 switch (type) { 359 case MOD_LOAD: 360 LIST_INIT(&ifv_list); 361 vlan_input_p = vlan_input; 362 vlan_ifdetach_cookie = 363 EVENTHANDLER_REGISTER(ifnet_detach_event, 364 vlan_ifdetach, NULL, 365 EVENTHANDLER_PRI_ANY); 366 if_clone_attach(&vlan_cloner); 367 break; 368 369 case MOD_UNLOAD: 370 if_clone_detach(&vlan_cloner); 371 372 vlan_input_p = NULL; 373 /* 374 * Make sure that all protocol threads see vlan_input_p change. 375 */ 376 netmsg_service_sync(); 377 378 EVENTHANDLER_DEREGISTER(ifnet_detach_event, 379 vlan_ifdetach_cookie); 380 while (!LIST_EMPTY(&ifv_list)) 381 vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if); 382 break; 383 } 384 return 0; 385 } 386 387 static moduledata_t vlan_mod = { 388 "if_vlan", 389 vlan_modevent, 390 0 391 }; 392 393 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 394 395 static void 396 vlan_ifdetach_dispatch(netmsg_t msg) 397 { 398 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg; 399 struct ifnet *ifp_p = vmsg->nv_ifp_p; 400 struct vlan_trunk *vlantrunks, *trunk; 401 struct vlan_entry *ifve; 402 403 vlantrunks = ifp_p->if_vlantrunks; 404 if (vlantrunks == NULL) 405 goto reply; 406 trunk = &vlantrunks[mycpuid]; 407 408 while (ifp_p->if_vlantrunks && 409 (ifve = LIST_FIRST(&trunk->vlan_list)) != NULL) 410 vlan_unconfig(ifve->ifv); 411 reply: 412 lwkt_replymsg(&vmsg->base.lmsg, 0); 413 } 414 415 static void 416 vlan_ifdetach(void *arg __unused, struct ifnet *ifp) 417 { 418 struct netmsg_vlan vmsg; 419 420 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp); 421 422 bzero(&vmsg, sizeof(vmsg)); 423 424 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport, 425 0, vlan_ifdetach_dispatch); 426 vmsg.nv_ifp_p = ifp; 427 428 lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0); 429 } 430 431 static int 432 vlan_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused) 433 { 434 struct ifvlan *ifv; 435 struct ifnet *ifp; 436 int vlan_size, i; 437 438 vlan_size = sizeof(struct ifvlan) 439 + ((ncpus - 1) * sizeof(struct vlan_entry)); 440 ifv = kmalloc(vlan_size, M_VLAN, M_WAITOK | M_ZERO); 441 SLIST_INIT(&ifv->vlan_mc_listhead); 442 for (i = 0; i < ncpus; ++i) 443 ifv->ifv_entries[i].ifv = ifv; 444 445 crit_enter(); /* XXX not MP safe */ 446 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list); 447 crit_exit(); 448 449 ifp = &ifv->ifv_if; 450 ifp->if_softc = ifv; 451 if_initname(ifp, "vlan", unit); 452 /* NB: flags are not set here */ 453 ifp->if_linkmib = &ifv->ifv_mib; 454 ifp->if_linkmiblen = sizeof ifv->ifv_mib; 455 /* NB: mtu is not set here */ 456 457 ifp->if_init = vlan_init; 458 ifp->if_start = vlan_start; 459 ifp->if_ioctl = vlan_ioctl; 460 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); 461 ifq_set_ready(&ifp->if_snd); 462 ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr, NULL); 463 /* Now undo some of the damage... */ 464 ifp->if_data.ifi_type = IFT_L2VLAN; 465 ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN; 466 467 return (0); 468 } 469 470 static int 471 vlan_clone_destroy(struct ifnet *ifp) 472 { 473 struct ifvlan *ifv = ifp->if_softc; 474 475 crit_enter(); /* XXX not MP safe */ 476 LIST_REMOVE(ifv, ifv_list); 477 crit_exit(); 478 479 vlan_unconfig(ifv); 480 ether_ifdetach(ifp); 481 482 kfree(ifv, M_VLAN); 483 484 return 0; 485 } 486 487 static void 488 vlan_init(void *xsc) 489 { 490 struct ifvlan *ifv = xsc; 491 struct ifnet *ifp = &ifv->ifv_if; 492 493 ASSERT_IFNET_SERIALIZED_ALL(ifp); 494 495 if (ifv->ifv_p != NULL) 496 ifp->if_flags |= IFF_RUNNING; 497 } 498 499 static void 500 vlan_start(struct ifnet *ifp) 501 { 502 struct ifvlan *ifv = ifp->if_softc; 503 struct ifnet *ifp_p = ifv->ifv_p; 504 struct mbuf *m; 505 506 ASSERT_IFNET_SERIALIZED_TX(ifp); 507 508 if (ifp_p == NULL) { 509 ifq_purge(&ifp->if_snd); 510 return; 511 } 512 513 if ((ifp->if_flags & IFF_RUNNING) == 0) 514 return; 515 516 for (;;) { 517 struct netmsg_packet *nmp; 518 519 m = ifq_dequeue(&ifp->if_snd, NULL); 520 if (m == NULL) 521 break; 522 BPF_MTAP(ifp, m); 523 524 /* 525 * Do not run parent's if_start() if the parent is not up, 526 * or parent's driver will cause a system crash. 527 */ 528 if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) != 529 (IFF_UP | IFF_RUNNING)) { 530 m_freem(m); 531 ifp->if_data.ifi_collisions++; 532 continue; 533 } 534 535 /* 536 * We need some way to tell the interface where the packet 537 * came from so that it knows how to find the VLAN tag to 538 * use, so we set the ether_vlantag in the mbuf packet header 539 * to our vlan tag. We also set the M_VLANTAG flag in the 540 * mbuf to let the parent driver know that the ether_vlantag 541 * is really valid. 542 */ 543 m->m_pkthdr.ether_vlantag = ifv->ifv_tag; 544 m->m_flags |= M_VLANTAG; 545 546 nmp = &m->m_hdr.mh_netmsg; 547 548 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport, 549 0, vlan_start_dispatch); 550 nmp->nm_packet = m; 551 nmp->base.lmsg.u.ms_resultp = ifp_p; 552 553 lwkt_sendmsg(netisr_portfn(ifp_p->if_start_cpuid(ifp_p)), 554 &nmp->base.lmsg); 555 ifp->if_opackets++; 556 } 557 } 558 559 static void 560 vlan_input(struct mbuf *m) 561 { 562 struct ifvlan *ifv = NULL; 563 struct ifnet *rcvif; 564 struct vlan_trunk *vlantrunks; 565 struct vlan_entry *entry; 566 567 rcvif = m->m_pkthdr.rcvif; 568 KKASSERT(m->m_flags & M_VLANTAG); 569 570 vlantrunks = rcvif->if_vlantrunks; 571 if (vlantrunks == NULL) { 572 rcvif->if_noproto++; 573 m_freem(m); 574 return; 575 } 576 577 crit_enter(); /* XXX Necessary? */ 578 LIST_FOREACH(entry, &vlantrunks[mycpuid].vlan_list, ifv_link) { 579 if (entry->ifv->ifv_tag == 580 EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag)) { 581 ifv = entry->ifv; 582 break; 583 } 584 } 585 crit_exit(); 586 587 /* 588 * Packet is discarded if: 589 * - no corresponding vlan(4) interface 590 * - vlan(4) interface has not been completely set up yet, 591 * or is being destroyed (ifv->ifv_p != rcvif) 592 */ 593 if (ifv == NULL || ifv->ifv_p != rcvif) { 594 rcvif->if_noproto++; 595 m_freem(m); 596 return; 597 } 598 599 /* 600 * Clear M_VLANTAG, before the packet is handed to 601 * vlan(4) interface 602 */ 603 m->m_flags &= ~M_VLANTAG; 604 605 ether_reinput_oncpu(&ifv->ifv_if, m, REINPUT_RUNBPF); 606 } 607 608 static void 609 vlan_link_dispatch(netmsg_t msg) 610 { 611 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg; 612 struct ifvlan *ifv = vmsg->nv_ifv; 613 struct ifnet *ifp_p = vmsg->nv_ifp_p; 614 struct vlan_entry *entry; 615 struct vlan_trunk *vlantrunks, *trunk; 616 int cpu = mycpuid; 617 618 vlantrunks = ifp_p->if_vlantrunks; 619 KASSERT(vlantrunks != NULL, 620 ("vlan trunk has not been initialized yet")); 621 622 entry = &ifv->ifv_entries[cpu]; 623 trunk = &vlantrunks[cpu]; 624 625 crit_enter(); 626 LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link); 627 crit_exit(); 628 629 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1); 630 } 631 632 static void 633 vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p) 634 { 635 struct netmsg_vlan vmsg; 636 637 /* Assert in netisr0 */ 638 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if); 639 640 if (ifp_p->if_vlantrunks == NULL) { 641 struct vlan_trunk *vlantrunks; 642 int i; 643 644 vlantrunks = kmalloc(sizeof(*vlantrunks) * ncpus, M_VLAN, 645 M_WAITOK | M_ZERO); 646 for (i = 0; i < ncpus; ++i) 647 LIST_INIT(&vlantrunks[i].vlan_list); 648 649 ifp_p->if_vlantrunks = vlantrunks; 650 } 651 652 bzero(&vmsg, sizeof(vmsg)); 653 654 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport, 655 0, vlan_link_dispatch); 656 vmsg.nv_ifv = ifv; 657 vmsg.nv_ifp_p = ifp_p; 658 659 ifnet_domsg(&vmsg.base.lmsg, 0); 660 } 661 662 static void 663 vlan_config_dispatch(netmsg_t msg) 664 { 665 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg; 666 struct ifvlan *ifv; 667 struct ifnet *ifp_p, *ifp; 668 struct sockaddr_dl *sdl1, *sdl2; 669 int error; 670 671 /* Assert in netisr0 */ 672 673 ifp_p = ifunit(vmsg->nv_parent_name); 674 if (ifp_p == NULL) { 675 error = ENOENT; 676 goto reply; 677 } 678 679 if (ifp_p->if_data.ifi_type != IFT_ETHER) { 680 error = EPROTONOSUPPORT; 681 goto reply; 682 } 683 684 ifv = vmsg->nv_ifv; 685 ifp = &ifv->ifv_if; 686 687 if (ifv->ifv_p) { 688 error = EBUSY; 689 goto reply; 690 } 691 692 /* Link vlan into parent's vlantrunk */ 693 vlan_link(ifv, ifp_p); 694 695 ifnet_serialize_all(ifp); 696 697 ifv->ifv_tag = vmsg->nv_vlantag; 698 if (ifp_p->if_capenable & IFCAP_VLAN_MTU) 699 ifp->if_mtu = ifp_p->if_mtu; 700 else 701 ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN; 702 703 /* 704 * Copy only a selected subset of flags from the parent. 705 * Other flags are none of our business. 706 */ 707 #define VLAN_INHERIT_FLAGS (IFF_BROADCAST | IFF_MULTICAST | \ 708 IFF_SIMPLEX | IFF_POINTOPOINT) 709 710 ifp->if_flags &= ~VLAN_INHERIT_FLAGS; 711 ifp->if_flags |= (ifp_p->if_flags & VLAN_INHERIT_FLAGS); 712 713 #undef VLAN_INHERIT_FLAGS 714 715 /* 716 * Set up our ``Ethernet address'' to reflect the underlying 717 * physical interface's. 718 */ 719 sdl1 = IF_LLSOCKADDR(ifp); 720 sdl2 = IF_LLSOCKADDR(ifp_p); 721 sdl1->sdl_type = IFT_ETHER; 722 sdl1->sdl_alen = ETHER_ADDR_LEN; 723 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN); 724 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 725 726 /* 727 * Release vlan's serializer before reprogramming parent's 728 * multicast filter to avoid possible dead lock. 729 */ 730 ifnet_deserialize_all(ifp); 731 732 /* 733 * Configure multicast addresses that may already be 734 * joined on the vlan device. 735 */ 736 vlan_setmulti(ifv, ifp_p); 737 738 /* 739 * Set flags on the parent, if necessary. 740 */ 741 vlan_setflags(ifv, ifp_p, 1); 742 743 /* 744 * Connect to parent after everything have been set up, 745 * so input/output could know that vlan is ready to go 746 */ 747 ifv->ifv_p = ifp_p; 748 error = 0; 749 reply: 750 lwkt_replymsg(&vmsg->base.lmsg, error); 751 } 752 753 static int 754 vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag) 755 { 756 struct netmsg_vlan vmsg; 757 758 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if); 759 760 bzero(&vmsg, sizeof(vmsg)); 761 762 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport, 763 0, vlan_config_dispatch); 764 vmsg.nv_ifv = ifv; 765 vmsg.nv_parent_name = parent_name; 766 vmsg.nv_vlantag = vlantag; 767 768 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0); 769 } 770 771 static void 772 vlan_unlink_dispatch(netmsg_t msg) 773 { 774 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg; 775 struct ifvlan *ifv = vmsg->nv_ifv; 776 struct vlan_entry *entry; 777 int cpu = mycpuid; 778 779 KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL, 780 ("vlan trunk has not been initialized yet")); 781 entry = &ifv->ifv_entries[cpu]; 782 783 crit_enter(); 784 LIST_REMOVE(entry, ifv_link); 785 crit_exit(); 786 787 ifnet_forwardmsg(&vmsg->base.lmsg, cpu + 1); 788 } 789 790 static void 791 vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p) 792 { 793 struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks; 794 struct netmsg_vlan vmsg; 795 796 /* Assert in netisr0 */ 797 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if); 798 799 KASSERT(ifp_p->if_vlantrunks != NULL, 800 ("vlan trunk has not been initialized yet")); 801 802 bzero(&vmsg, sizeof(vmsg)); 803 804 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport, 805 0, vlan_unlink_dispatch); 806 vmsg.nv_ifv = ifv; 807 vmsg.nv_ifp_p = ifp_p; 808 809 ifnet_domsg(&vmsg.base.lmsg, 0); 810 811 crit_enter(); 812 if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) { 813 ifp_p->if_vlantrunks = NULL; 814 815 /* 816 * Make sure that all protocol threads see if_vlantrunks change. 817 */ 818 netmsg_service_sync(); 819 kfree(vlantrunks, M_VLAN); 820 } 821 crit_exit(); 822 } 823 824 static void 825 vlan_unconfig_dispatch(netmsg_t msg) 826 { 827 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg; 828 struct sockaddr_dl *sdl; 829 struct ifvlan *ifv; 830 struct ifnet *ifp_p, *ifp; 831 int error; 832 833 /* Assert in netisr0 */ 834 835 ifv = vmsg->nv_ifv; 836 ifp = &ifv->ifv_if; 837 838 if (ifp->if_flags & IFF_UP) 839 if_down(ifp); 840 841 ifnet_serialize_all(ifp); 842 843 ifp->if_flags &= ~IFF_RUNNING; 844 845 /* 846 * Save parent ifnet pointer and disconnect from parent. 847 * 848 * This is done early in this function, so input/output could 849 * know that we are disconnecting. 850 */ 851 ifp_p = ifv->ifv_p; 852 ifv->ifv_p = NULL; 853 854 /* 855 * Release vlan's serializer before reprogramming parent's 856 * multicast filter to avoid possible dead lock. 857 */ 858 ifnet_deserialize_all(ifp); 859 860 if (ifp_p) { 861 /* 862 * Since the interface is being unconfigured, we need to 863 * empty the list of multicast groups that we may have joined 864 * while we were alive from the parent's list. 865 */ 866 vlan_clrmulti(ifv, ifp_p); 867 868 /* Clear parent's flags which was set by us. */ 869 vlan_setflags(ifv, ifp_p, 0); 870 } 871 872 ifnet_serialize_all(ifp); 873 874 ifp->if_mtu = ETHERMTU; 875 876 /* Clear our MAC address. */ 877 sdl = IF_LLSOCKADDR(ifp); 878 sdl->sdl_type = IFT_ETHER; 879 sdl->sdl_alen = ETHER_ADDR_LEN; 880 bzero(LLADDR(sdl), ETHER_ADDR_LEN); 881 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 882 883 ifnet_deserialize_all(ifp); 884 885 /* Unlink vlan from parent's vlantrunk */ 886 if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL) 887 vlan_unlink(ifv, ifp_p); 888 889 error = 0; 890 lwkt_replymsg(&vmsg->base.lmsg, error); 891 } 892 893 static int 894 vlan_unconfig(struct ifvlan *ifv) 895 { 896 struct netmsg_vlan vmsg; 897 898 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if); 899 900 bzero(&vmsg, sizeof(vmsg)); 901 902 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport, 903 0, vlan_unconfig_dispatch); 904 vmsg.nv_ifv = ifv; 905 906 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0); 907 } 908 909 static int 910 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 911 { 912 struct ifvlan *ifv = ifp->if_softc; 913 struct ifreq *ifr = (struct ifreq *)data; 914 struct ifnet *ifp_p; 915 struct vlanreq vlr; 916 int error = 0; 917 918 ASSERT_IFNET_SERIALIZED_ALL(ifp); 919 920 switch (cmd) { 921 case SIOCGIFMEDIA: 922 ifp_p = ifv->ifv_p; 923 if (ifp_p != NULL) { 924 /* 925 * Release vlan interface's serializer to void 926 * possible dead lock. 927 */ 928 ifnet_deserialize_all(ifp); 929 930 ifnet_serialize_all(ifp_p); 931 error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr); 932 ifnet_deserialize_all(ifp_p); 933 934 ifnet_serialize_all(ifp); 935 936 if (ifv->ifv_p == NULL || ifv->ifv_p != ifp_p) { 937 /* 938 * We are disconnected from the original 939 * parent interface or the parent interface 940 * is changed, after vlan interface's 941 * serializer is released. 942 */ 943 error = EINVAL; 944 } 945 946 /* Limit the result to the parent's current config. */ 947 if (error == 0) { 948 struct ifmediareq *ifmr; 949 950 ifmr = (struct ifmediareq *) data; 951 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 952 ifmr->ifm_count = 1; 953 error = copyout(&ifmr->ifm_current, 954 ifmr->ifm_ulist, 955 sizeof(int)); 956 } 957 } 958 } else { 959 error = EINVAL; 960 } 961 break; 962 963 case SIOCSIFMEDIA: 964 error = EINVAL; 965 break; 966 967 case SIOCSETVLAN: 968 error = copyin(ifr->ifr_data, &vlr, sizeof vlr); 969 if (error) 970 break; 971 972 ifnet_deserialize_all(ifp); 973 if (vlr.vlr_parent[0] == '\0') 974 error = vlan_unconfig(ifv); 975 else 976 error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag); 977 ifnet_serialize_all(ifp); 978 break; 979 980 case SIOCGETVLAN: 981 bzero(&vlr, sizeof(vlr)); 982 if (ifv->ifv_p) { 983 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname, 984 sizeof(vlr.vlr_parent)); 985 vlr.vlr_tag = ifv->ifv_tag; 986 } 987 error = copyout(&vlr, ifr->ifr_data, sizeof vlr); 988 break; 989 990 case SIOCSIFFLAGS: 991 if (ifp->if_flags & IFF_UP) 992 ifp->if_init(ifp); 993 else 994 ifp->if_flags &= ~IFF_RUNNING; 995 996 /* 997 * We should propagate selected flags to the parent, 998 * e.g., promiscuous mode. 999 */ 1000 ifnet_deserialize_all(ifp); 1001 error = vlan_config_flags(ifv); 1002 ifnet_serialize_all(ifp); 1003 break; 1004 1005 case SIOCADDMULTI: 1006 case SIOCDELMULTI: 1007 ifnet_deserialize_all(ifp); 1008 error = vlan_config_multi(ifv); 1009 ifnet_serialize_all(ifp); 1010 break; 1011 1012 default: 1013 error = ether_ioctl(ifp, cmd, data); 1014 break; 1015 } 1016 return error; 1017 } 1018 1019 static void 1020 vlan_multi_dispatch(netmsg_t msg) 1021 { 1022 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg; 1023 struct ifvlan *ifv = vmsg->nv_ifv; 1024 int error = 0; 1025 1026 /* 1027 * If we don't have a parent, just remember the membership for 1028 * when we do. 1029 */ 1030 if (ifv->ifv_p != NULL) 1031 error = vlan_setmulti(ifv, ifv->ifv_p); 1032 lwkt_replymsg(&vmsg->base.lmsg, error); 1033 } 1034 1035 static int 1036 vlan_config_multi(struct ifvlan *ifv) 1037 { 1038 struct netmsg_vlan vmsg; 1039 1040 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if); 1041 1042 bzero(&vmsg, sizeof(vmsg)); 1043 1044 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport, 1045 0, vlan_multi_dispatch); 1046 vmsg.nv_ifv = ifv; 1047 1048 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0); 1049 } 1050 1051 static void 1052 vlan_flags_dispatch(netmsg_t msg) 1053 { 1054 struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg; 1055 struct ifvlan *ifv = vmsg->nv_ifv; 1056 int error = 0; 1057 1058 /* 1059 * If we don't have a parent, just remember the flags for 1060 * when we do. 1061 */ 1062 if (ifv->ifv_p != NULL) 1063 error = vlan_setflags(ifv, ifv->ifv_p, 1); 1064 lwkt_replymsg(&vmsg->base.lmsg, error); 1065 } 1066 1067 static int 1068 vlan_config_flags(struct ifvlan *ifv) 1069 { 1070 struct netmsg_vlan vmsg; 1071 1072 ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if); 1073 1074 bzero(&vmsg, sizeof(vmsg)); 1075 1076 netmsg_init(&vmsg.base, NULL, &curthread->td_msgport, 1077 0, vlan_flags_dispatch); 1078 vmsg.nv_ifv = ifv; 1079 1080 return lwkt_domsg(netisr_portfn(0), &vmsg.base.lmsg, 0); 1081 } 1082