ieee80211_proto.c revision 1.34.14.3 1 /* $NetBSD: ieee80211_proto.c,v 1.34.14.3 2018/07/16 20:11:11 phil Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 2001 Atsushi Onoe
7 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
8 * Copyright (c) 2012 IEEE
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #if __FreeBSD__
34 __FBSDID("$FreeBSD$");
35 #endif
36
37 /*
38 * IEEE 802.11 protocol support.
39 */
40
41 #include "opt_inet.h"
42 #include "opt_wlan.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #ifdef __NetBSD__
49 #include <sys/mbuf.h>
50 #endif
51
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54
55 #include <net/if.h>
56 #if __FreeBSD__
57 #include <net/if_var.h>
58 #endif
59 #include <net/if_media.h>
60 #if __FreeBSD__
61 #include <net/ethernet.h> /* XXX for ether_sprintf */
62 #endif
63 #ifdef __NetBSD__
64 #include <net/if_ether.h>
65 #include <net/route.h>
66 #endif
67
68 #include <net80211/ieee80211_var.h>
69 #include <net80211/ieee80211_adhoc.h>
70 #include <net80211/ieee80211_sta.h>
71 #include <net80211/ieee80211_hostap.h>
72 #include <net80211/ieee80211_wds.h>
73 #ifdef IEEE80211_SUPPORT_MESH
74 #include <net80211/ieee80211_mesh.h>
75 #endif
76 #include <net80211/ieee80211_monitor.h>
77 #include <net80211/ieee80211_input.h>
78
79 #ifdef __NetBSD__
80 #undef KASSERT
81 #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
82 #endif
83
84 #if __NetBSD__
85 extern const struct ieee80211_authenticator auth_xauth;
86 #endif
87
88 /* XXX tunables */
89 #define AGGRESSIVE_MODE_SWITCH_HYSTERESIS 3 /* pkts / 100ms */
90 #define HIGH_PRI_SWITCH_THRESH 10 /* pkts / 100ms */
91
92 const char *mgt_subtype_name[] = {
93 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp",
94 "probe_req", "probe_resp", "timing_adv", "reserved#7",
95 "beacon", "atim", "disassoc", "auth",
96 "deauth", "action", "action_noack", "reserved#15"
97 };
98 const char *ctl_subtype_name[] = {
99 "reserved#0", "reserved#1", "reserved#2", "reserved#3",
100 "reserved#4", "reserved#5", "reserved#6", "control_wrap",
101 "bar", "ba", "ps_poll", "rts",
102 "cts", "ack", "cf_end", "cf_end_ack"
103 };
104 const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
105 "IBSS", /* IEEE80211_M_IBSS */
106 "STA", /* IEEE80211_M_STA */
107 "WDS", /* IEEE80211_M_WDS */
108 "AHDEMO", /* IEEE80211_M_AHDEMO */
109 "HOSTAP", /* IEEE80211_M_HOSTAP */
110 "MONITOR", /* IEEE80211_M_MONITOR */
111 "MBSS" /* IEEE80211_M_MBSS */
112 };
113 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
114 "INIT", /* IEEE80211_S_INIT */
115 "SCAN", /* IEEE80211_S_SCAN */
116 "AUTH", /* IEEE80211_S_AUTH */
117 "ASSOC", /* IEEE80211_S_ASSOC */
118 "CAC", /* IEEE80211_S_CAC */
119 "RUN", /* IEEE80211_S_RUN */
120 "CSA", /* IEEE80211_S_CSA */
121 "SLEEP", /* IEEE80211_S_SLEEP */
122 };
123 const char *ieee80211_wme_acnames[] = {
124 "WME_AC_BE",
125 "WME_AC_BK",
126 "WME_AC_VI",
127 "WME_AC_VO",
128 "WME_UPSD",
129 };
130
131
132 /*
133 * Reason code descriptions were (mostly) obtained from
134 * IEEE Std 802.11-2012, pp. 442-445 Table 8-36.
135 */
136 const char *
137 ieee80211_reason_to_string(uint16_t reason)
138 {
139 switch (reason) {
140 case IEEE80211_REASON_UNSPECIFIED:
141 return ("unspecified");
142 case IEEE80211_REASON_AUTH_EXPIRE:
143 return ("previous authentication is expired");
144 case IEEE80211_REASON_AUTH_LEAVE:
145 return ("sending STA is leaving/has left IBSS or ESS");
146 case IEEE80211_REASON_ASSOC_EXPIRE:
147 return ("disassociated due to inactivity");
148 case IEEE80211_REASON_ASSOC_TOOMANY:
149 return ("too many associated STAs");
150 case IEEE80211_REASON_NOT_AUTHED:
151 return ("class 2 frame received from nonauthenticated STA");
152 case IEEE80211_REASON_NOT_ASSOCED:
153 return ("class 3 frame received from nonassociated STA");
154 case IEEE80211_REASON_ASSOC_LEAVE:
155 return ("sending STA is leaving/has left BSS");
156 case IEEE80211_REASON_ASSOC_NOT_AUTHED:
157 return ("STA requesting (re)association is not authenticated");
158 case IEEE80211_REASON_DISASSOC_PWRCAP_BAD:
159 return ("information in the Power Capability element is "
160 "unacceptable");
161 case IEEE80211_REASON_DISASSOC_SUPCHAN_BAD:
162 return ("information in the Supported Channels element is "
163 "unacceptable");
164 case IEEE80211_REASON_IE_INVALID:
165 return ("invalid element");
166 case IEEE80211_REASON_MIC_FAILURE:
167 return ("MIC failure");
168 case IEEE80211_REASON_4WAY_HANDSHAKE_TIMEOUT:
169 return ("4-Way handshake timeout");
170 case IEEE80211_REASON_GROUP_KEY_UPDATE_TIMEOUT:
171 return ("group key update timeout");
172 case IEEE80211_REASON_IE_IN_4WAY_DIFFERS:
173 return ("element in 4-Way handshake different from "
174 "(re)association request/probe response/beacon frame");
175 case IEEE80211_REASON_GROUP_CIPHER_INVALID:
176 return ("invalid group cipher");
177 case IEEE80211_REASON_PAIRWISE_CIPHER_INVALID:
178 return ("invalid pairwise cipher");
179 case IEEE80211_REASON_AKMP_INVALID:
180 return ("invalid AKMP");
181 case IEEE80211_REASON_UNSUPP_RSN_IE_VERSION:
182 return ("unsupported version in RSN IE");
183 case IEEE80211_REASON_INVALID_RSN_IE_CAP:
184 return ("invalid capabilities in RSN IE");
185 case IEEE80211_REASON_802_1X_AUTH_FAILED:
186 return ("IEEE 802.1X authentication failed");
187 case IEEE80211_REASON_CIPHER_SUITE_REJECTED:
188 return ("cipher suite rejected because of the security "
189 "policy");
190 case IEEE80211_REASON_UNSPECIFIED_QOS:
191 return ("unspecified (QoS-related)");
192 case IEEE80211_REASON_INSUFFICIENT_BW:
193 return ("QoS AP lacks sufficient bandwidth for this QoS STA");
194 case IEEE80211_REASON_TOOMANY_FRAMES:
195 return ("too many frames need to be acknowledged");
196 case IEEE80211_REASON_OUTSIDE_TXOP:
197 return ("STA is transmitting outside the limits of its TXOPs");
198 case IEEE80211_REASON_LEAVING_QBSS:
199 return ("requested from peer STA (the STA is "
200 "resetting/leaving the BSS)");
201 case IEEE80211_REASON_BAD_MECHANISM:
202 return ("requested from peer STA (it does not want to use "
203 "the mechanism)");
204 case IEEE80211_REASON_SETUP_NEEDED:
205 return ("requested from peer STA (setup is required for the "
206 "used mechanism)");
207 case IEEE80211_REASON_TIMEOUT:
208 return ("requested from peer STA (timeout)");
209 case IEEE80211_REASON_PEER_LINK_CANCELED:
210 return ("SME cancels the mesh peering instance (not related "
211 "to the maximum number of peer mesh STAs)");
212 case IEEE80211_REASON_MESH_MAX_PEERS:
213 return ("maximum number of peer mesh STAs was reached");
214 case IEEE80211_REASON_MESH_CPVIOLATION:
215 return ("the received information violates the Mesh "
216 "Configuration policy configured in the mesh STA "
217 "profile");
218 case IEEE80211_REASON_MESH_CLOSE_RCVD:
219 return ("the mesh STA has received a Mesh Peering Close "
220 "message requesting to close the mesh peering");
221 case IEEE80211_REASON_MESH_MAX_RETRIES:
222 return ("the mesh STA has resent dot11MeshMaxRetries Mesh "
223 "Peering Open messages, without receiving a Mesh "
224 "Peering Confirm message");
225 case IEEE80211_REASON_MESH_CONFIRM_TIMEOUT:
226 return ("the confirmTimer for the mesh peering instance times "
227 "out");
228 case IEEE80211_REASON_MESH_INVALID_GTK:
229 return ("the mesh STA fails to unwrap the GTK or the values "
230 "in the wrapped contents do not match");
231 case IEEE80211_REASON_MESH_INCONS_PARAMS:
232 return ("the mesh STA receives inconsistent information about "
233 "the mesh parameters between Mesh Peering Management "
234 "frames");
235 case IEEE80211_REASON_MESH_INVALID_SECURITY:
236 return ("the mesh STA fails the authenticated mesh peering "
237 "exchange because due to failure in selecting "
238 "pairwise/group ciphersuite");
239 case IEEE80211_REASON_MESH_PERR_NO_PROXY:
240 return ("the mesh STA does not have proxy information for "
241 "this external destination");
242 case IEEE80211_REASON_MESH_PERR_NO_FI:
243 return ("the mesh STA does not have forwarding information "
244 "for this destination");
245 case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
246 return ("the mesh STA determines that the link to the next "
247 "hop of an active path in its forwarding information "
248 "is no longer usable");
249 case IEEE80211_REASON_MESH_MAC_ALRDY_EXISTS_MBSS:
250 return ("the MAC address of the STA already exists in the "
251 "mesh BSS");
252 case IEEE80211_REASON_MESH_CHAN_SWITCH_REG:
253 return ("the mesh STA performs channel switch to meet "
254 "regulatory requirements");
255 case IEEE80211_REASON_MESH_CHAN_SWITCH_UNSPEC:
256 return ("the mesh STA performs channel switch with "
257 "unspecified reason");
258 default:
259 return ("reserved/unknown");
260 }
261 }
262
263 static void beacon_miss(void *, int);
264 static void beacon_swmiss(void *, int);
265 static void parent_updown(void *, int);
266 static void update_mcast(void *, int);
267 static void update_promisc(void *, int);
268 static void update_channel(void *, int);
269 static void update_chw(void *, int);
270 static void vap_update_wme(void *, int);
271 static void restart_vaps(void *, int);
272 static void ieee80211_newstate_cb(void *, int);
273
274 static int
275 null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
276 const struct ieee80211_bpf_params *params)
277 {
278
279 ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
280 m_freem(m);
281 return ENETDOWN;
282 }
283
284 void
285 ieee80211_proto_attach(struct ieee80211com *ic)
286 {
287 uint8_t hdrlen;
288
289 /* override the 802.3 setting */
290 hdrlen = ic->ic_headroom
291 + sizeof(struct ieee80211_qosframe_addr4)
292 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
293 + IEEE80211_WEP_EXTIVLEN;
294 /* XXX no way to recalculate on ifdetach */
295 if (ALIGN(hdrlen) > max_linkhdr) {
296 /* XXX sanity check... */
297 max_linkhdr = ALIGN(hdrlen);
298 max_hdr = max_linkhdr + max_protohdr;
299 max_datalen = MHLEN - max_hdr;
300 }
301 ic->ic_protmode = IEEE80211_PROT_CTSONLY;
302
303 TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
304 TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
305 TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
306 TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
307 TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
308 TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
309 TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
310
311 ic->ic_wme.wme_hipri_switch_hysteresis =
312 AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
313
314 /* initialize management frame handlers */
315 ic->ic_send_mgmt = ieee80211_send_mgmt;
316 ic->ic_raw_xmit = null_raw_xmit;
317
318 ieee80211_adhoc_attach(ic);
319 ieee80211_sta_attach(ic);
320 ieee80211_wds_attach(ic);
321 ieee80211_hostap_attach(ic);
322 #ifdef IEEE80211_SUPPORT_MESH
323 ieee80211_mesh_attach(ic);
324 #endif
325 ieee80211_monitor_attach(ic);
326 }
327
328 void
329 ieee80211_proto_detach(struct ieee80211com *ic)
330 {
331 ieee80211_monitor_detach(ic);
332 #ifdef IEEE80211_SUPPORT_MESH
333 ieee80211_mesh_detach(ic);
334 #endif
335 ieee80211_hostap_detach(ic);
336 ieee80211_wds_detach(ic);
337 ieee80211_adhoc_detach(ic);
338 ieee80211_sta_detach(ic);
339 }
340
341 static void
342 null_update_beacon(struct ieee80211vap *vap, int item)
343 {
344 }
345
346 void
347 ieee80211_proto_vattach(struct ieee80211vap *vap)
348 {
349 struct ieee80211com *ic = vap->iv_ic;
350 struct ifnet *ifp = vap->iv_ifp;
351 int i;
352
353 /* override the 802.3 setting */
354 ifp->if_hdrlen = ic->ic_headroom
355 + sizeof(struct ieee80211_qosframe_addr4)
356 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
357 + IEEE80211_WEP_EXTIVLEN;
358
359 vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
360 vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
361 vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
362 #if __FreeBSD__
363 callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
364 callout_init(&vap->iv_mgtsend, 1);
365 #elif __NetBSD__
366 /* NNN need to do something with iv_swbmiss ... */
367 callout_init(&vap->iv_mgtsend, CALLOUT_MPSAFE);
368 #endif
369 TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
370 TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
371 TASK_INIT(&vap->iv_wme_task, 0, vap_update_wme, vap);
372 /*
373 * Install default tx rate handling: no fixed rate, lowest
374 * supported rate for mgmt and multicast frames. Default
375 * max retry count. These settings can be changed by the
376 * driver and/or user applications.
377 */
378 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
379 const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
380
381 vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
382
383 /*
384 * Setting the management rate to MCS 0 assumes that the
385 * BSS Basic rate set is empty and the BSS Basic MCS set
386 * is not.
387 *
388 * Since we're not checking this, default to the lowest
389 * defined rate for this mode.
390 *
391 * At least one 11n AP (DLINK DIR-825) is reported to drop
392 * some MCS management traffic (eg BA response frames.)
393 *
394 * See also: 9.6.0 of the 802.11n-2009 specification.
395 */
396 #ifdef NOTYET
397 if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
398 vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
399 vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
400 } else {
401 vap->iv_txparms[i].mgmtrate =
402 rs->rs_rates[0] & IEEE80211_RATE_VAL;
403 vap->iv_txparms[i].mcastrate =
404 rs->rs_rates[0] & IEEE80211_RATE_VAL;
405 }
406 #endif
407 vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
408 vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
409 vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
410 }
411 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
412
413 vap->iv_update_beacon = null_update_beacon;
414 vap->iv_deliver_data = ieee80211_deliver_data;
415
416 /* attach support for operating mode */
417 ic->ic_vattach[vap->iv_opmode](vap);
418 }
419
420 void
421 ieee80211_proto_vdetach(struct ieee80211vap *vap)
422 {
423 #define FREEAPPIE(ie) do { \
424 if (ie != NULL) \
425 IEEE80211_FREE(ie, M_80211_NODE_IE); \
426 } while (0)
427 /*
428 * Detach operating mode module.
429 */
430 if (vap->iv_opdetach != NULL)
431 vap->iv_opdetach(vap);
432 /*
433 * This should not be needed as we detach when reseting
434 * the state but be conservative here since the
435 * authenticator may do things like spawn kernel threads.
436 */
437 if (vap->iv_auth->ia_detach != NULL)
438 vap->iv_auth->ia_detach(vap);
439 /*
440 * Detach any ACL'ator.
441 */
442 if (vap->iv_acl != NULL)
443 vap->iv_acl->iac_detach(vap);
444
445 FREEAPPIE(vap->iv_appie_beacon);
446 FREEAPPIE(vap->iv_appie_probereq);
447 FREEAPPIE(vap->iv_appie_proberesp);
448 FREEAPPIE(vap->iv_appie_assocreq);
449 FREEAPPIE(vap->iv_appie_assocresp);
450 FREEAPPIE(vap->iv_appie_wpa);
451 #undef FREEAPPIE
452 }
453
454 /*
455 * Simple-minded authenticator module support.
456 */
457
458 #define IEEE80211_AUTH_MAX (IEEE80211_AUTH_WPA+1)
459 /* XXX well-known names */
460 #if __FreeBSD__
461 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
462 "wlan_internal", /* IEEE80211_AUTH_NONE */
463 "wlan_internal", /* IEEE80211_AUTH_OPEN */
464 "wlan_internal", /* IEEE80211_AUTH_SHARED */
465 "wlan_xauth", /* IEEE80211_AUTH_8021X */
466 "wlan_internal", /* IEEE80211_AUTH_AUTO */
467 "wlan_xauth", /* IEEE80211_AUTH_WPA */
468 };
469 #endif
470
471 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
472
473 static const struct ieee80211_authenticator auth_internal = {
474 .ia_name = "wlan_internal",
475 .ia_attach = NULL,
476 .ia_detach = NULL,
477 .ia_node_join = NULL,
478 .ia_node_leave = NULL,
479 };
480
481
482 /*
483 * Setup internal authenticators once; they are never unregistered.
484 */
485 #if __FreeBSD__
486 static void
487 #elif __NetBSD__
488 void
489 #endif
490 ieee80211_auth_setup(void)
491 {
492 ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
493 ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
494 ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
495 #if __NetBSD__
496 ieee80211_authenticator_register(IEEE80211_AUTH_8021X, &auth_xauth);
497 ieee80211_authenticator_register(IEEE80211_AUTH_WPA, &auth_xauth);
498 #endif
499 }
500 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
501
502 const struct ieee80211_authenticator *
503 ieee80211_authenticator_get(int auth)
504 {
505 if (auth >= IEEE80211_AUTH_MAX)
506 return NULL;
507 #if __FreeBSD__
508 if (authenticators[auth] == NULL)
509 ieee80211_load_module(auth_modnames[auth]);
510 #endif
511 return authenticators[auth];
512 }
513
514 void
515 ieee80211_authenticator_register(int type,
516 const struct ieee80211_authenticator *auth)
517 {
518 if (type >= IEEE80211_AUTH_MAX)
519 return;
520 authenticators[type] = auth;
521 }
522
523 void
524 ieee80211_authenticator_unregister(int type)
525 {
526
527 if (type >= IEEE80211_AUTH_MAX)
528 return;
529 authenticators[type] = NULL;
530 }
531
532 /*
533 * Very simple-minded ACL module support.
534 */
535 /* XXX just one for now */
536 static const struct ieee80211_aclator *acl = NULL;
537
538 void
539 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
540 {
541 printf("wlan: %s acl policy registered\n", iac->iac_name);
542 acl = iac;
543 }
544
545 void
546 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
547 {
548 if (acl == iac)
549 acl = NULL;
550 printf("wlan: %s acl policy unregistered\n", iac->iac_name);
551 }
552
553 const struct ieee80211_aclator *
554 ieee80211_aclator_get(const char *name)
555 {
556 #if __FreeBSD__
557 if (acl == NULL)
558 ieee80211_load_module("wlan_acl");
559 #endif
560 return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
561 }
562
563 void
564 ieee80211_print_essid(const uint8_t *essid, int len)
565 {
566 const uint8_t *p;
567 int i;
568
569 if (len > IEEE80211_NWID_LEN)
570 len = IEEE80211_NWID_LEN;
571 /* determine printable or not */
572 for (i = 0, p = essid; i < len; i++, p++) {
573 if (*p < ' ' || *p > 0x7e)
574 break;
575 }
576 if (i == len) {
577 printf("\"");
578 for (i = 0, p = essid; i < len; i++, p++)
579 printf("%c", *p);
580 printf("\"");
581 } else {
582 printf("0x");
583 for (i = 0, p = essid; i < len; i++, p++)
584 printf("%02x", *p);
585 }
586 }
587
588 void
589 ieee80211_dump_pkt(struct ieee80211com *ic,
590 const uint8_t *buf, int len, int rate, int rssi)
591 {
592 const struct ieee80211_frame *wh;
593 int i;
594
595 wh = (const struct ieee80211_frame *)buf;
596 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
597 case IEEE80211_FC1_DIR_NODS:
598 printf("NODS %s", ether_sprintf(wh->i_addr2));
599 printf("->%s", ether_sprintf(wh->i_addr1));
600 printf("(%s)", ether_sprintf(wh->i_addr3));
601 break;
602 case IEEE80211_FC1_DIR_TODS:
603 printf("TODS %s", ether_sprintf(wh->i_addr2));
604 printf("->%s", ether_sprintf(wh->i_addr3));
605 printf("(%s)", ether_sprintf(wh->i_addr1));
606 break;
607 case IEEE80211_FC1_DIR_FROMDS:
608 printf("FRDS %s", ether_sprintf(wh->i_addr3));
609 printf("->%s", ether_sprintf(wh->i_addr1));
610 printf("(%s)", ether_sprintf(wh->i_addr2));
611 break;
612 case IEEE80211_FC1_DIR_DSTODS:
613 printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
614 printf("->%s", ether_sprintf(wh->i_addr3));
615 printf("(%s", ether_sprintf(wh->i_addr2));
616 printf("->%s)", ether_sprintf(wh->i_addr1));
617 break;
618 }
619 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
620 case IEEE80211_FC0_TYPE_DATA:
621 printf(" data");
622 break;
623 case IEEE80211_FC0_TYPE_MGT:
624 printf(" %s", ieee80211_mgt_subtype_name(wh->i_fc[0]));
625 break;
626 default:
627 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
628 break;
629 }
630 if (IEEE80211_QOS_HAS_SEQ(wh)) {
631 const struct ieee80211_qosframe *qwh =
632 (const struct ieee80211_qosframe *)buf;
633 printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
634 qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
635 }
636 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
637 int off;
638
639 off = ieee80211_anyhdrspace(ic, wh);
640 printf(" WEP [IV %.02x %.02x %.02x",
641 buf[off+0], buf[off+1], buf[off+2]);
642 if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
643 printf(" %.02x %.02x %.02x",
644 buf[off+4], buf[off+5], buf[off+6]);
645 printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
646 }
647 if (rate >= 0)
648 printf(" %dM", rate / 2);
649 if (rssi >= 0)
650 printf(" +%d", rssi);
651 printf("\n");
652 if (len > 0) {
653 for (i = 0; i < len; i++) {
654 if ((i & 1) == 0)
655 printf(" ");
656 printf("%02x", buf[i]);
657 }
658 printf("\n");
659 }
660 }
661
662 static __inline int
663 findrix(const struct ieee80211_rateset *rs, int r)
664 {
665 int i;
666
667 for (i = 0; i < rs->rs_nrates; i++)
668 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
669 return i;
670 return -1;
671 }
672
673 int
674 ieee80211_fix_rate(struct ieee80211_node *ni,
675 struct ieee80211_rateset *nrs, int flags)
676 {
677 struct ieee80211vap *vap = ni->ni_vap;
678 struct ieee80211com *ic = ni->ni_ic;
679 int i, j, rix, error;
680 int okrate, badrate, fixedrate, ucastrate;
681 const struct ieee80211_rateset *srs;
682 uint8_t r;
683
684 error = 0;
685 okrate = badrate = 0;
686 ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
687 if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
688 /*
689 * Workaround awkwardness with fixed rate. We are called
690 * to check both the legacy rate set and the HT rate set
691 * but we must apply any legacy fixed rate check only to the
692 * legacy rate set and vice versa. We cannot tell what type
693 * of rate set we've been given (legacy or HT) but we can
694 * distinguish the fixed rate type (MCS have 0x80 set).
695 * So to deal with this the caller communicates whether to
696 * check MCS or legacy rate using the flags and we use the
697 * type of any fixed rate to avoid applying an MCS to a
698 * legacy rate and vice versa.
699 */
700 if (ucastrate & 0x80) {
701 if (flags & IEEE80211_F_DOFRATE)
702 flags &= ~IEEE80211_F_DOFRATE;
703 } else if ((ucastrate & 0x80) == 0) {
704 if (flags & IEEE80211_F_DOFMCS)
705 flags &= ~IEEE80211_F_DOFMCS;
706 }
707 /* NB: required to make MCS match below work */
708 ucastrate &= IEEE80211_RATE_VAL;
709 }
710 fixedrate = IEEE80211_FIXED_RATE_NONE;
711 /*
712 * XXX we are called to process both MCS and legacy rates;
713 * we must use the appropriate basic rate set or chaos will
714 * ensue; for now callers that want MCS must supply
715 * IEEE80211_F_DOBRS; at some point we'll need to split this
716 * function so there are two variants, one for MCS and one
717 * for legacy rates.
718 */
719 if (flags & IEEE80211_F_DOBRS)
720 srs = (const struct ieee80211_rateset *)
721 ieee80211_get_suphtrates(ic, ni->ni_chan);
722 else
723 srs = ieee80211_get_suprates(ic, ni->ni_chan);
724 for (i = 0; i < nrs->rs_nrates; ) {
725 if (flags & IEEE80211_F_DOSORT) {
726 /*
727 * Sort rates.
728 */
729 for (j = i + 1; j < nrs->rs_nrates; j++) {
730 if (IEEE80211_RV(nrs->rs_rates[i]) >
731 IEEE80211_RV(nrs->rs_rates[j])) {
732 r = nrs->rs_rates[i];
733 nrs->rs_rates[i] = nrs->rs_rates[j];
734 nrs->rs_rates[j] = r;
735 }
736 }
737 }
738 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
739 badrate = r;
740 /*
741 * Check for fixed rate.
742 */
743 if (r == ucastrate)
744 fixedrate = r;
745 /*
746 * Check against supported rates.
747 */
748 rix = findrix(srs, r);
749 if (flags & IEEE80211_F_DONEGO) {
750 if (rix < 0) {
751 /*
752 * A rate in the node's rate set is not
753 * supported. If this is a basic rate and we
754 * are operating as a STA then this is an error.
755 * Otherwise we just discard/ignore the rate.
756 */
757 if ((flags & IEEE80211_F_JOIN) &&
758 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
759 error++;
760 } else if ((flags & IEEE80211_F_JOIN) == 0) {
761 /*
762 * Overwrite with the supported rate
763 * value so any basic rate bit is set.
764 */
765 nrs->rs_rates[i] = srs->rs_rates[rix];
766 }
767 }
768 if ((flags & IEEE80211_F_DODEL) && rix < 0) {
769 /*
770 * Delete unacceptable rates.
771 */
772 nrs->rs_nrates--;
773 for (j = i; j < nrs->rs_nrates; j++)
774 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
775 nrs->rs_rates[j] = 0;
776 continue;
777 }
778 if (rix >= 0)
779 okrate = nrs->rs_rates[i];
780 i++;
781 }
782 if (okrate == 0 || error != 0 ||
783 ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
784 fixedrate != ucastrate)) {
785 IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
786 "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
787 "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
788 return badrate | IEEE80211_RATE_BASIC;
789 } else
790 return IEEE80211_RV(okrate);
791 }
792
793 /*
794 * Reset 11g-related state.
795 */
796 void
797 ieee80211_reset_erp(struct ieee80211com *ic)
798 {
799 ic->ic_flags &= ~IEEE80211_F_USEPROT;
800 ic->ic_nonerpsta = 0;
801 ic->ic_longslotsta = 0;
802 /*
803 * Short slot time is enabled only when operating in 11g
804 * and not in an IBSS. We must also honor whether or not
805 * the driver is capable of doing it.
806 */
807 ieee80211_set_shortslottime(ic,
808 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
809 IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
810 (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
811 ic->ic_opmode == IEEE80211_M_HOSTAP &&
812 (ic->ic_caps & IEEE80211_C_SHSLOT)));
813 /*
814 * Set short preamble and ERP barker-preamble flags.
815 */
816 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
817 (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
818 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
819 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
820 } else {
821 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
822 ic->ic_flags |= IEEE80211_F_USEBARKER;
823 }
824 }
825
826 /*
827 * Set the short slot time state and notify the driver.
828 */
829 void
830 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
831 {
832 if (onoff)
833 ic->ic_flags |= IEEE80211_F_SHSLOT;
834 else
835 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
836 /* notify driver */
837 if (ic->ic_updateslot != NULL)
838 ic->ic_updateslot(ic);
839 }
840
841 /*
842 * Check if the specified rate set supports ERP.
843 * NB: the rate set is assumed to be sorted.
844 */
845 int
846 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
847 {
848 static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
849 int i, j;
850
851 if (rs->rs_nrates < nitems(rates))
852 return 0;
853 for (i = 0; i < nitems(rates); i++) {
854 for (j = 0; j < rs->rs_nrates; j++) {
855 int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
856 if (rates[i] == r)
857 goto next;
858 if (r > rates[i])
859 return 0;
860 }
861 return 0;
862 next:
863 ;
864 }
865 return 1;
866 }
867
868 /*
869 * Mark the basic rates for the rate table based on the
870 * operating mode. For real 11g we mark all the 11b rates
871 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
872 * 11b rates. There's also a pseudo 11a-mode used to mark only
873 * the basic OFDM rates.
874 */
875 static void
876 setbasicrates(struct ieee80211_rateset *rs,
877 enum ieee80211_phymode mode, int add)
878 {
879 static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
880 [IEEE80211_MODE_11A] = { 3, { 12, 24, 48 } },
881 [IEEE80211_MODE_11B] = { 2, { 2, 4 } },
882 /* NB: mixed b/g */
883 [IEEE80211_MODE_11G] = { 4, { 2, 4, 11, 22 } },
884 [IEEE80211_MODE_TURBO_A] = { 3, { 12, 24, 48 } },
885 [IEEE80211_MODE_TURBO_G] = { 4, { 2, 4, 11, 22 } },
886 [IEEE80211_MODE_STURBO_A] = { 3, { 12, 24, 48 } },
887 [IEEE80211_MODE_HALF] = { 3, { 6, 12, 24 } },
888 [IEEE80211_MODE_QUARTER] = { 3, { 3, 6, 12 } },
889 [IEEE80211_MODE_11NA] = { 3, { 12, 24, 48 } },
890 /* NB: mixed b/g */
891 [IEEE80211_MODE_11NG] = { 4, { 2, 4, 11, 22 } },
892 /* NB: mixed b/g */
893 [IEEE80211_MODE_VHT_2GHZ] = { 4, { 2, 4, 11, 22 } },
894 [IEEE80211_MODE_VHT_5GHZ] = { 3, { 12, 24, 48 } },
895 };
896 int i, j;
897
898 for (i = 0; i < rs->rs_nrates; i++) {
899 if (!add)
900 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
901 for (j = 0; j < basic[mode].rs_nrates; j++)
902 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
903 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
904 break;
905 }
906 }
907 }
908
909 /*
910 * Set the basic rates in a rate set.
911 */
912 void
913 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
914 enum ieee80211_phymode mode)
915 {
916 setbasicrates(rs, mode, 0);
917 }
918
919 /*
920 * Add basic rates to a rate set.
921 */
922 void
923 ieee80211_addbasicrates(struct ieee80211_rateset *rs,
924 enum ieee80211_phymode mode)
925 {
926 setbasicrates(rs, mode, 1);
927 }
928
929 /*
930 * WME protocol support.
931 *
932 * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
933 * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
934 * Draft 2.0 Test Plan (Appendix D).
935 *
936 * Static/Dynamic Turbo mode settings come from Atheros.
937 */
938 typedef struct phyParamType {
939 uint8_t aifsn;
940 uint8_t logcwmin;
941 uint8_t logcwmax;
942 uint16_t txopLimit;
943 uint8_t acm;
944 } paramType;
945
946 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
947 [IEEE80211_MODE_AUTO] = { 3, 4, 6, 0, 0 },
948 [IEEE80211_MODE_11A] = { 3, 4, 6, 0, 0 },
949 [IEEE80211_MODE_11B] = { 3, 4, 6, 0, 0 },
950 [IEEE80211_MODE_11G] = { 3, 4, 6, 0, 0 },
951 [IEEE80211_MODE_FH] = { 3, 4, 6, 0, 0 },
952 [IEEE80211_MODE_TURBO_A]= { 2, 3, 5, 0, 0 },
953 [IEEE80211_MODE_TURBO_G]= { 2, 3, 5, 0, 0 },
954 [IEEE80211_MODE_STURBO_A]={ 2, 3, 5, 0, 0 },
955 [IEEE80211_MODE_HALF] = { 3, 4, 6, 0, 0 },
956 [IEEE80211_MODE_QUARTER]= { 3, 4, 6, 0, 0 },
957 [IEEE80211_MODE_11NA] = { 3, 4, 6, 0, 0 },
958 [IEEE80211_MODE_11NG] = { 3, 4, 6, 0, 0 },
959 [IEEE80211_MODE_VHT_2GHZ] = { 3, 4, 6, 0, 0 },
960 [IEEE80211_MODE_VHT_5GHZ] = { 3, 4, 6, 0, 0 },
961 };
962 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
963 [IEEE80211_MODE_AUTO] = { 7, 4, 10, 0, 0 },
964 [IEEE80211_MODE_11A] = { 7, 4, 10, 0, 0 },
965 [IEEE80211_MODE_11B] = { 7, 4, 10, 0, 0 },
966 [IEEE80211_MODE_11G] = { 7, 4, 10, 0, 0 },
967 [IEEE80211_MODE_FH] = { 7, 4, 10, 0, 0 },
968 [IEEE80211_MODE_TURBO_A]= { 7, 3, 10, 0, 0 },
969 [IEEE80211_MODE_TURBO_G]= { 7, 3, 10, 0, 0 },
970 [IEEE80211_MODE_STURBO_A]={ 7, 3, 10, 0, 0 },
971 [IEEE80211_MODE_HALF] = { 7, 4, 10, 0, 0 },
972 [IEEE80211_MODE_QUARTER]= { 7, 4, 10, 0, 0 },
973 [IEEE80211_MODE_11NA] = { 7, 4, 10, 0, 0 },
974 [IEEE80211_MODE_11NG] = { 7, 4, 10, 0, 0 },
975 [IEEE80211_MODE_VHT_2GHZ] = { 7, 4, 10, 0, 0 },
976 [IEEE80211_MODE_VHT_5GHZ] = { 7, 4, 10, 0, 0 },
977 };
978 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
979 [IEEE80211_MODE_AUTO] = { 1, 3, 4, 94, 0 },
980 [IEEE80211_MODE_11A] = { 1, 3, 4, 94, 0 },
981 [IEEE80211_MODE_11B] = { 1, 3, 4, 188, 0 },
982 [IEEE80211_MODE_11G] = { 1, 3, 4, 94, 0 },
983 [IEEE80211_MODE_FH] = { 1, 3, 4, 188, 0 },
984 [IEEE80211_MODE_TURBO_A]= { 1, 2, 3, 94, 0 },
985 [IEEE80211_MODE_TURBO_G]= { 1, 2, 3, 94, 0 },
986 [IEEE80211_MODE_STURBO_A]={ 1, 2, 3, 94, 0 },
987 [IEEE80211_MODE_HALF] = { 1, 3, 4, 94, 0 },
988 [IEEE80211_MODE_QUARTER]= { 1, 3, 4, 94, 0 },
989 [IEEE80211_MODE_11NA] = { 1, 3, 4, 94, 0 },
990 [IEEE80211_MODE_11NG] = { 1, 3, 4, 94, 0 },
991 [IEEE80211_MODE_VHT_2GHZ] = { 1, 3, 4, 94, 0 },
992 [IEEE80211_MODE_VHT_5GHZ] = { 1, 3, 4, 94, 0 },
993 };
994 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
995 [IEEE80211_MODE_AUTO] = { 1, 2, 3, 47, 0 },
996 [IEEE80211_MODE_11A] = { 1, 2, 3, 47, 0 },
997 [IEEE80211_MODE_11B] = { 1, 2, 3, 102, 0 },
998 [IEEE80211_MODE_11G] = { 1, 2, 3, 47, 0 },
999 [IEEE80211_MODE_FH] = { 1, 2, 3, 102, 0 },
1000 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
1001 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
1002 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
1003 [IEEE80211_MODE_HALF] = { 1, 2, 3, 47, 0 },
1004 [IEEE80211_MODE_QUARTER]= { 1, 2, 3, 47, 0 },
1005 [IEEE80211_MODE_11NA] = { 1, 2, 3, 47, 0 },
1006 [IEEE80211_MODE_11NG] = { 1, 2, 3, 47, 0 },
1007 [IEEE80211_MODE_VHT_2GHZ] = { 1, 2, 3, 47, 0 },
1008 [IEEE80211_MODE_VHT_5GHZ] = { 1, 2, 3, 47, 0 },
1009 };
1010
1011 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
1012 [IEEE80211_MODE_AUTO] = { 3, 4, 10, 0, 0 },
1013 [IEEE80211_MODE_11A] = { 3, 4, 10, 0, 0 },
1014 [IEEE80211_MODE_11B] = { 3, 4, 10, 0, 0 },
1015 [IEEE80211_MODE_11G] = { 3, 4, 10, 0, 0 },
1016 [IEEE80211_MODE_FH] = { 3, 4, 10, 0, 0 },
1017 [IEEE80211_MODE_TURBO_A]= { 2, 3, 10, 0, 0 },
1018 [IEEE80211_MODE_TURBO_G]= { 2, 3, 10, 0, 0 },
1019 [IEEE80211_MODE_STURBO_A]={ 2, 3, 10, 0, 0 },
1020 [IEEE80211_MODE_HALF] = { 3, 4, 10, 0, 0 },
1021 [IEEE80211_MODE_QUARTER]= { 3, 4, 10, 0, 0 },
1022 [IEEE80211_MODE_11NA] = { 3, 4, 10, 0, 0 },
1023 [IEEE80211_MODE_11NG] = { 3, 4, 10, 0, 0 },
1024 };
1025 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
1026 [IEEE80211_MODE_AUTO] = { 2, 3, 4, 94, 0 },
1027 [IEEE80211_MODE_11A] = { 2, 3, 4, 94, 0 },
1028 [IEEE80211_MODE_11B] = { 2, 3, 4, 188, 0 },
1029 [IEEE80211_MODE_11G] = { 2, 3, 4, 94, 0 },
1030 [IEEE80211_MODE_FH] = { 2, 3, 4, 188, 0 },
1031 [IEEE80211_MODE_TURBO_A]= { 2, 2, 3, 94, 0 },
1032 [IEEE80211_MODE_TURBO_G]= { 2, 2, 3, 94, 0 },
1033 [IEEE80211_MODE_STURBO_A]={ 2, 2, 3, 94, 0 },
1034 [IEEE80211_MODE_HALF] = { 2, 3, 4, 94, 0 },
1035 [IEEE80211_MODE_QUARTER]= { 2, 3, 4, 94, 0 },
1036 [IEEE80211_MODE_11NA] = { 2, 3, 4, 94, 0 },
1037 [IEEE80211_MODE_11NG] = { 2, 3, 4, 94, 0 },
1038 };
1039 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
1040 [IEEE80211_MODE_AUTO] = { 2, 2, 3, 47, 0 },
1041 [IEEE80211_MODE_11A] = { 2, 2, 3, 47, 0 },
1042 [IEEE80211_MODE_11B] = { 2, 2, 3, 102, 0 },
1043 [IEEE80211_MODE_11G] = { 2, 2, 3, 47, 0 },
1044 [IEEE80211_MODE_FH] = { 2, 2, 3, 102, 0 },
1045 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
1046 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
1047 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
1048 [IEEE80211_MODE_HALF] = { 2, 2, 3, 47, 0 },
1049 [IEEE80211_MODE_QUARTER]= { 2, 2, 3, 47, 0 },
1050 [IEEE80211_MODE_11NA] = { 2, 2, 3, 47, 0 },
1051 [IEEE80211_MODE_11NG] = { 2, 2, 3, 47, 0 },
1052 };
1053
1054 static void
1055 _setifsparams(struct wmeParams *wmep, const paramType *phy)
1056 {
1057 wmep->wmep_aifsn = phy->aifsn;
1058 wmep->wmep_logcwmin = phy->logcwmin;
1059 wmep->wmep_logcwmax = phy->logcwmax;
1060 wmep->wmep_txopLimit = phy->txopLimit;
1061 }
1062
1063 static void
1064 setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
1065 struct wmeParams *wmep, const paramType *phy)
1066 {
1067 wmep->wmep_acm = phy->acm;
1068 _setifsparams(wmep, phy);
1069
1070 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1071 "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
1072 ieee80211_wme_acnames[ac], type,
1073 wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
1074 wmep->wmep_logcwmax, wmep->wmep_txopLimit);
1075 }
1076
1077 static void
1078 ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
1079 {
1080 struct ieee80211com *ic = vap->iv_ic;
1081 struct ieee80211_wme_state *wme = &ic->ic_wme;
1082 const paramType *pPhyParam, *pBssPhyParam;
1083 struct wmeParams *wmep;
1084 enum ieee80211_phymode mode;
1085 int i;
1086
1087 IEEE80211_LOCK_ASSERT(ic);
1088
1089 if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
1090 return;
1091
1092 /*
1093 * Clear the wme cap_info field so a qoscount from a previous
1094 * vap doesn't confuse later code which only parses the beacon
1095 * field and updates hardware when said field changes.
1096 * Otherwise the hardware is programmed with defaults, not what
1097 * the beacon actually announces.
1098 */
1099 wme->wme_wmeChanParams.cap_info = 0;
1100
1101 /*
1102 * Select mode; we can be called early in which case we
1103 * always use auto mode. We know we'll be called when
1104 * entering the RUN state with bsschan setup properly
1105 * so state will eventually get set correctly
1106 */
1107 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1108 mode = ieee80211_chan2mode(ic->ic_bsschan);
1109 else
1110 mode = IEEE80211_MODE_AUTO;
1111 for (i = 0; i < WME_NUM_AC; i++) {
1112 switch (i) {
1113 case WME_AC_BK:
1114 pPhyParam = &phyParamForAC_BK[mode];
1115 pBssPhyParam = &phyParamForAC_BK[mode];
1116 break;
1117 case WME_AC_VI:
1118 pPhyParam = &phyParamForAC_VI[mode];
1119 pBssPhyParam = &bssPhyParamForAC_VI[mode];
1120 break;
1121 case WME_AC_VO:
1122 pPhyParam = &phyParamForAC_VO[mode];
1123 pBssPhyParam = &bssPhyParamForAC_VO[mode];
1124 break;
1125 case WME_AC_BE:
1126 default:
1127 pPhyParam = &phyParamForAC_BE[mode];
1128 pBssPhyParam = &bssPhyParamForAC_BE[mode];
1129 break;
1130 }
1131 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1132 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1133 setwmeparams(vap, "chan", i, wmep, pPhyParam);
1134 } else {
1135 setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
1136 }
1137 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1138 setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
1139 }
1140 /* NB: check ic_bss to avoid NULL deref on initial attach */
1141 if (vap->iv_bss != NULL) {
1142 /*
1143 * Calculate aggressive mode switching threshold based
1144 * on beacon interval. This doesn't need locking since
1145 * we're only called before entering the RUN state at
1146 * which point we start sending beacon frames.
1147 */
1148 wme->wme_hipri_switch_thresh =
1149 (HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
1150 wme->wme_flags &= ~WME_F_AGGRMODE;
1151 ieee80211_wme_updateparams(vap);
1152 }
1153 }
1154
1155 void
1156 ieee80211_wme_initparams(struct ieee80211vap *vap)
1157 {
1158 struct ieee80211com *ic = vap->iv_ic;
1159
1160 IEEE80211_LOCK(ic);
1161 ieee80211_wme_initparams_locked(vap);
1162 IEEE80211_UNLOCK(ic);
1163 }
1164
1165 /*
1166 * Update WME parameters for ourself and the BSS.
1167 */
1168 void
1169 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
1170 {
1171 static const paramType aggrParam[IEEE80211_MODE_MAX] = {
1172 [IEEE80211_MODE_AUTO] = { 2, 4, 10, 64, 0 },
1173 [IEEE80211_MODE_11A] = { 2, 4, 10, 64, 0 },
1174 [IEEE80211_MODE_11B] = { 2, 5, 10, 64, 0 },
1175 [IEEE80211_MODE_11G] = { 2, 4, 10, 64, 0 },
1176 [IEEE80211_MODE_FH] = { 2, 5, 10, 64, 0 },
1177 [IEEE80211_MODE_TURBO_A] = { 1, 3, 10, 64, 0 },
1178 [IEEE80211_MODE_TURBO_G] = { 1, 3, 10, 64, 0 },
1179 [IEEE80211_MODE_STURBO_A] = { 1, 3, 10, 64, 0 },
1180 [IEEE80211_MODE_HALF] = { 2, 4, 10, 64, 0 },
1181 [IEEE80211_MODE_QUARTER] = { 2, 4, 10, 64, 0 },
1182 [IEEE80211_MODE_11NA] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1183 [IEEE80211_MODE_11NG] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1184 [IEEE80211_MODE_VHT_2GHZ] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1185 [IEEE80211_MODE_VHT_5GHZ] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
1186 };
1187 struct ieee80211com *ic = vap->iv_ic;
1188 struct ieee80211_wme_state *wme = &ic->ic_wme;
1189 const struct wmeParams *wmep;
1190 struct wmeParams *chanp, *bssp;
1191 enum ieee80211_phymode mode;
1192 int i;
1193 int do_aggrmode = 0;
1194
1195 /*
1196 * Set up the channel access parameters for the physical
1197 * device. First populate the configured settings.
1198 */
1199 for (i = 0; i < WME_NUM_AC; i++) {
1200 chanp = &wme->wme_chanParams.cap_wmeParams[i];
1201 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1202 chanp->wmep_aifsn = wmep->wmep_aifsn;
1203 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1204 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1205 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1206
1207 chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
1208 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1209 chanp->wmep_aifsn = wmep->wmep_aifsn;
1210 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1211 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1212 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1213 }
1214
1215 /*
1216 * Select mode; we can be called early in which case we
1217 * always use auto mode. We know we'll be called when
1218 * entering the RUN state with bsschan setup properly
1219 * so state will eventually get set correctly
1220 */
1221 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1222 mode = ieee80211_chan2mode(ic->ic_bsschan);
1223 else
1224 mode = IEEE80211_MODE_AUTO;
1225
1226 /*
1227 * This implements aggressive mode as found in certain
1228 * vendors' AP's. When there is significant high
1229 * priority (VI/VO) traffic in the BSS throttle back BE
1230 * traffic by using conservative parameters. Otherwise
1231 * BE uses aggressive params to optimize performance of
1232 * legacy/non-QoS traffic.
1233 */
1234
1235 /* Hostap? Only if aggressive mode is enabled */
1236 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1237 (wme->wme_flags & WME_F_AGGRMODE) != 0)
1238 do_aggrmode = 1;
1239
1240 /*
1241 * Station? Only if we're in a non-QoS BSS.
1242 */
1243 else if ((vap->iv_opmode == IEEE80211_M_STA &&
1244 (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1245 do_aggrmode = 1;
1246
1247 /*
1248 * IBSS? Only if we we have WME enabled.
1249 */
1250 else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1251 (vap->iv_flags & IEEE80211_F_WME))
1252 do_aggrmode = 1;
1253
1254 /*
1255 * If WME is disabled on this VAP, default to aggressive mode
1256 * regardless of the configuration.
1257 */
1258 if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1259 do_aggrmode = 1;
1260
1261 /* XXX WDS? */
1262
1263 /* XXX MBSS? */
1264
1265 if (do_aggrmode) {
1266 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1267 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1268
1269 chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1270 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1271 aggrParam[mode].logcwmin;
1272 chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1273 aggrParam[mode].logcwmax;
1274 chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1275 (vap->iv_flags & IEEE80211_F_BURST) ?
1276 aggrParam[mode].txopLimit : 0;
1277 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1278 "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1279 "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1280 chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1281 chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1282 }
1283
1284
1285 /*
1286 * Change the contention window based on the number of associated
1287 * stations. If the number of associated stations is 1 and
1288 * aggressive mode is enabled, lower the contention window even
1289 * further.
1290 */
1291 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1292 ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1293 static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1294 [IEEE80211_MODE_AUTO] = 3,
1295 [IEEE80211_MODE_11A] = 3,
1296 [IEEE80211_MODE_11B] = 4,
1297 [IEEE80211_MODE_11G] = 3,
1298 [IEEE80211_MODE_FH] = 4,
1299 [IEEE80211_MODE_TURBO_A] = 3,
1300 [IEEE80211_MODE_TURBO_G] = 3,
1301 [IEEE80211_MODE_STURBO_A] = 3,
1302 [IEEE80211_MODE_HALF] = 3,
1303 [IEEE80211_MODE_QUARTER] = 3,
1304 [IEEE80211_MODE_11NA] = 3,
1305 [IEEE80211_MODE_11NG] = 3,
1306 [IEEE80211_MODE_VHT_2GHZ] = 3,
1307 [IEEE80211_MODE_VHT_5GHZ] = 3,
1308 };
1309 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1310 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1311
1312 chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1313 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1314 "update %s (chan+bss) logcwmin %u\n",
1315 ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1316 }
1317
1318 /*
1319 * Arrange for the beacon update.
1320 *
1321 * XXX what about MBSS, WDS?
1322 */
1323 if (vap->iv_opmode == IEEE80211_M_HOSTAP
1324 || vap->iv_opmode == IEEE80211_M_IBSS) {
1325 /*
1326 * Arrange for a beacon update and bump the parameter
1327 * set number so associated stations load the new values.
1328 */
1329 wme->wme_bssChanParams.cap_info =
1330 (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1331 ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1332 }
1333
1334 /* schedule the deferred WME update */
1335 ieee80211_runtask(ic, &vap->iv_wme_task);
1336
1337 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1338 "%s: WME params updated, cap_info 0x%x\n", __func__,
1339 vap->iv_opmode == IEEE80211_M_STA ?
1340 wme->wme_wmeChanParams.cap_info :
1341 wme->wme_bssChanParams.cap_info);
1342 }
1343
1344 void
1345 ieee80211_wme_updateparams(struct ieee80211vap *vap)
1346 {
1347 struct ieee80211com *ic = vap->iv_ic;
1348
1349 if (ic->ic_caps & IEEE80211_C_WME) {
1350 IEEE80211_LOCK(ic);
1351 ieee80211_wme_updateparams_locked(vap);
1352 IEEE80211_UNLOCK(ic);
1353 }
1354 }
1355
1356 /*
1357 * Fetch the WME parameters for the given VAP.
1358 *
1359 * When net80211 grows p2p, etc support, this may return different
1360 * parameters for each VAP.
1361 */
1362 void
1363 ieee80211_wme_vap_getparams(struct ieee80211vap *vap, struct chanAccParams *wp)
1364 {
1365
1366 memcpy(wp, &vap->iv_ic->ic_wme.wme_chanParams, sizeof(*wp));
1367 }
1368
1369 /*
1370 * For NICs which only support one set of WME paramaters (ie, softmac NICs)
1371 * there may be different VAP WME parameters but only one is "active".
1372 * This returns the "NIC" WME parameters for the currently active
1373 * context.
1374 */
1375 void
1376 ieee80211_wme_ic_getparams(struct ieee80211com *ic, struct chanAccParams *wp)
1377 {
1378
1379 memcpy(wp, &ic->ic_wme.wme_chanParams, sizeof(*wp));
1380 }
1381
1382 /*
1383 * Return whether to use QoS on a given WME queue.
1384 *
1385 * This is intended to be called from the transmit path of softmac drivers
1386 * which are setting NoAck bits in transmit descriptors.
1387 *
1388 * Ideally this would be set in some transmit field before the packet is
1389 * queued to the driver but net80211 isn't quite there yet.
1390 */
1391 int
1392 ieee80211_wme_vap_ac_is_noack(struct ieee80211vap *vap, int ac)
1393 {
1394 /* Bounds/sanity check */
1395 if (ac < 0 || ac >= WME_NUM_AC)
1396 return (0);
1397
1398 /* Again, there's only one global context for now */
1399 return (!! vap->iv_ic->ic_wme.wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy);
1400 }
1401
1402 static void
1403 parent_updown(void *arg, int npending)
1404 {
1405 struct ieee80211com *ic = arg;
1406
1407 ic->ic_parent(ic);
1408 }
1409
1410 static void
1411 update_mcast(void *arg, int npending)
1412 {
1413 struct ieee80211com *ic = arg;
1414
1415 ic->ic_update_mcast(ic);
1416 }
1417
1418 static void
1419 update_promisc(void *arg, int npending)
1420 {
1421 struct ieee80211com *ic = arg;
1422
1423 ic->ic_update_promisc(ic);
1424 }
1425
1426 static void
1427 update_channel(void *arg, int npending)
1428 {
1429 struct ieee80211com *ic = arg;
1430
1431 ic->ic_set_channel(ic);
1432 ieee80211_radiotap_chan_change(ic);
1433 }
1434
1435 static void
1436 update_chw(void *arg, int npending)
1437 {
1438 struct ieee80211com *ic = arg;
1439
1440 /*
1441 * XXX should we defer the channel width _config_ update until now?
1442 */
1443 ic->ic_update_chw(ic);
1444 }
1445
1446 /*
1447 * Deferred WME update.
1448 *
1449 * In preparation for per-VAP WME configuration, call the VAP
1450 * method if the VAP requires it. Otherwise, just call the
1451 * older global method. There isn't a per-VAP WME configuration
1452 * just yet so for now just use the global configuration.
1453 */
1454 static void
1455 vap_update_wme(void *arg, int npending)
1456 {
1457 struct ieee80211vap *vap = arg;
1458 struct ieee80211com *ic = vap->iv_ic;
1459
1460 if (vap->iv_wme_update != NULL)
1461 vap->iv_wme_update(vap,
1462 ic->ic_wme.wme_chanParams.cap_wmeParams);
1463 else
1464 ic->ic_wme.wme_update(ic);
1465 }
1466
1467 static void
1468 restart_vaps(void *arg, int npending)
1469 {
1470 struct ieee80211com *ic = arg;
1471
1472 ieee80211_suspend_all(ic);
1473 ieee80211_resume_all(ic);
1474 }
1475
1476 /*
1477 * Block until the parent is in a known state. This is
1478 * used after any operations that dispatch a task (e.g.
1479 * to auto-configure the parent device up/down).
1480 */
1481 void
1482 ieee80211_waitfor_parent(struct ieee80211com *ic)
1483 {
1484 taskqueue_block(ic->ic_tq);
1485 ieee80211_draintask(ic, &ic->ic_parent_task);
1486 ieee80211_draintask(ic, &ic->ic_mcast_task);
1487 ieee80211_draintask(ic, &ic->ic_promisc_task);
1488 ieee80211_draintask(ic, &ic->ic_chan_task);
1489 ieee80211_draintask(ic, &ic->ic_bmiss_task);
1490 ieee80211_draintask(ic, &ic->ic_chw_task);
1491 taskqueue_unblock(ic->ic_tq);
1492 }
1493
1494 /*
1495 * Check to see whether the current channel needs reset.
1496 *
1497 * Some devices don't handle being given an invalid channel
1498 * in their operating mode very well (eg wpi(4) will throw a
1499 * firmware exception.)
1500 *
1501 * Return 0 if we're ok, 1 if the channel needs to be reset.
1502 *
1503 * See PR kern/202502.
1504 */
1505 static int
1506 ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
1507 {
1508 struct ieee80211com *ic = vap->iv_ic;
1509
1510 if ((vap->iv_opmode == IEEE80211_M_IBSS &&
1511 IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
1512 (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1513 IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
1514 return (1);
1515 return (0);
1516 }
1517
1518 /*
1519 * Reset the curchan to a known good state.
1520 */
1521 static void
1522 ieee80211_start_reset_chan(struct ieee80211vap *vap)
1523 {
1524 struct ieee80211com *ic = vap->iv_ic;
1525
1526 ic->ic_curchan = &ic->ic_channels[0];
1527 }
1528
1529 /*
1530 * Start a vap running. If this is the first vap to be
1531 * set running on the underlying device then we
1532 * automatically bring the device up.
1533 */
1534 void
1535 ieee80211_start_locked(struct ieee80211vap *vap)
1536 {
1537 struct ifnet *ifp = vap->iv_ifp;
1538 struct ieee80211com *ic = vap->iv_ic;
1539
1540 IEEE80211_LOCK_ASSERT(ic);
1541
1542 IEEE80211_DPRINTF(vap,
1543 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1544 "start running, %d vaps running\n", ic->ic_nrunning);
1545
1546 #if __FreeBSD__
1547 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1548 #elif __NetBSD__
1549 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1550 #endif
1551 /*
1552 * Mark us running. Note that it's ok to do this first;
1553 * if we need to bring the parent device up we defer that
1554 * to avoid dropping the com lock. We expect the device
1555 * to respond to being marked up by calling back into us
1556 * through ieee80211_start_all at which point we'll come
1557 * back in here and complete the work.
1558 */
1559 #if __FreeBSD__
1560 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1561 #elif __NetBSD__
1562 ifp->if_flags |= IFF_RUNNING;
1563 #endif
1564 /*
1565 * We are not running; if this we are the first vap
1566 * to be brought up auto-up the parent if necessary.
1567 */
1568 if (ic->ic_nrunning++ == 0) {
1569
1570 /* reset the channel to a known good channel */
1571 if (ieee80211_start_check_reset_chan(vap))
1572 ieee80211_start_reset_chan(vap);
1573
1574 IEEE80211_DPRINTF(vap,
1575 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1576 "%s: up parent %s\n", __func__, ic->ic_name);
1577 ieee80211_runtask(ic, &ic->ic_parent_task);
1578 return;
1579 }
1580 }
1581 /*
1582 * If the parent is up and running, then kick the
1583 * 802.11 state machine as appropriate.
1584 */
1585 if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1586 if (vap->iv_opmode == IEEE80211_M_STA) {
1587 #if 0
1588 /* XXX bypasses scan too easily; disable for now */
1589 /*
1590 * Try to be intelligent about clocking the state
1591 * machine. If we're currently in RUN state then
1592 * we should be able to apply any new state/parameters
1593 * simply by re-associating. Otherwise we need to
1594 * re-scan to select an appropriate ap.
1595 */
1596 if (vap->iv_state >= IEEE80211_S_RUN)
1597 ieee80211_new_state_locked(vap,
1598 IEEE80211_S_ASSOC, 1);
1599 else
1600 #endif
1601 ieee80211_new_state_locked(vap,
1602 IEEE80211_S_SCAN, 0);
1603 } else {
1604 /*
1605 * For monitor+wds mode there's nothing to do but
1606 * start running. Otherwise if this is the first
1607 * vap to be brought up, start a scan which may be
1608 * preempted if the station is locked to a particular
1609 * channel.
1610 */
1611 vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1612 if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1613 vap->iv_opmode == IEEE80211_M_WDS)
1614 ieee80211_new_state_locked(vap,
1615 IEEE80211_S_RUN, -1);
1616 else
1617 ieee80211_new_state_locked(vap,
1618 IEEE80211_S_SCAN, 0);
1619 }
1620 }
1621 }
1622
1623 /*
1624 * Start a single vap.
1625 */
1626 #if __FreeBSD__
1627 void
1628 ieee80211_init(void *arg)
1629 {
1630 struct ieee80211vap *vap = arg;
1631
1632 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1633 "%s\n", __func__);
1634
1635 IEEE80211_LOCK(vap->iv_ic);
1636 ieee80211_start_locked(vap);
1637 IEEE80211_UNLOCK(vap->iv_ic);
1638 }
1639 #elif __NetBSD__
1640 int
1641 ieee80211_init(struct ifnet *ifp)
1642 {
1643 struct ieee80211vap *vap = ifp->if_softc;
1644
1645 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1646 "%s\n", __func__);
1647
1648 IEEE80211_LOCK(vap->iv_ic);
1649 ieee80211_start_locked(vap);
1650 IEEE80211_UNLOCK(vap->iv_ic);
1651 return 0;
1652 }
1653 #endif
1654
1655 /*
1656 * Start all runnable vap's on a device.
1657 */
1658 void
1659 ieee80211_start_all(struct ieee80211com *ic)
1660 {
1661 struct ieee80211vap *vap;
1662
1663 IEEE80211_LOCK(ic);
1664 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1665 struct ifnet *ifp = vap->iv_ifp;
1666 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1667 ieee80211_start_locked(vap);
1668 }
1669 IEEE80211_UNLOCK(ic);
1670 }
1671
1672 /*
1673 * Stop a vap. We force it down using the state machine
1674 * then mark it's ifnet not running. If this is the last
1675 * vap running on the underlying device then we close it
1676 * too to insure it will be properly initialized when the
1677 * next vap is brought up.
1678 */
1679 void
1680 ieee80211_stop_locked(struct ieee80211vap *vap)
1681 {
1682 struct ieee80211com *ic = vap->iv_ic;
1683 struct ifnet *ifp = vap->iv_ifp;
1684
1685 IEEE80211_LOCK_ASSERT(ic);
1686
1687 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1688 "stop running, %d vaps running\n", ic->ic_nrunning);
1689
1690 ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1691 #if __FreeBSD__
1692 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1693 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; /* mark us stopped */
1694 #elif __NetBSD__
1695 if (ifp->if_flags & IFF_RUNNING) {
1696 ifp->if_flags &= ~IFF_RUNNING; /* mark us stopped */
1697 #endif
1698 if (--ic->ic_nrunning == 0) {
1699 IEEE80211_DPRINTF(vap,
1700 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1701 "down parent %s\n", ic->ic_name);
1702 ieee80211_runtask(ic, &ic->ic_parent_task);
1703 }
1704 }
1705 }
1706
1707 void
1708 ieee80211_stop(struct ieee80211vap *vap)
1709 {
1710 struct ieee80211com *ic = vap->iv_ic;
1711
1712 IEEE80211_LOCK(ic);
1713 ieee80211_stop_locked(vap);
1714 IEEE80211_UNLOCK(ic);
1715 }
1716
1717 /*
1718 * Stop all vap's running on a device.
1719 */
1720 void
1721 ieee80211_stop_all(struct ieee80211com *ic)
1722 {
1723 struct ieee80211vap *vap;
1724
1725 IEEE80211_LOCK(ic);
1726 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1727 struct ifnet *ifp = vap->iv_ifp;
1728 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1729 ieee80211_stop_locked(vap);
1730 }
1731 IEEE80211_UNLOCK(ic);
1732
1733 ieee80211_waitfor_parent(ic);
1734 }
1735
1736 /*
1737 * Stop all vap's running on a device and arrange
1738 * for those that were running to be resumed.
1739 */
1740 void
1741 ieee80211_suspend_all(struct ieee80211com *ic)
1742 {
1743 struct ieee80211vap *vap;
1744
1745 IEEE80211_LOCK(ic);
1746 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1747 struct ifnet *ifp = vap->iv_ifp;
1748 if (IFNET_IS_UP_RUNNING(ifp)) { /* NB: avoid recursion */
1749 vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1750 ieee80211_stop_locked(vap);
1751 }
1752 }
1753 IEEE80211_UNLOCK(ic);
1754
1755 ieee80211_waitfor_parent(ic);
1756 }
1757
1758 /*
1759 * Start all vap's marked for resume.
1760 */
1761 void
1762 ieee80211_resume_all(struct ieee80211com *ic)
1763 {
1764 struct ieee80211vap *vap;
1765
1766 IEEE80211_LOCK(ic);
1767 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1768 struct ifnet *ifp = vap->iv_ifp;
1769 if (!IFNET_IS_UP_RUNNING(ifp) &&
1770 (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1771 vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1772 ieee80211_start_locked(vap);
1773 }
1774 }
1775 IEEE80211_UNLOCK(ic);
1776 }
1777
1778 /*
1779 * Restart all vap's running on a device.
1780 */
1781 void
1782 ieee80211_restart_all(struct ieee80211com *ic)
1783 {
1784 /*
1785 * NB: do not use ieee80211_runtask here, we will
1786 * block & drain net80211 taskqueue.
1787 */
1788 taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
1789 }
1790
1791 void
1792 ieee80211_beacon_miss(struct ieee80211com *ic)
1793 {
1794 IEEE80211_LOCK(ic);
1795 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1796 /* Process in a taskq, the handler may reenter the driver */
1797 ieee80211_runtask(ic, &ic->ic_bmiss_task);
1798 }
1799 IEEE80211_UNLOCK(ic);
1800 }
1801
1802 static void
1803 beacon_miss(void *arg, int npending)
1804 {
1805 struct ieee80211com *ic = arg;
1806 struct ieee80211vap *vap;
1807
1808 IEEE80211_LOCK(ic);
1809 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1810 /*
1811 * We only pass events through for sta vap's in RUN+ state;
1812 * may be too restrictive but for now this saves all the
1813 * handlers duplicating these checks.
1814 */
1815 if (vap->iv_opmode == IEEE80211_M_STA &&
1816 vap->iv_state >= IEEE80211_S_RUN &&
1817 vap->iv_bmiss != NULL)
1818 vap->iv_bmiss(vap);
1819 }
1820 IEEE80211_UNLOCK(ic);
1821 }
1822
1823 static void
1824 beacon_swmiss(void *arg, int npending)
1825 {
1826 struct ieee80211vap *vap = arg;
1827 struct ieee80211com *ic = vap->iv_ic;
1828
1829 IEEE80211_LOCK(ic);
1830 if (vap->iv_state >= IEEE80211_S_RUN) {
1831 /* XXX Call multiple times if npending > zero? */
1832 vap->iv_bmiss(vap);
1833 }
1834 IEEE80211_UNLOCK(ic);
1835 }
1836
1837 /*
1838 * Software beacon miss handling. Check if any beacons
1839 * were received in the last period. If not post a
1840 * beacon miss; otherwise reset the counter.
1841 */
1842 void
1843 ieee80211_swbmiss(void *arg)
1844 {
1845 struct ieee80211vap *vap = arg;
1846 struct ieee80211com *ic = vap->iv_ic;
1847
1848 IEEE80211_LOCK_ASSERT(ic);
1849
1850 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1851 ("wrong state %d", vap->iv_state));
1852
1853 if (ic->ic_flags & IEEE80211_F_SCAN) {
1854 /*
1855 * If scanning just ignore and reset state. If we get a
1856 * bmiss after coming out of scan because we haven't had
1857 * time to receive a beacon then we should probe the AP
1858 * before posting a real bmiss (unless iv_bmiss_max has
1859 * been artifiically lowered). A cleaner solution might
1860 * be to disable the timer on scan start/end but to handle
1861 * case of multiple sta vap's we'd need to disable the
1862 * timers of all affected vap's.
1863 */
1864 vap->iv_swbmiss_count = 0;
1865 } else if (vap->iv_swbmiss_count == 0) {
1866 if (vap->iv_bmiss != NULL)
1867 ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1868 } else
1869 vap->iv_swbmiss_count = 0;
1870 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1871 ieee80211_swbmiss, vap);
1872 }
1873
1874 /*
1875 * Start an 802.11h channel switch. We record the parameters,
1876 * mark the operation pending, notify each vap through the
1877 * beacon update mechanism so it can update the beacon frame
1878 * contents, and then switch vap's to CSA state to block outbound
1879 * traffic. Devices that handle CSA directly can use the state
1880 * switch to do the right thing so long as they call
1881 * ieee80211_csa_completeswitch when it's time to complete the
1882 * channel change. Devices that depend on the net80211 layer can
1883 * use ieee80211_beacon_update to handle the countdown and the
1884 * channel switch.
1885 */
1886 void
1887 ieee80211_csa_startswitch(struct ieee80211com *ic,
1888 struct ieee80211_channel *c, int mode, int count)
1889 {
1890 struct ieee80211vap *vap;
1891
1892 IEEE80211_LOCK_ASSERT(ic);
1893
1894 ic->ic_csa_newchan = c;
1895 ic->ic_csa_mode = mode;
1896 ic->ic_csa_count = count;
1897 ic->ic_flags |= IEEE80211_F_CSAPENDING;
1898 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1899 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1900 vap->iv_opmode == IEEE80211_M_IBSS ||
1901 vap->iv_opmode == IEEE80211_M_MBSS)
1902 ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1903 /* switch to CSA state to block outbound traffic */
1904 if (vap->iv_state == IEEE80211_S_RUN)
1905 ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1906 }
1907 ieee80211_notify_csa(ic, c, mode, count);
1908 }
1909
1910 /*
1911 * Complete the channel switch by transitioning all CSA VAPs to RUN.
1912 * This is called by both the completion and cancellation functions
1913 * so each VAP is placed back in the RUN state and can thus transmit.
1914 */
1915 static void
1916 csa_completeswitch(struct ieee80211com *ic)
1917 {
1918 struct ieee80211vap *vap;
1919
1920 ic->ic_csa_newchan = NULL;
1921 ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1922
1923 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1924 if (vap->iv_state == IEEE80211_S_CSA)
1925 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1926 }
1927
1928 /*
1929 * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1930 * We clear state and move all vap's in CSA state to RUN state
1931 * so they can again transmit.
1932 *
1933 * Although this may not be completely correct, update the BSS channel
1934 * for each VAP to the newly configured channel. The setcurchan sets
1935 * the current operating channel for the interface (so the radio does
1936 * switch over) but the VAP BSS isn't updated, leading to incorrectly
1937 * reported information via ioctl.
1938 */
1939 void
1940 ieee80211_csa_completeswitch(struct ieee80211com *ic)
1941 {
1942 struct ieee80211vap *vap;
1943
1944 IEEE80211_LOCK_ASSERT(ic);
1945
1946 KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1947
1948 ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1949 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1950 if (vap->iv_state == IEEE80211_S_CSA)
1951 vap->iv_bss->ni_chan = ic->ic_curchan;
1952
1953 csa_completeswitch(ic);
1954 }
1955
1956 /*
1957 * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1958 * We clear state and move all vap's in CSA state to RUN state
1959 * so they can again transmit.
1960 */
1961 void
1962 ieee80211_csa_cancelswitch(struct ieee80211com *ic)
1963 {
1964 IEEE80211_LOCK_ASSERT(ic);
1965
1966 csa_completeswitch(ic);
1967 }
1968
1969 /*
1970 * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1971 * We clear state and move all vap's in CAC state to RUN state.
1972 */
1973 void
1974 ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1975 {
1976 struct ieee80211com *ic = vap0->iv_ic;
1977 struct ieee80211vap *vap;
1978
1979 IEEE80211_LOCK(ic);
1980 /*
1981 * Complete CAC state change for lead vap first; then
1982 * clock all the other vap's waiting.
1983 */
1984 KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1985 ("wrong state %d", vap0->iv_state));
1986 ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1987
1988 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1989 if (vap->iv_state == IEEE80211_S_CAC && vap != vap0)
1990 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1991 IEEE80211_UNLOCK(ic);
1992 }
1993
1994 /*
1995 * Force all vap's other than the specified vap to the INIT state
1996 * and mark them as waiting for a scan to complete. These vaps
1997 * will be brought up when the scan completes and the scanning vap
1998 * reaches RUN state by wakeupwaiting.
1999 */
2000 static void
2001 markwaiting(struct ieee80211vap *vap0)
2002 {
2003 struct ieee80211com *ic = vap0->iv_ic;
2004 struct ieee80211vap *vap;
2005
2006 IEEE80211_LOCK_ASSERT(ic);
2007
2008 /*
2009 * A vap list entry can not disappear since we are running on the
2010 * taskqueue and a vap destroy will queue and drain another state
2011 * change task.
2012 */
2013 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2014 if (vap == vap0)
2015 continue;
2016 if (vap->iv_state != IEEE80211_S_INIT) {
2017 /* NB: iv_newstate may drop the lock */
2018 vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
2019 IEEE80211_LOCK_ASSERT(ic);
2020 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2021 }
2022 }
2023 }
2024
2025 /*
2026 * Wakeup all vap's waiting for a scan to complete. This is the
2027 * companion to markwaiting (above) and is used to coordinate
2028 * multiple vaps scanning.
2029 * This is called from the state taskqueue.
2030 */
2031 static void
2032 wakeupwaiting(struct ieee80211vap *vap0)
2033 {
2034 struct ieee80211com *ic = vap0->iv_ic;
2035 struct ieee80211vap *vap;
2036
2037 IEEE80211_LOCK_ASSERT(ic);
2038
2039 /*
2040 * A vap list entry can not disappear since we are running on the
2041 * taskqueue and a vap destroy will queue and drain another state
2042 * change task.
2043 */
2044 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2045 if (vap == vap0)
2046 continue;
2047 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
2048 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2049 /* NB: sta's cannot go INIT->RUN */
2050 /* NB: iv_newstate may drop the lock */
2051 vap->iv_newstate(vap,
2052 vap->iv_opmode == IEEE80211_M_STA ?
2053 IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
2054 IEEE80211_LOCK_ASSERT(ic);
2055 }
2056 }
2057 }
2058
2059 /*
2060 * Handle post state change work common to all operating modes.
2061 */
2062 static void
2063 ieee80211_newstate_cb(void *xvap, int npending)
2064 {
2065 struct ieee80211vap *vap = xvap;
2066 struct ieee80211com *ic = vap->iv_ic;
2067 enum ieee80211_state nstate, ostate;
2068 int arg, rc;
2069
2070 IEEE80211_LOCK(ic);
2071 nstate = vap->iv_nstate;
2072 arg = vap->iv_nstate_arg;
2073
2074 if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
2075 /*
2076 * We have been requested to drop back to the INIT before
2077 * proceeding to the new state.
2078 */
2079 /* Deny any state changes while we are here. */
2080 vap->iv_nstate = IEEE80211_S_INIT;
2081 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2082 "%s: %s -> %s arg %d\n", __func__,
2083 ieee80211_state_name[vap->iv_state],
2084 ieee80211_state_name[vap->iv_nstate], arg);
2085 vap->iv_newstate(vap, vap->iv_nstate, 0);
2086 IEEE80211_LOCK_ASSERT(ic);
2087 vap->iv_flags_ext &= ~(IEEE80211_FEXT_REINIT |
2088 IEEE80211_FEXT_STATEWAIT);
2089 /* enqueue new state transition after cancel_scan() task */
2090 ieee80211_new_state_locked(vap, nstate, arg);
2091 goto done;
2092 }
2093
2094 ostate = vap->iv_state;
2095 if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
2096 /*
2097 * SCAN was forced; e.g. on beacon miss. Force other running
2098 * vap's to INIT state and mark them as waiting for the scan to
2099 * complete. This insures they don't interfere with our
2100 * scanning. Since we are single threaded the vaps can not
2101 * transition again while we are executing.
2102 *
2103 * XXX not always right, assumes ap follows sta
2104 */
2105 markwaiting(vap);
2106 }
2107 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2108 "%s: %s -> %s arg %d\n", __func__,
2109 ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
2110
2111 rc = vap->iv_newstate(vap, nstate, arg);
2112 IEEE80211_LOCK_ASSERT(ic);
2113 vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
2114 if (rc != 0) {
2115 /* State transition failed */
2116 KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
2117 KASSERT(nstate != IEEE80211_S_INIT,
2118 ("INIT state change failed"));
2119 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2120 "%s: %s returned error %d\n", __func__,
2121 ieee80211_state_name[nstate], rc);
2122 goto done;
2123 }
2124
2125 /* No actual transition, skip post processing */
2126 if (ostate == nstate)
2127 goto done;
2128
2129 if (nstate == IEEE80211_S_RUN) {
2130 /*
2131 * OACTIVE may be set on the vap if the upper layer
2132 * tried to transmit (e.g. IPv6 NDP) before we reach
2133 * RUN state. Clear it and restart xmit.
2134 *
2135 * Note this can also happen as a result of SLEEP->RUN
2136 * (i.e. coming out of power save mode).
2137 */
2138 #if __FreeBSD__
2139 vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2140 #elif __NetBSD__
2141 vap->iv_ifp->if_flags &= ~IFF_OACTIVE;
2142 #endif
2143
2144 /*
2145 * XXX TODO Kick-start a VAP queue - this should be a method!
2146 */
2147
2148 /* bring up any vaps waiting on us */
2149 wakeupwaiting(vap);
2150 } else if (nstate == IEEE80211_S_INIT) {
2151 /*
2152 * Flush the scan cache if we did the last scan (XXX?)
2153 * and flush any frames on send queues from this vap.
2154 * Note the mgt q is used only for legacy drivers and
2155 * will go away shortly.
2156 */
2157 ieee80211_scan_flush(vap);
2158
2159 /*
2160 * XXX TODO: ic/vap queue flush
2161 */
2162 }
2163 done:
2164 IEEE80211_UNLOCK(ic);
2165 }
2166
2167 /*
2168 * Public interface for initiating a state machine change.
2169 * This routine single-threads the request and coordinates
2170 * the scheduling of multiple vaps for the purpose of selecting
2171 * an operating channel. Specifically the following scenarios
2172 * are handled:
2173 * o only one vap can be selecting a channel so on transition to
2174 * SCAN state if another vap is already scanning then
2175 * mark the caller for later processing and return without
2176 * doing anything (XXX? expectations by caller of synchronous operation)
2177 * o only one vap can be doing CAC of a channel so on transition to
2178 * CAC state if another vap is already scanning for radar then
2179 * mark the caller for later processing and return without
2180 * doing anything (XXX? expectations by caller of synchronous operation)
2181 * o if another vap is already running when a request is made
2182 * to SCAN then an operating channel has been chosen; bypass
2183 * the scan and just join the channel
2184 *
2185 * Note that the state change call is done through the iv_newstate
2186 * method pointer so any driver routine gets invoked. The driver
2187 * will normally call back into operating mode-specific
2188 * ieee80211_newstate routines (below) unless it needs to completely
2189 * bypass the state machine (e.g. because the firmware has it's
2190 * own idea how things should work). Bypassing the net80211 layer
2191 * is usually a mistake and indicates lack of proper integration
2192 * with the net80211 layer.
2193 */
2194 int
2195 ieee80211_new_state_locked(struct ieee80211vap *vap,
2196 enum ieee80211_state nstate, int arg)
2197 {
2198 struct ieee80211com *ic = vap->iv_ic;
2199 struct ieee80211vap *vp;
2200 enum ieee80211_state ostate;
2201 int nrunning, nscanning;
2202
2203 IEEE80211_LOCK_ASSERT(ic);
2204
2205 if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
2206 if (vap->iv_nstate == IEEE80211_S_INIT ||
2207 ((vap->iv_state == IEEE80211_S_INIT ||
2208 (vap->iv_flags_ext & IEEE80211_FEXT_REINIT)) &&
2209 vap->iv_nstate == IEEE80211_S_SCAN &&
2210 nstate > IEEE80211_S_SCAN)) {
2211 /*
2212 * XXX The vap is being stopped/started,
2213 * do not allow any other state changes
2214 * until this is completed.
2215 */
2216 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2217 "%s: %s -> %s (%s) transition discarded\n",
2218 __func__,
2219 ieee80211_state_name[vap->iv_state],
2220 ieee80211_state_name[nstate],
2221 ieee80211_state_name[vap->iv_nstate]);
2222 return -1;
2223 } else if (vap->iv_state != vap->iv_nstate) {
2224 #if 0
2225 /* Warn if the previous state hasn't completed. */
2226 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2227 "%s: pending %s -> %s transition lost\n", __func__,
2228 ieee80211_state_name[vap->iv_state],
2229 ieee80211_state_name[vap->iv_nstate]);
2230 #else
2231 /* XXX temporarily enable to identify issues */
2232 if_printf(vap->iv_ifp,
2233 "%s: pending %s -> %s transition lost\n",
2234 __func__, ieee80211_state_name[vap->iv_state],
2235 ieee80211_state_name[vap->iv_nstate]);
2236 #endif
2237 }
2238 }
2239
2240 nrunning = nscanning = 0;
2241 /* XXX can track this state instead of calculating */
2242 TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
2243 if (vp != vap) {
2244 if (vp->iv_state >= IEEE80211_S_RUN)
2245 nrunning++;
2246 /* XXX doesn't handle bg scan */
2247 /* NB: CAC+AUTH+ASSOC treated like SCAN */
2248 else if (vp->iv_state > IEEE80211_S_INIT)
2249 nscanning++;
2250 }
2251 }
2252 ostate = vap->iv_state;
2253 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2254 "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
2255 ieee80211_state_name[ostate], ieee80211_state_name[nstate],
2256 nrunning, nscanning);
2257 switch (nstate) {
2258 case IEEE80211_S_SCAN:
2259 if (ostate == IEEE80211_S_INIT) {
2260 /*
2261 * INIT -> SCAN happens on initial bringup.
2262 */
2263 KASSERT(!(nscanning && nrunning),
2264 ("%d scanning and %d running", nscanning, nrunning));
2265 if (nscanning) {
2266 /*
2267 * Someone is scanning, defer our state
2268 * change until the work has completed.
2269 */
2270 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2271 "%s: defer %s -> %s\n",
2272 __func__, ieee80211_state_name[ostate],
2273 ieee80211_state_name[nstate]);
2274 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2275 return 0;
2276 }
2277 if (nrunning) {
2278 /*
2279 * Someone is operating; just join the channel
2280 * they have chosen.
2281 */
2282 /* XXX kill arg? */
2283 /* XXX check each opmode, adhoc? */
2284 if (vap->iv_opmode == IEEE80211_M_STA)
2285 nstate = IEEE80211_S_SCAN;
2286 else
2287 nstate = IEEE80211_S_RUN;
2288 #ifdef IEEE80211_DEBUG
2289 if (nstate != IEEE80211_S_SCAN) {
2290 IEEE80211_DPRINTF(vap,
2291 IEEE80211_MSG_STATE,
2292 "%s: override, now %s -> %s\n",
2293 __func__,
2294 ieee80211_state_name[ostate],
2295 ieee80211_state_name[nstate]);
2296 }
2297 #endif
2298 }
2299 }
2300 break;
2301 case IEEE80211_S_RUN:
2302 if (vap->iv_opmode == IEEE80211_M_WDS &&
2303 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
2304 nscanning) {
2305 /*
2306 * Legacy WDS with someone else scanning; don't
2307 * go online until that completes as we should
2308 * follow the other vap to the channel they choose.
2309 */
2310 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2311 "%s: defer %s -> %s (legacy WDS)\n", __func__,
2312 ieee80211_state_name[ostate],
2313 ieee80211_state_name[nstate]);
2314 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2315 return 0;
2316 }
2317 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
2318 IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2319 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
2320 !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
2321 /*
2322 * This is a DFS channel, transition to CAC state
2323 * instead of RUN. This allows us to initiate
2324 * Channel Availability Check (CAC) as specified
2325 * by 11h/DFS.
2326 */
2327 nstate = IEEE80211_S_CAC;
2328 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2329 "%s: override %s -> %s (DFS)\n", __func__,
2330 ieee80211_state_name[ostate],
2331 ieee80211_state_name[nstate]);
2332 }
2333 break;
2334 case IEEE80211_S_INIT:
2335 /* cancel any scan in progress */
2336 ieee80211_cancel_scan(vap);
2337 if (ostate == IEEE80211_S_INIT ) {
2338 /* XXX don't believe this */
2339 /* INIT -> INIT. nothing to do */
2340 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2341 }
2342 /* fall thru... */
2343 default:
2344 break;
2345 }
2346 /* defer the state change to a thread */
2347 vap->iv_nstate = nstate;
2348 vap->iv_nstate_arg = arg;
2349 vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
2350 ieee80211_runtask(ic, &vap->iv_nstate_task);
2351 return EINPROGRESS;
2352 }
2353
2354 int
2355 ieee80211_new_state(struct ieee80211vap *vap,
2356 enum ieee80211_state nstate, int arg)
2357 {
2358 struct ieee80211com *ic = vap->iv_ic;
2359 int rc;
2360
2361 IEEE80211_LOCK(ic);
2362 rc = ieee80211_new_state_locked(vap, nstate, arg);
2363 IEEE80211_UNLOCK(ic);
2364 return rc;
2365 }
2366