xref: /openbsd-src/sys/net/if_media.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: if_media.c,v 1.23 2014/07/12 18:44:22 tedu 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 minwords;
312 			int *kptr;
313 
314 			minwords = nwords > (size_t)ifmr->ifm_count ?
315 			    (size_t)ifmr->ifm_count : nwords;
316 			kptr = malloc(nwords * sizeof(int), M_TEMP,
317 			    M_WAITOK | M_ZERO);
318 			/*
319 			 * Get the media words from the interface's list.
320 			 */
321 			ep = TAILQ_FIRST(&ifm->ifm_list);
322 			for (nwords = 0; ep != NULL && nwords < minwords;
323 			    ep = TAILQ_NEXT(ep, ifm_list))
324 				kptr[nwords++] = ep->ifm_media;
325 			if (ep == NULL)
326 				error = copyout(kptr, ifmr->ifm_ulist,
327 				    nwords * sizeof(int));
328 			else
329 				error = E2BIG;
330 			free(kptr, M_TEMP, 0);
331 		}
332 		ifmr->ifm_count = nwords;
333 		break;
334 	}
335 
336 	default:
337 		return (ENOTTY);
338 	}
339 
340 	return (error);
341 }
342 
343 /*
344  * Find media entry matching a given ifm word.
345  */
346 struct ifmedia_entry *
347 ifmedia_match(struct ifmedia *ifm, u_int target, u_int mask)
348 {
349 	struct ifmedia_entry *match, *next;
350 
351 	match = NULL;
352 	mask = ~mask;
353 
354 	TAILQ_FOREACH(next, &ifm->ifm_list, ifm_list) {
355 		if ((next->ifm_media & mask) == (target & mask)) {
356 			if (match) {
357 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
358 				printf("ifmedia_match: multiple match for "
359 				    "0x%x/0x%x, selected instance %d\n",
360 				    target, mask, IFM_INST(match->ifm_media));
361 #endif
362 				break;
363 			}
364 			match = next;
365 		}
366 	}
367 
368 	return (match);
369 }
370 
371 /*
372  * Delete all media for a given instance.
373  */
374 void
375 ifmedia_delete_instance(struct ifmedia *ifm, u_int inst)
376 {
377 	struct ifmedia_entry *ife, *nife;
378 
379 	TAILQ_FOREACH_SAFE(ife, &ifm->ifm_list, ifm_list, nife) {
380 		if (inst == IFM_INST_ANY ||
381 		    inst == IFM_INST(ife->ifm_media)) {
382 			TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
383 			free(ife, M_IFADDR, 0);
384 		}
385 	}
386 }
387 
388 /*
389  * Compute the interface `baudrate' from the media, for the interface
390  * metrics (used by routing daemons).
391  */
392 struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
393     IFM_BAUDRATE_DESCRIPTIONS;
394 
395 u_int64_t
396 ifmedia_baudrate(int mword)
397 {
398 	int i;
399 
400 	for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
401 		if ((mword & (IFM_NMASK|IFM_TMASK)) ==
402 		    ifmedia_baudrate_descriptions[i].ifmb_word)
403 			return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
404 	}
405 
406 	/* Not known. */
407 	return (0);
408 }
409 
410 #ifdef IFMEDIA_DEBUG
411 
412 struct ifmedia_description ifm_type_descriptions[] =
413     IFM_TYPE_DESCRIPTIONS;
414 
415 struct ifmedia_description ifm_subtype_descriptions[] =
416     IFM_SUBTYPE_DESCRIPTIONS;
417 
418 struct ifmedia_description ifm_option_descriptions[] =
419     IFM_OPTION_DESCRIPTIONS;
420 
421 /*
422  * print a media word.
423  */
424 static void
425 ifmedia_printword(int ifmw)
426 {
427 	struct ifmedia_description *desc;
428 	int seen_option = 0;
429 
430 	/* Print the top-level interface type. */
431 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
432 	     desc++) {
433 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
434 			break;
435 	}
436 	if (desc->ifmt_string == NULL)
437 		printf("<unknown type> ");
438 	else
439 		printf("%s ", desc->ifmt_string);
440 
441 	/* Print the subtype. */
442 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
443 	     desc++) {
444 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
445 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
446 			break;
447 	}
448 	if (desc->ifmt_string == NULL)
449 		printf("<unknown subtype>");
450 	else
451 		printf("%s", desc->ifmt_string);
452 
453 	/* Print any options. */
454 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
455 	     desc++) {
456 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
457 		    (ifmw & desc->ifmt_word) != 0 &&
458 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
459 			if (seen_option == 0)
460 				printf(" <");
461 			printf("%s%s", seen_option ? "," : "",
462 			    desc->ifmt_string);
463 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
464 		}
465 	}
466 	printf("%s\n", seen_option ? ">" : "");
467 }
468 
469 #endif /* IFMEDIA_DEBUG */
470