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