Commit | Line | Data |
---|---|---|
b7080c8e | 1 | /* $NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $ */ |
8d01c344 | 2 | /* $FreeBSD: src/sbin/ifconfig/ifmedia.c,v 1.25.6.1 2008/11/25 02:59:29 kensmith Exp $ */ |
b7080c8e A |
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 | ||
8d01c344 A |
92 | static void domediaopt(const char *, int, int); |
93 | static int get_media_subtype(int, const char *); | |
94 | #ifdef notdef | |
95 | static int get_media_mode(int, const char *); | |
96 | #endif | |
97 | static int get_media_options(int, const char *); | |
98 | static int lookup_media_word(struct ifmedia_description *, const char *); | |
99 | static void print_media_word(int, int); | |
100 | static void print_media_word_ifconfig(int); | |
101 | ||
102 | static struct ifmedia_description *get_toptype_desc(int); | |
103 | static struct ifmedia_type_to_subtype *get_toptype_ttos(int); | |
104 | static struct ifmedia_description *get_subtype_desc(int, | |
105 | struct ifmedia_type_to_subtype *ttos); | |
b7080c8e | 106 | |
8d01c344 A |
107 | static void |
108 | media_status(int s) | |
b7080c8e A |
109 | { |
110 | struct ifmediareq ifmr; | |
111 | int *media_list, i; | |
112 | ||
113 | (void) memset(&ifmr, 0, sizeof(ifmr)); | |
114 | (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); | |
115 | ||
116 | if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { | |
117 | /* | |
118 | * Interface doesn't support SIOC{G,S}IFMEDIA. | |
119 | */ | |
120 | return; | |
121 | } | |
122 | ||
123 | if (ifmr.ifm_count == 0) { | |
8d01c344 A |
124 | warnx("%s: no media types?", name); |
125 | return; | |
b7080c8e A |
126 | } |
127 | ||
8d01c344 A |
128 | media_list = (int *)malloc(ifmr.ifm_count * sizeof(int)); |
129 | if (media_list == NULL) | |
130 | err(1, "malloc"); | |
131 | ifmr.ifm_ulist = media_list; | |
132 | ||
133 | if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) | |
134 | err(1, "SIOCGIFMEDIA"); | |
135 | ||
b7080c8e | 136 | printf("\tmedia: "); |
8d01c344 | 137 | print_media_word(ifmr.ifm_current, 1); |
b7080c8e A |
138 | if (ifmr.ifm_active != ifmr.ifm_current) { |
139 | putchar(' '); | |
140 | putchar('('); | |
8d01c344 | 141 | print_media_word(ifmr.ifm_active, 0); |
b7080c8e A |
142 | putchar(')'); |
143 | } | |
144 | ||
8d01c344 A |
145 | putchar('\n'); |
146 | ||
b7080c8e | 147 | if (ifmr.ifm_status & IFM_AVALID) { |
8d01c344 A |
148 | printf("\tstatus: "); |
149 | #ifdef notdef | |
b7080c8e A |
150 | switch (IFM_TYPE(ifmr.ifm_active)) { |
151 | case IFM_ETHER: | |
8d01c344 | 152 | case IFM_ATM: |
b7080c8e A |
153 | if (ifmr.ifm_status & IFM_ACTIVE) |
154 | printf("active"); | |
155 | else | |
156 | printf("no carrier"); | |
157 | break; | |
158 | ||
159 | case IFM_FDDI: | |
160 | case IFM_TOKEN: | |
161 | if (ifmr.ifm_status & IFM_ACTIVE) | |
162 | printf("inserted"); | |
163 | else | |
164 | printf("no ring"); | |
165 | break; | |
8d01c344 A |
166 | |
167 | case IFM_IEEE80211: | |
168 | /* XXX: Different value for adhoc? */ | |
169 | if (ifmr.ifm_status & IFM_ACTIVE) | |
170 | printf("associated"); | |
171 | else | |
172 | printf("no carrier"); | |
173 | break; | |
b7080c8e | 174 | } |
8d01c344 | 175 | #else |
b7080c8e A |
176 | if (ifmr.ifm_status & IFM_ACTIVE) |
177 | printf("active"); | |
178 | else | |
8d01c344 A |
179 | printf("inactive"); |
180 | #endif | |
181 | putchar('\n'); | |
2b484d24 | 182 | } |
2b484d24 | 183 | |
8d01c344 A |
184 | if (ifmr.ifm_count > 0 && supmedia) { |
185 | printf("\tsupported media:\n"); | |
b7080c8e | 186 | for (i = 0; i < ifmr.ifm_count; i++) { |
8d01c344 A |
187 | printf("\t\t"); |
188 | print_media_word_ifconfig(media_list[i]); | |
189 | putchar('\n'); | |
b7080c8e | 190 | } |
b7080c8e A |
191 | } |
192 | ||
193 | free(media_list); | |
194 | } | |
195 | ||
8d01c344 A |
196 | struct ifmediareq * |
197 | ifmedia_getstate(int s) | |
b7080c8e | 198 | { |
8d01c344 A |
199 | static struct ifmediareq *ifmr = NULL; |
200 | int *mwords; | |
b7080c8e | 201 | |
8d01c344 A |
202 | if (ifmr == NULL) { |
203 | ifmr = (struct ifmediareq *)malloc(sizeof(struct ifmediareq)); | |
204 | if (ifmr == NULL) | |
205 | err(1, "malloc"); | |
206 | ||
207 | (void) memset(ifmr, 0, sizeof(struct ifmediareq)); | |
208 | (void) strncpy(ifmr->ifm_name, name, | |
209 | sizeof(ifmr->ifm_name)); | |
210 | ||
211 | ifmr->ifm_count = 0; | |
212 | ifmr->ifm_ulist = NULL; | |
b7080c8e | 213 | |
b7080c8e | 214 | /* |
8d01c344 A |
215 | * We must go through the motions of reading all |
216 | * supported media because we need to know both | |
217 | * the current media type and the top-level type. | |
b7080c8e | 218 | */ |
8d01c344 A |
219 | |
220 | if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) { | |
b7080c8e | 221 | err(1, "SIOCGIFMEDIA"); |
8d01c344 A |
222 | } |
223 | ||
224 | if (ifmr->ifm_count == 0) | |
225 | errx(1, "%s: no media types?", name); | |
226 | ||
227 | mwords = (int *)malloc(ifmr->ifm_count * sizeof(int)); | |
228 | if (mwords == NULL) | |
229 | err(1, "malloc"); | |
230 | ||
231 | ifmr->ifm_ulist = mwords; | |
232 | if (ioctl(s, SIOCGIFMEDIA, (caddr_t)ifmr) < 0) | |
233 | err(1, "SIOCGIFMEDIA"); | |
234 | } | |
235 | ||
236 | return ifmr; | |
237 | } | |
238 | ||
239 | static void | |
240 | setifmediacallback(int s, void *arg) | |
241 | { | |
242 | struct ifmediareq *ifmr = (struct ifmediareq *)arg; | |
243 | static int did_it = 0; | |
244 | ||
245 | if (!did_it) { | |
246 | ifr.ifr_media = ifmr->ifm_current; | |
247 | if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0) | |
248 | err(1, "SIOCSIFMEDIA (media)"); | |
249 | free(ifmr->ifm_ulist); | |
250 | free(ifmr); | |
251 | did_it = 1; | |
b7080c8e | 252 | } |
8d01c344 | 253 | } |
b7080c8e | 254 | |
8d01c344 A |
255 | static void |
256 | setmedia(const char *val, int d, int s, const struct afswtch *afp) | |
257 | { | |
258 | struct ifmediareq *ifmr; | |
259 | int subtype; | |
260 | ||
261 | ifmr = ifmedia_getstate(s); | |
b7080c8e A |
262 | |
263 | /* | |
264 | * We are primarily concerned with the top-level type. | |
265 | * However, "current" may be only IFM_NONE, so we just look | |
266 | * for the top-level type in the first "supported type" | |
267 | * entry. | |
268 | * | |
269 | * (I'm assuming that all supported media types for a given | |
270 | * interface will be the same top-level type..) | |
271 | */ | |
8d01c344 | 272 | subtype = get_media_subtype(IFM_TYPE(ifmr->ifm_ulist[0]), val); |
b7080c8e A |
273 | |
274 | strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); | |
8d01c344 A |
275 | ifr.ifr_media = (ifmr->ifm_current & ~(IFM_NMASK|IFM_TMASK)) | |
276 | IFM_TYPE(ifmr->ifm_ulist[0]) | subtype; | |
b7080c8e | 277 | |
8d01c344 A |
278 | if ((ifr.ifr_media & IFM_TMASK) == 0) { |
279 | ifr.ifr_media &= ~IFM_GMASK; | |
280 | } | |
281 | ||
282 | ifmr->ifm_current = ifr.ifr_media; | |
283 | callback_register(setifmediacallback, (void *)ifmr); | |
b7080c8e A |
284 | } |
285 | ||
8d01c344 A |
286 | static void |
287 | setmediaopt(const char *val, int d, int s, const struct afswtch *afp) | |
b7080c8e A |
288 | { |
289 | ||
290 | domediaopt(val, 0, s); | |
291 | } | |
292 | ||
8d01c344 A |
293 | static void |
294 | unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp) | |
b7080c8e A |
295 | { |
296 | ||
297 | domediaopt(val, 1, s); | |
298 | } | |
299 | ||
300 | static void | |
8d01c344 | 301 | domediaopt(const char *val, int clear, int s) |
b7080c8e | 302 | { |
8d01c344 A |
303 | struct ifmediareq *ifmr; |
304 | int options; | |
b7080c8e | 305 | |
8d01c344 | 306 | ifmr = ifmedia_getstate(s); |
b7080c8e | 307 | |
8d01c344 | 308 | options = get_media_options(IFM_TYPE(ifmr->ifm_ulist[0]), val); |
b7080c8e | 309 | |
8d01c344 A |
310 | strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
311 | ifr.ifr_media = ifmr->ifm_current; | |
312 | if (clear) | |
313 | ifr.ifr_media &= ~options; | |
314 | else { | |
315 | if (options & IFM_HDX) { | |
316 | ifr.ifr_media &= ~IFM_FDX; | |
317 | options &= ~IFM_HDX; | |
318 | } | |
319 | ifr.ifr_media |= options; | |
320 | } | |
321 | ifmr->ifm_current = ifr.ifr_media; | |
322 | callback_register(setifmediacallback, (void *)ifmr); | |
323 | } | |
b7080c8e | 324 | |
8d01c344 A |
325 | static void |
326 | setmediainst(const char *val, int d, int s, const struct afswtch *afp) | |
327 | { | |
328 | struct ifmediareq *ifmr; | |
329 | int inst; | |
b7080c8e | 330 | |
8d01c344 | 331 | ifmr = ifmedia_getstate(s); |
b7080c8e | 332 | |
8d01c344 A |
333 | inst = atoi(val); |
334 | if (inst < 0 || inst > IFM_INST_MAX) | |
335 | errx(1, "invalid media instance: %s", val); | |
b7080c8e | 336 | |
8d01c344 A |
337 | strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
338 | ifr.ifr_media = (ifmr->ifm_current & ~IFM_IMASK) | inst << IFM_ISHIFT; | |
339 | ||
340 | ifmr->ifm_current = ifr.ifr_media; | |
341 | callback_register(setifmediacallback, (void *)ifmr); | |
342 | } | |
343 | ||
344 | #ifdef notdef | |
345 | static void | |
346 | setmediamode(const char *val, int d, int s, const struct afswtch *afp) | |
347 | { | |
348 | struct ifmediareq *ifmr; | |
349 | int mode; | |
b7080c8e | 350 | |
8d01c344 A |
351 | ifmr = ifmedia_getstate(s); |
352 | ||
353 | mode = get_media_mode(IFM_TYPE(ifmr->ifm_ulist[0]), val); | |
b7080c8e A |
354 | |
355 | strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); | |
8d01c344 | 356 | ifr.ifr_media = (ifmr->ifm_current & ~IFM_MMASK) | mode; |
b7080c8e | 357 | |
8d01c344 A |
358 | ifmr->ifm_current = ifr.ifr_media; |
359 | callback_register(setifmediacallback, (void *)ifmr); | |
b7080c8e | 360 | } |
8d01c344 | 361 | #endif |
b7080c8e A |
362 | |
363 | /********************************************************************** | |
364 | * A good chunk of this is duplicated from sys/net/ifmedia.c | |
365 | **********************************************************************/ | |
366 | ||
367 | static struct ifmedia_description ifm_type_descriptions[] = | |
368 | IFM_TYPE_DESCRIPTIONS; | |
369 | ||
370 | static struct ifmedia_description ifm_subtype_ethernet_descriptions[] = | |
371 | IFM_SUBTYPE_ETHERNET_DESCRIPTIONS; | |
372 | ||
373 | static struct ifmedia_description ifm_subtype_ethernet_aliases[] = | |
374 | IFM_SUBTYPE_ETHERNET_ALIASES; | |
375 | ||
376 | static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] = | |
377 | IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS; | |
378 | ||
379 | static struct ifmedia_description ifm_subtype_tokenring_descriptions[] = | |
380 | IFM_SUBTYPE_TOKENRING_DESCRIPTIONS; | |
381 | ||
382 | static struct ifmedia_description ifm_subtype_tokenring_aliases[] = | |
383 | IFM_SUBTYPE_TOKENRING_ALIASES; | |
384 | ||
385 | static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] = | |
386 | IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS; | |
387 | ||
388 | static struct ifmedia_description ifm_subtype_fddi_descriptions[] = | |
389 | IFM_SUBTYPE_FDDI_DESCRIPTIONS; | |
390 | ||
391 | static struct ifmedia_description ifm_subtype_fddi_aliases[] = | |
392 | IFM_SUBTYPE_FDDI_ALIASES; | |
393 | ||
394 | static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] = | |
395 | IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS; | |
396 | ||
8d01c344 | 397 | #ifdef notdef |
b7080c8e A |
398 | static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] = |
399 | IFM_SUBTYPE_IEEE80211_DESCRIPTIONS; | |
400 | ||
8d01c344 A |
401 | static struct ifmedia_description ifm_subtype_ieee80211_aliases[] = |
402 | IFM_SUBTYPE_IEEE80211_ALIASES; | |
403 | ||
b7080c8e A |
404 | static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] = |
405 | IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS; | |
406 | ||
8d01c344 A |
407 | struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] = |
408 | IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS; | |
409 | ||
410 | struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] = | |
411 | IFM_SUBTYPE_IEEE80211_MODE_ALIASES; | |
412 | ||
413 | static struct ifmedia_description ifm_subtype_atm_descriptions[] = | |
414 | IFM_SUBTYPE_ATM_DESCRIPTIONS; | |
415 | ||
416 | static struct ifmedia_description ifm_subtype_atm_aliases[] = | |
417 | IFM_SUBTYPE_ATM_ALIASES; | |
418 | ||
419 | static struct ifmedia_description ifm_subtype_atm_option_descriptions[] = | |
420 | IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS; | |
421 | #endif | |
422 | ||
b7080c8e A |
423 | static struct ifmedia_description ifm_subtype_shared_descriptions[] = |
424 | IFM_SUBTYPE_SHARED_DESCRIPTIONS; | |
425 | ||
426 | static struct ifmedia_description ifm_subtype_shared_aliases[] = | |
427 | IFM_SUBTYPE_SHARED_ALIASES; | |
428 | ||
429 | static struct ifmedia_description ifm_shared_option_descriptions[] = | |
430 | IFM_SHARED_OPTION_DESCRIPTIONS; | |
431 | ||
432 | struct ifmedia_type_to_subtype { | |
433 | struct { | |
434 | struct ifmedia_description *desc; | |
435 | int alias; | |
436 | } subtypes[5]; | |
437 | struct { | |
438 | struct ifmedia_description *desc; | |
439 | int alias; | |
440 | } options[3]; | |
8d01c344 A |
441 | struct { |
442 | struct ifmedia_description *desc; | |
443 | int alias; | |
444 | } modes[3]; | |
b7080c8e A |
445 | }; |
446 | ||
447 | /* must be in the same order as IFM_TYPE_DESCRIPTIONS */ | |
448 | static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = { | |
449 | { | |
450 | { | |
451 | { &ifm_subtype_shared_descriptions[0], 0 }, | |
452 | { &ifm_subtype_shared_aliases[0], 1 }, | |
453 | { &ifm_subtype_ethernet_descriptions[0], 0 }, | |
454 | { &ifm_subtype_ethernet_aliases[0], 1 }, | |
455 | { NULL, 0 }, | |
456 | }, | |
457 | { | |
458 | { &ifm_shared_option_descriptions[0], 0 }, | |
8d01c344 A |
459 | { &ifm_subtype_ethernet_option_descriptions[0], 0 }, |
460 | { NULL, 0 }, | |
461 | }, | |
462 | { | |
b7080c8e A |
463 | { NULL, 0 }, |
464 | }, | |
465 | }, | |
466 | { | |
467 | { | |
468 | { &ifm_subtype_shared_descriptions[0], 0 }, | |
469 | { &ifm_subtype_shared_aliases[0], 1 }, | |
470 | { &ifm_subtype_tokenring_descriptions[0], 0 }, | |
471 | { &ifm_subtype_tokenring_aliases[0], 1 }, | |
472 | { NULL, 0 }, | |
473 | }, | |
474 | { | |
475 | { &ifm_shared_option_descriptions[0], 0 }, | |
8d01c344 A |
476 | { &ifm_subtype_tokenring_option_descriptions[0], 0 }, |
477 | { NULL, 0 }, | |
478 | }, | |
479 | { | |
b7080c8e A |
480 | { NULL, 0 }, |
481 | }, | |
482 | }, | |
483 | { | |
484 | { | |
485 | { &ifm_subtype_shared_descriptions[0], 0 }, | |
486 | { &ifm_subtype_shared_aliases[0], 1 }, | |
487 | { &ifm_subtype_fddi_descriptions[0], 0 }, | |
488 | { &ifm_subtype_fddi_aliases[0], 1 }, | |
489 | { NULL, 0 }, | |
490 | }, | |
491 | { | |
492 | { &ifm_shared_option_descriptions[0], 0 }, | |
8d01c344 A |
493 | { &ifm_subtype_fddi_option_descriptions[0], 0 }, |
494 | { NULL, 0 }, | |
495 | }, | |
496 | { | |
b7080c8e A |
497 | { NULL, 0 }, |
498 | }, | |
499 | }, | |
8d01c344 | 500 | #ifdef notdef |
b7080c8e A |
501 | { |
502 | { | |
503 | { &ifm_subtype_shared_descriptions[0], 0 }, | |
504 | { &ifm_subtype_shared_aliases[0], 1 }, | |
505 | { &ifm_subtype_ieee80211_descriptions[0], 0 }, | |
8d01c344 | 506 | { &ifm_subtype_ieee80211_aliases[0], 1 }, |
b7080c8e A |
507 | { NULL, 0 }, |
508 | }, | |
509 | { | |
510 | { &ifm_shared_option_descriptions[0], 0 }, | |
8d01c344 A |
511 | { &ifm_subtype_ieee80211_option_descriptions[0], 0 }, |
512 | { NULL, 0 }, | |
513 | }, | |
514 | { | |
515 | { &ifm_subtype_ieee80211_mode_descriptions[0], 0 }, | |
516 | { &ifm_subtype_ieee80211_mode_aliases[0], 0 }, | |
b7080c8e A |
517 | { NULL, 0 }, |
518 | }, | |
519 | }, | |
8d01c344 A |
520 | { |
521 | { | |
522 | { &ifm_subtype_shared_descriptions[0], 0 }, | |
523 | { &ifm_subtype_shared_aliases[0], 1 }, | |
524 | { &ifm_subtype_atm_descriptions[0], 0 }, | |
525 | { &ifm_subtype_atm_aliases[0], 1 }, | |
526 | { NULL, 0 }, | |
527 | }, | |
528 | { | |
529 | { &ifm_shared_option_descriptions[0], 0 }, | |
530 | { &ifm_subtype_atm_option_descriptions[0], 0 }, | |
531 | { NULL, 0 }, | |
532 | }, | |
533 | { | |
534 | { NULL, 0 }, | |
535 | }, | |
536 | }, | |
537 | #endif | |
b7080c8e A |
538 | }; |
539 | ||
540 | static int | |
8d01c344 | 541 | get_media_subtype(int type, const char *val) |
b7080c8e A |
542 | { |
543 | struct ifmedia_description *desc; | |
544 | struct ifmedia_type_to_subtype *ttos; | |
545 | int rval, i; | |
546 | ||
547 | /* Find the top-level interface type. */ | |
548 | for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; | |
549 | desc->ifmt_string != NULL; desc++, ttos++) | |
550 | if (type == desc->ifmt_word) | |
551 | break; | |
552 | if (desc->ifmt_string == NULL) | |
553 | errx(1, "unknown media type 0x%x", type); | |
554 | ||
555 | for (i = 0; ttos->subtypes[i].desc != NULL; i++) { | |
556 | rval = lookup_media_word(ttos->subtypes[i].desc, val); | |
557 | if (rval != -1) | |
558 | return (rval); | |
559 | } | |
560 | errx(1, "unknown media subtype: %s", val); | |
8d01c344 A |
561 | /*NOTREACHED*/ |
562 | } | |
563 | ||
564 | #ifdef notdef | |
565 | static int | |
566 | get_media_mode(int type, const char *val) | |
567 | { | |
568 | struct ifmedia_description *desc; | |
569 | struct ifmedia_type_to_subtype *ttos; | |
570 | int rval, i; | |
571 | ||
572 | /* Find the top-level interface type. */ | |
573 | for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; | |
574 | desc->ifmt_string != NULL; desc++, ttos++) | |
575 | if (type == desc->ifmt_word) | |
576 | break; | |
577 | if (desc->ifmt_string == NULL) | |
578 | errx(1, "unknown media mode 0x%x", type); | |
579 | ||
580 | for (i = 0; ttos->modes[i].desc != NULL; i++) { | |
581 | rval = lookup_media_word(ttos->modes[i].desc, val); | |
582 | if (rval != -1) | |
583 | return (rval); | |
584 | } | |
585 | return -1; | |
b7080c8e | 586 | } |
8d01c344 | 587 | #endif |
b7080c8e A |
588 | |
589 | static int | |
8d01c344 | 590 | get_media_options(int type, const char *val) |
b7080c8e A |
591 | { |
592 | struct ifmedia_description *desc; | |
593 | struct ifmedia_type_to_subtype *ttos; | |
594 | char *optlist, *optptr; | |
595 | int option = 0, i, rval = 0; | |
596 | ||
597 | /* We muck with the string, so copy it. */ | |
598 | optlist = strdup(val); | |
599 | if (optlist == NULL) | |
600 | err(1, "strdup"); | |
601 | ||
602 | /* Find the top-level interface type. */ | |
603 | for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; | |
604 | desc->ifmt_string != NULL; desc++, ttos++) | |
605 | if (type == desc->ifmt_word) | |
606 | break; | |
607 | if (desc->ifmt_string == NULL) | |
608 | errx(1, "unknown media type 0x%x", type); | |
609 | ||
610 | /* | |
611 | * Look up the options in the user-provided comma-separated | |
612 | * list. | |
613 | */ | |
614 | optptr = optlist; | |
615 | for (; (optptr = strtok(optptr, ",")) != NULL; optptr = NULL) { | |
616 | for (i = 0; ttos->options[i].desc != NULL; i++) { | |
617 | option = lookup_media_word(ttos->options[i].desc, optptr); | |
618 | if (option != -1) | |
619 | break; | |
620 | } | |
621 | if (option == 0) | |
622 | errx(1, "unknown option: %s", optptr); | |
623 | rval |= option; | |
624 | } | |
625 | ||
626 | free(optlist); | |
627 | return (rval); | |
628 | } | |
629 | ||
630 | static int | |
8d01c344 | 631 | lookup_media_word(struct ifmedia_description *desc, const char *val) |
b7080c8e A |
632 | { |
633 | ||
634 | for (; desc->ifmt_string != NULL; desc++) | |
635 | if (strcasecmp(desc->ifmt_string, val) == 0) | |
636 | return (desc->ifmt_word); | |
637 | ||
638 | return (-1); | |
639 | } | |
640 | ||
8d01c344 A |
641 | static struct ifmedia_description *get_toptype_desc(int ifmw) |
642 | { | |
643 | struct ifmedia_description *desc; | |
644 | ||
645 | for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++) | |
646 | if (IFM_TYPE(ifmw) == desc->ifmt_word) | |
647 | break; | |
648 | ||
649 | return desc; | |
650 | } | |
651 | ||
652 | static struct ifmedia_type_to_subtype *get_toptype_ttos(int ifmw) | |
b7080c8e A |
653 | { |
654 | struct ifmedia_description *desc; | |
655 | struct ifmedia_type_to_subtype *ttos; | |
b7080c8e | 656 | |
b7080c8e A |
657 | for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; |
658 | desc->ifmt_string != NULL; desc++, ttos++) | |
659 | if (IFM_TYPE(ifmw) == desc->ifmt_word) | |
660 | break; | |
8d01c344 A |
661 | |
662 | return ttos; | |
663 | } | |
664 | ||
665 | static struct ifmedia_description *get_subtype_desc(int ifmw, | |
666 | struct ifmedia_type_to_subtype *ttos) | |
667 | { | |
668 | int i; | |
669 | struct ifmedia_description *desc; | |
670 | ||
671 | for (i = 0; ttos->subtypes[i].desc != NULL; i++) { | |
672 | if (ttos->subtypes[i].alias) | |
673 | continue; | |
674 | for (desc = ttos->subtypes[i].desc; | |
675 | desc->ifmt_string != NULL; desc++) { | |
676 | if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) | |
677 | return desc; | |
678 | } | |
679 | } | |
680 | ||
681 | return NULL; | |
682 | } | |
683 | ||
684 | #ifdef notdef | |
685 | static struct ifmedia_description *get_mode_desc(int ifmw, | |
686 | struct ifmedia_type_to_subtype *ttos) | |
687 | { | |
688 | int i; | |
689 | struct ifmedia_description *desc; | |
690 | ||
691 | for (i = 0; ttos->modes[i].desc != NULL; i++) { | |
692 | if (ttos->modes[i].alias) | |
693 | continue; | |
694 | for (desc = ttos->modes[i].desc; | |
695 | desc->ifmt_string != NULL; desc++) { | |
696 | if (IFM_MODE(ifmw) == desc->ifmt_word) | |
697 | return desc; | |
698 | } | |
699 | } | |
700 | ||
701 | return NULL; | |
702 | } | |
703 | #endif | |
704 | ||
705 | static void | |
706 | print_media_word(int ifmw, int print_toptype) | |
707 | { | |
708 | struct ifmedia_description *desc; | |
709 | struct ifmedia_type_to_subtype *ttos; | |
710 | int seen_option = 0, i; | |
711 | ||
712 | /* Find the top-level interface type. */ | |
713 | desc = get_toptype_desc(ifmw); | |
714 | ttos = get_toptype_ttos(ifmw); | |
b7080c8e A |
715 | if (desc->ifmt_string == NULL) { |
716 | printf("<unknown type>"); | |
717 | return; | |
8d01c344 A |
718 | #ifdef notdef |
719 | } else if (print_toptype) { | |
720 | printf("%s", desc->ifmt_string); | |
721 | #endif | |
b7080c8e A |
722 | } |
723 | ||
724 | /* | |
725 | * Don't print the top-level type; it's not like we can | |
726 | * change it, or anything. | |
727 | */ | |
728 | ||
729 | /* Find subtype. */ | |
8d01c344 A |
730 | desc = get_subtype_desc(ifmw, ttos); |
731 | if (desc == NULL) { | |
732 | printf("<unknown subtype>"); | |
733 | return; | |
b7080c8e A |
734 | } |
735 | ||
8d01c344 A |
736 | #ifdef notdef |
737 | if (print_toptype) | |
738 | putchar(' '); | |
739 | #endif | |
740 | ||
b7080c8e A |
741 | printf("%s", desc->ifmt_string); |
742 | ||
8d01c344 A |
743 | #ifdef notdef |
744 | if (print_toptype) { | |
745 | desc = get_mode_desc(ifmw, ttos); | |
746 | if (desc != NULL && strcasecmp("autoselect", desc->ifmt_string)) | |
747 | printf(" mode %s", desc->ifmt_string); | |
748 | } | |
749 | #endif | |
b7080c8e A |
750 | /* Find options. */ |
751 | for (i = 0; ttos->options[i].desc != NULL; i++) { | |
752 | if (ttos->options[i].alias) | |
753 | continue; | |
754 | for (desc = ttos->options[i].desc; | |
755 | desc->ifmt_string != NULL; desc++) { | |
756 | if (ifmw & desc->ifmt_word) { | |
757 | if (seen_option == 0) | |
758 | printf(" <"); | |
759 | printf("%s%s", seen_option++ ? "," : "", | |
760 | desc->ifmt_string); | |
761 | } | |
762 | } | |
763 | } | |
764 | printf("%s", seen_option ? ">" : ""); | |
8d01c344 A |
765 | |
766 | #ifdef notdef | |
767 | if (print_toptype && IFM_INST(ifmw) != 0) | |
768 | printf(" instance %d", IFM_INST(ifmw)); | |
769 | #endif | |
770 | } | |
771 | ||
772 | static void | |
773 | print_media_word_ifconfig(int ifmw) | |
774 | { | |
775 | struct ifmedia_description *desc; | |
776 | struct ifmedia_type_to_subtype *ttos; | |
777 | int i; | |
778 | ||
779 | /* Find the top-level interface type. */ | |
780 | desc = get_toptype_desc(ifmw); | |
781 | ttos = get_toptype_ttos(ifmw); | |
782 | if (desc->ifmt_string == NULL) { | |
783 | printf("<unknown type>"); | |
784 | return; | |
785 | } | |
786 | ||
787 | /* | |
788 | * Don't print the top-level type; it's not like we can | |
789 | * change it, or anything. | |
790 | */ | |
791 | ||
792 | /* Find subtype. */ | |
793 | desc = get_subtype_desc(ifmw, ttos); | |
794 | if (desc == NULL) { | |
795 | printf("<unknown subtype>"); | |
796 | return; | |
797 | } | |
798 | ||
799 | printf("media %s", desc->ifmt_string); | |
800 | ||
801 | #ifdef notdef | |
802 | desc = get_mode_desc(ifmw, ttos); | |
803 | if (desc != NULL) | |
804 | printf(" mode %s", desc->ifmt_string); | |
805 | #endif | |
806 | ||
807 | /* Find options. */ | |
808 | for (i = 0; ttos->options[i].desc != NULL; i++) { | |
809 | if (ttos->options[i].alias) | |
810 | continue; | |
811 | for (desc = ttos->options[i].desc; | |
812 | desc->ifmt_string != NULL; desc++) { | |
813 | if (ifmw & desc->ifmt_word) { | |
814 | printf(" mediaopt %s", desc->ifmt_string); | |
815 | } | |
816 | } | |
817 | } | |
818 | ||
819 | if (IFM_INST(ifmw) != 0) | |
820 | printf(" instance %d", IFM_INST(ifmw)); | |
b7080c8e A |
821 | } |
822 | ||
823 | /********************************************************************** | |
824 | * ...until here. | |
825 | **********************************************************************/ | |
8d01c344 A |
826 | |
827 | static struct cmd media_cmds[] = { | |
828 | DEF_CMD_ARG("media", setmedia), | |
829 | #ifdef notdef | |
830 | DEF_CMD_ARG("mode", setmediamode), | |
831 | #endif | |
832 | DEF_CMD_ARG("mediaopt", setmediaopt), | |
833 | DEF_CMD_ARG("-mediaopt",unsetmediaopt), | |
834 | DEF_CMD_ARG("inst", setmediainst), | |
835 | DEF_CMD_ARG("instance", setmediainst), | |
836 | }; | |
837 | static struct afswtch af_media = { | |
838 | .af_name = "af_media", | |
839 | .af_af = AF_UNSPEC, | |
840 | .af_other_status = media_status, | |
841 | }; | |
842 | ||
843 | static __constructor void | |
844 | ifmedia_ctor(void) | |
845 | { | |
846 | #define N(a) (sizeof(a) / sizeof(a[0])) | |
847 | int i; | |
848 | ||
849 | for (i = 0; i < N(media_cmds); i++) | |
850 | cmd_register(&media_cmds[i]); | |
851 | af_register(&af_media); | |
852 | #undef N | |
853 | } |