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