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 $ */
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. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #include <sys/param.h>
66 #include <sys/ioctl.h>
67 #include <sys/socket.h>
68 #include <sys/sysctl.h>
72 #include <net/if_dl.h>
73 #include <net/if_types.h>
74 #include <net/if_media.h>
75 #include <net/route.h>
88 static void domediaopt(const char *, int, int);
89 static int get_media_subtype(int, const char *);
90 static int get_media_mode(int, const char *);
91 static int get_media_options(int, const char *);
92 static int lookup_media_word(struct ifmedia_description *, const char *);
93 static void print_media_word(int, int);
94 static void print_media_word_ifconfig(int);
96 static struct ifmedia_description *get_toptype_desc(int);
97 static struct ifmedia_type_to_subtype *get_toptype_ttos(int);
98 static struct ifmedia_description *get_subtype_desc(int,
99 struct ifmedia_type_to_subtype *ttos);
104 struct ifmediareq ifmr;
108 memset(&ifmr, 0, sizeof(ifmr));
109 strlcpy(ifmr.ifm_name, IfName, sizeof(ifmr.ifm_name));
112 * Check if interface supports extended media types.
114 if (ioctl(s, SIOCGIFXMEDIA, &ifmr) < 0)
116 if (xmedia == 0 && ioctl(s, SIOCGIFMEDIA, &ifmr) < 0) {
118 * Interface doesn't support SIOC{G,S}IFMEDIA.
123 if (ifmr.ifm_count == 0) {
124 warnx("%s: no media types?", IfName);
128 media_list = malloc(ifmr.ifm_count * sizeof(int));
129 if (media_list == NULL)
131 ifmr.ifm_ulist = media_list;
134 if (ioctl(s, SIOCGIFXMEDIA, &ifmr) < 0)
135 err(1, "SIOCGIFXMEDIA");
137 if (ioctl(s, SIOCGIFMEDIA, &ifmr) < 0)
138 err(1, "SIOCGIFMEDIA");
142 print_media_word(ifmr.ifm_current, 1);
143 if (ifmr.ifm_active != ifmr.ifm_current) {
146 print_media_word(ifmr.ifm_active, 0);
152 if (ifmr.ifm_status & IFM_AVALID) {
153 printf("\tstatus: ");
154 switch (IFM_TYPE(ifmr.ifm_active)) {
156 if (ifmr.ifm_status & IFM_ACTIVE)
159 printf("no carrier");
163 if (ifmr.ifm_status & IFM_ACTIVE)
166 printf("no carrier");
170 /* XXX: Different value for adhoc? */
171 if (ifmr.ifm_status & IFM_ACTIVE)
172 printf("associated");
174 printf("no carrier");
177 if (ifmr.ifm_status & IFM_ACTIVE)
186 if (ifmr.ifm_count > 0 && supmedia) {
187 printf("\tsupported media:\n");
188 for (i = 0; i < ifmr.ifm_count; i++) {
190 print_media_word_ifconfig(media_list[i]);
199 ifmedia_getstate(int s)
201 static struct ifmediareq *ifmr = NULL;
206 ifmr = malloc(sizeof(struct ifmediareq));
210 memset(ifmr, 0, sizeof(struct ifmediareq));
211 strlcpy(ifmr->ifm_name, IfName, sizeof(ifmr->ifm_name));
214 ifmr->ifm_ulist = NULL;
217 * We must go through the motions of reading all
218 * supported media because we need to know both
219 * the current media type and the top-level type.
222 if (ioctl(s, SIOCGIFXMEDIA, ifmr) < 0)
224 if (xmedia == 0 && ioctl(s, SIOCGIFMEDIA, ifmr) < 0)
225 err(1, "SIOCGIFMEDIA");
227 if (ifmr->ifm_count == 0)
228 errx(1, "%s: no media types?", IfName);
230 mwords = malloc(ifmr->ifm_count * sizeof(int));
234 ifmr->ifm_ulist = mwords;
236 if (ioctl(s, SIOCGIFXMEDIA, ifmr) < 0)
237 err(1, "SIOCGIFXMEDIA");
239 if (ioctl(s, SIOCGIFMEDIA, ifmr) < 0)
240 err(1, "SIOCGIFMEDIA");
248 setifmediacallback(int s, void *arg)
250 static int did_it = 0;
251 struct ifmediareq *ifmr = arg;
255 memset(&ifr, 0, sizeof(ifr));
256 strlcpy(ifr.ifr_name, IfName, sizeof(ifr.ifr_name));
257 ifr.ifr_media = ifmr->ifm_current;
258 if (ioctl(s, SIOCSIFMEDIA, &ifr) < 0)
259 err(1, "SIOCSIFMEDIA (media)");
260 free(ifmr->ifm_ulist);
267 setmedia(const char *val, int d __unused, int s,
268 const struct afswtch *afp __unused)
270 struct ifmediareq *ifmr;
273 ifmr = ifmedia_getstate(s);
276 * We are primarily concerned with the top-level type.
277 * However, "current" may be only IFM_NONE, so we just look
278 * for the top-level type in the first "supported type"
281 * (I'm assuming that all supported media types for a given
282 * interface will be the same top-level type..)
284 subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val);
286 ifmr->ifm_current = (ifmr->ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
287 IFM_TYPE(ifmr->ifm_ulist[0]) |
289 if ((ifmr->ifm_current & IFM_TMASK) == 0)
290 ifmr->ifm_current &= ~IFM_GMASK;
292 callback_register(setifmediacallback, ifmr);
296 setmediaopt(const char *val, int d __unused, int s,
297 const struct afswtch *afp __unused)
299 domediaopt(val, 0, s);
303 unsetmediaopt(const char *val, int d __unused, int s,
304 const struct afswtch *afp __unused)
306 domediaopt(val, 1, s);
310 domediaopt(const char *val, int clear, int s)
312 struct ifmediareq *ifmr;
315 ifmr = ifmedia_getstate(s);
317 options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
319 ifmr->ifm_current &= ~options;
321 ifmr->ifm_current |= options;
323 callback_register(setifmediacallback, ifmr);
328 setmediamode(const char *val, int d __unused, int s,
329 const struct afswtch *afp __unused)
331 struct ifmediareq *ifmr;
334 ifmr = ifmedia_getstate(s);
336 mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
337 ifmr->ifm_current = (ifmr->ifm_current & ~IFM_MMASK) | mode;
339 callback_register(setifmediacallback, ifmr);
342 /**********************************************************************
343 * A good chunk of this is duplicated from sys/net/ifmedia.c
344 **********************************************************************/
346 static struct ifmedia_description ifm_type_descriptions[] =
347 IFM_TYPE_DESCRIPTIONS;
349 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
350 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
352 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
353 IFM_SUBTYPE_ETHERNET_ALIASES;
355 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
356 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
358 static struct ifmedia_description ifm_subtype_ethernet_option_alias[] =
359 IFM_SUBTYPE_ETHERNET_OPTION_ALIAS;
361 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
362 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
364 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
365 IFM_SUBTYPE_IEEE80211_ALIASES;
367 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
368 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
370 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
371 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
373 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
374 IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
376 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
377 IFM_SUBTYPE_ATM_DESCRIPTIONS;
379 static struct ifmedia_description ifm_subtype_atm_aliases[] =
380 IFM_SUBTYPE_ATM_ALIASES;
382 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
383 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
385 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
386 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
388 static struct ifmedia_description ifm_subtype_shared_aliases[] =
389 IFM_SUBTYPE_SHARED_ALIASES;
391 static struct ifmedia_description ifm_shared_option_descriptions[] =
392 IFM_SHARED_OPTION_DESCRIPTIONS;
394 struct ifmedia_type_to_subtype {
396 struct ifmedia_description *desc;
400 struct ifmedia_description *desc;
404 struct ifmedia_description *desc;
409 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
410 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
413 { &ifm_subtype_shared_descriptions[0], 0 },
414 { &ifm_subtype_shared_aliases[0], 1 },
415 { &ifm_subtype_ethernet_descriptions[0], 0 },
416 { &ifm_subtype_ethernet_aliases[0], 1 },
420 { &ifm_shared_option_descriptions[0], 0 },
421 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
422 { &ifm_subtype_ethernet_option_alias[0], 1 },
431 { &ifm_subtype_shared_descriptions[0], 0 },
432 { &ifm_subtype_shared_aliases[0], 1 },
433 { &ifm_subtype_ieee80211_descriptions[0], 0 },
434 { &ifm_subtype_ieee80211_aliases[0], 1 },
438 { &ifm_shared_option_descriptions[0], 0 },
439 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
443 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
444 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
450 { &ifm_subtype_shared_descriptions[0], 0 },
451 { &ifm_subtype_shared_aliases[0], 1 },
452 { &ifm_subtype_atm_descriptions[0], 0 },
453 { &ifm_subtype_atm_aliases[0], 1 },
457 { &ifm_shared_option_descriptions[0], 0 },
458 { &ifm_subtype_atm_option_descriptions[0], 0 },
468 get_media_subtype(int type, const char *val)
470 struct ifmedia_description *desc;
471 struct ifmedia_type_to_subtype *ttos;
474 /* Find the top-level interface type. */
475 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
476 desc->ifmt_string != NULL; desc++, ttos++)
477 if (type == desc->ifmt_word)
479 if (desc->ifmt_string == NULL)
480 errx(1, "unknown media type 0x%x", type);
482 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
483 rval = lookup_media_word(ttos->subtypes[i].desc, val);
487 errx(1, "unknown media subtype: %s", val);
492 get_media_mode(int type, const char *val)
494 struct ifmedia_description *desc;
495 struct ifmedia_type_to_subtype *ttos;
498 /* Find the top-level interface type. */
499 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
500 desc->ifmt_string != NULL; desc++, ttos++)
501 if (type == desc->ifmt_word)
503 if (desc->ifmt_string == NULL)
504 errx(1, "unknown media mode 0x%x", type);
506 for (i = 0; ttos->modes[i].desc != NULL; i++) {
507 rval = lookup_media_word(ttos->modes[i].desc, val);
515 get_media_options(int type, const char *val)
517 struct ifmedia_description *desc;
518 struct ifmedia_type_to_subtype *ttos;
519 char *optlist, *optptr;
520 int option = 0, i, rval = 0;
522 /* We muck with the string, so copy it. */
523 optlist = strdup(val);
527 /* Find the top-level interface type. */
528 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
529 desc->ifmt_string != NULL; desc++, ttos++)
530 if (type == desc->ifmt_word)
532 if (desc->ifmt_string == NULL)
533 errx(1, "unknown media type 0x%x", type);
536 * Look up the options in the user-provided comma-separated
540 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
541 for (i = 0; ttos->options[i].desc != NULL; i++) {
542 option = lookup_media_word(ttos->options[i].desc, optptr);
547 errx(1, "unknown option: %s", optptr);
556 lookup_media_word(struct ifmedia_description *desc, const char *val)
559 for (; desc->ifmt_string != NULL; desc++)
560 if (strcasecmp(desc->ifmt_string, val) == 0)
561 return (desc->ifmt_word);
566 static struct ifmedia_description *get_toptype_desc(int ifmw)
568 struct ifmedia_description *desc;
570 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
571 if (IFM_TYPE(ifmw) == desc->ifmt_word)
577 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
579 struct ifmedia_description *desc;
580 struct ifmedia_type_to_subtype *ttos;
582 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
583 desc->ifmt_string != NULL; desc++, ttos++)
584 if (IFM_TYPE(ifmw) == desc->ifmt_word)
590 static struct ifmedia_description *get_subtype_desc(int ifmw,
591 struct ifmedia_type_to_subtype *ttos)
594 struct ifmedia_description *desc;
596 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
597 if (ttos->subtypes[i].alias)
599 for (desc = ttos->subtypes[i].desc;
600 desc->ifmt_string != NULL; desc++) {
601 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
609 static struct ifmedia_description *get_mode_desc(int ifmw,
610 struct ifmedia_type_to_subtype *ttos)
613 struct ifmedia_description *desc;
615 for (i = 0; ttos->modes[i].desc != NULL; i++) {
616 if (ttos->modes[i].alias)
618 for (desc = ttos->modes[i].desc;
619 desc->ifmt_string != NULL; desc++) {
620 if (IFM_MODE(ifmw) == desc->ifmt_word)
629 print_media_word(int ifmw, int print_toptype)
631 struct ifmedia_description *desc;
632 struct ifmedia_type_to_subtype *ttos;
633 int seen_option = 0, i;
635 /* Find the top-level interface type. */
636 desc = get_toptype_desc(ifmw);
637 ttos = get_toptype_ttos(ifmw);
638 if (desc->ifmt_string == NULL) {
639 printf("<unknown type>");
641 } else if (print_toptype) {
642 printf("%s", desc->ifmt_string);
646 * Don't print the top-level type; it's not like we can
647 * change it, or anything.
651 desc = get_subtype_desc(ifmw, ttos);
655 /* Falling to here means unknown subtype. */
656 printf("<unknown subtype>");
663 printf("%s", desc->ifmt_string);
666 desc = get_mode_desc(ifmw, ttos);
667 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
668 printf(" mode %s", desc->ifmt_string);
672 for (i = 0; ttos->options[i].desc != NULL; i++) {
673 if (ttos->options[i].alias)
675 for (desc = ttos->options[i].desc;
676 desc->ifmt_string != NULL; desc++) {
677 if (ifmw & desc->ifmt_word) {
678 if (seen_option == 0)
680 printf("%s%s", seen_option++ ? "," : "",
685 printf("%s", seen_option ? ">" : "");
689 print_media_word_ifconfig(int ifmw)
691 struct ifmedia_description *desc;
692 struct ifmedia_type_to_subtype *ttos;
695 /* Find the top-level interface type. */
696 desc = get_toptype_desc(ifmw);
697 ttos = get_toptype_ttos(ifmw);
698 if (desc->ifmt_string == NULL) {
699 printf("<unknown type>");
704 * Don't print the top-level type; it's not like we can
705 * change it, or anything.
709 desc = get_subtype_desc(ifmw, ttos);
713 /* Falling to here means unknown subtype. */
714 printf("<unknown subtype>");
718 printf("media %s", desc->ifmt_string);
720 desc = get_mode_desc(ifmw, ttos);
722 printf(" mode %s", desc->ifmt_string);
725 for (i = 0; ttos->options[i].desc != NULL; i++) {
726 if (ttos->options[i].alias)
728 for (desc = ttos->options[i].desc;
729 desc->ifmt_string != NULL; desc++) {
730 if (ifmw & desc->ifmt_word) {
731 printf(" mediaopt %s", desc->ifmt_string);
737 /**********************************************************************
739 **********************************************************************/
741 static struct cmd media_cmds[] = {
742 DEF_CMD_ARG("media", setmedia),
743 DEF_CMD_ARG("mode", setmediamode),
744 DEF_CMD_ARG("mediaopt", setmediaopt),
745 DEF_CMD_ARG("-mediaopt",unsetmediaopt),
747 static struct afswtch af_media = {
748 .af_name = "af_media",
750 .af_other_status = media_status,
759 for (i = 0; i < nitems(media_cmds); i++)
760 cmd_register(&media_cmds[i]);
762 af_register(&af_media);