xref: /freebsd/sys/net/if_vlan.c (revision dfcd04c4c811096e5183a406d0f001a7c0ffa60a)
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 "opt_inet.h"
46 #include "opt_inet6.h"
47 #include "opt_ipsec.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_capenable2;
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;
198 #ifndef VLAN_ARRAY
199 	CK_SLIST_ENTRY(ifvlan) ifv_list;
200 #endif
201 };
202 
203 /* Special flags we should propagate to parent. */
204 static struct {
205 	int flag;
206 	int (*func)(struct ifnet *, int);
207 } vlan_pflags[] = {
208 	{IFF_PROMISC, ifpromisc},
209 	{IFF_ALLMULTI, if_allmulti},
210 	{0, NULL}
211 };
212 
213 VNET_DECLARE(int, vlan_mtag_pcp);
214 #define	V_vlan_mtag_pcp	VNET(vlan_mtag_pcp)
215 
216 static const char vlanname[] = "vlan";
217 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
218 
219 static eventhandler_tag ifdetach_tag;
220 static eventhandler_tag iflladdr_tag;
221 static eventhandler_tag ifevent_tag;
222 
223 /*
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).
229  *
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.
234  *
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.
239  *
240  */
241 #define _VLAN_SX_ID ifv_sx
242 
243 static struct sx _VLAN_SX_ID;
244 
245 #define VLAN_LOCKING_INIT() \
246 	sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE)
247 
248 #define VLAN_LOCKING_DESTROY() \
249 	sx_destroy(&_VLAN_SX_ID)
250 
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)
258 
259 /*
260  * We also have a per-trunk mutex that should be acquired when changing
261  * its state.
262  */
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);
268 
269 /*
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.
274  */
275 #ifndef VLAN_ARRAY
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,
282 	uint16_t vid);
283 #endif
284 static	void trunk_destroy(struct ifvlantrunk *trunk);
285 
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 *);
300 #endif
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);
307 #ifdef ALTQ
308 static void vlan_altq_start(struct ifnet *ifp);
309 static	int vlan_altq_transmit(struct ifnet *ifp, struct mbuf *m);
310 #endif
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,
316 	uint16_t proto);
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);
320 
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);
326 
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);
331 
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);
335 
336 static  void vlan_lladdr_fn(void *arg, int pending);
337 
338 static struct if_clone *vlan_cloner;
339 
340 #ifdef VIMAGE
341 VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
342 #define	V_vlan_cloner	VNET(vlan_cloner)
343 #endif
344 
345 #ifdef RATELIMIT
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
352 };
353 
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
360 };
361 #endif
362 
363 #ifdef KERN_TLS
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
370 };
371 
372 #ifdef RATELIMIT
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
379 };
380 #endif
381 #endif
382 
383 static void
vlan_mc_free(struct epoch_context * ctx)384 vlan_mc_free(struct epoch_context *ctx)
385 {
386 	struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
387 	free(mc, M_VLAN);
388 }
389 
390 #ifndef VLAN_ARRAY
391 #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
392 
393 static void
vlan_inithash(struct ifvlantrunk * trunk)394 vlan_inithash(struct ifvlantrunk *trunk)
395 {
396 	int i, n;
397 
398 	/*
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.
402 	 */
403 
404 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
405 	    ("%s: hash already initialized", __func__));
406 
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]);
413 }
414 
415 static void
vlan_freehash(struct ifvlantrunk * trunk)416 vlan_freehash(struct ifvlantrunk *trunk)
417 {
418 #ifdef INVARIANTS
419 	int i;
420 
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__));
425 #endif
426 	free(trunk->hash, M_VLAN);
427 	trunk->hash = NULL;
428 	trunk->hwidth = trunk->hmask = 0;
429 }
430 
431 static int
vlan_inshash(struct ifvlantrunk * trunk,struct ifvlan * ifv)432 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
433 {
434 	int i, b;
435 	struct ifvlan *ifv2;
436 
437 	VLAN_XLOCK_ASSERT();
438 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
439 
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)
444 			return (EEXIST);
445 
446 	/*
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
449 	 * buckets/2.
450 	 */
451 	if (trunk->refcnt > (b * b) / 2) {
452 		vlan_growhash(trunk, 1);
453 		i = HASH(ifv->ifv_vid, trunk->hmask);
454 	}
455 	CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
456 	trunk->refcnt++;
457 
458 	return (0);
459 }
460 
461 static int
vlan_remhash(struct ifvlantrunk * trunk,struct ifvlan * ifv)462 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
463 {
464 	int i, b;
465 	struct ifvlan *ifv2;
466 
467 	VLAN_XLOCK_ASSERT();
468 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
469 
470 	b = 1 << (trunk->hwidth - 1);
471 	i = HASH(ifv->ifv_vid, trunk->hmask);
472 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
473 		if (ifv2 == ifv) {
474 			trunk->refcnt--;
475 			CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
476 			if (trunk->refcnt < (b * b) / 2)
477 				vlan_growhash(trunk, -1);
478 			return (0);
479 		}
480 
481 	panic("%s: vlan not found\n", __func__);
482 	return (ENOENT); /*NOTREACHED*/
483 }
484 
485 /*
486  * Grow the hash larger or smaller if memory permits.
487  */
488 static void
vlan_growhash(struct ifvlantrunk * trunk,int howmuch)489 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
490 {
491 	struct ifvlan *ifv;
492 	struct ifvlanhead *hash2;
493 	int hwidth2, i, j, n, n2;
494 
495 	VLAN_XLOCK_ASSERT();
496 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
497 
498 	if (howmuch == 0) {
499 		/* Harmless yet obvious coding error */
500 		printf("%s: howmuch is 0\n", __func__);
501 		return;
502 	}
503 
504 	hwidth2 = trunk->hwidth + howmuch;
505 	n = 1 << trunk->hwidth;
506 	n2 = 1 << hwidth2;
507 	/* Do not shrink the table below the default */
508 	if (hwidth2 < VLAN_DEF_HWIDTH)
509 		return;
510 
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);
519 		}
520 	NET_EPOCH_WAIT();
521 	free(trunk->hash, M_VLAN);
522 	trunk->hash = hash2;
523 	trunk->hwidth = hwidth2;
524 	trunk->hmask = n2 - 1;
525 
526 	if (bootverbose)
527 		if_printf(trunk->parent,
528 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
529 }
530 
531 static __inline struct ifvlan *
vlan_gethash(struct ifvlantrunk * trunk,uint16_t vid)532 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
533 {
534 	struct ifvlan *ifv;
535 
536 	NET_EPOCH_ASSERT();
537 
538 	CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
539 		if (ifv->ifv_vid == vid)
540 			return (ifv);
541 	return (NULL);
542 }
543 
544 #if 0
545 /* Debugging code to view the hashtables. */
546 static void
547 vlan_dumphash(struct ifvlantrunk *trunk)
548 {
549 	int i;
550 	struct ifvlan *ifv;
551 
552 	for (i = 0; i < (1 << trunk->hwidth); i++) {
553 		printf("%d: ", i);
554 		CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
555 			printf("%s ", ifv->ifv_ifp->if_xname);
556 		printf("\n");
557 	}
558 }
559 #endif /* 0 */
560 #else
561 
562 static __inline struct ifvlan *
vlan_gethash(struct ifvlantrunk * trunk,uint16_t vid)563 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
564 {
565 
566 	return trunk->vlans[vid];
567 }
568 
569 static __inline int
vlan_inshash(struct ifvlantrunk * trunk,struct ifvlan * ifv)570 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
571 {
572 
573 	if (trunk->vlans[ifv->ifv_vid] != NULL)
574 		return EEXIST;
575 	trunk->vlans[ifv->ifv_vid] = ifv;
576 	trunk->refcnt++;
577 
578 	return (0);
579 }
580 
581 static __inline int
vlan_remhash(struct ifvlantrunk * trunk,struct ifvlan * ifv)582 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
583 {
584 
585 	trunk->vlans[ifv->ifv_vid] = NULL;
586 	trunk->refcnt--;
587 
588 	return (0);
589 }
590 
591 static __inline void
vlan_freehash(struct ifvlantrunk * trunk)592 vlan_freehash(struct ifvlantrunk *trunk)
593 {
594 }
595 
596 static __inline void
vlan_inithash(struct ifvlantrunk * trunk)597 vlan_inithash(struct ifvlantrunk *trunk)
598 {
599 }
600 
601 #endif /* !VLAN_ARRAY */
602 
603 static void
trunk_destroy(struct ifvlantrunk * trunk)604 trunk_destroy(struct ifvlantrunk *trunk)
605 {
606 	VLAN_XLOCK_ASSERT();
607 
608 	vlan_freehash(trunk);
609 	trunk->parent->if_vlantrunk = NULL;
610 	TRUNK_LOCK_DESTROY(trunk);
611 	if_rele(trunk->parent);
612 	free(trunk, M_VLAN);
613 }
614 
615 /*
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.
622  */
623 static int
vlan_setmulti(struct ifnet * ifp)624 vlan_setmulti(struct ifnet *ifp)
625 {
626 	struct ifnet		*ifp_p;
627 	struct ifmultiaddr	*ifma;
628 	struct ifvlan		*sc;
629 	struct vlan_mc_entry	*mc;
630 	int			error;
631 
632 	VLAN_XLOCK_ASSERT();
633 
634 	/* Find the parent. */
635 	sc = ifp->if_softc;
636 	ifp_p = PARENT(sc);
637 
638 	CURVNET_SET_QUIET(ifp_p->if_vnet);
639 
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);
645 	}
646 
647 	/* Now program new ones. */
648 	IF_ADDR_WLOCK(ifp);
649 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
650 		if (ifma->ifma_addr->sa_family != AF_LINK)
651 			continue;
652 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
653 		if (mc == NULL) {
654 			IF_ADDR_WUNLOCK(ifp);
655 			CURVNET_RESTORE();
656 			return (ENOMEM);
657 		}
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);
661 	}
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,
665 		    NULL);
666 		if (error) {
667 			CURVNET_RESTORE();
668 			return (error);
669 		}
670 	}
671 
672 	CURVNET_RESTORE();
673 	return (0);
674 }
675 
676 /*
677  * A handler for interface ifnet events.
678  */
679 static void
vlan_ifevent(void * arg __unused,struct ifnet * ifp,int event)680 vlan_ifevent(void *arg __unused, struct ifnet *ifp, int event)
681 {
682 	struct epoch_tracker et;
683 	struct ifvlan *ifv;
684 	struct ifvlantrunk *trunk;
685 
686 	if (event != IFNET_EVENT_UPDATE_BAUDRATE)
687 		return;
688 
689 	NET_EPOCH_ENTER(et);
690 	trunk = ifp->if_vlantrunk;
691 	if (trunk == NULL) {
692 		NET_EPOCH_EXIT(et);
693 		return;
694 	}
695 
696 	TRUNK_WLOCK(trunk);
697 	VLAN_FOREACH(ifv, trunk) {
698 		ifv->ifv_ifp->if_baudrate = ifp->if_baudrate;
699 	}
700 	TRUNK_WUNLOCK(trunk);
701 	NET_EPOCH_EXIT(et);
702 }
703 
704 /*
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.
708  */
709 static void
vlan_iflladdr(void * arg __unused,struct ifnet * ifp)710 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
711 {
712 	struct epoch_tracker et;
713 	struct ifvlan *ifv;
714 	struct ifnet *ifv_ifp;
715 	struct ifvlantrunk *trunk;
716 	struct sockaddr_dl *sdl;
717 
718 	/* Need the epoch since this is run on taskqueue_swi. */
719 	NET_EPOCH_ENTER(et);
720 	trunk = ifp->if_vlantrunk;
721 	if (trunk == NULL) {
722 		NET_EPOCH_EXIT(et);
723 		return;
724 	}
725 
726 	/*
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.
730 	 */
731 	TRUNK_WLOCK(trunk);
732 	VLAN_FOREACH(ifv, trunk) {
733 		/*
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
738 		 * lock.
739 		 */
740 		ifv_ifp = ifv->ifv_ifp;
741 		bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
742 		    ifp->if_addrlen);
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);
746 	}
747 	TRUNK_WUNLOCK(trunk);
748 	NET_EPOCH_EXIT(et);
749 }
750 
751 /*
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.
757  */
758 static void
vlan_ifdetach(void * arg __unused,struct ifnet * ifp)759 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
760 {
761 	struct ifvlan *ifv;
762 	struct ifvlantrunk *trunk;
763 
764 	VLAN_XLOCK();
765 	trunk = ifp->if_vlantrunk;
766 	if (trunk == NULL) {
767 		VLAN_XUNLOCK();
768 		return;
769 	}
770 
771 	/*
772 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
773 	 * Check trunk pointer after each vlan_unconfig() as it will
774 	 * free it and set to NULL after the last vlan was detached.
775 	 */
776 	VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
777 	    ifp->if_vlantrunk == NULL)
778 		vlan_unconfig_locked(ifv->ifv_ifp, 1);
779 
780 	/* Trunk should have been destroyed in vlan_unconfig(). */
781 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
782 	VLAN_XUNLOCK();
783 }
784 
785 /*
786  * Return the trunk device for a virtual interface.
787  */
788 static struct ifnet  *
vlan_trunkdev(struct ifnet * ifp)789 vlan_trunkdev(struct ifnet *ifp)
790 {
791 	struct ifvlan *ifv;
792 
793 	NET_EPOCH_ASSERT();
794 
795 	if (ifp->if_type != IFT_L2VLAN)
796 		return (NULL);
797 
798 	ifv = ifp->if_softc;
799 	ifp = NULL;
800 	if (ifv->ifv_trunk)
801 		ifp = PARENT(ifv);
802 	return (ifp);
803 }
804 
805 /*
806  * Return the 12-bit VLAN VID for this interface, for use by external
807  * components such as Infiniband.
808  *
809  * XXXRW: Note that the function name here is historical; it should be named
810  * vlan_vid().
811  */
812 static int
vlan_tag(struct ifnet * ifp,uint16_t * vidp)813 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
814 {
815 	struct ifvlan *ifv;
816 
817 	if (ifp->if_type != IFT_L2VLAN)
818 		return (EINVAL);
819 	ifv = ifp->if_softc;
820 	*vidp = ifv->ifv_vid;
821 	return (0);
822 }
823 
824 static int
vlan_pcp(struct ifnet * ifp,uint16_t * pcpp)825 vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
826 {
827 	struct ifvlan *ifv;
828 
829 	if (ifp->if_type != IFT_L2VLAN)
830 		return (EINVAL);
831 	ifv = ifp->if_softc;
832 	*pcpp = ifv->ifv_pcp;
833 	return (0);
834 }
835 
836 /*
837  * Return a driver specific cookie for this interface.  Synchronization
838  * with setcookie must be provided by the driver.
839  */
840 static void *
vlan_cookie(struct ifnet * ifp)841 vlan_cookie(struct ifnet *ifp)
842 {
843 	struct ifvlan *ifv;
844 
845 	if (ifp->if_type != IFT_L2VLAN)
846 		return (NULL);
847 	ifv = ifp->if_softc;
848 	return (ifv->ifv_cookie);
849 }
850 
851 /*
852  * Store a cookie in our softc that drivers can use to store driver
853  * private per-instance data in.
854  */
855 static int
vlan_setcookie(struct ifnet * ifp,void * cookie)856 vlan_setcookie(struct ifnet *ifp, void *cookie)
857 {
858 	struct ifvlan *ifv;
859 
860 	if (ifp->if_type != IFT_L2VLAN)
861 		return (EINVAL);
862 	ifv = ifp->if_softc;
863 	ifv->ifv_cookie = cookie;
864 	return (0);
865 }
866 
867 /*
868  * Return the vlan device present at the specific VID.
869  */
870 static struct ifnet *
vlan_devat(struct ifnet * ifp,uint16_t vid)871 vlan_devat(struct ifnet *ifp, uint16_t vid)
872 {
873 	struct ifvlantrunk *trunk;
874 	struct ifvlan *ifv;
875 
876 	NET_EPOCH_ASSERT();
877 
878 	trunk = ifp->if_vlantrunk;
879 	if (trunk == NULL)
880 		return (NULL);
881 	ifp = NULL;
882 	ifv = vlan_gethash(trunk, vid);
883 	if (ifv)
884 		ifp = ifv->ifv_ifp;
885 	return (ifp);
886 }
887 
888 /* For if_link_state_change() eyes only... */
889 extern	void (*vlan_link_state_p)(struct ifnet *);
890 
891 static struct if_clone_addreq_v2 vlan_addreq = {
892 	.version = 2,
893 	.match_f = vlan_clone_match,
894 	.create_f = vlan_clone_create,
895 	.destroy_f = vlan_clone_destroy,
896 	.create_nl_f = vlan_clone_create_nl,
897 	.modify_nl_f = vlan_clone_modify_nl,
898 	.dump_nl_f = vlan_clone_dump_nl,
899 };
900 
901 static int
vlan_modevent(module_t mod,int type,void * data)902 vlan_modevent(module_t mod, int type, void *data)
903 {
904 
905 	switch (type) {
906 	case MOD_LOAD:
907 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
908 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
909 		if (ifdetach_tag == NULL)
910 			return (ENOMEM);
911 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
912 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
913 		if (iflladdr_tag == NULL)
914 			return (ENOMEM);
915 		ifevent_tag = EVENTHANDLER_REGISTER(ifnet_event,
916 		    vlan_ifevent, NULL, EVENTHANDLER_PRI_ANY);
917 		if (ifevent_tag == NULL)
918 			return (ENOMEM);
919 		VLAN_LOCKING_INIT();
920 		vlan_input_p = vlan_input;
921 		vlan_link_state_p = vlan_link_state;
922 		vlan_trunk_cap_p = vlan_trunk_capabilities;
923 		vlan_trunkdev_p = vlan_trunkdev;
924 		vlan_cookie_p = vlan_cookie;
925 		vlan_setcookie_p = vlan_setcookie;
926 		vlan_tag_p = vlan_tag;
927 		vlan_pcp_p = vlan_pcp;
928 		vlan_devat_p = vlan_devat;
929 #ifndef VIMAGE
930 		vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq);
931 #endif
932 		if (bootverbose)
933 			printf("vlan: initialized, using "
934 #ifdef VLAN_ARRAY
935 			       "full-size arrays"
936 #else
937 			       "hash tables with chaining"
938 #endif
939 
940 			       "\n");
941 		break;
942 	case MOD_UNLOAD:
943 #ifndef VIMAGE
944 		ifc_detach_cloner(vlan_cloner);
945 #endif
946 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
947 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
948 		EVENTHANDLER_DEREGISTER(ifnet_event, ifevent_tag);
949 		vlan_input_p = NULL;
950 		vlan_link_state_p = NULL;
951 		vlan_trunk_cap_p = NULL;
952 		vlan_trunkdev_p = NULL;
953 		vlan_tag_p = NULL;
954 		vlan_cookie_p = NULL;
955 		vlan_setcookie_p = NULL;
956 		vlan_devat_p = NULL;
957 		VLAN_LOCKING_DESTROY();
958 		if (bootverbose)
959 			printf("vlan: unloaded\n");
960 		break;
961 	default:
962 		return (EOPNOTSUPP);
963 	}
964 	return (0);
965 }
966 
967 static moduledata_t vlan_mod = {
968 	"if_vlan",
969 	vlan_modevent,
970 	0
971 };
972 
973 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
974 MODULE_VERSION(if_vlan, 3);
975 
976 #ifdef VIMAGE
977 static void
vnet_vlan_init(const void * unused __unused)978 vnet_vlan_init(const void *unused __unused)
979 {
980 	vlan_cloner = ifc_attach_cloner(vlanname, (struct if_clone_addreq *)&vlan_addreq);
981 	V_vlan_cloner = vlan_cloner;
982 }
983 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
984     vnet_vlan_init, NULL);
985 
986 static void
vnet_vlan_uninit(const void * unused __unused)987 vnet_vlan_uninit(const void *unused __unused)
988 {
989 
990 	ifc_detach_cloner(V_vlan_cloner);
991 }
992 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
993     vnet_vlan_uninit, NULL);
994 #endif
995 
996 /*
997  * Check for <etherif>.<vlan>[.<vlan> ...] style interface names.
998  */
999 static struct ifnet *
vlan_clone_match_ethervid(const char * name,int * vidp)1000 vlan_clone_match_ethervid(const char *name, int *vidp)
1001 {
1002 	char ifname[IFNAMSIZ];
1003 	char *cp;
1004 	struct ifnet *ifp;
1005 	int vid;
1006 
1007 	strlcpy(ifname, name, IFNAMSIZ);
1008 	if ((cp = strrchr(ifname, '.')) == NULL)
1009 		return (NULL);
1010 	*cp = '\0';
1011 	if ((ifp = ifunit_ref(ifname)) == NULL)
1012 		return (NULL);
1013 	/* Parse VID. */
1014 	if (*++cp == '\0') {
1015 		if_rele(ifp);
1016 		return (NULL);
1017 	}
1018 	vid = 0;
1019 	for(; *cp >= '0' && *cp <= '9'; cp++)
1020 		vid = (vid * 10) + (*cp - '0');
1021 	if (*cp != '\0') {
1022 		if_rele(ifp);
1023 		return (NULL);
1024 	}
1025 	if (vidp != NULL)
1026 		*vidp = vid;
1027 
1028 	return (ifp);
1029 }
1030 
1031 static int
vlan_clone_match(struct if_clone * ifc,const char * name)1032 vlan_clone_match(struct if_clone *ifc, const char *name)
1033 {
1034 	struct ifnet *ifp;
1035 	const char *cp;
1036 
1037 	ifp = vlan_clone_match_ethervid(name, NULL);
1038 	if (ifp != NULL) {
1039 		if_rele(ifp);
1040 		return (1);
1041 	}
1042 
1043 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
1044 		return (0);
1045 	for (cp = name + 4; *cp != '\0'; cp++) {
1046 		if (*cp < '0' || *cp > '9')
1047 			return (0);
1048 	}
1049 
1050 	return (1);
1051 }
1052 
1053 static int
vlan_clone_create(struct if_clone * ifc,char * name,size_t len,struct ifc_data * ifd,struct ifnet ** ifpp)1054 vlan_clone_create(struct if_clone *ifc, char *name, size_t len,
1055     struct ifc_data *ifd, struct ifnet **ifpp)
1056 {
1057 	char *dp;
1058 	bool wildcard = false;
1059 	bool subinterface = false;
1060 	int unit;
1061 	int error;
1062 	int vid = 0;
1063 	uint16_t proto = ETHERTYPE_VLAN;
1064 	struct ifvlan *ifv;
1065 	struct ifnet *ifp;
1066 	struct ifnet *p = NULL;
1067 	struct ifaddr *ifa;
1068 	struct sockaddr_dl *sdl;
1069 	struct vlanreq vlr;
1070 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
1071 
1072 
1073 	/*
1074 	 * There are three ways to specify the cloned device:
1075 	 * o pass a parameter block with the clone request.
1076 	 * o specify parameters in the text of the clone device name
1077 	 * o specify no parameters and get an unattached device that
1078 	 *   must be configured separately.
1079 	 * The first technique is preferred; the latter two are supported
1080 	 * for backwards compatibility.
1081 	 *
1082 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
1083 	 * called for.
1084 	 */
1085 
1086 	if (ifd->params != NULL) {
1087 		error = ifc_copyin(ifd, &vlr, sizeof(vlr));
1088 		if (error)
1089 			return error;
1090 		vid = vlr.vlr_tag;
1091 		proto = vlr.vlr_proto;
1092 		if (proto == 0)
1093 			proto = ETHERTYPE_VLAN;
1094 		p = ifunit_ref(vlr.vlr_parent);
1095 		if (p == NULL)
1096 			return (ENXIO);
1097 	}
1098 
1099 	if ((error = ifc_name2unit(name, &unit)) == 0) {
1100 
1101 		/*
1102 		 * vlanX interface. Set wildcard to true if the unit number
1103 		 * is not fixed (-1)
1104 		 */
1105 		wildcard = (unit < 0);
1106 	} else {
1107 		struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid);
1108 		if (p_tmp != NULL) {
1109 			error = 0;
1110 			subinterface = true;
1111 			unit = IF_DUNIT_NONE;
1112 			wildcard = false;
1113 			if (p != NULL) {
1114 				if_rele(p_tmp);
1115 				if (p != p_tmp)
1116 					error = EINVAL;
1117 			} else
1118 				p = p_tmp;
1119 		} else
1120 			error = ENXIO;
1121 	}
1122 
1123 	if (error != 0) {
1124 		if (p != NULL)
1125 			if_rele(p);
1126 		return (error);
1127 	}
1128 
1129 	if (!subinterface) {
1130 		/* vlanX interface, mark X as busy or allocate new unit # */
1131 		error = ifc_alloc_unit(ifc, &unit);
1132 		if (error != 0) {
1133 			if (p != NULL)
1134 				if_rele(p);
1135 			return (error);
1136 		}
1137 	}
1138 
1139 	/* In the wildcard case, we need to update the name. */
1140 	if (wildcard) {
1141 		for (dp = name; *dp != '\0'; dp++);
1142 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1143 		    len - (dp-name) - 1) {
1144 			panic("%s: interface name too long", __func__);
1145 		}
1146 	}
1147 
1148 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1149 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1150 	CK_SLIST_INIT(&ifv->vlan_mc_listhead);
1151 	ifp->if_softc = ifv;
1152 	/*
1153 	 * Set the name manually rather than using if_initname because
1154 	 * we don't conform to the default naming convention for interfaces.
1155 	 */
1156 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
1157 	ifp->if_dname = vlanname;
1158 	ifp->if_dunit = unit;
1159 
1160 	ifp->if_init = vlan_init;
1161 #ifdef ALTQ
1162 	ifp->if_start = vlan_altq_start;
1163 	ifp->if_transmit = vlan_altq_transmit;
1164 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1165 	ifp->if_snd.ifq_drv_maxlen = 0;
1166 	IFQ_SET_READY(&ifp->if_snd);
1167 #else
1168 	ifp->if_transmit = vlan_transmit;
1169 #endif
1170 	ifp->if_qflush = vlan_qflush;
1171 	ifp->if_ioctl = vlan_ioctl;
1172 #if defined(KERN_TLS) || defined(RATELIMIT)
1173 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1174 	ifp->if_ratelimit_query = vlan_ratelimit_query;
1175 #endif
1176 	ifp->if_flags = VLAN_IFFLAGS;
1177 	ifp->if_type = IFT_L2VLAN;
1178 	ether_ifattach(ifp, eaddr);
1179 	/* Now undo some of the damage... */
1180 	ifp->if_baudrate = 0;
1181 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1182 	ifa = ifp->if_addr;
1183 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1184 	sdl->sdl_type = IFT_L2VLAN;
1185 
1186 	if (p != NULL) {
1187 		error = vlan_config(ifv, p, vid, proto);
1188 		if_rele(p);
1189 		if (error != 0) {
1190 			/*
1191 			 * Since we've partially failed, we need to back
1192 			 * out all the way, otherwise userland could get
1193 			 * confused.  Thus, we destroy the interface.
1194 			 */
1195 			ether_ifdetach(ifp);
1196 			vlan_unconfig(ifp);
1197 			if_free(ifp);
1198 			if (!subinterface)
1199 				ifc_free_unit(ifc, unit);
1200 			free(ifv, M_VLAN);
1201 
1202 			return (error);
1203 		}
1204 	}
1205 	*ifpp = ifp;
1206 
1207 	return (0);
1208 }
1209 
1210 /*
1211  *
1212  * Parsers of IFLA_INFO_DATA inside IFLA_LINKINFO of RTM_NEWLINK
1213  *    {{nla_len=8, nla_type=IFLA_LINK}, 2},
1214  *    {{nla_len=12, nla_type=IFLA_IFNAME}, "xvlan22"},
1215  *    {{nla_len=24, nla_type=IFLA_LINKINFO},
1216  *     [
1217  *      {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
1218  *      {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]}
1219  */
1220 
1221 struct nl_parsed_vlan {
1222 	uint16_t vlan_id;
1223 	uint16_t vlan_proto;
1224 	struct ifla_vlan_flags vlan_flags;
1225 };
1226 
1227 #define	_OUT(_field)	offsetof(struct nl_parsed_vlan, _field)
1228 static const struct nlattr_parser nla_p_vlan[] = {
1229 	{ .type = IFLA_VLAN_ID, .off = _OUT(vlan_id), .cb = nlattr_get_uint16 },
1230 	{ .type = IFLA_VLAN_FLAGS, .off = _OUT(vlan_flags), .cb = nlattr_get_nla },
1231 	{ .type = IFLA_VLAN_PROTOCOL, .off = _OUT(vlan_proto), .cb = nlattr_get_uint16 },
1232 };
1233 #undef _OUT
1234 NL_DECLARE_ATTR_PARSER(vlan_parser, nla_p_vlan);
1235 
1236 static int
vlan_clone_create_nl(struct if_clone * ifc,char * name,size_t len,struct ifc_data_nl * ifd)1237 vlan_clone_create_nl(struct if_clone *ifc, char *name, size_t len,
1238     struct ifc_data_nl *ifd)
1239 {
1240 	struct epoch_tracker et;
1241         struct ifnet *ifp_parent;
1242 	struct nl_pstate *npt = ifd->npt;
1243 	struct nl_parsed_link *lattrs = ifd->lattrs;
1244 	int error;
1245 
1246 	/*
1247 	 * lattrs.ifla_ifname is the new interface name
1248 	 * lattrs.ifi_index contains parent interface index
1249 	 * lattrs.ifla_idata contains un-parsed vlan data
1250 	 */
1251 	struct nl_parsed_vlan attrs = {
1252 		.vlan_id = 0xFEFE,
1253 		.vlan_proto = ETHERTYPE_VLAN
1254 	};
1255 
1256 	if (lattrs->ifla_idata == NULL) {
1257 		nlmsg_report_err_msg(npt, "vlan id is required, guessing not supported");
1258 		return (ENOTSUP);
1259 	}
1260 
1261 	error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, npt, &attrs);
1262 	if (error != 0)
1263 		return (error);
1264 	if (attrs.vlan_id > DOT1Q_VID_MAX) {
1265 		nlmsg_report_err_msg(npt, "Invalid VID: %d", attrs.vlan_id);
1266 		return (EINVAL);
1267 	}
1268 	if (attrs.vlan_proto != ETHERTYPE_VLAN && attrs.vlan_proto != ETHERTYPE_QINQ) {
1269 		nlmsg_report_err_msg(npt, "Unsupported ethertype: 0x%04X", attrs.vlan_proto);
1270 		return (ENOTSUP);
1271 	}
1272 
1273 	struct vlanreq params = {
1274 		.vlr_tag = attrs.vlan_id,
1275 		.vlr_proto = attrs.vlan_proto,
1276 	};
1277 	struct ifc_data ifd_new = { .flags = IFC_F_SYSSPACE, .unit = ifd->unit, .params = &params };
1278 
1279 	NET_EPOCH_ENTER(et);
1280 	ifp_parent = ifnet_byindex(lattrs->ifi_index);
1281 	if (ifp_parent != NULL)
1282 		strlcpy(params.vlr_parent, if_name(ifp_parent), sizeof(params.vlr_parent));
1283 	NET_EPOCH_EXIT(et);
1284 
1285 	if (ifp_parent == NULL) {
1286 		nlmsg_report_err_msg(npt, "unable to find parent interface %u", lattrs->ifi_index);
1287 		return (ENOENT);
1288 	}
1289 
1290 	error = vlan_clone_create(ifc, name, len, &ifd_new, &ifd->ifp);
1291 
1292 	return (error);
1293 }
1294 
1295 static int
vlan_clone_modify_nl(struct ifnet * ifp,struct ifc_data_nl * ifd)1296 vlan_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd)
1297 {
1298 	struct nl_parsed_link *lattrs = ifd->lattrs;
1299 
1300 	if ((lattrs->ifla_idata != NULL) && ((ifd->flags & IFC_F_CREATE) == 0)) {
1301 		struct epoch_tracker et;
1302 		struct nl_parsed_vlan attrs = {
1303 			.vlan_proto = ETHERTYPE_VLAN,
1304 		};
1305 		int error;
1306 
1307 		error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, ifd->npt, &attrs);
1308 		if (error != 0)
1309 			return (error);
1310 
1311 		NET_EPOCH_ENTER(et);
1312 		struct ifnet *ifp_parent = ifnet_byindex_ref(lattrs->ifla_link);
1313 		NET_EPOCH_EXIT(et);
1314 
1315 		if (ifp_parent == NULL) {
1316 			nlmsg_report_err_msg(ifd->npt, "unable to find parent interface %u",
1317 			    lattrs->ifla_link);
1318 			return (ENOENT);
1319 		}
1320 
1321 		struct ifvlan *ifv = ifp->if_softc;
1322 		error = vlan_config(ifv, ifp_parent, attrs.vlan_id, attrs.vlan_proto);
1323 
1324 		if_rele(ifp_parent);
1325 		if (error != 0)
1326 			return (error);
1327 	}
1328 
1329 	return (nl_modify_ifp_generic(ifp, ifd->lattrs, ifd->bm, ifd->npt));
1330 }
1331 
1332 /*
1333  *    {{nla_len=24, nla_type=IFLA_LINKINFO},
1334  *     [
1335  *      {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
1336  *      {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]}
1337  */
1338 static void
vlan_clone_dump_nl(struct ifnet * ifp,struct nl_writer * nw)1339 vlan_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw)
1340 {
1341 	struct ifvlan *ifv;
1342 	uint32_t parent_index = 0;
1343 	uint16_t vlan_id = 0;
1344 	uint16_t vlan_proto = 0;
1345 
1346 	VLAN_SLOCK();
1347 	if (__predict_false((ifv = ifp->if_softc) == NULL)) {
1348 		VLAN_SUNLOCK();
1349 		/*
1350 		 * XXXGL: the interface already went through if_dead().  This
1351 		 * check to be removed when we got better interface removal.
1352 		 */
1353 		return;
1354 	}
1355 	if (TRUNK(ifv) != NULL)
1356 		parent_index = PARENT(ifv)->if_index;
1357 	vlan_id = ifv->ifv_vid;
1358 	vlan_proto = ifv->ifv_proto;
1359 	VLAN_SUNLOCK();
1360 
1361 	if (parent_index != 0)
1362 		nlattr_add_u32(nw, IFLA_LINK, parent_index);
1363 
1364 	int off = nlattr_add_nested(nw, IFLA_LINKINFO);
1365 	if (off != 0) {
1366 		nlattr_add_string(nw, IFLA_INFO_KIND, "vlan");
1367 		int off2 = nlattr_add_nested(nw, IFLA_INFO_DATA);
1368 		if (off2 != 0) {
1369 			nlattr_add_u16(nw, IFLA_VLAN_ID, vlan_id);
1370 			nlattr_add_u16(nw, IFLA_VLAN_PROTOCOL, vlan_proto);
1371 			nlattr_set_len(nw, off2);
1372 		}
1373 		nlattr_set_len(nw, off);
1374 	}
1375 }
1376 
1377 static int
vlan_clone_destroy(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)1378 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
1379 {
1380 	struct ifvlan *ifv = ifp->if_softc;
1381 	int unit = ifp->if_dunit;
1382 
1383 	if (ifp->if_vlantrunk)
1384 		return (EBUSY);
1385 
1386 #ifdef ALTQ
1387 	IFQ_PURGE(&ifp->if_snd);
1388 #endif
1389 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1390 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1391 	/*
1392 	 * We should have the only reference to the ifv now, so we can now
1393 	 * drain any remaining lladdr task before freeing the ifnet and the
1394 	 * ifvlan.
1395 	 */
1396 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1397 	NET_EPOCH_WAIT();
1398 	ifp->if_softc = NULL;
1399 	if_free(ifp);
1400 	free(ifv, M_VLAN);
1401 	if (unit != IF_DUNIT_NONE)
1402 		ifc_free_unit(ifc, unit);
1403 
1404 	return (0);
1405 }
1406 
1407 /*
1408  * The ifp->if_init entry point for vlan(4) is a no-op.
1409  */
1410 static void
vlan_init(void * foo __unused)1411 vlan_init(void *foo __unused)
1412 {
1413 }
1414 
1415 /*
1416  * The if_transmit method for vlan(4) interface.
1417  */
1418 static int
vlan_transmit(struct ifnet * ifp,struct mbuf * m)1419 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1420 {
1421 	struct ifvlan *ifv;
1422 	struct ifnet *p;
1423 	int error, len, mcast;
1424 
1425 	NET_EPOCH_ASSERT();
1426 
1427 	ifv = ifp->if_softc;
1428 	if (TRUNK(ifv) == NULL) {
1429 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1430 		m_freem(m);
1431 		return (ENETDOWN);
1432 	}
1433 	p = PARENT(ifv);
1434 	len = m->m_pkthdr.len;
1435 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1436 
1437 	BPF_MTAP(ifp, m);
1438 
1439 #if defined(KERN_TLS) || defined(RATELIMIT)
1440 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1441 		struct vlan_snd_tag *vst;
1442 		struct m_snd_tag *mst;
1443 
1444 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1445 		mst = m->m_pkthdr.snd_tag;
1446 		vst = mst_to_vst(mst);
1447 		if (vst->tag->ifp != p) {
1448 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1449 			m_freem(m);
1450 			return (EAGAIN);
1451 		}
1452 
1453 		m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1454 		m_snd_tag_rele(mst);
1455 	}
1456 #endif
1457 
1458 	/*
1459 	 * Do not run parent's if_transmit() if the parent is not up,
1460 	 * or parent's driver will cause a system crash.
1461 	 */
1462 	if (!UP_AND_RUNNING(p)) {
1463 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1464 		m_freem(m);
1465 		return (ENETDOWN);
1466 	}
1467 
1468 	if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) {
1469 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1470 		return (0);
1471 	}
1472 
1473 	/*
1474 	 * Send it, precisely as ether_output() would have.
1475 	 */
1476 	error = (p->if_transmit)(p, m);
1477 	if (error == 0) {
1478 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1479 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1480 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1481 	} else
1482 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1483 	return (error);
1484 }
1485 
1486 static int
vlan_output(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * dst,struct route * ro)1487 vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1488     struct route *ro)
1489 {
1490 	struct ifvlan *ifv;
1491 	struct ifnet *p;
1492 
1493 	NET_EPOCH_ASSERT();
1494 
1495 	/*
1496 	 * Find the first non-VLAN parent interface.
1497 	 */
1498 	ifv = ifp->if_softc;
1499 	do {
1500 		if (TRUNK(ifv) == NULL) {
1501 			m_freem(m);
1502 			return (ENETDOWN);
1503 		}
1504 		p = PARENT(ifv);
1505 		ifv = p->if_softc;
1506 	} while (p->if_type == IFT_L2VLAN);
1507 
1508 	return p->if_output(ifp, m, dst, ro);
1509 }
1510 
1511 #ifdef ALTQ
1512 static void
vlan_altq_start(if_t ifp)1513 vlan_altq_start(if_t ifp)
1514 {
1515 	struct ifaltq *ifq = &ifp->if_snd;
1516 	struct mbuf *m;
1517 
1518 	IFQ_LOCK(ifq);
1519 	IFQ_DEQUEUE_NOLOCK(ifq, m);
1520 	while (m != NULL) {
1521 		vlan_transmit(ifp, m);
1522 		IFQ_DEQUEUE_NOLOCK(ifq, m);
1523 	}
1524 	IFQ_UNLOCK(ifq);
1525 }
1526 
1527 static int
vlan_altq_transmit(if_t ifp,struct mbuf * m)1528 vlan_altq_transmit(if_t ifp, struct mbuf *m)
1529 {
1530 	int err;
1531 
1532 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
1533 		IFQ_ENQUEUE(&ifp->if_snd, m, err);
1534 		if (err == 0)
1535 			vlan_altq_start(ifp);
1536 	} else
1537 		err = vlan_transmit(ifp, m);
1538 
1539 	return (err);
1540 }
1541 #endif	/* ALTQ */
1542 
1543 /*
1544  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1545  */
1546 static void
vlan_qflush(struct ifnet * ifp __unused)1547 vlan_qflush(struct ifnet *ifp __unused)
1548 {
1549 }
1550 
1551 static void
vlan_input(struct ifnet * ifp,struct mbuf * m)1552 vlan_input(struct ifnet *ifp, struct mbuf *m)
1553 {
1554 	struct ifvlantrunk *trunk;
1555 	struct ifvlan *ifv;
1556 	struct m_tag *mtag;
1557 	uint16_t vid, tag;
1558 
1559 	NET_EPOCH_ASSERT();
1560 
1561 	trunk = ifp->if_vlantrunk;
1562 	if (trunk == NULL) {
1563 		m_freem(m);
1564 		return;
1565 	}
1566 
1567 	if (m->m_flags & M_VLANTAG) {
1568 		/*
1569 		 * Packet is tagged, but m contains a normal
1570 		 * Ethernet frame; the tag is stored out-of-band.
1571 		 */
1572 		tag = m->m_pkthdr.ether_vtag;
1573 		m->m_flags &= ~M_VLANTAG;
1574 	} else {
1575 		struct ether_vlan_header *evl;
1576 
1577 		/*
1578 		 * Packet is tagged in-band as specified by 802.1q.
1579 		 */
1580 		switch (ifp->if_type) {
1581 		case IFT_ETHER:
1582 			if (m->m_len < sizeof(*evl) &&
1583 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1584 				if_printf(ifp, "cannot pullup VLAN header\n");
1585 				return;
1586 			}
1587 			evl = mtod(m, struct ether_vlan_header *);
1588 			tag = ntohs(evl->evl_tag);
1589 
1590 			/*
1591 			 * Remove the 802.1q header by copying the Ethernet
1592 			 * addresses over it and adjusting the beginning of
1593 			 * the data in the mbuf.  The encapsulated Ethernet
1594 			 * type field is already in place.
1595 			 */
1596 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1597 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
1598 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1599 			break;
1600 
1601 		default:
1602 #ifdef INVARIANTS
1603 			panic("%s: %s has unsupported if_type %u",
1604 			      __func__, ifp->if_xname, ifp->if_type);
1605 #endif
1606 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1607 			m_freem(m);
1608 			return;
1609 		}
1610 	}
1611 
1612 	vid = EVL_VLANOFTAG(tag);
1613 
1614 	ifv = vlan_gethash(trunk, vid);
1615 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1616 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1617 		m_freem(m);
1618 		return;
1619 	}
1620 
1621 	if (V_vlan_mtag_pcp) {
1622 		/*
1623 		 * While uncommon, it is possible that we will find a 802.1q
1624 		 * packet encapsulated inside another packet that also had an
1625 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
1626 		 * arriving over ethernet.  In that case, we replace the
1627 		 * existing 802.1q PCP m_tag value.
1628 		 */
1629 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1630 		if (mtag == NULL) {
1631 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1632 			    sizeof(uint8_t), M_NOWAIT);
1633 			if (mtag == NULL) {
1634 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1635 				m_freem(m);
1636 				return;
1637 			}
1638 			m_tag_prepend(m, mtag);
1639 		}
1640 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1641 	}
1642 
1643 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1644 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1645 
1646 	/* Pass it back through the parent's input routine. */
1647 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1648 }
1649 
1650 static void
vlan_lladdr_fn(void * arg,int pending __unused)1651 vlan_lladdr_fn(void *arg, int pending __unused)
1652 {
1653 	struct ifvlan *ifv;
1654 	struct ifnet *ifp;
1655 
1656 	ifv = (struct ifvlan *)arg;
1657 	ifp = ifv->ifv_ifp;
1658 
1659 	CURVNET_SET(ifp->if_vnet);
1660 
1661 	/* The ifv_ifp already has the lladdr copied in. */
1662 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1663 
1664 	CURVNET_RESTORE();
1665 }
1666 
1667 static int
vlan_config(struct ifvlan * ifv,struct ifnet * p,uint16_t vid,uint16_t proto)1668 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid,
1669 	uint16_t proto)
1670 {
1671 	struct epoch_tracker et;
1672 	struct ifvlantrunk *trunk;
1673 	struct ifnet *ifp;
1674 	int error = 0;
1675 
1676 	/*
1677 	 * We can handle non-ethernet hardware types as long as
1678 	 * they handle the tagging and headers themselves.
1679 	 */
1680 	if (p->if_type != IFT_ETHER &&
1681 	    p->if_type != IFT_L2VLAN &&
1682 	    p->if_type != IFT_BRIDGE &&
1683 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1684 		return (EPROTONOSUPPORT);
1685 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1686 		return (EPROTONOSUPPORT);
1687 	/*
1688 	 * Don't let the caller set up a VLAN VID with
1689 	 * anything except VLID bits.
1690 	 * VID numbers 0x0 and 0xFFF are reserved.
1691 	 */
1692 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1693 		return (EINVAL);
1694 	if (ifv->ifv_trunk) {
1695 		trunk = ifv->ifv_trunk;
1696 		if (trunk->parent != p)
1697 			return (EBUSY);
1698 
1699 		VLAN_XLOCK();
1700 
1701 		ifv->ifv_proto = proto;
1702 
1703 		if (ifv->ifv_vid != vid) {
1704 			int oldvid = ifv->ifv_vid;
1705 
1706 			/* Re-hash */
1707 			vlan_remhash(trunk, ifv);
1708 			ifv->ifv_vid = vid;
1709 			error = vlan_inshash(trunk, ifv);
1710 			if (error) {
1711 				int ret __diagused;
1712 
1713 				ifv->ifv_vid = oldvid;
1714 				/* Re-insert back where we found it. */
1715 				ret = vlan_inshash(trunk, ifv);
1716 				MPASS(ret == 0);
1717 			}
1718 		}
1719 		/* Will unlock */
1720 		goto done;
1721 	}
1722 
1723 	VLAN_XLOCK();
1724 	if (p->if_vlantrunk == NULL) {
1725 		trunk = malloc(sizeof(struct ifvlantrunk),
1726 		    M_VLAN, M_WAITOK | M_ZERO);
1727 		vlan_inithash(trunk);
1728 		TRUNK_LOCK_INIT(trunk);
1729 		TRUNK_WLOCK(trunk);
1730 		p->if_vlantrunk = trunk;
1731 		trunk->parent = p;
1732 		if_ref(trunk->parent);
1733 		TRUNK_WUNLOCK(trunk);
1734 	} else {
1735 		trunk = p->if_vlantrunk;
1736 	}
1737 
1738 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
1739 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
1740 	error = vlan_inshash(trunk, ifv);
1741 	if (error)
1742 		goto done;
1743 	ifv->ifv_proto = proto;
1744 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1745 	ifv->ifv_mintu = ETHERMIN;
1746 	ifv->ifv_pflags = 0;
1747 	ifv->ifv_capenable = -1;
1748 	ifv->ifv_capenable2 = -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 #ifdef IPSEC_OFFLOAD
2008 #define	VLAN_IPSEC_METHOD(exp)				\
2009 	if_t p;						\
2010 	struct ifvlan *ifv;				\
2011 	int error;					\
2012 							\
2013 	ifv = ifp->if_softc;				\
2014 	VLAN_SLOCK();					\
2015 	if (TRUNK(ifv) != NULL) {			\
2016 		p = PARENT(ifv);			\
2017 		if_ref(p);				\
2018 		error = p->if_ipsec_accel_m->exp;	\
2019 		if_rele(p);				\
2020 	} else {					\
2021 		error = ENXIO;				\
2022 	}						\
2023 	VLAN_SUNLOCK();					\
2024 	return (error);
2025 
2026 
2027 static int
vlan_if_spdadd(if_t ifp,void * sp,void * inp,void ** priv)2028 vlan_if_spdadd(if_t ifp, void *sp, void *inp, void **priv)
2029 {
2030 	VLAN_IPSEC_METHOD(if_spdadd(ifp, sp, inp, priv));
2031 }
2032 
2033 static int
vlan_if_spddel(if_t ifp,void * sp,void * priv)2034 vlan_if_spddel(if_t ifp, void *sp, void *priv)
2035 {
2036 	VLAN_IPSEC_METHOD(if_spddel(ifp, sp, priv));
2037 }
2038 
2039 static int
vlan_if_sa_newkey(if_t ifp,void * sav,u_int drv_spi,void ** privp)2040 vlan_if_sa_newkey(if_t ifp, void *sav, u_int drv_spi, void **privp)
2041 {
2042 	VLAN_IPSEC_METHOD(if_sa_newkey(ifp, sav, drv_spi, privp));
2043 }
2044 
2045 static int
vlan_if_sa_deinstall(if_t ifp,u_int drv_spi,void * priv)2046 vlan_if_sa_deinstall(if_t ifp, u_int drv_spi, void *priv)
2047 {
2048 	VLAN_IPSEC_METHOD(if_sa_deinstall(ifp, drv_spi, priv));
2049 }
2050 
2051 static int
vlan_if_sa_cnt(if_t ifp,void * sa,uint32_t drv_spi,void * priv,struct seclifetime * lt)2052 vlan_if_sa_cnt(if_t ifp, void *sa, uint32_t drv_spi, void *priv,
2053     struct seclifetime *lt)
2054 {
2055 	VLAN_IPSEC_METHOD(if_sa_cnt(ifp, sa, drv_spi, priv, lt));
2056 }
2057 
2058 static int
vlan_if_ipsec_hwassist(if_t ifp,void * sav,u_int drv_spi,void * priv)2059 vlan_if_ipsec_hwassist(if_t ifp, void *sav, u_int drv_spi,void *priv)
2060 {
2061 	if_t trunk;
2062 
2063 	NET_EPOCH_ASSERT();
2064 	trunk = vlan_trunkdev(ifp);
2065 	if (trunk == NULL)
2066 		return (0);
2067 	return (trunk->if_ipsec_accel_m->if_hwassist(trunk, sav,
2068 	    drv_spi, priv));
2069 }
2070 
2071 static const struct if_ipsec_accel_methods vlan_if_ipsec_accel_methods = {
2072 	.if_spdadd = vlan_if_spdadd,
2073 	.if_spddel = vlan_if_spddel,
2074 	.if_sa_newkey = vlan_if_sa_newkey,
2075 	.if_sa_deinstall = vlan_if_sa_deinstall,
2076 	.if_sa_cnt = vlan_if_sa_cnt,
2077 	.if_hwassist = vlan_if_ipsec_hwassist,
2078 };
2079 
2080 #undef VLAN_IPSEC_METHOD
2081 #endif	/* IPSEC_OFFLOAD */
2082 
2083 static void
vlan_capabilities(struct ifvlan * ifv)2084 vlan_capabilities(struct ifvlan *ifv)
2085 {
2086 	struct ifnet *p;
2087 	struct ifnet *ifp;
2088 	struct ifnet_hw_tsomax hw_tsomax;
2089 	int cap = 0, ena = 0, mena, cap2 = 0, ena2 = 0;
2090 	int mena2 __unused;
2091 	u_long hwa = 0;
2092 
2093 	NET_EPOCH_ASSERT();
2094 	VLAN_SXLOCK_ASSERT();
2095 
2096 	p = PARENT(ifv);
2097 	ifp = ifv->ifv_ifp;
2098 
2099 	/* Mask parent interface enabled capabilities disabled by user. */
2100 	mena = p->if_capenable & ifv->ifv_capenable;
2101 	mena2 = p->if_capenable2 & ifv->ifv_capenable2;
2102 
2103 	/*
2104 	 * If the parent interface can do checksum offloading
2105 	 * on VLANs, then propagate its hardware-assisted
2106 	 * checksumming flags. Also assert that checksum
2107 	 * offloading requires hardware VLAN tagging.
2108 	 */
2109 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
2110 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
2111 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
2112 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
2113 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
2114 		if (ena & IFCAP_TXCSUM)
2115 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
2116 			    CSUM_UDP | CSUM_SCTP);
2117 		if (ena & IFCAP_TXCSUM_IPV6)
2118 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
2119 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
2120 	}
2121 
2122 	/*
2123 	 * If the parent interface can do TSO on VLANs then
2124 	 * propagate the hardware-assisted flag. TSO on VLANs
2125 	 * does not necessarily require hardware VLAN tagging.
2126 	 */
2127 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
2128 	if_hw_tsomax_common(p, &hw_tsomax);
2129 	if_hw_tsomax_update(ifp, &hw_tsomax);
2130 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
2131 		cap |= p->if_capabilities & IFCAP_TSO;
2132 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
2133 		ena |= mena & IFCAP_TSO;
2134 		if (ena & IFCAP_TSO)
2135 			hwa |= p->if_hwassist & CSUM_TSO;
2136 	}
2137 
2138 	/*
2139 	 * If the parent interface can do LRO and checksum offloading on
2140 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
2141 	 * cost nothing, while false negative may lead to some confusions.
2142 	 */
2143 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
2144 		cap |= p->if_capabilities & IFCAP_LRO;
2145 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
2146 		ena |= mena & IFCAP_LRO;
2147 
2148 	/*
2149 	 * If the parent interface can offload TCP connections over VLANs then
2150 	 * propagate its TOE capability to the VLAN interface.
2151 	 *
2152 	 * All TOE drivers in the tree today can deal with VLANs.  If this
2153 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
2154 	 * with its own bit.
2155 	 */
2156 #define	IFCAP_VLAN_TOE IFCAP_TOE
2157 	if (p->if_capabilities & IFCAP_VLAN_TOE)
2158 		cap |= p->if_capabilities & IFCAP_TOE;
2159 	if (p->if_capenable & IFCAP_VLAN_TOE) {
2160 		SETTOEDEV(ifp, TOEDEV(p));
2161 		ena |= mena & IFCAP_TOE;
2162 	}
2163 
2164 	/*
2165 	 * If the parent interface supports dynamic link state, so does the
2166 	 * VLAN interface.
2167 	 */
2168 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
2169 	ena |= (mena & IFCAP_LINKSTATE);
2170 
2171 #ifdef RATELIMIT
2172 	/*
2173 	 * If the parent interface supports ratelimiting, so does the
2174 	 * VLAN interface.
2175 	 */
2176 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
2177 	ena |= (mena & IFCAP_TXRTLMT);
2178 #endif
2179 
2180 	/*
2181 	 * If the parent interface supports unmapped mbufs, so does
2182 	 * the VLAN interface.  Note that this should be fine even for
2183 	 * interfaces that don't support hardware tagging as headers
2184 	 * are prepended in normal mbufs to unmapped mbufs holding
2185 	 * payload data.
2186 	 */
2187 	cap |= (p->if_capabilities & IFCAP_MEXTPG);
2188 	ena |= (mena & IFCAP_MEXTPG);
2189 
2190 	/*
2191 	 * If the parent interface can offload encryption and segmentation
2192 	 * of TLS records over TCP, propagate it's capability to the VLAN
2193 	 * interface.
2194 	 *
2195 	 * All TLS drivers in the tree today can deal with VLANs.  If
2196 	 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
2197 	 * defined.
2198 	 */
2199 	if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
2200 		cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
2201 	if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
2202 		ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
2203 
2204 	ifp->if_capabilities = cap;
2205 	ifp->if_capenable = ena;
2206 	ifp->if_hwassist = hwa;
2207 
2208 #ifdef IPSEC_OFFLOAD
2209 	cap2 |= p->if_capabilities2 & IFCAP2_BIT(IFCAP2_IPSEC_OFFLOAD);
2210 	ena2 |= mena2 & IFCAP2_BIT(IFCAP2_IPSEC_OFFLOAD);
2211 	ifp->if_ipsec_accel_m = &vlan_if_ipsec_accel_methods;
2212 #endif
2213 
2214 	ifp->if_capabilities2 = cap2;
2215 	ifp->if_capenable2 = ena2;
2216 }
2217 
2218 static void
vlan_trunk_capabilities(struct ifnet * ifp)2219 vlan_trunk_capabilities(struct ifnet *ifp)
2220 {
2221 	struct epoch_tracker et;
2222 	struct ifvlantrunk *trunk;
2223 	struct ifvlan *ifv;
2224 
2225 	VLAN_SLOCK();
2226 	trunk = ifp->if_vlantrunk;
2227 	if (trunk == NULL) {
2228 		VLAN_SUNLOCK();
2229 		return;
2230 	}
2231 	NET_EPOCH_ENTER(et);
2232 	VLAN_FOREACH(ifv, trunk)
2233 		vlan_capabilities(ifv);
2234 	NET_EPOCH_EXIT(et);
2235 	VLAN_SUNLOCK();
2236 }
2237 
2238 static int
vlan_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)2239 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2240 {
2241 	struct ifnet *p;
2242 	struct ifreq *ifr;
2243 #ifdef INET
2244 	struct ifaddr *ifa;
2245 #endif
2246 	struct ifvlan *ifv;
2247 	struct ifvlantrunk *trunk;
2248 	struct vlanreq vlr;
2249 	int error = 0, oldmtu;
2250 
2251 	ifr = (struct ifreq *)data;
2252 #ifdef INET
2253 	ifa = (struct ifaddr *) data;
2254 #endif
2255 	ifv = ifp->if_softc;
2256 
2257 	switch (cmd) {
2258 	case SIOCSIFADDR:
2259 		ifp->if_flags |= IFF_UP;
2260 #ifdef INET
2261 		if (ifa->ifa_addr->sa_family == AF_INET)
2262 			arp_ifinit(ifp, ifa);
2263 #endif
2264 		break;
2265 	case SIOCGIFADDR:
2266 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
2267 		    ifp->if_addrlen);
2268 		break;
2269 	case SIOCGIFMEDIA:
2270 		VLAN_SLOCK();
2271 		if (TRUNK(ifv) != NULL) {
2272 			p = PARENT(ifv);
2273 			if_ref(p);
2274 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
2275 			if_rele(p);
2276 			/* Limit the result to the parent's current config. */
2277 			if (error == 0) {
2278 				struct ifmediareq *ifmr;
2279 
2280 				ifmr = (struct ifmediareq *)data;
2281 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
2282 					ifmr->ifm_count = 1;
2283 					error = copyout(&ifmr->ifm_current,
2284 						ifmr->ifm_ulist,
2285 						sizeof(int));
2286 				}
2287 			}
2288 		} else {
2289 			error = EINVAL;
2290 		}
2291 		VLAN_SUNLOCK();
2292 		break;
2293 
2294 	case SIOCSIFMEDIA:
2295 		error = EINVAL;
2296 		break;
2297 
2298 	case SIOCSIFMTU:
2299 		/*
2300 		 * Set the interface MTU.
2301 		 */
2302 		VLAN_SLOCK();
2303 		trunk = TRUNK(ifv);
2304 		if (trunk != NULL) {
2305 			TRUNK_WLOCK(trunk);
2306 			if (ifr->ifr_mtu >
2307 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
2308 			    ifr->ifr_mtu <
2309 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
2310 				error = EINVAL;
2311 			else
2312 				ifp->if_mtu = ifr->ifr_mtu;
2313 			TRUNK_WUNLOCK(trunk);
2314 		} else
2315 			error = EINVAL;
2316 		VLAN_SUNLOCK();
2317 		break;
2318 
2319 	case SIOCSETVLAN:
2320 #ifdef VIMAGE
2321 		/*
2322 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
2323 		 * interface to be delegated to a jail without allowing the
2324 		 * jail to change what underlying interface/VID it is
2325 		 * associated with.  We are not entirely convinced that this
2326 		 * is the right way to accomplish that policy goal.
2327 		 */
2328 		if (ifp->if_vnet != ifp->if_home_vnet) {
2329 			error = EPERM;
2330 			break;
2331 		}
2332 #endif
2333 		error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
2334 		if (error)
2335 			break;
2336 		if (vlr.vlr_parent[0] == '\0') {
2337 			vlan_unconfig(ifp);
2338 			break;
2339 		}
2340 		p = ifunit_ref(vlr.vlr_parent);
2341 		if (p == NULL) {
2342 			error = ENOENT;
2343 			break;
2344 		}
2345 
2346 		/*
2347 		 * If the ifp is in a bridge, do not allow setting the device
2348 		 * to a bridge; this prevents having a bridge SVI as a bridge
2349 		 * member (which is not permitted).
2350 		 */
2351 		if (ifp->if_bridge != NULL && p->if_type == IFT_BRIDGE) {
2352 			if_rele(p);
2353 			error = EINVAL;
2354 			break;
2355 		}
2356 
2357 		if (vlr.vlr_proto == 0)
2358 			vlr.vlr_proto = ETHERTYPE_VLAN;
2359 		oldmtu = ifp->if_mtu;
2360 		error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto);
2361 		if_rele(p);
2362 
2363 		/*
2364 		 * VLAN MTU may change during addition of the vlandev.
2365 		 * If it did, do network layer specific procedure.
2366 		 */
2367 		if (ifp->if_mtu != oldmtu)
2368 			if_notifymtu(ifp);
2369 		break;
2370 
2371 	case SIOCGETVLAN:
2372 #ifdef VIMAGE
2373 		if (ifp->if_vnet != ifp->if_home_vnet) {
2374 			error = EPERM;
2375 			break;
2376 		}
2377 #endif
2378 		bzero(&vlr, sizeof(vlr));
2379 		VLAN_SLOCK();
2380 		if (TRUNK(ifv) != NULL) {
2381 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
2382 			    sizeof(vlr.vlr_parent));
2383 			vlr.vlr_tag = ifv->ifv_vid;
2384 			vlr.vlr_proto = ifv->ifv_proto;
2385 		}
2386 		VLAN_SUNLOCK();
2387 		error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
2388 		break;
2389 
2390 	case SIOCSIFFLAGS:
2391 		/*
2392 		 * We should propagate selected flags to the parent,
2393 		 * e.g., promiscuous mode.
2394 		 */
2395 		VLAN_SLOCK();
2396 		if (TRUNK(ifv) != NULL)
2397 			error = vlan_setflags(ifp, 1);
2398 		VLAN_SUNLOCK();
2399 		break;
2400 
2401 	case SIOCADDMULTI:
2402 	case SIOCDELMULTI:
2403 		/*
2404 		 * If we don't have a parent, just remember the membership for
2405 		 * when we do.
2406 		 *
2407 		 * XXX We need the rmlock here to avoid sleeping while
2408 		 * holding in6_multi_mtx.
2409 		 */
2410 		VLAN_XLOCK();
2411 		trunk = TRUNK(ifv);
2412 		if (trunk != NULL)
2413 			error = vlan_setmulti(ifp);
2414 		VLAN_XUNLOCK();
2415 
2416 		break;
2417 	case SIOCGVLANPCP:
2418 #ifdef VIMAGE
2419 		if (ifp->if_vnet != ifp->if_home_vnet) {
2420 			error = EPERM;
2421 			break;
2422 		}
2423 #endif
2424 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
2425 		break;
2426 
2427 	case SIOCSVLANPCP:
2428 #ifdef VIMAGE
2429 		if (ifp->if_vnet != ifp->if_home_vnet) {
2430 			error = EPERM;
2431 			break;
2432 		}
2433 #endif
2434 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
2435 		if (error)
2436 			break;
2437 		if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) {
2438 			error = EINVAL;
2439 			break;
2440 		}
2441 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
2442 		ifp->if_pcp = ifv->ifv_pcp;
2443 		/* broadcast event about PCP change */
2444 		EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
2445 		break;
2446 
2447 	case SIOCSIFCAP:
2448 		VLAN_SLOCK();
2449 		ifv->ifv_capenable = ifr->ifr_reqcap;
2450 		trunk = TRUNK(ifv);
2451 		if (trunk != NULL) {
2452 			struct epoch_tracker et;
2453 
2454 			NET_EPOCH_ENTER(et);
2455 			vlan_capabilities(ifv);
2456 			NET_EPOCH_EXIT(et);
2457 		}
2458 		VLAN_SUNLOCK();
2459 		break;
2460 
2461 	default:
2462 		error = EINVAL;
2463 		break;
2464 	}
2465 
2466 	return (error);
2467 }
2468 
2469 #if defined(KERN_TLS) || defined(RATELIMIT)
2470 static int
vlan_snd_tag_alloc(struct ifnet * ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** ppmt)2471 vlan_snd_tag_alloc(struct ifnet *ifp,
2472     union if_snd_tag_alloc_params *params,
2473     struct m_snd_tag **ppmt)
2474 {
2475 	struct epoch_tracker et;
2476 	const struct if_snd_tag_sw *sw;
2477 	struct vlan_snd_tag *vst;
2478 	struct ifvlan *ifv;
2479 	struct ifnet *parent;
2480 	struct m_snd_tag *mst;
2481 	int error;
2482 
2483 	NET_EPOCH_ENTER(et);
2484 	ifv = ifp->if_softc;
2485 
2486 	switch (params->hdr.type) {
2487 #ifdef RATELIMIT
2488 	case IF_SND_TAG_TYPE_UNLIMITED:
2489 		sw = &vlan_snd_tag_ul_sw;
2490 		break;
2491 	case IF_SND_TAG_TYPE_RATE_LIMIT:
2492 		sw = &vlan_snd_tag_rl_sw;
2493 		break;
2494 #endif
2495 #ifdef KERN_TLS
2496 	case IF_SND_TAG_TYPE_TLS:
2497 		sw = &vlan_snd_tag_tls_sw;
2498 		break;
2499 	case IF_SND_TAG_TYPE_TLS_RX:
2500 		sw = NULL;
2501 		if (params->tls_rx.vlan_id != 0)
2502 			goto failure;
2503 		params->tls_rx.vlan_id = ifv->ifv_vid;
2504 		break;
2505 #ifdef RATELIMIT
2506 	case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
2507 		sw = &vlan_snd_tag_tls_rl_sw;
2508 		break;
2509 #endif
2510 #endif
2511 	default:
2512 		goto failure;
2513 	}
2514 
2515 	if (ifv->ifv_trunk != NULL)
2516 		parent = PARENT(ifv);
2517 	else
2518 		parent = NULL;
2519 	if (parent == NULL)
2520 		goto failure;
2521 	if_ref(parent);
2522 	NET_EPOCH_EXIT(et);
2523 
2524 	if (sw != NULL) {
2525 		vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2526 		if (vst == NULL) {
2527 			if_rele(parent);
2528 			return (ENOMEM);
2529 		}
2530 	} else
2531 		vst = NULL;
2532 
2533 	error = m_snd_tag_alloc(parent, params, &mst);
2534 	if_rele(parent);
2535 	if (error) {
2536 		free(vst, M_VLAN);
2537 		return (error);
2538 	}
2539 
2540 	if (sw != NULL) {
2541 		m_snd_tag_init(&vst->com, ifp, sw);
2542 		vst->tag = mst;
2543 
2544 		*ppmt = &vst->com;
2545 	} else
2546 		*ppmt = mst;
2547 
2548 	return (0);
2549 failure:
2550 	NET_EPOCH_EXIT(et);
2551 	return (EOPNOTSUPP);
2552 }
2553 
2554 static struct m_snd_tag *
vlan_next_snd_tag(struct m_snd_tag * mst)2555 vlan_next_snd_tag(struct m_snd_tag *mst)
2556 {
2557 	struct vlan_snd_tag *vst;
2558 
2559 	vst = mst_to_vst(mst);
2560 	return (vst->tag);
2561 }
2562 
2563 static int
vlan_snd_tag_modify(struct m_snd_tag * mst,union if_snd_tag_modify_params * params)2564 vlan_snd_tag_modify(struct m_snd_tag *mst,
2565     union if_snd_tag_modify_params *params)
2566 {
2567 	struct vlan_snd_tag *vst;
2568 
2569 	vst = mst_to_vst(mst);
2570 	return (vst->tag->sw->snd_tag_modify(vst->tag, params));
2571 }
2572 
2573 static int
vlan_snd_tag_query(struct m_snd_tag * mst,union if_snd_tag_query_params * params)2574 vlan_snd_tag_query(struct m_snd_tag *mst,
2575     union if_snd_tag_query_params *params)
2576 {
2577 	struct vlan_snd_tag *vst;
2578 
2579 	vst = mst_to_vst(mst);
2580 	return (vst->tag->sw->snd_tag_query(vst->tag, params));
2581 }
2582 
2583 static void
vlan_snd_tag_free(struct m_snd_tag * mst)2584 vlan_snd_tag_free(struct m_snd_tag *mst)
2585 {
2586 	struct vlan_snd_tag *vst;
2587 
2588 	vst = mst_to_vst(mst);
2589 	m_snd_tag_rele(vst->tag);
2590 	free(vst, M_VLAN);
2591 }
2592 
2593 static void
vlan_ratelimit_query(struct ifnet * ifp __unused,struct if_ratelimit_query_results * q)2594 vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
2595 {
2596 	/*
2597 	 * For vlan, we have an indirect
2598 	 * interface. The caller needs to
2599 	 * get a ratelimit tag on the actual
2600 	 * interface the flow will go on.
2601 	 */
2602 	q->rate_table = NULL;
2603 	q->flags = RT_IS_INDIRECT;
2604 	q->max_flows = 0;
2605 	q->number_of_rates = 0;
2606 }
2607 
2608 #endif
2609