xref: /dragonfly/sbin/ifconfig/ifmedia.c (revision c9c5aa9e)
1 /*	$NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $	*/
2 /* $FreeBSD: src/sbin/ifconfig/ifmedia.c,v 1.19.2.1 2006/03/01 22:24:23 glebius Exp $ */
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. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  */
64 
65 #include <sys/param.h>
66 #include <sys/ioctl.h>
67 #include <sys/socket.h>
68 #include <sys/sysctl.h>
69 #include <sys/time.h>
70 
71 #include <net/if.h>
72 #include <net/if_dl.h>
73 #include <net/if_types.h>
74 #include <net/if_media.h>
75 #include <net/route.h>
76 
77 #include <ctype.h>
78 #include <err.h>
79 #include <errno.h>
80 #include <fcntl.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <unistd.h>
85 
86 #include "ifconfig.h"
87 
88 static void	domediaopt(const char *, int, int);
89 static int	get_media_subtype(int, const char *);
90 static int	get_media_mode(int, const char *);
91 static int	get_media_options(int, const char *);
92 static int	lookup_media_word(struct ifmedia_description *, const char *);
93 static void	print_media_word(int, int);
94 static void	print_media_word_ifconfig(int);
95 
96 static struct ifmedia_description *get_toptype_desc(int);
97 static struct ifmedia_type_to_subtype *get_toptype_ttos(int);
98 static struct ifmedia_description *get_subtype_desc(int,
99     struct ifmedia_type_to_subtype *ttos);
100 
101 static void
102 media_status(int s)
103 {
104 	struct ifmediareq ifmr;
105 	int *media_list, i;
106 	int xmedia = 1;
107 
108 	memset(&ifmr, 0, sizeof(ifmr));
109 	strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
110 
111 	/*
112 	 * Check if interface supports extended media types.
113 	 */
114 	if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)&ifmr) < 0)
115 		xmedia = 0;
116 	if (xmedia == 0 && ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
117 		/*
118 		 * Interface doesn't support SIOC{G,S}IFMEDIA.
119 		 */
120 		return;
121 	}
122 
123 	if (ifmr.ifm_count == 0) {
124 		warnx("%s: no media types?", name);
125 		return;
126 	}
127 
128 	media_list = malloc(ifmr.ifm_count * sizeof(int));
129 	if (media_list == NULL)
130 		err(1, "malloc");
131 	ifmr.ifm_ulist = media_list;
132 
133 	if (xmedia) {
134 		if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)&ifmr) < 0)
135 			err(1, "SIOCGIFXMEDIA");
136 	} else {
137 		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
138 			err(1, "SIOCGIFMEDIA");
139 	}
140 
141 	printf("\tmedia: ");
142 	print_media_word(ifmr.ifm_current, 1);
143 	if (ifmr.ifm_active != ifmr.ifm_current) {
144 		putchar(' ');
145 		putchar('(');
146 		print_media_word(ifmr.ifm_active, 0);
147 		putchar(')');
148 	}
149 
150 	putchar('\n');
151 
152 	if (ifmr.ifm_status & IFM_AVALID) {
153 		printf("\tstatus: ");
154 		switch (IFM_TYPE(ifmr.ifm_active)) {
155 		case IFM_ETHER:
156 			if (ifmr.ifm_status & IFM_ACTIVE)
157 				printf("active");
158 			else
159 				printf("no carrier");
160 			break;
161 
162 		case IFM_ATM:
163 			if (ifmr.ifm_status & IFM_ACTIVE)
164 				printf("active");
165 			else
166 				printf("no carrier");
167 			break;
168 
169 		case IFM_IEEE80211:
170 			/* XXX: Different value for adhoc? */
171 			if (ifmr.ifm_status & IFM_ACTIVE)
172 				printf("associated");
173 			else
174 				printf("no carrier");
175 			break;
176 		case IFM_CARP:
177 			if (ifmr.ifm_status & IFM_ACTIVE)
178 				printf("master");
179 			else
180 				printf("backup");
181 			break;
182 		}
183 		putchar('\n');
184 	}
185 
186 	if (ifmr.ifm_count > 0 && supmedia) {
187 		printf("\tsupported media:\n");
188 		for (i = 0; i < ifmr.ifm_count; i++) {
189 			printf("\t\t");
190 			print_media_word_ifconfig(media_list[i]);
191 			putchar('\n');
192 		}
193 	}
194 
195 	free(media_list);
196 }
197 
198 struct ifmediareq *
199 ifmedia_getstate(int s)
200 {
201 	static struct ifmediareq *ifmr = NULL;
202 	int *mwords;
203 	int xmedia = 1;
204 
205 	if (ifmr == NULL) {
206 		ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq));
207 		if (ifmr == NULL)
208 			err(1, "malloc");
209 
210 		memset(ifmr, 0, sizeof(struct ifmediareq));
211 		strlcpy(ifmr->ifm_name, name, sizeof(ifmr->ifm_name));
212 
213 		ifmr->ifm_count = 0;
214 		ifmr->ifm_ulist = NULL;
215 
216 		/*
217 		 * We must go through the motions of reading all
218 		 * supported media because we need to know both
219 		 * the current media type and the top-level type.
220 		 */
221 
222 		if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)ifmr) < 0)
223 			xmedia = 0;
224 		if (xmedia == 0 && ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0)
225 			err(1, "SIOCGIFMEDIA");
226 
227 		if (ifmr->ifm_count == 0)
228 			errx(1, "%s: no media types?", name);
229 
230 		mwords = (int *)malloc(ifmr->ifm_count * sizeof(int));
231 		if (mwords == NULL)
232 			err(1, "malloc");
233 
234 		ifmr->ifm_ulist = mwords;
235 		if (xmedia) {
236 			if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)ifmr) < 0)
237 				err(1, "SIOCGIFXMEDIA");
238 		} else {
239 			if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0)
240 				err(1, "SIOCGIFMEDIA");
241 		}
242 	}
243 
244 	return ifmr;
245 }
246 
247 static void
248 setifmediacallback(int s, void *arg)
249 {
250 	struct ifmediareq *ifmr = (struct ifmediareq *)arg;
251 	static int did_it = 0;
252 
253 	if (!did_it) {
254 		ifr.ifr_media = ifmr->ifm_current;
255 		if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
256 			err(1, "SIOCSIFMEDIA (media)");
257 		free(ifmr->ifm_ulist);
258 		free(ifmr);
259 		did_it = 1;
260 	}
261 }
262 
263 static void
264 setmedia(const char *val, int d, int s, const struct afswtch *afp)
265 {
266 	struct ifmediareq *ifmr;
267 	int subtype;
268 
269 	ifmr = ifmedia_getstate(s);
270 
271 	/*
272 	 * We are primarily concerned with the top-level type.
273 	 * However, "current" may be only IFM_NONE, so we just look
274 	 * for the top-level type in the first "supported type"
275 	 * entry.
276 	 *
277 	 * (I'm assuming that all supported media types for a given
278 	 * interface will be the same top-level type..)
279 	 */
280 	subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val);
281 
282 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
283 	ifr.ifr_media = (ifmr->ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
284 	    IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
285 
286 	if ((ifr.ifr_media & IFM_TMASK) == 0) {
287 		ifr.ifr_media &= ~IFM_GMASK;
288 	}
289 
290 	ifmr->ifm_current = ifr.ifr_media;
291 	callback_register(setifmediacallback, (void *)ifmr);
292 }
293 
294 static void
295 setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
296 {
297 
298 	domediaopt(val, 0, s);
299 }
300 
301 static void
302 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
303 {
304 
305 	domediaopt(val, 1, s);
306 }
307 
308 static void
309 domediaopt(const char *val, int clear, int s)
310 {
311 	struct ifmediareq *ifmr;
312 	int options;
313 
314 	ifmr = ifmedia_getstate(s);
315 
316 	options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
317 
318 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
319 	ifr.ifr_media = ifmr->ifm_current;
320 	if (clear)
321 		ifr.ifr_media &= ~options;
322 	else
323 		ifr.ifr_media |= options;
324 
325 	ifmr->ifm_current = ifr.ifr_media;
326 	callback_register(setifmediacallback, (void *)ifmr);
327 }
328 
329 
330 static void
331 setmediamode(const char *val, int d, int s, const struct afswtch *afp)
332 {
333 	struct ifmediareq *ifmr;
334 	int mode;
335 
336 	ifmr = ifmedia_getstate(s);
337 
338 	mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
339 
340 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
341 	ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode;
342 
343 	ifmr->ifm_current = ifr.ifr_media;
344 	callback_register(setifmediacallback, (void *)ifmr);
345 }
346 
347 /**********************************************************************
348  * A good chunk of this is duplicated from sys/net/ifmedia.c
349  **********************************************************************/
350 
351 static struct ifmedia_description ifm_type_descriptions[] =
352     IFM_TYPE_DESCRIPTIONS;
353 
354 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
355     IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
356 
357 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
358     IFM_SUBTYPE_ETHERNET_ALIASES;
359 
360 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
361     IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
362 
363 static struct ifmedia_description ifm_subtype_ethernet_option_alias[] =
364     IFM_SUBTYPE_ETHERNET_OPTION_ALIAS;
365 
366 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
367     IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
368 
369 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
370     IFM_SUBTYPE_IEEE80211_ALIASES;
371 
372 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
373     IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
374 
375 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
376     IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
377 
378 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
379     IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
380 
381 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
382     IFM_SUBTYPE_ATM_DESCRIPTIONS;
383 
384 static struct ifmedia_description ifm_subtype_atm_aliases[] =
385     IFM_SUBTYPE_ATM_ALIASES;
386 
387 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
388     IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
389 
390 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
391     IFM_SUBTYPE_SHARED_DESCRIPTIONS;
392 
393 static struct ifmedia_description ifm_subtype_shared_aliases[] =
394     IFM_SUBTYPE_SHARED_ALIASES;
395 
396 static struct ifmedia_description ifm_shared_option_descriptions[] =
397     IFM_SHARED_OPTION_DESCRIPTIONS;
398 
399 struct ifmedia_type_to_subtype {
400 	struct {
401 		struct ifmedia_description *desc;
402 		int alias;
403 	} subtypes[5];
404 	struct {
405 		struct ifmedia_description *desc;
406 		int alias;
407 	} options[4];
408 	struct {
409 		struct ifmedia_description *desc;
410 		int alias;
411 	} modes[3];
412 };
413 
414 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
415 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
416 	{
417 		{
418 			{ &ifm_subtype_shared_descriptions[0], 0 },
419 			{ &ifm_subtype_shared_aliases[0], 1 },
420 			{ &ifm_subtype_ethernet_descriptions[0], 0 },
421 			{ &ifm_subtype_ethernet_aliases[0], 1 },
422 			{ NULL, 0 },
423 		},
424 		{
425 			{ &ifm_shared_option_descriptions[0], 0 },
426 			{ &ifm_subtype_ethernet_option_descriptions[0], 0 },
427 			{ &ifm_subtype_ethernet_option_alias[0], 1 },
428 			{ NULL, 0 },
429 		},
430 		{
431 			{ NULL, 0 },
432 		},
433 	},
434 	{
435 		{
436 			{ &ifm_subtype_shared_descriptions[0], 0 },
437 			{ &ifm_subtype_shared_aliases[0], 1 },
438 			{ &ifm_subtype_ieee80211_descriptions[0], 0 },
439 			{ &ifm_subtype_ieee80211_aliases[0], 1 },
440 			{ NULL, 0 },
441 		},
442 		{
443 			{ &ifm_shared_option_descriptions[0], 0 },
444 			{ &ifm_subtype_ieee80211_option_descriptions[0], 0 },
445 			{ NULL, 0 },
446 		},
447 		{
448 			{ &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
449 			{ &ifm_subtype_ieee80211_mode_aliases[0], 0 },
450 			{ NULL, 0 },
451 		},
452 	},
453 	{
454 		{
455 			{ &ifm_subtype_shared_descriptions[0], 0 },
456 			{ &ifm_subtype_shared_aliases[0], 1 },
457 			{ &ifm_subtype_atm_descriptions[0], 0 },
458 			{ &ifm_subtype_atm_aliases[0], 1 },
459 			{ NULL, 0 },
460 		},
461 		{
462 			{ &ifm_shared_option_descriptions[0], 0 },
463 			{ &ifm_subtype_atm_option_descriptions[0], 0 },
464 			{ NULL, 0 },
465 		},
466 		{
467 			{ NULL, 0 },
468 		},
469 	},
470 };
471 
472 static int
473 get_media_subtype(int type, const char *val)
474 {
475 	struct ifmedia_description *desc;
476 	struct ifmedia_type_to_subtype *ttos;
477 	int rval, i;
478 
479 	/* Find the top-level interface type. */
480 	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
481 	    desc->ifmt_string != NULL; desc++, ttos++)
482 		if (type == desc->ifmt_word)
483 			break;
484 	if (desc->ifmt_string == NULL)
485 		errx(1, "unknown media type 0x%x", type);
486 
487 	for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
488 		rval = lookup_media_word(ttos->subtypes[i].desc, val);
489 		if (rval != -1)
490 			return (rval);
491 	}
492 	errx(1, "unknown media subtype: %s", val);
493 	/*NOTREACHED*/
494 }
495 
496 static int
497 get_media_mode(int type, const char *val)
498 {
499 	struct ifmedia_description *desc;
500 	struct ifmedia_type_to_subtype *ttos;
501 	int rval, i;
502 
503 	/* Find the top-level interface type. */
504 	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
505 	    desc->ifmt_string != NULL; desc++, ttos++)
506 		if (type == desc->ifmt_word)
507 			break;
508 	if (desc->ifmt_string == NULL)
509 		errx(1, "unknown media mode 0x%x", type);
510 
511 	for (i = 0; ttos->modes[i].desc != NULL; i++) {
512 		rval = lookup_media_word(ttos->modes[i].desc, val);
513 		if (rval != -1)
514 			return (rval);
515 	}
516 	return -1;
517 }
518 
519 static int
520 get_media_options(int type, const char *val)
521 {
522 	struct ifmedia_description *desc;
523 	struct ifmedia_type_to_subtype *ttos;
524 	char *optlist, *optptr;
525 	int option = 0, i, rval = 0;
526 
527 	/* We muck with the string, so copy it. */
528 	optlist = strdup(val);
529 	if (optlist == NULL)
530 		err(1, "strdup");
531 
532 	/* Find the top-level interface type. */
533 	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
534 	    desc->ifmt_string != NULL; desc++, ttos++)
535 		if (type == desc->ifmt_word)
536 			break;
537 	if (desc->ifmt_string == NULL)
538 		errx(1, "unknown media type 0x%x", type);
539 
540 	/*
541 	 * Look up the options in the user-provided comma-separated
542 	 * list.
543 	 */
544 	optptr = optlist;
545 	for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
546 		for (i = 0; ttos->options[i].desc != NULL; i++) {
547 			option = lookup_media_word(ttos->options[i].desc, optptr);
548 			if (option != -1)
549 				break;
550 		}
551 		if (option == 0)
552 			errx(1, "unknown option: %s", optptr);
553 		rval |= option;
554 	}
555 
556 	free(optlist);
557 	return (rval);
558 }
559 
560 static int
561 lookup_media_word(struct ifmedia_description *desc, const char *val)
562 {
563 
564 	for (; desc->ifmt_string != NULL; desc++)
565 		if (strcasecmp(desc->ifmt_string, val) == 0)
566 			return (desc->ifmt_word);
567 
568 	return (-1);
569 }
570 
571 static struct ifmedia_description *get_toptype_desc(int ifmw)
572 {
573 	struct ifmedia_description *desc;
574 
575 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
576 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
577 			break;
578 
579 	return desc;
580 }
581 
582 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
583 {
584 	struct ifmedia_description *desc;
585 	struct ifmedia_type_to_subtype *ttos;
586 
587 	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
588 	    desc->ifmt_string != NULL; desc++, ttos++)
589 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
590 			break;
591 
592 	return ttos;
593 }
594 
595 static struct ifmedia_description *get_subtype_desc(int ifmw,
596     struct ifmedia_type_to_subtype *ttos)
597 {
598 	int i;
599 	struct ifmedia_description *desc;
600 
601 	for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
602 		if (ttos->subtypes[i].alias)
603 			continue;
604 		for (desc = ttos->subtypes[i].desc;
605 		    desc->ifmt_string != NULL; desc++) {
606 			if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
607 				return desc;
608 		}
609 	}
610 
611 	return NULL;
612 }
613 
614 static struct ifmedia_description *get_mode_desc(int ifmw,
615     struct ifmedia_type_to_subtype *ttos)
616 {
617 	int i;
618 	struct ifmedia_description *desc;
619 
620 	for (i = 0; ttos->modes[i].desc != NULL; i++) {
621 		if (ttos->modes[i].alias)
622 			continue;
623 		for (desc = ttos->modes[i].desc;
624 		    desc->ifmt_string != NULL; desc++) {
625 			if (IFM_MODE(ifmw) == desc->ifmt_word)
626 				return desc;
627 		}
628 	}
629 
630 	return NULL;
631 }
632 
633 static void
634 print_media_word(int ifmw, int print_toptype)
635 {
636 	struct ifmedia_description *desc;
637 	struct ifmedia_type_to_subtype *ttos;
638 	int seen_option = 0, i;
639 
640 	/* Find the top-level interface type. */
641 	desc = get_toptype_desc(ifmw);
642 	ttos = get_toptype_ttos(ifmw);
643 	if (desc->ifmt_string == NULL) {
644 		printf("<unknown type>");
645 		return;
646 	} else if (print_toptype) {
647 		printf("%s", desc->ifmt_string);
648 	}
649 
650 	/*
651 	 * Don't print the top-level type; it's not like we can
652 	 * change it, or anything.
653 	 */
654 
655 	/* Find subtype. */
656 	desc = get_subtype_desc(ifmw, ttos);
657 	if (desc != NULL)
658 		goto got_subtype;
659 
660 	/* Falling to here means unknown subtype. */
661 	printf("<unknown subtype>");
662 	return;
663 
664  got_subtype:
665 	if (print_toptype)
666 		putchar(' ');
667 
668 	printf("%s", desc->ifmt_string);
669 
670 	if (print_toptype) {
671 		desc = get_mode_desc(ifmw, ttos);
672 		if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
673 			printf(" mode %s", desc->ifmt_string);
674 	}
675 
676 	/* Find options. */
677 	for (i = 0; ttos->options[i].desc != NULL; i++) {
678 		if (ttos->options[i].alias)
679 			continue;
680 		for (desc = ttos->options[i].desc;
681 		    desc->ifmt_string != NULL; desc++) {
682 			if (ifmw & desc->ifmt_word) {
683 				if (seen_option == 0)
684 					printf(" <");
685 				printf("%s%s", seen_option++ ? "," : "",
686 				    desc->ifmt_string);
687 			}
688 		}
689 	}
690 	printf("%s", seen_option ? ">" : "");
691 }
692 
693 static void
694 print_media_word_ifconfig(int ifmw)
695 {
696 	struct ifmedia_description *desc;
697 	struct ifmedia_type_to_subtype *ttos;
698 	int i;
699 
700 	/* Find the top-level interface type. */
701 	desc = get_toptype_desc(ifmw);
702 	ttos = get_toptype_ttos(ifmw);
703 	if (desc->ifmt_string == NULL) {
704 		printf("<unknown type>");
705 		return;
706 	}
707 
708 	/*
709 	 * Don't print the top-level type; it's not like we can
710 	 * change it, or anything.
711 	 */
712 
713 	/* Find subtype. */
714 	desc = get_subtype_desc(ifmw, ttos);
715 	if (desc != NULL)
716 		goto got_subtype;
717 
718 	/* Falling to here means unknown subtype. */
719 	printf("<unknown subtype>");
720 	return;
721 
722  got_subtype:
723 	printf("media %s", desc->ifmt_string);
724 
725 	desc = get_mode_desc(ifmw, ttos);
726 	if (desc != NULL)
727 		printf(" mode %s", desc->ifmt_string);
728 
729 	/* Find options. */
730 	for (i = 0; ttos->options[i].desc != NULL; i++) {
731 		if (ttos->options[i].alias)
732 			continue;
733 		for (desc = ttos->options[i].desc;
734 		    desc->ifmt_string != NULL; desc++) {
735 			if (ifmw & desc->ifmt_word) {
736 				printf(" mediaopt %s", desc->ifmt_string);
737 			}
738 		}
739 	}
740 }
741 
742 /**********************************************************************
743  * ...until here.
744  **********************************************************************/
745 
746 static struct cmd media_cmds[] = {
747 	DEF_CMD_ARG("media",	setmedia),
748 	DEF_CMD_ARG("mode",	setmediamode),
749 	DEF_CMD_ARG("mediaopt",	setmediaopt),
750 	DEF_CMD_ARG("-mediaopt",unsetmediaopt),
751 };
752 static struct afswtch af_media = {
753 	.af_name	= "af_media",
754 	.af_af		= AF_UNSPEC,
755 	.af_other_status = media_status,
756 };
757 
758 static __constructor(101) void
759 ifmedia_ctor(void)
760 {
761 	size_t i;
762 
763 	for (i = 0; i < nitems(media_cmds);  i++)
764 		cmd_register(&media_cmds[i]);
765 	af_register(&af_media);
766 }
767