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