1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Data-Link Services Module 28 */ 29 30 #include <sys/sysmacros.h> 31 #include <sys/strsubr.h> 32 #include <sys/strsun.h> 33 #include <sys/vlan.h> 34 #include <sys/dld_impl.h> 35 #include <sys/sdt.h> 36 #include <sys/atomic.h> 37 38 static kmem_cache_t *i_dls_link_cachep; 39 mod_hash_t *i_dls_link_hash; 40 static uint_t i_dls_link_count; 41 42 #define LINK_HASHSZ 67 /* prime */ 43 #define IMPL_HASHSZ 67 /* prime */ 44 45 /* 46 * Construct a hash key encompassing both DLSAP value and VLAN idenitifier. 47 */ 48 #define MAKE_KEY(_sap) \ 49 ((mod_hash_key_t)(uintptr_t)((_sap) << VLAN_ID_SIZE)) 50 51 #define DLS_STRIP_PADDING(pktsize, p) { \ 52 if (pktsize != 0) { \ 53 ssize_t delta = pktsize - msgdsize(p); \ 54 \ 55 if (delta < 0) \ 56 (void) adjmsg(p, delta); \ 57 } \ 58 } 59 60 /* 61 * Private functions. 62 */ 63 64 /*ARGSUSED*/ 65 static int 66 i_dls_link_constructor(void *buf, void *arg, int kmflag) 67 { 68 dls_link_t *dlp = buf; 69 char name[MAXNAMELEN]; 70 71 bzero(buf, sizeof (dls_link_t)); 72 73 (void) snprintf(name, MAXNAMELEN, "dls_link_t_%p_hash", buf); 74 dlp->dl_str_hash = mod_hash_create_idhash(name, IMPL_HASHSZ, 75 mod_hash_null_valdtor); 76 77 return (0); 78 } 79 80 /*ARGSUSED*/ 81 static void 82 i_dls_link_destructor(void *buf, void *arg) 83 { 84 dls_link_t *dlp = buf; 85 86 ASSERT(dlp->dl_ref == 0); 87 ASSERT(dlp->dl_mh == NULL); 88 ASSERT(dlp->dl_mah == NULL); 89 ASSERT(dlp->dl_unknowns == 0); 90 91 mod_hash_destroy_idhash(dlp->dl_str_hash); 92 dlp->dl_str_hash = NULL; 93 94 } 95 96 /* 97 * - Parse the mac header information of the given packet. 98 * - Strip the padding and skip over the header. Note that because some 99 * DLS consumers only check the db_ref count of the first mblk, we 100 * pullup the message into a single mblk. Because the original message 101 * is freed as the result of message pulling up, dls_link_header_info() 102 * is called again to update the mhi_saddr and mhi_daddr pointers in the 103 * mhip. Further, the dls_link_header_info() function ensures that the 104 * size of the pulled message is greater than the MAC header size, 105 * therefore we can directly advance b_rptr to point at the payload. 106 * 107 * We choose to use a macro for performance reasons. 108 */ 109 #define DLS_PREPARE_PKT(dlp, mp, mhip, err) { \ 110 mblk_t *nextp = (mp)->b_next; \ 111 if (((err) = dls_link_header_info((dlp), (mp), (mhip))) == 0) { \ 112 DLS_STRIP_PADDING((mhip)->mhi_pktsize, (mp)); \ 113 if (MBLKL((mp)) < (mhip)->mhi_hdrsize) { \ 114 mblk_t *newmp; \ 115 if ((newmp = msgpullup((mp), -1)) == NULL) { \ 116 (err) = EINVAL; \ 117 } else { \ 118 (mp)->b_next = NULL; \ 119 freemsg((mp)); \ 120 (mp) = newmp; \ 121 VERIFY(dls_link_header_info((dlp), \ 122 (mp), (mhip)) == 0); \ 123 (mp)->b_next = nextp; \ 124 (mp)->b_rptr += (mhip)->mhi_hdrsize; \ 125 } \ 126 } else { \ 127 (mp)->b_rptr += (mhip)->mhi_hdrsize; \ 128 } \ 129 } \ 130 } 131 132 /* 133 * Truncate the chain starting at mp such that all packets in the chain 134 * have identical source and destination addresses, saps, and tag types 135 * (see below). It returns a pointer to the mblk following the chain, 136 * NULL if there is no further packet following the processed chain. 137 * The countp argument is set to the number of valid packets in the chain. 138 * Note that the whole MAC header (including the VLAN tag if any) in each 139 * packet will be stripped. 140 */ 141 static mblk_t * 142 i_dls_link_subchain(dls_link_t *dlp, mblk_t *mp, const mac_header_info_t *mhip, 143 uint_t *countp) 144 { 145 mblk_t *prevp; 146 uint_t npacket = 1; 147 size_t addr_size = dlp->dl_mip->mi_addr_length; 148 uint16_t vid = VLAN_ID(mhip->mhi_tci); 149 uint16_t pri = VLAN_PRI(mhip->mhi_tci); 150 151 /* 152 * Compare with subsequent headers until we find one that has 153 * differing header information. After checking each packet 154 * strip padding and skip over the header. 155 */ 156 for (prevp = mp; (mp = mp->b_next) != NULL; prevp = mp) { 157 mac_header_info_t cmhi; 158 uint16_t cvid, cpri; 159 int err; 160 161 DLS_PREPARE_PKT(dlp, mp, &cmhi, err); 162 if (err != 0) 163 break; 164 165 prevp->b_next = mp; 166 167 /* 168 * The source, destination, sap, vlan id and the MSGNOLOOP 169 * flag must all match in a given subchain. 170 */ 171 if (memcmp(mhip->mhi_daddr, cmhi.mhi_daddr, addr_size) != 0 || 172 memcmp(mhip->mhi_saddr, cmhi.mhi_saddr, addr_size) != 0 || 173 mhip->mhi_bindsap != cmhi.mhi_bindsap) { 174 /* 175 * Note that we don't need to restore the padding. 176 */ 177 mp->b_rptr -= cmhi.mhi_hdrsize; 178 break; 179 } 180 181 cvid = VLAN_ID(cmhi.mhi_tci); 182 cpri = VLAN_PRI(cmhi.mhi_tci); 183 184 /* 185 * There are several types of packets. Packets don't match 186 * if they are classified to different type or if they are 187 * VLAN packets but belong to different VLANs: 188 * 189 * packet type tagged vid pri 190 * --------------------------------------------------------- 191 * untagged No zero zero 192 * VLAN packets Yes non-zero - 193 * priority tagged Yes zero non-zero 194 * 0 tagged Yes zero zero 195 */ 196 if ((mhip->mhi_istagged != cmhi.mhi_istagged) || 197 (vid != cvid) || ((vid == VLAN_ID_NONE) && 198 (((pri == 0) && (cpri != 0)) || 199 ((pri != 0) && (cpri == 0))))) { 200 mp->b_rptr -= cmhi.mhi_hdrsize; 201 break; 202 } 203 204 npacket++; 205 } 206 207 /* 208 * Break the chain at this point and return a pointer to the next 209 * sub-chain. 210 */ 211 prevp->b_next = NULL; 212 *countp = npacket; 213 return (mp); 214 } 215 216 /* ARGSUSED */ 217 static int 218 i_dls_head_hold(mod_hash_key_t key, mod_hash_val_t val) 219 { 220 dls_head_t *dhp = (dls_head_t *)val; 221 222 /* 223 * The lock order is mod_hash's internal lock -> dh_lock as in the 224 * call to i_dls_link_rx -> mod_hash_find_cb_rval -> i_dls_head_hold 225 */ 226 mutex_enter(&dhp->dh_lock); 227 if (dhp->dh_removing) { 228 mutex_exit(&dhp->dh_lock); 229 return (-1); 230 } 231 dhp->dh_ref++; 232 mutex_exit(&dhp->dh_lock); 233 return (0); 234 } 235 236 void 237 i_dls_head_rele(dls_head_t *dhp) 238 { 239 mutex_enter(&dhp->dh_lock); 240 dhp->dh_ref--; 241 if (dhp->dh_ref == 0 && dhp->dh_removing != 0) 242 cv_broadcast(&dhp->dh_cv); 243 mutex_exit(&dhp->dh_lock); 244 } 245 246 static dls_head_t * 247 i_dls_head_alloc(mod_hash_key_t key) 248 { 249 dls_head_t *dhp; 250 251 dhp = kmem_zalloc(sizeof (dls_head_t), KM_SLEEP); 252 dhp->dh_key = key; 253 return (dhp); 254 } 255 256 static void 257 i_dls_head_free(dls_head_t *dhp) 258 { 259 ASSERT(dhp->dh_ref == 0); 260 kmem_free(dhp, sizeof (dls_head_t)); 261 } 262 263 /* 264 * Try to send mp up to the streams of the given sap and vid. Return B_TRUE 265 * if this message is sent to any streams. 266 * Note that this function will copy the message chain and the original 267 * mp will remain valid after this function 268 */ 269 static uint_t 270 i_dls_link_rx_func(dls_link_t *dlp, mac_resource_handle_t mrh, 271 mac_header_info_t *mhip, mblk_t *mp, uint32_t sap, 272 boolean_t (*acceptfunc)()) 273 { 274 mod_hash_t *hash = dlp->dl_str_hash; 275 mod_hash_key_t key; 276 dls_head_t *dhp; 277 dld_str_t *dsp; 278 mblk_t *nmp; 279 dls_rx_t ds_rx; 280 void *ds_rx_arg; 281 uint_t naccepted = 0; 282 int rval; 283 284 /* 285 * Construct a hash key from the VLAN identifier and the 286 * DLSAP that represents dld_str_t in promiscuous mode. 287 */ 288 key = MAKE_KEY(sap); 289 290 /* 291 * Search the hash table for dld_str_t eligible to receive 292 * a packet chain for this DLSAP/VLAN combination. The mod hash's 293 * internal lock serializes find/insert/remove from the mod hash list. 294 * Incrementing the dh_ref (while holding the mod hash lock) ensures 295 * dls_link_remove will wait for the upcall to finish. 296 */ 297 if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp, 298 i_dls_head_hold, &rval) != 0 || (rval != 0)) { 299 return (B_FALSE); 300 } 301 302 /* 303 * Find dld_str_t that will accept the sub-chain. 304 */ 305 for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next) { 306 if (!acceptfunc(dsp, mhip, &ds_rx, &ds_rx_arg)) 307 continue; 308 309 /* 310 * We have at least one acceptor. 311 */ 312 naccepted++; 313 314 /* 315 * There will normally be at least more dld_str_t 316 * (since we've yet to check for non-promiscuous 317 * dld_str_t) so dup the sub-chain. 318 */ 319 if ((nmp = copymsgchain(mp)) != NULL) 320 ds_rx(ds_rx_arg, mrh, nmp, mhip); 321 } 322 323 /* 324 * Release the hold on the dld_str_t chain now that we have 325 * finished walking it. 326 */ 327 i_dls_head_rele(dhp); 328 return (naccepted); 329 } 330 331 /* ARGSUSED */ 332 void 333 i_dls_link_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp, 334 boolean_t loopback) 335 { 336 dls_link_t *dlp = arg; 337 mod_hash_t *hash = dlp->dl_str_hash; 338 mblk_t *nextp; 339 mac_header_info_t mhi; 340 dls_head_t *dhp; 341 dld_str_t *dsp; 342 dld_str_t *ndsp; 343 mblk_t *nmp; 344 mod_hash_key_t key; 345 uint_t npacket; 346 boolean_t accepted; 347 dls_rx_t ds_rx, nds_rx; 348 void *ds_rx_arg, *nds_rx_arg; 349 uint16_t vid; 350 int err, rval; 351 352 /* 353 * Walk the packet chain. 354 */ 355 for (; mp != NULL; mp = nextp) { 356 /* 357 * Wipe the accepted state. 358 */ 359 accepted = B_FALSE; 360 361 DLS_PREPARE_PKT(dlp, mp, &mhi, err); 362 if (err != 0) { 363 atomic_add_32(&(dlp->dl_unknowns), 1); 364 nextp = mp->b_next; 365 mp->b_next = NULL; 366 freemsg(mp); 367 continue; 368 } 369 370 /* 371 * Grab the longest sub-chain we can process as a single 372 * unit. 373 */ 374 nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket); 375 ASSERT(npacket != 0); 376 377 vid = VLAN_ID(mhi.mhi_tci); 378 379 if (mhi.mhi_istagged) { 380 /* 381 * If it is tagged traffic, send it upstream to 382 * all dld_str_t which are attached to the physical 383 * link and bound to SAP 0x8100. 384 */ 385 if (i_dls_link_rx_func(dlp, mrh, &mhi, mp, 386 ETHERTYPE_VLAN, dls_accept) > 0) { 387 accepted = B_TRUE; 388 } 389 390 /* 391 * Don't pass the packets up if they are tagged 392 * packets and: 393 * - their VID and priority are both zero and the 394 * original packet isn't using the PVID (invalid 395 * packets). 396 * - their sap is ETHERTYPE_VLAN and their VID is 397 * zero as they have already been sent upstreams. 398 */ 399 if ((vid == VLAN_ID_NONE && !mhi.mhi_ispvid && 400 VLAN_PRI(mhi.mhi_tci) == 0) || 401 (mhi.mhi_bindsap == ETHERTYPE_VLAN && 402 vid == VLAN_ID_NONE)) { 403 freemsgchain(mp); 404 goto loop; 405 } 406 } 407 408 /* 409 * Construct a hash key from the VLAN identifier and the 410 * DLSAP. 411 */ 412 key = MAKE_KEY(mhi.mhi_bindsap); 413 414 /* 415 * Search the has table for dld_str_t eligible to receive 416 * a packet chain for this DLSAP/VLAN combination. 417 */ 418 if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp, 419 i_dls_head_hold, &rval) != 0 || (rval != 0)) { 420 freemsgchain(mp); 421 goto loop; 422 } 423 424 /* 425 * Find the first dld_str_t that will accept the sub-chain. 426 */ 427 for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next) 428 if (dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg)) 429 break; 430 431 /* 432 * If we did not find any dld_str_t willing to accept the 433 * sub-chain then throw it away. 434 */ 435 if (dsp == NULL) { 436 i_dls_head_rele(dhp); 437 freemsgchain(mp); 438 goto loop; 439 } 440 441 /* 442 * We have at least one acceptor. 443 */ 444 accepted = B_TRUE; 445 for (;;) { 446 /* 447 * Find the next dld_str_t that will accept the 448 * sub-chain. 449 */ 450 for (ndsp = dsp->ds_next; ndsp != NULL; 451 ndsp = ndsp->ds_next) 452 if (dls_accept(ndsp, &mhi, &nds_rx, 453 &nds_rx_arg)) 454 break; 455 456 /* 457 * If there are no more dld_str_t that are willing 458 * to accept the sub-chain then we don't need to dup 459 * it before handing it to the current one. 460 */ 461 if (ndsp == NULL) { 462 ds_rx(ds_rx_arg, mrh, mp, &mhi); 463 464 /* 465 * Since there are no more dld_str_t, we're 466 * done. 467 */ 468 break; 469 } 470 471 /* 472 * There are more dld_str_t so dup the sub-chain. 473 */ 474 if ((nmp = copymsgchain(mp)) != NULL) 475 ds_rx(ds_rx_arg, mrh, nmp, &mhi); 476 477 dsp = ndsp; 478 ds_rx = nds_rx; 479 ds_rx_arg = nds_rx_arg; 480 } 481 482 /* 483 * Release the hold on the dld_str_t chain now that we have 484 * finished walking it. 485 */ 486 i_dls_head_rele(dhp); 487 488 loop: 489 /* 490 * If there were no acceptors then add the packet count to the 491 * 'unknown' count. 492 */ 493 if (!accepted) 494 atomic_add_32(&(dlp->dl_unknowns), npacket); 495 } 496 } 497 498 /* ARGSUSED */ 499 void 500 dls_rx_vlan_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp, 501 boolean_t loopback) 502 { 503 dld_str_t *dsp = arg; 504 dls_link_t *dlp = dsp->ds_dlp; 505 mac_header_info_t mhi; 506 dls_rx_t ds_rx; 507 void *ds_rx_arg; 508 int err; 509 510 DLS_PREPARE_PKT(dlp, mp, &mhi, err); 511 if (err != 0) 512 goto drop; 513 514 /* 515 * If there is promiscuous handle for vlan, we filter out the untagged 516 * pkts and pkts that are not for the primary unicast address. 517 */ 518 if (dsp->ds_vlan_mph != NULL) { 519 uint8_t prim_addr[MAXMACADDRLEN]; 520 size_t addr_length = dsp->ds_mip->mi_addr_length; 521 522 if (!(mhi.mhi_istagged)) 523 goto drop; 524 ASSERT(dsp->ds_mh != NULL); 525 mac_unicast_primary_get(dsp->ds_mh, (uint8_t *)prim_addr); 526 if (memcmp(mhi.mhi_daddr, prim_addr, addr_length) != 0) 527 goto drop; 528 529 if (!dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg)) 530 goto drop; 531 532 ds_rx(ds_rx_arg, NULL, mp, &mhi); 533 return; 534 } 535 536 drop: 537 atomic_add_32(&dlp->dl_unknowns, 1); 538 freemsg(mp); 539 } 540 541 /* ARGSUSED */ 542 void 543 dls_rx_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp, 544 boolean_t loopback) 545 { 546 dld_str_t *dsp = arg; 547 dls_link_t *dlp = dsp->ds_dlp; 548 mac_header_info_t mhi; 549 dls_rx_t ds_rx; 550 void *ds_rx_arg; 551 int err; 552 dls_head_t *dhp; 553 mod_hash_key_t key; 554 555 DLS_PREPARE_PKT(dlp, mp, &mhi, err); 556 if (err != 0) 557 goto drop; 558 559 /* 560 * In order to filter out sap pkt that no dls channel listens, search 561 * the hash table trying to find a dld_str_t eligible to receive the pkt 562 */ 563 if ((dsp->ds_promisc & DLS_PROMISC_SAP) == 0) { 564 key = MAKE_KEY(mhi.mhi_bindsap); 565 if (mod_hash_find(dsp->ds_dlp->dl_str_hash, key, 566 (mod_hash_val_t *)&dhp) != 0) 567 goto drop; 568 } 569 570 if (!dls_accept_promisc(dsp, &mhi, &ds_rx, &ds_rx_arg, loopback)) 571 goto drop; 572 573 ds_rx(ds_rx_arg, NULL, mp, &mhi); 574 return; 575 576 drop: 577 atomic_add_32(&dlp->dl_unknowns, 1); 578 freemsg(mp); 579 } 580 581 static void 582 i_dls_link_destroy(dls_link_t *dlp) 583 { 584 ASSERT(dlp->dl_nactive == 0); 585 ASSERT(dlp->dl_impl_count == 0); 586 ASSERT(dlp->dl_zone_ref == 0); 587 588 /* 589 * Free the structure back to the cache. 590 */ 591 if (dlp->dl_mch != NULL) 592 mac_client_close(dlp->dl_mch, 0); 593 594 if (dlp->dl_mh != NULL) { 595 ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 596 mac_close(dlp->dl_mh); 597 } 598 599 dlp->dl_mh = NULL; 600 dlp->dl_mch = NULL; 601 dlp->dl_mip = NULL; 602 dlp->dl_unknowns = 0; 603 kmem_cache_free(i_dls_link_cachep, dlp); 604 } 605 606 static int 607 i_dls_link_create(const char *name, dls_link_t **dlpp) 608 { 609 dls_link_t *dlp; 610 int err; 611 612 /* 613 * Allocate a new dls_link_t structure. 614 */ 615 dlp = kmem_cache_alloc(i_dls_link_cachep, KM_SLEEP); 616 617 /* 618 * Name the dls_link_t after the MAC interface it represents. 619 */ 620 (void) strlcpy(dlp->dl_name, name, sizeof (dlp->dl_name)); 621 622 /* 623 * First reference; hold open the MAC interface. 624 */ 625 ASSERT(dlp->dl_mh == NULL); 626 err = mac_open(dlp->dl_name, &dlp->dl_mh); 627 if (err != 0) 628 goto bail; 629 630 ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 631 dlp->dl_mip = mac_info(dlp->dl_mh); 632 633 /* DLS is the "primary" MAC client */ 634 ASSERT(dlp->dl_mch == NULL); 635 636 err = mac_client_open(dlp->dl_mh, &dlp->dl_mch, NULL, 637 MAC_OPEN_FLAGS_USE_DATALINK_NAME); 638 if (err != 0) 639 goto bail; 640 641 DTRACE_PROBE2(dls__primary__client, char *, dlp->dl_name, void *, 642 dlp->dl_mch); 643 644 *dlpp = dlp; 645 return (0); 646 647 bail: 648 i_dls_link_destroy(dlp); 649 return (err); 650 } 651 652 /* 653 * Module initialization functions. 654 */ 655 656 void 657 dls_link_init(void) 658 { 659 /* 660 * Create a kmem_cache of dls_link_t structures. 661 */ 662 i_dls_link_cachep = kmem_cache_create("dls_link_cache", 663 sizeof (dls_link_t), 0, i_dls_link_constructor, 664 i_dls_link_destructor, NULL, NULL, NULL, 0); 665 ASSERT(i_dls_link_cachep != NULL); 666 667 /* 668 * Create a dls_link_t hash table and associated lock. 669 */ 670 i_dls_link_hash = mod_hash_create_extended("dls_link_hash", 671 IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor, 672 mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP); 673 i_dls_link_count = 0; 674 } 675 676 int 677 dls_link_fini(void) 678 { 679 if (i_dls_link_count > 0) 680 return (EBUSY); 681 682 /* 683 * Destroy the kmem_cache. 684 */ 685 kmem_cache_destroy(i_dls_link_cachep); 686 687 /* 688 * Destroy the hash table and associated lock. 689 */ 690 mod_hash_destroy_hash(i_dls_link_hash); 691 return (0); 692 } 693 694 /* 695 * Exported functions. 696 */ 697 698 static int 699 dls_link_hold_common(const char *name, dls_link_t **dlpp, boolean_t create) 700 { 701 dls_link_t *dlp; 702 int err; 703 704 /* 705 * Look up a dls_link_t corresponding to the given macname in the 706 * global hash table. The i_dls_link_hash itself is protected by the 707 * mod_hash package's internal lock which synchronizes 708 * find/insert/remove into the global mod_hash list. Assumes that 709 * inserts and removes are single threaded on a per mac end point 710 * by the mac perimeter. 711 */ 712 if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name, 713 (mod_hash_val_t *)&dlp)) == 0) 714 goto done; 715 716 ASSERT(err == MH_ERR_NOTFOUND); 717 if (!create) 718 return (ENOENT); 719 720 /* 721 * We didn't find anything so we need to create one. 722 */ 723 if ((err = i_dls_link_create(name, &dlp)) != 0) 724 return (err); 725 726 /* 727 * Insert the dls_link_t. 728 */ 729 err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)dlp->dl_name, 730 (mod_hash_val_t)dlp); 731 ASSERT(err == 0); 732 733 atomic_add_32(&i_dls_link_count, 1); 734 ASSERT(i_dls_link_count != 0); 735 736 done: 737 ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 738 /* 739 * Bump the reference count and hand back the reference. 740 */ 741 dlp->dl_ref++; 742 *dlpp = dlp; 743 return (0); 744 } 745 746 int 747 dls_link_hold_create(const char *name, dls_link_t **dlpp) 748 { 749 return (dls_link_hold_common(name, dlpp, B_TRUE)); 750 } 751 752 int 753 dls_link_hold(const char *name, dls_link_t **dlpp) 754 { 755 return (dls_link_hold_common(name, dlpp, B_FALSE)); 756 } 757 758 dev_info_t * 759 dls_link_devinfo(dev_t dev) 760 { 761 dls_link_t *dlp; 762 dev_info_t *dip; 763 char macname[MAXNAMELEN]; 764 char *drv; 765 mac_perim_handle_t mph; 766 767 if ((drv = ddi_major_to_name(getmajor(dev))) == NULL) 768 return (NULL); 769 (void) snprintf(macname, MAXNAMELEN, "%s%d", drv, getminor(dev) - 1); 770 771 /* 772 * The code below assumes that the name constructed above is the 773 * macname. This is not the case for legacy devices. Currently this 774 * is ok because this function is only called in the getinfo(9e) path, 775 * which for a legacy device would directly end up in the driver's 776 * getinfo, rather than here 777 */ 778 if (mac_perim_enter_by_macname(macname, &mph) != 0) 779 return (NULL); 780 781 if (dls_link_hold(macname, &dlp) != 0) { 782 mac_perim_exit(mph); 783 return (NULL); 784 } 785 786 dip = mac_devinfo_get(dlp->dl_mh); 787 dls_link_rele(dlp); 788 mac_perim_exit(mph); 789 790 return (dip); 791 } 792 793 dev_t 794 dls_link_dev(dls_link_t *dlp) 795 { 796 return (makedevice(ddi_driver_major(mac_devinfo_get(dlp->dl_mh)), 797 mac_minor(dlp->dl_mh))); 798 } 799 800 void 801 dls_link_rele(dls_link_t *dlp) 802 { 803 mod_hash_val_t val; 804 805 ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 806 /* 807 * Check if there are any more references. 808 */ 809 if (--dlp->dl_ref == 0) { 810 (void) mod_hash_remove(i_dls_link_hash, 811 (mod_hash_key_t)dlp->dl_name, &val); 812 ASSERT(dlp == (dls_link_t *)val); 813 814 /* 815 * Destroy the dls_link_t. 816 */ 817 i_dls_link_destroy(dlp); 818 ASSERT(i_dls_link_count > 0); 819 atomic_add_32(&i_dls_link_count, -1); 820 } 821 } 822 823 int 824 dls_link_rele_by_name(const char *name) 825 { 826 dls_link_t *dlp; 827 828 if (mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name, 829 (mod_hash_val_t *)&dlp) != 0) 830 return (ENOENT); 831 832 ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 833 834 /* 835 * Must fail detach if mac client is busy. 836 */ 837 ASSERT(dlp->dl_ref > 0 && dlp->dl_mch != NULL); 838 if (mac_link_has_flows(dlp->dl_mch)) 839 return (ENOTEMPTY); 840 841 dls_link_rele(dlp); 842 return (0); 843 } 844 845 int 846 dls_link_setzid(const char *name, zoneid_t zid) 847 { 848 dls_link_t *dlp; 849 int err = 0; 850 zoneid_t old_zid; 851 852 if ((err = dls_link_hold_create(name, &dlp)) != 0) 853 return (err); 854 855 ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 856 857 if ((old_zid = dlp->dl_zid) == zid) 858 goto done; 859 860 /* 861 * Check whether this dlp is used by its own zone. If yes, we cannot 862 * change its zoneid. 863 */ 864 if (dlp->dl_zone_ref != 0) { 865 err = EBUSY; 866 goto done; 867 } 868 869 dlp->dl_zid = zid; 870 871 if (zid == GLOBAL_ZONEID) { 872 /* 873 * The link is moving from a non-global zone to the global 874 * zone, so we need to release the reference that was held 875 * when the link was originally assigned to the non-global 876 * zone. 877 */ 878 dls_link_rele(dlp); 879 } 880 881 done: 882 /* 883 * We only keep the reference to this link open if the link has 884 * successfully moved from the global zone to a non-global zone. 885 */ 886 if (err != 0 || old_zid != GLOBAL_ZONEID) 887 dls_link_rele(dlp); 888 return (err); 889 } 890 891 void 892 dls_link_add(dls_link_t *dlp, uint32_t sap, dld_str_t *dsp) 893 { 894 mod_hash_t *hash = dlp->dl_str_hash; 895 mod_hash_key_t key; 896 dls_head_t *dhp; 897 dld_str_t *p; 898 int err; 899 900 ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 901 902 /* 903 * Generate a hash key based on the sap. 904 */ 905 key = MAKE_KEY(sap); 906 907 /* 908 * Search the table for a list head with this key. 909 */ 910 if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) { 911 ASSERT(err == MH_ERR_NOTFOUND); 912 913 dhp = i_dls_head_alloc(key); 914 err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp); 915 ASSERT(err == 0); 916 } 917 918 /* 919 * Add the dld_str_t to the head of the list. List walkers in 920 * i_dls_link_rx_* bump up dh_ref to ensure the list does not change 921 * while they walk the list. The membar below ensures that list walkers 922 * see exactly the old list or the new list. 923 */ 924 ASSERT(dsp->ds_next == NULL); 925 p = dhp->dh_list; 926 dsp->ds_next = p; 927 928 membar_producer(); 929 930 dhp->dh_list = dsp; 931 932 /* 933 * Save a pointer to the list head. 934 */ 935 dsp->ds_head = dhp; 936 dlp->dl_impl_count++; 937 } 938 939 void 940 dls_link_remove(dls_link_t *dlp, dld_str_t *dsp) 941 { 942 mod_hash_t *hash = dlp->dl_str_hash; 943 dld_str_t **pp; 944 dld_str_t *p; 945 dls_head_t *dhp; 946 947 ASSERT(MAC_PERIM_HELD(dlp->dl_mh)); 948 949 /* 950 * We set dh_removing here to tell the receive callbacks not to pass 951 * up packets anymore. Then wait till the current callbacks are done. 952 * This happens either in the close path or in processing the 953 * DL_UNBIND_REQ via a taskq thread, and it is ok to cv_wait in either. 954 * The dh_ref ensures there aren't and there won't be any upcalls 955 * walking or using the dh_list. The mod hash internal lock ensures 956 * that the insert/remove of the dls_head_t itself synchronizes with 957 * any i_dls_link_rx trying to locate it. The perimeter ensures that 958 * there isn't another simultaneous dls_link_add/remove. 959 */ 960 dhp = dsp->ds_head; 961 962 mutex_enter(&dhp->dh_lock); 963 dhp->dh_removing = B_TRUE; 964 while (dhp->dh_ref != 0) 965 cv_wait(&dhp->dh_cv, &dhp->dh_lock); 966 mutex_exit(&dhp->dh_lock); 967 968 /* 969 * Walk the list and remove the dld_str_t. 970 */ 971 for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->ds_next)) { 972 if (p == dsp) 973 break; 974 } 975 ASSERT(p != NULL); 976 *pp = p->ds_next; 977 p->ds_next = NULL; 978 p->ds_head = NULL; 979 980 ASSERT(dlp->dl_impl_count != 0); 981 dlp->dl_impl_count--; 982 983 if (dhp->dh_list == NULL) { 984 mod_hash_val_t val = NULL; 985 986 /* 987 * The list is empty so remove the hash table entry. 988 */ 989 (void) mod_hash_remove(hash, dhp->dh_key, &val); 990 ASSERT(dhp == (dls_head_t *)val); 991 i_dls_head_free(dhp); 992 } else { 993 mutex_enter(&dhp->dh_lock); 994 dhp->dh_removing = B_FALSE; 995 mutex_exit(&dhp->dh_lock); 996 } 997 } 998 999 int 1000 dls_link_header_info(dls_link_t *dlp, mblk_t *mp, mac_header_info_t *mhip) 1001 { 1002 boolean_t is_ethernet = (dlp->dl_mip->mi_media == DL_ETHER); 1003 uint16_t pvid = mac_get_pvid(dlp->dl_mh); 1004 int err = 0; 1005 1006 /* 1007 * Packets should always be at least 16 bit aligned. 1008 */ 1009 ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t))); 1010 1011 if ((err = mac_header_info(dlp->dl_mh, mp, mhip)) != 0) 1012 return (err); 1013 1014 /* 1015 * If this is a VLAN-tagged Ethernet packet, then the SAP in the 1016 * mac_header_info_t as returned by mac_header_info() is 1017 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header. 1018 */ 1019 mhip->mhi_ispvid = B_FALSE; 1020 if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) { 1021 struct ether_vlan_header *evhp; 1022 uint16_t sap; 1023 mblk_t *tmp = NULL; 1024 size_t size; 1025 1026 size = sizeof (struct ether_vlan_header); 1027 if (MBLKL(mp) < size) { 1028 /* 1029 * Pullup the message in order to get the MAC header 1030 * infomation. Note that this is a read-only function, 1031 * we keep the input packet intact. 1032 */ 1033 if ((tmp = msgpullup(mp, size)) == NULL) 1034 return (EINVAL); 1035 1036 mp = tmp; 1037 } 1038 evhp = (struct ether_vlan_header *)mp->b_rptr; 1039 sap = ntohs(evhp->ether_type); 1040 (void) mac_sap_verify(dlp->dl_mh, sap, &mhip->mhi_bindsap); 1041 mhip->mhi_hdrsize = sizeof (struct ether_vlan_header); 1042 mhip->mhi_tci = ntohs(evhp->ether_tci); 1043 mhip->mhi_istagged = B_TRUE; 1044 freemsg(tmp); 1045 1046 /* 1047 * If this port has a non-zero PVID, then we have to lie to the 1048 * caller about the VLAN ID. It's always zero on receive for 1049 * that VLAN. 1050 */ 1051 if (pvid != VLAN_ID_NONE && VLAN_ID(mhip->mhi_tci) == pvid) { 1052 mhip->mhi_tci &= ~(VLAN_ID_MASK << VLAN_ID_SHIFT); 1053 mhip->mhi_ispvid = B_TRUE; 1054 } 1055 1056 if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI) 1057 return (EINVAL); 1058 } else { 1059 mhip->mhi_istagged = B_FALSE; 1060 mhip->mhi_tci = 0; 1061 } 1062 1063 return (0); 1064 } 1065