9cf1db648749289194774936f865589438a61f02
[dragonfly.git] / sbin / ifconfig / 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.6.2.3 2001/11/14 04:35:07 yar Exp $ */
3 /* $DragonFly: src/sbin/ifconfig/ifmedia.c,v 1.7 2005/03/04 00:11:11 cpressey Exp $ */
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
93 static void     domediaopt(const char *, int, int);
94 static int      get_media_subtype(int, const char *);
95 static int      get_media_options(int, const char *);
96 static int      lookup_media_word(struct ifmedia_description *, const char *);
97 static void     print_media_word(int, int);
98 static void     print_media_word_ifconfig(int);
99
100 static struct ifmedia_description *get_toptype_desc(int);
101 static struct ifmedia_type_to_subtype *get_toptype_ttos(int);
102 static struct ifmedia_description *get_subtype_desc(int,
103     struct ifmedia_type_to_subtype *ttos);
104
105 void
106 media_status(int s, struct rt_addrinfo *info __unused)
107 {
108         struct ifmediareq ifmr;
109         int *media_list, i;
110
111         memset(&ifmr, 0, sizeof(ifmr));
112         strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
113
114         if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
115                 /*
116                  * Interface doesn't support SIOC{G,S}IFMEDIA.
117                  */
118                 return;
119         }
120
121         if (ifmr.ifm_count == 0) {
122                 warnx("%s: no media types?", name);
123                 return;
124         }
125
126         media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
127         if (media_list == NULL)
128                 err(1, "malloc");
129         ifmr.ifm_ulist = media_list;
130
131         if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
132                 err(1, "SIOCGIFMEDIA");
133
134         printf("\tmedia: ");
135         print_media_word(ifmr.ifm_current, 1);
136         if (ifmr.ifm_active != ifmr.ifm_current) {
137                 putchar(' ');
138                 putchar('(');
139                 print_media_word(ifmr.ifm_active, 0);
140                 putchar(')');
141         }
142
143         putchar('\n');
144
145         if (ifmr.ifm_status & IFM_AVALID) {
146                 printf("\tstatus: ");
147                 switch (IFM_TYPE(ifmr.ifm_active)) {
148                 case IFM_ETHER:
149                         if (ifmr.ifm_status & IFM_ACTIVE)
150                                 printf("active");
151                         else
152                                 printf("no carrier");
153                         break;
154
155                 case IFM_FDDI:
156                 case IFM_TOKEN:
157                         if (ifmr.ifm_status & IFM_ACTIVE)
158                                 printf("inserted");
159                         else
160                                 printf("no ring");
161                         break;
162                 case IFM_IEEE80211:
163                         /* XXX: Different value for adhoc? */
164                         if (ifmr.ifm_status & IFM_ACTIVE)
165                                 printf("associated");
166                         else
167                                 printf("no carrier");
168                         break;
169                 }
170                 putchar('\n');
171         }
172
173         if (ifmr.ifm_count > 0 && supmedia) {
174                 printf("\tsupported media:\n");
175                 for (i = 0; i < ifmr.ifm_count; i++) {
176                         printf("\t\t");
177                         print_media_word_ifconfig(media_list[i]);
178                         putchar('\n');
179                 }
180         }
181
182         free(media_list);
183 }
184
185 void
186 setmedia(const char *val, int d __unused, int s,
187          const struct afswtch *afp __unused)
188 {
189         struct ifmediareq ifmr;
190         int first_type, subtype;
191
192         memset(&ifmr, 0, sizeof(ifmr));
193         strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
194
195         ifmr.ifm_count = 1;
196         ifmr.ifm_ulist = &first_type;
197         if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
198                 /*
199                  * If we get E2BIG, the kernel is telling us
200                  * that there are more, so we can ignore it.
201                  */
202                 if (errno != E2BIG)
203                         err(1, "SIOCGIFMEDIA");
204         }
205
206         if (ifmr.ifm_count == 0)
207                 errx(1, "%s: no media types?", name);
208
209         /*
210          * We are primarily concerned with the top-level type.
211          * However, "current" may be only IFM_NONE, so we just look
212          * for the top-level type in the first "supported type"
213          * entry.
214          *
215          * (I'm assuming that all supported media types for a given
216          * interface will be the same top-level type..)
217          */
218         subtype = get_media_subtype(IFM_TYPE(first_type), val);
219
220         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
221         ifr.ifr_media = (ifmr.ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
222             IFM_TYPE(first_type) | subtype;
223
224         if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
225                 err(1, "SIOCSIFMEDIA");
226 }
227
228 void
229 setmediaopt(const char *val, int d __unused, int s,
230             const struct afswtch *afp __unused)
231 {
232
233         domediaopt(val, 0, s);
234 }
235
236 void
237 unsetmediaopt(const char *val, int d __unused, int s,
238               const struct afswtch *afp __unused)
239 {
240
241         domediaopt(val, 1, s);
242 }
243
244 static void
245 domediaopt(const char *val, int clear, int s)
246 {
247         struct ifmediareq ifmr;
248         int *mwords, options;
249
250         memset(&ifmr, 0, sizeof(ifmr));
251         strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
252
253         /*
254          * We must go through the motions of reading all
255          * supported media because we need to know both
256          * the current media type and the top-level type.
257          */
258
259         if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
260                 err(1, "SIOCGIFMEDIA");
261
262         if (ifmr.ifm_count == 0)
263                 errx(1, "%s: no media types?", name);
264
265         mwords = (int *)malloc(ifmr.ifm_count * sizeof(int));
266         if (mwords == NULL)
267                 err(1, "malloc");
268
269         ifmr.ifm_ulist = mwords;
270         if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
271                 err(1, "SIOCGIFMEDIA");
272
273         options = get_media_options(IFM_TYPE(mwords[0]), val);
274
275         free(mwords);
276
277         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
278         ifr.ifr_media = ifmr.ifm_current;
279         if (clear)
280                 ifr.ifr_media &= ~options;
281         else
282                 ifr.ifr_media |= options;
283
284         if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
285                 err(1, "SIOCSIFMEDIA");
286 }
287
288 /**********************************************************************
289  * A good chunk of this is duplicated from sys/net/ifmedia.c
290  **********************************************************************/
291
292 static struct ifmedia_description ifm_type_descriptions[] =
293     IFM_TYPE_DESCRIPTIONS;
294
295 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
296     IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
297
298 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
299     IFM_SUBTYPE_ETHERNET_ALIASES;
300
301 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
302     IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
303
304 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
305     IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
306
307 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
308     IFM_SUBTYPE_TOKENRING_ALIASES;
309
310 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
311     IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
312
313 static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
314     IFM_SUBTYPE_FDDI_DESCRIPTIONS;
315
316 static struct ifmedia_description ifm_subtype_fddi_aliases[] =
317     IFM_SUBTYPE_FDDI_ALIASES;
318
319 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
320     IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
321
322 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
323     IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
324
325 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
326     IFM_SUBTYPE_IEEE80211_ALIASES;
327
328 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
329     IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
330
331 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
332     IFM_SUBTYPE_SHARED_DESCRIPTIONS;
333
334 static struct ifmedia_description ifm_subtype_shared_aliases[] =
335     IFM_SUBTYPE_SHARED_ALIASES;
336
337 static struct ifmedia_description ifm_shared_option_descriptions[] =
338     IFM_SHARED_OPTION_DESCRIPTIONS;
339
340 struct ifmedia_type_to_subtype {
341         struct {
342                 struct ifmedia_description *desc;
343                 int alias;
344         } subtypes[5];
345         struct {
346                 struct ifmedia_description *desc;
347                 int alias;
348         } options[3];
349 };
350
351 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
352 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
353         {
354                 {
355                         { &ifm_subtype_shared_descriptions[0], 0 },
356                         { &ifm_subtype_shared_aliases[0], 1 },
357                         { &ifm_subtype_ethernet_descriptions[0], 0 },
358                         { &ifm_subtype_ethernet_aliases[0], 1 },
359                         { NULL, 0 },
360                 },
361                 {
362                         { &ifm_shared_option_descriptions[0], 0 },
363                         { &ifm_subtype_ethernet_option_descriptions[0], 0 },
364                         { NULL, 0 },
365                 },
366         },
367         {
368                 {
369                         { &ifm_subtype_shared_descriptions[0], 0 },
370                         { &ifm_subtype_shared_aliases[0], 1 },
371                         { &ifm_subtype_tokenring_descriptions[0], 0 },
372                         { &ifm_subtype_tokenring_aliases[0], 1 },
373                         { NULL, 0 },
374                 },
375                 {
376                         { &ifm_shared_option_descriptions[0], 0 },
377                         { &ifm_subtype_tokenring_option_descriptions[0], 0 },
378                         { NULL, 0 },
379                 },
380         },
381         {
382                 {
383                         { &ifm_subtype_shared_descriptions[0], 0 },
384                         { &ifm_subtype_shared_aliases[0], 1 },
385                         { &ifm_subtype_fddi_descriptions[0], 0 },
386                         { &ifm_subtype_fddi_aliases[0], 1 },
387                         { NULL, 0 },
388                 },
389                 {
390                         { &ifm_shared_option_descriptions[0], 0 },
391                         { &ifm_subtype_fddi_option_descriptions[0], 0 },
392                         { NULL, 0 },
393                 },
394         },
395         {
396                 {
397                         { &ifm_subtype_shared_descriptions[0], 0 },
398                         { &ifm_subtype_shared_aliases[0], 1 },
399                         { &ifm_subtype_ieee80211_descriptions[0], 0 },
400                         { &ifm_subtype_ieee80211_aliases[0], 1 },
401                         { NULL, 0 },
402                 },
403                 {
404                         { &ifm_shared_option_descriptions[0], 0 },
405                         { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
406                         { NULL, 0 },
407                 },
408         },
409 };
410
411 static int
412 get_media_subtype(int type, const char *val)
413 {
414         struct ifmedia_description *desc;
415         struct ifmedia_type_to_subtype *ttos;
416         int rval, i;
417
418         /* Find the top-level interface type. */
419         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
420             desc->ifmt_string != NULL; desc++, ttos++)
421                 if (type == desc->ifmt_word)
422                         break;
423         if (desc->ifmt_string == NULL)
424                 errx(1, "unknown media type 0x%x", type);
425
426         for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
427                 rval = lookup_media_word(ttos->subtypes[i].desc, val);
428                 if (rval != -1)
429                         return (rval);
430         }
431         errx(1, "unknown media subtype: %s", val);
432         /* NOTREACHED */
433 }
434
435 static int
436 get_media_options(int type, const char *val)
437 {
438         struct ifmedia_description *desc;
439         struct ifmedia_type_to_subtype *ttos;
440         char *optlist, *optptr;
441         int option = 0, i, rval = 0;
442
443         /* We muck with the string, so copy it. */
444         optlist = strdup(val);
445         if (optlist == NULL)
446                 err(1, "strdup");
447
448         /* Find the top-level interface type. */
449         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
450             desc->ifmt_string != NULL; desc++, ttos++)
451                 if (type == desc->ifmt_word)
452                         break;
453         if (desc->ifmt_string == NULL)
454                 errx(1, "unknown media type 0x%x", type);
455
456         /*
457          * Look up the options in the user-provided comma-separated
458          * list.
459          */
460         optptr = optlist;
461         for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) {
462                 for (i = 0; ttos->options[i].desc != NULL; i++) {
463                         option = lookup_media_word(ttos->options[i].desc, optptr);
464                         if (option != -1)
465                                 break;
466                 }
467                 if (option == 0)
468                         errx(1, "unknown option: %s", optptr);
469                 rval |= option;
470         }
471
472         free(optlist);
473         return (rval);
474 }
475
476 static int
477 lookup_media_word(struct ifmedia_description *desc, const char *val)
478 {
479
480         for (; desc->ifmt_string != NULL; desc++)
481                 if (strcasecmp(desc->ifmt_string, val) == 0)
482                         return (desc->ifmt_word);
483
484         return (-1);
485 }
486
487 static struct ifmedia_description *get_toptype_desc(int ifmw)
488 {
489         struct ifmedia_description *desc;
490
491         for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++)
492                 if (IFM_TYPE(ifmw) == desc->ifmt_word)
493                         break;
494
495         return desc;
496 }
497
498 static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw)
499 {
500         struct ifmedia_description *desc;
501         struct ifmedia_type_to_subtype *ttos;
502
503         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
504             desc->ifmt_string != NULL; desc++, ttos++)
505                 if (IFM_TYPE(ifmw) == desc->ifmt_word)
506                         break;
507
508         return ttos;
509 }
510
511 static struct ifmedia_description *get_subtype_desc(int ifmw,
512                                   struct ifmedia_type_to_subtype *ttos)
513 {
514         int i;
515         struct ifmedia_description *desc;
516
517         for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
518                 if (ttos->subtypes[i].alias)
519                         continue;
520                 for (desc = ttos->subtypes[i].desc;
521                     desc->ifmt_string != NULL; desc++) {
522                         if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
523                                 return desc;
524                 }
525         }
526
527         return NULL;
528 }
529
530 static void
531 print_media_word(int ifmw, int print_toptype)
532 {
533         struct ifmedia_description *desc;
534         struct ifmedia_type_to_subtype *ttos;
535         int seen_option = 0, i;
536
537         /* Find the top-level interface type. */
538         desc = get_toptype_desc(ifmw);
539         ttos = get_toptype_ttos(ifmw);
540         if (desc->ifmt_string == NULL) {
541                 printf("<unknown type>");
542                 return;
543         } else if (print_toptype) {
544                 printf("%s", desc->ifmt_string);
545         }
546
547         /*
548          * Don't print the top-level type; it's not like we can
549          * change it, or anything.
550          */
551
552         /* Find subtype. */
553         desc = get_subtype_desc(ifmw, ttos);
554         if (desc != NULL)
555                 goto got_subtype;
556
557         /* Falling to here means unknown subtype. */
558         printf("<unknown subtype>");
559         return;
560
561  got_subtype:
562         if (print_toptype)
563                 putchar(' ');
564
565         printf("%s", desc->ifmt_string);
566
567         /* Find options. */
568         for (i = 0; ttos->options[i].desc != NULL; i++) {
569                 if (ttos->options[i].alias)
570                         continue;
571                 for (desc = ttos->options[i].desc;
572                     desc->ifmt_string != NULL; desc++) {
573                         if (ifmw & desc->ifmt_word) {
574                                 if (seen_option == 0)
575                                         printf(" <");
576                                 printf("%s%s", seen_option++ ? "," : "",
577                                     desc->ifmt_string);
578                         }
579                 }
580         }
581         printf("%s", seen_option ? ">" : "");
582 }
583
584 static void
585 print_media_word_ifconfig(int ifmw)
586 {
587         struct ifmedia_description *desc;
588         struct ifmedia_type_to_subtype *ttos;
589         int i;
590
591         /* Find the top-level interface type. */
592         desc = get_toptype_desc(ifmw);
593         ttos = get_toptype_ttos(ifmw);
594         if (desc->ifmt_string == NULL) {
595                 printf("<unknown type>");
596                 return;
597         }
598
599         /*
600          * Don't print the top-level type; it's not like we can
601          * change it, or anything.
602          */
603
604         /* Find subtype. */
605         desc = get_subtype_desc(ifmw, ttos);
606         if (desc != NULL)
607                 goto got_subtype;
608
609         /* Falling to here means unknown subtype. */
610         printf("<unknown subtype>");
611         return;
612
613  got_subtype:
614         printf("media %s", desc->ifmt_string);
615
616         /* Find options. */
617         for (i = 0; ttos->options[i].desc != NULL; i++) {
618                 if (ttos->options[i].alias)
619                         continue;
620                 for (desc = ttos->options[i].desc;
621                     desc->ifmt_string != NULL; desc++) {
622                         if (ifmw & desc->ifmt_word) {
623                                 printf(" mediaopt %s", desc->ifmt_string);
624                         }
625                 }
626         }
627 }
628
629 /**********************************************************************
630  * ...until here.
631  **********************************************************************/