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