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