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 * based on sbin/ifconfig/ifmedia.c r221954
70 #include <sys/param.h>
71 #include <sys/ioctl.h>
72 #include <sys/socket.h>
73 #include <sys/sysctl.h>
77 #include <net/if_dl.h>
78 #include <net/if_types.h>
79 #include <net/if_media.h>
80 #include <net/route.h>
91 void domediaopt(const char *, int, int);
92 int get_media_subtype(int, const char *);
93 int get_media_mode(int, const char *);
94 int get_media_options(int, const char *);
95 int lookup_media_word(struct ifmedia_description *, const char *);
96 void print_media_word(int, int);
97 void print_media_word_ifconfig(int);
100 static struct ifmedia_description *get_toptype_desc(int);
101 static struct ifmedia_type_to_subtype *get_toptype_ttos(int);
102 static struct ifmedia_description *get_subtype_desc(int,
103 struct ifmedia_type_to_subtype *ttos);
105 #define IFM_OPMODE(x) \
106 ((x) & (IFM_IEEE80211_ADHOC | IFM_IEEE80211_HOSTAP | \
107 IFM_IEEE80211_IBSS | IFM_IEEE80211_WDS | IFM_IEEE80211_MONITOR | \
109 #define IFM_IEEE80211_STA 0
114 struct ifmediareq ifmr;
117 (void) memset(&ifmr, 0, sizeof(ifmr));
118 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
120 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
122 * Interface doesn't support SIOC{G,S}IFMEDIA.
127 if (ifmr.ifm_count == 0) {
128 warnx("%s: no media types?", name);
132 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
133 if (media_list == NULL)
135 ifmr.ifm_ulist = media_list;
137 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
138 err(1, "SIOCGIFMEDIA");
141 print_media_word(ifmr.ifm_current, 1);
142 if (ifmr.ifm_active != ifmr.ifm_current) {
145 print_media_word(ifmr.ifm_active, 0);
151 if (ifmr.ifm_status & IFM_AVALID) {
152 printf("\tstatus: ");
153 switch (IFM_TYPE(ifmr.ifm_active)) {
156 if (ifmr.ifm_status & IFM_ACTIVE)
159 printf("no carrier");
164 if (ifmr.ifm_status & IFM_ACTIVE)
171 if (ifmr.ifm_status & IFM_ACTIVE) {
172 /* NB: only sta mode associates */
173 if (IFM_OPMODE(ifmr.ifm_active) == IFM_IEEE80211_STA)
174 printf("associated");
178 printf("no carrier");
184 if (ifmr.ifm_count > 0 && supmedia) {
185 printf("\tsupported media:\n");
186 for (i = 0; i < ifmr.ifm_count; i++) {
188 print_media_word_ifconfig(media_list[i]);
197 ifmedia_getstate(int s)
199 static struct ifmediareq *ifmr = NULL;
203 ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq));
207 (void) memset(ifmr, 0, sizeof(struct ifmediareq));
208 (void) strncpy(ifmr->ifm_name, name,
209 sizeof(ifmr->ifm_name));
212 ifmr->ifm_ulist = NULL;
215 * We must go through the motions of reading all
216 * supported media because we need to know both
217 * the current media type and the top-level type.
220 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) {
221 err(1, "SIOCGIFMEDIA");
224 if (ifmr->ifm_count == 0)
225 errx(1, "%s: no media types?", name);
227 mwords = (int *)malloc(ifmr->ifm_count * sizeof(int));
231 ifmr->ifm_ulist = mwords;
232 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0)
233 err(1, "SIOCGIFMEDIA");
240 setifmediacallback(int s, void *arg)
242 struct ifmediareq *ifmr = (struct ifmediareq *)arg;
243 static int did_it = 0;
246 ifr.ifr_media = ifmr->ifm_current;
247 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
248 err(1, "SIOCSIFMEDIA (media)");
249 free(ifmr->ifm_ulist);
256 setmedia(const char *val, int d, int s, const struct afswtch *afp)
258 struct ifmediareq *ifmr;
261 ifmr = ifmedia_getstate(s);
264 * We are primarily concerned with the top-level type.
265 * However, "current" may be only IFM_NONE, so we just look
266 * for the top-level type in the first "supported type"
269 * (I'm assuming that all supported media types for a given
270 * interface will be the same top-level type..)
272 subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val);
274 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
275 ifr.ifr_media = (ifmr->ifm_current & IFM_IMASK) |
276 IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
278 ifmr->ifm_current = ifr.ifr_media;
279 callback_register(setifmediacallback, (void *)ifmr);
283 setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
286 domediaopt(val, 0, s);
290 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
293 domediaopt(val, 1, s);
297 domediaopt(const char *val, int clear, int s)
299 struct ifmediareq *ifmr;
302 ifmr = ifmedia_getstate(s);
304 options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
306 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
307 ifr.ifr_media = ifmr->ifm_current;
309 ifr.ifr_media &= ~options;
311 if (options & IFM_HDX) {
312 ifr.ifr_media &= ~IFM_FDX;
315 ifr.ifr_media |= options;
317 ifmr->ifm_current = ifr.ifr_media;
318 callback_register(setifmediacallback, (void *)ifmr);
322 setmediainst(const char *val, int d, int s, const struct afswtch *afp)
324 struct ifmediareq *ifmr;
327 ifmr = ifmedia_getstate(s);
330 if (inst < 0 || inst > (int)IFM_INST_MAX)
331 errx(1, "invalid media instance: %s", val);
333 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
334 ifr.ifr_media = (ifmr->ifm_current & ~IFM_IMASK) | inst << IFM_ISHIFT;
336 ifmr->ifm_current = ifr.ifr_media;
337 callback_register(setifmediacallback, (void *)ifmr);
341 setmediamode(const char *val, int d, int s, const struct afswtch *afp)
343 struct ifmediareq *ifmr;
346 ifmr = ifmedia_getstate(s);
348 mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
350 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
351 ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode;
353 ifmr->ifm_current = ifr.ifr_media;
354 callback_register(setifmediacallback, (void *)ifmr);
358 /**********************************************************************
359 * A good chunk of this is duplicated from sys/net/ifmedia.c
360 **********************************************************************/
362 static struct ifmedia_description ifm_type_descriptions[] =
363 IFM_TYPE_DESCRIPTIONS;
365 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
366 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
368 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
369 IFM_SUBTYPE_ETHERNET_ALIASES;
371 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
372 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
374 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
375 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
377 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
378 IFM_SUBTYPE_TOKENRING_ALIASES;
380 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
381 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
383 static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
384 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
386 static struct ifmedia_description ifm_subtype_fddi_aliases[] =
387 IFM_SUBTYPE_FDDI_ALIASES;
389 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
390 IFM_SUBTYPE_FDDI_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 static struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
402 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
404 static 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_fddi_descriptions[0], 0 },
486 { &ifm_subtype_fddi_aliases[0], 1 },
490 { &ifm_shared_option_descriptions[0], 0 },
491 { &ifm_shared_option_aliases[0], 1 },
492 { &ifm_subtype_fddi_option_descriptions[0], 0 },
501 { &ifm_subtype_shared_descriptions[0], 0 },
502 { &ifm_subtype_shared_aliases[0], 1 },
503 { &ifm_subtype_ieee80211_descriptions[0], 0 },
504 { &ifm_subtype_ieee80211_aliases[0], 1 },
508 { &ifm_shared_option_descriptions[0], 0 },
509 { &ifm_shared_option_aliases[0], 1 },
510 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
514 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
515 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
521 { &ifm_subtype_shared_descriptions[0], 0 },
522 { &ifm_subtype_shared_aliases[0], 1 },
523 { &ifm_subtype_atm_descriptions[0], 0 },
524 { &ifm_subtype_atm_aliases[0], 1 },
528 { &ifm_shared_option_descriptions[0], 0 },
529 { &ifm_shared_option_aliases[0], 1 },
530 { &ifm_subtype_atm_option_descriptions[0], 0 },
540 get_media_subtype(int type, const char *val)
542 struct ifmedia_description *desc;
543 struct ifmedia_type_to_subtype *ttos;
546 /* Find the top-level interface type. */
547 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
548 desc->ifmt_string != NULL; desc++, ttos++)
549 if (type == desc->ifmt_word)
551 if (desc->ifmt_string == NULL)
552 errx(1, "unknown media type 0x%x", type);
554 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
555 rval = lookup_media_word(ttos->subtypes[i].desc, val);
559 errx(1, "unknown media subtype: %s", val);
564 get_media_mode(int type, const char *val)
566 struct ifmedia_description *desc;
567 struct ifmedia_type_to_subtype *ttos;
570 /* Find the top-level interface type. */
571 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
572 desc->ifmt_string != NULL; desc++, ttos++)
573 if (type == desc->ifmt_word)
575 if (desc->ifmt_string == NULL)
576 errx(1, "unknown media mode 0x%x", type);
578 for (i = 0; ttos->modes[i].desc != NULL; i++) {
579 rval = lookup_media_word(ttos->modes[i].desc, val);
587 get_media_options(int type, const char *val)
589 struct ifmedia_description *desc;
590 struct ifmedia_type_to_subtype *ttos;
591 char *optlist, *optptr;
592 int option = 0, i, rval = 0;
594 /* We muck with the string, so copy it. */
595 optlist = strdup(val);
599 /* Find the top-level interface type. */
600 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
601 desc->ifmt_string != NULL; desc++, ttos++)
602 if (type == desc->ifmt_word)
604 if (desc->ifmt_string == NULL)
605 errx(1, "unknown media type 0x%x", type);
608 * Look up the options in the user-provided comma-separated
612 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
613 for (i = 0; ttos->options[i].desc != NULL; i++) {
614 option = lookup_media_word(ttos->options[i].desc, optptr);
619 errx(1, "unknown option: %s", optptr);
628 lookup_media_word(struct ifmedia_description *desc, const char *val)
631 for (; desc->ifmt_string != NULL; desc++)
632 if (strcasecmp(desc->ifmt_string, val) == 0)
633 return (desc->ifmt_word);
638 static struct ifmedia_description *get_toptype_desc(int ifmw)
640 struct ifmedia_description *desc;
642 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
643 if (IFM_TYPE(ifmw) == desc->ifmt_word)
649 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
651 struct ifmedia_description *desc;
652 struct ifmedia_type_to_subtype *ttos;
654 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
655 desc->ifmt_string != NULL; desc++, ttos++)
656 if (IFM_TYPE(ifmw) == desc->ifmt_word)
662 static struct ifmedia_description *get_subtype_desc(int ifmw,
663 struct ifmedia_type_to_subtype *ttos)
666 struct ifmedia_description *desc;
668 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
669 if (ttos->subtypes[i].alias)
671 for (desc = ttos->subtypes[i].desc;
672 desc->ifmt_string != NULL; desc++) {
673 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
681 static struct ifmedia_description *get_mode_desc(int ifmw,
682 struct ifmedia_type_to_subtype *ttos)
685 struct ifmedia_description *desc;
687 for (i = 0; ttos->modes[i].desc != NULL; i++) {
688 if (ttos->modes[i].alias)
690 for (desc = ttos->modes[i].desc;
691 desc->ifmt_string != NULL; desc++) {
692 if (IFM_MODE(ifmw) == desc->ifmt_word)
701 print_media_word(int ifmw, int print_toptype)
703 struct ifmedia_description *desc;
704 struct ifmedia_type_to_subtype *ttos;
705 int seen_option = 0, i;
707 /* Find the top-level interface type. */
708 desc = get_toptype_desc(ifmw);
709 ttos = get_toptype_ttos(ifmw);
710 if (desc->ifmt_string == NULL) {
711 printf("<unknown type>");
713 } else if (print_toptype) {
714 printf("%s", desc->ifmt_string);
718 * Don't print the top-level type; it's not like we can
719 * change it, or anything.
723 desc = get_subtype_desc(ifmw, ttos);
725 printf("<unknown subtype>");
732 printf("%s", desc->ifmt_string);
735 desc = get_mode_desc(ifmw, ttos);
736 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
737 printf(" mode %s", desc->ifmt_string);
741 for (i = 0; ttos->options[i].desc != NULL; i++) {
742 if (ttos->options[i].alias)
744 for (desc = ttos->options[i].desc;
745 desc->ifmt_string != NULL; desc++) {
746 if (ifmw & desc->ifmt_word) {
747 if (seen_option == 0)
749 printf("%s%s", seen_option++ ? "," : "",
754 printf("%s", seen_option ? ">" : "");
756 if (print_toptype && IFM_INST(ifmw) != 0)
757 printf(" instance %d", IFM_INST(ifmw));
761 print_media_word_ifconfig(int ifmw)
763 struct ifmedia_description *desc;
764 struct ifmedia_type_to_subtype *ttos;
765 int seen_option = 0, i;
767 /* Find the top-level interface type. */
768 desc = get_toptype_desc(ifmw);
769 ttos = get_toptype_ttos(ifmw);
770 if (desc->ifmt_string == NULL) {
771 printf("<unknown type>");
776 * Don't print the top-level type; it's not like we can
777 * change it, or anything.
781 desc = get_subtype_desc(ifmw, ttos);
783 printf("<unknown subtype>");
787 printf("media %s", desc->ifmt_string);
789 desc = get_mode_desc(ifmw, ttos);
791 printf(" mode %s", desc->ifmt_string);
794 for (i = 0; ttos->options[i].desc != NULL; i++) {
795 if (ttos->options[i].alias)
797 for (desc = ttos->options[i].desc;
798 desc->ifmt_string != NULL; desc++) {
799 if (ifmw & desc->ifmt_word) {
800 if (seen_option == 0)
801 printf(" mediaopt ");
802 printf("%s%s", seen_option++ ? "," : "",
808 if (IFM_INST(ifmw) != 0)
809 printf(" instance %d", IFM_INST(ifmw));
812 /**********************************************************************
814 **********************************************************************/