2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * IEEE 802.11 generic handler
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>
42 #include <machine/stdarg.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>
51 #include <net80211/ieee80211_var.h>
52 #include <net80211/ieee80211_regdomain.h>
53 #ifdef IEEE80211_SUPPORT_SUPERG
54 #include <net80211/ieee80211_superg.h>
56 #include <net80211/ieee80211_ratectl.h>
57 #include <net80211/ieee80211_vht.h>
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",
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,
90 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
91 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
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);
104 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
107 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
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 } };
123 static int set_vht_extchan(struct ieee80211_channel *c);
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.
131 ieee80211_chan_init(struct ieee80211com *ic)
133 #define DEFAULTRATES(m, def) do { \
134 if (ic->ic_sup_rates[m].rs_nrates == 0) \
135 ic->ic_sup_rates[m] = def; \
137 struct ieee80211_channel *c;
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"));
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.
155 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
158 * Setup the HT40/VHT40 upper/lower bits.
159 * The VHT80 math is done elsewhere.
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),
166 /* Update VHT math */
168 * XXX VHT again, note that this assumes VHT80 channels
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);
178 * Identify mode capabilities.
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);
207 /* initialize candidate channels to all available */
208 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
209 sizeof(ic->ic_chan_avail));
211 /* sort channel table to allow lookup optimizations */
212 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
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);
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);
237 * Setup required information to fill the mcsset field, if driver did
238 * not. Assume a 2T2R setup for historic reasons.
240 if (ic->ic_rxstream == 0)
242 if (ic->ic_txstream == 0)
245 ieee80211_init_suphtrates(ic);
248 * Set auto mode to reset active channel state and any desired channel.
250 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
255 null_update_mcast(struct ieee80211com *ic)
258 ic_printf(ic, "need multicast update callback\n");
262 null_update_promisc(struct ieee80211com *ic)
265 ic_printf(ic, "need promiscuous mode update callback\n");
269 null_update_chw(struct ieee80211com *ic)
272 ic_printf(ic, "%s: need callback\n", __func__);
276 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
281 retval = printf("%s: ", ic->ic_name);
283 retval += vprintf(fmt, ap);
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);
293 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
295 struct ieee80211com *ic;
300 error = sysctl_wire_old_buffer(req, 0);
303 sbuf_new_for_sysctl(&sb, NULL, 8, req);
304 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
306 mtx_lock(&ic_list_mtx);
307 LIST_FOREACH(ic, &ic_head, ic_next) {
308 sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
311 mtx_unlock(&ic_list_mtx);
312 error = sbuf_finish(&sb);
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");
322 * Attach/setup the common net80211 state. Called by
323 * the driver on attach to prior to creating any vap's.
326 ieee80211_ifattach(struct ieee80211com *ic)
329 IEEE80211_LOCK_INIT(ic, ic->ic_name);
330 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
331 TAILQ_INIT(&ic->ic_vaps);
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",
338 ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
339 ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
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.
345 ieee80211_chan_init(ic);
347 ic->ic_update_mcast = null_update_mcast;
348 ic->ic_update_promisc = null_update_promisc;
349 ic->ic_update_chw = null_update_chw;
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;
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);
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);
369 ieee80211_sysctl_attach(ic);
371 mtx_lock(&ic_list_mtx);
372 LIST_INSERT_HEAD(&ic_head, ic, ic_next);
373 mtx_unlock(&ic_list_mtx);
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.
383 ieee80211_ifdetach(struct ieee80211com *ic)
385 struct ieee80211vap *vap;
387 mtx_lock(&ic_list_mtx);
388 LIST_REMOVE(ic, ic_next);
389 mtx_unlock(&ic_list_mtx);
391 taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
394 * The VAP is responsible for setting and clearing
395 * the VIMAGE context.
397 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
398 ieee80211_vap_destroy(vap);
399 ieee80211_waitfor_parent(ic);
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);
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);
416 counter_u64_free(ic->ic_ierrors);
417 counter_u64_free(ic->ic_oerrors);
419 taskqueue_free(ic->ic_tq);
420 IEEE80211_TX_LOCK_DESTROY(ic);
421 IEEE80211_LOCK_DESTROY(ic);
424 struct ieee80211com *
425 ieee80211_find_com(const char *name)
427 struct ieee80211com *ic;
429 mtx_lock(&ic_list_mtx);
430 LIST_FOREACH(ic, &ic_head, ic_next)
431 if (strcmp(ic->ic_name, name) == 0)
433 mtx_unlock(&ic_list_mtx);
439 ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
441 struct ieee80211com *ic;
443 mtx_lock(&ic_list_mtx);
444 LIST_FOREACH(ic, &ic_head, ic_next)
446 mtx_unlock(&ic_list_mtx);
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.
459 default_reset(struct ieee80211vap *vap, u_long cmd)
465 * Default for updating the VAP default TX key index.
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
472 default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
475 /* XXX assert validity */
476 /* XXX assert we're in a key update block */
477 vap->iv_def_txkey = kid;
481 * Add underlying device errors to vap errors.
484 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
486 struct ieee80211vap *vap = ifp->if_softc;
487 struct ieee80211com *ic = vap->iv_ic;
490 rv = if_get_counter_default(ifp, cnt);
492 case IFCOUNTER_OERRORS:
493 rv += counter_u64_fetch(ic->ic_oerrors);
495 case IFCOUNTER_IERRORS:
496 rv += counter_u64_fetch(ic->ic_ierrors);
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).
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])
517 ifp = if_alloc(IFT_ETHER);
519 ic_printf(ic, "%s: unable to allocate ifnet\n",
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;
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;
539 /* 11n capabilities - XXX methodize */
540 vap->iv_htcaps = ic->ic_htcaps;
541 vap->iv_htextcaps = ic->ic_htextcaps;
543 /* 11ac capabilities - XXX methodize */
544 vap->iv_vhtcaps = ic->ic_vhtcaps;
545 vap->iv_vhtextcaps = ic->ic_vhtextcaps;
547 vap->iv_opmode = opmode;
548 vap->iv_caps |= ieee80211_opcap[opmode];
549 IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
551 case IEEE80211_M_WDS:
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.
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;
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));
572 * Propagate TDMA capability to mark vap; this
573 * cannot be removed and is used to distinguish
574 * regular ahdemo operation from ahdemo+tdma.
576 vap->iv_caps |= IEEE80211_C_TDMA;
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;
590 * Enable various functionality by default if we're
591 * capable; the driver can override us if it knows better.
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;
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;
611 * Install a default reset method for the ioctl support;
612 * the driver can override this.
614 vap->iv_reset = default_reset;
617 * Install a default crypto key update method, the driver
620 vap->iv_update_deftxkey = default_update_deftxkey;
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);
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);
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.
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])
649 struct ifnet *ifp = vap->iv_ifp;
650 struct ieee80211com *ic = vap->iv_ic;
651 struct ifmediareq imr;
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);
660 * Do late attach work that cannot happen until after
661 * the driver has had a chance to override defaults.
663 ieee80211_node_latevattach(vap);
664 ieee80211_power_latevattach(vap);
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));
673 ifp->if_baudrate = IF_Mbps(maxrate);
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 */
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);
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);
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);
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
710 ieee80211_vap_detach(struct ieee80211vap *vap)
712 struct ieee80211com *ic = vap->iv_ic;
713 struct ifnet *ifp = vap->iv_ifp;
715 CURVNET_SET(ifp->if_vnet);
717 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
718 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
720 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
726 * Flush any deferred vap tasks.
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);
733 /* XXX band-aid until ifnet handles this for us */
734 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
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);
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);
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);
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);
762 ifmedia_removeall(&vap->iv_media);
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);
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);
785 * Count number of vaps in promisc, and issue promisc on
786 * parent respectively.
789 ieee80211_promisc(struct ieee80211vap *vap, bool on)
791 struct ieee80211com *ic = vap->iv_ic;
793 IEEE80211_LOCK_ASSERT(ic);
796 if (++ic->ic_promisc == 1)
797 ieee80211_runtask(ic, &ic->ic_promisc_task);
799 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
801 if (--ic->ic_promisc == 0)
802 ieee80211_runtask(ic, &ic->ic_promisc_task);
807 * Count number of vaps in allmulti, and issue allmulti on
808 * parent respectively.
811 ieee80211_allmulti(struct ieee80211vap *vap, bool on)
813 struct ieee80211com *ic = vap->iv_ic;
815 IEEE80211_LOCK_ASSERT(ic);
818 if (++ic->ic_allmulti == 1)
819 ieee80211_runtask(ic, &ic->ic_mcast_task);
821 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
823 if (--ic->ic_allmulti == 0)
824 ieee80211_runtask(ic, &ic->ic_mcast_task);
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.
834 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
836 struct ieee80211vap *vap;
839 IEEE80211_LOCK_ASSERT(ic);
842 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
843 if (vap->iv_flags & flag) {
848 ic->ic_flags |= flag;
850 ic->ic_flags &= ~flag;
854 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
856 struct ieee80211com *ic = vap->iv_ic;
861 vap->iv_flags &= ~flag;
863 vap->iv_flags |= flag;
864 ieee80211_syncflag_locked(ic, flag);
865 IEEE80211_UNLOCK(ic);
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.
874 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
876 struct ieee80211vap *vap;
879 IEEE80211_LOCK_ASSERT(ic);
882 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
883 if (vap->iv_flags_ht & flag) {
888 ic->ic_flags_ht |= flag;
890 ic->ic_flags_ht &= ~flag;
894 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
896 struct ieee80211com *ic = vap->iv_ic;
901 vap->iv_flags_ht &= ~flag;
903 vap->iv_flags_ht |= flag;
904 ieee80211_syncflag_ht_locked(ic, flag);
905 IEEE80211_UNLOCK(ic);
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.
914 ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag)
916 struct ieee80211vap *vap;
919 IEEE80211_LOCK_ASSERT(ic);
922 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
923 if (vap->iv_flags_vht & flag) {
928 ic->ic_flags_vht |= flag;
930 ic->ic_flags_vht &= ~flag;
934 ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag)
936 struct ieee80211com *ic = vap->iv_ic;
941 vap->iv_flags_vht &= ~flag;
943 vap->iv_flags_vht |= flag;
944 ieee80211_syncflag_vht_locked(ic, flag);
945 IEEE80211_UNLOCK(ic);
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.
954 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
956 struct ieee80211vap *vap;
959 IEEE80211_LOCK_ASSERT(ic);
962 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
963 if (vap->iv_flags_ext & flag) {
968 ic->ic_flags_ext |= flag;
970 ic->ic_flags_ext &= ~flag;
974 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
976 struct ieee80211com *ic = vap->iv_ic;
981 vap->iv_flags_ext &= ~flag;
983 vap->iv_flags_ext |= flag;
984 ieee80211_syncflag_ext_locked(ic, flag);
985 IEEE80211_UNLOCK(ic);
989 mapgsm(u_int freq, u_int flags)
992 if (flags & IEEE80211_CHAN_QUARTER)
994 else if (flags & IEEE80211_CHAN_HALF)
998 /* NB: there is no 907/20 wide but leave room */
999 return (freq - 906*10) / 5;
1003 mappsb(u_int freq, u_int flags)
1005 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
1009 * Convert MHz frequency to IEEE channel number.
1012 ieee80211_mhz2ieee(u_int freq, u_int flags)
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 */
1021 return ((int) freq - 2407) / 5;
1023 return 15 + ((freq - 2512) / 20);
1024 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
1026 /* XXX check regdomain? */
1027 if (IS_FREQ_IN_PSB(freq))
1028 return mappsb(freq, flags);
1029 return (freq - 4000) / 5;
1031 return (freq - 5000) / 5;
1032 } else { /* either, guess */
1036 if (907 <= freq && freq <= 922)
1037 return mapgsm(freq, flags);
1038 return ((int) freq - 2407) / 5;
1041 if (IS_FREQ_IN_PSB(freq))
1042 return mappsb(freq, flags);
1043 else if (freq > 4900)
1044 return (freq - 4000) / 5;
1046 return 15 + ((freq - 2512) / 20);
1048 return (freq - 5000) / 5;
1050 #undef IS_FREQ_IN_PSB
1054 * Convert channel to IEEE channel number.
1057 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
1060 ic_printf(ic, "invalid channel (NULL)\n");
1063 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
1067 * Convert IEEE channel number to MHz frequency.
1070 ieee80211_ieee2mhz(u_int chan, u_int flags)
1072 if (flags & IEEE80211_CHAN_GSM)
1073 return 907 + 5 * (chan / 10);
1074 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
1078 return 2407 + chan*5;
1080 return 2512 + ((chan-15)*20);
1081 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
1082 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
1084 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
1086 return 5000 + (chan*5);
1087 } else { /* either, guess */
1088 /* XXX can't distinguish PSB+GSM channels */
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);
1099 static __inline void
1100 set_extchan(struct ieee80211_channel *c)
1104 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1105 * "the secondary channel number shall be 'N + [1,-1] * 4'
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;
1116 * Populate the freq1/freq2 fields as appropriate for VHT channels.
1118 * This for now uses a hard-coded list of 80MHz wide channels.
1120 * For HT20/HT40, freq1 just is the centre frequency of the 40MHz
1121 * wide channel we've already decided upon.
1123 * For VHT80 and VHT160, there are only a small number of fixed
1124 * 80/160MHz wide channels, so we just use those.
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
1132 struct vht_chan_range {
1133 uint16_t freq_start;
1137 struct vht_chan_range vht80_chan_ranges[] = {
1148 set_vht_extchan(struct ieee80211_channel *c)
1152 if (! IEEE80211_IS_CHAN_VHT(c)) {
1156 if (IEEE80211_IS_CHAN_VHT20(c)) {
1157 c->ic_vht_ch_freq1 = c->ic_ieee;
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;
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) {
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;
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);
1192 printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n",
1201 * Return whether the current channel could possibly be a part of
1204 * This doesn't check that the whole range is in the allowed list
1205 * according to regulatory.
1208 is_vht80_valid_freq(uint16_t freq)
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)
1220 addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1221 uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1223 struct ieee80211_channel *c;
1225 if (*nchans >= maxchans)
1229 printf("%s: %d: ieee=%d, freq=%d, flags=0x%08x\n",
1237 c = &chans[(*nchans)++];
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;
1252 copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1255 struct ieee80211_channel *c;
1257 KASSERT(*nchans > 0, ("channel list is empty\n"));
1259 if (*nchans >= maxchans)
1263 printf("%s: %d: flags=0x%08x\n",
1269 c = &chans[(*nchans)++];
1271 c->ic_flags = flags;
1272 c->ic_vht_ch_freq1 = 0;
1273 c->ic_vht_ch_freq2 = 0;
1284 getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
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;
1296 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1297 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1303 getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
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.
1312 * It also assumes HT40U is before HT40D.
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;
1328 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1330 if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1331 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U
1332 | IEEE80211_CHAN_VHT40U;
1335 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1337 if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1338 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D
1339 | IEEE80211_CHAN_VHT40D;
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;
1356 getflags(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
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))
1369 getflags_5ghz(bands, flags, ht40, vht80);
1371 getflags_2ghz(bands, flags, ht40);
1375 * Add one 20 MHz channel into specified channel list.
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[])
1383 uint32_t flags[IEEE80211_MODE_MAX];
1386 getflags(bands, flags, 0, 0);
1387 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
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);
1399 static struct ieee80211_channel *
1400 findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1403 struct ieee80211_channel *c;
1406 flags &= IEEE80211_CHAN_ALLTURBO;
1407 /* brute force search */
1408 for (i = 0; i < nchans; i++) {
1410 if (c->ic_freq == freq &&
1411 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1418 * Add 40 MHz channel pair into specified channel list.
1422 ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1423 int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1425 struct ieee80211_channel *cent, *extc;
1429 freq = ieee80211_ieee2mhz(ieee, flags);
1432 * Each entry defines an HT40 channel pair; find the
1433 * center channel, then the extension channel above.
1435 flags |= IEEE80211_CHAN_HT20;
1436 cent = findchannel(chans, *nchans, freq, flags);
1440 extc = findchannel(chans, *nchans, freq + 20, flags);
1444 flags &= ~IEEE80211_CHAN_HT;
1445 error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1446 maxregpower, flags | IEEE80211_CHAN_HT40U);
1450 error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1451 maxregpower, flags | IEEE80211_CHAN_HT40D);
1457 * Fetch the center frequency for the primary channel.
1460 ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
1463 return (c->ic_freq);
1467 * Fetch the center frequency for the primary BAND channel.
1469 * For 5, 10, 20MHz channels it'll be the normally configured channel
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).
1475 * For 80+80MHz channels this will be the centre of the primary
1476 * 80MHz channel; the secondary 80MHz channel will be center_freq2().
1479 ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
1483 * VHT - use the pre-calculated centre frequency
1484 * of the given channel.
1486 if (IEEE80211_IS_CHAN_VHT(c))
1487 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags));
1489 if (IEEE80211_IS_CHAN_HT40U(c)) {
1490 return (c->ic_freq + 10);
1492 if (IEEE80211_IS_CHAN_HT40D(c)) {
1493 return (c->ic_freq - 10);
1496 return (c->ic_freq);
1500 * For now, no 80+80 support; it will likely always return 0.
1503 ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
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));
1513 * Adds channels into specified channel list (ieee[] array must be sorted).
1514 * Channels are already sorted.
1517 add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1518 const uint8_t ieee[], int nieee, uint32_t flags[])
1524 for (i = 0; i < nieee; i++) {
1525 freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1526 for (j = 0; flags[j] != 0; j++) {
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.
1535 is_vht = !! (flags[j] & IEEE80211_CHAN_VHT);
1539 * XXX This is all very broken right now.
1540 * What we /should/ do is:
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
1547 if (is_vht && flags[j] & IEEE80211_CHAN_VHT80)
1548 if (! is_vht80_valid_freq(freq))
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..
1559 if (flags[j] & IEEE80211_CHAN_HT40D)
1561 * Can't have a "lower" channel if we are the
1564 * Can't have a "lower" channel if it's below/
1565 * within 20MHz of the first channel.
1567 * Can't have a "lower" channel if the channel
1568 * below it is not 20MHz away.
1570 if (i == 0 || ieee[i] < ieee[0] + 4 ||
1572 ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1574 if (flags[j] & IEEE80211_CHAN_HT40U)
1576 * Can't have an "upper" channel if we are
1579 * Can't have an "upper" channel be above the
1580 * last channel in the list.
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.)
1586 if (i == nieee - 1 ||
1587 ieee[i] + 4 > ieee[nieee - 1] ||
1589 ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1593 error = addchan(chans, maxchans, nchans,
1594 ieee[i], freq, 0, flags[j]);
1596 error = copychan_prev(chans, maxchans, nchans,
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[],
1612 uint32_t flags[IEEE80211_MODE_MAX];
1614 /* XXX no VHT for now */
1615 getflags_2ghz(bands, flags, ht40);
1616 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1618 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
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[],
1626 uint32_t flags[IEEE80211_MODE_MAX];
1630 * For now, assume VHT == VHT80 support as a minimum.
1632 if (isset(bands, IEEE80211_MODE_VHT_5GHZ))
1635 getflags_5ghz(bands, flags, ht40, vht80);
1636 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1638 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
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.
1646 struct ieee80211_channel *
1647 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1649 struct ieee80211_channel *c;
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)
1656 /* brute force search */
1657 return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
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.
1665 struct ieee80211_channel *
1666 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1668 struct ieee80211_channel *c;
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)
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)
1687 * Lookup a channel suitable for the given rx status.
1689 * This is used to find a channel for a frame (eg beacon, probe
1690 * response) based purely on the received PHY information.
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.
1699 * Takes the ic and rxstatus; returns the channel or NULL
1702 * XXX TODO: Add support for that when the need arises.
1704 struct ieee80211_channel *
1705 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1706 const struct ieee80211_rx_stats *rxs)
1708 struct ieee80211com *ic = vap->iv_ic;
1710 struct ieee80211_channel *c;
1716 * Strictly speaking we only use freq for now,
1717 * however later on we may wish to just store
1718 * the ieee for verification.
1720 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1722 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
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.
1733 /* Determine a band */
1734 /* XXX should be done by the driver? */
1735 if (rxs->c_freq < 3000) {
1736 flags = IEEE80211_CHAN_G;
1738 flags = IEEE80211_CHAN_A;
1741 /* Channel lookup */
1742 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1744 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1745 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1756 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
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,
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);
1798 * Setup the media data structures according to the channel and
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)
1806 int i, j, rate, maxrate, mword, r;
1807 enum ieee80211_phymode mode;
1808 const struct ieee80211_rateset *rs;
1809 struct ieee80211_rateset allrates;
1812 * Fill in media characteristics.
1814 ifmedia_init(media, 0, media_change, media_stat);
1817 * Add media for legacy operating modes.
1819 memset(&allrates, 0, sizeof(allrates));
1820 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1821 if (isclr(ic->ic_modecaps, mode))
1823 addmedia(media, caps, addsta, mode, IFM_AUTO);
1824 if (mode == IEEE80211_MODE_AUTO)
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);
1832 addmedia(media, caps, addsta, mode, mword);
1834 * Add legacy rate to the collection of all rates.
1836 r = rate & IEEE80211_RATE_VAL;
1837 for (j = 0; j < allrates.rs_nrates; j++)
1838 if (allrates.rs_rates[j] == r)
1840 if (j == allrates.rs_nrates) {
1841 /* unique, add to the set */
1842 allrates.rs_rates[j] = r;
1843 allrates.rs_nrates++;
1845 rate = (rate & IEEE80211_RATE_VAL) / 2;
1850 for (i = 0; i < allrates.rs_nrates; i++) {
1851 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1852 IEEE80211_MODE_AUTO);
1855 /* NB: remove media options from mword */
1856 addmedia(media, caps, addsta,
1857 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
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.
1865 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1866 if (isclr(ic->ic_modecaps, mode))
1868 addmedia(media, caps, addsta, mode, IFM_AUTO);
1869 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
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;
1884 rate = ieee80211_htrates[i].ht20_rate_800ns;
1892 for (; mode <= IEEE80211_MODE_VHT_5GHZ; mode++) {
1893 if (isclr(ic->ic_modecaps, mode))
1895 addmedia(media, caps, addsta, mode, IFM_AUTO);
1896 addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT);
1898 /* XXX TODO: VHT maxrate */
1904 /* XXX inline or eliminate? */
1905 const struct ieee80211_rateset *
1906 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1908 /* XXX does this work for 11ng basic rates? */
1909 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1912 /* XXX inline or eliminate? */
1913 const struct ieee80211_htrateset *
1914 ieee80211_get_suphtrates(struct ieee80211com *ic,
1915 const struct ieee80211_channel *c)
1917 return &ic->ic_sup_htrates;
1921 ieee80211_announce(struct ieee80211com *ic)
1924 enum ieee80211_phymode mode;
1925 const struct ieee80211_rateset *rs;
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))
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);
1937 rate = ieee80211_media2rate(mword);
1938 printf("%s%d%sMbps", (i != 0 ? " " : ""),
1939 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1943 ieee80211_ht_announce(ic);
1944 ieee80211_vht_announce(ic);
1948 ieee80211_announce_channels(struct ieee80211com *ic)
1950 const struct ieee80211_channel *c;
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))
1959 else if (IEEE80211_IS_CHAN_108A(c))
1961 else if (IEEE80211_IS_CHAN_108G(c))
1963 else if (IEEE80211_IS_CHAN_HT(c))
1965 else if (IEEE80211_IS_CHAN_A(c))
1967 else if (IEEE80211_IS_CHAN_ANYG(c))
1969 else if (IEEE80211_IS_CHAN_B(c))
1973 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1975 else if (IEEE80211_IS_CHAN_HALF(c))
1977 else if (IEEE80211_IS_CHAN_QUARTER(c))
1981 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1982 , c->ic_ieee, c->ic_freq, type
1984 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1985 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1987 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1988 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1994 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1996 switch (IFM_MODE(ime->ifm_media)) {
1997 case IFM_IEEE80211_11A:
1998 *mode = IEEE80211_MODE_11A;
2000 case IFM_IEEE80211_11B:
2001 *mode = IEEE80211_MODE_11B;
2003 case IFM_IEEE80211_11G:
2004 *mode = IEEE80211_MODE_11G;
2006 case IFM_IEEE80211_FH:
2007 *mode = IEEE80211_MODE_FH;
2009 case IFM_IEEE80211_11NA:
2010 *mode = IEEE80211_MODE_11NA;
2012 case IFM_IEEE80211_11NG:
2013 *mode = IEEE80211_MODE_11NG;
2016 *mode = IEEE80211_MODE_AUTO;
2022 * Turbo mode is an ``option''.
2023 * XXX does not apply to AUTO
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;
2030 *mode = IEEE80211_MODE_STURBO_A;
2031 } else if (*mode == IEEE80211_MODE_11G)
2032 *mode = IEEE80211_MODE_TURBO_G;
2041 * Handle a media change request on the vap interface.
2044 ieee80211_media_change(struct ifnet *ifp)
2046 struct ieee80211vap *vap = ifp->if_softc;
2047 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
2050 if (!media2mode(ime, vap->iv_flags, &newmode))
2052 if (vap->iv_des_mode != newmode) {
2053 vap->iv_des_mode = newmode;
2054 /* XXX kick state machine if up+running */
2060 * Common code to calculate the media status word
2061 * from the operating mode and channel state.
2064 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
2068 status = IFM_IEEE80211;
2070 case IEEE80211_M_STA:
2072 case IEEE80211_M_IBSS:
2073 status |= IFM_IEEE80211_ADHOC;
2075 case IEEE80211_M_HOSTAP:
2076 status |= IFM_IEEE80211_HOSTAP;
2078 case IEEE80211_M_MONITOR:
2079 status |= IFM_IEEE80211_MONITOR;
2081 case IEEE80211_M_AHDEMO:
2082 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
2084 case IEEE80211_M_WDS:
2085 status |= IFM_IEEE80211_WDS;
2087 case IEEE80211_M_MBSS:
2088 status |= IFM_IEEE80211_MBSS;
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;
2104 /* XXX else complain? */
2106 if (IEEE80211_IS_CHAN_TURBO(chan))
2107 status |= IFM_IEEE80211_TURBO;
2109 if (IEEE80211_IS_CHAN_HT20(chan))
2110 status |= IFM_IEEE80211_HT20;
2111 if (IEEE80211_IS_CHAN_HT40(chan))
2112 status |= IFM_IEEE80211_HT40;
2118 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2120 struct ieee80211vap *vap = ifp->if_softc;
2121 struct ieee80211com *ic = vap->iv_ic;
2122 enum ieee80211_phymode mode;
2124 imr->ifm_status = IFM_AVALID;
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.
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);
2135 mode = IEEE80211_MODE_AUTO;
2136 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
2138 * Calculate a current rate if possible.
2140 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
2142 * A fixed rate is set, report that.
2144 imr->ifm_active |= ieee80211_rate2media(ic,
2145 vap->iv_txparms[mode].ucastrate, mode);
2146 } else if (vap->iv_opmode == IEEE80211_M_STA) {
2148 * In station mode report the current transmit rate.
2150 imr->ifm_active |= ieee80211_rate2media(ic,
2151 vap->iv_bss->ni_txrate, mode);
2153 imr->ifm_active |= IFM_AUTO;
2154 if (imr->ifm_status & IFM_ACTIVE)
2155 imr->ifm_current = imr->ifm_active;
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.
2165 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
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
2173 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
2174 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
2176 ic->ic_curmode = mode;
2177 ieee80211_reset_erp(ic); /* reset ERP state */
2183 * Return the phy mode for with the specified channel.
2185 enum ieee80211_phymode
2186 ieee80211_chan2mode(const struct ieee80211_channel *chan)
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;
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;
2223 u_int match; /* rate + mode */
2224 u_int media; /* if_media rate */
2228 findmedia(const struct ratemedia rates[], int n, u_int match)
2232 for (i = 0; i < n; i++)
2233 if (rates[i].match == match)
2234 return rates[i].media;
2239 * Convert IEEE80211 rate value to ifmedia subtype.
2240 * Rate is either a legacy rate in units of 0.5Mbps
2244 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
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 */
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 },
2361 * Check 11n rates first for match as an MCS.
2363 if (mode == IEEE80211_MODE_11NA) {
2364 if (rate & IEEE80211_RATE_MCS) {
2365 rate &= ~IEEE80211_RATE_MCS;
2366 m = findmedia(htrates, nitems(htrates), rate);
2368 return m | IFM_IEEE80211_11NA;
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);
2376 return m | IFM_IEEE80211_11NG;
2379 rate &= IEEE80211_RATE_VAL;
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 */
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 */
2415 ieee80211_media2rate(int mword)
2417 static const int ieeerates[] = {
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 */
2445 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2446 ieeerates[IFM_SUBTYPE(mword)] : 0;
2450 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2451 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2453 #define mix(a, b, c) \
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)
2467 ieee80211_mac_hash(const struct ieee80211com *ic,
2468 const uint8_t addr[IEEE80211_ADDR_LEN])
2470 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2486 ieee80211_channel_type_char(const struct ieee80211_channel *c)
2488 if (IEEE80211_IS_CHAN_ST(c))
2490 if (IEEE80211_IS_CHAN_108A(c))
2492 if (IEEE80211_IS_CHAN_108G(c))
2494 if (IEEE80211_IS_CHAN_VHT(c))
2496 if (IEEE80211_IS_CHAN_HT(c))
2498 if (IEEE80211_IS_CHAN_A(c))
2500 if (IEEE80211_IS_CHAN_ANYG(c))
2502 if (IEEE80211_IS_CHAN_B(c))