1 /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */ 2 /* $FreeBSD$ */ 3 4 /*- 5 * Copyright (c) 1997 6 * Jonathan Stone and Jason R. Thorpe. All rights reserved. 7 * 8 * This software is derived from information provided by Matt Thomas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by Jonathan Stone 21 * and Jason R. Thorpe for the NetBSD Project. 22 * 4. The names of the authors may not be used to endorse or promote products 23 * derived from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 /* 39 * BSD/OS-compatible network interface media selection. 40 * 41 * Where it is safe to do so, this code strays slightly from the BSD/OS 42 * design. Software which uses the API (device drivers, basically) 43 * shouldn't notice any difference. 44 * 45 * Many thanks to Matt Thomas for providing the information necessary 46 * to implement this interface. 47 */ 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/socket.h> 52 #include <sys/sockio.h> 53 #include <sys/malloc.h> 54 #include <sys/module.h> 55 #include <sys/sysctl.h> 56 57 #include <net/if.h> 58 #include <net/if_media.h> 59 60 /* 61 * Compile-time options: 62 * IFMEDIA_DEBUG: 63 * turn on implementation-level debug printfs. 64 * Useful for debugging newly-ported drivers. 65 */ 66 67 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm, 68 int flags, int mask); 69 70 #ifdef IFMEDIA_DEBUG 71 int ifmedia_debug = 0; 72 SYSCTL_INT(_debug, OID_AUTO, ifmedia, CTLFLAG_RW, &ifmedia_debug, 73 0, "if_media debugging msgs"); 74 static void ifmedia_printword(int); 75 #endif 76 77 /* 78 * Initialize if_media struct for a specific interface instance. 79 */ 80 void 81 ifmedia_init(ifm, dontcare_mask, change_callback, status_callback) 82 struct ifmedia *ifm; 83 int dontcare_mask; 84 ifm_change_cb_t change_callback; 85 ifm_stat_cb_t status_callback; 86 { 87 88 LIST_INIT(&ifm->ifm_list); 89 ifm->ifm_cur = NULL; 90 ifm->ifm_media = 0; 91 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */ 92 ifm->ifm_change = change_callback; 93 ifm->ifm_status = status_callback; 94 } 95 96 void 97 ifmedia_removeall(ifm) 98 struct ifmedia *ifm; 99 { 100 struct ifmedia_entry *entry; 101 102 for (entry = LIST_FIRST(&ifm->ifm_list); entry; 103 entry = LIST_FIRST(&ifm->ifm_list)) { 104 LIST_REMOVE(entry, ifm_list); 105 free(entry, M_IFADDR); 106 } 107 } 108 109 /* 110 * Add a media configuration to the list of supported media 111 * for a specific interface instance. 112 */ 113 void 114 ifmedia_add(ifm, mword, data, aux) 115 struct ifmedia *ifm; 116 int mword; 117 int data; 118 void *aux; 119 { 120 register struct ifmedia_entry *entry; 121 122 #ifdef IFMEDIA_DEBUG 123 if (ifmedia_debug) { 124 if (ifm == NULL) { 125 printf("ifmedia_add: null ifm\n"); 126 return; 127 } 128 printf("Adding entry for "); 129 ifmedia_printword(mword); 130 } 131 #endif 132 133 entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT); 134 if (entry == NULL) 135 panic("ifmedia_add: can't malloc entry"); 136 137 entry->ifm_media = mword; 138 entry->ifm_data = data; 139 entry->ifm_aux = aux; 140 141 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list); 142 } 143 144 /* 145 * Add an array of media configurations to the list of 146 * supported media for a specific interface instance. 147 */ 148 void 149 ifmedia_list_add(ifm, lp, count) 150 struct ifmedia *ifm; 151 struct ifmedia_entry *lp; 152 int count; 153 { 154 int i; 155 156 for (i = 0; i < count; i++) 157 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data, 158 lp[i].ifm_aux); 159 } 160 161 /* 162 * Set the default active media. 163 * 164 * Called by device-specific code which is assumed to have already 165 * selected the default media in hardware. We do _not_ call the 166 * media-change callback. 167 */ 168 void 169 ifmedia_set(ifm, target) 170 struct ifmedia *ifm; 171 int target; 172 173 { 174 struct ifmedia_entry *match; 175 176 match = ifmedia_match(ifm, target, ifm->ifm_mask); 177 178 if (match == NULL) { 179 printf("ifmedia_set: no match for 0x%x/0x%x\n", 180 target, ~ifm->ifm_mask); 181 panic("ifmedia_set"); 182 } 183 ifm->ifm_cur = match; 184 185 #ifdef IFMEDIA_DEBUG 186 if (ifmedia_debug) { 187 printf("ifmedia_set: target "); 188 ifmedia_printword(target); 189 printf("ifmedia_set: setting to "); 190 ifmedia_printword(ifm->ifm_cur->ifm_media); 191 } 192 #endif 193 } 194 195 /* 196 * Device-independent media ioctl support function. 197 */ 198 int 199 ifmedia_ioctl(ifp, ifr, ifm, cmd) 200 struct ifnet *ifp; 201 struct ifreq *ifr; 202 struct ifmedia *ifm; 203 u_long cmd; 204 { 205 struct ifmedia_entry *match; 206 struct ifmediareq *ifmr = (struct ifmediareq *) ifr; 207 int error = 0; 208 209 if (ifp == NULL || ifr == NULL || ifm == NULL) 210 return(EINVAL); 211 212 switch (cmd) { 213 214 /* 215 * Set the current media. 216 */ 217 case SIOCSIFMEDIA: 218 { 219 struct ifmedia_entry *oldentry; 220 int oldmedia; 221 int newmedia = ifr->ifr_media; 222 223 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask); 224 if (match == NULL) { 225 #ifdef IFMEDIA_DEBUG 226 if (ifmedia_debug) { 227 printf( 228 "ifmedia_ioctl: no media found for 0x%x\n", 229 newmedia); 230 } 231 #endif 232 return (ENXIO); 233 } 234 235 /* 236 * If no change, we're done. 237 * XXX Automedia may invole software intervention. 238 * Keep going in case the connected media changed. 239 * Similarly, if best match changed (kernel debugger?). 240 */ 241 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) && 242 (newmedia == ifm->ifm_media) && 243 (match == ifm->ifm_cur)) 244 return 0; 245 246 /* 247 * We found a match, now make the driver switch to it. 248 * Make sure to preserve our old media type in case the 249 * driver can't switch. 250 */ 251 #ifdef IFMEDIA_DEBUG 252 if (ifmedia_debug) { 253 printf("ifmedia_ioctl: switching %s to ", 254 ifp->if_xname); 255 ifmedia_printword(match->ifm_media); 256 } 257 #endif 258 oldentry = ifm->ifm_cur; 259 oldmedia = ifm->ifm_media; 260 ifm->ifm_cur = match; 261 ifm->ifm_media = newmedia; 262 error = (*ifm->ifm_change)(ifp); 263 if (error) { 264 ifm->ifm_cur = oldentry; 265 ifm->ifm_media = oldmedia; 266 } 267 break; 268 } 269 270 /* 271 * Get list of available media and current media on interface. 272 */ 273 case SIOCGIFMEDIA: 274 { 275 struct ifmedia_entry *ep; 276 int i; 277 278 if (ifmr->ifm_count < 0) 279 return (EINVAL); 280 281 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ? 282 ifm->ifm_cur->ifm_media : IFM_NONE; 283 ifmr->ifm_mask = ifm->ifm_mask; 284 ifmr->ifm_status = 0; 285 (*ifm->ifm_status)(ifp, ifmr); 286 287 /* 288 * If there are more interfaces on the list, count 289 * them. This allows the caller to set ifmr->ifm_count 290 * to 0 on the first call to know how much space to 291 * allocate. 292 */ 293 i = 0; 294 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list) 295 if (i++ < ifmr->ifm_count) { 296 error = copyout(&ep->ifm_media, 297 ifmr->ifm_ulist + i - 1, sizeof(int)); 298 if (error) 299 break; 300 } 301 if (error == 0 && i > ifmr->ifm_count) 302 error = ifmr->ifm_count ? E2BIG : 0; 303 ifmr->ifm_count = i; 304 break; 305 } 306 307 default: 308 return (EINVAL); 309 } 310 311 return (error); 312 } 313 314 /* 315 * Find media entry matching a given ifm word. 316 * 317 */ 318 static struct ifmedia_entry * 319 ifmedia_match(ifm, target, mask) 320 struct ifmedia *ifm; 321 int target; 322 int mask; 323 { 324 struct ifmedia_entry *match, *next; 325 326 match = NULL; 327 mask = ~mask; 328 329 LIST_FOREACH(next, &ifm->ifm_list, ifm_list) { 330 if ((next->ifm_media & mask) == (target & mask)) { 331 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC) 332 if (match) { 333 printf("ifmedia_match: multiple match for " 334 "0x%x/0x%x\n", target, mask); 335 } 336 #endif 337 match = next; 338 } 339 } 340 341 return match; 342 } 343 344 /* 345 * Compute the interface `baudrate' from the media, for the interface 346 * metrics (used by routing daemons). 347 */ 348 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] = 349 IFM_BAUDRATE_DESCRIPTIONS; 350 351 uint64_t 352 ifmedia_baudrate(int mword) 353 { 354 int i; 355 356 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) { 357 if ((mword & (IFM_NMASK|IFM_TMASK)) == 358 ifmedia_baudrate_descriptions[i].ifmb_word) 359 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate); 360 } 361 362 /* Not known. */ 363 return (0); 364 } 365 366 #ifdef IFMEDIA_DEBUG 367 struct ifmedia_description ifm_type_descriptions[] = 368 IFM_TYPE_DESCRIPTIONS; 369 370 struct ifmedia_description ifm_subtype_ethernet_descriptions[] = 371 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS; 372 373 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] = 374 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS; 375 376 struct ifmedia_description ifm_subtype_tokenring_descriptions[] = 377 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS; 378 379 struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] = 380 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS; 381 382 struct ifmedia_description ifm_subtype_fddi_descriptions[] = 383 IFM_SUBTYPE_FDDI_DESCRIPTIONS; 384 385 struct ifmedia_description ifm_subtype_fddi_option_descriptions[] = 386 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS; 387 388 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] = 389 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS; 390 391 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] = 392 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS; 393 394 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] = 395 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS; 396 397 struct ifmedia_description ifm_subtype_atm_descriptions[] = 398 IFM_SUBTYPE_ATM_DESCRIPTIONS; 399 400 struct ifmedia_description ifm_subtype_atm_option_descriptions[] = 401 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS; 402 403 struct ifmedia_description ifm_subtype_shared_descriptions[] = 404 IFM_SUBTYPE_SHARED_DESCRIPTIONS; 405 406 struct ifmedia_description ifm_shared_option_descriptions[] = 407 IFM_SHARED_OPTION_DESCRIPTIONS; 408 409 struct ifmedia_type_to_subtype { 410 struct ifmedia_description *subtypes; 411 struct ifmedia_description *options; 412 struct ifmedia_description *modes; 413 }; 414 415 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */ 416 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = { 417 { 418 &ifm_subtype_ethernet_descriptions[0], 419 &ifm_subtype_ethernet_option_descriptions[0], 420 NULL, 421 }, 422 { 423 &ifm_subtype_tokenring_descriptions[0], 424 &ifm_subtype_tokenring_option_descriptions[0], 425 NULL, 426 }, 427 { 428 &ifm_subtype_fddi_descriptions[0], 429 &ifm_subtype_fddi_option_descriptions[0], 430 NULL, 431 }, 432 { 433 &ifm_subtype_ieee80211_descriptions[0], 434 &ifm_subtype_ieee80211_option_descriptions[0], 435 &ifm_subtype_ieee80211_mode_descriptions[0] 436 }, 437 { 438 &ifm_subtype_atm_descriptions[0], 439 &ifm_subtype_atm_option_descriptions[0], 440 NULL, 441 }, 442 }; 443 444 /* 445 * print a media word. 446 */ 447 static void 448 ifmedia_printword(ifmw) 449 int ifmw; 450 { 451 struct ifmedia_description *desc; 452 struct ifmedia_type_to_subtype *ttos; 453 int seen_option = 0; 454 455 /* Find the top-level interface type. */ 456 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 457 desc->ifmt_string != NULL; desc++, ttos++) 458 if (IFM_TYPE(ifmw) == desc->ifmt_word) 459 break; 460 if (desc->ifmt_string == NULL) { 461 printf("<unknown type>\n"); 462 return; 463 } 464 printf(desc->ifmt_string); 465 466 /* Any mode. */ 467 for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++) 468 if (IFM_MODE(ifmw) == desc->ifmt_word) { 469 if (desc->ifmt_string != NULL) 470 printf(" mode %s", desc->ifmt_string); 471 break; 472 } 473 474 /* 475 * Check for the shared subtype descriptions first, then the 476 * type-specific ones. 477 */ 478 for (desc = ifm_subtype_shared_descriptions; 479 desc->ifmt_string != NULL; desc++) 480 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) 481 goto got_subtype; 482 483 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++) 484 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) 485 break; 486 if (desc->ifmt_string == NULL) { 487 printf(" <unknown subtype>\n"); 488 return; 489 } 490 491 got_subtype: 492 printf(" %s", desc->ifmt_string); 493 494 /* 495 * Look for shared options. 496 */ 497 for (desc = ifm_shared_option_descriptions; 498 desc->ifmt_string != NULL; desc++) { 499 if (ifmw & desc->ifmt_word) { 500 if (seen_option == 0) 501 printf(" <"); 502 printf("%s%s", seen_option++ ? "," : "", 503 desc->ifmt_string); 504 } 505 } 506 507 /* 508 * Look for subtype-specific options. 509 */ 510 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) { 511 if (ifmw & desc->ifmt_word) { 512 if (seen_option == 0) 513 printf(" <"); 514 printf("%s%s", seen_option++ ? "," : "", 515 desc->ifmt_string); 516 } 517 } 518 printf("%s\n", seen_option ? ">" : ""); 519 } 520 #endif /* IFMEDIA_DEBUG */ 521