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