ifmedia.c revision 77217
1/*	$NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $	*/
2/* $FreeBSD: head/sbin/ifconfig/ifmedia.c 77217 2001-05-26 09:27:08Z phk $ */
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 __P((const char *, int, int));
93static int	get_media_subtype __P((int, const char *));
94static int	get_media_options __P((int, const char *));
95static int	lookup_media_word __P((struct ifmedia_description *, const char *));
96static void	print_media_word __P((int));
97
98void
99media_status(s, info)
100	int s;
101	struct rt_addrinfo *info __unused;
102{
103	struct ifmediareq ifmr;
104	int *media_list, i;
105
106	(void) memset(&ifmr, 0, sizeof(ifmr));
107	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
108
109	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
110		/*
111		 * Interface doesn't support SIOC{G,S}IFMEDIA.
112		 */
113		return;
114	}
115
116	if (ifmr.ifm_count == 0) {
117		warnx("%s: no media types?", name);
118		return;
119	}
120
121	media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
122	if (media_list == NULL)
123		err(1, "malloc");
124	ifmr.ifm_ulist = media_list;
125
126	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
127		err(1, "SIOCGIFMEDIA");
128
129	printf("\tmedia: ");
130	print_media_word(ifmr.ifm_current);
131	if (ifmr.ifm_active != ifmr.ifm_current) {
132		putchar(' ');
133		putchar('(');
134		print_media_word(ifmr.ifm_active);
135		putchar(')');
136	}
137
138	if (ifmr.ifm_status & IFM_AVALID) {
139		printf(" status: ");
140		switch (IFM_TYPE(ifmr.ifm_active)) {
141		case IFM_ETHER:
142			if (ifmr.ifm_status & IFM_ACTIVE)
143				printf("active");
144			else
145				printf("no carrier");
146			break;
147
148		case IFM_FDDI:
149		case IFM_TOKEN:
150			if (ifmr.ifm_status & IFM_ACTIVE)
151				printf("inserted");
152			else
153				printf("no ring");
154			break;
155		case IFM_IEEE80211:
156			/* XXX: Different value for adhoc? */
157			if (ifmr.ifm_status & IFM_ACTIVE)
158				printf("associated");
159			else
160				printf("no carrier");
161			break;
162		}
163	}
164
165	putchar('\n');
166
167	if (ifmr.ifm_count > 0) {
168		printf("\tsupported media:");
169		for (i = 0; i < ifmr.ifm_count; i++) {
170			putchar(' ');
171			print_media_word(media_list[i]);
172		}
173		putchar('\n');
174	}
175
176	free(media_list);
177}
178
179void
180setmedia(val, d, s, afp)
181	const char *val;
182	int d;
183	int s;
184	const struct afswtch *afp;
185{
186	struct ifmediareq ifmr;
187	int first_type, subtype;
188
189	(void) memset(&ifmr, 0, sizeof(ifmr));
190	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
191
192	ifmr.ifm_count = 1;
193	ifmr.ifm_ulist = &first_type;
194	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
195		/*
196		 * If we get E2BIG, the kernel is telling us
197		 * that there are more, so we can ignore it.
198		 */
199		if (errno != E2BIG)
200			err(1, "SIOCGIFMEDIA");
201	}
202
203	if (ifmr.ifm_count == 0)
204		errx(1, "%s: no media types?", name);
205
206	/*
207	 * We are primarily concerned with the top-level type.
208	 * However, "current" may be only IFM_NONE, so we just look
209	 * for the top-level type in the first "supported type"
210	 * entry.
211	 *
212	 * (I'm assuming that all supported media types for a given
213	 * interface will be the same top-level type..)
214	 */
215	subtype = get_media_subtype(IFM_TYPE(first_type), val);
216
217	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
218	ifr.ifr_media = (ifmr.ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
219	    IFM_TYPE(first_type) | subtype;
220
221	if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
222		err(1, "SIOCSIFMEDIA");
223}
224
225void
226setmediaopt(val, d, s, afp)
227	const char *val;
228	int d;
229	int s;
230	const struct afswtch *afp;
231{
232
233	domediaopt(val, 0, s);
234}
235
236void
237unsetmediaopt(val, d, s, afp)
238	const char *val;
239	int d;
240	int s;
241	const struct afswtch *afp;
242{
243
244	domediaopt(val, 1, s);
245}
246
247static void
248domediaopt(val, clear, s)
249	const char *val;
250	int clear;
251	int s;
252{
253	struct ifmediareq ifmr;
254	int *mwords, options;
255
256	(void) memset(&ifmr, 0, sizeof(ifmr));
257	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
258
259	/*
260	 * We must go through the motions of reading all
261	 * supported media because we need to know both
262	 * the current media type and the top-level type.
263	 */
264
265	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
266		err(1, "SIOCGIFMEDIA");
267
268	if (ifmr.ifm_count == 0)
269		errx(1, "%s: no media types?", name);
270
271	mwords = (int *)malloc(ifmr.ifm_count * sizeof(int));
272	if (mwords == NULL)
273		err(1, "malloc");
274
275	ifmr.ifm_ulist = mwords;
276	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
277		err(1, "SIOCGIFMEDIA");
278
279	options = get_media_options(IFM_TYPE(mwords[0]), val);
280
281	free(mwords);
282
283	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
284	ifr.ifr_media = ifmr.ifm_current;
285	if (clear)
286		ifr.ifr_media &= ~options;
287	else
288		ifr.ifr_media |= options;
289
290	if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
291		err(1, "SIOCSIFMEDIA");
292}
293
294/**********************************************************************
295 * A good chunk of this is duplicated from sys/net/ifmedia.c
296 **********************************************************************/
297
298static struct ifmedia_description ifm_type_descriptions[] =
299    IFM_TYPE_DESCRIPTIONS;
300
301static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
302    IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
303
304static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
305    IFM_SUBTYPE_ETHERNET_ALIASES;
306
307static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
308    IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
309
310static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
311    IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
312
313static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
314    IFM_SUBTYPE_TOKENRING_ALIASES;
315
316static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
317    IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
318
319static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
320    IFM_SUBTYPE_FDDI_DESCRIPTIONS;
321
322static struct ifmedia_description ifm_subtype_fddi_aliases[] =
323    IFM_SUBTYPE_FDDI_ALIASES;
324
325static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
326    IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
327
328static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
329    IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
330
331static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
332    IFM_SUBTYPE_IEEE80211_ALIASES;
333
334static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
335    IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
336
337static struct ifmedia_description ifm_subtype_shared_descriptions[] =
338    IFM_SUBTYPE_SHARED_DESCRIPTIONS;
339
340static struct ifmedia_description ifm_subtype_shared_aliases[] =
341    IFM_SUBTYPE_SHARED_ALIASES;
342
343static struct ifmedia_description ifm_shared_option_descriptions[] =
344    IFM_SHARED_OPTION_DESCRIPTIONS;
345
346struct ifmedia_type_to_subtype {
347	struct {
348		struct ifmedia_description *desc;
349		int alias;
350	} subtypes[5];
351	struct {
352		struct ifmedia_description *desc;
353		int alias;
354	} options[3];
355};
356
357/* must be in the same order as IFM_TYPE_DESCRIPTIONS */
358static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
359	{
360		{
361			{ &ifm_subtype_shared_descriptions[0], 0 },
362			{ &ifm_subtype_shared_aliases[0], 1 },
363			{ &ifm_subtype_ethernet_descriptions[0], 0 },
364			{ &ifm_subtype_ethernet_aliases[0], 1 },
365			{ NULL, 0 },
366		},
367		{
368			{ &ifm_shared_option_descriptions[0], 0 },
369			{ &ifm_subtype_ethernet_option_descriptions[0], 0 },
370			{ NULL, 0 },
371		},
372	},
373	{
374		{
375			{ &ifm_subtype_shared_descriptions[0], 0 },
376			{ &ifm_subtype_shared_aliases[0], 1 },
377			{ &ifm_subtype_tokenring_descriptions[0], 0 },
378			{ &ifm_subtype_tokenring_aliases[0], 1 },
379			{ NULL, 0 },
380		},
381		{
382			{ &ifm_shared_option_descriptions[0], 0 },
383			{ &ifm_subtype_tokenring_option_descriptions[0], 0 },
384			{ NULL, 0 },
385		},
386	},
387	{
388		{
389			{ &ifm_subtype_shared_descriptions[0], 0 },
390			{ &ifm_subtype_shared_aliases[0], 1 },
391			{ &ifm_subtype_fddi_descriptions[0], 0 },
392			{ &ifm_subtype_fddi_aliases[0], 1 },
393			{ NULL, 0 },
394		},
395		{
396			{ &ifm_shared_option_descriptions[0], 0 },
397			{ &ifm_subtype_fddi_option_descriptions[0], 0 },
398			{ NULL, 0 },
399		},
400	},
401	{
402		{
403			{ &ifm_subtype_shared_descriptions[0], 0 },
404			{ &ifm_subtype_shared_aliases[0], 1 },
405			{ &ifm_subtype_ieee80211_descriptions[0], 0 },
406			{ &ifm_subtype_ieee80211_aliases[0], 1 },
407			{ NULL, 0 },
408		},
409		{
410			{ &ifm_shared_option_descriptions[0], 0 },
411			{ &ifm_subtype_ieee80211_option_descriptions[0], 0 },
412			{ NULL, 0 },
413		},
414	},
415};
416
417static int
418get_media_subtype(type, val)
419	int type;
420	const char *val;
421{
422	struct ifmedia_description *desc;
423	struct ifmedia_type_to_subtype *ttos;
424	int rval, i;
425
426	/* Find the top-level interface type. */
427	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
428	    desc->ifmt_string != NULL; desc++, ttos++)
429		if (type == desc->ifmt_word)
430			break;
431	if (desc->ifmt_string == NULL)
432		errx(1, "unknown media type 0x%x", type);
433
434	for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
435		rval = lookup_media_word(ttos->subtypes[i].desc, val);
436		if (rval != -1)
437			return (rval);
438	}
439	errx(1, "unknown media subtype: %s", val);
440	/* NOTREACHED */
441}
442
443static int
444get_media_options(type, val)
445	int type;
446	const char *val;
447{
448	struct ifmedia_description *desc;
449	struct ifmedia_type_to_subtype *ttos;
450	char *optlist, *optptr;
451	int option = 0, i, rval = 0;
452
453	/* We muck with the string, so copy it. */
454	optlist = strdup(val);
455	if (optlist == NULL)
456		err(1, "strdup");
457
458	/* Find the top-level interface type. */
459	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
460	    desc->ifmt_string != NULL; desc++, ttos++)
461		if (type == desc->ifmt_word)
462			break;
463	if (desc->ifmt_string == NULL)
464		errx(1, "unknown media type 0x%x", type);
465
466	/*
467	 * Look up the options in the user-provided comma-separated
468	 * list.
469	 */
470	optptr = optlist;
471	for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
472		for (i = 0; ttos->options[i].desc != NULL; i++) {
473			option = lookup_media_word(ttos->options[i].desc, optptr);
474			if (option != -1)
475				break;
476		}
477		if (option == 0)
478			errx(1, "unknown option: %s", optptr);
479		rval |= option;
480	}
481
482	free(optlist);
483	return (rval);
484}
485
486static int
487lookup_media_word(desc, val)
488	struct ifmedia_description *desc;
489	const char *val;
490{
491
492	for (; desc->ifmt_string != NULL; desc++)
493		if (strcasecmp(desc->ifmt_string, val) == 0)
494			return (desc->ifmt_word);
495
496	return (-1);
497}
498
499static void
500print_media_word(ifmw)
501	int ifmw;
502{
503	struct ifmedia_description *desc;
504	struct ifmedia_type_to_subtype *ttos;
505	int seen_option = 0, i;
506
507	/* Find the top-level interface type. */
508	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
509	    desc->ifmt_string != NULL; desc++, ttos++)
510		if (IFM_TYPE(ifmw) == desc->ifmt_word)
511			break;
512	if (desc->ifmt_string == NULL) {
513		printf("<unknown type>");
514		return;
515	}
516
517	/*
518	 * Don't print the top-level type; it's not like we can
519	 * change it, or anything.
520	 */
521
522	/* Find subtype. */
523	for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
524		if (ttos->subtypes[i].alias)
525			continue;
526		for (desc = ttos->subtypes[i].desc;
527		    desc->ifmt_string != NULL; desc++) {
528			if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
529				goto got_subtype;
530		}
531	}
532
533	/* Falling to here means unknown subtype. */
534	printf("<unknown subtype>");
535	return;
536
537 got_subtype:
538	printf("%s", desc->ifmt_string);
539
540	/* Find options. */
541	for (i = 0; ttos->options[i].desc != NULL; i++) {
542		if (ttos->options[i].alias)
543			continue;
544		for (desc = ttos->options[i].desc;
545		    desc->ifmt_string != NULL; desc++) {
546			if (ifmw & desc->ifmt_word) {
547				if (seen_option == 0)
548					printf(" <");
549				printf("%s%s", seen_option++ ? "," : "",
550				    desc->ifmt_string);
551			}
552		}
553	}
554	printf("%s", seen_option ? ">" : "");
555}
556
557/**********************************************************************
558 * ...until here.
559 **********************************************************************/
560