]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_media.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / net / if_media.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ff6e181a
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
1c79356b 12 *
ff6e181a
A
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ff6e181a
A
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
1c79356b
A
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
9bccf70c 24/* $FreeBSD: src/sys/net/if_media.c,v 1.9.2.4 2001/07/04 00:12:38 brooks Exp $ */
1c79356b
A
25
26/*
27 * Copyright (c) 1997
28 * Jonathan Stone and Jason R. Thorpe. All rights reserved.
29 *
30 * This software is derived from information provided by Matt Thomas.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by Jonathan Stone
43 * and Jason R. Thorpe for the NetBSD Project.
44 * 4. The names of the authors may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
52 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
54 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59
60/*
61 * BSD/OS-compatible network interface media selection.
62 *
63 * Where it is safe to do so, this code strays slightly from the BSD/OS
64 * design. Software which uses the API (device drivers, basically)
65 * shouldn't notice any difference.
66 *
67 * Many thanks to Matt Thomas for providing the information necessary
68 * to implement this interface.
69 */
70
71#include <sys/param.h>
72#include <sys/systm.h>
73#include <sys/socket.h>
74#include <sys/sockio.h>
75#include <sys/malloc.h>
76
77#include <net/if.h>
78#include <net/if_media.h>
79
80/*
81 * Compile-time options:
82 * IFMEDIA_DEBUG:
83 * turn on implementation-level debug printfs.
84 * Useful for debugging newly-ported drivers.
85 */
86
91447636
A
87static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
88 int flags, int mask);
1c79356b
A
89
90#ifdef IFMEDIA_DEBUG
91int ifmedia_debug = 0;
91447636 92static void ifmedia_printword(int);
1c79356b
A
93#endif
94
95/*
96 * Initialize if_media struct for a specific interface instance.
97 */
98void
99ifmedia_init(ifm, dontcare_mask, change_callback, status_callback)
100 struct ifmedia *ifm;
101 int dontcare_mask;
102 ifm_change_cb_t change_callback;
103 ifm_stat_cb_t status_callback;
104{
105
106 LIST_INIT(&ifm->ifm_list);
107 ifm->ifm_cur = NULL;
108 ifm->ifm_media = 0;
109 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
110 ifm->ifm_change = change_callback;
111 ifm->ifm_status = status_callback;
112}
113
9bccf70c
A
114void
115ifmedia_removeall(ifm)
116 struct ifmedia *ifm;
117{
118 struct ifmedia_entry *entry;
119
120 for (entry = LIST_FIRST(&ifm->ifm_list); entry;
121 entry = LIST_FIRST(&ifm->ifm_list)) {
122 LIST_REMOVE(entry, ifm_list);
123 FREE(entry, M_IFADDR);
124 }
125}
126
1c79356b
A
127/*
128 * Add a media configuration to the list of supported media
129 * for a specific interface instance.
130 */
131void
132ifmedia_add(ifm, mword, data, aux)
133 struct ifmedia *ifm;
134 int mword;
135 int data;
136 void *aux;
137{
138 register struct ifmedia_entry *entry;
139
140#ifdef IFMEDIA_DEBUG
141 if (ifmedia_debug) {
142 if (ifm == NULL) {
143 printf("ifmedia_add: null ifm\n");
144 return;
145 }
146 printf("Adding entry for ");
147 ifmedia_printword(mword);
148 }
149#endif
150
9bccf70c 151 entry = _MALLOC(sizeof(*entry), M_IFADDR, M_NOWAIT);
1c79356b
A
152 if (entry == NULL)
153 panic("ifmedia_add: can't malloc entry");
154
155 entry->ifm_media = mword;
156 entry->ifm_data = data;
157 entry->ifm_aux = aux;
158
159 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
160}
161
162/*
163 * Add an array of media configurations to the list of
164 * supported media for a specific interface instance.
165 */
166void
167ifmedia_list_add(ifm, lp, count)
168 struct ifmedia *ifm;
169 struct ifmedia_entry *lp;
170 int count;
171{
172 int i;
173
174 for (i = 0; i < count; i++)
175 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
176 lp[i].ifm_aux);
177}
178
179/*
180 * Set the default active media.
181 *
182 * Called by device-specific code which is assumed to have already
183 * selected the default media in hardware. We do _not_ call the
184 * media-change callback.
185 */
186void
187ifmedia_set(ifm, target)
188 struct ifmedia *ifm;
189 int target;
190
191{
192 struct ifmedia_entry *match;
193
194 match = ifmedia_match(ifm, target, ifm->ifm_mask);
195
196 if (match == NULL) {
197 printf("ifmedia_set: no match for 0x%x/0x%x\n",
198 target, ~ifm->ifm_mask);
199 panic("ifmedia_set");
200 }
201 ifm->ifm_cur = match;
202
203#ifdef IFMEDIA_DEBUG
204 if (ifmedia_debug) {
205 printf("ifmedia_set: target ");
206 ifmedia_printword(target);
207 printf("ifmedia_set: setting to ");
208 ifmedia_printword(ifm->ifm_cur->ifm_media);
209 }
210#endif
211}
212
213/*
214 * Device-independent media ioctl support function.
215 */
216int
91447636
A
217ifmedia_ioctl(
218 struct ifnet *ifp,
219 struct ifreq *ifr,
220 struct ifmedia *ifm,
221 u_long cmd)
1c79356b
A
222{
223 struct ifmedia_entry *match;
224 struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
225 int error = 0, sticky;
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 int oldmedia;
239 int 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(
246 "ifmedia_ioctl: no media found for 0x%x\n",
247 newmedia);
248 }
249#endif
250 return (ENXIO);
251 }
252
253 /*
254 * If no change, we're done.
255 * XXX Automedia may invole software intervention.
256 * Keep going in case the the connected media changed.
257 * Similarly, if best match changed (kernel debugger?).
258 */
259 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
260 (newmedia == ifm->ifm_media) &&
261 (match == ifm->ifm_cur))
262 return 0;
263
264 /*
265 * We found a match, now make the driver switch to it.
266 * Make sure to preserve our old media type in case the
267 * driver can't switch.
268 */
269#ifdef IFMEDIA_DEBUG
270 if (ifmedia_debug) {
271 printf("ifmedia_ioctl: switching %s to ",
272 ifp->if_xname);
273 ifmedia_printword(match->ifm_media);
274 }
275#endif
276 oldentry = ifm->ifm_cur;
277 oldmedia = ifm->ifm_media;
278 ifm->ifm_cur = match;
279 ifm->ifm_media = newmedia;
280 error = (*ifm->ifm_change)(ifp);
281 if (error) {
282 ifm->ifm_cur = oldentry;
283 ifm->ifm_media = oldmedia;
284 }
285 break;
286 }
287
288 /*
289 * Get list of available media and current media on interface.
290 */
291 case SIOCGIFMEDIA:
292 {
293 struct ifmedia_entry *ep;
294 int *kptr, count;
9bccf70c 295 int usermax; /* user requested max */
1c79356b
A
296
297 kptr = NULL; /* XXX gcc */
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 count = 0;
9bccf70c
A
306 usermax = 0;
307
308 /*
309 * If there are more interfaces on the list, count
310 * them. This allows the caller to set ifmr->ifm_count
311 * to 0 on the first call to know how much space to
312 * allocate.
313 */
314 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list)
315 usermax++;
316
317 /*
318 * Don't allow the user to ask for too many
319 * or a negative number.
320 */
321 if (ifmr->ifm_count > usermax)
322 ifmr->ifm_count = usermax;
323 else if (ifmr->ifm_count < 0)
324 return (EINVAL);
1c79356b
A
325
326 if (ifmr->ifm_count != 0) {
327 kptr = (int *) _MALLOC(ifmr->ifm_count * sizeof(int),
328 M_TEMP, M_WAITOK);
329
330 /*
331 * Get the media words from the interface's list.
332 */
9bccf70c 333 ep = LIST_FIRST(&ifm->ifm_list);
1c79356b 334 for (; ep != NULL && count < ifmr->ifm_count;
9bccf70c 335 ep = LIST_NEXT(ep, ifm_list), count++)
1c79356b
A
336 kptr[count] = ep->ifm_media;
337
338 if (ep != NULL)
339 error = E2BIG; /* oops! */
9bccf70c
A
340 } else {
341 count = usermax;
1c79356b
A
342 }
343
1c79356b
A
344 /*
345 * We do the copyout on E2BIG, because that's
346 * just our way of telling userland that there
347 * are more. This is the behavior I've observed
348 * under BSD/OS 3.0
349 */
350 sticky = error;
351 if ((error == 0 || error == E2BIG) && ifmr->ifm_count != 0) {
352 error = copyout((caddr_t)kptr,
91447636 353 CAST_USER_ADDR_T(ifmr->ifm_ulist),
1c79356b
A
354 ifmr->ifm_count * sizeof(int));
355 }
356
357 if (error == 0)
358 error = sticky;
359
360 if (ifmr->ifm_count != 0)
361 FREE(kptr, M_TEMP);
362
363 ifmr->ifm_count = count;
364 break;
365 }
366
367 default:
368 return (EINVAL);
369 }
370
371 return (error);
372}
373
374/*
375 * Find media entry matching a given ifm word.
376 *
377 */
378static struct ifmedia_entry *
379ifmedia_match(ifm, target, mask)
380 struct ifmedia *ifm;
381 int target;
382 int mask;
383{
384 struct ifmedia_entry *match, *next;
385
386 match = NULL;
387 mask = ~mask;
388
9bccf70c 389 LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
1c79356b
A
390 if ((next->ifm_media & mask) == (target & mask)) {
391#if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
392 if (match) {
393 printf("ifmedia_match: multiple match for "
394 "0x%x/0x%x\n", target, mask);
395 }
396#endif
397 match = next;
398 }
399 }
400
401 return match;
402}
403
404#ifdef IFMEDIA_DEBUG
405struct ifmedia_description ifm_type_descriptions[] =
406 IFM_TYPE_DESCRIPTIONS;
407
408struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
409 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
410
411struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
412 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
413
414struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
415 IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
416
417struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
418 IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
419
420struct ifmedia_description ifm_subtype_fddi_descriptions[] =
421 IFM_SUBTYPE_FDDI_DESCRIPTIONS;
422
423struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
424 IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
425
426struct ifmedia_description ifm_subtype_80211_descriptions[] =
427 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
428
429struct ifmedia_description ifm_subtype_80211_option_descriptions[] =
430 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
431
432struct ifmedia_description ifm_subtype_shared_descriptions[] =
433 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
434
435struct ifmedia_description ifm_shared_option_descriptions[] =
436 IFM_SHARED_OPTION_DESCRIPTIONS;
437
438struct ifmedia_type_to_subtype {
439 struct ifmedia_description *subtypes;
440 struct ifmedia_description *options;
441};
442
443/* must be in the same order as IFM_TYPE_DESCRIPTIONS */
444struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
445 {
446 &ifm_subtype_ethernet_descriptions[0],
447 &ifm_subtype_ethernet_option_descriptions[0]
448 },
449 {
450 &ifm_subtype_tokenring_descriptions[0],
451 &ifm_subtype_tokenring_option_descriptions[0]
452 },
453 {
454 &ifm_subtype_fddi_descriptions[0],
455 &ifm_subtype_fddi_option_descriptions[0]
456 },
457 {
458 &ifm_subtype_80211_descriptions[0],
459 &ifm_subtype_80211_option_descriptions[0]
460 },
461};
462
463/*
464 * print a media word.
465 */
466static void
467ifmedia_printword(ifmw)
468 int ifmw;
469{
470 struct ifmedia_description *desc;
471 struct ifmedia_type_to_subtype *ttos;
472 int seen_option = 0;
473
474 /* Find the top-level interface type. */
475 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
476 desc->ifmt_string != NULL; desc++, ttos++)
477 if (IFM_TYPE(ifmw) == desc->ifmt_word)
478 break;
479 if (desc->ifmt_string == NULL) {
480 printf("<unknown type>\n");
481 return;
482 }
483 printf(desc->ifmt_string);
484
485 /*
486 * Check for the shared subtype descriptions first, then the
487 * type-specific ones.
488 */
489 for (desc = ifm_subtype_shared_descriptions;
490 desc->ifmt_string != NULL; desc++)
491 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
492 goto got_subtype;
493
494 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
495 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
496 break;
497 if (desc->ifmt_string == NULL) {
498 printf(" <unknown subtype>\n");
499 return;
500 }
501
502 got_subtype:
503 printf(" %s", desc->ifmt_string);
504
505 /*
506 * Look for shared options.
507 */
508 for (desc = ifm_shared_option_descriptions;
509 desc->ifmt_string != NULL; desc++) {
510 if (ifmw & desc->ifmt_word) {
511 if (seen_option == 0)
512 printf(" <");
513 printf("%s%s", seen_option++ ? "," : "",
514 desc->ifmt_string);
515 }
516 }
517
518 /*
519 * Look for subtype-specific options.
520 */
521 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
522 if (ifmw & desc->ifmt_word) {
523 if (seen_option == 0)
524 printf(" <");
525 printf("%s%s", seen_option++ ? "," : "",
526 desc->ifmt_string);
527 }
528 }
529 printf("%s\n", seen_option ? ">" : "");
530}
531#endif /* IFMEDIA_DEBUG */