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