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.9 2005/11/06 12:21:42 swildner 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 memset(&ifmr, 0, sizeof(ifmr));
131 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 memset(&ifmr, 0, sizeof(ifmr));
222 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 memset(&ifmr, 0, sizeof(ifmr));
280 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 memset(&ifmr, 0, sizeof(ifmr));
326 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 *
638 get_toptype_desc(int ifmw)
640 struct ifmedia_description *desc;
642 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
643 if (IFM_TYPE(ifmw) == desc->ifmt_word)
649 static struct ifmedia_type_to_subtype *
650 get_toptype_ttos(int ifmw)
652 struct ifmedia_description *desc;
653 struct ifmedia_type_to_subtype *ttos;
655 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
656 desc->ifmt_string != NULL; desc++, ttos++)
657 if (IFM_TYPE(ifmw) == desc->ifmt_word)
663 static struct ifmedia_description *
664 get_subtype_desc(int ifmw, struct ifmedia_type_to_subtype *ttos)
667 struct ifmedia_description *desc;
669 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
670 if (ttos->subtypes[i].alias)
672 for (desc = ttos->subtypes[i].desc;
673 desc->ifmt_string != NULL; desc++) {
674 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
682 static struct ifmedia_description *
683 get_mode_desc(int ifmw, struct ifmedia_type_to_subtype *ttos)
686 struct ifmedia_description *desc;
688 for (i = 0; ttos->modes[i].desc != NULL; i++) {
689 if (ttos->modes[i].alias)
691 for (desc = ttos->modes[i].desc;
692 desc->ifmt_string != NULL; desc++) {
693 if (IFM_MODE(ifmw) == desc->ifmt_word)
702 print_media_word(int ifmw, int print_toptype)
704 struct ifmedia_description *desc;
705 struct ifmedia_type_to_subtype *ttos;
706 int seen_option = 0, i;
708 /* Find the top-level interface type. */
709 desc = get_toptype_desc(ifmw);
710 ttos = get_toptype_ttos(ifmw);
711 if (desc->ifmt_string == NULL) {
712 printf("<unknown type>");
714 } else if (print_toptype) {
715 printf("%s", desc->ifmt_string);
719 * Don't print the top-level type; it's not like we can
720 * change it, or anything.
724 desc = get_subtype_desc(ifmw, ttos);
728 /* Falling to here means unknown subtype. */
729 printf("<unknown subtype>");
736 printf("%s", desc->ifmt_string);
739 desc = get_mode_desc(ifmw, ttos);
740 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
741 printf(" mode %s", desc->ifmt_string);
745 for (i = 0; ttos->options[i].desc != NULL; i++) {
746 if (ttos->options[i].alias)
748 for (desc = ttos->options[i].desc;
749 desc->ifmt_string != NULL; desc++) {
750 if (ifmw & desc->ifmt_word) {
751 if (seen_option == 0)
753 printf("%s%s", seen_option++ ? "," : "",
758 printf("%s", seen_option ? ">" : "");
762 print_media_word_ifconfig(int ifmw)
764 struct ifmedia_description *desc;
765 struct ifmedia_type_to_subtype *ttos;
768 /* Find the top-level interface type. */
769 desc = get_toptype_desc(ifmw);
770 ttos = get_toptype_ttos(ifmw);
771 if (desc->ifmt_string == NULL) {
772 printf("<unknown type>");
777 * Don't print the top-level type; it's not like we can
778 * change it, or anything.
782 desc = get_subtype_desc(ifmw, ttos);
786 /* Falling to here means unknown subtype. */
787 printf("<unknown subtype>");
791 printf("media %s", desc->ifmt_string);
793 desc = get_mode_desc(ifmw, ttos);
795 printf(" mode %s", desc->ifmt_string);
798 for (i = 0; ttos->options[i].desc != NULL; i++) {
799 if (ttos->options[i].alias)
801 for (desc = ttos->options[i].desc;
802 desc->ifmt_string != NULL; desc++) {
803 if (ifmw & desc->ifmt_word) {
804 printf(" mediaopt %s", desc->ifmt_string);
810 /**********************************************************************
812 **********************************************************************/