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