1 /* $OpenBSD: ieee80211_proto.c,v 1.102 2021/04/25 15:32:21 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 ba->ba_params |= IEEE80211_ADDBA_AMSDU; 699 if ((ic->ic_htcaps & IEEE80211_HTCAP_DELAYEDBA) == 0) 700 /* immediate BA */ 701 ba->ba_params |= IEEE80211_ADDBA_BA_POLICY; 702 703 timeout_add_sec(&ba->ba_to, 1); /* dot11ADDBAResponseTimeout */ 704 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA, 705 IEEE80211_ACTION_ADDBA_REQ, tid); 706 return 0; 707 } 708 709 /* 710 * Request the deletion of Block Ack with a peer and notify driver. 711 */ 712 void 713 ieee80211_delba_request(struct ieee80211com *ic, struct ieee80211_node *ni, 714 u_int16_t reason, u_int8_t dir, u_int8_t tid) 715 { 716 /* MLME-DELBA.request */ 717 718 if (reason) { 719 /* transmit a DELBA frame */ 720 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA, 721 IEEE80211_ACTION_DELBA, reason << 16 | dir << 8 | tid); 722 } 723 if (dir) { 724 /* MLME-DELBA.confirm(Originator) */ 725 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 726 727 if (ic->ic_ampdu_tx_stop != NULL) 728 ic->ic_ampdu_tx_stop(ic, ni, tid); 729 730 ba->ba_state = IEEE80211_BA_INIT; 731 /* stop Block Ack inactivity timer */ 732 timeout_del(&ba->ba_to); 733 } else { 734 /* MLME-DELBA.confirm(Recipient) */ 735 struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid]; 736 int i; 737 738 if (ic->ic_ampdu_rx_stop != NULL) 739 ic->ic_ampdu_rx_stop(ic, ni, tid); 740 741 ba->ba_state = IEEE80211_BA_INIT; 742 /* stop Block Ack inactivity timer */ 743 timeout_del(&ba->ba_to); 744 timeout_del(&ba->ba_gap_to); 745 746 if (ba->ba_buf != NULL) { 747 /* free all MSDUs stored in reordering buffer */ 748 for (i = 0; i < IEEE80211_BA_MAX_WINSZ; i++) 749 m_freem(ba->ba_buf[i].m); 750 /* free reordering buffer */ 751 free(ba->ba_buf, M_DEVBUF, 752 IEEE80211_BA_MAX_WINSZ * sizeof(*ba->ba_buf)); 753 ba->ba_buf = NULL; 754 } 755 } 756 } 757 758 #ifndef IEEE80211_STA_ONLY 759 void 760 ieee80211_auth_open_confirm(struct ieee80211com *ic, 761 struct ieee80211_node *ni, uint16_t seq) 762 { 763 struct ifnet *ifp = &ic->ic_if; 764 765 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1); 766 if (ifp->if_flags & IFF_DEBUG) 767 printf("%s: station %s %s authenticated (open)\n", 768 ifp->if_xname, 769 ether_sprintf((u_int8_t *)ni->ni_macaddr), 770 ni->ni_state != IEEE80211_STA_CACHE ? 771 "newly" : "already"); 772 ieee80211_node_newstate(ni, IEEE80211_STA_AUTH); 773 } 774 #endif 775 776 void 777 ieee80211_try_another_bss(struct ieee80211com *ic) 778 { 779 struct ieee80211_node *curbs, *selbs; 780 struct ifnet *ifp = &ic->ic_if; 781 782 /* Don't select our current AP again. */ 783 curbs = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 784 if (curbs) { 785 curbs->ni_fails++; 786 ieee80211_node_newstate(curbs, IEEE80211_STA_CACHE); 787 } 788 789 /* Try a different AP from the same ESS if available. */ 790 if (ic->ic_caps & IEEE80211_C_SCANALLBAND) { 791 /* 792 * Make sure we will consider APs on all bands during 793 * access point selection in ieee80211_node_choose_bss(). 794 * During multi-band scans, our previous AP may be trying 795 * to steer us onto another band by denying authentication. 796 */ 797 ieee80211_setmode(ic, IEEE80211_MODE_AUTO); 798 } 799 selbs = ieee80211_node_choose_bss(ic, 0, NULL); 800 if (selbs == NULL) 801 return; 802 803 /* Should not happen but seriously, don't try the same AP again. */ 804 if (memcmp(selbs->ni_macaddr, ic->ic_bss->ni_macaddr, 805 IEEE80211_NWID_LEN) == 0) 806 return; 807 808 if (ifp->if_flags & IFF_DEBUG) 809 printf("%s: trying AP %s on channel %d instead\n", 810 ifp->if_xname, ether_sprintf(selbs->ni_macaddr), 811 ieee80211_chan2ieee(ic, selbs->ni_chan)); 812 813 /* Triggers an AUTH->AUTH transition, avoiding another SCAN. */ 814 ieee80211_node_join_bss(ic, selbs); 815 } 816 817 void 818 ieee80211_auth_open(struct ieee80211com *ic, const struct ieee80211_frame *wh, 819 struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi, u_int16_t seq, 820 u_int16_t status) 821 { 822 struct ifnet *ifp = &ic->ic_if; 823 switch (ic->ic_opmode) { 824 #ifndef IEEE80211_STA_ONLY 825 case IEEE80211_M_IBSS: 826 if (ic->ic_state != IEEE80211_S_RUN || 827 seq != IEEE80211_AUTH_OPEN_REQUEST) { 828 DPRINTF(("discard auth from %s; state %u, seq %u\n", 829 ether_sprintf((u_int8_t *)wh->i_addr2), 830 ic->ic_state, seq)); 831 ic->ic_stats.is_rx_bad_auth++; 832 return; 833 } 834 ieee80211_new_state(ic, IEEE80211_S_AUTH, 835 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 836 837 /* In IBSS mode no (re)association frames are sent. */ 838 if (ic->ic_flags & IEEE80211_F_RSNON) 839 ni->ni_rsn_supp_state = RSNA_SUPP_PTKSTART; 840 break; 841 842 case IEEE80211_M_AHDEMO: 843 /* should not come here */ 844 break; 845 846 case IEEE80211_M_HOSTAP: 847 if (ic->ic_state != IEEE80211_S_RUN || 848 seq != IEEE80211_AUTH_OPEN_REQUEST) { 849 DPRINTF(("discard auth from %s; state %u, seq %u\n", 850 ether_sprintf((u_int8_t *)wh->i_addr2), 851 ic->ic_state, seq)); 852 ic->ic_stats.is_rx_bad_auth++; 853 return; 854 } 855 if (ni == ic->ic_bss) { 856 ni = ieee80211_find_node(ic, wh->i_addr2); 857 if (ni == NULL) 858 ni = ieee80211_alloc_node(ic, wh->i_addr2); 859 if (ni == NULL) { 860 return; 861 } 862 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 863 ni->ni_rssi = rxi->rxi_rssi; 864 ni->ni_rstamp = rxi->rxi_tstamp; 865 ni->ni_chan = ic->ic_bss->ni_chan; 866 } 867 868 /* 869 * Drivers may want to set up state before confirming. 870 * In which case this returns EBUSY and the driver will 871 * later call ieee80211_auth_open_confirm() by itself. 872 */ 873 if (ic->ic_newauth && ic->ic_newauth(ic, ni, 874 ni->ni_state != IEEE80211_STA_CACHE, seq) != 0) 875 break; 876 ieee80211_auth_open_confirm(ic, ni, seq); 877 break; 878 #endif /* IEEE80211_STA_ONLY */ 879 880 case IEEE80211_M_STA: 881 if (ic->ic_state != IEEE80211_S_AUTH || 882 seq != IEEE80211_AUTH_OPEN_RESPONSE) { 883 ic->ic_stats.is_rx_bad_auth++; 884 DPRINTF(("discard auth from %s; state %u, seq %u\n", 885 ether_sprintf((u_int8_t *)wh->i_addr2), 886 ic->ic_state, seq)); 887 return; 888 } 889 if (ic->ic_flags & IEEE80211_F_RSNON) { 890 /* XXX not here! */ 891 ic->ic_bss->ni_flags &= ~IEEE80211_NODE_TXRXPROT; 892 ic->ic_bss->ni_port_valid = 0; 893 ic->ic_bss->ni_replaycnt_ok = 0; 894 (*ic->ic_delete_key)(ic, ic->ic_bss, 895 &ic->ic_bss->ni_pairwise_key); 896 } 897 if (status != 0) { 898 if (ifp->if_flags & IFF_DEBUG) 899 printf("%s: open authentication failed " 900 "(status %d) for %s\n", ifp->if_xname, 901 status, 902 ether_sprintf((u_int8_t *)wh->i_addr3)); 903 if (ni != ic->ic_bss) 904 ni->ni_fails++; 905 else 906 ieee80211_try_another_bss(ic); 907 ic->ic_stats.is_rx_auth_fail++; 908 return; 909 } 910 ieee80211_new_state(ic, IEEE80211_S_ASSOC, 911 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 912 break; 913 default: 914 break; 915 } 916 } 917 918 void 919 ieee80211_set_beacon_miss_threshold(struct ieee80211com *ic) 920 { 921 struct ifnet *ifp = &ic->ic_if; 922 923 /* 924 * Scale the missed beacon counter threshold to the AP's actual 925 * beacon interval. 926 */ 927 int btimeout = MIN(IEEE80211_BEACON_MISS_THRES * ic->ic_bss->ni_intval, 928 IEEE80211_BEACON_MISS_THRES * (IEEE80211_DUR_TU / 10)); 929 /* Ensure that at least one beacon may be missed. */ 930 btimeout = MAX(btimeout, 2 * ic->ic_bss->ni_intval); 931 if (ic->ic_bss->ni_intval > 0) /* don't crash if interval is bogus */ 932 ic->ic_bmissthres = btimeout / ic->ic_bss->ni_intval; 933 934 if (ifp->if_flags & IFF_DEBUG) 935 printf("%s: missed beacon threshold set to %d beacons, " 936 "beacon interval is %u TU\n", ifp->if_xname, 937 ic->ic_bmissthres, ic->ic_bss->ni_intval); 938 } 939 940 /* Tell our peer, and the driver, to stop A-MPDU Tx for all TIDs. */ 941 void 942 ieee80211_stop_ampdu_tx(struct ieee80211com *ic, struct ieee80211_node *ni, 943 int mgt) 944 { 945 int tid; 946 947 for (tid = 0; tid < nitems(ni->ni_tx_ba); tid++) { 948 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 949 if (ba->ba_state != IEEE80211_BA_AGREED) 950 continue; 951 ieee80211_delba_request(ic, ni, 952 mgt == -1 ? 0 : IEEE80211_REASON_AUTH_LEAVE, 1, tid); 953 } 954 } 955 956 void 957 ieee80211_check_wpa_supplicant_failure(struct ieee80211com *ic, 958 struct ieee80211_node *ni) 959 { 960 struct ieee80211_node *ni2; 961 962 if (ic->ic_opmode != IEEE80211_M_STA 963 #ifndef IEEE80211_STA_ONLY 964 && ic->ic_opmode != IEEE80211_M_IBSS 965 #endif 966 ) 967 return; 968 969 if (ni->ni_rsn_supp_state != RSNA_SUPP_PTKNEGOTIATING) 970 return; 971 972 ni->ni_assoc_fail |= IEEE80211_NODE_ASSOCFAIL_WPA_KEY; 973 974 if (ni != ic->ic_bss) 975 return; 976 977 /* Also update the copy of our AP's node in the node cache. */ 978 ni2 = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 979 if (ni2) 980 ni2->ni_assoc_fail |= ic->ic_bss->ni_assoc_fail; 981 } 982 983 int 984 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, 985 int mgt) 986 { 987 struct ifnet *ifp = &ic->ic_if; 988 struct ieee80211_node *ni; 989 enum ieee80211_state ostate; 990 u_int rate; 991 #ifndef IEEE80211_STA_ONLY 992 int s; 993 #endif 994 995 ostate = ic->ic_state; 996 if (ifp->if_flags & IFF_DEBUG) 997 printf("%s: %s -> %s\n", ifp->if_xname, 998 ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 999 ic->ic_state = nstate; /* state transition */ 1000 ni = ic->ic_bss; /* NB: no reference held */ 1001 ieee80211_set_link_state(ic, LINK_STATE_DOWN); 1002 ic->ic_xflags &= ~IEEE80211_F_TX_MGMT_ONLY; 1003 switch (nstate) { 1004 case IEEE80211_S_INIT: 1005 /* 1006 * If mgt = -1, driver is already partway down, so do 1007 * not send management frames. 1008 */ 1009 switch (ostate) { 1010 case IEEE80211_S_INIT: 1011 break; 1012 case IEEE80211_S_RUN: 1013 if (mgt == -1) 1014 goto justcleanup; 1015 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1016 ieee80211_ba_del(ni); 1017 switch (ic->ic_opmode) { 1018 case IEEE80211_M_STA: 1019 IEEE80211_SEND_MGMT(ic, ni, 1020 IEEE80211_FC0_SUBTYPE_DISASSOC, 1021 IEEE80211_REASON_ASSOC_LEAVE); 1022 break; 1023 #ifndef IEEE80211_STA_ONLY 1024 case IEEE80211_M_HOSTAP: 1025 s = splnet(); 1026 RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) { 1027 if (ni->ni_state != IEEE80211_STA_ASSOC) 1028 continue; 1029 IEEE80211_SEND_MGMT(ic, ni, 1030 IEEE80211_FC0_SUBTYPE_DISASSOC, 1031 IEEE80211_REASON_ASSOC_LEAVE); 1032 } 1033 splx(s); 1034 break; 1035 #endif 1036 default: 1037 break; 1038 } 1039 /* FALLTHROUGH */ 1040 case IEEE80211_S_ASSOC: 1041 if (mgt == -1) 1042 goto justcleanup; 1043 switch (ic->ic_opmode) { 1044 case IEEE80211_M_STA: 1045 IEEE80211_SEND_MGMT(ic, ni, 1046 IEEE80211_FC0_SUBTYPE_DEAUTH, 1047 IEEE80211_REASON_AUTH_LEAVE); 1048 break; 1049 #ifndef IEEE80211_STA_ONLY 1050 case IEEE80211_M_HOSTAP: 1051 s = splnet(); 1052 RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) { 1053 IEEE80211_SEND_MGMT(ic, ni, 1054 IEEE80211_FC0_SUBTYPE_DEAUTH, 1055 IEEE80211_REASON_AUTH_LEAVE); 1056 } 1057 splx(s); 1058 break; 1059 #endif 1060 default: 1061 break; 1062 } 1063 /* FALLTHROUGH */ 1064 case IEEE80211_S_AUTH: 1065 case IEEE80211_S_SCAN: 1066 justcleanup: 1067 #ifndef IEEE80211_STA_ONLY 1068 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1069 timeout_del(&ic->ic_rsn_timeout); 1070 #endif 1071 ieee80211_ba_del(ni); 1072 timeout_del(&ic->ic_bgscan_timeout); 1073 ic->ic_bgscan_fail = 0; 1074 ic->ic_mgt_timer = 0; 1075 mq_purge(&ic->ic_mgtq); 1076 mq_purge(&ic->ic_pwrsaveq); 1077 ieee80211_free_allnodes(ic, 1); 1078 break; 1079 } 1080 ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE; 1081 ni->ni_assoc_fail = 0; 1082 if (ic->ic_flags & IEEE80211_F_RSNON) 1083 ieee80211_crypto_clear_groupkeys(ic); 1084 break; 1085 case IEEE80211_S_SCAN: 1086 ic->ic_flags &= ~IEEE80211_F_SIBSS; 1087 /* initialize bss for probe request */ 1088 IEEE80211_ADDR_COPY(ni->ni_macaddr, etherbroadcastaddr); 1089 IEEE80211_ADDR_COPY(ni->ni_bssid, etherbroadcastaddr); 1090 ni->ni_rates = ic->ic_sup_rates[ 1091 ieee80211_chan2mode(ic, ni->ni_chan)]; 1092 ni->ni_associd = 0; 1093 ni->ni_rstamp = 0; 1094 ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE; 1095 if (ic->ic_flags & IEEE80211_F_RSNON) 1096 ieee80211_crypto_clear_groupkeys(ic); 1097 switch (ostate) { 1098 case IEEE80211_S_INIT: 1099 #ifndef IEEE80211_STA_ONLY 1100 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1101 ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 1102 /* 1103 * AP operation and we already have a channel; 1104 * bypass the scan and startup immediately. 1105 */ 1106 ieee80211_create_ibss(ic, ic->ic_des_chan); 1107 } else 1108 #endif 1109 ieee80211_begin_scan(ifp); 1110 break; 1111 case IEEE80211_S_SCAN: 1112 /* scan next */ 1113 if (ic->ic_flags & IEEE80211_F_ASCAN) { 1114 IEEE80211_SEND_MGMT(ic, ni, 1115 IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0); 1116 } 1117 break; 1118 case IEEE80211_S_RUN: 1119 /* beacon miss */ 1120 if (ifp->if_flags & IFF_DEBUG) { 1121 /* XXX bssid clobbered above */ 1122 printf("%s: no recent beacons from %s;" 1123 " rescanning\n", ifp->if_xname, 1124 ether_sprintf(ic->ic_bss->ni_bssid)); 1125 } 1126 timeout_del(&ic->ic_bgscan_timeout); 1127 ic->ic_bgscan_fail = 0; 1128 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1129 ieee80211_free_allnodes(ic, 1); 1130 /* FALLTHROUGH */ 1131 case IEEE80211_S_AUTH: 1132 case IEEE80211_S_ASSOC: 1133 /* timeout restart scan */ 1134 ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 1135 if (ni != NULL) 1136 ni->ni_fails++; 1137 ieee80211_begin_scan(ifp); 1138 break; 1139 } 1140 break; 1141 case IEEE80211_S_AUTH: 1142 if (ostate == IEEE80211_S_RUN) 1143 ieee80211_check_wpa_supplicant_failure(ic, ni); 1144 ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE; 1145 if (ic->ic_flags & IEEE80211_F_RSNON) 1146 ieee80211_crypto_clear_groupkeys(ic); 1147 switch (ostate) { 1148 case IEEE80211_S_INIT: 1149 if (ifp->if_flags & IFF_DEBUG) 1150 printf("%s: invalid transition %s -> %s\n", 1151 ifp->if_xname, ieee80211_state_name[ostate], 1152 ieee80211_state_name[nstate]); 1153 break; 1154 case IEEE80211_S_SCAN: 1155 IEEE80211_SEND_MGMT(ic, ni, 1156 IEEE80211_FC0_SUBTYPE_AUTH, 1); 1157 break; 1158 case IEEE80211_S_AUTH: 1159 case IEEE80211_S_ASSOC: 1160 switch (mgt) { 1161 case IEEE80211_FC0_SUBTYPE_AUTH: 1162 if (ic->ic_opmode == IEEE80211_M_STA) { 1163 IEEE80211_SEND_MGMT(ic, ni, 1164 IEEE80211_FC0_SUBTYPE_AUTH, 1165 IEEE80211_AUTH_OPEN_REQUEST); 1166 } 1167 break; 1168 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1169 /* ignore and retry scan on timeout */ 1170 break; 1171 } 1172 break; 1173 case IEEE80211_S_RUN: 1174 timeout_del(&ic->ic_bgscan_timeout); 1175 ic->ic_bgscan_fail = 0; 1176 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1177 ieee80211_ba_del(ni); 1178 switch (mgt) { 1179 case IEEE80211_FC0_SUBTYPE_AUTH: 1180 IEEE80211_SEND_MGMT(ic, ni, 1181 IEEE80211_FC0_SUBTYPE_AUTH, 2); 1182 ic->ic_state = ostate; /* stay RUN */ 1183 break; 1184 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1185 /* try to reauth */ 1186 IEEE80211_SEND_MGMT(ic, ni, 1187 IEEE80211_FC0_SUBTYPE_AUTH, 1); 1188 break; 1189 } 1190 break; 1191 } 1192 break; 1193 case IEEE80211_S_ASSOC: 1194 switch (ostate) { 1195 case IEEE80211_S_INIT: 1196 case IEEE80211_S_SCAN: 1197 case IEEE80211_S_ASSOC: 1198 if (ifp->if_flags & IFF_DEBUG) 1199 printf("%s: invalid transition %s -> %s\n", 1200 ifp->if_xname, ieee80211_state_name[ostate], 1201 ieee80211_state_name[nstate]); 1202 break; 1203 case IEEE80211_S_AUTH: 1204 IEEE80211_SEND_MGMT(ic, ni, 1205 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0); 1206 break; 1207 case IEEE80211_S_RUN: 1208 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1209 ieee80211_ba_del(ni); 1210 IEEE80211_SEND_MGMT(ic, ni, 1211 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1); 1212 break; 1213 } 1214 break; 1215 case IEEE80211_S_RUN: 1216 switch (ostate) { 1217 case IEEE80211_S_INIT: 1218 if (ic->ic_opmode == IEEE80211_M_MONITOR) 1219 break; 1220 case IEEE80211_S_AUTH: 1221 case IEEE80211_S_RUN: 1222 if (ifp->if_flags & IFF_DEBUG) 1223 printf("%s: invalid transition %s -> %s\n", 1224 ifp->if_xname, ieee80211_state_name[ostate], 1225 ieee80211_state_name[nstate]); 1226 break; 1227 case IEEE80211_S_SCAN: /* adhoc/hostap mode */ 1228 case IEEE80211_S_ASSOC: /* infra mode */ 1229 if (ni->ni_txrate >= ni->ni_rates.rs_nrates) 1230 panic("%s: bogus xmit rate %u setup", 1231 __func__, ni->ni_txrate); 1232 if (ifp->if_flags & IFF_DEBUG) { 1233 printf("%s: %s with %s ssid ", 1234 ifp->if_xname, 1235 ic->ic_opmode == IEEE80211_M_STA ? 1236 "associated" : "synchronized", 1237 ether_sprintf(ni->ni_bssid)); 1238 ieee80211_print_essid(ic->ic_bss->ni_essid, 1239 ni->ni_esslen); 1240 rate = ni->ni_rates.rs_rates[ni->ni_txrate] & 1241 IEEE80211_RATE_VAL; 1242 printf(" channel %d", 1243 ieee80211_chan2ieee(ic, ni->ni_chan)); 1244 if (ni->ni_flags & IEEE80211_NODE_HT) 1245 printf(" start MCS %u", ni->ni_txmcs); 1246 else 1247 printf(" start %u%sMb", 1248 rate / 2, (rate & 1) ? ".5" : ""); 1249 printf(" %s preamble %s slot time%s%s\n", 1250 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? 1251 "short" : "long", 1252 (ic->ic_flags & IEEE80211_F_SHSLOT) ? 1253 "short" : "long", 1254 (ic->ic_flags & IEEE80211_F_USEPROT) ? 1255 " protection enabled" : "", 1256 (ni->ni_flags & IEEE80211_NODE_HT) ? 1257 " HT enabled" : ""); 1258 } 1259 if (!(ic->ic_flags & IEEE80211_F_RSNON)) { 1260 /* 1261 * NB: When RSN is enabled, we defer setting 1262 * the link up until the port is valid. 1263 */ 1264 ieee80211_set_link_state(ic, LINK_STATE_UP); 1265 ni->ni_assoc_fail = 0; 1266 } 1267 ic->ic_mgt_timer = 0; 1268 ieee80211_set_beacon_miss_threshold(ic); 1269 if_start(ifp); 1270 break; 1271 } 1272 break; 1273 } 1274 return 0; 1275 } 1276 1277 void 1278 ieee80211_set_link_state(struct ieee80211com *ic, int nstate) 1279 { 1280 struct ifnet *ifp = &ic->ic_if; 1281 1282 switch (ic->ic_opmode) { 1283 #ifndef IEEE80211_STA_ONLY 1284 case IEEE80211_M_IBSS: 1285 case IEEE80211_M_HOSTAP: 1286 nstate = LINK_STATE_UNKNOWN; 1287 break; 1288 #endif 1289 case IEEE80211_M_MONITOR: 1290 nstate = LINK_STATE_DOWN; 1291 break; 1292 default: 1293 break; 1294 } 1295 if (nstate != ifp->if_link_state) { 1296 ifp->if_link_state = nstate; 1297 if (LINK_STATE_IS_UP(nstate)) { 1298 struct if_ieee80211_data ifie; 1299 memset(&ifie, 0, sizeof(ifie)); 1300 ifie.ifie_nwid_len = ic->ic_bss->ni_esslen; 1301 memcpy(ifie.ifie_nwid, ic->ic_bss->ni_essid, 1302 sizeof(ifie.ifie_nwid)); 1303 memcpy(ifie.ifie_addr, ic->ic_bss->ni_bssid, 1304 sizeof(ifie.ifie_addr)); 1305 ifie.ifie_channel = ieee80211_chan2ieee(ic, 1306 ic->ic_bss->ni_chan); 1307 ifie.ifie_flags = ic->ic_flags; 1308 ifie.ifie_xflags = ic->ic_xflags; 1309 rtm_80211info(&ic->ic_if, &ifie); 1310 } 1311 if_link_state_change(ifp); 1312 } 1313 } 1314