1 /*-
2 * Copyright 1998 Massachusetts Institute of Technology
3 * Copyright 2012 ADARA Networks, Inc.
4 * Copyright 2017 Dell EMC Isilon
5 *
6 * Portions of this software were developed by Robert N. M. Watson under
7 * contract to ADARA Networks, Inc.
8 *
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby
11 * granted, provided that both the above copyright notice and this
12 * permission notice appear in all copies, that both the above
13 * copyright notice and this permission notice appear in all
14 * supporting documentation, and that the name of M.I.T. not be used
15 * in advertising or publicity pertaining to distribution of the
16 * software without specific, written prior permission. M.I.T. makes
17 * no representations about the suitability of this software for any
18 * purpose. It is provided "as is" without express or implied
19 * warranty.
20 *
21 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
22 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
25 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
37 * This is sort of sneaky in the implementation, since
38 * we need to pretend to be enough of an Ethernet implementation
39 * to make arp work. The way we do this is by telling everyone
40 * that we are an Ethernet, and then catch the packets that
41 * ether_output() sends to us via if_transmit(), rewrite them for
42 * use by the real outgoing interface, and ask it to send them.
43 */
44
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47
48 #include "opt_inet.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_clone.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <net/if_vlan_var.h>
77 #include <net/vnet.h>
78
79 #ifdef INET
80 #include <netinet/in.h>
81 #include <netinet/if_ether.h>
82 #endif
83
84 #define VLAN_DEF_HWIDTH 4
85 #define VLAN_IFFLAGS (IFF_BROADCAST | IFF_MULTICAST)
86
87 #define UP_AND_RUNNING(ifp) \
88 ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
89
90 CK_SLIST_HEAD(ifvlanhead, ifvlan);
91
92 struct ifvlantrunk {
93 struct ifnet *parent; /* parent interface of this trunk */
94 struct mtx lock;
95 #ifdef VLAN_ARRAY
96 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
97 struct ifvlan *vlans[VLAN_ARRAY_SIZE]; /* static table */
98 #else
99 struct ifvlanhead *hash; /* dynamic hash-list table */
100 uint16_t hmask;
101 uint16_t hwidth;
102 #endif
103 int refcnt;
104 };
105
106 /*
107 * This macro provides a facility to iterate over every vlan on a trunk with
108 * the assumption that none will be added/removed during iteration.
109 */
110 #ifdef VLAN_ARRAY
111 #define VLAN_FOREACH(_ifv, _trunk) \
112 size_t _i; \
113 for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
114 if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
115 #else /* VLAN_ARRAY */
116 #define VLAN_FOREACH(_ifv, _trunk) \
117 struct ifvlan *_next; \
118 size_t _i; \
119 for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
120 CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
121 #endif /* VLAN_ARRAY */
122
123 /*
124 * This macro provides a facility to iterate over every vlan on a trunk while
125 * also modifying the number of vlans on the trunk. The iteration continues
126 * until some condition is met or there are no more vlans on the trunk.
127 */
128 #ifdef VLAN_ARRAY
129 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
130 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
131 size_t _i; \
132 for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
133 if (((_ifv) = (_trunk)->vlans[_i]))
134 #else /* VLAN_ARRAY */
135 /*
136 * The hash table case is more complicated. We allow for the hash table to be
137 * modified (i.e. vlans removed) while we are iterating over it. To allow for
138 * this we must restart the iteration every time we "touch" something during
139 * the iteration, since removal will resize the hash table and invalidate our
140 * current position. If acting on the touched element causes the trunk to be
141 * emptied, then iteration also stops.
142 */
143 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
144 size_t _i; \
145 bool _touch = false; \
146 for (_i = 0; \
147 !(_cond) && _i < (1 << (_trunk)->hwidth); \
148 _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
149 if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
150 (_touch = true))
151 #endif /* VLAN_ARRAY */
152
153 struct vlan_mc_entry {
154 struct sockaddr_dl mc_addr;
155 CK_SLIST_ENTRY(vlan_mc_entry) mc_entries;
156 struct epoch_context mc_epoch_ctx;
157 };
158
159 struct ifvlan {
160 struct ifvlantrunk *ifv_trunk;
161 struct ifnet *ifv_ifp;
162 #define TRUNK(ifv) ((ifv)->ifv_trunk)
163 #define PARENT(ifv) ((ifv)->ifv_trunk->parent)
164 void *ifv_cookie;
165 int ifv_pflags; /* special flags we have set on parent */
166 int ifv_capenable;
167 int ifv_encaplen; /* encapsulation length */
168 int ifv_mtufudge; /* MTU fudged by this much */
169 int ifv_mintu; /* min transmission unit */
170 uint16_t ifv_proto; /* encapsulation ethertype */
171 uint16_t ifv_tag; /* tag to apply on packets leaving if */
172 uint16_t ifv_vid; /* VLAN ID */
173 uint8_t ifv_pcp; /* Priority Code Point (PCP). */
174 struct task lladdr_task;
175 CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
176 #ifndef VLAN_ARRAY
177 CK_SLIST_ENTRY(ifvlan) ifv_list;
178 #endif
179 };
180
181 /* Special flags we should propagate to parent. */
182 static struct {
183 int flag;
184 int (*func)(struct ifnet *, int);
185 } vlan_pflags[] = {
186 {IFF_PROMISC, ifpromisc},
187 {IFF_ALLMULTI, if_allmulti},
188 {0, NULL}
189 };
190
191 extern int vlan_mtag_pcp;
192
193 static const char vlanname[] = "vlan";
194 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
195
196 static eventhandler_tag ifdetach_tag;
197 static eventhandler_tag iflladdr_tag;
198
199 /*
200 * if_vlan uses two module-level synchronizations primitives to allow concurrent
201 * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
202 * while they are being used for tx/rx. To accomplish this in a way that has
203 * acceptable performance and cooperation with other parts of the network stack
204 * there is a non-sleepable epoch(9) and an sx(9).
205 *
206 * The performance-sensitive paths that warrant using the epoch(9) are
207 * vlan_transmit and vlan_input. Both have to check for the vlan interface's
208 * existence using if_vlantrunk, and being in the network tx/rx paths the use
209 * of an epoch(9) gives a measureable improvement in performance.
210 *
211 * The reason for having an sx(9) is mostly because there are still areas that
212 * must be sleepable and also have safe concurrent access to a vlan interface.
213 * Since the sx(9) exists, it is used by default in most paths unless sleeping
214 * is not permitted, or if it is not clear whether sleeping is permitted.
215 *
216 */
217 #define _VLAN_SX_ID ifv_sx
218
219 static struct sx _VLAN_SX_ID;
220
221 #define VLAN_LOCKING_INIT() \
222 sx_init(&_VLAN_SX_ID, "vlan_sx")
223
224 #define VLAN_LOCKING_DESTROY() \
225 sx_destroy(&_VLAN_SX_ID)
226
227 #define VLAN_RLOCK() NET_EPOCH_ENTER();
228 #define VLAN_RUNLOCK() NET_EPOCH_EXIT();
229 #define VLAN_RLOCK_ASSERT() MPASS(in_epoch(net_epoch_preempt))
230
231 #define VLAN_SLOCK() sx_slock(&_VLAN_SX_ID)
232 #define VLAN_SUNLOCK() sx_sunlock(&_VLAN_SX_ID)
233 #define VLAN_XLOCK() sx_xlock(&_VLAN_SX_ID)
234 #define VLAN_XUNLOCK() sx_xunlock(&_VLAN_SX_ID)
235 #define VLAN_SLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
236 #define VLAN_XLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
237 #define VLAN_SXLOCK_ASSERT() sx_assert(&_VLAN_SX_ID, SA_LOCKED)
238
239
240 /*
241 * We also have a per-trunk mutex that should be acquired when changing
242 * its state.
243 */
244 #define TRUNK_LOCK_INIT(trunk) mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
245 #define TRUNK_LOCK_DESTROY(trunk) mtx_destroy(&(trunk)->lock)
246 #define TRUNK_RLOCK(trunk) NET_EPOCH_ENTER()
247 #define TRUNK_WLOCK(trunk) mtx_lock(&(trunk)->lock)
248 #define TRUNK_RUNLOCK(trunk) NET_EPOCH_EXIT();
249 #define TRUNK_WUNLOCK(trunk) mtx_unlock(&(trunk)->lock)
250 #define TRUNK_RLOCK_ASSERT(trunk) MPASS(in_epoch(net_epoch_preempt))
251 #define TRUNK_LOCK_ASSERT(trunk) MPASS(in_epoch(net_epoch_preempt) || mtx_owned(&(trunk)->lock))
252 #define TRUNK_WLOCK_ASSERT(trunk) mtx_assert(&(trunk)->lock, MA_OWNED);
253
254 /*
255 * The VLAN_ARRAY substitutes the dynamic hash with a static array
256 * with 4096 entries. In theory this can give a boost in processing,
257 * however in practice it does not. Probably this is because the array
258 * is too big to fit into CPU cache.
259 */
260 #ifndef VLAN_ARRAY
261 static void vlan_inithash(struct ifvlantrunk *trunk);
262 static void vlan_freehash(struct ifvlantrunk *trunk);
263 static int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
264 static int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
265 static void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
266 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
267 uint16_t vid);
268 #endif
269 static void trunk_destroy(struct ifvlantrunk *trunk);
270
271 static void vlan_init(void *foo);
272 static void vlan_input(struct ifnet *ifp, struct mbuf *m);
273 static int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
274 #ifdef RATELIMIT
275 static int vlan_snd_tag_alloc(struct ifnet *,
276 union if_snd_tag_alloc_params *, struct m_snd_tag **);
277 #endif
278 static void vlan_qflush(struct ifnet *ifp);
279 static int vlan_setflag(struct ifnet *ifp, int flag, int status,
280 int (*func)(struct ifnet *, int));
281 static int vlan_setflags(struct ifnet *ifp, int status);
282 static int vlan_setmulti(struct ifnet *ifp);
283 static int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
284 static void vlan_unconfig(struct ifnet *ifp);
285 static void vlan_unconfig_locked(struct ifnet *ifp, int departing);
286 static int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
287 static void vlan_link_state(struct ifnet *ifp);
288 static void vlan_capabilities(struct ifvlan *ifv);
289 static void vlan_trunk_capabilities(struct ifnet *ifp);
290
291 static struct ifnet *vlan_clone_match_ethervid(const char *, int *);
292 static int vlan_clone_match(struct if_clone *, const char *);
293 static int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
294 static int vlan_clone_destroy(struct if_clone *, struct ifnet *);
295
296 static void vlan_ifdetach(void *arg, struct ifnet *ifp);
297 static void vlan_iflladdr(void *arg, struct ifnet *ifp);
298
299 static void vlan_lladdr_fn(void *arg, int pending);
300
301 static struct if_clone *vlan_cloner;
302
303 #ifdef VIMAGE
304 VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
305 #define V_vlan_cloner VNET(vlan_cloner)
306 #endif
307
308 static void
vlan_mc_free(struct epoch_context * ctx)309 vlan_mc_free(struct epoch_context *ctx)
310 {
311 struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
312 free(mc, M_VLAN);
313 }
314
315 #ifndef VLAN_ARRAY
316 #define HASH(n, m) ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
317
318 static void
vlan_inithash(struct ifvlantrunk * trunk)319 vlan_inithash(struct ifvlantrunk *trunk)
320 {
321 int i, n;
322
323 /*
324 * The trunk must not be locked here since we call malloc(M_WAITOK).
325 * It is OK in case this function is called before the trunk struct
326 * gets hooked up and becomes visible from other threads.
327 */
328
329 KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
330 ("%s: hash already initialized", __func__));
331
332 trunk->hwidth = VLAN_DEF_HWIDTH;
333 n = 1 << trunk->hwidth;
334 trunk->hmask = n - 1;
335 trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
336 for (i = 0; i < n; i++)
337 CK_SLIST_INIT(&trunk->hash[i]);
338 }
339
340 static void
vlan_freehash(struct ifvlantrunk * trunk)341 vlan_freehash(struct ifvlantrunk *trunk)
342 {
343 #ifdef INVARIANTS
344 int i;
345
346 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
347 for (i = 0; i < (1 << trunk->hwidth); i++)
348 KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
349 ("%s: hash table not empty", __func__));
350 #endif
351 free(trunk->hash, M_VLAN);
352 trunk->hash = NULL;
353 trunk->hwidth = trunk->hmask = 0;
354 }
355
356 static int
vlan_inshash(struct ifvlantrunk * trunk,struct ifvlan * ifv)357 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
358 {
359 int i, b;
360 struct ifvlan *ifv2;
361
362 VLAN_XLOCK_ASSERT();
363 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
364
365 b = 1 << trunk->hwidth;
366 i = HASH(ifv->ifv_vid, trunk->hmask);
367 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
368 if (ifv->ifv_vid == ifv2->ifv_vid)
369 return (EEXIST);
370
371 /*
372 * Grow the hash when the number of vlans exceeds half of the number of
373 * hash buckets squared. This will make the average linked-list length
374 * buckets/2.
375 */
376 if (trunk->refcnt > (b * b) / 2) {
377 vlan_growhash(trunk, 1);
378 i = HASH(ifv->ifv_vid, trunk->hmask);
379 }
380 CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
381 trunk->refcnt++;
382
383 return (0);
384 }
385
386 static int
vlan_remhash(struct ifvlantrunk * trunk,struct ifvlan * ifv)387 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
388 {
389 int i, b;
390 struct ifvlan *ifv2;
391
392 VLAN_XLOCK_ASSERT();
393 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
394
395 b = 1 << trunk->hwidth;
396 i = HASH(ifv->ifv_vid, trunk->hmask);
397 CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
398 if (ifv2 == ifv) {
399 trunk->refcnt--;
400 CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
401 if (trunk->refcnt < (b * b) / 2)
402 vlan_growhash(trunk, -1);
403 return (0);
404 }
405
406 panic("%s: vlan not found\n", __func__);
407 return (ENOENT); /*NOTREACHED*/
408 }
409
410 /*
411 * Grow the hash larger or smaller if memory permits.
412 */
413 static void
vlan_growhash(struct ifvlantrunk * trunk,int howmuch)414 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
415 {
416 struct ifvlan *ifv;
417 struct ifvlanhead *hash2;
418 int hwidth2, i, j, n, n2;
419
420 VLAN_XLOCK_ASSERT();
421 KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
422
423 if (howmuch == 0) {
424 /* Harmless yet obvious coding error */
425 printf("%s: howmuch is 0\n", __func__);
426 return;
427 }
428
429 hwidth2 = trunk->hwidth + howmuch;
430 n = 1 << trunk->hwidth;
431 n2 = 1 << hwidth2;
432 /* Do not shrink the table below the default */
433 if (hwidth2 < VLAN_DEF_HWIDTH)
434 return;
435
436 hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
437 if (hash2 == NULL) {
438 printf("%s: out of memory -- hash size not changed\n",
439 __func__);
440 return; /* We can live with the old hash table */
441 }
442 for (j = 0; j < n2; j++)
443 CK_SLIST_INIT(&hash2[j]);
444 for (i = 0; i < n; i++)
445 while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
446 CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
447 j = HASH(ifv->ifv_vid, n2 - 1);
448 CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
449 }
450 NET_EPOCH_WAIT();
451 free(trunk->hash, M_VLAN);
452 trunk->hash = hash2;
453 trunk->hwidth = hwidth2;
454 trunk->hmask = n2 - 1;
455
456 if (bootverbose)
457 if_printf(trunk->parent,
458 "VLAN hash table resized from %d to %d buckets\n", n, n2);
459 }
460
461 static __inline struct ifvlan *
vlan_gethash(struct ifvlantrunk * trunk,uint16_t vid)462 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
463 {
464 struct ifvlan *ifv;
465
466 TRUNK_RLOCK_ASSERT(trunk);
467
468 CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
469 if (ifv->ifv_vid == vid)
470 return (ifv);
471 return (NULL);
472 }
473
474 #if 0
475 /* Debugging code to view the hashtables. */
476 static void
477 vlan_dumphash(struct ifvlantrunk *trunk)
478 {
479 int i;
480 struct ifvlan *ifv;
481
482 for (i = 0; i < (1 << trunk->hwidth); i++) {
483 printf("%d: ", i);
484 CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
485 printf("%s ", ifv->ifv_ifp->if_xname);
486 printf("\n");
487 }
488 }
489 #endif /* 0 */
490 #else
491
492 static __inline struct ifvlan *
vlan_gethash(struct ifvlantrunk * trunk,uint16_t vid)493 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
494 {
495
496 return trunk->vlans[vid];
497 }
498
499 static __inline int
vlan_inshash(struct ifvlantrunk * trunk,struct ifvlan * ifv)500 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
501 {
502
503 if (trunk->vlans[ifv->ifv_vid] != NULL)
504 return EEXIST;
505 trunk->vlans[ifv->ifv_vid] = ifv;
506 trunk->refcnt++;
507
508 return (0);
509 }
510
511 static __inline int
vlan_remhash(struct ifvlantrunk * trunk,struct ifvlan * ifv)512 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
513 {
514
515 trunk->vlans[ifv->ifv_vid] = NULL;
516 trunk->refcnt--;
517
518 return (0);
519 }
520
521 static __inline void
vlan_freehash(struct ifvlantrunk * trunk)522 vlan_freehash(struct ifvlantrunk *trunk)
523 {
524 }
525
526 static __inline void
vlan_inithash(struct ifvlantrunk * trunk)527 vlan_inithash(struct ifvlantrunk *trunk)
528 {
529 }
530
531 #endif /* !VLAN_ARRAY */
532
533 static void
trunk_destroy(struct ifvlantrunk * trunk)534 trunk_destroy(struct ifvlantrunk *trunk)
535 {
536 VLAN_XLOCK_ASSERT();
537
538 vlan_freehash(trunk);
539 trunk->parent->if_vlantrunk = NULL;
540 TRUNK_LOCK_DESTROY(trunk);
541 if_rele(trunk->parent);
542 free(trunk, M_VLAN);
543 }
544
545 /*
546 * Program our multicast filter. What we're actually doing is
547 * programming the multicast filter of the parent. This has the
548 * side effect of causing the parent interface to receive multicast
549 * traffic that it doesn't really want, which ends up being discarded
550 * later by the upper protocol layers. Unfortunately, there's no way
551 * to avoid this: there really is only one physical interface.
552 */
553 static int
vlan_setmulti(struct ifnet * ifp)554 vlan_setmulti(struct ifnet *ifp)
555 {
556 struct ifnet *ifp_p;
557 struct ifmultiaddr *ifma;
558 struct ifvlan *sc;
559 struct vlan_mc_entry *mc;
560 int error;
561
562 VLAN_XLOCK_ASSERT();
563
564 /* Find the parent. */
565 sc = ifp->if_softc;
566 ifp_p = PARENT(sc);
567
568 CURVNET_SET_QUIET(ifp_p->if_vnet);
569
570 /* First, remove any existing filter entries. */
571 while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
572 CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
573 (void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
574 epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
575 }
576
577 /* Now program new ones. */
578 IF_ADDR_WLOCK(ifp);
579 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
580 if (ifma->ifma_addr->sa_family != AF_LINK)
581 continue;
582 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
583 if (mc == NULL) {
584 IF_ADDR_WUNLOCK(ifp);
585 return (ENOMEM);
586 }
587 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
588 mc->mc_addr.sdl_index = ifp_p->if_index;
589 CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
590 }
591 IF_ADDR_WUNLOCK(ifp);
592 CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
593 error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
594 NULL);
595 if (error)
596 return (error);
597 }
598
599 CURVNET_RESTORE();
600 return (0);
601 }
602
603 /*
604 * A handler for parent interface link layer address changes.
605 * If the parent interface link layer address is changed we
606 * should also change it on all children vlans.
607 */
608 static void
vlan_iflladdr(void * arg __unused,struct ifnet * ifp)609 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
610 {
611 struct ifvlan *ifv;
612 struct ifnet *ifv_ifp;
613 struct ifvlantrunk *trunk;
614 struct sockaddr_dl *sdl;
615
616 /* Need the rmlock since this is run on taskqueue_swi. */
617 VLAN_RLOCK();
618 trunk = ifp->if_vlantrunk;
619 if (trunk == NULL) {
620 VLAN_RUNLOCK();
621 return;
622 }
623
624 /*
625 * OK, it's a trunk. Loop over and change all vlan's lladdrs on it.
626 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
627 * ioctl calls on the parent garbling the lladdr of the child vlan.
628 */
629 TRUNK_WLOCK(trunk);
630 VLAN_FOREACH(ifv, trunk) {
631 /*
632 * Copy new new lladdr into the ifv_ifp, enqueue a task
633 * to actually call if_setlladdr. if_setlladdr needs to
634 * be deferred to a taskqueue because it will call into
635 * the if_vlan ioctl path and try to acquire the global
636 * lock.
637 */
638 ifv_ifp = ifv->ifv_ifp;
639 bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
640 ifp->if_addrlen);
641 sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
642 sdl->sdl_alen = ifp->if_addrlen;
643 taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
644 }
645 TRUNK_WUNLOCK(trunk);
646 VLAN_RUNLOCK();
647 }
648
649 /*
650 * A handler for network interface departure events.
651 * Track departure of trunks here so that we don't access invalid
652 * pointers or whatever if a trunk is ripped from under us, e.g.,
653 * by ejecting its hot-plug card. However, if an ifnet is simply
654 * being renamed, then there's no need to tear down the state.
655 */
656 static void
vlan_ifdetach(void * arg __unused,struct ifnet * ifp)657 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
658 {
659 struct ifvlan *ifv;
660 struct ifvlantrunk *trunk;
661
662 /* If the ifnet is just being renamed, don't do anything. */
663 if (ifp->if_flags & IFF_RENAMING)
664 return;
665 VLAN_XLOCK();
666 trunk = ifp->if_vlantrunk;
667 if (trunk == NULL) {
668 VLAN_XUNLOCK();
669 return;
670 }
671
672 /*
673 * OK, it's a trunk. Loop over and detach all vlan's on it.
674 * Check trunk pointer after each vlan_unconfig() as it will
675 * free it and set to NULL after the last vlan was detached.
676 */
677 VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
678 ifp->if_vlantrunk == NULL)
679 vlan_unconfig_locked(ifv->ifv_ifp, 1);
680
681 /* Trunk should have been destroyed in vlan_unconfig(). */
682 KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
683 VLAN_XUNLOCK();
684 }
685
686 /*
687 * Return the trunk device for a virtual interface.
688 */
689 static struct ifnet *
vlan_trunkdev(struct ifnet * ifp)690 vlan_trunkdev(struct ifnet *ifp)
691 {
692 struct ifvlan *ifv;
693
694 if (ifp->if_type != IFT_L2VLAN)
695 return (NULL);
696
697 VLAN_RLOCK();
698 ifv = ifp->if_softc;
699 ifp = NULL;
700 if (ifv->ifv_trunk)
701 ifp = PARENT(ifv);
702 VLAN_RUNLOCK();
703 return (ifp);
704 }
705
706 /*
707 * Return the 12-bit VLAN VID for this interface, for use by external
708 * components such as Infiniband.
709 *
710 * XXXRW: Note that the function name here is historical; it should be named
711 * vlan_vid().
712 */
713 static int
vlan_tag(struct ifnet * ifp,uint16_t * vidp)714 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
715 {
716 struct ifvlan *ifv;
717
718 if (ifp->if_type != IFT_L2VLAN)
719 return (EINVAL);
720 ifv = ifp->if_softc;
721 *vidp = ifv->ifv_vid;
722 return (0);
723 }
724
725 static int
vlan_pcp(struct ifnet * ifp,uint16_t * pcpp)726 vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
727 {
728 struct ifvlan *ifv;
729
730 if (ifp->if_type != IFT_L2VLAN)
731 return (EINVAL);
732 ifv = ifp->if_softc;
733 *pcpp = ifv->ifv_pcp;
734 return (0);
735 }
736
737 /*
738 * Return a driver specific cookie for this interface. Synchronization
739 * with setcookie must be provided by the driver.
740 */
741 static void *
vlan_cookie(struct ifnet * ifp)742 vlan_cookie(struct ifnet *ifp)
743 {
744 struct ifvlan *ifv;
745
746 if (ifp->if_type != IFT_L2VLAN)
747 return (NULL);
748 ifv = ifp->if_softc;
749 return (ifv->ifv_cookie);
750 }
751
752 /*
753 * Store a cookie in our softc that drivers can use to store driver
754 * private per-instance data in.
755 */
756 static int
vlan_setcookie(struct ifnet * ifp,void * cookie)757 vlan_setcookie(struct ifnet *ifp, void *cookie)
758 {
759 struct ifvlan *ifv;
760
761 if (ifp->if_type != IFT_L2VLAN)
762 return (EINVAL);
763 ifv = ifp->if_softc;
764 ifv->ifv_cookie = cookie;
765 return (0);
766 }
767
768 /*
769 * Return the vlan device present at the specific VID.
770 */
771 static struct ifnet *
vlan_devat(struct ifnet * ifp,uint16_t vid)772 vlan_devat(struct ifnet *ifp, uint16_t vid)
773 {
774 struct ifvlantrunk *trunk;
775 struct ifvlan *ifv;
776
777 VLAN_RLOCK();
778 trunk = ifp->if_vlantrunk;
779 if (trunk == NULL) {
780 VLAN_RUNLOCK();
781 return (NULL);
782 }
783 ifp = NULL;
784 ifv = vlan_gethash(trunk, vid);
785 if (ifv)
786 ifp = ifv->ifv_ifp;
787 VLAN_RUNLOCK();
788 return (ifp);
789 }
790
791 /*
792 * Recalculate the cached VLAN tag exposed via the MIB.
793 */
794 static void
vlan_tag_recalculate(struct ifvlan * ifv)795 vlan_tag_recalculate(struct ifvlan *ifv)
796 {
797
798 ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
799 }
800
801 /*
802 * VLAN support can be loaded as a module. The only place in the
803 * system that's intimately aware of this is ether_input. We hook
804 * into this code through vlan_input_p which is defined there and
805 * set here. No one else in the system should be aware of this so
806 * we use an explicit reference here.
807 */
808 extern void (*vlan_input_p)(struct ifnet *, struct mbuf *);
809
810 /* For if_link_state_change() eyes only... */
811 extern void (*vlan_link_state_p)(struct ifnet *);
812
813 static int
vlan_modevent(module_t mod,int type,void * data)814 vlan_modevent(module_t mod, int type, void *data)
815 {
816
817 switch (type) {
818 case MOD_LOAD:
819 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
820 vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
821 if (ifdetach_tag == NULL)
822 return (ENOMEM);
823 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
824 vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
825 if (iflladdr_tag == NULL)
826 return (ENOMEM);
827 VLAN_LOCKING_INIT();
828 vlan_input_p = vlan_input;
829 vlan_link_state_p = vlan_link_state;
830 vlan_trunk_cap_p = vlan_trunk_capabilities;
831 vlan_trunkdev_p = vlan_trunkdev;
832 vlan_cookie_p = vlan_cookie;
833 vlan_setcookie_p = vlan_setcookie;
834 vlan_tag_p = vlan_tag;
835 vlan_pcp_p = vlan_pcp;
836 vlan_devat_p = vlan_devat;
837 #ifndef VIMAGE
838 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
839 vlan_clone_create, vlan_clone_destroy);
840 #endif
841 if (bootverbose)
842 printf("vlan: initialized, using "
843 #ifdef VLAN_ARRAY
844 "full-size arrays"
845 #else
846 "hash tables with chaining"
847 #endif
848
849 "\n");
850 break;
851 case MOD_UNLOAD:
852 #ifndef VIMAGE
853 if_clone_detach(vlan_cloner);
854 #endif
855 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
856 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
857 vlan_input_p = NULL;
858 vlan_link_state_p = NULL;
859 vlan_trunk_cap_p = NULL;
860 vlan_trunkdev_p = NULL;
861 vlan_tag_p = NULL;
862 vlan_cookie_p = NULL;
863 vlan_setcookie_p = NULL;
864 vlan_devat_p = NULL;
865 VLAN_LOCKING_DESTROY();
866 if (bootverbose)
867 printf("vlan: unloaded\n");
868 break;
869 default:
870 return (EOPNOTSUPP);
871 }
872 return (0);
873 }
874
875 static moduledata_t vlan_mod = {
876 "if_vlan",
877 vlan_modevent,
878 0
879 };
880
881 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
882 MODULE_VERSION(if_vlan, 3);
883
884 #ifdef VIMAGE
885 static void
vnet_vlan_init(const void * unused __unused)886 vnet_vlan_init(const void *unused __unused)
887 {
888
889 vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
890 vlan_clone_create, vlan_clone_destroy);
891 V_vlan_cloner = vlan_cloner;
892 }
893 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
894 vnet_vlan_init, NULL);
895
896 static void
vnet_vlan_uninit(const void * unused __unused)897 vnet_vlan_uninit(const void *unused __unused)
898 {
899
900 if_clone_detach(V_vlan_cloner);
901 }
902 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
903 vnet_vlan_uninit, NULL);
904 #endif
905
906 /*
907 * Check for <etherif>.<vlan> style interface names.
908 */
909 static struct ifnet *
vlan_clone_match_ethervid(const char * name,int * vidp)910 vlan_clone_match_ethervid(const char *name, int *vidp)
911 {
912 char ifname[IFNAMSIZ];
913 char *cp;
914 struct ifnet *ifp;
915 int vid;
916
917 strlcpy(ifname, name, IFNAMSIZ);
918 if ((cp = strchr(ifname, '.')) == NULL)
919 return (NULL);
920 *cp = '\0';
921 if ((ifp = ifunit_ref(ifname)) == NULL)
922 return (NULL);
923 /* Parse VID. */
924 if (*++cp == '\0') {
925 if_rele(ifp);
926 return (NULL);
927 }
928 vid = 0;
929 for(; *cp >= '0' && *cp <= '9'; cp++)
930 vid = (vid * 10) + (*cp - '0');
931 if (*cp != '\0') {
932 if_rele(ifp);
933 return (NULL);
934 }
935 if (vidp != NULL)
936 *vidp = vid;
937
938 return (ifp);
939 }
940
941 static int
vlan_clone_match(struct if_clone * ifc,const char * name)942 vlan_clone_match(struct if_clone *ifc, const char *name)
943 {
944 const char *cp;
945
946 if (vlan_clone_match_ethervid(name, NULL) != NULL)
947 return (1);
948
949 if (strncmp(vlanname, name, strlen(vlanname)) != 0)
950 return (0);
951 for (cp = name + 4; *cp != '\0'; cp++) {
952 if (*cp < '0' || *cp > '9')
953 return (0);
954 }
955
956 return (1);
957 }
958
959 static int
vlan_clone_create(struct if_clone * ifc,char * name,size_t len,caddr_t params)960 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
961 {
962 char *dp;
963 int wildcard;
964 int unit;
965 int error;
966 int vid;
967 struct ifvlan *ifv;
968 struct ifnet *ifp;
969 struct ifnet *p;
970 struct ifaddr *ifa;
971 struct sockaddr_dl *sdl;
972 struct vlanreq vlr;
973 static const u_char eaddr[ETHER_ADDR_LEN]; /* 00:00:00:00:00:00 */
974
975 /*
976 * There are 3 (ugh) ways to specify the cloned device:
977 * o pass a parameter block with the clone request.
978 * o specify parameters in the text of the clone device name
979 * o specify no parameters and get an unattached device that
980 * must be configured separately.
981 * The first technique is preferred; the latter two are
982 * supported for backwards compatibility.
983 *
984 * XXXRW: Note historic use of the word "tag" here. New ioctls may be
985 * called for.
986 */
987 if (params) {
988 error = copyin(params, &vlr, sizeof(vlr));
989 if (error)
990 return error;
991 p = ifunit_ref(vlr.vlr_parent);
992 if (p == NULL)
993 return (ENXIO);
994 error = ifc_name2unit(name, &unit);
995 if (error != 0) {
996 if_rele(p);
997 return (error);
998 }
999 vid = vlr.vlr_tag;
1000 wildcard = (unit < 0);
1001 } else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1002 unit = -1;
1003 wildcard = 0;
1004 } else {
1005 p = NULL;
1006 error = ifc_name2unit(name, &unit);
1007 if (error != 0)
1008 return (error);
1009
1010 wildcard = (unit < 0);
1011 }
1012
1013 error = ifc_alloc_unit(ifc, &unit);
1014 if (error != 0) {
1015 if (p != NULL)
1016 if_rele(p);
1017 return (error);
1018 }
1019
1020 /* In the wildcard case, we need to update the name. */
1021 if (wildcard) {
1022 for (dp = name; *dp != '\0'; dp++);
1023 if (snprintf(dp, len - (dp-name), "%d", unit) >
1024 len - (dp-name) - 1) {
1025 panic("%s: interface name too long", __func__);
1026 }
1027 }
1028
1029 ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1030 ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1031 if (ifp == NULL) {
1032 ifc_free_unit(ifc, unit);
1033 free(ifv, M_VLAN);
1034 if (p != NULL)
1035 if_rele(p);
1036 return (ENOSPC);
1037 }
1038 CK_SLIST_INIT(&ifv->vlan_mc_listhead);
1039 ifp->if_softc = ifv;
1040 /*
1041 * Set the name manually rather than using if_initname because
1042 * we don't conform to the default naming convention for interfaces.
1043 */
1044 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1045 ifp->if_dname = vlanname;
1046 ifp->if_dunit = unit;
1047
1048 ifp->if_init = vlan_init;
1049 ifp->if_transmit = vlan_transmit;
1050 ifp->if_qflush = vlan_qflush;
1051 ifp->if_ioctl = vlan_ioctl;
1052 #ifdef RATELIMIT
1053 ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1054 #endif
1055 ifp->if_flags = VLAN_IFFLAGS;
1056 ether_ifattach(ifp, eaddr);
1057 /* Now undo some of the damage... */
1058 ifp->if_baudrate = 0;
1059 ifp->if_type = IFT_L2VLAN;
1060 ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1061 ifa = ifp->if_addr;
1062 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1063 sdl->sdl_type = IFT_L2VLAN;
1064
1065 if (p != NULL) {
1066 error = vlan_config(ifv, p, vid);
1067 if_rele(p);
1068 if (error != 0) {
1069 /*
1070 * Since we've partially failed, we need to back
1071 * out all the way, otherwise userland could get
1072 * confused. Thus, we destroy the interface.
1073 */
1074 ether_ifdetach(ifp);
1075 vlan_unconfig(ifp);
1076 if_free(ifp);
1077 ifc_free_unit(ifc, unit);
1078 free(ifv, M_VLAN);
1079
1080 return (error);
1081 }
1082 }
1083
1084 return (0);
1085 }
1086
1087 static int
vlan_clone_destroy(struct if_clone * ifc,struct ifnet * ifp)1088 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
1089 {
1090 struct ifvlan *ifv = ifp->if_softc;
1091 int unit = ifp->if_dunit;
1092
1093 ether_ifdetach(ifp); /* first, remove it from system-wide lists */
1094 vlan_unconfig(ifp); /* now it can be unconfigured and freed */
1095 /*
1096 * We should have the only reference to the ifv now, so we can now
1097 * drain any remaining lladdr task before freeing the ifnet and the
1098 * ifvlan.
1099 */
1100 taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1101 NET_EPOCH_WAIT();
1102 if_free(ifp);
1103 free(ifv, M_VLAN);
1104 ifc_free_unit(ifc, unit);
1105
1106 return (0);
1107 }
1108
1109 /*
1110 * The ifp->if_init entry point for vlan(4) is a no-op.
1111 */
1112 static void
vlan_init(void * foo __unused)1113 vlan_init(void *foo __unused)
1114 {
1115 }
1116
1117 /*
1118 * The if_transmit method for vlan(4) interface.
1119 */
1120 static int
vlan_transmit(struct ifnet * ifp,struct mbuf * m)1121 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1122 {
1123 struct ifvlan *ifv;
1124 struct ifnet *p;
1125 int error, len, mcast;
1126
1127 VLAN_RLOCK();
1128 ifv = ifp->if_softc;
1129 if (TRUNK(ifv) == NULL) {
1130 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1131 VLAN_RUNLOCK();
1132 m_freem(m);
1133 return (ENETDOWN);
1134 }
1135 p = PARENT(ifv);
1136 len = m->m_pkthdr.len;
1137 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1138
1139 BPF_MTAP(ifp, m);
1140
1141 /*
1142 * Do not run parent's if_transmit() if the parent is not up,
1143 * or parent's driver will cause a system crash.
1144 */
1145 if (!UP_AND_RUNNING(p)) {
1146 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1147 VLAN_RUNLOCK();
1148 m_freem(m);
1149 return (ENETDOWN);
1150 }
1151
1152 if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1153 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1154 VLAN_RUNLOCK();
1155 return (0);
1156 }
1157
1158 /*
1159 * Send it, precisely as ether_output() would have.
1160 */
1161 error = (p->if_transmit)(p, m);
1162 if (error == 0) {
1163 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1164 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1165 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1166 } else
1167 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1168 VLAN_RUNLOCK();
1169 return (error);
1170 }
1171
1172 /*
1173 * The ifp->if_qflush entry point for vlan(4) is a no-op.
1174 */
1175 static void
vlan_qflush(struct ifnet * ifp __unused)1176 vlan_qflush(struct ifnet *ifp __unused)
1177 {
1178 }
1179
1180 static void
vlan_input(struct ifnet * ifp,struct mbuf * m)1181 vlan_input(struct ifnet *ifp, struct mbuf *m)
1182 {
1183 struct ifvlantrunk *trunk;
1184 struct ifvlan *ifv;
1185 struct m_tag *mtag;
1186 uint16_t vid, tag;
1187
1188 VLAN_RLOCK();
1189 trunk = ifp->if_vlantrunk;
1190 if (trunk == NULL) {
1191 VLAN_RUNLOCK();
1192 m_freem(m);
1193 return;
1194 }
1195
1196 if (m->m_flags & M_VLANTAG) {
1197 /*
1198 * Packet is tagged, but m contains a normal
1199 * Ethernet frame; the tag is stored out-of-band.
1200 */
1201 tag = m->m_pkthdr.ether_vtag;
1202 m->m_flags &= ~M_VLANTAG;
1203 } else {
1204 struct ether_vlan_header *evl;
1205
1206 /*
1207 * Packet is tagged in-band as specified by 802.1q.
1208 */
1209 switch (ifp->if_type) {
1210 case IFT_ETHER:
1211 if (m->m_len < sizeof(*evl) &&
1212 (m = m_pullup(m, sizeof(*evl))) == NULL) {
1213 if_printf(ifp, "cannot pullup VLAN header\n");
1214 VLAN_RUNLOCK();
1215 return;
1216 }
1217 evl = mtod(m, struct ether_vlan_header *);
1218 tag = ntohs(evl->evl_tag);
1219
1220 /*
1221 * Remove the 802.1q header by copying the Ethernet
1222 * addresses over it and adjusting the beginning of
1223 * the data in the mbuf. The encapsulated Ethernet
1224 * type field is already in place.
1225 */
1226 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1227 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1228 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1229 break;
1230
1231 default:
1232 #ifdef INVARIANTS
1233 panic("%s: %s has unsupported if_type %u",
1234 __func__, ifp->if_xname, ifp->if_type);
1235 #endif
1236 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1237 VLAN_RUNLOCK();
1238 m_freem(m);
1239 return;
1240 }
1241 }
1242
1243 vid = EVL_VLANOFTAG(tag);
1244
1245 ifv = vlan_gethash(trunk, vid);
1246 if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1247 VLAN_RUNLOCK();
1248 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1249 m_freem(m);
1250 return;
1251 }
1252
1253 if (vlan_mtag_pcp) {
1254 /*
1255 * While uncommon, it is possible that we will find a 802.1q
1256 * packet encapsulated inside another packet that also had an
1257 * 802.1q header. For example, ethernet tunneled over IPSEC
1258 * arriving over ethernet. In that case, we replace the
1259 * existing 802.1q PCP m_tag value.
1260 */
1261 mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1262 if (mtag == NULL) {
1263 mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1264 sizeof(uint8_t), M_NOWAIT);
1265 if (mtag == NULL) {
1266 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1267 VLAN_RUNLOCK();
1268 m_freem(m);
1269 return;
1270 }
1271 m_tag_prepend(m, mtag);
1272 }
1273 *(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1274 }
1275
1276 m->m_pkthdr.rcvif = ifv->ifv_ifp;
1277 if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1278 VLAN_RUNLOCK();
1279
1280 /* Pass it back through the parent's input routine. */
1281 (*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1282 }
1283
1284 static void
vlan_lladdr_fn(void * arg,int pending __unused)1285 vlan_lladdr_fn(void *arg, int pending __unused)
1286 {
1287 struct ifvlan *ifv;
1288 struct ifnet *ifp;
1289
1290 ifv = (struct ifvlan *)arg;
1291 ifp = ifv->ifv_ifp;
1292
1293 CURVNET_SET(ifp->if_vnet);
1294
1295 /* The ifv_ifp already has the lladdr copied in. */
1296 if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1297
1298 CURVNET_RESTORE();
1299 }
1300
1301 static int
vlan_config(struct ifvlan * ifv,struct ifnet * p,uint16_t vid)1302 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
1303 {
1304 struct ifvlantrunk *trunk;
1305 struct ifnet *ifp;
1306 int error = 0;
1307
1308 /*
1309 * We can handle non-ethernet hardware types as long as
1310 * they handle the tagging and headers themselves.
1311 */
1312 if (p->if_type != IFT_ETHER &&
1313 (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1314 return (EPROTONOSUPPORT);
1315 if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1316 return (EPROTONOSUPPORT);
1317 /*
1318 * Don't let the caller set up a VLAN VID with
1319 * anything except VLID bits.
1320 * VID numbers 0x0 and 0xFFF are reserved.
1321 */
1322 if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1323 return (EINVAL);
1324 if (ifv->ifv_trunk)
1325 return (EBUSY);
1326
1327 VLAN_XLOCK();
1328 if (p->if_vlantrunk == NULL) {
1329 trunk = malloc(sizeof(struct ifvlantrunk),
1330 M_VLAN, M_WAITOK | M_ZERO);
1331 vlan_inithash(trunk);
1332 TRUNK_LOCK_INIT(trunk);
1333 TRUNK_WLOCK(trunk);
1334 p->if_vlantrunk = trunk;
1335 trunk->parent = p;
1336 if_ref(trunk->parent);
1337 TRUNK_WUNLOCK(trunk);
1338 } else {
1339 trunk = p->if_vlantrunk;
1340 }
1341
1342 ifv->ifv_vid = vid; /* must set this before vlan_inshash() */
1343 ifv->ifv_pcp = 0; /* Default: best effort delivery. */
1344 vlan_tag_recalculate(ifv);
1345 error = vlan_inshash(trunk, ifv);
1346 if (error)
1347 goto done;
1348 ifv->ifv_proto = ETHERTYPE_VLAN;
1349 ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1350 ifv->ifv_mintu = ETHERMIN;
1351 ifv->ifv_pflags = 0;
1352 ifv->ifv_capenable = -1;
1353
1354 /*
1355 * If the parent supports the VLAN_MTU capability,
1356 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1357 * use it.
1358 */
1359 if (p->if_capenable & IFCAP_VLAN_MTU) {
1360 /*
1361 * No need to fudge the MTU since the parent can
1362 * handle extended frames.
1363 */
1364 ifv->ifv_mtufudge = 0;
1365 } else {
1366 /*
1367 * Fudge the MTU by the encapsulation size. This
1368 * makes us incompatible with strictly compliant
1369 * 802.1Q implementations, but allows us to use
1370 * the feature with other NetBSD implementations,
1371 * which might still be useful.
1372 */
1373 ifv->ifv_mtufudge = ifv->ifv_encaplen;
1374 }
1375
1376 ifv->ifv_trunk = trunk;
1377 ifp = ifv->ifv_ifp;
1378 /*
1379 * Initialize fields from our parent. This duplicates some
1380 * work with ether_ifattach() but allows for non-ethernet
1381 * interfaces to also work.
1382 */
1383 ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1384 ifp->if_baudrate = p->if_baudrate;
1385 ifp->if_output = p->if_output;
1386 ifp->if_input = p->if_input;
1387 ifp->if_resolvemulti = p->if_resolvemulti;
1388 ifp->if_addrlen = p->if_addrlen;
1389 ifp->if_broadcastaddr = p->if_broadcastaddr;
1390 ifp->if_pcp = ifv->ifv_pcp;
1391
1392 /*
1393 * Copy only a selected subset of flags from the parent.
1394 * Other flags are none of our business.
1395 */
1396 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1397 ifp->if_flags &= ~VLAN_COPY_FLAGS;
1398 ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1399 #undef VLAN_COPY_FLAGS
1400
1401 ifp->if_link_state = p->if_link_state;
1402
1403 TRUNK_RLOCK(TRUNK(ifv));
1404 vlan_capabilities(ifv);
1405 TRUNK_RUNLOCK(TRUNK(ifv));
1406
1407 /*
1408 * Set up our interface address to reflect the underlying
1409 * physical interface's.
1410 */
1411 bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1412 ((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1413 p->if_addrlen;
1414
1415 TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1416
1417 /* We are ready for operation now. */
1418 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1419
1420 /* Update flags on the parent, if necessary. */
1421 vlan_setflags(ifp, 1);
1422
1423 /*
1424 * Configure multicast addresses that may already be
1425 * joined on the vlan device.
1426 */
1427 (void)vlan_setmulti(ifp);
1428
1429 done:
1430 if (error == 0)
1431 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1432 VLAN_XUNLOCK();
1433
1434 return (error);
1435 }
1436
1437 static void
vlan_unconfig(struct ifnet * ifp)1438 vlan_unconfig(struct ifnet *ifp)
1439 {
1440
1441 VLAN_XLOCK();
1442 vlan_unconfig_locked(ifp, 0);
1443 VLAN_XUNLOCK();
1444 }
1445
1446 static void
vlan_unconfig_locked(struct ifnet * ifp,int departing)1447 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1448 {
1449 struct ifvlantrunk *trunk;
1450 struct vlan_mc_entry *mc;
1451 struct ifvlan *ifv;
1452 struct ifnet *parent;
1453 int error;
1454
1455 VLAN_XLOCK_ASSERT();
1456
1457 ifv = ifp->if_softc;
1458 trunk = ifv->ifv_trunk;
1459 parent = NULL;
1460
1461 if (trunk != NULL) {
1462 parent = trunk->parent;
1463
1464 /*
1465 * Since the interface is being unconfigured, we need to
1466 * empty the list of multicast groups that we may have joined
1467 * while we were alive from the parent's list.
1468 */
1469 while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1470 /*
1471 * If the parent interface is being detached,
1472 * all its multicast addresses have already
1473 * been removed. Warn about errors if
1474 * if_delmulti() does fail, but don't abort as
1475 * all callers expect vlan destruction to
1476 * succeed.
1477 */
1478 if (!departing) {
1479 error = if_delmulti(parent,
1480 (struct sockaddr *)&mc->mc_addr);
1481 if (error)
1482 if_printf(ifp,
1483 "Failed to delete multicast address from parent: %d\n",
1484 error);
1485 }
1486 CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1487 epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
1488 }
1489
1490 vlan_setflags(ifp, 0); /* clear special flags on parent */
1491
1492 vlan_remhash(trunk, ifv);
1493 ifv->ifv_trunk = NULL;
1494
1495 /*
1496 * Check if we were the last.
1497 */
1498 if (trunk->refcnt == 0) {
1499 parent->if_vlantrunk = NULL;
1500 NET_EPOCH_WAIT();
1501 trunk_destroy(trunk);
1502 }
1503 }
1504
1505 /* Disconnect from parent. */
1506 if (ifv->ifv_pflags)
1507 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1508 ifp->if_mtu = ETHERMTU;
1509 ifp->if_link_state = LINK_STATE_UNKNOWN;
1510 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1511
1512 /*
1513 * Only dispatch an event if vlan was
1514 * attached, otherwise there is nothing
1515 * to cleanup anyway.
1516 */
1517 if (parent != NULL)
1518 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1519 }
1520
1521 /* Handle a reference counted flag that should be set on the parent as well */
1522 static int
vlan_setflag(struct ifnet * ifp,int flag,int status,int (* func)(struct ifnet *,int))1523 vlan_setflag(struct ifnet *ifp, int flag, int status,
1524 int (*func)(struct ifnet *, int))
1525 {
1526 struct ifvlan *ifv;
1527 int error;
1528
1529 VLAN_SXLOCK_ASSERT();
1530
1531 ifv = ifp->if_softc;
1532 status = status ? (ifp->if_flags & flag) : 0;
1533 /* Now "status" contains the flag value or 0 */
1534
1535 /*
1536 * See if recorded parent's status is different from what
1537 * we want it to be. If it is, flip it. We record parent's
1538 * status in ifv_pflags so that we won't clear parent's flag
1539 * we haven't set. In fact, we don't clear or set parent's
1540 * flags directly, but get or release references to them.
1541 * That's why we can be sure that recorded flags still are
1542 * in accord with actual parent's flags.
1543 */
1544 if (status != (ifv->ifv_pflags & flag)) {
1545 error = (*func)(PARENT(ifv), status);
1546 if (error)
1547 return (error);
1548 ifv->ifv_pflags &= ~flag;
1549 ifv->ifv_pflags |= status;
1550 }
1551 return (0);
1552 }
1553
1554 /*
1555 * Handle IFF_* flags that require certain changes on the parent:
1556 * if "status" is true, update parent's flags respective to our if_flags;
1557 * if "status" is false, forcedly clear the flags set on parent.
1558 */
1559 static int
vlan_setflags(struct ifnet * ifp,int status)1560 vlan_setflags(struct ifnet *ifp, int status)
1561 {
1562 int error, i;
1563
1564 for (i = 0; vlan_pflags[i].flag; i++) {
1565 error = vlan_setflag(ifp, vlan_pflags[i].flag,
1566 status, vlan_pflags[i].func);
1567 if (error)
1568 return (error);
1569 }
1570 return (0);
1571 }
1572
1573 /* Inform all vlans that their parent has changed link state */
1574 static void
vlan_link_state(struct ifnet * ifp)1575 vlan_link_state(struct ifnet *ifp)
1576 {
1577 struct ifvlantrunk *trunk;
1578 struct ifvlan *ifv;
1579
1580 /* Called from a taskqueue_swi task, so we cannot sleep. */
1581 VLAN_RLOCK();
1582 trunk = ifp->if_vlantrunk;
1583 if (trunk == NULL) {
1584 VLAN_RUNLOCK();
1585 return;
1586 }
1587
1588 TRUNK_WLOCK(trunk);
1589 VLAN_FOREACH(ifv, trunk) {
1590 ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1591 if_link_state_change(ifv->ifv_ifp,
1592 trunk->parent->if_link_state);
1593 }
1594 TRUNK_WUNLOCK(trunk);
1595 VLAN_RUNLOCK();
1596 }
1597
1598 static void
vlan_capabilities(struct ifvlan * ifv)1599 vlan_capabilities(struct ifvlan *ifv)
1600 {
1601 struct ifnet *p;
1602 struct ifnet *ifp;
1603 struct ifnet_hw_tsomax hw_tsomax;
1604 int cap = 0, ena = 0, mena;
1605 u_long hwa = 0;
1606
1607 VLAN_SXLOCK_ASSERT();
1608 TRUNK_RLOCK_ASSERT(TRUNK(ifv));
1609 p = PARENT(ifv);
1610 ifp = ifv->ifv_ifp;
1611
1612 /* Mask parent interface enabled capabilities disabled by user. */
1613 mena = p->if_capenable & ifv->ifv_capenable;
1614
1615 /*
1616 * If the parent interface can do checksum offloading
1617 * on VLANs, then propagate its hardware-assisted
1618 * checksumming flags. Also assert that checksum
1619 * offloading requires hardware VLAN tagging.
1620 */
1621 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1622 cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1623 if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1624 p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1625 ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1626 if (ena & IFCAP_TXCSUM)
1627 hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1628 CSUM_UDP | CSUM_SCTP);
1629 if (ena & IFCAP_TXCSUM_IPV6)
1630 hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1631 CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
1632 }
1633
1634 /*
1635 * If the parent interface can do TSO on VLANs then
1636 * propagate the hardware-assisted flag. TSO on VLANs
1637 * does not necessarily require hardware VLAN tagging.
1638 */
1639 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
1640 if_hw_tsomax_common(p, &hw_tsomax);
1641 if_hw_tsomax_update(ifp, &hw_tsomax);
1642 if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1643 cap |= p->if_capabilities & IFCAP_TSO;
1644 if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1645 ena |= mena & IFCAP_TSO;
1646 if (ena & IFCAP_TSO)
1647 hwa |= p->if_hwassist & CSUM_TSO;
1648 }
1649
1650 /*
1651 * If the parent interface can do LRO and checksum offloading on
1652 * VLANs, then guess it may do LRO on VLANs. False positive here
1653 * cost nothing, while false negative may lead to some confusions.
1654 */
1655 if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1656 cap |= p->if_capabilities & IFCAP_LRO;
1657 if (p->if_capenable & IFCAP_VLAN_HWCSUM)
1658 ena |= p->if_capenable & IFCAP_LRO;
1659
1660 /*
1661 * If the parent interface can offload TCP connections over VLANs then
1662 * propagate its TOE capability to the VLAN interface.
1663 *
1664 * All TOE drivers in the tree today can deal with VLANs. If this
1665 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1666 * with its own bit.
1667 */
1668 #define IFCAP_VLAN_TOE IFCAP_TOE
1669 if (p->if_capabilities & IFCAP_VLAN_TOE)
1670 cap |= p->if_capabilities & IFCAP_TOE;
1671 if (p->if_capenable & IFCAP_VLAN_TOE) {
1672 TOEDEV(ifp) = TOEDEV(p);
1673 ena |= mena & IFCAP_TOE;
1674 }
1675
1676 /*
1677 * If the parent interface supports dynamic link state, so does the
1678 * VLAN interface.
1679 */
1680 cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1681 ena |= (mena & IFCAP_LINKSTATE);
1682
1683 #ifdef RATELIMIT
1684 /*
1685 * If the parent interface supports ratelimiting, so does the
1686 * VLAN interface.
1687 */
1688 cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1689 ena |= (mena & IFCAP_TXRTLMT);
1690 #endif
1691
1692 ifp->if_capabilities = cap;
1693 ifp->if_capenable = ena;
1694 ifp->if_hwassist = hwa;
1695 }
1696
1697 static void
vlan_trunk_capabilities(struct ifnet * ifp)1698 vlan_trunk_capabilities(struct ifnet *ifp)
1699 {
1700 struct ifvlantrunk *trunk;
1701 struct ifvlan *ifv;
1702
1703 VLAN_SLOCK();
1704 trunk = ifp->if_vlantrunk;
1705 if (trunk == NULL) {
1706 VLAN_SUNLOCK();
1707 return;
1708 }
1709 TRUNK_RLOCK(trunk);
1710 VLAN_FOREACH(ifv, trunk) {
1711 vlan_capabilities(ifv);
1712 }
1713 TRUNK_RUNLOCK(trunk);
1714 VLAN_SUNLOCK();
1715 }
1716
1717 static int
vlan_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)1718 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1719 {
1720 struct ifnet *p;
1721 struct ifreq *ifr;
1722 struct ifaddr *ifa;
1723 struct ifvlan *ifv;
1724 struct ifvlantrunk *trunk;
1725 struct vlanreq vlr;
1726 int error = 0;
1727
1728 ifr = (struct ifreq *)data;
1729 ifa = (struct ifaddr *) data;
1730 ifv = ifp->if_softc;
1731
1732 switch (cmd) {
1733 case SIOCSIFADDR:
1734 ifp->if_flags |= IFF_UP;
1735 #ifdef INET
1736 if (ifa->ifa_addr->sa_family == AF_INET)
1737 arp_ifinit(ifp, ifa);
1738 #endif
1739 break;
1740 case SIOCGIFADDR:
1741 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
1742 ifp->if_addrlen);
1743 break;
1744 case SIOCGIFMEDIA:
1745 VLAN_SLOCK();
1746 if (TRUNK(ifv) != NULL) {
1747 p = PARENT(ifv);
1748 if_ref(p);
1749 error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1750 if_rele(p);
1751 /* Limit the result to the parent's current config. */
1752 if (error == 0) {
1753 struct ifmediareq *ifmr;
1754
1755 ifmr = (struct ifmediareq *)data;
1756 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1757 ifmr->ifm_count = 1;
1758 error = copyout(&ifmr->ifm_current,
1759 ifmr->ifm_ulist,
1760 sizeof(int));
1761 }
1762 }
1763 } else {
1764 error = EINVAL;
1765 }
1766 VLAN_SUNLOCK();
1767 break;
1768
1769 case SIOCSIFMEDIA:
1770 error = EINVAL;
1771 break;
1772
1773 case SIOCSIFMTU:
1774 /*
1775 * Set the interface MTU.
1776 */
1777 VLAN_SLOCK();
1778 trunk = TRUNK(ifv);
1779 if (trunk != NULL) {
1780 TRUNK_WLOCK(trunk);
1781 if (ifr->ifr_mtu >
1782 (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1783 ifr->ifr_mtu <
1784 (ifv->ifv_mintu - ifv->ifv_mtufudge))
1785 error = EINVAL;
1786 else
1787 ifp->if_mtu = ifr->ifr_mtu;
1788 TRUNK_WUNLOCK(trunk);
1789 } else
1790 error = EINVAL;
1791 VLAN_SUNLOCK();
1792 break;
1793
1794 case SIOCSETVLAN:
1795 #ifdef VIMAGE
1796 /*
1797 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
1798 * interface to be delegated to a jail without allowing the
1799 * jail to change what underlying interface/VID it is
1800 * associated with. We are not entirely convinced that this
1801 * is the right way to accomplish that policy goal.
1802 */
1803 if (ifp->if_vnet != ifp->if_home_vnet) {
1804 error = EPERM;
1805 break;
1806 }
1807 #endif
1808 error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
1809 if (error)
1810 break;
1811 if (vlr.vlr_parent[0] == '\0') {
1812 vlan_unconfig(ifp);
1813 break;
1814 }
1815 p = ifunit_ref(vlr.vlr_parent);
1816 if (p == NULL) {
1817 error = ENOENT;
1818 break;
1819 }
1820 error = vlan_config(ifv, p, vlr.vlr_tag);
1821 if_rele(p);
1822 break;
1823
1824 case SIOCGETVLAN:
1825 #ifdef VIMAGE
1826 if (ifp->if_vnet != ifp->if_home_vnet) {
1827 error = EPERM;
1828 break;
1829 }
1830 #endif
1831 bzero(&vlr, sizeof(vlr));
1832 VLAN_SLOCK();
1833 if (TRUNK(ifv) != NULL) {
1834 strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1835 sizeof(vlr.vlr_parent));
1836 vlr.vlr_tag = ifv->ifv_vid;
1837 }
1838 VLAN_SUNLOCK();
1839 error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
1840 break;
1841
1842 case SIOCSIFFLAGS:
1843 /*
1844 * We should propagate selected flags to the parent,
1845 * e.g., promiscuous mode.
1846 */
1847 VLAN_XLOCK();
1848 if (TRUNK(ifv) != NULL)
1849 error = vlan_setflags(ifp, 1);
1850 VLAN_XUNLOCK();
1851 break;
1852
1853 case SIOCADDMULTI:
1854 case SIOCDELMULTI:
1855 /*
1856 * If we don't have a parent, just remember the membership for
1857 * when we do.
1858 *
1859 * XXX We need the rmlock here to avoid sleeping while
1860 * holding in6_multi_mtx.
1861 */
1862 VLAN_XLOCK();
1863 trunk = TRUNK(ifv);
1864 if (trunk != NULL)
1865 error = vlan_setmulti(ifp);
1866 VLAN_XUNLOCK();
1867
1868 break;
1869 case SIOCGVLANPCP:
1870 #ifdef VIMAGE
1871 if (ifp->if_vnet != ifp->if_home_vnet) {
1872 error = EPERM;
1873 break;
1874 }
1875 #endif
1876 ifr->ifr_vlan_pcp = ifv->ifv_pcp;
1877 break;
1878
1879 case SIOCSVLANPCP:
1880 #ifdef VIMAGE
1881 if (ifp->if_vnet != ifp->if_home_vnet) {
1882 error = EPERM;
1883 break;
1884 }
1885 #endif
1886 error = priv_check(curthread, PRIV_NET_SETVLANPCP);
1887 if (error)
1888 break;
1889 if (ifr->ifr_vlan_pcp > 7) {
1890 error = EINVAL;
1891 break;
1892 }
1893 ifv->ifv_pcp = ifr->ifr_vlan_pcp;
1894 ifp->if_pcp = ifv->ifv_pcp;
1895 vlan_tag_recalculate(ifv);
1896 /* broadcast event about PCP change */
1897 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
1898 break;
1899
1900 case SIOCSIFCAP:
1901 VLAN_SLOCK();
1902 ifv->ifv_capenable = ifr->ifr_reqcap;
1903 trunk = TRUNK(ifv);
1904 if (trunk != NULL) {
1905 TRUNK_RLOCK(trunk);
1906 vlan_capabilities(ifv);
1907 TRUNK_RUNLOCK(trunk);
1908 }
1909 VLAN_SUNLOCK();
1910 break;
1911
1912 default:
1913 error = EINVAL;
1914 break;
1915 }
1916
1917 return (error);
1918 }
1919
1920 #ifdef RATELIMIT
1921 static int
vlan_snd_tag_alloc(struct ifnet * ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** ppmt)1922 vlan_snd_tag_alloc(struct ifnet *ifp,
1923 union if_snd_tag_alloc_params *params,
1924 struct m_snd_tag **ppmt)
1925 {
1926
1927 /* get trunk device */
1928 ifp = vlan_trunkdev(ifp);
1929 if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1930 return (EOPNOTSUPP);
1931 /* forward allocation request */
1932 return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1933 }
1934 #endif
1935