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
31 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
32 * This is sort of sneaky in the implementation, since
33 * we need to pretend to be enough of an Ethernet implementation
34 * to make arp work. The way we do this is by telling everyone
35 * that we are an Ethernet, and then catch the packets that
36 * ether_output() sends to us via if_transmit(), rewrite them for
37 * use by the real outgoing interface, and ask it to send them.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/eventhandler.h>
48 #include <sys/kernel.h>
50 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <sys/rmlock.h>
54 #include <sys/queue.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/systm.h>
62 #include <net/ethernet.h>
64 #include <net/if_var.h>
65 #include <net/if_clone.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/if_vlan_var.h>
72 #include <netinet/in.h>
73 #include <netinet/if_ether.h>
76 #define VLAN_DEF_HWIDTH 4
77 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
79 #define UP_AND_RUNNING(ifp) \
80 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
82 LIST_HEAD(ifvlanhead, ifvlan);
85 struct ifnet *parent; /* parent interface of this trunk */
88 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
89 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
91 struct ifvlanhead *hash; /* dynamic hash-list table */
98 struct vlan_mc_entry {
99 struct sockaddr_dl mc_addr;
100 SLIST_ENTRY(vlan_mc_entry) mc_entries;
104 struct ifvlantrunk *ifv_trunk;
105 struct ifnet *ifv_ifp;
106 counter_u64_t ifv_ipackets;
107 counter_u64_t ifv_ibytes;
108 counter_u64_t ifv_opackets;
109 counter_u64_t ifv_obytes;
110 counter_u64_t ifv_omcasts;
111 counter_u64_t ifv_oerrors;
112 #define TRUNK(ifv) ((ifv)->ifv_trunk)
113 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
115 int ifv_pflags; /* special flags we have set on parent */
117 int ifvm_encaplen; /* encapsulation length */
118 int ifvm_mtufudge; /* MTU fudged by this much */
119 int ifvm_mintu; /* min transmission unit */
120 uint16_t ifvm_proto; /* encapsulation ethertype */
121 uint16_t ifvm_tag; /* tag to apply on packets leaving if */
123 SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
125 LIST_ENTRY(ifvlan) ifv_list;
128 #define ifv_proto ifv_mib.ifvm_proto
129 #define ifv_vid ifv_mib.ifvm_tag
130 #define ifv_encaplen ifv_mib.ifvm_encaplen
131 #define ifv_mtufudge ifv_mib.ifvm_mtufudge
132 #define ifv_mintu ifv_mib.ifvm_mintu
134 /* Special flags we should propagate to parent. */
137 int (*func)(struct ifnet *, int);
139 {IFF_PROMISC, ifpromisc},
140 {IFF_ALLMULTI, if_allmulti},
144 SYSCTL_DECL(_net_link);
145 static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0,
147 static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0,
150 static int soft_pad = 0;
151 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
152 "pad short frames before tagging");
154 static const char vlanname[] = "vlan";
155 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
157 static eventhandler_tag ifdetach_tag;
158 static eventhandler_tag iflladdr_tag;
161 * We have a global mutex, that is used to serialize configuration
162 * changes and isn't used in normal packet delivery.
164 * We also have a per-trunk rwlock, that is locked shared on packet
165 * processing and exclusive when configuration is changed.
167 * The VLAN_ARRAY substitutes the dynamic hash with a static array
168 * with 4096 entries. In theory this can give a boost in processing,
169 * however on practice it does not. Probably this is because array
170 * is too big to fit into CPU cache.
172 static struct sx ifv_lock;
173 #define VLAN_LOCK_INIT() sx_init(&ifv_lock, "vlan_global")
174 #define VLAN_LOCK_DESTROY() sx_destroy(&ifv_lock)
175 #define VLAN_LOCK_ASSERT() sx_assert(&ifv_lock, SA_LOCKED)
176 #define VLAN_LOCK() sx_xlock(&ifv_lock)
177 #define VLAN_UNLOCK() sx_xunlock(&ifv_lock)
178 #define TRUNK_LOCK_INIT(trunk) rm_init(&(trunk)->lock, vlanname)
179 #define TRUNK_LOCK_DESTROY(trunk) rm_destroy(&(trunk)->lock)
180 #define TRUNK_LOCK(trunk) rm_wlock(&(trunk)->lock)
181 #define TRUNK_UNLOCK(trunk) rm_wunlock(&(trunk)->lock)
182 #define TRUNK_LOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_WLOCKED)
183 #define TRUNK_RLOCK(trunk) rm_rlock(&(trunk)->lock, &tracker)
184 #define TRUNK_RUNLOCK(trunk) rm_runlock(&(trunk)->lock, &tracker)
185 #define TRUNK_LOCK_RASSERT(trunk) rm_assert(&(trunk)->lock, RA_RLOCKED)
186 #define TRUNK_LOCK_READER struct rm_priotracker tracker
189 static void vlan_inithash(struct ifvlantrunk *trunk);
190 static void vlan_freehash(struct ifvlantrunk *trunk);
191 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
192 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
193 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
194 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
197 static void trunk_destroy(struct ifvlantrunk *trunk);
199 static void vlan_init(void *foo);
200 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
201 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
202 static void vlan_qflush(struct ifnet *ifp);
203 static uint64_t vlan_get_counter(struct ifnet *ifp, ifnet_counter cnt);
204 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
205 int (*func)(struct ifnet *, int));
206 static int vlan_setflags(struct ifnet *ifp, int status);
207 static int vlan_setmulti(struct ifnet *ifp);
208 static int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
209 static void vlan_unconfig(struct ifnet *ifp);
210 static void vlan_unconfig_locked(struct ifnet *ifp, int departing);
211 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
212 static void vlan_link_state(struct ifnet *ifp);
213 static void vlan_capabilities(struct ifvlan *ifv);
214 static void vlan_trunk_capabilities(struct ifnet *ifp);
216 static struct ifnet *vlan_clone_match_ethervid(struct if_clone *,
217 const char *, int *);
218 static int vlan_clone_match(struct if_clone *, const char *);
219 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
220 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
222 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
223 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
225 static struct if_clone *vlan_cloner;
228 static VNET_DEFINE(struct if_clone *, vlan_cloner);
229 #define V_vlan_cloner VNET(vlan_cloner)
233 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
236 vlan_inithash(struct ifvlantrunk *trunk)
241 * The trunk must not be locked here since we call malloc(M_WAITOK).
242 * It is OK in case this function is called before the trunk struct
243 * gets hooked up and becomes visible from other threads.
246 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
247 ("%s: hash already initialized", __func__));
249 trunk->hwidth = VLAN_DEF_HWIDTH;
250 n = 1 << trunk->hwidth;
251 trunk->hmask = n - 1;
252 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
253 for (i = 0; i < n; i++)
254 LIST_INIT(&trunk->hash[i]);
258 vlan_freehash(struct ifvlantrunk *trunk)
263 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
264 for (i = 0; i < (1 << trunk->hwidth); i++)
265 KASSERT(LIST_EMPTY(&trunk->hash[i]),
266 ("%s: hash table not empty", __func__));
268 free(trunk->hash, M_VLAN);
270 trunk->hwidth = trunk->hmask = 0;
274 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
279 TRUNK_LOCK_ASSERT(trunk);
280 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
282 b = 1 << trunk->hwidth;
283 i = HASH(ifv->ifv_vid, trunk->hmask);
284 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
285 if (ifv->ifv_vid == ifv2->ifv_vid)
289 * Grow the hash when the number of vlans exceeds half of the number of
290 * hash buckets squared. This will make the average linked-list length
293 if (trunk->refcnt > (b * b) / 2) {
294 vlan_growhash(trunk, 1);
295 i = HASH(ifv->ifv_vid, trunk->hmask);
297 LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
304 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
309 TRUNK_LOCK_ASSERT(trunk);
310 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
312 b = 1 << trunk->hwidth;
313 i = HASH(ifv->ifv_vid, trunk->hmask);
314 LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
317 LIST_REMOVE(ifv2, ifv_list);
318 if (trunk->refcnt < (b * b) / 2)
319 vlan_growhash(trunk, -1);
323 panic("%s: vlan not found\n", __func__);
324 return (ENOENT); /*NOTREACHED*/
328 * Grow the hash larger or smaller if memory permits.
331 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
334 struct ifvlanhead *hash2;
335 int hwidth2, i, j, n, n2;
337 TRUNK_LOCK_ASSERT(trunk);
338 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
341 /* Harmless yet obvious coding error */
342 printf("%s: howmuch is 0\n", __func__);
346 hwidth2 = trunk->hwidth + howmuch;
347 n = 1 << trunk->hwidth;
349 /* Do not shrink the table below the default */
350 if (hwidth2 < VLAN_DEF_HWIDTH)
353 /* M_NOWAIT because we're called with trunk mutex held */
354 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
356 printf("%s: out of memory -- hash size not changed\n",
358 return; /* We can live with the old hash table */
360 for (j = 0; j < n2; j++)
361 LIST_INIT(&hash2[j]);
362 for (i = 0; i < n; i++)
363 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
364 LIST_REMOVE(ifv, ifv_list);
365 j = HASH(ifv->ifv_vid, n2 - 1);
366 LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
368 free(trunk->hash, M_VLAN);
370 trunk->hwidth = hwidth2;
371 trunk->hmask = n2 - 1;
374 if_printf(trunk->parent,
375 "VLAN hash table resized from %d to %d buckets\n", n, n2);
378 static __inline struct ifvlan *
379 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
383 TRUNK_LOCK_RASSERT(trunk);
385 LIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
386 if (ifv->ifv_vid == vid)
392 /* Debugging code to view the hashtables. */
394 vlan_dumphash(struct ifvlantrunk *trunk)
399 for (i = 0; i < (1 << trunk->hwidth); i++) {
401 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
402 printf("%s ", ifv->ifv_ifp->if_xname);
409 static __inline struct ifvlan *
410 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
413 return trunk->vlans[vid];
417 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
420 if (trunk->vlans[ifv->ifv_vid] != NULL)
422 trunk->vlans[ifv->ifv_vid] = ifv;
429 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
432 trunk->vlans[ifv->ifv_vid] = NULL;
439 vlan_freehash(struct ifvlantrunk *trunk)
444 vlan_inithash(struct ifvlantrunk *trunk)
448 #endif /* !VLAN_ARRAY */
451 trunk_destroy(struct ifvlantrunk *trunk)
456 vlan_freehash(trunk);
457 trunk->parent->if_vlantrunk = NULL;
459 TRUNK_LOCK_DESTROY(trunk);
464 * Program our multicast filter. What we're actually doing is
465 * programming the multicast filter of the parent. This has the
466 * side effect of causing the parent interface to receive multicast
467 * traffic that it doesn't really want, which ends up being discarded
468 * later by the upper protocol layers. Unfortunately, there's no way
469 * to avoid this: there really is only one physical interface.
472 vlan_setmulti(struct ifnet *ifp)
475 struct ifmultiaddr *ifma;
477 struct vlan_mc_entry *mc;
480 /* Find the parent. */
482 TRUNK_LOCK_ASSERT(TRUNK(sc));
485 CURVNET_SET_QUIET(ifp_p->if_vnet);
487 /* First, remove any existing filter entries. */
488 while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
489 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
490 (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
494 /* Now program new ones. */
496 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
497 if (ifma->ifma_addr->sa_family != AF_LINK)
499 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
501 IF_ADDR_WUNLOCK(ifp);
504 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
505 mc->mc_addr.sdl_index = ifp_p->if_index;
506 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
508 IF_ADDR_WUNLOCK(ifp);
509 SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
510 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
521 * A handler for parent interface link layer address changes.
522 * If the parent interface link layer address is changed we
523 * should also change it on all children vlans.
526 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
535 * Check if it's a trunk interface first of all
536 * to avoid needless locking.
538 if (ifp->if_vlantrunk == NULL)
543 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
546 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
547 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
548 #else /* VLAN_ARRAY */
549 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
550 LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) {
551 #endif /* VLAN_ARRAY */
553 if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp),
562 * A handler for network interface departure events.
563 * Track departure of trunks here so that we don't access invalid
564 * pointers or whatever if a trunk is ripped from under us, e.g.,
565 * by ejecting its hot-plug card. However, if an ifnet is simply
566 * being renamed, then there's no need to tear down the state.
569 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
575 * Check if it's a trunk interface first of all
576 * to avoid needless locking.
578 if (ifp->if_vlantrunk == NULL)
581 /* If the ifnet is just being renamed, don't do anything. */
582 if (ifp->if_flags & IFF_RENAMING)
587 * OK, it's a trunk. Loop over and detach all vlan's on it.
588 * Check trunk pointer after each vlan_unconfig() as it will
589 * free it and set to NULL after the last vlan was detached.
592 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
593 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
594 vlan_unconfig_locked(ifv->ifv_ifp, 1);
595 if (ifp->if_vlantrunk == NULL)
598 #else /* VLAN_ARRAY */
600 for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
601 if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
602 vlan_unconfig_locked(ifv->ifv_ifp, 1);
603 if (ifp->if_vlantrunk)
604 goto restart; /* trunk->hwidth can change */
608 #endif /* VLAN_ARRAY */
609 /* Trunk should have been destroyed in vlan_unconfig(). */
610 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
615 * Return the trunk device for a virtual interface.
617 static struct ifnet *
618 vlan_trunkdev(struct ifnet *ifp)
622 if (ifp->if_type != IFT_L2VLAN)
634 * Return the 12-bit VLAN VID for this interface, for use by external
635 * components such as Infiniband.
637 * XXXRW: Note that the function name here is historical; it should be named
641 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
645 if (ifp->if_type != IFT_L2VLAN)
648 *vidp = ifv->ifv_vid;
653 * Return a driver specific cookie for this interface. Synchronization
654 * with setcookie must be provided by the driver.
657 vlan_cookie(struct ifnet *ifp)
661 if (ifp->if_type != IFT_L2VLAN)
664 return (ifv->ifv_cookie);
668 * Store a cookie in our softc that drivers can use to store driver
669 * private per-instance data in.
672 vlan_setcookie(struct ifnet *ifp, void *cookie)
676 if (ifp->if_type != IFT_L2VLAN)
679 ifv->ifv_cookie = cookie;
684 * Return the vlan device present at the specific VID.
686 static struct ifnet *
687 vlan_devat(struct ifnet *ifp, uint16_t vid)
689 struct ifvlantrunk *trunk;
693 trunk = ifp->if_vlantrunk;
698 ifv = vlan_gethash(trunk, vid);
701 TRUNK_RUNLOCK(trunk);
706 * VLAN support can be loaded as a module. The only place in the
707 * system that's intimately aware of this is ether_input. We hook
708 * into this code through vlan_input_p which is defined there and
709 * set here. Noone else in the system should be aware of this so
710 * we use an explicit reference here.
712 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
714 /* For if_link_state_change() eyes only... */
715 extern void (*vlan_link_state_p)(struct ifnet *);
718 vlan_modevent(module_t mod, int type, void *data)
723 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
724 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
725 if (ifdetach_tag == NULL)
727 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
728 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
729 if (iflladdr_tag == NULL)
732 vlan_input_p = vlan_input;
733 vlan_link_state_p = vlan_link_state;
734 vlan_trunk_cap_p = vlan_trunk_capabilities;
735 vlan_trunkdev_p = vlan_trunkdev;
736 vlan_cookie_p = vlan_cookie;
737 vlan_setcookie_p = vlan_setcookie;
738 vlan_tag_p = vlan_tag;
739 vlan_devat_p = vlan_devat;
741 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
742 vlan_clone_create, vlan_clone_destroy);
745 printf("vlan: initialized, using "
749 "hash tables with chaining"
756 if_clone_detach(vlan_cloner);
758 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
759 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
761 vlan_link_state_p = NULL;
762 vlan_trunk_cap_p = NULL;
763 vlan_trunkdev_p = NULL;
765 vlan_cookie_p = NULL;
766 vlan_setcookie_p = NULL;
770 printf("vlan: unloaded\n");
778 static moduledata_t vlan_mod = {
784 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
785 MODULE_VERSION(if_vlan, 3);
789 vnet_vlan_init(const void *unused __unused)
792 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
793 vlan_clone_create, vlan_clone_destroy);
794 V_vlan_cloner = vlan_cloner;
796 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
797 vnet_vlan_init, NULL);
800 vnet_vlan_uninit(const void *unused __unused)
803 if_clone_detach(V_vlan_cloner);
805 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
806 vnet_vlan_uninit, NULL);
809 static struct ifnet *
810 vlan_clone_match_ethervid(struct if_clone *ifc, const char *name, int *vidp)
816 /* Check for <etherif>.<vlan> style interface names. */
817 IFNET_RLOCK_NOSLEEP();
818 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
820 * We can handle non-ethernet hardware types as long as
821 * they handle the tagging and headers themselves.
823 if (ifp->if_type != IFT_ETHER &&
824 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
826 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
828 cp = name + strlen(ifp->if_xname);
834 for(; *cp >= '0' && *cp <= '9'; cp++)
835 vid = (vid * 10) + (*cp - '0');
842 IFNET_RUNLOCK_NOSLEEP();
848 vlan_clone_match(struct if_clone *ifc, const char *name)
852 if (vlan_clone_match_ethervid(ifc, name, NULL) != NULL)
855 if (strncmp(vlanname, name, strlen(vlanname)) != 0)
857 for (cp = name + 4; *cp != '\0'; cp++) {
858 if (*cp < '0' || *cp > '9')
866 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
878 struct sockaddr_dl *sdl;
880 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
883 * There are 3 (ugh) ways to specify the cloned device:
884 * o pass a parameter block with the clone request.
885 * o specify parameters in the text of the clone device name
886 * o specify no parameters and get an unattached device that
887 * must be configured separately.
888 * The first technique is preferred; the latter two are
889 * supported for backwards compatibilty.
891 * XXXRW: Note historic use of the word "tag" here. New ioctls may be
895 error = copyin(params, &vlr, sizeof(vlr));
898 p = ifunit(vlr.vlr_parent);
902 * Don't let the caller set up a VLAN VID with
903 * anything except VLID bits.
905 if (vlr.vlr_tag & ~EVL_VLID_MASK)
907 error = ifc_name2unit(name, &unit);
913 wildcard = (unit < 0);
914 } else if ((p = vlan_clone_match_ethervid(ifc, name, &vid)) != NULL) {
920 * Don't let the caller set up a VLAN VID with
921 * anything except VLID bits.
923 if (vid & ~EVL_VLID_MASK)
928 error = ifc_name2unit(name, &unit);
932 wildcard = (unit < 0);
935 error = ifc_alloc_unit(ifc, &unit);
939 /* In the wildcard case, we need to update the name. */
941 for (dp = name; *dp != '\0'; dp++);
942 if (snprintf(dp, len - (dp-name), "%d", unit) >
943 len - (dp-name) - 1) {
944 panic("%s: interface name too long", __func__);
948 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
949 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
951 ifc_free_unit(ifc, unit);
955 SLIST_INIT(&ifv->vlan_mc_listhead);
956 /* Prepare pcpu counters */
957 ifv->ifv_ipackets = counter_u64_alloc(M_WAITOK);
958 ifv->ifv_opackets = counter_u64_alloc(M_WAITOK);
959 ifv->ifv_ibytes = counter_u64_alloc(M_WAITOK);
960 ifv->ifv_obytes = counter_u64_alloc(M_WAITOK);
961 ifv->ifv_omcasts = counter_u64_alloc(M_WAITOK);
962 ifv->ifv_oerrors = counter_u64_alloc(M_WAITOK);
966 * Set the name manually rather than using if_initname because
967 * we don't conform to the default naming convention for interfaces.
969 strlcpy(ifp->if_xname, name, IFNAMSIZ);
970 ifp->if_dname = vlanname;
971 ifp->if_dunit = unit;
972 /* NB: flags are not set here */
973 ifp->if_linkmib = &ifv->ifv_mib;
974 ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
975 /* NB: mtu is not set here */
977 ifp->if_init = vlan_init;
978 ifp->if_transmit = vlan_transmit;
979 ifp->if_qflush = vlan_qflush;
980 ifp->if_ioctl = vlan_ioctl;
981 ifp->if_flags = VLAN_IFFLAGS;
982 ifp->if_get_counter = vlan_get_counter;
983 ether_ifattach(ifp, eaddr);
984 /* Now undo some of the damage... */
985 ifp->if_baudrate = 0;
986 ifp->if_type = IFT_L2VLAN;
987 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
989 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
990 sdl->sdl_type = IFT_L2VLAN;
993 error = vlan_config(ifv, p, vid);
996 * Since we've partially failed, we need to back
997 * out all the way, otherwise userland could get
998 * confused. Thus, we destroy the interface.
1000 ether_ifdetach(ifp);
1003 ifc_free_unit(ifc, unit);
1009 /* Update flags on the parent, if necessary. */
1010 vlan_setflags(ifp, 1);
1017 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
1019 struct ifvlan *ifv = ifp->if_softc;
1020 int unit = ifp->if_dunit;
1022 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
1023 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
1025 counter_u64_free(ifv->ifv_ipackets);
1026 counter_u64_free(ifv->ifv_opackets);
1027 counter_u64_free(ifv->ifv_ibytes);
1028 counter_u64_free(ifv->ifv_obytes);
1029 counter_u64_free(ifv->ifv_omcasts);
1030 counter_u64_free(ifv->ifv_oerrors);
1032 ifc_free_unit(ifc, unit);
1038 * The ifp->if_init entry point for vlan(4) is a no-op.
1041 vlan_init(void *foo __unused)
1046 * The if_transmit method for vlan(4) interface.
1049 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1053 int error, len, mcast;
1055 ifv = ifp->if_softc;
1057 len = m->m_pkthdr.len;
1058 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1063 * Do not run parent's if_transmit() if the parent is not up,
1064 * or parent's driver will cause a system crash.
1066 if (!UP_AND_RUNNING(p)) {
1068 counter_u64_add(ifv->ifv_oerrors, 1);
1073 * Pad the frame to the minimum size allowed if told to.
1074 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
1075 * paragraph C.4.4.3.b. It can help to work around buggy
1076 * bridges that violate paragraph C.4.4.3.a from the same
1077 * document, i.e., fail to pad short frames after untagging.
1078 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
1079 * untagging it will produce a 62-byte frame, which is a runt
1080 * and requires padding. There are VLAN-enabled network
1081 * devices that just discard such runts instead or mishandle
1084 if (soft_pad && p->if_type == IFT_ETHER) {
1085 static char pad[8]; /* just zeros */
1088 for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
1089 n > 0; n -= sizeof(pad))
1090 if (!m_append(m, min(n, sizeof(pad)), pad))
1094 if_printf(ifp, "cannot pad short frame\n");
1095 counter_u64_add(ifv->ifv_oerrors, 1);
1102 * If underlying interface can do VLAN tag insertion itself,
1103 * just pass the packet along. However, we need some way to
1104 * tell the interface where the packet came from so that it
1105 * knows how to find the VLAN tag to use, so we attach a
1106 * packet tag that holds it.
1108 if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1109 m->m_pkthdr.ether_vtag = ifv->ifv_vid;
1110 m->m_flags |= M_VLANTAG;
1112 m = ether_vlanencap(m, ifv->ifv_vid);
1114 if_printf(ifp, "unable to prepend VLAN header\n");
1115 counter_u64_add(ifv->ifv_oerrors, 1);
1121 * Send it, precisely as ether_output() would have.
1123 error = (p->if_transmit)(p, m);
1125 counter_u64_add(ifv->ifv_opackets, 1);
1126 counter_u64_add(ifv->ifv_obytes, len);
1127 counter_u64_add(ifv->ifv_omcasts, mcast);
1129 counter_u64_add(ifv->ifv_oerrors, 1);
1134 vlan_get_counter(struct ifnet *ifp, ifnet_counter cnt)
1138 ifv = ifp->if_softc;
1141 case IFCOUNTER_IPACKETS:
1142 return (counter_u64_fetch(ifv->ifv_ipackets));
1143 case IFCOUNTER_OPACKETS:
1144 return (counter_u64_fetch(ifv->ifv_opackets));
1145 case IFCOUNTER_IBYTES:
1146 return (counter_u64_fetch(ifv->ifv_ibytes));
1147 case IFCOUNTER_OBYTES:
1148 return (counter_u64_fetch(ifv->ifv_obytes));
1149 case IFCOUNTER_OMCASTS:
1150 return (counter_u64_fetch(ifv->ifv_omcasts));
1151 case IFCOUNTER_OERRORS:
1152 return (counter_u64_fetch(ifv->ifv_oerrors));
1154 return (if_get_counter_compat(ifp, cnt));
1160 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1163 vlan_qflush(struct ifnet *ifp __unused)
1168 vlan_input(struct ifnet *ifp, struct mbuf *m)
1170 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1175 KASSERT(trunk != NULL, ("%s: no trunk", __func__));
1177 if (m->m_flags & M_VLANTAG) {
1179 * Packet is tagged, but m contains a normal
1180 * Ethernet frame; the tag is stored out-of-band.
1182 vid = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
1183 m->m_flags &= ~M_VLANTAG;
1185 struct ether_vlan_header *evl;
1188 * Packet is tagged in-band as specified by 802.1q.
1190 switch (ifp->if_type) {
1192 if (m->m_len < sizeof(*evl) &&
1193 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1194 if_printf(ifp, "cannot pullup VLAN header\n");
1197 evl = mtod(m, struct ether_vlan_header *);
1198 vid = EVL_VLANOFTAG(ntohs(evl->evl_tag));
1201 * Remove the 802.1q header by copying the Ethernet
1202 * addresses over it and adjusting the beginning of
1203 * the data in the mbuf. The encapsulated Ethernet
1204 * type field is already in place.
1206 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1207 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1208 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1213 panic("%s: %s has unsupported if_type %u",
1214 __func__, ifp->if_xname, ifp->if_type);
1223 ifv = vlan_gethash(trunk, vid);
1224 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1225 TRUNK_RUNLOCK(trunk);
1230 TRUNK_RUNLOCK(trunk);
1232 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1233 counter_u64_add(ifv->ifv_ipackets, 1);
1234 counter_u64_add(ifv->ifv_ibytes, m->m_pkthdr.len);
1236 /* Pass it back through the parent's input routine. */
1237 (*ifp->if_input)(ifv->ifv_ifp, m);
1241 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
1243 struct ifvlantrunk *trunk;
1247 /* VID numbers 0x0 and 0xFFF are reserved */
1248 if (vid == 0 || vid == 0xFFF)
1250 if (p->if_type != IFT_ETHER &&
1251 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1252 return (EPROTONOSUPPORT);
1253 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1254 return (EPROTONOSUPPORT);
1258 if (p->if_vlantrunk == NULL) {
1259 trunk = malloc(sizeof(struct ifvlantrunk),
1260 M_VLAN, M_WAITOK | M_ZERO);
1261 vlan_inithash(trunk);
1263 if (p->if_vlantrunk != NULL) {
1264 /* A race that that is very unlikely to be hit. */
1265 vlan_freehash(trunk);
1266 free(trunk, M_VLAN);
1269 TRUNK_LOCK_INIT(trunk);
1271 p->if_vlantrunk = trunk;
1276 trunk = p->if_vlantrunk;
1280 ifv->ifv_vid = vid; /* must set this before vlan_inshash() */
1281 error = vlan_inshash(trunk, ifv);
1284 ifv->ifv_proto = ETHERTYPE_VLAN;
1285 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1286 ifv->ifv_mintu = ETHERMIN;
1287 ifv->ifv_pflags = 0;
1290 * If the parent supports the VLAN_MTU capability,
1291 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1294 if (p->if_capenable & IFCAP_VLAN_MTU) {
1296 * No need to fudge the MTU since the parent can
1297 * handle extended frames.
1299 ifv->ifv_mtufudge = 0;
1302 * Fudge the MTU by the encapsulation size. This
1303 * makes us incompatible with strictly compliant
1304 * 802.1Q implementations, but allows us to use
1305 * the feature with other NetBSD implementations,
1306 * which might still be useful.
1308 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1311 ifv->ifv_trunk = trunk;
1314 * Initialize fields from our parent. This duplicates some
1315 * work with ether_ifattach() but allows for non-ethernet
1316 * interfaces to also work.
1318 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1319 ifp->if_baudrate = p->if_baudrate;
1320 ifp->if_output = p->if_output;
1321 ifp->if_input = p->if_input;
1322 ifp->if_resolvemulti = p->if_resolvemulti;
1323 ifp->if_addrlen = p->if_addrlen;
1324 ifp->if_broadcastaddr = p->if_broadcastaddr;
1327 * Copy only a selected subset of flags from the parent.
1328 * Other flags are none of our business.
1330 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1331 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1332 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1333 #undef VLAN_COPY_FLAGS
1335 ifp->if_link_state = p->if_link_state;
1337 vlan_capabilities(ifv);
1340 * Set up our interface address to reflect the underlying
1341 * physical interface's.
1343 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1344 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1348 * Configure multicast addresses that may already be
1349 * joined on the vlan device.
1351 (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1353 /* We are ready for operation now. */
1354 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1356 TRUNK_UNLOCK(trunk);
1358 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1365 vlan_unconfig(struct ifnet *ifp)
1369 vlan_unconfig_locked(ifp, 0);
1374 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1376 struct ifvlantrunk *trunk;
1377 struct vlan_mc_entry *mc;
1379 struct ifnet *parent;
1384 ifv = ifp->if_softc;
1385 trunk = ifv->ifv_trunk;
1388 if (trunk != NULL) {
1391 parent = trunk->parent;
1394 * Since the interface is being unconfigured, we need to
1395 * empty the list of multicast groups that we may have joined
1396 * while we were alive from the parent's list.
1398 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1400 * If the parent interface is being detached,
1401 * all its multicast addresses have already
1402 * been removed. Warn about errors if
1403 * if_delmulti() does fail, but don't abort as
1404 * all callers expect vlan destruction to
1408 error = if_delmulti(parent,
1409 (struct sockaddr *)&mc->mc_addr);
1412 "Failed to delete multicast address from parent: %d\n",
1415 SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1419 vlan_setflags(ifp, 0); /* clear special flags on parent */
1420 vlan_remhash(trunk, ifv);
1421 ifv->ifv_trunk = NULL;
1424 * Check if we were the last.
1426 if (trunk->refcnt == 0) {
1427 parent->if_vlantrunk = NULL;
1429 * XXXGL: If some ithread has already entered
1430 * vlan_input() and is now blocked on the trunk
1431 * lock, then it should preempt us right after
1432 * unlock and finish its work. Then we will acquire
1433 * lock again in trunk_destroy().
1435 TRUNK_UNLOCK(trunk);
1436 trunk_destroy(trunk);
1438 TRUNK_UNLOCK(trunk);
1441 /* Disconnect from parent. */
1442 if (ifv->ifv_pflags)
1443 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1444 ifp->if_mtu = ETHERMTU;
1445 ifp->if_link_state = LINK_STATE_UNKNOWN;
1446 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1449 * Only dispatch an event if vlan was
1450 * attached, otherwise there is nothing
1451 * to cleanup anyway.
1454 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1457 /* Handle a reference counted flag that should be set on the parent as well */
1459 vlan_setflag(struct ifnet *ifp, int flag, int status,
1460 int (*func)(struct ifnet *, int))
1465 /* XXX VLAN_LOCK_ASSERT(); */
1467 ifv = ifp->if_softc;
1468 status = status ? (ifp->if_flags & flag) : 0;
1469 /* Now "status" contains the flag value or 0 */
1472 * See if recorded parent's status is different from what
1473 * we want it to be. If it is, flip it. We record parent's
1474 * status in ifv_pflags so that we won't clear parent's flag
1475 * we haven't set. In fact, we don't clear or set parent's
1476 * flags directly, but get or release references to them.
1477 * That's why we can be sure that recorded flags still are
1478 * in accord with actual parent's flags.
1480 if (status != (ifv->ifv_pflags & flag)) {
1481 error = (*func)(PARENT(ifv), status);
1484 ifv->ifv_pflags &= ~flag;
1485 ifv->ifv_pflags |= status;
1491 * Handle IFF_* flags that require certain changes on the parent:
1492 * if "status" is true, update parent's flags respective to our if_flags;
1493 * if "status" is false, forcedly clear the flags set on parent.
1496 vlan_setflags(struct ifnet *ifp, int status)
1500 for (i = 0; vlan_pflags[i].flag; i++) {
1501 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1502 status, vlan_pflags[i].func);
1509 /* Inform all vlans that their parent has changed link state */
1511 vlan_link_state(struct ifnet *ifp)
1513 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1519 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1520 if (trunk->vlans[i] != NULL) {
1521 ifv = trunk->vlans[i];
1523 for (i = 0; i < (1 << trunk->hwidth); i++)
1524 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
1526 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1527 if_link_state_change(ifv->ifv_ifp,
1528 trunk->parent->if_link_state);
1530 TRUNK_UNLOCK(trunk);
1534 vlan_capabilities(struct ifvlan *ifv)
1536 struct ifnet *p = PARENT(ifv);
1537 struct ifnet *ifp = ifv->ifv_ifp;
1539 TRUNK_LOCK_ASSERT(TRUNK(ifv));
1542 * If the parent interface can do checksum offloading
1543 * on VLANs, then propagate its hardware-assisted
1544 * checksumming flags. Also assert that checksum
1545 * offloading requires hardware VLAN tagging.
1547 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1548 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1550 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1551 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1552 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1553 ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP |
1554 CSUM_UDP | CSUM_SCTP);
1556 ifp->if_capenable = 0;
1557 ifp->if_hwassist = 0;
1560 * If the parent interface can do TSO on VLANs then
1561 * propagate the hardware-assisted flag. TSO on VLANs
1562 * does not necessarily require hardware VLAN tagging.
1564 if (p->if_hw_tsomax > 0)
1565 ifp->if_hw_tsomax = p->if_hw_tsomax;
1566 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1567 ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO;
1568 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1569 ifp->if_capenable |= p->if_capenable & IFCAP_TSO;
1570 ifp->if_hwassist |= p->if_hwassist & CSUM_TSO;
1572 ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO);
1573 ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO);
1577 * If the parent interface can offload TCP connections over VLANs then
1578 * propagate its TOE capability to the VLAN interface.
1580 * All TOE drivers in the tree today can deal with VLANs. If this
1581 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1584 #define IFCAP_VLAN_TOE IFCAP_TOE
1585 if (p->if_capabilities & IFCAP_VLAN_TOE)
1586 ifp->if_capabilities |= p->if_capabilities & IFCAP_TOE;
1587 if (p->if_capenable & IFCAP_VLAN_TOE) {
1588 TOEDEV(ifp) = TOEDEV(p);
1589 ifp->if_capenable |= p->if_capenable & IFCAP_TOE;
1594 vlan_trunk_capabilities(struct ifnet *ifp)
1596 struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1602 for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1603 if (trunk->vlans[i] != NULL) {
1604 ifv = trunk->vlans[i];
1606 for (i = 0; i < (1 << trunk->hwidth); i++) {
1607 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1609 vlan_capabilities(ifv);
1611 TRUNK_UNLOCK(trunk);
1615 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1621 struct ifvlantrunk *trunk;
1625 ifr = (struct ifreq *)data;
1626 ifa = (struct ifaddr *) data;
1627 ifv = ifp->if_softc;
1631 ifp->if_flags |= IFF_UP;
1633 if (ifa->ifa_addr->sa_family == AF_INET)
1634 arp_ifinit(ifp, ifa);
1639 struct sockaddr *sa;
1641 sa = (struct sockaddr *)&ifr->ifr_data;
1642 bcopy(IF_LLADDR(ifp), sa->sa_data, ifp->if_addrlen);
1647 if (TRUNK(ifv) != NULL) {
1650 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1651 /* Limit the result to the parent's current config. */
1653 struct ifmediareq *ifmr;
1655 ifmr = (struct ifmediareq *)data;
1656 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1657 ifmr->ifm_count = 1;
1658 error = copyout(&ifmr->ifm_current,
1675 * Set the interface MTU.
1678 if (TRUNK(ifv) != NULL) {
1680 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1682 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1685 ifp->if_mtu = ifr->ifr_mtu;
1694 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
1695 * interface to be delegated to a jail without allowing the
1696 * jail to change what underlying interface/VID it is
1697 * associated with. We are not entirely convinced that this
1698 * is the right way to accomplish that policy goal.
1700 if (ifp->if_vnet != ifp->if_home_vnet) {
1705 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1708 if (vlr.vlr_parent[0] == '\0') {
1712 p = ifunit(vlr.vlr_parent);
1718 * Don't let the caller set up a VLAN VID with
1719 * anything except VLID bits.
1721 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1725 error = vlan_config(ifv, p, vlr.vlr_tag);
1729 /* Update flags on the parent, if necessary. */
1730 vlan_setflags(ifp, 1);
1735 if (ifp->if_vnet != ifp->if_home_vnet) {
1740 bzero(&vlr, sizeof(vlr));
1742 if (TRUNK(ifv) != NULL) {
1743 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1744 sizeof(vlr.vlr_parent));
1745 vlr.vlr_tag = ifv->ifv_vid;
1748 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1753 * We should propagate selected flags to the parent,
1754 * e.g., promiscuous mode.
1756 if (TRUNK(ifv) != NULL)
1757 error = vlan_setflags(ifp, 1);
1763 * If we don't have a parent, just remember the membership for
1767 if (trunk != NULL) {
1769 error = vlan_setmulti(ifp);
1770 TRUNK_UNLOCK(trunk);