xref: /openbsd/sys/net/if_media.c (revision 274d7c50)
1 /*	$OpenBSD: if_media.c,v 1.31 2019/04/21 08:28:53 stsp 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 #ifdef IFMEDIA_DEBUG
88 #include <net/if_var.h>
89 #endif
90 #include <net/if_media.h>
91 #include <net/netisr.h>
92 
93 /*
94  * Compile-time options:
95  * IFMEDIA_DEBUG:
96  *	turn on implementation-level debug printfs.
97  *	Useful for debugging newly-ported  drivers.
98  */
99 
100 #ifdef IFMEDIA_DEBUG
101 int	ifmedia_debug = 0;
102 static	void ifmedia_printword(uint64_t);
103 #endif
104 
105 /*
106  * Initialize if_media struct for a specific interface instance.
107  */
108 void
109 ifmedia_init(struct ifmedia *ifm, uint64_t dontcare_mask,
110     ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
111 {
112 	TAILQ_INIT(&ifm->ifm_list);
113 	ifm->ifm_cur = NULL;
114 	ifm->ifm_media = 0;
115 	ifm->ifm_mask = dontcare_mask;		/* IF don't-care bits */
116 	ifm->ifm_change = change_callback;
117 	ifm->ifm_status = status_callback;
118 }
119 
120 /*
121  * Add a media configuration to the list of supported media
122  * for a specific interface instance.
123  */
124 void
125 ifmedia_add(struct ifmedia *ifm, uint64_t mword, int data, void *aux)
126 {
127 	struct ifmedia_entry *entry;
128 
129 #ifdef IFMEDIA_DEBUG
130 	if (ifmedia_debug) {
131 		if (ifm == NULL) {
132 			printf("ifmedia_add: null ifm\n");
133 			return;
134 		}
135 		printf("Adding entry for ");
136 		ifmedia_printword(mword);
137 	}
138 #endif
139 
140 	entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
141 	if (entry == NULL)
142 		panic("ifmedia_add: can't malloc entry");
143 
144 	entry->ifm_media = mword;
145 	entry->ifm_data = data;
146 	entry->ifm_aux = aux;
147 
148 	TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list);
149 }
150 
151 /*
152  * Add an array of media configurations to the list of
153  * supported media for a specific interface instance.
154  */
155 void
156 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count)
157 {
158 	int i;
159 
160 	for (i = 0; i < count; i++)
161 		ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
162 		    lp[i].ifm_aux);
163 }
164 
165 /*
166  * Set the default active media.
167  *
168  * Called by device-specific code which is assumed to have already
169  * selected the default media in hardware.  We do _not_ call the
170  * media-change callback.
171  */
172 void
173 ifmedia_set(struct ifmedia *ifm, uint64_t target)
174 {
175 	struct ifmedia_entry *match;
176 
177 	match = ifmedia_match(ifm, target, ifm->ifm_mask);
178 
179 	/*
180 	 * If we didn't find the requested media, then we try to fall
181 	 * back to target-type (IFM_ETHER, e.g.) | IFM_NONE.  If that's
182 	 * not on the list, then we add it and set the media to it.
183 	 *
184 	 * Since ifmedia_set is almost always called with IFM_AUTO or
185 	 * with a known-good media, this really should only occur if we:
186 	 *
187 	 * a) didn't find any PHYs, or
188 	 * b) didn't find an autoselect option on the PHY when the
189 	 *    parent ethernet driver expected to.
190 	 *
191 	 * In either case, it makes sense to select no media.
192 	 */
193 	if (match == NULL) {
194 		printf("ifmedia_set: no match for 0x%llx/0x%llx\n",
195 		    target, ~ifm->ifm_mask);
196 		target = (target & IFM_NMASK) | IFM_NONE;
197 		match = ifmedia_match(ifm, target, ifm->ifm_mask);
198 		if (match == NULL) {
199 			ifmedia_add(ifm, target, 0, NULL);
200 			match = ifmedia_match(ifm, target, ifm->ifm_mask);
201 			if (match == NULL)
202 				panic("ifmedia_set failed");
203 		}
204 	}
205 	ifm->ifm_cur = match;
206 
207 #ifdef IFMEDIA_DEBUG
208 	if (ifmedia_debug) {
209 		printf("ifmedia_set: target ");
210 		ifmedia_printword(target);
211 		printf("ifmedia_set: setting to ");
212 		ifmedia_printword(ifm->ifm_cur->ifm_media);
213 	}
214 #endif
215 }
216 
217 /*
218  * Device-independent media ioctl support function.
219  */
220 int
221 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
222     u_long cmd)
223 {
224 	struct ifmedia_entry *match;
225 	int error = 0;
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 		uint64_t oldmedia;
239 		uint64_t 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("ifmedia_ioctl: no media found for 0x%llx\n",
246 				    newmedia);
247 			}
248 #endif
249 			return (EINVAL);
250 		}
251 
252 		/*
253 		 * If no change, we're done.
254 		 * XXX Automedia may involve software intervention.
255 		 *     Keep going in case the connected media changed.
256 		 *     Similarly, if best match changed (kernel debugger?).
257 		 */
258 		if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
259 		    (newmedia == ifm->ifm_media) &&
260 		    (match == ifm->ifm_cur))
261 			return (0);
262 
263 		/*
264 		 * We found a match, now make the driver switch to it.
265 		 * Make sure to preserve our old media type in case the
266 		 * driver can't switch.
267 		 */
268 #ifdef IFMEDIA_DEBUG
269 		if (ifmedia_debug) {
270 			printf("ifmedia_ioctl: switching %s to ",
271 			    ifp->if_xname);
272 			ifmedia_printword(match->ifm_media);
273 		}
274 #endif
275 		oldentry = ifm->ifm_cur;
276 		oldmedia = ifm->ifm_media;
277 		ifm->ifm_cur = match;
278 		ifm->ifm_media = newmedia;
279 		error = (*ifm->ifm_change)(ifp);
280 		if (error && error != ENETRESET) {
281 			ifm->ifm_cur = oldentry;
282 			ifm->ifm_media = oldmedia;
283 		}
284 		break;
285 	}
286 
287 	/*
288 	 * Get list of available media and current media on interface.
289 	 */
290 	case  SIOCGIFMEDIA:
291 	{
292 		struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
293 		struct ifmedia_entry *ep;
294 		size_t nwords;
295 
296 		if (ifmr->ifm_count < 0)
297 			return (EINVAL);
298 
299 		ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
300 		    ifm->ifm_cur->ifm_media : IFM_NONE;
301 		ifmr->ifm_mask = ifm->ifm_mask;
302 		ifmr->ifm_status = 0;
303 		(*ifm->ifm_status)(ifp, ifmr);
304 
305 		/*
306 		 * Count them so we know a-priori how much is the max we'll
307 		 * need.
308 		 */
309 		ep = TAILQ_FIRST(&ifm->ifm_list);
310 		for (nwords = 0; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list))
311 			nwords++;
312 
313 		if (ifmr->ifm_count != 0) {
314 			size_t minwords, ksiz;
315 			uint64_t *kptr;
316 
317 			minwords = nwords > (size_t)ifmr->ifm_count ?
318 			    (size_t)ifmr->ifm_count : nwords;
319 			kptr = mallocarray(nwords, sizeof(*kptr), M_TEMP,
320 			    M_WAITOK | M_ZERO);
321 			ksiz = nwords * sizeof(*kptr);
322 			/*
323 			 * Get the media words from the interface's list.
324 			 */
325 			ep = TAILQ_FIRST(&ifm->ifm_list);
326 			for (nwords = 0; ep != NULL && nwords < minwords;
327 			    ep = TAILQ_NEXT(ep, ifm_list))
328 				kptr[nwords++] = ep->ifm_media;
329 			if (ep == NULL)
330 				error = copyout(kptr, ifmr->ifm_ulist,
331 				    nwords * sizeof(*kptr));
332 			else
333 				error = E2BIG;
334 			free(kptr, M_TEMP, ksiz);
335 		}
336 		ifmr->ifm_count = nwords;
337 		break;
338 	}
339 
340 	default:
341 		return (ENOTTY);
342 	}
343 
344 	return (error);
345 }
346 
347 /*
348  * Find media entry matching a given ifm word.
349  */
350 struct ifmedia_entry *
351 ifmedia_match(struct ifmedia *ifm, uint64_t target, uint64_t mask)
352 {
353 	struct ifmedia_entry *match, *next;
354 
355 	match = NULL;
356 	mask = ~mask;
357 
358 	TAILQ_FOREACH(next, &ifm->ifm_list, ifm_list) {
359 		if ((next->ifm_media & mask) == (target & mask)) {
360 			if (match) {
361 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
362 				printf("ifmedia_match: multiple match for "
363 				    "0x%llx/0x%llx, selected instance %lld\n",
364 				    target, mask, IFM_INST(match->ifm_media));
365 #endif
366 				break;
367 			}
368 			match = next;
369 		}
370 	}
371 
372 	return (match);
373 }
374 
375 /*
376  * Delete all media for a given instance.
377  */
378 void
379 ifmedia_delete_instance(struct ifmedia *ifm, uint64_t inst)
380 {
381 	struct ifmedia_entry *ife, *nife;
382 
383 	TAILQ_FOREACH_SAFE(ife, &ifm->ifm_list, ifm_list, nife) {
384 		if (inst == IFM_INST_ANY ||
385 		    inst == IFM_INST(ife->ifm_media)) {
386 			TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
387 			free(ife, M_IFADDR, sizeof *ife);
388 		}
389 	}
390 }
391 
392 /*
393  * Compute the interface `baudrate' from the media, for the interface
394  * metrics (used by routing daemons).
395  */
396 struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
397     IFM_BAUDRATE_DESCRIPTIONS;
398 
399 uint64_t
400 ifmedia_baudrate(uint64_t mword)
401 {
402 	int i;
403 
404 	for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
405 		if ((mword & (IFM_NMASK|IFM_TMASK)) ==
406 		    ifmedia_baudrate_descriptions[i].ifmb_word)
407 			return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
408 	}
409 
410 	/* Not known. */
411 	return (0);
412 }
413 
414 #ifdef IFMEDIA_DEBUG
415 
416 struct ifmedia_description ifm_type_descriptions[] =
417     IFM_TYPE_DESCRIPTIONS;
418 
419 struct ifmedia_description ifm_subtype_descriptions[] =
420     IFM_SUBTYPE_DESCRIPTIONS;
421 
422 struct ifmedia_description ifm_option_descriptions[] =
423     IFM_OPTION_DESCRIPTIONS;
424 
425 /*
426  * print a media word.
427  */
428 static void
429 ifmedia_printword(uint64_t ifmw)
430 {
431 	struct ifmedia_description *desc;
432 	uint64_t seen_option = 0;
433 
434 	/* Print the top-level interface type. */
435 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
436 	     desc++) {
437 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
438 			break;
439 	}
440 	if (desc->ifmt_string == NULL)
441 		printf("<unknown type> ");
442 	else
443 		printf("%s ", desc->ifmt_string);
444 
445 	/* Print the subtype. */
446 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
447 	     desc++) {
448 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
449 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
450 			break;
451 	}
452 	if (desc->ifmt_string == NULL)
453 		printf("<unknown subtype>");
454 	else
455 		printf("%s", desc->ifmt_string);
456 
457 	/* Print any options. */
458 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
459 	     desc++) {
460 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
461 		    (ifmw & desc->ifmt_word) != 0 &&
462 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
463 			if (seen_option == 0)
464 				printf(" <");
465 			printf("%s%s", seen_option ? "," : "",
466 			    desc->ifmt_string);
467 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
468 		}
469 	}
470 	printf("%s\n", seen_option ? ">" : "");
471 }
472 
473 #endif /* IFMEDIA_DEBUG */
474