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>
60 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
61 [IEEE80211_MODE_AUTO] = "auto",
62 [IEEE80211_MODE_11A] = "11a",
63 [IEEE80211_MODE_11B] = "11b",
64 [IEEE80211_MODE_11G] = "11g",
65 [IEEE80211_MODE_FH] = "FH",
66 [IEEE80211_MODE_TURBO_A] = "turboA",
67 [IEEE80211_MODE_TURBO_G] = "turboG",
68 [IEEE80211_MODE_STURBO_A] = "sturboA",
69 [IEEE80211_MODE_HALF] = "half",
70 [IEEE80211_MODE_QUARTER] = "quarter",
71 [IEEE80211_MODE_11NA] = "11na",
72 [IEEE80211_MODE_11NG] = "11ng",
74 /* map ieee80211_opmode to the corresponding capability bit */
75 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
76 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
77 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
78 [IEEE80211_M_STA] = IEEE80211_C_STA,
79 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
80 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
81 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
82 #ifdef IEEE80211_SUPPORT_MESH
83 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
87 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
88 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
90 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
91 static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
92 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
93 static void ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag);
94 static int ieee80211_media_setup(struct ieee80211com *ic,
95 struct ifmedia *media, int caps, int addsta,
96 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
97 static int media_status(enum ieee80211_opmode,
98 const struct ieee80211_channel *);
99 static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
101 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
104 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
106 #define B(r) ((r) | IEEE80211_RATE_BASIC)
107 static const struct ieee80211_rateset ieee80211_rateset_11a =
108 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
109 static const struct ieee80211_rateset ieee80211_rateset_half =
110 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
111 static const struct ieee80211_rateset ieee80211_rateset_quarter =
112 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
113 static const struct ieee80211_rateset ieee80211_rateset_11b =
114 { 4, { B(2), B(4), B(11), B(22) } };
115 /* NB: OFDM rates are handled specially based on mode */
116 static const struct ieee80211_rateset ieee80211_rateset_11g =
117 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
121 * Fill in 802.11 available channel set, mark
122 * all available channels as active, and pick
123 * a default channel if not already specified.
126 ieee80211_chan_init(struct ieee80211com *ic)
128 #define DEFAULTRATES(m, def) do { \
129 if (ic->ic_sup_rates[m].rs_nrates == 0) \
130 ic->ic_sup_rates[m] = def; \
132 struct ieee80211_channel *c;
135 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
136 ("invalid number of channels specified: %u", ic->ic_nchans));
137 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
138 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
139 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
140 for (i = 0; i < ic->ic_nchans; i++) {
141 c = &ic->ic_channels[i];
142 KASSERT(c->ic_flags != 0, ("channel with no flags"));
144 * Help drivers that work only with frequencies by filling
145 * in IEEE channel #'s if not already calculated. Note this
146 * mimics similar work done in ieee80211_setregdomain when
147 * changing regulatory state.
150 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
151 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
152 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
153 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
155 /* default max tx power to max regulatory */
156 if (c->ic_maxpower == 0)
157 c->ic_maxpower = 2*c->ic_maxregpower;
158 setbit(ic->ic_chan_avail, c->ic_ieee);
160 * Identify mode capabilities.
162 if (IEEE80211_IS_CHAN_A(c))
163 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
164 if (IEEE80211_IS_CHAN_B(c))
165 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
166 if (IEEE80211_IS_CHAN_ANYG(c))
167 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
168 if (IEEE80211_IS_CHAN_FHSS(c))
169 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
170 if (IEEE80211_IS_CHAN_108A(c))
171 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
172 if (IEEE80211_IS_CHAN_108G(c))
173 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
174 if (IEEE80211_IS_CHAN_ST(c))
175 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
176 if (IEEE80211_IS_CHAN_HALF(c))
177 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
178 if (IEEE80211_IS_CHAN_QUARTER(c))
179 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
180 if (IEEE80211_IS_CHAN_HTA(c))
181 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
182 if (IEEE80211_IS_CHAN_HTG(c))
183 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
185 /* initialize candidate channels to all available */
186 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
187 sizeof(ic->ic_chan_avail));
189 /* sort channel table to allow lookup optimizations */
190 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
192 /* invalidate any previous state */
193 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
194 ic->ic_prevchan = NULL;
195 ic->ic_csa_newchan = NULL;
196 /* arbitrarily pick the first channel */
197 ic->ic_curchan = &ic->ic_channels[0];
198 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
200 /* fillin well-known rate sets if driver has not specified */
201 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
202 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
203 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
204 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
205 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
206 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
207 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
208 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
209 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
210 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
213 * Setup required information to fill the mcsset field, if driver did
214 * not. Assume a 2T2R setup for historic reasons.
216 if (ic->ic_rxstream == 0)
218 if (ic->ic_txstream == 0)
222 * Set auto mode to reset active channel state and any desired channel.
224 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
229 null_update_mcast(struct ieee80211com *ic)
232 ic_printf(ic, "need multicast update callback\n");
236 null_update_promisc(struct ieee80211com *ic)
239 ic_printf(ic, "need promiscuous mode update callback\n");
243 null_update_chw(struct ieee80211com *ic)
246 ic_printf(ic, "%s: need callback\n", __func__);
250 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
255 retval = printf("%s: ", ic->ic_name);
257 retval += vprintf(fmt, ap);
262 static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
263 static struct mtx ic_list_mtx;
264 MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
267 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
269 struct ieee80211com *ic;
274 error = sysctl_wire_old_buffer(req, 0);
277 sbuf_new_for_sysctl(&sb, NULL, 8, req);
278 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
280 mtx_lock(&ic_list_mtx);
281 LIST_FOREACH(ic, &ic_head, ic_next) {
282 sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
285 mtx_unlock(&ic_list_mtx);
286 error = sbuf_finish(&sb);
291 SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
292 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
293 sysctl_ieee80211coms, "A", "names of available 802.11 devices");
296 * Attach/setup the common net80211 state. Called by
297 * the driver on attach to prior to creating any vap's.
300 ieee80211_ifattach(struct ieee80211com *ic)
303 IEEE80211_LOCK_INIT(ic, ic->ic_name);
304 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
305 TAILQ_INIT(&ic->ic_vaps);
307 /* Create a taskqueue for all state changes */
308 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
309 taskqueue_thread_enqueue, &ic->ic_tq);
310 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
312 ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
313 ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
315 * Fill in 802.11 available channel set, mark all
316 * available channels as active, and pick a default
317 * channel if not already specified.
319 ieee80211_chan_init(ic);
321 ic->ic_update_mcast = null_update_mcast;
322 ic->ic_update_promisc = null_update_promisc;
323 ic->ic_update_chw = null_update_chw;
325 ic->ic_hash_key = arc4random();
326 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
327 ic->ic_lintval = ic->ic_bintval;
328 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
330 ieee80211_crypto_attach(ic);
331 ieee80211_node_attach(ic);
332 ieee80211_power_attach(ic);
333 ieee80211_proto_attach(ic);
334 #ifdef IEEE80211_SUPPORT_SUPERG
335 ieee80211_superg_attach(ic);
337 ieee80211_ht_attach(ic);
338 ieee80211_scan_attach(ic);
339 ieee80211_regdomain_attach(ic);
340 ieee80211_dfs_attach(ic);
342 ieee80211_sysctl_attach(ic);
344 mtx_lock(&ic_list_mtx);
345 LIST_INSERT_HEAD(&ic_head, ic, ic_next);
346 mtx_unlock(&ic_list_mtx);
350 * Detach net80211 state on device detach. Tear down
351 * all vap's and reclaim all common state prior to the
352 * device state going away. Note we may call back into
353 * driver; it must be prepared for this.
356 ieee80211_ifdetach(struct ieee80211com *ic)
358 struct ieee80211vap *vap;
360 mtx_lock(&ic_list_mtx);
361 LIST_REMOVE(ic, ic_next);
362 mtx_unlock(&ic_list_mtx);
364 taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
367 * The VAP is responsible for setting and clearing
368 * the VIMAGE context.
370 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
371 ieee80211_vap_destroy(vap);
372 ieee80211_waitfor_parent(ic);
374 ieee80211_sysctl_detach(ic);
375 ieee80211_dfs_detach(ic);
376 ieee80211_regdomain_detach(ic);
377 ieee80211_scan_detach(ic);
378 #ifdef IEEE80211_SUPPORT_SUPERG
379 ieee80211_superg_detach(ic);
381 ieee80211_ht_detach(ic);
382 /* NB: must be called before ieee80211_node_detach */
383 ieee80211_proto_detach(ic);
384 ieee80211_crypto_detach(ic);
385 ieee80211_power_detach(ic);
386 ieee80211_node_detach(ic);
388 counter_u64_free(ic->ic_ierrors);
389 counter_u64_free(ic->ic_oerrors);
391 taskqueue_free(ic->ic_tq);
392 IEEE80211_TX_LOCK_DESTROY(ic);
393 IEEE80211_LOCK_DESTROY(ic);
396 struct ieee80211com *
397 ieee80211_find_com(const char *name)
399 struct ieee80211com *ic;
401 mtx_lock(&ic_list_mtx);
402 LIST_FOREACH(ic, &ic_head, ic_next)
403 if (strcmp(ic->ic_name, name) == 0)
405 mtx_unlock(&ic_list_mtx);
411 ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
413 struct ieee80211com *ic;
415 mtx_lock(&ic_list_mtx);
416 LIST_FOREACH(ic, &ic_head, ic_next)
418 mtx_unlock(&ic_list_mtx);
422 * Default reset method for use with the ioctl support. This
423 * method is invoked after any state change in the 802.11
424 * layer that should be propagated to the hardware but not
425 * require re-initialization of the 802.11 state machine (e.g
426 * rescanning for an ap). We always return ENETRESET which
427 * should cause the driver to re-initialize the device. Drivers
428 * can override this method to implement more optimized support.
431 default_reset(struct ieee80211vap *vap, u_long cmd)
437 * Default for updating the VAP default TX key index.
439 * Drivers that support TX offload as well as hardware encryption offload
440 * may need to be informed of key index changes separate from the key
444 default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
447 /* XXX assert validity */
448 /* XXX assert we're in a key update block */
449 vap->iv_def_txkey = kid;
453 * Add underlying device errors to vap errors.
456 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
458 struct ieee80211vap *vap = ifp->if_softc;
459 struct ieee80211com *ic = vap->iv_ic;
462 rv = if_get_counter_default(ifp, cnt);
464 case IFCOUNTER_OERRORS:
465 rv += counter_u64_fetch(ic->ic_oerrors);
467 case IFCOUNTER_IERRORS:
468 rv += counter_u64_fetch(ic->ic_ierrors);
478 * Prepare a vap for use. Drivers use this call to
479 * setup net80211 state in new vap's prior attaching
480 * them with ieee80211_vap_attach (below).
483 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
484 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
485 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
489 ifp = if_alloc(IFT_ETHER);
491 ic_printf(ic, "%s: unable to allocate ifnet\n",
495 if_initname(ifp, name, unit);
496 ifp->if_softc = vap; /* back pointer */
497 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
498 ifp->if_transmit = ieee80211_vap_transmit;
499 ifp->if_qflush = ieee80211_vap_qflush;
500 ifp->if_ioctl = ieee80211_ioctl;
501 ifp->if_init = ieee80211_init;
502 ifp->if_get_counter = ieee80211_get_counter;
506 vap->iv_flags = ic->ic_flags; /* propagate common flags */
507 vap->iv_flags_ext = ic->ic_flags_ext;
508 vap->iv_flags_ven = ic->ic_flags_ven;
509 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
510 vap->iv_htcaps = ic->ic_htcaps;
511 vap->iv_htextcaps = ic->ic_htextcaps;
512 vap->iv_opmode = opmode;
513 vap->iv_caps |= ieee80211_opcap[opmode];
514 IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
516 case IEEE80211_M_WDS:
518 * WDS links must specify the bssid of the far end.
519 * For legacy operation this is a static relationship.
520 * For non-legacy operation the station must associate
521 * and be authorized to pass traffic. Plumbing the
522 * vap to the proper node happens when the vap
523 * transitions to RUN state.
525 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
526 vap->iv_flags |= IEEE80211_F_DESBSSID;
527 if (flags & IEEE80211_CLONE_WDSLEGACY)
528 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
530 #ifdef IEEE80211_SUPPORT_TDMA
531 case IEEE80211_M_AHDEMO:
532 if (flags & IEEE80211_CLONE_TDMA) {
533 /* NB: checked before clone operation allowed */
534 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
535 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
537 * Propagate TDMA capability to mark vap; this
538 * cannot be removed and is used to distinguish
539 * regular ahdemo operation from ahdemo+tdma.
541 vap->iv_caps |= IEEE80211_C_TDMA;
548 /* auto-enable s/w beacon miss support */
549 if (flags & IEEE80211_CLONE_NOBEACONS)
550 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
551 /* auto-generated or user supplied MAC address */
552 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
553 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
555 * Enable various functionality by default if we're
556 * capable; the driver can override us if it knows better.
558 if (vap->iv_caps & IEEE80211_C_WME)
559 vap->iv_flags |= IEEE80211_F_WME;
560 if (vap->iv_caps & IEEE80211_C_BURST)
561 vap->iv_flags |= IEEE80211_F_BURST;
562 /* NB: bg scanning only makes sense for station mode right now */
563 if (vap->iv_opmode == IEEE80211_M_STA &&
564 (vap->iv_caps & IEEE80211_C_BGSCAN))
565 vap->iv_flags |= IEEE80211_F_BGSCAN;
566 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
567 /* NB: DFS support only makes sense for ap mode right now */
568 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
569 (vap->iv_caps & IEEE80211_C_DFS))
570 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
572 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
573 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
574 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
576 * Install a default reset method for the ioctl support;
577 * the driver can override this.
579 vap->iv_reset = default_reset;
582 * Install a default crypto key update method, the driver
585 vap->iv_update_deftxkey = default_update_deftxkey;
587 ieee80211_sysctl_vattach(vap);
588 ieee80211_crypto_vattach(vap);
589 ieee80211_node_vattach(vap);
590 ieee80211_power_vattach(vap);
591 ieee80211_proto_vattach(vap);
592 #ifdef IEEE80211_SUPPORT_SUPERG
593 ieee80211_superg_vattach(vap);
595 ieee80211_ht_vattach(vap);
596 ieee80211_scan_vattach(vap);
597 ieee80211_regdomain_vattach(vap);
598 ieee80211_radiotap_vattach(vap);
599 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
605 * Activate a vap. State should have been prepared with a
606 * call to ieee80211_vap_setup and by the driver. On return
607 * from this call the vap is ready for use.
610 ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
611 ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
613 struct ifnet *ifp = vap->iv_ifp;
614 struct ieee80211com *ic = vap->iv_ic;
615 struct ifmediareq imr;
618 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
619 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
620 __func__, ieee80211_opmode_name[vap->iv_opmode],
621 ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
624 * Do late attach work that cannot happen until after
625 * the driver has had a chance to override defaults.
627 ieee80211_node_latevattach(vap);
628 ieee80211_power_latevattach(vap);
630 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
631 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
632 ieee80211_media_status(ifp, &imr);
633 /* NB: strip explicit mode; we're actually in autoselect */
634 ifmedia_set(&vap->iv_media,
635 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
637 ifp->if_baudrate = IF_Mbps(maxrate);
639 ether_ifattach(ifp, macaddr);
640 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
641 /* hook output method setup by ether_ifattach */
642 vap->iv_output = ifp->if_output;
643 ifp->if_output = ieee80211_output;
644 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
647 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
648 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
649 #ifdef IEEE80211_SUPPORT_SUPERG
650 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
652 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
653 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
654 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
655 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
657 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
658 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
659 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
660 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
661 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
662 IEEE80211_UNLOCK(ic);
668 * Tear down vap state and reclaim the ifnet.
669 * The driver is assumed to have prepared for
670 * this; e.g. by turning off interrupts for the
674 ieee80211_vap_detach(struct ieee80211vap *vap)
676 struct ieee80211com *ic = vap->iv_ic;
677 struct ifnet *ifp = vap->iv_ifp;
679 CURVNET_SET(ifp->if_vnet);
681 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
682 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
684 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
690 * Flush any deferred vap tasks.
692 ieee80211_draintask(ic, &vap->iv_nstate_task);
693 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
695 /* XXX band-aid until ifnet handles this for us */
696 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
699 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
700 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
701 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
702 #ifdef IEEE80211_SUPPORT_SUPERG
703 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
705 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
706 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
707 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
708 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
710 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
711 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
712 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
713 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
714 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
716 /* NB: this handles the bpfdetach done below */
717 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
718 if (vap->iv_ifflags & IFF_PROMISC)
719 ieee80211_promisc(vap, false);
720 if (vap->iv_ifflags & IFF_ALLMULTI)
721 ieee80211_allmulti(vap, false);
722 IEEE80211_UNLOCK(ic);
724 ifmedia_removeall(&vap->iv_media);
726 ieee80211_radiotap_vdetach(vap);
727 ieee80211_regdomain_vdetach(vap);
728 ieee80211_scan_vdetach(vap);
729 #ifdef IEEE80211_SUPPORT_SUPERG
730 ieee80211_superg_vdetach(vap);
732 ieee80211_ht_vdetach(vap);
733 /* NB: must be before ieee80211_node_vdetach */
734 ieee80211_proto_vdetach(vap);
735 ieee80211_crypto_vdetach(vap);
736 ieee80211_power_vdetach(vap);
737 ieee80211_node_vdetach(vap);
738 ieee80211_sysctl_vdetach(vap);
746 * Count number of vaps in promisc, and issue promisc on
747 * parent respectively.
750 ieee80211_promisc(struct ieee80211vap *vap, bool on)
752 struct ieee80211com *ic = vap->iv_ic;
754 IEEE80211_LOCK_ASSERT(ic);
757 if (++ic->ic_promisc == 1)
758 ieee80211_runtask(ic, &ic->ic_promisc_task);
760 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
762 if (--ic->ic_promisc == 0)
763 ieee80211_runtask(ic, &ic->ic_promisc_task);
768 * Count number of vaps in allmulti, and issue allmulti on
769 * parent respectively.
772 ieee80211_allmulti(struct ieee80211vap *vap, bool on)
774 struct ieee80211com *ic = vap->iv_ic;
776 IEEE80211_LOCK_ASSERT(ic);
779 if (++ic->ic_allmulti == 1)
780 ieee80211_runtask(ic, &ic->ic_mcast_task);
782 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
784 if (--ic->ic_allmulti == 0)
785 ieee80211_runtask(ic, &ic->ic_mcast_task);
790 * Synchronize flag bit state in the com structure
791 * according to the state of all vap's. This is used,
792 * for example, to handle state changes via ioctls.
795 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
797 struct ieee80211vap *vap;
800 IEEE80211_LOCK_ASSERT(ic);
803 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
804 if (vap->iv_flags & flag) {
809 ic->ic_flags |= flag;
811 ic->ic_flags &= ~flag;
815 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
817 struct ieee80211com *ic = vap->iv_ic;
822 vap->iv_flags &= ~flag;
824 vap->iv_flags |= flag;
825 ieee80211_syncflag_locked(ic, flag);
826 IEEE80211_UNLOCK(ic);
830 * Synchronize flags_ht bit state in the com structure
831 * according to the state of all vap's. This is used,
832 * for example, to handle state changes via ioctls.
835 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
837 struct ieee80211vap *vap;
840 IEEE80211_LOCK_ASSERT(ic);
843 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
844 if (vap->iv_flags_ht & flag) {
849 ic->ic_flags_ht |= flag;
851 ic->ic_flags_ht &= ~flag;
855 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
857 struct ieee80211com *ic = vap->iv_ic;
862 vap->iv_flags_ht &= ~flag;
864 vap->iv_flags_ht |= flag;
865 ieee80211_syncflag_ht_locked(ic, flag);
866 IEEE80211_UNLOCK(ic);
870 * Synchronize flags_vht bit state in the com structure
871 * according to the state of all vap's. This is used,
872 * for example, to handle state changes via ioctls.
875 ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag)
877 struct ieee80211vap *vap;
880 IEEE80211_LOCK_ASSERT(ic);
883 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
884 if (vap->iv_flags_vht & flag) {
889 ic->ic_flags_vht |= flag;
891 ic->ic_flags_vht &= ~flag;
895 ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag)
897 struct ieee80211com *ic = vap->iv_ic;
902 vap->iv_flags_vht &= ~flag;
904 vap->iv_flags_vht |= flag;
905 ieee80211_syncflag_vht_locked(ic, flag);
906 IEEE80211_UNLOCK(ic);
910 * Synchronize flags_ext bit state in the com structure
911 * according to the state of all vap's. This is used,
912 * for example, to handle state changes via ioctls.
915 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
917 struct ieee80211vap *vap;
920 IEEE80211_LOCK_ASSERT(ic);
923 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
924 if (vap->iv_flags_ext & flag) {
929 ic->ic_flags_ext |= flag;
931 ic->ic_flags_ext &= ~flag;
935 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
937 struct ieee80211com *ic = vap->iv_ic;
942 vap->iv_flags_ext &= ~flag;
944 vap->iv_flags_ext |= flag;
945 ieee80211_syncflag_ext_locked(ic, flag);
946 IEEE80211_UNLOCK(ic);
950 mapgsm(u_int freq, u_int flags)
953 if (flags & IEEE80211_CHAN_QUARTER)
955 else if (flags & IEEE80211_CHAN_HALF)
959 /* NB: there is no 907/20 wide but leave room */
960 return (freq - 906*10) / 5;
964 mappsb(u_int freq, u_int flags)
966 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
970 * Convert MHz frequency to IEEE channel number.
973 ieee80211_mhz2ieee(u_int freq, u_int flags)
975 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
976 if (flags & IEEE80211_CHAN_GSM)
977 return mapgsm(freq, flags);
978 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
982 return ((int) freq - 2407) / 5;
984 return 15 + ((freq - 2512) / 20);
985 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
987 /* XXX check regdomain? */
988 if (IS_FREQ_IN_PSB(freq))
989 return mappsb(freq, flags);
990 return (freq - 4000) / 5;
992 return (freq - 5000) / 5;
993 } else { /* either, guess */
997 if (907 <= freq && freq <= 922)
998 return mapgsm(freq, flags);
999 return ((int) freq - 2407) / 5;
1002 if (IS_FREQ_IN_PSB(freq))
1003 return mappsb(freq, flags);
1004 else if (freq > 4900)
1005 return (freq - 4000) / 5;
1007 return 15 + ((freq - 2512) / 20);
1009 return (freq - 5000) / 5;
1011 #undef IS_FREQ_IN_PSB
1015 * Convert channel to IEEE channel number.
1018 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
1021 ic_printf(ic, "invalid channel (NULL)\n");
1024 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
1028 * Convert IEEE channel number to MHz frequency.
1031 ieee80211_ieee2mhz(u_int chan, u_int flags)
1033 if (flags & IEEE80211_CHAN_GSM)
1034 return 907 + 5 * (chan / 10);
1035 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
1039 return 2407 + chan*5;
1041 return 2512 + ((chan-15)*20);
1042 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
1043 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
1045 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
1047 return 5000 + (chan*5);
1048 } else { /* either, guess */
1049 /* XXX can't distinguish PSB+GSM channels */
1052 if (chan < 14) /* 0-13 */
1053 return 2407 + chan*5;
1054 if (chan < 27) /* 15-26 */
1055 return 2512 + ((chan-15)*20);
1056 return 5000 + (chan*5);
1060 static __inline void
1061 set_extchan(struct ieee80211_channel *c)
1065 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1066 * "the secondary channel number shall be 'N + [1,-1] * 4'
1068 if (c->ic_flags & IEEE80211_CHAN_HT40U)
1069 c->ic_extieee = c->ic_ieee + 4;
1070 else if (c->ic_flags & IEEE80211_CHAN_HT40D)
1071 c->ic_extieee = c->ic_ieee - 4;
1077 addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1078 uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1080 struct ieee80211_channel *c;
1082 if (*nchans >= maxchans)
1085 c = &chans[(*nchans)++];
1087 c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1088 c->ic_maxregpower = maxregpower;
1089 c->ic_maxpower = 2 * maxregpower;
1090 c->ic_flags = flags;
1097 copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1100 struct ieee80211_channel *c;
1102 KASSERT(*nchans > 0, ("channel list is empty\n"));
1104 if (*nchans >= maxchans)
1107 c = &chans[(*nchans)++];
1109 c->ic_flags = flags;
1116 getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1121 if (isset(bands, IEEE80211_MODE_11B))
1122 flags[nmodes++] = IEEE80211_CHAN_B;
1123 if (isset(bands, IEEE80211_MODE_11G))
1124 flags[nmodes++] = IEEE80211_CHAN_G;
1125 if (isset(bands, IEEE80211_MODE_11NG))
1126 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1128 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1129 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1135 getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1140 if (isset(bands, IEEE80211_MODE_11A))
1141 flags[nmodes++] = IEEE80211_CHAN_A;
1142 if (isset(bands, IEEE80211_MODE_11NA))
1143 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1145 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1146 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1152 getflags(const uint8_t bands[], uint32_t flags[], int ht40)
1156 if (isset(bands, IEEE80211_MODE_11A) ||
1157 isset(bands, IEEE80211_MODE_11NA)) {
1158 if (isset(bands, IEEE80211_MODE_11B) ||
1159 isset(bands, IEEE80211_MODE_11G) ||
1160 isset(bands, IEEE80211_MODE_11NG))
1163 getflags_5ghz(bands, flags, ht40);
1165 getflags_2ghz(bands, flags, ht40);
1169 * Add one 20 MHz channel into specified channel list.
1172 ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1173 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1174 uint32_t chan_flags, const uint8_t bands[])
1176 uint32_t flags[IEEE80211_MODE_MAX];
1179 getflags(bands, flags, 0);
1180 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1182 error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1183 flags[0] | chan_flags);
1184 for (i = 1; flags[i] != 0 && error == 0; i++) {
1185 error = copychan_prev(chans, maxchans, nchans,
1186 flags[i] | chan_flags);
1192 static struct ieee80211_channel *
1193 findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1196 struct ieee80211_channel *c;
1199 flags &= IEEE80211_CHAN_ALLTURBO;
1200 /* brute force search */
1201 for (i = 0; i < nchans; i++) {
1203 if (c->ic_freq == freq &&
1204 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1211 * Add 40 MHz channel pair into specified channel list.
1214 ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1215 int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1217 struct ieee80211_channel *cent, *extc;
1221 freq = ieee80211_ieee2mhz(ieee, flags);
1224 * Each entry defines an HT40 channel pair; find the
1225 * center channel, then the extension channel above.
1227 flags |= IEEE80211_CHAN_HT20;
1228 cent = findchannel(chans, *nchans, freq, flags);
1232 extc = findchannel(chans, *nchans, freq + 20, flags);
1236 flags &= ~IEEE80211_CHAN_HT;
1237 error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1238 maxregpower, flags | IEEE80211_CHAN_HT40U);
1242 error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1243 maxregpower, flags | IEEE80211_CHAN_HT40D);
1249 * Fetch the center frequency for the primary channel.
1252 ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
1255 return (c->ic_freq);
1259 * Fetch the center frequency for the primary BAND channel.
1261 * For 5, 10, 20MHz channels it'll be the normally configured channel
1264 * For 40MHz, 80MHz, 160Mhz channels it'll the the centre of the
1265 * wide channel, not the centre of the primary channel (that's ic_freq).
1267 * For 80+80MHz channels this will be the centre of the primary
1268 * 80MHz channel; the secondary 80MHz channel will be center_freq2().
1272 ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
1275 if (IEEE80211_IS_CHAN_HT40U(c)) {
1276 return (c->ic_freq + 10);
1278 if (IEEE80211_IS_CHAN_HT40D(c)) {
1279 return (c->ic_freq - 10);
1282 return (c->ic_freq);
1286 * For now, no 80+80 support; this is zero.
1289 ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
1296 * Adds channels into specified channel list (ieee[] array must be sorted).
1297 * Channels are already sorted.
1300 add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1301 const uint8_t ieee[], int nieee, uint32_t flags[])
1306 for (i = 0; i < nieee; i++) {
1307 freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1308 for (j = 0; flags[j] != 0; j++) {
1309 if (flags[j] & IEEE80211_CHAN_HT40D)
1310 if (i == 0 || ieee[i] < ieee[0] + 4 ||
1312 ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1314 if (flags[j] & IEEE80211_CHAN_HT40U)
1315 if (i == nieee - 1 ||
1316 ieee[i] + 4 > ieee[nieee - 1] ||
1318 ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1322 error = addchan(chans, maxchans, nchans,
1323 ieee[i], freq, 0, flags[j]);
1325 error = copychan_prev(chans, maxchans, nchans,
1337 ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1338 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1341 uint32_t flags[IEEE80211_MODE_MAX];
1343 getflags_2ghz(bands, flags, ht40);
1344 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1346 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1350 ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1351 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1354 uint32_t flags[IEEE80211_MODE_MAX];
1356 getflags_5ghz(bands, flags, ht40);
1357 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1359 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1363 * Locate a channel given a frequency+flags. We cache
1364 * the previous lookup to optimize switching between two
1365 * channels--as happens with dynamic turbo.
1367 struct ieee80211_channel *
1368 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1370 struct ieee80211_channel *c;
1372 flags &= IEEE80211_CHAN_ALLTURBO;
1373 c = ic->ic_prevchan;
1374 if (c != NULL && c->ic_freq == freq &&
1375 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1377 /* brute force search */
1378 return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
1382 * Locate a channel given a channel number+flags. We cache
1383 * the previous lookup to optimize switching between two
1384 * channels--as happens with dynamic turbo.
1386 struct ieee80211_channel *
1387 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1389 struct ieee80211_channel *c;
1392 flags &= IEEE80211_CHAN_ALLTURBO;
1393 c = ic->ic_prevchan;
1394 if (c != NULL && c->ic_ieee == ieee &&
1395 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1397 /* brute force search */
1398 for (i = 0; i < ic->ic_nchans; i++) {
1399 c = &ic->ic_channels[i];
1400 if (c->ic_ieee == ieee &&
1401 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1408 * Lookup a channel suitable for the given rx status.
1410 * This is used to find a channel for a frame (eg beacon, probe
1411 * response) based purely on the received PHY information.
1413 * For now it tries to do it based on R_FREQ / R_IEEE.
1414 * This is enough for 11bg and 11a (and thus 11ng/11na)
1415 * but it will not be enough for GSM, PSB channels and the
1416 * like. It also doesn't know about legacy-turbog and
1417 * legacy-turbo modes, which some offload NICs actually
1418 * support in weird ways.
1420 * Takes the ic and rxstatus; returns the channel or NULL
1423 * XXX TODO: Add support for that when the need arises.
1425 struct ieee80211_channel *
1426 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1427 const struct ieee80211_rx_stats *rxs)
1429 struct ieee80211com *ic = vap->iv_ic;
1431 struct ieee80211_channel *c;
1437 * Strictly speaking we only use freq for now,
1438 * however later on we may wish to just store
1439 * the ieee for verification.
1441 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1443 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1447 * If the rx status contains a valid ieee/freq, then
1448 * ensure we populate the correct channel information
1449 * in rxchan before passing it up to the scan infrastructure.
1450 * Offload NICs will pass up beacons from all channels
1451 * during background scans.
1454 /* Determine a band */
1455 /* XXX should be done by the driver? */
1456 if (rxs->c_freq < 3000) {
1457 flags = IEEE80211_CHAN_G;
1459 flags = IEEE80211_CHAN_A;
1462 /* Channel lookup */
1463 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1465 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1466 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1477 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1479 #define ADD(_ic, _s, _o) \
1480 ifmedia_add(media, \
1481 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1482 static const u_int mopts[IEEE80211_MODE_MAX] = {
1483 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1484 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1485 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1486 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1487 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1488 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1489 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1490 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1491 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1492 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1493 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1494 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1500 ADD(ic, mword, mopt); /* STA mode has no cap */
1501 if (caps & IEEE80211_C_IBSS)
1502 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1503 if (caps & IEEE80211_C_HOSTAP)
1504 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1505 if (caps & IEEE80211_C_AHDEMO)
1506 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1507 if (caps & IEEE80211_C_MONITOR)
1508 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1509 if (caps & IEEE80211_C_WDS)
1510 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1511 if (caps & IEEE80211_C_MBSS)
1512 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1517 * Setup the media data structures according to the channel and
1521 ieee80211_media_setup(struct ieee80211com *ic,
1522 struct ifmedia *media, int caps, int addsta,
1523 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1525 int i, j, rate, maxrate, mword, r;
1526 enum ieee80211_phymode mode;
1527 const struct ieee80211_rateset *rs;
1528 struct ieee80211_rateset allrates;
1531 * Fill in media characteristics.
1533 ifmedia_init(media, 0, media_change, media_stat);
1536 * Add media for legacy operating modes.
1538 memset(&allrates, 0, sizeof(allrates));
1539 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1540 if (isclr(ic->ic_modecaps, mode))
1542 addmedia(media, caps, addsta, mode, IFM_AUTO);
1543 if (mode == IEEE80211_MODE_AUTO)
1545 rs = &ic->ic_sup_rates[mode];
1546 for (i = 0; i < rs->rs_nrates; i++) {
1547 rate = rs->rs_rates[i];
1548 mword = ieee80211_rate2media(ic, rate, mode);
1551 addmedia(media, caps, addsta, mode, mword);
1553 * Add legacy rate to the collection of all rates.
1555 r = rate & IEEE80211_RATE_VAL;
1556 for (j = 0; j < allrates.rs_nrates; j++)
1557 if (allrates.rs_rates[j] == r)
1559 if (j == allrates.rs_nrates) {
1560 /* unique, add to the set */
1561 allrates.rs_rates[j] = r;
1562 allrates.rs_nrates++;
1564 rate = (rate & IEEE80211_RATE_VAL) / 2;
1569 for (i = 0; i < allrates.rs_nrates; i++) {
1570 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1571 IEEE80211_MODE_AUTO);
1574 /* NB: remove media options from mword */
1575 addmedia(media, caps, addsta,
1576 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1579 * Add HT/11n media. Note that we do not have enough
1580 * bits in the media subtype to express the MCS so we
1581 * use a "placeholder" media subtype and any fixed MCS
1582 * must be specified with a different mechanism.
1584 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1585 if (isclr(ic->ic_modecaps, mode))
1587 addmedia(media, caps, addsta, mode, IFM_AUTO);
1588 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1590 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1591 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1592 addmedia(media, caps, addsta,
1593 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1594 i = ic->ic_txstream * 8 - 1;
1595 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1596 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1597 rate = ieee80211_htrates[i].ht40_rate_400ns;
1598 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1599 rate = ieee80211_htrates[i].ht40_rate_800ns;
1600 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1601 rate = ieee80211_htrates[i].ht20_rate_400ns;
1603 rate = ieee80211_htrates[i].ht20_rate_800ns;
1610 /* XXX inline or eliminate? */
1611 const struct ieee80211_rateset *
1612 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1614 /* XXX does this work for 11ng basic rates? */
1615 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1619 ieee80211_announce(struct ieee80211com *ic)
1622 enum ieee80211_phymode mode;
1623 const struct ieee80211_rateset *rs;
1625 /* NB: skip AUTO since it has no rates */
1626 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1627 if (isclr(ic->ic_modecaps, mode))
1629 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1630 rs = &ic->ic_sup_rates[mode];
1631 for (i = 0; i < rs->rs_nrates; i++) {
1632 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1635 rate = ieee80211_media2rate(mword);
1636 printf("%s%d%sMbps", (i != 0 ? " " : ""),
1637 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1641 ieee80211_ht_announce(ic);
1645 ieee80211_announce_channels(struct ieee80211com *ic)
1647 const struct ieee80211_channel *c;
1651 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1652 for (i = 0; i < ic->ic_nchans; i++) {
1653 c = &ic->ic_channels[i];
1654 if (IEEE80211_IS_CHAN_ST(c))
1656 else if (IEEE80211_IS_CHAN_108A(c))
1658 else if (IEEE80211_IS_CHAN_108G(c))
1660 else if (IEEE80211_IS_CHAN_HT(c))
1662 else if (IEEE80211_IS_CHAN_A(c))
1664 else if (IEEE80211_IS_CHAN_ANYG(c))
1666 else if (IEEE80211_IS_CHAN_B(c))
1670 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1672 else if (IEEE80211_IS_CHAN_HALF(c))
1674 else if (IEEE80211_IS_CHAN_QUARTER(c))
1678 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1679 , c->ic_ieee, c->ic_freq, type
1681 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1682 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1684 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1685 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1691 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1693 switch (IFM_MODE(ime->ifm_media)) {
1694 case IFM_IEEE80211_11A:
1695 *mode = IEEE80211_MODE_11A;
1697 case IFM_IEEE80211_11B:
1698 *mode = IEEE80211_MODE_11B;
1700 case IFM_IEEE80211_11G:
1701 *mode = IEEE80211_MODE_11G;
1703 case IFM_IEEE80211_FH:
1704 *mode = IEEE80211_MODE_FH;
1706 case IFM_IEEE80211_11NA:
1707 *mode = IEEE80211_MODE_11NA;
1709 case IFM_IEEE80211_11NG:
1710 *mode = IEEE80211_MODE_11NG;
1713 *mode = IEEE80211_MODE_AUTO;
1719 * Turbo mode is an ``option''.
1720 * XXX does not apply to AUTO
1722 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1723 if (*mode == IEEE80211_MODE_11A) {
1724 if (flags & IEEE80211_F_TURBOP)
1725 *mode = IEEE80211_MODE_TURBO_A;
1727 *mode = IEEE80211_MODE_STURBO_A;
1728 } else if (*mode == IEEE80211_MODE_11G)
1729 *mode = IEEE80211_MODE_TURBO_G;
1738 * Handle a media change request on the vap interface.
1741 ieee80211_media_change(struct ifnet *ifp)
1743 struct ieee80211vap *vap = ifp->if_softc;
1744 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1747 if (!media2mode(ime, vap->iv_flags, &newmode))
1749 if (vap->iv_des_mode != newmode) {
1750 vap->iv_des_mode = newmode;
1751 /* XXX kick state machine if up+running */
1757 * Common code to calculate the media status word
1758 * from the operating mode and channel state.
1761 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1765 status = IFM_IEEE80211;
1767 case IEEE80211_M_STA:
1769 case IEEE80211_M_IBSS:
1770 status |= IFM_IEEE80211_ADHOC;
1772 case IEEE80211_M_HOSTAP:
1773 status |= IFM_IEEE80211_HOSTAP;
1775 case IEEE80211_M_MONITOR:
1776 status |= IFM_IEEE80211_MONITOR;
1778 case IEEE80211_M_AHDEMO:
1779 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1781 case IEEE80211_M_WDS:
1782 status |= IFM_IEEE80211_WDS;
1784 case IEEE80211_M_MBSS:
1785 status |= IFM_IEEE80211_MBSS;
1788 if (IEEE80211_IS_CHAN_HTA(chan)) {
1789 status |= IFM_IEEE80211_11NA;
1790 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1791 status |= IFM_IEEE80211_11NG;
1792 } else if (IEEE80211_IS_CHAN_A(chan)) {
1793 status |= IFM_IEEE80211_11A;
1794 } else if (IEEE80211_IS_CHAN_B(chan)) {
1795 status |= IFM_IEEE80211_11B;
1796 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1797 status |= IFM_IEEE80211_11G;
1798 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1799 status |= IFM_IEEE80211_FH;
1801 /* XXX else complain? */
1803 if (IEEE80211_IS_CHAN_TURBO(chan))
1804 status |= IFM_IEEE80211_TURBO;
1806 if (IEEE80211_IS_CHAN_HT20(chan))
1807 status |= IFM_IEEE80211_HT20;
1808 if (IEEE80211_IS_CHAN_HT40(chan))
1809 status |= IFM_IEEE80211_HT40;
1815 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1817 struct ieee80211vap *vap = ifp->if_softc;
1818 struct ieee80211com *ic = vap->iv_ic;
1819 enum ieee80211_phymode mode;
1821 imr->ifm_status = IFM_AVALID;
1823 * NB: use the current channel's mode to lock down a xmit
1824 * rate only when running; otherwise we may have a mismatch
1825 * in which case the rate will not be convertible.
1827 if (vap->iv_state == IEEE80211_S_RUN ||
1828 vap->iv_state == IEEE80211_S_SLEEP) {
1829 imr->ifm_status |= IFM_ACTIVE;
1830 mode = ieee80211_chan2mode(ic->ic_curchan);
1832 mode = IEEE80211_MODE_AUTO;
1833 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1835 * Calculate a current rate if possible.
1837 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1839 * A fixed rate is set, report that.
1841 imr->ifm_active |= ieee80211_rate2media(ic,
1842 vap->iv_txparms[mode].ucastrate, mode);
1843 } else if (vap->iv_opmode == IEEE80211_M_STA) {
1845 * In station mode report the current transmit rate.
1847 imr->ifm_active |= ieee80211_rate2media(ic,
1848 vap->iv_bss->ni_txrate, mode);
1850 imr->ifm_active |= IFM_AUTO;
1851 if (imr->ifm_status & IFM_ACTIVE)
1852 imr->ifm_current = imr->ifm_active;
1856 * Set the current phy mode and recalculate the active channel
1857 * set based on the available channels for this mode. Also
1858 * select a new default/current channel if the current one is
1859 * inappropriate for this mode.
1862 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1865 * Adjust basic rates in 11b/11g supported rate set.
1866 * Note that if operating on a hal/quarter rate channel
1867 * this is a noop as those rates sets are different
1870 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1871 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1873 ic->ic_curmode = mode;
1874 ieee80211_reset_erp(ic); /* reset ERP state */
1880 * Return the phy mode for with the specified channel.
1882 enum ieee80211_phymode
1883 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1886 if (IEEE80211_IS_CHAN_HTA(chan))
1887 return IEEE80211_MODE_11NA;
1888 else if (IEEE80211_IS_CHAN_HTG(chan))
1889 return IEEE80211_MODE_11NG;
1890 else if (IEEE80211_IS_CHAN_108G(chan))
1891 return IEEE80211_MODE_TURBO_G;
1892 else if (IEEE80211_IS_CHAN_ST(chan))
1893 return IEEE80211_MODE_STURBO_A;
1894 else if (IEEE80211_IS_CHAN_TURBO(chan))
1895 return IEEE80211_MODE_TURBO_A;
1896 else if (IEEE80211_IS_CHAN_HALF(chan))
1897 return IEEE80211_MODE_HALF;
1898 else if (IEEE80211_IS_CHAN_QUARTER(chan))
1899 return IEEE80211_MODE_QUARTER;
1900 else if (IEEE80211_IS_CHAN_A(chan))
1901 return IEEE80211_MODE_11A;
1902 else if (IEEE80211_IS_CHAN_ANYG(chan))
1903 return IEEE80211_MODE_11G;
1904 else if (IEEE80211_IS_CHAN_B(chan))
1905 return IEEE80211_MODE_11B;
1906 else if (IEEE80211_IS_CHAN_FHSS(chan))
1907 return IEEE80211_MODE_FH;
1909 /* NB: should not get here */
1910 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1911 __func__, chan->ic_freq, chan->ic_flags);
1912 return IEEE80211_MODE_11B;
1916 u_int match; /* rate + mode */
1917 u_int media; /* if_media rate */
1921 findmedia(const struct ratemedia rates[], int n, u_int match)
1925 for (i = 0; i < n; i++)
1926 if (rates[i].match == match)
1927 return rates[i].media;
1932 * Convert IEEE80211 rate value to ifmedia subtype.
1933 * Rate is either a legacy rate in units of 0.5Mbps
1937 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1939 static const struct ratemedia rates[] = {
1940 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1941 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1942 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1943 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1944 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1945 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1946 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1947 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1948 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1949 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1950 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1951 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1952 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1953 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1954 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1955 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1956 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1957 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1958 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1959 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1960 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1961 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1962 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1963 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1964 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1965 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1966 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1967 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1968 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1969 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1970 /* NB: OFDM72 doesn't really exist so we don't handle it */
1972 static const struct ratemedia htrates[] = {
1973 { 0, IFM_IEEE80211_MCS },
1974 { 1, IFM_IEEE80211_MCS },
1975 { 2, IFM_IEEE80211_MCS },
1976 { 3, IFM_IEEE80211_MCS },
1977 { 4, IFM_IEEE80211_MCS },
1978 { 5, IFM_IEEE80211_MCS },
1979 { 6, IFM_IEEE80211_MCS },
1980 { 7, IFM_IEEE80211_MCS },
1981 { 8, IFM_IEEE80211_MCS },
1982 { 9, IFM_IEEE80211_MCS },
1983 { 10, IFM_IEEE80211_MCS },
1984 { 11, IFM_IEEE80211_MCS },
1985 { 12, IFM_IEEE80211_MCS },
1986 { 13, IFM_IEEE80211_MCS },
1987 { 14, IFM_IEEE80211_MCS },
1988 { 15, IFM_IEEE80211_MCS },
1989 { 16, IFM_IEEE80211_MCS },
1990 { 17, IFM_IEEE80211_MCS },
1991 { 18, IFM_IEEE80211_MCS },
1992 { 19, IFM_IEEE80211_MCS },
1993 { 20, IFM_IEEE80211_MCS },
1994 { 21, IFM_IEEE80211_MCS },
1995 { 22, IFM_IEEE80211_MCS },
1996 { 23, IFM_IEEE80211_MCS },
1997 { 24, IFM_IEEE80211_MCS },
1998 { 25, IFM_IEEE80211_MCS },
1999 { 26, IFM_IEEE80211_MCS },
2000 { 27, IFM_IEEE80211_MCS },
2001 { 28, IFM_IEEE80211_MCS },
2002 { 29, IFM_IEEE80211_MCS },
2003 { 30, IFM_IEEE80211_MCS },
2004 { 31, IFM_IEEE80211_MCS },
2005 { 32, IFM_IEEE80211_MCS },
2006 { 33, IFM_IEEE80211_MCS },
2007 { 34, IFM_IEEE80211_MCS },
2008 { 35, IFM_IEEE80211_MCS },
2009 { 36, IFM_IEEE80211_MCS },
2010 { 37, IFM_IEEE80211_MCS },
2011 { 38, IFM_IEEE80211_MCS },
2012 { 39, IFM_IEEE80211_MCS },
2013 { 40, IFM_IEEE80211_MCS },
2014 { 41, IFM_IEEE80211_MCS },
2015 { 42, IFM_IEEE80211_MCS },
2016 { 43, IFM_IEEE80211_MCS },
2017 { 44, IFM_IEEE80211_MCS },
2018 { 45, IFM_IEEE80211_MCS },
2019 { 46, IFM_IEEE80211_MCS },
2020 { 47, IFM_IEEE80211_MCS },
2021 { 48, IFM_IEEE80211_MCS },
2022 { 49, IFM_IEEE80211_MCS },
2023 { 50, IFM_IEEE80211_MCS },
2024 { 51, IFM_IEEE80211_MCS },
2025 { 52, IFM_IEEE80211_MCS },
2026 { 53, IFM_IEEE80211_MCS },
2027 { 54, IFM_IEEE80211_MCS },
2028 { 55, IFM_IEEE80211_MCS },
2029 { 56, IFM_IEEE80211_MCS },
2030 { 57, IFM_IEEE80211_MCS },
2031 { 58, IFM_IEEE80211_MCS },
2032 { 59, IFM_IEEE80211_MCS },
2033 { 60, IFM_IEEE80211_MCS },
2034 { 61, IFM_IEEE80211_MCS },
2035 { 62, IFM_IEEE80211_MCS },
2036 { 63, IFM_IEEE80211_MCS },
2037 { 64, IFM_IEEE80211_MCS },
2038 { 65, IFM_IEEE80211_MCS },
2039 { 66, IFM_IEEE80211_MCS },
2040 { 67, IFM_IEEE80211_MCS },
2041 { 68, IFM_IEEE80211_MCS },
2042 { 69, IFM_IEEE80211_MCS },
2043 { 70, IFM_IEEE80211_MCS },
2044 { 71, IFM_IEEE80211_MCS },
2045 { 72, IFM_IEEE80211_MCS },
2046 { 73, IFM_IEEE80211_MCS },
2047 { 74, IFM_IEEE80211_MCS },
2048 { 75, IFM_IEEE80211_MCS },
2049 { 76, IFM_IEEE80211_MCS },
2054 * Check 11n rates first for match as an MCS.
2056 if (mode == IEEE80211_MODE_11NA) {
2057 if (rate & IEEE80211_RATE_MCS) {
2058 rate &= ~IEEE80211_RATE_MCS;
2059 m = findmedia(htrates, nitems(htrates), rate);
2061 return m | IFM_IEEE80211_11NA;
2063 } else if (mode == IEEE80211_MODE_11NG) {
2064 /* NB: 12 is ambiguous, it will be treated as an MCS */
2065 if (rate & IEEE80211_RATE_MCS) {
2066 rate &= ~IEEE80211_RATE_MCS;
2067 m = findmedia(htrates, nitems(htrates), rate);
2069 return m | IFM_IEEE80211_11NG;
2072 rate &= IEEE80211_RATE_VAL;
2074 case IEEE80211_MODE_11A:
2075 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
2076 case IEEE80211_MODE_QUARTER:
2077 case IEEE80211_MODE_11NA:
2078 case IEEE80211_MODE_TURBO_A:
2079 case IEEE80211_MODE_STURBO_A:
2080 return findmedia(rates, nitems(rates),
2081 rate | IFM_IEEE80211_11A);
2082 case IEEE80211_MODE_11B:
2083 return findmedia(rates, nitems(rates),
2084 rate | IFM_IEEE80211_11B);
2085 case IEEE80211_MODE_FH:
2086 return findmedia(rates, nitems(rates),
2087 rate | IFM_IEEE80211_FH);
2088 case IEEE80211_MODE_AUTO:
2089 /* NB: ic may be NULL for some drivers */
2090 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
2091 return findmedia(rates, nitems(rates),
2092 rate | IFM_IEEE80211_FH);
2093 /* NB: hack, 11g matches both 11b+11a rates */
2095 case IEEE80211_MODE_11G:
2096 case IEEE80211_MODE_11NG:
2097 case IEEE80211_MODE_TURBO_G:
2098 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
2099 case IEEE80211_MODE_VHT_2GHZ:
2100 case IEEE80211_MODE_VHT_5GHZ:
2101 /* XXX TODO: need to figure out mapping for VHT rates */
2108 ieee80211_media2rate(int mword)
2110 static const int ieeerates[] = {
2114 2, /* IFM_IEEE80211_FH1 */
2115 4, /* IFM_IEEE80211_FH2 */
2116 2, /* IFM_IEEE80211_DS1 */
2117 4, /* IFM_IEEE80211_DS2 */
2118 11, /* IFM_IEEE80211_DS5 */
2119 22, /* IFM_IEEE80211_DS11 */
2120 44, /* IFM_IEEE80211_DS22 */
2121 12, /* IFM_IEEE80211_OFDM6 */
2122 18, /* IFM_IEEE80211_OFDM9 */
2123 24, /* IFM_IEEE80211_OFDM12 */
2124 36, /* IFM_IEEE80211_OFDM18 */
2125 48, /* IFM_IEEE80211_OFDM24 */
2126 72, /* IFM_IEEE80211_OFDM36 */
2127 96, /* IFM_IEEE80211_OFDM48 */
2128 108, /* IFM_IEEE80211_OFDM54 */
2129 144, /* IFM_IEEE80211_OFDM72 */
2130 0, /* IFM_IEEE80211_DS354k */
2131 0, /* IFM_IEEE80211_DS512k */
2132 6, /* IFM_IEEE80211_OFDM3 */
2133 9, /* IFM_IEEE80211_OFDM4 */
2134 54, /* IFM_IEEE80211_OFDM27 */
2135 -1, /* IFM_IEEE80211_MCS */
2136 -1, /* IFM_IEEE80211_VHT */
2138 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2139 ieeerates[IFM_SUBTYPE(mword)] : 0;
2143 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2144 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2146 #define mix(a, b, c) \
2148 a -= b; a -= c; a ^= (c >> 13); \
2149 b -= c; b -= a; b ^= (a << 8); \
2150 c -= a; c -= b; c ^= (b >> 13); \
2151 a -= b; a -= c; a ^= (c >> 12); \
2152 b -= c; b -= a; b ^= (a << 16); \
2153 c -= a; c -= b; c ^= (b >> 5); \
2154 a -= b; a -= c; a ^= (c >> 3); \
2155 b -= c; b -= a; b ^= (a << 10); \
2156 c -= a; c -= b; c ^= (b >> 15); \
2157 } while (/*CONSTCOND*/0)
2160 ieee80211_mac_hash(const struct ieee80211com *ic,
2161 const uint8_t addr[IEEE80211_ADDR_LEN])
2163 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2179 ieee80211_channel_type_char(const struct ieee80211_channel *c)
2181 if (IEEE80211_IS_CHAN_ST(c))
2183 if (IEEE80211_IS_CHAN_108A(c))
2185 if (IEEE80211_IS_CHAN_108G(c))
2187 if (IEEE80211_IS_CHAN_VHT(c))
2189 if (IEEE80211_IS_CHAN_HT(c))
2191 if (IEEE80211_IS_CHAN_A(c))
2193 if (IEEE80211_IS_CHAN_ANYG(c))
2195 if (IEEE80211_IS_CHAN_B(c))