1 /* $NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $ */
2 /* $FreeBSD: src/sbin/ifconfig/ifmedia.c,v 1.19.2.1 2006/03/01 22:24:23 glebius Exp $ */
3 /* $DragonFly: src/sbin/ifconfig/ifmedia.c,v 1.11 2007/08/16 20:03:55 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 static void domediaopt(const char *, int, int);
94 static int get_media_subtype(int, const char *);
95 static int get_media_mode(int, const char *);
96 static int get_media_options(int, const char *);
97 static int lookup_media_word(struct ifmedia_description *, const char *);
98 static void print_media_word(int, int);
99 static void print_media_word_ifconfig(int);
101 static struct ifmedia_description *get_toptype_desc(int);
102 static struct ifmedia_type_to_subtype *get_toptype_ttos(int);
103 static struct ifmedia_description *get_subtype_desc(int,
104 struct ifmedia_type_to_subtype *ttos);
109 struct ifmediareq ifmr;
112 memset(&ifmr, 0, sizeof(ifmr));
113 strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
115 if (ioctl(s, SIOCGIFMEDIA, &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 = malloc(ifmr.ifm_count * sizeof(int));
128 if (media_list == NULL)
130 ifmr.ifm_ulist = media_list;
132 if (ioctl(s, SIOCGIFMEDIA, &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)
165 if (ifmr.ifm_status & IFM_ACTIVE)
168 printf("no carrier");
172 /* XXX: Different value for adhoc? */
173 if (ifmr.ifm_status & IFM_ACTIVE)
174 printf("associated");
176 printf("no carrier");
179 if (ifmr.ifm_status & IFM_ACTIVE)
188 if (ifmr.ifm_count > 0 && supmedia) {
189 printf("\tsupported media:\n");
190 for (i = 0; i < ifmr.ifm_count; i++) {
192 print_media_word_ifconfig(media_list[i]);
200 static struct ifmediareq *
201 getifmediastate(int s)
203 static struct ifmediareq *ifmr = NULL;
207 ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq));
211 (void) memset(ifmr, 0, sizeof(struct ifmediareq));
212 (void) strncpy(ifmr->ifm_name, name,
213 sizeof(ifmr->ifm_name));
216 ifmr->ifm_ulist = NULL;
219 * We must go through the motions of reading all
220 * supported media because we need to know both
221 * the current media type and the top-level type.
224 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) {
225 err(1, "SIOCGIFMEDIA");
228 if (ifmr->ifm_count == 0)
229 errx(1, "%s: no media types?", name);
231 mwords = (int *)malloc(ifmr->ifm_count * sizeof(int));
235 ifmr->ifm_ulist = mwords;
236 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0)
237 err(1, "SIOCGIFMEDIA");
244 setifmediacallback(int s, void *arg)
246 struct ifmediareq *ifmr = (struct ifmediareq *)arg;
247 static int did_it = 0;
250 ifr.ifr_media = ifmr->ifm_current;
251 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
252 err(1, "SIOCSIFMEDIA (media)");
253 free(ifmr->ifm_ulist);
260 setmedia(const char *val, int d, int s, const struct afswtch *afp)
262 struct ifmediareq *ifmr;
266 ifmr = getifmediastate(s);
269 * We are primarily concerned with the top-level type.
270 * However, "current" may be only IFM_NONE, so we just look
271 * for the top-level type in the first "supported type"
274 * (I'm assuming that all supported media types for a given
275 * interface will be the same top-level type..)
277 subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val);
279 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
280 ifr.ifr_media = (ifmr->ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
281 IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
283 if ((ifr.ifr_media & IFM_TMASK) == 0) {
284 ifr.ifr_media &= ~IFM_GMASK;
287 ifmr->ifm_current = ifr.ifr_media;
288 callback_register(setifmediacallback, (void *)ifmr);
292 setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
295 domediaopt(val, 0, s);
299 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
302 domediaopt(val, 1, s);
306 domediaopt(const char *val, int clear, int s)
308 struct ifmediareq *ifmr;
311 ifmr = getifmediastate(s);
313 options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
315 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
316 ifr.ifr_media = ifmr->ifm_current;
318 ifr.ifr_media &= ~options;
320 ifr.ifr_media |= options;
322 ifmr->ifm_current = ifr.ifr_media;
323 callback_register(setifmediacallback, (void *)ifmr);
328 setmediamode(const char *val, int d, int s, const struct afswtch *afp)
330 struct ifmediareq *ifmr;
333 ifmr = getifmediastate(s);
335 mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
337 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
338 ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode;
340 ifmr->ifm_current = ifr.ifr_media;
341 callback_register(setifmediacallback, (void *)ifmr);
344 /**********************************************************************
345 * A good chunk of this is duplicated from sys/net/ifmedia.c
346 **********************************************************************/
348 static struct ifmedia_description ifm_type_descriptions[] =
349 IFM_TYPE_DESCRIPTIONS;
351 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
352 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
354 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
355 IFM_SUBTYPE_ETHERNET_ALIASES;
357 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
358 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
360 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
361 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
363 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
364 IFM_SUBTYPE_TOKENRING_ALIASES;
366 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
367 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
369 static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
370 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
372 static struct ifmedia_description ifm_subtype_fddi_aliases[] =
373 IFM_SUBTYPE_FDDI_ALIASES;
375 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
376 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
378 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
379 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
381 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
382 IFM_SUBTYPE_IEEE80211_ALIASES;
384 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
385 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
387 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
388 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
390 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
391 IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
393 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
394 IFM_SUBTYPE_ATM_DESCRIPTIONS;
396 static struct ifmedia_description ifm_subtype_atm_aliases[] =
397 IFM_SUBTYPE_ATM_ALIASES;
399 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
400 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
402 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
403 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
405 static struct ifmedia_description ifm_subtype_shared_aliases[] =
406 IFM_SUBTYPE_SHARED_ALIASES;
408 static struct ifmedia_description ifm_shared_option_descriptions[] =
409 IFM_SHARED_OPTION_DESCRIPTIONS;
411 struct ifmedia_type_to_subtype {
413 struct ifmedia_description *desc;
417 struct ifmedia_description *desc;
421 struct ifmedia_description *desc;
426 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
427 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
430 { &ifm_subtype_shared_descriptions[0], 0 },
431 { &ifm_subtype_shared_aliases[0], 1 },
432 { &ifm_subtype_ethernet_descriptions[0], 0 },
433 { &ifm_subtype_ethernet_aliases[0], 1 },
437 { &ifm_shared_option_descriptions[0], 0 },
438 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
447 { &ifm_subtype_shared_descriptions[0], 0 },
448 { &ifm_subtype_shared_aliases[0], 1 },
449 { &ifm_subtype_tokenring_descriptions[0], 0 },
450 { &ifm_subtype_tokenring_aliases[0], 1 },
454 { &ifm_shared_option_descriptions[0], 0 },
455 { &ifm_subtype_tokenring_option_descriptions[0], 0 },
464 { &ifm_subtype_shared_descriptions[0], 0 },
465 { &ifm_subtype_shared_aliases[0], 1 },
466 { &ifm_subtype_fddi_descriptions[0], 0 },
467 { &ifm_subtype_fddi_aliases[0], 1 },
471 { &ifm_shared_option_descriptions[0], 0 },
472 { &ifm_subtype_fddi_option_descriptions[0], 0 },
481 { &ifm_subtype_shared_descriptions[0], 0 },
482 { &ifm_subtype_shared_aliases[0], 1 },
483 { &ifm_subtype_ieee80211_descriptions[0], 0 },
484 { &ifm_subtype_ieee80211_aliases[0], 1 },
488 { &ifm_shared_option_descriptions[0], 0 },
489 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
493 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
494 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
500 { &ifm_subtype_shared_descriptions[0], 0 },
501 { &ifm_subtype_shared_aliases[0], 1 },
502 { &ifm_subtype_atm_descriptions[0], 0 },
503 { &ifm_subtype_atm_aliases[0], 1 },
507 { &ifm_shared_option_descriptions[0], 0 },
508 { &ifm_subtype_atm_option_descriptions[0], 0 },
518 get_media_subtype(int type, const char *val)
520 struct ifmedia_description *desc;
521 struct ifmedia_type_to_subtype *ttos;
524 /* Find the top-level interface type. */
525 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
526 desc->ifmt_string != NULL; desc++, ttos++)
527 if (type == desc->ifmt_word)
529 if (desc->ifmt_string == NULL)
530 errx(1, "unknown media type 0x%x", type);
532 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
533 rval = lookup_media_word(ttos->subtypes[i].desc, val);
537 errx(1, "unknown media subtype: %s", val);
542 get_media_mode(int type, const char *val)
544 struct ifmedia_description *desc;
545 struct ifmedia_type_to_subtype *ttos;
548 /* Find the top-level interface type. */
549 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
550 desc->ifmt_string != NULL; desc++, ttos++)
551 if (type == desc->ifmt_word)
553 if (desc->ifmt_string == NULL)
554 errx(1, "unknown media mode 0x%x", type);
556 for (i = 0; ttos->modes[i].desc != NULL; i++) {
557 rval = lookup_media_word(ttos->modes[i].desc, val);
565 get_media_options(int type, const char *val)
567 struct ifmedia_description *desc;
568 struct ifmedia_type_to_subtype *ttos;
569 char *optlist, *optptr;
570 int option = 0, i, rval = 0;
572 /* We muck with the string, so copy it. */
573 optlist = strdup(val);
577 /* Find the top-level interface type. */
578 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
579 desc->ifmt_string != NULL; desc++, ttos++)
580 if (type == desc->ifmt_word)
582 if (desc->ifmt_string == NULL)
583 errx(1, "unknown media type 0x%x", type);
586 * Look up the options in the user-provided comma-separated
590 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
591 for (i = 0; ttos->options[i].desc != NULL; i++) {
592 option = lookup_media_word(ttos->options[i].desc, optptr);
597 errx(1, "unknown option: %s", optptr);
606 lookup_media_word(struct ifmedia_description *desc, const char *val)
609 for (; desc->ifmt_string != NULL; desc++)
610 if (strcasecmp(desc->ifmt_string, val) == 0)
611 return (desc->ifmt_word);
616 static struct ifmedia_description *get_toptype_desc(int ifmw)
618 struct ifmedia_description *desc;
620 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
621 if (IFM_TYPE(ifmw) == desc->ifmt_word)
627 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
629 struct ifmedia_description *desc;
630 struct ifmedia_type_to_subtype *ttos;
632 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
633 desc->ifmt_string != NULL; desc++, ttos++)
634 if (IFM_TYPE(ifmw) == desc->ifmt_word)
640 static struct ifmedia_description *get_subtype_desc(int ifmw,
641 struct ifmedia_type_to_subtype *ttos)
644 struct ifmedia_description *desc;
646 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
647 if (ttos->subtypes[i].alias)
649 for (desc = ttos->subtypes[i].desc;
650 desc->ifmt_string != NULL; desc++) {
651 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
659 static struct ifmedia_description *get_mode_desc(int ifmw,
660 struct ifmedia_type_to_subtype *ttos)
663 struct ifmedia_description *desc;
665 for (i = 0; ttos->modes[i].desc != NULL; i++) {
666 if (ttos->modes[i].alias)
668 for (desc = ttos->modes[i].desc;
669 desc->ifmt_string != NULL; desc++) {
670 if (IFM_MODE(ifmw) == desc->ifmt_word)
679 print_media_word(int ifmw, int print_toptype)
681 struct ifmedia_description *desc;
682 struct ifmedia_type_to_subtype *ttos;
683 int seen_option = 0, i;
685 /* Find the top-level interface type. */
686 desc = get_toptype_desc(ifmw);
687 ttos = get_toptype_ttos(ifmw);
688 if (desc->ifmt_string == NULL) {
689 printf("<unknown type>");
691 } else if (print_toptype) {
692 printf("%s", desc->ifmt_string);
696 * Don't print the top-level type; it's not like we can
697 * change it, or anything.
701 desc = get_subtype_desc(ifmw, ttos);
705 /* Falling to here means unknown subtype. */
706 printf("<unknown subtype>");
713 printf("%s", desc->ifmt_string);
716 desc = get_mode_desc(ifmw, ttos);
717 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
718 printf(" mode %s", desc->ifmt_string);
722 for (i = 0; ttos->options[i].desc != NULL; i++) {
723 if (ttos->options[i].alias)
725 for (desc = ttos->options[i].desc;
726 desc->ifmt_string != NULL; desc++) {
727 if (ifmw & desc->ifmt_word) {
728 if (seen_option == 0)
730 printf("%s%s", seen_option++ ? "," : "",
735 printf("%s", seen_option ? ">" : "");
739 print_media_word_ifconfig(int ifmw)
741 struct ifmedia_description *desc;
742 struct ifmedia_type_to_subtype *ttos;
745 /* Find the top-level interface type. */
746 desc = get_toptype_desc(ifmw);
747 ttos = get_toptype_ttos(ifmw);
748 if (desc->ifmt_string == NULL) {
749 printf("<unknown type>");
754 * Don't print the top-level type; it's not like we can
755 * change it, or anything.
759 desc = get_subtype_desc(ifmw, ttos);
763 /* Falling to here means unknown subtype. */
764 printf("<unknown subtype>");
768 printf("media %s", desc->ifmt_string);
770 desc = get_mode_desc(ifmw, ttos);
772 printf(" mode %s", desc->ifmt_string);
775 for (i = 0; ttos->options[i].desc != NULL; i++) {
776 if (ttos->options[i].alias)
778 for (desc = ttos->options[i].desc;
779 desc->ifmt_string != NULL; desc++) {
780 if (ifmw & desc->ifmt_word) {
781 printf(" mediaopt %s", desc->ifmt_string);
787 /**********************************************************************
789 **********************************************************************/
791 static struct cmd media_cmds[] = {
792 DEF_CMD_ARG("media", setmedia),
793 DEF_CMD_ARG("mode", setmediamode),
794 DEF_CMD_ARG("mediaopt", setmediaopt),
795 DEF_CMD_ARG("-mediaopt",unsetmediaopt),
797 static struct afswtch af_media = {
798 .af_name = "af_media",
800 .af_other_status = media_status,
803 static __constructor void
806 #define N(a) (sizeof(a) / sizeof(a[0]))
809 for (i = 0; i < N(media_cmds); i++)
810 cmd_register(&media_cmds[i]);
811 af_register(&af_media);