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