]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_media.c
OpenSSL: update to 3.0.10
[FreeBSD/FreeBSD.git] / sys / net / if_media.c
1 /*      $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $     */
2
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 1997
7  *      Jonathan Stone and Jason R. Thorpe.  All rights reserved.
8  *
9  * This software is derived from information provided by Matt Thomas.
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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by Jonathan Stone
22  *      and Jason R. Thorpe for the NetBSD Project.
23  * 4. The names of the authors may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38
39 /*
40  * BSD/OS-compatible network interface media selection.
41  *
42  * Where it is safe to do so, this code strays slightly from the BSD/OS
43  * design.  Software which uses the API (device drivers, basically)
44  * shouldn't notice any difference.
45  *
46  * Many thanks to Matt Thomas for providing the information necessary
47  * to implement this interface.
48  */
49
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
52
53 #include "opt_ifmedia.h"
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/socket.h>
58 #include <sys/sockio.h>
59 #include <sys/malloc.h>
60 #include <sys/module.h>
61 #include <sys/sysctl.h>
62
63 #include <net/if.h>
64 #include <net/if_media.h>
65
66 /*
67  * Compile-time options:
68  * IFMEDIA_DEBUG:
69  *      turn on implementation-level debug printfs.
70  *      Useful for debugging newly-ported  drivers.
71  */
72
73 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
74     int flags, int mask);
75
76 #ifdef IFMEDIA_DEBUG
77 #include <net/if_var.h>
78 #include <net/if_private.h>
79 int     ifmedia_debug = 0;
80 SYSCTL_INT(_debug, OID_AUTO, ifmedia, CTLFLAG_RW, &ifmedia_debug,
81             0, "if_media debugging msgs");
82 static  void ifmedia_printword(int);
83 #endif
84
85 /*
86  * Initialize if_media struct for a specific interface instance.
87  */
88 void
89 ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
90     ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
91 {
92
93         LIST_INIT(&ifm->ifm_list);
94         ifm->ifm_cur = NULL;
95         ifm->ifm_media = 0;
96         ifm->ifm_mask = dontcare_mask;          /* IF don't-care bits */
97         ifm->ifm_change = change_callback;
98         ifm->ifm_status = status_callback;
99 }
100
101 void
102 ifmedia_removeall(struct ifmedia *ifm)
103 {
104         struct ifmedia_entry *entry;
105
106         while ((entry = LIST_FIRST(&ifm->ifm_list)) != NULL) {
107                 LIST_REMOVE(entry, ifm_list);
108                 free(entry, M_IFADDR);
109         }
110         ifm->ifm_cur = NULL;
111 }
112
113 /*
114  * Add a media configuration to the list of supported media
115  * for a specific interface instance.
116  */
117 void
118 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
119 {
120         struct ifmedia_entry *entry;
121
122 #ifdef IFMEDIA_DEBUG
123         if (ifmedia_debug) {
124                 if (ifm == NULL) {
125                         printf("ifmedia_add: null ifm\n");
126                         return;
127                 }
128                 printf("Adding entry for (%#010x) ", mword);
129                 ifmedia_printword(mword);
130         }
131 #endif
132
133         entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
134         if (entry == NULL)
135                 panic("ifmedia_add: can't malloc entry");
136
137         entry->ifm_media = mword;
138         entry->ifm_data = data;
139         entry->ifm_aux = aux;
140
141         LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
142 }
143
144 /*
145  * Add an array of media configurations to the list of
146  * supported media for a specific interface instance.
147  */
148 void
149 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count)
150 {
151         int i;
152
153         for (i = 0; i < count; i++)
154                 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
155                     lp[i].ifm_aux);
156 }
157
158 /*
159  * Set the default active media. 
160  *
161  * Called by device-specific code which is assumed to have already
162  * selected the default media in hardware.  We do _not_ call the
163  * media-change callback.
164  */
165 void
166 ifmedia_set(struct ifmedia *ifm, int target)
167 {
168         struct ifmedia_entry *match;
169
170         match = ifmedia_match(ifm, target, ifm->ifm_mask);
171
172         if (match == NULL) {
173                 printf("ifmedia_set: no match for 0x%x/0x%x\n",
174                     target, ~ifm->ifm_mask);
175                 panic("ifmedia_set");
176         }
177         ifm->ifm_cur = match;
178
179 #ifdef IFMEDIA_DEBUG
180         if (ifmedia_debug) {
181                 printf("ifmedia_set: target ");
182                 ifmedia_printword(target);
183                 printf("ifmedia_set: setting to ");
184                 ifmedia_printword(ifm->ifm_cur->ifm_media);
185         }
186 #endif
187 }
188
189 /*
190  * Given a media word, return one suitable for an application
191  * using the original encoding.
192  */
193 static int
194 compat_media(int media)
195 {
196
197         if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
198                 media &= ~(IFM_ETH_XTYPE|IFM_TMASK);
199                 media |= IFM_OTHER;
200         }
201         return (media);
202 }
203
204 /*
205  * Device-independent media ioctl support function.
206  */
207 int
208 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
209     u_long cmd)
210 {
211         struct ifmedia_entry *match;
212         struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
213         int error = 0;
214
215         if (ifp == NULL || ifr == NULL || ifm == NULL)
216                 return (EINVAL);
217
218         switch (cmd) {
219         /*
220          * Set the current media.
221          */
222         case  SIOCSIFMEDIA:
223         {
224                 struct ifmedia_entry *oldentry;
225                 int oldmedia;
226                 int newmedia = ifr->ifr_media;
227
228                 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
229                 if (match == NULL) {
230 #ifdef IFMEDIA_DEBUG
231                         if (ifmedia_debug) {
232                                 printf(
233                     "ifmedia_ioctl: no media found for %#010x mask %#010x\n", 
234                                     newmedia, ifm->ifm_mask);
235                         }
236 #endif
237                         return (ENXIO);
238                 }
239
240                 /*
241                  * If no change, we're done.
242                  * XXX Automedia may invole software intervention.
243                  *     Keep going in case the connected media changed.
244                  *     Similarly, if best match changed (kernel debugger?).
245                  */
246                 if (IFM_SUBTYPE(newmedia) != IFM_AUTO &&
247                     newmedia == ifm->ifm_media && match == ifm->ifm_cur)
248                         return (0);
249
250                 /*
251                  * We found a match, now make the driver switch to it.
252                  * Make sure to preserve our old media type in case the
253                  * driver can't switch.
254                  */
255 #ifdef IFMEDIA_DEBUG
256                 if (ifmedia_debug) {
257                         printf("ifmedia_ioctl: switching %s to ",
258                             ifp->if_xname);
259                         ifmedia_printword(match->ifm_media);
260                 }
261 #endif
262                 oldentry = ifm->ifm_cur;
263                 oldmedia = ifm->ifm_media;
264                 ifm->ifm_cur = match;
265                 ifm->ifm_media = newmedia;
266                 error = (*ifm->ifm_change)(ifp);
267                 if (error) {
268                         ifm->ifm_cur = oldentry;
269                         ifm->ifm_media = oldmedia;
270                 }
271                 break;
272         }
273
274         /*
275          * Get list of available media and current media on interface.
276          */
277         case  SIOCGIFMEDIA: 
278         case  SIOCGIFXMEDIA: 
279         {
280                 struct ifmedia_entry *ep;
281                 int i;
282
283                 if (ifmr->ifm_count < 0)
284                         return (EINVAL);
285
286                 if (cmd == SIOCGIFMEDIA) {
287                         ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
288                             compat_media(ifm->ifm_cur->ifm_media) : IFM_NONE;
289                 } else {
290                         ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
291                             ifm->ifm_cur->ifm_media : IFM_NONE;
292                 }
293                 ifmr->ifm_mask = ifm->ifm_mask;
294                 ifmr->ifm_status = 0;
295                 (*ifm->ifm_status)(ifp, ifmr);
296
297                 /*
298                  * If there are more interfaces on the list, count
299                  * them.  This allows the caller to set ifmr->ifm_count
300                  * to 0 on the first call to know how much space to
301                  * allocate.
302                  */
303                 i = 0;
304                 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list) {
305                         if (i < ifmr->ifm_count) {
306                                 error = copyout(&ep->ifm_media,
307                                     ifmr->ifm_ulist + i, sizeof(int));
308                                 if (error != 0)
309                                         break;
310                         }
311                         i++;
312                 }
313                 if (error == 0 && i > ifmr->ifm_count)
314                         error = ifmr->ifm_count != 0 ? E2BIG : 0;
315                 ifmr->ifm_count = i;
316                 break;
317         }
318
319         default:
320                 return (EINVAL);
321         }
322
323         return (error);
324 }
325
326 /*
327  * Find media entry matching a given ifm word.
328  *
329  */
330 static struct ifmedia_entry *
331 ifmedia_match(struct ifmedia *ifm, int target, int mask)
332 {
333         struct ifmedia_entry *match, *next;
334
335         match = NULL;
336         mask = ~mask;
337
338         LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
339                 if ((next->ifm_media & mask) == (target & mask)) {
340 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
341                         if (match) {
342                                 printf("ifmedia_match: multiple match for "
343                                     "%#010x/%#010x\n", target, mask);
344                         }
345 #endif
346                         match = next;
347                 }
348         }
349
350         return (match);
351 }
352
353 /*
354  * Compute the interface `baudrate' from the media, for the interface
355  * metrics (used by routing daemons).
356  */
357 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =   
358     IFM_BAUDRATE_DESCRIPTIONS;
359
360 uint64_t
361 ifmedia_baudrate(int mword)
362 {
363         int i;
364
365         for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
366                 if (IFM_TYPE_MATCH(mword, ifmedia_baudrate_descriptions[i].
367                     ifmb_word))
368                         return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
369         }
370
371         /* Not known. */
372         return (0);
373 }
374
375 #ifdef IFMEDIA_DEBUG
376 static const struct ifmedia_description ifm_type_descriptions[] =
377     IFM_TYPE_DESCRIPTIONS;
378
379 static const struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
380     IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
381
382 static const struct ifmedia_description
383     ifm_subtype_ethernet_option_descriptions[] =
384     IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
385
386 static const struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
387     IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
388
389 static const struct ifmedia_description
390     ifm_subtype_ieee80211_option_descriptions[] =
391     IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
392
393 static const struct ifmedia_description
394     ifm_subtype_ieee80211_mode_descriptions[] =
395     IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
396
397 static const struct ifmedia_description ifm_subtype_atm_descriptions[] =
398     IFM_SUBTYPE_ATM_DESCRIPTIONS;
399
400 static const struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
401     IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
402
403 static const struct ifmedia_description ifm_subtype_shared_descriptions[] =
404     IFM_SUBTYPE_SHARED_DESCRIPTIONS;
405
406 static const struct ifmedia_description ifm_shared_option_descriptions[] =
407     IFM_SHARED_OPTION_DESCRIPTIONS;
408
409 struct ifmedia_type_to_subtype {
410         const struct ifmedia_description *subtypes;
411         const struct ifmedia_description *options;
412         const struct ifmedia_description *modes;
413 };
414
415 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
416 static const struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
417         {
418           &ifm_subtype_ethernet_descriptions[0],
419           &ifm_subtype_ethernet_option_descriptions[0],
420           NULL,
421         },
422         {
423           &ifm_subtype_ieee80211_descriptions[0],
424           &ifm_subtype_ieee80211_option_descriptions[0],
425           &ifm_subtype_ieee80211_mode_descriptions[0]
426         },
427         {
428           &ifm_subtype_atm_descriptions[0],
429           &ifm_subtype_atm_option_descriptions[0],
430           NULL,
431         },
432 };
433
434 /*
435  * print a media word.
436  */
437 static void
438 ifmedia_printword(int ifmw)
439 {
440         const struct ifmedia_description *desc;
441         const struct ifmedia_type_to_subtype *ttos;
442         int seen_option = 0;
443
444         /* Find the top-level interface type. */
445         for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
446             desc->ifmt_string != NULL; desc++, ttos++)
447                 if (IFM_TYPE(ifmw) == desc->ifmt_word)
448                         break;
449         if (desc->ifmt_string == NULL) {
450                 printf("<unknown type>\n");
451                 return;
452         }
453         printf("%s", desc->ifmt_string);
454
455         /* Any mode. */
456         for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++)
457                 if (IFM_MODE(ifmw) == desc->ifmt_word) {
458                         if (desc->ifmt_string != NULL)
459                                 printf(" mode %s", desc->ifmt_string);
460                         break;
461                 }
462
463         /*
464          * Check for the shared subtype descriptions first, then the
465          * type-specific ones.
466          */
467         for (desc = ifm_subtype_shared_descriptions;
468             desc->ifmt_string != NULL; desc++)
469                 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
470                         goto got_subtype;
471
472         for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
473                 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
474                         break;
475         if (desc->ifmt_string == NULL) {
476                 printf(" <unknown subtype>\n");
477                 return;
478         }
479
480  got_subtype:
481         printf(" %s", desc->ifmt_string);
482
483         /*
484          * Look for shared options.
485          */
486         for (desc = ifm_shared_option_descriptions;
487             desc->ifmt_string != NULL; desc++) {
488                 if (ifmw & desc->ifmt_word) {
489                         if (seen_option == 0)
490                                 printf(" <");
491                         printf("%s%s", seen_option++ ? "," : "",
492                             desc->ifmt_string);
493                 }
494         }
495
496         /*
497          * Look for subtype-specific options.
498          */
499         for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
500                 if (ifmw & desc->ifmt_word) {
501                         if (seen_option == 0)
502                                 printf(" <");
503                         printf("%s%s", seen_option++ ? "," : "",
504                             desc->ifmt_string); 
505                 }
506         }
507         printf("%s\n", seen_option ? ">" : "");
508 }
509 #endif /* IFMEDIA_DEBUG */