3 __RCSID("$NetBSD: media.c,v 1.6 2011/08/29 14:35:00 joerg Exp $");
14 #include <sys/ioctl.h>
17 #include <net/if_dl.h>
18 #include <net/if_media.h>
20 #include <prop/proplib.h>
29 static void init_current_media(prop_dictionary_t
, prop_dictionary_t
);
30 static void media_constructor(void) __attribute__((constructor
));
31 static int setmedia(prop_dictionary_t
, prop_dictionary_t
);
32 static int setmediainst(prop_dictionary_t
, prop_dictionary_t
);
33 static int setmediamode(prop_dictionary_t
, prop_dictionary_t
);
34 static int setmediaopt(prop_dictionary_t
, prop_dictionary_t
);
35 static int unsetmediaopt(prop_dictionary_t
, prop_dictionary_t
);
38 * Media stuff. Whenever a media command is first performed, the
39 * currently select media is grabbed for this interface. If `media'
40 * is given, the current media word is modifed. `mediaopt' commands
41 * only modify the set and clear words. They then operate on the
42 * current media word later.
44 static int media_current
;
45 static int mediaopt_set
;
46 static int mediaopt_clear
;
48 static struct usage_func usage
;
50 static const int ifm_status_valid_list
[] = IFM_STATUS_VALID_LIST
;
52 static const struct ifmedia_status_description ifm_status_descriptions
[] =
53 IFM_STATUS_DESCRIPTIONS
;
55 static struct pstr mediamode
= PSTR_INITIALIZER1(&mediamode
, "mediamode",
56 setmediamode
, "mediamode", false, &command_root
.pb_parser
);
58 static struct pinteger mediainst
= PINTEGER_INITIALIZER1(&mediainst
,
59 "mediainst", 0, IFM_INST_MAX
, 10, setmediainst
, "mediainst",
60 &command_root
.pb_parser
);
62 static struct pstr unmediaopt
= PSTR_INITIALIZER1(&unmediaopt
, "-mediaopt",
63 unsetmediaopt
, "unmediaopt", false, &command_root
.pb_parser
);
65 static struct pstr mediaopt
= PSTR_INITIALIZER1(&mediaopt
, "mediaopt",
66 setmediaopt
, "mediaopt", false, &command_root
.pb_parser
);
68 static struct pstr media
= PSTR_INITIALIZER1(&media
, "media", setmedia
, "media",
69 false, &command_root
.pb_parser
);
71 static const struct kwinst mediakw
[] = {
72 {.k_word
= "instance", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
73 .k_bool
= true, .k_act
= "media", .k_deact
= "mediainst",
74 .k_nextparser
= &mediainst
.pi_parser
}
75 , {.k_word
= "inst", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
76 .k_bool
= true, .k_act
= "media", .k_deact
= "mediainst",
77 .k_nextparser
= &mediainst
.pi_parser
}
78 , {.k_word
= "media", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
79 .k_bool
= true, .k_deact
= "media", .k_altdeact
= "anymedia",
80 .k_nextparser
= &media
.ps_parser
}
81 , {.k_word
= "mediaopt", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
82 .k_bool
= true, .k_deact
= "mediaopt", .k_altdeact
= "instance",
83 .k_nextparser
= &mediaopt
.ps_parser
}
84 , {.k_word
= "-mediaopt", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
85 .k_bool
= true, .k_deact
= "unmediaopt", .k_altdeact
= "media",
86 .k_nextparser
= &unmediaopt
.ps_parser
}
87 , {.k_word
= "mode", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
88 .k_bool
= true, .k_deact
= "mode",
89 .k_nextparser
= &mediamode
.ps_parser
}
92 struct pkw kwmedia
= PKW_INITIALIZER(&kwmedia
, "media keywords", NULL
, NULL
,
93 mediakw
, __arraycount(mediakw
), NULL
);
96 media_error(int type
, const char *val
, const char *opt
)
98 errx(EXIT_FAILURE
, "unknown %s media %s: %s",
99 get_media_type_string(type
), opt
, val
);
103 init_current_media(prop_dictionary_t env
, prop_dictionary_t oenv
)
106 struct ifmediareq ifmr
;
108 if ((ifname
= getifname(env
)) == NULL
)
109 err(EXIT_FAILURE
, "getifname");
112 * If we have not yet done so, grab the currently-selected
116 if (prop_dictionary_get(env
, "initmedia") == NULL
) {
117 memset(&ifmr
, 0, sizeof(ifmr
));
119 if (direct_ioctl(env
, SIOCGIFMEDIA
, &ifmr
) == -1) {
121 * If we get E2BIG, the kernel is telling us
122 * that there are more, so we can ignore it.
125 err(EXIT_FAILURE
, "SIOCGIFMEDIA");
128 if (!prop_dictionary_set_bool(oenv
, "initmedia", true)) {
129 err(EXIT_FAILURE
, "%s: prop_dictionary_set_bool",
132 media_current
= ifmr
.ifm_current
;
136 if (IFM_TYPE(media_current
) == 0)
137 errx(EXIT_FAILURE
, "%s: no link type?", ifname
);
141 process_media_commands(prop_dictionary_t env
)
145 if (prop_dictionary_get(env
, "media") == NULL
&&
146 prop_dictionary_get(env
, "mediaopt") == NULL
&&
147 prop_dictionary_get(env
, "unmediaopt") == NULL
&&
148 prop_dictionary_get(env
, "mediamode") == NULL
) {
154 * Media already set up, and commands sanity-checked. Set/clear
155 * any options, and we're ready to go.
157 media_current
|= mediaopt_set
;
158 media_current
&= ~mediaopt_clear
;
160 memset(&ifr
, 0, sizeof(ifr
));
161 ifr
.ifr_media
= media_current
;
163 if (direct_ioctl(env
, SIOCSIFMEDIA
, &ifr
) == -1)
164 err(EXIT_FAILURE
, "SIOCSIFMEDIA");
168 setmedia(prop_dictionary_t env
, prop_dictionary_t oenv
)
170 int type
, subtype
, inst
;
174 init_current_media(env
, oenv
);
176 data
= (prop_data_t
)prop_dictionary_get(env
, "media");
177 assert(data
!= NULL
);
179 /* Only one media command may be given. */
180 /* Must not come after mode commands */
181 /* Must not come after mediaopt commands */
184 * No need to check if `instance' has been issued; setmediainst()
185 * craps out if `media' has not been specified.
188 type
= IFM_TYPE(media_current
);
189 inst
= IFM_INST(media_current
);
191 val
= strndup(prop_data_data_nocopy(data
), prop_data_size(data
));
195 /* Look up the subtype. */
196 subtype
= get_media_subtype(type
, val
);
198 media_error(type
, val
, "subtype");
200 /* Build the new current media word. */
201 media_current
= IFM_MAKEWORD(type
, subtype
, 0, inst
);
203 /* Media will be set after other processing is complete. */
208 setmediaopt(prop_dictionary_t env
, prop_dictionary_t oenv
)
214 init_current_media(env
, oenv
);
216 data
= (prop_data_t
)prop_dictionary_get(env
, "mediaopt");
217 assert(data
!= NULL
);
219 /* Can only issue `mediaopt' once. */
220 /* Can't issue `mediaopt' if `instance' has already been issued. */
222 val
= strndup(prop_data_data_nocopy(data
), prop_data_size(data
));
226 mediaopt_set
= get_media_options(media_current
, val
, &invalid
);
228 if (mediaopt_set
== -1)
229 media_error(media_current
, invalid
, "option");
231 /* Media will be set after other processing is complete. */
236 unsetmediaopt(prop_dictionary_t env
, prop_dictionary_t oenv
)
241 init_current_media(env
, oenv
);
243 data
= (prop_data_t
)prop_dictionary_get(env
, "unmediaopt");
249 val
= strndup(prop_data_data_nocopy(data
), prop_data_size(data
));
254 * No need to check for A_MEDIAINST, since the test for A_MEDIA
255 * implicitly checks for A_MEDIAINST.
258 mediaopt_clear
= get_media_options(media_current
, val
, &invalid
);
260 if (mediaopt_clear
== -1)
261 media_error(media_current
, invalid
, "option");
263 /* Media will be set after other processing is complete. */
268 setmediainst(prop_dictionary_t env
, prop_dictionary_t oenv
)
270 int type
, subtype
, options
;
274 init_current_media(env
, oenv
);
276 rc
= prop_dictionary_get_int64(env
, "mediainst", &inst
);
279 /* Can only issue `instance' once. */
280 /* Must have already specified `media' */
282 type
= IFM_TYPE(media_current
);
283 subtype
= IFM_SUBTYPE(media_current
);
284 options
= IFM_OPTIONS(media_current
);
286 media_current
= IFM_MAKEWORD(type
, subtype
, options
, inst
);
288 /* Media will be set after other processing is complete. */
293 setmediamode(prop_dictionary_t env
, prop_dictionary_t oenv
)
295 int type
, subtype
, options
, inst
, mode
;
299 init_current_media(env
, oenv
);
301 data
= (prop_data_t
)prop_dictionary_get(env
, "mediamode");
302 assert(data
!= NULL
);
304 type
= IFM_TYPE(media_current
);
305 subtype
= IFM_SUBTYPE(media_current
);
306 options
= IFM_OPTIONS(media_current
);
307 inst
= IFM_INST(media_current
);
309 val
= strndup(prop_data_data_nocopy(data
), prop_data_size(data
));
313 mode
= get_media_mode(type
, val
);
315 media_error(type
, val
, "mode");
319 media_current
= IFM_MAKEWORD(type
, subtype
, options
, inst
) | mode
;
321 /* Media will be set after other processing is complete. */
326 print_media_word(int ifmw
, const char *opt_sep
)
330 printf("%s", get_media_subtype_string(ifmw
));
333 if (IFM_MODE(ifmw
) != 0) {
334 str
= get_media_mode_string(ifmw
);
336 printf(" mode %s", str
);
340 for (; (str
= get_media_option_string(&ifmw
)) != NULL
; opt_sep
= ",")
341 printf("%s%s", opt_sep
, str
);
343 if (IFM_INST(ifmw
) != 0)
344 printf(" instance %d", IFM_INST(ifmw
));
348 media_status(prop_dictionary_t env
, prop_dictionary_t oenv
)
350 struct ifmediareq ifmr
;
355 if ((ifname
= getifname(env
)) == NULL
)
356 err(EXIT_FAILURE
, "getifname");
357 if ((af
= getaf(env
)) == -1)
360 /* get out early if the family is unsupported by the kernel */
361 if ((s
= getsock(af
)) == -1)
362 err(EXIT_FAILURE
, "%s: getsock", __func__
);
364 memset(&ifmr
, 0, sizeof(ifmr
));
365 estrlcpy(ifmr
.ifm_name
, ifname
, sizeof(ifmr
.ifm_name
));
367 if (prog_ioctl(s
, SIOCGIFMEDIA
, &ifmr
) == -1) {
369 * Interface doesn't support SIOC{G,S}IFMEDIA.
374 if (ifmr
.ifm_count
== 0) {
375 warnx("%s: no media types?", ifname
);
379 media_list
= (int *)malloc(ifmr
.ifm_count
* sizeof(int));
380 if (media_list
== NULL
)
381 err(EXIT_FAILURE
, "malloc");
382 ifmr
.ifm_ulist
= media_list
;
384 if (prog_ioctl(s
, SIOCGIFMEDIA
, &ifmr
) == -1)
385 err(EXIT_FAILURE
, "SIOCGIFMEDIA");
387 printf("\tmedia: %s ", get_media_type_string(ifmr
.ifm_current
));
388 print_media_word(ifmr
.ifm_current
, " ");
389 if (ifmr
.ifm_active
!= ifmr
.ifm_current
) {
391 print_media_word(ifmr
.ifm_active
, " ");
396 if (ifmr
.ifm_status
& IFM_STATUS_VALID
) {
397 const struct ifmedia_status_description
*ifms
;
398 int bitno
, found
= 0;
400 printf("\tstatus: ");
401 for (bitno
= 0; ifm_status_valid_list
[bitno
] != 0; bitno
++) {
402 for (ifms
= ifm_status_descriptions
;
403 ifms
->ifms_valid
!= 0; ifms
++) {
404 if (ifms
->ifms_type
!=
405 IFM_TYPE(ifmr
.ifm_current
) ||
407 ifm_status_valid_list
[bitno
])
409 printf("%s%s", found
? ", " : "",
410 IFM_STATUS_DESC(ifms
, ifmr
.ifm_status
));
414 * For each valid indicator bit, there's
415 * only one entry for each media type, so
416 * terminate the inner loop now.
428 int type
, printed_type
;
430 for (type
= IFM_NMIN
; type
<= IFM_NMAX
; type
+= IFM_NMIN
) {
431 for (i
= 0, printed_type
= 0; i
< ifmr
.ifm_count
; i
++) {
432 if (IFM_TYPE(media_list
[i
]) != type
)
434 if (printed_type
== 0) {
435 printf("\tsupported %s media:\n",
436 get_media_type_string(type
));
439 printf("\t\tmedia ");
440 print_media_word(media_list
[i
], " mediaopt ");
450 media_usage(prop_dictionary_t env
)
453 "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
454 "[ instance minst ]\n");
458 media_constructor(void)
460 if (register_flag('m') != 0)
461 err(EXIT_FAILURE
, __func__
);
462 usage_func_init(&usage
, media_usage
);
463 register_usage(&usage
);