2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Atsushi Onoe
5 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
6 * Copyright (c) 2012 IEEE
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * IEEE 802.11 protocol support.
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
49 #include <net/if_var.h>
50 #include <net/if_media.h>
51 #include <net/ethernet.h> /* XXX for ether_sprintf */
53 #include <net80211/ieee80211_var.h>
54 #include <net80211/ieee80211_adhoc.h>
55 #include <net80211/ieee80211_sta.h>
56 #include <net80211/ieee80211_hostap.h>
57 #include <net80211/ieee80211_wds.h>
58 #ifdef IEEE80211_SUPPORT_MESH
59 #include <net80211/ieee80211_mesh.h>
61 #include <net80211/ieee80211_monitor.h>
62 #include <net80211/ieee80211_input.h>
65 #define AGGRESSIVE_MODE_SWITCH_HYSTERESIS 3 /* pkts / 100ms */
66 #define HIGH_PRI_SWITCH_THRESH 10 /* pkts / 100ms */
68 const char *mgt_subtype_name[] = {
69 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp",
70 "probe_req", "probe_resp", "timing_adv", "reserved#7",
71 "beacon", "atim", "disassoc", "auth",
72 "deauth", "action", "action_noack", "reserved#15"
74 const char *ctl_subtype_name[] = {
75 "reserved#0", "reserved#1", "reserved#2", "reserved#3",
76 "reserved#4", "reserved#5", "reserved#6", "control_wrap",
77 "bar", "ba", "ps_poll", "rts",
78 "cts", "ack", "cf_end", "cf_end_ack"
80 const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
81 "IBSS", /* IEEE80211_M_IBSS */
82 "STA", /* IEEE80211_M_STA */
83 "WDS", /* IEEE80211_M_WDS */
84 "AHDEMO", /* IEEE80211_M_AHDEMO */
85 "HOSTAP", /* IEEE80211_M_HOSTAP */
86 "MONITOR", /* IEEE80211_M_MONITOR */
87 "MBSS" /* IEEE80211_M_MBSS */
89 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
90 "INIT", /* IEEE80211_S_INIT */
91 "SCAN", /* IEEE80211_S_SCAN */
92 "AUTH", /* IEEE80211_S_AUTH */
93 "ASSOC", /* IEEE80211_S_ASSOC */
94 "CAC", /* IEEE80211_S_CAC */
95 "RUN", /* IEEE80211_S_RUN */
96 "CSA", /* IEEE80211_S_CSA */
97 "SLEEP", /* IEEE80211_S_SLEEP */
99 const char *ieee80211_wme_acnames[] = {
108 * Reason code descriptions were (mostly) obtained from
109 * IEEE Std 802.11-2012, pp. 442-445 Table 8-36.
112 ieee80211_reason_to_string(uint16_t reason)
115 case IEEE80211_REASON_UNSPECIFIED:
116 return ("unspecified");
117 case IEEE80211_REASON_AUTH_EXPIRE:
118 return ("previous authentication is expired");
119 case IEEE80211_REASON_AUTH_LEAVE:
120 return ("sending STA is leaving/has left IBSS or ESS");
121 case IEEE80211_REASON_ASSOC_EXPIRE:
122 return ("disassociated due to inactivity");
123 case IEEE80211_REASON_ASSOC_TOOMANY:
124 return ("too many associated STAs");
125 case IEEE80211_REASON_NOT_AUTHED:
126 return ("class 2 frame received from nonauthenticated STA");
127 case IEEE80211_REASON_NOT_ASSOCED:
128 return ("class 3 frame received from nonassociated STA");
129 case IEEE80211_REASON_ASSOC_LEAVE:
130 return ("sending STA is leaving/has left BSS");
131 case IEEE80211_REASON_ASSOC_NOT_AUTHED:
132 return ("STA requesting (re)association is not authenticated");
133 case IEEE80211_REASON_DISASSOC_PWRCAP_BAD:
134 return ("information in the Power Capability element is "
136 case IEEE80211_REASON_DISASSOC_SUPCHAN_BAD:
137 return ("information in the Supported Channels element is "
139 case IEEE80211_REASON_IE_INVALID:
140 return ("invalid element");
141 case IEEE80211_REASON_MIC_FAILURE:
142 return ("MIC failure");
143 case IEEE80211_REASON_4WAY_HANDSHAKE_TIMEOUT:
144 return ("4-Way handshake timeout");
145 case IEEE80211_REASON_GROUP_KEY_UPDATE_TIMEOUT:
146 return ("group key update timeout");
147 case IEEE80211_REASON_IE_IN_4WAY_DIFFERS:
148 return ("element in 4-Way handshake different from "
149 "(re)association request/probe response/beacon frame");
150 case IEEE80211_REASON_GROUP_CIPHER_INVALID:
151 return ("invalid group cipher");
152 case IEEE80211_REASON_PAIRWISE_CIPHER_INVALID:
153 return ("invalid pairwise cipher");
154 case IEEE80211_REASON_AKMP_INVALID:
155 return ("invalid AKMP");
156 case IEEE80211_REASON_UNSUPP_RSN_IE_VERSION:
157 return ("unsupported version in RSN IE");
158 case IEEE80211_REASON_INVALID_RSN_IE_CAP:
159 return ("invalid capabilities in RSN IE");
160 case IEEE80211_REASON_802_1X_AUTH_FAILED:
161 return ("IEEE 802.1X authentication failed");
162 case IEEE80211_REASON_CIPHER_SUITE_REJECTED:
163 return ("cipher suite rejected because of the security "
165 case IEEE80211_REASON_UNSPECIFIED_QOS:
166 return ("unspecified (QoS-related)");
167 case IEEE80211_REASON_INSUFFICIENT_BW:
168 return ("QoS AP lacks sufficient bandwidth for this QoS STA");
169 case IEEE80211_REASON_TOOMANY_FRAMES:
170 return ("too many frames need to be acknowledged");
171 case IEEE80211_REASON_OUTSIDE_TXOP:
172 return ("STA is transmitting outside the limits of its TXOPs");
173 case IEEE80211_REASON_LEAVING_QBSS:
174 return ("requested from peer STA (the STA is "
175 "resetting/leaving the BSS)");
176 case IEEE80211_REASON_BAD_MECHANISM:
177 return ("requested from peer STA (it does not want to use "
179 case IEEE80211_REASON_SETUP_NEEDED:
180 return ("requested from peer STA (setup is required for the "
182 case IEEE80211_REASON_TIMEOUT:
183 return ("requested from peer STA (timeout)");
184 case IEEE80211_REASON_PEER_LINK_CANCELED:
185 return ("SME cancels the mesh peering instance (not related "
186 "to the maximum number of peer mesh STAs)");
187 case IEEE80211_REASON_MESH_MAX_PEERS:
188 return ("maximum number of peer mesh STAs was reached");
189 case IEEE80211_REASON_MESH_CPVIOLATION:
190 return ("the received information violates the Mesh "
191 "Configuration policy configured in the mesh STA "
193 case IEEE80211_REASON_MESH_CLOSE_RCVD:
194 return ("the mesh STA has received a Mesh Peering Close "
195 "message requesting to close the mesh peering");
196 case IEEE80211_REASON_MESH_MAX_RETRIES:
197 return ("the mesh STA has resent dot11MeshMaxRetries Mesh "
198 "Peering Open messages, without receiving a Mesh "
199 "Peering Confirm message");
200 case IEEE80211_REASON_MESH_CONFIRM_TIMEOUT:
201 return ("the confirmTimer for the mesh peering instance times "
203 case IEEE80211_REASON_MESH_INVALID_GTK:
204 return ("the mesh STA fails to unwrap the GTK or the values "
205 "in the wrapped contents do not match");
206 case IEEE80211_REASON_MESH_INCONS_PARAMS:
207 return ("the mesh STA receives inconsistent information about "
208 "the mesh parameters between Mesh Peering Management "
210 case IEEE80211_REASON_MESH_INVALID_SECURITY:
211 return ("the mesh STA fails the authenticated mesh peering "
212 "exchange because due to failure in selecting "
213 "pairwise/group ciphersuite");
214 case IEEE80211_REASON_MESH_PERR_NO_PROXY:
215 return ("the mesh STA does not have proxy information for "
216 "this external destination");
217 case IEEE80211_REASON_MESH_PERR_NO_FI:
218 return ("the mesh STA does not have forwarding information "
219 "for this destination");
220 case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
221 return ("the mesh STA determines that the link to the next "
222 "hop of an active path in its forwarding information "
223 "is no longer usable");
224 case IEEE80211_REASON_MESH_MAC_ALRDY_EXISTS_MBSS:
225 return ("the MAC address of the STA already exists in the "
227 case IEEE80211_REASON_MESH_CHAN_SWITCH_REG:
228 return ("the mesh STA performs channel switch to meet "
229 "regulatory requirements");
230 case IEEE80211_REASON_MESH_CHAN_SWITCH_UNSPEC:
231 return ("the mesh STA performs channel switch with "
232 "unspecified reason");
234 return ("reserved/unknown");
238 static void beacon_miss(void *, int);
239 static void beacon_swmiss(void *, int);
240 static void parent_updown(void *, int);
241 static void update_mcast(void *, int);
242 static void update_promisc(void *, int);
243 static void update_channel(void *, int);
244 static void update_chw(void *, int);
245 static void vap_update_wme(void *, int);
246 static void vap_update_slot(void *, int);
247 static void restart_vaps(void *, int);
248 static void vap_update_erp_protmode(void *, int);
249 static void vap_update_preamble(void *, int);
250 static void vap_update_ht_protmode(void *, int);
251 static void ieee80211_newstate_cb(void *, int);
252 static struct ieee80211_node *vap_update_bss(struct ieee80211vap *,
253 struct ieee80211_node *);
256 null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
257 const struct ieee80211_bpf_params *params)
260 ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
266 ieee80211_proto_attach(struct ieee80211com *ic)
270 /* override the 802.3 setting */
271 hdrlen = ic->ic_headroom
272 + sizeof(struct ieee80211_qosframe_addr4)
273 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
274 + IEEE80211_WEP_EXTIVLEN;
275 /* XXX no way to recalculate on ifdetach */
276 if (ALIGN(hdrlen) > max_linkhdr) {
277 /* XXX sanity check... */
278 max_linkhdr = ALIGN(hdrlen);
279 max_hdr = max_linkhdr + max_protohdr;
280 max_datalen = MHLEN - max_hdr;
282 //ic->ic_protmode = IEEE80211_PROT_CTSONLY;
284 TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
285 TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
286 TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
287 TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
288 TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
289 TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
290 TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
292 ic->ic_wme.wme_hipri_switch_hysteresis =
293 AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
295 /* initialize management frame handlers */
296 ic->ic_send_mgmt = ieee80211_send_mgmt;
297 ic->ic_raw_xmit = null_raw_xmit;
299 ieee80211_adhoc_attach(ic);
300 ieee80211_sta_attach(ic);
301 ieee80211_wds_attach(ic);
302 ieee80211_hostap_attach(ic);
303 #ifdef IEEE80211_SUPPORT_MESH
304 ieee80211_mesh_attach(ic);
306 ieee80211_monitor_attach(ic);
310 ieee80211_proto_detach(struct ieee80211com *ic)
312 ieee80211_monitor_detach(ic);
313 #ifdef IEEE80211_SUPPORT_MESH
314 ieee80211_mesh_detach(ic);
316 ieee80211_hostap_detach(ic);
317 ieee80211_wds_detach(ic);
318 ieee80211_adhoc_detach(ic);
319 ieee80211_sta_detach(ic);
323 null_update_beacon(struct ieee80211vap *vap, int item)
328 ieee80211_proto_vattach(struct ieee80211vap *vap)
330 struct ieee80211com *ic = vap->iv_ic;
331 struct ifnet *ifp = vap->iv_ifp;
334 /* override the 802.3 setting */
335 ifp->if_hdrlen = ic->ic_headroom
336 + sizeof(struct ieee80211_qosframe_addr4)
337 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
338 + IEEE80211_WEP_EXTIVLEN;
340 vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
341 vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
342 vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
343 callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
344 callout_init(&vap->iv_mgtsend, 1);
345 TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
346 TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
347 TASK_INIT(&vap->iv_wme_task, 0, vap_update_wme, vap);
348 TASK_INIT(&vap->iv_slot_task, 0, vap_update_slot, vap);
349 TASK_INIT(&vap->iv_erp_protmode_task, 0, vap_update_erp_protmode, vap);
350 TASK_INIT(&vap->iv_ht_protmode_task, 0, vap_update_ht_protmode, vap);
351 TASK_INIT(&vap->iv_preamble_task, 0, vap_update_preamble, vap);
353 * Install default tx rate handling: no fixed rate, lowest
354 * supported rate for mgmt and multicast frames. Default
355 * max retry count. These settings can be changed by the
356 * driver and/or user applications.
358 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
359 if (isclr(ic->ic_modecaps, i))
362 const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
364 vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
367 * Setting the management rate to MCS 0 assumes that the
368 * BSS Basic rate set is empty and the BSS Basic MCS set
371 * Since we're not checking this, default to the lowest
372 * defined rate for this mode.
374 * At least one 11n AP (DLINK DIR-825) is reported to drop
375 * some MCS management traffic (eg BA response frames.)
377 * See also: 9.6.0 of the 802.11n-2009 specification.
380 if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
381 vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
382 vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
384 vap->iv_txparms[i].mgmtrate =
385 rs->rs_rates[0] & IEEE80211_RATE_VAL;
386 vap->iv_txparms[i].mcastrate =
387 rs->rs_rates[0] & IEEE80211_RATE_VAL;
390 vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
391 vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
392 vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
394 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
396 vap->iv_update_beacon = null_update_beacon;
397 vap->iv_deliver_data = ieee80211_deliver_data;
398 vap->iv_protmode = IEEE80211_PROT_CTSONLY;
399 vap->iv_update_bss = vap_update_bss;
401 /* attach support for operating mode */
402 ic->ic_vattach[vap->iv_opmode](vap);
406 ieee80211_proto_vdetach(struct ieee80211vap *vap)
408 #define FREEAPPIE(ie) do { \
410 IEEE80211_FREE(ie, M_80211_NODE_IE); \
413 * Detach operating mode module.
415 if (vap->iv_opdetach != NULL)
416 vap->iv_opdetach(vap);
418 * This should not be needed as we detach when reseting
419 * the state but be conservative here since the
420 * authenticator may do things like spawn kernel threads.
422 if (vap->iv_auth->ia_detach != NULL)
423 vap->iv_auth->ia_detach(vap);
425 * Detach any ACL'ator.
427 if (vap->iv_acl != NULL)
428 vap->iv_acl->iac_detach(vap);
430 FREEAPPIE(vap->iv_appie_beacon);
431 FREEAPPIE(vap->iv_appie_probereq);
432 FREEAPPIE(vap->iv_appie_proberesp);
433 FREEAPPIE(vap->iv_appie_assocreq);
434 FREEAPPIE(vap->iv_appie_assocresp);
435 FREEAPPIE(vap->iv_appie_wpa);
440 * Simple-minded authenticator module support.
443 #define IEEE80211_AUTH_MAX (IEEE80211_AUTH_WPA+1)
444 /* XXX well-known names */
445 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
446 "wlan_internal", /* IEEE80211_AUTH_NONE */
447 "wlan_internal", /* IEEE80211_AUTH_OPEN */
448 "wlan_internal", /* IEEE80211_AUTH_SHARED */
449 "wlan_xauth", /* IEEE80211_AUTH_8021X */
450 "wlan_internal", /* IEEE80211_AUTH_AUTO */
451 "wlan_xauth", /* IEEE80211_AUTH_WPA */
453 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
455 static const struct ieee80211_authenticator auth_internal = {
456 .ia_name = "wlan_internal",
459 .ia_node_join = NULL,
460 .ia_node_leave = NULL,
464 * Setup internal authenticators once; they are never unregistered.
467 ieee80211_auth_setup(void)
469 ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
470 ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
471 ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
473 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
475 const struct ieee80211_authenticator *
476 ieee80211_authenticator_get(int auth)
478 if (auth >= IEEE80211_AUTH_MAX)
480 if (authenticators[auth] == NULL)
481 ieee80211_load_module(auth_modnames[auth]);
482 return authenticators[auth];
486 ieee80211_authenticator_register(int type,
487 const struct ieee80211_authenticator *auth)
489 if (type >= IEEE80211_AUTH_MAX)
491 authenticators[type] = auth;
495 ieee80211_authenticator_unregister(int type)
498 if (type >= IEEE80211_AUTH_MAX)
500 authenticators[type] = NULL;
504 * Very simple-minded ACL module support.
506 /* XXX just one for now */
507 static const struct ieee80211_aclator *acl = NULL;
510 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
512 printf("wlan: %s acl policy registered\n", iac->iac_name);
517 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
521 printf("wlan: %s acl policy unregistered\n", iac->iac_name);
524 const struct ieee80211_aclator *
525 ieee80211_aclator_get(const char *name)
528 ieee80211_load_module("wlan_acl");
529 return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
533 ieee80211_print_essid(const uint8_t *essid, int len)
538 if (len > IEEE80211_NWID_LEN)
539 len = IEEE80211_NWID_LEN;
540 /* determine printable or not */
541 for (i = 0, p = essid; i < len; i++, p++) {
542 if (*p < ' ' || *p > 0x7e)
547 for (i = 0, p = essid; i < len; i++, p++)
552 for (i = 0, p = essid; i < len; i++, p++)
558 ieee80211_dump_pkt(struct ieee80211com *ic,
559 const uint8_t *buf, int len, int rate, int rssi)
561 const struct ieee80211_frame *wh;
564 wh = (const struct ieee80211_frame *)buf;
565 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
566 case IEEE80211_FC1_DIR_NODS:
567 printf("NODS %s", ether_sprintf(wh->i_addr2));
568 printf("->%s", ether_sprintf(wh->i_addr1));
569 printf("(%s)", ether_sprintf(wh->i_addr3));
571 case IEEE80211_FC1_DIR_TODS:
572 printf("TODS %s", ether_sprintf(wh->i_addr2));
573 printf("->%s", ether_sprintf(wh->i_addr3));
574 printf("(%s)", ether_sprintf(wh->i_addr1));
576 case IEEE80211_FC1_DIR_FROMDS:
577 printf("FRDS %s", ether_sprintf(wh->i_addr3));
578 printf("->%s", ether_sprintf(wh->i_addr1));
579 printf("(%s)", ether_sprintf(wh->i_addr2));
581 case IEEE80211_FC1_DIR_DSTODS:
582 printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
583 printf("->%s", ether_sprintf(wh->i_addr3));
584 printf("(%s", ether_sprintf(wh->i_addr2));
585 printf("->%s)", ether_sprintf(wh->i_addr1));
588 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
589 case IEEE80211_FC0_TYPE_DATA:
592 case IEEE80211_FC0_TYPE_MGT:
593 printf(" %s", ieee80211_mgt_subtype_name(wh->i_fc[0]));
596 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
599 if (IEEE80211_QOS_HAS_SEQ(wh)) {
600 const struct ieee80211_qosframe *qwh =
601 (const struct ieee80211_qosframe *)buf;
602 printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
603 qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
605 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
608 off = ieee80211_anyhdrspace(ic, wh);
609 printf(" WEP [IV %.02x %.02x %.02x",
610 buf[off+0], buf[off+1], buf[off+2]);
611 if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
612 printf(" %.02x %.02x %.02x",
613 buf[off+4], buf[off+5], buf[off+6]);
614 printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
617 printf(" %dM", rate / 2);
619 printf(" +%d", rssi);
622 for (i = 0; i < len; i++) {
625 printf("%02x", buf[i]);
632 findrix(const struct ieee80211_rateset *rs, int r)
636 for (i = 0; i < rs->rs_nrates; i++)
637 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
643 ieee80211_fix_rate(struct ieee80211_node *ni,
644 struct ieee80211_rateset *nrs, int flags)
646 struct ieee80211vap *vap = ni->ni_vap;
647 struct ieee80211com *ic = ni->ni_ic;
648 int i, j, rix, error;
649 int okrate, badrate, fixedrate, ucastrate;
650 const struct ieee80211_rateset *srs;
654 okrate = badrate = 0;
655 ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
656 if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
658 * Workaround awkwardness with fixed rate. We are called
659 * to check both the legacy rate set and the HT rate set
660 * but we must apply any legacy fixed rate check only to the
661 * legacy rate set and vice versa. We cannot tell what type
662 * of rate set we've been given (legacy or HT) but we can
663 * distinguish the fixed rate type (MCS have 0x80 set).
664 * So to deal with this the caller communicates whether to
665 * check MCS or legacy rate using the flags and we use the
666 * type of any fixed rate to avoid applying an MCS to a
667 * legacy rate and vice versa.
669 if (ucastrate & 0x80) {
670 if (flags & IEEE80211_F_DOFRATE)
671 flags &= ~IEEE80211_F_DOFRATE;
672 } else if ((ucastrate & 0x80) == 0) {
673 if (flags & IEEE80211_F_DOFMCS)
674 flags &= ~IEEE80211_F_DOFMCS;
676 /* NB: required to make MCS match below work */
677 ucastrate &= IEEE80211_RATE_VAL;
679 fixedrate = IEEE80211_FIXED_RATE_NONE;
681 * XXX we are called to process both MCS and legacy rates;
682 * we must use the appropriate basic rate set or chaos will
683 * ensue; for now callers that want MCS must supply
684 * IEEE80211_F_DOBRS; at some point we'll need to split this
685 * function so there are two variants, one for MCS and one
688 if (flags & IEEE80211_F_DOBRS)
689 srs = (const struct ieee80211_rateset *)
690 ieee80211_get_suphtrates(ic, ni->ni_chan);
692 srs = ieee80211_get_suprates(ic, ni->ni_chan);
693 for (i = 0; i < nrs->rs_nrates; ) {
694 if (flags & IEEE80211_F_DOSORT) {
698 for (j = i + 1; j < nrs->rs_nrates; j++) {
699 if (IEEE80211_RV(nrs->rs_rates[i]) >
700 IEEE80211_RV(nrs->rs_rates[j])) {
701 r = nrs->rs_rates[i];
702 nrs->rs_rates[i] = nrs->rs_rates[j];
703 nrs->rs_rates[j] = r;
707 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
710 * Check for fixed rate.
715 * Check against supported rates.
717 rix = findrix(srs, r);
718 if (flags & IEEE80211_F_DONEGO) {
721 * A rate in the node's rate set is not
722 * supported. If this is a basic rate and we
723 * are operating as a STA then this is an error.
724 * Otherwise we just discard/ignore the rate.
726 if ((flags & IEEE80211_F_JOIN) &&
727 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
729 } else if ((flags & IEEE80211_F_JOIN) == 0) {
731 * Overwrite with the supported rate
732 * value so any basic rate bit is set.
734 nrs->rs_rates[i] = srs->rs_rates[rix];
737 if ((flags & IEEE80211_F_DODEL) && rix < 0) {
739 * Delete unacceptable rates.
742 for (j = i; j < nrs->rs_nrates; j++)
743 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
744 nrs->rs_rates[j] = 0;
748 okrate = nrs->rs_rates[i];
751 if (okrate == 0 || error != 0 ||
752 ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
753 fixedrate != ucastrate)) {
754 IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
755 "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
756 "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
757 return badrate | IEEE80211_RATE_BASIC;
759 return IEEE80211_RV(okrate);
763 * Reset 11g-related state.
765 * This is for per-VAP ERP/11g state.
767 * Eventually everything in ieee80211_reset_erp() will be
768 * per-VAP and in here.
771 ieee80211_vap_reset_erp(struct ieee80211vap *vap)
773 struct ieee80211com *ic = vap->iv_ic;
775 vap->iv_nonerpsta = 0;
776 vap->iv_longslotsta = 0;
778 vap->iv_flags &= ~IEEE80211_F_USEPROT;
780 * Set short preamble and ERP barker-preamble flags.
782 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
783 (vap->iv_caps & IEEE80211_C_SHPREAMBLE)) {
784 vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
785 vap->iv_flags &= ~IEEE80211_F_USEBARKER;
787 vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
788 vap->iv_flags |= IEEE80211_F_USEBARKER;
792 * Short slot time is enabled only when operating in 11g
793 * and not in an IBSS. We must also honor whether or not
794 * the driver is capable of doing it.
796 ieee80211_vap_set_shortslottime(vap,
797 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
798 IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
799 (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
800 vap->iv_opmode == IEEE80211_M_HOSTAP &&
801 (ic->ic_caps & IEEE80211_C_SHSLOT)));
805 * Reset 11g-related state.
807 * Note this resets the global state and a caller should schedule
808 * a re-check of all the VAPs after setup to update said state.
811 ieee80211_reset_erp(struct ieee80211com *ic)
814 ic->ic_flags &= ~IEEE80211_F_USEPROT;
816 * Set short preamble and ERP barker-preamble flags.
818 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
819 (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
820 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
821 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
823 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
824 ic->ic_flags |= IEEE80211_F_USEBARKER;
827 /* XXX TODO: schedule a new per-VAP ERP calculation */
830 static struct ieee80211_node *
831 vap_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
833 struct ieee80211_node *obss;
842 * Deferred slot time update.
844 * For per-VAP slot time configuration, call the VAP
845 * method if the VAP requires it. Otherwise, just call the
846 * older global method.
848 * If the per-VAP method is called then it's expected that
849 * the driver/firmware will take care of turning the per-VAP
850 * flags into slot time configuration.
852 * If the per-VAP method is not called then the global flags will be
853 * flipped into sync with the VAPs; ic_flags IEEE80211_F_SHSLOT will
854 * be set only if all of the vaps will have it set.
856 * Look at the comments for vap_update_erp_protmode() for more
857 * background; this assumes all VAPs are on the same channel.
860 vap_update_slot(void *arg, int npending)
862 struct ieee80211vap *vap = arg;
863 struct ieee80211com *ic = vap->iv_ic;
864 struct ieee80211vap *iv;
865 int num_shslot = 0, num_lgslot = 0;
868 * Per-VAP path - we've already had the flags updated;
869 * so just notify the driver and move on.
871 if (vap->iv_updateslot != NULL) {
872 vap->iv_updateslot(vap);
877 * Iterate over all of the VAP flags to update the
880 * If all vaps have short slot enabled then flip on
881 * short slot. If any vap has it disabled then
882 * we leave it globally disabled. This should provide
883 * correct behaviour in a multi-BSS scenario where
884 * at least one VAP has short slot disabled for some
888 TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
889 if (iv->iv_flags & IEEE80211_F_SHSLOT)
896 * It looks backwards but - if the number of short slot VAPs
897 * is zero then we're not short slot. Else, we have one
898 * or more short slot VAPs and we're checking to see if ANY
899 * of them have short slot disabled.
902 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
903 else if (num_lgslot == 0)
904 ic->ic_flags |= IEEE80211_F_SHSLOT;
905 IEEE80211_UNLOCK(ic);
908 * Call the driver with our new global slot time flags.
910 if (ic->ic_updateslot != NULL)
911 ic->ic_updateslot(ic);
915 * Deferred ERP protmode update.
917 * This currently calculates the global ERP protection mode flag
918 * based on each of the VAPs. Any VAP with it enabled is enough
919 * for the global flag to be enabled. All VAPs with it disabled
920 * is enough for it to be disabled.
922 * This may make sense right now for the supported hardware where
923 * net80211 is controlling the single channel configuration, but
924 * offload firmware that's doing channel changes (eg off-channel
925 * TDLS, off-channel STA, off-channel P2P STA/AP) may get some
926 * silly looking flag updates.
928 * Ideally the protection mode calculation is done based on the
929 * channel, and all VAPs using that channel will inherit it.
930 * But until that's what net80211 does, this wil have to do.
933 vap_update_erp_protmode(void *arg, int npending)
935 struct ieee80211vap *vap = arg;
936 struct ieee80211com *ic = vap->iv_ic;
937 struct ieee80211vap *iv;
938 int enable_protmode = 0;
939 int non_erp_present = 0;
942 * Iterate over all of the VAPs to calculate the overlapping
943 * ERP protection mode configuration and ERP present math.
945 * For now we assume that if a driver can handle this per-VAP
946 * then it'll ignore the ic->ic_protmode variant and instead
947 * will look at the vap related flags.
950 TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
951 if (iv->iv_flags & IEEE80211_F_USEPROT)
953 if (iv->iv_flags_ext & IEEE80211_FEXT_NONERP_PR)
958 ic->ic_flags |= IEEE80211_F_USEPROT;
960 ic->ic_flags &= ~IEEE80211_F_USEPROT;
963 ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
965 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
967 /* Beacon update on all VAPs */
968 ieee80211_notify_erp_locked(ic);
970 IEEE80211_UNLOCK(ic);
972 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
973 "%s: called; enable_protmode=%d, non_erp_present=%d\n",
974 __func__, enable_protmode, non_erp_present);
977 * Now that the global configuration flags are calculated,
978 * notify the VAP about its configuration.
980 * The global flags will be used when assembling ERP IEs
981 * for multi-VAP operation, even if it's on a different
982 * channel. Yes, that's going to need fixing in the
985 if (vap->iv_erp_protmode_update != NULL)
986 vap->iv_erp_protmode_update(vap);
990 * Deferred ERP short preamble/barker update.
992 * All VAPs need to use short preamble for it to be globally
995 * Look at the comments for vap_update_erp_protmode() for more
996 * background; this assumes all VAPs are on the same channel.
999 vap_update_preamble(void *arg, int npending)
1001 struct ieee80211vap *vap = arg;
1002 struct ieee80211com *ic = vap->iv_ic;
1003 struct ieee80211vap *iv;
1004 int barker_count = 0, short_preamble_count = 0, count = 0;
1007 * Iterate over all of the VAPs to calculate the overlapping
1008 * short or long preamble configuration.
1010 * For now we assume that if a driver can handle this per-VAP
1011 * then it'll ignore the ic->ic_flags variant and instead
1012 * will look at the vap related flags.
1015 TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
1016 if (iv->iv_flags & IEEE80211_F_USEBARKER)
1018 if (iv->iv_flags & IEEE80211_F_SHPREAMBLE)
1019 short_preamble_count++;
1024 * As with vap_update_erp_protmode(), the global flags are
1025 * currently used for beacon IEs.
1027 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1028 "%s: called; barker_count=%d, short_preamble_count=%d\n",
1029 __func__, barker_count, short_preamble_count);
1032 * Only flip on short preamble if all of the VAPs support
1035 if (barker_count == 0 && short_preamble_count == count) {
1036 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1037 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1039 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1040 ic->ic_flags |= IEEE80211_F_USEBARKER;
1042 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1043 "%s: global barker=%d preamble=%d\n",
1045 !! (ic->ic_flags & IEEE80211_F_USEBARKER),
1046 !! (ic->ic_flags & IEEE80211_F_SHPREAMBLE));
1048 /* Beacon update on all VAPs */
1049 ieee80211_notify_erp_locked(ic);
1051 IEEE80211_UNLOCK(ic);
1053 /* Driver notification */
1054 if (vap->iv_erp_protmode_update != NULL)
1055 vap->iv_preamble_update(vap);
1059 * Deferred HT protmode update and beacon update.
1061 * Look at the comments for vap_update_erp_protmode() for more
1062 * background; this assumes all VAPs are on the same channel.
1065 vap_update_ht_protmode(void *arg, int npending)
1067 struct ieee80211vap *vap = arg;
1068 struct ieee80211vap *iv;
1069 struct ieee80211com *ic = vap->iv_ic;
1070 int num_vaps = 0, num_pure = 0;
1071 int num_optional = 0, num_ht2040 = 0, num_nonht = 0;
1072 int num_ht_sta = 0, num_ht40_sta = 0, num_sta = 0;
1073 int num_nonhtpr = 0;
1076 * Iterate over all of the VAPs to calculate everything.
1078 * There are a few different flags to calculate:
1080 * + whether there's HT only or HT+legacy stations;
1081 * + whether there's HT20, HT40, or HT20+HT40 stations;
1082 * + whether the desired protection mode is mixed, pure or
1083 * one of the two above.
1085 * For now we assume that if a driver can handle this per-VAP
1086 * then it'll ignore the ic->ic_htprotmode / ic->ic_curhtprotmode
1087 * variant and instead will look at the vap related variables.
1089 * XXX TODO: non-greenfield STAs present (IEEE80211_HTINFO_NONGF_PRESENT) !
1093 TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
1095 /* overlapping BSSes advertising non-HT status present */
1096 if (iv->iv_flags_ht & IEEE80211_FHT_NONHT_PR)
1098 /* Operating mode flags */
1099 if (iv->iv_curhtprotmode & IEEE80211_HTINFO_NONHT_PRESENT)
1101 switch (iv->iv_curhtprotmode & IEEE80211_HTINFO_OPMODE) {
1102 case IEEE80211_HTINFO_OPMODE_PURE:
1105 case IEEE80211_HTINFO_OPMODE_PROTOPT:
1108 case IEEE80211_HTINFO_OPMODE_HT20PR:
1113 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
1114 "%s: vap %s: nonht_pr=%d, curhtprotmode=0x%02x\n",
1116 ieee80211_get_vap_ifname(iv),
1117 !! (iv->iv_flags_ht & IEEE80211_FHT_NONHT_PR),
1118 iv->iv_curhtprotmode);
1120 num_ht_sta += iv->iv_ht_sta_assoc;
1121 num_ht40_sta += iv->iv_ht40_sta_assoc;
1122 num_sta += iv->iv_sta_assoc;
1126 * Step 1 - if any VAPs indicate NONHT_PR set (overlapping BSS
1127 * non-HT present), set it here. This shouldn't be used by
1128 * anything but the old overlapping BSS logic so if any drivers
1129 * consume it, it's up to date.
1132 ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1134 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1137 * Step 2 - default HT protection mode to MIXED (802.11-2016 10.26.3.1.)
1139 * + If all VAPs are PURE, we can stay PURE.
1140 * + If all VAPs are PROTOPT, we can go to PROTOPT.
1141 * + If any VAP has HT20PR then it sees at least a HT40+HT20 station.
1142 * Note that we may have a VAP with one HT20 and a VAP with one HT40;
1143 * So we look at the sum ht and sum ht40 sta counts; if we have a
1144 * HT station and the HT20 != HT40 count, we have to do HT20PR here.
1145 * Note all stations need to be HT for this to be an option.
1146 * + The fall-through is MIXED, because it means we have some odd
1147 * non HT40-involved combination of opmode and this is the most
1150 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_MIXED;
1152 if (num_pure == num_vaps)
1153 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
1155 if (num_optional == num_vaps)
1156 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PROTOPT;
1159 * Note: we need /a/ HT40 station somewhere for this to
1162 if ((num_ht2040 > 0) ||
1163 ((num_ht_sta > 0) && (num_ht40_sta > 0) &&
1164 (num_ht_sta != num_ht40_sta)))
1165 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1168 * Step 3 - if any of the stations across the VAPs are
1169 * non-HT then this needs to be flipped back to MIXED.
1171 if (num_ht_sta != num_sta)
1172 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_MIXED;
1175 * Step 4 - If we see any overlapping BSS non-HT stations
1176 * via beacons then flip on NONHT_PRESENT.
1178 if (num_nonhtpr > 0)
1179 ic->ic_curhtprotmode |= IEEE80211_HTINFO_NONHT_PRESENT;
1181 /* Notify all VAPs to potentially update their beacons */
1182 TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next)
1183 ieee80211_htinfo_notify(iv);
1185 IEEE80211_UNLOCK(ic);
1187 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
1188 "%s: global: nonht_pr=%d ht_opmode=0x%02x\n",
1190 !! (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR),
1191 ic->ic_curhtprotmode);
1194 if (vap->iv_erp_protmode_update != NULL)
1195 vap->iv_ht_protmode_update(vap);
1199 * Set the short slot time state and notify the driver.
1201 * This is the per-VAP slot time state.
1204 ieee80211_vap_set_shortslottime(struct ieee80211vap *vap, int onoff)
1206 struct ieee80211com *ic = vap->iv_ic;
1211 * Only modify the per-VAP slot time.
1214 vap->iv_flags |= IEEE80211_F_SHSLOT;
1216 vap->iv_flags &= ~IEEE80211_F_SHSLOT;
1218 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1219 "%s: called; onoff=%d\n", __func__, onoff);
1220 /* schedule the deferred slot flag update and update */
1221 ieee80211_runtask(ic, &vap->iv_slot_task);
1225 * Update the VAP short /long / barker preamble state and
1226 * update beacon state if needed.
1228 * For now it simply copies the global flags into the per-vap
1229 * flags and schedules the callback. Later this will support
1230 * both global and per-VAP flags, especially useful for
1231 * and STA+STA multi-channel operation (eg p2p).
1234 ieee80211_vap_update_preamble(struct ieee80211vap *vap)
1236 struct ieee80211com *ic = vap->iv_ic;
1240 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1241 "%s: called\n", __func__);
1242 /* schedule the deferred slot flag update and update */
1243 ieee80211_runtask(ic, &vap->iv_preamble_task);
1247 * Update the VAP 11g protection mode and update beacon state
1251 ieee80211_vap_update_erp_protmode(struct ieee80211vap *vap)
1253 struct ieee80211com *ic = vap->iv_ic;
1257 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1258 "%s: called\n", __func__);
1259 /* schedule the deferred slot flag update and update */
1260 ieee80211_runtask(ic, &vap->iv_erp_protmode_task);
1264 * Update the VAP 11n protection mode and update beacon state
1268 ieee80211_vap_update_ht_protmode(struct ieee80211vap *vap)
1270 struct ieee80211com *ic = vap->iv_ic;
1274 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1275 "%s: called\n", __func__);
1276 /* schedule the deferred protmode update */
1277 ieee80211_runtask(ic, &vap->iv_ht_protmode_task);
1281 * Check if the specified rate set supports ERP.
1282 * NB: the rate set is assumed to be sorted.
1285 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
1287 static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
1290 if (rs->rs_nrates < nitems(rates))
1292 for (i = 0; i < nitems(rates); i++) {
1293 for (j = 0; j < rs->rs_nrates; j++) {
1294 int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
1308 * Mark the basic rates for the rate table based on the
1309 * operating mode. For real 11g we mark all the 11b rates
1310 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
1311 * 11b rates. There's also a pseudo 11a-mode used to mark only
1312 * the basic OFDM rates.
1315 setbasicrates(struct ieee80211_rateset *rs,
1316 enum ieee80211_phymode mode, int add)
1318 static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
1319 [IEEE80211_MODE_11A] = { 3, { 12, 24, 48 } },
1320 [IEEE80211_MODE_11B] = { 2, { 2, 4 } },
1322 [IEEE80211_MODE_11G] = { 4, { 2, 4, 11, 22 } },
1323 [IEEE80211_MODE_TURBO_A] = { 3, { 12, 24, 48 } },
1324 [IEEE80211_MODE_TURBO_G] = { 4, { 2, 4, 11, 22 } },
1325 [IEEE80211_MODE_STURBO_A] = { 3, { 12, 24, 48 } },
1326 [IEEE80211_MODE_HALF] = { 3, { 6, 12, 24 } },
1327 [IEEE80211_MODE_QUARTER] = { 3, { 3, 6, 12 } },
1328 [IEEE80211_MODE_11NA] = { 3, { 12, 24, 48 } },
1330 [IEEE80211_MODE_11NG] = { 4, { 2, 4, 11, 22 } },
1332 [IEEE80211_MODE_VHT_2GHZ] = { 4, { 2, 4, 11, 22 } },
1333 [IEEE80211_MODE_VHT_5GHZ] = { 3, { 12, 24, 48 } },
1337 for (i = 0; i < rs->rs_nrates; i++) {
1339 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
1340 for (j = 0; j < basic[mode].rs_nrates; j++)
1341 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
1342 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
1349 * Set the basic rates in a rate set.
1352 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
1353 enum ieee80211_phymode mode)
1355 setbasicrates(rs, mode, 0);
1359 * Add basic rates to a rate set.
1362 ieee80211_addbasicrates(struct ieee80211_rateset *rs,
1363 enum ieee80211_phymode mode)
1365 setbasicrates(rs, mode, 1);
1369 * WME protocol support.
1371 * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
1372 * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
1373 * Draft 2.0 Test Plan (Appendix D).
1375 * Static/Dynamic Turbo mode settings come from Atheros.
1377 typedef struct phyParamType {
1385 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
1386 [IEEE80211_MODE_AUTO] = { 3, 4, 6, 0, 0 },
1387 [IEEE80211_MODE_11A] = { 3, 4, 6, 0, 0 },
1388 [IEEE80211_MODE_11B] = { 3, 4, 6, 0, 0 },
1389 [IEEE80211_MODE_11G] = { 3, 4, 6, 0, 0 },
1390 [IEEE80211_MODE_FH] = { 3, 4, 6, 0, 0 },
1391 [IEEE80211_MODE_TURBO_A]= { 2, 3, 5, 0, 0 },
1392 [IEEE80211_MODE_TURBO_G]= { 2, 3, 5, 0, 0 },
1393 [IEEE80211_MODE_STURBO_A]={ 2, 3, 5, 0, 0 },
1394 [IEEE80211_MODE_HALF] = { 3, 4, 6, 0, 0 },
1395 [IEEE80211_MODE_QUARTER]= { 3, 4, 6, 0, 0 },
1396 [IEEE80211_MODE_11NA] = { 3, 4, 6, 0, 0 },
1397 [IEEE80211_MODE_11NG] = { 3, 4, 6, 0, 0 },
1398 [IEEE80211_MODE_VHT_2GHZ] = { 3, 4, 6, 0, 0 },
1399 [IEEE80211_MODE_VHT_5GHZ] = { 3, 4, 6, 0, 0 },
1401 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
1402 [IEEE80211_MODE_AUTO] = { 7, 4, 10, 0, 0 },
1403 [IEEE80211_MODE_11A] = { 7, 4, 10, 0, 0 },
1404 [IEEE80211_MODE_11B] = { 7, 4, 10, 0, 0 },
1405 [IEEE80211_MODE_11G] = { 7, 4, 10, 0, 0 },
1406 [IEEE80211_MODE_FH] = { 7, 4, 10, 0, 0 },
1407 [IEEE80211_MODE_TURBO_A]= { 7, 3, 10, 0, 0 },
1408 [IEEE80211_MODE_TURBO_G]= { 7, 3, 10, 0, 0 },
1409 [IEEE80211_MODE_STURBO_A]={ 7, 3, 10, 0, 0 },
1410 [IEEE80211_MODE_HALF] = { 7, 4, 10, 0, 0 },
1411 [IEEE80211_MODE_QUARTER]= { 7, 4, 10, 0, 0 },
1412 [IEEE80211_MODE_11NA] = { 7, 4, 10, 0, 0 },
1413 [IEEE80211_MODE_11NG] = { 7, 4, 10, 0, 0 },
1414 [IEEE80211_MODE_VHT_2GHZ] = { 7, 4, 10, 0, 0 },
1415 [IEEE80211_MODE_VHT_5GHZ] = { 7, 4, 10, 0, 0 },
1417 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
1418 [IEEE80211_MODE_AUTO] = { 1, 3, 4, 94, 0 },
1419 [IEEE80211_MODE_11A] = { 1, 3, 4, 94, 0 },
1420 [IEEE80211_MODE_11B] = { 1, 3, 4, 188, 0 },
1421 [IEEE80211_MODE_11G] = { 1, 3, 4, 94, 0 },
1422 [IEEE80211_MODE_FH] = { 1, 3, 4, 188, 0 },
1423 [IEEE80211_MODE_TURBO_A]= { 1, 2, 3, 94, 0 },
1424 [IEEE80211_MODE_TURBO_G]= { 1, 2, 3, 94, 0 },
1425 [IEEE80211_MODE_STURBO_A]={ 1, 2, 3, 94, 0 },
1426 [IEEE80211_MODE_HALF] = { 1, 3, 4, 94, 0 },
1427 [IEEE80211_MODE_QUARTER]= { 1, 3, 4, 94, 0 },
1428 [IEEE80211_MODE_11NA] = { 1, 3, 4, 94, 0 },
1429 [IEEE80211_MODE_11NG] = { 1, 3, 4, 94, 0 },
1430 [IEEE80211_MODE_VHT_2GHZ] = { 1, 3, 4, 94, 0 },
1431 [IEEE80211_MODE_VHT_5GHZ] = { 1, 3, 4, 94, 0 },
1433 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
1434 [IEEE80211_MODE_AUTO] = { 1, 2, 3, 47, 0 },
1435 [IEEE80211_MODE_11A] = { 1, 2, 3, 47, 0 },
1436 [IEEE80211_MODE_11B] = { 1, 2, 3, 102, 0 },
1437 [IEEE80211_MODE_11G] = { 1, 2, 3, 47, 0 },
1438 [IEEE80211_MODE_FH] = { 1, 2, 3, 102, 0 },
1439 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
1440 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
1441 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
1442 [IEEE80211_MODE_HALF] = { 1, 2, 3, 47, 0 },
1443 [IEEE80211_MODE_QUARTER]= { 1, 2, 3, 47, 0 },
1444 [IEEE80211_MODE_11NA] = { 1, 2, 3, 47, 0 },
1445 [IEEE80211_MODE_11NG] = { 1, 2, 3, 47, 0 },
1446 [IEEE80211_MODE_VHT_2GHZ] = { 1, 2, 3, 47, 0 },
1447 [IEEE80211_MODE_VHT_5GHZ] = { 1, 2, 3, 47, 0 },
1450 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
1451 [IEEE80211_MODE_AUTO] = { 3, 4, 10, 0, 0 },
1452 [IEEE80211_MODE_11A] = { 3, 4, 10, 0, 0 },
1453 [IEEE80211_MODE_11B] = { 3, 4, 10, 0, 0 },
1454 [IEEE80211_MODE_11G] = { 3, 4, 10, 0, 0 },
1455 [IEEE80211_MODE_FH] = { 3, 4, 10, 0, 0 },
1456 [IEEE80211_MODE_TURBO_A]= { 2, 3, 10, 0, 0 },
1457 [IEEE80211_MODE_TURBO_G]= { 2, 3, 10, 0, 0 },
1458 [IEEE80211_MODE_STURBO_A]={ 2, 3, 10, 0, 0 },
1459 [IEEE80211_MODE_HALF] = { 3, 4, 10, 0, 0 },
1460 [IEEE80211_MODE_QUARTER]= { 3, 4, 10, 0, 0 },
1461 [IEEE80211_MODE_11NA] = { 3, 4, 10, 0, 0 },
1462 [IEEE80211_MODE_11NG] = { 3, 4, 10, 0, 0 },
1464 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
1465 [IEEE80211_MODE_AUTO] = { 2, 3, 4, 94, 0 },
1466 [IEEE80211_MODE_11A] = { 2, 3, 4, 94, 0 },
1467 [IEEE80211_MODE_11B] = { 2, 3, 4, 188, 0 },
1468 [IEEE80211_MODE_11G] = { 2, 3, 4, 94, 0 },
1469 [IEEE80211_MODE_FH] = { 2, 3, 4, 188, 0 },
1470 [IEEE80211_MODE_TURBO_A]= { 2, 2, 3, 94, 0 },
1471 [IEEE80211_MODE_TURBO_G]= { 2, 2, 3, 94, 0 },
1472 [IEEE80211_MODE_STURBO_A]={ 2, 2, 3, 94, 0 },
1473 [IEEE80211_MODE_HALF] = { 2, 3, 4, 94, 0 },
1474 [IEEE80211_MODE_QUARTER]= { 2, 3, 4, 94, 0 },
1475 [IEEE80211_MODE_11NA] = { 2, 3, 4, 94, 0 },
1476 [IEEE80211_MODE_11NG] = { 2, 3, 4, 94, 0 },
1478 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
1479 [IEEE80211_MODE_AUTO] = { 2, 2, 3, 47, 0 },
1480 [IEEE80211_MODE_11A] = { 2, 2, 3, 47, 0 },
1481 [IEEE80211_MODE_11B] = { 2, 2, 3, 102, 0 },
1482 [IEEE80211_MODE_11G] = { 2, 2, 3, 47, 0 },
1483 [IEEE80211_MODE_FH] = { 2, 2, 3, 102, 0 },
1484 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
1485 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
1486 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
1487 [IEEE80211_MODE_HALF] = { 2, 2, 3, 47, 0 },
1488 [IEEE80211_MODE_QUARTER]= { 2, 2, 3, 47, 0 },
1489 [IEEE80211_MODE_11NA] = { 2, 2, 3, 47, 0 },
1490 [IEEE80211_MODE_11NG] = { 2, 2, 3, 47, 0 },
1494 _setifsparams(struct wmeParams *wmep, const paramType *phy)
1496 wmep->wmep_aifsn = phy->aifsn;
1497 wmep->wmep_logcwmin = phy->logcwmin;
1498 wmep->wmep_logcwmax = phy->logcwmax;
1499 wmep->wmep_txopLimit = phy->txopLimit;
1503 setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
1504 struct wmeParams *wmep, const paramType *phy)
1506 wmep->wmep_acm = phy->acm;
1507 _setifsparams(wmep, phy);
1509 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1510 "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
1511 ieee80211_wme_acnames[ac], type,
1512 wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
1513 wmep->wmep_logcwmax, wmep->wmep_txopLimit);
1517 ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
1519 struct ieee80211com *ic = vap->iv_ic;
1520 struct ieee80211_wme_state *wme = &ic->ic_wme;
1521 const paramType *pPhyParam, *pBssPhyParam;
1522 struct wmeParams *wmep;
1523 enum ieee80211_phymode mode;
1526 IEEE80211_LOCK_ASSERT(ic);
1528 if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
1532 * Clear the wme cap_info field so a qoscount from a previous
1533 * vap doesn't confuse later code which only parses the beacon
1534 * field and updates hardware when said field changes.
1535 * Otherwise the hardware is programmed with defaults, not what
1536 * the beacon actually announces.
1538 * Note that we can't ever have 0xff as an actual value;
1539 * the only valid values are 0..15.
1541 wme->wme_wmeChanParams.cap_info = 0xfe;
1544 * Select mode; we can be called early in which case we
1545 * always use auto mode. We know we'll be called when
1546 * entering the RUN state with bsschan setup properly
1547 * so state will eventually get set correctly
1549 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1550 mode = ieee80211_chan2mode(ic->ic_bsschan);
1552 mode = IEEE80211_MODE_AUTO;
1553 for (i = 0; i < WME_NUM_AC; i++) {
1556 pPhyParam = &phyParamForAC_BK[mode];
1557 pBssPhyParam = &phyParamForAC_BK[mode];
1560 pPhyParam = &phyParamForAC_VI[mode];
1561 pBssPhyParam = &bssPhyParamForAC_VI[mode];
1564 pPhyParam = &phyParamForAC_VO[mode];
1565 pBssPhyParam = &bssPhyParamForAC_VO[mode];
1569 pPhyParam = &phyParamForAC_BE[mode];
1570 pBssPhyParam = &bssPhyParamForAC_BE[mode];
1573 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1574 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1575 setwmeparams(vap, "chan", i, wmep, pPhyParam);
1577 setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
1579 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1580 setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
1582 /* NB: check ic_bss to avoid NULL deref on initial attach */
1583 if (vap->iv_bss != NULL) {
1585 * Calculate aggressive mode switching threshold based
1586 * on beacon interval. This doesn't need locking since
1587 * we're only called before entering the RUN state at
1588 * which point we start sending beacon frames.
1590 wme->wme_hipri_switch_thresh =
1591 (HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
1592 wme->wme_flags &= ~WME_F_AGGRMODE;
1593 ieee80211_wme_updateparams(vap);
1598 ieee80211_wme_initparams(struct ieee80211vap *vap)
1600 struct ieee80211com *ic = vap->iv_ic;
1603 ieee80211_wme_initparams_locked(vap);
1604 IEEE80211_UNLOCK(ic);
1608 * Update WME parameters for ourself and the BSS.
1611 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
1613 static const paramType aggrParam[IEEE80211_MODE_MAX] = {
1614 [IEEE80211_MODE_AUTO] = { 2, 4, 10, 64, 0 },
1615 [IEEE80211_MODE_11A] = { 2, 4, 10, 64, 0 },
1616 [IEEE80211_MODE_11B] = { 2, 5, 10, 64, 0 },
1617 [IEEE80211_MODE_11G] = { 2, 4, 10, 64, 0 },
1618 [IEEE80211_MODE_FH] = { 2, 5, 10, 64, 0 },
1619 [IEEE80211_MODE_TURBO_A] = { 1, 3, 10, 64, 0 },
1620 [IEEE80211_MODE_TURBO_G] = { 1, 3, 10, 64, 0 },
1621 [IEEE80211_MODE_STURBO_A] = { 1, 3, 10, 64, 0 },
1622 [IEEE80211_MODE_HALF] = { 2, 4, 10, 64, 0 },
1623 [IEEE80211_MODE_QUARTER] = { 2, 4, 10, 64, 0 },
1624 [IEEE80211_MODE_11NA] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1625 [IEEE80211_MODE_11NG] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1626 [IEEE80211_MODE_VHT_2GHZ] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1627 [IEEE80211_MODE_VHT_5GHZ] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1629 struct ieee80211com *ic = vap->iv_ic;
1630 struct ieee80211_wme_state *wme = &ic->ic_wme;
1631 const struct wmeParams *wmep;
1632 struct wmeParams *chanp, *bssp;
1633 enum ieee80211_phymode mode;
1635 int do_aggrmode = 0;
1638 * Set up the channel access parameters for the physical
1639 * device. First populate the configured settings.
1641 for (i = 0; i < WME_NUM_AC; i++) {
1642 chanp = &wme->wme_chanParams.cap_wmeParams[i];
1643 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1644 chanp->wmep_aifsn = wmep->wmep_aifsn;
1645 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1646 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1647 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1649 chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
1650 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1651 chanp->wmep_aifsn = wmep->wmep_aifsn;
1652 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1653 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1654 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1658 * Select mode; we can be called early in which case we
1659 * always use auto mode. We know we'll be called when
1660 * entering the RUN state with bsschan setup properly
1661 * so state will eventually get set correctly
1663 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1664 mode = ieee80211_chan2mode(ic->ic_bsschan);
1666 mode = IEEE80211_MODE_AUTO;
1669 * This implements aggressive mode as found in certain
1670 * vendors' AP's. When there is significant high
1671 * priority (VI/VO) traffic in the BSS throttle back BE
1672 * traffic by using conservative parameters. Otherwise
1673 * BE uses aggressive params to optimize performance of
1674 * legacy/non-QoS traffic.
1677 /* Hostap? Only if aggressive mode is enabled */
1678 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1679 (wme->wme_flags & WME_F_AGGRMODE) != 0)
1683 * Station? Only if we're in a non-QoS BSS.
1685 else if ((vap->iv_opmode == IEEE80211_M_STA &&
1686 (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1690 * IBSS? Only if we we have WME enabled.
1692 else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1693 (vap->iv_flags & IEEE80211_F_WME))
1697 * If WME is disabled on this VAP, default to aggressive mode
1698 * regardless of the configuration.
1700 if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1708 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1709 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1711 chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1712 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1713 aggrParam[mode].logcwmin;
1714 chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1715 aggrParam[mode].logcwmax;
1716 chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1717 (vap->iv_flags & IEEE80211_F_BURST) ?
1718 aggrParam[mode].txopLimit : 0;
1719 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1720 "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1721 "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1722 chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1723 chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1727 * Change the contention window based on the number of associated
1728 * stations. If the number of associated stations is 1 and
1729 * aggressive mode is enabled, lower the contention window even
1732 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1733 vap->iv_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1734 static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1735 [IEEE80211_MODE_AUTO] = 3,
1736 [IEEE80211_MODE_11A] = 3,
1737 [IEEE80211_MODE_11B] = 4,
1738 [IEEE80211_MODE_11G] = 3,
1739 [IEEE80211_MODE_FH] = 4,
1740 [IEEE80211_MODE_TURBO_A] = 3,
1741 [IEEE80211_MODE_TURBO_G] = 3,
1742 [IEEE80211_MODE_STURBO_A] = 3,
1743 [IEEE80211_MODE_HALF] = 3,
1744 [IEEE80211_MODE_QUARTER] = 3,
1745 [IEEE80211_MODE_11NA] = 3,
1746 [IEEE80211_MODE_11NG] = 3,
1747 [IEEE80211_MODE_VHT_2GHZ] = 3,
1748 [IEEE80211_MODE_VHT_5GHZ] = 3,
1750 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1751 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1753 chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1754 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1755 "update %s (chan+bss) logcwmin %u\n",
1756 ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1759 /* schedule the deferred WME update */
1760 ieee80211_runtask(ic, &vap->iv_wme_task);
1762 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1763 "%s: WME params updated, cap_info 0x%x\n", __func__,
1764 vap->iv_opmode == IEEE80211_M_STA ?
1765 wme->wme_wmeChanParams.cap_info :
1766 wme->wme_bssChanParams.cap_info);
1770 ieee80211_wme_updateparams(struct ieee80211vap *vap)
1772 struct ieee80211com *ic = vap->iv_ic;
1774 if (ic->ic_caps & IEEE80211_C_WME) {
1776 ieee80211_wme_updateparams_locked(vap);
1777 IEEE80211_UNLOCK(ic);
1782 * Fetch the WME parameters for the given VAP.
1784 * When net80211 grows p2p, etc support, this may return different
1785 * parameters for each VAP.
1788 ieee80211_wme_vap_getparams(struct ieee80211vap *vap, struct chanAccParams *wp)
1791 memcpy(wp, &vap->iv_ic->ic_wme.wme_chanParams, sizeof(*wp));
1795 * For NICs which only support one set of WME parameters (ie, softmac NICs)
1796 * there may be different VAP WME parameters but only one is "active".
1797 * This returns the "NIC" WME parameters for the currently active
1801 ieee80211_wme_ic_getparams(struct ieee80211com *ic, struct chanAccParams *wp)
1804 memcpy(wp, &ic->ic_wme.wme_chanParams, sizeof(*wp));
1808 * Return whether to use QoS on a given WME queue.
1810 * This is intended to be called from the transmit path of softmac drivers
1811 * which are setting NoAck bits in transmit descriptors.
1813 * Ideally this would be set in some transmit field before the packet is
1814 * queued to the driver but net80211 isn't quite there yet.
1817 ieee80211_wme_vap_ac_is_noack(struct ieee80211vap *vap, int ac)
1819 /* Bounds/sanity check */
1820 if (ac < 0 || ac >= WME_NUM_AC)
1823 /* Again, there's only one global context for now */
1824 return (!! vap->iv_ic->ic_wme.wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy);
1828 parent_updown(void *arg, int npending)
1830 struct ieee80211com *ic = arg;
1836 update_mcast(void *arg, int npending)
1838 struct ieee80211com *ic = arg;
1840 ic->ic_update_mcast(ic);
1844 update_promisc(void *arg, int npending)
1846 struct ieee80211com *ic = arg;
1848 ic->ic_update_promisc(ic);
1852 update_channel(void *arg, int npending)
1854 struct ieee80211com *ic = arg;
1856 ic->ic_set_channel(ic);
1857 ieee80211_radiotap_chan_change(ic);
1861 update_chw(void *arg, int npending)
1863 struct ieee80211com *ic = arg;
1866 * XXX should we defer the channel width _config_ update until now?
1868 ic->ic_update_chw(ic);
1872 * Deferred WME parameter and beacon update.
1874 * In preparation for per-VAP WME configuration, call the VAP
1875 * method if the VAP requires it. Otherwise, just call the
1876 * older global method. There isn't a per-VAP WME configuration
1877 * just yet so for now just use the global configuration.
1880 vap_update_wme(void *arg, int npending)
1882 struct ieee80211vap *vap = arg;
1883 struct ieee80211com *ic = vap->iv_ic;
1884 struct ieee80211_wme_state *wme = &ic->ic_wme;
1887 if (vap->iv_wme_update != NULL)
1888 vap->iv_wme_update(vap,
1889 ic->ic_wme.wme_chanParams.cap_wmeParams);
1891 ic->ic_wme.wme_update(ic);
1895 * Arrange for the beacon update.
1897 * XXX what about MBSS, WDS?
1899 if (vap->iv_opmode == IEEE80211_M_HOSTAP
1900 || vap->iv_opmode == IEEE80211_M_IBSS) {
1902 * Arrange for a beacon update and bump the parameter
1903 * set number so associated stations load the new values.
1905 wme->wme_bssChanParams.cap_info =
1906 (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1907 ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1909 IEEE80211_UNLOCK(ic);
1913 restart_vaps(void *arg, int npending)
1915 struct ieee80211com *ic = arg;
1917 ieee80211_suspend_all(ic);
1918 ieee80211_resume_all(ic);
1922 * Block until the parent is in a known state. This is
1923 * used after any operations that dispatch a task (e.g.
1924 * to auto-configure the parent device up/down).
1927 ieee80211_waitfor_parent(struct ieee80211com *ic)
1929 taskqueue_block(ic->ic_tq);
1930 ieee80211_draintask(ic, &ic->ic_parent_task);
1931 ieee80211_draintask(ic, &ic->ic_mcast_task);
1932 ieee80211_draintask(ic, &ic->ic_promisc_task);
1933 ieee80211_draintask(ic, &ic->ic_chan_task);
1934 ieee80211_draintask(ic, &ic->ic_bmiss_task);
1935 ieee80211_draintask(ic, &ic->ic_chw_task);
1936 taskqueue_unblock(ic->ic_tq);
1940 * Check to see whether the current channel needs reset.
1942 * Some devices don't handle being given an invalid channel
1943 * in their operating mode very well (eg wpi(4) will throw a
1944 * firmware exception.)
1946 * Return 0 if we're ok, 1 if the channel needs to be reset.
1948 * See PR kern/202502.
1951 ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
1953 struct ieee80211com *ic = vap->iv_ic;
1955 if ((vap->iv_opmode == IEEE80211_M_IBSS &&
1956 IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
1957 (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1958 IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
1964 * Reset the curchan to a known good state.
1967 ieee80211_start_reset_chan(struct ieee80211vap *vap)
1969 struct ieee80211com *ic = vap->iv_ic;
1971 ic->ic_curchan = &ic->ic_channels[0];
1975 * Start a vap running. If this is the first vap to be
1976 * set running on the underlying device then we
1977 * automatically bring the device up.
1980 ieee80211_start_locked(struct ieee80211vap *vap)
1982 struct ifnet *ifp = vap->iv_ifp;
1983 struct ieee80211com *ic = vap->iv_ic;
1985 IEEE80211_LOCK_ASSERT(ic);
1987 IEEE80211_DPRINTF(vap,
1988 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1989 "start running, %d vaps running\n", ic->ic_nrunning);
1991 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1993 * Mark us running. Note that it's ok to do this first;
1994 * if we need to bring the parent device up we defer that
1995 * to avoid dropping the com lock. We expect the device
1996 * to respond to being marked up by calling back into us
1997 * through ieee80211_start_all at which point we'll come
1998 * back in here and complete the work.
2000 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2001 ieee80211_notify_ifnet_change(vap);
2004 * We are not running; if this we are the first vap
2005 * to be brought up auto-up the parent if necessary.
2007 if (ic->ic_nrunning++ == 0) {
2008 /* reset the channel to a known good channel */
2009 if (ieee80211_start_check_reset_chan(vap))
2010 ieee80211_start_reset_chan(vap);
2012 IEEE80211_DPRINTF(vap,
2013 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2014 "%s: up parent %s\n", __func__, ic->ic_name);
2015 ieee80211_runtask(ic, &ic->ic_parent_task);
2020 * If the parent is up and running, then kick the
2021 * 802.11 state machine as appropriate.
2023 if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
2024 if (vap->iv_opmode == IEEE80211_M_STA) {
2026 /* XXX bypasses scan too easily; disable for now */
2028 * Try to be intelligent about clocking the state
2029 * machine. If we're currently in RUN state then
2030 * we should be able to apply any new state/parameters
2031 * simply by re-associating. Otherwise we need to
2032 * re-scan to select an appropriate ap.
2034 if (vap->iv_state >= IEEE80211_S_RUN)
2035 ieee80211_new_state_locked(vap,
2036 IEEE80211_S_ASSOC, 1);
2039 ieee80211_new_state_locked(vap,
2040 IEEE80211_S_SCAN, 0);
2043 * For monitor+wds mode there's nothing to do but
2044 * start running. Otherwise if this is the first
2045 * vap to be brought up, start a scan which may be
2046 * preempted if the station is locked to a particular
2049 vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
2050 if (vap->iv_opmode == IEEE80211_M_MONITOR ||
2051 vap->iv_opmode == IEEE80211_M_WDS)
2052 ieee80211_new_state_locked(vap,
2053 IEEE80211_S_RUN, -1);
2055 ieee80211_new_state_locked(vap,
2056 IEEE80211_S_SCAN, 0);
2062 * Start a single vap.
2065 ieee80211_init(void *arg)
2067 struct ieee80211vap *vap = arg;
2069 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2072 IEEE80211_LOCK(vap->iv_ic);
2073 ieee80211_start_locked(vap);
2074 IEEE80211_UNLOCK(vap->iv_ic);
2078 * Start all runnable vap's on a device.
2081 ieee80211_start_all(struct ieee80211com *ic)
2083 struct ieee80211vap *vap;
2086 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2087 struct ifnet *ifp = vap->iv_ifp;
2088 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
2089 ieee80211_start_locked(vap);
2091 IEEE80211_UNLOCK(ic);
2095 * Stop a vap. We force it down using the state machine
2096 * then mark it's ifnet not running. If this is the last
2097 * vap running on the underlying device then we close it
2098 * too to insure it will be properly initialized when the
2099 * next vap is brought up.
2102 ieee80211_stop_locked(struct ieee80211vap *vap)
2104 struct ieee80211com *ic = vap->iv_ic;
2105 struct ifnet *ifp = vap->iv_ifp;
2107 IEEE80211_LOCK_ASSERT(ic);
2109 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2110 "stop running, %d vaps running\n", ic->ic_nrunning);
2112 ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
2113 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2114 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; /* mark us stopped */
2115 ieee80211_notify_ifnet_change(vap);
2116 if (--ic->ic_nrunning == 0) {
2117 IEEE80211_DPRINTF(vap,
2118 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2119 "down parent %s\n", ic->ic_name);
2120 ieee80211_runtask(ic, &ic->ic_parent_task);
2126 ieee80211_stop(struct ieee80211vap *vap)
2128 struct ieee80211com *ic = vap->iv_ic;
2131 ieee80211_stop_locked(vap);
2132 IEEE80211_UNLOCK(ic);
2136 * Stop all vap's running on a device.
2139 ieee80211_stop_all(struct ieee80211com *ic)
2141 struct ieee80211vap *vap;
2144 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2145 struct ifnet *ifp = vap->iv_ifp;
2146 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
2147 ieee80211_stop_locked(vap);
2149 IEEE80211_UNLOCK(ic);
2151 ieee80211_waitfor_parent(ic);
2155 * Stop all vap's running on a device and arrange
2156 * for those that were running to be resumed.
2159 ieee80211_suspend_all(struct ieee80211com *ic)
2161 struct ieee80211vap *vap;
2164 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2165 struct ifnet *ifp = vap->iv_ifp;
2166 if (IFNET_IS_UP_RUNNING(ifp)) { /* NB: avoid recursion */
2167 vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
2168 ieee80211_stop_locked(vap);
2171 IEEE80211_UNLOCK(ic);
2173 ieee80211_waitfor_parent(ic);
2177 * Start all vap's marked for resume.
2180 ieee80211_resume_all(struct ieee80211com *ic)
2182 struct ieee80211vap *vap;
2185 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2186 struct ifnet *ifp = vap->iv_ifp;
2187 if (!IFNET_IS_UP_RUNNING(ifp) &&
2188 (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
2189 vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
2190 ieee80211_start_locked(vap);
2193 IEEE80211_UNLOCK(ic);
2197 * Restart all vap's running on a device.
2200 ieee80211_restart_all(struct ieee80211com *ic)
2203 * NB: do not use ieee80211_runtask here, we will
2204 * block & drain net80211 taskqueue.
2206 taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
2210 ieee80211_beacon_miss(struct ieee80211com *ic)
2213 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2214 /* Process in a taskq, the handler may reenter the driver */
2215 ieee80211_runtask(ic, &ic->ic_bmiss_task);
2217 IEEE80211_UNLOCK(ic);
2221 beacon_miss(void *arg, int npending)
2223 struct ieee80211com *ic = arg;
2224 struct ieee80211vap *vap;
2227 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2229 * We only pass events through for sta vap's in RUN+ state;
2230 * may be too restrictive but for now this saves all the
2231 * handlers duplicating these checks.
2233 if (vap->iv_opmode == IEEE80211_M_STA &&
2234 vap->iv_state >= IEEE80211_S_RUN &&
2235 vap->iv_bmiss != NULL)
2238 IEEE80211_UNLOCK(ic);
2242 beacon_swmiss(void *arg, int npending)
2244 struct ieee80211vap *vap = arg;
2245 struct ieee80211com *ic = vap->iv_ic;
2248 if (vap->iv_state >= IEEE80211_S_RUN) {
2249 /* XXX Call multiple times if npending > zero? */
2252 IEEE80211_UNLOCK(ic);
2256 * Software beacon miss handling. Check if any beacons
2257 * were received in the last period. If not post a
2258 * beacon miss; otherwise reset the counter.
2261 ieee80211_swbmiss(void *arg)
2263 struct ieee80211vap *vap = arg;
2264 struct ieee80211com *ic = vap->iv_ic;
2266 IEEE80211_LOCK_ASSERT(ic);
2268 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
2269 ("wrong state %d", vap->iv_state));
2271 if (ic->ic_flags & IEEE80211_F_SCAN) {
2273 * If scanning just ignore and reset state. If we get a
2274 * bmiss after coming out of scan because we haven't had
2275 * time to receive a beacon then we should probe the AP
2276 * before posting a real bmiss (unless iv_bmiss_max has
2277 * been artifiically lowered). A cleaner solution might
2278 * be to disable the timer on scan start/end but to handle
2279 * case of multiple sta vap's we'd need to disable the
2280 * timers of all affected vap's.
2282 vap->iv_swbmiss_count = 0;
2283 } else if (vap->iv_swbmiss_count == 0) {
2284 if (vap->iv_bmiss != NULL)
2285 ieee80211_runtask(ic, &vap->iv_swbmiss_task);
2287 vap->iv_swbmiss_count = 0;
2288 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
2289 ieee80211_swbmiss, vap);
2293 * Start an 802.11h channel switch. We record the parameters,
2294 * mark the operation pending, notify each vap through the
2295 * beacon update mechanism so it can update the beacon frame
2296 * contents, and then switch vap's to CSA state to block outbound
2297 * traffic. Devices that handle CSA directly can use the state
2298 * switch to do the right thing so long as they call
2299 * ieee80211_csa_completeswitch when it's time to complete the
2300 * channel change. Devices that depend on the net80211 layer can
2301 * use ieee80211_beacon_update to handle the countdown and the
2305 ieee80211_csa_startswitch(struct ieee80211com *ic,
2306 struct ieee80211_channel *c, int mode, int count)
2308 struct ieee80211vap *vap;
2310 IEEE80211_LOCK_ASSERT(ic);
2312 ic->ic_csa_newchan = c;
2313 ic->ic_csa_mode = mode;
2314 ic->ic_csa_count = count;
2315 ic->ic_flags |= IEEE80211_F_CSAPENDING;
2316 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2317 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2318 vap->iv_opmode == IEEE80211_M_IBSS ||
2319 vap->iv_opmode == IEEE80211_M_MBSS)
2320 ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
2321 /* switch to CSA state to block outbound traffic */
2322 if (vap->iv_state == IEEE80211_S_RUN)
2323 ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
2325 ieee80211_notify_csa(ic, c, mode, count);
2329 * Complete the channel switch by transitioning all CSA VAPs to RUN.
2330 * This is called by both the completion and cancellation functions
2331 * so each VAP is placed back in the RUN state and can thus transmit.
2334 csa_completeswitch(struct ieee80211com *ic)
2336 struct ieee80211vap *vap;
2338 ic->ic_csa_newchan = NULL;
2339 ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
2341 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2342 if (vap->iv_state == IEEE80211_S_CSA)
2343 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
2347 * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
2348 * We clear state and move all vap's in CSA state to RUN state
2349 * so they can again transmit.
2351 * Although this may not be completely correct, update the BSS channel
2352 * for each VAP to the newly configured channel. The setcurchan sets
2353 * the current operating channel for the interface (so the radio does
2354 * switch over) but the VAP BSS isn't updated, leading to incorrectly
2355 * reported information via ioctl.
2358 ieee80211_csa_completeswitch(struct ieee80211com *ic)
2360 struct ieee80211vap *vap;
2362 IEEE80211_LOCK_ASSERT(ic);
2364 KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
2366 ieee80211_setcurchan(ic, ic->ic_csa_newchan);
2367 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2368 if (vap->iv_state == IEEE80211_S_CSA)
2369 vap->iv_bss->ni_chan = ic->ic_curchan;
2371 csa_completeswitch(ic);
2375 * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
2376 * We clear state and move all vap's in CSA state to RUN state
2377 * so they can again transmit.
2380 ieee80211_csa_cancelswitch(struct ieee80211com *ic)
2382 IEEE80211_LOCK_ASSERT(ic);
2384 csa_completeswitch(ic);
2388 * Complete a DFS CAC started by ieee80211_dfs_cac_start.
2389 * We clear state and move all vap's in CAC state to RUN state.
2392 ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
2394 struct ieee80211com *ic = vap0->iv_ic;
2395 struct ieee80211vap *vap;
2399 * Complete CAC state change for lead vap first; then
2400 * clock all the other vap's waiting.
2402 KASSERT(vap0->iv_state == IEEE80211_S_CAC,
2403 ("wrong state %d", vap0->iv_state));
2404 ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
2406 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2407 if (vap->iv_state == IEEE80211_S_CAC && vap != vap0)
2408 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
2409 IEEE80211_UNLOCK(ic);
2413 * Force all vap's other than the specified vap to the INIT state
2414 * and mark them as waiting for a scan to complete. These vaps
2415 * will be brought up when the scan completes and the scanning vap
2416 * reaches RUN state by wakeupwaiting.
2419 markwaiting(struct ieee80211vap *vap0)
2421 struct ieee80211com *ic = vap0->iv_ic;
2422 struct ieee80211vap *vap;
2424 IEEE80211_LOCK_ASSERT(ic);
2427 * A vap list entry can not disappear since we are running on the
2428 * taskqueue and a vap destroy will queue and drain another state
2431 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2434 if (vap->iv_state != IEEE80211_S_INIT) {
2435 /* NB: iv_newstate may drop the lock */
2436 vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
2437 IEEE80211_LOCK_ASSERT(ic);
2438 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2444 * Wakeup all vap's waiting for a scan to complete. This is the
2445 * companion to markwaiting (above) and is used to coordinate
2446 * multiple vaps scanning.
2447 * This is called from the state taskqueue.
2450 wakeupwaiting(struct ieee80211vap *vap0)
2452 struct ieee80211com *ic = vap0->iv_ic;
2453 struct ieee80211vap *vap;
2455 IEEE80211_LOCK_ASSERT(ic);
2458 * A vap list entry can not disappear since we are running on the
2459 * taskqueue and a vap destroy will queue and drain another state
2462 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2465 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
2466 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2467 /* NB: sta's cannot go INIT->RUN */
2468 /* NB: iv_newstate may drop the lock */
2471 * This is problematic if the interface has OACTIVE
2472 * set. Only the deferred ieee80211_newstate_cb()
2473 * will end up actually /clearing/ the OACTIVE
2474 * flag on a state transition to RUN from a non-RUN
2477 * But, we're not actually deferring this callback;
2478 * and when the deferred call occurs it shows up as
2479 * a RUN->RUN transition! So the flag isn't/wasn't
2482 * I'm also not sure if it's correct to actually
2483 * do the transitions here fully through the deferred
2484 * paths either as other things can be invoked as
2485 * part of that state machine.
2487 * So just keep this in mind when looking at what
2488 * the markwaiting/wakeupwaiting routines are doing
2489 * and how they invoke vap state changes.
2492 vap->iv_newstate(vap,
2493 vap->iv_opmode == IEEE80211_M_STA ?
2494 IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
2495 IEEE80211_LOCK_ASSERT(ic);
2501 * Handle post state change work common to all operating modes.
2504 ieee80211_newstate_cb(void *xvap, int npending)
2506 struct ieee80211vap *vap = xvap;
2507 struct ieee80211com *ic = vap->iv_ic;
2508 enum ieee80211_state nstate, ostate;
2512 nstate = vap->iv_nstate;
2513 arg = vap->iv_nstate_arg;
2515 if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
2517 * We have been requested to drop back to the INIT before
2518 * proceeding to the new state.
2520 /* Deny any state changes while we are here. */
2521 vap->iv_nstate = IEEE80211_S_INIT;
2522 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2523 "%s: %s -> %s arg %d\n", __func__,
2524 ieee80211_state_name[vap->iv_state],
2525 ieee80211_state_name[vap->iv_nstate], arg);
2526 vap->iv_newstate(vap, vap->iv_nstate, 0);
2527 IEEE80211_LOCK_ASSERT(ic);
2528 vap->iv_flags_ext &= ~(IEEE80211_FEXT_REINIT |
2529 IEEE80211_FEXT_STATEWAIT);
2530 /* enqueue new state transition after cancel_scan() task */
2531 ieee80211_new_state_locked(vap, nstate, arg);
2535 ostate = vap->iv_state;
2536 if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
2538 * SCAN was forced; e.g. on beacon miss. Force other running
2539 * vap's to INIT state and mark them as waiting for the scan to
2540 * complete. This insures they don't interfere with our
2541 * scanning. Since we are single threaded the vaps can not
2542 * transition again while we are executing.
2544 * XXX not always right, assumes ap follows sta
2548 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2549 "%s: %s -> %s arg %d\n", __func__,
2550 ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
2552 rc = vap->iv_newstate(vap, nstate, arg);
2553 IEEE80211_LOCK_ASSERT(ic);
2554 vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
2556 /* State transition failed */
2557 KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
2558 KASSERT(nstate != IEEE80211_S_INIT,
2559 ("INIT state change failed"));
2560 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2561 "%s: %s returned error %d\n", __func__,
2562 ieee80211_state_name[nstate], rc);
2567 * Handle the case of a RUN->RUN transition occuring when STA + AP
2568 * VAPs occur on the same radio.
2570 * The mark and wakeup waiting routines call iv_newstate() directly,
2571 * but they do not end up deferring state changes here.
2572 * Thus, although the VAP newstate method sees a transition
2573 * of RUN->INIT->RUN, the deferred path here only sees a RUN->RUN
2574 * transition. If OACTIVE is set then it is never cleared.
2576 * So, if we're here and the state is RUN, just clear OACTIVE.
2577 * At some point if the markwaiting/wakeupwaiting paths end up
2578 * also invoking the deferred state updates then this will
2579 * be no-op code - and also if OACTIVE is finally retired, it'll
2580 * also be no-op code.
2582 if (nstate == IEEE80211_S_RUN) {
2584 * OACTIVE may be set on the vap if the upper layer
2585 * tried to transmit (e.g. IPv6 NDP) before we reach
2586 * RUN state. Clear it and restart xmit.
2588 * Note this can also happen as a result of SLEEP->RUN
2589 * (i.e. coming out of power save mode).
2591 * Historically this was done only for a state change
2592 * but is needed earlier; see next comment. The 2nd half
2593 * of the work is still only done in case of an actual
2594 * state change below.
2597 * Unblock the VAP queue; a RUN->RUN state can happen
2598 * on a STA+AP setup on the AP vap. See wakeupwaiting().
2600 vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2603 * XXX TODO Kick-start a VAP queue - this should be a method!
2607 /* No actual transition, skip post processing */
2608 if (ostate == nstate)
2611 if (nstate == IEEE80211_S_RUN) {
2613 /* bring up any vaps waiting on us */
2615 } else if (nstate == IEEE80211_S_INIT) {
2617 * Flush the scan cache if we did the last scan (XXX?)
2618 * and flush any frames on send queues from this vap.
2619 * Note the mgt q is used only for legacy drivers and
2620 * will go away shortly.
2622 ieee80211_scan_flush(vap);
2625 * XXX TODO: ic/vap queue flush
2629 IEEE80211_UNLOCK(ic);
2633 * Public interface for initiating a state machine change.
2634 * This routine single-threads the request and coordinates
2635 * the scheduling of multiple vaps for the purpose of selecting
2636 * an operating channel. Specifically the following scenarios
2638 * o only one vap can be selecting a channel so on transition to
2639 * SCAN state if another vap is already scanning then
2640 * mark the caller for later processing and return without
2641 * doing anything (XXX? expectations by caller of synchronous operation)
2642 * o only one vap can be doing CAC of a channel so on transition to
2643 * CAC state if another vap is already scanning for radar then
2644 * mark the caller for later processing and return without
2645 * doing anything (XXX? expectations by caller of synchronous operation)
2646 * o if another vap is already running when a request is made
2647 * to SCAN then an operating channel has been chosen; bypass
2648 * the scan and just join the channel
2650 * Note that the state change call is done through the iv_newstate
2651 * method pointer so any driver routine gets invoked. The driver
2652 * will normally call back into operating mode-specific
2653 * ieee80211_newstate routines (below) unless it needs to completely
2654 * bypass the state machine (e.g. because the firmware has it's
2655 * own idea how things should work). Bypassing the net80211 layer
2656 * is usually a mistake and indicates lack of proper integration
2657 * with the net80211 layer.
2660 ieee80211_new_state_locked(struct ieee80211vap *vap,
2661 enum ieee80211_state nstate, int arg)
2663 struct ieee80211com *ic = vap->iv_ic;
2664 struct ieee80211vap *vp;
2665 enum ieee80211_state ostate;
2666 int nrunning, nscanning;
2668 IEEE80211_LOCK_ASSERT(ic);
2670 if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
2671 if (vap->iv_nstate == IEEE80211_S_INIT ||
2672 ((vap->iv_state == IEEE80211_S_INIT ||
2673 (vap->iv_flags_ext & IEEE80211_FEXT_REINIT)) &&
2674 vap->iv_nstate == IEEE80211_S_SCAN &&
2675 nstate > IEEE80211_S_SCAN)) {
2677 * XXX The vap is being stopped/started,
2678 * do not allow any other state changes
2679 * until this is completed.
2681 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2682 "%s: %s -> %s (%s) transition discarded\n",
2684 ieee80211_state_name[vap->iv_state],
2685 ieee80211_state_name[nstate],
2686 ieee80211_state_name[vap->iv_nstate]);
2688 } else if (vap->iv_state != vap->iv_nstate) {
2690 /* Warn if the previous state hasn't completed. */
2691 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2692 "%s: pending %s -> %s transition lost\n", __func__,
2693 ieee80211_state_name[vap->iv_state],
2694 ieee80211_state_name[vap->iv_nstate]);
2696 /* XXX temporarily enable to identify issues */
2697 if_printf(vap->iv_ifp,
2698 "%s: pending %s -> %s transition lost\n",
2699 __func__, ieee80211_state_name[vap->iv_state],
2700 ieee80211_state_name[vap->iv_nstate]);
2705 nrunning = nscanning = 0;
2706 /* XXX can track this state instead of calculating */
2707 TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
2709 if (vp->iv_state >= IEEE80211_S_RUN)
2711 /* XXX doesn't handle bg scan */
2712 /* NB: CAC+AUTH+ASSOC treated like SCAN */
2713 else if (vp->iv_state > IEEE80211_S_INIT)
2717 ostate = vap->iv_state;
2718 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2719 "%s: %s -> %s (arg %d) (nrunning %d nscanning %d)\n", __func__,
2720 ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg,
2721 nrunning, nscanning);
2723 case IEEE80211_S_SCAN:
2724 if (ostate == IEEE80211_S_INIT) {
2726 * INIT -> SCAN happens on initial bringup.
2728 KASSERT(!(nscanning && nrunning),
2729 ("%d scanning and %d running", nscanning, nrunning));
2732 * Someone is scanning, defer our state
2733 * change until the work has completed.
2735 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2736 "%s: defer %s -> %s\n",
2737 __func__, ieee80211_state_name[ostate],
2738 ieee80211_state_name[nstate]);
2739 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2744 * Someone is operating; just join the channel
2748 /* XXX check each opmode, adhoc? */
2749 if (vap->iv_opmode == IEEE80211_M_STA)
2750 nstate = IEEE80211_S_SCAN;
2752 nstate = IEEE80211_S_RUN;
2753 #ifdef IEEE80211_DEBUG
2754 if (nstate != IEEE80211_S_SCAN) {
2755 IEEE80211_DPRINTF(vap,
2756 IEEE80211_MSG_STATE,
2757 "%s: override, now %s -> %s\n",
2759 ieee80211_state_name[ostate],
2760 ieee80211_state_name[nstate]);
2766 case IEEE80211_S_RUN:
2767 if (vap->iv_opmode == IEEE80211_M_WDS &&
2768 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
2771 * Legacy WDS with someone else scanning; don't
2772 * go online until that completes as we should
2773 * follow the other vap to the channel they choose.
2775 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2776 "%s: defer %s -> %s (legacy WDS)\n", __func__,
2777 ieee80211_state_name[ostate],
2778 ieee80211_state_name[nstate]);
2779 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2782 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
2783 IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2784 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
2785 !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
2787 * This is a DFS channel, transition to CAC state
2788 * instead of RUN. This allows us to initiate
2789 * Channel Availability Check (CAC) as specified
2792 nstate = IEEE80211_S_CAC;
2793 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2794 "%s: override %s -> %s (DFS)\n", __func__,
2795 ieee80211_state_name[ostate],
2796 ieee80211_state_name[nstate]);
2799 case IEEE80211_S_INIT:
2800 /* cancel any scan in progress */
2801 ieee80211_cancel_scan(vap);
2802 if (ostate == IEEE80211_S_INIT ) {
2803 /* XXX don't believe this */
2804 /* INIT -> INIT. nothing to do */
2805 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2811 /* defer the state change to a thread */
2812 vap->iv_nstate = nstate;
2813 vap->iv_nstate_arg = arg;
2814 vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
2815 ieee80211_runtask(ic, &vap->iv_nstate_task);
2820 ieee80211_new_state(struct ieee80211vap *vap,
2821 enum ieee80211_state nstate, int arg)
2823 struct ieee80211com *ic = vap->iv_ic;
2827 rc = ieee80211_new_state_locked(vap, nstate, arg);
2828 IEEE80211_UNLOCK(ic);