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