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