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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * Ethernet MAC plugin for the Nemo mac module 30 */ 31 32 #include <sys/types.h> 33 #include <sys/modctl.h> 34 #include <sys/dlpi.h> 35 #include <sys/mac.h> 36 #include <sys/mac_ether.h> 37 #include <sys/dls.h> 38 #include <sys/ethernet.h> 39 #include <sys/byteorder.h> 40 #include <sys/strsun.h> 41 #include <inet/common.h> 42 43 static uint8_t ether_brdcst[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 44 45 static mac_stat_info_t ether_stats[] = { 46 /* RFC1643 stats */ 47 { ETHER_STAT_ALIGN_ERRORS, "align_errors", KSTAT_DATA_UINT32, 0 }, 48 { ETHER_STAT_FCS_ERRORS, "fcs_errors", KSTAT_DATA_UINT32, 0 }, 49 { ETHER_STAT_FIRST_COLLISIONS, "first_collisions", KSTAT_DATA_UINT32, 50 0 }, 51 { ETHER_STAT_MULTI_COLLISIONS, "multi_collisions", KSTAT_DATA_UINT32, 52 0 }, 53 { ETHER_STAT_SQE_ERRORS, "sqe_errors", KSTAT_DATA_UINT32, 0}, 54 { ETHER_STAT_DEFER_XMTS, "defer_xmts", KSTAT_DATA_UINT32, 0}, 55 { ETHER_STAT_TX_LATE_COLLISIONS, "tx_late_collisions", 56 KSTAT_DATA_UINT32, 0 }, 57 { ETHER_STAT_EX_COLLISIONS, "ex_collisions", KSTAT_DATA_UINT32, 0 }, 58 { ETHER_STAT_MACXMT_ERRORS, "macxmt_errors", KSTAT_DATA_UINT32, 0 }, 59 { ETHER_STAT_CARRIER_ERRORS, "carrier_errors", KSTAT_DATA_UINT32, 0 }, 60 { ETHER_STAT_TOOLONG_ERRORS, "toolong_errors", KSTAT_DATA_UINT32, 0 }, 61 { ETHER_STAT_MACRCV_ERRORS, "macrcv_errors", KSTAT_DATA_UINT32, 0 }, 62 { ETHER_STAT_TOOSHORT_ERRORS, "runt_errors", KSTAT_DATA_UINT32, 0 }, 63 { ETHER_STAT_JABBER_ERRORS, "jabber_errors", KSTAT_DATA_UINT32, 0 }, 64 65 /* Statistics described in the ieee802.3(5) man page */ 66 { ETHER_STAT_XCVR_ADDR, "xcvr_addr", KSTAT_DATA_UINT32, 0 }, 67 { ETHER_STAT_XCVR_ID, "xcvr_id", KSTAT_DATA_UINT32, 0 }, 68 { ETHER_STAT_XCVR_INUSE, "xcvr_inuse", KSTAT_DATA_UINT32, 0 }, 69 { ETHER_STAT_CAP_1000FDX, "cap_1000fdx", KSTAT_DATA_UINT32, 0 }, 70 { ETHER_STAT_CAP_1000HDX, "cap_1000hdx", KSTAT_DATA_UINT32, 0 }, 71 { ETHER_STAT_CAP_100T4, "cap_100T4", KSTAT_DATA_UINT32, 0 }, 72 { ETHER_STAT_CAP_100FDX, "cap_100fdx", KSTAT_DATA_UINT32, 0 }, 73 { ETHER_STAT_CAP_100HDX, "cap_100hdx", KSTAT_DATA_UINT32, 0 }, 74 { ETHER_STAT_CAP_10FDX, "cap_10fdx", KSTAT_DATA_UINT32, 0 }, 75 { ETHER_STAT_CAP_10HDX, "cap_10hdx", KSTAT_DATA_UINT32, 0 }, 76 { ETHER_STAT_CAP_ASMPAUSE, "cap_asmpause", KSTAT_DATA_UINT32, 0 }, 77 { ETHER_STAT_CAP_PAUSE, "cap_pause", KSTAT_DATA_UINT32, 0 }, 78 { ETHER_STAT_CAP_AUTONEG, "cap_autoneg", KSTAT_DATA_UINT32, 0 }, 79 { ETHER_STAT_CAP_REMFAULT, "cap_rem_fault", KSTAT_DATA_UINT32, 0 }, 80 { ETHER_STAT_ADV_CAP_1000FDX, "adv_cap_1000fdx", KSTAT_DATA_UINT32, 0 }, 81 { ETHER_STAT_ADV_CAP_1000HDX, "adv_cap_1000hdx", KSTAT_DATA_UINT32, 0 }, 82 { ETHER_STAT_ADV_CAP_100T4, "adv_cap_100T4", KSTAT_DATA_UINT32, 0 }, 83 { ETHER_STAT_ADV_CAP_100FDX, "adv_cap_100fdx", KSTAT_DATA_UINT32, 0}, 84 { ETHER_STAT_ADV_CAP_100HDX, "adv_cap_100hdx", KSTAT_DATA_UINT32, 0}, 85 { ETHER_STAT_ADV_CAP_10FDX, "adv_cap_10fdx", KSTAT_DATA_UINT32, 0 }, 86 { ETHER_STAT_ADV_CAP_10HDX, "adv_cap_10hdx", KSTAT_DATA_UINT32, 0 }, 87 { ETHER_STAT_ADV_CAP_ASMPAUSE, "adv_cap_asmpause", KSTAT_DATA_UINT32, 88 0 }, 89 { ETHER_STAT_ADV_CAP_PAUSE, "adv_cap_pause", KSTAT_DATA_UINT32, 0 }, 90 { ETHER_STAT_ADV_CAP_AUTONEG, "adv_cap_autoneg", KSTAT_DATA_UINT32, 0 }, 91 { ETHER_STAT_ADV_REMFAULT, "adv_rem_fault", KSTAT_DATA_UINT32, 0 }, 92 { ETHER_STAT_LP_CAP_1000FDX, "lp_cap_1000fdx", KSTAT_DATA_UINT32, 0 }, 93 { ETHER_STAT_LP_CAP_1000HDX, "lp_cap_1000hdx", KSTAT_DATA_UINT32, 0 }, 94 { ETHER_STAT_LP_CAP_100T4, "lp_cap_100T4", KSTAT_DATA_UINT32, 0 }, 95 { ETHER_STAT_LP_CAP_100FDX, "lp_cap_100fdx", KSTAT_DATA_UINT32, 0 }, 96 { ETHER_STAT_LP_CAP_100HDX, "lp_cap_100hdx", KSTAT_DATA_UINT32, 0 }, 97 { ETHER_STAT_LP_CAP_10FDX, "lp_cap_10fdx", KSTAT_DATA_UINT32, 0 }, 98 { ETHER_STAT_LP_CAP_10HDX, "lp_cap_10hdx", KSTAT_DATA_UINT32, 0 }, 99 { ETHER_STAT_LP_CAP_ASMPAUSE, "lp_cap_asmpause", KSTAT_DATA_UINT32, 0 }, 100 { ETHER_STAT_LP_CAP_PAUSE, "lp_cap_pause", KSTAT_DATA_UINT32, 0 }, 101 { ETHER_STAT_LP_CAP_AUTONEG, "lp_cap_autoneg", KSTAT_DATA_UINT32, 0 }, 102 { ETHER_STAT_LP_REMFAULT, "lp_rem_fault", KSTAT_DATA_UINT32, 0 }, 103 { ETHER_STAT_LINK_ASMPAUSE, "link_asmpause", KSTAT_DATA_UINT32, 0 }, 104 { ETHER_STAT_LINK_PAUSE, "link_pause", KSTAT_DATA_UINT32, 0 }, 105 { ETHER_STAT_LINK_AUTONEG, "link_autoneg", KSTAT_DATA_UINT32, 0 }, 106 { ETHER_STAT_LINK_DUPLEX, "link_duplex", KSTAT_DATA_UINT32, 0 } 107 }; 108 109 static struct modlmisc mac_ether_modlmisc = { 110 &mod_miscops, 111 "Ethernet MAC plugin %I%" 112 }; 113 114 static struct modlinkage mac_ether_modlinkage = { 115 MODREV_1, 116 &mac_ether_modlmisc, 117 NULL 118 }; 119 120 static mactype_ops_t mac_ether_type_ops; 121 122 int 123 _init(void) 124 { 125 mactype_register_t *mtrp; 126 int err; 127 128 if ((mtrp = mactype_alloc(MACTYPE_VERSION)) == NULL) 129 return (ENOTSUP); 130 mtrp->mtr_ident = MAC_PLUGIN_IDENT_ETHER; 131 mtrp->mtr_ops = &mac_ether_type_ops; 132 mtrp->mtr_mactype = DL_ETHER; 133 mtrp->mtr_nativetype = DL_ETHER; 134 mtrp->mtr_addrlen = ETHERADDRL; 135 mtrp->mtr_brdcst_addr = ether_brdcst; 136 mtrp->mtr_stats = ether_stats; 137 mtrp->mtr_statcount = A_CNT(ether_stats); 138 if ((err = mactype_register(mtrp)) == 0) { 139 if ((err = mod_install(&mac_ether_modlinkage)) != 0) 140 (void) mactype_unregister(MAC_PLUGIN_IDENT_ETHER); 141 } 142 mactype_free(mtrp); 143 return (err); 144 } 145 146 int 147 _fini(void) 148 { 149 int err; 150 151 if ((err = mactype_unregister(MAC_PLUGIN_IDENT_ETHER)) != 0) 152 return (err); 153 return (mod_remove(&mac_ether_modlinkage)); 154 } 155 156 int 157 _info(struct modinfo *modinfop) 158 { 159 return (mod_info(&mac_ether_modlinkage, modinfop)); 160 } 161 162 /* 163 * MAC Type plugin operations 164 */ 165 166 /* ARGSUSED */ 167 int 168 mac_ether_unicst_verify(const void *addr, void *mac_pdata) 169 { 170 /* If it's not a group address, then it's a valid unicast address. */ 171 return (((((uint8_t *)addr)[0] & 0x01) != 0) ? EINVAL : 0); 172 } 173 174 /* ARGSUSED */ 175 int 176 mac_ether_multicst_verify(const void *addr, void *mac_pdata) 177 { 178 /* The address must be a group address. */ 179 if ((((uint8_t *)addr)[0] & 0x01) == 0) 180 return (EINVAL); 181 /* The address must not be the media broadcast address. */ 182 if (bcmp(addr, ether_brdcst, ETHERADDRL) == 0) 183 return (EINVAL); 184 return (0); 185 } 186 187 /* 188 * Check the legality of an Ethernet SAP value. The following values are 189 * allowed, as specified by PSARC 2003/150: 190 * 191 * 0..ETHERMTU (1500) 802 semantics 192 * ETHERTYPE_802_MIN (1536)..ETHERTYPE_MAX (65535) ethertype semantics 193 * 194 * Note that SAP values less than or equal to ETHERMTU (1500) represent LLC 195 * channels. (See PSARC 2003/150). We strictly use SAP 0 to represent LLC 196 * channels. 197 */ 198 /* ARGSUSED */ 199 boolean_t 200 mac_ether_sap_verify(uint32_t sap, uint32_t *bind_sap, void *mac_pdata) 201 { 202 if (sap >= ETHERTYPE_802_MIN && sap <= ETHERTYPE_MAX) { 203 if (bind_sap != NULL) 204 *bind_sap = sap; 205 return (B_TRUE); 206 } 207 208 if (sap <= ETHERMTU) { 209 if (bind_sap != NULL) 210 *bind_sap = DLS_SAP_LLC; 211 return (B_TRUE); 212 } 213 return (B_FALSE); 214 } 215 216 /* ARGSUSED */ 217 mblk_t * 218 mac_ether_header(const void *saddr, const void *daddr, uint32_t sap, 219 void *mac_pdata, mblk_t *payload, size_t extra_len) 220 { 221 struct ether_header *ehp; 222 mblk_t *mp; 223 uint32_t bind_sap; 224 225 if (!mac_ether_sap_verify(sap, &bind_sap, NULL)) 226 return (NULL); 227 228 mp = allocb(sizeof (struct ether_header) + extra_len, BPRI_HI); 229 if (mp == NULL) 230 return (NULL); 231 232 ehp = (struct ether_header *)mp->b_rptr; 233 bcopy(daddr, &(ehp->ether_dhost), ETHERADDRL); 234 bcopy(saddr, &(ehp->ether_shost), ETHERADDRL); 235 236 /* 237 * sap <= ETHERMTU indicates that LLC is being used. If that's the 238 * case, then the ether_type needs to be set to the payload length. 239 */ 240 if ((bind_sap == DLS_SAP_LLC) && (payload != NULL)) 241 sap = msgdsize(payload); 242 ehp->ether_type = htons(sap); 243 244 mp->b_wptr += sizeof (struct ether_header); 245 return (mp); 246 } 247 248 /* ARGSUSED */ 249 int 250 mac_ether_header_info(mblk_t *mp, void *mac_pdata, mac_header_info_t *hdr_info) 251 { 252 struct ether_header *ehp; 253 uint16_t ether_type; 254 255 if (MBLKL(mp) < sizeof (struct ether_header)) 256 return (EINVAL); 257 258 ehp = (struct ether_header *)mp->b_rptr; 259 ether_type = ntohs(ehp->ether_type); 260 261 hdr_info->mhi_hdrsize = sizeof (struct ether_header); 262 hdr_info->mhi_daddr = (const uint8_t *)&(ehp->ether_dhost); 263 hdr_info->mhi_saddr = (const uint8_t *)&(ehp->ether_shost); 264 hdr_info->mhi_origsap = ether_type; 265 hdr_info->mhi_bindsap = (ether_type > ETHERMTU) ? 266 ether_type : DLS_SAP_LLC; 267 hdr_info->mhi_pktsize = (hdr_info->mhi_bindsap == DLS_SAP_LLC) ? 268 hdr_info->mhi_hdrsize + ether_type : 0; 269 270 if (mac_ether_unicst_verify(hdr_info->mhi_daddr, NULL) == 0) 271 hdr_info->mhi_dsttype = MAC_ADDRTYPE_UNICAST; 272 else if (mac_ether_multicst_verify(hdr_info->mhi_daddr, NULL) == 0) 273 hdr_info->mhi_dsttype = MAC_ADDRTYPE_MULTICAST; 274 else 275 hdr_info->mhi_dsttype = MAC_ADDRTYPE_BROADCAST; 276 277 return (0); 278 } 279 280 /*ARGSUSED3*/ 281 void 282 mac_ether_link_details(char *buf, size_t sz, mac_handle_t mh, void *mac_pdata) 283 { 284 link_duplex_t duplex; 285 uint64_t speed; 286 287 duplex = mac_stat_get(mh, ETHER_STAT_LINK_DUPLEX); 288 speed = mac_stat_get(mh, MAC_STAT_IFSPEED); 289 290 /* convert to Mbps */ 291 speed /= 1000000; 292 293 buf[0] = 0; 294 (void) snprintf(buf, sz, "%u Mbps, %s duplex", (uint32_t)speed, 295 duplex == LINK_DUPLEX_FULL ? "full" : 296 duplex == LINK_DUPLEX_HALF ? "half" : "unknown"); 297 } 298 299 static mactype_ops_t mac_ether_type_ops = { 300 MTOPS_LINK_DETAILS, 301 mac_ether_unicst_verify, 302 mac_ether_multicst_verify, 303 mac_ether_sap_verify, 304 mac_ether_header, 305 mac_ether_header_info, 306 NULL, /* pdata_verify */ 307 NULL, /* header_cook */ 308 NULL, /* header_uncook */ 309 mac_ether_link_details 310 }; 311