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