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