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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2018 Joyent, Inc.
  24  */
  25 
  26 /*
  27  * MAC Services Module - misc utilities
  28  */
  29 
  30 #include <sys/types.h>
  31 #include <sys/mac.h>
  32 #include <sys/mac_impl.h>
  33 #include <sys/mac_client_priv.h>
  34 #include <sys/mac_client_impl.h>
  35 #include <sys/mac_soft_ring.h>
  36 #include <sys/strsubr.h>
  37 #include <sys/strsun.h>
  38 #include <sys/vlan.h>
  39 #include <sys/pattr.h>
  40 #include <sys/pci_tools.h>
  41 #include <inet/ip.h>
  42 #include <inet/ip_impl.h>
  43 #include <inet/ip6.h>
  44 #include <sys/vtrace.h>
  45 #include <sys/dlpi.h>
  46 #include <sys/sunndi.h>
  47 #include <inet/ipsec_impl.h>
  48 #include <inet/sadb.h>
  49 #include <inet/ipsecesp.h>
  50 #include <inet/ipsecah.h>
  51 
  52 /*
  53  * Copy an mblk, preserving its hardware checksum flags.
  54  */
  55 static mblk_t *
  56 mac_copymsg_cksum(mblk_t *mp)
  57 {
  58         mblk_t *mp1;
  59         uint32_t start, stuff, end, value, flags;
  60 
  61         mp1 = copymsg(mp);
  62         if (mp1 == NULL)
  63                 return (NULL);
  64 
  65         hcksum_retrieve(mp, NULL, NULL, &start, &stuff, &end, &value, &flags);
  66         (void) hcksum_assoc(mp1, NULL, NULL, start, stuff, end, value,
  67             flags, KM_NOSLEEP);
  68 
  69         return (mp1);
  70 }
  71 
  72 /*
  73  * Copy an mblk chain, presenting the hardware checksum flags of the
  74  * individual mblks.
  75  */
  76 mblk_t *
  77 mac_copymsgchain_cksum(mblk_t *mp)
  78 {
  79         mblk_t *nmp = NULL;
  80         mblk_t **nmpp = &nmp;
  81 
  82         for (; mp != NULL; mp = mp->b_next) {
  83                 if ((*nmpp = mac_copymsg_cksum(mp)) == NULL) {
  84                         freemsgchain(nmp);
  85                         return (NULL);
  86                 }
  87 
  88                 nmpp = &((*nmpp)->b_next);
  89         }
  90 
  91         return (nmp);
  92 }
  93 
  94 /*
  95  * Process the specified mblk chain for proper handling of hardware
  96  * checksum offload. This routine is invoked for loopback traffic
  97  * between MAC clients.
  98  * The function handles a NULL mblk chain passed as argument.
  99  */
 100 mblk_t *
 101 mac_fix_cksum(mblk_t *mp_chain)
 102 {
 103         mblk_t *mp, *prev = NULL, *new_chain = mp_chain, *mp1;
 104         uint32_t flags, start, stuff, end, value;
 105 
 106         for (mp = mp_chain; mp != NULL; prev = mp, mp = mp->b_next) {
 107                 uint16_t len;
 108                 uint32_t offset;
 109                 struct ether_header *ehp;
 110                 uint16_t sap;
 111 
 112                 hcksum_retrieve(mp, NULL, NULL, &start, &stuff, &end, &value,
 113                     &flags);
 114                 if (flags == 0)
 115                         continue;
 116 
 117                 /*
 118                  * Since the processing of checksum offload for loopback
 119                  * traffic requires modification of the packet contents,
 120                  * ensure sure that we are always modifying our own copy.
 121                  */
 122                 if (DB_REF(mp) > 1) {
 123                         mp1 = copymsg(mp);
 124                         if (mp1 == NULL)
 125                                 continue;
 126                         mp1->b_next = mp->b_next;
 127                         mp->b_next = NULL;
 128                         freemsg(mp);
 129                         if (prev != NULL)
 130                                 prev->b_next = mp1;
 131                         else
 132                                 new_chain = mp1;
 133                         mp = mp1;
 134                 }
 135 
 136                 /*
 137                  * Ethernet, and optionally VLAN header.
 138                  */
 139                 /* LINTED: improper alignment cast */
 140                 ehp = (struct ether_header *)mp->b_rptr;
 141                 if (ntohs(ehp->ether_type) == VLAN_TPID) {
 142                         struct ether_vlan_header *evhp;
 143 
 144                         ASSERT(MBLKL(mp) >= sizeof (struct ether_vlan_header));
 145                         /* LINTED: improper alignment cast */
 146                         evhp = (struct ether_vlan_header *)mp->b_rptr;
 147                         sap = ntohs(evhp->ether_type);
 148                         offset = sizeof (struct ether_vlan_header);
 149                 } else {
 150                         sap = ntohs(ehp->ether_type);
 151                         offset = sizeof (struct ether_header);
 152                 }
 153 
 154                 if (MBLKL(mp) <= offset) {
 155                         offset -= MBLKL(mp);
 156                         if (mp->b_cont == NULL) {
 157                                 /* corrupted packet, skip it */
 158                                 if (prev != NULL)
 159                                         prev->b_next = mp->b_next;
 160                                 else
 161                                         new_chain = mp->b_next;
 162                                 mp1 = mp->b_next;
 163                                 mp->b_next = NULL;
 164                                 freemsg(mp);
 165                                 mp = mp1;
 166                                 continue;
 167                         }
 168                         mp = mp->b_cont;
 169                 }
 170 
 171                 if (flags & (HCK_FULLCKSUM | HCK_IPV4_HDRCKSUM)) {
 172                         ipha_t *ipha = NULL;
 173 
 174                         /*
 175                          * In order to compute the full and header
 176                          * checksums, we need to find and parse
 177                          * the IP and/or ULP headers.
 178                          */
 179 
 180                         sap = (sap < ETHERTYPE_802_MIN) ? 0 : sap;
 181 
 182                         /*
 183                          * IP header.
 184                          */
 185                         if (sap != ETHERTYPE_IP)
 186                                 continue;
 187 
 188                         ASSERT(MBLKL(mp) >= offset + sizeof (ipha_t));
 189                         /* LINTED: improper alignment cast */
 190                         ipha = (ipha_t *)(mp->b_rptr + offset);
 191 
 192                         if (flags & HCK_FULLCKSUM) {
 193                                 ipaddr_t src, dst;
 194                                 uint32_t cksum;
 195                                 uint16_t *up;
 196                                 uint8_t proto;
 197 
 198                                 /*
 199                                  * Pointer to checksum field in ULP header.
 200                                  */
 201                                 proto = ipha->ipha_protocol;
 202                                 ASSERT(ipha->ipha_version_and_hdr_length ==
 203                                     IP_SIMPLE_HDR_VERSION);
 204 
 205                                 switch (proto) {
 206                                 case IPPROTO_TCP:
 207                                         /* LINTED: improper alignment cast */
 208                                         up = IPH_TCPH_CHECKSUMP(ipha,
 209                                             IP_SIMPLE_HDR_LENGTH);
 210                                         break;
 211 
 212                                 case IPPROTO_UDP:
 213                                         /* LINTED: improper alignment cast */
 214                                         up = IPH_UDPH_CHECKSUMP(ipha,
 215                                             IP_SIMPLE_HDR_LENGTH);
 216                                         break;
 217 
 218                                 default:
 219                                         cmn_err(CE_WARN, "mac_fix_cksum: "
 220                                             "unexpected protocol: %d", proto);
 221                                         continue;
 222                                 }
 223 
 224                                 /*
 225                                  * Pseudo-header checksum.
 226                                  */
 227                                 src = ipha->ipha_src;
 228                                 dst = ipha->ipha_dst;
 229                                 len = ntohs(ipha->ipha_length) -
 230                                     IP_SIMPLE_HDR_LENGTH;
 231 
 232                                 cksum = (dst >> 16) + (dst & 0xFFFF) +
 233                                     (src >> 16) + (src & 0xFFFF);
 234                                 cksum += htons(len);
 235 
 236                                 /*
 237                                  * The checksum value stored in the packet needs
 238                                  * to be correct. Compute it here.
 239                                  */
 240                                 *up = 0;
 241                                 cksum += (((proto) == IPPROTO_UDP) ?
 242                                     IP_UDP_CSUM_COMP : IP_TCP_CSUM_COMP);
 243                                 cksum = IP_CSUM(mp, IP_SIMPLE_HDR_LENGTH +
 244                                     offset, cksum);
 245                                 *(up) = (uint16_t)(cksum ? cksum : ~cksum);
 246 
 247                                 /*
 248                                  * Flag the packet so that it appears
 249                                  * that the checksum has already been
 250                                  * verified by the hardware.
 251                                  */
 252                                 flags &= ~HCK_FULLCKSUM;
 253                                 flags |= HCK_FULLCKSUM_OK;
 254                                 value = 0;
 255                         }
 256 
 257                         if (flags & HCK_IPV4_HDRCKSUM) {
 258                                 ASSERT(ipha != NULL);
 259                                 ipha->ipha_hdr_checksum =
 260                                     (uint16_t)ip_csum_hdr(ipha);
 261                                 flags &= ~HCK_IPV4_HDRCKSUM;
 262                                 flags |= HCK_IPV4_HDRCKSUM_OK;
 263 
 264                         }
 265                 }
 266 
 267                 if (flags & HCK_PARTIALCKSUM) {
 268                         uint16_t *up, partial, cksum;
 269                         uchar_t *ipp; /* ptr to beginning of IP header */
 270 
 271                         if (mp->b_cont != NULL) {
 272                                 mblk_t *mp1;
 273 
 274                                 mp1 = msgpullup(mp, offset + end);
 275                                 if (mp1 == NULL)
 276                                         continue;
 277                                 mp1->b_next = mp->b_next;
 278                                 mp->b_next = NULL;
 279                                 freemsg(mp);
 280                                 if (prev != NULL)
 281                                         prev->b_next = mp1;
 282                                 else
 283                                         new_chain = mp1;
 284                                 mp = mp1;
 285                         }
 286 
 287                         ipp = mp->b_rptr + offset;
 288                         /* LINTED: cast may result in improper alignment */
 289                         up = (uint16_t *)((uchar_t *)ipp + stuff);
 290                         partial = *up;
 291                         *up = 0;
 292 
 293                         cksum = IP_BCSUM_PARTIAL(mp->b_rptr + offset + start,
 294                             end - start, partial);
 295                         cksum = ~cksum;
 296                         *up = cksum ? cksum : ~cksum;
 297 
 298                         /*
 299                          * Since we already computed the whole checksum,
 300                          * indicate to the stack that it has already
 301                          * been verified by the hardware.
 302                          */
 303                         flags &= ~HCK_PARTIALCKSUM;
 304                         flags |= HCK_FULLCKSUM_OK;
 305                         value = 0;
 306                 }
 307 
 308                 (void) hcksum_assoc(mp, NULL, NULL, start, stuff, end,
 309                     value, flags, KM_NOSLEEP);
 310         }
 311 
 312         return (new_chain);
 313 }
 314 
 315 /*
 316  * Add VLAN tag to the specified mblk.
 317  */
 318 mblk_t *
 319 mac_add_vlan_tag(mblk_t *mp, uint_t pri, uint16_t vid)
 320 {
 321         mblk_t *hmp;
 322         struct ether_vlan_header *evhp;
 323         struct ether_header *ehp;
 324         uint32_t start, stuff, end, value, flags;
 325 
 326         ASSERT(pri != 0 || vid != 0);
 327 
 328         /*
 329          * Allocate an mblk for the new tagged ethernet header,
 330          * and copy the MAC addresses and ethertype from the
 331          * original header.
 332          */
 333 
 334         hmp = allocb(sizeof (struct ether_vlan_header), BPRI_MED);
 335         if (hmp == NULL) {
 336                 freemsg(mp);
 337                 return (NULL);
 338         }
 339 
 340         evhp = (struct ether_vlan_header *)hmp->b_rptr;
 341         ehp = (struct ether_header *)mp->b_rptr;
 342 
 343         bcopy(ehp, evhp, (ETHERADDRL * 2));
 344         evhp->ether_type = ehp->ether_type;
 345         evhp->ether_tpid = htons(ETHERTYPE_VLAN);
 346 
 347         hmp->b_wptr += sizeof (struct ether_vlan_header);
 348         mp->b_rptr += sizeof (struct ether_header);
 349 
 350         /*
 351          * Free the original message if it's now empty. Link the
 352          * rest of messages to the header message.
 353          */
 354         hcksum_retrieve(mp, NULL, NULL, &start, &stuff, &end, &value, &flags);
 355         (void) hcksum_assoc(hmp, NULL, NULL, start, stuff, end, value, flags,
 356             KM_NOSLEEP);
 357         if (MBLKL(mp) == 0) {
 358                 hmp->b_cont = mp->b_cont;
 359                 freeb(mp);
 360         } else {
 361                 hmp->b_cont = mp;
 362         }
 363         ASSERT(MBLKL(hmp) >= sizeof (struct ether_vlan_header));
 364 
 365         /*
 366          * Initialize the new TCI (Tag Control Information).
 367          */
 368         evhp->ether_tci = htons(VLAN_TCI(pri, 0, vid));
 369 
 370         return (hmp);
 371 }
 372 
 373 /*
 374  * Adds a VLAN tag with the specified VID and priority to each mblk of
 375  * the specified chain.
 376  */
 377 mblk_t *
 378 mac_add_vlan_tag_chain(mblk_t *mp_chain, uint_t pri, uint16_t vid)
 379 {
 380         mblk_t *next_mp, **prev, *mp;
 381 
 382         mp = mp_chain;
 383         prev = &mp_chain;
 384 
 385         while (mp != NULL) {
 386                 next_mp = mp->b_next;
 387                 mp->b_next = NULL;
 388                 if ((mp = mac_add_vlan_tag(mp, pri, vid)) == NULL) {
 389                         freemsgchain(next_mp);
 390                         break;
 391                 }
 392                 *prev = mp;
 393                 prev = &mp->b_next;
 394                 mp = mp->b_next = next_mp;
 395         }
 396 
 397         return (mp_chain);
 398 }
 399 
 400 /*
 401  * Strip VLAN tag
 402  */
 403 mblk_t *
 404 mac_strip_vlan_tag(mblk_t *mp)
 405 {
 406         mblk_t *newmp;
 407         struct ether_vlan_header *evhp;
 408 
 409         evhp = (struct ether_vlan_header *)mp->b_rptr;
 410         if (ntohs(evhp->ether_tpid) == ETHERTYPE_VLAN) {
 411                 ASSERT(MBLKL(mp) >= sizeof (struct ether_vlan_header));
 412 
 413                 if (DB_REF(mp) > 1) {
 414                         newmp = copymsg(mp);
 415                         if (newmp == NULL)
 416                                 return (NULL);
 417                         freemsg(mp);
 418                         mp = newmp;
 419                 }
 420 
 421                 evhp = (struct ether_vlan_header *)mp->b_rptr;
 422 
 423                 ovbcopy(mp->b_rptr, mp->b_rptr + VLAN_TAGSZ, 2 * ETHERADDRL);
 424                 mp->b_rptr += VLAN_TAGSZ;
 425         }
 426         return (mp);
 427 }
 428 
 429 /*
 430  * Strip VLAN tag from each mblk of the chain.
 431  */
 432 mblk_t *
 433 mac_strip_vlan_tag_chain(mblk_t *mp_chain)
 434 {
 435         mblk_t *mp, *next_mp, **prev;
 436 
 437         mp = mp_chain;
 438         prev = &mp_chain;
 439 
 440         while (mp != NULL) {
 441                 next_mp = mp->b_next;
 442                 mp->b_next = NULL;
 443                 if ((mp = mac_strip_vlan_tag(mp)) == NULL) {
 444                         freemsgchain(next_mp);
 445                         break;
 446                 }
 447                 *prev = mp;
 448                 prev = &mp->b_next;
 449                 mp = mp->b_next = next_mp;
 450         }
 451 
 452         return (mp_chain);
 453 }
 454 
 455 /*
 456  * Default callback function. Used when the datapath is not yet initialized.
 457  */
 458 /* ARGSUSED */
 459 void
 460 mac_pkt_drop(void *arg, mac_resource_handle_t resource, mblk_t *mp,
 461     boolean_t loopback)
 462 {
 463         mblk_t  *mp1 = mp;
 464 
 465         while (mp1 != NULL) {
 466                 mp1->b_prev = NULL;
 467                 mp1->b_queue = NULL;
 468                 mp1 = mp1->b_next;
 469         }
 470         freemsgchain(mp);
 471 }
 472 
 473 /*
 474  * Determines the IPv6 header length accounting for all the optional IPv6
 475  * headers (hop-by-hop, destination, routing and fragment). The header length
 476  * and next header value (a transport header) is captured.
 477  *
 478  * Returns B_FALSE if all the IP headers are not in the same mblk otherwise
 479  * returns B_TRUE.
 480  */
 481 boolean_t
 482 mac_ip_hdr_length_v6(ip6_t *ip6h, uint8_t *endptr, uint16_t *hdr_length,
 483     uint8_t *next_hdr, ip6_frag_t **fragp)
 484 {
 485         uint16_t length;
 486         uint_t  ehdrlen;
 487         uint8_t *whereptr;
 488         uint8_t *nexthdrp;
 489         ip6_dest_t *desthdr;
 490         ip6_rthdr_t *rthdr;
 491         ip6_frag_t *fraghdr;
 492 
 493         if (((uchar_t *)ip6h + IPV6_HDR_LEN) > endptr)
 494                 return (B_FALSE);
 495         /*
 496          * NOTE: Version-check failure here may cause mismatched IP versions
 497          * to propagate ENOBUFS in some callers, instead of letting the
 498          * packet reach the appropriate IP input function, where version check
 499          * errors are logged & counted.
 500          */
 501         if (IPH_HDR_VERSION(ip6h) != IPV6_VERSION)
 502                 return (B_FALSE);
 503         length = IPV6_HDR_LEN;
 504         whereptr = ((uint8_t *)&ip6h[1]); /* point to next hdr */
 505 
 506         if (fragp != NULL)
 507                 *fragp = NULL;
 508 
 509         nexthdrp = &ip6h->ip6_nxt;
 510         while (whereptr < endptr) {
 511                 /* Is there enough left for len + nexthdr? */
 512                 if (whereptr + MIN_EHDR_LEN > endptr)
 513                         break;
 514 
 515                 switch (*nexthdrp) {
 516                 case IPPROTO_HOPOPTS:
 517                 case IPPROTO_DSTOPTS:
 518                         /* Assumes the headers are identical for hbh and dst */
 519                         desthdr = (ip6_dest_t *)whereptr;
 520                         ehdrlen = 8 * (desthdr->ip6d_len + 1);
 521                         if ((uchar_t *)desthdr +  ehdrlen > endptr)
 522                                 return (B_FALSE);
 523                         nexthdrp = &desthdr->ip6d_nxt;
 524                         break;
 525                 case IPPROTO_ROUTING:
 526                         rthdr = (ip6_rthdr_t *)whereptr;
 527                         ehdrlen =  8 * (rthdr->ip6r_len + 1);
 528                         if ((uchar_t *)rthdr +  ehdrlen > endptr)
 529                                 return (B_FALSE);
 530                         nexthdrp = &rthdr->ip6r_nxt;
 531                         break;
 532                 case IPPROTO_FRAGMENT:
 533                         fraghdr = (ip6_frag_t *)whereptr;
 534                         ehdrlen = sizeof (ip6_frag_t);
 535                         if ((uchar_t *)&fraghdr[1] > endptr)
 536                                 return (B_FALSE);
 537                         nexthdrp = &fraghdr->ip6f_nxt;
 538                         if (fragp != NULL)
 539                                 *fragp = fraghdr;
 540                         break;
 541                 case IPPROTO_NONE:
 542                         /* No next header means we're finished */
 543                 default:
 544                         *hdr_length = length;
 545                         *next_hdr = *nexthdrp;
 546                         return (B_TRUE);
 547                 }
 548                 length += ehdrlen;
 549                 whereptr += ehdrlen;
 550                 *hdr_length = length;
 551                 *next_hdr = *nexthdrp;
 552         }
 553         switch (*nexthdrp) {
 554         case IPPROTO_HOPOPTS:
 555         case IPPROTO_DSTOPTS:
 556         case IPPROTO_ROUTING:
 557         case IPPROTO_FRAGMENT:
 558                 /*
 559                  * If any know extension headers are still to be processed,
 560                  * the packet's malformed (or at least all the IP header(s) are
 561                  * not in the same mblk - and that should never happen.
 562                  */
 563                 return (B_FALSE);
 564 
 565         default:
 566                 /*
 567                  * If we get here, we know that all of the IP headers were in
 568                  * the same mblk, even if the ULP header is in the next mblk.
 569                  */
 570                 *hdr_length = length;
 571                 *next_hdr = *nexthdrp;
 572                 return (B_TRUE);
 573         }
 574 }
 575 
 576 /*
 577  * The following set of routines are there to take care of interrupt
 578  * re-targeting for legacy (fixed) interrupts. Some older versions
 579  * of the popular NICs like e1000g do not support MSI-X interrupts
 580  * and they reserve fixed interrupts for RX/TX rings. To re-target
 581  * these interrupts, PCITOOL ioctls need to be used.
 582  */
 583 typedef struct mac_dladm_intr {
 584         int     ino;
 585         int     cpu_id;
 586         char    driver_path[MAXPATHLEN];
 587         char    nexus_path[MAXPATHLEN];
 588 } mac_dladm_intr_t;
 589 
 590 /* Bind the interrupt to cpu_num */
 591 static int
 592 mac_set_intr(ldi_handle_t lh, processorid_t cpu_num, int oldcpuid, int ino)
 593 {
 594         pcitool_intr_set_t      iset;
 595         int                     err;
 596 
 597         iset.old_cpu = oldcpuid;
 598         iset.ino = ino;
 599         iset.cpu_id = cpu_num;
 600         iset.user_version = PCITOOL_VERSION;
 601         err = ldi_ioctl(lh, PCITOOL_DEVICE_SET_INTR, (intptr_t)&iset, FKIOCTL,
 602             kcred, NULL);
 603 
 604         return (err);
 605 }
 606 
 607 /*
 608  * Search interrupt information. iget is filled in with the info to search
 609  */
 610 static boolean_t
 611 mac_search_intrinfo(pcitool_intr_get_t *iget_p, mac_dladm_intr_t *dln)
 612 {
 613         int     i;
 614         char    driver_path[2 * MAXPATHLEN];
 615 
 616         for (i = 0; i < iget_p->num_devs; i++) {
 617                 (void) strlcpy(driver_path, iget_p->dev[i].path, MAXPATHLEN);
 618                 (void) snprintf(&driver_path[strlen(driver_path)], MAXPATHLEN,
 619                     ":%s%d", iget_p->dev[i].driver_name,
 620                     iget_p->dev[i].dev_inst);
 621                 /* Match the device path for the device path */
 622                 if (strcmp(driver_path, dln->driver_path) == 0) {
 623                         dln->ino = iget_p->ino;
 624                         dln->cpu_id = iget_p->cpu_id;
 625                         return (B_TRUE);
 626                 }
 627         }
 628         return (B_FALSE);
 629 }
 630 
 631 /*
 632  * Get information about ino, i.e. if this is the interrupt for our
 633  * device and where it is bound etc.
 634  */
 635 static boolean_t
 636 mac_get_single_intr(ldi_handle_t lh, int oldcpuid, int ino,
 637     mac_dladm_intr_t *dln)
 638 {
 639         pcitool_intr_get_t      *iget_p;
 640         int                     ipsz;
 641         int                     nipsz;
 642         int                     err;
 643         uint8_t                 inum;
 644 
 645         /*
 646          * Check if SLEEP is OK, i.e if could come here in response to
 647          * changing the fanout due to some callback from the driver, say
 648          * link speed changes.
 649          */
 650         ipsz = PCITOOL_IGET_SIZE(0);
 651         iget_p = kmem_zalloc(ipsz, KM_SLEEP);
 652 
 653         iget_p->num_devs_ret = 0;
 654         iget_p->user_version = PCITOOL_VERSION;
 655         iget_p->cpu_id = oldcpuid;
 656         iget_p->ino = ino;
 657 
 658         err = ldi_ioctl(lh, PCITOOL_DEVICE_GET_INTR, (intptr_t)iget_p,
 659             FKIOCTL, kcred, NULL);
 660         if (err != 0) {
 661                 kmem_free(iget_p, ipsz);
 662                 return (B_FALSE);
 663         }
 664         if (iget_p->num_devs == 0) {
 665                 kmem_free(iget_p, ipsz);
 666                 return (B_FALSE);
 667         }
 668         inum = iget_p->num_devs;
 669         if (iget_p->num_devs_ret < iget_p->num_devs) {
 670                 /* Reallocate */
 671                 nipsz = PCITOOL_IGET_SIZE(iget_p->num_devs);
 672 
 673                 kmem_free(iget_p, ipsz);
 674                 ipsz = nipsz;
 675                 iget_p = kmem_zalloc(ipsz, KM_SLEEP);
 676 
 677                 iget_p->num_devs_ret = inum;
 678                 iget_p->cpu_id = oldcpuid;
 679                 iget_p->ino = ino;
 680                 iget_p->user_version = PCITOOL_VERSION;
 681                 err = ldi_ioctl(lh, PCITOOL_DEVICE_GET_INTR, (intptr_t)iget_p,
 682                     FKIOCTL, kcred, NULL);
 683                 if (err != 0) {
 684                         kmem_free(iget_p, ipsz);
 685                         return (B_FALSE);
 686                 }
 687                 /* defensive */
 688                 if (iget_p->num_devs != iget_p->num_devs_ret) {
 689                         kmem_free(iget_p, ipsz);
 690                         return (B_FALSE);
 691                 }
 692         }
 693 
 694         if (mac_search_intrinfo(iget_p, dln)) {
 695                 kmem_free(iget_p, ipsz);
 696                 return (B_TRUE);
 697         }
 698         kmem_free(iget_p, ipsz);
 699         return (B_FALSE);
 700 }
 701 
 702 /*
 703  * Get the interrupts and check each one to see if it is for our device.
 704  */
 705 static int
 706 mac_validate_intr(ldi_handle_t lh, mac_dladm_intr_t *dln, processorid_t cpuid)
 707 {
 708         pcitool_intr_info_t     intr_info;
 709         int                     err;
 710         int                     ino;
 711         int                     oldcpuid;
 712 
 713         err = ldi_ioctl(lh, PCITOOL_SYSTEM_INTR_INFO, (intptr_t)&intr_info,
 714             FKIOCTL, kcred, NULL);
 715         if (err != 0)
 716                 return (-1);
 717 
 718         for (oldcpuid = 0; oldcpuid < intr_info.num_cpu; oldcpuid++) {
 719                 for (ino = 0; ino < intr_info.num_intr; ino++) {
 720                         if (mac_get_single_intr(lh, oldcpuid, ino, dln)) {
 721                                 if (dln->cpu_id == cpuid)
 722                                         return (0);
 723                                 return (1);
 724                         }
 725                 }
 726         }
 727         return (-1);
 728 }
 729 
 730 /*
 731  * Obtain the nexus parent node info. for mdip.
 732  */
 733 static dev_info_t *
 734 mac_get_nexus_node(dev_info_t *mdip, mac_dladm_intr_t *dln)
 735 {
 736         struct dev_info         *tdip = (struct dev_info *)mdip;
 737         struct ddi_minor_data   *minordata;
 738         int                     circ;
 739         dev_info_t              *pdip;
 740         char                    pathname[MAXPATHLEN];
 741 
 742         while (tdip != NULL) {
 743                 /*
 744                  * The netboot code could call this function while walking the
 745                  * device tree so we need to use ndi_devi_tryenter() here to
 746                  * avoid deadlock.
 747                  */
 748                 if (ndi_devi_tryenter((dev_info_t *)tdip, &circ) == 0)
 749                         break;
 750 
 751                 for (minordata = tdip->devi_minor; minordata != NULL;
 752                     minordata = minordata->next) {
 753                         if (strncmp(minordata->ddm_node_type, DDI_NT_INTRCTL,
 754                             strlen(DDI_NT_INTRCTL)) == 0) {
 755                                 pdip = minordata->dip;
 756                                 (void) ddi_pathname(pdip, pathname);
 757                                 (void) snprintf(dln->nexus_path, MAXPATHLEN,
 758                                     "/devices%s:intr", pathname);
 759                                 (void) ddi_pathname_minor(minordata, pathname);
 760                                 ndi_devi_exit((dev_info_t *)tdip, circ);
 761                                 return (pdip);
 762                         }
 763                 }
 764                 ndi_devi_exit((dev_info_t *)tdip, circ);
 765                 tdip = tdip->devi_parent;
 766         }
 767         return (NULL);
 768 }
 769 
 770 /*
 771  * For a primary MAC client, if the user has set a list or CPUs or
 772  * we have obtained it implicitly, we try to retarget the interrupt
 773  * for that device on one of the CPUs in the list.
 774  * We assign the interrupt to the same CPU as the poll thread.
 775  */
 776 static boolean_t
 777 mac_check_interrupt_binding(dev_info_t *mdip, int32_t cpuid)
 778 {
 779         ldi_handle_t            lh = NULL;
 780         ldi_ident_t             li = NULL;
 781         int                     err;
 782         int                     ret;
 783         mac_dladm_intr_t        dln;
 784         dev_info_t              *dip;
 785         struct ddi_minor_data   *minordata;
 786 
 787         dln.nexus_path[0] = '\0';
 788         dln.driver_path[0] = '\0';
 789 
 790         minordata = ((struct dev_info *)mdip)->devi_minor;
 791         while (minordata != NULL) {
 792                 if (minordata->type == DDM_MINOR)
 793                         break;
 794                 minordata = minordata->next;
 795         }
 796         if (minordata == NULL)
 797                 return (B_FALSE);
 798 
 799         (void) ddi_pathname_minor(minordata, dln.driver_path);
 800 
 801         dip = mac_get_nexus_node(mdip, &dln);
 802         /* defensive */
 803         if (dip == NULL)
 804                 return (B_FALSE);
 805 
 806         err = ldi_ident_from_major(ddi_driver_major(dip), &li);
 807         if (err != 0)
 808                 return (B_FALSE);
 809 
 810         err = ldi_open_by_name(dln.nexus_path, FREAD|FWRITE, kcred, &lh, li);
 811         if (err != 0)
 812                 return (B_FALSE);
 813 
 814         ret = mac_validate_intr(lh, &dln, cpuid);
 815         if (ret < 0) {
 816                 (void) ldi_close(lh, FREAD|FWRITE, kcred);
 817                 return (B_FALSE);
 818         }
 819         /* cmn_note? */
 820         if (ret != 0)
 821                 if ((err = (mac_set_intr(lh, cpuid, dln.cpu_id, dln.ino)))
 822                     != 0) {
 823                         (void) ldi_close(lh, FREAD|FWRITE, kcred);
 824                         return (B_FALSE);
 825                 }
 826         (void) ldi_close(lh, FREAD|FWRITE, kcred);
 827         return (B_TRUE);
 828 }
 829 
 830 void
 831 mac_client_set_intr_cpu(void *arg, mac_client_handle_t mch, int32_t cpuid)
 832 {
 833         dev_info_t              *mdip = (dev_info_t *)arg;
 834         mac_client_impl_t       *mcip = (mac_client_impl_t *)mch;
 835         mac_resource_props_t    *mrp;
 836         mac_perim_handle_t      mph;
 837         flow_entry_t            *flent = mcip->mci_flent;
 838         mac_soft_ring_set_t     *rx_srs;
 839         mac_cpus_t              *srs_cpu;
 840 
 841         if (!mac_check_interrupt_binding(mdip, cpuid))
 842                 cpuid = -1;
 843         mac_perim_enter_by_mh((mac_handle_t)mcip->mci_mip, &mph);
 844         mrp = MCIP_RESOURCE_PROPS(mcip);
 845         mrp->mrp_rx_intr_cpu = cpuid;
 846         if (flent != NULL && flent->fe_rx_srs_cnt == 2) {
 847                 rx_srs = flent->fe_rx_srs[1];
 848                 srs_cpu = &rx_srs->srs_cpu;
 849                 srs_cpu->mc_rx_intr_cpu = cpuid;
 850         }
 851         mac_perim_exit(mph);
 852 }
 853 
 854 int32_t
 855 mac_client_intr_cpu(mac_client_handle_t mch)
 856 {
 857         mac_client_impl_t       *mcip = (mac_client_impl_t *)mch;
 858         mac_cpus_t              *srs_cpu;
 859         mac_soft_ring_set_t     *rx_srs;
 860         flow_entry_t            *flent = mcip->mci_flent;
 861         mac_resource_props_t    *mrp = MCIP_RESOURCE_PROPS(mcip);
 862         mac_ring_t              *ring;
 863         mac_intr_t              *mintr;
 864 
 865         /*
 866          * Check if we need to retarget the interrupt. We do this only
 867          * for the primary MAC client. We do this if we have the only
 868          * exclusive ring in the group.
 869          */
 870         if (mac_is_primary_client(mcip) && flent->fe_rx_srs_cnt == 2) {
 871                 rx_srs = flent->fe_rx_srs[1];
 872                 srs_cpu = &rx_srs->srs_cpu;
 873                 ring = rx_srs->srs_ring;
 874                 mintr = &ring->mr_info.mri_intr;
 875                 /*
 876                  * If ddi_handle is present or the poll CPU is
 877                  * already bound to the interrupt CPU, return -1.
 878                  */
 879                 if (mintr->mi_ddi_handle != NULL ||
 880                     ((mrp->mrp_ncpus != 0) &&
 881                     (mrp->mrp_rx_intr_cpu == srs_cpu->mc_rx_pollid))) {
 882                         return (-1);
 883                 }
 884                 return (srs_cpu->mc_rx_pollid);
 885         }
 886         return (-1);
 887 }
 888 
 889 void *
 890 mac_get_devinfo(mac_handle_t mh)
 891 {
 892         mac_impl_t      *mip = (mac_impl_t *)mh;
 893 
 894         return ((void *)mip->mi_dip);
 895 }
 896 
 897 #define PKT_HASH_2BYTES(x) ((x)[0] ^ (x)[1])
 898 #define PKT_HASH_4BYTES(x) ((x)[0] ^ (x)[1] ^ (x)[2] ^ (x)[3])
 899 #define PKT_HASH_MAC(x) ((x)[0] ^ (x)[1] ^ (x)[2] ^ (x)[3] ^ (x)[4] ^ (x)[5])
 900 
 901 uint64_t
 902 mac_pkt_hash(uint_t media, mblk_t *mp, uint8_t policy, boolean_t is_outbound)
 903 {
 904         struct ether_header *ehp;
 905         uint64_t hash = 0;
 906         uint16_t sap;
 907         uint_t skip_len;
 908         uint8_t proto;
 909         boolean_t ip_fragmented;
 910 
 911         /*
 912          * We may want to have one of these per MAC type plugin in the
 913          * future. For now supports only ethernet.
 914          */
 915         if (media != DL_ETHER)
 916                 return (0L);
 917 
 918         /* for now we support only outbound packets */
 919         ASSERT(is_outbound);
 920         ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
 921         ASSERT(MBLKL(mp) >= sizeof (struct ether_header));
 922 
 923         /* compute L2 hash */
 924 
 925         ehp = (struct ether_header *)mp->b_rptr;
 926 
 927         if ((policy & MAC_PKT_HASH_L2) != 0) {
 928                 uchar_t *mac_src = ehp->ether_shost.ether_addr_octet;
 929                 uchar_t *mac_dst = ehp->ether_dhost.ether_addr_octet;
 930                 hash = PKT_HASH_MAC(mac_src) ^ PKT_HASH_MAC(mac_dst);
 931                 policy &= ~MAC_PKT_HASH_L2;
 932         }
 933 
 934         if (policy == 0)
 935                 goto done;
 936 
 937         /* skip ethernet header */
 938 
 939         sap = ntohs(ehp->ether_type);
 940         if (sap == ETHERTYPE_VLAN) {
 941                 struct ether_vlan_header *evhp;
 942                 mblk_t *newmp = NULL;
 943 
 944                 skip_len = sizeof (struct ether_vlan_header);
 945                 if (MBLKL(mp) < skip_len) {
 946                         /* the vlan tag is the payload, pull up first */
 947                         newmp = msgpullup(mp, -1);
 948                         if ((newmp == NULL) || (MBLKL(newmp) < skip_len)) {
 949                                 goto done;
 950                         }
 951                         evhp = (struct ether_vlan_header *)newmp->b_rptr;
 952                 } else {
 953                         evhp = (struct ether_vlan_header *)mp->b_rptr;
 954                 }
 955 
 956                 sap = ntohs(evhp->ether_type);
 957                 freemsg(newmp);
 958         } else {
 959                 skip_len = sizeof (struct ether_header);
 960         }
 961 
 962         /* if ethernet header is in its own mblk, skip it */
 963         if (MBLKL(mp) <= skip_len) {
 964                 skip_len -= MBLKL(mp);
 965                 mp = mp->b_cont;
 966                 if (mp == NULL)
 967                         goto done;
 968         }
 969 
 970         sap = (sap < ETHERTYPE_802_MIN) ? 0 : sap;
 971 
 972         /* compute IP src/dst addresses hash and skip IPv{4,6} header */
 973 
 974         switch (sap) {
 975         case ETHERTYPE_IP: {
 976                 ipha_t *iphp;
 977 
 978                 /*
 979                  * If the header is not aligned or the header doesn't fit
 980                  * in the mblk, bail now. Note that this may cause packets
 981                  * reordering.
 982                  */
 983                 iphp = (ipha_t *)(mp->b_rptr + skip_len);
 984                 if (((unsigned char *)iphp + sizeof (ipha_t) > mp->b_wptr) ||
 985                     !OK_32PTR((char *)iphp))
 986                         goto done;
 987 
 988                 proto = iphp->ipha_protocol;
 989                 skip_len += IPH_HDR_LENGTH(iphp);
 990 
 991                 /* Check if the packet is fragmented. */
 992                 ip_fragmented = ntohs(iphp->ipha_fragment_offset_and_flags) &
 993                     IPH_OFFSET;
 994 
 995                 /*
 996                  * For fragmented packets, use addresses in addition to
 997                  * the frag_id to generate the hash inorder to get
 998                  * better distribution.
 999                  */
1000                 if (ip_fragmented || (policy & MAC_PKT_HASH_L3) != 0) {
1001                         uint8_t *ip_src = (uint8_t *)&(iphp->ipha_src);
1002                         uint8_t *ip_dst = (uint8_t *)&(iphp->ipha_dst);
1003 
1004                         hash ^= (PKT_HASH_4BYTES(ip_src) ^
1005                             PKT_HASH_4BYTES(ip_dst));
1006                         policy &= ~MAC_PKT_HASH_L3;
1007                 }
1008 
1009                 if (ip_fragmented) {
1010                         uint8_t *identp = (uint8_t *)&iphp->ipha_ident;
1011                         hash ^= PKT_HASH_2BYTES(identp);
1012                         goto done;
1013                 }
1014                 break;
1015         }
1016         case ETHERTYPE_IPV6: {
1017                 ip6_t *ip6hp;
1018                 ip6_frag_t *frag = NULL;
1019                 uint16_t hdr_length;
1020 
1021                 /*
1022                  * If the header is not aligned or the header doesn't fit
1023                  * in the mblk, bail now. Note that this may cause packets
1024                  * reordering.
1025                  */
1026 
1027                 ip6hp = (ip6_t *)(mp->b_rptr + skip_len);
1028                 if (((unsigned char *)ip6hp + IPV6_HDR_LEN > mp->b_wptr) ||
1029                     !OK_32PTR((char *)ip6hp))
1030                         goto done;
1031 
1032                 if (!mac_ip_hdr_length_v6(ip6hp, mp->b_wptr, &hdr_length,
1033                     &proto, &frag))
1034                         goto done;
1035                 skip_len += hdr_length;
1036 
1037                 /*
1038                  * For fragmented packets, use addresses in addition to
1039                  * the frag_id to generate the hash inorder to get
1040                  * better distribution.
1041                  */
1042                 if (frag != NULL || (policy & MAC_PKT_HASH_L3) != 0) {
1043                         uint8_t *ip_src = &(ip6hp->ip6_src.s6_addr8[12]);
1044                         uint8_t *ip_dst = &(ip6hp->ip6_dst.s6_addr8[12]);
1045 
1046                         hash ^= (PKT_HASH_4BYTES(ip_src) ^
1047                             PKT_HASH_4BYTES(ip_dst));
1048                         policy &= ~MAC_PKT_HASH_L3;
1049                 }
1050 
1051                 if (frag != NULL) {
1052                         uint8_t *identp = (uint8_t *)&frag->ip6f_ident;
1053                         hash ^= PKT_HASH_4BYTES(identp);
1054                         goto done;
1055                 }
1056                 break;
1057         }
1058         default:
1059                 goto done;
1060         }
1061 
1062         if (policy == 0)
1063                 goto done;
1064 
1065         /* if ip header is in its own mblk, skip it */
1066         if (MBLKL(mp) <= skip_len) {
1067                 skip_len -= MBLKL(mp);
1068                 mp = mp->b_cont;
1069                 if (mp == NULL)
1070                         goto done;
1071         }
1072 
1073         /* parse ULP header */
1074 again:
1075         switch (proto) {
1076         case IPPROTO_TCP:
1077         case IPPROTO_UDP:
1078         case IPPROTO_ESP:
1079         case IPPROTO_SCTP:
1080                 /*
1081                  * These Internet Protocols are intentionally designed
1082                  * for hashing from the git-go.  Port numbers are in the first
1083                  * word for transports, SPI is first for ESP.
1084                  */
1085                 if (mp->b_rptr + skip_len + 4 > mp->b_wptr)
1086                         goto done;
1087                 hash ^= PKT_HASH_4BYTES((mp->b_rptr + skip_len));
1088                 break;
1089 
1090         case IPPROTO_AH: {
1091                 ah_t *ah = (ah_t *)(mp->b_rptr + skip_len);
1092                 uint_t ah_length = AH_TOTAL_LEN(ah);
1093 
1094                 if ((unsigned char *)ah + sizeof (ah_t) > mp->b_wptr)
1095                         goto done;
1096 
1097                 proto = ah->ah_nexthdr;
1098                 skip_len += ah_length;
1099 
1100                 /* if AH header is in its own mblk, skip it */
1101                 if (MBLKL(mp) <= skip_len) {
1102                         skip_len -= MBLKL(mp);
1103                         mp = mp->b_cont;
1104                         if (mp == NULL)
1105                                 goto done;
1106                 }
1107 
1108                 goto again;
1109         }
1110         }
1111 
1112 done:
1113         return (hash);
1114 }