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