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/socket.h>
41 #include <machine/stdarg.h>
44 #include <net/if_var.h>
45 #include <net/if_dl.h>
46 #include <net/if_media.h>
47 #include <net/if_types.h>
48 #include <net/ethernet.h>
50 #include <net80211/ieee80211_var.h>
51 #include <net80211/ieee80211_regdomain.h>
52 #ifdef IEEE80211_SUPPORT_SUPERG
53 #include <net80211/ieee80211_superg.h>
55 #include <net80211/ieee80211_ratectl.h>
59 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
60 [IEEE80211_MODE_AUTO] = "auto",
61 [IEEE80211_MODE_11A] = "11a",
62 [IEEE80211_MODE_11B] = "11b",
63 [IEEE80211_MODE_11G] = "11g",
64 [IEEE80211_MODE_FH] = "FH",
65 [IEEE80211_MODE_TURBO_A] = "turboA",
66 [IEEE80211_MODE_TURBO_G] = "turboG",
67 [IEEE80211_MODE_STURBO_A] = "sturboA",
68 [IEEE80211_MODE_HALF] = "half",
69 [IEEE80211_MODE_QUARTER] = "quarter",
70 [IEEE80211_MODE_11NA] = "11na",
71 [IEEE80211_MODE_11NG] = "11ng",
73 /* map ieee80211_opmode to the corresponding capability bit */
74 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
75 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
76 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
77 [IEEE80211_M_STA] = IEEE80211_C_STA,
78 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
79 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
80 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
81 #ifdef IEEE80211_SUPPORT_MESH
82 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
86 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
87 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
89 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
90 static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
91 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
92 static int ieee80211_media_setup(struct ieee80211com *ic,
93 struct ifmedia *media, int caps, int addsta,
94 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
95 static int media_status(enum ieee80211_opmode,
96 const struct ieee80211_channel *);
97 static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
99 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
102 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
104 #define B(r) ((r) | IEEE80211_RATE_BASIC)
105 static const struct ieee80211_rateset ieee80211_rateset_11a =
106 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
107 static const struct ieee80211_rateset ieee80211_rateset_half =
108 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
109 static const struct ieee80211_rateset ieee80211_rateset_quarter =
110 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
111 static const struct ieee80211_rateset ieee80211_rateset_11b =
112 { 4, { B(2), B(4), B(11), B(22) } };
113 /* NB: OFDM rates are handled specially based on mode */
114 static const struct ieee80211_rateset ieee80211_rateset_11g =
115 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
119 * Fill in 802.11 available channel set, mark
120 * all available channels as active, and pick
121 * a default channel if not already specified.
124 ieee80211_chan_init(struct ieee80211com *ic)
126 #define DEFAULTRATES(m, def) do { \
127 if (ic->ic_sup_rates[m].rs_nrates == 0) \
128 ic->ic_sup_rates[m] = def; \
130 struct ieee80211_channel *c;
133 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
134 ("invalid number of channels specified: %u", ic->ic_nchans));
135 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
136 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
137 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
138 for (i = 0; i < ic->ic_nchans; i++) {
139 c = &ic->ic_channels[i];
140 KASSERT(c->ic_flags != 0, ("channel with no flags"));
142 * Help drivers that work only with frequencies by filling
143 * in IEEE channel #'s if not already calculated. Note this
144 * mimics similar work done in ieee80211_setregdomain when
145 * changing regulatory state.
148 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
149 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
150 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
151 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
153 /* default max tx power to max regulatory */
154 if (c->ic_maxpower == 0)
155 c->ic_maxpower = 2*c->ic_maxregpower;
156 setbit(ic->ic_chan_avail, c->ic_ieee);
158 * Identify mode capabilities.
160 if (IEEE80211_IS_CHAN_A(c))
161 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
162 if (IEEE80211_IS_CHAN_B(c))
163 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
164 if (IEEE80211_IS_CHAN_ANYG(c))
165 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
166 if (IEEE80211_IS_CHAN_FHSS(c))
167 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
168 if (IEEE80211_IS_CHAN_108A(c))
169 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
170 if (IEEE80211_IS_CHAN_108G(c))
171 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
172 if (IEEE80211_IS_CHAN_ST(c))
173 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
174 if (IEEE80211_IS_CHAN_HALF(c))
175 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
176 if (IEEE80211_IS_CHAN_QUARTER(c))
177 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
178 if (IEEE80211_IS_CHAN_HTA(c))
179 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
180 if (IEEE80211_IS_CHAN_HTG(c))
181 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
183 /* initialize candidate channels to all available */
184 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
185 sizeof(ic->ic_chan_avail));
187 /* sort channel table to allow lookup optimizations */
188 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
190 /* invalidate any previous state */
191 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
192 ic->ic_prevchan = NULL;
193 ic->ic_csa_newchan = NULL;
194 /* arbitrarily pick the first channel */
195 ic->ic_curchan = &ic->ic_channels[0];
196 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
198 /* fillin well-known rate sets if driver has not specified */
199 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
200 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
201 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
202 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
203 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
204 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
205 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
206 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
207 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
208 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
211 * Setup required information to fill the mcsset field, if driver did
212 * not. Assume a 2T2R setup for historic reasons.
214 if (ic->ic_rxstream == 0)
216 if (ic->ic_txstream == 0)
220 * Set auto mode to reset active channel state and any desired channel.
222 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
227 null_update_mcast(struct ieee80211com *ic)
230 ic_printf(ic, "need multicast update callback\n");
234 null_update_promisc(struct ieee80211com *ic)
237 ic_printf(ic, "need promiscuous mode update callback\n");
241 null_update_chw(struct ieee80211com *ic)
244 ic_printf(ic, "%s: need callback\n", __func__);
248 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
253 retval = printf("%s: ", ic->ic_name);
255 retval += vprintf(fmt, ap);
260 static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
261 static struct mtx ic_list_mtx;
262 MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
265 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
267 struct ieee80211com *ic;
272 sb = sbuf_new_auto();
274 mtx_lock(&ic_list_mtx);
275 LIST_FOREACH(ic, &ic_head, ic_next) {
276 sbuf_printf(sb, "%s%s", sp, ic->ic_name);
279 mtx_unlock(&ic_list_mtx);
281 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
286 SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
287 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
288 sysctl_ieee80211coms, "A", "names of available 802.11 devices");
291 * Attach/setup the common net80211 state. Called by
292 * the driver on attach to prior to creating any vap's.
295 ieee80211_ifattach(struct ieee80211com *ic)
298 IEEE80211_LOCK_INIT(ic, ic->ic_name);
299 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
300 TAILQ_INIT(&ic->ic_vaps);
302 /* Create a taskqueue for all state changes */
303 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
304 taskqueue_thread_enqueue, &ic->ic_tq);
305 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
307 ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
308 ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
310 * Fill in 802.11 available channel set, mark all
311 * available channels as active, and pick a default
312 * channel if not already specified.
314 ieee80211_chan_init(ic);
316 ic->ic_update_mcast = null_update_mcast;
317 ic->ic_update_promisc = null_update_promisc;
318 ic->ic_update_chw = null_update_chw;
320 ic->ic_hash_key = arc4random();
321 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
322 ic->ic_lintval = ic->ic_bintval;
323 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
325 ieee80211_crypto_attach(ic);
326 ieee80211_node_attach(ic);
327 ieee80211_power_attach(ic);
328 ieee80211_proto_attach(ic);
329 #ifdef IEEE80211_SUPPORT_SUPERG
330 ieee80211_superg_attach(ic);
332 ieee80211_ht_attach(ic);
333 ieee80211_scan_attach(ic);
334 ieee80211_regdomain_attach(ic);
335 ieee80211_dfs_attach(ic);
337 ieee80211_sysctl_attach(ic);
339 mtx_lock(&ic_list_mtx);
340 LIST_INSERT_HEAD(&ic_head, ic, ic_next);
341 mtx_unlock(&ic_list_mtx);
345 * Detach net80211 state on device detach. Tear down
346 * all vap's and reclaim all common state prior to the
347 * device state going away. Note we may call back into
348 * driver; it must be prepared for this.
351 ieee80211_ifdetach(struct ieee80211com *ic)
353 struct ieee80211vap *vap;
355 mtx_lock(&ic_list_mtx);
356 LIST_REMOVE(ic, ic_next);
357 mtx_unlock(&ic_list_mtx);
359 taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
362 * The VAP is responsible for setting and clearing
363 * the VIMAGE context.
365 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
366 ieee80211_vap_destroy(vap);
367 ieee80211_waitfor_parent(ic);
369 ieee80211_sysctl_detach(ic);
370 ieee80211_dfs_detach(ic);
371 ieee80211_regdomain_detach(ic);
372 ieee80211_scan_detach(ic);
373 #ifdef IEEE80211_SUPPORT_SUPERG
374 ieee80211_superg_detach(ic);
376 ieee80211_ht_detach(ic);
377 /* NB: must be called before ieee80211_node_detach */
378 ieee80211_proto_detach(ic);
379 ieee80211_crypto_detach(ic);
380 ieee80211_power_detach(ic);
381 ieee80211_node_detach(ic);
383 counter_u64_free(ic->ic_ierrors);
384 counter_u64_free(ic->ic_oerrors);
386 taskqueue_free(ic->ic_tq);
387 IEEE80211_TX_LOCK_DESTROY(ic);
388 IEEE80211_LOCK_DESTROY(ic);
391 struct ieee80211com *
392 ieee80211_find_com(const char *name)
394 struct ieee80211com *ic;
396 mtx_lock(&ic_list_mtx);
397 LIST_FOREACH(ic, &ic_head, ic_next)
398 if (strcmp(ic->ic_name, name) == 0)
400 mtx_unlock(&ic_list_mtx);
406 * Default reset method for use with the ioctl support. This
407 * method is invoked after any state change in the 802.11
408 * layer that should be propagated to the hardware but not
409 * require re-initialization of the 802.11 state machine (e.g
410 * rescanning for an ap). We always return ENETRESET which
411 * should cause the driver to re-initialize the device. Drivers
412 * can override this method to implement more optimized support.
415 default_reset(struct ieee80211vap *vap, u_long cmd)
421 * Add underlying device errors to vap errors.
424 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
426 struct ieee80211vap *vap = ifp->if_softc;
427 struct ieee80211com *ic = vap->iv_ic;
430 rv = if_get_counter_default(ifp, cnt);
432 case IFCOUNTER_OERRORS:
433 rv += counter_u64_fetch(ic->ic_oerrors);
435 case IFCOUNTER_IERRORS:
436 rv += counter_u64_fetch(ic->ic_ierrors);
446 * Prepare a vap for use. Drivers use this call to
447 * setup net80211 state in new vap's prior attaching
448 * them with ieee80211_vap_attach (below).
451 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
452 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
453 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
457 ifp = if_alloc(IFT_ETHER);
459 ic_printf(ic, "%s: unable to allocate ifnet\n",
463 if_initname(ifp, name, unit);
464 ifp->if_softc = vap; /* back pointer */
465 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
466 ifp->if_transmit = ieee80211_vap_transmit;
467 ifp->if_qflush = ieee80211_vap_qflush;
468 ifp->if_ioctl = ieee80211_ioctl;
469 ifp->if_init = ieee80211_init;
470 ifp->if_get_counter = ieee80211_get_counter;
474 vap->iv_flags = ic->ic_flags; /* propagate common flags */
475 vap->iv_flags_ext = ic->ic_flags_ext;
476 vap->iv_flags_ven = ic->ic_flags_ven;
477 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
478 vap->iv_htcaps = ic->ic_htcaps;
479 vap->iv_htextcaps = ic->ic_htextcaps;
480 vap->iv_opmode = opmode;
481 vap->iv_caps |= ieee80211_opcap[opmode];
482 vap->iv_myaddr = ic->ic_macaddr;
484 case IEEE80211_M_WDS:
486 * WDS links must specify the bssid of the far end.
487 * For legacy operation this is a static relationship.
488 * For non-legacy operation the station must associate
489 * and be authorized to pass traffic. Plumbing the
490 * vap to the proper node happens when the vap
491 * transitions to RUN state.
493 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
494 vap->iv_flags |= IEEE80211_F_DESBSSID;
495 if (flags & IEEE80211_CLONE_WDSLEGACY)
496 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
498 #ifdef IEEE80211_SUPPORT_TDMA
499 case IEEE80211_M_AHDEMO:
500 if (flags & IEEE80211_CLONE_TDMA) {
501 /* NB: checked before clone operation allowed */
502 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
503 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
505 * Propagate TDMA capability to mark vap; this
506 * cannot be removed and is used to distinguish
507 * regular ahdemo operation from ahdemo+tdma.
509 vap->iv_caps |= IEEE80211_C_TDMA;
516 /* auto-enable s/w beacon miss support */
517 if (flags & IEEE80211_CLONE_NOBEACONS)
518 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
519 /* auto-generated or user supplied MAC address */
520 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
521 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
523 * Enable various functionality by default if we're
524 * capable; the driver can override us if it knows better.
526 if (vap->iv_caps & IEEE80211_C_WME)
527 vap->iv_flags |= IEEE80211_F_WME;
528 if (vap->iv_caps & IEEE80211_C_BURST)
529 vap->iv_flags |= IEEE80211_F_BURST;
530 /* NB: bg scanning only makes sense for station mode right now */
531 if (vap->iv_opmode == IEEE80211_M_STA &&
532 (vap->iv_caps & IEEE80211_C_BGSCAN))
533 vap->iv_flags |= IEEE80211_F_BGSCAN;
534 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
535 /* NB: DFS support only makes sense for ap mode right now */
536 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
537 (vap->iv_caps & IEEE80211_C_DFS))
538 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
540 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
541 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
542 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
544 * Install a default reset method for the ioctl support;
545 * the driver can override this.
547 vap->iv_reset = default_reset;
549 ieee80211_sysctl_vattach(vap);
550 ieee80211_crypto_vattach(vap);
551 ieee80211_node_vattach(vap);
552 ieee80211_power_vattach(vap);
553 ieee80211_proto_vattach(vap);
554 #ifdef IEEE80211_SUPPORT_SUPERG
555 ieee80211_superg_vattach(vap);
557 ieee80211_ht_vattach(vap);
558 ieee80211_scan_vattach(vap);
559 ieee80211_regdomain_vattach(vap);
560 ieee80211_radiotap_vattach(vap);
561 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
567 * Activate a vap. State should have been prepared with a
568 * call to ieee80211_vap_setup and by the driver. On return
569 * from this call the vap is ready for use.
572 ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
573 ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
575 struct ifnet *ifp = vap->iv_ifp;
576 struct ieee80211com *ic = vap->iv_ic;
577 struct ifmediareq imr;
580 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
581 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
582 __func__, ieee80211_opmode_name[vap->iv_opmode],
583 ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
586 * Do late attach work that cannot happen until after
587 * the driver has had a chance to override defaults.
589 ieee80211_node_latevattach(vap);
590 ieee80211_power_latevattach(vap);
592 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
593 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
594 ieee80211_media_status(ifp, &imr);
595 /* NB: strip explicit mode; we're actually in autoselect */
596 ifmedia_set(&vap->iv_media,
597 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
599 ifp->if_baudrate = IF_Mbps(maxrate);
601 ether_ifattach(ifp, macaddr);
602 vap->iv_myaddr = IF_LLADDR(ifp);
603 /* hook output method setup by ether_ifattach */
604 vap->iv_output = ifp->if_output;
605 ifp->if_output = ieee80211_output;
606 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
609 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
610 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
611 #ifdef IEEE80211_SUPPORT_SUPERG
612 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
614 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
615 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
616 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
617 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
618 IEEE80211_UNLOCK(ic);
624 * Tear down vap state and reclaim the ifnet.
625 * The driver is assumed to have prepared for
626 * this; e.g. by turning off interrupts for the
630 ieee80211_vap_detach(struct ieee80211vap *vap)
632 struct ieee80211com *ic = vap->iv_ic;
633 struct ifnet *ifp = vap->iv_ifp;
635 CURVNET_SET(ifp->if_vnet);
637 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
638 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
640 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
646 * Flush any deferred vap tasks.
648 ieee80211_draintask(ic, &vap->iv_nstate_task);
649 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
651 /* XXX band-aid until ifnet handles this for us */
652 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
655 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
656 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
657 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
658 #ifdef IEEE80211_SUPPORT_SUPERG
659 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
661 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
662 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
663 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
664 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
665 /* NB: this handles the bpfdetach done below */
666 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
667 if (vap->iv_ifflags & IFF_PROMISC)
668 ieee80211_promisc(vap, false);
669 if (vap->iv_ifflags & IFF_ALLMULTI)
670 ieee80211_allmulti(vap, false);
671 IEEE80211_UNLOCK(ic);
673 ifmedia_removeall(&vap->iv_media);
675 ieee80211_radiotap_vdetach(vap);
676 ieee80211_regdomain_vdetach(vap);
677 ieee80211_scan_vdetach(vap);
678 #ifdef IEEE80211_SUPPORT_SUPERG
679 ieee80211_superg_vdetach(vap);
681 ieee80211_ht_vdetach(vap);
682 /* NB: must be before ieee80211_node_vdetach */
683 ieee80211_proto_vdetach(vap);
684 ieee80211_crypto_vdetach(vap);
685 ieee80211_power_vdetach(vap);
686 ieee80211_node_vdetach(vap);
687 ieee80211_sysctl_vdetach(vap);
695 * Count number of vaps in promisc, and issue promisc on
696 * parent respectively.
699 ieee80211_promisc(struct ieee80211vap *vap, bool on)
701 struct ieee80211com *ic = vap->iv_ic;
704 * XXX the bridge sets PROMISC but we don't want to
705 * enable it on the device, discard here so all the
706 * drivers don't need to special-case it
708 if (!(vap->iv_opmode == IEEE80211_M_MONITOR ||
709 (vap->iv_opmode == IEEE80211_M_AHDEMO &&
710 (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
715 if (++ic->ic_promisc == 1)
716 ieee80211_runtask(ic, &ic->ic_promisc_task);
718 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
720 if (--ic->ic_promisc == 0)
721 ieee80211_runtask(ic, &ic->ic_promisc_task);
723 IEEE80211_UNLOCK(ic);
727 * Count number of vaps in allmulti, and issue allmulti on
728 * parent respectively.
731 ieee80211_allmulti(struct ieee80211vap *vap, bool on)
733 struct ieee80211com *ic = vap->iv_ic;
737 if (++ic->ic_allmulti == 1)
738 ieee80211_runtask(ic, &ic->ic_mcast_task);
740 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
742 if (--ic->ic_allmulti == 0)
743 ieee80211_runtask(ic, &ic->ic_mcast_task);
745 IEEE80211_UNLOCK(ic);
749 * Synchronize flag bit state in the com structure
750 * according to the state of all vap's. This is used,
751 * for example, to handle state changes via ioctls.
754 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
756 struct ieee80211vap *vap;
759 IEEE80211_LOCK_ASSERT(ic);
762 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
763 if (vap->iv_flags & flag) {
768 ic->ic_flags |= flag;
770 ic->ic_flags &= ~flag;
774 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
776 struct ieee80211com *ic = vap->iv_ic;
781 vap->iv_flags &= ~flag;
783 vap->iv_flags |= flag;
784 ieee80211_syncflag_locked(ic, flag);
785 IEEE80211_UNLOCK(ic);
789 * Synchronize flags_ht bit state in the com structure
790 * according to the state of all vap's. This is used,
791 * for example, to handle state changes via ioctls.
794 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
796 struct ieee80211vap *vap;
799 IEEE80211_LOCK_ASSERT(ic);
802 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
803 if (vap->iv_flags_ht & flag) {
808 ic->ic_flags_ht |= flag;
810 ic->ic_flags_ht &= ~flag;
814 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
816 struct ieee80211com *ic = vap->iv_ic;
821 vap->iv_flags_ht &= ~flag;
823 vap->iv_flags_ht |= flag;
824 ieee80211_syncflag_ht_locked(ic, flag);
825 IEEE80211_UNLOCK(ic);
829 * Synchronize flags_ext bit state in the com structure
830 * according to the state of all vap's. This is used,
831 * for example, to handle state changes via ioctls.
834 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
836 struct ieee80211vap *vap;
839 IEEE80211_LOCK_ASSERT(ic);
842 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
843 if (vap->iv_flags_ext & flag) {
848 ic->ic_flags_ext |= flag;
850 ic->ic_flags_ext &= ~flag;
854 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
856 struct ieee80211com *ic = vap->iv_ic;
861 vap->iv_flags_ext &= ~flag;
863 vap->iv_flags_ext |= flag;
864 ieee80211_syncflag_ext_locked(ic, flag);
865 IEEE80211_UNLOCK(ic);
869 mapgsm(u_int freq, u_int flags)
872 if (flags & IEEE80211_CHAN_QUARTER)
874 else if (flags & IEEE80211_CHAN_HALF)
878 /* NB: there is no 907/20 wide but leave room */
879 return (freq - 906*10) / 5;
883 mappsb(u_int freq, u_int flags)
885 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
889 * Convert MHz frequency to IEEE channel number.
892 ieee80211_mhz2ieee(u_int freq, u_int flags)
894 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
895 if (flags & IEEE80211_CHAN_GSM)
896 return mapgsm(freq, flags);
897 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
901 return ((int) freq - 2407) / 5;
903 return 15 + ((freq - 2512) / 20);
904 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
906 /* XXX check regdomain? */
907 if (IS_FREQ_IN_PSB(freq))
908 return mappsb(freq, flags);
909 return (freq - 4000) / 5;
911 return (freq - 5000) / 5;
912 } else { /* either, guess */
916 if (907 <= freq && freq <= 922)
917 return mapgsm(freq, flags);
918 return ((int) freq - 2407) / 5;
921 if (IS_FREQ_IN_PSB(freq))
922 return mappsb(freq, flags);
923 else if (freq > 4900)
924 return (freq - 4000) / 5;
926 return 15 + ((freq - 2512) / 20);
928 return (freq - 5000) / 5;
930 #undef IS_FREQ_IN_PSB
934 * Convert channel to IEEE channel number.
937 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
940 ic_printf(ic, "invalid channel (NULL)\n");
943 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
947 * Convert IEEE channel number to MHz frequency.
950 ieee80211_ieee2mhz(u_int chan, u_int flags)
952 if (flags & IEEE80211_CHAN_GSM)
953 return 907 + 5 * (chan / 10);
954 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
958 return 2407 + chan*5;
960 return 2512 + ((chan-15)*20);
961 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
962 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
964 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
966 return 5000 + (chan*5);
967 } else { /* either, guess */
968 /* XXX can't distinguish PSB+GSM channels */
971 if (chan < 14) /* 0-13 */
972 return 2407 + chan*5;
973 if (chan < 27) /* 15-26 */
974 return 2512 + ((chan-15)*20);
975 return 5000 + (chan*5);
980 * Locate a channel given a frequency+flags. We cache
981 * the previous lookup to optimize switching between two
982 * channels--as happens with dynamic turbo.
984 struct ieee80211_channel *
985 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
987 struct ieee80211_channel *c;
990 flags &= IEEE80211_CHAN_ALLTURBO;
992 if (c != NULL && c->ic_freq == freq &&
993 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
995 /* brute force search */
996 for (i = 0; i < ic->ic_nchans; i++) {
997 c = &ic->ic_channels[i];
998 if (c->ic_freq == freq &&
999 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1006 * Locate a channel given a channel number+flags. We cache
1007 * the previous lookup to optimize switching between two
1008 * channels--as happens with dynamic turbo.
1010 struct ieee80211_channel *
1011 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1013 struct ieee80211_channel *c;
1016 flags &= IEEE80211_CHAN_ALLTURBO;
1017 c = ic->ic_prevchan;
1018 if (c != NULL && c->ic_ieee == ieee &&
1019 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1021 /* brute force search */
1022 for (i = 0; i < ic->ic_nchans; i++) {
1023 c = &ic->ic_channels[i];
1024 if (c->ic_ieee == ieee &&
1025 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1032 * Lookup a channel suitable for the given rx status.
1034 * This is used to find a channel for a frame (eg beacon, probe
1035 * response) based purely on the received PHY information.
1037 * For now it tries to do it based on R_FREQ / R_IEEE.
1038 * This is enough for 11bg and 11a (and thus 11ng/11na)
1039 * but it will not be enough for GSM, PSB channels and the
1040 * like. It also doesn't know about legacy-turbog and
1041 * legacy-turbo modes, which some offload NICs actually
1042 * support in weird ways.
1044 * Takes the ic and rxstatus; returns the channel or NULL
1047 * XXX TODO: Add support for that when the need arises.
1049 struct ieee80211_channel *
1050 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1051 const struct ieee80211_rx_stats *rxs)
1053 struct ieee80211com *ic = vap->iv_ic;
1055 struct ieee80211_channel *c;
1061 * Strictly speaking we only use freq for now,
1062 * however later on we may wish to just store
1063 * the ieee for verification.
1065 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1067 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1071 * If the rx status contains a valid ieee/freq, then
1072 * ensure we populate the correct channel information
1073 * in rxchan before passing it up to the scan infrastructure.
1074 * Offload NICs will pass up beacons from all channels
1075 * during background scans.
1078 /* Determine a band */
1079 /* XXX should be done by the driver? */
1080 if (rxs->c_freq < 3000) {
1081 flags = IEEE80211_CHAN_G;
1083 flags = IEEE80211_CHAN_A;
1086 /* Channel lookup */
1087 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1089 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1090 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1101 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1103 #define ADD(_ic, _s, _o) \
1104 ifmedia_add(media, \
1105 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1106 static const u_int mopts[IEEE80211_MODE_MAX] = {
1107 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1108 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1109 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1110 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1111 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1112 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1113 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1114 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1115 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1116 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1117 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1118 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1124 ADD(ic, mword, mopt); /* STA mode has no cap */
1125 if (caps & IEEE80211_C_IBSS)
1126 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1127 if (caps & IEEE80211_C_HOSTAP)
1128 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1129 if (caps & IEEE80211_C_AHDEMO)
1130 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1131 if (caps & IEEE80211_C_MONITOR)
1132 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1133 if (caps & IEEE80211_C_WDS)
1134 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1135 if (caps & IEEE80211_C_MBSS)
1136 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1141 * Setup the media data structures according to the channel and
1145 ieee80211_media_setup(struct ieee80211com *ic,
1146 struct ifmedia *media, int caps, int addsta,
1147 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1149 int i, j, rate, maxrate, mword, r;
1150 enum ieee80211_phymode mode;
1151 const struct ieee80211_rateset *rs;
1152 struct ieee80211_rateset allrates;
1155 * Fill in media characteristics.
1157 ifmedia_init(media, 0, media_change, media_stat);
1160 * Add media for legacy operating modes.
1162 memset(&allrates, 0, sizeof(allrates));
1163 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1164 if (isclr(ic->ic_modecaps, mode))
1166 addmedia(media, caps, addsta, mode, IFM_AUTO);
1167 if (mode == IEEE80211_MODE_AUTO)
1169 rs = &ic->ic_sup_rates[mode];
1170 for (i = 0; i < rs->rs_nrates; i++) {
1171 rate = rs->rs_rates[i];
1172 mword = ieee80211_rate2media(ic, rate, mode);
1175 addmedia(media, caps, addsta, mode, mword);
1177 * Add legacy rate to the collection of all rates.
1179 r = rate & IEEE80211_RATE_VAL;
1180 for (j = 0; j < allrates.rs_nrates; j++)
1181 if (allrates.rs_rates[j] == r)
1183 if (j == allrates.rs_nrates) {
1184 /* unique, add to the set */
1185 allrates.rs_rates[j] = r;
1186 allrates.rs_nrates++;
1188 rate = (rate & IEEE80211_RATE_VAL) / 2;
1193 for (i = 0; i < allrates.rs_nrates; i++) {
1194 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1195 IEEE80211_MODE_AUTO);
1198 /* NB: remove media options from mword */
1199 addmedia(media, caps, addsta,
1200 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1203 * Add HT/11n media. Note that we do not have enough
1204 * bits in the media subtype to express the MCS so we
1205 * use a "placeholder" media subtype and any fixed MCS
1206 * must be specified with a different mechanism.
1208 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1209 if (isclr(ic->ic_modecaps, mode))
1211 addmedia(media, caps, addsta, mode, IFM_AUTO);
1212 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1214 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1215 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1216 addmedia(media, caps, addsta,
1217 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1218 i = ic->ic_txstream * 8 - 1;
1219 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1220 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1221 rate = ieee80211_htrates[i].ht40_rate_400ns;
1222 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1223 rate = ieee80211_htrates[i].ht40_rate_800ns;
1224 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1225 rate = ieee80211_htrates[i].ht20_rate_400ns;
1227 rate = ieee80211_htrates[i].ht20_rate_800ns;
1234 /* XXX inline or eliminate? */
1235 const struct ieee80211_rateset *
1236 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1238 /* XXX does this work for 11ng basic rates? */
1239 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1243 ieee80211_announce(struct ieee80211com *ic)
1246 enum ieee80211_phymode mode;
1247 const struct ieee80211_rateset *rs;
1249 /* NB: skip AUTO since it has no rates */
1250 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1251 if (isclr(ic->ic_modecaps, mode))
1253 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1254 rs = &ic->ic_sup_rates[mode];
1255 for (i = 0; i < rs->rs_nrates; i++) {
1256 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1259 rate = ieee80211_media2rate(mword);
1260 printf("%s%d%sMbps", (i != 0 ? " " : ""),
1261 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1265 ieee80211_ht_announce(ic);
1269 ieee80211_announce_channels(struct ieee80211com *ic)
1271 const struct ieee80211_channel *c;
1275 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1276 for (i = 0; i < ic->ic_nchans; i++) {
1277 c = &ic->ic_channels[i];
1278 if (IEEE80211_IS_CHAN_ST(c))
1280 else if (IEEE80211_IS_CHAN_108A(c))
1282 else if (IEEE80211_IS_CHAN_108G(c))
1284 else if (IEEE80211_IS_CHAN_HT(c))
1286 else if (IEEE80211_IS_CHAN_A(c))
1288 else if (IEEE80211_IS_CHAN_ANYG(c))
1290 else if (IEEE80211_IS_CHAN_B(c))
1294 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1296 else if (IEEE80211_IS_CHAN_HALF(c))
1298 else if (IEEE80211_IS_CHAN_QUARTER(c))
1302 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1303 , c->ic_ieee, c->ic_freq, type
1305 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1306 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1308 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1309 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1315 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1317 switch (IFM_MODE(ime->ifm_media)) {
1318 case IFM_IEEE80211_11A:
1319 *mode = IEEE80211_MODE_11A;
1321 case IFM_IEEE80211_11B:
1322 *mode = IEEE80211_MODE_11B;
1324 case IFM_IEEE80211_11G:
1325 *mode = IEEE80211_MODE_11G;
1327 case IFM_IEEE80211_FH:
1328 *mode = IEEE80211_MODE_FH;
1330 case IFM_IEEE80211_11NA:
1331 *mode = IEEE80211_MODE_11NA;
1333 case IFM_IEEE80211_11NG:
1334 *mode = IEEE80211_MODE_11NG;
1337 *mode = IEEE80211_MODE_AUTO;
1343 * Turbo mode is an ``option''.
1344 * XXX does not apply to AUTO
1346 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1347 if (*mode == IEEE80211_MODE_11A) {
1348 if (flags & IEEE80211_F_TURBOP)
1349 *mode = IEEE80211_MODE_TURBO_A;
1351 *mode = IEEE80211_MODE_STURBO_A;
1352 } else if (*mode == IEEE80211_MODE_11G)
1353 *mode = IEEE80211_MODE_TURBO_G;
1362 * Handle a media change request on the vap interface.
1365 ieee80211_media_change(struct ifnet *ifp)
1367 struct ieee80211vap *vap = ifp->if_softc;
1368 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1371 if (!media2mode(ime, vap->iv_flags, &newmode))
1373 if (vap->iv_des_mode != newmode) {
1374 vap->iv_des_mode = newmode;
1375 /* XXX kick state machine if up+running */
1381 * Common code to calculate the media status word
1382 * from the operating mode and channel state.
1385 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1389 status = IFM_IEEE80211;
1391 case IEEE80211_M_STA:
1393 case IEEE80211_M_IBSS:
1394 status |= IFM_IEEE80211_ADHOC;
1396 case IEEE80211_M_HOSTAP:
1397 status |= IFM_IEEE80211_HOSTAP;
1399 case IEEE80211_M_MONITOR:
1400 status |= IFM_IEEE80211_MONITOR;
1402 case IEEE80211_M_AHDEMO:
1403 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1405 case IEEE80211_M_WDS:
1406 status |= IFM_IEEE80211_WDS;
1408 case IEEE80211_M_MBSS:
1409 status |= IFM_IEEE80211_MBSS;
1412 if (IEEE80211_IS_CHAN_HTA(chan)) {
1413 status |= IFM_IEEE80211_11NA;
1414 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1415 status |= IFM_IEEE80211_11NG;
1416 } else if (IEEE80211_IS_CHAN_A(chan)) {
1417 status |= IFM_IEEE80211_11A;
1418 } else if (IEEE80211_IS_CHAN_B(chan)) {
1419 status |= IFM_IEEE80211_11B;
1420 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1421 status |= IFM_IEEE80211_11G;
1422 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1423 status |= IFM_IEEE80211_FH;
1425 /* XXX else complain? */
1427 if (IEEE80211_IS_CHAN_TURBO(chan))
1428 status |= IFM_IEEE80211_TURBO;
1430 if (IEEE80211_IS_CHAN_HT20(chan))
1431 status |= IFM_IEEE80211_HT20;
1432 if (IEEE80211_IS_CHAN_HT40(chan))
1433 status |= IFM_IEEE80211_HT40;
1439 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1441 struct ieee80211vap *vap = ifp->if_softc;
1442 struct ieee80211com *ic = vap->iv_ic;
1443 enum ieee80211_phymode mode;
1445 imr->ifm_status = IFM_AVALID;
1447 * NB: use the current channel's mode to lock down a xmit
1448 * rate only when running; otherwise we may have a mismatch
1449 * in which case the rate will not be convertible.
1451 if (vap->iv_state == IEEE80211_S_RUN ||
1452 vap->iv_state == IEEE80211_S_SLEEP) {
1453 imr->ifm_status |= IFM_ACTIVE;
1454 mode = ieee80211_chan2mode(ic->ic_curchan);
1456 mode = IEEE80211_MODE_AUTO;
1457 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1459 * Calculate a current rate if possible.
1461 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1463 * A fixed rate is set, report that.
1465 imr->ifm_active |= ieee80211_rate2media(ic,
1466 vap->iv_txparms[mode].ucastrate, mode);
1467 } else if (vap->iv_opmode == IEEE80211_M_STA) {
1469 * In station mode report the current transmit rate.
1471 imr->ifm_active |= ieee80211_rate2media(ic,
1472 vap->iv_bss->ni_txrate, mode);
1474 imr->ifm_active |= IFM_AUTO;
1475 if (imr->ifm_status & IFM_ACTIVE)
1476 imr->ifm_current = imr->ifm_active;
1480 * Set the current phy mode and recalculate the active channel
1481 * set based on the available channels for this mode. Also
1482 * select a new default/current channel if the current one is
1483 * inappropriate for this mode.
1486 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1489 * Adjust basic rates in 11b/11g supported rate set.
1490 * Note that if operating on a hal/quarter rate channel
1491 * this is a noop as those rates sets are different
1494 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1495 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1497 ic->ic_curmode = mode;
1498 ieee80211_reset_erp(ic); /* reset ERP state */
1504 * Return the phy mode for with the specified channel.
1506 enum ieee80211_phymode
1507 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1510 if (IEEE80211_IS_CHAN_HTA(chan))
1511 return IEEE80211_MODE_11NA;
1512 else if (IEEE80211_IS_CHAN_HTG(chan))
1513 return IEEE80211_MODE_11NG;
1514 else if (IEEE80211_IS_CHAN_108G(chan))
1515 return IEEE80211_MODE_TURBO_G;
1516 else if (IEEE80211_IS_CHAN_ST(chan))
1517 return IEEE80211_MODE_STURBO_A;
1518 else if (IEEE80211_IS_CHAN_TURBO(chan))
1519 return IEEE80211_MODE_TURBO_A;
1520 else if (IEEE80211_IS_CHAN_HALF(chan))
1521 return IEEE80211_MODE_HALF;
1522 else if (IEEE80211_IS_CHAN_QUARTER(chan))
1523 return IEEE80211_MODE_QUARTER;
1524 else if (IEEE80211_IS_CHAN_A(chan))
1525 return IEEE80211_MODE_11A;
1526 else if (IEEE80211_IS_CHAN_ANYG(chan))
1527 return IEEE80211_MODE_11G;
1528 else if (IEEE80211_IS_CHAN_B(chan))
1529 return IEEE80211_MODE_11B;
1530 else if (IEEE80211_IS_CHAN_FHSS(chan))
1531 return IEEE80211_MODE_FH;
1533 /* NB: should not get here */
1534 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1535 __func__, chan->ic_freq, chan->ic_flags);
1536 return IEEE80211_MODE_11B;
1540 u_int match; /* rate + mode */
1541 u_int media; /* if_media rate */
1545 findmedia(const struct ratemedia rates[], int n, u_int match)
1549 for (i = 0; i < n; i++)
1550 if (rates[i].match == match)
1551 return rates[i].media;
1556 * Convert IEEE80211 rate value to ifmedia subtype.
1557 * Rate is either a legacy rate in units of 0.5Mbps
1561 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1563 static const struct ratemedia rates[] = {
1564 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1565 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1566 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1567 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1568 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1569 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1570 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1571 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1572 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1573 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1574 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1575 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1576 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1577 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1578 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1579 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1580 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1581 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1582 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1583 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1584 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1585 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1586 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1587 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1588 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1589 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1590 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1591 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1592 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1593 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1594 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1596 static const struct ratemedia htrates[] = {
1597 { 0, IFM_IEEE80211_MCS },
1598 { 1, IFM_IEEE80211_MCS },
1599 { 2, IFM_IEEE80211_MCS },
1600 { 3, IFM_IEEE80211_MCS },
1601 { 4, IFM_IEEE80211_MCS },
1602 { 5, IFM_IEEE80211_MCS },
1603 { 6, IFM_IEEE80211_MCS },
1604 { 7, IFM_IEEE80211_MCS },
1605 { 8, IFM_IEEE80211_MCS },
1606 { 9, IFM_IEEE80211_MCS },
1607 { 10, IFM_IEEE80211_MCS },
1608 { 11, IFM_IEEE80211_MCS },
1609 { 12, IFM_IEEE80211_MCS },
1610 { 13, IFM_IEEE80211_MCS },
1611 { 14, IFM_IEEE80211_MCS },
1612 { 15, IFM_IEEE80211_MCS },
1613 { 16, IFM_IEEE80211_MCS },
1614 { 17, IFM_IEEE80211_MCS },
1615 { 18, IFM_IEEE80211_MCS },
1616 { 19, IFM_IEEE80211_MCS },
1617 { 20, IFM_IEEE80211_MCS },
1618 { 21, IFM_IEEE80211_MCS },
1619 { 22, IFM_IEEE80211_MCS },
1620 { 23, IFM_IEEE80211_MCS },
1621 { 24, IFM_IEEE80211_MCS },
1622 { 25, IFM_IEEE80211_MCS },
1623 { 26, IFM_IEEE80211_MCS },
1624 { 27, IFM_IEEE80211_MCS },
1625 { 28, IFM_IEEE80211_MCS },
1626 { 29, IFM_IEEE80211_MCS },
1627 { 30, IFM_IEEE80211_MCS },
1628 { 31, IFM_IEEE80211_MCS },
1629 { 32, IFM_IEEE80211_MCS },
1630 { 33, IFM_IEEE80211_MCS },
1631 { 34, IFM_IEEE80211_MCS },
1632 { 35, IFM_IEEE80211_MCS },
1633 { 36, IFM_IEEE80211_MCS },
1634 { 37, IFM_IEEE80211_MCS },
1635 { 38, IFM_IEEE80211_MCS },
1636 { 39, IFM_IEEE80211_MCS },
1637 { 40, IFM_IEEE80211_MCS },
1638 { 41, IFM_IEEE80211_MCS },
1639 { 42, IFM_IEEE80211_MCS },
1640 { 43, IFM_IEEE80211_MCS },
1641 { 44, IFM_IEEE80211_MCS },
1642 { 45, IFM_IEEE80211_MCS },
1643 { 46, IFM_IEEE80211_MCS },
1644 { 47, IFM_IEEE80211_MCS },
1645 { 48, IFM_IEEE80211_MCS },
1646 { 49, IFM_IEEE80211_MCS },
1647 { 50, IFM_IEEE80211_MCS },
1648 { 51, IFM_IEEE80211_MCS },
1649 { 52, IFM_IEEE80211_MCS },
1650 { 53, IFM_IEEE80211_MCS },
1651 { 54, IFM_IEEE80211_MCS },
1652 { 55, IFM_IEEE80211_MCS },
1653 { 56, IFM_IEEE80211_MCS },
1654 { 57, IFM_IEEE80211_MCS },
1655 { 58, IFM_IEEE80211_MCS },
1656 { 59, IFM_IEEE80211_MCS },
1657 { 60, IFM_IEEE80211_MCS },
1658 { 61, IFM_IEEE80211_MCS },
1659 { 62, IFM_IEEE80211_MCS },
1660 { 63, IFM_IEEE80211_MCS },
1661 { 64, IFM_IEEE80211_MCS },
1662 { 65, IFM_IEEE80211_MCS },
1663 { 66, IFM_IEEE80211_MCS },
1664 { 67, IFM_IEEE80211_MCS },
1665 { 68, IFM_IEEE80211_MCS },
1666 { 69, IFM_IEEE80211_MCS },
1667 { 70, IFM_IEEE80211_MCS },
1668 { 71, IFM_IEEE80211_MCS },
1669 { 72, IFM_IEEE80211_MCS },
1670 { 73, IFM_IEEE80211_MCS },
1671 { 74, IFM_IEEE80211_MCS },
1672 { 75, IFM_IEEE80211_MCS },
1673 { 76, IFM_IEEE80211_MCS },
1678 * Check 11n rates first for match as an MCS.
1680 if (mode == IEEE80211_MODE_11NA) {
1681 if (rate & IEEE80211_RATE_MCS) {
1682 rate &= ~IEEE80211_RATE_MCS;
1683 m = findmedia(htrates, nitems(htrates), rate);
1685 return m | IFM_IEEE80211_11NA;
1687 } else if (mode == IEEE80211_MODE_11NG) {
1688 /* NB: 12 is ambiguous, it will be treated as an MCS */
1689 if (rate & IEEE80211_RATE_MCS) {
1690 rate &= ~IEEE80211_RATE_MCS;
1691 m = findmedia(htrates, nitems(htrates), rate);
1693 return m | IFM_IEEE80211_11NG;
1696 rate &= IEEE80211_RATE_VAL;
1698 case IEEE80211_MODE_11A:
1699 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
1700 case IEEE80211_MODE_QUARTER:
1701 case IEEE80211_MODE_11NA:
1702 case IEEE80211_MODE_TURBO_A:
1703 case IEEE80211_MODE_STURBO_A:
1704 return findmedia(rates, nitems(rates),
1705 rate | IFM_IEEE80211_11A);
1706 case IEEE80211_MODE_11B:
1707 return findmedia(rates, nitems(rates),
1708 rate | IFM_IEEE80211_11B);
1709 case IEEE80211_MODE_FH:
1710 return findmedia(rates, nitems(rates),
1711 rate | IFM_IEEE80211_FH);
1712 case IEEE80211_MODE_AUTO:
1713 /* NB: ic may be NULL for some drivers */
1714 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1715 return findmedia(rates, nitems(rates),
1716 rate | IFM_IEEE80211_FH);
1717 /* NB: hack, 11g matches both 11b+11a rates */
1719 case IEEE80211_MODE_11G:
1720 case IEEE80211_MODE_11NG:
1721 case IEEE80211_MODE_TURBO_G:
1722 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1728 ieee80211_media2rate(int mword)
1730 static const int ieeerates[] = {
1734 2, /* IFM_IEEE80211_FH1 */
1735 4, /* IFM_IEEE80211_FH2 */
1736 2, /* IFM_IEEE80211_DS1 */
1737 4, /* IFM_IEEE80211_DS2 */
1738 11, /* IFM_IEEE80211_DS5 */
1739 22, /* IFM_IEEE80211_DS11 */
1740 44, /* IFM_IEEE80211_DS22 */
1741 12, /* IFM_IEEE80211_OFDM6 */
1742 18, /* IFM_IEEE80211_OFDM9 */
1743 24, /* IFM_IEEE80211_OFDM12 */
1744 36, /* IFM_IEEE80211_OFDM18 */
1745 48, /* IFM_IEEE80211_OFDM24 */
1746 72, /* IFM_IEEE80211_OFDM36 */
1747 96, /* IFM_IEEE80211_OFDM48 */
1748 108, /* IFM_IEEE80211_OFDM54 */
1749 144, /* IFM_IEEE80211_OFDM72 */
1750 0, /* IFM_IEEE80211_DS354k */
1751 0, /* IFM_IEEE80211_DS512k */
1752 6, /* IFM_IEEE80211_OFDM3 */
1753 9, /* IFM_IEEE80211_OFDM4 */
1754 54, /* IFM_IEEE80211_OFDM27 */
1755 -1, /* IFM_IEEE80211_MCS */
1757 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
1758 ieeerates[IFM_SUBTYPE(mword)] : 0;
1762 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1763 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1765 #define mix(a, b, c) \
1767 a -= b; a -= c; a ^= (c >> 13); \
1768 b -= c; b -= a; b ^= (a << 8); \
1769 c -= a; c -= b; c ^= (b >> 13); \
1770 a -= b; a -= c; a ^= (c >> 12); \
1771 b -= c; b -= a; b ^= (a << 16); \
1772 c -= a; c -= b; c ^= (b >> 5); \
1773 a -= b; a -= c; a ^= (c >> 3); \
1774 b -= c; b -= a; b ^= (a << 10); \
1775 c -= a; c -= b; c ^= (b >> 15); \
1776 } while (/*CONSTCOND*/0)
1779 ieee80211_mac_hash(const struct ieee80211com *ic,
1780 const uint8_t addr[IEEE80211_ADDR_LEN])
1782 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1798 ieee80211_channel_type_char(const struct ieee80211_channel *c)
1800 if (IEEE80211_IS_CHAN_ST(c))
1802 if (IEEE80211_IS_CHAN_108A(c))
1804 if (IEEE80211_IS_CHAN_108G(c))
1806 if (IEEE80211_IS_CHAN_HT(c))
1808 if (IEEE80211_IS_CHAN_A(c))
1810 if (IEEE80211_IS_CHAN_ANYG(c))
1812 if (IEEE80211_IS_CHAN_B(c))