ieee80211.c revision 1.1.1.4 1 /*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2005 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
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.
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 *
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.
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211.c,v 1.19 2005/01/27 17:39:17 sam Exp $");
35
36 /*
37 * IEEE 802.11 generic handler
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43
44 #include <sys/socket.h>
45
46 #include <net/if.h>
47 #include <net/if_media.h>
48 #include <net/ethernet.h>
49
50 #include <net80211/ieee80211_var.h>
51
52 #include <net/bpf.h>
53
54 const char *ieee80211_phymode_name[] = {
55 "auto", /* IEEE80211_MODE_AUTO */
56 "11a", /* IEEE80211_MODE_11A */
57 "11b", /* IEEE80211_MODE_11B */
58 "11g", /* IEEE80211_MODE_11G */
59 "FH", /* IEEE80211_MODE_FH */
60 "turboA", /* IEEE80211_MODE_TURBO_A */
61 "turboG", /* IEEE80211_MODE_TURBO_G */
62 };
63
64 /* list of all instances */
65 SLIST_HEAD(ieee80211_list, ieee80211com);
66 static struct ieee80211_list ieee80211_list =
67 SLIST_HEAD_INITIALIZER(ieee80211_list);
68 static u_int8_t ieee80211_vapmap[32]; /* enough for 256 */
69 static struct mtx ieee80211_vap_mtx;
70 MTX_SYSINIT(ieee80211, &ieee80211_vap_mtx, "net80211 instances", MTX_DEF);
71
72 static void
73 ieee80211_add_vap(struct ieee80211com *ic)
74 {
75 #define N(a) (sizeof(a)/sizeof(a[0]))
76 int i;
77 u_int8_t b;
78
79 mtx_lock(&ieee80211_vap_mtx);
80 ic->ic_vap = 0;
81 for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++)
82 ic->ic_vap += NBBY;
83 if (i == N(ieee80211_vapmap))
84 panic("vap table full");
85 for (b = ieee80211_vapmap[i]; b & 1; b >>= 1)
86 ic->ic_vap++;
87 setbit(ieee80211_vapmap, ic->ic_vap);
88 SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next);
89 mtx_unlock(&ieee80211_vap_mtx);
90 #undef N
91 }
92
93 static void
94 ieee80211_remove_vap(struct ieee80211com *ic)
95 {
96 mtx_lock(&ieee80211_vap_mtx);
97 SLIST_REMOVE(&ieee80211_list, ic, ieee80211com, ic_next);
98 KASSERT(ic->ic_vap < sizeof(ieee80211_vapmap)*NBBY,
99 ("invalid vap id %d", ic->ic_vap));
100 KASSERT(isset(ieee80211_vapmap, ic->ic_vap),
101 ("vap id %d not allocated", ic->ic_vap));
102 clrbit(ieee80211_vapmap, ic->ic_vap);
103 mtx_unlock(&ieee80211_vap_mtx);
104 }
105
106 /*
107 * Default reset method for use with the ioctl support. This
108 * method is invoked after any state change in the 802.11
109 * layer that should be propagated to the hardware but not
110 * require re-initialization of the 802.11 state machine (e.g
111 * rescanning for an ap). We always return ENETRESET which
112 * should cause the driver to re-initialize the device. Drivers
113 * can override this method to implement more optimized support.
114 */
115 static int
116 ieee80211_default_reset(struct ifnet *ifp)
117 {
118 return ENETRESET;
119 }
120
121 void
122 ieee80211_ifattach(struct ieee80211com *ic)
123 {
124 struct ifnet *ifp = ic->ic_ifp;
125 struct ieee80211_channel *c;
126 int i;
127
128 ether_ifattach(ifp, ic->ic_myaddr);
129 bpfattach2(ifp, DLT_IEEE802_11,
130 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
131
132 ieee80211_crypto_attach(ic);
133
134 /*
135 * Fill in 802.11 available channel set, mark
136 * all available channels as active, and pick
137 * a default channel if not already specified.
138 */
139 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
140 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
141 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
142 c = &ic->ic_channels[i];
143 if (c->ic_flags) {
144 /*
145 * Verify driver passed us valid data.
146 */
147 if (i != ieee80211_chan2ieee(ic, c)) {
148 if_printf(ifp, "bad channel ignored; "
149 "freq %u flags %x number %u\n",
150 c->ic_freq, c->ic_flags, i);
151 c->ic_flags = 0; /* NB: remove */
152 continue;
153 }
154 setbit(ic->ic_chan_avail, i);
155 /*
156 * Identify mode capabilities.
157 */
158 if (IEEE80211_IS_CHAN_A(c))
159 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
160 if (IEEE80211_IS_CHAN_B(c))
161 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
162 if (IEEE80211_IS_CHAN_PUREG(c))
163 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
164 if (IEEE80211_IS_CHAN_FHSS(c))
165 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
166 if (IEEE80211_IS_CHAN_T(c))
167 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_A;
168 if (IEEE80211_IS_CHAN_108G(c))
169 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_G;
170 }
171 }
172 /* validate ic->ic_curmode */
173 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
174 ic->ic_curmode = IEEE80211_MODE_AUTO;
175 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
176 #if 0
177 /*
178 * Enable WME by default if we're capable.
179 */
180 if (ic->ic_caps & IEEE80211_C_WME)
181 ic->ic_flags |= IEEE80211_F_WME;
182 #endif
183 (void) ieee80211_setmode(ic, ic->ic_curmode);
184
185 if (ic->ic_lintval == 0)
186 ic->ic_lintval = IEEE80211_BINTVAL_DEFAULT;
187 ic->ic_bmisstimeout = 7*ic->ic_lintval; /* default 7 beacons */
188 ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT;
189 IEEE80211_BEACON_LOCK_INIT(ic, "beacon");
190
191 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
192
193 ieee80211_node_attach(ic);
194 ieee80211_proto_attach(ic);
195
196 ieee80211_add_vap(ic);
197
198 ieee80211_sysctl_attach(ic); /* NB: requires ic_vap */
199
200 /*
201 * Install a default reset method for the ioctl support.
202 * The driver is expected to fill this in before calling us.
203 */
204 if (ic->ic_reset == NULL)
205 ic->ic_reset = ieee80211_default_reset;
206 }
207
208 void
209 ieee80211_ifdetach(struct ieee80211com *ic)
210 {
211 struct ifnet *ifp = ic->ic_ifp;
212
213 ieee80211_remove_vap(ic);
214
215 ieee80211_sysctl_detach(ic);
216 ieee80211_proto_detach(ic);
217 ieee80211_crypto_detach(ic);
218 ieee80211_node_detach(ic);
219 ifmedia_removeall(&ic->ic_media);
220
221 IEEE80211_BEACON_LOCK_DESTROY(ic);
222
223 bpfdetach(ifp);
224 ether_ifdetach(ifp);
225 }
226
227 /*
228 * Convert MHz frequency to IEEE channel number.
229 */
230 u_int
231 ieee80211_mhz2ieee(u_int freq, u_int flags)
232 {
233 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
234 if (freq == 2484)
235 return 14;
236 if (freq < 2484)
237 return (freq - 2407) / 5;
238 else
239 return 15 + ((freq - 2512) / 20);
240 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
241 return (freq - 5000) / 5;
242 } else { /* either, guess */
243 if (freq == 2484)
244 return 14;
245 if (freq < 2484)
246 return (freq - 2407) / 5;
247 if (freq < 5000)
248 return 15 + ((freq - 2512) / 20);
249 return (freq - 5000) / 5;
250 }
251 }
252
253 /*
254 * Convert channel to IEEE channel number.
255 */
256 u_int
257 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
258 {
259 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
260 return c - ic->ic_channels;
261 else if (c == IEEE80211_CHAN_ANYC)
262 return IEEE80211_CHAN_ANY;
263 else if (c != NULL) {
264 if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n",
265 c->ic_freq, c->ic_flags);
266 return 0; /* XXX */
267 } else {
268 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
269 return 0; /* XXX */
270 }
271 }
272
273 /*
274 * Convert IEEE channel number to MHz frequency.
275 */
276 u_int
277 ieee80211_ieee2mhz(u_int chan, u_int flags)
278 {
279 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
280 if (chan == 14)
281 return 2484;
282 if (chan < 14)
283 return 2407 + chan*5;
284 else
285 return 2512 + ((chan-15)*20);
286 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
287 return 5000 + (chan*5);
288 } else { /* either, guess */
289 if (chan == 14)
290 return 2484;
291 if (chan < 14) /* 0-13 */
292 return 2407 + chan*5;
293 if (chan < 27) /* 15-26 */
294 return 2512 + ((chan-15)*20);
295 return 5000 + (chan*5);
296 }
297 }
298
299 /*
300 * Setup the media data structures according to the channel and
301 * rate tables. This must be called by the driver after
302 * ieee80211_attach and before most anything else.
303 */
304 void
305 ieee80211_media_init(struct ieee80211com *ic,
306 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
307 {
308 #define ADD(_ic, _s, _o) \
309 ifmedia_add(&(_ic)->ic_media, \
310 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
311 struct ifnet *ifp = ic->ic_ifp;
312 struct ifmediareq imr;
313 int i, j, mode, rate, maxrate, mword, mopt, r;
314 struct ieee80211_rateset *rs;
315 struct ieee80211_rateset allrates;
316
317 /*
318 * Do late attach work that must wait for any subclass
319 * (i.e. driver) work such as overriding methods.
320 */
321 ieee80211_node_lateattach(ic);
322
323 /*
324 * Fill in media characteristics.
325 */
326 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
327 maxrate = 0;
328 memset(&allrates, 0, sizeof(allrates));
329 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
330 static const u_int mopts[] = {
331 IFM_AUTO,
332 IFM_IEEE80211_11A,
333 IFM_IEEE80211_11B,
334 IFM_IEEE80211_11G,
335 IFM_IEEE80211_FH,
336 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
337 IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
338 };
339 if ((ic->ic_modecaps & (1<<mode)) == 0)
340 continue;
341 mopt = mopts[mode];
342 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
343 if (ic->ic_caps & IEEE80211_C_IBSS)
344 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
345 if (ic->ic_caps & IEEE80211_C_HOSTAP)
346 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
347 if (ic->ic_caps & IEEE80211_C_AHDEMO)
348 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
349 if (ic->ic_caps & IEEE80211_C_MONITOR)
350 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
351 if (mode == IEEE80211_MODE_AUTO)
352 continue;
353 rs = &ic->ic_sup_rates[mode];
354 for (i = 0; i < rs->rs_nrates; i++) {
355 rate = rs->rs_rates[i];
356 mword = ieee80211_rate2media(ic, rate, mode);
357 if (mword == 0)
358 continue;
359 ADD(ic, mword, mopt);
360 if (ic->ic_caps & IEEE80211_C_IBSS)
361 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
362 if (ic->ic_caps & IEEE80211_C_HOSTAP)
363 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
364 if (ic->ic_caps & IEEE80211_C_AHDEMO)
365 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
366 if (ic->ic_caps & IEEE80211_C_MONITOR)
367 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
368 /*
369 * Add rate to the collection of all rates.
370 */
371 r = rate & IEEE80211_RATE_VAL;
372 for (j = 0; j < allrates.rs_nrates; j++)
373 if (allrates.rs_rates[j] == r)
374 break;
375 if (j == allrates.rs_nrates) {
376 /* unique, add to the set */
377 allrates.rs_rates[j] = r;
378 allrates.rs_nrates++;
379 }
380 rate = (rate & IEEE80211_RATE_VAL) / 2;
381 if (rate > maxrate)
382 maxrate = rate;
383 }
384 }
385 for (i = 0; i < allrates.rs_nrates; i++) {
386 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
387 IEEE80211_MODE_AUTO);
388 if (mword == 0)
389 continue;
390 mword = IFM_SUBTYPE(mword); /* remove media options */
391 ADD(ic, mword, 0);
392 if (ic->ic_caps & IEEE80211_C_IBSS)
393 ADD(ic, mword, IFM_IEEE80211_ADHOC);
394 if (ic->ic_caps & IEEE80211_C_HOSTAP)
395 ADD(ic, mword, IFM_IEEE80211_HOSTAP);
396 if (ic->ic_caps & IEEE80211_C_AHDEMO)
397 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
398 if (ic->ic_caps & IEEE80211_C_MONITOR)
399 ADD(ic, mword, IFM_IEEE80211_MONITOR);
400 }
401 ieee80211_media_status(ifp, &imr);
402 ifmedia_set(&ic->ic_media, imr.ifm_active);
403
404 if (maxrate)
405 ifp->if_baudrate = IF_Mbps(maxrate);
406 #undef ADD
407 }
408
409 void
410 ieee80211_announce(struct ieee80211com *ic)
411 {
412 struct ifnet *ifp = ic->ic_ifp;
413 int i, mode, rate, mword;
414 struct ieee80211_rateset *rs;
415
416 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
417 if ((ic->ic_modecaps & (1<<mode)) == 0)
418 continue;
419 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
420 rs = &ic->ic_sup_rates[mode];
421 for (i = 0; i < rs->rs_nrates; i++) {
422 rate = rs->rs_rates[i];
423 mword = ieee80211_rate2media(ic, rate, mode);
424 if (mword == 0)
425 continue;
426 printf("%s%d%sMbps", (i != 0 ? " " : ""),
427 (rate & IEEE80211_RATE_VAL) / 2,
428 ((rate & 0x1) != 0 ? ".5" : ""));
429 }
430 printf("\n");
431 }
432 }
433
434 static int
435 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
436 {
437 #define IEEERATE(_ic,_m,_i) \
438 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
439 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
440 for (i = 0; i < nrates; i++)
441 if (IEEERATE(ic, mode, i) == rate)
442 return i;
443 return -1;
444 #undef IEEERATE
445 }
446
447 /*
448 * Find an instance by it's mac address.
449 */
450 struct ieee80211com *
451 ieee80211_find_vap(const u_int8_t mac[IEEE80211_ADDR_LEN])
452 {
453 struct ieee80211com *ic;
454
455 /* XXX lock */
456 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
457 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
458 return ic;
459 return NULL;
460 }
461
462 static struct ieee80211com *
463 ieee80211_find_instance(struct ifnet *ifp)
464 {
465 struct ieee80211com *ic;
466
467 /* XXX lock */
468 /* XXX not right for multiple instances but works for now */
469 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
470 if (ic->ic_ifp == ifp)
471 return ic;
472 return NULL;
473 }
474
475 /*
476 * Handle a media change request.
477 */
478 int
479 ieee80211_media_change(struct ifnet *ifp)
480 {
481 struct ieee80211com *ic;
482 struct ifmedia_entry *ime;
483 enum ieee80211_opmode newopmode;
484 enum ieee80211_phymode newphymode;
485 int i, j, newrate, error = 0;
486
487 ic = ieee80211_find_instance(ifp);
488 if (!ic) {
489 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
490 return EINVAL;
491 }
492 ime = ic->ic_media.ifm_cur;
493 /*
494 * First, identify the phy mode.
495 */
496 switch (IFM_MODE(ime->ifm_media)) {
497 case IFM_IEEE80211_11A:
498 newphymode = IEEE80211_MODE_11A;
499 break;
500 case IFM_IEEE80211_11B:
501 newphymode = IEEE80211_MODE_11B;
502 break;
503 case IFM_IEEE80211_11G:
504 newphymode = IEEE80211_MODE_11G;
505 break;
506 case IFM_IEEE80211_FH:
507 newphymode = IEEE80211_MODE_FH;
508 break;
509 case IFM_AUTO:
510 newphymode = IEEE80211_MODE_AUTO;
511 break;
512 default:
513 return EINVAL;
514 }
515 /*
516 * Turbo mode is an ``option''.
517 * XXX does not apply to AUTO
518 */
519 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
520 if (newphymode == IEEE80211_MODE_11A)
521 newphymode = IEEE80211_MODE_TURBO_A;
522 else if (newphymode == IEEE80211_MODE_11G)
523 newphymode = IEEE80211_MODE_TURBO_G;
524 else
525 return EINVAL;
526 }
527 /*
528 * Validate requested mode is available.
529 */
530 if ((ic->ic_modecaps & (1<<newphymode)) == 0)
531 return EINVAL;
532
533 /*
534 * Next, the fixed/variable rate.
535 */
536 i = -1;
537 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
538 /*
539 * Convert media subtype to rate.
540 */
541 newrate = ieee80211_media2rate(ime->ifm_media);
542 if (newrate == 0)
543 return EINVAL;
544 /*
545 * Check the rate table for the specified/current phy.
546 */
547 if (newphymode == IEEE80211_MODE_AUTO) {
548 /*
549 * In autoselect mode search for the rate.
550 */
551 for (j = IEEE80211_MODE_11A;
552 j < IEEE80211_MODE_MAX; j++) {
553 if ((ic->ic_modecaps & (1<<j)) == 0)
554 continue;
555 i = findrate(ic, j, newrate);
556 if (i != -1) {
557 /* lock mode too */
558 newphymode = j;
559 break;
560 }
561 }
562 } else {
563 i = findrate(ic, newphymode, newrate);
564 }
565 if (i == -1) /* mode/rate mismatch */
566 return EINVAL;
567 }
568 /* NB: defer rate setting to later */
569
570 /*
571 * Deduce new operating mode but don't install it just yet.
572 */
573 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
574 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
575 newopmode = IEEE80211_M_AHDEMO;
576 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
577 newopmode = IEEE80211_M_HOSTAP;
578 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
579 newopmode = IEEE80211_M_IBSS;
580 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
581 newopmode = IEEE80211_M_MONITOR;
582 else
583 newopmode = IEEE80211_M_STA;
584
585 /*
586 * Autoselect doesn't make sense when operating as an AP.
587 * If no phy mode has been selected, pick one and lock it
588 * down so rate tables can be used in forming beacon frames
589 * and the like.
590 */
591 if (newopmode == IEEE80211_M_HOSTAP &&
592 newphymode == IEEE80211_MODE_AUTO) {
593 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
594 if (ic->ic_modecaps & (1<<j)) {
595 newphymode = j;
596 break;
597 }
598 }
599
600 /*
601 * Handle phy mode change.
602 */
603 if (ic->ic_curmode != newphymode) { /* change phy mode */
604 error = ieee80211_setmode(ic, newphymode);
605 if (error != 0)
606 return error;
607 error = ENETRESET;
608 }
609
610 /*
611 * Committed to changes, install the rate setting.
612 */
613 if (ic->ic_fixed_rate != i) {
614 ic->ic_fixed_rate = i; /* set fixed tx rate */
615 error = ENETRESET;
616 }
617
618 /*
619 * Handle operating mode change.
620 */
621 if (ic->ic_opmode != newopmode) {
622 ic->ic_opmode = newopmode;
623 switch (newopmode) {
624 case IEEE80211_M_AHDEMO:
625 case IEEE80211_M_HOSTAP:
626 case IEEE80211_M_STA:
627 case IEEE80211_M_MONITOR:
628 ic->ic_flags &= ~IEEE80211_F_IBSSON;
629 break;
630 case IEEE80211_M_IBSS:
631 ic->ic_flags |= IEEE80211_F_IBSSON;
632 break;
633 }
634 /*
635 * Yech, slot time may change depending on the
636 * operating mode so reset it to be sure everything
637 * is setup appropriately.
638 */
639 ieee80211_reset_erp(ic);
640 ieee80211_wme_initparams(ic); /* after opmode change */
641 error = ENETRESET;
642 }
643 #ifdef notdef
644 if (error == 0)
645 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
646 #endif
647 return error;
648 }
649
650 void
651 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
652 {
653 struct ieee80211com *ic;
654 struct ieee80211_rateset *rs;
655
656 ic = ieee80211_find_instance(ifp);
657 if (!ic) {
658 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
659 return;
660 }
661 imr->ifm_status = IFM_AVALID;
662 imr->ifm_active = IFM_IEEE80211;
663 if (ic->ic_state == IEEE80211_S_RUN)
664 imr->ifm_status |= IFM_ACTIVE;
665 /*
666 * Calculate a current rate if possible.
667 */
668 if (ic->ic_fixed_rate != -1) {
669 /*
670 * A fixed rate is set, report that.
671 */
672 rs = &ic->ic_sup_rates[ic->ic_curmode];
673 imr->ifm_active |= ieee80211_rate2media(ic,
674 rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode);
675 } else if (ic->ic_opmode == IEEE80211_M_STA) {
676 /*
677 * In station mode report the current transmit rate.
678 */
679 rs = &ic->ic_bss->ni_rates;
680 imr->ifm_active |= ieee80211_rate2media(ic,
681 rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode);
682 } else
683 imr->ifm_active |= IFM_AUTO;
684 switch (ic->ic_opmode) {
685 case IEEE80211_M_STA:
686 break;
687 case IEEE80211_M_IBSS:
688 imr->ifm_active |= IFM_IEEE80211_ADHOC;
689 break;
690 case IEEE80211_M_AHDEMO:
691 /* should not come here */
692 break;
693 case IEEE80211_M_HOSTAP:
694 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
695 break;
696 case IEEE80211_M_MONITOR:
697 imr->ifm_active |= IFM_IEEE80211_MONITOR;
698 break;
699 }
700 switch (ic->ic_curmode) {
701 case IEEE80211_MODE_11A:
702 imr->ifm_active |= IFM_IEEE80211_11A;
703 break;
704 case IEEE80211_MODE_11B:
705 imr->ifm_active |= IFM_IEEE80211_11B;
706 break;
707 case IEEE80211_MODE_11G:
708 imr->ifm_active |= IFM_IEEE80211_11G;
709 break;
710 case IEEE80211_MODE_FH:
711 imr->ifm_active |= IFM_IEEE80211_FH;
712 break;
713 case IEEE80211_MODE_TURBO_A:
714 imr->ifm_active |= IFM_IEEE80211_11A
715 | IFM_IEEE80211_TURBO;
716 break;
717 case IEEE80211_MODE_TURBO_G:
718 imr->ifm_active |= IFM_IEEE80211_11G
719 | IFM_IEEE80211_TURBO;
720 break;
721 }
722 }
723
724 void
725 ieee80211_watchdog(struct ieee80211com *ic)
726 {
727 struct ieee80211_node_table *nt;
728 int need_inact_timer = 0;
729
730 if (ic->ic_state != IEEE80211_S_INIT) {
731 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
732 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
733 nt = &ic->ic_scan;
734 if (nt->nt_inact_timer) {
735 if (--nt->nt_inact_timer == 0)
736 nt->nt_timeout(nt);
737 need_inact_timer += nt->nt_inact_timer;
738 }
739 nt = &ic->ic_sta;
740 if (nt->nt_inact_timer) {
741 if (--nt->nt_inact_timer == 0)
742 nt->nt_timeout(nt);
743 need_inact_timer += nt->nt_inact_timer;
744 }
745 }
746 if (ic->ic_mgt_timer != 0 || need_inact_timer)
747 ic->ic_ifp->if_timer = 1;
748 }
749
750 /*
751 * Set the current phy mode and recalculate the active channel
752 * set based on the available channels for this mode. Also
753 * select a new default/current channel if the current one is
754 * inappropriate for this mode.
755 */
756 int
757 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
758 {
759 #define N(a) (sizeof(a) / sizeof(a[0]))
760 static const u_int chanflags[] = {
761 0, /* IEEE80211_MODE_AUTO */
762 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
763 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
764 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
765 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
766 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO_A */
767 IEEE80211_CHAN_108G, /* IEEE80211_MODE_TURBO_G */
768 };
769 struct ieee80211_channel *c;
770 u_int modeflags;
771 int i;
772
773 /* validate new mode */
774 if ((ic->ic_modecaps & (1<<mode)) == 0) {
775 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
776 "%s: mode %u not supported (caps 0x%x)\n",
777 __func__, mode, ic->ic_modecaps);
778 return EINVAL;
779 }
780
781 /*
782 * Verify at least one channel is present in the available
783 * channel list before committing to the new mode.
784 */
785 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
786 modeflags = chanflags[mode];
787 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
788 c = &ic->ic_channels[i];
789 if (mode == IEEE80211_MODE_AUTO) {
790 /* ignore turbo channels for autoselect */
791 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
792 break;
793 } else {
794 if ((c->ic_flags & modeflags) == modeflags)
795 break;
796 }
797 }
798 if (i > IEEE80211_CHAN_MAX) {
799 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
800 "%s: no channels found for mode %u\n", __func__, mode);
801 return EINVAL;
802 }
803
804 /*
805 * Calculate the active channel set.
806 */
807 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
808 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
809 c = &ic->ic_channels[i];
810 if (mode == IEEE80211_MODE_AUTO) {
811 /* take anything but pure turbo channels */
812 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
813 setbit(ic->ic_chan_active, i);
814 } else {
815 if ((c->ic_flags & modeflags) == modeflags)
816 setbit(ic->ic_chan_active, i);
817 }
818 }
819 /*
820 * If no current/default channel is setup or the current
821 * channel is wrong for the mode then pick the first
822 * available channel from the active list. This is likely
823 * not the right one.
824 */
825 if (ic->ic_ibss_chan == NULL ||
826 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
827 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
828 if (isset(ic->ic_chan_active, i)) {
829 ic->ic_ibss_chan = &ic->ic_channels[i];
830 break;
831 }
832 KASSERT(ic->ic_ibss_chan != NULL &&
833 isset(ic->ic_chan_active,
834 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
835 ("Bad IBSS channel %u",
836 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
837 }
838 /*
839 * If the desired channel is set but no longer valid then reset it.
840 */
841 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
842 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan)))
843 ic->ic_des_chan = IEEE80211_CHAN_ANYC;
844
845 /*
846 * Do mode-specific rate setup.
847 */
848 if (mode == IEEE80211_MODE_11G) {
849 /*
850 * Use a mixed 11b/11g rate set.
851 */
852 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
853 IEEE80211_MODE_11G);
854 } else if (mode == IEEE80211_MODE_11B) {
855 /*
856 * Force pure 11b rate set.
857 */
858 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
859 IEEE80211_MODE_11B);
860 }
861 /*
862 * Setup an initial rate set according to the
863 * current/default channel selected above. This
864 * will be changed when scanning but must exist
865 * now so driver have a consistent state of ic_ibss_chan.
866 */
867 if (ic->ic_bss) /* NB: can be called before lateattach */
868 ic->ic_bss->ni_rates = ic->ic_sup_rates[mode];
869
870 ic->ic_curmode = mode;
871 ieee80211_reset_erp(ic); /* reset ERP state */
872 ieee80211_wme_initparams(ic); /* reset WME stat */
873
874 return 0;
875 #undef N
876 }
877
878 /*
879 * Return the phy mode for with the specified channel so the
880 * caller can select a rate set. This is problematic for channels
881 * where multiple operating modes are possible (e.g. 11g+11b).
882 * In those cases we defer to the current operating mode when set.
883 */
884 enum ieee80211_phymode
885 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
886 {
887 if (IEEE80211_IS_CHAN_5GHZ(chan)) {
888 /*
889 * This assumes all 11a turbo channels are also
890 * usable withut turbo, which is currently true.
891 */
892 if (ic->ic_curmode == IEEE80211_MODE_TURBO_A)
893 return IEEE80211_MODE_TURBO_A;
894 return IEEE80211_MODE_11A;
895 } else if (IEEE80211_IS_CHAN_FHSS(chan))
896 return IEEE80211_MODE_FH;
897 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) {
898 /*
899 * This assumes all 11g channels are also usable
900 * for 11b, which is currently true.
901 */
902 if (ic->ic_curmode == IEEE80211_MODE_TURBO_G)
903 return IEEE80211_MODE_TURBO_G;
904 if (ic->ic_curmode == IEEE80211_MODE_11B)
905 return IEEE80211_MODE_11B;
906 return IEEE80211_MODE_11G;
907 } else
908 return IEEE80211_MODE_11B;
909 }
910
911 /*
912 * convert IEEE80211 rate value to ifmedia subtype.
913 * ieee80211 rate is in unit of 0.5Mbps.
914 */
915 int
916 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
917 {
918 #define N(a) (sizeof(a) / sizeof(a[0]))
919 static const struct {
920 u_int m; /* rate + mode */
921 u_int r; /* if_media rate */
922 } rates[] = {
923 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
924 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
925 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
926 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
927 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
928 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
929 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
930 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
931 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
932 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
933 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
934 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
935 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
936 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
937 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
938 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
939 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
940 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
941 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
942 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
943 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
944 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
945 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
946 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
947 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
948 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
949 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
950 /* NB: OFDM72 doesn't realy exist so we don't handle it */
951 };
952 u_int mask, i;
953
954 mask = rate & IEEE80211_RATE_VAL;
955 switch (mode) {
956 case IEEE80211_MODE_11A:
957 case IEEE80211_MODE_TURBO_A:
958 mask |= IFM_IEEE80211_11A;
959 break;
960 case IEEE80211_MODE_11B:
961 mask |= IFM_IEEE80211_11B;
962 break;
963 case IEEE80211_MODE_FH:
964 mask |= IFM_IEEE80211_FH;
965 break;
966 case IEEE80211_MODE_AUTO:
967 /* NB: ic may be NULL for some drivers */
968 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
969 mask |= IFM_IEEE80211_FH;
970 break;
971 }
972 /* NB: hack, 11g matches both 11b+11a rates */
973 /* fall thru... */
974 case IEEE80211_MODE_11G:
975 case IEEE80211_MODE_TURBO_G:
976 mask |= IFM_IEEE80211_11G;
977 break;
978 }
979 for (i = 0; i < N(rates); i++)
980 if (rates[i].m == mask)
981 return rates[i].r;
982 return IFM_AUTO;
983 #undef N
984 }
985
986 int
987 ieee80211_media2rate(int mword)
988 {
989 #define N(a) (sizeof(a) / sizeof(a[0]))
990 static const int ieeerates[] = {
991 -1, /* IFM_AUTO */
992 0, /* IFM_MANUAL */
993 0, /* IFM_NONE */
994 2, /* IFM_IEEE80211_FH1 */
995 4, /* IFM_IEEE80211_FH2 */
996 2, /* IFM_IEEE80211_DS1 */
997 4, /* IFM_IEEE80211_DS2 */
998 11, /* IFM_IEEE80211_DS5 */
999 22, /* IFM_IEEE80211_DS11 */
1000 44, /* IFM_IEEE80211_DS22 */
1001 12, /* IFM_IEEE80211_OFDM6 */
1002 18, /* IFM_IEEE80211_OFDM9 */
1003 24, /* IFM_IEEE80211_OFDM12 */
1004 36, /* IFM_IEEE80211_OFDM18 */
1005 48, /* IFM_IEEE80211_OFDM24 */
1006 72, /* IFM_IEEE80211_OFDM36 */
1007 96, /* IFM_IEEE80211_OFDM48 */
1008 108, /* IFM_IEEE80211_OFDM54 */
1009 144, /* IFM_IEEE80211_OFDM72 */
1010 };
1011 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1012 ieeerates[IFM_SUBTYPE(mword)] : 0;
1013 #undef N
1014 }
1015