80211 - Update up to FreeBSD's r287029 (only skipping a few minor changes)
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211.c
1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2009 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * IEEE 802.11 generic handler
32  */
33 #include "opt_wlan.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38
39 #include <sys/socket.h>
40
41 #include <machine/stdarg.h>
42
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/if_dl.h>
46 #include <net/if_media.h>
47 #include <net/if_types.h>
48 #include <net/ethernet.h>
49
50 #include <netproto/802_11/ieee80211_var.h>
51 #include <netproto/802_11/ieee80211_regdomain.h>
52 #ifdef IEEE80211_SUPPORT_SUPERG
53 #include <netproto/802_11/ieee80211_superg.h>
54 #endif
55 #include <netproto/802_11/ieee80211_ratectl.h>
56
57 #include <net/bpf.h>
58
59 #define IEEE80211_NMBCLUSTERS_DEFMIN    32
60 #define IEEE80211_NMBCLUSTERS_DEFAULT   128
61
62 static int ieee80211_nmbclusters_default = IEEE80211_NMBCLUSTERS_DEFAULT;
63 TUNABLE_INT("net.link.ieee80211.nmbclusters", &ieee80211_nmbclusters_default);
64
65 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
66         [IEEE80211_MODE_AUTO]     = "auto",
67         [IEEE80211_MODE_11A]      = "11a",
68         [IEEE80211_MODE_11B]      = "11b",
69         [IEEE80211_MODE_11G]      = "11g",
70         [IEEE80211_MODE_FH]       = "FH",
71         [IEEE80211_MODE_TURBO_A]  = "turboA",
72         [IEEE80211_MODE_TURBO_G]  = "turboG",
73         [IEEE80211_MODE_STURBO_A] = "sturboA",
74         [IEEE80211_MODE_HALF]     = "half",
75         [IEEE80211_MODE_QUARTER]  = "quarter",
76         [IEEE80211_MODE_11NA]     = "11na",
77         [IEEE80211_MODE_11NG]     = "11ng",
78 };
79 /* map ieee80211_opmode to the corresponding capability bit */
80 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
81         [IEEE80211_M_IBSS]      = IEEE80211_C_IBSS,
82         [IEEE80211_M_WDS]       = IEEE80211_C_WDS,
83         [IEEE80211_M_STA]       = IEEE80211_C_STA,
84         [IEEE80211_M_AHDEMO]    = IEEE80211_C_AHDEMO,
85         [IEEE80211_M_HOSTAP]    = IEEE80211_C_HOSTAP,
86         [IEEE80211_M_MONITOR]   = IEEE80211_C_MONITOR,
87 #ifdef IEEE80211_SUPPORT_MESH
88         [IEEE80211_M_MBSS]      = IEEE80211_C_MBSS,
89 #endif
90 };
91
92 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
93         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
94
95 static  void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
96 static  void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
97 static  void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
98 static  int ieee80211_media_setup(struct ieee80211com *ic,
99                 struct ifmedia *media, int caps, int addsta,
100                 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
101 static  void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
102 static  int ieee80211com_media_change(struct ifnet *);
103 static  int media_status(enum ieee80211_opmode,
104                 const struct ieee80211_channel *);
105
106 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
107
108 /*
109  * Default supported rates for 802.11 operation (in IEEE .5Mb units).
110  */
111 #define B(r)    ((r) | IEEE80211_RATE_BASIC)
112 static const struct ieee80211_rateset ieee80211_rateset_11a =
113         { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
114 static const struct ieee80211_rateset ieee80211_rateset_half =
115         { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
116 static const struct ieee80211_rateset ieee80211_rateset_quarter =
117         { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
118 static const struct ieee80211_rateset ieee80211_rateset_11b =
119         { 4, { B(2), B(4), B(11), B(22) } };
120 /* NB: OFDM rates are handled specially based on mode */
121 static const struct ieee80211_rateset ieee80211_rateset_11g =
122         { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
123 #undef B
124
125 /*
126  * Fill in 802.11 available channel set, mark
127  * all available channels as active, and pick
128  * a default channel if not already specified.
129  */
130 static void
131 ieee80211_chan_init(struct ieee80211com *ic)
132 {
133 #define DEFAULTRATES(m, def) do { \
134         if (ic->ic_sup_rates[m].rs_nrates == 0) \
135                 ic->ic_sup_rates[m] = def; \
136 } while (0)
137         struct ieee80211_channel *c;
138         int i;
139
140         KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
141                 ("invalid number of channels specified: %u", ic->ic_nchans));
142         memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
143         memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
144         setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
145         for (i = 0; i < ic->ic_nchans; i++) {
146                 c = &ic->ic_channels[i];
147                 KASSERT(c->ic_flags != 0, ("channel with no flags"));
148                 /*
149                  * Help drivers that work only with frequencies by filling
150                  * in IEEE channel #'s if not already calculated.  Note this
151                  * mimics similar work done in ieee80211_setregdomain when
152                  * changing regulatory state.
153                  */
154                 if (c->ic_ieee == 0)
155                         c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
156                 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
157                         c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
158                             (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
159                             c->ic_flags);
160                 /* default max tx power to max regulatory */
161                 if (c->ic_maxpower == 0)
162                         c->ic_maxpower = 2*c->ic_maxregpower;
163                 setbit(ic->ic_chan_avail, c->ic_ieee);
164                 /*
165                  * Identify mode capabilities.
166                  */
167                 if (IEEE80211_IS_CHAN_A(c))
168                         setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
169                 if (IEEE80211_IS_CHAN_B(c))
170                         setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
171                 if (IEEE80211_IS_CHAN_ANYG(c))
172                         setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
173                 if (IEEE80211_IS_CHAN_FHSS(c))
174                         setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
175                 if (IEEE80211_IS_CHAN_108A(c))
176                         setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
177                 if (IEEE80211_IS_CHAN_108G(c))
178                         setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
179                 if (IEEE80211_IS_CHAN_ST(c))
180                         setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
181                 if (IEEE80211_IS_CHAN_HALF(c))
182                         setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
183                 if (IEEE80211_IS_CHAN_QUARTER(c))
184                         setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
185                 if (IEEE80211_IS_CHAN_HTA(c))
186                         setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
187                 if (IEEE80211_IS_CHAN_HTG(c))
188                         setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
189         }
190         /* initialize candidate channels to all available */
191         memcpy(ic->ic_chan_active, ic->ic_chan_avail,
192                 sizeof(ic->ic_chan_avail));
193
194         /* sort channel table to allow lookup optimizations */
195         ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
196
197         /* invalidate any previous state */
198         ic->ic_bsschan = IEEE80211_CHAN_ANYC;
199         ic->ic_prevchan = NULL;
200         ic->ic_csa_newchan = NULL;
201         /* arbitrarily pick the first channel */
202         ic->ic_curchan = &ic->ic_channels[0];
203         ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
204
205         /* fillin well-known rate sets if driver has not specified */
206         DEFAULTRATES(IEEE80211_MODE_11B,         ieee80211_rateset_11b);
207         DEFAULTRATES(IEEE80211_MODE_11G,         ieee80211_rateset_11g);
208         DEFAULTRATES(IEEE80211_MODE_11A,         ieee80211_rateset_11a);
209         DEFAULTRATES(IEEE80211_MODE_TURBO_A,     ieee80211_rateset_11a);
210         DEFAULTRATES(IEEE80211_MODE_TURBO_G,     ieee80211_rateset_11g);
211         DEFAULTRATES(IEEE80211_MODE_STURBO_A,    ieee80211_rateset_11a);
212         DEFAULTRATES(IEEE80211_MODE_HALF,        ieee80211_rateset_half);
213         DEFAULTRATES(IEEE80211_MODE_QUARTER,     ieee80211_rateset_quarter);
214         DEFAULTRATES(IEEE80211_MODE_11NA,        ieee80211_rateset_11a);
215         DEFAULTRATES(IEEE80211_MODE_11NG,        ieee80211_rateset_11g);
216
217         /*
218          * Setup required information to fill the mcsset field, if driver did
219          * not. Assume a 2T2R setup for historic reasons.
220          */
221         if (ic->ic_rxstream == 0)
222                 ic->ic_rxstream = 2;
223         if (ic->ic_txstream == 0)
224                 ic->ic_txstream = 2;
225
226         /*
227          * Set auto mode to reset active channel state and any desired channel.
228          */
229         (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
230 #undef DEFAULTRATES
231 }
232
233 static void
234 null_update_mcast(struct ieee80211com *ic)
235 {
236         ic_printf(ic, "need multicast update callback\n");
237 }
238
239 static void
240 null_update_promisc(struct ieee80211com *ic)
241 {
242         ic_printf(ic, "need promiscuous mode update callback\n");
243 }
244
245 static int
246 null_transmit(struct ifnet *ifp, struct mbuf *m)
247 {
248         m_freem(m);
249         IFNET_STAT_INC(ifp, oerrors, 1);
250         return EACCES;          /* XXX EIO/EPERM? */
251 }
252
253 #if defined(__DragonFly__)
254 static int
255 null_output(struct ifnet *ifp, struct mbuf *m,
256             struct sockaddr *dst, struct rtentry *ro)
257 #else
258 static int
259 null_output(struct ifnet *ifp, struct mbuf *m,
260         const struct sockaddr *dst, struct route *ro)
261 #endif
262 {
263         if_printf(ifp, "discard raw packet\n");
264         return null_transmit(ifp, m);
265 }
266
267 #if defined(__DragonFly__)
268
269 static void
270 null_input(struct ifnet *ifp, struct mbuf *m,
271            const struct pktinfo *pi, int cpuid)
272 {
273         if_printf(ifp, "if_input should not be called\n");
274         m_freem(m);
275 }
276
277 #else
278
279 static void
280 null_input(struct ifnet *ifp, struct mbuf *m)
281 {
282         if_printf(ifp, "if_input should not be called\n");
283         m_freem(m);
284 }
285
286 #endif
287
288 static void
289 null_update_chw(struct ieee80211com *ic)
290 {
291
292         ic_printf(ic, "%s: need callback\n", __func__);
293 }
294
295 int
296 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
297 {
298         osdep_va_list ap;
299         int retval;
300
301         retval = kprintf("%s: ", ic->ic_name);
302         osdep_va_start(ap, fmt);
303         retval += kvprintf(fmt, ap);
304         osdep_va_end(ap);
305         return (retval);
306 }
307
308 /*
309  * Attach/setup the common net80211 state.  Called by
310  * the driver on attach to prior to creating any vap's.
311  */
312 void
313 ieee80211_ifattach(struct ieee80211com *ic,
314         const uint8_t macaddr[IEEE80211_ADDR_LEN])
315 {
316         struct ifnet *ifp = ic->ic_ifp;
317         struct sockaddr_dl *sdl;
318         struct ifaddr *ifa;
319
320         KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
321
322         IEEE80211_LOCK_INIT(ic, ic->ic_name);
323         IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
324         TAILQ_INIT(&ic->ic_vaps);
325
326         /* Create a taskqueue for all state changes */
327         ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
328             taskqueue_thread_enqueue, &ic->ic_tq);
329 #if defined(__DragonFly__)
330         taskqueue_start_threads(&ic->ic_tq, 1, TDPRI_KERN_DAEMON, -1,
331                                 "%s net80211 taskq", ic->ic_name);
332 #else
333         taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
334             ic->ic_name);
335 #endif
336         /*
337          * Fill in 802.11 available channel set, mark all
338          * available channels as active, and pick a default
339          * channel if not already specified.
340          */
341         ieee80211_media_init(ic);
342
343         ic->ic_update_mcast = null_update_mcast;
344         ic->ic_update_promisc = null_update_promisc;
345         ic->ic_update_chw = null_update_chw;
346
347         ic->ic_hash_key = arc4random();
348         ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
349         ic->ic_lintval = ic->ic_bintval;
350         ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
351
352         ieee80211_crypto_attach(ic);
353         ieee80211_node_attach(ic);
354         ieee80211_power_attach(ic);
355         ieee80211_proto_attach(ic);
356 #ifdef IEEE80211_SUPPORT_SUPERG
357         ieee80211_superg_attach(ic);
358 #endif
359         ieee80211_ht_attach(ic);
360         ieee80211_scan_attach(ic);
361         ieee80211_regdomain_attach(ic);
362         ieee80211_dfs_attach(ic);
363
364         ieee80211_sysctl_attach(ic);
365
366         ifp->if_addrlen = IEEE80211_ADDR_LEN;
367         ifp->if_hdrlen = 0;
368
369         /*
370          * If driver does not configure # of mbuf clusters/jclusters
371          * that could sit on the device queues for quite some time,
372          * we then assume:
373          * - The device queues only consume mbuf clusters.
374          * - No more than ieee80211_nmbclusters_default (by default
375          *   128) mbuf clusters will sit on the device queues for
376          *   quite some time.
377          */
378         if (ifp->if_nmbclusters <= 0 && ifp->if_nmbjclusters <= 0) {
379                 if (ieee80211_nmbclusters_default <
380                     IEEE80211_NMBCLUSTERS_DEFMIN) {
381                         kprintf("ieee80211 nmbclusters %d -> %d\n",
382                             ieee80211_nmbclusters_default,
383                             IEEE80211_NMBCLUSTERS_DEFAULT);
384                         ieee80211_nmbclusters_default =
385                             IEEE80211_NMBCLUSTERS_DEFAULT;
386                 }
387                 ifp->if_nmbclusters = ieee80211_nmbclusters_default;
388         }
389
390         CURVNET_SET(vnet0);
391
392         /*
393          * This function must _not_ be serialized by the WLAN serializer,
394          * since it could dead-lock the domsg to netisrs in if_attach().
395          */
396         wlan_serialize_exit();
397 #if defined(__DragonFly__)
398         if_attach(ifp, &wlan_global_serializer);
399 #else
400         if_attach(ifp);
401 #endif
402         wlan_serialize_enter();
403
404         ifp->if_mtu = IEEE80211_MTU_MAX;
405         ifp->if_broadcastaddr = ieee80211broadcastaddr;
406         ifp->if_output = null_output;
407         ifp->if_input = null_input;     /* just in case */
408         ifp->if_resolvemulti = NULL;    /* NB: callers check */
409
410         ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
411         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
412         sdl->sdl_type = IFT_ETHER;              /* XXX IFT_IEEE80211? */
413         sdl->sdl_alen = IEEE80211_ADDR_LEN;
414         IEEE80211_ADDR_COPY(LLADDR(sdl), macaddr);
415
416         CURVNET_RESTORE();
417 }
418
419 /*
420  * Detach net80211 state on device detach.  Tear down
421  * all vap's and reclaim all common state prior to the
422  * device state going away.  Note we may call back into
423  * driver; it must be prepared for this.
424  */
425 void
426 ieee80211_ifdetach(struct ieee80211com *ic)
427 {
428         struct ifnet *ifp = ic->ic_ifp;
429         struct ieee80211vap *vap;
430
431         /*
432          * The VAP is responsible for setting and clearing
433          * the VIMAGE context.
434          */
435         while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
436                 ieee80211_vap_destroy(vap);
437
438         /*
439          * WLAN serializer must _not_ be held for if_detach(),
440          * since it could dead-lock the domsg to netisrs.
441          *
442          * XXX
443          * This function actually should _not_ be serialized
444          * by the WLAN serializer, however, all 802.11 device
445          * drivers serialize it ...
446          */
447         wlan_serialize_exit();
448
449         /*
450          * This detaches the main interface, but not the vaps.
451          * Each VAP may be in a separate VIMAGE.
452          *
453          * Detach the main interface _after_ all vaps are
454          * destroyed, since the main interface is referenced
455          * on vaps' detach path.
456          */
457         CURVNET_SET(ifp->if_vnet);
458         if_detach(ifp);
459         CURVNET_RESTORE();
460
461         /* Re-hold WLAN serializer */
462         wlan_serialize_enter();
463
464         ieee80211_waitfor_parent(ic);
465
466         ieee80211_sysctl_detach(ic);
467         ieee80211_dfs_detach(ic);
468         ieee80211_regdomain_detach(ic);
469         ieee80211_scan_detach(ic);
470 #ifdef IEEE80211_SUPPORT_SUPERG
471         ieee80211_superg_detach(ic);
472 #endif
473         ieee80211_ht_detach(ic);
474         /* NB: must be called before ieee80211_node_detach */
475         ieee80211_proto_detach(ic);
476         ieee80211_crypto_detach(ic);
477         ieee80211_power_detach(ic);
478         ieee80211_node_detach(ic);
479
480         /* XXX VNET needed? */
481         ifmedia_removeall(&ic->ic_media);
482
483         taskqueue_free(ic->ic_tq);
484         IEEE80211_TX_LOCK_DESTROY(ic);
485         IEEE80211_LOCK_DESTROY(ic);
486 }
487
488 /*
489  * Default reset method for use with the ioctl support.  This
490  * method is invoked after any state change in the 802.11
491  * layer that should be propagated to the hardware but not
492  * require re-initialization of the 802.11 state machine (e.g
493  * rescanning for an ap).  We always return ENETRESET which
494  * should cause the driver to re-initialize the device. Drivers
495  * can override this method to implement more optimized support.
496  */
497 static int
498 default_reset(struct ieee80211vap *vap, u_long cmd)
499 {
500         return ENETRESET;
501 }
502
503 /*
504  * Prepare a vap for use.  Drivers use this call to
505  * setup net80211 state in new vap's prior attaching
506  * them with ieee80211_vap_attach (below).
507  */
508 int
509 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
510     const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
511     int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
512     const uint8_t macaddr[IEEE80211_ADDR_LEN])
513 {
514         struct ifnet *ifp;
515
516         ifp = if_alloc(IFT_ETHER);
517         if (ifp == NULL) {
518                 ic_printf(ic, "%s: unable to allocate ifnet\n",
519                     __func__);
520                 return ENOMEM;
521         }
522         if_initname(ifp, name, unit);
523         ifp->if_softc = vap;                    /* back pointer */
524         ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
525         ifp->if_start = ieee80211_vap_start;
526 #if 0
527         ifp->if_transmit = ieee80211_vap_transmit;
528         ifp->if_qflush = ieee80211_vap_qflush;
529 #endif
530         ifp->if_ioctl = ieee80211_ioctl;
531         ifp->if_init = ieee80211_init;
532
533         vap->iv_ifp = ifp;
534         vap->iv_ic = ic;
535         vap->iv_flags = ic->ic_flags;           /* propagate common flags */
536         vap->iv_flags_ext = ic->ic_flags_ext;
537         vap->iv_flags_ven = ic->ic_flags_ven;
538         vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
539         vap->iv_htcaps = ic->ic_htcaps;
540         vap->iv_htextcaps = ic->ic_htextcaps;
541         vap->iv_opmode = opmode;
542         vap->iv_caps |= ieee80211_opcap[opmode];
543         switch (opmode) {
544         case IEEE80211_M_WDS:
545                 /*
546                  * WDS links must specify the bssid of the far end.
547                  * For legacy operation this is a static relationship.
548                  * For non-legacy operation the station must associate
549                  * and be authorized to pass traffic.  Plumbing the
550                  * vap to the proper node happens when the vap
551                  * transitions to RUN state.
552                  */
553                 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
554                 vap->iv_flags |= IEEE80211_F_DESBSSID;
555                 if (flags & IEEE80211_CLONE_WDSLEGACY)
556                         vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
557                 break;
558 #ifdef IEEE80211_SUPPORT_TDMA
559         case IEEE80211_M_AHDEMO:
560                 if (flags & IEEE80211_CLONE_TDMA) {
561                         /* NB: checked before clone operation allowed */
562                         KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
563                             ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
564                         /*
565                          * Propagate TDMA capability to mark vap; this
566                          * cannot be removed and is used to distinguish
567                          * regular ahdemo operation from ahdemo+tdma.
568                          */
569                         vap->iv_caps |= IEEE80211_C_TDMA;
570                 }
571                 break;
572 #endif
573         default:
574                 break;
575         }
576         /* auto-enable s/w beacon miss support */
577         if (flags & IEEE80211_CLONE_NOBEACONS)
578                 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
579         /* auto-generated or user supplied MAC address */
580         if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
581                 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
582         /*
583          * Enable various functionality by default if we're
584          * capable; the driver can override us if it knows better.
585          */
586         if (vap->iv_caps & IEEE80211_C_WME)
587                 vap->iv_flags |= IEEE80211_F_WME;
588         if (vap->iv_caps & IEEE80211_C_BURST)
589                 vap->iv_flags |= IEEE80211_F_BURST;
590         /* NB: bg scanning only makes sense for station mode right now */
591 #if 0
592         /*
593          * DISABLE BGSCAN BY DEFAULT, many issues can crop up including
594          * the link going dead.
595          */
596         if (vap->iv_opmode == IEEE80211_M_STA &&
597             (vap->iv_caps & IEEE80211_C_BGSCAN))
598                 vap->iv_flags |= IEEE80211_F_BGSCAN;
599 #endif
600         vap->iv_flags |= IEEE80211_F_DOTH;      /* XXX no cap, just ena */
601         /* NB: DFS support only makes sense for ap mode right now */
602         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
603             (vap->iv_caps & IEEE80211_C_DFS))
604                 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
605
606         vap->iv_des_chan = IEEE80211_CHAN_ANYC;         /* any channel is ok */
607         vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
608         vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
609         /*
610          * Install a default reset method for the ioctl support;
611          * the driver can override this.
612          */
613         vap->iv_reset = default_reset;
614
615         IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
616
617         ieee80211_sysctl_vattach(vap);
618         ieee80211_crypto_vattach(vap);
619         ieee80211_node_vattach(vap);
620         ieee80211_power_vattach(vap);
621         ieee80211_proto_vattach(vap);
622 #ifdef IEEE80211_SUPPORT_SUPERG
623         ieee80211_superg_vattach(vap);
624 #endif
625         ieee80211_ht_vattach(vap);
626         ieee80211_scan_vattach(vap);
627         ieee80211_regdomain_vattach(vap);
628         ieee80211_radiotap_vattach(vap);
629         ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
630
631         return 0;
632 }
633
634 /*
635  * Activate a vap.  State should have been prepared with a
636  * call to ieee80211_vap_setup and by the driver.  On return
637  * from this call the vap is ready for use.
638  */
639 int
640 ieee80211_vap_attach(struct ieee80211vap *vap,
641         ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
642 {
643         struct ifnet *ifp = vap->iv_ifp;
644         struct ieee80211com *ic = vap->iv_ic;
645         struct ifmediareq imr;
646         int maxrate;
647
648         /*
649          * This function must _not_ be serialized by the WLAN serializer,
650          * since it could dead-lock the domsg to netisrs in ether_ifattach().
651          */
652         wlan_assert_notserialized();
653
654         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
655             "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
656             __func__, ieee80211_opmode_name[vap->iv_opmode],
657             ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
658
659         /*
660          * Do late attach work that cannot happen until after
661          * the driver has had a chance to override defaults.
662          */
663         ieee80211_node_latevattach(vap);
664         ieee80211_power_latevattach(vap);
665
666         maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
667             vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
668         ieee80211_media_status(ifp, &imr);
669         /* NB: strip explicit mode; we're actually in autoselect */
670         ifmedia_set(&vap->iv_media,
671             imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
672         if (maxrate)
673                 ifp->if_baudrate = IF_Mbps(maxrate);
674
675 #if defined(__DragonFly__)
676         ether_ifattach(ifp, vap->iv_myaddr, &wlan_global_serializer);
677 #else
678         ether_ifattach(ifp, vap->iv_myaddr);
679 #endif
680         /* hook output method setup by ether_ifattach */
681         vap->iv_output = ifp->if_output;
682         ifp->if_output = ieee80211_output;
683         /* NB: if_mtu set by ether_ifattach to ETHERMTU */
684
685         IEEE80211_LOCK(ic);
686         TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
687         ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
688 #ifdef IEEE80211_SUPPORT_SUPERG
689         ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
690 #endif
691         ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
692         ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
693         ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
694         ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
695         ieee80211_syncifflag_locked(ic, IFF_PROMISC);
696         ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
697         IEEE80211_UNLOCK(ic);
698
699         return 1;
700 }
701
702 /* 
703  * Tear down vap state and reclaim the ifnet.
704  * The driver is assumed to have prepared for
705  * this; e.g. by turning off interrupts for the
706  * underlying device.
707  */
708 void
709 ieee80211_vap_detach(struct ieee80211vap *vap)
710 {
711         struct ieee80211com *ic = vap->iv_ic;
712         struct ifnet *ifp = vap->iv_ifp;
713
714         /*
715          * This function must _not_ be serialized by the WLAN serializer,
716          * since it could dead-lock the domsg to netisrs in ether_ifdettach().
717          */
718         wlan_assert_notserialized();
719
720         CURVNET_SET(ifp->if_vnet);
721
722         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
723             __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
724
725         /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
726         ether_ifdetach(ifp);
727
728         ieee80211_stop(vap);
729
730         /*
731          * Flush any deferred vap tasks.
732          */
733         ieee80211_draintask(ic, &vap->iv_nstate_task);
734         ieee80211_draintask(ic, &vap->iv_swbmiss_task);
735
736 #if !defined(__DragonFly__)
737         /* XXX band-aid until ifnet handles this for us */
738         taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
739 #endif
740
741         IEEE80211_LOCK(ic);
742         KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
743         TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
744         ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
745 #ifdef IEEE80211_SUPPORT_SUPERG
746         ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
747 #endif
748         ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
749         ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
750         ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
751         ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
752         /* NB: this handles the bpfdetach done below */
753         ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
754         ieee80211_syncifflag_locked(ic, IFF_PROMISC);
755         ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
756         IEEE80211_UNLOCK(ic);
757
758         ifmedia_removeall(&vap->iv_media);
759
760         ieee80211_radiotap_vdetach(vap);
761         ieee80211_regdomain_vdetach(vap);
762         ieee80211_scan_vdetach(vap);
763 #ifdef IEEE80211_SUPPORT_SUPERG
764         ieee80211_superg_vdetach(vap);
765 #endif
766         ieee80211_ht_vdetach(vap);
767         /* NB: must be before ieee80211_node_vdetach */
768         ieee80211_proto_vdetach(vap);
769         ieee80211_crypto_vdetach(vap);
770         ieee80211_power_vdetach(vap);
771         ieee80211_node_vdetach(vap);
772         ieee80211_sysctl_vdetach(vap);
773
774         if_free(ifp);
775
776         CURVNET_RESTORE();
777 }
778
779 /*
780  * Synchronize flag bit state in the parent ifnet structure
781  * according to the state of all vap ifnet's.  This is used,
782  * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
783  */
784 void
785 ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
786 {
787         struct ifnet *ifp = ic->ic_ifp;
788         struct ieee80211vap *vap;
789         int bit, oflags;
790
791         IEEE80211_LOCK_ASSERT(ic);
792
793         bit = 0;
794         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
795                 if (vap->iv_ifp->if_flags & flag) {
796                         /*
797                          * XXX the bridge sets PROMISC but we don't want to
798                          * enable it on the device, discard here so all the
799                          * drivers don't need to special-case it
800                          */
801                         if (flag == IFF_PROMISC &&
802                             !(vap->iv_opmode == IEEE80211_M_MONITOR ||
803                               (vap->iv_opmode == IEEE80211_M_AHDEMO &&
804                                (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
805                                 continue;
806                         bit = 1;
807                         break;
808                 }
809         oflags = ifp->if_flags;
810         if (bit)
811                 ifp->if_flags |= flag;
812         else
813                 ifp->if_flags &= ~flag;
814         if ((ifp->if_flags ^ oflags) & flag) {
815                 /* XXX should we return 1/0 and let caller do this? */
816                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
817                         if (flag == IFF_PROMISC)
818                                 ieee80211_runtask(ic, &ic->ic_promisc_task);
819                         else if (flag == IFF_ALLMULTI)
820                                 ieee80211_runtask(ic, &ic->ic_mcast_task);
821                 }
822         }
823 }
824
825 /*
826  * Synchronize flag bit state in the com structure
827  * according to the state of all vap's.  This is used,
828  * for example, to handle state changes via ioctls.
829  */
830 static void
831 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
832 {
833         struct ieee80211vap *vap;
834         int bit;
835
836         IEEE80211_LOCK_ASSERT(ic);
837
838         bit = 0;
839         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
840                 if (vap->iv_flags & flag) {
841                         bit = 1;
842                         break;
843                 }
844         if (bit)
845                 ic->ic_flags |= flag;
846         else
847                 ic->ic_flags &= ~flag;
848 }
849
850 void
851 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
852 {
853         struct ieee80211com *ic = vap->iv_ic;
854
855         IEEE80211_LOCK(ic);
856         if (flag < 0) {
857                 flag = -flag;
858                 vap->iv_flags &= ~flag;
859         } else
860                 vap->iv_flags |= flag;
861         ieee80211_syncflag_locked(ic, flag);
862         IEEE80211_UNLOCK(ic);
863 }
864
865 /*
866  * Synchronize flags_ht bit state in the com structure
867  * according to the state of all vap's.  This is used,
868  * for example, to handle state changes via ioctls.
869  */
870 static void
871 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
872 {
873         struct ieee80211vap *vap;
874         int bit;
875
876         IEEE80211_LOCK_ASSERT(ic);
877
878         bit = 0;
879         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
880                 if (vap->iv_flags_ht & flag) {
881                         bit = 1;
882                         break;
883                 }
884         if (bit)
885                 ic->ic_flags_ht |= flag;
886         else
887                 ic->ic_flags_ht &= ~flag;
888 }
889
890 void
891 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
892 {
893         struct ieee80211com *ic = vap->iv_ic;
894
895         IEEE80211_LOCK(ic);
896         if (flag < 0) {
897                 flag = -flag;
898                 vap->iv_flags_ht &= ~flag;
899         } else
900                 vap->iv_flags_ht |= flag;
901         ieee80211_syncflag_ht_locked(ic, flag);
902         IEEE80211_UNLOCK(ic);
903 }
904
905 /*
906  * Synchronize flags_ext bit state in the com structure
907  * according to the state of all vap's.  This is used,
908  * for example, to handle state changes via ioctls.
909  */
910 static void
911 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
912 {
913         struct ieee80211vap *vap;
914         int bit;
915
916         IEEE80211_LOCK_ASSERT(ic);
917
918         bit = 0;
919         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
920                 if (vap->iv_flags_ext & flag) {
921                         bit = 1;
922                         break;
923                 }
924         if (bit)
925                 ic->ic_flags_ext |= flag;
926         else
927                 ic->ic_flags_ext &= ~flag;
928 }
929
930 void
931 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
932 {
933         struct ieee80211com *ic = vap->iv_ic;
934
935         IEEE80211_LOCK(ic);
936         if (flag < 0) {
937                 flag = -flag;
938                 vap->iv_flags_ext &= ~flag;
939         } else
940                 vap->iv_flags_ext |= flag;
941         ieee80211_syncflag_ext_locked(ic, flag);
942         IEEE80211_UNLOCK(ic);
943 }
944
945 static __inline int
946 mapgsm(u_int freq, u_int flags)
947 {
948         freq *= 10;
949         if (flags & IEEE80211_CHAN_QUARTER)
950                 freq += 5;
951         else if (flags & IEEE80211_CHAN_HALF)
952                 freq += 10;
953         else
954                 freq += 20;
955         /* NB: there is no 907/20 wide but leave room */
956         return (freq - 906*10) / 5;
957 }
958
959 static __inline int
960 mappsb(u_int freq, u_int flags)
961 {
962         return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
963 }
964
965 /*
966  * Convert MHz frequency to IEEE channel number.
967  */
968 int
969 ieee80211_mhz2ieee(u_int freq, u_int flags)
970 {
971 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
972         if (flags & IEEE80211_CHAN_GSM)
973                 return mapgsm(freq, flags);
974         if (flags & IEEE80211_CHAN_2GHZ) {      /* 2GHz band */
975                 if (freq == 2484)
976                         return 14;
977                 if (freq < 2484)
978                         return ((int) freq - 2407) / 5;
979                 else
980                         return 15 + ((freq - 2512) / 20);
981         } else if (flags & IEEE80211_CHAN_5GHZ) {       /* 5Ghz band */
982                 if (freq <= 5000) {
983                         /* XXX check regdomain? */
984                         if (IS_FREQ_IN_PSB(freq))
985                                 return mappsb(freq, flags);
986                         return (freq - 4000) / 5;
987                 } else
988                         return (freq - 5000) / 5;
989         } else {                                /* either, guess */
990                 if (freq == 2484)
991                         return 14;
992                 if (freq < 2484) {
993                         if (907 <= freq && freq <= 922)
994                                 return mapgsm(freq, flags);
995                         return ((int) freq - 2407) / 5;
996                 }
997                 if (freq < 5000) {
998                         if (IS_FREQ_IN_PSB(freq))
999                                 return mappsb(freq, flags);
1000                         else if (freq > 4900)
1001                                 return (freq - 4000) / 5;
1002                         else
1003                                 return 15 + ((freq - 2512) / 20);
1004                 }
1005                 return (freq - 5000) / 5;
1006         }
1007 #undef IS_FREQ_IN_PSB
1008 }
1009
1010 /*
1011  * Convert channel to IEEE channel number.
1012  */
1013 int
1014 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
1015 {
1016         if (c == NULL) {
1017                 ic_printf(ic, "invalid channel (NULL)\n");
1018                 return 0;               /* XXX */
1019         }
1020         return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
1021 }
1022
1023 /*
1024  * Convert IEEE channel number to MHz frequency.
1025  */
1026 u_int
1027 ieee80211_ieee2mhz(u_int chan, u_int flags)
1028 {
1029         if (flags & IEEE80211_CHAN_GSM)
1030                 return 907 + 5 * (chan / 10);
1031         if (flags & IEEE80211_CHAN_2GHZ) {      /* 2GHz band */
1032                 if (chan == 14)
1033                         return 2484;
1034                 if (chan < 14)
1035                         return 2407 + chan*5;
1036                 else
1037                         return 2512 + ((chan-15)*20);
1038         } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
1039                 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
1040                         chan -= 37;
1041                         return 4940 + chan*5 + (chan % 5 ? 2 : 0);
1042                 }
1043                 return 5000 + (chan*5);
1044         } else {                                /* either, guess */
1045                 /* XXX can't distinguish PSB+GSM channels */
1046                 if (chan == 14)
1047                         return 2484;
1048                 if (chan < 14)                  /* 0-13 */
1049                         return 2407 + chan*5;
1050                 if (chan < 27)                  /* 15-26 */
1051                         return 2512 + ((chan-15)*20);
1052                 return 5000 + (chan*5);
1053         }
1054 }
1055
1056 /*
1057  * Locate a channel given a frequency+flags.  We cache
1058  * the previous lookup to optimize switching between two
1059  * channels--as happens with dynamic turbo.
1060  */
1061 struct ieee80211_channel *
1062 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1063 {
1064         struct ieee80211_channel *c;
1065         int i;
1066
1067         flags &= IEEE80211_CHAN_ALLTURBO;
1068         c = ic->ic_prevchan;
1069         if (c != NULL && c->ic_freq == freq &&
1070             (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1071                 return c;
1072         /* brute force search */
1073         for (i = 0; i < ic->ic_nchans; i++) {
1074                 c = &ic->ic_channels[i];
1075                 if (c->ic_freq == freq &&
1076                     (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1077                         return c;
1078         }
1079         return NULL;
1080 }
1081
1082 /*
1083  * Locate a channel given a channel number+flags.  We cache
1084  * the previous lookup to optimize switching between two
1085  * channels--as happens with dynamic turbo.
1086  */
1087 struct ieee80211_channel *
1088 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1089 {
1090         struct ieee80211_channel *c;
1091         int i;
1092
1093         flags &= IEEE80211_CHAN_ALLTURBO;
1094         c = ic->ic_prevchan;
1095         if (c != NULL && c->ic_ieee == ieee &&
1096             (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1097                 return c;
1098         /* brute force search */
1099         for (i = 0; i < ic->ic_nchans; i++) {
1100                 c = &ic->ic_channels[i];
1101                 if (c->ic_ieee == ieee &&
1102                     (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1103                         return c;
1104         }
1105         return NULL;
1106 }
1107
1108 /*
1109  * Lookup a channel suitable for the given rx status.
1110  *
1111  * This is used to find a channel for a frame (eg beacon, probe
1112  * response) based purely on the received PHY information.
1113  *
1114  * For now it tries to do it based on R_FREQ / R_IEEE.
1115  * This is enough for 11bg and 11a (and thus 11ng/11na)
1116  * but it will not be enough for GSM, PSB channels and the
1117  * like.  It also doesn't know about legacy-turbog and
1118  * legacy-turbo modes, which some offload NICs actually
1119  * support in weird ways.
1120  *
1121  * Takes the ic and rxstatus; returns the channel or NULL
1122  * if not found.
1123  *
1124  * XXX TODO: Add support for that when the need arises.
1125  */
1126 struct ieee80211_channel *
1127 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1128     const struct ieee80211_rx_stats *rxs)
1129 {
1130         struct ieee80211com *ic = vap->iv_ic;
1131         uint32_t flags;
1132         struct ieee80211_channel *c;
1133
1134         if (rxs == NULL)
1135                 return (NULL);
1136
1137         /*
1138          * Strictly speaking we only use freq for now,
1139          * however later on we may wish to just store
1140          * the ieee for verification.
1141          */
1142         if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1143                 return (NULL);
1144         if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1145                 return (NULL);
1146
1147         /*
1148          * If the rx status contains a valid ieee/freq, then
1149          * ensure we populate the correct channel information
1150          * in rxchan before passing it up to the scan infrastructure.
1151          * Offload NICs will pass up beacons from all channels
1152          * during background scans.
1153          */
1154
1155         /* Determine a band */
1156         /* XXX should be done by the driver? */
1157         if (rxs->c_freq < 3000) {
1158                 flags = IEEE80211_CHAN_G;
1159         } else {
1160                 flags = IEEE80211_CHAN_A;
1161         }
1162
1163         /* Channel lookup */
1164         c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1165
1166         IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1167             "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1168             __func__,
1169             (int) rxs->c_freq,
1170             (int) rxs->c_ieee,
1171             flags,
1172             c);
1173
1174         return (c);
1175 }
1176
1177 static void
1178 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1179 {
1180 #define ADD(_ic, _s, _o) \
1181         ifmedia_add(media, \
1182                 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1183         static const u_int mopts[IEEE80211_MODE_MAX] = { 
1184             [IEEE80211_MODE_AUTO]       = IFM_AUTO,
1185             [IEEE80211_MODE_11A]        = IFM_IEEE80211_11A,
1186             [IEEE80211_MODE_11B]        = IFM_IEEE80211_11B,
1187             [IEEE80211_MODE_11G]        = IFM_IEEE80211_11G,
1188             [IEEE80211_MODE_FH]         = IFM_IEEE80211_FH,
1189             [IEEE80211_MODE_TURBO_A]    = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1190             [IEEE80211_MODE_TURBO_G]    = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1191             [IEEE80211_MODE_STURBO_A]   = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1192             [IEEE80211_MODE_HALF]       = IFM_IEEE80211_11A,    /* XXX */
1193             [IEEE80211_MODE_QUARTER]    = IFM_IEEE80211_11A,    /* XXX */
1194             [IEEE80211_MODE_11NA]       = IFM_IEEE80211_11NA,
1195             [IEEE80211_MODE_11NG]       = IFM_IEEE80211_11NG,
1196         };
1197         u_int mopt;
1198
1199         mopt = mopts[mode];
1200         if (addsta)
1201                 ADD(ic, mword, mopt);   /* STA mode has no cap */
1202         if (caps & IEEE80211_C_IBSS)
1203                 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1204         if (caps & IEEE80211_C_HOSTAP)
1205                 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1206         if (caps & IEEE80211_C_AHDEMO)
1207                 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1208         if (caps & IEEE80211_C_MONITOR)
1209                 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1210         if (caps & IEEE80211_C_WDS)
1211                 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1212         if (caps & IEEE80211_C_MBSS)
1213                 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1214 #undef ADD
1215 }
1216
1217 /*
1218  * Setup the media data structures according to the channel and
1219  * rate tables.
1220  */
1221 static int
1222 ieee80211_media_setup(struct ieee80211com *ic,
1223         struct ifmedia *media, int caps, int addsta,
1224         ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1225 {
1226         int i, j, rate, maxrate, mword, r;
1227         enum ieee80211_phymode mode;
1228         const struct ieee80211_rateset *rs;
1229         struct ieee80211_rateset allrates;
1230
1231         /*
1232          * Fill in media characteristics.
1233          */
1234         ifmedia_init(media, 0, media_change, media_stat);
1235         maxrate = 0;
1236         /*
1237          * Add media for legacy operating modes.
1238          */
1239         memset(&allrates, 0, sizeof(allrates));
1240         for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1241                 if (isclr(ic->ic_modecaps, mode))
1242                         continue;
1243                 addmedia(media, caps, addsta, mode, IFM_AUTO);
1244                 if (mode == IEEE80211_MODE_AUTO)
1245                         continue;
1246                 rs = &ic->ic_sup_rates[mode];
1247                 for (i = 0; i < rs->rs_nrates; i++) {
1248                         rate = rs->rs_rates[i];
1249                         mword = ieee80211_rate2media(ic, rate, mode);
1250                         if (mword == 0)
1251                                 continue;
1252                         addmedia(media, caps, addsta, mode, mword);
1253                         /*
1254                          * Add legacy rate to the collection of all rates.
1255                          */
1256                         r = rate & IEEE80211_RATE_VAL;
1257                         for (j = 0; j < allrates.rs_nrates; j++)
1258                                 if (allrates.rs_rates[j] == r)
1259                                         break;
1260                         if (j == allrates.rs_nrates) {
1261                                 /* unique, add to the set */
1262                                 allrates.rs_rates[j] = r;
1263                                 allrates.rs_nrates++;
1264                         }
1265                         rate = (rate & IEEE80211_RATE_VAL) / 2;
1266                         if (rate > maxrate)
1267                                 maxrate = rate;
1268                 }
1269         }
1270         for (i = 0; i < allrates.rs_nrates; i++) {
1271                 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1272                                 IEEE80211_MODE_AUTO);
1273                 if (mword == 0)
1274                         continue;
1275                 /* NB: remove media options from mword */
1276                 addmedia(media, caps, addsta,
1277                     IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1278         }
1279         /*
1280          * Add HT/11n media.  Note that we do not have enough
1281          * bits in the media subtype to express the MCS so we
1282          * use a "placeholder" media subtype and any fixed MCS
1283          * must be specified with a different mechanism.
1284          */
1285         for (; mode <= IEEE80211_MODE_11NG; mode++) {
1286                 if (isclr(ic->ic_modecaps, mode))
1287                         continue;
1288                 addmedia(media, caps, addsta, mode, IFM_AUTO);
1289                 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1290         }
1291         if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1292             isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1293                 addmedia(media, caps, addsta,
1294                     IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1295                 i = ic->ic_txstream * 8 - 1;
1296                 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1297                     (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1298                         rate = ieee80211_htrates[i].ht40_rate_400ns;
1299                 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1300                         rate = ieee80211_htrates[i].ht40_rate_800ns;
1301                 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1302                         rate = ieee80211_htrates[i].ht20_rate_400ns;
1303                 else
1304                         rate = ieee80211_htrates[i].ht20_rate_800ns;
1305                 if (rate > maxrate)
1306                         maxrate = rate;
1307         }
1308         return maxrate;
1309 }
1310
1311 void
1312 ieee80211_media_init(struct ieee80211com *ic)
1313 {
1314         struct ifnet *ifp = ic->ic_ifp;
1315         int maxrate;
1316
1317         /* NB: this works because the structure is initialized to zero */
1318         if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1319                 /*
1320                  * We are re-initializing the channel list; clear
1321                  * the existing media state as the media routines
1322                  * don't suppress duplicates.
1323                  */
1324                 ifmedia_removeall(&ic->ic_media);
1325         }
1326         ieee80211_chan_init(ic);
1327
1328         /*
1329          * Recalculate media settings in case new channel list changes
1330          * the set of available modes.
1331          */
1332         maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1333                 ieee80211com_media_change, ieee80211com_media_status);
1334         /* NB: strip explicit mode; we're actually in autoselect */
1335         ifmedia_set(&ic->ic_media,
1336             media_status(ic->ic_opmode, ic->ic_curchan) &~
1337                 (IFM_MMASK | IFM_IEEE80211_TURBO));
1338         if (maxrate)
1339                 ifp->if_baudrate = IF_Mbps(maxrate);
1340
1341         /* XXX need to propagate new media settings to vap's */
1342 }
1343
1344 /* XXX inline or eliminate? */
1345 const struct ieee80211_rateset *
1346 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1347 {
1348         /* XXX does this work for 11ng basic rates? */
1349         return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1350 }
1351
1352 void
1353 ieee80211_announce(struct ieee80211com *ic)
1354 {
1355         int i, rate, mword;
1356         enum ieee80211_phymode mode;
1357         const struct ieee80211_rateset *rs;
1358
1359         /* NB: skip AUTO since it has no rates */
1360         for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1361                 if (isclr(ic->ic_modecaps, mode))
1362                         continue;
1363                 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1364                 rs = &ic->ic_sup_rates[mode];
1365                 for (i = 0; i < rs->rs_nrates; i++) {
1366                         mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1367                         if (mword == 0)
1368                                 continue;
1369                         rate = ieee80211_media2rate(mword);
1370                         kprintf("%s%d%sMbps", (i != 0 ? " " : ""),
1371                             rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1372                 }
1373                 kprintf("\n");
1374         }
1375         ieee80211_ht_announce(ic);
1376 }
1377
1378 void
1379 ieee80211_announce_channels(struct ieee80211com *ic)
1380 {
1381         const struct ieee80211_channel *c;
1382         char type;
1383         int i, cw;
1384
1385         kprintf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1386         for (i = 0; i < ic->ic_nchans; i++) {
1387                 c = &ic->ic_channels[i];
1388                 if (IEEE80211_IS_CHAN_ST(c))
1389                         type = 'S';
1390                 else if (IEEE80211_IS_CHAN_108A(c))
1391                         type = 'T';
1392                 else if (IEEE80211_IS_CHAN_108G(c))
1393                         type = 'G';
1394                 else if (IEEE80211_IS_CHAN_HT(c))
1395                         type = 'n';
1396                 else if (IEEE80211_IS_CHAN_A(c))
1397                         type = 'a';
1398                 else if (IEEE80211_IS_CHAN_ANYG(c))
1399                         type = 'g';
1400                 else if (IEEE80211_IS_CHAN_B(c))
1401                         type = 'b';
1402                 else
1403                         type = 'f';
1404                 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1405                         cw = 40;
1406                 else if (IEEE80211_IS_CHAN_HALF(c))
1407                         cw = 10;
1408                 else if (IEEE80211_IS_CHAN_QUARTER(c))
1409                         cw = 5;
1410                 else
1411                         cw = 20;
1412                 kprintf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1413                         , c->ic_ieee, c->ic_freq, type
1414                         , cw
1415                         , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1416                           IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1417                         , c->ic_maxregpower
1418                         , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1419                         , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1420                 );
1421         }
1422 }
1423
1424 static int
1425 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1426 {
1427         switch (IFM_MODE(ime->ifm_media)) {
1428         case IFM_IEEE80211_11A:
1429                 *mode = IEEE80211_MODE_11A;
1430                 break;
1431         case IFM_IEEE80211_11B:
1432                 *mode = IEEE80211_MODE_11B;
1433                 break;
1434         case IFM_IEEE80211_11G:
1435                 *mode = IEEE80211_MODE_11G;
1436                 break;
1437         case IFM_IEEE80211_FH:
1438                 *mode = IEEE80211_MODE_FH;
1439                 break;
1440         case IFM_IEEE80211_11NA:
1441                 *mode = IEEE80211_MODE_11NA;
1442                 break;
1443         case IFM_IEEE80211_11NG:
1444                 *mode = IEEE80211_MODE_11NG;
1445                 break;
1446         case IFM_AUTO:
1447                 *mode = IEEE80211_MODE_AUTO;
1448                 break;
1449         default:
1450                 return 0;
1451         }
1452         /*
1453          * Turbo mode is an ``option''.
1454          * XXX does not apply to AUTO
1455          */
1456         if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1457                 if (*mode == IEEE80211_MODE_11A) {
1458                         if (flags & IEEE80211_F_TURBOP)
1459                                 *mode = IEEE80211_MODE_TURBO_A;
1460                         else
1461                                 *mode = IEEE80211_MODE_STURBO_A;
1462                 } else if (*mode == IEEE80211_MODE_11G)
1463                         *mode = IEEE80211_MODE_TURBO_G;
1464                 else
1465                         return 0;
1466         }
1467         /* XXX HT40 +/- */
1468         return 1;
1469 }
1470
1471 /*
1472  * Handle a media change request on the underlying interface.
1473  */
1474 static int
1475 ieee80211com_media_change(struct ifnet *ifp)
1476 {
1477         return EINVAL;
1478 }
1479
1480 /*
1481  * Handle a media change request on the vap interface.
1482  */
1483 int
1484 ieee80211_media_change(struct ifnet *ifp)
1485 {
1486         struct ieee80211vap *vap = ifp->if_softc;
1487         struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1488         uint16_t newmode;
1489
1490         if (!media2mode(ime, vap->iv_flags, &newmode))
1491                 return EINVAL;
1492         if (vap->iv_des_mode != newmode) {
1493                 vap->iv_des_mode = newmode;
1494                 /* XXX kick state machine if up+running */
1495         }
1496         return 0;
1497 }
1498
1499 /*
1500  * Common code to calculate the media status word
1501  * from the operating mode and channel state.
1502  */
1503 static int
1504 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1505 {
1506         int status;
1507
1508         status = IFM_IEEE80211;
1509         switch (opmode) {
1510         case IEEE80211_M_STA:
1511                 break;
1512         case IEEE80211_M_IBSS:
1513                 status |= IFM_IEEE80211_ADHOC;
1514                 break;
1515         case IEEE80211_M_HOSTAP:
1516                 status |= IFM_IEEE80211_HOSTAP;
1517                 break;
1518         case IEEE80211_M_MONITOR:
1519                 status |= IFM_IEEE80211_MONITOR;
1520                 break;
1521         case IEEE80211_M_AHDEMO:
1522                 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1523                 break;
1524         case IEEE80211_M_WDS:
1525                 status |= IFM_IEEE80211_WDS;
1526                 break;
1527         case IEEE80211_M_MBSS:
1528                 status |= IFM_IEEE80211_MBSS;
1529                 break;
1530         }
1531         if (IEEE80211_IS_CHAN_HTA(chan)) {
1532                 status |= IFM_IEEE80211_11NA;
1533         } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1534                 status |= IFM_IEEE80211_11NG;
1535         } else if (IEEE80211_IS_CHAN_A(chan)) {
1536                 status |= IFM_IEEE80211_11A;
1537         } else if (IEEE80211_IS_CHAN_B(chan)) {
1538                 status |= IFM_IEEE80211_11B;
1539         } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1540                 status |= IFM_IEEE80211_11G;
1541         } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1542                 status |= IFM_IEEE80211_FH;
1543         }
1544         /* XXX else complain? */
1545
1546         if (IEEE80211_IS_CHAN_TURBO(chan))
1547                 status |= IFM_IEEE80211_TURBO;
1548 #if 0
1549         if (IEEE80211_IS_CHAN_HT20(chan))
1550                 status |= IFM_IEEE80211_HT20;
1551         if (IEEE80211_IS_CHAN_HT40(chan))
1552                 status |= IFM_IEEE80211_HT40;
1553 #endif
1554         return status;
1555 }
1556
1557 static void
1558 ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1559 {
1560         struct ieee80211com *ic = ifp->if_l2com;
1561         struct ieee80211vap *vap;
1562
1563         imr->ifm_status = IFM_AVALID;
1564         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1565                 if (vap->iv_ifp->if_flags & IFF_UP) {
1566                         imr->ifm_status |= IFM_ACTIVE;
1567                         break;
1568                 }
1569         imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1570         if (imr->ifm_status & IFM_ACTIVE)
1571                 imr->ifm_current = imr->ifm_active;
1572 }
1573
1574 void
1575 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1576 {
1577         struct ieee80211vap *vap = ifp->if_softc;
1578         struct ieee80211com *ic = vap->iv_ic;
1579         enum ieee80211_phymode mode;
1580
1581         imr->ifm_status = IFM_AVALID;
1582         /*
1583          * NB: use the current channel's mode to lock down a xmit
1584          * rate only when running; otherwise we may have a mismatch
1585          * in which case the rate will not be convertible.
1586          */
1587         if (vap->iv_state == IEEE80211_S_RUN ||
1588             vap->iv_state == IEEE80211_S_SLEEP) {
1589                 imr->ifm_status |= IFM_ACTIVE;
1590                 mode = ieee80211_chan2mode(ic->ic_curchan);
1591         } else
1592                 mode = IEEE80211_MODE_AUTO;
1593         imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1594         /*
1595          * Calculate a current rate if possible.
1596          */
1597         if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1598                 /*
1599                  * A fixed rate is set, report that.
1600                  */
1601                 imr->ifm_active |= ieee80211_rate2media(ic,
1602                         vap->iv_txparms[mode].ucastrate, mode);
1603         } else if (vap->iv_opmode == IEEE80211_M_STA) {
1604                 /*
1605                  * In station mode report the current transmit rate.
1606                  */
1607                 imr->ifm_active |= ieee80211_rate2media(ic,
1608                         vap->iv_bss->ni_txrate, mode);
1609         } else
1610                 imr->ifm_active |= IFM_AUTO;
1611         if (imr->ifm_status & IFM_ACTIVE)
1612                 imr->ifm_current = imr->ifm_active;
1613 }
1614
1615 /*
1616  * Set the current phy mode and recalculate the active channel
1617  * set based on the available channels for this mode.  Also
1618  * select a new default/current channel if the current one is
1619  * inappropriate for this mode.
1620  */
1621 int
1622 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1623 {
1624         /*
1625          * Adjust basic rates in 11b/11g supported rate set.
1626          * Note that if operating on a hal/quarter rate channel
1627          * this is a noop as those rates sets are different
1628          * and used instead.
1629          */
1630         if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1631                 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1632
1633         ic->ic_curmode = mode;
1634         ieee80211_reset_erp(ic);        /* reset ERP state */
1635
1636         return 0;
1637 }
1638
1639 /*
1640  * Return the phy mode for with the specified channel.
1641  */
1642 enum ieee80211_phymode
1643 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1644 {
1645
1646         if (IEEE80211_IS_CHAN_HTA(chan))
1647                 return IEEE80211_MODE_11NA;
1648         else if (IEEE80211_IS_CHAN_HTG(chan))
1649                 return IEEE80211_MODE_11NG;
1650         else if (IEEE80211_IS_CHAN_108G(chan))
1651                 return IEEE80211_MODE_TURBO_G;
1652         else if (IEEE80211_IS_CHAN_ST(chan))
1653                 return IEEE80211_MODE_STURBO_A;
1654         else if (IEEE80211_IS_CHAN_TURBO(chan))
1655                 return IEEE80211_MODE_TURBO_A;
1656         else if (IEEE80211_IS_CHAN_HALF(chan))
1657                 return IEEE80211_MODE_HALF;
1658         else if (IEEE80211_IS_CHAN_QUARTER(chan))
1659                 return IEEE80211_MODE_QUARTER;
1660         else if (IEEE80211_IS_CHAN_A(chan))
1661                 return IEEE80211_MODE_11A;
1662         else if (IEEE80211_IS_CHAN_ANYG(chan))
1663                 return IEEE80211_MODE_11G;
1664         else if (IEEE80211_IS_CHAN_B(chan))
1665                 return IEEE80211_MODE_11B;
1666         else if (IEEE80211_IS_CHAN_FHSS(chan))
1667                 return IEEE80211_MODE_FH;
1668
1669         /* NB: should not get here */
1670         kprintf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1671                 __func__, chan->ic_freq, chan->ic_flags);
1672         return IEEE80211_MODE_11B;
1673 }
1674
1675 struct ratemedia {
1676         u_int   match;  /* rate + mode */
1677         u_int   media;  /* if_media rate */
1678 };
1679
1680 static int
1681 findmedia(const struct ratemedia rates[], int n, u_int match)
1682 {
1683         int i;
1684
1685         for (i = 0; i < n; i++)
1686                 if (rates[i].match == match)
1687                         return rates[i].media;
1688         return IFM_AUTO;
1689 }
1690
1691 /*
1692  * Convert IEEE80211 rate value to ifmedia subtype.
1693  * Rate is either a legacy rate in units of 0.5Mbps
1694  * or an MCS index.
1695  */
1696 int
1697 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1698 {
1699         static const struct ratemedia rates[] = {
1700                 {   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1701                 {   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1702                 {   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1703                 {   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1704                 {  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1705                 {  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1706                 {  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1707                 {  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1708                 {  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1709                 {  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1710                 {  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1711                 {  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1712                 {  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1713                 {  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1714                 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1715                 {   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1716                 {   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1717                 {  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1718                 {  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1719                 {  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1720                 {  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1721                 {  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1722                 {  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1723                 {  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1724                 {  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1725                 {  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1726                 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1727                 {   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1728                 {   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1729                 {  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1730                 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1731         };
1732         static const struct ratemedia htrates[] = {
1733                 {   0, IFM_IEEE80211_MCS },
1734                 {   1, IFM_IEEE80211_MCS },
1735                 {   2, IFM_IEEE80211_MCS },
1736                 {   3, IFM_IEEE80211_MCS },
1737                 {   4, IFM_IEEE80211_MCS },
1738                 {   5, IFM_IEEE80211_MCS },
1739                 {   6, IFM_IEEE80211_MCS },
1740                 {   7, IFM_IEEE80211_MCS },
1741                 {   8, IFM_IEEE80211_MCS },
1742                 {   9, IFM_IEEE80211_MCS },
1743                 {  10, IFM_IEEE80211_MCS },
1744                 {  11, IFM_IEEE80211_MCS },
1745                 {  12, IFM_IEEE80211_MCS },
1746                 {  13, IFM_IEEE80211_MCS },
1747                 {  14, IFM_IEEE80211_MCS },
1748                 {  15, IFM_IEEE80211_MCS },
1749                 {  16, IFM_IEEE80211_MCS },
1750                 {  17, IFM_IEEE80211_MCS },
1751                 {  18, IFM_IEEE80211_MCS },
1752                 {  19, IFM_IEEE80211_MCS },
1753                 {  20, IFM_IEEE80211_MCS },
1754                 {  21, IFM_IEEE80211_MCS },
1755                 {  22, IFM_IEEE80211_MCS },
1756                 {  23, IFM_IEEE80211_MCS },
1757                 {  24, IFM_IEEE80211_MCS },
1758                 {  25, IFM_IEEE80211_MCS },
1759                 {  26, IFM_IEEE80211_MCS },
1760                 {  27, IFM_IEEE80211_MCS },
1761                 {  28, IFM_IEEE80211_MCS },
1762                 {  29, IFM_IEEE80211_MCS },
1763                 {  30, IFM_IEEE80211_MCS },
1764                 {  31, IFM_IEEE80211_MCS },
1765                 {  32, IFM_IEEE80211_MCS },
1766                 {  33, IFM_IEEE80211_MCS },
1767                 {  34, IFM_IEEE80211_MCS },
1768                 {  35, IFM_IEEE80211_MCS },
1769                 {  36, IFM_IEEE80211_MCS },
1770                 {  37, IFM_IEEE80211_MCS },
1771                 {  38, IFM_IEEE80211_MCS },
1772                 {  39, IFM_IEEE80211_MCS },
1773                 {  40, IFM_IEEE80211_MCS },
1774                 {  41, IFM_IEEE80211_MCS },
1775                 {  42, IFM_IEEE80211_MCS },
1776                 {  43, IFM_IEEE80211_MCS },
1777                 {  44, IFM_IEEE80211_MCS },
1778                 {  45, IFM_IEEE80211_MCS },
1779                 {  46, IFM_IEEE80211_MCS },
1780                 {  47, IFM_IEEE80211_MCS },
1781                 {  48, IFM_IEEE80211_MCS },
1782                 {  49, IFM_IEEE80211_MCS },
1783                 {  50, IFM_IEEE80211_MCS },
1784                 {  51, IFM_IEEE80211_MCS },
1785                 {  52, IFM_IEEE80211_MCS },
1786                 {  53, IFM_IEEE80211_MCS },
1787                 {  54, IFM_IEEE80211_MCS },
1788                 {  55, IFM_IEEE80211_MCS },
1789                 {  56, IFM_IEEE80211_MCS },
1790                 {  57, IFM_IEEE80211_MCS },
1791                 {  58, IFM_IEEE80211_MCS },
1792                 {  59, IFM_IEEE80211_MCS },
1793                 {  60, IFM_IEEE80211_MCS },
1794                 {  61, IFM_IEEE80211_MCS },
1795                 {  62, IFM_IEEE80211_MCS },
1796                 {  63, IFM_IEEE80211_MCS },
1797                 {  64, IFM_IEEE80211_MCS },
1798                 {  65, IFM_IEEE80211_MCS },
1799                 {  66, IFM_IEEE80211_MCS },
1800                 {  67, IFM_IEEE80211_MCS },
1801                 {  68, IFM_IEEE80211_MCS },
1802                 {  69, IFM_IEEE80211_MCS },
1803                 {  70, IFM_IEEE80211_MCS },
1804                 {  71, IFM_IEEE80211_MCS },
1805                 {  72, IFM_IEEE80211_MCS },
1806                 {  73, IFM_IEEE80211_MCS },
1807                 {  74, IFM_IEEE80211_MCS },
1808                 {  75, IFM_IEEE80211_MCS },
1809                 {  76, IFM_IEEE80211_MCS },
1810         };
1811         int m;
1812
1813         /*
1814          * Check 11n rates first for match as an MCS.
1815          */
1816         if (mode == IEEE80211_MODE_11NA) {
1817                 if (rate & IEEE80211_RATE_MCS) {
1818                         rate &= ~IEEE80211_RATE_MCS;
1819                         m = findmedia(htrates, nitems(htrates), rate);
1820                         if (m != IFM_AUTO)
1821                                 return m | IFM_IEEE80211_11NA;
1822                 }
1823         } else if (mode == IEEE80211_MODE_11NG) {
1824                 /* NB: 12 is ambiguous, it will be treated as an MCS */
1825                 if (rate & IEEE80211_RATE_MCS) {
1826                         rate &= ~IEEE80211_RATE_MCS;
1827                         m = findmedia(htrates, nitems(htrates), rate);
1828                         if (m != IFM_AUTO)
1829                                 return m | IFM_IEEE80211_11NG;
1830                 }
1831         }
1832         rate &= IEEE80211_RATE_VAL;
1833         switch (mode) {
1834         case IEEE80211_MODE_11A:
1835         case IEEE80211_MODE_HALF:               /* XXX good 'nuf */
1836         case IEEE80211_MODE_QUARTER:
1837         case IEEE80211_MODE_11NA:
1838         case IEEE80211_MODE_TURBO_A:
1839         case IEEE80211_MODE_STURBO_A:
1840                 return findmedia(rates, nitems(rates),
1841                     rate | IFM_IEEE80211_11A);
1842         case IEEE80211_MODE_11B:
1843                 return findmedia(rates, nitems(rates),
1844                     rate | IFM_IEEE80211_11B);
1845         case IEEE80211_MODE_FH:
1846                 return findmedia(rates, nitems(rates),
1847                     rate | IFM_IEEE80211_FH);
1848         case IEEE80211_MODE_AUTO:
1849                 /* NB: ic may be NULL for some drivers */
1850                 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1851                         return findmedia(rates, nitems(rates),
1852                             rate | IFM_IEEE80211_FH);
1853                 /* NB: hack, 11g matches both 11b+11a rates */
1854                 /* fall thru... */
1855         case IEEE80211_MODE_11G:
1856         case IEEE80211_MODE_11NG:
1857         case IEEE80211_MODE_TURBO_G:
1858                 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1859         }
1860         return IFM_AUTO;
1861 }
1862
1863 int
1864 ieee80211_media2rate(int mword)
1865 {
1866         static const int ieeerates[] = {
1867                 -1,             /* IFM_AUTO */
1868                 0,              /* IFM_MANUAL */
1869                 0,              /* IFM_NONE */
1870                 2,              /* IFM_IEEE80211_FH1 */
1871                 4,              /* IFM_IEEE80211_FH2 */
1872                 2,              /* IFM_IEEE80211_DS1 */
1873                 4,              /* IFM_IEEE80211_DS2 */
1874                 11,             /* IFM_IEEE80211_DS5 */
1875                 22,             /* IFM_IEEE80211_DS11 */
1876                 44,             /* IFM_IEEE80211_DS22 */
1877                 12,             /* IFM_IEEE80211_OFDM6 */
1878                 18,             /* IFM_IEEE80211_OFDM9 */
1879                 24,             /* IFM_IEEE80211_OFDM12 */
1880                 36,             /* IFM_IEEE80211_OFDM18 */
1881                 48,             /* IFM_IEEE80211_OFDM24 */
1882                 72,             /* IFM_IEEE80211_OFDM36 */
1883                 96,             /* IFM_IEEE80211_OFDM48 */
1884                 108,            /* IFM_IEEE80211_OFDM54 */
1885                 144,            /* IFM_IEEE80211_OFDM72 */
1886                 0,              /* IFM_IEEE80211_DS354k */
1887                 0,              /* IFM_IEEE80211_DS512k */
1888                 6,              /* IFM_IEEE80211_OFDM3 */
1889                 9,              /* IFM_IEEE80211_OFDM4 */
1890                 54,             /* IFM_IEEE80211_OFDM27 */
1891                 -1,             /* IFM_IEEE80211_MCS */
1892         };
1893         return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
1894                 ieeerates[IFM_SUBTYPE(mword)] : 0;
1895 }
1896
1897 /*
1898  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1899  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1900  */
1901 #define mix(a, b, c)                                                    \
1902 do {                                                                    \
1903         a -= b; a -= c; a ^= (c >> 13);                                 \
1904         b -= c; b -= a; b ^= (a << 8);                                  \
1905         c -= a; c -= b; c ^= (b >> 13);                                 \
1906         a -= b; a -= c; a ^= (c >> 12);                                 \
1907         b -= c; b -= a; b ^= (a << 16);                                 \
1908         c -= a; c -= b; c ^= (b >> 5);                                  \
1909         a -= b; a -= c; a ^= (c >> 3);                                  \
1910         b -= c; b -= a; b ^= (a << 10);                                 \
1911         c -= a; c -= b; c ^= (b >> 15);                                 \
1912 } while (/*CONSTCOND*/0)
1913
1914 uint32_t
1915 ieee80211_mac_hash(const struct ieee80211com *ic,
1916         const uint8_t addr[IEEE80211_ADDR_LEN])
1917 {
1918         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1919
1920         b += addr[5] << 8;
1921         b += addr[4];
1922         a += addr[3] << 24;
1923         a += addr[2] << 16;
1924         a += addr[1] << 8;
1925         a += addr[0];
1926
1927         mix(a, b, c);
1928
1929         return c;
1930 }
1931 #undef mix
1932
1933 char
1934 ieee80211_channel_type_char(const struct ieee80211_channel *c)
1935 {
1936         if (IEEE80211_IS_CHAN_ST(c))
1937                 return 'S';
1938         if (IEEE80211_IS_CHAN_108A(c))
1939                 return 'T';
1940         if (IEEE80211_IS_CHAN_108G(c))
1941                 return 'G';
1942         if (IEEE80211_IS_CHAN_HT(c))
1943                 return 'n';
1944         if (IEEE80211_IS_CHAN_A(c))
1945                 return 'a';
1946         if (IEEE80211_IS_CHAN_ANYG(c))
1947                 return 'g';
1948         if (IEEE80211_IS_CHAN_B(c))
1949                 return 'b';
1950         return 'f';
1951 }