1 /* $OpenBSD: ieee80211_proto.c,v 1.105 2021/10/11 09:01:06 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 /* install GTK */ 475 kid = (ic->ic_def_txkey == 1) ? 2 : 1; 476 switch ((*ic->ic_set_key)(ic, ic->ic_bss, &ic->ic_nw_keys[kid])) { 477 case 0: 478 case EBUSY: 479 ic->ic_def_txkey = kid; 480 break; 481 default: 482 break; 483 } 484 485 if (ic->ic_caps & IEEE80211_C_MFP) { 486 /* install IGTK */ 487 kid = (ic->ic_igtk_kid == 4) ? 5 : 4; 488 switch ((*ic->ic_set_key)(ic, ic->ic_bss, &ic->ic_nw_keys[kid])) { 489 case 0: 490 case EBUSY: 491 ic->ic_igtk_kid = kid; 492 break; 493 default: 494 break; 495 } 496 } 497 } 498 499 /* 500 * Group key lifetime has expired, update it. 501 */ 502 void 503 ieee80211_gtk_rekey_timeout(void *arg) 504 { 505 struct ieee80211com *ic = arg; 506 int s; 507 508 s = splnet(); 509 ieee80211_setkeys(ic); 510 splx(s); 511 512 /* re-schedule a GTK rekeying after 3600s */ 513 timeout_add_sec(&ic->ic_rsn_timeout, 3600); 514 } 515 516 void 517 ieee80211_sa_query_timeout(void *arg) 518 { 519 struct ieee80211_node *ni = arg; 520 struct ieee80211com *ic = ni->ni_ic; 521 int s; 522 523 s = splnet(); 524 if (++ni->ni_sa_query_count >= 3) { 525 ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY; 526 ni->ni_flags |= IEEE80211_NODE_SA_QUERY_FAILED; 527 } else /* retry SA Query Request */ 528 ieee80211_sa_query_request(ic, ni); 529 splx(s); 530 } 531 532 /* 533 * Request that a SA Query Request frame be sent to a specified peer STA 534 * to which the STA is associated. 535 */ 536 void 537 ieee80211_sa_query_request(struct ieee80211com *ic, struct ieee80211_node *ni) 538 { 539 /* MLME-SAQuery.request */ 540 541 if (!(ni->ni_flags & IEEE80211_NODE_SA_QUERY)) { 542 ni->ni_flags |= IEEE80211_NODE_SA_QUERY; 543 ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY_FAILED; 544 ni->ni_sa_query_count = 0; 545 } 546 /* generate new Transaction Identifier */ 547 ni->ni_sa_query_trid++; 548 549 /* send SA Query Request */ 550 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_SA_QUERY, 551 IEEE80211_ACTION_SA_QUERY_REQ, 0); 552 timeout_add_msec(&ni->ni_sa_query_to, 10); 553 } 554 #endif /* IEEE80211_STA_ONLY */ 555 556 void 557 ieee80211_ht_negotiate(struct ieee80211com *ic, struct ieee80211_node *ni) 558 { 559 int i; 560 561 ni->ni_flags &= ~(IEEE80211_NODE_HT | IEEE80211_NODE_HT_SGI20 | 562 IEEE80211_NODE_HT_SGI40); 563 564 /* Check if we support HT. */ 565 if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) == 0) 566 return; 567 568 /* Check if HT support has been explicitly disabled. */ 569 if ((ic->ic_flags & IEEE80211_F_HTON) == 0) 570 return; 571 572 /* 573 * Check if the peer supports HT. 574 * Require at least one of the mandatory MCS. 575 * MCS 0-7 are mandatory but some APs have particular MCS disabled. 576 */ 577 if (!ieee80211_node_supports_ht(ni)) { 578 ic->ic_stats.is_ht_nego_no_mandatory_mcs++; 579 return; 580 } 581 582 if (ic->ic_opmode == IEEE80211_M_STA) { 583 /* We must support the AP's basic MCS set. */ 584 for (i = 0; i < IEEE80211_HT_NUM_MCS; i++) { 585 if (isset(ni->ni_basic_mcs, i) && 586 !isset(ic->ic_sup_mcs, i)) { 587 ic->ic_stats.is_ht_nego_no_basic_mcs++; 588 return; 589 } 590 } 591 } 592 593 /* 594 * Don't allow group cipher (includes WEP) or TKIP 595 * for pairwise encryption (see 802.11-2012 11.1.6). 596 */ 597 if (ic->ic_flags & IEEE80211_F_WEPON) { 598 ic->ic_stats.is_ht_nego_bad_crypto++; 599 return; 600 } 601 if ((ic->ic_flags & IEEE80211_F_RSNON) && 602 (ni->ni_rsnciphers & IEEE80211_CIPHER_USEGROUP || 603 ni->ni_rsnciphers & IEEE80211_CIPHER_TKIP)) { 604 ic->ic_stats.is_ht_nego_bad_crypto++; 605 return; 606 } 607 608 ni->ni_flags |= IEEE80211_NODE_HT; 609 610 if (ieee80211_node_supports_ht_sgi20(ni) && 611 (ic->ic_htcaps & IEEE80211_HTCAP_SGI20)) 612 ni->ni_flags |= IEEE80211_NODE_HT_SGI20; 613 if (ieee80211_node_supports_ht_sgi40(ni) && 614 (ic->ic_htcaps & IEEE80211_HTCAP_SGI40)) 615 ni->ni_flags |= IEEE80211_NODE_HT_SGI40; 616 } 617 618 void 619 ieee80211_tx_ba_timeout(void *arg) 620 { 621 struct ieee80211_tx_ba *ba = arg; 622 struct ieee80211_node *ni = ba->ba_ni; 623 struct ieee80211com *ic = ni->ni_ic; 624 u_int8_t tid; 625 int s; 626 627 s = splnet(); 628 tid = ((caddr_t)ba - (caddr_t)ni->ni_tx_ba) / sizeof(*ba); 629 if (ba->ba_state == IEEE80211_BA_REQUESTED) { 630 /* MLME-ADDBA.confirm(TIMEOUT) */ 631 ba->ba_state = IEEE80211_BA_INIT; 632 if (ni->ni_addba_req_intval[tid] < 633 IEEE80211_ADDBA_REQ_INTVAL_MAX) 634 ni->ni_addba_req_intval[tid]++; 635 /* 636 * In case the peer believes there is an existing 637 * block ack agreement with us, try to delete it. 638 */ 639 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA, 640 IEEE80211_ACTION_DELBA, 641 IEEE80211_REASON_SETUP_REQUIRED << 16 | 1 << 8 | tid); 642 } else if (ba->ba_state == IEEE80211_BA_AGREED) { 643 /* Block Ack inactivity timeout */ 644 ic->ic_stats.is_ht_tx_ba_timeout++; 645 ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT, 646 1, tid); 647 } 648 splx(s); 649 } 650 651 void 652 ieee80211_rx_ba_timeout(void *arg) 653 { 654 struct ieee80211_rx_ba *ba = arg; 655 struct ieee80211_node *ni = ba->ba_ni; 656 struct ieee80211com *ic = ni->ni_ic; 657 u_int8_t tid; 658 int s; 659 660 ic->ic_stats.is_ht_rx_ba_timeout++; 661 662 s = splnet(); 663 664 /* Block Ack inactivity timeout */ 665 tid = ((caddr_t)ba - (caddr_t)ni->ni_rx_ba) / sizeof(*ba); 666 ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT, 0, tid); 667 668 splx(s); 669 } 670 671 /* 672 * Request initiation of Block Ack with the specified peer. 673 */ 674 int 675 ieee80211_addba_request(struct ieee80211com *ic, struct ieee80211_node *ni, 676 u_int16_t ssn, u_int8_t tid) 677 { 678 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 679 680 if (ba->ba_state != IEEE80211_BA_INIT) 681 return EBUSY; 682 683 /* MLME-ADDBA.request */ 684 685 /* setup Block Ack */ 686 ba->ba_ni = ni; 687 ba->ba_state = IEEE80211_BA_REQUESTED; 688 ba->ba_token = ic->ic_dialog_token++; 689 ba->ba_timeout_val = 0; 690 timeout_set(&ba->ba_to, ieee80211_tx_ba_timeout, ba); 691 ba->ba_winsize = IEEE80211_BA_MAX_WINSZ; 692 ba->ba_winstart = ssn; 693 ba->ba_winend = (ba->ba_winstart + ba->ba_winsize - 1) & 0xfff; 694 ba->ba_params = 695 (ba->ba_winsize << IEEE80211_ADDBA_BUFSZ_SHIFT) | 696 (tid << IEEE80211_ADDBA_TID_SHIFT); 697 ba->ba_params |= IEEE80211_ADDBA_AMSDU; 698 if ((ic->ic_htcaps & IEEE80211_HTCAP_DELAYEDBA) == 0) 699 /* immediate BA */ 700 ba->ba_params |= IEEE80211_ADDBA_BA_POLICY; 701 702 if ((ic->ic_caps & IEEE80211_C_ADDBA_OFFLOAD) && 703 ic->ic_ampdu_tx_start != NULL) { 704 int err = ic->ic_ampdu_tx_start(ic, ni, tid); 705 if (err && err != EBUSY) { 706 /* driver failed to setup, rollback */ 707 ieee80211_addba_resp_refuse(ic, ni, tid, 708 IEEE80211_STATUS_UNSPECIFIED); 709 } else if (err == 0) 710 ieee80211_addba_resp_accept(ic, ni, tid); 711 return err; /* The device will send an ADDBA frame. */ 712 } 713 714 timeout_add_sec(&ba->ba_to, 1); /* dot11ADDBAResponseTimeout */ 715 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA, 716 IEEE80211_ACTION_ADDBA_REQ, tid); 717 return 0; 718 } 719 720 /* 721 * Request the deletion of Block Ack with a peer and notify driver. 722 */ 723 void 724 ieee80211_delba_request(struct ieee80211com *ic, struct ieee80211_node *ni, 725 u_int16_t reason, u_int8_t dir, u_int8_t tid) 726 { 727 /* MLME-DELBA.request */ 728 729 if (reason) { 730 /* transmit a DELBA frame */ 731 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA, 732 IEEE80211_ACTION_DELBA, reason << 16 | dir << 8 | tid); 733 } 734 if (dir) { 735 /* MLME-DELBA.confirm(Originator) */ 736 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 737 738 if (ic->ic_ampdu_tx_stop != NULL) 739 ic->ic_ampdu_tx_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 } else { 745 /* MLME-DELBA.confirm(Recipient) */ 746 struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid]; 747 int i; 748 749 if (ic->ic_ampdu_rx_stop != NULL) 750 ic->ic_ampdu_rx_stop(ic, ni, tid); 751 752 ba->ba_state = IEEE80211_BA_INIT; 753 /* stop Block Ack inactivity timer */ 754 timeout_del(&ba->ba_to); 755 timeout_del(&ba->ba_gap_to); 756 757 if (ba->ba_buf != NULL) { 758 /* free all MSDUs stored in reordering buffer */ 759 for (i = 0; i < IEEE80211_BA_MAX_WINSZ; i++) 760 m_freem(ba->ba_buf[i].m); 761 /* free reordering buffer */ 762 free(ba->ba_buf, M_DEVBUF, 763 IEEE80211_BA_MAX_WINSZ * sizeof(*ba->ba_buf)); 764 ba->ba_buf = NULL; 765 } 766 } 767 } 768 769 #ifndef IEEE80211_STA_ONLY 770 void 771 ieee80211_auth_open_confirm(struct ieee80211com *ic, 772 struct ieee80211_node *ni, uint16_t seq) 773 { 774 struct ifnet *ifp = &ic->ic_if; 775 776 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1); 777 if (ifp->if_flags & IFF_DEBUG) 778 printf("%s: station %s %s authenticated (open)\n", 779 ifp->if_xname, 780 ether_sprintf((u_int8_t *)ni->ni_macaddr), 781 ni->ni_state != IEEE80211_STA_CACHE ? 782 "newly" : "already"); 783 ieee80211_node_newstate(ni, IEEE80211_STA_AUTH); 784 } 785 #endif 786 787 void 788 ieee80211_try_another_bss(struct ieee80211com *ic) 789 { 790 struct ieee80211_node *curbs, *selbs; 791 struct ifnet *ifp = &ic->ic_if; 792 793 /* Don't select our current AP again. */ 794 curbs = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 795 if (curbs) { 796 curbs->ni_fails++; 797 ieee80211_node_newstate(curbs, IEEE80211_STA_CACHE); 798 } 799 800 /* Try a different AP from the same ESS if available. */ 801 if (ic->ic_caps & IEEE80211_C_SCANALLBAND) { 802 /* 803 * Make sure we will consider APs on all bands during 804 * access point selection in ieee80211_node_choose_bss(). 805 * During multi-band scans, our previous AP may be trying 806 * to steer us onto another band by denying authentication. 807 */ 808 ieee80211_setmode(ic, IEEE80211_MODE_AUTO); 809 } 810 selbs = ieee80211_node_choose_bss(ic, 0, NULL); 811 if (selbs == NULL) 812 return; 813 814 /* Should not happen but seriously, don't try the same AP again. */ 815 if (memcmp(selbs->ni_macaddr, ic->ic_bss->ni_macaddr, 816 IEEE80211_NWID_LEN) == 0) 817 return; 818 819 if (ifp->if_flags & IFF_DEBUG) 820 printf("%s: trying AP %s on channel %d instead\n", 821 ifp->if_xname, ether_sprintf(selbs->ni_macaddr), 822 ieee80211_chan2ieee(ic, selbs->ni_chan)); 823 824 /* Triggers an AUTH->AUTH transition, avoiding another SCAN. */ 825 ieee80211_node_join_bss(ic, selbs); 826 } 827 828 void 829 ieee80211_auth_open(struct ieee80211com *ic, const struct ieee80211_frame *wh, 830 struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi, u_int16_t seq, 831 u_int16_t status) 832 { 833 struct ifnet *ifp = &ic->ic_if; 834 switch (ic->ic_opmode) { 835 #ifndef IEEE80211_STA_ONLY 836 case IEEE80211_M_IBSS: 837 if (ic->ic_state != IEEE80211_S_RUN || 838 seq != IEEE80211_AUTH_OPEN_REQUEST) { 839 DPRINTF(("discard auth from %s; state %u, seq %u\n", 840 ether_sprintf((u_int8_t *)wh->i_addr2), 841 ic->ic_state, seq)); 842 ic->ic_stats.is_rx_bad_auth++; 843 return; 844 } 845 ieee80211_new_state(ic, IEEE80211_S_AUTH, 846 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 847 848 /* In IBSS mode no (re)association frames are sent. */ 849 if (ic->ic_flags & IEEE80211_F_RSNON) 850 ni->ni_rsn_supp_state = RSNA_SUPP_PTKSTART; 851 break; 852 853 case IEEE80211_M_AHDEMO: 854 /* should not come here */ 855 break; 856 857 case IEEE80211_M_HOSTAP: 858 if (ic->ic_state != IEEE80211_S_RUN || 859 seq != IEEE80211_AUTH_OPEN_REQUEST) { 860 DPRINTF(("discard auth from %s; state %u, seq %u\n", 861 ether_sprintf((u_int8_t *)wh->i_addr2), 862 ic->ic_state, seq)); 863 ic->ic_stats.is_rx_bad_auth++; 864 return; 865 } 866 if (ni == ic->ic_bss) { 867 ni = ieee80211_find_node(ic, wh->i_addr2); 868 if (ni == NULL) 869 ni = ieee80211_alloc_node(ic, wh->i_addr2); 870 if (ni == NULL) { 871 return; 872 } 873 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 874 ni->ni_rssi = rxi->rxi_rssi; 875 ni->ni_rstamp = rxi->rxi_tstamp; 876 ni->ni_chan = ic->ic_bss->ni_chan; 877 } 878 879 /* 880 * Drivers may want to set up state before confirming. 881 * In which case this returns EBUSY and the driver will 882 * later call ieee80211_auth_open_confirm() by itself. 883 */ 884 if (ic->ic_newauth && ic->ic_newauth(ic, ni, 885 ni->ni_state != IEEE80211_STA_CACHE, seq) != 0) 886 break; 887 ieee80211_auth_open_confirm(ic, ni, seq); 888 break; 889 #endif /* IEEE80211_STA_ONLY */ 890 891 case IEEE80211_M_STA: 892 if (ic->ic_state != IEEE80211_S_AUTH || 893 seq != IEEE80211_AUTH_OPEN_RESPONSE) { 894 ic->ic_stats.is_rx_bad_auth++; 895 DPRINTF(("discard auth from %s; state %u, seq %u\n", 896 ether_sprintf((u_int8_t *)wh->i_addr2), 897 ic->ic_state, seq)); 898 return; 899 } 900 if (ic->ic_flags & IEEE80211_F_RSNON) { 901 /* XXX not here! */ 902 ic->ic_bss->ni_flags &= ~IEEE80211_NODE_TXRXPROT; 903 ic->ic_bss->ni_port_valid = 0; 904 ic->ic_bss->ni_replaycnt_ok = 0; 905 (*ic->ic_delete_key)(ic, ic->ic_bss, 906 &ic->ic_bss->ni_pairwise_key); 907 } 908 if (status != 0) { 909 if (ifp->if_flags & IFF_DEBUG) 910 printf("%s: open authentication failed " 911 "(status %d) for %s\n", ifp->if_xname, 912 status, 913 ether_sprintf((u_int8_t *)wh->i_addr3)); 914 if (ni != ic->ic_bss) 915 ni->ni_fails++; 916 else 917 ieee80211_try_another_bss(ic); 918 ic->ic_stats.is_rx_auth_fail++; 919 return; 920 } 921 ieee80211_new_state(ic, IEEE80211_S_ASSOC, 922 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 923 break; 924 default: 925 break; 926 } 927 } 928 929 void 930 ieee80211_set_beacon_miss_threshold(struct ieee80211com *ic) 931 { 932 struct ifnet *ifp = &ic->ic_if; 933 934 /* 935 * Scale the missed beacon counter threshold to the AP's actual 936 * beacon interval. 937 */ 938 int btimeout = MIN(IEEE80211_BEACON_MISS_THRES * ic->ic_bss->ni_intval, 939 IEEE80211_BEACON_MISS_THRES * (IEEE80211_DUR_TU / 10)); 940 /* Ensure that at least one beacon may be missed. */ 941 btimeout = MAX(btimeout, 2 * ic->ic_bss->ni_intval); 942 if (ic->ic_bss->ni_intval > 0) /* don't crash if interval is bogus */ 943 ic->ic_bmissthres = btimeout / ic->ic_bss->ni_intval; 944 945 if (ifp->if_flags & IFF_DEBUG) 946 printf("%s: missed beacon threshold set to %d beacons, " 947 "beacon interval is %u TU\n", ifp->if_xname, 948 ic->ic_bmissthres, ic->ic_bss->ni_intval); 949 } 950 951 /* Tell our peer, and the driver, to stop A-MPDU Tx for all TIDs. */ 952 void 953 ieee80211_stop_ampdu_tx(struct ieee80211com *ic, struct ieee80211_node *ni, 954 int mgt) 955 { 956 int tid; 957 958 for (tid = 0; tid < nitems(ni->ni_tx_ba); tid++) { 959 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 960 if (ba->ba_state != IEEE80211_BA_AGREED) 961 continue; 962 963 if (ic->ic_caps & IEEE80211_C_ADDBA_OFFLOAD) { 964 if (ic->ic_ampdu_tx_stop != NULL) 965 ic->ic_ampdu_tx_stop(ic, ni, tid); 966 continue; /* Don't change ba->ba_state! */ 967 } 968 969 ieee80211_delba_request(ic, ni, 970 mgt == -1 ? 0 : IEEE80211_REASON_AUTH_LEAVE, 1, tid); 971 } 972 } 973 974 void 975 ieee80211_check_wpa_supplicant_failure(struct ieee80211com *ic, 976 struct ieee80211_node *ni) 977 { 978 struct ieee80211_node *ni2; 979 980 if (ic->ic_opmode != IEEE80211_M_STA 981 #ifndef IEEE80211_STA_ONLY 982 && ic->ic_opmode != IEEE80211_M_IBSS 983 #endif 984 ) 985 return; 986 987 if (ni->ni_rsn_supp_state != RSNA_SUPP_PTKNEGOTIATING) 988 return; 989 990 ni->ni_assoc_fail |= IEEE80211_NODE_ASSOCFAIL_WPA_KEY; 991 992 if (ni != ic->ic_bss) 993 return; 994 995 /* Also update the copy of our AP's node in the node cache. */ 996 ni2 = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 997 if (ni2) 998 ni2->ni_assoc_fail |= ic->ic_bss->ni_assoc_fail; 999 } 1000 1001 int 1002 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, 1003 int mgt) 1004 { 1005 struct ifnet *ifp = &ic->ic_if; 1006 struct ieee80211_node *ni; 1007 enum ieee80211_state ostate; 1008 u_int rate; 1009 #ifndef IEEE80211_STA_ONLY 1010 int s; 1011 #endif 1012 1013 ostate = ic->ic_state; 1014 if (ifp->if_flags & IFF_DEBUG) 1015 printf("%s: %s -> %s\n", ifp->if_xname, 1016 ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 1017 ic->ic_state = nstate; /* state transition */ 1018 ni = ic->ic_bss; /* NB: no reference held */ 1019 ieee80211_set_link_state(ic, LINK_STATE_DOWN); 1020 ic->ic_xflags &= ~IEEE80211_F_TX_MGMT_ONLY; 1021 switch (nstate) { 1022 case IEEE80211_S_INIT: 1023 /* 1024 * If mgt = -1, driver is already partway down, so do 1025 * not send management frames. 1026 */ 1027 switch (ostate) { 1028 case IEEE80211_S_INIT: 1029 break; 1030 case IEEE80211_S_RUN: 1031 if (mgt == -1) 1032 goto justcleanup; 1033 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1034 ieee80211_ba_del(ni); 1035 switch (ic->ic_opmode) { 1036 case IEEE80211_M_STA: 1037 IEEE80211_SEND_MGMT(ic, ni, 1038 IEEE80211_FC0_SUBTYPE_DISASSOC, 1039 IEEE80211_REASON_ASSOC_LEAVE); 1040 break; 1041 #ifndef IEEE80211_STA_ONLY 1042 case IEEE80211_M_HOSTAP: 1043 s = splnet(); 1044 RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) { 1045 if (ni->ni_state != IEEE80211_STA_ASSOC) 1046 continue; 1047 IEEE80211_SEND_MGMT(ic, ni, 1048 IEEE80211_FC0_SUBTYPE_DISASSOC, 1049 IEEE80211_REASON_ASSOC_LEAVE); 1050 } 1051 splx(s); 1052 break; 1053 #endif 1054 default: 1055 break; 1056 } 1057 /* FALLTHROUGH */ 1058 case IEEE80211_S_ASSOC: 1059 if (mgt == -1) 1060 goto justcleanup; 1061 switch (ic->ic_opmode) { 1062 case IEEE80211_M_STA: 1063 IEEE80211_SEND_MGMT(ic, ni, 1064 IEEE80211_FC0_SUBTYPE_DEAUTH, 1065 IEEE80211_REASON_AUTH_LEAVE); 1066 break; 1067 #ifndef IEEE80211_STA_ONLY 1068 case IEEE80211_M_HOSTAP: 1069 s = splnet(); 1070 RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) { 1071 IEEE80211_SEND_MGMT(ic, ni, 1072 IEEE80211_FC0_SUBTYPE_DEAUTH, 1073 IEEE80211_REASON_AUTH_LEAVE); 1074 } 1075 splx(s); 1076 break; 1077 #endif 1078 default: 1079 break; 1080 } 1081 /* FALLTHROUGH */ 1082 case IEEE80211_S_AUTH: 1083 case IEEE80211_S_SCAN: 1084 justcleanup: 1085 #ifndef IEEE80211_STA_ONLY 1086 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1087 timeout_del(&ic->ic_rsn_timeout); 1088 #endif 1089 ieee80211_ba_del(ni); 1090 timeout_del(&ic->ic_bgscan_timeout); 1091 ic->ic_bgscan_fail = 0; 1092 ic->ic_mgt_timer = 0; 1093 mq_purge(&ic->ic_mgtq); 1094 mq_purge(&ic->ic_pwrsaveq); 1095 ieee80211_free_allnodes(ic, 1); 1096 break; 1097 } 1098 ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE; 1099 ni->ni_assoc_fail = 0; 1100 if (ic->ic_flags & IEEE80211_F_RSNON) 1101 ieee80211_crypto_clear_groupkeys(ic); 1102 break; 1103 case IEEE80211_S_SCAN: 1104 ic->ic_flags &= ~IEEE80211_F_SIBSS; 1105 /* initialize bss for probe request */ 1106 IEEE80211_ADDR_COPY(ni->ni_macaddr, etherbroadcastaddr); 1107 IEEE80211_ADDR_COPY(ni->ni_bssid, etherbroadcastaddr); 1108 ni->ni_rates = ic->ic_sup_rates[ 1109 ieee80211_chan2mode(ic, ni->ni_chan)]; 1110 ni->ni_associd = 0; 1111 ni->ni_rstamp = 0; 1112 ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE; 1113 if (ic->ic_flags & IEEE80211_F_RSNON) 1114 ieee80211_crypto_clear_groupkeys(ic); 1115 switch (ostate) { 1116 case IEEE80211_S_INIT: 1117 #ifndef IEEE80211_STA_ONLY 1118 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1119 ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 1120 /* 1121 * AP operation and we already have a channel; 1122 * bypass the scan and startup immediately. 1123 */ 1124 ieee80211_create_ibss(ic, ic->ic_des_chan); 1125 } else 1126 #endif 1127 ieee80211_begin_scan(ifp); 1128 break; 1129 case IEEE80211_S_SCAN: 1130 /* scan next */ 1131 if (ic->ic_flags & IEEE80211_F_ASCAN) { 1132 IEEE80211_SEND_MGMT(ic, ni, 1133 IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0); 1134 } 1135 break; 1136 case IEEE80211_S_RUN: 1137 /* beacon miss */ 1138 if (ifp->if_flags & IFF_DEBUG) { 1139 /* XXX bssid clobbered above */ 1140 printf("%s: no recent beacons from %s;" 1141 " rescanning\n", ifp->if_xname, 1142 ether_sprintf(ic->ic_bss->ni_bssid)); 1143 } 1144 timeout_del(&ic->ic_bgscan_timeout); 1145 ic->ic_bgscan_fail = 0; 1146 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1147 ieee80211_free_allnodes(ic, 1); 1148 /* FALLTHROUGH */ 1149 case IEEE80211_S_AUTH: 1150 case IEEE80211_S_ASSOC: 1151 /* timeout restart scan */ 1152 ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 1153 if (ni != NULL) 1154 ni->ni_fails++; 1155 ieee80211_begin_scan(ifp); 1156 break; 1157 } 1158 break; 1159 case IEEE80211_S_AUTH: 1160 if (ostate == IEEE80211_S_RUN) 1161 ieee80211_check_wpa_supplicant_failure(ic, ni); 1162 ni->ni_rsn_supp_state = RSNA_SUPP_INITIALIZE; 1163 if (ic->ic_flags & IEEE80211_F_RSNON) 1164 ieee80211_crypto_clear_groupkeys(ic); 1165 switch (ostate) { 1166 case IEEE80211_S_INIT: 1167 if (ifp->if_flags & IFF_DEBUG) 1168 printf("%s: invalid transition %s -> %s\n", 1169 ifp->if_xname, ieee80211_state_name[ostate], 1170 ieee80211_state_name[nstate]); 1171 break; 1172 case IEEE80211_S_SCAN: 1173 IEEE80211_SEND_MGMT(ic, ni, 1174 IEEE80211_FC0_SUBTYPE_AUTH, 1); 1175 break; 1176 case IEEE80211_S_AUTH: 1177 case IEEE80211_S_ASSOC: 1178 switch (mgt) { 1179 case IEEE80211_FC0_SUBTYPE_AUTH: 1180 if (ic->ic_opmode == IEEE80211_M_STA) { 1181 IEEE80211_SEND_MGMT(ic, ni, 1182 IEEE80211_FC0_SUBTYPE_AUTH, 1183 IEEE80211_AUTH_OPEN_REQUEST); 1184 } 1185 break; 1186 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1187 /* ignore and retry scan on timeout */ 1188 break; 1189 } 1190 break; 1191 case IEEE80211_S_RUN: 1192 timeout_del(&ic->ic_bgscan_timeout); 1193 ic->ic_bgscan_fail = 0; 1194 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1195 ieee80211_ba_del(ni); 1196 switch (mgt) { 1197 case IEEE80211_FC0_SUBTYPE_AUTH: 1198 IEEE80211_SEND_MGMT(ic, ni, 1199 IEEE80211_FC0_SUBTYPE_AUTH, 2); 1200 ic->ic_state = ostate; /* stay RUN */ 1201 break; 1202 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1203 /* try to reauth */ 1204 IEEE80211_SEND_MGMT(ic, ni, 1205 IEEE80211_FC0_SUBTYPE_AUTH, 1); 1206 break; 1207 } 1208 break; 1209 } 1210 break; 1211 case IEEE80211_S_ASSOC: 1212 switch (ostate) { 1213 case IEEE80211_S_INIT: 1214 case IEEE80211_S_SCAN: 1215 case IEEE80211_S_ASSOC: 1216 if (ifp->if_flags & IFF_DEBUG) 1217 printf("%s: invalid transition %s -> %s\n", 1218 ifp->if_xname, ieee80211_state_name[ostate], 1219 ieee80211_state_name[nstate]); 1220 break; 1221 case IEEE80211_S_AUTH: 1222 IEEE80211_SEND_MGMT(ic, ni, 1223 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0); 1224 break; 1225 case IEEE80211_S_RUN: 1226 ieee80211_stop_ampdu_tx(ic, ni, mgt); 1227 ieee80211_ba_del(ni); 1228 IEEE80211_SEND_MGMT(ic, ni, 1229 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1); 1230 break; 1231 } 1232 break; 1233 case IEEE80211_S_RUN: 1234 switch (ostate) { 1235 case IEEE80211_S_INIT: 1236 if (ic->ic_opmode == IEEE80211_M_MONITOR) 1237 break; 1238 case IEEE80211_S_AUTH: 1239 case IEEE80211_S_RUN: 1240 if (ifp->if_flags & IFF_DEBUG) 1241 printf("%s: invalid transition %s -> %s\n", 1242 ifp->if_xname, ieee80211_state_name[ostate], 1243 ieee80211_state_name[nstate]); 1244 break; 1245 case IEEE80211_S_SCAN: /* adhoc/hostap mode */ 1246 case IEEE80211_S_ASSOC: /* infra mode */ 1247 if (ni->ni_txrate >= ni->ni_rates.rs_nrates) 1248 panic("%s: bogus xmit rate %u setup", 1249 __func__, ni->ni_txrate); 1250 if (ifp->if_flags & IFF_DEBUG) { 1251 printf("%s: %s with %s ssid ", 1252 ifp->if_xname, 1253 ic->ic_opmode == IEEE80211_M_STA ? 1254 "associated" : "synchronized", 1255 ether_sprintf(ni->ni_bssid)); 1256 ieee80211_print_essid(ic->ic_bss->ni_essid, 1257 ni->ni_esslen); 1258 rate = ni->ni_rates.rs_rates[ni->ni_txrate] & 1259 IEEE80211_RATE_VAL; 1260 printf(" channel %d", 1261 ieee80211_chan2ieee(ic, ni->ni_chan)); 1262 if (ni->ni_flags & IEEE80211_NODE_HT) 1263 printf(" start MCS %u", ni->ni_txmcs); 1264 else 1265 printf(" start %u%sMb", 1266 rate / 2, (rate & 1) ? ".5" : ""); 1267 printf(" %s preamble %s slot time%s%s\n", 1268 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? 1269 "short" : "long", 1270 (ic->ic_flags & IEEE80211_F_SHSLOT) ? 1271 "short" : "long", 1272 (ic->ic_flags & IEEE80211_F_USEPROT) ? 1273 " protection enabled" : "", 1274 (ni->ni_flags & IEEE80211_NODE_HT) ? 1275 " HT enabled" : ""); 1276 } 1277 if (!(ic->ic_flags & IEEE80211_F_RSNON)) { 1278 /* 1279 * NB: When RSN is enabled, we defer setting 1280 * the link up until the port is valid. 1281 */ 1282 ieee80211_set_link_state(ic, LINK_STATE_UP); 1283 ni->ni_assoc_fail = 0; 1284 } 1285 ic->ic_mgt_timer = 0; 1286 ieee80211_set_beacon_miss_threshold(ic); 1287 if_start(ifp); 1288 break; 1289 } 1290 break; 1291 } 1292 return 0; 1293 } 1294 1295 void 1296 ieee80211_set_link_state(struct ieee80211com *ic, int nstate) 1297 { 1298 struct ifnet *ifp = &ic->ic_if; 1299 1300 switch (ic->ic_opmode) { 1301 #ifndef IEEE80211_STA_ONLY 1302 case IEEE80211_M_IBSS: 1303 case IEEE80211_M_HOSTAP: 1304 nstate = LINK_STATE_UNKNOWN; 1305 break; 1306 #endif 1307 case IEEE80211_M_MONITOR: 1308 nstate = LINK_STATE_DOWN; 1309 break; 1310 default: 1311 break; 1312 } 1313 if (nstate != ifp->if_link_state) { 1314 ifp->if_link_state = nstate; 1315 if (LINK_STATE_IS_UP(nstate)) { 1316 struct if_ieee80211_data ifie; 1317 memset(&ifie, 0, sizeof(ifie)); 1318 ifie.ifie_nwid_len = ic->ic_bss->ni_esslen; 1319 memcpy(ifie.ifie_nwid, ic->ic_bss->ni_essid, 1320 sizeof(ifie.ifie_nwid)); 1321 memcpy(ifie.ifie_addr, ic->ic_bss->ni_bssid, 1322 sizeof(ifie.ifie_addr)); 1323 ifie.ifie_channel = ieee80211_chan2ieee(ic, 1324 ic->ic_bss->ni_chan); 1325 ifie.ifie_flags = ic->ic_flags; 1326 ifie.ifie_xflags = ic->ic_xflags; 1327 rtm_80211info(&ic->ic_if, &ifie); 1328 } 1329 if_link_state_change(ifp); 1330 } 1331 } 1332