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