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