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