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