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.2.3 2001/11/14 04:35:07 yar Exp $ */
3 /* $DragonFly: src/sbin/ifconfig/ifmedia.c,v 1.2 2003/06/17 04:27:33 dillon Exp $ */
6 * Copyright (c) 1997 Jason R. Thorpe.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
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
38 * Copyright (c) 1983, 1993
39 * The Regents of the University of California. All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
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.
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
70 #include <sys/param.h>
71 #include <sys/ioctl.h>
72 #include <sys/socket.h>
73 #include <sys/sysctl.h>
77 #include <net/if_dl.h>
78 #include <net/if_types.h>
79 #include <net/if_media.h>
80 #include <net/route.h>
93 static void domediaopt __P((const char *, int, int));
94 static int get_media_subtype __P((int, const char *));
95 static int get_media_options __P((int, const char *));
96 static int lookup_media_word __P((struct ifmedia_description *, const char *));
97 static void print_media_word __P((int, int));
98 static void print_media_word_ifconfig __P((int));
100 static struct ifmedia_description *get_toptype_desc __P((int));
101 static struct ifmedia_type_to_subtype *get_toptype_ttos __P((int));
102 static struct ifmedia_description *get_subtype_desc __P((int,
103 struct ifmedia_type_to_subtype *ttos));
106 media_status(s, info)
108 struct rt_addrinfo *info __unused;
110 struct ifmediareq ifmr;
113 (void) memset(&ifmr, 0, sizeof(ifmr));
114 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
116 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
118 * Interface doesn't support SIOC{G,S}IFMEDIA.
123 if (ifmr.ifm_count == 0) {
124 warnx("%s: no media types?", name);
128 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
129 if (media_list == NULL)
131 ifmr.ifm_ulist = media_list;
133 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
134 err(1, "SIOCGIFMEDIA");
137 print_media_word(ifmr.ifm_current, 1);
138 if (ifmr.ifm_active != ifmr.ifm_current) {
141 print_media_word(ifmr.ifm_active, 0);
147 if (ifmr.ifm_status & IFM_AVALID) {
148 printf("\tstatus: ");
149 switch (IFM_TYPE(ifmr.ifm_active)) {
151 if (ifmr.ifm_status & IFM_ACTIVE)
154 printf("no carrier");
159 if (ifmr.ifm_status & IFM_ACTIVE)
165 /* XXX: Different value for adhoc? */
166 if (ifmr.ifm_status & IFM_ACTIVE)
167 printf("associated");
169 printf("no carrier");
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 setmedia(val, d, s, afp)
192 const struct afswtch *afp;
194 struct ifmediareq ifmr;
195 int first_type, subtype;
197 (void) memset(&ifmr, 0, sizeof(ifmr));
198 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
201 ifmr.ifm_ulist = &first_type;
202 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
204 * If we get E2BIG, the kernel is telling us
205 * that there are more, so we can ignore it.
208 err(1, "SIOCGIFMEDIA");
211 if (ifmr.ifm_count == 0)
212 errx(1, "%s: no media types?", name);
215 * We are primarily concerned with the top-level type.
216 * However, "current" may be only IFM_NONE, so we just look
217 * for the top-level type in the first "supported type"
220 * (I'm assuming that all supported media types for a given
221 * interface will be the same top-level type..)
223 subtype = get_media_subtype(IFM_TYPE(first_type), val);
225 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
226 ifr.ifr_media = (ifmr.ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
227 IFM_TYPE(first_type) | subtype;
229 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
230 err(1, "SIOCSIFMEDIA");
234 setmediaopt(val, d, s, afp)
238 const struct afswtch *afp;
241 domediaopt(val, 0, s);
245 unsetmediaopt(val, d, s, afp)
249 const struct afswtch *afp;
252 domediaopt(val, 1, s);
256 domediaopt(val, clear, s)
261 struct ifmediareq ifmr;
262 int *mwords, options;
264 (void) memset(&ifmr, 0, sizeof(ifmr));
265 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
268 * We must go through the motions of reading all
269 * supported media because we need to know both
270 * the current media type and the top-level type.
273 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
274 err(1, "SIOCGIFMEDIA");
276 if (ifmr.ifm_count == 0)
277 errx(1, "%s: no media types?", name);
279 mwords = (int *)malloc(ifmr.ifm_count * sizeof(int));
283 ifmr.ifm_ulist = mwords;
284 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
285 err(1, "SIOCGIFMEDIA");
287 options = get_media_options(IFM_TYPE(mwords[0]), val);
291 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
292 ifr.ifr_media = ifmr.ifm_current;
294 ifr.ifr_media &= ~options;
296 ifr.ifr_media |= options;
298 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
299 err(1, "SIOCSIFMEDIA");
302 /**********************************************************************
303 * A good chunk of this is duplicated from sys/net/ifmedia.c
304 **********************************************************************/
306 static struct ifmedia_description ifm_type_descriptions[] =
307 IFM_TYPE_DESCRIPTIONS;
309 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
310 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
312 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
313 IFM_SUBTYPE_ETHERNET_ALIASES;
315 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
316 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
318 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
319 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
321 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
322 IFM_SUBTYPE_TOKENRING_ALIASES;
324 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
325 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
327 static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
328 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
330 static struct ifmedia_description ifm_subtype_fddi_aliases[] =
331 IFM_SUBTYPE_FDDI_ALIASES;
333 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
334 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
336 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
337 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
339 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
340 IFM_SUBTYPE_IEEE80211_ALIASES;
342 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
343 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
345 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
346 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
348 static struct ifmedia_description ifm_subtype_shared_aliases[] =
349 IFM_SUBTYPE_SHARED_ALIASES;
351 static struct ifmedia_description ifm_shared_option_descriptions[] =
352 IFM_SHARED_OPTION_DESCRIPTIONS;
354 struct ifmedia_type_to_subtype {
356 struct ifmedia_description *desc;
360 struct ifmedia_description *desc;
365 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
366 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
369 { &ifm_subtype_shared_descriptions[0], 0 },
370 { &ifm_subtype_shared_aliases[0], 1 },
371 { &ifm_subtype_ethernet_descriptions[0], 0 },
372 { &ifm_subtype_ethernet_aliases[0], 1 },
376 { &ifm_shared_option_descriptions[0], 0 },
377 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
383 { &ifm_subtype_shared_descriptions[0], 0 },
384 { &ifm_subtype_shared_aliases[0], 1 },
385 { &ifm_subtype_tokenring_descriptions[0], 0 },
386 { &ifm_subtype_tokenring_aliases[0], 1 },
390 { &ifm_shared_option_descriptions[0], 0 },
391 { &ifm_subtype_tokenring_option_descriptions[0], 0 },
397 { &ifm_subtype_shared_descriptions[0], 0 },
398 { &ifm_subtype_shared_aliases[0], 1 },
399 { &ifm_subtype_fddi_descriptions[0], 0 },
400 { &ifm_subtype_fddi_aliases[0], 1 },
404 { &ifm_shared_option_descriptions[0], 0 },
405 { &ifm_subtype_fddi_option_descriptions[0], 0 },
411 { &ifm_subtype_shared_descriptions[0], 0 },
412 { &ifm_subtype_shared_aliases[0], 1 },
413 { &ifm_subtype_ieee80211_descriptions[0], 0 },
414 { &ifm_subtype_ieee80211_aliases[0], 1 },
418 { &ifm_shared_option_descriptions[0], 0 },
419 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
426 get_media_subtype(type, val)
430 struct ifmedia_description *desc;
431 struct ifmedia_type_to_subtype *ttos;
434 /* Find the top-level interface type. */
435 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
436 desc->ifmt_string != NULL; desc++, ttos++)
437 if (type == desc->ifmt_word)
439 if (desc->ifmt_string == NULL)
440 errx(1, "unknown media type 0x%x", type);
442 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
443 rval = lookup_media_word(ttos->subtypes[i].desc, val);
447 errx(1, "unknown media subtype: %s", val);
452 get_media_options(type, val)
456 struct ifmedia_description *desc;
457 struct ifmedia_type_to_subtype *ttos;
458 char *optlist, *optptr;
459 int option = 0, i, rval = 0;
461 /* We muck with the string, so copy it. */
462 optlist = strdup(val);
466 /* Find the top-level interface type. */
467 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
468 desc->ifmt_string != NULL; desc++, ttos++)
469 if (type == desc->ifmt_word)
471 if (desc->ifmt_string == NULL)
472 errx(1, "unknown media type 0x%x", type);
475 * Look up the options in the user-provided comma-separated
479 for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
480 for (i = 0; ttos->options[i].desc != NULL; i++) {
481 option = lookup_media_word(ttos->options[i].desc, optptr);
486 errx(1, "unknown option: %s", optptr);
495 lookup_media_word(desc, val)
496 struct ifmedia_description *desc;
500 for (; desc->ifmt_string != NULL; desc++)
501 if (strcasecmp(desc->ifmt_string, val) == 0)
502 return (desc->ifmt_word);
507 static struct ifmedia_description *get_toptype_desc(ifmw)
510 struct ifmedia_description *desc;
512 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
513 if (IFM_TYPE(ifmw) == desc->ifmt_word)
519 static struct ifmedia_type_to_subtype *get_toptype_ttos(ifmw)
522 struct ifmedia_description *desc;
523 struct ifmedia_type_to_subtype *ttos;
525 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
526 desc->ifmt_string != NULL; desc++, ttos++)
527 if (IFM_TYPE(ifmw) == desc->ifmt_word)
533 static struct ifmedia_description *get_subtype_desc(ifmw, ttos)
535 struct ifmedia_type_to_subtype *ttos;
538 struct ifmedia_description *desc;
540 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
541 if (ttos->subtypes[i].alias)
543 for (desc = ttos->subtypes[i].desc;
544 desc->ifmt_string != NULL; desc++) {
545 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
554 print_media_word(ifmw, print_toptype)
558 struct ifmedia_description *desc;
559 struct ifmedia_type_to_subtype *ttos;
560 int seen_option = 0, i;
562 /* Find the top-level interface type. */
563 desc = get_toptype_desc(ifmw);
564 ttos = get_toptype_ttos(ifmw);
565 if (desc->ifmt_string == NULL) {
566 printf("<unknown type>");
568 } else if (print_toptype) {
569 printf("%s", desc->ifmt_string);
573 * Don't print the top-level type; it's not like we can
574 * change it, or anything.
578 desc = get_subtype_desc(ifmw, ttos);
582 /* Falling to here means unknown subtype. */
583 printf("<unknown subtype>");
590 printf("%s", desc->ifmt_string);
593 for (i = 0; ttos->options[i].desc != NULL; i++) {
594 if (ttos->options[i].alias)
596 for (desc = ttos->options[i].desc;
597 desc->ifmt_string != NULL; desc++) {
598 if (ifmw & desc->ifmt_word) {
599 if (seen_option == 0)
601 printf("%s%s", seen_option++ ? "," : "",
606 printf("%s", seen_option ? ">" : "");
610 print_media_word_ifconfig(ifmw)
613 struct ifmedia_description *desc;
614 struct ifmedia_type_to_subtype *ttos;
617 /* Find the top-level interface type. */
618 desc = get_toptype_desc(ifmw);
619 ttos = get_toptype_ttos(ifmw);
620 if (desc->ifmt_string == NULL) {
621 printf("<unknown type>");
626 * Don't print the top-level type; it's not like we can
627 * change it, or anything.
631 desc = get_subtype_desc(ifmw, ttos);
635 /* Falling to here means unknown subtype. */
636 printf("<unknown subtype>");
640 printf("media %s", desc->ifmt_string);
643 for (i = 0; ttos->options[i].desc != NULL; i++) {
644 if (ttos->options[i].alias)
646 for (desc = ttos->options[i].desc;
647 desc->ifmt_string != NULL; desc++) {
648 if (ifmw & desc->ifmt_word) {
649 printf(" mediaopt %s", desc->ifmt_string);
655 /**********************************************************************
657 **********************************************************************/