1 /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
7 * Jonathan Stone and Jason R. Thorpe. All rights reserved.
9 * This software is derived from information provided by Matt Thomas.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Jonathan Stone
22 * and Jason R. Thorpe for the NetBSD Project.
23 * 4. The names of the authors may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * BSD/OS-compatible network interface media selection.
42 * Where it is safe to do so, this code strays slightly from the BSD/OS
43 * design. Software which uses the API (device drivers, basically)
44 * shouldn't notice any difference.
46 * Many thanks to Matt Thomas for providing the information necessary
47 * to implement this interface.
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include "opt_ifmedia.h"
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/socket.h>
58 #include <sys/sockio.h>
59 #include <sys/malloc.h>
60 #include <sys/module.h>
61 #include <sys/sysctl.h>
64 #include <net/if_media.h>
67 * Compile-time options:
69 * turn on implementation-level debug printfs.
70 * Useful for debugging newly-ported drivers.
73 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
77 #include <net/if_var.h>
78 int ifmedia_debug = 0;
79 SYSCTL_INT(_debug, OID_AUTO, ifmedia, CTLFLAG_RW, &ifmedia_debug,
80 0, "if_media debugging msgs");
81 static void ifmedia_printword(int);
85 * Initialize if_media struct for a specific interface instance.
88 ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
89 ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
92 LIST_INIT(&ifm->ifm_list);
95 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
96 ifm->ifm_change = change_callback;
97 ifm->ifm_status = status_callback;
101 ifmedia_removeall(struct ifmedia *ifm)
103 struct ifmedia_entry *entry;
105 while ((entry = LIST_FIRST(&ifm->ifm_list)) != NULL) {
106 LIST_REMOVE(entry, ifm_list);
107 free(entry, M_IFADDR);
113 * Add a media configuration to the list of supported media
114 * for a specific interface instance.
117 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
119 struct ifmedia_entry *entry;
124 printf("ifmedia_add: null ifm\n");
127 printf("Adding entry for (%#010x) ", mword);
128 ifmedia_printword(mword);
132 entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
134 panic("ifmedia_add: can't malloc entry");
136 entry->ifm_media = mword;
137 entry->ifm_data = data;
138 entry->ifm_aux = aux;
140 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
144 * Add an array of media configurations to the list of
145 * supported media for a specific interface instance.
148 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count)
152 for (i = 0; i < count; i++)
153 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
158 * Set the default active media.
160 * Called by device-specific code which is assumed to have already
161 * selected the default media in hardware. We do _not_ call the
162 * media-change callback.
165 ifmedia_set(struct ifmedia *ifm, int target)
167 struct ifmedia_entry *match;
169 match = ifmedia_match(ifm, target, ifm->ifm_mask);
172 printf("ifmedia_set: no match for 0x%x/0x%x\n",
173 target, ~ifm->ifm_mask);
174 panic("ifmedia_set");
176 ifm->ifm_cur = match;
180 printf("ifmedia_set: target ");
181 ifmedia_printword(target);
182 printf("ifmedia_set: setting to ");
183 ifmedia_printword(ifm->ifm_cur->ifm_media);
189 * Given a media word, return one suitable for an application
190 * using the original encoding.
193 compat_media(int media)
196 if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
197 media &= ~(IFM_ETH_XTYPE|IFM_TMASK);
204 * Device-independent media ioctl support function.
207 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
210 struct ifmedia_entry *match;
211 struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
214 if (ifp == NULL || ifr == NULL || ifm == NULL)
219 * Set the current media.
223 struct ifmedia_entry *oldentry;
225 int newmedia = ifr->ifr_media;
227 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
232 "ifmedia_ioctl: no media found for %#010x mask %#010x\n",
233 newmedia, ifm->ifm_mask);
240 * If no change, we're done.
241 * XXX Automedia may invole software intervention.
242 * Keep going in case the connected media changed.
243 * Similarly, if best match changed (kernel debugger?).
245 if (IFM_SUBTYPE(newmedia) != IFM_AUTO &&
246 newmedia == ifm->ifm_media && match == ifm->ifm_cur)
250 * We found a match, now make the driver switch to it.
251 * Make sure to preserve our old media type in case the
252 * driver can't switch.
256 printf("ifmedia_ioctl: switching %s to ",
258 ifmedia_printword(match->ifm_media);
261 oldentry = ifm->ifm_cur;
262 oldmedia = ifm->ifm_media;
263 ifm->ifm_cur = match;
264 ifm->ifm_media = newmedia;
265 error = (*ifm->ifm_change)(ifp);
267 ifm->ifm_cur = oldentry;
268 ifm->ifm_media = oldmedia;
274 * Get list of available media and current media on interface.
279 struct ifmedia_entry *ep;
282 if (ifmr->ifm_count < 0)
285 if (cmd == SIOCGIFMEDIA) {
286 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
287 compat_media(ifm->ifm_cur->ifm_media) : IFM_NONE;
289 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
290 ifm->ifm_cur->ifm_media : IFM_NONE;
292 ifmr->ifm_mask = ifm->ifm_mask;
293 ifmr->ifm_status = 0;
294 (*ifm->ifm_status)(ifp, ifmr);
297 * If there are more interfaces on the list, count
298 * them. This allows the caller to set ifmr->ifm_count
299 * to 0 on the first call to know how much space to
303 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list) {
304 if (i < ifmr->ifm_count) {
305 error = copyout(&ep->ifm_media,
306 ifmr->ifm_ulist + i, sizeof(int));
312 if (error == 0 && i > ifmr->ifm_count)
313 error = ifmr->ifm_count != 0 ? E2BIG : 0;
326 * Find media entry matching a given ifm word.
329 static struct ifmedia_entry *
330 ifmedia_match(struct ifmedia *ifm, int target, int mask)
332 struct ifmedia_entry *match, *next;
337 LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
338 if ((next->ifm_media & mask) == (target & mask)) {
339 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
341 printf("ifmedia_match: multiple match for "
342 "%#010x/%#010x\n", target, mask);
353 * Compute the interface `baudrate' from the media, for the interface
354 * metrics (used by routing daemons).
356 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
357 IFM_BAUDRATE_DESCRIPTIONS;
360 ifmedia_baudrate(int mword)
364 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
365 if (IFM_TYPE_MATCH(mword, ifmedia_baudrate_descriptions[i].
367 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
375 static const struct ifmedia_description ifm_type_descriptions[] =
376 IFM_TYPE_DESCRIPTIONS;
378 static const struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
379 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
381 static const struct ifmedia_description
382 ifm_subtype_ethernet_option_descriptions[] =
383 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
385 static const struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
386 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
388 static const struct ifmedia_description
389 ifm_subtype_ieee80211_option_descriptions[] =
390 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
392 static const struct ifmedia_description
393 ifm_subtype_ieee80211_mode_descriptions[] =
394 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
396 static const struct ifmedia_description ifm_subtype_atm_descriptions[] =
397 IFM_SUBTYPE_ATM_DESCRIPTIONS;
399 static const struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
400 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
402 static const struct ifmedia_description ifm_subtype_shared_descriptions[] =
403 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
405 static const struct ifmedia_description ifm_shared_option_descriptions[] =
406 IFM_SHARED_OPTION_DESCRIPTIONS;
408 struct ifmedia_type_to_subtype {
409 const struct ifmedia_description *subtypes;
410 const struct ifmedia_description *options;
411 const struct ifmedia_description *modes;
414 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
415 static const struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
417 &ifm_subtype_ethernet_descriptions[0],
418 &ifm_subtype_ethernet_option_descriptions[0],
422 &ifm_subtype_ieee80211_descriptions[0],
423 &ifm_subtype_ieee80211_option_descriptions[0],
424 &ifm_subtype_ieee80211_mode_descriptions[0]
427 &ifm_subtype_atm_descriptions[0],
428 &ifm_subtype_atm_option_descriptions[0],
434 * print a media word.
437 ifmedia_printword(int ifmw)
439 const struct ifmedia_description *desc;
440 const struct ifmedia_type_to_subtype *ttos;
443 /* Find the top-level interface type. */
444 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
445 desc->ifmt_string != NULL; desc++, ttos++)
446 if (IFM_TYPE(ifmw) == desc->ifmt_word)
448 if (desc->ifmt_string == NULL) {
449 printf("<unknown type>\n");
452 printf("%s", desc->ifmt_string);
455 for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++)
456 if (IFM_MODE(ifmw) == desc->ifmt_word) {
457 if (desc->ifmt_string != NULL)
458 printf(" mode %s", desc->ifmt_string);
463 * Check for the shared subtype descriptions first, then the
464 * type-specific ones.
466 for (desc = ifm_subtype_shared_descriptions;
467 desc->ifmt_string != NULL; desc++)
468 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
471 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
472 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
474 if (desc->ifmt_string == NULL) {
475 printf(" <unknown subtype>\n");
480 printf(" %s", desc->ifmt_string);
483 * Look for shared options.
485 for (desc = ifm_shared_option_descriptions;
486 desc->ifmt_string != NULL; desc++) {
487 if (ifmw & desc->ifmt_word) {
488 if (seen_option == 0)
490 printf("%s%s", seen_option++ ? "," : "",
496 * Look for subtype-specific options.
498 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
499 if (ifmw & desc->ifmt_word) {
500 if (seen_option == 0)
502 printf("%s%s", seen_option++ ? "," : "",
506 printf("%s\n", seen_option ? ">" : "");
508 #endif /* IFMEDIA_DEBUG */