Home | History | Annotate | Line # | Download | only in net
if_media.c revision 1.22
      1 /*	$NetBSD: if_media.c,v 1.22 2004/04/09 20:44:57 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     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 the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1997
     42  *	Jonathan Stone and Jason R. Thorpe.  All rights reserved.
     43  *
     44  * This software is derived from information provided by Matt Thomas.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. All advertising materials mentioning features or use of this software
     55  *    must display the following acknowledgement:
     56  *      This product includes software developed by Jonathan Stone
     57  *	and Jason R. Thorpe for the NetBSD Project.
     58  * 4. The names of the authors may not be used to endorse or promote products
     59  *    derived from this software without specific prior written permission.
     60  *
     61  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     62  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     63  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     64  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     65  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     66  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     67  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     68  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     69  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71  * SUCH DAMAGE.
     72  */
     73 
     74 /*
     75  * BSD/OS-compatible network interface media selection.
     76  *
     77  * Where it is safe to do so, this code strays slightly from the BSD/OS
     78  * design.  Software which uses the API (device drivers, basically)
     79  * shouldn't notice any difference.
     80  *
     81  * Many thanks to Matt Thomas for providing the information necessary
     82  * to implement this interface.
     83  */
     84 
     85 #include <sys/cdefs.h>
     86 __KERNEL_RCSID(0, "$NetBSD: if_media.c,v 1.22 2004/04/09 20:44:57 thorpej Exp $");
     87 
     88 #include <sys/param.h>
     89 #include <sys/systm.h>
     90 #include <sys/errno.h>
     91 #include <sys/ioctl.h>
     92 #include <sys/socket.h>
     93 #include <sys/malloc.h>
     94 
     95 #include <net/if.h>
     96 #include <net/if_media.h>
     97 #include <net/netisr.h>
     98 
     99 /*
    100  * Compile-time options:
    101  * IFMEDIA_DEBUG:
    102  *	turn on implementation-level debug printfs.
    103  * 	Useful for debugging newly-ported  drivers.
    104  */
    105 
    106 #ifdef IFMEDIA_DEBUG
    107 int	ifmedia_debug = 0;
    108 static	void ifmedia_printword(int);
    109 #endif
    110 
    111 /*
    112  * Initialize if_media struct for a specific interface instance.
    113  */
    114 void
    115 ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
    116     ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
    117 {
    118 
    119 	TAILQ_INIT(&ifm->ifm_list);
    120 	ifm->ifm_cur = NULL;
    121 	ifm->ifm_media = 0;
    122 	ifm->ifm_mask = dontcare_mask;		/* IF don't-care bits */
    123 	ifm->ifm_change = change_callback;
    124 	ifm->ifm_status = status_callback;
    125 }
    126 
    127 /*
    128  * Add a media configuration to the list of supported media
    129  * for a specific interface instance.
    130  */
    131 void
    132 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
    133 {
    134 	struct ifmedia_entry *entry;
    135 
    136 #ifdef IFMEDIA_DEBUG
    137 	if (ifmedia_debug) {
    138 		if (ifm == NULL) {
    139 			printf("ifmedia_add: null ifm\n");
    140 			return;
    141 		}
    142 		printf("Adding entry for ");
    143 		ifmedia_printword(mword);
    144 	}
    145 #endif
    146 
    147 	entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
    148 	if (entry == NULL)
    149 		panic("ifmedia_add: can't malloc entry");
    150 
    151 	entry->ifm_media = mword;
    152 	entry->ifm_data = data;
    153 	entry->ifm_aux = aux;
    154 
    155 	TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list);
    156 }
    157 
    158 /*
    159  * Add an array of media configurations to the list of
    160  * supported media for a specific interface instance.
    161  */
    162 void
    163 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count)
    164 {
    165 	int i;
    166 
    167 	for (i = 0; i < count; i++)
    168 		ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
    169 		    lp[i].ifm_aux);
    170 }
    171 
    172 /*
    173  * Set the default active media.
    174  *
    175  * Called by device-specific code which is assumed to have already
    176  * selected the default media in hardware.  We do _not_ call the
    177  * media-change callback.
    178  */
    179 void
    180 ifmedia_set(struct ifmedia *ifm, int target)
    181 {
    182 	struct ifmedia_entry *match;
    183 
    184 	match = ifmedia_match(ifm, target, ifm->ifm_mask);
    185 
    186 	/*
    187 	 * If we didn't find the requested media, then we try to fall
    188 	 * back to target-type (IFM_ETHER, e.g.) | IFM_NONE.  If that's
    189 	 * not on the list, then we add it and set the media to it.
    190 	 *
    191 	 * Since ifmedia_set is almost always called with IFM_AUTO or
    192 	 * with a known-good media, this really should only occur if we:
    193 	 *
    194 	 * a) didn't find any PHYs, or
    195 	 * b) didn't find an autoselect option on the PHY when the
    196 	 *    parent ethernet driver expected to.
    197 	 *
    198 	 * In either case, it makes sense to select no media.
    199 	 */
    200 	if (match == NULL) {
    201 		printf("ifmedia_set: no match for 0x%x/0x%x\n",
    202 		    target, ~ifm->ifm_mask);
    203 		target = (target & IFM_NMASK) | IFM_NONE;
    204 		match = ifmedia_match(ifm, target, ifm->ifm_mask);
    205 		if (match == NULL) {
    206 			ifmedia_add(ifm, target, 0, NULL);
    207 			match = ifmedia_match(ifm, target, ifm->ifm_mask);
    208 			if (match == NULL) {
    209 				panic("ifmedia_set failed");
    210 			}
    211 		}
    212 	}
    213 	ifm->ifm_cur = match;
    214 
    215 #ifdef IFMEDIA_DEBUG
    216 	if (ifmedia_debug) {
    217 		printf("ifmedia_set: target ");
    218 		ifmedia_printword(target);
    219 		printf("ifmedia_set: setting to ");
    220 		ifmedia_printword(ifm->ifm_cur->ifm_media);
    221 	}
    222 #endif
    223 }
    224 
    225 /*
    226  * Device-independent media ioctl support function.
    227  */
    228 int
    229 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
    230     u_long cmd)
    231 {
    232 	struct ifmedia_entry *match;
    233 	struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
    234 	int error = 0;
    235 
    236 	if (ifp == NULL || ifr == NULL || ifm == NULL)
    237 		return (EINVAL);
    238 
    239 	switch (cmd) {
    240 
    241 	/*
    242 	 * Set the current media.
    243 	 */
    244 	case SIOCSIFMEDIA:
    245 	{
    246 		struct ifmedia_entry *oldentry;
    247 		u_int oldmedia;
    248 		u_int newmedia = ifr->ifr_media;
    249 
    250 		match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
    251 		if (match == NULL) {
    252 #ifdef IFMEDIA_DEBUG
    253 			if (ifmedia_debug) {
    254 				printf(
    255 				    "ifmedia_ioctl: no media found for 0x%x\n",
    256 				    newmedia);
    257 			}
    258 #endif
    259 			return (EINVAL);
    260 		}
    261 
    262 		/*
    263 		 * If no change, we're done.
    264 		 * XXX Automedia may involve software intervention.
    265 		 *     Keep going in case the connected media changed.
    266 		 *     Similarly, if best match changed (kernel debugger?).
    267 		 */
    268 		if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
    269 		    (newmedia == ifm->ifm_media) &&
    270 		    (match == ifm->ifm_cur))
    271 			return 0;
    272 
    273 		/*
    274 		 * We found a match, now make the driver switch to it.
    275 		 * Make sure to preserve our old media type in case the
    276 		 * driver can't switch.
    277 		 */
    278 #ifdef IFMEDIA_DEBUG
    279 		if (ifmedia_debug) {
    280 			printf("ifmedia_ioctl: switching %s to ",
    281 			    ifp->if_xname);
    282 			ifmedia_printword(match->ifm_media);
    283 		}
    284 #endif
    285 		oldentry = ifm->ifm_cur;
    286 		oldmedia = ifm->ifm_media;
    287 		ifm->ifm_cur = match;
    288 		ifm->ifm_media = newmedia;
    289 		error = (*ifm->ifm_change)(ifp);
    290 		if (error) {
    291 			ifm->ifm_cur = oldentry;
    292 			ifm->ifm_media = oldmedia;
    293 		}
    294 		break;
    295 	}
    296 
    297 	/*
    298 	 * Get list of available media and current media on interface.
    299 	 */
    300 	case SIOCGIFMEDIA:
    301 	{
    302 		struct ifmedia_entry *ep;
    303 		size_t nwords;
    304 
    305 		if (ifmr->ifm_count < 0)
    306 			return EINVAL;
    307 
    308 		ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
    309 		    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);
    313 
    314 		/*
    315 		 * Count them so we know a-priori how much is the max we'll
    316 		 * need.
    317 		 */
    318 		ep = TAILQ_FIRST(&ifm->ifm_list);
    319 		for (nwords = 0; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list))
    320 			nwords++;
    321 
    322 		if (ifmr->ifm_count != 0) {
    323 			size_t count;
    324 			size_t minwords = nwords > (size_t)ifmr->ifm_count
    325 			    ? (size_t)ifmr->ifm_count
    326 			    : nwords;
    327 			int *kptr = (int *)malloc(minwords * sizeof(int),
    328 			    M_TEMP, M_WAITOK);
    329 			/*
    330 			 * Get the media words from the interface's list.
    331 			 */
    332 			ep = TAILQ_FIRST(&ifm->ifm_list);
    333 			for (count = 0; ep != NULL && count < minwords;
    334 			    ep = TAILQ_NEXT(ep, ifm_list), count++)
    335 				kptr[count] = ep->ifm_media;
    336 
    337 			error = copyout(kptr, ifmr->ifm_ulist,
    338 			    minwords * sizeof(int));
    339 			if (error == 0 && ep != NULL)
    340 				error = E2BIG;	/* oops! */
    341 			free(kptr, M_TEMP);
    342 		}
    343 		ifmr->ifm_count = nwords;
    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 struct ifmedia_entry *
    358 ifmedia_match(struct ifmedia *ifm, u_int target, u_int mask)
    359 {
    360 	struct ifmedia_entry *match, *next;
    361 
    362 	match = NULL;
    363 	mask = ~mask;
    364 
    365 	for (next = TAILQ_FIRST(&ifm->ifm_list); next != NULL;
    366 	     next = TAILQ_NEXT(next, ifm_list)) {
    367 		if ((next->ifm_media & mask) == (target & mask)) {
    368 			if (match) {
    369 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
    370 				printf("ifmedia_match: multiple match for "
    371 				    "0x%x/0x%x, selected instance %d\n",
    372 				    target, mask, IFM_INST(match->ifm_media));
    373 #endif
    374 				break;
    375 			}
    376 			match = next;
    377 		}
    378 	}
    379 
    380 	return match;
    381 }
    382 
    383 /*
    384  * Delete all media for a given instance.
    385  */
    386 void
    387 ifmedia_delete_instance(struct ifmedia *ifm, u_int inst)
    388 {
    389 	struct ifmedia_entry *ife, *nife;
    390 
    391 	for (ife = TAILQ_FIRST(&ifm->ifm_list); ife != NULL;
    392 	     ife = nife) {
    393 		nife = TAILQ_NEXT(ife, ifm_list);
    394 		if (inst == IFM_INST_ANY ||
    395 		    inst == IFM_INST(ife->ifm_media)) {
    396 			TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
    397 			free(ife, M_DEVBUF);
    398 		}
    399 	}
    400 }
    401 
    402 /*
    403  * Compute the interface `baudrate' from the media, for the interface
    404  * metrics (used by routing daemons).
    405  */
    406 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
    407     IFM_BAUDRATE_DESCRIPTIONS;
    408 
    409 u_quad_t
    410 ifmedia_baudrate(int mword)
    411 {
    412 	int i;
    413 
    414 	for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
    415 		if ((mword & (IFM_NMASK|IFM_TMASK)) ==
    416 		    ifmedia_baudrate_descriptions[i].ifmb_word)
    417 			return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
    418 	}
    419 
    420 	/* Not known. */
    421 	return (0);
    422 }
    423 
    424 #ifdef IFMEDIA_DEBUG
    425 
    426 static const struct ifmedia_description ifm_type_descriptions[] =
    427     IFM_TYPE_DESCRIPTIONS;
    428 
    429 static const struct ifmedia_description ifm_subtype_descriptions[] =
    430     IFM_SUBTYPE_DESCRIPTIONS;
    431 
    432 static const struct ifmedia_description ifm_option_descriptions[] =
    433     IFM_OPTION_DESCRIPTIONS;
    434 
    435 /*
    436  * print a media word.
    437  */
    438 static void
    439 ifmedia_printword(int ifmw)
    440 {
    441 	const struct ifmedia_description *desc;
    442 	int seen_option = 0;
    443 
    444 	/* Print the top-level interface type. */
    445 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
    446 	     desc++) {
    447 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
    448 			break;
    449 	}
    450 	if (desc->ifmt_string == NULL)
    451 		printf("<unknown type> ");
    452 	else
    453 		printf("%s ", desc->ifmt_string);
    454 
    455 	/* Print the subtype. */
    456 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
    457 	     desc++) {
    458 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
    459 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
    460 			break;
    461 	}
    462 	if (desc->ifmt_string == NULL)
    463 		printf("<unknown subtype>");
    464 	else
    465 		printf("%s", desc->ifmt_string);
    466 
    467 	/* Print any options. */
    468 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
    469 	     desc++) {
    470 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
    471 		    (ifmw & desc->ifmt_word) != 0 &&
    472 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
    473 			if (seen_option == 0)
    474 				printf(" <");
    475 			printf("%s%s", seen_option ? "," : "",
    476 			    desc->ifmt_string);
    477 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
    478 		}
    479 	}
    480 	printf("%s\n", seen_option ? ">" : "");
    481 }
    482 
    483 #endif /* IFMEDIA_DEBUG */
    484