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_FDDI: 163 case IFM_TOKEN: 164 if (ifmr.ifm_status & IFM_ACTIVE) 165 printf("inserted"); 166 else 167 printf("no ring"); 168 break; 169 170 case IFM_IEEE80211: 171 if (ifmr.ifm_status & IFM_ACTIVE) { 172 /* NB: only sta mode associates */ 173 if (IFM_OPMODE(ifmr.ifm_active) == IFM_IEEE80211_STA) 174 printf("associated"); 175 else 176 printf("running"); 177 } else 178 printf("no carrier"); 179 break; 180 } 181 putchar('\n'); 182 } 183 184 if (ifmr.ifm_count > 0 && supmedia) { 185 printf("\tsupported media:\n"); 186 for (i = 0; i < ifmr.ifm_count; i++) { 187 printf("\t\t"); 188 print_media_word_ifconfig(media_list[i]); 189 putchar('\n'); 190 } 191 } 192 193 free(media_list); 194 } 195 196 struct ifmediareq * 197 ifmedia_getstate(int s) 198 { 199 static struct ifmediareq *ifmr = NULL; 200 int *mwords; 201 202 if (ifmr == NULL) { 203 ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq)); 204 if (ifmr == NULL) 205 err(1, "malloc"); 206 207 (void) memset(ifmr, 0, sizeof(struct ifmediareq)); 208 (void) strncpy(ifmr->ifm_name, name, 209 sizeof(ifmr->ifm_name)); 210 211 ifmr->ifm_count = 0; 212 ifmr->ifm_ulist = NULL; 213 214 /* 215 * We must go through the motions of reading all 216 * supported media because we need to know both 217 * the current media type and the top-level type. 218 */ 219 220 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) { 221 err(1, "SIOCGIFMEDIA"); 222 } 223 224 if (ifmr->ifm_count == 0) 225 errx(1, "%s: no media types?", name); 226 227 mwords = (int *)malloc(ifmr->ifm_count * sizeof(int)); 228 if (mwords == NULL) 229 err(1, "malloc"); 230 231 ifmr->ifm_ulist = mwords; 232 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) 233 err(1, "SIOCGIFMEDIA"); 234 } 235 236 return ifmr; 237 } 238 239 static void 240 setifmediacallback(int s, void *arg) 241 { 242 struct ifmediareq *ifmr = (struct ifmediareq *)arg; 243 static int did_it = 0; 244 245 if (!did_it) { 246 ifr.ifr_media = ifmr->ifm_current; 247 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0) 248 err(1, "SIOCSIFMEDIA (media)"); 249 free(ifmr->ifm_ulist); 250 free(ifmr); 251 did_it = 1; 252 } 253 } 254 255 static void 256 setmedia(const char *val, int d, int s, const struct afswtch *afp) 257 { 258 struct ifmediareq *ifmr; 259 int subtype; 260 261 ifmr = ifmedia_getstate(s); 262 263 /* 264 * We are primarily concerned with the top-level type. 265 * However, "current" may be only IFM_NONE, so we just look 266 * for the top-level type in the first "supported type" 267 * entry. 268 * 269 * (I'm assuming that all supported media types for a given 270 * interface will be the same top-level type..) 271 */ 272 subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val); 273 274 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 275 ifr.ifr_media = (ifmr->ifm_current & IFM_IMASK) | 276 IFM_TYPE(ifmr->ifm_ulist[0]) | subtype; 277 278 ifmr->ifm_current = ifr.ifr_media; 279 callback_register(setifmediacallback, (void *)ifmr); 280 } 281 282 static void 283 setmediaopt(const char *val, int d, int s, const struct afswtch *afp) 284 { 285 286 domediaopt(val, 0, s); 287 } 288 289 static void 290 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp) 291 { 292 293 domediaopt(val, 1, s); 294 } 295 296 static void 297 domediaopt(const char *val, int clear, int s) 298 { 299 struct ifmediareq *ifmr; 300 int options; 301 302 ifmr = ifmedia_getstate(s); 303 304 options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val); 305 306 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 307 ifr.ifr_media = ifmr->ifm_current; 308 if (clear) 309 ifr.ifr_media &= ~options; 310 else { 311 if (options & IFM_HDX) { 312 ifr.ifr_media &= ~IFM_FDX; 313 options &= ~IFM_HDX; 314 } 315 ifr.ifr_media |= options; 316 } 317 ifmr->ifm_current = ifr.ifr_media; 318 callback_register(setifmediacallback, (void *)ifmr); 319 } 320 321 static void 322 setmediainst(const char *val, int d, int s, const struct afswtch *afp) 323 { 324 struct ifmediareq *ifmr; 325 int inst; 326 327 ifmr = ifmedia_getstate(s); 328 329 inst = atoi(val); 330 if (inst < 0 || inst > (int)IFM_INST_MAX) 331 errx(1, "invalid media instance: %s", val); 332 333 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 334 ifr.ifr_media = (ifmr->ifm_current & ~IFM_IMASK) | inst << IFM_ISHIFT; 335 336 ifmr->ifm_current = ifr.ifr_media; 337 callback_register(setifmediacallback, (void *)ifmr); 338 } 339 340 static void 341 setmediamode(const char *val, int d, int s, const struct afswtch *afp) 342 { 343 struct ifmediareq *ifmr; 344 int mode; 345 346 ifmr = ifmedia_getstate(s); 347 348 mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val); 349 350 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 351 ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode; 352 353 ifmr->ifm_current = ifr.ifr_media; 354 callback_register(setifmediacallback, (void *)ifmr); 355 } 356 #endif 357 358 /********************************************************************** 359 * A good chunk of this is duplicated from sys/net/ifmedia.c 360 **********************************************************************/ 361 362 static struct ifmedia_description ifm_type_descriptions[] = 363 IFM_TYPE_DESCRIPTIONS; 364 365 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] = 366 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS; 367 368 static struct ifmedia_description ifm_subtype_ethernet_aliases[] = 369 IFM_SUBTYPE_ETHERNET_ALIASES; 370 371 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] = 372 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS; 373 374 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] = 375 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS; 376 377 static struct ifmedia_description ifm_subtype_tokenring_aliases[] = 378 IFM_SUBTYPE_TOKENRING_ALIASES; 379 380 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] = 381 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS; 382 383 static struct ifmedia_description ifm_subtype_fddi_descriptions[] = 384 IFM_SUBTYPE_FDDI_DESCRIPTIONS; 385 386 static struct ifmedia_description ifm_subtype_fddi_aliases[] = 387 IFM_SUBTYPE_FDDI_ALIASES; 388 389 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] = 390 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS; 391 392 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] = 393 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS; 394 395 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] = 396 IFM_SUBTYPE_IEEE80211_ALIASES; 397 398 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] = 399 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS; 400 401 static struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] = 402 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS; 403 404 static struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] = 405 IFM_SUBTYPE_IEEE80211_MODE_ALIASES; 406 407 static struct ifmedia_description ifm_subtype_atm_descriptions[] = 408 IFM_SUBTYPE_ATM_DESCRIPTIONS; 409 410 static struct ifmedia_description ifm_subtype_atm_aliases[] = 411 IFM_SUBTYPE_ATM_ALIASES; 412 413 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] = 414 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS; 415 416 static struct ifmedia_description ifm_subtype_shared_descriptions[] = 417 IFM_SUBTYPE_SHARED_DESCRIPTIONS; 418 419 static struct ifmedia_description ifm_subtype_shared_aliases[] = 420 IFM_SUBTYPE_SHARED_ALIASES; 421 422 static struct ifmedia_description ifm_shared_option_descriptions[] = 423 IFM_SHARED_OPTION_DESCRIPTIONS; 424 425 static struct ifmedia_description ifm_shared_option_aliases[] = 426 IFM_SHARED_OPTION_ALIASES; 427 428 struct ifmedia_type_to_subtype { 429 struct { 430 struct ifmedia_description *desc; 431 int alias; 432 } subtypes[5]; 433 struct { 434 struct ifmedia_description *desc; 435 int alias; 436 } options[4]; 437 struct { 438 struct ifmedia_description *desc; 439 int alias; 440 } modes[3]; 441 }; 442 443 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */ 444 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = { 445 { 446 { 447 { &ifm_subtype_shared_descriptions[0], 0 }, 448 { &ifm_subtype_shared_aliases[0], 1 }, 449 { &ifm_subtype_ethernet_descriptions[0], 0 }, 450 { &ifm_subtype_ethernet_aliases[0], 1 }, 451 { NULL, 0 }, 452 }, 453 { 454 { &ifm_shared_option_descriptions[0], 0 }, 455 { &ifm_shared_option_aliases[0], 1 }, 456 { &ifm_subtype_ethernet_option_descriptions[0], 0 }, 457 { NULL, 0 }, 458 }, 459 { 460 { NULL, 0 }, 461 }, 462 }, 463 { 464 { 465 { &ifm_subtype_shared_descriptions[0], 0 }, 466 { &ifm_subtype_shared_aliases[0], 1 }, 467 { &ifm_subtype_tokenring_descriptions[0], 0 }, 468 { &ifm_subtype_tokenring_aliases[0], 1 }, 469 { NULL, 0 }, 470 }, 471 { 472 { &ifm_shared_option_descriptions[0], 0 }, 473 { &ifm_shared_option_aliases[0], 1 }, 474 { &ifm_subtype_tokenring_option_descriptions[0], 0 }, 475 { NULL, 0 }, 476 }, 477 { 478 { NULL, 0 }, 479 }, 480 }, 481 { 482 { 483 { &ifm_subtype_shared_descriptions[0], 0 }, 484 { &ifm_subtype_shared_aliases[0], 1 }, 485 { &ifm_subtype_fddi_descriptions[0], 0 }, 486 { &ifm_subtype_fddi_aliases[0], 1 }, 487 { NULL, 0 }, 488 }, 489 { 490 { &ifm_shared_option_descriptions[0], 0 }, 491 { &ifm_shared_option_aliases[0], 1 }, 492 { &ifm_subtype_fddi_option_descriptions[0], 0 }, 493 { NULL, 0 }, 494 }, 495 { 496 { NULL, 0 }, 497 }, 498 }, 499 { 500 { 501 { &ifm_subtype_shared_descriptions[0], 0 }, 502 { &ifm_subtype_shared_aliases[0], 1 }, 503 { &ifm_subtype_ieee80211_descriptions[0], 0 }, 504 { &ifm_subtype_ieee80211_aliases[0], 1 }, 505 { NULL, 0 }, 506 }, 507 { 508 { &ifm_shared_option_descriptions[0], 0 }, 509 { &ifm_shared_option_aliases[0], 1 }, 510 { &ifm_subtype_ieee80211_option_descriptions[0], 0 }, 511 { NULL, 0 }, 512 }, 513 { 514 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 }, 515 { &ifm_subtype_ieee80211_mode_aliases[0], 0 }, 516 { NULL, 0 }, 517 }, 518 }, 519 { 520 { 521 { &ifm_subtype_shared_descriptions[0], 0 }, 522 { &ifm_subtype_shared_aliases[0], 1 }, 523 { &ifm_subtype_atm_descriptions[0], 0 }, 524 { &ifm_subtype_atm_aliases[0], 1 }, 525 { NULL, 0 }, 526 }, 527 { 528 { &ifm_shared_option_descriptions[0], 0 }, 529 { &ifm_shared_option_aliases[0], 1 }, 530 { &ifm_subtype_atm_option_descriptions[0], 0 }, 531 { NULL, 0 }, 532 }, 533 { 534 { NULL, 0 }, 535 }, 536 }, 537 }; 538 539 int 540 get_media_subtype(int type, const char *val) 541 { 542 struct ifmedia_description *desc; 543 struct ifmedia_type_to_subtype *ttos; 544 int rval, i; 545 546 /* Find the top-level interface type. */ 547 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 548 desc->ifmt_string != NULL; desc++, ttos++) 549 if (type == desc->ifmt_word) 550 break; 551 if (desc->ifmt_string == NULL) 552 errx(1, "unknown media type 0x%x", type); 553 554 for (i = 0; ttos->subtypes[i].desc != NULL; i++) { 555 rval = lookup_media_word(ttos->subtypes[i].desc, val); 556 if (rval != -1) 557 return (rval); 558 } 559 errx(1, "unknown media subtype: %s", val); 560 /*NOTREACHED*/ 561 } 562 563 int 564 get_media_mode(int type, const char *val) 565 { 566 struct ifmedia_description *desc; 567 struct ifmedia_type_to_subtype *ttos; 568 int rval, i; 569 570 /* Find the top-level interface type. */ 571 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 572 desc->ifmt_string != NULL; desc++, ttos++) 573 if (type == desc->ifmt_word) 574 break; 575 if (desc->ifmt_string == NULL) 576 errx(1, "unknown media mode 0x%x", type); 577 578 for (i = 0; ttos->modes[i].desc != NULL; i++) { 579 rval = lookup_media_word(ttos->modes[i].desc, val); 580 if (rval != -1) 581 return (rval); 582 } 583 return -1; 584 } 585 586 int 587 get_media_options(int type, const char *val) 588 { 589 struct ifmedia_description *desc; 590 struct ifmedia_type_to_subtype *ttos; 591 char *optlist, *optptr; 592 int option = 0, i, rval = 0; 593 594 /* We muck with the string, so copy it. */ 595 optlist = strdup(val); 596 if (optlist == NULL) 597 err(1, "strdup"); 598 599 /* Find the top-level interface type. */ 600 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 601 desc->ifmt_string != NULL; desc++, ttos++) 602 if (type == desc->ifmt_word) 603 break; 604 if (desc->ifmt_string == NULL) 605 errx(1, "unknown media type 0x%x", type); 606 607 /* 608 * Look up the options in the user-provided comma-separated 609 * list. 610 */ 611 optptr = optlist; 612 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) { 613 for (i = 0; ttos->options[i].desc != NULL; i++) { 614 option = lookup_media_word(ttos->options[i].desc, optptr); 615 if (option != -1) 616 break; 617 } 618 if (option == 0) 619 errx(1, "unknown option: %s", optptr); 620 rval |= option; 621 } 622 623 free(optlist); 624 return (rval); 625 } 626 627 int 628 lookup_media_word(struct ifmedia_description *desc, const char *val) 629 { 630 631 for (; desc->ifmt_string != NULL; desc++) 632 if (strcasecmp(desc->ifmt_string, val) == 0) 633 return (desc->ifmt_word); 634 635 return (-1); 636 } 637 638 static struct ifmedia_description *get_toptype_desc(int ifmw) 639 { 640 struct ifmedia_description *desc; 641 642 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++) 643 if (IFM_TYPE(ifmw) == desc->ifmt_word) 644 break; 645 646 return desc; 647 } 648 649 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw) 650 { 651 struct ifmedia_description *desc; 652 struct ifmedia_type_to_subtype *ttos; 653 654 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 655 desc->ifmt_string != NULL; desc++, ttos++) 656 if (IFM_TYPE(ifmw) == desc->ifmt_word) 657 break; 658 659 return ttos; 660 } 661 662 static struct ifmedia_description *get_subtype_desc(int ifmw, 663 struct ifmedia_type_to_subtype *ttos) 664 { 665 int i; 666 struct ifmedia_description *desc; 667 668 for (i = 0; ttos->subtypes[i].desc != NULL; i++) { 669 if (ttos->subtypes[i].alias) 670 continue; 671 for (desc = ttos->subtypes[i].desc; 672 desc->ifmt_string != NULL; desc++) { 673 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) 674 return desc; 675 } 676 } 677 678 return NULL; 679 } 680 681 static struct ifmedia_description *get_mode_desc(int ifmw, 682 struct ifmedia_type_to_subtype *ttos) 683 { 684 int i; 685 struct ifmedia_description *desc; 686 687 for (i = 0; ttos->modes[i].desc != NULL; i++) { 688 if (ttos->modes[i].alias) 689 continue; 690 for (desc = ttos->modes[i].desc; 691 desc->ifmt_string != NULL; desc++) { 692 if (IFM_MODE(ifmw) == desc->ifmt_word) 693 return desc; 694 } 695 } 696 697 return NULL; 698 } 699 700 void 701 print_media_word(int ifmw, int print_toptype) 702 { 703 struct ifmedia_description *desc; 704 struct ifmedia_type_to_subtype *ttos; 705 int seen_option = 0, i; 706 707 /* Find the top-level interface type. */ 708 desc = get_toptype_desc(ifmw); 709 ttos = get_toptype_ttos(ifmw); 710 if (desc->ifmt_string == NULL) { 711 printf("<unknown type>"); 712 return; 713 } else if (print_toptype) { 714 printf("%s", desc->ifmt_string); 715 } 716 717 /* 718 * Don't print the top-level type; it's not like we can 719 * change it, or anything. 720 */ 721 722 /* Find subtype. */ 723 desc = get_subtype_desc(ifmw, ttos); 724 if (desc == NULL) { 725 printf("<unknown subtype>"); 726 return; 727 } 728 729 if (print_toptype) 730 putchar(' '); 731 732 printf("%s", desc->ifmt_string); 733 734 if (print_toptype) { 735 desc = get_mode_desc(ifmw, ttos); 736 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string)) 737 printf(" mode %s", desc->ifmt_string); 738 } 739 740 /* Find options. */ 741 for (i = 0; ttos->options[i].desc != NULL; i++) { 742 if (ttos->options[i].alias) 743 continue; 744 for (desc = ttos->options[i].desc; 745 desc->ifmt_string != NULL; desc++) { 746 if (ifmw & desc->ifmt_word) { 747 if (seen_option == 0) 748 printf(" <"); 749 printf("%s%s", seen_option++ ? "," : "", 750 desc->ifmt_string); 751 } 752 } 753 } 754 printf("%s", seen_option ? ">" : ""); 755 756 if (print_toptype && IFM_INST(ifmw) != 0) 757 printf(" instance %d", IFM_INST(ifmw)); 758 } 759 760 void 761 print_media_word_ifconfig(int ifmw) 762 { 763 struct ifmedia_description *desc; 764 struct ifmedia_type_to_subtype *ttos; 765 int seen_option = 0, i; 766 767 /* Find the top-level interface type. */ 768 desc = get_toptype_desc(ifmw); 769 ttos = get_toptype_ttos(ifmw); 770 if (desc->ifmt_string == NULL) { 771 printf("<unknown type>"); 772 return; 773 } 774 775 /* 776 * Don't print the top-level type; it's not like we can 777 * change it, or anything. 778 */ 779 780 /* Find subtype. */ 781 desc = get_subtype_desc(ifmw, ttos); 782 if (desc == NULL) { 783 printf("<unknown subtype>"); 784 return; 785 } 786 787 printf("media %s", desc->ifmt_string); 788 789 desc = get_mode_desc(ifmw, ttos); 790 if (desc != NULL) 791 printf(" mode %s", desc->ifmt_string); 792 793 /* Find options. */ 794 for (i = 0; ttos->options[i].desc != NULL; i++) { 795 if (ttos->options[i].alias) 796 continue; 797 for (desc = ttos->options[i].desc; 798 desc->ifmt_string != NULL; desc++) { 799 if (ifmw & desc->ifmt_word) { 800 if (seen_option == 0) 801 printf(" mediaopt "); 802 printf("%s%s", seen_option++ ? "," : "", 803 desc->ifmt_string); 804 } 805 } 806 } 807 808 if (IFM_INST(ifmw) != 0) 809 printf(" instance %d", IFM_INST(ifmw)); 810 } 811 812 /********************************************************************** 813 * ...until here. 814 **********************************************************************/ 815