]> git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/ifmedia.c
network_cmds-543.260.3.tar.gz
[apple/network_cmds.git] / ifconfig.tproj / ifmedia.c
1 /* $NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $ */
2 /* $FreeBSD: src/sbin/ifconfig/ifmedia.c,v 1.25.6.1 2008/11/25 02:59:29 kensmith Exp $ */
3
4 /*
5 * Copyright (c) 1997 Jason R. Thorpe.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed for the NetBSD Project
19 * by Jason R. Thorpe.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 /*
37 * Copyright (c) 1983, 1993
38 * The Regents of the University of California. All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 */
68
69 #include <sys/param.h>
70 #include <sys/ioctl.h>
71 #include <sys/socket.h>
72 #include <sys/sysctl.h>
73 #include <sys/time.h>
74
75 #include <net/if.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_media.h>
79 #include <net/route.h>
80
81 #include <ctype.h>
82 #include <err.h>
83 #include <errno.h>
84 #include <fcntl.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <string.h>
88 #include <unistd.h>
89
90 #include "ifconfig.h"
91
92 static void domediaopt(const char *, int, int);
93 static int get_media_subtype(int, const char *);
94 #ifdef notdef
95 static int get_media_mode(int, const char *);
96 #endif
97 static int get_media_options(int, const char *);
98 static int lookup_media_word(struct ifmedia_description *, const char *);
99 static void print_media_word(int, int);
100 static void print_media_word_ifconfig(int);
101
102 static struct ifmedia_description *get_toptype_desc(int);
103 static struct ifmedia_type_to_subtype *get_toptype_ttos(int);
104 static struct ifmedia_description *get_subtype_desc(int,
105 struct ifmedia_type_to_subtype *ttos);
106
107 static void
108 media_status(int s)
109 {
110 struct ifmediareq ifmr;
111 int *media_list, i;
112
113 (void) memset(&ifmr, 0, sizeof(ifmr));
114 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
115
116 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)&ifmr) < 0) {
117 /*
118 * Interface doesn't support SIOC{G,S}IFMEDIA.
119 */
120 return;
121 }
122
123 if (ifmr.ifm_count == 0) {
124 warnx("%s: no media types?", name);
125 return;
126 }
127
128 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
129 if (media_list == NULL)
130 err(1, "malloc");
131 ifmr.ifm_ulist = media_list;
132
133 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)&ifmr) < 0)
134 err(1, "SIOCGIFXMEDIA");
135 printf("\tmedia: ");
136 print_media_word(ifmr.ifm_current, 1);
137 if (ifmr.ifm_active != ifmr.ifm_current) {
138 putchar(' ');
139 putchar('(');
140 print_media_word(ifmr.ifm_active, 0);
141 putchar(')');
142 }
143
144 putchar('\n');
145
146 if (ifmr.ifm_status & IFM_AVALID) {
147 printf("\tstatus: ");
148 #ifdef notdef
149 switch (IFM_TYPE(ifmr.ifm_active)) {
150 case IFM_ETHER:
151 case IFM_ATM:
152 if (ifmr.ifm_status & IFM_ACTIVE)
153 printf("active");
154 else
155 printf("no carrier");
156 break;
157
158 case IFM_FDDI:
159 case IFM_TOKEN:
160 if (ifmr.ifm_status & IFM_ACTIVE)
161 printf("inserted");
162 else
163 printf("no ring");
164 break;
165
166 case IFM_IEEE80211:
167 /* XXX: Different value for adhoc? */
168 if (ifmr.ifm_status & IFM_ACTIVE)
169 printf("associated");
170 else
171 printf("no carrier");
172 break;
173 }
174 #else
175 if (ifmr.ifm_status & IFM_ACTIVE)
176 printf("active");
177 else
178 printf("inactive");
179 #endif
180 putchar('\n');
181 }
182
183 if (ifmr.ifm_count > 0 && supmedia) {
184 printf("\tsupported media:\n");
185 for (i = 0; i < ifmr.ifm_count; i++) {
186 printf("\t\t");
187 print_media_word_ifconfig(media_list[i]);
188 putchar('\n');
189 }
190 }
191
192 free(media_list);
193 }
194
195 struct ifmediareq *
196 ifmedia_getstate(int s)
197 {
198 static struct ifmediareq *ifmr = NULL;
199 int *mwords;
200
201 if (ifmr == NULL) {
202 ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq));
203 if (ifmr == NULL)
204 err(1, "malloc");
205
206 (void) memset(ifmr, 0, sizeof(struct ifmediareq));
207 (void) strncpy(ifmr->ifm_name, name,
208 sizeof(ifmr->ifm_name));
209
210 ifmr->ifm_count = 0;
211 ifmr->ifm_ulist = NULL;
212
213 /*
214 * We must go through the motions of reading all
215 * supported media because we need to know both
216 * the current media type and the top-level type.
217 */
218
219 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)ifmr) < 0) {
220 err(1, "SIOCGIFXMEDIA");
221 }
222
223 if (ifmr->ifm_count == 0)
224 errx(1, "%s: no media types?", name);
225
226 mwords = (int *)malloc(ifmr->ifm_count * sizeof(int));
227 if (mwords == NULL)
228 err(1, "malloc");
229
230 ifmr->ifm_ulist = mwords;
231 if (ioctl(s, SIOCGIFXMEDIA, (caddr_t)ifmr) < 0)
232 err(1, "SIOCGIFXMEDIA");
233 }
234
235 return ifmr;
236 }
237
238 static void
239 setifmediacallback(int s, void *arg)
240 {
241 struct ifmediareq *ifmr = (struct ifmediareq *)arg;
242 static int did_it = 0;
243
244 if (!did_it) {
245 ifr.ifr_media = ifmr->ifm_current;
246 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
247 err(1, "SIOCSIFMEDIA (media)");
248 free(ifmr->ifm_ulist);
249 free(ifmr);
250 did_it = 1;
251 }
252 }
253
254 static void
255 setmedia(const char *val, int d, int s, const struct afswtch *afp)
256 {
257 struct ifmediareq *ifmr;
258 int subtype;
259
260 ifmr = ifmedia_getstate(s);
261
262 /*
263 * We are primarily concerned with the top-level type.
264 * However, "current" may be only IFM_NONE, so we just look
265 * for the top-level type in the first "supported type"
266 * entry.
267 *
268 * (I'm assuming that all supported media types for a given
269 * interface will be the same top-level type..)
270 */
271 subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val);
272
273 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
274 ifr.ifr_media = (ifmr->ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
275 IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
276
277 if ((ifr.ifr_media & IFM_TMASK) == 0) {
278 ifr.ifr_media &= ~IFM_GMASK;
279 }
280
281 ifmr->ifm_current = ifr.ifr_media;
282 callback_register(setifmediacallback, (void *)ifmr);
283 }
284
285 static void
286 setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
287 {
288
289 domediaopt(val, 0, s);
290 }
291
292 static void
293 unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
294 {
295
296 domediaopt(val, 1, s);
297 }
298
299 static void
300 domediaopt(const char *val, int clear, int s)
301 {
302 struct ifmediareq *ifmr;
303 int options;
304
305 ifmr = ifmedia_getstate(s);
306
307 options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val);
308
309 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
310 ifr.ifr_media = ifmr->ifm_current;
311 if (clear)
312 ifr.ifr_media &= ~options;
313 else {
314 if (options & IFM_HDX) {
315 ifr.ifr_media &= ~IFM_FDX;
316 options &= ~IFM_HDX;
317 }
318 ifr.ifr_media |= options;
319 }
320 ifmr->ifm_current = ifr.ifr_media;
321 callback_register(setifmediacallback, (void *)ifmr);
322 }
323
324 static void
325 setmediainst(const char *val, int d, int s, const struct afswtch *afp)
326 {
327 struct ifmediareq *ifmr;
328 int inst;
329
330 ifmr = ifmedia_getstate(s);
331
332 inst = atoi(val);
333 if (inst < 0 || inst > IFM_INST_MAX)
334 errx(1, "invalid media instance: %s", val);
335
336 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
337 ifr.ifr_media = (ifmr->ifm_current & ~IFM_IMASK) | inst << IFM_ISHIFT;
338
339 ifmr->ifm_current = ifr.ifr_media;
340 callback_register(setifmediacallback, (void *)ifmr);
341 }
342
343 #ifdef notdef
344 static void
345 setmediamode(const char *val, int d, int s, const struct afswtch *afp)
346 {
347 struct ifmediareq *ifmr;
348 int mode;
349
350 ifmr = ifmedia_getstate(s);
351
352 mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val);
353
354 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
355 ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode;
356
357 ifmr->ifm_current = ifr.ifr_media;
358 callback_register(setifmediacallback, (void *)ifmr);
359 }
360 #endif
361
362 /**********************************************************************
363 * A good chunk of this is duplicated from sys/net/ifmedia.c
364 **********************************************************************/
365
366 static struct ifmedia_description ifm_type_descriptions[] =
367 IFM_TYPE_DESCRIPTIONS;
368
369 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
370 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
371
372 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
373 IFM_SUBTYPE_ETHERNET_ALIASES;
374
375 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
376 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
377
378 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
379 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
380
381 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
382 IFM_SUBTYPE_TOKENRING_ALIASES;
383
384 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
385 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
386
387 static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
388 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
389
390 static struct ifmedia_description ifm_subtype_fddi_aliases[] =
391 IFM_SUBTYPE_FDDI_ALIASES;
392
393 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
394 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
395
396 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
397 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
398
399 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
400 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
401
402 #ifdef notdef
403 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
404 IFM_SUBTYPE_IEEE80211_ALIASES;
405
406 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
407 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
408
409 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
410 IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
411
412 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
413 IFM_SUBTYPE_ATM_DESCRIPTIONS;
414
415 static struct ifmedia_description ifm_subtype_atm_aliases[] =
416 IFM_SUBTYPE_ATM_ALIASES;
417
418 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
419 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
420 #endif
421
422 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
423 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
424
425 static struct ifmedia_description ifm_subtype_shared_aliases[] =
426 IFM_SUBTYPE_SHARED_ALIASES;
427
428 static struct ifmedia_description ifm_shared_option_descriptions[] =
429 IFM_SHARED_OPTION_DESCRIPTIONS;
430
431 struct ifmedia_type_to_subtype {
432 struct {
433 struct ifmedia_description *desc;
434 int alias;
435 } subtypes[5];
436 struct {
437 struct ifmedia_description *desc;
438 int alias;
439 } options[3];
440 struct {
441 struct ifmedia_description *desc;
442 int alias;
443 } modes[3];
444 };
445
446 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
447 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
448 {
449 {
450 { &ifm_subtype_shared_descriptions[0], 0 },
451 { &ifm_subtype_shared_aliases[0], 1 },
452 { &ifm_subtype_ethernet_descriptions[0], 0 },
453 { &ifm_subtype_ethernet_aliases[0], 1 },
454 { NULL, 0 },
455 },
456 {
457 { &ifm_shared_option_descriptions[0], 0 },
458 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
459 { NULL, 0 },
460 },
461 {
462 { NULL, 0 },
463 },
464 },
465 {
466 {
467 { &ifm_subtype_shared_descriptions[0], 0 },
468 { &ifm_subtype_shared_aliases[0], 1 },
469 { &ifm_subtype_tokenring_descriptions[0], 0 },
470 { &ifm_subtype_tokenring_aliases[0], 1 },
471 { NULL, 0 },
472 },
473 {
474 { &ifm_shared_option_descriptions[0], 0 },
475 { &ifm_subtype_tokenring_option_descriptions[0], 0 },
476 { NULL, 0 },
477 },
478 {
479 { NULL, 0 },
480 },
481 },
482 {
483 {
484 { &ifm_subtype_shared_descriptions[0], 0 },
485 { &ifm_subtype_shared_aliases[0], 1 },
486 { &ifm_subtype_fddi_descriptions[0], 0 },
487 { &ifm_subtype_fddi_aliases[0], 1 },
488 { NULL, 0 },
489 },
490 {
491 { &ifm_shared_option_descriptions[0], 0 },
492 { &ifm_subtype_fddi_option_descriptions[0], 0 },
493 { NULL, 0 },
494 },
495 {
496 { NULL, 0 },
497 },
498 },
499 #ifdef __APPLE__
500 {
501 {
502 { &ifm_subtype_shared_descriptions[0], 0 },
503 { &ifm_subtype_shared_aliases[0], 1 },
504 { &ifm_subtype_ieee80211_descriptions[0], 0 },
505 { NULL, 0 },
506 },
507 {
508 { &ifm_shared_option_descriptions[0], 0 },
509 { &ifm_subtype_ieee80211_option_descriptions[0], 1 },
510 { NULL, 0 },
511 },
512 {
513 { NULL, 0 },
514 },
515 },
516 #else /* __APPLE__ */
517 #ifdef notdef
518 {
519 {
520 { &ifm_subtype_shared_descriptions[0], 0 },
521 { &ifm_subtype_shared_aliases[0], 1 },
522 { &ifm_subtype_ieee80211_descriptions[0], 0 },
523 { &ifm_subtype_ieee80211_aliases[0], 1 },
524 { NULL, 0 },
525 },
526 {
527 { &ifm_shared_option_descriptions[0], 0 },
528 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
529 { NULL, 0 },
530 },
531 {
532 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
533 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
534 { NULL, 0 },
535 },
536 },
537 {
538 {
539 { &ifm_subtype_shared_descriptions[0], 0 },
540 { &ifm_subtype_shared_aliases[0], 1 },
541 { &ifm_subtype_atm_descriptions[0], 0 },
542 { &ifm_subtype_atm_aliases[0], 1 },
543 { NULL, 0 },
544 },
545 {
546 { &ifm_shared_option_descriptions[0], 0 },
547 { &ifm_subtype_atm_option_descriptions[0], 0 },
548 { NULL, 0 },
549 },
550 {
551 { NULL, 0 },
552 },
553 },
554 #endif
555 #endif /* __APPLE__ */
556 };
557
558 static int
559 get_media_subtype(int type, const char *val)
560 {
561 struct ifmedia_description *desc;
562 struct ifmedia_type_to_subtype *ttos;
563 int rval, i;
564
565 /* Find the top-level interface type. */
566 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
567 desc->ifmt_string != NULL; desc++, ttos++)
568 if (type == desc->ifmt_word)
569 break;
570 if (desc->ifmt_string == NULL)
571 errx(1, "unknown media type 0x%x", type);
572
573 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
574 rval = lookup_media_word(ttos->subtypes[i].desc, val);
575 if (rval != -1)
576 return (rval);
577 }
578 errx(1, "unknown media subtype: %s", val);
579 /*NOTREACHED*/
580 }
581
582 #ifdef notdef
583 static int
584 get_media_mode(int type, const char *val)
585 {
586 struct ifmedia_description *desc;
587 struct ifmedia_type_to_subtype *ttos;
588 int rval, i;
589
590 /* Find the top-level interface type. */
591 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
592 desc->ifmt_string != NULL; desc++, ttos++)
593 if (type == desc->ifmt_word)
594 break;
595 if (desc->ifmt_string == NULL)
596 errx(1, "unknown media mode 0x%x", type);
597
598 for (i = 0; ttos->modes[i].desc != NULL; i++) {
599 rval = lookup_media_word(ttos->modes[i].desc, val);
600 if (rval != -1)
601 return (rval);
602 }
603 return -1;
604 }
605 #endif
606
607 static int
608 get_media_options(int type, const char *val)
609 {
610 struct ifmedia_description *desc;
611 struct ifmedia_type_to_subtype *ttos;
612 char *optlist, *optptr;
613 int option = 0, i, rval = 0;
614
615 /* We muck with the string, so copy it. */
616 optlist = strdup(val);
617 if (optlist == NULL)
618 err(1, "strdup");
619
620 /* Find the top-level interface type. */
621 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
622 desc->ifmt_string != NULL; desc++, ttos++)
623 if (type == desc->ifmt_word)
624 break;
625 if (desc->ifmt_string == NULL)
626 errx(1, "unknown media type 0x%x", type);
627
628 /*
629 * Look up the options in the user-provided comma-separated
630 * list.
631 */
632 optptr = optlist;
633 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
634 for (i = 0; ttos->options[i].desc != NULL; i++) {
635 option = lookup_media_word(ttos->options[i].desc, optptr);
636 if (option != -1)
637 break;
638 }
639 if (option == 0)
640 errx(1, "unknown option: %s", optptr);
641 rval |= option;
642 }
643
644 free(optlist);
645 return (rval);
646 }
647
648 static int
649 lookup_media_word(struct ifmedia_description *desc, const char *val)
650 {
651
652 for (; desc->ifmt_string != NULL; desc++)
653 if (strcasecmp(desc->ifmt_string, val) == 0)
654 return (desc->ifmt_word);
655
656 return (-1);
657 }
658
659 static struct ifmedia_description *get_toptype_desc(int ifmw)
660 {
661 struct ifmedia_description *desc;
662
663 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
664 if (IFM_TYPE(ifmw) == desc->ifmt_word)
665 break;
666
667 return desc;
668 }
669
670 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
671 {
672 struct ifmedia_description *desc;
673 struct ifmedia_type_to_subtype *ttos;
674
675 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
676 desc->ifmt_string != NULL; desc++, ttos++)
677 if (IFM_TYPE(ifmw) == desc->ifmt_word)
678 break;
679
680 return ttos;
681 }
682
683 static struct ifmedia_description *get_subtype_desc(int ifmw,
684 struct ifmedia_type_to_subtype *ttos)
685 {
686 int i;
687 struct ifmedia_description *desc;
688
689 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
690 if (ttos->subtypes[i].alias)
691 continue;
692 for (desc = ttos->subtypes[i].desc;
693 desc->ifmt_string != NULL; desc++) {
694 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
695 return desc;
696 }
697 }
698
699 return NULL;
700 }
701
702 #ifdef notdef
703 static struct ifmedia_description *get_mode_desc(int ifmw,
704 struct ifmedia_type_to_subtype *ttos)
705 {
706 int i;
707 struct ifmedia_description *desc;
708
709 for (i = 0; ttos->modes[i].desc != NULL; i++) {
710 if (ttos->modes[i].alias)
711 continue;
712 for (desc = ttos->modes[i].desc;
713 desc->ifmt_string != NULL; desc++) {
714 if (IFM_MODE(ifmw) == desc->ifmt_word)
715 return desc;
716 }
717 }
718
719 return NULL;
720 }
721 #endif
722
723 static void
724 print_media_word(int ifmw, int print_toptype)
725 {
726 struct ifmedia_description *desc;
727 struct ifmedia_type_to_subtype *ttos;
728 int seen_option = 0, i;
729
730 /* Find the top-level interface type. */
731 desc = get_toptype_desc(ifmw);
732 ttos = get_toptype_ttos(ifmw);
733 if (desc->ifmt_string == NULL) {
734 printf("<unknown type>");
735 return;
736 #ifdef notdef
737 } else if (print_toptype) {
738 printf("%s", desc->ifmt_string);
739 #endif
740 }
741
742 /*
743 * Don't print the top-level type; it's not like we can
744 * change it, or anything.
745 */
746
747 /* Find subtype. */
748 desc = get_subtype_desc(ifmw, ttos);
749 if (desc == NULL) {
750 printf("<unknown subtype>");
751 return;
752 }
753
754 #ifdef notdef
755 if (print_toptype)
756 putchar(' ');
757 #endif
758
759 printf("%s", desc->ifmt_string);
760
761 #ifdef notdef
762 if (print_toptype) {
763 desc = get_mode_desc(ifmw, ttos);
764 if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string))
765 printf(" mode %s", desc->ifmt_string);
766 }
767 #endif
768 /* Find options. */
769 for (i = 0; ttos->options[i].desc != NULL; i++) {
770 if (ttos->options[i].alias)
771 continue;
772 for (desc = ttos->options[i].desc;
773 desc->ifmt_string != NULL; desc++) {
774 if (ifmw & desc->ifmt_word) {
775 if (seen_option == 0)
776 printf(" <");
777 printf("%s%s", seen_option++ ? "," : "",
778 desc->ifmt_string);
779 }
780 }
781 }
782 printf("%s", seen_option ? ">" : "");
783
784 #ifdef notdef
785 if (print_toptype && IFM_INST(ifmw) != 0)
786 printf(" instance %d", IFM_INST(ifmw));
787 #endif
788 }
789
790 static void
791 print_media_word_ifconfig(int ifmw)
792 {
793 struct ifmedia_description *desc;
794 struct ifmedia_type_to_subtype *ttos;
795 int i;
796
797 /* Find the top-level interface type. */
798 desc = get_toptype_desc(ifmw);
799 ttos = get_toptype_ttos(ifmw);
800 if (desc->ifmt_string == NULL) {
801 printf("<unknown type>");
802 return;
803 }
804
805 /*
806 * Don't print the top-level type; it's not like we can
807 * change it, or anything.
808 */
809
810 /* Find subtype. */
811 desc = get_subtype_desc(ifmw, ttos);
812 if (desc == NULL) {
813 printf("<unknown subtype>");
814 return;
815 }
816
817 printf("media %s", desc->ifmt_string);
818
819 #ifdef notdef
820 desc = get_mode_desc(ifmw, ttos);
821 if (desc != NULL)
822 printf(" mode %s", desc->ifmt_string);
823 #endif
824
825 /* Find options. */
826 for (i = 0; ttos->options[i].desc != NULL; i++) {
827 if (ttos->options[i].alias)
828 continue;
829 for (desc = ttos->options[i].desc;
830 desc->ifmt_string != NULL; desc++) {
831 if (ifmw & desc->ifmt_word) {
832 printf(" mediaopt %s", desc->ifmt_string);
833 }
834 }
835 }
836
837 if (IFM_INST(ifmw) != 0)
838 printf(" instance %d", IFM_INST(ifmw));
839 }
840
841 /**********************************************************************
842 * ...until here.
843 **********************************************************************/
844
845 static struct cmd media_cmds[] = {
846 DEF_CMD_ARG("media", setmedia),
847 #ifdef notdef
848 DEF_CMD_ARG("mode", setmediamode),
849 #endif
850 DEF_CMD_ARG("mediaopt", setmediaopt),
851 DEF_CMD_ARG("-mediaopt",unsetmediaopt),
852 DEF_CMD_ARG("inst", setmediainst),
853 DEF_CMD_ARG("instance", setmediainst),
854 };
855 static struct afswtch af_media = {
856 .af_name = "af_media",
857 .af_af = AF_UNSPEC,
858 .af_other_status = media_status,
859 };
860
861 static __constructor void
862 ifmedia_ctor(void)
863 {
864 #define N(a) (sizeof(a) / sizeof(a[0]))
865 int i;
866
867 for (i = 0; i < N(media_cmds); i++)
868 cmd_register(&media_cmds[i]);
869 af_register(&af_media);
870 #undef N
871 }