1 /* $NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $ */
2 /* $FreeBSD: src/sbin/ifconfig/ifmedia.c,v 1.19.2.1 2006/03/01 22:24:23 glebius Exp $ */
5 * Copyright (c) 1997 Jason R. Thorpe.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
37 * Copyright (c) 1983, 1993
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #include <sys/param.h>
66 #include <sys/ioctl.h>
67 #include <sys/socket.h>
68 #include <sys/sysctl.h>
72 #include <net/if_dl.h>
73 #include <net/if_types.h>
74 #include <net/if_media.h>
75 #include <net/route.h>
88 static void domediaopt(const char *, int, int);
89 static int get_media_subtype(int, const char *);
90 static int get_media_mode(int, const char *);
91 static int get_media_options(int, const char *);
92 static int lookup_media_word(struct ifmedia_description
*, const char *);
93 static void print_media_word(int, int);
94 static void print_media_word_ifconfig(int);
96 static struct ifmedia_description
*get_toptype_desc(int);
97 static struct ifmedia_type_to_subtype
*get_toptype_ttos(int);
98 static struct ifmedia_description
*get_subtype_desc(int,
99 struct ifmedia_type_to_subtype
*ttos
);
104 struct ifmediareq ifmr
;
107 memset(&ifmr
, 0, sizeof(ifmr
));
108 strncpy(ifmr
.ifm_name
, name
, sizeof(ifmr
.ifm_name
));
110 if (ioctl(s
, SIOCGIFMEDIA
, &ifmr
) < 0) {
112 * Interface doesn't support SIOC{G,S}IFMEDIA.
117 if (ifmr
.ifm_count
== 0) {
118 warnx("%s: no media types?", name
);
122 media_list
= malloc(ifmr
.ifm_count
* sizeof(int));
123 if (media_list
== NULL
)
125 ifmr
.ifm_ulist
= media_list
;
127 if (ioctl(s
, SIOCGIFMEDIA
, &ifmr
) < 0)
128 err(1, "SIOCGIFMEDIA");
131 print_media_word(ifmr
.ifm_current
, 1);
132 if (ifmr
.ifm_active
!= ifmr
.ifm_current
) {
135 print_media_word(ifmr
.ifm_active
, 0);
141 if (ifmr
.ifm_status
& IFM_AVALID
) {
142 printf("\tstatus: ");
143 switch (IFM_TYPE(ifmr
.ifm_active
)) {
145 if (ifmr
.ifm_status
& IFM_ACTIVE
)
148 printf("no carrier");
152 if (ifmr
.ifm_status
& IFM_ACTIVE
)
155 printf("no carrier");
159 /* XXX: Different value for adhoc? */
160 if (ifmr
.ifm_status
& IFM_ACTIVE
)
161 printf("associated");
163 printf("no carrier");
166 if (ifmr
.ifm_status
& IFM_ACTIVE
)
175 if (ifmr
.ifm_count
> 0 && supmedia
) {
176 printf("\tsupported media:\n");
177 for (i
= 0; i
< ifmr
.ifm_count
; i
++) {
179 print_media_word_ifconfig(media_list
[i
]);
188 ifmedia_getstate(int s
)
190 static struct ifmediareq
*ifmr
= NULL
;
194 ifmr
= (struct ifmediareq
*)malloc(sizeof(struct ifmediareq
));
198 (void) memset(ifmr
, 0, sizeof(struct ifmediareq
));
199 (void) strncpy(ifmr
->ifm_name
, name
,
200 sizeof(ifmr
->ifm_name
));
203 ifmr
->ifm_ulist
= NULL
;
206 * We must go through the motions of reading all
207 * supported media because we need to know both
208 * the current media type and the top-level type.
211 if (ioctl(s
, SIOCGIFMEDIA
, (caddr_t
)ifmr
) < 0) {
212 err(1, "SIOCGIFMEDIA");
215 if (ifmr
->ifm_count
== 0)
216 errx(1, "%s: no media types?", name
);
218 mwords
= (int *)malloc(ifmr
->ifm_count
* sizeof(int));
222 ifmr
->ifm_ulist
= mwords
;
223 if (ioctl(s
, SIOCGIFMEDIA
, (caddr_t
)ifmr
) < 0)
224 err(1, "SIOCGIFMEDIA");
231 setifmediacallback(int s
, void *arg
)
233 struct ifmediareq
*ifmr
= (struct ifmediareq
*)arg
;
234 static int did_it
= 0;
237 ifr
.ifr_media
= ifmr
->ifm_current
;
238 if (ioctl(s
, SIOCSIFMEDIA
, (caddr_t
)&ifr
) < 0)
239 err(1, "SIOCSIFMEDIA (media)");
240 free(ifmr
->ifm_ulist
);
247 setmedia(const char *val
, int d
, int s
, const struct afswtch
*afp
)
249 struct ifmediareq
*ifmr
;
253 ifmr
= ifmedia_getstate(s
);
256 * We are primarily concerned with the top-level type.
257 * However, "current" may be only IFM_NONE, so we just look
258 * for the top-level type in the first "supported type"
261 * (I'm assuming that all supported media types for a given
262 * interface will be the same top-level type..)
264 subtype
= get_media_subtype(IFM_TYPE(ifmr
->ifm_ulist
[0]), val
);
266 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
267 ifr
.ifr_media
= (ifmr
->ifm_current
& ~(IFM_NMASK
|IFM_TMASK
)) |
268 IFM_TYPE(ifmr
->ifm_ulist
[0]) | subtype
;
270 if ((ifr
.ifr_media
& IFM_TMASK
) == 0) {
271 ifr
.ifr_media
&= ~IFM_GMASK
;
274 ifmr
->ifm_current
= ifr
.ifr_media
;
275 callback_register(setifmediacallback
, (void *)ifmr
);
279 setmediaopt(const char *val
, int d
, int s
, const struct afswtch
*afp
)
282 domediaopt(val
, 0, s
);
286 unsetmediaopt(const char *val
, int d
, int s
, const struct afswtch
*afp
)
289 domediaopt(val
, 1, s
);
293 domediaopt(const char *val
, int clear
, int s
)
295 struct ifmediareq
*ifmr
;
298 ifmr
= ifmedia_getstate(s
);
300 options
= get_media_options(IFM_TYPE(ifmr
->ifm_ulist
[0]), val
);
302 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
303 ifr
.ifr_media
= ifmr
->ifm_current
;
305 ifr
.ifr_media
&= ~options
;
307 ifr
.ifr_media
|= options
;
309 ifmr
->ifm_current
= ifr
.ifr_media
;
310 callback_register(setifmediacallback
, (void *)ifmr
);
315 setmediamode(const char *val
, int d
, int s
, const struct afswtch
*afp
)
317 struct ifmediareq
*ifmr
;
320 ifmr
= ifmedia_getstate(s
);
322 mode
= get_media_mode(IFM_TYPE(ifmr
->ifm_ulist
[0]), val
);
324 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
325 ifr
.ifr_media
= (ifmr
->ifm_current
& ~IFM_MMASK
) | mode
;
327 ifmr
->ifm_current
= ifr
.ifr_media
;
328 callback_register(setifmediacallback
, (void *)ifmr
);
331 /**********************************************************************
332 * A good chunk of this is duplicated from sys/net/ifmedia.c
333 **********************************************************************/
335 static struct ifmedia_description ifm_type_descriptions
[] =
336 IFM_TYPE_DESCRIPTIONS
;
338 static struct ifmedia_description ifm_subtype_ethernet_descriptions
[] =
339 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS
;
341 static struct ifmedia_description ifm_subtype_ethernet_aliases
[] =
342 IFM_SUBTYPE_ETHERNET_ALIASES
;
344 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions
[] =
345 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS
;
347 static struct ifmedia_description ifm_subtype_ethernet_option_alias
[] =
348 IFM_SUBTYPE_ETHERNET_OPTION_ALIAS
;
350 static struct ifmedia_description ifm_subtype_ieee80211_descriptions
[] =
351 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS
;
353 static struct ifmedia_description ifm_subtype_ieee80211_aliases
[] =
354 IFM_SUBTYPE_IEEE80211_ALIASES
;
356 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions
[] =
357 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS
;
359 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions
[] =
360 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS
;
362 struct ifmedia_description ifm_subtype_ieee80211_mode_aliases
[] =
363 IFM_SUBTYPE_IEEE80211_MODE_ALIASES
;
365 static struct ifmedia_description ifm_subtype_atm_descriptions
[] =
366 IFM_SUBTYPE_ATM_DESCRIPTIONS
;
368 static struct ifmedia_description ifm_subtype_atm_aliases
[] =
369 IFM_SUBTYPE_ATM_ALIASES
;
371 static struct ifmedia_description ifm_subtype_atm_option_descriptions
[] =
372 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS
;
374 static struct ifmedia_description ifm_subtype_shared_descriptions
[] =
375 IFM_SUBTYPE_SHARED_DESCRIPTIONS
;
377 static struct ifmedia_description ifm_subtype_shared_aliases
[] =
378 IFM_SUBTYPE_SHARED_ALIASES
;
380 static struct ifmedia_description ifm_shared_option_descriptions
[] =
381 IFM_SHARED_OPTION_DESCRIPTIONS
;
383 struct ifmedia_type_to_subtype
{
385 struct ifmedia_description
*desc
;
389 struct ifmedia_description
*desc
;
393 struct ifmedia_description
*desc
;
398 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
399 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes
[] = {
402 { &ifm_subtype_shared_descriptions
[0], 0 },
403 { &ifm_subtype_shared_aliases
[0], 1 },
404 { &ifm_subtype_ethernet_descriptions
[0], 0 },
405 { &ifm_subtype_ethernet_aliases
[0], 1 },
409 { &ifm_shared_option_descriptions
[0], 0 },
410 { &ifm_subtype_ethernet_option_descriptions
[0], 0 },
411 { &ifm_subtype_ethernet_option_alias
[0], 1 },
420 { &ifm_subtype_shared_descriptions
[0], 0 },
421 { &ifm_subtype_shared_aliases
[0], 1 },
422 { &ifm_subtype_ieee80211_descriptions
[0], 0 },
423 { &ifm_subtype_ieee80211_aliases
[0], 1 },
427 { &ifm_shared_option_descriptions
[0], 0 },
428 { &ifm_subtype_ieee80211_option_descriptions
[0], 0 },
432 { &ifm_subtype_ieee80211_mode_descriptions
[0], 0 },
433 { &ifm_subtype_ieee80211_mode_aliases
[0], 0 },
439 { &ifm_subtype_shared_descriptions
[0], 0 },
440 { &ifm_subtype_shared_aliases
[0], 1 },
441 { &ifm_subtype_atm_descriptions
[0], 0 },
442 { &ifm_subtype_atm_aliases
[0], 1 },
446 { &ifm_shared_option_descriptions
[0], 0 },
447 { &ifm_subtype_atm_option_descriptions
[0], 0 },
457 get_media_subtype(int type
, const char *val
)
459 struct ifmedia_description
*desc
;
460 struct ifmedia_type_to_subtype
*ttos
;
463 /* Find the top-level interface type. */
464 for (desc
= ifm_type_descriptions
, ttos
= ifmedia_types_to_subtypes
;
465 desc
->ifmt_string
!= NULL
; desc
++, ttos
++)
466 if (type
== desc
->ifmt_word
)
468 if (desc
->ifmt_string
== NULL
)
469 errx(1, "unknown media type 0x%x", type
);
471 for (i
= 0; ttos
->subtypes
[i
].desc
!= NULL
; i
++) {
472 rval
= lookup_media_word(ttos
->subtypes
[i
].desc
, val
);
476 errx(1, "unknown media subtype: %s", val
);
481 get_media_mode(int type
, const char *val
)
483 struct ifmedia_description
*desc
;
484 struct ifmedia_type_to_subtype
*ttos
;
487 /* Find the top-level interface type. */
488 for (desc
= ifm_type_descriptions
, ttos
= ifmedia_types_to_subtypes
;
489 desc
->ifmt_string
!= NULL
; desc
++, ttos
++)
490 if (type
== desc
->ifmt_word
)
492 if (desc
->ifmt_string
== NULL
)
493 errx(1, "unknown media mode 0x%x", type
);
495 for (i
= 0; ttos
->modes
[i
].desc
!= NULL
; i
++) {
496 rval
= lookup_media_word(ttos
->modes
[i
].desc
, val
);
504 get_media_options(int type
, const char *val
)
506 struct ifmedia_description
*desc
;
507 struct ifmedia_type_to_subtype
*ttos
;
508 char *optlist
, *optptr
;
509 int option
= 0, i
, rval
= 0;
511 /* We muck with the string, so copy it. */
512 optlist
= strdup(val
);
516 /* Find the top-level interface type. */
517 for (desc
= ifm_type_descriptions
, ttos
= ifmedia_types_to_subtypes
;
518 desc
->ifmt_string
!= NULL
; desc
++, ttos
++)
519 if (type
== desc
->ifmt_word
)
521 if (desc
->ifmt_string
== NULL
)
522 errx(1, "unknown media type 0x%x", type
);
525 * Look up the options in the user-provided comma-separated
529 for (; (optptr
= strtok(optptr
, ",")) != NULL
; optptr
= NULL
) {
530 for (i
= 0; ttos
->options
[i
].desc
!= NULL
; i
++) {
531 option
= lookup_media_word(ttos
->options
[i
].desc
, optptr
);
536 errx(1, "unknown option: %s", optptr
);
545 lookup_media_word(struct ifmedia_description
*desc
, const char *val
)
548 for (; desc
->ifmt_string
!= NULL
; desc
++)
549 if (strcasecmp(desc
->ifmt_string
, val
) == 0)
550 return (desc
->ifmt_word
);
555 static struct ifmedia_description
*get_toptype_desc(int ifmw
)
557 struct ifmedia_description
*desc
;
559 for (desc
= ifm_type_descriptions
; desc
->ifmt_string
!= NULL
; desc
++)
560 if (IFM_TYPE(ifmw
) == desc
->ifmt_word
)
566 static struct ifmedia_type_to_subtype
*get_toptype_ttos(int ifmw
)
568 struct ifmedia_description
*desc
;
569 struct ifmedia_type_to_subtype
*ttos
;
571 for (desc
= ifm_type_descriptions
, ttos
= ifmedia_types_to_subtypes
;
572 desc
->ifmt_string
!= NULL
; desc
++, ttos
++)
573 if (IFM_TYPE(ifmw
) == desc
->ifmt_word
)
579 static struct ifmedia_description
*get_subtype_desc(int ifmw
,
580 struct ifmedia_type_to_subtype
*ttos
)
583 struct ifmedia_description
*desc
;
585 for (i
= 0; ttos
->subtypes
[i
].desc
!= NULL
; i
++) {
586 if (ttos
->subtypes
[i
].alias
)
588 for (desc
= ttos
->subtypes
[i
].desc
;
589 desc
->ifmt_string
!= NULL
; desc
++) {
590 if (IFM_SUBTYPE(ifmw
) == desc
->ifmt_word
)
598 static struct ifmedia_description
*get_mode_desc(int ifmw
,
599 struct ifmedia_type_to_subtype
*ttos
)
602 struct ifmedia_description
*desc
;
604 for (i
= 0; ttos
->modes
[i
].desc
!= NULL
; i
++) {
605 if (ttos
->modes
[i
].alias
)
607 for (desc
= ttos
->modes
[i
].desc
;
608 desc
->ifmt_string
!= NULL
; desc
++) {
609 if (IFM_MODE(ifmw
) == desc
->ifmt_word
)
618 print_media_word(int ifmw
, int print_toptype
)
620 struct ifmedia_description
*desc
;
621 struct ifmedia_type_to_subtype
*ttos
;
622 int seen_option
= 0, i
;
624 /* Find the top-level interface type. */
625 desc
= get_toptype_desc(ifmw
);
626 ttos
= get_toptype_ttos(ifmw
);
627 if (desc
->ifmt_string
== NULL
) {
628 printf("<unknown type>");
630 } else if (print_toptype
) {
631 printf("%s", desc
->ifmt_string
);
635 * Don't print the top-level type; it's not like we can
636 * change it, or anything.
640 desc
= get_subtype_desc(ifmw
, ttos
);
644 /* Falling to here means unknown subtype. */
645 printf("<unknown subtype>");
652 printf("%s", desc
->ifmt_string
);
655 desc
= get_mode_desc(ifmw
, ttos
);
656 if (desc
!= NULL
&& strcasecmp("autoselect", desc
->ifmt_string
))
657 printf(" mode %s", desc
->ifmt_string
);
661 for (i
= 0; ttos
->options
[i
].desc
!= NULL
; i
++) {
662 if (ttos
->options
[i
].alias
)
664 for (desc
= ttos
->options
[i
].desc
;
665 desc
->ifmt_string
!= NULL
; desc
++) {
666 if (ifmw
& desc
->ifmt_word
) {
667 if (seen_option
== 0)
669 printf("%s%s", seen_option
++ ? "," : "",
674 printf("%s", seen_option
? ">" : "");
678 print_media_word_ifconfig(int ifmw
)
680 struct ifmedia_description
*desc
;
681 struct ifmedia_type_to_subtype
*ttos
;
684 /* Find the top-level interface type. */
685 desc
= get_toptype_desc(ifmw
);
686 ttos
= get_toptype_ttos(ifmw
);
687 if (desc
->ifmt_string
== NULL
) {
688 printf("<unknown type>");
693 * Don't print the top-level type; it's not like we can
694 * change it, or anything.
698 desc
= get_subtype_desc(ifmw
, ttos
);
702 /* Falling to here means unknown subtype. */
703 printf("<unknown subtype>");
707 printf("media %s", desc
->ifmt_string
);
709 desc
= get_mode_desc(ifmw
, ttos
);
711 printf(" mode %s", desc
->ifmt_string
);
714 for (i
= 0; ttos
->options
[i
].desc
!= NULL
; i
++) {
715 if (ttos
->options
[i
].alias
)
717 for (desc
= ttos
->options
[i
].desc
;
718 desc
->ifmt_string
!= NULL
; desc
++) {
719 if (ifmw
& desc
->ifmt_word
) {
720 printf(" mediaopt %s", desc
->ifmt_string
);
726 /**********************************************************************
728 **********************************************************************/
730 static struct cmd media_cmds
[] = {
731 DEF_CMD_ARG("media", setmedia
),
732 DEF_CMD_ARG("mode", setmediamode
),
733 DEF_CMD_ARG("mediaopt", setmediaopt
),
734 DEF_CMD_ARG("-mediaopt",unsetmediaopt
),
736 static struct afswtch af_media
= {
737 .af_name
= "af_media",
739 .af_other_status
= media_status
,
742 static __constructor(101) void
745 #define N(a) (sizeof(a) / sizeof(a[0]))
748 for (i
= 0; i
< N(media_cmds
); i
++)
749 cmd_register(&media_cmds
[i
]);
750 af_register(&af_media
);