Home | History | Annotate | Line # | Download | only in ifconfig
media.c revision 1.3.4.2
      1  1.3.4.2  mjf #include <sys/cdefs.h>
      2  1.3.4.2  mjf #ifndef lint
      3  1.3.4.2  mjf __RCSID("$NetBSD: media.c,v 1.3.4.2 2008/09/28 11:17:11 mjf Exp $");
      4  1.3.4.2  mjf #endif /* not lint */
      5  1.3.4.2  mjf 
      6  1.3.4.2  mjf #include <assert.h>
      7  1.3.4.2  mjf #include <err.h>
      8  1.3.4.2  mjf #include <errno.h>
      9  1.3.4.2  mjf #include <stdio.h>
     10  1.3.4.2  mjf #include <stdlib.h>
     11  1.3.4.2  mjf #include <string.h>
     12  1.3.4.2  mjf #include <util.h>
     13  1.3.4.2  mjf 
     14  1.3.4.2  mjf #include <sys/ioctl.h>
     15  1.3.4.2  mjf 
     16  1.3.4.2  mjf #include <net/if.h>
     17  1.3.4.2  mjf #include <net/if_dl.h>
     18  1.3.4.2  mjf #include <net/if_media.h>
     19  1.3.4.2  mjf 
     20  1.3.4.2  mjf #include <prop/proplib.h>
     21  1.3.4.2  mjf 
     22  1.3.4.2  mjf #include "env.h"
     23  1.3.4.2  mjf #include "extern.h"
     24  1.3.4.2  mjf #include "media.h"
     25  1.3.4.2  mjf #include "parse.h"
     26  1.3.4.2  mjf #include "util.h"
     27  1.3.4.2  mjf 
     28  1.3.4.2  mjf static void init_current_media(prop_dictionary_t, prop_dictionary_t);
     29  1.3.4.2  mjf static void media_constructor(void) __attribute__((constructor));
     30  1.3.4.2  mjf static int setmedia(prop_dictionary_t, prop_dictionary_t);
     31  1.3.4.2  mjf static int setmediainst(prop_dictionary_t, prop_dictionary_t);
     32  1.3.4.2  mjf static int setmediamode(prop_dictionary_t, prop_dictionary_t);
     33  1.3.4.2  mjf static int setmediaopt(prop_dictionary_t, prop_dictionary_t);
     34  1.3.4.2  mjf static int unsetmediaopt(prop_dictionary_t, prop_dictionary_t);
     35  1.3.4.2  mjf 
     36  1.3.4.2  mjf /*
     37  1.3.4.2  mjf  * Media stuff.  Whenever a media command is first performed, the
     38  1.3.4.2  mjf  * currently select media is grabbed for this interface.  If `media'
     39  1.3.4.2  mjf  * is given, the current media word is modifed.  `mediaopt' commands
     40  1.3.4.2  mjf  * only modify the set and clear words.  They then operate on the
     41  1.3.4.2  mjf  * current media word later.
     42  1.3.4.2  mjf  */
     43  1.3.4.2  mjf static int	media_current;
     44  1.3.4.2  mjf static int	mediaopt_set;
     45  1.3.4.2  mjf static int	mediaopt_clear;
     46  1.3.4.2  mjf 
     47  1.3.4.2  mjf static struct usage_func usage;
     48  1.3.4.2  mjf 
     49  1.3.4.2  mjf static const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
     50  1.3.4.2  mjf 
     51  1.3.4.2  mjf static const struct ifmedia_status_description ifm_status_descriptions[] =
     52  1.3.4.2  mjf     IFM_STATUS_DESCRIPTIONS;
     53  1.3.4.2  mjf 
     54  1.3.4.2  mjf static struct pstr mediamode = PSTR_INITIALIZER(&mediamode, "mediamode",
     55  1.3.4.2  mjf     setmediamode, "mediamode", &command_root.pb_parser);
     56  1.3.4.2  mjf 
     57  1.3.4.2  mjf static struct pinteger mediainst = PINTEGER_INITIALIZER1(&mediainst,
     58  1.3.4.2  mjf     "mediainst", 0, IFM_INST_MAX, 10, setmediainst, "mediainst",
     59  1.3.4.2  mjf     &command_root.pb_parser);
     60  1.3.4.2  mjf 
     61  1.3.4.2  mjf static struct pstr unmediaopt = PSTR_INITIALIZER(&unmediaopt, "-mediaopt",
     62  1.3.4.2  mjf     unsetmediaopt, "unmediaopt", &command_root.pb_parser);
     63  1.3.4.2  mjf 
     64  1.3.4.2  mjf static struct pstr mediaopt = PSTR_INITIALIZER(&mediaopt, "mediaopt",
     65  1.3.4.2  mjf     setmediaopt, "mediaopt", &command_root.pb_parser);
     66  1.3.4.2  mjf 
     67  1.3.4.2  mjf static struct pstr media = PSTR_INITIALIZER(&media, "media", setmedia, "media",
     68  1.3.4.2  mjf     &command_root.pb_parser);
     69  1.3.4.2  mjf 
     70  1.3.4.2  mjf static const struct kwinst mediakw[] = {
     71  1.3.4.2  mjf 	  {.k_word = "instance", .k_key = "anymedia", .k_type = KW_T_BOOL,
     72  1.3.4.2  mjf 	   .k_bool = true, .k_act = "media", .k_deact = "mediainst",
     73  1.3.4.2  mjf 	   .k_nextparser = &mediainst.pi_parser}
     74  1.3.4.2  mjf 	, {.k_word = "inst", .k_key = "anymedia", .k_type = KW_T_BOOL,
     75  1.3.4.2  mjf 	   .k_bool = true, .k_act = "media", .k_deact = "mediainst",
     76  1.3.4.2  mjf 	   .k_nextparser = &mediainst.pi_parser}
     77  1.3.4.2  mjf 	, {.k_word = "media", .k_key = "anymedia", .k_type = KW_T_BOOL,
     78  1.3.4.2  mjf 	   .k_bool = true, .k_deact = "media", .k_altdeact = "anymedia",
     79  1.3.4.2  mjf 	   .k_nextparser = &media.ps_parser}
     80  1.3.4.2  mjf 	, {.k_word = "mediaopt", .k_key = "anymedia", .k_type = KW_T_BOOL,
     81  1.3.4.2  mjf 	   .k_bool = true, .k_deact = "mediaopt", .k_altdeact = "instance",
     82  1.3.4.2  mjf 	   .k_nextparser = &mediaopt.ps_parser}
     83  1.3.4.2  mjf 	, {.k_word = "-mediaopt", .k_key = "anymedia", .k_type = KW_T_BOOL,
     84  1.3.4.2  mjf 	   .k_bool = true, .k_deact = "unmediaopt", .k_altdeact = "media",
     85  1.3.4.2  mjf 	   .k_nextparser = &unmediaopt.ps_parser}
     86  1.3.4.2  mjf 	, {.k_word = "mode", .k_key = "anymedia", .k_type = KW_T_BOOL,
     87  1.3.4.2  mjf 	   .k_bool = true, .k_deact = "mode",
     88  1.3.4.2  mjf 	   .k_nextparser = &mediamode.ps_parser}
     89  1.3.4.2  mjf };
     90  1.3.4.2  mjf 
     91  1.3.4.2  mjf struct pkw kwmedia = PKW_INITIALIZER(&kwmedia, "media keywords", NULL, NULL,
     92  1.3.4.2  mjf     mediakw, __arraycount(mediakw), NULL);
     93  1.3.4.2  mjf 
     94  1.3.4.2  mjf static void
     95  1.3.4.2  mjf media_error(int type, const char *val, const char *opt)
     96  1.3.4.2  mjf {
     97  1.3.4.2  mjf 	errx(EXIT_FAILURE, "unknown %s media %s: %s",
     98  1.3.4.2  mjf 		get_media_type_string(type), opt, val);
     99  1.3.4.2  mjf }
    100  1.3.4.2  mjf 
    101  1.3.4.2  mjf void
    102  1.3.4.2  mjf init_current_media(prop_dictionary_t env, prop_dictionary_t oenv)
    103  1.3.4.2  mjf {
    104  1.3.4.2  mjf 	const char *ifname;
    105  1.3.4.2  mjf 	struct ifmediareq ifmr;
    106  1.3.4.2  mjf 
    107  1.3.4.2  mjf 	if ((ifname = getifname(env)) == NULL)
    108  1.3.4.2  mjf 		err(EXIT_FAILURE, "getifname");
    109  1.3.4.2  mjf 
    110  1.3.4.2  mjf 	/*
    111  1.3.4.2  mjf 	 * If we have not yet done so, grab the currently-selected
    112  1.3.4.2  mjf 	 * media.
    113  1.3.4.2  mjf 	 */
    114  1.3.4.2  mjf 
    115  1.3.4.2  mjf 	if (prop_dictionary_get(env, "initmedia") == NULL) {
    116  1.3.4.2  mjf 		memset(&ifmr, 0, sizeof(ifmr));
    117  1.3.4.2  mjf 
    118  1.3.4.2  mjf 		if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1) {
    119  1.3.4.2  mjf 			/*
    120  1.3.4.2  mjf 			 * If we get E2BIG, the kernel is telling us
    121  1.3.4.2  mjf 			 * that there are more, so we can ignore it.
    122  1.3.4.2  mjf 			 */
    123  1.3.4.2  mjf 			if (errno != E2BIG)
    124  1.3.4.2  mjf 				err(EXIT_FAILURE, "SIOCGIFMEDIA");
    125  1.3.4.2  mjf 		}
    126  1.3.4.2  mjf 
    127  1.3.4.2  mjf 		if (!prop_dictionary_set_bool(oenv, "initmedia", true)) {
    128  1.3.4.2  mjf 			err(EXIT_FAILURE, "%s: prop_dictionary_set_bool",
    129  1.3.4.2  mjf 			    __func__);
    130  1.3.4.2  mjf 		}
    131  1.3.4.2  mjf 		media_current = ifmr.ifm_current;
    132  1.3.4.2  mjf 	}
    133  1.3.4.2  mjf 
    134  1.3.4.2  mjf 	/* Sanity. */
    135  1.3.4.2  mjf 	if (IFM_TYPE(media_current) == 0)
    136  1.3.4.2  mjf 		errx(EXIT_FAILURE, "%s: no link type?", ifname);
    137  1.3.4.2  mjf }
    138  1.3.4.2  mjf 
    139  1.3.4.2  mjf void
    140  1.3.4.2  mjf process_media_commands(prop_dictionary_t env)
    141  1.3.4.2  mjf {
    142  1.3.4.2  mjf 	struct ifreq ifr;
    143  1.3.4.2  mjf 
    144  1.3.4.2  mjf 	if (prop_dictionary_get(env, "media") == NULL &&
    145  1.3.4.2  mjf 	    prop_dictionary_get(env, "mediaopt") == NULL &&
    146  1.3.4.2  mjf 	    prop_dictionary_get(env, "unmediaopt") == NULL &&
    147  1.3.4.2  mjf 	    prop_dictionary_get(env, "mediamode") == NULL) {
    148  1.3.4.2  mjf 		/* Nothing to do. */
    149  1.3.4.2  mjf 		return;
    150  1.3.4.2  mjf 	}
    151  1.3.4.2  mjf 
    152  1.3.4.2  mjf 	/*
    153  1.3.4.2  mjf 	 * Media already set up, and commands sanity-checked.  Set/clear
    154  1.3.4.2  mjf 	 * any options, and we're ready to go.
    155  1.3.4.2  mjf 	 */
    156  1.3.4.2  mjf 	media_current |= mediaopt_set;
    157  1.3.4.2  mjf 	media_current &= ~mediaopt_clear;
    158  1.3.4.2  mjf 
    159  1.3.4.2  mjf 	memset(&ifr, 0, sizeof(ifr));
    160  1.3.4.2  mjf 	ifr.ifr_media = media_current;
    161  1.3.4.2  mjf 
    162  1.3.4.2  mjf 	if (direct_ioctl(env, SIOCSIFMEDIA, &ifr) == -1)
    163  1.3.4.2  mjf 		err(EXIT_FAILURE, "SIOCSIFMEDIA");
    164  1.3.4.2  mjf }
    165  1.3.4.2  mjf 
    166  1.3.4.2  mjf static int
    167  1.3.4.2  mjf setmedia(prop_dictionary_t env, prop_dictionary_t oenv)
    168  1.3.4.2  mjf {
    169  1.3.4.2  mjf 	int type, subtype, inst;
    170  1.3.4.2  mjf 	prop_data_t data;
    171  1.3.4.2  mjf 	char *val;
    172  1.3.4.2  mjf 
    173  1.3.4.2  mjf 	init_current_media(env, oenv);
    174  1.3.4.2  mjf 
    175  1.3.4.2  mjf 	data = (prop_data_t)prop_dictionary_get(env, "media");
    176  1.3.4.2  mjf 	assert(data != NULL);
    177  1.3.4.2  mjf 
    178  1.3.4.2  mjf 	/* Only one media command may be given. */
    179  1.3.4.2  mjf 	/* Must not come after mode commands */
    180  1.3.4.2  mjf 	/* Must not come after mediaopt commands */
    181  1.3.4.2  mjf 
    182  1.3.4.2  mjf 	/*
    183  1.3.4.2  mjf 	 * No need to check if `instance' has been issued; setmediainst()
    184  1.3.4.2  mjf 	 * craps out if `media' has not been specified.
    185  1.3.4.2  mjf 	 */
    186  1.3.4.2  mjf 
    187  1.3.4.2  mjf 	type = IFM_TYPE(media_current);
    188  1.3.4.2  mjf 	inst = IFM_INST(media_current);
    189  1.3.4.2  mjf 
    190  1.3.4.2  mjf 	val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
    191  1.3.4.2  mjf 	if (val == NULL)
    192  1.3.4.2  mjf 		return -1;
    193  1.3.4.2  mjf 
    194  1.3.4.2  mjf 	/* Look up the subtype. */
    195  1.3.4.2  mjf 	subtype = get_media_subtype(type, val);
    196  1.3.4.2  mjf 	if (subtype == -1)
    197  1.3.4.2  mjf 		media_error(type, val, "subtype");
    198  1.3.4.2  mjf 
    199  1.3.4.2  mjf 	/* Build the new current media word. */
    200  1.3.4.2  mjf 	media_current = IFM_MAKEWORD(type, subtype, 0, inst);
    201  1.3.4.2  mjf 
    202  1.3.4.2  mjf 	/* Media will be set after other processing is complete. */
    203  1.3.4.2  mjf 	return 0;
    204  1.3.4.2  mjf }
    205  1.3.4.2  mjf 
    206  1.3.4.2  mjf static int
    207  1.3.4.2  mjf setmediaopt(prop_dictionary_t env, prop_dictionary_t oenv)
    208  1.3.4.2  mjf {
    209  1.3.4.2  mjf 	char *invalid;
    210  1.3.4.2  mjf 	prop_data_t data;
    211  1.3.4.2  mjf 	char *val;
    212  1.3.4.2  mjf 
    213  1.3.4.2  mjf 	init_current_media(env, oenv);
    214  1.3.4.2  mjf 
    215  1.3.4.2  mjf 	data = (prop_data_t)prop_dictionary_get(env, "mediaopt");
    216  1.3.4.2  mjf 	assert(data != NULL);
    217  1.3.4.2  mjf 
    218  1.3.4.2  mjf 	/* Can only issue `mediaopt' once. */
    219  1.3.4.2  mjf 	/* Can't issue `mediaopt' if `instance' has already been issued. */
    220  1.3.4.2  mjf 
    221  1.3.4.2  mjf 	val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
    222  1.3.4.2  mjf 	if (val == NULL)
    223  1.3.4.2  mjf 		return -1;
    224  1.3.4.2  mjf 
    225  1.3.4.2  mjf 	mediaopt_set = get_media_options(media_current, val, &invalid);
    226  1.3.4.2  mjf 	free(val);
    227  1.3.4.2  mjf 	if (mediaopt_set == -1)
    228  1.3.4.2  mjf 		media_error(media_current, invalid, "option");
    229  1.3.4.2  mjf 
    230  1.3.4.2  mjf 	/* Media will be set after other processing is complete. */
    231  1.3.4.2  mjf 	return 0;
    232  1.3.4.2  mjf }
    233  1.3.4.2  mjf 
    234  1.3.4.2  mjf static int
    235  1.3.4.2  mjf unsetmediaopt(prop_dictionary_t env, prop_dictionary_t oenv)
    236  1.3.4.2  mjf {
    237  1.3.4.2  mjf 	char *invalid, *val;
    238  1.3.4.2  mjf 	prop_data_t data;
    239  1.3.4.2  mjf 
    240  1.3.4.2  mjf 	init_current_media(env, oenv);
    241  1.3.4.2  mjf 
    242  1.3.4.2  mjf 	data = (prop_data_t)prop_dictionary_get(env, "unmediaopt");
    243  1.3.4.2  mjf 	if (data == NULL) {
    244  1.3.4.2  mjf 		errno = ENOENT;
    245  1.3.4.2  mjf 		return -1;
    246  1.3.4.2  mjf 	}
    247  1.3.4.2  mjf 
    248  1.3.4.2  mjf 	val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
    249  1.3.4.2  mjf 	if (val == NULL)
    250  1.3.4.2  mjf 		return -1;
    251  1.3.4.2  mjf 
    252  1.3.4.2  mjf 	/*
    253  1.3.4.2  mjf 	 * No need to check for A_MEDIAINST, since the test for A_MEDIA
    254  1.3.4.2  mjf 	 * implicitly checks for A_MEDIAINST.
    255  1.3.4.2  mjf 	 */
    256  1.3.4.2  mjf 
    257  1.3.4.2  mjf 	mediaopt_clear = get_media_options(media_current, val, &invalid);
    258  1.3.4.2  mjf 	free(val);
    259  1.3.4.2  mjf 	if (mediaopt_clear == -1)
    260  1.3.4.2  mjf 		media_error(media_current, invalid, "option");
    261  1.3.4.2  mjf 
    262  1.3.4.2  mjf 	/* Media will be set after other processing is complete. */
    263  1.3.4.2  mjf 	return 0;
    264  1.3.4.2  mjf }
    265  1.3.4.2  mjf 
    266  1.3.4.2  mjf static int
    267  1.3.4.2  mjf setmediainst(prop_dictionary_t env, prop_dictionary_t oenv)
    268  1.3.4.2  mjf {
    269  1.3.4.2  mjf 	int type, subtype, options;
    270  1.3.4.2  mjf 	int64_t inst;
    271  1.3.4.2  mjf 	bool rc;
    272  1.3.4.2  mjf 
    273  1.3.4.2  mjf 	init_current_media(env, oenv);
    274  1.3.4.2  mjf 
    275  1.3.4.2  mjf 	rc = prop_dictionary_get_int64(env, "mediainst", &inst);
    276  1.3.4.2  mjf 	assert(rc);
    277  1.3.4.2  mjf 
    278  1.3.4.2  mjf 	/* Can only issue `instance' once. */
    279  1.3.4.2  mjf 	/* Must have already specified `media' */
    280  1.3.4.2  mjf 
    281  1.3.4.2  mjf 	type = IFM_TYPE(media_current);
    282  1.3.4.2  mjf 	subtype = IFM_SUBTYPE(media_current);
    283  1.3.4.2  mjf 	options = IFM_OPTIONS(media_current);
    284  1.3.4.2  mjf 
    285  1.3.4.2  mjf 	media_current = IFM_MAKEWORD(type, subtype, options, inst);
    286  1.3.4.2  mjf 
    287  1.3.4.2  mjf 	/* Media will be set after other processing is complete. */
    288  1.3.4.2  mjf 	return 0;
    289  1.3.4.2  mjf }
    290  1.3.4.2  mjf 
    291  1.3.4.2  mjf static int
    292  1.3.4.2  mjf setmediamode(prop_dictionary_t env, prop_dictionary_t oenv)
    293  1.3.4.2  mjf {
    294  1.3.4.2  mjf 	int type, subtype, options, inst, mode;
    295  1.3.4.2  mjf 	prop_data_t data;
    296  1.3.4.2  mjf 	char *val;
    297  1.3.4.2  mjf 
    298  1.3.4.2  mjf 	init_current_media(env, oenv);
    299  1.3.4.2  mjf 
    300  1.3.4.2  mjf 	data = (prop_data_t)prop_dictionary_get(env, "mediamode");
    301  1.3.4.2  mjf 	assert(data != NULL);
    302  1.3.4.2  mjf 
    303  1.3.4.2  mjf 	type = IFM_TYPE(media_current);
    304  1.3.4.2  mjf 	subtype = IFM_SUBTYPE(media_current);
    305  1.3.4.2  mjf 	options = IFM_OPTIONS(media_current);
    306  1.3.4.2  mjf 	inst = IFM_INST(media_current);
    307  1.3.4.2  mjf 
    308  1.3.4.2  mjf 	val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
    309  1.3.4.2  mjf 	if (val == NULL)
    310  1.3.4.2  mjf 		return -1;
    311  1.3.4.2  mjf 
    312  1.3.4.2  mjf 	mode = get_media_mode(type, val);
    313  1.3.4.2  mjf 	if (mode == -1)
    314  1.3.4.2  mjf 		media_error(type, val, "mode");
    315  1.3.4.2  mjf 
    316  1.3.4.2  mjf 	free(val);
    317  1.3.4.2  mjf 
    318  1.3.4.2  mjf 	media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
    319  1.3.4.2  mjf 
    320  1.3.4.2  mjf 	/* Media will be set after other processing is complete. */
    321  1.3.4.2  mjf 	return 0;
    322  1.3.4.2  mjf }
    323  1.3.4.2  mjf 
    324  1.3.4.2  mjf void
    325  1.3.4.2  mjf print_media_word(int ifmw, const char *opt_sep)
    326  1.3.4.2  mjf {
    327  1.3.4.2  mjf 	const char *str;
    328  1.3.4.2  mjf 
    329  1.3.4.2  mjf 	printf("%s", get_media_subtype_string(ifmw));
    330  1.3.4.2  mjf 
    331  1.3.4.2  mjf 	/* Find mode. */
    332  1.3.4.2  mjf 	if (IFM_MODE(ifmw) != 0) {
    333  1.3.4.2  mjf 		str = get_media_mode_string(ifmw);
    334  1.3.4.2  mjf 		if (str != NULL)
    335  1.3.4.2  mjf 			printf(" mode %s", str);
    336  1.3.4.2  mjf 	}
    337  1.3.4.2  mjf 
    338  1.3.4.2  mjf 	/* Find options. */
    339  1.3.4.2  mjf 	for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
    340  1.3.4.2  mjf 		printf("%s%s", opt_sep, str);
    341  1.3.4.2  mjf 
    342  1.3.4.2  mjf 	if (IFM_INST(ifmw) != 0)
    343  1.3.4.2  mjf 		printf(" instance %d", IFM_INST(ifmw));
    344  1.3.4.2  mjf }
    345  1.3.4.2  mjf 
    346  1.3.4.2  mjf void
    347  1.3.4.2  mjf media_status(prop_dictionary_t env, prop_dictionary_t oenv)
    348  1.3.4.2  mjf {
    349  1.3.4.2  mjf 	struct ifmediareq ifmr;
    350  1.3.4.2  mjf 	int af, i, s;
    351  1.3.4.2  mjf 	int *media_list;
    352  1.3.4.2  mjf 	const char *ifname;
    353  1.3.4.2  mjf 
    354  1.3.4.2  mjf 	if ((ifname = getifname(env)) == NULL)
    355  1.3.4.2  mjf 		err(EXIT_FAILURE, "getifname");
    356  1.3.4.2  mjf 	if ((af = getaf(env)) == -1)
    357  1.3.4.2  mjf 		af = AF_UNSPEC;
    358  1.3.4.2  mjf 
    359  1.3.4.2  mjf 	/* get out early if the family is unsupported by the kernel */
    360  1.3.4.2  mjf 	if ((s = getsock(af)) == -1)
    361  1.3.4.2  mjf 		err(EXIT_FAILURE, "%s: getsock", __func__);
    362  1.3.4.2  mjf 
    363  1.3.4.2  mjf 	memset(&ifmr, 0, sizeof(ifmr));
    364  1.3.4.2  mjf 	estrlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
    365  1.3.4.2  mjf 
    366  1.3.4.2  mjf 	if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
    367  1.3.4.2  mjf 		/*
    368  1.3.4.2  mjf 		 * Interface doesn't support SIOC{G,S}IFMEDIA.
    369  1.3.4.2  mjf 		 */
    370  1.3.4.2  mjf 		return;
    371  1.3.4.2  mjf 	}
    372  1.3.4.2  mjf 
    373  1.3.4.2  mjf 	if (ifmr.ifm_count == 0) {
    374  1.3.4.2  mjf 		warnx("%s: no media types?", ifname);
    375  1.3.4.2  mjf 		return;
    376  1.3.4.2  mjf 	}
    377  1.3.4.2  mjf 
    378  1.3.4.2  mjf 	media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
    379  1.3.4.2  mjf 	if (media_list == NULL)
    380  1.3.4.2  mjf 		err(EXIT_FAILURE, "malloc");
    381  1.3.4.2  mjf 	ifmr.ifm_ulist = media_list;
    382  1.3.4.2  mjf 
    383  1.3.4.2  mjf 	if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
    384  1.3.4.2  mjf 		err(EXIT_FAILURE, "SIOCGIFMEDIA");
    385  1.3.4.2  mjf 
    386  1.3.4.2  mjf 	printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
    387  1.3.4.2  mjf 	print_media_word(ifmr.ifm_current, " ");
    388  1.3.4.2  mjf 	if (ifmr.ifm_active != ifmr.ifm_current) {
    389  1.3.4.2  mjf 		printf(" (");
    390  1.3.4.2  mjf 		print_media_word(ifmr.ifm_active, " ");
    391  1.3.4.2  mjf 		printf(")");
    392  1.3.4.2  mjf 	}
    393  1.3.4.2  mjf 	printf("\n");
    394  1.3.4.2  mjf 
    395  1.3.4.2  mjf 	if (ifmr.ifm_status & IFM_STATUS_VALID) {
    396  1.3.4.2  mjf 		const struct ifmedia_status_description *ifms;
    397  1.3.4.2  mjf 		int bitno, found = 0;
    398  1.3.4.2  mjf 
    399  1.3.4.2  mjf 		printf("\tstatus: ");
    400  1.3.4.2  mjf 		for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
    401  1.3.4.2  mjf 			for (ifms = ifm_status_descriptions;
    402  1.3.4.2  mjf 			     ifms->ifms_valid != 0; ifms++) {
    403  1.3.4.2  mjf 				if (ifms->ifms_type !=
    404  1.3.4.2  mjf 				      IFM_TYPE(ifmr.ifm_current) ||
    405  1.3.4.2  mjf 				    ifms->ifms_valid !=
    406  1.3.4.2  mjf 				      ifm_status_valid_list[bitno])
    407  1.3.4.2  mjf 					continue;
    408  1.3.4.2  mjf 				printf("%s%s", found ? ", " : "",
    409  1.3.4.2  mjf 				    IFM_STATUS_DESC(ifms, ifmr.ifm_status));
    410  1.3.4.2  mjf 				found = 1;
    411  1.3.4.2  mjf 
    412  1.3.4.2  mjf 				/*
    413  1.3.4.2  mjf 				 * For each valid indicator bit, there's
    414  1.3.4.2  mjf 				 * only one entry for each media type, so
    415  1.3.4.2  mjf 				 * terminate the inner loop now.
    416  1.3.4.2  mjf 				 */
    417  1.3.4.2  mjf 				break;
    418  1.3.4.2  mjf 			}
    419  1.3.4.2  mjf 		}
    420  1.3.4.2  mjf 
    421  1.3.4.2  mjf 		if (found == 0)
    422  1.3.4.2  mjf 			printf("unknown");
    423  1.3.4.2  mjf 		printf("\n");
    424  1.3.4.2  mjf 	}
    425  1.3.4.2  mjf 
    426  1.3.4.2  mjf 	if (get_flag('m')) {
    427  1.3.4.2  mjf 		int type, printed_type;
    428  1.3.4.2  mjf 
    429  1.3.4.2  mjf 		for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
    430  1.3.4.2  mjf 			for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
    431  1.3.4.2  mjf 				if (IFM_TYPE(media_list[i]) != type)
    432  1.3.4.2  mjf 					continue;
    433  1.3.4.2  mjf 				if (printed_type == 0) {
    434  1.3.4.2  mjf 					printf("\tsupported %s media:\n",
    435  1.3.4.2  mjf 					    get_media_type_string(type));
    436  1.3.4.2  mjf 					printed_type = 1;
    437  1.3.4.2  mjf 				}
    438  1.3.4.2  mjf 				printf("\t\tmedia ");
    439  1.3.4.2  mjf 				print_media_word(media_list[i], " mediaopt ");
    440  1.3.4.2  mjf 				printf("\n");
    441  1.3.4.2  mjf 			}
    442  1.3.4.2  mjf 		}
    443  1.3.4.2  mjf 	}
    444  1.3.4.2  mjf 
    445  1.3.4.2  mjf 	free(media_list);
    446  1.3.4.2  mjf }
    447  1.3.4.2  mjf 
    448  1.3.4.2  mjf static void
    449  1.3.4.2  mjf media_usage(prop_dictionary_t env)
    450  1.3.4.2  mjf {
    451  1.3.4.2  mjf 	fprintf(stderr,
    452  1.3.4.2  mjf 	    "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
    453  1.3.4.2  mjf 	    "[ instance minst ]\n");
    454  1.3.4.2  mjf }
    455  1.3.4.2  mjf 
    456  1.3.4.2  mjf static void
    457  1.3.4.2  mjf media_constructor(void)
    458  1.3.4.2  mjf {
    459  1.3.4.2  mjf 	if (register_flag('m') != 0)
    460  1.3.4.2  mjf 		err(EXIT_FAILURE, __func__);
    461  1.3.4.2  mjf 	usage_func_init(&usage, media_usage);
    462  1.3.4.2  mjf 	register_usage(&usage);
    463  1.3.4.2  mjf }
    464