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