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_tokenring_descriptions[] = 384 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS; 385 386 static struct ifmedia_description ifm_subtype_tokenring_aliases[] = 387 IFM_SUBTYPE_TOKENRING_ALIASES; 388 389 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] = 390 IFM_SUBTYPE_TOKENRING_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 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] = 402 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS; 403 404 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_ieee80211_descriptions[0], 0 }, 486 { &ifm_subtype_ieee80211_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_ieee80211_option_descriptions[0], 0 }, 493 { NULL, 0 }, 494 }, 495 { 496 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 }, 497 { &ifm_subtype_ieee80211_mode_aliases[0], 0 }, 498 { NULL, 0 }, 499 }, 500 }, 501 { 502 { 503 { &ifm_subtype_shared_descriptions[0], 0 }, 504 { &ifm_subtype_shared_aliases[0], 1 }, 505 { &ifm_subtype_atm_descriptions[0], 0 }, 506 { &ifm_subtype_atm_aliases[0], 1 }, 507 { NULL, 0 }, 508 }, 509 { 510 { &ifm_shared_option_descriptions[0], 0 }, 511 { &ifm_shared_option_aliases[0], 1 }, 512 { &ifm_subtype_atm_option_descriptions[0], 0 }, 513 { NULL, 0 }, 514 }, 515 { 516 { NULL, 0 }, 517 }, 518 }, 519 }; 520 521 static int 522 get_media_subtype(int type, const char *val) 523 { 524 struct ifmedia_description *desc; 525 struct ifmedia_type_to_subtype *ttos; 526 int rval, i; 527 528 /* Find the top-level interface type. */ 529 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 530 desc->ifmt_string != NULL; desc++, ttos++) 531 if (type == desc->ifmt_word) 532 break; 533 if (desc->ifmt_string == NULL) 534 errx(1, "unknown media type 0x%x", type); 535 536 for (i = 0; ttos->subtypes[i].desc != NULL; i++) { 537 rval = lookup_media_word(ttos->subtypes[i].desc, val); 538 if (rval != -1) 539 return (rval); 540 } 541 errx(1, "unknown media subtype: %s", val); 542 /*NOTREACHED*/ 543 } 544 545 static int 546 get_media_mode(int type, const char *val) 547 { 548 struct ifmedia_description *desc; 549 struct ifmedia_type_to_subtype *ttos; 550 int rval, i; 551 552 /* Find the top-level interface type. */ 553 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 554 desc->ifmt_string != NULL; desc++, ttos++) 555 if (type == desc->ifmt_word) 556 break; 557 if (desc->ifmt_string == NULL) 558 errx(1, "unknown media mode 0x%x", type); 559 560 for (i = 0; ttos->modes[i].desc != NULL; i++) { 561 rval = lookup_media_word(ttos->modes[i].desc, val); 562 if (rval != -1) 563 return (rval); 564 } 565 return -1; 566 } 567 568 static int 569 get_media_options(int type, const char *val) 570 { 571 struct ifmedia_description *desc; 572 struct ifmedia_type_to_subtype *ttos; 573 char *optlist, *optptr; 574 int option = 0, i, rval = 0; 575 576 /* We muck with the string, so copy it. */ 577 optlist = strdup(val); 578 if (optlist == NULL) 579 err(1, "strdup"); 580 581 /* Find the top-level interface type. */ 582 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 583 desc->ifmt_string != NULL; desc++, ttos++) 584 if (type == desc->ifmt_word) 585 break; 586 if (desc->ifmt_string == NULL) 587 errx(1, "unknown media type 0x%x", type); 588 589 /* 590 * Look up the options in the user-provided comma-separated 591 * list. 592 */ 593 optptr = optlist; 594 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) { 595 for (i = 0; ttos->options[i].desc != NULL; i++) { 596 option = lookup_media_word(ttos->options[i].desc, optptr); 597 if (option != -1) 598 break; 599 } 600 if (option == 0) 601 errx(1, "unknown option: %s", optptr); 602 rval |= option; 603 } 604 605 free(optlist); 606 return (rval); 607 } 608 609 static int 610 lookup_media_word(struct ifmedia_description *desc, const char *val) 611 { 612 613 for (; desc->ifmt_string != NULL; desc++) 614 if (strcasecmp(desc->ifmt_string, val) == 0) 615 return (desc->ifmt_word); 616 617 return (-1); 618 } 619 620 static struct ifmedia_description *get_toptype_desc(int ifmw) 621 { 622 struct ifmedia_description *desc; 623 624 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++) 625 if (IFM_TYPE(ifmw) == desc->ifmt_word) 626 break; 627 628 return desc; 629 } 630 631 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw) 632 { 633 struct ifmedia_description *desc; 634 struct ifmedia_type_to_subtype *ttos; 635 636 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; 637 desc->ifmt_string != NULL; desc++, ttos++) 638 if (IFM_TYPE(ifmw) == desc->ifmt_word) 639 break; 640 641 return ttos; 642 } 643 644 static struct ifmedia_description *get_subtype_desc(int ifmw, 645 struct ifmedia_type_to_subtype *ttos) 646 { 647 int i; 648 struct ifmedia_description *desc; 649 650 for (i = 0; ttos->subtypes[i].desc != NULL; i++) { 651 if (ttos->subtypes[i].alias) 652 continue; 653 for (desc = ttos->subtypes[i].desc; 654 desc->ifmt_string != NULL; desc++) { 655 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) 656 return desc; 657 } 658 } 659 660 return NULL; 661 } 662 663 static struct ifmedia_description *get_mode_desc(int ifmw, 664 struct ifmedia_type_to_subtype *ttos) 665 { 666 int i; 667 struct ifmedia_description *desc; 668 669 for (i = 0; ttos->modes[i].desc != NULL; i++) { 670 if (ttos->modes[i].alias) 671 continue; 672 for (desc = ttos->modes[i].desc; 673 desc->ifmt_string != NULL; desc++) { 674 if (IFM_MODE(ifmw) == desc->ifmt_word) 675 return desc; 676 } 677 } 678 679 return NULL; 680 } 681 682 static void 683 print_media_word(int ifmw, int print_toptype) 684 { 685 struct ifmedia_description *desc; 686 struct ifmedia_type_to_subtype *ttos; 687 int seen_option = 0, i; 688 689 /* Find the top-level interface type. */ 690 desc = get_toptype_desc(ifmw); 691 ttos = get_toptype_ttos(ifmw); 692 if (desc->ifmt_string == NULL) { 693 printf("<unknown type>"); 694 return; 695 } else if (print_toptype) { 696 printf("%s", desc->ifmt_string); 697 } 698 699 /* 700 * Don't print the top-level type; it's not like we can 701 * change it, or anything. 702 */ 703 704 /* Find subtype. */ 705 desc = get_subtype_desc(ifmw, ttos); 706 if (desc == NULL) { 707 printf("<unknown subtype>"); 708 return; 709 } 710 711 if (print_toptype) 712 putchar(' '); 713 714 printf("%s", desc->ifmt_string); 715 716 if (print_toptype) { 717 desc = get_mode_desc(ifmw, ttos); 718 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string)) 719 printf(" mode %s", desc->ifmt_string); 720 } 721 722 /* Find options. */ 723 for (i = 0; ttos->options[i].desc != NULL; i++) { 724 if (ttos->options[i].alias) 725 continue; 726 for (desc = ttos->options[i].desc; 727 desc->ifmt_string != NULL; desc++) { 728 if (ifmw & desc->ifmt_word) { 729 if (seen_option == 0) 730 printf(" <"); 731 printf("%s%s", seen_option++ ? "," : "", 732 desc->ifmt_string); 733 } 734 } 735 } 736 printf("%s", seen_option ? ">" : ""); 737 738 if (print_toptype && IFM_INST(ifmw) != 0) 739 printf(" instance %d", IFM_INST(ifmw)); 740 } 741 742 static void 743 print_media_word_ifconfig(int ifmw) 744 { 745 struct ifmedia_description *desc; 746 struct ifmedia_type_to_subtype *ttos; 747 int seen_option = 0, i; 748 749 /* Find the top-level interface type. */ 750 desc = get_toptype_desc(ifmw); 751 ttos = get_toptype_ttos(ifmw); 752 if (desc->ifmt_string == NULL) { 753 printf("<unknown type>"); 754 return; 755 } 756 757 /* 758 * Don't print the top-level type; it's not like we can 759 * change it, or anything. 760 */ 761 762 /* Find subtype. */ 763 desc = get_subtype_desc(ifmw, ttos); 764 if (desc == NULL) { 765 printf("<unknown subtype>"); 766 return; 767 } 768 769 printf("media %s", desc->ifmt_string); 770 771 desc = get_mode_desc(ifmw, ttos); 772 if (desc != NULL) 773 printf(" mode %s", desc->ifmt_string); 774 775 /* Find options. */ 776 for (i = 0; ttos->options[i].desc != NULL; i++) { 777 if (ttos->options[i].alias) 778 continue; 779 for (desc = ttos->options[i].desc; 780 desc->ifmt_string != NULL; desc++) { 781 if (ifmw & desc->ifmt_word) { 782 if (seen_option == 0) 783 printf(" mediaopt "); 784 printf("%s%s", seen_option++ ? "," : "", 785 desc->ifmt_string); 786 } 787 } 788 } 789 790 if (IFM_INST(ifmw) != 0) 791 printf(" instance %d", IFM_INST(ifmw)); 792 } 793 794 /********************************************************************** 795 * ...until here. 796 **********************************************************************/ 797 798 static struct cmd media_cmds[] = { 799 DEF_CMD_ARG("media", setmedia), 800 DEF_CMD_ARG("mode", setmediamode), 801 DEF_CMD_ARG("mediaopt", setmediaopt), 802 DEF_CMD_ARG("-mediaopt",unsetmediaopt), 803 DEF_CMD_ARG("inst", setmediainst), 804 DEF_CMD_ARG("instance", setmediainst), 805 }; 806 static struct afswtch af_media = { 807 .af_name = "af_media", 808 .af_af = AF_UNSPEC, 809 .af_other_status = media_status, 810 }; 811 812 static __constructor void 813 ifmedia_ctor(void) 814 { 815 size_t i; 816 817 for (i = 0; i < nitems(media_cmds); i++) 818 cmd_register(&media_cmds[i]); 819 af_register(&af_media); 820 } 821