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