xref: /netbsd-src/sys/net/if_vlan.c (revision a24efa7dea9f1f56c3bdb15a927d3516792ace1c)
1 /*	$NetBSD: if_vlan.c,v 1.88 2016/05/09 15:05:15 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran, and by Jason R. Thorpe of Zembu Labs, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright 1998 Massachusetts Institute of Technology
34  *
35  * Permission to use, copy, modify, and distribute this software and
36  * its documentation for any purpose and without fee is hereby
37  * granted, provided that both the above copyright notice and this
38  * permission notice appear in all copies, that both the above
39  * copyright notice and this permission notice appear in all
40  * supporting documentation, and that the name of M.I.T. not be used
41  * in advertising or publicity pertaining to distribution of the
42  * software without specific, written prior permission.  M.I.T. makes
43  * no representations about the suitability of this software for any
44  * purpose.  It is provided "as is" without express or implied
45  * warranty.
46  *
47  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
48  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
49  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
50  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
51  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
61  * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
62  */
63 
64 /*
65  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.  Might be
66  * extended some day to also handle IEEE 802.1P priority tagging.  This is
67  * sort of sneaky in the implementation, since we need to pretend to be
68  * enough of an Ethernet implementation to make ARP work.  The way we do
69  * this is by telling everyone that we are an Ethernet interface, and then
70  * catch the packets that ether_output() left on our output queue when it
71  * calls if_start(), rewrite them for use by the real outgoing interface,
72  * and ask it to send them.
73  *
74  * TODO:
75  *
76  *	- Need some way to notify vlan interfaces when the parent
77  *	  interface changes MTU.
78  */
79 
80 #include <sys/cdefs.h>
81 __KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.88 2016/05/09 15:05:15 christos Exp $");
82 
83 #ifdef _KERNEL_OPT
84 #include "opt_inet.h"
85 #include "opt_net_mpsafe.h"
86 #endif
87 
88 #include <sys/param.h>
89 #include <sys/kernel.h>
90 #include <sys/mbuf.h>
91 #include <sys/queue.h>
92 #include <sys/socket.h>
93 #include <sys/sockio.h>
94 #include <sys/systm.h>
95 #include <sys/proc.h>
96 #include <sys/kauth.h>
97 #include <sys/mutex.h>
98 
99 #include <net/bpf.h>
100 #include <net/if.h>
101 #include <net/if_dl.h>
102 #include <net/if_types.h>
103 #include <net/if_ether.h>
104 #include <net/if_vlanvar.h>
105 
106 #ifdef INET
107 #include <netinet/in.h>
108 #include <netinet/if_inarp.h>
109 #endif
110 #ifdef INET6
111 #include <netinet6/in6_ifattach.h>
112 #endif
113 
114 #include "ioconf.h"
115 
116 struct vlan_mc_entry {
117 	LIST_ENTRY(vlan_mc_entry)	mc_entries;
118 	/*
119 	 * A key to identify this entry.  The mc_addr below can't be
120 	 * used since multiple sockaddr may mapped into the same
121 	 * ether_multi (e.g., AF_UNSPEC).
122 	 */
123 	union {
124 		struct ether_multi	*mcu_enm;
125 	} mc_u;
126 	struct sockaddr_storage		mc_addr;
127 };
128 
129 #define	mc_enm		mc_u.mcu_enm
130 
131 struct ifvlan {
132 	union {
133 		struct ethercom ifvu_ec;
134 	} ifv_u;
135 	struct ifnet *ifv_p;	/* parent interface of this vlan */
136 	struct ifv_linkmib {
137 		const struct vlan_multisw *ifvm_msw;
138 		int	ifvm_encaplen;	/* encapsulation length */
139 		int	ifvm_mtufudge;	/* MTU fudged by this much */
140 		int	ifvm_mintu;	/* min transmission unit */
141 		uint16_t ifvm_proto;	/* encapsulation ethertype */
142 		uint16_t ifvm_tag;	/* tag to apply on packets */
143 	} ifv_mib;
144 	LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
145 	LIST_ENTRY(ifvlan) ifv_list;
146 	int ifv_flags;
147 };
148 
149 #define	IFVF_PROMISC	0x01		/* promiscuous mode enabled */
150 
151 #define	ifv_ec		ifv_u.ifvu_ec
152 
153 #define	ifv_if		ifv_ec.ec_if
154 
155 #define	ifv_msw		ifv_mib.ifvm_msw
156 #define	ifv_encaplen	ifv_mib.ifvm_encaplen
157 #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
158 #define	ifv_mintu	ifv_mib.ifvm_mintu
159 #define	ifv_tag		ifv_mib.ifvm_tag
160 
161 struct vlan_multisw {
162 	int	(*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
163 	int	(*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
164 	void	(*vmsw_purgemulti)(struct ifvlan *);
165 };
166 
167 static int	vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
168 static int	vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
169 static void	vlan_ether_purgemulti(struct ifvlan *);
170 
171 const struct vlan_multisw vlan_ether_multisw = {
172 	vlan_ether_addmulti,
173 	vlan_ether_delmulti,
174 	vlan_ether_purgemulti,
175 };
176 
177 static int	vlan_clone_create(struct if_clone *, int);
178 static int	vlan_clone_destroy(struct ifnet *);
179 static int	vlan_config(struct ifvlan *, struct ifnet *);
180 static int	vlan_ioctl(struct ifnet *, u_long, void *);
181 static void	vlan_start(struct ifnet *);
182 static void	vlan_unconfig(struct ifnet *);
183 
184 /* XXX This should be a hash table with the tag as the basis of the key. */
185 static LIST_HEAD(, ifvlan) ifv_list;
186 
187 static kmutex_t ifv_mtx __cacheline_aligned;
188 
189 struct if_clone vlan_cloner =
190     IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
191 
192 /* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
193 static char vlan_zero_pad_buff[ETHER_MIN_LEN];
194 
195 void
196 vlanattach(int n)
197 {
198 
199 	LIST_INIT(&ifv_list);
200 	mutex_init(&ifv_mtx, MUTEX_DEFAULT, IPL_NONE);
201 	if_clone_attach(&vlan_cloner);
202 }
203 
204 static void
205 vlan_reset_linkname(struct ifnet *ifp)
206 {
207 
208 	/*
209 	 * We start out with a "802.1Q VLAN" type and zero-length
210 	 * addresses.  When we attach to a parent interface, we
211 	 * inherit its type, address length, address, and data link
212 	 * type.
213 	 */
214 
215 	ifp->if_type = IFT_L2VLAN;
216 	ifp->if_addrlen = 0;
217 	ifp->if_dlt = DLT_NULL;
218 	if_alloc_sadl(ifp);
219 }
220 
221 static int
222 vlan_clone_create(struct if_clone *ifc, int unit)
223 {
224 	struct ifvlan *ifv;
225 	struct ifnet *ifp;
226 	int s;
227 
228 	ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK|M_ZERO);
229 	ifp = &ifv->ifv_if;
230 	LIST_INIT(&ifv->ifv_mc_listhead);
231 
232 	s = splnet();
233 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
234 	splx(s);
235 
236 	if_initname(ifp, ifc->ifc_name, unit);
237 	ifp->if_softc = ifv;
238 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
239 	ifp->if_start = vlan_start;
240 	ifp->if_ioctl = vlan_ioctl;
241 	IFQ_SET_READY(&ifp->if_snd);
242 
243 	if_initialize(ifp);
244 	vlan_reset_linkname(ifp);
245 	if_register(ifp);
246 
247 	return (0);
248 }
249 
250 static int
251 vlan_clone_destroy(struct ifnet *ifp)
252 {
253 	struct ifvlan *ifv = ifp->if_softc;
254 	int s;
255 
256 	s = splnet();
257 	LIST_REMOVE(ifv, ifv_list);
258 	vlan_unconfig(ifp);
259 	if_detach(ifp);
260 	splx(s);
261 
262 	free(ifv, M_DEVBUF);
263 
264 	return (0);
265 }
266 
267 /*
268  * Configure a VLAN interface.  Must be called at splnet().
269  */
270 static int
271 vlan_config(struct ifvlan *ifv, struct ifnet *p)
272 {
273 	struct ifnet *ifp = &ifv->ifv_if;
274 	int error;
275 
276 	if (ifv->ifv_p != NULL)
277 		return (EBUSY);
278 
279 	switch (p->if_type) {
280 	case IFT_ETHER:
281 	    {
282 		struct ethercom *ec = (void *) p;
283 
284 		ifv->ifv_msw = &vlan_ether_multisw;
285 		ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
286 		ifv->ifv_mintu = ETHERMIN;
287 
288 		if (ec->ec_nvlans == 0) {
289 			if ((error = ether_enable_vlan_mtu(p)) >= 0) {
290 				if (error)
291 					return error;
292 				ifv->ifv_mtufudge = 0;
293 			} else {
294 				/*
295 				 * Fudge the MTU by the encapsulation size. This
296 				 * makes us incompatible with strictly compliant
297 				 * 802.1Q implementations, but allows us to use
298 				 * the feature with other NetBSD
299 				 * implementations, which might still be useful.
300 				 */
301 				ifv->ifv_mtufudge = ifv->ifv_encaplen;
302 			}
303 		}
304 		ec->ec_nvlans++;
305 
306 		/*
307 		 * If the parent interface can do hardware-assisted
308 		 * VLAN encapsulation, then propagate its hardware-
309 		 * assisted checksumming flags and tcp segmentation
310 		 * offload.
311 		 */
312 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
313 		        ec->ec_capenable |= ETHERCAP_VLAN_HWTAGGING;
314 			ifp->if_capabilities = p->if_capabilities &
315 			    (IFCAP_TSOv4 | IFCAP_TSOv6 |
316 			     IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx|
317 			     IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx|
318 			     IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx|
319 			     IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx|
320 			     IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx);
321                 }
322 		/*
323 		 * We inherit the parent's Ethernet address.
324 		 */
325 		ether_ifattach(ifp, CLLADDR(p->if_sadl));
326 		ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
327 		break;
328 	    }
329 
330 	default:
331 		return (EPROTONOSUPPORT);
332 	}
333 
334 	ifv->ifv_p = p;
335 	ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
336 	ifv->ifv_if.if_flags = p->if_flags &
337 	    (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
338 
339 	/*
340 	 * Inherit the if_type from the parent.  This allows us
341 	 * to participate in bridges of that type.
342 	 */
343 	ifv->ifv_if.if_type = p->if_type;
344 
345 	return (0);
346 }
347 
348 /*
349  * Unconfigure a VLAN interface.  Must be called at splnet().
350  */
351 static void
352 vlan_unconfig(struct ifnet *ifp)
353 {
354 	struct ifvlan *ifv = ifp->if_softc;
355 	struct ifnet *p;
356 
357 	mutex_enter(&ifv_mtx);
358 	p = ifv->ifv_p;
359 
360 	if (p == NULL) {
361 		mutex_exit(&ifv_mtx);
362 		return;
363 	}
364 
365 	/*
366  	 * Since the interface is being unconfigured, we need to empty the
367 	 * list of multicast groups that we may have joined while we were
368 	 * alive and remove them from the parent's list also.
369 	 */
370 	(*ifv->ifv_msw->vmsw_purgemulti)(ifv);
371 
372 	/* Disconnect from parent. */
373 	switch (p->if_type) {
374 	case IFT_ETHER:
375 	    {
376 		struct ethercom *ec = (void *)p;
377 		if (--ec->ec_nvlans == 0)
378 			(void)ether_disable_vlan_mtu(p);
379 
380 		ether_ifdetach(ifp);
381 		/* Restore vlan_ioctl overwritten by ether_ifdetach */
382 		ifp->if_ioctl = vlan_ioctl;
383 		vlan_reset_linkname(ifp);
384 		break;
385 	    }
386 
387 #ifdef DIAGNOSTIC
388 	default:
389 		panic("vlan_unconfig: impossible");
390 #endif
391 	}
392 
393 	ifv->ifv_p = NULL;
394 	ifv->ifv_if.if_mtu = 0;
395 	ifv->ifv_flags = 0;
396 
397 #ifdef INET6
398 	/* To delete v6 link local addresses */
399 	in6_ifdetach(ifp);
400 #endif
401 	if ((ifp->if_flags & IFF_PROMISC) != 0)
402 		ifpromisc(ifp, 0);
403 	if_down(ifp);
404 	ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
405 	ifp->if_capabilities = 0;
406 
407 	mutex_exit(&ifv_mtx);
408 }
409 
410 /*
411  * Called when a parent interface is detaching; destroy any VLAN
412  * configuration for the parent interface.
413  */
414 void
415 vlan_ifdetach(struct ifnet *p)
416 {
417 	struct ifvlan *ifv;
418 	int s;
419 
420 	s = splnet();
421 
422 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
423 	     ifv = LIST_NEXT(ifv, ifv_list)) {
424 		if (ifv->ifv_p == p)
425 			vlan_unconfig(&ifv->ifv_if);
426 	}
427 
428 	splx(s);
429 }
430 
431 static int
432 vlan_set_promisc(struct ifnet *ifp)
433 {
434 	struct ifvlan *ifv = ifp->if_softc;
435 	int error = 0;
436 
437 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
438 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
439 			error = ifpromisc(ifv->ifv_p, 1);
440 			if (error == 0)
441 				ifv->ifv_flags |= IFVF_PROMISC;
442 		}
443 	} else {
444 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
445 			error = ifpromisc(ifv->ifv_p, 0);
446 			if (error == 0)
447 				ifv->ifv_flags &= ~IFVF_PROMISC;
448 		}
449 	}
450 
451 	return (error);
452 }
453 
454 static int
455 vlan_ioctl(struct ifnet *ifp, u_long cmd, void *data)
456 {
457 	struct lwp *l = curlwp;	/* XXX */
458 	struct ifvlan *ifv = ifp->if_softc;
459 	struct ifaddr *ifa = (struct ifaddr *) data;
460 	struct ifreq *ifr = (struct ifreq *) data;
461 	struct ifnet *pr;
462 	struct ifcapreq *ifcr;
463 	struct vlanreq vlr;
464 	int s, error = 0;
465 
466 	s = splnet();
467 
468 	switch (cmd) {
469 	case SIOCSIFMTU:
470 		if (ifv->ifv_p == NULL)
471 			error = EINVAL;
472 		else if (
473 		    ifr->ifr_mtu > (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
474 		    ifr->ifr_mtu < (ifv->ifv_mintu - ifv->ifv_mtufudge))
475 			error = EINVAL;
476 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
477 			error = 0;
478 		break;
479 
480 	case SIOCSETVLAN:
481 		if ((error = kauth_authorize_network(l->l_cred,
482 		    KAUTH_NETWORK_INTERFACE,
483 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
484 		    NULL)) != 0)
485 			break;
486 		if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
487 			break;
488 		if (vlr.vlr_parent[0] == '\0') {
489 			if (ifv->ifv_p != NULL &&
490 			    (ifp->if_flags & IFF_PROMISC) != 0)
491 				error = ifpromisc(ifv->ifv_p, 0);
492 			vlan_unconfig(ifp);
493 			break;
494 		}
495 		if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
496 			error = EINVAL;		 /* check for valid tag */
497 			break;
498 		}
499 		if ((pr = ifunit(vlr.vlr_parent)) == 0) {
500 			error = ENOENT;
501 			break;
502 		}
503 		if ((error = vlan_config(ifv, pr)) != 0)
504 			break;
505 		ifv->ifv_tag = vlr.vlr_tag;
506 		ifp->if_flags |= IFF_RUNNING;
507 
508 		/* Update promiscuous mode, if necessary. */
509 		vlan_set_promisc(ifp);
510 		break;
511 
512 	case SIOCGETVLAN:
513 		memset(&vlr, 0, sizeof(vlr));
514 		if (ifv->ifv_p != NULL) {
515 			snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
516 			    ifv->ifv_p->if_xname);
517 			vlr.vlr_tag = ifv->ifv_tag;
518 		}
519 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
520 		break;
521 
522 	case SIOCSIFFLAGS:
523 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
524 			break;
525 		/*
526 		 * For promiscuous mode, we enable promiscuous mode on
527 		 * the parent if we need promiscuous on the VLAN interface.
528 		 */
529 		if (ifv->ifv_p != NULL)
530 			error = vlan_set_promisc(ifp);
531 		break;
532 
533 	case SIOCADDMULTI:
534 		error = (ifv->ifv_p != NULL) ?
535 		    (*ifv->ifv_msw->vmsw_addmulti)(ifv, ifr) : EINVAL;
536 		break;
537 
538 	case SIOCDELMULTI:
539 		error = (ifv->ifv_p != NULL) ?
540 		    (*ifv->ifv_msw->vmsw_delmulti)(ifv, ifr) : EINVAL;
541 		break;
542 
543 	case SIOCSIFCAP:
544 		ifcr = data;
545 		/* make sure caps are enabled on parent */
546 		if ((ifv->ifv_p->if_capenable & ifcr->ifcr_capenable) !=
547 		    ifcr->ifcr_capenable) {
548 			error = EINVAL;
549 			break;
550 		}
551 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
552 			error = 0;
553 		break;
554 	case SIOCINITIFADDR:
555 		if (ifv->ifv_p == NULL) {
556 			error = EINVAL;
557 			break;
558 		}
559 
560 		ifp->if_flags |= IFF_UP;
561 #ifdef INET
562 		if (ifa->ifa_addr->sa_family == AF_INET)
563 			arp_ifinit(ifp, ifa);
564 #endif
565 		break;
566 
567 	default:
568 		error = ether_ioctl(ifp, cmd, data);
569 	}
570 
571 	splx(s);
572 
573 	return (error);
574 }
575 
576 static int
577 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
578 {
579 	const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr);
580 	struct vlan_mc_entry *mc;
581 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
582 	int error;
583 
584 	if (sa->sa_len > sizeof(struct sockaddr_storage))
585 		return (EINVAL);
586 
587 	error = ether_addmulti(sa, &ifv->ifv_ec);
588 	if (error != ENETRESET)
589 		return (error);
590 
591 	/*
592 	 * This is new multicast address.  We have to tell parent
593 	 * about it.  Also, remember this multicast address so that
594 	 * we can delete them on unconfigure.
595 	 */
596 	mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT);
597 	if (mc == NULL) {
598 		error = ENOMEM;
599 		goto alloc_failed;
600 	}
601 
602 	/*
603 	 * As ether_addmulti() returns ENETRESET, following two
604 	 * statement shouldn't fail.
605 	 */
606 	(void)ether_multiaddr(sa, addrlo, addrhi);
607 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
608 	memcpy(&mc->mc_addr, sa, sa->sa_len);
609 	LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
610 
611 	error = if_mcast_op(ifv->ifv_p, SIOCADDMULTI, sa);
612 	if (error != 0)
613 		goto ioctl_failed;
614 	return (error);
615 
616  ioctl_failed:
617 	LIST_REMOVE(mc, mc_entries);
618 	free(mc, M_DEVBUF);
619  alloc_failed:
620 	(void)ether_delmulti(sa, &ifv->ifv_ec);
621 	return (error);
622 }
623 
624 static int
625 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
626 {
627 	const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr);
628 	struct ether_multi *enm;
629 	struct vlan_mc_entry *mc;
630 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
631 	int error;
632 
633 	/*
634 	 * Find a key to lookup vlan_mc_entry.  We have to do this
635 	 * before calling ether_delmulti for obvious reason.
636 	 */
637 	if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0)
638 		return (error);
639 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
640 
641 	error = ether_delmulti(sa, &ifv->ifv_ec);
642 	if (error != ENETRESET)
643 		return (error);
644 
645 	/* We no longer use this multicast address.  Tell parent so. */
646 	error = if_mcast_op(ifv->ifv_p, SIOCDELMULTI, sa);
647 	if (error == 0) {
648 		/* And forget about this address. */
649 		for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
650 		    mc = LIST_NEXT(mc, mc_entries)) {
651 			if (mc->mc_enm == enm) {
652 				LIST_REMOVE(mc, mc_entries);
653 				free(mc, M_DEVBUF);
654 				break;
655 			}
656 		}
657 		KASSERT(mc != NULL);
658 	} else
659 		(void)ether_addmulti(sa, &ifv->ifv_ec);
660 	return (error);
661 }
662 
663 /*
664  * Delete any multicast address we have asked to add from parent
665  * interface.  Called when the vlan is being unconfigured.
666  */
667 static void
668 vlan_ether_purgemulti(struct ifvlan *ifv)
669 {
670 	struct ifnet *ifp = ifv->ifv_p;		/* Parent. */
671 	struct vlan_mc_entry *mc;
672 
673 	while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
674 		(void)if_mcast_op(ifp, SIOCDELMULTI,
675 		    (const struct sockaddr *)&mc->mc_addr);
676 		LIST_REMOVE(mc, mc_entries);
677 		free(mc, M_DEVBUF);
678 	}
679 }
680 
681 static void
682 vlan_start(struct ifnet *ifp)
683 {
684 	struct ifvlan *ifv = ifp->if_softc;
685 	struct ifnet *p = ifv->ifv_p;
686 	struct ethercom *ec = (void *) ifv->ifv_p;
687 	struct mbuf *m;
688 	int error;
689 
690 #ifndef NET_MPSAFE
691 	KASSERT(KERNEL_LOCKED_P());
692 #endif
693 
694 	ifp->if_flags |= IFF_OACTIVE;
695 
696 	for (;;) {
697 		IFQ_DEQUEUE(&ifp->if_snd, m);
698 		if (m == NULL)
699 			break;
700 
701 #ifdef ALTQ
702 		/*
703 		 * If ALTQ is enabled on the parent interface, do
704 		 * classification; the queueing discipline might
705 		 * not require classification, but might require
706 		 * the address family/header pointer in the pktattr.
707 		 */
708 		if (ALTQ_IS_ENABLED(&p->if_snd)) {
709 			switch (p->if_type) {
710 			case IFT_ETHER:
711 				altq_etherclassify(&p->if_snd, m);
712 				break;
713 #ifdef DIAGNOSTIC
714 			default:
715 				panic("vlan_start: impossible (altq)");
716 #endif
717 			}
718 		}
719 #endif /* ALTQ */
720 
721 		bpf_mtap(ifp, m);
722 		/*
723 		 * If the parent can insert the tag itself, just mark
724 		 * the tag in the mbuf header.
725 		 */
726 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
727 			struct m_tag *mtag;
728 
729 			mtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
730 			    M_NOWAIT);
731 			if (mtag == NULL) {
732 				ifp->if_oerrors++;
733 				m_freem(m);
734 				continue;
735 			}
736 			*(u_int *)(mtag + 1) = ifv->ifv_tag;
737 			m_tag_prepend(m, mtag);
738 		} else {
739 			/*
740 			 * insert the tag ourselves
741 			 */
742 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
743 			if (m == NULL) {
744 				printf("%s: unable to prepend encap header",
745 				    ifv->ifv_p->if_xname);
746 				ifp->if_oerrors++;
747 				continue;
748 			}
749 
750 			switch (p->if_type) {
751 			case IFT_ETHER:
752 			    {
753 				struct ether_vlan_header *evl;
754 
755 				if (m->m_len < sizeof(struct ether_vlan_header))
756 					m = m_pullup(m,
757 					    sizeof(struct ether_vlan_header));
758 				if (m == NULL) {
759 					printf("%s: unable to pullup encap "
760 					    "header", ifv->ifv_p->if_xname);
761 					ifp->if_oerrors++;
762 					continue;
763 				}
764 
765 				/*
766 				 * Transform the Ethernet header into an
767 				 * Ethernet header with 802.1Q encapsulation.
768 				 */
769 				memmove(mtod(m, void *),
770 				    mtod(m, char *) + ifv->ifv_encaplen,
771 				    sizeof(struct ether_header));
772 				evl = mtod(m, struct ether_vlan_header *);
773 				evl->evl_proto = evl->evl_encap_proto;
774 				evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
775 				evl->evl_tag = htons(ifv->ifv_tag);
776 
777 				/*
778 				 * To cater for VLAN-aware layer 2 ethernet
779 				 * switches which may need to strip the tag
780 				 * before forwarding the packet, make sure
781 				 * the packet+tag is at least 68 bytes long.
782 				 * This is necessary because our parent will
783 				 * only pad to 64 bytes (ETHER_MIN_LEN) and
784 				 * some switches will not pad by themselves
785 				 * after deleting a tag.
786 				 */
787 				if (m->m_pkthdr.len <
788 				    (ETHER_MIN_LEN - ETHER_CRC_LEN +
789 				     ETHER_VLAN_ENCAP_LEN)) {
790 					m_copyback(m, m->m_pkthdr.len,
791 					    (ETHER_MIN_LEN - ETHER_CRC_LEN +
792 					     ETHER_VLAN_ENCAP_LEN) -
793 					     m->m_pkthdr.len,
794 					    vlan_zero_pad_buff);
795 				}
796 				break;
797 			    }
798 
799 #ifdef DIAGNOSTIC
800 			default:
801 				panic("vlan_start: impossible");
802 #endif
803 			}
804 		}
805 
806 		/*
807 		 * Send it, precisely as the parent's output routine
808 		 * would have.  We are already running at splnet.
809 		 */
810 		if ((p->if_flags & IFF_RUNNING) != 0) {
811 			error = (*p->if_transmit)(p, m);
812 			if (error) {
813 				/* mbuf is already freed */
814 				ifp->if_oerrors++;
815 				continue;
816 			}
817 		}
818 
819 		ifp->if_opackets++;
820 	}
821 
822 	ifp->if_flags &= ~IFF_OACTIVE;
823 }
824 
825 /*
826  * Given an Ethernet frame, find a valid vlan interface corresponding to the
827  * given source interface and tag, then run the real packet through the
828  * parent's input routine.
829  */
830 void
831 vlan_input(struct ifnet *ifp, struct mbuf *m)
832 {
833 	struct ifvlan *ifv;
834 	u_int tag;
835 	struct m_tag *mtag;
836 
837 	mtag = m_tag_find(m, PACKET_TAG_VLAN, NULL);
838 	if (mtag != NULL) {
839 		/* m contains a normal ethernet frame, the tag is in mtag */
840 		tag = EVL_VLANOFTAG(*(u_int *)(mtag + 1));
841 		m_tag_delete(m, mtag);
842 	} else {
843 		switch (ifp->if_type) {
844 		case IFT_ETHER:
845 		    {
846 			struct ether_vlan_header *evl;
847 
848 			if (m->m_len < sizeof(struct ether_vlan_header) &&
849 			    (m = m_pullup(m,
850 			     sizeof(struct ether_vlan_header))) == NULL) {
851 				printf("%s: no memory for VLAN header, "
852 				    "dropping packet.\n", ifp->if_xname);
853 				return;
854 			}
855 			evl = mtod(m, struct ether_vlan_header *);
856 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
857 
858 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
859 
860 			/*
861 			 * Restore the original ethertype.  We'll remove
862 			 * the encapsulation after we've found the vlan
863 			 * interface corresponding to the tag.
864 			 */
865 			evl->evl_encap_proto = evl->evl_proto;
866 			break;
867 		    }
868 
869 		default:
870 			tag = (u_int) -1;	/* XXX GCC */
871 #ifdef DIAGNOSTIC
872 			panic("vlan_input: impossible");
873 #endif
874 		}
875 	}
876 
877 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
878 	    ifv = LIST_NEXT(ifv, ifv_list))
879 		if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
880 			break;
881 
882 	if (ifv == NULL ||
883 	    (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
884 	     (IFF_UP|IFF_RUNNING)) {
885 		m_freem(m);
886 		ifp->if_noproto++;
887 		return;
888 	}
889 
890 	/*
891 	 * Now, remove the encapsulation header.  The original
892 	 * header has already been fixed up above.
893 	 */
894 	if (mtag == NULL) {
895 		memmove(mtod(m, char *) + ifv->ifv_encaplen,
896 		    mtod(m, void *), sizeof(struct ether_header));
897 		m_adj(m, ifv->ifv_encaplen);
898 	}
899 
900 	m->m_pkthdr.rcvif = &ifv->ifv_if;
901 	ifv->ifv_if.if_ipackets++;
902 
903 	bpf_mtap(&ifv->ifv_if, m);
904 
905 	m->m_flags &= ~M_PROMISC;
906 	if_input(&ifv->ifv_if, m);
907 }
908