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)
246 * Set auto mode to reset active channel state and any desired channel.
248 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
253 null_update_mcast(struct ieee80211com *ic)
256 ic_printf(ic, "need multicast update callback\n");
260 null_update_promisc(struct ieee80211com *ic)
263 ic_printf(ic, "need promiscuous mode update callback\n");
267 null_update_chw(struct ieee80211com *ic)
270 ic_printf(ic, "%s: need callback\n", __func__);
274 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
279 retval = printf("%s: ", ic->ic_name);
281 retval += vprintf(fmt, ap);
286 static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
287 static struct mtx ic_list_mtx;
288 MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
291 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
293 struct ieee80211com *ic;
298 error = sysctl_wire_old_buffer(req, 0);
301 sbuf_new_for_sysctl(&sb, NULL, 8, req);
302 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
304 mtx_lock(&ic_list_mtx);
305 LIST_FOREACH(ic, &ic_head, ic_next) {
306 sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
309 mtx_unlock(&ic_list_mtx);
310 error = sbuf_finish(&sb);
315 SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
316 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
317 sysctl_ieee80211coms, "A", "names of available 802.11 devices");
320 * Attach/setup the common net80211 state. Called by
321 * the driver on attach to prior to creating any vap's.
324 ieee80211_ifattach(struct ieee80211com *ic)
327 IEEE80211_LOCK_INIT(ic, ic->ic_name);
328 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
329 TAILQ_INIT(&ic->ic_vaps);
331 /* Create a taskqueue for all state changes */
332 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
333 taskqueue_thread_enqueue, &ic->ic_tq);
334 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
336 ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
337 ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
339 * Fill in 802.11 available channel set, mark all
340 * available channels as active, and pick a default
341 * channel if not already specified.
343 ieee80211_chan_init(ic);
345 ic->ic_update_mcast = null_update_mcast;
346 ic->ic_update_promisc = null_update_promisc;
347 ic->ic_update_chw = null_update_chw;
349 ic->ic_hash_key = arc4random();
350 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
351 ic->ic_lintval = ic->ic_bintval;
352 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
354 ieee80211_crypto_attach(ic);
355 ieee80211_node_attach(ic);
356 ieee80211_power_attach(ic);
357 ieee80211_proto_attach(ic);
358 #ifdef IEEE80211_SUPPORT_SUPERG
359 ieee80211_superg_attach(ic);
361 ieee80211_ht_attach(ic);
362 ieee80211_vht_attach(ic);
363 ieee80211_scan_attach(ic);
364 ieee80211_regdomain_attach(ic);
365 ieee80211_dfs_attach(ic);
367 ieee80211_sysctl_attach(ic);
369 mtx_lock(&ic_list_mtx);
370 LIST_INSERT_HEAD(&ic_head, ic, ic_next);
371 mtx_unlock(&ic_list_mtx);
375 * Detach net80211 state on device detach. Tear down
376 * all vap's and reclaim all common state prior to the
377 * device state going away. Note we may call back into
378 * driver; it must be prepared for this.
381 ieee80211_ifdetach(struct ieee80211com *ic)
383 struct ieee80211vap *vap;
385 mtx_lock(&ic_list_mtx);
386 LIST_REMOVE(ic, ic_next);
387 mtx_unlock(&ic_list_mtx);
389 taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
392 * The VAP is responsible for setting and clearing
393 * the VIMAGE context.
395 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
396 ieee80211_vap_destroy(vap);
397 ieee80211_waitfor_parent(ic);
399 ieee80211_sysctl_detach(ic);
400 ieee80211_dfs_detach(ic);
401 ieee80211_regdomain_detach(ic);
402 ieee80211_scan_detach(ic);
403 #ifdef IEEE80211_SUPPORT_SUPERG
404 ieee80211_superg_detach(ic);
406 ieee80211_vht_detach(ic);
407 ieee80211_ht_detach(ic);
408 /* NB: must be called before ieee80211_node_detach */
409 ieee80211_proto_detach(ic);
410 ieee80211_crypto_detach(ic);
411 ieee80211_power_detach(ic);
412 ieee80211_node_detach(ic);
414 counter_u64_free(ic->ic_ierrors);
415 counter_u64_free(ic->ic_oerrors);
417 taskqueue_free(ic->ic_tq);
418 IEEE80211_TX_LOCK_DESTROY(ic);
419 IEEE80211_LOCK_DESTROY(ic);
422 struct ieee80211com *
423 ieee80211_find_com(const char *name)
425 struct ieee80211com *ic;
427 mtx_lock(&ic_list_mtx);
428 LIST_FOREACH(ic, &ic_head, ic_next)
429 if (strcmp(ic->ic_name, name) == 0)
431 mtx_unlock(&ic_list_mtx);
437 ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
439 struct ieee80211com *ic;
441 mtx_lock(&ic_list_mtx);
442 LIST_FOREACH(ic, &ic_head, ic_next)
444 mtx_unlock(&ic_list_mtx);
448 * Default reset method for use with the ioctl support. This
449 * method is invoked after any state change in the 802.11
450 * layer that should be propagated to the hardware but not
451 * require re-initialization of the 802.11 state machine (e.g
452 * rescanning for an ap). We always return ENETRESET which
453 * should cause the driver to re-initialize the device. Drivers
454 * can override this method to implement more optimized support.
457 default_reset(struct ieee80211vap *vap, u_long cmd)
463 * Default for updating the VAP default TX key index.
465 * Drivers that support TX offload as well as hardware encryption offload
466 * may need to be informed of key index changes separate from the key
470 default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
473 /* XXX assert validity */
474 /* XXX assert we're in a key update block */
475 vap->iv_def_txkey = kid;
479 * Add underlying device errors to vap errors.
482 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
484 struct ieee80211vap *vap = ifp->if_softc;
485 struct ieee80211com *ic = vap->iv_ic;
488 rv = if_get_counter_default(ifp, cnt);
490 case IFCOUNTER_OERRORS:
491 rv += counter_u64_fetch(ic->ic_oerrors);
493 case IFCOUNTER_IERRORS:
494 rv += counter_u64_fetch(ic->ic_ierrors);
504 * Prepare a vap for use. Drivers use this call to
505 * setup net80211 state in new vap's prior attaching
506 * them with ieee80211_vap_attach (below).
509 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
510 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
511 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
515 ifp = if_alloc(IFT_ETHER);
517 ic_printf(ic, "%s: unable to allocate ifnet\n",
521 if_initname(ifp, name, unit);
522 ifp->if_softc = vap; /* back pointer */
523 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
524 ifp->if_transmit = ieee80211_vap_transmit;
525 ifp->if_qflush = ieee80211_vap_qflush;
526 ifp->if_ioctl = ieee80211_ioctl;
527 ifp->if_init = ieee80211_init;
528 ifp->if_get_counter = ieee80211_get_counter;
532 vap->iv_flags = ic->ic_flags; /* propagate common flags */
533 vap->iv_flags_ext = ic->ic_flags_ext;
534 vap->iv_flags_ven = ic->ic_flags_ven;
535 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
537 /* 11n capabilities - XXX methodize */
538 vap->iv_htcaps = ic->ic_htcaps;
539 vap->iv_htextcaps = ic->ic_htextcaps;
541 /* 11ac capabilities - XXX methodize */
542 vap->iv_vhtcaps = ic->ic_vhtcaps;
543 vap->iv_vhtextcaps = ic->ic_vhtextcaps;
545 vap->iv_opmode = opmode;
546 vap->iv_caps |= ieee80211_opcap[opmode];
547 IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
549 case IEEE80211_M_WDS:
551 * WDS links must specify the bssid of the far end.
552 * For legacy operation this is a static relationship.
553 * For non-legacy operation the station must associate
554 * and be authorized to pass traffic. Plumbing the
555 * vap to the proper node happens when the vap
556 * transitions to RUN state.
558 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
559 vap->iv_flags |= IEEE80211_F_DESBSSID;
560 if (flags & IEEE80211_CLONE_WDSLEGACY)
561 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
563 #ifdef IEEE80211_SUPPORT_TDMA
564 case IEEE80211_M_AHDEMO:
565 if (flags & IEEE80211_CLONE_TDMA) {
566 /* NB: checked before clone operation allowed */
567 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
568 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
570 * Propagate TDMA capability to mark vap; this
571 * cannot be removed and is used to distinguish
572 * regular ahdemo operation from ahdemo+tdma.
574 vap->iv_caps |= IEEE80211_C_TDMA;
581 /* auto-enable s/w beacon miss support */
582 if (flags & IEEE80211_CLONE_NOBEACONS)
583 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
584 /* auto-generated or user supplied MAC address */
585 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
586 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
588 * Enable various functionality by default if we're
589 * capable; the driver can override us if it knows better.
591 if (vap->iv_caps & IEEE80211_C_WME)
592 vap->iv_flags |= IEEE80211_F_WME;
593 if (vap->iv_caps & IEEE80211_C_BURST)
594 vap->iv_flags |= IEEE80211_F_BURST;
595 /* NB: bg scanning only makes sense for station mode right now */
596 if (vap->iv_opmode == IEEE80211_M_STA &&
597 (vap->iv_caps & IEEE80211_C_BGSCAN))
598 vap->iv_flags |= IEEE80211_F_BGSCAN;
599 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
600 /* NB: DFS support only makes sense for ap mode right now */
601 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
602 (vap->iv_caps & IEEE80211_C_DFS))
603 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
605 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
606 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
607 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
609 * Install a default reset method for the ioctl support;
610 * the driver can override this.
612 vap->iv_reset = default_reset;
615 * Install a default crypto key update method, the driver
618 vap->iv_update_deftxkey = default_update_deftxkey;
620 ieee80211_sysctl_vattach(vap);
621 ieee80211_crypto_vattach(vap);
622 ieee80211_node_vattach(vap);
623 ieee80211_power_vattach(vap);
624 ieee80211_proto_vattach(vap);
625 #ifdef IEEE80211_SUPPORT_SUPERG
626 ieee80211_superg_vattach(vap);
628 ieee80211_ht_vattach(vap);
629 ieee80211_vht_vattach(vap);
630 ieee80211_scan_vattach(vap);
631 ieee80211_regdomain_vattach(vap);
632 ieee80211_radiotap_vattach(vap);
633 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
639 * Activate a vap. State should have been prepared with a
640 * call to ieee80211_vap_setup and by the driver. On return
641 * from this call the vap is ready for use.
644 ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
645 ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
647 struct ifnet *ifp = vap->iv_ifp;
648 struct ieee80211com *ic = vap->iv_ic;
649 struct ifmediareq imr;
652 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
653 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
654 __func__, ieee80211_opmode_name[vap->iv_opmode],
655 ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
658 * Do late attach work that cannot happen until after
659 * the driver has had a chance to override defaults.
661 ieee80211_node_latevattach(vap);
662 ieee80211_power_latevattach(vap);
664 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
665 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
666 ieee80211_media_status(ifp, &imr);
667 /* NB: strip explicit mode; we're actually in autoselect */
668 ifmedia_set(&vap->iv_media,
669 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
671 ifp->if_baudrate = IF_Mbps(maxrate);
673 ether_ifattach(ifp, macaddr);
674 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
675 /* hook output method setup by ether_ifattach */
676 vap->iv_output = ifp->if_output;
677 ifp->if_output = ieee80211_output;
678 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
681 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
682 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
683 #ifdef IEEE80211_SUPPORT_SUPERG
684 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
686 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
687 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
688 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
689 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
691 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
692 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
693 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
694 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
695 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
696 IEEE80211_UNLOCK(ic);
702 * Tear down vap state and reclaim the ifnet.
703 * The driver is assumed to have prepared for
704 * this; e.g. by turning off interrupts for the
708 ieee80211_vap_detach(struct ieee80211vap *vap)
710 struct ieee80211com *ic = vap->iv_ic;
711 struct ifnet *ifp = vap->iv_ifp;
713 CURVNET_SET(ifp->if_vnet);
715 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
716 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
718 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
724 * Flush any deferred vap tasks.
726 ieee80211_draintask(ic, &vap->iv_nstate_task);
727 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
729 /* XXX band-aid until ifnet handles this for us */
730 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
733 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
734 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
735 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
736 #ifdef IEEE80211_SUPPORT_SUPERG
737 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
739 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
740 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
741 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
742 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
744 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
745 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
746 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
747 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
748 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
750 /* NB: this handles the bpfdetach done below */
751 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
752 if (vap->iv_ifflags & IFF_PROMISC)
753 ieee80211_promisc(vap, false);
754 if (vap->iv_ifflags & IFF_ALLMULTI)
755 ieee80211_allmulti(vap, false);
756 IEEE80211_UNLOCK(ic);
758 ifmedia_removeall(&vap->iv_media);
760 ieee80211_radiotap_vdetach(vap);
761 ieee80211_regdomain_vdetach(vap);
762 ieee80211_scan_vdetach(vap);
763 #ifdef IEEE80211_SUPPORT_SUPERG
764 ieee80211_superg_vdetach(vap);
766 ieee80211_vht_vdetach(vap);
767 ieee80211_ht_vdetach(vap);
768 /* NB: must be before ieee80211_node_vdetach */
769 ieee80211_proto_vdetach(vap);
770 ieee80211_crypto_vdetach(vap);
771 ieee80211_power_vdetach(vap);
772 ieee80211_node_vdetach(vap);
773 ieee80211_sysctl_vdetach(vap);
781 * Count number of vaps in promisc, and issue promisc on
782 * parent respectively.
785 ieee80211_promisc(struct ieee80211vap *vap, bool on)
787 struct ieee80211com *ic = vap->iv_ic;
789 IEEE80211_LOCK_ASSERT(ic);
792 if (++ic->ic_promisc == 1)
793 ieee80211_runtask(ic, &ic->ic_promisc_task);
795 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
797 if (--ic->ic_promisc == 0)
798 ieee80211_runtask(ic, &ic->ic_promisc_task);
803 * Count number of vaps in allmulti, and issue allmulti on
804 * parent respectively.
807 ieee80211_allmulti(struct ieee80211vap *vap, bool on)
809 struct ieee80211com *ic = vap->iv_ic;
811 IEEE80211_LOCK_ASSERT(ic);
814 if (++ic->ic_allmulti == 1)
815 ieee80211_runtask(ic, &ic->ic_mcast_task);
817 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
819 if (--ic->ic_allmulti == 0)
820 ieee80211_runtask(ic, &ic->ic_mcast_task);
825 * Synchronize flag bit state in the com structure
826 * according to the state of all vap's. This is used,
827 * for example, to handle state changes via ioctls.
830 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
832 struct ieee80211vap *vap;
835 IEEE80211_LOCK_ASSERT(ic);
838 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
839 if (vap->iv_flags & flag) {
844 ic->ic_flags |= flag;
846 ic->ic_flags &= ~flag;
850 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
852 struct ieee80211com *ic = vap->iv_ic;
857 vap->iv_flags &= ~flag;
859 vap->iv_flags |= flag;
860 ieee80211_syncflag_locked(ic, flag);
861 IEEE80211_UNLOCK(ic);
865 * Synchronize flags_ht bit state in the com structure
866 * according to the state of all vap's. This is used,
867 * for example, to handle state changes via ioctls.
870 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
872 struct ieee80211vap *vap;
875 IEEE80211_LOCK_ASSERT(ic);
878 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
879 if (vap->iv_flags_ht & flag) {
884 ic->ic_flags_ht |= flag;
886 ic->ic_flags_ht &= ~flag;
890 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
892 struct ieee80211com *ic = vap->iv_ic;
897 vap->iv_flags_ht &= ~flag;
899 vap->iv_flags_ht |= flag;
900 ieee80211_syncflag_ht_locked(ic, flag);
901 IEEE80211_UNLOCK(ic);
905 * Synchronize flags_vht bit state in the com structure
906 * according to the state of all vap's. This is used,
907 * for example, to handle state changes via ioctls.
910 ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag)
912 struct ieee80211vap *vap;
915 IEEE80211_LOCK_ASSERT(ic);
918 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
919 if (vap->iv_flags_vht & flag) {
924 ic->ic_flags_vht |= flag;
926 ic->ic_flags_vht &= ~flag;
930 ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag)
932 struct ieee80211com *ic = vap->iv_ic;
937 vap->iv_flags_vht &= ~flag;
939 vap->iv_flags_vht |= flag;
940 ieee80211_syncflag_vht_locked(ic, flag);
941 IEEE80211_UNLOCK(ic);
945 * Synchronize flags_ext bit state in the com structure
946 * according to the state of all vap's. This is used,
947 * for example, to handle state changes via ioctls.
950 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
952 struct ieee80211vap *vap;
955 IEEE80211_LOCK_ASSERT(ic);
958 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
959 if (vap->iv_flags_ext & flag) {
964 ic->ic_flags_ext |= flag;
966 ic->ic_flags_ext &= ~flag;
970 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
972 struct ieee80211com *ic = vap->iv_ic;
977 vap->iv_flags_ext &= ~flag;
979 vap->iv_flags_ext |= flag;
980 ieee80211_syncflag_ext_locked(ic, flag);
981 IEEE80211_UNLOCK(ic);
985 mapgsm(u_int freq, u_int flags)
988 if (flags & IEEE80211_CHAN_QUARTER)
990 else if (flags & IEEE80211_CHAN_HALF)
994 /* NB: there is no 907/20 wide but leave room */
995 return (freq - 906*10) / 5;
999 mappsb(u_int freq, u_int flags)
1001 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
1005 * Convert MHz frequency to IEEE channel number.
1008 ieee80211_mhz2ieee(u_int freq, u_int flags)
1010 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
1011 if (flags & IEEE80211_CHAN_GSM)
1012 return mapgsm(freq, flags);
1013 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
1017 return ((int) freq - 2407) / 5;
1019 return 15 + ((freq - 2512) / 20);
1020 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
1022 /* XXX check regdomain? */
1023 if (IS_FREQ_IN_PSB(freq))
1024 return mappsb(freq, flags);
1025 return (freq - 4000) / 5;
1027 return (freq - 5000) / 5;
1028 } else { /* either, guess */
1032 if (907 <= freq && freq <= 922)
1033 return mapgsm(freq, flags);
1034 return ((int) freq - 2407) / 5;
1037 if (IS_FREQ_IN_PSB(freq))
1038 return mappsb(freq, flags);
1039 else if (freq > 4900)
1040 return (freq - 4000) / 5;
1042 return 15 + ((freq - 2512) / 20);
1044 return (freq - 5000) / 5;
1046 #undef IS_FREQ_IN_PSB
1050 * Convert channel to IEEE channel number.
1053 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
1056 ic_printf(ic, "invalid channel (NULL)\n");
1059 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
1063 * Convert IEEE channel number to MHz frequency.
1066 ieee80211_ieee2mhz(u_int chan, u_int flags)
1068 if (flags & IEEE80211_CHAN_GSM)
1069 return 907 + 5 * (chan / 10);
1070 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
1074 return 2407 + chan*5;
1076 return 2512 + ((chan-15)*20);
1077 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
1078 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
1080 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
1082 return 5000 + (chan*5);
1083 } else { /* either, guess */
1084 /* XXX can't distinguish PSB+GSM channels */
1087 if (chan < 14) /* 0-13 */
1088 return 2407 + chan*5;
1089 if (chan < 27) /* 15-26 */
1090 return 2512 + ((chan-15)*20);
1091 return 5000 + (chan*5);
1095 static __inline void
1096 set_extchan(struct ieee80211_channel *c)
1100 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1101 * "the secondary channel number shall be 'N + [1,-1] * 4'
1103 if (c->ic_flags & IEEE80211_CHAN_HT40U)
1104 c->ic_extieee = c->ic_ieee + 4;
1105 else if (c->ic_flags & IEEE80211_CHAN_HT40D)
1106 c->ic_extieee = c->ic_ieee - 4;
1112 * Populate the freq1/freq2 fields as appropriate for VHT channels.
1114 * This for now uses a hard-coded list of 80MHz wide channels.
1116 * For HT20/HT40, freq1 just is the centre frequency of the 40MHz
1117 * wide channel we've already decided upon.
1119 * For VHT80 and VHT160, there are only a small number of fixed
1120 * 80/160MHz wide channels, so we just use those.
1122 * This is all likely very very wrong - both the regulatory code
1123 * and this code needs to ensure that all four channels are
1124 * available and valid before the VHT80 (and eight for VHT160) channel
1128 struct vht_chan_range {
1129 uint16_t freq_start;
1133 struct vht_chan_range vht80_chan_ranges[] = {
1144 set_vht_extchan(struct ieee80211_channel *c)
1148 if (! IEEE80211_IS_CHAN_VHT(c)) {
1152 if (IEEE80211_IS_CHAN_VHT20(c)) {
1153 c->ic_vht_ch_freq1 = c->ic_ieee;
1157 if (IEEE80211_IS_CHAN_VHT40(c)) {
1158 if (IEEE80211_IS_CHAN_HT40U(c))
1159 c->ic_vht_ch_freq1 = c->ic_ieee + 2;
1160 else if (IEEE80211_IS_CHAN_HT40D(c))
1161 c->ic_vht_ch_freq1 = c->ic_ieee - 2;
1167 if (IEEE80211_IS_CHAN_VHT80(c)) {
1168 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1169 if (c->ic_freq >= vht80_chan_ranges[i].freq_start &&
1170 c->ic_freq < vht80_chan_ranges[i].freq_end) {
1173 midpoint = vht80_chan_ranges[i].freq_start + 40;
1174 c->ic_vht_ch_freq1 =
1175 ieee80211_mhz2ieee(midpoint, c->ic_flags);
1176 c->ic_vht_ch_freq2 = 0;
1178 printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
1179 __func__, c->ic_ieee, c->ic_freq, midpoint,
1180 c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
1188 printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n",
1197 * Return whether the current channel could possibly be a part of
1200 * This doesn't check that the whole range is in the allowed list
1201 * according to regulatory.
1204 is_vht80_valid_freq(uint16_t freq)
1207 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
1208 if (freq >= vht80_chan_ranges[i].freq_start &&
1209 freq < vht80_chan_ranges[i].freq_end)
1216 addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1217 uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1219 struct ieee80211_channel *c;
1221 if (*nchans >= maxchans)
1225 printf("%s: %d: ieee=%d, freq=%d, flags=0x%08x\n",
1233 c = &chans[(*nchans)++];
1235 c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1236 c->ic_maxregpower = maxregpower;
1237 c->ic_maxpower = 2 * maxregpower;
1238 c->ic_flags = flags;
1239 c->ic_vht_ch_freq1 = 0;
1240 c->ic_vht_ch_freq2 = 0;
1248 copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1251 struct ieee80211_channel *c;
1253 KASSERT(*nchans > 0, ("channel list is empty\n"));
1255 if (*nchans >= maxchans)
1259 printf("%s: %d: flags=0x%08x\n",
1265 c = &chans[(*nchans)++];
1267 c->ic_flags = flags;
1268 c->ic_vht_ch_freq1 = 0;
1269 c->ic_vht_ch_freq2 = 0;
1280 getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1285 if (isset(bands, IEEE80211_MODE_11B))
1286 flags[nmodes++] = IEEE80211_CHAN_B;
1287 if (isset(bands, IEEE80211_MODE_11G))
1288 flags[nmodes++] = IEEE80211_CHAN_G;
1289 if (isset(bands, IEEE80211_MODE_11NG))
1290 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1292 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1293 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1299 getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1304 * the addchan_list function seems to expect the flags array to
1305 * be in channel width order, so the VHT bits are interspersed
1306 * as appropriate to maintain said order.
1308 * It also assumes HT40U is before HT40D.
1313 if (isset(bands, IEEE80211_MODE_11A))
1314 flags[nmodes++] = IEEE80211_CHAN_A;
1315 if (isset(bands, IEEE80211_MODE_11NA))
1316 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1317 if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1318 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
1319 IEEE80211_CHAN_VHT20;
1324 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1326 if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1327 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U
1328 | IEEE80211_CHAN_VHT40U;
1331 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1333 if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1334 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D
1335 | IEEE80211_CHAN_VHT40D;
1339 if (vht80 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1340 flags[nmodes++] = IEEE80211_CHAN_A |
1341 IEEE80211_CHAN_HT40U | IEEE80211_CHAN_VHT80;
1342 flags[nmodes++] = IEEE80211_CHAN_A |
1343 IEEE80211_CHAN_HT40D | IEEE80211_CHAN_VHT80;
1352 getflags(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
1356 if (isset(bands, IEEE80211_MODE_11A) ||
1357 isset(bands, IEEE80211_MODE_11NA) ||
1358 isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
1359 if (isset(bands, IEEE80211_MODE_11B) ||
1360 isset(bands, IEEE80211_MODE_11G) ||
1361 isset(bands, IEEE80211_MODE_11NG) ||
1362 isset(bands, IEEE80211_MODE_VHT_2GHZ))
1365 getflags_5ghz(bands, flags, ht40, vht80);
1367 getflags_2ghz(bands, flags, ht40);
1371 * Add one 20 MHz channel into specified channel list.
1375 ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1376 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1377 uint32_t chan_flags, const uint8_t bands[])
1379 uint32_t flags[IEEE80211_MODE_MAX];
1382 getflags(bands, flags, 0, 0);
1383 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1385 error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1386 flags[0] | chan_flags);
1387 for (i = 1; flags[i] != 0 && error == 0; i++) {
1388 error = copychan_prev(chans, maxchans, nchans,
1389 flags[i] | chan_flags);
1395 static struct ieee80211_channel *
1396 findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1399 struct ieee80211_channel *c;
1402 flags &= IEEE80211_CHAN_ALLTURBO;
1403 /* brute force search */
1404 for (i = 0; i < nchans; i++) {
1406 if (c->ic_freq == freq &&
1407 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1414 * Add 40 MHz channel pair into specified channel list.
1418 ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1419 int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1421 struct ieee80211_channel *cent, *extc;
1425 freq = ieee80211_ieee2mhz(ieee, flags);
1428 * Each entry defines an HT40 channel pair; find the
1429 * center channel, then the extension channel above.
1431 flags |= IEEE80211_CHAN_HT20;
1432 cent = findchannel(chans, *nchans, freq, flags);
1436 extc = findchannel(chans, *nchans, freq + 20, flags);
1440 flags &= ~IEEE80211_CHAN_HT;
1441 error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1442 maxregpower, flags | IEEE80211_CHAN_HT40U);
1446 error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1447 maxregpower, flags | IEEE80211_CHAN_HT40D);
1453 * Fetch the center frequency for the primary channel.
1456 ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
1459 return (c->ic_freq);
1463 * Fetch the center frequency for the primary BAND channel.
1465 * For 5, 10, 20MHz channels it'll be the normally configured channel
1468 * For 40MHz, 80MHz, 160Mhz channels it'll the the centre of the
1469 * wide channel, not the centre of the primary channel (that's ic_freq).
1471 * For 80+80MHz channels this will be the centre of the primary
1472 * 80MHz channel; the secondary 80MHz channel will be center_freq2().
1475 ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
1479 * VHT - use the pre-calculated centre frequency
1480 * of the given channel.
1482 if (IEEE80211_IS_CHAN_VHT(c))
1483 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags));
1485 if (IEEE80211_IS_CHAN_HT40U(c)) {
1486 return (c->ic_freq + 10);
1488 if (IEEE80211_IS_CHAN_HT40D(c)) {
1489 return (c->ic_freq - 10);
1492 return (c->ic_freq);
1496 * For now, no 80+80 support; it will likely always return 0.
1499 ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
1502 if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0))
1503 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags));
1509 * Adds channels into specified channel list (ieee[] array must be sorted).
1510 * Channels are already sorted.
1513 add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1514 const uint8_t ieee[], int nieee, uint32_t flags[])
1520 for (i = 0; i < nieee; i++) {
1521 freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1522 for (j = 0; flags[j] != 0; j++) {
1525 * + HT40 and VHT40 channels occur together, so
1526 * we need to be careful that we actually allow that.
1527 * + VHT80, VHT160 will coexist with HT40/VHT40, so
1528 * make sure it's not skipped because of the overlap
1529 * check used for (V)HT40.
1531 is_vht = !! (flags[j] & IEEE80211_CHAN_VHT);
1535 * XXX This is all very broken right now.
1536 * What we /should/ do is:
1538 * + check that the frequency is in the list of
1539 * allowed VHT80 ranges; and
1540 * + the other 3 channels in the list are actually
1543 if (is_vht && flags[j] & IEEE80211_CHAN_VHT80)
1544 if (! is_vht80_valid_freq(freq))
1550 * This is also a fall through from VHT80; as we only
1551 * allow a VHT80 channel if the VHT40 combination is
1552 * also valid. If the VHT40 form is not valid then
1553 * we certainly can't do VHT80..
1555 if (flags[j] & IEEE80211_CHAN_HT40D)
1557 * Can't have a "lower" channel if we are the
1560 * Can't have a "lower" channel if it's below/
1561 * within 20MHz of the first channel.
1563 * Can't have a "lower" channel if the channel
1564 * below it is not 20MHz away.
1566 if (i == 0 || ieee[i] < ieee[0] + 4 ||
1568 ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1570 if (flags[j] & IEEE80211_CHAN_HT40U)
1572 * Can't have an "upper" channel if we are
1575 * Can't have an "upper" channel be above the
1576 * last channel in the list.
1578 * Can't have an "upper" channel if the next
1579 * channel according to the math isn't 20MHz
1580 * away. (Likely for channel 13/14.)
1582 if (i == nieee - 1 ||
1583 ieee[i] + 4 > ieee[nieee - 1] ||
1585 ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1589 error = addchan(chans, maxchans, nchans,
1590 ieee[i], freq, 0, flags[j]);
1592 error = copychan_prev(chans, maxchans, nchans,
1604 ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1605 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1608 uint32_t flags[IEEE80211_MODE_MAX];
1610 /* XXX no VHT for now */
1611 getflags_2ghz(bands, flags, ht40);
1612 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1614 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1618 ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1619 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1622 uint32_t flags[IEEE80211_MODE_MAX];
1626 * For now, assume VHT == VHT80 support as a minimum.
1628 if (isset(bands, IEEE80211_MODE_VHT_5GHZ))
1631 getflags_5ghz(bands, flags, ht40, vht80);
1632 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1634 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1638 * Locate a channel given a frequency+flags. We cache
1639 * the previous lookup to optimize switching between two
1640 * channels--as happens with dynamic turbo.
1642 struct ieee80211_channel *
1643 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1645 struct ieee80211_channel *c;
1647 flags &= IEEE80211_CHAN_ALLTURBO;
1648 c = ic->ic_prevchan;
1649 if (c != NULL && c->ic_freq == freq &&
1650 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1652 /* brute force search */
1653 return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
1657 * Locate a channel given a channel number+flags. We cache
1658 * the previous lookup to optimize switching between two
1659 * channels--as happens with dynamic turbo.
1661 struct ieee80211_channel *
1662 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1664 struct ieee80211_channel *c;
1667 flags &= IEEE80211_CHAN_ALLTURBO;
1668 c = ic->ic_prevchan;
1669 if (c != NULL && c->ic_ieee == ieee &&
1670 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1672 /* brute force search */
1673 for (i = 0; i < ic->ic_nchans; i++) {
1674 c = &ic->ic_channels[i];
1675 if (c->ic_ieee == ieee &&
1676 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1683 * Lookup a channel suitable for the given rx status.
1685 * This is used to find a channel for a frame (eg beacon, probe
1686 * response) based purely on the received PHY information.
1688 * For now it tries to do it based on R_FREQ / R_IEEE.
1689 * This is enough for 11bg and 11a (and thus 11ng/11na)
1690 * but it will not be enough for GSM, PSB channels and the
1691 * like. It also doesn't know about legacy-turbog and
1692 * legacy-turbo modes, which some offload NICs actually
1693 * support in weird ways.
1695 * Takes the ic and rxstatus; returns the channel or NULL
1698 * XXX TODO: Add support for that when the need arises.
1700 struct ieee80211_channel *
1701 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1702 const struct ieee80211_rx_stats *rxs)
1704 struct ieee80211com *ic = vap->iv_ic;
1706 struct ieee80211_channel *c;
1712 * Strictly speaking we only use freq for now,
1713 * however later on we may wish to just store
1714 * the ieee for verification.
1716 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1718 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1722 * If the rx status contains a valid ieee/freq, then
1723 * ensure we populate the correct channel information
1724 * in rxchan before passing it up to the scan infrastructure.
1725 * Offload NICs will pass up beacons from all channels
1726 * during background scans.
1729 /* Determine a band */
1730 /* XXX should be done by the driver? */
1731 if (rxs->c_freq < 3000) {
1732 flags = IEEE80211_CHAN_G;
1734 flags = IEEE80211_CHAN_A;
1737 /* Channel lookup */
1738 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1740 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1741 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1752 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1754 #define ADD(_ic, _s, _o) \
1755 ifmedia_add(media, \
1756 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1757 static const u_int mopts[IEEE80211_MODE_MAX] = {
1758 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1759 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1760 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1761 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1762 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1763 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1764 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1765 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1766 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1767 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1768 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1769 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1770 [IEEE80211_MODE_VHT_2GHZ] = IFM_IEEE80211_VHT2G,
1771 [IEEE80211_MODE_VHT_5GHZ] = IFM_IEEE80211_VHT5G,
1777 ADD(ic, mword, mopt); /* STA mode has no cap */
1778 if (caps & IEEE80211_C_IBSS)
1779 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1780 if (caps & IEEE80211_C_HOSTAP)
1781 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1782 if (caps & IEEE80211_C_AHDEMO)
1783 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1784 if (caps & IEEE80211_C_MONITOR)
1785 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1786 if (caps & IEEE80211_C_WDS)
1787 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1788 if (caps & IEEE80211_C_MBSS)
1789 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1794 * Setup the media data structures according to the channel and
1798 ieee80211_media_setup(struct ieee80211com *ic,
1799 struct ifmedia *media, int caps, int addsta,
1800 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1802 int i, j, rate, maxrate, mword, r;
1803 enum ieee80211_phymode mode;
1804 const struct ieee80211_rateset *rs;
1805 struct ieee80211_rateset allrates;
1808 * Fill in media characteristics.
1810 ifmedia_init(media, 0, media_change, media_stat);
1813 * Add media for legacy operating modes.
1815 memset(&allrates, 0, sizeof(allrates));
1816 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1817 if (isclr(ic->ic_modecaps, mode))
1819 addmedia(media, caps, addsta, mode, IFM_AUTO);
1820 if (mode == IEEE80211_MODE_AUTO)
1822 rs = &ic->ic_sup_rates[mode];
1823 for (i = 0; i < rs->rs_nrates; i++) {
1824 rate = rs->rs_rates[i];
1825 mword = ieee80211_rate2media(ic, rate, mode);
1828 addmedia(media, caps, addsta, mode, mword);
1830 * Add legacy rate to the collection of all rates.
1832 r = rate & IEEE80211_RATE_VAL;
1833 for (j = 0; j < allrates.rs_nrates; j++)
1834 if (allrates.rs_rates[j] == r)
1836 if (j == allrates.rs_nrates) {
1837 /* unique, add to the set */
1838 allrates.rs_rates[j] = r;
1839 allrates.rs_nrates++;
1841 rate = (rate & IEEE80211_RATE_VAL) / 2;
1846 for (i = 0; i < allrates.rs_nrates; i++) {
1847 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1848 IEEE80211_MODE_AUTO);
1851 /* NB: remove media options from mword */
1852 addmedia(media, caps, addsta,
1853 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1856 * Add HT/11n media. Note that we do not have enough
1857 * bits in the media subtype to express the MCS so we
1858 * use a "placeholder" media subtype and any fixed MCS
1859 * must be specified with a different mechanism.
1861 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1862 if (isclr(ic->ic_modecaps, mode))
1864 addmedia(media, caps, addsta, mode, IFM_AUTO);
1865 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1867 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1868 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1869 addmedia(media, caps, addsta,
1870 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1871 i = ic->ic_txstream * 8 - 1;
1872 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1873 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1874 rate = ieee80211_htrates[i].ht40_rate_400ns;
1875 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1876 rate = ieee80211_htrates[i].ht40_rate_800ns;
1877 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1878 rate = ieee80211_htrates[i].ht20_rate_400ns;
1880 rate = ieee80211_htrates[i].ht20_rate_800ns;
1888 for (; mode <= IEEE80211_MODE_VHT_5GHZ; mode++) {
1889 if (isclr(ic->ic_modecaps, mode))
1891 addmedia(media, caps, addsta, mode, IFM_AUTO);
1892 addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT);
1894 /* XXX TODO: VHT maxrate */
1900 /* XXX inline or eliminate? */
1901 const struct ieee80211_rateset *
1902 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1904 /* XXX does this work for 11ng basic rates? */
1905 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1909 ieee80211_announce(struct ieee80211com *ic)
1912 enum ieee80211_phymode mode;
1913 const struct ieee80211_rateset *rs;
1915 /* NB: skip AUTO since it has no rates */
1916 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1917 if (isclr(ic->ic_modecaps, mode))
1919 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1920 rs = &ic->ic_sup_rates[mode];
1921 for (i = 0; i < rs->rs_nrates; i++) {
1922 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1925 rate = ieee80211_media2rate(mword);
1926 printf("%s%d%sMbps", (i != 0 ? " " : ""),
1927 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1931 ieee80211_ht_announce(ic);
1932 ieee80211_vht_announce(ic);
1936 ieee80211_announce_channels(struct ieee80211com *ic)
1938 const struct ieee80211_channel *c;
1942 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1943 for (i = 0; i < ic->ic_nchans; i++) {
1944 c = &ic->ic_channels[i];
1945 if (IEEE80211_IS_CHAN_ST(c))
1947 else if (IEEE80211_IS_CHAN_108A(c))
1949 else if (IEEE80211_IS_CHAN_108G(c))
1951 else if (IEEE80211_IS_CHAN_HT(c))
1953 else if (IEEE80211_IS_CHAN_A(c))
1955 else if (IEEE80211_IS_CHAN_ANYG(c))
1957 else if (IEEE80211_IS_CHAN_B(c))
1961 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1963 else if (IEEE80211_IS_CHAN_HALF(c))
1965 else if (IEEE80211_IS_CHAN_QUARTER(c))
1969 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1970 , c->ic_ieee, c->ic_freq, type
1972 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1973 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1975 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1976 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1982 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1984 switch (IFM_MODE(ime->ifm_media)) {
1985 case IFM_IEEE80211_11A:
1986 *mode = IEEE80211_MODE_11A;
1988 case IFM_IEEE80211_11B:
1989 *mode = IEEE80211_MODE_11B;
1991 case IFM_IEEE80211_11G:
1992 *mode = IEEE80211_MODE_11G;
1994 case IFM_IEEE80211_FH:
1995 *mode = IEEE80211_MODE_FH;
1997 case IFM_IEEE80211_11NA:
1998 *mode = IEEE80211_MODE_11NA;
2000 case IFM_IEEE80211_11NG:
2001 *mode = IEEE80211_MODE_11NG;
2004 *mode = IEEE80211_MODE_AUTO;
2010 * Turbo mode is an ``option''.
2011 * XXX does not apply to AUTO
2013 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
2014 if (*mode == IEEE80211_MODE_11A) {
2015 if (flags & IEEE80211_F_TURBOP)
2016 *mode = IEEE80211_MODE_TURBO_A;
2018 *mode = IEEE80211_MODE_STURBO_A;
2019 } else if (*mode == IEEE80211_MODE_11G)
2020 *mode = IEEE80211_MODE_TURBO_G;
2029 * Handle a media change request on the vap interface.
2032 ieee80211_media_change(struct ifnet *ifp)
2034 struct ieee80211vap *vap = ifp->if_softc;
2035 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
2038 if (!media2mode(ime, vap->iv_flags, &newmode))
2040 if (vap->iv_des_mode != newmode) {
2041 vap->iv_des_mode = newmode;
2042 /* XXX kick state machine if up+running */
2048 * Common code to calculate the media status word
2049 * from the operating mode and channel state.
2052 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
2056 status = IFM_IEEE80211;
2058 case IEEE80211_M_STA:
2060 case IEEE80211_M_IBSS:
2061 status |= IFM_IEEE80211_ADHOC;
2063 case IEEE80211_M_HOSTAP:
2064 status |= IFM_IEEE80211_HOSTAP;
2066 case IEEE80211_M_MONITOR:
2067 status |= IFM_IEEE80211_MONITOR;
2069 case IEEE80211_M_AHDEMO:
2070 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
2072 case IEEE80211_M_WDS:
2073 status |= IFM_IEEE80211_WDS;
2075 case IEEE80211_M_MBSS:
2076 status |= IFM_IEEE80211_MBSS;
2079 if (IEEE80211_IS_CHAN_HTA(chan)) {
2080 status |= IFM_IEEE80211_11NA;
2081 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
2082 status |= IFM_IEEE80211_11NG;
2083 } else if (IEEE80211_IS_CHAN_A(chan)) {
2084 status |= IFM_IEEE80211_11A;
2085 } else if (IEEE80211_IS_CHAN_B(chan)) {
2086 status |= IFM_IEEE80211_11B;
2087 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
2088 status |= IFM_IEEE80211_11G;
2089 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
2090 status |= IFM_IEEE80211_FH;
2092 /* XXX else complain? */
2094 if (IEEE80211_IS_CHAN_TURBO(chan))
2095 status |= IFM_IEEE80211_TURBO;
2097 if (IEEE80211_IS_CHAN_HT20(chan))
2098 status |= IFM_IEEE80211_HT20;
2099 if (IEEE80211_IS_CHAN_HT40(chan))
2100 status |= IFM_IEEE80211_HT40;
2106 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2108 struct ieee80211vap *vap = ifp->if_softc;
2109 struct ieee80211com *ic = vap->iv_ic;
2110 enum ieee80211_phymode mode;
2112 imr->ifm_status = IFM_AVALID;
2114 * NB: use the current channel's mode to lock down a xmit
2115 * rate only when running; otherwise we may have a mismatch
2116 * in which case the rate will not be convertible.
2118 if (vap->iv_state == IEEE80211_S_RUN ||
2119 vap->iv_state == IEEE80211_S_SLEEP) {
2120 imr->ifm_status |= IFM_ACTIVE;
2121 mode = ieee80211_chan2mode(ic->ic_curchan);
2123 mode = IEEE80211_MODE_AUTO;
2124 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
2126 * Calculate a current rate if possible.
2128 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
2130 * A fixed rate is set, report that.
2132 imr->ifm_active |= ieee80211_rate2media(ic,
2133 vap->iv_txparms[mode].ucastrate, mode);
2134 } else if (vap->iv_opmode == IEEE80211_M_STA) {
2136 * In station mode report the current transmit rate.
2138 imr->ifm_active |= ieee80211_rate2media(ic,
2139 vap->iv_bss->ni_txrate, mode);
2141 imr->ifm_active |= IFM_AUTO;
2142 if (imr->ifm_status & IFM_ACTIVE)
2143 imr->ifm_current = imr->ifm_active;
2147 * Set the current phy mode and recalculate the active channel
2148 * set based on the available channels for this mode. Also
2149 * select a new default/current channel if the current one is
2150 * inappropriate for this mode.
2153 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
2156 * Adjust basic rates in 11b/11g supported rate set.
2157 * Note that if operating on a hal/quarter rate channel
2158 * this is a noop as those rates sets are different
2161 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
2162 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
2164 ic->ic_curmode = mode;
2165 ieee80211_reset_erp(ic); /* reset ERP state */
2171 * Return the phy mode for with the specified channel.
2173 enum ieee80211_phymode
2174 ieee80211_chan2mode(const struct ieee80211_channel *chan)
2177 if (IEEE80211_IS_CHAN_VHT_2GHZ(chan))
2178 return IEEE80211_MODE_VHT_2GHZ;
2179 else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan))
2180 return IEEE80211_MODE_VHT_5GHZ;
2181 else if (IEEE80211_IS_CHAN_HTA(chan))
2182 return IEEE80211_MODE_11NA;
2183 else if (IEEE80211_IS_CHAN_HTG(chan))
2184 return IEEE80211_MODE_11NG;
2185 else if (IEEE80211_IS_CHAN_108G(chan))
2186 return IEEE80211_MODE_TURBO_G;
2187 else if (IEEE80211_IS_CHAN_ST(chan))
2188 return IEEE80211_MODE_STURBO_A;
2189 else if (IEEE80211_IS_CHAN_TURBO(chan))
2190 return IEEE80211_MODE_TURBO_A;
2191 else if (IEEE80211_IS_CHAN_HALF(chan))
2192 return IEEE80211_MODE_HALF;
2193 else if (IEEE80211_IS_CHAN_QUARTER(chan))
2194 return IEEE80211_MODE_QUARTER;
2195 else if (IEEE80211_IS_CHAN_A(chan))
2196 return IEEE80211_MODE_11A;
2197 else if (IEEE80211_IS_CHAN_ANYG(chan))
2198 return IEEE80211_MODE_11G;
2199 else if (IEEE80211_IS_CHAN_B(chan))
2200 return IEEE80211_MODE_11B;
2201 else if (IEEE80211_IS_CHAN_FHSS(chan))
2202 return IEEE80211_MODE_FH;
2204 /* NB: should not get here */
2205 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
2206 __func__, chan->ic_freq, chan->ic_flags);
2207 return IEEE80211_MODE_11B;
2211 u_int match; /* rate + mode */
2212 u_int media; /* if_media rate */
2216 findmedia(const struct ratemedia rates[], int n, u_int match)
2220 for (i = 0; i < n; i++)
2221 if (rates[i].match == match)
2222 return rates[i].media;
2227 * Convert IEEE80211 rate value to ifmedia subtype.
2228 * Rate is either a legacy rate in units of 0.5Mbps
2232 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
2234 static const struct ratemedia rates[] = {
2235 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
2236 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
2237 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
2238 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
2239 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
2240 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
2241 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
2242 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
2243 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
2244 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
2245 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
2246 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
2247 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
2248 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
2249 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
2250 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
2251 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
2252 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
2253 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
2254 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
2255 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
2256 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
2257 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
2258 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
2259 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
2260 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
2261 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
2262 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
2263 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
2264 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
2265 /* NB: OFDM72 doesn't really exist so we don't handle it */
2267 static const struct ratemedia htrates[] = {
2268 { 0, IFM_IEEE80211_MCS },
2269 { 1, IFM_IEEE80211_MCS },
2270 { 2, IFM_IEEE80211_MCS },
2271 { 3, IFM_IEEE80211_MCS },
2272 { 4, IFM_IEEE80211_MCS },
2273 { 5, IFM_IEEE80211_MCS },
2274 { 6, IFM_IEEE80211_MCS },
2275 { 7, IFM_IEEE80211_MCS },
2276 { 8, IFM_IEEE80211_MCS },
2277 { 9, IFM_IEEE80211_MCS },
2278 { 10, IFM_IEEE80211_MCS },
2279 { 11, IFM_IEEE80211_MCS },
2280 { 12, IFM_IEEE80211_MCS },
2281 { 13, IFM_IEEE80211_MCS },
2282 { 14, IFM_IEEE80211_MCS },
2283 { 15, IFM_IEEE80211_MCS },
2284 { 16, IFM_IEEE80211_MCS },
2285 { 17, IFM_IEEE80211_MCS },
2286 { 18, IFM_IEEE80211_MCS },
2287 { 19, IFM_IEEE80211_MCS },
2288 { 20, IFM_IEEE80211_MCS },
2289 { 21, IFM_IEEE80211_MCS },
2290 { 22, IFM_IEEE80211_MCS },
2291 { 23, IFM_IEEE80211_MCS },
2292 { 24, IFM_IEEE80211_MCS },
2293 { 25, IFM_IEEE80211_MCS },
2294 { 26, IFM_IEEE80211_MCS },
2295 { 27, IFM_IEEE80211_MCS },
2296 { 28, IFM_IEEE80211_MCS },
2297 { 29, IFM_IEEE80211_MCS },
2298 { 30, IFM_IEEE80211_MCS },
2299 { 31, IFM_IEEE80211_MCS },
2300 { 32, IFM_IEEE80211_MCS },
2301 { 33, IFM_IEEE80211_MCS },
2302 { 34, IFM_IEEE80211_MCS },
2303 { 35, IFM_IEEE80211_MCS },
2304 { 36, IFM_IEEE80211_MCS },
2305 { 37, IFM_IEEE80211_MCS },
2306 { 38, IFM_IEEE80211_MCS },
2307 { 39, IFM_IEEE80211_MCS },
2308 { 40, IFM_IEEE80211_MCS },
2309 { 41, IFM_IEEE80211_MCS },
2310 { 42, IFM_IEEE80211_MCS },
2311 { 43, IFM_IEEE80211_MCS },
2312 { 44, IFM_IEEE80211_MCS },
2313 { 45, IFM_IEEE80211_MCS },
2314 { 46, IFM_IEEE80211_MCS },
2315 { 47, IFM_IEEE80211_MCS },
2316 { 48, IFM_IEEE80211_MCS },
2317 { 49, IFM_IEEE80211_MCS },
2318 { 50, IFM_IEEE80211_MCS },
2319 { 51, IFM_IEEE80211_MCS },
2320 { 52, IFM_IEEE80211_MCS },
2321 { 53, IFM_IEEE80211_MCS },
2322 { 54, IFM_IEEE80211_MCS },
2323 { 55, IFM_IEEE80211_MCS },
2324 { 56, IFM_IEEE80211_MCS },
2325 { 57, IFM_IEEE80211_MCS },
2326 { 58, IFM_IEEE80211_MCS },
2327 { 59, IFM_IEEE80211_MCS },
2328 { 60, IFM_IEEE80211_MCS },
2329 { 61, IFM_IEEE80211_MCS },
2330 { 62, IFM_IEEE80211_MCS },
2331 { 63, IFM_IEEE80211_MCS },
2332 { 64, IFM_IEEE80211_MCS },
2333 { 65, IFM_IEEE80211_MCS },
2334 { 66, IFM_IEEE80211_MCS },
2335 { 67, IFM_IEEE80211_MCS },
2336 { 68, IFM_IEEE80211_MCS },
2337 { 69, IFM_IEEE80211_MCS },
2338 { 70, IFM_IEEE80211_MCS },
2339 { 71, IFM_IEEE80211_MCS },
2340 { 72, IFM_IEEE80211_MCS },
2341 { 73, IFM_IEEE80211_MCS },
2342 { 74, IFM_IEEE80211_MCS },
2343 { 75, IFM_IEEE80211_MCS },
2344 { 76, IFM_IEEE80211_MCS },
2349 * Check 11n rates first for match as an MCS.
2351 if (mode == IEEE80211_MODE_11NA) {
2352 if (rate & IEEE80211_RATE_MCS) {
2353 rate &= ~IEEE80211_RATE_MCS;
2354 m = findmedia(htrates, nitems(htrates), rate);
2356 return m | IFM_IEEE80211_11NA;
2358 } else if (mode == IEEE80211_MODE_11NG) {
2359 /* NB: 12 is ambiguous, it will be treated as an MCS */
2360 if (rate & IEEE80211_RATE_MCS) {
2361 rate &= ~IEEE80211_RATE_MCS;
2362 m = findmedia(htrates, nitems(htrates), rate);
2364 return m | IFM_IEEE80211_11NG;
2367 rate &= IEEE80211_RATE_VAL;
2369 case IEEE80211_MODE_11A:
2370 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
2371 case IEEE80211_MODE_QUARTER:
2372 case IEEE80211_MODE_11NA:
2373 case IEEE80211_MODE_TURBO_A:
2374 case IEEE80211_MODE_STURBO_A:
2375 return findmedia(rates, nitems(rates),
2376 rate | IFM_IEEE80211_11A);
2377 case IEEE80211_MODE_11B:
2378 return findmedia(rates, nitems(rates),
2379 rate | IFM_IEEE80211_11B);
2380 case IEEE80211_MODE_FH:
2381 return findmedia(rates, nitems(rates),
2382 rate | IFM_IEEE80211_FH);
2383 case IEEE80211_MODE_AUTO:
2384 /* NB: ic may be NULL for some drivers */
2385 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
2386 return findmedia(rates, nitems(rates),
2387 rate | IFM_IEEE80211_FH);
2388 /* NB: hack, 11g matches both 11b+11a rates */
2390 case IEEE80211_MODE_11G:
2391 case IEEE80211_MODE_11NG:
2392 case IEEE80211_MODE_TURBO_G:
2393 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
2394 case IEEE80211_MODE_VHT_2GHZ:
2395 case IEEE80211_MODE_VHT_5GHZ:
2396 /* XXX TODO: need to figure out mapping for VHT rates */
2403 ieee80211_media2rate(int mword)
2405 static const int ieeerates[] = {
2409 2, /* IFM_IEEE80211_FH1 */
2410 4, /* IFM_IEEE80211_FH2 */
2411 2, /* IFM_IEEE80211_DS1 */
2412 4, /* IFM_IEEE80211_DS2 */
2413 11, /* IFM_IEEE80211_DS5 */
2414 22, /* IFM_IEEE80211_DS11 */
2415 44, /* IFM_IEEE80211_DS22 */
2416 12, /* IFM_IEEE80211_OFDM6 */
2417 18, /* IFM_IEEE80211_OFDM9 */
2418 24, /* IFM_IEEE80211_OFDM12 */
2419 36, /* IFM_IEEE80211_OFDM18 */
2420 48, /* IFM_IEEE80211_OFDM24 */
2421 72, /* IFM_IEEE80211_OFDM36 */
2422 96, /* IFM_IEEE80211_OFDM48 */
2423 108, /* IFM_IEEE80211_OFDM54 */
2424 144, /* IFM_IEEE80211_OFDM72 */
2425 0, /* IFM_IEEE80211_DS354k */
2426 0, /* IFM_IEEE80211_DS512k */
2427 6, /* IFM_IEEE80211_OFDM3 */
2428 9, /* IFM_IEEE80211_OFDM4 */
2429 54, /* IFM_IEEE80211_OFDM27 */
2430 -1, /* IFM_IEEE80211_MCS */
2431 -1, /* IFM_IEEE80211_VHT */
2433 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2434 ieeerates[IFM_SUBTYPE(mword)] : 0;
2438 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2439 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2441 #define mix(a, b, c) \
2443 a -= b; a -= c; a ^= (c >> 13); \
2444 b -= c; b -= a; b ^= (a << 8); \
2445 c -= a; c -= b; c ^= (b >> 13); \
2446 a -= b; a -= c; a ^= (c >> 12); \
2447 b -= c; b -= a; b ^= (a << 16); \
2448 c -= a; c -= b; c ^= (b >> 5); \
2449 a -= b; a -= c; a ^= (c >> 3); \
2450 b -= c; b -= a; b ^= (a << 10); \
2451 c -= a; c -= b; c ^= (b >> 15); \
2452 } while (/*CONSTCOND*/0)
2455 ieee80211_mac_hash(const struct ieee80211com *ic,
2456 const uint8_t addr[IEEE80211_ADDR_LEN])
2458 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2474 ieee80211_channel_type_char(const struct ieee80211_channel *c)
2476 if (IEEE80211_IS_CHAN_ST(c))
2478 if (IEEE80211_IS_CHAN_108A(c))
2480 if (IEEE80211_IS_CHAN_108G(c))
2482 if (IEEE80211_IS_CHAN_VHT(c))
2484 if (IEEE80211_IS_CHAN_HT(c))
2486 if (IEEE80211_IS_CHAN_A(c))
2488 if (IEEE80211_IS_CHAN_ANYG(c))
2490 if (IEEE80211_IS_CHAN_B(c))