2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2008 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 protocol support.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
46 #include <net/if_var.h>
47 #include <net/if_media.h>
48 #include <net/ethernet.h> /* XXX for ether_sprintf */
50 #include <net80211/ieee80211_var.h>
51 #include <net80211/ieee80211_adhoc.h>
52 #include <net80211/ieee80211_sta.h>
53 #include <net80211/ieee80211_hostap.h>
54 #include <net80211/ieee80211_wds.h>
55 #ifdef IEEE80211_SUPPORT_MESH
56 #include <net80211/ieee80211_mesh.h>
58 #include <net80211/ieee80211_monitor.h>
59 #include <net80211/ieee80211_input.h>
62 #define AGGRESSIVE_MODE_SWITCH_HYSTERESIS 3 /* pkts / 100ms */
63 #define HIGH_PRI_SWITCH_THRESH 10 /* pkts / 100ms */
65 const char *ieee80211_mgt_subtype_name[] = {
66 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp",
67 "probe_req", "probe_resp", "reserved#6", "reserved#7",
68 "beacon", "atim", "disassoc", "auth",
69 "deauth", "action", "action_noack", "reserved#15"
71 const char *ieee80211_ctl_subtype_name[] = {
72 "reserved#0", "reserved#1", "reserved#2", "reserved#3",
73 "reserved#3", "reserved#5", "reserved#6", "reserved#7",
74 "reserved#8", "reserved#9", "ps_poll", "rts",
75 "cts", "ack", "cf_end", "cf_end_ack"
77 const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
78 "IBSS", /* IEEE80211_M_IBSS */
79 "STA", /* IEEE80211_M_STA */
80 "WDS", /* IEEE80211_M_WDS */
81 "AHDEMO", /* IEEE80211_M_AHDEMO */
82 "HOSTAP", /* IEEE80211_M_HOSTAP */
83 "MONITOR", /* IEEE80211_M_MONITOR */
84 "MBSS" /* IEEE80211_M_MBSS */
86 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
87 "INIT", /* IEEE80211_S_INIT */
88 "SCAN", /* IEEE80211_S_SCAN */
89 "AUTH", /* IEEE80211_S_AUTH */
90 "ASSOC", /* IEEE80211_S_ASSOC */
91 "CAC", /* IEEE80211_S_CAC */
92 "RUN", /* IEEE80211_S_RUN */
93 "CSA", /* IEEE80211_S_CSA */
94 "SLEEP", /* IEEE80211_S_SLEEP */
96 const char *ieee80211_wme_acnames[] = {
104 static void beacon_miss(void *, int);
105 static void beacon_swmiss(void *, int);
106 static void parent_updown(void *, int);
107 static void update_mcast(void *, int);
108 static void update_promisc(void *, int);
109 static void update_channel(void *, int);
110 static void update_chw(void *, int);
111 static void update_wme(void *, int);
112 static void restart_vaps(void *, int);
113 static void ieee80211_newstate_cb(void *, int);
116 null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
117 const struct ieee80211_bpf_params *params)
120 ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
126 ieee80211_proto_attach(struct ieee80211com *ic)
130 /* override the 802.3 setting */
131 hdrlen = ic->ic_headroom
132 + sizeof(struct ieee80211_qosframe_addr4)
133 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
134 + IEEE80211_WEP_EXTIVLEN;
135 /* XXX no way to recalculate on ifdetach */
136 if (ALIGN(hdrlen) > max_linkhdr) {
137 /* XXX sanity check... */
138 max_linkhdr = ALIGN(hdrlen);
139 max_hdr = max_linkhdr + max_protohdr;
140 max_datalen = MHLEN - max_hdr;
142 ic->ic_protmode = IEEE80211_PROT_CTSONLY;
144 TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
145 TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
146 TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
147 TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
148 TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
149 TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
150 TASK_INIT(&ic->ic_wme_task, 0, update_wme, ic);
151 TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
153 ic->ic_wme.wme_hipri_switch_hysteresis =
154 AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
156 /* initialize management frame handlers */
157 ic->ic_send_mgmt = ieee80211_send_mgmt;
158 ic->ic_raw_xmit = null_raw_xmit;
160 ieee80211_adhoc_attach(ic);
161 ieee80211_sta_attach(ic);
162 ieee80211_wds_attach(ic);
163 ieee80211_hostap_attach(ic);
164 #ifdef IEEE80211_SUPPORT_MESH
165 ieee80211_mesh_attach(ic);
167 ieee80211_monitor_attach(ic);
171 ieee80211_proto_detach(struct ieee80211com *ic)
173 ieee80211_monitor_detach(ic);
174 #ifdef IEEE80211_SUPPORT_MESH
175 ieee80211_mesh_detach(ic);
177 ieee80211_hostap_detach(ic);
178 ieee80211_wds_detach(ic);
179 ieee80211_adhoc_detach(ic);
180 ieee80211_sta_detach(ic);
184 null_update_beacon(struct ieee80211vap *vap, int item)
189 ieee80211_proto_vattach(struct ieee80211vap *vap)
191 struct ieee80211com *ic = vap->iv_ic;
192 struct ifnet *ifp = vap->iv_ifp;
195 /* override the 802.3 setting */
196 ifp->if_hdrlen = ic->ic_headroom
197 + sizeof(struct ieee80211_qosframe_addr4)
198 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
199 + IEEE80211_WEP_EXTIVLEN;
201 vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
202 vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
203 vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
204 callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
205 callout_init(&vap->iv_mgtsend, 1);
206 TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
207 TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
209 * Install default tx rate handling: no fixed rate, lowest
210 * supported rate for mgmt and multicast frames. Default
211 * max retry count. These settings can be changed by the
212 * driver and/or user applications.
214 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
215 const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
217 vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
220 * Setting the management rate to MCS 0 assumes that the
221 * BSS Basic rate set is empty and the BSS Basic MCS set
224 * Since we're not checking this, default to the lowest
225 * defined rate for this mode.
227 * At least one 11n AP (DLINK DIR-825) is reported to drop
228 * some MCS management traffic (eg BA response frames.)
230 * See also: 9.6.0 of the 802.11n-2009 specification.
233 if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
234 vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
235 vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
237 vap->iv_txparms[i].mgmtrate =
238 rs->rs_rates[0] & IEEE80211_RATE_VAL;
239 vap->iv_txparms[i].mcastrate =
240 rs->rs_rates[0] & IEEE80211_RATE_VAL;
243 vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
244 vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
245 vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
247 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
249 vap->iv_update_beacon = null_update_beacon;
250 vap->iv_deliver_data = ieee80211_deliver_data;
252 /* attach support for operating mode */
253 ic->ic_vattach[vap->iv_opmode](vap);
257 ieee80211_proto_vdetach(struct ieee80211vap *vap)
259 #define FREEAPPIE(ie) do { \
261 IEEE80211_FREE(ie, M_80211_NODE_IE); \
264 * Detach operating mode module.
266 if (vap->iv_opdetach != NULL)
267 vap->iv_opdetach(vap);
269 * This should not be needed as we detach when reseting
270 * the state but be conservative here since the
271 * authenticator may do things like spawn kernel threads.
273 if (vap->iv_auth->ia_detach != NULL)
274 vap->iv_auth->ia_detach(vap);
276 * Detach any ACL'ator.
278 if (vap->iv_acl != NULL)
279 vap->iv_acl->iac_detach(vap);
281 FREEAPPIE(vap->iv_appie_beacon);
282 FREEAPPIE(vap->iv_appie_probereq);
283 FREEAPPIE(vap->iv_appie_proberesp);
284 FREEAPPIE(vap->iv_appie_assocreq);
285 FREEAPPIE(vap->iv_appie_assocresp);
286 FREEAPPIE(vap->iv_appie_wpa);
291 * Simple-minded authenticator module support.
294 #define IEEE80211_AUTH_MAX (IEEE80211_AUTH_WPA+1)
295 /* XXX well-known names */
296 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
297 "wlan_internal", /* IEEE80211_AUTH_NONE */
298 "wlan_internal", /* IEEE80211_AUTH_OPEN */
299 "wlan_internal", /* IEEE80211_AUTH_SHARED */
300 "wlan_xauth", /* IEEE80211_AUTH_8021X */
301 "wlan_internal", /* IEEE80211_AUTH_AUTO */
302 "wlan_xauth", /* IEEE80211_AUTH_WPA */
304 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
306 static const struct ieee80211_authenticator auth_internal = {
307 .ia_name = "wlan_internal",
310 .ia_node_join = NULL,
311 .ia_node_leave = NULL,
315 * Setup internal authenticators once; they are never unregistered.
318 ieee80211_auth_setup(void)
320 ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
321 ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
322 ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
324 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
326 const struct ieee80211_authenticator *
327 ieee80211_authenticator_get(int auth)
329 if (auth >= IEEE80211_AUTH_MAX)
331 if (authenticators[auth] == NULL)
332 ieee80211_load_module(auth_modnames[auth]);
333 return authenticators[auth];
337 ieee80211_authenticator_register(int type,
338 const struct ieee80211_authenticator *auth)
340 if (type >= IEEE80211_AUTH_MAX)
342 authenticators[type] = auth;
346 ieee80211_authenticator_unregister(int type)
349 if (type >= IEEE80211_AUTH_MAX)
351 authenticators[type] = NULL;
355 * Very simple-minded ACL module support.
357 /* XXX just one for now */
358 static const struct ieee80211_aclator *acl = NULL;
361 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
363 printf("wlan: %s acl policy registered\n", iac->iac_name);
368 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
372 printf("wlan: %s acl policy unregistered\n", iac->iac_name);
375 const struct ieee80211_aclator *
376 ieee80211_aclator_get(const char *name)
379 ieee80211_load_module("wlan_acl");
380 return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
384 ieee80211_print_essid(const uint8_t *essid, int len)
389 if (len > IEEE80211_NWID_LEN)
390 len = IEEE80211_NWID_LEN;
391 /* determine printable or not */
392 for (i = 0, p = essid; i < len; i++, p++) {
393 if (*p < ' ' || *p > 0x7e)
398 for (i = 0, p = essid; i < len; i++, p++)
403 for (i = 0, p = essid; i < len; i++, p++)
409 ieee80211_dump_pkt(struct ieee80211com *ic,
410 const uint8_t *buf, int len, int rate, int rssi)
412 const struct ieee80211_frame *wh;
415 wh = (const struct ieee80211_frame *)buf;
416 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
417 case IEEE80211_FC1_DIR_NODS:
418 printf("NODS %s", ether_sprintf(wh->i_addr2));
419 printf("->%s", ether_sprintf(wh->i_addr1));
420 printf("(%s)", ether_sprintf(wh->i_addr3));
422 case IEEE80211_FC1_DIR_TODS:
423 printf("TODS %s", ether_sprintf(wh->i_addr2));
424 printf("->%s", ether_sprintf(wh->i_addr3));
425 printf("(%s)", ether_sprintf(wh->i_addr1));
427 case IEEE80211_FC1_DIR_FROMDS:
428 printf("FRDS %s", ether_sprintf(wh->i_addr3));
429 printf("->%s", ether_sprintf(wh->i_addr1));
430 printf("(%s)", ether_sprintf(wh->i_addr2));
432 case IEEE80211_FC1_DIR_DSTODS:
433 printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
434 printf("->%s", ether_sprintf(wh->i_addr3));
435 printf("(%s", ether_sprintf(wh->i_addr2));
436 printf("->%s)", ether_sprintf(wh->i_addr1));
439 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
440 case IEEE80211_FC0_TYPE_DATA:
443 case IEEE80211_FC0_TYPE_MGT:
444 printf(" %s", ieee80211_mgt_subtype_name[
445 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
446 >> IEEE80211_FC0_SUBTYPE_SHIFT]);
449 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
452 if (IEEE80211_QOS_HAS_SEQ(wh)) {
453 const struct ieee80211_qosframe *qwh =
454 (const struct ieee80211_qosframe *)buf;
455 printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
456 qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
458 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
461 off = ieee80211_anyhdrspace(ic, wh);
462 printf(" WEP [IV %.02x %.02x %.02x",
463 buf[off+0], buf[off+1], buf[off+2]);
464 if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
465 printf(" %.02x %.02x %.02x",
466 buf[off+4], buf[off+5], buf[off+6]);
467 printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
470 printf(" %dM", rate / 2);
472 printf(" +%d", rssi);
475 for (i = 0; i < len; i++) {
478 printf("%02x", buf[i]);
485 findrix(const struct ieee80211_rateset *rs, int r)
489 for (i = 0; i < rs->rs_nrates; i++)
490 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
496 ieee80211_fix_rate(struct ieee80211_node *ni,
497 struct ieee80211_rateset *nrs, int flags)
499 struct ieee80211vap *vap = ni->ni_vap;
500 struct ieee80211com *ic = ni->ni_ic;
501 int i, j, rix, error;
502 int okrate, badrate, fixedrate, ucastrate;
503 const struct ieee80211_rateset *srs;
507 okrate = badrate = 0;
508 ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
509 if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
511 * Workaround awkwardness with fixed rate. We are called
512 * to check both the legacy rate set and the HT rate set
513 * but we must apply any legacy fixed rate check only to the
514 * legacy rate set and vice versa. We cannot tell what type
515 * of rate set we've been given (legacy or HT) but we can
516 * distinguish the fixed rate type (MCS have 0x80 set).
517 * So to deal with this the caller communicates whether to
518 * check MCS or legacy rate using the flags and we use the
519 * type of any fixed rate to avoid applying an MCS to a
520 * legacy rate and vice versa.
522 if (ucastrate & 0x80) {
523 if (flags & IEEE80211_F_DOFRATE)
524 flags &= ~IEEE80211_F_DOFRATE;
525 } else if ((ucastrate & 0x80) == 0) {
526 if (flags & IEEE80211_F_DOFMCS)
527 flags &= ~IEEE80211_F_DOFMCS;
529 /* NB: required to make MCS match below work */
530 ucastrate &= IEEE80211_RATE_VAL;
532 fixedrate = IEEE80211_FIXED_RATE_NONE;
534 * XXX we are called to process both MCS and legacy rates;
535 * we must use the appropriate basic rate set or chaos will
536 * ensue; for now callers that want MCS must supply
537 * IEEE80211_F_DOBRS; at some point we'll need to split this
538 * function so there are two variants, one for MCS and one
541 if (flags & IEEE80211_F_DOBRS)
542 srs = (const struct ieee80211_rateset *)
543 ieee80211_get_suphtrates(ic, ni->ni_chan);
545 srs = ieee80211_get_suprates(ic, ni->ni_chan);
546 for (i = 0; i < nrs->rs_nrates; ) {
547 if (flags & IEEE80211_F_DOSORT) {
551 for (j = i + 1; j < nrs->rs_nrates; j++) {
552 if (IEEE80211_RV(nrs->rs_rates[i]) >
553 IEEE80211_RV(nrs->rs_rates[j])) {
554 r = nrs->rs_rates[i];
555 nrs->rs_rates[i] = nrs->rs_rates[j];
556 nrs->rs_rates[j] = r;
560 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
563 * Check for fixed rate.
568 * Check against supported rates.
570 rix = findrix(srs, r);
571 if (flags & IEEE80211_F_DONEGO) {
574 * A rate in the node's rate set is not
575 * supported. If this is a basic rate and we
576 * are operating as a STA then this is an error.
577 * Otherwise we just discard/ignore the rate.
579 if ((flags & IEEE80211_F_JOIN) &&
580 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
582 } else if ((flags & IEEE80211_F_JOIN) == 0) {
584 * Overwrite with the supported rate
585 * value so any basic rate bit is set.
587 nrs->rs_rates[i] = srs->rs_rates[rix];
590 if ((flags & IEEE80211_F_DODEL) && rix < 0) {
592 * Delete unacceptable rates.
595 for (j = i; j < nrs->rs_nrates; j++)
596 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
597 nrs->rs_rates[j] = 0;
601 okrate = nrs->rs_rates[i];
604 if (okrate == 0 || error != 0 ||
605 ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
606 fixedrate != ucastrate)) {
607 IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
608 "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
609 "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
610 return badrate | IEEE80211_RATE_BASIC;
612 return IEEE80211_RV(okrate);
616 * Reset 11g-related state.
619 ieee80211_reset_erp(struct ieee80211com *ic)
621 ic->ic_flags &= ~IEEE80211_F_USEPROT;
622 ic->ic_nonerpsta = 0;
623 ic->ic_longslotsta = 0;
625 * Short slot time is enabled only when operating in 11g
626 * and not in an IBSS. We must also honor whether or not
627 * the driver is capable of doing it.
629 ieee80211_set_shortslottime(ic,
630 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
631 IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
632 (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
633 ic->ic_opmode == IEEE80211_M_HOSTAP &&
634 (ic->ic_caps & IEEE80211_C_SHSLOT)));
636 * Set short preamble and ERP barker-preamble flags.
638 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
639 (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
640 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
641 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
643 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
644 ic->ic_flags |= IEEE80211_F_USEBARKER;
649 * Set the short slot time state and notify the driver.
652 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
655 ic->ic_flags |= IEEE80211_F_SHSLOT;
657 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
659 if (ic->ic_updateslot != NULL)
660 ic->ic_updateslot(ic);
664 * Check if the specified rate set supports ERP.
665 * NB: the rate set is assumed to be sorted.
668 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
670 static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
673 if (rs->rs_nrates < nitems(rates))
675 for (i = 0; i < nitems(rates); i++) {
676 for (j = 0; j < rs->rs_nrates; j++) {
677 int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
691 * Mark the basic rates for the rate table based on the
692 * operating mode. For real 11g we mark all the 11b rates
693 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
694 * 11b rates. There's also a pseudo 11a-mode used to mark only
695 * the basic OFDM rates.
698 setbasicrates(struct ieee80211_rateset *rs,
699 enum ieee80211_phymode mode, int add)
701 static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
702 [IEEE80211_MODE_11A] = { 3, { 12, 24, 48 } },
703 [IEEE80211_MODE_11B] = { 2, { 2, 4 } },
705 [IEEE80211_MODE_11G] = { 4, { 2, 4, 11, 22 } },
706 [IEEE80211_MODE_TURBO_A] = { 3, { 12, 24, 48 } },
707 [IEEE80211_MODE_TURBO_G] = { 4, { 2, 4, 11, 22 } },
708 [IEEE80211_MODE_STURBO_A] = { 3, { 12, 24, 48 } },
709 [IEEE80211_MODE_HALF] = { 3, { 6, 12, 24 } },
710 [IEEE80211_MODE_QUARTER] = { 3, { 3, 6, 12 } },
711 [IEEE80211_MODE_11NA] = { 3, { 12, 24, 48 } },
713 [IEEE80211_MODE_11NG] = { 4, { 2, 4, 11, 22 } },
717 for (i = 0; i < rs->rs_nrates; i++) {
719 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
720 for (j = 0; j < basic[mode].rs_nrates; j++)
721 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
722 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
729 * Set the basic rates in a rate set.
732 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
733 enum ieee80211_phymode mode)
735 setbasicrates(rs, mode, 0);
739 * Add basic rates to a rate set.
742 ieee80211_addbasicrates(struct ieee80211_rateset *rs,
743 enum ieee80211_phymode mode)
745 setbasicrates(rs, mode, 1);
749 * WME protocol support.
751 * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
752 * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
753 * Draft 2.0 Test Plan (Appendix D).
755 * Static/Dynamic Turbo mode settings come from Atheros.
757 typedef struct phyParamType {
765 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
766 [IEEE80211_MODE_AUTO] = { 3, 4, 6, 0, 0 },
767 [IEEE80211_MODE_11A] = { 3, 4, 6, 0, 0 },
768 [IEEE80211_MODE_11B] = { 3, 4, 6, 0, 0 },
769 [IEEE80211_MODE_11G] = { 3, 4, 6, 0, 0 },
770 [IEEE80211_MODE_FH] = { 3, 4, 6, 0, 0 },
771 [IEEE80211_MODE_TURBO_A]= { 2, 3, 5, 0, 0 },
772 [IEEE80211_MODE_TURBO_G]= { 2, 3, 5, 0, 0 },
773 [IEEE80211_MODE_STURBO_A]={ 2, 3, 5, 0, 0 },
774 [IEEE80211_MODE_HALF] = { 3, 4, 6, 0, 0 },
775 [IEEE80211_MODE_QUARTER]= { 3, 4, 6, 0, 0 },
776 [IEEE80211_MODE_11NA] = { 3, 4, 6, 0, 0 },
777 [IEEE80211_MODE_11NG] = { 3, 4, 6, 0, 0 },
779 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
780 [IEEE80211_MODE_AUTO] = { 7, 4, 10, 0, 0 },
781 [IEEE80211_MODE_11A] = { 7, 4, 10, 0, 0 },
782 [IEEE80211_MODE_11B] = { 7, 4, 10, 0, 0 },
783 [IEEE80211_MODE_11G] = { 7, 4, 10, 0, 0 },
784 [IEEE80211_MODE_FH] = { 7, 4, 10, 0, 0 },
785 [IEEE80211_MODE_TURBO_A]= { 7, 3, 10, 0, 0 },
786 [IEEE80211_MODE_TURBO_G]= { 7, 3, 10, 0, 0 },
787 [IEEE80211_MODE_STURBO_A]={ 7, 3, 10, 0, 0 },
788 [IEEE80211_MODE_HALF] = { 7, 4, 10, 0, 0 },
789 [IEEE80211_MODE_QUARTER]= { 7, 4, 10, 0, 0 },
790 [IEEE80211_MODE_11NA] = { 7, 4, 10, 0, 0 },
791 [IEEE80211_MODE_11NG] = { 7, 4, 10, 0, 0 },
793 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
794 [IEEE80211_MODE_AUTO] = { 1, 3, 4, 94, 0 },
795 [IEEE80211_MODE_11A] = { 1, 3, 4, 94, 0 },
796 [IEEE80211_MODE_11B] = { 1, 3, 4, 188, 0 },
797 [IEEE80211_MODE_11G] = { 1, 3, 4, 94, 0 },
798 [IEEE80211_MODE_FH] = { 1, 3, 4, 188, 0 },
799 [IEEE80211_MODE_TURBO_A]= { 1, 2, 3, 94, 0 },
800 [IEEE80211_MODE_TURBO_G]= { 1, 2, 3, 94, 0 },
801 [IEEE80211_MODE_STURBO_A]={ 1, 2, 3, 94, 0 },
802 [IEEE80211_MODE_HALF] = { 1, 3, 4, 94, 0 },
803 [IEEE80211_MODE_QUARTER]= { 1, 3, 4, 94, 0 },
804 [IEEE80211_MODE_11NA] = { 1, 3, 4, 94, 0 },
805 [IEEE80211_MODE_11NG] = { 1, 3, 4, 94, 0 },
807 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
808 [IEEE80211_MODE_AUTO] = { 1, 2, 3, 47, 0 },
809 [IEEE80211_MODE_11A] = { 1, 2, 3, 47, 0 },
810 [IEEE80211_MODE_11B] = { 1, 2, 3, 102, 0 },
811 [IEEE80211_MODE_11G] = { 1, 2, 3, 47, 0 },
812 [IEEE80211_MODE_FH] = { 1, 2, 3, 102, 0 },
813 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
814 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
815 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
816 [IEEE80211_MODE_HALF] = { 1, 2, 3, 47, 0 },
817 [IEEE80211_MODE_QUARTER]= { 1, 2, 3, 47, 0 },
818 [IEEE80211_MODE_11NA] = { 1, 2, 3, 47, 0 },
819 [IEEE80211_MODE_11NG] = { 1, 2, 3, 47, 0 },
822 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
823 [IEEE80211_MODE_AUTO] = { 3, 4, 10, 0, 0 },
824 [IEEE80211_MODE_11A] = { 3, 4, 10, 0, 0 },
825 [IEEE80211_MODE_11B] = { 3, 4, 10, 0, 0 },
826 [IEEE80211_MODE_11G] = { 3, 4, 10, 0, 0 },
827 [IEEE80211_MODE_FH] = { 3, 4, 10, 0, 0 },
828 [IEEE80211_MODE_TURBO_A]= { 2, 3, 10, 0, 0 },
829 [IEEE80211_MODE_TURBO_G]= { 2, 3, 10, 0, 0 },
830 [IEEE80211_MODE_STURBO_A]={ 2, 3, 10, 0, 0 },
831 [IEEE80211_MODE_HALF] = { 3, 4, 10, 0, 0 },
832 [IEEE80211_MODE_QUARTER]= { 3, 4, 10, 0, 0 },
833 [IEEE80211_MODE_11NA] = { 3, 4, 10, 0, 0 },
834 [IEEE80211_MODE_11NG] = { 3, 4, 10, 0, 0 },
836 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
837 [IEEE80211_MODE_AUTO] = { 2, 3, 4, 94, 0 },
838 [IEEE80211_MODE_11A] = { 2, 3, 4, 94, 0 },
839 [IEEE80211_MODE_11B] = { 2, 3, 4, 188, 0 },
840 [IEEE80211_MODE_11G] = { 2, 3, 4, 94, 0 },
841 [IEEE80211_MODE_FH] = { 2, 3, 4, 188, 0 },
842 [IEEE80211_MODE_TURBO_A]= { 2, 2, 3, 94, 0 },
843 [IEEE80211_MODE_TURBO_G]= { 2, 2, 3, 94, 0 },
844 [IEEE80211_MODE_STURBO_A]={ 2, 2, 3, 94, 0 },
845 [IEEE80211_MODE_HALF] = { 2, 3, 4, 94, 0 },
846 [IEEE80211_MODE_QUARTER]= { 2, 3, 4, 94, 0 },
847 [IEEE80211_MODE_11NA] = { 2, 3, 4, 94, 0 },
848 [IEEE80211_MODE_11NG] = { 2, 3, 4, 94, 0 },
850 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
851 [IEEE80211_MODE_AUTO] = { 2, 2, 3, 47, 0 },
852 [IEEE80211_MODE_11A] = { 2, 2, 3, 47, 0 },
853 [IEEE80211_MODE_11B] = { 2, 2, 3, 102, 0 },
854 [IEEE80211_MODE_11G] = { 2, 2, 3, 47, 0 },
855 [IEEE80211_MODE_FH] = { 2, 2, 3, 102, 0 },
856 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
857 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
858 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
859 [IEEE80211_MODE_HALF] = { 2, 2, 3, 47, 0 },
860 [IEEE80211_MODE_QUARTER]= { 2, 2, 3, 47, 0 },
861 [IEEE80211_MODE_11NA] = { 2, 2, 3, 47, 0 },
862 [IEEE80211_MODE_11NG] = { 2, 2, 3, 47, 0 },
866 _setifsparams(struct wmeParams *wmep, const paramType *phy)
868 wmep->wmep_aifsn = phy->aifsn;
869 wmep->wmep_logcwmin = phy->logcwmin;
870 wmep->wmep_logcwmax = phy->logcwmax;
871 wmep->wmep_txopLimit = phy->txopLimit;
875 setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
876 struct wmeParams *wmep, const paramType *phy)
878 wmep->wmep_acm = phy->acm;
879 _setifsparams(wmep, phy);
881 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
882 "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
883 ieee80211_wme_acnames[ac], type,
884 wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
885 wmep->wmep_logcwmax, wmep->wmep_txopLimit);
889 ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
891 struct ieee80211com *ic = vap->iv_ic;
892 struct ieee80211_wme_state *wme = &ic->ic_wme;
893 const paramType *pPhyParam, *pBssPhyParam;
894 struct wmeParams *wmep;
895 enum ieee80211_phymode mode;
898 IEEE80211_LOCK_ASSERT(ic);
900 if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
904 * Clear the wme cap_info field so a qoscount from a previous
905 * vap doesn't confuse later code which only parses the beacon
906 * field and updates hardware when said field changes.
907 * Otherwise the hardware is programmed with defaults, not what
908 * the beacon actually announces.
910 wme->wme_wmeChanParams.cap_info = 0;
913 * Select mode; we can be called early in which case we
914 * always use auto mode. We know we'll be called when
915 * entering the RUN state with bsschan setup properly
916 * so state will eventually get set correctly
918 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
919 mode = ieee80211_chan2mode(ic->ic_bsschan);
921 mode = IEEE80211_MODE_AUTO;
922 for (i = 0; i < WME_NUM_AC; i++) {
925 pPhyParam = &phyParamForAC_BK[mode];
926 pBssPhyParam = &phyParamForAC_BK[mode];
929 pPhyParam = &phyParamForAC_VI[mode];
930 pBssPhyParam = &bssPhyParamForAC_VI[mode];
933 pPhyParam = &phyParamForAC_VO[mode];
934 pBssPhyParam = &bssPhyParamForAC_VO[mode];
938 pPhyParam = &phyParamForAC_BE[mode];
939 pBssPhyParam = &bssPhyParamForAC_BE[mode];
942 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
943 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
944 setwmeparams(vap, "chan", i, wmep, pPhyParam);
946 setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
948 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
949 setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
951 /* NB: check ic_bss to avoid NULL deref on initial attach */
952 if (vap->iv_bss != NULL) {
954 * Calculate agressive mode switching threshold based
955 * on beacon interval. This doesn't need locking since
956 * we're only called before entering the RUN state at
957 * which point we start sending beacon frames.
959 wme->wme_hipri_switch_thresh =
960 (HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
961 wme->wme_flags &= ~WME_F_AGGRMODE;
962 ieee80211_wme_updateparams(vap);
967 ieee80211_wme_initparams(struct ieee80211vap *vap)
969 struct ieee80211com *ic = vap->iv_ic;
972 ieee80211_wme_initparams_locked(vap);
973 IEEE80211_UNLOCK(ic);
977 * Update WME parameters for ourself and the BSS.
980 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
982 static const paramType aggrParam[IEEE80211_MODE_MAX] = {
983 [IEEE80211_MODE_AUTO] = { 2, 4, 10, 64, 0 },
984 [IEEE80211_MODE_11A] = { 2, 4, 10, 64, 0 },
985 [IEEE80211_MODE_11B] = { 2, 5, 10, 64, 0 },
986 [IEEE80211_MODE_11G] = { 2, 4, 10, 64, 0 },
987 [IEEE80211_MODE_FH] = { 2, 5, 10, 64, 0 },
988 [IEEE80211_MODE_TURBO_A] = { 1, 3, 10, 64, 0 },
989 [IEEE80211_MODE_TURBO_G] = { 1, 3, 10, 64, 0 },
990 [IEEE80211_MODE_STURBO_A] = { 1, 3, 10, 64, 0 },
991 [IEEE80211_MODE_HALF] = { 2, 4, 10, 64, 0 },
992 [IEEE80211_MODE_QUARTER] = { 2, 4, 10, 64, 0 },
993 [IEEE80211_MODE_11NA] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
994 [IEEE80211_MODE_11NG] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
996 struct ieee80211com *ic = vap->iv_ic;
997 struct ieee80211_wme_state *wme = &ic->ic_wme;
998 const struct wmeParams *wmep;
999 struct wmeParams *chanp, *bssp;
1000 enum ieee80211_phymode mode;
1002 int do_aggrmode = 0;
1005 * Set up the channel access parameters for the physical
1006 * device. First populate the configured settings.
1008 for (i = 0; i < WME_NUM_AC; i++) {
1009 chanp = &wme->wme_chanParams.cap_wmeParams[i];
1010 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1011 chanp->wmep_aifsn = wmep->wmep_aifsn;
1012 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1013 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1014 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1016 chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
1017 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1018 chanp->wmep_aifsn = wmep->wmep_aifsn;
1019 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1020 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1021 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1025 * Select mode; we can be called early in which case we
1026 * always use auto mode. We know we'll be called when
1027 * entering the RUN state with bsschan setup properly
1028 * so state will eventually get set correctly
1030 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1031 mode = ieee80211_chan2mode(ic->ic_bsschan);
1033 mode = IEEE80211_MODE_AUTO;
1036 * This implements agressive mode as found in certain
1037 * vendors' AP's. When there is significant high
1038 * priority (VI/VO) traffic in the BSS throttle back BE
1039 * traffic by using conservative parameters. Otherwise
1040 * BE uses agressive params to optimize performance of
1041 * legacy/non-QoS traffic.
1044 /* Hostap? Only if aggressive mode is enabled */
1045 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1046 (wme->wme_flags & WME_F_AGGRMODE) != 0)
1050 * Station? Only if we're in a non-QoS BSS.
1052 else if ((vap->iv_opmode == IEEE80211_M_STA &&
1053 (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1057 * IBSS? Only if we we have WME enabled.
1059 else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1060 (vap->iv_flags & IEEE80211_F_WME))
1064 * If WME is disabled on this VAP, default to aggressive mode
1065 * regardless of the configuration.
1067 if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1075 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1076 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1078 chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1079 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1080 aggrParam[mode].logcwmin;
1081 chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1082 aggrParam[mode].logcwmax;
1083 chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1084 (vap->iv_flags & IEEE80211_F_BURST) ?
1085 aggrParam[mode].txopLimit : 0;
1086 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1087 "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1088 "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1089 chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1090 chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1095 * Change the contention window based on the number of associated
1096 * stations. If the number of associated stations is 1 and
1097 * aggressive mode is enabled, lower the contention window even
1100 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1101 ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1102 static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1103 [IEEE80211_MODE_AUTO] = 3,
1104 [IEEE80211_MODE_11A] = 3,
1105 [IEEE80211_MODE_11B] = 4,
1106 [IEEE80211_MODE_11G] = 3,
1107 [IEEE80211_MODE_FH] = 4,
1108 [IEEE80211_MODE_TURBO_A] = 3,
1109 [IEEE80211_MODE_TURBO_G] = 3,
1110 [IEEE80211_MODE_STURBO_A] = 3,
1111 [IEEE80211_MODE_HALF] = 3,
1112 [IEEE80211_MODE_QUARTER] = 3,
1113 [IEEE80211_MODE_11NA] = 3,
1114 [IEEE80211_MODE_11NG] = 3,
1116 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1117 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1119 chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1120 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1121 "update %s (chan+bss) logcwmin %u\n",
1122 ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1126 * Arrange for the beacon update.
1128 * XXX what about MBSS, WDS?
1130 if (vap->iv_opmode == IEEE80211_M_HOSTAP
1131 || vap->iv_opmode == IEEE80211_M_IBSS) {
1133 * Arrange for a beacon update and bump the parameter
1134 * set number so associated stations load the new values.
1136 wme->wme_bssChanParams.cap_info =
1137 (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1138 ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1141 /* schedule the deferred WME update */
1142 ieee80211_runtask(ic, &ic->ic_wme_task);
1144 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1145 "%s: WME params updated, cap_info 0x%x\n", __func__,
1146 vap->iv_opmode == IEEE80211_M_STA ?
1147 wme->wme_wmeChanParams.cap_info :
1148 wme->wme_bssChanParams.cap_info);
1152 ieee80211_wme_updateparams(struct ieee80211vap *vap)
1154 struct ieee80211com *ic = vap->iv_ic;
1156 if (ic->ic_caps & IEEE80211_C_WME) {
1158 ieee80211_wme_updateparams_locked(vap);
1159 IEEE80211_UNLOCK(ic);
1164 parent_updown(void *arg, int npending)
1166 struct ieee80211com *ic = arg;
1172 update_mcast(void *arg, int npending)
1174 struct ieee80211com *ic = arg;
1176 ic->ic_update_mcast(ic);
1180 update_promisc(void *arg, int npending)
1182 struct ieee80211com *ic = arg;
1184 ic->ic_update_promisc(ic);
1188 update_channel(void *arg, int npending)
1190 struct ieee80211com *ic = arg;
1192 ic->ic_set_channel(ic);
1193 ieee80211_radiotap_chan_change(ic);
1197 update_chw(void *arg, int npending)
1199 struct ieee80211com *ic = arg;
1202 * XXX should we defer the channel width _config_ update until now?
1204 ic->ic_update_chw(ic);
1208 update_wme(void *arg, int npending)
1210 struct ieee80211com *ic = arg;
1213 * XXX should we defer the WME configuration update until now?
1215 ic->ic_wme.wme_update(ic);
1219 restart_vaps(void *arg, int npending)
1221 struct ieee80211com *ic = arg;
1223 ieee80211_suspend_all(ic);
1224 ieee80211_resume_all(ic);
1228 * Block until the parent is in a known state. This is
1229 * used after any operations that dispatch a task (e.g.
1230 * to auto-configure the parent device up/down).
1233 ieee80211_waitfor_parent(struct ieee80211com *ic)
1235 taskqueue_block(ic->ic_tq);
1236 ieee80211_draintask(ic, &ic->ic_parent_task);
1237 ieee80211_draintask(ic, &ic->ic_mcast_task);
1238 ieee80211_draintask(ic, &ic->ic_promisc_task);
1239 ieee80211_draintask(ic, &ic->ic_chan_task);
1240 ieee80211_draintask(ic, &ic->ic_bmiss_task);
1241 ieee80211_draintask(ic, &ic->ic_chw_task);
1242 ieee80211_draintask(ic, &ic->ic_wme_task);
1243 taskqueue_unblock(ic->ic_tq);
1247 * Check to see whether the current channel needs reset.
1249 * Some devices don't handle being given an invalid channel
1250 * in their operating mode very well (eg wpi(4) will throw a
1251 * firmware exception.)
1253 * Return 0 if we're ok, 1 if the channel needs to be reset.
1255 * See PR kern/202502.
1258 ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
1260 struct ieee80211com *ic = vap->iv_ic;
1262 if ((vap->iv_opmode == IEEE80211_M_IBSS &&
1263 IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
1264 (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1265 IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
1271 * Reset the curchan to a known good state.
1274 ieee80211_start_reset_chan(struct ieee80211vap *vap)
1276 struct ieee80211com *ic = vap->iv_ic;
1278 ic->ic_curchan = &ic->ic_channels[0];
1282 * Start a vap running. If this is the first vap to be
1283 * set running on the underlying device then we
1284 * automatically bring the device up.
1287 ieee80211_start_locked(struct ieee80211vap *vap)
1289 struct ifnet *ifp = vap->iv_ifp;
1290 struct ieee80211com *ic = vap->iv_ic;
1292 IEEE80211_LOCK_ASSERT(ic);
1294 IEEE80211_DPRINTF(vap,
1295 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1296 "start running, %d vaps running\n", ic->ic_nrunning);
1298 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1300 * Mark us running. Note that it's ok to do this first;
1301 * if we need to bring the parent device up we defer that
1302 * to avoid dropping the com lock. We expect the device
1303 * to respond to being marked up by calling back into us
1304 * through ieee80211_start_all at which point we'll come
1305 * back in here and complete the work.
1307 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1309 * We are not running; if this we are the first vap
1310 * to be brought up auto-up the parent if necessary.
1312 if (ic->ic_nrunning++ == 0) {
1314 /* reset the channel to a known good channel */
1315 if (ieee80211_start_check_reset_chan(vap))
1316 ieee80211_start_reset_chan(vap);
1318 IEEE80211_DPRINTF(vap,
1319 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1320 "%s: up parent %s\n", __func__, ic->ic_name);
1321 ieee80211_runtask(ic, &ic->ic_parent_task);
1326 * If the parent is up and running, then kick the
1327 * 802.11 state machine as appropriate.
1329 if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1330 if (vap->iv_opmode == IEEE80211_M_STA) {
1332 /* XXX bypasses scan too easily; disable for now */
1334 * Try to be intelligent about clocking the state
1335 * machine. If we're currently in RUN state then
1336 * we should be able to apply any new state/parameters
1337 * simply by re-associating. Otherwise we need to
1338 * re-scan to select an appropriate ap.
1340 if (vap->iv_state >= IEEE80211_S_RUN)
1341 ieee80211_new_state_locked(vap,
1342 IEEE80211_S_ASSOC, 1);
1345 ieee80211_new_state_locked(vap,
1346 IEEE80211_S_SCAN, 0);
1349 * For monitor+wds mode there's nothing to do but
1350 * start running. Otherwise if this is the first
1351 * vap to be brought up, start a scan which may be
1352 * preempted if the station is locked to a particular
1355 vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1356 if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1357 vap->iv_opmode == IEEE80211_M_WDS)
1358 ieee80211_new_state_locked(vap,
1359 IEEE80211_S_RUN, -1);
1361 ieee80211_new_state_locked(vap,
1362 IEEE80211_S_SCAN, 0);
1368 * Start a single vap.
1371 ieee80211_init(void *arg)
1373 struct ieee80211vap *vap = arg;
1375 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1378 IEEE80211_LOCK(vap->iv_ic);
1379 ieee80211_start_locked(vap);
1380 IEEE80211_UNLOCK(vap->iv_ic);
1384 * Start all runnable vap's on a device.
1387 ieee80211_start_all(struct ieee80211com *ic)
1389 struct ieee80211vap *vap;
1392 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1393 struct ifnet *ifp = vap->iv_ifp;
1394 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1395 ieee80211_start_locked(vap);
1397 IEEE80211_UNLOCK(ic);
1401 * Stop a vap. We force it down using the state machine
1402 * then mark it's ifnet not running. If this is the last
1403 * vap running on the underlying device then we close it
1404 * too to insure it will be properly initialized when the
1405 * next vap is brought up.
1408 ieee80211_stop_locked(struct ieee80211vap *vap)
1410 struct ieee80211com *ic = vap->iv_ic;
1411 struct ifnet *ifp = vap->iv_ifp;
1413 IEEE80211_LOCK_ASSERT(ic);
1415 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1416 "stop running, %d vaps running\n", ic->ic_nrunning);
1418 ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1419 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1420 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; /* mark us stopped */
1421 if (--ic->ic_nrunning == 0) {
1422 IEEE80211_DPRINTF(vap,
1423 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1424 "down parent %s\n", ic->ic_name);
1425 ieee80211_runtask(ic, &ic->ic_parent_task);
1431 ieee80211_stop(struct ieee80211vap *vap)
1433 struct ieee80211com *ic = vap->iv_ic;
1436 ieee80211_stop_locked(vap);
1437 IEEE80211_UNLOCK(ic);
1441 * Stop all vap's running on a device.
1444 ieee80211_stop_all(struct ieee80211com *ic)
1446 struct ieee80211vap *vap;
1449 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1450 struct ifnet *ifp = vap->iv_ifp;
1451 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1452 ieee80211_stop_locked(vap);
1454 IEEE80211_UNLOCK(ic);
1456 ieee80211_waitfor_parent(ic);
1460 * Stop all vap's running on a device and arrange
1461 * for those that were running to be resumed.
1464 ieee80211_suspend_all(struct ieee80211com *ic)
1466 struct ieee80211vap *vap;
1469 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1470 struct ifnet *ifp = vap->iv_ifp;
1471 if (IFNET_IS_UP_RUNNING(ifp)) { /* NB: avoid recursion */
1472 vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1473 ieee80211_stop_locked(vap);
1476 IEEE80211_UNLOCK(ic);
1478 ieee80211_waitfor_parent(ic);
1482 * Start all vap's marked for resume.
1485 ieee80211_resume_all(struct ieee80211com *ic)
1487 struct ieee80211vap *vap;
1490 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1491 struct ifnet *ifp = vap->iv_ifp;
1492 if (!IFNET_IS_UP_RUNNING(ifp) &&
1493 (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1494 vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1495 ieee80211_start_locked(vap);
1498 IEEE80211_UNLOCK(ic);
1502 * Restart all vap's running on a device.
1505 ieee80211_restart_all(struct ieee80211com *ic)
1508 * NB: do not use ieee80211_runtask here, we will
1509 * block & drain net80211 taskqueue.
1511 taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
1515 ieee80211_beacon_miss(struct ieee80211com *ic)
1518 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1519 /* Process in a taskq, the handler may reenter the driver */
1520 ieee80211_runtask(ic, &ic->ic_bmiss_task);
1522 IEEE80211_UNLOCK(ic);
1526 beacon_miss(void *arg, int npending)
1528 struct ieee80211com *ic = arg;
1529 struct ieee80211vap *vap;
1532 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1534 * We only pass events through for sta vap's in RUN state;
1535 * may be too restrictive but for now this saves all the
1536 * handlers duplicating these checks.
1538 if (vap->iv_opmode == IEEE80211_M_STA &&
1539 vap->iv_state >= IEEE80211_S_RUN &&
1540 vap->iv_bmiss != NULL)
1543 IEEE80211_UNLOCK(ic);
1547 beacon_swmiss(void *arg, int npending)
1549 struct ieee80211vap *vap = arg;
1550 struct ieee80211com *ic = vap->iv_ic;
1553 if (vap->iv_state == IEEE80211_S_RUN) {
1554 /* XXX Call multiple times if npending > zero? */
1557 IEEE80211_UNLOCK(ic);
1561 * Software beacon miss handling. Check if any beacons
1562 * were received in the last period. If not post a
1563 * beacon miss; otherwise reset the counter.
1566 ieee80211_swbmiss(void *arg)
1568 struct ieee80211vap *vap = arg;
1569 struct ieee80211com *ic = vap->iv_ic;
1571 IEEE80211_LOCK_ASSERT(ic);
1573 /* XXX sleep state? */
1574 KASSERT(vap->iv_state == IEEE80211_S_RUN,
1575 ("wrong state %d", vap->iv_state));
1577 if (ic->ic_flags & IEEE80211_F_SCAN) {
1579 * If scanning just ignore and reset state. If we get a
1580 * bmiss after coming out of scan because we haven't had
1581 * time to receive a beacon then we should probe the AP
1582 * before posting a real bmiss (unless iv_bmiss_max has
1583 * been artifiically lowered). A cleaner solution might
1584 * be to disable the timer on scan start/end but to handle
1585 * case of multiple sta vap's we'd need to disable the
1586 * timers of all affected vap's.
1588 vap->iv_swbmiss_count = 0;
1589 } else if (vap->iv_swbmiss_count == 0) {
1590 if (vap->iv_bmiss != NULL)
1591 ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1593 vap->iv_swbmiss_count = 0;
1594 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1595 ieee80211_swbmiss, vap);
1599 * Start an 802.11h channel switch. We record the parameters,
1600 * mark the operation pending, notify each vap through the
1601 * beacon update mechanism so it can update the beacon frame
1602 * contents, and then switch vap's to CSA state to block outbound
1603 * traffic. Devices that handle CSA directly can use the state
1604 * switch to do the right thing so long as they call
1605 * ieee80211_csa_completeswitch when it's time to complete the
1606 * channel change. Devices that depend on the net80211 layer can
1607 * use ieee80211_beacon_update to handle the countdown and the
1611 ieee80211_csa_startswitch(struct ieee80211com *ic,
1612 struct ieee80211_channel *c, int mode, int count)
1614 struct ieee80211vap *vap;
1616 IEEE80211_LOCK_ASSERT(ic);
1618 ic->ic_csa_newchan = c;
1619 ic->ic_csa_mode = mode;
1620 ic->ic_csa_count = count;
1621 ic->ic_flags |= IEEE80211_F_CSAPENDING;
1622 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1623 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1624 vap->iv_opmode == IEEE80211_M_IBSS ||
1625 vap->iv_opmode == IEEE80211_M_MBSS)
1626 ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1627 /* switch to CSA state to block outbound traffic */
1628 if (vap->iv_state == IEEE80211_S_RUN)
1629 ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1631 ieee80211_notify_csa(ic, c, mode, count);
1635 * Complete the channel switch by transitioning all CSA VAPs to RUN.
1636 * This is called by both the completion and cancellation functions
1637 * so each VAP is placed back in the RUN state and can thus transmit.
1640 csa_completeswitch(struct ieee80211com *ic)
1642 struct ieee80211vap *vap;
1644 ic->ic_csa_newchan = NULL;
1645 ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1647 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1648 if (vap->iv_state == IEEE80211_S_CSA)
1649 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1653 * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1654 * We clear state and move all vap's in CSA state to RUN state
1655 * so they can again transmit.
1657 * Although this may not be completely correct, update the BSS channel
1658 * for each VAP to the newly configured channel. The setcurchan sets
1659 * the current operating channel for the interface (so the radio does
1660 * switch over) but the VAP BSS isn't updated, leading to incorrectly
1661 * reported information via ioctl.
1664 ieee80211_csa_completeswitch(struct ieee80211com *ic)
1666 struct ieee80211vap *vap;
1668 IEEE80211_LOCK_ASSERT(ic);
1670 KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1672 ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1673 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1674 if (vap->iv_state == IEEE80211_S_CSA)
1675 vap->iv_bss->ni_chan = ic->ic_curchan;
1677 csa_completeswitch(ic);
1681 * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1682 * We clear state and move all vap's in CSA state to RUN state
1683 * so they can again transmit.
1686 ieee80211_csa_cancelswitch(struct ieee80211com *ic)
1688 IEEE80211_LOCK_ASSERT(ic);
1690 csa_completeswitch(ic);
1694 * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1695 * We clear state and move all vap's in CAC state to RUN state.
1698 ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1700 struct ieee80211com *ic = vap0->iv_ic;
1701 struct ieee80211vap *vap;
1705 * Complete CAC state change for lead vap first; then
1706 * clock all the other vap's waiting.
1708 KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1709 ("wrong state %d", vap0->iv_state));
1710 ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1712 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1713 if (vap->iv_state == IEEE80211_S_CAC)
1714 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1715 IEEE80211_UNLOCK(ic);
1719 * Force all vap's other than the specified vap to the INIT state
1720 * and mark them as waiting for a scan to complete. These vaps
1721 * will be brought up when the scan completes and the scanning vap
1722 * reaches RUN state by wakeupwaiting.
1725 markwaiting(struct ieee80211vap *vap0)
1727 struct ieee80211com *ic = vap0->iv_ic;
1728 struct ieee80211vap *vap;
1730 IEEE80211_LOCK_ASSERT(ic);
1733 * A vap list entry can not disappear since we are running on the
1734 * taskqueue and a vap destroy will queue and drain another state
1737 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1740 if (vap->iv_state != IEEE80211_S_INIT) {
1741 /* NB: iv_newstate may drop the lock */
1742 vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1743 IEEE80211_LOCK_ASSERT(ic);
1744 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1750 * Wakeup all vap's waiting for a scan to complete. This is the
1751 * companion to markwaiting (above) and is used to coordinate
1752 * multiple vaps scanning.
1753 * This is called from the state taskqueue.
1756 wakeupwaiting(struct ieee80211vap *vap0)
1758 struct ieee80211com *ic = vap0->iv_ic;
1759 struct ieee80211vap *vap;
1761 IEEE80211_LOCK_ASSERT(ic);
1764 * A vap list entry can not disappear since we are running on the
1765 * taskqueue and a vap destroy will queue and drain another state
1768 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1771 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1772 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1773 /* NB: sta's cannot go INIT->RUN */
1774 /* NB: iv_newstate may drop the lock */
1775 vap->iv_newstate(vap,
1776 vap->iv_opmode == IEEE80211_M_STA ?
1777 IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1778 IEEE80211_LOCK_ASSERT(ic);
1784 * Handle post state change work common to all operating modes.
1787 ieee80211_newstate_cb(void *xvap, int npending)
1789 struct ieee80211vap *vap = xvap;
1790 struct ieee80211com *ic = vap->iv_ic;
1791 enum ieee80211_state nstate, ostate;
1795 nstate = vap->iv_nstate;
1796 arg = vap->iv_nstate_arg;
1798 if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
1800 * We have been requested to drop back to the INIT before
1801 * proceeding to the new state.
1803 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1804 "%s: %s -> %s arg %d\n", __func__,
1805 ieee80211_state_name[vap->iv_state],
1806 ieee80211_state_name[IEEE80211_S_INIT], arg);
1807 vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1808 IEEE80211_LOCK_ASSERT(ic);
1809 vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
1812 ostate = vap->iv_state;
1813 if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
1815 * SCAN was forced; e.g. on beacon miss. Force other running
1816 * vap's to INIT state and mark them as waiting for the scan to
1817 * complete. This insures they don't interfere with our
1818 * scanning. Since we are single threaded the vaps can not
1819 * transition again while we are executing.
1821 * XXX not always right, assumes ap follows sta
1825 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1826 "%s: %s -> %s arg %d\n", __func__,
1827 ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
1829 rc = vap->iv_newstate(vap, nstate, arg);
1830 IEEE80211_LOCK_ASSERT(ic);
1831 vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
1833 /* State transition failed */
1834 KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
1835 KASSERT(nstate != IEEE80211_S_INIT,
1836 ("INIT state change failed"));
1837 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1838 "%s: %s returned error %d\n", __func__,
1839 ieee80211_state_name[nstate], rc);
1843 /* No actual transition, skip post processing */
1844 if (ostate == nstate)
1847 if (nstate == IEEE80211_S_RUN) {
1849 * OACTIVE may be set on the vap if the upper layer
1850 * tried to transmit (e.g. IPv6 NDP) before we reach
1851 * RUN state. Clear it and restart xmit.
1853 * Note this can also happen as a result of SLEEP->RUN
1854 * (i.e. coming out of power save mode).
1856 vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1859 * XXX TODO Kick-start a VAP queue - this should be a method!
1862 /* bring up any vaps waiting on us */
1864 } else if (nstate == IEEE80211_S_INIT) {
1866 * Flush the scan cache if we did the last scan (XXX?)
1867 * and flush any frames on send queues from this vap.
1868 * Note the mgt q is used only for legacy drivers and
1869 * will go away shortly.
1871 ieee80211_scan_flush(vap);
1874 * XXX TODO: ic/vap queue flush
1878 IEEE80211_UNLOCK(ic);
1882 * Public interface for initiating a state machine change.
1883 * This routine single-threads the request and coordinates
1884 * the scheduling of multiple vaps for the purpose of selecting
1885 * an operating channel. Specifically the following scenarios
1887 * o only one vap can be selecting a channel so on transition to
1888 * SCAN state if another vap is already scanning then
1889 * mark the caller for later processing and return without
1890 * doing anything (XXX? expectations by caller of synchronous operation)
1891 * o only one vap can be doing CAC of a channel so on transition to
1892 * CAC state if another vap is already scanning for radar then
1893 * mark the caller for later processing and return without
1894 * doing anything (XXX? expectations by caller of synchronous operation)
1895 * o if another vap is already running when a request is made
1896 * to SCAN then an operating channel has been chosen; bypass
1897 * the scan and just join the channel
1899 * Note that the state change call is done through the iv_newstate
1900 * method pointer so any driver routine gets invoked. The driver
1901 * will normally call back into operating mode-specific
1902 * ieee80211_newstate routines (below) unless it needs to completely
1903 * bypass the state machine (e.g. because the firmware has it's
1904 * own idea how things should work). Bypassing the net80211 layer
1905 * is usually a mistake and indicates lack of proper integration
1906 * with the net80211 layer.
1909 ieee80211_new_state_locked(struct ieee80211vap *vap,
1910 enum ieee80211_state nstate, int arg)
1912 struct ieee80211com *ic = vap->iv_ic;
1913 struct ieee80211vap *vp;
1914 enum ieee80211_state ostate;
1915 int nrunning, nscanning;
1917 IEEE80211_LOCK_ASSERT(ic);
1919 if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
1920 if (vap->iv_nstate == IEEE80211_S_INIT) {
1922 * XXX The vap is being stopped, do no allow any other
1923 * state changes until this is completed.
1926 } else if (vap->iv_state != vap->iv_nstate) {
1928 /* Warn if the previous state hasn't completed. */
1929 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1930 "%s: pending %s -> %s transition lost\n", __func__,
1931 ieee80211_state_name[vap->iv_state],
1932 ieee80211_state_name[vap->iv_nstate]);
1934 /* XXX temporarily enable to identify issues */
1935 if_printf(vap->iv_ifp,
1936 "%s: pending %s -> %s transition lost\n",
1937 __func__, ieee80211_state_name[vap->iv_state],
1938 ieee80211_state_name[vap->iv_nstate]);
1943 nrunning = nscanning = 0;
1944 /* XXX can track this state instead of calculating */
1945 TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1947 if (vp->iv_state >= IEEE80211_S_RUN)
1949 /* XXX doesn't handle bg scan */
1950 /* NB: CAC+AUTH+ASSOC treated like SCAN */
1951 else if (vp->iv_state > IEEE80211_S_INIT)
1955 ostate = vap->iv_state;
1956 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1957 "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1958 ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1959 nrunning, nscanning);
1961 case IEEE80211_S_SCAN:
1962 if (ostate == IEEE80211_S_INIT) {
1964 * INIT -> SCAN happens on initial bringup.
1966 KASSERT(!(nscanning && nrunning),
1967 ("%d scanning and %d running", nscanning, nrunning));
1970 * Someone is scanning, defer our state
1971 * change until the work has completed.
1973 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1974 "%s: defer %s -> %s\n",
1975 __func__, ieee80211_state_name[ostate],
1976 ieee80211_state_name[nstate]);
1977 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1982 * Someone is operating; just join the channel
1986 /* XXX check each opmode, adhoc? */
1987 if (vap->iv_opmode == IEEE80211_M_STA)
1988 nstate = IEEE80211_S_SCAN;
1990 nstate = IEEE80211_S_RUN;
1991 #ifdef IEEE80211_DEBUG
1992 if (nstate != IEEE80211_S_SCAN) {
1993 IEEE80211_DPRINTF(vap,
1994 IEEE80211_MSG_STATE,
1995 "%s: override, now %s -> %s\n",
1997 ieee80211_state_name[ostate],
1998 ieee80211_state_name[nstate]);
2004 case IEEE80211_S_RUN:
2005 if (vap->iv_opmode == IEEE80211_M_WDS &&
2006 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
2009 * Legacy WDS with someone else scanning; don't
2010 * go online until that completes as we should
2011 * follow the other vap to the channel they choose.
2013 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2014 "%s: defer %s -> %s (legacy WDS)\n", __func__,
2015 ieee80211_state_name[ostate],
2016 ieee80211_state_name[nstate]);
2017 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2020 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
2021 IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2022 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
2023 !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
2025 * This is a DFS channel, transition to CAC state
2026 * instead of RUN. This allows us to initiate
2027 * Channel Availability Check (CAC) as specified
2030 nstate = IEEE80211_S_CAC;
2031 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2032 "%s: override %s -> %s (DFS)\n", __func__,
2033 ieee80211_state_name[ostate],
2034 ieee80211_state_name[nstate]);
2037 case IEEE80211_S_INIT:
2038 /* cancel any scan in progress */
2039 ieee80211_cancel_scan(vap);
2040 if (ostate == IEEE80211_S_INIT ) {
2041 /* XXX don't believe this */
2042 /* INIT -> INIT. nothing to do */
2043 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2049 /* defer the state change to a thread */
2050 vap->iv_nstate = nstate;
2051 vap->iv_nstate_arg = arg;
2052 vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
2053 ieee80211_runtask(ic, &vap->iv_nstate_task);
2058 ieee80211_new_state(struct ieee80211vap *vap,
2059 enum ieee80211_state nstate, int arg)
2061 struct ieee80211com *ic = vap->iv_ic;
2065 rc = ieee80211_new_state_locked(vap, nstate, arg);
2066 IEEE80211_UNLOCK(ic);