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