]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sbin/ifconfig/ifmedia.c
MFC r306478:
[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_IMASK) |
276             IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
277
278         ifmr->ifm_current = ifr.ifr_media;
279         callback_register(setifmediacallback, (void *)ifmr);
280 }
281
282 static void
283 setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
284 {
285
286         domediaopt(val, 0, s);
287 }
288
289 static void
290 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
291 {
292
293         domediaopt(val, 1, s);
294 }
295
296 static void
297 domediaopt(const char *val, int clear, int s)
298 {
299         struct ifmediareq *ifmr;
300         int options;
301
302         ifmr = ifmedia_getstate(s);
303
304         options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
305
306         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
307         ifr.ifr_media = ifmr->ifm_current;
308         if (clear)
309                 ifr.ifr_media &= ~options;
310         else {
311                 if (options & IFM_HDX) {
312                         ifr.ifr_media &= ~IFM_FDX;
313                         options &= ~IFM_HDX;
314                 }
315                 ifr.ifr_media |= options;
316         }
317         ifmr->ifm_current = ifr.ifr_media;
318         callback_register(setifmediacallback, (void *)ifmr);
319 }
320
321 static void
322 setmediainst(const char *val, int d, int s, const struct afswtch *afp)
323 {
324         struct ifmediareq *ifmr;
325         int inst;
326
327         ifmr = ifmedia_getstate(s);
328
329         inst = atoi(val);
330         if (inst < 0 || inst > (int)IFM_INST_MAX)
331                 errx(1, "invalid media instance: %s", val);
332
333         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
334         ifr.ifr_media = (ifmr->ifm_current & ~IFM_IMASK) | inst << IFM_ISHIFT;
335
336         ifmr->ifm_current = ifr.ifr_media;
337         callback_register(setifmediacallback, (void *)ifmr);
338 }
339
340 static void
341 setmediamode(const char *val, int d, int s, const struct afswtch *afp)
342 {
343         struct ifmediareq *ifmr;
344         int mode;
345
346         ifmr = ifmedia_getstate(s);
347
348         mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
349
350         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
351         ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode;
352
353         ifmr->ifm_current = ifr.ifr_media;
354         callback_register(setifmediacallback, (void *)ifmr);
355 }
356
357 /**********************************************************************
358  * A good chunk of this is duplicated from sys/net/ifmedia.c
359  **********************************************************************/
360
361 static struct ifmedia_description ifm_type_descriptions[] =
362     IFM_TYPE_DESCRIPTIONS;
363
364 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
365     IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
366
367 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
368     IFM_SUBTYPE_ETHERNET_ALIASES;
369
370 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
371     IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
372
373 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
374     IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
375
376 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
377     IFM_SUBTYPE_TOKENRING_ALIASES;
378
379 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
380     IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
381
382 static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
383     IFM_SUBTYPE_FDDI_DESCRIPTIONS;
384
385 static struct ifmedia_description ifm_subtype_fddi_aliases[] =
386     IFM_SUBTYPE_FDDI_ALIASES;
387
388 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
389     IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
390
391 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
392     IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
393
394 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
395     IFM_SUBTYPE_IEEE80211_ALIASES;
396
397 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
398     IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
399
400 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
401     IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
402
403 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
404     IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
405
406 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
407     IFM_SUBTYPE_ATM_DESCRIPTIONS;
408
409 static struct ifmedia_description ifm_subtype_atm_aliases[] =
410     IFM_SUBTYPE_ATM_ALIASES;
411
412 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
413     IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
414
415 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
416     IFM_SUBTYPE_SHARED_DESCRIPTIONS;
417
418 static struct ifmedia_description ifm_subtype_shared_aliases[] =
419     IFM_SUBTYPE_SHARED_ALIASES;
420
421 static struct ifmedia_description ifm_shared_option_descriptions[] =
422     IFM_SHARED_OPTION_DESCRIPTIONS;
423
424 static struct ifmedia_description ifm_shared_option_aliases[] =
425     IFM_SHARED_OPTION_ALIASES;
426
427 struct ifmedia_type_to_subtype {
428         struct {
429                 struct ifmedia_description *desc;
430                 int alias;
431         } subtypes[5];
432         struct {
433                 struct ifmedia_description *desc;
434                 int alias;
435         } options[4];
436         struct {
437                 struct ifmedia_description *desc;
438                 int alias;
439         } modes[3];
440 };
441
442 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
443 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
444         {
445                 {
446                         { &ifm_subtype_shared_descriptions[0], 0 },
447                         { &ifm_subtype_shared_aliases[0], 1 },
448                         { &ifm_subtype_ethernet_descriptions[0], 0 },
449                         { &ifm_subtype_ethernet_aliases[0], 1 },
450                         { NULL, 0 },
451                 },
452                 {
453                         { &ifm_shared_option_descriptions[0], 0 },
454                         { &ifm_shared_option_aliases[0], 1 },
455                         { &ifm_subtype_ethernet_option_descriptions[0], 0 },
456                         { NULL, 0 },
457                 },
458                 {
459                         { NULL, 0 },
460                 },
461         },
462         {
463                 {
464                         { &ifm_subtype_shared_descriptions[0], 0 },
465                         { &ifm_subtype_shared_aliases[0], 1 },
466                         { &ifm_subtype_tokenring_descriptions[0], 0 },
467                         { &ifm_subtype_tokenring_aliases[0], 1 },
468                         { NULL, 0 },
469                 },
470                 {
471                         { &ifm_shared_option_descriptions[0], 0 },
472                         { &ifm_shared_option_aliases[0], 1 },
473                         { &ifm_subtype_tokenring_option_descriptions[0], 0 },
474                         { NULL, 0 },
475                 },
476                 {
477                         { NULL, 0 },
478                 },
479         },
480         {
481                 {
482                         { &ifm_subtype_shared_descriptions[0], 0 },
483                         { &ifm_subtype_shared_aliases[0], 1 },
484                         { &ifm_subtype_fddi_descriptions[0], 0 },
485                         { &ifm_subtype_fddi_aliases[0], 1 },
486                         { NULL, 0 },
487                 },
488                 {
489                         { &ifm_shared_option_descriptions[0], 0 },
490                         { &ifm_shared_option_aliases[0], 1 },
491                         { &ifm_subtype_fddi_option_descriptions[0], 0 },
492                         { NULL, 0 },
493                 },
494                 {
495                         { NULL, 0 },
496                 },
497         },
498         {
499                 {
500                         { &ifm_subtype_shared_descriptions[0], 0 },
501                         { &ifm_subtype_shared_aliases[0], 1 },
502                         { &ifm_subtype_ieee80211_descriptions[0], 0 },
503                         { &ifm_subtype_ieee80211_aliases[0], 1 },
504                         { NULL, 0 },
505                 },
506                 {
507                         { &ifm_shared_option_descriptions[0], 0 },
508                         { &ifm_shared_option_aliases[0], 1 },
509                         { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
510                         { NULL, 0 },
511                 },
512                 {
513                         { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
514                         { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
515                         { NULL, 0 },
516                 },
517         },
518         {
519                 {
520                         { &ifm_subtype_shared_descriptions[0], 0 },
521                         { &ifm_subtype_shared_aliases[0], 1 },
522                         { &ifm_subtype_atm_descriptions[0], 0 },
523                         { &ifm_subtype_atm_aliases[0], 1 },
524                         { NULL, 0 },
525                 },
526                 {
527                         { &ifm_shared_option_descriptions[0], 0 },
528                         { &ifm_shared_option_aliases[0], 1 },
529                         { &ifm_subtype_atm_option_descriptions[0], 0 },
530                         { NULL, 0 },
531                 },
532                 {
533                         { NULL, 0 },
534                 },
535         },
536 };
537
538 static int
539 get_media_subtype(int type, const char *val)
540 {
541         struct ifmedia_description *desc;
542         struct ifmedia_type_to_subtype *ttos;
543         int rval, i;
544
545         /* Find the top-level interface type. */
546         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
547             desc->ifmt_string != NULL; desc++, ttos++)
548                 if (type == desc->ifmt_word)
549                         break;
550         if (desc->ifmt_string == NULL)
551                 errx(1, "unknown media type 0x%x", type);
552
553         for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
554                 rval = lookup_media_word(ttos->subtypes[i].desc, val);
555                 if (rval != -1)
556                         return (rval);
557         }
558         errx(1, "unknown media subtype: %s", val);
559         /*NOTREACHED*/
560 }
561
562 static int
563 get_media_mode(int type, const char *val)
564 {
565         struct ifmedia_description *desc;
566         struct ifmedia_type_to_subtype *ttos;
567         int rval, i;
568
569         /* Find the top-level interface type. */
570         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
571             desc->ifmt_string != NULL; desc++, ttos++)
572                 if (type == desc->ifmt_word)
573                         break;
574         if (desc->ifmt_string == NULL)
575                 errx(1, "unknown media mode 0x%x", type);
576
577         for (i = 0; ttos->modes[i].desc != NULL; i++) {
578                 rval = lookup_media_word(ttos->modes[i].desc, val);
579                 if (rval != -1)
580                         return (rval);
581         }
582         return -1;
583 }
584
585 static int
586 get_media_options(int type, const char *val)
587 {
588         struct ifmedia_description *desc;
589         struct ifmedia_type_to_subtype *ttos;
590         char *optlist, *optptr;
591         int option = 0, i, rval = 0;
592
593         /* We muck with the string, so copy it. */
594         optlist = strdup(val);
595         if (optlist == NULL)
596                 err(1, "strdup");
597
598         /* Find the top-level interface type. */
599         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
600             desc->ifmt_string != NULL; desc++, ttos++)
601                 if (type == desc->ifmt_word)
602                         break;
603         if (desc->ifmt_string == NULL)
604                 errx(1, "unknown media type 0x%x", type);
605
606         /*
607          * Look up the options in the user-provided comma-separated
608          * list.
609          */
610         optptr = optlist;
611         for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
612                 for (i = 0; ttos->options[i].desc != NULL; i++) {
613                         option = lookup_media_word(ttos->options[i].desc, optptr);
614                         if (option != -1)
615                                 break;
616                 }
617                 if (option == 0)
618                         errx(1, "unknown option: %s", optptr);
619                 rval |= option;
620         }
621
622         free(optlist);
623         return (rval);
624 }
625
626 static int
627 lookup_media_word(struct ifmedia_description *desc, const char *val)
628 {
629
630         for (; desc->ifmt_string != NULL; desc++)
631                 if (strcasecmp(desc->ifmt_string, val) == 0)
632                         return (desc->ifmt_word);
633
634         return (-1);
635 }
636
637 static struct ifmedia_description *get_toptype_desc(int ifmw)
638 {
639         struct ifmedia_description *desc;
640
641         for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
642                 if (IFM_TYPE(ifmw) == desc->ifmt_word)
643                         break;
644
645         return desc;
646 }
647
648 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
649 {
650         struct ifmedia_description *desc;
651         struct ifmedia_type_to_subtype *ttos;
652
653         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
654             desc->ifmt_string != NULL; desc++, ttos++)
655                 if (IFM_TYPE(ifmw) == desc->ifmt_word)
656                         break;
657
658         return ttos;
659 }
660
661 static struct ifmedia_description *get_subtype_desc(int ifmw, 
662     struct ifmedia_type_to_subtype *ttos)
663 {
664         int i;
665         struct ifmedia_description *desc;
666
667         for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
668                 if (ttos->subtypes[i].alias)
669                         continue;
670                 for (desc = ttos->subtypes[i].desc;
671                     desc->ifmt_string != NULL; desc++) {
672                         if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
673                                 return desc;
674                 }
675         }
676
677         return NULL;
678 }
679
680 static struct ifmedia_description *get_mode_desc(int ifmw, 
681     struct ifmedia_type_to_subtype *ttos)
682 {
683         int i;
684         struct ifmedia_description *desc;
685
686         for (i = 0; ttos->modes[i].desc != NULL; i++) {
687                 if (ttos->modes[i].alias)
688                         continue;
689                 for (desc = ttos->modes[i].desc;
690                     desc->ifmt_string != NULL; desc++) {
691                         if (IFM_MODE(ifmw) == desc->ifmt_word)
692                                 return desc;
693                 }
694         }
695
696         return NULL;
697 }
698
699 static void
700 print_media_word(int ifmw, int print_toptype)
701 {
702         struct ifmedia_description *desc;
703         struct ifmedia_type_to_subtype *ttos;
704         int seen_option = 0, i;
705
706         /* Find the top-level interface type. */
707         desc = get_toptype_desc(ifmw);
708         ttos = get_toptype_ttos(ifmw);
709         if (desc->ifmt_string == NULL) {
710                 printf("<unknown type>");
711                 return;
712         } else if (print_toptype) {
713                 printf("%s", desc->ifmt_string);
714         }
715
716         /*
717          * Don't print the top-level type; it's not like we can
718          * change it, or anything.
719          */
720
721         /* Find subtype. */
722         desc = get_subtype_desc(ifmw, ttos);
723         if (desc == NULL) {
724                 printf("<unknown subtype>");
725                 return;
726         }
727
728         if (print_toptype)
729                 putchar(' ');
730
731         printf("%s", desc->ifmt_string);
732
733         if (print_toptype) {
734                 desc = get_mode_desc(ifmw, ttos);
735                 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
736                         printf(" mode %s", desc->ifmt_string);
737         }
738
739         /* Find options. */
740         for (i = 0; ttos->options[i].desc != NULL; i++) {
741                 if (ttos->options[i].alias)
742                         continue;
743                 for (desc = ttos->options[i].desc;
744                     desc->ifmt_string != NULL; desc++) {
745                         if (ifmw & desc->ifmt_word) {
746                                 if (seen_option == 0)
747                                         printf(" <");
748                                 printf("%s%s", seen_option++ ? "," : "",
749                                     desc->ifmt_string);
750                         }
751                 }
752         }
753         printf("%s", seen_option ? ">" : "");
754
755         if (print_toptype && IFM_INST(ifmw) != 0)
756                 printf(" instance %d", IFM_INST(ifmw));
757 }
758
759 static void
760 print_media_word_ifconfig(int ifmw)
761 {
762         struct ifmedia_description *desc;
763         struct ifmedia_type_to_subtype *ttos;
764         int seen_option = 0, i;
765
766         /* Find the top-level interface type. */
767         desc = get_toptype_desc(ifmw);
768         ttos = get_toptype_ttos(ifmw);
769         if (desc->ifmt_string == NULL) {
770                 printf("<unknown type>");
771                 return;
772         }
773
774         /*
775          * Don't print the top-level type; it's not like we can
776          * change it, or anything.
777          */
778
779         /* Find subtype. */
780         desc = get_subtype_desc(ifmw, ttos);
781         if (desc == NULL) {
782                 printf("<unknown subtype>");
783                 return;
784         }
785
786         printf("media %s", desc->ifmt_string);
787
788         desc = get_mode_desc(ifmw, ttos);
789         if (desc != NULL)
790                 printf(" mode %s", desc->ifmt_string);
791
792         /* Find options. */
793         for (i = 0; ttos->options[i].desc != NULL; i++) {
794                 if (ttos->options[i].alias)
795                         continue;
796                 for (desc = ttos->options[i].desc;
797                     desc->ifmt_string != NULL; desc++) {
798                         if (ifmw & desc->ifmt_word) {
799                                 if (seen_option == 0)
800                                         printf(" mediaopt ");
801                                 printf("%s%s", seen_option++ ? "," : "",
802                                     desc->ifmt_string);
803                         }
804                 }
805         }
806
807         if (IFM_INST(ifmw) != 0)
808                 printf(" instance %d", IFM_INST(ifmw));
809 }
810
811 /**********************************************************************
812  * ...until here.
813  **********************************************************************/
814
815 static struct cmd media_cmds[] = {
816         DEF_CMD_ARG("media",    setmedia),
817         DEF_CMD_ARG("mode",     setmediamode),
818         DEF_CMD_ARG("mediaopt", setmediaopt),
819         DEF_CMD_ARG("-mediaopt",unsetmediaopt),
820         DEF_CMD_ARG("inst",     setmediainst),
821         DEF_CMD_ARG("instance", setmediainst),
822 };
823 static struct afswtch af_media = {
824         .af_name        = "af_media",
825         .af_af          = AF_UNSPEC,
826         .af_other_status = media_status,
827 };
828
829 static __constructor void
830 ifmedia_ctor(void)
831 {
832 #define N(a)    (sizeof(a) / sizeof(a[0]))
833         size_t i;
834
835         for (i = 0; i < N(media_cmds);  i++)
836                 cmd_register(&media_cmds[i]);
837         af_register(&af_media);
838 #undef N
839 }