xref: /freebsd/sys/net/if_vlan.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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 		if_attach(ifp);
174 		ether_ifattach(ifp);
175 		bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
176 		/* Now undo some of the damage... */
177 		ifp->if_data.ifi_type = IFT_8021_VLAN;
178 		ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;
179 		ifp->if_resolvemulti = 0;
180 	}
181 }
182 PSEUDO_SET(vlaninit, if_vlan);
183 
184 static void
185 vlan_ifinit(void *foo)
186 {
187 	return;
188 }
189 
190 static void
191 vlan_start(struct ifnet *ifp)
192 {
193 	struct ifvlan *ifv;
194 	struct ifnet *p;
195 	struct ether_vlan_header *evl;
196 	struct mbuf *m;
197 
198 	ifv = ifp->if_softc;
199 	p = ifv->ifv_p;
200 
201 	ifp->if_flags |= IFF_OACTIVE;
202 	for (;;) {
203 		IF_DEQUEUE(&ifp->if_snd, m);
204 		if (m == 0)
205 			break;
206 		if (ifp->if_bpf)
207 			bpf_mtap(ifp, m);
208 
209 		/*
210 		 * If the LINK0 flag is set, it means the underlying interface
211 		 * can do VLAN tag insertion itself and doesn't require us to
212 	 	 * create a special header for it. In this case, we just pass
213 		 * the packet along. However, we need some way to tell the
214 		 * interface where the packet came from so that it knows how
215 		 * to find the VLAN tag to use, so we set the rcvif in the
216 		 * mbuf header to our ifnet.
217 		 *
218 		 * Note: we also set the M_PROTO1 flag in the mbuf to let
219 		 * the parent driver know that the rcvif pointer is really
220 		 * valid. We need to do this because sometimes mbufs will
221 		 * be allocated by other parts of the system that contain
222 		 * garbage in the rcvif pointer. Using the M_PROTO1 flag
223 		 * lets the driver perform a proper sanity check and avoid
224 		 * following potentially bogus rcvif pointers off into
225 		 * never-never land.
226 		 */
227 		if (ifp->if_flags & IFF_LINK0) {
228 			m->m_pkthdr.rcvif = ifp;
229 			m->m_flags |= M_PROTO1;
230 		} else {
231 			M_PREPEND(m, EVL_ENCAPLEN, M_DONTWAIT);
232 			if (m == NULL) {
233 				printf("vlan%d: M_PREPEND failed", ifp->if_unit);
234 				ifp->if_ierrors++;
235 				continue;
236 			}
237 			/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
238 
239 			m = m_pullup(m, ETHER_HDR_LEN + EVL_ENCAPLEN);
240 			if (m == NULL) {
241 				printf("vlan%d: m_pullup failed", ifp->if_unit);
242 				ifp->if_ierrors++;
243 				continue;
244 			}
245 
246 			/*
247 			 * Transform the Ethernet header into an Ethernet header
248 			 * with 802.1Q encapsulation.
249 			 */
250 			bcopy(mtod(m, char *) + EVL_ENCAPLEN, mtod(m, char *),
251 			      sizeof(struct ether_header));
252 			evl = mtod(m, struct ether_vlan_header *);
253 			evl->evl_proto = evl->evl_encap_proto;
254 			evl->evl_encap_proto = htons(vlan_proto);
255 			evl->evl_tag = htons(ifv->ifv_tag);
256 #ifdef DEBUG
257 			printf("vlan_start: %*D\n", sizeof *evl,
258 			    (char *)evl, ":");
259 #endif
260 		}
261 
262 		/*
263 		 * Send it, precisely as ether_output() would have.
264 		 * We are already running at splimp.
265 		 */
266 		if (IF_QFULL(&p->if_snd)) {
267 			IF_DROP(&p->if_snd);
268 				/* XXX stats */
269 			ifp->if_oerrors++;
270 			m_freem(m);
271 			continue;
272 		}
273 		IF_ENQUEUE(&p->if_snd, m);
274 		if ((p->if_flags & IFF_OACTIVE) == 0) {
275 			p->if_start(p);
276 			ifp->if_opackets++;
277 		}
278 	}
279 	ifp->if_flags &= ~IFF_OACTIVE;
280 
281 	return;
282 }
283 
284 int
285 vlan_input_tag(struct ether_header *eh, struct mbuf *m, u_int16_t t)
286 {
287 	int i;
288 	struct ifvlan *ifv;
289 
290 	for (i = 0; i < NVLAN; i++) {
291 		ifv = &ifv_softc[i];
292 		if (ifv->ifv_tag == t)
293 			break;
294 	}
295 
296 	if (i >= NVLAN || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
297 		m_free(m);
298 		return -1;	/* So the parent can take note */
299 	}
300 
301 	/*
302 	 * Having found a valid vlan interface corresponding to
303 	 * the given source interface and vlan tag, run the
304 	 * the real packet through ethert_input().
305 	 */
306 	m->m_pkthdr.rcvif = &ifv->ifv_if;
307 
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 	ifv->ifv_if.if_ipackets++;
322 	ether_input(&ifv->ifv_if, eh, m);
323 	return 0;
324 }
325 
326 int
327 vlan_input(struct ether_header *eh, struct mbuf *m)
328 {
329 	int i;
330 	struct ifvlan *ifv;
331 
332 	for (i = 0; i < NVLAN; i++) {
333 		ifv = &ifv_softc[i];
334 		if (m->m_pkthdr.rcvif == ifv->ifv_p
335 		    && (EVL_VLANOFTAG(ntohs(*mtod(m, u_int16_t *)))
336 			== ifv->ifv_tag))
337 			break;
338 	}
339 
340 	if (i >= NVLAN || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
341 		m_freem(m);
342 		return -1;	/* so ether_input can take note */
343 	}
344 
345 	/*
346 	 * Having found a valid vlan interface corresponding to
347 	 * the given source interface and vlan tag, remove the
348 	 * encapsulation, and run the real packet through
349 	 * ether_input() a second time (it had better be
350 	 * reentrant!).
351 	 */
352 	m->m_pkthdr.rcvif = &ifv->ifv_if;
353 	eh->ether_type = mtod(m, u_int16_t *)[1];
354 	m->m_data += EVL_ENCAPLEN;
355 	m->m_len -= EVL_ENCAPLEN;
356 	m->m_pkthdr.len -= EVL_ENCAPLEN;
357 
358 	if (ifv->ifv_if.if_bpf) {
359 		/*
360 		 * Do the usual BPF fakery.  Note that we don't support
361 		 * promiscuous mode here, since it would require the
362 		 * drivers to know about VLANs and we're not ready for
363 		 * that yet.
364 		 */
365 		struct mbuf m0;
366 		m0.m_next = m;
367 		m0.m_len = sizeof(struct ether_header);
368 		m0.m_data = (char *)eh;
369 		bpf_mtap(&ifv->ifv_if, &m0);
370 	}
371 	ifv->ifv_if.if_ipackets++;
372 	ether_input(&ifv->ifv_if, eh, m);
373 	return 0;
374 }
375 
376 static int
377 vlan_config(struct ifvlan *ifv, struct ifnet *p)
378 {
379 	struct ifaddr *ifa1, *ifa2;
380 	struct sockaddr_dl *sdl1, *sdl2;
381 
382 	if (p->if_data.ifi_type != IFT_ETHER)
383 		return EPROTONOSUPPORT;
384 	if (ifv->ifv_p)
385 		return EBUSY;
386 	ifv->ifv_p = p;
387 	if (p->if_data.ifi_hdrlen == sizeof(struct ether_vlan_header))
388 		ifv->ifv_if.if_mtu = p->if_mtu;
389 	else
390 		ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN;
391 
392 	/*
393 	 * Preserve the state of the LINK0 flag for ourselves.
394 	 */
395 	ifv->ifv_if.if_flags = (p->if_flags & ~(IFF_LINK0));
396 
397 	/*
398 	 * Set up our ``Ethernet address'' to reflect the underlying
399 	 * physical interface's.
400 	 */
401 	ifa1 = ifnet_addrs[ifv->ifv_if.if_index - 1];
402 	ifa2 = ifnet_addrs[p->if_index - 1];
403 	sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
404 	sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
405 	sdl1->sdl_type = IFT_ETHER;
406 	sdl1->sdl_alen = ETHER_ADDR_LEN;
407 	bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
408 	bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
409 	return 0;
410 }
411 
412 static int
413 vlan_unconfig(struct ifnet *ifp)
414 {
415 	struct ifaddr *ifa;
416 	struct sockaddr_dl *sdl;
417 	struct vlan_mc_entry *mc;
418 	struct ifvlan *ifv;
419 	struct ifnet *p;
420 	int error;
421 
422 	ifv = ifp->if_softc;
423 	p = ifv->ifv_p;
424 
425 	/*
426  	 * Since the interface is being unconfigured, we need to
427 	 * empty the list of multicast groups that we may have joined
428 	 * while we were alive and remove them from the parent's list
429 	 * as well.
430 	 */
431 	while(ifv->vlan_mc_listhead.slh_first != NULL) {
432 		struct sockaddr_dl	sdl;
433 
434 		sdl.sdl_len = ETHER_ADDR_LEN;
435 		sdl.sdl_family = AF_LINK;
436 		mc = ifv->vlan_mc_listhead.slh_first;
437 		bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
438 		error = if_delmulti(p, (struct sockaddr *)&sdl);
439 		error = if_delmulti(ifp, (struct sockaddr *)&sdl);
440 		if (error)
441 			return(error);
442 		SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
443 		free(mc, M_DEVBUF);
444 	}
445 
446 	/* Disconnect from parent. */
447 	ifv->ifv_p = NULL;
448 	ifv->ifv_if.if_mtu = ETHERMTU;
449 
450 	/* Clear our MAC address. */
451 	ifa = ifnet_addrs[ifv->ifv_if.if_index - 1];
452 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
453 	sdl->sdl_type = IFT_ETHER;
454 	sdl->sdl_alen = ETHER_ADDR_LEN;
455 	bzero(LLADDR(sdl), ETHER_ADDR_LEN);
456 	bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
457 
458 	return 0;
459 }
460 
461 static int
462 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
463 {
464 	struct ifaddr *ifa;
465 	struct ifnet *p;
466 	struct ifreq *ifr;
467 	struct ifvlan *ifv;
468 	struct vlanreq vlr;
469 	int error = 0;
470 
471 	ifr = (struct ifreq *)data;
472 	ifa = (struct ifaddr *)data;
473 	ifv = ifp->if_softc;
474 
475 	switch (cmd) {
476 	case SIOCSIFADDR:
477 		ifp->if_flags |= IFF_UP;
478 
479 		switch (ifa->ifa_addr->sa_family) {
480 #ifdef INET
481 		case AF_INET:
482 			arp_ifinit(&ifv->ifv_ac, ifa);
483 			break;
484 #endif
485 		default:
486 			break;
487 		}
488 		break;
489 
490 	case SIOCGIFADDR:
491 		{
492 			struct sockaddr *sa;
493 
494 			sa = (struct sockaddr *) &ifr->ifr_data;
495 			bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
496 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
497 		}
498 		break;
499 
500 	case SIOCSIFMTU:
501 		/*
502 		 * Set the interface MTU.
503 		 * This is bogus. The underlying interface might support
504 	 	 * jumbo frames.
505 		 */
506 		if (ifr->ifr_mtu > ETHERMTU) {
507 			error = EINVAL;
508 		} else {
509 			ifp->if_mtu = ifr->ifr_mtu;
510 		}
511 		break;
512 
513 	case SIOCSETVLAN:
514 		error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
515 		if (error)
516 			break;
517 		if (vlr.vlr_parent[0] == '\0') {
518 			vlan_unconfig(ifp);
519 			if_down(ifp);
520 			ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
521 			break;
522 		}
523 		p = ifunit(vlr.vlr_parent);
524 		if (p == 0) {
525 			error = ENOENT;
526 			break;
527 		}
528 		error = vlan_config(ifv, p);
529 		if (error)
530 			break;
531 		ifv->ifv_tag = vlr.vlr_tag;
532 		ifp->if_flags |= IFF_RUNNING;
533 		break;
534 
535 	case SIOCGETVLAN:
536 		bzero(&vlr, sizeof vlr);
537 		if (ifv->ifv_p) {
538 			snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent),
539 			    "%s%d", ifv->ifv_p->if_name, ifv->ifv_p->if_unit);
540 			vlr.vlr_tag = ifv->ifv_tag;
541 		}
542 		error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
543 		break;
544 
545 	case SIOCSIFFLAGS:
546 		/*
547 		 * We don't support promiscuous mode
548 		 * right now because it would require help from the
549 		 * underlying drivers, which hasn't been implemented.
550 		 */
551 		if (ifr->ifr_flags & (IFF_PROMISC)) {
552 			ifp->if_flags &= ~(IFF_PROMISC);
553 			error = EINVAL;
554 		}
555 		break;
556 	case SIOCADDMULTI:
557 	case SIOCDELMULTI:
558 		error = vlan_setmulti(ifp);
559 		break;
560 	default:
561 		error = EINVAL;
562 	}
563 	return error;
564 }
565