xref: /freebsd-14-stable/sys/net/if_vlan.c (revision ff5a685270a3946bf7941e82bb77528670b3aba3)
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  * Copyright 2012 ADARA Networks, Inc.
4  * Copyright 2017 Dell EMC Isilon
5  *
6  * Portions of this software were developed by Robert N. M. Watson under
7  * contract to ADARA Networks, Inc.
8  *
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
19  * warranty.
20  *
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
32  * SUCH DAMAGE.
33  */
34 
35 /*
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.
43  */
44 
45 #include <sys/cdefs.h>
46 #include "opt_inet.h"
47 #include "opt_inet6.h"
48 #include "opt_kern_tls.h"
49 #include "opt_vlan.h"
50 #include "opt_ratelimit.h"
51 
52 #include <sys/param.h>
53 #include <sys/eventhandler.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/mbuf.h>
58 #include <sys/module.h>
59 #include <sys/rmlock.h>
60 #include <sys/priv.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>
66 #include <sys/sx.h>
67 #include <sys/taskqueue.h>
68 
69 #include <net/bpf.h>
70 #include <net/ethernet.h>
71 #include <net/if.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>
79 #include <net/vnet.h>
80 
81 #ifdef INET
82 #include <netinet/in.h>
83 #include <netinet/if_ether.h>
84 #endif
85 
86 #include <netlink/netlink.h>
87 #include <netlink/netlink_ctl.h>
88 #include <netlink/netlink_route.h>
89 #include <netlink/route/route_var.h>
90 
91 #define	VLAN_DEF_HWIDTH	4
92 #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
93 
94 #define	UP_AND_RUNNING(ifp) \
95     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
96 
97 CK_SLIST_HEAD(ifvlanhead, ifvlan);
98 
99 struct ifvlantrunk {
100 	struct	ifnet   *parent;	/* parent interface of this trunk */
101 	struct	mtx	lock;
102 #ifdef VLAN_ARRAY
103 #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
104 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
105 #else
106 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
107 	uint16_t	hmask;
108 	uint16_t	hwidth;
109 #endif
110 	int		refcnt;
111 };
112 
113 #if defined(KERN_TLS) || defined(RATELIMIT)
114 struct vlan_snd_tag {
115 	struct m_snd_tag com;
116 	struct m_snd_tag *tag;
117 };
118 
119 static inline struct vlan_snd_tag *
mst_to_vst(struct m_snd_tag * mst)120 mst_to_vst(struct m_snd_tag *mst)
121 {
122 
123 	return (__containerof(mst, struct vlan_snd_tag, com));
124 }
125 #endif
126 
127 /*
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.
130  */
131 #ifdef VLAN_ARRAY
132 #define VLAN_FOREACH(_ifv, _trunk) \
133 	size_t _i; \
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; \
139 	size_t _i; \
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 */
143 
144 /*
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.
148  */
149 #ifdef VLAN_ARRAY
150 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
151 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
152 	size_t _i; \
153 	for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
154 		if (((_ifv) = (_trunk)->vlans[_i]))
155 #else /* VLAN_ARRAY */
156 /*
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.
163  */
164 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
165 	size_t _i; \
166 	bool _touch = false; \
167 	for (_i = 0; \
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 && \
171 		    (_touch = true))
172 #endif /* VLAN_ARRAY */
173 
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;
178 };
179 
180 struct ifvlan {
181 	struct	ifvlantrunk *ifv_trunk;
182 	struct	ifnet *ifv_ifp;
183 #define	TRUNK(ifv)	((ifv)->ifv_trunk)
184 #define	PARENT(ifv)	(TRUNK(ifv)->parent)
185 	void	*ifv_cookie;
186 	int	ifv_pflags;	/* special flags we have set on parent */
187 	int	ifv_capenable;
188 	int	ifv_encaplen;	/* encapsulation length */
189 	int	ifv_mtufudge;	/* MTU fudged by this much */
190 	int	ifv_mintu;	/* min transmission unit */
191 	struct  ether_8021q_tag ifv_qtag;
192 #define ifv_proto	ifv_qtag.proto
193 #define ifv_vid		ifv_qtag.vid
194 #define ifv_pcp		ifv_qtag.pcp
195 	struct task lladdr_task;
196 	CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
197 #ifndef VLAN_ARRAY
198 	CK_SLIST_ENTRY(ifvlan) ifv_list;
199 #endif
200 };
201 
202 /* Special flags we should propagate to parent. */
203 static struct {
204 	int flag;
205 	int (*func)(struct ifnet *, int);
206 } vlan_pflags[] = {
207 	{IFF_PROMISC, ifpromisc},
208 	{IFF_ALLMULTI, if_allmulti},
209 	{0, NULL}
210 };
211 
212 VNET_DECLARE(int, vlan_mtag_pcp);
213 #define	V_vlan_mtag_pcp	VNET(vlan_mtag_pcp)
214 
215 static const char vlanname[] = "vlan";
216 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
217 
218 static eventhandler_tag ifdetach_tag;
219 static eventhandler_tag iflladdr_tag;
220 static eventhandler_tag ifevent_tag;
221 
222 /*
223  * if_vlan uses two module-level synchronizations primitives to allow concurrent
224  * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
225  * while they are being used for tx/rx. To accomplish this in a way that has
226  * acceptable performance and cooperation with other parts of the network stack
227  * there is a non-sleepable epoch(9) and an sx(9).
228  *
229  * The performance-sensitive paths that warrant using the epoch(9) are
230  * vlan_transmit and vlan_input. Both have to check for the vlan interface's
231  * existence using if_vlantrunk, and being in the network tx/rx paths the use
232  * of an epoch(9) gives a measureable improvement in performance.
233  *
234  * The reason for having an sx(9) is mostly because there are still areas that
235  * must be sleepable and also have safe concurrent access to a vlan interface.
236  * Since the sx(9) exists, it is used by default in most paths unless sleeping
237  * is not permitted, or if it is not clear whether sleeping is permitted.
238  *
239  */
240 #define _VLAN_SX_ID ifv_sx
241 
242 static struct sx _VLAN_SX_ID;
243 
244 #define VLAN_LOCKING_INIT() \
245 	sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE)
246 
247 #define VLAN_LOCKING_DESTROY() \
248 	sx_destroy(&_VLAN_SX_ID)
249 
250 #define	VLAN_SLOCK()			sx_slock(&_VLAN_SX_ID)
251 #define	VLAN_SUNLOCK()			sx_sunlock(&_VLAN_SX_ID)
252 #define	VLAN_XLOCK()			sx_xlock(&_VLAN_SX_ID)
253 #define	VLAN_XUNLOCK()			sx_xunlock(&_VLAN_SX_ID)
254 #define	VLAN_SLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
255 #define	VLAN_XLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
256 #define	VLAN_SXLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_LOCKED)
257 
258 /*
259  * We also have a per-trunk mutex that should be acquired when changing
260  * its state.
261  */
262 #define	TRUNK_LOCK_INIT(trunk)		mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
263 #define	TRUNK_LOCK_DESTROY(trunk)	mtx_destroy(&(trunk)->lock)
264 #define	TRUNK_WLOCK(trunk)		mtx_lock(&(trunk)->lock)
265 #define	TRUNK_WUNLOCK(trunk)		mtx_unlock(&(trunk)->lock)
266 #define	TRUNK_WLOCK_ASSERT(trunk)	mtx_assert(&(trunk)->lock, MA_OWNED);
267 
268 /*
269  * The VLAN_ARRAY substitutes the dynamic hash with a static array
270  * with 4096 entries. In theory this can give a boost in processing,
271  * however in practice it does not. Probably this is because the array
272  * is too big to fit into CPU cache.
273  */
274 #ifndef VLAN_ARRAY
275 static	void vlan_inithash(struct ifvlantrunk *trunk);
276 static	void vlan_freehash(struct ifvlantrunk *trunk);
277 static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
278 static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
279 static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
280 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
281 	uint16_t vid);
282 #endif
283 static	void trunk_destroy(struct ifvlantrunk *trunk);
284 
285 static	void vlan_init(void *foo);
286 static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
287 static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
288 #if defined(KERN_TLS) || defined(RATELIMIT)
289 static	int vlan_snd_tag_alloc(struct ifnet *,
290     union if_snd_tag_alloc_params *, struct m_snd_tag **);
291 static	int vlan_snd_tag_modify(struct m_snd_tag *,
292     union if_snd_tag_modify_params *);
293 static	int vlan_snd_tag_query(struct m_snd_tag *,
294     union if_snd_tag_query_params *);
295 static	void vlan_snd_tag_free(struct m_snd_tag *);
296 static struct m_snd_tag *vlan_next_snd_tag(struct m_snd_tag *);
297 static void vlan_ratelimit_query(struct ifnet *,
298     struct if_ratelimit_query_results *);
299 #endif
300 static	void vlan_qflush(struct ifnet *ifp);
301 static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
302     int (*func)(struct ifnet *, int));
303 static	int vlan_setflags(struct ifnet *ifp, int status);
304 static	int vlan_setmulti(struct ifnet *ifp);
305 static	int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
306 #ifdef ALTQ
307 static void vlan_altq_start(struct ifnet *ifp);
308 static	int vlan_altq_transmit(struct ifnet *ifp, struct mbuf *m);
309 #endif
310 static	int vlan_output(struct ifnet *ifp, struct mbuf *m,
311     const struct sockaddr *dst, struct route *ro);
312 static	void vlan_unconfig(struct ifnet *ifp);
313 static	void vlan_unconfig_locked(struct ifnet *ifp, int departing);
314 static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag,
315 	uint16_t proto);
316 static	void vlan_link_state(struct ifnet *ifp);
317 static	void vlan_capabilities(struct ifvlan *ifv);
318 static	void vlan_trunk_capabilities(struct ifnet *ifp);
319 
320 static	struct ifnet *vlan_clone_match_ethervid(const char *, int *);
321 static	int vlan_clone_match(struct if_clone *, const char *);
322 static	int vlan_clone_create(struct if_clone *, char *, size_t,
323     struct ifc_data *, struct ifnet **);
324 static	int vlan_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
325 
326 static int vlan_clone_create_nl(struct if_clone *ifc, char *name, size_t len,
327     struct ifc_data_nl *ifd);
328 static int vlan_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd);
329 static void vlan_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw);
330 
331 static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
332 static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
333 static  void vlan_ifevent(void *arg, struct ifnet *ifp, int event);
334 
335 static  void vlan_lladdr_fn(void *arg, int pending);
336 
337 static struct if_clone *vlan_cloner;
338 
339 #ifdef VIMAGE
340 VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
341 #define	V_vlan_cloner	VNET(vlan_cloner)
342 #endif
343 
344 #ifdef RATELIMIT
345 static const struct if_snd_tag_sw vlan_snd_tag_ul_sw = {
346 	.snd_tag_modify = vlan_snd_tag_modify,
347 	.snd_tag_query = vlan_snd_tag_query,
348 	.snd_tag_free = vlan_snd_tag_free,
349 	.next_snd_tag = vlan_next_snd_tag,
350 	.type = IF_SND_TAG_TYPE_UNLIMITED
351 };
352 
353 static const struct if_snd_tag_sw vlan_snd_tag_rl_sw = {
354 	.snd_tag_modify = vlan_snd_tag_modify,
355 	.snd_tag_query = vlan_snd_tag_query,
356 	.snd_tag_free = vlan_snd_tag_free,
357 	.next_snd_tag = vlan_next_snd_tag,
358 	.type = IF_SND_TAG_TYPE_RATE_LIMIT
359 };
360 #endif
361 
362 #ifdef KERN_TLS
363 static const struct if_snd_tag_sw vlan_snd_tag_tls_sw = {
364 	.snd_tag_modify = vlan_snd_tag_modify,
365 	.snd_tag_query = vlan_snd_tag_query,
366 	.snd_tag_free = vlan_snd_tag_free,
367 	.next_snd_tag = vlan_next_snd_tag,
368 	.type = IF_SND_TAG_TYPE_TLS
369 };
370 
371 #ifdef RATELIMIT
372 static const struct if_snd_tag_sw vlan_snd_tag_tls_rl_sw = {
373 	.snd_tag_modify = vlan_snd_tag_modify,
374 	.snd_tag_query = vlan_snd_tag_query,
375 	.snd_tag_free = vlan_snd_tag_free,
376 	.next_snd_tag = vlan_next_snd_tag,
377 	.type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT
378 };
379 #endif
380 #endif
381 
382 static void
vlan_mc_free(struct epoch_context * ctx)383 vlan_mc_free(struct epoch_context *ctx)
384 {
385 	struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
386 	free(mc, M_VLAN);
387 }
388 
389 #ifndef VLAN_ARRAY
390 #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
391 
392 static void
vlan_inithash(struct ifvlantrunk * trunk)393 vlan_inithash(struct ifvlantrunk *trunk)
394 {
395 	int i, n;
396 
397 	/*
398 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
399 	 * It is OK in case this function is called before the trunk struct
400 	 * gets hooked up and becomes visible from other threads.
401 	 */
402 
403 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
404 	    ("%s: hash already initialized", __func__));
405 
406 	trunk->hwidth = VLAN_DEF_HWIDTH;
407 	n = 1 << trunk->hwidth;
408 	trunk->hmask = n - 1;
409 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
410 	for (i = 0; i < n; i++)
411 		CK_SLIST_INIT(&trunk->hash[i]);
412 }
413 
414 static void
vlan_freehash(struct ifvlantrunk * trunk)415 vlan_freehash(struct ifvlantrunk *trunk)
416 {
417 #ifdef INVARIANTS
418 	int i;
419 
420 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
421 	for (i = 0; i < (1 << trunk->hwidth); i++)
422 		KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
423 		    ("%s: hash table not empty", __func__));
424 #endif
425 	free(trunk->hash, M_VLAN);
426 	trunk->hash = NULL;
427 	trunk->hwidth = trunk->hmask = 0;
428 }
429 
430 static int
vlan_inshash(struct ifvlantrunk * trunk,struct ifvlan * ifv)431 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
432 {
433 	int i, b;
434 	struct ifvlan *ifv2;
435 
436 	VLAN_XLOCK_ASSERT();
437 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
438 
439 	b = 1 << trunk->hwidth;
440 	i = HASH(ifv->ifv_vid, trunk->hmask);
441 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
442 		if (ifv->ifv_vid == ifv2->ifv_vid)
443 			return (EEXIST);
444 
445 	/*
446 	 * Grow the hash when the number of vlans exceeds half of the number of
447 	 * hash buckets squared. This will make the average linked-list length
448 	 * buckets/2.
449 	 */
450 	if (trunk->refcnt > (b * b) / 2) {
451 		vlan_growhash(trunk, 1);
452 		i = HASH(ifv->ifv_vid, trunk->hmask);
453 	}
454 	CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
455 	trunk->refcnt++;
456 
457 	return (0);
458 }
459 
460 static int
vlan_remhash(struct ifvlantrunk * trunk,struct ifvlan * ifv)461 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
462 {
463 	int i, b;
464 	struct ifvlan *ifv2;
465 
466 	VLAN_XLOCK_ASSERT();
467 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
468 
469 	b = 1 << (trunk->hwidth - 1);
470 	i = HASH(ifv->ifv_vid, trunk->hmask);
471 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
472 		if (ifv2 == ifv) {
473 			trunk->refcnt--;
474 			CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
475 			if (trunk->refcnt < (b * b) / 2)
476 				vlan_growhash(trunk, -1);
477 			return (0);
478 		}
479 
480 	panic("%s: vlan not found\n", __func__);
481 	return (ENOENT); /*NOTREACHED*/
482 }
483 
484 /*
485  * Grow the hash larger or smaller if memory permits.
486  */
487 static void
vlan_growhash(struct ifvlantrunk * trunk,int howmuch)488 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
489 {
490 	struct ifvlan *ifv;
491 	struct ifvlanhead *hash2;
492 	int hwidth2, i, j, n, n2;
493 
494 	VLAN_XLOCK_ASSERT();
495 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
496 
497 	if (howmuch == 0) {
498 		/* Harmless yet obvious coding error */
499 		printf("%s: howmuch is 0\n", __func__);
500 		return;
501 	}
502 
503 	hwidth2 = trunk->hwidth + howmuch;
504 	n = 1 << trunk->hwidth;
505 	n2 = 1 << hwidth2;
506 	/* Do not shrink the table below the default */
507 	if (hwidth2 < VLAN_DEF_HWIDTH)
508 		return;
509 
510 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
511 	for (j = 0; j < n2; j++)
512 		CK_SLIST_INIT(&hash2[j]);
513 	for (i = 0; i < n; i++)
514 		while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
515 			CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
516 			j = HASH(ifv->ifv_vid, n2 - 1);
517 			CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
518 		}
519 	NET_EPOCH_WAIT();
520 	free(trunk->hash, M_VLAN);
521 	trunk->hash = hash2;
522 	trunk->hwidth = hwidth2;
523 	trunk->hmask = n2 - 1;
524 
525 	if (bootverbose)
526 		if_printf(trunk->parent,
527 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
528 }
529 
530 static __inline struct ifvlan *
vlan_gethash(struct ifvlantrunk * trunk,uint16_t vid)531 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
532 {
533 	struct ifvlan *ifv;
534 
535 	NET_EPOCH_ASSERT();
536 
537 	CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
538 		if (ifv->ifv_vid == vid)
539 			return (ifv);
540 	return (NULL);
541 }
542 
543 #if 0
544 /* Debugging code to view the hashtables. */
545 static void
546 vlan_dumphash(struct ifvlantrunk *trunk)
547 {
548 	int i;
549 	struct ifvlan *ifv;
550 
551 	for (i = 0; i < (1 << trunk->hwidth); i++) {
552 		printf("%d: ", i);
553 		CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
554 			printf("%s ", ifv->ifv_ifp->if_xname);
555 		printf("\n");
556 	}
557 }
558 #endif /* 0 */
559 #else
560 
561 static __inline struct ifvlan *
vlan_gethash(struct ifvlantrunk * trunk,uint16_t vid)562 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
563 {
564 
565 	return trunk->vlans[vid];
566 }
567 
568 static __inline int
vlan_inshash(struct ifvlantrunk * trunk,struct ifvlan * ifv)569 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
570 {
571 
572 	if (trunk->vlans[ifv->ifv_vid] != NULL)
573 		return EEXIST;
574 	trunk->vlans[ifv->ifv_vid] = ifv;
575 	trunk->refcnt++;
576 
577 	return (0);
578 }
579 
580 static __inline int
vlan_remhash(struct ifvlantrunk * trunk,struct ifvlan * ifv)581 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
582 {
583 
584 	trunk->vlans[ifv->ifv_vid] = NULL;
585 	trunk->refcnt--;
586 
587 	return (0);
588 }
589 
590 static __inline void
vlan_freehash(struct ifvlantrunk * trunk)591 vlan_freehash(struct ifvlantrunk *trunk)
592 {
593 }
594 
595 static __inline void
vlan_inithash(struct ifvlantrunk * trunk)596 vlan_inithash(struct ifvlantrunk *trunk)
597 {
598 }
599 
600 #endif /* !VLAN_ARRAY */
601 
602 static void
trunk_destroy(struct ifvlantrunk * trunk)603 trunk_destroy(struct ifvlantrunk *trunk)
604 {
605 	VLAN_XLOCK_ASSERT();
606 
607 	vlan_freehash(trunk);
608 	trunk->parent->if_vlantrunk = NULL;
609 	TRUNK_LOCK_DESTROY(trunk);
610 	if_rele(trunk->parent);
611 	free(trunk, M_VLAN);
612 }
613 
614 /*
615  * Program our multicast filter. What we're actually doing is
616  * programming the multicast filter of the parent. This has the
617  * side effect of causing the parent interface to receive multicast
618  * traffic that it doesn't really want, which ends up being discarded
619  * later by the upper protocol layers. Unfortunately, there's no way
620  * to avoid this: there really is only one physical interface.
621  */
622 static int
vlan_setmulti(struct ifnet * ifp)623 vlan_setmulti(struct ifnet *ifp)
624 {
625 	struct ifnet		*ifp_p;
626 	struct ifmultiaddr	*ifma;
627 	struct ifvlan		*sc;
628 	struct vlan_mc_entry	*mc;
629 	int			error;
630 
631 	VLAN_XLOCK_ASSERT();
632 
633 	/* Find the parent. */
634 	sc = ifp->if_softc;
635 	ifp_p = PARENT(sc);
636 
637 	CURVNET_SET_QUIET(ifp_p->if_vnet);
638 
639 	/* First, remove any existing filter entries. */
640 	while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
641 		CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
642 		(void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
643 		NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
644 	}
645 
646 	/* Now program new ones. */
647 	IF_ADDR_WLOCK(ifp);
648 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
649 		if (ifma->ifma_addr->sa_family != AF_LINK)
650 			continue;
651 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
652 		if (mc == NULL) {
653 			IF_ADDR_WUNLOCK(ifp);
654 			CURVNET_RESTORE();
655 			return (ENOMEM);
656 		}
657 		bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
658 		mc->mc_addr.sdl_index = ifp_p->if_index;
659 		CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
660 	}
661 	IF_ADDR_WUNLOCK(ifp);
662 	CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
663 		error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
664 		    NULL);
665 		if (error) {
666 			CURVNET_RESTORE();
667 			return (error);
668 		}
669 	}
670 
671 	CURVNET_RESTORE();
672 	return (0);
673 }
674 
675 /*
676  * A handler for interface ifnet events.
677  */
678 static void
vlan_ifevent(void * arg __unused,struct ifnet * ifp,int event)679 vlan_ifevent(void *arg __unused, struct ifnet *ifp, int event)
680 {
681 	struct epoch_tracker et;
682 	struct ifvlan *ifv;
683 	struct ifvlantrunk *trunk;
684 
685 	if (event != IFNET_EVENT_UPDATE_BAUDRATE)
686 		return;
687 
688 	NET_EPOCH_ENTER(et);
689 	trunk = ifp->if_vlantrunk;
690 	if (trunk == NULL) {
691 		NET_EPOCH_EXIT(et);
692 		return;
693 	}
694 
695 	TRUNK_WLOCK(trunk);
696 	VLAN_FOREACH(ifv, trunk) {
697 		ifv->ifv_ifp->if_baudrate = ifp->if_baudrate;
698 	}
699 	TRUNK_WUNLOCK(trunk);
700 	NET_EPOCH_EXIT(et);
701 }
702 
703 /*
704  * A handler for parent interface link layer address changes.
705  * If the parent interface link layer address is changed we
706  * should also change it on all children vlans.
707  */
708 static void
vlan_iflladdr(void * arg __unused,struct ifnet * ifp)709 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
710 {
711 	struct epoch_tracker et;
712 	struct ifvlan *ifv;
713 	struct ifnet *ifv_ifp;
714 	struct ifvlantrunk *trunk;
715 	struct sockaddr_dl *sdl;
716 
717 	/* Need the epoch since this is run on taskqueue_swi. */
718 	NET_EPOCH_ENTER(et);
719 	trunk = ifp->if_vlantrunk;
720 	if (trunk == NULL) {
721 		NET_EPOCH_EXIT(et);
722 		return;
723 	}
724 
725 	/*
726 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
727 	 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
728 	 * ioctl calls on the parent garbling the lladdr of the child vlan.
729 	 */
730 	TRUNK_WLOCK(trunk);
731 	VLAN_FOREACH(ifv, trunk) {
732 		/*
733 		 * Copy new new lladdr into the ifv_ifp, enqueue a task
734 		 * to actually call if_setlladdr. if_setlladdr needs to
735 		 * be deferred to a taskqueue because it will call into
736 		 * the if_vlan ioctl path and try to acquire the global
737 		 * lock.
738 		 */
739 		ifv_ifp = ifv->ifv_ifp;
740 		bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
741 		    ifp->if_addrlen);
742 		sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
743 		sdl->sdl_alen = ifp->if_addrlen;
744 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
745 	}
746 	TRUNK_WUNLOCK(trunk);
747 	NET_EPOCH_EXIT(et);
748 }
749 
750 /*
751  * A handler for network interface departure events.
752  * Track departure of trunks here so that we don't access invalid
753  * pointers or whatever if a trunk is ripped from under us, e.g.,
754  * by ejecting its hot-plug card.  However, if an ifnet is simply
755  * being renamed, then there's no need to tear down the state.
756  */
757 static void
vlan_ifdetach(void * arg __unused,struct ifnet * ifp)758 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
759 {
760 	struct ifvlan *ifv;
761 	struct ifvlantrunk *trunk;
762 
763 	/* If the ifnet is just being renamed, don't do anything. */
764 	if (ifp->if_flags & IFF_RENAMING)
765 		return;
766 	VLAN_XLOCK();
767 	trunk = ifp->if_vlantrunk;
768 	if (trunk == NULL) {
769 		VLAN_XUNLOCK();
770 		return;
771 	}
772 
773 	/*
774 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
775 	 * Check trunk pointer after each vlan_unconfig() as it will
776 	 * free it and set to NULL after the last vlan was detached.
777 	 */
778 	VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
779 	    ifp->if_vlantrunk == NULL)
780 		vlan_unconfig_locked(ifv->ifv_ifp, 1);
781 
782 	/* Trunk should have been destroyed in vlan_unconfig(). */
783 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
784 	VLAN_XUNLOCK();
785 }
786 
787 /*
788  * Return the trunk device for a virtual interface.
789  */
790 static struct ifnet  *
vlan_trunkdev(struct ifnet * ifp)791 vlan_trunkdev(struct ifnet *ifp)
792 {
793 	struct ifvlan *ifv;
794 
795 	NET_EPOCH_ASSERT();
796 
797 	if (ifp->if_type != IFT_L2VLAN)
798 		return (NULL);
799 
800 	ifv = ifp->if_softc;
801 	ifp = NULL;
802 	if (ifv->ifv_trunk)
803 		ifp = PARENT(ifv);
804 	return (ifp);
805 }
806 
807 /*
808  * Return the 12-bit VLAN VID for this interface, for use by external
809  * components such as Infiniband.
810  *
811  * XXXRW: Note that the function name here is historical; it should be named
812  * vlan_vid().
813  */
814 static int
vlan_tag(struct ifnet * ifp,uint16_t * vidp)815 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
816 {
817 	struct ifvlan *ifv;
818 
819 	if (ifp->if_type != IFT_L2VLAN)
820 		return (EINVAL);
821 	ifv = ifp->if_softc;
822 	*vidp = ifv->ifv_vid;
823 	return (0);
824 }
825 
826 static int
vlan_pcp(struct ifnet * ifp,uint16_t * pcpp)827 vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
828 {
829 	struct ifvlan *ifv;
830 
831 	if (ifp->if_type != IFT_L2VLAN)
832 		return (EINVAL);
833 	ifv = ifp->if_softc;
834 	*pcpp = ifv->ifv_pcp;
835 	return (0);
836 }
837 
838 /*
839  * Return a driver specific cookie for this interface.  Synchronization
840  * with setcookie must be provided by the driver.
841  */
842 static void *
vlan_cookie(struct ifnet * ifp)843 vlan_cookie(struct ifnet *ifp)
844 {
845 	struct ifvlan *ifv;
846 
847 	if (ifp->if_type != IFT_L2VLAN)
848 		return (NULL);
849 	ifv = ifp->if_softc;
850 	return (ifv->ifv_cookie);
851 }
852 
853 /*
854  * Store a cookie in our softc that drivers can use to store driver
855  * private per-instance data in.
856  */
857 static int
vlan_setcookie(struct ifnet * ifp,void * cookie)858 vlan_setcookie(struct ifnet *ifp, void *cookie)
859 {
860 	struct ifvlan *ifv;
861 
862 	if (ifp->if_type != IFT_L2VLAN)
863 		return (EINVAL);
864 	ifv = ifp->if_softc;
865 	ifv->ifv_cookie = cookie;
866 	return (0);
867 }
868 
869 /*
870  * Return the vlan device present at the specific VID.
871  */
872 static struct ifnet *
vlan_devat(struct ifnet * ifp,uint16_t vid)873 vlan_devat(struct ifnet *ifp, uint16_t vid)
874 {
875 	struct ifvlantrunk *trunk;
876 	struct ifvlan *ifv;
877 
878 	NET_EPOCH_ASSERT();
879 
880 	trunk = ifp->if_vlantrunk;
881 	if (trunk == NULL)
882 		return (NULL);
883 	ifp = NULL;
884 	ifv = vlan_gethash(trunk, vid);
885 	if (ifv)
886 		ifp = ifv->ifv_ifp;
887 	return (ifp);
888 }
889 
890 /*
891  * VLAN support can be loaded as a module.  The only place in the
892  * system that's intimately aware of this is ether_input.  We hook
893  * into this code through vlan_input_p which is defined there and
894  * set here.  No one else in the system should be aware of this so
895  * we use an explicit reference here.
896  */
897 extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
898 
899 /* For if_link_state_change() eyes only... */
900 extern	void (*vlan_link_state_p)(struct ifnet *);
901 
902 static struct if_clone_addreq_v2 vlan_addreq = {
903 	.version = 2,
904 	.match_f = vlan_clone_match,
905 	.create_f = vlan_clone_create,
906 	.destroy_f = vlan_clone_destroy,
907 	.create_nl_f = vlan_clone_create_nl,
908 	.modify_nl_f = vlan_clone_modify_nl,
909 	.dump_nl_f = vlan_clone_dump_nl,
910 };
911 
912 static int
vlan_modevent(module_t mod,int type,void * data)913 vlan_modevent(module_t mod, int type, void *data)
914 {
915 
916 	switch (type) {
917 	case MOD_LOAD:
918 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
919 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
920 		if (ifdetach_tag == NULL)
921 			return (ENOMEM);
922 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
923 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
924 		if (iflladdr_tag == NULL)
925 			return (ENOMEM);
926 		ifevent_tag = EVENTHANDLER_REGISTER(ifnet_event,
927 		    vlan_ifevent, NULL, EVENTHANDLER_PRI_ANY);
928 		if (ifevent_tag == NULL)
929 			return (ENOMEM);
930 		VLAN_LOCKING_INIT();
931 		vlan_input_p = vlan_input;
932 		vlan_link_state_p = vlan_link_state;
933 		vlan_trunk_cap_p = vlan_trunk_capabilities;
934 		vlan_trunkdev_p = vlan_trunkdev;
935 		vlan_cookie_p = vlan_cookie;
936 		vlan_setcookie_p = vlan_setcookie;
937 		vlan_tag_p = vlan_tag;
938 		vlan_pcp_p = vlan_pcp;
939 		vlan_devat_p = vlan_devat;
940 #ifndef VIMAGE
941 		vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq);
942 #endif
943 		if (bootverbose)
944 			printf("vlan: initialized, using "
945 #ifdef VLAN_ARRAY
946 			       "full-size arrays"
947 #else
948 			       "hash tables with chaining"
949 #endif
950 
951 			       "\n");
952 		break;
953 	case MOD_UNLOAD:
954 #ifndef VIMAGE
955 		ifc_detach_cloner(vlan_cloner);
956 #endif
957 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
958 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
959 		EVENTHANDLER_DEREGISTER(ifnet_event, ifevent_tag);
960 		vlan_input_p = NULL;
961 		vlan_link_state_p = NULL;
962 		vlan_trunk_cap_p = NULL;
963 		vlan_trunkdev_p = NULL;
964 		vlan_tag_p = NULL;
965 		vlan_cookie_p = NULL;
966 		vlan_setcookie_p = NULL;
967 		vlan_devat_p = NULL;
968 		VLAN_LOCKING_DESTROY();
969 		if (bootverbose)
970 			printf("vlan: unloaded\n");
971 		break;
972 	default:
973 		return (EOPNOTSUPP);
974 	}
975 	return (0);
976 }
977 
978 static moduledata_t vlan_mod = {
979 	"if_vlan",
980 	vlan_modevent,
981 	0
982 };
983 
984 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
985 MODULE_VERSION(if_vlan, 3);
986 
987 #ifdef VIMAGE
988 static void
vnet_vlan_init(const void * unused __unused)989 vnet_vlan_init(const void *unused __unused)
990 {
991 	vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq);
992 	V_vlan_cloner = vlan_cloner;
993 }
994 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
995     vnet_vlan_init, NULL);
996 
997 static void
vnet_vlan_uninit(const void * unused __unused)998 vnet_vlan_uninit(const void *unused __unused)
999 {
1000 
1001 	ifc_detach_cloner(V_vlan_cloner);
1002 }
1003 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
1004     vnet_vlan_uninit, NULL);
1005 #endif
1006 
1007 /*
1008  * Check for <etherif>.<vlan>[.<vlan> ...] style interface names.
1009  */
1010 static struct ifnet *
vlan_clone_match_ethervid(const char * name,int * vidp)1011 vlan_clone_match_ethervid(const char *name, int *vidp)
1012 {
1013 	char ifname[IFNAMSIZ];
1014 	char *cp;
1015 	struct ifnet *ifp;
1016 	int vid;
1017 
1018 	strlcpy(ifname, name, IFNAMSIZ);
1019 	if ((cp = strrchr(ifname, '.')) == NULL)
1020 		return (NULL);
1021 	*cp = '\0';
1022 	if ((ifp = ifunit_ref(ifname)) == NULL)
1023 		return (NULL);
1024 	/* Parse VID. */
1025 	if (*++cp == '\0') {
1026 		if_rele(ifp);
1027 		return (NULL);
1028 	}
1029 	vid = 0;
1030 	for(; *cp >= '0' && *cp <= '9'; cp++)
1031 		vid = (vid * 10) + (*cp - '0');
1032 	if (*cp != '\0') {
1033 		if_rele(ifp);
1034 		return (NULL);
1035 	}
1036 	if (vidp != NULL)
1037 		*vidp = vid;
1038 
1039 	return (ifp);
1040 }
1041 
1042 static int
vlan_clone_match(struct if_clone * ifc,const char * name)1043 vlan_clone_match(struct if_clone *ifc, const char *name)
1044 {
1045 	struct ifnet *ifp;
1046 	const char *cp;
1047 
1048 	ifp = vlan_clone_match_ethervid(name, NULL);
1049 	if (ifp != NULL) {
1050 		if_rele(ifp);
1051 		return (1);
1052 	}
1053 
1054 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
1055 		return (0);
1056 	for (cp = name + 4; *cp != '\0'; cp++) {
1057 		if (*cp < '0' || *cp > '9')
1058 			return (0);
1059 	}
1060 
1061 	return (1);
1062 }
1063 
1064 static int
vlan_clone_create(struct if_clone * ifc,char * name,size_t len,struct ifc_data * ifd,struct ifnet ** ifpp)1065 vlan_clone_create(struct if_clone *ifc, char *name, size_t len,
1066     struct ifc_data *ifd, struct ifnet **ifpp)
1067 {
1068 	char *dp;
1069 	bool wildcard = false;
1070 	bool subinterface = false;
1071 	int unit;
1072 	int error;
1073 	int vid = 0;
1074 	uint16_t proto = ETHERTYPE_VLAN;
1075 	struct ifvlan *ifv;
1076 	struct ifnet *ifp;
1077 	struct ifnet *p = NULL;
1078 	struct ifaddr *ifa;
1079 	struct sockaddr_dl *sdl;
1080 	struct vlanreq vlr;
1081 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
1082 
1083 
1084 	/*
1085 	 * There are three ways to specify the cloned device:
1086 	 * o pass a parameter block with the clone request.
1087 	 * o specify parameters in the text of the clone device name
1088 	 * o specify no parameters and get an unattached device that
1089 	 *   must be configured separately.
1090 	 * The first technique is preferred; the latter two are supported
1091 	 * for backwards compatibility.
1092 	 *
1093 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
1094 	 * called for.
1095 	 */
1096 
1097 	if (ifd->params != NULL) {
1098 		error = ifc_copyin(ifd, &vlr, sizeof(vlr));
1099 		if (error)
1100 			return error;
1101 		vid = vlr.vlr_tag;
1102 		proto = vlr.vlr_proto;
1103 		if (proto == 0)
1104 			proto = ETHERTYPE_VLAN;
1105 		p = ifunit_ref(vlr.vlr_parent);
1106 		if (p == NULL)
1107 			return (ENXIO);
1108 	}
1109 
1110 	if ((error = ifc_name2unit(name, &unit)) == 0) {
1111 
1112 		/*
1113 		 * vlanX interface. Set wildcard to true if the unit number
1114 		 * is not fixed (-1)
1115 		 */
1116 		wildcard = (unit < 0);
1117 	} else {
1118 		struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid);
1119 		if (p_tmp != NULL) {
1120 			error = 0;
1121 			subinterface = true;
1122 			unit = IF_DUNIT_NONE;
1123 			wildcard = false;
1124 			if (p != NULL) {
1125 				if_rele(p_tmp);
1126 				if (p != p_tmp)
1127 					error = EINVAL;
1128 			} else
1129 				p = p_tmp;
1130 		} else
1131 			error = ENXIO;
1132 	}
1133 
1134 	if (error != 0) {
1135 		if (p != NULL)
1136 			if_rele(p);
1137 		return (error);
1138 	}
1139 
1140 	if (!subinterface) {
1141 		/* vlanX interface, mark X as busy or allocate new unit # */
1142 		error = ifc_alloc_unit(ifc, &unit);
1143 		if (error != 0) {
1144 			if (p != NULL)
1145 				if_rele(p);
1146 			return (error);
1147 		}
1148 	}
1149 
1150 	/* In the wildcard case, we need to update the name. */
1151 	if (wildcard) {
1152 		for (dp = name; *dp != '\0'; dp++);
1153 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1154 		    len - (dp-name) - 1) {
1155 			panic("%s: interface name too long", __func__);
1156 		}
1157 	}
1158 
1159 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1160 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1161 	CK_SLIST_INIT(&ifv->vlan_mc_listhead);
1162 	ifp->if_softc = ifv;
1163 	/*
1164 	 * Set the name manually rather than using if_initname because
1165 	 * we don't conform to the default naming convention for interfaces.
1166 	 */
1167 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
1168 	ifp->if_dname = vlanname;
1169 	ifp->if_dunit = unit;
1170 
1171 	ifp->if_init = vlan_init;
1172 #ifdef ALTQ
1173 	ifp->if_start = vlan_altq_start;
1174 	ifp->if_transmit = vlan_altq_transmit;
1175 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1176 	ifp->if_snd.ifq_drv_maxlen = 0;
1177 	IFQ_SET_READY(&ifp->if_snd);
1178 #else
1179 	ifp->if_transmit = vlan_transmit;
1180 #endif
1181 	ifp->if_qflush = vlan_qflush;
1182 	ifp->if_ioctl = vlan_ioctl;
1183 #if defined(KERN_TLS) || defined(RATELIMIT)
1184 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1185 	ifp->if_ratelimit_query = vlan_ratelimit_query;
1186 #endif
1187 	ifp->if_flags = VLAN_IFFLAGS;
1188 	ether_ifattach(ifp, eaddr);
1189 	/* Now undo some of the damage... */
1190 	ifp->if_baudrate = 0;
1191 	ifp->if_type = IFT_L2VLAN;
1192 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1193 	ifa = ifp->if_addr;
1194 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1195 	sdl->sdl_type = IFT_L2VLAN;
1196 
1197 	if (p != NULL) {
1198 		error = vlan_config(ifv, p, vid, proto);
1199 		if_rele(p);
1200 		if (error != 0) {
1201 			/*
1202 			 * Since we've partially failed, we need to back
1203 			 * out all the way, otherwise userland could get
1204 			 * confused.  Thus, we destroy the interface.
1205 			 */
1206 			ether_ifdetach(ifp);
1207 			vlan_unconfig(ifp);
1208 			if_free(ifp);
1209 			if (!subinterface)
1210 				ifc_free_unit(ifc, unit);
1211 			free(ifv, M_VLAN);
1212 
1213 			return (error);
1214 		}
1215 	}
1216 	*ifpp = ifp;
1217 
1218 	return (0);
1219 }
1220 
1221 /*
1222  *
1223  * Parsers of IFLA_INFO_DATA inside IFLA_LINKINFO of RTM_NEWLINK
1224  *    {{nla_len=8, nla_type=IFLA_LINK}, 2},
1225  *    {{nla_len=12, nla_type=IFLA_IFNAME}, "xvlan22"},
1226  *    {{nla_len=24, nla_type=IFLA_LINKINFO},
1227  *     [
1228  *      {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
1229  *      {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]}
1230  */
1231 
1232 struct nl_parsed_vlan {
1233 	uint16_t vlan_id;
1234 	uint16_t vlan_proto;
1235 	struct ifla_vlan_flags vlan_flags;
1236 };
1237 
1238 #define	_OUT(_field)	offsetof(struct nl_parsed_vlan, _field)
1239 static const struct nlattr_parser nla_p_vlan[] = {
1240 	{ .type = IFLA_VLAN_ID, .off = _OUT(vlan_id), .cb = nlattr_get_uint16 },
1241 	{ .type = IFLA_VLAN_FLAGS, .off = _OUT(vlan_flags), .cb = nlattr_get_nla },
1242 	{ .type = IFLA_VLAN_PROTOCOL, .off = _OUT(vlan_proto), .cb = nlattr_get_uint16 },
1243 };
1244 #undef _OUT
1245 NL_DECLARE_ATTR_PARSER(vlan_parser, nla_p_vlan);
1246 
1247 static int
vlan_clone_create_nl(struct if_clone * ifc,char * name,size_t len,struct ifc_data_nl * ifd)1248 vlan_clone_create_nl(struct if_clone *ifc, char *name, size_t len,
1249     struct ifc_data_nl *ifd)
1250 {
1251 	struct epoch_tracker et;
1252         struct ifnet *ifp_parent;
1253 	struct nl_pstate *npt = ifd->npt;
1254 	struct nl_parsed_link *lattrs = ifd->lattrs;
1255 	int error;
1256 
1257 	/*
1258 	 * lattrs.ifla_ifname is the new interface name
1259 	 * lattrs.ifi_index contains parent interface index
1260 	 * lattrs.ifla_idata contains un-parsed vlan data
1261 	 */
1262 	struct nl_parsed_vlan attrs = {
1263 		.vlan_id = 0xFEFE,
1264 		.vlan_proto = ETHERTYPE_VLAN
1265 	};
1266 
1267 	if (lattrs->ifla_idata == NULL) {
1268 		nlmsg_report_err_msg(npt, "vlan id is required, guessing not supported");
1269 		return (ENOTSUP);
1270 	}
1271 
1272 	error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, npt, &attrs);
1273 	if (error != 0)
1274 		return (error);
1275 	if (attrs.vlan_id > 4095) {
1276 		nlmsg_report_err_msg(npt, "Invalid VID: %d", attrs.vlan_id);
1277 		return (EINVAL);
1278 	}
1279 	if (attrs.vlan_proto != ETHERTYPE_VLAN && attrs.vlan_proto != ETHERTYPE_QINQ) {
1280 		nlmsg_report_err_msg(npt, "Unsupported ethertype: 0x%04X", attrs.vlan_proto);
1281 		return (ENOTSUP);
1282 	}
1283 
1284 	struct vlanreq params = {
1285 		.vlr_tag = attrs.vlan_id,
1286 		.vlr_proto = attrs.vlan_proto,
1287 	};
1288 	struct ifc_data ifd_new = { .flags = IFC_F_SYSSPACE, .unit = ifd->unit, .params = &params };
1289 
1290 	NET_EPOCH_ENTER(et);
1291 	ifp_parent = ifnet_byindex(lattrs->ifi_index);
1292 	if (ifp_parent != NULL)
1293 		strlcpy(params.vlr_parent, if_name(ifp_parent), sizeof(params.vlr_parent));
1294 	NET_EPOCH_EXIT(et);
1295 
1296 	if (ifp_parent == NULL) {
1297 		nlmsg_report_err_msg(npt, "unable to find parent interface %u", lattrs->ifi_index);
1298 		return (ENOENT);
1299 	}
1300 
1301 	error = vlan_clone_create(ifc, name, len, &ifd_new, &ifd->ifp);
1302 
1303 	return (error);
1304 }
1305 
1306 static int
vlan_clone_modify_nl(struct ifnet * ifp,struct ifc_data_nl * ifd)1307 vlan_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd)
1308 {
1309 	struct nl_parsed_link *lattrs = ifd->lattrs;
1310 
1311 	if ((lattrs->ifla_idata != NULL) && ((ifd->flags & IFC_F_CREATE) == 0)) {
1312 		struct epoch_tracker et;
1313 		struct nl_parsed_vlan attrs = {
1314 			.vlan_proto = ETHERTYPE_VLAN,
1315 		};
1316 		int error;
1317 
1318 		error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, ifd->npt, &attrs);
1319 		if (error != 0)
1320 			return (error);
1321 
1322 		NET_EPOCH_ENTER(et);
1323 		struct ifnet *ifp_parent = ifnet_byindex_ref(lattrs->ifla_link);
1324 		NET_EPOCH_EXIT(et);
1325 
1326 		if (ifp_parent == NULL) {
1327 			nlmsg_report_err_msg(ifd->npt, "unable to find parent interface %u",
1328 			    lattrs->ifla_link);
1329 			return (ENOENT);
1330 		}
1331 
1332 		struct ifvlan *ifv = ifp->if_softc;
1333 		error = vlan_config(ifv, ifp_parent, attrs.vlan_id, attrs.vlan_proto);
1334 
1335 		if_rele(ifp_parent);
1336 		if (error != 0)
1337 			return (error);
1338 	}
1339 
1340 	return (nl_modify_ifp_generic(ifp, ifd->lattrs, ifd->bm, ifd->npt));
1341 }
1342 
1343 /*
1344  *    {{nla_len=24, nla_type=IFLA_LINKINFO},
1345  *     [
1346  *      {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
1347  *      {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]}
1348  */
1349 static void
vlan_clone_dump_nl(struct ifnet * ifp,struct nl_writer * nw)1350 vlan_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw)
1351 {
1352 	uint32_t parent_index = 0;
1353 	uint16_t vlan_id = 0;
1354 	uint16_t vlan_proto = 0;
1355 
1356 	VLAN_SLOCK();
1357 	struct ifvlan *ifv = ifp->if_softc;
1358 	if (TRUNK(ifv) != NULL)
1359 		parent_index = PARENT(ifv)->if_index;
1360 	vlan_id = ifv->ifv_vid;
1361 	vlan_proto = ifv->ifv_proto;
1362 	VLAN_SUNLOCK();
1363 
1364 	if (parent_index != 0)
1365 		nlattr_add_u32(nw, IFLA_LINK, parent_index);
1366 
1367 	int off = nlattr_add_nested(nw, IFLA_LINKINFO);
1368 	if (off != 0) {
1369 		nlattr_add_string(nw, IFLA_INFO_KIND, "vlan");
1370 		int off2 = nlattr_add_nested(nw, IFLA_INFO_DATA);
1371 		if (off2 != 0) {
1372 			nlattr_add_u16(nw, IFLA_VLAN_ID, vlan_id);
1373 			nlattr_add_u16(nw, IFLA_VLAN_PROTOCOL, vlan_proto);
1374 			nlattr_set_len(nw, off2);
1375 		}
1376 		nlattr_set_len(nw, off);
1377 	}
1378 }
1379 
1380 static int
vlan_clone_destroy(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)1381 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
1382 {
1383 	struct ifvlan *ifv = ifp->if_softc;
1384 	int unit = ifp->if_dunit;
1385 
1386 	if (ifp->if_vlantrunk)
1387 		return (EBUSY);
1388 
1389 #ifdef ALTQ
1390 	IFQ_PURGE(&ifp->if_snd);
1391 #endif
1392 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1393 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1394 	/*
1395 	 * We should have the only reference to the ifv now, so we can now
1396 	 * drain any remaining lladdr task before freeing the ifnet and the
1397 	 * ifvlan.
1398 	 */
1399 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1400 	NET_EPOCH_WAIT();
1401 	if_free(ifp);
1402 	free(ifv, M_VLAN);
1403 	if (unit != IF_DUNIT_NONE)
1404 		ifc_free_unit(ifc, unit);
1405 
1406 	return (0);
1407 }
1408 
1409 /*
1410  * The ifp->if_init entry point for vlan(4) is a no-op.
1411  */
1412 static void
vlan_init(void * foo __unused)1413 vlan_init(void *foo __unused)
1414 {
1415 }
1416 
1417 /*
1418  * The if_transmit method for vlan(4) interface.
1419  */
1420 static int
vlan_transmit(struct ifnet * ifp,struct mbuf * m)1421 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1422 {
1423 	struct ifvlan *ifv;
1424 	struct ifnet *p;
1425 	int error, len, mcast;
1426 
1427 	NET_EPOCH_ASSERT();
1428 
1429 	ifv = ifp->if_softc;
1430 	if (TRUNK(ifv) == NULL) {
1431 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1432 		m_freem(m);
1433 		return (ENETDOWN);
1434 	}
1435 	p = PARENT(ifv);
1436 	len = m->m_pkthdr.len;
1437 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1438 
1439 	BPF_MTAP(ifp, m);
1440 
1441 #if defined(KERN_TLS) || defined(RATELIMIT)
1442 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1443 		struct vlan_snd_tag *vst;
1444 		struct m_snd_tag *mst;
1445 
1446 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1447 		mst = m->m_pkthdr.snd_tag;
1448 		vst = mst_to_vst(mst);
1449 		if (vst->tag->ifp != p) {
1450 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1451 			m_freem(m);
1452 			return (EAGAIN);
1453 		}
1454 
1455 		m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1456 		m_snd_tag_rele(mst);
1457 	}
1458 #endif
1459 
1460 	/*
1461 	 * Do not run parent's if_transmit() if the parent is not up,
1462 	 * or parent's driver will cause a system crash.
1463 	 */
1464 	if (!UP_AND_RUNNING(p)) {
1465 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1466 		m_freem(m);
1467 		return (ENETDOWN);
1468 	}
1469 
1470 	if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) {
1471 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1472 		return (0);
1473 	}
1474 
1475 	/*
1476 	 * Send it, precisely as ether_output() would have.
1477 	 */
1478 	error = (p->if_transmit)(p, m);
1479 	if (error == 0) {
1480 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1481 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1482 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1483 	} else
1484 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1485 	return (error);
1486 }
1487 
1488 static int
vlan_output(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * dst,struct route * ro)1489 vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1490     struct route *ro)
1491 {
1492 	struct ifvlan *ifv;
1493 	struct ifnet *p;
1494 
1495 	NET_EPOCH_ASSERT();
1496 
1497 	/*
1498 	 * Find the first non-VLAN parent interface.
1499 	 */
1500 	ifv = ifp->if_softc;
1501 	do {
1502 		if (TRUNK(ifv) == NULL) {
1503 			m_freem(m);
1504 			return (ENETDOWN);
1505 		}
1506 		p = PARENT(ifv);
1507 		ifv = p->if_softc;
1508 	} while (p->if_type == IFT_L2VLAN);
1509 
1510 	return p->if_output(ifp, m, dst, ro);
1511 }
1512 
1513 #ifdef ALTQ
1514 static void
vlan_altq_start(if_t ifp)1515 vlan_altq_start(if_t ifp)
1516 {
1517 	struct ifaltq *ifq = &ifp->if_snd;
1518 	struct mbuf *m;
1519 
1520 	IFQ_LOCK(ifq);
1521 	IFQ_DEQUEUE_NOLOCK(ifq, m);
1522 	while (m != NULL) {
1523 		vlan_transmit(ifp, m);
1524 		IFQ_DEQUEUE_NOLOCK(ifq, m);
1525 	}
1526 	IFQ_UNLOCK(ifq);
1527 }
1528 
1529 static int
vlan_altq_transmit(if_t ifp,struct mbuf * m)1530 vlan_altq_transmit(if_t ifp, struct mbuf *m)
1531 {
1532 	int err;
1533 
1534 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
1535 		IFQ_ENQUEUE(&ifp->if_snd, m, err);
1536 		if (err == 0)
1537 			vlan_altq_start(ifp);
1538 	} else
1539 		err = vlan_transmit(ifp, m);
1540 
1541 	return (err);
1542 }
1543 #endif	/* ALTQ */
1544 
1545 /*
1546  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1547  */
1548 static void
vlan_qflush(struct ifnet * ifp __unused)1549 vlan_qflush(struct ifnet *ifp __unused)
1550 {
1551 }
1552 
1553 static void
vlan_input(struct ifnet * ifp,struct mbuf * m)1554 vlan_input(struct ifnet *ifp, struct mbuf *m)
1555 {
1556 	struct ifvlantrunk *trunk;
1557 	struct ifvlan *ifv;
1558 	struct m_tag *mtag;
1559 	uint16_t vid, tag;
1560 
1561 	NET_EPOCH_ASSERT();
1562 
1563 	trunk = ifp->if_vlantrunk;
1564 	if (trunk == NULL) {
1565 		m_freem(m);
1566 		return;
1567 	}
1568 
1569 	if (m->m_flags & M_VLANTAG) {
1570 		/*
1571 		 * Packet is tagged, but m contains a normal
1572 		 * Ethernet frame; the tag is stored out-of-band.
1573 		 */
1574 		tag = m->m_pkthdr.ether_vtag;
1575 		m->m_flags &= ~M_VLANTAG;
1576 	} else {
1577 		struct ether_vlan_header *evl;
1578 
1579 		/*
1580 		 * Packet is tagged in-band as specified by 802.1q.
1581 		 */
1582 		switch (ifp->if_type) {
1583 		case IFT_ETHER:
1584 			if (m->m_len < sizeof(*evl) &&
1585 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1586 				if_printf(ifp, "cannot pullup VLAN header\n");
1587 				return;
1588 			}
1589 			evl = mtod(m, struct ether_vlan_header *);
1590 			tag = ntohs(evl->evl_tag);
1591 
1592 			/*
1593 			 * Remove the 802.1q header by copying the Ethernet
1594 			 * addresses over it and adjusting the beginning of
1595 			 * the data in the mbuf.  The encapsulated Ethernet
1596 			 * type field is already in place.
1597 			 */
1598 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1599 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
1600 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1601 			break;
1602 
1603 		default:
1604 #ifdef INVARIANTS
1605 			panic("%s: %s has unsupported if_type %u",
1606 			      __func__, ifp->if_xname, ifp->if_type);
1607 #endif
1608 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1609 			m_freem(m);
1610 			return;
1611 		}
1612 	}
1613 
1614 	vid = EVL_VLANOFTAG(tag);
1615 
1616 	ifv = vlan_gethash(trunk, vid);
1617 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1618 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1619 		m_freem(m);
1620 		return;
1621 	}
1622 
1623 	if (V_vlan_mtag_pcp) {
1624 		/*
1625 		 * While uncommon, it is possible that we will find a 802.1q
1626 		 * packet encapsulated inside another packet that also had an
1627 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
1628 		 * arriving over ethernet.  In that case, we replace the
1629 		 * existing 802.1q PCP m_tag value.
1630 		 */
1631 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1632 		if (mtag == NULL) {
1633 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1634 			    sizeof(uint8_t), M_NOWAIT);
1635 			if (mtag == NULL) {
1636 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1637 				m_freem(m);
1638 				return;
1639 			}
1640 			m_tag_prepend(m, mtag);
1641 		}
1642 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1643 	}
1644 
1645 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1646 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1647 
1648 	/* Pass it back through the parent's input routine. */
1649 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1650 }
1651 
1652 static void
vlan_lladdr_fn(void * arg,int pending __unused)1653 vlan_lladdr_fn(void *arg, int pending __unused)
1654 {
1655 	struct ifvlan *ifv;
1656 	struct ifnet *ifp;
1657 
1658 	ifv = (struct ifvlan *)arg;
1659 	ifp = ifv->ifv_ifp;
1660 
1661 	CURVNET_SET(ifp->if_vnet);
1662 
1663 	/* The ifv_ifp already has the lladdr copied in. */
1664 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1665 
1666 	CURVNET_RESTORE();
1667 }
1668 
1669 static int
vlan_config(struct ifvlan * ifv,struct ifnet * p,uint16_t vid,uint16_t proto)1670 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid,
1671 	uint16_t proto)
1672 {
1673 	struct epoch_tracker et;
1674 	struct ifvlantrunk *trunk;
1675 	struct ifnet *ifp;
1676 	int error = 0;
1677 
1678 	/*
1679 	 * We can handle non-ethernet hardware types as long as
1680 	 * they handle the tagging and headers themselves.
1681 	 */
1682 	if (p->if_type != IFT_ETHER &&
1683 	    p->if_type != IFT_L2VLAN &&
1684 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1685 		return (EPROTONOSUPPORT);
1686 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1687 		return (EPROTONOSUPPORT);
1688 	/*
1689 	 * Don't let the caller set up a VLAN VID with
1690 	 * anything except VLID bits.
1691 	 * VID numbers 0x0 and 0xFFF are reserved.
1692 	 */
1693 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1694 		return (EINVAL);
1695 	if (ifv->ifv_trunk) {
1696 		trunk = ifv->ifv_trunk;
1697 		if (trunk->parent != p)
1698 			return (EBUSY);
1699 
1700 		VLAN_XLOCK();
1701 
1702 		ifv->ifv_proto = proto;
1703 
1704 		if (ifv->ifv_vid != vid) {
1705 			int oldvid = ifv->ifv_vid;
1706 
1707 			/* Re-hash */
1708 			vlan_remhash(trunk, ifv);
1709 			ifv->ifv_vid = vid;
1710 			error = vlan_inshash(trunk, ifv);
1711 			if (error) {
1712 				int ret __diagused;
1713 
1714 				ifv->ifv_vid = oldvid;
1715 				/* Re-insert back where we found it. */
1716 				ret = vlan_inshash(trunk, ifv);
1717 				MPASS(ret == 0);
1718 			}
1719 		}
1720 		/* Will unlock */
1721 		goto done;
1722 	}
1723 
1724 	VLAN_XLOCK();
1725 	if (p->if_vlantrunk == NULL) {
1726 		trunk = malloc(sizeof(struct ifvlantrunk),
1727 		    M_VLAN, M_WAITOK | M_ZERO);
1728 		vlan_inithash(trunk);
1729 		TRUNK_LOCK_INIT(trunk);
1730 		TRUNK_WLOCK(trunk);
1731 		p->if_vlantrunk = trunk;
1732 		trunk->parent = p;
1733 		if_ref(trunk->parent);
1734 		TRUNK_WUNLOCK(trunk);
1735 	} else {
1736 		trunk = p->if_vlantrunk;
1737 	}
1738 
1739 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
1740 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
1741 	error = vlan_inshash(trunk, ifv);
1742 	if (error)
1743 		goto done;
1744 	ifv->ifv_proto = proto;
1745 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1746 	ifv->ifv_mintu = ETHERMIN;
1747 	ifv->ifv_pflags = 0;
1748 	ifv->ifv_capenable = -1;
1749 
1750 	/*
1751 	 * If the parent supports the VLAN_MTU capability,
1752 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1753 	 * use it.
1754 	 */
1755 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1756 		/*
1757 		 * No need to fudge the MTU since the parent can
1758 		 * handle extended frames.
1759 		 */
1760 		ifv->ifv_mtufudge = 0;
1761 	} else {
1762 		/*
1763 		 * Fudge the MTU by the encapsulation size.  This
1764 		 * makes us incompatible with strictly compliant
1765 		 * 802.1Q implementations, but allows us to use
1766 		 * the feature with other NetBSD implementations,
1767 		 * which might still be useful.
1768 		 */
1769 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1770 	}
1771 
1772 	ifv->ifv_trunk = trunk;
1773 	ifp = ifv->ifv_ifp;
1774 	/*
1775 	 * Initialize fields from our parent.  This duplicates some
1776 	 * work with ether_ifattach() but allows for non-ethernet
1777 	 * interfaces to also work.
1778 	 */
1779 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1780 	ifp->if_baudrate = p->if_baudrate;
1781 	ifp->if_input = p->if_input;
1782 	ifp->if_resolvemulti = p->if_resolvemulti;
1783 	ifp->if_addrlen = p->if_addrlen;
1784 	ifp->if_broadcastaddr = p->if_broadcastaddr;
1785 	ifp->if_pcp = ifv->ifv_pcp;
1786 
1787 	/*
1788 	 * We wrap the parent's if_output using vlan_output to ensure that it
1789 	 * can't become stale.
1790 	 */
1791 	ifp->if_output = vlan_output;
1792 
1793 	/*
1794 	 * Copy only a selected subset of flags from the parent.
1795 	 * Other flags are none of our business.
1796 	 */
1797 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1798 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
1799 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1800 #undef VLAN_COPY_FLAGS
1801 
1802 	ifp->if_link_state = p->if_link_state;
1803 
1804 	NET_EPOCH_ENTER(et);
1805 	vlan_capabilities(ifv);
1806 	NET_EPOCH_EXIT(et);
1807 
1808 	/*
1809 	 * Set up our interface address to reflect the underlying
1810 	 * physical interface's.
1811 	 */
1812 	TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1813 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1814 	    p->if_addrlen;
1815 
1816 	/*
1817 	 * Do not schedule link address update if it was the same
1818 	 * as previous parent's. This helps avoid updating for each
1819 	 * associated llentry.
1820 	 */
1821 	if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) {
1822 		bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1823 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
1824 	}
1825 
1826 	/* We are ready for operation now. */
1827 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1828 
1829 	/* Update flags on the parent, if necessary. */
1830 	vlan_setflags(ifp, 1);
1831 
1832 	/*
1833 	 * Configure multicast addresses that may already be
1834 	 * joined on the vlan device.
1835 	 */
1836 	(void)vlan_setmulti(ifp);
1837 
1838 done:
1839 	if (error == 0)
1840 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1841 	VLAN_XUNLOCK();
1842 
1843 	return (error);
1844 }
1845 
1846 static void
vlan_unconfig(struct ifnet * ifp)1847 vlan_unconfig(struct ifnet *ifp)
1848 {
1849 
1850 	VLAN_XLOCK();
1851 	vlan_unconfig_locked(ifp, 0);
1852 	VLAN_XUNLOCK();
1853 }
1854 
1855 static void
vlan_unconfig_locked(struct ifnet * ifp,int departing)1856 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1857 {
1858 	struct ifvlantrunk *trunk;
1859 	struct vlan_mc_entry *mc;
1860 	struct ifvlan *ifv;
1861 	struct ifnet  *parent;
1862 	int error;
1863 
1864 	VLAN_XLOCK_ASSERT();
1865 
1866 	ifv = ifp->if_softc;
1867 	trunk = ifv->ifv_trunk;
1868 	parent = NULL;
1869 
1870 	if (trunk != NULL) {
1871 		parent = trunk->parent;
1872 
1873 		/*
1874 		 * Since the interface is being unconfigured, we need to
1875 		 * empty the list of multicast groups that we may have joined
1876 		 * while we were alive from the parent's list.
1877 		 */
1878 		while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1879 			/*
1880 			 * If the parent interface is being detached,
1881 			 * all its multicast addresses have already
1882 			 * been removed.  Warn about errors if
1883 			 * if_delmulti() does fail, but don't abort as
1884 			 * all callers expect vlan destruction to
1885 			 * succeed.
1886 			 */
1887 			if (!departing) {
1888 				error = if_delmulti(parent,
1889 				    (struct sockaddr *)&mc->mc_addr);
1890 				if (error)
1891 					if_printf(ifp,
1892 		    "Failed to delete multicast address from parent: %d\n",
1893 					    error);
1894 			}
1895 			CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1896 			NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
1897 		}
1898 
1899 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1900 
1901 		vlan_remhash(trunk, ifv);
1902 		ifv->ifv_trunk = NULL;
1903 
1904 		/*
1905 		 * Check if we were the last.
1906 		 */
1907 		if (trunk->refcnt == 0) {
1908 			parent->if_vlantrunk = NULL;
1909 			NET_EPOCH_WAIT();
1910 			trunk_destroy(trunk);
1911 		}
1912 	}
1913 
1914 	/* Disconnect from parent. */
1915 	if (ifv->ifv_pflags)
1916 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1917 	ifp->if_mtu = ETHERMTU;
1918 	ifp->if_link_state = LINK_STATE_UNKNOWN;
1919 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1920 
1921 	/*
1922 	 * Only dispatch an event if vlan was
1923 	 * attached, otherwise there is nothing
1924 	 * to cleanup anyway.
1925 	 */
1926 	if (parent != NULL)
1927 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1928 }
1929 
1930 /* Handle a reference counted flag that should be set on the parent as well */
1931 static int
vlan_setflag(struct ifnet * ifp,int flag,int status,int (* func)(struct ifnet *,int))1932 vlan_setflag(struct ifnet *ifp, int flag, int status,
1933 	     int (*func)(struct ifnet *, int))
1934 {
1935 	struct ifvlan *ifv;
1936 	int error;
1937 
1938 	VLAN_SXLOCK_ASSERT();
1939 
1940 	ifv = ifp->if_softc;
1941 	status = status ? (ifp->if_flags & flag) : 0;
1942 	/* Now "status" contains the flag value or 0 */
1943 
1944 	/*
1945 	 * See if recorded parent's status is different from what
1946 	 * we want it to be.  If it is, flip it.  We record parent's
1947 	 * status in ifv_pflags so that we won't clear parent's flag
1948 	 * we haven't set.  In fact, we don't clear or set parent's
1949 	 * flags directly, but get or release references to them.
1950 	 * That's why we can be sure that recorded flags still are
1951 	 * in accord with actual parent's flags.
1952 	 */
1953 	if (status != (ifv->ifv_pflags & flag)) {
1954 		error = (*func)(PARENT(ifv), status);
1955 		if (error)
1956 			return (error);
1957 		ifv->ifv_pflags &= ~flag;
1958 		ifv->ifv_pflags |= status;
1959 	}
1960 	return (0);
1961 }
1962 
1963 /*
1964  * Handle IFF_* flags that require certain changes on the parent:
1965  * if "status" is true, update parent's flags respective to our if_flags;
1966  * if "status" is false, forcedly clear the flags set on parent.
1967  */
1968 static int
vlan_setflags(struct ifnet * ifp,int status)1969 vlan_setflags(struct ifnet *ifp, int status)
1970 {
1971 	int error, i;
1972 
1973 	for (i = 0; vlan_pflags[i].flag; i++) {
1974 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
1975 				     status, vlan_pflags[i].func);
1976 		if (error)
1977 			return (error);
1978 	}
1979 	return (0);
1980 }
1981 
1982 /* Inform all vlans that their parent has changed link state */
1983 static void
vlan_link_state(struct ifnet * ifp)1984 vlan_link_state(struct ifnet *ifp)
1985 {
1986 	struct epoch_tracker et;
1987 	struct ifvlantrunk *trunk;
1988 	struct ifvlan *ifv;
1989 
1990 	NET_EPOCH_ENTER(et);
1991 	trunk = ifp->if_vlantrunk;
1992 	if (trunk == NULL) {
1993 		NET_EPOCH_EXIT(et);
1994 		return;
1995 	}
1996 
1997 	TRUNK_WLOCK(trunk);
1998 	VLAN_FOREACH(ifv, trunk) {
1999 		ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
2000 		if_link_state_change(ifv->ifv_ifp,
2001 		    trunk->parent->if_link_state);
2002 	}
2003 	TRUNK_WUNLOCK(trunk);
2004 	NET_EPOCH_EXIT(et);
2005 }
2006 
2007 static void
vlan_capabilities(struct ifvlan * ifv)2008 vlan_capabilities(struct ifvlan *ifv)
2009 {
2010 	struct ifnet *p;
2011 	struct ifnet *ifp;
2012 	struct ifnet_hw_tsomax hw_tsomax;
2013 	int cap = 0, ena = 0, mena;
2014 	u_long hwa = 0;
2015 
2016 	NET_EPOCH_ASSERT();
2017 	VLAN_SXLOCK_ASSERT();
2018 
2019 	p = PARENT(ifv);
2020 	ifp = ifv->ifv_ifp;
2021 
2022 	/* Mask parent interface enabled capabilities disabled by user. */
2023 	mena = p->if_capenable & ifv->ifv_capenable;
2024 
2025 	/*
2026 	 * If the parent interface can do checksum offloading
2027 	 * on VLANs, then propagate its hardware-assisted
2028 	 * checksumming flags. Also assert that checksum
2029 	 * offloading requires hardware VLAN tagging.
2030 	 */
2031 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
2032 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
2033 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
2034 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
2035 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
2036 		if (ena & IFCAP_TXCSUM)
2037 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
2038 			    CSUM_UDP | CSUM_SCTP);
2039 		if (ena & IFCAP_TXCSUM_IPV6)
2040 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
2041 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
2042 	}
2043 
2044 	/*
2045 	 * If the parent interface can do TSO on VLANs then
2046 	 * propagate the hardware-assisted flag. TSO on VLANs
2047 	 * does not necessarily require hardware VLAN tagging.
2048 	 */
2049 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
2050 	if_hw_tsomax_common(p, &hw_tsomax);
2051 	if_hw_tsomax_update(ifp, &hw_tsomax);
2052 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
2053 		cap |= p->if_capabilities & IFCAP_TSO;
2054 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
2055 		ena |= mena & IFCAP_TSO;
2056 		if (ena & IFCAP_TSO)
2057 			hwa |= p->if_hwassist & CSUM_TSO;
2058 	}
2059 
2060 	/*
2061 	 * If the parent interface can do LRO and checksum offloading on
2062 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
2063 	 * cost nothing, while false negative may lead to some confusions.
2064 	 */
2065 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
2066 		cap |= p->if_capabilities & IFCAP_LRO;
2067 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
2068 		ena |= mena & IFCAP_LRO;
2069 
2070 	/*
2071 	 * If the parent interface can offload TCP connections over VLANs then
2072 	 * propagate its TOE capability to the VLAN interface.
2073 	 *
2074 	 * All TOE drivers in the tree today can deal with VLANs.  If this
2075 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
2076 	 * with its own bit.
2077 	 */
2078 #define	IFCAP_VLAN_TOE IFCAP_TOE
2079 	if (p->if_capabilities & IFCAP_VLAN_TOE)
2080 		cap |= p->if_capabilities & IFCAP_TOE;
2081 	if (p->if_capenable & IFCAP_VLAN_TOE) {
2082 		SETTOEDEV(ifp, TOEDEV(p));
2083 		ena |= mena & IFCAP_TOE;
2084 	}
2085 
2086 	/*
2087 	 * If the parent interface supports dynamic link state, so does the
2088 	 * VLAN interface.
2089 	 */
2090 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
2091 	ena |= (mena & IFCAP_LINKSTATE);
2092 
2093 #ifdef RATELIMIT
2094 	/*
2095 	 * If the parent interface supports ratelimiting, so does the
2096 	 * VLAN interface.
2097 	 */
2098 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
2099 	ena |= (mena & IFCAP_TXRTLMT);
2100 #endif
2101 
2102 	/*
2103 	 * If the parent interface supports unmapped mbufs, so does
2104 	 * the VLAN interface.  Note that this should be fine even for
2105 	 * interfaces that don't support hardware tagging as headers
2106 	 * are prepended in normal mbufs to unmapped mbufs holding
2107 	 * payload data.
2108 	 */
2109 	cap |= (p->if_capabilities & IFCAP_MEXTPG);
2110 	ena |= (mena & IFCAP_MEXTPG);
2111 
2112 	/*
2113 	 * If the parent interface can offload encryption and segmentation
2114 	 * of TLS records over TCP, propagate it's capability to the VLAN
2115 	 * interface.
2116 	 *
2117 	 * All TLS drivers in the tree today can deal with VLANs.  If
2118 	 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
2119 	 * defined.
2120 	 */
2121 	if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
2122 		cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
2123 	if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
2124 		ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
2125 
2126 	ifp->if_capabilities = cap;
2127 	ifp->if_capenable = ena;
2128 	ifp->if_hwassist = hwa;
2129 }
2130 
2131 static void
vlan_trunk_capabilities(struct ifnet * ifp)2132 vlan_trunk_capabilities(struct ifnet *ifp)
2133 {
2134 	struct epoch_tracker et;
2135 	struct ifvlantrunk *trunk;
2136 	struct ifvlan *ifv;
2137 
2138 	VLAN_SLOCK();
2139 	trunk = ifp->if_vlantrunk;
2140 	if (trunk == NULL) {
2141 		VLAN_SUNLOCK();
2142 		return;
2143 	}
2144 	NET_EPOCH_ENTER(et);
2145 	VLAN_FOREACH(ifv, trunk)
2146 		vlan_capabilities(ifv);
2147 	NET_EPOCH_EXIT(et);
2148 	VLAN_SUNLOCK();
2149 }
2150 
2151 static int
vlan_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)2152 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2153 {
2154 	struct ifnet *p;
2155 	struct ifreq *ifr;
2156 #ifdef INET
2157 	struct ifaddr *ifa;
2158 #endif
2159 	struct ifvlan *ifv;
2160 	struct ifvlantrunk *trunk;
2161 	struct vlanreq vlr;
2162 	int error = 0, oldmtu;
2163 
2164 	ifr = (struct ifreq *)data;
2165 #ifdef INET
2166 	ifa = (struct ifaddr *) data;
2167 #endif
2168 	ifv = ifp->if_softc;
2169 
2170 	switch (cmd) {
2171 	case SIOCSIFADDR:
2172 		ifp->if_flags |= IFF_UP;
2173 #ifdef INET
2174 		if (ifa->ifa_addr->sa_family == AF_INET)
2175 			arp_ifinit(ifp, ifa);
2176 #endif
2177 		break;
2178 	case SIOCGIFADDR:
2179 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
2180 		    ifp->if_addrlen);
2181 		break;
2182 	case SIOCGIFMEDIA:
2183 		VLAN_SLOCK();
2184 		if (TRUNK(ifv) != NULL) {
2185 			p = PARENT(ifv);
2186 			if_ref(p);
2187 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
2188 			if_rele(p);
2189 			/* Limit the result to the parent's current config. */
2190 			if (error == 0) {
2191 				struct ifmediareq *ifmr;
2192 
2193 				ifmr = (struct ifmediareq *)data;
2194 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
2195 					ifmr->ifm_count = 1;
2196 					error = copyout(&ifmr->ifm_current,
2197 						ifmr->ifm_ulist,
2198 						sizeof(int));
2199 				}
2200 			}
2201 		} else {
2202 			error = EINVAL;
2203 		}
2204 		VLAN_SUNLOCK();
2205 		break;
2206 
2207 	case SIOCSIFMEDIA:
2208 		error = EINVAL;
2209 		break;
2210 
2211 	case SIOCSIFMTU:
2212 		/*
2213 		 * Set the interface MTU.
2214 		 */
2215 		VLAN_SLOCK();
2216 		trunk = TRUNK(ifv);
2217 		if (trunk != NULL) {
2218 			TRUNK_WLOCK(trunk);
2219 			if (ifr->ifr_mtu >
2220 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
2221 			    ifr->ifr_mtu <
2222 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
2223 				error = EINVAL;
2224 			else
2225 				ifp->if_mtu = ifr->ifr_mtu;
2226 			TRUNK_WUNLOCK(trunk);
2227 		} else
2228 			error = EINVAL;
2229 		VLAN_SUNLOCK();
2230 		break;
2231 
2232 	case SIOCSETVLAN:
2233 #ifdef VIMAGE
2234 		/*
2235 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
2236 		 * interface to be delegated to a jail without allowing the
2237 		 * jail to change what underlying interface/VID it is
2238 		 * associated with.  We are not entirely convinced that this
2239 		 * is the right way to accomplish that policy goal.
2240 		 */
2241 		if (ifp->if_vnet != ifp->if_home_vnet) {
2242 			error = EPERM;
2243 			break;
2244 		}
2245 #endif
2246 		error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
2247 		if (error)
2248 			break;
2249 		if (vlr.vlr_parent[0] == '\0') {
2250 			vlan_unconfig(ifp);
2251 			break;
2252 		}
2253 		p = ifunit_ref(vlr.vlr_parent);
2254 		if (p == NULL) {
2255 			error = ENOENT;
2256 			break;
2257 		}
2258 		if (vlr.vlr_proto == 0)
2259 			vlr.vlr_proto = ETHERTYPE_VLAN;
2260 		oldmtu = ifp->if_mtu;
2261 		error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto);
2262 		if_rele(p);
2263 
2264 		/*
2265 		 * VLAN MTU may change during addition of the vlandev.
2266 		 * If it did, do network layer specific procedure.
2267 		 */
2268 		if (ifp->if_mtu != oldmtu)
2269 			if_notifymtu(ifp);
2270 		break;
2271 
2272 	case SIOCGETVLAN:
2273 #ifdef VIMAGE
2274 		if (ifp->if_vnet != ifp->if_home_vnet) {
2275 			error = EPERM;
2276 			break;
2277 		}
2278 #endif
2279 		bzero(&vlr, sizeof(vlr));
2280 		VLAN_SLOCK();
2281 		if (TRUNK(ifv) != NULL) {
2282 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
2283 			    sizeof(vlr.vlr_parent));
2284 			vlr.vlr_tag = ifv->ifv_vid;
2285 			vlr.vlr_proto = ifv->ifv_proto;
2286 		}
2287 		VLAN_SUNLOCK();
2288 		error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
2289 		break;
2290 
2291 	case SIOCSIFFLAGS:
2292 		/*
2293 		 * We should propagate selected flags to the parent,
2294 		 * e.g., promiscuous mode.
2295 		 */
2296 		VLAN_SLOCK();
2297 		if (TRUNK(ifv) != NULL)
2298 			error = vlan_setflags(ifp, 1);
2299 		VLAN_SUNLOCK();
2300 		break;
2301 
2302 	case SIOCADDMULTI:
2303 	case SIOCDELMULTI:
2304 		/*
2305 		 * If we don't have a parent, just remember the membership for
2306 		 * when we do.
2307 		 *
2308 		 * XXX We need the rmlock here to avoid sleeping while
2309 		 * holding in6_multi_mtx.
2310 		 */
2311 		VLAN_XLOCK();
2312 		trunk = TRUNK(ifv);
2313 		if (trunk != NULL)
2314 			error = vlan_setmulti(ifp);
2315 		VLAN_XUNLOCK();
2316 
2317 		break;
2318 	case SIOCGVLANPCP:
2319 #ifdef VIMAGE
2320 		if (ifp->if_vnet != ifp->if_home_vnet) {
2321 			error = EPERM;
2322 			break;
2323 		}
2324 #endif
2325 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
2326 		break;
2327 
2328 	case SIOCSVLANPCP:
2329 #ifdef VIMAGE
2330 		if (ifp->if_vnet != ifp->if_home_vnet) {
2331 			error = EPERM;
2332 			break;
2333 		}
2334 #endif
2335 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
2336 		if (error)
2337 			break;
2338 		if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) {
2339 			error = EINVAL;
2340 			break;
2341 		}
2342 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
2343 		ifp->if_pcp = ifv->ifv_pcp;
2344 		/* broadcast event about PCP change */
2345 		EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
2346 		break;
2347 
2348 	case SIOCSIFCAP:
2349 		VLAN_SLOCK();
2350 		ifv->ifv_capenable = ifr->ifr_reqcap;
2351 		trunk = TRUNK(ifv);
2352 		if (trunk != NULL) {
2353 			struct epoch_tracker et;
2354 
2355 			NET_EPOCH_ENTER(et);
2356 			vlan_capabilities(ifv);
2357 			NET_EPOCH_EXIT(et);
2358 		}
2359 		VLAN_SUNLOCK();
2360 		break;
2361 
2362 	default:
2363 		error = EINVAL;
2364 		break;
2365 	}
2366 
2367 	return (error);
2368 }
2369 
2370 #if defined(KERN_TLS) || defined(RATELIMIT)
2371 static int
vlan_snd_tag_alloc(struct ifnet * ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** ppmt)2372 vlan_snd_tag_alloc(struct ifnet *ifp,
2373     union if_snd_tag_alloc_params *params,
2374     struct m_snd_tag **ppmt)
2375 {
2376 	struct epoch_tracker et;
2377 	const struct if_snd_tag_sw *sw;
2378 	struct vlan_snd_tag *vst;
2379 	struct ifvlan *ifv;
2380 	struct ifnet *parent;
2381 	struct m_snd_tag *mst;
2382 	int error;
2383 
2384 	NET_EPOCH_ENTER(et);
2385 	ifv = ifp->if_softc;
2386 
2387 	switch (params->hdr.type) {
2388 #ifdef RATELIMIT
2389 	case IF_SND_TAG_TYPE_UNLIMITED:
2390 		sw = &vlan_snd_tag_ul_sw;
2391 		break;
2392 	case IF_SND_TAG_TYPE_RATE_LIMIT:
2393 		sw = &vlan_snd_tag_rl_sw;
2394 		break;
2395 #endif
2396 #ifdef KERN_TLS
2397 	case IF_SND_TAG_TYPE_TLS:
2398 		sw = &vlan_snd_tag_tls_sw;
2399 		break;
2400 	case IF_SND_TAG_TYPE_TLS_RX:
2401 		sw = NULL;
2402 		if (params->tls_rx.vlan_id != 0)
2403 			goto failure;
2404 		params->tls_rx.vlan_id = ifv->ifv_vid;
2405 		break;
2406 #ifdef RATELIMIT
2407 	case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
2408 		sw = &vlan_snd_tag_tls_rl_sw;
2409 		break;
2410 #endif
2411 #endif
2412 	default:
2413 		goto failure;
2414 	}
2415 
2416 	if (ifv->ifv_trunk != NULL)
2417 		parent = PARENT(ifv);
2418 	else
2419 		parent = NULL;
2420 	if (parent == NULL)
2421 		goto failure;
2422 	if_ref(parent);
2423 	NET_EPOCH_EXIT(et);
2424 
2425 	if (sw != NULL) {
2426 		vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2427 		if (vst == NULL) {
2428 			if_rele(parent);
2429 			return (ENOMEM);
2430 		}
2431 	} else
2432 		vst = NULL;
2433 
2434 	error = m_snd_tag_alloc(parent, params, &mst);
2435 	if_rele(parent);
2436 	if (error) {
2437 		free(vst, M_VLAN);
2438 		return (error);
2439 	}
2440 
2441 	if (sw != NULL) {
2442 		m_snd_tag_init(&vst->com, ifp, sw);
2443 		vst->tag = mst;
2444 
2445 		*ppmt = &vst->com;
2446 	} else
2447 		*ppmt = mst;
2448 
2449 	return (0);
2450 failure:
2451 	NET_EPOCH_EXIT(et);
2452 	return (EOPNOTSUPP);
2453 }
2454 
2455 static struct m_snd_tag *
vlan_next_snd_tag(struct m_snd_tag * mst)2456 vlan_next_snd_tag(struct m_snd_tag *mst)
2457 {
2458 	struct vlan_snd_tag *vst;
2459 
2460 	vst = mst_to_vst(mst);
2461 	return (vst->tag);
2462 }
2463 
2464 static int
vlan_snd_tag_modify(struct m_snd_tag * mst,union if_snd_tag_modify_params * params)2465 vlan_snd_tag_modify(struct m_snd_tag *mst,
2466     union if_snd_tag_modify_params *params)
2467 {
2468 	struct vlan_snd_tag *vst;
2469 
2470 	vst = mst_to_vst(mst);
2471 	return (vst->tag->sw->snd_tag_modify(vst->tag, params));
2472 }
2473 
2474 static int
vlan_snd_tag_query(struct m_snd_tag * mst,union if_snd_tag_query_params * params)2475 vlan_snd_tag_query(struct m_snd_tag *mst,
2476     union if_snd_tag_query_params *params)
2477 {
2478 	struct vlan_snd_tag *vst;
2479 
2480 	vst = mst_to_vst(mst);
2481 	return (vst->tag->sw->snd_tag_query(vst->tag, params));
2482 }
2483 
2484 static void
vlan_snd_tag_free(struct m_snd_tag * mst)2485 vlan_snd_tag_free(struct m_snd_tag *mst)
2486 {
2487 	struct vlan_snd_tag *vst;
2488 
2489 	vst = mst_to_vst(mst);
2490 	m_snd_tag_rele(vst->tag);
2491 	free(vst, M_VLAN);
2492 }
2493 
2494 static void
vlan_ratelimit_query(struct ifnet * ifp __unused,struct if_ratelimit_query_results * q)2495 vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
2496 {
2497 	/*
2498 	 * For vlan, we have an indirect
2499 	 * interface. The caller needs to
2500 	 * get a ratelimit tag on the actual
2501 	 * interface the flow will go on.
2502 	 */
2503 	q->rate_table = NULL;
2504 	q->flags = RT_IS_INDIRECT;
2505 	q->max_flows = 0;
2506 	q->number_of_rates = 0;
2507 }
2508 
2509 #endif
2510