ieee80211.c revision 1.24 1 /* $NetBSD: ieee80211.c,v 1.24 2004/07/23 05:33:41 mycroft Exp $ */
2 /*-
3 * Copyright (c) 2001 Atsushi Onoe
4 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2 as published by the Free
20 * Software Foundation.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #ifdef __FreeBSD__
36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211.c,v 1.11 2004/04/02 20:19:20 sam Exp $");
37 #else
38 __KERNEL_RCSID(0, "$NetBSD: ieee80211.c,v 1.24 2004/07/23 05:33:41 mycroft Exp $");
39 #endif
40
41 /*
42 * IEEE 802.11 generic handler
43 */
44
45 #include "opt_inet.h"
46 #include "bpfilter.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/mbuf.h>
51 #include <sys/malloc.h>
52 #include <sys/kernel.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/endian.h>
56 #include <sys/errno.h>
57 #ifdef __FreeBSD__
58 #include <sys/bus.h>
59 #endif
60 #include <sys/proc.h>
61 #include <sys/sysctl.h>
62
63 #ifdef __FreeBSD__
64 #include <machine/atomic.h>
65 #endif
66
67 #include <net/if.h>
68 #include <net/if_dl.h>
69 #include <net/if_media.h>
70 #include <net/if_arp.h>
71 #ifdef __FreeBSD__
72 #include <net/ethernet.h>
73 #else
74 #include <net/if_ether.h>
75 #endif
76 #include <net/if_llc.h>
77
78 #include <net80211/ieee80211_var.h>
79 #include <net80211/ieee80211_compat.h>
80 #include <net80211/ieee80211_sysctl.h>
81
82 #include <net/bpf.h>
83
84 #ifdef INET
85 #include <netinet/in.h>
86 #ifdef __FreeBSD__
87 #include <netinet/if_ether.h>
88 #else
89 #include <net/if_ether.h>
90 #endif
91 #endif
92
93 #ifdef IEEE80211_DEBUG
94 int ieee80211_debug = 0;
95 #ifdef __NetBSD__
96 static int ieee80211_debug_nodenum;
97 #endif /* __NetBSD__ */
98
99 #ifdef __FreeBSD__
100 SYSCTL_INT(_debug, OID_AUTO, ieee80211, CTLFLAG_RW, &ieee80211_debug,
101 0, "IEEE 802.11 media debugging printfs");
102 #endif
103 #endif
104
105 int ieee80211_inact_max = IEEE80211_INACT_MAX;
106 static int ieee80211_inact_max_nodenum;
107
108 struct ieee80211com_head ieee80211com_head =
109 LIST_HEAD_INITIALIZER(ieee80211com_head);
110
111 static void ieee80211_setbasicrates(struct ieee80211com *);
112
113 #ifdef __NetBSD__
114 static void sysctl_ieee80211_fill_node(struct ieee80211_node *,
115 struct ieee80211_node_sysctl *, int, struct ieee80211_channel *, int);
116 static struct ieee80211_node *ieee80211_node_walknext(
117 struct ieee80211_node_walk *);
118 static struct ieee80211_node *ieee80211_node_walkfirst(
119 struct ieee80211_node_walk *, u_short);
120 static int sysctl_ieee80211_verify(SYSCTLFN_ARGS);
121 static int sysctl_ieee80211_node(SYSCTLFN_ARGS);
122 #endif /* __NetBSD__ */
123
124 #define LOGICALLY_EQUAL(x, y) (!(x) == !(y))
125
126 static const char *ieee80211_phymode_name[] = {
127 "auto", /* IEEE80211_MODE_AUTO */
128 "11a", /* IEEE80211_MODE_11A */
129 "11b", /* IEEE80211_MODE_11B */
130 "11g", /* IEEE80211_MODE_11G */
131 "FH", /* IEEE80211_MODE_FH */
132 "turbo", /* IEEE80211_MODE_TURBO */
133 };
134
135 void
136 ieee80211_ifattach(struct ifnet *ifp)
137 {
138 struct ieee80211com *ic = (void *)ifp;
139 struct ieee80211_channel *c;
140 int i;
141
142 ether_ifattach(ifp, ic->ic_myaddr);
143 #if NBPFILTER > 0
144 bpfattach2(ifp, DLT_IEEE802_11,
145 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
146 #endif
147 ieee80211_crypto_attach(ifp);
148
149 /*
150 * Fill in 802.11 available channel set, mark
151 * all available channels as active, and pick
152 * a default channel if not already specified.
153 */
154 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
155 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
156 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
157 c = &ic->ic_channels[i];
158 if (c->ic_flags) {
159 /*
160 * Verify driver passed us valid data.
161 */
162 if (i != ieee80211_chan2ieee(ic, c)) {
163 if_printf(ifp, "bad channel ignored; "
164 "freq %u flags %x number %u\n",
165 c->ic_freq, c->ic_flags, i);
166 c->ic_flags = 0; /* NB: remove */
167 continue;
168 }
169 setbit(ic->ic_chan_avail, i);
170 /*
171 * Identify mode capabilities.
172 */
173 if (IEEE80211_IS_CHAN_A(c))
174 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
175 if (IEEE80211_IS_CHAN_B(c))
176 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
177 if (IEEE80211_IS_CHAN_PUREG(c))
178 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
179 if (IEEE80211_IS_CHAN_FHSS(c))
180 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
181 if (IEEE80211_IS_CHAN_T(c))
182 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO;
183 }
184 }
185 /* validate ic->ic_curmode */
186 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
187 ic->ic_curmode = IEEE80211_MODE_AUTO;
188 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
189
190 ieee80211_setbasicrates(ic);
191 (void) ieee80211_setmode(ic, ic->ic_curmode);
192
193 if (ic->ic_lintval == 0)
194 ic->ic_lintval = 100; /* default sleep */
195 ic->ic_bmisstimeout = 7*ic->ic_lintval; /* default 7 beacons */
196
197 LIST_INSERT_HEAD(&ieee80211com_head, ic, ic_list);
198 ieee80211_node_attach(ifp);
199 ieee80211_proto_attach(ifp);
200 }
201
202 void
203 ieee80211_ifdetach(struct ifnet *ifp)
204 {
205 struct ieee80211com *ic = (void *)ifp;
206
207 ieee80211_proto_detach(ifp);
208 ieee80211_crypto_detach(ifp);
209 ieee80211_node_detach(ifp);
210 LIST_REMOVE(ic, ic_list);
211 #ifdef __FreeBSD__
212 ifmedia_removeall(&ic->ic_media);
213 #else
214 ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY);
215 #endif
216 #if NBPFILTER > 0
217 bpfdetach(ifp);
218 #endif
219 ether_ifdetach(ifp);
220 }
221
222 /*
223 * Convert MHz frequency to IEEE channel number.
224 */
225 u_int
226 ieee80211_mhz2ieee(u_int freq, u_int flags)
227 {
228 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
229 if (freq == 2484)
230 return 14;
231 if (freq < 2484)
232 return (freq - 2407) / 5;
233 else
234 return 15 + ((freq - 2512) / 20);
235 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
236 return (freq - 5000) / 5;
237 } else { /* either, guess */
238 if (freq == 2484)
239 return 14;
240 if (freq < 2484)
241 return (freq - 2407) / 5;
242 if (freq < 5000)
243 return 15 + ((freq - 2512) / 20);
244 return (freq - 5000) / 5;
245 }
246 }
247
248 /*
249 * Convert channel to IEEE channel number.
250 */
251 u_int
252 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
253 {
254 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
255 return c - ic->ic_channels;
256 else if (c == IEEE80211_CHAN_ANYC)
257 return IEEE80211_CHAN_ANY;
258 else if (c != NULL) {
259 if_printf(&ic->ic_if, "invalid channel freq %u flags %x\n",
260 c->ic_freq, c->ic_flags);
261 return 0; /* XXX */
262 } else {
263 if_printf(&ic->ic_if, "invalid channel (NULL)\n");
264 return 0; /* XXX */
265 }
266 }
267
268 /*
269 * Convert IEEE channel number to MHz frequency.
270 */
271 u_int
272 ieee80211_ieee2mhz(u_int chan, u_int flags)
273 {
274 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
275 if (chan == 14)
276 return 2484;
277 if (chan < 14)
278 return 2407 + chan*5;
279 else
280 return 2512 + ((chan-15)*20);
281 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
282 return 5000 + (chan*5);
283 } else { /* either, guess */
284 if (chan == 14)
285 return 2484;
286 if (chan < 14) /* 0-13 */
287 return 2407 + chan*5;
288 if (chan < 27) /* 15-26 */
289 return 2512 + ((chan-15)*20);
290 return 5000 + (chan*5);
291 }
292 }
293
294 /*
295 * Setup the media data structures according to the channel and
296 * rate tables. This must be called by the driver after
297 * ieee80211_attach and before most anything else.
298 */
299 void
300 ieee80211_media_init(struct ifnet *ifp,
301 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
302 {
303 #define ADD(_ic, _s, _o) \
304 ifmedia_add(&(_ic)->ic_media, \
305 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
306 struct ieee80211com *ic = (void *)ifp;
307 struct ifmediareq imr;
308 int i, j, mode, rate, maxrate, mword, mopt, r;
309 struct ieee80211_rateset *rs;
310 struct ieee80211_rateset allrates;
311
312 /*
313 * Do late attach work that must wait for any subclass
314 * (i.e. driver) work such as overriding methods.
315 */
316 ieee80211_node_lateattach(ifp);
317
318 /*
319 * Fill in media characteristics.
320 */
321 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
322 maxrate = 0;
323 memset(&allrates, 0, sizeof(allrates));
324 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
325 static const u_int mopts[] = {
326 IFM_AUTO,
327 IFM_IEEE80211_11A,
328 IFM_IEEE80211_11B,
329 IFM_IEEE80211_11G,
330 IFM_IEEE80211_FH,
331 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
332 };
333 if ((ic->ic_modecaps & (1<<mode)) == 0)
334 continue;
335 mopt = mopts[mode];
336 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
337 if (ic->ic_caps & IEEE80211_C_IBSS)
338 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
339 if (ic->ic_caps & IEEE80211_C_HOSTAP)
340 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
341 if (ic->ic_caps & IEEE80211_C_AHDEMO)
342 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
343 if (ic->ic_caps & IEEE80211_C_MONITOR)
344 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
345 if (mode == IEEE80211_MODE_AUTO)
346 continue;
347 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
348 rs = &ic->ic_sup_rates[mode];
349 for (i = 0; i < rs->rs_nrates; i++) {
350 rate = rs->rs_rates[i];
351 mword = ieee80211_rate2media(ic, rate, mode);
352 if (mword == 0)
353 continue;
354 printf("%s%d%sMbps", (i != 0 ? " " : ""),
355 (rate & IEEE80211_RATE_VAL) / 2,
356 ((rate & 0x1) != 0 ? ".5" : ""));
357 ADD(ic, mword, mopt);
358 if (ic->ic_caps & IEEE80211_C_IBSS)
359 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
360 if (ic->ic_caps & IEEE80211_C_HOSTAP)
361 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
362 if (ic->ic_caps & IEEE80211_C_AHDEMO)
363 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
364 if (ic->ic_caps & IEEE80211_C_MONITOR)
365 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
366 /*
367 * Add rate to the collection of all rates.
368 */
369 r = rate & IEEE80211_RATE_VAL;
370 for (j = 0; j < allrates.rs_nrates; j++)
371 if (allrates.rs_rates[j] == r)
372 break;
373 if (j == allrates.rs_nrates) {
374 /* unique, add to the set */
375 allrates.rs_rates[j] = r;
376 allrates.rs_nrates++;
377 }
378 rate = (rate & IEEE80211_RATE_VAL) / 2;
379 if (rate > maxrate)
380 maxrate = rate;
381 }
382 printf("\n");
383 }
384 for (i = 0; i < allrates.rs_nrates; i++) {
385 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
386 IEEE80211_MODE_AUTO);
387 if (mword == 0)
388 continue;
389 mword = IFM_SUBTYPE(mword); /* remove media options */
390 ADD(ic, mword, 0);
391 if (ic->ic_caps & IEEE80211_C_IBSS)
392 ADD(ic, mword, IFM_IEEE80211_ADHOC);
393 if (ic->ic_caps & IEEE80211_C_HOSTAP)
394 ADD(ic, mword, IFM_IEEE80211_HOSTAP);
395 if (ic->ic_caps & IEEE80211_C_AHDEMO)
396 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
397 if (ic->ic_caps & IEEE80211_C_MONITOR)
398 ADD(ic, mword, IFM_IEEE80211_MONITOR);
399 }
400 ieee80211_media_status(ifp, &imr);
401 ifmedia_set(&ic->ic_media, imr.ifm_active);
402
403 if (maxrate)
404 ifp->if_baudrate = IF_Mbps(maxrate);
405
406 if (ic->ic_max_aid == 0)
407 ic->ic_max_aid = IEEE80211_AID_MAX;
408
409 #undef ADD
410 }
411
412 static int
413 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
414 {
415 #define IEEERATE(_ic,_m,_i) \
416 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
417 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
418 for (i = 0; i < nrates; i++)
419 if (IEEERATE(ic, mode, i) == rate)
420 return i;
421 return -1;
422 #undef IEEERATE
423 }
424
425 /*
426 * Handle a media change request.
427 */
428 int
429 ieee80211_media_change(struct ifnet *ifp)
430 {
431 struct ieee80211com *ic = (void *)ifp;
432 struct ifmedia_entry *ime;
433 enum ieee80211_opmode newopmode;
434 enum ieee80211_phymode newphymode;
435 int i, j, newrate, error = 0;
436
437 ime = ic->ic_media.ifm_cur;
438 /*
439 * First, identify the phy mode.
440 */
441 switch (IFM_MODE(ime->ifm_media)) {
442 case IFM_IEEE80211_11A:
443 newphymode = IEEE80211_MODE_11A;
444 break;
445 case IFM_IEEE80211_11B:
446 newphymode = IEEE80211_MODE_11B;
447 break;
448 case IFM_IEEE80211_11G:
449 newphymode = IEEE80211_MODE_11G;
450 break;
451 case IFM_IEEE80211_FH:
452 newphymode = IEEE80211_MODE_FH;
453 break;
454 case IFM_AUTO:
455 newphymode = IEEE80211_MODE_AUTO;
456 break;
457 default:
458 return EINVAL;
459 }
460 /*
461 * Turbo mode is an ``option''. Eventually it
462 * needs to be applied to 11g too.
463 */
464 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
465 if (newphymode != IEEE80211_MODE_11A)
466 return EINVAL;
467 newphymode = IEEE80211_MODE_TURBO;
468 }
469 /*
470 * Validate requested mode is available.
471 */
472 if ((ic->ic_modecaps & (1<<newphymode)) == 0)
473 return EINVAL;
474
475 /*
476 * Next, the fixed/variable rate.
477 */
478 i = -1;
479 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
480 /*
481 * Convert media subtype to rate.
482 */
483 newrate = ieee80211_media2rate(ime->ifm_media);
484 if (newrate == 0)
485 return EINVAL;
486 /*
487 * Check the rate table for the specified/current phy.
488 */
489 if (newphymode == IEEE80211_MODE_AUTO) {
490 /*
491 * In autoselect mode search for the rate.
492 */
493 for (j = IEEE80211_MODE_11A;
494 j < IEEE80211_MODE_MAX; j++) {
495 if ((ic->ic_modecaps & (1<<j)) == 0)
496 continue;
497 i = findrate(ic, j, newrate);
498 if (i != -1) {
499 /* lock mode too */
500 newphymode = j;
501 break;
502 }
503 }
504 } else {
505 i = findrate(ic, newphymode, newrate);
506 }
507 if (i == -1) /* mode/rate mismatch */
508 return EINVAL;
509 }
510 /* NB: defer rate setting to later */
511
512 /*
513 * Deduce new operating mode but don't install it just yet.
514 */
515 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
516 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
517 newopmode = IEEE80211_M_AHDEMO;
518 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
519 newopmode = IEEE80211_M_HOSTAP;
520 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
521 newopmode = IEEE80211_M_IBSS;
522 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
523 newopmode = IEEE80211_M_MONITOR;
524 else
525 newopmode = IEEE80211_M_STA;
526
527 /*
528 * Autoselect doesn't make sense when operating as an AP.
529 * If no phy mode has been selected, pick one and lock it
530 * down so rate tables can be used in forming beacon frames
531 * and the like.
532 */
533 if (newopmode == IEEE80211_M_HOSTAP &&
534 newphymode == IEEE80211_MODE_AUTO) {
535 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
536 if (ic->ic_modecaps & (1<<j)) {
537 newphymode = j;
538 break;
539 }
540 }
541
542 /*
543 * Handle phy mode change.
544 */
545 if (ic->ic_curmode != newphymode) { /* change phy mode */
546 error = ieee80211_setmode(ic, newphymode);
547 if (error != 0)
548 return error;
549 error = ENETRESET;
550 }
551
552 /*
553 * Committed to changes, install the rate setting.
554 */
555 if (ic->ic_fixed_rate != i) {
556 ic->ic_fixed_rate = i; /* set fixed tx rate */
557 error = ENETRESET;
558 }
559
560 /*
561 * Handle operating mode change.
562 */
563 if (ic->ic_opmode != newopmode) {
564 ic->ic_opmode = newopmode;
565 switch (newopmode) {
566 case IEEE80211_M_AHDEMO:
567 case IEEE80211_M_HOSTAP:
568 case IEEE80211_M_STA:
569 case IEEE80211_M_MONITOR:
570 ic->ic_flags &= ~IEEE80211_F_IBSSON;
571 break;
572 case IEEE80211_M_IBSS:
573 ic->ic_flags |= IEEE80211_F_IBSSON;
574 break;
575 }
576 error = ENETRESET;
577 }
578 #ifdef notdef
579 if (error == 0)
580 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
581 #endif
582 return error;
583 }
584
585 void
586 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
587 {
588 struct ieee80211com *ic = (void *)ifp;
589 struct ieee80211_node *ni = NULL;
590
591 imr->ifm_status = IFM_AVALID;
592 imr->ifm_active = IFM_IEEE80211;
593 if (ic->ic_state == IEEE80211_S_RUN)
594 imr->ifm_status |= IFM_ACTIVE;
595 imr->ifm_active |= IFM_AUTO;
596 switch (ic->ic_opmode) {
597 case IEEE80211_M_STA:
598 ni = ic->ic_bss;
599 /* calculate rate subtype */
600 imr->ifm_active |= ieee80211_rate2media(ic,
601 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
602 break;
603 case IEEE80211_M_IBSS:
604 imr->ifm_active |= IFM_IEEE80211_ADHOC;
605 break;
606 case IEEE80211_M_AHDEMO:
607 /* should not come here */
608 break;
609 case IEEE80211_M_HOSTAP:
610 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
611 break;
612 case IEEE80211_M_MONITOR:
613 imr->ifm_active |= IFM_IEEE80211_MONITOR;
614 break;
615 }
616 switch (ic->ic_curmode) {
617 case IEEE80211_MODE_11A:
618 imr->ifm_active |= IFM_IEEE80211_11A;
619 break;
620 case IEEE80211_MODE_11B:
621 imr->ifm_active |= IFM_IEEE80211_11B;
622 break;
623 case IEEE80211_MODE_11G:
624 imr->ifm_active |= IFM_IEEE80211_11G;
625 break;
626 case IEEE80211_MODE_FH:
627 imr->ifm_active |= IFM_IEEE80211_FH;
628 break;
629 case IEEE80211_MODE_TURBO:
630 imr->ifm_active |= IFM_IEEE80211_11A
631 | IFM_IEEE80211_TURBO;
632 break;
633 }
634 }
635
636 void
637 ieee80211_watchdog(struct ifnet *ifp)
638 {
639 struct ieee80211com *ic = (void *)ifp;
640
641 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
642 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
643 if (ic->ic_inact_timer && --ic->ic_inact_timer == 0)
644 ieee80211_timeout_nodes(ic);
645
646 if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0)
647 ifp->if_timer = 1;
648 }
649
650 /*
651 * Mark the basic rates for the 11g rate table based on the
652 * operating mode. For real 11g we mark all the 11b rates
653 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
654 * 11b rates. There's also a pseudo 11a-mode used to mark only
655 * the basic OFDM rates.
656 */
657 static void
658 ieee80211_setbasicrates(struct ieee80211com *ic)
659 {
660 static const struct ieee80211_rateset basic[] = {
661 { 0 }, /* IEEE80211_MODE_AUTO */
662 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */
663 { 2, { 2, 4 } }, /* IEEE80211_MODE_11B */
664 { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */
665 { 2, { 2, 4 } }, /* IEEE80211_MODE_FH */
666 { 0 }, /* IEEE80211_MODE_TURBO */
667 };
668 enum ieee80211_phymode mode;
669 struct ieee80211_rateset *rs;
670 int i, j;
671
672 for (mode = 0; mode < IEEE80211_MODE_MAX; mode++) {
673 rs = &ic->ic_sup_rates[mode];
674 for (i = 0; i < rs->rs_nrates; i++) {
675 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
676 for (j = 0; j < basic[mode].rs_nrates; j++)
677 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
678 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
679 break;
680 }
681 }
682 }
683 }
684
685 /*
686 * Set the current phy mode and recalculate the active channel
687 * set based on the available channels for this mode. Also
688 * select a new default/current channel if the current one is
689 * inappropriate for this mode.
690 */
691 int
692 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
693 {
694 #define N(a) (sizeof(a) / sizeof(a[0]))
695 static const u_int chanflags[] = {
696 0, /* IEEE80211_MODE_AUTO */
697 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
698 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
699 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
700 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
701 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO */
702 };
703 struct ieee80211_channel *c;
704 u_int modeflags;
705 int i;
706
707 /* validate new mode */
708 if ((ic->ic_modecaps & (1<<mode)) == 0) {
709 IEEE80211_DPRINTF(("%s: mode %u not supported (caps 0x%x)\n",
710 __func__, mode, ic->ic_modecaps));
711 return EINVAL;
712 }
713
714 /*
715 * Verify at least one channel is present in the available
716 * channel list before committing to the new mode.
717 */
718 IASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
719 modeflags = chanflags[mode];
720 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
721 c = &ic->ic_channels[i];
722 if (mode == IEEE80211_MODE_AUTO) {
723 /* ignore turbo channels for autoselect */
724 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
725 break;
726 } else {
727 if ((c->ic_flags & modeflags) == modeflags)
728 break;
729 }
730 }
731 if (i > IEEE80211_CHAN_MAX) {
732 IEEE80211_DPRINTF(("%s: no channels found for mode %u\n",
733 __func__, mode));
734 return EINVAL;
735 }
736
737 /*
738 * Calculate the active channel set.
739 */
740 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
741 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
742 c = &ic->ic_channels[i];
743 if (mode == IEEE80211_MODE_AUTO) {
744 /* take anything but pure turbo channels */
745 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
746 setbit(ic->ic_chan_active, i);
747 } else {
748 if ((c->ic_flags & modeflags) == modeflags)
749 setbit(ic->ic_chan_active, i);
750 }
751 }
752 /*
753 * If no current/default channel is setup or the current
754 * channel is wrong for the mode then pick the first
755 * available channel from the active list. This is likely
756 * not the right one.
757 */
758 if (ic->ic_ibss_chan == NULL ||
759 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
760 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
761 if (isset(ic->ic_chan_active, i)) {
762 ic->ic_ibss_chan = &ic->ic_channels[i];
763 break;
764 }
765 IASSERT(ic->ic_ibss_chan != NULL &&
766 isset(ic->ic_chan_active,
767 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
768 ("Bad IBSS channel %u\n",
769 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
770 }
771
772 /*
773 * Set/reset state flags that influence beacon contents, etc.
774 *
775 * XXX what if we have stations already associated???
776 * XXX probably not right for autoselect?
777 */
778 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
779 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
780 if (mode == IEEE80211_MODE_11G) {
781 if (ic->ic_caps & IEEE80211_C_SHSLOT)
782 ic->ic_flags |= IEEE80211_F_SHSLOT;
783 } else
784 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
785
786 ic->ic_curmode = mode;
787 return 0;
788 #undef N
789 }
790
791 /*
792 * Return the phy mode for with the specified channel so the
793 * caller can select a rate set. This is problematic and the
794 * work here assumes how things work elsewhere in this code.
795 *
796 * XXX never returns turbo modes -dcy
797 */
798 enum ieee80211_phymode
799 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
800 {
801 /*
802 * NB: this assumes the channel would not be supplied to us
803 * unless it was already compatible with the current mode.
804 */
805 if (ic->ic_curmode != IEEE80211_MODE_AUTO ||
806 chan == IEEE80211_CHAN_ANYC)
807 return ic->ic_curmode;
808 /*
809 * In autoselect mode; deduce a mode based on the channel
810 * characteristics. We assume that turbo-only channels
811 * are not considered when the channel set is constructed.
812 */
813 if (IEEE80211_IS_CHAN_5GHZ(chan))
814 return IEEE80211_MODE_11A;
815 else if (IEEE80211_IS_CHAN_FHSS(chan))
816 return IEEE80211_MODE_FH;
817 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
818 return IEEE80211_MODE_11G;
819 else
820 return IEEE80211_MODE_11B;
821 }
822
823 /*
824 * convert IEEE80211 rate value to ifmedia subtype.
825 * ieee80211 rate is in unit of 0.5Mbps.
826 */
827 int
828 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
829 {
830 #define N(a) (sizeof(a) / sizeof(a[0]))
831 static const struct {
832 u_int m; /* rate + mode */
833 u_int r; /* if_media rate */
834 } rates[] = {
835 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
836 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
837 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
838 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
839 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
840 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
841 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
842 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
843 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
844 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
845 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
846 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
847 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
848 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
849 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
850 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
851 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
852 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
853 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
854 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
855 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
856 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
857 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
858 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
859 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
860 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
861 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
862 /* NB: OFDM72 doesn't realy exist so we don't handle it */
863 };
864 u_int mask, i;
865
866 mask = rate & IEEE80211_RATE_VAL;
867 switch (mode) {
868 case IEEE80211_MODE_11A:
869 case IEEE80211_MODE_TURBO:
870 mask |= IFM_IEEE80211_11A;
871 break;
872 case IEEE80211_MODE_11B:
873 mask |= IFM_IEEE80211_11B;
874 break;
875 case IEEE80211_MODE_FH:
876 mask |= IFM_IEEE80211_FH;
877 break;
878 case IEEE80211_MODE_AUTO:
879 /* NB: ic may be NULL for some drivers */
880 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
881 mask |= IFM_IEEE80211_FH;
882 break;
883 }
884 /* NB: hack, 11g matches both 11b+11a rates */
885 /* fall thru... */
886 case IEEE80211_MODE_11G:
887 mask |= IFM_IEEE80211_11G;
888 break;
889 }
890 for (i = 0; i < N(rates); i++)
891 if (rates[i].m == mask)
892 return rates[i].r;
893 return IFM_AUTO;
894 #undef N
895 }
896
897 int
898 ieee80211_media2rate(int mword)
899 {
900 #define N(a) (sizeof(a) / sizeof(a[0]))
901 int i;
902 static const struct {
903 int subtype;
904 int rate;
905 } ieeerates[] = {
906 { IFM_AUTO, -1 },
907 { IFM_MANUAL, 0 },
908 { IFM_NONE, 0 },
909 { IFM_IEEE80211_FH1, 2 },
910 { IFM_IEEE80211_FH2, 4 },
911 { IFM_IEEE80211_DS1, 2 },
912 { IFM_IEEE80211_DS2, 4 },
913 { IFM_IEEE80211_DS5, 11 },
914 { IFM_IEEE80211_DS11, 22 },
915 { IFM_IEEE80211_DS22, 44 },
916 { IFM_IEEE80211_OFDM6, 12 },
917 { IFM_IEEE80211_OFDM9, 18 },
918 { IFM_IEEE80211_OFDM12, 24 },
919 { IFM_IEEE80211_OFDM18, 36 },
920 { IFM_IEEE80211_OFDM24, 48 },
921 { IFM_IEEE80211_OFDM36, 72 },
922 { IFM_IEEE80211_OFDM48, 96 },
923 { IFM_IEEE80211_OFDM54, 108 },
924 { IFM_IEEE80211_OFDM72, 144 },
925 };
926 for (i = 0; i < N(ieeerates); i++) {
927 if (ieeerates[i].subtype == IFM_SUBTYPE(mword))
928 return ieeerates[i].rate;
929 }
930 return 0;
931 #undef N
932 }
933
934 #ifdef __NetBSD__
935 /* TBD factor with sysctl_ath_verify. */
936 static int
937 sysctl_ieee80211_verify(SYSCTLFN_ARGS)
938 {
939 int error, t;
940 struct sysctlnode node;
941
942 node = *rnode;
943 t = *(int*)rnode->sysctl_data;
944 node.sysctl_data = &t;
945 error = sysctl_lookup(SYSCTLFN_CALL(&node));
946 if (error || newp == NULL)
947 return (error);
948
949 IEEE80211_DPRINTF(("%s: t = %d, nodenum = %d, rnodenum = %d\n",
950 __func__, t, node.sysctl_num, rnode->sysctl_num));
951
952 if (node.sysctl_num == ieee80211_inact_max_nodenum) {
953 if (t < 1)
954 return (EINVAL);
955 t = roundup(t, IEEE80211_INACT_WAIT) / IEEE80211_INACT_WAIT;
956 #ifdef IEEE80211_DEBUG
957 } else if (node.sysctl_num == ieee80211_debug_nodenum) {
958 if (t < 0 || t > 2)
959 return (EINVAL);
960 #endif /* IEEE80211_DEBUG */
961 } else
962 return (EINVAL);
963
964 *(int*)rnode->sysctl_data = t;
965
966 return (0);
967 }
968
969 /*
970 * Pointers for testing:
971 *
972 * If there are no interfaces, or else no 802.11 interfaces,
973 * ieee80211_node_walkfirst must return NULL.
974 *
975 * If there is any single 802.11 interface, ieee80211_node_walkfirst
976 * must not return NULL.
977 */
978 static struct ieee80211_node *
979 ieee80211_node_walkfirst(struct ieee80211_node_walk *nw,
980 u_short if_index)
981 {
982 struct ieee80211com *ic;
983 (void)memset(nw, 0, sizeof(*nw));
984
985 nw->nw_ifindex = if_index;
986
987 LIST_FOREACH(ic, &ieee80211com_head, ic_list) {
988 IEEE80211_DPRINTF(("%s: visiting ic %p\n", __func__, ic));
989 if (if_index != 0 && ic->ic_if.if_index != if_index) {
990 IEEE80211_DPRINTF(("%s: wrong ifindex, "
991 "skipping ic %p\n", __func__, ic));
992 continue;
993 }
994 nw->nw_ic = ic;
995 nw->nw_ni = ic->ic_bss;
996 break;
997 }
998 IEEE80211_DPRINTF(("%s: ic %p ni %p\n", __func__, nw->nw_ic,
999 nw->nw_ni));
1000
1001 KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
1002
1003 return nw->nw_ni;
1004 }
1005
1006 static struct ieee80211_node *
1007 ieee80211_node_walknext(struct ieee80211_node_walk *nw)
1008 {
1009 KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
1010
1011 if (nw->nw_ic == NULL && nw->nw_ni == NULL)
1012 return NULL;
1013
1014 if (nw->nw_ni == nw->nw_ic->ic_bss)
1015 nw->nw_ni = TAILQ_FIRST(&nw->nw_ic->ic_node);
1016 else
1017 nw->nw_ni = TAILQ_NEXT(nw->nw_ni, ni_list);
1018
1019 if (nw->nw_ni == NULL) {
1020 if (nw->nw_ifindex != 0)
1021 return NULL;
1022
1023 nw->nw_ic = LIST_NEXT(nw->nw_ic, ic_list);
1024 if (nw->nw_ic == NULL)
1025 return NULL;
1026
1027 nw->nw_ni = nw->nw_ic->ic_bss;
1028 }
1029
1030 IEEE80211_DPRINTF(("%s: ic %p bss %p ni %p\n", __func__, nw->nw_ic,
1031 nw->nw_ic->ic_bss, nw->nw_ni));
1032
1033 KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
1034
1035 return nw->nw_ni;
1036 }
1037
1038 static void
1039 sysctl_ieee80211_fill_node(struct ieee80211_node *ni,
1040 struct ieee80211_node_sysctl *ns, int ifindex,
1041 struct ieee80211_channel *chan0, int is_bss)
1042 {
1043 ns->ns_ifindex = ifindex;
1044 ns->ns_capinfo = ni->ni_capinfo;
1045 ns->ns_flags = (is_bss) ? IEEE80211_NODE_SYSCTL_F_BSS : 0;
1046 (void)memcpy(ns->ns_macaddr, ni->ni_macaddr, sizeof(ns->ns_macaddr));
1047 (void)memcpy(ns->ns_bssid, ni->ni_bssid, sizeof(ns->ns_bssid));
1048 if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
1049 ns->ns_freq = ni->ni_chan->ic_freq;
1050 ns->ns_chanflags = ni->ni_chan->ic_flags;
1051 ns->ns_chanidx = ni->ni_chan - chan0;
1052 } else {
1053 ns->ns_freq = ns->ns_chanflags = 0;
1054 ns->ns_chanidx = 0;
1055 }
1056 ns->ns_rssi = ni->ni_rssi;
1057 ns->ns_esslen = ni->ni_esslen;
1058 (void)memcpy(ns->ns_essid, ni->ni_essid, sizeof(ns->ns_essid));
1059 ns->ns_pwrsave = ni->ni_pwrsave;
1060 ns->ns_erp = ni->ni_erp;
1061 ns->ns_associd = ni->ni_associd;
1062 ns->ns_inact = ni->ni_inact * IEEE80211_INACT_WAIT;
1063 ns->ns_rstamp = ni->ni_rstamp;
1064 ns->ns_rates = ni->ni_rates;
1065 ns->ns_txrate = ni->ni_txrate;
1066 ns->ns_intval = ni->ni_intval;
1067 (void)memcpy(ns->ns_tstamp, ni->ni_tstamp, sizeof(ns->ns_tstamp));
1068 ns->ns_txseq = ni->ni_txseq;
1069 ns->ns_rxseq = ni->ni_rxseq;
1070 ns->ns_fhdwell = ni->ni_fhdwell;
1071 ns->ns_fhindex = ni->ni_fhindex;
1072 ns->ns_fails = ni->ni_fails;
1073 }
1074
1075 /* Between two examinations of the sysctl tree, I expect each
1076 * interface to add no more than 5 nodes.
1077 */
1078 #define IEEE80211_SYSCTL_NODE_GROWTH 5
1079
1080 static int
1081 sysctl_ieee80211_node(SYSCTLFN_ARGS)
1082 {
1083 struct ieee80211_node_walk nw;
1084 struct ieee80211_node *ni;
1085 struct ieee80211_node_sysctl ns;
1086 char *dp;
1087 u_int cur_ifindex, ifcount, ifindex, last_ifindex, op, arg, hdr_type;
1088 size_t len, needed, eltsize, out_size;
1089 int error, s, nelt;
1090
1091 if (namelen == 1 && name[0] == CTL_QUERY)
1092 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1093
1094 if (namelen != IEEE80211_SYSCTL_NODENAMELEN)
1095 return (EINVAL);
1096
1097 /* ifindex.op.arg.header-type.eltsize.nelt */
1098 dp = oldp;
1099 len = (oldp != NULL) ? *oldlenp : 0;
1100 ifindex = name[IEEE80211_SYSCTL_NODENAME_IF];
1101 op = name[IEEE80211_SYSCTL_NODENAME_OP];
1102 arg = name[IEEE80211_SYSCTL_NODENAME_ARG];
1103 hdr_type = name[IEEE80211_SYSCTL_NODENAME_TYPE];
1104 eltsize = name[IEEE80211_SYSCTL_NODENAME_ELTSIZE];
1105 nelt = name[IEEE80211_SYSCTL_NODENAME_ELTCOUNT];
1106 out_size = MIN(sizeof(ns), eltsize);
1107
1108 IEEE80211_DPRINTF(("%s: ifindex %u op %u arg %u hdr_type %u eltsize %u "
1109 "nelt %d out_size %u\n", __func__, ifindex, op, arg, hdr_type,
1110 eltsize, nelt, out_size));
1111
1112 if (op != IEEE80211_SYSCTL_OP_ALL || arg != 0 ||
1113 hdr_type != IEEE80211_SYSCTL_T_NODE || eltsize < 1 || nelt < 0)
1114 return (EINVAL);
1115
1116 error = 0;
1117 needed = 0;
1118 ifcount = 0;
1119 last_ifindex = 0;
1120
1121 s = splnet();
1122
1123 for (ni = ieee80211_node_walkfirst(&nw, ifindex); ni != NULL;
1124 ni = ieee80211_node_walknext(&nw)) {
1125 struct ieee80211com *ic;
1126
1127 ic = nw.nw_ic;
1128 cur_ifindex = ic->ic_if.if_index;
1129
1130 IEEE80211_DPRINTF(("%s: visit ic %p idx %u\n", __func__, ic,
1131 cur_ifindex));
1132
1133 if (cur_ifindex != last_ifindex) {
1134 ifcount++;
1135 last_ifindex = cur_ifindex;
1136 }
1137
1138 IEEE80211_DPRINTF(("%s: ni %p dp %p len %u nelt %d\n",
1139 __func__, ni, dp, len, nelt));
1140
1141 if (nelt <= 0)
1142 continue;
1143
1144 if (len >= eltsize) {
1145 sysctl_ieee80211_fill_node(ni, &ns, cur_ifindex,
1146 &ic->ic_channels[0], ni == ic->ic_bss);
1147 error = copyout(&ns, dp, out_size);
1148 if (error)
1149 goto cleanup;
1150 dp += eltsize;
1151 len -= eltsize;
1152 }
1153 needed += eltsize;
1154 if (nelt != INT_MAX)
1155 nelt--;
1156 IEEE80211_DPRINTF(("%s: needed %u\n", __func__, needed));
1157 }
1158 IEEE80211_DPRINTF(("%s: %u interfaces\n", __func__, ifcount));
1159 cleanup:
1160 splx(s);
1161
1162 *oldlenp = needed;
1163 if (oldp == NULL)
1164 *oldlenp += ifcount * IEEE80211_SYSCTL_NODE_GROWTH * eltsize;
1165
1166 return (error);
1167 }
1168
1169 /*
1170 * Setup sysctl(3) MIB, net.ieee80211.*
1171 *
1172 * TBD condition CTLFLAG_PERMANENT on being an LKM or not
1173 */
1174 SYSCTL_SETUP(sysctl_ieee80211, "sysctl ieee80211 subtree setup")
1175 {
1176 int rc;
1177 struct sysctlnode *cnode, *rnode;
1178
1179 if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
1180 CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL,
1181 NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0)
1182 goto err;
1183
1184 if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
1185 CTLFLAG_PERMANENT, CTLTYPE_NODE, "link",
1186 "link-layer statistics and controls",
1187 NULL, 0, NULL, 0, PF_LINK, CTL_EOL)) != 0)
1188 goto err;
1189
1190 if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
1191 CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211",
1192 "IEEE 802.11 WLAN statistics and controls",
1193 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
1194 goto err;
1195
1196 if ((rc = sysctl_createv(clog, 0, &rnode, NULL,
1197 CTLFLAG_PERMANENT, CTLTYPE_NODE, "nodes", "client/peer stations",
1198 sysctl_ieee80211_node, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
1199 goto err;
1200
1201 #ifdef IEEE80211_DEBUG
1202
1203 /* control debugging printfs */
1204 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
1205 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
1206 "debug", SYSCTL_DESCR("Enable IEEE 802.11 debugging output"),
1207 sysctl_ieee80211_verify, 0, &ieee80211_debug, 0,
1208 CTL_CREATE, CTL_EOL)) != 0)
1209 goto err;
1210
1211 ieee80211_debug_nodenum = cnode->sysctl_num;
1212
1213 #endif /* IEEE80211_DEBUG */
1214
1215 /* control inactivity timer */
1216 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
1217 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
1218 "maxinact", SYSCTL_DESCR("Station inactivity timeout"),
1219 sysctl_ieee80211_verify, 0, &ieee80211_inact_max,
1220 0, CTL_CREATE, CTL_EOL)) != 0)
1221 goto err;
1222
1223 ieee80211_inact_max_nodenum = cnode->sysctl_num;
1224
1225 return;
1226 err:
1227 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
1228 }
1229 #endif /* __NetBSD__ */
1230
1231 #ifdef __FreeBSD__
1232 /*
1233 * Module glue.
1234 *
1235 * NB: the module name is "wlan" for compatibility with NetBSD.
1236 */
1237
1238 static int
1239 ieee80211_modevent(module_t mod, int type, void *unused)
1240 {
1241 switch (type) {
1242 case MOD_LOAD:
1243 if (bootverbose)
1244 printf("wlan: <802.11 Link Layer>\n");
1245 return 0;
1246 case MOD_UNLOAD:
1247 return 0;
1248 }
1249 return EINVAL;
1250 }
1251
1252 static moduledata_t ieee80211_mod = {
1253 "wlan",
1254 ieee80211_modevent,
1255 0
1256 };
1257 DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1258 MODULE_VERSION(wlan, 1);
1259 MODULE_DEPEND(wlan, rc4, 1, 1, 1);
1260 MODULE_DEPEND(wlan, ether, 1, 1, 1);
1261 #endif
1262