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