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