if_vlan.c revision 128618
1255570Strasz/* 2255570Strasz * Copyright 1998 Massachusetts Institute of Technology 3255570Strasz * 4255570Strasz * Permission to use, copy, modify, and distribute this software and 5255570Strasz * its documentation for any purpose and without fee is hereby 6255570Strasz * granted, provided that both the above copyright notice and this 7255570Strasz * permission notice appear in all copies, that both the above 8255570Strasz * copyright notice and this permission notice appear in all 9255570Strasz * supporting documentation, and that the name of M.I.T. not be used 10255570Strasz * in advertising or publicity pertaining to distribution of the 11255570Strasz * software without specific, written prior permission. M.I.T. makes 12255570Strasz * no representations about the suitability of this software for any 13255570Strasz * purpose. It is provided "as is" without express or implied 14255570Strasz * warranty. 15255570Strasz * 16255570Strasz * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17255570Strasz * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18255570Strasz * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19255570Strasz * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20255570Strasz * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21255570Strasz * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22255570Strasz * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23255570Strasz * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24255570Strasz * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25255570Strasz * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26255570Strasz * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27255570Strasz * SUCH DAMAGE. 28255570Strasz * 29255570Strasz * $FreeBSD: head/sys/net/if_vlan.c 128618 2004-04-24 22:24:48Z luigi $ 30255570Strasz */ 31270279Strasz 32270279Strasz/* 33270279Strasz * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. 34255570Strasz * Might be extended some day to also handle IEEE 802.1p priority 35255570Strasz * tagging. This is sort of sneaky in the implementation, since 36255570Strasz * we need to pretend to be enough of an Ethernet implementation 37255570Strasz * to make arp work. The way we do this is by telling everyone 38255570Strasz * that we are an Ethernet, and then catch the packets that 39255570Strasz * ether_output() left on our output queue when it calls 40267606Smav * if_start(), rewrite them for use by the real outgoing interface, 41267606Smav * and ask it to send them. 42255570Strasz */ 43255570Strasz 44255570Strasz#include "opt_inet.h" 45255570Strasz 46255570Strasz#include <sys/param.h> 47255570Strasz#include <sys/kernel.h> 48255570Strasz#include <sys/malloc.h> 49255570Strasz#include <sys/mbuf.h> 50255570Strasz#include <sys/module.h> 51255570Strasz#include <sys/queue.h> 52255570Strasz#include <sys/socket.h> 53255570Strasz#include <sys/sockio.h> 54255570Strasz#include <sys/sysctl.h> 55255570Strasz#include <sys/systm.h> 56255570Strasz 57255570Strasz#include <net/bpf.h> 58255570Strasz#include <net/ethernet.h> 59255570Strasz#include <net/if.h> 60255570Strasz#include <net/if_arp.h> 61255570Strasz#include <net/if_dl.h> 62255570Strasz#include <net/if_types.h> 63255570Strasz#include <net/if_vlan_var.h> 64255570Strasz 65255570Strasz#ifdef INET 66255570Strasz#include <netinet/in.h> 67255570Strasz#include <netinet/if_ether.h> 68275864Smav#endif 69255570Strasz 70275864Smav#define VLANNAME "vlan" 71255570Strasz 72255570Straszstruct vlan_mc_entry { 73255570Strasz struct ether_addr mc_addr; 74275864Smav SLIST_ENTRY(vlan_mc_entry) mc_entries; 75255570Strasz}; 76255570Strasz 77255570Straszstruct ifvlan { 78279589Smav struct arpcom ifv_ac; /* make this an interface */ 79279589Smav struct ifnet *ifv_p; /* parent inteface of this vlan */ 80255570Strasz struct ifv_linkmib { 81255570Strasz int ifvm_parent; 82255570Strasz int ifvm_encaplen; /* encapsulation length */ 83255570Strasz int ifvm_mtufudge; /* MTU fudged by this much */ 84255570Strasz int ifvm_mintu; /* min transmission unit */ 85255570Strasz u_int16_t ifvm_proto; /* encapsulation ethertype */ 86255570Strasz u_int16_t ifvm_tag; /* tag to apply on packets leaving if */ 87255570Strasz } ifv_mib; 88255570Strasz SLIST_HEAD(__vlan_mchead, vlan_mc_entry) vlan_mc_listhead; 89255570Strasz LIST_ENTRY(ifvlan) ifv_list; 90255570Strasz int ifv_flags; 91255570Strasz}; 92255570Strasz#define ifv_if ifv_ac.ac_if 93255570Strasz#define ifv_tag ifv_mib.ifvm_tag 94255570Strasz#define ifv_encaplen ifv_mib.ifvm_encaplen 95255570Strasz#define ifv_mtufudge ifv_mib.ifvm_mtufudge 96255570Strasz#define ifv_mintu ifv_mib.ifvm_mintu 97255570Strasz 98255570Strasz#define IFVF_PROMISC 0x01 /* promiscuous mode enabled */ 99255570Strasz 100255570StraszSYSCTL_DECL(_net_link); 101255570StraszSYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 102255570StraszSYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 103255570Strasz 104255570Straszstatic MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface"); 105255570Straszstatic LIST_HEAD(, ifvlan) ifv_list; 106255570Strasz 107255570Strasz/* 108255570Strasz * Locking: one lock is used to guard both the ifv_list and modification 109255570Strasz * to vlan data structures. We are rather conservative here; probably 110255570Strasz * more than necessary. 111255570Strasz */ 112255570Straszstatic struct mtx ifv_mtx; 113255570Strasz#define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, VLANNAME, NULL, MTX_DEF) 114255570Strasz#define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx) 115255570Strasz#define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED) 116255570Strasz#define VLAN_LOCK() mtx_lock(&ifv_mtx) 117255570Strasz#define VLAN_UNLOCK() mtx_unlock(&ifv_mtx) 118255570Strasz 119255570Straszstatic int vlan_clone_create(struct if_clone *, int); 120255570Straszstatic void vlan_clone_destroy(struct ifnet *); 121255570Straszstatic void vlan_start(struct ifnet *ifp); 122255570Straszstatic void vlan_ifinit(void *foo); 123255570Straszstatic void vlan_input(struct ifnet *ifp, struct mbuf *m); 124255570Straszstatic int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 125275864Smavstatic int vlan_setmulti(struct ifnet *ifp); 126255570Straszstatic int vlan_unconfig(struct ifnet *ifp); 127275864Smavstatic int vlan_config(struct ifvlan *ifv, struct ifnet *p); 128255570Strasz 129255570Straszstruct if_clone vlan_cloner = IF_CLONE_INITIALIZER(VLANNAME, 130255570Strasz vlan_clone_create, vlan_clone_destroy, 0, IF_MAXUNIT); 131255570Strasz 132275864Smav/* 133255570Strasz * Program our multicast filter. What we're actually doing is 134255570Strasz * programming the multicast filter of the parent. This has the 135255570Strasz * side effect of causing the parent interface to receive multicast 136279589Smav * traffic that it doesn't really want, which ends up being discarded 137279589Smav * later by the upper protocol layers. Unfortunately, there's no way 138255570Strasz * to avoid this: there really is only one physical interface. 139255570Strasz */ 140255570Straszstatic int 141255570Straszvlan_setmulti(struct ifnet *ifp) 142255570Strasz{ 143255570Strasz struct ifnet *ifp_p; 144255570Strasz struct ifmultiaddr *ifma, *rifma = NULL; 145255570Strasz struct ifvlan *sc; 146255570Strasz struct vlan_mc_entry *mc = NULL; 147255570Strasz struct sockaddr_dl sdl; 148255570Strasz int error; 149255570Strasz 150255570Strasz /* Find the parent. */ 151255570Strasz sc = ifp->if_softc; 152255570Strasz ifp_p = sc->ifv_p; 153255570Strasz 154255570Strasz /* 155255570Strasz * If we don't have a parent, just remember the membership for 156255570Strasz * when we do. 157255570Strasz */ 158255570Strasz if (ifp_p == NULL) 159255570Strasz return(0); 160255570Strasz 161255570Strasz bzero((char *)&sdl, sizeof sdl); 162255570Strasz sdl.sdl_len = sizeof sdl; 163255570Strasz sdl.sdl_family = AF_LINK; 164255570Strasz sdl.sdl_index = ifp_p->if_index; 165255570Strasz sdl.sdl_type = IFT_ETHER; 166267606Smav sdl.sdl_alen = ETHER_ADDR_LEN; 167273543Strasz 168267606Smav /* First, remove any existing filter entries. */ 169278322Smav while(SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) { 170267606Smav mc = SLIST_FIRST(&sc->vlan_mc_listhead); 171267606Smav bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 172267606Smav error = if_delmulti(ifp_p, (struct sockaddr *)&sdl); 173267606Smav if (error) 174267606Smav return(error); 175267606Smav SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 176267606Smav free(mc, M_VLAN); 177278322Smav } 178278322Smav 179278322Smav /* Now program new ones. */ 180278322Smav TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 181267606Smav if (ifma->ifma_addr->sa_family != AF_LINK) 182267606Smav continue; 183267606Smav mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK); 184267606Smav bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 185267606Smav (char *)&mc->mc_addr, ETHER_ADDR_LEN); 186267606Smav SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 187267606Smav bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 188267606Smav LLADDR(&sdl), ETHER_ADDR_LEN); 189267606Smav error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 190267606Smav if (error) 191267606Smav return(error); 192267606Smav } 193267606Smav 194278322Smav return(0); 195267606Smav} 196267606Smav 197267606Smav/* 198267606Smav * VLAN support can be loaded as a module. The only place in the 199267606Smav * system that's intimately aware of this is ether_input. We hook 200278322Smav * into this code through vlan_input_p which is defined there and 201267606Smav * set here. Noone else in the system should be aware of this so 202267606Smav * we use an explicit reference here. 203267606Smav * 204267606Smav * NB: Noone should ever need to check if vlan_input_p is null or 205267606Smav * not. This is because interfaces have a count of the number 206267606Smav * of active vlans (if_nvlans) and this should never be bumped 207267606Smav * except by vlan_config--which is in this module so therefore 208267606Smav * the module must be loaded and vlan_input_p must be non-NULL. 209278322Smav */ 210267606Smavextern void (*vlan_input_p)(struct ifnet *, struct mbuf *); 211267606Smav 212267606Smavstatic int 213273813Straszvlan_modevent(module_t mod, int type, void *data) 214273813Strasz{ 215278322Smav 216273813Strasz switch (type) { 217273813Strasz case MOD_LOAD: 218273813Strasz LIST_INIT(&ifv_list); 219278322Smav VLAN_LOCK_INIT(); 220273813Strasz vlan_input_p = vlan_input; 221273813Strasz if_clone_attach(&vlan_cloner); 222273813Strasz break; 223278322Smav case MOD_UNLOAD: 224278322Smav if_clone_detach(&vlan_cloner); 225278322Smav vlan_input_p = NULL; 226278322Smav while (!LIST_EMPTY(&ifv_list)) 227273813Strasz vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if); 228273813Strasz VLAN_LOCK_DESTROY(); 229273813Strasz break; 230273813Strasz } 231273813Strasz return 0; 232273813Strasz} 233273813Strasz 234273813Straszstatic moduledata_t vlan_mod = { 235273813Strasz "if_vlan", 236273813Strasz vlan_modevent, 237273813Strasz 0 238273813Strasz}; 239273813Strasz 240273813StraszDECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 241273813Strasz 242273813Straszstatic int 243273813Straszvlan_clone_create(struct if_clone *ifc, int unit) 244273813Strasz{ 245273813Strasz struct ifvlan *ifv; 246273813Strasz struct ifnet *ifp; 247273813Strasz 248273813Strasz ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO); 249273813Strasz ifp = &ifv->ifv_if; 250273813Strasz SLIST_INIT(&ifv->vlan_mc_listhead); 251273813Strasz 252273813Strasz ifp->if_softc = ifv; 253273813Strasz if_initname(ifp, ifc->ifc_name, unit); 254273813Strasz /* NB: flags are not set here */ 255273813Strasz ifp->if_linkmib = &ifv->ifv_mib; 256273813Strasz ifp->if_linkmiblen = sizeof ifv->ifv_mib; 257273813Strasz /* NB: mtu is not set here */ 258273813Strasz 259273813Strasz ifp->if_init = vlan_ifinit; 260273813Strasz ifp->if_start = vlan_start; 261273813Strasz ifp->if_ioctl = vlan_ioctl; 262273813Strasz ifp->if_snd.ifq_maxlen = ifqmaxlen; 263273813Strasz ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr); 264273813Strasz /* Now undo some of the damage... */ 265273813Strasz ifp->if_baudrate = 0; 266273813Strasz ifp->if_type = IFT_L2VLAN; 267273813Strasz ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN; 268273813Strasz 269273813Strasz VLAN_LOCK(); 270273813Strasz LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list); 271273813Strasz VLAN_UNLOCK(); 272273813Strasz 273273813Strasz return (0); 274273813Strasz} 275273813Strasz 276255570Straszstatic void 277255570Straszvlan_clone_destroy(struct ifnet *ifp) 278255570Strasz{ 279255570Strasz struct ifvlan *ifv = ifp->if_softc; 280255570Strasz 281278322Smav VLAN_LOCK(); 282273543Strasz LIST_REMOVE(ifv, ifv_list); 283255570Strasz vlan_unconfig(ifp); 284255570Strasz VLAN_UNLOCK(); 285273543Strasz 286273543Strasz ether_ifdetach(ifp); 287255570Strasz 288255570Strasz free(ifv, M_VLAN); 289255570Strasz} 290255570Strasz 291255570Straszstatic void 292255570Straszvlan_ifinit(void *foo) 293255570Strasz{ 294255570Strasz return; 295255570Strasz} 296255570Strasz 297255570Straszstatic void 298255570Straszvlan_start(struct ifnet *ifp) 299255570Strasz{ 300278322Smav struct ifvlan *ifv; 301278322Smav struct ifnet *p; 302273813Strasz struct ether_vlan_header *evl; 303273813Strasz struct mbuf *m; 304273813Strasz 305278322Smav ifv = ifp->if_softc; 306255570Strasz p = ifv->ifv_p; 307255570Strasz 308278322Smav ifp->if_flags |= IFF_OACTIVE; 309278322Smav for (;;) { 310255570Strasz IF_DEQUEUE(&ifp->if_snd, m); 311255570Strasz if (m == 0) 312273813Strasz break; 313278322Smav BPF_MTAP(ifp, m); 314273813Strasz 315273813Strasz /* 316278322Smav * Do not run parent's if_start() if the parent is not up, 317273813Strasz * or parent's driver will cause a system crash. 318273813Strasz */ 319255570Strasz if ((p->if_flags & (IFF_UP | IFF_RUNNING)) != 320255570Strasz (IFF_UP | IFF_RUNNING)) { 321255570Strasz m_freem(m); 322255570Strasz ifp->if_collisions++; 323255570Strasz continue; 324255570Strasz } 325255570Strasz 326255570Strasz /* 327255570Strasz * If underlying interface can do VLAN tag insertion itself, 328255570Strasz * just pass the packet along. However, we need some way to 329255570Strasz * tell the interface where the packet came from so that it 330255570Strasz * knows how to find the VLAN tag to use, so we attach a 331255570Strasz * packet tag that holds it. 332255570Strasz */ 333255570Strasz if (p->if_capabilities & IFCAP_VLAN_HWTAGGING) { 334255570Strasz struct m_tag *mtag = m_tag_alloc(MTAG_VLAN, 335255570Strasz MTAG_VLAN_TAG, 336255570Strasz sizeof (u_int), 337255570Strasz M_NOWAIT); 338 if (mtag == NULL) { 339 ifp->if_oerrors++; 340 m_freem(m); 341 continue; 342 } 343 *(u_int*)(mtag+1) = ifv->ifv_tag; 344 m_tag_prepend(m, mtag); 345 } else { 346 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT); 347 if (m == NULL) { 348 if_printf(ifp, "unable to prepend VLAN header"); 349 ifp->if_oerrors++; 350 continue; 351 } 352 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 353 354 if (m->m_len < sizeof(*evl)) { 355 m = m_pullup(m, sizeof(*evl)); 356 if (m == NULL) { 357 if_printf(ifp, 358 "cannot pullup VLAN header"); 359 ifp->if_oerrors++; 360 continue; 361 } 362 } 363 364 /* 365 * Transform the Ethernet header into an Ethernet header 366 * with 802.1Q encapsulation. 367 */ 368 bcopy(mtod(m, char *) + ifv->ifv_encaplen, 369 mtod(m, char *), ETHER_HDR_LEN); 370 evl = mtod(m, struct ether_vlan_header *); 371 evl->evl_proto = evl->evl_encap_proto; 372 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 373 evl->evl_tag = htons(ifv->ifv_tag); 374#ifdef DEBUG 375 printf("vlan_start: %*D\n", (int)sizeof *evl, 376 (unsigned char *)evl, ":"); 377#endif 378 } 379 380 /* 381 * Send it, precisely as ether_output() would have. 382 * We are already running at splimp. 383 */ 384 if (IF_HANDOFF(&p->if_snd, m, p)) 385 ifp->if_opackets++; 386 else 387 ifp->if_oerrors++; 388 } 389 ifp->if_flags &= ~IFF_OACTIVE; 390 391 return; 392} 393 394static void 395vlan_input(struct ifnet *ifp, struct mbuf *m) 396{ 397 struct ether_vlan_header *evl; 398 struct ifvlan *ifv; 399 struct m_tag *mtag; 400 u_int tag; 401 402 mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL); 403 if (mtag != NULL) { 404 /* 405 * Packet is tagged, m contains a normal 406 * Ethernet frame; the tag is stored out-of-band. 407 */ 408 tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)); 409 m_tag_delete(m, mtag); 410 } else { 411 switch (ifp->if_type) { 412 case IFT_ETHER: 413 if (m->m_len < sizeof (*evl) && 414 (m = m_pullup(m, sizeof (*evl))) == NULL) { 415 if_printf(ifp, "cannot pullup VLAN header\n"); 416 return; 417 } 418 evl = mtod(m, struct ether_vlan_header *); 419 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN, 420 ("vlan_input: bad encapsulated protocols (%u)", 421 ntohs(evl->evl_encap_proto))); 422 423 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 424 425 /* 426 * Restore the original ethertype. We'll remove 427 * the encapsulation after we've found the vlan 428 * interface corresponding to the tag. 429 */ 430 evl->evl_encap_proto = evl->evl_proto; 431 break; 432 default: 433 tag = (u_int) -1; 434#ifdef DIAGNOSTIC 435 panic("vlan_input: unsupported if type %u", ifp->if_type); 436#endif 437 break; 438 } 439 } 440 441 VLAN_LOCK(); 442 LIST_FOREACH(ifv, &ifv_list, ifv_list) 443 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag) 444 break; 445 446 if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 447 VLAN_UNLOCK(); 448 m_freem(m); 449 ifp->if_noproto++; 450 return; 451 } 452 VLAN_UNLOCK(); /* XXX extend below? */ 453 454 if (mtag == NULL) { 455 /* 456 * Packet had an in-line encapsulation header; 457 * remove it. The original header has already 458 * been fixed up above. 459 */ 460 bcopy(mtod(m, caddr_t), 461 mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN, 462 ETHER_HDR_LEN); 463 m_adj(m, ETHER_VLAN_ENCAP_LEN); 464 } 465 466 m->m_pkthdr.rcvif = &ifv->ifv_if; 467 ifv->ifv_if.if_ipackets++; 468 469 /* Pass it back through the parent's input routine. */ 470 (*ifp->if_input)(&ifv->ifv_if, m); 471} 472 473static int 474vlan_config(struct ifvlan *ifv, struct ifnet *p) 475{ 476 struct ifaddr *ifa1, *ifa2; 477 struct sockaddr_dl *sdl1, *sdl2; 478 479 VLAN_LOCK_ASSERT(); 480 481 if (p->if_data.ifi_type != IFT_ETHER) 482 return EPROTONOSUPPORT; 483 if (ifv->ifv_p) 484 return EBUSY; 485 486 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN; 487 ifv->ifv_mintu = ETHERMIN; 488 ifv->ifv_flags = 0; 489 490 /* 491 * If the parent supports the VLAN_MTU capability, 492 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames, 493 * enable it. 494 */ 495 p->if_nvlans++; 496 if (p->if_nvlans == 1 && (p->if_capabilities & IFCAP_VLAN_MTU) != 0) { 497 /* 498 * Enable Tx/Rx of VLAN-sized frames. 499 */ 500 p->if_capenable |= IFCAP_VLAN_MTU; 501 if (p->if_flags & IFF_UP) { 502 struct ifreq ifr; 503 int error; 504 505 ifr.ifr_flags = p->if_flags; 506 error = (*p->if_ioctl)(p, SIOCSIFFLAGS, 507 (caddr_t) &ifr); 508 if (error) { 509 p->if_nvlans--; 510 if (p->if_nvlans == 0) 511 p->if_capenable &= ~IFCAP_VLAN_MTU; 512 return (error); 513 } 514 } 515 ifv->ifv_mtufudge = 0; 516 } else if ((p->if_capabilities & IFCAP_VLAN_MTU) == 0) { 517 /* 518 * Fudge the MTU by the encapsulation size. This 519 * makes us incompatible with strictly compliant 520 * 802.1Q implementations, but allows us to use 521 * the feature with other NetBSD implementations, 522 * which might still be useful. 523 */ 524 ifv->ifv_mtufudge = ifv->ifv_encaplen; 525 } 526 527 ifv->ifv_p = p; 528 ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge; 529 /* 530 * Copy only a selected subset of flags from the parent. 531 * Other flags are none of our business. 532 */ 533 ifv->ifv_if.if_flags = (p->if_flags & 534 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT)); 535 536 /* 537 * If the parent interface can do hardware-assisted 538 * VLAN encapsulation, then propagate its hardware- 539 * assisted checksumming flags. 540 */ 541 if (p->if_capabilities & IFCAP_VLAN_HWTAGGING) 542 ifv->ifv_if.if_capabilities |= p->if_capabilities & IFCAP_HWCSUM; 543 544 /* 545 * Set up our ``Ethernet address'' to reflect the underlying 546 * physical interface's. 547 */ 548 ifa1 = ifaddr_byindex(ifv->ifv_if.if_index); 549 ifa2 = ifaddr_byindex(p->if_index); 550 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr; 551 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr; 552 sdl1->sdl_type = IFT_ETHER; 553 sdl1->sdl_alen = ETHER_ADDR_LEN; 554 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN); 555 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 556 557 /* 558 * Configure multicast addresses that may already be 559 * joined on the vlan device. 560 */ 561 (void)vlan_setmulti(&ifv->ifv_if); 562 563 return 0; 564} 565 566static int 567vlan_unconfig(struct ifnet *ifp) 568{ 569 struct ifaddr *ifa; 570 struct sockaddr_dl *sdl; 571 struct vlan_mc_entry *mc; 572 struct ifvlan *ifv; 573 struct ifnet *p; 574 int error; 575 576 VLAN_LOCK_ASSERT(); 577 578 ifv = ifp->if_softc; 579 p = ifv->ifv_p; 580 581 if (p) { 582 struct sockaddr_dl sdl; 583 584 /* 585 * Since the interface is being unconfigured, we need to 586 * empty the list of multicast groups that we may have joined 587 * while we were alive from the parent's list. 588 */ 589 bzero((char *)&sdl, sizeof sdl); 590 sdl.sdl_len = sizeof sdl; 591 sdl.sdl_family = AF_LINK; 592 sdl.sdl_index = p->if_index; 593 sdl.sdl_type = IFT_ETHER; 594 sdl.sdl_alen = ETHER_ADDR_LEN; 595 596 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) { 597 mc = SLIST_FIRST(&ifv->vlan_mc_listhead); 598 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 599 error = if_delmulti(p, (struct sockaddr *)&sdl); 600 if (error) 601 return(error); 602 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 603 free(mc, M_VLAN); 604 } 605 606 p->if_nvlans--; 607 if (p->if_nvlans == 0) { 608 /* 609 * Disable Tx/Rx of VLAN-sized frames. 610 */ 611 p->if_capenable &= ~IFCAP_VLAN_MTU; 612 if (p->if_flags & IFF_UP) { 613 struct ifreq ifr; 614 615 ifr.ifr_flags = p->if_flags; 616 (*p->if_ioctl)(p, SIOCSIFFLAGS, (caddr_t) &ifr); 617 } 618 } 619 } 620 621 /* Disconnect from parent. */ 622 ifv->ifv_p = NULL; 623 ifv->ifv_if.if_mtu = ETHERMTU; /* XXX why not 0? */ 624 ifv->ifv_flags = 0; 625 626 /* Clear our MAC address. */ 627 ifa = ifaddr_byindex(ifv->ifv_if.if_index); 628 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 629 sdl->sdl_type = IFT_ETHER; 630 sdl->sdl_alen = ETHER_ADDR_LEN; 631 bzero(LLADDR(sdl), ETHER_ADDR_LEN); 632 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 633 634 return 0; 635} 636 637static int 638vlan_set_promisc(struct ifnet *ifp) 639{ 640 struct ifvlan *ifv = ifp->if_softc; 641 int error = 0; 642 643 if ((ifp->if_flags & IFF_PROMISC) != 0) { 644 if ((ifv->ifv_flags & IFVF_PROMISC) == 0) { 645 error = ifpromisc(ifv->ifv_p, 1); 646 if (error == 0) 647 ifv->ifv_flags |= IFVF_PROMISC; 648 } 649 } else { 650 if ((ifv->ifv_flags & IFVF_PROMISC) != 0) { 651 error = ifpromisc(ifv->ifv_p, 0); 652 if (error == 0) 653 ifv->ifv_flags &= ~IFVF_PROMISC; 654 } 655 } 656 657 return (error); 658} 659 660static int 661vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 662{ 663 struct ifaddr *ifa; 664 struct ifnet *p; 665 struct ifreq *ifr; 666 struct ifvlan *ifv; 667 struct vlanreq vlr; 668 int error = 0; 669 670 ifr = (struct ifreq *)data; 671 ifa = (struct ifaddr *)data; 672 ifv = ifp->if_softc; 673 674 switch (cmd) { 675 case SIOCSIFADDR: 676 ifp->if_flags |= IFF_UP; 677 678 switch (ifa->ifa_addr->sa_family) { 679#ifdef INET 680 case AF_INET: 681 arp_ifinit(&ifv->ifv_if, ifa); 682 break; 683#endif 684 default: 685 break; 686 } 687 break; 688 689 case SIOCGIFADDR: 690 { 691 struct sockaddr *sa; 692 693 sa = (struct sockaddr *) &ifr->ifr_data; 694 bcopy(IFP2AC(ifp)->ac_enaddr, 695 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 696 } 697 break; 698 699 case SIOCGIFMEDIA: 700 VLAN_LOCK(); 701 if (ifv->ifv_p != NULL) { 702 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, 703 SIOCGIFMEDIA, data); 704 VLAN_UNLOCK(); 705 /* Limit the result to the parent's current config. */ 706 if (error == 0) { 707 struct ifmediareq *ifmr; 708 709 ifmr = (struct ifmediareq *) data; 710 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) { 711 ifmr->ifm_count = 1; 712 error = copyout(&ifmr->ifm_current, 713 ifmr->ifm_ulist, 714 sizeof(int)); 715 } 716 } 717 } else { 718 VLAN_UNLOCK(); 719 error = EINVAL; 720 } 721 break; 722 723 case SIOCSIFMEDIA: 724 error = EINVAL; 725 break; 726 727 case SIOCSIFMTU: 728 /* 729 * Set the interface MTU. 730 */ 731 VLAN_LOCK(); 732 if (ifv->ifv_p != NULL) { 733 if (ifr->ifr_mtu > 734 (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) || 735 ifr->ifr_mtu < 736 (ifv->ifv_mintu - ifv->ifv_mtufudge)) 737 error = EINVAL; 738 else 739 ifp->if_mtu = ifr->ifr_mtu; 740 } else 741 error = EINVAL; 742 VLAN_UNLOCK(); 743 break; 744 745 case SIOCSETVLAN: 746 error = copyin(ifr->ifr_data, &vlr, sizeof vlr); 747 if (error) 748 break; 749 if (vlr.vlr_parent[0] == '\0') { 750 VLAN_LOCK(); 751 vlan_unconfig(ifp); 752 if (ifp->if_flags & IFF_UP) 753 if_down(ifp); 754 ifp->if_flags &= ~IFF_RUNNING; 755 VLAN_UNLOCK(); 756 break; 757 } 758 p = ifunit(vlr.vlr_parent); 759 if (p == 0) { 760 error = ENOENT; 761 break; 762 } 763 /* 764 * Don't let the caller set up a VLAN tag with 765 * anything except VLID bits. 766 */ 767 if (vlr.vlr_tag & ~EVL_VLID_MASK) { 768 error = EINVAL; 769 break; 770 } 771 VLAN_LOCK(); 772 error = vlan_config(ifv, p); 773 if (error) { 774 VLAN_UNLOCK(); 775 break; 776 } 777 ifv->ifv_tag = vlr.vlr_tag; 778 ifp->if_flags |= IFF_RUNNING; 779 VLAN_UNLOCK(); 780 781 /* Update promiscuous mode, if necessary. */ 782 vlan_set_promisc(ifp); 783 break; 784 785 case SIOCGETVLAN: 786 bzero(&vlr, sizeof vlr); 787 VLAN_LOCK(); 788 if (ifv->ifv_p) { 789 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname, 790 sizeof(vlr.vlr_parent)); 791 vlr.vlr_tag = ifv->ifv_tag; 792 } 793 VLAN_UNLOCK(); 794 error = copyout(&vlr, ifr->ifr_data, sizeof vlr); 795 break; 796 797 case SIOCSIFFLAGS: 798 /* 799 * For promiscuous mode, we enable promiscuous mode on 800 * the parent if we need promiscuous on the VLAN interface. 801 */ 802 if (ifv->ifv_p != NULL) 803 error = vlan_set_promisc(ifp); 804 break; 805 806 case SIOCADDMULTI: 807 case SIOCDELMULTI: 808 error = vlan_setmulti(ifp); 809 break; 810 default: 811 error = EINVAL; 812 } 813 return error; 814} 815