18275SEric Cheng /* 28275SEric Cheng * CDDL HEADER START 38275SEric Cheng * 48275SEric Cheng * The contents of this file are subject to the terms of the 58275SEric Cheng * Common Development and Distribution License (the "License"). 68275SEric Cheng * You may not use this file except in compliance with the License. 78275SEric Cheng * 88275SEric Cheng * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 98275SEric Cheng * or http://www.opensolaris.org/os/licensing. 108275SEric Cheng * See the License for the specific language governing permissions 118275SEric Cheng * and limitations under the License. 128275SEric Cheng * 138275SEric Cheng * When distributing Covered Code, include this CDDL HEADER in each 148275SEric Cheng * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 158275SEric Cheng * If applicable, add the following below this CDDL HEADER, with the 168275SEric Cheng * fields enclosed by brackets "[]" replaced with your own identifying 178275SEric Cheng * information: Portions Copyright [yyyy] [name of copyright owner] 188275SEric Cheng * 198275SEric Cheng * CDDL HEADER END 208275SEric Cheng */ 218275SEric Cheng /* 22*8833SVenu.Iyer@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 238275SEric Cheng * Use is subject to license terms. 248275SEric Cheng */ 258275SEric Cheng 268275SEric Cheng /* 278275SEric Cheng * MAC Services Module - misc utilities 288275SEric Cheng */ 298275SEric Cheng 308275SEric Cheng #include <sys/types.h> 318275SEric Cheng #include <sys/mac.h> 328275SEric Cheng #include <sys/mac_impl.h> 338275SEric Cheng #include <sys/mac_client_priv.h> 348275SEric Cheng #include <sys/mac_client_impl.h> 358275SEric Cheng #include <sys/mac_soft_ring.h> 368275SEric Cheng #include <sys/strsubr.h> 378275SEric Cheng #include <sys/strsun.h> 388275SEric Cheng #include <sys/vlan.h> 398275SEric Cheng #include <sys/pattr.h> 408275SEric Cheng #include <sys/pci_tools.h> 418275SEric Cheng #include <inet/ip.h> 428275SEric Cheng #include <inet/ip_impl.h> 438275SEric Cheng #include <inet/ip6.h> 448275SEric Cheng #include <sys/vtrace.h> 458275SEric Cheng #include <sys/dlpi.h> 468275SEric Cheng #include <sys/sunndi.h> 47*8833SVenu.Iyer@Sun.COM #include <inet/ipsec_impl.h> 48*8833SVenu.Iyer@Sun.COM #include <inet/sadb.h> 49*8833SVenu.Iyer@Sun.COM #include <inet/ipsecesp.h> 50*8833SVenu.Iyer@Sun.COM #include <inet/ipsecah.h> 518275SEric Cheng 528275SEric Cheng /* 538275SEric Cheng * Copy an mblk, preserving its hardware checksum flags. 548275SEric Cheng */ 558275SEric Cheng static mblk_t * 568275SEric Cheng mac_copymsg_cksum(mblk_t *mp) 578275SEric Cheng { 588275SEric Cheng mblk_t *mp1; 598275SEric Cheng uint32_t start, stuff, end, value, flags; 608275SEric Cheng 618275SEric Cheng mp1 = copymsg(mp); 628275SEric Cheng if (mp1 == NULL) 638275SEric Cheng return (NULL); 648275SEric Cheng 658275SEric Cheng hcksum_retrieve(mp, NULL, NULL, &start, &stuff, &end, &value, &flags); 668275SEric Cheng (void) hcksum_assoc(mp1, NULL, NULL, start, stuff, end, value, 678275SEric Cheng flags, KM_NOSLEEP); 688275SEric Cheng 698275SEric Cheng return (mp1); 708275SEric Cheng } 718275SEric Cheng 728275SEric Cheng /* 738275SEric Cheng * Copy an mblk chain, presenting the hardware checksum flags of the 748275SEric Cheng * individual mblks. 758275SEric Cheng */ 768275SEric Cheng mblk_t * 778275SEric Cheng mac_copymsgchain_cksum(mblk_t *mp) 788275SEric Cheng { 798275SEric Cheng mblk_t *nmp = NULL; 808275SEric Cheng mblk_t **nmpp = &nmp; 818275SEric Cheng 828275SEric Cheng for (; mp != NULL; mp = mp->b_next) { 838275SEric Cheng if ((*nmpp = mac_copymsg_cksum(mp)) == NULL) { 848275SEric Cheng freemsgchain(nmp); 858275SEric Cheng return (NULL); 868275SEric Cheng } 878275SEric Cheng 888275SEric Cheng nmpp = &((*nmpp)->b_next); 898275SEric Cheng } 908275SEric Cheng 918275SEric Cheng return (nmp); 928275SEric Cheng } 938275SEric Cheng 948275SEric Cheng /* 958275SEric Cheng * Process the specified mblk chain for proper handling of hardware 968275SEric Cheng * checksum offload. This routine is invoked for loopback traffic 978275SEric Cheng * between MAC clients. 988275SEric Cheng * The function handles a NULL mblk chain passed as argument. 998275SEric Cheng */ 1008275SEric Cheng mblk_t * 1018275SEric Cheng mac_fix_cksum(mblk_t *mp_chain) 1028275SEric Cheng { 1038275SEric Cheng mblk_t *mp, *prev = NULL, *new_chain = mp_chain, *mp1; 1048275SEric Cheng uint32_t flags, start, stuff, end, value; 1058275SEric Cheng 1068275SEric Cheng for (mp = mp_chain; mp != NULL; prev = mp, mp = mp->b_next) { 1078275SEric Cheng uint16_t len; 1088275SEric Cheng uint32_t offset; 1098275SEric Cheng struct ether_header *ehp; 1108275SEric Cheng uint16_t sap; 1118275SEric Cheng 1128275SEric Cheng hcksum_retrieve(mp, NULL, NULL, &start, &stuff, &end, &value, 1138275SEric Cheng &flags); 1148275SEric Cheng if (flags == 0) 1158275SEric Cheng continue; 1168275SEric Cheng 1178275SEric Cheng /* 1188275SEric Cheng * Since the processing of checksum offload for loopback 1198275SEric Cheng * traffic requires modification of the packet contents, 1208275SEric Cheng * ensure sure that we are always modifying our own copy. 1218275SEric Cheng */ 1228275SEric Cheng if (DB_REF(mp) > 1) { 1238275SEric Cheng mp1 = copymsg(mp); 1248275SEric Cheng if (mp1 == NULL) 1258275SEric Cheng continue; 1268275SEric Cheng mp1->b_next = mp->b_next; 1278275SEric Cheng mp->b_next = NULL; 1288275SEric Cheng freemsg(mp); 1298275SEric Cheng if (prev != NULL) 1308275SEric Cheng prev->b_next = mp1; 1318275SEric Cheng else 1328275SEric Cheng new_chain = mp1; 1338275SEric Cheng mp = mp1; 1348275SEric Cheng } 1358275SEric Cheng 1368275SEric Cheng /* 1378275SEric Cheng * Ethernet, and optionally VLAN header. 1388275SEric Cheng */ 1398275SEric Cheng /* LINTED: improper alignment cast */ 1408275SEric Cheng ehp = (struct ether_header *)mp->b_rptr; 1418275SEric Cheng if (ntohs(ehp->ether_type) == VLAN_TPID) { 1428275SEric Cheng struct ether_vlan_header *evhp; 1438275SEric Cheng 1448275SEric Cheng ASSERT(MBLKL(mp) >= sizeof (struct ether_vlan_header)); 1458275SEric Cheng /* LINTED: improper alignment cast */ 1468275SEric Cheng evhp = (struct ether_vlan_header *)mp->b_rptr; 1478275SEric Cheng sap = ntohs(evhp->ether_type); 1488275SEric Cheng offset = sizeof (struct ether_vlan_header); 1498275SEric Cheng } else { 1508275SEric Cheng sap = ntohs(ehp->ether_type); 1518275SEric Cheng offset = sizeof (struct ether_header); 1528275SEric Cheng } 1538275SEric Cheng 1548275SEric Cheng if (MBLKL(mp) <= offset) { 1558275SEric Cheng offset -= MBLKL(mp); 1568275SEric Cheng if (mp->b_cont == NULL) { 1578275SEric Cheng /* corrupted packet, skip it */ 1588275SEric Cheng if (prev != NULL) 1598275SEric Cheng prev->b_next = mp->b_next; 1608275SEric Cheng else 1618275SEric Cheng new_chain = mp->b_next; 1628275SEric Cheng mp1 = mp->b_next; 1638275SEric Cheng mp->b_next = NULL; 1648275SEric Cheng freemsg(mp); 1658275SEric Cheng mp = mp1; 1668275SEric Cheng continue; 1678275SEric Cheng } 1688275SEric Cheng mp = mp->b_cont; 1698275SEric Cheng } 1708275SEric Cheng 1718275SEric Cheng if (flags & (HCK_FULLCKSUM | HCK_IPV4_HDRCKSUM)) { 1728275SEric Cheng ipha_t *ipha = NULL; 1738275SEric Cheng 1748275SEric Cheng /* 1758275SEric Cheng * In order to compute the full and header 1768275SEric Cheng * checksums, we need to find and parse 1778275SEric Cheng * the IP and/or ULP headers. 1788275SEric Cheng */ 1798275SEric Cheng 1808275SEric Cheng sap = (sap < ETHERTYPE_802_MIN) ? 0 : sap; 1818275SEric Cheng 1828275SEric Cheng /* 1838275SEric Cheng * IP header. 1848275SEric Cheng */ 1858275SEric Cheng if (sap != ETHERTYPE_IP) 1868275SEric Cheng continue; 1878275SEric Cheng 1888275SEric Cheng ASSERT(MBLKL(mp) >= offset + sizeof (ipha_t)); 1898275SEric Cheng /* LINTED: improper alignment cast */ 1908275SEric Cheng ipha = (ipha_t *)(mp->b_rptr + offset); 1918275SEric Cheng 1928275SEric Cheng if (flags & HCK_FULLCKSUM) { 1938275SEric Cheng ipaddr_t src, dst; 1948275SEric Cheng uint32_t cksum; 1958275SEric Cheng uint16_t *up; 1968275SEric Cheng uint8_t proto; 1978275SEric Cheng 1988275SEric Cheng /* 1998275SEric Cheng * Pointer to checksum field in ULP header. 2008275SEric Cheng */ 2018275SEric Cheng proto = ipha->ipha_protocol; 2028275SEric Cheng ASSERT(ipha->ipha_version_and_hdr_length == 2038275SEric Cheng IP_SIMPLE_HDR_VERSION); 2048275SEric Cheng if (proto == IPPROTO_TCP) { 2058275SEric Cheng /* LINTED: improper alignment cast */ 2068275SEric Cheng up = IPH_TCPH_CHECKSUMP(ipha, 2078275SEric Cheng IP_SIMPLE_HDR_LENGTH); 2088275SEric Cheng } else { 2098275SEric Cheng ASSERT(proto == IPPROTO_UDP); 2108275SEric Cheng /* LINTED: improper alignment cast */ 2118275SEric Cheng up = IPH_UDPH_CHECKSUMP(ipha, 2128275SEric Cheng IP_SIMPLE_HDR_LENGTH); 2138275SEric Cheng } 2148275SEric Cheng 2158275SEric Cheng /* 2168275SEric Cheng * Pseudo-header checksum. 2178275SEric Cheng */ 2188275SEric Cheng src = ipha->ipha_src; 2198275SEric Cheng dst = ipha->ipha_dst; 2208275SEric Cheng len = ntohs(ipha->ipha_length) - 2218275SEric Cheng IP_SIMPLE_HDR_LENGTH; 2228275SEric Cheng 2238275SEric Cheng cksum = (dst >> 16) + (dst & 0xFFFF) + 2248275SEric Cheng (src >> 16) + (src & 0xFFFF); 2258275SEric Cheng cksum += htons(len); 2268275SEric Cheng 2278275SEric Cheng /* 2288275SEric Cheng * The checksum value stored in the packet needs 2298275SEric Cheng * to be correct. Compute it here. 2308275SEric Cheng */ 2318275SEric Cheng *up = 0; 2328275SEric Cheng cksum += (((proto) == IPPROTO_UDP) ? 2338275SEric Cheng IP_UDP_CSUM_COMP : IP_TCP_CSUM_COMP); 2348275SEric Cheng cksum = IP_CSUM(mp, IP_SIMPLE_HDR_LENGTH + 2358275SEric Cheng offset, cksum); 2368275SEric Cheng *(up) = (uint16_t)(cksum ? cksum : ~cksum); 2378275SEric Cheng 2388275SEric Cheng flags |= HCK_FULLCKSUM_OK; 2398275SEric Cheng value = 0xffff; 2408275SEric Cheng } 2418275SEric Cheng 2428275SEric Cheng if (flags & HCK_IPV4_HDRCKSUM) { 2438275SEric Cheng ASSERT(ipha != NULL); 2448275SEric Cheng ipha->ipha_hdr_checksum = 2458275SEric Cheng (uint16_t)ip_csum_hdr(ipha); 2468275SEric Cheng } 2478275SEric Cheng } 2488275SEric Cheng 2498275SEric Cheng if (flags & HCK_PARTIALCKSUM) { 2508275SEric Cheng uint16_t *up, partial, cksum; 2518275SEric Cheng uchar_t *ipp; /* ptr to beginning of IP header */ 2528275SEric Cheng 2538275SEric Cheng if (mp->b_cont != NULL) { 2548275SEric Cheng mblk_t *mp1; 2558275SEric Cheng 2568275SEric Cheng mp1 = msgpullup(mp, offset + end); 2578275SEric Cheng if (mp1 == NULL) 2588275SEric Cheng continue; 2598275SEric Cheng mp1->b_next = mp->b_next; 2608275SEric Cheng mp->b_next = NULL; 2618275SEric Cheng freemsg(mp); 2628275SEric Cheng if (prev != NULL) 2638275SEric Cheng prev->b_next = mp1; 2648275SEric Cheng else 2658275SEric Cheng new_chain = mp1; 2668275SEric Cheng mp = mp1; 2678275SEric Cheng } 2688275SEric Cheng 2698275SEric Cheng ipp = mp->b_rptr + offset; 2708275SEric Cheng /* LINTED: cast may result in improper alignment */ 2718275SEric Cheng up = (uint16_t *)((uchar_t *)ipp + stuff); 2728275SEric Cheng partial = *up; 2738275SEric Cheng *up = 0; 2748275SEric Cheng 2758275SEric Cheng cksum = IP_BCSUM_PARTIAL(mp->b_rptr + offset + start, 2768275SEric Cheng end - start, partial); 2778275SEric Cheng cksum = ~cksum; 2788275SEric Cheng *up = cksum ? cksum : ~cksum; 2798275SEric Cheng 2808275SEric Cheng /* 2818275SEric Cheng * Since we already computed the whole checksum, 2828275SEric Cheng * indicate to the stack that it has already 2838275SEric Cheng * been verified by the hardware. 2848275SEric Cheng */ 2858275SEric Cheng flags &= ~HCK_PARTIALCKSUM; 2868275SEric Cheng flags |= (HCK_FULLCKSUM | HCK_FULLCKSUM_OK); 2878275SEric Cheng value = 0xffff; 2888275SEric Cheng } 2898275SEric Cheng 2908275SEric Cheng (void) hcksum_assoc(mp, NULL, NULL, start, stuff, end, 2918275SEric Cheng value, flags, KM_NOSLEEP); 2928275SEric Cheng } 2938275SEric Cheng 2948275SEric Cheng return (new_chain); 2958275SEric Cheng } 2968275SEric Cheng 2978275SEric Cheng /* 2988275SEric Cheng * Add VLAN tag to the specified mblk. 2998275SEric Cheng */ 3008275SEric Cheng mblk_t * 3018275SEric Cheng mac_add_vlan_tag(mblk_t *mp, uint_t pri, uint16_t vid) 3028275SEric Cheng { 3038275SEric Cheng mblk_t *hmp; 3048275SEric Cheng struct ether_vlan_header *evhp; 3058275SEric Cheng struct ether_header *ehp; 3068275SEric Cheng uint32_t start, stuff, end, value, flags; 3078275SEric Cheng 3088275SEric Cheng ASSERT(pri != 0 || vid != 0); 3098275SEric Cheng 3108275SEric Cheng /* 3118275SEric Cheng * Allocate an mblk for the new tagged ethernet header, 3128275SEric Cheng * and copy the MAC addresses and ethertype from the 3138275SEric Cheng * original header. 3148275SEric Cheng */ 3158275SEric Cheng 3168275SEric Cheng hmp = allocb(sizeof (struct ether_vlan_header), BPRI_MED); 3178275SEric Cheng if (hmp == NULL) { 3188275SEric Cheng freemsg(mp); 3198275SEric Cheng return (NULL); 3208275SEric Cheng } 3218275SEric Cheng 3228275SEric Cheng evhp = (struct ether_vlan_header *)hmp->b_rptr; 3238275SEric Cheng ehp = (struct ether_header *)mp->b_rptr; 3248275SEric Cheng 3258275SEric Cheng bcopy(ehp, evhp, (ETHERADDRL * 2)); 3268275SEric Cheng evhp->ether_type = ehp->ether_type; 3278275SEric Cheng evhp->ether_tpid = htons(ETHERTYPE_VLAN); 3288275SEric Cheng 3298275SEric Cheng hmp->b_wptr += sizeof (struct ether_vlan_header); 3308275SEric Cheng mp->b_rptr += sizeof (struct ether_header); 3318275SEric Cheng 3328275SEric Cheng /* 3338275SEric Cheng * Free the original message if it's now empty. Link the 3348275SEric Cheng * rest of messages to the header message. 3358275SEric Cheng */ 3368275SEric Cheng hcksum_retrieve(mp, NULL, NULL, &start, &stuff, &end, &value, &flags); 3378275SEric Cheng (void) hcksum_assoc(hmp, NULL, NULL, start, stuff, end, value, flags, 3388275SEric Cheng KM_NOSLEEP); 3398275SEric Cheng if (MBLKL(mp) == 0) { 3408275SEric Cheng hmp->b_cont = mp->b_cont; 3418275SEric Cheng freeb(mp); 3428275SEric Cheng } else { 3438275SEric Cheng hmp->b_cont = mp; 3448275SEric Cheng } 3458275SEric Cheng ASSERT(MBLKL(hmp) >= sizeof (struct ether_vlan_header)); 3468275SEric Cheng 3478275SEric Cheng /* 3488275SEric Cheng * Initialize the new TCI (Tag Control Information). 3498275SEric Cheng */ 3508275SEric Cheng evhp->ether_tci = htons(VLAN_TCI(pri, 0, vid)); 3518275SEric Cheng 3528275SEric Cheng return (hmp); 3538275SEric Cheng } 3548275SEric Cheng 3558275SEric Cheng /* 3568275SEric Cheng * Adds a VLAN tag with the specified VID and priority to each mblk of 3578275SEric Cheng * the specified chain. 3588275SEric Cheng */ 3598275SEric Cheng mblk_t * 3608275SEric Cheng mac_add_vlan_tag_chain(mblk_t *mp_chain, uint_t pri, uint16_t vid) 3618275SEric Cheng { 3628275SEric Cheng mblk_t *next_mp, **prev, *mp; 3638275SEric Cheng 3648275SEric Cheng mp = mp_chain; 3658275SEric Cheng prev = &mp_chain; 3668275SEric Cheng 3678275SEric Cheng while (mp != NULL) { 3688275SEric Cheng next_mp = mp->b_next; 3698275SEric Cheng mp->b_next = NULL; 3708275SEric Cheng if ((mp = mac_add_vlan_tag(mp, pri, vid)) == NULL) { 3718275SEric Cheng freemsgchain(next_mp); 3728275SEric Cheng break; 3738275SEric Cheng } 3748275SEric Cheng *prev = mp; 3758275SEric Cheng prev = &mp->b_next; 3768275SEric Cheng mp = mp->b_next = next_mp; 3778275SEric Cheng } 3788275SEric Cheng 3798275SEric Cheng return (mp_chain); 3808275SEric Cheng } 3818275SEric Cheng 3828275SEric Cheng /* 3838275SEric Cheng * Strip VLAN tag 3848275SEric Cheng */ 3858275SEric Cheng mblk_t * 3868275SEric Cheng mac_strip_vlan_tag(mblk_t *mp) 3878275SEric Cheng { 3888275SEric Cheng mblk_t *newmp; 3898275SEric Cheng struct ether_vlan_header *evhp; 3908275SEric Cheng 3918275SEric Cheng evhp = (struct ether_vlan_header *)mp->b_rptr; 3928275SEric Cheng if (ntohs(evhp->ether_tpid) == ETHERTYPE_VLAN) { 3938275SEric Cheng ASSERT(MBLKL(mp) >= sizeof (struct ether_vlan_header)); 3948275SEric Cheng 3958275SEric Cheng if (DB_REF(mp) > 1) { 3968275SEric Cheng newmp = copymsg(mp); 3978275SEric Cheng if (newmp == NULL) 3988275SEric Cheng return (NULL); 3998275SEric Cheng freemsg(mp); 4008275SEric Cheng mp = newmp; 4018275SEric Cheng } 4028275SEric Cheng 4038275SEric Cheng evhp = (struct ether_vlan_header *)mp->b_rptr; 4048275SEric Cheng 4058275SEric Cheng ovbcopy(mp->b_rptr, mp->b_rptr + VLAN_TAGSZ, 2 * ETHERADDRL); 4068275SEric Cheng mp->b_rptr += VLAN_TAGSZ; 4078275SEric Cheng } 4088275SEric Cheng return (mp); 4098275SEric Cheng } 4108275SEric Cheng 4118275SEric Cheng /* 4128275SEric Cheng * Strip VLAN tag from each mblk of the chain. 4138275SEric Cheng */ 4148275SEric Cheng mblk_t * 4158275SEric Cheng mac_strip_vlan_tag_chain(mblk_t *mp_chain) 4168275SEric Cheng { 4178275SEric Cheng mblk_t *mp, *next_mp, **prev; 4188275SEric Cheng 4198275SEric Cheng mp = mp_chain; 4208275SEric Cheng prev = &mp_chain; 4218275SEric Cheng 4228275SEric Cheng while (mp != NULL) { 4238275SEric Cheng next_mp = mp->b_next; 4248275SEric Cheng mp->b_next = NULL; 4258275SEric Cheng if ((mp = mac_strip_vlan_tag(mp)) == NULL) { 4268275SEric Cheng freemsgchain(next_mp); 4278275SEric Cheng break; 4288275SEric Cheng } 4298275SEric Cheng *prev = mp; 4308275SEric Cheng prev = &mp->b_next; 4318275SEric Cheng mp = mp->b_next = next_mp; 4328275SEric Cheng } 4338275SEric Cheng 4348275SEric Cheng return (mp_chain); 4358275SEric Cheng } 4368275SEric Cheng 4378275SEric Cheng /* 4388275SEric Cheng * Default callback function. Used when the datapath is not yet initialized. 4398275SEric Cheng */ 4408275SEric Cheng /* ARGSUSED */ 4418275SEric Cheng void 4428275SEric Cheng mac_pkt_drop(void *arg, mac_resource_handle_t resource, mblk_t *mp, 4438275SEric Cheng boolean_t loopback) 4448275SEric Cheng { 4458275SEric Cheng mblk_t *mp1 = mp; 4468275SEric Cheng 4478275SEric Cheng while (mp1 != NULL) { 4488275SEric Cheng mp1->b_prev = NULL; 4498275SEric Cheng mp1->b_queue = NULL; 4508275SEric Cheng mp1 = mp1->b_next; 4518275SEric Cheng } 4528275SEric Cheng freemsgchain(mp); 4538275SEric Cheng } 4548275SEric Cheng 4558275SEric Cheng /* 4568275SEric Cheng * Determines the IPv6 header length accounting for all the optional IPv6 4578275SEric Cheng * headers (hop-by-hop, destination, routing and fragment). The header length 4588275SEric Cheng * and next header value (a transport header) is captured. 4598275SEric Cheng * 4608275SEric Cheng * Returns B_FALSE if all the IP headers are not in the same mblk otherwise 4618275SEric Cheng * returns B_TRUE. 4628275SEric Cheng */ 4638275SEric Cheng boolean_t 4648275SEric Cheng mac_ip_hdr_length_v6(mblk_t *mp, ip6_t *ip6h, uint16_t *hdr_length, 4658275SEric Cheng uint8_t *next_hdr) 4668275SEric Cheng { 4678275SEric Cheng uint16_t length; 4688275SEric Cheng uint_t ehdrlen; 4698275SEric Cheng uint8_t *whereptr; 4708275SEric Cheng uint8_t *endptr; 4718275SEric Cheng uint8_t *nexthdrp; 4728275SEric Cheng ip6_dest_t *desthdr; 4738275SEric Cheng ip6_rthdr_t *rthdr; 4748275SEric Cheng ip6_frag_t *fraghdr; 4758275SEric Cheng 4768275SEric Cheng endptr = mp->b_wptr; 4778275SEric Cheng if (((uchar_t *)ip6h + IPV6_HDR_LEN) > endptr) 4788275SEric Cheng return (B_FALSE); 4798275SEric Cheng ASSERT((IPH_HDR_VERSION(ip6h) & ~IP_FORWARD_PROG_BIT) == IPV6_VERSION); 4808275SEric Cheng length = IPV6_HDR_LEN; 4818275SEric Cheng whereptr = ((uint8_t *)&ip6h[1]); /* point to next hdr */ 4828275SEric Cheng 4838275SEric Cheng nexthdrp = &ip6h->ip6_nxt; 4848275SEric Cheng while (whereptr < endptr) { 4858275SEric Cheng /* Is there enough left for len + nexthdr? */ 4868275SEric Cheng if (whereptr + MIN_EHDR_LEN > endptr) 4878275SEric Cheng break; 4888275SEric Cheng 4898275SEric Cheng switch (*nexthdrp) { 4908275SEric Cheng case IPPROTO_HOPOPTS: 4918275SEric Cheng case IPPROTO_DSTOPTS: 4928275SEric Cheng /* Assumes the headers are identical for hbh and dst */ 4938275SEric Cheng desthdr = (ip6_dest_t *)whereptr; 4948275SEric Cheng ehdrlen = 8 * (desthdr->ip6d_len + 1); 4958275SEric Cheng if ((uchar_t *)desthdr + ehdrlen > endptr) 4968275SEric Cheng return (B_FALSE); 4978275SEric Cheng nexthdrp = &desthdr->ip6d_nxt; 4988275SEric Cheng break; 4998275SEric Cheng case IPPROTO_ROUTING: 5008275SEric Cheng rthdr = (ip6_rthdr_t *)whereptr; 5018275SEric Cheng ehdrlen = 8 * (rthdr->ip6r_len + 1); 5028275SEric Cheng if ((uchar_t *)rthdr + ehdrlen > endptr) 5038275SEric Cheng return (B_FALSE); 5048275SEric Cheng nexthdrp = &rthdr->ip6r_nxt; 5058275SEric Cheng break; 5068275SEric Cheng case IPPROTO_FRAGMENT: 5078275SEric Cheng fraghdr = (ip6_frag_t *)whereptr; 5088275SEric Cheng ehdrlen = sizeof (ip6_frag_t); 5098275SEric Cheng if ((uchar_t *)&fraghdr[1] > endptr) 5108275SEric Cheng return (B_FALSE); 5118275SEric Cheng nexthdrp = &fraghdr->ip6f_nxt; 5128275SEric Cheng break; 5138275SEric Cheng case IPPROTO_NONE: 5148275SEric Cheng /* No next header means we're finished */ 5158275SEric Cheng default: 5168275SEric Cheng *hdr_length = length; 5178275SEric Cheng *next_hdr = *nexthdrp; 5188275SEric Cheng return (B_TRUE); 5198275SEric Cheng } 5208275SEric Cheng length += ehdrlen; 5218275SEric Cheng whereptr += ehdrlen; 5228275SEric Cheng *hdr_length = length; 5238275SEric Cheng *next_hdr = *nexthdrp; 5248275SEric Cheng } 5258275SEric Cheng switch (*nexthdrp) { 5268275SEric Cheng case IPPROTO_HOPOPTS: 5278275SEric Cheng case IPPROTO_DSTOPTS: 5288275SEric Cheng case IPPROTO_ROUTING: 5298275SEric Cheng case IPPROTO_FRAGMENT: 5308275SEric Cheng /* 5318275SEric Cheng * If any know extension headers are still to be processed, 5328275SEric Cheng * the packet's malformed (or at least all the IP header(s) are 5338275SEric Cheng * not in the same mblk - and that should never happen. 5348275SEric Cheng */ 5358275SEric Cheng return (B_FALSE); 5368275SEric Cheng 5378275SEric Cheng default: 5388275SEric Cheng /* 5398275SEric Cheng * If we get here, we know that all of the IP headers were in 5408275SEric Cheng * the same mblk, even if the ULP header is in the next mblk. 5418275SEric Cheng */ 5428275SEric Cheng *hdr_length = length; 5438275SEric Cheng *next_hdr = *nexthdrp; 5448275SEric Cheng return (B_TRUE); 5458275SEric Cheng } 5468275SEric Cheng } 5478275SEric Cheng 5488275SEric Cheng typedef struct mac_dladm_intr { 5498275SEric Cheng int ino; 5508275SEric Cheng int cpu_id; 5518275SEric Cheng char driver_path[MAXPATHLEN]; 5528275SEric Cheng char nexus_path[MAXPATHLEN]; 5538275SEric Cheng } mac_dladm_intr_t; 5548275SEric Cheng 5558275SEric Cheng /* Bind the interrupt to cpu_num */ 5568275SEric Cheng static int 5578275SEric Cheng mac_set_intr(ldi_handle_t lh, processorid_t cpu_num, int ino) 5588275SEric Cheng { 5598275SEric Cheng pcitool_intr_set_t iset; 5608275SEric Cheng int err; 5618275SEric Cheng 5628275SEric Cheng iset.ino = ino; 5638275SEric Cheng iset.cpu_id = cpu_num; 5648275SEric Cheng iset.user_version = PCITOOL_VERSION; 5658275SEric Cheng err = ldi_ioctl(lh, PCITOOL_DEVICE_SET_INTR, (intptr_t)&iset, FKIOCTL, 5668275SEric Cheng kcred, NULL); 5678275SEric Cheng 5688275SEric Cheng return (err); 5698275SEric Cheng } 5708275SEric Cheng 5718275SEric Cheng /* 5728275SEric Cheng * Search interrupt information. iget is filled in with the info to search 5738275SEric Cheng */ 5748275SEric Cheng static boolean_t 5758275SEric Cheng mac_search_intrinfo(pcitool_intr_get_t *iget_p, mac_dladm_intr_t *dln) 5768275SEric Cheng { 5778275SEric Cheng int i; 5788275SEric Cheng char driver_path[2 * MAXPATHLEN]; 5798275SEric Cheng 5808275SEric Cheng for (i = 0; i < iget_p->num_devs; i++) { 5818275SEric Cheng (void) strlcpy(driver_path, iget_p->dev[i].path, MAXPATHLEN); 5828275SEric Cheng (void) snprintf(&driver_path[strlen(driver_path)], MAXPATHLEN, 5838275SEric Cheng ":%s%d", iget_p->dev[i].driver_name, 5848275SEric Cheng iget_p->dev[i].dev_inst); 5858275SEric Cheng /* Match the device path for the device path */ 5868275SEric Cheng if (strcmp(driver_path, dln->driver_path) == 0) { 5878275SEric Cheng dln->ino = iget_p->ino; 5888275SEric Cheng dln->cpu_id = iget_p->cpu_id; 5898275SEric Cheng return (B_TRUE); 5908275SEric Cheng } 5918275SEric Cheng } 5928275SEric Cheng return (B_FALSE); 5938275SEric Cheng } 5948275SEric Cheng 5958275SEric Cheng /* 5968275SEric Cheng * Get information about ino, i.e. if this is the interrupt for our 5978275SEric Cheng * device and where it is bound etc. 5988275SEric Cheng */ 5998275SEric Cheng static boolean_t 6008275SEric Cheng mac_get_single_intr(ldi_handle_t lh, int ino, mac_dladm_intr_t *dln) 6018275SEric Cheng { 6028275SEric Cheng pcitool_intr_get_t *iget_p; 6038275SEric Cheng int ipsz; 6048275SEric Cheng int nipsz; 6058275SEric Cheng int err; 6068275SEric Cheng uint8_t inum; 6078275SEric Cheng 6088275SEric Cheng /* 6098275SEric Cheng * Check if SLEEP is OK, i.e if could come here in response to 6108275SEric Cheng * changing the fanout due to some callback from the driver, say 6118275SEric Cheng * link speed changes. 6128275SEric Cheng */ 6138275SEric Cheng ipsz = PCITOOL_IGET_SIZE(0); 6148275SEric Cheng iget_p = kmem_zalloc(ipsz, KM_SLEEP); 6158275SEric Cheng 6168275SEric Cheng iget_p->num_devs_ret = 0; 6178275SEric Cheng iget_p->user_version = PCITOOL_VERSION; 6188275SEric Cheng iget_p->ino = ino; 6198275SEric Cheng 6208275SEric Cheng err = ldi_ioctl(lh, PCITOOL_DEVICE_GET_INTR, (intptr_t)iget_p, 6218275SEric Cheng FKIOCTL, kcred, NULL); 6228275SEric Cheng if (err != 0) { 6238275SEric Cheng kmem_free(iget_p, ipsz); 6248275SEric Cheng return (B_FALSE); 6258275SEric Cheng } 6268275SEric Cheng if (iget_p->num_devs == 0) { 6278275SEric Cheng kmem_free(iget_p, ipsz); 6288275SEric Cheng return (B_FALSE); 6298275SEric Cheng } 6308275SEric Cheng inum = iget_p->num_devs; 6318275SEric Cheng if (iget_p->num_devs_ret < iget_p->num_devs) { 6328275SEric Cheng /* Reallocate */ 6338275SEric Cheng nipsz = PCITOOL_IGET_SIZE(iget_p->num_devs); 6348275SEric Cheng 6358275SEric Cheng kmem_free(iget_p, ipsz); 6368275SEric Cheng ipsz = nipsz; 6378275SEric Cheng iget_p = kmem_zalloc(ipsz, KM_SLEEP); 6388275SEric Cheng 6398275SEric Cheng iget_p->num_devs_ret = inum; 6408275SEric Cheng iget_p->ino = ino; 6418275SEric Cheng iget_p->user_version = PCITOOL_VERSION; 6428275SEric Cheng err = ldi_ioctl(lh, PCITOOL_DEVICE_GET_INTR, (intptr_t)iget_p, 6438275SEric Cheng FKIOCTL, kcred, NULL); 6448275SEric Cheng if (err != 0) { 6458275SEric Cheng kmem_free(iget_p, ipsz); 6468275SEric Cheng return (B_FALSE); 6478275SEric Cheng } 6488275SEric Cheng /* defensive */ 6498275SEric Cheng if (iget_p->num_devs != iget_p->num_devs_ret) { 6508275SEric Cheng kmem_free(iget_p, ipsz); 6518275SEric Cheng return (B_FALSE); 6528275SEric Cheng } 6538275SEric Cheng } 6548275SEric Cheng 6558275SEric Cheng if (mac_search_intrinfo(iget_p, dln)) { 6568275SEric Cheng kmem_free(iget_p, ipsz); 6578275SEric Cheng return (B_TRUE); 6588275SEric Cheng } 6598275SEric Cheng kmem_free(iget_p, ipsz); 6608275SEric Cheng return (B_FALSE); 6618275SEric Cheng } 6628275SEric Cheng 6638275SEric Cheng /* 6648275SEric Cheng * Get the interrupts and check each one to see if it is for our device. 6658275SEric Cheng */ 6668275SEric Cheng static int 6678275SEric Cheng mac_validate_intr(ldi_handle_t lh, mac_dladm_intr_t *dln, processorid_t cpuid) 6688275SEric Cheng { 6698275SEric Cheng pcitool_intr_info_t intr_info; 6708275SEric Cheng int err; 6718275SEric Cheng int ino; 6728275SEric Cheng 6738275SEric Cheng err = ldi_ioctl(lh, PCITOOL_SYSTEM_INTR_INFO, (intptr_t)&intr_info, 6748275SEric Cheng FKIOCTL, kcred, NULL); 6758275SEric Cheng if (err != 0) 6768275SEric Cheng return (-1); 6778275SEric Cheng 6788275SEric Cheng for (ino = 0; ino < intr_info.num_intr; ino++) { 6798275SEric Cheng if (mac_get_single_intr(lh, ino, dln)) { 6808275SEric Cheng if (dln->cpu_id == cpuid) 6818275SEric Cheng return (0); 6828275SEric Cheng return (1); 6838275SEric Cheng } 6848275SEric Cheng } 6858275SEric Cheng return (-1); 6868275SEric Cheng } 6878275SEric Cheng 6888275SEric Cheng /* 6898275SEric Cheng * Obtain the nexus parent node info. for mdip. 6908275SEric Cheng */ 6918275SEric Cheng static dev_info_t * 6928275SEric Cheng mac_get_nexus_node(dev_info_t *mdip, mac_dladm_intr_t *dln) 6938275SEric Cheng { 6948275SEric Cheng struct dev_info *tdip = (struct dev_info *)mdip; 6958275SEric Cheng struct ddi_minor_data *minordata; 6968275SEric Cheng int circ; 6978275SEric Cheng dev_info_t *pdip; 6988275SEric Cheng char pathname[MAXPATHLEN]; 6998275SEric Cheng 7008275SEric Cheng while (tdip != NULL) { 7018275SEric Cheng ndi_devi_enter((dev_info_t *)tdip, &circ); 7028275SEric Cheng for (minordata = tdip->devi_minor; minordata != NULL; 7038275SEric Cheng minordata = minordata->next) { 7048275SEric Cheng if (strncmp(minordata->ddm_node_type, DDI_NT_INTRCTL, 7058275SEric Cheng strlen(DDI_NT_INTRCTL)) == 0) { 7068275SEric Cheng pdip = minordata->dip; 7078275SEric Cheng (void) ddi_pathname(pdip, pathname); 7088275SEric Cheng (void) snprintf(dln->nexus_path, MAXPATHLEN, 7098275SEric Cheng "/devices%s:intr", pathname); 7108275SEric Cheng (void) ddi_pathname_minor(minordata, pathname); 7118275SEric Cheng ndi_devi_exit((dev_info_t *)tdip, circ); 7128275SEric Cheng return (pdip); 7138275SEric Cheng } 7148275SEric Cheng } 7158275SEric Cheng ndi_devi_exit((dev_info_t *)tdip, circ); 7168275SEric Cheng tdip = tdip->devi_parent; 7178275SEric Cheng } 7188275SEric Cheng return (NULL); 7198275SEric Cheng } 7208275SEric Cheng 7218275SEric Cheng /* 7228275SEric Cheng * For a primary MAC client, if the user has set a list or CPUs or 7238275SEric Cheng * we have obtained it implicitly, we try to retarget the interrupt 7248275SEric Cheng * for that device on one of the CPUs in the list. 7258275SEric Cheng * We assign the interrupt to the same CPU as the poll thread. 7268275SEric Cheng */ 7278275SEric Cheng static boolean_t 7288275SEric Cheng mac_check_interrupt_binding(dev_info_t *mdip, int32_t cpuid) 7298275SEric Cheng { 7308275SEric Cheng ldi_handle_t lh = NULL; 7318275SEric Cheng ldi_ident_t li = NULL; 7328275SEric Cheng int err; 7338275SEric Cheng int ret; 7348275SEric Cheng mac_dladm_intr_t dln; 7358275SEric Cheng dev_info_t *dip; 7368275SEric Cheng struct ddi_minor_data *minordata; 7378275SEric Cheng 7388275SEric Cheng dln.nexus_path[0] = '\0'; 7398275SEric Cheng dln.driver_path[0] = '\0'; 7408275SEric Cheng 7418275SEric Cheng minordata = ((struct dev_info *)mdip)->devi_minor; 7428275SEric Cheng while (minordata != NULL) { 7438275SEric Cheng if (minordata->type == DDM_MINOR) 7448275SEric Cheng break; 7458275SEric Cheng minordata = minordata->next; 7468275SEric Cheng } 7478275SEric Cheng if (minordata == NULL) 7488275SEric Cheng return (B_FALSE); 7498275SEric Cheng 7508275SEric Cheng (void) ddi_pathname_minor(minordata, dln.driver_path); 7518275SEric Cheng 7528275SEric Cheng dip = mac_get_nexus_node(mdip, &dln); 7538275SEric Cheng /* defensive */ 7548275SEric Cheng if (dip == NULL) 7558275SEric Cheng return (B_FALSE); 7568275SEric Cheng 7578275SEric Cheng err = ldi_ident_from_major(ddi_driver_major(dip), &li); 7588275SEric Cheng if (err != 0) 7598275SEric Cheng return (B_FALSE); 7608275SEric Cheng 7618275SEric Cheng err = ldi_open_by_name(dln.nexus_path, FREAD|FWRITE, kcred, &lh, li); 7628275SEric Cheng if (err != 0) 7638275SEric Cheng return (B_FALSE); 7648275SEric Cheng 7658275SEric Cheng ret = mac_validate_intr(lh, &dln, cpuid); 7668275SEric Cheng if (ret < 0) { 7678275SEric Cheng (void) ldi_close(lh, FREAD|FWRITE, kcred); 7688275SEric Cheng return (B_FALSE); 7698275SEric Cheng } 7708275SEric Cheng /* cmn_note? */ 7718275SEric Cheng if (ret != 0) 7728275SEric Cheng if ((err = (mac_set_intr(lh, cpuid, dln.ino))) != 0) { 7738275SEric Cheng (void) ldi_close(lh, FREAD|FWRITE, kcred); 7748275SEric Cheng return (B_FALSE); 7758275SEric Cheng } 7768275SEric Cheng (void) ldi_close(lh, FREAD|FWRITE, kcred); 7778275SEric Cheng return (B_TRUE); 7788275SEric Cheng } 7798275SEric Cheng 7808275SEric Cheng void 7818275SEric Cheng mac_client_set_intr_cpu(void *arg, mac_client_handle_t mch, int32_t cpuid) 7828275SEric Cheng { 7838275SEric Cheng dev_info_t *mdip = (dev_info_t *)arg; 7848275SEric Cheng mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 7858275SEric Cheng mac_resource_props_t *mrp; 7868275SEric Cheng mac_perim_handle_t mph; 7878275SEric Cheng 7888275SEric Cheng if (cpuid == -1 || !mac_check_interrupt_binding(mdip, cpuid)) 7898275SEric Cheng return; 7908275SEric Cheng 7918275SEric Cheng mac_perim_enter_by_mh((mac_handle_t)mcip->mci_mip, &mph); 7928275SEric Cheng mrp = MCIP_RESOURCE_PROPS(mcip); 7938275SEric Cheng mrp->mrp_intr_cpu = cpuid; 7948275SEric Cheng mac_perim_exit(mph); 7958275SEric Cheng } 7968275SEric Cheng 7978275SEric Cheng int32_t 7988275SEric Cheng mac_client_intr_cpu(mac_client_handle_t mch) 7998275SEric Cheng { 8008275SEric Cheng mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 8018275SEric Cheng mac_cpus_t *srs_cpu; 8028275SEric Cheng mac_soft_ring_set_t *rx_srs; 8038275SEric Cheng flow_entry_t *flent = mcip->mci_flent; 8048275SEric Cheng mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip); 8058275SEric Cheng 8068275SEric Cheng /* 8078275SEric Cheng * Check if we need to retarget the interrupt. We do this only 8088275SEric Cheng * for the primary MAC client. We do this if we have the only 8098275SEric Cheng * exclusive ring in the group. 8108275SEric Cheng */ 8118275SEric Cheng if (mac_is_primary_client(mcip) && flent->fe_rx_srs_cnt == 2) { 8128275SEric Cheng rx_srs = flent->fe_rx_srs[1]; 8138275SEric Cheng srs_cpu = &rx_srs->srs_cpu; 8148275SEric Cheng if (mrp->mrp_intr_cpu == srs_cpu->mc_pollid) 8158275SEric Cheng return (-1); 8168275SEric Cheng return (srs_cpu->mc_pollid); 8178275SEric Cheng } 8188275SEric Cheng return (-1); 8198275SEric Cheng } 8208275SEric Cheng 8218275SEric Cheng void * 8228275SEric Cheng mac_get_devinfo(mac_handle_t mh) 8238275SEric Cheng { 8248275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 8258275SEric Cheng 8268275SEric Cheng return ((void *)mip->mi_dip); 8278275SEric Cheng } 828*8833SVenu.Iyer@Sun.COM 829*8833SVenu.Iyer@Sun.COM #define PKT_HASH_4BYTES(x) ((x)[0] ^ (x)[1] ^ (x)[2] ^ (x)[3]) 830*8833SVenu.Iyer@Sun.COM #define PKT_HASH_MAC(x) ((x)[0] ^ (x)[1] ^ (x)[2] ^ (x)[3] ^ (x)[4] ^ (x)[5]) 831*8833SVenu.Iyer@Sun.COM 832*8833SVenu.Iyer@Sun.COM uint64_t 833*8833SVenu.Iyer@Sun.COM mac_pkt_hash(uint_t media, mblk_t *mp, uint8_t policy, boolean_t is_outbound) 834*8833SVenu.Iyer@Sun.COM { 835*8833SVenu.Iyer@Sun.COM struct ether_header *ehp; 836*8833SVenu.Iyer@Sun.COM uint64_t hash = 0; 837*8833SVenu.Iyer@Sun.COM uint16_t sap; 838*8833SVenu.Iyer@Sun.COM uint_t skip_len; 839*8833SVenu.Iyer@Sun.COM uint8_t proto; 840*8833SVenu.Iyer@Sun.COM 841*8833SVenu.Iyer@Sun.COM /* 842*8833SVenu.Iyer@Sun.COM * We may want to have one of these per MAC type plugin in the 843*8833SVenu.Iyer@Sun.COM * future. For now supports only ethernet. 844*8833SVenu.Iyer@Sun.COM */ 845*8833SVenu.Iyer@Sun.COM if (media != DL_ETHER) 846*8833SVenu.Iyer@Sun.COM return (0L); 847*8833SVenu.Iyer@Sun.COM 848*8833SVenu.Iyer@Sun.COM /* for now we support only outbound packets */ 849*8833SVenu.Iyer@Sun.COM ASSERT(is_outbound); 850*8833SVenu.Iyer@Sun.COM ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t))); 851*8833SVenu.Iyer@Sun.COM ASSERT(MBLKL(mp) >= sizeof (struct ether_header)); 852*8833SVenu.Iyer@Sun.COM 853*8833SVenu.Iyer@Sun.COM /* compute L2 hash */ 854*8833SVenu.Iyer@Sun.COM 855*8833SVenu.Iyer@Sun.COM ehp = (struct ether_header *)mp->b_rptr; 856*8833SVenu.Iyer@Sun.COM 857*8833SVenu.Iyer@Sun.COM if ((policy & MAC_PKT_HASH_L2) != 0) { 858*8833SVenu.Iyer@Sun.COM uchar_t *mac_src = ehp->ether_shost.ether_addr_octet; 859*8833SVenu.Iyer@Sun.COM uchar_t *mac_dst = ehp->ether_dhost.ether_addr_octet; 860*8833SVenu.Iyer@Sun.COM hash = PKT_HASH_MAC(mac_src) ^ PKT_HASH_MAC(mac_dst); 861*8833SVenu.Iyer@Sun.COM policy &= ~MAC_PKT_HASH_L2; 862*8833SVenu.Iyer@Sun.COM } 863*8833SVenu.Iyer@Sun.COM 864*8833SVenu.Iyer@Sun.COM if (policy == 0) 865*8833SVenu.Iyer@Sun.COM goto done; 866*8833SVenu.Iyer@Sun.COM 867*8833SVenu.Iyer@Sun.COM /* skip ethernet header */ 868*8833SVenu.Iyer@Sun.COM 869*8833SVenu.Iyer@Sun.COM sap = ntohs(ehp->ether_type); 870*8833SVenu.Iyer@Sun.COM if (sap == ETHERTYPE_VLAN) { 871*8833SVenu.Iyer@Sun.COM struct ether_vlan_header *evhp; 872*8833SVenu.Iyer@Sun.COM mblk_t *newmp = NULL; 873*8833SVenu.Iyer@Sun.COM 874*8833SVenu.Iyer@Sun.COM skip_len = sizeof (struct ether_vlan_header); 875*8833SVenu.Iyer@Sun.COM if (MBLKL(mp) < skip_len) { 876*8833SVenu.Iyer@Sun.COM /* the vlan tag is the payload, pull up first */ 877*8833SVenu.Iyer@Sun.COM newmp = msgpullup(mp, -1); 878*8833SVenu.Iyer@Sun.COM if ((newmp == NULL) || (MBLKL(newmp) < skip_len)) { 879*8833SVenu.Iyer@Sun.COM goto done; 880*8833SVenu.Iyer@Sun.COM } 881*8833SVenu.Iyer@Sun.COM evhp = (struct ether_vlan_header *)newmp->b_rptr; 882*8833SVenu.Iyer@Sun.COM } else { 883*8833SVenu.Iyer@Sun.COM evhp = (struct ether_vlan_header *)mp->b_rptr; 884*8833SVenu.Iyer@Sun.COM } 885*8833SVenu.Iyer@Sun.COM 886*8833SVenu.Iyer@Sun.COM sap = ntohs(evhp->ether_type); 887*8833SVenu.Iyer@Sun.COM freemsg(newmp); 888*8833SVenu.Iyer@Sun.COM } else { 889*8833SVenu.Iyer@Sun.COM skip_len = sizeof (struct ether_header); 890*8833SVenu.Iyer@Sun.COM } 891*8833SVenu.Iyer@Sun.COM 892*8833SVenu.Iyer@Sun.COM /* if ethernet header is in its own mblk, skip it */ 893*8833SVenu.Iyer@Sun.COM if (MBLKL(mp) <= skip_len) { 894*8833SVenu.Iyer@Sun.COM skip_len -= MBLKL(mp); 895*8833SVenu.Iyer@Sun.COM mp = mp->b_cont; 896*8833SVenu.Iyer@Sun.COM if (mp == NULL) 897*8833SVenu.Iyer@Sun.COM goto done; 898*8833SVenu.Iyer@Sun.COM } 899*8833SVenu.Iyer@Sun.COM 900*8833SVenu.Iyer@Sun.COM sap = (sap < ETHERTYPE_802_MIN) ? 0 : sap; 901*8833SVenu.Iyer@Sun.COM 902*8833SVenu.Iyer@Sun.COM /* compute IP src/dst addresses hash and skip IPv{4,6} header */ 903*8833SVenu.Iyer@Sun.COM 904*8833SVenu.Iyer@Sun.COM switch (sap) { 905*8833SVenu.Iyer@Sun.COM case ETHERTYPE_IP: { 906*8833SVenu.Iyer@Sun.COM ipha_t *iphp; 907*8833SVenu.Iyer@Sun.COM 908*8833SVenu.Iyer@Sun.COM /* 909*8833SVenu.Iyer@Sun.COM * If the header is not aligned or the header doesn't fit 910*8833SVenu.Iyer@Sun.COM * in the mblk, bail now. Note that this may cause packets 911*8833SVenu.Iyer@Sun.COM * reordering. 912*8833SVenu.Iyer@Sun.COM */ 913*8833SVenu.Iyer@Sun.COM iphp = (ipha_t *)(mp->b_rptr + skip_len); 914*8833SVenu.Iyer@Sun.COM if (((unsigned char *)iphp + sizeof (ipha_t) > mp->b_wptr) || 915*8833SVenu.Iyer@Sun.COM !OK_32PTR((char *)iphp)) 916*8833SVenu.Iyer@Sun.COM goto done; 917*8833SVenu.Iyer@Sun.COM 918*8833SVenu.Iyer@Sun.COM proto = iphp->ipha_protocol; 919*8833SVenu.Iyer@Sun.COM skip_len += IPH_HDR_LENGTH(iphp); 920*8833SVenu.Iyer@Sun.COM 921*8833SVenu.Iyer@Sun.COM if ((policy & MAC_PKT_HASH_L3) != 0) { 922*8833SVenu.Iyer@Sun.COM uint8_t *ip_src = (uint8_t *)&(iphp->ipha_src); 923*8833SVenu.Iyer@Sun.COM uint8_t *ip_dst = (uint8_t *)&(iphp->ipha_dst); 924*8833SVenu.Iyer@Sun.COM 925*8833SVenu.Iyer@Sun.COM hash ^= (PKT_HASH_4BYTES(ip_src) ^ 926*8833SVenu.Iyer@Sun.COM PKT_HASH_4BYTES(ip_dst)); 927*8833SVenu.Iyer@Sun.COM policy &= ~MAC_PKT_HASH_L3; 928*8833SVenu.Iyer@Sun.COM } 929*8833SVenu.Iyer@Sun.COM break; 930*8833SVenu.Iyer@Sun.COM } 931*8833SVenu.Iyer@Sun.COM case ETHERTYPE_IPV6: { 932*8833SVenu.Iyer@Sun.COM ip6_t *ip6hp; 933*8833SVenu.Iyer@Sun.COM uint16_t hdr_length; 934*8833SVenu.Iyer@Sun.COM 935*8833SVenu.Iyer@Sun.COM /* 936*8833SVenu.Iyer@Sun.COM * If the header is not aligned or the header doesn't fit 937*8833SVenu.Iyer@Sun.COM * in the mblk, bail now. Note that this may cause packets 938*8833SVenu.Iyer@Sun.COM * reordering. 939*8833SVenu.Iyer@Sun.COM */ 940*8833SVenu.Iyer@Sun.COM 941*8833SVenu.Iyer@Sun.COM ip6hp = (ip6_t *)(mp->b_rptr + skip_len); 942*8833SVenu.Iyer@Sun.COM if (((unsigned char *)ip6hp + IPV6_HDR_LEN > mp->b_wptr) || 943*8833SVenu.Iyer@Sun.COM !OK_32PTR((char *)ip6hp)) 944*8833SVenu.Iyer@Sun.COM goto done; 945*8833SVenu.Iyer@Sun.COM 946*8833SVenu.Iyer@Sun.COM if (!mac_ip_hdr_length_v6(mp, ip6hp, &hdr_length, &proto)) 947*8833SVenu.Iyer@Sun.COM goto done; 948*8833SVenu.Iyer@Sun.COM skip_len += hdr_length; 949*8833SVenu.Iyer@Sun.COM 950*8833SVenu.Iyer@Sun.COM if ((policy & MAC_PKT_HASH_L3) != 0) { 951*8833SVenu.Iyer@Sun.COM uint8_t *ip_src = &(ip6hp->ip6_src.s6_addr8[12]); 952*8833SVenu.Iyer@Sun.COM uint8_t *ip_dst = &(ip6hp->ip6_dst.s6_addr8[12]); 953*8833SVenu.Iyer@Sun.COM 954*8833SVenu.Iyer@Sun.COM hash ^= (PKT_HASH_4BYTES(ip_src) ^ 955*8833SVenu.Iyer@Sun.COM PKT_HASH_4BYTES(ip_dst)); 956*8833SVenu.Iyer@Sun.COM policy &= ~MAC_PKT_HASH_L3; 957*8833SVenu.Iyer@Sun.COM } 958*8833SVenu.Iyer@Sun.COM break; 959*8833SVenu.Iyer@Sun.COM } 960*8833SVenu.Iyer@Sun.COM default: 961*8833SVenu.Iyer@Sun.COM goto done; 962*8833SVenu.Iyer@Sun.COM } 963*8833SVenu.Iyer@Sun.COM 964*8833SVenu.Iyer@Sun.COM if (policy == 0) 965*8833SVenu.Iyer@Sun.COM goto done; 966*8833SVenu.Iyer@Sun.COM 967*8833SVenu.Iyer@Sun.COM /* if ip header is in its own mblk, skip it */ 968*8833SVenu.Iyer@Sun.COM if (MBLKL(mp) <= skip_len) { 969*8833SVenu.Iyer@Sun.COM skip_len -= MBLKL(mp); 970*8833SVenu.Iyer@Sun.COM mp = mp->b_cont; 971*8833SVenu.Iyer@Sun.COM if (mp == NULL) 972*8833SVenu.Iyer@Sun.COM goto done; 973*8833SVenu.Iyer@Sun.COM } 974*8833SVenu.Iyer@Sun.COM 975*8833SVenu.Iyer@Sun.COM /* parse ULP header */ 976*8833SVenu.Iyer@Sun.COM again: 977*8833SVenu.Iyer@Sun.COM switch (proto) { 978*8833SVenu.Iyer@Sun.COM case IPPROTO_TCP: 979*8833SVenu.Iyer@Sun.COM case IPPROTO_UDP: 980*8833SVenu.Iyer@Sun.COM case IPPROTO_ESP: 981*8833SVenu.Iyer@Sun.COM case IPPROTO_SCTP: 982*8833SVenu.Iyer@Sun.COM /* 983*8833SVenu.Iyer@Sun.COM * These Internet Protocols are intentionally designed 984*8833SVenu.Iyer@Sun.COM * for hashing from the git-go. Port numbers are in the first 985*8833SVenu.Iyer@Sun.COM * word for transports, SPI is first for ESP. 986*8833SVenu.Iyer@Sun.COM */ 987*8833SVenu.Iyer@Sun.COM if (mp->b_rptr + skip_len + 4 > mp->b_wptr) 988*8833SVenu.Iyer@Sun.COM goto done; 989*8833SVenu.Iyer@Sun.COM hash ^= PKT_HASH_4BYTES((mp->b_rptr + skip_len)); 990*8833SVenu.Iyer@Sun.COM break; 991*8833SVenu.Iyer@Sun.COM 992*8833SVenu.Iyer@Sun.COM case IPPROTO_AH: { 993*8833SVenu.Iyer@Sun.COM ah_t *ah = (ah_t *)(mp->b_rptr + skip_len); 994*8833SVenu.Iyer@Sun.COM uint_t ah_length = AH_TOTAL_LEN(ah); 995*8833SVenu.Iyer@Sun.COM 996*8833SVenu.Iyer@Sun.COM if ((unsigned char *)ah + sizeof (ah_t) > mp->b_wptr) 997*8833SVenu.Iyer@Sun.COM goto done; 998*8833SVenu.Iyer@Sun.COM 999*8833SVenu.Iyer@Sun.COM proto = ah->ah_nexthdr; 1000*8833SVenu.Iyer@Sun.COM skip_len += ah_length; 1001*8833SVenu.Iyer@Sun.COM 1002*8833SVenu.Iyer@Sun.COM /* if AH header is in its own mblk, skip it */ 1003*8833SVenu.Iyer@Sun.COM if (MBLKL(mp) <= skip_len) { 1004*8833SVenu.Iyer@Sun.COM skip_len -= MBLKL(mp); 1005*8833SVenu.Iyer@Sun.COM mp = mp->b_cont; 1006*8833SVenu.Iyer@Sun.COM if (mp == NULL) 1007*8833SVenu.Iyer@Sun.COM goto done; 1008*8833SVenu.Iyer@Sun.COM } 1009*8833SVenu.Iyer@Sun.COM 1010*8833SVenu.Iyer@Sun.COM goto again; 1011*8833SVenu.Iyer@Sun.COM } 1012*8833SVenu.Iyer@Sun.COM } 1013*8833SVenu.Iyer@Sun.COM 1014*8833SVenu.Iyer@Sun.COM done: 1015*8833SVenu.Iyer@Sun.COM return (hash); 1016*8833SVenu.Iyer@Sun.COM } 1017