2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
26 * $FreeBSD: head/sys/net80211/ieee80211_proto.c 195618 2009-07-11 15:02:45Z rpaulo $
31 * IEEE 802.11 protocol support.
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
45 #include <net/if_media.h>
46 #include <net/route.h>
48 #include <netproto/802_11/ieee80211_var.h>
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>
56 #include <netproto/802_11/ieee80211_monitor.h>
57 #include <netproto/802_11/ieee80211_input.h>
60 #define AGGRESSIVE_MODE_SWITCH_HYSTERESIS 3 /* pkts / 100ms */
61 #define HIGH_PRI_SWITCH_THRESH 10 /* pkts / 100ms */
63 const 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"
69 const 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"
75 const 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 */
84 const 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 */
89 "CAC", /* IEEE80211_S_CAC */
90 "RUN", /* IEEE80211_S_RUN */
91 "CSA", /* IEEE80211_S_CSA */
92 "SLEEP", /* IEEE80211_S_SLEEP */
94 const char *ieee80211_wme_acnames[] = {
102 static void beacon_miss_task(void *, int);
103 static void beacon_swmiss_task(void *, int);
104 static void parent_updown_task(void *, int);
105 static void update_mcast_task(void *, int);
106 static void update_promisc_task(void *, int);
107 static void update_channel_task(void *, int);
108 static void ieee80211_newstate_task(void *, int);
109 static int ieee80211_new_state_locked(struct ieee80211vap *,
110 enum ieee80211_state, int);
113 null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
114 const struct ieee80211_bpf_params *params)
116 struct ifnet *ifp = ni->ni_ic->ic_ifp;
118 if_printf(ifp, "missing ic_raw_xmit callback, drop frame\n");
124 ieee80211_proto_attach(struct ieee80211com *ic)
126 struct ifnet *ifp = ic->ic_ifp;
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;
140 ic->ic_protmode = IEEE80211_PROT_CTSONLY;
142 TASK_INIT(&ic->ic_parent_task, 0, parent_updown_task, ifp);
143 TASK_INIT(&ic->ic_mcast_task, 0, update_mcast_task, ic);
144 TASK_INIT(&ic->ic_promisc_task, 0, update_promisc_task, ic);
145 TASK_INIT(&ic->ic_chan_task, 0, update_channel_task, ic);
146 TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss_task, ic);
148 ic->ic_wme.wme_hipri_switch_hysteresis =
149 AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
151 /* initialize management frame handlers */
152 ic->ic_send_mgmt = ieee80211_send_mgmt;
153 ic->ic_raw_xmit = null_raw_xmit;
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);
162 ieee80211_monitor_attach(ic);
166 ieee80211_proto_detach(struct ieee80211com *ic)
168 ieee80211_monitor_detach(ic);
169 #ifdef IEEE80211_SUPPORT_MESH
170 ieee80211_mesh_detach(ic);
172 ieee80211_hostap_detach(ic);
173 ieee80211_wds_detach(ic);
174 ieee80211_adhoc_detach(ic);
175 ieee80211_sta_detach(ic);
179 null_update_beacon(struct ieee80211vap *vap, int item)
184 ieee80211_proto_vattach(struct ieee80211vap *vap)
186 struct ieee80211com *ic = vap->iv_ic;
187 struct ifnet *ifp = vap->iv_ifp;
190 /* override the 802.3 setting */
191 ifp->if_hdrlen = ic->ic_ifp->if_hdrlen;
193 vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
194 vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
195 vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
196 callout_init_mp(&vap->iv_swbmiss);
197 callout_init_mp(&vap->iv_mgtsend);
198 TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_task, vap);
199 TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss_task, vap);
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.
206 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
207 const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
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;
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;
219 vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
221 vap->iv_roaming = IEEE80211_ROAMING_AUTO;
223 vap->iv_update_beacon = null_update_beacon;
224 vap->iv_deliver_data = ieee80211_deliver_data;
226 /* attach support for operating mode */
227 ic->ic_vattach[vap->iv_opmode](vap);
231 ieee80211_proto_vdetach(struct ieee80211vap *vap)
233 #define FREEAPPIE(ie) do { \
235 kfree(ie, M_80211_NODE_IE); \
238 * Detach operating mode module.
240 if (vap->iv_opdetach != NULL)
241 vap->iv_opdetach(vap);
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.
247 if (vap->iv_auth->ia_detach != NULL)
248 vap->iv_auth->ia_detach(vap);
250 * Detach any ACL'ator.
252 if (vap->iv_acl != NULL)
253 vap->iv_acl->iac_detach(vap);
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);
265 * Simple-minded authenticator module support.
268 #define IEEE80211_AUTH_MAX (IEEE80211_AUTH_WPA+1)
269 /* XXX well-known names */
270 static 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 */
278 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
280 static const struct ieee80211_authenticator auth_internal = {
281 .ia_name = "wlan_internal",
284 .ia_node_join = NULL,
285 .ia_node_leave = NULL,
289 * Setup internal authenticators once; they are never unregistered.
292 ieee80211_auth_setup(void)
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);
298 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
300 const struct ieee80211_authenticator *
301 ieee80211_authenticator_get(int auth)
303 if (auth >= IEEE80211_AUTH_MAX)
305 if (authenticators[auth] == NULL)
306 ieee80211_load_module(auth_modnames[auth]);
307 return authenticators[auth];
311 ieee80211_authenticator_register(int type,
312 const struct ieee80211_authenticator *auth)
314 if (type >= IEEE80211_AUTH_MAX)
316 authenticators[type] = auth;
320 ieee80211_authenticator_unregister(int type)
323 if (type >= IEEE80211_AUTH_MAX)
325 authenticators[type] = NULL;
329 * Very simple-minded ACL module support.
331 /* XXX just one for now */
332 static const struct ieee80211_aclator *acl = NULL;
335 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
337 kprintf("wlan: %s acl policy registered\n", iac->iac_name);
342 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
346 kprintf("wlan: %s acl policy unregistered\n", iac->iac_name);
349 const struct ieee80211_aclator *
350 ieee80211_aclator_get(const char *name)
353 ieee80211_load_module("wlan_acl");
354 return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
358 ieee80211_print_essid(const uint8_t *essid, int len)
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)
372 for (i = 0, p = essid; i < len; i++, p++)
377 for (i = 0, p = essid; i < len; i++, p++)
383 ieee80211_dump_pkt(struct ieee80211com *ic,
384 const uint8_t *buf, int len, int rate, int rssi)
386 const struct ieee80211_frame *wh;
389 wh = (const struct ieee80211_frame *)buf;
390 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
391 case IEEE80211_FC1_DIR_NODS:
392 kprintf("NODS %6D", wh->i_addr2, ":");
393 kprintf("->%6D", wh->i_addr1, ":");
394 kprintf("(%6D)", wh->i_addr3, ":");
396 case IEEE80211_FC1_DIR_TODS:
397 kprintf("TODS %6D", wh->i_addr2, ":");
398 kprintf("->%6D", wh->i_addr3, ":");
399 kprintf("(%6D)", wh->i_addr1, ":");
401 case IEEE80211_FC1_DIR_FROMDS:
402 kprintf("FRDS %6D", wh->i_addr3, ":");
403 kprintf("->%6D", wh->i_addr1, ":");
404 kprintf("(%6D)", wh->i_addr2, ":");
406 case IEEE80211_FC1_DIR_DSTODS:
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, ":");
413 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
414 case IEEE80211_FC0_TYPE_DATA:
417 case IEEE80211_FC0_TYPE_MGT:
418 kprintf(" %s", ieee80211_mgt_subtype_name[
419 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
420 >> IEEE80211_FC0_SUBTYPE_SHIFT]);
423 kprintf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
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" : "");
432 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
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);
444 kprintf(" %dM", rate / 2);
446 kprintf(" +%d", rssi);
449 for (i = 0; i < len; i++) {
452 kprintf("%02x", buf[i]);
459 findrix(const struct ieee80211_rateset *rs, int r)
463 for (i = 0; i < rs->rs_nrates; i++)
464 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
470 ieee80211_fix_rate(struct ieee80211_node *ni,
471 struct ieee80211_rateset *nrs, int flags)
473 #define RV(v) ((v) & IEEE80211_RATE_VAL)
474 struct ieee80211vap *vap = ni->ni_vap;
475 struct ieee80211com *ic = ni->ni_ic;
476 int i, j, rix, error;
477 int okrate, badrate, fixedrate, ucastrate;
478 const struct ieee80211_rateset *srs;
482 okrate = badrate = 0;
483 ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
484 if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
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.
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;
504 /* NB: required to make MCS match below work */
505 ucastrate &= IEEE80211_RATE_VAL;
507 fixedrate = IEEE80211_FIXED_RATE_NONE;
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
516 if (flags & IEEE80211_F_DOBRS)
517 srs = (const struct ieee80211_rateset *)
518 ieee80211_get_suphtrates(ic, ni->ni_chan);
520 srs = ieee80211_get_suprates(ic, ni->ni_chan);
521 for (i = 0; i < nrs->rs_nrates; ) {
522 if (flags & IEEE80211_F_DOSORT) {
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;
534 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
537 * Check for fixed rate.
542 * Check against supported rates.
544 rix = findrix(srs, r);
545 if (flags & IEEE80211_F_DONEGO) {
548 * A rate in the node's rate set is not
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.
553 if ((flags & IEEE80211_F_JOIN) &&
554 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
556 } else if ((flags & IEEE80211_F_JOIN) == 0) {
558 * Overwrite with the supported rate
559 * value so any basic rate bit is set.
561 nrs->rs_rates[i] = srs->rs_rates[rix];
564 if ((flags & IEEE80211_F_DODEL) && rix < 0) {
566 * Delete unacceptable rates.
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;
575 okrate = nrs->rs_rates[i];
578 if (okrate == 0 || error != 0 ||
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__, flags, okrate, error,
584 fixedrate, ucastrate);
585 return badrate | IEEE80211_RATE_BASIC;
592 * Reset 11g-related state.
595 ieee80211_reset_erp(struct ieee80211com *ic)
597 ic->ic_flags &= ~IEEE80211_F_USEPROT;
598 ic->ic_nonerpsta = 0;
599 ic->ic_longslotsta = 0;
601 * Short slot time is enabled only when operating in 11g
602 * and not in an IBSS. We must also honor whether or not
603 * the driver is capable of doing it.
605 ieee80211_set_shortslottime(ic,
606 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
607 IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
608 (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
609 ic->ic_opmode == IEEE80211_M_HOSTAP &&
610 (ic->ic_caps & IEEE80211_C_SHSLOT)));
612 * Set short preamble and ERP barker-preamble flags.
614 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
615 (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
616 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
617 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
619 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
620 ic->ic_flags |= IEEE80211_F_USEBARKER;
625 * Set the short slot time state and notify the driver.
628 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
631 ic->ic_flags |= IEEE80211_F_SHSLOT;
633 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
635 if (ic->ic_updateslot != NULL)
636 ic->ic_updateslot(ic->ic_ifp);
640 * Check if the specified rate set supports ERP.
641 * NB: the rate set is assumed to be sorted.
644 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
646 static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
649 if (rs->rs_nrates < NELEM(rates))
651 for (i = 0; i < NELEM(rates); i++) {
652 for (j = 0; j < rs->rs_nrates; j++) {
653 int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
667 * Mark the basic rates for the rate table based on the
668 * operating mode. For real 11g we mark all the 11b rates
669 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
670 * 11b rates. There's also a pseudo 11a-mode used to mark only
671 * the basic OFDM rates.
674 setbasicrates(struct ieee80211_rateset *rs,
675 enum ieee80211_phymode mode, int add)
677 static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
678 [IEEE80211_MODE_11A] = { 3, { 12, 24, 48 } },
679 [IEEE80211_MODE_11B] = { 2, { 2, 4 } },
681 [IEEE80211_MODE_11G] = { 4, { 2, 4, 11, 22 } },
682 [IEEE80211_MODE_TURBO_A] = { 3, { 12, 24, 48 } },
683 [IEEE80211_MODE_TURBO_G] = { 4, { 2, 4, 11, 22 } },
684 [IEEE80211_MODE_STURBO_A] = { 3, { 12, 24, 48 } },
685 [IEEE80211_MODE_HALF] = { 3, { 6, 12, 24 } },
686 [IEEE80211_MODE_QUARTER] = { 3, { 3, 6, 12 } },
687 [IEEE80211_MODE_11NA] = { 3, { 12, 24, 48 } },
689 [IEEE80211_MODE_11NG] = { 4, { 2, 4, 11, 22 } },
693 for (i = 0; i < rs->rs_nrates; i++) {
695 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
696 for (j = 0; j < basic[mode].rs_nrates; j++)
697 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
698 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
705 * Set the basic rates in a rate set.
708 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
709 enum ieee80211_phymode mode)
711 setbasicrates(rs, mode, 0);
715 * Add basic rates to a rate set.
718 ieee80211_addbasicrates(struct ieee80211_rateset *rs,
719 enum ieee80211_phymode mode)
721 setbasicrates(rs, mode, 1);
725 * WME protocol support.
727 * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
728 * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
729 * Draft 2.0 Test Plan (Appendix D).
731 * Static/Dynamic Turbo mode settings come from Atheros.
733 typedef struct phyParamType {
741 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
742 [IEEE80211_MODE_AUTO] = { 3, 4, 6, 0, 0 },
743 [IEEE80211_MODE_11A] = { 3, 4, 6, 0, 0 },
744 [IEEE80211_MODE_11B] = { 3, 4, 6, 0, 0 },
745 [IEEE80211_MODE_11G] = { 3, 4, 6, 0, 0 },
746 [IEEE80211_MODE_FH] = { 3, 4, 6, 0, 0 },
747 [IEEE80211_MODE_TURBO_A]= { 2, 3, 5, 0, 0 },
748 [IEEE80211_MODE_TURBO_G]= { 2, 3, 5, 0, 0 },
749 [IEEE80211_MODE_STURBO_A]={ 2, 3, 5, 0, 0 },
750 [IEEE80211_MODE_HALF] = { 3, 4, 6, 0, 0 },
751 [IEEE80211_MODE_QUARTER]= { 3, 4, 6, 0, 0 },
752 [IEEE80211_MODE_11NA] = { 3, 4, 6, 0, 0 },
753 [IEEE80211_MODE_11NG] = { 3, 4, 6, 0, 0 },
755 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
756 [IEEE80211_MODE_AUTO] = { 7, 4, 10, 0, 0 },
757 [IEEE80211_MODE_11A] = { 7, 4, 10, 0, 0 },
758 [IEEE80211_MODE_11B] = { 7, 4, 10, 0, 0 },
759 [IEEE80211_MODE_11G] = { 7, 4, 10, 0, 0 },
760 [IEEE80211_MODE_FH] = { 7, 4, 10, 0, 0 },
761 [IEEE80211_MODE_TURBO_A]= { 7, 3, 10, 0, 0 },
762 [IEEE80211_MODE_TURBO_G]= { 7, 3, 10, 0, 0 },
763 [IEEE80211_MODE_STURBO_A]={ 7, 3, 10, 0, 0 },
764 [IEEE80211_MODE_HALF] = { 7, 4, 10, 0, 0 },
765 [IEEE80211_MODE_QUARTER]= { 7, 4, 10, 0, 0 },
766 [IEEE80211_MODE_11NA] = { 7, 4, 10, 0, 0 },
767 [IEEE80211_MODE_11NG] = { 7, 4, 10, 0, 0 },
769 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
770 [IEEE80211_MODE_AUTO] = { 1, 3, 4, 94, 0 },
771 [IEEE80211_MODE_11A] = { 1, 3, 4, 94, 0 },
772 [IEEE80211_MODE_11B] = { 1, 3, 4, 188, 0 },
773 [IEEE80211_MODE_11G] = { 1, 3, 4, 94, 0 },
774 [IEEE80211_MODE_FH] = { 1, 3, 4, 188, 0 },
775 [IEEE80211_MODE_TURBO_A]= { 1, 2, 3, 94, 0 },
776 [IEEE80211_MODE_TURBO_G]= { 1, 2, 3, 94, 0 },
777 [IEEE80211_MODE_STURBO_A]={ 1, 2, 3, 94, 0 },
778 [IEEE80211_MODE_HALF] = { 1, 3, 4, 94, 0 },
779 [IEEE80211_MODE_QUARTER]= { 1, 3, 4, 94, 0 },
780 [IEEE80211_MODE_11NA] = { 1, 3, 4, 94, 0 },
781 [IEEE80211_MODE_11NG] = { 1, 3, 4, 94, 0 },
783 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
784 [IEEE80211_MODE_AUTO] = { 1, 2, 3, 47, 0 },
785 [IEEE80211_MODE_11A] = { 1, 2, 3, 47, 0 },
786 [IEEE80211_MODE_11B] = { 1, 2, 3, 102, 0 },
787 [IEEE80211_MODE_11G] = { 1, 2, 3, 47, 0 },
788 [IEEE80211_MODE_FH] = { 1, 2, 3, 102, 0 },
789 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
790 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
791 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
792 [IEEE80211_MODE_HALF] = { 1, 2, 3, 47, 0 },
793 [IEEE80211_MODE_QUARTER]= { 1, 2, 3, 47, 0 },
794 [IEEE80211_MODE_11NA] = { 1, 2, 3, 47, 0 },
795 [IEEE80211_MODE_11NG] = { 1, 2, 3, 47, 0 },
798 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
799 [IEEE80211_MODE_AUTO] = { 3, 4, 10, 0, 0 },
800 [IEEE80211_MODE_11A] = { 3, 4, 10, 0, 0 },
801 [IEEE80211_MODE_11B] = { 3, 4, 10, 0, 0 },
802 [IEEE80211_MODE_11G] = { 3, 4, 10, 0, 0 },
803 [IEEE80211_MODE_FH] = { 3, 4, 10, 0, 0 },
804 [IEEE80211_MODE_TURBO_A]= { 2, 3, 10, 0, 0 },
805 [IEEE80211_MODE_TURBO_G]= { 2, 3, 10, 0, 0 },
806 [IEEE80211_MODE_STURBO_A]={ 2, 3, 10, 0, 0 },
807 [IEEE80211_MODE_HALF] = { 3, 4, 10, 0, 0 },
808 [IEEE80211_MODE_QUARTER]= { 3, 4, 10, 0, 0 },
809 [IEEE80211_MODE_11NA] = { 3, 4, 10, 0, 0 },
810 [IEEE80211_MODE_11NG] = { 3, 4, 10, 0, 0 },
812 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
813 [IEEE80211_MODE_AUTO] = { 2, 3, 4, 94, 0 },
814 [IEEE80211_MODE_11A] = { 2, 3, 4, 94, 0 },
815 [IEEE80211_MODE_11B] = { 2, 3, 4, 188, 0 },
816 [IEEE80211_MODE_11G] = { 2, 3, 4, 94, 0 },
817 [IEEE80211_MODE_FH] = { 2, 3, 4, 188, 0 },
818 [IEEE80211_MODE_TURBO_A]= { 2, 2, 3, 94, 0 },
819 [IEEE80211_MODE_TURBO_G]= { 2, 2, 3, 94, 0 },
820 [IEEE80211_MODE_STURBO_A]={ 2, 2, 3, 94, 0 },
821 [IEEE80211_MODE_HALF] = { 2, 3, 4, 94, 0 },
822 [IEEE80211_MODE_QUARTER]= { 2, 3, 4, 94, 0 },
823 [IEEE80211_MODE_11NA] = { 2, 3, 4, 94, 0 },
824 [IEEE80211_MODE_11NG] = { 2, 3, 4, 94, 0 },
826 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
827 [IEEE80211_MODE_AUTO] = { 2, 2, 3, 47, 0 },
828 [IEEE80211_MODE_11A] = { 2, 2, 3, 47, 0 },
829 [IEEE80211_MODE_11B] = { 2, 2, 3, 102, 0 },
830 [IEEE80211_MODE_11G] = { 2, 2, 3, 47, 0 },
831 [IEEE80211_MODE_FH] = { 2, 2, 3, 102, 0 },
832 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 },
833 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 },
834 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 },
835 [IEEE80211_MODE_HALF] = { 2, 2, 3, 47, 0 },
836 [IEEE80211_MODE_QUARTER]= { 2, 2, 3, 47, 0 },
837 [IEEE80211_MODE_11NA] = { 2, 2, 3, 47, 0 },
838 [IEEE80211_MODE_11NG] = { 2, 2, 3, 47, 0 },
842 _setifsparams(struct wmeParams *wmep, const paramType *phy)
844 wmep->wmep_aifsn = phy->aifsn;
845 wmep->wmep_logcwmin = phy->logcwmin;
846 wmep->wmep_logcwmax = phy->logcwmax;
847 wmep->wmep_txopLimit = phy->txopLimit;
851 setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
852 struct wmeParams *wmep, const paramType *phy)
854 wmep->wmep_acm = phy->acm;
855 _setifsparams(wmep, phy);
857 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
858 "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
859 ieee80211_wme_acnames[ac], type,
860 wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
861 wmep->wmep_logcwmax, wmep->wmep_txopLimit);
865 ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
867 struct ieee80211com *ic = vap->iv_ic;
868 struct ieee80211_wme_state *wme = &ic->ic_wme;
869 const paramType *pPhyParam, *pBssPhyParam;
870 struct wmeParams *wmep;
871 enum ieee80211_phymode mode;
874 if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
878 * Select mode; we can be called early in which case we
879 * always use auto mode. We know we'll be called when
880 * entering the RUN state with bsschan setup properly
881 * so state will eventually get set correctly
883 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
884 mode = ieee80211_chan2mode(ic->ic_bsschan);
886 mode = IEEE80211_MODE_AUTO;
887 for (i = 0; i < WME_NUM_AC; i++) {
890 pPhyParam = &phyParamForAC_BK[mode];
891 pBssPhyParam = &phyParamForAC_BK[mode];
894 pPhyParam = &phyParamForAC_VI[mode];
895 pBssPhyParam = &bssPhyParamForAC_VI[mode];
898 pPhyParam = &phyParamForAC_VO[mode];
899 pBssPhyParam = &bssPhyParamForAC_VO[mode];
903 pPhyParam = &phyParamForAC_BE[mode];
904 pBssPhyParam = &bssPhyParamForAC_BE[mode];
907 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
908 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
909 setwmeparams(vap, "chan", i, wmep, pPhyParam);
911 setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
913 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
914 setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
916 /* NB: check ic_bss to avoid NULL deref on initial attach */
917 if (vap->iv_bss != NULL) {
919 * Calculate agressive mode switching threshold based
920 * on beacon interval. This doesn't need locking since
921 * we're only called before entering the RUN state at
922 * which point we start sending beacon frames.
924 wme->wme_hipri_switch_thresh =
925 (HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
926 wme->wme_flags &= ~WME_F_AGGRMODE;
927 ieee80211_wme_updateparams(vap);
932 ieee80211_wme_initparams(struct ieee80211vap *vap)
934 struct ieee80211com *ic = vap->iv_ic;
937 ieee80211_wme_initparams_locked(vap);
941 * Update WME parameters for ourself and the BSS.
944 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
946 static const paramType aggrParam[IEEE80211_MODE_MAX] = {
947 [IEEE80211_MODE_AUTO] = { 2, 4, 10, 64, 0 },
948 [IEEE80211_MODE_11A] = { 2, 4, 10, 64, 0 },
949 [IEEE80211_MODE_11B] = { 2, 5, 10, 64, 0 },
950 [IEEE80211_MODE_11G] = { 2, 4, 10, 64, 0 },
951 [IEEE80211_MODE_FH] = { 2, 5, 10, 64, 0 },
952 [IEEE80211_MODE_TURBO_A] = { 1, 3, 10, 64, 0 },
953 [IEEE80211_MODE_TURBO_G] = { 1, 3, 10, 64, 0 },
954 [IEEE80211_MODE_STURBO_A] = { 1, 3, 10, 64, 0 },
955 [IEEE80211_MODE_HALF] = { 2, 4, 10, 64, 0 },
956 [IEEE80211_MODE_QUARTER] = { 2, 4, 10, 64, 0 },
957 [IEEE80211_MODE_11NA] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
958 [IEEE80211_MODE_11NG] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/
960 struct ieee80211com *ic = vap->iv_ic;
961 struct ieee80211_wme_state *wme = &ic->ic_wme;
962 const struct wmeParams *wmep;
963 struct wmeParams *chanp, *bssp;
964 enum ieee80211_phymode mode;
968 * Set up the channel access parameters for the physical
969 * device. First populate the configured settings.
971 for (i = 0; i < WME_NUM_AC; i++) {
972 chanp = &wme->wme_chanParams.cap_wmeParams[i];
973 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
974 chanp->wmep_aifsn = wmep->wmep_aifsn;
975 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
976 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
977 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
979 chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
980 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
981 chanp->wmep_aifsn = wmep->wmep_aifsn;
982 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
983 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
984 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
988 * Select mode; we can be called early in which case we
989 * always use auto mode. We know we'll be called when
990 * entering the RUN state with bsschan setup properly
991 * so state will eventually get set correctly
993 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
994 mode = ieee80211_chan2mode(ic->ic_bsschan);
996 mode = IEEE80211_MODE_AUTO;
999 * This implements agressive mode as found in certain
1000 * vendors' AP's. When there is significant high
1001 * priority (VI/VO) traffic in the BSS throttle back BE
1002 * traffic by using conservative parameters. Otherwise
1003 * BE uses agressive params to optimize performance of
1004 * legacy/non-QoS traffic.
1006 if ((vap->iv_opmode == IEEE80211_M_HOSTAP &&
1007 (wme->wme_flags & WME_F_AGGRMODE) != 0) ||
1008 (vap->iv_opmode == IEEE80211_M_STA &&
1009 (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
1010 (vap->iv_flags & IEEE80211_F_WME) == 0) {
1011 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1012 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1014 chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1015 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1016 aggrParam[mode].logcwmin;
1017 chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1018 aggrParam[mode].logcwmax;
1019 chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1020 (vap->iv_flags & IEEE80211_F_BURST) ?
1021 aggrParam[mode].txopLimit : 0;
1022 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1023 "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1024 "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1025 chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1026 chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1029 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1030 ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1031 static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1032 [IEEE80211_MODE_AUTO] = 3,
1033 [IEEE80211_MODE_11A] = 3,
1034 [IEEE80211_MODE_11B] = 4,
1035 [IEEE80211_MODE_11G] = 3,
1036 [IEEE80211_MODE_FH] = 4,
1037 [IEEE80211_MODE_TURBO_A] = 3,
1038 [IEEE80211_MODE_TURBO_G] = 3,
1039 [IEEE80211_MODE_STURBO_A] = 3,
1040 [IEEE80211_MODE_HALF] = 3,
1041 [IEEE80211_MODE_QUARTER] = 3,
1042 [IEEE80211_MODE_11NA] = 3,
1043 [IEEE80211_MODE_11NG] = 3,
1045 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1046 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1048 chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1049 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1050 "update %s (chan+bss) logcwmin %u\n",
1051 ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1053 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { /* XXX ibss? */
1055 * Arrange for a beacon update and bump the parameter
1056 * set number so associated stations load the new values.
1058 wme->wme_bssChanParams.cap_info =
1059 (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1060 ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1063 wme->wme_update(ic);
1065 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1066 "%s: WME params updated, cap_info 0x%x\n", __func__,
1067 vap->iv_opmode == IEEE80211_M_STA ?
1068 wme->wme_wmeChanParams.cap_info :
1069 wme->wme_bssChanParams.cap_info);
1073 ieee80211_wme_updateparams(struct ieee80211vap *vap)
1075 struct ieee80211com *ic = vap->iv_ic;
1077 if (ic->ic_caps & IEEE80211_C_WME) {
1078 ieee80211_wme_updateparams_locked(vap);
1083 parent_updown_task(void *arg, int npending)
1085 struct ifnet *parent = arg;
1087 wlan_serialize_enter();
1088 parent->if_ioctl(parent, SIOCSIFFLAGS, NULL, curthread->td_ucred);
1089 wlan_serialize_exit();
1093 update_mcast_task(void *arg, int npending)
1095 struct ieee80211com *ic = arg;
1096 struct ifnet *parent = ic->ic_ifp;
1098 wlan_serialize_enter();
1099 ic->ic_update_mcast(parent);
1100 wlan_serialize_exit();
1104 update_promisc_task(void *arg, int npending)
1106 struct ieee80211com *ic = arg;
1107 struct ifnet *parent = ic->ic_ifp;
1109 wlan_serialize_enter();
1110 ic->ic_update_promisc(parent);
1111 wlan_serialize_exit();
1115 update_channel_task(void *arg, int npending)
1117 struct ieee80211com *ic = arg;
1119 wlan_serialize_enter();
1120 ic->ic_set_channel(ic);
1121 ieee80211_radiotap_chan_change(ic);
1122 wlan_serialize_exit();
1126 * Block until the parent is in a known state. This is
1127 * used after any operations that dispatch a task (e.g.
1128 * to auto-configure the parent device up/down).
1131 ieee80211_waitfor_parent(struct ieee80211com *ic)
1133 wlan_assert_serialized();
1134 wlan_serialize_exit(); /* exit to block */
1135 taskqueue_block(ic->ic_tq);
1136 ieee80211_draintask(ic, &ic->ic_parent_task);
1137 ieee80211_draintask(ic, &ic->ic_mcast_task);
1138 ieee80211_draintask(ic, &ic->ic_promisc_task);
1139 ieee80211_draintask(ic, &ic->ic_chan_task);
1140 ieee80211_draintask(ic, &ic->ic_bmiss_task);
1141 taskqueue_unblock(ic->ic_tq);
1142 wlan_serialize_enter(); /* then re-enter */
1146 * Start a vap running. If this is the first vap to be
1147 * set running on the underlying device then we
1148 * automatically bring the device up.
1151 ieee80211_start_locked(struct ieee80211vap *vap)
1153 struct ifnet *ifp = vap->iv_ifp;
1154 struct ieee80211com *ic = vap->iv_ic;
1155 struct ifnet *parent = ic->ic_ifp;
1157 IEEE80211_DPRINTF(vap,
1158 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1159 "start running, %d vaps running\n", ic->ic_nrunning);
1161 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1163 * Mark us running. Note that it's ok to do this first;
1164 * if we need to bring the parent device up we defer that
1165 * to avoid dropping the com lock. We expect the device
1166 * to respond to being marked up by calling back into us
1167 * through ieee80211_start_all at which point we'll come
1168 * back in here and complete the work.
1170 ifp->if_flags |= IFF_RUNNING;
1172 * We are not running; if this we are the first vap
1173 * to be brought up auto-up the parent if necessary.
1175 if (ic->ic_nrunning++ == 0 &&
1176 (parent->if_flags & IFF_RUNNING) == 0) {
1177 IEEE80211_DPRINTF(vap,
1178 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1179 "%s: up parent %s\n", __func__, parent->if_xname);
1180 parent->if_flags |= IFF_UP;
1181 ieee80211_runtask(ic, &ic->ic_parent_task);
1186 * If the parent is up and running, then kick the
1187 * 802.11 state machine as appropriate.
1189 if ((parent->if_flags & IFF_RUNNING) &&
1190 vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1191 if (vap->iv_opmode == IEEE80211_M_STA) {
1193 /* XXX bypasses scan too easily; disable for now */
1195 * Try to be intelligent about clocking the state
1196 * machine. If we're currently in RUN state then
1197 * we should be able to apply any new state/parameters
1198 * simply by re-associating. Otherwise we need to
1199 * re-scan to select an appropriate ap.
1201 if (vap->iv_state >= IEEE80211_S_RUN)
1202 ieee80211_new_state_locked(vap,
1203 IEEE80211_S_ASSOC, 1);
1206 ieee80211_new_state_locked(vap,
1207 IEEE80211_S_SCAN, 0);
1210 * For monitor+wds mode there's nothing to do but
1211 * start running. Otherwise if this is the first
1212 * vap to be brought up, start a scan which may be
1213 * preempted if the station is locked to a particular
1216 vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1217 if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1218 vap->iv_opmode == IEEE80211_M_WDS)
1219 ieee80211_new_state_locked(vap,
1220 IEEE80211_S_RUN, -1);
1222 ieee80211_new_state_locked(vap,
1223 IEEE80211_S_SCAN, 0);
1229 * Start a single vap.
1232 ieee80211_init(void *arg)
1234 struct ieee80211vap *vap = arg;
1236 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1239 ieee80211_start_locked(vap);
1243 * Start all runnable vap's on a device.
1246 ieee80211_start_all(struct ieee80211com *ic)
1248 struct ieee80211vap *vap;
1250 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1251 struct ifnet *ifp = vap->iv_ifp;
1252 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1253 ieee80211_start_locked(vap);
1258 * Stop a vap. We force it down using the state machine
1259 * then mark it's ifnet not running. If this is the last
1260 * vap running on the underlying device then we close it
1261 * too to insure it will be properly initialized when the
1262 * next vap is brought up.
1265 ieee80211_stop_locked(struct ieee80211vap *vap)
1267 struct ieee80211com *ic = vap->iv_ic;
1268 struct ifnet *ifp = vap->iv_ifp;
1269 struct ifnet *parent = ic->ic_ifp;
1271 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1272 "stop running, %d vaps running\n", ic->ic_nrunning);
1274 ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1275 if (ifp->if_flags & IFF_RUNNING) {
1276 ifp->if_flags &= ~IFF_RUNNING; /* mark us stopped */
1277 if (--ic->ic_nrunning == 0 &&
1278 (parent->if_flags & IFF_RUNNING)) {
1279 IEEE80211_DPRINTF(vap,
1280 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1281 "down parent %s\n", parent->if_xname);
1282 parent->if_flags &= ~IFF_UP;
1283 ieee80211_runtask(ic, &ic->ic_parent_task);
1289 ieee80211_stop(struct ieee80211vap *vap)
1291 struct ieee80211com *ic = vap->iv_ic;
1294 ieee80211_stop_locked(vap);
1298 * Stop all vap's running on a device.
1301 ieee80211_stop_all(struct ieee80211com *ic)
1303 struct ieee80211vap *vap;
1305 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1306 struct ifnet *ifp = vap->iv_ifp;
1307 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1308 ieee80211_stop_locked(vap);
1311 ieee80211_waitfor_parent(ic);
1315 * Stop all vap's running on a device and arrange
1316 * for those that were running to be resumed.
1319 ieee80211_suspend_all(struct ieee80211com *ic)
1321 struct ieee80211vap *vap;
1323 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1324 struct ifnet *ifp = vap->iv_ifp;
1325 if (IFNET_IS_UP_RUNNING(ifp)) { /* NB: avoid recursion */
1326 vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1327 ieee80211_stop_locked(vap);
1331 ieee80211_waitfor_parent(ic);
1335 * Start all vap's marked for resume.
1338 ieee80211_resume_all(struct ieee80211com *ic)
1340 struct ieee80211vap *vap;
1342 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1343 struct ifnet *ifp = vap->iv_ifp;
1344 if (!IFNET_IS_UP_RUNNING(ifp) &&
1345 (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1346 vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1347 ieee80211_start_locked(vap);
1353 ieee80211_beacon_miss(struct ieee80211com *ic)
1355 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1356 /* Process in a taskq, the handler may reenter the driver */
1357 ieee80211_runtask(ic, &ic->ic_bmiss_task);
1362 beacon_miss_task(void *arg, int npending)
1364 struct ieee80211com *ic = arg;
1365 struct ieee80211vap *vap;
1367 wlan_serialize_enter();
1368 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1370 * We only pass events through for sta vap's in RUN state;
1371 * may be too restrictive but for now this saves all the
1372 * handlers duplicating these checks.
1374 if (vap->iv_opmode == IEEE80211_M_STA &&
1375 vap->iv_state >= IEEE80211_S_RUN &&
1376 vap->iv_bmiss != NULL)
1379 wlan_serialize_exit();
1383 beacon_swmiss_task(void *arg, int npending)
1385 struct ieee80211vap *vap = arg;
1387 wlan_serialize_enter();
1388 if (vap->iv_state == IEEE80211_S_RUN) {
1389 /* XXX Call multiple times if npending > zero? */
1392 wlan_serialize_exit();
1396 * Software beacon miss handling. Check if any beacons
1397 * were received in the last period. If not post a
1398 * beacon miss; otherwise reset the counter.
1401 ieee80211_swbmiss_callout(void *arg)
1403 struct ieee80211vap *vap = arg;
1404 struct ieee80211com *ic = vap->iv_ic;
1406 wlan_serialize_enter();
1407 KASSERT(vap->iv_state == IEEE80211_S_RUN,
1408 ("wrong state %d", vap->iv_state));
1410 if (ic->ic_flags & IEEE80211_F_SCAN) {
1412 * If scanning just ignore and reset state. If we get a
1413 * bmiss after coming out of scan because we haven't had
1414 * time to receive a beacon then we should probe the AP
1415 * before posting a real bmiss (unless iv_bmiss_max has
1416 * been artifiically lowered). A cleaner solution might
1417 * be to disable the timer on scan start/end but to handle
1418 * case of multiple sta vap's we'd need to disable the
1419 * timers of all affected vap's.
1421 vap->iv_swbmiss_count = 0;
1422 } else if (vap->iv_swbmiss_count == 0) {
1423 if (vap->iv_bmiss != NULL)
1424 ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1425 if (vap->iv_bmiss_count == 0) /* don't re-arm timer */
1428 vap->iv_swbmiss_count = 0;
1430 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1431 ieee80211_swbmiss_callout, vap);
1433 wlan_serialize_exit();
1437 * Start an 802.11h channel switch. We record the parameters,
1438 * mark the operation pending, notify each vap through the
1439 * beacon update mechanism so it can update the beacon frame
1440 * contents, and then switch vap's to CSA state to block outbound
1441 * traffic. Devices that handle CSA directly can use the state
1442 * switch to do the right thing so long as they call
1443 * ieee80211_csa_completeswitch when it's time to complete the
1444 * channel change. Devices that depend on the net80211 layer can
1445 * use ieee80211_beacon_update to handle the countdown and the
1449 ieee80211_csa_startswitch(struct ieee80211com *ic,
1450 struct ieee80211_channel *c, int mode, int count)
1452 struct ieee80211vap *vap;
1454 ic->ic_csa_newchan = c;
1455 ic->ic_csa_mode = mode;
1456 ic->ic_csa_count = count;
1457 ic->ic_flags |= IEEE80211_F_CSAPENDING;
1458 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1459 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1460 vap->iv_opmode == IEEE80211_M_IBSS ||
1461 vap->iv_opmode == IEEE80211_M_MBSS)
1462 ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1463 /* switch to CSA state to block outbound traffic */
1464 if (vap->iv_state == IEEE80211_S_RUN)
1465 ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1467 ieee80211_notify_csa(ic, c, mode, count);
1471 csa_completeswitch(struct ieee80211com *ic)
1473 struct ieee80211vap *vap;
1475 ic->ic_csa_newchan = NULL;
1476 ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1478 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1479 if (vap->iv_state == IEEE80211_S_CSA)
1480 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1484 * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1485 * We clear state and move all vap's in CSA state to RUN state
1486 * so they can again transmit.
1489 ieee80211_csa_completeswitch(struct ieee80211com *ic)
1491 KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1493 ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1494 csa_completeswitch(ic);
1498 * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1499 * We clear state and move all vap's in CSA state to RUN state
1500 * so they can again transmit.
1503 ieee80211_csa_cancelswitch(struct ieee80211com *ic)
1505 csa_completeswitch(ic);
1509 * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1510 * We clear state and move all vap's in CAC state to RUN state.
1513 ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1515 struct ieee80211com *ic = vap0->iv_ic;
1516 struct ieee80211vap *vap;
1519 * Complete CAC state change for lead vap first; then
1520 * clock all the other vap's waiting.
1522 KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1523 ("wrong state %d", vap0->iv_state));
1524 ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1526 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1527 if (vap->iv_state == IEEE80211_S_CAC)
1528 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1532 * Force all vap's other than the specified vap to the INIT state
1533 * and mark them as waiting for a scan to complete. These vaps
1534 * will be brought up when the scan completes and the scanning vap
1535 * reaches RUN state by wakeupwaiting.
1538 markwaiting(struct ieee80211vap *vap0)
1540 struct ieee80211com *ic = vap0->iv_ic;
1541 struct ieee80211vap *vap;
1544 * A vap list entry can not disappear since we are running on the
1545 * taskqueue and a vap destroy will queue and drain another state
1548 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1551 if (vap->iv_state != IEEE80211_S_INIT) {
1552 /* NB: iv_newstate may drop the lock */
1553 vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1554 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1560 * Wakeup all vap's waiting for a scan to complete. This is the
1561 * companion to markwaiting (above) and is used to coordinate
1562 * multiple vaps scanning.
1563 * This is called from the state taskqueue.
1566 wakeupwaiting(struct ieee80211vap *vap0)
1568 struct ieee80211com *ic = vap0->iv_ic;
1569 struct ieee80211vap *vap;
1572 * A vap list entry can not disappear since we are running on the
1573 * taskqueue and a vap destroy will queue and drain another state
1576 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1579 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1580 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1581 /* NB: sta's cannot go INIT->RUN */
1582 /* NB: iv_newstate may drop the lock */
1583 vap->iv_newstate(vap,
1584 vap->iv_opmode == IEEE80211_M_STA ?
1585 IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1591 * Handle post state change work common to all operating modes.
1594 ieee80211_newstate_task(void *xvap, int npending)
1596 struct ieee80211vap *vap = xvap;
1597 struct ieee80211com *ic;
1598 enum ieee80211_state nstate, ostate;
1601 wlan_serialize_enter();
1604 nstate = vap->iv_nstate;
1605 arg = vap->iv_nstate_arg;
1607 if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
1609 * We have been requested to drop back to the INIT before
1610 * proceeding to the new state.
1612 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1613 "%s: %s -> %s arg %d\n", __func__,
1614 ieee80211_state_name[vap->iv_state],
1615 ieee80211_state_name[IEEE80211_S_INIT], arg);
1616 vap->iv_newstate(vap, IEEE80211_S_INIT, arg);
1617 vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT;
1620 ostate = vap->iv_state;
1621 if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
1623 * SCAN was forced; e.g. on beacon miss. Force other running
1624 * vap's to INIT state and mark them as waiting for the scan to
1625 * complete. This insures they don't interfere with our
1626 * scanning. Since we are single threaded the vaps can not
1627 * transition again while we are executing.
1629 * XXX not always right, assumes ap follows sta
1633 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1634 "%s: %s -> %s arg %d\n", __func__,
1635 ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
1637 rc = vap->iv_newstate(vap, nstate, arg);
1638 vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
1640 /* State transition failed */
1641 KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
1642 KASSERT(nstate != IEEE80211_S_INIT,
1643 ("INIT state change failed"));
1644 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1645 "%s: %s returned error %d\n", __func__,
1646 ieee80211_state_name[nstate], rc);
1650 /* No actual transition, skip post processing */
1651 if (ostate == nstate)
1654 if (nstate == IEEE80211_S_RUN) {
1656 * OACTIVE may be set on the vap if the upper layer
1657 * tried to transmit (e.g. IPv6 NDP) before we reach
1658 * RUN state. Clear it and restart xmit.
1660 * Note this can also happen as a result of SLEEP->RUN
1661 * (i.e. coming out of power save mode).
1663 vap->iv_ifp->if_flags &= ~IFF_OACTIVE;
1664 vap->iv_ifp->if_start(vap->iv_ifp);
1666 /* bring up any vaps waiting on us */
1668 } else if (nstate == IEEE80211_S_INIT) {
1670 * Flush the scan cache if we did the last scan (XXX?)
1671 * and flush any frames on send queues from this vap.
1672 * Note the mgt q is used only for legacy drivers and
1673 * will go away shortly.
1675 ieee80211_scan_flush(vap);
1677 /* XXX NB: cast for altq */
1678 ieee80211_flush_ifq((struct ifqueue *)&ic->ic_ifp->if_snd, vap);
1681 wlan_serialize_exit();
1685 * Public interface for initiating a state machine change.
1686 * This routine single-threads the request and coordinates
1687 * the scheduling of multiple vaps for the purpose of selecting
1688 * an operating channel. Specifically the following scenarios
1690 * o only one vap can be selecting a channel so on transition to
1691 * SCAN state if another vap is already scanning then
1692 * mark the caller for later processing and return without
1693 * doing anything (XXX? expectations by caller of synchronous operation)
1694 * o only one vap can be doing CAC of a channel so on transition to
1695 * CAC state if another vap is already scanning for radar then
1696 * mark the caller for later processing and return without
1697 * doing anything (XXX? expectations by caller of synchronous operation)
1698 * o if another vap is already running when a request is made
1699 * to SCAN then an operating channel has been chosen; bypass
1700 * the scan and just join the channel
1702 * Note that the state change call is done through the iv_newstate
1703 * method pointer so any driver routine gets invoked. The driver
1704 * will normally call back into operating mode-specific
1705 * ieee80211_newstate routines (below) unless it needs to completely
1706 * bypass the state machine (e.g. because the firmware has it's
1707 * own idea how things should work). Bypassing the net80211 layer
1708 * is usually a mistake and indicates lack of proper integration
1709 * with the net80211 layer.
1712 ieee80211_new_state_locked(struct ieee80211vap *vap,
1713 enum ieee80211_state nstate, int arg)
1715 struct ieee80211com *ic = vap->iv_ic;
1716 struct ieee80211vap *vp;
1717 enum ieee80211_state ostate;
1718 int nrunning, nscanning;
1720 if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
1721 if (vap->iv_nstate == IEEE80211_S_INIT) {
1723 * XXX The vap is being stopped, do no allow any other
1724 * state changes until this is completed.
1727 } else if (vap->iv_state != vap->iv_nstate) {
1729 /* Warn if the previous state hasn't completed. */
1730 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1731 "%s: pending %s -> %s transition lost\n", __func__,
1732 ieee80211_state_name[vap->iv_state],
1733 ieee80211_state_name[vap->iv_nstate]);
1735 /* XXX temporarily enable to identify issues */
1736 if_printf(vap->iv_ifp,
1737 "%s: pending %s -> %s transition lost\n",
1738 __func__, ieee80211_state_name[vap->iv_state],
1739 ieee80211_state_name[vap->iv_nstate]);
1744 nrunning = nscanning = 0;
1745 /* XXX can track this state instead of calculating */
1746 TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
1748 if (vp->iv_state >= IEEE80211_S_RUN)
1750 /* XXX doesn't handle bg scan */
1751 /* NB: CAC+AUTH+ASSOC treated like SCAN */
1752 else if (vp->iv_state > IEEE80211_S_INIT)
1756 ostate = vap->iv_state;
1757 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1758 "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
1759 ieee80211_state_name[ostate], ieee80211_state_name[nstate],
1760 nrunning, nscanning);
1762 case IEEE80211_S_SCAN:
1763 if (ostate == IEEE80211_S_INIT) {
1765 * INIT -> SCAN happens on initial bringup.
1767 KASSERT(!(nscanning && nrunning),
1768 ("%d scanning and %d running", nscanning, nrunning));
1771 * Someone is scanning, defer our state
1772 * change until the work has completed.
1774 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1775 "%s: defer %s -> %s\n",
1776 __func__, ieee80211_state_name[ostate],
1777 ieee80211_state_name[nstate]);
1778 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1783 * Someone is operating; just join the channel
1787 /* XXX check each opmode, adhoc? */
1788 if (vap->iv_opmode == IEEE80211_M_STA)
1789 nstate = IEEE80211_S_SCAN;
1791 nstate = IEEE80211_S_RUN;
1792 #ifdef IEEE80211_DEBUG
1793 if (nstate != IEEE80211_S_SCAN) {
1794 IEEE80211_DPRINTF(vap,
1795 IEEE80211_MSG_STATE,
1796 "%s: override, now %s -> %s\n",
1798 ieee80211_state_name[ostate],
1799 ieee80211_state_name[nstate]);
1805 case IEEE80211_S_RUN:
1806 if (vap->iv_opmode == IEEE80211_M_WDS &&
1807 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
1810 * Legacy WDS with someone else scanning; don't
1811 * go online until that completes as we should
1812 * follow the other vap to the channel they choose.
1814 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1815 "%s: defer %s -> %s (legacy WDS)\n", __func__,
1816 ieee80211_state_name[ostate],
1817 ieee80211_state_name[nstate]);
1818 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1821 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1822 IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
1823 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
1824 !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
1826 * This is a DFS channel, transition to CAC state
1827 * instead of RUN. This allows us to initiate
1828 * Channel Availability Check (CAC) as specified
1831 nstate = IEEE80211_S_CAC;
1832 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1833 "%s: override %s -> %s (DFS)\n", __func__,
1834 ieee80211_state_name[ostate],
1835 ieee80211_state_name[nstate]);
1838 case IEEE80211_S_INIT:
1839 /* cancel any scan in progress */
1840 ieee80211_cancel_scan(vap);
1841 if (ostate == IEEE80211_S_INIT ) {
1842 /* XXX don't believe this */
1843 /* INIT -> INIT. nothing to do */
1844 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1850 /* defer the state change to a thread */
1851 vap->iv_nstate = nstate;
1852 vap->iv_nstate_arg = arg;
1853 vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
1854 ieee80211_runtask(ic, &vap->iv_nstate_task);
1859 ieee80211_new_state(struct ieee80211vap *vap,
1860 enum ieee80211_state nstate, int arg)
1862 struct ieee80211com *ic = vap->iv_ic;
1866 rc = ieee80211_new_state_locked(vap, nstate, arg);