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