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_HANDOFF(&p->if_snd, m, p)) 265 ifp->if_opackets++; 266 else 267 ifp->if_oerrors++; 268 } 269 ifp->if_flags &= ~IFF_OACTIVE; 270 271 return; 272 } 273 274 int 275 vlan_input_tag(struct ether_header *eh, struct mbuf *m, u_int16_t t) 276 { 277 int i; 278 struct ifvlan *ifv; 279 280 for (i = 0; i < NVLAN; i++) { 281 ifv = &ifv_softc[i]; 282 if (ifv->ifv_tag == t) 283 break; 284 } 285 286 if (i >= NVLAN || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 287 m_free(m); 288 return -1; /* So the parent can take note */ 289 } 290 291 /* 292 * Having found a valid vlan interface corresponding to 293 * the given source interface and vlan tag, run the 294 * the real packet through ethert_input(). 295 */ 296 m->m_pkthdr.rcvif = &ifv->ifv_if; 297 298 ifv->ifv_if.if_ipackets++; 299 ether_input(&ifv->ifv_if, eh, m); 300 return 0; 301 } 302 303 int 304 vlan_input(struct ether_header *eh, struct mbuf *m) 305 { 306 int i; 307 struct ifvlan *ifv; 308 309 for (i = 0; i < NVLAN; i++) { 310 ifv = &ifv_softc[i]; 311 if (m->m_pkthdr.rcvif == ifv->ifv_p 312 && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *))) 313 == ifv->ifv_tag)) 314 break; 315 } 316 317 if (i >= NVLAN || (ifv->ifv_if.if_flags & IFF_UP) == 0) { 318 m_freem(m); 319 return -1; /* so ether_input can take note */ 320 } 321 322 /* 323 * Having found a valid vlan interface corresponding to 324 * the given source interface and vlan tag, remove the 325 * encapsulation, and run the real packet through 326 * ether_input() a second time (it had better be 327 * reentrant!). 328 */ 329 m->m_pkthdr.rcvif = &ifv->ifv_if; 330 eh->ether_type = mtod(m, u_int16_t *)[1]; 331 m->m_data += EVL_ENCAPLEN; 332 m->m_len -= EVL_ENCAPLEN; 333 m->m_pkthdr.len -= EVL_ENCAPLEN; 334 335 ifv->ifv_if.if_ipackets++; 336 ether_input(&ifv->ifv_if, eh, m); 337 return 0; 338 } 339 340 static int 341 vlan_config(struct ifvlan *ifv, struct ifnet *p) 342 { 343 struct ifaddr *ifa1, *ifa2; 344 struct sockaddr_dl *sdl1, *sdl2; 345 346 if (p->if_data.ifi_type != IFT_ETHER) 347 return EPROTONOSUPPORT; 348 if (ifv->ifv_p) 349 return EBUSY; 350 ifv->ifv_p = p; 351 if (p->if_data.ifi_hdrlen == sizeof(struct ether_vlan_header)) 352 ifv->ifv_if.if_mtu = p->if_mtu; 353 else 354 ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN; 355 356 /* 357 * Preserve the state of the LINK0 flag for ourselves. 358 */ 359 ifv->ifv_if.if_flags = (p->if_flags & ~(IFF_LINK0)); 360 361 /* 362 * Set up our ``Ethernet address'' to reflect the underlying 363 * physical interface's. 364 */ 365 ifa1 = ifnet_addrs[ifv->ifv_if.if_index - 1]; 366 ifa2 = ifnet_addrs[p->if_index - 1]; 367 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr; 368 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr; 369 sdl1->sdl_type = IFT_ETHER; 370 sdl1->sdl_alen = ETHER_ADDR_LEN; 371 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN); 372 bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 373 return 0; 374 } 375 376 static int 377 vlan_unconfig(struct ifnet *ifp) 378 { 379 struct ifaddr *ifa; 380 struct sockaddr_dl *sdl; 381 struct vlan_mc_entry *mc; 382 struct ifvlan *ifv; 383 struct ifnet *p; 384 int error; 385 386 ifv = ifp->if_softc; 387 p = ifv->ifv_p; 388 389 /* 390 * Since the interface is being unconfigured, we need to 391 * empty the list of multicast groups that we may have joined 392 * while we were alive and remove them from the parent's list 393 * as well. 394 */ 395 while(ifv->vlan_mc_listhead.slh_first != NULL) { 396 struct sockaddr_dl sdl; 397 398 sdl.sdl_len = ETHER_ADDR_LEN; 399 sdl.sdl_family = AF_LINK; 400 mc = ifv->vlan_mc_listhead.slh_first; 401 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN); 402 error = if_delmulti(p, (struct sockaddr *)&sdl); 403 error = if_delmulti(ifp, (struct sockaddr *)&sdl); 404 if (error) 405 return(error); 406 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries); 407 free(mc, M_DEVBUF); 408 } 409 410 /* Disconnect from parent. */ 411 ifv->ifv_p = NULL; 412 ifv->ifv_if.if_mtu = ETHERMTU; 413 414 /* Clear our MAC address. */ 415 ifa = ifnet_addrs[ifv->ifv_if.if_index - 1]; 416 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 417 sdl->sdl_type = IFT_ETHER; 418 sdl->sdl_alen = ETHER_ADDR_LEN; 419 bzero(LLADDR(sdl), ETHER_ADDR_LEN); 420 bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN); 421 422 return 0; 423 } 424 425 static int 426 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 427 { 428 struct ifaddr *ifa; 429 struct ifnet *p; 430 struct ifreq *ifr; 431 struct ifvlan *ifv; 432 struct vlanreq vlr; 433 int error = 0; 434 435 ifr = (struct ifreq *)data; 436 ifa = (struct ifaddr *)data; 437 ifv = ifp->if_softc; 438 439 switch (cmd) { 440 case SIOCSIFADDR: 441 ifp->if_flags |= IFF_UP; 442 443 switch (ifa->ifa_addr->sa_family) { 444 #ifdef INET 445 case AF_INET: 446 arp_ifinit(&ifv->ifv_ac, ifa); 447 break; 448 #endif 449 default: 450 break; 451 } 452 break; 453 454 case SIOCGIFADDR: 455 { 456 struct sockaddr *sa; 457 458 sa = (struct sockaddr *) &ifr->ifr_data; 459 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, 460 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 461 } 462 break; 463 464 case SIOCSIFMTU: 465 /* 466 * Set the interface MTU. 467 * This is bogus. The underlying interface might support 468 * jumbo frames. 469 */ 470 if (ifr->ifr_mtu > ETHERMTU) { 471 error = EINVAL; 472 } else { 473 ifp->if_mtu = ifr->ifr_mtu; 474 } 475 break; 476 477 case SIOCSETVLAN: 478 error = copyin(ifr->ifr_data, &vlr, sizeof vlr); 479 if (error) 480 break; 481 if (vlr.vlr_parent[0] == '\0') { 482 vlan_unconfig(ifp); 483 if_down(ifp); 484 ifp->if_flags &= ~(IFF_UP|IFF_RUNNING); 485 break; 486 } 487 p = ifunit(vlr.vlr_parent); 488 if (p == 0) { 489 error = ENOENT; 490 break; 491 } 492 error = vlan_config(ifv, p); 493 if (error) 494 break; 495 ifv->ifv_tag = vlr.vlr_tag; 496 ifp->if_flags |= IFF_RUNNING; 497 break; 498 499 case SIOCGETVLAN: 500 bzero(&vlr, sizeof vlr); 501 if (ifv->ifv_p) { 502 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), 503 "%s%d", ifv->ifv_p->if_name, ifv->ifv_p->if_unit); 504 vlr.vlr_tag = ifv->ifv_tag; 505 } 506 error = copyout(&vlr, ifr->ifr_data, sizeof vlr); 507 break; 508 509 case SIOCSIFFLAGS: 510 /* 511 * We don't support promiscuous mode 512 * right now because it would require help from the 513 * underlying drivers, which hasn't been implemented. 514 */ 515 if (ifr->ifr_flags & (IFF_PROMISC)) { 516 ifp->if_flags &= ~(IFF_PROMISC); 517 error = EINVAL; 518 } 519 break; 520 case SIOCADDMULTI: 521 case SIOCDELMULTI: 522 error = vlan_setmulti(ifp); 523 break; 524 default: 525 error = EINVAL; 526 } 527 return error; 528 } 529