1 /* $NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $ */ 2 /* $FreeBSD$ */ 3 4 /*- 5 * SPDX-License-Identifier: BSD-4-Clause 6 * 7 * Copyright (c) 1997 Jason R. Thorpe. 8 * All rights reserved. 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 for the NetBSD Project 21 * by Jason R. Thorpe. 22 * 4. The name of the author 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 AUTHOR ``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 * Copyright (c) 1983, 1993 40 * The Regents of the University of California. All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 */ 66 /* 67 * based on sbin/ifconfig/ifmedia.c r221954 68 */ 69 70 #include <sys/param.h> 71 #include <sys/ioctl.h> 72 #include <sys/socket.h> 73 #include <sys/sysctl.h> 74 #include <sys/time.h> 75 76 #include <net/if.h> 77 #include <net/if_dl.h> 78 #include <net/if_types.h> 79 #include <net/if_media.h> 80 #include <net/route.h> 81 82 #include <ctype.h> 83 #include <err.h> 84 #include <errno.h> 85 #include <fcntl.h> 86 #include <stdio.h> 87 #include <stdlib.h> 88 #include <string.h> 89 #include <unistd.h> 90 91 void domediaopt(const char *, int, int); 92 int get_media_subtype(int, const char *); 93 int get_media_mode(int, const char *); 94 int get_media_options(int, const char *); 95 int lookup_media_word(struct ifmedia_description *, const char *); 96 void print_media_word(int, int); 97 void print_media_word_ifconfig(int); 98 99 #if 0 100 static struct ifmedia_description *get_toptype_desc(int); 101 static struct ifmedia_type_to_subtype *get_toptype_ttos(int); 102 static struct ifmedia_description *get_subtype_desc(int, 103 struct ifmedia_type_to_subtype *ttos); 104 105 #define IFM_OPMODE(x) \ 106 ((x) & (IFM_IEEE80211_ADHOC | IFM_IEEE80211_HOSTAP | \ 107 IFM_IEEE80211_IBSS | IFM_IEEE80211_WDS | IFM_IEEE80211_MONITOR | \ 108 IFM_IEEE80211_MBSS)) 109 #define IFM_IEEE80211_STA 0 110 111 static void 112 media_status(int s) 113 { 114 struct ifmediareq ifmr; 115 int *media_list, i; 116 117 (void) memset(&ifmr, 0, sizeof(ifmr)); 118 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); 119 120 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { 121 /* 122 * Interface doesn't support SIOC{G,S}IFMEDIA. 123 */ 124 return; 125 } 126 127 if (ifmr.ifm_count == 0) { 128 warnx("%s: no media types?", name); 129 return; 130 } 131 132 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int)); 133 if (media_list == NULL) 134 err(1, "malloc"); 135 ifmr.ifm_ulist = media_list; 136 137 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) 138 err(1, "SIOCGIFMEDIA"); 139 140 printf("\tmedia: "); 141 print_media_word(ifmr.ifm_current, 1); 142 if (ifmr.ifm_active != ifmr.ifm_current) { 143 putchar(' '); 144 putchar('('); 145 print_media_word(ifmr.ifm_active, 0); 146 putchar(')'); 147 } 148 149 putchar('\n'); 150 151 if (ifmr.ifm_status & IFM_AVALID) { 152 printf("\tstatus: "); 153 switch (IFM_TYPE(ifmr.ifm_active)) { 154 case IFM_ETHER: 155 case IFM_ATM: 156 if (ifmr.ifm_status & IFM_ACTIVE) 157 printf("active"); 158 else 159 printf("no carrier"); 160 break; 161 162 case IFM_IEEE80211: 163 if (ifmr.ifm_status & IFM_ACTIVE) { 164 /* NB: only sta mode associates */ 165 if (IFM_OPMODE(ifmr.ifm_active) == IFM_IEEE80211_STA) 166 printf("associated"); 167 else 168 printf("running"); 169 } else 170 printf("no carrier"); 171 break; 172 } 173 putchar('\n'); 174 } 175 176 if (ifmr.ifm_count > 0 && supmedia) { 177 printf("\tsupported media:\n"); 178 for (i = 0; i < ifmr.ifm_count; i++) { 179 printf("\t\t"); 180 print_media_word_ifconfig(media_list[i]); 181 putchar('\n'); 182 } 183 } 184 185 free(media_list); 186 } 187 188 struct ifmediareq * 189 ifmedia_getstate(int s) 190 { 191 static struct ifmediareq *ifmr = NULL; 192 int *mwords; 193 194 if (ifmr == NULL) { 195 ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq)); 196 if (ifmr == NULL) 197 err(1, "malloc"); 198 199 (void) memset(ifmr, 0, sizeof(struct ifmediareq)); 200 (void) strncpy(ifmr->ifm_name, name, 201 sizeof(ifmr->ifm_name)); 202 203 ifmr->ifm_count = 0; 204 ifmr->ifm_ulist = NULL; 205 206 /* 207 * We must go through the motions of reading all 208 * supported media because we need to know both 209 * the current media type and the top-level type. 210 */ 211 212 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) { 213 err(1, "SIOCGIFMEDIA"); 214 } 215 216 if (ifmr->ifm_count == 0) 217 errx(1, "%s: no media types?", name); 218 219 mwords = (int *)malloc(ifmr->ifm_count * sizeof(int)); 220 if (mwords == NULL) 221 err(1, "malloc"); 222 223 ifmr->ifm_ulist = mwords; 224 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) 225 err(1, "SIOCGIFMEDIA"); 226 } 227 228 return ifmr; 229 } 230 231 static void 232 setifmediacallback(int s, void *arg) 233 { 234 struct ifmediareq *ifmr = (struct ifmediareq *)arg; 235 static int did_it = 0; 236 237 if (!did_it) { 238 ifr.ifr_media = ifmr->ifm_current; 239 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0) 240 err(1, "SIOCSIFMEDIA (media)"); 241 free(ifmr->ifm_ulist); 242 free(ifmr); 243 did_it = 1; 244 } 245 } 246 247 static void 248 setmedia(const char *val, int d, int s, const struct afswtch *afp) 249 { 250 struct ifmediareq *ifmr; 251 int subtype; 252 253 ifmr = ifmedia_getstate(s); 254 255 /* 256 * We are primarily concerned with the top-level type. 257 * However, "current" may be only IFM_NONE, so we just look 258 * for the top-level type in the first "supported type" 259 * entry. 260 * 261 * (I'm assuming that all supported media types for a given 262 * interface will be the same top-level type..) 263 */ 264 subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val); 265 266 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 267 ifr.ifr_media = (ifmr->ifm_current & IFM_IMASK) | 268 IFM_TYPE(ifmr->ifm_ulist[0]) | subtype; 269 270 ifmr->ifm_current = ifr.ifr_media; 271 callback_register(setifmediacallback, (void *)ifmr); 272 } 273 274 static void 275 setmediaopt(const char *val, int d, int s, const struct afswtch *afp) 276 { 277 278 domediaopt(val, 0, s); 279 } 280 281 static void 282 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp) 283 { 284 285 domediaopt(val, 1, s); 286 } 287 288 static void 289 domediaopt(const char *val, int clear, int s) 290 { 291 struct ifmediareq *ifmr; 292 int options; 293 294 ifmr = ifmedia_getstate(s); 295 296 options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val); 297 298 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 299 ifr.ifr_media = ifmr->ifm_current; 300 if (clear) 301 ifr.ifr_media &= ~options; 302 else { 303 if (options & IFM_HDX) { 304 ifr.ifr_media &= ~IFM_FDX; 305 options &= ~IFM_HDX; 306 } 307 ifr.ifr_media |= options; 308 } 309 ifmr->ifm_current = ifr.ifr_media; 310 callback_register(setifmediacallback, (void *)ifmr); 311 } 312 313 static void 314 setmediainst(const char *val, int d, int s, const struct afswtch *afp) 315 { 316 struct ifmediareq *ifmr; 317 int inst; 318 319 ifmr = ifmedia_getstate(s); 320 321 inst = atoi(val); 322 if (inst < 0 || inst > (int)IFM_INST_MAX) 323 errx(1, "invalid media instance: %s", val); 324 325 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 326 ifr.ifr_media = (ifmr->ifm_current & ~IFM_IMASK) | inst << IFM_ISHIFT; 327 328 ifmr->ifm_current = ifr.ifr_media; 329 callback_register(setifmediacallback, (void *)ifmr); 330 } 331 332 static void 333 setmediamode(const char *val, int d, int s, const struct afswtch *afp) 334 { 335 struct ifmediareq *ifmr; 336 int mode; 337 338 ifmr = ifmedia_getstate(s); 339 340 mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val); 341 342 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 343 ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode; 344 345 ifmr->ifm_current = ifr.ifr_media; 346 callback_register(setifmediacallback, (void *)ifmr); 347 } 348 #endif 349 350 /********************************************************************** 351 * A good chunk of this is duplicated from sys/net/ifmedia.c 352 **********************************************************************/ 353 354 static struct ifmedia_description ifm_type_descriptions[] = 355 IFM_TYPE_DESCRIPTIONS; 356 357 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] = 358 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS; 359 360 static struct ifmedia_description ifm_subtype_ethernet_aliases[] = 361 IFM_SUBTYPE_ETHERNET_ALIASES; 362 363 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] = 364 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS; 365 366 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] = 367 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS; 368 369 static struct ifmedia_description ifm_subtype_tokenring_aliases[] = 370 IFM_SUBTYPE_TOKENRING_ALIASES; 371 372 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] = 373 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS; 374 375 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] = 376 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS; 377 378 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] = 379 IFM_SUBTYPE_IEEE80211_ALIASES; 380 381 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] = 382 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS; 383 384 static struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] = 385 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS; 386 387 static struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] = 388 IFM_SUBTYPE_IEEE80211_MODE_ALIASES; 389 390 static struct ifmedia_description ifm_subtype_atm_descriptions[] = 391 IFM_SUBTYPE_ATM_DESCRIPTIONS; 392 393 static struct ifmedia_description ifm_subtype_atm_aliases[] = 394 IFM_SUBTYPE_ATM_ALIASES; 395 396 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] = 397 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS; 398 399 static struct ifmedia_description ifm_subtype_shared_descriptions[] = 400 IFM_SUBTYPE_SHARED_DESCRIPTIONS; 401 402 static struct ifmedia_description ifm_subtype_shared_aliases[] = 403 IFM_SUBTYPE_SHARED_ALIASES; 404 405 static struct ifmedia_description ifm_shared_option_descriptions[] = 406 IFM_SHARED_OPTION_DESCRIPTIONS; 407 408 static struct ifmedia_description ifm_shared_option_aliases[] = 409 IFM_SHARED_OPTION_ALIASES; 410 411 struct ifmedia_type_to_subtype { 412 struct { 413 struct ifmedia_description *desc; 414 int alias; 415 } subtypes[5]; 416 struct { 417 struct ifmedia_description *desc; 418 int alias; 419 } options[4]; 420 struct { 421 struct ifmedia_description *desc; 422 int alias; 423 } modes[3]; 424 }; 425 426 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */ 427 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = { 428 { 429 { 430 { &ifm_subtype_shared_descriptions[0], 0 }, 431 { &ifm_subtype_shared_aliases[0], 1 }, 432 { &ifm_subtype_ethernet_descriptions[0], 0 }, 433 { &ifm_subtype_ethernet_aliases[0], 1 }, 434 { NULL, 0 }, 435 }, 436 { 437 { &ifm_shared_option_descriptions[0], 0 }, 438 { &ifm_shared_option_aliases[0], 1 }, 439 { &ifm_subtype_ethernet_option_descriptions[0], 0 }, 440 { NULL, 0 }, 441 }, 442 { 443 { NULL, 0 }, 444 }, 445 }, 446 { 447 { 448 { &ifm_subtype_shared_descriptions[0], 0 }, 449 { &ifm_subtype_shared_aliases[0], 1 }, 450 { &ifm_subtype_tokenring_descriptions[0], 0 }, 451 { &ifm_subtype_tokenring_aliases[0], 1 }, 452 { NULL, 0 }, 453 }, 454 { 455 { &ifm_shared_option_descriptions[0], 0 }, 456 { &ifm_shared_option_aliases[0], 1 }, 457 { &ifm_subtype_tokenring_option_descriptions[0], 0 }, 458 { NULL, 0 }, 459 }, 460 { 461 { NULL, 0 }, 462 }, 463 }, 464 { 465 { 466 { &ifm_subtype_shared_descriptions[0], 0 }, 467 { &ifm_subtype_shared_aliases[0], 1 }, 468 { &ifm_subtype_ieee80211_descriptions[0], 0 }, 469 { &ifm_subtype_ieee80211_aliases[0], 1 }, 470 { NULL, 0 }, 471 }, 472 { 473 { &ifm_shared_option_descriptions[0], 0 }, 474 { &ifm_shared_option_aliases[0], 1 }, 475 { &ifm_subtype_ieee80211_option_descriptions[0], 0 }, 476 { NULL, 0 }, 477 }, 478 { 479 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 }, 480 { &ifm_subtype_ieee80211_mode_aliases[0], 0 }, 481 { NULL, 0 }, 482 }, 483 }, 484 { 485 { 486 { &ifm_subtype_shared_descriptions[0], 0 }, 487 { &ifm_subtype_shared_aliases[0], 1 }, 488 { &ifm_subtype_atm_descriptions[0], 0 }, 489 { &ifm_subtype_atm_aliases[0], 1 }, 490 { NULL, 0 }, 491 }, 492 { 493 { &ifm_shared_option_descriptions[0], 0 }, 494 { &ifm_shared_option_aliases[0], 1 }, 495 { &ifm_subtype_atm_option_descriptions[0], 0 }, 496 { NULL, 0 }, 497 }, 498 { 499 { NULL, 0 }, 500 }, 501 }, 502 }; 503 504 int 505 get_media_subtype(int type, const char *val) 506 { 507 struct ifmedia_description *desc; 508 struct ifmedia_type_to_subtype *ttos; 509 int rval, i; 510 511 /* Find the top-level interface type. */ 512 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 513 desc->ifmt_string != NULL; desc++, ttos++) 514 if (type == desc->ifmt_word) 515 break; 516 if (desc->ifmt_string == NULL) 517 errx(1, "unknown media type 0x%x", type); 518 519 for (i = 0; ttos->subtypes[i].desc != NULL; i++) { 520 rval = lookup_media_word(ttos->subtypes[i].desc, val); 521 if (rval != -1) 522 return (rval); 523 } 524 errx(1, "unknown media subtype: %s", val); 525 /*NOTREACHED*/ 526 } 527 528 int 529 get_media_mode(int type, const char *val) 530 { 531 struct ifmedia_description *desc; 532 struct ifmedia_type_to_subtype *ttos; 533 int rval, i; 534 535 /* Find the top-level interface type. */ 536 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 537 desc->ifmt_string != NULL; desc++, ttos++) 538 if (type == desc->ifmt_word) 539 break; 540 if (desc->ifmt_string == NULL) 541 errx(1, "unknown media mode 0x%x", type); 542 543 for (i = 0; ttos->modes[i].desc != NULL; i++) { 544 rval = lookup_media_word(ttos->modes[i].desc, val); 545 if (rval != -1) 546 return (rval); 547 } 548 return -1; 549 } 550 551 int 552 get_media_options(int type, const char *val) 553 { 554 struct ifmedia_description *desc; 555 struct ifmedia_type_to_subtype *ttos; 556 char *optlist, *optptr; 557 int option = 0, i, rval = 0; 558 559 /* We muck with the string, so copy it. */ 560 optlist = strdup(val); 561 if (optlist == NULL) 562 err(1, "strdup"); 563 564 /* Find the top-level interface type. */ 565 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 566 desc->ifmt_string != NULL; desc++, ttos++) 567 if (type == desc->ifmt_word) 568 break; 569 if (desc->ifmt_string == NULL) 570 errx(1, "unknown media type 0x%x", type); 571 572 /* 573 * Look up the options in the user-provided comma-separated 574 * list. 575 */ 576 optptr = optlist; 577 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) { 578 for (i = 0; ttos->options[i].desc != NULL; i++) { 579 option = lookup_media_word(ttos->options[i].desc, optptr); 580 if (option != -1) 581 break; 582 } 583 if (option == 0) 584 errx(1, "unknown option: %s", optptr); 585 rval |= option; 586 } 587 588 free(optlist); 589 return (rval); 590 } 591 592 int 593 lookup_media_word(struct ifmedia_description *desc, const char *val) 594 { 595 596 for (; desc->ifmt_string != NULL; desc++) 597 if (strcasecmp(desc->ifmt_string, val) == 0) 598 return (desc->ifmt_word); 599 600 return (-1); 601 } 602 603 static struct ifmedia_description *get_toptype_desc(int ifmw) 604 { 605 struct ifmedia_description *desc; 606 607 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++) 608 if (IFM_TYPE(ifmw) == desc->ifmt_word) 609 break; 610 611 return desc; 612 } 613 614 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw) 615 { 616 struct ifmedia_description *desc; 617 struct ifmedia_type_to_subtype *ttos; 618 619 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 620 desc->ifmt_string != NULL; desc++, ttos++) 621 if (IFM_TYPE(ifmw) == desc->ifmt_word) 622 break; 623 624 return ttos; 625 } 626 627 static struct ifmedia_description *get_subtype_desc(int ifmw, 628 struct ifmedia_type_to_subtype *ttos) 629 { 630 int i; 631 struct ifmedia_description *desc; 632 633 for (i = 0; ttos->subtypes[i].desc != NULL; i++) { 634 if (ttos->subtypes[i].alias) 635 continue; 636 for (desc = ttos->subtypes[i].desc; 637 desc->ifmt_string != NULL; desc++) { 638 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) 639 return desc; 640 } 641 } 642 643 return NULL; 644 } 645 646 static struct ifmedia_description *get_mode_desc(int ifmw, 647 struct ifmedia_type_to_subtype *ttos) 648 { 649 int i; 650 struct ifmedia_description *desc; 651 652 for (i = 0; ttos->modes[i].desc != NULL; i++) { 653 if (ttos->modes[i].alias) 654 continue; 655 for (desc = ttos->modes[i].desc; 656 desc->ifmt_string != NULL; desc++) { 657 if (IFM_MODE(ifmw) == desc->ifmt_word) 658 return desc; 659 } 660 } 661 662 return NULL; 663 } 664 665 void 666 print_media_word(int ifmw, int print_toptype) 667 { 668 struct ifmedia_description *desc; 669 struct ifmedia_type_to_subtype *ttos; 670 int seen_option = 0, i; 671 672 /* Find the top-level interface type. */ 673 desc = get_toptype_desc(ifmw); 674 ttos = get_toptype_ttos(ifmw); 675 if (desc->ifmt_string == NULL) { 676 printf("<unknown type>"); 677 return; 678 } else if (print_toptype) { 679 printf("%s", desc->ifmt_string); 680 } 681 682 /* 683 * Don't print the top-level type; it's not like we can 684 * change it, or anything. 685 */ 686 687 /* Find subtype. */ 688 desc = get_subtype_desc(ifmw, ttos); 689 if (desc == NULL) { 690 printf("<unknown subtype>"); 691 return; 692 } 693 694 if (print_toptype) 695 putchar(' '); 696 697 printf("%s", desc->ifmt_string); 698 699 if (print_toptype) { 700 desc = get_mode_desc(ifmw, ttos); 701 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string)) 702 printf(" mode %s", desc->ifmt_string); 703 } 704 705 /* Find options. */ 706 for (i = 0; ttos->options[i].desc != NULL; i++) { 707 if (ttos->options[i].alias) 708 continue; 709 for (desc = ttos->options[i].desc; 710 desc->ifmt_string != NULL; desc++) { 711 if (ifmw & desc->ifmt_word) { 712 if (seen_option == 0) 713 printf(" <"); 714 printf("%s%s", seen_option++ ? "," : "", 715 desc->ifmt_string); 716 } 717 } 718 } 719 printf("%s", seen_option ? ">" : ""); 720 721 if (print_toptype && IFM_INST(ifmw) != 0) 722 printf(" instance %d", IFM_INST(ifmw)); 723 } 724 725 void 726 print_media_word_ifconfig(int ifmw) 727 { 728 struct ifmedia_description *desc; 729 struct ifmedia_type_to_subtype *ttos; 730 int seen_option = 0, i; 731 732 /* Find the top-level interface type. */ 733 desc = get_toptype_desc(ifmw); 734 ttos = get_toptype_ttos(ifmw); 735 if (desc->ifmt_string == NULL) { 736 printf("<unknown type>"); 737 return; 738 } 739 740 /* 741 * Don't print the top-level type; it's not like we can 742 * change it, or anything. 743 */ 744 745 /* Find subtype. */ 746 desc = get_subtype_desc(ifmw, ttos); 747 if (desc == NULL) { 748 printf("<unknown subtype>"); 749 return; 750 } 751 752 printf("media %s", desc->ifmt_string); 753 754 desc = get_mode_desc(ifmw, ttos); 755 if (desc != NULL) 756 printf(" mode %s", desc->ifmt_string); 757 758 /* Find options. */ 759 for (i = 0; ttos->options[i].desc != NULL; i++) { 760 if (ttos->options[i].alias) 761 continue; 762 for (desc = ttos->options[i].desc; 763 desc->ifmt_string != NULL; desc++) { 764 if (ifmw & desc->ifmt_word) { 765 if (seen_option == 0) 766 printf(" mediaopt "); 767 printf("%s%s", seen_option++ ? "," : "", 768 desc->ifmt_string); 769 } 770 } 771 } 772 773 if (IFM_INST(ifmw) != 0) 774 printf(" instance %d", IFM_INST(ifmw)); 775 } 776 777 /********************************************************************** 778 * ...until here. 779 **********************************************************************/ 780