2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2005 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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * IEEE 802.11 protocol support.
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
46 #include <sys/socket.h>
49 #include <net/if_media.h>
50 #include <net/ethernet.h> /* XXX for ether_sprintf */
52 #include <net80211/ieee80211_var.h>
55 #define AGGRESSIVE_MODE_SWITCH_HYSTERESIS 3 /* pkts / 100ms */
56 #define HIGH_PRI_SWITCH_THRESH 10 /* pkts / 100ms */
58 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
60 const char *ieee80211_mgt_subtype_name[] = {
61 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp",
62 "probe_req", "probe_resp", "reserved#6", "reserved#7",
63 "beacon", "atim", "disassoc", "auth",
64 "deauth", "reserved#13", "reserved#14", "reserved#15"
66 const char *ieee80211_ctl_subtype_name[] = {
67 "reserved#0", "reserved#1", "reserved#2", "reserved#3",
68 "reserved#3", "reserved#5", "reserved#6", "reserved#7",
69 "reserved#8", "reserved#9", "ps_poll", "rts",
70 "cts", "ack", "cf_end", "cf_end_ack"
72 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
73 "INIT", /* IEEE80211_S_INIT */
74 "SCAN", /* IEEE80211_S_SCAN */
75 "AUTH", /* IEEE80211_S_AUTH */
76 "ASSOC", /* IEEE80211_S_ASSOC */
77 "RUN" /* IEEE80211_S_RUN */
79 const char *ieee80211_wme_acnames[] = {
87 static int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int);
90 ieee80211_proto_attach(struct ieee80211com *ic)
92 struct ifnet *ifp = ic->ic_ifp;
94 /* XXX room for crypto */
95 ifp->if_hdrlen = sizeof(struct ieee80211_qosframe_addr4);
97 ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT;
98 ic->ic_fragthreshold = IEEE80211_FRAG_DEFAULT;
99 ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE;
100 ic->ic_bmiss_max = IEEE80211_BMISS_MAX;
101 callout_init(&ic->ic_swbmiss, CALLOUT_MPSAFE);
102 ic->ic_mcast_rate = IEEE80211_MCAST_RATE_DEFAULT;
103 ic->ic_protmode = IEEE80211_PROT_CTSONLY;
104 ic->ic_roaming = IEEE80211_ROAMING_AUTO;
106 ic->ic_wme.wme_hipri_switch_hysteresis =
107 AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
109 mtx_init(&ic->ic_mgtq.ifq_mtx, ifp->if_xname, "mgmt send q", MTX_DEF);
111 /* protocol state change handler */
112 ic->ic_newstate = ieee80211_newstate;
114 /* initialize management frame handlers */
115 ic->ic_recv_mgmt = ieee80211_recv_mgmt;
116 ic->ic_send_mgmt = ieee80211_send_mgmt;
120 ieee80211_proto_detach(struct ieee80211com *ic)
124 * This should not be needed as we detach when reseting
125 * the state but be conservative here since the
126 * authenticator may do things like spawn kernel threads.
128 if (ic->ic_auth->ia_detach)
129 ic->ic_auth->ia_detach(ic);
131 IF_DRAIN(&ic->ic_mgtq);
132 mtx_destroy(&ic->ic_mgtq.ifq_mtx);
135 * Detach any ACL'ator.
137 if (ic->ic_acl != NULL)
138 ic->ic_acl->iac_detach(ic);
142 * Simple-minded authenticator module support.
145 #define IEEE80211_AUTH_MAX (IEEE80211_AUTH_WPA+1)
146 /* XXX well-known names */
147 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
148 "wlan_internal", /* IEEE80211_AUTH_NONE */
149 "wlan_internal", /* IEEE80211_AUTH_OPEN */
150 "wlan_internal", /* IEEE80211_AUTH_SHARED */
151 "wlan_xauth", /* IEEE80211_AUTH_8021X */
152 "wlan_internal", /* IEEE80211_AUTH_AUTO */
153 "wlan_xauth", /* IEEE80211_AUTH_WPA */
155 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
157 static const struct ieee80211_authenticator auth_internal = {
158 .ia_name = "wlan_internal",
161 .ia_node_join = NULL,
162 .ia_node_leave = NULL,
166 * Setup internal authenticators once; they are never unregistered.
169 ieee80211_auth_setup(void)
171 ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
172 ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
173 ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
175 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
177 const struct ieee80211_authenticator *
178 ieee80211_authenticator_get(int auth)
180 if (auth >= IEEE80211_AUTH_MAX)
182 if (authenticators[auth] == NULL)
183 ieee80211_load_module(auth_modnames[auth]);
184 return authenticators[auth];
188 ieee80211_authenticator_register(int type,
189 const struct ieee80211_authenticator *auth)
191 if (type >= IEEE80211_AUTH_MAX)
193 authenticators[type] = auth;
197 ieee80211_authenticator_unregister(int type)
200 if (type >= IEEE80211_AUTH_MAX)
202 authenticators[type] = NULL;
206 * Very simple-minded ACL module support.
208 /* XXX just one for now */
209 static const struct ieee80211_aclator *acl = NULL;
212 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
214 printf("wlan: %s acl policy registered\n", iac->iac_name);
219 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
223 printf("wlan: %s acl policy unregistered\n", iac->iac_name);
226 const struct ieee80211_aclator *
227 ieee80211_aclator_get(const char *name)
230 ieee80211_load_module("wlan_acl");
231 return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
235 ieee80211_print_essid(const u_int8_t *essid, int len)
240 if (len > IEEE80211_NWID_LEN)
241 len = IEEE80211_NWID_LEN;
242 /* determine printable or not */
243 for (i = 0, p = essid; i < len; i++, p++) {
244 if (*p < ' ' || *p > 0x7e)
249 for (i = 0, p = essid; i < len; i++, p++)
254 for (i = 0, p = essid; i < len; i++, p++)
260 ieee80211_dump_pkt(const u_int8_t *buf, int len, int rate, int rssi)
262 const struct ieee80211_frame *wh;
265 wh = (const struct ieee80211_frame *)buf;
266 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
267 case IEEE80211_FC1_DIR_NODS:
268 printf("NODS %s", ether_sprintf(wh->i_addr2));
269 printf("->%s", ether_sprintf(wh->i_addr1));
270 printf("(%s)", ether_sprintf(wh->i_addr3));
272 case IEEE80211_FC1_DIR_TODS:
273 printf("TODS %s", ether_sprintf(wh->i_addr2));
274 printf("->%s", ether_sprintf(wh->i_addr3));
275 printf("(%s)", ether_sprintf(wh->i_addr1));
277 case IEEE80211_FC1_DIR_FROMDS:
278 printf("FRDS %s", ether_sprintf(wh->i_addr3));
279 printf("->%s", ether_sprintf(wh->i_addr1));
280 printf("(%s)", ether_sprintf(wh->i_addr2));
282 case IEEE80211_FC1_DIR_DSTODS:
283 printf("DSDS %s", ether_sprintf((const u_int8_t *)&wh[1]));
284 printf("->%s", ether_sprintf(wh->i_addr3));
285 printf("(%s", ether_sprintf(wh->i_addr2));
286 printf("->%s)", ether_sprintf(wh->i_addr1));
289 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
290 case IEEE80211_FC0_TYPE_DATA:
293 case IEEE80211_FC0_TYPE_MGT:
294 printf(" %s", ieee80211_mgt_subtype_name[
295 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
296 >> IEEE80211_FC0_SUBTYPE_SHIFT]);
299 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
302 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
305 for (i = 0; i < IEEE80211_WEP_IVLEN; i++)
306 printf(" %.02x", buf[sizeof(*wh)+i]);
307 printf(" KID %u]", buf[sizeof(*wh)+i] >> 6);
310 printf(" %dM", rate / 2);
312 printf(" +%d", rssi);
315 for (i = 0; i < len; i++) {
318 printf("%02x", buf[i]);
325 ieee80211_fix_rate(struct ieee80211_node *ni, int flags)
327 #define RV(v) ((v) & IEEE80211_RATE_VAL)
328 struct ieee80211com *ic = ni->ni_ic;
329 int i, j, ignore, error;
330 int okrate, badrate, fixedrate;
331 struct ieee80211_rateset *srs, *nrs;
335 * If the fixed rate check was requested but no
336 * fixed has been defined then just remove it.
338 if ((flags & IEEE80211_F_DOFRATE) &&
339 ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
340 flags &= ~IEEE80211_F_DOFRATE;
342 okrate = badrate = fixedrate = 0;
343 srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
345 for (i = 0; i < nrs->rs_nrates; ) {
347 if (flags & IEEE80211_F_DOSORT) {
351 for (j = i + 1; j < nrs->rs_nrates; j++) {
352 if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
353 r = nrs->rs_rates[i];
354 nrs->rs_rates[i] = nrs->rs_rates[j];
355 nrs->rs_rates[j] = r;
359 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
361 if (flags & IEEE80211_F_DOFRATE) {
363 * Check any fixed rate is included.
365 if (r == RV(srs->rs_rates[ic->ic_fixed_rate]))
368 if (flags & IEEE80211_F_DONEGO) {
370 * Check against supported rates.
372 for (j = 0; j < srs->rs_nrates; j++) {
373 if (r == RV(srs->rs_rates[j])) {
375 * Overwrite with the supported rate
376 * value so any basic rate bit is set.
377 * This insures that response we send
378 * to stations have the necessary basic
381 nrs->rs_rates[i] = srs->rs_rates[j];
385 if (j == srs->rs_nrates) {
387 * A rate in the node's rate set is not
388 * supported. If this is a basic rate and we
389 * are operating as an AP then this is an error.
390 * Otherwise we just discard/ignore the rate.
391 * Note that this is important for 11b stations
392 * when they want to associate with an 11g AP.
394 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
395 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
400 if (flags & IEEE80211_F_DODEL) {
402 * Delete unacceptable rates.
406 for (j = i; j < nrs->rs_nrates; j++)
407 nrs->rs_rates[j] = nrs->rs_rates[j + 1];
408 nrs->rs_rates[j] = 0;
413 okrate = nrs->rs_rates[i];
416 if (okrate == 0 || error != 0 ||
417 ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0))
418 return badrate | IEEE80211_RATE_BASIC;
425 * Reset 11g-related state.
428 ieee80211_reset_erp(struct ieee80211com *ic)
430 ic->ic_flags &= ~IEEE80211_F_USEPROT;
431 ic->ic_nonerpsta = 0;
432 ic->ic_longslotsta = 0;
434 * Short slot time is enabled only when operating in 11g
435 * and not in an IBSS. We must also honor whether or not
436 * the driver is capable of doing it.
438 ieee80211_set_shortslottime(ic,
439 ic->ic_curmode == IEEE80211_MODE_11A ||
440 (ic->ic_curmode == IEEE80211_MODE_11G &&
441 ic->ic_opmode == IEEE80211_M_HOSTAP &&
442 (ic->ic_caps & IEEE80211_C_SHSLOT)));
444 * Set short preamble and ERP barker-preamble flags.
446 if (ic->ic_curmode == IEEE80211_MODE_11A ||
447 (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
448 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
449 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
451 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
452 ic->ic_flags |= IEEE80211_F_USEBARKER;
457 * Set the short slot time state and notify the driver.
460 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
463 ic->ic_flags |= IEEE80211_F_SHSLOT;
465 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
467 if (ic->ic_updateslot != NULL)
468 ic->ic_updateslot(ic->ic_ifp);
472 * Check if the specified rate set supports ERP.
473 * NB: the rate set is assumed to be sorted.
476 ieee80211_iserp_rateset(struct ieee80211com *ic, struct ieee80211_rateset *rs)
478 #define N(a) (sizeof(a) / sizeof(a[0]))
479 static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
482 if (rs->rs_nrates < N(rates))
484 for (i = 0; i < N(rates); i++) {
485 for (j = 0; j < rs->rs_nrates; j++) {
486 int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
501 * Mark the basic rates for the 11g rate table based on the
502 * operating mode. For real 11g we mark all the 11b rates
503 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
504 * 11b rates. There's also a pseudo 11a-mode used to mark only
505 * the basic OFDM rates.
508 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
510 static const struct ieee80211_rateset basic[] = {
511 { 0 }, /* IEEE80211_MODE_AUTO */
512 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */
513 { 2, { 2, 4 } }, /* IEEE80211_MODE_11B */
514 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11G (mixed b/g) */
515 { 0 }, /* IEEE80211_MODE_FH */
516 /* IEEE80211_MODE_PUREG (not yet) */
517 { 7, { 2, 4, 11, 22, 12, 24, 48 } },
521 for (i = 0; i < rs->rs_nrates; i++) {
522 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
523 for (j = 0; j < basic[mode].rs_nrates; j++)
524 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
525 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
532 * WME protocol support. The following parameters come from the spec.
534 typedef struct phyParamType {
542 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
543 { 3, 4, 6 }, /* IEEE80211_MODE_AUTO */
544 { 3, 4, 6 }, /* IEEE80211_MODE_11A */
545 { 3, 5, 7 }, /* IEEE80211_MODE_11B */
546 { 3, 4, 6 }, /* IEEE80211_MODE_11G */
547 { 3, 5, 7 }, /* IEEE80211_MODE_FH */
548 { 2, 3, 5 }, /* IEEE80211_MODE_TURBO_A */
549 { 2, 3, 5 }, /* IEEE80211_MODE_TURBO_G */
551 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
552 { 7, 4, 10 }, /* IEEE80211_MODE_AUTO */
553 { 7, 4, 10 }, /* IEEE80211_MODE_11A */
554 { 7, 5, 10 }, /* IEEE80211_MODE_11B */
555 { 7, 4, 10 }, /* IEEE80211_MODE_11G */
556 { 7, 5, 10 }, /* IEEE80211_MODE_FH */
557 { 7, 3, 10 }, /* IEEE80211_MODE_TURBO_A */
558 { 7, 3, 10 }, /* IEEE80211_MODE_TURBO_G */
560 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
561 { 1, 3, 4, 94 }, /* IEEE80211_MODE_AUTO */
562 { 1, 3, 4, 94 }, /* IEEE80211_MODE_11A */
563 { 1, 4, 5, 188 }, /* IEEE80211_MODE_11B */
564 { 1, 3, 4, 94 }, /* IEEE80211_MODE_11G */
565 { 1, 4, 5, 188 }, /* IEEE80211_MODE_FH */
566 { 1, 2, 3, 94 }, /* IEEE80211_MODE_TURBO_A */
567 { 1, 2, 3, 94 }, /* IEEE80211_MODE_TURBO_G */
569 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
570 { 1, 2, 3, 47 }, /* IEEE80211_MODE_AUTO */
571 { 1, 2, 3, 47 }, /* IEEE80211_MODE_11A */
572 { 1, 3, 4, 102 }, /* IEEE80211_MODE_11B */
573 { 1, 2, 3, 47 }, /* IEEE80211_MODE_11G */
574 { 1, 3, 4, 102 }, /* IEEE80211_MODE_FH */
575 { 1, 2, 2, 47 }, /* IEEE80211_MODE_TURBO_A */
576 { 1, 2, 2, 47 }, /* IEEE80211_MODE_TURBO_G */
579 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
580 { 3, 4, 10 }, /* IEEE80211_MODE_AUTO */
581 { 3, 4, 10 }, /* IEEE80211_MODE_11A */
582 { 3, 5, 10 }, /* IEEE80211_MODE_11B */
583 { 3, 4, 10 }, /* IEEE80211_MODE_11G */
584 { 3, 5, 10 }, /* IEEE80211_MODE_FH */
585 { 2, 3, 10 }, /* IEEE80211_MODE_TURBO_A */
586 { 2, 3, 10 }, /* IEEE80211_MODE_TURBO_G */
588 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
589 { 2, 3, 4, 94 }, /* IEEE80211_MODE_AUTO */
590 { 2, 3, 4, 94 }, /* IEEE80211_MODE_11A */
591 { 2, 4, 5, 188 }, /* IEEE80211_MODE_11B */
592 { 2, 3, 4, 94 }, /* IEEE80211_MODE_11G */
593 { 2, 4, 5, 188 }, /* IEEE80211_MODE_FH */
594 { 2, 2, 3, 94 }, /* IEEE80211_MODE_TURBO_A */
595 { 2, 2, 3, 94 }, /* IEEE80211_MODE_TURBO_G */
597 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
598 { 2, 2, 3, 47 }, /* IEEE80211_MODE_AUTO */
599 { 2, 2, 3, 47 }, /* IEEE80211_MODE_11A */
600 { 2, 3, 4, 102 }, /* IEEE80211_MODE_11B */
601 { 2, 2, 3, 47 }, /* IEEE80211_MODE_11G */
602 { 2, 3, 4, 102 }, /* IEEE80211_MODE_FH */
603 { 1, 2, 2, 47 }, /* IEEE80211_MODE_TURBO_A */
604 { 1, 2, 2, 47 }, /* IEEE80211_MODE_TURBO_G */
608 ieee80211_wme_initparams(struct ieee80211com *ic)
610 struct ieee80211_wme_state *wme = &ic->ic_wme;
611 const paramType *pPhyParam, *pBssPhyParam;
612 struct wmeParams *wmep;
615 if ((ic->ic_caps & IEEE80211_C_WME) == 0)
618 for (i = 0; i < WME_NUM_AC; i++) {
621 pPhyParam = &phyParamForAC_BK[ic->ic_curmode];
622 pBssPhyParam = &phyParamForAC_BK[ic->ic_curmode];
625 pPhyParam = &phyParamForAC_VI[ic->ic_curmode];
626 pBssPhyParam = &bssPhyParamForAC_VI[ic->ic_curmode];
629 pPhyParam = &phyParamForAC_VO[ic->ic_curmode];
630 pBssPhyParam = &bssPhyParamForAC_VO[ic->ic_curmode];
634 pPhyParam = &phyParamForAC_BE[ic->ic_curmode];
635 pBssPhyParam = &bssPhyParamForAC_BE[ic->ic_curmode];
639 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
640 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
641 wmep->wmep_acm = pPhyParam->acm;
642 wmep->wmep_aifsn = pPhyParam->aifsn;
643 wmep->wmep_logcwmin = pPhyParam->logcwmin;
644 wmep->wmep_logcwmax = pPhyParam->logcwmax;
645 wmep->wmep_txopLimit = pPhyParam->txopLimit;
647 wmep->wmep_acm = pBssPhyParam->acm;
648 wmep->wmep_aifsn = pBssPhyParam->aifsn;
649 wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
650 wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
651 wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
654 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
655 "%s: %s chan [acm %u aifsn %u log2(cwmin) %u "
656 "log2(cwmax) %u txpoLimit %u]\n", __func__
657 , ieee80211_wme_acnames[i]
660 , wmep->wmep_logcwmin
661 , wmep->wmep_logcwmax
662 , wmep->wmep_txopLimit
665 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
666 wmep->wmep_acm = pBssPhyParam->acm;
667 wmep->wmep_aifsn = pBssPhyParam->aifsn;
668 wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
669 wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
670 wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
671 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
672 "%s: %s bss [acm %u aifsn %u log2(cwmin) %u "
673 "log2(cwmax) %u txpoLimit %u]\n", __func__
674 , ieee80211_wme_acnames[i]
677 , wmep->wmep_logcwmin
678 , wmep->wmep_logcwmax
679 , wmep->wmep_txopLimit
682 /* NB: check ic_bss to avoid NULL deref on initial attach */
683 if (ic->ic_bss != NULL) {
685 * Calculate agressive mode switching threshold based
686 * on beacon interval. This doesn't need locking since
687 * we're only called before entering the RUN state at
688 * which point we start sending beacon frames.
690 wme->wme_hipri_switch_thresh =
691 (HIGH_PRI_SWITCH_THRESH * ic->ic_bss->ni_intval) / 100;
692 ieee80211_wme_updateparams(ic);
697 * Update WME parameters for ourself and the BSS.
700 ieee80211_wme_updateparams_locked(struct ieee80211com *ic)
702 static const paramType phyParam[IEEE80211_MODE_MAX] = {
703 { 2, 4, 10, 64 }, /* IEEE80211_MODE_AUTO */
704 { 2, 4, 10, 64 }, /* IEEE80211_MODE_11A */
705 { 2, 5, 10, 64 }, /* IEEE80211_MODE_11B */
706 { 2, 4, 10, 64 }, /* IEEE80211_MODE_11G */
707 { 2, 5, 10, 64 }, /* IEEE80211_MODE_FH */
708 { 1, 3, 10, 64 }, /* IEEE80211_MODE_TURBO_A */
709 { 1, 3, 10, 64 }, /* IEEE80211_MODE_TURBO_G */
711 struct ieee80211_wme_state *wme = &ic->ic_wme;
712 const struct wmeParams *wmep;
713 struct wmeParams *chanp, *bssp;
716 /* set up the channel access parameters for the physical device */
717 for (i = 0; i < WME_NUM_AC; i++) {
718 chanp = &wme->wme_chanParams.cap_wmeParams[i];
719 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
720 chanp->wmep_aifsn = wmep->wmep_aifsn;
721 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
722 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
723 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
725 chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
726 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
727 chanp->wmep_aifsn = wmep->wmep_aifsn;
728 chanp->wmep_logcwmin = wmep->wmep_logcwmin;
729 chanp->wmep_logcwmax = wmep->wmep_logcwmax;
730 chanp->wmep_txopLimit = wmep->wmep_txopLimit;
734 * This implements agressive mode as found in certain
735 * vendors' AP's. When there is significant high
736 * priority (VI/VO) traffic in the BSS throttle back BE
737 * traffic by using conservative parameters. Otherwise
738 * BE uses agressive params to optimize performance of
739 * legacy/non-QoS traffic.
741 if ((ic->ic_opmode == IEEE80211_M_HOSTAP &&
742 (wme->wme_flags & WME_F_AGGRMODE) == 0) ||
743 (ic->ic_opmode == IEEE80211_M_STA &&
744 (ic->ic_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
745 (ic->ic_flags & IEEE80211_F_WME) == 0) {
746 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
747 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
749 chanp->wmep_aifsn = bssp->wmep_aifsn =
750 phyParam[ic->ic_curmode].aifsn;
751 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
752 phyParam[ic->ic_curmode].logcwmin;
753 chanp->wmep_logcwmax = bssp->wmep_logcwmax =
754 phyParam[ic->ic_curmode].logcwmax;
755 chanp->wmep_txopLimit = bssp->wmep_txopLimit =
756 (ic->ic_flags & IEEE80211_F_BURST) ?
757 phyParam[ic->ic_curmode].txopLimit : 0;
758 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
759 "%s: %s [acm %u aifsn %u log2(cwmin) %u "
760 "log2(cwmax) %u txpoLimit %u]\n", __func__
761 , ieee80211_wme_acnames[WME_AC_BE]
764 , chanp->wmep_logcwmin
765 , chanp->wmep_logcwmax
766 , chanp->wmep_txopLimit
770 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
771 ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) == 0) {
772 static const u_int8_t logCwMin[IEEE80211_MODE_MAX] = {
773 3, /* IEEE80211_MODE_AUTO */
774 3, /* IEEE80211_MODE_11A */
775 4, /* IEEE80211_MODE_11B */
776 3, /* IEEE80211_MODE_11G */
777 4, /* IEEE80211_MODE_FH */
778 3, /* IEEE80211_MODE_TURBO_A */
779 3, /* IEEE80211_MODE_TURBO_G */
781 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
782 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
784 chanp->wmep_logcwmin = bssp->wmep_logcwmin =
785 logCwMin[ic->ic_curmode];
786 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
787 "%s: %s log2(cwmin) %u\n", __func__
788 , ieee80211_wme_acnames[WME_AC_BE]
789 , chanp->wmep_logcwmin
792 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* XXX ibss? */
794 * Arrange for a beacon update and bump the parameter
795 * set number so associated stations load the new values.
797 wme->wme_bssChanParams.cap_info =
798 (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
799 ic->ic_flags |= IEEE80211_F_WMEUPDATE;
804 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
805 "%s: WME params updated, cap_info 0x%x\n", __func__,
806 ic->ic_opmode == IEEE80211_M_STA ?
807 wme->wme_wmeChanParams.cap_info :
808 wme->wme_bssChanParams.cap_info);
812 ieee80211_wme_updateparams(struct ieee80211com *ic)
815 if (ic->ic_caps & IEEE80211_C_WME) {
816 IEEE80211_BEACON_LOCK(ic);
817 ieee80211_wme_updateparams_locked(ic);
818 IEEE80211_BEACON_UNLOCK(ic);
823 ieee80211_beacon_miss(struct ieee80211com *ic)
826 if (ic->ic_flags & IEEE80211_F_SCAN) {
827 /* XXX check ic_curchan != ic_bsschan? */
830 IEEE80211_DPRINTF(ic,
831 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
832 "%s\n", "beacon miss");
835 * Our handling is only meaningful for stations that are
836 * associated; any other conditions else will be handled
837 * through different means (e.g. the tx timeout on mgt frames).
839 if (ic->ic_opmode != IEEE80211_M_STA || ic->ic_state != IEEE80211_S_RUN)
842 if (++ic->ic_bmiss_count < ic->ic_bmiss_max) {
844 * Send a directed probe req before falling back to a scan;
845 * if we receive a response ic_bmiss_count will be reset.
846 * Some cards mistakenly report beacon miss so this avoids
847 * the expensive scan if the ap is still there.
849 ieee80211_send_probereq(ic->ic_bss, ic->ic_myaddr,
850 ic->ic_bss->ni_bssid, ic->ic_bss->ni_bssid,
851 ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen,
852 ic->ic_opt_ie, ic->ic_opt_ie_len);
855 ic->ic_bmiss_count = 0;
856 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
860 * Software beacon miss handling. Check if any beacons
861 * were received in the last period. If not post a
862 * beacon miss; otherwise reset the counter.
865 ieee80211_swbmiss(void *arg)
867 struct ieee80211com *ic = arg;
869 if (ic->ic_swbmiss_count == 0) {
870 ieee80211_beacon_miss(ic);
871 if (ic->ic_bmiss_count == 0) /* don't re-arm timer */
874 ic->ic_swbmiss_count = 0;
875 callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period,
876 ieee80211_swbmiss, ic);
880 sta_disassoc(void *arg, struct ieee80211_node *ni)
882 struct ieee80211com *ic = arg;
884 if (ni->ni_associd != 0) {
885 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
886 IEEE80211_REASON_ASSOC_LEAVE);
887 ieee80211_node_leave(ic, ni);
892 sta_deauth(void *arg, struct ieee80211_node *ni)
894 struct ieee80211com *ic = arg;
896 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
897 IEEE80211_REASON_ASSOC_LEAVE);
901 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
903 struct ifnet *ifp = ic->ic_ifp;
904 struct ieee80211_node *ni;
905 enum ieee80211_state ostate;
907 ostate = ic->ic_state;
908 IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
909 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
910 ic->ic_state = nstate; /* state transition */
911 ni = ic->ic_bss; /* NB: no reference held */
912 if (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS)
913 callout_stop(&ic->ic_swbmiss);
915 case IEEE80211_S_INIT:
917 case IEEE80211_S_INIT:
919 case IEEE80211_S_RUN:
920 switch (ic->ic_opmode) {
921 case IEEE80211_M_STA:
922 IEEE80211_SEND_MGMT(ic, ni,
923 IEEE80211_FC0_SUBTYPE_DISASSOC,
924 IEEE80211_REASON_ASSOC_LEAVE);
925 ieee80211_sta_leave(ic, ni);
927 case IEEE80211_M_HOSTAP:
928 ieee80211_iterate_nodes(&ic->ic_sta,
935 case IEEE80211_S_ASSOC:
936 switch (ic->ic_opmode) {
937 case IEEE80211_M_STA:
938 IEEE80211_SEND_MGMT(ic, ni,
939 IEEE80211_FC0_SUBTYPE_DEAUTH,
940 IEEE80211_REASON_AUTH_LEAVE);
942 case IEEE80211_M_HOSTAP:
943 ieee80211_iterate_nodes(&ic->ic_sta,
950 case IEEE80211_S_SCAN:
951 ieee80211_cancel_scan(ic);
953 case IEEE80211_S_AUTH:
955 ic->ic_mgt_timer = 0;
956 IF_DRAIN(&ic->ic_mgtq);
957 ieee80211_reset_bss(ic);
960 if (ic->ic_auth->ia_detach != NULL)
961 ic->ic_auth->ia_detach(ic);
963 case IEEE80211_S_SCAN:
965 case IEEE80211_S_INIT:
966 if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
967 ic->ic_opmode == IEEE80211_M_IBSS ||
968 ic->ic_opmode == IEEE80211_M_AHDEMO) &&
969 ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
971 * AP operation and we already have a channel;
972 * bypass the scan and startup immediately.
974 ieee80211_create_ibss(ic, ic->ic_des_chan);
976 ieee80211_begin_scan(ic, arg);
979 case IEEE80211_S_SCAN:
981 * Scan next. If doing an active scan and the
982 * channel is not marked passive-only then send
983 * a probe request. Otherwise just listen for
984 * beacons on the channel.
986 if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
987 (ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
988 ieee80211_send_probereq(ni,
989 ic->ic_myaddr, ifp->if_broadcastaddr,
990 ifp->if_broadcastaddr,
991 ic->ic_des_essid, ic->ic_des_esslen,
992 ic->ic_opt_ie, ic->ic_opt_ie_len);
995 case IEEE80211_S_RUN:
997 IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE,
998 "no recent beacons from %s; rescanning\n",
999 ether_sprintf(ic->ic_bss->ni_bssid));
1000 ieee80211_sta_leave(ic, ni);
1001 ic->ic_flags &= ~IEEE80211_F_SIBSS; /* XXX */
1003 case IEEE80211_S_AUTH:
1004 case IEEE80211_S_ASSOC:
1005 /* timeout restart scan */
1006 ni = ieee80211_find_node(&ic->ic_scan,
1007 ic->ic_bss->ni_macaddr);
1010 ieee80211_unref_node(&ni);
1012 if (ic->ic_roaming == IEEE80211_ROAMING_AUTO)
1013 ieee80211_begin_scan(ic, arg);
1017 case IEEE80211_S_AUTH:
1019 case IEEE80211_S_INIT:
1020 case IEEE80211_S_SCAN:
1021 IEEE80211_SEND_MGMT(ic, ni,
1022 IEEE80211_FC0_SUBTYPE_AUTH, 1);
1024 case IEEE80211_S_AUTH:
1025 case IEEE80211_S_ASSOC:
1027 case IEEE80211_FC0_SUBTYPE_AUTH:
1029 IEEE80211_SEND_MGMT(ic, ni,
1030 IEEE80211_FC0_SUBTYPE_AUTH, 2);
1032 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1033 /* ignore and retry scan on timeout */
1037 case IEEE80211_S_RUN:
1039 case IEEE80211_FC0_SUBTYPE_AUTH:
1040 IEEE80211_SEND_MGMT(ic, ni,
1041 IEEE80211_FC0_SUBTYPE_AUTH, 2);
1042 ic->ic_state = ostate; /* stay RUN */
1044 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1045 ieee80211_sta_leave(ic, ni);
1046 if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
1048 IEEE80211_SEND_MGMT(ic, ni,
1049 IEEE80211_FC0_SUBTYPE_AUTH, 1);
1056 case IEEE80211_S_ASSOC:
1058 case IEEE80211_S_INIT:
1059 case IEEE80211_S_SCAN:
1060 case IEEE80211_S_ASSOC:
1061 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1062 "%s: invalid transition\n", __func__);
1064 case IEEE80211_S_AUTH:
1065 IEEE80211_SEND_MGMT(ic, ni,
1066 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
1068 case IEEE80211_S_RUN:
1069 ieee80211_sta_leave(ic, ni);
1070 if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
1071 IEEE80211_SEND_MGMT(ic, ni,
1072 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
1077 case IEEE80211_S_RUN:
1078 if (ic->ic_flags & IEEE80211_F_WPA) {
1079 /* XXX validate prerequisites */
1082 case IEEE80211_S_INIT:
1083 if (ic->ic_opmode == IEEE80211_M_MONITOR)
1086 case IEEE80211_S_AUTH:
1087 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1088 "%s: invalid transition\n", __func__);
1090 case IEEE80211_S_RUN:
1092 case IEEE80211_S_SCAN: /* adhoc/hostap mode */
1093 case IEEE80211_S_ASSOC: /* infra mode */
1094 KASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
1095 ("%s: bogus xmit rate %u setup\n", __func__,
1097 #ifdef IEEE80211_DEBUG
1098 if (ieee80211_msg_debug(ic)) {
1099 if (ic->ic_opmode == IEEE80211_M_STA)
1100 if_printf(ifp, "associated ");
1102 if_printf(ifp, "synchronized ");
1103 printf("with %s ssid ",
1104 ether_sprintf(ni->ni_bssid));
1105 ieee80211_print_essid(ic->ic_bss->ni_essid,
1107 printf(" channel %d start %uMb\n",
1108 ieee80211_chan2ieee(ic, ic->ic_curchan),
1109 IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
1112 ic->ic_mgt_timer = 0;
1113 if (ic->ic_opmode == IEEE80211_M_STA)
1114 ieee80211_notify_node_join(ic, ni,
1115 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
1116 if_start(ifp); /* XXX not authorized yet */
1119 if (ostate != IEEE80211_S_RUN &&
1120 ic->ic_opmode == IEEE80211_M_STA &&
1121 (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS)) {
1123 * Start s/w beacon miss timer for devices w/o
1124 * hardware support. We fudge a bit here since
1125 * we're doing this in software.
1127 ic->ic_swbmiss_period = IEEE80211_TU_TO_TICKS(
1128 2 * ic->ic_bmissthreshold * ni->ni_intval);
1129 ic->ic_swbmiss_count = 0;
1130 callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period,
1131 ieee80211_swbmiss, ic);
1134 * Start/stop the authenticator when operating as an
1135 * AP. We delay until here to allow configuration to
1136 * happen out of order.
1138 if (ic->ic_opmode == IEEE80211_M_HOSTAP && /* XXX IBSS/AHDEMO */
1139 ic->ic_auth->ia_attach != NULL) {
1140 /* XXX check failure */
1141 ic->ic_auth->ia_attach(ic);
1142 } else if (ic->ic_auth->ia_detach != NULL) {
1143 ic->ic_auth->ia_detach(ic);
1146 * When 802.1x is not in use mark the port authorized
1147 * at this point so traffic can flow.
1149 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
1150 ieee80211_node_authorize(ni);
1152 * Enable inactivity processing.
1155 ic->ic_scan.nt_inact_timer = IEEE80211_INACT_WAIT;
1156 ic->ic_sta.nt_inact_timer = IEEE80211_INACT_WAIT;