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