1 /* $OpenBSD: ieee80211_proto.c,v 1.40 2009/03/26 20:34:54 damien 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 "bpfilter.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/mbuf.h> 42 #include <sys/kernel.h> 43 #include <sys/socket.h> 44 #include <sys/sockio.h> 45 #include <sys/endian.h> 46 #include <sys/errno.h> 47 #include <sys/proc.h> 48 #include <sys/sysctl.h> 49 50 #include <net/if.h> 51 #include <net/if_dl.h> 52 #include <net/if_media.h> 53 #include <net/if_arp.h> 54 #include <net/if_llc.h> 55 56 #if NBPFILTER > 0 57 #include <net/bpf.h> 58 #endif 59 60 #ifdef INET 61 #include <netinet/in.h> 62 #include <netinet/if_ether.h> 63 #endif 64 65 #include <net80211/ieee80211_var.h> 66 #include <net80211/ieee80211_priv.h> 67 68 #include <dev/rndvar.h> 69 70 const char * const ieee80211_mgt_subtype_name[] = { 71 "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp", 72 "probe_req", "probe_resp", "reserved#6", "reserved#7", 73 "beacon", "atim", "disassoc", "auth", 74 "deauth", "action", "action_noack", "reserved#15" 75 }; 76 const char * const ieee80211_state_name[IEEE80211_S_MAX] = { 77 "INIT", /* IEEE80211_S_INIT */ 78 "SCAN", /* IEEE80211_S_SCAN */ 79 "AUTH", /* IEEE80211_S_AUTH */ 80 "ASSOC", /* IEEE80211_S_ASSOC */ 81 "RUN" /* IEEE80211_S_RUN */ 82 }; 83 const char * const ieee80211_phymode_name[] = { 84 "auto", /* IEEE80211_MODE_AUTO */ 85 "11a", /* IEEE80211_MODE_11A */ 86 "11b", /* IEEE80211_MODE_11B */ 87 "11g", /* IEEE80211_MODE_11G */ 88 "turbo", /* IEEE80211_MODE_TURBO */ 89 }; 90 91 int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int); 92 void ieee80211_set_link_state(struct ieee80211com *, int); 93 94 void 95 ieee80211_proto_attach(struct ifnet *ifp) 96 { 97 struct ieee80211com *ic = (void *)ifp; 98 99 ifp->if_hdrlen = sizeof(struct ieee80211_frame); 100 101 #ifdef notdef 102 ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT; 103 #else 104 ic->ic_rtsthreshold = IEEE80211_RTS_MAX; 105 #endif 106 ic->ic_fragthreshold = 2346; /* XXX not used yet */ 107 ic->ic_fixed_rate = -1; /* no fixed rate */ 108 ic->ic_protmode = IEEE80211_PROT_CTSONLY; 109 110 /* protocol state change handler */ 111 ic->ic_newstate = ieee80211_newstate; 112 113 /* initialize management frame handlers */ 114 ic->ic_recv_mgmt = ieee80211_recv_mgmt; 115 ic->ic_send_mgmt = ieee80211_send_mgmt; 116 } 117 118 void 119 ieee80211_proto_detach(struct ifnet *ifp) 120 { 121 struct ieee80211com *ic = (void *)ifp; 122 123 IF_PURGE(&ic->ic_mgtq); 124 IF_PURGE(&ic->ic_pwrsaveq); 125 } 126 127 void 128 ieee80211_print_essid(const u_int8_t *essid, int len) 129 { 130 int i; 131 const u_int8_t *p; 132 133 if (len > IEEE80211_NWID_LEN) 134 len = IEEE80211_NWID_LEN; 135 /* determine printable or not */ 136 for (i = 0, p = essid; i < len; i++, p++) { 137 if (*p < ' ' || *p > 0x7e) 138 break; 139 } 140 if (i == len) { 141 printf("\""); 142 for (i = 0, p = essid; i < len; i++, p++) 143 printf("%c", *p); 144 printf("\""); 145 } else { 146 printf("0x"); 147 for (i = 0, p = essid; i < len; i++, p++) 148 printf("%02x", *p); 149 } 150 } 151 152 #ifdef IEEE80211_DEBUG 153 void 154 ieee80211_dump_pkt(const u_int8_t *buf, int len, int rate, int rssi) 155 { 156 struct ieee80211_frame *wh; 157 int i; 158 159 wh = (struct ieee80211_frame *)buf; 160 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 161 case IEEE80211_FC1_DIR_NODS: 162 printf("NODS %s", ether_sprintf(wh->i_addr2)); 163 printf("->%s", ether_sprintf(wh->i_addr1)); 164 printf("(%s)", ether_sprintf(wh->i_addr3)); 165 break; 166 case IEEE80211_FC1_DIR_TODS: 167 printf("TODS %s", ether_sprintf(wh->i_addr2)); 168 printf("->%s", ether_sprintf(wh->i_addr3)); 169 printf("(%s)", ether_sprintf(wh->i_addr1)); 170 break; 171 case IEEE80211_FC1_DIR_FROMDS: 172 printf("FRDS %s", ether_sprintf(wh->i_addr3)); 173 printf("->%s", ether_sprintf(wh->i_addr1)); 174 printf("(%s)", ether_sprintf(wh->i_addr2)); 175 break; 176 case IEEE80211_FC1_DIR_DSTODS: 177 printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1])); 178 printf("->%s", ether_sprintf(wh->i_addr3)); 179 printf("(%s", ether_sprintf(wh->i_addr2)); 180 printf("->%s)", ether_sprintf(wh->i_addr1)); 181 break; 182 } 183 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 184 case IEEE80211_FC0_TYPE_DATA: 185 printf(" data"); 186 break; 187 case IEEE80211_FC0_TYPE_MGT: 188 printf(" %s", ieee80211_mgt_subtype_name[ 189 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) 190 >> IEEE80211_FC0_SUBTYPE_SHIFT]); 191 break; 192 default: 193 printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK); 194 break; 195 } 196 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 197 printf(" WEP"); 198 if (rate >= 0) 199 printf(" %d%sM", rate / 2, (rate & 1) ? ".5" : ""); 200 if (rssi >= 0) 201 printf(" +%d", rssi); 202 printf("\n"); 203 if (len > 0) { 204 for (i = 0; i < len; i++) { 205 if ((i & 1) == 0) 206 printf(" "); 207 printf("%02x", buf[i]); 208 } 209 printf("\n"); 210 } 211 } 212 #endif 213 214 int 215 ieee80211_fix_rate(struct ieee80211com *ic, struct ieee80211_node *ni, 216 int flags) 217 { 218 #define RV(v) ((v) & IEEE80211_RATE_VAL) 219 int i, j, ignore, error; 220 int okrate, badrate, fixedrate; 221 const struct ieee80211_rateset *srs; 222 struct ieee80211_rateset *nrs; 223 u_int8_t r; 224 225 /* 226 * If the fixed rate check was requested but no fixed rate has been 227 * defined then just remove the check. 228 */ 229 if ((flags & IEEE80211_F_DOFRATE) && ic->ic_fixed_rate == -1) 230 flags &= ~IEEE80211_F_DOFRATE; 231 232 error = 0; 233 okrate = badrate = fixedrate = 0; 234 srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)]; 235 nrs = &ni->ni_rates; 236 for (i = 0; i < nrs->rs_nrates; ) { 237 ignore = 0; 238 if (flags & IEEE80211_F_DOSORT) { 239 /* 240 * Sort rates. 241 */ 242 for (j = i + 1; j < nrs->rs_nrates; j++) { 243 if (RV(nrs->rs_rates[i]) > 244 RV(nrs->rs_rates[j])) { 245 r = nrs->rs_rates[i]; 246 nrs->rs_rates[i] = nrs->rs_rates[j]; 247 nrs->rs_rates[j] = r; 248 } 249 } 250 } 251 r = nrs->rs_rates[i] & IEEE80211_RATE_VAL; 252 badrate = r; 253 if (flags & IEEE80211_F_DOFRATE) { 254 /* 255 * Check fixed rate is included. 256 */ 257 if (r == RV(srs->rs_rates[ic->ic_fixed_rate])) 258 fixedrate = r; 259 } 260 if (flags & IEEE80211_F_DONEGO) { 261 /* 262 * Check against supported rates. 263 */ 264 for (j = 0; j < srs->rs_nrates; j++) { 265 if (r == RV(srs->rs_rates[j])) { 266 /* 267 * Overwrite with the supported rate 268 * value so any basic rate bit is set. 269 * This insures that response we send 270 * to stations have the necessary basic 271 * rate bit set. 272 */ 273 nrs->rs_rates[i] = srs->rs_rates[j]; 274 break; 275 } 276 } 277 if (j == srs->rs_nrates) { 278 /* 279 * A rate in the node's rate set is not 280 * supported. If this is a basic rate and we 281 * are operating as an AP then this is an error. 282 * Otherwise we just discard/ignore the rate. 283 * Note that this is important for 11b stations 284 * when they want to associate with an 11g AP. 285 */ 286 #ifndef IEEE80211_STA_ONLY 287 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 288 (nrs->rs_rates[i] & IEEE80211_RATE_BASIC)) 289 error++; 290 #endif 291 ignore++; 292 } 293 } 294 if (flags & IEEE80211_F_DODEL) { 295 /* 296 * Delete unacceptable rates. 297 */ 298 if (ignore) { 299 nrs->rs_nrates--; 300 for (j = i; j < nrs->rs_nrates; j++) 301 nrs->rs_rates[j] = nrs->rs_rates[j + 1]; 302 nrs->rs_rates[j] = 0; 303 continue; 304 } 305 } 306 if (!ignore) 307 okrate = nrs->rs_rates[i]; 308 i++; 309 } 310 if (okrate == 0 || error != 0 || 311 ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0)) 312 return badrate | IEEE80211_RATE_BASIC; 313 else 314 return RV(okrate); 315 #undef RV 316 } 317 318 /* 319 * Reset 11g-related state. 320 */ 321 void 322 ieee80211_reset_erp(struct ieee80211com *ic) 323 { 324 ic->ic_flags &= ~IEEE80211_F_USEPROT; 325 ic->ic_nonerpsta = 0; 326 ic->ic_longslotsta = 0; 327 328 /* 329 * Enable short slot time iff: 330 * - we're operating in 802.11a or 331 * - we're operating in 802.11g and we're not in IBSS mode and 332 * the device supports short slot time 333 */ 334 ieee80211_set_shortslottime(ic, 335 ic->ic_curmode == IEEE80211_MODE_11A 336 #ifndef IEEE80211_STA_ONLY 337 || 338 (ic->ic_curmode == IEEE80211_MODE_11G && 339 ic->ic_opmode == IEEE80211_M_HOSTAP && 340 (ic->ic_caps & IEEE80211_C_SHSLOT)) 341 #endif 342 ); 343 344 if (ic->ic_curmode == IEEE80211_MODE_11A || 345 (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) 346 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 347 else 348 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 349 } 350 351 /* 352 * Set the short slot time state and notify the driver. 353 */ 354 void 355 ieee80211_set_shortslottime(struct ieee80211com *ic, int on) 356 { 357 if (on) 358 ic->ic_flags |= IEEE80211_F_SHSLOT; 359 else 360 ic->ic_flags &= ~IEEE80211_F_SHSLOT; 361 362 /* notify the driver */ 363 if (ic->ic_updateslot != NULL) 364 ic->ic_updateslot(ic); 365 } 366 367 /* 368 * This function is called by the 802.1X PACP machine (via an ioctl) when 369 * the transmit key machine (4-Way Handshake for 802.11) should run. 370 */ 371 int 372 ieee80211_keyrun(struct ieee80211com *ic, u_int8_t *macaddr) 373 { 374 #ifndef IEEE80211_STA_ONLY 375 struct ieee80211_node *ni; 376 struct ieee80211_pmk *pmk; 377 #endif 378 379 /* STA must be associated or AP must be ready */ 380 if (ic->ic_state != IEEE80211_S_RUN || 381 !(ic->ic_flags & IEEE80211_F_RSNON)) 382 return ENETDOWN; 383 384 #ifndef IEEE80211_STA_ONLY 385 if (ic->ic_opmode == IEEE80211_M_STA) 386 #endif 387 return 0; /* supplicant only, do nothing */ 388 389 #ifndef IEEE80211_STA_ONLY 390 /* find the STA with which we must start the key exchange */ 391 if ((ni = ieee80211_find_node(ic, macaddr)) == NULL) { 392 DPRINTF(("no node found for %s\n", ether_sprintf(macaddr))); 393 return EINVAL; 394 } 395 /* check that the STA is in the correct state */ 396 if (ni->ni_state != IEEE80211_STA_ASSOC || 397 ni->ni_rsn_state != RSNA_AUTHENTICATION_2) { 398 DPRINTF(("unexpected in state %d\n", ni->ni_rsn_state)); 399 return EINVAL; 400 } 401 ni->ni_rsn_state = RSNA_INITPMK; 402 403 /* make sure a PMK is available for this STA, otherwise deauth it */ 404 if ((pmk = ieee80211_pmksa_find(ic, ni, NULL)) == NULL) { 405 DPRINTF(("no PMK available for %s\n", ether_sprintf(macaddr))); 406 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH, 407 IEEE80211_REASON_AUTH_LEAVE); 408 ieee80211_node_leave(ic, ni); 409 return EINVAL; 410 } 411 memcpy(ni->ni_pmk, pmk->pmk_key, IEEE80211_PMK_LEN); 412 memcpy(ni->ni_pmkid, pmk->pmk_pmkid, IEEE80211_PMKID_LEN); 413 ni->ni_flags |= IEEE80211_NODE_PMK; 414 415 /* initiate key exchange (4-Way Handshake) with STA */ 416 return ieee80211_send_4way_msg1(ic, ni); 417 #endif /* IEEE80211_STA_ONLY */ 418 } 419 420 #ifndef IEEE80211_STA_ONLY 421 /* 422 * Initiate a group key handshake with a node. 423 */ 424 static void 425 ieee80211_node_gtk_rekey(void *arg, struct ieee80211_node *ni) 426 { 427 struct ieee80211com *ic = arg; 428 429 if (ni->ni_state != IEEE80211_STA_ASSOC || 430 ni->ni_rsn_gstate != RSNA_IDLE) 431 return; 432 433 /* initiate a group key handshake with STA */ 434 if (ieee80211_send_group_msg1(ic, ni) == 0) { 435 ni->ni_flags |= IEEE80211_NODE_REKEY; 436 ic->ic_rsn_keydonesta++; 437 } 438 } 439 440 /* 441 * This function is called in HostAP mode when the group key needs to be 442 * changed. 443 */ 444 void 445 ieee80211_setkeys(struct ieee80211com *ic) 446 { 447 struct ieee80211_key *k; 448 u_int8_t kid; 449 450 /* Swap(GM, GN) */ 451 kid = (ic->ic_def_txkey == 1) ? 2 : 1; 452 k = &ic->ic_nw_keys[kid]; 453 memset(k, 0, sizeof(*k)); 454 k->k_id = kid; 455 k->k_cipher = ic->ic_bss->ni_rsngroupcipher; 456 k->k_flags = IEEE80211_KEY_GROUP | IEEE80211_KEY_TX; 457 k->k_len = ieee80211_cipher_keylen(k->k_cipher); 458 arc4random_buf(k->k_key, k->k_len); 459 460 if (ic->ic_caps & IEEE80211_C_MFP) { 461 /* Swap(GM_igtk, GN_igtk) */ 462 kid = (ic->ic_igtk_kid == 4) ? 5 : 4; 463 k = &ic->ic_nw_keys[kid]; 464 memset(k, 0, sizeof(*k)); 465 k->k_id = kid; 466 k->k_cipher = ic->ic_bss->ni_rsngroupmgmtcipher; 467 k->k_flags = IEEE80211_KEY_IGTK | IEEE80211_KEY_TX; 468 k->k_len = 16; 469 arc4random_buf(k->k_key, k->k_len); 470 } 471 472 ic->ic_rsn_keydonesta = 0; 473 ieee80211_iterate_nodes(ic, ieee80211_node_gtk_rekey, ic); 474 } 475 476 /* 477 * The group key handshake has been completed with all associated stations. 478 */ 479 void 480 ieee80211_setkeysdone(struct ieee80211com *ic) 481 { 482 u_int8_t kid; 483 484 /* install GTK */ 485 kid = (ic->ic_def_txkey == 1) ? 2 : 1; 486 if ((*ic->ic_set_key)(ic, ic->ic_bss, &ic->ic_nw_keys[kid]) == 0) 487 ic->ic_def_txkey = kid; 488 489 if (ic->ic_caps & IEEE80211_C_MFP) { 490 /* install IGTK */ 491 kid = (ic->ic_igtk_kid == 4) ? 5 : 4; 492 if ((*ic->ic_set_key)(ic, ic->ic_bss, 493 &ic->ic_nw_keys[kid]) == 0) 494 ic->ic_igtk_kid = kid; 495 } 496 } 497 498 /* 499 * Group key lifetime has expired, update it. 500 */ 501 void 502 ieee80211_gtk_rekey_timeout(void *arg) 503 { 504 struct ieee80211com *ic = arg; 505 int s; 506 507 s = splnet(); 508 ieee80211_setkeys(ic); 509 splx(s); 510 511 /* re-schedule a GTK rekeying after 3600s */ 512 timeout_add_sec(&ic->ic_rsn_timeout, 3600); 513 } 514 515 void 516 ieee80211_sa_query_timeout(void *arg) 517 { 518 struct ieee80211_node *ni = arg; 519 struct ieee80211com *ic = ni->ni_ic; 520 int s; 521 522 s = splnet(); 523 if (++ni->ni_sa_query_count >= 3) { 524 ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY; 525 ni->ni_flags |= IEEE80211_NODE_SA_QUERY_FAILED; 526 } else /* retry SA Query Request */ 527 ieee80211_sa_query_request(ic, ni); 528 splx(s); 529 } 530 531 /* 532 * Request that a SA Query Request frame be sent to a specified peer STA 533 * to which the STA is associated. 534 */ 535 void 536 ieee80211_sa_query_request(struct ieee80211com *ic, struct ieee80211_node *ni) 537 { 538 /* MLME-SAQuery.request */ 539 540 if (!(ni->ni_flags & IEEE80211_NODE_SA_QUERY)) { 541 ni->ni_flags |= IEEE80211_NODE_SA_QUERY; 542 ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY_FAILED; 543 ni->ni_sa_query_count = 0; 544 } 545 /* generate new Transaction Identifier */ 546 ni->ni_sa_query_trid++; 547 548 /* send SA Query Request */ 549 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_SA_QUERY, 550 IEEE80211_ACTION_SA_QUERY_REQ, 0); 551 timeout_add_msec(&ni->ni_sa_query_to, 10); 552 } 553 #endif /* IEEE80211_STA_ONLY */ 554 555 #ifndef IEEE80211_NO_HT 556 void 557 ieee80211_tx_ba_timeout(void *arg) 558 { 559 struct ieee80211_tx_ba *ba = arg; 560 struct ieee80211_node *ni = ba->ba_ni; 561 struct ieee80211com *ic = ni->ni_ic; 562 u_int8_t tid; 563 int s; 564 565 s = splnet(); 566 if (ba->ba_state == IEEE80211_BA_REQUESTED) { 567 /* MLME-ADDBA.confirm(TIMEOUT) */ 568 ba->ba_state = IEEE80211_BA_INIT; 569 570 } else if (ba->ba_state == IEEE80211_BA_AGREED) { 571 /* Block Ack inactivity timeout */ 572 tid = ((caddr_t)ba - (caddr_t)ni->ni_tx_ba) / sizeof(*ba); 573 ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT, 574 1, tid); 575 } 576 splx(s); 577 } 578 579 void 580 ieee80211_rx_ba_timeout(void *arg) 581 { 582 struct ieee80211_rx_ba *ba = arg; 583 struct ieee80211_node *ni = ba->ba_ni; 584 struct ieee80211com *ic = ni->ni_ic; 585 u_int8_t tid; 586 int s; 587 588 s = splnet(); 589 590 /* Block Ack inactivity timeout */ 591 tid = ((caddr_t)ba - (caddr_t)ni->ni_rx_ba) / sizeof(*ba); 592 ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT, 0, tid); 593 594 splx(s); 595 } 596 597 /* 598 * Request initiation of Block Ack with the specified peer. 599 */ 600 int 601 ieee80211_addba_request(struct ieee80211com *ic, struct ieee80211_node *ni, 602 u_int16_t ssn, u_int8_t tid) 603 { 604 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 605 606 /* MLME-ADDBA.request */ 607 608 /* setup Block Ack */ 609 ba->ba_state = IEEE80211_BA_REQUESTED; 610 ba->ba_token = ic->ic_dialog_token++; 611 ba->ba_timeout_val = IEEE80211_BA_MAX_TIMEOUT; 612 timeout_set(&ba->ba_to, ieee80211_tx_ba_timeout, ba); 613 ba->ba_winsize = IEEE80211_BA_MAX_WINSZ; 614 ba->ba_winstart = ssn; 615 ba->ba_winend = (ba->ba_winstart + ba->ba_winsize - 1) & 0xfff; 616 617 timeout_add_sec(&ba->ba_to, 1); /* dot11ADDBAResponseTimeout */ 618 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA, 619 IEEE80211_ACTION_ADDBA_REQ, tid); 620 return 0; 621 } 622 623 /* 624 * Request the deletion of Block Ack with a peer. 625 */ 626 void 627 ieee80211_delba_request(struct ieee80211com *ic, struct ieee80211_node *ni, 628 u_int16_t reason, u_int8_t dir, u_int8_t tid) 629 { 630 /* MLME-DELBA.request */ 631 632 /* transmit a DELBA frame */ 633 IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA, 634 IEEE80211_ACTION_DELBA, reason << 16 | dir << 8 | tid); 635 if (dir) { 636 /* MLME-DELBA.confirm(Originator) */ 637 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 638 639 if (ic->ic_ampdu_tx_stop != NULL) 640 ic->ic_ampdu_tx_stop(ic, ni, tid); 641 642 ba->ba_state = IEEE80211_BA_INIT; 643 /* stop Block Ack inactivity timer */ 644 timeout_del(&ba->ba_to); 645 } else { 646 /* MLME-DELBA.confirm(Recipient) */ 647 struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid]; 648 int i; 649 650 if (ic->ic_ampdu_rx_stop != NULL) 651 ic->ic_ampdu_rx_stop(ic, ni, tid); 652 653 ba->ba_state = IEEE80211_BA_INIT; 654 /* stop Block Ack inactivity timer */ 655 timeout_del(&ba->ba_to); 656 657 if (ba->ba_buf != NULL) { 658 /* free all MSDUs stored in reordering buffer */ 659 for (i = 0; i < IEEE80211_BA_MAX_WINSZ; i++) 660 if (ba->ba_buf[i].m != NULL) 661 m_freem(ba->ba_buf[i].m); 662 /* free reordering buffer */ 663 free(ba->ba_buf, M_DEVBUF); 664 ba->ba_buf = NULL; 665 } 666 } 667 } 668 #endif /* !IEEE80211_NO_HT */ 669 670 void 671 ieee80211_auth_open(struct ieee80211com *ic, const struct ieee80211_frame *wh, 672 struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi, u_int16_t seq, 673 u_int16_t status) 674 { 675 struct ifnet *ifp = &ic->ic_if; 676 switch (ic->ic_opmode) { 677 #ifndef IEEE80211_STA_ONLY 678 case IEEE80211_M_IBSS: 679 if (ic->ic_state != IEEE80211_S_RUN || 680 seq != IEEE80211_AUTH_OPEN_REQUEST) { 681 DPRINTF(("discard auth from %s; state %u, seq %u\n", 682 ether_sprintf((u_int8_t *)wh->i_addr2), 683 ic->ic_state, seq)); 684 ic->ic_stats.is_rx_bad_auth++; 685 return; 686 } 687 ieee80211_new_state(ic, IEEE80211_S_AUTH, 688 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 689 break; 690 691 case IEEE80211_M_AHDEMO: 692 /* should not come here */ 693 break; 694 695 case IEEE80211_M_HOSTAP: 696 if (ic->ic_state != IEEE80211_S_RUN || 697 seq != IEEE80211_AUTH_OPEN_REQUEST) { 698 DPRINTF(("discard auth from %s; state %u, seq %u\n", 699 ether_sprintf((u_int8_t *)wh->i_addr2), 700 ic->ic_state, seq)); 701 ic->ic_stats.is_rx_bad_auth++; 702 return; 703 } 704 if (ni == ic->ic_bss) { 705 ni = ieee80211_alloc_node(ic, wh->i_addr2); 706 if (ni == NULL) { 707 ic->ic_stats.is_rx_nodealloc++; 708 return; 709 } 710 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 711 ni->ni_rssi = rxi->rxi_rssi; 712 ni->ni_rstamp = rxi->rxi_tstamp; 713 ni->ni_chan = ic->ic_bss->ni_chan; 714 } 715 IEEE80211_SEND_MGMT(ic, ni, 716 IEEE80211_FC0_SUBTYPE_AUTH, seq + 1); 717 if (ifp->if_flags & IFF_DEBUG) 718 printf("%s: station %s %s authenticated (open)\n", 719 ifp->if_xname, 720 ether_sprintf((u_int8_t *)ni->ni_macaddr), 721 ni->ni_state != IEEE80211_STA_CACHE ? 722 "newly" : "already"); 723 ieee80211_node_newstate(ni, IEEE80211_STA_AUTH); 724 break; 725 #endif /* IEEE80211_STA_ONLY */ 726 727 case IEEE80211_M_STA: 728 if (ic->ic_state != IEEE80211_S_AUTH || 729 seq != IEEE80211_AUTH_OPEN_RESPONSE) { 730 ic->ic_stats.is_rx_bad_auth++; 731 DPRINTF(("discard auth from %s; state %u, seq %u\n", 732 ether_sprintf((u_int8_t *)wh->i_addr2), 733 ic->ic_state, seq)); 734 return; 735 } 736 if (ic->ic_flags & IEEE80211_F_RSNON) { 737 /* XXX not here! */ 738 ic->ic_bss->ni_flags &= ~IEEE80211_NODE_TXRXPROT; 739 ic->ic_bss->ni_port_valid = 0; 740 ic->ic_bss->ni_replaycnt_ok = 0; 741 (*ic->ic_delete_key)(ic, ic->ic_bss, 742 &ic->ic_bss->ni_pairwise_key); 743 } 744 if (status != 0) { 745 if (ifp->if_flags & IFF_DEBUG) 746 printf("%s: open authentication failed " 747 "(reason %d) for %s\n", ifp->if_xname, 748 status, 749 ether_sprintf((u_int8_t *)wh->i_addr3)); 750 if (ni != ic->ic_bss) 751 ni->ni_fails++; 752 ic->ic_stats.is_rx_auth_fail++; 753 return; 754 } 755 ieee80211_new_state(ic, IEEE80211_S_ASSOC, 756 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 757 break; 758 default: 759 break; 760 } 761 } 762 763 int 764 ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, 765 int mgt) 766 { 767 struct ifnet *ifp = &ic->ic_if; 768 struct ieee80211_node *ni; 769 enum ieee80211_state ostate; 770 u_int rate; 771 #ifndef IEEE80211_STA_ONLY 772 int s; 773 #endif 774 775 ostate = ic->ic_state; 776 DPRINTF(("%s -> %s\n", ieee80211_state_name[ostate], 777 ieee80211_state_name[nstate])); 778 ic->ic_state = nstate; /* state transition */ 779 ni = ic->ic_bss; /* NB: no reference held */ 780 if (ostate == IEEE80211_S_RUN) 781 ieee80211_set_link_state(ic, LINK_STATE_DOWN); 782 switch (nstate) { 783 case IEEE80211_S_INIT: 784 switch (ostate) { 785 case IEEE80211_S_INIT: 786 break; 787 case IEEE80211_S_RUN: 788 switch (ic->ic_opmode) { 789 case IEEE80211_M_STA: 790 IEEE80211_SEND_MGMT(ic, ni, 791 IEEE80211_FC0_SUBTYPE_DISASSOC, 792 IEEE80211_REASON_ASSOC_LEAVE); 793 break; 794 #ifndef IEEE80211_STA_ONLY 795 case IEEE80211_M_HOSTAP: 796 s = splnet(); 797 RB_FOREACH(ni, ieee80211_tree, &ic->ic_tree) { 798 if (ni->ni_associd == 0) 799 continue; 800 IEEE80211_SEND_MGMT(ic, ni, 801 IEEE80211_FC0_SUBTYPE_DISASSOC, 802 IEEE80211_REASON_ASSOC_LEAVE); 803 } 804 splx(s); 805 break; 806 #endif 807 default: 808 break; 809 } 810 /* FALLTHROUGH */ 811 case IEEE80211_S_ASSOC: 812 switch (ic->ic_opmode) { 813 case IEEE80211_M_STA: 814 IEEE80211_SEND_MGMT(ic, ni, 815 IEEE80211_FC0_SUBTYPE_DEAUTH, 816 IEEE80211_REASON_AUTH_LEAVE); 817 break; 818 #ifndef IEEE80211_STA_ONLY 819 case IEEE80211_M_HOSTAP: 820 s = splnet(); 821 RB_FOREACH(ni, ieee80211_tree, &ic->ic_tree) { 822 IEEE80211_SEND_MGMT(ic, ni, 823 IEEE80211_FC0_SUBTYPE_DEAUTH, 824 IEEE80211_REASON_AUTH_LEAVE); 825 } 826 splx(s); 827 break; 828 #endif 829 default: 830 break; 831 } 832 /* FALLTHROUGH */ 833 case IEEE80211_S_AUTH: 834 case IEEE80211_S_SCAN: 835 #ifndef IEEE80211_STA_ONLY 836 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 837 timeout_del(&ic->ic_rsn_timeout); 838 #endif 839 ic->ic_mgt_timer = 0; 840 IF_PURGE(&ic->ic_mgtq); 841 IF_PURGE(&ic->ic_pwrsaveq); 842 ieee80211_free_allnodes(ic); 843 break; 844 } 845 break; 846 case IEEE80211_S_SCAN: 847 ic->ic_flags &= ~IEEE80211_F_SIBSS; 848 /* initialize bss for probe request */ 849 IEEE80211_ADDR_COPY(ni->ni_macaddr, etherbroadcastaddr); 850 IEEE80211_ADDR_COPY(ni->ni_bssid, etherbroadcastaddr); 851 ni->ni_rates = ic->ic_sup_rates[ 852 ieee80211_chan2mode(ic, ni->ni_chan)]; 853 ni->ni_associd = 0; 854 ni->ni_rstamp = 0; 855 switch (ostate) { 856 case IEEE80211_S_INIT: 857 #ifndef IEEE80211_STA_ONLY 858 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 859 ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 860 /* 861 * AP operation and we already have a channel; 862 * bypass the scan and startup immediately. 863 */ 864 ieee80211_create_ibss(ic, ic->ic_des_chan); 865 } else 866 #endif 867 ieee80211_begin_scan(ifp); 868 break; 869 case IEEE80211_S_SCAN: 870 /* scan next */ 871 if (ic->ic_flags & IEEE80211_F_ASCAN) { 872 IEEE80211_SEND_MGMT(ic, ni, 873 IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0); 874 } 875 break; 876 case IEEE80211_S_RUN: 877 /* beacon miss */ 878 if (ifp->if_flags & IFF_DEBUG) { 879 /* XXX bssid clobbered above */ 880 printf("%s: no recent beacons from %s;" 881 " rescanning\n", ifp->if_xname, 882 ether_sprintf(ic->ic_bss->ni_bssid)); 883 } 884 ieee80211_free_allnodes(ic); 885 /* FALLTHROUGH */ 886 case IEEE80211_S_AUTH: 887 case IEEE80211_S_ASSOC: 888 /* timeout restart scan */ 889 ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 890 if (ni != NULL) 891 ni->ni_fails++; 892 ieee80211_begin_scan(ifp); 893 break; 894 } 895 break; 896 case IEEE80211_S_AUTH: 897 switch (ostate) { 898 case IEEE80211_S_INIT: 899 DPRINTF(("invalid transition\n")); 900 break; 901 case IEEE80211_S_SCAN: 902 IEEE80211_SEND_MGMT(ic, ni, 903 IEEE80211_FC0_SUBTYPE_AUTH, 1); 904 break; 905 case IEEE80211_S_AUTH: 906 case IEEE80211_S_ASSOC: 907 switch (mgt) { 908 case IEEE80211_FC0_SUBTYPE_AUTH: 909 /* ??? */ 910 IEEE80211_SEND_MGMT(ic, ni, 911 IEEE80211_FC0_SUBTYPE_AUTH, 2); 912 break; 913 case IEEE80211_FC0_SUBTYPE_DEAUTH: 914 /* ignore and retry scan on timeout */ 915 break; 916 } 917 break; 918 case IEEE80211_S_RUN: 919 switch (mgt) { 920 case IEEE80211_FC0_SUBTYPE_AUTH: 921 IEEE80211_SEND_MGMT(ic, ni, 922 IEEE80211_FC0_SUBTYPE_AUTH, 2); 923 ic->ic_state = ostate; /* stay RUN */ 924 break; 925 case IEEE80211_FC0_SUBTYPE_DEAUTH: 926 /* try to reauth */ 927 IEEE80211_SEND_MGMT(ic, ni, 928 IEEE80211_FC0_SUBTYPE_AUTH, 1); 929 break; 930 } 931 break; 932 } 933 break; 934 case IEEE80211_S_ASSOC: 935 switch (ostate) { 936 case IEEE80211_S_INIT: 937 case IEEE80211_S_SCAN: 938 case IEEE80211_S_ASSOC: 939 DPRINTF(("invalid transition\n")); 940 break; 941 case IEEE80211_S_AUTH: 942 IEEE80211_SEND_MGMT(ic, ni, 943 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0); 944 break; 945 case IEEE80211_S_RUN: 946 IEEE80211_SEND_MGMT(ic, ni, 947 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1); 948 break; 949 } 950 break; 951 case IEEE80211_S_RUN: 952 ieee80211_set_link_state(ic, LINK_STATE_UP); 953 switch (ostate) { 954 case IEEE80211_S_INIT: 955 case IEEE80211_S_AUTH: 956 case IEEE80211_S_RUN: 957 DPRINTF(("invalid transition\n")); 958 break; 959 case IEEE80211_S_SCAN: /* adhoc/hostap mode */ 960 case IEEE80211_S_ASSOC: /* infra mode */ 961 if (ni->ni_txrate >= ni->ni_rates.rs_nrates) 962 panic("%s: bogus xmit rate %u setup\n", 963 __func__, ni->ni_txrate); 964 if (ifp->if_flags & IFF_DEBUG) { 965 printf("%s: %s with %s ssid ", 966 ifp->if_xname, 967 ic->ic_opmode == IEEE80211_M_STA ? 968 "associated" : "synchronized", 969 ether_sprintf(ni->ni_bssid)); 970 ieee80211_print_essid(ic->ic_bss->ni_essid, 971 ni->ni_esslen); 972 rate = ni->ni_rates.rs_rates[ni->ni_txrate] & 973 IEEE80211_RATE_VAL; 974 printf(" channel %d start %u%sMb", 975 ieee80211_chan2ieee(ic, ni->ni_chan), 976 rate / 2, (rate & 1) ? ".5" : ""); 977 printf(" %s preamble %s slot time%s\n", 978 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? 979 "short" : "long", 980 (ic->ic_flags & IEEE80211_F_SHSLOT) ? 981 "short" : "long", 982 (ic->ic_flags & IEEE80211_F_USEPROT) ? 983 " protection enabled" : ""); 984 } 985 ic->ic_mgt_timer = 0; 986 (*ifp->if_start)(ifp); 987 break; 988 } 989 break; 990 } 991 return 0; 992 } 993 994 void 995 ieee80211_set_link_state(struct ieee80211com *ic, int nstate) 996 { 997 struct ifnet *ifp = &ic->ic_if; 998 999 switch (ic->ic_opmode) { 1000 #ifndef IEEE80211_STA_ONLY 1001 case IEEE80211_M_IBSS: 1002 case IEEE80211_M_HOSTAP: 1003 nstate = LINK_STATE_UNKNOWN; 1004 break; 1005 #endif 1006 case IEEE80211_M_MONITOR: 1007 nstate = LINK_STATE_DOWN; 1008 break; 1009 default: 1010 break; 1011 } 1012 if (nstate != ifp->if_link_state) { 1013 ifp->if_link_state = nstate; 1014 if_link_state_change(ifp); 1015 } 1016 } 1017