123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- /*
- * Copyright (c) 1983, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- #include <sys/param.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/sysctl.h>
- #include <sys/time.h>
- #include <net/if.h>
- #include <net/if_dl.h>
- #include <net/if_types.h>
- #include <net/if_media.h>
- #include <net/route.h>
- #include <assert.h>
- #include <ctype.h>
- #include <err.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include "libifconfig.h"
- #include "libifconfig_internal.h"
- static const struct ifmedia_description *lookup_media_desc(
- const struct ifmedia_description *, const char *);
- static const struct ifmedia_type_to_subtype *get_toptype_ttos(ifmedia_t);
- #define IFM_OPMODE(x) \
- ((x) & (IFM_IEEE80211_ADHOC | IFM_IEEE80211_HOSTAP | \
- IFM_IEEE80211_IBSS | IFM_IEEE80211_WDS | IFM_IEEE80211_MONITOR | \
- IFM_IEEE80211_MBSS))
- #define IFM_IEEE80211_STA 0
- static const struct ifmedia_description
- ifm_type_descriptions[] =
- IFM_TYPE_DESCRIPTIONS;
- static const struct ifmedia_description
- ifm_subtype_ethernet_descriptions[] =
- IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
- static const struct ifmedia_description
- ifm_subtype_ethernet_aliases[] =
- IFM_SUBTYPE_ETHERNET_ALIASES;
- static const struct ifmedia_description
- ifm_subtype_ethernet_option_descriptions[] =
- IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
- static const struct ifmedia_description
- ifm_subtype_ieee80211_descriptions[] =
- IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
- static const struct ifmedia_description
- ifm_subtype_ieee80211_aliases[] =
- IFM_SUBTYPE_IEEE80211_ALIASES;
- static const struct ifmedia_description
- ifm_subtype_ieee80211_option_descriptions[] =
- IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
- static const struct ifmedia_description
- ifm_subtype_ieee80211_mode_descriptions[] =
- IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
- static const struct ifmedia_description
- ifm_subtype_ieee80211_mode_aliases[] =
- IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
- static const struct ifmedia_description
- ifm_subtype_atm_descriptions[] =
- IFM_SUBTYPE_ATM_DESCRIPTIONS;
- static const struct ifmedia_description
- ifm_subtype_atm_aliases[] =
- IFM_SUBTYPE_ATM_ALIASES;
- static const struct ifmedia_description
- ifm_subtype_atm_option_descriptions[] =
- IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
- static const struct ifmedia_description
- ifm_subtype_shared_descriptions[] =
- IFM_SUBTYPE_SHARED_DESCRIPTIONS;
- static const struct ifmedia_description
- ifm_subtype_shared_aliases[] =
- IFM_SUBTYPE_SHARED_ALIASES;
- static const struct ifmedia_description
- ifm_shared_option_descriptions[] =
- IFM_SHARED_OPTION_DESCRIPTIONS;
- static const struct ifmedia_description
- ifm_shared_option_aliases[] =
- IFM_SHARED_OPTION_ALIASES;
- static const struct ifmedia_description *
- lookup_media_desc(const struct ifmedia_description *desc, const char *name)
- {
- for (; desc->ifmt_string != NULL; ++desc)
- if (strcasecmp(desc->ifmt_string, name) == 0)
- return (desc);
- return (NULL);
- }
- struct ifmedia_type_to_subtype {
- struct {
- const struct ifmedia_description *desc;
- bool alias;
- }
- subtypes[5];
- struct {
- const struct ifmedia_description *desc;
- bool alias;
- }
- options[4];
- struct {
- const struct ifmedia_description *desc;
- bool alias;
- }
- modes[3];
- };
- /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
- static const struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] =
- {
- {
- {
- { &ifm_subtype_shared_descriptions[0], 0 },
- { &ifm_subtype_shared_aliases[0], 1 },
- { &ifm_subtype_ethernet_descriptions[0], 0 },
- { &ifm_subtype_ethernet_aliases[0], 1 },
- { NULL, 0 },
- },
- {
- { &ifm_shared_option_descriptions[0], 0 },
- { &ifm_shared_option_aliases[0], 1 },
- { &ifm_subtype_ethernet_option_descriptions[0], 0 },
- { NULL, 0 },
- },
- {
- { NULL, 0 },
- },
- },
- {
- {
- { &ifm_subtype_shared_descriptions[0], 0 },
- { &ifm_subtype_shared_aliases[0], 1 },
- { &ifm_subtype_ieee80211_descriptions[0], 0 },
- { &ifm_subtype_ieee80211_aliases[0], 1 },
- { NULL, 0 },
- },
- {
- { &ifm_shared_option_descriptions[0], 0 },
- { &ifm_shared_option_aliases[0], 1 },
- { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
- { NULL, 0 },
- },
- {
- { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
- { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
- { NULL, 0 },
- },
- },
- {
- {
- { &ifm_subtype_shared_descriptions[0], 0 },
- { &ifm_subtype_shared_aliases[0], 1 },
- { &ifm_subtype_atm_descriptions[0], 0 },
- { &ifm_subtype_atm_aliases[0], 1 },
- { NULL, 0 },
- },
- {
- { &ifm_shared_option_descriptions[0], 0 },
- { &ifm_shared_option_aliases[0], 1 },
- { &ifm_subtype_atm_option_descriptions[0], 0 },
- { NULL, 0 },
- },
- {
- { NULL, 0 },
- },
- },
- };
- static const struct ifmedia_type_to_subtype *
- get_toptype_ttos(ifmedia_t media)
- {
- const struct ifmedia_description *desc;
- const struct ifmedia_type_to_subtype *ttos;
- for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
- desc->ifmt_string != NULL; desc++, ttos++) {
- if (IFM_TYPE(media) == desc->ifmt_word)
- return (ttos);
- }
- errno = ENOENT;
- return (NULL);
- }
- const char *
- ifconfig_media_get_type(ifmedia_t media)
- {
- const struct ifmedia_description *desc;
- for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; ++desc) {
- if (IFM_TYPE(media) == desc->ifmt_word)
- return (desc->ifmt_string);
- }
- errno = ENOENT;
- return (NULL);
- }
- ifmedia_t
- ifconfig_media_lookup_type(const char *name)
- {
- const struct ifmedia_description *desc;
- desc = lookup_media_desc(ifm_type_descriptions, name);
- return (desc == NULL ? INVALID_IFMEDIA : desc->ifmt_word);
- }
- const char *
- ifconfig_media_get_subtype(ifmedia_t media)
- {
- const struct ifmedia_description *desc;
- const struct ifmedia_type_to_subtype *ttos;
- ttos = get_toptype_ttos(media);
- if (ttos == NULL) {
- errno = EINVAL;
- return (NULL);
- }
- for (size_t i = 0; ttos->subtypes[i].desc != NULL; ++i) {
- if (ttos->subtypes[i].alias)
- continue;
- for (desc = ttos->subtypes[i].desc;
- desc->ifmt_string != NULL; ++desc) {
- if (IFM_SUBTYPE(media) == desc->ifmt_word)
- return (desc->ifmt_string);
- }
- }
- errno = ENOENT;
- return (NULL);
- }
- ifmedia_t
- ifconfig_media_lookup_subtype(ifmedia_t media, const char *name)
- {
- const struct ifmedia_description *desc;
- const struct ifmedia_type_to_subtype *ttos;
- ttos = get_toptype_ttos(media);
- if (ttos == NULL) {
- errno = EINVAL;
- return (INVALID_IFMEDIA);
- }
- for (size_t i = 0; ttos->subtypes[i].desc != NULL; ++i) {
- desc = lookup_media_desc(ttos->subtypes[i].desc, name);
- if (desc != NULL)
- return (desc->ifmt_word);
- }
- errno = ENOENT;
- return (INVALID_IFMEDIA);
- }
- const char *
- ifconfig_media_get_mode(ifmedia_t media)
- {
- const struct ifmedia_description *desc;
- const struct ifmedia_type_to_subtype *ttos;
- ttos = get_toptype_ttos(media);
- if (ttos == NULL) {
- errno = EINVAL;
- return (NULL);
- }
- for (size_t i = 0; ttos->modes[i].desc != NULL; ++i) {
- if (ttos->modes[i].alias)
- continue;
- for (desc = ttos->modes[i].desc;
- desc->ifmt_string != NULL; ++desc) {
- if (IFM_MODE(media) == desc->ifmt_word)
- return (desc->ifmt_string);
- }
- }
- errno = ENOENT;
- return (NULL);
- }
- ifmedia_t
- ifconfig_media_lookup_mode(ifmedia_t media, const char *name)
- {
- const struct ifmedia_description *desc;
- const struct ifmedia_type_to_subtype *ttos;
- ttos = get_toptype_ttos(media);
- if (ttos == NULL) {
- errno = EINVAL;
- return (INVALID_IFMEDIA);
- }
- for (size_t i = 0; ttos->modes[i].desc != NULL; ++i) {
- desc = lookup_media_desc(ttos->modes[i].desc, name);
- if (desc != NULL)
- return (desc->ifmt_word);
- }
- errno = ENOENT;
- return (INVALID_IFMEDIA);
- }
- const char **
- ifconfig_media_get_options(ifmedia_t media)
- {
- const char **options;
- const struct ifmedia_description *desc;
- const struct ifmedia_type_to_subtype *ttos;
- size_t n;
- ttos = get_toptype_ttos(media);
- if (ttos == NULL) {
- errno = EINVAL;
- return (NULL);
- }
- n = 0;
- for (size_t i = 0; ttos->options[i].desc != NULL; ++i) {
- if (ttos->options[i].alias)
- continue;
- for (desc = ttos->options[i].desc;
- desc->ifmt_string != NULL; ++desc) {
- if ((media & desc->ifmt_word) != 0)
- ++n;
- }
- }
- if (n == 0) {
- errno = ENOENT;
- return (NULL);
- }
- options = calloc(n + 1, sizeof(*options));
- if (options == NULL)
- return (NULL);
- options[n] = NULL;
- n = 0;
- for (size_t i = 0; ttos->options[i].desc != NULL; ++i) {
- if (ttos->options[i].alias)
- continue;
- for (desc = ttos->options[i].desc;
- desc->ifmt_string != NULL; ++desc) {
- if ((media & desc->ifmt_word) != 0) {
- options[n] = desc->ifmt_string;
- ++n;
- }
- }
- }
- return (options);
- }
- ifmedia_t *
- ifconfig_media_lookup_options(ifmedia_t media, const char **opts, size_t nopts)
- {
- ifmedia_t *options;
- const struct ifmedia_description *desc, *opt;
- const struct ifmedia_type_to_subtype *ttos;
- assert(opts != NULL);
- assert(nopts > 0);
- ttos = get_toptype_ttos(media);
- if (ttos == NULL) {
- errno = EINVAL;
- return (NULL);
- }
- options = calloc(nopts, sizeof(*options));
- if (options == NULL)
- return (NULL);
- (void)memset(options, INVALID_IFMEDIA, nopts * sizeof(ifmedia_t));
- for (size_t i = 0; ttos->options[i].desc != NULL; ++i) {
- desc = ttos->options[i].desc;
- for (size_t j = 0; j < nopts; ++j) {
- opt = lookup_media_desc(desc, opts[j]);
- if (opt != NULL)
- options[j] = opt->ifmt_word;
- }
- }
- return (options);
- }
- /***************************************************************************
- * Above this point, this file is mostly copied from sbin/ifconfig/ifmedia.c
- ***************************************************************************/
- /* Internal structure used for allocations and frees */
- struct _ifconfig_media_status {
- struct ifmediareq ifmr;
- int medialist[0];
- };
- int
- ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name,
- struct ifmediareq **ifmr)
- {
- struct _ifconfig_media_status *ms, *ms2;
- unsigned long cmd = SIOCGIFXMEDIA;
- *ifmr = NULL;
- ms = calloc(1, sizeof(*ms));
- if (ms == NULL) {
- h->error.errtype = OTHER;
- h->error.errcode = ENOMEM;
- return (-1);
- }
- (void)strlcpy(ms->ifmr.ifm_name, name, sizeof(ms->ifmr.ifm_name));
- /*
- * Check if interface supports extended media types.
- */
- if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms->ifmr) < 0) {
- cmd = SIOCGIFMEDIA;
- if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms->ifmr) < 0) {
- /* Interface doesn't support SIOC{G,S}IFMEDIA. */
- h->error.errtype = OK;
- free(ms);
- return (-1);
- }
- }
- if (ms->ifmr.ifm_count == 0) {
- *ifmr = &ms->ifmr;
- return (0); /* Interface has no media types ?*/
- }
- ms2 = realloc(ms, sizeof(*ms) + sizeof(int) * ms->ifmr.ifm_count);
- if (ms2 == NULL) {
- h->error.errtype = OTHER;
- h->error.errcode = ENOMEM;
- free(ms);
- return (-1);
- }
- ms2->ifmr.ifm_ulist = &ms2->medialist[0];
- if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms2->ifmr) < 0) {
- free(ms2);
- return (-1);
- }
- *ifmr = &ms2->ifmr;
- return (0);
- }
- const char *
- ifconfig_media_get_status(const struct ifmediareq *ifmr)
- {
- switch (IFM_TYPE(ifmr->ifm_active)) {
- case IFM_ETHER:
- case IFM_ATM:
- if (ifmr->ifm_status & IFM_ACTIVE) {
- return ("active");
- } else {
- return ("no carrier");
- }
- break;
- case IFM_IEEE80211:
- if (ifmr->ifm_status & IFM_ACTIVE) {
- /* NB: only sta mode associates */
- if (IFM_OPMODE(ifmr->ifm_active) == IFM_IEEE80211_STA) {
- return ("associated");
- } else {
- return ("running");
- }
- } else {
- return ("no carrier");
- }
- break;
- default:
- return ("");
- }
- }
- int
- ifconfig_media_get_downreason(ifconfig_handle_t *h, const char *name,
- struct ifdownreason *ifdr)
- {
- (void)memset(ifdr, 0, sizeof(*ifdr));
- (void)strlcpy(ifdr->ifdr_name, name, sizeof(ifdr->ifdr_name));
- return (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFDOWNREASON, ifdr));
- }
|