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