xref: /freebsd-13-stable/sys/net80211/ieee80211_proto.c (revision a7e1fc7f620d3341549c1380f550aaafbdb45622)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Atsushi Onoe
5  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
6  * Copyright (c) 2012 IEEE
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
28  */
29 
30 #include <sys/cdefs.h>
31 /*
32  * IEEE 802.11 protocol support.
33  */
34 
35 #include "opt_inet.h"
36 #include "opt_wlan.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_media.h>
49 #include <net/ethernet.h>		/* XXX for ether_sprintf */
50 
51 #include <net80211/ieee80211_var.h>
52 #include <net80211/ieee80211_adhoc.h>
53 #include <net80211/ieee80211_sta.h>
54 #include <net80211/ieee80211_hostap.h>
55 #include <net80211/ieee80211_wds.h>
56 #ifdef IEEE80211_SUPPORT_MESH
57 #include <net80211/ieee80211_mesh.h>
58 #endif
59 #include <net80211/ieee80211_monitor.h>
60 #include <net80211/ieee80211_input.h>
61 
62 /* XXX tunables */
63 #define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
64 #define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
65 
66 const char *mgt_subtype_name[] = {
67 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
68 	"probe_req",	"probe_resp",	"timing_adv",	"reserved#7",
69 	"beacon",	"atim",		"disassoc",	"auth",
70 	"deauth",	"action",	"action_noack",	"reserved#15"
71 };
72 const char *ctl_subtype_name[] = {
73 	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
74 	"reserved#4",	"reserved#5",	"reserved#6",	"control_wrap",
75 	"bar",		"ba",		"ps_poll",	"rts",
76 	"cts",		"ack",		"cf_end",	"cf_end_ack"
77 };
78 const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
79 	"IBSS",		/* IEEE80211_M_IBSS */
80 	"STA",		/* IEEE80211_M_STA */
81 	"WDS",		/* IEEE80211_M_WDS */
82 	"AHDEMO",	/* IEEE80211_M_AHDEMO */
83 	"HOSTAP",	/* IEEE80211_M_HOSTAP */
84 	"MONITOR",	/* IEEE80211_M_MONITOR */
85 	"MBSS"		/* IEEE80211_M_MBSS */
86 };
87 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
88 	"INIT",		/* IEEE80211_S_INIT */
89 	"SCAN",		/* IEEE80211_S_SCAN */
90 	"AUTH",		/* IEEE80211_S_AUTH */
91 	"ASSOC",	/* IEEE80211_S_ASSOC */
92 	"CAC",		/* IEEE80211_S_CAC */
93 	"RUN",		/* IEEE80211_S_RUN */
94 	"CSA",		/* IEEE80211_S_CSA */
95 	"SLEEP",	/* IEEE80211_S_SLEEP */
96 };
97 const char *ieee80211_wme_acnames[] = {
98 	"WME_AC_BE",
99 	"WME_AC_BK",
100 	"WME_AC_VI",
101 	"WME_AC_VO",
102 	"WME_UPSD",
103 };
104 
105 /*
106  * Reason code descriptions were (mostly) obtained from
107  * IEEE Std 802.11-2012, pp. 442-445 Table 8-36.
108  */
109 const char *
ieee80211_reason_to_string(uint16_t reason)110 ieee80211_reason_to_string(uint16_t reason)
111 {
112 	switch (reason) {
113 	case IEEE80211_REASON_UNSPECIFIED:
114 		return ("unspecified");
115 	case IEEE80211_REASON_AUTH_EXPIRE:
116 		return ("previous authentication is expired");
117 	case IEEE80211_REASON_AUTH_LEAVE:
118 		return ("sending STA is leaving/has left IBSS or ESS");
119 	case IEEE80211_REASON_ASSOC_EXPIRE:
120 		return ("disassociated due to inactivity");
121 	case IEEE80211_REASON_ASSOC_TOOMANY:
122 		return ("too many associated STAs");
123 	case IEEE80211_REASON_NOT_AUTHED:
124 		return ("class 2 frame received from nonauthenticated STA");
125 	case IEEE80211_REASON_NOT_ASSOCED:
126 		return ("class 3 frame received from nonassociated STA");
127 	case IEEE80211_REASON_ASSOC_LEAVE:
128 		return ("sending STA is leaving/has left BSS");
129 	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
130 		return ("STA requesting (re)association is not authenticated");
131 	case IEEE80211_REASON_DISASSOC_PWRCAP_BAD:
132 		return ("information in the Power Capability element is "
133 			"unacceptable");
134 	case IEEE80211_REASON_DISASSOC_SUPCHAN_BAD:
135 		return ("information in the Supported Channels element is "
136 			"unacceptable");
137 	case IEEE80211_REASON_IE_INVALID:
138 		return ("invalid element");
139 	case IEEE80211_REASON_MIC_FAILURE:
140 		return ("MIC failure");
141 	case IEEE80211_REASON_4WAY_HANDSHAKE_TIMEOUT:
142 		return ("4-Way handshake timeout");
143 	case IEEE80211_REASON_GROUP_KEY_UPDATE_TIMEOUT:
144 		return ("group key update timeout");
145 	case IEEE80211_REASON_IE_IN_4WAY_DIFFERS:
146 		return ("element in 4-Way handshake different from "
147 			"(re)association request/probe response/beacon frame");
148 	case IEEE80211_REASON_GROUP_CIPHER_INVALID:
149 		return ("invalid group cipher");
150 	case IEEE80211_REASON_PAIRWISE_CIPHER_INVALID:
151 		return ("invalid pairwise cipher");
152 	case IEEE80211_REASON_AKMP_INVALID:
153 		return ("invalid AKMP");
154 	case IEEE80211_REASON_UNSUPP_RSN_IE_VERSION:
155 		return ("unsupported version in RSN IE");
156 	case IEEE80211_REASON_INVALID_RSN_IE_CAP:
157 		return ("invalid capabilities in RSN IE");
158 	case IEEE80211_REASON_802_1X_AUTH_FAILED:
159 		return ("IEEE 802.1X authentication failed");
160 	case IEEE80211_REASON_CIPHER_SUITE_REJECTED:
161 		return ("cipher suite rejected because of the security "
162 			"policy");
163 	case IEEE80211_REASON_UNSPECIFIED_QOS:
164 		return ("unspecified (QoS-related)");
165 	case IEEE80211_REASON_INSUFFICIENT_BW:
166 		return ("QoS AP lacks sufficient bandwidth for this QoS STA");
167 	case IEEE80211_REASON_TOOMANY_FRAMES:
168 		return ("too many frames need to be acknowledged");
169 	case IEEE80211_REASON_OUTSIDE_TXOP:
170 		return ("STA is transmitting outside the limits of its TXOPs");
171 	case IEEE80211_REASON_LEAVING_QBSS:
172 		return ("requested from peer STA (the STA is "
173 			"resetting/leaving the BSS)");
174 	case IEEE80211_REASON_BAD_MECHANISM:
175 		return ("requested from peer STA (it does not want to use "
176 			"the mechanism)");
177 	case IEEE80211_REASON_SETUP_NEEDED:
178 		return ("requested from peer STA (setup is required for the "
179 			"used mechanism)");
180 	case IEEE80211_REASON_TIMEOUT:
181 		return ("requested from peer STA (timeout)");
182 	case IEEE80211_REASON_PEER_LINK_CANCELED:
183 		return ("SME cancels the mesh peering instance (not related "
184 			"to the maximum number of peer mesh STAs)");
185 	case IEEE80211_REASON_MESH_MAX_PEERS:
186 		return ("maximum number of peer mesh STAs was reached");
187 	case IEEE80211_REASON_MESH_CPVIOLATION:
188 		return ("the received information violates the Mesh "
189 			"Configuration policy configured in the mesh STA "
190 			"profile");
191 	case IEEE80211_REASON_MESH_CLOSE_RCVD:
192 		return ("the mesh STA has received a Mesh Peering Close "
193 			"message requesting to close the mesh peering");
194 	case IEEE80211_REASON_MESH_MAX_RETRIES:
195 		return ("the mesh STA has resent dot11MeshMaxRetries Mesh "
196 			"Peering Open messages, without receiving a Mesh "
197 			"Peering Confirm message");
198 	case IEEE80211_REASON_MESH_CONFIRM_TIMEOUT:
199 		return ("the confirmTimer for the mesh peering instance times "
200 			"out");
201 	case IEEE80211_REASON_MESH_INVALID_GTK:
202 		return ("the mesh STA fails to unwrap the GTK or the values "
203 			"in the wrapped contents do not match");
204 	case IEEE80211_REASON_MESH_INCONS_PARAMS:
205 		return ("the mesh STA receives inconsistent information about "
206 			"the mesh parameters between Mesh Peering Management "
207 			"frames");
208 	case IEEE80211_REASON_MESH_INVALID_SECURITY:
209 		return ("the mesh STA fails the authenticated mesh peering "
210 			"exchange because due to failure in selecting "
211 			"pairwise/group ciphersuite");
212 	case IEEE80211_REASON_MESH_PERR_NO_PROXY:
213 		return ("the mesh STA does not have proxy information for "
214 			"this external destination");
215 	case IEEE80211_REASON_MESH_PERR_NO_FI:
216 		return ("the mesh STA does not have forwarding information "
217 			"for this destination");
218 	case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
219 		return ("the mesh STA determines that the link to the next "
220 			"hop of an active path in its forwarding information "
221 			"is no longer usable");
222 	case IEEE80211_REASON_MESH_MAC_ALRDY_EXISTS_MBSS:
223 		return ("the MAC address of the STA already exists in the "
224 			"mesh BSS");
225 	case IEEE80211_REASON_MESH_CHAN_SWITCH_REG:
226 		return ("the mesh STA performs channel switch to meet "
227 			"regulatory requirements");
228 	case IEEE80211_REASON_MESH_CHAN_SWITCH_UNSPEC:
229 		return ("the mesh STA performs channel switch with "
230 			"unspecified reason");
231 	default:
232 		return ("reserved/unknown");
233 	}
234 }
235 
236 static void beacon_miss(void *, int);
237 static void beacon_swmiss(void *, int);
238 static void parent_updown(void *, int);
239 static void update_mcast(void *, int);
240 static void update_promisc(void *, int);
241 static void update_channel(void *, int);
242 static void update_chw(void *, int);
243 static void vap_update_wme(void *, int);
244 static void vap_update_slot(void *, int);
245 static void restart_vaps(void *, int);
246 static void vap_update_erp_protmode(void *, int);
247 static void vap_update_preamble(void *, int);
248 static void vap_update_ht_protmode(void *, int);
249 static void ieee80211_newstate_cb(void *, int);
250 static struct ieee80211_node *vap_update_bss(struct ieee80211vap *,
251     struct ieee80211_node *);
252 
253 static int
null_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)254 null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
255 	const struct ieee80211_bpf_params *params)
256 {
257 
258 	ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
259 	m_freem(m);
260 	return ENETDOWN;
261 }
262 
263 void
ieee80211_proto_attach(struct ieee80211com * ic)264 ieee80211_proto_attach(struct ieee80211com *ic)
265 {
266 	uint8_t hdrlen;
267 
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;
279 	}
280 	//ic->ic_protmode = IEEE80211_PROT_CTSONLY;
281 
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);
289 
290 	ic->ic_wme.wme_hipri_switch_hysteresis =
291 		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
292 
293 	/* initialize management frame handlers */
294 	ic->ic_send_mgmt = ieee80211_send_mgmt;
295 	ic->ic_raw_xmit = null_raw_xmit;
296 
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);
303 #endif
304 	ieee80211_monitor_attach(ic);
305 }
306 
307 void
ieee80211_proto_detach(struct ieee80211com * ic)308 ieee80211_proto_detach(struct ieee80211com *ic)
309 {
310 	ieee80211_monitor_detach(ic);
311 #ifdef IEEE80211_SUPPORT_MESH
312 	ieee80211_mesh_detach(ic);
313 #endif
314 	ieee80211_hostap_detach(ic);
315 	ieee80211_wds_detach(ic);
316 	ieee80211_adhoc_detach(ic);
317 	ieee80211_sta_detach(ic);
318 }
319 
320 static void
null_update_beacon(struct ieee80211vap * vap,int item)321 null_update_beacon(struct ieee80211vap *vap, int item)
322 {
323 }
324 
325 void
ieee80211_proto_vattach(struct ieee80211vap * vap)326 ieee80211_proto_vattach(struct ieee80211vap *vap)
327 {
328 	struct ieee80211com *ic = vap->iv_ic;
329 	struct ifnet *ifp = vap->iv_ifp;
330 	int i;
331 
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;
337 
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 	for (i = 0; i < NET80211_IV_NSTATE_NUM; i++)
344 		TASK_INIT(&vap->iv_nstate_task[i], 0, ieee80211_newstate_cb, vap);
345 	TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
346 	TASK_INIT(&vap->iv_wme_task, 0, vap_update_wme, vap);
347 	TASK_INIT(&vap->iv_slot_task, 0, vap_update_slot, vap);
348 	TASK_INIT(&vap->iv_erp_protmode_task, 0, vap_update_erp_protmode, vap);
349 	TASK_INIT(&vap->iv_ht_protmode_task, 0, vap_update_ht_protmode, vap);
350 	TASK_INIT(&vap->iv_preamble_task, 0, vap_update_preamble, vap);
351 	/*
352 	 * Install default tx rate handling: no fixed rate, lowest
353 	 * supported rate for mgmt and multicast frames.  Default
354 	 * max retry count.  These settings can be changed by the
355 	 * driver and/or user applications.
356 	 */
357 	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
358 		if (isclr(ic->ic_modecaps, i))
359 			continue;
360 
361 		const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
362 
363 		vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
364 
365 		/*
366 		 * Setting the management rate to MCS 0 assumes that the
367 		 * BSS Basic rate set is empty and the BSS Basic MCS set
368 		 * is not.
369 		 *
370 		 * Since we're not checking this, default to the lowest
371 		 * defined rate for this mode.
372 		 *
373 		 * At least one 11n AP (DLINK DIR-825) is reported to drop
374 		 * some MCS management traffic (eg BA response frames.)
375 		 *
376 		 * See also: 9.6.0 of the 802.11n-2009 specification.
377 		 */
378 #ifdef	NOTYET
379 		if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
380 			vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
381 			vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
382 		} else {
383 			vap->iv_txparms[i].mgmtrate =
384 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
385 			vap->iv_txparms[i].mcastrate =
386 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
387 		}
388 #endif
389 		vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
390 		vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
391 		vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
392 	}
393 	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
394 
395 	vap->iv_update_beacon = null_update_beacon;
396 	vap->iv_deliver_data = ieee80211_deliver_data;
397 	vap->iv_protmode = IEEE80211_PROT_CTSONLY;
398 	vap->iv_update_bss = vap_update_bss;
399 
400 	/* attach support for operating mode */
401 	ic->ic_vattach[vap->iv_opmode](vap);
402 }
403 
404 void
ieee80211_proto_vdetach(struct ieee80211vap * vap)405 ieee80211_proto_vdetach(struct ieee80211vap *vap)
406 {
407 #define	FREEAPPIE(ie) do { \
408 	if (ie != NULL) \
409 		IEEE80211_FREE(ie, M_80211_NODE_IE); \
410 } while (0)
411 	/*
412 	 * Detach operating mode module.
413 	 */
414 	if (vap->iv_opdetach != NULL)
415 		vap->iv_opdetach(vap);
416 	/*
417 	 * This should not be needed as we detach when reseting
418 	 * the state but be conservative here since the
419 	 * authenticator may do things like spawn kernel threads.
420 	 */
421 	if (vap->iv_auth->ia_detach != NULL)
422 		vap->iv_auth->ia_detach(vap);
423 	/*
424 	 * Detach any ACL'ator.
425 	 */
426 	if (vap->iv_acl != NULL)
427 		vap->iv_acl->iac_detach(vap);
428 
429 	FREEAPPIE(vap->iv_appie_beacon);
430 	FREEAPPIE(vap->iv_appie_probereq);
431 	FREEAPPIE(vap->iv_appie_proberesp);
432 	FREEAPPIE(vap->iv_appie_assocreq);
433 	FREEAPPIE(vap->iv_appie_assocresp);
434 	FREEAPPIE(vap->iv_appie_wpa);
435 #undef FREEAPPIE
436 }
437 
438 /*
439  * Simple-minded authenticator module support.
440  */
441 
442 #define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
443 /* XXX well-known names */
444 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
445 	"wlan_internal",	/* IEEE80211_AUTH_NONE */
446 	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
447 	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
448 	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
449 	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
450 	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
451 };
452 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
453 
454 static const struct ieee80211_authenticator auth_internal = {
455 	.ia_name		= "wlan_internal",
456 	.ia_attach		= NULL,
457 	.ia_detach		= NULL,
458 	.ia_node_join		= NULL,
459 	.ia_node_leave		= NULL,
460 };
461 
462 /*
463  * Setup internal authenticators once; they are never unregistered.
464  */
465 static void
ieee80211_auth_setup(void)466 ieee80211_auth_setup(void)
467 {
468 	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
469 	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
470 	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
471 }
472 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
473 
474 const struct ieee80211_authenticator *
ieee80211_authenticator_get(int auth)475 ieee80211_authenticator_get(int auth)
476 {
477 	if (auth >= IEEE80211_AUTH_MAX)
478 		return NULL;
479 	if (authenticators[auth] == NULL)
480 		ieee80211_load_module(auth_modnames[auth]);
481 	return authenticators[auth];
482 }
483 
484 void
ieee80211_authenticator_register(int type,const struct ieee80211_authenticator * auth)485 ieee80211_authenticator_register(int type,
486 	const struct ieee80211_authenticator *auth)
487 {
488 	if (type >= IEEE80211_AUTH_MAX)
489 		return;
490 	authenticators[type] = auth;
491 }
492 
493 void
ieee80211_authenticator_unregister(int type)494 ieee80211_authenticator_unregister(int type)
495 {
496 
497 	if (type >= IEEE80211_AUTH_MAX)
498 		return;
499 	authenticators[type] = NULL;
500 }
501 
502 /*
503  * Very simple-minded ACL module support.
504  */
505 /* XXX just one for now */
506 static	const struct ieee80211_aclator *acl = NULL;
507 
508 void
ieee80211_aclator_register(const struct ieee80211_aclator * iac)509 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
510 {
511 	printf("wlan: %s acl policy registered\n", iac->iac_name);
512 	acl = iac;
513 }
514 
515 void
ieee80211_aclator_unregister(const struct ieee80211_aclator * iac)516 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
517 {
518 	if (acl == iac)
519 		acl = NULL;
520 	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
521 }
522 
523 const struct ieee80211_aclator *
ieee80211_aclator_get(const char * name)524 ieee80211_aclator_get(const char *name)
525 {
526 	if (acl == NULL)
527 		ieee80211_load_module("wlan_acl");
528 	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
529 }
530 
531 void
ieee80211_print_essid(const uint8_t * essid,int len)532 ieee80211_print_essid(const uint8_t *essid, int len)
533 {
534 	const uint8_t *p;
535 	int i;
536 
537 	if (len > IEEE80211_NWID_LEN)
538 		len = IEEE80211_NWID_LEN;
539 	/* determine printable or not */
540 	for (i = 0, p = essid; i < len; i++, p++) {
541 		if (*p < ' ' || *p > 0x7e)
542 			break;
543 	}
544 	if (i == len) {
545 		printf("\"");
546 		for (i = 0, p = essid; i < len; i++, p++)
547 			printf("%c", *p);
548 		printf("\"");
549 	} else {
550 		printf("0x");
551 		for (i = 0, p = essid; i < len; i++, p++)
552 			printf("%02x", *p);
553 	}
554 }
555 
556 void
ieee80211_dump_pkt(struct ieee80211com * ic,const uint8_t * buf,int len,int rate,int rssi)557 ieee80211_dump_pkt(struct ieee80211com *ic,
558 	const uint8_t *buf, int len, int rate, int rssi)
559 {
560 	const struct ieee80211_frame *wh;
561 	int i;
562 
563 	wh = (const struct ieee80211_frame *)buf;
564 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
565 	case IEEE80211_FC1_DIR_NODS:
566 		printf("NODS %s", ether_sprintf(wh->i_addr2));
567 		printf("->%s", ether_sprintf(wh->i_addr1));
568 		printf("(%s)", ether_sprintf(wh->i_addr3));
569 		break;
570 	case IEEE80211_FC1_DIR_TODS:
571 		printf("TODS %s", ether_sprintf(wh->i_addr2));
572 		printf("->%s", ether_sprintf(wh->i_addr3));
573 		printf("(%s)", ether_sprintf(wh->i_addr1));
574 		break;
575 	case IEEE80211_FC1_DIR_FROMDS:
576 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
577 		printf("->%s", ether_sprintf(wh->i_addr1));
578 		printf("(%s)", ether_sprintf(wh->i_addr2));
579 		break;
580 	case IEEE80211_FC1_DIR_DSTODS:
581 		printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
582 		printf("->%s", ether_sprintf(wh->i_addr3));
583 		printf("(%s", ether_sprintf(wh->i_addr2));
584 		printf("->%s)", ether_sprintf(wh->i_addr1));
585 		break;
586 	}
587 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
588 	case IEEE80211_FC0_TYPE_DATA:
589 		printf(" data");
590 		break;
591 	case IEEE80211_FC0_TYPE_MGT:
592 		printf(" %s", ieee80211_mgt_subtype_name(wh->i_fc[0]));
593 		break;
594 	default:
595 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
596 		break;
597 	}
598 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
599 		const struct ieee80211_qosframe *qwh =
600 			(const struct ieee80211_qosframe *)buf;
601 		printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
602 			qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
603 	}
604 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
605 		int off;
606 
607 		off = ieee80211_anyhdrspace(ic, wh);
608 		printf(" WEP [IV %.02x %.02x %.02x",
609 			buf[off+0], buf[off+1], buf[off+2]);
610 		if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
611 			printf(" %.02x %.02x %.02x",
612 				buf[off+4], buf[off+5], buf[off+6]);
613 		printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
614 	}
615 	if (rate >= 0)
616 		printf(" %dM", rate / 2);
617 	if (rssi >= 0)
618 		printf(" +%d", rssi);
619 	printf("\n");
620 	if (len > 0) {
621 		for (i = 0; i < len; i++) {
622 			if ((i & 1) == 0)
623 				printf(" ");
624 			printf("%02x", buf[i]);
625 		}
626 		printf("\n");
627 	}
628 }
629 
630 static __inline int
findrix(const struct ieee80211_rateset * rs,int r)631 findrix(const struct ieee80211_rateset *rs, int r)
632 {
633 	int i;
634 
635 	for (i = 0; i < rs->rs_nrates; i++)
636 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
637 			return i;
638 	return -1;
639 }
640 
641 int
ieee80211_fix_rate(struct ieee80211_node * ni,struct ieee80211_rateset * nrs,int flags)642 ieee80211_fix_rate(struct ieee80211_node *ni,
643 	struct ieee80211_rateset *nrs, int flags)
644 {
645 	struct ieee80211vap *vap = ni->ni_vap;
646 	struct ieee80211com *ic = ni->ni_ic;
647 	int i, j, rix, error;
648 	int okrate, badrate, fixedrate, ucastrate;
649 	const struct ieee80211_rateset *srs;
650 	uint8_t r;
651 
652 	error = 0;
653 	okrate = badrate = 0;
654 	ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
655 	if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
656 		/*
657 		 * Workaround awkwardness with fixed rate.  We are called
658 		 * to check both the legacy rate set and the HT rate set
659 		 * but we must apply any legacy fixed rate check only to the
660 		 * legacy rate set and vice versa.  We cannot tell what type
661 		 * of rate set we've been given (legacy or HT) but we can
662 		 * distinguish the fixed rate type (MCS have 0x80 set).
663 		 * So to deal with this the caller communicates whether to
664 		 * check MCS or legacy rate using the flags and we use the
665 		 * type of any fixed rate to avoid applying an MCS to a
666 		 * legacy rate and vice versa.
667 		 */
668 		if (ucastrate & 0x80) {
669 			if (flags & IEEE80211_F_DOFRATE)
670 				flags &= ~IEEE80211_F_DOFRATE;
671 		} else if ((ucastrate & 0x80) == 0) {
672 			if (flags & IEEE80211_F_DOFMCS)
673 				flags &= ~IEEE80211_F_DOFMCS;
674 		}
675 		/* NB: required to make MCS match below work */
676 		ucastrate &= IEEE80211_RATE_VAL;
677 	}
678 	fixedrate = IEEE80211_FIXED_RATE_NONE;
679 	/*
680 	 * XXX we are called to process both MCS and legacy rates;
681 	 * we must use the appropriate basic rate set or chaos will
682 	 * ensue; for now callers that want MCS must supply
683 	 * IEEE80211_F_DOBRS; at some point we'll need to split this
684 	 * function so there are two variants, one for MCS and one
685 	 * for legacy rates.
686 	 */
687 	if (flags & IEEE80211_F_DOBRS)
688 		srs = (const struct ieee80211_rateset *)
689 		    ieee80211_get_suphtrates(ic, ni->ni_chan);
690 	else
691 		srs = ieee80211_get_suprates(ic, ni->ni_chan);
692 	for (i = 0; i < nrs->rs_nrates; ) {
693 		if (flags & IEEE80211_F_DOSORT) {
694 			/*
695 			 * Sort rates.
696 			 */
697 			for (j = i + 1; j < nrs->rs_nrates; j++) {
698 				if (IEEE80211_RV(nrs->rs_rates[i]) >
699 				    IEEE80211_RV(nrs->rs_rates[j])) {
700 					r = nrs->rs_rates[i];
701 					nrs->rs_rates[i] = nrs->rs_rates[j];
702 					nrs->rs_rates[j] = r;
703 				}
704 			}
705 		}
706 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
707 		badrate = r;
708 		/*
709 		 * Check for fixed rate.
710 		 */
711 		if (r == ucastrate)
712 			fixedrate = r;
713 		/*
714 		 * Check against supported rates.
715 		 */
716 		rix = findrix(srs, r);
717 		if (flags & IEEE80211_F_DONEGO) {
718 			if (rix < 0) {
719 				/*
720 				 * A rate in the node's rate set is not
721 				 * supported.  If this is a basic rate and we
722 				 * are operating as a STA then this is an error.
723 				 * Otherwise we just discard/ignore the rate.
724 				 */
725 				if ((flags & IEEE80211_F_JOIN) &&
726 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
727 					error++;
728 			} else if ((flags & IEEE80211_F_JOIN) == 0) {
729 				/*
730 				 * Overwrite with the supported rate
731 				 * value so any basic rate bit is set.
732 				 */
733 				nrs->rs_rates[i] = srs->rs_rates[rix];
734 			}
735 		}
736 		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
737 			/*
738 			 * Delete unacceptable rates.
739 			 */
740 			nrs->rs_nrates--;
741 			for (j = i; j < nrs->rs_nrates; j++)
742 				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
743 			nrs->rs_rates[j] = 0;
744 			continue;
745 		}
746 		if (rix >= 0)
747 			okrate = nrs->rs_rates[i];
748 		i++;
749 	}
750 	if (okrate == 0 || error != 0 ||
751 	    ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
752 	     fixedrate != ucastrate)) {
753 		IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
754 		    "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
755 		    "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
756 		return badrate | IEEE80211_RATE_BASIC;
757 	} else
758 		return IEEE80211_RV(okrate);
759 }
760 
761 /*
762  * Reset 11g-related state.
763  *
764  * This is for per-VAP ERP/11g state.
765  *
766  * Eventually everything in ieee80211_reset_erp() will be
767  * per-VAP and in here.
768  */
769 void
ieee80211_vap_reset_erp(struct ieee80211vap * vap)770 ieee80211_vap_reset_erp(struct ieee80211vap *vap)
771 {
772 	struct ieee80211com *ic = vap->iv_ic;
773 
774 	vap->iv_nonerpsta = 0;
775 	vap->iv_longslotsta = 0;
776 
777 	vap->iv_flags &= ~IEEE80211_F_USEPROT;
778 	/*
779 	 * Set short preamble and ERP barker-preamble flags.
780 	 */
781 	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
782 	    (vap->iv_caps & IEEE80211_C_SHPREAMBLE)) {
783 		vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
784 		vap->iv_flags &= ~IEEE80211_F_USEBARKER;
785 	} else {
786 		vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
787 		vap->iv_flags |= IEEE80211_F_USEBARKER;
788 	}
789 
790 	/*
791 	 * Short slot time is enabled only when operating in 11g
792 	 * and not in an IBSS.  We must also honor whether or not
793 	 * the driver is capable of doing it.
794 	 */
795 	ieee80211_vap_set_shortslottime(vap,
796 		IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
797 		IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
798 		(IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
799 		vap->iv_opmode == IEEE80211_M_HOSTAP &&
800 		(ic->ic_caps & IEEE80211_C_SHSLOT)));
801 }
802 
803 /*
804  * Reset 11g-related state.
805  *
806  * Note this resets the global state and a caller should schedule
807  * a re-check of all the VAPs after setup to update said state.
808  */
809 void
ieee80211_reset_erp(struct ieee80211com * ic)810 ieee80211_reset_erp(struct ieee80211com *ic)
811 {
812 #if 0
813 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
814 	/*
815 	 * Set short preamble and ERP barker-preamble flags.
816 	 */
817 	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
818 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
819 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
820 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
821 	} else {
822 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
823 		ic->ic_flags |= IEEE80211_F_USEBARKER;
824 	}
825 #endif
826 	/* XXX TODO: schedule a new per-VAP ERP calculation */
827 }
828 
829 static struct ieee80211_node *
vap_update_bss(struct ieee80211vap * vap,struct ieee80211_node * ni)830 vap_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
831 {
832 	struct ieee80211_node *obss;
833 
834 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
835 
836 	obss = vap->iv_bss;
837 	vap->iv_bss = ni;
838 
839 	return (obss);
840 }
841 
842 /*
843  * Deferred slot time update.
844  *
845  * For per-VAP slot time configuration, call the VAP
846  * method if the VAP requires it.  Otherwise, just call the
847  * older global method.
848  *
849  * If the per-VAP method is called then it's expected that
850  * the driver/firmware will take care of turning the per-VAP
851  * flags into slot time configuration.
852  *
853  * If the per-VAP method is not called then the global flags will be
854  * flipped into sync with the VAPs; ic_flags IEEE80211_F_SHSLOT will
855  * be set only if all of the vaps will have it set.
856  *
857  * Look at the comments for vap_update_erp_protmode() for more
858  * background; this assumes all VAPs are on the same channel.
859  */
860 static void
vap_update_slot(void * arg,int npending)861 vap_update_slot(void *arg, int npending)
862 {
863 	struct ieee80211vap *vap = arg;
864 	struct ieee80211com *ic = vap->iv_ic;
865 	struct ieee80211vap *iv;
866 	int num_shslot = 0, num_lgslot = 0;
867 
868 	/*
869 	 * Per-VAP path - we've already had the flags updated;
870 	 * so just notify the driver and move on.
871 	 */
872 	if (vap->iv_updateslot != NULL) {
873 		vap->iv_updateslot(vap);
874 		return;
875 	}
876 
877 	/*
878 	 * Iterate over all of the VAP flags to update the
879 	 * global flag.
880 	 *
881 	 * If all vaps have short slot enabled then flip on
882 	 * short slot.  If any vap has it disabled then
883 	 * we leave it globally disabled.  This should provide
884 	 * correct behaviour in a multi-BSS scenario where
885 	 * at least one VAP has short slot disabled for some
886 	 * reason.
887 	 */
888 	IEEE80211_LOCK(ic);
889 	TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
890 		if (iv->iv_flags & IEEE80211_F_SHSLOT)
891 			num_shslot++;
892 		else
893 			num_lgslot++;
894 	}
895 
896 	/*
897 	 * It looks backwards but - if the number of short slot VAPs
898 	 * is zero then we're not short slot.  Else, we have one
899 	 * or more short slot VAPs and we're checking to see if ANY
900 	 * of them have short slot disabled.
901 	 */
902 	if (num_shslot == 0)
903 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
904 	else if (num_lgslot == 0)
905 		ic->ic_flags |= IEEE80211_F_SHSLOT;
906 	IEEE80211_UNLOCK(ic);
907 
908 	/*
909 	 * Call the driver with our new global slot time flags.
910 	 */
911 	if (ic->ic_updateslot != NULL)
912 		ic->ic_updateslot(ic);
913 }
914 
915 /*
916  * Deferred ERP protmode update.
917  *
918  * This currently calculates the global ERP protection mode flag
919  * based on each of the VAPs.  Any VAP with it enabled is enough
920  * for the global flag to be enabled.  All VAPs with it disabled
921  * is enough for it to be disabled.
922  *
923  * This may make sense right now for the supported hardware where
924  * net80211 is controlling the single channel configuration, but
925  * offload firmware that's doing channel changes (eg off-channel
926  * TDLS, off-channel STA, off-channel P2P STA/AP) may get some
927  * silly looking flag updates.
928  *
929  * Ideally the protection mode calculation is done based on the
930  * channel, and all VAPs using that channel will inherit it.
931  * But until that's what net80211 does, this wil have to do.
932  */
933 static void
vap_update_erp_protmode(void * arg,int npending)934 vap_update_erp_protmode(void *arg, int npending)
935 {
936 	struct ieee80211vap *vap = arg;
937 	struct ieee80211com *ic = vap->iv_ic;
938 	struct ieee80211vap *iv;
939 	int enable_protmode = 0;
940 	int non_erp_present = 0;
941 
942 	/*
943 	 * Iterate over all of the VAPs to calculate the overlapping
944 	 * ERP protection mode configuration and ERP present math.
945 	 *
946 	 * For now we assume that if a driver can handle this per-VAP
947 	 * then it'll ignore the ic->ic_protmode variant and instead
948 	 * will look at the vap related flags.
949 	 */
950 	IEEE80211_LOCK(ic);
951 	TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
952 		if (iv->iv_flags & IEEE80211_F_USEPROT)
953 			enable_protmode = 1;
954 		if (iv->iv_flags_ext & IEEE80211_FEXT_NONERP_PR)
955 			non_erp_present = 1;
956 	}
957 
958 	if (enable_protmode)
959 		ic->ic_flags |= IEEE80211_F_USEPROT;
960 	else
961 		ic->ic_flags &= ~IEEE80211_F_USEPROT;
962 
963 	if (non_erp_present)
964 		ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
965 	else
966 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
967 
968 	/* Beacon update on all VAPs */
969 	ieee80211_notify_erp_locked(ic);
970 
971 	IEEE80211_UNLOCK(ic);
972 
973 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
974 	    "%s: called; enable_protmode=%d, non_erp_present=%d\n",
975 	    __func__, enable_protmode, non_erp_present);
976 
977 	/*
978 	 * Now that the global configuration flags are calculated,
979 	 * notify the VAP about its configuration.
980 	 *
981 	 * The global flags will be used when assembling ERP IEs
982 	 * for multi-VAP operation, even if it's on a different
983 	 * channel.  Yes, that's going to need fixing in the
984 	 * future.
985 	 */
986 	if (vap->iv_erp_protmode_update != NULL)
987 		vap->iv_erp_protmode_update(vap);
988 }
989 
990 /*
991  * Deferred ERP short preamble/barker update.
992  *
993  * All VAPs need to use short preamble for it to be globally
994  * enabled or not.
995  *
996  * Look at the comments for vap_update_erp_protmode() for more
997  * background; this assumes all VAPs are on the same channel.
998  */
999 static void
vap_update_preamble(void * arg,int npending)1000 vap_update_preamble(void *arg, int npending)
1001 {
1002 	struct ieee80211vap *vap = arg;
1003 	struct ieee80211com *ic = vap->iv_ic;
1004 	struct ieee80211vap *iv;
1005 	int barker_count = 0, short_preamble_count = 0, count = 0;
1006 
1007 	/*
1008 	 * Iterate over all of the VAPs to calculate the overlapping
1009 	 * short or long preamble configuration.
1010 	 *
1011 	 * For now we assume that if a driver can handle this per-VAP
1012 	 * then it'll ignore the ic->ic_flags variant and instead
1013 	 * will look at the vap related flags.
1014 	 */
1015 	IEEE80211_LOCK(ic);
1016 	TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
1017 		if (iv->iv_flags & IEEE80211_F_USEBARKER)
1018 			barker_count++;
1019 		if (iv->iv_flags & IEEE80211_F_SHPREAMBLE)
1020 			short_preamble_count++;
1021 		count++;
1022 	}
1023 
1024 	/*
1025 	 * As with vap_update_erp_protmode(), the global flags are
1026 	 * currently used for beacon IEs.
1027 	 */
1028 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1029 	    "%s: called; barker_count=%d, short_preamble_count=%d\n",
1030 	    __func__, barker_count, short_preamble_count);
1031 
1032 	/*
1033 	 * Only flip on short preamble if all of the VAPs support
1034 	 * it.
1035 	 */
1036 	if (barker_count == 0 && short_preamble_count == count) {
1037 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1038 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1039 	} else {
1040 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1041 		ic->ic_flags |= IEEE80211_F_USEBARKER;
1042 	}
1043 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1044 	  "%s: global barker=%d preamble=%d\n",
1045 	  __func__,
1046 	  !! (ic->ic_flags & IEEE80211_F_USEBARKER),
1047 	  !! (ic->ic_flags & IEEE80211_F_SHPREAMBLE));
1048 
1049 	/* Beacon update on all VAPs */
1050 	ieee80211_notify_erp_locked(ic);
1051 
1052 	IEEE80211_UNLOCK(ic);
1053 
1054 	/* Driver notification */
1055 	if (vap->iv_preamble_update != NULL)
1056 		vap->iv_preamble_update(vap);
1057 }
1058 
1059 /*
1060  * Deferred HT protmode update and beacon update.
1061  *
1062  * Look at the comments for vap_update_erp_protmode() for more
1063  * background; this assumes all VAPs are on the same channel.
1064  */
1065 static void
vap_update_ht_protmode(void * arg,int npending)1066 vap_update_ht_protmode(void *arg, int npending)
1067 {
1068 	struct ieee80211vap *vap = arg;
1069 	struct ieee80211vap *iv;
1070 	struct ieee80211com *ic = vap->iv_ic;
1071 	int num_vaps = 0, num_pure = 0;
1072 	int num_optional = 0, num_ht2040 = 0, num_nonht = 0;
1073 	int num_ht_sta = 0, num_ht40_sta = 0, num_sta = 0;
1074 	int num_nonhtpr = 0;
1075 
1076 	/*
1077 	 * Iterate over all of the VAPs to calculate everything.
1078 	 *
1079 	 * There are a few different flags to calculate:
1080 	 *
1081 	 * + whether there's HT only or HT+legacy stations;
1082 	 * + whether there's HT20, HT40, or HT20+HT40 stations;
1083 	 * + whether the desired protection mode is mixed, pure or
1084 	 *   one of the two above.
1085 	 *
1086 	 * For now we assume that if a driver can handle this per-VAP
1087 	 * then it'll ignore the ic->ic_htprotmode / ic->ic_curhtprotmode
1088 	 * variant and instead will look at the vap related variables.
1089 	 *
1090 	 * XXX TODO: non-greenfield STAs present (IEEE80211_HTINFO_NONGF_PRESENT) !
1091 	 */
1092 
1093 	IEEE80211_LOCK(ic);
1094 	TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next) {
1095 		num_vaps++;
1096 		/* overlapping BSSes advertising non-HT status present */
1097 		if (iv->iv_flags_ht & IEEE80211_FHT_NONHT_PR)
1098 			num_nonht++;
1099 		/* Operating mode flags */
1100 		if (iv->iv_curhtprotmode & IEEE80211_HTINFO_NONHT_PRESENT)
1101 			num_nonhtpr++;
1102 		switch (iv->iv_curhtprotmode & IEEE80211_HTINFO_OPMODE) {
1103 		case IEEE80211_HTINFO_OPMODE_PURE:
1104 			num_pure++;
1105 			break;
1106 		case IEEE80211_HTINFO_OPMODE_PROTOPT:
1107 			num_optional++;
1108 			break;
1109 		case IEEE80211_HTINFO_OPMODE_HT20PR:
1110 			num_ht2040++;
1111 			break;
1112 		}
1113 
1114 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
1115 		    "%s: vap %s: nonht_pr=%d, curhtprotmode=0x%02x\n",
1116 		    __func__,
1117 		    ieee80211_get_vap_ifname(iv),
1118 		    !! (iv->iv_flags_ht & IEEE80211_FHT_NONHT_PR),
1119 		    iv->iv_curhtprotmode);
1120 
1121 		num_ht_sta += iv->iv_ht_sta_assoc;
1122 		num_ht40_sta += iv->iv_ht40_sta_assoc;
1123 		num_sta += iv->iv_sta_assoc;
1124 	}
1125 
1126 	/*
1127 	 * Step 1 - if any VAPs indicate NONHT_PR set (overlapping BSS
1128 	 * non-HT present), set it here.  This shouldn't be used by
1129 	 * anything but the old overlapping BSS logic so if any drivers
1130 	 * consume it, it's up to date.
1131 	 */
1132 	if (num_nonht > 0)
1133 		ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1134 	else
1135 		ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1136 
1137 	/*
1138 	 * Step 2 - default HT protection mode to MIXED (802.11-2016 10.26.3.1.)
1139 	 *
1140 	 * + If all VAPs are PURE, we can stay PURE.
1141 	 * + If all VAPs are PROTOPT, we can go to PROTOPT.
1142 	 * + If any VAP has HT20PR then it sees at least a HT40+HT20 station.
1143 	 *   Note that we may have a VAP with one HT20 and a VAP with one HT40;
1144 	 *   So we look at the sum ht and sum ht40 sta counts; if we have a
1145 	 *   HT station and the HT20 != HT40 count, we have to do HT20PR here.
1146 	 *   Note all stations need to be HT for this to be an option.
1147 	 * + The fall-through is MIXED, because it means we have some odd
1148 	 *   non HT40-involved combination of opmode and this is the most
1149 	 *   sensible default.
1150 	 */
1151 	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_MIXED;
1152 
1153 	if (num_pure == num_vaps)
1154 		ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
1155 
1156 	if (num_optional == num_vaps)
1157 		ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PROTOPT;
1158 
1159 	/*
1160 	 * Note: we need /a/ HT40 station somewhere for this to
1161 	 * be a possibility.
1162 	 */
1163 	if ((num_ht2040 > 0) ||
1164 	    ((num_ht_sta > 0) && (num_ht40_sta > 0) &&
1165 	     (num_ht_sta != num_ht40_sta)))
1166 		ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1167 
1168 	/*
1169 	 * Step 3 - if any of the stations across the VAPs are
1170 	 * non-HT then this needs to be flipped back to MIXED.
1171 	 */
1172 	if (num_ht_sta != num_sta)
1173 		ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_MIXED;
1174 
1175 	/*
1176 	 * Step 4 - If we see any overlapping BSS non-HT stations
1177 	 * via beacons then flip on NONHT_PRESENT.
1178 	 */
1179 	if (num_nonhtpr > 0)
1180 		ic->ic_curhtprotmode |= IEEE80211_HTINFO_NONHT_PRESENT;
1181 
1182 	/* Notify all VAPs to potentially update their beacons */
1183 	TAILQ_FOREACH(iv, &ic->ic_vaps, iv_next)
1184 		ieee80211_htinfo_notify(iv);
1185 
1186 	IEEE80211_UNLOCK(ic);
1187 
1188 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
1189 	  "%s: global: nonht_pr=%d ht_opmode=0x%02x\n",
1190 	  __func__,
1191 	  !! (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR),
1192 	  ic->ic_curhtprotmode);
1193 
1194 	/* Driver update */
1195 	if (vap->iv_ht_protmode_update != NULL)
1196 		vap->iv_ht_protmode_update(vap);
1197 }
1198 
1199 /*
1200  * Set the short slot time state and notify the driver.
1201  *
1202  * This is the per-VAP slot time state.
1203  */
1204 void
ieee80211_vap_set_shortslottime(struct ieee80211vap * vap,int onoff)1205 ieee80211_vap_set_shortslottime(struct ieee80211vap *vap, int onoff)
1206 {
1207 	struct ieee80211com *ic = vap->iv_ic;
1208 
1209 	/* XXX lock? */
1210 
1211 	/*
1212 	 * Only modify the per-VAP slot time.
1213 	 */
1214 	if (onoff)
1215 		vap->iv_flags |= IEEE80211_F_SHSLOT;
1216 	else
1217 		vap->iv_flags &= ~IEEE80211_F_SHSLOT;
1218 
1219 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1220 	    "%s: called; onoff=%d\n", __func__, onoff);
1221 	/* schedule the deferred slot flag update and update */
1222 	ieee80211_runtask(ic, &vap->iv_slot_task);
1223 }
1224 
1225 /*
1226  * Update the VAP short /long / barker preamble state and
1227  * update beacon state if needed.
1228  *
1229  * For now it simply copies the global flags into the per-vap
1230  * flags and schedules the callback.  Later this will support
1231  * both global and per-VAP flags, especially useful for
1232  * and STA+STA multi-channel operation (eg p2p).
1233  */
1234 void
ieee80211_vap_update_preamble(struct ieee80211vap * vap)1235 ieee80211_vap_update_preamble(struct ieee80211vap *vap)
1236 {
1237 	struct ieee80211com *ic = vap->iv_ic;
1238 
1239 	/* XXX lock? */
1240 
1241 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1242 	    "%s: called\n", __func__);
1243 	/* schedule the deferred slot flag update and update */
1244 	ieee80211_runtask(ic, &vap->iv_preamble_task);
1245 }
1246 
1247 /*
1248  * Update the VAP 11g protection mode and update beacon state
1249  * if needed.
1250  */
1251 void
ieee80211_vap_update_erp_protmode(struct ieee80211vap * vap)1252 ieee80211_vap_update_erp_protmode(struct ieee80211vap *vap)
1253 {
1254 	struct ieee80211com *ic = vap->iv_ic;
1255 
1256 	/* XXX lock? */
1257 
1258 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1259 	    "%s: called\n", __func__);
1260 	/* schedule the deferred slot flag update and update */
1261 	ieee80211_runtask(ic, &vap->iv_erp_protmode_task);
1262 }
1263 
1264 /*
1265  * Update the VAP 11n protection mode and update beacon state
1266  * if needed.
1267  */
1268 void
ieee80211_vap_update_ht_protmode(struct ieee80211vap * vap)1269 ieee80211_vap_update_ht_protmode(struct ieee80211vap *vap)
1270 {
1271 	struct ieee80211com *ic = vap->iv_ic;
1272 
1273 	/* XXX lock? */
1274 
1275 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG,
1276 	    "%s: called\n", __func__);
1277 	/* schedule the deferred protmode update */
1278 	ieee80211_runtask(ic, &vap->iv_ht_protmode_task);
1279 }
1280 
1281 /*
1282  * Check if the specified rate set supports ERP.
1283  * NB: the rate set is assumed to be sorted.
1284  */
1285 int
ieee80211_iserp_rateset(const struct ieee80211_rateset * rs)1286 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
1287 {
1288 	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
1289 	int i, j;
1290 
1291 	if (rs->rs_nrates < nitems(rates))
1292 		return 0;
1293 	for (i = 0; i < nitems(rates); i++) {
1294 		for (j = 0; j < rs->rs_nrates; j++) {
1295 			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
1296 			if (rates[i] == r)
1297 				goto next;
1298 			if (r > rates[i])
1299 				return 0;
1300 		}
1301 		return 0;
1302 	next:
1303 		;
1304 	}
1305 	return 1;
1306 }
1307 
1308 /*
1309  * Mark the basic rates for the rate table based on the
1310  * operating mode.  For real 11g we mark all the 11b rates
1311  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
1312  * 11b rates.  There's also a pseudo 11a-mode used to mark only
1313  * the basic OFDM rates.
1314  */
1315 static void
setbasicrates(struct ieee80211_rateset * rs,enum ieee80211_phymode mode,int add)1316 setbasicrates(struct ieee80211_rateset *rs,
1317     enum ieee80211_phymode mode, int add)
1318 {
1319 	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
1320 	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
1321 	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
1322 					    /* NB: mixed b/g */
1323 	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
1324 	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
1325 	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
1326 	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
1327 	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
1328 	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
1329 	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
1330 					    /* NB: mixed b/g */
1331 	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
1332 					    /* NB: mixed b/g */
1333 	    [IEEE80211_MODE_VHT_2GHZ]	= { 4, { 2, 4, 11, 22 } },
1334 	    [IEEE80211_MODE_VHT_5GHZ]	= { 3, { 12, 24, 48 } },
1335 	};
1336 	int i, j;
1337 
1338 	for (i = 0; i < rs->rs_nrates; i++) {
1339 		if (!add)
1340 			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
1341 		for (j = 0; j < basic[mode].rs_nrates; j++)
1342 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
1343 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
1344 				break;
1345 			}
1346 	}
1347 }
1348 
1349 /*
1350  * Set the basic rates in a rate set.
1351  */
1352 void
ieee80211_setbasicrates(struct ieee80211_rateset * rs,enum ieee80211_phymode mode)1353 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
1354     enum ieee80211_phymode mode)
1355 {
1356 	setbasicrates(rs, mode, 0);
1357 }
1358 
1359 /*
1360  * Add basic rates to a rate set.
1361  */
1362 void
ieee80211_addbasicrates(struct ieee80211_rateset * rs,enum ieee80211_phymode mode)1363 ieee80211_addbasicrates(struct ieee80211_rateset *rs,
1364     enum ieee80211_phymode mode)
1365 {
1366 	setbasicrates(rs, mode, 1);
1367 }
1368 
1369 /*
1370  * WME protocol support.
1371  *
1372  * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
1373  * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
1374  * Draft 2.0 Test Plan (Appendix D).
1375  *
1376  * Static/Dynamic Turbo mode settings come from Atheros.
1377  */
1378 typedef struct phyParamType {
1379 	uint8_t		aifsn;
1380 	uint8_t		logcwmin;
1381 	uint8_t		logcwmax;
1382 	uint16_t	txopLimit;
1383 	uint8_t 	acm;
1384 } paramType;
1385 
1386 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
1387 	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
1388 	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
1389 	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
1390 	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
1391 	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
1392 	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
1393 	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
1394 	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
1395 	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
1396 	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
1397 	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
1398 	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
1399 	[IEEE80211_MODE_VHT_2GHZ]	= { 3, 4,  6,  0, 0 },
1400 	[IEEE80211_MODE_VHT_5GHZ]	= { 3, 4,  6,  0, 0 },
1401 };
1402 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
1403 	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
1404 	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
1405 	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
1406 	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
1407 	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
1408 	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
1409 	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
1410 	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
1411 	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
1412 	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
1413 	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
1414 	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
1415 	[IEEE80211_MODE_VHT_2GHZ]	= { 7, 4, 10,  0, 0 },
1416 	[IEEE80211_MODE_VHT_5GHZ]	= { 7, 4, 10,  0, 0 },
1417 };
1418 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
1419 	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
1420 	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
1421 	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
1422 	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
1423 	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
1424 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
1425 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
1426 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
1427 	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
1428 	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
1429 	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
1430 	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
1431 	[IEEE80211_MODE_VHT_2GHZ]	= { 1, 3, 4,  94, 0 },
1432 	[IEEE80211_MODE_VHT_5GHZ]	= { 1, 3, 4,  94, 0 },
1433 };
1434 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
1435 	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
1436 	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
1437 	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
1438 	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
1439 	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
1440 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
1441 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
1442 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
1443 	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
1444 	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
1445 	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
1446 	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
1447 	[IEEE80211_MODE_VHT_2GHZ]	= { 1, 2, 3,  47, 0 },
1448 	[IEEE80211_MODE_VHT_5GHZ]	= { 1, 2, 3,  47, 0 },
1449 };
1450 
1451 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
1452 	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
1453 	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
1454 	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
1455 	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
1456 	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
1457 	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
1458 	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
1459 	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
1460 	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
1461 	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
1462 	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
1463 	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
1464 };
1465 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
1466 	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
1467 	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
1468 	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
1469 	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
1470 	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
1471 	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
1472 	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
1473 	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
1474 	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
1475 	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
1476 	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
1477 	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
1478 };
1479 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
1480 	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
1481 	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
1482 	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
1483 	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
1484 	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
1485 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
1486 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
1487 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
1488 	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
1489 	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
1490 	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
1491 	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
1492 };
1493 
1494 static void
_setifsparams(struct wmeParams * wmep,const paramType * phy)1495 _setifsparams(struct wmeParams *wmep, const paramType *phy)
1496 {
1497 	wmep->wmep_aifsn = phy->aifsn;
1498 	wmep->wmep_logcwmin = phy->logcwmin;
1499 	wmep->wmep_logcwmax = phy->logcwmax;
1500 	wmep->wmep_txopLimit = phy->txopLimit;
1501 }
1502 
1503 static void
setwmeparams(struct ieee80211vap * vap,const char * type,int ac,struct wmeParams * wmep,const paramType * phy)1504 setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
1505 	struct wmeParams *wmep, const paramType *phy)
1506 {
1507 	wmep->wmep_acm = phy->acm;
1508 	_setifsparams(wmep, phy);
1509 
1510 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1511 	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
1512 	    ieee80211_wme_acnames[ac], type,
1513 	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
1514 	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
1515 }
1516 
1517 static void
ieee80211_wme_initparams_locked(struct ieee80211vap * vap)1518 ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
1519 {
1520 	struct ieee80211com *ic = vap->iv_ic;
1521 	struct ieee80211_wme_state *wme = &ic->ic_wme;
1522 	const paramType *pPhyParam, *pBssPhyParam;
1523 	struct wmeParams *wmep;
1524 	enum ieee80211_phymode mode;
1525 	int i;
1526 
1527 	IEEE80211_LOCK_ASSERT(ic);
1528 
1529 	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
1530 		return;
1531 
1532 	/*
1533 	 * Clear the wme cap_info field so a qoscount from a previous
1534 	 * vap doesn't confuse later code which only parses the beacon
1535 	 * field and updates hardware when said field changes.
1536 	 * Otherwise the hardware is programmed with defaults, not what
1537 	 * the beacon actually announces.
1538 	 *
1539 	 * Note that we can't ever have 0xff as an actual value;
1540 	 * the only valid values are 0..15.
1541 	 */
1542 	wme->wme_wmeChanParams.cap_info = 0xfe;
1543 
1544 	/*
1545 	 * Select mode; we can be called early in which case we
1546 	 * always use auto mode.  We know we'll be called when
1547 	 * entering the RUN state with bsschan setup properly
1548 	 * so state will eventually get set correctly
1549 	 */
1550 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1551 		mode = ieee80211_chan2mode(ic->ic_bsschan);
1552 	else
1553 		mode = IEEE80211_MODE_AUTO;
1554 	for (i = 0; i < WME_NUM_AC; i++) {
1555 		switch (i) {
1556 		case WME_AC_BK:
1557 			pPhyParam = &phyParamForAC_BK[mode];
1558 			pBssPhyParam = &phyParamForAC_BK[mode];
1559 			break;
1560 		case WME_AC_VI:
1561 			pPhyParam = &phyParamForAC_VI[mode];
1562 			pBssPhyParam = &bssPhyParamForAC_VI[mode];
1563 			break;
1564 		case WME_AC_VO:
1565 			pPhyParam = &phyParamForAC_VO[mode];
1566 			pBssPhyParam = &bssPhyParamForAC_VO[mode];
1567 			break;
1568 		case WME_AC_BE:
1569 		default:
1570 			pPhyParam = &phyParamForAC_BE[mode];
1571 			pBssPhyParam = &bssPhyParamForAC_BE[mode];
1572 			break;
1573 		}
1574 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1575 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1576 			setwmeparams(vap, "chan", i, wmep, pPhyParam);
1577 		} else {
1578 			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
1579 		}
1580 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1581 		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
1582 	}
1583 	/* NB: check ic_bss to avoid NULL deref on initial attach */
1584 	if (vap->iv_bss != NULL) {
1585 		/*
1586 		 * Calculate aggressive mode switching threshold based
1587 		 * on beacon interval.  This doesn't need locking since
1588 		 * we're only called before entering the RUN state at
1589 		 * which point we start sending beacon frames.
1590 		 */
1591 		wme->wme_hipri_switch_thresh =
1592 			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
1593 		wme->wme_flags &= ~WME_F_AGGRMODE;
1594 		ieee80211_wme_updateparams(vap);
1595 	}
1596 }
1597 
1598 void
ieee80211_wme_initparams(struct ieee80211vap * vap)1599 ieee80211_wme_initparams(struct ieee80211vap *vap)
1600 {
1601 	struct ieee80211com *ic = vap->iv_ic;
1602 
1603 	IEEE80211_LOCK(ic);
1604 	ieee80211_wme_initparams_locked(vap);
1605 	IEEE80211_UNLOCK(ic);
1606 }
1607 
1608 /*
1609  * Update WME parameters for ourself and the BSS.
1610  */
1611 void
ieee80211_wme_updateparams_locked(struct ieee80211vap * vap)1612 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
1613 {
1614 	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
1615 	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
1616 	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
1617 	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
1618 	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
1619 	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
1620 	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
1621 	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
1622 	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
1623 	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
1624 	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
1625 	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
1626 	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
1627 	    [IEEE80211_MODE_VHT_2GHZ]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
1628 	    [IEEE80211_MODE_VHT_5GHZ]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
1629 	};
1630 	struct ieee80211com *ic = vap->iv_ic;
1631 	struct ieee80211_wme_state *wme = &ic->ic_wme;
1632 	const struct wmeParams *wmep;
1633 	struct wmeParams *chanp, *bssp;
1634 	enum ieee80211_phymode mode;
1635 	int i;
1636 	int do_aggrmode = 0;
1637 
1638        	/*
1639 	 * Set up the channel access parameters for the physical
1640 	 * device.  First populate the configured settings.
1641 	 */
1642 	for (i = 0; i < WME_NUM_AC; i++) {
1643 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
1644 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1645 		chanp->wmep_aifsn = wmep->wmep_aifsn;
1646 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1647 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1648 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1649 
1650 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
1651 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1652 		chanp->wmep_aifsn = wmep->wmep_aifsn;
1653 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1654 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1655 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1656 	}
1657 
1658 	/*
1659 	 * Select mode; we can be called early in which case we
1660 	 * always use auto mode.  We know we'll be called when
1661 	 * entering the RUN state with bsschan setup properly
1662 	 * so state will eventually get set correctly
1663 	 */
1664 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1665 		mode = ieee80211_chan2mode(ic->ic_bsschan);
1666 	else
1667 		mode = IEEE80211_MODE_AUTO;
1668 
1669 	/*
1670 	 * This implements aggressive mode as found in certain
1671 	 * vendors' AP's.  When there is significant high
1672 	 * priority (VI/VO) traffic in the BSS throttle back BE
1673 	 * traffic by using conservative parameters.  Otherwise
1674 	 * BE uses aggressive params to optimize performance of
1675 	 * legacy/non-QoS traffic.
1676 	 */
1677 
1678 	/* Hostap? Only if aggressive mode is enabled */
1679         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1680 	     (wme->wme_flags & WME_F_AGGRMODE) != 0)
1681 		do_aggrmode = 1;
1682 
1683 	/*
1684 	 * Station? Only if we're in a non-QoS BSS.
1685 	 */
1686 	else if ((vap->iv_opmode == IEEE80211_M_STA &&
1687 	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1688 		do_aggrmode = 1;
1689 
1690 	/*
1691 	 * IBSS? Only if we have WME enabled.
1692 	 */
1693 	else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1694 	    (vap->iv_flags & IEEE80211_F_WME))
1695 		do_aggrmode = 1;
1696 
1697 	/*
1698 	 * If WME is disabled on this VAP, default to aggressive mode
1699 	 * regardless of the configuration.
1700 	 */
1701 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1702 		do_aggrmode = 1;
1703 
1704 	/* XXX WDS? */
1705 
1706 	/* XXX MBSS? */
1707 
1708 	if (do_aggrmode) {
1709 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1710 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1711 
1712 		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1713 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1714 		    aggrParam[mode].logcwmin;
1715 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1716 		    aggrParam[mode].logcwmax;
1717 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1718 		    (vap->iv_flags & IEEE80211_F_BURST) ?
1719 			aggrParam[mode].txopLimit : 0;
1720 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1721 		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1722 		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1723 		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1724 		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1725 	}
1726 
1727 	/*
1728 	 * Change the contention window based on the number of associated
1729 	 * stations.  If the number of associated stations is 1 and
1730 	 * aggressive mode is enabled, lower the contention window even
1731 	 * further.
1732 	 */
1733 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1734 	    vap->iv_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1735 		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1736 		    [IEEE80211_MODE_AUTO]	= 3,
1737 		    [IEEE80211_MODE_11A]	= 3,
1738 		    [IEEE80211_MODE_11B]	= 4,
1739 		    [IEEE80211_MODE_11G]	= 3,
1740 		    [IEEE80211_MODE_FH]		= 4,
1741 		    [IEEE80211_MODE_TURBO_A]	= 3,
1742 		    [IEEE80211_MODE_TURBO_G]	= 3,
1743 		    [IEEE80211_MODE_STURBO_A]	= 3,
1744 		    [IEEE80211_MODE_HALF]	= 3,
1745 		    [IEEE80211_MODE_QUARTER]	= 3,
1746 		    [IEEE80211_MODE_11NA]	= 3,
1747 		    [IEEE80211_MODE_11NG]	= 3,
1748 		    [IEEE80211_MODE_VHT_2GHZ]	= 3,
1749 		    [IEEE80211_MODE_VHT_5GHZ]	= 3,
1750 		};
1751 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1752 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1753 
1754 		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1755 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1756 		    "update %s (chan+bss) logcwmin %u\n",
1757 		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1758 	}
1759 
1760 	/* schedule the deferred WME update */
1761 	ieee80211_runtask(ic, &vap->iv_wme_task);
1762 
1763 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1764 	    "%s: WME params updated, cap_info 0x%x\n", __func__,
1765 	    vap->iv_opmode == IEEE80211_M_STA ?
1766 		wme->wme_wmeChanParams.cap_info :
1767 		wme->wme_bssChanParams.cap_info);
1768 }
1769 
1770 void
ieee80211_wme_updateparams(struct ieee80211vap * vap)1771 ieee80211_wme_updateparams(struct ieee80211vap *vap)
1772 {
1773 	struct ieee80211com *ic = vap->iv_ic;
1774 
1775 	if (ic->ic_caps & IEEE80211_C_WME) {
1776 		IEEE80211_LOCK(ic);
1777 		ieee80211_wme_updateparams_locked(vap);
1778 		IEEE80211_UNLOCK(ic);
1779 	}
1780 }
1781 
1782 /*
1783  * Fetch the WME parameters for the given VAP.
1784  *
1785  * When net80211 grows p2p, etc support, this may return different
1786  * parameters for each VAP.
1787  */
1788 void
ieee80211_wme_vap_getparams(struct ieee80211vap * vap,struct chanAccParams * wp)1789 ieee80211_wme_vap_getparams(struct ieee80211vap *vap, struct chanAccParams *wp)
1790 {
1791 
1792 	memcpy(wp, &vap->iv_ic->ic_wme.wme_chanParams, sizeof(*wp));
1793 }
1794 
1795 /*
1796  * For NICs which only support one set of WME parameters (ie, softmac NICs)
1797  * there may be different VAP WME parameters but only one is "active".
1798  * This returns the "NIC" WME parameters for the currently active
1799  * context.
1800  */
1801 void
ieee80211_wme_ic_getparams(struct ieee80211com * ic,struct chanAccParams * wp)1802 ieee80211_wme_ic_getparams(struct ieee80211com *ic, struct chanAccParams *wp)
1803 {
1804 
1805 	memcpy(wp, &ic->ic_wme.wme_chanParams, sizeof(*wp));
1806 }
1807 
1808 /*
1809  * Return whether to use QoS on a given WME queue.
1810  *
1811  * This is intended to be called from the transmit path of softmac drivers
1812  * which are setting NoAck bits in transmit descriptors.
1813  *
1814  * Ideally this would be set in some transmit field before the packet is
1815  * queued to the driver but net80211 isn't quite there yet.
1816  */
1817 int
ieee80211_wme_vap_ac_is_noack(struct ieee80211vap * vap,int ac)1818 ieee80211_wme_vap_ac_is_noack(struct ieee80211vap *vap, int ac)
1819 {
1820 	/* Bounds/sanity check */
1821 	if (ac < 0 || ac >= WME_NUM_AC)
1822 		return (0);
1823 
1824 	/* Again, there's only one global context for now */
1825 	return (!! vap->iv_ic->ic_wme.wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy);
1826 }
1827 
1828 static void
parent_updown(void * arg,int npending)1829 parent_updown(void *arg, int npending)
1830 {
1831 	struct ieee80211com *ic = arg;
1832 
1833 	ic->ic_parent(ic);
1834 }
1835 
1836 static void
update_mcast(void * arg,int npending)1837 update_mcast(void *arg, int npending)
1838 {
1839 	struct ieee80211com *ic = arg;
1840 
1841 	ic->ic_update_mcast(ic);
1842 }
1843 
1844 static void
update_promisc(void * arg,int npending)1845 update_promisc(void *arg, int npending)
1846 {
1847 	struct ieee80211com *ic = arg;
1848 
1849 	ic->ic_update_promisc(ic);
1850 }
1851 
1852 static void
update_channel(void * arg,int npending)1853 update_channel(void *arg, int npending)
1854 {
1855 	struct ieee80211com *ic = arg;
1856 
1857 	ic->ic_set_channel(ic);
1858 	ieee80211_radiotap_chan_change(ic);
1859 }
1860 
1861 static void
update_chw(void * arg,int npending)1862 update_chw(void *arg, int npending)
1863 {
1864 	struct ieee80211com *ic = arg;
1865 
1866 	/*
1867 	 * XXX should we defer the channel width _config_ update until now?
1868 	 */
1869 	ic->ic_update_chw(ic);
1870 }
1871 
1872 /*
1873  * Deferred WME parameter and beacon update.
1874  *
1875  * In preparation for per-VAP WME configuration, call the VAP
1876  * method if the VAP requires it.  Otherwise, just call the
1877  * older global method.  There isn't a per-VAP WME configuration
1878  * just yet so for now just use the global configuration.
1879  */
1880 static void
vap_update_wme(void * arg,int npending)1881 vap_update_wme(void *arg, int npending)
1882 {
1883 	struct ieee80211vap *vap = arg;
1884 	struct ieee80211com *ic = vap->iv_ic;
1885 	struct ieee80211_wme_state *wme = &ic->ic_wme;
1886 
1887 	/* Driver update */
1888 	if (vap->iv_wme_update != NULL)
1889 		vap->iv_wme_update(vap,
1890 		    ic->ic_wme.wme_chanParams.cap_wmeParams);
1891 	else
1892 		ic->ic_wme.wme_update(ic);
1893 
1894 	IEEE80211_LOCK(ic);
1895 	/*
1896 	 * Arrange for the beacon update.
1897 	 *
1898 	 * XXX what about MBSS, WDS?
1899 	 */
1900 	if (vap->iv_opmode == IEEE80211_M_HOSTAP
1901 	    || vap->iv_opmode == IEEE80211_M_IBSS) {
1902 		/*
1903 		 * Arrange for a beacon update and bump the parameter
1904 		 * set number so associated stations load the new values.
1905 		 */
1906 		wme->wme_bssChanParams.cap_info =
1907 			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1908 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1909 	}
1910 	IEEE80211_UNLOCK(ic);
1911 }
1912 
1913 static void
restart_vaps(void * arg,int npending)1914 restart_vaps(void *arg, int npending)
1915 {
1916 	struct ieee80211com *ic = arg;
1917 
1918 	ieee80211_suspend_all(ic);
1919 	ieee80211_resume_all(ic);
1920 }
1921 
1922 /*
1923  * Block until the parent is in a known state.  This is
1924  * used after any operations that dispatch a task (e.g.
1925  * to auto-configure the parent device up/down).
1926  */
1927 void
ieee80211_waitfor_parent(struct ieee80211com * ic)1928 ieee80211_waitfor_parent(struct ieee80211com *ic)
1929 {
1930 	taskqueue_block(ic->ic_tq);
1931 	ieee80211_draintask(ic, &ic->ic_parent_task);
1932 	ieee80211_draintask(ic, &ic->ic_mcast_task);
1933 	ieee80211_draintask(ic, &ic->ic_promisc_task);
1934 	ieee80211_draintask(ic, &ic->ic_chan_task);
1935 	ieee80211_draintask(ic, &ic->ic_bmiss_task);
1936 	ieee80211_draintask(ic, &ic->ic_chw_task);
1937 	taskqueue_unblock(ic->ic_tq);
1938 }
1939 
1940 /*
1941  * Check to see whether the current channel needs reset.
1942  *
1943  * Some devices don't handle being given an invalid channel
1944  * in their operating mode very well (eg wpi(4) will throw a
1945  * firmware exception.)
1946  *
1947  * Return 0 if we're ok, 1 if the channel needs to be reset.
1948  *
1949  * See PR kern/202502.
1950  */
1951 static int
ieee80211_start_check_reset_chan(struct ieee80211vap * vap)1952 ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
1953 {
1954 	struct ieee80211com *ic = vap->iv_ic;
1955 
1956 	if ((vap->iv_opmode == IEEE80211_M_IBSS &&
1957 	     IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
1958 	    (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1959 	     IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
1960 		return (1);
1961 	return (0);
1962 }
1963 
1964 /*
1965  * Reset the curchan to a known good state.
1966  */
1967 static void
ieee80211_start_reset_chan(struct ieee80211vap * vap)1968 ieee80211_start_reset_chan(struct ieee80211vap *vap)
1969 {
1970 	struct ieee80211com *ic = vap->iv_ic;
1971 
1972 	ic->ic_curchan = &ic->ic_channels[0];
1973 }
1974 
1975 /*
1976  * Start a vap running.  If this is the first vap to be
1977  * set running on the underlying device then we
1978  * automatically bring the device up.
1979  */
1980 void
ieee80211_start_locked(struct ieee80211vap * vap)1981 ieee80211_start_locked(struct ieee80211vap *vap)
1982 {
1983 	struct ifnet *ifp = vap->iv_ifp;
1984 	struct ieee80211com *ic = vap->iv_ic;
1985 
1986 	IEEE80211_LOCK_ASSERT(ic);
1987 
1988 	IEEE80211_DPRINTF(vap,
1989 		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1990 		"start running, %d vaps running\n", ic->ic_nrunning);
1991 
1992 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1993 		/*
1994 		 * Mark us running.  Note that it's ok to do this first;
1995 		 * if we need to bring the parent device up we defer that
1996 		 * to avoid dropping the com lock.  We expect the device
1997 		 * to respond to being marked up by calling back into us
1998 		 * through ieee80211_start_all at which point we'll come
1999 		 * back in here and complete the work.
2000 		 */
2001 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
2002 		ieee80211_notify_ifnet_change(vap);
2003 
2004 		/*
2005 		 * We are not running; if this we are the first vap
2006 		 * to be brought up auto-up the parent if necessary.
2007 		 */
2008 		if (ic->ic_nrunning++ == 0) {
2009 			/* reset the channel to a known good channel */
2010 			if (ieee80211_start_check_reset_chan(vap))
2011 				ieee80211_start_reset_chan(vap);
2012 
2013 			IEEE80211_DPRINTF(vap,
2014 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2015 			    "%s: up parent %s\n", __func__, ic->ic_name);
2016 			ieee80211_runtask(ic, &ic->ic_parent_task);
2017 			return;
2018 		}
2019 	}
2020 	/*
2021 	 * If the parent is up and running, then kick the
2022 	 * 802.11 state machine as appropriate.
2023 	 */
2024 	if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
2025 		if (vap->iv_opmode == IEEE80211_M_STA) {
2026 #if 0
2027 			/* XXX bypasses scan too easily; disable for now */
2028 			/*
2029 			 * Try to be intelligent about clocking the state
2030 			 * machine.  If we're currently in RUN state then
2031 			 * we should be able to apply any new state/parameters
2032 			 * simply by re-associating.  Otherwise we need to
2033 			 * re-scan to select an appropriate ap.
2034 			 */
2035 			if (vap->iv_state >= IEEE80211_S_RUN)
2036 				ieee80211_new_state_locked(vap,
2037 				    IEEE80211_S_ASSOC, 1);
2038 			else
2039 #endif
2040 				ieee80211_new_state_locked(vap,
2041 				    IEEE80211_S_SCAN, 0);
2042 		} else {
2043 			/*
2044 			 * For monitor+wds mode there's nothing to do but
2045 			 * start running.  Otherwise if this is the first
2046 			 * vap to be brought up, start a scan which may be
2047 			 * preempted if the station is locked to a particular
2048 			 * channel.
2049 			 */
2050 			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
2051 			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
2052 			    vap->iv_opmode == IEEE80211_M_WDS)
2053 				ieee80211_new_state_locked(vap,
2054 				    IEEE80211_S_RUN, -1);
2055 			else
2056 				ieee80211_new_state_locked(vap,
2057 				    IEEE80211_S_SCAN, 0);
2058 		}
2059 	}
2060 }
2061 
2062 /*
2063  * Start a single vap.
2064  */
2065 void
ieee80211_init(void * arg)2066 ieee80211_init(void *arg)
2067 {
2068 	struct ieee80211vap *vap = arg;
2069 
2070 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2071 	    "%s\n", __func__);
2072 
2073 	IEEE80211_LOCK(vap->iv_ic);
2074 	ieee80211_start_locked(vap);
2075 	IEEE80211_UNLOCK(vap->iv_ic);
2076 }
2077 
2078 /*
2079  * Start all runnable vap's on a device.
2080  */
2081 void
ieee80211_start_all(struct ieee80211com * ic)2082 ieee80211_start_all(struct ieee80211com *ic)
2083 {
2084 	struct ieee80211vap *vap;
2085 
2086 	IEEE80211_LOCK(ic);
2087 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2088 		struct ifnet *ifp = vap->iv_ifp;
2089 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
2090 			ieee80211_start_locked(vap);
2091 	}
2092 	IEEE80211_UNLOCK(ic);
2093 }
2094 
2095 /*
2096  * Stop a vap.  We force it down using the state machine
2097  * then mark it's ifnet not running.  If this is the last
2098  * vap running on the underlying device then we close it
2099  * too to insure it will be properly initialized when the
2100  * next vap is brought up.
2101  */
2102 void
ieee80211_stop_locked(struct ieee80211vap * vap)2103 ieee80211_stop_locked(struct ieee80211vap *vap)
2104 {
2105 	struct ieee80211com *ic = vap->iv_ic;
2106 	struct ifnet *ifp = vap->iv_ifp;
2107 
2108 	IEEE80211_LOCK_ASSERT(ic);
2109 
2110 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2111 	    "stop running, %d vaps running\n", ic->ic_nrunning);
2112 
2113 	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
2114 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2115 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
2116 		ieee80211_notify_ifnet_change(vap);
2117 		if (--ic->ic_nrunning == 0) {
2118 			IEEE80211_DPRINTF(vap,
2119 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
2120 			    "down parent %s\n", ic->ic_name);
2121 			ieee80211_runtask(ic, &ic->ic_parent_task);
2122 		}
2123 	}
2124 }
2125 
2126 void
ieee80211_stop(struct ieee80211vap * vap)2127 ieee80211_stop(struct ieee80211vap *vap)
2128 {
2129 	struct ieee80211com *ic = vap->iv_ic;
2130 
2131 	IEEE80211_LOCK(ic);
2132 	ieee80211_stop_locked(vap);
2133 	IEEE80211_UNLOCK(ic);
2134 }
2135 
2136 /*
2137  * Stop all vap's running on a device.
2138  */
2139 void
ieee80211_stop_all(struct ieee80211com * ic)2140 ieee80211_stop_all(struct ieee80211com *ic)
2141 {
2142 	struct ieee80211vap *vap;
2143 
2144 	IEEE80211_LOCK(ic);
2145 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2146 		struct ifnet *ifp = vap->iv_ifp;
2147 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
2148 			ieee80211_stop_locked(vap);
2149 	}
2150 	IEEE80211_UNLOCK(ic);
2151 
2152 	ieee80211_waitfor_parent(ic);
2153 }
2154 
2155 /*
2156  * Stop all vap's running on a device and arrange
2157  * for those that were running to be resumed.
2158  */
2159 void
ieee80211_suspend_all(struct ieee80211com * ic)2160 ieee80211_suspend_all(struct ieee80211com *ic)
2161 {
2162 	struct ieee80211vap *vap;
2163 
2164 	IEEE80211_LOCK(ic);
2165 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2166 		struct ifnet *ifp = vap->iv_ifp;
2167 		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
2168 			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
2169 			ieee80211_stop_locked(vap);
2170 		}
2171 	}
2172 	IEEE80211_UNLOCK(ic);
2173 
2174 	ieee80211_waitfor_parent(ic);
2175 }
2176 
2177 /*
2178  * Start all vap's marked for resume.
2179  */
2180 void
ieee80211_resume_all(struct ieee80211com * ic)2181 ieee80211_resume_all(struct ieee80211com *ic)
2182 {
2183 	struct ieee80211vap *vap;
2184 
2185 	IEEE80211_LOCK(ic);
2186 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2187 		struct ifnet *ifp = vap->iv_ifp;
2188 		if (!IFNET_IS_UP_RUNNING(ifp) &&
2189 		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
2190 			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
2191 			ieee80211_start_locked(vap);
2192 		}
2193 	}
2194 	IEEE80211_UNLOCK(ic);
2195 }
2196 
2197 /*
2198  * Restart all vap's running on a device.
2199  */
2200 void
ieee80211_restart_all(struct ieee80211com * ic)2201 ieee80211_restart_all(struct ieee80211com *ic)
2202 {
2203 	/*
2204 	 * NB: do not use ieee80211_runtask here, we will
2205 	 * block & drain net80211 taskqueue.
2206 	 */
2207 	taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
2208 }
2209 
2210 void
ieee80211_beacon_miss(struct ieee80211com * ic)2211 ieee80211_beacon_miss(struct ieee80211com *ic)
2212 {
2213 	IEEE80211_LOCK(ic);
2214 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2215 		/* Process in a taskq, the handler may reenter the driver */
2216 		ieee80211_runtask(ic, &ic->ic_bmiss_task);
2217 	}
2218 	IEEE80211_UNLOCK(ic);
2219 }
2220 
2221 static void
beacon_miss(void * arg,int npending)2222 beacon_miss(void *arg, int npending)
2223 {
2224 	struct ieee80211com *ic = arg;
2225 	struct ieee80211vap *vap;
2226 
2227 	IEEE80211_LOCK(ic);
2228 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2229 		/*
2230 		 * We only pass events through for sta vap's in RUN+ state;
2231 		 * may be too restrictive but for now this saves all the
2232 		 * handlers duplicating these checks.
2233 		 */
2234 		if (vap->iv_opmode == IEEE80211_M_STA &&
2235 		    vap->iv_state >= IEEE80211_S_RUN &&
2236 		    vap->iv_bmiss != NULL)
2237 			vap->iv_bmiss(vap);
2238 	}
2239 	IEEE80211_UNLOCK(ic);
2240 }
2241 
2242 static void
beacon_swmiss(void * arg,int npending)2243 beacon_swmiss(void *arg, int npending)
2244 {
2245 	struct ieee80211vap *vap = arg;
2246 	struct ieee80211com *ic = vap->iv_ic;
2247 
2248 	IEEE80211_LOCK(ic);
2249 	if (vap->iv_state >= IEEE80211_S_RUN) {
2250 		/* XXX Call multiple times if npending > zero? */
2251 		vap->iv_bmiss(vap);
2252 	}
2253 	IEEE80211_UNLOCK(ic);
2254 }
2255 
2256 /*
2257  * Software beacon miss handling.  Check if any beacons
2258  * were received in the last period.  If not post a
2259  * beacon miss; otherwise reset the counter.
2260  */
2261 void
ieee80211_swbmiss(void * arg)2262 ieee80211_swbmiss(void *arg)
2263 {
2264 	struct ieee80211vap *vap = arg;
2265 	struct ieee80211com *ic = vap->iv_ic;
2266 
2267 	IEEE80211_LOCK_ASSERT(ic);
2268 
2269 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
2270 	    ("wrong state %d", vap->iv_state));
2271 
2272 	if (ic->ic_flags & IEEE80211_F_SCAN) {
2273 		/*
2274 		 * If scanning just ignore and reset state.  If we get a
2275 		 * bmiss after coming out of scan because we haven't had
2276 		 * time to receive a beacon then we should probe the AP
2277 		 * before posting a real bmiss (unless iv_bmiss_max has
2278 		 * been artifiically lowered).  A cleaner solution might
2279 		 * be to disable the timer on scan start/end but to handle
2280 		 * case of multiple sta vap's we'd need to disable the
2281 		 * timers of all affected vap's.
2282 		 */
2283 		vap->iv_swbmiss_count = 0;
2284 	} else if (vap->iv_swbmiss_count == 0) {
2285 		if (vap->iv_bmiss != NULL)
2286 			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
2287 	} else
2288 		vap->iv_swbmiss_count = 0;
2289 	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
2290 		ieee80211_swbmiss, vap);
2291 }
2292 
2293 /*
2294  * Start an 802.11h channel switch.  We record the parameters,
2295  * mark the operation pending, notify each vap through the
2296  * beacon update mechanism so it can update the beacon frame
2297  * contents, and then switch vap's to CSA state to block outbound
2298  * traffic.  Devices that handle CSA directly can use the state
2299  * switch to do the right thing so long as they call
2300  * ieee80211_csa_completeswitch when it's time to complete the
2301  * channel change.  Devices that depend on the net80211 layer can
2302  * use ieee80211_beacon_update to handle the countdown and the
2303  * channel switch.
2304  */
2305 void
ieee80211_csa_startswitch(struct ieee80211com * ic,struct ieee80211_channel * c,int mode,int count)2306 ieee80211_csa_startswitch(struct ieee80211com *ic,
2307 	struct ieee80211_channel *c, int mode, int count)
2308 {
2309 	struct ieee80211vap *vap;
2310 
2311 	IEEE80211_LOCK_ASSERT(ic);
2312 
2313 	ic->ic_csa_newchan = c;
2314 	ic->ic_csa_mode = mode;
2315 	ic->ic_csa_count = count;
2316 	ic->ic_flags |= IEEE80211_F_CSAPENDING;
2317 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2318 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2319 		    vap->iv_opmode == IEEE80211_M_IBSS ||
2320 		    vap->iv_opmode == IEEE80211_M_MBSS)
2321 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
2322 		/* switch to CSA state to block outbound traffic */
2323 		if (vap->iv_state == IEEE80211_S_RUN)
2324 			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
2325 	}
2326 	ieee80211_notify_csa(ic, c, mode, count);
2327 }
2328 
2329 /*
2330  * Complete the channel switch by transitioning all CSA VAPs to RUN.
2331  * This is called by both the completion and cancellation functions
2332  * so each VAP is placed back in the RUN state and can thus transmit.
2333  */
2334 static void
csa_completeswitch(struct ieee80211com * ic)2335 csa_completeswitch(struct ieee80211com *ic)
2336 {
2337 	struct ieee80211vap *vap;
2338 
2339 	ic->ic_csa_newchan = NULL;
2340 	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
2341 
2342 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2343 		if (vap->iv_state == IEEE80211_S_CSA)
2344 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
2345 }
2346 
2347 /*
2348  * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
2349  * We clear state and move all vap's in CSA state to RUN state
2350  * so they can again transmit.
2351  *
2352  * Although this may not be completely correct, update the BSS channel
2353  * for each VAP to the newly configured channel. The setcurchan sets
2354  * the current operating channel for the interface (so the radio does
2355  * switch over) but the VAP BSS isn't updated, leading to incorrectly
2356  * reported information via ioctl.
2357  */
2358 void
ieee80211_csa_completeswitch(struct ieee80211com * ic)2359 ieee80211_csa_completeswitch(struct ieee80211com *ic)
2360 {
2361 	struct ieee80211vap *vap;
2362 
2363 	IEEE80211_LOCK_ASSERT(ic);
2364 
2365 	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
2366 
2367 	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
2368 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2369 		if (vap->iv_state == IEEE80211_S_CSA)
2370 			vap->iv_bss->ni_chan = ic->ic_curchan;
2371 
2372 	csa_completeswitch(ic);
2373 }
2374 
2375 /*
2376  * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
2377  * We clear state and move all vap's in CSA state to RUN state
2378  * so they can again transmit.
2379  */
2380 void
ieee80211_csa_cancelswitch(struct ieee80211com * ic)2381 ieee80211_csa_cancelswitch(struct ieee80211com *ic)
2382 {
2383 	IEEE80211_LOCK_ASSERT(ic);
2384 
2385 	csa_completeswitch(ic);
2386 }
2387 
2388 /*
2389  * Complete a DFS CAC started by ieee80211_dfs_cac_start.
2390  * We clear state and move all vap's in CAC state to RUN state.
2391  */
2392 void
ieee80211_cac_completeswitch(struct ieee80211vap * vap0)2393 ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
2394 {
2395 	struct ieee80211com *ic = vap0->iv_ic;
2396 	struct ieee80211vap *vap;
2397 
2398 	IEEE80211_LOCK(ic);
2399 	/*
2400 	 * Complete CAC state change for lead vap first; then
2401 	 * clock all the other vap's waiting.
2402 	 */
2403 	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
2404 	    ("wrong state %d", vap0->iv_state));
2405 	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
2406 
2407 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2408 		if (vap->iv_state == IEEE80211_S_CAC && vap != vap0)
2409 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
2410 	IEEE80211_UNLOCK(ic);
2411 }
2412 
2413 /*
2414  * Force all vap's other than the specified vap to the INIT state
2415  * and mark them as waiting for a scan to complete.  These vaps
2416  * will be brought up when the scan completes and the scanning vap
2417  * reaches RUN state by wakeupwaiting.
2418  */
2419 static void
markwaiting(struct ieee80211vap * vap0)2420 markwaiting(struct ieee80211vap *vap0)
2421 {
2422 	struct ieee80211com *ic = vap0->iv_ic;
2423 	struct ieee80211vap *vap;
2424 
2425 	IEEE80211_LOCK_ASSERT(ic);
2426 
2427 	/*
2428 	 * A vap list entry can not disappear since we are running on the
2429 	 * taskqueue and a vap destroy will queue and drain another state
2430 	 * change task.
2431 	 */
2432 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2433 		if (vap == vap0)
2434 			continue;
2435 		if (vap->iv_state != IEEE80211_S_INIT) {
2436 			/* NB: iv_newstate may drop the lock */
2437 			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
2438 			IEEE80211_LOCK_ASSERT(ic);
2439 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2440 		}
2441 	}
2442 }
2443 
2444 /*
2445  * Wakeup all vap's waiting for a scan to complete.  This is the
2446  * companion to markwaiting (above) and is used to coordinate
2447  * multiple vaps scanning.
2448  * This is called from the state taskqueue.
2449  */
2450 static void
wakeupwaiting(struct ieee80211vap * vap0)2451 wakeupwaiting(struct ieee80211vap *vap0)
2452 {
2453 	struct ieee80211com *ic = vap0->iv_ic;
2454 	struct ieee80211vap *vap;
2455 
2456 	IEEE80211_LOCK_ASSERT(ic);
2457 
2458 	/*
2459 	 * A vap list entry can not disappear since we are running on the
2460 	 * taskqueue and a vap destroy will queue and drain another state
2461 	 * change task.
2462 	 */
2463 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2464 		if (vap == vap0)
2465 			continue;
2466 		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
2467 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2468 			/* NB: sta's cannot go INIT->RUN */
2469 			/* NB: iv_newstate may drop the lock */
2470 
2471 			/*
2472 			 * This is problematic if the interface has OACTIVE
2473 			 * set.  Only the deferred ieee80211_newstate_cb()
2474 			 * will end up actually /clearing/ the OACTIVE
2475 			 * flag on a state transition to RUN from a non-RUN
2476 			 * state.
2477 			 *
2478 			 * But, we're not actually deferring this callback;
2479 			 * and when the deferred call occurs it shows up as
2480 			 * a RUN->RUN transition!  So the flag isn't/wasn't
2481 			 * cleared!
2482 			 *
2483 			 * I'm also not sure if it's correct to actually
2484 			 * do the transitions here fully through the deferred
2485 			 * paths either as other things can be invoked as
2486 			 * part of that state machine.
2487 			 *
2488 			 * So just keep this in mind when looking at what
2489 			 * the markwaiting/wakeupwaiting routines are doing
2490 			 * and how they invoke vap state changes.
2491 			 */
2492 
2493 			vap->iv_newstate(vap,
2494 			    vap->iv_opmode == IEEE80211_M_STA ?
2495 			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
2496 			IEEE80211_LOCK_ASSERT(ic);
2497 		}
2498 	}
2499 }
2500 
2501 static int
_ieee80211_newstate_get_next_empty_slot(struct ieee80211vap * vap)2502 _ieee80211_newstate_get_next_empty_slot(struct ieee80211vap *vap)
2503 {
2504 	int nstate_num;
2505 
2506 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2507 
2508 	if (vap->iv_nstate_n >= NET80211_IV_NSTATE_NUM)
2509 		return (-1);
2510 
2511 	nstate_num = vap->iv_nstate_b + vap->iv_nstate_n;
2512 	nstate_num %= NET80211_IV_NSTATE_NUM;
2513 	vap->iv_nstate_n++;
2514 
2515 	return (nstate_num);
2516 }
2517 
2518 static int
_ieee80211_newstate_get_next_pending_slot(struct ieee80211vap * vap)2519 _ieee80211_newstate_get_next_pending_slot(struct ieee80211vap *vap)
2520 {
2521 	int nstate_num;
2522 
2523 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2524 
2525 	KASSERT(vap->iv_nstate_n > 0, ("%s: vap %p iv_nstate_n %d\n",
2526 	    __func__, vap, vap->iv_nstate_n));
2527 
2528 	nstate_num = vap->iv_nstate_b;
2529 	vap->iv_nstate_b++;
2530 	if (vap->iv_nstate_b >= NET80211_IV_NSTATE_NUM)
2531 		vap->iv_nstate_b = 0;
2532 	vap->iv_nstate_n--;
2533 
2534 	return (nstate_num);
2535 }
2536 
2537 static int
_ieee80211_newstate_get_npending(struct ieee80211vap * vap)2538 _ieee80211_newstate_get_npending(struct ieee80211vap *vap)
2539 {
2540 
2541 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2542 
2543 	return (vap->iv_nstate_n);
2544 }
2545 
2546 /*
2547  * Handle post state change work common to all operating modes.
2548  */
2549 static void
ieee80211_newstate_cb(void * xvap,int npending)2550 ieee80211_newstate_cb(void *xvap, int npending)
2551 {
2552 	struct ieee80211vap *vap = xvap;
2553 	struct ieee80211com *ic = vap->iv_ic;
2554 	enum ieee80211_state nstate, ostate;
2555 	int arg, rc, nstate_num;
2556 
2557 	KASSERT(npending == 1, ("%s: vap %p with npending %d != 1\n",
2558 	    __func__, vap, npending));
2559 	IEEE80211_LOCK(ic);
2560 	nstate_num = _ieee80211_newstate_get_next_pending_slot(vap);
2561 
2562 	/*
2563 	 * Update the historic fields for now as they are used in some
2564 	 * drivers and reduce code changes for now.
2565 	 */
2566 	vap->iv_nstate = nstate = vap->iv_nstates[nstate_num];
2567 	arg = vap->iv_nstate_args[nstate_num];
2568 
2569 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2570 	    "%s:%d: running state update %s -> %s (%d)\n",
2571 	    __func__, __LINE__,
2572 	    ieee80211_state_name[vap->iv_state],
2573 	    ieee80211_state_name[nstate],
2574 	    npending);
2575 
2576 	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
2577 		/*
2578 		 * We have been requested to drop back to the INIT before
2579 		 * proceeding to the new state.
2580 		 */
2581 		/* Deny any state changes while we are here. */
2582 		vap->iv_nstate = IEEE80211_S_INIT;
2583 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2584 		    "%s: %s -> %s arg %d -> %s arg %d\n", __func__,
2585 		    ieee80211_state_name[vap->iv_state],
2586 		    ieee80211_state_name[vap->iv_nstate], 0,
2587 		    ieee80211_state_name[nstate], arg);
2588 		vap->iv_newstate(vap, vap->iv_nstate, 0);
2589 		IEEE80211_LOCK_ASSERT(ic);
2590 		vap->iv_flags_ext &= ~(IEEE80211_FEXT_REINIT |
2591 		    IEEE80211_FEXT_STATEWAIT);
2592 		/* enqueue new state transition after cancel_scan() task */
2593 		ieee80211_new_state_locked(vap, nstate, arg);
2594 		goto done;
2595 	}
2596 
2597 	ostate = vap->iv_state;
2598 	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
2599 		/*
2600 		 * SCAN was forced; e.g. on beacon miss.  Force other running
2601 		 * vap's to INIT state and mark them as waiting for the scan to
2602 		 * complete.  This insures they don't interfere with our
2603 		 * scanning.  Since we are single threaded the vaps can not
2604 		 * transition again while we are executing.
2605 		 *
2606 		 * XXX not always right, assumes ap follows sta
2607 		 */
2608 		markwaiting(vap);
2609 	}
2610 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2611 	    "%s: %s -> %s arg %d\n", __func__,
2612 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
2613 
2614 	rc = vap->iv_newstate(vap, nstate, arg);
2615 	IEEE80211_LOCK_ASSERT(ic);
2616 	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
2617 	if (rc != 0) {
2618 		/* State transition failed */
2619 		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
2620 		KASSERT(nstate != IEEE80211_S_INIT,
2621 		    ("INIT state change failed"));
2622 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2623 		    "%s: %s returned error %d\n", __func__,
2624 		    ieee80211_state_name[nstate], rc);
2625 		goto done;
2626 	}
2627 
2628 	/*
2629 	 * Handle the case of a RUN->RUN transition occuring when STA + AP
2630 	 * VAPs occur on the same radio.
2631 	 *
2632 	 * The mark and wakeup waiting routines call iv_newstate() directly,
2633 	 * but they do not end up deferring state changes here.
2634 	 * Thus, although the VAP newstate method sees a transition
2635 	 * of RUN->INIT->RUN, the deferred path here only sees a RUN->RUN
2636 	 * transition.  If OACTIVE is set then it is never cleared.
2637 	 *
2638 	 * So, if we're here and the state is RUN, just clear OACTIVE.
2639 	 * At some point if the markwaiting/wakeupwaiting paths end up
2640 	 * also invoking the deferred state updates then this will
2641 	 * be no-op code - and also if OACTIVE is finally retired, it'll
2642 	 * also be no-op code.
2643 	 */
2644 	if (nstate == IEEE80211_S_RUN) {
2645 		/*
2646 		 * OACTIVE may be set on the vap if the upper layer
2647 		 * tried to transmit (e.g. IPv6 NDP) before we reach
2648 		 * RUN state.  Clear it and restart xmit.
2649 		 *
2650 		 * Note this can also happen as a result of SLEEP->RUN
2651 		 * (i.e. coming out of power save mode).
2652 		 *
2653 		 * Historically this was done only for a state change
2654 		 * but is needed earlier; see next comment.  The 2nd half
2655 		 * of the work is still only done in case of an actual
2656 		 * state change below.
2657 		 */
2658 		/*
2659 		 * Unblock the VAP queue; a RUN->RUN state can happen
2660 		 * on a STA+AP setup on the AP vap.  See wakeupwaiting().
2661 		 */
2662 		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2663 
2664 		/*
2665 		 * XXX TODO Kick-start a VAP queue - this should be a method!
2666 		 */
2667 	}
2668 
2669 	/* No actual transition, skip post processing */
2670 	if (ostate == nstate)
2671 		goto done;
2672 
2673 	if (nstate == IEEE80211_S_RUN) {
2674 
2675 		/* bring up any vaps waiting on us */
2676 		wakeupwaiting(vap);
2677 	} else if (nstate == IEEE80211_S_INIT) {
2678 		/*
2679 		 * Flush the scan cache if we did the last scan (XXX?)
2680 		 * and flush any frames on send queues from this vap.
2681 		 * Note the mgt q is used only for legacy drivers and
2682 		 * will go away shortly.
2683 		 */
2684 		ieee80211_scan_flush(vap);
2685 
2686 		/*
2687 		 * XXX TODO: ic/vap queue flush
2688 		 */
2689 	}
2690 done:
2691 	IEEE80211_UNLOCK(ic);
2692 }
2693 
2694 /*
2695  * Public interface for initiating a state machine change.
2696  * This routine single-threads the request and coordinates
2697  * the scheduling of multiple vaps for the purpose of selecting
2698  * an operating channel.  Specifically the following scenarios
2699  * are handled:
2700  * o only one vap can be selecting a channel so on transition to
2701  *   SCAN state if another vap is already scanning then
2702  *   mark the caller for later processing and return without
2703  *   doing anything (XXX? expectations by caller of synchronous operation)
2704  * o only one vap can be doing CAC of a channel so on transition to
2705  *   CAC state if another vap is already scanning for radar then
2706  *   mark the caller for later processing and return without
2707  *   doing anything (XXX? expectations by caller of synchronous operation)
2708  * o if another vap is already running when a request is made
2709  *   to SCAN then an operating channel has been chosen; bypass
2710  *   the scan and just join the channel
2711  *
2712  * Note that the state change call is done through the iv_newstate
2713  * method pointer so any driver routine gets invoked.  The driver
2714  * will normally call back into operating mode-specific
2715  * ieee80211_newstate routines (below) unless it needs to completely
2716  * bypass the state machine (e.g. because the firmware has it's
2717  * own idea how things should work).  Bypassing the net80211 layer
2718  * is usually a mistake and indicates lack of proper integration
2719  * with the net80211 layer.
2720  */
2721 int
ieee80211_new_state_locked(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2722 ieee80211_new_state_locked(struct ieee80211vap *vap,
2723 	enum ieee80211_state nstate, int arg)
2724 {
2725 	struct ieee80211com *ic = vap->iv_ic;
2726 	struct ieee80211vap *vp;
2727 	enum ieee80211_state ostate;
2728 	int nrunning, nscanning, nstate_num;
2729 
2730 	IEEE80211_LOCK_ASSERT(ic);
2731 
2732 	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
2733 		if (vap->iv_nstate == IEEE80211_S_INIT ||
2734 		    ((vap->iv_state == IEEE80211_S_INIT ||
2735 		    (vap->iv_flags_ext & IEEE80211_FEXT_REINIT)) &&
2736 		    vap->iv_nstate == IEEE80211_S_SCAN &&
2737 		    nstate > IEEE80211_S_SCAN)) {
2738 			/*
2739 			 * XXX The vap is being stopped/started,
2740 			 * do not allow any other state changes
2741 			 * until this is completed.
2742 			 */
2743 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2744 			    "%s:%d: %s -> %s (%s) transition discarded\n",
2745 			    __func__, __LINE__,
2746 			    ieee80211_state_name[vap->iv_state],
2747 			    ieee80211_state_name[nstate],
2748 			    ieee80211_state_name[vap->iv_nstate]);
2749 			return -1;
2750 		}
2751 	}
2752 
2753 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2754 	    "%s:%d: starting state update %s -> %s (%s)\n",
2755 	    __func__, __LINE__,
2756 	    ieee80211_state_name[vap->iv_state],
2757 	    ieee80211_state_name[vap->iv_nstate],
2758 	    ieee80211_state_name[nstate]);
2759 
2760 	nrunning = nscanning = 0;
2761 	/* XXX can track this state instead of calculating */
2762 	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
2763 		if (vp != vap) {
2764 			if (vp->iv_state >= IEEE80211_S_RUN)
2765 				nrunning++;
2766 			/* XXX doesn't handle bg scan */
2767 			/* NB: CAC+AUTH+ASSOC treated like SCAN */
2768 			else if (vp->iv_state > IEEE80211_S_INIT)
2769 				nscanning++;
2770 		}
2771 	}
2772 	/*
2773 	 * Look ahead for the "old state" at that point when the last queued
2774 	 * state transition is run.
2775 	 */
2776 	if (vap->iv_nstate_n == 0) {
2777 		ostate = vap->iv_state;
2778 	} else {
2779 		nstate_num = (vap->iv_nstate_b + vap->iv_nstate_n - 1) % NET80211_IV_NSTATE_NUM;
2780 		ostate = vap->iv_nstates[nstate_num];
2781 	}
2782 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2783 	    "%s: %s -> %s (arg %d) (nrunning %d nscanning %d)\n", __func__,
2784 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg,
2785 	    nrunning, nscanning);
2786 	switch (nstate) {
2787 	case IEEE80211_S_SCAN:
2788 		if (ostate == IEEE80211_S_INIT) {
2789 			/*
2790 			 * INIT -> SCAN happens on initial bringup.
2791 			 */
2792 			KASSERT(!(nscanning && nrunning),
2793 			    ("%d scanning and %d running", nscanning, nrunning));
2794 			if (nscanning) {
2795 				/*
2796 				 * Someone is scanning, defer our state
2797 				 * change until the work has completed.
2798 				 */
2799 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2800 				    "%s: defer %s -> %s\n",
2801 				    __func__, ieee80211_state_name[ostate],
2802 				    ieee80211_state_name[nstate]);
2803 				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2804 				return 0;
2805 			}
2806 			if (nrunning) {
2807 				/*
2808 				 * Someone is operating; just join the channel
2809 				 * they have chosen.
2810 				 */
2811 				/* XXX kill arg? */
2812 				/* XXX check each opmode, adhoc? */
2813 				if (vap->iv_opmode == IEEE80211_M_STA)
2814 					nstate = IEEE80211_S_SCAN;
2815 				else
2816 					nstate = IEEE80211_S_RUN;
2817 #ifdef IEEE80211_DEBUG
2818 				if (nstate != IEEE80211_S_SCAN) {
2819 					IEEE80211_DPRINTF(vap,
2820 					    IEEE80211_MSG_STATE,
2821 					    "%s: override, now %s -> %s\n",
2822 					    __func__,
2823 					    ieee80211_state_name[ostate],
2824 					    ieee80211_state_name[nstate]);
2825 				}
2826 #endif
2827 			}
2828 		}
2829 		break;
2830 	case IEEE80211_S_RUN:
2831 		if (vap->iv_opmode == IEEE80211_M_WDS &&
2832 		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
2833 		    nscanning) {
2834 			/*
2835 			 * Legacy WDS with someone else scanning; don't
2836 			 * go online until that completes as we should
2837 			 * follow the other vap to the channel they choose.
2838 			 */
2839 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2840 			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
2841 			     ieee80211_state_name[ostate],
2842 			     ieee80211_state_name[nstate]);
2843 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2844 			return 0;
2845 		}
2846 		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
2847 		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2848 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
2849 		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
2850 			/*
2851 			 * This is a DFS channel, transition to CAC state
2852 			 * instead of RUN.  This allows us to initiate
2853 			 * Channel Availability Check (CAC) as specified
2854 			 * by 11h/DFS.
2855 			 */
2856 			nstate = IEEE80211_S_CAC;
2857 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2858 			     "%s: override %s -> %s (DFS)\n", __func__,
2859 			     ieee80211_state_name[ostate],
2860 			     ieee80211_state_name[nstate]);
2861 		}
2862 		break;
2863 	case IEEE80211_S_INIT:
2864 		/* cancel any scan in progress */
2865 		ieee80211_cancel_scan(vap);
2866 		if (ostate == IEEE80211_S_INIT ) {
2867 			/* XXX don't believe this */
2868 			/* INIT -> INIT. nothing to do */
2869 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2870 		}
2871 		/* fall thru... */
2872 	default:
2873 		break;
2874 	}
2875 	/*
2876 	 * Defer the state change to a thread.
2877 	 * We support up-to NET80211_IV_NSTATE_NUM pending state changes
2878 	 * using a separate task for each. Otherwise, if we enqueue
2879 	 * more than one state change they will be folded together,
2880 	 * npedning will be > 1 and we may run then out of sequence with
2881 	 * other events.
2882 	 * This is kind-of a hack after 10 years but we know how to provoke
2883 	 * these cases now (and seen them in the wild).
2884 	 */
2885 	nstate_num = _ieee80211_newstate_get_next_empty_slot(vap);
2886 	if (nstate_num == -1) {
2887 		/*
2888 		 * This is really bad and we should just go kaboom.
2889 		 * Instead drop it.  No one checks the return code anyway.
2890 		 */
2891 		ic_printf(ic, "%s:%d: pending %s -> %s (now to %s) "
2892 		    "transition lost. %d/%d pending state changes:\n",
2893 		    __func__, __LINE__,
2894 		    ieee80211_state_name[vap->iv_state],
2895 		    ieee80211_state_name[vap->iv_nstate],
2896 		    ieee80211_state_name[nstate],
2897 		    _ieee80211_newstate_get_npending(vap),
2898 		    NET80211_IV_NSTATE_NUM);
2899 
2900 		return (EAGAIN);
2901 	}
2902 	vap->iv_nstates[nstate_num] = nstate;
2903 	vap->iv_nstate_args[nstate_num] = arg;
2904 	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
2905 	ieee80211_runtask(ic, &vap->iv_nstate_task[nstate_num]);
2906 	return EINPROGRESS;
2907 }
2908 
2909 int
ieee80211_new_state(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2910 ieee80211_new_state(struct ieee80211vap *vap,
2911 	enum ieee80211_state nstate, int arg)
2912 {
2913 	struct ieee80211com *ic = vap->iv_ic;
2914 	int rc;
2915 
2916 	IEEE80211_LOCK(ic);
2917 	rc = ieee80211_new_state_locked(vap, nstate, arg);
2918 	IEEE80211_UNLOCK(ic);
2919 	return rc;
2920 }
2921