2 * Copyright 1998 Massachusetts Institute of Technology
3 * Copyright 2012 ADARA Networks, Inc.
4 * Copyright 2017 Dell EMC Isilon
6 * Portions of this software were developed by Robert N. M. Watson under
7 * contract to ADARA Networks, Inc.
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby
11 * granted, provided that both the above copyright notice and this
12 * permission notice appear in all copies, that both the above
13 * copyright notice and this permission notice appear in all
14 * supporting documentation, and that the name of M.I.T. not be used
15 * in advertising or publicity pertaining to distribution of the
16 * software without specific, written prior permission. M.I.T. makes
17 * no representations about the suitability of this software for any
18 * purpose. It is provided "as is" without express or implied
21 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
22 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
25 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
37 * This is sort of sneaky in the implementation, since
38 * we need to pretend to be enough of an Ethernet implementation
39 * to make arp work. The way we do this is by telling everyone
40 * that we are an Ethernet, and then catch the packets that
41 * ether_output() sends to us via if_transmit(), rewrite them for
42 * use by the real outgoing interface, and ask it to send them.
46 #include "opt_inet6.h"
47 #include "opt_ipsec.h"
48 #include "opt_kern_tls.h"
50 #include "opt_ratelimit.h"
52 #include <sys/param.h>
53 #include <sys/eventhandler.h>
54 #include <sys/kernel.h>
56 #include <sys/malloc.h>
58 #include <sys/module.h>
59 #include <sys/rmlock.h>
61 #include <sys/queue.h>
62 #include <sys/socket.h>
63 #include <sys/sockio.h>
64 #include <sys/sysctl.h>
65 #include <sys/systm.h>
67 #include <sys/taskqueue.h>
70 #include <net/ethernet.h>
72 #include <net/if_var.h>
73 #include <net/if_private.h>
74 #include <net/if_clone.h>
75 #include <net/if_dl.h>
76 #include <net/if_types.h>
77 #include <net/if_vlan_var.h>
78 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/if_ether.h>
86 #include <netlink/netlink.h>
87 #include <netlink/netlink_ctl.h>
88 #include <netlink/netlink_route.h>
89 #include <netlink/route/route_var.h>
91 #define VLAN_DEF_HWIDTH 4
92 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
94 #define UP_AND_RUNNING(ifp) \
95 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
97 CK_SLIST_HEAD(ifvlanhead
, ifvlan
);
100 struct ifnet
*parent
; /* parent interface of this trunk */
103 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
104 struct ifvlan
*vlans
[VLAN_ARRAY_SIZE
]; /* static table */
106 struct ifvlanhead
*hash
; /* dynamic hash-list table */
113 #if defined(KERN_TLS) || defined(RATELIMIT)
114 struct vlan_snd_tag
{
115 struct m_snd_tag com
;
116 struct m_snd_tag
*tag
;
119 static inline struct vlan_snd_tag
*
120 mst_to_vst(struct m_snd_tag
*mst
)
123 return (__containerof(mst
, struct vlan_snd_tag
, com
));
128 * This macro provides a facility to iterate over every vlan on a trunk with
129 * the assumption that none will be added/removed during iteration.
132 #define VLAN_FOREACH(_ifv, _trunk) \
134 for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
135 if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
136 #else /* VLAN_ARRAY */
137 #define VLAN_FOREACH(_ifv, _trunk) \
138 struct ifvlan *_next; \
140 for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
141 CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
142 #endif /* VLAN_ARRAY */
145 * This macro provides a facility to iterate over every vlan on a trunk while
146 * also modifying the number of vlans on the trunk. The iteration continues
147 * until some condition is met or there are no more vlans on the trunk.
150 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
151 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
153 for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
154 if (((_ifv) = (_trunk)->vlans[_i]))
155 #else /* VLAN_ARRAY */
157 * The hash table case is more complicated. We allow for the hash table to be
158 * modified (i.e. vlans removed) while we are iterating over it. To allow for
159 * this we must restart the iteration every time we "touch" something during
160 * the iteration, since removal will resize the hash table and invalidate our
161 * current position. If acting on the touched element causes the trunk to be
162 * emptied, then iteration also stops.
164 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
166 bool _touch = false; \
168 !(_cond) && _i < (1 << (_trunk)->hwidth); \
169 _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
170 if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
172 #endif /* VLAN_ARRAY */
174 struct vlan_mc_entry
{
175 struct sockaddr_dl mc_addr
;
176 CK_SLIST_ENTRY(vlan_mc_entry
) mc_entries
;
177 struct epoch_context mc_epoch_ctx
;
181 struct ifvlantrunk
*ifv_trunk
;
182 struct ifnet
*ifv_ifp
;
183 #define TRUNK(ifv) ((ifv)->ifv_trunk)
184 #define PARENT(ifv) (TRUNK(ifv)->parent)
186 int ifv_pflags
; /* special flags we have set on parent */
189 int ifv_encaplen
; /* encapsulation length */
190 int ifv_mtufudge
; /* MTU fudged by this much */
191 int ifv_mintu
; /* min transmission unit */
192 struct ether_8021q_tag ifv_qtag
;
193 #define ifv_proto ifv_qtag.proto
194 #define ifv_vid ifv_qtag.vid
195 #define ifv_pcp ifv_qtag.pcp
196 struct task lladdr_task
;
197 CK_SLIST_HEAD(, vlan_mc_entry
) vlan_mc_listhead
;
199 CK_SLIST_ENTRY(ifvlan
) ifv_list
;
203 /* Special flags we should propagate to parent. */
206 int (*func
)(struct ifnet
*, int);
208 {IFF_PROMISC
, ifpromisc
},
209 {IFF_ALLMULTI
, if_allmulti
},
213 VNET_DECLARE(int, vlan_mtag_pcp
);
214 #define V_vlan_mtag_pcp VNET(vlan_mtag_pcp)
216 static const char vlanname
[] = "vlan";
217 static MALLOC_DEFINE(M_VLAN
, vlanname
, "802.1Q Virtual LAN Interface");
219 static eventhandler_tag ifdetach_tag
;
220 static eventhandler_tag iflladdr_tag
;
221 static eventhandler_tag ifevent_tag
;
224 * if_vlan uses two module-level synchronizations primitives to allow concurrent
225 * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
226 * while they are being used for tx/rx. To accomplish this in a way that has
227 * acceptable performance and cooperation with other parts of the network stack
228 * there is a non-sleepable epoch(9) and an sx(9).
230 * The performance-sensitive paths that warrant using the epoch(9) are
231 * vlan_transmit and vlan_input. Both have to check for the vlan interface's
232 * existence using if_vlantrunk, and being in the network tx/rx paths the use
233 * of an epoch(9) gives a measureable improvement in performance.
235 * The reason for having an sx(9) is mostly because there are still areas that
236 * must be sleepable and also have safe concurrent access to a vlan interface.
237 * Since the sx(9) exists, it is used by default in most paths unless sleeping
238 * is not permitted, or if it is not clear whether sleeping is permitted.
241 #define _VLAN_SX_ID ifv_sx
243 static struct sx _VLAN_SX_ID
;
245 #define VLAN_LOCKING_INIT() \
246 sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE)
248 #define VLAN_LOCKING_DESTROY() \
249 sx_destroy(&_VLAN_SX_ID)
251 #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID)
252 #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID)
253 #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID)
254 #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID)
255 #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
256 #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
257 #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED)
260 * We also have a per-trunk mutex that should be acquired when changing
263 #define TRUNK_LOCK_INIT(trunk) mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
264 #define TRUNK_LOCK_DESTROY(trunk) mtx_destroy(&(trunk)->lock)
265 #define TRUNK_WLOCK(trunk) mtx_lock(&(trunk)->lock)
266 #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock)
267 #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED);
270 * The VLAN_ARRAY substitutes the dynamic hash with a static array
271 * with 4096 entries. In theory this can give a boost in processing,
272 * however in practice it does not. Probably this is because the array
273 * is too big to fit into CPU cache.
276 static void vlan_inithash(struct ifvlantrunk
*trunk
);
277 static void vlan_freehash(struct ifvlantrunk
*trunk
);
278 static int vlan_inshash(struct ifvlantrunk
*trunk
, struct ifvlan
*ifv
);
279 static int vlan_remhash(struct ifvlantrunk
*trunk
, struct ifvlan
*ifv
);
280 static void vlan_growhash(struct ifvlantrunk
*trunk
, int howmuch
);
281 static __inline
struct ifvlan
* vlan_gethash(struct ifvlantrunk
*trunk
,
284 static void trunk_destroy(struct ifvlantrunk
*trunk
);
286 static void vlan_init(void *foo
);
287 static void vlan_input(struct ifnet
*ifp
, struct mbuf
*m
);
288 static int vlan_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t addr
);
289 #if defined(KERN_TLS) || defined(RATELIMIT)
290 static int vlan_snd_tag_alloc(struct ifnet
*,
291 union if_snd_tag_alloc_params
*, struct m_snd_tag
**);
292 static int vlan_snd_tag_modify(struct m_snd_tag
*,
293 union if_snd_tag_modify_params
*);
294 static int vlan_snd_tag_query(struct m_snd_tag
*,
295 union if_snd_tag_query_params
*);
296 static void vlan_snd_tag_free(struct m_snd_tag
*);
297 static struct m_snd_tag
*vlan_next_snd_tag(struct m_snd_tag
*);
298 static void vlan_ratelimit_query(struct ifnet
*,
299 struct if_ratelimit_query_results
*);
301 static void vlan_qflush(struct ifnet
*ifp
);
302 static int vlan_setflag(struct ifnet
*ifp
, int flag
, int status
,
303 int (*func
)(struct ifnet
*, int));
304 static int vlan_setflags(struct ifnet
*ifp
, int status
);
305 static int vlan_setmulti(struct ifnet
*ifp
);
306 static int vlan_transmit(struct ifnet
*ifp
, struct mbuf
*m
);
308 static void vlan_altq_start(struct ifnet
*ifp
);
309 static int vlan_altq_transmit(struct ifnet
*ifp
, struct mbuf
*m
);
311 static int vlan_output(struct ifnet
*ifp
, struct mbuf
*m
,
312 const struct sockaddr
*dst
, struct route
*ro
);
313 static void vlan_unconfig(struct ifnet
*ifp
);
314 static void vlan_unconfig_locked(struct ifnet
*ifp
, int departing
);
315 static int vlan_config(struct ifvlan
*ifv
, struct ifnet
*p
, uint16_t tag
,
317 static void vlan_link_state(struct ifnet
*ifp
);
318 static void vlan_capabilities(struct ifvlan
*ifv
);
319 static void vlan_trunk_capabilities(struct ifnet
*ifp
);
321 static struct ifnet
*vlan_clone_match_ethervid(const char *, int *);
322 static int vlan_clone_match(struct if_clone
*, const char *);
323 static int vlan_clone_create(struct if_clone
*, char *, size_t,
324 struct ifc_data
*, struct ifnet
**);
325 static int vlan_clone_destroy(struct if_clone
*, struct ifnet
*, uint32_t);
327 static int vlan_clone_create_nl(struct if_clone
*ifc
, char *name
, size_t len
,
328 struct ifc_data_nl
*ifd
);
329 static int vlan_clone_modify_nl(struct ifnet
*ifp
, struct ifc_data_nl
*ifd
);
330 static void vlan_clone_dump_nl(struct ifnet
*ifp
, struct nl_writer
*nw
);
332 static void vlan_ifdetach(void *arg
, struct ifnet
*ifp
);
333 static void vlan_iflladdr(void *arg
, struct ifnet
*ifp
);
334 static void vlan_ifevent(void *arg
, struct ifnet
*ifp
, int event
);
336 static void vlan_lladdr_fn(void *arg
, int pending
);
338 static struct if_clone
*vlan_cloner
;
341 VNET_DEFINE_STATIC(struct if_clone
*, vlan_cloner
);
342 #define V_vlan_cloner VNET(vlan_cloner)
346 static const struct if_snd_tag_sw vlan_snd_tag_ul_sw
= {
347 .snd_tag_modify
= vlan_snd_tag_modify
,
348 .snd_tag_query
= vlan_snd_tag_query
,
349 .snd_tag_free
= vlan_snd_tag_free
,
350 .next_snd_tag
= vlan_next_snd_tag
,
351 .type
= IF_SND_TAG_TYPE_UNLIMITED
354 static const struct if_snd_tag_sw vlan_snd_tag_rl_sw
= {
355 .snd_tag_modify
= vlan_snd_tag_modify
,
356 .snd_tag_query
= vlan_snd_tag_query
,
357 .snd_tag_free
= vlan_snd_tag_free
,
358 .next_snd_tag
= vlan_next_snd_tag
,
359 .type
= IF_SND_TAG_TYPE_RATE_LIMIT
364 static const struct if_snd_tag_sw vlan_snd_tag_tls_sw
= {
365 .snd_tag_modify
= vlan_snd_tag_modify
,
366 .snd_tag_query
= vlan_snd_tag_query
,
367 .snd_tag_free
= vlan_snd_tag_free
,
368 .next_snd_tag
= vlan_next_snd_tag
,
369 .type
= IF_SND_TAG_TYPE_TLS
373 static const struct if_snd_tag_sw vlan_snd_tag_tls_rl_sw
= {
374 .snd_tag_modify
= vlan_snd_tag_modify
,
375 .snd_tag_query
= vlan_snd_tag_query
,
376 .snd_tag_free
= vlan_snd_tag_free
,
377 .next_snd_tag
= vlan_next_snd_tag
,
378 .type
= IF_SND_TAG_TYPE_TLS_RATE_LIMIT
384 vlan_mc_free(struct epoch_context
*ctx
)
386 struct vlan_mc_entry
*mc
= __containerof(ctx
, struct vlan_mc_entry
, mc_epoch_ctx
);
391 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
394 vlan_inithash(struct ifvlantrunk
*trunk
)
399 * The trunk must not be locked here since we call malloc(M_WAITOK).
400 * It is OK in case this function is called before the trunk struct
401 * gets hooked up and becomes visible from other threads.
404 KASSERT(trunk
->hwidth
== 0 && trunk
->hash
== NULL
,
405 ("%s: hash already initialized", __func__
));
407 trunk
->hwidth
= VLAN_DEF_HWIDTH
;
408 n
= 1 << trunk
->hwidth
;
409 trunk
->hmask
= n
- 1;
410 trunk
->hash
= malloc(sizeof(struct ifvlanhead
) * n
, M_VLAN
, M_WAITOK
);
411 for (i
= 0; i
< n
; i
++)
412 CK_SLIST_INIT(&trunk
->hash
[i
]);
416 vlan_freehash(struct ifvlantrunk
*trunk
)
421 KASSERT(trunk
->hwidth
> 0, ("%s: hwidth not positive", __func__
));
422 for (i
= 0; i
< (1 << trunk
->hwidth
); i
++)
423 KASSERT(CK_SLIST_EMPTY(&trunk
->hash
[i
]),
424 ("%s: hash table not empty", __func__
));
426 free(trunk
->hash
, M_VLAN
);
428 trunk
->hwidth
= trunk
->hmask
= 0;
432 vlan_inshash(struct ifvlantrunk
*trunk
, struct ifvlan
*ifv
)
438 KASSERT(trunk
->hwidth
> 0, ("%s: hwidth not positive", __func__
));
440 b
= 1 << trunk
->hwidth
;
441 i
= HASH(ifv
->ifv_vid
, trunk
->hmask
);
442 CK_SLIST_FOREACH(ifv2
, &trunk
->hash
[i
], ifv_list
)
443 if (ifv
->ifv_vid
== ifv2
->ifv_vid
)
447 * Grow the hash when the number of vlans exceeds half of the number of
448 * hash buckets squared. This will make the average linked-list length
451 if (trunk
->refcnt
> (b
* b
) / 2) {
452 vlan_growhash(trunk
, 1);
453 i
= HASH(ifv
->ifv_vid
, trunk
->hmask
);
455 CK_SLIST_INSERT_HEAD(&trunk
->hash
[i
], ifv
, ifv_list
);
462 vlan_remhash(struct ifvlantrunk
*trunk
, struct ifvlan
*ifv
)
468 KASSERT(trunk
->hwidth
> 0, ("%s: hwidth not positive", __func__
));
470 b
= 1 << (trunk
->hwidth
- 1);
471 i
= HASH(ifv
->ifv_vid
, trunk
->hmask
);
472 CK_SLIST_FOREACH(ifv2
, &trunk
->hash
[i
], ifv_list
)
475 CK_SLIST_REMOVE(&trunk
->hash
[i
], ifv2
, ifvlan
, ifv_list
);
476 if (trunk
->refcnt
< (b
* b
) / 2)
477 vlan_growhash(trunk
, -1);
481 panic("%s: vlan not found\n", __func__
);
482 return (ENOENT
); /*NOTREACHED*/
486 * Grow the hash larger or smaller if memory permits.
489 vlan_growhash(struct ifvlantrunk
*trunk
, int howmuch
)
492 struct ifvlanhead
*hash2
;
493 int hwidth2
, i
, j
, n
, n2
;
496 KASSERT(trunk
->hwidth
> 0, ("%s: hwidth not positive", __func__
));
499 /* Harmless yet obvious coding error */
500 printf("%s: howmuch is 0\n", __func__
);
504 hwidth2
= trunk
->hwidth
+ howmuch
;
505 n
= 1 << trunk
->hwidth
;
507 /* Do not shrink the table below the default */
508 if (hwidth2
< VLAN_DEF_HWIDTH
)
511 hash2
= malloc(sizeof(struct ifvlanhead
) * n2
, M_VLAN
, M_WAITOK
);
512 for (j
= 0; j
< n2
; j
++)
513 CK_SLIST_INIT(&hash2
[j
]);
514 for (i
= 0; i
< n
; i
++)
515 while ((ifv
= CK_SLIST_FIRST(&trunk
->hash
[i
])) != NULL
) {
516 CK_SLIST_REMOVE(&trunk
->hash
[i
], ifv
, ifvlan
, ifv_list
);
517 j
= HASH(ifv
->ifv_vid
, n2
- 1);
518 CK_SLIST_INSERT_HEAD(&hash2
[j
], ifv
, ifv_list
);
521 free(trunk
->hash
, M_VLAN
);
523 trunk
->hwidth
= hwidth2
;
524 trunk
->hmask
= n2
- 1;
527 if_printf(trunk
->parent
,
528 "VLAN hash table resized from %d to %d buckets\n", n
, n2
);
531 static __inline
struct ifvlan
*
532 vlan_gethash(struct ifvlantrunk
*trunk
, uint16_t vid
)
538 CK_SLIST_FOREACH(ifv
, &trunk
->hash
[HASH(vid
, trunk
->hmask
)], ifv_list
)
539 if (ifv
->ifv_vid
== vid
)
545 /* Debugging code to view the hashtables. */
547 vlan_dumphash(struct ifvlantrunk
*trunk
)
552 for (i
= 0; i
< (1 << trunk
->hwidth
); i
++) {
554 CK_SLIST_FOREACH(ifv
, &trunk
->hash
[i
], ifv_list
)
555 printf("%s ", ifv
->ifv_ifp
->if_xname
);
562 static __inline
struct ifvlan
*
563 vlan_gethash(struct ifvlantrunk
*trunk
, uint16_t vid
)
566 return trunk
->vlans
[vid
];
570 vlan_inshash(struct ifvlantrunk
*trunk
, struct ifvlan
*ifv
)
573 if (trunk
->vlans
[ifv
->ifv_vid
] != NULL
)
575 trunk
->vlans
[ifv
->ifv_vid
] = ifv
;
582 vlan_remhash(struct ifvlantrunk
*trunk
, struct ifvlan
*ifv
)
585 trunk
->vlans
[ifv
->ifv_vid
] = NULL
;
592 vlan_freehash(struct ifvlantrunk
*trunk
)
597 vlan_inithash(struct ifvlantrunk
*trunk
)
601 #endif /* !VLAN_ARRAY */
604 trunk_destroy(struct ifvlantrunk
*trunk
)
608 vlan_freehash(trunk
);
609 trunk
->parent
->if_vlantrunk
= NULL
;
610 TRUNK_LOCK_DESTROY(trunk
);
611 if_rele(trunk
->parent
);
616 * Program our multicast filter. What we're actually doing is
617 * programming the multicast filter of the parent. This has the
618 * side effect of causing the parent interface to receive multicast
619 * traffic that it doesn't really want, which ends up being discarded
620 * later by the upper protocol layers. Unfortunately, there's no way
621 * to avoid this: there really is only one physical interface.
624 vlan_setmulti(struct ifnet
*ifp
)
627 struct ifmultiaddr
*ifma
;
629 struct vlan_mc_entry
*mc
;
634 /* Find the parent. */
638 CURVNET_SET_QUIET(ifp_p
->if_vnet
);
640 /* First, remove any existing filter entries. */
641 while ((mc
= CK_SLIST_FIRST(&sc
->vlan_mc_listhead
)) != NULL
) {
642 CK_SLIST_REMOVE_HEAD(&sc
->vlan_mc_listhead
, mc_entries
);
643 (void)if_delmulti(ifp_p
, (struct sockaddr
*)&mc
->mc_addr
);
644 NET_EPOCH_CALL(vlan_mc_free
, &mc
->mc_epoch_ctx
);
647 /* Now program new ones. */
649 CK_STAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
650 if (ifma
->ifma_addr
->sa_family
!= AF_LINK
)
652 mc
= malloc(sizeof(struct vlan_mc_entry
), M_VLAN
, M_NOWAIT
);
654 IF_ADDR_WUNLOCK(ifp
);
658 bcopy(ifma
->ifma_addr
, &mc
->mc_addr
, ifma
->ifma_addr
->sa_len
);
659 mc
->mc_addr
.sdl_index
= ifp_p
->if_index
;
660 CK_SLIST_INSERT_HEAD(&sc
->vlan_mc_listhead
, mc
, mc_entries
);
662 IF_ADDR_WUNLOCK(ifp
);
663 CK_SLIST_FOREACH (mc
, &sc
->vlan_mc_listhead
, mc_entries
) {
664 error
= if_addmulti(ifp_p
, (struct sockaddr
*)&mc
->mc_addr
,
677 * A handler for interface ifnet events.
680 vlan_ifevent(void *arg __unused
, struct ifnet
*ifp
, int event
)
682 struct epoch_tracker et
;
684 struct ifvlantrunk
*trunk
;
686 if (event
!= IFNET_EVENT_UPDATE_BAUDRATE
)
690 trunk
= ifp
->if_vlantrunk
;
697 VLAN_FOREACH(ifv
, trunk
) {
698 ifv
->ifv_ifp
->if_baudrate
= ifp
->if_baudrate
;
700 TRUNK_WUNLOCK(trunk
);
705 * A handler for parent interface link layer address changes.
706 * If the parent interface link layer address is changed we
707 * should also change it on all children vlans.
710 vlan_iflladdr(void *arg __unused
, struct ifnet
*ifp
)
712 struct epoch_tracker et
;
714 struct ifnet
*ifv_ifp
;
715 struct ifvlantrunk
*trunk
;
716 struct sockaddr_dl
*sdl
;
718 /* Need the epoch since this is run on taskqueue_swi. */
720 trunk
= ifp
->if_vlantrunk
;
727 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
728 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
729 * ioctl calls on the parent garbling the lladdr of the child vlan.
732 VLAN_FOREACH(ifv
, trunk
) {
734 * Copy new new lladdr into the ifv_ifp, enqueue a task
735 * to actually call if_setlladdr. if_setlladdr needs to
736 * be deferred to a taskqueue because it will call into
737 * the if_vlan ioctl path and try to acquire the global
740 ifv_ifp
= ifv
->ifv_ifp
;
741 bcopy(IF_LLADDR(ifp
), IF_LLADDR(ifv_ifp
),
743 sdl
= (struct sockaddr_dl
*)ifv_ifp
->if_addr
->ifa_addr
;
744 sdl
->sdl_alen
= ifp
->if_addrlen
;
745 taskqueue_enqueue(taskqueue_thread
, &ifv
->lladdr_task
);
747 TRUNK_WUNLOCK(trunk
);
752 * A handler for network interface departure events.
753 * Track departure of trunks here so that we don't access invalid
754 * pointers or whatever if a trunk is ripped from under us, e.g.,
755 * by ejecting its hot-plug card. However, if an ifnet is simply
756 * being renamed, then there's no need to tear down the state.
759 vlan_ifdetach(void *arg __unused
, struct ifnet
*ifp
)
762 struct ifvlantrunk
*trunk
;
764 /* If the ifnet is just being renamed, don't do anything. */
765 if (ifp
->if_flags
& IFF_RENAMING
)
768 trunk
= ifp
->if_vlantrunk
;
775 * OK, it's a trunk. Loop over and detach all vlan's on it.
776 * Check trunk pointer after each vlan_unconfig() as it will
777 * free it and set to NULL after the last vlan was detached.
779 VLAN_FOREACH_UNTIL_SAFE(ifv
, ifp
->if_vlantrunk
,
780 ifp
->if_vlantrunk
== NULL
)
781 vlan_unconfig_locked(ifv
->ifv_ifp
, 1);
783 /* Trunk should have been destroyed in vlan_unconfig(). */
784 KASSERT(ifp
->if_vlantrunk
== NULL
, ("%s: purge failed", __func__
));
789 * Return the trunk device for a virtual interface.
791 static struct ifnet
*
792 vlan_trunkdev(struct ifnet
*ifp
)
798 if (ifp
->if_type
!= IFT_L2VLAN
)
809 * Return the 12-bit VLAN VID for this interface, for use by external
810 * components such as Infiniband.
812 * XXXRW: Note that the function name here is historical; it should be named
816 vlan_tag(struct ifnet
*ifp
, uint16_t *vidp
)
820 if (ifp
->if_type
!= IFT_L2VLAN
)
823 *vidp
= ifv
->ifv_vid
;
828 vlan_pcp(struct ifnet
*ifp
, uint16_t *pcpp
)
832 if (ifp
->if_type
!= IFT_L2VLAN
)
835 *pcpp
= ifv
->ifv_pcp
;
840 * Return a driver specific cookie for this interface. Synchronization
841 * with setcookie must be provided by the driver.
844 vlan_cookie(struct ifnet
*ifp
)
848 if (ifp
->if_type
!= IFT_L2VLAN
)
851 return (ifv
->ifv_cookie
);
855 * Store a cookie in our softc that drivers can use to store driver
856 * private per-instance data in.
859 vlan_setcookie(struct ifnet
*ifp
, void *cookie
)
863 if (ifp
->if_type
!= IFT_L2VLAN
)
866 ifv
->ifv_cookie
= cookie
;
871 * Return the vlan device present at the specific VID.
873 static struct ifnet
*
874 vlan_devat(struct ifnet
*ifp
, uint16_t vid
)
876 struct ifvlantrunk
*trunk
;
881 trunk
= ifp
->if_vlantrunk
;
885 ifv
= vlan_gethash(trunk
, vid
);
892 * VLAN support can be loaded as a module. The only place in the
893 * system that's intimately aware of this is ether_input. We hook
894 * into this code through vlan_input_p which is defined there and
895 * set here. No one else in the system should be aware of this so
896 * we use an explicit reference here.
898 extern void (*vlan_input_p
)(struct ifnet
*, struct mbuf
*);
900 /* For if_link_state_change() eyes only... */
901 extern void (*vlan_link_state_p
)(struct ifnet
*);
903 static struct if_clone_addreq_v2 vlan_addreq
= {
905 .match_f
= vlan_clone_match
,
906 .create_f
= vlan_clone_create
,
907 .destroy_f
= vlan_clone_destroy
,
908 .create_nl_f
= vlan_clone_create_nl
,
909 .modify_nl_f
= vlan_clone_modify_nl
,
910 .dump_nl_f
= vlan_clone_dump_nl
,
914 vlan_modevent(module_t mod
, int type
, void *data
)
919 ifdetach_tag
= EVENTHANDLER_REGISTER(ifnet_departure_event
,
920 vlan_ifdetach
, NULL
, EVENTHANDLER_PRI_ANY
);
921 if (ifdetach_tag
== NULL
)
923 iflladdr_tag
= EVENTHANDLER_REGISTER(iflladdr_event
,
924 vlan_iflladdr
, NULL
, EVENTHANDLER_PRI_ANY
);
925 if (iflladdr_tag
== NULL
)
927 ifevent_tag
= EVENTHANDLER_REGISTER(ifnet_event
,
928 vlan_ifevent
, NULL
, EVENTHANDLER_PRI_ANY
);
929 if (ifevent_tag
== NULL
)
932 vlan_input_p
= vlan_input
;
933 vlan_link_state_p
= vlan_link_state
;
934 vlan_trunk_cap_p
= vlan_trunk_capabilities
;
935 vlan_trunkdev_p
= vlan_trunkdev
;
936 vlan_cookie_p
= vlan_cookie
;
937 vlan_setcookie_p
= vlan_setcookie
;
938 vlan_tag_p
= vlan_tag
;
939 vlan_pcp_p
= vlan_pcp
;
940 vlan_devat_p
= vlan_devat
;
942 vlan_cloner
= ifc_attach_cloner(vlanname
, (struct if_clone_addreq
*)&vlan_addreq
);
945 printf("vlan: initialized, using "
949 "hash tables with chaining"
956 ifc_detach_cloner(vlan_cloner
);
958 EVENTHANDLER_DEREGISTER(ifnet_departure_event
, ifdetach_tag
);
959 EVENTHANDLER_DEREGISTER(iflladdr_event
, iflladdr_tag
);
960 EVENTHANDLER_DEREGISTER(ifnet_event
, ifevent_tag
);
962 vlan_link_state_p
= NULL
;
963 vlan_trunk_cap_p
= NULL
;
964 vlan_trunkdev_p
= NULL
;
966 vlan_cookie_p
= NULL
;
967 vlan_setcookie_p
= NULL
;
969 VLAN_LOCKING_DESTROY();
971 printf("vlan: unloaded\n");
979 static moduledata_t vlan_mod
= {
985 DECLARE_MODULE(if_vlan
, vlan_mod
, SI_SUB_PSEUDO
, SI_ORDER_ANY
);
986 MODULE_VERSION(if_vlan
, 3);
990 vnet_vlan_init(const void *unused __unused
)
992 vlan_cloner
= ifc_attach_cloner(vlanname
, (struct if_clone_addreq
*)&vlan_addreq
);
993 V_vlan_cloner
= vlan_cloner
;
995 VNET_SYSINIT(vnet_vlan_init
, SI_SUB_PROTO_IFATTACHDOMAIN
, SI_ORDER_ANY
,
996 vnet_vlan_init
, NULL
);
999 vnet_vlan_uninit(const void *unused __unused
)
1002 ifc_detach_cloner(V_vlan_cloner
);
1004 VNET_SYSUNINIT(vnet_vlan_uninit
, SI_SUB_INIT_IF
, SI_ORDER_ANY
,
1005 vnet_vlan_uninit
, NULL
);
1009 * Check for <etherif>.<vlan>[.<vlan> ...] style interface names.
1011 static struct ifnet
*
1012 vlan_clone_match_ethervid(const char *name
, int *vidp
)
1014 char ifname
[IFNAMSIZ
];
1019 strlcpy(ifname
, name
, IFNAMSIZ
);
1020 if ((cp
= strrchr(ifname
, '.')) == NULL
)
1023 if ((ifp
= ifunit_ref(ifname
)) == NULL
)
1026 if (*++cp
== '\0') {
1031 for(; *cp
>= '0' && *cp
<= '9'; cp
++)
1032 vid
= (vid
* 10) + (*cp
- '0');
1044 vlan_clone_match(struct if_clone
*ifc
, const char *name
)
1049 ifp
= vlan_clone_match_ethervid(name
, NULL
);
1055 if (strncmp(vlanname
, name
, strlen(vlanname
)) != 0)
1057 for (cp
= name
+ 4; *cp
!= '\0'; cp
++) {
1058 if (*cp
< '0' || *cp
> '9')
1066 vlan_clone_create(struct if_clone
*ifc
, char *name
, size_t len
,
1067 struct ifc_data
*ifd
, struct ifnet
**ifpp
)
1070 bool wildcard
= false;
1071 bool subinterface
= false;
1075 uint16_t proto
= ETHERTYPE_VLAN
;
1078 struct ifnet
*p
= NULL
;
1080 struct sockaddr_dl
*sdl
;
1082 static const u_char eaddr
[ETHER_ADDR_LEN
]; /* 00:00:00:00:00:00 */
1086 * There are three ways to specify the cloned device:
1087 * o pass a parameter block with the clone request.
1088 * o specify parameters in the text of the clone device name
1089 * o specify no parameters and get an unattached device that
1090 * must be configured separately.
1091 * The first technique is preferred; the latter two are supported
1092 * for backwards compatibility.
1094 * XXXRW: Note historic use of the word "tag" here. New ioctls may be
1098 if (ifd
->params
!= NULL
) {
1099 error
= ifc_copyin(ifd
, &vlr
, sizeof(vlr
));
1103 proto
= vlr
.vlr_proto
;
1105 proto
= ETHERTYPE_VLAN
;
1106 p
= ifunit_ref(vlr
.vlr_parent
);
1111 if ((error
= ifc_name2unit(name
, &unit
)) == 0) {
1114 * vlanX interface. Set wildcard to true if the unit number
1117 wildcard
= (unit
< 0);
1119 struct ifnet
*p_tmp
= vlan_clone_match_ethervid(name
, &vid
);
1120 if (p_tmp
!= NULL
) {
1122 subinterface
= true;
1123 unit
= IF_DUNIT_NONE
;
1141 if (!subinterface
) {
1142 /* vlanX interface, mark X as busy or allocate new unit # */
1143 error
= ifc_alloc_unit(ifc
, &unit
);
1151 /* In the wildcard case, we need to update the name. */
1153 for (dp
= name
; *dp
!= '\0'; dp
++);
1154 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
1155 len
- (dp
-name
) - 1) {
1156 panic("%s: interface name too long", __func__
);
1160 ifv
= malloc(sizeof(struct ifvlan
), M_VLAN
, M_WAITOK
| M_ZERO
);
1161 ifp
= ifv
->ifv_ifp
= if_alloc(IFT_ETHER
);
1162 CK_SLIST_INIT(&ifv
->vlan_mc_listhead
);
1163 ifp
->if_softc
= ifv
;
1165 * Set the name manually rather than using if_initname because
1166 * we don't conform to the default naming convention for interfaces.
1168 strlcpy(ifp
->if_xname
, name
, IFNAMSIZ
);
1169 ifp
->if_dname
= vlanname
;
1170 ifp
->if_dunit
= unit
;
1172 ifp
->if_init
= vlan_init
;
1174 ifp
->if_start
= vlan_altq_start
;
1175 ifp
->if_transmit
= vlan_altq_transmit
;
1176 IFQ_SET_MAXLEN(&ifp
->if_snd
, ifqmaxlen
);
1177 ifp
->if_snd
.ifq_drv_maxlen
= 0;
1178 IFQ_SET_READY(&ifp
->if_snd
);
1180 ifp
->if_transmit
= vlan_transmit
;
1182 ifp
->if_qflush
= vlan_qflush
;
1183 ifp
->if_ioctl
= vlan_ioctl
;
1184 #if defined(KERN_TLS) || defined(RATELIMIT)
1185 ifp
->if_snd_tag_alloc
= vlan_snd_tag_alloc
;
1186 ifp
->if_ratelimit_query
= vlan_ratelimit_query
;
1188 ifp
->if_flags
= VLAN_IFFLAGS
;
1189 ether_ifattach(ifp
, eaddr
);
1190 /* Now undo some of the damage... */
1191 ifp
->if_baudrate
= 0;
1192 ifp
->if_type
= IFT_L2VLAN
;
1193 ifp
->if_hdrlen
= ETHER_VLAN_ENCAP_LEN
;
1195 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1196 sdl
->sdl_type
= IFT_L2VLAN
;
1199 error
= vlan_config(ifv
, p
, vid
, proto
);
1203 * Since we've partially failed, we need to back
1204 * out all the way, otherwise userland could get
1205 * confused. Thus, we destroy the interface.
1207 ether_ifdetach(ifp
);
1211 ifc_free_unit(ifc
, unit
);
1224 * Parsers of IFLA_INFO_DATA inside IFLA_LINKINFO of RTM_NEWLINK
1225 * {{nla_len=8, nla_type=IFLA_LINK}, 2},
1226 * {{nla_len=12, nla_type=IFLA_IFNAME}, "xvlan22"},
1227 * {{nla_len=24, nla_type=IFLA_LINKINFO},
1229 * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
1230 * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]}
1233 struct nl_parsed_vlan
{
1235 uint16_t vlan_proto
;
1236 struct ifla_vlan_flags vlan_flags
;
1239 #define _OUT(_field) offsetof(struct nl_parsed_vlan, _field)
1240 static const struct nlattr_parser nla_p_vlan
[] = {
1241 { .type
= IFLA_VLAN_ID
, .off
= _OUT(vlan_id
), .cb
= nlattr_get_uint16
},
1242 { .type
= IFLA_VLAN_FLAGS
, .off
= _OUT(vlan_flags
), .cb
= nlattr_get_nla
},
1243 { .type
= IFLA_VLAN_PROTOCOL
, .off
= _OUT(vlan_proto
), .cb
= nlattr_get_uint16
},
1246 NL_DECLARE_ATTR_PARSER(vlan_parser
, nla_p_vlan
);
1249 vlan_clone_create_nl(struct if_clone
*ifc
, char *name
, size_t len
,
1250 struct ifc_data_nl
*ifd
)
1252 struct epoch_tracker et
;
1253 struct ifnet
*ifp_parent
;
1254 struct nl_pstate
*npt
= ifd
->npt
;
1255 struct nl_parsed_link
*lattrs
= ifd
->lattrs
;
1259 * lattrs.ifla_ifname is the new interface name
1260 * lattrs.ifi_index contains parent interface index
1261 * lattrs.ifla_idata contains un-parsed vlan data
1263 struct nl_parsed_vlan attrs
= {
1265 .vlan_proto
= ETHERTYPE_VLAN
1268 if (lattrs
->ifla_idata
== NULL
) {
1269 nlmsg_report_err_msg(npt
, "vlan id is required, guessing not supported");
1273 error
= nl_parse_nested(lattrs
->ifla_idata
, &vlan_parser
, npt
, &attrs
);
1276 if (attrs
.vlan_id
> 4095) {
1277 nlmsg_report_err_msg(npt
, "Invalid VID: %d", attrs
.vlan_id
);
1280 if (attrs
.vlan_proto
!= ETHERTYPE_VLAN
&& attrs
.vlan_proto
!= ETHERTYPE_QINQ
) {
1281 nlmsg_report_err_msg(npt
, "Unsupported ethertype: 0x%04X", attrs
.vlan_proto
);
1285 struct vlanreq params
= {
1286 .vlr_tag
= attrs
.vlan_id
,
1287 .vlr_proto
= attrs
.vlan_proto
,
1289 struct ifc_data ifd_new
= { .flags
= IFC_F_SYSSPACE
, .unit
= ifd
->unit
, .params
= ¶ms
};
1291 NET_EPOCH_ENTER(et
);
1292 ifp_parent
= ifnet_byindex(lattrs
->ifi_index
);
1293 if (ifp_parent
!= NULL
)
1294 strlcpy(params
.vlr_parent
, if_name(ifp_parent
), sizeof(params
.vlr_parent
));
1297 if (ifp_parent
== NULL
) {
1298 nlmsg_report_err_msg(npt
, "unable to find parent interface %u", lattrs
->ifi_index
);
1302 error
= vlan_clone_create(ifc
, name
, len
, &ifd_new
, &ifd
->ifp
);
1308 vlan_clone_modify_nl(struct ifnet
*ifp
, struct ifc_data_nl
*ifd
)
1310 struct nl_parsed_link
*lattrs
= ifd
->lattrs
;
1312 if ((lattrs
->ifla_idata
!= NULL
) && ((ifd
->flags
& IFC_F_CREATE
) == 0)) {
1313 struct epoch_tracker et
;
1314 struct nl_parsed_vlan attrs
= {
1315 .vlan_proto
= ETHERTYPE_VLAN
,
1319 error
= nl_parse_nested(lattrs
->ifla_idata
, &vlan_parser
, ifd
->npt
, &attrs
);
1323 NET_EPOCH_ENTER(et
);
1324 struct ifnet
*ifp_parent
= ifnet_byindex_ref(lattrs
->ifla_link
);
1327 if (ifp_parent
== NULL
) {
1328 nlmsg_report_err_msg(ifd
->npt
, "unable to find parent interface %u",
1333 struct ifvlan
*ifv
= ifp
->if_softc
;
1334 error
= vlan_config(ifv
, ifp_parent
, attrs
.vlan_id
, attrs
.vlan_proto
);
1336 if_rele(ifp_parent
);
1341 return (nl_modify_ifp_generic(ifp
, ifd
->lattrs
, ifd
->bm
, ifd
->npt
));
1345 * {{nla_len=24, nla_type=IFLA_LINKINFO},
1347 * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
1348 * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]}
1351 vlan_clone_dump_nl(struct ifnet
*ifp
, struct nl_writer
*nw
)
1353 uint32_t parent_index
= 0;
1354 uint16_t vlan_id
= 0;
1355 uint16_t vlan_proto
= 0;
1358 struct ifvlan
*ifv
= ifp
->if_softc
;
1359 if (TRUNK(ifv
) != NULL
)
1360 parent_index
= PARENT(ifv
)->if_index
;
1361 vlan_id
= ifv
->ifv_vid
;
1362 vlan_proto
= ifv
->ifv_proto
;
1365 if (parent_index
!= 0)
1366 nlattr_add_u32(nw
, IFLA_LINK
, parent_index
);
1368 int off
= nlattr_add_nested(nw
, IFLA_LINKINFO
);
1370 nlattr_add_string(nw
, IFLA_INFO_KIND
, "vlan");
1371 int off2
= nlattr_add_nested(nw
, IFLA_INFO_DATA
);
1373 nlattr_add_u16(nw
, IFLA_VLAN_ID
, vlan_id
);
1374 nlattr_add_u16(nw
, IFLA_VLAN_PROTOCOL
, vlan_proto
);
1375 nlattr_set_len(nw
, off2
);
1377 nlattr_set_len(nw
, off
);
1382 vlan_clone_destroy(struct if_clone
*ifc
, struct ifnet
*ifp
, uint32_t flags
)
1384 struct ifvlan
*ifv
= ifp
->if_softc
;
1385 int unit
= ifp
->if_dunit
;
1387 if (ifp
->if_vlantrunk
)
1391 IFQ_PURGE(&ifp
->if_snd
);
1393 ether_ifdetach(ifp
); /* first, remove it from system-wide lists */
1394 vlan_unconfig(ifp
); /* now it can be unconfigured and freed */
1396 * We should have the only reference to the ifv now, so we can now
1397 * drain any remaining lladdr task before freeing the ifnet and the
1400 taskqueue_drain(taskqueue_thread
, &ifv
->lladdr_task
);
1404 if (unit
!= IF_DUNIT_NONE
)
1405 ifc_free_unit(ifc
, unit
);
1411 * The ifp->if_init entry point for vlan(4) is a no-op.
1414 vlan_init(void *foo __unused
)
1419 * The if_transmit method for vlan(4) interface.
1422 vlan_transmit(struct ifnet
*ifp
, struct mbuf
*m
)
1426 int error
, len
, mcast
;
1430 ifv
= ifp
->if_softc
;
1431 if (TRUNK(ifv
) == NULL
) {
1432 if_inc_counter(ifp
, IFCOUNTER_OERRORS
, 1);
1437 len
= m
->m_pkthdr
.len
;
1438 mcast
= (m
->m_flags
& (M_MCAST
| M_BCAST
)) ? 1 : 0;
1442 #if defined(KERN_TLS) || defined(RATELIMIT)
1443 if (m
->m_pkthdr
.csum_flags
& CSUM_SND_TAG
) {
1444 struct vlan_snd_tag
*vst
;
1445 struct m_snd_tag
*mst
;
1447 MPASS(m
->m_pkthdr
.snd_tag
->ifp
== ifp
);
1448 mst
= m
->m_pkthdr
.snd_tag
;
1449 vst
= mst_to_vst(mst
);
1450 if (vst
->tag
->ifp
!= p
) {
1451 if_inc_counter(ifp
, IFCOUNTER_OERRORS
, 1);
1456 m
->m_pkthdr
.snd_tag
= m_snd_tag_ref(vst
->tag
);
1457 m_snd_tag_rele(mst
);
1462 * Do not run parent's if_transmit() if the parent is not up,
1463 * or parent's driver will cause a system crash.
1465 if (!UP_AND_RUNNING(p
)) {
1466 if_inc_counter(ifp
, IFCOUNTER_OERRORS
, 1);
1471 if (!ether_8021q_frame(&m
, ifp
, p
, &ifv
->ifv_qtag
)) {
1472 if_inc_counter(ifp
, IFCOUNTER_OERRORS
, 1);
1477 * Send it, precisely as ether_output() would have.
1479 error
= (p
->if_transmit
)(p
, m
);
1481 if_inc_counter(ifp
, IFCOUNTER_OPACKETS
, 1);
1482 if_inc_counter(ifp
, IFCOUNTER_OBYTES
, len
);
1483 if_inc_counter(ifp
, IFCOUNTER_OMCASTS
, mcast
);
1485 if_inc_counter(ifp
, IFCOUNTER_OERRORS
, 1);
1490 vlan_output(struct ifnet
*ifp
, struct mbuf
*m
, const struct sockaddr
*dst
,
1499 * Find the first non-VLAN parent interface.
1501 ifv
= ifp
->if_softc
;
1503 if (TRUNK(ifv
) == NULL
) {
1509 } while (p
->if_type
== IFT_L2VLAN
);
1511 return p
->if_output(ifp
, m
, dst
, ro
);
1516 vlan_altq_start(if_t ifp
)
1518 struct ifaltq
*ifq
= &ifp
->if_snd
;
1522 IFQ_DEQUEUE_NOLOCK(ifq
, m
);
1524 vlan_transmit(ifp
, m
);
1525 IFQ_DEQUEUE_NOLOCK(ifq
, m
);
1531 vlan_altq_transmit(if_t ifp
, struct mbuf
*m
)
1535 if (ALTQ_IS_ENABLED(&ifp
->if_snd
)) {
1536 IFQ_ENQUEUE(&ifp
->if_snd
, m
, err
);
1538 vlan_altq_start(ifp
);
1540 err
= vlan_transmit(ifp
, m
);
1547 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1550 vlan_qflush(struct ifnet
*ifp __unused
)
1555 vlan_input(struct ifnet
*ifp
, struct mbuf
*m
)
1557 struct ifvlantrunk
*trunk
;
1564 trunk
= ifp
->if_vlantrunk
;
1565 if (trunk
== NULL
) {
1570 if (m
->m_flags
& M_VLANTAG
) {
1572 * Packet is tagged, but m contains a normal
1573 * Ethernet frame; the tag is stored out-of-band.
1575 tag
= m
->m_pkthdr
.ether_vtag
;
1576 m
->m_flags
&= ~M_VLANTAG
;
1578 struct ether_vlan_header
*evl
;
1581 * Packet is tagged in-band as specified by 802.1q.
1583 switch (ifp
->if_type
) {
1585 if (m
->m_len
< sizeof(*evl
) &&
1586 (m
= m_pullup(m
, sizeof(*evl
))) == NULL
) {
1587 if_printf(ifp
, "cannot pullup VLAN header\n");
1590 evl
= mtod(m
, struct ether_vlan_header
*);
1591 tag
= ntohs(evl
->evl_tag
);
1594 * Remove the 802.1q header by copying the Ethernet
1595 * addresses over it and adjusting the beginning of
1596 * the data in the mbuf. The encapsulated Ethernet
1597 * type field is already in place.
1599 bcopy((char *)evl
, (char *)evl
+ ETHER_VLAN_ENCAP_LEN
,
1600 ETHER_HDR_LEN
- ETHER_TYPE_LEN
);
1601 m_adj(m
, ETHER_VLAN_ENCAP_LEN
);
1606 panic("%s: %s has unsupported if_type %u",
1607 __func__
, ifp
->if_xname
, ifp
->if_type
);
1609 if_inc_counter(ifp
, IFCOUNTER_NOPROTO
, 1);
1615 vid
= EVL_VLANOFTAG(tag
);
1617 ifv
= vlan_gethash(trunk
, vid
);
1618 if (ifv
== NULL
|| !UP_AND_RUNNING(ifv
->ifv_ifp
)) {
1619 if_inc_counter(ifp
, IFCOUNTER_NOPROTO
, 1);
1624 if (V_vlan_mtag_pcp
) {
1626 * While uncommon, it is possible that we will find a 802.1q
1627 * packet encapsulated inside another packet that also had an
1628 * 802.1q header. For example, ethernet tunneled over IPSEC
1629 * arriving over ethernet. In that case, we replace the
1630 * existing 802.1q PCP m_tag value.
1632 mtag
= m_tag_locate(m
, MTAG_8021Q
, MTAG_8021Q_PCP_IN
, NULL
);
1634 mtag
= m_tag_alloc(MTAG_8021Q
, MTAG_8021Q_PCP_IN
,
1635 sizeof(uint8_t), M_NOWAIT
);
1637 if_inc_counter(ifp
, IFCOUNTER_IERRORS
, 1);
1641 m_tag_prepend(m
, mtag
);
1643 *(uint8_t *)(mtag
+ 1) = EVL_PRIOFTAG(tag
);
1646 m
->m_pkthdr
.rcvif
= ifv
->ifv_ifp
;
1647 if_inc_counter(ifv
->ifv_ifp
, IFCOUNTER_IPACKETS
, 1);
1649 /* Pass it back through the parent's input routine. */
1650 (*ifv
->ifv_ifp
->if_input
)(ifv
->ifv_ifp
, m
);
1654 vlan_lladdr_fn(void *arg
, int pending __unused
)
1659 ifv
= (struct ifvlan
*)arg
;
1662 CURVNET_SET(ifp
->if_vnet
);
1664 /* The ifv_ifp already has the lladdr copied in. */
1665 if_setlladdr(ifp
, IF_LLADDR(ifp
), ifp
->if_addrlen
);
1671 vlan_config(struct ifvlan
*ifv
, struct ifnet
*p
, uint16_t vid
,
1674 struct epoch_tracker et
;
1675 struct ifvlantrunk
*trunk
;
1680 * We can handle non-ethernet hardware types as long as
1681 * they handle the tagging and headers themselves.
1683 if (p
->if_type
!= IFT_ETHER
&&
1684 p
->if_type
!= IFT_L2VLAN
&&
1685 (p
->if_capenable
& IFCAP_VLAN_HWTAGGING
) == 0)
1686 return (EPROTONOSUPPORT
);
1687 if ((p
->if_flags
& VLAN_IFFLAGS
) != VLAN_IFFLAGS
)
1688 return (EPROTONOSUPPORT
);
1690 * Don't let the caller set up a VLAN VID with
1691 * anything except VLID bits.
1692 * VID numbers 0x0 and 0xFFF are reserved.
1694 if (vid
== 0 || vid
== 0xFFF || (vid
& ~EVL_VLID_MASK
))
1696 if (ifv
->ifv_trunk
) {
1697 trunk
= ifv
->ifv_trunk
;
1698 if (trunk
->parent
!= p
)
1703 ifv
->ifv_proto
= proto
;
1705 if (ifv
->ifv_vid
!= vid
) {
1706 int oldvid
= ifv
->ifv_vid
;
1709 vlan_remhash(trunk
, ifv
);
1711 error
= vlan_inshash(trunk
, ifv
);
1715 ifv
->ifv_vid
= oldvid
;
1716 /* Re-insert back where we found it. */
1717 ret
= vlan_inshash(trunk
, ifv
);
1726 if (p
->if_vlantrunk
== NULL
) {
1727 trunk
= malloc(sizeof(struct ifvlantrunk
),
1728 M_VLAN
, M_WAITOK
| M_ZERO
);
1729 vlan_inithash(trunk
);
1730 TRUNK_LOCK_INIT(trunk
);
1732 p
->if_vlantrunk
= trunk
;
1734 if_ref(trunk
->parent
);
1735 TRUNK_WUNLOCK(trunk
);
1737 trunk
= p
->if_vlantrunk
;
1740 ifv
->ifv_vid
= vid
; /* must set this before vlan_inshash() */
1741 ifv
->ifv_pcp
= 0; /* Default: best effort delivery. */
1742 error
= vlan_inshash(trunk
, ifv
);
1745 ifv
->ifv_proto
= proto
;
1746 ifv
->ifv_encaplen
= ETHER_VLAN_ENCAP_LEN
;
1747 ifv
->ifv_mintu
= ETHERMIN
;
1748 ifv
->ifv_pflags
= 0;
1749 ifv
->ifv_capenable
= -1;
1750 ifv
->ifv_capenable2
= -1;
1753 * If the parent supports the VLAN_MTU capability,
1754 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1757 if (p
->if_capenable
& IFCAP_VLAN_MTU
) {
1759 * No need to fudge the MTU since the parent can
1760 * handle extended frames.
1762 ifv
->ifv_mtufudge
= 0;
1765 * Fudge the MTU by the encapsulation size. This
1766 * makes us incompatible with strictly compliant
1767 * 802.1Q implementations, but allows us to use
1768 * the feature with other NetBSD implementations,
1769 * which might still be useful.
1771 ifv
->ifv_mtufudge
= ifv
->ifv_encaplen
;
1774 ifv
->ifv_trunk
= trunk
;
1777 * Initialize fields from our parent. This duplicates some
1778 * work with ether_ifattach() but allows for non-ethernet
1779 * interfaces to also work.
1781 ifp
->if_mtu
= p
->if_mtu
- ifv
->ifv_mtufudge
;
1782 ifp
->if_baudrate
= p
->if_baudrate
;
1783 ifp
->if_input
= p
->if_input
;
1784 ifp
->if_resolvemulti
= p
->if_resolvemulti
;
1785 ifp
->if_addrlen
= p
->if_addrlen
;
1786 ifp
->if_broadcastaddr
= p
->if_broadcastaddr
;
1787 ifp
->if_pcp
= ifv
->ifv_pcp
;
1790 * We wrap the parent's if_output using vlan_output to ensure that it
1791 * can't become stale.
1793 ifp
->if_output
= vlan_output
;
1796 * Copy only a selected subset of flags from the parent.
1797 * Other flags are none of our business.
1799 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1800 ifp
->if_flags
&= ~VLAN_COPY_FLAGS
;
1801 ifp
->if_flags
|= p
->if_flags
& VLAN_COPY_FLAGS
;
1802 #undef VLAN_COPY_FLAGS
1804 ifp
->if_link_state
= p
->if_link_state
;
1806 NET_EPOCH_ENTER(et
);
1807 vlan_capabilities(ifv
);
1811 * Set up our interface address to reflect the underlying
1812 * physical interface's.
1814 TASK_INIT(&ifv
->lladdr_task
, 0, vlan_lladdr_fn
, ifv
);
1815 ((struct sockaddr_dl
*)ifp
->if_addr
->ifa_addr
)->sdl_alen
=
1819 * Do not schedule link address update if it was the same
1820 * as previous parent's. This helps avoid updating for each
1821 * associated llentry.
1823 if (memcmp(IF_LLADDR(p
), IF_LLADDR(ifp
), p
->if_addrlen
) != 0) {
1824 bcopy(IF_LLADDR(p
), IF_LLADDR(ifp
), p
->if_addrlen
);
1825 taskqueue_enqueue(taskqueue_thread
, &ifv
->lladdr_task
);
1828 /* We are ready for operation now. */
1829 ifp
->if_drv_flags
|= IFF_DRV_RUNNING
;
1831 /* Update flags on the parent, if necessary. */
1832 vlan_setflags(ifp
, 1);
1835 * Configure multicast addresses that may already be
1836 * joined on the vlan device.
1838 (void)vlan_setmulti(ifp
);
1842 EVENTHANDLER_INVOKE(vlan_config
, p
, ifv
->ifv_vid
);
1849 vlan_unconfig(struct ifnet
*ifp
)
1853 vlan_unconfig_locked(ifp
, 0);
1858 vlan_unconfig_locked(struct ifnet
*ifp
, int departing
)
1860 struct ifvlantrunk
*trunk
;
1861 struct vlan_mc_entry
*mc
;
1863 struct ifnet
*parent
;
1866 VLAN_XLOCK_ASSERT();
1868 ifv
= ifp
->if_softc
;
1869 trunk
= ifv
->ifv_trunk
;
1872 if (trunk
!= NULL
) {
1873 parent
= trunk
->parent
;
1876 * Since the interface is being unconfigured, we need to
1877 * empty the list of multicast groups that we may have joined
1878 * while we were alive from the parent's list.
1880 while ((mc
= CK_SLIST_FIRST(&ifv
->vlan_mc_listhead
)) != NULL
) {
1882 * If the parent interface is being detached,
1883 * all its multicast addresses have already
1884 * been removed. Warn about errors if
1885 * if_delmulti() does fail, but don't abort as
1886 * all callers expect vlan destruction to
1890 error
= if_delmulti(parent
,
1891 (struct sockaddr
*)&mc
->mc_addr
);
1894 "Failed to delete multicast address from parent: %d\n",
1897 CK_SLIST_REMOVE_HEAD(&ifv
->vlan_mc_listhead
, mc_entries
);
1898 NET_EPOCH_CALL(vlan_mc_free
, &mc
->mc_epoch_ctx
);
1901 vlan_setflags(ifp
, 0); /* clear special flags on parent */
1903 vlan_remhash(trunk
, ifv
);
1904 ifv
->ifv_trunk
= NULL
;
1907 * Check if we were the last.
1909 if (trunk
->refcnt
== 0) {
1910 parent
->if_vlantrunk
= NULL
;
1912 trunk_destroy(trunk
);
1916 /* Disconnect from parent. */
1917 if (ifv
->ifv_pflags
)
1918 if_printf(ifp
, "%s: ifv_pflags unclean\n", __func__
);
1919 ifp
->if_mtu
= ETHERMTU
;
1920 ifp
->if_link_state
= LINK_STATE_UNKNOWN
;
1921 ifp
->if_drv_flags
&= ~IFF_DRV_RUNNING
;
1924 * Only dispatch an event if vlan was
1925 * attached, otherwise there is nothing
1926 * to cleanup anyway.
1929 EVENTHANDLER_INVOKE(vlan_unconfig
, parent
, ifv
->ifv_vid
);
1932 /* Handle a reference counted flag that should be set on the parent as well */
1934 vlan_setflag(struct ifnet
*ifp
, int flag
, int status
,
1935 int (*func
)(struct ifnet
*, int))
1940 VLAN_SXLOCK_ASSERT();
1942 ifv
= ifp
->if_softc
;
1943 status
= status
? (ifp
->if_flags
& flag
) : 0;
1944 /* Now "status" contains the flag value or 0 */
1947 * See if recorded parent's status is different from what
1948 * we want it to be. If it is, flip it. We record parent's
1949 * status in ifv_pflags so that we won't clear parent's flag
1950 * we haven't set. In fact, we don't clear or set parent's
1951 * flags directly, but get or release references to them.
1952 * That's why we can be sure that recorded flags still are
1953 * in accord with actual parent's flags.
1955 if (status
!= (ifv
->ifv_pflags
& flag
)) {
1956 error
= (*func
)(PARENT(ifv
), status
);
1959 ifv
->ifv_pflags
&= ~flag
;
1960 ifv
->ifv_pflags
|= status
;
1966 * Handle IFF_* flags that require certain changes on the parent:
1967 * if "status" is true, update parent's flags respective to our if_flags;
1968 * if "status" is false, forcedly clear the flags set on parent.
1971 vlan_setflags(struct ifnet
*ifp
, int status
)
1975 for (i
= 0; vlan_pflags
[i
].flag
; i
++) {
1976 error
= vlan_setflag(ifp
, vlan_pflags
[i
].flag
,
1977 status
, vlan_pflags
[i
].func
);
1984 /* Inform all vlans that their parent has changed link state */
1986 vlan_link_state(struct ifnet
*ifp
)
1988 struct epoch_tracker et
;
1989 struct ifvlantrunk
*trunk
;
1992 NET_EPOCH_ENTER(et
);
1993 trunk
= ifp
->if_vlantrunk
;
1994 if (trunk
== NULL
) {
2000 VLAN_FOREACH(ifv
, trunk
) {
2001 ifv
->ifv_ifp
->if_baudrate
= trunk
->parent
->if_baudrate
;
2002 if_link_state_change(ifv
->ifv_ifp
,
2003 trunk
->parent
->if_link_state
);
2005 TRUNK_WUNLOCK(trunk
);
2009 #ifdef IPSEC_OFFLOAD
2010 #define VLAN_IPSEC_METHOD(exp) \
2012 struct ifvlan *ifv; \
2015 ifv = ifp->if_softc; \
2017 if (TRUNK(ifv) != NULL) { \
2020 error = p->if_ipsec_accel_m->exp; \
2030 vlan_if_spdadd(if_t ifp
, void *sp
, void *inp
, void **priv
)
2032 VLAN_IPSEC_METHOD(if_spdadd(ifp
, sp
, inp
, priv
));
2036 vlan_if_spddel(if_t ifp
, void *sp
, void *priv
)
2038 VLAN_IPSEC_METHOD(if_spddel(ifp
, sp
, priv
));
2042 vlan_if_sa_newkey(if_t ifp
, void *sav
, u_int drv_spi
, void **privp
)
2044 VLAN_IPSEC_METHOD(if_sa_newkey(ifp
, sav
, drv_spi
, privp
));
2048 vlan_if_sa_deinstall(if_t ifp
, u_int drv_spi
, void *priv
)
2050 VLAN_IPSEC_METHOD(if_sa_deinstall(ifp
, drv_spi
, priv
));
2054 vlan_if_sa_cnt(if_t ifp
, void *sa
, uint32_t drv_spi
, void *priv
,
2055 struct seclifetime
*lt
)
2057 VLAN_IPSEC_METHOD(if_sa_cnt(ifp
, sa
, drv_spi
, priv
, lt
));
2061 vlan_if_ipsec_hwassist(if_t ifp
, void *sav
, u_int drv_spi
,void *priv
)
2066 trunk
= vlan_trunkdev(ifp
);
2069 return (trunk
->if_ipsec_accel_m
->if_hwassist(trunk
, sav
,
2073 static const struct if_ipsec_accel_methods vlan_if_ipsec_accel_methods
= {
2074 .if_spdadd
= vlan_if_spdadd
,
2075 .if_spddel
= vlan_if_spddel
,
2076 .if_sa_newkey
= vlan_if_sa_newkey
,
2077 .if_sa_deinstall
= vlan_if_sa_deinstall
,
2078 .if_sa_cnt
= vlan_if_sa_cnt
,
2079 .if_hwassist
= vlan_if_ipsec_hwassist
,
2082 #undef VLAN_IPSEC_METHOD
2083 #endif /* IPSEC_OFFLOAD */
2086 vlan_capabilities(struct ifvlan
*ifv
)
2090 struct ifnet_hw_tsomax hw_tsomax
;
2091 int cap
= 0, ena
= 0, mena
, cap2
= 0, ena2
= 0;
2096 VLAN_SXLOCK_ASSERT();
2101 /* Mask parent interface enabled capabilities disabled by user. */
2102 mena
= p
->if_capenable
& ifv
->ifv_capenable
;
2103 mena2
= p
->if_capenable2
& ifv
->ifv_capenable2
;
2106 * If the parent interface can do checksum offloading
2107 * on VLANs, then propagate its hardware-assisted
2108 * checksumming flags. Also assert that checksum
2109 * offloading requires hardware VLAN tagging.
2111 if (p
->if_capabilities
& IFCAP_VLAN_HWCSUM
)
2112 cap
|= p
->if_capabilities
& (IFCAP_HWCSUM
| IFCAP_HWCSUM_IPV6
);
2113 if (p
->if_capenable
& IFCAP_VLAN_HWCSUM
&&
2114 p
->if_capenable
& IFCAP_VLAN_HWTAGGING
) {
2115 ena
|= mena
& (IFCAP_HWCSUM
| IFCAP_HWCSUM_IPV6
);
2116 if (ena
& IFCAP_TXCSUM
)
2117 hwa
|= p
->if_hwassist
& (CSUM_IP
| CSUM_TCP
|
2118 CSUM_UDP
| CSUM_SCTP
);
2119 if (ena
& IFCAP_TXCSUM_IPV6
)
2120 hwa
|= p
->if_hwassist
& (CSUM_TCP_IPV6
|
2121 CSUM_UDP_IPV6
| CSUM_SCTP_IPV6
);
2125 * If the parent interface can do TSO on VLANs then
2126 * propagate the hardware-assisted flag. TSO on VLANs
2127 * does not necessarily require hardware VLAN tagging.
2129 memset(&hw_tsomax
, 0, sizeof(hw_tsomax
));
2130 if_hw_tsomax_common(p
, &hw_tsomax
);
2131 if_hw_tsomax_update(ifp
, &hw_tsomax
);
2132 if (p
->if_capabilities
& IFCAP_VLAN_HWTSO
)
2133 cap
|= p
->if_capabilities
& IFCAP_TSO
;
2134 if (p
->if_capenable
& IFCAP_VLAN_HWTSO
) {
2135 ena
|= mena
& IFCAP_TSO
;
2136 if (ena
& IFCAP_TSO
)
2137 hwa
|= p
->if_hwassist
& CSUM_TSO
;
2141 * If the parent interface can do LRO and checksum offloading on
2142 * VLANs, then guess it may do LRO on VLANs. False positive here
2143 * cost nothing, while false negative may lead to some confusions.
2145 if (p
->if_capabilities
& IFCAP_VLAN_HWCSUM
)
2146 cap
|= p
->if_capabilities
& IFCAP_LRO
;
2147 if (p
->if_capenable
& IFCAP_VLAN_HWCSUM
)
2148 ena
|= mena
& IFCAP_LRO
;
2151 * If the parent interface can offload TCP connections over VLANs then
2152 * propagate its TOE capability to the VLAN interface.
2154 * All TOE drivers in the tree today can deal with VLANs. If this
2155 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
2158 #define IFCAP_VLAN_TOE IFCAP_TOE
2159 if (p
->if_capabilities
& IFCAP_VLAN_TOE
)
2160 cap
|= p
->if_capabilities
& IFCAP_TOE
;
2161 if (p
->if_capenable
& IFCAP_VLAN_TOE
) {
2162 SETTOEDEV(ifp
, TOEDEV(p
));
2163 ena
|= mena
& IFCAP_TOE
;
2167 * If the parent interface supports dynamic link state, so does the
2170 cap
|= (p
->if_capabilities
& IFCAP_LINKSTATE
);
2171 ena
|= (mena
& IFCAP_LINKSTATE
);
2175 * If the parent interface supports ratelimiting, so does the
2178 cap
|= (p
->if_capabilities
& IFCAP_TXRTLMT
);
2179 ena
|= (mena
& IFCAP_TXRTLMT
);
2183 * If the parent interface supports unmapped mbufs, so does
2184 * the VLAN interface. Note that this should be fine even for
2185 * interfaces that don't support hardware tagging as headers
2186 * are prepended in normal mbufs to unmapped mbufs holding
2189 cap
|= (p
->if_capabilities
& IFCAP_MEXTPG
);
2190 ena
|= (mena
& IFCAP_MEXTPG
);
2193 * If the parent interface can offload encryption and segmentation
2194 * of TLS records over TCP, propagate it's capability to the VLAN
2197 * All TLS drivers in the tree today can deal with VLANs. If
2198 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
2201 if (p
->if_capabilities
& (IFCAP_TXTLS
| IFCAP_TXTLS_RTLMT
))
2202 cap
|= p
->if_capabilities
& (IFCAP_TXTLS
| IFCAP_TXTLS_RTLMT
);
2203 if (p
->if_capenable
& (IFCAP_TXTLS
| IFCAP_TXTLS_RTLMT
))
2204 ena
|= mena
& (IFCAP_TXTLS
| IFCAP_TXTLS_RTLMT
);
2206 ifp
->if_capabilities
= cap
;
2207 ifp
->if_capenable
= ena
;
2208 ifp
->if_hwassist
= hwa
;
2210 #ifdef IPSEC_OFFLOAD
2211 cap2
|= p
->if_capabilities2
& IFCAP2_BIT(IFCAP2_IPSEC_OFFLOAD
);
2212 ena2
|= mena2
& IFCAP2_BIT(IFCAP2_IPSEC_OFFLOAD
);
2213 ifp
->if_ipsec_accel_m
= &vlan_if_ipsec_accel_methods
;
2216 ifp
->if_capabilities2
= cap2
;
2217 ifp
->if_capenable2
= ena2
;
2221 vlan_trunk_capabilities(struct ifnet
*ifp
)
2223 struct epoch_tracker et
;
2224 struct ifvlantrunk
*trunk
;
2228 trunk
= ifp
->if_vlantrunk
;
2229 if (trunk
== NULL
) {
2233 NET_EPOCH_ENTER(et
);
2234 VLAN_FOREACH(ifv
, trunk
)
2235 vlan_capabilities(ifv
);
2241 vlan_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2249 struct ifvlantrunk
*trunk
;
2251 int error
= 0, oldmtu
;
2253 ifr
= (struct ifreq
*)data
;
2255 ifa
= (struct ifaddr
*) data
;
2257 ifv
= ifp
->if_softc
;
2261 ifp
->if_flags
|= IFF_UP
;
2263 if (ifa
->ifa_addr
->sa_family
== AF_INET
)
2264 arp_ifinit(ifp
, ifa
);
2268 bcopy(IF_LLADDR(ifp
), &ifr
->ifr_addr
.sa_data
[0],
2273 if (TRUNK(ifv
) != NULL
) {
2276 error
= (*p
->if_ioctl
)(p
, SIOCGIFMEDIA
, data
);
2278 /* Limit the result to the parent's current config. */
2280 struct ifmediareq
*ifmr
;
2282 ifmr
= (struct ifmediareq
*)data
;
2283 if (ifmr
->ifm_count
>= 1 && ifmr
->ifm_ulist
) {
2284 ifmr
->ifm_count
= 1;
2285 error
= copyout(&ifmr
->ifm_current
,
2302 * Set the interface MTU.
2306 if (trunk
!= NULL
) {
2309 (PARENT(ifv
)->if_mtu
- ifv
->ifv_mtufudge
) ||
2311 (ifv
->ifv_mintu
- ifv
->ifv_mtufudge
))
2314 ifp
->if_mtu
= ifr
->ifr_mtu
;
2315 TRUNK_WUNLOCK(trunk
);
2324 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
2325 * interface to be delegated to a jail without allowing the
2326 * jail to change what underlying interface/VID it is
2327 * associated with. We are not entirely convinced that this
2328 * is the right way to accomplish that policy goal.
2330 if (ifp
->if_vnet
!= ifp
->if_home_vnet
) {
2335 error
= copyin(ifr_data_get_ptr(ifr
), &vlr
, sizeof(vlr
));
2338 if (vlr
.vlr_parent
[0] == '\0') {
2342 p
= ifunit_ref(vlr
.vlr_parent
);
2347 if (vlr
.vlr_proto
== 0)
2348 vlr
.vlr_proto
= ETHERTYPE_VLAN
;
2349 oldmtu
= ifp
->if_mtu
;
2350 error
= vlan_config(ifv
, p
, vlr
.vlr_tag
, vlr
.vlr_proto
);
2354 * VLAN MTU may change during addition of the vlandev.
2355 * If it did, do network layer specific procedure.
2357 if (ifp
->if_mtu
!= oldmtu
)
2363 if (ifp
->if_vnet
!= ifp
->if_home_vnet
) {
2368 bzero(&vlr
, sizeof(vlr
));
2370 if (TRUNK(ifv
) != NULL
) {
2371 strlcpy(vlr
.vlr_parent
, PARENT(ifv
)->if_xname
,
2372 sizeof(vlr
.vlr_parent
));
2373 vlr
.vlr_tag
= ifv
->ifv_vid
;
2374 vlr
.vlr_proto
= ifv
->ifv_proto
;
2377 error
= copyout(&vlr
, ifr_data_get_ptr(ifr
), sizeof(vlr
));
2382 * We should propagate selected flags to the parent,
2383 * e.g., promiscuous mode.
2386 if (TRUNK(ifv
) != NULL
)
2387 error
= vlan_setflags(ifp
, 1);
2394 * If we don't have a parent, just remember the membership for
2397 * XXX We need the rmlock here to avoid sleeping while
2398 * holding in6_multi_mtx.
2403 error
= vlan_setmulti(ifp
);
2409 if (ifp
->if_vnet
!= ifp
->if_home_vnet
) {
2414 ifr
->ifr_vlan_pcp
= ifv
->ifv_pcp
;
2419 if (ifp
->if_vnet
!= ifp
->if_home_vnet
) {
2424 error
= priv_check(curthread
, PRIV_NET_SETVLANPCP
);
2427 if (ifr
->ifr_vlan_pcp
> VLAN_PCP_MAX
) {
2431 ifv
->ifv_pcp
= ifr
->ifr_vlan_pcp
;
2432 ifp
->if_pcp
= ifv
->ifv_pcp
;
2433 /* broadcast event about PCP change */
2434 EVENTHANDLER_INVOKE(ifnet_event
, ifp
, IFNET_EVENT_PCP
);
2439 ifv
->ifv_capenable
= ifr
->ifr_reqcap
;
2441 if (trunk
!= NULL
) {
2442 struct epoch_tracker et
;
2444 NET_EPOCH_ENTER(et
);
2445 vlan_capabilities(ifv
);
2459 #if defined(KERN_TLS) || defined(RATELIMIT)
2461 vlan_snd_tag_alloc(struct ifnet
*ifp
,
2462 union if_snd_tag_alloc_params
*params
,
2463 struct m_snd_tag
**ppmt
)
2465 struct epoch_tracker et
;
2466 const struct if_snd_tag_sw
*sw
;
2467 struct vlan_snd_tag
*vst
;
2469 struct ifnet
*parent
;
2470 struct m_snd_tag
*mst
;
2473 NET_EPOCH_ENTER(et
);
2474 ifv
= ifp
->if_softc
;
2476 switch (params
->hdr
.type
) {
2478 case IF_SND_TAG_TYPE_UNLIMITED
:
2479 sw
= &vlan_snd_tag_ul_sw
;
2481 case IF_SND_TAG_TYPE_RATE_LIMIT
:
2482 sw
= &vlan_snd_tag_rl_sw
;
2486 case IF_SND_TAG_TYPE_TLS
:
2487 sw
= &vlan_snd_tag_tls_sw
;
2489 case IF_SND_TAG_TYPE_TLS_RX
:
2491 if (params
->tls_rx
.vlan_id
!= 0)
2493 params
->tls_rx
.vlan_id
= ifv
->ifv_vid
;
2496 case IF_SND_TAG_TYPE_TLS_RATE_LIMIT
:
2497 sw
= &vlan_snd_tag_tls_rl_sw
;
2505 if (ifv
->ifv_trunk
!= NULL
)
2506 parent
= PARENT(ifv
);
2515 vst
= malloc(sizeof(*vst
), M_VLAN
, M_NOWAIT
);
2523 error
= m_snd_tag_alloc(parent
, params
, &mst
);
2531 m_snd_tag_init(&vst
->com
, ifp
, sw
);
2541 return (EOPNOTSUPP
);
2544 static struct m_snd_tag
*
2545 vlan_next_snd_tag(struct m_snd_tag
*mst
)
2547 struct vlan_snd_tag
*vst
;
2549 vst
= mst_to_vst(mst
);
2554 vlan_snd_tag_modify(struct m_snd_tag
*mst
,
2555 union if_snd_tag_modify_params
*params
)
2557 struct vlan_snd_tag
*vst
;
2559 vst
= mst_to_vst(mst
);
2560 return (vst
->tag
->sw
->snd_tag_modify(vst
->tag
, params
));
2564 vlan_snd_tag_query(struct m_snd_tag
*mst
,
2565 union if_snd_tag_query_params
*params
)
2567 struct vlan_snd_tag
*vst
;
2569 vst
= mst_to_vst(mst
);
2570 return (vst
->tag
->sw
->snd_tag_query(vst
->tag
, params
));
2574 vlan_snd_tag_free(struct m_snd_tag
*mst
)
2576 struct vlan_snd_tag
*vst
;
2578 vst
= mst_to_vst(mst
);
2579 m_snd_tag_rele(vst
->tag
);
2584 vlan_ratelimit_query(struct ifnet
*ifp __unused
, struct if_ratelimit_query_results
*q
)
2587 * For vlan, we have an indirect
2588 * interface. The caller needs to
2589 * get a ratelimit tag on the actual
2590 * interface the flow will go on.
2592 q
->rate_table
= NULL
;
2593 q
->flags
= RT_IS_INDIRECT
;
2595 q
->number_of_rates
= 0;