1 /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
8 * Jonathan Stone and Jason R. Thorpe. All rights reserved.
10 * This software is derived from information provided by Matt Thomas.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Jonathan Stone
23 * and Jason R. Thorpe for the NetBSD Project.
24 * 4. The names of the authors may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * BSD/OS-compatible network interface media selection.
43 * Where it is safe to do so, this code strays slightly from the BSD/OS
44 * design. Software which uses the API (device drivers, basically)
45 * shouldn't notice any difference.
47 * Many thanks to Matt Thomas for providing the information necessary
48 * to implement this interface.
51 #include "opt_ifmedia.h"
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/malloc.h>
58 #include <sys/module.h>
59 #include <sys/sysctl.h>
62 #include <net/if_media.h>
65 * Compile-time options:
67 * turn on implementation-level debug printfs.
68 * Useful for debugging newly-ported drivers.
71 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
75 #include <net/if_var.h>
76 int ifmedia_debug = 0;
77 SYSCTL_INT(_debug, OID_AUTO, ifmedia, CTLFLAG_RW, &ifmedia_debug,
78 0, "if_media debugging msgs");
79 static void ifmedia_printword(int);
83 * Initialize if_media struct for a specific interface instance.
86 ifmedia_init(ifm, dontcare_mask, change_callback, status_callback)
89 ifm_change_cb_t change_callback;
90 ifm_stat_cb_t status_callback;
93 LIST_INIT(&ifm->ifm_list);
96 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
97 ifm->ifm_change = change_callback;
98 ifm->ifm_status = status_callback;
102 ifmedia_removeall(ifm)
105 struct ifmedia_entry *entry;
107 for (entry = LIST_FIRST(&ifm->ifm_list); entry;
108 entry = LIST_FIRST(&ifm->ifm_list)) {
109 LIST_REMOVE(entry, ifm_list);
110 free(entry, M_IFADDR);
116 * Add a media configuration to the list of supported media
117 * for a specific interface instance.
120 ifmedia_add(ifm, mword, data, aux)
126 struct ifmedia_entry *entry;
131 printf("ifmedia_add: null ifm\n");
134 printf("Adding entry for ");
135 ifmedia_printword(mword);
139 entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
141 panic("ifmedia_add: can't malloc entry");
143 entry->ifm_media = mword;
144 entry->ifm_data = data;
145 entry->ifm_aux = aux;
147 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
151 * Add an array of media configurations to the list of
152 * supported media for a specific interface instance.
155 ifmedia_list_add(ifm, lp, count)
157 struct ifmedia_entry *lp;
162 for (i = 0; i < count; i++)
163 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
168 * Set the default active media.
170 * Called by device-specific code which is assumed to have already
171 * selected the default media in hardware. We do _not_ call the
172 * media-change callback.
175 ifmedia_set(ifm, target)
180 struct ifmedia_entry *match;
182 match = ifmedia_match(ifm, target, ifm->ifm_mask);
185 printf("ifmedia_set: no match for 0x%x/0x%x\n",
186 target, ~ifm->ifm_mask);
187 panic("ifmedia_set");
189 ifm->ifm_cur = match;
193 printf("ifmedia_set: target ");
194 ifmedia_printword(target);
195 printf("ifmedia_set: setting to ");
196 ifmedia_printword(ifm->ifm_cur->ifm_media);
202 * Given a media word, return one suitable for an application
203 * using the original encoding.
206 compat_media(int media)
209 if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
210 media &= ~(IFM_ETH_XTYPE|IFM_TMASK);
217 * Device-independent media ioctl support function.
220 ifmedia_ioctl(ifp, ifr, ifm, cmd)
226 struct ifmedia_entry *match;
227 struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
230 if (ifp == NULL || ifr == NULL || ifm == NULL)
236 * Set the current media.
240 struct ifmedia_entry *oldentry;
242 int newmedia = ifr->ifr_media;
244 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
249 "ifmedia_ioctl: no media found for 0x%x\n",
257 * If no change, we're done.
258 * XXX Automedia may invole software intervention.
259 * Keep going in case the connected media changed.
260 * Similarly, if best match changed (kernel debugger?).
262 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
263 (newmedia == ifm->ifm_media) &&
264 (match == ifm->ifm_cur))
268 * We found a match, now make the driver switch to it.
269 * Make sure to preserve our old media type in case the
270 * driver can't switch.
274 printf("ifmedia_ioctl: switching %s to ",
276 ifmedia_printword(match->ifm_media);
279 oldentry = ifm->ifm_cur;
280 oldmedia = ifm->ifm_media;
281 ifm->ifm_cur = match;
282 ifm->ifm_media = newmedia;
283 error = (*ifm->ifm_change)(ifp);
285 ifm->ifm_cur = oldentry;
286 ifm->ifm_media = oldmedia;
292 * Get list of available media and current media on interface.
297 struct ifmedia_entry *ep;
300 if (ifmr->ifm_count < 0)
303 if (cmd == SIOCGIFMEDIA) {
304 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
305 compat_media(ifm->ifm_cur->ifm_media) : IFM_NONE;
307 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
308 ifm->ifm_cur->ifm_media : IFM_NONE;
310 ifmr->ifm_mask = ifm->ifm_mask;
311 ifmr->ifm_status = 0;
312 (*ifm->ifm_status)(ifp, ifmr);
315 * If there are more interfaces on the list, count
316 * them. This allows the caller to set ifmr->ifm_count
317 * to 0 on the first call to know how much space to
321 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list)
322 if (i++ < ifmr->ifm_count) {
323 error = copyout(&ep->ifm_media,
324 ifmr->ifm_ulist + i - 1, sizeof(int));
328 if (error == 0 && i > ifmr->ifm_count)
329 error = ifmr->ifm_count ? E2BIG : 0;
342 * Find media entry matching a given ifm word.
345 static struct ifmedia_entry *
346 ifmedia_match(ifm, target, mask)
351 struct ifmedia_entry *match, *next;
356 LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
357 if ((next->ifm_media & mask) == (target & mask)) {
358 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
360 printf("ifmedia_match: multiple match for "
361 "0x%x/0x%x\n", target, mask);
372 * Compute the interface `baudrate' from the media, for the interface
373 * metrics (used by routing daemons).
375 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
376 IFM_BAUDRATE_DESCRIPTIONS;
379 ifmedia_baudrate(int mword)
383 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
384 if (IFM_TYPE_MATCH(mword, ifmedia_baudrate_descriptions[i].ifmb_word))
385 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
393 struct ifmedia_description ifm_type_descriptions[] =
394 IFM_TYPE_DESCRIPTIONS;
396 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
397 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
399 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
400 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
402 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
403 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
405 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
406 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
408 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
409 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
411 struct ifmedia_description ifm_subtype_atm_descriptions[] =
412 IFM_SUBTYPE_ATM_DESCRIPTIONS;
414 struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
415 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
417 struct ifmedia_description ifm_subtype_shared_descriptions[] =
418 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
420 struct ifmedia_description ifm_shared_option_descriptions[] =
421 IFM_SHARED_OPTION_DESCRIPTIONS;
423 struct ifmedia_type_to_subtype {
424 struct ifmedia_description *subtypes;
425 struct ifmedia_description *options;
426 struct ifmedia_description *modes;
429 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
430 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
432 &ifm_subtype_ethernet_descriptions[0],
433 &ifm_subtype_ethernet_option_descriptions[0],
437 &ifm_subtype_ieee80211_descriptions[0],
438 &ifm_subtype_ieee80211_option_descriptions[0],
439 &ifm_subtype_ieee80211_mode_descriptions[0]
442 &ifm_subtype_atm_descriptions[0],
443 &ifm_subtype_atm_option_descriptions[0],
449 * print a media word.
452 ifmedia_printword(ifmw)
455 struct ifmedia_description *desc;
456 struct ifmedia_type_to_subtype *ttos;
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 (IFM_TYPE(ifmw) == desc->ifmt_word)
464 if (desc->ifmt_string == NULL) {
465 printf("<unknown type>\n");
468 printf("%s", desc->ifmt_string);
471 for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++)
472 if (IFM_MODE(ifmw) == desc->ifmt_word) {
473 if (desc->ifmt_string != NULL)
474 printf(" mode %s", desc->ifmt_string);
479 * Check for the shared subtype descriptions first, then the
480 * type-specific ones.
482 for (desc = ifm_subtype_shared_descriptions;
483 desc->ifmt_string != NULL; desc++)
484 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
487 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
488 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
490 if (desc->ifmt_string == NULL) {
491 printf(" <unknown subtype>\n");
496 printf(" %s", desc->ifmt_string);
499 * Look for shared options.
501 for (desc = ifm_shared_option_descriptions;
502 desc->ifmt_string != NULL; desc++) {
503 if (ifmw & desc->ifmt_word) {
504 if (seen_option == 0)
506 printf("%s%s", seen_option++ ? "," : "",
512 * Look for subtype-specific options.
514 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
515 if (ifmw & desc->ifmt_word) {
516 if (seen_option == 0)
518 printf("%s%s", seen_option++ ? "," : "",
522 printf("%s\n", seen_option ? ">" : "");
524 #endif /* IFMEDIA_DEBUG */