1 /* $NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $ */
2 /* $FreeBSD: src/sbin/ifconfig/ifmedia.c,v 1.6 1999/08/28 00:13:08 peter 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. 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.
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
69 #include <sys/param.h>
70 #include <sys/ioctl.h>
71 #include <sys/socket.h>
72 #include <sys/sysctl.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_media.h>
79 #include <net/route.h>
92 static void domediaopt
__P((const char *, int, int));
93 static int get_media_subtype
__P((int, const char *));
94 static int get_media_options
__P((int, const char *));
95 static int lookup_media_word
__P((struct ifmedia_description
*, const char *));
96 static void print_media_word
__P((int));
101 media_status(s
, info
)
103 struct rt_addrinfo
*info
;
105 struct ifmediareq ifmr
;
108 (void) memset(&ifmr
, 0, sizeof(ifmr
));
109 (void) strncpy(ifmr
.ifm_name
, name
, sizeof(ifmr
.ifm_name
));
111 if (ioctl(s
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) < 0) {
113 * Interface doesn't support SIOC{G,S}IFMEDIA.
118 if (ifmr
.ifm_count
== 0) {
119 //warnx("%s: no media types?", name);
124 print_media_word(ifmr
.ifm_current
);
125 if (ifmr
.ifm_active
!= ifmr
.ifm_current
) {
128 print_media_word(ifmr
.ifm_active
);
132 if (ifmr
.ifm_status
& IFM_AVALID
) {
135 switch (IFM_TYPE(ifmr
.ifm_active
)) {
137 if (ifmr
.ifm_status
& IFM_ACTIVE
)
140 printf("no carrier");
145 if (ifmr
.ifm_status
& IFM_ACTIVE
)
152 if (ifmr
.ifm_status
& IFM_ACTIVE
)
164 media_list
= (int *)malloc(ifmr
.ifm_count
* sizeof(int));
165 if (media_list
== NULL
)
167 ifmr
.ifm_ulist
= media_list
;
169 if (ioctl(s
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) < 0)
170 err(1, "SIOCGIFMEDIA");
172 if (ifmr
.ifm_count
> 0) {
173 printf("\tsupported media:");
174 for (i
= 0; i
< ifmr
.ifm_count
; i
++) {
176 print_media_word(media_list
[i
]);
185 setmedia(val
, d
, s
, afp
)
189 const struct afswtch
*afp
;
191 struct ifmediareq ifmr
;
192 int first_type
, subtype
;
194 (void) memset(&ifmr
, 0, sizeof(ifmr
));
195 (void) strncpy(ifmr
.ifm_name
, name
, sizeof(ifmr
.ifm_name
));
198 ifmr
.ifm_ulist
= &first_type
;
199 if (ioctl(s
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) < 0) {
201 * If we get E2BIG, the kernel is telling us
202 * that there are more, so we can ignore it.
205 err(1, "SIOCGIFMEDIA");
208 if (ifmr
.ifm_count
== 0)
209 errx(1, "%s: no media types?", name
);
212 * We are primarily concerned with the top-level type.
213 * However, "current" may be only IFM_NONE, so we just look
214 * for the top-level type in the first "supported type"
217 * (I'm assuming that all supported media types for a given
218 * interface will be the same top-level type..)
220 subtype
= get_media_subtype(IFM_TYPE(first_type
), val
);
222 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
223 ifr
.ifr_media
= (ifmr
.ifm_current
& ~(IFM_NMASK
|IFM_TMASK
)) |
224 IFM_TYPE(first_type
) | subtype
;
226 if (ioctl(s
, SIOCSIFMEDIA
, (caddr_t
)&ifr
) < 0)
227 err(1, "SIOCSIFMEDIA");
231 setmediaopt(val
, d
, s
, afp
)
235 const struct afswtch
*afp
;
238 domediaopt(val
, 0, s
);
242 unsetmediaopt(val
, d
, s
, afp
)
246 const struct afswtch
*afp
;
249 domediaopt(val
, 1, s
);
253 domediaopt(val
, clear
, s
)
258 struct ifmediareq ifmr
;
259 int *mwords
, options
;
261 (void) memset(&ifmr
, 0, sizeof(ifmr
));
262 (void) strncpy(ifmr
.ifm_name
, name
, sizeof(ifmr
.ifm_name
));
265 * We must go through the motions of reading all
266 * supported media because we need to know both
267 * the current media type and the top-level type.
270 if (ioctl(s
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) < 0)
271 err(1, "SIOCGIFMEDIA");
273 if (ifmr
.ifm_count
== 0)
274 errx(1, "%s: no media types?", name
);
276 mwords
= (int *)malloc(ifmr
.ifm_count
* sizeof(int));
280 ifmr
.ifm_ulist
= mwords
;
281 if (ioctl(s
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) < 0)
282 err(1, "SIOCGIFMEDIA");
284 options
= get_media_options(IFM_TYPE(mwords
[0]), val
);
288 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
289 ifr
.ifr_media
= ifmr
.ifm_current
;
291 ifr
.ifr_media
&= ~options
;
293 ifr
.ifr_media
|= options
;
295 if (ioctl(s
, SIOCSIFMEDIA
, (caddr_t
)&ifr
) < 0)
296 err(1, "SIOCSIFMEDIA");
299 /**********************************************************************
300 * A good chunk of this is duplicated from sys/net/ifmedia.c
301 **********************************************************************/
303 static struct ifmedia_description ifm_type_descriptions
[] =
304 IFM_TYPE_DESCRIPTIONS
;
306 static struct ifmedia_description ifm_subtype_ethernet_descriptions
[] =
307 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS
;
309 static struct ifmedia_description ifm_subtype_ethernet_aliases
[] =
310 IFM_SUBTYPE_ETHERNET_ALIASES
;
312 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions
[] =
313 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS
;
315 static struct ifmedia_description ifm_subtype_tokenring_descriptions
[] =
316 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS
;
318 static struct ifmedia_description ifm_subtype_tokenring_aliases
[] =
319 IFM_SUBTYPE_TOKENRING_ALIASES
;
321 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions
[] =
322 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS
;
324 static struct ifmedia_description ifm_subtype_fddi_descriptions
[] =
325 IFM_SUBTYPE_FDDI_DESCRIPTIONS
;
327 static struct ifmedia_description ifm_subtype_fddi_aliases
[] =
328 IFM_SUBTYPE_FDDI_ALIASES
;
330 static struct ifmedia_description ifm_subtype_fddi_option_descriptions
[] =
331 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS
;
333 static struct ifmedia_description ifm_subtype_ieee80211_descriptions
[] =
334 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS
;
336 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions
[] =
337 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS
;
339 static struct ifmedia_description ifm_subtype_shared_descriptions
[] =
340 IFM_SUBTYPE_SHARED_DESCRIPTIONS
;
342 static struct ifmedia_description ifm_subtype_shared_aliases
[] =
343 IFM_SUBTYPE_SHARED_ALIASES
;
345 static struct ifmedia_description ifm_shared_option_descriptions
[] =
346 IFM_SHARED_OPTION_DESCRIPTIONS
;
348 struct ifmedia_type_to_subtype
{
350 struct ifmedia_description
*desc
;
354 struct ifmedia_description
*desc
;
359 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
360 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes
[] = {
363 { &ifm_subtype_shared_descriptions
[0], 0 },
364 { &ifm_subtype_shared_aliases
[0], 1 },
365 { &ifm_subtype_ethernet_descriptions
[0], 0 },
366 { &ifm_subtype_ethernet_aliases
[0], 1 },
370 { &ifm_shared_option_descriptions
[0], 0 },
371 { &ifm_subtype_ethernet_option_descriptions
[0], 1 },
377 { &ifm_subtype_shared_descriptions
[0], 0 },
378 { &ifm_subtype_shared_aliases
[0], 1 },
379 { &ifm_subtype_tokenring_descriptions
[0], 0 },
380 { &ifm_subtype_tokenring_aliases
[0], 1 },
384 { &ifm_shared_option_descriptions
[0], 0 },
385 { &ifm_subtype_tokenring_option_descriptions
[0], 1 },
391 { &ifm_subtype_shared_descriptions
[0], 0 },
392 { &ifm_subtype_shared_aliases
[0], 1 },
393 { &ifm_subtype_fddi_descriptions
[0], 0 },
394 { &ifm_subtype_fddi_aliases
[0], 1 },
398 { &ifm_shared_option_descriptions
[0], 0 },
399 { &ifm_subtype_fddi_option_descriptions
[0], 1 },
405 { &ifm_subtype_shared_descriptions
[0], 0 },
406 { &ifm_subtype_shared_aliases
[0], 1 },
407 { &ifm_subtype_ieee80211_descriptions
[0], 0 },
411 { &ifm_shared_option_descriptions
[0], 0 },
412 { &ifm_subtype_ieee80211_option_descriptions
[0], 1 },
419 get_media_subtype(type
, val
)
423 struct ifmedia_description
*desc
;
424 struct ifmedia_type_to_subtype
*ttos
;
427 /* Find the top-level interface type. */
428 for (desc
= ifm_type_descriptions
, ttos
= ifmedia_types_to_subtypes
;
429 desc
->ifmt_string
!= NULL
; desc
++, ttos
++)
430 if (type
== desc
->ifmt_word
)
432 if (desc
->ifmt_string
== NULL
)
433 errx(1, "unknown media type 0x%x", type
);
435 for (i
= 0; ttos
->subtypes
[i
].desc
!= NULL
; i
++) {
436 rval
= lookup_media_word(ttos
->subtypes
[i
].desc
, val
);
440 errx(1, "unknown media subtype: %s", val
);
445 get_media_options(type
, val
)
449 struct ifmedia_description
*desc
;
450 struct ifmedia_type_to_subtype
*ttos
;
451 char *optlist
, *optptr
;
452 int option
= 0, i
, rval
= 0;
454 /* We muck with the string, so copy it. */
455 optlist
= strdup(val
);
459 /* Find the top-level interface type. */
460 for (desc
= ifm_type_descriptions
, ttos
= ifmedia_types_to_subtypes
;
461 desc
->ifmt_string
!= NULL
; desc
++, ttos
++)
462 if (type
== desc
->ifmt_word
)
464 if (desc
->ifmt_string
== NULL
)
465 errx(1, "unknown media type 0x%x", type
);
468 * Look up the options in the user-provided comma-separated
472 for (; (optptr
= strtok(optptr
, ",")) != NULL
; optptr
= NULL
) {
473 for (i
= 0; ttos
->options
[i
].desc
!= NULL
; i
++) {
474 option
= lookup_media_word(ttos
->options
[i
].desc
, optptr
);
479 errx(1, "unknown option: %s", optptr
);
488 lookup_media_word(desc
, val
)
489 struct ifmedia_description
*desc
;
493 for (; desc
->ifmt_string
!= NULL
; desc
++)
494 if (strcasecmp(desc
->ifmt_string
, val
) == 0)
495 return (desc
->ifmt_word
);
501 print_media_word(ifmw
)
504 struct ifmedia_description
*desc
;
505 struct ifmedia_type_to_subtype
*ttos
;
506 int seen_option
= 0, i
;
508 /* Find the top-level interface type. */
509 for (desc
= ifm_type_descriptions
, ttos
= ifmedia_types_to_subtypes
;
510 desc
->ifmt_string
!= NULL
; desc
++, ttos
++)
511 if (IFM_TYPE(ifmw
) == desc
->ifmt_word
)
513 if (desc
->ifmt_string
== NULL
) {
514 printf("<unknown type>");
519 * Don't print the top-level type; it's not like we can
520 * change it, or anything.
524 for (i
= 0; ttos
->subtypes
[i
].desc
!= NULL
; i
++) {
525 if (ttos
->subtypes
[i
].alias
)
527 for (desc
= ttos
->subtypes
[i
].desc
;
528 desc
->ifmt_string
!= NULL
; desc
++) {
529 if (IFM_SUBTYPE(ifmw
) == desc
->ifmt_word
)
534 /* Falling to here means unknown subtype. */
535 printf("<unknown subtype>");
539 printf("%s", desc
->ifmt_string
);
542 for (i
= 0; ttos
->options
[i
].desc
!= NULL
; i
++) {
543 if (ttos
->options
[i
].alias
)
545 for (desc
= ttos
->options
[i
].desc
;
546 desc
->ifmt_string
!= NULL
; desc
++) {
547 if (ifmw
& desc
->ifmt_word
) {
548 if (seen_option
== 0)
550 printf("%s%s", seen_option
++ ? "," : "",
555 printf("%s", seen_option
? ">" : "");
558 /**********************************************************************
560 **********************************************************************/