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