1 /* $OpenBSD: ieee80211_proto.c,v 1.101 2020/12/09 15:50:58 stsp Exp $ */ 2 /* $NetBSD: ieee80211_proto.c,v 1.8 2004/04/30 23:58:20 dyoung Exp $ */ 3 4 /*- 5 * Copyright (c) 2001 Atsushi Onoe 6 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 7 * Copyright (c) 2008, 2009 Damien Bergamini 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * IEEE 802.11 protocol support. 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/mbuf.h> 40 #include <sys/kernel.h> 41 #include <sys/socket.h> 42 #include <sys/sockio.h> 43 #include <sys/endian.h> 44 #include <sys/errno.h> 45 #include <sys/sysctl.h> 46 47 #include <net/if.h> 48 #include <net/if_dl.h> 49 #include <net/if_media.h> 50 #include <net/if_llc.h> 51 #include <net/route.h> 52 53 #include <netinet/in.h> 54 #include <netinet/if_ether.h> 55 56 #include <net80211/ieee80211_var.h> 57 #include <net80211/ieee80211_priv.h> 58 59 const char * const ieee80211_mgt_subtype_name[] = { 60 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp", 61 "probe_req", "probe_resp", "reserved#6", "reserved#7", 62 "beacon", "atim", "disassoc", "auth", 63 "deauth", "action", "action_noack", "reserved#15" 64 }; 65 const char * const ieee80211_state_name[IEEE80211_S_MAX] = { 66 "INIT", /* IEEE80211_S_INIT */ 67 "SCAN", /* IEEE80211_S_SCAN */ 68 "AUTH", /* IEEE80211_S_AUTH */ 69 "ASSOC", /* IEEE80211_S_ASSOC */ 70 "RUN" /* IEEE80211_S_RUN */ 71 }; 72 const char * const ieee80211_phymode_name[] = { 73 "auto", /* IEEE80211_MODE_AUTO */ 74 "11a", /* IEEE80211_MODE_11A */ 75 "11b", /* IEEE80211_MODE_11B */ 76 "11g", /* IEEE80211_MODE_11G */ 77 "11n", /* IEEE80211_MODE_11N */ 78 }; 79 80 void ieee80211_set_beacon_miss_threshold(struct ieee80211com *); 81 int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int); 82 83 void 84 ieee80211_proto_attach(struct ifnet *ifp) 85 { 86 struct ieee80211com *ic = (void *)ifp; 87 88 mq_init(&ic->ic_mgtq, IFQ_MAXLEN, IPL_NET); 89 mq_init(&ic->ic_pwrsaveq, IFQ_MAXLEN, IPL_NET); 90 91 ifp->if_hdrlen = sizeof(struct ieee80211_frame); 92 93 ic->ic_rtsthreshold = IEEE80211_RTS_MAX; 94 ic->ic_fragthreshold = 2346; /* XXX not used yet */ 95 ic->ic_fixed_rate = -1; /* no fixed rate */ 96 ic->ic_fixed_mcs = -1; /* no fixed mcs */ 97 ic->ic_protmode = IEEE80211_PROT_CTSONLY; 98 99 /* protocol state change handler */ 100 ic->ic_newstate = ieee80211_newstate; 101 102 /* initialize management frame handlers */ 103 ic->ic_recv_mgmt = ieee80211_recv_mgmt; 104 ic->ic_send_mgmt = ieee80211_send_mgmt; 105 } 106 107 void 108 ieee80211_proto_detach(struct ifnet *ifp) 109 { 110 struct ieee80211com *ic = (void *)ifp; 111 112 mq_purge(&ic->ic_mgtq); 113 mq_purge(&ic->ic_pwrsaveq); 114 } 115 116 void 117 ieee80211_print_essid(const u_int8_t *essid, int len) 118 { 119 int i; 120 const u_int8_t *p; 121 122 if (len > IEEE80211_NWID_LEN) 123 len = IEEE80211_NWID_LEN; 124 /* determine printable or not */ 125 for (i = 0, p = essid; i < len; i++, p++) { 126 if (*p < ' ' || *p > 0x7e) 127 break; 128 } 129 if (i == len) { 130 printf("\""); 131 for (i = 0, p = essid; i < len; i++, p++) 132 printf("%c", *p); 133 printf("\""); 134 } else { 135 printf("0x"); 136 for (i = 0, p = essid; i < len; i++, p++) 137 printf("%02x", *p); 138 } 139 } 140 141 #ifdef IEEE80211_DEBUG 142 void 143 ieee80211_dump_pkt(const u_int8_t *buf, int len, int rate, int rssi) 144 { 145 struct ieee80211_frame *wh; 146 int i; 147 148 wh = (struct ieee80211_frame *)buf; 149 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 150 case IEEE80211_FC1_DIR_NODS: 151 printf("NODS %s", ether_sprintf(wh->i_addr2)); 152 printf("->%s", ether_sprintf(wh->i_addr1)); 153 printf("(%s)", ether_sprintf(wh->i_addr3)); 154 break; 155 case IEEE80211_FC1_DIR_TODS: 156 printf("TODS %s", ether_sprintf(wh->i_addr2)); 157 printf("->%s", ether_sprintf(wh->i_addr3)); 158 printf("(%s)", ether_sprintf(wh->i_addr1)); 159 break; 160 case IEEE80211_FC1_DIR_FROMDS: 161 printf("FRDS %s", ether_sprintf(wh->i_addr3)); 162 printf("->%s", ether_sprintf(wh->i_addr1)); 163 printf("(%s)", ether_sprintf(wh->i_addr2)); 164 break; 165 case IEEE80211_FC1_DIR_DSTODS: 166 printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1])); 167 printf("->%s", ether_sprintf(wh->i_addr3)); 168 printf("(%s", ether_sprintf(wh->i_addr2)); 169 printf("->%s)", ether_sprintf(wh->i_addr1)); 170 break; 171 } 172 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 173 case IEEE80211_FC0_TYPE_DATA: 174 printf(" data"); 175 break; 176 case IEEE80211_FC0_TYPE_MGT: 177 printf(" %s", ieee80211_mgt_subtype_name[ 178 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) 179 >> IEEE80211_FC0_SUBTYPE_SHIFT]); 180 break; 181 default: 182 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK); 183 break; 184 } 185 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 186 printf(" WEP"); 187 if (rate >= 0) 188 printf(" %d%sM", rate / 2, (rate & 1) ? ".5" : ""); 189 if (rssi >= 0) 190 printf(" +%d", rssi); 191 printf("\n"); 192 if (len > 0) { 193 for (i = 0; i < len; i++) { 194 if ((i & 1) == 0) 195 printf(" "); 196 printf("%02x", buf[i]); 197 } 198 printf("\n"); 199 } 200 } 201 #endif 202 203 int 204 ieee80211_fix_rate(struct ieee80211com *ic, struct ieee80211_node *ni, 205 int flags) 206 { 207 #define RV(v) ((v) & IEEE80211_RATE_VAL) 208 int i, j, ignore, error; 209 int okrate, badrate, fixedrate; 210 const struct ieee80211_rateset *srs; 211 struct ieee80211_rateset *nrs; 212 u_int8_t r; 213 214 /* 215 * If the fixed rate check was requested but no fixed rate has been 216 * defined then just remove the check. 217 */ 218 if ((flags & IEEE80211_F_DOFRATE) && ic->ic_fixed_rate == -1) 219 flags &= ~IEEE80211_F_DOFRATE; 220 221 error = 0; 222 okrate = badrate = fixedrate = 0; 223 srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)]; 224 nrs = &ni->ni_rates; 225 for (i = 0; i < nrs->rs_nrates; ) { 226 ignore = 0; 227 if (flags & IEEE80211_F_DOSORT) { 228 /* 229 * Sort rates. 230 */ 231 for (j = i + 1; j < nrs->rs_nrates; j++) { 232 if (RV(nrs->rs_rates[i]) > 233 RV(nrs->rs_rates[j])) { 234 r = nrs->rs_rates[i]; 235 nrs->rs_rates[i] = nrs->rs_rates[j]; 236 nrs->rs_rates[j] = r; 237 } 238 } 239 } 240 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL; 241 badrate = r; 242 if (flags & IEEE80211_F_DOFRATE) { 243 /* 244 * Check fixed rate is included. 245 */ 246 if (r == RV(srs->rs_rates[ic->ic_fixed_rate])) 247 fixedrate = r; 248 } 249 if (flags & IEEE80211_F_DONEGO) { 250 /* 251 * Check against supported rates. 252 */ 253 for (j = 0; j < srs->rs_nrates; j++) { 254 if (r == RV(srs->rs_rates[j])) { 255 /* 256 * Overwrite with the supported rate 257 * value so any basic rate bit is set. 258 * This insures that response we send 259 * to stations have the necessary basic 260 * rate bit set. 261 */ 262 nrs->rs_rates[i] = srs->rs_rates[j]; 263 break; 264 } 265 } 266 if (j == srs->rs_nrates) { 267 /* 268 * A rate in the node's rate set is not 269 * supported. If this is a basic rate and we 270 * are operating as an AP then this is an error. 271 * Otherwise we just discard/ignore the rate. 272 * Note that this is important for 11b stations 273 * when they want to associate with an 11g AP. 274 */ 275 #ifndef IEEE80211_STA_ONLY 276 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 277 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC)) 278 error++; 279 #endif 280 ignore++; 281 } 282 } 283 if (flags & IEEE80211_F_DODEL) { 284 /* 285 * Delete unacceptable rates. 286 */ 287 if (ignore) { 288 nrs->rs_nrates--; 289 for (j = i; j < nrs->rs_nrates; j++) 290 nrs->rs_rates[j] = nrs->rs_rates[j + 1]; 291 nrs->rs_rates[j] = 0; 292 continue; 293 } 294 } 295 if (!ignore) 296 okrate = nrs->rs_rates[i]; 297 i++; 298 } 299 if (okrate == 0 || error != 0 || 300 ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0)) 301 return badrate | IEEE80211_RATE_BASIC; 302 else 303 return RV(okrate); 304 #undef RV 305 } 306 307 /* 308 * Reset 11g-related state. 309 */ 310 void 311 ieee80211_reset_erp(struct ieee80211com *ic) 312 { 313 ic->ic_flags &= ~IEEE80211_F_USEPROT; 314 315 ieee80211_set_shortslottime(ic, 316 ic->ic_curmode == IEEE80211_MODE_11A || 317 (ic->ic_curmode == IEEE80211_MODE_11N && 318 IEEE80211_IS_CHAN_5GHZ(ic->ic_ibss_chan)) 319 #ifndef IEEE80211_STA_ONLY 320 || 321 ((ic->ic_curmode == IEEE80211_MODE_11G || 322 (ic->ic_curmode == IEEE80211_MODE_11N && 323 IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan))) && 324 ic->ic_opmode == IEEE80211_M_HOSTAP && 325 (ic->ic_caps & IEEE80211_C_SHSLOT)) 326 #endif 327 ); 328 329 if (ic->ic_curmode == IEEE80211_MODE_11A || 330 (ic->ic_curmode == IEEE80211_MODE_11N && 331 IEEE80211_IS_CHAN_5GHZ(ic->ic_ibss_chan)) || 332 (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) 333 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 334 else 335 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 336 } 337 338 /* 339 * Set the short slot time state and notify the driver. 340 */ 341 void 342 ieee80211_set_shortslottime(struct ieee80211com *ic, int on) 343 { 344 if (on) 345 ic->ic_flags |= IEEE80211_F_SHSLOT; 346 else 347 ic->ic_flags &= ~IEEE80211_F_SHSLOT; 348 349 /* notify the driver */ 350 if (ic->ic_updateslot != NULL) 351 ic->ic_updateslot(ic); 352 } 353 354 /* 355 * This function is called by the 802.1X PACP machine (via an ioctl) when 356 * the transmit key machine (4-Way Handshake for 802.11) should run. 357 */ 358 int 359 ieee80211_keyrun(struct ieee80211com *ic, u_int8_t *macaddr) 360 { 361 struct ieee80211_node *ni = ic->ic_bss; 362 #ifndef IEEE80211_STA_ONLY 363 struct ieee80211_pmk *pmk; 364 #endif 365 366 /* STA must be associated or AP must be ready */ 367 if (ic->ic_state != IEEE80211_S_RUN || 368 !(ic->ic_flags & IEEE80211_F_RSNON)) 369 return ENETDOWN; 370 371 ni->ni_rsn_supp_state = RSNA_SUPP_PTKSTART; 372 #ifndef IEEE80211_STA_ONLY 373 if (ic->ic_opmode == IEEE80211_M_STA) 374 #endif 375 return 0; /* supplicant only, do nothing */ 376 377 #ifndef IEEE80211_STA_ONLY 378 /* find the STA with which we must start the key exchange */ 379 if ((ni = ieee80211_find_node(ic, macaddr)) == NULL) { 380 DPRINTF(("no node found for %s\n", ether_sprintf(macaddr))); 381 return EINVAL; 382 } 383 /* check that the STA is in the correct state */ 384 if (ni->ni_state != IEEE80211_STA_ASSOC || 385 ni->ni_rsn_state != RSNA_AUTHENTICATION_2) { 386 DPRINTF(("unexpected in state %d\n", ni->ni_rsn_state)); 387 return EINVAL; 388 } 389 ni->ni_rsn_state = RSNA_INITPMK; 390 391 /* make sure a PMK is available for this STA, otherwise deauth it */ 392 if ((pmk = ieee80211_pmksa_find(ic, ni, NULL)) == NULL) { 393 DPRINTF(("no PMK available for %s\n", ether_sprintf(macaddr))); 394 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH, 395 IEEE80211_REASON_AUTH_LEAVE); 396 ieee80211_node_leave(ic, ni); 397 return EINVAL; 398 } 399 memcpy(ni->ni_pmk, pmk->pmk_key, IEEE80211_PMK_LEN); 400 memcpy(ni->ni_pmkid, pmk->pmk_pmkid, IEEE80211_PMKID_LEN); 401 ni->ni_flags |= IEEE80211_NODE_PMK; 402 403 /* initiate key exchange (4-Way Handshake) with STA */ 404 return ieee80211_send_4way_msg1(ic, ni); 405 #endif /* IEEE80211_STA_ONLY */ 406 } 407 408 #ifndef IEEE80211_STA_ONLY 409 /* 410 * Initiate a group key handshake with a node. 411 */ 412 static void 413 ieee80211_node_gtk_rekey(void *arg, struct ieee80211_node *ni) 414 { 415 struct ieee80211com *ic = arg; 416 417 if (ni->ni_state != IEEE80211_STA_ASSOC || 418 ni->ni_rsn_gstate != RSNA_IDLE) 419 return; 420 421 /* initiate a group key handshake with STA */ 422 ni->ni_flags |= IEEE80211_NODE_REKEY; 423 if (ieee80211_send_group_msg1(ic, ni) != 0) 424 ni->ni_flags &= ~IEEE80211_NODE_REKEY; 425 } 426 427 /* 428 * This function is called in HostAP mode when the group key needs to be 429 * changed. 430 */ 431 void 432 ieee80211_setkeys(struct ieee80211com *ic) 433 { 434 struct ieee80211_key *k; 435 u_int8_t kid; 436 int rekeysta = 0; 437 438 /* Swap(GM, GN) */ 439 kid = (ic->ic_def_txkey == 1) ? 2 : 1; 440 k = &ic->ic_nw_keys[kid]; 441 memset(k, 0, sizeof(*k)); 442 k->k_id = kid; 443 k->k_cipher = ic->ic_bss->ni_rsngroupcipher; 444 k->k_flags = IEEE80211_KEY_GROUP | IEEE80211_KEY_TX; 445 k->k_len = ieee80211_cipher_keylen(k->k_cipher); 446 arc4random_buf(k->k_key, k->k_len); 447 448 if (ic->ic_caps & IEEE80211_C_MFP) { 449 /* Swap(GM_igtk, GN_igtk) */ 450 kid = (ic->ic_igtk_kid == 4) ? 5 : 4; 451 k = &ic->ic_nw_keys[kid]; 452 memset(k, 0, sizeof(*k)); 453 k->k_id = kid; 454 k->k_cipher = ic->ic_bss->ni_rsngroupmgmtcipher; 455 k->k_flags = IEEE80211_KEY_IGTK | IEEE80211_KEY_TX; 456 k->k_len = 16; 457 arc4random_buf(k->k_key, k->k_len); 458 } 459 460 ieee80211_iterate_nodes(ic, ieee80211_node_gtk_rekey, ic); 461 ieee80211_iterate_nodes(ic, ieee80211_count_rekeysta, &rekeysta); 462 if (rekeysta == 0) 463 ieee80211_setkeysdone(ic); 464 } 465 466 /* 467 * The group key handshake has been completed with all associated stations. 468 */ 469 void 470 ieee80211_setkeysdone(struct ieee80211com *ic) 471 { 472 u_int8_t kid; 473 474 /* 475 * Discard frames buffered for power-saving which were encrypted with 476 * the old group key. Clients are no longer able to decrypt them. 477 */ 478 mq_purge(&ic->ic_bss->ni_savedq); 479 480 /* install GTK */ 481 kid = (ic->ic_def_txkey == 1) ? 2 : 1; 482 switch ((*ic->ic_set_key)(ic, ic->ic_bss, &ic->ic_nw_keys[kid])) { 483 case 0: 484 case EBUSY: 485 ic->ic_def_txkey = kid; 486 break; 487 default: 488 break; 489 } 490 491 if (ic->ic_caps & IEEE80211_C_MFP) { 492 /* install IGTK */ 493 kid = (ic->ic_igtk_kid == 4) ? 5 : 4; 494 switch ((*ic->ic_set_key)(ic, ic->ic_bss, &ic->ic_nw_keys[kid])) { 495 case 0: 496 case EBUSY: 497 ic->ic_igtk_kid = kid; 498 break; 499 default: 500 break; 501 } 502 } 503 } 504 505 /* 506 * Group key lifetime has expired, update it. 507 */ 508 void 509 ieee80211_gtk_rekey_timeout(void *arg) 510 { 511 struct ieee80211com *ic = arg; 512 int s; 513 514 s = splnet(); 515 ieee80211_setkeys(ic); 516 splx(s); 517 518 /* re-schedule a GTK rekeying after 3600s */ 519 timeout_add_sec(&ic->ic_rsn_timeout, 3600); 520 } 521 522 void 523 ieee80211_sa_query_timeout(void *arg) 524 { 525 struct ieee80211_node *ni = arg; 526 struct ieee80211com *ic = ni->ni_ic; 527 int s; 528 529 s = splnet(); 530 if (++ni->ni_sa_query_count >= 3) { 531 ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY; 532 ni->ni_flags |= IEEE80211_NODE_SA_QUERY_FAILED; 533 } else /* retry SA Query Request */ 534 ieee80211_sa_query_request(ic, ni); 535 splx(s); 536 } 537 538 /* 539 * Request that a SA Query Request frame be sent to a specified peer STA 540 * to which the STA is associated. 541 */ 542 void 543 ieee80211_sa_query_request(struct ieee80211com *ic, struct ieee80211_node *ni) 544 { 545 /* MLME-SAQuery.request */ 546 547 if (!(ni->ni_flags & IEEE80211_NODE_SA_QUERY)) { 548 ni->ni_flags |= IEEE80211_NODE_SA_QUERY; 549 ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY_FAILED; 550 ni->ni_sa_query_count = 0; 551 } 552 /* generate new Transaction Identifier */ 553 ni->ni_sa_query_trid++; 554 555 /* send SA Query Request */ 556 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_SA_QUERY, 557 IEEE80211_ACTION_SA_QUERY_REQ, 0); 558 timeout_add_msec(&ni->ni_sa_query_to, 10); 559 } 560 #endif /* IEEE80211_STA_ONLY */ 561 562 void 563 ieee80211_ht_negotiate(struct ieee80211com *ic, struct ieee80211_node *ni) 564 { 565 int i; 566 567 ni->ni_flags &= ~(IEEE80211_NODE_HT | IEEE80211_NODE_HT_SGI20 | 568 IEEE80211_NODE_HT_SGI40); 569 570 /* Check if we support HT. */ 571 if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) == 0) 572 return; 573 574 /* Check if HT support has been explicitly disabled. */ 575 if ((ic->ic_flags & IEEE80211_F_HTON) == 0) 576 return; 577 578 /* 579 * Check if the peer supports HT. 580 * Require at least one of the mandatory MCS. 581 * MCS 0-7 are mandatory but some APs have particular MCS disabled. 582 */ 583 if (!ieee80211_node_supports_ht(ni)) { 584 ic->ic_stats.is_ht_nego_no_mandatory_mcs++; 585 return; 586 } 587 588 if (ic->ic_opmode == IEEE80211_M_STA) { 589 /* We must support the AP's basic MCS set. */ 590 for (i = 0; i < IEEE80211_HT_NUM_MCS; i++) { 591 if (isset(ni->ni_basic_mcs, i) && 592 !isset(ic->ic_sup_mcs, i)) { 593 ic->ic_stats.is_ht_nego_no_basic_mcs++; 594 return; 595 } 596 } 597 } 598 599 /* 600 * Don't allow group cipher (includes WEP) or TKIP 601 * for pairwise encryption (see 802.11-2012 11.1.6). 602 */ 603 if (ic->ic_flags & IEEE80211_F_WEPON) { 604 ic->ic_stats.is_ht_nego_bad_crypto++; 605 return; 606 } 607 if ((ic->ic_flags & IEEE80211_F_RSNON) && 608 (ni->ni_rsnciphers & IEEE80211_CIPHER_USEGROUP || 609 ni->ni_rsnciphers & IEEE80211_CIPHER_TKIP)) { 610 ic->ic_stats.is_ht_nego_bad_crypto++; 611 return; 612 } 613 614 ni->ni_flags |= IEEE80211_NODE_HT; 615 616 /* Flags IEEE8021_NODE_HT_SGI20/40 are set by drivers if supported. */ 617 } 618 619 void 620 ieee80211_tx_ba_timeout(void *arg) 621 { 622 struct ieee80211_tx_ba *ba = arg; 623 struct ieee80211_node *ni = ba->ba_ni; 624 struct ieee80211com *ic = ni->ni_ic; 625 u_int8_t tid; 626 int s; 627 628 s = splnet(); 629 tid = ((caddr_t)ba - (caddr_t)ni->ni_tx_ba) / sizeof(*ba); 630 if (ba->ba_state == IEEE80211_BA_REQUESTED) { 631 /* MLME-ADDBA.confirm(TIMEOUT) */ 632 ba->ba_state = IEEE80211_BA_INIT; 633 if (ni->ni_addba_req_intval[tid] < 634 IEEE80211_ADDBA_REQ_INTVAL_MAX) 635 ni->ni_addba_req_intval[tid]++; 636 /* 637 * In case the peer believes there is an existing 638 * block ack agreement with us, try to delete it. 639 */ 640 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA, 641 IEEE80211_ACTION_DELBA, 642 IEEE80211_REASON_SETUP_REQUIRED << 16 | 1 << 8 | tid); 643 } else if (ba->ba_state == IEEE80211_BA_AGREED) { 644 /* Block Ack inactivity timeout */ 645 ic->ic_stats.is_ht_tx_ba_timeout++; 646 ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT, 647 1, tid); 648 } 649 splx(s); 650 } 651 652 void 653 ieee80211_rx_ba_timeout(void *arg) 654 { 655 struct ieee80211_rx_ba *ba = arg; 656 struct ieee80211_node *ni = ba->ba_ni; 657 struct ieee80211com *ic = ni->ni_ic; 658 u_int8_t tid; 659 int s; 660 661 ic->ic_stats.is_ht_rx_ba_timeout++; 662 663 s = splnet(); 664 665 /* Block Ack inactivity timeout */ 666 tid = ((caddr_t)ba - (caddr_t)ni->ni_rx_ba) / sizeof(*ba); 667 ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT, 0, tid); 668 669 splx(s); 670 } 671 672 /* 673 * Request initiation of Block Ack with the specified peer. 674 */ 675 int 676 ieee80211_addba_request(struct ieee80211com *ic, struct ieee80211_node *ni, 677 u_int16_t ssn, u_int8_t tid) 678 { 679 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 680 681 if (ba->ba_state != IEEE80211_BA_INIT) 682 return EBUSY; 683 684 /* MLME-ADDBA.request */ 685 686 /* setup Block Ack */ 687 ba->ba_ni = ni; 688 ba->ba_state = IEEE80211_BA_REQUESTED; 689 ba->ba_token = ic->ic_dialog_token++; 690 ba->ba_timeout_val = 0; 691 timeout_set(&ba->ba_to, ieee80211_tx_ba_timeout, ba); 692 ba->ba_winsize = IEEE80211_BA_MAX_WINSZ; 693 ba->ba_winstart = ssn; 694 ba->ba_winend = (ba->ba_winstart + ba->ba_winsize - 1) & 0xfff; 695 ba->ba_params = 696 (ba->ba_winsize << IEEE80211_ADDBA_BUFSZ_SHIFT) | 697 (tid << IEEE80211_ADDBA_TID_SHIFT); 698 #if 0 699 /* iwm(4) 9k and iwx(4) need more work before AMSDU can be enabled. */ 700 ba->ba_params |= IEEE80211_ADDBA_AMSDU; 701 #endif 702 if ((ic->ic_htcaps & IEEE80211_HTCAP_DELAYEDBA) == 0) 703 /* immediate BA */ 704 ba->ba_params |= IEEE80211_ADDBA_BA_POLICY; 705 706 timeout_add_sec(&ba->ba_to, 1); /* dot11ADDBAResponseTimeout */ 707 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA, 708 IEEE80211_ACTION_ADDBA_REQ, tid); 709 return 0; 710 } 711 712 /* 713 * Request the deletion of Block Ack with a peer and notify driver. 714 */ 715 void 716 ieee80211_delba_request(struct ieee80211com *ic, struct ieee80211_node *ni, 717 u_int16_t reason, u_int8_t dir, u_int8_t tid) 718 { 719 /* MLME-DELBA.request */ 720 721 if (reason) { 722 /* transmit a DELBA frame */ 723 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA, 724 IEEE80211_ACTION_DELBA, reason << 16 | dir << 8 | tid); 725 } 726 if (dir) { 727 /* MLME-DELBA.confirm(Originator) */ 728 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 729 730 if (ic->ic_ampdu_tx_stop != NULL) 731 ic->ic_ampdu_tx_stop(ic, ni, tid); 732 733 ba->ba_state = IEEE80211_BA_INIT; 734 /* stop Block Ack inactivity timer */ 735 timeout_del(&ba->ba_to); 736 } else { 737 /* MLME-DELBA.confirm(Recipient) */ 738 struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid]; 739 int i; 740 741 if (ic->ic_ampdu_rx_stop != NULL) 742 ic->ic_ampdu_rx_stop(ic, ni, tid); 743 744 ba->ba_state = IEEE80211_BA_INIT; 745 /* stop Block Ack inactivity timer */ 746 timeout_del(&ba->ba_to); 747 timeout_del(&ba->ba_gap_to); 748 749 if (ba->ba_buf != NULL) { 750 /* free all MSDUs stored in reordering buffer */ 751 for (i = 0; i < IEEE80211_BA_MAX_WINSZ; i++) 752 m_freem(ba->ba_buf[i].m); 753 /* free reordering buffer */ 754 free(ba->ba_buf, M_DEVBUF, 755 IEEE80211_BA_MAX_WINSZ * sizeof(*ba->ba_buf)); 756 ba->ba_buf = NULL; 757 } 758 } 759 } 760 761 #ifndef IEEE80211_STA_ONLY 762 void 763 ieee80211_auth_open_confirm(struct ieee80211com *ic, 764 struct ieee80211_node *ni, uint16_t seq) 765 { 766 struct ifnet *ifp = &ic->ic_if; 767 768 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1); 769 if (ifp->if_flags & IFF_DEBUG) 770 printf("%s: station %s %s authenticated (open)\n", 771 ifp->if_xname, 772 ether_sprintf((u_int8_t *)ni->ni_macaddr), 773 ni->ni_state != IEEE80211_STA_CACHE ? 774 "newly" : "already"); 775 ieee80211_node_newstate(ni, IEEE80211_STA_AUTH); 776 } 777 #endif 778 779 void 780 ieee80211_try_another_bss(struct ieee80211com *ic) 781 { 782 struct ieee80211_node *curbs, *selbs; 783 struct ifnet *ifp = &ic->ic_if; 784 785 /* Don't select our current AP again. */ 786 curbs = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 787 if (curbs) { 788 curbs->ni_fails++; 789 ieee80211_node_newstate(curbs, IEEE80211_STA_CACHE); 790 } 791 792 /* Try a different AP from the same ESS if available. */ 793 if (ic->ic_caps & IEEE80211_C_SCANALLBAND) { 794 /* 795 * Make sure we will consider APs on all bands during 796 * access point selection in ieee80211_node_choose_bss(). 797 * During multi-band scans, our previous AP may be trying 798 * to steer us onto another band by denying authentication. 799 */ 800 ieee80211_setmode(ic, IEEE80211_MODE_AUTO); 801 } 802 selbs = ieee80211_node_choose_bss(ic, 0, NULL); 803 if (selbs == NULL) 804 return; 805 806 /* Should not happen but seriously, don't try the same AP again. */ 807 if (memcmp(selbs->ni_macaddr, ic->ic_bss->ni_macaddr, 808 IEEE80211_NWID_LEN) == 0) 809 return; 810 811 if (ifp->if_flags & IFF_DEBUG) 812 printf("%s: trying AP %s on channel %d instead\n", 813 ifp->if_xname, ether_sprintf(selbs->ni_macaddr), 814 ieee80211_chan2ieee(ic, selbs->ni_chan)); 815 816 /* Triggers an AUTH->AUTH transition, avoiding another SCAN. */ 817 ieee80211_node_join_bss(ic, selbs); 818 } 819 820 void 821 ieee80211_auth_open(struct ieee80211com *ic, const struct ieee80211_frame *wh, 822 struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi, u_int16_t seq, 823 u_int16_t status) 824 { 825 struct ifnet *ifp = &ic->ic_if; 826 switch (ic->ic_opmode) { 827 #ifndef IEEE80211_STA_ONLY 828 case IEEE80211_M_IBSS: 829 if (ic->ic_state != IEEE80211_S_RUN || 830 seq != IEEE80211_AUTH_OPEN_REQUEST) { 831 DPRINTF(("discard auth from %s; state %u, seq %u\n", 832 ether_sprintf((u_int8_t *)wh->i_addr2), 833 ic->ic_state, seq)); 834 ic->ic_stats.is_rx_bad_auth++; 835 return; 836 } 837 ieee80211_new_state(ic, IEEE80211_S_AUTH, 838 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 839 840 /* In IBSS mode no (re)association frames are sent. */ 841 if (ic->ic_flags & IEEE80211_F_RSNON) 842 ni->ni_rsn_supp_state = RSNA_SUPP_PTKSTART; 843 break; 844 845 case IEEE80211_M_AHDEMO: 846 /* should not come here */ 847 break; 848 849 case IEEE80211_M_HOSTAP: 850 if (ic->ic_state != IEEE80211_S_RUN || 851 seq != IEEE80211_AUTH_OPEN_REQUEST) { 852 DPRINTF(("discard auth from %s; state %u, seq %u\n", 853 ether_sprintf((u_int8_t *)wh->i_addr2), 854 ic->ic_state, seq)); 855 ic->ic_stats.is_rx_bad_auth++; 856 return; 857 } 858 if (ni == ic->ic_bss) { 859 ni = ieee80211_find_node(ic, wh->i_addr2); 860 if (ni == NULL) 861 ni = ieee80211_alloc_node(ic, wh->i_addr2); 862 if (ni == NULL) { 863 return; 864 } 865 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 866 ni->ni_rssi = rxi->rxi_rssi; 867 ni->ni_rstamp = rxi->rxi_tstamp; 868 ni->ni_chan = ic->ic_bss->ni_chan; 869 } 870 871 /* 872 * Drivers may want to set up state before confirming. 873 * In which case this returns EBUSY and the driver will 874 * later call ieee80211_auth_open_confirm() by itself. 875 */ 876 if (ic->ic_newauth && ic->ic_newauth(ic, ni, 877 ni->ni_state != IEEE80211_STA_CACHE, seq) != 0) 878 break; 879 ieee80211_auth_open_confirm(ic, ni, seq); 880 break; 881 #endif /* IEEE80211_STA_ONLY */ 882 883 case IEEE80211_M_STA: 884 if (ic->ic_state != IEEE80211_S_AUTH || 885 seq != IEEE80211_AUTH_OPEN_RESPONSE) { 886 ic->ic_stats.is_rx_bad_auth++; 887 DPRINTF(("discard auth from %s; state %u, seq %u\n", 888 ether_sprintf((u_int8_t *)wh->i_addr2), 889 ic->ic_state, seq)); 890 return; 891 } 892 if (ic->ic_flags & IEEE80211_F_RSNON) { 893 /* XXX not here! */ 894 ic->ic_bss->ni_flags &= ~IEEE80211_NODE_TXRXPROT; 895 ic->ic_bss->ni_port_valid = 0; 896 ic->ic_bss->ni_replaycnt_ok = 0; 897 (*ic->ic_delete_key)(ic, ic->ic_bss, 898 &ic->ic_bss->ni_pairwise_key); 899 } 900 if (status != 0) { 901 if (ifp->if_flags & IFF_DEBUG) 902 printf("%s: open authentication failed " 903 "(status %d) for %s\n", ifp->if_xname, 904 status, 905 ether_sprintf((u_int8_t *)wh->i_addr3)); 906 if (ni != ic->ic_bss) 907 ni->ni_fails++; 908 else 909 ieee80211_try_another_bss(ic); 910 ic->ic_stats.is_rx_auth_fail++; 911 return; 912 } 913 ieee80211_new_state(ic, IEEE80211_S_ASSOC, 914 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 915 break; 916 default: 917 break; 918 } 919 } 920 921 void 922 ieee80211_set_beacon_miss_threshold(struct ieee80211com *ic) 923 { 924 struct ifnet *ifp = &ic->ic_if; 925 926 /* 927 * Scale the missed beacon counter threshold to the AP's actual 928 * beacon interval. 929 */ 930 int btimeout = MIN(IEEE80211_BEACON_MISS_THRES * ic->ic_bss->ni_intval, 931 IEEE80211_BEACON_MISS_THRES * (IEEE80211_DUR_TU / 10)); 932 /* Ensure that at least one beacon may be missed. */ 933 btimeout = MAX(btimeout, 2 * ic->ic_bss->ni_intval); 934 if (ic->ic_bss->ni_intval > 0) /* don't crash if interval is bogus */ 935 ic->ic_bmissthres = btimeout / ic->ic_bss->ni_intval; 936 937 if (ifp->if_flags & IFF_DEBUG) 938 printf("%s: missed beacon threshold set to %d beacons, " 939 "beacon interval is %u TU\n", ifp->if_xname, 940 ic->ic_bmissthres, ic->ic_bss->ni_intval); 941 } 942 943 /* Tell our peer, and the driver, to stop A-MPDU Tx for all TIDs. */ 944 void 945 ieee80211_stop_ampdu_tx(struct ieee80211com *ic, struct ieee80211_node *ni, 946 int mgt) 947 { 948 int tid; 949 950 for (tid = 0; tid < nitems(ni->ni_tx_ba); tid++) { 951 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 952 if (ba->ba_state != IEEE80211_BA_AGREED) 953 continue; 954 ieee80211_delba_request(ic, ni, 955 mgt == -1 ? 0 : IEEE80211_REASON_AUTH_LEAVE, 1, tid); 956 } 957 } 958 959 void 960 ieee80211_check_wpa_supplicant_failure(struct ieee80211com *ic, 961 struct ieee80211_node *ni) 962 { 963 struct ieee80211_node *ni2; 964 965 if (ic->ic_opmode != IEEE80211_M_STA 966 #ifndef IEEE80211_STA_ONLY 967 && ic->ic_opmode != IEEE80211_M_IBSS 968 #endif 969 ) 970 return; 971 972 if (ni->ni_rsn_supp_state != RSNA_SUPP_PTKNEGOTIATING) 973 return; 974 975 ni->ni_assoc_fail |= IEEE80211_NODE_ASSOCFAIL_WPA_KEY; 976 977 if (ni != ic->ic_bss) 978 return; 979 980 /* Also update the copy of our AP's node in the node cache. */ 981 ni2 = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 982 if (ni2) 983 ni2->ni_assoc_fail |= ic->ic_bss->ni_assoc_fail; 984 } 985 986 int 987 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, 988 int mgt) 989 { 990 struct ifnet *ifp = &ic->ic_if; 991 struct ieee80211_node *ni; 992 enum ieee80211_state ostate; 993 u_int rate; 994 #ifndef IEEE80211_STA_ONLY 995 int s; 996 #endif 997 998 ostate = ic->ic_state; 999 if (ifp->if_flags & IFF_DEBUG) 1000 printf("%s: %s -> %s\n", ifp->if_xname, 1001 ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 1002 ic->ic_state = nstate; /* state transition */ 1003 ni = ic->ic_bss; /* NB: no reference held */ 1004 ieee80211_set_link_state(ic, LINK_STATE_DOWN); 1005 ic->ic_xflags &= ~IEEE80211_F_TX_MGMT_ONLY; 1006 switch (nstate) { 1007 case IEEE80211_S_INIT: 1008 /* 1009 * If mgt = -1, driver is already partway down, so do 1010 * not send management frames. 1011 */ 1012 switch (ostate) { 1013 case IEEE80211_S_INIT: 1014 break; 1015 case IEEE80211_S_RUN: 1016 if (mgt == -1) 1017 goto justcleanup; 1018 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1019 ieee80211_ba_del(ni); 1020 switch (ic->ic_opmode) { 1021 case IEEE80211_M_STA: 1022 IEEE80211_SEND_MGMT(ic, ni, 1023 IEEE80211_FC0_SUBTYPE_DISASSOC, 1024 IEEE80211_REASON_ASSOC_LEAVE); 1025 break; 1026 #ifndef IEEE80211_STA_ONLY 1027 case IEEE80211_M_HOSTAP: 1028 s = splnet(); 1029 RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) { 1030 if (ni->ni_state != IEEE80211_STA_ASSOC) 1031 continue; 1032 IEEE80211_SEND_MGMT(ic, ni, 1033 IEEE80211_FC0_SUBTYPE_DISASSOC, 1034 IEEE80211_REASON_ASSOC_LEAVE); 1035 } 1036 splx(s); 1037 break; 1038 #endif 1039 default: 1040 break; 1041 } 1042 /* FALLTHROUGH */ 1043 case IEEE80211_S_ASSOC: 1044 if (mgt == -1) 1045 goto justcleanup; 1046 switch (ic->ic_opmode) { 1047 case IEEE80211_M_STA: 1048 IEEE80211_SEND_MGMT(ic, ni, 1049 IEEE80211_FC0_SUBTYPE_DEAUTH, 1050 IEEE80211_REASON_AUTH_LEAVE); 1051 break; 1052 #ifndef IEEE80211_STA_ONLY 1053 case IEEE80211_M_HOSTAP: 1054 s = splnet(); 1055 RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) { 1056 IEEE80211_SEND_MGMT(ic, ni, 1057 IEEE80211_FC0_SUBTYPE_DEAUTH, 1058 IEEE80211_REASON_AUTH_LEAVE); 1059 } 1060 splx(s); 1061 break; 1062 #endif 1063 default: 1064 break; 1065 } 1066 /* FALLTHROUGH */ 1067 case IEEE80211_S_AUTH: 1068 case IEEE80211_S_SCAN: 1069 justcleanup: 1070 #ifndef IEEE80211_STA_ONLY 1071 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1072 timeout_del(&ic->ic_rsn_timeout); 1073 #endif 1074 ieee80211_ba_del(ni); 1075 timeout_del(&ic->ic_bgscan_timeout); 1076 ic->ic_bgscan_fail = 0; 1077 ic->ic_mgt_timer = 0; 1078 mq_purge(&ic->ic_mgtq); 1079 mq_purge(&ic->ic_pwrsaveq); 1080 ieee80211_free_allnodes(ic, 1); 1081 break; 1082 } 1083 ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE; 1084 ni->ni_assoc_fail = 0; 1085 if (ic->ic_flags & IEEE80211_F_RSNON) 1086 ieee80211_crypto_clear_groupkeys(ic); 1087 break; 1088 case IEEE80211_S_SCAN: 1089 ic->ic_flags &= ~IEEE80211_F_SIBSS; 1090 /* initialize bss for probe request */ 1091 IEEE80211_ADDR_COPY(ni->ni_macaddr, etherbroadcastaddr); 1092 IEEE80211_ADDR_COPY(ni->ni_bssid, etherbroadcastaddr); 1093 ni->ni_rates = ic->ic_sup_rates[ 1094 ieee80211_chan2mode(ic, ni->ni_chan)]; 1095 ni->ni_associd = 0; 1096 ni->ni_rstamp = 0; 1097 ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE; 1098 if (ic->ic_flags & IEEE80211_F_RSNON) 1099 ieee80211_crypto_clear_groupkeys(ic); 1100 switch (ostate) { 1101 case IEEE80211_S_INIT: 1102 #ifndef IEEE80211_STA_ONLY 1103 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1104 ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 1105 /* 1106 * AP operation and we already have a channel; 1107 * bypass the scan and startup immediately. 1108 */ 1109 ieee80211_create_ibss(ic, ic->ic_des_chan); 1110 } else 1111 #endif 1112 ieee80211_begin_scan(ifp); 1113 break; 1114 case IEEE80211_S_SCAN: 1115 /* scan next */ 1116 if (ic->ic_flags & IEEE80211_F_ASCAN) { 1117 IEEE80211_SEND_MGMT(ic, ni, 1118 IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0); 1119 } 1120 break; 1121 case IEEE80211_S_RUN: 1122 /* beacon miss */ 1123 if (ifp->if_flags & IFF_DEBUG) { 1124 /* XXX bssid clobbered above */ 1125 printf("%s: no recent beacons from %s;" 1126 " rescanning\n", ifp->if_xname, 1127 ether_sprintf(ic->ic_bss->ni_bssid)); 1128 } 1129 timeout_del(&ic->ic_bgscan_timeout); 1130 ic->ic_bgscan_fail = 0; 1131 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1132 ieee80211_free_allnodes(ic, 1); 1133 /* FALLTHROUGH */ 1134 case IEEE80211_S_AUTH: 1135 case IEEE80211_S_ASSOC: 1136 /* timeout restart scan */ 1137 ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 1138 if (ni != NULL) 1139 ni->ni_fails++; 1140 ieee80211_begin_scan(ifp); 1141 break; 1142 } 1143 break; 1144 case IEEE80211_S_AUTH: 1145 if (ostate == IEEE80211_S_RUN) 1146 ieee80211_check_wpa_supplicant_failure(ic, ni); 1147 ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE; 1148 if (ic->ic_flags & IEEE80211_F_RSNON) 1149 ieee80211_crypto_clear_groupkeys(ic); 1150 switch (ostate) { 1151 case IEEE80211_S_INIT: 1152 if (ifp->if_flags & IFF_DEBUG) 1153 printf("%s: invalid transition %s -> %s\n", 1154 ifp->if_xname, ieee80211_state_name[ostate], 1155 ieee80211_state_name[nstate]); 1156 break; 1157 case IEEE80211_S_SCAN: 1158 IEEE80211_SEND_MGMT(ic, ni, 1159 IEEE80211_FC0_SUBTYPE_AUTH, 1); 1160 break; 1161 case IEEE80211_S_AUTH: 1162 case IEEE80211_S_ASSOC: 1163 switch (mgt) { 1164 case IEEE80211_FC0_SUBTYPE_AUTH: 1165 if (ic->ic_opmode == IEEE80211_M_STA) { 1166 IEEE80211_SEND_MGMT(ic, ni, 1167 IEEE80211_FC0_SUBTYPE_AUTH, 1168 IEEE80211_AUTH_OPEN_REQUEST); 1169 } 1170 break; 1171 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1172 /* ignore and retry scan on timeout */ 1173 break; 1174 } 1175 break; 1176 case IEEE80211_S_RUN: 1177 timeout_del(&ic->ic_bgscan_timeout); 1178 ic->ic_bgscan_fail = 0; 1179 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1180 ieee80211_ba_del(ni); 1181 switch (mgt) { 1182 case IEEE80211_FC0_SUBTYPE_AUTH: 1183 IEEE80211_SEND_MGMT(ic, ni, 1184 IEEE80211_FC0_SUBTYPE_AUTH, 2); 1185 ic->ic_state = ostate; /* stay RUN */ 1186 break; 1187 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1188 /* try to reauth */ 1189 IEEE80211_SEND_MGMT(ic, ni, 1190 IEEE80211_FC0_SUBTYPE_AUTH, 1); 1191 break; 1192 } 1193 break; 1194 } 1195 break; 1196 case IEEE80211_S_ASSOC: 1197 switch (ostate) { 1198 case IEEE80211_S_INIT: 1199 case IEEE80211_S_SCAN: 1200 case IEEE80211_S_ASSOC: 1201 if (ifp->if_flags & IFF_DEBUG) 1202 printf("%s: invalid transition %s -> %s\n", 1203 ifp->if_xname, ieee80211_state_name[ostate], 1204 ieee80211_state_name[nstate]); 1205 break; 1206 case IEEE80211_S_AUTH: 1207 IEEE80211_SEND_MGMT(ic, ni, 1208 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0); 1209 break; 1210 case IEEE80211_S_RUN: 1211 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1212 ieee80211_ba_del(ni); 1213 IEEE80211_SEND_MGMT(ic, ni, 1214 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1); 1215 break; 1216 } 1217 break; 1218 case IEEE80211_S_RUN: 1219 switch (ostate) { 1220 case IEEE80211_S_INIT: 1221 if (ic->ic_opmode == IEEE80211_M_MONITOR) 1222 break; 1223 case IEEE80211_S_AUTH: 1224 case IEEE80211_S_RUN: 1225 if (ifp->if_flags & IFF_DEBUG) 1226 printf("%s: invalid transition %s -> %s\n", 1227 ifp->if_xname, ieee80211_state_name[ostate], 1228 ieee80211_state_name[nstate]); 1229 break; 1230 case IEEE80211_S_SCAN: /* adhoc/hostap mode */ 1231 case IEEE80211_S_ASSOC: /* infra mode */ 1232 if (ni->ni_txrate >= ni->ni_rates.rs_nrates) 1233 panic("%s: bogus xmit rate %u setup", 1234 __func__, ni->ni_txrate); 1235 if (ifp->if_flags & IFF_DEBUG) { 1236 printf("%s: %s with %s ssid ", 1237 ifp->if_xname, 1238 ic->ic_opmode == IEEE80211_M_STA ? 1239 "associated" : "synchronized", 1240 ether_sprintf(ni->ni_bssid)); 1241 ieee80211_print_essid(ic->ic_bss->ni_essid, 1242 ni->ni_esslen); 1243 rate = ni->ni_rates.rs_rates[ni->ni_txrate] & 1244 IEEE80211_RATE_VAL; 1245 printf(" channel %d", 1246 ieee80211_chan2ieee(ic, ni->ni_chan)); 1247 if (ni->ni_flags & IEEE80211_NODE_HT) 1248 printf(" start MCS %u", ni->ni_txmcs); 1249 else 1250 printf(" start %u%sMb", 1251 rate / 2, (rate & 1) ? ".5" : ""); 1252 printf(" %s preamble %s slot time%s%s\n", 1253 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? 1254 "short" : "long", 1255 (ic->ic_flags & IEEE80211_F_SHSLOT) ? 1256 "short" : "long", 1257 (ic->ic_flags & IEEE80211_F_USEPROT) ? 1258 " protection enabled" : "", 1259 (ni->ni_flags & IEEE80211_NODE_HT) ? 1260 " HT enabled" : ""); 1261 } 1262 if (!(ic->ic_flags & IEEE80211_F_RSNON)) { 1263 /* 1264 * NB: When RSN is enabled, we defer setting 1265 * the link up until the port is valid. 1266 */ 1267 ieee80211_set_link_state(ic, LINK_STATE_UP); 1268 ni->ni_assoc_fail = 0; 1269 } 1270 ic->ic_mgt_timer = 0; 1271 ieee80211_set_beacon_miss_threshold(ic); 1272 if_start(ifp); 1273 break; 1274 } 1275 break; 1276 } 1277 return 0; 1278 } 1279 1280 void 1281 ieee80211_set_link_state(struct ieee80211com *ic, int nstate) 1282 { 1283 struct ifnet *ifp = &ic->ic_if; 1284 1285 switch (ic->ic_opmode) { 1286 #ifndef IEEE80211_STA_ONLY 1287 case IEEE80211_M_IBSS: 1288 case IEEE80211_M_HOSTAP: 1289 nstate = LINK_STATE_UNKNOWN; 1290 break; 1291 #endif 1292 case IEEE80211_M_MONITOR: 1293 nstate = LINK_STATE_DOWN; 1294 break; 1295 default: 1296 break; 1297 } 1298 if (nstate != ifp->if_link_state) { 1299 ifp->if_link_state = nstate; 1300 if (LINK_STATE_IS_UP(nstate)) { 1301 struct if_ieee80211_data ifie; 1302 memset(&ifie, 0, sizeof(ifie)); 1303 ifie.ifie_nwid_len = ic->ic_bss->ni_esslen; 1304 memcpy(ifie.ifie_nwid, ic->ic_bss->ni_essid, 1305 sizeof(ifie.ifie_nwid)); 1306 memcpy(ifie.ifie_addr, ic->ic_bss->ni_bssid, 1307 sizeof(ifie.ifie_addr)); 1308 ifie.ifie_channel = ieee80211_chan2ieee(ic, 1309 ic->ic_bss->ni_chan); 1310 ifie.ifie_flags = ic->ic_flags; 1311 ifie.ifie_xflags = ic->ic_xflags; 1312 rtm_80211info(&ic->ic_if, &ifie); 1313 } 1314 if_link_state_change(ifp); 1315 } 1316 } 1317