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