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