Changeset View
Changeset View
Standalone View
Standalone View
sbin/ifconfig/ifmedia.c
Show First 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | |||||
#include "ifconfig.h" | #include "ifconfig.h" | ||||
static void domediaopt(const char *, int, int); | static void domediaopt(const char *, int, int); | ||||
static int get_media_subtype(int, const char *); | static int get_media_subtype(int, const char *); | ||||
static int get_media_mode(int, const char *); | static int get_media_mode(int, const char *); | ||||
static int get_media_options(int, const char *); | static int get_media_options(int, const char *); | ||||
static int lookup_media_word(struct ifmedia_description *, const char *); | static int lookup_media_word(struct ifmedia_description *, const char *); | ||||
static void print_media_word(int, int); | static void print_media(struct ifmed *, int); | ||||
static void print_media_word_ifconfig(int); | static void print_media_ifconfig(struct ifmed *); | ||||
static struct ifmedia_description *get_toptype_desc(int); | static struct ifmedia_description *get_toptype_desc(struct ifmed *); | ||||
static struct ifmedia_type_to_subtype *get_toptype_ttos(int); | static struct ifmedia_type_to_subtype *get_toptype_ttos(struct ifmed *); | ||||
static struct ifmedia_description *get_subtype_desc(int, | static struct ifmedia_description *get_subtype_desc(struct ifmed *, | ||||
struct ifmedia_type_to_subtype *ttos); | struct ifmedia_type_to_subtype *ttos); | ||||
#define IFM_OPMODE(x) \ | #define IFM_OPMODE(x) \ | ||||
((x) & (IFM_IEEE80211_ADHOC | IFM_IEEE80211_HOSTAP | \ | ((x) & (IFM_IEEE80211_ADHOC | IFM_IEEE80211_HOSTAP | \ | ||||
IFM_IEEE80211_IBSS | IFM_IEEE80211_WDS | IFM_IEEE80211_MONITOR | \ | IFM_IEEE80211_IBSS | IFM_IEEE80211_WDS | IFM_IEEE80211_MONITOR | \ | ||||
IFM_IEEE80211_MBSS)) | IFM_IEEE80211_MBSS)) | ||||
#define IFM_IEEE80211_STA 0 | #define IFM_IEEE80211_STA 0 | ||||
static void | static void | ||||
media_status(int s) | media_status(int s) | ||||
{ | { | ||||
struct ifmediareq ifmr; | struct ifmediareq2 ifmr; | ||||
int *media_list, i; | struct ifmed *medias; | ||||
int i; | |||||
(void) memset(&ifmr, 0, sizeof(ifmr)); | |||||
(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); | (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); | ||||
ifmr.ifm_count = 0; | |||||
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { | if (ioctl(s, SIOCGIFMEDIA2, &ifmr) < 0) { | ||||
/* | /* | ||||
* Interface doesn't support SIOC{G,S}IFMEDIA. | * Interface doesn't support SIOC{G,S}IFMEDIA. | ||||
*/ | */ | ||||
return; | return; | ||||
} | } | ||||
if (ifmr.ifm_count == 0) { | if (ifmr.ifm_count == 0) { | ||||
warnx("%s: no media types?", name); | warnx("%s: no media types?", name); | ||||
return; | return; | ||||
} | } | ||||
media_list = (int *)malloc(ifmr.ifm_count * sizeof(int)); | medias = malloc(ifmr.ifm_count * sizeof(struct ifmed)); | ||||
if (media_list == NULL) | if (medias == NULL) | ||||
err(1, "malloc"); | err(1, "malloc"); | ||||
ifmr.ifm_ulist = media_list; | ifmr.ifm_entries = medias; | ||||
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) | if (ioctl(s, SIOCGIFMEDIA2, &ifmr) < 0) | ||||
err(1, "SIOCGIFMEDIA"); | err(1, "SIOCGIFMEDIA2"); | ||||
printf("\tmedia: "); | printf("\tmedia: "); | ||||
print_media_word(ifmr.ifm_current, 1); | print_media(&medias[ifmr.ifm_current], 1); | ||||
if (ifmr.ifm_active != ifmr.ifm_current) { | if (bcmp(&ifmr.ifm_active, &medias[ifmr.ifm_current], | ||||
sizeof(struct ifmed)) != 0) { | |||||
putchar(' '); | putchar(' '); | ||||
putchar('('); | putchar('('); | ||||
print_media_word(ifmr.ifm_active, 0); | print_media(&ifmr.ifm_active, 0); | ||||
putchar(')'); | putchar(')'); | ||||
} | } | ||||
putchar('\n'); | putchar('\n'); | ||||
if (ifmr.ifm_status & IFM_AVALID) { | if (ifmr.ifm_status & IFM_AVALID) { | ||||
printf("\tstatus: "); | printf("\tstatus: "); | ||||
switch (IFM_TYPE(ifmr.ifm_active)) { | switch (ifmr.ifm_active.ifm_type) { | ||||
case IFM_ETHER: | case IFM_ETHER: | ||||
case IFM_ATM: | case IFM_ATM: | ||||
if (ifmr.ifm_status & IFM_ACTIVE) | if (ifmr.ifm_status & IFM_ACTIVE) | ||||
printf("active"); | printf("active"); | ||||
else | else | ||||
printf("no carrier"); | printf("no carrier"); | ||||
break; | break; | ||||
case IFM_FDDI: | case IFM_FDDI: | ||||
case IFM_TOKEN: | case IFM_TOKEN: | ||||
if (ifmr.ifm_status & IFM_ACTIVE) | if (ifmr.ifm_status & IFM_ACTIVE) | ||||
printf("inserted"); | printf("inserted"); | ||||
else | else | ||||
printf("no ring"); | printf("no ring"); | ||||
break; | break; | ||||
case IFM_IEEE80211: | case IFM_IEEE80211: | ||||
if (ifmr.ifm_status & IFM_ACTIVE) { | if (ifmr.ifm_status & IFM_ACTIVE) { | ||||
/* NB: only sta mode associates */ | /* NB: only sta mode associates */ | ||||
if (IFM_OPMODE(ifmr.ifm_active) == IFM_IEEE80211_STA) | if (IFM_OPMODE(ifmr.ifm_active.ifm_tflags) == | ||||
IFM_IEEE80211_STA) | |||||
printf("associated"); | printf("associated"); | ||||
else | else | ||||
printf("running"); | printf("running"); | ||||
} else | } else | ||||
printf("no carrier"); | printf("no carrier"); | ||||
break; | break; | ||||
} | } | ||||
putchar('\n'); | putchar('\n'); | ||||
} | } | ||||
if (ifmr.ifm_count > 0 && supmedia) { | if (ifmr.ifm_count > 0 && supmedia) { | ||||
printf("\tsupported media:\n"); | printf("\tsupported media:\n"); | ||||
for (i = 0; i < ifmr.ifm_count; i++) { | for (i = 0; i < ifmr.ifm_count; i++) { | ||||
printf("\t\t"); | printf("\t\t"); | ||||
print_media_word_ifconfig(media_list[i]); | print_media_ifconfig(&medias[i]); | ||||
putchar('\n'); | putchar('\n'); | ||||
} | } | ||||
} | } | ||||
free(media_list); | free(medias); | ||||
} | } | ||||
struct ifmediareq * | struct ifmediareq * | ||||
ifmedia_getstate(int s) | ifmedia_getstate(int s) | ||||
{ | { | ||||
static struct ifmediareq *ifmr = NULL; | static struct ifmediareq *ifmr = NULL; | ||||
int *mwords; | int *mwords; | ||||
▲ Show 20 Lines • Show All 427 Lines • ▼ Show 20 Lines | lookup_media_word(struct ifmedia_description *desc, const char *val) | ||||
for (; desc->ifmt_string != NULL; desc++) | for (; desc->ifmt_string != NULL; desc++) | ||||
if (strcasecmp(desc->ifmt_string, val) == 0) | if (strcasecmp(desc->ifmt_string, val) == 0) | ||||
return (desc->ifmt_word); | return (desc->ifmt_word); | ||||
return (-1); | return (-1); | ||||
} | } | ||||
static struct ifmedia_description *get_toptype_desc(int ifmw) | static struct ifmedia_description * | ||||
get_toptype_desc(struct ifmed *media) | |||||
{ | { | ||||
struct ifmedia_description *desc; | struct ifmedia_description *desc; | ||||
for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++) | for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++) | ||||
if (IFM_TYPE(ifmw) == desc->ifmt_word) | if (media->ifm_type == desc->ifmt_word) | ||||
break; | break; | ||||
return desc; | return desc; | ||||
} | } | ||||
static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw) | static struct ifmedia_type_to_subtype * | ||||
get_toptype_ttos(struct ifmed *media) | |||||
{ | { | ||||
struct ifmedia_description *desc; | struct ifmedia_description *desc; | ||||
struct ifmedia_type_to_subtype *ttos; | struct ifmedia_type_to_subtype *ttos; | ||||
for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; | for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; | ||||
desc->ifmt_string != NULL; desc++, ttos++) | desc->ifmt_string != NULL; desc++, ttos++) | ||||
if (IFM_TYPE(ifmw) == desc->ifmt_word) | if (media->ifm_type == desc->ifmt_word) | ||||
break; | break; | ||||
return ttos; | return ttos; | ||||
} | } | ||||
static struct ifmedia_description *get_subtype_desc(int ifmw, | static struct ifmedia_description * | ||||
struct ifmedia_type_to_subtype *ttos) | get_subtype_desc(struct ifmed *media, struct ifmedia_type_to_subtype *ttos) | ||||
{ | { | ||||
int i; | int i; | ||||
struct ifmedia_description *desc; | struct ifmedia_description *desc; | ||||
for (i = 0; ttos->subtypes[i].desc != NULL; i++) { | for (i = 0; ttos->subtypes[i].desc != NULL; i++) { | ||||
if (ttos->subtypes[i].alias) | if (ttos->subtypes[i].alias) | ||||
continue; | continue; | ||||
for (desc = ttos->subtypes[i].desc; | for (desc = ttos->subtypes[i].desc; | ||||
desc->ifmt_string != NULL; desc++) { | desc->ifmt_string != NULL; desc++) { | ||||
if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) | if (media->ifm_variant == desc->ifmt_word) | ||||
return desc; | return desc; | ||||
} | } | ||||
} | } | ||||
return NULL; | return NULL; | ||||
} | } | ||||
static struct ifmedia_description *get_mode_desc(int ifmw, | static struct ifmedia_description * | ||||
struct ifmedia_type_to_subtype *ttos) | get_mode_desc(struct ifmed *media, struct ifmedia_type_to_subtype *ttos) | ||||
{ | { | ||||
int i; | int i; | ||||
struct ifmedia_description *desc; | struct ifmedia_description *desc; | ||||
for (i = 0; ttos->modes[i].desc != NULL; i++) { | for (i = 0; ttos->modes[i].desc != NULL; i++) { | ||||
if (ttos->modes[i].alias) | if (ttos->modes[i].alias) | ||||
continue; | continue; | ||||
for (desc = ttos->modes[i].desc; | for (desc = ttos->modes[i].desc; | ||||
desc->ifmt_string != NULL; desc++) { | desc->ifmt_string != NULL; desc++) { | ||||
if (IFM_MODE(ifmw) == desc->ifmt_word) | if (media->ifm_tflags == desc->ifmt_word) | ||||
return desc; | return desc; | ||||
} | } | ||||
} | } | ||||
return NULL; | return NULL; | ||||
} | } | ||||
static void | static void | ||||
print_media_word(int ifmw, int print_toptype) | print_media(struct ifmed *media, int print_toptype) | ||||
{ | { | ||||
struct ifmedia_description *desc; | struct ifmedia_description *desc; | ||||
struct ifmedia_type_to_subtype *ttos; | struct ifmedia_type_to_subtype *ttos; | ||||
int seen_option = 0, i; | int seen_option = 0, i; | ||||
/* Find the top-level interface type. */ | /* Find the top-level interface type. */ | ||||
desc = get_toptype_desc(ifmw); | desc = get_toptype_desc(media); | ||||
ttos = get_toptype_ttos(ifmw); | ttos = get_toptype_ttos(media); | ||||
if (desc->ifmt_string == NULL) { | if (desc->ifmt_string == NULL) { | ||||
printf("<unknown type>"); | printf("<unknown type>"); | ||||
return; | return; | ||||
} else if (print_toptype) { | } else if (print_toptype) { | ||||
printf("%s", desc->ifmt_string); | printf("%s", desc->ifmt_string); | ||||
} | } | ||||
/* | /* | ||||
* Don't print the top-level type; it's not like we can | * Don't print the top-level type; it's not like we can | ||||
* change it, or anything. | * change it, or anything. | ||||
*/ | */ | ||||
/* Find subtype. */ | /* Find subtype. */ | ||||
desc = get_subtype_desc(ifmw, ttos); | desc = get_subtype_desc(media, ttos); | ||||
if (desc == NULL) { | if (desc == NULL) { | ||||
printf("<unknown subtype>"); | printf("<unknown subtype>"); | ||||
return; | return; | ||||
} | } | ||||
if (print_toptype) | if (print_toptype) | ||||
putchar(' '); | putchar(' '); | ||||
printf("%s", desc->ifmt_string); | printf("%s", desc->ifmt_string); | ||||
if (print_toptype) { | if (print_toptype) { | ||||
desc = get_mode_desc(ifmw, ttos); | desc = get_mode_desc(media, ttos); | ||||
if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string)) | if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string)) | ||||
printf(" mode %s", desc->ifmt_string); | printf(" mode %s", desc->ifmt_string); | ||||
} | } | ||||
/* Find options. */ | /* Find options. */ | ||||
for (i = 0; ttos->options[i].desc != NULL; i++) { | for (i = 0; ttos->options[i].desc != NULL; i++) { | ||||
if (ttos->options[i].alias) | if (ttos->options[i].alias) | ||||
continue; | continue; | ||||
for (desc = ttos->options[i].desc; | for (desc = ttos->options[i].desc; | ||||
desc->ifmt_string != NULL; desc++) { | desc->ifmt_string != NULL; desc++) { | ||||
if (ifmw & desc->ifmt_word) { | if (media->ifm_flags & desc->ifmt_word) { | ||||
if (seen_option == 0) | if (seen_option == 0) | ||||
printf(" <"); | printf(" <"); | ||||
printf("%s%s", seen_option++ ? "," : "", | printf("%s%s", seen_option++ ? "," : "", | ||||
desc->ifmt_string); | desc->ifmt_string); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
printf("%s", seen_option ? ">" : ""); | printf("%s", seen_option ? ">" : ""); | ||||
if (print_toptype && IFM_INST(ifmw) != 0) | if (print_toptype && media->ifm_instance != 0) | ||||
printf(" instance %d", IFM_INST(ifmw)); | printf(" instance %u", media->ifm_instance); | ||||
} | } | ||||
static void | static void | ||||
print_media_word_ifconfig(int ifmw) | print_media_ifconfig(struct ifmed *media) | ||||
{ | { | ||||
struct ifmedia_description *desc; | struct ifmedia_description *desc; | ||||
struct ifmedia_type_to_subtype *ttos; | struct ifmedia_type_to_subtype *ttos; | ||||
int seen_option = 0, i; | int seen_option = 0, i; | ||||
/* Find the top-level interface type. */ | /* Find the top-level interface type. */ | ||||
desc = get_toptype_desc(ifmw); | desc = get_toptype_desc(media); | ||||
ttos = get_toptype_ttos(ifmw); | ttos = get_toptype_ttos(media); | ||||
if (desc->ifmt_string == NULL) { | if (desc->ifmt_string == NULL) { | ||||
printf("<unknown type>"); | printf("<unknown type>"); | ||||
return; | return; | ||||
} | } | ||||
/* | /* | ||||
* Don't print the top-level type; it's not like we can | * Don't print the top-level type; it's not like we can | ||||
* change it, or anything. | * change it, or anything. | ||||
*/ | */ | ||||
/* Find subtype. */ | /* Find subtype. */ | ||||
desc = get_subtype_desc(ifmw, ttos); | desc = get_subtype_desc(media, ttos); | ||||
if (desc == NULL) { | if (desc == NULL) { | ||||
printf("<unknown subtype>"); | printf("<unknown subtype>"); | ||||
return; | return; | ||||
} | } | ||||
printf("media %s", desc->ifmt_string); | printf("media %s", desc->ifmt_string); | ||||
desc = get_mode_desc(ifmw, ttos); | desc = get_mode_desc(media, ttos); | ||||
if (desc != NULL) | if (desc != NULL) | ||||
printf(" mode %s", desc->ifmt_string); | printf(" mode %s", desc->ifmt_string); | ||||
/* Find options. */ | /* Find options. */ | ||||
for (i = 0; ttos->options[i].desc != NULL; i++) { | for (i = 0; ttos->options[i].desc != NULL; i++) { | ||||
if (ttos->options[i].alias) | if (ttos->options[i].alias) | ||||
continue; | continue; | ||||
for (desc = ttos->options[i].desc; | for (desc = ttos->options[i].desc; | ||||
desc->ifmt_string != NULL; desc++) { | desc->ifmt_string != NULL; desc++) { | ||||
if (ifmw & desc->ifmt_word) { | if (media->ifm_flags & desc->ifmt_word) { | ||||
if (seen_option == 0) | if (seen_option == 0) | ||||
printf(" mediaopt "); | printf(" mediaopt "); | ||||
printf("%s%s", seen_option++ ? "," : "", | printf("%s%s", seen_option++ ? "," : "", | ||||
desc->ifmt_string); | desc->ifmt_string); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (IFM_INST(ifmw) != 0) | if (media->ifm_instance != 0) | ||||
printf(" instance %d", IFM_INST(ifmw)); | printf(" instance %u", media->ifm_instance); | ||||
} | } | ||||
/********************************************************************** | /********************************************************************** | ||||
* ...until here. | * ...until here. | ||||
**********************************************************************/ | **********************************************************************/ | ||||
static struct cmd media_cmds[] = { | static struct cmd media_cmds[] = { | ||||
DEF_CMD_ARG("media", setmedia), | DEF_CMD_ARG("media", setmedia), | ||||
Show All 23 Lines |