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_ieee80211_descriptions[] =
81 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
83 static struct ifmedia_description ifm_subtype_ieee80211_aliases[] =
84 IFM_SUBTYPE_IEEE80211_ALIASES;
86 static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
87 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
89 static struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
90 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
92 static struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] =
93 IFM_SUBTYPE_IEEE80211_MODE_ALIASES;
95 static struct ifmedia_description ifm_subtype_atm_descriptions[] =
96 IFM_SUBTYPE_ATM_DESCRIPTIONS;
98 static struct ifmedia_description ifm_subtype_atm_aliases[] =
99 IFM_SUBTYPE_ATM_ALIASES;
101 static struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
102 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
104 static struct ifmedia_description ifm_subtype_shared_descriptions[] =
105 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
107 static struct ifmedia_description ifm_subtype_shared_aliases[] =
108 IFM_SUBTYPE_SHARED_ALIASES;
110 static struct ifmedia_description ifm_shared_option_descriptions[] =
111 IFM_SHARED_OPTION_DESCRIPTIONS;
113 static struct ifmedia_description ifm_shared_option_aliases[] =
114 IFM_SHARED_OPTION_ALIASES;
116 struct ifmedia_type_to_subtype {
118 struct ifmedia_description *desc;
123 struct ifmedia_description *desc;
128 struct ifmedia_description *desc;
134 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
135 static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] =
139 { &ifm_subtype_shared_descriptions[0], 0 },
140 { &ifm_subtype_shared_aliases[0], 1 },
141 { &ifm_subtype_ethernet_descriptions[0], 0 },
142 { &ifm_subtype_ethernet_aliases[0], 1 },
146 { &ifm_shared_option_descriptions[0], 0 },
147 { &ifm_shared_option_aliases[0], 1 },
148 { &ifm_subtype_ethernet_option_descriptions[0], 0 },
157 { &ifm_subtype_shared_descriptions[0], 0 },
158 { &ifm_subtype_shared_aliases[0], 1 },
159 { &ifm_subtype_ieee80211_descriptions[0], 0 },
160 { &ifm_subtype_ieee80211_aliases[0], 1 },
164 { &ifm_shared_option_descriptions[0], 0 },
165 { &ifm_shared_option_aliases[0], 1 },
166 { &ifm_subtype_ieee80211_option_descriptions[0], 0 },
170 { &ifm_subtype_ieee80211_mode_descriptions[0], 0 },
171 { &ifm_subtype_ieee80211_mode_aliases[0], 0 },
177 { &ifm_subtype_shared_descriptions[0], 0 },
178 { &ifm_subtype_shared_aliases[0], 1 },
179 { &ifm_subtype_atm_descriptions[0], 0 },
180 { &ifm_subtype_atm_aliases[0], 1 },
184 { &ifm_shared_option_descriptions[0], 0 },
185 { &ifm_shared_option_aliases[0], 1 },
186 { &ifm_subtype_atm_option_descriptions[0], 0 },
195 static struct ifmedia_description *
196 get_toptype_desc(int ifmw)
198 struct ifmedia_description *desc;
200 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++) {
201 if (IFM_TYPE(ifmw) == desc->ifmt_word) {
209 static struct ifmedia_type_to_subtype *
210 get_toptype_ttos(int ifmw)
212 struct ifmedia_description *desc;
213 struct ifmedia_type_to_subtype *ttos;
215 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
216 desc->ifmt_string != NULL; desc++, ttos++) {
217 if (IFM_TYPE(ifmw) == desc->ifmt_word) {
225 static struct ifmedia_description *
226 get_subtype_desc(int ifmw,
227 struct ifmedia_type_to_subtype *ttos)
230 struct ifmedia_description *desc;
232 for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
233 if (ttos->subtypes[i].alias) {
236 for (desc = ttos->subtypes[i].desc;
237 desc->ifmt_string != NULL; desc++) {
238 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) {
248 ifconfig_media_get_type(int ifmw)
250 struct ifmedia_description *desc;
252 /*int seen_option = 0, i;*/
254 /* Find the top-level interface type. */
255 desc = get_toptype_desc(ifmw);
256 if (desc->ifmt_string == NULL) {
257 return ("<unknown type>");
259 return (desc->ifmt_string);
264 ifconfig_media_get_subtype(int ifmw)
266 struct ifmedia_description *desc;
267 struct ifmedia_type_to_subtype *ttos;
269 ttos = get_toptype_ttos(ifmw);
270 desc = get_subtype_desc(ifmw, ttos);
271 return (desc->ifmt_string);
274 /***************************************************************************
275 * Above this point, this file is mostly copied from sbin/ifconfig/ifmedia.c
276 ***************************************************************************/
278 /* Internal structure used for allocations and frees */
279 struct _ifconfig_media_status {
280 struct ifmediareq ifmr;
285 ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name,
286 struct ifmediareq **ifmr)
288 struct _ifconfig_media_status *ms, *ms2;
289 unsigned long cmd = SIOCGIFXMEDIA;
292 ms = calloc(1, sizeof(*ms));
294 h->error.errtype = OTHER;
295 h->error.errcode = ENOMEM;
298 (void)memset(ms, 0, sizeof(*ms));
299 (void)strlcpy(ms->ifmr.ifm_name, name, sizeof(ms->ifmr.ifm_name));
302 * Check if interface supports extended media types.
304 if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms->ifmr) < 0) {
306 if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms->ifmr) < 0) {
307 /* Interface doesn't support SIOC{G,S}IFMEDIA. */
308 h->error.errtype = OK;
313 if (ms->ifmr.ifm_count == 0) {
315 return (0); /* Interface has no media types ?*/
318 ms2 = realloc(ms, sizeof(*ms) + sizeof(int) * ms->ifmr.ifm_count);
320 h->error.errtype = OTHER;
321 h->error.errcode = ENOMEM;
325 ms2->ifmr.ifm_ulist = &ms2->medialist[0];
327 if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms2->ifmr) < 0) {
337 ifconfig_media_get_status(const struct ifmediareq *ifmr)
339 switch (IFM_TYPE(ifmr->ifm_active)) {
342 if (ifmr->ifm_status & IFM_ACTIVE) {
345 return ("no carrier");
350 if (ifmr->ifm_status & IFM_ACTIVE) {
351 /* NB: only sta mode associates */
352 if (IFM_OPMODE(ifmr->ifm_active) == IFM_IEEE80211_STA) {
353 return ("associated");
358 return ("no carrier");
367 ifconfig_media_get_options_string(int ifmw, char *buf, size_t buflen)
369 struct ifmedia_type_to_subtype *ttos;
370 struct ifmedia_description *desc;
371 int i, seen_option = 0;
376 ttos = get_toptype_ttos(ifmw);
377 for (i = 0; ttos->options[i].desc != NULL; i++) {
378 if (ttos->options[i].alias) {
381 for (desc = ttos->options[i].desc;
382 desc->ifmt_string != NULL; desc++) {
383 if (ifmw & desc->ifmt_word) {
385 strlcat(buf, ",", buflen);
387 len = strlcat(buf, desc->ifmt_string, buflen);
388 assert(len < buflen);