xref: /netbsd-src/sys/net/if_media.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: if_media.c,v 1.14 2001/05/18 08:56:27 drochner Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
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 the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1997
42  *	Jonathan Stone and Jason R. Thorpe.  All rights reserved.
43  *
44  * This software is derived from information provided by Matt Thomas.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *      This product includes software developed by Jonathan Stone
57  *	and Jason R. Thorpe for the NetBSD Project.
58  * 4. The names of the authors may not be used to endorse or promote products
59  *    derived from this software without specific prior written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
62  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
63  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
64  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
65  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
66  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
67  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
68  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
69  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71  * SUCH DAMAGE.
72  */
73 
74 /*
75  * BSD/OS-compatible network interface media selection.
76  *
77  * Where it is safe to do so, this code strays slightly from the BSD/OS
78  * design.  Software which uses the API (device drivers, basically)
79  * shouldn't notice any difference.
80  *
81  * Many thanks to Matt Thomas for providing the information necessary
82  * to implement this interface.
83  */
84 
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/errno.h>
88 #include <sys/ioctl.h>
89 #include <sys/socket.h>
90 #include <sys/malloc.h>
91 
92 #include <net/if.h>
93 #include <net/if_media.h>
94 #include <net/netisr.h>
95 
96 /*
97  * Compile-time options:
98  * IFMEDIA_DEBUG:
99  *	turn on implementation-level debug printfs.
100  * 	Useful for debugging newly-ported  drivers.
101  */
102 
103 #ifdef IFMEDIA_DEBUG
104 int	ifmedia_debug = 0;
105 static	void ifmedia_printword __P((int));
106 #endif
107 
108 /*
109  * Initialize if_media struct for a specific interface instance.
110  */
111 void
112 ifmedia_init(ifm, dontcare_mask, change_callback, status_callback)
113 	struct ifmedia *ifm;
114 	int dontcare_mask;
115 	ifm_change_cb_t change_callback;
116 	ifm_stat_cb_t status_callback;
117 {
118 
119 	TAILQ_INIT(&ifm->ifm_list);
120 	ifm->ifm_cur = NULL;
121 	ifm->ifm_media = 0;
122 	ifm->ifm_mask = dontcare_mask;		/* IF don't-care bits */
123 	ifm->ifm_change = change_callback;
124 	ifm->ifm_status = status_callback;
125 }
126 
127 /*
128  * Add a media configuration to the list of supported media
129  * for a specific interface instance.
130  */
131 void
132 ifmedia_add(ifm, mword, data, aux)
133 	struct ifmedia *ifm;
134 	int mword;
135 	int data;
136 	void *aux;
137 {
138 	struct ifmedia_entry *entry;
139 
140 #ifdef IFMEDIA_DEBUG
141 	if (ifmedia_debug) {
142 		if (ifm == NULL) {
143 			printf("ifmedia_add: null ifm\n");
144 			return;
145 		}
146 		printf("Adding entry for ");
147 		ifmedia_printword(mword);
148 	}
149 #endif
150 
151 	entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
152 	if (entry == NULL)
153 		panic("ifmedia_add: can't malloc entry");
154 
155 	entry->ifm_media = mword;
156 	entry->ifm_data = data;
157 	entry->ifm_aux = aux;
158 
159 	TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list);
160 }
161 
162 /*
163  * Add an array of media configurations to the list of
164  * supported media for a specific interface instance.
165  */
166 void
167 ifmedia_list_add(ifm, lp, count)
168 	struct ifmedia *ifm;
169 	struct ifmedia_entry *lp;
170 	int count;
171 {
172 	int i;
173 
174 	for (i = 0; i < count; i++)
175 		ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
176 		    lp[i].ifm_aux);
177 }
178 
179 /*
180  * Set the default active media.
181  *
182  * Called by device-specific code which is assumed to have already
183  * selected the default media in hardware.  We do _not_ call the
184  * media-change callback.
185  */
186 void
187 ifmedia_set(ifm, target)
188 	struct ifmedia *ifm;
189 	int target;
190 
191 {
192 	struct ifmedia_entry *match;
193 
194 	match = ifmedia_match(ifm, target, ifm->ifm_mask);
195 
196 	if (match == NULL) {
197 		printf("ifmedia_set: no match for 0x%x/0x%x\n",
198 		    target, ~ifm->ifm_mask);
199 		panic("ifmedia_set");
200 	}
201 	ifm->ifm_cur = match;
202 
203 #ifdef IFMEDIA_DEBUG
204 	if (ifmedia_debug) {
205 		printf("ifmedia_set: target ");
206 		ifmedia_printword(target);
207 		printf("ifmedia_set: setting to ");
208 		ifmedia_printword(ifm->ifm_cur->ifm_media);
209 	}
210 #endif
211 }
212 
213 /*
214  * Device-independent media ioctl support function.
215  */
216 int
217 ifmedia_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, sticky;
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 (EINVAL);
251 		}
252 
253 		/*
254 		 * If no change, we're done.
255 		 * XXX Automedia may involve 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 	{
293 		struct ifmedia_entry *ep;
294 		int *kptr, count;
295 
296 		kptr = NULL;		/* XXX gcc */
297 
298 		ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
299 		    ifm->ifm_cur->ifm_media : IFM_NONE;
300 		ifmr->ifm_mask = ifm->ifm_mask;
301 		ifmr->ifm_status = 0;
302 		(*ifm->ifm_status)(ifp, ifmr);
303 
304 		count = 0;
305 		ep = TAILQ_FIRST(&ifm->ifm_list);
306 
307 		if (ifmr->ifm_count != 0) {
308 			kptr = (int *)malloc(ifmr->ifm_count * sizeof(int),
309 			    M_TEMP, M_WAITOK);
310 
311 			/*
312 			 * Get the media words from the interface's list.
313 			 */
314 			for (; ep != NULL && count < ifmr->ifm_count;
315 			    ep = TAILQ_NEXT(ep, ifm_list), count++)
316 				kptr[count] = ep->ifm_media;
317 
318 			if (ep != NULL)
319 				error = E2BIG;	/* oops! */
320 		}
321 
322 		/*
323 		 * If there are more interfaces on the list, count
324 		 * them.  This allows the caller to set ifmr->ifm_count
325 		 * to 0 on the first call to know how much space to
326 		 * callocate.
327 		 */
328 		for (; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list))
329 			count++;
330 
331 		/*
332 		 * We do the copyout on E2BIG, because that's
333 		 * just our way of telling userland that there
334 		 * are more.  This is the behavior I've observed
335 		 * under BSD/OS 3.0
336 		 */
337 		sticky = error;
338 		if ((error == 0 || error == E2BIG) && ifmr->ifm_count != 0) {
339 			error = copyout((caddr_t)kptr,
340 			    (caddr_t)ifmr->ifm_ulist,
341 			    ifmr->ifm_count * sizeof(int));
342 		}
343 
344 		if (error == 0)
345 			error = sticky;
346 
347 		if (ifmr->ifm_count != 0)
348 			free(kptr, M_TEMP);
349 
350 		ifmr->ifm_count = count;
351 		break;
352 	}
353 
354 	default:
355 		return (EINVAL);
356 	}
357 
358 	return (error);
359 }
360 
361 /*
362  * Find media entry matching a given ifm word.
363  */
364 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 	for (next = TAILQ_FIRST(&ifm->ifm_list); next != NULL;
376 	     next = TAILQ_NEXT(next, ifm_list)) {
377 		if ((next->ifm_media & mask) == (target & mask)) {
378 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
379 			if (match) {
380 				printf("ifmedia_match: multiple match for "
381 				    "0x%x/0x%x\n", target, mask);
382 			}
383 #endif
384 			match = next;
385 		}
386 	}
387 
388 	return match;
389 }
390 
391 /*
392  * Delete all media for a given instance.
393  */
394 void
395 ifmedia_delete_instance(ifm, inst)
396 	struct ifmedia *ifm;
397 	int inst;
398 {
399 	struct ifmedia_entry *ife, *nife;
400 
401 	for (ife = TAILQ_FIRST(&ifm->ifm_list); ife != NULL;
402 	     ife = nife) {
403 		nife = TAILQ_NEXT(ife, ifm_list);
404 		if (inst == IFM_INST_ANY ||
405 		    inst == IFM_INST(ife->ifm_media)) {
406 			TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
407 			free(ife, M_DEVBUF);
408 		}
409 	}
410 }
411 
412 /*
413  * Compute the interface `baudrate' from the media, for the interface
414  * metrics (used by routing daemons).
415  */
416 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
417     IFM_BAUDRATE_DESCRIPTIONS;
418 
419 int
420 ifmedia_baudrate(mword)
421 	int mword;
422 {
423 	int i;
424 
425 	for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
426 		if ((mword & (IFM_NMASK|IFM_TMASK)) ==
427 		    ifmedia_baudrate_descriptions[i].ifmb_word)
428 			return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
429 	}
430 
431 	/* Not known. */
432 	return (0);
433 }
434 
435 #ifdef IFMEDIA_DEBUG
436 
437 static const struct ifmedia_description ifm_type_descriptions[] =
438     IFM_TYPE_DESCRIPTIONS;
439 
440 static const struct ifmedia_description ifm_subtype_descriptions[] =
441     IFM_SUBTYPE_DESCRIPTIONS;
442 
443 static const struct ifmedia_description ifm_option_descriptions[] =
444     IFM_OPTION_DESCRIPTIONS;
445 
446 /*
447  * print a media word.
448  */
449 static void
450 ifmedia_printword(ifmw)
451 	int ifmw;
452 {
453 	const struct ifmedia_description *desc;
454 	int seen_option = 0;
455 
456 	/* Print the top-level interface type. */
457 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
458 	     desc++) {
459 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
460 			break;
461 	}
462 	if (desc->ifmt_string == NULL)
463 		printf("<unknown type> ");
464 	else
465 		printf("%s ", desc->ifmt_string);
466 
467 	/* Print the subtype. */
468 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
469 	     desc++) {
470 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
471 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
472 			break;
473 	}
474 	if (desc->ifmt_string == NULL)
475 		printf("<unknown subtype>");
476 	else
477 		printf("%s", desc->ifmt_string);
478 
479 	/* Print any options. */
480 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
481 	     desc++) {
482 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
483 		    (ifmw & desc->ifmt_word) != 0 &&
484 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
485 			if (seen_option == 0)
486 				printf(" <");
487 			printf("%s%s", seen_option ? "," : "",
488 			    desc->ifmt_string);
489 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
490 		}
491 	}
492 	printf("%s\n", seen_option ? ">" : "");
493 }
494 
495 #endif /* IFMEDIA_DEBUG */
496