xref: /openbsd/sys/net/if_media.c (revision 404b540a)
1 /*	$OpenBSD: if_media.c,v 1.20 2008/06/26 05:42:20 ray Exp $	*/
2 /*	$NetBSD: if_media.c,v 1.10 2000/03/13 23:52:39 soren Exp $	*/
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1997
36  *	Jonathan Stone and Jason R. Thorpe.  All rights reserved.
37  *
38  * This software is derived from information provided by Matt Thomas.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *      This product includes software developed by Jonathan Stone
51  *	and Jason R. Thorpe for the NetBSD Project.
52  * 4. The names of the authors may not be used to endorse or promote products
53  *    derived from this software without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
56  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
57  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
58  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
59  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
60  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
61  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
62  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
63  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  */
67 
68 /*
69  * BSD/OS-compatible network interface media selection.
70  *
71  * Where it is safe to do so, this code strays slightly from the BSD/OS
72  * design.  Software which uses the API (device drivers, basically)
73  * shouldn't notice any difference.
74  *
75  * Many thanks to Matt Thomas for providing the information necessary
76  * to implement this interface.
77  */
78 
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/errno.h>
82 #include <sys/ioctl.h>
83 #include <sys/socket.h>
84 #include <sys/malloc.h>
85 
86 #include <net/if.h>
87 #include <net/if_media.h>
88 #include <net/netisr.h>
89 
90 /*
91  * Compile-time options:
92  * IFMEDIA_DEBUG:
93  *	turn on implementation-level debug printfs.
94  * 	Useful for debugging newly-ported  drivers.
95  */
96 
97 #ifdef IFMEDIA_DEBUG
98 int	ifmedia_debug = 0;
99 static	void ifmedia_printword(int);
100 #endif
101 
102 /*
103  * Initialize if_media struct for a specific interface instance.
104  */
105 void
106 ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
107     ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
108 {
109 	TAILQ_INIT(&ifm->ifm_list);
110 	ifm->ifm_cur = NULL;
111 	ifm->ifm_media = 0;
112 	ifm->ifm_mask = dontcare_mask;		/* IF don't-care bits */
113 	ifm->ifm_change = change_callback;
114 	ifm->ifm_status = status_callback;
115 }
116 
117 /*
118  * Add a media configuration to the list of supported media
119  * for a specific interface instance.
120  */
121 void
122 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
123 {
124 	struct ifmedia_entry *entry;
125 
126 #ifdef IFMEDIA_DEBUG
127 	if (ifmedia_debug) {
128 		if (ifm == NULL) {
129 			printf("ifmedia_add: null ifm\n");
130 			return;
131 		}
132 		printf("Adding entry for ");
133 		ifmedia_printword(mword);
134 	}
135 #endif
136 
137 	entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
138 	if (entry == NULL)
139 		panic("ifmedia_add: can't malloc entry");
140 
141 	entry->ifm_media = mword;
142 	entry->ifm_data = data;
143 	entry->ifm_aux = aux;
144 
145 	TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list);
146 }
147 
148 /*
149  * Add an array of media configurations to the list of
150  * supported media for a specific interface instance.
151  */
152 void
153 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count)
154 {
155 	int i;
156 
157 	for (i = 0; i < count; i++)
158 		ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
159 		    lp[i].ifm_aux);
160 }
161 
162 /*
163  * Set the default active media.
164  *
165  * Called by device-specific code which is assumed to have already
166  * selected the default media in hardware.  We do _not_ call the
167  * media-change callback.
168  */
169 void
170 ifmedia_set(struct ifmedia *ifm, int target)
171 {
172 	struct ifmedia_entry *match;
173 
174 	match = ifmedia_match(ifm, target, ifm->ifm_mask);
175 
176 	/*
177 	 * If we didn't find the requested media, then we try to fall
178 	 * back to target-type (IFM_ETHER, e.g.) | IFM_NONE.  If that's
179 	 * not on the list, then we add it and set the media to it.
180 	 *
181 	 * Since ifmedia_set is almost always called with IFM_AUTO or
182 	 * with a known-good media, this really should only occur if we:
183 	 *
184 	 * a) didn't find any PHYs, or
185 	 * b) didn't find an autoselect option on the PHY when the
186 	 *    parent ethernet driver expected to.
187 	 *
188 	 * In either case, it makes sense to select no media.
189 	 */
190 	if (match == NULL) {
191 		printf("ifmedia_set: no match for 0x%x/0x%x\n",
192 		    target, ~ifm->ifm_mask);
193 		target = (target & IFM_NMASK) | IFM_NONE;
194 		match = ifmedia_match(ifm, target, ifm->ifm_mask);
195 		if (match == NULL) {
196 			ifmedia_add(ifm, target, 0, NULL);
197 			match = ifmedia_match(ifm, target, ifm->ifm_mask);
198 			if (match == NULL)
199 				panic("ifmedia_set failed");
200 		}
201 	}
202 	ifm->ifm_cur = match;
203 
204 #ifdef IFMEDIA_DEBUG
205 	if (ifmedia_debug) {
206 		printf("ifmedia_set: target ");
207 		ifmedia_printword(target);
208 		printf("ifmedia_set: setting to ");
209 		ifmedia_printword(ifm->ifm_cur->ifm_media);
210 	}
211 #endif
212 }
213 
214 /*
215  * Device-independent media ioctl support function.
216  */
217 int
218 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
219     u_long cmd)
220 {
221 	struct ifmedia_entry *match;
222 	struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
223 	int error = 0;
224 
225 	if (ifp == NULL || ifr == NULL || ifm == NULL)
226 		return (EINVAL);
227 
228 	switch (cmd) {
229 
230 	/*
231 	 * Set the current media.
232 	 */
233 	case  SIOCSIFMEDIA:
234 	{
235 		struct ifmedia_entry *oldentry;
236 		u_int oldmedia;
237 		u_int newmedia = ifr->ifr_media;
238 
239 		match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
240 		if (match == NULL) {
241 #ifdef IFMEDIA_DEBUG
242 			if (ifmedia_debug) {
243 				printf("ifmedia_ioctl: no media found for 0x%x\n",
244 				    newmedia);
245 			}
246 #endif
247 			return (EINVAL);
248 		}
249 
250 		/*
251 		 * If no change, we're done.
252 		 * XXX Automedia may involve software intervention.
253 		 *     Keep going in case the connected media changed.
254 		 *     Similarly, if best match changed (kernel debugger?).
255 		 */
256 		if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
257 		    (newmedia == ifm->ifm_media) &&
258 		    (match == ifm->ifm_cur))
259 			return (0);
260 
261 		/*
262 		 * We found a match, now make the driver switch to it.
263 		 * Make sure to preserve our old media type in case the
264 		 * driver can't switch.
265 		 */
266 #ifdef IFMEDIA_DEBUG
267 		if (ifmedia_debug) {
268 			printf("ifmedia_ioctl: switching %s to ",
269 			    ifp->if_xname);
270 			ifmedia_printword(match->ifm_media);
271 		}
272 #endif
273 		oldentry = ifm->ifm_cur;
274 		oldmedia = ifm->ifm_media;
275 		ifm->ifm_cur = match;
276 		ifm->ifm_media = newmedia;
277 		error = (*ifm->ifm_change)(ifp);
278 		if (error) {
279 			ifm->ifm_cur = oldentry;
280 			ifm->ifm_media = oldmedia;
281 		}
282 		break;
283 	}
284 
285 	/*
286 	 * Get list of available media and current media on interface.
287 	 */
288 	case  SIOCGIFMEDIA:
289 	{
290 		struct ifmedia_entry *ep;
291 		size_t nwords;
292 
293 		if(ifmr->ifm_count < 0)
294 			return (EINVAL);
295 
296 		ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
297 		    ifm->ifm_cur->ifm_media : IFM_NONE;
298 		ifmr->ifm_mask = ifm->ifm_mask;
299 		ifmr->ifm_status = 0;
300 		(*ifm->ifm_status)(ifp, ifmr);
301 
302 		/*
303 		 * Count them so we know a-priori how much is the max we'll
304 		 * need.
305 		 */
306 		ep = TAILQ_FIRST(&ifm->ifm_list);
307 		for (nwords = 0; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list))
308 			nwords++;
309 
310 		if (ifmr->ifm_count != 0) {
311 			size_t count;
312 			size_t minwords = nwords > (size_t)ifmr->ifm_count
313 			    ? (size_t)ifmr->ifm_count : nwords;
314 			int *kptr = (int *)malloc(minwords * sizeof(int),
315  			    M_TEMP, M_WAITOK);
316 			/*
317 			 * Get the media words from the interface's list.
318 			 */
319 			ep = TAILQ_FIRST(&ifm->ifm_list);
320 			for (count = 0; ep != NULL && count < minwords;
321 			    ep = TAILQ_NEXT(ep, ifm_list), count++)
322 				kptr[count] = ep->ifm_media;
323 
324 			error = copyout(kptr, ifmr->ifm_ulist,
325 			    minwords * sizeof(int));
326 			if (error == 0 && ep != NULL)
327 				error = E2BIG;	/* oops! */
328 			free(kptr, M_TEMP);
329 		}
330 		ifmr->ifm_count = nwords;
331 		break;
332 	}
333 
334 	default:
335 		return (ENOTTY);
336 	}
337 
338 	return (error);
339 }
340 
341 /*
342  * Find media entry matching a given ifm word.
343  */
344 struct ifmedia_entry *
345 ifmedia_match(struct ifmedia *ifm, u_int target, u_int mask)
346 {
347 	struct ifmedia_entry *match, *next;
348 
349 	match = NULL;
350 	mask = ~mask;
351 
352 	for (next = TAILQ_FIRST(&ifm->ifm_list); next != NULL;
353 	     next = TAILQ_NEXT(next, ifm_list)) {
354 		if ((next->ifm_media & mask) == (target & mask)) {
355 			if (match) {
356 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
357 				printf("ifmedia_match: multiple match for "
358 				    "0x%x/0x%x, selected instance %d\n",
359 				    target, mask, IFM_INST(match->ifm_media));
360 #endif
361 				break;
362 			}
363 			match = next;
364 		}
365 	}
366 
367 	return (match);
368 }
369 
370 /*
371  * Delete all media for a given instance.
372  */
373 void
374 ifmedia_delete_instance(struct ifmedia *ifm, u_int inst)
375 {
376 	struct ifmedia_entry *ife, *nife;
377 
378 	for (ife = TAILQ_FIRST(&ifm->ifm_list); ife != NULL;
379 	     ife = nife) {
380 		nife = TAILQ_NEXT(ife, ifm_list);
381 		if (inst == IFM_INST_ANY ||
382 		    inst == IFM_INST(ife->ifm_media)) {
383 			TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
384 			free(ife, M_IFADDR);
385 		}
386 	}
387 }
388 
389 /*
390  * Compute the interface `baudrate' from the media, for the interface
391  * metrics (used by routing daemons).
392  */
393 struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
394     IFM_BAUDRATE_DESCRIPTIONS;
395 
396 u_int64_t
397 ifmedia_baudrate(int mword)
398 {
399 	int i;
400 
401 	for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
402 		if ((mword & (IFM_NMASK|IFM_TMASK)) ==
403 		    ifmedia_baudrate_descriptions[i].ifmb_word)
404 			return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
405 	}
406 
407 	/* Not known. */
408 	return (0);
409 }
410 
411 #ifdef IFMEDIA_DEBUG
412 
413 struct ifmedia_description ifm_type_descriptions[] =
414     IFM_TYPE_DESCRIPTIONS;
415 
416 struct ifmedia_description ifm_subtype_descriptions[] =
417     IFM_SUBTYPE_DESCRIPTIONS;
418 
419 struct ifmedia_description ifm_option_descriptions[] =
420     IFM_OPTION_DESCRIPTIONS;
421 
422 /*
423  * print a media word.
424  */
425 static void
426 ifmedia_printword(int ifmw)
427 {
428 	struct ifmedia_description *desc;
429 	int seen_option = 0;
430 
431 	/* Print the top-level interface type. */
432 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
433 	     desc++) {
434 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
435 			break;
436 	}
437 	if (desc->ifmt_string == NULL)
438 		printf("<unknown type> ");
439 	else
440 		printf("%s ", desc->ifmt_string);
441 
442 	/* Print the subtype. */
443 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
444 	     desc++) {
445 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
446 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
447 			break;
448 	}
449 	if (desc->ifmt_string == NULL)
450 		printf("<unknown subtype>");
451 	else
452 		printf("%s", desc->ifmt_string);
453 
454 	/* Print any options. */
455 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
456 	     desc++) {
457 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
458 		    (ifmw & desc->ifmt_word) != 0 &&
459 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
460 			if (seen_option == 0)
461 				printf(" <");
462 			printf("%s%s", seen_option ? "," : "",
463 			    desc->ifmt_string);
464 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
465 		}
466 	}
467 	printf("%s\n", seen_option ? ">" : "");
468 }
469 
470 #endif /* IFMEDIA_DEBUG */
471