wlan - Rip out all wlan locks part 1/2
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_proto.c
CommitLineData
32176cfd 1/*-
f186073c 2 * Copyright (c) 2001 Atsushi Onoe
32176cfd 3 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
f186073c
JS
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
f186073c
JS
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
32176cfd
RP
26 * $FreeBSD: head/sys/net80211/ieee80211_proto.c 195618 2009-07-11 15:02:45Z rpaulo $
27 * $DragonFly$
f186073c
JS
28 */
29
30/*
31 * IEEE 802.11 protocol support.
32 */
33
34#include "opt_inet.h"
32176cfd 35#include "opt_wlan.h"
f186073c
JS
36
37#include <sys/param.h>
f186073c 38#include <sys/kernel.h>
32176cfd
RP
39#include <sys/systm.h>
40
841ab66c 41#include <sys/socket.h>
32176cfd 42#include <sys/sockio.h>
841ab66c 43
f186073c 44#include <net/if.h>
841ab66c 45#include <net/if_media.h>
32176cfd 46#include <net/route.h>
f186073c
JS
47
48#include <netproto/802_11/ieee80211_var.h>
32176cfd
RP
49#include <netproto/802_11/ieee80211_adhoc.h>
50#include <netproto/802_11/ieee80211_sta.h>
51#include <netproto/802_11/ieee80211_hostap.h>
52#include <netproto/802_11/ieee80211_wds.h>
53#ifdef IEEE80211_SUPPORT_MESH
54#include <netproto/802_11/ieee80211_mesh.h>
55#endif
56#include <netproto/802_11/ieee80211_monitor.h>
57#include <netproto/802_11/ieee80211_input.h>
f186073c 58
841ab66c
SZ
59/* XXX tunables */
60#define AGGRESSIVE_MODE_SWITCH_HYSTERESIS 3 /* pkts / 100ms */
61#define HIGH_PRI_SWITCH_THRESH 10 /* pkts / 100ms */
f186073c 62
32176cfd
RP
63const char *ieee80211_mgt_subtype_name[] = {
64 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp",
65 "probe_req", "probe_resp", "reserved#6", "reserved#7",
66 "beacon", "atim", "disassoc", "auth",
67 "deauth", "action", "reserved#14", "reserved#15"
68};
69const char *ieee80211_ctl_subtype_name[] = {
70 "reserved#0", "reserved#1", "reserved#2", "reserved#3",
71 "reserved#3", "reserved#5", "reserved#6", "reserved#7",
72 "reserved#8", "reserved#9", "ps_poll", "rts",
73 "cts", "ack", "cf_end", "cf_end_ack"
74};
75const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
76 "IBSS", /* IEEE80211_M_IBSS */
77 "STA", /* IEEE80211_M_STA */
78 "WDS", /* IEEE80211_M_WDS */
79 "AHDEMO", /* IEEE80211_M_AHDEMO */
80 "HOSTAP", /* IEEE80211_M_HOSTAP */
81 "MONITOR", /* IEEE80211_M_MONITOR */
82 "MBSS" /* IEEE80211_M_MBSS */
83};
f186073c
JS
84const char *ieee80211_state_name[IEEE80211_S_MAX] = {
85 "INIT", /* IEEE80211_S_INIT */
86 "SCAN", /* IEEE80211_S_SCAN */
87 "AUTH", /* IEEE80211_S_AUTH */
88 "ASSOC", /* IEEE80211_S_ASSOC */
32176cfd
RP
89 "CAC", /* IEEE80211_S_CAC */
90 "RUN", /* IEEE80211_S_RUN */
91 "CSA", /* IEEE80211_S_CSA */
92 "SLEEP", /* IEEE80211_S_SLEEP */
f186073c 93};
841ab66c
SZ
94const char *ieee80211_wme_acnames[] = {
95 "WME_AC_BE",
96 "WME_AC_BK",
97 "WME_AC_VI",
98 "WME_AC_VO",
99 "WME_UPSD",
100};
f186073c 101
32176cfd
RP
102static void beacon_miss(void *, int);
103static void beacon_swmiss(void *, int);
104static void parent_updown(void *, int);
105static void update_mcast(void *, int);
106static void update_promisc(void *, int);
107static void update_channel(void *, int);
108static void ieee80211_newstate_cb(void *, int);
109static int ieee80211_new_state_locked(struct ieee80211vap *,
110 enum ieee80211_state, int);
111
112static int
113null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
114 const struct ieee80211_bpf_params *params)
115{
116 struct ifnet *ifp = ni->ni_ic->ic_ifp;
117
118 if_printf(ifp, "missing ic_raw_xmit callback, drop frame\n");
119 m_freem(m);
120 return ENETDOWN;
121}
f186073c
JS
122
123void
841ab66c 124ieee80211_proto_attach(struct ieee80211com *ic)
f186073c 125{
841ab66c 126 struct ifnet *ifp = ic->ic_ifp;
f186073c 127
32176cfd
RP
128 /* override the 802.3 setting */
129 ifp->if_hdrlen = ic->ic_headroom
130 + sizeof(struct ieee80211_qosframe_addr4)
131 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
132 + IEEE80211_WEP_EXTIVLEN;
133 /* XXX no way to recalculate on ifdetach */
134 if (ALIGN(ifp->if_hdrlen) > max_linkhdr) {
135 /* XXX sanity check... */
136 max_linkhdr = ALIGN(ifp->if_hdrlen);
137 max_hdr = max_linkhdr + max_protohdr;
138 max_datalen = MHLEN - max_hdr;
139 }
f186073c 140 ic->ic_protmode = IEEE80211_PROT_CTSONLY;
32176cfd
RP
141
142 TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ifp);
143 TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
144 TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
145 TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
146 TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
841ab66c
SZ
147
148 ic->ic_wme.wme_hipri_switch_hysteresis =
149 AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
f186073c 150
f186073c 151 /* initialize management frame handlers */
f186073c 152 ic->ic_send_mgmt = ieee80211_send_mgmt;
32176cfd
RP
153 ic->ic_raw_xmit = null_raw_xmit;
154
155 ieee80211_adhoc_attach(ic);
156 ieee80211_sta_attach(ic);
157 ieee80211_wds_attach(ic);
158 ieee80211_hostap_attach(ic);
159#ifdef IEEE80211_SUPPORT_MESH
160 ieee80211_mesh_attach(ic);
161#endif
162 ieee80211_monitor_attach(ic);
f186073c
JS
163}
164
165void
841ab66c 166ieee80211_proto_detach(struct ieee80211com *ic)
f186073c 167{
32176cfd
RP
168 ieee80211_monitor_detach(ic);
169#ifdef IEEE80211_SUPPORT_MESH
170 ieee80211_mesh_detach(ic);
171#endif
172 ieee80211_hostap_detach(ic);
173 ieee80211_wds_detach(ic);
174 ieee80211_adhoc_detach(ic);
175 ieee80211_sta_detach(ic);
176}
177
178static void
179null_update_beacon(struct ieee80211vap *vap, int item)
180{
181}
182
183void
184ieee80211_proto_vattach(struct ieee80211vap *vap)
185{
186 struct ieee80211com *ic = vap->iv_ic;
187 struct ifnet *ifp = vap->iv_ifp;
188 int i;
189
190 /* override the 802.3 setting */
191 ifp->if_hdrlen = ic->ic_ifp->if_hdrlen;
841ab66c 192
32176cfd
RP
193 vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
194 vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
195 vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
34a60cf6
RP
196 callout_init_mp(&vap->iv_swbmiss);
197 callout_init_mp(&vap->iv_mgtsend);
32176cfd
RP
198 TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
199 TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
200 /*
201 * Install default tx rate handling: no fixed rate, lowest
202 * supported rate for mgmt and multicast frames. Default
203 * max retry count. These settings can be changed by the
204 * driver and/or user applications.
205 */
206 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
207 const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
208
209 vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
210 if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
211 vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
212 vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
213 } else {
214 vap->iv_txparms[i].mgmtrate =
215 rs->rs_rates[0] & IEEE80211_RATE_VAL;
216 vap->iv_txparms[i].mcastrate =
217 rs->rs_rates[0] & IEEE80211_RATE_VAL;
218 }
219 vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
220 }
221 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
222
223 vap->iv_update_beacon = null_update_beacon;
224 vap->iv_deliver_data = ieee80211_deliver_data;
225
226 /* attach support for operating mode */
227 ic->ic_vattach[vap->iv_opmode](vap);
228}
229
230void
231ieee80211_proto_vdetach(struct ieee80211vap *vap)
232{
233#define FREEAPPIE(ie) do { \
234 if (ie != NULL) \
235 kfree(ie, M_80211_NODE_IE); \
236} while (0)
237 /*
238 * Detach operating mode module.
239 */
240 if (vap->iv_opdetach != NULL)
241 vap->iv_opdetach(vap);
841ab66c
SZ
242 /*
243 * This should not be needed as we detach when reseting
244 * the state but be conservative here since the
245 * authenticator may do things like spawn kernel threads.
246 */
32176cfd
RP
247 if (vap->iv_auth->ia_detach != NULL)
248 vap->iv_auth->ia_detach(vap);
841ab66c
SZ
249 /*
250 * Detach any ACL'ator.
251 */
32176cfd
RP
252 if (vap->iv_acl != NULL)
253 vap->iv_acl->iac_detach(vap);
254
255 FREEAPPIE(vap->iv_appie_beacon);
256 FREEAPPIE(vap->iv_appie_probereq);
257 FREEAPPIE(vap->iv_appie_proberesp);
258 FREEAPPIE(vap->iv_appie_assocreq);
259 FREEAPPIE(vap->iv_appie_assocresp);
260 FREEAPPIE(vap->iv_appie_wpa);
261#undef FREEAPPIE
841ab66c
SZ
262}
263
264/*
265 * Simple-minded authenticator module support.
266 */
267
268#define IEEE80211_AUTH_MAX (IEEE80211_AUTH_WPA+1)
269/* XXX well-known names */
270static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
271 "wlan_internal", /* IEEE80211_AUTH_NONE */
272 "wlan_internal", /* IEEE80211_AUTH_OPEN */
273 "wlan_internal", /* IEEE80211_AUTH_SHARED */
274 "wlan_xauth", /* IEEE80211_AUTH_8021X */
275 "wlan_internal", /* IEEE80211_AUTH_AUTO */
276 "wlan_xauth", /* IEEE80211_AUTH_WPA */
277};
278static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
279
280static const struct ieee80211_authenticator auth_internal = {
281 .ia_name = "wlan_internal",
282 .ia_attach = NULL,
283 .ia_detach = NULL,
284 .ia_node_join = NULL,
285 .ia_node_leave = NULL,
286};
287
288/*
289 * Setup internal authenticators once; they are never unregistered.
290 */
291static void
292ieee80211_auth_setup(void)
293{
294 ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
295 ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
296 ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
297}
298SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
299
300const struct ieee80211_authenticator *
301ieee80211_authenticator_get(int auth)
302{
303 if (auth >= IEEE80211_AUTH_MAX)
304 return NULL;
305 if (authenticators[auth] == NULL)
306 ieee80211_load_module(auth_modnames[auth]);
307 return authenticators[auth];
308}
309
310void
311ieee80211_authenticator_register(int type,
312 const struct ieee80211_authenticator *auth)
313{
314 if (type >= IEEE80211_AUTH_MAX)
315 return;
316 authenticators[type] = auth;
317}
318
319void
320ieee80211_authenticator_unregister(int type)
321{
322
323 if (type >= IEEE80211_AUTH_MAX)
324 return;
325 authenticators[type] = NULL;
326}
327
328/*
329 * Very simple-minded ACL module support.
330 */
331/* XXX just one for now */
332static const struct ieee80211_aclator *acl = NULL;
333
334void
335ieee80211_aclator_register(const struct ieee80211_aclator *iac)
336{
a6ec04bc 337 kprintf("wlan: %s acl policy registered\n", iac->iac_name);
841ab66c
SZ
338 acl = iac;
339}
340
341void
342ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
343{
344 if (acl == iac)
345 acl = NULL;
a6ec04bc 346 kprintf("wlan: %s acl policy unregistered\n", iac->iac_name);
841ab66c
SZ
347}
348
349const struct ieee80211_aclator *
350ieee80211_aclator_get(const char *name)
351{
352 if (acl == NULL)
353 ieee80211_load_module("wlan_acl");
354 return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
f186073c
JS
355}
356
357void
841ab66c 358ieee80211_print_essid(const uint8_t *essid, int len)
f186073c 359{
32176cfd 360 const uint8_t *p;
f186073c 361 int i;
f186073c
JS
362
363 if (len > IEEE80211_NWID_LEN)
364 len = IEEE80211_NWID_LEN;
365 /* determine printable or not */
366 for (i = 0, p = essid; i < len; i++, p++) {
367 if (*p < ' ' || *p > 0x7e)
368 break;
369 }
370 if (i == len) {
a6ec04bc 371 kprintf("\"");
f186073c 372 for (i = 0, p = essid; i < len; i++, p++)
a6ec04bc
SW
373 kprintf("%c", *p);
374 kprintf("\"");
f186073c 375 } else {
a6ec04bc 376 kprintf("0x");
f186073c 377 for (i = 0, p = essid; i < len; i++, p++)
a6ec04bc 378 kprintf("%02x", *p);
f186073c
JS
379 }
380}
381
382void
32176cfd
RP
383ieee80211_dump_pkt(struct ieee80211com *ic,
384 const uint8_t *buf, int len, int rate, int rssi)
f186073c 385{
841ab66c 386 const struct ieee80211_frame *wh;
f186073c
JS
387 int i;
388
841ab66c 389 wh = (const struct ieee80211_frame *)buf;
f186073c
JS
390 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
391 case IEEE80211_FC1_DIR_NODS:
a6ec04bc
SW
392 kprintf("NODS %6D", wh->i_addr2, ":");
393 kprintf("->%6D", wh->i_addr1, ":");
394 kprintf("(%6D)", wh->i_addr3, ":");
f186073c
JS
395 break;
396 case IEEE80211_FC1_DIR_TODS:
a6ec04bc
SW
397 kprintf("TODS %6D", wh->i_addr2, ":");
398 kprintf("->%6D", wh->i_addr3, ":");
399 kprintf("(%6D)", wh->i_addr1, ":");
f186073c
JS
400 break;
401 case IEEE80211_FC1_DIR_FROMDS:
a6ec04bc
SW
402 kprintf("FRDS %6D", wh->i_addr3, ":");
403 kprintf("->%6D", wh->i_addr1, ":");
404 kprintf("(%6D)", wh->i_addr2, ":");
f186073c
JS
405 break;
406 case IEEE80211_FC1_DIR_DSTODS:
a6ec04bc
SW
407 kprintf("DSDS %6D", (const uint8_t *)&wh[1], ":");
408 kprintf("->%6D", wh->i_addr3, ":");
409 kprintf("(%6D", wh->i_addr2, ":");
410 kprintf("->%6D)", wh->i_addr1, ":");
f186073c
JS
411 break;
412 }
413 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
414 case IEEE80211_FC0_TYPE_DATA:
a6ec04bc 415 kprintf(" data");
f186073c
JS
416 break;
417 case IEEE80211_FC0_TYPE_MGT:
a6ec04bc 418 kprintf(" %s", ieee80211_mgt_subtype_name[
f186073c
JS
419 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
420 >> IEEE80211_FC0_SUBTYPE_SHIFT]);
421 break;
422 default:
a6ec04bc 423 kprintf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
f186073c
JS
424 break;
425 }
32176cfd
RP
426 if (IEEE80211_QOS_HAS_SEQ(wh)) {
427 const struct ieee80211_qosframe *qwh =
428 (const struct ieee80211_qosframe *)buf;
429 kprintf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
430 qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
431 }
841ab66c 432 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
32176cfd
RP
433 int off;
434
435 off = ieee80211_anyhdrspace(ic, wh);
436 kprintf(" WEP [IV %.02x %.02x %.02x",
437 buf[off+0], buf[off+1], buf[off+2]);
438 if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
439 kprintf(" %.02x %.02x %.02x",
440 buf[off+4], buf[off+5], buf[off+6]);
441 kprintf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
841ab66c 442 }
f186073c 443 if (rate >= 0)
a6ec04bc 444 kprintf(" %dM", rate / 2);
f186073c 445 if (rssi >= 0)
a6ec04bc
SW
446 kprintf(" +%d", rssi);
447 kprintf("\n");
f186073c
JS
448 if (len > 0) {
449 for (i = 0; i < len; i++) {
450 if ((i & 1) == 0)
a6ec04bc
SW
451 kprintf(" ");
452 kprintf("%02x", buf[i]);
f186073c 453 }
a6ec04bc 454 kprintf("\n");
f186073c
JS
455 }
456}
457
32176cfd
RP
458static __inline int
459findrix(const struct ieee80211_rateset *rs, int r)
460{
461 int i;
462
463 for (i = 0; i < rs->rs_nrates; i++)
464 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
465 return i;
466 return -1;
467}
468
f186073c 469int
32176cfd
RP
470ieee80211_fix_rate(struct ieee80211_node *ni,
471 struct ieee80211_rateset *nrs, int flags)
f186073c
JS
472{
473#define RV(v) ((v) & IEEE80211_RATE_VAL)
32176cfd 474 struct ieee80211vap *vap = ni->ni_vap;
841ab66c 475 struct ieee80211com *ic = ni->ni_ic;
32176cfd
RP
476 int i, j, rix, error;
477 int okrate, badrate, fixedrate, ucastrate;
208a1285 478 const struct ieee80211_rateset *srs;
f186073c
JS
479 uint8_t r;
480
32176cfd
RP
481 error = 0;
482 okrate = badrate = 0;
483 ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
484 if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
485 /*
486 * Workaround awkwardness with fixed rate. We are called
487 * to check both the legacy rate set and the HT rate set
488 * but we must apply any legacy fixed rate check only to the
489 * legacy rate set and vice versa. We cannot tell what type
490 * of rate set we've been given (legacy or HT) but we can
491 * distinguish the fixed rate type (MCS have 0x80 set).
492 * So to deal with this the caller communicates whether to
493 * check MCS or legacy rate using the flags and we use the
494 * type of any fixed rate to avoid applying an MCS to a
495 * legacy rate and vice versa.
496 */
497 if (ucastrate & 0x80) {
498 if (flags & IEEE80211_F_DOFRATE)
499 flags &= ~IEEE80211_F_DOFRATE;
500 } else if ((ucastrate & 0x80) == 0) {
501 if (flags & IEEE80211_F_DOFMCS)
502 flags &= ~IEEE80211_F_DOFMCS;
503 }
504 /* NB: required to make MCS match below work */
505 ucastrate &= IEEE80211_RATE_VAL;
506 }
507 fixedrate = IEEE80211_FIXED_RATE_NONE;
841ab66c 508 /*
32176cfd
RP
509 * XXX we are called to process both MCS and legacy rates;
510 * we must use the appropriate basic rate set or chaos will
511 * ensue; for now callers that want MCS must supply
512 * IEEE80211_F_DOBRS; at some point we'll need to split this
513 * function so there are two variants, one for MCS and one
514 * for legacy rates.
841ab66c 515 */
32176cfd
RP
516 if (flags & IEEE80211_F_DOBRS)
517 srs = (const struct ieee80211_rateset *)
518 ieee80211_get_suphtrates(ic, ni->ni_chan);
519 else
520 srs = ieee80211_get_suprates(ic, ni->ni_chan);
f186073c 521 for (i = 0; i < nrs->rs_nrates; ) {
f186073c
JS
522 if (flags & IEEE80211_F_DOSORT) {
523 /*
524 * Sort rates.
525 */
526 for (j = i + 1; j < nrs->rs_nrates; j++) {
527 if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
528 r = nrs->rs_rates[i];
529 nrs->rs_rates[i] = nrs->rs_rates[j];
530 nrs->rs_rates[j] = r;
531 }
532 }
533 }
534 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
535 badrate = r;
32176cfd
RP
536 /*
537 * Check for fixed rate.
538 */
539 if (r == ucastrate)
540 fixedrate = r;
541 /*
542 * Check against supported rates.
543 */
544 rix = findrix(srs, r);
545 if (flags & IEEE80211_F_DONEGO) {
546 if (rix < 0) {
f186073c
JS
547 /*
548 * A rate in the node's rate set is not
32176cfd
RP
549 * supported. If this is a basic rate and we
550 * are operating as a STA then this is an error.
551 * Otherwise we just discard/ignore the rate.
f186073c 552 */
32176cfd 553 if ((flags & IEEE80211_F_JOIN) &&
f186073c
JS
554 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
555 error++;
32176cfd
RP
556 } else if ((flags & IEEE80211_F_JOIN) == 0) {
557 /*
558 * Overwrite with the supported rate
559 * value so any basic rate bit is set.
560 */
561 nrs->rs_rates[i] = srs->rs_rates[rix];
f186073c
JS
562 }
563 }
32176cfd 564 if ((flags & IEEE80211_F_DODEL) && rix < 0) {
f186073c
JS
565 /*
566 * Delete unacceptable rates.
567 */
32176cfd
RP
568 nrs->rs_nrates--;
569 for (j = i; j < nrs->rs_nrates; j++)
570 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
571 nrs->rs_rates[j] = 0;
572 continue;
f186073c 573 }
32176cfd 574 if (rix >= 0)
f186073c
JS
575 okrate = nrs->rs_rates[i];
576 i++;
577 }
841ab66c 578 if (okrate == 0 || error != 0 ||
32176cfd
RP
579 ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
580 fixedrate != ucastrate)) {
581 IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
582 "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
583 "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
f186073c 584 return badrate | IEEE80211_RATE_BASIC;
32176cfd 585 } else
f186073c
JS
586 return RV(okrate);
587#undef RV
588}
589
841ab66c
SZ
590/*
591 * Reset 11g-related state.
592 */
593void
594ieee80211_reset_erp(struct ieee80211com *ic)
595{
596 ic->ic_flags &= ~IEEE80211_F_USEPROT;
597 ic->ic_nonerpsta = 0;
598 ic->ic_longslotsta = 0;
599 /*
600 * Short slot time is enabled only when operating in 11g
601 * and not in an IBSS. We must also honor whether or not
602 * the driver is capable of doing it.
603 */
604 ieee80211_set_shortslottime(ic,
32176cfd
RP
605 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
606 IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
607 (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
841ab66c
SZ
608 ic->ic_opmode == IEEE80211_M_HOSTAP &&
609 (ic->ic_caps & IEEE80211_C_SHSLOT)));
610 /*
611 * Set short preamble and ERP barker-preamble flags.
612 */
32176cfd
RP
613 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
614 (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
615 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
616 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
617 } else {
618 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
619 ic->ic_flags |= IEEE80211_F_USEBARKER;
620 }
841ab66c
SZ
621}
622
623/*
624 * Set the short slot time state and notify the driver.
625 */
626void
627ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
628{
629 if (onoff)
630 ic->ic_flags |= IEEE80211_F_SHSLOT;
631 else
632 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
32176cfd 633 /* notify driver */
841ab66c
SZ
634 if (ic->ic_updateslot != NULL)
635 ic->ic_updateslot(ic->ic_ifp);
636}
637
638/*
639 * Check if the specified rate set supports ERP.
640 * NB: the rate set is assumed to be sorted.
641 */
642int
32176cfd 643ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
841ab66c
SZ
644{
645#define N(a) (sizeof(a) / sizeof(a[0]))
646 static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
647 int i, j;
648
649 if (rs->rs_nrates < N(rates))
650 return 0;
651 for (i = 0; i < N(rates); i++) {
652 for (j = 0; j < rs->rs_nrates; j++) {
653 int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
654 if (rates[i] == r)
655 goto next;
656 if (r > rates[i])
657 return 0;
658 }
659 return 0;
660 next:
661 ;
662 }
663 return 1;
664#undef N
665}
666
667/*
32176cfd 668 * Mark the basic rates for the rate table based on the
841ab66c
SZ
669 * operating mode. For real 11g we mark all the 11b rates
670 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
671 * 11b rates. There's also a pseudo 11a-mode used to mark only
672 * the basic OFDM rates.
673 */
32176cfd
RP
674static void
675setbasicrates(struct ieee80211_rateset *rs,
676 enum ieee80211_phymode mode, int add)
841ab66c 677{
32176cfd 678 static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
9639b71d
SZ
679 [IEEE80211_MODE_11A] = { 3, { 12, 24, 48 } },
680 [IEEE80211_MODE_11B] = { 2, { 2, 4 } },
32176cfd 681 /* NB: mixed b/g */
9639b71d 682 [IEEE80211_MODE_11G] = { 4, { 2, 4, 11, 22 } },
9639b71d 683 [IEEE80211_MODE_TURBO_A] = { 3, { 12, 24, 48 } },
32176cfd
RP
684 [IEEE80211_MODE_TURBO_G] = { 4, { 2, 4, 11, 22 } },
685 [IEEE80211_MODE_STURBO_A] = { 3, { 12, 24, 48 } },
686 [IEEE80211_MODE_HALF] = { 3, { 6, 12, 24 } },
687 [IEEE80211_MODE_QUARTER] = { 3, { 3, 6, 12 } },
688 [IEEE80211_MODE_11NA] = { 3, { 12, 24, 48 } },
689 /* NB: mixed b/g */
690 [IEEE80211_MODE_11NG] = { 4, { 2, 4, 11, 22 } },
841ab66c
SZ
691 };
692 int i, j;
693
694 for (i = 0; i < rs->rs_nrates; i++) {
32176cfd
RP
695 if (!add)
696 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
697 for (j = 0; j < basic[mode].rs_nrates; j++)
698 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
841ab66c
SZ
699 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
700 break;
701 }
702 }
703}
704
32176cfd
RP
705/*
706 * Set the basic rates in a rate set.
707 */
708void
709ieee80211_setbasicrates(struct ieee80211_rateset *rs,
710 enum ieee80211_phymode mode)
208a1285 711{
32176cfd
RP
712 setbasicrates(rs, mode, 0);
713}
208a1285 714
32176cfd
RP
715/*
716 * Add basic rates to a rate set.
717 */
718void
719ieee80211_addbasicrates(struct ieee80211_rateset *rs,
720 enum ieee80211_phymode mode)
721{
722 setbasicrates(rs, mode, 1);
208a1285
SZ
723}
724
841ab66c 725/*
32176cfd
RP
726 * WME protocol support.
727 *
728 * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
729 * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
730 * Draft 2.0 Test Plan (Appendix D).
731 *
732 * Static/Dynamic Turbo mode settings come from Atheros.
841ab66c
SZ
733 */
734typedef struct phyParamType {
32176cfd
RP
735 uint8_t aifsn;
736 uint8_t logcwmin;
737 uint8_t logcwmax;
738 uint16_t txopLimit;
739 uint8_t acm;
841ab66c
SZ
740} paramType;
741
742static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
32176cfd
RP
743 [IEEE80211_MODE_AUTO] = { 3, 4, 6, 0, 0 },
744 [IEEE80211_MODE_11A] = { 3, 4, 6, 0, 0 },
745 [IEEE80211_MODE_11B] = { 3, 4, 6, 0, 0 },
746 [IEEE80211_MODE_11G] = { 3, 4, 6, 0, 0 },
747 [IEEE80211_MODE_FH] = { 3, 4, 6, 0, 0 },
748 [IEEE80211_MODE_TURBO_A]= { 2, 3, 5, 0, 0 },
749 [IEEE80211_MODE_TURBO_G]= { 2, 3, 5, 0, 0 },
750 [IEEE80211_MODE_STURBO_A]={ 2, 3, 5, 0, 0 },
751 [IEEE80211_MODE_HALF] = { 3, 4, 6, 0, 0 },
752 [IEEE80211_MODE_QUARTER]= { 3, 4, 6, 0, 0 },
753 [IEEE80211_MODE_11NA] = { 3, 4, 6, 0, 0 },
754 [IEEE80211_MODE_11NG] = { 3, 4, 6, 0, 0 },
841ab66c
SZ
755};
756static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
32176cfd
RP
757 [IEEE80211_MODE_AUTO] = { 7, 4, 10, 0, 0 },
758 [IEEE80211_MODE_11A] = { 7, 4, 10, 0, 0 },
759 [IEEE80211_MODE_11B] = { 7, 4, 10, 0, 0 },
760 [IEEE80211_MODE_11G] = { 7, 4, 10, 0, 0 },
761 [IEEE80211_MODE_FH] = { 7, 4, 10, 0, 0 },
762 [IEEE80211_MODE_TURBO_A]= { 7, 3, 10, 0, 0 },
763 [IEEE80211_MODE_TURBO_G]= { 7, 3, 10, 0, 0 },
764 [IEEE80211_MODE_STURBO_A]={ 7, 3, 10, 0, 0 },
765 [IEEE80211_MODE_HALF] = { 7, 4, 10, 0, 0 },
766 [IEEE80211_MODE_QUARTER]= { 7, 4, 10, 0, 0 },
767 [IEEE80211_MODE_11NA] = { 7, 4, 10, 0, 0 },
768 [IEEE80211_MODE_11NG] = { 7, 4, 10, 0, 0 },
841ab66c
SZ
769};
770static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
32176cfd
RP
771 [IEEE80211_MODE_AUTO] = { 1, 3, 4, 94, 0 },
772 [IEEE80211_MODE_11A] = { 1, 3, 4, 94, 0 },
773 [IEEE80211_MODE_11B] = { 1, 3, 4, 188, 0 },
774 [IEEE80211_MODE_11G] = { 1, 3, 4, 94, 0 },
775 [IEEE80211_MODE_FH] = { 1, 3, 4, 188, 0 },
776 [IEEE80211_MODE_TURBO_A]= { 1, 2, 3, 94, 0 },
777 [IEEE80211_MODE_TURBO_G]= { 1, 2, 3, 94, 0 },
778 [IEEE80211_MODE_STURBO_A]={ 1, 2, 3, 94, 0 },
779 [IEEE80211_MODE_HALF] = { 1, 3, 4, 94, 0 },
780 [IEEE80211_MODE_QUARTER]= { 1, 3, 4, 94, 0 },
781 [IEEE80211_MODE_11NA] = { 1, 3, 4, 94, 0 },
782 [IEEE80211_MODE_11NG] = { 1, 3, 4, 94, 0 },
841ab66c
SZ
783};
784static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
32176cfd
RP
785 [IEEE80211_MODE_AUTO] = { 1, 2, 3, 47, 0 },
786 [IEEE80211_MODE_11A] = { 1, 2, 3, 47, 0 },
787 [IEEE80211_MODE_11B] = { 1, 2, 3, 102, 0 },
788 [IEEE80211_MODE_11G] = { 1, 2, 3, 47, 0 },
789 [IEEE80211_MODE_FH] = { 1, 2, 3, 102, 0 },
790 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
791 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
792 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
793 [IEEE80211_MODE_HALF] = { 1, 2, 3, 47, 0 },
794 [IEEE80211_MODE_QUARTER]= { 1, 2, 3, 47, 0 },
795 [IEEE80211_MODE_11NA] = { 1, 2, 3, 47, 0 },
796 [IEEE80211_MODE_11NG] = { 1, 2, 3, 47, 0 },
841ab66c
SZ
797};
798
799static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
32176cfd
RP
800 [IEEE80211_MODE_AUTO] = { 3, 4, 10, 0, 0 },
801 [IEEE80211_MODE_11A] = { 3, 4, 10, 0, 0 },
802 [IEEE80211_MODE_11B] = { 3, 4, 10, 0, 0 },
803 [IEEE80211_MODE_11G] = { 3, 4, 10, 0, 0 },
804 [IEEE80211_MODE_FH] = { 3, 4, 10, 0, 0 },
805 [IEEE80211_MODE_TURBO_A]= { 2, 3, 10, 0, 0 },
806 [IEEE80211_MODE_TURBO_G]= { 2, 3, 10, 0, 0 },
807 [IEEE80211_MODE_STURBO_A]={ 2, 3, 10, 0, 0 },
808 [IEEE80211_MODE_HALF] = { 3, 4, 10, 0, 0 },
809 [IEEE80211_MODE_QUARTER]= { 3, 4, 10, 0, 0 },
810 [IEEE80211_MODE_11NA] = { 3, 4, 10, 0, 0 },
811 [IEEE80211_MODE_11NG] = { 3, 4, 10, 0, 0 },
841ab66c
SZ
812};
813static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
32176cfd
RP
814 [IEEE80211_MODE_AUTO] = { 2, 3, 4, 94, 0 },
815 [IEEE80211_MODE_11A] = { 2, 3, 4, 94, 0 },
816 [IEEE80211_MODE_11B] = { 2, 3, 4, 188, 0 },
817 [IEEE80211_MODE_11G] = { 2, 3, 4, 94, 0 },
818 [IEEE80211_MODE_FH] = { 2, 3, 4, 188, 0 },
819 [IEEE80211_MODE_TURBO_A]= { 2, 2, 3, 94, 0 },
820 [IEEE80211_MODE_TURBO_G]= { 2, 2, 3, 94, 0 },
821 [IEEE80211_MODE_STURBO_A]={ 2, 2, 3, 94, 0 },
822 [IEEE80211_MODE_HALF] = { 2, 3, 4, 94, 0 },
823 [IEEE80211_MODE_QUARTER]= { 2, 3, 4, 94, 0 },
824 [IEEE80211_MODE_11NA] = { 2, 3, 4, 94, 0 },
825 [IEEE80211_MODE_11NG] = { 2, 3, 4, 94, 0 },
841ab66c
SZ
826};
827static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
32176cfd
RP
828 [IEEE80211_MODE_AUTO] = { 2, 2, 3, 47, 0 },
829 [IEEE80211_MODE_11A] = { 2, 2, 3, 47, 0 },
830 [IEEE80211_MODE_11B] = { 2, 2, 3, 102, 0 },
831 [IEEE80211_MODE_11G] = { 2, 2, 3, 47, 0 },
832 [IEEE80211_MODE_FH] = { 2, 2, 3, 102, 0 },
833 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
834 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
835 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
836 [IEEE80211_MODE_HALF] = { 2, 2, 3, 47, 0 },
837 [IEEE80211_MODE_QUARTER]= { 2, 2, 3, 47, 0 },
838 [IEEE80211_MODE_11NA] = { 2, 2, 3, 47, 0 },
839 [IEEE80211_MODE_11NG] = { 2, 2, 3, 47, 0 },
841ab66c
SZ
840};
841
32176cfd
RP
842static void
843_setifsparams(struct wmeParams *wmep, const paramType *phy)
844{
845 wmep->wmep_aifsn = phy->aifsn;
846 wmep->wmep_logcwmin = phy->logcwmin;
847 wmep->wmep_logcwmax = phy->logcwmax;
848 wmep->wmep_txopLimit = phy->txopLimit;
849}
850
851static void
852setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
853 struct wmeParams *wmep, const paramType *phy)
841ab66c 854{
32176cfd
RP
855 wmep->wmep_acm = phy->acm;
856 _setifsparams(wmep, phy);
857
858 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
859 "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
860 ieee80211_wme_acnames[ac], type,
861 wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
862 wmep->wmep_logcwmax, wmep->wmep_txopLimit);
863}
864
865static void
866ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
867{
868 struct ieee80211com *ic = vap->iv_ic;
841ab66c
SZ
869 struct ieee80211_wme_state *wme = &ic->ic_wme;
870 const paramType *pPhyParam, *pBssPhyParam;
871 struct wmeParams *wmep;
32176cfd 872 enum ieee80211_phymode mode;
841ab66c
SZ
873 int i;
874
32176cfd 875 if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
841ab66c
SZ
876 return;
877
32176cfd
RP
878 /*
879 * Select mode; we can be called early in which case we
880 * always use auto mode. We know we'll be called when
881 * entering the RUN state with bsschan setup properly
882 * so state will eventually get set correctly
883 */
884 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
885 mode = ieee80211_chan2mode(ic->ic_bsschan);
886 else
887 mode = IEEE80211_MODE_AUTO;
841ab66c
SZ
888 for (i = 0; i < WME_NUM_AC; i++) {
889 switch (i) {
890 case WME_AC_BK:
32176cfd
RP
891 pPhyParam = &phyParamForAC_BK[mode];
892 pBssPhyParam = &phyParamForAC_BK[mode];
841ab66c
SZ
893 break;
894 case WME_AC_VI:
32176cfd
RP
895 pPhyParam = &phyParamForAC_VI[mode];
896 pBssPhyParam = &bssPhyParamForAC_VI[mode];
841ab66c
SZ
897 break;
898 case WME_AC_VO:
32176cfd
RP
899 pPhyParam = &phyParamForAC_VO[mode];
900 pBssPhyParam = &bssPhyParamForAC_VO[mode];
841ab66c
SZ
901 break;
902 case WME_AC_BE:
903 default:
32176cfd
RP
904 pPhyParam = &phyParamForAC_BE[mode];
905 pBssPhyParam = &bssPhyParamForAC_BE[mode];
841ab66c
SZ
906 break;
907 }
841ab66c
SZ
908 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
909 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
32176cfd 910 setwmeparams(vap, "chan", i, wmep, pPhyParam);
841ab66c 911 } else {
32176cfd 912 setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
841ab66c 913 }
841ab66c 914 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
32176cfd 915 setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
841ab66c
SZ
916 }
917 /* NB: check ic_bss to avoid NULL deref on initial attach */
32176cfd 918 if (vap->iv_bss != NULL) {
841ab66c
SZ
919 /*
920 * Calculate agressive mode switching threshold based
921 * on beacon interval. This doesn't need locking since
922 * we're only called before entering the RUN state at
923 * which point we start sending beacon frames.
924 */
925 wme->wme_hipri_switch_thresh =
32176cfd
RP
926 (HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
927 wme->wme_flags &= ~WME_F_AGGRMODE;
928 ieee80211_wme_updateparams(vap);
841ab66c
SZ
929 }
930}
931
32176cfd
RP
932void
933ieee80211_wme_initparams(struct ieee80211vap *vap)
934{
935 struct ieee80211com *ic = vap->iv_ic;
936
e8361ca0 937 ic = vap->iv_ic;
32176cfd 938 ieee80211_wme_initparams_locked(vap);
32176cfd
RP
939}
940
841ab66c
SZ
941/*
942 * Update WME parameters for ourself and the BSS.
943 */
944void
32176cfd 945ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
841ab66c 946{
32176cfd
RP
947 static const paramType aggrParam[IEEE80211_MODE_MAX] = {
948 [IEEE80211_MODE_AUTO] = { 2, 4, 10, 64, 0 },
949 [IEEE80211_MODE_11A] = { 2, 4, 10, 64, 0 },
950 [IEEE80211_MODE_11B] = { 2, 5, 10, 64, 0 },
951 [IEEE80211_MODE_11G] = { 2, 4, 10, 64, 0 },
952 [IEEE80211_MODE_FH] = { 2, 5, 10, 64, 0 },
953 [IEEE80211_MODE_TURBO_A] = { 1, 3, 10, 64, 0 },
954 [IEEE80211_MODE_TURBO_G] = { 1, 3, 10, 64, 0 },
955 [IEEE80211_MODE_STURBO_A] = { 1, 3, 10, 64, 0 },
956 [IEEE80211_MODE_HALF] = { 2, 4, 10, 64, 0 },
957 [IEEE80211_MODE_QUARTER] = { 2, 4, 10, 64, 0 },
958 [IEEE80211_MODE_11NA] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
959 [IEEE80211_MODE_11NG] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
841ab66c 960 };
32176cfd 961 struct ieee80211com *ic = vap->iv_ic;
841ab66c
SZ
962 struct ieee80211_wme_state *wme = &ic->ic_wme;
963 const struct wmeParams *wmep;
964 struct wmeParams *chanp, *bssp;
32176cfd 965 enum ieee80211_phymode mode;
841ab66c
SZ
966 int i;
967
32176cfd
RP
968 /*
969 * Set up the channel access parameters for the physical
970 * device. First populate the configured settings.
971 */
841ab66c
SZ
972 for (i = 0; i < WME_NUM_AC; i++) {
973 chanp = &wme->wme_chanParams.cap_wmeParams[i];
974 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
975 chanp->wmep_aifsn = wmep->wmep_aifsn;
976 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
977 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
978 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
979
980 chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
981 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
982 chanp->wmep_aifsn = wmep->wmep_aifsn;
983 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
984 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
985 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
986 }
987
988 /*
32176cfd
RP
989 * Select mode; we can be called early in which case we
990 * always use auto mode. We know we'll be called when
991 * entering the RUN state with bsschan setup properly
992 * so state will eventually get set correctly
993 */
994 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
995 mode = ieee80211_chan2mode(ic->ic_bsschan);
996 else
997 mode = IEEE80211_MODE_AUTO;
998
999 /*
841ab66c
SZ
1000 * This implements agressive mode as found in certain
1001 * vendors' AP's. When there is significant high
1002 * priority (VI/VO) traffic in the BSS throttle back BE
1003 * traffic by using conservative parameters. Otherwise
1004 * BE uses agressive params to optimize performance of
1005 * legacy/non-QoS traffic.
1006 */
32176cfd 1007 if ((vap->iv_opmode == IEEE80211_M_HOSTAP &&
841ab66c 1008 (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
32176cfd
RP
1009 (vap->iv_opmode == IEEE80211_M_STA &&
1010 (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
1011 (vap->iv_flags & IEEE80211_F_WME) == 0) {
841ab66c
SZ
1012 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1013 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1014
32176cfd 1015 chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
841ab66c 1016 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
32176cfd 1017 aggrParam[mode].logcwmin;
841ab66c 1018 chanp->wmep_logcwmax = bssp->wmep_logcwmax =
32176cfd 1019 aggrParam[mode].logcwmax;
841ab66c 1020 chanp->wmep_txopLimit = bssp->wmep_txopLimit =
32176cfd
RP
1021 (vap->iv_flags & IEEE80211_F_BURST) ?
1022 aggrParam[mode].txopLimit : 0;
1023 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1024 "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1025 "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1026 chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1027 chanp->wmep_logcwmax, chanp->wmep_txopLimit);
841ab66c
SZ
1028 }
1029
32176cfd 1030 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
841ab66c 1031 ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
32176cfd
RP
1032 static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1033 [IEEE80211_MODE_AUTO] = 3,
1034 [IEEE80211_MODE_11A] = 3,
1035 [IEEE80211_MODE_11B] = 4,
1036 [IEEE80211_MODE_11G] = 3,
1037 [IEEE80211_MODE_FH] = 4,
1038 [IEEE80211_MODE_TURBO_A] = 3,
1039 [IEEE80211_MODE_TURBO_G] = 3,
1040 [IEEE80211_MODE_STURBO_A] = 3,
1041 [IEEE80211_MODE_HALF] = 3,
1042 [IEEE80211_MODE_QUARTER] = 3,
1043 [IEEE80211_MODE_11NA] = 3,
1044 [IEEE80211_MODE_11NG] = 3,
841ab66c
SZ
1045 };
1046 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1047 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1048
32176cfd
RP
1049 chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1050 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1051 "update %s (chan+bss) logcwmin %u\n",
1052 ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
841ab66c 1053 }
32176cfd 1054 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { /* XXX ibss? */
841ab66c
SZ
1055 /*
1056 * Arrange for a beacon update and bump the parameter
1057 * set number so associated stations load the new values.
1058 */
1059 wme->wme_bssChanParams.cap_info =
1060 (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
32176cfd 1061 ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
841ab66c
SZ
1062 }
1063
1064 wme->wme_update(ic);
1065
32176cfd
RP
1066 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1067 "%s: WME params updated, cap_info 0x%x\n", __func__,
1068 vap->iv_opmode == IEEE80211_M_STA ?
1069 wme->wme_wmeChanParams.cap_info :
1070 wme->wme_bssChanParams.cap_info);
841ab66c
SZ
1071}
1072
1073void
32176cfd 1074ieee80211_wme_updateparams(struct ieee80211vap *vap)
841ab66c 1075{
32176cfd 1076 struct ieee80211com *ic = vap->iv_ic;
841ab66c 1077
32176cfd 1078 if (ic->ic_caps & IEEE80211_C_WME) {
32176cfd 1079 ieee80211_wme_updateparams_locked(vap);
841ab66c 1080 }
32176cfd
RP
1081}
1082
1083static void
1084parent_updown(void *arg, int npending)
1085{
1086 struct ifnet *parent = arg;
1087
34a60cf6 1088 parent->if_ioctl(parent, SIOCSIFFLAGS, NULL, curthread->td_ucred);
32176cfd
RP
1089}
1090
1091static void
1092update_mcast(void *arg, int npending)
1093{
1094 struct ieee80211com *ic = arg;
1095 struct ifnet *parent = ic->ic_ifp;
1096
1097 ic->ic_update_mcast(parent);
1098}
1099
1100static void
1101update_promisc(void *arg, int npending)
1102{
1103 struct ieee80211com *ic = arg;
1104 struct ifnet *parent = ic->ic_ifp;
1105
1106 ic->ic_update_promisc(parent);
1107}
1108
1109static void
1110update_channel(void *arg, int npending)
1111{
1112 struct ieee80211com *ic = arg;
1113
1114 ic->ic_set_channel(ic);
1115 ieee80211_radiotap_chan_change(ic);
1116}
1117
1118/*
1119 * Block until the parent is in a known state. This is
1120 * used after any operations that dispatch a task (e.g.
1121 * to auto-configure the parent device up/down).
1122 */
1123void
1124ieee80211_waitfor_parent(struct ieee80211com *ic)
1125{
1126 taskqueue_block(ic->ic_tq);
1127 ieee80211_draintask(ic, &ic->ic_parent_task);
1128 ieee80211_draintask(ic, &ic->ic_mcast_task);
1129 ieee80211_draintask(ic, &ic->ic_promisc_task);
1130 ieee80211_draintask(ic, &ic->ic_chan_task);
1131 ieee80211_draintask(ic, &ic->ic_bmiss_task);
1132 taskqueue_unblock(ic->ic_tq);
1133}
1134
1135/*
1136 * Start a vap running. If this is the first vap to be
1137 * set running on the underlying device then we
1138 * automatically bring the device up.
1139 */
1140void
1141ieee80211_start_locked(struct ieee80211vap *vap)
1142{
1143 struct ifnet *ifp = vap->iv_ifp;
1144 struct ieee80211com *ic = vap->iv_ic;
1145 struct ifnet *parent = ic->ic_ifp;
1146
32176cfd 1147 IEEE80211_DPRINTF(vap,
841ab66c 1148 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
32176cfd 1149 "start running, %d vaps running\n", ic->ic_nrunning);
841ab66c 1150
34a60cf6 1151 if ((ifp->if_flags & IFF_RUNNING) == 0) {
32176cfd
RP
1152 /*
1153 * Mark us running. Note that it's ok to do this first;
1154 * if we need to bring the parent device up we defer that
1155 * to avoid dropping the com lock. We expect the device
1156 * to respond to being marked up by calling back into us
1157 * through ieee80211_start_all at which point we'll come
1158 * back in here and complete the work.
1159 */
34a60cf6 1160 ifp->if_flags |= IFF_RUNNING;
32176cfd
RP
1161 /*
1162 * We are not running; if this we are the first vap
1163 * to be brought up auto-up the parent if necessary.
1164 */
1165 if (ic->ic_nrunning++ == 0 &&
34a60cf6 1166 (parent->if_flags & IFF_RUNNING) == 0) {
32176cfd
RP
1167 IEEE80211_DPRINTF(vap,
1168 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1169 "%s: up parent %s\n", __func__, parent->if_xname);
1170 parent->if_flags |= IFF_UP;
1171 ieee80211_runtask(ic, &ic->ic_parent_task);
1172 return;
1173 }
1174 }
841ab66c 1175 /*
32176cfd
RP
1176 * If the parent is up and running, then kick the
1177 * 802.11 state machine as appropriate.
841ab66c 1178 */
34a60cf6 1179 if ((parent->if_flags & IFF_RUNNING) &&
32176cfd
RP
1180 vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1181 if (vap->iv_opmode == IEEE80211_M_STA) {
1182#if 0
1183 /* XXX bypasses scan too easily; disable for now */
1184 /*
1185 * Try to be intelligent about clocking the state
1186 * machine. If we're currently in RUN state then
1187 * we should be able to apply any new state/parameters
1188 * simply by re-associating. Otherwise we need to
1189 * re-scan to select an appropriate ap.
1190 */
1191 if (vap->iv_state >= IEEE80211_S_RUN)
1192 ieee80211_new_state_locked(vap,
1193 IEEE80211_S_ASSOC, 1);
1194 else
1195#endif
1196 ieee80211_new_state_locked(vap,
1197 IEEE80211_S_SCAN, 0);
1198 } else {
1199 /*
1200 * For monitor+wds mode there's nothing to do but
1201 * start running. Otherwise if this is the first
1202 * vap to be brought up, start a scan which may be
1203 * preempted if the station is locked to a particular
1204 * channel.
1205 */
1206 vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1207 if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1208 vap->iv_opmode == IEEE80211_M_WDS)
1209 ieee80211_new_state_locked(vap,
1210 IEEE80211_S_RUN, -1);
1211 else
1212 ieee80211_new_state_locked(vap,
1213 IEEE80211_S_SCAN, 0);
1214 }
1215 }
1216}
1217
1218/*
1219 * Start a single vap.
1220 */
1221void
1222ieee80211_init(void *arg)
1223{
1224 struct ieee80211vap *vap = arg;
1225
1226 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1227 "%s\n", __func__);
1228
32176cfd 1229 ieee80211_start_locked(vap);
32176cfd
RP
1230}
1231
1232/*
1233 * Start all runnable vap's on a device.
1234 */
1235void
1236ieee80211_start_all(struct ieee80211com *ic)
1237{
1238 struct ieee80211vap *vap;
841ab66c 1239
32176cfd
RP
1240 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1241 struct ifnet *ifp = vap->iv_ifp;
1242 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1243 ieee80211_start_locked(vap);
1244 }
32176cfd
RP
1245}
1246
1247/*
1248 * Stop a vap. We force it down using the state machine
1249 * then mark it's ifnet not running. If this is the last
1250 * vap running on the underlying device then we close it
1251 * too to insure it will be properly initialized when the
1252 * next vap is brought up.
1253 */
1254void
1255ieee80211_stop_locked(struct ieee80211vap *vap)
1256{
1257 struct ieee80211com *ic = vap->iv_ic;
1258 struct ifnet *ifp = vap->iv_ifp;
1259 struct ifnet *parent = ic->ic_ifp;
1260
32176cfd
RP
1261 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1262 "stop running, %d vaps running\n", ic->ic_nrunning);
1263
1264 ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
34a60cf6
RP
1265 if (ifp->if_flags & IFF_RUNNING) {
1266 ifp->if_flags &= ~IFF_RUNNING; /* mark us stopped */
32176cfd 1267 if (--ic->ic_nrunning == 0 &&
34a60cf6 1268 (parent->if_flags & IFF_RUNNING)) {
32176cfd
RP
1269 IEEE80211_DPRINTF(vap,
1270 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1271 "down parent %s\n", parent->if_xname);
1272 parent->if_flags &= ~IFF_UP;
1273 ieee80211_runtask(ic, &ic->ic_parent_task);
1274 }
1275 }
1276}
1277
1278void
1279ieee80211_stop(struct ieee80211vap *vap)
1280{
1281 struct ieee80211com *ic = vap->iv_ic;
1282
e8361ca0 1283 ic = vap->iv_ic;
32176cfd 1284 ieee80211_stop_locked(vap);
32176cfd
RP
1285}
1286
1287/*
1288 * Stop all vap's running on a device.
1289 */
1290void
1291ieee80211_stop_all(struct ieee80211com *ic)
1292{
1293 struct ieee80211vap *vap;
1294
32176cfd
RP
1295 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1296 struct ifnet *ifp = vap->iv_ifp;
1297 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1298 ieee80211_stop_locked(vap);
1299 }
32176cfd
RP
1300
1301 ieee80211_waitfor_parent(ic);
1302}
1303
1304/*
1305 * Stop all vap's running on a device and arrange
1306 * for those that were running to be resumed.
1307 */
1308void
1309ieee80211_suspend_all(struct ieee80211com *ic)
1310{
1311 struct ieee80211vap *vap;
1312
32176cfd
RP
1313 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1314 struct ifnet *ifp = vap->iv_ifp;
1315 if (IFNET_IS_UP_RUNNING(ifp)) { /* NB: avoid recursion */
1316 vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1317 ieee80211_stop_locked(vap);
1318 }
1319 }
32176cfd
RP
1320
1321 ieee80211_waitfor_parent(ic);
1322}
1323
1324/*
1325 * Start all vap's marked for resume.
1326 */
1327void
1328ieee80211_resume_all(struct ieee80211com *ic)
1329{
1330 struct ieee80211vap *vap;
1331
32176cfd
RP
1332 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1333 struct ifnet *ifp = vap->iv_ifp;
1334 if (!IFNET_IS_UP_RUNNING(ifp) &&
1335 (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1336 vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1337 ieee80211_start_locked(vap);
1338 }
1339 }
32176cfd
RP
1340}
1341
1342void
1343ieee80211_beacon_miss(struct ieee80211com *ic)
1344{
32176cfd
RP
1345 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1346 /* Process in a taskq, the handler may reenter the driver */
1347 ieee80211_runtask(ic, &ic->ic_bmiss_task);
1348 }
32176cfd
RP
1349}
1350
1351static void
1352beacon_miss(void *arg, int npending)
1353{
1354 struct ieee80211com *ic = arg;
1355 struct ieee80211vap *vap;
1356
1357 /* XXX locking */
1358 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
841ab66c 1359 /*
32176cfd
RP
1360 * We only pass events through for sta vap's in RUN state;
1361 * may be too restrictive but for now this saves all the
1362 * handlers duplicating these checks.
841ab66c 1363 */
32176cfd
RP
1364 if (vap->iv_opmode == IEEE80211_M_STA &&
1365 vap->iv_state >= IEEE80211_S_RUN &&
1366 vap->iv_bmiss != NULL)
1367 vap->iv_bmiss(vap);
841ab66c 1368 }
32176cfd
RP
1369}
1370
1371static void
1372beacon_swmiss(void *arg, int npending)
1373{
1374 struct ieee80211vap *vap = arg;
1375
1376 if (vap->iv_state != IEEE80211_S_RUN)
1377 return;
1378
1379 /* XXX Call multiple times if npending > zero? */
1380 vap->iv_bmiss(vap);
841ab66c
SZ
1381}
1382
1383/*
1384 * Software beacon miss handling. Check if any beacons
1385 * were received in the last period. If not post a
1386 * beacon miss; otherwise reset the counter.
1387 */
32176cfd 1388void
841ab66c
SZ
1389ieee80211_swbmiss(void *arg)
1390{
32176cfd
RP
1391 struct ieee80211vap *vap = arg;
1392 struct ieee80211com *ic = vap->iv_ic;
841ab66c 1393
32176cfd
RP
1394 /* XXX sleep state? */
1395 KASSERT(vap->iv_state == IEEE80211_S_RUN,
1396 ("wrong state %d", vap->iv_state));
841ab66c 1397
32176cfd
RP
1398 if (ic->ic_flags & IEEE80211_F_SCAN) {
1399 /*
1400 * If scanning just ignore and reset state. If we get a
1401 * bmiss after coming out of scan because we haven't had
1402 * time to receive a beacon then we should probe the AP
1403 * before posting a real bmiss (unless iv_bmiss_max has
1404 * been artifiically lowered). A cleaner solution might
1405 * be to disable the timer on scan start/end but to handle
1406 * case of multiple sta vap's we'd need to disable the
1407 * timers of all affected vap's.
1408 */
1409 vap->iv_swbmiss_count = 0;
1410 } else if (vap->iv_swbmiss_count == 0) {
1411 if (vap->iv_bmiss != NULL)
1412 ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1413 if (vap->iv_bmiss_count == 0) /* don't re-arm timer */
1414 return;
841ab66c 1415 } else
32176cfd
RP
1416 vap->iv_swbmiss_count = 0;
1417 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1418 ieee80211_swbmiss, vap);
1419}
841ab66c 1420
32176cfd
RP
1421/*
1422 * Start an 802.11h channel switch. We record the parameters,
1423 * mark the operation pending, notify each vap through the
1424 * beacon update mechanism so it can update the beacon frame
1425 * contents, and then switch vap's to CSA state to block outbound
1426 * traffic. Devices that handle CSA directly can use the state
1427 * switch to do the right thing so long as they call
1428 * ieee80211_csa_completeswitch when it's time to complete the
1429 * channel change. Devices that depend on the net80211 layer can
1430 * use ieee80211_beacon_update to handle the countdown and the
1431 * channel switch.
1432 */
1433void
1434ieee80211_csa_startswitch(struct ieee80211com *ic,
1435 struct ieee80211_channel *c, int mode, int count)
1436{
1437 struct ieee80211vap *vap;
1438
32176cfd
RP
1439 ic->ic_csa_newchan = c;
1440 ic->ic_csa_mode = mode;
1441 ic->ic_csa_count = count;
1442 ic->ic_flags |= IEEE80211_F_CSAPENDING;
1443 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1444 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1445 vap->iv_opmode == IEEE80211_M_IBSS ||
1446 vap->iv_opmode == IEEE80211_M_MBSS)
1447 ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1448 /* switch to CSA state to block outbound traffic */
1449 if (vap->iv_state == IEEE80211_S_RUN)
1450 ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1451 }
1452 ieee80211_notify_csa(ic, c, mode, count);
841ab66c
SZ
1453}
1454
1455static void
32176cfd 1456csa_completeswitch(struct ieee80211com *ic)
841ab66c 1457{
32176cfd 1458 struct ieee80211vap *vap;
841ab66c 1459
32176cfd
RP
1460 ic->ic_csa_newchan = NULL;
1461 ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1462
1463 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1464 if (vap->iv_state == IEEE80211_S_CSA)
1465 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
841ab66c
SZ
1466}
1467
32176cfd
RP
1468/*
1469 * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1470 * We clear state and move all vap's in CSA state to RUN state
1471 * so they can again transmit.
1472 */
846cf0bc 1473void
32176cfd 1474ieee80211_csa_completeswitch(struct ieee80211com *ic)
841ab66c 1475{
32176cfd 1476 KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
846cf0bc 1477
32176cfd
RP
1478 ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1479 csa_completeswitch(ic);
1480}
846cf0bc 1481
32176cfd
RP
1482/*
1483 * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1484 * We clear state and move all vap's in CSA state to RUN state
1485 * so they can again transmit.
1486 */
1487void
1488ieee80211_csa_cancelswitch(struct ieee80211com *ic)
1489{
32176cfd
RP
1490 csa_completeswitch(ic);
1491}
1492
1493/*
1494 * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1495 * We clear state and move all vap's in CAC state to RUN state.
1496 */
1497void
1498ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1499{
1500 struct ieee80211com *ic = vap0->iv_ic;
1501 struct ieee80211vap *vap;
1502
32176cfd
RP
1503 /*
1504 * Complete CAC state change for lead vap first; then
1505 * clock all the other vap's waiting.
1506 */
1507 KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1508 ("wrong state %d", vap0->iv_state));
1509 ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1510
1511 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1512 if (vap->iv_state == IEEE80211_S_CAC)
1513 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
32176cfd
RP
1514}
1515
1516/*
1517 * Force all vap's other than the specified vap to the INIT state
1518 * and mark them as waiting for a scan to complete. These vaps
1519 * will be brought up when the scan completes and the scanning vap
1520 * reaches RUN state by wakeupwaiting.
1521 */
1522static void
1523markwaiting(struct ieee80211vap *vap0)
1524{
1525 struct ieee80211com *ic = vap0->iv_ic;
1526 struct ieee80211vap *vap;
1527
32176cfd
RP
1528 /*
1529 * A vap list entry can not disappear since we are running on the
1530 * taskqueue and a vap destroy will queue and drain another state
1531 * change task.
1532 */
1533 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1534 if (vap == vap0)
1535 continue;
1536 if (vap->iv_state != IEEE80211_S_INIT) {
1537 /* NB: iv_newstate may drop the lock */
1538 vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1539 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
846cf0bc 1540 }
32176cfd
RP
1541 }
1542}
1543
1544/*
1545 * Wakeup all vap's waiting for a scan to complete. This is the
1546 * companion to markwaiting (above) and is used to coordinate
1547 * multiple vaps scanning.
1548 * This is called from the state taskqueue.
1549 */
1550static void
1551wakeupwaiting(struct ieee80211vap *vap0)
1552{
1553 struct ieee80211com *ic = vap0->iv_ic;
1554 struct ieee80211vap *vap;
1555
32176cfd
RP
1556 /*
1557 * A vap list entry can not disappear since we are running on the
1558 * taskqueue and a vap destroy will queue and drain another state
1559 * change task.
1560 */
1561 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1562 if (vap == vap0)
1563 continue;
1564 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1565 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1566 /* NB: sta's cannot go INIT->RUN */
1567 /* NB: iv_newstate may drop the lock */
1568 vap->iv_newstate(vap,
1569 vap->iv_opmode == IEEE80211_M_STA ?
1570 IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
846cf0bc 1571 }
846cf0bc 1572 }
32176cfd
RP
1573}
1574
1575/*
1576 * Handle post state change work common to all operating modes.
1577 */
1578static void
1579ieee80211_newstate_cb(void *xvap, int npending)
1580{
1581 struct ieee80211vap *vap = xvap;
1582 struct ieee80211com *ic = vap->iv_ic;
1583 enum ieee80211_state nstate, ostate;
1584 int arg, rc;
841ab66c 1585
32176cfd
RP
1586 nstate = vap->iv_nstate;
1587 arg = vap->iv_nstate_arg;
1588
1589 if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
1590 /*
1591 * We have been requested to drop back to the INIT before
1592 * proceeding to the new state.
1593 */
1594 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1595 "%s: %s -> %s arg %d\n", __func__,
1596 ieee80211_state_name[vap->iv_state],
1597 ieee80211_state_name[IEEE80211_S_INIT], arg);
1598 vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1599 vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
1600 }
1601
1602 ostate = vap->iv_state;
1603 if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
1604 /*
1605 * SCAN was forced; e.g. on beacon miss. Force other running
1606 * vap's to INIT state and mark them as waiting for the scan to
1607 * complete. This insures they don't interfere with our
1608 * scanning. Since we are single threaded the vaps can not
1609 * transition again while we are executing.
1610 *
1611 * XXX not always right, assumes ap follows sta
1612 */
1613 markwaiting(vap);
1614 }
1615 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1616 "%s: %s -> %s arg %d\n", __func__,
1617 ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
1618
1619 rc = vap->iv_newstate(vap, nstate, arg);
1620 vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
1621 if (rc != 0) {
1622 /* State transition failed */
1623 KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
1624 KASSERT(nstate != IEEE80211_S_INIT,
1625 ("INIT state change failed"));
1626 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1627 "%s: %s returned error %d\n", __func__,
1628 ieee80211_state_name[nstate], rc);
1629 goto done;
1630 }
1631
1632 /* No actual transition, skip post processing */
1633 if (ostate == nstate)
1634 goto done;
1635
1636 if (nstate == IEEE80211_S_RUN) {
1637 /*
1638 * OACTIVE may be set on the vap if the upper layer
1639 * tried to transmit (e.g. IPv6 NDP) before we reach
1640 * RUN state. Clear it and restart xmit.
1641 *
1642 * Note this can also happen as a result of SLEEP->RUN
1643 * (i.e. coming out of power save mode).
1644 */
34a60cf6 1645 vap->iv_ifp->if_flags &= ~IFF_OACTIVE;
6168f72e 1646 vap->iv_ifp->if_start(vap->iv_ifp);
32176cfd
RP
1647
1648 /* bring up any vaps waiting on us */
1649 wakeupwaiting(vap);
1650 } else if (nstate == IEEE80211_S_INIT) {
1651 /*
1652 * Flush the scan cache if we did the last scan (XXX?)
1653 * and flush any frames on send queues from this vap.
1654 * Note the mgt q is used only for legacy drivers and
1655 * will go away shortly.
1656 */
1657 ieee80211_scan_flush(vap);
1658
1659 /* XXX NB: cast for altq */
1660 ieee80211_flush_ifq((struct ifqueue *)&ic->ic_ifp->if_snd, vap);
1661 }
1662done:
26c6f223 1663 ;
841ab66c
SZ
1664}
1665
32176cfd
RP
1666/*
1667 * Public interface for initiating a state machine change.
1668 * This routine single-threads the request and coordinates
1669 * the scheduling of multiple vaps for the purpose of selecting
1670 * an operating channel. Specifically the following scenarios
1671 * are handled:
1672 * o only one vap can be selecting a channel so on transition to
1673 * SCAN state if another vap is already scanning then
1674 * mark the caller for later processing and return without
1675 * doing anything (XXX? expectations by caller of synchronous operation)
1676 * o only one vap can be doing CAC of a channel so on transition to
1677 * CAC state if another vap is already scanning for radar then
1678 * mark the caller for later processing and return without
1679 * doing anything (XXX? expectations by caller of synchronous operation)
1680 * o if another vap is already running when a request is made
1681 * to SCAN then an operating channel has been chosen; bypass
1682 * the scan and just join the channel
1683 *
1684 * Note that the state change call is done through the iv_newstate
1685 * method pointer so any driver routine gets invoked. The driver
1686 * will normally call back into operating mode-specific
1687 * ieee80211_newstate routines (below) unless it needs to completely
1688 * bypass the state machine (e.g. because the firmware has it's
1689 * own idea how things should work). Bypassing the net80211 layer
1690 * is usually a mistake and indicates lack of proper integration
1691 * with the net80211 layer.
1692 */
f186073c 1693static int
32176cfd
RP
1694ieee80211_new_state_locked(struct ieee80211vap *vap,
1695 enum ieee80211_state nstate, int arg)
f186073c 1696{
32176cfd
RP
1697 struct ieee80211com *ic = vap->iv_ic;
1698 struct ieee80211vap *vp;
f186073c 1699 enum ieee80211_state ostate;
32176cfd 1700 int nrunning, nscanning;
f186073c 1701
32176cfd
RP
1702 if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
1703 if (vap->iv_nstate == IEEE80211_S_INIT) {
841ab66c 1704 /*
32176cfd
RP
1705 * XXX The vap is being stopped, do no allow any other
1706 * state changes until this is completed.
841ab66c 1707 */
32176cfd
RP
1708 return -1;
1709 } else if (vap->iv_state != vap->iv_nstate) {
1710#if 0
1711 /* Warn if the previous state hasn't completed. */
1712 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1713 "%s: pending %s -> %s transition lost\n", __func__,
1714 ieee80211_state_name[vap->iv_state],
1715 ieee80211_state_name[vap->iv_nstate]);
1716#else
1717 /* XXX temporarily enable to identify issues */
1718 if_printf(vap->iv_ifp,
1719 "%s: pending %s -> %s transition lost\n",
1720 __func__, ieee80211_state_name[vap->iv_state],
1721 ieee80211_state_name[vap->iv_nstate]);
1722#endif
f186073c 1723 }
32176cfd
RP
1724 }
1725
1726 nrunning = nscanning = 0;
1727 /* XXX can track this state instead of calculating */
1728 TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1729 if (vp != vap) {
1730 if (vp->iv_state >= IEEE80211_S_RUN)
1731 nrunning++;
1732 /* XXX doesn't handle bg scan */
1733 /* NB: CAC+AUTH+ASSOC treated like SCAN */
1734 else if (vp->iv_state > IEEE80211_S_INIT)
1735 nscanning++;
1736 }
1737 }
1738 ostate = vap->iv_state;
1739 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1740 "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1741 ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1742 nrunning, nscanning);
1743 switch (nstate) {
1744 case IEEE80211_S_SCAN:
1745 if (ostate == IEEE80211_S_INIT) {
1746 /*
1747 * INIT -> SCAN happens on initial bringup.
1748 */
1749 KASSERT(!(nscanning && nrunning),
1750 ("%d scanning and %d running", nscanning, nrunning));
1751 if (nscanning) {
1752 /*
1753 * Someone is scanning, defer our state
1754 * change until the work has completed.
1755 */
1756 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1757 "%s: defer %s -> %s\n",
1758 __func__, ieee80211_state_name[ostate],
1759 ieee80211_state_name[nstate]);
1760 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1761 return 0;
f186073c 1762 }
32176cfd
RP
1763 if (nrunning) {
1764 /*
1765 * Someone is operating; just join the channel
1766 * they have chosen.
1767 */
1768 /* XXX kill arg? */
1769 /* XXX check each opmode, adhoc? */
1770 if (vap->iv_opmode == IEEE80211_M_STA)
1771 nstate = IEEE80211_S_SCAN;
1772 else
1773 nstate = IEEE80211_S_RUN;
1774#ifdef IEEE80211_DEBUG
1775 if (nstate != IEEE80211_S_SCAN) {
1776 IEEE80211_DPRINTF(vap,
1777 IEEE80211_MSG_STATE,
1778 "%s: override, now %s -> %s\n",
1779 __func__,
1780 ieee80211_state_name[ostate],
1781 ieee80211_state_name[nstate]);
841ab66c 1782 }
32176cfd 1783#endif
841ab66c 1784 }
f186073c
JS
1785 }
1786 break;
1787 case IEEE80211_S_RUN:
32176cfd
RP
1788 if (vap->iv_opmode == IEEE80211_M_WDS &&
1789 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
1790 nscanning) {
1791 /*
1792 * Legacy WDS with someone else scanning; don't
1793 * go online until that completes as we should
1794 * follow the other vap to the channel they choose.
1795 */
1796 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1797 "%s: defer %s -> %s (legacy WDS)\n", __func__,
1798 ieee80211_state_name[ostate],
1799 ieee80211_state_name[nstate]);
1800 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1801 return 0;
f186073c 1802 }
32176cfd
RP
1803 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1804 IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
1805 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
1806 !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
841ab66c 1807 /*
32176cfd
RP
1808 * This is a DFS channel, transition to CAC state
1809 * instead of RUN. This allows us to initiate
1810 * Channel Availability Check (CAC) as specified
1811 * by 11h/DFS.
841ab66c 1812 */
32176cfd
RP
1813 nstate = IEEE80211_S_CAC;
1814 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1815 "%s: override %s -> %s (DFS)\n", __func__,
1816 ieee80211_state_name[ostate],
1817 ieee80211_state_name[nstate]);
841ab66c 1818 }
32176cfd
RP
1819 break;
1820 case IEEE80211_S_INIT:
1821 /* cancel any scan in progress */
1822 ieee80211_cancel_scan(vap);
1823 if (ostate == IEEE80211_S_INIT ) {
1824 /* XXX don't believe this */
1825 /* INIT -> INIT. nothing to do */
1826 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
841ab66c 1827 }
32176cfd
RP
1828 /* fall thru... */
1829 default:
f186073c
JS
1830 break;
1831 }
32176cfd
RP
1832 /* defer the state change to a thread */
1833 vap->iv_nstate = nstate;
1834 vap->iv_nstate_arg = arg;
1835 vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
1836 ieee80211_runtask(ic, &vap->iv_nstate_task);
1837 return EINPROGRESS;
1838}
1839
1840int
1841ieee80211_new_state(struct ieee80211vap *vap,
1842 enum ieee80211_state nstate, int arg)
1843{
1844 struct ieee80211com *ic = vap->iv_ic;
1845 int rc;
1846
e8361ca0 1847 ic = vap->iv_ic;
32176cfd 1848 rc = ieee80211_new_state_locked(vap, nstate, arg);
32176cfd 1849 return rc;
f186073c 1850}