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