ieee80211.c revision 1.21 1 /* $NetBSD: ieee80211.c,v 1.21 2004/07/16 02:54:05 dyoung 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.21 2004/07/16 02:54:05 dyoung 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
81 #include <net/bpf.h>
82
83 #ifdef INET
84 #include <netinet/in.h>
85 #ifdef __FreeBSD__
86 #include <netinet/if_ether.h>
87 #else
88 #include <net/if_ether.h>
89 #endif
90 #endif
91
92 #ifdef IEEE80211_DEBUG
93 int ieee80211_debug = 0;
94 #ifdef __NetBSD__
95 static int ieee80211_debug_nodenum;
96 #endif /* __NetBSD__ */
97
98 #ifdef __FreeBSD__
99 SYSCTL_INT(_debug, OID_AUTO, ieee80211, CTLFLAG_RW, &ieee80211_debug,
100 0, "IEEE 802.11 media debugging printfs");
101 #endif
102 #endif
103
104 int ieee80211_inact_max = IEEE80211_INACT_MAX;
105 static int ieee80211_inact_max_nodenum;
106
107 struct ieee80211com_head ieee80211com_head =
108 LIST_HEAD_INITIALIZER(ieee80211com_head);
109
110 static void ieee80211_set11gbasicrates(struct ieee80211_rateset *,
111 enum ieee80211_phymode);
112
113 static const char *ieee80211_phymode_name[] = {
114 "auto", /* IEEE80211_MODE_AUTO */
115 "11a", /* IEEE80211_MODE_11A */
116 "11b", /* IEEE80211_MODE_11B */
117 "11g", /* IEEE80211_MODE_11G */
118 "FH", /* IEEE80211_MODE_FH */
119 "turbo", /* IEEE80211_MODE_TURBO */
120 };
121
122 void
123 ieee80211_ifattach(struct ifnet *ifp)
124 {
125 struct ieee80211com *ic = (void *)ifp;
126 struct ieee80211_channel *c;
127 int i;
128
129 ether_ifattach(ifp, ic->ic_myaddr);
130 #if NBPFILTER > 0
131 bpfattach2(ifp, DLT_IEEE802_11,
132 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
133 #endif
134 ieee80211_crypto_attach(ifp);
135
136 /*
137 * Fill in 802.11 available channel set, mark
138 * all available channels as active, and pick
139 * a default channel if not already specified.
140 */
141 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
142 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
143 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
144 c = &ic->ic_channels[i];
145 if (c->ic_flags) {
146 /*
147 * Verify driver passed us valid data.
148 */
149 if (i != ieee80211_chan2ieee(ic, c)) {
150 if_printf(ifp, "bad channel ignored; "
151 "freq %u flags %x number %u\n",
152 c->ic_freq, c->ic_flags, i);
153 c->ic_flags = 0; /* NB: remove */
154 continue;
155 }
156 setbit(ic->ic_chan_avail, i);
157 /*
158 * Identify mode capabilities.
159 */
160 if (IEEE80211_IS_CHAN_A(c))
161 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
162 if (IEEE80211_IS_CHAN_B(c))
163 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
164 if (IEEE80211_IS_CHAN_PUREG(c))
165 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
166 if (IEEE80211_IS_CHAN_FHSS(c))
167 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
168 if (IEEE80211_IS_CHAN_T(c))
169 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO;
170 }
171 }
172 /* validate ic->ic_curmode */
173 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
174 ic->ic_curmode = IEEE80211_MODE_AUTO;
175 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
176
177 (void) ieee80211_setmode(ic, ic->ic_curmode);
178
179 if (ic->ic_lintval == 0)
180 ic->ic_lintval = 100; /* default sleep */
181 ic->ic_bmisstimeout = 7*ic->ic_lintval; /* default 7 beacons */
182
183 LIST_INSERT_HEAD(&ieee80211com_head, ic, ic_list);
184 ieee80211_node_attach(ifp);
185 ieee80211_proto_attach(ifp);
186 }
187
188 void
189 ieee80211_ifdetach(struct ifnet *ifp)
190 {
191 struct ieee80211com *ic = (void *)ifp;
192
193 ieee80211_proto_detach(ifp);
194 ieee80211_crypto_detach(ifp);
195 ieee80211_node_detach(ifp);
196 LIST_REMOVE(ic, ic_list);
197 #ifdef __FreeBSD__
198 ifmedia_removeall(&ic->ic_media);
199 #else
200 ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY);
201 #endif
202 #if NBPFILTER > 0
203 bpfdetach(ifp);
204 #endif
205 ether_ifdetach(ifp);
206 }
207
208 /*
209 * Convert MHz frequency to IEEE channel number.
210 */
211 u_int
212 ieee80211_mhz2ieee(u_int freq, u_int flags)
213 {
214 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
215 if (freq == 2484)
216 return 14;
217 if (freq < 2484)
218 return (freq - 2407) / 5;
219 else
220 return 15 + ((freq - 2512) / 20);
221 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
222 return (freq - 5000) / 5;
223 } else { /* either, guess */
224 if (freq == 2484)
225 return 14;
226 if (freq < 2484)
227 return (freq - 2407) / 5;
228 if (freq < 5000)
229 return 15 + ((freq - 2512) / 20);
230 return (freq - 5000) / 5;
231 }
232 }
233
234 /*
235 * Convert channel to IEEE channel number.
236 */
237 u_int
238 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
239 {
240 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
241 return c - ic->ic_channels;
242 else if (c == IEEE80211_CHAN_ANYC)
243 return IEEE80211_CHAN_ANY;
244 else if (c != NULL) {
245 if_printf(&ic->ic_if, "invalid channel freq %u flags %x\n",
246 c->ic_freq, c->ic_flags);
247 return 0; /* XXX */
248 } else {
249 if_printf(&ic->ic_if, "invalid channel (NULL)\n");
250 return 0; /* XXX */
251 }
252 }
253
254 /*
255 * Convert IEEE channel number to MHz frequency.
256 */
257 u_int
258 ieee80211_ieee2mhz(u_int chan, u_int flags)
259 {
260 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
261 if (chan == 14)
262 return 2484;
263 if (chan < 14)
264 return 2407 + chan*5;
265 else
266 return 2512 + ((chan-15)*20);
267 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
268 return 5000 + (chan*5);
269 } else { /* either, guess */
270 if (chan == 14)
271 return 2484;
272 if (chan < 14) /* 0-13 */
273 return 2407 + chan*5;
274 if (chan < 27) /* 15-26 */
275 return 2512 + ((chan-15)*20);
276 return 5000 + (chan*5);
277 }
278 }
279
280 /*
281 * Setup the media data structures according to the channel and
282 * rate tables. This must be called by the driver after
283 * ieee80211_attach and before most anything else.
284 */
285 void
286 ieee80211_media_init(struct ifnet *ifp,
287 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
288 {
289 #define ADD(_ic, _s, _o) \
290 ifmedia_add(&(_ic)->ic_media, \
291 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
292 struct ieee80211com *ic = (void *)ifp;
293 struct ifmediareq imr;
294 int i, j, mode, rate, maxrate, mword, mopt, r;
295 struct ieee80211_rateset *rs;
296 struct ieee80211_rateset allrates;
297
298 /*
299 * Do late attach work that must wait for any subclass
300 * (i.e. driver) work such as overriding methods.
301 */
302 ieee80211_node_lateattach(ifp);
303
304 /*
305 * Fill in media characteristics.
306 */
307 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
308 maxrate = 0;
309 memset(&allrates, 0, sizeof(allrates));
310 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
311 static const u_int mopts[] = {
312 IFM_AUTO,
313 IFM_IEEE80211_11A,
314 IFM_IEEE80211_11B,
315 IFM_IEEE80211_11G,
316 IFM_IEEE80211_FH,
317 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
318 };
319 if ((ic->ic_modecaps & (1<<mode)) == 0)
320 continue;
321 mopt = mopts[mode];
322 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
323 if (ic->ic_caps & IEEE80211_C_IBSS)
324 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
325 if (ic->ic_caps & IEEE80211_C_HOSTAP)
326 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
327 if (ic->ic_caps & IEEE80211_C_AHDEMO)
328 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
329 if (ic->ic_caps & IEEE80211_C_MONITOR)
330 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
331 if (mode == IEEE80211_MODE_AUTO)
332 continue;
333 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
334 rs = &ic->ic_sup_rates[mode];
335 for (i = 0; i < rs->rs_nrates; i++) {
336 rate = rs->rs_rates[i];
337 mword = ieee80211_rate2media(ic, rate, mode);
338 if (mword == 0)
339 continue;
340 printf("%s%d%sMbps", (i != 0 ? " " : ""),
341 (rate & IEEE80211_RATE_VAL) / 2,
342 ((rate & 0x1) != 0 ? ".5" : ""));
343 ADD(ic, mword, mopt);
344 if (ic->ic_caps & IEEE80211_C_IBSS)
345 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
346 if (ic->ic_caps & IEEE80211_C_HOSTAP)
347 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
348 if (ic->ic_caps & IEEE80211_C_AHDEMO)
349 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
350 if (ic->ic_caps & IEEE80211_C_MONITOR)
351 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
352 /*
353 * Add rate to the collection of all rates.
354 */
355 r = rate & IEEE80211_RATE_VAL;
356 for (j = 0; j < allrates.rs_nrates; j++)
357 if (allrates.rs_rates[j] == r)
358 break;
359 if (j == allrates.rs_nrates) {
360 /* unique, add to the set */
361 allrates.rs_rates[j] = r;
362 allrates.rs_nrates++;
363 }
364 rate = (rate & IEEE80211_RATE_VAL) / 2;
365 if (rate > maxrate)
366 maxrate = rate;
367 }
368 printf("\n");
369 }
370 for (i = 0; i < allrates.rs_nrates; i++) {
371 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
372 IEEE80211_MODE_AUTO);
373 if (mword == 0)
374 continue;
375 mword = IFM_SUBTYPE(mword); /* remove media options */
376 ADD(ic, mword, 0);
377 if (ic->ic_caps & IEEE80211_C_IBSS)
378 ADD(ic, mword, IFM_IEEE80211_ADHOC);
379 if (ic->ic_caps & IEEE80211_C_HOSTAP)
380 ADD(ic, mword, IFM_IEEE80211_HOSTAP);
381 if (ic->ic_caps & IEEE80211_C_AHDEMO)
382 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
383 if (ic->ic_caps & IEEE80211_C_MONITOR)
384 ADD(ic, mword, IFM_IEEE80211_MONITOR);
385 }
386 ieee80211_media_status(ifp, &imr);
387 ifmedia_set(&ic->ic_media, imr.ifm_active);
388
389 if (maxrate)
390 ifp->if_baudrate = IF_Mbps(maxrate);
391
392 if (ic->ic_max_aid == 0)
393 ic->ic_max_aid = IEEE80211_MAX_AID;
394
395 #undef ADD
396 }
397
398 static int
399 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
400 {
401 #define IEEERATE(_ic,_m,_i) \
402 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
403 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
404 for (i = 0; i < nrates; i++)
405 if (IEEERATE(ic, mode, i) == rate)
406 return i;
407 return -1;
408 #undef IEEERATE
409 }
410
411 /*
412 * Handle a media change request.
413 */
414 int
415 ieee80211_media_change(struct ifnet *ifp)
416 {
417 struct ieee80211com *ic = (void *)ifp;
418 struct ifmedia_entry *ime;
419 enum ieee80211_opmode newopmode;
420 enum ieee80211_phymode newphymode;
421 int i, j, newrate, error = 0;
422
423 ime = ic->ic_media.ifm_cur;
424 /*
425 * First, identify the phy mode.
426 */
427 switch (IFM_MODE(ime->ifm_media)) {
428 case IFM_IEEE80211_11A:
429 newphymode = IEEE80211_MODE_11A;
430 break;
431 case IFM_IEEE80211_11B:
432 newphymode = IEEE80211_MODE_11B;
433 break;
434 case IFM_IEEE80211_11G:
435 newphymode = IEEE80211_MODE_11G;
436 break;
437 case IFM_IEEE80211_FH:
438 newphymode = IEEE80211_MODE_FH;
439 break;
440 case IFM_AUTO:
441 newphymode = IEEE80211_MODE_AUTO;
442 break;
443 default:
444 return EINVAL;
445 }
446 /*
447 * Turbo mode is an ``option''. Eventually it
448 * needs to be applied to 11g too.
449 */
450 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
451 if (newphymode != IEEE80211_MODE_11A)
452 return EINVAL;
453 newphymode = IEEE80211_MODE_TURBO;
454 }
455 /*
456 * Validate requested mode is available.
457 */
458 if ((ic->ic_modecaps & (1<<newphymode)) == 0)
459 return EINVAL;
460
461 /*
462 * Next, the fixed/variable rate.
463 */
464 i = -1;
465 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
466 /*
467 * Convert media subtype to rate.
468 */
469 newrate = ieee80211_media2rate(ime->ifm_media);
470 if (newrate == 0)
471 return EINVAL;
472 /*
473 * Check the rate table for the specified/current phy.
474 */
475 if (newphymode == IEEE80211_MODE_AUTO) {
476 /*
477 * In autoselect mode search for the rate.
478 */
479 for (j = IEEE80211_MODE_11A;
480 j < IEEE80211_MODE_MAX; j++) {
481 if ((ic->ic_modecaps & (1<<j)) == 0)
482 continue;
483 i = findrate(ic, j, newrate);
484 if (i != -1) {
485 /* lock mode too */
486 newphymode = j;
487 break;
488 }
489 }
490 } else {
491 i = findrate(ic, newphymode, newrate);
492 }
493 if (i == -1) /* mode/rate mismatch */
494 return EINVAL;
495 }
496 /* NB: defer rate setting to later */
497
498 /*
499 * Deduce new operating mode but don't install it just yet.
500 */
501 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
502 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
503 newopmode = IEEE80211_M_AHDEMO;
504 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
505 newopmode = IEEE80211_M_HOSTAP;
506 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
507 newopmode = IEEE80211_M_IBSS;
508 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
509 newopmode = IEEE80211_M_MONITOR;
510 else
511 newopmode = IEEE80211_M_STA;
512
513 /*
514 * Autoselect doesn't make sense when operating as an AP.
515 * If no phy mode has been selected, pick one and lock it
516 * down so rate tables can be used in forming beacon frames
517 * and the like.
518 */
519 if (newopmode == IEEE80211_M_HOSTAP &&
520 newphymode == IEEE80211_MODE_AUTO) {
521 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
522 if (ic->ic_modecaps & (1<<j)) {
523 newphymode = j;
524 break;
525 }
526 }
527
528 /*
529 * Handle phy mode change.
530 */
531 if (ic->ic_curmode != newphymode) { /* change phy mode */
532 error = ieee80211_setmode(ic, newphymode);
533 if (error != 0)
534 return error;
535 error = ENETRESET;
536 }
537
538 /*
539 * Committed to changes, install the rate setting.
540 */
541 if (ic->ic_fixed_rate != i) {
542 ic->ic_fixed_rate = i; /* set fixed tx rate */
543 error = ENETRESET;
544 }
545
546 /*
547 * Handle operating mode change.
548 */
549 if (ic->ic_opmode != newopmode) {
550 ic->ic_opmode = newopmode;
551 switch (newopmode) {
552 case IEEE80211_M_AHDEMO:
553 case IEEE80211_M_HOSTAP:
554 case IEEE80211_M_STA:
555 case IEEE80211_M_MONITOR:
556 ic->ic_flags &= ~IEEE80211_F_IBSSON;
557 break;
558 case IEEE80211_M_IBSS:
559 ic->ic_flags |= IEEE80211_F_IBSSON;
560 #ifdef notdef
561 if (ic->ic_curmode == IEEE80211_MODE_11G)
562 ieee80211_set11gbasicrates(
563 &ic->ic_sup_rates[newphymode],
564 IEEE80211_MODE_11B);
565 #endif
566 break;
567 }
568 error = ENETRESET;
569 }
570 #ifdef notdef
571 if (error == 0)
572 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
573 #endif
574 return error;
575 }
576
577 void
578 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
579 {
580 struct ieee80211com *ic = (void *)ifp;
581 struct ieee80211_node *ni = NULL;
582
583 imr->ifm_status = IFM_AVALID;
584 imr->ifm_active = IFM_IEEE80211;
585 if (ic->ic_state == IEEE80211_S_RUN)
586 imr->ifm_status |= IFM_ACTIVE;
587 imr->ifm_active |= IFM_AUTO;
588 switch (ic->ic_opmode) {
589 case IEEE80211_M_STA:
590 ni = ic->ic_bss;
591 /* calculate rate subtype */
592 imr->ifm_active |= ieee80211_rate2media(ic,
593 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
594 break;
595 case IEEE80211_M_IBSS:
596 imr->ifm_active |= IFM_IEEE80211_ADHOC;
597 break;
598 case IEEE80211_M_AHDEMO:
599 /* should not come here */
600 break;
601 case IEEE80211_M_HOSTAP:
602 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
603 break;
604 case IEEE80211_M_MONITOR:
605 imr->ifm_active |= IFM_IEEE80211_MONITOR;
606 break;
607 }
608 switch (ic->ic_curmode) {
609 case IEEE80211_MODE_11A:
610 imr->ifm_active |= IFM_IEEE80211_11A;
611 break;
612 case IEEE80211_MODE_11B:
613 imr->ifm_active |= IFM_IEEE80211_11B;
614 break;
615 case IEEE80211_MODE_11G:
616 imr->ifm_active |= IFM_IEEE80211_11G;
617 break;
618 case IEEE80211_MODE_FH:
619 imr->ifm_active |= IFM_IEEE80211_FH;
620 break;
621 case IEEE80211_MODE_TURBO:
622 imr->ifm_active |= IFM_IEEE80211_11A
623 | IFM_IEEE80211_TURBO;
624 break;
625 }
626 }
627
628 void
629 ieee80211_watchdog(struct ifnet *ifp)
630 {
631 struct ieee80211com *ic = (void *)ifp;
632
633 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
634 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
635 if (ic->ic_inact_timer && --ic->ic_inact_timer == 0)
636 ieee80211_timeout_nodes(ic);
637
638 if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0)
639 ifp->if_timer = 1;
640 }
641
642 /*
643 * Mark the basic rates for the 11g rate table based on the
644 * operating mode. For real 11g we mark all the 11b rates
645 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
646 * 11b rates. There's also a pseudo 11a-mode used to mark only
647 * the basic OFDM rates.
648 */
649 static void
650 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
651 {
652 static const struct ieee80211_rateset basic[] = {
653 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */
654 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11B */
655 { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */
656 { 0 }, /* IEEE80211_MODE_FH */
657 { 0 }, /* IEEE80211_MODE_TURBO */
658 };
659 int i, j;
660
661 for (i = 0; i < rs->rs_nrates; i++) {
662 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
663 for (j = 0; j < basic[mode].rs_nrates; j++)
664 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
665 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
666 break;
667 }
668 }
669 }
670
671 /*
672 * Set the current phy mode and recalculate the active channel
673 * set based on the available channels for this mode. Also
674 * select a new default/current channel if the current one is
675 * inappropriate for this mode.
676 */
677 int
678 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
679 {
680 #define N(a) (sizeof(a) / sizeof(a[0]))
681 static const u_int chanflags[] = {
682 0, /* IEEE80211_MODE_AUTO */
683 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
684 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
685 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
686 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
687 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO */
688 };
689 struct ieee80211_channel *c;
690 u_int modeflags;
691 int i;
692
693 /* validate new mode */
694 if ((ic->ic_modecaps & (1<<mode)) == 0) {
695 IEEE80211_DPRINTF(("%s: mode %u not supported (caps 0x%x)\n",
696 __func__, mode, ic->ic_modecaps));
697 return EINVAL;
698 }
699
700 /*
701 * Verify at least one channel is present in the available
702 * channel list before committing to the new mode.
703 */
704 IASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
705 modeflags = chanflags[mode];
706 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
707 c = &ic->ic_channels[i];
708 if (mode == IEEE80211_MODE_AUTO) {
709 /* ignore turbo channels for autoselect */
710 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
711 break;
712 } else {
713 if ((c->ic_flags & modeflags) == modeflags)
714 break;
715 }
716 }
717 if (i > IEEE80211_CHAN_MAX) {
718 IEEE80211_DPRINTF(("%s: no channels found for mode %u\n",
719 __func__, mode));
720 return EINVAL;
721 }
722
723 /*
724 * Calculate the active channel set.
725 */
726 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
727 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
728 c = &ic->ic_channels[i];
729 if (mode == IEEE80211_MODE_AUTO) {
730 /* take anything but pure turbo channels */
731 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
732 setbit(ic->ic_chan_active, i);
733 } else {
734 if ((c->ic_flags & modeflags) == modeflags)
735 setbit(ic->ic_chan_active, i);
736 }
737 }
738 /*
739 * If no current/default channel is setup or the current
740 * channel is wrong for the mode then pick the first
741 * available channel from the active list. This is likely
742 * not the right one.
743 */
744 if (ic->ic_ibss_chan == NULL ||
745 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
746 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
747 if (isset(ic->ic_chan_active, i)) {
748 ic->ic_ibss_chan = &ic->ic_channels[i];
749 break;
750 }
751 IASSERT(ic->ic_ibss_chan != NULL &&
752 isset(ic->ic_chan_active,
753 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
754 ("Bad IBSS channel %u\n",
755 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
756 }
757
758 /*
759 * Set/reset state flags that influence beacon contents, etc.
760 *
761 * XXX what if we have stations already associated???
762 * XXX probably not right for autoselect?
763 */
764 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
765 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
766 if (mode == IEEE80211_MODE_11G) {
767 if (ic->ic_caps & IEEE80211_C_SHSLOT)
768 ic->ic_flags |= IEEE80211_F_SHSLOT;
769 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
770 IEEE80211_MODE_11G);
771 } else {
772 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
773 }
774
775 ic->ic_curmode = mode;
776 return 0;
777 #undef N
778 }
779
780 /*
781 * Return the phy mode for with the specified channel so the
782 * caller can select a rate set. This is problematic and the
783 * work here assumes how things work elsewhere in this code.
784 *
785 * XXX never returns turbo modes -dcy
786 */
787 enum ieee80211_phymode
788 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
789 {
790 /*
791 * NB: this assumes the channel would not be supplied to us
792 * unless it was already compatible with the current mode.
793 */
794 if (ic->ic_curmode != IEEE80211_MODE_AUTO ||
795 chan == IEEE80211_CHAN_ANYC)
796 return ic->ic_curmode;
797 /*
798 * In autoselect mode; deduce a mode based on the channel
799 * characteristics. We assume that turbo-only channels
800 * are not considered when the channel set is constructed.
801 */
802 if (IEEE80211_IS_CHAN_5GHZ(chan))
803 return IEEE80211_MODE_11A;
804 else if (IEEE80211_IS_CHAN_FHSS(chan))
805 return IEEE80211_MODE_FH;
806 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
807 return IEEE80211_MODE_11G;
808 else
809 return IEEE80211_MODE_11B;
810 }
811
812 /*
813 * convert IEEE80211 rate value to ifmedia subtype.
814 * ieee80211 rate is in unit of 0.5Mbps.
815 */
816 int
817 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
818 {
819 #define N(a) (sizeof(a) / sizeof(a[0]))
820 static const struct {
821 u_int m; /* rate + mode */
822 u_int r; /* if_media rate */
823 } rates[] = {
824 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
825 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
826 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
827 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
828 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
829 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
830 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
831 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
832 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
833 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
834 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
835 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
836 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
837 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
838 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
839 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
840 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
841 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
842 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
843 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
844 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
845 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
846 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
847 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
848 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
849 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
850 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
851 /* NB: OFDM72 doesn't realy exist so we don't handle it */
852 };
853 u_int mask, i;
854
855 mask = rate & IEEE80211_RATE_VAL;
856 switch (mode) {
857 case IEEE80211_MODE_11A:
858 case IEEE80211_MODE_TURBO:
859 mask |= IFM_IEEE80211_11A;
860 break;
861 case IEEE80211_MODE_11B:
862 mask |= IFM_IEEE80211_11B;
863 break;
864 case IEEE80211_MODE_FH:
865 mask |= IFM_IEEE80211_FH;
866 break;
867 case IEEE80211_MODE_AUTO:
868 /* NB: ic may be NULL for some drivers */
869 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
870 mask |= IFM_IEEE80211_FH;
871 break;
872 }
873 /* NB: hack, 11g matches both 11b+11a rates */
874 /* fall thru... */
875 case IEEE80211_MODE_11G:
876 mask |= IFM_IEEE80211_11G;
877 break;
878 }
879 for (i = 0; i < N(rates); i++)
880 if (rates[i].m == mask)
881 return rates[i].r;
882 return IFM_AUTO;
883 #undef N
884 }
885
886 int
887 ieee80211_media2rate(int mword)
888 {
889 #define N(a) (sizeof(a) / sizeof(a[0]))
890 int i;
891 static const struct {
892 int subtype;
893 int rate;
894 } ieeerates[] = {
895 { IFM_AUTO, -1 },
896 { IFM_MANUAL, 0 },
897 { IFM_NONE, 0 },
898 { IFM_IEEE80211_FH1, 2 },
899 { IFM_IEEE80211_FH2, 4 },
900 { IFM_IEEE80211_DS1, 2 },
901 { IFM_IEEE80211_DS2, 4 },
902 { IFM_IEEE80211_DS5, 11 },
903 { IFM_IEEE80211_DS11, 22 },
904 { IFM_IEEE80211_DS22, 44 },
905 { IFM_IEEE80211_OFDM6, 12 },
906 { IFM_IEEE80211_OFDM9, 18 },
907 { IFM_IEEE80211_OFDM12, 24 },
908 { IFM_IEEE80211_OFDM18, 36 },
909 { IFM_IEEE80211_OFDM24, 48 },
910 { IFM_IEEE80211_OFDM36, 72 },
911 { IFM_IEEE80211_OFDM48, 96 },
912 { IFM_IEEE80211_OFDM54, 108 },
913 { IFM_IEEE80211_OFDM72, 144 },
914 };
915 for (i = 0; i < N(ieeerates); i++) {
916 if (ieeerates[i].subtype == IFM_SUBTYPE(mword))
917 return ieeerates[i].rate;
918 }
919 return 0;
920 #undef N
921 }
922
923 #ifdef __NetBSD__
924 /* TBD factor with sysctl_ath_verify. */
925 static int
926 sysctl_ieee80211_verify(SYSCTLFN_ARGS)
927 {
928 int error, t;
929 struct sysctlnode node;
930
931 node = *rnode;
932 t = *(int*)rnode->sysctl_data;
933 node.sysctl_data = &t;
934 error = sysctl_lookup(SYSCTLFN_CALL(&node));
935 if (error || newp == NULL)
936 return (error);
937
938 IEEE80211_DPRINTF(("%s: t = %d, nodenum = %d, rnodenum = %d\n",
939 __func__, t, node.sysctl_num, rnode->sysctl_num));
940
941 if (node.sysctl_num == ieee80211_inact_max_nodenum) {
942 if (t < 1)
943 return (EINVAL);
944 t = roundup(t, IEEE80211_INACT_WAIT) / IEEE80211_INACT_WAIT;
945 #ifdef IEEE80211_DEBUG
946 } else if (node.sysctl_num == ieee80211_debug_nodenum) {
947 if (t < 0 || t > 2)
948 return (EINVAL);
949 #endif /* IEEE80211_DEBUG */
950 } else
951 return (EINVAL);
952
953 *(int*)rnode->sysctl_data = t;
954
955 return (0);
956 }
957
958 /*
959 * Setup sysctl(3) MIB, net.ieee80211.*
960 *
961 * TBD condition CTLFLAG_PERMANENT on being an LKM or not
962 */
963 SYSCTL_SETUP(sysctl_ieee80211, "sysctl ieee80211 subtree setup")
964 {
965 int rc;
966 struct sysctlnode *cnode, *rnode;
967
968 if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
969 CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL,
970 NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0)
971 goto err;
972
973 if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
974 CTLFLAG_PERMANENT, CTLTYPE_NODE, "link",
975 "link-layer statistics and controls",
976 NULL, 0, NULL, 0, PF_LINK, CTL_EOL)) != 0)
977 goto err;
978
979 if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
980 CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211",
981 "IEEE 802.11 WLAN statistics and controls",
982 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
983 goto err;
984
985 #ifdef IEEE80211_DEBUG
986
987 /* control debugging printfs */
988 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
989 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
990 "debug", SYSCTL_DESCR("Enable IEEE 802.11 debugging output"),
991 sysctl_ieee80211_verify, 0, &ieee80211_debug, 0,
992 CTL_CREATE, CTL_EOL)) != 0)
993 goto err;
994
995 ieee80211_debug_nodenum = cnode->sysctl_num;
996
997 #endif /* IEEE80211_DEBUG */
998
999 /* control inactivity timer */
1000 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
1001 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
1002 "maxinact", SYSCTL_DESCR("Station inactivity timeout"),
1003 sysctl_ieee80211_verify, 0, &ieee80211_inact_max,
1004 0, CTL_CREATE, CTL_EOL)) != 0)
1005 goto err;
1006
1007 ieee80211_inact_max_nodenum = cnode->sysctl_num;
1008
1009 return;
1010 err:
1011 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
1012 }
1013 #endif /* __NetBSD__ */
1014
1015 #ifdef __FreeBSD__
1016 /*
1017 * Module glue.
1018 *
1019 * NB: the module name is "wlan" for compatibility with NetBSD.
1020 */
1021
1022 static int
1023 ieee80211_modevent(module_t mod, int type, void *unused)
1024 {
1025 switch (type) {
1026 case MOD_LOAD:
1027 if (bootverbose)
1028 printf("wlan: <802.11 Link Layer>\n");
1029 return 0;
1030 case MOD_UNLOAD:
1031 return 0;
1032 }
1033 return EINVAL;
1034 }
1035
1036 static moduledata_t ieee80211_mod = {
1037 "wlan",
1038 ieee80211_modevent,
1039 0
1040 };
1041 DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1042 MODULE_VERSION(wlan, 1);
1043 MODULE_DEPEND(wlan, rc4, 1, 1, 1);
1044 MODULE_DEPEND(wlan, ether, 1, 1, 1);
1045 #endif
1046