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 #include <net/if_var.h> 72 int ifmedia_debug = 0; 73 SYSCTL_INT(_debug, OID_AUTO, ifmedia, CTLFLAG_RW, &ifmedia_debug, 74 0, "if_media debugging msgs"); 75 static void ifmedia_printword(int); 76 #endif 77 78 /* 79 * Initialize if_media struct for a specific interface instance. 80 */ 81 void 82 ifmedia_init(ifm, dontcare_mask, change_callback, status_callback) 83 struct ifmedia *ifm; 84 int dontcare_mask; 85 ifm_change_cb_t change_callback; 86 ifm_stat_cb_t status_callback; 87 { 88 89 LIST_INIT(&ifm->ifm_list); 90 ifm->ifm_cur = NULL; 91 ifm->ifm_media = 0; 92 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */ 93 ifm->ifm_change = change_callback; 94 ifm->ifm_status = status_callback; 95 } 96 97 void 98 ifmedia_removeall(ifm) 99 struct ifmedia *ifm; 100 { 101 struct ifmedia_entry *entry; 102 103 for (entry = LIST_FIRST(&ifm->ifm_list); entry; 104 entry = LIST_FIRST(&ifm->ifm_list)) { 105 LIST_REMOVE(entry, ifm_list); 106 free(entry, M_IFADDR); 107 } 108 } 109 110 /* 111 * Add a media configuration to the list of supported media 112 * for a specific interface instance. 113 */ 114 void 115 ifmedia_add(ifm, mword, data, aux) 116 struct ifmedia *ifm; 117 int mword; 118 int data; 119 void *aux; 120 { 121 register struct ifmedia_entry *entry; 122 123 #ifdef IFMEDIA_DEBUG 124 if (ifmedia_debug) { 125 if (ifm == NULL) { 126 printf("ifmedia_add: null ifm\n"); 127 return; 128 } 129 printf("Adding entry for "); 130 ifmedia_printword(mword); 131 } 132 #endif 133 134 entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT); 135 if (entry == NULL) 136 panic("ifmedia_add: can't malloc entry"); 137 138 entry->ifm_media = mword; 139 entry->ifm_data = data; 140 entry->ifm_aux = aux; 141 142 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list); 143 } 144 145 /* 146 * Add an array of media configurations to the list of 147 * supported media for a specific interface instance. 148 */ 149 void 150 ifmedia_list_add(ifm, lp, count) 151 struct ifmedia *ifm; 152 struct ifmedia_entry *lp; 153 int count; 154 { 155 int i; 156 157 for (i = 0; i < count; i++) 158 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data, 159 lp[i].ifm_aux); 160 } 161 162 /* 163 * Set the default active media. 164 * 165 * Called by device-specific code which is assumed to have already 166 * selected the default media in hardware. We do _not_ call the 167 * media-change callback. 168 */ 169 void 170 ifmedia_set(ifm, target) 171 struct ifmedia *ifm; 172 int target; 173 174 { 175 struct ifmedia_entry *match; 176 177 match = ifmedia_match(ifm, target, ifm->ifm_mask); 178 179 if (match == NULL) { 180 printf("ifmedia_set: no match for 0x%x/0x%x\n", 181 target, ~ifm->ifm_mask); 182 panic("ifmedia_set"); 183 } 184 ifm->ifm_cur = match; 185 186 #ifdef IFMEDIA_DEBUG 187 if (ifmedia_debug) { 188 printf("ifmedia_set: target "); 189 ifmedia_printword(target); 190 printf("ifmedia_set: setting to "); 191 ifmedia_printword(ifm->ifm_cur->ifm_media); 192 } 193 #endif 194 } 195 196 /* 197 * Given a media word, return one suitable for an application 198 * using the original encoding. 199 */ 200 static int 201 compat_media(int media) 202 { 203 204 if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) { 205 media &= ~(IFM_ETH_XTYPE|IFM_TMASK); 206 media |= IFM_OTHER; 207 } 208 return (media); 209 } 210 211 /* 212 * Device-independent media ioctl support function. 213 */ 214 int 215 ifmedia_ioctl(ifp, ifr, ifm, cmd) 216 struct ifnet *ifp; 217 struct ifreq *ifr; 218 struct ifmedia *ifm; 219 u_long cmd; 220 { 221 struct ifmedia_entry *match; 222 struct ifmediareq *ifmr = (struct ifmediareq *) ifr; 223 int error = 0; 224 225 if (ifp == NULL || ifr == NULL || ifm == NULL) 226 return(EINVAL); 227 228 switch (cmd) { 229 230 /* 231 * Set the current media. 232 */ 233 case SIOCSIFMEDIA: 234 { 235 struct ifmedia_entry *oldentry; 236 int oldmedia; 237 int newmedia = ifr->ifr_media; 238 239 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask); 240 if (match == NULL) { 241 #ifdef IFMEDIA_DEBUG 242 if (ifmedia_debug) { 243 printf( 244 "ifmedia_ioctl: no media found for 0x%x\n", 245 newmedia); 246 } 247 #endif 248 return (ENXIO); 249 } 250 251 /* 252 * If no change, we're done. 253 * XXX Automedia may invole software intervention. 254 * Keep going in case the connected media changed. 255 * Similarly, if best match changed (kernel debugger?). 256 */ 257 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) && 258 (newmedia == ifm->ifm_media) && 259 (match == ifm->ifm_cur)) 260 return 0; 261 262 /* 263 * We found a match, now make the driver switch to it. 264 * Make sure to preserve our old media type in case the 265 * driver can't switch. 266 */ 267 #ifdef IFMEDIA_DEBUG 268 if (ifmedia_debug) { 269 printf("ifmedia_ioctl: switching %s to ", 270 ifp->if_xname); 271 ifmedia_printword(match->ifm_media); 272 } 273 #endif 274 oldentry = ifm->ifm_cur; 275 oldmedia = ifm->ifm_media; 276 ifm->ifm_cur = match; 277 ifm->ifm_media = newmedia; 278 error = (*ifm->ifm_change)(ifp); 279 if (error) { 280 ifm->ifm_cur = oldentry; 281 ifm->ifm_media = oldmedia; 282 } 283 break; 284 } 285 286 /* 287 * Get list of available media and current media on interface. 288 */ 289 case SIOCGIFMEDIA: 290 case SIOCGIFXMEDIA: 291 { 292 struct ifmedia_entry *ep; 293 int i; 294 295 if (ifmr->ifm_count < 0) 296 return (EINVAL); 297 298 if (cmd == SIOCGIFMEDIA) { 299 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ? 300 compat_media(ifm->ifm_cur->ifm_media) : IFM_NONE; 301 } else { 302 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ? 303 ifm->ifm_cur->ifm_media : IFM_NONE; 304 } 305 ifmr->ifm_mask = ifm->ifm_mask; 306 ifmr->ifm_status = 0; 307 (*ifm->ifm_status)(ifp, ifmr); 308 309 /* 310 * If there are more interfaces on the list, count 311 * them. This allows the caller to set ifmr->ifm_count 312 * to 0 on the first call to know how much space to 313 * allocate. 314 */ 315 i = 0; 316 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list) 317 if (i++ < ifmr->ifm_count) { 318 error = copyout(&ep->ifm_media, 319 ifmr->ifm_ulist + i - 1, sizeof(int)); 320 if (error) 321 break; 322 } 323 if (error == 0 && i > ifmr->ifm_count) 324 error = ifmr->ifm_count ? E2BIG : 0; 325 ifmr->ifm_count = i; 326 break; 327 } 328 329 default: 330 return (EINVAL); 331 } 332 333 return (error); 334 } 335 336 /* 337 * Find media entry matching a given ifm word. 338 * 339 */ 340 static struct ifmedia_entry * 341 ifmedia_match(ifm, target, mask) 342 struct ifmedia *ifm; 343 int target; 344 int mask; 345 { 346 struct ifmedia_entry *match, *next; 347 348 match = NULL; 349 mask = ~mask; 350 351 LIST_FOREACH(next, &ifm->ifm_list, ifm_list) { 352 if ((next->ifm_media & mask) == (target & mask)) { 353 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC) 354 if (match) { 355 printf("ifmedia_match: multiple match for " 356 "0x%x/0x%x\n", target, mask); 357 } 358 #endif 359 match = next; 360 } 361 } 362 363 return match; 364 } 365 366 /* 367 * Compute the interface `baudrate' from the media, for the interface 368 * metrics (used by routing daemons). 369 */ 370 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] = 371 IFM_BAUDRATE_DESCRIPTIONS; 372 373 uint64_t 374 ifmedia_baudrate(int mword) 375 { 376 int i; 377 378 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) { 379 if (IFM_TYPE_MATCH(mword, ifmedia_baudrate_descriptions[i].ifmb_word)) 380 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate); 381 } 382 383 /* Not known. */ 384 return (0); 385 } 386 387 #ifdef IFMEDIA_DEBUG 388 struct ifmedia_description ifm_type_descriptions[] = 389 IFM_TYPE_DESCRIPTIONS; 390 391 struct ifmedia_description ifm_subtype_ethernet_descriptions[] = 392 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS; 393 394 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] = 395 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS; 396 397 struct ifmedia_description ifm_subtype_tokenring_descriptions[] = 398 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS; 399 400 struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] = 401 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS; 402 403 struct ifmedia_description ifm_subtype_fddi_descriptions[] = 404 IFM_SUBTYPE_FDDI_DESCRIPTIONS; 405 406 struct ifmedia_description ifm_subtype_fddi_option_descriptions[] = 407 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS; 408 409 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] = 410 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS; 411 412 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] = 413 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS; 414 415 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] = 416 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS; 417 418 struct ifmedia_description ifm_subtype_atm_descriptions[] = 419 IFM_SUBTYPE_ATM_DESCRIPTIONS; 420 421 struct ifmedia_description ifm_subtype_atm_option_descriptions[] = 422 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS; 423 424 struct ifmedia_description ifm_subtype_shared_descriptions[] = 425 IFM_SUBTYPE_SHARED_DESCRIPTIONS; 426 427 struct ifmedia_description ifm_shared_option_descriptions[] = 428 IFM_SHARED_OPTION_DESCRIPTIONS; 429 430 struct ifmedia_type_to_subtype { 431 struct ifmedia_description *subtypes; 432 struct ifmedia_description *options; 433 struct ifmedia_description *modes; 434 }; 435 436 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */ 437 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = { 438 { 439 &ifm_subtype_ethernet_descriptions[0], 440 &ifm_subtype_ethernet_option_descriptions[0], 441 NULL, 442 }, 443 { 444 &ifm_subtype_tokenring_descriptions[0], 445 &ifm_subtype_tokenring_option_descriptions[0], 446 NULL, 447 }, 448 { 449 &ifm_subtype_fddi_descriptions[0], 450 &ifm_subtype_fddi_option_descriptions[0], 451 NULL, 452 }, 453 { 454 &ifm_subtype_ieee80211_descriptions[0], 455 &ifm_subtype_ieee80211_option_descriptions[0], 456 &ifm_subtype_ieee80211_mode_descriptions[0] 457 }, 458 { 459 &ifm_subtype_atm_descriptions[0], 460 &ifm_subtype_atm_option_descriptions[0], 461 NULL, 462 }, 463 }; 464 465 /* 466 * print a media word. 467 */ 468 static void 469 ifmedia_printword(ifmw) 470 int ifmw; 471 { 472 struct ifmedia_description *desc; 473 struct ifmedia_type_to_subtype *ttos; 474 int seen_option = 0; 475 476 /* Find the top-level interface type. */ 477 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 478 desc->ifmt_string != NULL; desc++, ttos++) 479 if (IFM_TYPE(ifmw) == desc->ifmt_word) 480 break; 481 if (desc->ifmt_string == NULL) { 482 printf("<unknown type>\n"); 483 return; 484 } 485 printf("%s", desc->ifmt_string); 486 487 /* Any mode. */ 488 for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++) 489 if (IFM_MODE(ifmw) == desc->ifmt_word) { 490 if (desc->ifmt_string != NULL) 491 printf(" mode %s", desc->ifmt_string); 492 break; 493 } 494 495 /* 496 * Check for the shared subtype descriptions first, then the 497 * type-specific ones. 498 */ 499 for (desc = ifm_subtype_shared_descriptions; 500 desc->ifmt_string != NULL; desc++) 501 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) 502 goto got_subtype; 503 504 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++) 505 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) 506 break; 507 if (desc->ifmt_string == NULL) { 508 printf(" <unknown subtype>\n"); 509 return; 510 } 511 512 got_subtype: 513 printf(" %s", desc->ifmt_string); 514 515 /* 516 * Look for shared options. 517 */ 518 for (desc = ifm_shared_option_descriptions; 519 desc->ifmt_string != NULL; desc++) { 520 if (ifmw & desc->ifmt_word) { 521 if (seen_option == 0) 522 printf(" <"); 523 printf("%s%s", seen_option++ ? "," : "", 524 desc->ifmt_string); 525 } 526 } 527 528 /* 529 * Look for subtype-specific options. 530 */ 531 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) { 532 if (ifmw & desc->ifmt_word) { 533 if (seen_option == 0) 534 printf(" <"); 535 printf("%s%s", seen_option++ ? "," : "", 536 desc->ifmt_string); 537 } 538 } 539 printf("%s\n", seen_option ? ">" : ""); 540 } 541 #endif /* IFMEDIA_DEBUG */ 542