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