The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/if_media.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $     */
    2 /* $FreeBSD: releng/6.1/sys/net/if_media.c 156816 2006-03-17 20:17:43Z glebius $ */
    3 
    4 /*-
    5  * Copyright (c) 1997
    6  *      Jonathan Stone and Jason R. Thorpe.  All rights reserved.
    7  *
    8  * This software is derived from information provided by Matt Thomas.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   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.
   24  *
   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
   35  * SUCH DAMAGE.
   36  */
   37 
   38 /*
   39  * BSD/OS-compatible network interface media selection.
   40  *
   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.
   44  *
   45  * Many thanks to Matt Thomas for providing the information necessary
   46  * to implement this interface.
   47  */
   48 
   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 
   55 #include <net/if.h>
   56 #include <net/if_media.h>
   57 
   58 /*
   59  * Compile-time options:
   60  * IFMEDIA_DEBUG:
   61  *      turn on implementation-level debug printfs.
   62  *      Useful for debugging newly-ported  drivers.
   63  */
   64 
   65 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
   66     int flags, int mask);
   67 
   68 #ifdef IFMEDIA_DEBUG
   69 int     ifmedia_debug = 0;
   70 static  void ifmedia_printword(int);
   71 #endif
   72 
   73 /*
   74  * Initialize if_media struct for a specific interface instance.
   75  */
   76 void
   77 ifmedia_init(ifm, dontcare_mask, change_callback, status_callback)
   78         struct ifmedia *ifm;
   79         int dontcare_mask;
   80         ifm_change_cb_t change_callback;
   81         ifm_stat_cb_t status_callback;
   82 {
   83 
   84         LIST_INIT(&ifm->ifm_list);
   85         ifm->ifm_cur = NULL;
   86         ifm->ifm_media = 0;
   87         ifm->ifm_mask = dontcare_mask;          /* IF don't-care bits */
   88         ifm->ifm_change = change_callback;
   89         ifm->ifm_status = status_callback;
   90 }
   91 
   92 void
   93 ifmedia_removeall(ifm)
   94         struct ifmedia *ifm;
   95 {
   96         struct ifmedia_entry *entry;
   97 
   98         for (entry = LIST_FIRST(&ifm->ifm_list); entry;
   99              entry = LIST_FIRST(&ifm->ifm_list)) {
  100                 LIST_REMOVE(entry, ifm_list);
  101                 free(entry, M_IFADDR);
  102         }
  103 }
  104 
  105 /*
  106  * Add a media configuration to the list of supported media
  107  * for a specific interface instance.
  108  */
  109 void
  110 ifmedia_add(ifm, mword, data, aux)
  111         struct ifmedia *ifm;
  112         int mword;
  113         int data;
  114         void *aux;
  115 {
  116         register struct ifmedia_entry *entry;
  117 
  118 #ifdef IFMEDIA_DEBUG
  119         if (ifmedia_debug) {
  120                 if (ifm == NULL) {
  121                         printf("ifmedia_add: null ifm\n");
  122                         return;
  123                 }
  124                 printf("Adding entry for ");
  125                 ifmedia_printword(mword);
  126         }
  127 #endif
  128 
  129         entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
  130         if (entry == NULL)
  131                 panic("ifmedia_add: can't malloc entry");
  132 
  133         entry->ifm_media = mword;
  134         entry->ifm_data = data;
  135         entry->ifm_aux = aux;
  136 
  137         LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
  138 }
  139 
  140 /*
  141  * Add an array of media configurations to the list of
  142  * supported media for a specific interface instance.
  143  */
  144 void
  145 ifmedia_list_add(ifm, lp, count)
  146         struct ifmedia *ifm;
  147         struct ifmedia_entry *lp;
  148         int count;
  149 {
  150         int i;
  151 
  152         for (i = 0; i < count; i++)
  153                 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
  154                     lp[i].ifm_aux);
  155 }
  156 
  157 /*
  158  * Set the default active media. 
  159  *
  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.
  163  */
  164 void
  165 ifmedia_set(ifm, target)
  166         struct ifmedia *ifm; 
  167         int target;
  168 
  169 {
  170         struct ifmedia_entry *match;
  171 
  172         match = ifmedia_match(ifm, target, ifm->ifm_mask);
  173 
  174         if (match == NULL) {
  175                 printf("ifmedia_set: no match for 0x%x/0x%x\n",
  176                     target, ~ifm->ifm_mask);
  177                 panic("ifmedia_set");
  178         }
  179         ifm->ifm_cur = match;
  180 
  181 #ifdef IFMEDIA_DEBUG
  182         if (ifmedia_debug) {
  183                 printf("ifmedia_set: target ");
  184                 ifmedia_printword(target);
  185                 printf("ifmedia_set: setting to ");
  186                 ifmedia_printword(ifm->ifm_cur->ifm_media);
  187         }
  188 #endif
  189 }
  190 
  191 /*
  192  * Device-independent media ioctl support function.
  193  */
  194 int
  195 ifmedia_ioctl(ifp, ifr, ifm, cmd)
  196         struct ifnet *ifp;
  197         struct ifreq *ifr;
  198         struct ifmedia *ifm;
  199         u_long cmd;
  200 {
  201         struct ifmedia_entry *match;
  202         struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
  203         int error = 0, sticky;
  204 
  205         if (ifp == NULL || ifr == NULL || ifm == NULL)
  206                 return(EINVAL);
  207 
  208         switch (cmd) {
  209 
  210         /*
  211          * Set the current media.
  212          */
  213         case  SIOCSIFMEDIA:
  214         {
  215                 struct ifmedia_entry *oldentry;
  216                 int oldmedia;
  217                 int newmedia = ifr->ifr_media;
  218 
  219                 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
  220                 if (match == NULL) {
  221 #ifdef IFMEDIA_DEBUG
  222                         if (ifmedia_debug) {
  223                                 printf(
  224                                     "ifmedia_ioctl: no media found for 0x%x\n", 
  225                                     newmedia);
  226                         }
  227 #endif
  228                         return (ENXIO);
  229                 }
  230 
  231                 /*
  232                  * If no change, we're done.
  233                  * XXX Automedia may invole software intervention.
  234                  *     Keep going in case the the connected media changed.
  235                  *     Similarly, if best match changed (kernel debugger?).
  236                  */
  237                 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
  238                     (newmedia == ifm->ifm_media) &&
  239                     (match == ifm->ifm_cur))
  240                         return 0;
  241 
  242                 /*
  243                  * We found a match, now make the driver switch to it.
  244                  * Make sure to preserve our old media type in case the
  245                  * driver can't switch.
  246                  */
  247 #ifdef IFMEDIA_DEBUG
  248                 if (ifmedia_debug) {
  249                         printf("ifmedia_ioctl: switching %s to ",
  250                             ifp->if_xname);
  251                         ifmedia_printword(match->ifm_media);
  252                 }
  253 #endif
  254                 oldentry = ifm->ifm_cur;
  255                 oldmedia = ifm->ifm_media;
  256                 ifm->ifm_cur = match;
  257                 ifm->ifm_media = newmedia;
  258                 error = (*ifm->ifm_change)(ifp);
  259                 if (error) {
  260                         ifm->ifm_cur = oldentry;
  261                         ifm->ifm_media = oldmedia;
  262                 }
  263                 break;
  264         }
  265 
  266         /*
  267          * Get list of available media and current media on interface.
  268          */
  269         case  SIOCGIFMEDIA: 
  270         {
  271                 struct ifmedia_entry *ep;
  272                 int *kptr, count;
  273                 int usermax;    /* user requested max */
  274 
  275                 kptr = NULL;            /* XXX gcc */
  276 
  277                 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
  278                     ifm->ifm_cur->ifm_media : IFM_NONE;
  279                 ifmr->ifm_mask = ifm->ifm_mask;
  280                 ifmr->ifm_status = 0;
  281                 (*ifm->ifm_status)(ifp, ifmr);
  282 
  283                 count = 0;
  284                 usermax = 0;
  285 
  286                 /*
  287                  * If there are more interfaces on the list, count
  288                  * them.  This allows the caller to set ifmr->ifm_count
  289                  * to 0 on the first call to know how much space to
  290                  * allocate.
  291                  */
  292                 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list)
  293                         usermax++;
  294 
  295                 /*
  296                  * Don't allow the user to ask for too many
  297                  * or a negative number.
  298                  */
  299                 if (ifmr->ifm_count > usermax)
  300                         ifmr->ifm_count = usermax;
  301                 else if (ifmr->ifm_count < 0)
  302                         return (EINVAL);
  303 
  304                 if (ifmr->ifm_count != 0) {
  305                         kptr = (int *)malloc(ifmr->ifm_count * sizeof(int),
  306                             M_TEMP, M_NOWAIT);
  307 
  308                         if (kptr == NULL)
  309                                 return (ENOMEM);
  310                         /*
  311                          * Get the media words from the interface's list.
  312                          */
  313                         ep = LIST_FIRST(&ifm->ifm_list);
  314                         for (; ep != NULL && count < ifmr->ifm_count;
  315                             ep = LIST_NEXT(ep, ifm_list), count++)
  316                                 kptr[count] = ep->ifm_media;
  317 
  318                         if (ep != NULL)
  319                                 error = E2BIG;  /* oops! */
  320                 } else {
  321                         count = usermax;
  322                 }
  323 
  324                 /*
  325                  * We do the copyout on E2BIG, because that's
  326                  * just our way of telling userland that there
  327                  * are more.  This is the behavior I've observed
  328                  * under BSD/OS 3.0
  329                  */
  330                 sticky = error;
  331                 if ((error == 0 || error == E2BIG) && ifmr->ifm_count != 0) {
  332                         error = copyout((caddr_t)kptr,
  333                             (caddr_t)ifmr->ifm_ulist,
  334                             ifmr->ifm_count * sizeof(int));
  335                 }
  336 
  337                 if (error == 0)
  338                         error = sticky;
  339 
  340                 if (ifmr->ifm_count != 0)
  341                         free(kptr, M_TEMP);
  342 
  343                 ifmr->ifm_count = count;
  344                 break;
  345         }
  346 
  347         default:
  348                 return (EINVAL);
  349         }
  350 
  351         return (error);
  352 }
  353 
  354 /*
  355  * Find media entry matching a given ifm word.
  356  *
  357  */
  358 static struct ifmedia_entry *
  359 ifmedia_match(ifm, target, mask)
  360         struct ifmedia *ifm; 
  361         int target;
  362         int mask;
  363 {
  364         struct ifmedia_entry *match, *next;
  365 
  366         match = NULL;
  367         mask = ~mask;
  368 
  369         LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
  370                 if ((next->ifm_media & mask) == (target & mask)) {
  371 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
  372                         if (match) {
  373                                 printf("ifmedia_match: multiple match for "
  374                                     "0x%x/0x%x\n", target, mask);
  375                         }
  376 #endif
  377                         match = next;
  378                 }
  379         }
  380 
  381         return match;
  382 }
  383 
  384 /*
  385  * Compute the interface `baudrate' from the media, for the interface
  386  * metrics (used by routing daemons).
  387  */
  388 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =   
  389     IFM_BAUDRATE_DESCRIPTIONS;
  390 
  391 uint64_t
  392 ifmedia_baudrate(int mword)
  393 {
  394         int i;
  395 
  396         for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
  397                 if ((mword & (IFM_NMASK|IFM_TMASK)) ==
  398                     ifmedia_baudrate_descriptions[i].ifmb_word)
  399                         return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
  400         }
  401 
  402         /* Not known. */
  403         return (0);
  404 }
  405  
  406 #ifdef IFMEDIA_DEBUG
  407 struct ifmedia_description ifm_type_descriptions[] =
  408     IFM_TYPE_DESCRIPTIONS;
  409 
  410 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
  411     IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
  412 
  413 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
  414     IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
  415 
  416 struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
  417     IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
  418 
  419 struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
  420     IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
  421 
  422 struct ifmedia_description ifm_subtype_fddi_descriptions[] =
  423     IFM_SUBTYPE_FDDI_DESCRIPTIONS;
  424 
  425 struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
  426     IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
  427 
  428 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
  429     IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
  430 
  431 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
  432     IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
  433 
  434 struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] =
  435     IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
  436 
  437 struct ifmedia_description ifm_subtype_atm_descriptions[] =
  438     IFM_SUBTYPE_ATM_DESCRIPTIONS;
  439 
  440 struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
  441     IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
  442 
  443 struct ifmedia_description ifm_subtype_shared_descriptions[] =
  444     IFM_SUBTYPE_SHARED_DESCRIPTIONS;
  445 
  446 struct ifmedia_description ifm_shared_option_descriptions[] =
  447     IFM_SHARED_OPTION_DESCRIPTIONS;
  448 
  449 struct ifmedia_type_to_subtype {
  450         struct ifmedia_description *subtypes;
  451         struct ifmedia_description *options;
  452         struct ifmedia_description *modes;
  453 };
  454 
  455 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
  456 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
  457         {
  458           &ifm_subtype_ethernet_descriptions[0],
  459           &ifm_subtype_ethernet_option_descriptions[0],
  460           NULL,
  461         },
  462         {
  463           &ifm_subtype_tokenring_descriptions[0],
  464           &ifm_subtype_tokenring_option_descriptions[0],
  465           NULL,
  466         },
  467         {
  468           &ifm_subtype_fddi_descriptions[0],
  469           &ifm_subtype_fddi_option_descriptions[0],
  470           NULL,
  471         },
  472         {
  473           &ifm_subtype_ieee80211_descriptions[0],
  474           &ifm_subtype_ieee80211_option_descriptions[0],
  475           &ifm_subtype_ieee80211_mode_descriptions[0]
  476         },
  477         {
  478           &ifm_subtype_atm_descriptions[0],
  479           &ifm_subtype_atm_option_descriptions[0],
  480           NULL,
  481         },
  482 };
  483 
  484 /*
  485  * print a media word.
  486  */
  487 static void
  488 ifmedia_printword(ifmw)
  489         int ifmw;
  490 {
  491         struct ifmedia_description *desc;
  492         struct ifmedia_type_to_subtype *ttos;
  493         int seen_option = 0;
  494 
  495         /* Find the top-level interface type. */
  496         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
  497             desc->ifmt_string != NULL; desc++, ttos++)
  498                 if (IFM_TYPE(ifmw) == desc->ifmt_word)
  499                         break;
  500         if (desc->ifmt_string == NULL) {
  501                 printf("<unknown type>\n");
  502                 return;
  503         }
  504         printf(desc->ifmt_string);
  505 
  506         /* Any mode. */
  507         for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++)
  508                 if (IFM_MODE(ifmw) == desc->ifmt_word) {
  509                         if (desc->ifmt_string != NULL)
  510                                 printf(" mode %s", desc->ifmt_string);
  511                         break;
  512                 }
  513 
  514         /*
  515          * Check for the shared subtype descriptions first, then the
  516          * type-specific ones.
  517          */
  518         for (desc = ifm_subtype_shared_descriptions;
  519             desc->ifmt_string != NULL; desc++)
  520                 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
  521                         goto got_subtype;
  522 
  523         for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
  524                 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
  525                         break;
  526         if (desc->ifmt_string == NULL) {
  527                 printf(" <unknown subtype>\n");
  528                 return;
  529         }
  530 
  531  got_subtype:
  532         printf(" %s", desc->ifmt_string);
  533 
  534         /*
  535          * Look for shared options.
  536          */
  537         for (desc = ifm_shared_option_descriptions;
  538             desc->ifmt_string != NULL; desc++) {
  539                 if (ifmw & desc->ifmt_word) {
  540                         if (seen_option == 0)
  541                                 printf(" <");
  542                         printf("%s%s", seen_option++ ? "," : "",
  543                             desc->ifmt_string);
  544                 }
  545         }
  546 
  547         /*
  548          * Look for subtype-specific options.
  549          */
  550         for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
  551                 if (ifmw & desc->ifmt_word) {
  552                         if (seen_option == 0)
  553                                 printf(" <");
  554                         printf("%s%s", seen_option++ ? "," : "",
  555                             desc->ifmt_string); 
  556                 }
  557         }
  558         printf("%s\n", seen_option ? ">" : "");
  559 }
  560 #endif /* IFMEDIA_DEBUG */

Cache object: f1840184c165be1ffff7ee0e0dbac6d1


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.