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