1 /*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 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. 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 * 26 * $FreeBSD: head/sys/net80211/ieee80211_proto.c 195618 2009-07-11 15:02:45Z rpaulo $ 27 * $DragonFly$ 28 */ 29 30 /* 31 * IEEE 802.11 protocol support. 32 */ 33 34 #include "opt_inet.h" 35 #include "opt_wlan.h" 36 37 #include <sys/param.h> 38 #include <sys/kernel.h> 39 #include <sys/systm.h> 40 41 #include <sys/socket.h> 42 #include <sys/sockio.h> 43 44 #include <net/if.h> 45 #include <net/if_media.h> 46 #include <net/route.h> 47 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> 55 #endif 56 #include <netproto/802_11/ieee80211_monitor.h> 57 #include <netproto/802_11/ieee80211_input.h> 58 59 /* XXX tunables */ 60 #define AGGRESSIVE_MODE_SWITCH_HYSTERESIS 3 /* pkts / 100ms */ 61 #define HIGH_PRI_SWITCH_THRESH 10 /* pkts / 100ms */ 62 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" 68 }; 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" 74 }; 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 */ 83 }; 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 */ 93 }; 94 const char *ieee80211_wme_acnames[] = { 95 "WME_AC_BE", 96 "WME_AC_BK", 97 "WME_AC_VI", 98 "WME_AC_VO", 99 "WME_UPSD", 100 }; 101 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); 111 112 static int 113 null_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 } 122 123 void 124 ieee80211_proto_attach(struct ieee80211com *ic) 125 { 126 struct ifnet *ifp = ic->ic_ifp; 127 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 } 140 ic->ic_protmode = IEEE80211_PROT_CTSONLY; 141 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); 147 148 ic->ic_wme.wme_hipri_switch_hysteresis = 149 AGGRESSIVE_MODE_SWITCH_HYSTERESIS; 150 151 /* initialize management frame handlers */ 152 ic->ic_send_mgmt = ieee80211_send_mgmt; 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); 163 } 164 165 void 166 ieee80211_proto_detach(struct ieee80211com *ic) 167 { 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 178 static void 179 null_update_beacon(struct ieee80211vap *vap, int item) 180 { 181 } 182 183 void 184 ieee80211_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; 192 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); 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 230 void 231 ieee80211_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); 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 */ 247 if (vap->iv_auth->ia_detach != NULL) 248 vap->iv_auth->ia_detach(vap); 249 /* 250 * Detach any ACL'ator. 251 */ 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 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 */ 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 */ 277 }; 278 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX]; 279 280 static 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 */ 291 static void 292 ieee80211_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 } 298 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL); 299 300 const struct ieee80211_authenticator * 301 ieee80211_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 310 void 311 ieee80211_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 319 void 320 ieee80211_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 */ 332 static const struct ieee80211_aclator *acl = NULL; 333 334 void 335 ieee80211_aclator_register(const struct ieee80211_aclator *iac) 336 { 337 kprintf("wlan: %s acl policy registered\n", iac->iac_name); 338 acl = iac; 339 } 340 341 void 342 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac) 343 { 344 if (acl == iac) 345 acl = NULL; 346 kprintf("wlan: %s acl policy unregistered\n", iac->iac_name); 347 } 348 349 const struct ieee80211_aclator * 350 ieee80211_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; 355 } 356 357 void 358 ieee80211_print_essid(const uint8_t *essid, int len) 359 { 360 const uint8_t *p; 361 int i; 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) { 371 kprintf("\""); 372 for (i = 0, p = essid; i < len; i++, p++) 373 kprintf("%c", *p); 374 kprintf("\""); 375 } else { 376 kprintf("0x"); 377 for (i = 0, p = essid; i < len; i++, p++) 378 kprintf("%02x", *p); 379 } 380 } 381 382 void 383 ieee80211_dump_pkt(struct ieee80211com *ic, 384 const uint8_t *buf, int len, int rate, int rssi) 385 { 386 const struct ieee80211_frame *wh; 387 int i; 388 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, ":"); 395 break; 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, ":"); 400 break; 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, ":"); 405 break; 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, ":"); 411 break; 412 } 413 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 414 case IEEE80211_FC0_TYPE_DATA: 415 kprintf(" data"); 416 break; 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]); 421 break; 422 default: 423 kprintf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK); 424 break; 425 } 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 } 432 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 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); 442 } 443 if (rate >= 0) 444 kprintf(" %dM", rate / 2); 445 if (rssi >= 0) 446 kprintf(" +%d", rssi); 447 kprintf("\n"); 448 if (len > 0) { 449 for (i = 0; i < len; i++) { 450 if ((i & 1) == 0) 451 kprintf(" "); 452 kprintf("%02x", buf[i]); 453 } 454 kprintf("\n"); 455 } 456 } 457 458 static __inline int 459 findrix(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 469 int 470 ieee80211_fix_rate(struct ieee80211_node *ni, 471 struct ieee80211_rateset *nrs, int flags) 472 { 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; 479 uint8_t r; 480 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; 508 /* 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. 515 */ 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); 521 for (i = 0; i < nrs->rs_nrates; ) { 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; 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) { 547 /* 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. 552 */ 553 if ((flags & IEEE80211_F_JOIN) && 554 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC)) 555 error++; 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]; 562 } 563 } 564 if ((flags & IEEE80211_F_DODEL) && rix < 0) { 565 /* 566 * Delete unacceptable rates. 567 */ 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; 573 } 574 if (rix >= 0) 575 okrate = nrs->rs_rates[i]; 576 i++; 577 } 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; 586 } else 587 return RV(okrate); 588 #undef RV 589 } 590 591 /* 592 * Reset 11g-related state. 593 */ 594 void 595 ieee80211_reset_erp(struct ieee80211com *ic) 596 { 597 ic->ic_flags &= ~IEEE80211_F_USEPROT; 598 ic->ic_nonerpsta = 0; 599 ic->ic_longslotsta = 0; 600 /* 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. 604 */ 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))); 611 /* 612 * Set short preamble and ERP barker-preamble flags. 613 */ 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; 618 } else { 619 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 620 ic->ic_flags |= IEEE80211_F_USEBARKER; 621 } 622 } 623 624 /* 625 * Set the short slot time state and notify the driver. 626 */ 627 void 628 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff) 629 { 630 if (onoff) 631 ic->ic_flags |= IEEE80211_F_SHSLOT; 632 else 633 ic->ic_flags &= ~IEEE80211_F_SHSLOT; 634 /* notify driver */ 635 if (ic->ic_updateslot != NULL) 636 ic->ic_updateslot(ic->ic_ifp); 637 } 638 639 /* 640 * Check if the specified rate set supports ERP. 641 * NB: the rate set is assumed to be sorted. 642 */ 643 int 644 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs) 645 { 646 #define N(a) (sizeof(a) / sizeof(a[0])) 647 static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 }; 648 int i, j; 649 650 if (rs->rs_nrates < N(rates)) 651 return 0; 652 for (i = 0; i < N(rates); i++) { 653 for (j = 0; j < rs->rs_nrates; j++) { 654 int r = rs->rs_rates[j] & IEEE80211_RATE_VAL; 655 if (rates[i] == r) 656 goto next; 657 if (r > rates[i]) 658 return 0; 659 } 660 return 0; 661 next: 662 ; 663 } 664 return 1; 665 #undef N 666 } 667 668 /* 669 * Mark the basic rates for the rate table based on the 670 * operating mode. For real 11g we mark all the 11b rates 671 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only 672 * 11b rates. There's also a pseudo 11a-mode used to mark only 673 * the basic OFDM rates. 674 */ 675 static void 676 setbasicrates(struct ieee80211_rateset *rs, 677 enum ieee80211_phymode mode, int add) 678 { 679 static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = { 680 [IEEE80211_MODE_11A] = { 3, { 12, 24, 48 } }, 681 [IEEE80211_MODE_11B] = { 2, { 2, 4 } }, 682 /* NB: mixed b/g */ 683 [IEEE80211_MODE_11G] = { 4, { 2, 4, 11, 22 } }, 684 [IEEE80211_MODE_TURBO_A] = { 3, { 12, 24, 48 } }, 685 [IEEE80211_MODE_TURBO_G] = { 4, { 2, 4, 11, 22 } }, 686 [IEEE80211_MODE_STURBO_A] = { 3, { 12, 24, 48 } }, 687 [IEEE80211_MODE_HALF] = { 3, { 6, 12, 24 } }, 688 [IEEE80211_MODE_QUARTER] = { 3, { 3, 6, 12 } }, 689 [IEEE80211_MODE_11NA] = { 3, { 12, 24, 48 } }, 690 /* NB: mixed b/g */ 691 [IEEE80211_MODE_11NG] = { 4, { 2, 4, 11, 22 } }, 692 }; 693 int i, j; 694 695 for (i = 0; i < rs->rs_nrates; i++) { 696 if (!add) 697 rs->rs_rates[i] &= IEEE80211_RATE_VAL; 698 for (j = 0; j < basic[mode].rs_nrates; j++) 699 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) { 700 rs->rs_rates[i] |= IEEE80211_RATE_BASIC; 701 break; 702 } 703 } 704 } 705 706 /* 707 * Set the basic rates in a rate set. 708 */ 709 void 710 ieee80211_setbasicrates(struct ieee80211_rateset *rs, 711 enum ieee80211_phymode mode) 712 { 713 setbasicrates(rs, mode, 0); 714 } 715 716 /* 717 * Add basic rates to a rate set. 718 */ 719 void 720 ieee80211_addbasicrates(struct ieee80211_rateset *rs, 721 enum ieee80211_phymode mode) 722 { 723 setbasicrates(rs, mode, 1); 724 } 725 726 /* 727 * WME protocol support. 728 * 729 * The default 11a/b/g/n parameters come from the WiFi Alliance WMM 730 * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n 731 * Draft 2.0 Test Plan (Appendix D). 732 * 733 * Static/Dynamic Turbo mode settings come from Atheros. 734 */ 735 typedef struct phyParamType { 736 uint8_t aifsn; 737 uint8_t logcwmin; 738 uint8_t logcwmax; 739 uint16_t txopLimit; 740 uint8_t acm; 741 } paramType; 742 743 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = { 744 [IEEE80211_MODE_AUTO] = { 3, 4, 6, 0, 0 }, 745 [IEEE80211_MODE_11A] = { 3, 4, 6, 0, 0 }, 746 [IEEE80211_MODE_11B] = { 3, 4, 6, 0, 0 }, 747 [IEEE80211_MODE_11G] = { 3, 4, 6, 0, 0 }, 748 [IEEE80211_MODE_FH] = { 3, 4, 6, 0, 0 }, 749 [IEEE80211_MODE_TURBO_A]= { 2, 3, 5, 0, 0 }, 750 [IEEE80211_MODE_TURBO_G]= { 2, 3, 5, 0, 0 }, 751 [IEEE80211_MODE_STURBO_A]={ 2, 3, 5, 0, 0 }, 752 [IEEE80211_MODE_HALF] = { 3, 4, 6, 0, 0 }, 753 [IEEE80211_MODE_QUARTER]= { 3, 4, 6, 0, 0 }, 754 [IEEE80211_MODE_11NA] = { 3, 4, 6, 0, 0 }, 755 [IEEE80211_MODE_11NG] = { 3, 4, 6, 0, 0 }, 756 }; 757 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = { 758 [IEEE80211_MODE_AUTO] = { 7, 4, 10, 0, 0 }, 759 [IEEE80211_MODE_11A] = { 7, 4, 10, 0, 0 }, 760 [IEEE80211_MODE_11B] = { 7, 4, 10, 0, 0 }, 761 [IEEE80211_MODE_11G] = { 7, 4, 10, 0, 0 }, 762 [IEEE80211_MODE_FH] = { 7, 4, 10, 0, 0 }, 763 [IEEE80211_MODE_TURBO_A]= { 7, 3, 10, 0, 0 }, 764 [IEEE80211_MODE_TURBO_G]= { 7, 3, 10, 0, 0 }, 765 [IEEE80211_MODE_STURBO_A]={ 7, 3, 10, 0, 0 }, 766 [IEEE80211_MODE_HALF] = { 7, 4, 10, 0, 0 }, 767 [IEEE80211_MODE_QUARTER]= { 7, 4, 10, 0, 0 }, 768 [IEEE80211_MODE_11NA] = { 7, 4, 10, 0, 0 }, 769 [IEEE80211_MODE_11NG] = { 7, 4, 10, 0, 0 }, 770 }; 771 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = { 772 [IEEE80211_MODE_AUTO] = { 1, 3, 4, 94, 0 }, 773 [IEEE80211_MODE_11A] = { 1, 3, 4, 94, 0 }, 774 [IEEE80211_MODE_11B] = { 1, 3, 4, 188, 0 }, 775 [IEEE80211_MODE_11G] = { 1, 3, 4, 94, 0 }, 776 [IEEE80211_MODE_FH] = { 1, 3, 4, 188, 0 }, 777 [IEEE80211_MODE_TURBO_A]= { 1, 2, 3, 94, 0 }, 778 [IEEE80211_MODE_TURBO_G]= { 1, 2, 3, 94, 0 }, 779 [IEEE80211_MODE_STURBO_A]={ 1, 2, 3, 94, 0 }, 780 [IEEE80211_MODE_HALF] = { 1, 3, 4, 94, 0 }, 781 [IEEE80211_MODE_QUARTER]= { 1, 3, 4, 94, 0 }, 782 [IEEE80211_MODE_11NA] = { 1, 3, 4, 94, 0 }, 783 [IEEE80211_MODE_11NG] = { 1, 3, 4, 94, 0 }, 784 }; 785 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = { 786 [IEEE80211_MODE_AUTO] = { 1, 2, 3, 47, 0 }, 787 [IEEE80211_MODE_11A] = { 1, 2, 3, 47, 0 }, 788 [IEEE80211_MODE_11B] = { 1, 2, 3, 102, 0 }, 789 [IEEE80211_MODE_11G] = { 1, 2, 3, 47, 0 }, 790 [IEEE80211_MODE_FH] = { 1, 2, 3, 102, 0 }, 791 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 }, 792 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 }, 793 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 }, 794 [IEEE80211_MODE_HALF] = { 1, 2, 3, 47, 0 }, 795 [IEEE80211_MODE_QUARTER]= { 1, 2, 3, 47, 0 }, 796 [IEEE80211_MODE_11NA] = { 1, 2, 3, 47, 0 }, 797 [IEEE80211_MODE_11NG] = { 1, 2, 3, 47, 0 }, 798 }; 799 800 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = { 801 [IEEE80211_MODE_AUTO] = { 3, 4, 10, 0, 0 }, 802 [IEEE80211_MODE_11A] = { 3, 4, 10, 0, 0 }, 803 [IEEE80211_MODE_11B] = { 3, 4, 10, 0, 0 }, 804 [IEEE80211_MODE_11G] = { 3, 4, 10, 0, 0 }, 805 [IEEE80211_MODE_FH] = { 3, 4, 10, 0, 0 }, 806 [IEEE80211_MODE_TURBO_A]= { 2, 3, 10, 0, 0 }, 807 [IEEE80211_MODE_TURBO_G]= { 2, 3, 10, 0, 0 }, 808 [IEEE80211_MODE_STURBO_A]={ 2, 3, 10, 0, 0 }, 809 [IEEE80211_MODE_HALF] = { 3, 4, 10, 0, 0 }, 810 [IEEE80211_MODE_QUARTER]= { 3, 4, 10, 0, 0 }, 811 [IEEE80211_MODE_11NA] = { 3, 4, 10, 0, 0 }, 812 [IEEE80211_MODE_11NG] = { 3, 4, 10, 0, 0 }, 813 }; 814 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = { 815 [IEEE80211_MODE_AUTO] = { 2, 3, 4, 94, 0 }, 816 [IEEE80211_MODE_11A] = { 2, 3, 4, 94, 0 }, 817 [IEEE80211_MODE_11B] = { 2, 3, 4, 188, 0 }, 818 [IEEE80211_MODE_11G] = { 2, 3, 4, 94, 0 }, 819 [IEEE80211_MODE_FH] = { 2, 3, 4, 188, 0 }, 820 [IEEE80211_MODE_TURBO_A]= { 2, 2, 3, 94, 0 }, 821 [IEEE80211_MODE_TURBO_G]= { 2, 2, 3, 94, 0 }, 822 [IEEE80211_MODE_STURBO_A]={ 2, 2, 3, 94, 0 }, 823 [IEEE80211_MODE_HALF] = { 2, 3, 4, 94, 0 }, 824 [IEEE80211_MODE_QUARTER]= { 2, 3, 4, 94, 0 }, 825 [IEEE80211_MODE_11NA] = { 2, 3, 4, 94, 0 }, 826 [IEEE80211_MODE_11NG] = { 2, 3, 4, 94, 0 }, 827 }; 828 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = { 829 [IEEE80211_MODE_AUTO] = { 2, 2, 3, 47, 0 }, 830 [IEEE80211_MODE_11A] = { 2, 2, 3, 47, 0 }, 831 [IEEE80211_MODE_11B] = { 2, 2, 3, 102, 0 }, 832 [IEEE80211_MODE_11G] = { 2, 2, 3, 47, 0 }, 833 [IEEE80211_MODE_FH] = { 2, 2, 3, 102, 0 }, 834 [IEEE80211_MODE_TURBO_A]= { 1, 2, 2, 47, 0 }, 835 [IEEE80211_MODE_TURBO_G]= { 1, 2, 2, 47, 0 }, 836 [IEEE80211_MODE_STURBO_A]={ 1, 2, 2, 47, 0 }, 837 [IEEE80211_MODE_HALF] = { 2, 2, 3, 47, 0 }, 838 [IEEE80211_MODE_QUARTER]= { 2, 2, 3, 47, 0 }, 839 [IEEE80211_MODE_11NA] = { 2, 2, 3, 47, 0 }, 840 [IEEE80211_MODE_11NG] = { 2, 2, 3, 47, 0 }, 841 }; 842 843 static void 844 _setifsparams(struct wmeParams *wmep, const paramType *phy) 845 { 846 wmep->wmep_aifsn = phy->aifsn; 847 wmep->wmep_logcwmin = phy->logcwmin; 848 wmep->wmep_logcwmax = phy->logcwmax; 849 wmep->wmep_txopLimit = phy->txopLimit; 850 } 851 852 static void 853 setwmeparams(struct ieee80211vap *vap, const char *type, int ac, 854 struct wmeParams *wmep, const paramType *phy) 855 { 856 wmep->wmep_acm = phy->acm; 857 _setifsparams(wmep, phy); 858 859 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 860 "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n", 861 ieee80211_wme_acnames[ac], type, 862 wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin, 863 wmep->wmep_logcwmax, wmep->wmep_txopLimit); 864 } 865 866 static void 867 ieee80211_wme_initparams_locked(struct ieee80211vap *vap) 868 { 869 struct ieee80211com *ic = vap->iv_ic; 870 struct ieee80211_wme_state *wme = &ic->ic_wme; 871 const paramType *pPhyParam, *pBssPhyParam; 872 struct wmeParams *wmep; 873 enum ieee80211_phymode mode; 874 int i; 875 876 if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1) 877 return; 878 879 /* 880 * Select mode; we can be called early in which case we 881 * always use auto mode. We know we'll be called when 882 * entering the RUN state with bsschan setup properly 883 * so state will eventually get set correctly 884 */ 885 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC) 886 mode = ieee80211_chan2mode(ic->ic_bsschan); 887 else 888 mode = IEEE80211_MODE_AUTO; 889 for (i = 0; i < WME_NUM_AC; i++) { 890 switch (i) { 891 case WME_AC_BK: 892 pPhyParam = &phyParamForAC_BK[mode]; 893 pBssPhyParam = &phyParamForAC_BK[mode]; 894 break; 895 case WME_AC_VI: 896 pPhyParam = &phyParamForAC_VI[mode]; 897 pBssPhyParam = &bssPhyParamForAC_VI[mode]; 898 break; 899 case WME_AC_VO: 900 pPhyParam = &phyParamForAC_VO[mode]; 901 pBssPhyParam = &bssPhyParamForAC_VO[mode]; 902 break; 903 case WME_AC_BE: 904 default: 905 pPhyParam = &phyParamForAC_BE[mode]; 906 pBssPhyParam = &bssPhyParamForAC_BE[mode]; 907 break; 908 } 909 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i]; 910 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 911 setwmeparams(vap, "chan", i, wmep, pPhyParam); 912 } else { 913 setwmeparams(vap, "chan", i, wmep, pBssPhyParam); 914 } 915 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i]; 916 setwmeparams(vap, "bss ", i, wmep, pBssPhyParam); 917 } 918 /* NB: check ic_bss to avoid NULL deref on initial attach */ 919 if (vap->iv_bss != NULL) { 920 /* 921 * Calculate agressive mode switching threshold based 922 * on beacon interval. This doesn't need locking since 923 * we're only called before entering the RUN state at 924 * which point we start sending beacon frames. 925 */ 926 wme->wme_hipri_switch_thresh = 927 (HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100; 928 wme->wme_flags &= ~WME_F_AGGRMODE; 929 ieee80211_wme_updateparams(vap); 930 } 931 } 932 933 void 934 ieee80211_wme_initparams(struct ieee80211vap *vap) 935 { 936 struct ieee80211com *ic = vap->iv_ic; 937 938 ic = vap->iv_ic; 939 ieee80211_wme_initparams_locked(vap); 940 } 941 942 /* 943 * Update WME parameters for ourself and the BSS. 944 */ 945 void 946 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap) 947 { 948 static const paramType aggrParam[IEEE80211_MODE_MAX] = { 949 [IEEE80211_MODE_AUTO] = { 2, 4, 10, 64, 0 }, 950 [IEEE80211_MODE_11A] = { 2, 4, 10, 64, 0 }, 951 [IEEE80211_MODE_11B] = { 2, 5, 10, 64, 0 }, 952 [IEEE80211_MODE_11G] = { 2, 4, 10, 64, 0 }, 953 [IEEE80211_MODE_FH] = { 2, 5, 10, 64, 0 }, 954 [IEEE80211_MODE_TURBO_A] = { 1, 3, 10, 64, 0 }, 955 [IEEE80211_MODE_TURBO_G] = { 1, 3, 10, 64, 0 }, 956 [IEEE80211_MODE_STURBO_A] = { 1, 3, 10, 64, 0 }, 957 [IEEE80211_MODE_HALF] = { 2, 4, 10, 64, 0 }, 958 [IEEE80211_MODE_QUARTER] = { 2, 4, 10, 64, 0 }, 959 [IEEE80211_MODE_11NA] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/ 960 [IEEE80211_MODE_11NG] = { 2, 4, 10, 64, 0 }, /* XXXcheck*/ 961 }; 962 struct ieee80211com *ic = vap->iv_ic; 963 struct ieee80211_wme_state *wme = &ic->ic_wme; 964 const struct wmeParams *wmep; 965 struct wmeParams *chanp, *bssp; 966 enum ieee80211_phymode mode; 967 int i; 968 969 /* 970 * Set up the channel access parameters for the physical 971 * device. First populate the configured settings. 972 */ 973 for (i = 0; i < WME_NUM_AC; i++) { 974 chanp = &wme->wme_chanParams.cap_wmeParams[i]; 975 wmep = &wme->wme_wmeChanParams.cap_wmeParams[i]; 976 chanp->wmep_aifsn = wmep->wmep_aifsn; 977 chanp->wmep_logcwmin = wmep->wmep_logcwmin; 978 chanp->wmep_logcwmax = wmep->wmep_logcwmax; 979 chanp->wmep_txopLimit = wmep->wmep_txopLimit; 980 981 chanp = &wme->wme_bssChanParams.cap_wmeParams[i]; 982 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i]; 983 chanp->wmep_aifsn = wmep->wmep_aifsn; 984 chanp->wmep_logcwmin = wmep->wmep_logcwmin; 985 chanp->wmep_logcwmax = wmep->wmep_logcwmax; 986 chanp->wmep_txopLimit = wmep->wmep_txopLimit; 987 } 988 989 /* 990 * Select mode; we can be called early in which case we 991 * always use auto mode. We know we'll be called when 992 * entering the RUN state with bsschan setup properly 993 * so state will eventually get set correctly 994 */ 995 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC) 996 mode = ieee80211_chan2mode(ic->ic_bsschan); 997 else 998 mode = IEEE80211_MODE_AUTO; 999 1000 /* 1001 * This implements agressive mode as found in certain 1002 * vendors' AP's. When there is significant high 1003 * priority (VI/VO) traffic in the BSS throttle back BE 1004 * traffic by using conservative parameters. Otherwise 1005 * BE uses agressive params to optimize performance of 1006 * legacy/non-QoS traffic. 1007 */ 1008 if ((vap->iv_opmode == IEEE80211_M_HOSTAP && 1009 (wme->wme_flags & WME_F_AGGRMODE) != 0) || 1010 (vap->iv_opmode == IEEE80211_M_STA && 1011 (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0) || 1012 (vap->iv_flags & IEEE80211_F_WME) == 0) { 1013 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE]; 1014 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE]; 1015 1016 chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn; 1017 chanp->wmep_logcwmin = bssp->wmep_logcwmin = 1018 aggrParam[mode].logcwmin; 1019 chanp->wmep_logcwmax = bssp->wmep_logcwmax = 1020 aggrParam[mode].logcwmax; 1021 chanp->wmep_txopLimit = bssp->wmep_txopLimit = 1022 (vap->iv_flags & IEEE80211_F_BURST) ? 1023 aggrParam[mode].txopLimit : 0; 1024 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 1025 "update %s (chan+bss) [acm %u aifsn %u logcwmin %u " 1026 "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE], 1027 chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin, 1028 chanp->wmep_logcwmax, chanp->wmep_txopLimit); 1029 } 1030 1031 if (vap->iv_opmode == IEEE80211_M_HOSTAP && 1032 ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) { 1033 static const uint8_t logCwMin[IEEE80211_MODE_MAX] = { 1034 [IEEE80211_MODE_AUTO] = 3, 1035 [IEEE80211_MODE_11A] = 3, 1036 [IEEE80211_MODE_11B] = 4, 1037 [IEEE80211_MODE_11G] = 3, 1038 [IEEE80211_MODE_FH] = 4, 1039 [IEEE80211_MODE_TURBO_A] = 3, 1040 [IEEE80211_MODE_TURBO_G] = 3, 1041 [IEEE80211_MODE_STURBO_A] = 3, 1042 [IEEE80211_MODE_HALF] = 3, 1043 [IEEE80211_MODE_QUARTER] = 3, 1044 [IEEE80211_MODE_11NA] = 3, 1045 [IEEE80211_MODE_11NG] = 3, 1046 }; 1047 chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE]; 1048 bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE]; 1049 1050 chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode]; 1051 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 1052 "update %s (chan+bss) logcwmin %u\n", 1053 ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin); 1054 } 1055 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { /* XXX ibss? */ 1056 /* 1057 * Arrange for a beacon update and bump the parameter 1058 * set number so associated stations load the new values. 1059 */ 1060 wme->wme_bssChanParams.cap_info = 1061 (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT; 1062 ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME); 1063 } 1064 1065 wme->wme_update(ic); 1066 1067 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 1068 "%s: WME params updated, cap_info 0x%x\n", __func__, 1069 vap->iv_opmode == IEEE80211_M_STA ? 1070 wme->wme_wmeChanParams.cap_info : 1071 wme->wme_bssChanParams.cap_info); 1072 } 1073 1074 void 1075 ieee80211_wme_updateparams(struct ieee80211vap *vap) 1076 { 1077 struct ieee80211com *ic = vap->iv_ic; 1078 1079 if (ic->ic_caps & IEEE80211_C_WME) { 1080 ieee80211_wme_updateparams_locked(vap); 1081 } 1082 } 1083 1084 static void 1085 parent_updown_task(void *arg, int npending) 1086 { 1087 struct ifnet *parent = arg; 1088 1089 wlan_serialize_enter(); 1090 parent->if_ioctl(parent, SIOCSIFFLAGS, NULL, curthread->td_ucred); 1091 wlan_serialize_exit(); 1092 } 1093 1094 static void 1095 update_mcast_task(void *arg, int npending) 1096 { 1097 struct ieee80211com *ic = arg; 1098 struct ifnet *parent = ic->ic_ifp; 1099 1100 wlan_serialize_enter(); 1101 ic->ic_update_mcast(parent); 1102 wlan_serialize_exit(); 1103 } 1104 1105 static void 1106 update_promisc_task(void *arg, int npending) 1107 { 1108 struct ieee80211com *ic = arg; 1109 struct ifnet *parent = ic->ic_ifp; 1110 1111 wlan_serialize_enter(); 1112 ic->ic_update_promisc(parent); 1113 wlan_serialize_exit(); 1114 } 1115 1116 static void 1117 update_channel_task(void *arg, int npending) 1118 { 1119 struct ieee80211com *ic = arg; 1120 1121 wlan_serialize_enter(); 1122 ic->ic_set_channel(ic); 1123 ieee80211_radiotap_chan_change(ic); 1124 wlan_serialize_exit(); 1125 } 1126 1127 /* 1128 * Block until the parent is in a known state. This is 1129 * used after any operations that dispatch a task (e.g. 1130 * to auto-configure the parent device up/down). 1131 */ 1132 void 1133 ieee80211_waitfor_parent(struct ieee80211com *ic) 1134 { 1135 wlan_assert_serialized(); 1136 wlan_serialize_exit(); /* exit to block */ 1137 taskqueue_block(ic->ic_tq); 1138 ieee80211_draintask(ic, &ic->ic_parent_task); 1139 ieee80211_draintask(ic, &ic->ic_mcast_task); 1140 ieee80211_draintask(ic, &ic->ic_promisc_task); 1141 ieee80211_draintask(ic, &ic->ic_chan_task); 1142 ieee80211_draintask(ic, &ic->ic_bmiss_task); 1143 taskqueue_unblock(ic->ic_tq); 1144 wlan_serialize_enter(); /* then re-enter */ 1145 } 1146 1147 /* 1148 * Start a vap running. If this is the first vap to be 1149 * set running on the underlying device then we 1150 * automatically bring the device up. 1151 */ 1152 void 1153 ieee80211_start_locked(struct ieee80211vap *vap) 1154 { 1155 struct ifnet *ifp = vap->iv_ifp; 1156 struct ieee80211com *ic = vap->iv_ic; 1157 struct ifnet *parent = ic->ic_ifp; 1158 1159 IEEE80211_DPRINTF(vap, 1160 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 1161 "start running, %d vaps running\n", ic->ic_nrunning); 1162 1163 if ((ifp->if_flags & IFF_RUNNING) == 0) { 1164 /* 1165 * Mark us running. Note that it's ok to do this first; 1166 * if we need to bring the parent device up we defer that 1167 * to avoid dropping the com lock. We expect the device 1168 * to respond to being marked up by calling back into us 1169 * through ieee80211_start_all at which point we'll come 1170 * back in here and complete the work. 1171 */ 1172 ifp->if_flags |= IFF_RUNNING; 1173 /* 1174 * We are not running; if this we are the first vap 1175 * to be brought up auto-up the parent if necessary. 1176 */ 1177 if (ic->ic_nrunning++ == 0 && 1178 (parent->if_flags & IFF_RUNNING) == 0) { 1179 IEEE80211_DPRINTF(vap, 1180 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 1181 "%s: up parent %s\n", __func__, parent->if_xname); 1182 parent->if_flags |= IFF_UP; 1183 ieee80211_runtask(ic, &ic->ic_parent_task); 1184 return; 1185 } 1186 } 1187 /* 1188 * If the parent is up and running, then kick the 1189 * 802.11 state machine as appropriate. 1190 */ 1191 if ((parent->if_flags & IFF_RUNNING) && 1192 vap->iv_roaming != IEEE80211_ROAMING_MANUAL) { 1193 if (vap->iv_opmode == IEEE80211_M_STA) { 1194 #if 0 1195 /* XXX bypasses scan too easily; disable for now */ 1196 /* 1197 * Try to be intelligent about clocking the state 1198 * machine. If we're currently in RUN state then 1199 * we should be able to apply any new state/parameters 1200 * simply by re-associating. Otherwise we need to 1201 * re-scan to select an appropriate ap. 1202 */ 1203 if (vap->iv_state >= IEEE80211_S_RUN) 1204 ieee80211_new_state_locked(vap, 1205 IEEE80211_S_ASSOC, 1); 1206 else 1207 #endif 1208 ieee80211_new_state_locked(vap, 1209 IEEE80211_S_SCAN, 0); 1210 } else { 1211 /* 1212 * For monitor+wds mode there's nothing to do but 1213 * start running. Otherwise if this is the first 1214 * vap to be brought up, start a scan which may be 1215 * preempted if the station is locked to a particular 1216 * channel. 1217 */ 1218 vap->iv_flags_ext |= IEEE80211_FEXT_REINIT; 1219 if (vap->iv_opmode == IEEE80211_M_MONITOR || 1220 vap->iv_opmode == IEEE80211_M_WDS) 1221 ieee80211_new_state_locked(vap, 1222 IEEE80211_S_RUN, -1); 1223 else 1224 ieee80211_new_state_locked(vap, 1225 IEEE80211_S_SCAN, 0); 1226 } 1227 } 1228 } 1229 1230 /* 1231 * Start a single vap. 1232 */ 1233 void 1234 ieee80211_init(void *arg) 1235 { 1236 struct ieee80211vap *vap = arg; 1237 1238 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 1239 "%s\n", __func__); 1240 1241 ieee80211_start_locked(vap); 1242 } 1243 1244 /* 1245 * Start all runnable vap's on a device. 1246 */ 1247 void 1248 ieee80211_start_all(struct ieee80211com *ic) 1249 { 1250 struct ieee80211vap *vap; 1251 1252 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 1253 struct ifnet *ifp = vap->iv_ifp; 1254 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */ 1255 ieee80211_start_locked(vap); 1256 } 1257 } 1258 1259 /* 1260 * Stop a vap. We force it down using the state machine 1261 * then mark it's ifnet not running. If this is the last 1262 * vap running on the underlying device then we close it 1263 * too to insure it will be properly initialized when the 1264 * next vap is brought up. 1265 */ 1266 void 1267 ieee80211_stop_locked(struct ieee80211vap *vap) 1268 { 1269 struct ieee80211com *ic = vap->iv_ic; 1270 struct ifnet *ifp = vap->iv_ifp; 1271 struct ifnet *parent = ic->ic_ifp; 1272 1273 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 1274 "stop running, %d vaps running\n", ic->ic_nrunning); 1275 1276 ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1); 1277 if (ifp->if_flags & IFF_RUNNING) { 1278 ifp->if_flags &= ~IFF_RUNNING; /* mark us stopped */ 1279 if (--ic->ic_nrunning == 0 && 1280 (parent->if_flags & IFF_RUNNING)) { 1281 IEEE80211_DPRINTF(vap, 1282 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 1283 "down parent %s\n", parent->if_xname); 1284 parent->if_flags &= ~IFF_UP; 1285 ieee80211_runtask(ic, &ic->ic_parent_task); 1286 } 1287 } 1288 } 1289 1290 void 1291 ieee80211_stop(struct ieee80211vap *vap) 1292 { 1293 struct ieee80211com *ic = vap->iv_ic; 1294 1295 ic = vap->iv_ic; 1296 ieee80211_stop_locked(vap); 1297 } 1298 1299 /* 1300 * Stop all vap's running on a device. 1301 */ 1302 void 1303 ieee80211_stop_all(struct ieee80211com *ic) 1304 { 1305 struct ieee80211vap *vap; 1306 1307 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 1308 struct ifnet *ifp = vap->iv_ifp; 1309 if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */ 1310 ieee80211_stop_locked(vap); 1311 } 1312 1313 ieee80211_waitfor_parent(ic); 1314 } 1315 1316 /* 1317 * Stop all vap's running on a device and arrange 1318 * for those that were running to be resumed. 1319 */ 1320 void 1321 ieee80211_suspend_all(struct ieee80211com *ic) 1322 { 1323 struct ieee80211vap *vap; 1324 1325 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 1326 struct ifnet *ifp = vap->iv_ifp; 1327 if (IFNET_IS_UP_RUNNING(ifp)) { /* NB: avoid recursion */ 1328 vap->iv_flags_ext |= IEEE80211_FEXT_RESUME; 1329 ieee80211_stop_locked(vap); 1330 } 1331 } 1332 1333 ieee80211_waitfor_parent(ic); 1334 } 1335 1336 /* 1337 * Start all vap's marked for resume. 1338 */ 1339 void 1340 ieee80211_resume_all(struct ieee80211com *ic) 1341 { 1342 struct ieee80211vap *vap; 1343 1344 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 1345 struct ifnet *ifp = vap->iv_ifp; 1346 if (!IFNET_IS_UP_RUNNING(ifp) && 1347 (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) { 1348 vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME; 1349 ieee80211_start_locked(vap); 1350 } 1351 } 1352 } 1353 1354 void 1355 ieee80211_beacon_miss(struct ieee80211com *ic) 1356 { 1357 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 1358 /* Process in a taskq, the handler may reenter the driver */ 1359 ieee80211_runtask(ic, &ic->ic_bmiss_task); 1360 } 1361 } 1362 1363 static void 1364 beacon_miss_task(void *arg, int npending) 1365 { 1366 struct ieee80211com *ic = arg; 1367 struct ieee80211vap *vap; 1368 1369 wlan_serialize_enter(); 1370 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 1371 /* 1372 * We only pass events through for sta vap's in RUN state; 1373 * may be too restrictive but for now this saves all the 1374 * handlers duplicating these checks. 1375 */ 1376 if (vap->iv_opmode == IEEE80211_M_STA && 1377 vap->iv_state >= IEEE80211_S_RUN && 1378 vap->iv_bmiss != NULL) 1379 vap->iv_bmiss(vap); 1380 } 1381 wlan_serialize_exit(); 1382 } 1383 1384 static void 1385 beacon_swmiss_task(void *arg, int npending) 1386 { 1387 struct ieee80211vap *vap = arg; 1388 1389 wlan_serialize_enter(); 1390 if (vap->iv_state == IEEE80211_S_RUN) { 1391 /* XXX Call multiple times if npending > zero? */ 1392 vap->iv_bmiss(vap); 1393 } 1394 wlan_serialize_exit(); 1395 } 1396 1397 /* 1398 * Software beacon miss handling. Check if any beacons 1399 * were received in the last period. If not post a 1400 * beacon miss; otherwise reset the counter. 1401 */ 1402 void 1403 ieee80211_swbmiss_callout(void *arg) 1404 { 1405 struct ieee80211vap *vap = arg; 1406 struct ieee80211com *ic = vap->iv_ic; 1407 1408 wlan_serialize_enter(); 1409 KASSERT(vap->iv_state == IEEE80211_S_RUN, 1410 ("wrong state %d", vap->iv_state)); 1411 1412 if (ic->ic_flags & IEEE80211_F_SCAN) { 1413 /* 1414 * If scanning just ignore and reset state. If we get a 1415 * bmiss after coming out of scan because we haven't had 1416 * time to receive a beacon then we should probe the AP 1417 * before posting a real bmiss (unless iv_bmiss_max has 1418 * been artifiically lowered). A cleaner solution might 1419 * be to disable the timer on scan start/end but to handle 1420 * case of multiple sta vap's we'd need to disable the 1421 * timers of all affected vap's. 1422 */ 1423 vap->iv_swbmiss_count = 0; 1424 } else if (vap->iv_swbmiss_count == 0) { 1425 if (vap->iv_bmiss != NULL) 1426 ieee80211_runtask(ic, &vap->iv_swbmiss_task); 1427 if (vap->iv_bmiss_count == 0) /* don't re-arm timer */ 1428 goto done; 1429 } else { 1430 vap->iv_swbmiss_count = 0; 1431 } 1432 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period, 1433 ieee80211_swbmiss_callout, vap); 1434 done: 1435 wlan_serialize_exit(); 1436 } 1437 1438 /* 1439 * Start an 802.11h channel switch. We record the parameters, 1440 * mark the operation pending, notify each vap through the 1441 * beacon update mechanism so it can update the beacon frame 1442 * contents, and then switch vap's to CSA state to block outbound 1443 * traffic. Devices that handle CSA directly can use the state 1444 * switch to do the right thing so long as they call 1445 * ieee80211_csa_completeswitch when it's time to complete the 1446 * channel change. Devices that depend on the net80211 layer can 1447 * use ieee80211_beacon_update to handle the countdown and the 1448 * channel switch. 1449 */ 1450 void 1451 ieee80211_csa_startswitch(struct ieee80211com *ic, 1452 struct ieee80211_channel *c, int mode, int count) 1453 { 1454 struct ieee80211vap *vap; 1455 1456 ic->ic_csa_newchan = c; 1457 ic->ic_csa_mode = mode; 1458 ic->ic_csa_count = count; 1459 ic->ic_flags |= IEEE80211_F_CSAPENDING; 1460 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 1461 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 1462 vap->iv_opmode == IEEE80211_M_IBSS || 1463 vap->iv_opmode == IEEE80211_M_MBSS) 1464 ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA); 1465 /* switch to CSA state to block outbound traffic */ 1466 if (vap->iv_state == IEEE80211_S_RUN) 1467 ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0); 1468 } 1469 ieee80211_notify_csa(ic, c, mode, count); 1470 } 1471 1472 static void 1473 csa_completeswitch(struct ieee80211com *ic) 1474 { 1475 struct ieee80211vap *vap; 1476 1477 ic->ic_csa_newchan = NULL; 1478 ic->ic_flags &= ~IEEE80211_F_CSAPENDING; 1479 1480 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 1481 if (vap->iv_state == IEEE80211_S_CSA) 1482 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0); 1483 } 1484 1485 /* 1486 * Complete an 802.11h channel switch started by ieee80211_csa_startswitch. 1487 * We clear state and move all vap's in CSA state to RUN state 1488 * so they can again transmit. 1489 */ 1490 void 1491 ieee80211_csa_completeswitch(struct ieee80211com *ic) 1492 { 1493 KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending")); 1494 1495 ieee80211_setcurchan(ic, ic->ic_csa_newchan); 1496 csa_completeswitch(ic); 1497 } 1498 1499 /* 1500 * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch. 1501 * We clear state and move all vap's in CSA state to RUN state 1502 * so they can again transmit. 1503 */ 1504 void 1505 ieee80211_csa_cancelswitch(struct ieee80211com *ic) 1506 { 1507 csa_completeswitch(ic); 1508 } 1509 1510 /* 1511 * Complete a DFS CAC started by ieee80211_dfs_cac_start. 1512 * We clear state and move all vap's in CAC state to RUN state. 1513 */ 1514 void 1515 ieee80211_cac_completeswitch(struct ieee80211vap *vap0) 1516 { 1517 struct ieee80211com *ic = vap0->iv_ic; 1518 struct ieee80211vap *vap; 1519 1520 /* 1521 * Complete CAC state change for lead vap first; then 1522 * clock all the other vap's waiting. 1523 */ 1524 KASSERT(vap0->iv_state == IEEE80211_S_CAC, 1525 ("wrong state %d", vap0->iv_state)); 1526 ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0); 1527 1528 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 1529 if (vap->iv_state == IEEE80211_S_CAC) 1530 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0); 1531 } 1532 1533 /* 1534 * Force all vap's other than the specified vap to the INIT state 1535 * and mark them as waiting for a scan to complete. These vaps 1536 * will be brought up when the scan completes and the scanning vap 1537 * reaches RUN state by wakeupwaiting. 1538 */ 1539 static void 1540 markwaiting(struct ieee80211vap *vap0) 1541 { 1542 struct ieee80211com *ic = vap0->iv_ic; 1543 struct ieee80211vap *vap; 1544 1545 /* 1546 * A vap list entry can not disappear since we are running on the 1547 * taskqueue and a vap destroy will queue and drain another state 1548 * change task. 1549 */ 1550 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 1551 if (vap == vap0) 1552 continue; 1553 if (vap->iv_state != IEEE80211_S_INIT) { 1554 /* NB: iv_newstate may drop the lock */ 1555 vap->iv_newstate(vap, IEEE80211_S_INIT, 0); 1556 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT; 1557 } 1558 } 1559 } 1560 1561 /* 1562 * Wakeup all vap's waiting for a scan to complete. This is the 1563 * companion to markwaiting (above) and is used to coordinate 1564 * multiple vaps scanning. 1565 * This is called from the state taskqueue. 1566 */ 1567 static void 1568 wakeupwaiting(struct ieee80211vap *vap0) 1569 { 1570 struct ieee80211com *ic = vap0->iv_ic; 1571 struct ieee80211vap *vap; 1572 1573 /* 1574 * A vap list entry can not disappear since we are running on the 1575 * taskqueue and a vap destroy will queue and drain another state 1576 * change task. 1577 */ 1578 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 1579 if (vap == vap0) 1580 continue; 1581 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) { 1582 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT; 1583 /* NB: sta's cannot go INIT->RUN */ 1584 /* NB: iv_newstate may drop the lock */ 1585 vap->iv_newstate(vap, 1586 vap->iv_opmode == IEEE80211_M_STA ? 1587 IEEE80211_S_SCAN : IEEE80211_S_RUN, 0); 1588 } 1589 } 1590 } 1591 1592 /* 1593 * Handle post state change work common to all operating modes. 1594 */ 1595 static void 1596 ieee80211_newstate_task(void *xvap, int npending) 1597 { 1598 struct ieee80211vap *vap = xvap; 1599 struct ieee80211com *ic; 1600 enum ieee80211_state nstate, ostate; 1601 int arg, rc; 1602 1603 wlan_serialize_enter(); 1604 1605 ic = vap->iv_ic; 1606 nstate = vap->iv_nstate; 1607 arg = vap->iv_nstate_arg; 1608 1609 if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) { 1610 /* 1611 * We have been requested to drop back to the INIT before 1612 * proceeding to the new state. 1613 */ 1614 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 1615 "%s: %s -> %s arg %d\n", __func__, 1616 ieee80211_state_name[vap->iv_state], 1617 ieee80211_state_name[IEEE80211_S_INIT], arg); 1618 vap->iv_newstate(vap, IEEE80211_S_INIT, arg); 1619 vap->iv_flags_ext &= ~IEEE80211_FEXT_REINIT; 1620 } 1621 1622 ostate = vap->iv_state; 1623 if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) { 1624 /* 1625 * SCAN was forced; e.g. on beacon miss. Force other running 1626 * vap's to INIT state and mark them as waiting for the scan to 1627 * complete. This insures they don't interfere with our 1628 * scanning. Since we are single threaded the vaps can not 1629 * transition again while we are executing. 1630 * 1631 * XXX not always right, assumes ap follows sta 1632 */ 1633 markwaiting(vap); 1634 } 1635 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 1636 "%s: %s -> %s arg %d\n", __func__, 1637 ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg); 1638 1639 rc = vap->iv_newstate(vap, nstate, arg); 1640 vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT; 1641 if (rc != 0) { 1642 /* State transition failed */ 1643 KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred")); 1644 KASSERT(nstate != IEEE80211_S_INIT, 1645 ("INIT state change failed")); 1646 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 1647 "%s: %s returned error %d\n", __func__, 1648 ieee80211_state_name[nstate], rc); 1649 goto done; 1650 } 1651 1652 /* No actual transition, skip post processing */ 1653 if (ostate == nstate) 1654 goto done; 1655 1656 if (nstate == IEEE80211_S_RUN) { 1657 /* 1658 * OACTIVE may be set on the vap if the upper layer 1659 * tried to transmit (e.g. IPv6 NDP) before we reach 1660 * RUN state. Clear it and restart xmit. 1661 * 1662 * Note this can also happen as a result of SLEEP->RUN 1663 * (i.e. coming out of power save mode). 1664 */ 1665 vap->iv_ifp->if_flags &= ~IFF_OACTIVE; 1666 vap->iv_ifp->if_start(vap->iv_ifp); 1667 1668 /* bring up any vaps waiting on us */ 1669 wakeupwaiting(vap); 1670 } else if (nstate == IEEE80211_S_INIT) { 1671 /* 1672 * Flush the scan cache if we did the last scan (XXX?) 1673 * and flush any frames on send queues from this vap. 1674 * Note the mgt q is used only for legacy drivers and 1675 * will go away shortly. 1676 */ 1677 ieee80211_scan_flush(vap); 1678 1679 /* XXX NB: cast for altq */ 1680 ieee80211_flush_ifq((struct ifqueue *)&ic->ic_ifp->if_snd, vap); 1681 } 1682 done: 1683 wlan_serialize_exit(); 1684 } 1685 1686 /* 1687 * Public interface for initiating a state machine change. 1688 * This routine single-threads the request and coordinates 1689 * the scheduling of multiple vaps for the purpose of selecting 1690 * an operating channel. Specifically the following scenarios 1691 * are handled: 1692 * o only one vap can be selecting a channel so on transition to 1693 * SCAN state if another vap is already scanning then 1694 * mark the caller for later processing and return without 1695 * doing anything (XXX? expectations by caller of synchronous operation) 1696 * o only one vap can be doing CAC of a channel so on transition to 1697 * CAC state if another vap is already scanning for radar then 1698 * mark the caller for later processing and return without 1699 * doing anything (XXX? expectations by caller of synchronous operation) 1700 * o if another vap is already running when a request is made 1701 * to SCAN then an operating channel has been chosen; bypass 1702 * the scan and just join the channel 1703 * 1704 * Note that the state change call is done through the iv_newstate 1705 * method pointer so any driver routine gets invoked. The driver 1706 * will normally call back into operating mode-specific 1707 * ieee80211_newstate routines (below) unless it needs to completely 1708 * bypass the state machine (e.g. because the firmware has it's 1709 * own idea how things should work). Bypassing the net80211 layer 1710 * is usually a mistake and indicates lack of proper integration 1711 * with the net80211 layer. 1712 */ 1713 static int 1714 ieee80211_new_state_locked(struct ieee80211vap *vap, 1715 enum ieee80211_state nstate, int arg) 1716 { 1717 struct ieee80211com *ic = vap->iv_ic; 1718 struct ieee80211vap *vp; 1719 enum ieee80211_state ostate; 1720 int nrunning, nscanning; 1721 1722 if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) { 1723 if (vap->iv_nstate == IEEE80211_S_INIT) { 1724 /* 1725 * XXX The vap is being stopped, do no allow any other 1726 * state changes until this is completed. 1727 */ 1728 return -1; 1729 } else if (vap->iv_state != vap->iv_nstate) { 1730 #if 0 1731 /* Warn if the previous state hasn't completed. */ 1732 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 1733 "%s: pending %s -> %s transition lost\n", __func__, 1734 ieee80211_state_name[vap->iv_state], 1735 ieee80211_state_name[vap->iv_nstate]); 1736 #else 1737 /* XXX temporarily enable to identify issues */ 1738 if_printf(vap->iv_ifp, 1739 "%s: pending %s -> %s transition lost\n", 1740 __func__, ieee80211_state_name[vap->iv_state], 1741 ieee80211_state_name[vap->iv_nstate]); 1742 #endif 1743 } 1744 } 1745 1746 nrunning = nscanning = 0; 1747 /* XXX can track this state instead of calculating */ 1748 TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) { 1749 if (vp != vap) { 1750 if (vp->iv_state >= IEEE80211_S_RUN) 1751 nrunning++; 1752 /* XXX doesn't handle bg scan */ 1753 /* NB: CAC+AUTH+ASSOC treated like SCAN */ 1754 else if (vp->iv_state > IEEE80211_S_INIT) 1755 nscanning++; 1756 } 1757 } 1758 ostate = vap->iv_state; 1759 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 1760 "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__, 1761 ieee80211_state_name[ostate], ieee80211_state_name[nstate], 1762 nrunning, nscanning); 1763 switch (nstate) { 1764 case IEEE80211_S_SCAN: 1765 if (ostate == IEEE80211_S_INIT) { 1766 /* 1767 * INIT -> SCAN happens on initial bringup. 1768 */ 1769 KASSERT(!(nscanning && nrunning), 1770 ("%d scanning and %d running", nscanning, nrunning)); 1771 if (nscanning) { 1772 /* 1773 * Someone is scanning, defer our state 1774 * change until the work has completed. 1775 */ 1776 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 1777 "%s: defer %s -> %s\n", 1778 __func__, ieee80211_state_name[ostate], 1779 ieee80211_state_name[nstate]); 1780 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT; 1781 return 0; 1782 } 1783 if (nrunning) { 1784 /* 1785 * Someone is operating; just join the channel 1786 * they have chosen. 1787 */ 1788 /* XXX kill arg? */ 1789 /* XXX check each opmode, adhoc? */ 1790 if (vap->iv_opmode == IEEE80211_M_STA) 1791 nstate = IEEE80211_S_SCAN; 1792 else 1793 nstate = IEEE80211_S_RUN; 1794 #ifdef IEEE80211_DEBUG 1795 if (nstate != IEEE80211_S_SCAN) { 1796 IEEE80211_DPRINTF(vap, 1797 IEEE80211_MSG_STATE, 1798 "%s: override, now %s -> %s\n", 1799 __func__, 1800 ieee80211_state_name[ostate], 1801 ieee80211_state_name[nstate]); 1802 } 1803 #endif 1804 } 1805 } 1806 break; 1807 case IEEE80211_S_RUN: 1808 if (vap->iv_opmode == IEEE80211_M_WDS && 1809 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) && 1810 nscanning) { 1811 /* 1812 * Legacy WDS with someone else scanning; don't 1813 * go online until that completes as we should 1814 * follow the other vap to the channel they choose. 1815 */ 1816 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 1817 "%s: defer %s -> %s (legacy WDS)\n", __func__, 1818 ieee80211_state_name[ostate], 1819 ieee80211_state_name[nstate]); 1820 vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT; 1821 return 0; 1822 } 1823 if (vap->iv_opmode == IEEE80211_M_HOSTAP && 1824 IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) && 1825 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) && 1826 !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) { 1827 /* 1828 * This is a DFS channel, transition to CAC state 1829 * instead of RUN. This allows us to initiate 1830 * Channel Availability Check (CAC) as specified 1831 * by 11h/DFS. 1832 */ 1833 nstate = IEEE80211_S_CAC; 1834 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 1835 "%s: override %s -> %s (DFS)\n", __func__, 1836 ieee80211_state_name[ostate], 1837 ieee80211_state_name[nstate]); 1838 } 1839 break; 1840 case IEEE80211_S_INIT: 1841 /* cancel any scan in progress */ 1842 ieee80211_cancel_scan(vap); 1843 if (ostate == IEEE80211_S_INIT ) { 1844 /* XXX don't believe this */ 1845 /* INIT -> INIT. nothing to do */ 1846 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT; 1847 } 1848 /* fall thru... */ 1849 default: 1850 break; 1851 } 1852 /* defer the state change to a thread */ 1853 vap->iv_nstate = nstate; 1854 vap->iv_nstate_arg = arg; 1855 vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT; 1856 ieee80211_runtask(ic, &vap->iv_nstate_task); 1857 return EINPROGRESS; 1858 } 1859 1860 int 1861 ieee80211_new_state(struct ieee80211vap *vap, 1862 enum ieee80211_state nstate, int arg) 1863 { 1864 struct ieee80211com *ic = vap->iv_ic; 1865 int rc; 1866 1867 ic = vap->iv_ic; 1868 rc = ieee80211_new_state_locked(vap, nstate, arg); 1869 return rc; 1870 } 1871