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