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