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");
163 if (ifmr.ifm_status & IFM_ACTIVE) {
164 /* NB: only sta mode associates */
165 if (IFM_OPMODE(ifmr.ifm_active) == IFM_IEEE80211_STA)
166 printf("associated");
170 printf("no carrier");
176 if (ifmr.ifm_count > 0 && supmedia) {
177 printf("\tsupported media:\n");
178 for (i = 0; i < ifmr.ifm_count; i++) {
180 print_media_word_ifconfig(media_list[i]);
189 ifmedia_getstate(int s)
191 static struct ifmediareq *ifmr = NULL;
195 ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq));
199 (void) memset(ifmr, 0, sizeof(struct ifmediareq));
200 (void) strncpy(ifmr->ifm_name, name,
201 sizeof(ifmr->ifm_name));
204 ifmr->ifm_ulist = NULL;
207 * We must go through the motions of reading all
208 * supported media because we need to know both
209 * the current media type and the top-level type.
212 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) {
213 err(1, "SIOCGIFMEDIA");
216 if (ifmr->ifm_count == 0)
217 errx(1, "%s: no media types?", name);
219 mwords = (int *)malloc(ifmr->ifm_count * sizeof(int));
223 ifmr->ifm_ulist = mwords;
224 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0)
225 err(1, "SIOCGIFMEDIA");
232 setifmediacallback(int s, void *arg)
234 struct ifmediareq *ifmr = (struct ifmediareq *)arg;
235 static int did_it = 0;
238 ifr.ifr_media = ifmr->ifm_current;
239 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
240 err(1, "SIOCSIFMEDIA (media)");
241 free(ifmr->ifm_ulist);
248 setmedia(const char *val, int d, int s, const struct afswtch *afp)
250 struct ifmediareq *ifmr;
253 ifmr = ifmedia_getstate(s);
256 * We are primarily concerned with the top-level type.
257 * However, "current" may be only IFM_NONE, so we just look
258 * for the top-level type in the first "supported type"
261 * (I'm assuming that all supported media types for a given
262 * interface will be the same top-level type..)
264 subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val);
266 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
267 ifr.ifr_media = (ifmr->ifm_current & IFM_IMASK) |
268 IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
270 ifmr->ifm_current = ifr.ifr_media;
271 callback_register(setifmediacallback, (void *)ifmr);
275 setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
278 domediaopt(val, 0, s);
282 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
285 domediaopt(val, 1, s);
289 domediaopt(const char *val, int clear, int s)
291 struct ifmediareq *ifmr;
294 ifmr = ifmedia_getstate(s);
296 options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
298 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
299 ifr.ifr_media = ifmr->ifm_current;
301 ifr.ifr_media &= ~options;
303 if (options & IFM_HDX) {
304 ifr.ifr_media &= ~IFM_FDX;
307 ifr.ifr_media |= options;
309 ifmr->ifm_current = ifr.ifr_media;
310 callback_register(setifmediacallback, (void *)ifmr);
314 setmediainst(const char *val, int d, int s, const struct afswtch *afp)
316 struct ifmediareq *ifmr;
319 ifmr = ifmedia_getstate(s);
322 if (inst < 0 || inst > (int)IFM_INST_MAX)
323 errx(1, "invalid media instance: %s", val);
325 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
326 ifr.ifr_media = (ifmr->ifm_current & ~IFM_IMASK) | inst << IFM_ISHIFT;
328 ifmr->ifm_current = ifr.ifr_media;
329 callback_register(setifmediacallback, (void *)ifmr);
333 setmediamode(const char *val, int d, int s, const struct afswtch *afp)
335 struct ifmediareq *ifmr;
338 ifmr = ifmedia_getstate(s);
340 mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
342 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
343 ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode;
345 ifmr->ifm_current = ifr.ifr_media;
346 callback_register(setifmediacallback, (void *)ifmr);
350 /**********************************************************************
351 * A good chunk of this is duplicated from sys/net/ifmedia.c
352 **********************************************************************/
354 static struct ifmedia_description ifm_type_descriptions[] =
355 IFM_TYPE_DESCRIPTIONS;
357 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
358 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
360 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
361 IFM_SUBTYPE_ETHERNET_ALIASES;
363 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
364 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
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 static struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
376 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
378 static 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 static struct ifmedia_description ifm_shared_option_aliases[] =
400 IFM_SHARED_OPTION_ALIASES;
402 struct ifmedia_type_to_subtype {
404 struct ifmedia_description *desc;
408 struct ifmedia_description *desc;
412 struct ifmedia_description *desc;
417 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
418 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
421 { &ifm_subtype_shared_descriptions[0], 0 },
422 { &ifm_subtype_shared_aliases[0], 1 },
423 { &ifm_subtype_ethernet_descriptions[0], 0 },
424 { &ifm_subtype_ethernet_aliases[0], 1 },
428 { &ifm_shared_option_descriptions[0], 0 },
429 { &ifm_shared_option_aliases[0], 1 },
430 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
439 { &ifm_subtype_shared_descriptions[0], 0 },
440 { &ifm_subtype_shared_aliases[0], 1 },
441 { &ifm_subtype_ieee80211_descriptions[0], 0 },
442 { &ifm_subtype_ieee80211_aliases[0], 1 },
446 { &ifm_shared_option_descriptions[0], 0 },
447 { &ifm_shared_option_aliases[0], 1 },
448 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
452 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
453 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
459 { &ifm_subtype_shared_descriptions[0], 0 },
460 { &ifm_subtype_shared_aliases[0], 1 },
461 { &ifm_subtype_atm_descriptions[0], 0 },
462 { &ifm_subtype_atm_aliases[0], 1 },
466 { &ifm_shared_option_descriptions[0], 0 },
467 { &ifm_shared_option_aliases[0], 1 },
468 { &ifm_subtype_atm_option_descriptions[0], 0 },
478 get_media_subtype(int type, const char *val)
480 struct ifmedia_description *desc;
481 struct ifmedia_type_to_subtype *ttos;
484 /* Find the top-level interface type. */
485 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
486 desc->ifmt_string != NULL; desc++, ttos++)
487 if (type == desc->ifmt_word)
489 if (desc->ifmt_string == NULL)
490 errx(1, "unknown media type 0x%x", type);
492 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
493 rval = lookup_media_word(ttos->subtypes[i].desc, val);
497 errx(1, "unknown media subtype: %s", val);
502 get_media_mode(int type, const char *val)
504 struct ifmedia_description *desc;
505 struct ifmedia_type_to_subtype *ttos;
508 /* Find the top-level interface type. */
509 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
510 desc->ifmt_string != NULL; desc++, ttos++)
511 if (type == desc->ifmt_word)
513 if (desc->ifmt_string == NULL)
514 errx(1, "unknown media mode 0x%x", type);
516 for (i = 0; ttos->modes[i].desc != NULL; i++) {
517 rval = lookup_media_word(ttos->modes[i].desc, val);
525 get_media_options(int type, const char *val)
527 struct ifmedia_description *desc;
528 struct ifmedia_type_to_subtype *ttos;
529 char *optlist, *optptr;
530 int option = 0, i, rval = 0;
532 /* We muck with the string, so copy it. */
533 optlist = strdup(val);
537 /* Find the top-level interface type. */
538 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
539 desc->ifmt_string != NULL; desc++, ttos++)
540 if (type == desc->ifmt_word)
542 if (desc->ifmt_string == NULL)
543 errx(1, "unknown media type 0x%x", type);
546 * Look up the options in the user-provided comma-separated
550 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
551 for (i = 0; ttos->options[i].desc != NULL; i++) {
552 option = lookup_media_word(ttos->options[i].desc, optptr);
557 errx(1, "unknown option: %s", optptr);
566 lookup_media_word(struct ifmedia_description *desc, const char *val)
569 for (; desc->ifmt_string != NULL; desc++)
570 if (strcasecmp(desc->ifmt_string, val) == 0)
571 return (desc->ifmt_word);
576 static struct ifmedia_description *get_toptype_desc(int ifmw)
578 struct ifmedia_description *desc;
580 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
581 if (IFM_TYPE(ifmw) == desc->ifmt_word)
587 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
589 struct ifmedia_description *desc;
590 struct ifmedia_type_to_subtype *ttos;
592 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
593 desc->ifmt_string != NULL; desc++, ttos++)
594 if (IFM_TYPE(ifmw) == desc->ifmt_word)
600 static struct ifmedia_description *get_subtype_desc(int ifmw,
601 struct ifmedia_type_to_subtype *ttos)
604 struct ifmedia_description *desc;
606 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
607 if (ttos->subtypes[i].alias)
609 for (desc = ttos->subtypes[i].desc;
610 desc->ifmt_string != NULL; desc++) {
611 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
619 static struct ifmedia_description *get_mode_desc(int ifmw,
620 struct ifmedia_type_to_subtype *ttos)
623 struct ifmedia_description *desc;
625 for (i = 0; ttos->modes[i].desc != NULL; i++) {
626 if (ttos->modes[i].alias)
628 for (desc = ttos->modes[i].desc;
629 desc->ifmt_string != NULL; desc++) {
630 if (IFM_MODE(ifmw) == desc->ifmt_word)
639 print_media_word(int ifmw, int print_toptype)
641 struct ifmedia_description *desc;
642 struct ifmedia_type_to_subtype *ttos;
643 int seen_option = 0, i;
645 /* Find the top-level interface type. */
646 desc = get_toptype_desc(ifmw);
647 ttos = get_toptype_ttos(ifmw);
648 if (desc->ifmt_string == NULL) {
649 printf("<unknown type>");
651 } else if (print_toptype) {
652 printf("%s", desc->ifmt_string);
656 * Don't print the top-level type; it's not like we can
657 * change it, or anything.
661 desc = get_subtype_desc(ifmw, ttos);
663 printf("<unknown subtype>");
670 printf("%s", desc->ifmt_string);
673 desc = get_mode_desc(ifmw, ttos);
674 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
675 printf(" mode %s", desc->ifmt_string);
679 for (i = 0; ttos->options[i].desc != NULL; i++) {
680 if (ttos->options[i].alias)
682 for (desc = ttos->options[i].desc;
683 desc->ifmt_string != NULL; desc++) {
684 if (ifmw & desc->ifmt_word) {
685 if (seen_option == 0)
687 printf("%s%s", seen_option++ ? "," : "",
692 printf("%s", seen_option ? ">" : "");
694 if (print_toptype && IFM_INST(ifmw) != 0)
695 printf(" instance %d", IFM_INST(ifmw));
699 print_media_word_ifconfig(int ifmw)
701 struct ifmedia_description *desc;
702 struct ifmedia_type_to_subtype *ttos;
703 int seen_option = 0, i;
705 /* Find the top-level interface type. */
706 desc = get_toptype_desc(ifmw);
707 ttos = get_toptype_ttos(ifmw);
708 if (desc->ifmt_string == NULL) {
709 printf("<unknown type>");
714 * Don't print the top-level type; it's not like we can
715 * change it, or anything.
719 desc = get_subtype_desc(ifmw, ttos);
721 printf("<unknown subtype>");
725 printf("media %s", desc->ifmt_string);
727 desc = get_mode_desc(ifmw, ttos);
729 printf(" mode %s", desc->ifmt_string);
732 for (i = 0; ttos->options[i].desc != NULL; i++) {
733 if (ttos->options[i].alias)
735 for (desc = ttos->options[i].desc;
736 desc->ifmt_string != NULL; desc++) {
737 if (ifmw & desc->ifmt_word) {
738 if (seen_option == 0)
739 printf(" mediaopt ");
740 printf("%s%s", seen_option++ ? "," : "",
746 if (IFM_INST(ifmw) != 0)
747 printf(" instance %d", IFM_INST(ifmw));
750 /**********************************************************************
752 **********************************************************************/