1 /* $NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $ */
2 /* $FreeBSD: src/sbin/ifconfig/ifmedia.c,v 1.6.2.3 2001/11/14 04:35:07 yar Exp $ */
5 * Copyright (c) 1997 Jason R. Thorpe.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
37 * Copyright (c) 1983, 1993
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
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.
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
69 #include <sys/param.h>
70 #include <sys/ioctl.h>
71 #include <sys/socket.h>
72 #include <sys/sysctl.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_media.h>
79 #include <net/route.h>
92 static void domediaopt __P((const char *, int, int));
93 static int get_media_subtype __P((int, const char *));
94 static int get_media_options __P((int, const char *));
95 static int lookup_media_word __P((struct ifmedia_description *, const char *));
96 static void print_media_word __P((int, int));
97 static void print_media_word_ifconfig __P((int));
99 static struct ifmedia_description *get_toptype_desc __P((int));
100 static struct ifmedia_type_to_subtype *get_toptype_ttos __P((int));
101 static struct ifmedia_description *get_subtype_desc __P((int,
102 struct ifmedia_type_to_subtype *ttos));
105 media_status(s, info)
107 struct rt_addrinfo *info __unused;
109 struct ifmediareq ifmr;
112 (void) memset(&ifmr, 0, sizeof(ifmr));
113 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
115 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
117 * Interface doesn't support SIOC{G,S}IFMEDIA.
122 if (ifmr.ifm_count == 0) {
123 warnx("%s: no media types?", name);
127 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
128 if (media_list == NULL)
130 ifmr.ifm_ulist = media_list;
132 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
133 err(1, "SIOCGIFMEDIA");
136 print_media_word(ifmr.ifm_current, 1);
137 if (ifmr.ifm_active != ifmr.ifm_current) {
140 print_media_word(ifmr.ifm_active, 0);
146 if (ifmr.ifm_status & IFM_AVALID) {
147 printf("\tstatus: ");
148 switch (IFM_TYPE(ifmr.ifm_active)) {
150 if (ifmr.ifm_status & IFM_ACTIVE)
153 printf("no carrier");
158 if (ifmr.ifm_status & IFM_ACTIVE)
164 /* XXX: Different value for adhoc? */
165 if (ifmr.ifm_status & IFM_ACTIVE)
166 printf("associated");
168 printf("no carrier");
174 if (ifmr.ifm_count > 0 && supmedia) {
175 printf("\tsupported media:\n");
176 for (i = 0; i < ifmr.ifm_count; i++) {
178 print_media_word_ifconfig(media_list[i]);
187 setmedia(val, d, s, afp)
191 const struct afswtch *afp;
193 struct ifmediareq ifmr;
194 int first_type, subtype;
196 (void) memset(&ifmr, 0, sizeof(ifmr));
197 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
200 ifmr.ifm_ulist = &first_type;
201 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
203 * If we get E2BIG, the kernel is telling us
204 * that there are more, so we can ignore it.
207 err(1, "SIOCGIFMEDIA");
210 if (ifmr.ifm_count == 0)
211 errx(1, "%s: no media types?", name);
214 * We are primarily concerned with the top-level type.
215 * However, "current" may be only IFM_NONE, so we just look
216 * for the top-level type in the first "supported type"
219 * (I'm assuming that all supported media types for a given
220 * interface will be the same top-level type..)
222 subtype = get_media_subtype(IFM_TYPE(first_type), val);
224 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
225 ifr.ifr_media = (ifmr.ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
226 IFM_TYPE(first_type) | subtype;
228 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
229 err(1, "SIOCSIFMEDIA");
233 setmediaopt(val, d, s, afp)
237 const struct afswtch *afp;
240 domediaopt(val, 0, s);
244 unsetmediaopt(val, d, s, afp)
248 const struct afswtch *afp;
251 domediaopt(val, 1, s);
255 domediaopt(val, clear, s)
260 struct ifmediareq ifmr;
261 int *mwords, options;
263 (void) memset(&ifmr, 0, sizeof(ifmr));
264 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
267 * We must go through the motions of reading all
268 * supported media because we need to know both
269 * the current media type and the top-level type.
272 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
273 err(1, "SIOCGIFMEDIA");
275 if (ifmr.ifm_count == 0)
276 errx(1, "%s: no media types?", name);
278 mwords = (int *)malloc(ifmr.ifm_count * sizeof(int));
282 ifmr.ifm_ulist = mwords;
283 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
284 err(1, "SIOCGIFMEDIA");
286 options = get_media_options(IFM_TYPE(mwords[0]), val);
290 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
291 ifr.ifr_media = ifmr.ifm_current;
293 ifr.ifr_media &= ~options;
295 ifr.ifr_media |= options;
297 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
298 err(1, "SIOCSIFMEDIA");
301 /**********************************************************************
302 * A good chunk of this is duplicated from sys/net/ifmedia.c
303 **********************************************************************/
305 static struct ifmedia_description ifm_type_descriptions[] =
306 IFM_TYPE_DESCRIPTIONS;
308 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
309 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
311 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
312 IFM_SUBTYPE_ETHERNET_ALIASES;
314 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
315 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
317 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
318 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
320 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
321 IFM_SUBTYPE_TOKENRING_ALIASES;
323 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
324 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
326 static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
327 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
329 static struct ifmedia_description ifm_subtype_fddi_aliases[] =
330 IFM_SUBTYPE_FDDI_ALIASES;
332 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
333 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
335 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
336 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
338 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
339 IFM_SUBTYPE_IEEE80211_ALIASES;
341 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
342 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
344 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
345 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
347 static struct ifmedia_description ifm_subtype_shared_aliases[] =
348 IFM_SUBTYPE_SHARED_ALIASES;
350 static struct ifmedia_description ifm_shared_option_descriptions[] =
351 IFM_SHARED_OPTION_DESCRIPTIONS;
353 struct ifmedia_type_to_subtype {
355 struct ifmedia_description *desc;
359 struct ifmedia_description *desc;
364 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
365 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
368 { &ifm_subtype_shared_descriptions[0], 0 },
369 { &ifm_subtype_shared_aliases[0], 1 },
370 { &ifm_subtype_ethernet_descriptions[0], 0 },
371 { &ifm_subtype_ethernet_aliases[0], 1 },
375 { &ifm_shared_option_descriptions[0], 0 },
376 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
382 { &ifm_subtype_shared_descriptions[0], 0 },
383 { &ifm_subtype_shared_aliases[0], 1 },
384 { &ifm_subtype_tokenring_descriptions[0], 0 },
385 { &ifm_subtype_tokenring_aliases[0], 1 },
389 { &ifm_shared_option_descriptions[0], 0 },
390 { &ifm_subtype_tokenring_option_descriptions[0], 0 },
396 { &ifm_subtype_shared_descriptions[0], 0 },
397 { &ifm_subtype_shared_aliases[0], 1 },
398 { &ifm_subtype_fddi_descriptions[0], 0 },
399 { &ifm_subtype_fddi_aliases[0], 1 },
403 { &ifm_shared_option_descriptions[0], 0 },
404 { &ifm_subtype_fddi_option_descriptions[0], 0 },
410 { &ifm_subtype_shared_descriptions[0], 0 },
411 { &ifm_subtype_shared_aliases[0], 1 },
412 { &ifm_subtype_ieee80211_descriptions[0], 0 },
413 { &ifm_subtype_ieee80211_aliases[0], 1 },
417 { &ifm_shared_option_descriptions[0], 0 },
418 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
425 get_media_subtype(type, val)
429 struct ifmedia_description *desc;
430 struct ifmedia_type_to_subtype *ttos;
433 /* Find the top-level interface type. */
434 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
435 desc->ifmt_string != NULL; desc++, ttos++)
436 if (type == desc->ifmt_word)
438 if (desc->ifmt_string == NULL)
439 errx(1, "unknown media type 0x%x", type);
441 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
442 rval = lookup_media_word(ttos->subtypes[i].desc, val);
446 errx(1, "unknown media subtype: %s", val);
451 get_media_options(type, val)
455 struct ifmedia_description *desc;
456 struct ifmedia_type_to_subtype *ttos;
457 char *optlist, *optptr;
458 int option = 0, i, rval = 0;
460 /* We muck with the string, so copy it. */
461 optlist = strdup(val);
465 /* Find the top-level interface type. */
466 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
467 desc->ifmt_string != NULL; desc++, ttos++)
468 if (type == desc->ifmt_word)
470 if (desc->ifmt_string == NULL)
471 errx(1, "unknown media type 0x%x", type);
474 * Look up the options in the user-provided comma-separated
478 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
479 for (i = 0; ttos->options[i].desc != NULL; i++) {
480 option = lookup_media_word(ttos->options[i].desc, optptr);
485 errx(1, "unknown option: %s", optptr);
494 lookup_media_word(desc, val)
495 struct ifmedia_description *desc;
499 for (; desc->ifmt_string != NULL; desc++)
500 if (strcasecmp(desc->ifmt_string, val) == 0)
501 return (desc->ifmt_word);
506 static struct ifmedia_description *get_toptype_desc(ifmw)
509 struct ifmedia_description *desc;
511 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
512 if (IFM_TYPE(ifmw) == desc->ifmt_word)
518 static struct ifmedia_type_to_subtype *get_toptype_ttos(ifmw)
521 struct ifmedia_description *desc;
522 struct ifmedia_type_to_subtype *ttos;
524 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
525 desc->ifmt_string != NULL; desc++, ttos++)
526 if (IFM_TYPE(ifmw) == desc->ifmt_word)
532 static struct ifmedia_description *get_subtype_desc(ifmw, ttos)
534 struct ifmedia_type_to_subtype *ttos;
537 struct ifmedia_description *desc;
539 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
540 if (ttos->subtypes[i].alias)
542 for (desc = ttos->subtypes[i].desc;
543 desc->ifmt_string != NULL; desc++) {
544 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
553 print_media_word(ifmw, print_toptype)
557 struct ifmedia_description *desc;
558 struct ifmedia_type_to_subtype *ttos;
559 int seen_option = 0, i;
561 /* Find the top-level interface type. */
562 desc = get_toptype_desc(ifmw);
563 ttos = get_toptype_ttos(ifmw);
564 if (desc->ifmt_string == NULL) {
565 printf("<unknown type>");
567 } else if (print_toptype) {
568 printf("%s", desc->ifmt_string);
572 * Don't print the top-level type; it's not like we can
573 * change it, or anything.
577 desc = get_subtype_desc(ifmw, ttos);
581 /* Falling to here means unknown subtype. */
582 printf("<unknown subtype>");
589 printf("%s", desc->ifmt_string);
592 for (i = 0; ttos->options[i].desc != NULL; i++) {
593 if (ttos->options[i].alias)
595 for (desc = ttos->options[i].desc;
596 desc->ifmt_string != NULL; desc++) {
597 if (ifmw & desc->ifmt_word) {
598 if (seen_option == 0)
600 printf("%s%s", seen_option++ ? "," : "",
605 printf("%s", seen_option ? ">" : "");
609 print_media_word_ifconfig(ifmw)
612 struct ifmedia_description *desc;
613 struct ifmedia_type_to_subtype *ttos;
616 /* Find the top-level interface type. */
617 desc = get_toptype_desc(ifmw);
618 ttos = get_toptype_ttos(ifmw);
619 if (desc->ifmt_string == NULL) {
620 printf("<unknown type>");
625 * Don't print the top-level type; it's not like we can
626 * change it, or anything.
630 desc = get_subtype_desc(ifmw, ttos);
634 /* Falling to here means unknown subtype. */
635 printf("<unknown subtype>");
639 printf("media %s", desc->ifmt_string);
642 for (i = 0; ttos->options[i].desc != NULL; i++) {
643 if (ttos->options[i].alias)
645 for (desc = ttos->options[i].desc;
646 desc->ifmt_string != NULL; desc++) {
647 if (ifmw & desc->ifmt_word) {
648 printf(" mediaopt %s", desc->ifmt_string);
654 /**********************************************************************
656 **********************************************************************/