]> git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/ifmedia.c
network_cmds-245.tar.gz
[apple/network_cmds.git] / ifconfig.tproj / 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 1999/08/28 00:13:08 peter Exp $ */
3
4 /*
5 * Copyright (c) 1997 Jason R. Thorpe.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
19 * by Jason R. Thorpe.
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.
22 *
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
33 * SUCH DAMAGE.
34 */
35
36 /*
37 * Copyright (c) 1983, 1993
38 * The Regents of the University of California. All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
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.
55 *
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
66 * SUCH DAMAGE.
67 */
68
69 #include <sys/param.h>
70 #include <sys/ioctl.h>
71 #include <sys/socket.h>
72 #include <sys/sysctl.h>
73 #include <sys/time.h>
74
75 #include <net/if.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_media.h>
79 #include <net/route.h>
80
81 #include <ctype.h>
82 #include <err.h>
83 #include <errno.h>
84 #include <fcntl.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <string.h>
88 #include <unistd.h>
89
90 #include "ifconfig.h"
91
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));
97
98 extern int supmedia;
99
100 void
101 media_status(s, info)
102 int s;
103 struct rt_addrinfo *info;
104 {
105 struct ifmediareq ifmr;
106 int *media_list, i;
107
108 (void) memset(&ifmr, 0, sizeof(ifmr));
109 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
110
111 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
112 /*
113 * Interface doesn't support SIOC{G,S}IFMEDIA.
114 */
115 return;
116 }
117
118 if (ifmr.ifm_count == 0) {
119 //warnx("%s: no media types?", name);
120 return;
121 }
122
123 printf("\tmedia: ");
124 print_media_word(ifmr.ifm_current);
125 if (ifmr.ifm_active != ifmr.ifm_current) {
126 putchar(' ');
127 putchar('(');
128 print_media_word(ifmr.ifm_active);
129 putchar(')');
130 }
131
132 if (ifmr.ifm_status & IFM_AVALID) {
133 printf(" status: ");
134 #if 0
135 switch (IFM_TYPE(ifmr.ifm_active)) {
136 case IFM_ETHER:
137 if (ifmr.ifm_status & IFM_ACTIVE)
138 printf("active");
139 else
140 printf("no carrier");
141 break;
142
143 case IFM_FDDI:
144 case IFM_TOKEN:
145 if (ifmr.ifm_status & IFM_ACTIVE)
146 printf("inserted");
147 else
148 printf("no ring");
149 break;
150 }
151 #endif 0
152 if (ifmr.ifm_status & IFM_ACTIVE)
153 printf("active");
154 else
155 printf("inactive");
156 }
157 putchar('\n');
158
159 #if 0
160 if (supmedia == 0) {
161 return;
162 }
163 #endif 0
164 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
165 if (media_list == NULL)
166 err(1, "malloc");
167 ifmr.ifm_ulist = media_list;
168
169 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
170 err(1, "SIOCGIFMEDIA");
171
172 if (ifmr.ifm_count > 0) {
173 printf("\tsupported media:");
174 for (i = 0; i < ifmr.ifm_count; i++) {
175 putchar(' ');
176 print_media_word(media_list[i]);
177 }
178 putchar('\n');
179 }
180
181 free(media_list);
182 }
183
184 void
185 setmedia(val, d, s, afp)
186 const char *val;
187 int d;
188 int s;
189 const struct afswtch *afp;
190 {
191 struct ifmediareq ifmr;
192 int first_type, subtype;
193
194 (void) memset(&ifmr, 0, sizeof(ifmr));
195 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
196
197 ifmr.ifm_count = 1;
198 ifmr.ifm_ulist = &first_type;
199 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
200 /*
201 * If we get E2BIG, the kernel is telling us
202 * that there are more, so we can ignore it.
203 */
204 if (errno != E2BIG)
205 err(1, "SIOCGIFMEDIA");
206 }
207
208 if (ifmr.ifm_count == 0)
209 errx(1, "%s: no media types?", name);
210
211 /*
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"
215 * entry.
216 *
217 * (I'm assuming that all supported media types for a given
218 * interface will be the same top-level type..)
219 */
220 subtype = get_media_subtype(IFM_TYPE(first_type), val);
221
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;
225
226 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
227 err(1, "SIOCSIFMEDIA");
228 }
229
230 void
231 setmediaopt(val, d, s, afp)
232 const char *val;
233 int d;
234 int s;
235 const struct afswtch *afp;
236 {
237
238 domediaopt(val, 0, s);
239 }
240
241 void
242 unsetmediaopt(val, d, s, afp)
243 const char *val;
244 int d;
245 int s;
246 const struct afswtch *afp;
247 {
248
249 domediaopt(val, 1, s);
250 }
251
252 static void
253 domediaopt(val, clear, s)
254 const char *val;
255 int clear;
256 int s;
257 {
258 struct ifmediareq ifmr;
259 int *mwords, options;
260
261 (void) memset(&ifmr, 0, sizeof(ifmr));
262 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
263
264 /*
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.
268 */
269
270 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
271 err(1, "SIOCGIFMEDIA");
272
273 if (ifmr.ifm_count == 0)
274 errx(1, "%s: no media types?", name);
275
276 mwords = (int *)malloc(ifmr.ifm_count * sizeof(int));
277 if (mwords == NULL)
278 err(1, "malloc");
279
280 ifmr.ifm_ulist = mwords;
281 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
282 err(1, "SIOCGIFMEDIA");
283
284 options = get_media_options(IFM_TYPE(mwords[0]), val);
285
286 free(mwords);
287
288 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
289 ifr.ifr_media = ifmr.ifm_current;
290 if (clear)
291 ifr.ifr_media &= ~options;
292 else
293 ifr.ifr_media |= options;
294
295 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
296 err(1, "SIOCSIFMEDIA");
297 }
298
299 /**********************************************************************
300 * A good chunk of this is duplicated from sys/net/ifmedia.c
301 **********************************************************************/
302
303 static struct ifmedia_description ifm_type_descriptions[] =
304 IFM_TYPE_DESCRIPTIONS;
305
306 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
307 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
308
309 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
310 IFM_SUBTYPE_ETHERNET_ALIASES;
311
312 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
313 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
314
315 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
316 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
317
318 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
319 IFM_SUBTYPE_TOKENRING_ALIASES;
320
321 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
322 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
323
324 static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
325 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
326
327 static struct ifmedia_description ifm_subtype_fddi_aliases[] =
328 IFM_SUBTYPE_FDDI_ALIASES;
329
330 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
331 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
332
333 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
334 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
335
336 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
337 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
338
339 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
340 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
341
342 static struct ifmedia_description ifm_subtype_shared_aliases[] =
343 IFM_SUBTYPE_SHARED_ALIASES;
344
345 static struct ifmedia_description ifm_shared_option_descriptions[] =
346 IFM_SHARED_OPTION_DESCRIPTIONS;
347
348 struct ifmedia_type_to_subtype {
349 struct {
350 struct ifmedia_description *desc;
351 int alias;
352 } subtypes[5];
353 struct {
354 struct ifmedia_description *desc;
355 int alias;
356 } options[3];
357 };
358
359 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
360 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
361 {
362 {
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 },
367 { NULL, 0 },
368 },
369 {
370 { &ifm_shared_option_descriptions[0], 0 },
371 { &ifm_subtype_ethernet_option_descriptions[0], 1 },
372 { NULL, 0 },
373 },
374 },
375 {
376 {
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 },
381 { NULL, 0 },
382 },
383 {
384 { &ifm_shared_option_descriptions[0], 0 },
385 { &ifm_subtype_tokenring_option_descriptions[0], 1 },
386 { NULL, 0 },
387 },
388 },
389 {
390 {
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 },
395 { NULL, 0 },
396 },
397 {
398 { &ifm_shared_option_descriptions[0], 0 },
399 { &ifm_subtype_fddi_option_descriptions[0], 1 },
400 { NULL, 0 },
401 },
402 },
403 {
404 {
405 { &ifm_subtype_shared_descriptions[0], 0 },
406 { &ifm_subtype_shared_aliases[0], 1 },
407 { &ifm_subtype_ieee80211_descriptions[0], 0 },
408 { NULL, 0 },
409 },
410 {
411 { &ifm_shared_option_descriptions[0], 0 },
412 { &ifm_subtype_ieee80211_option_descriptions[0], 1 },
413 { NULL, 0 },
414 },
415 },
416 };
417
418 static int
419 get_media_subtype(type, val)
420 int type;
421 const char *val;
422 {
423 struct ifmedia_description *desc;
424 struct ifmedia_type_to_subtype *ttos;
425 int rval, i;
426
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)
431 break;
432 if (desc->ifmt_string == NULL)
433 errx(1, "unknown media type 0x%x", type);
434
435 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
436 rval = lookup_media_word(ttos->subtypes[i].desc, val);
437 if (rval != -1)
438 return (rval);
439 }
440 errx(1, "unknown media subtype: %s", val);
441 /* NOTREACHED */
442 }
443
444 static int
445 get_media_options(type, val)
446 int type;
447 const char *val;
448 {
449 struct ifmedia_description *desc;
450 struct ifmedia_type_to_subtype *ttos;
451 char *optlist, *optptr;
452 int option = 0, i, rval = 0;
453
454 /* We muck with the string, so copy it. */
455 optlist = strdup(val);
456 if (optlist == NULL)
457 err(1, "strdup");
458
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)
463 break;
464 if (desc->ifmt_string == NULL)
465 errx(1, "unknown media type 0x%x", type);
466
467 /*
468 * Look up the options in the user-provided comma-separated
469 * list.
470 */
471 optptr = optlist;
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);
475 if (option != -1)
476 break;
477 }
478 if (option == 0)
479 errx(1, "unknown option: %s", optptr);
480 rval |= option;
481 }
482
483 free(optlist);
484 return (rval);
485 }
486
487 static int
488 lookup_media_word(desc, val)
489 struct ifmedia_description *desc;
490 const char *val;
491 {
492
493 for (; desc->ifmt_string != NULL; desc++)
494 if (strcasecmp(desc->ifmt_string, val) == 0)
495 return (desc->ifmt_word);
496
497 return (-1);
498 }
499
500 static void
501 print_media_word(ifmw)
502 int ifmw;
503 {
504 struct ifmedia_description *desc;
505 struct ifmedia_type_to_subtype *ttos;
506 int seen_option = 0, i;
507
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)
512 break;
513 if (desc->ifmt_string == NULL) {
514 printf("<unknown type>");
515 return;
516 }
517
518 /*
519 * Don't print the top-level type; it's not like we can
520 * change it, or anything.
521 */
522
523 /* Find subtype. */
524 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
525 if (ttos->subtypes[i].alias)
526 continue;
527 for (desc = ttos->subtypes[i].desc;
528 desc->ifmt_string != NULL; desc++) {
529 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
530 goto got_subtype;
531 }
532 }
533
534 /* Falling to here means unknown subtype. */
535 printf("<unknown subtype>");
536 return;
537
538 got_subtype:
539 printf("%s", desc->ifmt_string);
540
541 /* Find options. */
542 for (i = 0; ttos->options[i].desc != NULL; i++) {
543 if (ttos->options[i].alias)
544 continue;
545 for (desc = ttos->options[i].desc;
546 desc->ifmt_string != NULL; desc++) {
547 if (ifmw & desc->ifmt_word) {
548 if (seen_option == 0)
549 printf(" <");
550 printf("%s%s", seen_option++ ? "," : "",
551 desc->ifmt_string);
552 }
553 }
554 }
555 printf("%s", seen_option ? ">" : "");
556 }
557
558 /**********************************************************************
559 * ...until here.
560 **********************************************************************/