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