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