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