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