ifmedia.c revision 165359
1/*	$NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $	*/
2/* $FreeBSD: head/sbin/ifconfig/ifmedia.c 165359 2006-12-19 22:50:49Z jkim $ */
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
92static void	domediaopt(const char *, int, int);
93static int	get_media_subtype(int, const char *);
94static int	get_media_mode(int, const char *);
95static int	get_media_options(int, const char *);
96static int	lookup_media_word(struct ifmedia_description *, const char *);
97static void	print_media_word(int, int);
98static void	print_media_word_ifconfig(int);
99
100static struct ifmedia_description *get_toptype_desc(int);
101static struct ifmedia_type_to_subtype *get_toptype_ttos(int);
102static struct ifmedia_description *get_subtype_desc(int,
103    struct ifmedia_type_to_subtype *ttos);
104
105static void
106media_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
187static struct ifmediareq *
188getifmediastate(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
230static void
231setifmediacallback(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
246static void
247setmedia(const char *val, int d, int s, const struct afswtch *afp)
248{
249	struct ifmediareq *ifmr;
250	int subtype;
251
252
253	ifmr = getifmediastate(s);
254
255	/*
256	 * We are primarily concerned with the top-level type.
257	 * However, "current" may be only IFM_NONE, so we just look
258	 * for the top-level type in the first "supported type"
259	 * entry.
260	 *
261	 * (I'm assuming that all supported media types for a given
262	 * interface will be the same top-level type..)
263	 */
264	subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val);
265
266	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
267	ifr.ifr_media = (ifmr->ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
268	    IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
269
270	if ((ifr.ifr_media & IFM_TMASK) == 0) {
271		ifr.ifr_media &= ~IFM_GMASK;
272	}
273
274	ifmr->ifm_current = ifr.ifr_media;
275	callback_register(setifmediacallback, (void *)ifmr);
276}
277
278static void
279setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
280{
281
282	domediaopt(val, 0, s);
283}
284
285static void
286unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
287{
288
289	domediaopt(val, 1, s);
290}
291
292static void
293domediaopt(const char *val, int clear, int s)
294{
295	struct ifmediareq *ifmr;
296	int options;
297
298	ifmr = getifmediastate(s);
299
300	options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
301
302	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
303	ifr.ifr_media = ifmr->ifm_current;
304	if (clear)
305		ifr.ifr_media &= ~options;
306	else {
307		if (options & IFM_HDX) {
308			ifr.ifr_media &= ~IFM_FDX;
309			options &= ~IFM_HDX;
310		}
311		ifr.ifr_media |= options;
312	}
313	ifmr->ifm_current = ifr.ifr_media;
314	callback_register(setifmediacallback, (void *)ifmr);
315}
316
317
318static void
319setmediamode(const char *val, int d, int s, const struct afswtch *afp)
320{
321	struct ifmediareq *ifmr;
322	int mode;
323
324	ifmr = getifmediastate(s);
325
326	mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
327
328	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
329	ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode;
330
331	ifmr->ifm_current = ifr.ifr_media;
332	callback_register(setifmediacallback, (void *)ifmr);
333}
334
335/**********************************************************************
336 * A good chunk of this is duplicated from sys/net/ifmedia.c
337 **********************************************************************/
338
339static struct ifmedia_description ifm_type_descriptions[] =
340    IFM_TYPE_DESCRIPTIONS;
341
342static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
343    IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
344
345static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
346    IFM_SUBTYPE_ETHERNET_ALIASES;
347
348static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
349    IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
350
351static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
352    IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
353
354static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
355    IFM_SUBTYPE_TOKENRING_ALIASES;
356
357static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
358    IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
359
360static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
361    IFM_SUBTYPE_FDDI_DESCRIPTIONS;
362
363static struct ifmedia_description ifm_subtype_fddi_aliases[] =
364    IFM_SUBTYPE_FDDI_ALIASES;
365
366static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
367    IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
368
369static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
370    IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
371
372static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
373    IFM_SUBTYPE_IEEE80211_ALIASES;
374
375static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
376    IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
377
378struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
379    IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
380
381struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
382    IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
383
384static struct ifmedia_description ifm_subtype_atm_descriptions[] =
385    IFM_SUBTYPE_ATM_DESCRIPTIONS;
386
387static struct ifmedia_description ifm_subtype_atm_aliases[] =
388    IFM_SUBTYPE_ATM_ALIASES;
389
390static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
391    IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
392
393static struct ifmedia_description ifm_subtype_shared_descriptions[] =
394    IFM_SUBTYPE_SHARED_DESCRIPTIONS;
395
396static struct ifmedia_description ifm_subtype_shared_aliases[] =
397    IFM_SUBTYPE_SHARED_ALIASES;
398
399static struct ifmedia_description ifm_shared_option_descriptions[] =
400    IFM_SHARED_OPTION_DESCRIPTIONS;
401
402struct ifmedia_type_to_subtype {
403	struct {
404		struct ifmedia_description *desc;
405		int alias;
406	} subtypes[5];
407	struct {
408		struct ifmedia_description *desc;
409		int alias;
410	} options[3];
411	struct {
412		struct ifmedia_description *desc;
413		int alias;
414	} modes[3];
415};
416
417/* must be in the same order as IFM_TYPE_DESCRIPTIONS */
418static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
419	{
420		{
421			{ &ifm_subtype_shared_descriptions[0], 0 },
422			{ &ifm_subtype_shared_aliases[0], 1 },
423			{ &ifm_subtype_ethernet_descriptions[0], 0 },
424			{ &ifm_subtype_ethernet_aliases[0], 1 },
425			{ NULL, 0 },
426		},
427		{
428			{ &ifm_shared_option_descriptions[0], 0 },
429			{ &ifm_subtype_ethernet_option_descriptions[0], 0 },
430			{ NULL, 0 },
431		},
432		{
433			{ NULL, 0 },
434		},
435	},
436	{
437		{
438			{ &ifm_subtype_shared_descriptions[0], 0 },
439			{ &ifm_subtype_shared_aliases[0], 1 },
440			{ &ifm_subtype_tokenring_descriptions[0], 0 },
441			{ &ifm_subtype_tokenring_aliases[0], 1 },
442			{ NULL, 0 },
443		},
444		{
445			{ &ifm_shared_option_descriptions[0], 0 },
446			{ &ifm_subtype_tokenring_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_fddi_descriptions[0], 0 },
458			{ &ifm_subtype_fddi_aliases[0], 1 },
459			{ NULL, 0 },
460		},
461		{
462			{ &ifm_shared_option_descriptions[0], 0 },
463			{ &ifm_subtype_fddi_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_ieee80211_descriptions[0], 0 },
475			{ &ifm_subtype_ieee80211_aliases[0], 1 },
476			{ NULL, 0 },
477		},
478		{
479			{ &ifm_shared_option_descriptions[0], 0 },
480			{ &ifm_subtype_ieee80211_option_descriptions[0], 0 },
481			{ NULL, 0 },
482		},
483		{
484			{ &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
485			{ &ifm_subtype_ieee80211_mode_aliases[0], 0 },
486			{ NULL, 0 },
487		},
488	},
489	{
490		{
491			{ &ifm_subtype_shared_descriptions[0], 0 },
492			{ &ifm_subtype_shared_aliases[0], 1 },
493			{ &ifm_subtype_atm_descriptions[0], 0 },
494			{ &ifm_subtype_atm_aliases[0], 1 },
495			{ NULL, 0 },
496		},
497		{
498			{ &ifm_shared_option_descriptions[0], 0 },
499			{ &ifm_subtype_atm_option_descriptions[0], 0 },
500			{ NULL, 0 },
501		},
502		{
503			{ NULL, 0 },
504		},
505	},
506};
507
508static int
509get_media_subtype(int type, const char *val)
510{
511	struct ifmedia_description *desc;
512	struct ifmedia_type_to_subtype *ttos;
513	int rval, i;
514
515	/* Find the top-level interface type. */
516	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
517	    desc->ifmt_string != NULL; desc++, ttos++)
518		if (type == desc->ifmt_word)
519			break;
520	if (desc->ifmt_string == NULL)
521		errx(1, "unknown media type 0x%x", type);
522
523	for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
524		rval = lookup_media_word(ttos->subtypes[i].desc, val);
525		if (rval != -1)
526			return (rval);
527	}
528	errx(1, "unknown media subtype: %s", val);
529	/*NOTREACHED*/
530}
531
532static int
533get_media_mode(int type, const char *val)
534{
535	struct ifmedia_description *desc;
536	struct ifmedia_type_to_subtype *ttos;
537	int rval, i;
538
539	/* Find the top-level interface type. */
540	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
541	    desc->ifmt_string != NULL; desc++, ttos++)
542		if (type == desc->ifmt_word)
543			break;
544	if (desc->ifmt_string == NULL)
545		errx(1, "unknown media mode 0x%x", type);
546
547	for (i = 0; ttos->modes[i].desc != NULL; i++) {
548		rval = lookup_media_word(ttos->modes[i].desc, val);
549		if (rval != -1)
550			return (rval);
551	}
552	return -1;
553}
554
555static int
556get_media_options(int type, const char *val)
557{
558	struct ifmedia_description *desc;
559	struct ifmedia_type_to_subtype *ttos;
560	char *optlist, *optptr;
561	int option = 0, i, rval = 0;
562
563	/* We muck with the string, so copy it. */
564	optlist = strdup(val);
565	if (optlist == NULL)
566		err(1, "strdup");
567
568	/* Find the top-level interface type. */
569	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
570	    desc->ifmt_string != NULL; desc++, ttos++)
571		if (type == desc->ifmt_word)
572			break;
573	if (desc->ifmt_string == NULL)
574		errx(1, "unknown media type 0x%x", type);
575
576	/*
577	 * Look up the options in the user-provided comma-separated
578	 * list.
579	 */
580	optptr = optlist;
581	for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
582		for (i = 0; ttos->options[i].desc != NULL; i++) {
583			option = lookup_media_word(ttos->options[i].desc, optptr);
584			if (option != -1)
585				break;
586		}
587		if (option == 0)
588			errx(1, "unknown option: %s", optptr);
589		rval |= option;
590	}
591
592	free(optlist);
593	return (rval);
594}
595
596static int
597lookup_media_word(struct ifmedia_description *desc, const char *val)
598{
599
600	for (; desc->ifmt_string != NULL; desc++)
601		if (strcasecmp(desc->ifmt_string, val) == 0)
602			return (desc->ifmt_word);
603
604	return (-1);
605}
606
607static struct ifmedia_description *get_toptype_desc(int ifmw)
608{
609	struct ifmedia_description *desc;
610
611	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
612		if (IFM_TYPE(ifmw) == desc->ifmt_word)
613			break;
614
615	return desc;
616}
617
618static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
619{
620	struct ifmedia_description *desc;
621	struct ifmedia_type_to_subtype *ttos;
622
623	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
624	    desc->ifmt_string != NULL; desc++, ttos++)
625		if (IFM_TYPE(ifmw) == desc->ifmt_word)
626			break;
627
628	return ttos;
629}
630
631static struct ifmedia_description *get_subtype_desc(int ifmw,
632    struct ifmedia_type_to_subtype *ttos)
633{
634	int i;
635	struct ifmedia_description *desc;
636
637	for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
638		if (ttos->subtypes[i].alias)
639			continue;
640		for (desc = ttos->subtypes[i].desc;
641		    desc->ifmt_string != NULL; desc++) {
642			if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
643				return desc;
644		}
645	}
646
647	return NULL;
648}
649
650static struct ifmedia_description *get_mode_desc(int ifmw,
651    struct ifmedia_type_to_subtype *ttos)
652{
653	int i;
654	struct ifmedia_description *desc;
655
656	for (i = 0; ttos->modes[i].desc != NULL; i++) {
657		if (ttos->modes[i].alias)
658			continue;
659		for (desc = ttos->modes[i].desc;
660		    desc->ifmt_string != NULL; desc++) {
661			if (IFM_MODE(ifmw) == desc->ifmt_word)
662				return desc;
663		}
664	}
665
666	return NULL;
667}
668
669static void
670print_media_word(int ifmw, int print_toptype)
671{
672	struct ifmedia_description *desc;
673	struct ifmedia_type_to_subtype *ttos;
674	int seen_option = 0, i;
675
676	/* Find the top-level interface type. */
677	desc = get_toptype_desc(ifmw);
678	ttos = get_toptype_ttos(ifmw);
679	if (desc->ifmt_string == NULL) {
680		printf("<unknown type>");
681		return;
682	} else if (print_toptype) {
683		printf("%s", desc->ifmt_string);
684	}
685
686	/*
687	 * Don't print the top-level type; it's not like we can
688	 * change it, or anything.
689	 */
690
691	/* Find subtype. */
692	desc = get_subtype_desc(ifmw, ttos);
693	if (desc == NULL) {
694		printf("<unknown subtype>");
695		return;
696	}
697
698	if (print_toptype)
699		putchar(' ');
700
701	printf("%s", desc->ifmt_string);
702
703	if (print_toptype) {
704		desc = get_mode_desc(ifmw, ttos);
705		if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
706			printf(" mode %s", desc->ifmt_string);
707	}
708
709	/* Find options. */
710	for (i = 0; ttos->options[i].desc != NULL; i++) {
711		if (ttos->options[i].alias)
712			continue;
713		for (desc = ttos->options[i].desc;
714		    desc->ifmt_string != NULL; desc++) {
715			if (ifmw & desc->ifmt_word) {
716				if (seen_option == 0)
717					printf(" <");
718				printf("%s%s", seen_option++ ? "," : "",
719				    desc->ifmt_string);
720			}
721		}
722	}
723	printf("%s", seen_option ? ">" : "");
724}
725
726static void
727print_media_word_ifconfig(int ifmw)
728{
729	struct ifmedia_description *desc;
730	struct ifmedia_type_to_subtype *ttos;
731	int i;
732
733	/* Find the top-level interface type. */
734	desc = get_toptype_desc(ifmw);
735	ttos = get_toptype_ttos(ifmw);
736	if (desc->ifmt_string == NULL) {
737		printf("<unknown type>");
738		return;
739	}
740
741	/*
742	 * Don't print the top-level type; it's not like we can
743	 * change it, or anything.
744	 */
745
746	/* Find subtype. */
747	desc = get_subtype_desc(ifmw, ttos);
748	if (desc == NULL) {
749		printf("<unknown subtype>");
750		return;
751	}
752
753	printf("media %s", desc->ifmt_string);
754
755	desc = get_mode_desc(ifmw, ttos);
756	if (desc != NULL)
757		printf(" mode %s", desc->ifmt_string);
758
759	/* Find options. */
760	for (i = 0; ttos->options[i].desc != NULL; i++) {
761		if (ttos->options[i].alias)
762			continue;
763		for (desc = ttos->options[i].desc;
764		    desc->ifmt_string != NULL; desc++) {
765			if (ifmw & desc->ifmt_word) {
766				printf(" mediaopt %s", desc->ifmt_string);
767			}
768		}
769	}
770}
771
772/**********************************************************************
773 * ...until here.
774 **********************************************************************/
775
776static struct cmd media_cmds[] = {
777	DEF_CMD_ARG("media",	setmedia),
778	DEF_CMD_ARG("mode",	setmediamode),
779	DEF_CMD_ARG("mediaopt",	setmediaopt),
780	DEF_CMD_ARG("-mediaopt",unsetmediaopt),
781};
782static struct afswtch af_media = {
783	.af_name	= "af_media",
784	.af_af		= AF_UNSPEC,
785	.af_other_status = media_status,
786};
787
788static __constructor void
789ifmedia_ctor(void)
790{
791#define	N(a)	(sizeof(a) / sizeof(a[0]))
792	int i;
793
794	for (i = 0; i < N(media_cmds);  i++)
795		cmd_register(&media_cmds[i]);
796	af_register(&af_media);
797#undef N
798}
799