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: head/sys/net/if_vlan.c 72484 2001-02-14 13:24:01Z asmodai $
|
29 * $FreeBSD: head/sys/net/if_vlan.c 74943 2001-03-28 15:52:12Z yar $ |
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 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 * XXX It's incorrect to assume that we must always kludge up 45 * headers on the physical device's behalf: some devices support 46 * VLAN tag insertion and extraction in firmware. For these cases, 47 * one can change the behavior of the vlan interface by setting 48 * the LINK0 flag on it (that is setting the vlan interface's LINK0 49 * flag, _not_ the parent's LINK0 flag; we try to leave the parent 50 * alone). If the interface has the LINK0 flag set, then it will 51 * not modify the ethernet header on output, because the parent 52 * can do that for itself. On input, the parent can call vlan_input_tag() 53 * directly in order to supply us with an incoming mbuf and the vlan 54 * tag value that goes with it. 55 */ 56 57#include "vlan.h" 58#include "opt_inet.h" 59 60#include <sys/param.h> 61#include <sys/kernel.h> 62#include <sys/malloc.h> 63#include <sys/mbuf.h> 64#include <sys/module.h> 65#include <sys/queue.h> 66#include <sys/socket.h> 67#include <sys/sockio.h> 68#include <sys/sysctl.h> 69#include <sys/systm.h> 70 71#include <net/bpf.h> 72#include <net/ethernet.h> 73#include <net/if.h> 74#include <net/if_arp.h> 75#include <net/if_dl.h> 76#include <net/if_types.h> 77#include <net/if_vlan_var.h> 78 79#ifdef INET 80#include <netinet/in.h> 81#include <netinet/if_ether.h> 82#endif 83 84SYSCTL_DECL(_net_link); 85SYSCTL_NODE(_net_link, IFT_8021_VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN"); 86SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency"); 87 88u_int vlan_proto = ETHERTYPE_VLAN; 89SYSCTL_INT(_net_link_vlan_link, VLANCTL_PROTO, proto, CTLFLAG_RW, &vlan_proto, 90 0, "Ethernet protocol used for VLAN encapsulation"); 91 92static struct ifvlan ifv_softc[NVLAN]; 93 94static void vlan_start(struct ifnet *ifp); 95static void vlan_ifinit(void *foo); 96static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr); 97static int vlan_setmulti(struct ifnet *ifp); 98static int vlan_unconfig(struct ifnet *ifp); 99static int vlan_config(struct ifvlan *ifv, struct ifnet *p); 100 101/* 102 * Program our multicast filter. What we're actually doing is 103 * programming the multicast filter of the parent. This has the 104 * side effect of causing the parent interface to receive multicast 105 * traffic that it doesn't really want, which ends up being discarded 106 * later by the upper protocol layers. Unfortunately, there's no way 107 * to avoid this: there really is only one physical interface. 108 */ 109static int 110vlan_setmulti(struct ifnet *ifp) 111{ 112 struct ifnet *ifp_p; 113 struct ifmultiaddr *ifma, *rifma = NULL; 114 struct ifvlan *sc; 115 struct vlan_mc_entry *mc = NULL; 116 struct sockaddr_dl sdl; 117 int error; 118 119 /* Find the parent. */ 120 sc = ifp->if_softc; 121 ifp_p = sc->ifv_p; 122
|
123 sdl.sdl_len = ETHER_ADDR_LEN;
|
123 bzero((char *)&sdl, sizeof sdl); 124 sdl.sdl_len = sizeof sdl; |
125 sdl.sdl_family = AF_LINK;
|
126 sdl.sdl_alen = ETHER_ADDR_LEN; |
127 128 /* First, remove any existing filter entries. */ 129 while(SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) { 130 mc = SLIST_FIRST(&sc->vlan_mc_listhead); 131 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 132 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl); 133 if (error) 134 return(error); 135 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries); 136 free(mc, M_DEVBUF); 137 } 138 139 /* Now program new ones. */ 140 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 141 if (ifma->ifma_addr->sa_family != AF_LINK) 142 continue;
|
141 mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT);
|
143 mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_WAITOK); |
144 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 145 (char *)&mc->mc_addr, ETHER_ADDR_LEN); 146 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries); 147 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma); 148 if (error) 149 return(error); 150 } 151 152 return(0); 153} 154 155static void 156vlaninit(void) 157{ 158 int i; 159 160 for (i = 0; i < NVLAN; i++) { 161 struct ifnet *ifp = &ifv_softc[i].ifv_if; 162 163 ifp->if_softc = &ifv_softc[i]; 164 ifp->if_name = "vlan"; 165 ifp->if_unit = i; 166 /* NB: flags are not set here */ 167 ifp->if_linkmib = &ifv_softc[i].ifv_mib; 168 ifp->if_linkmiblen = sizeof ifv_softc[i].ifv_mib; 169 /* NB: mtu is not set here */ 170 171 ifp->if_init = vlan_ifinit; 172 ifp->if_start = vlan_start; 173 ifp->if_ioctl = vlan_ioctl; 174 ifp->if_output = ether_output; 175 ifp->if_snd.ifq_maxlen = ifqmaxlen; 176 ether_ifattach(ifp, ETHER_BPF_SUPPORTED); 177 /* Now undo some of the damage... */ 178 ifp->if_data.ifi_type = IFT_8021_VLAN; 179 ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN; 180 ifp->if_resolvemulti = 0; 181 } 182} 183 184static int 185vlan_modevent(module_t mod, int type, void *data) 186{ 187 switch (type) { 188 case MOD_LOAD: 189 vlaninit(); 190 break; 191 case MOD_UNLOAD: 192 printf("if_vlan module unload - not possible for this module type\n"); 193 return EINVAL; 194 } 195 return 0; 196} 197 198static moduledata_t vlan_mod = { 199 "if_vlan", 200 vlan_modevent, 201 0 202}; 203 204DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 205 206static void 207vlan_ifinit(void *foo) 208{ 209 return; 210} 211 212static void 213vlan_start(struct ifnet *ifp) 214{ 215 struct ifvlan *ifv; 216 struct ifnet *p; 217 struct ether_vlan_header *evl; 218 struct mbuf *m; 219 220 ifv = ifp->if_softc; 221 p = ifv->ifv_p; 222 223 ifp->if_flags |= IFF_OACTIVE; 224 for (;;) { 225 IF_DEQUEUE(&ifp->if_snd, m); 226 if (m == 0) 227 break; 228 if (ifp->if_bpf) 229 bpf_mtap(ifp, m); 230 231 /*
|
232 * Do not run parent's if_start() if the parent is not up, 233 * or parent's driver will cause a system crash. 234 */ 235 if ((p->if_flags & (IFF_UP | IFF_RUNNING)) != 236 (IFF_UP | IFF_RUNNING)) { 237 m_freem(m); 238 ifp->if_data.ifi_collisions++; 239 continue; 240 } 241 242 /* |
243 * If the LINK0 flag is set, it means the underlying interface 244 * can do VLAN tag insertion itself and doesn't require us to 245 * create a special header for it. In this case, we just pass 246 * the packet along. However, we need some way to tell the 247 * interface where the packet came from so that it knows how 248 * to find the VLAN tag to use, so we set the rcvif in the 249 * mbuf header to our ifnet. 250 * 251 * Note: we also set the M_PROTO1 flag in the mbuf to let 252 * the parent driver know that the rcvif pointer is really 253 * valid. We need to do this because sometimes mbufs will 254 * be allocated by other parts of the system that contain 255 * garbage in the rcvif pointer. Using the M_PROTO1 flag 256 * lets the driver perform a proper sanity check and avoid 257 * following potentially bogus rcvif pointers off into 258 * never-never land. 259 */ 260 if (ifp->if_flags & IFF_LINK0) { 261 m->m_pkthdr.rcvif = ifp; 262 m->m_flags |= M_PROTO1; 263 } else { 264 M_PREPEND(m, EVL_ENCAPLEN, M_DONTWAIT); 265 if (m == NULL) { 266 printf("vlan%d: M_PREPEND failed", ifp->if_unit); 267 ifp->if_ierrors++; 268 continue; 269 } 270 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 271 272 m = m_pullup(m, ETHER_HDR_LEN + EVL_ENCAPLEN); 273 if (m == NULL) { 274 printf("vlan%d: m_pullup failed", ifp->if_unit); 275 ifp->if_ierrors++; 276 continue; 277 } 278 279 /* 280 * Transform the Ethernet header into an Ethernet header 281 * with 802.1Q encapsulation. 282 */ 283 bcopy(mtod(m, char *) + EVL_ENCAPLEN, mtod(m, char *), 284 sizeof(struct ether_header)); 285 evl = mtod(m, struct ether_vlan_header *); 286 evl->evl_proto = evl->evl_encap_proto; 287 evl->evl_encap_proto = htons(vlan_proto); 288 evl->evl_tag = htons(ifv->ifv_tag); 289#ifdef DEBUG 290 printf("vlan_start: %*D\n", sizeof *evl, 291 (char *)evl, ":"); 292#endif 293 } 294 295 /* 296 * Send it, precisely as ether_output() would have. 297 * We are already running at splimp. 298 */ 299 if (IF_HANDOFF(&p->if_snd, m, p)) 300 ifp->if_opackets++; 301 else 302 ifp->if_oerrors++; 303 } 304 ifp->if_flags &= ~IFF_OACTIVE; 305 306 return; 307} 308 309int 310vlan_input_tag(struct ether_header *eh, struct mbuf *m, u_int16_t t) 311{ 312 int i; 313 struct ifvlan *ifv; 314 315 for (i = 0; i < NVLAN; i++) { 316 ifv = &ifv_softc[i]; 317 if (ifv->ifv_tag == t) 318 break; 319 } 320 321 if (i >= NVLAN || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 322 m_free(m); 323 return -1; /* So the parent can take note */ 324 } 325 326 /* 327 * Having found a valid vlan interface corresponding to 328 * the given source interface and vlan tag, run the 329 * the real packet through ethert_input(). 330 */ 331 m->m_pkthdr.rcvif = &ifv->ifv_if; 332 333 ifv->ifv_if.if_ipackets++; 334 ether_input(&ifv->ifv_if, eh, m); 335 return 0; 336} 337 338int 339vlan_input(struct ether_header *eh, struct mbuf *m) 340{ 341 int i; 342 struct ifvlan *ifv; 343 344 for (i = 0; i < NVLAN; i++) { 345 ifv = &ifv_softc[i]; 346 if (m->m_pkthdr.rcvif == ifv->ifv_p 347 && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *))) 348 == ifv->ifv_tag)) 349 break; 350 } 351 352 if (i >= NVLAN || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 353 m_freem(m); 354 return -1; /* so ether_input can take note */ 355 } 356 357 /* 358 * Having found a valid vlan interface corresponding to 359 * the given source interface and vlan tag, remove the 360 * encapsulation, and run the real packet through 361 * ether_input() a second time (it had better be 362 * reentrant!). 363 */ 364 m->m_pkthdr.rcvif = &ifv->ifv_if; 365 eh->ether_type = mtod(m, u_int16_t *)[1]; 366 m->m_data += EVL_ENCAPLEN; 367 m->m_len -= EVL_ENCAPLEN; 368 m->m_pkthdr.len -= EVL_ENCAPLEN; 369 370 ifv->ifv_if.if_ipackets++; 371 ether_input(&ifv->ifv_if, eh, m); 372 return 0; 373} 374 375static int 376vlan_config(struct ifvlan *ifv, struct ifnet *p) 377{ 378 struct ifaddr *ifa1, *ifa2; 379 struct sockaddr_dl *sdl1, *sdl2; 380 381 if (p->if_data.ifi_type != IFT_ETHER) 382 return EPROTONOSUPPORT; 383 if (ifv->ifv_p) 384 return EBUSY; 385 ifv->ifv_p = p; 386 if (p->if_data.ifi_hdrlen == sizeof(struct ether_vlan_header)) 387 ifv->ifv_if.if_mtu = p->if_mtu; 388 else 389 ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN; 390 391 /*
|
379 * Preserve the state of the LINK0 flag for ourselves.
|
392 * Copy only a selected subset of flags from the parent. 393 * Other flags are none of our business. |
394 */
|
381 ifv->ifv_if.if_flags = (p->if_flags & ~(IFF_LINK0));
|
395 ifv->ifv_if.if_flags = (p->if_flags & 396 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT)); |
397 398 /* 399 * Set up our ``Ethernet address'' to reflect the underlying 400 * physical interface's. 401 */ 402 ifa1 = ifnet_addrs[ifv->ifv_if.if_index - 1]; 403 ifa2 = ifnet_addrs[p->if_index - 1]; 404 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr; 405 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr; 406 sdl1->sdl_type = IFT_ETHER; 407 sdl1->sdl_alen = ETHER_ADDR_LEN; 408 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN); 409 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 410 return 0; 411} 412 413static int 414vlan_unconfig(struct ifnet *ifp) 415{ 416 struct ifaddr *ifa; 417 struct sockaddr_dl *sdl; 418 struct vlan_mc_entry *mc; 419 struct ifvlan *ifv; 420 struct ifnet *p; 421 int error; 422 423 ifv = ifp->if_softc; 424 p = ifv->ifv_p; 425 426 /* 427 * Since the interface is being unconfigured, we need to 428 * empty the list of multicast groups that we may have joined 429 * while we were alive and remove them from the parent's list 430 * as well. 431 */ 432 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) { 433 struct sockaddr_dl sdl; 434 435 sdl.sdl_len = ETHER_ADDR_LEN; 436 sdl.sdl_family = AF_LINK; 437 mc = SLIST_FIRST(&ifv->vlan_mc_listhead); 438 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 439 error = if_delmulti(p, (struct sockaddr *)&sdl); 440 error = if_delmulti(ifp, (struct sockaddr *)&sdl); 441 if (error) 442 return(error); 443 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 444 free(mc, M_DEVBUF); 445 } 446 447 /* Disconnect from parent. */ 448 ifv->ifv_p = NULL; 449 ifv->ifv_if.if_mtu = ETHERMTU; 450 451 /* Clear our MAC address. */ 452 ifa = ifnet_addrs[ifv->ifv_if.if_index - 1]; 453 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 454 sdl->sdl_type = IFT_ETHER; 455 sdl->sdl_alen = ETHER_ADDR_LEN; 456 bzero(LLADDR(sdl), ETHER_ADDR_LEN); 457 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 458 459 return 0; 460} 461 462static int 463vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 464{ 465 struct ifaddr *ifa; 466 struct ifnet *p; 467 struct ifreq *ifr; 468 struct ifvlan *ifv; 469 struct vlanreq vlr; 470 int error = 0; 471 472 ifr = (struct ifreq *)data; 473 ifa = (struct ifaddr *)data; 474 ifv = ifp->if_softc; 475 476 switch (cmd) { 477 case SIOCSIFADDR: 478 ifp->if_flags |= IFF_UP; 479 480 switch (ifa->ifa_addr->sa_family) { 481#ifdef INET 482 case AF_INET: 483 arp_ifinit(&ifv->ifv_ac, ifa); 484 break; 485#endif 486 default: 487 break; 488 } 489 break; 490 491 case SIOCGIFADDR: 492 { 493 struct sockaddr *sa; 494 495 sa = (struct sockaddr *) &ifr->ifr_data; 496 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, 497 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 498 } 499 break; 500 501 case SIOCSIFMTU: 502 /* 503 * Set the interface MTU. 504 * This is bogus. The underlying interface might support 505 * jumbo frames. 506 */ 507 if (ifr->ifr_mtu > ETHERMTU) { 508 error = EINVAL; 509 } else { 510 ifp->if_mtu = ifr->ifr_mtu; 511 } 512 break; 513 514 case SIOCSETVLAN: 515 error = copyin(ifr->ifr_data, &vlr, sizeof vlr); 516 if (error) 517 break; 518 if (vlr.vlr_parent[0] == '\0') { 519 vlan_unconfig(ifp);
|
505 if_down(ifp);
506 ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
|
520 if (ifp->if_flags & IFF_UP) { 521 int s = splimp(); 522 if_down(ifp); 523 splx(s); 524 } 525 ifp->if_flags &= ~IFF_RUNNING; |
526 break; 527 } 528 p = ifunit(vlr.vlr_parent); 529 if (p == 0) { 530 error = ENOENT; 531 break; 532 } 533 error = vlan_config(ifv, p); 534 if (error) 535 break; 536 ifv->ifv_tag = vlr.vlr_tag; 537 ifp->if_flags |= IFF_RUNNING; 538 break; 539 540 case SIOCGETVLAN: 541 bzero(&vlr, sizeof vlr); 542 if (ifv->ifv_p) { 543 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), 544 "%s%d", ifv->ifv_p->if_name, ifv->ifv_p->if_unit); 545 vlr.vlr_tag = ifv->ifv_tag; 546 } 547 error = copyout(&vlr, ifr->ifr_data, sizeof vlr); 548 break; 549 550 case SIOCSIFFLAGS: 551 /* 552 * We don't support promiscuous mode 553 * right now because it would require help from the 554 * underlying drivers, which hasn't been implemented. 555 */ 556 if (ifr->ifr_flags & (IFF_PROMISC)) { 557 ifp->if_flags &= ~(IFF_PROMISC); 558 error = EINVAL; 559 } 560 break; 561 case SIOCADDMULTI: 562 case SIOCDELMULTI: 563 error = vlan_setmulti(ifp); 564 break; 565 default: 566 error = EINVAL; 567 } 568 return error; 569}
|