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*11528SBaban.Kenkre@Sun.COM * Copyright 2010 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> 478833SVenu.Iyer@Sun.COM #include <inet/ipsec_impl.h> 488833SVenu.Iyer@Sun.COM #include <inet/sadb.h> 498833SVenu.Iyer@Sun.COM #include <inet/ipsecesp.h> 508833SVenu.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, 465*11528SBaban.Kenkre@Sun.COM uint8_t *next_hdr, boolean_t *ip_fragmented, uint32_t *ip_frag_ident) 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); 47911042SErik.Nordmark@Sun.COM ASSERT(IPH_HDR_VERSION(ip6h) == IPV6_VERSION); 4808275SEric Cheng length = IPV6_HDR_LEN; 4818275SEric Cheng whereptr = ((uint8_t *)&ip6h[1]); /* point to next hdr */ 4828275SEric Cheng 483*11528SBaban.Kenkre@Sun.COM if (ip_fragmented != NULL) 484*11528SBaban.Kenkre@Sun.COM *ip_fragmented = B_FALSE; 485*11528SBaban.Kenkre@Sun.COM 4868275SEric Cheng nexthdrp = &ip6h->ip6_nxt; 4878275SEric Cheng while (whereptr < endptr) { 4888275SEric Cheng /* Is there enough left for len + nexthdr? */ 4898275SEric Cheng if (whereptr + MIN_EHDR_LEN > endptr) 4908275SEric Cheng break; 4918275SEric Cheng 4928275SEric Cheng switch (*nexthdrp) { 4938275SEric Cheng case IPPROTO_HOPOPTS: 4948275SEric Cheng case IPPROTO_DSTOPTS: 4958275SEric Cheng /* Assumes the headers are identical for hbh and dst */ 4968275SEric Cheng desthdr = (ip6_dest_t *)whereptr; 4978275SEric Cheng ehdrlen = 8 * (desthdr->ip6d_len + 1); 4988275SEric Cheng if ((uchar_t *)desthdr + ehdrlen > endptr) 4998275SEric Cheng return (B_FALSE); 5008275SEric Cheng nexthdrp = &desthdr->ip6d_nxt; 5018275SEric Cheng break; 5028275SEric Cheng case IPPROTO_ROUTING: 5038275SEric Cheng rthdr = (ip6_rthdr_t *)whereptr; 5048275SEric Cheng ehdrlen = 8 * (rthdr->ip6r_len + 1); 5058275SEric Cheng if ((uchar_t *)rthdr + ehdrlen > endptr) 5068275SEric Cheng return (B_FALSE); 5078275SEric Cheng nexthdrp = &rthdr->ip6r_nxt; 5088275SEric Cheng break; 5098275SEric Cheng case IPPROTO_FRAGMENT: 5108275SEric Cheng fraghdr = (ip6_frag_t *)whereptr; 5118275SEric Cheng ehdrlen = sizeof (ip6_frag_t); 5128275SEric Cheng if ((uchar_t *)&fraghdr[1] > endptr) 5138275SEric Cheng return (B_FALSE); 5148275SEric Cheng nexthdrp = &fraghdr->ip6f_nxt; 515*11528SBaban.Kenkre@Sun.COM if (ip_fragmented != NULL) 516*11528SBaban.Kenkre@Sun.COM *ip_fragmented = B_TRUE; 517*11528SBaban.Kenkre@Sun.COM if (ip_frag_ident != NULL) 518*11528SBaban.Kenkre@Sun.COM *ip_frag_ident = fraghdr->ip6f_ident; 5198275SEric Cheng break; 5208275SEric Cheng case IPPROTO_NONE: 5218275SEric Cheng /* No next header means we're finished */ 5228275SEric Cheng default: 5238275SEric Cheng *hdr_length = length; 5248275SEric Cheng *next_hdr = *nexthdrp; 5258275SEric Cheng return (B_TRUE); 5268275SEric Cheng } 5278275SEric Cheng length += ehdrlen; 5288275SEric Cheng whereptr += ehdrlen; 5298275SEric Cheng *hdr_length = length; 5308275SEric Cheng *next_hdr = *nexthdrp; 5318275SEric Cheng } 5328275SEric Cheng switch (*nexthdrp) { 5338275SEric Cheng case IPPROTO_HOPOPTS: 5348275SEric Cheng case IPPROTO_DSTOPTS: 5358275SEric Cheng case IPPROTO_ROUTING: 5368275SEric Cheng case IPPROTO_FRAGMENT: 5378275SEric Cheng /* 5388275SEric Cheng * If any know extension headers are still to be processed, 5398275SEric Cheng * the packet's malformed (or at least all the IP header(s) are 5408275SEric Cheng * not in the same mblk - and that should never happen. 5418275SEric Cheng */ 5428275SEric Cheng return (B_FALSE); 5438275SEric Cheng 5448275SEric Cheng default: 5458275SEric Cheng /* 5468275SEric Cheng * If we get here, we know that all of the IP headers were in 5478275SEric Cheng * the same mblk, even if the ULP header is in the next mblk. 5488275SEric Cheng */ 5498275SEric Cheng *hdr_length = length; 5508275SEric Cheng *next_hdr = *nexthdrp; 5518275SEric Cheng return (B_TRUE); 5528275SEric Cheng } 5538275SEric Cheng } 5548275SEric Cheng 5558275SEric Cheng typedef struct mac_dladm_intr { 5568275SEric Cheng int ino; 5578275SEric Cheng int cpu_id; 5588275SEric Cheng char driver_path[MAXPATHLEN]; 5598275SEric Cheng char nexus_path[MAXPATHLEN]; 5608275SEric Cheng } mac_dladm_intr_t; 5618275SEric Cheng 5628275SEric Cheng /* Bind the interrupt to cpu_num */ 5638275SEric Cheng static int 5648275SEric Cheng mac_set_intr(ldi_handle_t lh, processorid_t cpu_num, int ino) 5658275SEric Cheng { 5668275SEric Cheng pcitool_intr_set_t iset; 5678275SEric Cheng int err; 5688275SEric Cheng 5698275SEric Cheng iset.ino = ino; 5708275SEric Cheng iset.cpu_id = cpu_num; 5718275SEric Cheng iset.user_version = PCITOOL_VERSION; 5728275SEric Cheng err = ldi_ioctl(lh, PCITOOL_DEVICE_SET_INTR, (intptr_t)&iset, FKIOCTL, 5738275SEric Cheng kcred, NULL); 5748275SEric Cheng 5758275SEric Cheng return (err); 5768275SEric Cheng } 5778275SEric Cheng 5788275SEric Cheng /* 5798275SEric Cheng * Search interrupt information. iget is filled in with the info to search 5808275SEric Cheng */ 5818275SEric Cheng static boolean_t 5828275SEric Cheng mac_search_intrinfo(pcitool_intr_get_t *iget_p, mac_dladm_intr_t *dln) 5838275SEric Cheng { 5848275SEric Cheng int i; 5858275SEric Cheng char driver_path[2 * MAXPATHLEN]; 5868275SEric Cheng 5878275SEric Cheng for (i = 0; i < iget_p->num_devs; i++) { 5888275SEric Cheng (void) strlcpy(driver_path, iget_p->dev[i].path, MAXPATHLEN); 5898275SEric Cheng (void) snprintf(&driver_path[strlen(driver_path)], MAXPATHLEN, 5908275SEric Cheng ":%s%d", iget_p->dev[i].driver_name, 5918275SEric Cheng iget_p->dev[i].dev_inst); 5928275SEric Cheng /* Match the device path for the device path */ 5938275SEric Cheng if (strcmp(driver_path, dln->driver_path) == 0) { 5948275SEric Cheng dln->ino = iget_p->ino; 5958275SEric Cheng dln->cpu_id = iget_p->cpu_id; 5968275SEric Cheng return (B_TRUE); 5978275SEric Cheng } 5988275SEric Cheng } 5998275SEric Cheng return (B_FALSE); 6008275SEric Cheng } 6018275SEric Cheng 6028275SEric Cheng /* 6038275SEric Cheng * Get information about ino, i.e. if this is the interrupt for our 6048275SEric Cheng * device and where it is bound etc. 6058275SEric Cheng */ 6068275SEric Cheng static boolean_t 6078275SEric Cheng mac_get_single_intr(ldi_handle_t lh, int ino, mac_dladm_intr_t *dln) 6088275SEric Cheng { 6098275SEric Cheng pcitool_intr_get_t *iget_p; 6108275SEric Cheng int ipsz; 6118275SEric Cheng int nipsz; 6128275SEric Cheng int err; 6138275SEric Cheng uint8_t inum; 6148275SEric Cheng 6158275SEric Cheng /* 6168275SEric Cheng * Check if SLEEP is OK, i.e if could come here in response to 6178275SEric Cheng * changing the fanout due to some callback from the driver, say 6188275SEric Cheng * link speed changes. 6198275SEric Cheng */ 6208275SEric Cheng ipsz = PCITOOL_IGET_SIZE(0); 6218275SEric Cheng iget_p = kmem_zalloc(ipsz, KM_SLEEP); 6228275SEric Cheng 6238275SEric Cheng iget_p->num_devs_ret = 0; 6248275SEric Cheng iget_p->user_version = PCITOOL_VERSION; 6258275SEric Cheng iget_p->ino = ino; 6268275SEric Cheng 6278275SEric Cheng err = ldi_ioctl(lh, PCITOOL_DEVICE_GET_INTR, (intptr_t)iget_p, 6288275SEric Cheng FKIOCTL, kcred, NULL); 6298275SEric Cheng if (err != 0) { 6308275SEric Cheng kmem_free(iget_p, ipsz); 6318275SEric Cheng return (B_FALSE); 6328275SEric Cheng } 6338275SEric Cheng if (iget_p->num_devs == 0) { 6348275SEric Cheng kmem_free(iget_p, ipsz); 6358275SEric Cheng return (B_FALSE); 6368275SEric Cheng } 6378275SEric Cheng inum = iget_p->num_devs; 6388275SEric Cheng if (iget_p->num_devs_ret < iget_p->num_devs) { 6398275SEric Cheng /* Reallocate */ 6408275SEric Cheng nipsz = PCITOOL_IGET_SIZE(iget_p->num_devs); 6418275SEric Cheng 6428275SEric Cheng kmem_free(iget_p, ipsz); 6438275SEric Cheng ipsz = nipsz; 6448275SEric Cheng iget_p = kmem_zalloc(ipsz, KM_SLEEP); 6458275SEric Cheng 6468275SEric Cheng iget_p->num_devs_ret = inum; 6478275SEric Cheng iget_p->ino = ino; 6488275SEric Cheng iget_p->user_version = PCITOOL_VERSION; 6498275SEric Cheng err = ldi_ioctl(lh, PCITOOL_DEVICE_GET_INTR, (intptr_t)iget_p, 6508275SEric Cheng FKIOCTL, kcred, NULL); 6518275SEric Cheng if (err != 0) { 6528275SEric Cheng kmem_free(iget_p, ipsz); 6538275SEric Cheng return (B_FALSE); 6548275SEric Cheng } 6558275SEric Cheng /* defensive */ 6568275SEric Cheng if (iget_p->num_devs != iget_p->num_devs_ret) { 6578275SEric Cheng kmem_free(iget_p, ipsz); 6588275SEric Cheng return (B_FALSE); 6598275SEric Cheng } 6608275SEric Cheng } 6618275SEric Cheng 6628275SEric Cheng if (mac_search_intrinfo(iget_p, dln)) { 6638275SEric Cheng kmem_free(iget_p, ipsz); 6648275SEric Cheng return (B_TRUE); 6658275SEric Cheng } 6668275SEric Cheng kmem_free(iget_p, ipsz); 6678275SEric Cheng return (B_FALSE); 6688275SEric Cheng } 6698275SEric Cheng 6708275SEric Cheng /* 6718275SEric Cheng * Get the interrupts and check each one to see if it is for our device. 6728275SEric Cheng */ 6738275SEric Cheng static int 6748275SEric Cheng mac_validate_intr(ldi_handle_t lh, mac_dladm_intr_t *dln, processorid_t cpuid) 6758275SEric Cheng { 6768275SEric Cheng pcitool_intr_info_t intr_info; 6778275SEric Cheng int err; 6788275SEric Cheng int ino; 6798275SEric Cheng 6808275SEric Cheng err = ldi_ioctl(lh, PCITOOL_SYSTEM_INTR_INFO, (intptr_t)&intr_info, 6818275SEric Cheng FKIOCTL, kcred, NULL); 6828275SEric Cheng if (err != 0) 6838275SEric Cheng return (-1); 6848275SEric Cheng 6858275SEric Cheng for (ino = 0; ino < intr_info.num_intr; ino++) { 6868275SEric Cheng if (mac_get_single_intr(lh, ino, dln)) { 6878275SEric Cheng if (dln->cpu_id == cpuid) 6888275SEric Cheng return (0); 6898275SEric Cheng return (1); 6908275SEric Cheng } 6918275SEric Cheng } 6928275SEric Cheng return (-1); 6938275SEric Cheng } 6948275SEric Cheng 6958275SEric Cheng /* 6968275SEric Cheng * Obtain the nexus parent node info. for mdip. 6978275SEric Cheng */ 6988275SEric Cheng static dev_info_t * 6998275SEric Cheng mac_get_nexus_node(dev_info_t *mdip, mac_dladm_intr_t *dln) 7008275SEric Cheng { 7018275SEric Cheng struct dev_info *tdip = (struct dev_info *)mdip; 7028275SEric Cheng struct ddi_minor_data *minordata; 7038275SEric Cheng int circ; 7048275SEric Cheng dev_info_t *pdip; 7058275SEric Cheng char pathname[MAXPATHLEN]; 7068275SEric Cheng 7078275SEric Cheng while (tdip != NULL) { 7089359SEric Cheng /* 7099359SEric Cheng * The netboot code could call this function while walking the 7109359SEric Cheng * device tree so we need to use ndi_devi_tryenter() here to 7119359SEric Cheng * avoid deadlock. 7129359SEric Cheng */ 7139359SEric Cheng if (ndi_devi_tryenter((dev_info_t *)tdip, &circ) == 0) 7149359SEric Cheng break; 7159359SEric Cheng 7168275SEric Cheng for (minordata = tdip->devi_minor; minordata != NULL; 7178275SEric Cheng minordata = minordata->next) { 7188275SEric Cheng if (strncmp(minordata->ddm_node_type, DDI_NT_INTRCTL, 7198275SEric Cheng strlen(DDI_NT_INTRCTL)) == 0) { 7208275SEric Cheng pdip = minordata->dip; 7218275SEric Cheng (void) ddi_pathname(pdip, pathname); 7228275SEric Cheng (void) snprintf(dln->nexus_path, MAXPATHLEN, 7238275SEric Cheng "/devices%s:intr", pathname); 7248275SEric Cheng (void) ddi_pathname_minor(minordata, pathname); 7258275SEric Cheng ndi_devi_exit((dev_info_t *)tdip, circ); 7268275SEric Cheng return (pdip); 7278275SEric Cheng } 7288275SEric Cheng } 7298275SEric Cheng ndi_devi_exit((dev_info_t *)tdip, circ); 7308275SEric Cheng tdip = tdip->devi_parent; 7318275SEric Cheng } 7328275SEric Cheng return (NULL); 7338275SEric Cheng } 7348275SEric Cheng 7358275SEric Cheng /* 7368275SEric Cheng * For a primary MAC client, if the user has set a list or CPUs or 7378275SEric Cheng * we have obtained it implicitly, we try to retarget the interrupt 7388275SEric Cheng * for that device on one of the CPUs in the list. 7398275SEric Cheng * We assign the interrupt to the same CPU as the poll thread. 7408275SEric Cheng */ 7418275SEric Cheng static boolean_t 7428275SEric Cheng mac_check_interrupt_binding(dev_info_t *mdip, int32_t cpuid) 7438275SEric Cheng { 7448275SEric Cheng ldi_handle_t lh = NULL; 7458275SEric Cheng ldi_ident_t li = NULL; 7468275SEric Cheng int err; 7478275SEric Cheng int ret; 7488275SEric Cheng mac_dladm_intr_t dln; 7498275SEric Cheng dev_info_t *dip; 7508275SEric Cheng struct ddi_minor_data *minordata; 7518275SEric Cheng 7528275SEric Cheng dln.nexus_path[0] = '\0'; 7538275SEric Cheng dln.driver_path[0] = '\0'; 7548275SEric Cheng 7558275SEric Cheng minordata = ((struct dev_info *)mdip)->devi_minor; 7568275SEric Cheng while (minordata != NULL) { 7578275SEric Cheng if (minordata->type == DDM_MINOR) 7588275SEric Cheng break; 7598275SEric Cheng minordata = minordata->next; 7608275SEric Cheng } 7618275SEric Cheng if (minordata == NULL) 7628275SEric Cheng return (B_FALSE); 7638275SEric Cheng 7648275SEric Cheng (void) ddi_pathname_minor(minordata, dln.driver_path); 7658275SEric Cheng 7668275SEric Cheng dip = mac_get_nexus_node(mdip, &dln); 7678275SEric Cheng /* defensive */ 7688275SEric Cheng if (dip == NULL) 7698275SEric Cheng return (B_FALSE); 7708275SEric Cheng 7718275SEric Cheng err = ldi_ident_from_major(ddi_driver_major(dip), &li); 7728275SEric Cheng if (err != 0) 7738275SEric Cheng return (B_FALSE); 7748275SEric Cheng 7758275SEric Cheng err = ldi_open_by_name(dln.nexus_path, FREAD|FWRITE, kcred, &lh, li); 7768275SEric Cheng if (err != 0) 7778275SEric Cheng return (B_FALSE); 7788275SEric Cheng 7798275SEric Cheng ret = mac_validate_intr(lh, &dln, cpuid); 7808275SEric Cheng if (ret < 0) { 7818275SEric Cheng (void) ldi_close(lh, FREAD|FWRITE, kcred); 7828275SEric Cheng return (B_FALSE); 7838275SEric Cheng } 7848275SEric Cheng /* cmn_note? */ 7858275SEric Cheng if (ret != 0) 7868275SEric Cheng if ((err = (mac_set_intr(lh, cpuid, dln.ino))) != 0) { 7878275SEric Cheng (void) ldi_close(lh, FREAD|FWRITE, kcred); 7888275SEric Cheng return (B_FALSE); 7898275SEric Cheng } 7908275SEric Cheng (void) ldi_close(lh, FREAD|FWRITE, kcred); 7918275SEric Cheng return (B_TRUE); 7928275SEric Cheng } 7938275SEric Cheng 7948275SEric Cheng void 7958275SEric Cheng mac_client_set_intr_cpu(void *arg, mac_client_handle_t mch, int32_t cpuid) 7968275SEric Cheng { 7978275SEric Cheng dev_info_t *mdip = (dev_info_t *)arg; 7988275SEric Cheng mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 7998275SEric Cheng mac_resource_props_t *mrp; 8008275SEric Cheng mac_perim_handle_t mph; 8018275SEric Cheng 8028275SEric Cheng if (cpuid == -1 || !mac_check_interrupt_binding(mdip, cpuid)) 8038275SEric Cheng return; 8048275SEric Cheng 8058275SEric Cheng mac_perim_enter_by_mh((mac_handle_t)mcip->mci_mip, &mph); 8068275SEric Cheng mrp = MCIP_RESOURCE_PROPS(mcip); 8078275SEric Cheng mrp->mrp_intr_cpu = cpuid; 8088275SEric Cheng mac_perim_exit(mph); 8098275SEric Cheng } 8108275SEric Cheng 8118275SEric Cheng int32_t 8128275SEric Cheng mac_client_intr_cpu(mac_client_handle_t mch) 8138275SEric Cheng { 8148275SEric Cheng mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 8158275SEric Cheng mac_cpus_t *srs_cpu; 8168275SEric Cheng mac_soft_ring_set_t *rx_srs; 8178275SEric Cheng flow_entry_t *flent = mcip->mci_flent; 8188275SEric Cheng mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip); 8198275SEric Cheng 8208275SEric Cheng /* 8218275SEric Cheng * Check if we need to retarget the interrupt. We do this only 8228275SEric Cheng * for the primary MAC client. We do this if we have the only 8238275SEric Cheng * exclusive ring in the group. 8248275SEric Cheng */ 8258275SEric Cheng if (mac_is_primary_client(mcip) && flent->fe_rx_srs_cnt == 2) { 8268275SEric Cheng rx_srs = flent->fe_rx_srs[1]; 8278275SEric Cheng srs_cpu = &rx_srs->srs_cpu; 8288275SEric Cheng if (mrp->mrp_intr_cpu == srs_cpu->mc_pollid) 8298275SEric Cheng return (-1); 8308275SEric Cheng return (srs_cpu->mc_pollid); 8318275SEric Cheng } 8328275SEric Cheng return (-1); 8338275SEric Cheng } 8348275SEric Cheng 8358275SEric Cheng void * 8368275SEric Cheng mac_get_devinfo(mac_handle_t mh) 8378275SEric Cheng { 8388275SEric Cheng mac_impl_t *mip = (mac_impl_t *)mh; 8398275SEric Cheng 8408275SEric Cheng return ((void *)mip->mi_dip); 8418275SEric Cheng } 8428833SVenu.Iyer@Sun.COM 843*11528SBaban.Kenkre@Sun.COM #define PKT_HASH_2BYTES(x) ((x)[0] ^ (x)[1]) 8448833SVenu.Iyer@Sun.COM #define PKT_HASH_4BYTES(x) ((x)[0] ^ (x)[1] ^ (x)[2] ^ (x)[3]) 8458833SVenu.Iyer@Sun.COM #define PKT_HASH_MAC(x) ((x)[0] ^ (x)[1] ^ (x)[2] ^ (x)[3] ^ (x)[4] ^ (x)[5]) 8468833SVenu.Iyer@Sun.COM 8478833SVenu.Iyer@Sun.COM uint64_t 8488833SVenu.Iyer@Sun.COM mac_pkt_hash(uint_t media, mblk_t *mp, uint8_t policy, boolean_t is_outbound) 8498833SVenu.Iyer@Sun.COM { 8508833SVenu.Iyer@Sun.COM struct ether_header *ehp; 8518833SVenu.Iyer@Sun.COM uint64_t hash = 0; 8528833SVenu.Iyer@Sun.COM uint16_t sap; 8538833SVenu.Iyer@Sun.COM uint_t skip_len; 8548833SVenu.Iyer@Sun.COM uint8_t proto; 855*11528SBaban.Kenkre@Sun.COM boolean_t ip_fragmented; 8568833SVenu.Iyer@Sun.COM 8578833SVenu.Iyer@Sun.COM /* 8588833SVenu.Iyer@Sun.COM * We may want to have one of these per MAC type plugin in the 8598833SVenu.Iyer@Sun.COM * future. For now supports only ethernet. 8608833SVenu.Iyer@Sun.COM */ 8618833SVenu.Iyer@Sun.COM if (media != DL_ETHER) 8628833SVenu.Iyer@Sun.COM return (0L); 8638833SVenu.Iyer@Sun.COM 8648833SVenu.Iyer@Sun.COM /* for now we support only outbound packets */ 8658833SVenu.Iyer@Sun.COM ASSERT(is_outbound); 8668833SVenu.Iyer@Sun.COM ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t))); 8678833SVenu.Iyer@Sun.COM ASSERT(MBLKL(mp) >= sizeof (struct ether_header)); 8688833SVenu.Iyer@Sun.COM 8698833SVenu.Iyer@Sun.COM /* compute L2 hash */ 8708833SVenu.Iyer@Sun.COM 8718833SVenu.Iyer@Sun.COM ehp = (struct ether_header *)mp->b_rptr; 8728833SVenu.Iyer@Sun.COM 8738833SVenu.Iyer@Sun.COM if ((policy & MAC_PKT_HASH_L2) != 0) { 8748833SVenu.Iyer@Sun.COM uchar_t *mac_src = ehp->ether_shost.ether_addr_octet; 8758833SVenu.Iyer@Sun.COM uchar_t *mac_dst = ehp->ether_dhost.ether_addr_octet; 8768833SVenu.Iyer@Sun.COM hash = PKT_HASH_MAC(mac_src) ^ PKT_HASH_MAC(mac_dst); 8778833SVenu.Iyer@Sun.COM policy &= ~MAC_PKT_HASH_L2; 8788833SVenu.Iyer@Sun.COM } 8798833SVenu.Iyer@Sun.COM 8808833SVenu.Iyer@Sun.COM if (policy == 0) 8818833SVenu.Iyer@Sun.COM goto done; 8828833SVenu.Iyer@Sun.COM 8838833SVenu.Iyer@Sun.COM /* skip ethernet header */ 8848833SVenu.Iyer@Sun.COM 8858833SVenu.Iyer@Sun.COM sap = ntohs(ehp->ether_type); 8868833SVenu.Iyer@Sun.COM if (sap == ETHERTYPE_VLAN) { 8878833SVenu.Iyer@Sun.COM struct ether_vlan_header *evhp; 8888833SVenu.Iyer@Sun.COM mblk_t *newmp = NULL; 8898833SVenu.Iyer@Sun.COM 8908833SVenu.Iyer@Sun.COM skip_len = sizeof (struct ether_vlan_header); 8918833SVenu.Iyer@Sun.COM if (MBLKL(mp) < skip_len) { 8928833SVenu.Iyer@Sun.COM /* the vlan tag is the payload, pull up first */ 8938833SVenu.Iyer@Sun.COM newmp = msgpullup(mp, -1); 8948833SVenu.Iyer@Sun.COM if ((newmp == NULL) || (MBLKL(newmp) < skip_len)) { 8958833SVenu.Iyer@Sun.COM goto done; 8968833SVenu.Iyer@Sun.COM } 8978833SVenu.Iyer@Sun.COM evhp = (struct ether_vlan_header *)newmp->b_rptr; 8988833SVenu.Iyer@Sun.COM } else { 8998833SVenu.Iyer@Sun.COM evhp = (struct ether_vlan_header *)mp->b_rptr; 9008833SVenu.Iyer@Sun.COM } 9018833SVenu.Iyer@Sun.COM 9028833SVenu.Iyer@Sun.COM sap = ntohs(evhp->ether_type); 9038833SVenu.Iyer@Sun.COM freemsg(newmp); 9048833SVenu.Iyer@Sun.COM } else { 9058833SVenu.Iyer@Sun.COM skip_len = sizeof (struct ether_header); 9068833SVenu.Iyer@Sun.COM } 9078833SVenu.Iyer@Sun.COM 9088833SVenu.Iyer@Sun.COM /* if ethernet header is in its own mblk, skip it */ 9098833SVenu.Iyer@Sun.COM if (MBLKL(mp) <= skip_len) { 9108833SVenu.Iyer@Sun.COM skip_len -= MBLKL(mp); 9118833SVenu.Iyer@Sun.COM mp = mp->b_cont; 9128833SVenu.Iyer@Sun.COM if (mp == NULL) 9138833SVenu.Iyer@Sun.COM goto done; 9148833SVenu.Iyer@Sun.COM } 9158833SVenu.Iyer@Sun.COM 9168833SVenu.Iyer@Sun.COM sap = (sap < ETHERTYPE_802_MIN) ? 0 : sap; 9178833SVenu.Iyer@Sun.COM 9188833SVenu.Iyer@Sun.COM /* compute IP src/dst addresses hash and skip IPv{4,6} header */ 9198833SVenu.Iyer@Sun.COM 9208833SVenu.Iyer@Sun.COM switch (sap) { 9218833SVenu.Iyer@Sun.COM case ETHERTYPE_IP: { 9228833SVenu.Iyer@Sun.COM ipha_t *iphp; 9238833SVenu.Iyer@Sun.COM 9248833SVenu.Iyer@Sun.COM /* 9258833SVenu.Iyer@Sun.COM * If the header is not aligned or the header doesn't fit 9268833SVenu.Iyer@Sun.COM * in the mblk, bail now. Note that this may cause packets 9278833SVenu.Iyer@Sun.COM * reordering. 9288833SVenu.Iyer@Sun.COM */ 9298833SVenu.Iyer@Sun.COM iphp = (ipha_t *)(mp->b_rptr + skip_len); 9308833SVenu.Iyer@Sun.COM if (((unsigned char *)iphp + sizeof (ipha_t) > mp->b_wptr) || 9318833SVenu.Iyer@Sun.COM !OK_32PTR((char *)iphp)) 9328833SVenu.Iyer@Sun.COM goto done; 9338833SVenu.Iyer@Sun.COM 9348833SVenu.Iyer@Sun.COM proto = iphp->ipha_protocol; 9358833SVenu.Iyer@Sun.COM skip_len += IPH_HDR_LENGTH(iphp); 9368833SVenu.Iyer@Sun.COM 937*11528SBaban.Kenkre@Sun.COM /* Check if the packet is fragmented. */ 938*11528SBaban.Kenkre@Sun.COM ip_fragmented = ntohs(iphp->ipha_fragment_offset_and_flags) & 939*11528SBaban.Kenkre@Sun.COM IPH_OFFSET; 940*11528SBaban.Kenkre@Sun.COM 941*11528SBaban.Kenkre@Sun.COM /* 942*11528SBaban.Kenkre@Sun.COM * For fragmented packets, use addresses in addition to 943*11528SBaban.Kenkre@Sun.COM * the frag_id to generate the hash inorder to get 944*11528SBaban.Kenkre@Sun.COM * better distribution. 945*11528SBaban.Kenkre@Sun.COM */ 946*11528SBaban.Kenkre@Sun.COM if (ip_fragmented || (policy & MAC_PKT_HASH_L3) != 0) { 9478833SVenu.Iyer@Sun.COM uint8_t *ip_src = (uint8_t *)&(iphp->ipha_src); 9488833SVenu.Iyer@Sun.COM uint8_t *ip_dst = (uint8_t *)&(iphp->ipha_dst); 9498833SVenu.Iyer@Sun.COM 9508833SVenu.Iyer@Sun.COM hash ^= (PKT_HASH_4BYTES(ip_src) ^ 9518833SVenu.Iyer@Sun.COM PKT_HASH_4BYTES(ip_dst)); 9528833SVenu.Iyer@Sun.COM policy &= ~MAC_PKT_HASH_L3; 9538833SVenu.Iyer@Sun.COM } 954*11528SBaban.Kenkre@Sun.COM 955*11528SBaban.Kenkre@Sun.COM if (ip_fragmented) { 956*11528SBaban.Kenkre@Sun.COM uint8_t *identp = (uint8_t *)&iphp->ipha_ident; 957*11528SBaban.Kenkre@Sun.COM hash ^= PKT_HASH_2BYTES(identp); 958*11528SBaban.Kenkre@Sun.COM goto done; 959*11528SBaban.Kenkre@Sun.COM } 9608833SVenu.Iyer@Sun.COM break; 9618833SVenu.Iyer@Sun.COM } 9628833SVenu.Iyer@Sun.COM case ETHERTYPE_IPV6: { 9638833SVenu.Iyer@Sun.COM ip6_t *ip6hp; 9648833SVenu.Iyer@Sun.COM uint16_t hdr_length; 965*11528SBaban.Kenkre@Sun.COM uint32_t ip_frag_ident; 9668833SVenu.Iyer@Sun.COM 9678833SVenu.Iyer@Sun.COM /* 9688833SVenu.Iyer@Sun.COM * If the header is not aligned or the header doesn't fit 9698833SVenu.Iyer@Sun.COM * in the mblk, bail now. Note that this may cause packets 9708833SVenu.Iyer@Sun.COM * reordering. 9718833SVenu.Iyer@Sun.COM */ 9728833SVenu.Iyer@Sun.COM 9738833SVenu.Iyer@Sun.COM ip6hp = (ip6_t *)(mp->b_rptr + skip_len); 9748833SVenu.Iyer@Sun.COM if (((unsigned char *)ip6hp + IPV6_HDR_LEN > mp->b_wptr) || 9758833SVenu.Iyer@Sun.COM !OK_32PTR((char *)ip6hp)) 9768833SVenu.Iyer@Sun.COM goto done; 9778833SVenu.Iyer@Sun.COM 978*11528SBaban.Kenkre@Sun.COM if (!mac_ip_hdr_length_v6(mp, ip6hp, &hdr_length, &proto, 979*11528SBaban.Kenkre@Sun.COM &ip_fragmented, &ip_frag_ident)) 9808833SVenu.Iyer@Sun.COM goto done; 9818833SVenu.Iyer@Sun.COM skip_len += hdr_length; 9828833SVenu.Iyer@Sun.COM 983*11528SBaban.Kenkre@Sun.COM /* 984*11528SBaban.Kenkre@Sun.COM * For fragmented packets, use addresses in addition to 985*11528SBaban.Kenkre@Sun.COM * the frag_id to generate the hash inorder to get 986*11528SBaban.Kenkre@Sun.COM * better distribution. 987*11528SBaban.Kenkre@Sun.COM */ 988*11528SBaban.Kenkre@Sun.COM if (ip_fragmented || (policy & MAC_PKT_HASH_L3) != 0) { 9898833SVenu.Iyer@Sun.COM uint8_t *ip_src = &(ip6hp->ip6_src.s6_addr8[12]); 9908833SVenu.Iyer@Sun.COM uint8_t *ip_dst = &(ip6hp->ip6_dst.s6_addr8[12]); 9918833SVenu.Iyer@Sun.COM 9928833SVenu.Iyer@Sun.COM hash ^= (PKT_HASH_4BYTES(ip_src) ^ 9938833SVenu.Iyer@Sun.COM PKT_HASH_4BYTES(ip_dst)); 9948833SVenu.Iyer@Sun.COM policy &= ~MAC_PKT_HASH_L3; 9958833SVenu.Iyer@Sun.COM } 996*11528SBaban.Kenkre@Sun.COM 997*11528SBaban.Kenkre@Sun.COM if (ip_fragmented) { 998*11528SBaban.Kenkre@Sun.COM uint8_t *identp = (uint8_t *)&ip_frag_ident; 999*11528SBaban.Kenkre@Sun.COM hash ^= PKT_HASH_4BYTES(identp); 1000*11528SBaban.Kenkre@Sun.COM goto done; 1001*11528SBaban.Kenkre@Sun.COM } 10028833SVenu.Iyer@Sun.COM break; 10038833SVenu.Iyer@Sun.COM } 10048833SVenu.Iyer@Sun.COM default: 10058833SVenu.Iyer@Sun.COM goto done; 10068833SVenu.Iyer@Sun.COM } 10078833SVenu.Iyer@Sun.COM 10088833SVenu.Iyer@Sun.COM if (policy == 0) 10098833SVenu.Iyer@Sun.COM goto done; 10108833SVenu.Iyer@Sun.COM 10118833SVenu.Iyer@Sun.COM /* if ip header is in its own mblk, skip it */ 10128833SVenu.Iyer@Sun.COM if (MBLKL(mp) <= skip_len) { 10138833SVenu.Iyer@Sun.COM skip_len -= MBLKL(mp); 10148833SVenu.Iyer@Sun.COM mp = mp->b_cont; 10158833SVenu.Iyer@Sun.COM if (mp == NULL) 10168833SVenu.Iyer@Sun.COM goto done; 10178833SVenu.Iyer@Sun.COM } 10188833SVenu.Iyer@Sun.COM 10198833SVenu.Iyer@Sun.COM /* parse ULP header */ 10208833SVenu.Iyer@Sun.COM again: 10218833SVenu.Iyer@Sun.COM switch (proto) { 10228833SVenu.Iyer@Sun.COM case IPPROTO_TCP: 10238833SVenu.Iyer@Sun.COM case IPPROTO_UDP: 10248833SVenu.Iyer@Sun.COM case IPPROTO_ESP: 10258833SVenu.Iyer@Sun.COM case IPPROTO_SCTP: 10268833SVenu.Iyer@Sun.COM /* 10278833SVenu.Iyer@Sun.COM * These Internet Protocols are intentionally designed 10288833SVenu.Iyer@Sun.COM * for hashing from the git-go. Port numbers are in the first 10298833SVenu.Iyer@Sun.COM * word for transports, SPI is first for ESP. 10308833SVenu.Iyer@Sun.COM */ 10318833SVenu.Iyer@Sun.COM if (mp->b_rptr + skip_len + 4 > mp->b_wptr) 10328833SVenu.Iyer@Sun.COM goto done; 10338833SVenu.Iyer@Sun.COM hash ^= PKT_HASH_4BYTES((mp->b_rptr + skip_len)); 10348833SVenu.Iyer@Sun.COM break; 10358833SVenu.Iyer@Sun.COM 10368833SVenu.Iyer@Sun.COM case IPPROTO_AH: { 10378833SVenu.Iyer@Sun.COM ah_t *ah = (ah_t *)(mp->b_rptr + skip_len); 10388833SVenu.Iyer@Sun.COM uint_t ah_length = AH_TOTAL_LEN(ah); 10398833SVenu.Iyer@Sun.COM 10408833SVenu.Iyer@Sun.COM if ((unsigned char *)ah + sizeof (ah_t) > mp->b_wptr) 10418833SVenu.Iyer@Sun.COM goto done; 10428833SVenu.Iyer@Sun.COM 10438833SVenu.Iyer@Sun.COM proto = ah->ah_nexthdr; 10448833SVenu.Iyer@Sun.COM skip_len += ah_length; 10458833SVenu.Iyer@Sun.COM 10468833SVenu.Iyer@Sun.COM /* if AH header is in its own mblk, skip it */ 10478833SVenu.Iyer@Sun.COM if (MBLKL(mp) <= skip_len) { 10488833SVenu.Iyer@Sun.COM skip_len -= MBLKL(mp); 10498833SVenu.Iyer@Sun.COM mp = mp->b_cont; 10508833SVenu.Iyer@Sun.COM if (mp == NULL) 10518833SVenu.Iyer@Sun.COM goto done; 10528833SVenu.Iyer@Sun.COM } 10538833SVenu.Iyer@Sun.COM 10548833SVenu.Iyer@Sun.COM goto again; 10558833SVenu.Iyer@Sun.COM } 10568833SVenu.Iyer@Sun.COM } 10578833SVenu.Iyer@Sun.COM 10588833SVenu.Iyer@Sun.COM done: 10598833SVenu.Iyer@Sun.COM return (hash); 10608833SVenu.Iyer@Sun.COM } 1061