1 /* $NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
7 * Copyright (c) 1997 Jason R. Thorpe.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed for the NetBSD Project
22 * 4. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * Copyright (c) 1983, 1993
40 * The Regents of the University of California. All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 #include <sys/param.h>
68 #include <sys/ioctl.h>
69 #include <sys/socket.h>
70 #include <sys/sysctl.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <net/if_media.h>
77 #include <net/route.h>
91 static void domediaopt(const char *, int, int);
92 static int get_media_subtype(int, const char *);
93 static int get_media_mode(int, const char *);
94 static int get_media_options(int, const char *);
95 static int lookup_media_word(struct ifmedia_description *, const char *);
96 static void print_media_word(int, int);
97 static void print_media_word_ifconfig(int);
99 static struct ifmedia_description *get_toptype_desc(int);
100 static struct ifmedia_type_to_subtype *get_toptype_ttos(int);
101 static struct ifmedia_description *get_subtype_desc(int,
102 struct ifmedia_type_to_subtype *ttos);
104 #define IFM_OPMODE(x) \
105 ((x) & (IFM_IEEE80211_ADHOC | IFM_IEEE80211_HOSTAP | \
106 IFM_IEEE80211_IBSS | IFM_IEEE80211_WDS | IFM_IEEE80211_MONITOR | \
108 #define IFM_IEEE80211_STA 0
113 struct ifmediareq ifmr;
114 struct ifdownreason ifdr;
116 bool no_carrier, xmedia;
118 (void) memset(&ifmr, 0, sizeof(ifmr));
119 (void) strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
123 * Check if interface supports extended media types.
125 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)&ifmr) < 0)
127 if (!xmedia && ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
129 * Interface doesn't support SIOC{G,S}IFMEDIA.
134 if (ifmr.ifm_count == 0) {
135 warnx("%s: no media types?", name);
139 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
140 if (media_list == NULL)
142 ifmr.ifm_ulist = media_list;
145 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)&ifmr) < 0)
146 err(1, "SIOCGIFXMEDIA");
148 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
149 err(1, "SIOCGIFMEDIA");
153 print_media_word(ifmr.ifm_current, 1);
154 if (ifmr.ifm_active != ifmr.ifm_current) {
157 print_media_word(ifmr.ifm_active, 0);
163 if (ifmr.ifm_status & IFM_AVALID) {
165 printf("\tstatus: ");
166 switch (IFM_TYPE(ifmr.ifm_active)) {
169 if (ifmr.ifm_status & IFM_ACTIVE)
176 if (ifmr.ifm_status & IFM_ACTIVE) {
177 /* NB: only sta mode associates */
178 if (IFM_OPMODE(ifmr.ifm_active) == IFM_IEEE80211_STA)
179 printf("associated");
187 printf("no carrier");
188 memset(&ifdr, 0, sizeof(ifdr));
189 strlcpy(ifdr.ifdr_name, name, sizeof(ifdr.ifdr_name));
190 if (ioctl(s, SIOCGIFDOWNREASON, (caddr_t)&ifdr) == 0) {
191 switch (ifdr.ifdr_reason) {
192 case IFDR_REASON_MSG:
193 printf(" (%s)", ifdr.ifdr_msg);
195 case IFDR_REASON_VENDOR:
196 printf(" (vendor code %d)",
207 if (ifmr.ifm_count > 0 && supmedia) {
208 printf("\tsupported media:\n");
209 for (i = 0; i < ifmr.ifm_count; i++) {
211 print_media_word_ifconfig(media_list[i]);
220 ifmedia_getstate(int s)
222 static struct ifmediareq *ifmr = NULL;
227 ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq));
231 (void) memset(ifmr, 0, sizeof(struct ifmediareq));
232 (void) strlcpy(ifmr->ifm_name, name,
233 sizeof(ifmr->ifm_name));
236 ifmr->ifm_ulist = NULL;
239 * We must go through the motions of reading all
240 * supported media because we need to know both
241 * the current media type and the top-level type.
244 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)ifmr) < 0) {
247 if (xmedia == 0 && ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) {
248 err(1, "SIOCGIFMEDIA");
251 if (ifmr->ifm_count == 0)
252 errx(1, "%s: no media types?", name);
254 mwords = (int *)malloc(ifmr->ifm_count * sizeof(int));
258 ifmr->ifm_ulist = mwords;
260 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)ifmr) < 0)
261 err(1, "SIOCGIFXMEDIA");
263 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0)
264 err(1, "SIOCGIFMEDIA");
272 setifmediacallback(int s, void *arg)
274 struct ifmediareq *ifmr = (struct ifmediareq *)arg;
275 static int did_it = 0;
278 ifr.ifr_media = ifmr->ifm_current;
279 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
280 err(1, "SIOCSIFMEDIA (media)");
281 free(ifmr->ifm_ulist);
288 setmedia(const char *val, int d, int s, const struct afswtch *afp)
290 struct ifmediareq *ifmr;
293 ifmr = ifmedia_getstate(s);
296 * We are primarily concerned with the top-level type.
297 * However, "current" may be only IFM_NONE, so we just look
298 * for the top-level type in the first "supported type"
301 * (I'm assuming that all supported media types for a given
302 * interface will be the same top-level type..)
304 subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val);
306 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
307 ifr.ifr_media = (ifmr->ifm_current & IFM_IMASK) |
308 IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
310 ifmr->ifm_current = ifr.ifr_media;
311 callback_register(setifmediacallback, (void *)ifmr);
315 setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
318 domediaopt(val, 0, s);
322 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
325 domediaopt(val, 1, s);
329 domediaopt(const char *val, int clear, int s)
331 struct ifmediareq *ifmr;
334 ifmr = ifmedia_getstate(s);
336 options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
338 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
339 ifr.ifr_media = ifmr->ifm_current;
341 ifr.ifr_media &= ~options;
343 if (options & IFM_HDX) {
344 ifr.ifr_media &= ~IFM_FDX;
347 ifr.ifr_media |= options;
349 ifmr->ifm_current = ifr.ifr_media;
350 callback_register(setifmediacallback, (void *)ifmr);
354 setmediainst(const char *val, int d, int s, const struct afswtch *afp)
356 struct ifmediareq *ifmr;
359 ifmr = ifmedia_getstate(s);
362 if (inst < 0 || inst > (int)IFM_INST_MAX)
363 errx(1, "invalid media instance: %s", val);
365 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
366 ifr.ifr_media = (ifmr->ifm_current & ~IFM_IMASK) | inst << IFM_ISHIFT;
368 ifmr->ifm_current = ifr.ifr_media;
369 callback_register(setifmediacallback, (void *)ifmr);
373 setmediamode(const char *val, int d, int s, const struct afswtch *afp)
375 struct ifmediareq *ifmr;
378 ifmr = ifmedia_getstate(s);
380 mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
382 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
383 ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode;
385 ifmr->ifm_current = ifr.ifr_media;
386 callback_register(setifmediacallback, (void *)ifmr);
389 /**********************************************************************
390 * A good chunk of this is duplicated from sys/net/if_media.c
391 **********************************************************************/
393 static struct ifmedia_description ifm_type_descriptions[] =
394 IFM_TYPE_DESCRIPTIONS;
396 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
397 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
399 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
400 IFM_SUBTYPE_ETHERNET_ALIASES;
402 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
403 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
405 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
406 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
408 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
409 IFM_SUBTYPE_IEEE80211_ALIASES;
411 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
412 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
414 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
415 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
417 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
418 IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
420 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
421 IFM_SUBTYPE_ATM_DESCRIPTIONS;
423 static struct ifmedia_description ifm_subtype_atm_aliases[] =
424 IFM_SUBTYPE_ATM_ALIASES;
426 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
427 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
429 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
430 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
432 static struct ifmedia_description ifm_subtype_shared_aliases[] =
433 IFM_SUBTYPE_SHARED_ALIASES;
435 static struct ifmedia_description ifm_shared_option_descriptions[] =
436 IFM_SHARED_OPTION_DESCRIPTIONS;
438 static struct ifmedia_description ifm_shared_option_aliases[] =
439 IFM_SHARED_OPTION_ALIASES;
441 struct ifmedia_type_to_subtype {
443 struct ifmedia_description *desc;
447 struct ifmedia_description *desc;
451 struct ifmedia_description *desc;
456 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
457 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
460 { &ifm_subtype_shared_descriptions[0], 0 },
461 { &ifm_subtype_shared_aliases[0], 1 },
462 { &ifm_subtype_ethernet_descriptions[0], 0 },
463 { &ifm_subtype_ethernet_aliases[0], 1 },
467 { &ifm_shared_option_descriptions[0], 0 },
468 { &ifm_shared_option_aliases[0], 1 },
469 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
478 { &ifm_subtype_shared_descriptions[0], 0 },
479 { &ifm_subtype_shared_aliases[0], 1 },
480 { &ifm_subtype_ieee80211_descriptions[0], 0 },
481 { &ifm_subtype_ieee80211_aliases[0], 1 },
485 { &ifm_shared_option_descriptions[0], 0 },
486 { &ifm_shared_option_aliases[0], 1 },
487 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
491 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
492 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
498 { &ifm_subtype_shared_descriptions[0], 0 },
499 { &ifm_subtype_shared_aliases[0], 1 },
500 { &ifm_subtype_atm_descriptions[0], 0 },
501 { &ifm_subtype_atm_aliases[0], 1 },
505 { &ifm_shared_option_descriptions[0], 0 },
506 { &ifm_shared_option_aliases[0], 1 },
507 { &ifm_subtype_atm_option_descriptions[0], 0 },
517 get_media_subtype(int type, const char *val)
519 struct ifmedia_description *desc;
520 struct ifmedia_type_to_subtype *ttos;
523 /* Find the top-level interface type. */
524 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
525 desc->ifmt_string != NULL; desc++, ttos++)
526 if (type == desc->ifmt_word)
528 if (desc->ifmt_string == NULL)
529 errx(1, "unknown media type 0x%x", type);
531 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
532 rval = lookup_media_word(ttos->subtypes[i].desc, val);
536 errx(1, "unknown media subtype: %s", val);
541 get_media_mode(int type, const char *val)
543 struct ifmedia_description *desc;
544 struct ifmedia_type_to_subtype *ttos;
547 /* Find the top-level interface type. */
548 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
549 desc->ifmt_string != NULL; desc++, ttos++)
550 if (type == desc->ifmt_word)
552 if (desc->ifmt_string == NULL)
553 errx(1, "unknown media mode 0x%x", type);
555 for (i = 0; ttos->modes[i].desc != NULL; i++) {
556 rval = lookup_media_word(ttos->modes[i].desc, val);
564 get_media_options(int type, const char *val)
566 struct ifmedia_description *desc;
567 struct ifmedia_type_to_subtype *ttos;
568 char *optlist, *optptr;
569 int option = 0, i, rval = 0;
571 /* We muck with the string, so copy it. */
572 optlist = strdup(val);
576 /* Find the top-level interface type. */
577 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
578 desc->ifmt_string != NULL; desc++, ttos++)
579 if (type == desc->ifmt_word)
581 if (desc->ifmt_string == NULL)
582 errx(1, "unknown media type 0x%x", type);
585 * Look up the options in the user-provided comma-separated
589 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
590 for (i = 0; ttos->options[i].desc != NULL; i++) {
591 option = lookup_media_word(ttos->options[i].desc, optptr);
596 errx(1, "unknown option: %s", optptr);
605 lookup_media_word(struct ifmedia_description *desc, const char *val)
608 for (; desc->ifmt_string != NULL; desc++)
609 if (strcasecmp(desc->ifmt_string, val) == 0)
610 return (desc->ifmt_word);
615 static struct ifmedia_description *get_toptype_desc(int ifmw)
617 struct ifmedia_description *desc;
619 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
620 if (IFM_TYPE(ifmw) == desc->ifmt_word)
626 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
628 struct ifmedia_description *desc;
629 struct ifmedia_type_to_subtype *ttos;
631 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
632 desc->ifmt_string != NULL; desc++, ttos++)
633 if (IFM_TYPE(ifmw) == desc->ifmt_word)
639 static struct ifmedia_description *get_subtype_desc(int ifmw,
640 struct ifmedia_type_to_subtype *ttos)
643 struct ifmedia_description *desc;
645 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
646 if (ttos->subtypes[i].alias)
648 for (desc = ttos->subtypes[i].desc;
649 desc->ifmt_string != NULL; desc++) {
650 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
658 static struct ifmedia_description *get_mode_desc(int ifmw,
659 struct ifmedia_type_to_subtype *ttos)
662 struct ifmedia_description *desc;
664 for (i = 0; ttos->modes[i].desc != NULL; i++) {
665 if (ttos->modes[i].alias)
667 for (desc = ttos->modes[i].desc;
668 desc->ifmt_string != NULL; desc++) {
669 if (IFM_MODE(ifmw) == desc->ifmt_word)
678 print_media_word(int ifmw, int print_toptype)
680 struct ifmedia_description *desc;
681 struct ifmedia_type_to_subtype *ttos;
682 int seen_option = 0, i;
684 /* Find the top-level interface type. */
685 desc = get_toptype_desc(ifmw);
686 ttos = get_toptype_ttos(ifmw);
687 if (desc->ifmt_string == NULL) {
688 printf("<unknown type>");
690 } else if (print_toptype) {
691 printf("%s", desc->ifmt_string);
695 * Don't print the top-level type; it's not like we can
696 * change it, or anything.
700 desc = get_subtype_desc(ifmw, ttos);
702 printf("<unknown subtype>");
709 printf("%s", desc->ifmt_string);
712 desc = get_mode_desc(ifmw, ttos);
713 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
714 printf(" mode %s", desc->ifmt_string);
718 for (i = 0; ttos->options[i].desc != NULL; i++) {
719 if (ttos->options[i].alias)
721 for (desc = ttos->options[i].desc;
722 desc->ifmt_string != NULL; desc++) {
723 if (ifmw & desc->ifmt_word) {
724 if (seen_option == 0)
726 printf("%s%s", seen_option++ ? "," : "",
731 printf("%s", seen_option ? ">" : "");
733 if (print_toptype && IFM_INST(ifmw) != 0)
734 printf(" instance %d", IFM_INST(ifmw));
738 print_media_word_ifconfig(int ifmw)
740 struct ifmedia_description *desc;
741 struct ifmedia_type_to_subtype *ttos;
742 int seen_option = 0, i;
744 /* Find the top-level interface type. */
745 desc = get_toptype_desc(ifmw);
746 ttos = get_toptype_ttos(ifmw);
747 if (desc->ifmt_string == NULL) {
748 printf("<unknown type>");
753 * Don't print the top-level type; it's not like we can
754 * change it, or anything.
758 desc = get_subtype_desc(ifmw, ttos);
760 printf("<unknown subtype>");
764 printf("media %s", desc->ifmt_string);
766 desc = get_mode_desc(ifmw, ttos);
768 printf(" mode %s", desc->ifmt_string);
771 for (i = 0; ttos->options[i].desc != NULL; i++) {
772 if (ttos->options[i].alias)
774 for (desc = ttos->options[i].desc;
775 desc->ifmt_string != NULL; desc++) {
776 if (ifmw & desc->ifmt_word) {
777 if (seen_option == 0)
778 printf(" mediaopt ");
779 printf("%s%s", seen_option++ ? "," : "",
785 if (IFM_INST(ifmw) != 0)
786 printf(" instance %d", IFM_INST(ifmw));
789 /**********************************************************************
791 **********************************************************************/
793 static struct cmd media_cmds[] = {
794 DEF_CMD_ARG("media", setmedia),
795 DEF_CMD_ARG("mode", setmediamode),
796 DEF_CMD_ARG("mediaopt", setmediaopt),
797 DEF_CMD_ARG("-mediaopt",unsetmediaopt),
798 DEF_CMD_ARG("inst", setmediainst),
799 DEF_CMD_ARG("instance", setmediainst),
801 static struct afswtch af_media = {
802 .af_name = "af_media",
804 .af_other_status = media_status,
807 static __constructor void
812 for (i = 0; i < nitems(media_cmds); i++)
813 cmd_register(&media_cmds[i]);
814 af_register(&af_media);