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