Remove building with NOCRYPTO option
[minix.git] / sbin / ifconfig / media.c
blobb945cb3489a07eab4dc615a4be763fce17dacce2
1 #include <sys/cdefs.h>
2 #ifndef lint
3 __RCSID("$NetBSD: media.c,v 1.6 2011/08/29 14:35:00 joerg Exp $");
4 #endif /* not lint */
6 #include <assert.h>
7 #include <err.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <util.h>
14 #include <sys/ioctl.h>
16 #include <net/if.h>
17 #include <net/if_dl.h>
18 #include <net/if_media.h>
20 #include <prop/proplib.h>
22 #include "env.h"
23 #include "extern.h"
24 #include "media.h"
25 #include "parse.h"
26 #include "util.h"
27 #include "prog_ops.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);
95 __dead static void
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);
102 void
103 init_current_media(prop_dictionary_t env, prop_dictionary_t oenv)
105 const char *ifname;
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
113 * media.
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.
124 if (errno != E2BIG)
125 err(EXIT_FAILURE, "SIOCGIFMEDIA");
128 if (!prop_dictionary_set_bool(oenv, "initmedia", true)) {
129 err(EXIT_FAILURE, "%s: prop_dictionary_set_bool",
130 __func__);
132 media_current = ifmr.ifm_current;
135 /* Sanity. */
136 if (IFM_TYPE(media_current) == 0)
137 errx(EXIT_FAILURE, "%s: no link type?", ifname);
140 void
141 process_media_commands(prop_dictionary_t env)
143 struct ifreq ifr;
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) {
149 /* Nothing to do. */
150 return;
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");
167 static int
168 setmedia(prop_dictionary_t env, prop_dictionary_t oenv)
170 int type, subtype, inst;
171 prop_data_t data;
172 char *val;
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));
192 if (val == NULL)
193 return -1;
195 /* Look up the subtype. */
196 subtype = get_media_subtype(type, val);
197 if (subtype == -1)
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. */
204 return 0;
207 static int
208 setmediaopt(prop_dictionary_t env, prop_dictionary_t oenv)
210 char *invalid;
211 prop_data_t data;
212 char *val;
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));
223 if (val == NULL)
224 return -1;
226 mediaopt_set = get_media_options(media_current, val, &invalid);
227 free(val);
228 if (mediaopt_set == -1)
229 media_error(media_current, invalid, "option");
231 /* Media will be set after other processing is complete. */
232 return 0;
235 static int
236 unsetmediaopt(prop_dictionary_t env, prop_dictionary_t oenv)
238 char *invalid, *val;
239 prop_data_t data;
241 init_current_media(env, oenv);
243 data = (prop_data_t)prop_dictionary_get(env, "unmediaopt");
244 if (data == NULL) {
245 errno = ENOENT;
246 return -1;
249 val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
250 if (val == NULL)
251 return -1;
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);
259 free(val);
260 if (mediaopt_clear == -1)
261 media_error(media_current, invalid, "option");
263 /* Media will be set after other processing is complete. */
264 return 0;
267 static int
268 setmediainst(prop_dictionary_t env, prop_dictionary_t oenv)
270 int type, subtype, options;
271 int64_t inst;
272 bool rc;
274 init_current_media(env, oenv);
276 rc = prop_dictionary_get_int64(env, "mediainst", &inst);
277 assert(rc);
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. */
289 return 0;
292 static int
293 setmediamode(prop_dictionary_t env, prop_dictionary_t oenv)
295 int type, subtype, options, inst, mode;
296 prop_data_t data;
297 char *val;
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));
310 if (val == NULL)
311 return -1;
313 mode = get_media_mode(type, val);
314 if (mode == -1)
315 media_error(type, val, "mode");
317 free(val);
319 media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
321 /* Media will be set after other processing is complete. */
322 return 0;
325 void
326 print_media_word(int ifmw, const char *opt_sep)
328 const char *str;
330 printf("%s", get_media_subtype_string(ifmw));
332 /* Find mode. */
333 if (IFM_MODE(ifmw) != 0) {
334 str = get_media_mode_string(ifmw);
335 if (str != NULL)
336 printf(" mode %s", str);
339 /* Find options. */
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));
347 void
348 media_status(prop_dictionary_t env, prop_dictionary_t oenv)
350 struct ifmediareq ifmr;
351 int af, i, s;
352 int *media_list;
353 const char *ifname;
355 if ((ifname = getifname(env)) == NULL)
356 err(EXIT_FAILURE, "getifname");
357 if ((af = getaf(env)) == -1)
358 af = AF_UNSPEC;
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.
371 return;
374 if (ifmr.ifm_count == 0) {
375 warnx("%s: no media types?", ifname);
376 return;
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) {
390 printf(" (");
391 print_media_word(ifmr.ifm_active, " ");
392 printf(")");
394 printf("\n");
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) ||
406 ifms->ifms_valid !=
407 ifm_status_valid_list[bitno])
408 continue;
409 printf("%s%s", found ? ", " : "",
410 IFM_STATUS_DESC(ifms, ifmr.ifm_status));
411 found = 1;
414 * For each valid indicator bit, there's
415 * only one entry for each media type, so
416 * terminate the inner loop now.
418 break;
422 if (found == 0)
423 printf("unknown");
424 printf("\n");
427 if (get_flag('m')) {
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)
433 continue;
434 if (printed_type == 0) {
435 printf("\tsupported %s media:\n",
436 get_media_type_string(type));
437 printed_type = 1;
439 printf("\t\tmedia ");
440 print_media_word(media_list[i], " mediaopt ");
441 printf("\n");
446 free(media_list);
449 static void
450 media_usage(prop_dictionary_t env)
452 fprintf(stderr,
453 "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
454 "[ instance minst ]\n");
457 static void
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);