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 int ieee80211_media_setup(struct ieee80211com *ic,
94 struct ifmedia *media, int caps, int addsta,
95 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
96 static int media_status(enum ieee80211_opmode,
97 const struct ieee80211_channel *);
98 static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
100 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
103 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
105 #define B(r) ((r) | IEEE80211_RATE_BASIC)
106 static const struct ieee80211_rateset ieee80211_rateset_11a =
107 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
108 static const struct ieee80211_rateset ieee80211_rateset_half =
109 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
110 static const struct ieee80211_rateset ieee80211_rateset_quarter =
111 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
112 static const struct ieee80211_rateset ieee80211_rateset_11b =
113 { 4, { B(2), B(4), B(11), B(22) } };
114 /* NB: OFDM rates are handled specially based on mode */
115 static const struct ieee80211_rateset ieee80211_rateset_11g =
116 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
120 * Fill in 802.11 available channel set, mark
121 * all available channels as active, and pick
122 * a default channel if not already specified.
125 ieee80211_chan_init(struct ieee80211com *ic)
127 #define DEFAULTRATES(m, def) do { \
128 if (ic->ic_sup_rates[m].rs_nrates == 0) \
129 ic->ic_sup_rates[m] = def; \
131 struct ieee80211_channel *c;
134 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
135 ("invalid number of channels specified: %u", ic->ic_nchans));
136 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
137 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
138 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
139 for (i = 0; i < ic->ic_nchans; i++) {
140 c = &ic->ic_channels[i];
141 KASSERT(c->ic_flags != 0, ("channel with no flags"));
143 * Help drivers that work only with frequencies by filling
144 * in IEEE channel #'s if not already calculated. Note this
145 * mimics similar work done in ieee80211_setregdomain when
146 * changing regulatory state.
149 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
150 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
151 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
152 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
154 /* default max tx power to max regulatory */
155 if (c->ic_maxpower == 0)
156 c->ic_maxpower = 2*c->ic_maxregpower;
157 setbit(ic->ic_chan_avail, c->ic_ieee);
159 * Identify mode capabilities.
161 if (IEEE80211_IS_CHAN_A(c))
162 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
163 if (IEEE80211_IS_CHAN_B(c))
164 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
165 if (IEEE80211_IS_CHAN_ANYG(c))
166 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
167 if (IEEE80211_IS_CHAN_FHSS(c))
168 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
169 if (IEEE80211_IS_CHAN_108A(c))
170 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
171 if (IEEE80211_IS_CHAN_108G(c))
172 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
173 if (IEEE80211_IS_CHAN_ST(c))
174 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
175 if (IEEE80211_IS_CHAN_HALF(c))
176 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
177 if (IEEE80211_IS_CHAN_QUARTER(c))
178 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
179 if (IEEE80211_IS_CHAN_HTA(c))
180 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
181 if (IEEE80211_IS_CHAN_HTG(c))
182 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
184 /* initialize candidate channels to all available */
185 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
186 sizeof(ic->ic_chan_avail));
188 /* sort channel table to allow lookup optimizations */
189 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
191 /* invalidate any previous state */
192 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
193 ic->ic_prevchan = NULL;
194 ic->ic_csa_newchan = NULL;
195 /* arbitrarily pick the first channel */
196 ic->ic_curchan = &ic->ic_channels[0];
197 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
199 /* fillin well-known rate sets if driver has not specified */
200 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
201 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
202 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
203 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
204 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
205 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
206 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
207 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
208 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
209 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
212 * Setup required information to fill the mcsset field, if driver did
213 * not. Assume a 2T2R setup for historic reasons.
215 if (ic->ic_rxstream == 0)
217 if (ic->ic_txstream == 0)
221 * Set auto mode to reset active channel state and any desired channel.
223 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
228 null_update_mcast(struct ieee80211com *ic)
231 ic_printf(ic, "need multicast update callback\n");
235 null_update_promisc(struct ieee80211com *ic)
238 ic_printf(ic, "need promiscuous mode update callback\n");
242 null_update_chw(struct ieee80211com *ic)
245 ic_printf(ic, "%s: need callback\n", __func__);
249 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
254 retval = printf("%s: ", ic->ic_name);
256 retval += vprintf(fmt, ap);
261 static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
262 static struct mtx ic_list_mtx;
263 MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
266 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
268 struct ieee80211com *ic;
273 sb = sbuf_new_auto();
275 mtx_lock(&ic_list_mtx);
276 LIST_FOREACH(ic, &ic_head, ic_next) {
277 sbuf_printf(sb, "%s%s", sp, ic->ic_name);
280 mtx_unlock(&ic_list_mtx);
282 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
287 SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
288 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
289 sysctl_ieee80211coms, "A", "names of available 802.11 devices");
292 * Attach/setup the common net80211 state. Called by
293 * the driver on attach to prior to creating any vap's.
296 ieee80211_ifattach(struct ieee80211com *ic)
299 IEEE80211_LOCK_INIT(ic, ic->ic_name);
300 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
301 TAILQ_INIT(&ic->ic_vaps);
303 /* Create a taskqueue for all state changes */
304 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
305 taskqueue_thread_enqueue, &ic->ic_tq);
306 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
308 ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
309 ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
311 * Fill in 802.11 available channel set, mark all
312 * available channels as active, and pick a default
313 * channel if not already specified.
315 ieee80211_chan_init(ic);
317 ic->ic_update_mcast = null_update_mcast;
318 ic->ic_update_promisc = null_update_promisc;
319 ic->ic_update_chw = null_update_chw;
321 ic->ic_hash_key = arc4random();
322 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
323 ic->ic_lintval = ic->ic_bintval;
324 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
326 ieee80211_crypto_attach(ic);
327 ieee80211_node_attach(ic);
328 ieee80211_power_attach(ic);
329 ieee80211_proto_attach(ic);
330 #ifdef IEEE80211_SUPPORT_SUPERG
331 ieee80211_superg_attach(ic);
333 ieee80211_ht_attach(ic);
334 ieee80211_scan_attach(ic);
335 ieee80211_regdomain_attach(ic);
336 ieee80211_dfs_attach(ic);
338 ieee80211_sysctl_attach(ic);
340 mtx_lock(&ic_list_mtx);
341 LIST_INSERT_HEAD(&ic_head, ic, ic_next);
342 mtx_unlock(&ic_list_mtx);
346 * Detach net80211 state on device detach. Tear down
347 * all vap's and reclaim all common state prior to the
348 * device state going away. Note we may call back into
349 * driver; it must be prepared for this.
352 ieee80211_ifdetach(struct ieee80211com *ic)
354 struct ieee80211vap *vap;
356 mtx_lock(&ic_list_mtx);
357 LIST_REMOVE(ic, ic_next);
358 mtx_unlock(&ic_list_mtx);
360 taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
363 * The VAP is responsible for setting and clearing
364 * the VIMAGE context.
366 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
367 ieee80211_vap_destroy(vap);
368 ieee80211_waitfor_parent(ic);
370 ieee80211_sysctl_detach(ic);
371 ieee80211_dfs_detach(ic);
372 ieee80211_regdomain_detach(ic);
373 ieee80211_scan_detach(ic);
374 #ifdef IEEE80211_SUPPORT_SUPERG
375 ieee80211_superg_detach(ic);
377 ieee80211_ht_detach(ic);
378 /* NB: must be called before ieee80211_node_detach */
379 ieee80211_proto_detach(ic);
380 ieee80211_crypto_detach(ic);
381 ieee80211_power_detach(ic);
382 ieee80211_node_detach(ic);
384 counter_u64_free(ic->ic_ierrors);
385 counter_u64_free(ic->ic_oerrors);
387 taskqueue_free(ic->ic_tq);
388 IEEE80211_TX_LOCK_DESTROY(ic);
389 IEEE80211_LOCK_DESTROY(ic);
392 struct ieee80211com *
393 ieee80211_find_com(const char *name)
395 struct ieee80211com *ic;
397 mtx_lock(&ic_list_mtx);
398 LIST_FOREACH(ic, &ic_head, ic_next)
399 if (strcmp(ic->ic_name, name) == 0)
401 mtx_unlock(&ic_list_mtx);
407 * Default reset method for use with the ioctl support. This
408 * method is invoked after any state change in the 802.11
409 * layer that should be propagated to the hardware but not
410 * require re-initialization of the 802.11 state machine (e.g
411 * rescanning for an ap). We always return ENETRESET which
412 * should cause the driver to re-initialize the device. Drivers
413 * can override this method to implement more optimized support.
416 default_reset(struct ieee80211vap *vap, u_long cmd)
422 * Add underlying device errors to vap errors.
425 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
427 struct ieee80211vap *vap = ifp->if_softc;
428 struct ieee80211com *ic = vap->iv_ic;
431 rv = if_get_counter_default(ifp, cnt);
433 case IFCOUNTER_OERRORS:
434 rv += counter_u64_fetch(ic->ic_oerrors);
436 case IFCOUNTER_IERRORS:
437 rv += counter_u64_fetch(ic->ic_ierrors);
447 * Prepare a vap for use. Drivers use this call to
448 * setup net80211 state in new vap's prior attaching
449 * them with ieee80211_vap_attach (below).
452 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
453 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
454 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
458 ifp = if_alloc(IFT_ETHER);
460 ic_printf(ic, "%s: unable to allocate ifnet\n",
464 if_initname(ifp, name, unit);
465 ifp->if_softc = vap; /* back pointer */
466 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
467 ifp->if_transmit = ieee80211_vap_transmit;
468 ifp->if_qflush = ieee80211_vap_qflush;
469 ifp->if_ioctl = ieee80211_ioctl;
470 ifp->if_init = ieee80211_init;
471 ifp->if_get_counter = ieee80211_get_counter;
475 vap->iv_flags = ic->ic_flags; /* propagate common flags */
476 vap->iv_flags_ext = ic->ic_flags_ext;
477 vap->iv_flags_ven = ic->ic_flags_ven;
478 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
479 vap->iv_htcaps = ic->ic_htcaps;
480 vap->iv_htextcaps = ic->ic_htextcaps;
481 vap->iv_opmode = opmode;
482 vap->iv_caps |= ieee80211_opcap[opmode];
483 vap->iv_myaddr = ic->ic_macaddr;
485 case IEEE80211_M_WDS:
487 * WDS links must specify the bssid of the far end.
488 * For legacy operation this is a static relationship.
489 * For non-legacy operation the station must associate
490 * and be authorized to pass traffic. Plumbing the
491 * vap to the proper node happens when the vap
492 * transitions to RUN state.
494 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
495 vap->iv_flags |= IEEE80211_F_DESBSSID;
496 if (flags & IEEE80211_CLONE_WDSLEGACY)
497 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
499 #ifdef IEEE80211_SUPPORT_TDMA
500 case IEEE80211_M_AHDEMO:
501 if (flags & IEEE80211_CLONE_TDMA) {
502 /* NB: checked before clone operation allowed */
503 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
504 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
506 * Propagate TDMA capability to mark vap; this
507 * cannot be removed and is used to distinguish
508 * regular ahdemo operation from ahdemo+tdma.
510 vap->iv_caps |= IEEE80211_C_TDMA;
517 /* auto-enable s/w beacon miss support */
518 if (flags & IEEE80211_CLONE_NOBEACONS)
519 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
520 /* auto-generated or user supplied MAC address */
521 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
522 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
524 * Enable various functionality by default if we're
525 * capable; the driver can override us if it knows better.
527 if (vap->iv_caps & IEEE80211_C_WME)
528 vap->iv_flags |= IEEE80211_F_WME;
529 if (vap->iv_caps & IEEE80211_C_BURST)
530 vap->iv_flags |= IEEE80211_F_BURST;
531 /* NB: bg scanning only makes sense for station mode right now */
532 if (vap->iv_opmode == IEEE80211_M_STA &&
533 (vap->iv_caps & IEEE80211_C_BGSCAN))
534 vap->iv_flags |= IEEE80211_F_BGSCAN;
535 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
536 /* NB: DFS support only makes sense for ap mode right now */
537 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
538 (vap->iv_caps & IEEE80211_C_DFS))
539 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
541 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
542 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
543 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
545 * Install a default reset method for the ioctl support;
546 * the driver can override this.
548 vap->iv_reset = default_reset;
550 ieee80211_sysctl_vattach(vap);
551 ieee80211_crypto_vattach(vap);
552 ieee80211_node_vattach(vap);
553 ieee80211_power_vattach(vap);
554 ieee80211_proto_vattach(vap);
555 #ifdef IEEE80211_SUPPORT_SUPERG
556 ieee80211_superg_vattach(vap);
558 ieee80211_ht_vattach(vap);
559 ieee80211_scan_vattach(vap);
560 ieee80211_regdomain_vattach(vap);
561 ieee80211_radiotap_vattach(vap);
562 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
568 * Activate a vap. State should have been prepared with a
569 * call to ieee80211_vap_setup and by the driver. On return
570 * from this call the vap is ready for use.
573 ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
574 ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
576 struct ifnet *ifp = vap->iv_ifp;
577 struct ieee80211com *ic = vap->iv_ic;
578 struct ifmediareq imr;
581 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
582 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
583 __func__, ieee80211_opmode_name[vap->iv_opmode],
584 ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
587 * Do late attach work that cannot happen until after
588 * the driver has had a chance to override defaults.
590 ieee80211_node_latevattach(vap);
591 ieee80211_power_latevattach(vap);
593 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
594 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
595 ieee80211_media_status(ifp, &imr);
596 /* NB: strip explicit mode; we're actually in autoselect */
597 ifmedia_set(&vap->iv_media,
598 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
600 ifp->if_baudrate = IF_Mbps(maxrate);
602 ether_ifattach(ifp, macaddr);
603 vap->iv_myaddr = IF_LLADDR(ifp);
604 /* hook output method setup by ether_ifattach */
605 vap->iv_output = ifp->if_output;
606 ifp->if_output = ieee80211_output;
607 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
610 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
611 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
612 #ifdef IEEE80211_SUPPORT_SUPERG
613 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
615 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
616 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
617 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
618 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
619 IEEE80211_UNLOCK(ic);
625 * Tear down vap state and reclaim the ifnet.
626 * The driver is assumed to have prepared for
627 * this; e.g. by turning off interrupts for the
631 ieee80211_vap_detach(struct ieee80211vap *vap)
633 struct ieee80211com *ic = vap->iv_ic;
634 struct ifnet *ifp = vap->iv_ifp;
636 CURVNET_SET(ifp->if_vnet);
638 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
639 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
641 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
647 * Flush any deferred vap tasks.
649 ieee80211_draintask(ic, &vap->iv_nstate_task);
650 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
652 /* XXX band-aid until ifnet handles this for us */
653 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
656 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
657 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
658 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
659 #ifdef IEEE80211_SUPPORT_SUPERG
660 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
662 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
663 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
664 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
665 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
666 /* NB: this handles the bpfdetach done below */
667 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
668 if (vap->iv_ifflags & IFF_PROMISC)
669 ieee80211_promisc(vap, false);
670 if (vap->iv_ifflags & IFF_ALLMULTI)
671 ieee80211_allmulti(vap, false);
672 IEEE80211_UNLOCK(ic);
674 ifmedia_removeall(&vap->iv_media);
676 ieee80211_radiotap_vdetach(vap);
677 ieee80211_regdomain_vdetach(vap);
678 ieee80211_scan_vdetach(vap);
679 #ifdef IEEE80211_SUPPORT_SUPERG
680 ieee80211_superg_vdetach(vap);
682 ieee80211_ht_vdetach(vap);
683 /* NB: must be before ieee80211_node_vdetach */
684 ieee80211_proto_vdetach(vap);
685 ieee80211_crypto_vdetach(vap);
686 ieee80211_power_vdetach(vap);
687 ieee80211_node_vdetach(vap);
688 ieee80211_sysctl_vdetach(vap);
696 * Count number of vaps in promisc, and issue promisc on
697 * parent respectively.
700 ieee80211_promisc(struct ieee80211vap *vap, bool on)
702 struct ieee80211com *ic = vap->iv_ic;
705 * XXX the bridge sets PROMISC but we don't want to
706 * enable it on the device, discard here so all the
707 * drivers don't need to special-case it
709 if (!(vap->iv_opmode == IEEE80211_M_MONITOR ||
710 (vap->iv_opmode == IEEE80211_M_AHDEMO &&
711 (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
716 if (++ic->ic_promisc == 1)
717 ieee80211_runtask(ic, &ic->ic_promisc_task);
719 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
721 if (--ic->ic_promisc == 0)
722 ieee80211_runtask(ic, &ic->ic_promisc_task);
724 IEEE80211_UNLOCK(ic);
728 * Count number of vaps in allmulti, and issue allmulti on
729 * parent respectively.
732 ieee80211_allmulti(struct ieee80211vap *vap, bool on)
734 struct ieee80211com *ic = vap->iv_ic;
738 if (++ic->ic_allmulti == 1)
739 ieee80211_runtask(ic, &ic->ic_mcast_task);
741 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
743 if (--ic->ic_allmulti == 0)
744 ieee80211_runtask(ic, &ic->ic_mcast_task);
746 IEEE80211_UNLOCK(ic);
750 * Synchronize flag bit state in the com structure
751 * according to the state of all vap's. This is used,
752 * for example, to handle state changes via ioctls.
755 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
757 struct ieee80211vap *vap;
760 IEEE80211_LOCK_ASSERT(ic);
763 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
764 if (vap->iv_flags & flag) {
769 ic->ic_flags |= flag;
771 ic->ic_flags &= ~flag;
775 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
777 struct ieee80211com *ic = vap->iv_ic;
782 vap->iv_flags &= ~flag;
784 vap->iv_flags |= flag;
785 ieee80211_syncflag_locked(ic, flag);
786 IEEE80211_UNLOCK(ic);
790 * Synchronize flags_ht 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_ht_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_ht & flag) {
809 ic->ic_flags_ht |= flag;
811 ic->ic_flags_ht &= ~flag;
815 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
817 struct ieee80211com *ic = vap->iv_ic;
822 vap->iv_flags_ht &= ~flag;
824 vap->iv_flags_ht |= flag;
825 ieee80211_syncflag_ht_locked(ic, flag);
826 IEEE80211_UNLOCK(ic);
830 * Synchronize flags_ext 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_ext_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_ext & flag) {
849 ic->ic_flags_ext |= flag;
851 ic->ic_flags_ext &= ~flag;
855 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
857 struct ieee80211com *ic = vap->iv_ic;
862 vap->iv_flags_ext &= ~flag;
864 vap->iv_flags_ext |= flag;
865 ieee80211_syncflag_ext_locked(ic, flag);
866 IEEE80211_UNLOCK(ic);
870 mapgsm(u_int freq, u_int flags)
873 if (flags & IEEE80211_CHAN_QUARTER)
875 else if (flags & IEEE80211_CHAN_HALF)
879 /* NB: there is no 907/20 wide but leave room */
880 return (freq - 906*10) / 5;
884 mappsb(u_int freq, u_int flags)
886 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
890 * Convert MHz frequency to IEEE channel number.
893 ieee80211_mhz2ieee(u_int freq, u_int flags)
895 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
896 if (flags & IEEE80211_CHAN_GSM)
897 return mapgsm(freq, flags);
898 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
902 return ((int) freq - 2407) / 5;
904 return 15 + ((freq - 2512) / 20);
905 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
907 /* XXX check regdomain? */
908 if (IS_FREQ_IN_PSB(freq))
909 return mappsb(freq, flags);
910 return (freq - 4000) / 5;
912 return (freq - 5000) / 5;
913 } else { /* either, guess */
917 if (907 <= freq && freq <= 922)
918 return mapgsm(freq, flags);
919 return ((int) freq - 2407) / 5;
922 if (IS_FREQ_IN_PSB(freq))
923 return mappsb(freq, flags);
924 else if (freq > 4900)
925 return (freq - 4000) / 5;
927 return 15 + ((freq - 2512) / 20);
929 return (freq - 5000) / 5;
931 #undef IS_FREQ_IN_PSB
935 * Convert channel to IEEE channel number.
938 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
941 ic_printf(ic, "invalid channel (NULL)\n");
944 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
948 * Convert IEEE channel number to MHz frequency.
951 ieee80211_ieee2mhz(u_int chan, u_int flags)
953 if (flags & IEEE80211_CHAN_GSM)
954 return 907 + 5 * (chan / 10);
955 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
959 return 2407 + chan*5;
961 return 2512 + ((chan-15)*20);
962 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
963 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
965 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
967 return 5000 + (chan*5);
968 } else { /* either, guess */
969 /* XXX can't distinguish PSB+GSM channels */
972 if (chan < 14) /* 0-13 */
973 return 2407 + chan*5;
974 if (chan < 27) /* 15-26 */
975 return 2512 + ((chan-15)*20);
976 return 5000 + (chan*5);
981 * Locate a channel given a frequency+flags. We cache
982 * the previous lookup to optimize switching between two
983 * channels--as happens with dynamic turbo.
985 struct ieee80211_channel *
986 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
988 struct ieee80211_channel *c;
991 flags &= IEEE80211_CHAN_ALLTURBO;
993 if (c != NULL && c->ic_freq == freq &&
994 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
996 /* brute force search */
997 for (i = 0; i < ic->ic_nchans; i++) {
998 c = &ic->ic_channels[i];
999 if (c->ic_freq == freq &&
1000 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1007 * Locate a channel given a channel number+flags. We cache
1008 * the previous lookup to optimize switching between two
1009 * channels--as happens with dynamic turbo.
1011 struct ieee80211_channel *
1012 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1014 struct ieee80211_channel *c;
1017 flags &= IEEE80211_CHAN_ALLTURBO;
1018 c = ic->ic_prevchan;
1019 if (c != NULL && c->ic_ieee == ieee &&
1020 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1022 /* brute force search */
1023 for (i = 0; i < ic->ic_nchans; i++) {
1024 c = &ic->ic_channels[i];
1025 if (c->ic_ieee == ieee &&
1026 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1033 * Lookup a channel suitable for the given rx status.
1035 * This is used to find a channel for a frame (eg beacon, probe
1036 * response) based purely on the received PHY information.
1038 * For now it tries to do it based on R_FREQ / R_IEEE.
1039 * This is enough for 11bg and 11a (and thus 11ng/11na)
1040 * but it will not be enough for GSM, PSB channels and the
1041 * like. It also doesn't know about legacy-turbog and
1042 * legacy-turbo modes, which some offload NICs actually
1043 * support in weird ways.
1045 * Takes the ic and rxstatus; returns the channel or NULL
1048 * XXX TODO: Add support for that when the need arises.
1050 struct ieee80211_channel *
1051 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1052 const struct ieee80211_rx_stats *rxs)
1054 struct ieee80211com *ic = vap->iv_ic;
1056 struct ieee80211_channel *c;
1062 * Strictly speaking we only use freq for now,
1063 * however later on we may wish to just store
1064 * the ieee for verification.
1066 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1068 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1072 * If the rx status contains a valid ieee/freq, then
1073 * ensure we populate the correct channel information
1074 * in rxchan before passing it up to the scan infrastructure.
1075 * Offload NICs will pass up beacons from all channels
1076 * during background scans.
1079 /* Determine a band */
1080 /* XXX should be done by the driver? */
1081 if (rxs->c_freq < 3000) {
1082 flags = IEEE80211_CHAN_G;
1084 flags = IEEE80211_CHAN_A;
1087 /* Channel lookup */
1088 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1090 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1091 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1102 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1104 #define ADD(_ic, _s, _o) \
1105 ifmedia_add(media, \
1106 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1107 static const u_int mopts[IEEE80211_MODE_MAX] = {
1108 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1109 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1110 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1111 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1112 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1113 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1114 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1115 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1116 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1117 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1118 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1119 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1125 ADD(ic, mword, mopt); /* STA mode has no cap */
1126 if (caps & IEEE80211_C_IBSS)
1127 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1128 if (caps & IEEE80211_C_HOSTAP)
1129 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1130 if (caps & IEEE80211_C_AHDEMO)
1131 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1132 if (caps & IEEE80211_C_MONITOR)
1133 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1134 if (caps & IEEE80211_C_WDS)
1135 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1136 if (caps & IEEE80211_C_MBSS)
1137 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1142 * Setup the media data structures according to the channel and
1146 ieee80211_media_setup(struct ieee80211com *ic,
1147 struct ifmedia *media, int caps, int addsta,
1148 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1150 int i, j, rate, maxrate, mword, r;
1151 enum ieee80211_phymode mode;
1152 const struct ieee80211_rateset *rs;
1153 struct ieee80211_rateset allrates;
1156 * Fill in media characteristics.
1158 ifmedia_init(media, 0, media_change, media_stat);
1161 * Add media for legacy operating modes.
1163 memset(&allrates, 0, sizeof(allrates));
1164 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1165 if (isclr(ic->ic_modecaps, mode))
1167 addmedia(media, caps, addsta, mode, IFM_AUTO);
1168 if (mode == IEEE80211_MODE_AUTO)
1170 rs = &ic->ic_sup_rates[mode];
1171 for (i = 0; i < rs->rs_nrates; i++) {
1172 rate = rs->rs_rates[i];
1173 mword = ieee80211_rate2media(ic, rate, mode);
1176 addmedia(media, caps, addsta, mode, mword);
1178 * Add legacy rate to the collection of all rates.
1180 r = rate & IEEE80211_RATE_VAL;
1181 for (j = 0; j < allrates.rs_nrates; j++)
1182 if (allrates.rs_rates[j] == r)
1184 if (j == allrates.rs_nrates) {
1185 /* unique, add to the set */
1186 allrates.rs_rates[j] = r;
1187 allrates.rs_nrates++;
1189 rate = (rate & IEEE80211_RATE_VAL) / 2;
1194 for (i = 0; i < allrates.rs_nrates; i++) {
1195 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1196 IEEE80211_MODE_AUTO);
1199 /* NB: remove media options from mword */
1200 addmedia(media, caps, addsta,
1201 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1204 * Add HT/11n media. Note that we do not have enough
1205 * bits in the media subtype to express the MCS so we
1206 * use a "placeholder" media subtype and any fixed MCS
1207 * must be specified with a different mechanism.
1209 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1210 if (isclr(ic->ic_modecaps, mode))
1212 addmedia(media, caps, addsta, mode, IFM_AUTO);
1213 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1215 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1216 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1217 addmedia(media, caps, addsta,
1218 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1219 i = ic->ic_txstream * 8 - 1;
1220 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1221 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1222 rate = ieee80211_htrates[i].ht40_rate_400ns;
1223 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1224 rate = ieee80211_htrates[i].ht40_rate_800ns;
1225 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1226 rate = ieee80211_htrates[i].ht20_rate_400ns;
1228 rate = ieee80211_htrates[i].ht20_rate_800ns;
1235 /* XXX inline or eliminate? */
1236 const struct ieee80211_rateset *
1237 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1239 /* XXX does this work for 11ng basic rates? */
1240 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1244 ieee80211_announce(struct ieee80211com *ic)
1247 enum ieee80211_phymode mode;
1248 const struct ieee80211_rateset *rs;
1250 /* NB: skip AUTO since it has no rates */
1251 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1252 if (isclr(ic->ic_modecaps, mode))
1254 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1255 rs = &ic->ic_sup_rates[mode];
1256 for (i = 0; i < rs->rs_nrates; i++) {
1257 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1260 rate = ieee80211_media2rate(mword);
1261 printf("%s%d%sMbps", (i != 0 ? " " : ""),
1262 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1266 ieee80211_ht_announce(ic);
1270 ieee80211_announce_channels(struct ieee80211com *ic)
1272 const struct ieee80211_channel *c;
1276 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1277 for (i = 0; i < ic->ic_nchans; i++) {
1278 c = &ic->ic_channels[i];
1279 if (IEEE80211_IS_CHAN_ST(c))
1281 else if (IEEE80211_IS_CHAN_108A(c))
1283 else if (IEEE80211_IS_CHAN_108G(c))
1285 else if (IEEE80211_IS_CHAN_HT(c))
1287 else if (IEEE80211_IS_CHAN_A(c))
1289 else if (IEEE80211_IS_CHAN_ANYG(c))
1291 else if (IEEE80211_IS_CHAN_B(c))
1295 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1297 else if (IEEE80211_IS_CHAN_HALF(c))
1299 else if (IEEE80211_IS_CHAN_QUARTER(c))
1303 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1304 , c->ic_ieee, c->ic_freq, type
1306 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1307 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1309 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1310 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1316 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1318 switch (IFM_MODE(ime->ifm_media)) {
1319 case IFM_IEEE80211_11A:
1320 *mode = IEEE80211_MODE_11A;
1322 case IFM_IEEE80211_11B:
1323 *mode = IEEE80211_MODE_11B;
1325 case IFM_IEEE80211_11G:
1326 *mode = IEEE80211_MODE_11G;
1328 case IFM_IEEE80211_FH:
1329 *mode = IEEE80211_MODE_FH;
1331 case IFM_IEEE80211_11NA:
1332 *mode = IEEE80211_MODE_11NA;
1334 case IFM_IEEE80211_11NG:
1335 *mode = IEEE80211_MODE_11NG;
1338 *mode = IEEE80211_MODE_AUTO;
1344 * Turbo mode is an ``option''.
1345 * XXX does not apply to AUTO
1347 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1348 if (*mode == IEEE80211_MODE_11A) {
1349 if (flags & IEEE80211_F_TURBOP)
1350 *mode = IEEE80211_MODE_TURBO_A;
1352 *mode = IEEE80211_MODE_STURBO_A;
1353 } else if (*mode == IEEE80211_MODE_11G)
1354 *mode = IEEE80211_MODE_TURBO_G;
1363 * Handle a media change request on the vap interface.
1366 ieee80211_media_change(struct ifnet *ifp)
1368 struct ieee80211vap *vap = ifp->if_softc;
1369 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1372 if (!media2mode(ime, vap->iv_flags, &newmode))
1374 if (vap->iv_des_mode != newmode) {
1375 vap->iv_des_mode = newmode;
1376 /* XXX kick state machine if up+running */
1382 * Common code to calculate the media status word
1383 * from the operating mode and channel state.
1386 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1390 status = IFM_IEEE80211;
1392 case IEEE80211_M_STA:
1394 case IEEE80211_M_IBSS:
1395 status |= IFM_IEEE80211_ADHOC;
1397 case IEEE80211_M_HOSTAP:
1398 status |= IFM_IEEE80211_HOSTAP;
1400 case IEEE80211_M_MONITOR:
1401 status |= IFM_IEEE80211_MONITOR;
1403 case IEEE80211_M_AHDEMO:
1404 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1406 case IEEE80211_M_WDS:
1407 status |= IFM_IEEE80211_WDS;
1409 case IEEE80211_M_MBSS:
1410 status |= IFM_IEEE80211_MBSS;
1413 if (IEEE80211_IS_CHAN_HTA(chan)) {
1414 status |= IFM_IEEE80211_11NA;
1415 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1416 status |= IFM_IEEE80211_11NG;
1417 } else if (IEEE80211_IS_CHAN_A(chan)) {
1418 status |= IFM_IEEE80211_11A;
1419 } else if (IEEE80211_IS_CHAN_B(chan)) {
1420 status |= IFM_IEEE80211_11B;
1421 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1422 status |= IFM_IEEE80211_11G;
1423 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1424 status |= IFM_IEEE80211_FH;
1426 /* XXX else complain? */
1428 if (IEEE80211_IS_CHAN_TURBO(chan))
1429 status |= IFM_IEEE80211_TURBO;
1431 if (IEEE80211_IS_CHAN_HT20(chan))
1432 status |= IFM_IEEE80211_HT20;
1433 if (IEEE80211_IS_CHAN_HT40(chan))
1434 status |= IFM_IEEE80211_HT40;
1440 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1442 struct ieee80211vap *vap = ifp->if_softc;
1443 struct ieee80211com *ic = vap->iv_ic;
1444 enum ieee80211_phymode mode;
1446 imr->ifm_status = IFM_AVALID;
1448 * NB: use the current channel's mode to lock down a xmit
1449 * rate only when running; otherwise we may have a mismatch
1450 * in which case the rate will not be convertible.
1452 if (vap->iv_state == IEEE80211_S_RUN ||
1453 vap->iv_state == IEEE80211_S_SLEEP) {
1454 imr->ifm_status |= IFM_ACTIVE;
1455 mode = ieee80211_chan2mode(ic->ic_curchan);
1457 mode = IEEE80211_MODE_AUTO;
1458 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1460 * Calculate a current rate if possible.
1462 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1464 * A fixed rate is set, report that.
1466 imr->ifm_active |= ieee80211_rate2media(ic,
1467 vap->iv_txparms[mode].ucastrate, mode);
1468 } else if (vap->iv_opmode == IEEE80211_M_STA) {
1470 * In station mode report the current transmit rate.
1472 imr->ifm_active |= ieee80211_rate2media(ic,
1473 vap->iv_bss->ni_txrate, mode);
1475 imr->ifm_active |= IFM_AUTO;
1476 if (imr->ifm_status & IFM_ACTIVE)
1477 imr->ifm_current = imr->ifm_active;
1481 * Set the current phy mode and recalculate the active channel
1482 * set based on the available channels for this mode. Also
1483 * select a new default/current channel if the current one is
1484 * inappropriate for this mode.
1487 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1490 * Adjust basic rates in 11b/11g supported rate set.
1491 * Note that if operating on a hal/quarter rate channel
1492 * this is a noop as those rates sets are different
1495 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1496 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1498 ic->ic_curmode = mode;
1499 ieee80211_reset_erp(ic); /* reset ERP state */
1505 * Return the phy mode for with the specified channel.
1507 enum ieee80211_phymode
1508 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1511 if (IEEE80211_IS_CHAN_HTA(chan))
1512 return IEEE80211_MODE_11NA;
1513 else if (IEEE80211_IS_CHAN_HTG(chan))
1514 return IEEE80211_MODE_11NG;
1515 else if (IEEE80211_IS_CHAN_108G(chan))
1516 return IEEE80211_MODE_TURBO_G;
1517 else if (IEEE80211_IS_CHAN_ST(chan))
1518 return IEEE80211_MODE_STURBO_A;
1519 else if (IEEE80211_IS_CHAN_TURBO(chan))
1520 return IEEE80211_MODE_TURBO_A;
1521 else if (IEEE80211_IS_CHAN_HALF(chan))
1522 return IEEE80211_MODE_HALF;
1523 else if (IEEE80211_IS_CHAN_QUARTER(chan))
1524 return IEEE80211_MODE_QUARTER;
1525 else if (IEEE80211_IS_CHAN_A(chan))
1526 return IEEE80211_MODE_11A;
1527 else if (IEEE80211_IS_CHAN_ANYG(chan))
1528 return IEEE80211_MODE_11G;
1529 else if (IEEE80211_IS_CHAN_B(chan))
1530 return IEEE80211_MODE_11B;
1531 else if (IEEE80211_IS_CHAN_FHSS(chan))
1532 return IEEE80211_MODE_FH;
1534 /* NB: should not get here */
1535 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1536 __func__, chan->ic_freq, chan->ic_flags);
1537 return IEEE80211_MODE_11B;
1541 u_int match; /* rate + mode */
1542 u_int media; /* if_media rate */
1546 findmedia(const struct ratemedia rates[], int n, u_int match)
1550 for (i = 0; i < n; i++)
1551 if (rates[i].match == match)
1552 return rates[i].media;
1557 * Convert IEEE80211 rate value to ifmedia subtype.
1558 * Rate is either a legacy rate in units of 0.5Mbps
1562 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1564 static const struct ratemedia rates[] = {
1565 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1566 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1567 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1568 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1569 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1570 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1571 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1572 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1573 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1574 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1575 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1576 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1577 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1578 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1579 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1580 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1581 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1582 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1583 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1584 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1585 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1586 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1587 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1588 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1589 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1590 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1591 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1592 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1593 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1594 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1595 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1597 static const struct ratemedia htrates[] = {
1598 { 0, IFM_IEEE80211_MCS },
1599 { 1, IFM_IEEE80211_MCS },
1600 { 2, IFM_IEEE80211_MCS },
1601 { 3, IFM_IEEE80211_MCS },
1602 { 4, IFM_IEEE80211_MCS },
1603 { 5, IFM_IEEE80211_MCS },
1604 { 6, IFM_IEEE80211_MCS },
1605 { 7, IFM_IEEE80211_MCS },
1606 { 8, IFM_IEEE80211_MCS },
1607 { 9, IFM_IEEE80211_MCS },
1608 { 10, IFM_IEEE80211_MCS },
1609 { 11, IFM_IEEE80211_MCS },
1610 { 12, IFM_IEEE80211_MCS },
1611 { 13, IFM_IEEE80211_MCS },
1612 { 14, IFM_IEEE80211_MCS },
1613 { 15, IFM_IEEE80211_MCS },
1614 { 16, IFM_IEEE80211_MCS },
1615 { 17, IFM_IEEE80211_MCS },
1616 { 18, IFM_IEEE80211_MCS },
1617 { 19, IFM_IEEE80211_MCS },
1618 { 20, IFM_IEEE80211_MCS },
1619 { 21, IFM_IEEE80211_MCS },
1620 { 22, IFM_IEEE80211_MCS },
1621 { 23, IFM_IEEE80211_MCS },
1622 { 24, IFM_IEEE80211_MCS },
1623 { 25, IFM_IEEE80211_MCS },
1624 { 26, IFM_IEEE80211_MCS },
1625 { 27, IFM_IEEE80211_MCS },
1626 { 28, IFM_IEEE80211_MCS },
1627 { 29, IFM_IEEE80211_MCS },
1628 { 30, IFM_IEEE80211_MCS },
1629 { 31, IFM_IEEE80211_MCS },
1630 { 32, IFM_IEEE80211_MCS },
1631 { 33, IFM_IEEE80211_MCS },
1632 { 34, IFM_IEEE80211_MCS },
1633 { 35, IFM_IEEE80211_MCS },
1634 { 36, IFM_IEEE80211_MCS },
1635 { 37, IFM_IEEE80211_MCS },
1636 { 38, IFM_IEEE80211_MCS },
1637 { 39, IFM_IEEE80211_MCS },
1638 { 40, IFM_IEEE80211_MCS },
1639 { 41, IFM_IEEE80211_MCS },
1640 { 42, IFM_IEEE80211_MCS },
1641 { 43, IFM_IEEE80211_MCS },
1642 { 44, IFM_IEEE80211_MCS },
1643 { 45, IFM_IEEE80211_MCS },
1644 { 46, IFM_IEEE80211_MCS },
1645 { 47, IFM_IEEE80211_MCS },
1646 { 48, IFM_IEEE80211_MCS },
1647 { 49, IFM_IEEE80211_MCS },
1648 { 50, IFM_IEEE80211_MCS },
1649 { 51, IFM_IEEE80211_MCS },
1650 { 52, IFM_IEEE80211_MCS },
1651 { 53, IFM_IEEE80211_MCS },
1652 { 54, IFM_IEEE80211_MCS },
1653 { 55, IFM_IEEE80211_MCS },
1654 { 56, IFM_IEEE80211_MCS },
1655 { 57, IFM_IEEE80211_MCS },
1656 { 58, IFM_IEEE80211_MCS },
1657 { 59, IFM_IEEE80211_MCS },
1658 { 60, IFM_IEEE80211_MCS },
1659 { 61, IFM_IEEE80211_MCS },
1660 { 62, IFM_IEEE80211_MCS },
1661 { 63, IFM_IEEE80211_MCS },
1662 { 64, IFM_IEEE80211_MCS },
1663 { 65, IFM_IEEE80211_MCS },
1664 { 66, IFM_IEEE80211_MCS },
1665 { 67, IFM_IEEE80211_MCS },
1666 { 68, IFM_IEEE80211_MCS },
1667 { 69, IFM_IEEE80211_MCS },
1668 { 70, IFM_IEEE80211_MCS },
1669 { 71, IFM_IEEE80211_MCS },
1670 { 72, IFM_IEEE80211_MCS },
1671 { 73, IFM_IEEE80211_MCS },
1672 { 74, IFM_IEEE80211_MCS },
1673 { 75, IFM_IEEE80211_MCS },
1674 { 76, IFM_IEEE80211_MCS },
1679 * Check 11n rates first for match as an MCS.
1681 if (mode == IEEE80211_MODE_11NA) {
1682 if (rate & IEEE80211_RATE_MCS) {
1683 rate &= ~IEEE80211_RATE_MCS;
1684 m = findmedia(htrates, nitems(htrates), rate);
1686 return m | IFM_IEEE80211_11NA;
1688 } else if (mode == IEEE80211_MODE_11NG) {
1689 /* NB: 12 is ambiguous, it will be treated as an MCS */
1690 if (rate & IEEE80211_RATE_MCS) {
1691 rate &= ~IEEE80211_RATE_MCS;
1692 m = findmedia(htrates, nitems(htrates), rate);
1694 return m | IFM_IEEE80211_11NG;
1697 rate &= IEEE80211_RATE_VAL;
1699 case IEEE80211_MODE_11A:
1700 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
1701 case IEEE80211_MODE_QUARTER:
1702 case IEEE80211_MODE_11NA:
1703 case IEEE80211_MODE_TURBO_A:
1704 case IEEE80211_MODE_STURBO_A:
1705 return findmedia(rates, nitems(rates),
1706 rate | IFM_IEEE80211_11A);
1707 case IEEE80211_MODE_11B:
1708 return findmedia(rates, nitems(rates),
1709 rate | IFM_IEEE80211_11B);
1710 case IEEE80211_MODE_FH:
1711 return findmedia(rates, nitems(rates),
1712 rate | IFM_IEEE80211_FH);
1713 case IEEE80211_MODE_AUTO:
1714 /* NB: ic may be NULL for some drivers */
1715 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1716 return findmedia(rates, nitems(rates),
1717 rate | IFM_IEEE80211_FH);
1718 /* NB: hack, 11g matches both 11b+11a rates */
1720 case IEEE80211_MODE_11G:
1721 case IEEE80211_MODE_11NG:
1722 case IEEE80211_MODE_TURBO_G:
1723 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1729 ieee80211_media2rate(int mword)
1731 static const int ieeerates[] = {
1735 2, /* IFM_IEEE80211_FH1 */
1736 4, /* IFM_IEEE80211_FH2 */
1737 2, /* IFM_IEEE80211_DS1 */
1738 4, /* IFM_IEEE80211_DS2 */
1739 11, /* IFM_IEEE80211_DS5 */
1740 22, /* IFM_IEEE80211_DS11 */
1741 44, /* IFM_IEEE80211_DS22 */
1742 12, /* IFM_IEEE80211_OFDM6 */
1743 18, /* IFM_IEEE80211_OFDM9 */
1744 24, /* IFM_IEEE80211_OFDM12 */
1745 36, /* IFM_IEEE80211_OFDM18 */
1746 48, /* IFM_IEEE80211_OFDM24 */
1747 72, /* IFM_IEEE80211_OFDM36 */
1748 96, /* IFM_IEEE80211_OFDM48 */
1749 108, /* IFM_IEEE80211_OFDM54 */
1750 144, /* IFM_IEEE80211_OFDM72 */
1751 0, /* IFM_IEEE80211_DS354k */
1752 0, /* IFM_IEEE80211_DS512k */
1753 6, /* IFM_IEEE80211_OFDM3 */
1754 9, /* IFM_IEEE80211_OFDM4 */
1755 54, /* IFM_IEEE80211_OFDM27 */
1756 -1, /* IFM_IEEE80211_MCS */
1758 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
1759 ieeerates[IFM_SUBTYPE(mword)] : 0;
1763 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1764 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1766 #define mix(a, b, c) \
1768 a -= b; a -= c; a ^= (c >> 13); \
1769 b -= c; b -= a; b ^= (a << 8); \
1770 c -= a; c -= b; c ^= (b >> 13); \
1771 a -= b; a -= c; a ^= (c >> 12); \
1772 b -= c; b -= a; b ^= (a << 16); \
1773 c -= a; c -= b; c ^= (b >> 5); \
1774 a -= b; a -= c; a ^= (c >> 3); \
1775 b -= c; b -= a; b ^= (a << 10); \
1776 c -= a; c -= b; c ^= (b >> 15); \
1777 } while (/*CONSTCOND*/0)
1780 ieee80211_mac_hash(const struct ieee80211com *ic,
1781 const uint8_t addr[IEEE80211_ADDR_LEN])
1783 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1799 ieee80211_channel_type_char(const struct ieee80211_channel *c)
1801 if (IEEE80211_IS_CHAN_ST(c))
1803 if (IEEE80211_IS_CHAN_108A(c))
1805 if (IEEE80211_IS_CHAN_108G(c))
1807 if (IEEE80211_IS_CHAN_HT(c))
1809 if (IEEE80211_IS_CHAN_A(c))
1811 if (IEEE80211_IS_CHAN_ANYG(c))
1813 if (IEEE80211_IS_CHAN_B(c))