Home | History | Annotate | Line # | Download | only in net
if_media.c revision 1.49
      1 /*	$NetBSD: if_media.c,v 1.49 2020/01/20 19:35:39 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  *
     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.49 2020/01/20 19:35:39 thorpej 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 *,
     93     struct ifmediareq *);
     94 static int	ifmedia_ioctl_locked(struct ifnet *, struct ifreq *,
     95     struct ifmedia *, u_long);
     96 
     97 /*
     98  * Compile-time options:
     99  * IFMEDIA_DEBUG:
    100  *	Turn on implementation-level debug printfs.
    101  * 	Useful for debugging newly-ported drivers.
    102  */
    103 
    104 #ifdef IFMEDIA_DEBUG
    105 int	ifmedia_debug = 0;
    106 static	void ifmedia_printword(int);
    107 #endif
    108 
    109 MALLOC_DEFINE(M_IFMEDIA, "ifmedia", "interface media state");
    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 = IFM_NONE;
    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 int
    128 ifmedia_change(struct ifmedia *ifm, struct ifnet *ifp)
    129 {
    130 
    131 	if (ifm->ifm_change == NULL)
    132 		return -1;
    133 	return (*ifm->ifm_change)(ifp);
    134 }
    135 
    136 static void
    137 ifmedia_status(struct ifmedia *ifm, struct ifnet *ifp, struct ifmediareq *ifmr)
    138 {
    139 
    140 	if (ifm->ifm_status == NULL)
    141 		return;
    142 	(*ifm->ifm_status)(ifp, ifmr);
    143 }
    144 
    145 /*
    146  * Add a media configuration to the list of supported media
    147  * for a specific interface instance.
    148  */
    149 void
    150 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
    151 {
    152 	struct ifmedia_entry *entry;
    153 
    154 #ifdef IFMEDIA_DEBUG
    155 	if (ifmedia_debug) {
    156 		if (ifm == NULL) {
    157 			printf("ifmedia_add: null ifm\n");
    158 			return;
    159 		}
    160 		printf("Adding entry for ");
    161 		ifmedia_printword(mword);
    162 	}
    163 #endif
    164 
    165 	entry = malloc(sizeof(*entry), M_IFMEDIA, M_WAITOK);
    166 	entry->ifm_media = mword;
    167 	entry->ifm_data = data;
    168 	entry->ifm_aux = aux;
    169 	TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list);
    170 }
    171 
    172 /*
    173  * Add an array of media configurations to the list of
    174  * supported media for a specific interface instance.
    175  */
    176 void
    177 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count)
    178 {
    179 	int i;
    180 
    181 	for (i = 0; i < count; i++)
    182 		ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
    183 		    lp[i].ifm_aux);
    184 }
    185 
    186 /*
    187  * Set the default active media.
    188  *
    189  * Called by device-specific code which is assumed to have already
    190  * selected the default media in hardware.  We do _not_ call the
    191  * media-change callback.
    192  */
    193 void
    194 ifmedia_set(struct ifmedia *ifm, int target)
    195 {
    196 	struct ifmedia_entry *match;
    197 
    198 	match = ifmedia_match(ifm, target, ifm->ifm_mask);
    199 
    200 	/*
    201 	 * If we didn't find the requested media, then we try to fall
    202 	 * back to target-type (IFM_ETHER, e.g.) | IFM_NONE.  If that's
    203 	 * not on the list, then we add it and set the media to it.
    204 	 *
    205 	 * Since ifmedia_set is almost always called with IFM_AUTO or
    206 	 * with a known-good media, this really should only occur if we:
    207 	 *
    208 	 * a) didn't find any PHYs, or
    209 	 * b) didn't find an autoselect option on the PHY when the
    210 	 *    parent ethernet driver expected to.
    211 	 *
    212 	 * In either case, it makes sense to select no media.
    213 	 */
    214 	if (match == NULL) {
    215 		printf("ifmedia_set: no match for 0x%x/0x%x\n",
    216 		    target, ~ifm->ifm_mask);
    217 		target = (target & IFM_NMASK) | IFM_NONE;
    218 		match = ifmedia_match(ifm, target, ifm->ifm_mask);
    219 		if (match == NULL) {
    220 			ifmedia_add(ifm, target, 0, NULL);
    221 			match = ifmedia_match(ifm, target, ifm->ifm_mask);
    222 			if (match == NULL)
    223 				panic("ifmedia_set failed");
    224 		}
    225 	}
    226 	ifm->ifm_cur = match;
    227 
    228 #ifdef IFMEDIA_DEBUG
    229 	if (ifmedia_debug) {
    230 		printf("ifmedia_set: target ");
    231 		ifmedia_printword(target);
    232 		printf("ifmedia_set: setting to ");
    233 		ifmedia_printword(ifm->ifm_cur->ifm_media);
    234 	}
    235 #endif
    236 }
    237 
    238 /*
    239  * Device-independent media ioctl support function.
    240  */
    241 static int
    242 ifmedia_ioctl_locked(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
    243     u_long cmd)
    244 {
    245 	struct ifmedia_entry *match;
    246 	struct ifmediareq *ifmr = (struct ifmediareq *)ifr;
    247 	int error = 0;
    248 
    249 	if (ifp == NULL || ifr == NULL || ifm == NULL)
    250 		return EINVAL;
    251 
    252 	switch (cmd) {
    253 	case SIOCSIFMEDIA:	/* Set the current media. */
    254 	{
    255 		struct ifmedia_entry *oldentry;
    256 		u_int oldmedia;
    257 		u_int newmedia = ifr->ifr_media;
    258 
    259 		match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
    260 		if (match == NULL) {
    261 #ifdef IFMEDIA_DEBUG
    262 			if (ifmedia_debug) {
    263 				printf("ifmedia_ioctl: no media found for "
    264 				    "0x%08x\n", newmedia);
    265 			}
    266 #endif
    267 			return EINVAL;
    268 		}
    269 
    270 		/*
    271 		 * If no change, we're done.
    272 		 * XXX Automedia may involve software intervention.
    273 		 *     Keep going in case the connected media changed.
    274 		 *     Similarly, if best match changed (kernel debugger?).
    275 		 */
    276 		if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
    277 		    (newmedia == ifm->ifm_media) && (match == ifm->ifm_cur))
    278 			return 0;
    279 
    280 		/*
    281 		 * We found a match, now make the driver switch to it.
    282 		 * Make sure to preserve our old media type in case the
    283 		 * driver can't switch.
    284 		 */
    285 #ifdef IFMEDIA_DEBUG
    286 		if (ifmedia_debug) {
    287 			printf("ifmedia_ioctl: switching %s to ",
    288 			    ifp->if_xname);
    289 			ifmedia_printword(match->ifm_media);
    290 		}
    291 #endif
    292 		oldentry = ifm->ifm_cur;
    293 		oldmedia = ifm->ifm_media;
    294 		ifm->ifm_cur = match;
    295 		ifm->ifm_media = newmedia;
    296 		error = ifmedia_change(ifm, ifp);
    297 		if (error) {
    298 			ifm->ifm_cur = oldentry;
    299 			ifm->ifm_media = oldmedia;
    300 		}
    301 		break;
    302 	}
    303 
    304 	/* Get list of available media and current media on interface. */
    305 	case SIOCGIFMEDIA:
    306 	{
    307 		struct ifmedia_entry *ep;
    308 		size_t nwords;
    309 
    310 		if (ifmr->ifm_count < 0)
    311 			return EINVAL;
    312 
    313 		ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
    314 		    ifm->ifm_cur->ifm_media : IFM_NONE;
    315 		ifmr->ifm_mask = ifm->ifm_mask;
    316 		ifmr->ifm_status = 0;
    317 		ifmedia_status(ifm, ifp, ifmr);
    318 
    319 		/*
    320 		 * Count them so we know a-priori how much is the max we'll
    321 		 * need.
    322 		 */
    323 		ep = TAILQ_FIRST(&ifm->ifm_list);
    324 		for (nwords = 0; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list))
    325 			nwords++;
    326 
    327 		if (ifmr->ifm_count != 0) {
    328 			size_t count;
    329 			size_t minwords = nwords > (size_t)ifmr->ifm_count
    330 			    ? (size_t)ifmr->ifm_count : nwords;
    331 			int *kptr = malloc(minwords * sizeof(int), M_TEMP,
    332 			    M_WAITOK);
    333 
    334 			/* Get the media words from the interface's list. */
    335 			ep = TAILQ_FIRST(&ifm->ifm_list);
    336 			for (count = 0; ep != NULL && count < minwords;
    337 			    ep = TAILQ_NEXT(ep, ifm_list), count++)
    338 				kptr[count] = ep->ifm_media;
    339 
    340 			error = copyout(kptr, ifmr->ifm_ulist,
    341 			    minwords * sizeof(int));
    342 			if (error == 0 && ep != NULL)
    343 				error = E2BIG;	/* oops! */
    344 			free(kptr, M_TEMP);
    345 		}
    346 		/* Update with the real number */
    347 		ifmr->ifm_count = nwords;
    348 		break;
    349 	}
    350 
    351 	default:
    352 		return EINVAL;
    353 	}
    354 
    355 	return error;
    356 }
    357 
    358 int
    359 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
    360     u_long cmd)
    361 {
    362 	int e;
    363 
    364 	/*
    365 	 * If if_is_mpsafe(ifp), KERNEL_LOCK isn't held here and
    366 	 * ipl will not have been raised (well, maybe it has, but
    367 	 * it doesn't matter), but ifmedia_ioctl_locked isn't MP-safe
    368 	 * yet, so we go to splnet and grab the KERNEL_LOCK.
    369 	 *
    370 	 * In the non-mpsafe case, the interface's ioctl routine
    371 	 * will already be running at splnet() and so raising it
    372 	 * again is redundant, but also harmless.
    373 	 */
    374 	int s = splnet();
    375 	KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
    376 	e = ifmedia_ioctl_locked(ifp, ifr, ifm, cmd);
    377 	KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
    378 	splx(s);
    379 
    380 	return e;
    381 }
    382 
    383 /*
    384  * Find media entry matching a given ifm word.
    385  */
    386 struct ifmedia_entry *
    387 ifmedia_match(struct ifmedia *ifm, u_int target, u_int mask)
    388 {
    389 	struct ifmedia_entry *match, *next;
    390 
    391 	match = NULL;
    392 	mask = ~mask;
    393 
    394 	TAILQ_FOREACH(next, &ifm->ifm_list, ifm_list) {
    395 		if ((next->ifm_media & mask) == (target & mask)) {
    396 			if (match) {
    397 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
    398 				printf("ifmedia_match: multiple match for "
    399 				    "0x%x/0x%x, selected instance %d\n",
    400 				    target, mask, IFM_INST(match->ifm_media));
    401 #endif
    402 				break;
    403 			}
    404 			match = next;
    405 		}
    406 	}
    407 
    408 	return match;
    409 }
    410 
    411 /*
    412  * Delete all media for a given instance.
    413  */
    414 void
    415 ifmedia_delete_instance(struct ifmedia *ifm, u_int inst)
    416 {
    417 	struct ifmedia_entry *ife, *nife;
    418 
    419 	TAILQ_FOREACH_SAFE(ife, &ifm->ifm_list, ifm_list, nife) {
    420 		if (inst == IFM_INST_ANY ||
    421 		    inst == IFM_INST(ife->ifm_media)) {
    422 			TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
    423 			free(ife, M_IFMEDIA);
    424 		}
    425 	}
    426 	if (inst == IFM_INST_ANY) {
    427 		ifm->ifm_cur = NULL;
    428 		ifm->ifm_media = IFM_NONE;
    429 	}
    430 }
    431 
    432 void
    433 ifmedia_removeall(struct ifmedia *ifm)
    434 {
    435 
    436 	ifmedia_delete_instance(ifm, IFM_INST_ANY);
    437 }
    438 
    439 
    440 /*
    441  * Compute the interface `baudrate' from the media, for the interface
    442  * metrics (used by routing daemons).
    443  */
    444 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
    445     IFM_BAUDRATE_DESCRIPTIONS;
    446 
    447 uint64_t
    448 ifmedia_baudrate(int mword)
    449 {
    450 	int i;
    451 
    452 	for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
    453 		if (IFM_TYPE_SUBTYPE_MATCH(mword,
    454 		    ifmedia_baudrate_descriptions[i].ifmb_word))
    455 			return ifmedia_baudrate_descriptions[i].ifmb_baudrate;
    456 	}
    457 
    458 	/* Not known. */
    459 	return 0;
    460 }
    461 
    462 #ifdef IFMEDIA_DEBUG
    463 
    464 static const struct ifmedia_description ifm_type_descriptions[] =
    465     IFM_TYPE_DESCRIPTIONS;
    466 
    467 static const struct ifmedia_description ifm_subtype_descriptions[] =
    468     IFM_SUBTYPE_DESCRIPTIONS;
    469 
    470 static const struct ifmedia_description ifm_option_descriptions[] =
    471     IFM_OPTION_DESCRIPTIONS;
    472 
    473 /*
    474  * print a media word.
    475  */
    476 static void
    477 ifmedia_printword(int ifmw)
    478 {
    479 	const struct ifmedia_description *desc;
    480 	int seen_option = 0;
    481 
    482 	/* Print the top-level interface type. */
    483 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
    484 	     desc++) {
    485 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
    486 			break;
    487 	}
    488 	if (desc->ifmt_string == NULL)
    489 		printf("<unknown type> ");
    490 	else
    491 		printf("%s ", desc->ifmt_string);
    492 
    493 	/* Print the subtype. */
    494 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
    495 	     desc++) {
    496 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
    497 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
    498 			break;
    499 	}
    500 	if (desc->ifmt_string == NULL)
    501 		printf("<unknown subtype>");
    502 	else
    503 		printf("%s", desc->ifmt_string);
    504 
    505 	/* Print any options. */
    506 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
    507 	     desc++) {
    508 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
    509 		    (ifmw & desc->ifmt_word) != 0 &&
    510 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
    511 			if (seen_option == 0)
    512 				printf(" <");
    513 			printf("%s%s", seen_option ? "," : "",
    514 			    desc->ifmt_string);
    515 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
    516 		}
    517 	}
    518 	printf("%s\n", seen_option ? ">" : "");
    519 }
    520 
    521 #endif /* IFMEDIA_DEBUG */
    522