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