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