1 /* $OpenBSD: if_media.c,v 1.38 2022/08/12 16:42:54 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 while (1) { 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 break; 380 } 381 ifmr->ifm_count = nwords; 382 break; 383 } 384 385 default: 386 return (ENOTTY); 387 } 388 389 return (error); 390 } 391 392 /* 393 * Find media entry matching a given ifm word. Return 1 if found. 394 */ 395 int 396 ifmedia_match(struct ifmedia *ifm, uint64_t target, uint64_t mask) 397 { 398 struct ifmedia_entry *match; 399 400 mtx_enter(&ifmedia_mtx); 401 match = ifmedia_get(ifm, target, mask); 402 mtx_leave(&ifmedia_mtx); 403 404 return (match != NULL); 405 } 406 407 struct ifmedia_entry * 408 ifmedia_get(struct ifmedia *ifm, uint64_t target, uint64_t mask) 409 { 410 struct ifmedia_entry *match, *next; 411 412 MUTEX_ASSERT_LOCKED(&ifmedia_mtx); 413 414 match = NULL; 415 mask = ~mask; 416 417 TAILQ_FOREACH(next, &ifm->ifm_list, ifm_list) { 418 if ((next->ifm_media & mask) == (target & mask)) { 419 if (match) { 420 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC) 421 printf("%s: multiple match for 0x%llx/0x%llx, " 422 "selected instance %lld\n", __func__, 423 target, mask, IFM_INST(match->ifm_media)); 424 #endif 425 break; 426 } 427 match = next; 428 } 429 } 430 431 return (match); 432 } 433 434 /* 435 * Delete all media for a given instance. 436 */ 437 void 438 ifmedia_delete_instance(struct ifmedia *ifm, uint64_t inst) 439 { 440 struct ifmedia_entry *ife, *nife; 441 struct ifmedia_list ifmlist; 442 443 TAILQ_INIT(&ifmlist); 444 445 mtx_enter(&ifmedia_mtx); 446 TAILQ_FOREACH_SAFE(ife, &ifm->ifm_list, ifm_list, nife) { 447 if (inst == IFM_INST_ANY || 448 inst == IFM_INST(ife->ifm_media)) { 449 TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list); 450 ifm->ifm_nwords--; 451 TAILQ_INSERT_TAIL(&ifmlist, ife, ifm_list); 452 } 453 } 454 ifm->ifm_cur = NULL; 455 mtx_leave(&ifmedia_mtx); 456 457 /* Do not hold mutex longer than necessary, call free() without. */ 458 while((ife = TAILQ_FIRST(&ifmlist)) != NULL) { 459 TAILQ_REMOVE(&ifmlist, ife, ifm_list); 460 free(ife, M_IFADDR, sizeof *ife); 461 } 462 } 463 464 /* 465 * Compute the interface `baudrate' from the media, for the interface 466 * metrics (used by routing daemons). 467 */ 468 const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] = 469 IFM_BAUDRATE_DESCRIPTIONS; 470 471 uint64_t 472 ifmedia_baudrate(uint64_t mword) 473 { 474 int i; 475 476 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) { 477 if ((mword & (IFM_NMASK|IFM_TMASK)) == 478 ifmedia_baudrate_descriptions[i].ifmb_word) 479 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate); 480 } 481 482 /* Not known. */ 483 return (0); 484 } 485 486 #ifdef IFMEDIA_DEBUG 487 488 const struct ifmedia_description ifm_type_descriptions[] = 489 IFM_TYPE_DESCRIPTIONS; 490 491 const struct ifmedia_description ifm_subtype_descriptions[] = 492 IFM_SUBTYPE_DESCRIPTIONS; 493 494 const struct ifmedia_description ifm_option_descriptions[] = 495 IFM_OPTION_DESCRIPTIONS; 496 497 /* 498 * print a media word. 499 */ 500 static void 501 ifmedia_printword(uint64_t ifmw) 502 { 503 const struct ifmedia_description *desc; 504 uint64_t seen_option = 0; 505 506 /* Print the top-level interface type. */ 507 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; 508 desc++) { 509 if (IFM_TYPE(ifmw) == desc->ifmt_word) 510 break; 511 } 512 if (desc->ifmt_string == NULL) 513 printf("<unknown type> "); 514 else 515 printf("%s ", desc->ifmt_string); 516 517 /* Print the subtype. */ 518 for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; 519 desc++) { 520 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) && 521 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw)) 522 break; 523 } 524 if (desc->ifmt_string == NULL) 525 printf("<unknown subtype>"); 526 else 527 printf("%s", desc->ifmt_string); 528 529 /* Print any options. */ 530 for (desc = ifm_option_descriptions; desc->ifmt_string != NULL; 531 desc++) { 532 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) && 533 (ifmw & desc->ifmt_word) != 0 && 534 (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) { 535 if (seen_option == 0) 536 printf(" <"); 537 printf("%s%s", seen_option ? "," : "", 538 desc->ifmt_string); 539 seen_option |= IFM_OPTIONS(desc->ifmt_word); 540 } 541 } 542 printf("%s\n", seen_option ? ">" : ""); 543 } 544 545 #endif /* IFMEDIA_DEBUG */ 546