1 /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
6 * Jonathan Stone and Jason R. Thorpe. All rights reserved.
8 * This software is derived from information provided by Matt Thomas.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Jonathan Stone
21 * and Jason R. Thorpe for the NetBSD Project.
22 * 4. The names of the authors may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * BSD/OS-compatible network interface media selection.
41 * Where it is safe to do so, this code strays slightly from the BSD/OS
42 * design. Software which uses the API (device drivers, basically)
43 * shouldn't notice any difference.
45 * Many thanks to Matt Thomas for providing the information necessary
46 * to implement this interface.
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/malloc.h>
54 #include <sys/module.h>
55 #include <sys/sysctl.h>
58 #include <net/if_media.h>
61 * Compile-time options:
63 * turn on implementation-level debug printfs.
64 * Useful for debugging newly-ported drivers.
67 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
71 #include <net/if_var.h>
72 int ifmedia_debug = 0;
73 SYSCTL_INT(_debug, OID_AUTO, ifmedia, CTLFLAG_RW, &ifmedia_debug,
74 0, "if_media debugging msgs");
75 static void ifmedia_printword(int);
79 * Initialize if_media struct for a specific interface instance.
82 ifmedia_init(ifm, dontcare_mask, change_callback, status_callback)
85 ifm_change_cb_t change_callback;
86 ifm_stat_cb_t status_callback;
89 LIST_INIT(&ifm->ifm_list);
92 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
93 ifm->ifm_change = change_callback;
94 ifm->ifm_status = status_callback;
98 ifmedia_removeall(ifm)
101 struct ifmedia_entry *entry;
103 for (entry = LIST_FIRST(&ifm->ifm_list); entry;
104 entry = LIST_FIRST(&ifm->ifm_list)) {
105 LIST_REMOVE(entry, ifm_list);
106 free(entry, M_IFADDR);
111 * Add a media configuration to the list of supported media
112 * for a specific interface instance.
115 ifmedia_add(ifm, mword, data, aux)
121 register struct ifmedia_entry *entry;
126 printf("ifmedia_add: null ifm\n");
129 printf("Adding entry for ");
130 ifmedia_printword(mword);
134 entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
136 panic("ifmedia_add: can't malloc entry");
138 entry->ifm_media = mword;
139 entry->ifm_data = data;
140 entry->ifm_aux = aux;
142 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
146 * Add an array of media configurations to the list of
147 * supported media for a specific interface instance.
150 ifmedia_list_add(ifm, lp, count)
152 struct ifmedia_entry *lp;
157 for (i = 0; i < count; i++)
158 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
163 * Set the default active media.
165 * Called by device-specific code which is assumed to have already
166 * selected the default media in hardware. We do _not_ call the
167 * media-change callback.
170 ifmedia_set(ifm, target)
175 struct ifmedia_entry *match;
177 match = ifmedia_match(ifm, target, ifm->ifm_mask);
180 printf("ifmedia_set: no match for 0x%x/0x%x\n",
181 target, ~ifm->ifm_mask);
182 panic("ifmedia_set");
184 ifm->ifm_cur = match;
188 printf("ifmedia_set: target ");
189 ifmedia_printword(target);
190 printf("ifmedia_set: setting to ");
191 ifmedia_printword(ifm->ifm_cur->ifm_media);
197 * Given a media word, return one suitable for an application
198 * using the original encoding.
201 compat_media(int media)
204 if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
205 media &= ~(IFM_ETH_XTYPE|IFM_TMASK);
212 * Device-independent media ioctl support function.
215 ifmedia_ioctl(ifp, ifr, ifm, cmd)
221 struct ifmedia_entry *match;
222 struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
223 int error = 0, sticky;
225 if (ifp == NULL || ifr == NULL || ifm == NULL)
231 * Set the current media.
235 struct ifmedia_entry *oldentry;
237 int newmedia = ifr->ifr_media;
239 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
244 "ifmedia_ioctl: no media found for 0x%x\n",
252 * If no change, we're done.
253 * XXX Automedia may invole software intervention.
254 * Keep going in case the connected media changed.
255 * Similarly, if best match changed (kernel debugger?).
257 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
258 (newmedia == ifm->ifm_media) &&
259 (match == ifm->ifm_cur))
263 * We found a match, now make the driver switch to it.
264 * Make sure to preserve our old media type in case the
265 * driver can't switch.
269 printf("ifmedia_ioctl: switching %s to ",
271 ifmedia_printword(match->ifm_media);
274 oldentry = ifm->ifm_cur;
275 oldmedia = ifm->ifm_media;
276 ifm->ifm_cur = match;
277 ifm->ifm_media = newmedia;
278 error = (*ifm->ifm_change)(ifp);
280 ifm->ifm_cur = oldentry;
281 ifm->ifm_media = oldmedia;
287 * Get list of available media and current media on interface.
292 struct ifmedia_entry *ep;
294 int usermax; /* user requested max */
296 kptr = NULL; /* XXX gcc */
298 if (cmd == SIOCGIFMEDIA) {
299 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
300 compat_media(ifm->ifm_cur->ifm_media) : IFM_NONE;
302 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
303 ifm->ifm_cur->ifm_media : IFM_NONE;
305 ifmr->ifm_mask = ifm->ifm_mask;
306 ifmr->ifm_status = 0;
307 (*ifm->ifm_status)(ifp, ifmr);
313 * If there are more interfaces on the list, count
314 * them. This allows the caller to set ifmr->ifm_count
315 * to 0 on the first call to know how much space to
318 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list)
322 * Don't allow the user to ask for too many
323 * or a negative number.
325 if (ifmr->ifm_count > usermax)
326 ifmr->ifm_count = usermax;
327 else if (ifmr->ifm_count < 0)
330 if (ifmr->ifm_count != 0) {
331 kptr = (int *)malloc(ifmr->ifm_count * sizeof(int),
337 * Get the media words from the interface's list.
339 ep = LIST_FIRST(&ifm->ifm_list);
340 for (; ep != NULL && count < ifmr->ifm_count;
341 ep = LIST_NEXT(ep, ifm_list), count++)
342 kptr[count] = ep->ifm_media;
345 error = E2BIG; /* oops! */
351 * We do the copyout on E2BIG, because that's
352 * just our way of telling userland that there
353 * are more. This is the behavior I've observed
357 if ((error == 0 || error == E2BIG) && ifmr->ifm_count != 0) {
358 error = copyout((caddr_t)kptr,
359 (caddr_t)ifmr->ifm_ulist,
360 ifmr->ifm_count * sizeof(int));
366 if (ifmr->ifm_count != 0)
369 ifmr->ifm_count = count;
381 * Find media entry matching a given ifm word.
384 static struct ifmedia_entry *
385 ifmedia_match(ifm, target, mask)
390 struct ifmedia_entry *match, *next;
395 LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
396 if ((next->ifm_media & mask) == (target & mask)) {
397 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
399 printf("ifmedia_match: multiple match for "
400 "0x%x/0x%x\n", target, mask);
411 * Compute the interface `baudrate' from the media, for the interface
412 * metrics (used by routing daemons).
414 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
415 IFM_BAUDRATE_DESCRIPTIONS;
418 ifmedia_baudrate(int mword)
422 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
423 if (IFM_TYPE_MATCH(mword, ifmedia_baudrate_descriptions[i].ifmb_word))
424 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
432 struct ifmedia_description ifm_type_descriptions[] =
433 IFM_TYPE_DESCRIPTIONS;
435 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
436 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
438 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
439 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
441 struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
442 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
444 struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
445 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
447 struct ifmedia_description ifm_subtype_fddi_descriptions[] =
448 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
450 struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
451 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
453 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
454 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
456 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
457 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
459 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
460 IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
462 struct ifmedia_description ifm_subtype_atm_descriptions[] =
463 IFM_SUBTYPE_ATM_DESCRIPTIONS;
465 struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
466 IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
468 struct ifmedia_description ifm_subtype_shared_descriptions[] =
469 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
471 struct ifmedia_description ifm_shared_option_descriptions[] =
472 IFM_SHARED_OPTION_DESCRIPTIONS;
474 struct ifmedia_type_to_subtype {
475 struct ifmedia_description *subtypes;
476 struct ifmedia_description *options;
477 struct ifmedia_description *modes;
480 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
481 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
483 &ifm_subtype_ethernet_descriptions[0],
484 &ifm_subtype_ethernet_option_descriptions[0],
488 &ifm_subtype_tokenring_descriptions[0],
489 &ifm_subtype_tokenring_option_descriptions[0],
493 &ifm_subtype_fddi_descriptions[0],
494 &ifm_subtype_fddi_option_descriptions[0],
498 &ifm_subtype_ieee80211_descriptions[0],
499 &ifm_subtype_ieee80211_option_descriptions[0],
500 &ifm_subtype_ieee80211_mode_descriptions[0]
503 &ifm_subtype_atm_descriptions[0],
504 &ifm_subtype_atm_option_descriptions[0],
510 * print a media word.
513 ifmedia_printword(ifmw)
516 struct ifmedia_description *desc;
517 struct ifmedia_type_to_subtype *ttos;
520 /* Find the top-level interface type. */
521 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
522 desc->ifmt_string != NULL; desc++, ttos++)
523 if (IFM_TYPE(ifmw) == desc->ifmt_word)
525 if (desc->ifmt_string == NULL) {
526 printf("<unknown type>\n");
529 printf("%s", desc->ifmt_string);
532 for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++)
533 if (IFM_MODE(ifmw) == desc->ifmt_word) {
534 if (desc->ifmt_string != NULL)
535 printf(" mode %s", desc->ifmt_string);
540 * Check for the shared subtype descriptions first, then the
541 * type-specific ones.
543 for (desc = ifm_subtype_shared_descriptions;
544 desc->ifmt_string != NULL; desc++)
545 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
548 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
549 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
551 if (desc->ifmt_string == NULL) {
552 printf(" <unknown subtype>\n");
557 printf(" %s", desc->ifmt_string);
560 * Look for shared options.
562 for (desc = ifm_shared_option_descriptions;
563 desc->ifmt_string != NULL; desc++) {
564 if (ifmw & desc->ifmt_word) {
565 if (seen_option == 0)
567 printf("%s%s", seen_option++ ? "," : "",
573 * Look for subtype-specific options.
575 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
576 if (ifmw & desc->ifmt_word) {
577 if (seen_option == 0)
579 printf("%s%s", seen_option++ ? "," : "",
583 printf("%s\n", seen_option ? ">" : "");
585 #endif /* IFMEDIA_DEBUG */