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