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