3 __RCSID("$NetBSD: media.c,v 1.2 2008/07/15 20:56:13 dyoung Exp $");
14 #include <sys/ioctl.h>
17 #include <net/if_dl.h>
18 #include <net/if_media.h>
20 #include <prop/proplib.h>
28 static void init_current_media(prop_dictionary_t
, prop_dictionary_t
);
29 static void media_constructor(void) __attribute__((constructor
));
30 static int setmedia(prop_dictionary_t
, prop_dictionary_t
);
31 static int setmediainst(prop_dictionary_t
, prop_dictionary_t
);
32 static int setmediamode(prop_dictionary_t
, prop_dictionary_t
);
33 static int setmediaopt(prop_dictionary_t
, prop_dictionary_t
);
34 static int unsetmediaopt(prop_dictionary_t
, prop_dictionary_t
);
37 * Media stuff. Whenever a media command is first performed, the
38 * currently select media is grabbed for this interface. If `media'
39 * is given, the current media word is modifed. `mediaopt' commands
40 * only modify the set and clear words. They then operate on the
41 * current media word later.
43 static int media_current
;
44 static int mediaopt_set
;
45 static int mediaopt_clear
;
47 static struct usage_func usage
;
49 static const int ifm_status_valid_list
[] = IFM_STATUS_VALID_LIST
;
51 static const struct ifmedia_status_description ifm_status_descriptions
[] =
52 IFM_STATUS_DESCRIPTIONS
;
54 static struct pstr mediamode
= PSTR_INITIALIZER(&mediamode
, "mediamode",
55 setmediamode
, "mediamode", &command_root
.pb_parser
);
57 static struct pinteger mediainst
= PINTEGER_INITIALIZER1(&mediainst
,
58 "mediainst", 0, IFM_INST_MAX
, 10, setmediainst
, "mediainst",
59 &command_root
.pb_parser
);
61 static struct pstr unmediaopt
= PSTR_INITIALIZER(&unmediaopt
, "-mediaopt",
62 unsetmediaopt
, "unmediaopt", &command_root
.pb_parser
);
64 static struct pstr mediaopt
= PSTR_INITIALIZER(&mediaopt
, "mediaopt",
65 setmediaopt
, "mediaopt", &command_root
.pb_parser
);
67 static struct pstr media
= PSTR_INITIALIZER(&media
, "media", setmedia
, "media",
68 &command_root
.pb_parser
);
70 static const struct kwinst mediakw
[] = {
71 {.k_word
= "instance", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
72 .k_bool
= true, .k_act
= "media", .k_deact
= "mediainst",
73 .k_nextparser
= &mediainst
.pi_parser
}
74 , {.k_word
= "inst", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
75 .k_bool
= true, .k_act
= "media", .k_deact
= "mediainst",
76 .k_nextparser
= &mediainst
.pi_parser
}
77 , {.k_word
= "media", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
78 .k_bool
= true, .k_deact
= "media", .k_altdeact
= "anymedia",
79 .k_nextparser
= &media
.ps_parser
}
80 , {.k_word
= "mediaopt", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
81 .k_bool
= true, .k_deact
= "mediaopt", .k_altdeact
= "instance",
82 .k_nextparser
= &mediaopt
.ps_parser
}
83 , {.k_word
= "-mediaopt", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
84 .k_bool
= true, .k_deact
= "unmediaopt", .k_altdeact
= "media",
85 .k_nextparser
= &unmediaopt
.ps_parser
}
86 , {.k_word
= "mode", .k_key
= "anymedia", .k_type
= KW_T_BOOL
,
87 .k_bool
= true, .k_deact
= "mode",
88 .k_nextparser
= &mediamode
.ps_parser
}
91 struct pkw kwmedia
= PKW_INITIALIZER(&kwmedia
, "media keywords", NULL
, NULL
,
92 mediakw
, __arraycount(mediakw
), NULL
);
95 media_error(int type
, const char *val
, const char *opt
)
97 errx(EXIT_FAILURE
, "unknown %s media %s: %s",
98 get_media_type_string(type
), opt
, val
);
102 init_current_media(prop_dictionary_t env
, prop_dictionary_t oenv
)
105 struct ifmediareq ifmr
;
107 if ((ifname
= getifname(env
)) == NULL
)
108 err(EXIT_FAILURE
, "getifname");
111 * If we have not yet done so, grab the currently-selected
115 if (prop_dictionary_get(env
, "initmedia") == NULL
) {
116 memset(&ifmr
, 0, sizeof(ifmr
));
118 if (direct_ioctl(env
, SIOCGIFMEDIA
, &ifmr
) == -1) {
120 * If we get E2BIG, the kernel is telling us
121 * that there are more, so we can ignore it.
124 err(EXIT_FAILURE
, "SIOCGIFMEDIA");
127 if (!prop_dictionary_set_bool(oenv
, "initmedia", true)) {
128 err(EXIT_FAILURE
, "%s: prop_dictionary_set_bool",
131 media_current
= ifmr
.ifm_current
;
135 if (IFM_TYPE(media_current
) == 0)
136 errx(EXIT_FAILURE
, "%s: no link type?", ifname
);
140 process_media_commands(prop_dictionary_t env
)
144 if (prop_dictionary_get(env
, "media") == NULL
&&
145 prop_dictionary_get(env
, "mediaopt") == NULL
&&
146 prop_dictionary_get(env
, "unmediaopt") == NULL
&&
147 prop_dictionary_get(env
, "mediamode") == NULL
) {
153 * Media already set up, and commands sanity-checked. Set/clear
154 * any options, and we're ready to go.
156 media_current
|= mediaopt_set
;
157 media_current
&= ~mediaopt_clear
;
159 memset(&ifr
, 0, sizeof(ifr
));
160 ifr
.ifr_media
= media_current
;
162 if (direct_ioctl(env
, SIOCSIFMEDIA
, &ifr
) == -1)
163 err(EXIT_FAILURE
, "SIOCSIFMEDIA");
167 setmedia(prop_dictionary_t env
, prop_dictionary_t oenv
)
169 int type
, subtype
, inst
;
173 init_current_media(env
, oenv
);
175 data
= (prop_data_t
)prop_dictionary_get(env
, "media");
176 assert(data
!= NULL
);
178 /* Only one media command may be given. */
179 /* Must not come after mode commands */
180 /* Must not come after mediaopt commands */
183 * No need to check if `instance' has been issued; setmediainst()
184 * craps out if `media' has not been specified.
187 type
= IFM_TYPE(media_current
);
188 inst
= IFM_INST(media_current
);
190 val
= strndup(prop_data_data_nocopy(data
), prop_data_size(data
));
194 /* Look up the subtype. */
195 subtype
= get_media_subtype(type
, val
);
197 media_error(type
, val
, "subtype");
199 /* Build the new current media word. */
200 media_current
= IFM_MAKEWORD(type
, subtype
, 0, inst
);
202 /* Media will be set after other processing is complete. */
207 setmediaopt(prop_dictionary_t env
, prop_dictionary_t oenv
)
213 init_current_media(env
, oenv
);
215 data
= (prop_data_t
)prop_dictionary_get(env
, "mediaopt");
216 assert(data
!= NULL
);
218 /* Can only issue `mediaopt' once. */
219 /* Can't issue `mediaopt' if `instance' has already been issued. */
221 val
= strndup(prop_data_data_nocopy(data
), prop_data_size(data
));
225 mediaopt_set
= get_media_options(media_current
, val
, &invalid
);
227 if (mediaopt_set
== -1)
228 media_error(media_current
, invalid
, "option");
230 /* Media will be set after other processing is complete. */
235 unsetmediaopt(prop_dictionary_t env
, prop_dictionary_t oenv
)
240 init_current_media(env
, oenv
);
242 data
= (prop_data_t
)prop_dictionary_get(env
, "unmediaopt");
248 val
= strndup(prop_data_data_nocopy(data
), prop_data_size(data
));
253 * No need to check for A_MEDIAINST, since the test for A_MEDIA
254 * implicitly checks for A_MEDIAINST.
257 mediaopt_clear
= get_media_options(media_current
, val
, &invalid
);
259 if (mediaopt_clear
== -1)
260 media_error(media_current
, invalid
, "option");
262 /* Media will be set after other processing is complete. */
267 setmediainst(prop_dictionary_t env
, prop_dictionary_t oenv
)
269 int type
, subtype
, options
;
273 init_current_media(env
, oenv
);
275 rc
= prop_dictionary_get_int64(env
, "mediainst", &inst
);
278 /* Can only issue `instance' once. */
279 /* Must have already specified `media' */
281 type
= IFM_TYPE(media_current
);
282 subtype
= IFM_SUBTYPE(media_current
);
283 options
= IFM_OPTIONS(media_current
);
285 media_current
= IFM_MAKEWORD(type
, subtype
, options
, inst
);
287 /* Media will be set after other processing is complete. */
292 setmediamode(prop_dictionary_t env
, prop_dictionary_t oenv
)
294 int type
, subtype
, options
, inst
, mode
;
298 init_current_media(env
, oenv
);
300 data
= (prop_data_t
)prop_dictionary_get(env
, "mediamode");
301 assert(data
!= NULL
);
303 type
= IFM_TYPE(media_current
);
304 subtype
= IFM_SUBTYPE(media_current
);
305 options
= IFM_OPTIONS(media_current
);
306 inst
= IFM_INST(media_current
);
308 val
= strndup(prop_data_data_nocopy(data
), prop_data_size(data
));
312 mode
= get_media_mode(type
, val
);
314 media_error(type
, val
, "mode");
318 media_current
= IFM_MAKEWORD(type
, subtype
, options
, inst
) | mode
;
320 /* Media will be set after other processing is complete. */
325 print_media_word(int ifmw
, const char *opt_sep
)
329 printf("%s", get_media_subtype_string(ifmw
));
332 if (IFM_MODE(ifmw
) != 0) {
333 str
= get_media_mode_string(ifmw
);
335 printf(" mode %s", str
);
339 for (; (str
= get_media_option_string(&ifmw
)) != NULL
; opt_sep
= ",")
340 printf("%s%s", opt_sep
, str
);
342 if (IFM_INST(ifmw
) != 0)
343 printf(" instance %d", IFM_INST(ifmw
));
347 media_status(prop_dictionary_t env
, prop_dictionary_t oenv
)
349 struct ifmediareq ifmr
;
354 if ((ifname
= getifname(env
)) == NULL
)
355 err(EXIT_FAILURE
, "getifname");
356 if ((af
= getaf(env
)) == -1)
359 /* get out early if the family is unsupported by the kernel */
360 if ((s
= getsock(af
)) == -1)
361 err(EXIT_FAILURE
, "%s: getsock", __func__
);
363 memset(&ifmr
, 0, sizeof(ifmr
));
364 estrlcpy(ifmr
.ifm_name
, ifname
, sizeof(ifmr
.ifm_name
));
366 if (ioctl(s
, SIOCGIFMEDIA
, &ifmr
) == -1) {
368 * Interface doesn't support SIOC{G,S}IFMEDIA.
373 if (ifmr
.ifm_count
== 0) {
374 warnx("%s: no media types?", ifname
);
378 media_list
= (int *)malloc(ifmr
.ifm_count
* sizeof(int));
379 if (media_list
== NULL
)
380 err(EXIT_FAILURE
, "malloc");
381 ifmr
.ifm_ulist
= media_list
;
383 if (ioctl(s
, SIOCGIFMEDIA
, &ifmr
) == -1)
384 err(EXIT_FAILURE
, "SIOCGIFMEDIA");
386 printf("\tmedia: %s ", get_media_type_string(ifmr
.ifm_current
));
387 print_media_word(ifmr
.ifm_current
, " ");
388 if (ifmr
.ifm_active
!= ifmr
.ifm_current
) {
390 print_media_word(ifmr
.ifm_active
, " ");
395 if (ifmr
.ifm_status
& IFM_STATUS_VALID
) {
396 const struct ifmedia_status_description
*ifms
;
397 int bitno
, found
= 0;
399 printf("\tstatus: ");
400 for (bitno
= 0; ifm_status_valid_list
[bitno
] != 0; bitno
++) {
401 for (ifms
= ifm_status_descriptions
;
402 ifms
->ifms_valid
!= 0; ifms
++) {
403 if (ifms
->ifms_type
!=
404 IFM_TYPE(ifmr
.ifm_current
) ||
406 ifm_status_valid_list
[bitno
])
408 printf("%s%s", found
? ", " : "",
409 IFM_STATUS_DESC(ifms
, ifmr
.ifm_status
));
413 * For each valid indicator bit, there's
414 * only one entry for each media type, so
415 * terminate the inner loop now.
427 int type
, printed_type
;
429 for (type
= IFM_NMIN
; type
<= IFM_NMAX
; type
+= IFM_NMIN
) {
430 for (i
= 0, printed_type
= 0; i
< ifmr
.ifm_count
; i
++) {
431 if (IFM_TYPE(media_list
[i
]) != type
)
433 if (printed_type
== 0) {
434 printf("\tsupported %s media:\n",
435 get_media_type_string(type
));
438 printf("\t\tmedia ");
439 print_media_word(media_list
[i
], " mediaopt ");
449 media_usage(prop_dictionary_t env
)
452 "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
453 "[ instance minst ]\n");
457 media_constructor(void)
459 if (register_flag('m') != 0)
460 err(EXIT_FAILURE
, __func__
);
461 usage_func_init(&usage
, media_usage
);
462 register_usage(&usage
);