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);
254 null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
255 const struct ieee80211_bpf_params *params)
258 ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
264 ieee80211_proto_attach(struct ieee80211com *ic)
268 /* override the 802.3 setting */
269 hdrlen = ic->ic_headroom
270 + sizeof(struct ieee80211_qosframe_addr4)
271 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
272 + IEEE80211_WEP_EXTIVLEN;
273 /* XXX no way to recalculate on ifdetach */
274 if (ALIGN(hdrlen) > max_linkhdr) {
275 /* XXX sanity check... */
276 max_linkhdr = ALIGN(hdrlen);
277 max_hdr = max_linkhdr + max_protohdr;
278 max_datalen = MHLEN - max_hdr;
280 //ic->ic_protmode = IEEE80211_PROT_CTSONLY;
282 TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
283 TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
284 TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
285 TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
286 TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
287 TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
288 TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
290 ic->ic_wme.wme_hipri_switch_hysteresis =
291 AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
293 /* initialize management frame handlers */
294 ic->ic_send_mgmt = ieee80211_send_mgmt;
295 ic->ic_raw_xmit = null_raw_xmit;
297 ieee80211_adhoc_attach(ic);
298 ieee80211_sta_attach(ic);
299 ieee80211_wds_attach(ic);
300 ieee80211_hostap_attach(ic);
301 #ifdef IEEE80211_SUPPORT_MESH
302 ieee80211_mesh_attach(ic);
304 ieee80211_monitor_attach(ic);
308 ieee80211_proto_detach(struct ieee80211com *ic)
310 ieee80211_monitor_detach(ic);
311 #ifdef IEEE80211_SUPPORT_MESH
312 ieee80211_mesh_detach(ic);
314 ieee80211_hostap_detach(ic);
315 ieee80211_wds_detach(ic);
316 ieee80211_adhoc_detach(ic);
317 ieee80211_sta_detach(ic);
321 null_update_beacon(struct ieee80211vap *vap, int item)
326 ieee80211_proto_vattach(struct ieee80211vap *vap)
328 struct ieee80211com *ic = vap->iv_ic;
329 struct ifnet *ifp = vap->iv_ifp;
332 /* override the 802.3 setting */
333 ifp->if_hdrlen = ic->ic_headroom
334 + sizeof(struct ieee80211_qosframe_addr4)
335 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
336 + IEEE80211_WEP_EXTIVLEN;
338 vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
339 vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
340 vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
341 callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
342 callout_init(&vap->iv_mgtsend, 1);
343 TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
344 TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
345 TASK_INIT(&vap->iv_wme_task, 0, vap_update_wme, vap);
346 TASK_INIT(&vap->iv_slot_task, 0, vap_update_slot, vap);
347 TASK_INIT(&vap->iv_erp_protmode_task, 0, vap_update_erp_protmode, vap);
348 TASK_INIT(&vap->iv_ht_protmode_task, 0, vap_update_ht_protmode, vap);
349 TASK_INIT(&vap->iv_preamble_task, 0, vap_update_preamble, vap);
351 * Install default tx rate handling: no fixed rate, lowest
352 * supported rate for mgmt and multicast frames. Default
353 * max retry count. These settings can be changed by the
354 * driver and/or user applications.
356 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
357 if (isclr(ic->ic_modecaps, i))
360 const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
362 vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
365 * Setting the management rate to MCS 0 assumes that the
366 * BSS Basic rate set is empty and the BSS Basic MCS set
369 * Since we're not checking this, default to the lowest
370 * defined rate for this mode.
372 * At least one 11n AP (DLINK DIR-825) is reported to drop
373 * some MCS management traffic (eg BA response frames.)
375 * See also: 9.6.0 of the 802.11n-2009 specification.
378 if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
379 vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
380 vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
382 vap->iv_txparms[i].mgmtrate =
383 rs->rs_rates[0] & IEEE80211_RATE_VAL;
384 vap->iv_txparms[i].mcastrate =
385 rs->rs_rates[0] & IEEE80211_RATE_VAL;
388 vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
389 vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
390 vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
392 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
394 vap->iv_update_beacon = null_update_beacon;
395 vap->iv_deliver_data = ieee80211_deliver_data;
396 vap->iv_protmode = IEEE80211_PROT_CTSONLY;
398 /* attach support for operating mode */
399 ic->ic_vattach[vap->iv_opmode](vap);
403 ieee80211_proto_vdetach(struct ieee80211vap *vap)
405 #define FREEAPPIE(ie) do { \
407 IEEE80211_FREE(ie, M_80211_NODE_IE); \
410 * Detach operating mode module.
412 if (vap->iv_opdetach != NULL)
413 vap->iv_opdetach(vap);
415 * This should not be needed as we detach when reseting
416 * the state but be conservative here since the
417 * authenticator may do things like spawn kernel threads.
419 if (vap->iv_auth->ia_detach != NULL)
420 vap->iv_auth->ia_detach(vap);
422 * Detach any ACL'ator.
424 if (vap->iv_acl != NULL)
425 vap->iv_acl->iac_detach(vap);
427 FREEAPPIE(vap->iv_appie_beacon);
428 FREEAPPIE(vap->iv_appie_probereq);
429 FREEAPPIE(vap->iv_appie_proberesp);
430 FREEAPPIE(vap->iv_appie_assocreq);
431 FREEAPPIE(vap->iv_appie_assocresp);
432 FREEAPPIE(vap->iv_appie_wpa);
437 * Simple-minded authenticator module support.
440 #define IEEE80211_AUTH_MAX (IEEE80211_AUTH_WPA+1)
441 /* XXX well-known names */
442 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
443 "wlan_internal", /* IEEE80211_AUTH_NONE */
444 "wlan_internal", /* IEEE80211_AUTH_OPEN */
445 "wlan_internal", /* IEEE80211_AUTH_SHARED */
446 "wlan_xauth", /* IEEE80211_AUTH_8021X */
447 "wlan_internal", /* IEEE80211_AUTH_AUTO */
448 "wlan_xauth", /* IEEE80211_AUTH_WPA */
450 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
452 static const struct ieee80211_authenticator auth_internal = {
453 .ia_name = "wlan_internal",
456 .ia_node_join = NULL,
457 .ia_node_leave = NULL,
461 * Setup internal authenticators once; they are never unregistered.
464 ieee80211_auth_setup(void)
466 ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
467 ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
468 ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
470 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
472 const struct ieee80211_authenticator *
473 ieee80211_authenticator_get(int auth)
475 if (auth >= IEEE80211_AUTH_MAX)
477 if (authenticators[auth] == NULL)
478 ieee80211_load_module(auth_modnames[auth]);
479 return authenticators[auth];
483 ieee80211_authenticator_register(int type,
484 const struct ieee80211_authenticator *auth)
486 if (type >= IEEE80211_AUTH_MAX)
488 authenticators[type] = auth;
492 ieee80211_authenticator_unregister(int type)
495 if (type >= IEEE80211_AUTH_MAX)
497 authenticators[type] = NULL;
501 * Very simple-minded ACL module support.
503 /* XXX just one for now */
504 static const struct ieee80211_aclator *acl = NULL;
507 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
509 printf("wlan: %s acl policy registered\n", iac->iac_name);
514 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
518 printf("wlan: %s acl policy unregistered\n", iac->iac_name);
521 const struct ieee80211_aclator *
522 ieee80211_aclator_get(const char *name)
525 ieee80211_load_module("wlan_acl");
526 return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
530 ieee80211_print_essid(const uint8_t *essid, int len)
535 if (len > IEEE80211_NWID_LEN)
536 len = IEEE80211_NWID_LEN;
537 /* determine printable or not */
538 for (i = 0, p = essid; i < len; i++, p++) {
539 if (*p < ' ' || *p > 0x7e)
544 for (i = 0, p = essid; i < len; i++, p++)
549 for (i = 0, p = essid; i < len; i++, p++)
555 ieee80211_dump_pkt(struct ieee80211com *ic,
556 const uint8_t *buf, int len, int rate, int rssi)
558 const struct ieee80211_frame *wh;
561 wh = (const struct ieee80211_frame *)buf;
562 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
563 case IEEE80211_FC1_DIR_NODS:
564 printf("NODS %s", ether_sprintf(wh->i_addr2));
565 printf("->%s", ether_sprintf(wh->i_addr1));
566 printf("(%s)", ether_sprintf(wh->i_addr3));
568 case IEEE80211_FC1_DIR_TODS:
569 printf("TODS %s", ether_sprintf(wh->i_addr2));
570 printf("->%s", ether_sprintf(wh->i_addr3));
571 printf("(%s)", ether_sprintf(wh->i_addr1));
573 case IEEE80211_FC1_DIR_FROMDS:
574 printf("FRDS %s", ether_sprintf(wh->i_addr3));
575 printf("->%s", ether_sprintf(wh->i_addr1));
576 printf("(%s)", ether_sprintf(wh->i_addr2));
578 case IEEE80211_FC1_DIR_DSTODS:
579 printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
580 printf("->%s", ether_sprintf(wh->i_addr3));
581 printf("(%s", ether_sprintf(wh->i_addr2));
582 printf("->%s)", ether_sprintf(wh->i_addr1));
585 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
586 case IEEE80211_FC0_TYPE_DATA:
589 case IEEE80211_FC0_TYPE_MGT:
590 printf(" %s", ieee80211_mgt_subtype_name(wh->i_fc[0]));
593 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
596 if (IEEE80211_QOS_HAS_SEQ(wh)) {
597 const struct ieee80211_qosframe *qwh =
598 (const struct ieee80211_qosframe *)buf;
599 printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
600 qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
602 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
605 off = ieee80211_anyhdrspace(ic, wh);
606 printf(" WEP [IV %.02x %.02x %.02x",
607 buf[off+0], buf[off+1], buf[off+2]);
608 if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
609 printf(" %.02x %.02x %.02x",
610 buf[off+4], buf[off+5], buf[off+6]);
611 printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
614 printf(" %dM", rate / 2);
616 printf(" +%d", rssi);
619 for (i = 0; i < len; i++) {
622 printf("%02x", buf[i]);
629 findrix(const struct ieee80211_rateset *rs, int r)
633 for (i = 0; i < rs->rs_nrates; i++)
634 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
640 ieee80211_fix_rate(struct ieee80211_node *ni,
641 struct ieee80211_rateset *nrs, int flags)
643 struct ieee80211vap *vap = ni->ni_vap;
644 struct ieee80211com *ic = ni->ni_ic;
645 int i, j, rix, error;
646 int okrate, badrate, fixedrate, ucastrate;
647 const struct ieee80211_rateset *srs;
651 okrate = badrate = 0;
652 ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
653 if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
655 * Workaround awkwardness with fixed rate. We are called
656 * to check both the legacy rate set and the HT rate set
657 * but we must apply any legacy fixed rate check only to the
658 * legacy rate set and vice versa. We cannot tell what type
659 * of rate set we've been given (legacy or HT) but we can
660 * distinguish the fixed rate type (MCS have 0x80 set).
661 * So to deal with this the caller communicates whether to
662 * check MCS or legacy rate using the flags and we use the
663 * type of any fixed rate to avoid applying an MCS to a
664 * legacy rate and vice versa.
666 if (ucastrate & 0x80) {
667 if (flags & IEEE80211_F_DOFRATE)
668 flags &= ~IEEE80211_F_DOFRATE;
669 } else if ((ucastrate & 0x80) == 0) {
670 if (flags & IEEE80211_F_DOFMCS)
671 flags &= ~IEEE80211_F_DOFMCS;
673 /* NB: required to make MCS match below work */
674 ucastrate &= IEEE80211_RATE_VAL;
676 fixedrate = IEEE80211_FIXED_RATE_NONE;
678 * XXX we are called to process both MCS and legacy rates;
679 * we must use the appropriate basic rate set or chaos will
680 * ensue; for now callers that want MCS must supply
681 * IEEE80211_F_DOBRS; at some point we'll need to split this
682 * function so there are two variants, one for MCS and one
685 if (flags & IEEE80211_F_DOBRS)
686 srs = (const struct ieee80211_rateset *)
687 ieee80211_get_suphtrates(ic, ni->ni_chan);
689 srs = ieee80211_get_suprates(ic, ni->ni_chan);
690 for (i = 0; i < nrs->rs_nrates; ) {
691 if (flags & IEEE80211_F_DOSORT) {
695 for (j = i + 1; j < nrs->rs_nrates; j++) {
696 if (IEEE80211_RV(nrs->rs_rates[i]) >
697 IEEE80211_RV(nrs->rs_rates[j])) {
698 r = nrs->rs_rates[i];
699 nrs->rs_rates[i] = nrs->rs_rates[j];
700 nrs->rs_rates[j] = r;
704 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
707 * Check for fixed rate.
712 * Check against supported rates.
714 rix = findrix(srs, r);
715 if (flags & IEEE80211_F_DONEGO) {
718 * A rate in the node's rate set is not
719 * supported. If this is a basic rate and we
720 * are operating as a STA then this is an error.
721 * Otherwise we just discard/ignore the rate.
723 if ((flags & IEEE80211_F_JOIN) &&
724 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
726 } else if ((flags & IEEE80211_F_JOIN) == 0) {
728 * Overwrite with the supported rate
729 * value so any basic rate bit is set.
731 nrs->rs_rates[i] = srs->rs_rates[rix];
734 if ((flags & IEEE80211_F_DODEL) && rix < 0) {
736 * Delete unacceptable rates.
739 for (j = i; j < nrs->rs_nrates; j++)
740 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
741 nrs->rs_rates[j] = 0;
745 okrate = nrs->rs_rates[i];
748 if (okrate == 0 || error != 0 ||
749 ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
750 fixedrate != ucastrate)) {
751 IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
752 "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
753 "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
754 return badrate | IEEE80211_RATE_BASIC;
756 return IEEE80211_RV(okrate);
760 * Reset 11g-related state.
762 * This is for per-VAP ERP/11g state.
764 * Eventually everything in ieee80211_reset_erp() will be
765 * per-VAP and in here.
768 ieee80211_vap_reset_erp(struct ieee80211vap *vap)
770 struct ieee80211com *ic = vap->iv_ic;
772 vap->iv_nonerpsta = 0;
773 vap->iv_longslotsta = 0;
775 vap->iv_flags &= ~IEEE80211_F_USEPROT;
777 * Set short preamble and ERP barker-preamble flags.
779 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
780 (vap->iv_caps & IEEE80211_C_SHPREAMBLE)) {
781 vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
782 vap->iv_flags &= ~IEEE80211_F_USEBARKER;
784 vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
785 vap->iv_flags |= IEEE80211_F_USEBARKER;
789 * Short slot time is enabled only when operating in 11g
790 * and not in an IBSS. We must also honor whether or not
791 * the driver is capable of doing it.
793 ieee80211_vap_set_shortslottime(vap,
794 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
795 IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
796 (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
797 vap->iv_opmode == IEEE80211_M_HOSTAP &&
798 (ic->ic_caps & IEEE80211_C_SHSLOT)));
802 * Reset 11g-related state.
804 * Note this resets the global state and a caller should schedule
805 * a re-check of all the VAPs after setup to update said state.
808 ieee80211_reset_erp(struct ieee80211com *ic)
811 ic->ic_flags &= ~IEEE80211_F_USEPROT;
813 * Set short preamble and ERP barker-preamble flags.
815 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
816 (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
817 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
818 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
820 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
821 ic->ic_flags |= IEEE80211_F_USEBARKER;
824 /* XXX TODO: schedule a new per-VAP ERP calculation */
828 * Deferred slot time update.
830 * For per-VAP slot time configuration, call the VAP
831 * method if the VAP requires it. Otherwise, just call the
832 * older global method.
834 * If the per-VAP method is called then it's expected that
835 * the driver/firmware will take care of turning the per-VAP
836 * flags into slot time configuration.
838 * If the per-VAP method is not called then the global flags will be
839 * flipped into sync with the VAPs; ic_flags IEEE80211_F_SHSLOT will
840 * be set only if all of the vaps will have it set.
842 * Look at the comments for vap_update_erp_protmode() for more
843 * background; this assumes all VAPs are on the same channel.
846 vap_update_slot(void *arg, int npending)
848 struct ieee80211vap *vap = arg;
849 struct ieee80211com *ic = vap->iv_ic;
850 struct ieee80211vap *iv;
851 int num_shslot = 0, num_lgslot = 0;
854 * Per-VAP path - we've already had the flags updated;
855 * so just notify the driver and move on.
857 if (vap->iv_updateslot != NULL) {
858 vap->iv_updateslot(vap);
863 * Iterate over all of the VAP flags to update the
866 * If all vaps have short slot enabled then flip on
867 * short slot. If any vap has it disabled then
868 * we leave it globally disabled. This should provide
869 * correct behaviour in a multi-BSS scenario where
870 * at least one VAP has short slot disabled for some
874 TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
875 if (iv->iv_flags & IEEE80211_F_SHSLOT)
882 * It looks backwards but - if the number of short slot VAPs
883 * is zero then we're not short slot. Else, we have one
884 * or more short slot VAPs and we're checking to see if ANY
885 * of them have short slot disabled.
888 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
889 else if (num_lgslot == 0)
890 ic->ic_flags |= IEEE80211_F_SHSLOT;
891 IEEE80211_UNLOCK(ic);
894 * Call the driver with our new global slot time flags.
896 if (ic->ic_updateslot != NULL)
897 ic->ic_updateslot(ic);
901 * Deferred ERP protmode update.
903 * This currently calculates the global ERP protection mode flag
904 * based on each of the VAPs. Any VAP with it enabled is enough
905 * for the global flag to be enabled. All VAPs with it disabled
906 * is enough for it to be disabled.
908 * This may make sense right now for the supported hardware where
909 * net80211 is controlling the single channel configuration, but
910 * offload firmware that's doing channel changes (eg off-channel
911 * TDLS, off-channel STA, off-channel P2P STA/AP) may get some
912 * silly looking flag updates.
914 * Ideally the protection mode calculation is done based on the
915 * channel, and all VAPs using that channel will inherit it.
916 * But until that's what net80211 does, this wil have to do.
919 vap_update_erp_protmode(void *arg, int npending)
921 struct ieee80211vap *vap = arg;
922 struct ieee80211com *ic = vap->iv_ic;
923 struct ieee80211vap *iv;
924 int enable_protmode = 0;
925 int non_erp_present = 0;
928 * Iterate over all of the VAPs to calculate the overlapping
929 * ERP protection mode configuration and ERP present math.
931 * For now we assume that if a driver can handle this per-VAP
932 * then it'll ignore the ic->ic_protmode variant and instead
933 * will look at the vap related flags.
936 TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
937 if (iv->iv_flags & IEEE80211_F_USEPROT)
939 if (iv->iv_flags_ext & IEEE80211_FEXT_NONERP_PR)
944 ic->ic_flags |= IEEE80211_F_USEPROT;
946 ic->ic_flags &= ~IEEE80211_F_USEPROT;
949 ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
951 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
953 /* Beacon update on all VAPs */
954 ieee80211_notify_erp_locked(ic);
956 IEEE80211_UNLOCK(ic);
958 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
959 "%s: called; enable_protmode=%d, non_erp_present=%d\n",
960 __func__, enable_protmode, non_erp_present);
963 * Now that the global configuration flags are calculated,
964 * notify the VAP about its configuration.
966 * The global flags will be used when assembling ERP IEs
967 * for multi-VAP operation, even if it's on a different
968 * channel. Yes, that's going to need fixing in the
971 if (vap->iv_erp_protmode_update != NULL)
972 vap->iv_erp_protmode_update(vap);
976 * Deferred ERP short preamble/barker update.
978 * All VAPs need to use short preamble for it to be globally
981 * Look at the comments for vap_update_erp_protmode() for more
982 * background; this assumes all VAPs are on the same channel.
985 vap_update_preamble(void *arg, int npending)
987 struct ieee80211vap *vap = arg;
988 struct ieee80211com *ic = vap->iv_ic;
989 struct ieee80211vap *iv;
990 int barker_count = 0, short_preamble_count = 0, count = 0;
993 * Iterate over all of the VAPs to calculate the overlapping
994 * short or long preamble configuration.
996 * For now we assume that if a driver can handle this per-VAP
997 * then it'll ignore the ic->ic_flags variant and instead
998 * will look at the vap related flags.
1001 TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
1002 if (iv->iv_flags & IEEE80211_F_USEBARKER)
1004 if (iv->iv_flags & IEEE80211_F_SHPREAMBLE)
1005 short_preamble_count++;
1010 * As with vap_update_erp_protmode(), the global flags are
1011 * currently used for beacon IEs.
1013 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1014 "%s: called; barker_count=%d, short_preamble_count=%d\n",
1015 __func__, barker_count, short_preamble_count);
1018 * Only flip on short preamble if all of the VAPs support
1021 if (barker_count == 0 && short_preamble_count == count) {
1022 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1023 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1025 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1026 ic->ic_flags |= IEEE80211_F_USEBARKER;
1028 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1029 "%s: global barker=%d preamble=%d\n",
1031 !! (ic->ic_flags & IEEE80211_F_USEBARKER),
1032 !! (ic->ic_flags & IEEE80211_F_SHPREAMBLE));
1034 /* Beacon update on all VAPs */
1035 ieee80211_notify_erp_locked(ic);
1037 IEEE80211_UNLOCK(ic);
1039 /* Driver notification */
1040 if (vap->iv_erp_protmode_update != NULL)
1041 vap->iv_preamble_update(vap);
1045 * Deferred HT protmode update and beacon update.
1047 * Look at the comments for vap_update_erp_protmode() for more
1048 * background; this assumes all VAPs are on the same channel.
1051 vap_update_ht_protmode(void *arg, int npending)
1053 struct ieee80211vap *vap = arg;
1054 struct ieee80211vap *iv;
1055 struct ieee80211com *ic = vap->iv_ic;
1056 int num_vaps = 0, num_pure = 0, num_mixed = 0;
1057 int num_optional = 0, num_ht2040 = 0, num_nonht = 0;
1058 int num_ht_sta = 0, num_ht40_sta = 0, num_sta = 0;
1059 int num_nonhtpr = 0;
1062 * Iterate over all of the VAPs to calculate everything.
1064 * There are a few different flags to calculate:
1066 * + whether there's HT only or HT+legacy stations;
1067 * + whether there's HT20, HT40, or HT20+HT40 stations;
1068 * + whether the desired protection mode is mixed, pure or
1069 * one of the two above.
1071 * For now we assume that if a driver can handle this per-VAP
1072 * then it'll ignore the ic->ic_htprotmode / ic->ic_curhtprotmode
1073 * variant and instead will look at the vap related variables.
1075 * XXX TODO: non-greenfield STAs present (IEEE80211_HTINFO_NONGF_PRESENT) !
1079 TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
1081 /* overlapping BSSes advertising non-HT status present */
1082 if (iv->iv_flags_ht & IEEE80211_FHT_NONHT_PR)
1084 /* Operating mode flags */
1085 if (iv->iv_curhtprotmode & IEEE80211_HTINFO_NONHT_PRESENT)
1087 switch (iv->iv_curhtprotmode & IEEE80211_HTINFO_OPMODE) {
1088 case IEEE80211_HTINFO_OPMODE_PURE:
1091 case IEEE80211_HTINFO_OPMODE_PROTOPT:
1094 case IEEE80211_HTINFO_OPMODE_HT20PR:
1097 case IEEE80211_HTINFO_OPMODE_MIXED:
1102 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
1103 "%s: vap %s: nonht_pr=%d, curhtprotmode=0x%02x\n",
1105 ieee80211_get_vap_ifname(iv),
1106 !! (iv->iv_flags_ht & IEEE80211_FHT_NONHT_PR),
1107 iv->iv_curhtprotmode);
1109 num_ht_sta += iv->iv_ht_sta_assoc;
1110 num_ht40_sta += iv->iv_ht40_sta_assoc;
1111 num_sta += iv->iv_sta_assoc;
1115 * Step 1 - if any VAPs indicate NONHT_PR set (overlapping BSS
1116 * non-HT present), set it here. This shouldn't be used by
1117 * anything but the old overlapping BSS logic so if any drivers
1118 * consume it, it's up to date.
1121 ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1123 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1126 * Step 2 - default HT protection mode to MIXED (802.11-2016 10.26.3.1.)
1128 * + If all VAPs are PURE, we can stay PURE.
1129 * + If all VAPs are PROTOPT, we can go to PROTOPT.
1130 * + If any VAP has HT20PR then it sees at least a HT40+HT20 station.
1131 * Note that we may have a VAP with one HT20 and a VAP with one HT40;
1132 * So we look at the sum ht and sum ht40 sta counts; if we have a
1133 * HT station and the HT20 != HT40 count, we have to do HT20PR here.
1134 * Note all stations need to be HT for this to be an option.
1135 * + The fall-through is MIXED, because it means we have some odd
1136 * non HT40-involved combination of opmode and this is the most
1139 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_MIXED;
1141 if (num_pure == num_vaps)
1142 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
1144 if (num_optional == num_vaps)
1145 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PROTOPT;
1148 * Note: we need /a/ HT40 station somewhere for this to
1151 if ((num_ht2040 > 0) ||
1152 ((num_ht_sta > 0) && (num_ht40_sta > 0) &&
1153 (num_ht_sta != num_ht40_sta)))
1154 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1157 * Step 3 - if any of the stations across the VAPs are
1158 * non-HT then this needs to be flipped back to MIXED.
1160 if (num_ht_sta != num_sta)
1161 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_MIXED;
1164 * Step 4 - If we see any overlapping BSS non-HT stations
1165 * via beacons then flip on NONHT_PRESENT.
1167 if (num_nonhtpr > 0)
1168 ic->ic_curhtprotmode |= IEEE80211_HTINFO_NONHT_PRESENT;
1170 /* Notify all VAPs to potentially update their beacons */
1171 TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next)
1172 ieee80211_htinfo_notify(iv);
1174 IEEE80211_UNLOCK(ic);
1176 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
1177 "%s: global: nonht_pr=%d ht_opmode=0x%02x\n",
1179 !! (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR),
1180 ic->ic_curhtprotmode);
1183 if (vap->iv_erp_protmode_update != NULL)
1184 vap->iv_ht_protmode_update(vap);
1188 * Set the short slot time state and notify the driver.
1190 * This is the per-VAP slot time state.
1193 ieee80211_vap_set_shortslottime(struct ieee80211vap *vap, int onoff)
1195 struct ieee80211com *ic = vap->iv_ic;
1200 * Only modify the per-VAP slot time.
1203 vap->iv_flags |= IEEE80211_F_SHSLOT;
1205 vap->iv_flags &= ~IEEE80211_F_SHSLOT;
1207 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1208 "%s: called; onoff=%d\n", __func__, onoff);
1209 /* schedule the deferred slot flag update and update */
1210 ieee80211_runtask(ic, &vap->iv_slot_task);
1214 * Update the VAP short /long / barker preamble state and
1215 * update beacon state if needed.
1217 * For now it simply copies the global flags into the per-vap
1218 * flags and schedules the callback. Later this will support
1219 * both global and per-VAP flags, especially useful for
1220 * and STA+STA multi-channel operation (eg p2p).
1223 ieee80211_vap_update_preamble(struct ieee80211vap *vap)
1225 struct ieee80211com *ic = vap->iv_ic;
1229 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1230 "%s: called\n", __func__);
1231 /* schedule the deferred slot flag update and update */
1232 ieee80211_runtask(ic, &vap->iv_preamble_task);
1236 * Update the VAP 11g protection mode and update beacon state
1240 ieee80211_vap_update_erp_protmode(struct ieee80211vap *vap)
1242 struct ieee80211com *ic = vap->iv_ic;
1246 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1247 "%s: called\n", __func__);
1248 /* schedule the deferred slot flag update and update */
1249 ieee80211_runtask(ic, &vap->iv_erp_protmode_task);
1253 * Update the VAP 11n protection mode and update beacon state
1257 ieee80211_vap_update_ht_protmode(struct ieee80211vap *vap)
1259 struct ieee80211com *ic = vap->iv_ic;
1263 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1264 "%s: called\n", __func__);
1265 /* schedule the deferred protmode update */
1266 ieee80211_runtask(ic, &vap->iv_ht_protmode_task);
1270 * Check if the specified rate set supports ERP.
1271 * NB: the rate set is assumed to be sorted.
1274 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
1276 static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
1279 if (rs->rs_nrates < nitems(rates))
1281 for (i = 0; i < nitems(rates); i++) {
1282 for (j = 0; j < rs->rs_nrates; j++) {
1283 int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
1297 * Mark the basic rates for the rate table based on the
1298 * operating mode. For real 11g we mark all the 11b rates
1299 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
1300 * 11b rates. There's also a pseudo 11a-mode used to mark only
1301 * the basic OFDM rates.
1304 setbasicrates(struct ieee80211_rateset *rs,
1305 enum ieee80211_phymode mode, int add)
1307 static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
1308 [IEEE80211_MODE_11A] = { 3, { 12, 24, 48 } },
1309 [IEEE80211_MODE_11B] = { 2, { 2, 4 } },
1311 [IEEE80211_MODE_11G] = { 4, { 2, 4, 11, 22 } },
1312 [IEEE80211_MODE_TURBO_A] = { 3, { 12, 24, 48 } },
1313 [IEEE80211_MODE_TURBO_G] = { 4, { 2, 4, 11, 22 } },
1314 [IEEE80211_MODE_STURBO_A] = { 3, { 12, 24, 48 } },
1315 [IEEE80211_MODE_HALF] = { 3, { 6, 12, 24 } },
1316 [IEEE80211_MODE_QUARTER] = { 3, { 3, 6, 12 } },
1317 [IEEE80211_MODE_11NA] = { 3, { 12, 24, 48 } },
1319 [IEEE80211_MODE_11NG] = { 4, { 2, 4, 11, 22 } },
1321 [IEEE80211_MODE_VHT_2GHZ] = { 4, { 2, 4, 11, 22 } },
1322 [IEEE80211_MODE_VHT_5GHZ] = { 3, { 12, 24, 48 } },
1326 for (i = 0; i < rs->rs_nrates; i++) {
1328 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
1329 for (j = 0; j < basic[mode].rs_nrates; j++)
1330 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
1331 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
1338 * Set the basic rates in a rate set.
1341 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
1342 enum ieee80211_phymode mode)
1344 setbasicrates(rs, mode, 0);
1348 * Add basic rates to a rate set.
1351 ieee80211_addbasicrates(struct ieee80211_rateset *rs,
1352 enum ieee80211_phymode mode)
1354 setbasicrates(rs, mode, 1);
1358 * WME protocol support.
1360 * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
1361 * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
1362 * Draft 2.0 Test Plan (Appendix D).
1364 * Static/Dynamic Turbo mode settings come from Atheros.
1366 typedef struct phyParamType {
1374 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
1375 [IEEE80211_MODE_AUTO] = { 3, 4, 6, 0, 0 },
1376 [IEEE80211_MODE_11A] = { 3, 4, 6, 0, 0 },
1377 [IEEE80211_MODE_11B] = { 3, 4, 6, 0, 0 },
1378 [IEEE80211_MODE_11G] = { 3, 4, 6, 0, 0 },
1379 [IEEE80211_MODE_FH] = { 3, 4, 6, 0, 0 },
1380 [IEEE80211_MODE_TURBO_A]= { 2, 3, 5, 0, 0 },
1381 [IEEE80211_MODE_TURBO_G]= { 2, 3, 5, 0, 0 },
1382 [IEEE80211_MODE_STURBO_A]={ 2, 3, 5, 0, 0 },
1383 [IEEE80211_MODE_HALF] = { 3, 4, 6, 0, 0 },
1384 [IEEE80211_MODE_QUARTER]= { 3, 4, 6, 0, 0 },
1385 [IEEE80211_MODE_11NA] = { 3, 4, 6, 0, 0 },
1386 [IEEE80211_MODE_11NG] = { 3, 4, 6, 0, 0 },
1387 [IEEE80211_MODE_VHT_2GHZ] = { 3, 4, 6, 0, 0 },
1388 [IEEE80211_MODE_VHT_5GHZ] = { 3, 4, 6, 0, 0 },
1390 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
1391 [IEEE80211_MODE_AUTO] = { 7, 4, 10, 0, 0 },
1392 [IEEE80211_MODE_11A] = { 7, 4, 10, 0, 0 },
1393 [IEEE80211_MODE_11B] = { 7, 4, 10, 0, 0 },
1394 [IEEE80211_MODE_11G] = { 7, 4, 10, 0, 0 },
1395 [IEEE80211_MODE_FH] = { 7, 4, 10, 0, 0 },
1396 [IEEE80211_MODE_TURBO_A]= { 7, 3, 10, 0, 0 },
1397 [IEEE80211_MODE_TURBO_G]= { 7, 3, 10, 0, 0 },
1398 [IEEE80211_MODE_STURBO_A]={ 7, 3, 10, 0, 0 },
1399 [IEEE80211_MODE_HALF] = { 7, 4, 10, 0, 0 },
1400 [IEEE80211_MODE_QUARTER]= { 7, 4, 10, 0, 0 },
1401 [IEEE80211_MODE_11NA] = { 7, 4, 10, 0, 0 },
1402 [IEEE80211_MODE_11NG] = { 7, 4, 10, 0, 0 },
1403 [IEEE80211_MODE_VHT_2GHZ] = { 7, 4, 10, 0, 0 },
1404 [IEEE80211_MODE_VHT_5GHZ] = { 7, 4, 10, 0, 0 },
1406 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
1407 [IEEE80211_MODE_AUTO] = { 1, 3, 4, 94, 0 },
1408 [IEEE80211_MODE_11A] = { 1, 3, 4, 94, 0 },
1409 [IEEE80211_MODE_11B] = { 1, 3, 4, 188, 0 },
1410 [IEEE80211_MODE_11G] = { 1, 3, 4, 94, 0 },
1411 [IEEE80211_MODE_FH] = { 1, 3, 4, 188, 0 },
1412 [IEEE80211_MODE_TURBO_A]= { 1, 2, 3, 94, 0 },
1413 [IEEE80211_MODE_TURBO_G]= { 1, 2, 3, 94, 0 },
1414 [IEEE80211_MODE_STURBO_A]={ 1, 2, 3, 94, 0 },
1415 [IEEE80211_MODE_HALF] = { 1, 3, 4, 94, 0 },
1416 [IEEE80211_MODE_QUARTER]= { 1, 3, 4, 94, 0 },
1417 [IEEE80211_MODE_11NA] = { 1, 3, 4, 94, 0 },
1418 [IEEE80211_MODE_11NG] = { 1, 3, 4, 94, 0 },
1419 [IEEE80211_MODE_VHT_2GHZ] = { 1, 3, 4, 94, 0 },
1420 [IEEE80211_MODE_VHT_5GHZ] = { 1, 3, 4, 94, 0 },
1422 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
1423 [IEEE80211_MODE_AUTO] = { 1, 2, 3, 47, 0 },
1424 [IEEE80211_MODE_11A] = { 1, 2, 3, 47, 0 },
1425 [IEEE80211_MODE_11B] = { 1, 2, 3, 102, 0 },
1426 [IEEE80211_MODE_11G] = { 1, 2, 3, 47, 0 },
1427 [IEEE80211_MODE_FH] = { 1, 2, 3, 102, 0 },
1428 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
1429 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
1430 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
1431 [IEEE80211_MODE_HALF] = { 1, 2, 3, 47, 0 },
1432 [IEEE80211_MODE_QUARTER]= { 1, 2, 3, 47, 0 },
1433 [IEEE80211_MODE_11NA] = { 1, 2, 3, 47, 0 },
1434 [IEEE80211_MODE_11NG] = { 1, 2, 3, 47, 0 },
1435 [IEEE80211_MODE_VHT_2GHZ] = { 1, 2, 3, 47, 0 },
1436 [IEEE80211_MODE_VHT_5GHZ] = { 1, 2, 3, 47, 0 },
1439 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
1440 [IEEE80211_MODE_AUTO] = { 3, 4, 10, 0, 0 },
1441 [IEEE80211_MODE_11A] = { 3, 4, 10, 0, 0 },
1442 [IEEE80211_MODE_11B] = { 3, 4, 10, 0, 0 },
1443 [IEEE80211_MODE_11G] = { 3, 4, 10, 0, 0 },
1444 [IEEE80211_MODE_FH] = { 3, 4, 10, 0, 0 },
1445 [IEEE80211_MODE_TURBO_A]= { 2, 3, 10, 0, 0 },
1446 [IEEE80211_MODE_TURBO_G]= { 2, 3, 10, 0, 0 },
1447 [IEEE80211_MODE_STURBO_A]={ 2, 3, 10, 0, 0 },
1448 [IEEE80211_MODE_HALF] = { 3, 4, 10, 0, 0 },
1449 [IEEE80211_MODE_QUARTER]= { 3, 4, 10, 0, 0 },
1450 [IEEE80211_MODE_11NA] = { 3, 4, 10, 0, 0 },
1451 [IEEE80211_MODE_11NG] = { 3, 4, 10, 0, 0 },
1453 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
1454 [IEEE80211_MODE_AUTO] = { 2, 3, 4, 94, 0 },
1455 [IEEE80211_MODE_11A] = { 2, 3, 4, 94, 0 },
1456 [IEEE80211_MODE_11B] = { 2, 3, 4, 188, 0 },
1457 [IEEE80211_MODE_11G] = { 2, 3, 4, 94, 0 },
1458 [IEEE80211_MODE_FH] = { 2, 3, 4, 188, 0 },
1459 [IEEE80211_MODE_TURBO_A]= { 2, 2, 3, 94, 0 },
1460 [IEEE80211_MODE_TURBO_G]= { 2, 2, 3, 94, 0 },
1461 [IEEE80211_MODE_STURBO_A]={ 2, 2, 3, 94, 0 },
1462 [IEEE80211_MODE_HALF] = { 2, 3, 4, 94, 0 },
1463 [IEEE80211_MODE_QUARTER]= { 2, 3, 4, 94, 0 },
1464 [IEEE80211_MODE_11NA] = { 2, 3, 4, 94, 0 },
1465 [IEEE80211_MODE_11NG] = { 2, 3, 4, 94, 0 },
1467 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
1468 [IEEE80211_MODE_AUTO] = { 2, 2, 3, 47, 0 },
1469 [IEEE80211_MODE_11A] = { 2, 2, 3, 47, 0 },
1470 [IEEE80211_MODE_11B] = { 2, 2, 3, 102, 0 },
1471 [IEEE80211_MODE_11G] = { 2, 2, 3, 47, 0 },
1472 [IEEE80211_MODE_FH] = { 2, 2, 3, 102, 0 },
1473 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
1474 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
1475 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
1476 [IEEE80211_MODE_HALF] = { 2, 2, 3, 47, 0 },
1477 [IEEE80211_MODE_QUARTER]= { 2, 2, 3, 47, 0 },
1478 [IEEE80211_MODE_11NA] = { 2, 2, 3, 47, 0 },
1479 [IEEE80211_MODE_11NG] = { 2, 2, 3, 47, 0 },
1483 _setifsparams(struct wmeParams *wmep, const paramType *phy)
1485 wmep->wmep_aifsn = phy->aifsn;
1486 wmep->wmep_logcwmin = phy->logcwmin;
1487 wmep->wmep_logcwmax = phy->logcwmax;
1488 wmep->wmep_txopLimit = phy->txopLimit;
1492 setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
1493 struct wmeParams *wmep, const paramType *phy)
1495 wmep->wmep_acm = phy->acm;
1496 _setifsparams(wmep, phy);
1498 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1499 "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
1500 ieee80211_wme_acnames[ac], type,
1501 wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
1502 wmep->wmep_logcwmax, wmep->wmep_txopLimit);
1506 ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
1508 struct ieee80211com *ic = vap->iv_ic;
1509 struct ieee80211_wme_state *wme = &ic->ic_wme;
1510 const paramType *pPhyParam, *pBssPhyParam;
1511 struct wmeParams *wmep;
1512 enum ieee80211_phymode mode;
1515 IEEE80211_LOCK_ASSERT(ic);
1517 if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
1521 * Clear the wme cap_info field so a qoscount from a previous
1522 * vap doesn't confuse later code which only parses the beacon
1523 * field and updates hardware when said field changes.
1524 * Otherwise the hardware is programmed with defaults, not what
1525 * the beacon actually announces.
1527 * Note that we can't ever have 0xff as an actual value;
1528 * the only valid values are 0..15.
1530 wme->wme_wmeChanParams.cap_info = 0xfe;
1533 * Select mode; we can be called early in which case we
1534 * always use auto mode. We know we'll be called when
1535 * entering the RUN state with bsschan setup properly
1536 * so state will eventually get set correctly
1538 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1539 mode = ieee80211_chan2mode(ic->ic_bsschan);
1541 mode = IEEE80211_MODE_AUTO;
1542 for (i = 0; i < WME_NUM_AC; i++) {
1545 pPhyParam = &phyParamForAC_BK[mode];
1546 pBssPhyParam = &phyParamForAC_BK[mode];
1549 pPhyParam = &phyParamForAC_VI[mode];
1550 pBssPhyParam = &bssPhyParamForAC_VI[mode];
1553 pPhyParam = &phyParamForAC_VO[mode];
1554 pBssPhyParam = &bssPhyParamForAC_VO[mode];
1558 pPhyParam = &phyParamForAC_BE[mode];
1559 pBssPhyParam = &bssPhyParamForAC_BE[mode];
1562 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1563 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1564 setwmeparams(vap, "chan", i, wmep, pPhyParam);
1566 setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
1568 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1569 setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
1571 /* NB: check ic_bss to avoid NULL deref on initial attach */
1572 if (vap->iv_bss != NULL) {
1574 * Calculate aggressive mode switching threshold based
1575 * on beacon interval. This doesn't need locking since
1576 * we're only called before entering the RUN state at
1577 * which point we start sending beacon frames.
1579 wme->wme_hipri_switch_thresh =
1580 (HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
1581 wme->wme_flags &= ~WME_F_AGGRMODE;
1582 ieee80211_wme_updateparams(vap);
1587 ieee80211_wme_initparams(struct ieee80211vap *vap)
1589 struct ieee80211com *ic = vap->iv_ic;
1592 ieee80211_wme_initparams_locked(vap);
1593 IEEE80211_UNLOCK(ic);
1597 * Update WME parameters for ourself and the BSS.
1600 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
1602 static const paramType aggrParam[IEEE80211_MODE_MAX] = {
1603 [IEEE80211_MODE_AUTO] = { 2, 4, 10, 64, 0 },
1604 [IEEE80211_MODE_11A] = { 2, 4, 10, 64, 0 },
1605 [IEEE80211_MODE_11B] = { 2, 5, 10, 64, 0 },
1606 [IEEE80211_MODE_11G] = { 2, 4, 10, 64, 0 },
1607 [IEEE80211_MODE_FH] = { 2, 5, 10, 64, 0 },
1608 [IEEE80211_MODE_TURBO_A] = { 1, 3, 10, 64, 0 },
1609 [IEEE80211_MODE_TURBO_G] = { 1, 3, 10, 64, 0 },
1610 [IEEE80211_MODE_STURBO_A] = { 1, 3, 10, 64, 0 },
1611 [IEEE80211_MODE_HALF] = { 2, 4, 10, 64, 0 },
1612 [IEEE80211_MODE_QUARTER] = { 2, 4, 10, 64, 0 },
1613 [IEEE80211_MODE_11NA] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1614 [IEEE80211_MODE_11NG] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1615 [IEEE80211_MODE_VHT_2GHZ] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1616 [IEEE80211_MODE_VHT_5GHZ] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1618 struct ieee80211com *ic = vap->iv_ic;
1619 struct ieee80211_wme_state *wme = &ic->ic_wme;
1620 const struct wmeParams *wmep;
1621 struct wmeParams *chanp, *bssp;
1622 enum ieee80211_phymode mode;
1624 int do_aggrmode = 0;
1627 * Set up the channel access parameters for the physical
1628 * device. First populate the configured settings.
1630 for (i = 0; i < WME_NUM_AC; i++) {
1631 chanp = &wme->wme_chanParams.cap_wmeParams[i];
1632 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1633 chanp->wmep_aifsn = wmep->wmep_aifsn;
1634 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1635 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1636 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1638 chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
1639 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1640 chanp->wmep_aifsn = wmep->wmep_aifsn;
1641 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1642 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1643 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1647 * Select mode; we can be called early in which case we
1648 * always use auto mode. We know we'll be called when
1649 * entering the RUN state with bsschan setup properly
1650 * so state will eventually get set correctly
1652 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1653 mode = ieee80211_chan2mode(ic->ic_bsschan);
1655 mode = IEEE80211_MODE_AUTO;
1658 * This implements aggressive mode as found in certain
1659 * vendors' AP's. When there is significant high
1660 * priority (VI/VO) traffic in the BSS throttle back BE
1661 * traffic by using conservative parameters. Otherwise
1662 * BE uses aggressive params to optimize performance of
1663 * legacy/non-QoS traffic.
1666 /* Hostap? Only if aggressive mode is enabled */
1667 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1668 (wme->wme_flags & WME_F_AGGRMODE) != 0)
1672 * Station? Only if we're in a non-QoS BSS.
1674 else if ((vap->iv_opmode == IEEE80211_M_STA &&
1675 (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1679 * IBSS? Only if we we have WME enabled.
1681 else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1682 (vap->iv_flags & IEEE80211_F_WME))
1686 * If WME is disabled on this VAP, default to aggressive mode
1687 * regardless of the configuration.
1689 if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1697 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1698 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1700 chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1701 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1702 aggrParam[mode].logcwmin;
1703 chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1704 aggrParam[mode].logcwmax;
1705 chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1706 (vap->iv_flags & IEEE80211_F_BURST) ?
1707 aggrParam[mode].txopLimit : 0;
1708 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1709 "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1710 "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1711 chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1712 chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1716 * Change the contention window based on the number of associated
1717 * stations. If the number of associated stations is 1 and
1718 * aggressive mode is enabled, lower the contention window even
1721 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1722 vap->iv_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1723 static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1724 [IEEE80211_MODE_AUTO] = 3,
1725 [IEEE80211_MODE_11A] = 3,
1726 [IEEE80211_MODE_11B] = 4,
1727 [IEEE80211_MODE_11G] = 3,
1728 [IEEE80211_MODE_FH] = 4,
1729 [IEEE80211_MODE_TURBO_A] = 3,
1730 [IEEE80211_MODE_TURBO_G] = 3,
1731 [IEEE80211_MODE_STURBO_A] = 3,
1732 [IEEE80211_MODE_HALF] = 3,
1733 [IEEE80211_MODE_QUARTER] = 3,
1734 [IEEE80211_MODE_11NA] = 3,
1735 [IEEE80211_MODE_11NG] = 3,
1736 [IEEE80211_MODE_VHT_2GHZ] = 3,
1737 [IEEE80211_MODE_VHT_5GHZ] = 3,
1739 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1740 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1742 chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1743 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1744 "update %s (chan+bss) logcwmin %u\n",
1745 ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1748 /* schedule the deferred WME update */
1749 ieee80211_runtask(ic, &vap->iv_wme_task);
1751 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1752 "%s: WME params updated, cap_info 0x%x\n", __func__,
1753 vap->iv_opmode == IEEE80211_M_STA ?
1754 wme->wme_wmeChanParams.cap_info :
1755 wme->wme_bssChanParams.cap_info);
1759 ieee80211_wme_updateparams(struct ieee80211vap *vap)
1761 struct ieee80211com *ic = vap->iv_ic;
1763 if (ic->ic_caps & IEEE80211_C_WME) {
1765 ieee80211_wme_updateparams_locked(vap);
1766 IEEE80211_UNLOCK(ic);
1771 * Fetch the WME parameters for the given VAP.
1773 * When net80211 grows p2p, etc support, this may return different
1774 * parameters for each VAP.
1777 ieee80211_wme_vap_getparams(struct ieee80211vap *vap, struct chanAccParams *wp)
1780 memcpy(wp, &vap->iv_ic->ic_wme.wme_chanParams, sizeof(*wp));
1784 * For NICs which only support one set of WME paramaters (ie, softmac NICs)
1785 * there may be different VAP WME parameters but only one is "active".
1786 * This returns the "NIC" WME parameters for the currently active
1790 ieee80211_wme_ic_getparams(struct ieee80211com *ic, struct chanAccParams *wp)
1793 memcpy(wp, &ic->ic_wme.wme_chanParams, sizeof(*wp));
1797 * Return whether to use QoS on a given WME queue.
1799 * This is intended to be called from the transmit path of softmac drivers
1800 * which are setting NoAck bits in transmit descriptors.
1802 * Ideally this would be set in some transmit field before the packet is
1803 * queued to the driver but net80211 isn't quite there yet.
1806 ieee80211_wme_vap_ac_is_noack(struct ieee80211vap *vap, int ac)
1808 /* Bounds/sanity check */
1809 if (ac < 0 || ac >= WME_NUM_AC)
1812 /* Again, there's only one global context for now */
1813 return (!! vap->iv_ic->ic_wme.wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy);
1817 parent_updown(void *arg, int npending)
1819 struct ieee80211com *ic = arg;
1825 update_mcast(void *arg, int npending)
1827 struct ieee80211com *ic = arg;
1829 ic->ic_update_mcast(ic);
1833 update_promisc(void *arg, int npending)
1835 struct ieee80211com *ic = arg;
1837 ic->ic_update_promisc(ic);
1841 update_channel(void *arg, int npending)
1843 struct ieee80211com *ic = arg;
1845 ic->ic_set_channel(ic);
1846 ieee80211_radiotap_chan_change(ic);
1850 update_chw(void *arg, int npending)
1852 struct ieee80211com *ic = arg;
1855 * XXX should we defer the channel width _config_ update until now?
1857 ic->ic_update_chw(ic);
1861 * Deferred WME parameter and beacon update.
1863 * In preparation for per-VAP WME configuration, call the VAP
1864 * method if the VAP requires it. Otherwise, just call the
1865 * older global method. There isn't a per-VAP WME configuration
1866 * just yet so for now just use the global configuration.
1869 vap_update_wme(void *arg, int npending)
1871 struct ieee80211vap *vap = arg;
1872 struct ieee80211com *ic = vap->iv_ic;
1873 struct ieee80211_wme_state *wme = &ic->ic_wme;
1876 if (vap->iv_wme_update != NULL)
1877 vap->iv_wme_update(vap,
1878 ic->ic_wme.wme_chanParams.cap_wmeParams);
1880 ic->ic_wme.wme_update(ic);
1884 * Arrange for the beacon update.
1886 * XXX what about MBSS, WDS?
1888 if (vap->iv_opmode == IEEE80211_M_HOSTAP
1889 || vap->iv_opmode == IEEE80211_M_IBSS) {
1891 * Arrange for a beacon update and bump the parameter
1892 * set number so associated stations load the new values.
1894 wme->wme_bssChanParams.cap_info =
1895 (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1896 ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1898 IEEE80211_UNLOCK(ic);
1902 restart_vaps(void *arg, int npending)
1904 struct ieee80211com *ic = arg;
1906 ieee80211_suspend_all(ic);
1907 ieee80211_resume_all(ic);
1911 * Block until the parent is in a known state. This is
1912 * used after any operations that dispatch a task (e.g.
1913 * to auto-configure the parent device up/down).
1916 ieee80211_waitfor_parent(struct ieee80211com *ic)
1918 taskqueue_block(ic->ic_tq);
1919 ieee80211_draintask(ic, &ic->ic_parent_task);
1920 ieee80211_draintask(ic, &ic->ic_mcast_task);
1921 ieee80211_draintask(ic, &ic->ic_promisc_task);
1922 ieee80211_draintask(ic, &ic->ic_chan_task);
1923 ieee80211_draintask(ic, &ic->ic_bmiss_task);
1924 ieee80211_draintask(ic, &ic->ic_chw_task);
1925 taskqueue_unblock(ic->ic_tq);
1929 * Check to see whether the current channel needs reset.
1931 * Some devices don't handle being given an invalid channel
1932 * in their operating mode very well (eg wpi(4) will throw a
1933 * firmware exception.)
1935 * Return 0 if we're ok, 1 if the channel needs to be reset.
1937 * See PR kern/202502.
1940 ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
1942 struct ieee80211com *ic = vap->iv_ic;
1944 if ((vap->iv_opmode == IEEE80211_M_IBSS &&
1945 IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
1946 (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1947 IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
1953 * Reset the curchan to a known good state.
1956 ieee80211_start_reset_chan(struct ieee80211vap *vap)
1958 struct ieee80211com *ic = vap->iv_ic;
1960 ic->ic_curchan = &ic->ic_channels[0];
1964 * Start a vap running. If this is the first vap to be
1965 * set running on the underlying device then we
1966 * automatically bring the device up.
1969 ieee80211_start_locked(struct ieee80211vap *vap)
1971 struct ifnet *ifp = vap->iv_ifp;
1972 struct ieee80211com *ic = vap->iv_ic;
1974 IEEE80211_LOCK_ASSERT(ic);
1976 IEEE80211_DPRINTF(vap,
1977 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1978 "start running, %d vaps running\n", ic->ic_nrunning);
1980 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1982 * Mark us running. Note that it's ok to do this first;
1983 * if we need to bring the parent device up we defer that
1984 * to avoid dropping the com lock. We expect the device
1985 * to respond to being marked up by calling back into us
1986 * through ieee80211_start_all at which point we'll come
1987 * back in here and complete the work.
1989 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1990 ieee80211_notify_ifnet_change(vap);
1993 * We are not running; if this we are the first vap
1994 * to be brought up auto-up the parent if necessary.
1996 if (ic->ic_nrunning++ == 0) {
1997 /* reset the channel to a known good channel */
1998 if (ieee80211_start_check_reset_chan(vap))
1999 ieee80211_start_reset_chan(vap);
2001 IEEE80211_DPRINTF(vap,
2002 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2003 "%s: up parent %s\n", __func__, ic->ic_name);
2004 ieee80211_runtask(ic, &ic->ic_parent_task);
2009 * If the parent is up and running, then kick the
2010 * 802.11 state machine as appropriate.
2012 if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
2013 if (vap->iv_opmode == IEEE80211_M_STA) {
2015 /* XXX bypasses scan too easily; disable for now */
2017 * Try to be intelligent about clocking the state
2018 * machine. If we're currently in RUN state then
2019 * we should be able to apply any new state/parameters
2020 * simply by re-associating. Otherwise we need to
2021 * re-scan to select an appropriate ap.
2023 if (vap->iv_state >= IEEE80211_S_RUN)
2024 ieee80211_new_state_locked(vap,
2025 IEEE80211_S_ASSOC, 1);
2028 ieee80211_new_state_locked(vap,
2029 IEEE80211_S_SCAN, 0);
2032 * For monitor+wds mode there's nothing to do but
2033 * start running. Otherwise if this is the first
2034 * vap to be brought up, start a scan which may be
2035 * preempted if the station is locked to a particular
2038 vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
2039 if (vap->iv_opmode == IEEE80211_M_MONITOR ||
2040 vap->iv_opmode == IEEE80211_M_WDS)
2041 ieee80211_new_state_locked(vap,
2042 IEEE80211_S_RUN, -1);
2044 ieee80211_new_state_locked(vap,
2045 IEEE80211_S_SCAN, 0);
2051 * Start a single vap.
2054 ieee80211_init(void *arg)
2056 struct ieee80211vap *vap = arg;
2058 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2061 IEEE80211_LOCK(vap->iv_ic);
2062 ieee80211_start_locked(vap);
2063 IEEE80211_UNLOCK(vap->iv_ic);
2067 * Start all runnable vap's on a device.
2070 ieee80211_start_all(struct ieee80211com *ic)
2072 struct ieee80211vap *vap;
2075 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2076 struct ifnet *ifp = vap->iv_ifp;
2077 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
2078 ieee80211_start_locked(vap);
2080 IEEE80211_UNLOCK(ic);
2084 * Stop a vap. We force it down using the state machine
2085 * then mark it's ifnet not running. If this is the last
2086 * vap running on the underlying device then we close it
2087 * too to insure it will be properly initialized when the
2088 * next vap is brought up.
2091 ieee80211_stop_locked(struct ieee80211vap *vap)
2093 struct ieee80211com *ic = vap->iv_ic;
2094 struct ifnet *ifp = vap->iv_ifp;
2096 IEEE80211_LOCK_ASSERT(ic);
2098 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2099 "stop running, %d vaps running\n", ic->ic_nrunning);
2101 ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
2102 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2103 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; /* mark us stopped */
2104 ieee80211_notify_ifnet_change(vap);
2105 if (--ic->ic_nrunning == 0) {
2106 IEEE80211_DPRINTF(vap,
2107 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2108 "down parent %s\n", ic->ic_name);
2109 ieee80211_runtask(ic, &ic->ic_parent_task);
2115 ieee80211_stop(struct ieee80211vap *vap)
2117 struct ieee80211com *ic = vap->iv_ic;
2120 ieee80211_stop_locked(vap);
2121 IEEE80211_UNLOCK(ic);
2125 * Stop all vap's running on a device.
2128 ieee80211_stop_all(struct ieee80211com *ic)
2130 struct ieee80211vap *vap;
2133 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2134 struct ifnet *ifp = vap->iv_ifp;
2135 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
2136 ieee80211_stop_locked(vap);
2138 IEEE80211_UNLOCK(ic);
2140 ieee80211_waitfor_parent(ic);
2144 * Stop all vap's running on a device and arrange
2145 * for those that were running to be resumed.
2148 ieee80211_suspend_all(struct ieee80211com *ic)
2150 struct ieee80211vap *vap;
2153 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2154 struct ifnet *ifp = vap->iv_ifp;
2155 if (IFNET_IS_UP_RUNNING(ifp)) { /* NB: avoid recursion */
2156 vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
2157 ieee80211_stop_locked(vap);
2160 IEEE80211_UNLOCK(ic);
2162 ieee80211_waitfor_parent(ic);
2166 * Start all vap's marked for resume.
2169 ieee80211_resume_all(struct ieee80211com *ic)
2171 struct ieee80211vap *vap;
2174 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2175 struct ifnet *ifp = vap->iv_ifp;
2176 if (!IFNET_IS_UP_RUNNING(ifp) &&
2177 (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
2178 vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
2179 ieee80211_start_locked(vap);
2182 IEEE80211_UNLOCK(ic);
2186 * Restart all vap's running on a device.
2189 ieee80211_restart_all(struct ieee80211com *ic)
2192 * NB: do not use ieee80211_runtask here, we will
2193 * block & drain net80211 taskqueue.
2195 taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
2199 ieee80211_beacon_miss(struct ieee80211com *ic)
2202 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2203 /* Process in a taskq, the handler may reenter the driver */
2204 ieee80211_runtask(ic, &ic->ic_bmiss_task);
2206 IEEE80211_UNLOCK(ic);
2210 beacon_miss(void *arg, int npending)
2212 struct ieee80211com *ic = arg;
2213 struct ieee80211vap *vap;
2216 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2218 * We only pass events through for sta vap's in RUN+ state;
2219 * may be too restrictive but for now this saves all the
2220 * handlers duplicating these checks.
2222 if (vap->iv_opmode == IEEE80211_M_STA &&
2223 vap->iv_state >= IEEE80211_S_RUN &&
2224 vap->iv_bmiss != NULL)
2227 IEEE80211_UNLOCK(ic);
2231 beacon_swmiss(void *arg, int npending)
2233 struct ieee80211vap *vap = arg;
2234 struct ieee80211com *ic = vap->iv_ic;
2237 if (vap->iv_state >= IEEE80211_S_RUN) {
2238 /* XXX Call multiple times if npending > zero? */
2241 IEEE80211_UNLOCK(ic);
2245 * Software beacon miss handling. Check if any beacons
2246 * were received in the last period. If not post a
2247 * beacon miss; otherwise reset the counter.
2250 ieee80211_swbmiss(void *arg)
2252 struct ieee80211vap *vap = arg;
2253 struct ieee80211com *ic = vap->iv_ic;
2255 IEEE80211_LOCK_ASSERT(ic);
2257 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
2258 ("wrong state %d", vap->iv_state));
2260 if (ic->ic_flags & IEEE80211_F_SCAN) {
2262 * If scanning just ignore and reset state. If we get a
2263 * bmiss after coming out of scan because we haven't had
2264 * time to receive a beacon then we should probe the AP
2265 * before posting a real bmiss (unless iv_bmiss_max has
2266 * been artifiically lowered). A cleaner solution might
2267 * be to disable the timer on scan start/end but to handle
2268 * case of multiple sta vap's we'd need to disable the
2269 * timers of all affected vap's.
2271 vap->iv_swbmiss_count = 0;
2272 } else if (vap->iv_swbmiss_count == 0) {
2273 if (vap->iv_bmiss != NULL)
2274 ieee80211_runtask(ic, &vap->iv_swbmiss_task);
2276 vap->iv_swbmiss_count = 0;
2277 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
2278 ieee80211_swbmiss, vap);
2282 * Start an 802.11h channel switch. We record the parameters,
2283 * mark the operation pending, notify each vap through the
2284 * beacon update mechanism so it can update the beacon frame
2285 * contents, and then switch vap's to CSA state to block outbound
2286 * traffic. Devices that handle CSA directly can use the state
2287 * switch to do the right thing so long as they call
2288 * ieee80211_csa_completeswitch when it's time to complete the
2289 * channel change. Devices that depend on the net80211 layer can
2290 * use ieee80211_beacon_update to handle the countdown and the
2294 ieee80211_csa_startswitch(struct ieee80211com *ic,
2295 struct ieee80211_channel *c, int mode, int count)
2297 struct ieee80211vap *vap;
2299 IEEE80211_LOCK_ASSERT(ic);
2301 ic->ic_csa_newchan = c;
2302 ic->ic_csa_mode = mode;
2303 ic->ic_csa_count = count;
2304 ic->ic_flags |= IEEE80211_F_CSAPENDING;
2305 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2306 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2307 vap->iv_opmode == IEEE80211_M_IBSS ||
2308 vap->iv_opmode == IEEE80211_M_MBSS)
2309 ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
2310 /* switch to CSA state to block outbound traffic */
2311 if (vap->iv_state == IEEE80211_S_RUN)
2312 ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
2314 ieee80211_notify_csa(ic, c, mode, count);
2318 * Complete the channel switch by transitioning all CSA VAPs to RUN.
2319 * This is called by both the completion and cancellation functions
2320 * so each VAP is placed back in the RUN state and can thus transmit.
2323 csa_completeswitch(struct ieee80211com *ic)
2325 struct ieee80211vap *vap;
2327 ic->ic_csa_newchan = NULL;
2328 ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
2330 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2331 if (vap->iv_state == IEEE80211_S_CSA)
2332 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
2336 * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
2337 * We clear state and move all vap's in CSA state to RUN state
2338 * so they can again transmit.
2340 * Although this may not be completely correct, update the BSS channel
2341 * for each VAP to the newly configured channel. The setcurchan sets
2342 * the current operating channel for the interface (so the radio does
2343 * switch over) but the VAP BSS isn't updated, leading to incorrectly
2344 * reported information via ioctl.
2347 ieee80211_csa_completeswitch(struct ieee80211com *ic)
2349 struct ieee80211vap *vap;
2351 IEEE80211_LOCK_ASSERT(ic);
2353 KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
2355 ieee80211_setcurchan(ic, ic->ic_csa_newchan);
2356 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2357 if (vap->iv_state == IEEE80211_S_CSA)
2358 vap->iv_bss->ni_chan = ic->ic_curchan;
2360 csa_completeswitch(ic);
2364 * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
2365 * We clear state and move all vap's in CSA state to RUN state
2366 * so they can again transmit.
2369 ieee80211_csa_cancelswitch(struct ieee80211com *ic)
2371 IEEE80211_LOCK_ASSERT(ic);
2373 csa_completeswitch(ic);
2377 * Complete a DFS CAC started by ieee80211_dfs_cac_start.
2378 * We clear state and move all vap's in CAC state to RUN state.
2381 ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
2383 struct ieee80211com *ic = vap0->iv_ic;
2384 struct ieee80211vap *vap;
2388 * Complete CAC state change for lead vap first; then
2389 * clock all the other vap's waiting.
2391 KASSERT(vap0->iv_state == IEEE80211_S_CAC,
2392 ("wrong state %d", vap0->iv_state));
2393 ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
2395 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2396 if (vap->iv_state == IEEE80211_S_CAC && vap != vap0)
2397 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
2398 IEEE80211_UNLOCK(ic);
2402 * Force all vap's other than the specified vap to the INIT state
2403 * and mark them as waiting for a scan to complete. These vaps
2404 * will be brought up when the scan completes and the scanning vap
2405 * reaches RUN state by wakeupwaiting.
2408 markwaiting(struct ieee80211vap *vap0)
2410 struct ieee80211com *ic = vap0->iv_ic;
2411 struct ieee80211vap *vap;
2413 IEEE80211_LOCK_ASSERT(ic);
2416 * A vap list entry can not disappear since we are running on the
2417 * taskqueue and a vap destroy will queue and drain another state
2420 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2423 if (vap->iv_state != IEEE80211_S_INIT) {
2424 /* NB: iv_newstate may drop the lock */
2425 vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
2426 IEEE80211_LOCK_ASSERT(ic);
2427 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2433 * Wakeup all vap's waiting for a scan to complete. This is the
2434 * companion to markwaiting (above) and is used to coordinate
2435 * multiple vaps scanning.
2436 * This is called from the state taskqueue.
2439 wakeupwaiting(struct ieee80211vap *vap0)
2441 struct ieee80211com *ic = vap0->iv_ic;
2442 struct ieee80211vap *vap;
2444 IEEE80211_LOCK_ASSERT(ic);
2447 * A vap list entry can not disappear since we are running on the
2448 * taskqueue and a vap destroy will queue and drain another state
2451 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2454 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
2455 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2456 /* NB: sta's cannot go INIT->RUN */
2457 /* NB: iv_newstate may drop the lock */
2458 vap->iv_newstate(vap,
2459 vap->iv_opmode == IEEE80211_M_STA ?
2460 IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
2461 IEEE80211_LOCK_ASSERT(ic);
2467 * Handle post state change work common to all operating modes.
2470 ieee80211_newstate_cb(void *xvap, int npending)
2472 struct ieee80211vap *vap = xvap;
2473 struct ieee80211com *ic = vap->iv_ic;
2474 enum ieee80211_state nstate, ostate;
2478 nstate = vap->iv_nstate;
2479 arg = vap->iv_nstate_arg;
2481 if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
2483 * We have been requested to drop back to the INIT before
2484 * proceeding to the new state.
2486 /* Deny any state changes while we are here. */
2487 vap->iv_nstate = IEEE80211_S_INIT;
2488 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2489 "%s: %s -> %s arg %d\n", __func__,
2490 ieee80211_state_name[vap->iv_state],
2491 ieee80211_state_name[vap->iv_nstate], arg);
2492 vap->iv_newstate(vap, vap->iv_nstate, 0);
2493 IEEE80211_LOCK_ASSERT(ic);
2494 vap->iv_flags_ext &= ~(IEEE80211_FEXT_REINIT |
2495 IEEE80211_FEXT_STATEWAIT);
2496 /* enqueue new state transition after cancel_scan() task */
2497 ieee80211_new_state_locked(vap, nstate, arg);
2501 ostate = vap->iv_state;
2502 if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
2504 * SCAN was forced; e.g. on beacon miss. Force other running
2505 * vap's to INIT state and mark them as waiting for the scan to
2506 * complete. This insures they don't interfere with our
2507 * scanning. Since we are single threaded the vaps can not
2508 * transition again while we are executing.
2510 * XXX not always right, assumes ap follows sta
2514 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2515 "%s: %s -> %s arg %d\n", __func__,
2516 ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
2518 rc = vap->iv_newstate(vap, nstate, arg);
2519 IEEE80211_LOCK_ASSERT(ic);
2520 vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
2522 /* State transition failed */
2523 KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
2524 KASSERT(nstate != IEEE80211_S_INIT,
2525 ("INIT state change failed"));
2526 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2527 "%s: %s returned error %d\n", __func__,
2528 ieee80211_state_name[nstate], rc);
2532 /* No actual transition, skip post processing */
2533 if (ostate == nstate)
2536 if (nstate == IEEE80211_S_RUN) {
2538 * OACTIVE may be set on the vap if the upper layer
2539 * tried to transmit (e.g. IPv6 NDP) before we reach
2540 * RUN state. Clear it and restart xmit.
2542 * Note this can also happen as a result of SLEEP->RUN
2543 * (i.e. coming out of power save mode).
2545 vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2548 * XXX TODO Kick-start a VAP queue - this should be a method!
2551 /* bring up any vaps waiting on us */
2553 } else if (nstate == IEEE80211_S_INIT) {
2555 * Flush the scan cache if we did the last scan (XXX?)
2556 * and flush any frames on send queues from this vap.
2557 * Note the mgt q is used only for legacy drivers and
2558 * will go away shortly.
2560 ieee80211_scan_flush(vap);
2563 * XXX TODO: ic/vap queue flush
2567 IEEE80211_UNLOCK(ic);
2571 * Public interface for initiating a state machine change.
2572 * This routine single-threads the request and coordinates
2573 * the scheduling of multiple vaps for the purpose of selecting
2574 * an operating channel. Specifically the following scenarios
2576 * o only one vap can be selecting a channel so on transition to
2577 * SCAN state if another vap is already scanning then
2578 * mark the caller for later processing and return without
2579 * doing anything (XXX? expectations by caller of synchronous operation)
2580 * o only one vap can be doing CAC of a channel so on transition to
2581 * CAC state if another vap is already scanning for radar then
2582 * mark the caller for later processing and return without
2583 * doing anything (XXX? expectations by caller of synchronous operation)
2584 * o if another vap is already running when a request is made
2585 * to SCAN then an operating channel has been chosen; bypass
2586 * the scan and just join the channel
2588 * Note that the state change call is done through the iv_newstate
2589 * method pointer so any driver routine gets invoked. The driver
2590 * will normally call back into operating mode-specific
2591 * ieee80211_newstate routines (below) unless it needs to completely
2592 * bypass the state machine (e.g. because the firmware has it's
2593 * own idea how things should work). Bypassing the net80211 layer
2594 * is usually a mistake and indicates lack of proper integration
2595 * with the net80211 layer.
2598 ieee80211_new_state_locked(struct ieee80211vap *vap,
2599 enum ieee80211_state nstate, int arg)
2601 struct ieee80211com *ic = vap->iv_ic;
2602 struct ieee80211vap *vp;
2603 enum ieee80211_state ostate;
2604 int nrunning, nscanning;
2606 IEEE80211_LOCK_ASSERT(ic);
2608 if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
2609 if (vap->iv_nstate == IEEE80211_S_INIT ||
2610 ((vap->iv_state == IEEE80211_S_INIT ||
2611 (vap->iv_flags_ext & IEEE80211_FEXT_REINIT)) &&
2612 vap->iv_nstate == IEEE80211_S_SCAN &&
2613 nstate > IEEE80211_S_SCAN)) {
2615 * XXX The vap is being stopped/started,
2616 * do not allow any other state changes
2617 * until this is completed.
2619 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2620 "%s: %s -> %s (%s) transition discarded\n",
2622 ieee80211_state_name[vap->iv_state],
2623 ieee80211_state_name[nstate],
2624 ieee80211_state_name[vap->iv_nstate]);
2626 } else if (vap->iv_state != vap->iv_nstate) {
2628 /* Warn if the previous state hasn't completed. */
2629 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2630 "%s: pending %s -> %s transition lost\n", __func__,
2631 ieee80211_state_name[vap->iv_state],
2632 ieee80211_state_name[vap->iv_nstate]);
2634 /* XXX temporarily enable to identify issues */
2635 if_printf(vap->iv_ifp,
2636 "%s: pending %s -> %s transition lost\n",
2637 __func__, ieee80211_state_name[vap->iv_state],
2638 ieee80211_state_name[vap->iv_nstate]);
2643 nrunning = nscanning = 0;
2644 /* XXX can track this state instead of calculating */
2645 TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
2647 if (vp->iv_state >= IEEE80211_S_RUN)
2649 /* XXX doesn't handle bg scan */
2650 /* NB: CAC+AUTH+ASSOC treated like SCAN */
2651 else if (vp->iv_state > IEEE80211_S_INIT)
2655 ostate = vap->iv_state;
2656 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2657 "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
2658 ieee80211_state_name[ostate], ieee80211_state_name[nstate],
2659 nrunning, nscanning);
2661 case IEEE80211_S_SCAN:
2662 if (ostate == IEEE80211_S_INIT) {
2664 * INIT -> SCAN happens on initial bringup.
2666 KASSERT(!(nscanning && nrunning),
2667 ("%d scanning and %d running", nscanning, nrunning));
2670 * Someone is scanning, defer our state
2671 * change until the work has completed.
2673 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2674 "%s: defer %s -> %s\n",
2675 __func__, ieee80211_state_name[ostate],
2676 ieee80211_state_name[nstate]);
2677 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2682 * Someone is operating; just join the channel
2686 /* XXX check each opmode, adhoc? */
2687 if (vap->iv_opmode == IEEE80211_M_STA)
2688 nstate = IEEE80211_S_SCAN;
2690 nstate = IEEE80211_S_RUN;
2691 #ifdef IEEE80211_DEBUG
2692 if (nstate != IEEE80211_S_SCAN) {
2693 IEEE80211_DPRINTF(vap,
2694 IEEE80211_MSG_STATE,
2695 "%s: override, now %s -> %s\n",
2697 ieee80211_state_name[ostate],
2698 ieee80211_state_name[nstate]);
2704 case IEEE80211_S_RUN:
2705 if (vap->iv_opmode == IEEE80211_M_WDS &&
2706 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
2709 * Legacy WDS with someone else scanning; don't
2710 * go online until that completes as we should
2711 * follow the other vap to the channel they choose.
2713 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2714 "%s: defer %s -> %s (legacy WDS)\n", __func__,
2715 ieee80211_state_name[ostate],
2716 ieee80211_state_name[nstate]);
2717 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2720 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
2721 IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2722 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
2723 !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
2725 * This is a DFS channel, transition to CAC state
2726 * instead of RUN. This allows us to initiate
2727 * Channel Availability Check (CAC) as specified
2730 nstate = IEEE80211_S_CAC;
2731 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2732 "%s: override %s -> %s (DFS)\n", __func__,
2733 ieee80211_state_name[ostate],
2734 ieee80211_state_name[nstate]);
2737 case IEEE80211_S_INIT:
2738 /* cancel any scan in progress */
2739 ieee80211_cancel_scan(vap);
2740 if (ostate == IEEE80211_S_INIT ) {
2741 /* XXX don't believe this */
2742 /* INIT -> INIT. nothing to do */
2743 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2749 /* defer the state change to a thread */
2750 vap->iv_nstate = nstate;
2751 vap->iv_nstate_arg = arg;
2752 vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
2753 ieee80211_runtask(ic, &vap->iv_nstate_task);
2758 ieee80211_new_state(struct ieee80211vap *vap,
2759 enum ieee80211_state nstate, int arg)
2761 struct ieee80211com *ic = vap->iv_ic;
2765 rc = ieee80211_new_state_locked(vap, nstate, arg);
2766 IEEE80211_UNLOCK(ic);