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/kernel.h>
39 #include <sys/systm.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
45 #include <net/if_var.h>
46 #include <net/if_media.h>
47 #include <net/ethernet.h> /* XXX for ether_sprintf */
49 #include <net80211/ieee80211_var.h>
50 #include <net80211/ieee80211_adhoc.h>
51 #include <net80211/ieee80211_sta.h>
52 #include <net80211/ieee80211_hostap.h>
53 #include <net80211/ieee80211_wds.h>
54 #ifdef IEEE80211_SUPPORT_MESH
55 #include <net80211/ieee80211_mesh.h>
57 #include <net80211/ieee80211_monitor.h>
58 #include <net80211/ieee80211_input.h>
61 #define AGGRESSIVE_MODE_SWITCH_HYSTERESIS 3 /* pkts / 100ms */
62 #define HIGH_PRI_SWITCH_THRESH 10 /* pkts / 100ms */
64 const char *ieee80211_mgt_subtype_name[] = {
65 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp",
66 "probe_req", "probe_resp", "reserved#6", "reserved#7",
67 "beacon", "atim", "disassoc", "auth",
68 "deauth", "action", "action_noack", "reserved#15"
70 const char *ieee80211_ctl_subtype_name[] = {
71 "reserved#0", "reserved#1", "reserved#2", "reserved#3",
72 "reserved#3", "reserved#5", "reserved#6", "reserved#7",
73 "reserved#8", "reserved#9", "ps_poll", "rts",
74 "cts", "ack", "cf_end", "cf_end_ack"
76 const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
77 "IBSS", /* IEEE80211_M_IBSS */
78 "STA", /* IEEE80211_M_STA */
79 "WDS", /* IEEE80211_M_WDS */
80 "AHDEMO", /* IEEE80211_M_AHDEMO */
81 "HOSTAP", /* IEEE80211_M_HOSTAP */
82 "MONITOR", /* IEEE80211_M_MONITOR */
83 "MBSS" /* IEEE80211_M_MBSS */
85 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
86 "INIT", /* IEEE80211_S_INIT */
87 "SCAN", /* IEEE80211_S_SCAN */
88 "AUTH", /* IEEE80211_S_AUTH */
89 "ASSOC", /* IEEE80211_S_ASSOC */
90 "CAC", /* IEEE80211_S_CAC */
91 "RUN", /* IEEE80211_S_RUN */
92 "CSA", /* IEEE80211_S_CSA */
93 "SLEEP", /* IEEE80211_S_SLEEP */
95 const char *ieee80211_wme_acnames[] = {
103 static void beacon_miss(void *, int);
104 static void beacon_swmiss(void *, int);
105 static void parent_updown(void *, int);
106 static void update_mcast(void *, int);
107 static void update_promisc(void *, int);
108 static void update_channel(void *, int);
109 static void update_chw(void *, int);
110 static void ieee80211_newstate_cb(void *, int);
113 null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
114 const struct ieee80211_bpf_params *params)
117 ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
123 ieee80211_proto_attach(struct ieee80211com *ic)
127 /* override the 802.3 setting */
128 hdrlen = ic->ic_headroom
129 + sizeof(struct ieee80211_qosframe_addr4)
130 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
131 + IEEE80211_WEP_EXTIVLEN;
132 /* XXX no way to recalculate on ifdetach */
133 if (ALIGN(hdrlen) > max_linkhdr) {
134 /* XXX sanity check... */
135 max_linkhdr = ALIGN(hdrlen);
136 max_hdr = max_linkhdr + max_protohdr;
137 max_datalen = MHLEN - max_hdr;
139 ic->ic_protmode = IEEE80211_PROT_CTSONLY;
141 TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
142 TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
143 TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
144 TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
145 TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
146 TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
148 ic->ic_wme.wme_hipri_switch_hysteresis =
149 AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
151 /* initialize management frame handlers */
152 ic->ic_send_mgmt = ieee80211_send_mgmt;
153 ic->ic_raw_xmit = null_raw_xmit;
155 ieee80211_adhoc_attach(ic);
156 ieee80211_sta_attach(ic);
157 ieee80211_wds_attach(ic);
158 ieee80211_hostap_attach(ic);
159 #ifdef IEEE80211_SUPPORT_MESH
160 ieee80211_mesh_attach(ic);
162 ieee80211_monitor_attach(ic);
166 ieee80211_proto_detach(struct ieee80211com *ic)
168 ieee80211_monitor_detach(ic);
169 #ifdef IEEE80211_SUPPORT_MESH
170 ieee80211_mesh_detach(ic);
172 ieee80211_hostap_detach(ic);
173 ieee80211_wds_detach(ic);
174 ieee80211_adhoc_detach(ic);
175 ieee80211_sta_detach(ic);
179 null_update_beacon(struct ieee80211vap *vap, int item)
184 ieee80211_proto_vattach(struct ieee80211vap *vap)
186 struct ieee80211com *ic = vap->iv_ic;
187 struct ifnet *ifp = vap->iv_ifp;
190 /* override the 802.3 setting */
191 ifp->if_hdrlen = ic->ic_headroom
192 + sizeof(struct ieee80211_qosframe_addr4)
193 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
194 + IEEE80211_WEP_EXTIVLEN;
196 vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
197 vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
198 vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
199 callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
200 callout_init(&vap->iv_mgtsend, 1);
201 TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
202 TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
204 * Install default tx rate handling: no fixed rate, lowest
205 * supported rate for mgmt and multicast frames. Default
206 * max retry count. These settings can be changed by the
207 * driver and/or user applications.
209 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
210 const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
212 vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
215 * Setting the management rate to MCS 0 assumes that the
216 * BSS Basic rate set is empty and the BSS Basic MCS set
219 * Since we're not checking this, default to the lowest
220 * defined rate for this mode.
222 * At least one 11n AP (DLINK DIR-825) is reported to drop
223 * some MCS management traffic (eg BA response frames.)
225 * See also: 9.6.0 of the 802.11n-2009 specification.
228 if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
229 vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
230 vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
232 vap->iv_txparms[i].mgmtrate =
233 rs->rs_rates[0] & IEEE80211_RATE_VAL;
234 vap->iv_txparms[i].mcastrate =
235 rs->rs_rates[0] & IEEE80211_RATE_VAL;
238 vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
239 vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
240 vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
242 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
244 vap->iv_update_beacon = null_update_beacon;
245 vap->iv_deliver_data = ieee80211_deliver_data;
247 /* attach support for operating mode */
248 ic->ic_vattach[vap->iv_opmode](vap);
252 ieee80211_proto_vdetach(struct ieee80211vap *vap)
254 #define FREEAPPIE(ie) do { \
256 IEEE80211_FREE(ie, M_80211_NODE_IE); \
259 * Detach operating mode module.
261 if (vap->iv_opdetach != NULL)
262 vap->iv_opdetach(vap);
264 * This should not be needed as we detach when reseting
265 * the state but be conservative here since the
266 * authenticator may do things like spawn kernel threads.
268 if (vap->iv_auth->ia_detach != NULL)
269 vap->iv_auth->ia_detach(vap);
271 * Detach any ACL'ator.
273 if (vap->iv_acl != NULL)
274 vap->iv_acl->iac_detach(vap);
276 FREEAPPIE(vap->iv_appie_beacon);
277 FREEAPPIE(vap->iv_appie_probereq);
278 FREEAPPIE(vap->iv_appie_proberesp);
279 FREEAPPIE(vap->iv_appie_assocreq);
280 FREEAPPIE(vap->iv_appie_assocresp);
281 FREEAPPIE(vap->iv_appie_wpa);
286 * Simple-minded authenticator module support.
289 #define IEEE80211_AUTH_MAX (IEEE80211_AUTH_WPA+1)
290 /* XXX well-known names */
291 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
292 "wlan_internal", /* IEEE80211_AUTH_NONE */
293 "wlan_internal", /* IEEE80211_AUTH_OPEN */
294 "wlan_internal", /* IEEE80211_AUTH_SHARED */
295 "wlan_xauth", /* IEEE80211_AUTH_8021X */
296 "wlan_internal", /* IEEE80211_AUTH_AUTO */
297 "wlan_xauth", /* IEEE80211_AUTH_WPA */
299 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
301 static const struct ieee80211_authenticator auth_internal = {
302 .ia_name = "wlan_internal",
305 .ia_node_join = NULL,
306 .ia_node_leave = NULL,
310 * Setup internal authenticators once; they are never unregistered.
313 ieee80211_auth_setup(void)
315 ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
316 ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
317 ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
319 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
321 const struct ieee80211_authenticator *
322 ieee80211_authenticator_get(int auth)
324 if (auth >= IEEE80211_AUTH_MAX)
326 if (authenticators[auth] == NULL)
327 ieee80211_load_module(auth_modnames[auth]);
328 return authenticators[auth];
332 ieee80211_authenticator_register(int type,
333 const struct ieee80211_authenticator *auth)
335 if (type >= IEEE80211_AUTH_MAX)
337 authenticators[type] = auth;
341 ieee80211_authenticator_unregister(int type)
344 if (type >= IEEE80211_AUTH_MAX)
346 authenticators[type] = NULL;
350 * Very simple-minded ACL module support.
352 /* XXX just one for now */
353 static const struct ieee80211_aclator *acl = NULL;
356 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
358 printf("wlan: %s acl policy registered\n", iac->iac_name);
363 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
367 printf("wlan: %s acl policy unregistered\n", iac->iac_name);
370 const struct ieee80211_aclator *
371 ieee80211_aclator_get(const char *name)
374 ieee80211_load_module("wlan_acl");
375 return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
379 ieee80211_print_essid(const uint8_t *essid, int len)
384 if (len > IEEE80211_NWID_LEN)
385 len = IEEE80211_NWID_LEN;
386 /* determine printable or not */
387 for (i = 0, p = essid; i < len; i++, p++) {
388 if (*p < ' ' || *p > 0x7e)
393 for (i = 0, p = essid; i < len; i++, p++)
398 for (i = 0, p = essid; i < len; i++, p++)
404 ieee80211_dump_pkt(struct ieee80211com *ic,
405 const uint8_t *buf, int len, int rate, int rssi)
407 const struct ieee80211_frame *wh;
410 wh = (const struct ieee80211_frame *)buf;
411 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
412 case IEEE80211_FC1_DIR_NODS:
413 printf("NODS %s", ether_sprintf(wh->i_addr2));
414 printf("->%s", ether_sprintf(wh->i_addr1));
415 printf("(%s)", ether_sprintf(wh->i_addr3));
417 case IEEE80211_FC1_DIR_TODS:
418 printf("TODS %s", ether_sprintf(wh->i_addr2));
419 printf("->%s", ether_sprintf(wh->i_addr3));
420 printf("(%s)", ether_sprintf(wh->i_addr1));
422 case IEEE80211_FC1_DIR_FROMDS:
423 printf("FRDS %s", ether_sprintf(wh->i_addr3));
424 printf("->%s", ether_sprintf(wh->i_addr1));
425 printf("(%s)", ether_sprintf(wh->i_addr2));
427 case IEEE80211_FC1_DIR_DSTODS:
428 printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
429 printf("->%s", ether_sprintf(wh->i_addr3));
430 printf("(%s", ether_sprintf(wh->i_addr2));
431 printf("->%s)", ether_sprintf(wh->i_addr1));
434 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
435 case IEEE80211_FC0_TYPE_DATA:
438 case IEEE80211_FC0_TYPE_MGT:
439 printf(" %s", ieee80211_mgt_subtype_name[
440 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
441 >> IEEE80211_FC0_SUBTYPE_SHIFT]);
444 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
447 if (IEEE80211_QOS_HAS_SEQ(wh)) {
448 const struct ieee80211_qosframe *qwh =
449 (const struct ieee80211_qosframe *)buf;
450 printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
451 qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
453 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
456 off = ieee80211_anyhdrspace(ic, wh);
457 printf(" WEP [IV %.02x %.02x %.02x",
458 buf[off+0], buf[off+1], buf[off+2]);
459 if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
460 printf(" %.02x %.02x %.02x",
461 buf[off+4], buf[off+5], buf[off+6]);
462 printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
465 printf(" %dM", rate / 2);
467 printf(" +%d", rssi);
470 for (i = 0; i < len; i++) {
473 printf("%02x", buf[i]);
480 findrix(const struct ieee80211_rateset *rs, int r)
484 for (i = 0; i < rs->rs_nrates; i++)
485 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
491 ieee80211_fix_rate(struct ieee80211_node *ni,
492 struct ieee80211_rateset *nrs, int flags)
494 struct ieee80211vap *vap = ni->ni_vap;
495 struct ieee80211com *ic = ni->ni_ic;
496 int i, j, rix, error;
497 int okrate, badrate, fixedrate, ucastrate;
498 const struct ieee80211_rateset *srs;
502 okrate = badrate = 0;
503 ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
504 if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
506 * Workaround awkwardness with fixed rate. We are called
507 * to check both the legacy rate set and the HT rate set
508 * but we must apply any legacy fixed rate check only to the
509 * legacy rate set and vice versa. We cannot tell what type
510 * of rate set we've been given (legacy or HT) but we can
511 * distinguish the fixed rate type (MCS have 0x80 set).
512 * So to deal with this the caller communicates whether to
513 * check MCS or legacy rate using the flags and we use the
514 * type of any fixed rate to avoid applying an MCS to a
515 * legacy rate and vice versa.
517 if (ucastrate & 0x80) {
518 if (flags & IEEE80211_F_DOFRATE)
519 flags &= ~IEEE80211_F_DOFRATE;
520 } else if ((ucastrate & 0x80) == 0) {
521 if (flags & IEEE80211_F_DOFMCS)
522 flags &= ~IEEE80211_F_DOFMCS;
524 /* NB: required to make MCS match below work */
525 ucastrate &= IEEE80211_RATE_VAL;
527 fixedrate = IEEE80211_FIXED_RATE_NONE;
529 * XXX we are called to process both MCS and legacy rates;
530 * we must use the appropriate basic rate set or chaos will
531 * ensue; for now callers that want MCS must supply
532 * IEEE80211_F_DOBRS; at some point we'll need to split this
533 * function so there are two variants, one for MCS and one
536 if (flags & IEEE80211_F_DOBRS)
537 srs = (const struct ieee80211_rateset *)
538 ieee80211_get_suphtrates(ic, ni->ni_chan);
540 srs = ieee80211_get_suprates(ic, ni->ni_chan);
541 for (i = 0; i < nrs->rs_nrates; ) {
542 if (flags & IEEE80211_F_DOSORT) {
546 for (j = i + 1; j < nrs->rs_nrates; j++) {
547 if (IEEE80211_RV(nrs->rs_rates[i]) >
548 IEEE80211_RV(nrs->rs_rates[j])) {
549 r = nrs->rs_rates[i];
550 nrs->rs_rates[i] = nrs->rs_rates[j];
551 nrs->rs_rates[j] = r;
555 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
558 * Check for fixed rate.
563 * Check against supported rates.
565 rix = findrix(srs, r);
566 if (flags & IEEE80211_F_DONEGO) {
569 * A rate in the node's rate set is not
570 * supported. If this is a basic rate and we
571 * are operating as a STA then this is an error.
572 * Otherwise we just discard/ignore the rate.
574 if ((flags & IEEE80211_F_JOIN) &&
575 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
577 } else if ((flags & IEEE80211_F_JOIN) == 0) {
579 * Overwrite with the supported rate
580 * value so any basic rate bit is set.
582 nrs->rs_rates[i] = srs->rs_rates[rix];
585 if ((flags & IEEE80211_F_DODEL) && rix < 0) {
587 * Delete unacceptable rates.
590 for (j = i; j < nrs->rs_nrates; j++)
591 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
592 nrs->rs_rates[j] = 0;
596 okrate = nrs->rs_rates[i];
599 if (okrate == 0 || error != 0 ||
600 ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
601 fixedrate != ucastrate)) {
602 IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
603 "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
604 "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
605 return badrate | IEEE80211_RATE_BASIC;
607 return IEEE80211_RV(okrate);
611 * Reset 11g-related state.
614 ieee80211_reset_erp(struct ieee80211com *ic)
616 ic->ic_flags &= ~IEEE80211_F_USEPROT;
617 ic->ic_nonerpsta = 0;
618 ic->ic_longslotsta = 0;
620 * Short slot time is enabled only when operating in 11g
621 * and not in an IBSS. We must also honor whether or not
622 * the driver is capable of doing it.
624 ieee80211_set_shortslottime(ic,
625 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
626 IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
627 (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
628 ic->ic_opmode == IEEE80211_M_HOSTAP &&
629 (ic->ic_caps & IEEE80211_C_SHSLOT)));
631 * Set short preamble and ERP barker-preamble flags.
633 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
634 (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
635 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
636 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
638 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
639 ic->ic_flags |= IEEE80211_F_USEBARKER;
644 * Set the short slot time state and notify the driver.
647 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
650 ic->ic_flags |= IEEE80211_F_SHSLOT;
652 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
654 if (ic->ic_updateslot != NULL)
655 ic->ic_updateslot(ic);
659 * Check if the specified rate set supports ERP.
660 * NB: the rate set is assumed to be sorted.
663 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
665 static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
668 if (rs->rs_nrates < nitems(rates))
670 for (i = 0; i < nitems(rates); i++) {
671 for (j = 0; j < rs->rs_nrates; j++) {
672 int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
686 * Mark the basic rates for the rate table based on the
687 * operating mode. For real 11g we mark all the 11b rates
688 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
689 * 11b rates. There's also a pseudo 11a-mode used to mark only
690 * the basic OFDM rates.
693 setbasicrates(struct ieee80211_rateset *rs,
694 enum ieee80211_phymode mode, int add)
696 static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
697 [IEEE80211_MODE_11A] = { 3, { 12, 24, 48 } },
698 [IEEE80211_MODE_11B] = { 2, { 2, 4 } },
700 [IEEE80211_MODE_11G] = { 4, { 2, 4, 11, 22 } },
701 [IEEE80211_MODE_TURBO_A] = { 3, { 12, 24, 48 } },
702 [IEEE80211_MODE_TURBO_G] = { 4, { 2, 4, 11, 22 } },
703 [IEEE80211_MODE_STURBO_A] = { 3, { 12, 24, 48 } },
704 [IEEE80211_MODE_HALF] = { 3, { 6, 12, 24 } },
705 [IEEE80211_MODE_QUARTER] = { 3, { 3, 6, 12 } },
706 [IEEE80211_MODE_11NA] = { 3, { 12, 24, 48 } },
708 [IEEE80211_MODE_11NG] = { 4, { 2, 4, 11, 22 } },
712 for (i = 0; i < rs->rs_nrates; i++) {
714 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
715 for (j = 0; j < basic[mode].rs_nrates; j++)
716 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
717 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
724 * Set the basic rates in a rate set.
727 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
728 enum ieee80211_phymode mode)
730 setbasicrates(rs, mode, 0);
734 * Add basic rates to a rate set.
737 ieee80211_addbasicrates(struct ieee80211_rateset *rs,
738 enum ieee80211_phymode mode)
740 setbasicrates(rs, mode, 1);
744 * WME protocol support.
746 * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
747 * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
748 * Draft 2.0 Test Plan (Appendix D).
750 * Static/Dynamic Turbo mode settings come from Atheros.
752 typedef struct phyParamType {
760 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
761 [IEEE80211_MODE_AUTO] = { 3, 4, 6, 0, 0 },
762 [IEEE80211_MODE_11A] = { 3, 4, 6, 0, 0 },
763 [IEEE80211_MODE_11B] = { 3, 4, 6, 0, 0 },
764 [IEEE80211_MODE_11G] = { 3, 4, 6, 0, 0 },
765 [IEEE80211_MODE_FH] = { 3, 4, 6, 0, 0 },
766 [IEEE80211_MODE_TURBO_A]= { 2, 3, 5, 0, 0 },
767 [IEEE80211_MODE_TURBO_G]= { 2, 3, 5, 0, 0 },
768 [IEEE80211_MODE_STURBO_A]={ 2, 3, 5, 0, 0 },
769 [IEEE80211_MODE_HALF] = { 3, 4, 6, 0, 0 },
770 [IEEE80211_MODE_QUARTER]= { 3, 4, 6, 0, 0 },
771 [IEEE80211_MODE_11NA] = { 3, 4, 6, 0, 0 },
772 [IEEE80211_MODE_11NG] = { 3, 4, 6, 0, 0 },
774 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
775 [IEEE80211_MODE_AUTO] = { 7, 4, 10, 0, 0 },
776 [IEEE80211_MODE_11A] = { 7, 4, 10, 0, 0 },
777 [IEEE80211_MODE_11B] = { 7, 4, 10, 0, 0 },
778 [IEEE80211_MODE_11G] = { 7, 4, 10, 0, 0 },
779 [IEEE80211_MODE_FH] = { 7, 4, 10, 0, 0 },
780 [IEEE80211_MODE_TURBO_A]= { 7, 3, 10, 0, 0 },
781 [IEEE80211_MODE_TURBO_G]= { 7, 3, 10, 0, 0 },
782 [IEEE80211_MODE_STURBO_A]={ 7, 3, 10, 0, 0 },
783 [IEEE80211_MODE_HALF] = { 7, 4, 10, 0, 0 },
784 [IEEE80211_MODE_QUARTER]= { 7, 4, 10, 0, 0 },
785 [IEEE80211_MODE_11NA] = { 7, 4, 10, 0, 0 },
786 [IEEE80211_MODE_11NG] = { 7, 4, 10, 0, 0 },
788 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
789 [IEEE80211_MODE_AUTO] = { 1, 3, 4, 94, 0 },
790 [IEEE80211_MODE_11A] = { 1, 3, 4, 94, 0 },
791 [IEEE80211_MODE_11B] = { 1, 3, 4, 188, 0 },
792 [IEEE80211_MODE_11G] = { 1, 3, 4, 94, 0 },
793 [IEEE80211_MODE_FH] = { 1, 3, 4, 188, 0 },
794 [IEEE80211_MODE_TURBO_A]= { 1, 2, 3, 94, 0 },
795 [IEEE80211_MODE_TURBO_G]= { 1, 2, 3, 94, 0 },
796 [IEEE80211_MODE_STURBO_A]={ 1, 2, 3, 94, 0 },
797 [IEEE80211_MODE_HALF] = { 1, 3, 4, 94, 0 },
798 [IEEE80211_MODE_QUARTER]= { 1, 3, 4, 94, 0 },
799 [IEEE80211_MODE_11NA] = { 1, 3, 4, 94, 0 },
800 [IEEE80211_MODE_11NG] = { 1, 3, 4, 94, 0 },
802 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
803 [IEEE80211_MODE_AUTO] = { 1, 2, 3, 47, 0 },
804 [IEEE80211_MODE_11A] = { 1, 2, 3, 47, 0 },
805 [IEEE80211_MODE_11B] = { 1, 2, 3, 102, 0 },
806 [IEEE80211_MODE_11G] = { 1, 2, 3, 47, 0 },
807 [IEEE80211_MODE_FH] = { 1, 2, 3, 102, 0 },
808 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
809 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
810 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
811 [IEEE80211_MODE_HALF] = { 1, 2, 3, 47, 0 },
812 [IEEE80211_MODE_QUARTER]= { 1, 2, 3, 47, 0 },
813 [IEEE80211_MODE_11NA] = { 1, 2, 3, 47, 0 },
814 [IEEE80211_MODE_11NG] = { 1, 2, 3, 47, 0 },
817 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
818 [IEEE80211_MODE_AUTO] = { 3, 4, 10, 0, 0 },
819 [IEEE80211_MODE_11A] = { 3, 4, 10, 0, 0 },
820 [IEEE80211_MODE_11B] = { 3, 4, 10, 0, 0 },
821 [IEEE80211_MODE_11G] = { 3, 4, 10, 0, 0 },
822 [IEEE80211_MODE_FH] = { 3, 4, 10, 0, 0 },
823 [IEEE80211_MODE_TURBO_A]= { 2, 3, 10, 0, 0 },
824 [IEEE80211_MODE_TURBO_G]= { 2, 3, 10, 0, 0 },
825 [IEEE80211_MODE_STURBO_A]={ 2, 3, 10, 0, 0 },
826 [IEEE80211_MODE_HALF] = { 3, 4, 10, 0, 0 },
827 [IEEE80211_MODE_QUARTER]= { 3, 4, 10, 0, 0 },
828 [IEEE80211_MODE_11NA] = { 3, 4, 10, 0, 0 },
829 [IEEE80211_MODE_11NG] = { 3, 4, 10, 0, 0 },
831 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
832 [IEEE80211_MODE_AUTO] = { 2, 3, 4, 94, 0 },
833 [IEEE80211_MODE_11A] = { 2, 3, 4, 94, 0 },
834 [IEEE80211_MODE_11B] = { 2, 3, 4, 188, 0 },
835 [IEEE80211_MODE_11G] = { 2, 3, 4, 94, 0 },
836 [IEEE80211_MODE_FH] = { 2, 3, 4, 188, 0 },
837 [IEEE80211_MODE_TURBO_A]= { 2, 2, 3, 94, 0 },
838 [IEEE80211_MODE_TURBO_G]= { 2, 2, 3, 94, 0 },
839 [IEEE80211_MODE_STURBO_A]={ 2, 2, 3, 94, 0 },
840 [IEEE80211_MODE_HALF] = { 2, 3, 4, 94, 0 },
841 [IEEE80211_MODE_QUARTER]= { 2, 3, 4, 94, 0 },
842 [IEEE80211_MODE_11NA] = { 2, 3, 4, 94, 0 },
843 [IEEE80211_MODE_11NG] = { 2, 3, 4, 94, 0 },
845 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
846 [IEEE80211_MODE_AUTO] = { 2, 2, 3, 47, 0 },
847 [IEEE80211_MODE_11A] = { 2, 2, 3, 47, 0 },
848 [IEEE80211_MODE_11B] = { 2, 2, 3, 102, 0 },
849 [IEEE80211_MODE_11G] = { 2, 2, 3, 47, 0 },
850 [IEEE80211_MODE_FH] = { 2, 2, 3, 102, 0 },
851 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
852 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
853 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
854 [IEEE80211_MODE_HALF] = { 2, 2, 3, 47, 0 },
855 [IEEE80211_MODE_QUARTER]= { 2, 2, 3, 47, 0 },
856 [IEEE80211_MODE_11NA] = { 2, 2, 3, 47, 0 },
857 [IEEE80211_MODE_11NG] = { 2, 2, 3, 47, 0 },
861 _setifsparams(struct wmeParams *wmep, const paramType *phy)
863 wmep->wmep_aifsn = phy->aifsn;
864 wmep->wmep_logcwmin = phy->logcwmin;
865 wmep->wmep_logcwmax = phy->logcwmax;
866 wmep->wmep_txopLimit = phy->txopLimit;
870 setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
871 struct wmeParams *wmep, const paramType *phy)
873 wmep->wmep_acm = phy->acm;
874 _setifsparams(wmep, phy);
876 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
877 "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
878 ieee80211_wme_acnames[ac], type,
879 wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
880 wmep->wmep_logcwmax, wmep->wmep_txopLimit);
884 ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
886 struct ieee80211com *ic = vap->iv_ic;
887 struct ieee80211_wme_state *wme = &ic->ic_wme;
888 const paramType *pPhyParam, *pBssPhyParam;
889 struct wmeParams *wmep;
890 enum ieee80211_phymode mode;
893 IEEE80211_LOCK_ASSERT(ic);
895 if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
899 * Clear the wme cap_info field so a qoscount from a previous
900 * vap doesn't confuse later code which only parses the beacon
901 * field and updates hardware when said field changes.
902 * Otherwise the hardware is programmed with defaults, not what
903 * the beacon actually announces.
905 wme->wme_wmeChanParams.cap_info = 0;
908 * Select mode; we can be called early in which case we
909 * always use auto mode. We know we'll be called when
910 * entering the RUN state with bsschan setup properly
911 * so state will eventually get set correctly
913 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
914 mode = ieee80211_chan2mode(ic->ic_bsschan);
916 mode = IEEE80211_MODE_AUTO;
917 for (i = 0; i < WME_NUM_AC; i++) {
920 pPhyParam = &phyParamForAC_BK[mode];
921 pBssPhyParam = &phyParamForAC_BK[mode];
924 pPhyParam = &phyParamForAC_VI[mode];
925 pBssPhyParam = &bssPhyParamForAC_VI[mode];
928 pPhyParam = &phyParamForAC_VO[mode];
929 pBssPhyParam = &bssPhyParamForAC_VO[mode];
933 pPhyParam = &phyParamForAC_BE[mode];
934 pBssPhyParam = &bssPhyParamForAC_BE[mode];
937 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
938 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
939 setwmeparams(vap, "chan", i, wmep, pPhyParam);
941 setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
943 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
944 setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
946 /* NB: check ic_bss to avoid NULL deref on initial attach */
947 if (vap->iv_bss != NULL) {
949 * Calculate agressive mode switching threshold based
950 * on beacon interval. This doesn't need locking since
951 * we're only called before entering the RUN state at
952 * which point we start sending beacon frames.
954 wme->wme_hipri_switch_thresh =
955 (HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
956 wme->wme_flags &= ~WME_F_AGGRMODE;
957 ieee80211_wme_updateparams(vap);
962 ieee80211_wme_initparams(struct ieee80211vap *vap)
964 struct ieee80211com *ic = vap->iv_ic;
967 ieee80211_wme_initparams_locked(vap);
968 IEEE80211_UNLOCK(ic);
972 * Update WME parameters for ourself and the BSS.
975 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
977 static const paramType aggrParam[IEEE80211_MODE_MAX] = {
978 [IEEE80211_MODE_AUTO] = { 2, 4, 10, 64, 0 },
979 [IEEE80211_MODE_11A] = { 2, 4, 10, 64, 0 },
980 [IEEE80211_MODE_11B] = { 2, 5, 10, 64, 0 },
981 [IEEE80211_MODE_11G] = { 2, 4, 10, 64, 0 },
982 [IEEE80211_MODE_FH] = { 2, 5, 10, 64, 0 },
983 [IEEE80211_MODE_TURBO_A] = { 1, 3, 10, 64, 0 },
984 [IEEE80211_MODE_TURBO_G] = { 1, 3, 10, 64, 0 },
985 [IEEE80211_MODE_STURBO_A] = { 1, 3, 10, 64, 0 },
986 [IEEE80211_MODE_HALF] = { 2, 4, 10, 64, 0 },
987 [IEEE80211_MODE_QUARTER] = { 2, 4, 10, 64, 0 },
988 [IEEE80211_MODE_11NA] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
989 [IEEE80211_MODE_11NG] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
991 struct ieee80211com *ic = vap->iv_ic;
992 struct ieee80211_wme_state *wme = &ic->ic_wme;
993 const struct wmeParams *wmep;
994 struct wmeParams *chanp, *bssp;
995 enum ieee80211_phymode mode;
1000 * Set up the channel access parameters for the physical
1001 * device. First populate the configured settings.
1003 for (i = 0; i < WME_NUM_AC; i++) {
1004 chanp = &wme->wme_chanParams.cap_wmeParams[i];
1005 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1006 chanp->wmep_aifsn = wmep->wmep_aifsn;
1007 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1008 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1009 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1011 chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
1012 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1013 chanp->wmep_aifsn = wmep->wmep_aifsn;
1014 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1015 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1016 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1020 * Select mode; we can be called early in which case we
1021 * always use auto mode. We know we'll be called when
1022 * entering the RUN state with bsschan setup properly
1023 * so state will eventually get set correctly
1025 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1026 mode = ieee80211_chan2mode(ic->ic_bsschan);
1028 mode = IEEE80211_MODE_AUTO;
1031 * This implements agressive mode as found in certain
1032 * vendors' AP's. When there is significant high
1033 * priority (VI/VO) traffic in the BSS throttle back BE
1034 * traffic by using conservative parameters. Otherwise
1035 * BE uses agressive params to optimize performance of
1036 * legacy/non-QoS traffic.
1039 /* Hostap? Only if aggressive mode is enabled */
1040 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1041 (wme->wme_flags & WME_F_AGGRMODE) != 0)
1045 * Station? Only if we're in a non-QoS BSS.
1047 else if ((vap->iv_opmode == IEEE80211_M_STA &&
1048 (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1052 * IBSS? Only if we we have WME enabled.
1054 else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1055 (vap->iv_flags & IEEE80211_F_WME))
1059 * If WME is disabled on this VAP, default to aggressive mode
1060 * regardless of the configuration.
1062 if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1070 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1071 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1073 chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1074 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1075 aggrParam[mode].logcwmin;
1076 chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1077 aggrParam[mode].logcwmax;
1078 chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1079 (vap->iv_flags & IEEE80211_F_BURST) ?
1080 aggrParam[mode].txopLimit : 0;
1081 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1082 "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1083 "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1084 chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1085 chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1090 * Change the contention window based on the number of associated
1091 * stations. If the number of associated stations is 1 and
1092 * aggressive mode is enabled, lower the contention window even
1095 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1096 ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1097 static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1098 [IEEE80211_MODE_AUTO] = 3,
1099 [IEEE80211_MODE_11A] = 3,
1100 [IEEE80211_MODE_11B] = 4,
1101 [IEEE80211_MODE_11G] = 3,
1102 [IEEE80211_MODE_FH] = 4,
1103 [IEEE80211_MODE_TURBO_A] = 3,
1104 [IEEE80211_MODE_TURBO_G] = 3,
1105 [IEEE80211_MODE_STURBO_A] = 3,
1106 [IEEE80211_MODE_HALF] = 3,
1107 [IEEE80211_MODE_QUARTER] = 3,
1108 [IEEE80211_MODE_11NA] = 3,
1109 [IEEE80211_MODE_11NG] = 3,
1111 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1112 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1114 chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1115 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1116 "update %s (chan+bss) logcwmin %u\n",
1117 ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1121 * Arrange for the beacon update.
1123 * XXX what about MBSS, WDS?
1125 if (vap->iv_opmode == IEEE80211_M_HOSTAP
1126 || vap->iv_opmode == IEEE80211_M_IBSS) {
1128 * Arrange for a beacon update and bump the parameter
1129 * set number so associated stations load the new values.
1131 wme->wme_bssChanParams.cap_info =
1132 (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1133 ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1136 wme->wme_update(ic);
1138 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1139 "%s: WME params updated, cap_info 0x%x\n", __func__,
1140 vap->iv_opmode == IEEE80211_M_STA ?
1141 wme->wme_wmeChanParams.cap_info :
1142 wme->wme_bssChanParams.cap_info);
1146 ieee80211_wme_updateparams(struct ieee80211vap *vap)
1148 struct ieee80211com *ic = vap->iv_ic;
1150 if (ic->ic_caps & IEEE80211_C_WME) {
1152 ieee80211_wme_updateparams_locked(vap);
1153 IEEE80211_UNLOCK(ic);
1158 parent_updown(void *arg, int npending)
1160 struct ieee80211com *ic = arg;
1166 update_mcast(void *arg, int npending)
1168 struct ieee80211com *ic = arg;
1170 ic->ic_update_mcast(ic);
1174 update_promisc(void *arg, int npending)
1176 struct ieee80211com *ic = arg;
1178 ic->ic_update_promisc(ic);
1182 update_channel(void *arg, int npending)
1184 struct ieee80211com *ic = arg;
1186 ic->ic_set_channel(ic);
1187 ieee80211_radiotap_chan_change(ic);
1191 update_chw(void *arg, int npending)
1193 struct ieee80211com *ic = arg;
1196 * XXX should we defer the channel width _config_ update until now?
1198 ic->ic_update_chw(ic);
1202 * Block until the parent is in a known state. This is
1203 * used after any operations that dispatch a task (e.g.
1204 * to auto-configure the parent device up/down).
1207 ieee80211_waitfor_parent(struct ieee80211com *ic)
1209 taskqueue_block(ic->ic_tq);
1210 ieee80211_draintask(ic, &ic->ic_parent_task);
1211 ieee80211_draintask(ic, &ic->ic_mcast_task);
1212 ieee80211_draintask(ic, &ic->ic_promisc_task);
1213 ieee80211_draintask(ic, &ic->ic_chan_task);
1214 ieee80211_draintask(ic, &ic->ic_bmiss_task);
1215 ieee80211_draintask(ic, &ic->ic_chw_task);
1216 taskqueue_unblock(ic->ic_tq);
1220 * Check to see whether the current channel needs reset.
1222 * Some devices don't handle being given an invalid channel
1223 * in their operating mode very well (eg wpi(4) will throw a
1224 * firmware exception.)
1226 * Return 0 if we're ok, 1 if the channel needs to be reset.
1228 * See PR kern/202502.
1231 ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
1233 struct ieee80211com *ic = vap->iv_ic;
1235 if ((vap->iv_opmode == IEEE80211_M_IBSS &&
1236 IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
1237 (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1238 IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
1244 * Reset the curchan to a known good state.
1247 ieee80211_start_reset_chan(struct ieee80211vap *vap)
1249 struct ieee80211com *ic = vap->iv_ic;
1251 ic->ic_curchan = &ic->ic_channels[0];
1255 * Start a vap running. If this is the first vap to be
1256 * set running on the underlying device then we
1257 * automatically bring the device up.
1260 ieee80211_start_locked(struct ieee80211vap *vap)
1262 struct ifnet *ifp = vap->iv_ifp;
1263 struct ieee80211com *ic = vap->iv_ic;
1265 IEEE80211_LOCK_ASSERT(ic);
1267 IEEE80211_DPRINTF(vap,
1268 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1269 "start running, %d vaps running\n", ic->ic_nrunning);
1271 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1273 * Mark us running. Note that it's ok to do this first;
1274 * if we need to bring the parent device up we defer that
1275 * to avoid dropping the com lock. We expect the device
1276 * to respond to being marked up by calling back into us
1277 * through ieee80211_start_all at which point we'll come
1278 * back in here and complete the work.
1280 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1282 * We are not running; if this we are the first vap
1283 * to be brought up auto-up the parent if necessary.
1285 if (ic->ic_nrunning++ == 0) {
1287 /* reset the channel to a known good channel */
1288 if (ieee80211_start_check_reset_chan(vap))
1289 ieee80211_start_reset_chan(vap);
1291 IEEE80211_DPRINTF(vap,
1292 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1293 "%s: up parent %s\n", __func__, ic->ic_name);
1294 ieee80211_runtask(ic, &ic->ic_parent_task);
1299 * If the parent is up and running, then kick the
1300 * 802.11 state machine as appropriate.
1302 if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1303 if (vap->iv_opmode == IEEE80211_M_STA) {
1305 /* XXX bypasses scan too easily; disable for now */
1307 * Try to be intelligent about clocking the state
1308 * machine. If we're currently in RUN state then
1309 * we should be able to apply any new state/parameters
1310 * simply by re-associating. Otherwise we need to
1311 * re-scan to select an appropriate ap.
1313 if (vap->iv_state >= IEEE80211_S_RUN)
1314 ieee80211_new_state_locked(vap,
1315 IEEE80211_S_ASSOC, 1);
1318 ieee80211_new_state_locked(vap,
1319 IEEE80211_S_SCAN, 0);
1322 * For monitor+wds mode there's nothing to do but
1323 * start running. Otherwise if this is the first
1324 * vap to be brought up, start a scan which may be
1325 * preempted if the station is locked to a particular
1328 vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1329 if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1330 vap->iv_opmode == IEEE80211_M_WDS)
1331 ieee80211_new_state_locked(vap,
1332 IEEE80211_S_RUN, -1);
1334 ieee80211_new_state_locked(vap,
1335 IEEE80211_S_SCAN, 0);
1341 * Start a single vap.
1344 ieee80211_init(void *arg)
1346 struct ieee80211vap *vap = arg;
1348 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1351 IEEE80211_LOCK(vap->iv_ic);
1352 ieee80211_start_locked(vap);
1353 IEEE80211_UNLOCK(vap->iv_ic);
1357 * Start all runnable vap's on a device.
1360 ieee80211_start_all(struct ieee80211com *ic)
1362 struct ieee80211vap *vap;
1365 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1366 struct ifnet *ifp = vap->iv_ifp;
1367 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1368 ieee80211_start_locked(vap);
1370 IEEE80211_UNLOCK(ic);
1374 * Stop a vap. We force it down using the state machine
1375 * then mark it's ifnet not running. If this is the last
1376 * vap running on the underlying device then we close it
1377 * too to insure it will be properly initialized when the
1378 * next vap is brought up.
1381 ieee80211_stop_locked(struct ieee80211vap *vap)
1383 struct ieee80211com *ic = vap->iv_ic;
1384 struct ifnet *ifp = vap->iv_ifp;
1386 IEEE80211_LOCK_ASSERT(ic);
1388 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1389 "stop running, %d vaps running\n", ic->ic_nrunning);
1391 ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1392 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1393 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; /* mark us stopped */
1394 if (--ic->ic_nrunning == 0) {
1395 IEEE80211_DPRINTF(vap,
1396 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1397 "down parent %s\n", ic->ic_name);
1398 ieee80211_runtask(ic, &ic->ic_parent_task);
1404 ieee80211_stop(struct ieee80211vap *vap)
1406 struct ieee80211com *ic = vap->iv_ic;
1409 ieee80211_stop_locked(vap);
1410 IEEE80211_UNLOCK(ic);
1414 * Stop all vap's running on a device.
1417 ieee80211_stop_all(struct ieee80211com *ic)
1419 struct ieee80211vap *vap;
1422 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1423 struct ifnet *ifp = vap->iv_ifp;
1424 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1425 ieee80211_stop_locked(vap);
1427 IEEE80211_UNLOCK(ic);
1429 ieee80211_waitfor_parent(ic);
1433 * Stop all vap's running on a device and arrange
1434 * for those that were running to be resumed.
1437 ieee80211_suspend_all(struct ieee80211com *ic)
1439 struct ieee80211vap *vap;
1442 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1443 struct ifnet *ifp = vap->iv_ifp;
1444 if (IFNET_IS_UP_RUNNING(ifp)) { /* NB: avoid recursion */
1445 vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1446 ieee80211_stop_locked(vap);
1449 IEEE80211_UNLOCK(ic);
1451 ieee80211_waitfor_parent(ic);
1455 * Start all vap's marked for resume.
1458 ieee80211_resume_all(struct ieee80211com *ic)
1460 struct ieee80211vap *vap;
1463 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1464 struct ifnet *ifp = vap->iv_ifp;
1465 if (!IFNET_IS_UP_RUNNING(ifp) &&
1466 (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1467 vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1468 ieee80211_start_locked(vap);
1471 IEEE80211_UNLOCK(ic);
1475 ieee80211_beacon_miss(struct ieee80211com *ic)
1478 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1479 /* Process in a taskq, the handler may reenter the driver */
1480 ieee80211_runtask(ic, &ic->ic_bmiss_task);
1482 IEEE80211_UNLOCK(ic);
1486 beacon_miss(void *arg, int npending)
1488 struct ieee80211com *ic = arg;
1489 struct ieee80211vap *vap;
1492 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1494 * We only pass events through for sta vap's in RUN state;
1495 * may be too restrictive but for now this saves all the
1496 * handlers duplicating these checks.
1498 if (vap->iv_opmode == IEEE80211_M_STA &&
1499 vap->iv_state >= IEEE80211_S_RUN &&
1500 vap->iv_bmiss != NULL)
1503 IEEE80211_UNLOCK(ic);
1507 beacon_swmiss(void *arg, int npending)
1509 struct ieee80211vap *vap = arg;
1510 struct ieee80211com *ic = vap->iv_ic;
1513 if (vap->iv_state == IEEE80211_S_RUN) {
1514 /* XXX Call multiple times if npending > zero? */
1517 IEEE80211_UNLOCK(ic);
1521 * Software beacon miss handling. Check if any beacons
1522 * were received in the last period. If not post a
1523 * beacon miss; otherwise reset the counter.
1526 ieee80211_swbmiss(void *arg)
1528 struct ieee80211vap *vap = arg;
1529 struct ieee80211com *ic = vap->iv_ic;
1531 IEEE80211_LOCK_ASSERT(ic);
1533 /* XXX sleep state? */
1534 KASSERT(vap->iv_state == IEEE80211_S_RUN,
1535 ("wrong state %d", vap->iv_state));
1537 if (ic->ic_flags & IEEE80211_F_SCAN) {
1539 * If scanning just ignore and reset state. If we get a
1540 * bmiss after coming out of scan because we haven't had
1541 * time to receive a beacon then we should probe the AP
1542 * before posting a real bmiss (unless iv_bmiss_max has
1543 * been artifiically lowered). A cleaner solution might
1544 * be to disable the timer on scan start/end but to handle
1545 * case of multiple sta vap's we'd need to disable the
1546 * timers of all affected vap's.
1548 vap->iv_swbmiss_count = 0;
1549 } else if (vap->iv_swbmiss_count == 0) {
1550 if (vap->iv_bmiss != NULL)
1551 ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1553 vap->iv_swbmiss_count = 0;
1554 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1555 ieee80211_swbmiss, vap);
1559 * Start an 802.11h channel switch. We record the parameters,
1560 * mark the operation pending, notify each vap through the
1561 * beacon update mechanism so it can update the beacon frame
1562 * contents, and then switch vap's to CSA state to block outbound
1563 * traffic. Devices that handle CSA directly can use the state
1564 * switch to do the right thing so long as they call
1565 * ieee80211_csa_completeswitch when it's time to complete the
1566 * channel change. Devices that depend on the net80211 layer can
1567 * use ieee80211_beacon_update to handle the countdown and the
1571 ieee80211_csa_startswitch(struct ieee80211com *ic,
1572 struct ieee80211_channel *c, int mode, int count)
1574 struct ieee80211vap *vap;
1576 IEEE80211_LOCK_ASSERT(ic);
1578 ic->ic_csa_newchan = c;
1579 ic->ic_csa_mode = mode;
1580 ic->ic_csa_count = count;
1581 ic->ic_flags |= IEEE80211_F_CSAPENDING;
1582 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1583 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1584 vap->iv_opmode == IEEE80211_M_IBSS ||
1585 vap->iv_opmode == IEEE80211_M_MBSS)
1586 ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1587 /* switch to CSA state to block outbound traffic */
1588 if (vap->iv_state == IEEE80211_S_RUN)
1589 ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1591 ieee80211_notify_csa(ic, c, mode, count);
1595 * Complete the channel switch by transitioning all CSA VAPs to RUN.
1596 * This is called by both the completion and cancellation functions
1597 * so each VAP is placed back in the RUN state and can thus transmit.
1600 csa_completeswitch(struct ieee80211com *ic)
1602 struct ieee80211vap *vap;
1604 ic->ic_csa_newchan = NULL;
1605 ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1607 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1608 if (vap->iv_state == IEEE80211_S_CSA)
1609 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1613 * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1614 * We clear state and move all vap's in CSA state to RUN state
1615 * so they can again transmit.
1617 * Although this may not be completely correct, update the BSS channel
1618 * for each VAP to the newly configured channel. The setcurchan sets
1619 * the current operating channel for the interface (so the radio does
1620 * switch over) but the VAP BSS isn't updated, leading to incorrectly
1621 * reported information via ioctl.
1624 ieee80211_csa_completeswitch(struct ieee80211com *ic)
1626 struct ieee80211vap *vap;
1628 IEEE80211_LOCK_ASSERT(ic);
1630 KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1632 ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1633 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1634 if (vap->iv_state == IEEE80211_S_CSA)
1635 vap->iv_bss->ni_chan = ic->ic_curchan;
1637 csa_completeswitch(ic);
1641 * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1642 * We clear state and move all vap's in CSA state to RUN state
1643 * so they can again transmit.
1646 ieee80211_csa_cancelswitch(struct ieee80211com *ic)
1648 IEEE80211_LOCK_ASSERT(ic);
1650 csa_completeswitch(ic);
1654 * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1655 * We clear state and move all vap's in CAC state to RUN state.
1658 ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1660 struct ieee80211com *ic = vap0->iv_ic;
1661 struct ieee80211vap *vap;
1665 * Complete CAC state change for lead vap first; then
1666 * clock all the other vap's waiting.
1668 KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1669 ("wrong state %d", vap0->iv_state));
1670 ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1672 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1673 if (vap->iv_state == IEEE80211_S_CAC)
1674 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1675 IEEE80211_UNLOCK(ic);
1679 * Force all vap's other than the specified vap to the INIT state
1680 * and mark them as waiting for a scan to complete. These vaps
1681 * will be brought up when the scan completes and the scanning vap
1682 * reaches RUN state by wakeupwaiting.
1685 markwaiting(struct ieee80211vap *vap0)
1687 struct ieee80211com *ic = vap0->iv_ic;
1688 struct ieee80211vap *vap;
1690 IEEE80211_LOCK_ASSERT(ic);
1693 * A vap list entry can not disappear since we are running on the
1694 * taskqueue and a vap destroy will queue and drain another state
1697 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1700 if (vap->iv_state != IEEE80211_S_INIT) {
1701 /* NB: iv_newstate may drop the lock */
1702 vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1703 IEEE80211_LOCK_ASSERT(ic);
1704 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1710 * Wakeup all vap's waiting for a scan to complete. This is the
1711 * companion to markwaiting (above) and is used to coordinate
1712 * multiple vaps scanning.
1713 * This is called from the state taskqueue.
1716 wakeupwaiting(struct ieee80211vap *vap0)
1718 struct ieee80211com *ic = vap0->iv_ic;
1719 struct ieee80211vap *vap;
1721 IEEE80211_LOCK_ASSERT(ic);
1724 * A vap list entry can not disappear since we are running on the
1725 * taskqueue and a vap destroy will queue and drain another state
1728 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1731 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1732 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1733 /* NB: sta's cannot go INIT->RUN */
1734 /* NB: iv_newstate may drop the lock */
1735 vap->iv_newstate(vap,
1736 vap->iv_opmode == IEEE80211_M_STA ?
1737 IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1738 IEEE80211_LOCK_ASSERT(ic);
1744 * Handle post state change work common to all operating modes.
1747 ieee80211_newstate_cb(void *xvap, int npending)
1749 struct ieee80211vap *vap = xvap;
1750 struct ieee80211com *ic = vap->iv_ic;
1751 enum ieee80211_state nstate, ostate;
1755 nstate = vap->iv_nstate;
1756 arg = vap->iv_nstate_arg;
1758 if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
1760 * We have been requested to drop back to the INIT before
1761 * proceeding to the new state.
1763 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1764 "%s: %s -> %s arg %d\n", __func__,
1765 ieee80211_state_name[vap->iv_state],
1766 ieee80211_state_name[IEEE80211_S_INIT], arg);
1767 vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1768 IEEE80211_LOCK_ASSERT(ic);
1769 vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
1772 ostate = vap->iv_state;
1773 if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
1775 * SCAN was forced; e.g. on beacon miss. Force other running
1776 * vap's to INIT state and mark them as waiting for the scan to
1777 * complete. This insures they don't interfere with our
1778 * scanning. Since we are single threaded the vaps can not
1779 * transition again while we are executing.
1781 * XXX not always right, assumes ap follows sta
1785 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1786 "%s: %s -> %s arg %d\n", __func__,
1787 ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
1789 rc = vap->iv_newstate(vap, nstate, arg);
1790 IEEE80211_LOCK_ASSERT(ic);
1791 vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
1793 /* State transition failed */
1794 KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
1795 KASSERT(nstate != IEEE80211_S_INIT,
1796 ("INIT state change failed"));
1797 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1798 "%s: %s returned error %d\n", __func__,
1799 ieee80211_state_name[nstate], rc);
1803 /* No actual transition, skip post processing */
1804 if (ostate == nstate)
1807 if (nstate == IEEE80211_S_RUN) {
1809 * OACTIVE may be set on the vap if the upper layer
1810 * tried to transmit (e.g. IPv6 NDP) before we reach
1811 * RUN state. Clear it and restart xmit.
1813 * Note this can also happen as a result of SLEEP->RUN
1814 * (i.e. coming out of power save mode).
1816 vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1819 * XXX TODO Kick-start a VAP queue - this should be a method!
1822 /* bring up any vaps waiting on us */
1824 } else if (nstate == IEEE80211_S_INIT) {
1826 * Flush the scan cache if we did the last scan (XXX?)
1827 * and flush any frames on send queues from this vap.
1828 * Note the mgt q is used only for legacy drivers and
1829 * will go away shortly.
1831 ieee80211_scan_flush(vap);
1834 * XXX TODO: ic/vap queue flush
1838 IEEE80211_UNLOCK(ic);
1842 * Public interface for initiating a state machine change.
1843 * This routine single-threads the request and coordinates
1844 * the scheduling of multiple vaps for the purpose of selecting
1845 * an operating channel. Specifically the following scenarios
1847 * o only one vap can be selecting a channel so on transition to
1848 * SCAN state if another vap is already scanning then
1849 * mark the caller for later processing and return without
1850 * doing anything (XXX? expectations by caller of synchronous operation)
1851 * o only one vap can be doing CAC of a channel so on transition to
1852 * CAC state if another vap is already scanning for radar then
1853 * mark the caller for later processing and return without
1854 * doing anything (XXX? expectations by caller of synchronous operation)
1855 * o if another vap is already running when a request is made
1856 * to SCAN then an operating channel has been chosen; bypass
1857 * the scan and just join the channel
1859 * Note that the state change call is done through the iv_newstate
1860 * method pointer so any driver routine gets invoked. The driver
1861 * will normally call back into operating mode-specific
1862 * ieee80211_newstate routines (below) unless it needs to completely
1863 * bypass the state machine (e.g. because the firmware has it's
1864 * own idea how things should work). Bypassing the net80211 layer
1865 * is usually a mistake and indicates lack of proper integration
1866 * with the net80211 layer.
1869 ieee80211_new_state_locked(struct ieee80211vap *vap,
1870 enum ieee80211_state nstate, int arg)
1872 struct ieee80211com *ic = vap->iv_ic;
1873 struct ieee80211vap *vp;
1874 enum ieee80211_state ostate;
1875 int nrunning, nscanning;
1877 IEEE80211_LOCK_ASSERT(ic);
1879 if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
1880 if (vap->iv_nstate == IEEE80211_S_INIT) {
1882 * XXX The vap is being stopped, do no allow any other
1883 * state changes until this is completed.
1886 } else if (vap->iv_state != vap->iv_nstate) {
1888 /* Warn if the previous state hasn't completed. */
1889 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1890 "%s: pending %s -> %s transition lost\n", __func__,
1891 ieee80211_state_name[vap->iv_state],
1892 ieee80211_state_name[vap->iv_nstate]);
1894 /* XXX temporarily enable to identify issues */
1895 if_printf(vap->iv_ifp,
1896 "%s: pending %s -> %s transition lost\n",
1897 __func__, ieee80211_state_name[vap->iv_state],
1898 ieee80211_state_name[vap->iv_nstate]);
1903 nrunning = nscanning = 0;
1904 /* XXX can track this state instead of calculating */
1905 TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1907 if (vp->iv_state >= IEEE80211_S_RUN)
1909 /* XXX doesn't handle bg scan */
1910 /* NB: CAC+AUTH+ASSOC treated like SCAN */
1911 else if (vp->iv_state > IEEE80211_S_INIT)
1915 ostate = vap->iv_state;
1916 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1917 "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1918 ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1919 nrunning, nscanning);
1921 case IEEE80211_S_SCAN:
1922 if (ostate == IEEE80211_S_INIT) {
1924 * INIT -> SCAN happens on initial bringup.
1926 KASSERT(!(nscanning && nrunning),
1927 ("%d scanning and %d running", nscanning, nrunning));
1930 * Someone is scanning, defer our state
1931 * change until the work has completed.
1933 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1934 "%s: defer %s -> %s\n",
1935 __func__, ieee80211_state_name[ostate],
1936 ieee80211_state_name[nstate]);
1937 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1942 * Someone is operating; just join the channel
1946 /* XXX check each opmode, adhoc? */
1947 if (vap->iv_opmode == IEEE80211_M_STA)
1948 nstate = IEEE80211_S_SCAN;
1950 nstate = IEEE80211_S_RUN;
1951 #ifdef IEEE80211_DEBUG
1952 if (nstate != IEEE80211_S_SCAN) {
1953 IEEE80211_DPRINTF(vap,
1954 IEEE80211_MSG_STATE,
1955 "%s: override, now %s -> %s\n",
1957 ieee80211_state_name[ostate],
1958 ieee80211_state_name[nstate]);
1964 case IEEE80211_S_RUN:
1965 if (vap->iv_opmode == IEEE80211_M_WDS &&
1966 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
1969 * Legacy WDS with someone else scanning; don't
1970 * go online until that completes as we should
1971 * follow the other vap to the channel they choose.
1973 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1974 "%s: defer %s -> %s (legacy WDS)\n", __func__,
1975 ieee80211_state_name[ostate],
1976 ieee80211_state_name[nstate]);
1977 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1980 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1981 IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
1982 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
1983 !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
1985 * This is a DFS channel, transition to CAC state
1986 * instead of RUN. This allows us to initiate
1987 * Channel Availability Check (CAC) as specified
1990 nstate = IEEE80211_S_CAC;
1991 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1992 "%s: override %s -> %s (DFS)\n", __func__,
1993 ieee80211_state_name[ostate],
1994 ieee80211_state_name[nstate]);
1997 case IEEE80211_S_INIT:
1998 /* cancel any scan in progress */
1999 ieee80211_cancel_scan(vap);
2000 if (ostate == IEEE80211_S_INIT ) {
2001 /* XXX don't believe this */
2002 /* INIT -> INIT. nothing to do */
2003 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2009 /* defer the state change to a thread */
2010 vap->iv_nstate = nstate;
2011 vap->iv_nstate_arg = arg;
2012 vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
2013 ieee80211_runtask(ic, &vap->iv_nstate_task);
2018 ieee80211_new_state(struct ieee80211vap *vap,
2019 enum ieee80211_state nstate, int arg)
2021 struct ieee80211com *ic = vap->iv_ic;
2025 rc = ieee80211_new_state_locked(vap, nstate, arg);
2026 IEEE80211_UNLOCK(ic);