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