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_ieee80211_descriptions[] =
384 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
386 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
387 IFM_SUBTYPE_IEEE80211_ALIASES;
389 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
390 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
392 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
393 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
395 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
396 IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
398 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
399 IFM_SUBTYPE_ATM_DESCRIPTIONS;
401 static struct ifmedia_description ifm_subtype_atm_aliases[] =
402 IFM_SUBTYPE_ATM_ALIASES;
404 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
405 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
407 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
408 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
410 static struct ifmedia_description ifm_subtype_shared_aliases[] =
411 IFM_SUBTYPE_SHARED_ALIASES;
413 static struct ifmedia_description ifm_shared_option_descriptions[] =
414 IFM_SHARED_OPTION_DESCRIPTIONS;
416 static struct ifmedia_description ifm_shared_option_aliases[] =
417 IFM_SHARED_OPTION_ALIASES;
419 struct ifmedia_type_to_subtype {
421 struct ifmedia_description *desc;
425 struct ifmedia_description *desc;
429 struct ifmedia_description *desc;
434 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
435 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
438 { &ifm_subtype_shared_descriptions[0], 0 },
439 { &ifm_subtype_shared_aliases[0], 1 },
440 { &ifm_subtype_ethernet_descriptions[0], 0 },
441 { &ifm_subtype_ethernet_aliases[0], 1 },
445 { &ifm_shared_option_descriptions[0], 0 },
446 { &ifm_shared_option_aliases[0], 1 },
447 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
456 { &ifm_subtype_shared_descriptions[0], 0 },
457 { &ifm_subtype_shared_aliases[0], 1 },
458 { &ifm_subtype_ieee80211_descriptions[0], 0 },
459 { &ifm_subtype_ieee80211_aliases[0], 1 },
463 { &ifm_shared_option_descriptions[0], 0 },
464 { &ifm_shared_option_aliases[0], 1 },
465 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
469 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
470 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
476 { &ifm_subtype_shared_descriptions[0], 0 },
477 { &ifm_subtype_shared_aliases[0], 1 },
478 { &ifm_subtype_atm_descriptions[0], 0 },
479 { &ifm_subtype_atm_aliases[0], 1 },
483 { &ifm_shared_option_descriptions[0], 0 },
484 { &ifm_shared_option_aliases[0], 1 },
485 { &ifm_subtype_atm_option_descriptions[0], 0 },
495 get_media_subtype(int type, const char *val)
497 struct ifmedia_description *desc;
498 struct ifmedia_type_to_subtype *ttos;
501 /* Find the top-level interface type. */
502 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
503 desc->ifmt_string != NULL; desc++, ttos++)
504 if (type == desc->ifmt_word)
506 if (desc->ifmt_string == NULL)
507 errx(1, "unknown media type 0x%x", type);
509 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
510 rval = lookup_media_word(ttos->subtypes[i].desc, val);
514 errx(1, "unknown media subtype: %s", val);
519 get_media_mode(int type, const char *val)
521 struct ifmedia_description *desc;
522 struct ifmedia_type_to_subtype *ttos;
525 /* Find the top-level interface type. */
526 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
527 desc->ifmt_string != NULL; desc++, ttos++)
528 if (type == desc->ifmt_word)
530 if (desc->ifmt_string == NULL)
531 errx(1, "unknown media mode 0x%x", type);
533 for (i = 0; ttos->modes[i].desc != NULL; i++) {
534 rval = lookup_media_word(ttos->modes[i].desc, val);
542 get_media_options(int type, const char *val)
544 struct ifmedia_description *desc;
545 struct ifmedia_type_to_subtype *ttos;
546 char *optlist, *optptr;
547 int option = 0, i, rval = 0;
549 /* We muck with the string, so copy it. */
550 optlist = strdup(val);
554 /* Find the top-level interface type. */
555 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
556 desc->ifmt_string != NULL; desc++, ttos++)
557 if (type == desc->ifmt_word)
559 if (desc->ifmt_string == NULL)
560 errx(1, "unknown media type 0x%x", type);
563 * Look up the options in the user-provided comma-separated
567 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
568 for (i = 0; ttos->options[i].desc != NULL; i++) {
569 option = lookup_media_word(ttos->options[i].desc, optptr);
574 errx(1, "unknown option: %s", optptr);
583 lookup_media_word(struct ifmedia_description *desc, const char *val)
586 for (; desc->ifmt_string != NULL; desc++)
587 if (strcasecmp(desc->ifmt_string, val) == 0)
588 return (desc->ifmt_word);
593 static struct ifmedia_description *get_toptype_desc(int ifmw)
595 struct ifmedia_description *desc;
597 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
598 if (IFM_TYPE(ifmw) == desc->ifmt_word)
604 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
606 struct ifmedia_description *desc;
607 struct ifmedia_type_to_subtype *ttos;
609 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
610 desc->ifmt_string != NULL; desc++, ttos++)
611 if (IFM_TYPE(ifmw) == desc->ifmt_word)
617 static struct ifmedia_description *get_subtype_desc(int ifmw,
618 struct ifmedia_type_to_subtype *ttos)
621 struct ifmedia_description *desc;
623 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
624 if (ttos->subtypes[i].alias)
626 for (desc = ttos->subtypes[i].desc;
627 desc->ifmt_string != NULL; desc++) {
628 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
636 static struct ifmedia_description *get_mode_desc(int ifmw,
637 struct ifmedia_type_to_subtype *ttos)
640 struct ifmedia_description *desc;
642 for (i = 0; ttos->modes[i].desc != NULL; i++) {
643 if (ttos->modes[i].alias)
645 for (desc = ttos->modes[i].desc;
646 desc->ifmt_string != NULL; desc++) {
647 if (IFM_MODE(ifmw) == desc->ifmt_word)
656 print_media_word(int ifmw, int print_toptype)
658 struct ifmedia_description *desc;
659 struct ifmedia_type_to_subtype *ttos;
660 int seen_option = 0, i;
662 /* Find the top-level interface type. */
663 desc = get_toptype_desc(ifmw);
664 ttos = get_toptype_ttos(ifmw);
665 if (desc->ifmt_string == NULL) {
666 printf("<unknown type>");
668 } else if (print_toptype) {
669 printf("%s", desc->ifmt_string);
673 * Don't print the top-level type; it's not like we can
674 * change it, or anything.
678 desc = get_subtype_desc(ifmw, ttos);
680 printf("<unknown subtype>");
687 printf("%s", desc->ifmt_string);
690 desc = get_mode_desc(ifmw, ttos);
691 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
692 printf(" mode %s", desc->ifmt_string);
696 for (i = 0; ttos->options[i].desc != NULL; i++) {
697 if (ttos->options[i].alias)
699 for (desc = ttos->options[i].desc;
700 desc->ifmt_string != NULL; desc++) {
701 if (ifmw & desc->ifmt_word) {
702 if (seen_option == 0)
704 printf("%s%s", seen_option++ ? "," : "",
709 printf("%s", seen_option ? ">" : "");
711 if (print_toptype && IFM_INST(ifmw) != 0)
712 printf(" instance %d", IFM_INST(ifmw));
716 print_media_word_ifconfig(int ifmw)
718 struct ifmedia_description *desc;
719 struct ifmedia_type_to_subtype *ttos;
720 int seen_option = 0, i;
722 /* Find the top-level interface type. */
723 desc = get_toptype_desc(ifmw);
724 ttos = get_toptype_ttos(ifmw);
725 if (desc->ifmt_string == NULL) {
726 printf("<unknown type>");
731 * Don't print the top-level type; it's not like we can
732 * change it, or anything.
736 desc = get_subtype_desc(ifmw, ttos);
738 printf("<unknown subtype>");
742 printf("media %s", desc->ifmt_string);
744 desc = get_mode_desc(ifmw, ttos);
746 printf(" mode %s", desc->ifmt_string);
749 for (i = 0; ttos->options[i].desc != NULL; i++) {
750 if (ttos->options[i].alias)
752 for (desc = ttos->options[i].desc;
753 desc->ifmt_string != NULL; desc++) {
754 if (ifmw & desc->ifmt_word) {
755 if (seen_option == 0)
756 printf(" mediaopt ");
757 printf("%s%s", seen_option++ ? "," : "",
763 if (IFM_INST(ifmw) != 0)
764 printf(" instance %d", IFM_INST(ifmw));
767 /**********************************************************************
769 **********************************************************************/
771 static struct cmd media_cmds[] = {
772 DEF_CMD_ARG("media", setmedia),
773 DEF_CMD_ARG("mode", setmediamode),
774 DEF_CMD_ARG("mediaopt", setmediaopt),
775 DEF_CMD_ARG("-mediaopt",unsetmediaopt),
776 DEF_CMD_ARG("inst", setmediainst),
777 DEF_CMD_ARG("instance", setmediainst),
779 static struct afswtch af_media = {
780 .af_name = "af_media",
782 .af_other_status = media_status,
785 static __constructor void
790 for (i = 0; i < nitems(media_cmds); i++)
791 cmd_register(&media_cmds[i]);
792 af_register(&af_media);