if_vlan.c revision 105602
1238384Sjkim/* 2238384Sjkim * Copyright 1998 Massachusetts Institute of Technology 3238384Sjkim * 4238384Sjkim * Permission to use, copy, modify, and distribute this software and 5238384Sjkim * its documentation for any purpose and without fee is hereby 6238384Sjkim * granted, provided that both the above copyright notice and this 7238384Sjkim * permission notice appear in all copies, that both the above 8238384Sjkim * copyright notice and this permission notice appear in all 9238384Sjkim * supporting documentation, and that the name of M.I.T. not be used 10238384Sjkim * in advertising or publicity pertaining to distribution of the 11238384Sjkim * software without specific, written prior permission. M.I.T. makes 12238384Sjkim * no representations about the suitability of this software for any 13238384Sjkim * purpose. It is provided "as is" without express or implied 14238384Sjkim * warranty. 15238384Sjkim * 16238384Sjkim * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17238384Sjkim * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18238384Sjkim * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19238384Sjkim * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20238384Sjkim * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22238384Sjkim * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23238384Sjkim * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24238384Sjkim * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25238384Sjkim * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26238384Sjkim * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27238384Sjkim * SUCH DAMAGE. 28238384Sjkim * 29238384Sjkim * $FreeBSD: head/sys/net/if_vlan.c 105602 2002-10-21 03:40:30Z brooks $ 30238384Sjkim */ 31238384Sjkim 32238384Sjkim/* 33238384Sjkim * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 34238384Sjkim * Might be extended some day to also handle IEEE 802.1p priority 35238384Sjkim * tagging. This is sort of sneaky in the implementation, since 36238384Sjkim * we need to pretend to be enough of an Ethernet implementation 37238384Sjkim * to make arp work. The way we do this is by telling everyone 38238384Sjkim * that we are an Ethernet, and then catch the packets that 39238384Sjkim * ether_output() left on our output queue when it calls 40238384Sjkim * if_start(), rewrite them for use by the real outgoing interface, 41238384Sjkim * and ask it to send them. 42238384Sjkim * 43238384Sjkim * 44238384Sjkim * XXX It's incorrect to assume that we must always kludge up 45238384Sjkim * headers on the physical device's behalf: some devices support 46238384Sjkim * VLAN tag insertion and extraction in firmware. For these cases, 47238384Sjkim * one can change the behavior of the vlan interface by setting 48238384Sjkim * the LINK0 flag on it (that is setting the vlan interface's LINK0 49238384Sjkim * flag, _not_ the parent's LINK0 flag; we try to leave the parent 50238384Sjkim * alone). If the interface has the LINK0 flag set, then it will 51238384Sjkim * not modify the ethernet header on output, because the parent 52238384Sjkim * can do that for itself. On input, the parent can call vlan_input_tag() 53238384Sjkim * directly in order to supply us with an incoming mbuf and the vlan 54238384Sjkim * tag value that goes with it. 55238384Sjkim */ 56238384Sjkim 57238384Sjkim#include "opt_inet.h" 58238384Sjkim 59238384Sjkim#include <sys/param.h> 60238384Sjkim#include <sys/kernel.h> 61238384Sjkim#include <sys/malloc.h> 62238384Sjkim#include <sys/mbuf.h> 63238384Sjkim#include <sys/module.h> 64238384Sjkim#include <sys/queue.h> 65238384Sjkim#include <sys/socket.h> 66238384Sjkim#include <sys/sockio.h> 67238384Sjkim#include <sys/sysctl.h> 68238384Sjkim#include <sys/systm.h> 69238384Sjkim 70238384Sjkim#include <net/bpf.h> 71238384Sjkim#include <net/ethernet.h> 72238384Sjkim#include <net/if.h> 73238384Sjkim#include <net/if_arp.h> 74238384Sjkim#include <net/if_dl.h> 75238384Sjkim#include <net/if_types.h> 76238384Sjkim#include <net/if_vlan_var.h> 77238384Sjkim 78238384Sjkim#ifdef INET 79238384Sjkim#include <netinet/in.h> 80238384Sjkim#include <netinet/if_ether.h> 81238384Sjkim#endif 82238384Sjkim 83238384Sjkim#define VLANNAME "vlan" 84238384Sjkim 85238384SjkimSYSCTL_DECL(_net_link); 86238384SjkimSYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 87238384SjkimSYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 88238384Sjkim 89238384Sjkimstatic MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface"); 90238384Sjkimstatic LIST_HEAD(, ifvlan) ifv_list; 91238384Sjkim 92238384Sjkimstatic int vlan_clone_create(struct if_clone *, int); 93238384Sjkimstatic void vlan_clone_destroy(struct ifnet *); 94238384Sjkimstatic void vlan_start(struct ifnet *ifp); 95238384Sjkimstatic void vlan_ifinit(void *foo); 96238384Sjkimstatic int vlan_input(struct ether_header *eh, struct mbuf *m); 97238384Sjkimstatic int vlan_input_tag(struct ether_header *eh, struct mbuf *m, 98238384Sjkim u_int16_t t); 99238384Sjkimstatic int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 100238384Sjkimstatic int vlan_setmulti(struct ifnet *ifp); 101238384Sjkimstatic int vlan_unconfig(struct ifnet *ifp); 102238384Sjkimstatic int vlan_config(struct ifvlan *ifv, struct ifnet *p); 103238384Sjkim 104238384Sjkimstruct if_clone vlan_cloner = IF_CLONE_INITIALIZER("vlan", 105238384Sjkim vlan_clone_create, vlan_clone_destroy, 0, IF_MAXUNIT); 106238384Sjkim 107238384Sjkim/* 108238384Sjkim * Program our multicast filter. What we're actually doing is 109238384Sjkim * programming the multicast filter of the parent. This has the 110238384Sjkim * side effect of causing the parent interface to receive multicast 111238384Sjkim * traffic that it doesn't really want, which ends up being discarded 112238384Sjkim * later by the upper protocol layers. Unfortunately, there's no way 113238384Sjkim * to avoid this: there really is only one physical interface. 114238384Sjkim */ 115238384Sjkimstatic int 116238384Sjkimvlan_setmulti(struct ifnet *ifp) 117238384Sjkim{ 118238384Sjkim struct ifnet *ifp_p; 119238384Sjkim struct ifmultiaddr *ifma, *rifma = NULL; 120238384Sjkim struct ifvlan *sc; 121238384Sjkim struct vlan_mc_entry *mc = NULL; 122238384Sjkim struct sockaddr_dl sdl; 123238384Sjkim int error; 124238384Sjkim 125238384Sjkim /* Find the parent. */ 126238384Sjkim sc = ifp->if_softc; 127238384Sjkim ifp_p = sc->ifv_p; 128238384Sjkim 129238384Sjkim /* 130238384Sjkim * If we don't have a parent, just remember the membership for 131238384Sjkim * when we do. 132238384Sjkim */ 133238384Sjkim if (ifp_p == NULL) 134238384Sjkim return(0); 135238384Sjkim 136238384Sjkim bzero((char *)&sdl, sizeof sdl); 137238384Sjkim sdl.sdl_len = sizeof sdl; 138238384Sjkim sdl.sdl_family = AF_LINK; 139238384Sjkim sdl.sdl_index = ifp_p->if_index; 140238384Sjkim sdl.sdl_type = IFT_ETHER; 141238384Sjkim sdl.sdl_alen = ETHER_ADDR_LEN; 142238384Sjkim 143238384Sjkim /* First, remove any existing filter entries. */ 144238384Sjkim while(SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) { 145238384Sjkim mc = SLIST_FIRST(&sc->vlan_mc_listhead); 146238384Sjkim bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 147238384Sjkim error = if_delmulti(ifp_p, (struct sockaddr *)&sdl); 148238384Sjkim if (error) 149238384Sjkim return(error); 150238384Sjkim SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 151238384Sjkim free(mc, M_VLAN); 152238384Sjkim } 153238384Sjkim 154238384Sjkim /* Now program new ones. */ 155238384Sjkim TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 156238384Sjkim if (ifma->ifma_addr->sa_family != AF_LINK) 157238384Sjkim continue; 158238384Sjkim mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK); 159238384Sjkim bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 160238384Sjkim (char *)&mc->mc_addr, ETHER_ADDR_LEN); 161238384Sjkim SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 162238384Sjkim bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 163238384Sjkim LLADDR(&sdl), ETHER_ADDR_LEN); 164238384Sjkim error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 165238384Sjkim if (error) 166238384Sjkim return(error); 167238384Sjkim } 168238384Sjkim 169238384Sjkim return(0); 170238384Sjkim} 171238384Sjkim 172238384Sjkimstatic int 173238384Sjkimvlan_modevent(module_t mod, int type, void *data) 174238384Sjkim{ 175238384Sjkim 176238384Sjkim switch (type) { 177238384Sjkim case MOD_LOAD: 178238384Sjkim LIST_INIT(&ifv_list); 179238384Sjkim vlan_input_p = vlan_input; 180238384Sjkim vlan_input_tag_p = vlan_input_tag; 181238384Sjkim if_clone_attach(&vlan_cloner); 182238384Sjkim break; 183238384Sjkim case MOD_UNLOAD: 184238384Sjkim if_clone_detach(&vlan_cloner); 185238384Sjkim vlan_input_p = NULL; 186238384Sjkim vlan_input_tag_p = NULL; 187238384Sjkim while (!LIST_EMPTY(&ifv_list)) 188238384Sjkim vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if); 189238384Sjkim break; 190238384Sjkim } 191238384Sjkim return 0; 192238384Sjkim} 193238384Sjkim 194238384Sjkimstatic moduledata_t vlan_mod = { 195238384Sjkim "if_vlan", 196238384Sjkim vlan_modevent, 197238384Sjkim 0 198238384Sjkim}; 199238384Sjkim 200238384SjkimDECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 201238384Sjkim 202238384Sjkimstatic int 203238384Sjkimvlan_clone_create(struct if_clone *ifc, int unit) 204238384Sjkim{ 205238384Sjkim struct ifvlan *ifv; 206238384Sjkim struct ifnet *ifp; 207238384Sjkim int s; 208238384Sjkim 209238384Sjkim ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 210238384Sjkim ifp = &ifv->ifv_if; 211238384Sjkim SLIST_INIT(&ifv->vlan_mc_listhead); 212238384Sjkim 213238384Sjkim s = splnet(); 214238384Sjkim LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list); 215238384Sjkim splx(s); 216238384Sjkim 217238384Sjkim ifp->if_softc = ifv; 218238384Sjkim ifp->if_name = "vlan"; 219238384Sjkim ifp->if_unit = unit; 220238384Sjkim /* NB: flags are not set here */ 221238384Sjkim ifp->if_linkmib = &ifv->ifv_mib; 222238384Sjkim ifp->if_linkmiblen = sizeof ifv->ifv_mib; 223238384Sjkim /* NB: mtu is not set here */ 224238384Sjkim 225238384Sjkim ifp->if_init = vlan_ifinit; 226238384Sjkim ifp->if_start = vlan_start; 227238384Sjkim ifp->if_ioctl = vlan_ioctl; 228238384Sjkim ifp->if_output = ether_output; 229238384Sjkim ifp->if_snd.ifq_maxlen = ifqmaxlen; 230238384Sjkim ether_ifattach(ifp, ETHER_BPF_SUPPORTED); 231238384Sjkim /* Now undo some of the damage... */ 232238384Sjkim ifp->if_baudrate = 0; 233238384Sjkim ifp->if_data.ifi_type = IFT_L2VLAN; 234238384Sjkim ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN; 235238384Sjkim 236238384Sjkim return (0); 237238384Sjkim} 238238384Sjkim 239238384Sjkimstatic void 240238384Sjkimvlan_clone_destroy(struct ifnet *ifp) 241238384Sjkim{ 242238384Sjkim struct ifvlan *ifv = ifp->if_softc; 243238384Sjkim int s; 244238384Sjkim 245238384Sjkim s = splnet(); 246238384Sjkim LIST_REMOVE(ifv, ifv_list); 247238384Sjkim vlan_unconfig(ifp); 248238384Sjkim splx(s); 249238384Sjkim 250238384Sjkim ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); 251238384Sjkim 252238384Sjkim free(ifv, M_VLAN); 253238384Sjkim} 254238384Sjkim 255238384Sjkimstatic void 256238384Sjkimvlan_ifinit(void *foo) 257238384Sjkim{ 258238384Sjkim return; 259238384Sjkim} 260238384Sjkim 261238384Sjkimstatic void 262238384Sjkimvlan_start(struct ifnet *ifp) 263238384Sjkim{ 264238384Sjkim struct ifvlan *ifv; 265238384Sjkim struct ifnet *p; 266238384Sjkim struct ether_vlan_header *evl; 267238384Sjkim struct mbuf *m; 268238384Sjkim 269238384Sjkim ifv = ifp->if_softc; 270238384Sjkim p = ifv->ifv_p; 271238384Sjkim 272238384Sjkim ifp->if_flags |= IFF_OACTIVE; 273238384Sjkim for (;;) { 274238384Sjkim IF_DEQUEUE(&ifp->if_snd, m); 275238384Sjkim if (m == 0) 276238384Sjkim break; 277238384Sjkim if (ifp->if_bpf) 278238384Sjkim bpf_mtap(ifp, m); 279238384Sjkim 280238384Sjkim /* 281238384Sjkim * Do not run parent's if_start() if the parent is not up, 282238384Sjkim * or parent's driver will cause a system crash. 283238384Sjkim */ 284238384Sjkim if ((p->if_flags & (IFF_UP | IFF_RUNNING)) != 285238384Sjkim (IFF_UP | IFF_RUNNING)) { 286238384Sjkim m_freem(m); 287238384Sjkim ifp->if_data.ifi_collisions++; 288238384Sjkim continue; 289238384Sjkim } 290238384Sjkim 291238384Sjkim /* 292238384Sjkim * If the LINK0 flag is set, it means the underlying interface 293238384Sjkim * can do VLAN tag insertion itself and doesn't require us to 294238384Sjkim * create a special header for it. In this case, we just pass 295238384Sjkim * the packet along. However, we need some way to tell the 296238384Sjkim * interface where the packet came from so that it knows how 297238384Sjkim * to find the VLAN tag to use, so we set the rcvif in the 298238384Sjkim * mbuf header to our ifnet. 299238384Sjkim * 300238384Sjkim * Note: we also set the M_PROTO1 flag in the mbuf to let 301238384Sjkim * the parent driver know that the rcvif pointer is really 302238384Sjkim * valid. We need to do this because sometimes mbufs will 303238384Sjkim * be allocated by other parts of the system that contain 304238384Sjkim * garbage in the rcvif pointer. Using the M_PROTO1 flag 305238384Sjkim * lets the driver perform a proper sanity check and avoid 306238384Sjkim * following potentially bogus rcvif pointers off into 307238384Sjkim * never-never land. 308238384Sjkim */ 309238384Sjkim if (ifp->if_flags & IFF_LINK0) { 310238384Sjkim m->m_pkthdr.rcvif = ifp; 311238384Sjkim m->m_flags |= M_PROTO1; 312238384Sjkim } else { 313238384Sjkim M_PREPEND(m, EVL_ENCAPLEN, M_DONTWAIT); 314238384Sjkim if (m == NULL) { 315238384Sjkim if_printf(ifp, "M_PREPEND failed"); 316238384Sjkim ifp->if_ierrors++; 317238384Sjkim continue; 318238384Sjkim } 319238384Sjkim /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 320238384Sjkim 321238384Sjkim m = m_pullup(m, ETHER_HDR_LEN + EVL_ENCAPLEN); 322238384Sjkim if (m == NULL) { 323238384Sjkim if_printf(ifp, "m_pullup failed"); 324238384Sjkim ifp->if_ierrors++; 325238384Sjkim continue; 326238384Sjkim } 327238384Sjkim 328238384Sjkim /* 329238384Sjkim * Transform the Ethernet header into an Ethernet header 330238384Sjkim * with 802.1Q encapsulation. 331 */ 332 bcopy(mtod(m, char *) + EVL_ENCAPLEN, mtod(m, char *), 333 sizeof(struct ether_header)); 334 evl = mtod(m, struct ether_vlan_header *); 335 evl->evl_proto = evl->evl_encap_proto; 336 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 337 evl->evl_tag = htons(ifv->ifv_tag); 338#ifdef DEBUG 339 printf("vlan_start: %*D\n", sizeof *evl, 340 (unsigned char *)evl, ":"); 341#endif 342 } 343 344 /* 345 * Send it, precisely as ether_output() would have. 346 * We are already running at splimp. 347 */ 348 if (IF_HANDOFF(&p->if_snd, m, p)) 349 ifp->if_opackets++; 350 else 351 ifp->if_oerrors++; 352 } 353 ifp->if_flags &= ~IFF_OACTIVE; 354 355 return; 356} 357 358static int 359vlan_input_tag(struct ether_header *eh, struct mbuf *m, u_int16_t t) 360{ 361 struct ifvlan *ifv; 362 363 /* 364 * Fake up a header and send the packet to the physical interface's 365 * bpf tap if active. 366 */ 367 if (m->m_pkthdr.rcvif->if_bpf != NULL) { 368 struct m_hdr mh; 369 struct ether_vlan_header evh; 370 371 bcopy(eh, &evh, 2*ETHER_ADDR_LEN); 372 evh.evl_encap_proto = htons(ETHERTYPE_VLAN); 373 evh.evl_tag = htons(t); 374 evh.evl_proto = eh->ether_type; 375 376 /* This kludge is OK; BPF treats the "mbuf" as read-only */ 377 mh.mh_next = m; 378 mh.mh_data = (char *)&evh; 379 mh.mh_len = ETHER_HDR_LEN + EVL_ENCAPLEN; 380 bpf_mtap(m->m_pkthdr.rcvif, (struct mbuf *)&mh); 381 } 382 383 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL; 384 ifv = LIST_NEXT(ifv, ifv_list)) { 385 if (m->m_pkthdr.rcvif == ifv->ifv_p 386 && ifv->ifv_tag == t) 387 break; 388 } 389 390 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 391 m->m_pkthdr.rcvif->if_noproto++; 392 m_freem(m); 393 return -1; /* So the parent can take note */ 394 } 395 396 /* 397 * Having found a valid vlan interface corresponding to 398 * the given source interface and vlan tag, run the 399 * the real packet through ether_input(). 400 */ 401 m->m_pkthdr.rcvif = &ifv->ifv_if; 402 403 ifv->ifv_if.if_ipackets++; 404 ether_input(&ifv->ifv_if, eh, m); 405 return 0; 406} 407 408static int 409vlan_input(struct ether_header *eh, struct mbuf *m) 410{ 411 struct ifvlan *ifv; 412 413 for (ifv = LIST_FIRST(&ifv_list); ifv != NULL; 414 ifv = LIST_NEXT(ifv, ifv_list)) { 415 if (m->m_pkthdr.rcvif == ifv->ifv_p 416 && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *))) 417 == ifv->ifv_tag)) 418 break; 419 } 420 421 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 422 m->m_pkthdr.rcvif->if_noproto++; 423 m_freem(m); 424 return -1; /* so ether_input can take note */ 425 } 426 427 /* 428 * Having found a valid vlan interface corresponding to 429 * the given source interface and vlan tag, remove the 430 * encapsulation, and run the real packet through 431 * ether_input() a second time (it had better be 432 * reentrant!). 433 */ 434 m->m_pkthdr.rcvif = &ifv->ifv_if; 435 eh->ether_type = mtod(m, u_int16_t *)[1]; 436 m->m_data += EVL_ENCAPLEN; 437 m->m_len -= EVL_ENCAPLEN; 438 m->m_pkthdr.len -= EVL_ENCAPLEN; 439 440 ifv->ifv_if.if_ipackets++; 441 ether_input(&ifv->ifv_if, eh, m); 442 return 0; 443} 444 445static int 446vlan_config(struct ifvlan *ifv, struct ifnet *p) 447{ 448 struct ifaddr *ifa1, *ifa2; 449 struct sockaddr_dl *sdl1, *sdl2; 450 451 if (p->if_data.ifi_type != IFT_ETHER) 452 return EPROTONOSUPPORT; 453 if (ifv->ifv_p) 454 return EBUSY; 455 ifv->ifv_p = p; 456 if (p->if_data.ifi_hdrlen == sizeof(struct ether_vlan_header)) 457 ifv->ifv_if.if_mtu = p->if_mtu; 458 else 459 ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN; 460 461 /* 462 * Copy only a selected subset of flags from the parent. 463 * Other flags are none of our business. 464 */ 465 ifv->ifv_if.if_flags = (p->if_flags & 466 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT)); 467 468 /* 469 * Set up our ``Ethernet address'' to reflect the underlying 470 * physical interface's. 471 */ 472 ifa1 = ifaddr_byindex(ifv->ifv_if.if_index); 473 ifa2 = ifaddr_byindex(p->if_index); 474 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr; 475 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr; 476 sdl1->sdl_type = IFT_ETHER; 477 sdl1->sdl_alen = ETHER_ADDR_LEN; 478 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN); 479 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 480 481 /* 482 * Configure multicast addresses that may already be 483 * joined on the vlan device. 484 */ 485 (void)vlan_setmulti(&ifv->ifv_if); 486 487 return 0; 488} 489 490static int 491vlan_unconfig(struct ifnet *ifp) 492{ 493 struct ifaddr *ifa; 494 struct sockaddr_dl *sdl; 495 struct vlan_mc_entry *mc; 496 struct ifvlan *ifv; 497 struct ifnet *p; 498 int error; 499 500 ifv = ifp->if_softc; 501 p = ifv->ifv_p; 502 503 if (p) { 504 struct sockaddr_dl sdl; 505 506 /* 507 * Since the interface is being unconfigured, we need to 508 * empty the list of multicast groups that we may have joined 509 * while we were alive from the parent's list. 510 */ 511 bzero((char *)&sdl, sizeof sdl); 512 sdl.sdl_len = sizeof sdl; 513 sdl.sdl_family = AF_LINK; 514 sdl.sdl_index = p->if_index; 515 sdl.sdl_type = IFT_ETHER; 516 sdl.sdl_alen = ETHER_ADDR_LEN; 517 518 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) { 519 mc = SLIST_FIRST(&ifv->vlan_mc_listhead); 520 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 521 error = if_delmulti(p, (struct sockaddr *)&sdl); 522 if (error) 523 return(error); 524 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 525 free(mc, M_VLAN); 526 } 527 } 528 529 /* Disconnect from parent. */ 530 ifv->ifv_p = NULL; 531 ifv->ifv_if.if_mtu = ETHERMTU; 532 533 /* Clear our MAC address. */ 534 ifa = ifaddr_byindex(ifv->ifv_if.if_index); 535 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 536 sdl->sdl_type = IFT_ETHER; 537 sdl->sdl_alen = ETHER_ADDR_LEN; 538 bzero(LLADDR(sdl), ETHER_ADDR_LEN); 539 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 540 541 return 0; 542} 543 544static int 545vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 546{ 547 struct ifaddr *ifa; 548 struct ifnet *p; 549 struct ifreq *ifr; 550 struct ifvlan *ifv; 551 struct vlanreq vlr; 552 int error = 0; 553 554 ifr = (struct ifreq *)data; 555 ifa = (struct ifaddr *)data; 556 ifv = ifp->if_softc; 557 558 switch (cmd) { 559 case SIOCSIFADDR: 560 ifp->if_flags |= IFF_UP; 561 562 switch (ifa->ifa_addr->sa_family) { 563#ifdef INET 564 case AF_INET: 565 arp_ifinit(&ifv->ifv_if, ifa); 566 break; 567#endif 568 default: 569 break; 570 } 571 break; 572 573 case SIOCGIFADDR: 574 { 575 struct sockaddr *sa; 576 577 sa = (struct sockaddr *) &ifr->ifr_data; 578 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, 579 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 580 } 581 break; 582 583 case SIOCSIFMTU: 584 /* 585 * Set the interface MTU. 586 * This is bogus. The underlying interface might support 587 * jumbo frames. 588 */ 589 if (ifr->ifr_mtu > ETHERMTU) { 590 error = EINVAL; 591 } else { 592 ifp->if_mtu = ifr->ifr_mtu; 593 } 594 break; 595 596 case SIOCSETVLAN: 597 error = copyin(ifr->ifr_data, &vlr, sizeof vlr); 598 if (error) 599 break; 600 if (vlr.vlr_parent[0] == '\0') { 601 vlan_unconfig(ifp); 602 if (ifp->if_flags & IFF_UP) { 603 int s = splimp(); 604 if_down(ifp); 605 splx(s); 606 } 607 ifp->if_flags &= ~IFF_RUNNING; 608 break; 609 } 610 p = ifunit(vlr.vlr_parent); 611 if (p == 0) { 612 error = ENOENT; 613 break; 614 } 615 error = vlan_config(ifv, p); 616 if (error) 617 break; 618 ifv->ifv_tag = vlr.vlr_tag; 619 ifp->if_flags |= IFF_RUNNING; 620 break; 621 622 case SIOCGETVLAN: 623 bzero(&vlr, sizeof vlr); 624 if (ifv->ifv_p) { 625 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), 626 "%s%d", ifv->ifv_p->if_name, ifv->ifv_p->if_unit); 627 vlr.vlr_tag = ifv->ifv_tag; 628 } 629 error = copyout(&vlr, ifr->ifr_data, sizeof vlr); 630 break; 631 632 case SIOCSIFFLAGS: 633 /* 634 * We don't support promiscuous mode 635 * right now because it would require help from the 636 * underlying drivers, which hasn't been implemented. 637 */ 638 if (ifr->ifr_flags & (IFF_PROMISC)) { 639 ifp->if_flags &= ~(IFF_PROMISC); 640 error = EINVAL; 641 } 642 break; 643 case SIOCADDMULTI: 644 case SIOCDELMULTI: 645 error = vlan_setmulti(ifp); 646 break; 647 default: 648 error = EINVAL; 649 } 650 return error; 651} 652