1 /* $OpenBSD: ieee80211_node.c,v 1.112 2017/01/16 09:35:43 stsp Exp $ */ 2 /* $NetBSD: ieee80211_node.c,v 1.14 2004/05/09 09:18:47 dyoung Exp $ */ 3 4 /*- 5 * Copyright (c) 2001 Atsushi Onoe 6 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 7 * Copyright (c) 2008 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 #include "bridge.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/mbuf.h> 38 #include <sys/malloc.h> 39 #include <sys/kernel.h> 40 #include <sys/socket.h> 41 #include <sys/sockio.h> 42 #include <sys/endian.h> 43 #include <sys/errno.h> 44 #include <sys/sysctl.h> 45 #include <sys/tree.h> 46 47 #include <net/if.h> 48 #include <net/if_dl.h> 49 #include <net/if_media.h> 50 51 #include <netinet/in.h> 52 #include <netinet/if_ether.h> 53 54 #if NBRIDGE > 0 55 #include <net/if_bridge.h> 56 #endif 57 58 #include <net80211/ieee80211_var.h> 59 #include <net80211/ieee80211_priv.h> 60 61 struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *); 62 void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *); 63 void ieee80211_node_copy(struct ieee80211com *, struct ieee80211_node *, 64 const struct ieee80211_node *); 65 void ieee80211_choose_rsnparams(struct ieee80211com *); 66 u_int8_t ieee80211_node_getrssi(struct ieee80211com *, 67 const struct ieee80211_node *); 68 void ieee80211_setup_node(struct ieee80211com *, struct ieee80211_node *, 69 const u_int8_t *); 70 void ieee80211_free_node(struct ieee80211com *, struct ieee80211_node *); 71 void ieee80211_ba_del(struct ieee80211_node *); 72 struct ieee80211_node *ieee80211_alloc_node_helper(struct ieee80211com *); 73 void ieee80211_node_cleanup(struct ieee80211com *, struct ieee80211_node *); 74 void ieee80211_needs_auth(struct ieee80211com *, struct ieee80211_node *); 75 #ifndef IEEE80211_STA_ONLY 76 void ieee80211_node_join_ht(struct ieee80211com *, struct ieee80211_node *); 77 void ieee80211_node_join_rsn(struct ieee80211com *, struct ieee80211_node *); 78 void ieee80211_node_join_11g(struct ieee80211com *, struct ieee80211_node *); 79 void ieee80211_node_leave_ht(struct ieee80211com *, struct ieee80211_node *); 80 void ieee80211_node_leave_rsn(struct ieee80211com *, struct ieee80211_node *); 81 void ieee80211_node_leave_11g(struct ieee80211com *, struct ieee80211_node *); 82 void ieee80211_inact_timeout(void *); 83 void ieee80211_node_cache_timeout(void *); 84 #endif 85 86 #ifndef IEEE80211_STA_ONLY 87 void 88 ieee80211_inact_timeout(void *arg) 89 { 90 struct ieee80211com *ic = arg; 91 struct ieee80211_node *ni, *next_ni; 92 int s; 93 94 s = splnet(); 95 for (ni = RBT_MIN(ieee80211_tree, &ic->ic_tree); 96 ni != NULL; ni = next_ni) { 97 next_ni = RBT_NEXT(ieee80211_tree, ni); 98 if (ni->ni_refcnt > 0) 99 continue; 100 if (ni->ni_inact < IEEE80211_INACT_MAX) 101 ni->ni_inact++; 102 } 103 splx(s); 104 105 timeout_add_sec(&ic->ic_inact_timeout, IEEE80211_INACT_WAIT); 106 } 107 108 void 109 ieee80211_node_cache_timeout(void *arg) 110 { 111 struct ieee80211com *ic = arg; 112 113 ieee80211_clean_nodes(ic, 1); 114 timeout_add_sec(&ic->ic_node_cache_timeout, IEEE80211_CACHE_WAIT); 115 } 116 #endif 117 118 void 119 ieee80211_node_attach(struct ifnet *ifp) 120 { 121 struct ieee80211com *ic = (void *)ifp; 122 #ifndef IEEE80211_STA_ONLY 123 int size; 124 #endif 125 126 RBT_INIT(ieee80211_tree, &ic->ic_tree); 127 ic->ic_node_alloc = ieee80211_node_alloc; 128 ic->ic_node_free = ieee80211_node_free; 129 ic->ic_node_copy = ieee80211_node_copy; 130 ic->ic_node_getrssi = ieee80211_node_getrssi; 131 ic->ic_scangen = 1; 132 ic->ic_max_nnodes = ieee80211_cache_size; 133 134 if (ic->ic_max_aid == 0) 135 ic->ic_max_aid = IEEE80211_AID_DEF; 136 else if (ic->ic_max_aid > IEEE80211_AID_MAX) 137 ic->ic_max_aid = IEEE80211_AID_MAX; 138 #ifndef IEEE80211_STA_ONLY 139 size = howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t); 140 ic->ic_aid_bitmap = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO); 141 if (ic->ic_aid_bitmap == NULL) { 142 /* XXX no way to recover */ 143 printf("%s: no memory for AID bitmap!\n", __func__); 144 ic->ic_max_aid = 0; 145 } 146 if (ic->ic_caps & (IEEE80211_C_HOSTAP | IEEE80211_C_IBSS)) { 147 ic->ic_tim_len = howmany(ic->ic_max_aid, 8); 148 ic->ic_tim_bitmap = malloc(ic->ic_tim_len, M_DEVBUF, 149 M_NOWAIT | M_ZERO); 150 if (ic->ic_tim_bitmap == NULL) { 151 printf("%s: no memory for TIM bitmap!\n", __func__); 152 ic->ic_tim_len = 0; 153 } else 154 ic->ic_set_tim = ieee80211_set_tim; 155 timeout_set(&ic->ic_rsn_timeout, 156 ieee80211_gtk_rekey_timeout, ic); 157 timeout_set(&ic->ic_inact_timeout, 158 ieee80211_inact_timeout, ic); 159 timeout_set(&ic->ic_node_cache_timeout, 160 ieee80211_node_cache_timeout, ic); 161 } 162 #endif 163 } 164 165 struct ieee80211_node * 166 ieee80211_alloc_node_helper(struct ieee80211com *ic) 167 { 168 struct ieee80211_node *ni; 169 if (ic->ic_nnodes >= ic->ic_max_nnodes) 170 ieee80211_clean_nodes(ic, 0); 171 if (ic->ic_nnodes >= ic->ic_max_nnodes) 172 return NULL; 173 ni = (*ic->ic_node_alloc)(ic); 174 return ni; 175 } 176 177 void 178 ieee80211_node_lateattach(struct ifnet *ifp) 179 { 180 struct ieee80211com *ic = (void *)ifp; 181 struct ieee80211_node *ni; 182 183 ni = ieee80211_alloc_node_helper(ic); 184 if (ni == NULL) 185 panic("unable to setup inital BSS node"); 186 ni->ni_chan = IEEE80211_CHAN_ANYC; 187 ic->ic_bss = ieee80211_ref_node(ni); 188 ic->ic_txpower = IEEE80211_TXPOWER_MAX; 189 #ifndef IEEE80211_STA_ONLY 190 mq_init(&ni->ni_savedq, IEEE80211_PS_MAX_QUEUE, IPL_NET); 191 #endif 192 } 193 194 void 195 ieee80211_node_detach(struct ifnet *ifp) 196 { 197 struct ieee80211com *ic = (void *)ifp; 198 199 if (ic->ic_bss != NULL) { 200 (*ic->ic_node_free)(ic, ic->ic_bss); 201 ic->ic_bss = NULL; 202 } 203 ieee80211_free_allnodes(ic); 204 #ifndef IEEE80211_STA_ONLY 205 if (ic->ic_aid_bitmap != NULL) 206 free(ic->ic_aid_bitmap, M_DEVBUF, 0); 207 if (ic->ic_tim_bitmap != NULL) 208 free(ic->ic_tim_bitmap, M_DEVBUF, 0); 209 timeout_del(&ic->ic_inact_timeout); 210 timeout_del(&ic->ic_node_cache_timeout); 211 timeout_del(&ic->ic_tkip_micfail_timeout); 212 #endif 213 timeout_del(&ic->ic_rsn_timeout); 214 } 215 216 /* 217 * AP scanning support. 218 */ 219 220 /* 221 * Initialize the active channel set based on the set 222 * of available channels and the current PHY mode. 223 */ 224 void 225 ieee80211_reset_scan(struct ifnet *ifp) 226 { 227 struct ieee80211com *ic = (void *)ifp; 228 229 memcpy(ic->ic_chan_scan, ic->ic_chan_active, 230 sizeof(ic->ic_chan_active)); 231 /* NB: hack, setup so next_scan starts with the first channel */ 232 if (ic->ic_bss != NULL && ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC) 233 ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX]; 234 } 235 236 /* 237 * Begin an active scan. 238 */ 239 void 240 ieee80211_begin_scan(struct ifnet *ifp) 241 { 242 struct ieee80211com *ic = (void *)ifp; 243 244 if (ic->ic_scan_lock & IEEE80211_SCAN_LOCKED) 245 return; 246 ic->ic_scan_lock |= IEEE80211_SCAN_LOCKED; 247 248 /* 249 * In all but hostap mode scanning starts off in 250 * an active mode before switching to passive. 251 */ 252 #ifndef IEEE80211_STA_ONLY 253 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 254 #endif 255 { 256 ic->ic_flags |= IEEE80211_F_ASCAN; 257 ic->ic_stats.is_scan_active++; 258 } 259 #ifndef IEEE80211_STA_ONLY 260 else 261 ic->ic_stats.is_scan_passive++; 262 #endif 263 if (ifp->if_flags & IFF_DEBUG) 264 printf("%s: begin %s scan\n", ifp->if_xname, 265 (ic->ic_flags & IEEE80211_F_ASCAN) ? 266 "active" : "passive"); 267 268 /* 269 * Flush any previously seen AP's. Note that the latter 270 * assumes we don't act as both an AP and a station, 271 * otherwise we'll potentially flush state of stations 272 * associated with us. 273 */ 274 ieee80211_free_allnodes(ic); 275 276 /* 277 * Reset the current mode. Setting the current mode will also 278 * reset scan state. 279 */ 280 if (IFM_MODE(ic->ic_media.ifm_cur->ifm_media) == IFM_AUTO) 281 ic->ic_curmode = IEEE80211_MODE_AUTO; 282 ieee80211_setmode(ic, ic->ic_curmode); 283 284 ic->ic_scan_count = 0; 285 286 /* Scan the next channel. */ 287 ieee80211_next_scan(ifp); 288 } 289 290 /* 291 * Switch to the next channel marked for scanning. 292 */ 293 void 294 ieee80211_next_scan(struct ifnet *ifp) 295 { 296 struct ieee80211com *ic = (void *)ifp; 297 struct ieee80211_channel *chan; 298 299 chan = ic->ic_bss->ni_chan; 300 for (;;) { 301 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 302 chan = &ic->ic_channels[0]; 303 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 304 /* 305 * Ignore channels marked passive-only 306 * during an active scan. 307 */ 308 if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 || 309 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) 310 break; 311 } 312 if (chan == ic->ic_bss->ni_chan) { 313 ieee80211_end_scan(ifp); 314 return; 315 } 316 } 317 clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 318 DPRINTF(("chan %d->%d\n", 319 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan), 320 ieee80211_chan2ieee(ic, chan))); 321 ic->ic_bss->ni_chan = chan; 322 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 323 } 324 325 #ifndef IEEE80211_STA_ONLY 326 void 327 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 328 { 329 struct ieee80211_node *ni; 330 struct ifnet *ifp = &ic->ic_if; 331 332 ni = ic->ic_bss; 333 if (ifp->if_flags & IFF_DEBUG) 334 printf("%s: creating ibss\n", ifp->if_xname); 335 ic->ic_flags |= IEEE80211_F_SIBSS; 336 ni->ni_chan = chan; 337 ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)]; 338 ni->ni_txrate = 0; 339 IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr); 340 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 341 if (ic->ic_opmode == IEEE80211_M_IBSS) { 342 if ((ic->ic_flags & IEEE80211_F_DESBSSID) != 0) 343 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 344 else 345 ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 346 } 347 ni->ni_esslen = ic->ic_des_esslen; 348 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 349 ni->ni_rssi = 0; 350 ni->ni_rstamp = 0; 351 memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp)); 352 ni->ni_intval = ic->ic_lintval; 353 ni->ni_capinfo = IEEE80211_CAPINFO_IBSS; 354 if (ic->ic_flags & IEEE80211_F_WEPON) 355 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 356 if (ic->ic_flags & IEEE80211_F_HTON) { 357 const struct ieee80211_edca_ac_params *ac_qap; 358 struct ieee80211_edca_ac_params *ac; 359 int aci; 360 361 /* 362 * Default to non-member HT protection until we have a way 363 * of picking up information from the environment (such as 364 * beacons from other networks) which proves that only HT 365 * STAs are on the air. 366 */ 367 ni->ni_htop1 = IEEE80211_HTPROT_NONMEMBER; 368 ic->ic_protmode = IEEE80211_PROT_RTSCTS; 369 370 /* Configure QoS EDCA parameters. */ 371 for (aci = 0; aci < EDCA_NUM_AC; aci++) { 372 ac = &ic->ic_edca_ac[aci]; 373 ac_qap = &ieee80211_qap_edca_table[ic->ic_curmode][aci]; 374 ac->ac_acm = ac_qap->ac_acm; 375 ac->ac_aifsn = ac_qap->ac_aifsn; 376 ac->ac_ecwmin = ac_qap->ac_ecwmin; 377 ac->ac_ecwmax = ac_qap->ac_ecwmax; 378 ac->ac_txoplimit = ac_qap->ac_txoplimit; 379 } 380 if (ic->ic_updateedca) 381 (*ic->ic_updateedca)(ic); 382 } 383 if (ic->ic_flags & IEEE80211_F_RSNON) { 384 struct ieee80211_key *k; 385 386 /* initialize 256-bit global key counter to a random value */ 387 arc4random_buf(ic->ic_globalcnt, EAPOL_KEY_NONCE_LEN); 388 389 ni->ni_rsnprotos = ic->ic_rsnprotos; 390 ni->ni_rsnakms = ic->ic_rsnakms; 391 ni->ni_rsnciphers = ic->ic_rsnciphers; 392 ni->ni_rsngroupcipher = ic->ic_rsngroupcipher; 393 ni->ni_rsngroupmgmtcipher = ic->ic_rsngroupmgmtcipher; 394 ni->ni_rsncaps = 0; 395 if (ic->ic_caps & IEEE80211_C_MFP) { 396 ni->ni_rsncaps |= IEEE80211_RSNCAP_MFPC; 397 if (ic->ic_flags & IEEE80211_F_MFPR) 398 ni->ni_rsncaps |= IEEE80211_RSNCAP_MFPR; 399 } 400 401 ic->ic_def_txkey = 1; 402 ic->ic_flags &= ~IEEE80211_F_COUNTERM; 403 k = &ic->ic_nw_keys[ic->ic_def_txkey]; 404 memset(k, 0, sizeof(*k)); 405 k->k_id = ic->ic_def_txkey; 406 k->k_cipher = ni->ni_rsngroupcipher; 407 k->k_flags = IEEE80211_KEY_GROUP | IEEE80211_KEY_TX; 408 k->k_len = ieee80211_cipher_keylen(k->k_cipher); 409 arc4random_buf(k->k_key, k->k_len); 410 (*ic->ic_set_key)(ic, ni, k); /* XXX */ 411 412 if (ic->ic_caps & IEEE80211_C_MFP) { 413 ic->ic_igtk_kid = 4; 414 k = &ic->ic_nw_keys[ic->ic_igtk_kid]; 415 memset(k, 0, sizeof(*k)); 416 k->k_id = ic->ic_igtk_kid; 417 k->k_cipher = ni->ni_rsngroupmgmtcipher; 418 k->k_flags = IEEE80211_KEY_IGTK | IEEE80211_KEY_TX; 419 k->k_len = 16; 420 arc4random_buf(k->k_key, k->k_len); 421 (*ic->ic_set_key)(ic, ni, k); /* XXX */ 422 } 423 /* 424 * In HostAP mode, multicast traffic is sent using ic_bss 425 * as the Tx node, so mark our node as valid so we can send 426 * multicast frames using the group key we've just configured. 427 */ 428 ni->ni_port_valid = 1; 429 ni->ni_flags |= IEEE80211_NODE_TXPROT; 430 431 /* schedule a GTK/IGTK rekeying after 3600s */ 432 timeout_add_sec(&ic->ic_rsn_timeout, 3600); 433 } 434 timeout_add_sec(&ic->ic_inact_timeout, IEEE80211_INACT_WAIT); 435 timeout_add_sec(&ic->ic_node_cache_timeout, IEEE80211_CACHE_WAIT); 436 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 437 } 438 #endif /* IEEE80211_STA_ONLY */ 439 440 int 441 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni) 442 { 443 u_int8_t rate; 444 int fail; 445 446 fail = 0; 447 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 448 fail |= 0x01; 449 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 450 ni->ni_chan != ic->ic_des_chan) 451 fail |= 0x01; 452 #ifndef IEEE80211_STA_ONLY 453 if (ic->ic_opmode == IEEE80211_M_IBSS) { 454 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 455 fail |= 0x02; 456 } else 457 #endif 458 { 459 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 460 fail |= 0x02; 461 } 462 if (ic->ic_flags & (IEEE80211_F_WEPON | IEEE80211_F_RSNON)) { 463 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 464 fail |= 0x04; 465 } else { 466 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 467 fail |= 0x04; 468 } 469 470 rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO); 471 if (rate & IEEE80211_RATE_BASIC) 472 fail |= 0x08; 473 if (ic->ic_des_esslen != 0 && 474 (ni->ni_esslen != ic->ic_des_esslen || 475 memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) 476 fail |= 0x10; 477 if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 478 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 479 fail |= 0x20; 480 481 if (ic->ic_flags & IEEE80211_F_RSNON) { 482 /* 483 * If at least one RSN IE field from the AP's RSN IE fails 484 * to overlap with any value the STA supports, the STA shall 485 * decline to associate with that AP. 486 */ 487 if ((ni->ni_rsnprotos & ic->ic_rsnprotos) == 0) 488 fail |= 0x40; 489 if ((ni->ni_rsnakms & ic->ic_rsnakms) == 0) 490 fail |= 0x40; 491 if ((ni->ni_rsnakms & ic->ic_rsnakms & 492 ~(IEEE80211_AKM_PSK | IEEE80211_AKM_SHA256_PSK)) == 0) { 493 /* AP only supports PSK AKMPs */ 494 if (!(ic->ic_flags & IEEE80211_F_PSK)) 495 fail |= 0x40; 496 } 497 if (ni->ni_rsngroupcipher != IEEE80211_CIPHER_WEP40 && 498 ni->ni_rsngroupcipher != IEEE80211_CIPHER_TKIP && 499 ni->ni_rsngroupcipher != IEEE80211_CIPHER_CCMP && 500 ni->ni_rsngroupcipher != IEEE80211_CIPHER_WEP104) 501 fail |= 0x40; 502 if ((ni->ni_rsnciphers & ic->ic_rsnciphers) == 0) 503 fail |= 0x40; 504 505 /* we only support BIP as the IGTK cipher */ 506 if ((ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC) && 507 ni->ni_rsngroupmgmtcipher != IEEE80211_CIPHER_BIP) 508 fail |= 0x40; 509 510 /* we do not support MFP but AP requires it */ 511 if (!(ic->ic_caps & IEEE80211_C_MFP) && 512 (ni->ni_rsncaps & IEEE80211_RSNCAP_MFPR)) 513 fail |= 0x40; 514 515 /* we require MFP but AP does not support it */ 516 if ((ic->ic_caps & IEEE80211_C_MFP) && 517 (ic->ic_flags & IEEE80211_F_MFPR) && 518 !(ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC)) 519 fail |= 0x40; 520 } 521 522 #ifdef IEEE80211_DEBUG 523 if (ic->ic_if.if_flags & IFF_DEBUG) { 524 printf(" %c %s", fail ? '-' : '+', 525 ether_sprintf(ni->ni_macaddr)); 526 printf(" %s%c", ether_sprintf(ni->ni_bssid), 527 fail & 0x20 ? '!' : ' '); 528 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 529 fail & 0x01 ? '!' : ' '); 530 printf(" %+4d", ni->ni_rssi); 531 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 532 fail & 0x08 ? '!' : ' '); 533 printf(" %4s%c", 534 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 535 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 536 "????", 537 fail & 0x02 ? '!' : ' '); 538 printf(" %7s%c ", 539 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 540 "privacy" : "no", 541 fail & 0x04 ? '!' : ' '); 542 printf(" %3s%c ", 543 (ic->ic_flags & IEEE80211_F_RSNON) ? 544 "rsn" : "no", 545 fail & 0x40 ? '!' : ' '); 546 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 547 printf("%s\n", fail & 0x10 ? "!" : ""); 548 } 549 #endif 550 return fail; 551 } 552 553 /* 554 * Complete a scan of potential channels. 555 */ 556 void 557 ieee80211_end_scan(struct ifnet *ifp) 558 { 559 struct ieee80211com *ic = (void *)ifp; 560 struct ieee80211_node *ni, *nextbs, *selbs; 561 562 if (ifp->if_flags & IFF_DEBUG) 563 printf("%s: end %s scan\n", ifp->if_xname, 564 (ic->ic_flags & IEEE80211_F_ASCAN) ? 565 "active" : "passive"); 566 567 if (ic->ic_scan_count) 568 ic->ic_flags &= ~IEEE80211_F_ASCAN; 569 570 ni = RBT_MIN(ieee80211_tree, &ic->ic_tree); 571 572 #ifndef IEEE80211_STA_ONLY 573 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 574 /* XXX off stack? */ 575 u_char occupied[howmany(IEEE80211_CHAN_MAX, NBBY)]; 576 int i, fail; 577 578 /* 579 * The passive scan to look for existing AP's completed, 580 * select a channel to camp on. Identify the channels 581 * that already have one or more AP's and try to locate 582 * an unoccupied one. If that fails, pick a random 583 * channel from the active set. 584 */ 585 memset(occupied, 0, sizeof(occupied)); 586 RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) 587 setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan)); 588 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 589 if (isset(ic->ic_chan_active, i) && isclr(occupied, i)) 590 break; 591 if (i == IEEE80211_CHAN_MAX) { 592 fail = arc4random() & 3; /* random 0-3 */ 593 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 594 if (isset(ic->ic_chan_active, i) && fail-- == 0) 595 break; 596 } 597 ieee80211_create_ibss(ic, &ic->ic_channels[i]); 598 goto wakeup; 599 } 600 #endif 601 if (ni == NULL) { 602 DPRINTF(("no scan candidate\n")); 603 notfound: 604 605 #ifndef IEEE80211_STA_ONLY 606 if (ic->ic_opmode == IEEE80211_M_IBSS && 607 (ic->ic_flags & IEEE80211_F_IBSSON) && 608 ic->ic_des_esslen != 0) { 609 ieee80211_create_ibss(ic, ic->ic_ibss_chan); 610 goto wakeup; 611 } 612 #endif 613 /* 614 * Scan the next mode if nothing has been found. This 615 * is necessary if the device supports different 616 * incompatible modes in the same channel range, like 617 * like 11b and "pure" 11G mode. 618 * If the device scans all bands in one fell swoop, return 619 * current scan results to userspace regardless of mode. 620 * This will loop forever except for user-initiated scans. 621 */ 622 if (ieee80211_next_mode(ifp) == IEEE80211_MODE_AUTO || 623 (ic->ic_caps & IEEE80211_C_SCANALLBAND)) { 624 if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST && 625 ic->ic_scan_lock & IEEE80211_SCAN_RESUME) { 626 ic->ic_scan_lock = IEEE80211_SCAN_LOCKED; 627 /* Return from a user-initiated scan. */ 628 wakeup(&ic->ic_scan_lock); 629 } else if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST) 630 goto wakeup; 631 ic->ic_scan_count++; 632 } 633 634 /* 635 * Reset the list of channels to scan and start again. 636 */ 637 ieee80211_next_scan(ifp); 638 return; 639 } 640 selbs = NULL; 641 642 for (; ni != NULL; ni = nextbs) { 643 nextbs = RBT_NEXT(ieee80211_tree, ni); 644 if (ni->ni_fails) { 645 /* 646 * The configuration of the access points may change 647 * during my scan. So delete the entry for the AP 648 * and retry to associate if there is another beacon. 649 */ 650 if (ni->ni_fails++ > 2) 651 ieee80211_free_node(ic, ni); 652 continue; 653 } 654 if (ieee80211_match_bss(ic, ni) != 0) 655 continue; 656 657 /* Pick the AP/IBSS match with the best RSSI. */ 658 if (selbs == NULL) 659 selbs = ni; 660 else if ((ic->ic_caps & IEEE80211_C_SCANALLBAND) && 661 IEEE80211_IS_CHAN_5GHZ(selbs->ni_chan) && 662 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) && 663 selbs->ni_rssi >= (ic->ic_max_rssi - (ic->ic_max_rssi / 4))) 664 /* 665 * Prefer 5GHz (with reasonable RSSI) over 2GHz since 666 * the 5GHz band is usually less saturated. 667 */ 668 continue; 669 else if (ni->ni_rssi > selbs->ni_rssi) 670 selbs = ni; 671 } 672 if (selbs == NULL) 673 goto notfound; 674 (*ic->ic_node_copy)(ic, ic->ic_bss, selbs); 675 ni = ic->ic_bss; 676 677 ic->ic_curmode = ieee80211_chan2mode(ic, ni->ni_chan); 678 679 if (ic->ic_flags & IEEE80211_F_RSNON) 680 ieee80211_choose_rsnparams(ic); 681 else if (ic->ic_flags & IEEE80211_F_WEPON) 682 ni->ni_rsncipher = IEEE80211_CIPHER_USEGROUP; 683 684 ieee80211_node_newstate(selbs, IEEE80211_STA_BSS); 685 #ifndef IEEE80211_STA_ONLY 686 if (ic->ic_opmode == IEEE80211_M_IBSS) { 687 ieee80211_fix_rate(ic, ni, IEEE80211_F_DOFRATE | 688 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 689 if (ni->ni_rates.rs_nrates == 0) 690 goto notfound; 691 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 692 } else 693 #endif 694 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 695 696 wakeup: 697 if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST) { 698 /* Return from a user-initiated scan. */ 699 wakeup(&ic->ic_scan_lock); 700 } 701 702 ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED; 703 } 704 705 /* 706 * Autoselect the best RSN parameters (protocol, AKMP, pairwise cipher...) 707 * that are supported by both peers (STA mode only). 708 */ 709 void 710 ieee80211_choose_rsnparams(struct ieee80211com *ic) 711 { 712 struct ieee80211_node *ni = ic->ic_bss; 713 struct ieee80211_pmk *pmk; 714 715 /* filter out unsupported protocol versions */ 716 ni->ni_rsnprotos &= ic->ic_rsnprotos; 717 /* prefer RSN (aka WPA2) over WPA */ 718 if (ni->ni_rsnprotos & IEEE80211_PROTO_RSN) 719 ni->ni_rsnprotos = IEEE80211_PROTO_RSN; 720 else 721 ni->ni_rsnprotos = IEEE80211_PROTO_WPA; 722 723 /* filter out unsupported AKMPs */ 724 ni->ni_rsnakms &= ic->ic_rsnakms; 725 /* prefer SHA-256 based AKMPs */ 726 if ((ic->ic_flags & IEEE80211_F_PSK) && (ni->ni_rsnakms & 727 (IEEE80211_AKM_PSK | IEEE80211_AKM_SHA256_PSK))) { 728 /* AP supports PSK AKMP and a PSK is configured */ 729 if (ni->ni_rsnakms & IEEE80211_AKM_SHA256_PSK) 730 ni->ni_rsnakms = IEEE80211_AKM_SHA256_PSK; 731 else 732 ni->ni_rsnakms = IEEE80211_AKM_PSK; 733 } else { 734 if (ni->ni_rsnakms & IEEE80211_AKM_SHA256_8021X) 735 ni->ni_rsnakms = IEEE80211_AKM_SHA256_8021X; 736 else 737 ni->ni_rsnakms = IEEE80211_AKM_8021X; 738 /* check if we have a cached PMK for this AP */ 739 if (ni->ni_rsnprotos == IEEE80211_PROTO_RSN && 740 (pmk = ieee80211_pmksa_find(ic, ni, NULL)) != NULL) { 741 memcpy(ni->ni_pmkid, pmk->pmk_pmkid, 742 IEEE80211_PMKID_LEN); 743 ni->ni_flags |= IEEE80211_NODE_PMKID; 744 } 745 } 746 747 /* filter out unsupported pairwise ciphers */ 748 ni->ni_rsnciphers &= ic->ic_rsnciphers; 749 /* prefer CCMP over TKIP */ 750 if (ni->ni_rsnciphers & IEEE80211_CIPHER_CCMP) 751 ni->ni_rsnciphers = IEEE80211_CIPHER_CCMP; 752 else 753 ni->ni_rsnciphers = IEEE80211_CIPHER_TKIP; 754 ni->ni_rsncipher = ni->ni_rsnciphers; 755 756 /* use MFP if we both support it */ 757 if ((ic->ic_caps & IEEE80211_C_MFP) && 758 (ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC)) 759 ni->ni_flags |= IEEE80211_NODE_MFP; 760 } 761 762 int 763 ieee80211_get_rate(struct ieee80211com *ic) 764 { 765 u_int8_t (*rates)[IEEE80211_RATE_MAXSIZE]; 766 int rate; 767 768 rates = &ic->ic_bss->ni_rates.rs_rates; 769 770 if (ic->ic_fixed_rate != -1) 771 rate = (*rates)[ic->ic_fixed_rate]; 772 else if (ic->ic_state == IEEE80211_S_RUN) 773 rate = (*rates)[ic->ic_bss->ni_txrate]; 774 else 775 rate = 0; 776 777 return rate & IEEE80211_RATE_VAL; 778 } 779 780 struct ieee80211_node * 781 ieee80211_node_alloc(struct ieee80211com *ic) 782 { 783 return malloc(sizeof(struct ieee80211_node), M_DEVBUF, 784 M_NOWAIT | M_ZERO); 785 } 786 787 void 788 ieee80211_node_cleanup(struct ieee80211com *ic, struct ieee80211_node *ni) 789 { 790 if (ni->ni_rsnie != NULL) { 791 free(ni->ni_rsnie, M_DEVBUF, 0); 792 ni->ni_rsnie = NULL; 793 } 794 ieee80211_ba_del(ni); 795 } 796 797 void 798 ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni) 799 { 800 ieee80211_node_cleanup(ic, ni); 801 free(ni, M_DEVBUF, 0); 802 } 803 804 void 805 ieee80211_node_copy(struct ieee80211com *ic, 806 struct ieee80211_node *dst, const struct ieee80211_node *src) 807 { 808 ieee80211_node_cleanup(ic, dst); 809 *dst = *src; 810 dst->ni_rsnie = NULL; 811 if (src->ni_rsnie != NULL) 812 ieee80211_save_ie(src->ni_rsnie, &dst->ni_rsnie); 813 } 814 815 u_int8_t 816 ieee80211_node_getrssi(struct ieee80211com *ic, 817 const struct ieee80211_node *ni) 818 { 819 return ni->ni_rssi; 820 } 821 822 void 823 ieee80211_setup_node(struct ieee80211com *ic, 824 struct ieee80211_node *ni, const u_int8_t *macaddr) 825 { 826 int s; 827 828 DPRINTF(("%s\n", ether_sprintf((u_int8_t *)macaddr))); 829 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 830 ieee80211_node_newstate(ni, IEEE80211_STA_CACHE); 831 832 ni->ni_ic = ic; /* back-pointer */ 833 #ifndef IEEE80211_STA_ONLY 834 mq_init(&ni->ni_savedq, IEEE80211_PS_MAX_QUEUE, IPL_NET); 835 timeout_set(&ni->ni_eapol_to, ieee80211_eapol_timeout, ni); 836 timeout_set(&ni->ni_sa_query_to, ieee80211_sa_query_timeout, ni); 837 #endif 838 s = splnet(); 839 RBT_INSERT(ieee80211_tree, &ic->ic_tree, ni); 840 ic->ic_nnodes++; 841 splx(s); 842 } 843 844 struct ieee80211_node * 845 ieee80211_alloc_node(struct ieee80211com *ic, const u_int8_t *macaddr) 846 { 847 struct ieee80211_node *ni = ieee80211_alloc_node_helper(ic); 848 if (ni != NULL) 849 ieee80211_setup_node(ic, ni, macaddr); 850 else 851 ic->ic_stats.is_rx_nodealloc++; 852 return ni; 853 } 854 855 struct ieee80211_node * 856 ieee80211_dup_bss(struct ieee80211com *ic, const u_int8_t *macaddr) 857 { 858 struct ieee80211_node *ni = ieee80211_alloc_node_helper(ic); 859 if (ni != NULL) { 860 ieee80211_setup_node(ic, ni, macaddr); 861 /* 862 * Inherit from ic_bss. 863 */ 864 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 865 ni->ni_chan = ic->ic_bss->ni_chan; 866 } else 867 ic->ic_stats.is_rx_nodealloc++; 868 return ni; 869 } 870 871 struct ieee80211_node * 872 ieee80211_find_node(struct ieee80211com *ic, const u_int8_t *macaddr) 873 { 874 struct ieee80211_node *ni; 875 int cmp; 876 877 /* similar to RBT_FIND except we compare keys, not nodes */ 878 ni = RBT_ROOT(ieee80211_tree, &ic->ic_tree); 879 while (ni != NULL) { 880 cmp = memcmp(macaddr, ni->ni_macaddr, IEEE80211_ADDR_LEN); 881 if (cmp < 0) 882 ni = RBT_LEFT(ieee80211_tree, ni); 883 else if (cmp > 0) 884 ni = RBT_RIGHT(ieee80211_tree, ni); 885 else 886 break; 887 } 888 return ni; 889 } 890 891 /* 892 * Return a reference to the appropriate node for sending 893 * a data frame. This handles node discovery in adhoc networks. 894 * 895 * Drivers will call this, so increase the reference count before 896 * returning the node. 897 */ 898 struct ieee80211_node * 899 ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr) 900 { 901 #ifndef IEEE80211_STA_ONLY 902 struct ieee80211_node *ni; 903 int s; 904 #endif 905 906 /* 907 * The destination address should be in the node table 908 * unless we are operating in station mode or this is a 909 * multicast/broadcast frame. 910 */ 911 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr)) 912 return ieee80211_ref_node(ic->ic_bss); 913 914 #ifndef IEEE80211_STA_ONLY 915 s = splnet(); 916 ni = ieee80211_find_node(ic, macaddr); 917 splx(s); 918 if (ni == NULL) { 919 if (ic->ic_opmode != IEEE80211_M_IBSS && 920 ic->ic_opmode != IEEE80211_M_AHDEMO) 921 return NULL; 922 923 /* 924 * Fake up a node; this handles node discovery in 925 * adhoc mode. Note that for the driver's benefit 926 * we we treat this like an association so the driver 927 * has an opportunity to setup its private state. 928 * 929 * XXX need better way to handle this; issue probe 930 * request so we can deduce rate set, etc. 931 */ 932 if ((ni = ieee80211_dup_bss(ic, macaddr)) == NULL) 933 return NULL; 934 /* XXX no rate negotiation; just dup */ 935 ni->ni_rates = ic->ic_bss->ni_rates; 936 ni->ni_txrate = 0; 937 if (ic->ic_newassoc) 938 (*ic->ic_newassoc)(ic, ni, 1); 939 } 940 return ieee80211_ref_node(ni); 941 #else 942 return NULL; /* can't get there */ 943 #endif /* IEEE80211_STA_ONLY */ 944 } 945 946 /* 947 * It is usually desirable to process a Rx packet using its sender's 948 * node-record instead of the BSS record. 949 * 950 * - AP mode: keep a node-record for every authenticated/associated 951 * station *in the BSS*. For future use, we also track neighboring 952 * APs, since they might belong to the same ESS. APs in the same 953 * ESS may bridge packets to each other, forming a Wireless 954 * Distribution System (WDS). 955 * 956 * - IBSS mode: keep a node-record for every station *in the BSS*. 957 * Also track neighboring stations by their beacons/probe responses. 958 * 959 * - monitor mode: keep a node-record for every sender, regardless 960 * of BSS. 961 * 962 * - STA mode: the only available node-record is the BSS record, 963 * ic->ic_bss. 964 * 965 * Of all the 802.11 Control packets, only the node-records for 966 * RTS packets node-record can be looked up. 967 * 968 * Return non-zero if the packet's node-record is kept, zero 969 * otherwise. 970 */ 971 static __inline int 972 ieee80211_needs_rxnode(struct ieee80211com *ic, 973 const struct ieee80211_frame *wh, const u_int8_t **bssid) 974 { 975 int monitor, rc = 0; 976 977 monitor = (ic->ic_opmode == IEEE80211_M_MONITOR); 978 979 *bssid = NULL; 980 981 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 982 case IEEE80211_FC0_TYPE_CTL: 983 if (!monitor) 984 break; 985 return (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 986 IEEE80211_FC0_SUBTYPE_RTS; 987 case IEEE80211_FC0_TYPE_MGT: 988 *bssid = wh->i_addr3; 989 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) { 990 case IEEE80211_FC0_SUBTYPE_BEACON: 991 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 992 break; 993 default: 994 #ifndef IEEE80211_STA_ONLY 995 if (ic->ic_opmode == IEEE80211_M_STA) 996 break; 997 rc = IEEE80211_ADDR_EQ(*bssid, ic->ic_bss->ni_bssid) || 998 IEEE80211_ADDR_EQ(*bssid, etherbroadcastaddr); 999 #endif 1000 break; 1001 } 1002 break; 1003 case IEEE80211_FC0_TYPE_DATA: 1004 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 1005 case IEEE80211_FC1_DIR_NODS: 1006 *bssid = wh->i_addr3; 1007 #ifndef IEEE80211_STA_ONLY 1008 if (ic->ic_opmode == IEEE80211_M_IBSS || 1009 ic->ic_opmode == IEEE80211_M_AHDEMO) 1010 rc = IEEE80211_ADDR_EQ(*bssid, 1011 ic->ic_bss->ni_bssid); 1012 #endif 1013 break; 1014 case IEEE80211_FC1_DIR_TODS: 1015 *bssid = wh->i_addr1; 1016 #ifndef IEEE80211_STA_ONLY 1017 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1018 rc = IEEE80211_ADDR_EQ(*bssid, 1019 ic->ic_bss->ni_bssid); 1020 #endif 1021 break; 1022 case IEEE80211_FC1_DIR_FROMDS: 1023 case IEEE80211_FC1_DIR_DSTODS: 1024 *bssid = wh->i_addr2; 1025 #ifndef IEEE80211_STA_ONLY 1026 rc = (ic->ic_opmode == IEEE80211_M_HOSTAP); 1027 #endif 1028 break; 1029 } 1030 break; 1031 } 1032 return monitor || rc; 1033 } 1034 1035 /* 1036 * Drivers call this, so increase the reference count before returning 1037 * the node. 1038 */ 1039 struct ieee80211_node * 1040 ieee80211_find_rxnode(struct ieee80211com *ic, 1041 const struct ieee80211_frame *wh) 1042 { 1043 static const u_int8_t zero[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 1044 struct ieee80211_node *ni; 1045 const u_int8_t *bssid; 1046 int s; 1047 1048 if (!ieee80211_needs_rxnode(ic, wh, &bssid)) 1049 return ieee80211_ref_node(ic->ic_bss); 1050 1051 s = splnet(); 1052 ni = ieee80211_find_node(ic, wh->i_addr2); 1053 splx(s); 1054 1055 if (ni != NULL) 1056 return ieee80211_ref_node(ni); 1057 #ifndef IEEE80211_STA_ONLY 1058 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1059 return ieee80211_ref_node(ic->ic_bss); 1060 #endif 1061 /* XXX see remarks in ieee80211_find_txnode */ 1062 /* XXX no rate negotiation; just dup */ 1063 if ((ni = ieee80211_dup_bss(ic, wh->i_addr2)) == NULL) 1064 return ieee80211_ref_node(ic->ic_bss); 1065 1066 IEEE80211_ADDR_COPY(ni->ni_bssid, (bssid != NULL) ? bssid : zero); 1067 1068 ni->ni_rates = ic->ic_bss->ni_rates; 1069 ni->ni_txrate = 0; 1070 if (ic->ic_newassoc) 1071 (*ic->ic_newassoc)(ic, ni, 1); 1072 1073 DPRINTF(("faked-up node %p for %s\n", ni, 1074 ether_sprintf((u_int8_t *)wh->i_addr2))); 1075 1076 return ieee80211_ref_node(ni); 1077 } 1078 1079 struct ieee80211_node * 1080 ieee80211_find_node_for_beacon(struct ieee80211com *ic, 1081 const u_int8_t *macaddr, const struct ieee80211_channel *chan, 1082 const char *ssid, u_int8_t rssi) 1083 { 1084 struct ieee80211_node *ni, *keep = NULL; 1085 int s, score = 0; 1086 1087 if ((ni = ieee80211_find_node(ic, macaddr)) != NULL) { 1088 s = splnet(); 1089 1090 if (ni->ni_chan != chan && ni->ni_rssi >= rssi) 1091 score++; 1092 if (ssid[1] == 0 && ni->ni_esslen != 0) 1093 score++; 1094 if (score > 0) 1095 keep = ni; 1096 1097 splx(s); 1098 } 1099 1100 return (keep); 1101 } 1102 1103 void 1104 ieee80211_ba_del(struct ieee80211_node *ni) 1105 { 1106 int tid; 1107 1108 for (tid = 0; tid < nitems(ni->ni_rx_ba); tid++) { 1109 struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid]; 1110 if (ba->ba_state != IEEE80211_BA_INIT) { 1111 if (timeout_pending(&ba->ba_to)) 1112 timeout_del(&ba->ba_to); 1113 if (timeout_pending(&ba->ba_gap_to)) 1114 timeout_del(&ba->ba_gap_to); 1115 ba->ba_state = IEEE80211_BA_INIT; 1116 } 1117 } 1118 1119 for (tid = 0; tid < nitems(ni->ni_tx_ba); tid++) { 1120 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 1121 if (ba->ba_state != IEEE80211_BA_INIT) { 1122 if (timeout_pending(&ba->ba_to)) 1123 timeout_del(&ba->ba_to); 1124 ba->ba_state = IEEE80211_BA_INIT; 1125 } 1126 } 1127 } 1128 1129 void 1130 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni) 1131 { 1132 if (ni == ic->ic_bss) 1133 panic("freeing bss node"); 1134 1135 splassert(IPL_NET); 1136 1137 DPRINTF(("%s\n", ether_sprintf(ni->ni_macaddr))); 1138 #ifndef IEEE80211_STA_ONLY 1139 timeout_del(&ni->ni_eapol_to); 1140 timeout_del(&ni->ni_sa_query_to); 1141 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1142 #endif 1143 ieee80211_ba_del(ni); 1144 RBT_REMOVE(ieee80211_tree, &ic->ic_tree, ni); 1145 ic->ic_nnodes--; 1146 #ifndef IEEE80211_STA_ONLY 1147 if (mq_purge(&ni->ni_savedq) > 0) { 1148 if (ic->ic_set_tim != NULL) 1149 (*ic->ic_set_tim)(ic, ni->ni_associd, 0); 1150 } 1151 #endif 1152 (*ic->ic_node_free)(ic, ni); 1153 /* TBD indicate to drivers that a new node can be allocated */ 1154 } 1155 1156 void 1157 ieee80211_release_node(struct ieee80211com *ic, struct ieee80211_node *ni) 1158 { 1159 int s; 1160 1161 DPRINTF(("%s refcnt %u\n", ether_sprintf(ni->ni_macaddr), 1162 ni->ni_refcnt)); 1163 s = splnet(); 1164 if (ieee80211_node_decref(ni) == 0 && 1165 ni->ni_state == IEEE80211_STA_COLLECT) { 1166 ieee80211_free_node(ic, ni); 1167 } 1168 splx(s); 1169 } 1170 1171 void 1172 ieee80211_free_allnodes(struct ieee80211com *ic) 1173 { 1174 struct ieee80211_node *ni; 1175 int s; 1176 1177 DPRINTF(("freeing all nodes\n")); 1178 s = splnet(); 1179 while ((ni = RBT_MIN(ieee80211_tree, &ic->ic_tree)) != NULL) 1180 ieee80211_free_node(ic, ni); 1181 splx(s); 1182 1183 if (ic->ic_bss != NULL) 1184 ieee80211_node_cleanup(ic, ic->ic_bss); /* for station mode */ 1185 } 1186 1187 void 1188 ieee80211_clean_cached(struct ieee80211com *ic) 1189 { 1190 struct ieee80211_node *ni, *next_ni; 1191 int s; 1192 1193 s = splnet(); 1194 for (ni = RBT_MIN(ieee80211_tree, &ic->ic_tree); 1195 ni != NULL; ni = next_ni) { 1196 next_ni = RBT_NEXT(ieee80211_tree, ni); 1197 if (ni->ni_state == IEEE80211_STA_CACHE) 1198 ieee80211_free_node(ic, ni); 1199 } 1200 splx(s); 1201 } 1202 /* 1203 * Timeout inactive nodes. 1204 * 1205 * If called because of a cache timeout, which happens only in hostap and ibss 1206 * modes, clean all inactive cached or authenticated nodes but don't de-auth 1207 * any associated nodes. 1208 * 1209 * Else, this function is called because a new node must be allocated but the 1210 * node cache is full. In this case, return as soon as a free slot was made 1211 * available. If acting as hostap, clean cached nodes regardless of their 1212 * recent activity and also allow de-authing of authenticated nodes older 1213 * than one cache wait interval, and de-authing of inactive associated nodes. 1214 */ 1215 void 1216 ieee80211_clean_nodes(struct ieee80211com *ic, int cache_timeout) 1217 { 1218 struct ieee80211_node *ni, *next_ni; 1219 u_int gen = ic->ic_scangen++; /* NB: ok 'cuz single-threaded*/ 1220 int s; 1221 #ifndef IEEE80211_STA_ONLY 1222 int nnodes = 0; 1223 struct ifnet *ifp = &ic->ic_if; 1224 #endif 1225 1226 s = splnet(); 1227 for (ni = RBT_MIN(ieee80211_tree, &ic->ic_tree); 1228 ni != NULL; ni = next_ni) { 1229 next_ni = RBT_NEXT(ieee80211_tree, ni); 1230 if (!cache_timeout && ic->ic_nnodes < ic->ic_max_nnodes) 1231 break; 1232 if (ni->ni_scangen == gen) /* previously handled */ 1233 continue; 1234 #ifndef IEEE80211_STA_ONLY 1235 nnodes++; 1236 #endif 1237 ni->ni_scangen = gen; 1238 if (ni->ni_refcnt > 0) 1239 continue; 1240 #ifndef IEEE80211_STA_ONLY 1241 if ((ic->ic_opmode == IEEE80211_M_HOSTAP || 1242 ic->ic_opmode == IEEE80211_M_IBSS) && 1243 ic->ic_state == IEEE80211_S_RUN) { 1244 if (cache_timeout) { 1245 if (ni->ni_state != IEEE80211_STA_COLLECT && 1246 (ni->ni_state == IEEE80211_STA_ASSOC || 1247 ni->ni_inact < IEEE80211_INACT_MAX)) 1248 continue; 1249 } else { 1250 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1251 ((ni->ni_state == IEEE80211_STA_ASSOC && 1252 ni->ni_inact < IEEE80211_INACT_MAX) || 1253 (ni->ni_state == IEEE80211_STA_AUTH && 1254 ni->ni_inact == 0))) 1255 continue; 1256 1257 if (ic->ic_opmode == IEEE80211_M_IBSS && 1258 ni->ni_state != IEEE80211_STA_COLLECT && 1259 ni->ni_state != IEEE80211_STA_CACHE && 1260 ni->ni_inact < IEEE80211_INACT_MAX) 1261 continue; 1262 } 1263 } 1264 if (ifp->if_flags & IFF_DEBUG) 1265 printf("%s: station %s purged from node cache\n", 1266 ifp->if_xname, ether_sprintf(ni->ni_macaddr)); 1267 #endif 1268 /* 1269 * If we're hostap and the node is authenticated, send 1270 * a deauthentication frame. The node will be freed when 1271 * the driver calls ieee80211_release_node(). 1272 */ 1273 #ifndef IEEE80211_STA_ONLY 1274 nnodes--; 1275 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1276 ni->ni_state >= IEEE80211_STA_AUTH && 1277 ni->ni_state != IEEE80211_STA_COLLECT) { 1278 splx(s); 1279 IEEE80211_SEND_MGMT(ic, ni, 1280 IEEE80211_FC0_SUBTYPE_DEAUTH, 1281 IEEE80211_REASON_AUTH_EXPIRE); 1282 s = splnet(); 1283 ieee80211_node_leave(ic, ni); 1284 } else 1285 #endif 1286 ieee80211_free_node(ic, ni); 1287 ic->ic_stats.is_node_timeout++; 1288 } 1289 1290 #ifndef IEEE80211_STA_ONLY 1291 /* 1292 * During a cache timeout we iterate over all nodes. 1293 * Check for node leaks by comparing the actual number of cached 1294 * nodes with the ic_nnodes count, which is maintained while adding 1295 * and removing nodes from the cache. 1296 */ 1297 if ((ifp->if_flags & IFF_DEBUG) && cache_timeout && 1298 nnodes != ic->ic_nnodes) 1299 printf("%s: number of cached nodes is %d, expected %d," 1300 "possible nodes leak\n", ifp->if_xname, nnodes, 1301 ic->ic_nnodes); 1302 #endif 1303 splx(s); 1304 } 1305 1306 void 1307 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, 1308 void *arg) 1309 { 1310 struct ieee80211_node *ni; 1311 int s; 1312 1313 s = splnet(); 1314 RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) 1315 (*f)(arg, ni); 1316 splx(s); 1317 } 1318 1319 1320 /* 1321 * Install received HT caps information in the node's state block. 1322 */ 1323 void 1324 ieee80211_setup_htcaps(struct ieee80211_node *ni, const uint8_t *data, 1325 uint8_t len) 1326 { 1327 uint16_t rxrate; 1328 1329 if (len != 26) 1330 return; 1331 1332 ni->ni_htcaps = (data[0] | (data[1] << 8)); 1333 ni->ni_ampdu_param = data[2]; 1334 1335 memcpy(ni->ni_rxmcs, &data[3], sizeof(ni->ni_rxmcs)); 1336 /* clear reserved bits */ 1337 clrbit(ni->ni_rxmcs, 77); 1338 clrbit(ni->ni_rxmcs, 78); 1339 clrbit(ni->ni_rxmcs, 79); 1340 1341 /* Max MCS Rx rate in 1Mb/s units (0 means "not specified"). */ 1342 rxrate = ((data[13] | (data[14]) << 8) & IEEE80211_MCS_RX_RATE_HIGH); 1343 if (rxrate < 1024) 1344 ni->ni_max_rxrate = rxrate; 1345 1346 ni->ni_tx_mcs_set = data[15]; 1347 ni->ni_htxcaps = (data[19] | (data[20] << 8)); 1348 ni->ni_txbfcaps = (data[21] | (data[22] << 8) | (data[23] << 16) | 1349 (data[24] << 24)); 1350 ni->ni_aselcaps = data[25]; 1351 } 1352 1353 #ifndef IEEE80211_STA_ONLY 1354 /* 1355 * Handle nodes switching from 11n into legacy modes. 1356 */ 1357 void 1358 ieee80211_clear_htcaps(struct ieee80211_node *ni) 1359 { 1360 ni->ni_htcaps = 0; 1361 ni->ni_ampdu_param = 0; 1362 memset(ni->ni_rxmcs, 0, sizeof(ni->ni_rxmcs)); 1363 ni->ni_max_rxrate = 0; 1364 ni->ni_tx_mcs_set = 0; 1365 ni->ni_htxcaps = 0; 1366 ni->ni_txbfcaps = 0; 1367 ni->ni_aselcaps = 0; 1368 1369 ni->ni_flags &= ~IEEE80211_NODE_HT; 1370 1371 } 1372 #endif 1373 1374 /* 1375 * Install received HT op information in the node's state block. 1376 */ 1377 int 1378 ieee80211_setup_htop(struct ieee80211_node *ni, const uint8_t *data, 1379 uint8_t len) 1380 { 1381 if (len != 22) 1382 return 0; 1383 1384 ni->ni_primary_chan = data[0]; /* XXX corresponds to ni_chan */ 1385 1386 ni->ni_htop0 = data[1]; 1387 ni->ni_htop1 = (data[2] | (data[3] << 8)); 1388 ni->ni_htop2 = (data[3] | (data[4] << 8)); 1389 1390 memcpy(ni->ni_basic_mcs, &data[6], sizeof(ni->ni_basic_mcs)); 1391 1392 return 1; 1393 } 1394 1395 /* 1396 * Install received rate set information in the node's state block. 1397 */ 1398 int 1399 ieee80211_setup_rates(struct ieee80211com *ic, struct ieee80211_node *ni, 1400 const u_int8_t *rates, const u_int8_t *xrates, int flags) 1401 { 1402 struct ieee80211_rateset *rs = &ni->ni_rates; 1403 1404 memset(rs, 0, sizeof(*rs)); 1405 rs->rs_nrates = rates[1]; 1406 memcpy(rs->rs_rates, rates + 2, rs->rs_nrates); 1407 if (xrates != NULL) { 1408 u_int8_t nxrates; 1409 /* 1410 * Tack on 11g extended supported rate element. 1411 */ 1412 nxrates = xrates[1]; 1413 if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) { 1414 nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates; 1415 DPRINTF(("extended rate set too large; " 1416 "only using %u of %u rates\n", 1417 nxrates, xrates[1])); 1418 ic->ic_stats.is_rx_rstoobig++; 1419 } 1420 memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates); 1421 rs->rs_nrates += nxrates; 1422 } 1423 return ieee80211_fix_rate(ic, ni, flags); 1424 } 1425 1426 #ifndef IEEE80211_STA_ONLY 1427 /* 1428 * Check if the specified node supports ERP. 1429 */ 1430 int 1431 ieee80211_iserp_sta(const struct ieee80211_node *ni) 1432 { 1433 static const u_int8_t rates[] = { 2, 4, 11, 22, 12, 24, 48 }; 1434 const struct ieee80211_rateset *rs = &ni->ni_rates; 1435 int i, j; 1436 1437 /* 1438 * A STA supports ERP operation if it includes all the Clause 19 1439 * mandatory rates in its supported rate set. 1440 */ 1441 for (i = 0; i < nitems(rates); i++) { 1442 for (j = 0; j < rs->rs_nrates; j++) { 1443 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == rates[i]) 1444 break; 1445 } 1446 if (j == rs->rs_nrates) 1447 return 0; 1448 } 1449 return 1; 1450 } 1451 1452 /* 1453 * This function is called to notify the 802.1X PACP machine that a new 1454 * 802.1X port is enabled and must be authenticated. For 802.11, a port 1455 * becomes enabled whenever a STA successfully completes Open System 1456 * authentication with an AP. 1457 */ 1458 void 1459 ieee80211_needs_auth(struct ieee80211com *ic, struct ieee80211_node *ni) 1460 { 1461 /* 1462 * XXX this could be done via the route socket of via a dedicated 1463 * EAP socket or another kernel->userland notification mechanism. 1464 * The notification should include the MAC address (ni_macaddr). 1465 */ 1466 } 1467 1468 /* 1469 * Handle an HT STA joining an HT network. 1470 */ 1471 void 1472 ieee80211_node_join_ht(struct ieee80211com *ic, struct ieee80211_node *ni) 1473 { 1474 enum ieee80211_htprot; 1475 1476 /* Update HT protection setting. */ 1477 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) { 1478 ic->ic_nonhtsta++; 1479 ic->ic_bss->ni_htop1 = IEEE80211_HTPROT_NONHT_MIXED; 1480 if (ic->ic_update_htprot) 1481 ic->ic_update_htprot(ic, ic->ic_bss); 1482 } 1483 } 1484 1485 /* 1486 * Handle a station joining an RSN network. 1487 */ 1488 void 1489 ieee80211_node_join_rsn(struct ieee80211com *ic, struct ieee80211_node *ni) 1490 { 1491 DPRINTF(("station %s associated using proto %d akm 0x%x " 1492 "cipher 0x%x groupcipher 0x%x\n", ether_sprintf(ni->ni_macaddr), 1493 ni->ni_rsnprotos, ni->ni_rsnakms, ni->ni_rsnciphers, 1494 ni->ni_rsngroupcipher)); 1495 1496 ni->ni_rsn_state = RSNA_AUTHENTICATION; 1497 ic->ic_rsnsta++; 1498 1499 ni->ni_key_count = 0; 1500 ni->ni_port_valid = 0; 1501 ni->ni_flags &= ~IEEE80211_NODE_TXRXPROT; 1502 ni->ni_replaycnt = -1; /* XXX */ 1503 ni->ni_rsn_retries = 0; 1504 ni->ni_rsncipher = ni->ni_rsnciphers; 1505 1506 ni->ni_rsn_state = RSNA_AUTHENTICATION_2; 1507 1508 /* generate a new authenticator nonce (ANonce) */ 1509 arc4random_buf(ni->ni_nonce, EAPOL_KEY_NONCE_LEN); 1510 1511 if (!ieee80211_is_8021x_akm(ni->ni_rsnakms)) { 1512 memcpy(ni->ni_pmk, ic->ic_psk, IEEE80211_PMK_LEN); 1513 ni->ni_flags |= IEEE80211_NODE_PMK; 1514 (void)ieee80211_send_4way_msg1(ic, ni); 1515 } else if (ni->ni_flags & IEEE80211_NODE_PMK) { 1516 /* skip 802.1X auth if a cached PMK was found */ 1517 (void)ieee80211_send_4way_msg1(ic, ni); 1518 } else { 1519 /* no cached PMK found, needs full 802.1X auth */ 1520 ieee80211_needs_auth(ic, ni); 1521 } 1522 } 1523 1524 /* 1525 * Handle a station joining an 11g network. 1526 */ 1527 void 1528 ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1529 { 1530 if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)) { 1531 /* 1532 * Joining STA doesn't support short slot time. We must 1533 * disable the use of short slot time for all other associated 1534 * STAs and give the driver a chance to reconfigure the 1535 * hardware. 1536 */ 1537 if (++ic->ic_longslotsta == 1) { 1538 if (ic->ic_caps & IEEE80211_C_SHSLOT) 1539 ieee80211_set_shortslottime(ic, 0); 1540 } 1541 DPRINTF(("[%s] station needs long slot time, count %d\n", 1542 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta)); 1543 } 1544 1545 if (!ieee80211_iserp_sta(ni)) { 1546 /* 1547 * Joining STA is non-ERP. 1548 */ 1549 ic->ic_nonerpsta++; 1550 1551 DPRINTF(("[%s] station is non-ERP, %d non-ERP " 1552 "stations associated\n", ether_sprintf(ni->ni_macaddr), 1553 ic->ic_nonerpsta)); 1554 1555 /* must enable the use of protection */ 1556 if (ic->ic_protmode != IEEE80211_PROT_NONE) { 1557 DPRINTF(("enable use of protection\n")); 1558 ic->ic_flags |= IEEE80211_F_USEPROT; 1559 } 1560 1561 if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) 1562 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 1563 } else 1564 ni->ni_flags |= IEEE80211_NODE_ERP; 1565 } 1566 1567 void 1568 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, 1569 int resp) 1570 { 1571 int newassoc = (ni->ni_state != IEEE80211_STA_ASSOC); 1572 1573 if (ni->ni_associd == 0) { 1574 u_int16_t aid; 1575 1576 /* 1577 * It would be clever to search the bitmap 1578 * more efficiently, but this will do for now. 1579 */ 1580 for (aid = 1; aid < ic->ic_max_aid; aid++) { 1581 if (!IEEE80211_AID_ISSET(aid, 1582 ic->ic_aid_bitmap)) 1583 break; 1584 } 1585 if (aid >= ic->ic_max_aid) { 1586 IEEE80211_SEND_MGMT(ic, ni, resp, 1587 IEEE80211_REASON_ASSOC_TOOMANY); 1588 ieee80211_node_leave(ic, ni); 1589 return; 1590 } 1591 ni->ni_associd = aid | 0xc000; 1592 IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); 1593 if (ic->ic_curmode == IEEE80211_MODE_11G || 1594 (ic->ic_curmode == IEEE80211_MODE_11N && 1595 IEEE80211_IS_CHAN_2GHZ(ic->ic_bss->ni_chan))) 1596 ieee80211_node_join_11g(ic, ni); 1597 } 1598 1599 DPRINTF(("station %s %s associated at aid %d\n", 1600 ether_sprintf(ni->ni_macaddr), newassoc ? "newly" : "already", 1601 ni->ni_associd & ~0xc000)); 1602 1603 ieee80211_ht_negotiate(ic, ni); 1604 if (ic->ic_flags & IEEE80211_F_HTON) 1605 ieee80211_node_join_ht(ic, ni); 1606 1607 /* give driver a chance to setup state like ni_txrate */ 1608 if (ic->ic_newassoc) 1609 (*ic->ic_newassoc)(ic, ni, newassoc); 1610 IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); 1611 ieee80211_node_newstate(ni, IEEE80211_STA_ASSOC); 1612 1613 if (!(ic->ic_flags & IEEE80211_F_RSNON)) { 1614 ni->ni_port_valid = 1; 1615 ni->ni_rsncipher = IEEE80211_CIPHER_USEGROUP; 1616 } else 1617 ieee80211_node_join_rsn(ic, ni); 1618 1619 #if NBRIDGE > 0 1620 /* 1621 * If the parent interface is a bridgeport, learn 1622 * the node's address dynamically on this interface. 1623 */ 1624 if (ic->ic_if.if_bridgeport != NULL) 1625 bridge_update(&ic->ic_if, 1626 (struct ether_addr *)ni->ni_macaddr, 0); 1627 #endif 1628 } 1629 1630 /* 1631 * Handle an HT STA leaving an HT network. 1632 */ 1633 void 1634 ieee80211_node_leave_ht(struct ieee80211com *ic, struct ieee80211_node *ni) 1635 { 1636 struct ieee80211_rx_ba *ba; 1637 u_int8_t tid; 1638 int i; 1639 1640 /* free all Block Ack records */ 1641 ieee80211_ba_del(ni); 1642 for (tid = 0; tid < IEEE80211_NUM_TID; tid++) { 1643 ba = &ni->ni_rx_ba[tid]; 1644 if (ba->ba_buf != NULL) { 1645 for (i = 0; i < IEEE80211_BA_MAX_WINSZ; i++) 1646 m_freem(ba->ba_buf[i].m); 1647 free(ba->ba_buf, M_DEVBUF, 0); 1648 ba->ba_buf = NULL; 1649 } 1650 } 1651 1652 ieee80211_clear_htcaps(ni); 1653 } 1654 1655 /* 1656 * Handle a station leaving an RSN network. 1657 */ 1658 void 1659 ieee80211_node_leave_rsn(struct ieee80211com *ic, struct ieee80211_node *ni) 1660 { 1661 ni->ni_rsn_state = RSNA_DISCONNECTED; 1662 ic->ic_rsnsta--; 1663 1664 ni->ni_rsn_state = RSNA_INITIALIZE; 1665 if ((ni->ni_flags & IEEE80211_NODE_REKEY) && 1666 --ic->ic_rsn_keydonesta == 0) 1667 ieee80211_setkeysdone(ic); 1668 ni->ni_flags &= ~IEEE80211_NODE_REKEY; 1669 1670 ni->ni_flags &= ~IEEE80211_NODE_PMK; 1671 ni->ni_rsn_gstate = RSNA_IDLE; 1672 1673 timeout_del(&ni->ni_eapol_to); 1674 timeout_del(&ni->ni_sa_query_to); 1675 1676 ni->ni_rsn_retries = 0; 1677 ni->ni_flags &= ~IEEE80211_NODE_TXRXPROT; 1678 ni->ni_port_valid = 0; 1679 (*ic->ic_delete_key)(ic, ni, &ni->ni_pairwise_key); 1680 } 1681 1682 /* 1683 * Handle a station leaving an 11g network. 1684 */ 1685 void 1686 ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1687 { 1688 if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)) { 1689 #ifdef DIAGNOSTIC 1690 if (ic->ic_longslotsta == 0) { 1691 panic("bogus long slot station count %d", 1692 ic->ic_longslotsta); 1693 } 1694 #endif 1695 /* leaving STA did not support short slot time */ 1696 if (--ic->ic_longslotsta == 0) { 1697 /* 1698 * All associated STAs now support short slot time, so 1699 * enable this feature and give the driver a chance to 1700 * reconfigure the hardware. Notice that IBSS always 1701 * use a long slot time. 1702 */ 1703 if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 1704 ic->ic_opmode != IEEE80211_M_IBSS) 1705 ieee80211_set_shortslottime(ic, 1); 1706 } 1707 DPRINTF(("[%s] long slot time station leaves, count %d\n", 1708 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta)); 1709 } 1710 1711 if (!(ni->ni_flags & IEEE80211_NODE_ERP)) { 1712 #ifdef DIAGNOSTIC 1713 if (ic->ic_nonerpsta == 0) { 1714 panic("bogus non-ERP station count %d", 1715 ic->ic_nonerpsta); 1716 } 1717 #endif 1718 /* leaving STA was non-ERP */ 1719 if (--ic->ic_nonerpsta == 0) { 1720 /* 1721 * All associated STAs are now ERP capable, disable use 1722 * of protection and re-enable short preamble support. 1723 */ 1724 ic->ic_flags &= ~IEEE80211_F_USEPROT; 1725 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) 1726 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 1727 } 1728 DPRINTF(("[%s] non-ERP station leaves, count %d\n", 1729 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta)); 1730 } 1731 } 1732 1733 /* 1734 * Handle bookkeeping for station deauthentication/disassociation 1735 * when operating as an ap. 1736 */ 1737 void 1738 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 1739 { 1740 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 1741 panic("not in ap mode, mode %u", ic->ic_opmode); 1742 1743 if (ni->ni_state == IEEE80211_STA_COLLECT) 1744 return; 1745 /* 1746 * If node wasn't previously associated all we need to do is 1747 * reclaim the reference. 1748 */ 1749 if (ni->ni_associd == 0) { 1750 ieee80211_node_newstate(ni, IEEE80211_STA_COLLECT); 1751 return; 1752 } 1753 1754 if (ni->ni_pwrsave == IEEE80211_PS_DOZE) { 1755 ic->ic_pssta--; 1756 ni->ni_pwrsave = IEEE80211_PS_AWAKE; 1757 } 1758 1759 if (mq_purge(&ni->ni_savedq) > 0) { 1760 if (ic->ic_set_tim != NULL) 1761 (*ic->ic_set_tim)(ic, ni->ni_associd, 0); 1762 } 1763 1764 if (ic->ic_flags & IEEE80211_F_RSNON) 1765 ieee80211_node_leave_rsn(ic, ni); 1766 1767 if (ic->ic_curmode == IEEE80211_MODE_11G || 1768 (ic->ic_curmode == IEEE80211_MODE_11N && 1769 IEEE80211_IS_CHAN_2GHZ(ic->ic_bss->ni_chan))) 1770 ieee80211_node_leave_11g(ic, ni); 1771 1772 if (ni->ni_flags & IEEE80211_NODE_HT) 1773 ieee80211_node_leave_ht(ic, ni); 1774 else if (ic->ic_flags & IEEE80211_F_HTON) { 1775 if (ic->ic_nonhtsta == 0) 1776 panic("bogus non-HT station count %d", ic->ic_nonhtsta); 1777 if (--ic->ic_nonhtsta == 0) { 1778 /* All associated stations now support HT. */ 1779 ic->ic_bss->ni_htop1 = IEEE80211_HTPROT_NONMEMBER; 1780 if (ic->ic_update_htprot) 1781 ic->ic_update_htprot(ic, ic->ic_bss); 1782 } 1783 } 1784 1785 if (ic->ic_node_leave != NULL) 1786 (*ic->ic_node_leave)(ic, ni); 1787 1788 ieee80211_node_newstate(ni, IEEE80211_STA_COLLECT); 1789 1790 #if NBRIDGE > 0 1791 /* 1792 * If the parent interface is a bridgeport, delete 1793 * any dynamically learned address for this node. 1794 */ 1795 if (ic->ic_if.if_bridgeport != NULL) 1796 bridge_update(&ic->ic_if, 1797 (struct ether_addr *)ni->ni_macaddr, 1); 1798 #endif 1799 } 1800 1801 static int 1802 ieee80211_do_slow_print(struct ieee80211com *ic, int *did_print) 1803 { 1804 static const struct timeval merge_print_intvl = { 1805 .tv_sec = 1, .tv_usec = 0 1806 }; 1807 if ((ic->ic_if.if_flags & IFF_LINK0) == 0) 1808 return 0; 1809 if (!*did_print && (ic->ic_if.if_flags & IFF_DEBUG) == 0 && 1810 !ratecheck(&ic->ic_last_merge_print, &merge_print_intvl)) 1811 return 0; 1812 1813 *did_print = 1; 1814 return 1; 1815 } 1816 1817 /* ieee80211_ibss_merge helps merge 802.11 ad hoc networks. The 1818 * convention, set by the Wireless Ethernet Compatibility Alliance 1819 * (WECA), is that an 802.11 station will change its BSSID to match 1820 * the "oldest" 802.11 ad hoc network, on the same channel, that 1821 * has the station's desired SSID. The "oldest" 802.11 network 1822 * sends beacons with the greatest TSF timestamp. 1823 * 1824 * Return ENETRESET if the BSSID changed, 0 otherwise. 1825 * 1826 * XXX Perhaps we should compensate for the time that elapses 1827 * between the MAC receiving the beacon and the host processing it 1828 * in ieee80211_ibss_merge. 1829 */ 1830 int 1831 ieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni, 1832 u_int64_t local_tsft) 1833 { 1834 u_int64_t beacon_tsft; 1835 int did_print = 0, sign; 1836 union { 1837 u_int64_t word; 1838 u_int8_t tstamp[8]; 1839 } u; 1840 1841 /* ensure alignment */ 1842 (void)memcpy(&u, &ni->ni_tstamp[0], sizeof(u)); 1843 beacon_tsft = letoh64(u.word); 1844 1845 /* we are faster, let the other guy catch up */ 1846 if (beacon_tsft < local_tsft) 1847 sign = -1; 1848 else 1849 sign = 1; 1850 1851 if (IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) { 1852 if (!ieee80211_do_slow_print(ic, &did_print)) 1853 return 0; 1854 printf("%s: tsft offset %s%llu\n", ic->ic_if.if_xname, 1855 (sign < 0) ? "-" : "", 1856 (sign < 0) 1857 ? (local_tsft - beacon_tsft) 1858 : (beacon_tsft - local_tsft)); 1859 return 0; 1860 } 1861 1862 if (sign < 0) 1863 return 0; 1864 1865 if (ieee80211_match_bss(ic, ni) != 0) 1866 return 0; 1867 1868 if (ieee80211_do_slow_print(ic, &did_print)) { 1869 printf("%s: ieee80211_ibss_merge: bssid mismatch %s\n", 1870 ic->ic_if.if_xname, ether_sprintf(ni->ni_bssid)); 1871 printf("%s: my tsft %llu beacon tsft %llu\n", 1872 ic->ic_if.if_xname, local_tsft, beacon_tsft); 1873 printf("%s: sync TSF with %s\n", 1874 ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr)); 1875 } 1876 1877 ic->ic_flags &= ~IEEE80211_F_SIBSS; 1878 1879 /* negotiate rates with new IBSS */ 1880 ieee80211_fix_rate(ic, ni, IEEE80211_F_DOFRATE | 1881 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1882 if (ni->ni_rates.rs_nrates == 0) { 1883 if (ieee80211_do_slow_print(ic, &did_print)) { 1884 printf("%s: rates mismatch, BSSID %s\n", 1885 ic->ic_if.if_xname, ether_sprintf(ni->ni_bssid)); 1886 } 1887 return 0; 1888 } 1889 1890 if (ieee80211_do_slow_print(ic, &did_print)) { 1891 printf("%s: sync BSSID %s -> ", 1892 ic->ic_if.if_xname, ether_sprintf(ic->ic_bss->ni_bssid)); 1893 printf("%s ", ether_sprintf(ni->ni_bssid)); 1894 printf("(from %s)\n", ether_sprintf(ni->ni_macaddr)); 1895 } 1896 1897 ieee80211_node_newstate(ni, IEEE80211_STA_BSS); 1898 (*ic->ic_node_copy)(ic, ic->ic_bss, ni); 1899 1900 return ENETRESET; 1901 } 1902 1903 void 1904 ieee80211_set_tim(struct ieee80211com *ic, int aid, int set) 1905 { 1906 if (set) 1907 setbit(ic->ic_tim_bitmap, aid & ~0xc000); 1908 else 1909 clrbit(ic->ic_tim_bitmap, aid & ~0xc000); 1910 } 1911 1912 /* 1913 * This function shall be called by drivers immediately after every DTIM. 1914 * Transmit all group addressed MSDUs buffered at the AP. 1915 */ 1916 void 1917 ieee80211_notify_dtim(struct ieee80211com *ic) 1918 { 1919 /* NB: group addressed MSDUs are buffered in ic_bss */ 1920 struct ieee80211_node *ni = ic->ic_bss; 1921 struct ifnet *ifp = &ic->ic_if; 1922 struct ieee80211_frame *wh; 1923 struct mbuf *m; 1924 1925 KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP); 1926 1927 while ((m = mq_dequeue(&ni->ni_savedq)) != NULL) { 1928 if (!mq_empty(&ni->ni_savedq)) { 1929 /* more queued frames, set the more data bit */ 1930 wh = mtod(m, struct ieee80211_frame *); 1931 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 1932 } 1933 mq_enqueue(&ic->ic_pwrsaveq, m); 1934 if_start(ifp); 1935 } 1936 /* XXX assumes everything has been sent */ 1937 ic->ic_tim_mcast_pending = 0; 1938 } 1939 #endif /* IEEE80211_STA_ONLY */ 1940 1941 /* 1942 * Compare nodes in the tree by lladdr 1943 */ 1944 int 1945 ieee80211_node_cmp(const struct ieee80211_node *b1, 1946 const struct ieee80211_node *b2) 1947 { 1948 return (memcmp(b1->ni_macaddr, b2->ni_macaddr, IEEE80211_ADDR_LEN)); 1949 } 1950 1951 /* 1952 * Generate red-black tree function logic 1953 */ 1954 RBT_GENERATE(ieee80211_tree, ieee80211_node, ni_node, ieee80211_node_cmp); 1955