]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sbin/ifconfig/ifmedia.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sbin / ifconfig / ifmedia.c
1 /*      $NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $      */
2 /* $FreeBSD$ */
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         (void) memset(&ifmr, 0, sizeof(ifmr));
112         (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
113
114         if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&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 = (int *)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, (caddr_t)&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                 case IFM_ATM:
150                         if (ifmr.ifm_status & IFM_ACTIVE)
151                                 printf("active");
152                         else
153                                 printf("no carrier");
154                         break;
155
156                 case IFM_FDDI:
157                 case IFM_TOKEN:
158                         if (ifmr.ifm_status & IFM_ACTIVE)
159                                 printf("inserted");
160                         else
161                                 printf("no ring");
162                         break;
163
164                 case IFM_IEEE80211:
165                         /* XXX: Different value for adhoc? */
166                         if (ifmr.ifm_status & IFM_ACTIVE)
167                                 printf("associated");
168                         else
169                                 printf("no carrier");
170                         break;
171                 }
172                 putchar('\n');
173         }
174
175         if (ifmr.ifm_count > 0 && supmedia) {
176                 printf("\tsupported media:\n");
177                 for (i = 0; i < ifmr.ifm_count; i++) {
178                         printf("\t\t");
179                         print_media_word_ifconfig(media_list[i]);
180                         putchar('\n');
181                 }
182         }
183
184         free(media_list);
185 }
186
187 struct ifmediareq *
188 ifmedia_getstate(int s)
189 {
190         static struct ifmediareq *ifmr = NULL;
191         int *mwords;
192
193         if (ifmr == NULL) {
194                 ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq));
195                 if (ifmr == NULL)
196                         err(1, "malloc");
197
198                 (void) memset(ifmr, 0, sizeof(struct ifmediareq));
199                 (void) strncpy(ifmr->ifm_name, name,
200                     sizeof(ifmr->ifm_name));
201
202                 ifmr->ifm_count = 0;
203                 ifmr->ifm_ulist = NULL;
204
205                 /*
206                  * We must go through the motions of reading all
207                  * supported media because we need to know both
208                  * the current media type and the top-level type.
209                  */
210
211                 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) {
212                         err(1, "SIOCGIFMEDIA");
213                 }
214
215                 if (ifmr->ifm_count == 0)
216                         errx(1, "%s: no media types?", name);
217
218                 mwords = (int *)malloc(ifmr->ifm_count * sizeof(int));
219                 if (mwords == NULL)
220                         err(1, "malloc");
221   
222                 ifmr->ifm_ulist = mwords;
223                 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0)
224                         err(1, "SIOCGIFMEDIA");
225         }
226
227         return ifmr;
228 }
229
230 static void
231 setifmediacallback(int s, void *arg)
232 {
233         struct ifmediareq *ifmr = (struct ifmediareq *)arg;
234         static int did_it = 0;
235
236         if (!did_it) {
237                 ifr.ifr_media = ifmr->ifm_current;
238                 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
239                         err(1, "SIOCSIFMEDIA (media)");
240                 free(ifmr->ifm_ulist);
241                 free(ifmr);
242                 did_it = 1;
243         }
244 }
245
246 static void
247 setmedia(const char *val, int d, int s, const struct afswtch *afp)
248 {
249         struct ifmediareq *ifmr;
250         int subtype;
251
252         ifmr = ifmedia_getstate(s);
253
254         /*
255          * We are primarily concerned with the top-level type.
256          * However, "current" may be only IFM_NONE, so we just look
257          * for the top-level type in the first "supported type"
258          * entry.
259          *
260          * (I'm assuming that all supported media types for a given
261          * interface will be the same top-level type..)
262          */
263         subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val);
264
265         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
266         ifr.ifr_media = (ifmr->ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
267             IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
268
269         if ((ifr.ifr_media & IFM_TMASK) == 0) {
270                 ifr.ifr_media &= ~IFM_GMASK;
271         }
272
273         ifmr->ifm_current = ifr.ifr_media;
274         callback_register(setifmediacallback, (void *)ifmr);
275 }
276
277 static void
278 setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
279 {
280
281         domediaopt(val, 0, s);
282 }
283
284 static void
285 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
286 {
287
288         domediaopt(val, 1, s);
289 }
290
291 static void
292 domediaopt(const char *val, int clear, int s)
293 {
294         struct ifmediareq *ifmr;
295         int options;
296
297         ifmr = ifmedia_getstate(s);
298
299         options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
300
301         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
302         ifr.ifr_media = ifmr->ifm_current;
303         if (clear)
304                 ifr.ifr_media &= ~options;
305         else {
306                 if (options & IFM_HDX) {
307                         ifr.ifr_media &= ~IFM_FDX;
308                         options &= ~IFM_HDX;
309                 }
310                 ifr.ifr_media |= options;
311         }
312         ifmr->ifm_current = ifr.ifr_media;
313         callback_register(setifmediacallback, (void *)ifmr);
314 }
315
316 static void
317 setmediainst(const char *val, int d, int s, const struct afswtch *afp)
318 {
319         struct ifmediareq *ifmr;
320         int inst;
321
322         ifmr = ifmedia_getstate(s);
323
324         inst = atoi(val);
325         if (inst < 0 || inst > IFM_INST_MAX)
326                 errx(1, "invalid media instance: %s", val);
327
328         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
329         ifr.ifr_media = (ifmr->ifm_current & ~IFM_IMASK) | inst << IFM_ISHIFT;
330
331         ifmr->ifm_current = ifr.ifr_media;
332         callback_register(setifmediacallback, (void *)ifmr);
333 }
334
335 static void
336 setmediamode(const char *val, int d, int s, const struct afswtch *afp)
337 {
338         struct ifmediareq *ifmr;
339         int mode;
340
341         ifmr = ifmedia_getstate(s);
342
343         mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
344
345         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
346         ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode;
347
348         ifmr->ifm_current = ifr.ifr_media;
349         callback_register(setifmediacallback, (void *)ifmr);
350 }
351
352 /**********************************************************************
353  * A good chunk of this is duplicated from sys/net/ifmedia.c
354  **********************************************************************/
355
356 static struct ifmedia_description ifm_type_descriptions[] =
357     IFM_TYPE_DESCRIPTIONS;
358
359 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
360     IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
361
362 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
363     IFM_SUBTYPE_ETHERNET_ALIASES;
364
365 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
366     IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
367
368 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
369     IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
370
371 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
372     IFM_SUBTYPE_TOKENRING_ALIASES;
373
374 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
375     IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
376
377 static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
378     IFM_SUBTYPE_FDDI_DESCRIPTIONS;
379
380 static struct ifmedia_description ifm_subtype_fddi_aliases[] =
381     IFM_SUBTYPE_FDDI_ALIASES;
382
383 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
384     IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
385
386 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
387     IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
388
389 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
390     IFM_SUBTYPE_IEEE80211_ALIASES;
391
392 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
393     IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
394
395 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
396     IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
397
398 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
399     IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
400
401 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
402     IFM_SUBTYPE_ATM_DESCRIPTIONS;
403
404 static struct ifmedia_description ifm_subtype_atm_aliases[] =
405     IFM_SUBTYPE_ATM_ALIASES;
406
407 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
408     IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
409
410 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
411     IFM_SUBTYPE_SHARED_DESCRIPTIONS;
412
413 static struct ifmedia_description ifm_subtype_shared_aliases[] =
414     IFM_SUBTYPE_SHARED_ALIASES;
415
416 static struct ifmedia_description ifm_shared_option_descriptions[] =
417     IFM_SHARED_OPTION_DESCRIPTIONS;
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[3];
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_subtype_ethernet_option_descriptions[0], 0 },
447                         { NULL, 0 },
448                 },
449                 {
450                         { NULL, 0 },
451                 },
452         },
453         {
454                 {
455                         { &ifm_subtype_shared_descriptions[0], 0 },
456                         { &ifm_subtype_shared_aliases[0], 1 },
457                         { &ifm_subtype_tokenring_descriptions[0], 0 },
458                         { &ifm_subtype_tokenring_aliases[0], 1 },
459                         { NULL, 0 },
460                 },
461                 {
462                         { &ifm_shared_option_descriptions[0], 0 },
463                         { &ifm_subtype_tokenring_option_descriptions[0], 0 },
464                         { NULL, 0 },
465                 },
466                 {
467                         { NULL, 0 },
468                 },
469         },
470         {
471                 {
472                         { &ifm_subtype_shared_descriptions[0], 0 },
473                         { &ifm_subtype_shared_aliases[0], 1 },
474                         { &ifm_subtype_fddi_descriptions[0], 0 },
475                         { &ifm_subtype_fddi_aliases[0], 1 },
476                         { NULL, 0 },
477                 },
478                 {
479                         { &ifm_shared_option_descriptions[0], 0 },
480                         { &ifm_subtype_fddi_option_descriptions[0], 0 },
481                         { NULL, 0 },
482                 },
483                 {
484                         { NULL, 0 },
485                 },
486         },
487         {
488                 {
489                         { &ifm_subtype_shared_descriptions[0], 0 },
490                         { &ifm_subtype_shared_aliases[0], 1 },
491                         { &ifm_subtype_ieee80211_descriptions[0], 0 },
492                         { &ifm_subtype_ieee80211_aliases[0], 1 },
493                         { NULL, 0 },
494                 },
495                 {
496                         { &ifm_shared_option_descriptions[0], 0 },
497                         { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
498                         { NULL, 0 },
499                 },
500                 {
501                         { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
502                         { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
503                         { NULL, 0 },
504                 },
505         },
506         {
507                 {
508                         { &ifm_subtype_shared_descriptions[0], 0 },
509                         { &ifm_subtype_shared_aliases[0], 1 },
510                         { &ifm_subtype_atm_descriptions[0], 0 },
511                         { &ifm_subtype_atm_aliases[0], 1 },
512                         { NULL, 0 },
513                 },
514                 {
515                         { &ifm_shared_option_descriptions[0], 0 },
516                         { &ifm_subtype_atm_option_descriptions[0], 0 },
517                         { NULL, 0 },
518                 },
519                 {
520                         { NULL, 0 },
521                 },
522         },
523 };
524
525 static int
526 get_media_subtype(int type, const char *val)
527 {
528         struct ifmedia_description *desc;
529         struct ifmedia_type_to_subtype *ttos;
530         int rval, i;
531
532         /* Find the top-level interface type. */
533         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
534             desc->ifmt_string != NULL; desc++, ttos++)
535                 if (type == desc->ifmt_word)
536                         break;
537         if (desc->ifmt_string == NULL)
538                 errx(1, "unknown media type 0x%x", type);
539
540         for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
541                 rval = lookup_media_word(ttos->subtypes[i].desc, val);
542                 if (rval != -1)
543                         return (rval);
544         }
545         errx(1, "unknown media subtype: %s", val);
546         /*NOTREACHED*/
547 }
548
549 static int
550 get_media_mode(int type, const char *val)
551 {
552         struct ifmedia_description *desc;
553         struct ifmedia_type_to_subtype *ttos;
554         int rval, i;
555
556         /* Find the top-level interface type. */
557         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
558             desc->ifmt_string != NULL; desc++, ttos++)
559                 if (type == desc->ifmt_word)
560                         break;
561         if (desc->ifmt_string == NULL)
562                 errx(1, "unknown media mode 0x%x", type);
563
564         for (i = 0; ttos->modes[i].desc != NULL; i++) {
565                 rval = lookup_media_word(ttos->modes[i].desc, val);
566                 if (rval != -1)
567                         return (rval);
568         }
569         return -1;
570 }
571
572 static int
573 get_media_options(int type, const char *val)
574 {
575         struct ifmedia_description *desc;
576         struct ifmedia_type_to_subtype *ttos;
577         char *optlist, *optptr;
578         int option = 0, i, rval = 0;
579
580         /* We muck with the string, so copy it. */
581         optlist = strdup(val);
582         if (optlist == NULL)
583                 err(1, "strdup");
584
585         /* Find the top-level interface type. */
586         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
587             desc->ifmt_string != NULL; desc++, ttos++)
588                 if (type == desc->ifmt_word)
589                         break;
590         if (desc->ifmt_string == NULL)
591                 errx(1, "unknown media type 0x%x", type);
592
593         /*
594          * Look up the options in the user-provided comma-separated
595          * list.
596          */
597         optptr = optlist;
598         for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
599                 for (i = 0; ttos->options[i].desc != NULL; i++) {
600                         option = lookup_media_word(ttos->options[i].desc, optptr);
601                         if (option != -1)
602                                 break;
603                 }
604                 if (option == 0)
605                         errx(1, "unknown option: %s", optptr);
606                 rval |= option;
607         }
608
609         free(optlist);
610         return (rval);
611 }
612
613 static int
614 lookup_media_word(struct ifmedia_description *desc, const char *val)
615 {
616
617         for (; desc->ifmt_string != NULL; desc++)
618                 if (strcasecmp(desc->ifmt_string, val) == 0)
619                         return (desc->ifmt_word);
620
621         return (-1);
622 }
623
624 static struct ifmedia_description *get_toptype_desc(int ifmw)
625 {
626         struct ifmedia_description *desc;
627
628         for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
629                 if (IFM_TYPE(ifmw) == desc->ifmt_word)
630                         break;
631
632         return desc;
633 }
634
635 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
636 {
637         struct ifmedia_description *desc;
638         struct ifmedia_type_to_subtype *ttos;
639
640         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
641             desc->ifmt_string != NULL; desc++, ttos++)
642                 if (IFM_TYPE(ifmw) == desc->ifmt_word)
643                         break;
644
645         return ttos;
646 }
647
648 static struct ifmedia_description *get_subtype_desc(int ifmw, 
649     struct ifmedia_type_to_subtype *ttos)
650 {
651         int i;
652         struct ifmedia_description *desc;
653
654         for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
655                 if (ttos->subtypes[i].alias)
656                         continue;
657                 for (desc = ttos->subtypes[i].desc;
658                     desc->ifmt_string != NULL; desc++) {
659                         if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
660                                 return desc;
661                 }
662         }
663
664         return NULL;
665 }
666
667 static struct ifmedia_description *get_mode_desc(int ifmw, 
668     struct ifmedia_type_to_subtype *ttos)
669 {
670         int i;
671         struct ifmedia_description *desc;
672
673         for (i = 0; ttos->modes[i].desc != NULL; i++) {
674                 if (ttos->modes[i].alias)
675                         continue;
676                 for (desc = ttos->modes[i].desc;
677                     desc->ifmt_string != NULL; desc++) {
678                         if (IFM_MODE(ifmw) == desc->ifmt_word)
679                                 return desc;
680                 }
681         }
682
683         return NULL;
684 }
685
686 static void
687 print_media_word(int ifmw, int print_toptype)
688 {
689         struct ifmedia_description *desc;
690         struct ifmedia_type_to_subtype *ttos;
691         int seen_option = 0, i;
692
693         /* Find the top-level interface type. */
694         desc = get_toptype_desc(ifmw);
695         ttos = get_toptype_ttos(ifmw);
696         if (desc->ifmt_string == NULL) {
697                 printf("<unknown type>");
698                 return;
699         } else if (print_toptype) {
700                 printf("%s", desc->ifmt_string);
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                 printf("<unknown subtype>");
712                 return;
713         }
714
715         if (print_toptype)
716                 putchar(' ');
717
718         printf("%s", desc->ifmt_string);
719
720         if (print_toptype) {
721                 desc = get_mode_desc(ifmw, ttos);
722                 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
723                         printf(" mode %s", desc->ifmt_string);
724         }
725
726         /* Find options. */
727         for (i = 0; ttos->options[i].desc != NULL; i++) {
728                 if (ttos->options[i].alias)
729                         continue;
730                 for (desc = ttos->options[i].desc;
731                     desc->ifmt_string != NULL; desc++) {
732                         if (ifmw & desc->ifmt_word) {
733                                 if (seen_option == 0)
734                                         printf(" <");
735                                 printf("%s%s", seen_option++ ? "," : "",
736                                     desc->ifmt_string);
737                         }
738                 }
739         }
740         printf("%s", seen_option ? ">" : "");
741
742         if (print_toptype && IFM_INST(ifmw) != 0)
743                 printf(" instance %d", IFM_INST(ifmw));
744 }
745
746 static void
747 print_media_word_ifconfig(int ifmw)
748 {
749         struct ifmedia_description *desc;
750         struct ifmedia_type_to_subtype *ttos;
751         int i;
752
753         /* Find the top-level interface type. */
754         desc = get_toptype_desc(ifmw);
755         ttos = get_toptype_ttos(ifmw);
756         if (desc->ifmt_string == NULL) {
757                 printf("<unknown type>");
758                 return;
759         }
760
761         /*
762          * Don't print the top-level type; it's not like we can
763          * change it, or anything.
764          */
765
766         /* Find subtype. */
767         desc = get_subtype_desc(ifmw, ttos);
768         if (desc == NULL) {
769                 printf("<unknown subtype>");
770                 return;
771         }
772
773         printf("media %s", desc->ifmt_string);
774
775         desc = get_mode_desc(ifmw, ttos);
776         if (desc != NULL)
777                 printf(" mode %s", desc->ifmt_string);
778
779         /* Find options. */
780         for (i = 0; ttos->options[i].desc != NULL; i++) {
781                 if (ttos->options[i].alias)
782                         continue;
783                 for (desc = ttos->options[i].desc;
784                     desc->ifmt_string != NULL; desc++) {
785                         if (ifmw & desc->ifmt_word) {
786                                 printf(" mediaopt %s", desc->ifmt_string);
787                         }
788                 }
789         }
790
791         if (IFM_INST(ifmw) != 0)
792                 printf(" instance %d", IFM_INST(ifmw));
793 }
794
795 /**********************************************************************
796  * ...until here.
797  **********************************************************************/
798
799 static struct cmd media_cmds[] = {
800         DEF_CMD_ARG("media",    setmedia),
801         DEF_CMD_ARG("mode",     setmediamode),
802         DEF_CMD_ARG("mediaopt", setmediaopt),
803         DEF_CMD_ARG("-mediaopt",unsetmediaopt),
804         DEF_CMD_ARG("inst",     setmediainst),
805         DEF_CMD_ARG("instance", setmediainst),
806 };
807 static struct afswtch af_media = {
808         .af_name        = "af_media",
809         .af_af          = AF_UNSPEC,
810         .af_other_status = media_status,
811 };
812
813 static __constructor void
814 ifmedia_ctor(void)
815 {
816 #define N(a)    (sizeof(a) / sizeof(a[0]))
817         int i;
818
819         for (i = 0; i < N(media_cmds);  i++)
820                 cmd_register(&media_cmds[i]);
821         af_register(&af_media);
822 #undef N
823 }