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