xref: /freebsd/sys/net/if_media.c (revision 68e73f03655c308442a8354ee2c040df23f234b6)
1 /*	$NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 1997
7  *	Jonathan Stone and Jason R. Thorpe.  All rights reserved.
8  *
9  * This software is derived from information provided by Matt Thomas.
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 Jonathan Stone
22  *	and Jason R. Thorpe for the NetBSD Project.
23  * 4. The names of the authors may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 /*
40  * BSD/OS-compatible network interface media selection.
41  *
42  * Where it is safe to do so, this code strays slightly from the BSD/OS
43  * design.  Software which uses the API (device drivers, basically)
44  * shouldn't notice any difference.
45  *
46  * Many thanks to Matt Thomas for providing the information necessary
47  * to implement this interface.
48  */
49 
50 #include <sys/cdefs.h>
51 #include "opt_ifmedia.h"
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/malloc.h>
58 #include <sys/module.h>
59 #include <sys/sysctl.h>
60 
61 #include <net/if.h>
62 #include <net/if_var.h> /* for if_printf() */
63 #include <net/if_media.h>
64 
65 /*
66  * Compile-time options:
67  * IFMEDIA_DEBUG:
68  *	turn on implementation-level debug printfs.
69  * 	Useful for debugging newly-ported  drivers.
70  */
71 
72 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
73     int flags, int mask);
74 
75 #ifdef IFMEDIA_DEBUG
76 #include <net/if_var.h>
77 #include <net/if_private.h>
78 int	ifmedia_debug = 0;
79 SYSCTL_INT(_debug, OID_AUTO, ifmedia, CTLFLAG_RW, &ifmedia_debug,
80 	    0, "if_media debugging msgs");
81 static	void ifmedia_printword(int);
82 #endif
83 
84 /*
85  * Initialize if_media struct for a specific interface instance.
86  */
87 void
ifmedia_init(struct ifmedia * ifm,int dontcare_mask,ifm_change_cb_t change_callback,ifm_stat_cb_t status_callback)88 ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
89     ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
90 {
91 
92 	LIST_INIT(&ifm->ifm_list);
93 	ifm->ifm_cur = NULL;
94 	ifm->ifm_media = 0;
95 	ifm->ifm_mask = dontcare_mask;		/* IF don't-care bits */
96 	ifm->ifm_change = change_callback;
97 	ifm->ifm_status = status_callback;
98 }
99 
100 void
ifmedia_removeall(struct ifmedia * ifm)101 ifmedia_removeall(struct ifmedia *ifm)
102 {
103 	struct ifmedia_entry *entry;
104 
105 	while ((entry = LIST_FIRST(&ifm->ifm_list)) != NULL) {
106 		LIST_REMOVE(entry, ifm_list);
107 		free(entry, M_IFADDR);
108 	}
109 	ifm->ifm_cur = NULL;
110 }
111 
112 /*
113  * Add a media configuration to the list of supported media
114  * for a specific interface instance.
115  */
116 void
ifmedia_add(struct ifmedia * ifm,int mword,int data,void * aux)117 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
118 {
119 	struct ifmedia_entry *entry;
120 
121 #ifdef IFMEDIA_DEBUG
122 	if (ifmedia_debug) {
123 		if (ifm == NULL) {
124 			printf("ifmedia_add: null ifm\n");
125 			return;
126 		}
127 		printf("Adding entry for (%#010x) ", mword);
128 		ifmedia_printword(mword);
129 	}
130 #endif
131 
132 	entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
133 	if (entry == NULL)
134 		panic("ifmedia_add: can't malloc entry");
135 
136 	entry->ifm_media = mword;
137 	entry->ifm_data = data;
138 	entry->ifm_aux = aux;
139 
140 	LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
141 }
142 
143 /*
144  * Add an array of media configurations to the list of
145  * supported media for a specific interface instance.
146  */
147 void
ifmedia_list_add(struct ifmedia * ifm,struct ifmedia_entry * lp,int count)148 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, 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
ifmedia_set(struct ifmedia * ifm,int target)165 ifmedia_set(struct ifmedia *ifm, int target)
166 {
167 	struct ifmedia_entry *match;
168 
169 	match = ifmedia_match(ifm, target, ifm->ifm_mask);
170 
171 	if (match == NULL) {
172 		printf("ifmedia_set: no match for 0x%x/0x%x\n",
173 		    target, ~ifm->ifm_mask);
174 		panic("ifmedia_set");
175 	}
176 	ifm->ifm_cur = match;
177 
178 #ifdef IFMEDIA_DEBUG
179 	if (ifmedia_debug) {
180 		printf("ifmedia_set: target ");
181 		ifmedia_printword(target);
182 		printf("ifmedia_set: setting to ");
183 		ifmedia_printword(ifm->ifm_cur->ifm_media);
184 	}
185 #endif
186 }
187 
188 /*
189  * Given a media word, return one suitable for an application
190  * using the original encoding.
191  */
192 static int
compat_media(int media)193 compat_media(int media)
194 {
195 
196 	if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
197 		media &= ~(IFM_ETH_XTYPE|IFM_TMASK);
198 		media |= IFM_OTHER;
199 	}
200 	return (media);
201 }
202 
203 /*
204  * Device-independent media ioctl support function.
205  */
206 int
ifmedia_ioctl(struct ifnet * ifp,struct ifreq * ifr,struct ifmedia * ifm,u_long cmd)207 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
208     u_long cmd)
209 {
210 	struct ifmedia_entry *match;
211 	struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
212 	int error = 0;
213 
214 	if (ifp == NULL || ifr == NULL || ifm == NULL)
215 		return (EINVAL);
216 
217 	switch (cmd) {
218 	/*
219 	 * Set the current media.
220 	 */
221 	case  SIOCSIFMEDIA:
222 	{
223 		struct ifmedia_entry *oldentry;
224 		int oldmedia;
225 		int newmedia = ifr->ifr_media;
226 
227 		match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
228 		if (match == NULL) {
229 #ifdef IFMEDIA_DEBUG
230 			if (ifmedia_debug) {
231 				printf(
232 		    "ifmedia_ioctl: no media found for %#010x mask %#010x\n",
233 				    newmedia, ifm->ifm_mask);
234 			}
235 #endif
236 			return (ENXIO);
237 		}
238 
239 		/*
240 		 * If no change, we're done.
241 		 * XXX Automedia may invole software intervention.
242 		 *     Keep going in case the connected media changed.
243 		 *     Similarly, if best match changed (kernel debugger?).
244 		 */
245 		if (IFM_SUBTYPE(newmedia) != IFM_AUTO &&
246 		    newmedia == ifm->ifm_media && match == ifm->ifm_cur)
247 			return (0);
248 
249 		/*
250 		 * We found a match, now make the driver switch to it.
251 		 * Make sure to preserve our old media type in case the
252 		 * driver can't switch.
253 		 */
254 #ifdef IFMEDIA_DEBUG
255 		if (ifmedia_debug) {
256 			printf("ifmedia_ioctl: switching %s to ",
257 			    ifp->if_xname);
258 			ifmedia_printword(match->ifm_media);
259 		}
260 #endif
261 		oldentry = ifm->ifm_cur;
262 		oldmedia = ifm->ifm_media;
263 		ifm->ifm_cur = match;
264 		ifm->ifm_media = newmedia;
265 		error = (*ifm->ifm_change)(ifp);
266 		if (error) {
267 			ifm->ifm_cur = oldentry;
268 			ifm->ifm_media = oldmedia;
269 		}
270 		break;
271 	}
272 
273 	/*
274 	 * Get list of available media and current media on interface.
275 	 */
276 	case  SIOCGIFMEDIA:
277 	case  SIOCGIFXMEDIA:
278 	{
279 		struct ifmedia_entry *ep;
280 		int i;
281 
282 		if (ifmr->ifm_count < 0)
283 			return (EINVAL);
284 
285 		if (cmd == SIOCGIFMEDIA) {
286 			ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
287 			    compat_media(ifm->ifm_cur->ifm_media) : IFM_NONE;
288 		} else {
289 			ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
290 			    ifm->ifm_cur->ifm_media : IFM_NONE;
291 		}
292 		ifmr->ifm_mask = ifm->ifm_mask;
293 		ifmr->ifm_status = 0;
294 
295 		/*
296 		 * Don't panic if ifm_status isn't yet setup due to
297 		 * driver/miibus probe ordering.  This can happen if
298 		 * a kldload'ed driver doesn't set the module order
299 		 * to setup miibus early enough.
300 		 *
301 		 * See kern/286530 for more information.
302 		 */
303 		if (ifm->ifm_status == NULL) {
304 			if_printf(ifp,
305 			    "%s: ifm_status is NULL; please fix miibus/driver"
306 			    " order\n",
307 			    __func__);
308 			return (EDOOFUS);
309 		}
310 		(*ifm->ifm_status)(ifp, ifmr);
311 
312 		/*
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
316 		 * allocate.
317 		 */
318 		i = 0;
319 		LIST_FOREACH(ep, &ifm->ifm_list, ifm_list) {
320 			if (i < ifmr->ifm_count) {
321 				error = copyout(&ep->ifm_media,
322 				    ifmr->ifm_ulist + i, sizeof(int));
323 				if (error != 0)
324 					break;
325 			}
326 			i++;
327 		}
328 		if (error == 0 && i > ifmr->ifm_count)
329 			error = ifmr->ifm_count != 0 ? E2BIG : 0;
330 		ifmr->ifm_count = i;
331 		break;
332 	}
333 
334 	default:
335 		return (EINVAL);
336 	}
337 
338 	return (error);
339 }
340 
341 /*
342  * Find media entry matching a given ifm word.
343  *
344  */
345 static struct ifmedia_entry *
ifmedia_match(struct ifmedia * ifm,int target,int mask)346 ifmedia_match(struct ifmedia *ifm, int target, int mask)
347 {
348 	struct ifmedia_entry *match, *next;
349 
350 	match = NULL;
351 	mask = ~mask;
352 
353 	LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
354 		if ((next->ifm_media & mask) == (target & mask)) {
355 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
356 			if (match) {
357 				printf("ifmedia_match: multiple match for "
358 				    "%#010x/%#010x\n", target, mask);
359 			}
360 #endif
361 			match = next;
362 		}
363 	}
364 
365 	return (match);
366 }
367 
368 /*
369  * Compute the interface `baudrate' from the media, for the interface
370  * metrics (used by routing daemons).
371  */
372 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
373     IFM_BAUDRATE_DESCRIPTIONS;
374 
375 uint64_t
ifmedia_baudrate(int mword)376 ifmedia_baudrate(int mword)
377 {
378 	int i;
379 
380 	for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
381 		if (IFM_TYPE_MATCH(mword, ifmedia_baudrate_descriptions[i].
382 		    ifmb_word))
383 			return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
384 	}
385 
386 	/* Not known. */
387 	return (0);
388 }
389 
390 #ifdef IFMEDIA_DEBUG
391 static const struct ifmedia_description ifm_type_descriptions[] =
392     IFM_TYPE_DESCRIPTIONS;
393 
394 static const struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
395     IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
396 
397 static const struct ifmedia_description
398     ifm_subtype_ethernet_option_descriptions[] =
399     IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
400 
401 static const struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
402     IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
403 
404 static const struct ifmedia_description
405     ifm_subtype_ieee80211_option_descriptions[] =
406     IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
407 
408 static const struct ifmedia_description
409     ifm_subtype_ieee80211_mode_descriptions[] =
410     IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
411 
412 static const struct ifmedia_description ifm_subtype_atm_descriptions[] =
413     IFM_SUBTYPE_ATM_DESCRIPTIONS;
414 
415 static const struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
416     IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
417 
418 static const struct ifmedia_description ifm_subtype_shared_descriptions[] =
419     IFM_SUBTYPE_SHARED_DESCRIPTIONS;
420 
421 static const struct ifmedia_description ifm_shared_option_descriptions[] =
422     IFM_SHARED_OPTION_DESCRIPTIONS;
423 
424 struct ifmedia_type_to_subtype {
425 	const struct ifmedia_description *subtypes;
426 	const struct ifmedia_description *options;
427 	const struct ifmedia_description *modes;
428 };
429 
430 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
431 static const struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
432 	{
433 	  &ifm_subtype_ethernet_descriptions[0],
434 	  &ifm_subtype_ethernet_option_descriptions[0],
435 	  NULL,
436 	},
437 	{
438 	  &ifm_subtype_ieee80211_descriptions[0],
439 	  &ifm_subtype_ieee80211_option_descriptions[0],
440 	  &ifm_subtype_ieee80211_mode_descriptions[0]
441 	},
442 	{
443 	  &ifm_subtype_atm_descriptions[0],
444 	  &ifm_subtype_atm_option_descriptions[0],
445 	  NULL,
446 	},
447 };
448 
449 /*
450  * print a media word.
451  */
452 static void
ifmedia_printword(int ifmw)453 ifmedia_printword(int ifmw)
454 {
455 	const struct ifmedia_description *desc;
456 	const struct ifmedia_type_to_subtype *ttos;
457 	int seen_option = 0;
458 
459 	/* Find the top-level interface type. */
460 	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
461 	    desc->ifmt_string != NULL; desc++, ttos++)
462 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
463 			break;
464 	if (desc->ifmt_string == NULL) {
465 		printf("<unknown type>\n");
466 		return;
467 	}
468 	printf("%s", desc->ifmt_string);
469 
470 	/* Any mode. */
471 	for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++)
472 		if (IFM_MODE(ifmw) == desc->ifmt_word) {
473 			if (desc->ifmt_string != NULL)
474 				printf(" mode %s", desc->ifmt_string);
475 			break;
476 		}
477 
478 	/*
479 	 * Check for the shared subtype descriptions first, then the
480 	 * type-specific ones.
481 	 */
482 	for (desc = ifm_subtype_shared_descriptions;
483 	    desc->ifmt_string != NULL; desc++)
484 		if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
485 			goto got_subtype;
486 
487 	for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
488 		if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
489 			break;
490 	if (desc->ifmt_string == NULL) {
491 		printf(" <unknown subtype>\n");
492 		return;
493 	}
494 
495  got_subtype:
496 	printf(" %s", desc->ifmt_string);
497 
498 	/*
499 	 * Look for shared options.
500 	 */
501 	for (desc = ifm_shared_option_descriptions;
502 	    desc->ifmt_string != NULL; desc++) {
503 		if (ifmw & desc->ifmt_word) {
504 			if (seen_option == 0)
505 				printf(" <");
506 			printf("%s%s", seen_option++ ? "," : "",
507 			    desc->ifmt_string);
508 		}
509 	}
510 
511 	/*
512 	 * Look for subtype-specific options.
513 	 */
514 	for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
515 		if (ifmw & desc->ifmt_word) {
516 			if (seen_option == 0)
517 				printf(" <");
518 			printf("%s%s", seen_option++ ? "," : "",
519 			    desc->ifmt_string);
520 		}
521 	}
522 	printf("%s\n", seen_option ? ">" : "");
523 }
524 #endif /* IFMEDIA_DEBUG */
525