]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211.c
Update to zstd 1.3.2
[FreeBSD/FreeBSD.git] / sys / net80211 / 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 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/sbuf.h>
41
42 #include <machine/stdarg.h>
43
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <net/if_media.h>
48 #include <net/if_types.h>
49 #include <net/ethernet.h>
50
51 #include <net80211/ieee80211_var.h>
52 #include <net80211/ieee80211_regdomain.h>
53 #ifdef IEEE80211_SUPPORT_SUPERG
54 #include <net80211/ieee80211_superg.h>
55 #endif
56 #include <net80211/ieee80211_ratectl.h>
57 #include <net80211/ieee80211_vht.h>
58
59 #include <net/bpf.h>
60
61 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
62         [IEEE80211_MODE_AUTO]     = "auto",
63         [IEEE80211_MODE_11A]      = "11a",
64         [IEEE80211_MODE_11B]      = "11b",
65         [IEEE80211_MODE_11G]      = "11g",
66         [IEEE80211_MODE_FH]       = "FH",
67         [IEEE80211_MODE_TURBO_A]  = "turboA",
68         [IEEE80211_MODE_TURBO_G]  = "turboG",
69         [IEEE80211_MODE_STURBO_A] = "sturboA",
70         [IEEE80211_MODE_HALF]     = "half",
71         [IEEE80211_MODE_QUARTER]  = "quarter",
72         [IEEE80211_MODE_11NA]     = "11na",
73         [IEEE80211_MODE_11NG]     = "11ng",
74         [IEEE80211_MODE_VHT_2GHZ]         = "11acg",
75         [IEEE80211_MODE_VHT_5GHZ]         = "11ac",
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  void ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag);
97 static  int ieee80211_media_setup(struct ieee80211com *ic,
98                 struct ifmedia *media, int caps, int addsta,
99                 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
100 static  int media_status(enum ieee80211_opmode,
101                 const struct ieee80211_channel *);
102 static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
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 static int set_vht_extchan(struct ieee80211_channel *c);
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 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
157                 /*
158                  * Setup the HT40/VHT40 upper/lower bits.
159                  * The VHT80 math is done elsewhere.
160                  */
161                 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
162                         c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
163                             (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
164                             c->ic_flags);
165
166                 /* Update VHT math */
167                 /*
168                  * XXX VHT again, note that this assumes VHT80 channels
169                  * are legit already
170                  */
171                 set_vht_extchan(c);
172
173                 /* default max tx power to max regulatory */
174                 if (c->ic_maxpower == 0)
175                         c->ic_maxpower = 2*c->ic_maxregpower;
176                 setbit(ic->ic_chan_avail, c->ic_ieee);
177                 /*
178                  * Identify mode capabilities.
179                  */
180                 if (IEEE80211_IS_CHAN_A(c))
181                         setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
182                 if (IEEE80211_IS_CHAN_B(c))
183                         setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
184                 if (IEEE80211_IS_CHAN_ANYG(c))
185                         setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
186                 if (IEEE80211_IS_CHAN_FHSS(c))
187                         setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
188                 if (IEEE80211_IS_CHAN_108A(c))
189                         setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
190                 if (IEEE80211_IS_CHAN_108G(c))
191                         setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
192                 if (IEEE80211_IS_CHAN_ST(c))
193                         setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
194                 if (IEEE80211_IS_CHAN_HALF(c))
195                         setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
196                 if (IEEE80211_IS_CHAN_QUARTER(c))
197                         setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
198                 if (IEEE80211_IS_CHAN_HTA(c))
199                         setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
200                 if (IEEE80211_IS_CHAN_HTG(c))
201                         setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
202                 if (IEEE80211_IS_CHAN_VHTA(c))
203                         setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ);
204                 if (IEEE80211_IS_CHAN_VHTG(c))
205                         setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_2GHZ);
206         }
207         /* initialize candidate channels to all available */
208         memcpy(ic->ic_chan_active, ic->ic_chan_avail,
209                 sizeof(ic->ic_chan_avail));
210
211         /* sort channel table to allow lookup optimizations */
212         ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
213
214         /* invalidate any previous state */
215         ic->ic_bsschan = IEEE80211_CHAN_ANYC;
216         ic->ic_prevchan = NULL;
217         ic->ic_csa_newchan = NULL;
218         /* arbitrarily pick the first channel */
219         ic->ic_curchan = &ic->ic_channels[0];
220         ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
221
222         /* fillin well-known rate sets if driver has not specified */
223         DEFAULTRATES(IEEE80211_MODE_11B,         ieee80211_rateset_11b);
224         DEFAULTRATES(IEEE80211_MODE_11G,         ieee80211_rateset_11g);
225         DEFAULTRATES(IEEE80211_MODE_11A,         ieee80211_rateset_11a);
226         DEFAULTRATES(IEEE80211_MODE_TURBO_A,     ieee80211_rateset_11a);
227         DEFAULTRATES(IEEE80211_MODE_TURBO_G,     ieee80211_rateset_11g);
228         DEFAULTRATES(IEEE80211_MODE_STURBO_A,    ieee80211_rateset_11a);
229         DEFAULTRATES(IEEE80211_MODE_HALF,        ieee80211_rateset_half);
230         DEFAULTRATES(IEEE80211_MODE_QUARTER,     ieee80211_rateset_quarter);
231         DEFAULTRATES(IEEE80211_MODE_11NA,        ieee80211_rateset_11a);
232         DEFAULTRATES(IEEE80211_MODE_11NG,        ieee80211_rateset_11g);
233         DEFAULTRATES(IEEE80211_MODE_VHT_2GHZ,    ieee80211_rateset_11g);
234         DEFAULTRATES(IEEE80211_MODE_VHT_5GHZ,    ieee80211_rateset_11a);
235
236         /*
237          * Setup required information to fill the mcsset field, if driver did
238          * not. Assume a 2T2R setup for historic reasons.
239          */
240         if (ic->ic_rxstream == 0)
241                 ic->ic_rxstream = 2;
242         if (ic->ic_txstream == 0)
243                 ic->ic_txstream = 2;
244
245         ieee80211_init_suphtrates(ic);
246
247         /*
248          * Set auto mode to reset active channel state and any desired channel.
249          */
250         (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
251 #undef DEFAULTRATES
252 }
253
254 static void
255 null_update_mcast(struct ieee80211com *ic)
256 {
257
258         ic_printf(ic, "need multicast update callback\n");
259 }
260
261 static void
262 null_update_promisc(struct ieee80211com *ic)
263 {
264
265         ic_printf(ic, "need promiscuous mode update callback\n");
266 }
267
268 static void
269 null_update_chw(struct ieee80211com *ic)
270 {
271
272         ic_printf(ic, "%s: need callback\n", __func__);
273 }
274
275 int
276 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
277
278         va_list ap;
279         int retval;
280
281         retval = printf("%s: ", ic->ic_name);
282         va_start(ap, fmt);
283         retval += vprintf(fmt, ap);
284         va_end(ap);  
285         return (retval);
286 }
287
288 static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
289 static struct mtx ic_list_mtx;
290 MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
291
292 static int
293 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
294 {
295         struct ieee80211com *ic;
296         struct sbuf sb;
297         char *sp;
298         int error;
299
300         error = sysctl_wire_old_buffer(req, 0);
301         if (error)
302                 return (error);
303         sbuf_new_for_sysctl(&sb, NULL, 8, req);
304         sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
305         sp = "";
306         mtx_lock(&ic_list_mtx);
307         LIST_FOREACH(ic, &ic_head, ic_next) {
308                 sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
309                 sp = " ";
310         }
311         mtx_unlock(&ic_list_mtx);
312         error = sbuf_finish(&sb);
313         sbuf_delete(&sb);
314         return (error);
315 }
316
317 SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
318     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
319     sysctl_ieee80211coms, "A", "names of available 802.11 devices");
320
321 /*
322  * Attach/setup the common net80211 state.  Called by
323  * the driver on attach to prior to creating any vap's.
324  */
325 void
326 ieee80211_ifattach(struct ieee80211com *ic)
327 {
328
329         IEEE80211_LOCK_INIT(ic, ic->ic_name);
330         IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
331         TAILQ_INIT(&ic->ic_vaps);
332
333         /* Create a taskqueue for all state changes */
334         ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
335             taskqueue_thread_enqueue, &ic->ic_tq);
336         taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
337             ic->ic_name);
338         ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
339         ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
340         /*
341          * Fill in 802.11 available channel set, mark all
342          * available channels as active, and pick a default
343          * channel if not already specified.
344          */
345         ieee80211_chan_init(ic);
346
347         ic->ic_update_mcast = null_update_mcast;
348         ic->ic_update_promisc = null_update_promisc;
349         ic->ic_update_chw = null_update_chw;
350
351         ic->ic_hash_key = arc4random();
352         ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
353         ic->ic_lintval = ic->ic_bintval;
354         ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
355
356         ieee80211_crypto_attach(ic);
357         ieee80211_node_attach(ic);
358         ieee80211_power_attach(ic);
359         ieee80211_proto_attach(ic);
360 #ifdef IEEE80211_SUPPORT_SUPERG
361         ieee80211_superg_attach(ic);
362 #endif
363         ieee80211_ht_attach(ic);
364         ieee80211_vht_attach(ic);
365         ieee80211_scan_attach(ic);
366         ieee80211_regdomain_attach(ic);
367         ieee80211_dfs_attach(ic);
368
369         ieee80211_sysctl_attach(ic);
370
371         mtx_lock(&ic_list_mtx);
372         LIST_INSERT_HEAD(&ic_head, ic, ic_next);
373         mtx_unlock(&ic_list_mtx);
374 }
375
376 /*
377  * Detach net80211 state on device detach.  Tear down
378  * all vap's and reclaim all common state prior to the
379  * device state going away.  Note we may call back into
380  * driver; it must be prepared for this.
381  */
382 void
383 ieee80211_ifdetach(struct ieee80211com *ic)
384 {
385         struct ieee80211vap *vap;
386
387         mtx_lock(&ic_list_mtx);
388         LIST_REMOVE(ic, ic_next);
389         mtx_unlock(&ic_list_mtx);
390
391         taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
392
393         /*
394          * The VAP is responsible for setting and clearing
395          * the VIMAGE context.
396          */
397         while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
398                 ieee80211_vap_destroy(vap);
399         ieee80211_waitfor_parent(ic);
400
401         ieee80211_sysctl_detach(ic);
402         ieee80211_dfs_detach(ic);
403         ieee80211_regdomain_detach(ic);
404         ieee80211_scan_detach(ic);
405 #ifdef IEEE80211_SUPPORT_SUPERG
406         ieee80211_superg_detach(ic);
407 #endif
408         ieee80211_vht_detach(ic);
409         ieee80211_ht_detach(ic);
410         /* NB: must be called before ieee80211_node_detach */
411         ieee80211_proto_detach(ic);
412         ieee80211_crypto_detach(ic);
413         ieee80211_power_detach(ic);
414         ieee80211_node_detach(ic);
415
416         counter_u64_free(ic->ic_ierrors);
417         counter_u64_free(ic->ic_oerrors);
418
419         taskqueue_free(ic->ic_tq);
420         IEEE80211_TX_LOCK_DESTROY(ic);
421         IEEE80211_LOCK_DESTROY(ic);
422 }
423
424 struct ieee80211com *
425 ieee80211_find_com(const char *name)
426 {
427         struct ieee80211com *ic;
428
429         mtx_lock(&ic_list_mtx);
430         LIST_FOREACH(ic, &ic_head, ic_next)
431                 if (strcmp(ic->ic_name, name) == 0)
432                         break;
433         mtx_unlock(&ic_list_mtx);
434
435         return (ic);
436 }
437
438 void
439 ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
440 {
441         struct ieee80211com *ic;
442
443         mtx_lock(&ic_list_mtx);
444         LIST_FOREACH(ic, &ic_head, ic_next)
445                 (*f)(arg, ic);
446         mtx_unlock(&ic_list_mtx);
447 }
448
449 /*
450  * Default reset method for use with the ioctl support.  This
451  * method is invoked after any state change in the 802.11
452  * layer that should be propagated to the hardware but not
453  * require re-initialization of the 802.11 state machine (e.g
454  * rescanning for an ap).  We always return ENETRESET which
455  * should cause the driver to re-initialize the device. Drivers
456  * can override this method to implement more optimized support.
457  */
458 static int
459 default_reset(struct ieee80211vap *vap, u_long cmd)
460 {
461         return ENETRESET;
462 }
463
464 /*
465  * Default for updating the VAP default TX key index.
466  *
467  * Drivers that support TX offload as well as hardware encryption offload
468  * may need to be informed of key index changes separate from the key
469  * update.
470  */
471 static void
472 default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
473 {
474
475         /* XXX assert validity */
476         /* XXX assert we're in a key update block */
477         vap->iv_def_txkey = kid;
478 }
479
480 /*
481  * Add underlying device errors to vap errors.
482  */
483 static uint64_t
484 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
485 {
486         struct ieee80211vap *vap = ifp->if_softc;
487         struct ieee80211com *ic = vap->iv_ic;
488         uint64_t rv;
489
490         rv = if_get_counter_default(ifp, cnt);
491         switch (cnt) {
492         case IFCOUNTER_OERRORS:
493                 rv += counter_u64_fetch(ic->ic_oerrors);
494                 break;
495         case IFCOUNTER_IERRORS:
496                 rv += counter_u64_fetch(ic->ic_ierrors);
497                 break;
498         default:
499                 break;
500         }
501
502         return (rv);
503 }
504
505 /*
506  * Prepare a vap for use.  Drivers use this call to
507  * setup net80211 state in new vap's prior attaching
508  * them with ieee80211_vap_attach (below).
509  */
510 int
511 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
512     const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
513     int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
514 {
515         struct ifnet *ifp;
516
517         ifp = if_alloc(IFT_ETHER);
518         if (ifp == NULL) {
519                 ic_printf(ic, "%s: unable to allocate ifnet\n",
520                     __func__);
521                 return ENOMEM;
522         }
523         if_initname(ifp, name, unit);
524         ifp->if_softc = vap;                    /* back pointer */
525         ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
526         ifp->if_transmit = ieee80211_vap_transmit;
527         ifp->if_qflush = ieee80211_vap_qflush;
528         ifp->if_ioctl = ieee80211_ioctl;
529         ifp->if_init = ieee80211_init;
530         ifp->if_get_counter = ieee80211_get_counter;
531
532         vap->iv_ifp = ifp;
533         vap->iv_ic = ic;
534         vap->iv_flags = ic->ic_flags;           /* propagate common flags */
535         vap->iv_flags_ext = ic->ic_flags_ext;
536         vap->iv_flags_ven = ic->ic_flags_ven;
537         vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
538
539         /* 11n capabilities - XXX methodize */
540         vap->iv_htcaps = ic->ic_htcaps;
541         vap->iv_htextcaps = ic->ic_htextcaps;
542
543         /* 11ac capabilities - XXX methodize */
544         vap->iv_vhtcaps = ic->ic_vhtcaps;
545         vap->iv_vhtextcaps = ic->ic_vhtextcaps;
546
547         vap->iv_opmode = opmode;
548         vap->iv_caps |= ieee80211_opcap[opmode];
549         IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
550         switch (opmode) {
551         case IEEE80211_M_WDS:
552                 /*
553                  * WDS links must specify the bssid of the far end.
554                  * For legacy operation this is a static relationship.
555                  * For non-legacy operation the station must associate
556                  * and be authorized to pass traffic.  Plumbing the
557                  * vap to the proper node happens when the vap
558                  * transitions to RUN state.
559                  */
560                 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
561                 vap->iv_flags |= IEEE80211_F_DESBSSID;
562                 if (flags & IEEE80211_CLONE_WDSLEGACY)
563                         vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
564                 break;
565 #ifdef IEEE80211_SUPPORT_TDMA
566         case IEEE80211_M_AHDEMO:
567                 if (flags & IEEE80211_CLONE_TDMA) {
568                         /* NB: checked before clone operation allowed */
569                         KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
570                             ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
571                         /*
572                          * Propagate TDMA capability to mark vap; this
573                          * cannot be removed and is used to distinguish
574                          * regular ahdemo operation from ahdemo+tdma.
575                          */
576                         vap->iv_caps |= IEEE80211_C_TDMA;
577                 }
578                 break;
579 #endif
580         default:
581                 break;
582         }
583         /* auto-enable s/w beacon miss support */
584         if (flags & IEEE80211_CLONE_NOBEACONS)
585                 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
586         /* auto-generated or user supplied MAC address */
587         if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
588                 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
589         /*
590          * Enable various functionality by default if we're
591          * capable; the driver can override us if it knows better.
592          */
593         if (vap->iv_caps & IEEE80211_C_WME)
594                 vap->iv_flags |= IEEE80211_F_WME;
595         if (vap->iv_caps & IEEE80211_C_BURST)
596                 vap->iv_flags |= IEEE80211_F_BURST;
597         /* NB: bg scanning only makes sense for station mode right now */
598         if (vap->iv_opmode == IEEE80211_M_STA &&
599             (vap->iv_caps & IEEE80211_C_BGSCAN))
600                 vap->iv_flags |= IEEE80211_F_BGSCAN;
601         vap->iv_flags |= IEEE80211_F_DOTH;      /* XXX no cap, just ena */
602         /* NB: DFS support only makes sense for ap mode right now */
603         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
604             (vap->iv_caps & IEEE80211_C_DFS))
605                 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
606
607         vap->iv_des_chan = IEEE80211_CHAN_ANYC;         /* any channel is ok */
608         vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
609         vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
610         /*
611          * Install a default reset method for the ioctl support;
612          * the driver can override this.
613          */
614         vap->iv_reset = default_reset;
615
616         /*
617          * Install a default crypto key update method, the driver
618          * can override this.
619          */
620         vap->iv_update_deftxkey = default_update_deftxkey;
621
622         ieee80211_sysctl_vattach(vap);
623         ieee80211_crypto_vattach(vap);
624         ieee80211_node_vattach(vap);
625         ieee80211_power_vattach(vap);
626         ieee80211_proto_vattach(vap);
627 #ifdef IEEE80211_SUPPORT_SUPERG
628         ieee80211_superg_vattach(vap);
629 #endif
630         ieee80211_ht_vattach(vap);
631         ieee80211_vht_vattach(vap);
632         ieee80211_scan_vattach(vap);
633         ieee80211_regdomain_vattach(vap);
634         ieee80211_radiotap_vattach(vap);
635         ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
636
637         return 0;
638 }
639
640 /*
641  * Activate a vap.  State should have been prepared with a
642  * call to ieee80211_vap_setup and by the driver.  On return
643  * from this call the vap is ready for use.
644  */
645 int
646 ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
647     ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
648 {
649         struct ifnet *ifp = vap->iv_ifp;
650         struct ieee80211com *ic = vap->iv_ic;
651         struct ifmediareq imr;
652         int maxrate;
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         ether_ifattach(ifp, macaddr);
676         IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
677         /* hook output method setup by ether_ifattach */
678         vap->iv_output = ifp->if_output;
679         ifp->if_output = ieee80211_output;
680         /* NB: if_mtu set by ether_ifattach to ETHERMTU */
681
682         IEEE80211_LOCK(ic);
683         TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
684         ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
685 #ifdef IEEE80211_SUPPORT_SUPERG
686         ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
687 #endif
688         ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
689         ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
690         ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
691         ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
692
693         ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
694         ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
695         ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
696         ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
697         ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
698         IEEE80211_UNLOCK(ic);
699
700         return 1;
701 }
702
703 /* 
704  * Tear down vap state and reclaim the ifnet.
705  * The driver is assumed to have prepared for
706  * this; e.g. by turning off interrupts for the
707  * underlying device.
708  */
709 void
710 ieee80211_vap_detach(struct ieee80211vap *vap)
711 {
712         struct ieee80211com *ic = vap->iv_ic;
713         struct ifnet *ifp = vap->iv_ifp;
714
715         CURVNET_SET(ifp->if_vnet);
716
717         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
718             __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
719
720         /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
721         ether_ifdetach(ifp);
722
723         ieee80211_stop(vap);
724
725         /*
726          * Flush any deferred vap tasks.
727          */
728         ieee80211_draintask(ic, &vap->iv_nstate_task);
729         ieee80211_draintask(ic, &vap->iv_swbmiss_task);
730         ieee80211_draintask(ic, &vap->iv_wme_task);
731         ieee80211_draintask(ic, &ic->ic_parent_task);
732
733         /* XXX band-aid until ifnet handles this for us */
734         taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
735
736         IEEE80211_LOCK(ic);
737         KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
738         TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
739         ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
740 #ifdef IEEE80211_SUPPORT_SUPERG
741         ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
742 #endif
743         ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
744         ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
745         ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
746         ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
747
748         ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
749         ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
750         ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
751         ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
752         ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
753
754         /* NB: this handles the bpfdetach done below */
755         ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
756         if (vap->iv_ifflags & IFF_PROMISC)
757                 ieee80211_promisc(vap, false);
758         if (vap->iv_ifflags & IFF_ALLMULTI)
759                 ieee80211_allmulti(vap, false);
760         IEEE80211_UNLOCK(ic);
761
762         ifmedia_removeall(&vap->iv_media);
763
764         ieee80211_radiotap_vdetach(vap);
765         ieee80211_regdomain_vdetach(vap);
766         ieee80211_scan_vdetach(vap);
767 #ifdef IEEE80211_SUPPORT_SUPERG
768         ieee80211_superg_vdetach(vap);
769 #endif
770         ieee80211_vht_vdetach(vap);
771         ieee80211_ht_vdetach(vap);
772         /* NB: must be before ieee80211_node_vdetach */
773         ieee80211_proto_vdetach(vap);
774         ieee80211_crypto_vdetach(vap);
775         ieee80211_power_vdetach(vap);
776         ieee80211_node_vdetach(vap);
777         ieee80211_sysctl_vdetach(vap);
778
779         if_free(ifp);
780
781         CURVNET_RESTORE();
782 }
783
784 /*
785  * Count number of vaps in promisc, and issue promisc on
786  * parent respectively.
787  */
788 void
789 ieee80211_promisc(struct ieee80211vap *vap, bool on)
790 {
791         struct ieee80211com *ic = vap->iv_ic;
792
793         IEEE80211_LOCK_ASSERT(ic);
794
795         if (on) {
796                 if (++ic->ic_promisc == 1)
797                         ieee80211_runtask(ic, &ic->ic_promisc_task);
798         } else {
799                 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
800                     __func__, ic));
801                 if (--ic->ic_promisc == 0)
802                         ieee80211_runtask(ic, &ic->ic_promisc_task);
803         }
804 }
805
806 /*
807  * Count number of vaps in allmulti, and issue allmulti on
808  * parent respectively.
809  */
810 void
811 ieee80211_allmulti(struct ieee80211vap *vap, bool on)
812 {
813         struct ieee80211com *ic = vap->iv_ic;
814
815         IEEE80211_LOCK_ASSERT(ic);
816
817         if (on) {
818                 if (++ic->ic_allmulti == 1)
819                         ieee80211_runtask(ic, &ic->ic_mcast_task);
820         } else {
821                 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
822                     __func__, ic));
823                 if (--ic->ic_allmulti == 0)
824                         ieee80211_runtask(ic, &ic->ic_mcast_task);
825         }
826 }
827
828 /*
829  * Synchronize flag bit state in the com structure
830  * according to the state of all vap's.  This is used,
831  * for example, to handle state changes via ioctls.
832  */
833 static void
834 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
835 {
836         struct ieee80211vap *vap;
837         int bit;
838
839         IEEE80211_LOCK_ASSERT(ic);
840
841         bit = 0;
842         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
843                 if (vap->iv_flags & flag) {
844                         bit = 1;
845                         break;
846                 }
847         if (bit)
848                 ic->ic_flags |= flag;
849         else
850                 ic->ic_flags &= ~flag;
851 }
852
853 void
854 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
855 {
856         struct ieee80211com *ic = vap->iv_ic;
857
858         IEEE80211_LOCK(ic);
859         if (flag < 0) {
860                 flag = -flag;
861                 vap->iv_flags &= ~flag;
862         } else
863                 vap->iv_flags |= flag;
864         ieee80211_syncflag_locked(ic, flag);
865         IEEE80211_UNLOCK(ic);
866 }
867
868 /*
869  * Synchronize flags_ht bit state in the com structure
870  * according to the state of all vap's.  This is used,
871  * for example, to handle state changes via ioctls.
872  */
873 static void
874 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
875 {
876         struct ieee80211vap *vap;
877         int bit;
878
879         IEEE80211_LOCK_ASSERT(ic);
880
881         bit = 0;
882         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
883                 if (vap->iv_flags_ht & flag) {
884                         bit = 1;
885                         break;
886                 }
887         if (bit)
888                 ic->ic_flags_ht |= flag;
889         else
890                 ic->ic_flags_ht &= ~flag;
891 }
892
893 void
894 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
895 {
896         struct ieee80211com *ic = vap->iv_ic;
897
898         IEEE80211_LOCK(ic);
899         if (flag < 0) {
900                 flag = -flag;
901                 vap->iv_flags_ht &= ~flag;
902         } else
903                 vap->iv_flags_ht |= flag;
904         ieee80211_syncflag_ht_locked(ic, flag);
905         IEEE80211_UNLOCK(ic);
906 }
907
908 /*
909  * Synchronize flags_vht bit state in the com structure
910  * according to the state of all vap's.  This is used,
911  * for example, to handle state changes via ioctls.
912  */
913 static void
914 ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag)
915 {
916         struct ieee80211vap *vap;
917         int bit;
918
919         IEEE80211_LOCK_ASSERT(ic);
920
921         bit = 0;
922         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
923                 if (vap->iv_flags_vht & flag) {
924                         bit = 1;
925                         break;
926                 }
927         if (bit)
928                 ic->ic_flags_vht |= flag;
929         else
930                 ic->ic_flags_vht &= ~flag;
931 }
932
933 void
934 ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag)
935 {
936         struct ieee80211com *ic = vap->iv_ic;
937
938         IEEE80211_LOCK(ic);
939         if (flag < 0) {
940                 flag = -flag;
941                 vap->iv_flags_vht &= ~flag;
942         } else
943                 vap->iv_flags_vht |= flag;
944         ieee80211_syncflag_vht_locked(ic, flag);
945         IEEE80211_UNLOCK(ic);
946 }
947
948 /*
949  * Synchronize flags_ext bit state in the com structure
950  * according to the state of all vap's.  This is used,
951  * for example, to handle state changes via ioctls.
952  */
953 static void
954 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
955 {
956         struct ieee80211vap *vap;
957         int bit;
958
959         IEEE80211_LOCK_ASSERT(ic);
960
961         bit = 0;
962         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
963                 if (vap->iv_flags_ext & flag) {
964                         bit = 1;
965                         break;
966                 }
967         if (bit)
968                 ic->ic_flags_ext |= flag;
969         else
970                 ic->ic_flags_ext &= ~flag;
971 }
972
973 void
974 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
975 {
976         struct ieee80211com *ic = vap->iv_ic;
977
978         IEEE80211_LOCK(ic);
979         if (flag < 0) {
980                 flag = -flag;
981                 vap->iv_flags_ext &= ~flag;
982         } else
983                 vap->iv_flags_ext |= flag;
984         ieee80211_syncflag_ext_locked(ic, flag);
985         IEEE80211_UNLOCK(ic);
986 }
987
988 static __inline int
989 mapgsm(u_int freq, u_int flags)
990 {
991         freq *= 10;
992         if (flags & IEEE80211_CHAN_QUARTER)
993                 freq += 5;
994         else if (flags & IEEE80211_CHAN_HALF)
995                 freq += 10;
996         else
997                 freq += 20;
998         /* NB: there is no 907/20 wide but leave room */
999         return (freq - 906*10) / 5;
1000 }
1001
1002 static __inline int
1003 mappsb(u_int freq, u_int flags)
1004 {
1005         return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
1006 }
1007
1008 /*
1009  * Convert MHz frequency to IEEE channel number.
1010  */
1011 int
1012 ieee80211_mhz2ieee(u_int freq, u_int flags)
1013 {
1014 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
1015         if (flags & IEEE80211_CHAN_GSM)
1016                 return mapgsm(freq, flags);
1017         if (flags & IEEE80211_CHAN_2GHZ) {      /* 2GHz band */
1018                 if (freq == 2484)
1019                         return 14;
1020                 if (freq < 2484)
1021                         return ((int) freq - 2407) / 5;
1022                 else
1023                         return 15 + ((freq - 2512) / 20);
1024         } else if (flags & IEEE80211_CHAN_5GHZ) {       /* 5Ghz band */
1025                 if (freq <= 5000) {
1026                         /* XXX check regdomain? */
1027                         if (IS_FREQ_IN_PSB(freq))
1028                                 return mappsb(freq, flags);
1029                         return (freq - 4000) / 5;
1030                 } else
1031                         return (freq - 5000) / 5;
1032         } else {                                /* either, guess */
1033                 if (freq == 2484)
1034                         return 14;
1035                 if (freq < 2484) {
1036                         if (907 <= freq && freq <= 922)
1037                                 return mapgsm(freq, flags);
1038                         return ((int) freq - 2407) / 5;
1039                 }
1040                 if (freq < 5000) {
1041                         if (IS_FREQ_IN_PSB(freq))
1042                                 return mappsb(freq, flags);
1043                         else if (freq > 4900)
1044                                 return (freq - 4000) / 5;
1045                         else
1046                                 return 15 + ((freq - 2512) / 20);
1047                 }
1048                 return (freq - 5000) / 5;
1049         }
1050 #undef IS_FREQ_IN_PSB
1051 }
1052
1053 /*
1054  * Convert channel to IEEE channel number.
1055  */
1056 int
1057 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
1058 {
1059         if (c == NULL) {
1060                 ic_printf(ic, "invalid channel (NULL)\n");
1061                 return 0;               /* XXX */
1062         }
1063         return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
1064 }
1065
1066 /*
1067  * Convert IEEE channel number to MHz frequency.
1068  */
1069 u_int
1070 ieee80211_ieee2mhz(u_int chan, u_int flags)
1071 {
1072         if (flags & IEEE80211_CHAN_GSM)
1073                 return 907 + 5 * (chan / 10);
1074         if (flags & IEEE80211_CHAN_2GHZ) {      /* 2GHz band */
1075                 if (chan == 14)
1076                         return 2484;
1077                 if (chan < 14)
1078                         return 2407 + chan*5;
1079                 else
1080                         return 2512 + ((chan-15)*20);
1081         } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
1082                 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
1083                         chan -= 37;
1084                         return 4940 + chan*5 + (chan % 5 ? 2 : 0);
1085                 }
1086                 return 5000 + (chan*5);
1087         } else {                                /* either, guess */
1088                 /* XXX can't distinguish PSB+GSM channels */
1089                 if (chan == 14)
1090                         return 2484;
1091                 if (chan < 14)                  /* 0-13 */
1092                         return 2407 + chan*5;
1093                 if (chan < 27)                  /* 15-26 */
1094                         return 2512 + ((chan-15)*20);
1095                 return 5000 + (chan*5);
1096         }
1097 }
1098
1099 static __inline void
1100 set_extchan(struct ieee80211_channel *c)
1101 {
1102
1103         /*
1104          * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1105          * "the secondary channel number shall be 'N + [1,-1] * 4'
1106          */
1107         if (c->ic_flags & IEEE80211_CHAN_HT40U)
1108                 c->ic_extieee = c->ic_ieee + 4;
1109         else if (c->ic_flags & IEEE80211_CHAN_HT40D)
1110                 c->ic_extieee = c->ic_ieee - 4;
1111         else
1112                 c->ic_extieee = 0;
1113 }
1114
1115 /*
1116  * Populate the freq1/freq2 fields as appropriate for VHT channels.
1117  *
1118  * This for now uses a hard-coded list of 80MHz wide channels.
1119  *
1120  * For HT20/HT40, freq1 just is the centre frequency of the 40MHz
1121  * wide channel we've already decided upon.
1122  *
1123  * For VHT80 and VHT160, there are only a small number of fixed
1124  * 80/160MHz wide channels, so we just use those.
1125  *
1126  * This is all likely very very wrong - both the regulatory code
1127  * and this code needs to ensure that all four channels are
1128  * available and valid before the VHT80 (and eight for VHT160) channel
1129  * is created.
1130  */
1131
1132 struct vht_chan_range {
1133         uint16_t freq_start;
1134         uint16_t freq_end;
1135 };
1136
1137 struct vht_chan_range vht80_chan_ranges[] = {
1138         { 5170, 5250 },
1139         { 5250, 5330 },
1140         { 5490, 5570 },
1141         { 5570, 5650 },
1142         { 5650, 5730 },
1143         { 5735, 5815 },
1144         { 0, 0, }
1145 };
1146
1147 static int
1148 set_vht_extchan(struct ieee80211_channel *c)
1149 {
1150         int i;
1151
1152         if (! IEEE80211_IS_CHAN_VHT(c)) {
1153                 return (0);
1154         }
1155
1156         if (IEEE80211_IS_CHAN_VHT20(c)) {
1157                 c->ic_vht_ch_freq1 = c->ic_ieee;
1158                 return (1);
1159         }
1160
1161         if (IEEE80211_IS_CHAN_VHT40(c)) {
1162                 if (IEEE80211_IS_CHAN_HT40U(c))
1163                         c->ic_vht_ch_freq1 = c->ic_ieee + 2;
1164                 else if (IEEE80211_IS_CHAN_HT40D(c))
1165                         c->ic_vht_ch_freq1 = c->ic_ieee - 2;
1166                 else
1167                         return (0);
1168                 return (1);
1169         }
1170
1171         if (IEEE80211_IS_CHAN_VHT80(c)) {
1172                 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1173                         if (c->ic_freq >= vht80_chan_ranges[i].freq_start &&
1174                             c->ic_freq < vht80_chan_ranges[i].freq_end) {
1175                                 int midpoint;
1176
1177                                 midpoint = vht80_chan_ranges[i].freq_start + 40;
1178                                 c->ic_vht_ch_freq1 =
1179                                     ieee80211_mhz2ieee(midpoint, c->ic_flags);
1180                                 c->ic_vht_ch_freq2 = 0;
1181 #if 0
1182                                 printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
1183                                     __func__, c->ic_ieee, c->ic_freq, midpoint,
1184                                     c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
1185 #endif
1186                                 return (1);
1187                         }
1188                 }
1189                 return (0);
1190         }
1191
1192         printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n",
1193             __func__,
1194             c->ic_ieee,
1195             c->ic_flags);
1196
1197         return (0);
1198 }
1199
1200 /*
1201  * Return whether the current channel could possibly be a part of
1202  * a VHT80 channel.
1203  *
1204  * This doesn't check that the whole range is in the allowed list
1205  * according to regulatory.
1206  */
1207 static int
1208 is_vht80_valid_freq(uint16_t freq)
1209 {
1210         int i;
1211         for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1212                 if (freq >= vht80_chan_ranges[i].freq_start &&
1213                     freq < vht80_chan_ranges[i].freq_end)
1214                         return (1);
1215         }
1216         return (0);
1217 }
1218
1219 static int
1220 addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1221     uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1222 {
1223         struct ieee80211_channel *c;
1224
1225         if (*nchans >= maxchans)
1226                 return (ENOBUFS);
1227
1228 #if 0
1229         printf("%s: %d: ieee=%d, freq=%d, flags=0x%08x\n",
1230             __func__,
1231             *nchans,
1232             ieee,
1233             freq,
1234             flags);
1235 #endif
1236
1237         c = &chans[(*nchans)++];
1238         c->ic_ieee = ieee;
1239         c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1240         c->ic_maxregpower = maxregpower;
1241         c->ic_maxpower = 2 * maxregpower;
1242         c->ic_flags = flags;
1243         c->ic_vht_ch_freq1 = 0;
1244         c->ic_vht_ch_freq2 = 0;
1245         set_extchan(c);
1246         set_vht_extchan(c);
1247
1248         return (0);
1249 }
1250
1251 static int
1252 copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1253     uint32_t flags)
1254 {
1255         struct ieee80211_channel *c;
1256
1257         KASSERT(*nchans > 0, ("channel list is empty\n"));
1258
1259         if (*nchans >= maxchans)
1260                 return (ENOBUFS);
1261
1262 #if 0
1263         printf("%s: %d: flags=0x%08x\n",
1264             __func__,
1265             *nchans,
1266             flags);
1267 #endif
1268
1269         c = &chans[(*nchans)++];
1270         c[0] = c[-1];
1271         c->ic_flags = flags;
1272         c->ic_vht_ch_freq1 = 0;
1273         c->ic_vht_ch_freq2 = 0;
1274         set_extchan(c);
1275         set_vht_extchan(c);
1276
1277         return (0);
1278 }
1279
1280 /*
1281  * XXX VHT-2GHz
1282  */
1283 static void
1284 getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1285 {
1286         int nmodes;
1287
1288         nmodes = 0;
1289         if (isset(bands, IEEE80211_MODE_11B))
1290                 flags[nmodes++] = IEEE80211_CHAN_B;
1291         if (isset(bands, IEEE80211_MODE_11G))
1292                 flags[nmodes++] = IEEE80211_CHAN_G;
1293         if (isset(bands, IEEE80211_MODE_11NG))
1294                 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1295         if (ht40) {
1296                 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1297                 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1298         }
1299         flags[nmodes] = 0;
1300 }
1301
1302 static void
1303 getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1304 {
1305         int nmodes;
1306
1307         /*
1308          * the addchan_list function seems to expect the flags array to
1309          * be in channel width order, so the VHT bits are interspersed
1310          * as appropriate to maintain said order.
1311          *
1312          * It also assumes HT40U is before HT40D.
1313          */
1314         nmodes = 0;
1315
1316         /* 20MHz */
1317         if (isset(bands, IEEE80211_MODE_11A))
1318                 flags[nmodes++] = IEEE80211_CHAN_A;
1319         if (isset(bands, IEEE80211_MODE_11NA))
1320                 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1321         if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1322                 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
1323                     IEEE80211_CHAN_VHT20;
1324         }
1325
1326         /* 40MHz */
1327         if (ht40) {
1328                 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1329         }
1330         if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1331                 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U
1332                     | IEEE80211_CHAN_VHT40U;
1333         }
1334         if (ht40) {
1335                 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1336         }
1337         if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1338                 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D
1339                     | IEEE80211_CHAN_VHT40D;
1340         }
1341
1342         /* 80MHz */
1343         if (vht80 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1344                 flags[nmodes++] = IEEE80211_CHAN_A |
1345                     IEEE80211_CHAN_HT40U | IEEE80211_CHAN_VHT80;
1346                 flags[nmodes++] = IEEE80211_CHAN_A |
1347                     IEEE80211_CHAN_HT40D | IEEE80211_CHAN_VHT80;
1348         }
1349
1350         /* XXX VHT80+80 */
1351         /* XXX VHT160 */
1352         flags[nmodes] = 0;
1353 }
1354
1355 static void
1356 getflags(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1357 {
1358
1359         flags[0] = 0;
1360         if (isset(bands, IEEE80211_MODE_11A) ||
1361             isset(bands, IEEE80211_MODE_11NA) ||
1362             isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1363                 if (isset(bands, IEEE80211_MODE_11B) ||
1364                     isset(bands, IEEE80211_MODE_11G) ||
1365                     isset(bands, IEEE80211_MODE_11NG) ||
1366                     isset(bands, IEEE80211_MODE_VHT_2GHZ))
1367                         return;
1368
1369                 getflags_5ghz(bands, flags, ht40, vht80);
1370         } else
1371                 getflags_2ghz(bands, flags, ht40);
1372 }
1373
1374 /*
1375  * Add one 20 MHz channel into specified channel list.
1376  */
1377 /* XXX VHT */
1378 int
1379 ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1380     int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1381     uint32_t chan_flags, const uint8_t bands[])
1382 {
1383         uint32_t flags[IEEE80211_MODE_MAX];
1384         int i, error;
1385
1386         getflags(bands, flags, 0, 0);
1387         KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1388
1389         error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1390             flags[0] | chan_flags);
1391         for (i = 1; flags[i] != 0 && error == 0; i++) {
1392                 error = copychan_prev(chans, maxchans, nchans,
1393                     flags[i] | chan_flags);
1394         }
1395
1396         return (error);
1397 }
1398
1399 static struct ieee80211_channel *
1400 findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1401     uint32_t flags)
1402 {
1403         struct ieee80211_channel *c;
1404         int i;
1405
1406         flags &= IEEE80211_CHAN_ALLTURBO;
1407         /* brute force search */
1408         for (i = 0; i < nchans; i++) {
1409                 c = &chans[i];
1410                 if (c->ic_freq == freq &&
1411                     (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1412                         return c;
1413         }
1414         return NULL;
1415 }
1416
1417 /*
1418  * Add 40 MHz channel pair into specified channel list.
1419  */
1420 /* XXX VHT */
1421 int
1422 ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1423     int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1424 {
1425         struct ieee80211_channel *cent, *extc;
1426         uint16_t freq;
1427         int error;
1428
1429         freq = ieee80211_ieee2mhz(ieee, flags);
1430
1431         /*
1432          * Each entry defines an HT40 channel pair; find the
1433          * center channel, then the extension channel above.
1434          */
1435         flags |= IEEE80211_CHAN_HT20;
1436         cent = findchannel(chans, *nchans, freq, flags);
1437         if (cent == NULL)
1438                 return (EINVAL);
1439
1440         extc = findchannel(chans, *nchans, freq + 20, flags);
1441         if (extc == NULL)
1442                 return (ENOENT);
1443
1444         flags &= ~IEEE80211_CHAN_HT;
1445         error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1446             maxregpower, flags | IEEE80211_CHAN_HT40U);
1447         if (error != 0)
1448                 return (error);
1449
1450         error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1451             maxregpower, flags | IEEE80211_CHAN_HT40D);
1452
1453         return (error);
1454 }
1455
1456 /*
1457  * Fetch the center frequency for the primary channel.
1458  */
1459 uint32_t
1460 ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
1461 {
1462
1463         return (c->ic_freq);
1464 }
1465
1466 /*
1467  * Fetch the center frequency for the primary BAND channel.
1468  *
1469  * For 5, 10, 20MHz channels it'll be the normally configured channel
1470  * frequency.
1471  *
1472  * For 40MHz, 80MHz, 160Mhz channels it'll the the centre of the
1473  * wide channel, not the centre of the primary channel (that's ic_freq).
1474  *
1475  * For 80+80MHz channels this will be the centre of the primary
1476  * 80MHz channel; the secondary 80MHz channel will be center_freq2().
1477  */
1478 uint32_t
1479 ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
1480 {
1481
1482         /*
1483          * VHT - use the pre-calculated centre frequency
1484          * of the given channel.
1485          */
1486         if (IEEE80211_IS_CHAN_VHT(c))
1487                 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags));
1488
1489         if (IEEE80211_IS_CHAN_HT40U(c)) {
1490                 return (c->ic_freq + 10);
1491         }
1492         if (IEEE80211_IS_CHAN_HT40D(c)) {
1493                 return (c->ic_freq - 10);
1494         }
1495
1496         return (c->ic_freq);
1497 }
1498
1499 /*
1500  * For now, no 80+80 support; it will likely always return 0.
1501  */
1502 uint32_t
1503 ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
1504 {
1505
1506         if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0))
1507                 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags));
1508
1509         return (0);
1510 }
1511
1512 /*
1513  * Adds channels into specified channel list (ieee[] array must be sorted).
1514  * Channels are already sorted.
1515  */
1516 static int
1517 add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1518     const uint8_t ieee[], int nieee, uint32_t flags[])
1519 {
1520         uint16_t freq;
1521         int i, j, error;
1522         int is_vht;
1523
1524         for (i = 0; i < nieee; i++) {
1525                 freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1526                 for (j = 0; flags[j] != 0; j++) {
1527                         /*
1528                          * Notes:
1529                          * + HT40 and VHT40 channels occur together, so
1530                          *   we need to be careful that we actually allow that.
1531                          * + VHT80, VHT160 will coexist with HT40/VHT40, so
1532                          *   make sure it's not skipped because of the overlap
1533                          *   check used for (V)HT40.
1534                          */
1535                         is_vht = !! (flags[j] & IEEE80211_CHAN_VHT);
1536
1537                         /*
1538                          * Test for VHT80.
1539                          * XXX This is all very broken right now.
1540                          * What we /should/ do is:
1541                          *
1542                          * + check that the frequency is in the list of
1543                          *   allowed VHT80 ranges; and
1544                          * + the other 3 channels in the list are actually
1545                          *   also available.
1546                          */
1547                         if (is_vht && flags[j] & IEEE80211_CHAN_VHT80)
1548                                 if (! is_vht80_valid_freq(freq))
1549                                         continue;
1550
1551                         /*
1552                          * Test for (V)HT40.
1553                          *
1554                          * This is also a fall through from VHT80; as we only
1555                          * allow a VHT80 channel if the VHT40 combination is
1556                          * also valid.  If the VHT40 form is not valid then
1557                          * we certainly can't do VHT80..
1558                          */
1559                         if (flags[j] & IEEE80211_CHAN_HT40D)
1560                                 /*
1561                                  * Can't have a "lower" channel if we are the
1562                                  * first channel.
1563                                  *
1564                                  * Can't have a "lower" channel if it's below/
1565                                  * within 20MHz of the first channel.
1566                                  *
1567                                  * Can't have a "lower" channel if the channel
1568                                  * below it is not 20MHz away.
1569                                  */
1570                                 if (i == 0 || ieee[i] < ieee[0] + 4 ||
1571                                     freq - 20 !=
1572                                     ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1573                                         continue;
1574                         if (flags[j] & IEEE80211_CHAN_HT40U)
1575                                 /*
1576                                  * Can't have an "upper" channel if we are
1577                                  * the last channel.
1578                                  *
1579                                  * Can't have an "upper" channel be above the
1580                                  * last channel in the list.
1581                                  *
1582                                  * Can't have an "upper" channel if the next
1583                                  * channel according to the math isn't 20MHz
1584                                  * away.  (Likely for channel 13/14.)
1585                                  */
1586                                 if (i == nieee - 1 ||
1587                                     ieee[i] + 4 > ieee[nieee - 1] ||
1588                                     freq + 20 !=
1589                                     ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1590                                         continue;
1591
1592                         if (j == 0) {
1593                                 error = addchan(chans, maxchans, nchans,
1594                                     ieee[i], freq, 0, flags[j]);
1595                         } else {
1596                                 error = copychan_prev(chans, maxchans, nchans,
1597                                     flags[j]);
1598                         }
1599                         if (error != 0)
1600                                 return (error);
1601                 }
1602         }
1603
1604         return (0);
1605 }
1606
1607 int
1608 ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1609     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1610     int ht40)
1611 {
1612         uint32_t flags[IEEE80211_MODE_MAX];
1613
1614         /* XXX no VHT for now */
1615         getflags_2ghz(bands, flags, ht40);
1616         KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1617
1618         return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1619 }
1620
1621 int
1622 ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1623     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1624     int ht40)
1625 {
1626         uint32_t flags[IEEE80211_MODE_MAX];
1627         int vht80 = 0;
1628
1629         /*
1630          * For now, assume VHT == VHT80 support as a minimum.
1631          */
1632         if (isset(bands, IEEE80211_MODE_VHT_5GHZ))
1633                 vht80 = 1;
1634
1635         getflags_5ghz(bands, flags, ht40, vht80);
1636         KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1637
1638         return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1639 }
1640
1641 /*
1642  * Locate a channel given a frequency+flags.  We cache
1643  * the previous lookup to optimize switching between two
1644  * channels--as happens with dynamic turbo.
1645  */
1646 struct ieee80211_channel *
1647 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1648 {
1649         struct ieee80211_channel *c;
1650
1651         flags &= IEEE80211_CHAN_ALLTURBO;
1652         c = ic->ic_prevchan;
1653         if (c != NULL && c->ic_freq == freq &&
1654             (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1655                 return c;
1656         /* brute force search */
1657         return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
1658 }
1659
1660 /*
1661  * Locate a channel given a channel number+flags.  We cache
1662  * the previous lookup to optimize switching between two
1663  * channels--as happens with dynamic turbo.
1664  */
1665 struct ieee80211_channel *
1666 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1667 {
1668         struct ieee80211_channel *c;
1669         int i;
1670
1671         flags &= IEEE80211_CHAN_ALLTURBO;
1672         c = ic->ic_prevchan;
1673         if (c != NULL && c->ic_ieee == ieee &&
1674             (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1675                 return c;
1676         /* brute force search */
1677         for (i = 0; i < ic->ic_nchans; i++) {
1678                 c = &ic->ic_channels[i];
1679                 if (c->ic_ieee == ieee &&
1680                     (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1681                         return c;
1682         }
1683         return NULL;
1684 }
1685
1686 /*
1687  * Lookup a channel suitable for the given rx status.
1688  *
1689  * This is used to find a channel for a frame (eg beacon, probe
1690  * response) based purely on the received PHY information.
1691  *
1692  * For now it tries to do it based on R_FREQ / R_IEEE.
1693  * This is enough for 11bg and 11a (and thus 11ng/11na)
1694  * but it will not be enough for GSM, PSB channels and the
1695  * like.  It also doesn't know about legacy-turbog and
1696  * legacy-turbo modes, which some offload NICs actually
1697  * support in weird ways.
1698  *
1699  * Takes the ic and rxstatus; returns the channel or NULL
1700  * if not found.
1701  *
1702  * XXX TODO: Add support for that when the need arises.
1703  */
1704 struct ieee80211_channel *
1705 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1706     const struct ieee80211_rx_stats *rxs)
1707 {
1708         struct ieee80211com *ic = vap->iv_ic;
1709         uint32_t flags;
1710         struct ieee80211_channel *c;
1711
1712         if (rxs == NULL)
1713                 return (NULL);
1714
1715         /*
1716          * Strictly speaking we only use freq for now,
1717          * however later on we may wish to just store
1718          * the ieee for verification.
1719          */
1720         if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1721                 return (NULL);
1722         if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1723                 return (NULL);
1724
1725         /*
1726          * If the rx status contains a valid ieee/freq, then
1727          * ensure we populate the correct channel information
1728          * in rxchan before passing it up to the scan infrastructure.
1729          * Offload NICs will pass up beacons from all channels
1730          * during background scans.
1731          */
1732
1733         /* Determine a band */
1734         /* XXX should be done by the driver? */
1735         if (rxs->c_freq < 3000) {
1736                 flags = IEEE80211_CHAN_G;
1737         } else {
1738                 flags = IEEE80211_CHAN_A;
1739         }
1740
1741         /* Channel lookup */
1742         c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1743
1744         IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1745             "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1746             __func__,
1747             (int) rxs->c_freq,
1748             (int) rxs->c_ieee,
1749             flags,
1750             c);
1751
1752         return (c);
1753 }
1754
1755 static void
1756 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1757 {
1758 #define ADD(_ic, _s, _o) \
1759         ifmedia_add(media, \
1760                 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1761         static const u_int mopts[IEEE80211_MODE_MAX] = { 
1762             [IEEE80211_MODE_AUTO]       = IFM_AUTO,
1763             [IEEE80211_MODE_11A]        = IFM_IEEE80211_11A,
1764             [IEEE80211_MODE_11B]        = IFM_IEEE80211_11B,
1765             [IEEE80211_MODE_11G]        = IFM_IEEE80211_11G,
1766             [IEEE80211_MODE_FH]         = IFM_IEEE80211_FH,
1767             [IEEE80211_MODE_TURBO_A]    = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1768             [IEEE80211_MODE_TURBO_G]    = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1769             [IEEE80211_MODE_STURBO_A]   = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1770             [IEEE80211_MODE_HALF]       = IFM_IEEE80211_11A,    /* XXX */
1771             [IEEE80211_MODE_QUARTER]    = IFM_IEEE80211_11A,    /* XXX */
1772             [IEEE80211_MODE_11NA]       = IFM_IEEE80211_11NA,
1773             [IEEE80211_MODE_11NG]       = IFM_IEEE80211_11NG,
1774             [IEEE80211_MODE_VHT_2GHZ]   = IFM_IEEE80211_VHT2G,
1775             [IEEE80211_MODE_VHT_5GHZ]   = IFM_IEEE80211_VHT5G,
1776         };
1777         u_int mopt;
1778
1779         mopt = mopts[mode];
1780         if (addsta)
1781                 ADD(ic, mword, mopt);   /* STA mode has no cap */
1782         if (caps & IEEE80211_C_IBSS)
1783                 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1784         if (caps & IEEE80211_C_HOSTAP)
1785                 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1786         if (caps & IEEE80211_C_AHDEMO)
1787                 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1788         if (caps & IEEE80211_C_MONITOR)
1789                 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1790         if (caps & IEEE80211_C_WDS)
1791                 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1792         if (caps & IEEE80211_C_MBSS)
1793                 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1794 #undef ADD
1795 }
1796
1797 /*
1798  * Setup the media data structures according to the channel and
1799  * rate tables.
1800  */
1801 static int
1802 ieee80211_media_setup(struct ieee80211com *ic,
1803         struct ifmedia *media, int caps, int addsta,
1804         ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1805 {
1806         int i, j, rate, maxrate, mword, r;
1807         enum ieee80211_phymode mode;
1808         const struct ieee80211_rateset *rs;
1809         struct ieee80211_rateset allrates;
1810
1811         /*
1812          * Fill in media characteristics.
1813          */
1814         ifmedia_init(media, 0, media_change, media_stat);
1815         maxrate = 0;
1816         /*
1817          * Add media for legacy operating modes.
1818          */
1819         memset(&allrates, 0, sizeof(allrates));
1820         for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1821                 if (isclr(ic->ic_modecaps, mode))
1822                         continue;
1823                 addmedia(media, caps, addsta, mode, IFM_AUTO);
1824                 if (mode == IEEE80211_MODE_AUTO)
1825                         continue;
1826                 rs = &ic->ic_sup_rates[mode];
1827                 for (i = 0; i < rs->rs_nrates; i++) {
1828                         rate = rs->rs_rates[i];
1829                         mword = ieee80211_rate2media(ic, rate, mode);
1830                         if (mword == 0)
1831                                 continue;
1832                         addmedia(media, caps, addsta, mode, mword);
1833                         /*
1834                          * Add legacy rate to the collection of all rates.
1835                          */
1836                         r = rate & IEEE80211_RATE_VAL;
1837                         for (j = 0; j < allrates.rs_nrates; j++)
1838                                 if (allrates.rs_rates[j] == r)
1839                                         break;
1840                         if (j == allrates.rs_nrates) {
1841                                 /* unique, add to the set */
1842                                 allrates.rs_rates[j] = r;
1843                                 allrates.rs_nrates++;
1844                         }
1845                         rate = (rate & IEEE80211_RATE_VAL) / 2;
1846                         if (rate > maxrate)
1847                                 maxrate = rate;
1848                 }
1849         }
1850         for (i = 0; i < allrates.rs_nrates; i++) {
1851                 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1852                                 IEEE80211_MODE_AUTO);
1853                 if (mword == 0)
1854                         continue;
1855                 /* NB: remove media options from mword */
1856                 addmedia(media, caps, addsta,
1857                     IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1858         }
1859         /*
1860          * Add HT/11n media.  Note that we do not have enough
1861          * bits in the media subtype to express the MCS so we
1862          * use a "placeholder" media subtype and any fixed MCS
1863          * must be specified with a different mechanism.
1864          */
1865         for (; mode <= IEEE80211_MODE_11NG; mode++) {
1866                 if (isclr(ic->ic_modecaps, mode))
1867                         continue;
1868                 addmedia(media, caps, addsta, mode, IFM_AUTO);
1869                 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1870         }
1871         if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1872             isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1873                 addmedia(media, caps, addsta,
1874                     IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1875                 i = ic->ic_txstream * 8 - 1;
1876                 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1877                     (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1878                         rate = ieee80211_htrates[i].ht40_rate_400ns;
1879                 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1880                         rate = ieee80211_htrates[i].ht40_rate_800ns;
1881                 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1882                         rate = ieee80211_htrates[i].ht20_rate_400ns;
1883                 else
1884                         rate = ieee80211_htrates[i].ht20_rate_800ns;
1885                 if (rate > maxrate)
1886                         maxrate = rate;
1887         }
1888
1889         /*
1890          * Add VHT media.
1891          */
1892         for (; mode <= IEEE80211_MODE_VHT_5GHZ; mode++) {
1893                 if (isclr(ic->ic_modecaps, mode))
1894                         continue;
1895                 addmedia(media, caps, addsta, mode, IFM_AUTO);
1896                 addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT);
1897
1898                 /* XXX TODO: VHT maxrate */
1899         }
1900
1901         return maxrate;
1902 }
1903
1904 /* XXX inline or eliminate? */
1905 const struct ieee80211_rateset *
1906 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1907 {
1908         /* XXX does this work for 11ng basic rates? */
1909         return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1910 }
1911
1912 /* XXX inline or eliminate? */
1913 const struct ieee80211_htrateset *
1914 ieee80211_get_suphtrates(struct ieee80211com *ic,
1915     const struct ieee80211_channel *c)
1916 {
1917         return &ic->ic_sup_htrates;
1918 }
1919
1920 void
1921 ieee80211_announce(struct ieee80211com *ic)
1922 {
1923         int i, rate, mword;
1924         enum ieee80211_phymode mode;
1925         const struct ieee80211_rateset *rs;
1926
1927         /* NB: skip AUTO since it has no rates */
1928         for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1929                 if (isclr(ic->ic_modecaps, mode))
1930                         continue;
1931                 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1932                 rs = &ic->ic_sup_rates[mode];
1933                 for (i = 0; i < rs->rs_nrates; i++) {
1934                         mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1935                         if (mword == 0)
1936                                 continue;
1937                         rate = ieee80211_media2rate(mword);
1938                         printf("%s%d%sMbps", (i != 0 ? " " : ""),
1939                             rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1940                 }
1941                 printf("\n");
1942         }
1943         ieee80211_ht_announce(ic);
1944         ieee80211_vht_announce(ic);
1945 }
1946
1947 void
1948 ieee80211_announce_channels(struct ieee80211com *ic)
1949 {
1950         const struct ieee80211_channel *c;
1951         char type;
1952         int i, cw;
1953
1954         printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1955         for (i = 0; i < ic->ic_nchans; i++) {
1956                 c = &ic->ic_channels[i];
1957                 if (IEEE80211_IS_CHAN_ST(c))
1958                         type = 'S';
1959                 else if (IEEE80211_IS_CHAN_108A(c))
1960                         type = 'T';
1961                 else if (IEEE80211_IS_CHAN_108G(c))
1962                         type = 'G';
1963                 else if (IEEE80211_IS_CHAN_HT(c))
1964                         type = 'n';
1965                 else if (IEEE80211_IS_CHAN_A(c))
1966                         type = 'a';
1967                 else if (IEEE80211_IS_CHAN_ANYG(c))
1968                         type = 'g';
1969                 else if (IEEE80211_IS_CHAN_B(c))
1970                         type = 'b';
1971                 else
1972                         type = 'f';
1973                 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1974                         cw = 40;
1975                 else if (IEEE80211_IS_CHAN_HALF(c))
1976                         cw = 10;
1977                 else if (IEEE80211_IS_CHAN_QUARTER(c))
1978                         cw = 5;
1979                 else
1980                         cw = 20;
1981                 printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1982                         , c->ic_ieee, c->ic_freq, type
1983                         , cw
1984                         , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1985                           IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1986                         , c->ic_maxregpower
1987                         , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1988                         , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1989                 );
1990         }
1991 }
1992
1993 static int
1994 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1995 {
1996         switch (IFM_MODE(ime->ifm_media)) {
1997         case IFM_IEEE80211_11A:
1998                 *mode = IEEE80211_MODE_11A;
1999                 break;
2000         case IFM_IEEE80211_11B:
2001                 *mode = IEEE80211_MODE_11B;
2002                 break;
2003         case IFM_IEEE80211_11G:
2004                 *mode = IEEE80211_MODE_11G;
2005                 break;
2006         case IFM_IEEE80211_FH:
2007                 *mode = IEEE80211_MODE_FH;
2008                 break;
2009         case IFM_IEEE80211_11NA:
2010                 *mode = IEEE80211_MODE_11NA;
2011                 break;
2012         case IFM_IEEE80211_11NG:
2013                 *mode = IEEE80211_MODE_11NG;
2014                 break;
2015         case IFM_AUTO:
2016                 *mode = IEEE80211_MODE_AUTO;
2017                 break;
2018         default:
2019                 return 0;
2020         }
2021         /*
2022          * Turbo mode is an ``option''.
2023          * XXX does not apply to AUTO
2024          */
2025         if (ime->ifm_media & IFM_IEEE80211_TURBO) {
2026                 if (*mode == IEEE80211_MODE_11A) {
2027                         if (flags & IEEE80211_F_TURBOP)
2028                                 *mode = IEEE80211_MODE_TURBO_A;
2029                         else
2030                                 *mode = IEEE80211_MODE_STURBO_A;
2031                 } else if (*mode == IEEE80211_MODE_11G)
2032                         *mode = IEEE80211_MODE_TURBO_G;
2033                 else
2034                         return 0;
2035         }
2036         /* XXX HT40 +/- */
2037         return 1;
2038 }
2039
2040 /*
2041  * Handle a media change request on the vap interface.
2042  */
2043 int
2044 ieee80211_media_change(struct ifnet *ifp)
2045 {
2046         struct ieee80211vap *vap = ifp->if_softc;
2047         struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
2048         uint16_t newmode;
2049
2050         if (!media2mode(ime, vap->iv_flags, &newmode))
2051                 return EINVAL;
2052         if (vap->iv_des_mode != newmode) {
2053                 vap->iv_des_mode = newmode;
2054                 /* XXX kick state machine if up+running */
2055         }
2056         return 0;
2057 }
2058
2059 /*
2060  * Common code to calculate the media status word
2061  * from the operating mode and channel state.
2062  */
2063 static int
2064 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
2065 {
2066         int status;
2067
2068         status = IFM_IEEE80211;
2069         switch (opmode) {
2070         case IEEE80211_M_STA:
2071                 break;
2072         case IEEE80211_M_IBSS:
2073                 status |= IFM_IEEE80211_ADHOC;
2074                 break;
2075         case IEEE80211_M_HOSTAP:
2076                 status |= IFM_IEEE80211_HOSTAP;
2077                 break;
2078         case IEEE80211_M_MONITOR:
2079                 status |= IFM_IEEE80211_MONITOR;
2080                 break;
2081         case IEEE80211_M_AHDEMO:
2082                 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
2083                 break;
2084         case IEEE80211_M_WDS:
2085                 status |= IFM_IEEE80211_WDS;
2086                 break;
2087         case IEEE80211_M_MBSS:
2088                 status |= IFM_IEEE80211_MBSS;
2089                 break;
2090         }
2091         if (IEEE80211_IS_CHAN_HTA(chan)) {
2092                 status |= IFM_IEEE80211_11NA;
2093         } else if (IEEE80211_IS_CHAN_HTG(chan)) {
2094                 status |= IFM_IEEE80211_11NG;
2095         } else if (IEEE80211_IS_CHAN_A(chan)) {
2096                 status |= IFM_IEEE80211_11A;
2097         } else if (IEEE80211_IS_CHAN_B(chan)) {
2098                 status |= IFM_IEEE80211_11B;
2099         } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
2100                 status |= IFM_IEEE80211_11G;
2101         } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
2102                 status |= IFM_IEEE80211_FH;
2103         }
2104         /* XXX else complain? */
2105
2106         if (IEEE80211_IS_CHAN_TURBO(chan))
2107                 status |= IFM_IEEE80211_TURBO;
2108 #if 0
2109         if (IEEE80211_IS_CHAN_HT20(chan))
2110                 status |= IFM_IEEE80211_HT20;
2111         if (IEEE80211_IS_CHAN_HT40(chan))
2112                 status |= IFM_IEEE80211_HT40;
2113 #endif
2114         return status;
2115 }
2116
2117 void
2118 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2119 {
2120         struct ieee80211vap *vap = ifp->if_softc;
2121         struct ieee80211com *ic = vap->iv_ic;
2122         enum ieee80211_phymode mode;
2123
2124         imr->ifm_status = IFM_AVALID;
2125         /*
2126          * NB: use the current channel's mode to lock down a xmit
2127          * rate only when running; otherwise we may have a mismatch
2128          * in which case the rate will not be convertible.
2129          */
2130         if (vap->iv_state == IEEE80211_S_RUN ||
2131             vap->iv_state == IEEE80211_S_SLEEP) {
2132                 imr->ifm_status |= IFM_ACTIVE;
2133                 mode = ieee80211_chan2mode(ic->ic_curchan);
2134         } else
2135                 mode = IEEE80211_MODE_AUTO;
2136         imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
2137         /*
2138          * Calculate a current rate if possible.
2139          */
2140         if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
2141                 /*
2142                  * A fixed rate is set, report that.
2143                  */
2144                 imr->ifm_active |= ieee80211_rate2media(ic,
2145                         vap->iv_txparms[mode].ucastrate, mode);
2146         } else if (vap->iv_opmode == IEEE80211_M_STA) {
2147                 /*
2148                  * In station mode report the current transmit rate.
2149                  */
2150                 imr->ifm_active |= ieee80211_rate2media(ic,
2151                         vap->iv_bss->ni_txrate, mode);
2152         } else
2153                 imr->ifm_active |= IFM_AUTO;
2154         if (imr->ifm_status & IFM_ACTIVE)
2155                 imr->ifm_current = imr->ifm_active;
2156 }
2157
2158 /*
2159  * Set the current phy mode and recalculate the active channel
2160  * set based on the available channels for this mode.  Also
2161  * select a new default/current channel if the current one is
2162  * inappropriate for this mode.
2163  */
2164 int
2165 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
2166 {
2167         /*
2168          * Adjust basic rates in 11b/11g supported rate set.
2169          * Note that if operating on a hal/quarter rate channel
2170          * this is a noop as those rates sets are different
2171          * and used instead.
2172          */
2173         if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
2174                 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
2175
2176         ic->ic_curmode = mode;
2177         ieee80211_reset_erp(ic);        /* reset ERP state */
2178
2179         return 0;
2180 }
2181
2182 /*
2183  * Return the phy mode for with the specified channel.
2184  */
2185 enum ieee80211_phymode
2186 ieee80211_chan2mode(const struct ieee80211_channel *chan)
2187 {
2188
2189         if (IEEE80211_IS_CHAN_VHT_2GHZ(chan))
2190                 return IEEE80211_MODE_VHT_2GHZ;
2191         else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan))
2192                 return IEEE80211_MODE_VHT_5GHZ;
2193         else if (IEEE80211_IS_CHAN_HTA(chan))
2194                 return IEEE80211_MODE_11NA;
2195         else if (IEEE80211_IS_CHAN_HTG(chan))
2196                 return IEEE80211_MODE_11NG;
2197         else if (IEEE80211_IS_CHAN_108G(chan))
2198                 return IEEE80211_MODE_TURBO_G;
2199         else if (IEEE80211_IS_CHAN_ST(chan))
2200                 return IEEE80211_MODE_STURBO_A;
2201         else if (IEEE80211_IS_CHAN_TURBO(chan))
2202                 return IEEE80211_MODE_TURBO_A;
2203         else if (IEEE80211_IS_CHAN_HALF(chan))
2204                 return IEEE80211_MODE_HALF;
2205         else if (IEEE80211_IS_CHAN_QUARTER(chan))
2206                 return IEEE80211_MODE_QUARTER;
2207         else if (IEEE80211_IS_CHAN_A(chan))
2208                 return IEEE80211_MODE_11A;
2209         else if (IEEE80211_IS_CHAN_ANYG(chan))
2210                 return IEEE80211_MODE_11G;
2211         else if (IEEE80211_IS_CHAN_B(chan))
2212                 return IEEE80211_MODE_11B;
2213         else if (IEEE80211_IS_CHAN_FHSS(chan))
2214                 return IEEE80211_MODE_FH;
2215
2216         /* NB: should not get here */
2217         printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
2218                 __func__, chan->ic_freq, chan->ic_flags);
2219         return IEEE80211_MODE_11B;
2220 }
2221
2222 struct ratemedia {
2223         u_int   match;  /* rate + mode */
2224         u_int   media;  /* if_media rate */
2225 };
2226
2227 static int
2228 findmedia(const struct ratemedia rates[], int n, u_int match)
2229 {
2230         int i;
2231
2232         for (i = 0; i < n; i++)
2233                 if (rates[i].match == match)
2234                         return rates[i].media;
2235         return IFM_AUTO;
2236 }
2237
2238 /*
2239  * Convert IEEE80211 rate value to ifmedia subtype.
2240  * Rate is either a legacy rate in units of 0.5Mbps
2241  * or an MCS index.
2242  */
2243 int
2244 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
2245 {
2246         static const struct ratemedia rates[] = {
2247                 {   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
2248                 {   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
2249                 {   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
2250                 {   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
2251                 {  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
2252                 {  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
2253                 {  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
2254                 {  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
2255                 {  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
2256                 {  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
2257                 {  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
2258                 {  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
2259                 {  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
2260                 {  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
2261                 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
2262                 {   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
2263                 {   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
2264                 {  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
2265                 {  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
2266                 {  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
2267                 {  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
2268                 {  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
2269                 {  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
2270                 {  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
2271                 {  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
2272                 {  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
2273                 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
2274                 {   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
2275                 {   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
2276                 {  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
2277                 /* NB: OFDM72 doesn't really exist so we don't handle it */
2278         };
2279         static const struct ratemedia htrates[] = {
2280                 {   0, IFM_IEEE80211_MCS },
2281                 {   1, IFM_IEEE80211_MCS },
2282                 {   2, IFM_IEEE80211_MCS },
2283                 {   3, IFM_IEEE80211_MCS },
2284                 {   4, IFM_IEEE80211_MCS },
2285                 {   5, IFM_IEEE80211_MCS },
2286                 {   6, IFM_IEEE80211_MCS },
2287                 {   7, IFM_IEEE80211_MCS },
2288                 {   8, IFM_IEEE80211_MCS },
2289                 {   9, IFM_IEEE80211_MCS },
2290                 {  10, IFM_IEEE80211_MCS },
2291                 {  11, IFM_IEEE80211_MCS },
2292                 {  12, IFM_IEEE80211_MCS },
2293                 {  13, IFM_IEEE80211_MCS },
2294                 {  14, IFM_IEEE80211_MCS },
2295                 {  15, IFM_IEEE80211_MCS },
2296                 {  16, IFM_IEEE80211_MCS },
2297                 {  17, IFM_IEEE80211_MCS },
2298                 {  18, IFM_IEEE80211_MCS },
2299                 {  19, IFM_IEEE80211_MCS },
2300                 {  20, IFM_IEEE80211_MCS },
2301                 {  21, IFM_IEEE80211_MCS },
2302                 {  22, IFM_IEEE80211_MCS },
2303                 {  23, IFM_IEEE80211_MCS },
2304                 {  24, IFM_IEEE80211_MCS },
2305                 {  25, IFM_IEEE80211_MCS },
2306                 {  26, IFM_IEEE80211_MCS },
2307                 {  27, IFM_IEEE80211_MCS },
2308                 {  28, IFM_IEEE80211_MCS },
2309                 {  29, IFM_IEEE80211_MCS },
2310                 {  30, IFM_IEEE80211_MCS },
2311                 {  31, IFM_IEEE80211_MCS },
2312                 {  32, IFM_IEEE80211_MCS },
2313                 {  33, IFM_IEEE80211_MCS },
2314                 {  34, IFM_IEEE80211_MCS },
2315                 {  35, IFM_IEEE80211_MCS },
2316                 {  36, IFM_IEEE80211_MCS },
2317                 {  37, IFM_IEEE80211_MCS },
2318                 {  38, IFM_IEEE80211_MCS },
2319                 {  39, IFM_IEEE80211_MCS },
2320                 {  40, IFM_IEEE80211_MCS },
2321                 {  41, IFM_IEEE80211_MCS },
2322                 {  42, IFM_IEEE80211_MCS },
2323                 {  43, IFM_IEEE80211_MCS },
2324                 {  44, IFM_IEEE80211_MCS },
2325                 {  45, IFM_IEEE80211_MCS },
2326                 {  46, IFM_IEEE80211_MCS },
2327                 {  47, IFM_IEEE80211_MCS },
2328                 {  48, IFM_IEEE80211_MCS },
2329                 {  49, IFM_IEEE80211_MCS },
2330                 {  50, IFM_IEEE80211_MCS },
2331                 {  51, IFM_IEEE80211_MCS },
2332                 {  52, IFM_IEEE80211_MCS },
2333                 {  53, IFM_IEEE80211_MCS },
2334                 {  54, IFM_IEEE80211_MCS },
2335                 {  55, IFM_IEEE80211_MCS },
2336                 {  56, IFM_IEEE80211_MCS },
2337                 {  57, IFM_IEEE80211_MCS },
2338                 {  58, IFM_IEEE80211_MCS },
2339                 {  59, IFM_IEEE80211_MCS },
2340                 {  60, IFM_IEEE80211_MCS },
2341                 {  61, IFM_IEEE80211_MCS },
2342                 {  62, IFM_IEEE80211_MCS },
2343                 {  63, IFM_IEEE80211_MCS },
2344                 {  64, IFM_IEEE80211_MCS },
2345                 {  65, IFM_IEEE80211_MCS },
2346                 {  66, IFM_IEEE80211_MCS },
2347                 {  67, IFM_IEEE80211_MCS },
2348                 {  68, IFM_IEEE80211_MCS },
2349                 {  69, IFM_IEEE80211_MCS },
2350                 {  70, IFM_IEEE80211_MCS },
2351                 {  71, IFM_IEEE80211_MCS },
2352                 {  72, IFM_IEEE80211_MCS },
2353                 {  73, IFM_IEEE80211_MCS },
2354                 {  74, IFM_IEEE80211_MCS },
2355                 {  75, IFM_IEEE80211_MCS },
2356                 {  76, IFM_IEEE80211_MCS },
2357         };
2358         int m;
2359
2360         /*
2361          * Check 11n rates first for match as an MCS.
2362          */
2363         if (mode == IEEE80211_MODE_11NA) {
2364                 if (rate & IEEE80211_RATE_MCS) {
2365                         rate &= ~IEEE80211_RATE_MCS;
2366                         m = findmedia(htrates, nitems(htrates), rate);
2367                         if (m != IFM_AUTO)
2368                                 return m | IFM_IEEE80211_11NA;
2369                 }
2370         } else if (mode == IEEE80211_MODE_11NG) {
2371                 /* NB: 12 is ambiguous, it will be treated as an MCS */
2372                 if (rate & IEEE80211_RATE_MCS) {
2373                         rate &= ~IEEE80211_RATE_MCS;
2374                         m = findmedia(htrates, nitems(htrates), rate);
2375                         if (m != IFM_AUTO)
2376                                 return m | IFM_IEEE80211_11NG;
2377                 }
2378         }
2379         rate &= IEEE80211_RATE_VAL;
2380         switch (mode) {
2381         case IEEE80211_MODE_11A:
2382         case IEEE80211_MODE_HALF:               /* XXX good 'nuf */
2383         case IEEE80211_MODE_QUARTER:
2384         case IEEE80211_MODE_11NA:
2385         case IEEE80211_MODE_TURBO_A:
2386         case IEEE80211_MODE_STURBO_A:
2387                 return findmedia(rates, nitems(rates), 
2388                     rate | IFM_IEEE80211_11A);
2389         case IEEE80211_MODE_11B:
2390                 return findmedia(rates, nitems(rates), 
2391                     rate | IFM_IEEE80211_11B);
2392         case IEEE80211_MODE_FH:
2393                 return findmedia(rates, nitems(rates), 
2394                     rate | IFM_IEEE80211_FH);
2395         case IEEE80211_MODE_AUTO:
2396                 /* NB: ic may be NULL for some drivers */
2397                 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
2398                         return findmedia(rates, nitems(rates),
2399                             rate | IFM_IEEE80211_FH);
2400                 /* NB: hack, 11g matches both 11b+11a rates */
2401                 /* fall thru... */
2402         case IEEE80211_MODE_11G:
2403         case IEEE80211_MODE_11NG:
2404         case IEEE80211_MODE_TURBO_G:
2405                 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
2406         case IEEE80211_MODE_VHT_2GHZ:
2407         case IEEE80211_MODE_VHT_5GHZ:
2408                 /* XXX TODO: need to figure out mapping for VHT rates */
2409                 return IFM_AUTO;
2410         }
2411         return IFM_AUTO;
2412 }
2413
2414 int
2415 ieee80211_media2rate(int mword)
2416 {
2417         static const int ieeerates[] = {
2418                 -1,             /* IFM_AUTO */
2419                 0,              /* IFM_MANUAL */
2420                 0,              /* IFM_NONE */
2421                 2,              /* IFM_IEEE80211_FH1 */
2422                 4,              /* IFM_IEEE80211_FH2 */
2423                 2,              /* IFM_IEEE80211_DS1 */
2424                 4,              /* IFM_IEEE80211_DS2 */
2425                 11,             /* IFM_IEEE80211_DS5 */
2426                 22,             /* IFM_IEEE80211_DS11 */
2427                 44,             /* IFM_IEEE80211_DS22 */
2428                 12,             /* IFM_IEEE80211_OFDM6 */
2429                 18,             /* IFM_IEEE80211_OFDM9 */
2430                 24,             /* IFM_IEEE80211_OFDM12 */
2431                 36,             /* IFM_IEEE80211_OFDM18 */
2432                 48,             /* IFM_IEEE80211_OFDM24 */
2433                 72,             /* IFM_IEEE80211_OFDM36 */
2434                 96,             /* IFM_IEEE80211_OFDM48 */
2435                 108,            /* IFM_IEEE80211_OFDM54 */
2436                 144,            /* IFM_IEEE80211_OFDM72 */
2437                 0,              /* IFM_IEEE80211_DS354k */
2438                 0,              /* IFM_IEEE80211_DS512k */
2439                 6,              /* IFM_IEEE80211_OFDM3 */
2440                 9,              /* IFM_IEEE80211_OFDM4 */
2441                 54,             /* IFM_IEEE80211_OFDM27 */
2442                 -1,             /* IFM_IEEE80211_MCS */
2443                 -1,             /* IFM_IEEE80211_VHT */
2444         };
2445         return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2446                 ieeerates[IFM_SUBTYPE(mword)] : 0;
2447 }
2448
2449 /*
2450  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2451  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2452  */
2453 #define mix(a, b, c)                                                    \
2454 do {                                                                    \
2455         a -= b; a -= c; a ^= (c >> 13);                                 \
2456         b -= c; b -= a; b ^= (a << 8);                                  \
2457         c -= a; c -= b; c ^= (b >> 13);                                 \
2458         a -= b; a -= c; a ^= (c >> 12);                                 \
2459         b -= c; b -= a; b ^= (a << 16);                                 \
2460         c -= a; c -= b; c ^= (b >> 5);                                  \
2461         a -= b; a -= c; a ^= (c >> 3);                                  \
2462         b -= c; b -= a; b ^= (a << 10);                                 \
2463         c -= a; c -= b; c ^= (b >> 15);                                 \
2464 } while (/*CONSTCOND*/0)
2465
2466 uint32_t
2467 ieee80211_mac_hash(const struct ieee80211com *ic,
2468         const uint8_t addr[IEEE80211_ADDR_LEN])
2469 {
2470         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2471
2472         b += addr[5] << 8;
2473         b += addr[4];
2474         a += addr[3] << 24;
2475         a += addr[2] << 16;
2476         a += addr[1] << 8;
2477         a += addr[0];
2478
2479         mix(a, b, c);
2480
2481         return c;
2482 }
2483 #undef mix
2484
2485 char
2486 ieee80211_channel_type_char(const struct ieee80211_channel *c)
2487 {
2488         if (IEEE80211_IS_CHAN_ST(c))
2489                 return 'S';
2490         if (IEEE80211_IS_CHAN_108A(c))
2491                 return 'T';
2492         if (IEEE80211_IS_CHAN_108G(c))
2493                 return 'G';
2494         if (IEEE80211_IS_CHAN_VHT(c))
2495                 return 'v';
2496         if (IEEE80211_IS_CHAN_HT(c))
2497                 return 'n';
2498         if (IEEE80211_IS_CHAN_A(c))
2499                 return 'a';
2500         if (IEEE80211_IS_CHAN_ANYG(c))
2501                 return 'g';
2502         if (IEEE80211_IS_CHAN_B(c))
2503                 return 'b';
2504         return 'f';
2505 }