2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
38 #include <net/if_dl.h>
39 #include <net/if_types.h>
40 #include <net/if_media.h>
41 #include <net/route.h>
53 #include "libifconfig.h"
54 #include "libifconfig_internal.h"
57 static struct ifmedia_description *get_toptype_desc(int);
58 static struct ifmedia_type_to_subtype *get_toptype_ttos(int);
59 static struct ifmedia_description *get_subtype_desc(int,
60 struct ifmedia_type_to_subtype *ttos);
62 #define IFM_OPMODE(x) \
63 ((x) & (IFM_IEEE80211_ADHOC | IFM_IEEE80211_HOSTAP | \
64 IFM_IEEE80211_IBSS | IFM_IEEE80211_WDS | IFM_IEEE80211_MONITOR | \
66 #define IFM_IEEE80211_STA 0
68 static struct ifmedia_description ifm_type_descriptions[] =
69 IFM_TYPE_DESCRIPTIONS;
71 static struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
72 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
74 static struct ifmedia_description ifm_subtype_ethernet_aliases[] =
75 IFM_SUBTYPE_ETHERNET_ALIASES;
77 static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
78 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
80 static struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
81 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
83 static struct ifmedia_description ifm_subtype_tokenring_aliases[] =
84 IFM_SUBTYPE_TOKENRING_ALIASES;
86 static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
87 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
89 static struct ifmedia_description ifm_subtype_fddi_descriptions[] =
90 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
92 static struct ifmedia_description ifm_subtype_fddi_aliases[] =
93 IFM_SUBTYPE_FDDI_ALIASES;
95 static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
96 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
98 static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
99 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
101 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
102 IFM_SUBTYPE_IEEE80211_ALIASES;
104 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
105 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
107 static struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
108 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
110 static struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
111 IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
113 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
114 IFM_SUBTYPE_ATM_DESCRIPTIONS;
116 static struct ifmedia_description ifm_subtype_atm_aliases[] =
117 IFM_SUBTYPE_ATM_ALIASES;
119 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
120 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
122 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
123 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
125 static struct ifmedia_description ifm_subtype_shared_aliases[] =
126 IFM_SUBTYPE_SHARED_ALIASES;
128 static struct ifmedia_description ifm_shared_option_descriptions[] =
129 IFM_SHARED_OPTION_DESCRIPTIONS;
131 static struct ifmedia_description ifm_shared_option_aliases[] =
132 IFM_SHARED_OPTION_ALIASES;
134 struct ifmedia_type_to_subtype {
136 struct ifmedia_description *desc;
141 struct ifmedia_description *desc;
146 struct ifmedia_description *desc;
152 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
153 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] =
157 { &ifm_subtype_shared_descriptions[0], 0 },
158 { &ifm_subtype_shared_aliases[0], 1 },
159 { &ifm_subtype_ethernet_descriptions[0], 0 },
160 { &ifm_subtype_ethernet_aliases[0], 1 },
164 { &ifm_shared_option_descriptions[0], 0 },
165 { &ifm_shared_option_aliases[0], 1 },
166 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
175 { &ifm_subtype_shared_descriptions[0], 0 },
176 { &ifm_subtype_shared_aliases[0], 1 },
177 { &ifm_subtype_tokenring_descriptions[0], 0 },
178 { &ifm_subtype_tokenring_aliases[0], 1 },
182 { &ifm_shared_option_descriptions[0], 0 },
183 { &ifm_shared_option_aliases[0], 1 },
184 { &ifm_subtype_tokenring_option_descriptions[0], 0 },
193 { &ifm_subtype_shared_descriptions[0], 0 },
194 { &ifm_subtype_shared_aliases[0], 1 },
195 { &ifm_subtype_fddi_descriptions[0], 0 },
196 { &ifm_subtype_fddi_aliases[0], 1 },
200 { &ifm_shared_option_descriptions[0], 0 },
201 { &ifm_shared_option_aliases[0], 1 },
202 { &ifm_subtype_fddi_option_descriptions[0], 0 },
211 { &ifm_subtype_shared_descriptions[0], 0 },
212 { &ifm_subtype_shared_aliases[0], 1 },
213 { &ifm_subtype_ieee80211_descriptions[0], 0 },
214 { &ifm_subtype_ieee80211_aliases[0], 1 },
218 { &ifm_shared_option_descriptions[0], 0 },
219 { &ifm_shared_option_aliases[0], 1 },
220 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
224 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
225 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
231 { &ifm_subtype_shared_descriptions[0], 0 },
232 { &ifm_subtype_shared_aliases[0], 1 },
233 { &ifm_subtype_atm_descriptions[0], 0 },
234 { &ifm_subtype_atm_aliases[0], 1 },
238 { &ifm_shared_option_descriptions[0], 0 },
239 { &ifm_shared_option_aliases[0], 1 },
240 { &ifm_subtype_atm_option_descriptions[0], 0 },
249 static struct ifmedia_description *
250 get_toptype_desc(int ifmw)
252 struct ifmedia_description *desc;
254 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++) {
255 if (IFM_TYPE(ifmw) == desc->ifmt_word) {
263 static struct ifmedia_type_to_subtype *
264 get_toptype_ttos(int ifmw)
266 struct ifmedia_description *desc;
267 struct ifmedia_type_to_subtype *ttos;
269 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
270 desc->ifmt_string != NULL; desc++, ttos++) {
271 if (IFM_TYPE(ifmw) == desc->ifmt_word) {
279 static struct ifmedia_description *
280 get_subtype_desc(int ifmw,
281 struct ifmedia_type_to_subtype *ttos)
284 struct ifmedia_description *desc;
286 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
287 if (ttos->subtypes[i].alias) {
290 for (desc = ttos->subtypes[i].desc;
291 desc->ifmt_string != NULL; desc++) {
292 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) {
302 ifconfig_media_get_type(int ifmw)
304 struct ifmedia_description *desc;
306 /*int seen_option = 0, i;*/
308 /* Find the top-level interface type. */
309 desc = get_toptype_desc(ifmw);
310 if (desc->ifmt_string == NULL) {
311 return ("<unknown type>");
313 return (desc->ifmt_string);
318 ifconfig_media_get_subtype(int ifmw)
320 struct ifmedia_description *desc;
321 struct ifmedia_type_to_subtype *ttos;
323 ttos = get_toptype_ttos(ifmw);
324 desc = get_subtype_desc(ifmw, ttos);
325 return (desc->ifmt_string);
328 /***************************************************************************
329 * Above this point, this file is mostly copied from sbin/ifconfig/ifmedia.c
330 ***************************************************************************/
332 /* Internal structure used for allocations and frees */
333 struct _ifconfig_media_status {
334 struct ifmediareq ifmr;
339 ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name,
340 struct ifmediareq **ifmr)
342 struct _ifconfig_media_status *ms, *ms2;
343 unsigned long cmd = SIOCGIFXMEDIA;
346 ms = calloc(1, sizeof(*ms));
348 h->error.errtype = OTHER;
349 h->error.errcode = ENOMEM;
352 (void)memset(ms, 0, sizeof(*ms));
353 (void)strlcpy(ms->ifmr.ifm_name, name, sizeof(ms->ifmr.ifm_name));
356 * Check if interface supports extended media types.
358 if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms->ifmr) < 0) {
360 if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms->ifmr) < 0) {
361 /* Interface doesn't support SIOC{G,S}IFMEDIA. */
362 h->error.errtype = OK;
367 if (ms->ifmr.ifm_count == 0) {
369 return (0); /* Interface has no media types ?*/
372 ms2 = realloc(ms, sizeof(*ms) + sizeof(int) * ms->ifmr.ifm_count);
374 h->error.errtype = OTHER;
375 h->error.errcode = ENOMEM;
379 ms2->ifmr.ifm_ulist = &ms2->medialist[0];
381 if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms2->ifmr) < 0) {
391 ifconfig_media_get_status(const struct ifmediareq *ifmr)
393 switch (IFM_TYPE(ifmr->ifm_active)) {
396 if (ifmr->ifm_status & IFM_ACTIVE) {
399 return ("no carrier");
405 if (ifmr->ifm_status & IFM_ACTIVE) {
413 if (ifmr->ifm_status & IFM_ACTIVE) {
414 /* NB: only sta mode associates */
415 if (IFM_OPMODE(ifmr->ifm_active) == IFM_IEEE80211_STA) {
416 return ("associated");
421 return ("no carrier");
430 ifconfig_media_get_options_string(int ifmw, char *buf, size_t buflen)
432 struct ifmedia_type_to_subtype *ttos;
433 struct ifmedia_description *desc;
434 int i, seen_option = 0;
439 ttos = get_toptype_ttos(ifmw);
440 for (i = 0; ttos->options[i].desc != NULL; i++) {
441 if (ttos->options[i].alias) {
444 for (desc = ttos->options[i].desc;
445 desc->ifmt_string != NULL; desc++) {
446 if (ifmw & desc->ifmt_word) {
448 strlcat(buf, ",", buflen);
450 len = strlcat(buf, desc->ifmt_string, buflen);
451 assert(len < buflen);