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