xref: /freebsd/sys/net/if_vlan.c (revision fbf96e52bbd90bbbb9c9e2ae6fbc101fa6ebd080)
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 #include "opt_inet.h"
45 
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/module.h>
51 #include <sys/queue.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
56 
57 #include <net/bpf.h>
58 #include <net/ethernet.h>
59 #include <net/if.h>
60 #include <net/if_clone.h>
61 #include <net/if_arp.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 #include <net/if_vlan_var.h>
65 #include <net/route.h>
66 
67 #ifdef INET
68 #include <netinet/in.h>
69 #include <netinet/if_ether.h>
70 #endif
71 
72 #define VLANNAME	"vlan"
73 
74 struct vlan_mc_entry {
75 	struct ether_addr		mc_addr;
76 	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
77 };
78 
79 struct	ifvlan {
80 	struct	arpcom ifv_ac;	/* make this an interface */
81 	struct	ifnet *ifv_p;	/* parent inteface of this vlan */
82 	struct	ifv_linkmib {
83 		int	ifvm_parent;
84 		int	ifvm_encaplen;	/* encapsulation length */
85 		int	ifvm_mtufudge;	/* MTU fudged by this much */
86 		int	ifvm_mintu;	/* min transmission unit */
87 		u_int16_t ifvm_proto; /* encapsulation ethertype */
88 		u_int16_t ifvm_tag; /* tag to apply on packets leaving if */
89 	}	ifv_mib;
90 	SLIST_HEAD(__vlan_mchead, vlan_mc_entry)	vlan_mc_listhead;
91 	LIST_ENTRY(ifvlan) ifv_list;
92 	int	ifv_flags;
93 };
94 #define	ifv_if	ifv_ac.ac_if
95 #define	ifv_tag	ifv_mib.ifvm_tag
96 #define	ifv_encaplen	ifv_mib.ifvm_encaplen
97 #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
98 #define	ifv_mintu	ifv_mib.ifvm_mintu
99 
100 #define	IFVF_PROMISC	0x01		/* promiscuous mode enabled */
101 
102 SYSCTL_DECL(_net_link);
103 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
104 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
105 
106 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
107 static LIST_HEAD(, ifvlan) ifv_list;
108 
109 /*
110  * Locking: one lock is used to guard both the ifv_list and modification
111  * to vlan data structures.  We are rather conservative here; probably
112  * more than necessary.
113  */
114 static struct mtx ifv_mtx;
115 #define	VLAN_LOCK_INIT()	mtx_init(&ifv_mtx, VLANNAME, NULL, MTX_DEF)
116 #define	VLAN_LOCK_DESTROY()	mtx_destroy(&ifv_mtx)
117 #define	VLAN_LOCK_ASSERT()	mtx_assert(&ifv_mtx, MA_OWNED)
118 #define	VLAN_LOCK()	mtx_lock(&ifv_mtx)
119 #define	VLAN_UNLOCK()	mtx_unlock(&ifv_mtx)
120 
121 static	void vlan_start(struct ifnet *ifp);
122 static	void vlan_ifinit(void *foo);
123 static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
124 static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
125 static	int vlan_setmulti(struct ifnet *ifp);
126 static	int vlan_unconfig(struct ifnet *ifp);
127 static	int vlan_config(struct ifvlan *ifv, struct ifnet *p);
128 static	void vlan_link_state(struct ifnet *ifp, int link);
129 static	int vlan_set_promisc(struct ifnet *ifp);
130 
131 static	struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
132     const char *, int *);
133 static	int vlan_clone_match(struct if_clone *, const char *);
134 static	int vlan_clone_create(struct if_clone *, char *, size_t);
135 static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
136 
137 struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL, IF_MAXUNIT,
138     NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
139 
140 /*
141  * Program our multicast filter. What we're actually doing is
142  * programming the multicast filter of the parent. This has the
143  * side effect of causing the parent interface to receive multicast
144  * traffic that it doesn't really want, which ends up being discarded
145  * later by the upper protocol layers. Unfortunately, there's no way
146  * to avoid this: there really is only one physical interface.
147  *
148  * XXX: There is a possible race here if more than one thread is
149  *      modifying the multicast state of the vlan interface at the same time.
150  */
151 static int
152 vlan_setmulti(struct ifnet *ifp)
153 {
154 	struct ifnet		*ifp_p;
155 	struct ifmultiaddr	*ifma, *rifma = NULL;
156 	struct ifvlan		*sc;
157 	struct vlan_mc_entry	*mc = NULL;
158 	struct sockaddr_dl	sdl;
159 	int			error;
160 
161 	/*VLAN_LOCK_ASSERT();*/
162 
163 	/* Find the parent. */
164 	sc = ifp->if_softc;
165 	ifp_p = sc->ifv_p;
166 
167 	/*
168 	 * If we don't have a parent, just remember the membership for
169 	 * when we do.
170 	 */
171 	if (ifp_p == NULL)
172 		return (0);
173 
174 	bzero((char *)&sdl, sizeof(sdl));
175 	sdl.sdl_len = sizeof(sdl);
176 	sdl.sdl_family = AF_LINK;
177 	sdl.sdl_index = ifp_p->if_index;
178 	sdl.sdl_type = IFT_ETHER;
179 	sdl.sdl_alen = ETHER_ADDR_LEN;
180 
181 	/* First, remove any existing filter entries. */
182 	while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
183 		mc = SLIST_FIRST(&sc->vlan_mc_listhead);
184 		bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
185 		error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
186 		if (error)
187 			return (error);
188 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
189 		free(mc, M_VLAN);
190 	}
191 
192 	/* Now program new ones. */
193 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
194 		if (ifma->ifma_addr->sa_family != AF_LINK)
195 			continue;
196 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
197 		if (mc == NULL)
198 			return (ENOMEM);
199 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
200 		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
201 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
202 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
203 		    LLADDR(&sdl), ETHER_ADDR_LEN);
204 		error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
205 		if (error)
206 			return (error);
207 	}
208 
209 	return (0);
210 }
211 
212 /*
213  * VLAN support can be loaded as a module.  The only place in the
214  * system that's intimately aware of this is ether_input.  We hook
215  * into this code through vlan_input_p which is defined there and
216  * set here.  Noone else in the system should be aware of this so
217  * we use an explicit reference here.
218  *
219  * NB: Noone should ever need to check if vlan_input_p is null or
220  *     not.  This is because interfaces have a count of the number
221  *     of active vlans (if_nvlans) and this should never be bumped
222  *     except by vlan_config--which is in this module so therefore
223  *     the module must be loaded and vlan_input_p must be non-NULL.
224  */
225 extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
226 
227 /* For MII eyes only... */
228 extern	void (*vlan_link_state_p)(struct ifnet *, int);
229 
230 static int
231 vlan_modevent(module_t mod, int type, void *data)
232 {
233 
234 	switch (type) {
235 	case MOD_LOAD:
236 		LIST_INIT(&ifv_list);
237 		VLAN_LOCK_INIT();
238 		vlan_input_p = vlan_input;
239 		vlan_link_state_p = vlan_link_state;
240 		if_clone_attach(&vlan_cloner);
241 		break;
242 	case MOD_UNLOAD:
243 		if_clone_detach(&vlan_cloner);
244 		vlan_input_p = NULL;
245 		vlan_link_state_p = NULL;
246 		while (!LIST_EMPTY(&ifv_list))
247 			vlan_clone_destroy(&vlan_cloner,
248 			    &LIST_FIRST(&ifv_list)->ifv_if);
249 		VLAN_LOCK_DESTROY();
250 		break;
251 	}
252 	return (0);
253 }
254 
255 static moduledata_t vlan_mod = {
256 	"if_vlan",
257 	vlan_modevent,
258 	0
259 };
260 
261 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
262 MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
263 
264 static struct ifnet *
265 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
266 {
267 	const char *cp;
268 	struct ifnet *ifp;
269 	int t = 0;
270 
271 	/* Check for <etherif>.<vlan> style interface names. */
272 	IFNET_RLOCK();
273 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
274 		if (ifp->if_type != IFT_ETHER)
275 			continue;
276 		if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
277 			continue;
278 		cp = name + strlen(ifp->if_xname);
279 		if (*cp != '.')
280 			continue;
281 		for(; *cp != '\0'; cp++) {
282 			if (*cp < '0' || *cp > '9')
283 				continue;
284 			t = (t * 10) + (*cp - '0');
285 		}
286 		if (tag != NULL)
287 			*tag = t;
288 		break;
289 	}
290 	IFNET_RUNLOCK();
291 
292 	return (ifp);
293 }
294 
295 static int
296 vlan_clone_match(struct if_clone *ifc, const char *name)
297 {
298 	const char *cp;
299 
300 	if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
301 		return (1);
302 
303 	if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
304 		return (0);
305 	for (cp = name + 4; *cp != '\0'; cp++) {
306 		if (*cp < '0' || *cp > '9')
307 			return (0);
308 	}
309 
310 	return (1);
311 }
312 
313 static int
314 vlan_clone_create(struct if_clone *ifc, char *name, size_t len)
315 {
316 	char *dp;
317 	int wildcard;
318 	int unit;
319 	int error;
320 	int tag;
321 	int ethertag;
322 	struct ifvlan *ifv;
323 	struct ifnet *ifp;
324 	struct ifnet *p;
325 
326 	if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
327 		ethertag = 1;
328 		unit = -1;
329 		wildcard = 0;
330 
331 		/*
332 		 * Don't let the caller set up a VLAN tag with
333 		 * anything except VLID bits.
334 		 */
335 		if (tag & ~EVL_VLID_MASK)
336 			return (EINVAL);
337 	} else {
338 		ethertag = 0;
339 
340 		error = ifc_name2unit(name, &unit);
341 		if (error != 0)
342 			return (error);
343 
344 		wildcard = (unit < 0);
345 	}
346 
347 	error = ifc_alloc_unit(ifc, &unit);
348 	if (error != 0)
349 		return (error);
350 
351 	/* In the wildcard case, we need to update the name. */
352 	if (wildcard) {
353 		for (dp = name; *dp != '\0'; dp++);
354 		if (snprintf(dp, len - (dp-name), "%d", unit) >
355 		    len - (dp-name) - 1) {
356 			panic("%s: interface name too long", __func__);
357 		}
358 	}
359 
360 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
361 	ifp = &ifv->ifv_if;
362 	SLIST_INIT(&ifv->vlan_mc_listhead);
363 
364 	ifp->if_softc = ifv;
365 	/*
366 	 * Set the name manually rather then using if_initname because
367 	 * we don't conform to the default naming convention for interfaces.
368 	 */
369 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
370 	ifp->if_dname = ifc->ifc_name;
371 	ifp->if_dunit = unit;
372 	/* NB: flags are not set here */
373 	ifp->if_linkmib = &ifv->ifv_mib;
374 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
375 	/* NB: mtu is not set here */
376 
377 	ifp->if_init = vlan_ifinit;
378 	ifp->if_start = vlan_start;
379 	ifp->if_ioctl = vlan_ioctl;
380 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
381 	ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr);
382 	/* Now undo some of the damage... */
383 	ifp->if_baudrate = 0;
384 	ifp->if_type = IFT_L2VLAN;
385 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
386 
387 	VLAN_LOCK();
388 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
389 	VLAN_UNLOCK();
390 
391 	if (ethertag) {
392 		VLAN_LOCK();
393 		error = vlan_config(ifv, p);
394 		if (error != 0) {
395 			/*
396 			 * Since we've partialy failed, we need to back
397 			 * out all the way, otherwise userland could get
398 			 * confused.  Thus, we destroy the interface.
399 			 */
400 			LIST_REMOVE(ifv, ifv_list);
401 			vlan_unconfig(ifp);
402 			VLAN_UNLOCK();
403 			ether_ifdetach(ifp);
404 			free(ifv, M_VLAN);
405 
406 			return (error);
407 		}
408 		ifv->ifv_tag = tag;
409 		ifp->if_flags |= IFF_RUNNING;
410 		VLAN_UNLOCK();
411 
412 		/* Update promiscuous mode, if necessary. */
413 		vlan_set_promisc(ifp);
414 	}
415 
416 	return (0);
417 }
418 
419 static int
420 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
421 {
422 	struct ifvlan *ifv = ifp->if_softc;
423 
424 	VLAN_LOCK();
425 	LIST_REMOVE(ifv, ifv_list);
426 	vlan_unconfig(ifp);
427 	VLAN_UNLOCK();
428 
429 	ether_ifdetach(ifp);
430 
431 	free(ifv, M_VLAN);
432 
433 	return (0);
434 }
435 
436 /*
437  * The ifp->if_init entry point for vlan(4) is a no-op.
438  */
439 static void
440 vlan_ifinit(void *foo)
441 {
442 
443 }
444 
445 static void
446 vlan_start(struct ifnet *ifp)
447 {
448 	struct ifvlan *ifv;
449 	struct ifnet *p;
450 	struct ether_vlan_header *evl;
451 	struct mbuf *m;
452 	int error;
453 
454 	ifv = ifp->if_softc;
455 	p = ifv->ifv_p;
456 
457 	ifp->if_flags |= IFF_OACTIVE;
458 	for (;;) {
459 		IF_DEQUEUE(&ifp->if_snd, m);
460 		if (m == 0)
461 			break;
462 		BPF_MTAP(ifp, m);
463 
464 		/*
465 		 * Do not run parent's if_start() if the parent is not up,
466 		 * or parent's driver will cause a system crash.
467 		 */
468 		if ((p->if_flags & (IFF_UP | IFF_RUNNING)) !=
469 					(IFF_UP | IFF_RUNNING)) {
470 			m_freem(m);
471 			ifp->if_collisions++;
472 			continue;
473 		}
474 
475 		/*
476 		 * If underlying interface can do VLAN tag insertion itself,
477 		 * just pass the packet along. However, we need some way to
478 		 * tell the interface where the packet came from so that it
479 		 * knows how to find the VLAN tag to use, so we attach a
480 		 * packet tag that holds it.
481 		 */
482 		if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
483 			struct m_tag *mtag = m_tag_alloc(MTAG_VLAN,
484 							 MTAG_VLAN_TAG,
485 							 sizeof(u_int),
486 							 M_NOWAIT);
487 			if (mtag == NULL) {
488 				ifp->if_oerrors++;
489 				m_freem(m);
490 				continue;
491 			}
492 			*(u_int*)(mtag + 1) = ifv->ifv_tag;
493 			m_tag_prepend(m, mtag);
494 		} else {
495 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
496 			if (m == NULL) {
497 				if_printf(ifp,
498 				    "unable to prepend VLAN header\n");
499 				ifp->if_oerrors++;
500 				continue;
501 			}
502 			/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
503 
504 			if (m->m_len < sizeof(*evl)) {
505 				m = m_pullup(m, sizeof(*evl));
506 				if (m == NULL) {
507 					if_printf(ifp,
508 					    "cannot pullup VLAN header\n");
509 					ifp->if_oerrors++;
510 					continue;
511 				}
512 			}
513 
514 			/*
515 			 * Transform the Ethernet header into an Ethernet header
516 			 * with 802.1Q encapsulation.
517 			 */
518 			bcopy(mtod(m, char *) + ifv->ifv_encaplen,
519 			      mtod(m, char *), ETHER_HDR_LEN);
520 			evl = mtod(m, struct ether_vlan_header *);
521 			evl->evl_proto = evl->evl_encap_proto;
522 			evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
523 			evl->evl_tag = htons(ifv->ifv_tag);
524 #ifdef DEBUG
525 			printf("vlan_start: %*D\n", (int)sizeof(*evl),
526 			    (unsigned char *)evl, ":");
527 #endif
528 		}
529 
530 		/*
531 		 * Send it, precisely as ether_output() would have.
532 		 * We are already running at splimp.
533 		 */
534 		IFQ_HANDOFF(p, m, error);
535 		if (!error)
536 			ifp->if_opackets++;
537 		else
538 			ifp->if_oerrors++;
539 	}
540 	ifp->if_flags &= ~IFF_OACTIVE;
541 }
542 
543 static void
544 vlan_input(struct ifnet *ifp, struct mbuf *m)
545 {
546 	struct ether_vlan_header *evl;
547 	struct ifvlan *ifv;
548 	struct m_tag *mtag;
549 	u_int tag;
550 
551 	mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
552 	if (mtag != NULL) {
553 		/*
554 		 * Packet is tagged, m contains a normal
555 		 * Ethernet frame; the tag is stored out-of-band.
556 		 */
557 		tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag));
558 		m_tag_delete(m, mtag);
559 	} else {
560 		switch (ifp->if_type) {
561 		case IFT_ETHER:
562 			if (m->m_len < sizeof(*evl) &&
563 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
564 				if_printf(ifp, "cannot pullup VLAN header\n");
565 				return;
566 			}
567 			evl = mtod(m, struct ether_vlan_header *);
568 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN,
569 				("vlan_input: bad encapsulated protocols (%u)",
570 				 ntohs(evl->evl_encap_proto)));
571 
572 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
573 
574 			/*
575 			 * Restore the original ethertype.  We'll remove
576 			 * the encapsulation after we've found the vlan
577 			 * interface corresponding to the tag.
578 			 */
579 			evl->evl_encap_proto = evl->evl_proto;
580 			break;
581 		default:
582 			tag = (u_int) -1;
583 #ifdef DIAGNOSTIC
584 			panic("vlan_input: unsupported if type %u",
585 			    ifp->if_type);
586 #endif
587 			break;
588 		}
589 	}
590 
591 	VLAN_LOCK();
592 	LIST_FOREACH(ifv, &ifv_list, ifv_list)
593 		if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
594 			break;
595 
596 	if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
597 		VLAN_UNLOCK();
598 		m_freem(m);
599 		ifp->if_noproto++;
600 #ifdef DEBUG
601 		printf("vlan_input: tag %d, no interface\n", tag);
602 #endif
603 		return;
604 	}
605 	VLAN_UNLOCK();		/* XXX extend below? */
606 #ifdef DEBUG
607 	printf("vlan_input: tag %d, parent %s\n", tag, ifv->ifv_p->if_xname);
608 #endif
609 
610 	if (mtag == NULL) {
611 		/*
612 		 * Packet had an in-line encapsulation header;
613 		 * remove it.  The original header has already
614 		 * been fixed up above.
615 		 */
616 		bcopy(mtod(m, caddr_t),
617 		      mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
618 		      ETHER_HDR_LEN);
619 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
620 	}
621 
622 	m->m_pkthdr.rcvif = &ifv->ifv_if;
623 	ifv->ifv_if.if_ipackets++;
624 
625 	/* Pass it back through the parent's input routine. */
626 	(*ifp->if_input)(&ifv->ifv_if, m);
627 }
628 
629 static int
630 vlan_config(struct ifvlan *ifv, struct ifnet *p)
631 {
632 	struct ifaddr *ifa1, *ifa2;
633 	struct sockaddr_dl *sdl1, *sdl2;
634 
635 	VLAN_LOCK_ASSERT();
636 
637 	if (p->if_data.ifi_type != IFT_ETHER)
638 		return (EPROTONOSUPPORT);
639 	if (ifv->ifv_p)
640 		return (EBUSY);
641 
642 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
643 	ifv->ifv_mintu = ETHERMIN;
644 	ifv->ifv_flags = 0;
645 
646 	/*
647 	 * If the parent supports the VLAN_MTU capability,
648 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
649 	 * use it.
650 	 * First of all, enable Tx/Rx of such extended frames on the
651 	 * parent if it's disabled and we're the first to attach.
652 	 */
653 	p->if_nvlans++;
654 	if (p->if_nvlans == 1 &&
655 	    (p->if_capabilities & IFCAP_VLAN_MTU) &&
656 	    (p->if_capenable & IFCAP_VLAN_MTU) == 0) {
657 		struct ifreq ifr;
658 		int error;
659 
660 		ifr.ifr_reqcap = p->if_capenable | IFCAP_VLAN_MTU;
661 		error = (*p->if_ioctl)(p, SIOCSIFCAP, (caddr_t) &ifr);
662 		if (error) {
663 			p->if_nvlans--;
664 			return (error);
665 		}
666 	}
667 	if (p->if_capenable & IFCAP_VLAN_MTU) {
668 		/*
669 		 * No need to fudge the MTU since the parent can
670 		 * handle extended frames.
671 		 */
672 		ifv->ifv_mtufudge = 0;
673 	} else {
674 		/*
675 		 * Fudge the MTU by the encapsulation size.  This
676 		 * makes us incompatible with strictly compliant
677 		 * 802.1Q implementations, but allows us to use
678 		 * the feature with other NetBSD implementations,
679 		 * which might still be useful.
680 		 */
681 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
682 	}
683 
684 	ifv->ifv_p = p;
685 	ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
686 	/*
687 	 * Copy only a selected subset of flags from the parent.
688 	 * Other flags are none of our business.
689 	 */
690 	ifv->ifv_if.if_flags = (p->if_flags &
691 	    (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT));
692 	ifv->ifv_if.if_link_state = p->if_link_state;
693 
694 #if 0
695 	/*
696 	 * Not ready yet.  We need notification from the parent
697 	 * when hw checksumming flags in its if_capenable change.
698 	 * Flags set in if_capabilities only are useless.
699 	 */
700 	/*
701 	 * If the parent interface can do hardware-assisted
702 	 * VLAN encapsulation, then propagate its hardware-
703 	 * assisted checksumming flags.
704 	 */
705 	if (p->if_capabilities & IFCAP_VLAN_HWTAGGING)
706 		ifv->ifv_if.if_capabilities |= p->if_capabilities & IFCAP_HWCSUM;
707 #endif
708 
709 	/*
710 	 * Set up our ``Ethernet address'' to reflect the underlying
711 	 * physical interface's.
712 	 */
713 	ifa1 = ifaddr_byindex(ifv->ifv_if.if_index);
714 	ifa2 = ifaddr_byindex(p->if_index);
715 	sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
716 	sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
717 	sdl1->sdl_type = IFT_ETHER;
718 	sdl1->sdl_alen = ETHER_ADDR_LEN;
719 	bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
720 	bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
721 
722 	/*
723 	 * Configure multicast addresses that may already be
724 	 * joined on the vlan device.
725 	 */
726 	(void)vlan_setmulti(&ifv->ifv_if); /* XXX: VLAN lock held */
727 
728 	return (0);
729 }
730 
731 static int
732 vlan_unconfig(struct ifnet *ifp)
733 {
734 	struct ifaddr *ifa;
735 	struct sockaddr_dl *sdl;
736 	struct vlan_mc_entry *mc;
737 	struct ifvlan *ifv;
738 	struct ifnet *p;
739 	int error;
740 
741 	VLAN_LOCK_ASSERT();
742 
743 	ifv = ifp->if_softc;
744 	p = ifv->ifv_p;
745 
746 	if (p) {
747 		struct sockaddr_dl sdl;
748 
749 		/*
750 		 * Since the interface is being unconfigured, we need to
751 		 * empty the list of multicast groups that we may have joined
752 		 * while we were alive from the parent's list.
753 		 */
754 		bzero((char *)&sdl, sizeof(sdl));
755 		sdl.sdl_len = sizeof(sdl);
756 		sdl.sdl_family = AF_LINK;
757 		sdl.sdl_index = p->if_index;
758 		sdl.sdl_type = IFT_ETHER;
759 		sdl.sdl_alen = ETHER_ADDR_LEN;
760 
761 		while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
762 			mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
763 			bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
764 			    ETHER_ADDR_LEN);
765 			error = if_delmulti(p, (struct sockaddr *)&sdl);
766 			if (error)
767 				return (error);
768 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
769 			free(mc, M_VLAN);
770 		}
771 
772 		p->if_nvlans--;
773 		if (p->if_nvlans == 0) {
774 			struct ifreq ifr;
775 
776 			/*
777 			 * Try to disable Tx/Rx of VLAN-sized frames.
778 			 * This may have no effect for some interfaces,
779 			 * but only the parent driver knows that.
780 			 */
781 			ifr.ifr_reqcap = p->if_capenable & ~IFCAP_VLAN_MTU;
782 			(*p->if_ioctl)(p, SIOCSIFCAP, (caddr_t) &ifr);
783 		}
784 	}
785 
786 	/* Disconnect from parent. */
787 	ifv->ifv_p = NULL;
788 	ifv->ifv_if.if_mtu = ETHERMTU;		/* XXX why not 0? */
789 	ifv->ifv_flags = 0;
790 	ifv->ifv_if.if_link_state = LINK_STATE_UNKNOWN;
791 
792 	/* Clear our MAC address. */
793 	ifa = ifaddr_byindex(ifv->ifv_if.if_index);
794 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
795 	sdl->sdl_type = IFT_ETHER;
796 	sdl->sdl_alen = ETHER_ADDR_LEN;
797 	bzero(LLADDR(sdl), ETHER_ADDR_LEN);
798 	bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
799 
800 	return (0);
801 }
802 
803 static int
804 vlan_set_promisc(struct ifnet *ifp)
805 {
806 	struct ifvlan *ifv = ifp->if_softc;
807 	int error = 0;
808 
809 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
810 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
811 			error = ifpromisc(ifv->ifv_p, 1);
812 			if (error == 0)
813 				ifv->ifv_flags |= IFVF_PROMISC;
814 		}
815 	} else {
816 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
817 			error = ifpromisc(ifv->ifv_p, 0);
818 			if (error == 0)
819 				ifv->ifv_flags &= ~IFVF_PROMISC;
820 		}
821 	}
822 
823 	return (error);
824 }
825 
826 /* Inform all vlans that their parent has changed link state */
827 static void
828 vlan_link_state(struct ifnet *ifp, int link)
829 {
830 	struct ifvlan *ifv;
831 
832 	VLAN_LOCK();
833 	LIST_FOREACH(ifv, &ifv_list, ifv_list) {
834 		if (ifv->ifv_p == ifp) {
835 			ifv->ifv_if.if_link_state = ifv->ifv_p->if_link_state;
836 			rt_ifmsg(&(ifv->ifv_if));
837 			KNOTE(&ifp->if_klist, link);
838 		}
839 	}
840 	VLAN_UNLOCK();
841 }
842 
843 static int
844 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
845 {
846 	struct ifaddr *ifa;
847 	struct ifnet *p;
848 	struct ifreq *ifr;
849 	struct ifvlan *ifv;
850 	struct vlanreq vlr;
851 	int error = 0;
852 
853 	ifr = (struct ifreq *)data;
854 	ifa = (struct ifaddr *)data;
855 	ifv = ifp->if_softc;
856 
857 	switch (cmd) {
858 	case SIOCSIFADDR:
859 		ifp->if_flags |= IFF_UP;
860 
861 		switch (ifa->ifa_addr->sa_family) {
862 #ifdef INET
863 		case AF_INET:
864 			arp_ifinit(&ifv->ifv_if, ifa);
865 			break;
866 #endif
867 		default:
868 			break;
869 		}
870 		break;
871 
872 	case SIOCGIFADDR:
873 		{
874 			struct sockaddr *sa;
875 
876 			sa = (struct sockaddr *) &ifr->ifr_data;
877 			bcopy(IFP2AC(ifp)->ac_enaddr, (caddr_t)sa->sa_data,
878 			    ETHER_ADDR_LEN);
879 		}
880 		break;
881 
882 	case SIOCGIFMEDIA:
883 		VLAN_LOCK();
884 		if (ifv->ifv_p != NULL) {
885 			error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
886 					SIOCGIFMEDIA, data);
887 			VLAN_UNLOCK();
888 			/* Limit the result to the parent's current config. */
889 			if (error == 0) {
890 				struct ifmediareq *ifmr;
891 
892 				ifmr = (struct ifmediareq *)data;
893 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
894 					ifmr->ifm_count = 1;
895 					error = copyout(&ifmr->ifm_current,
896 						ifmr->ifm_ulist,
897 						sizeof(int));
898 				}
899 			}
900 		} else {
901 			VLAN_UNLOCK();
902 			error = EINVAL;
903 		}
904 		break;
905 
906 	case SIOCSIFMEDIA:
907 		error = EINVAL;
908 		break;
909 
910 	case SIOCSIFMTU:
911 		/*
912 		 * Set the interface MTU.
913 		 */
914 		VLAN_LOCK();
915 		if (ifv->ifv_p != NULL) {
916 			if (ifr->ifr_mtu >
917 			     (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
918 			    ifr->ifr_mtu <
919 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
920 				error = EINVAL;
921 			else
922 				ifp->if_mtu = ifr->ifr_mtu;
923 		} else
924 			error = EINVAL;
925 		VLAN_UNLOCK();
926 		break;
927 
928 	case SIOCSETVLAN:
929 		error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
930 		if (error)
931 			break;
932 		if (vlr.vlr_parent[0] == '\0') {
933 			VLAN_LOCK();
934 			vlan_unconfig(ifp);
935 			if (ifp->if_flags & IFF_UP)
936 				if_down(ifp);
937 			ifp->if_flags &= ~IFF_RUNNING;
938 			VLAN_UNLOCK();
939 			break;
940 		}
941 		p = ifunit(vlr.vlr_parent);
942 		if (p == 0) {
943 			error = ENOENT;
944 			break;
945 		}
946 		/*
947 		 * Don't let the caller set up a VLAN tag with
948 		 * anything except VLID bits.
949 		 */
950 		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
951 			error = EINVAL;
952 			break;
953 		}
954 		VLAN_LOCK();
955 		error = vlan_config(ifv, p);
956 		if (error) {
957 			VLAN_UNLOCK();
958 			break;
959 		}
960 		ifv->ifv_tag = vlr.vlr_tag;
961 		ifp->if_flags |= IFF_RUNNING;
962 		VLAN_UNLOCK();
963 
964 		/* Update promiscuous mode, if necessary. */
965 		vlan_set_promisc(ifp);
966 		break;
967 
968 	case SIOCGETVLAN:
969 		bzero(&vlr, sizeof(vlr));
970 		VLAN_LOCK();
971 		if (ifv->ifv_p) {
972 			strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
973 			    sizeof(vlr.vlr_parent));
974 			vlr.vlr_tag = ifv->ifv_tag;
975 		}
976 		VLAN_UNLOCK();
977 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
978 		break;
979 
980 	case SIOCSIFFLAGS:
981 		/*
982 		 * For promiscuous mode, we enable promiscuous mode on
983 		 * the parent if we need promiscuous on the VLAN interface.
984 		 */
985 		if (ifv->ifv_p != NULL)
986 			error = vlan_set_promisc(ifp);
987 		break;
988 
989 	case SIOCADDMULTI:
990 	case SIOCDELMULTI:
991 		/*VLAN_LOCK();*/
992 		error = vlan_setmulti(ifp);
993 		/*VLAN_UNLOCK();*/
994 		break;
995 	default:
996 		error = EINVAL;
997 	}
998 
999 	return (error);
1000 }
1001