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, name, sizeof(ifmr.ifm_name));
112 * Check if interface supports extended media types.
114 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)&ifmr) < 0)
116 if (xmedia == 0 && ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
118 * Interface doesn't support SIOC{G,S}IFMEDIA.
123 if (ifmr.ifm_count == 0) {
124 warnx("%s: no media types?", name);
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, (caddr_t)&ifmr) < 0)
135 err(1, "SIOCGIFXMEDIA");
137 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&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 = (struct ifmediareq *)malloc(sizeof(struct ifmediareq));
210 memset(ifmr, 0, sizeof(struct ifmediareq));
211 strlcpy(ifmr->ifm_name, name, 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, (caddr_t)ifmr) < 0)
224 if (xmedia == 0 && ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0)
225 err(1, "SIOCGIFMEDIA");
227 if (ifmr->ifm_count == 0)
228 errx(1, "%s: no media types?", name);
230 mwords = (int *)malloc(ifmr->ifm_count * sizeof(int));
234 ifmr->ifm_ulist = mwords;
236 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)ifmr) < 0)
237 err(1, "SIOCGIFXMEDIA");
239 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0)
240 err(1, "SIOCGIFMEDIA");
248 setifmediacallback(int s, void *arg)
250 struct ifmediareq *ifmr = (struct ifmediareq *)arg;
251 static int did_it = 0;
254 ifr.ifr_media = ifmr->ifm_current;
255 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
256 err(1, "SIOCSIFMEDIA (media)");
257 free(ifmr->ifm_ulist);
264 setmedia(const char *val, int d, int s, const struct afswtch *afp)
266 struct ifmediareq *ifmr;
269 ifmr = ifmedia_getstate(s);
272 * We are primarily concerned with the top-level type.
273 * However, "current" may be only IFM_NONE, so we just look
274 * for the top-level type in the first "supported type"
277 * (I'm assuming that all supported media types for a given
278 * interface will be the same top-level type..)
280 subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val);
282 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
283 ifr.ifr_media = (ifmr->ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
284 IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
286 if ((ifr.ifr_media & IFM_TMASK) == 0) {
287 ifr.ifr_media &= ~IFM_GMASK;
290 ifmr->ifm_current = ifr.ifr_media;
291 callback_register(setifmediacallback, (void *)ifmr);
295 setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
298 domediaopt(val, 0, s);
302 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
305 domediaopt(val, 1, s);
309 domediaopt(const char *val, int clear, int s)
311 struct ifmediareq *ifmr;
314 ifmr = ifmedia_getstate(s);
316 options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
318 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
319 ifr.ifr_media = ifmr->ifm_current;
321 ifr.ifr_media &= ~options;
323 ifr.ifr_media |= options;
325 ifmr->ifm_current = ifr.ifr_media;
326 callback_register(setifmediacallback, (void *)ifmr);
331 setmediamode(const char *val, int d, int s, const struct afswtch *afp)
333 struct ifmediareq *ifmr;
336 ifmr = ifmedia_getstate(s);
338 mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
340 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
341 ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode;
343 ifmr->ifm_current = ifr.ifr_media;
344 callback_register(setifmediacallback, (void *)ifmr);
347 /**********************************************************************
348 * A good chunk of this is duplicated from sys/net/ifmedia.c
349 **********************************************************************/
351 static struct ifmedia_description ifm_type_descriptions[] =
352 IFM_TYPE_DESCRIPTIONS;
354 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
355 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
357 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
358 IFM_SUBTYPE_ETHERNET_ALIASES;
360 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
361 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
363 static struct ifmedia_description ifm_subtype_ethernet_option_alias[] =
364 IFM_SUBTYPE_ETHERNET_OPTION_ALIAS;
366 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
367 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
369 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
370 IFM_SUBTYPE_IEEE80211_ALIASES;
372 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
373 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
375 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
376 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
378 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
379 IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
381 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
382 IFM_SUBTYPE_ATM_DESCRIPTIONS;
384 static struct ifmedia_description ifm_subtype_atm_aliases[] =
385 IFM_SUBTYPE_ATM_ALIASES;
387 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
388 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
390 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
391 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
393 static struct ifmedia_description ifm_subtype_shared_aliases[] =
394 IFM_SUBTYPE_SHARED_ALIASES;
396 static struct ifmedia_description ifm_shared_option_descriptions[] =
397 IFM_SHARED_OPTION_DESCRIPTIONS;
399 struct ifmedia_type_to_subtype {
401 struct ifmedia_description *desc;
405 struct ifmedia_description *desc;
409 struct ifmedia_description *desc;
414 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
415 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
418 { &ifm_subtype_shared_descriptions[0], 0 },
419 { &ifm_subtype_shared_aliases[0], 1 },
420 { &ifm_subtype_ethernet_descriptions[0], 0 },
421 { &ifm_subtype_ethernet_aliases[0], 1 },
425 { &ifm_shared_option_descriptions[0], 0 },
426 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
427 { &ifm_subtype_ethernet_option_alias[0], 1 },
436 { &ifm_subtype_shared_descriptions[0], 0 },
437 { &ifm_subtype_shared_aliases[0], 1 },
438 { &ifm_subtype_ieee80211_descriptions[0], 0 },
439 { &ifm_subtype_ieee80211_aliases[0], 1 },
443 { &ifm_shared_option_descriptions[0], 0 },
444 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
448 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
449 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
455 { &ifm_subtype_shared_descriptions[0], 0 },
456 { &ifm_subtype_shared_aliases[0], 1 },
457 { &ifm_subtype_atm_descriptions[0], 0 },
458 { &ifm_subtype_atm_aliases[0], 1 },
462 { &ifm_shared_option_descriptions[0], 0 },
463 { &ifm_subtype_atm_option_descriptions[0], 0 },
473 get_media_subtype(int type, const char *val)
475 struct ifmedia_description *desc;
476 struct ifmedia_type_to_subtype *ttos;
479 /* Find the top-level interface type. */
480 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
481 desc->ifmt_string != NULL; desc++, ttos++)
482 if (type == desc->ifmt_word)
484 if (desc->ifmt_string == NULL)
485 errx(1, "unknown media type 0x%x", type);
487 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
488 rval = lookup_media_word(ttos->subtypes[i].desc, val);
492 errx(1, "unknown media subtype: %s", val);
497 get_media_mode(int type, const char *val)
499 struct ifmedia_description *desc;
500 struct ifmedia_type_to_subtype *ttos;
503 /* Find the top-level interface type. */
504 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
505 desc->ifmt_string != NULL; desc++, ttos++)
506 if (type == desc->ifmt_word)
508 if (desc->ifmt_string == NULL)
509 errx(1, "unknown media mode 0x%x", type);
511 for (i = 0; ttos->modes[i].desc != NULL; i++) {
512 rval = lookup_media_word(ttos->modes[i].desc, val);
520 get_media_options(int type, const char *val)
522 struct ifmedia_description *desc;
523 struct ifmedia_type_to_subtype *ttos;
524 char *optlist, *optptr;
525 int option = 0, i, rval = 0;
527 /* We muck with the string, so copy it. */
528 optlist = strdup(val);
532 /* Find the top-level interface type. */
533 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
534 desc->ifmt_string != NULL; desc++, ttos++)
535 if (type == desc->ifmt_word)
537 if (desc->ifmt_string == NULL)
538 errx(1, "unknown media type 0x%x", type);
541 * Look up the options in the user-provided comma-separated
545 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
546 for (i = 0; ttos->options[i].desc != NULL; i++) {
547 option = lookup_media_word(ttos->options[i].desc, optptr);
552 errx(1, "unknown option: %s", optptr);
561 lookup_media_word(struct ifmedia_description *desc, const char *val)
564 for (; desc->ifmt_string != NULL; desc++)
565 if (strcasecmp(desc->ifmt_string, val) == 0)
566 return (desc->ifmt_word);
571 static struct ifmedia_description *get_toptype_desc(int ifmw)
573 struct ifmedia_description *desc;
575 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
576 if (IFM_TYPE(ifmw) == desc->ifmt_word)
582 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
584 struct ifmedia_description *desc;
585 struct ifmedia_type_to_subtype *ttos;
587 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
588 desc->ifmt_string != NULL; desc++, ttos++)
589 if (IFM_TYPE(ifmw) == desc->ifmt_word)
595 static struct ifmedia_description *get_subtype_desc(int ifmw,
596 struct ifmedia_type_to_subtype *ttos)
599 struct ifmedia_description *desc;
601 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
602 if (ttos->subtypes[i].alias)
604 for (desc = ttos->subtypes[i].desc;
605 desc->ifmt_string != NULL; desc++) {
606 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
614 static struct ifmedia_description *get_mode_desc(int ifmw,
615 struct ifmedia_type_to_subtype *ttos)
618 struct ifmedia_description *desc;
620 for (i = 0; ttos->modes[i].desc != NULL; i++) {
621 if (ttos->modes[i].alias)
623 for (desc = ttos->modes[i].desc;
624 desc->ifmt_string != NULL; desc++) {
625 if (IFM_MODE(ifmw) == desc->ifmt_word)
634 print_media_word(int ifmw, int print_toptype)
636 struct ifmedia_description *desc;
637 struct ifmedia_type_to_subtype *ttos;
638 int seen_option = 0, i;
640 /* Find the top-level interface type. */
641 desc = get_toptype_desc(ifmw);
642 ttos = get_toptype_ttos(ifmw);
643 if (desc->ifmt_string == NULL) {
644 printf("<unknown type>");
646 } else if (print_toptype) {
647 printf("%s", desc->ifmt_string);
651 * Don't print the top-level type; it's not like we can
652 * change it, or anything.
656 desc = get_subtype_desc(ifmw, ttos);
660 /* Falling to here means unknown subtype. */
661 printf("<unknown subtype>");
668 printf("%s", desc->ifmt_string);
671 desc = get_mode_desc(ifmw, ttos);
672 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
673 printf(" mode %s", desc->ifmt_string);
677 for (i = 0; ttos->options[i].desc != NULL; i++) {
678 if (ttos->options[i].alias)
680 for (desc = ttos->options[i].desc;
681 desc->ifmt_string != NULL; desc++) {
682 if (ifmw & desc->ifmt_word) {
683 if (seen_option == 0)
685 printf("%s%s", seen_option++ ? "," : "",
690 printf("%s", seen_option ? ">" : "");
694 print_media_word_ifconfig(int ifmw)
696 struct ifmedia_description *desc;
697 struct ifmedia_type_to_subtype *ttos;
700 /* Find the top-level interface type. */
701 desc = get_toptype_desc(ifmw);
702 ttos = get_toptype_ttos(ifmw);
703 if (desc->ifmt_string == NULL) {
704 printf("<unknown type>");
709 * Don't print the top-level type; it's not like we can
710 * change it, or anything.
714 desc = get_subtype_desc(ifmw, ttos);
718 /* Falling to here means unknown subtype. */
719 printf("<unknown subtype>");
723 printf("media %s", desc->ifmt_string);
725 desc = get_mode_desc(ifmw, ttos);
727 printf(" mode %s", desc->ifmt_string);
730 for (i = 0; ttos->options[i].desc != NULL; i++) {
731 if (ttos->options[i].alias)
733 for (desc = ttos->options[i].desc;
734 desc->ifmt_string != NULL; desc++) {
735 if (ifmw & desc->ifmt_word) {
736 printf(" mediaopt %s", desc->ifmt_string);
742 /**********************************************************************
744 **********************************************************************/
746 static struct cmd media_cmds[] = {
747 DEF_CMD_ARG("media", setmedia),
748 DEF_CMD_ARG("mode", setmediamode),
749 DEF_CMD_ARG("mediaopt", setmediaopt),
750 DEF_CMD_ARG("-mediaopt",unsetmediaopt),
752 static struct afswtch af_media = {
753 .af_name = "af_media",
755 .af_other_status = media_status,
758 static __constructor(101) void
763 for (i = 0; i < nitems(media_cmds); i++)
764 cmd_register(&media_cmds[i]);
765 af_register(&af_media);