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>
90 static void domediaopt(const char *, int, int);
91 static int get_media_subtype(int, const char *);
92 static int get_media_mode(int, const char *);
93 static int get_media_options(int, const char *);
94 static int lookup_media_word(struct ifmedia_description *, const char *);
95 static void print_media_word(int, int);
96 static void print_media_word_ifconfig(int);
98 static struct ifmedia_description *get_toptype_desc(int);
99 static struct ifmedia_type_to_subtype *get_toptype_ttos(int);
100 static struct ifmedia_description *get_subtype_desc(int,
101 struct ifmedia_type_to_subtype *ttos);
103 #define IFM_OPMODE(x) \
104 ((x) & (IFM_IEEE80211_ADHOC | IFM_IEEE80211_HOSTAP | \
105 IFM_IEEE80211_IBSS | IFM_IEEE80211_WDS | IFM_IEEE80211_MONITOR | \
107 #define IFM_IEEE80211_STA 0
112 struct ifmediareq ifmr;
116 (void) memset(&ifmr, 0, sizeof(ifmr));
117 (void) strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
120 * Check if interface supports extended media types.
122 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)&ifmr) < 0)
124 if (xmedia == 0 && ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
126 * Interface doesn't support SIOC{G,S}IFMEDIA.
131 if (ifmr.ifm_count == 0) {
132 warnx("%s: no media types?", name);
136 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
137 if (media_list == NULL)
139 ifmr.ifm_ulist = media_list;
142 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)&ifmr) < 0)
143 err(1, "SIOCGIFXMEDIA");
145 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
146 err(1, "SIOCGIFMEDIA");
150 print_media_word(ifmr.ifm_current, 1);
151 if (ifmr.ifm_active != ifmr.ifm_current) {
154 print_media_word(ifmr.ifm_active, 0);
160 if (ifmr.ifm_status & IFM_AVALID) {
161 printf("\tstatus: ");
162 switch (IFM_TYPE(ifmr.ifm_active)) {
165 if (ifmr.ifm_status & IFM_ACTIVE)
168 printf("no carrier");
172 if (ifmr.ifm_status & IFM_ACTIVE) {
173 /* NB: only sta mode associates */
174 if (IFM_OPMODE(ifmr.ifm_active) == IFM_IEEE80211_STA)
175 printf("associated");
179 printf("no carrier");
185 if (ifmr.ifm_count > 0 && supmedia) {
186 printf("\tsupported media:\n");
187 for (i = 0; i < ifmr.ifm_count; i++) {
189 print_media_word_ifconfig(media_list[i]);
198 ifmedia_getstate(int s)
200 static struct ifmediareq *ifmr = NULL;
205 ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq));
209 (void) memset(ifmr, 0, sizeof(struct ifmediareq));
210 (void) strlcpy(ifmr->ifm_name, name,
211 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) {
225 if (xmedia == 0 && ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) {
226 err(1, "SIOCGIFMEDIA");
229 if (ifmr->ifm_count == 0)
230 errx(1, "%s: no media types?", name);
232 mwords = (int *)malloc(ifmr->ifm_count * sizeof(int));
236 ifmr->ifm_ulist = mwords;
238 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)ifmr) < 0)
239 err(1, "SIOCGIFXMEDIA");
241 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0)
242 err(1, "SIOCGIFMEDIA");
250 setifmediacallback(int s, void *arg)
252 struct ifmediareq *ifmr = (struct ifmediareq *)arg;
253 static int did_it = 0;
256 ifr.ifr_media = ifmr->ifm_current;
257 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
258 err(1, "SIOCSIFMEDIA (media)");
259 free(ifmr->ifm_ulist);
266 setmedia(const char *val, int d, int s, const struct afswtch *afp)
268 struct ifmediareq *ifmr;
271 ifmr = ifmedia_getstate(s);
274 * We are primarily concerned with the top-level type.
275 * However, "current" may be only IFM_NONE, so we just look
276 * for the top-level type in the first "supported type"
279 * (I'm assuming that all supported media types for a given
280 * interface will be the same top-level type..)
282 subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val);
284 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
285 ifr.ifr_media = (ifmr->ifm_current & IFM_IMASK) |
286 IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
288 ifmr->ifm_current = ifr.ifr_media;
289 callback_register(setifmediacallback, (void *)ifmr);
293 setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
296 domediaopt(val, 0, s);
300 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
303 domediaopt(val, 1, s);
307 domediaopt(const char *val, int clear, int s)
309 struct ifmediareq *ifmr;
312 ifmr = ifmedia_getstate(s);
314 options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
316 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
317 ifr.ifr_media = ifmr->ifm_current;
319 ifr.ifr_media &= ~options;
321 if (options & IFM_HDX) {
322 ifr.ifr_media &= ~IFM_FDX;
325 ifr.ifr_media |= options;
327 ifmr->ifm_current = ifr.ifr_media;
328 callback_register(setifmediacallback, (void *)ifmr);
332 setmediainst(const char *val, int d, int s, const struct afswtch *afp)
334 struct ifmediareq *ifmr;
337 ifmr = ifmedia_getstate(s);
340 if (inst < 0 || inst > (int)IFM_INST_MAX)
341 errx(1, "invalid media instance: %s", val);
343 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
344 ifr.ifr_media = (ifmr->ifm_current & ~IFM_IMASK) | inst << IFM_ISHIFT;
346 ifmr->ifm_current = ifr.ifr_media;
347 callback_register(setifmediacallback, (void *)ifmr);
351 setmediamode(const char *val, int d, int s, const struct afswtch *afp)
353 struct ifmediareq *ifmr;
356 ifmr = ifmedia_getstate(s);
358 mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
360 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
361 ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode;
363 ifmr->ifm_current = ifr.ifr_media;
364 callback_register(setifmediacallback, (void *)ifmr);
367 /**********************************************************************
368 * A good chunk of this is duplicated from sys/net/if_media.c
369 **********************************************************************/
371 static struct ifmedia_description ifm_type_descriptions[] =
372 IFM_TYPE_DESCRIPTIONS;
374 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
375 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
377 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
378 IFM_SUBTYPE_ETHERNET_ALIASES;
380 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
381 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
383 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
384 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
386 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
387 IFM_SUBTYPE_TOKENRING_ALIASES;
389 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
390 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
392 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
393 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
395 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
396 IFM_SUBTYPE_IEEE80211_ALIASES;
398 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
399 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
401 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
402 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
404 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
405 IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
407 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
408 IFM_SUBTYPE_ATM_DESCRIPTIONS;
410 static struct ifmedia_description ifm_subtype_atm_aliases[] =
411 IFM_SUBTYPE_ATM_ALIASES;
413 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
414 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
416 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
417 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
419 static struct ifmedia_description ifm_subtype_shared_aliases[] =
420 IFM_SUBTYPE_SHARED_ALIASES;
422 static struct ifmedia_description ifm_shared_option_descriptions[] =
423 IFM_SHARED_OPTION_DESCRIPTIONS;
425 static struct ifmedia_description ifm_shared_option_aliases[] =
426 IFM_SHARED_OPTION_ALIASES;
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_shared_option_aliases[0], 1 },
456 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
465 { &ifm_subtype_shared_descriptions[0], 0 },
466 { &ifm_subtype_shared_aliases[0], 1 },
467 { &ifm_subtype_tokenring_descriptions[0], 0 },
468 { &ifm_subtype_tokenring_aliases[0], 1 },
472 { &ifm_shared_option_descriptions[0], 0 },
473 { &ifm_shared_option_aliases[0], 1 },
474 { &ifm_subtype_tokenring_option_descriptions[0], 0 },
483 { &ifm_subtype_shared_descriptions[0], 0 },
484 { &ifm_subtype_shared_aliases[0], 1 },
485 { &ifm_subtype_ieee80211_descriptions[0], 0 },
486 { &ifm_subtype_ieee80211_aliases[0], 1 },
490 { &ifm_shared_option_descriptions[0], 0 },
491 { &ifm_shared_option_aliases[0], 1 },
492 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
496 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
497 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
503 { &ifm_subtype_shared_descriptions[0], 0 },
504 { &ifm_subtype_shared_aliases[0], 1 },
505 { &ifm_subtype_atm_descriptions[0], 0 },
506 { &ifm_subtype_atm_aliases[0], 1 },
510 { &ifm_shared_option_descriptions[0], 0 },
511 { &ifm_shared_option_aliases[0], 1 },
512 { &ifm_subtype_atm_option_descriptions[0], 0 },
522 get_media_subtype(int type, const char *val)
524 struct ifmedia_description *desc;
525 struct ifmedia_type_to_subtype *ttos;
528 /* Find the top-level interface type. */
529 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
530 desc->ifmt_string != NULL; desc++, ttos++)
531 if (type == desc->ifmt_word)
533 if (desc->ifmt_string == NULL)
534 errx(1, "unknown media type 0x%x", type);
536 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
537 rval = lookup_media_word(ttos->subtypes[i].desc, val);
541 errx(1, "unknown media subtype: %s", val);
546 get_media_mode(int type, const char *val)
548 struct ifmedia_description *desc;
549 struct ifmedia_type_to_subtype *ttos;
552 /* Find the top-level interface type. */
553 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
554 desc->ifmt_string != NULL; desc++, ttos++)
555 if (type == desc->ifmt_word)
557 if (desc->ifmt_string == NULL)
558 errx(1, "unknown media mode 0x%x", type);
560 for (i = 0; ttos->modes[i].desc != NULL; i++) {
561 rval = lookup_media_word(ttos->modes[i].desc, val);
569 get_media_options(int type, const char *val)
571 struct ifmedia_description *desc;
572 struct ifmedia_type_to_subtype *ttos;
573 char *optlist, *optptr;
574 int option = 0, i, rval = 0;
576 /* We muck with the string, so copy it. */
577 optlist = strdup(val);
581 /* Find the top-level interface type. */
582 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
583 desc->ifmt_string != NULL; desc++, ttos++)
584 if (type == desc->ifmt_word)
586 if (desc->ifmt_string == NULL)
587 errx(1, "unknown media type 0x%x", type);
590 * Look up the options in the user-provided comma-separated
594 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
595 for (i = 0; ttos->options[i].desc != NULL; i++) {
596 option = lookup_media_word(ttos->options[i].desc, optptr);
601 errx(1, "unknown option: %s", optptr);
610 lookup_media_word(struct ifmedia_description *desc, const char *val)
613 for (; desc->ifmt_string != NULL; desc++)
614 if (strcasecmp(desc->ifmt_string, val) == 0)
615 return (desc->ifmt_word);
620 static struct ifmedia_description *get_toptype_desc(int ifmw)
622 struct ifmedia_description *desc;
624 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
625 if (IFM_TYPE(ifmw) == desc->ifmt_word)
631 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
633 struct ifmedia_description *desc;
634 struct ifmedia_type_to_subtype *ttos;
636 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
637 desc->ifmt_string != NULL; desc++, ttos++)
638 if (IFM_TYPE(ifmw) == desc->ifmt_word)
644 static struct ifmedia_description *get_subtype_desc(int ifmw,
645 struct ifmedia_type_to_subtype *ttos)
648 struct ifmedia_description *desc;
650 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
651 if (ttos->subtypes[i].alias)
653 for (desc = ttos->subtypes[i].desc;
654 desc->ifmt_string != NULL; desc++) {
655 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
663 static struct ifmedia_description *get_mode_desc(int ifmw,
664 struct ifmedia_type_to_subtype *ttos)
667 struct ifmedia_description *desc;
669 for (i = 0; ttos->modes[i].desc != NULL; i++) {
670 if (ttos->modes[i].alias)
672 for (desc = ttos->modes[i].desc;
673 desc->ifmt_string != NULL; desc++) {
674 if (IFM_MODE(ifmw) == desc->ifmt_word)
683 print_media_word(int ifmw, int print_toptype)
685 struct ifmedia_description *desc;
686 struct ifmedia_type_to_subtype *ttos;
687 int seen_option = 0, i;
689 /* Find the top-level interface type. */
690 desc = get_toptype_desc(ifmw);
691 ttos = get_toptype_ttos(ifmw);
692 if (desc->ifmt_string == NULL) {
693 printf("<unknown type>");
695 } else if (print_toptype) {
696 printf("%s", desc->ifmt_string);
700 * Don't print the top-level type; it's not like we can
701 * change it, or anything.
705 desc = get_subtype_desc(ifmw, ttos);
707 printf("<unknown subtype>");
714 printf("%s", desc->ifmt_string);
717 desc = get_mode_desc(ifmw, ttos);
718 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
719 printf(" mode %s", desc->ifmt_string);
723 for (i = 0; ttos->options[i].desc != NULL; i++) {
724 if (ttos->options[i].alias)
726 for (desc = ttos->options[i].desc;
727 desc->ifmt_string != NULL; desc++) {
728 if (ifmw & desc->ifmt_word) {
729 if (seen_option == 0)
731 printf("%s%s", seen_option++ ? "," : "",
736 printf("%s", seen_option ? ">" : "");
738 if (print_toptype && IFM_INST(ifmw) != 0)
739 printf(" instance %d", IFM_INST(ifmw));
743 print_media_word_ifconfig(int ifmw)
745 struct ifmedia_description *desc;
746 struct ifmedia_type_to_subtype *ttos;
747 int seen_option = 0, i;
749 /* Find the top-level interface type. */
750 desc = get_toptype_desc(ifmw);
751 ttos = get_toptype_ttos(ifmw);
752 if (desc->ifmt_string == NULL) {
753 printf("<unknown type>");
758 * Don't print the top-level type; it's not like we can
759 * change it, or anything.
763 desc = get_subtype_desc(ifmw, ttos);
765 printf("<unknown subtype>");
769 printf("media %s", desc->ifmt_string);
771 desc = get_mode_desc(ifmw, ttos);
773 printf(" mode %s", desc->ifmt_string);
776 for (i = 0; ttos->options[i].desc != NULL; i++) {
777 if (ttos->options[i].alias)
779 for (desc = ttos->options[i].desc;
780 desc->ifmt_string != NULL; desc++) {
781 if (ifmw & desc->ifmt_word) {
782 if (seen_option == 0)
783 printf(" mediaopt ");
784 printf("%s%s", seen_option++ ? "," : "",
790 if (IFM_INST(ifmw) != 0)
791 printf(" instance %d", IFM_INST(ifmw));
794 /**********************************************************************
796 **********************************************************************/
798 static struct cmd media_cmds[] = {
799 DEF_CMD_ARG("media", setmedia),
800 DEF_CMD_ARG("mode", setmediamode),
801 DEF_CMD_ARG("mediaopt", setmediaopt),
802 DEF_CMD_ARG("-mediaopt",unsetmediaopt),
803 DEF_CMD_ARG("inst", setmediainst),
804 DEF_CMD_ARG("instance", setmediainst),
806 static struct afswtch af_media = {
807 .af_name = "af_media",
809 .af_other_status = media_status,
812 static __constructor void
817 for (i = 0; i < nitems(media_cmds); i++)
818 cmd_register(&media_cmds[i]);
819 af_register(&af_media);