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