2 * Copyright 1998 Massachusetts Institute of Technology
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
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
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.
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.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>
58 #include <net/ethernet.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>
67 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
71 #define VLANNAME "vlan"
73 struct vlan_mc_entry {
74 struct ether_addr mc_addr;
75 SLIST_ENTRY(vlan_mc_entry) mc_entries;
79 struct ifnet *ifv_ifp;
80 struct ifnet *ifv_p; /* parent inteface of this vlan */
81 int ifv_pflags; /* special flags we have set on 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 */
90 SLIST_HEAD(__vlan_mchead, vlan_mc_entry) vlan_mc_listhead;
91 LIST_ENTRY(ifvlan) ifv_list;
93 #define ifv_tag ifv_mib.ifvm_tag
94 #define ifv_encaplen ifv_mib.ifvm_encaplen
95 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
96 #define ifv_mintu ifv_mib.ifvm_mintu
98 /* Special flags we should propagate to parent */
101 int (*func)(struct ifnet *, int);
103 {IFF_PROMISC, ifpromisc},
104 {IFF_ALLMULTI, if_allmulti},
108 SYSCTL_DECL(_net_link);
109 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
110 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
112 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
113 static LIST_HEAD(, ifvlan) ifv_list;
116 * Locking: one lock is used to guard both the ifv_list and modification
117 * to vlan data structures. We are rather conservative here; probably
118 * more than necessary.
120 static struct mtx ifv_mtx;
121 #define VLAN_LOCK_INIT() mtx_init(&ifv_mtx, VLANNAME, NULL, MTX_DEF)
122 #define VLAN_LOCK_DESTROY() mtx_destroy(&ifv_mtx)
123 #define VLAN_LOCK_ASSERT() mtx_assert(&ifv_mtx, MA_OWNED)
124 #define VLAN_LOCK() mtx_lock(&ifv_mtx)
125 #define VLAN_UNLOCK() mtx_unlock(&ifv_mtx)
127 static void vlan_start(struct ifnet *ifp);
128 static void vlan_ifinit(void *foo);
129 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
130 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
131 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
132 int (*func)(struct ifnet *, int));
133 static int vlan_setflags(struct ifnet *ifp, int status);
134 static int vlan_setmulti(struct ifnet *ifp);
135 static int vlan_unconfig(struct ifnet *ifp);
136 static int vlan_config(struct ifvlan *ifv, struct ifnet *p);
137 static void vlan_link_state(struct ifnet *ifp, int link);
139 static struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
140 const char *, int *);
141 static int vlan_clone_match(struct if_clone *, const char *);
142 static int vlan_clone_create(struct if_clone *, char *, size_t);
143 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
145 static struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
146 IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
149 * Program our multicast filter. What we're actually doing is
150 * programming the multicast filter of the parent. This has the
151 * side effect of causing the parent interface to receive multicast
152 * traffic that it doesn't really want, which ends up being discarded
153 * later by the upper protocol layers. Unfortunately, there's no way
154 * to avoid this: there really is only one physical interface.
156 * XXX: There is a possible race here if more than one thread is
157 * modifying the multicast state of the vlan interface at the same time.
160 vlan_setmulti(struct ifnet *ifp)
163 struct ifmultiaddr *ifma, *rifma = NULL;
165 struct vlan_mc_entry *mc = NULL;
166 struct sockaddr_dl sdl;
169 /*VLAN_LOCK_ASSERT();*/
171 /* Find the parent. */
176 * If we don't have a parent, just remember the membership for
182 bzero((char *)&sdl, sizeof(sdl));
183 sdl.sdl_len = sizeof(sdl);
184 sdl.sdl_family = AF_LINK;
185 sdl.sdl_index = ifp_p->if_index;
186 sdl.sdl_type = IFT_ETHER;
187 sdl.sdl_alen = ETHER_ADDR_LEN;
189 /* First, remove any existing filter entries. */
190 while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
191 mc = SLIST_FIRST(&sc->vlan_mc_listhead);
192 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
193 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
196 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
200 /* Now program new ones. */
201 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
202 if (ifma->ifma_addr->sa_family != AF_LINK)
204 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
207 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
208 (char *)&mc->mc_addr, ETHER_ADDR_LEN);
209 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
210 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
211 LLADDR(&sdl), ETHER_ADDR_LEN);
212 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
221 * VLAN support can be loaded as a module. The only place in the
222 * system that's intimately aware of this is ether_input. We hook
223 * into this code through vlan_input_p which is defined there and
224 * set here. Noone else in the system should be aware of this so
225 * we use an explicit reference here.
227 * NB: Noone should ever need to check if vlan_input_p is null or
228 * not. This is because interfaces have a count of the number
229 * of active vlans (if_nvlans) and this should never be bumped
230 * except by vlan_config--which is in this module so therefore
231 * the module must be loaded and vlan_input_p must be non-NULL.
233 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
235 /* For if_link_state_change() eyes only... */
236 extern void (*vlan_link_state_p)(struct ifnet *, int);
239 vlan_modevent(module_t mod, int type, void *data)
244 LIST_INIT(&ifv_list);
246 vlan_input_p = vlan_input;
247 vlan_link_state_p = vlan_link_state;
248 if_clone_attach(&vlan_cloner);
251 if_clone_detach(&vlan_cloner);
253 vlan_link_state_p = NULL;
254 while (!LIST_EMPTY(&ifv_list))
255 vlan_clone_destroy(&vlan_cloner,
256 LIST_FIRST(&ifv_list)->ifv_ifp);
265 static moduledata_t vlan_mod = {
271 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
272 MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
274 static struct ifnet *
275 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
281 /* Check for <etherif>.<vlan> style interface names. */
283 TAILQ_FOREACH(ifp, &ifnet, if_link) {
284 if (ifp->if_type != IFT_ETHER)
286 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
288 cp = name + strlen(ifp->if_xname);
291 for(; *cp != '\0'; cp++) {
292 if (*cp < '0' || *cp > '9')
294 t = (t * 10) + (*cp - '0');
306 vlan_clone_match(struct if_clone *ifc, const char *name)
310 if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
313 if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
315 for (cp = name + 4; *cp != '\0'; cp++) {
316 if (*cp < '0' || *cp > '9')
324 vlan_clone_create(struct if_clone *ifc, char *name, size_t len)
335 u_char eaddr[6] = {0,0,0,0,0,0};
337 if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
343 * Don't let the caller set up a VLAN tag with
344 * anything except VLID bits.
346 if (tag & ~EVL_VLID_MASK)
351 error = ifc_name2unit(name, &unit);
355 wildcard = (unit < 0);
358 error = ifc_alloc_unit(ifc, &unit);
362 /* In the wildcard case, we need to update the name. */
364 for (dp = name; *dp != '\0'; dp++);
365 if (snprintf(dp, len - (dp-name), "%d", unit) >
366 len - (dp-name) - 1) {
367 panic("%s: interface name too long", __func__);
371 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
372 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
374 ifc_free_unit(ifc, unit);
378 SLIST_INIT(&ifv->vlan_mc_listhead);
382 * Set the name manually rather than using if_initname because
383 * we don't conform to the default naming convention for interfaces.
385 strlcpy(ifp->if_xname, name, IFNAMSIZ);
386 ifp->if_dname = ifc->ifc_name;
387 ifp->if_dunit = unit;
388 /* NB: flags are not set here */
389 ifp->if_linkmib = &ifv->ifv_mib;
390 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
391 /* NB: mtu is not set here */
393 ifp->if_init = vlan_ifinit;
394 ifp->if_start = vlan_start;
395 ifp->if_ioctl = vlan_ioctl;
396 ifp->if_snd.ifq_maxlen = ifqmaxlen;
397 ether_ifattach(ifp, eaddr);
398 /* Now undo some of the damage... */
399 ifp->if_baudrate = 0;
400 ifp->if_type = IFT_L2VLAN;
401 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
404 LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
409 error = vlan_config(ifv, p);
412 * Since we've partialy failed, we need to back
413 * out all the way, otherwise userland could get
414 * confused. Thus, we destroy the interface.
416 LIST_REMOVE(ifv, ifv_list);
420 if_free_type(ifp, IFT_ETHER);
426 ifp->if_drv_flags |= IFF_DRV_RUNNING;
429 /* Update flags on the parent, if necessary. */
430 vlan_setflags(ifp, 1);
437 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
440 struct ifvlan *ifv = ifp->if_softc;
442 unit = ifp->if_dunit;
445 LIST_REMOVE(ifv, ifv_list);
450 if_free_type(ifp, IFT_ETHER);
454 ifc_free_unit(ifc, unit);
460 * The ifp->if_init entry point for vlan(4) is a no-op.
463 vlan_ifinit(void *foo)
469 vlan_start(struct ifnet *ifp)
473 struct ether_vlan_header *evl;
480 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
482 IF_DEQUEUE(&ifp->if_snd, m);
488 * Do not run parent's if_start() if the parent is not up,
489 * or parent's driver will cause a system crash.
491 if (!((p->if_flags & IFF_UP) &&
492 (p->if_drv_flags & IFF_DRV_RUNNING))) {
494 ifp->if_collisions++;
499 * If underlying interface can do VLAN tag insertion itself,
500 * just pass the packet along. However, we need some way to
501 * tell the interface where the packet came from so that it
502 * knows how to find the VLAN tag to use, so we attach a
503 * packet tag that holds it.
505 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
506 struct m_tag *mtag = m_tag_alloc(MTAG_VLAN,
515 VLAN_TAG_VALUE(mtag) = ifv->ifv_tag;
516 m_tag_prepend(m, mtag);
517 m->m_flags |= M_VLANTAG;
519 M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
522 "unable to prepend VLAN header\n");
526 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
528 if (m->m_len < sizeof(*evl)) {
529 m = m_pullup(m, sizeof(*evl));
532 "cannot pullup VLAN header\n");
539 * Transform the Ethernet header into an Ethernet header
540 * with 802.1Q encapsulation.
542 bcopy(mtod(m, char *) + ifv->ifv_encaplen,
543 mtod(m, char *), ETHER_HDR_LEN);
544 evl = mtod(m, struct ether_vlan_header *);
545 evl->evl_proto = evl->evl_encap_proto;
546 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
547 evl->evl_tag = htons(ifv->ifv_tag);
549 printf("%s: %*D\n", __func__, (int)sizeof(*evl),
550 (unsigned char *)evl, ":");
555 * Send it, precisely as ether_output() would have.
556 * We are already running at splimp.
558 IFQ_HANDOFF(p, m, error);
564 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
568 vlan_input(struct ifnet *ifp, struct mbuf *m)
570 struct ether_vlan_header *evl;
575 if (m->m_flags & M_VLANTAG) {
577 * Packet is tagged, but m contains a normal
578 * Ethernet frame; the tag is stored out-of-band.
580 mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
581 KASSERT(mtag != NULL,
582 ("%s: M_VLANTAG without m_tag", __func__));
583 tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag));
584 m_tag_delete(m, mtag);
585 m->m_flags &= ~M_VLANTAG;
588 * Packet is tagged in-band as specified by 802.1q.
591 switch (ifp->if_type) {
593 if (m->m_len < sizeof(*evl) &&
594 (m = m_pullup(m, sizeof(*evl))) == NULL) {
595 if_printf(ifp, "cannot pullup VLAN header\n");
598 evl = mtod(m, struct ether_vlan_header *);
599 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN,
600 ("%s: bad encapsulation protocol (%u)",
601 __func__, ntohs(evl->evl_encap_proto)));
603 tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
606 * Restore the original ethertype. We'll remove
607 * the encapsulation after we've found the vlan
608 * interface corresponding to the tag.
610 evl->evl_encap_proto = evl->evl_proto;
615 panic("%s: unsupported if_type (%u)",
616 __func__, ifp->if_type);
623 LIST_FOREACH(ifv, &ifv_list, ifv_list)
624 if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
627 if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
632 printf("%s: tag %d, no interface\n", __func__, tag);
636 VLAN_UNLOCK(); /* XXX extend below? */
638 printf("%s: tag %d, parent %s\n", __func__, tag, ifv->ifv_p->if_xname);
643 * Packet had an in-line encapsulation header;
644 * remove it. The original header has already
645 * been fixed up above.
647 bcopy(mtod(m, caddr_t),
648 mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
650 m_adj(m, ETHER_VLAN_ENCAP_LEN);
653 m->m_pkthdr.rcvif = ifv->ifv_ifp;
654 ifv->ifv_ifp->if_ipackets++;
656 /* Pass it back through the parent's input routine. */
657 (*ifp->if_input)(ifv->ifv_ifp, m);
661 vlan_config(struct ifvlan *ifv, struct ifnet *p)
663 struct ifaddr *ifa1, *ifa2;
665 struct sockaddr_dl *sdl1, *sdl2;
669 if (p->if_type != IFT_ETHER)
670 return (EPROTONOSUPPORT);
674 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
675 ifv->ifv_mintu = ETHERMIN;
679 * The active VLAN counter on the parent is used
680 * at various places to see if there is a vlan(4)
681 * attached to this physical interface.
686 * If the parent supports the VLAN_MTU capability,
687 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
690 if (p->if_capenable & IFCAP_VLAN_MTU) {
692 * No need to fudge the MTU since the parent can
693 * handle extended frames.
695 ifv->ifv_mtufudge = 0;
698 * Fudge the MTU by the encapsulation size. This
699 * makes us incompatible with strictly compliant
700 * 802.1Q implementations, but allows us to use
701 * the feature with other NetBSD implementations,
702 * which might still be useful.
704 ifv->ifv_mtufudge = ifv->ifv_encaplen;
709 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
711 * Copy only a selected subset of flags from the parent.
712 * Other flags are none of our business.
714 #define VLAN_COPY_FLAGS \
715 (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT)
716 ifp->if_flags &= ~VLAN_COPY_FLAGS;
717 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
718 #undef VLAN_COPY_FLAGS
720 ifp->if_link_state = p->if_link_state;
724 * Not ready yet. We need notification from the parent
725 * when hw checksumming flags in its if_capenable change.
726 * Flags set in if_capabilities only are useless.
729 * If the parent interface can do hardware-assisted
730 * VLAN encapsulation, then propagate its hardware-
731 * assisted checksumming flags.
733 if (p->if_capabilities & IFCAP_VLAN_HWTAGGING)
734 ifp->if_capabilities |= p->if_capabilities & IFCAP_HWCSUM;
738 * Set up our ``Ethernet address'' to reflect the underlying
739 * physical interface's.
741 ifa1 = ifaddr_byindex(ifp->if_index);
742 ifa2 = ifaddr_byindex(p->if_index);
743 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
744 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
745 sdl1->sdl_type = IFT_ETHER;
746 sdl1->sdl_alen = ETHER_ADDR_LEN;
747 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
748 bcopy(LLADDR(sdl2), IFP2ENADDR(ifp), ETHER_ADDR_LEN);
751 * Configure multicast addresses that may already be
752 * joined on the vlan device.
754 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
760 vlan_unconfig(struct ifnet *ifp)
763 struct sockaddr_dl *sdl;
764 struct vlan_mc_entry *mc;
775 struct sockaddr_dl sdl;
778 * Since the interface is being unconfigured, we need to
779 * empty the list of multicast groups that we may have joined
780 * while we were alive from the parent's list.
782 bzero((char *)&sdl, sizeof(sdl));
783 sdl.sdl_len = sizeof(sdl);
784 sdl.sdl_family = AF_LINK;
785 sdl.sdl_index = p->if_index;
786 sdl.sdl_type = IFT_ETHER;
787 sdl.sdl_alen = ETHER_ADDR_LEN;
789 while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
790 mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
791 bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
793 error = if_delmulti(p, (struct sockaddr *)&sdl);
796 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
800 vlan_setflags(ifp, 0); /* clear special flags on parent */
804 /* Disconnect from parent. */
806 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
808 ifv->ifv_ifp->if_mtu = ETHERMTU; /* XXX why not 0? */
809 ifv->ifv_ifp->if_link_state = LINK_STATE_UNKNOWN;
811 /* Clear our MAC address. */
812 ifa = ifaddr_byindex(ifv->ifv_ifp->if_index);
813 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
814 sdl->sdl_type = IFT_ETHER;
815 sdl->sdl_alen = ETHER_ADDR_LEN;
816 bzero(LLADDR(sdl), ETHER_ADDR_LEN);
817 bzero(IFP2ENADDR(ifv->ifv_ifp), ETHER_ADDR_LEN);
822 /* Handle a reference counted flag that should be set on the parent as well */
824 vlan_setflag(struct ifnet *ifp, int flag, int status,
825 int (*func)(struct ifnet *, int))
830 /* XXX VLAN_LOCK_ASSERT(); */
833 status = status ? (ifp->if_flags & flag) : 0;
834 /* Now "status" contains the flag value or 0 */
837 * See if recorded parent's status is different from what
838 * we want it to be. If it is, flip it. We record parent's
839 * status in ifv_pflags so that we won't clear parent's flag
840 * we haven't set. In fact, we don't clear or set parent's
841 * flags directly, but get or release references to them.
842 * That's why we can be sure that recorded flags still are
843 * in accord with actual parent's flags.
845 if (status != (ifv->ifv_pflags & flag)) {
846 error = (*func)(ifv->ifv_p, status);
849 ifv->ifv_pflags &= ~flag;
850 ifv->ifv_pflags |= status;
856 * Handle IFF_* flags that require certain changes on the parent:
857 * if "status" is true, update parent's flags respective to our if_flags;
858 * if "status" is false, forcedly clear the flags set on parent.
861 vlan_setflags(struct ifnet *ifp, int status)
865 for (i = 0; vlan_pflags[i].flag; i++) {
866 error = vlan_setflag(ifp, vlan_pflags[i].flag,
867 status, vlan_pflags[i].func);
874 /* Inform all vlans that their parent has changed link state */
876 vlan_link_state(struct ifnet *ifp, int link)
881 LIST_FOREACH(ifv, &ifv_list, ifv_list) {
882 if (ifv->ifv_p == ifp)
883 if_link_state_change(ifv->ifv_ifp,
884 ifv->ifv_p->if_link_state);
890 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
899 ifr = (struct ifreq *)data;
900 ifa = (struct ifaddr *)data;
905 ifp->if_flags |= IFF_UP;
907 switch (ifa->ifa_addr->sa_family) {
910 arp_ifinit(ifv->ifv_ifp, ifa);
922 sa = (struct sockaddr *) &ifr->ifr_data;
923 bcopy(IFP2ENADDR(ifp), (caddr_t)sa->sa_data,
930 if (ifv->ifv_p != NULL) {
931 error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
934 /* Limit the result to the parent's current config. */
936 struct ifmediareq *ifmr;
938 ifmr = (struct ifmediareq *)data;
939 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
941 error = copyout(&ifmr->ifm_current,
958 * Set the interface MTU.
961 if (ifv->ifv_p != NULL) {
963 (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
965 (ifv->ifv_mintu - ifv->ifv_mtufudge))
968 ifp->if_mtu = ifr->ifr_mtu;
975 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
978 if (vlr.vlr_parent[0] == '\0') {
981 if (ifp->if_flags & IFF_UP)
983 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
987 p = ifunit(vlr.vlr_parent);
993 * Don't let the caller set up a VLAN tag with
994 * anything except VLID bits.
996 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1001 error = vlan_config(ifv, p);
1006 ifv->ifv_tag = vlr.vlr_tag;
1007 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1010 /* Update flags on the parent, if necessary. */
1011 vlan_setflags(ifp, 1);
1015 bzero(&vlr, sizeof(vlr));
1018 strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
1019 sizeof(vlr.vlr_parent));
1020 vlr.vlr_tag = ifv->ifv_tag;
1023 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1028 * We should propagate selected flags to the parent,
1029 * e.g., promiscuous mode.
1031 if (ifv->ifv_p != NULL)
1032 error = vlan_setflags(ifp, 1);
1038 error = vlan_setmulti(ifp);