2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2001 Atsushi Onoe
5 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
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_private.h>
49 #include <net/if_types.h>
50 #include <net/ethernet.h>
52 #include <net80211/ieee80211_var.h>
53 #include <net80211/ieee80211_regdomain.h>
54 #ifdef IEEE80211_SUPPORT_SUPERG
55 #include <net80211/ieee80211_superg.h>
57 #include <net80211/ieee80211_ratectl.h>
58 #include <net80211/ieee80211_vht.h>
62 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
63 [IEEE80211_MODE_AUTO] = "auto",
64 [IEEE80211_MODE_11A] = "11a",
65 [IEEE80211_MODE_11B] = "11b",
66 [IEEE80211_MODE_11G] = "11g",
67 [IEEE80211_MODE_FH] = "FH",
68 [IEEE80211_MODE_TURBO_A] = "turboA",
69 [IEEE80211_MODE_TURBO_G] = "turboG",
70 [IEEE80211_MODE_STURBO_A] = "sturboA",
71 [IEEE80211_MODE_HALF] = "half",
72 [IEEE80211_MODE_QUARTER] = "quarter",
73 [IEEE80211_MODE_11NA] = "11na",
74 [IEEE80211_MODE_11NG] = "11ng",
75 [IEEE80211_MODE_VHT_2GHZ] = "11acg",
76 [IEEE80211_MODE_VHT_5GHZ] = "11ac",
78 /* map ieee80211_opmode to the corresponding capability bit */
79 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
80 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
81 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
82 [IEEE80211_M_STA] = IEEE80211_C_STA,
83 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
84 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
85 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
86 #ifdef IEEE80211_SUPPORT_MESH
87 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
91 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
92 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
94 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
95 static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
96 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
97 static void ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag);
98 static int ieee80211_media_setup(struct ieee80211com *ic,
99 struct ifmedia *media, int caps, int addsta,
100 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
101 static int media_status(enum ieee80211_opmode,
102 const struct ieee80211_channel *);
103 static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
105 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
108 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
110 #define B(r) ((r) | IEEE80211_RATE_BASIC)
111 static const struct ieee80211_rateset ieee80211_rateset_11a =
112 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
113 static const struct ieee80211_rateset ieee80211_rateset_half =
114 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
115 static const struct ieee80211_rateset ieee80211_rateset_quarter =
116 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
117 static const struct ieee80211_rateset ieee80211_rateset_11b =
118 { 4, { B(2), B(4), B(11), B(22) } };
119 /* NB: OFDM rates are handled specially based on mode */
120 static const struct ieee80211_rateset ieee80211_rateset_11g =
121 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
124 static int set_vht_extchan(struct ieee80211_channel *c);
127 * Fill in 802.11 available channel set, mark
128 * all available channels as active, and pick
129 * a default channel if not already specified.
132 ieee80211_chan_init(struct ieee80211com *ic)
134 #define DEFAULTRATES(m, def) do { \
135 if (ic->ic_sup_rates[m].rs_nrates == 0) \
136 ic->ic_sup_rates[m] = def; \
138 struct ieee80211_channel *c;
141 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
142 ("invalid number of channels specified: %u", ic->ic_nchans));
143 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
144 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
145 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
146 for (i = 0; i < ic->ic_nchans; i++) {
147 c = &ic->ic_channels[i];
148 KASSERT(c->ic_flags != 0, ("channel with no flags"));
150 * Help drivers that work only with frequencies by filling
151 * in IEEE channel #'s if not already calculated. Note this
152 * mimics similar work done in ieee80211_setregdomain when
153 * changing regulatory state.
156 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
159 * Setup the HT40/VHT40 upper/lower bits.
160 * The VHT80/... math is done elsewhere.
162 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
163 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
164 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
167 /* Update VHT math */
169 * XXX VHT again, note that this assumes VHT80/... channels
174 /* default max tx power to max regulatory */
175 if (c->ic_maxpower == 0)
176 c->ic_maxpower = 2*c->ic_maxregpower;
177 setbit(ic->ic_chan_avail, c->ic_ieee);
179 * Identify mode capabilities.
181 if (IEEE80211_IS_CHAN_A(c))
182 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
183 if (IEEE80211_IS_CHAN_B(c))
184 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
185 if (IEEE80211_IS_CHAN_ANYG(c))
186 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
187 if (IEEE80211_IS_CHAN_FHSS(c))
188 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
189 if (IEEE80211_IS_CHAN_108A(c))
190 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
191 if (IEEE80211_IS_CHAN_108G(c))
192 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
193 if (IEEE80211_IS_CHAN_ST(c))
194 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
195 if (IEEE80211_IS_CHAN_HALF(c))
196 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
197 if (IEEE80211_IS_CHAN_QUARTER(c))
198 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
199 if (IEEE80211_IS_CHAN_HTA(c))
200 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
201 if (IEEE80211_IS_CHAN_HTG(c))
202 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
203 if (IEEE80211_IS_CHAN_VHTA(c))
204 setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ);
205 if (IEEE80211_IS_CHAN_VHTG(c))
206 setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_2GHZ);
208 /* initialize candidate channels to all available */
209 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
210 sizeof(ic->ic_chan_avail));
212 /* sort channel table to allow lookup optimizations */
213 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
215 /* invalidate any previous state */
216 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
217 ic->ic_prevchan = NULL;
218 ic->ic_csa_newchan = NULL;
219 /* arbitrarily pick the first channel */
220 ic->ic_curchan = &ic->ic_channels[0];
221 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
223 /* fillin well-known rate sets if driver has not specified */
224 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
225 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
226 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
227 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
228 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
229 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
230 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
231 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
232 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
233 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
234 DEFAULTRATES(IEEE80211_MODE_VHT_2GHZ, ieee80211_rateset_11g);
235 DEFAULTRATES(IEEE80211_MODE_VHT_5GHZ, ieee80211_rateset_11a);
238 * Setup required information to fill the mcsset field, if driver did
239 * not. Assume a 2T2R setup for historic reasons.
241 if (ic->ic_rxstream == 0)
243 if (ic->ic_txstream == 0)
246 ieee80211_init_suphtrates(ic);
249 * Set auto mode to reset active channel state and any desired channel.
251 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
256 null_update_mcast(struct ieee80211com *ic)
259 ic_printf(ic, "need multicast update callback\n");
263 null_update_promisc(struct ieee80211com *ic)
266 ic_printf(ic, "need promiscuous mode update callback\n");
270 null_update_chw(struct ieee80211com *ic)
273 ic_printf(ic, "%s: need callback\n", __func__);
277 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
282 retval = printf("%s: ", ic->ic_name);
284 retval += vprintf(fmt, ap);
289 static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
290 static struct mtx ic_list_mtx;
291 MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
294 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
296 struct ieee80211com *ic;
301 error = sysctl_wire_old_buffer(req, 0);
304 sbuf_new_for_sysctl(&sb, NULL, 8, req);
305 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
307 mtx_lock(&ic_list_mtx);
308 LIST_FOREACH(ic, &ic_head, ic_next) {
309 sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
312 mtx_unlock(&ic_list_mtx);
313 error = sbuf_finish(&sb);
318 SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
319 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
320 sysctl_ieee80211coms, "A", "names of available 802.11 devices");
323 * Attach/setup the common net80211 state. Called by
324 * the driver on attach to prior to creating any vap's.
327 ieee80211_ifattach(struct ieee80211com *ic)
330 IEEE80211_LOCK_INIT(ic, ic->ic_name);
331 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
332 TAILQ_INIT(&ic->ic_vaps);
334 /* Create a taskqueue for all state changes */
335 ic->ic_tq = taskqueue_create("ic_taskq",
336 IEEE80211_M_WAITOK | IEEE80211_M_ZERO,
337 taskqueue_thread_enqueue, &ic->ic_tq);
338 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
340 ic->ic_ierrors = counter_u64_alloc(IEEE80211_M_WAITOK);
341 ic->ic_oerrors = counter_u64_alloc(IEEE80211_M_WAITOK);
343 * Fill in 802.11 available channel set, mark all
344 * available channels as active, and pick a default
345 * channel if not already specified.
347 ieee80211_chan_init(ic);
349 ic->ic_update_mcast = null_update_mcast;
350 ic->ic_update_promisc = null_update_promisc;
351 ic->ic_update_chw = null_update_chw;
353 ic->ic_hash_key = arc4random();
354 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
355 ic->ic_lintval = ic->ic_bintval;
356 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
358 ieee80211_crypto_attach(ic);
359 ieee80211_node_attach(ic);
360 ieee80211_power_attach(ic);
361 ieee80211_proto_attach(ic);
362 #ifdef IEEE80211_SUPPORT_SUPERG
363 ieee80211_superg_attach(ic);
365 ieee80211_ht_attach(ic);
366 ieee80211_vht_attach(ic);
367 ieee80211_scan_attach(ic);
368 ieee80211_regdomain_attach(ic);
369 ieee80211_dfs_attach(ic);
371 ieee80211_sysctl_attach(ic);
373 mtx_lock(&ic_list_mtx);
374 LIST_INSERT_HEAD(&ic_head, ic, ic_next);
375 mtx_unlock(&ic_list_mtx);
379 * Detach net80211 state on device detach. Tear down
380 * all vap's and reclaim all common state prior to the
381 * device state going away. Note we may call back into
382 * driver; it must be prepared for this.
385 ieee80211_ifdetach(struct ieee80211com *ic)
387 struct ieee80211vap *vap;
390 * We use this as an indicator that ifattach never had a chance to be
391 * called, e.g. early driver attach failed and ifdetach was called
392 * during subsequent detach. Never fear, for we have nothing to do
395 if (ic->ic_tq == NULL)
398 mtx_lock(&ic_list_mtx);
399 LIST_REMOVE(ic, ic_next);
400 mtx_unlock(&ic_list_mtx);
402 taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
405 * The VAP is responsible for setting and clearing
406 * the VIMAGE context.
408 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL) {
409 ieee80211_com_vdetach(vap);
410 ieee80211_vap_destroy(vap);
412 ieee80211_waitfor_parent(ic);
414 ieee80211_sysctl_detach(ic);
415 ieee80211_dfs_detach(ic);
416 ieee80211_regdomain_detach(ic);
417 ieee80211_scan_detach(ic);
418 #ifdef IEEE80211_SUPPORT_SUPERG
419 ieee80211_superg_detach(ic);
421 ieee80211_vht_detach(ic);
422 ieee80211_ht_detach(ic);
423 /* NB: must be called before ieee80211_node_detach */
424 ieee80211_proto_detach(ic);
425 ieee80211_crypto_detach(ic);
426 ieee80211_power_detach(ic);
427 ieee80211_node_detach(ic);
429 counter_u64_free(ic->ic_ierrors);
430 counter_u64_free(ic->ic_oerrors);
432 taskqueue_free(ic->ic_tq);
433 IEEE80211_TX_LOCK_DESTROY(ic);
434 IEEE80211_LOCK_DESTROY(ic);
437 struct ieee80211com *
438 ieee80211_find_com(const char *name)
440 struct ieee80211com *ic;
442 mtx_lock(&ic_list_mtx);
443 LIST_FOREACH(ic, &ic_head, ic_next)
444 if (strcmp(ic->ic_name, name) == 0)
446 mtx_unlock(&ic_list_mtx);
452 ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
454 struct ieee80211com *ic;
456 mtx_lock(&ic_list_mtx);
457 LIST_FOREACH(ic, &ic_head, ic_next)
459 mtx_unlock(&ic_list_mtx);
463 * Default reset method for use with the ioctl support. This
464 * method is invoked after any state change in the 802.11
465 * layer that should be propagated to the hardware but not
466 * require re-initialization of the 802.11 state machine (e.g
467 * rescanning for an ap). We always return ENETRESET which
468 * should cause the driver to re-initialize the device. Drivers
469 * can override this method to implement more optimized support.
472 default_reset(struct ieee80211vap *vap, u_long cmd)
478 * Default for updating the VAP default TX key index.
480 * Drivers that support TX offload as well as hardware encryption offload
481 * may need to be informed of key index changes separate from the key
485 default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
488 /* XXX assert validity */
489 /* XXX assert we're in a key update block */
490 vap->iv_def_txkey = kid;
494 * Add underlying device errors to vap errors.
497 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
499 struct ieee80211vap *vap = ifp->if_softc;
500 struct ieee80211com *ic = vap->iv_ic;
503 rv = if_get_counter_default(ifp, cnt);
505 case IFCOUNTER_OERRORS:
506 rv += counter_u64_fetch(ic->ic_oerrors);
508 case IFCOUNTER_IERRORS:
509 rv += counter_u64_fetch(ic->ic_ierrors);
519 * Prepare a vap for use. Drivers use this call to
520 * setup net80211 state in new vap's prior attaching
521 * them with ieee80211_vap_attach (below).
524 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
525 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
526 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
530 ifp = if_alloc(IFT_ETHER);
532 ic_printf(ic, "%s: unable to allocate ifnet\n", __func__);
535 if_initname(ifp, name, unit);
536 ifp->if_softc = vap; /* back pointer */
537 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
538 ifp->if_transmit = ieee80211_vap_transmit;
539 ifp->if_qflush = ieee80211_vap_qflush;
540 ifp->if_ioctl = ieee80211_ioctl;
541 ifp->if_init = ieee80211_init;
542 ifp->if_get_counter = ieee80211_get_counter;
546 vap->iv_flags = ic->ic_flags; /* propagate common flags */
547 vap->iv_flags_ext = ic->ic_flags_ext;
548 vap->iv_flags_ven = ic->ic_flags_ven;
549 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
551 /* 11n capabilities - XXX methodize */
552 vap->iv_htcaps = ic->ic_htcaps;
553 vap->iv_htextcaps = ic->ic_htextcaps;
555 /* 11ac capabilities - XXX methodize */
556 vap->iv_vhtcaps = ic->ic_vhtcaps;
557 vap->iv_vhtextcaps = ic->ic_vhtextcaps;
559 vap->iv_opmode = opmode;
560 vap->iv_caps |= ieee80211_opcap[opmode];
561 IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
563 case IEEE80211_M_WDS:
565 * WDS links must specify the bssid of the far end.
566 * For legacy operation this is a static relationship.
567 * For non-legacy operation the station must associate
568 * and be authorized to pass traffic. Plumbing the
569 * vap to the proper node happens when the vap
570 * transitions to RUN state.
572 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
573 vap->iv_flags |= IEEE80211_F_DESBSSID;
574 if (flags & IEEE80211_CLONE_WDSLEGACY)
575 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
577 #ifdef IEEE80211_SUPPORT_TDMA
578 case IEEE80211_M_AHDEMO:
579 if (flags & IEEE80211_CLONE_TDMA) {
580 /* NB: checked before clone operation allowed */
581 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
582 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
584 * Propagate TDMA capability to mark vap; this
585 * cannot be removed and is used to distinguish
586 * regular ahdemo operation from ahdemo+tdma.
588 vap->iv_caps |= IEEE80211_C_TDMA;
595 /* auto-enable s/w beacon miss support */
596 if (flags & IEEE80211_CLONE_NOBEACONS)
597 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
598 /* auto-generated or user supplied MAC address */
599 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
600 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
602 * Enable various functionality by default if we're
603 * capable; the driver can override us if it knows better.
605 if (vap->iv_caps & IEEE80211_C_WME)
606 vap->iv_flags |= IEEE80211_F_WME;
607 if (vap->iv_caps & IEEE80211_C_BURST)
608 vap->iv_flags |= IEEE80211_F_BURST;
609 /* NB: bg scanning only makes sense for station mode right now */
610 if (vap->iv_opmode == IEEE80211_M_STA &&
611 (vap->iv_caps & IEEE80211_C_BGSCAN))
612 vap->iv_flags |= IEEE80211_F_BGSCAN;
613 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
614 /* NB: DFS support only makes sense for ap mode right now */
615 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
616 (vap->iv_caps & IEEE80211_C_DFS))
617 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
618 /* NB: only flip on U-APSD for hostap/sta for now */
619 if ((vap->iv_opmode == IEEE80211_M_STA)
620 || (vap->iv_opmode == IEEE80211_M_HOSTAP)) {
621 if (vap->iv_caps & IEEE80211_C_UAPSD)
622 vap->iv_flags_ext |= IEEE80211_FEXT_UAPSD;
625 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
626 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
627 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
629 * Install a default reset method for the ioctl support;
630 * the driver can override this.
632 vap->iv_reset = default_reset;
635 * Install a default crypto key update method, the driver
638 vap->iv_update_deftxkey = default_update_deftxkey;
640 ieee80211_sysctl_vattach(vap);
641 ieee80211_crypto_vattach(vap);
642 ieee80211_node_vattach(vap);
643 ieee80211_power_vattach(vap);
644 ieee80211_proto_vattach(vap);
645 #ifdef IEEE80211_SUPPORT_SUPERG
646 ieee80211_superg_vattach(vap);
648 ieee80211_ht_vattach(vap);
649 ieee80211_vht_vattach(vap);
650 ieee80211_scan_vattach(vap);
651 ieee80211_regdomain_vattach(vap);
652 ieee80211_radiotap_vattach(vap);
653 ieee80211_vap_reset_erp(vap);
654 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
660 * Activate a vap. State should have been prepared with a
661 * call to ieee80211_vap_setup and by the driver. On return
662 * from this call the vap is ready for use.
665 ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
666 ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
668 struct ifnet *ifp = vap->iv_ifp;
669 struct ieee80211com *ic = vap->iv_ic;
670 struct ifmediareq imr;
673 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
674 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
675 __func__, ieee80211_opmode_name[vap->iv_opmode],
676 ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
679 * Do late attach work that cannot happen until after
680 * the driver has had a chance to override defaults.
682 ieee80211_node_latevattach(vap);
683 ieee80211_power_latevattach(vap);
685 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
686 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
687 ieee80211_media_status(ifp, &imr);
688 /* NB: strip explicit mode; we're actually in autoselect */
689 ifmedia_set(&vap->iv_media,
690 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
692 ifp->if_baudrate = IF_Mbps(maxrate);
694 ether_ifattach(ifp, macaddr);
695 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
696 /* hook output method setup by ether_ifattach */
697 vap->iv_output = ifp->if_output;
698 ifp->if_output = ieee80211_output;
699 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
702 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
703 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
704 #ifdef IEEE80211_SUPPORT_SUPERG
705 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
707 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
708 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
709 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
710 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
712 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
713 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
714 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
715 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
716 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
717 IEEE80211_UNLOCK(ic);
723 * Tear down vap state and reclaim the ifnet.
724 * The driver is assumed to have prepared for
725 * this; e.g. by turning off interrupts for the
729 ieee80211_vap_detach(struct ieee80211vap *vap)
731 struct ieee80211com *ic = vap->iv_ic;
732 struct ifnet *ifp = vap->iv_ifp;
734 CURVNET_SET(ifp->if_vnet);
736 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
737 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
739 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
745 * Flush any deferred vap tasks.
747 ieee80211_draintask(ic, &vap->iv_nstate_task);
748 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
749 ieee80211_draintask(ic, &vap->iv_wme_task);
750 ieee80211_draintask(ic, &ic->ic_parent_task);
752 /* XXX band-aid until ifnet handles this for us */
753 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
756 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
757 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
758 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
759 #ifdef IEEE80211_SUPPORT_SUPERG
760 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
762 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
763 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
764 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
765 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
767 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
768 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
769 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
770 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
771 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
773 /* NB: this handles the bpfdetach done below */
774 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
775 if (vap->iv_ifflags & IFF_PROMISC)
776 ieee80211_promisc(vap, false);
777 if (vap->iv_ifflags & IFF_ALLMULTI)
778 ieee80211_allmulti(vap, false);
779 IEEE80211_UNLOCK(ic);
781 ifmedia_removeall(&vap->iv_media);
783 ieee80211_radiotap_vdetach(vap);
784 ieee80211_regdomain_vdetach(vap);
785 ieee80211_scan_vdetach(vap);
786 #ifdef IEEE80211_SUPPORT_SUPERG
787 ieee80211_superg_vdetach(vap);
789 ieee80211_vht_vdetach(vap);
790 ieee80211_ht_vdetach(vap);
791 /* NB: must be before ieee80211_node_vdetach */
792 ieee80211_proto_vdetach(vap);
793 ieee80211_crypto_vdetach(vap);
794 ieee80211_power_vdetach(vap);
795 ieee80211_node_vdetach(vap);
796 ieee80211_sysctl_vdetach(vap);
804 * Count number of vaps in promisc, and issue promisc on
805 * parent respectively.
808 ieee80211_promisc(struct ieee80211vap *vap, bool on)
810 struct ieee80211com *ic = vap->iv_ic;
812 IEEE80211_LOCK_ASSERT(ic);
815 if (++ic->ic_promisc == 1)
816 ieee80211_runtask(ic, &ic->ic_promisc_task);
818 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
820 if (--ic->ic_promisc == 0)
821 ieee80211_runtask(ic, &ic->ic_promisc_task);
826 * Count number of vaps in allmulti, and issue allmulti on
827 * parent respectively.
830 ieee80211_allmulti(struct ieee80211vap *vap, bool on)
832 struct ieee80211com *ic = vap->iv_ic;
834 IEEE80211_LOCK_ASSERT(ic);
837 if (++ic->ic_allmulti == 1)
838 ieee80211_runtask(ic, &ic->ic_mcast_task);
840 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
842 if (--ic->ic_allmulti == 0)
843 ieee80211_runtask(ic, &ic->ic_mcast_task);
848 * Synchronize flag bit state in the com structure
849 * according to the state of all vap's. This is used,
850 * for example, to handle state changes via ioctls.
853 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
855 struct ieee80211vap *vap;
858 IEEE80211_LOCK_ASSERT(ic);
861 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
862 if (vap->iv_flags & flag) {
867 ic->ic_flags |= flag;
869 ic->ic_flags &= ~flag;
873 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
875 struct ieee80211com *ic = vap->iv_ic;
880 vap->iv_flags &= ~flag;
882 vap->iv_flags |= flag;
883 ieee80211_syncflag_locked(ic, flag);
884 IEEE80211_UNLOCK(ic);
888 * Synchronize flags_ht bit state in the com structure
889 * according to the state of all vap's. This is used,
890 * for example, to handle state changes via ioctls.
893 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
895 struct ieee80211vap *vap;
898 IEEE80211_LOCK_ASSERT(ic);
901 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
902 if (vap->iv_flags_ht & flag) {
907 ic->ic_flags_ht |= flag;
909 ic->ic_flags_ht &= ~flag;
913 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
915 struct ieee80211com *ic = vap->iv_ic;
920 vap->iv_flags_ht &= ~flag;
922 vap->iv_flags_ht |= flag;
923 ieee80211_syncflag_ht_locked(ic, flag);
924 IEEE80211_UNLOCK(ic);
928 * Synchronize flags_vht bit state in the com structure
929 * according to the state of all vap's. This is used,
930 * for example, to handle state changes via ioctls.
933 ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag)
935 struct ieee80211vap *vap;
938 IEEE80211_LOCK_ASSERT(ic);
941 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
942 if (vap->iv_flags_vht & flag) {
947 ic->ic_flags_vht |= flag;
949 ic->ic_flags_vht &= ~flag;
953 ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag)
955 struct ieee80211com *ic = vap->iv_ic;
960 vap->iv_flags_vht &= ~flag;
962 vap->iv_flags_vht |= flag;
963 ieee80211_syncflag_vht_locked(ic, flag);
964 IEEE80211_UNLOCK(ic);
968 * Synchronize flags_ext bit state in the com structure
969 * according to the state of all vap's. This is used,
970 * for example, to handle state changes via ioctls.
973 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
975 struct ieee80211vap *vap;
978 IEEE80211_LOCK_ASSERT(ic);
981 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
982 if (vap->iv_flags_ext & flag) {
987 ic->ic_flags_ext |= flag;
989 ic->ic_flags_ext &= ~flag;
993 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
995 struct ieee80211com *ic = vap->iv_ic;
1000 vap->iv_flags_ext &= ~flag;
1002 vap->iv_flags_ext |= flag;
1003 ieee80211_syncflag_ext_locked(ic, flag);
1004 IEEE80211_UNLOCK(ic);
1008 mapgsm(u_int freq, u_int flags)
1011 if (flags & IEEE80211_CHAN_QUARTER)
1013 else if (flags & IEEE80211_CHAN_HALF)
1017 /* NB: there is no 907/20 wide but leave room */
1018 return (freq - 906*10) / 5;
1022 mappsb(u_int freq, u_int flags)
1024 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
1028 * Convert MHz frequency to IEEE channel number.
1031 ieee80211_mhz2ieee(u_int freq, u_int flags)
1033 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
1034 if (flags & IEEE80211_CHAN_GSM)
1035 return mapgsm(freq, flags);
1036 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
1040 return ((int) freq - 2407) / 5;
1042 return 15 + ((freq - 2512) / 20);
1043 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
1045 /* XXX check regdomain? */
1046 if (IS_FREQ_IN_PSB(freq))
1047 return mappsb(freq, flags);
1048 return (freq - 4000) / 5;
1050 return (freq - 5000) / 5;
1051 } else { /* either, guess */
1055 if (907 <= freq && freq <= 922)
1056 return mapgsm(freq, flags);
1057 return ((int) freq - 2407) / 5;
1060 if (IS_FREQ_IN_PSB(freq))
1061 return mappsb(freq, flags);
1062 else if (freq > 4900)
1063 return (freq - 4000) / 5;
1065 return 15 + ((freq - 2512) / 20);
1067 return (freq - 5000) / 5;
1069 #undef IS_FREQ_IN_PSB
1073 * Convert channel to IEEE channel number.
1076 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
1079 ic_printf(ic, "invalid channel (NULL)\n");
1082 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
1086 * Convert IEEE channel number to MHz frequency.
1089 ieee80211_ieee2mhz(u_int chan, u_int flags)
1091 if (flags & IEEE80211_CHAN_GSM)
1092 return 907 + 5 * (chan / 10);
1093 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
1097 return 2407 + chan*5;
1099 return 2512 + ((chan-15)*20);
1100 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
1101 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
1103 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
1105 return 5000 + (chan*5);
1106 } else { /* either, guess */
1107 /* XXX can't distinguish PSB+GSM channels */
1110 if (chan < 14) /* 0-13 */
1111 return 2407 + chan*5;
1112 if (chan < 27) /* 15-26 */
1113 return 2512 + ((chan-15)*20);
1114 return 5000 + (chan*5);
1118 static __inline void
1119 set_extchan(struct ieee80211_channel *c)
1123 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1124 * "the secondary channel number shall be 'N + [1,-1] * 4'
1126 if (c->ic_flags & IEEE80211_CHAN_HT40U)
1127 c->ic_extieee = c->ic_ieee + 4;
1128 else if (c->ic_flags & IEEE80211_CHAN_HT40D)
1129 c->ic_extieee = c->ic_ieee - 4;
1135 * Populate the freq1/freq2 fields as appropriate for VHT channels.
1137 * This for now uses a hard-coded list of 80MHz wide channels.
1139 * For HT20/HT40, freq1 just is the centre frequency of the 40MHz
1140 * wide channel we've already decided upon.
1142 * For VHT80 and VHT160, there are only a small number of fixed
1143 * 80/160MHz wide channels, so we just use those.
1145 * This is all likely very very wrong - both the regulatory code
1146 * and this code needs to ensure that all four channels are
1147 * available and valid before the VHT80 (and eight for VHT160) channel
1151 struct vht_chan_range {
1152 uint16_t freq_start;
1156 struct vht_chan_range vht80_chan_ranges[] = {
1166 struct vht_chan_range vht160_chan_ranges[] = {
1173 set_vht_extchan(struct ieee80211_channel *c)
1177 if (! IEEE80211_IS_CHAN_VHT(c))
1180 if (IEEE80211_IS_CHAN_VHT80P80(c)) {
1181 printf("%s: TODO VHT80+80 channel (ieee=%d, flags=0x%08x)\n",
1182 __func__, c->ic_ieee, c->ic_flags);
1185 if (IEEE80211_IS_CHAN_VHT160(c)) {
1186 for (i = 0; vht160_chan_ranges[i].freq_start != 0; i++) {
1187 if (c->ic_freq >= vht160_chan_ranges[i].freq_start &&
1188 c->ic_freq < vht160_chan_ranges[i].freq_end) {
1191 midpoint = vht160_chan_ranges[i].freq_start + 80;
1192 c->ic_vht_ch_freq1 =
1193 ieee80211_mhz2ieee(midpoint, c->ic_flags);
1194 c->ic_vht_ch_freq2 = 0;
1196 printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
1197 __func__, c->ic_ieee, c->ic_freq, midpoint,
1198 c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
1206 if (IEEE80211_IS_CHAN_VHT80(c)) {
1207 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1208 if (c->ic_freq >= vht80_chan_ranges[i].freq_start &&
1209 c->ic_freq < vht80_chan_ranges[i].freq_end) {
1212 midpoint = vht80_chan_ranges[i].freq_start + 40;
1213 c->ic_vht_ch_freq1 =
1214 ieee80211_mhz2ieee(midpoint, c->ic_flags);
1215 c->ic_vht_ch_freq2 = 0;
1217 printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
1218 __func__, c->ic_ieee, c->ic_freq, midpoint,
1219 c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
1227 if (IEEE80211_IS_CHAN_VHT40(c)) {
1228 if (IEEE80211_IS_CHAN_HT40U(c))
1229 c->ic_vht_ch_freq1 = c->ic_ieee + 2;
1230 else if (IEEE80211_IS_CHAN_HT40D(c))
1231 c->ic_vht_ch_freq1 = c->ic_ieee - 2;
1237 if (IEEE80211_IS_CHAN_VHT20(c)) {
1238 c->ic_vht_ch_freq1 = c->ic_ieee;
1242 printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n",
1243 __func__, c->ic_ieee, c->ic_flags);
1249 * Return whether the current channel could possibly be a part of
1250 * a VHT80/VHT160 channel.
1252 * This doesn't check that the whole range is in the allowed list
1253 * according to regulatory.
1256 is_vht160_valid_freq(uint16_t freq)
1260 for (i = 0; vht160_chan_ranges[i].freq_start != 0; i++) {
1261 if (freq >= vht160_chan_ranges[i].freq_start &&
1262 freq < vht160_chan_ranges[i].freq_end)
1269 is_vht80_valid_freq(uint16_t freq)
1272 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1273 if (freq >= vht80_chan_ranges[i].freq_start &&
1274 freq < vht80_chan_ranges[i].freq_end)
1281 addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1282 uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1284 struct ieee80211_channel *c;
1286 if (*nchans >= maxchans)
1290 printf("%s: %d of %d: ieee=%d, freq=%d, flags=0x%08x\n",
1291 __func__, *nchans, maxchans, ieee, freq, flags);
1294 c = &chans[(*nchans)++];
1296 c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1297 c->ic_maxregpower = maxregpower;
1298 c->ic_maxpower = 2 * maxregpower;
1299 c->ic_flags = flags;
1300 c->ic_vht_ch_freq1 = 0;
1301 c->ic_vht_ch_freq2 = 0;
1309 copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1312 struct ieee80211_channel *c;
1314 KASSERT(*nchans > 0, ("channel list is empty\n"));
1316 if (*nchans >= maxchans)
1320 printf("%s: %d of %d: flags=0x%08x\n",
1321 __func__, *nchans, maxchans, flags);
1324 c = &chans[(*nchans)++];
1326 c->ic_flags = flags;
1327 c->ic_vht_ch_freq1 = 0;
1328 c->ic_vht_ch_freq2 = 0;
1339 getflags_2ghz(const uint8_t bands[], uint32_t flags[], int cbw_flags)
1344 if (isset(bands, IEEE80211_MODE_11B))
1345 flags[nmodes++] = IEEE80211_CHAN_B;
1346 if (isset(bands, IEEE80211_MODE_11G))
1347 flags[nmodes++] = IEEE80211_CHAN_G;
1348 if (isset(bands, IEEE80211_MODE_11NG))
1349 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1350 if (cbw_flags & NET80211_CBW_FLAG_HT40) {
1351 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1352 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1358 getflags_5ghz(const uint8_t bands[], uint32_t flags[], int cbw_flags)
1363 * The addchan_list() function seems to expect the flags array to
1364 * be in channel width order, so the VHT bits are interspersed
1365 * as appropriate to maintain said order.
1367 * It also assumes HT40U is before HT40D.
1372 if (isset(bands, IEEE80211_MODE_11A))
1373 flags[nmodes++] = IEEE80211_CHAN_A;
1374 if (isset(bands, IEEE80211_MODE_11NA))
1375 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1376 if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1377 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
1378 IEEE80211_CHAN_VHT20;
1382 if (cbw_flags & NET80211_CBW_FLAG_HT40)
1383 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1384 if ((cbw_flags & NET80211_CBW_FLAG_HT40) &&
1385 isset(bands, IEEE80211_MODE_VHT_5GHZ))
1386 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
1387 IEEE80211_CHAN_VHT40U;
1388 if (cbw_flags & NET80211_CBW_FLAG_HT40)
1389 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1390 if ((cbw_flags & NET80211_CBW_FLAG_HT40) &&
1391 isset(bands, IEEE80211_MODE_VHT_5GHZ))
1392 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
1393 IEEE80211_CHAN_VHT40D;
1396 if ((cbw_flags & NET80211_CBW_FLAG_VHT80) &&
1397 isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1398 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
1399 IEEE80211_CHAN_VHT80;
1400 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
1401 IEEE80211_CHAN_VHT80;
1405 if ((cbw_flags & NET80211_CBW_FLAG_VHT160) &&
1406 isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1407 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
1408 IEEE80211_CHAN_VHT160;
1409 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
1410 IEEE80211_CHAN_VHT160;
1414 if ((cbw_flags & NET80211_CBW_FLAG_VHT80P80) &&
1415 isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1416 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U |
1417 IEEE80211_CHAN_VHT80P80;
1418 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D |
1419 IEEE80211_CHAN_VHT80P80;
1426 getflags(const uint8_t bands[], uint32_t flags[], int cbw_flags)
1430 if (isset(bands, IEEE80211_MODE_11A) ||
1431 isset(bands, IEEE80211_MODE_11NA) ||
1432 isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1433 if (isset(bands, IEEE80211_MODE_11B) ||
1434 isset(bands, IEEE80211_MODE_11G) ||
1435 isset(bands, IEEE80211_MODE_11NG) ||
1436 isset(bands, IEEE80211_MODE_VHT_2GHZ))
1439 getflags_5ghz(bands, flags, cbw_flags);
1441 getflags_2ghz(bands, flags, cbw_flags);
1445 * Add one 20 MHz channel into specified channel list.
1446 * You MUST NOT mix bands when calling this. It will not add 5ghz
1447 * channels if you have any B/G/N band bit set.
1448 * The _cbw() variant does also support HT40/VHT80/160/80+80.
1451 ieee80211_add_channel_cbw(struct ieee80211_channel chans[], int maxchans,
1452 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1453 uint32_t chan_flags, const uint8_t bands[], int cbw_flags)
1455 uint32_t flags[IEEE80211_MODE_MAX];
1458 getflags(bands, flags, cbw_flags);
1459 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1461 error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1462 flags[0] | chan_flags);
1463 for (i = 1; flags[i] != 0 && error == 0; i++) {
1464 error = copychan_prev(chans, maxchans, nchans,
1465 flags[i] | chan_flags);
1472 ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1473 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1474 uint32_t chan_flags, const uint8_t bands[])
1477 return (ieee80211_add_channel_cbw(chans, maxchans, nchans, ieee, freq,
1478 maxregpower, chan_flags, bands, 0));
1481 static struct ieee80211_channel *
1482 findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1485 struct ieee80211_channel *c;
1488 flags &= IEEE80211_CHAN_ALLTURBO;
1489 /* brute force search */
1490 for (i = 0; i < nchans; i++) {
1492 if (c->ic_freq == freq &&
1493 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1500 * Add 40 MHz channel pair into specified channel list.
1504 ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1505 int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1507 struct ieee80211_channel *cent, *extc;
1511 freq = ieee80211_ieee2mhz(ieee, flags);
1514 * Each entry defines an HT40 channel pair; find the
1515 * center channel, then the extension channel above.
1517 flags |= IEEE80211_CHAN_HT20;
1518 cent = findchannel(chans, *nchans, freq, flags);
1522 extc = findchannel(chans, *nchans, freq + 20, flags);
1526 flags &= ~IEEE80211_CHAN_HT;
1527 error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1528 maxregpower, flags | IEEE80211_CHAN_HT40U);
1532 error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1533 maxregpower, flags | IEEE80211_CHAN_HT40D);
1539 * Fetch the center frequency for the primary channel.
1542 ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
1545 return (c->ic_freq);
1549 * Fetch the center frequency for the primary BAND channel.
1551 * For 5, 10, 20MHz channels it'll be the normally configured channel
1554 * For 40MHz, 80MHz, 160MHz channels it will be the centre of the
1555 * wide channel, not the centre of the primary channel (that's ic_freq).
1557 * For 80+80MHz channels this will be the centre of the primary
1558 * 80MHz channel; the secondary 80MHz channel will be center_freq2().
1561 ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
1565 * VHT - use the pre-calculated centre frequency
1566 * of the given channel.
1568 if (IEEE80211_IS_CHAN_VHT(c))
1569 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags));
1571 if (IEEE80211_IS_CHAN_HT40U(c)) {
1572 return (c->ic_freq + 10);
1574 if (IEEE80211_IS_CHAN_HT40D(c)) {
1575 return (c->ic_freq - 10);
1578 return (c->ic_freq);
1582 * For now, no 80+80 support; it will likely always return 0.
1585 ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
1588 if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0))
1589 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags));
1595 * Adds channels into specified channel list (ieee[] array must be sorted).
1596 * Channels are already sorted.
1599 add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1600 const uint8_t ieee[], int nieee, uint32_t flags[])
1606 for (i = 0; i < nieee; i++) {
1607 freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1608 for (j = 0; flags[j] != 0; j++) {
1611 * + HT40 and VHT40 channels occur together, so
1612 * we need to be careful that we actually allow that.
1613 * + VHT80, VHT160 will coexist with HT40/VHT40, so
1614 * make sure it's not skipped because of the overlap
1615 * check used for (V)HT40.
1617 is_vht = !! (flags[j] & IEEE80211_CHAN_VHT);
1619 /* XXX TODO FIXME VHT80P80. */
1621 /* Test for VHT160 analogue to the VHT80 below. */
1622 if (is_vht && flags[j] & IEEE80211_CHAN_VHT160)
1623 if (! is_vht160_valid_freq(freq))
1628 * XXX This is all very broken right now.
1629 * What we /should/ do is:
1631 * + check that the frequency is in the list of
1632 * allowed VHT80 ranges; and
1633 * + the other 3 channels in the list are actually
1636 if (is_vht && flags[j] & IEEE80211_CHAN_VHT80)
1637 if (! is_vht80_valid_freq(freq))
1643 * This is also a fall through from VHT80; as we only
1644 * allow a VHT80 channel if the VHT40 combination is
1645 * also valid. If the VHT40 form is not valid then
1646 * we certainly can't do VHT80..
1648 if (flags[j] & IEEE80211_CHAN_HT40D)
1650 * Can't have a "lower" channel if we are the
1653 * Can't have a "lower" channel if it's below/
1654 * within 20MHz of the first channel.
1656 * Can't have a "lower" channel if the channel
1657 * below it is not 20MHz away.
1659 if (i == 0 || ieee[i] < ieee[0] + 4 ||
1661 ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1663 if (flags[j] & IEEE80211_CHAN_HT40U)
1665 * Can't have an "upper" channel if we are
1668 * Can't have an "upper" channel be above the
1669 * last channel in the list.
1671 * Can't have an "upper" channel if the next
1672 * channel according to the math isn't 20MHz
1673 * away. (Likely for channel 13/14.)
1675 if (i == nieee - 1 ||
1676 ieee[i] + 4 > ieee[nieee - 1] ||
1678 ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1682 error = addchan(chans, maxchans, nchans,
1683 ieee[i], freq, 0, flags[j]);
1685 error = copychan_prev(chans, maxchans, nchans,
1697 ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1698 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1701 uint32_t flags[IEEE80211_MODE_MAX];
1703 /* XXX no VHT for now */
1704 getflags_2ghz(bands, flags, cbw_flags);
1705 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1707 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1711 ieee80211_add_channels_default_2ghz(struct ieee80211_channel chans[],
1712 int maxchans, int *nchans, const uint8_t bands[], int cbw_flags)
1714 const uint8_t default_chan_list[] =
1715 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
1717 return (ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
1718 default_chan_list, nitems(default_chan_list), bands, cbw_flags));
1722 ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1723 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1727 * XXX-BZ with HT and VHT there is no 1:1 mapping anymore. Review all
1728 * uses of IEEE80211_MODE_MAX and add a new #define name for array size.
1730 uint32_t flags[2 * IEEE80211_MODE_MAX];
1732 getflags_5ghz(bands, flags, cbw_flags);
1733 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1735 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1739 * Locate a channel given a frequency+flags. We cache
1740 * the previous lookup to optimize switching between two
1741 * channels--as happens with dynamic turbo.
1743 struct ieee80211_channel *
1744 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1746 struct ieee80211_channel *c;
1748 flags &= IEEE80211_CHAN_ALLTURBO;
1749 c = ic->ic_prevchan;
1750 if (c != NULL && c->ic_freq == freq &&
1751 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1753 /* brute force search */
1754 return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
1758 * Locate a channel given a channel number+flags. We cache
1759 * the previous lookup to optimize switching between two
1760 * channels--as happens with dynamic turbo.
1762 struct ieee80211_channel *
1763 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1765 struct ieee80211_channel *c;
1768 flags &= IEEE80211_CHAN_ALLTURBO;
1769 c = ic->ic_prevchan;
1770 if (c != NULL && c->ic_ieee == ieee &&
1771 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1773 /* brute force search */
1774 for (i = 0; i < ic->ic_nchans; i++) {
1775 c = &ic->ic_channels[i];
1776 if (c->ic_ieee == ieee &&
1777 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1784 * Lookup a channel suitable for the given rx status.
1786 * This is used to find a channel for a frame (eg beacon, probe
1787 * response) based purely on the received PHY information.
1789 * For now it tries to do it based on R_FREQ / R_IEEE.
1790 * This is enough for 11bg and 11a (and thus 11ng/11na)
1791 * but it will not be enough for GSM, PSB channels and the
1792 * like. It also doesn't know about legacy-turbog and
1793 * legacy-turbo modes, which some offload NICs actually
1794 * support in weird ways.
1796 * Takes the ic and rxstatus; returns the channel or NULL
1799 * XXX TODO: Add support for that when the need arises.
1801 struct ieee80211_channel *
1802 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1803 const struct ieee80211_rx_stats *rxs)
1805 struct ieee80211com *ic = vap->iv_ic;
1807 struct ieee80211_channel *c;
1813 * Strictly speaking we only use freq for now,
1814 * however later on we may wish to just store
1815 * the ieee for verification.
1817 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1819 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1821 if ((rxs->r_flags & IEEE80211_R_BAND) == 0)
1825 * If the rx status contains a valid ieee/freq, then
1826 * ensure we populate the correct channel information
1827 * in rxchan before passing it up to the scan infrastructure.
1828 * Offload NICs will pass up beacons from all channels
1829 * during background scans.
1832 /* Determine a band */
1833 switch (rxs->c_band) {
1834 case IEEE80211_CHAN_2GHZ:
1835 flags = IEEE80211_CHAN_G;
1837 case IEEE80211_CHAN_5GHZ:
1838 flags = IEEE80211_CHAN_A;
1841 if (rxs->c_freq < 3000) {
1842 flags = IEEE80211_CHAN_G;
1844 flags = IEEE80211_CHAN_A;
1849 /* Channel lookup */
1850 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1852 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1853 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1854 __func__, (int) rxs->c_freq, (int) rxs->c_ieee, flags, c);
1860 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1862 #define ADD(_ic, _s, _o) \
1863 ifmedia_add(media, \
1864 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1865 static const u_int mopts[IEEE80211_MODE_MAX] = {
1866 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1867 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1868 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1869 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1870 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1871 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1872 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1873 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1874 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1875 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1876 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1877 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1878 [IEEE80211_MODE_VHT_2GHZ] = IFM_IEEE80211_VHT2G,
1879 [IEEE80211_MODE_VHT_5GHZ] = IFM_IEEE80211_VHT5G,
1885 ADD(ic, mword, mopt); /* STA mode has no cap */
1886 if (caps & IEEE80211_C_IBSS)
1887 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1888 if (caps & IEEE80211_C_HOSTAP)
1889 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1890 if (caps & IEEE80211_C_AHDEMO)
1891 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1892 if (caps & IEEE80211_C_MONITOR)
1893 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1894 if (caps & IEEE80211_C_WDS)
1895 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1896 if (caps & IEEE80211_C_MBSS)
1897 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1902 * Setup the media data structures according to the channel and
1906 ieee80211_media_setup(struct ieee80211com *ic,
1907 struct ifmedia *media, int caps, int addsta,
1908 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1910 int i, j, rate, maxrate, mword, r;
1911 enum ieee80211_phymode mode;
1912 const struct ieee80211_rateset *rs;
1913 struct ieee80211_rateset allrates;
1916 * Fill in media characteristics.
1918 ifmedia_init(media, 0, media_change, media_stat);
1921 * Add media for legacy operating modes.
1923 memset(&allrates, 0, sizeof(allrates));
1924 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1925 if (isclr(ic->ic_modecaps, mode))
1927 addmedia(media, caps, addsta, mode, IFM_AUTO);
1928 if (mode == IEEE80211_MODE_AUTO)
1930 rs = &ic->ic_sup_rates[mode];
1931 for (i = 0; i < rs->rs_nrates; i++) {
1932 rate = rs->rs_rates[i];
1933 mword = ieee80211_rate2media(ic, rate, mode);
1936 addmedia(media, caps, addsta, mode, mword);
1938 * Add legacy rate to the collection of all rates.
1940 r = rate & IEEE80211_RATE_VAL;
1941 for (j = 0; j < allrates.rs_nrates; j++)
1942 if (allrates.rs_rates[j] == r)
1944 if (j == allrates.rs_nrates) {
1945 /* unique, add to the set */
1946 allrates.rs_rates[j] = r;
1947 allrates.rs_nrates++;
1949 rate = (rate & IEEE80211_RATE_VAL) / 2;
1954 for (i = 0; i < allrates.rs_nrates; i++) {
1955 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1956 IEEE80211_MODE_AUTO);
1959 /* NB: remove media options from mword */
1960 addmedia(media, caps, addsta,
1961 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1964 * Add HT/11n media. Note that we do not have enough
1965 * bits in the media subtype to express the MCS so we
1966 * use a "placeholder" media subtype and any fixed MCS
1967 * must be specified with a different mechanism.
1969 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1970 if (isclr(ic->ic_modecaps, mode))
1972 addmedia(media, caps, addsta, mode, IFM_AUTO);
1973 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1975 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1976 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1977 addmedia(media, caps, addsta,
1978 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1979 i = ic->ic_txstream * 8 - 1;
1980 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1981 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1982 rate = ieee80211_htrates[i].ht40_rate_400ns;
1983 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1984 rate = ieee80211_htrates[i].ht40_rate_800ns;
1985 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1986 rate = ieee80211_htrates[i].ht20_rate_400ns;
1988 rate = ieee80211_htrates[i].ht20_rate_800ns;
1995 * XXX-BZ skip "VHT_2GHZ" for now.
1997 for (mode = IEEE80211_MODE_VHT_5GHZ; mode <= IEEE80211_MODE_VHT_5GHZ;
1999 if (isclr(ic->ic_modecaps, mode))
2001 addmedia(media, caps, addsta, mode, IFM_AUTO);
2002 addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT);
2004 if (isset(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ)) {
2005 addmedia(media, caps, addsta,
2006 IEEE80211_MODE_AUTO, IFM_IEEE80211_VHT);
2008 /* XXX TODO: VHT maxrate */
2014 /* XXX inline or eliminate? */
2015 const struct ieee80211_rateset *
2016 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
2018 /* XXX does this work for 11ng basic rates? */
2019 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
2022 /* XXX inline or eliminate? */
2023 const struct ieee80211_htrateset *
2024 ieee80211_get_suphtrates(struct ieee80211com *ic,
2025 const struct ieee80211_channel *c)
2027 return &ic->ic_sup_htrates;
2031 ieee80211_announce(struct ieee80211com *ic)
2034 enum ieee80211_phymode mode;
2035 const struct ieee80211_rateset *rs;
2037 /* NB: skip AUTO since it has no rates */
2038 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
2039 if (isclr(ic->ic_modecaps, mode))
2041 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
2042 rs = &ic->ic_sup_rates[mode];
2043 for (i = 0; i < rs->rs_nrates; i++) {
2044 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
2047 rate = ieee80211_media2rate(mword);
2048 printf("%s%d%sMbps", (i != 0 ? " " : ""),
2049 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
2053 ieee80211_ht_announce(ic);
2054 ieee80211_vht_announce(ic);
2058 ieee80211_announce_channels(struct ieee80211com *ic)
2060 const struct ieee80211_channel *c;
2064 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
2065 for (i = 0; i < ic->ic_nchans; i++) {
2066 c = &ic->ic_channels[i];
2067 if (IEEE80211_IS_CHAN_ST(c))
2069 else if (IEEE80211_IS_CHAN_108A(c))
2071 else if (IEEE80211_IS_CHAN_108G(c))
2073 else if (IEEE80211_IS_CHAN_HT(c))
2075 else if (IEEE80211_IS_CHAN_A(c))
2077 else if (IEEE80211_IS_CHAN_ANYG(c))
2079 else if (IEEE80211_IS_CHAN_B(c))
2083 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
2085 else if (IEEE80211_IS_CHAN_HALF(c))
2087 else if (IEEE80211_IS_CHAN_QUARTER(c))
2091 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
2092 , c->ic_ieee, c->ic_freq, type
2094 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
2095 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
2097 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
2098 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
2104 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
2106 switch (IFM_MODE(ime->ifm_media)) {
2107 case IFM_IEEE80211_11A:
2108 *mode = IEEE80211_MODE_11A;
2110 case IFM_IEEE80211_11B:
2111 *mode = IEEE80211_MODE_11B;
2113 case IFM_IEEE80211_11G:
2114 *mode = IEEE80211_MODE_11G;
2116 case IFM_IEEE80211_FH:
2117 *mode = IEEE80211_MODE_FH;
2119 case IFM_IEEE80211_11NA:
2120 *mode = IEEE80211_MODE_11NA;
2122 case IFM_IEEE80211_11NG:
2123 *mode = IEEE80211_MODE_11NG;
2125 case IFM_IEEE80211_VHT2G:
2126 *mode = IEEE80211_MODE_VHT_2GHZ;
2128 case IFM_IEEE80211_VHT5G:
2129 *mode = IEEE80211_MODE_VHT_5GHZ;
2132 *mode = IEEE80211_MODE_AUTO;
2138 * Turbo mode is an ``option''.
2139 * XXX does not apply to AUTO
2141 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
2142 if (*mode == IEEE80211_MODE_11A) {
2143 if (flags & IEEE80211_F_TURBOP)
2144 *mode = IEEE80211_MODE_TURBO_A;
2146 *mode = IEEE80211_MODE_STURBO_A;
2147 } else if (*mode == IEEE80211_MODE_11G)
2148 *mode = IEEE80211_MODE_TURBO_G;
2157 * Handle a media change request on the vap interface.
2160 ieee80211_media_change(struct ifnet *ifp)
2162 struct ieee80211vap *vap = ifp->if_softc;
2163 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
2166 if (!media2mode(ime, vap->iv_flags, &newmode))
2168 if (vap->iv_des_mode != newmode) {
2169 vap->iv_des_mode = newmode;
2170 /* XXX kick state machine if up+running */
2176 * Common code to calculate the media status word
2177 * from the operating mode and channel state.
2180 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
2184 status = IFM_IEEE80211;
2186 case IEEE80211_M_STA:
2188 case IEEE80211_M_IBSS:
2189 status |= IFM_IEEE80211_ADHOC;
2191 case IEEE80211_M_HOSTAP:
2192 status |= IFM_IEEE80211_HOSTAP;
2194 case IEEE80211_M_MONITOR:
2195 status |= IFM_IEEE80211_MONITOR;
2197 case IEEE80211_M_AHDEMO:
2198 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
2200 case IEEE80211_M_WDS:
2201 status |= IFM_IEEE80211_WDS;
2203 case IEEE80211_M_MBSS:
2204 status |= IFM_IEEE80211_MBSS;
2207 if (IEEE80211_IS_CHAN_VHT_5GHZ(chan)) {
2208 status |= IFM_IEEE80211_VHT5G;
2209 } else if (IEEE80211_IS_CHAN_VHT_2GHZ(chan)) {
2210 status |= IFM_IEEE80211_VHT2G;
2211 } else if (IEEE80211_IS_CHAN_HTA(chan)) {
2212 status |= IFM_IEEE80211_11NA;
2213 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
2214 status |= IFM_IEEE80211_11NG;
2215 } else if (IEEE80211_IS_CHAN_A(chan)) {
2216 status |= IFM_IEEE80211_11A;
2217 } else if (IEEE80211_IS_CHAN_B(chan)) {
2218 status |= IFM_IEEE80211_11B;
2219 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
2220 status |= IFM_IEEE80211_11G;
2221 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
2222 status |= IFM_IEEE80211_FH;
2224 /* XXX else complain? */
2226 if (IEEE80211_IS_CHAN_TURBO(chan))
2227 status |= IFM_IEEE80211_TURBO;
2229 if (IEEE80211_IS_CHAN_HT20(chan))
2230 status |= IFM_IEEE80211_HT20;
2231 if (IEEE80211_IS_CHAN_HT40(chan))
2232 status |= IFM_IEEE80211_HT40;
2238 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2240 struct ieee80211vap *vap = ifp->if_softc;
2241 struct ieee80211com *ic = vap->iv_ic;
2242 enum ieee80211_phymode mode;
2244 imr->ifm_status = IFM_AVALID;
2246 * NB: use the current channel's mode to lock down a xmit
2247 * rate only when running; otherwise we may have a mismatch
2248 * in which case the rate will not be convertible.
2250 if (vap->iv_state == IEEE80211_S_RUN ||
2251 vap->iv_state == IEEE80211_S_SLEEP) {
2252 imr->ifm_status |= IFM_ACTIVE;
2253 mode = ieee80211_chan2mode(ic->ic_curchan);
2255 mode = IEEE80211_MODE_AUTO;
2256 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
2258 * Calculate a current rate if possible.
2260 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
2262 * A fixed rate is set, report that.
2264 imr->ifm_active |= ieee80211_rate2media(ic,
2265 vap->iv_txparms[mode].ucastrate, mode);
2266 } else if (vap->iv_opmode == IEEE80211_M_STA) {
2268 * In station mode report the current transmit rate.
2270 imr->ifm_active |= ieee80211_rate2media(ic,
2271 vap->iv_bss->ni_txrate, mode);
2273 imr->ifm_active |= IFM_AUTO;
2274 if (imr->ifm_status & IFM_ACTIVE)
2275 imr->ifm_current = imr->ifm_active;
2279 * Set the current phy mode and recalculate the active channel
2280 * set based on the available channels for this mode. Also
2281 * select a new default/current channel if the current one is
2282 * inappropriate for this mode.
2285 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
2288 * Adjust basic rates in 11b/11g supported rate set.
2289 * Note that if operating on a hal/quarter rate channel
2290 * this is a noop as those rates sets are different
2293 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
2294 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
2296 ic->ic_curmode = mode;
2297 ieee80211_reset_erp(ic); /* reset global ERP state */
2303 * Return the phy mode for with the specified channel.
2305 enum ieee80211_phymode
2306 ieee80211_chan2mode(const struct ieee80211_channel *chan)
2309 if (IEEE80211_IS_CHAN_VHT_2GHZ(chan))
2310 return IEEE80211_MODE_VHT_2GHZ;
2311 else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan))
2312 return IEEE80211_MODE_VHT_5GHZ;
2313 else if (IEEE80211_IS_CHAN_HTA(chan))
2314 return IEEE80211_MODE_11NA;
2315 else if (IEEE80211_IS_CHAN_HTG(chan))
2316 return IEEE80211_MODE_11NG;
2317 else if (IEEE80211_IS_CHAN_108G(chan))
2318 return IEEE80211_MODE_TURBO_G;
2319 else if (IEEE80211_IS_CHAN_ST(chan))
2320 return IEEE80211_MODE_STURBO_A;
2321 else if (IEEE80211_IS_CHAN_TURBO(chan))
2322 return IEEE80211_MODE_TURBO_A;
2323 else if (IEEE80211_IS_CHAN_HALF(chan))
2324 return IEEE80211_MODE_HALF;
2325 else if (IEEE80211_IS_CHAN_QUARTER(chan))
2326 return IEEE80211_MODE_QUARTER;
2327 else if (IEEE80211_IS_CHAN_A(chan))
2328 return IEEE80211_MODE_11A;
2329 else if (IEEE80211_IS_CHAN_ANYG(chan))
2330 return IEEE80211_MODE_11G;
2331 else if (IEEE80211_IS_CHAN_B(chan))
2332 return IEEE80211_MODE_11B;
2333 else if (IEEE80211_IS_CHAN_FHSS(chan))
2334 return IEEE80211_MODE_FH;
2336 /* NB: should not get here */
2337 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
2338 __func__, chan->ic_freq, chan->ic_flags);
2339 return IEEE80211_MODE_11B;
2343 u_int match; /* rate + mode */
2344 u_int media; /* if_media rate */
2348 findmedia(const struct ratemedia rates[], int n, u_int match)
2352 for (i = 0; i < n; i++)
2353 if (rates[i].match == match)
2354 return rates[i].media;
2359 * Convert IEEE80211 rate value to ifmedia subtype.
2360 * Rate is either a legacy rate in units of 0.5Mbps
2364 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
2366 static const struct ratemedia rates[] = {
2367 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
2368 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
2369 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
2370 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
2371 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
2372 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
2373 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
2374 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
2375 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
2376 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
2377 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
2378 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
2379 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
2380 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
2381 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
2382 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
2383 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
2384 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
2385 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
2386 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
2387 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
2388 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
2389 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
2390 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
2391 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
2392 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
2393 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
2394 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
2395 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
2396 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
2397 /* NB: OFDM72 doesn't really exist so we don't handle it */
2399 static const struct ratemedia htrates[] = {
2400 { 0, IFM_IEEE80211_MCS },
2401 { 1, IFM_IEEE80211_MCS },
2402 { 2, IFM_IEEE80211_MCS },
2403 { 3, IFM_IEEE80211_MCS },
2404 { 4, IFM_IEEE80211_MCS },
2405 { 5, IFM_IEEE80211_MCS },
2406 { 6, IFM_IEEE80211_MCS },
2407 { 7, IFM_IEEE80211_MCS },
2408 { 8, IFM_IEEE80211_MCS },
2409 { 9, IFM_IEEE80211_MCS },
2410 { 10, IFM_IEEE80211_MCS },
2411 { 11, IFM_IEEE80211_MCS },
2412 { 12, IFM_IEEE80211_MCS },
2413 { 13, IFM_IEEE80211_MCS },
2414 { 14, IFM_IEEE80211_MCS },
2415 { 15, IFM_IEEE80211_MCS },
2416 { 16, IFM_IEEE80211_MCS },
2417 { 17, IFM_IEEE80211_MCS },
2418 { 18, IFM_IEEE80211_MCS },
2419 { 19, IFM_IEEE80211_MCS },
2420 { 20, IFM_IEEE80211_MCS },
2421 { 21, IFM_IEEE80211_MCS },
2422 { 22, IFM_IEEE80211_MCS },
2423 { 23, IFM_IEEE80211_MCS },
2424 { 24, IFM_IEEE80211_MCS },
2425 { 25, IFM_IEEE80211_MCS },
2426 { 26, IFM_IEEE80211_MCS },
2427 { 27, IFM_IEEE80211_MCS },
2428 { 28, IFM_IEEE80211_MCS },
2429 { 29, IFM_IEEE80211_MCS },
2430 { 30, IFM_IEEE80211_MCS },
2431 { 31, IFM_IEEE80211_MCS },
2432 { 32, IFM_IEEE80211_MCS },
2433 { 33, IFM_IEEE80211_MCS },
2434 { 34, IFM_IEEE80211_MCS },
2435 { 35, IFM_IEEE80211_MCS },
2436 { 36, IFM_IEEE80211_MCS },
2437 { 37, IFM_IEEE80211_MCS },
2438 { 38, IFM_IEEE80211_MCS },
2439 { 39, IFM_IEEE80211_MCS },
2440 { 40, IFM_IEEE80211_MCS },
2441 { 41, IFM_IEEE80211_MCS },
2442 { 42, IFM_IEEE80211_MCS },
2443 { 43, IFM_IEEE80211_MCS },
2444 { 44, IFM_IEEE80211_MCS },
2445 { 45, IFM_IEEE80211_MCS },
2446 { 46, IFM_IEEE80211_MCS },
2447 { 47, IFM_IEEE80211_MCS },
2448 { 48, IFM_IEEE80211_MCS },
2449 { 49, IFM_IEEE80211_MCS },
2450 { 50, IFM_IEEE80211_MCS },
2451 { 51, IFM_IEEE80211_MCS },
2452 { 52, IFM_IEEE80211_MCS },
2453 { 53, IFM_IEEE80211_MCS },
2454 { 54, IFM_IEEE80211_MCS },
2455 { 55, IFM_IEEE80211_MCS },
2456 { 56, IFM_IEEE80211_MCS },
2457 { 57, IFM_IEEE80211_MCS },
2458 { 58, IFM_IEEE80211_MCS },
2459 { 59, IFM_IEEE80211_MCS },
2460 { 60, IFM_IEEE80211_MCS },
2461 { 61, IFM_IEEE80211_MCS },
2462 { 62, IFM_IEEE80211_MCS },
2463 { 63, IFM_IEEE80211_MCS },
2464 { 64, IFM_IEEE80211_MCS },
2465 { 65, IFM_IEEE80211_MCS },
2466 { 66, IFM_IEEE80211_MCS },
2467 { 67, IFM_IEEE80211_MCS },
2468 { 68, IFM_IEEE80211_MCS },
2469 { 69, IFM_IEEE80211_MCS },
2470 { 70, IFM_IEEE80211_MCS },
2471 { 71, IFM_IEEE80211_MCS },
2472 { 72, IFM_IEEE80211_MCS },
2473 { 73, IFM_IEEE80211_MCS },
2474 { 74, IFM_IEEE80211_MCS },
2475 { 75, IFM_IEEE80211_MCS },
2476 { 76, IFM_IEEE80211_MCS },
2478 static const struct ratemedia vhtrates[] = {
2479 { 0, IFM_IEEE80211_VHT },
2480 { 1, IFM_IEEE80211_VHT },
2481 { 2, IFM_IEEE80211_VHT },
2482 { 3, IFM_IEEE80211_VHT },
2483 { 4, IFM_IEEE80211_VHT },
2484 { 5, IFM_IEEE80211_VHT },
2485 { 6, IFM_IEEE80211_VHT },
2486 { 7, IFM_IEEE80211_VHT },
2487 { 8, IFM_IEEE80211_VHT }, /* Optional. */
2488 { 9, IFM_IEEE80211_VHT }, /* Optional. */
2490 /* Some QCA and BRCM seem to support this; offspec. */
2491 { 10, IFM_IEEE80211_VHT },
2492 { 11, IFM_IEEE80211_VHT },
2498 * Check 11ac/11n rates first for match as an MCS.
2500 if (mode == IEEE80211_MODE_VHT_5GHZ) {
2501 if (rate & IFM_IEEE80211_VHT) {
2502 rate &= ~IFM_IEEE80211_VHT;
2503 m = findmedia(vhtrates, nitems(vhtrates), rate);
2505 return (m | IFM_IEEE80211_VHT);
2507 } else if (mode == IEEE80211_MODE_11NA) {
2508 if (rate & IEEE80211_RATE_MCS) {
2509 rate &= ~IEEE80211_RATE_MCS;
2510 m = findmedia(htrates, nitems(htrates), rate);
2512 return m | IFM_IEEE80211_11NA;
2514 } else if (mode == IEEE80211_MODE_11NG) {
2515 /* NB: 12 is ambiguous, it will be treated as an MCS */
2516 if (rate & IEEE80211_RATE_MCS) {
2517 rate &= ~IEEE80211_RATE_MCS;
2518 m = findmedia(htrates, nitems(htrates), rate);
2520 return m | IFM_IEEE80211_11NG;
2523 rate &= IEEE80211_RATE_VAL;
2525 case IEEE80211_MODE_11A:
2526 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
2527 case IEEE80211_MODE_QUARTER:
2528 case IEEE80211_MODE_11NA:
2529 case IEEE80211_MODE_TURBO_A:
2530 case IEEE80211_MODE_STURBO_A:
2531 return findmedia(rates, nitems(rates),
2532 rate | IFM_IEEE80211_11A);
2533 case IEEE80211_MODE_11B:
2534 return findmedia(rates, nitems(rates),
2535 rate | IFM_IEEE80211_11B);
2536 case IEEE80211_MODE_FH:
2537 return findmedia(rates, nitems(rates),
2538 rate | IFM_IEEE80211_FH);
2539 case IEEE80211_MODE_AUTO:
2540 /* NB: ic may be NULL for some drivers */
2541 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
2542 return findmedia(rates, nitems(rates),
2543 rate | IFM_IEEE80211_FH);
2544 /* NB: hack, 11g matches both 11b+11a rates */
2546 case IEEE80211_MODE_11G:
2547 case IEEE80211_MODE_11NG:
2548 case IEEE80211_MODE_TURBO_G:
2549 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
2550 case IEEE80211_MODE_VHT_2GHZ:
2551 case IEEE80211_MODE_VHT_5GHZ:
2552 /* XXX TODO: need to figure out mapping for VHT rates */
2559 ieee80211_media2rate(int mword)
2561 static const int ieeerates[] = {
2565 2, /* IFM_IEEE80211_FH1 */
2566 4, /* IFM_IEEE80211_FH2 */
2567 2, /* IFM_IEEE80211_DS1 */
2568 4, /* IFM_IEEE80211_DS2 */
2569 11, /* IFM_IEEE80211_DS5 */
2570 22, /* IFM_IEEE80211_DS11 */
2571 44, /* IFM_IEEE80211_DS22 */
2572 12, /* IFM_IEEE80211_OFDM6 */
2573 18, /* IFM_IEEE80211_OFDM9 */
2574 24, /* IFM_IEEE80211_OFDM12 */
2575 36, /* IFM_IEEE80211_OFDM18 */
2576 48, /* IFM_IEEE80211_OFDM24 */
2577 72, /* IFM_IEEE80211_OFDM36 */
2578 96, /* IFM_IEEE80211_OFDM48 */
2579 108, /* IFM_IEEE80211_OFDM54 */
2580 144, /* IFM_IEEE80211_OFDM72 */
2581 0, /* IFM_IEEE80211_DS354k */
2582 0, /* IFM_IEEE80211_DS512k */
2583 6, /* IFM_IEEE80211_OFDM3 */
2584 9, /* IFM_IEEE80211_OFDM4 */
2585 54, /* IFM_IEEE80211_OFDM27 */
2586 -1, /* IFM_IEEE80211_MCS */
2587 -1, /* IFM_IEEE80211_VHT */
2589 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2590 ieeerates[IFM_SUBTYPE(mword)] : 0;
2594 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2595 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2597 #define mix(a, b, c) \
2599 a -= b; a -= c; a ^= (c >> 13); \
2600 b -= c; b -= a; b ^= (a << 8); \
2601 c -= a; c -= b; c ^= (b >> 13); \
2602 a -= b; a -= c; a ^= (c >> 12); \
2603 b -= c; b -= a; b ^= (a << 16); \
2604 c -= a; c -= b; c ^= (b >> 5); \
2605 a -= b; a -= c; a ^= (c >> 3); \
2606 b -= c; b -= a; b ^= (a << 10); \
2607 c -= a; c -= b; c ^= (b >> 15); \
2608 } while (/*CONSTCOND*/0)
2611 ieee80211_mac_hash(const struct ieee80211com *ic,
2612 const uint8_t addr[IEEE80211_ADDR_LEN])
2614 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2630 ieee80211_channel_type_char(const struct ieee80211_channel *c)
2632 if (IEEE80211_IS_CHAN_ST(c))
2634 if (IEEE80211_IS_CHAN_108A(c))
2636 if (IEEE80211_IS_CHAN_108G(c))
2638 if (IEEE80211_IS_CHAN_VHT(c))
2640 if (IEEE80211_IS_CHAN_HT(c))
2642 if (IEEE80211_IS_CHAN_A(c))
2644 if (IEEE80211_IS_CHAN_ANYG(c))
2646 if (IEEE80211_IS_CHAN_B(c))