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