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 $ */
3 /* $DragonFly: src/sbin/ifconfig/ifmedia.c,v 1.8 2005/03/06 05:01:59 dillon Exp $ */
6 * Copyright (c) 1997 Jason R. Thorpe.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Copyright (c) 1983, 1993
39 * The Regents of the University of California. All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 #include <sys/param.h>
71 #include <sys/ioctl.h>
72 #include <sys/socket.h>
73 #include <sys/sysctl.h>
77 #include <net/if_dl.h>
78 #include <net/if_types.h>
79 #include <net/if_media.h>
80 #include <net/route.h>
93 #ifndef IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS
94 #define IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS { \
95 { IFM_AUTO, "autoselect" }, \
96 { IFM_IEEE80211_11A, "11a" }, \
97 { IFM_IEEE80211_11B, "11b" }, \
98 { IFM_IEEE80211_11G, "11g" }, \
99 { IFM_IEEE80211_FH, "fh" }, \
104 #ifndef IFM_SUBTYPE_IEEE80211_MODE_ALIASES
105 #define IFM_SUBTYPE_IEEE80211_MODE_ALIASES { \
106 { IFM_AUTO, "auto" }, \
111 static void domediaopt(const char *, int, int);
112 static int get_media_subtype(int, const char *);
113 static int get_media_mode(int, const char *);
114 static int get_media_options(int, const char *);
115 static int lookup_media_word(struct ifmedia_description *, const char *);
116 static void print_media_word(int, int);
117 static void print_media_word_ifconfig(int);
119 static struct ifmedia_description *get_toptype_desc(int);
120 static struct ifmedia_type_to_subtype *get_toptype_ttos(int);
121 static struct ifmedia_description *get_subtype_desc(int,
122 struct ifmedia_type_to_subtype *ttos);
125 media_status(int s, struct rt_addrinfo *info __unused)
127 struct ifmediareq ifmr;
130 (void) memset(&ifmr, 0, sizeof(ifmr));
131 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
133 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
135 * Interface doesn't support SIOC{G,S}IFMEDIA.
140 if (ifmr.ifm_count == 0) {
141 warnx("%s: no media types?", name);
145 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
146 if (media_list == NULL)
148 ifmr.ifm_ulist = media_list;
150 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
151 err(1, "SIOCGIFMEDIA");
154 print_media_word(ifmr.ifm_current, 1);
155 if (ifmr.ifm_active != ifmr.ifm_current) {
158 print_media_word(ifmr.ifm_active, 0);
164 if (ifmr.ifm_status & IFM_AVALID) {
165 printf("\tstatus: ");
166 switch (IFM_TYPE(ifmr.ifm_active)) {
168 if (ifmr.ifm_status & IFM_ACTIVE)
171 printf("no carrier");
176 if (ifmr.ifm_status & IFM_ACTIVE)
184 if (ifmr.ifm_status & IFM_ACTIVE)
187 printf("no carrier");
192 /* XXX: Different value for adhoc? */
193 if (ifmr.ifm_status & IFM_ACTIVE)
194 printf("associated");
196 printf("no carrier");
202 if (ifmr.ifm_count > 0 && supmedia) {
203 printf("\tsupported media:\n");
204 for (i = 0; i < ifmr.ifm_count; i++) {
206 print_media_word_ifconfig(media_list[i]);
215 setmedia(const char *val, int d __unused, int s,
216 const struct afswtch *afp __unused)
218 struct ifmediareq ifmr;
219 int first_type, subtype;
221 (void) memset(&ifmr, 0, sizeof(ifmr));
222 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
225 ifmr.ifm_ulist = &first_type;
226 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
228 * If we get E2BIG, the kernel is telling us
229 * that there are more, so we can ignore it.
232 err(1, "SIOCGIFMEDIA");
235 if (ifmr.ifm_count == 0)
236 errx(1, "%s: no media types?", name);
239 * We are primarily concerned with the top-level type.
240 * However, "current" may be only IFM_NONE, so we just look
241 * for the top-level type in the first "supported type"
244 * (I'm assuming that all supported media types for a given
245 * interface will be the same top-level type..)
247 subtype = get_media_subtype(IFM_TYPE(first_type), val);
249 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
250 ifr.ifr_media = (ifmr.ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
251 IFM_TYPE(first_type) | subtype;
253 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
254 err(1, "SIOCSIFMEDIA (media)");
258 setmediaopt(const char *val, int d __unused, int s,
259 const struct afswtch *afp __unused)
262 domediaopt(val, 0, s);
266 unsetmediaopt(const char *val, int d __unused, int s,
267 const struct afswtch *afp __unused)
270 domediaopt(val, 1, s);
274 domediaopt(const char *val, int clear, int s)
276 struct ifmediareq ifmr;
277 int *mwords, options;
279 (void) memset(&ifmr, 0, sizeof(ifmr));
280 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
283 * We must go through the motions of reading all
284 * supported media because we need to know both
285 * the current media type and the top-level type.
288 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
289 err(1, "SIOCGIFMEDIA");
291 if (ifmr.ifm_count == 0)
292 errx(1, "%s: no media types?", name);
294 mwords = (int *)malloc(ifmr.ifm_count * sizeof(int));
298 ifmr.ifm_ulist = mwords;
299 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
300 err(1, "SIOCGIFMEDIA");
302 options = get_media_options(IFM_TYPE(mwords[0]), val);
306 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
307 ifr.ifr_media = ifmr.ifm_current;
309 ifr.ifr_media &= ~options;
311 ifr.ifr_media |= options;
313 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
314 err(1, "SIOCSIFMEDIA (mediaopt)");
319 setmediamode(const char *val, int d __unused, int s,
320 const struct afswtch *afp __unused)
322 struct ifmediareq ifmr;
325 (void) memset(&ifmr, 0, sizeof(ifmr));
326 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
329 * We must go through the motions of reading all
330 * supported media because we need to know both
331 * the current media type and the top-level type.
334 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
335 err(1, "SIOCGIFMEDIA");
337 if (ifmr.ifm_count == 0)
338 errx(1, "%s: no media types?", name);
340 mwords = (int *)malloc(ifmr.ifm_count * sizeof(int));
344 ifmr.ifm_ulist = mwords;
345 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
346 err(1, "SIOCGIFMEDIA");
348 mode = get_media_mode(IFM_TYPE(mwords[0]), val);
352 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
353 ifr.ifr_media = (ifmr.ifm_current & ~IFM_MMASK) | mode;
355 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
356 err(1, "SIOCSIFMEDIA (mode)");
359 /**********************************************************************
360 * A good chunk of this is duplicated from sys/net/ifmedia.c
361 **********************************************************************/
363 static struct ifmedia_description ifm_type_descriptions[] =
364 IFM_TYPE_DESCRIPTIONS;
366 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
367 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
369 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
370 IFM_SUBTYPE_ETHERNET_ALIASES;
372 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
373 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
375 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
376 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
378 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
379 IFM_SUBTYPE_TOKENRING_ALIASES;
381 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
382 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
384 static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
385 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
387 static struct ifmedia_description ifm_subtype_fddi_aliases[] =
388 IFM_SUBTYPE_FDDI_ALIASES;
390 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
391 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
393 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
394 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
396 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
397 IFM_SUBTYPE_IEEE80211_ALIASES;
399 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
400 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
402 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
403 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
405 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
406 IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
409 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
410 IFM_SUBTYPE_ATM_DESCRIPTIONS;
412 static struct ifmedia_description ifm_subtype_atm_aliases[] =
413 IFM_SUBTYPE_ATM_ALIASES;
415 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
416 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
419 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
420 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
422 static struct ifmedia_description ifm_subtype_shared_aliases[] =
423 IFM_SUBTYPE_SHARED_ALIASES;
425 static struct ifmedia_description ifm_shared_option_descriptions[] =
426 IFM_SHARED_OPTION_DESCRIPTIONS;
428 struct ifmedia_type_to_subtype {
430 struct ifmedia_description *desc;
434 struct ifmedia_description *desc;
438 struct ifmedia_description *desc;
443 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
444 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
447 { &ifm_subtype_shared_descriptions[0], 0 },
448 { &ifm_subtype_shared_aliases[0], 1 },
449 { &ifm_subtype_ethernet_descriptions[0], 0 },
450 { &ifm_subtype_ethernet_aliases[0], 1 },
454 { &ifm_shared_option_descriptions[0], 0 },
455 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
464 { &ifm_subtype_shared_descriptions[0], 0 },
465 { &ifm_subtype_shared_aliases[0], 1 },
466 { &ifm_subtype_tokenring_descriptions[0], 0 },
467 { &ifm_subtype_tokenring_aliases[0], 1 },
471 { &ifm_shared_option_descriptions[0], 0 },
472 { &ifm_subtype_tokenring_option_descriptions[0], 0 },
481 { &ifm_subtype_shared_descriptions[0], 0 },
482 { &ifm_subtype_shared_aliases[0], 1 },
483 { &ifm_subtype_fddi_descriptions[0], 0 },
484 { &ifm_subtype_fddi_aliases[0], 1 },
488 { &ifm_shared_option_descriptions[0], 0 },
489 { &ifm_subtype_fddi_option_descriptions[0], 0 },
498 { &ifm_subtype_shared_descriptions[0], 0 },
499 { &ifm_subtype_shared_aliases[0], 1 },
500 { &ifm_subtype_ieee80211_descriptions[0], 0 },
501 { &ifm_subtype_ieee80211_aliases[0], 1 },
505 { &ifm_shared_option_descriptions[0], 0 },
506 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
510 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
511 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
517 { &ifm_subtype_shared_descriptions[0], 0 },
518 { &ifm_subtype_shared_aliases[0], 1 },
520 { &ifm_subtype_atm_descriptions[0], 0 },
521 { &ifm_subtype_atm_aliases[0], 1 },
526 { &ifm_shared_option_descriptions[0], 0 },
528 { &ifm_subtype_atm_option_descriptions[0], 0 },
539 get_media_subtype(int type, const char *val)
541 struct ifmedia_description *desc;
542 struct ifmedia_type_to_subtype *ttos;
545 /* Find the top-level interface type. */
546 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
547 desc->ifmt_string != NULL; desc++, ttos++)
548 if (type == desc->ifmt_word)
550 if (desc->ifmt_string == NULL)
551 errx(1, "unknown media type 0x%x", type);
553 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
554 rval = lookup_media_word(ttos->subtypes[i].desc, val);
558 errx(1, "unknown media subtype: %s", val);
563 get_media_mode(int type, const char *val)
565 struct ifmedia_description *desc;
566 struct ifmedia_type_to_subtype *ttos;
569 /* Find the top-level interface type. */
570 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
571 desc->ifmt_string != NULL; desc++, ttos++)
572 if (type == desc->ifmt_word)
574 if (desc->ifmt_string == NULL)
575 errx(1, "unknown media mode 0x%x", type);
577 for (i = 0; ttos->modes[i].desc != NULL; i++) {
578 rval = lookup_media_word(ttos->modes[i].desc, val);
586 get_media_options(int type, const char *val)
588 struct ifmedia_description *desc;
589 struct ifmedia_type_to_subtype *ttos;
590 char *optlist, *optptr;
591 int option = 0, i, rval = 0;
593 /* We muck with the string, so copy it. */
594 optlist = strdup(val);
598 /* Find the top-level interface type. */
599 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
600 desc->ifmt_string != NULL; desc++, ttos++)
601 if (type == desc->ifmt_word)
603 if (desc->ifmt_string == NULL)
604 errx(1, "unknown media type 0x%x", type);
607 * Look up the options in the user-provided comma-separated
611 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
612 for (i = 0; ttos->options[i].desc != NULL; i++) {
613 option = lookup_media_word(ttos->options[i].desc, optptr);
618 errx(1, "unknown option: %s", optptr);
627 lookup_media_word(struct ifmedia_description *desc, const char *val)
630 for (; desc->ifmt_string != NULL; desc++)
631 if (strcasecmp(desc->ifmt_string, val) == 0)
632 return (desc->ifmt_word);
637 static struct ifmedia_description *get_toptype_desc(int ifmw)
639 struct ifmedia_description *desc;
641 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
642 if (IFM_TYPE(ifmw) == desc->ifmt_word)
648 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
650 struct ifmedia_description *desc;
651 struct ifmedia_type_to_subtype *ttos;
653 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
654 desc->ifmt_string != NULL; desc++, ttos++)
655 if (IFM_TYPE(ifmw) == desc->ifmt_word)
661 static struct ifmedia_description *get_subtype_desc(int ifmw,
662 struct ifmedia_type_to_subtype *ttos)
665 struct ifmedia_description *desc;
667 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
668 if (ttos->subtypes[i].alias)
670 for (desc = ttos->subtypes[i].desc;
671 desc->ifmt_string != NULL; desc++) {
672 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
680 static struct ifmedia_description *get_mode_desc(int ifmw,
681 struct ifmedia_type_to_subtype *ttos)
684 struct ifmedia_description *desc;
686 for (i = 0; ttos->modes[i].desc != NULL; i++) {
687 if (ttos->modes[i].alias)
689 for (desc = ttos->modes[i].desc;
690 desc->ifmt_string != NULL; desc++) {
691 if (IFM_MODE(ifmw) == desc->ifmt_word)
700 print_media_word(int ifmw, int print_toptype)
702 struct ifmedia_description *desc;
703 struct ifmedia_type_to_subtype *ttos;
704 int seen_option = 0, i;
706 /* Find the top-level interface type. */
707 desc = get_toptype_desc(ifmw);
708 ttos = get_toptype_ttos(ifmw);
709 if (desc->ifmt_string == NULL) {
710 printf("<unknown type>");
712 } else if (print_toptype) {
713 printf("%s", desc->ifmt_string);
717 * Don't print the top-level type; it's not like we can
718 * change it, or anything.
722 desc = get_subtype_desc(ifmw, ttos);
726 /* Falling to here means unknown subtype. */
727 printf("<unknown subtype>");
734 printf("%s", desc->ifmt_string);
737 desc = get_mode_desc(ifmw, ttos);
738 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
739 printf(" mode %s", desc->ifmt_string);
743 for (i = 0; ttos->options[i].desc != NULL; i++) {
744 if (ttos->options[i].alias)
746 for (desc = ttos->options[i].desc;
747 desc->ifmt_string != NULL; desc++) {
748 if (ifmw & desc->ifmt_word) {
749 if (seen_option == 0)
751 printf("%s%s", seen_option++ ? "," : "",
756 printf("%s", seen_option ? ">" : "");
760 print_media_word_ifconfig(int ifmw)
762 struct ifmedia_description *desc;
763 struct ifmedia_type_to_subtype *ttos;
766 /* Find the top-level interface type. */
767 desc = get_toptype_desc(ifmw);
768 ttos = get_toptype_ttos(ifmw);
769 if (desc->ifmt_string == NULL) {
770 printf("<unknown type>");
775 * Don't print the top-level type; it's not like we can
776 * change it, or anything.
780 desc = get_subtype_desc(ifmw, ttos);
784 /* Falling to here means unknown subtype. */
785 printf("<unknown subtype>");
789 printf("media %s", desc->ifmt_string);
791 desc = get_mode_desc(ifmw, ttos);
793 printf(" mode %s", desc->ifmt_string);
796 for (i = 0; ttos->options[i].desc != NULL; i++) {
797 if (ttos->options[i].alias)
799 for (desc = ttos->options[i].desc;
800 desc->ifmt_string != NULL; desc++) {
801 if (ifmw & desc->ifmt_word) {
802 printf(" mediaopt %s", desc->ifmt_string);
808 /**********************************************************************
810 **********************************************************************/