1 /* $OpenBSD: ieee80211_node.c,v 1.179 2020/01/15 09:34:27 phessler 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 int ieee80211_node_checkrssi(struct ieee80211com *, 69 const struct ieee80211_node *); 70 int ieee80211_ess_is_better(struct ieee80211com *ic, struct ieee80211_node *, 71 struct ieee80211_node *); 72 void ieee80211_node_set_timeouts(struct ieee80211_node *); 73 void ieee80211_setup_node(struct ieee80211com *, struct ieee80211_node *, 74 const u_int8_t *); 75 struct ieee80211_node *ieee80211_alloc_node_helper(struct ieee80211com *); 76 void ieee80211_node_switch_bss(struct ieee80211com *, struct ieee80211_node *); 77 void ieee80211_node_addba_request(struct ieee80211_node *, int); 78 void ieee80211_node_addba_request_ac_be_to(void *); 79 void ieee80211_node_addba_request_ac_bk_to(void *); 80 void ieee80211_node_addba_request_ac_vi_to(void *); 81 void ieee80211_node_addba_request_ac_vo_to(void *); 82 void ieee80211_needs_auth(struct ieee80211com *, struct ieee80211_node *); 83 #ifndef IEEE80211_STA_ONLY 84 void ieee80211_node_join_ht(struct ieee80211com *, struct ieee80211_node *); 85 void ieee80211_node_join_rsn(struct ieee80211com *, struct ieee80211_node *); 86 void ieee80211_node_join_11g(struct ieee80211com *, struct ieee80211_node *); 87 void ieee80211_node_leave_ht(struct ieee80211com *, struct ieee80211_node *); 88 void ieee80211_node_leave_rsn(struct ieee80211com *, struct ieee80211_node *); 89 void ieee80211_node_leave_11g(struct ieee80211com *, struct ieee80211_node *); 90 void ieee80211_inact_timeout(void *); 91 void ieee80211_node_cache_timeout(void *); 92 #endif 93 void ieee80211_clean_inactive_nodes(struct ieee80211com *, int); 94 95 #ifndef IEEE80211_STA_ONLY 96 void 97 ieee80211_inact_timeout(void *arg) 98 { 99 struct ieee80211com *ic = arg; 100 struct ieee80211_node *ni, *next_ni; 101 int s; 102 103 s = splnet(); 104 for (ni = RBT_MIN(ieee80211_tree, &ic->ic_tree); 105 ni != NULL; ni = next_ni) { 106 next_ni = RBT_NEXT(ieee80211_tree, ni); 107 if (ni->ni_refcnt > 0) 108 continue; 109 if (ni->ni_inact < IEEE80211_INACT_MAX) 110 ni->ni_inact++; 111 } 112 splx(s); 113 114 timeout_add_sec(&ic->ic_inact_timeout, IEEE80211_INACT_WAIT); 115 } 116 117 void 118 ieee80211_node_cache_timeout(void *arg) 119 { 120 struct ieee80211com *ic = arg; 121 122 ieee80211_clean_nodes(ic, 1); 123 timeout_add_sec(&ic->ic_node_cache_timeout, IEEE80211_CACHE_WAIT); 124 } 125 #endif 126 127 /* 128 * For debug purposes 129 */ 130 void 131 ieee80211_print_ess(struct ieee80211_ess *ess) 132 { 133 ieee80211_print_essid(ess->essid, ess->esslen); 134 if (ess->flags & IEEE80211_F_RSNON) { 135 printf(" wpa"); 136 if (ess->rsnprotos & IEEE80211_PROTO_RSN) 137 printf(",wpa2"); 138 if (ess->rsnprotos & IEEE80211_PROTO_WPA) 139 printf(",wpa1"); 140 141 if (ess->rsnakms & IEEE80211_AKM_8021X || 142 ess->rsnakms & IEEE80211_AKM_SHA256_8021X) 143 printf(",802.1x"); 144 printf(" "); 145 146 if (ess->rsnciphers & IEEE80211_CIPHER_USEGROUP) 147 printf(" usegroup"); 148 if (ess->rsnciphers & IEEE80211_CIPHER_WEP40) 149 printf(" wep40"); 150 if (ess->rsnciphers & IEEE80211_CIPHER_WEP104) 151 printf(" wep104"); 152 if (ess->rsnciphers & IEEE80211_CIPHER_TKIP) 153 printf(" tkip"); 154 if (ess->rsnciphers & IEEE80211_CIPHER_CCMP) 155 printf(" ccmp"); 156 } 157 if (ess->flags & IEEE80211_F_WEPON) { 158 int i = ess->def_txkey; 159 160 printf(" wep,"); 161 if (ess->nw_keys[i].k_cipher & IEEE80211_CIPHER_WEP40) 162 printf("wep40"); 163 if (ess->nw_keys[i].k_cipher & IEEE80211_CIPHER_WEP104) 164 printf("wep104"); 165 } 166 if (ess->flags == 0) 167 printf(" clear"); 168 printf("\n"); 169 } 170 171 void 172 ieee80211_print_ess_list(struct ieee80211com *ic) 173 { 174 struct ifnet *ifp = &ic->ic_if; 175 struct ieee80211_ess *ess; 176 177 printf("%s: known networks\n", ifp->if_xname); 178 TAILQ_FOREACH(ess, &ic->ic_ess, ess_next) { 179 ieee80211_print_ess(ess); 180 } 181 } 182 183 struct ieee80211_ess * 184 ieee80211_get_ess(struct ieee80211com *ic, const char *nwid, int len) 185 { 186 struct ieee80211_ess *ess; 187 188 TAILQ_FOREACH(ess, &ic->ic_ess, ess_next) { 189 if (len == ess->esslen && 190 memcmp(ess->essid, nwid, ess->esslen) == 0) 191 return ess; 192 } 193 194 return NULL; 195 } 196 197 void 198 ieee80211_del_ess(struct ieee80211com *ic, char *nwid, int len, int all) 199 { 200 struct ieee80211_ess *ess, *next; 201 202 TAILQ_FOREACH_SAFE(ess, &ic->ic_ess, ess_next, next) { 203 if (all == 1 || (ess->esslen == len && 204 memcmp(ess->essid, nwid, len) == 0)) { 205 TAILQ_REMOVE(&ic->ic_ess, ess, ess_next); 206 explicit_bzero(ess, sizeof(*ess)); 207 free(ess, M_DEVBUF, sizeof(*ess)); 208 if (TAILQ_EMPTY(&ic->ic_ess)) 209 ic->ic_flags &= ~IEEE80211_F_AUTO_JOIN; 210 if (all != 1) 211 return; 212 } 213 } 214 } 215 216 /* Keep in sync with ieee80211_ioctl.c:ieee80211_ioctl_setnwkeys() */ 217 static int 218 ieee80211_ess_setnwkeys(struct ieee80211_ess *ess, 219 const struct ieee80211_nwkey *nwkey) 220 { 221 struct ieee80211_key *k; 222 int error, i; 223 224 if (nwkey->i_wepon == IEEE80211_NWKEY_OPEN) { 225 if (!(ess->flags & IEEE80211_F_WEPON)) 226 return 0; 227 ess->flags &= ~IEEE80211_F_WEPON; 228 return ENETRESET; 229 } 230 if (nwkey->i_defkid < 1 || nwkey->i_defkid > IEEE80211_WEP_NKID) 231 return EINVAL; 232 233 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 234 if (nwkey->i_key[i].i_keylen == 0 || 235 nwkey->i_key[i].i_keydat == NULL) 236 continue; /* entry not set */ 237 if (nwkey->i_key[i].i_keylen > IEEE80211_KEYBUF_SIZE) 238 return EINVAL; 239 240 /* map wep key to ieee80211_key */ 241 k = &ess->nw_keys[i]; 242 memset(k, 0, sizeof(*k)); 243 if (nwkey->i_key[i].i_keylen <= 5) 244 k->k_cipher = IEEE80211_CIPHER_WEP40; 245 else 246 k->k_cipher = IEEE80211_CIPHER_WEP104; 247 k->k_len = ieee80211_cipher_keylen(k->k_cipher); 248 k->k_flags = IEEE80211_KEY_GROUP | IEEE80211_KEY_TX; 249 error = copyin(nwkey->i_key[i].i_keydat, k->k_key, k->k_len); 250 if (error != 0) 251 return error; 252 } 253 ess->def_txkey = nwkey->i_defkid - 1; 254 ess->flags |= IEEE80211_F_WEPON; 255 256 return ENETRESET; 257 } 258 259 260 /* Keep in sync with ieee80211_ioctl.c:ieee80211_ioctl_setwpaparms() */ 261 static int 262 ieee80211_ess_setwpaparms(struct ieee80211_ess *ess, 263 const struct ieee80211_wpaparams *wpa) 264 { 265 if (!wpa->i_enabled) { 266 if (!(ess->flags & IEEE80211_F_RSNON)) 267 return 0; 268 ess->flags &= ~IEEE80211_F_RSNON; 269 ess->rsnprotos = 0; 270 ess->rsnakms = 0; 271 ess->rsngroupcipher = 0; 272 ess->rsnciphers = 0; 273 return ENETRESET; 274 } 275 276 ess->rsnprotos = 0; 277 if (wpa->i_protos & IEEE80211_WPA_PROTO_WPA1) 278 ess->rsnprotos |= IEEE80211_PROTO_WPA; 279 if (wpa->i_protos & IEEE80211_WPA_PROTO_WPA2) 280 ess->rsnprotos |= IEEE80211_PROTO_RSN; 281 if (ess->rsnprotos == 0) /* set to default (RSN) */ 282 ess->rsnprotos = IEEE80211_PROTO_RSN; 283 284 ess->rsnakms = 0; 285 if (wpa->i_akms & IEEE80211_WPA_AKM_PSK) 286 ess->rsnakms |= IEEE80211_AKM_PSK; 287 if (wpa->i_akms & IEEE80211_WPA_AKM_SHA256_PSK) 288 ess->rsnakms |= IEEE80211_AKM_SHA256_PSK; 289 if (wpa->i_akms & IEEE80211_WPA_AKM_8021X) 290 ess->rsnakms |= IEEE80211_AKM_8021X; 291 if (wpa->i_akms & IEEE80211_WPA_AKM_SHA256_8021X) 292 ess->rsnakms |= IEEE80211_AKM_SHA256_8021X; 293 if (ess->rsnakms == 0) /* set to default (PSK) */ 294 ess->rsnakms = IEEE80211_AKM_PSK; 295 296 if (wpa->i_groupcipher == IEEE80211_WPA_CIPHER_WEP40) 297 ess->rsngroupcipher = IEEE80211_CIPHER_WEP40; 298 else if (wpa->i_groupcipher == IEEE80211_WPA_CIPHER_TKIP) 299 ess->rsngroupcipher = IEEE80211_CIPHER_TKIP; 300 else if (wpa->i_groupcipher == IEEE80211_WPA_CIPHER_CCMP) 301 ess->rsngroupcipher = IEEE80211_CIPHER_CCMP; 302 else if (wpa->i_groupcipher == IEEE80211_WPA_CIPHER_WEP104) 303 ess->rsngroupcipher = IEEE80211_CIPHER_WEP104; 304 else { /* set to default */ 305 if (ess->rsnprotos & IEEE80211_PROTO_WPA) 306 ess->rsngroupcipher = IEEE80211_CIPHER_TKIP; 307 else 308 ess->rsngroupcipher = IEEE80211_CIPHER_CCMP; 309 } 310 311 ess->rsnciphers = 0; 312 if (wpa->i_ciphers & IEEE80211_WPA_CIPHER_TKIP) 313 ess->rsnciphers |= IEEE80211_CIPHER_TKIP; 314 if (wpa->i_ciphers & IEEE80211_WPA_CIPHER_CCMP) 315 ess->rsnciphers |= IEEE80211_CIPHER_CCMP; 316 if (wpa->i_ciphers & IEEE80211_WPA_CIPHER_USEGROUP) 317 ess->rsnciphers = IEEE80211_CIPHER_USEGROUP; 318 if (ess->rsnciphers == 0) { /* set to default (CCMP, TKIP if WPA1) */ 319 ess->rsnciphers = IEEE80211_CIPHER_CCMP; 320 if (ess->rsnprotos & IEEE80211_PROTO_WPA) 321 ess->rsnciphers |= IEEE80211_CIPHER_TKIP; 322 } 323 324 ess->flags |= IEEE80211_F_RSNON; 325 326 if (ess->rsnakms & 327 (IEEE80211_AKM_8021X|IEEE80211_WPA_AKM_SHA256_8021X)) 328 ess->flags |= IEEE80211_JOIN_8021X; 329 330 return ENETRESET; 331 } 332 333 static void 334 ieee80211_ess_clear_wep(struct ieee80211_ess *ess) 335 { 336 int i; 337 338 /* Disable WEP */ 339 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 340 explicit_bzero(&ess->nw_keys[i], sizeof(ess->nw_keys[0])); 341 } 342 ess->def_txkey = 0; 343 ess->flags &= ~IEEE80211_F_WEPON; 344 } 345 346 static void 347 ieee80211_ess_clear_wpa(struct ieee80211_ess *ess) 348 { 349 /* Disable WPA */ 350 ess->rsnprotos = ess->rsnakms = ess->rsngroupcipher = 351 ess->rsnciphers = 0; 352 explicit_bzero(ess->psk, sizeof(ess->psk)); 353 ess->flags &= ~(IEEE80211_F_PSK | IEEE80211_F_RSNON); 354 } 355 356 int 357 ieee80211_add_ess(struct ieee80211com *ic, struct ieee80211_join *join) 358 { 359 struct ieee80211_ess *ess; 360 int new = 0, ness = 0; 361 362 /* only valid for station (aka, client) mode */ 363 if (ic->ic_opmode != IEEE80211_M_STA) 364 return (0); 365 366 TAILQ_FOREACH(ess, &ic->ic_ess, ess_next) { 367 if (ess->esslen == join->i_len && 368 memcmp(ess->essid, join->i_nwid, ess->esslen) == 0) 369 break; 370 ness++; 371 } 372 373 if (ess == NULL) { 374 /* if not found, and wpa/wep are set, then return */ 375 if ((join->i_flags & IEEE80211_JOIN_WPA) && 376 (join->i_flags & IEEE80211_JOIN_NWKEY)) { 377 return (EINVAL); 378 } 379 if (ness > IEEE80211_CACHE_SIZE) 380 return (ERANGE); 381 new = 1; 382 ess = malloc(sizeof(*ess), M_DEVBUF, M_NOWAIT|M_ZERO); 383 if (ess == NULL) 384 return (ENOMEM); 385 memcpy(ess->essid, join->i_nwid, join->i_len); 386 ess->esslen = join->i_len; 387 } 388 389 if (join->i_flags & IEEE80211_JOIN_WPA) { 390 if (join->i_wpaparams.i_enabled) { 391 if (!(ic->ic_caps & IEEE80211_C_RSN)) { 392 free(ess, M_DEVBUF, sizeof(*ess)); 393 return ENODEV; 394 } 395 ieee80211_ess_setwpaparms(ess, 396 &join->i_wpaparams); 397 if (join->i_flags & IEEE80211_JOIN_WPAPSK) { 398 ess->flags |= IEEE80211_F_PSK; 399 explicit_bzero(ess->psk, sizeof(ess->psk)); 400 memcpy(ess->psk, &join->i_wpapsk.i_psk, 401 sizeof(ess->psk)); 402 } 403 ieee80211_ess_clear_wep(ess); 404 } else { 405 ieee80211_ess_clear_wpa(ess); 406 } 407 } else if (join->i_flags & IEEE80211_JOIN_NWKEY) { 408 if (join->i_nwkey.i_wepon) { 409 if (!(ic->ic_caps & IEEE80211_C_WEP)) { 410 free(ess, M_DEVBUF, sizeof(*ess)); 411 return ENODEV; 412 } 413 ieee80211_ess_setnwkeys(ess, &join->i_nwkey); 414 ieee80211_ess_clear_wpa(ess); 415 } else { 416 ieee80211_ess_clear_wep(ess); 417 } 418 } 419 420 if (new) 421 TAILQ_INSERT_TAIL(&ic->ic_ess, ess, ess_next); 422 423 return (0); 424 } 425 426 uint8_t 427 ieee80211_ess_adjust_rssi(struct ieee80211com *ic, struct ieee80211_node *ni) 428 { 429 uint8_t rssi = ni->ni_rssi; 430 431 /* 432 * Slightly punish 2 GHz RSSI values since they are usually 433 * stronger than 5 GHz RSSI values. 434 */ 435 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) { 436 if (ic->ic_max_rssi) { 437 uint8_t p = (5 * ic->ic_max_rssi) / 100; 438 if (rssi >= p) 439 rssi -= p; /* punish by 5% */ 440 } else { 441 if (rssi >= 8) 442 rssi -= 8; /* punish by 8 dBm */ 443 } 444 } 445 446 return rssi; 447 } 448 449 int 450 ieee80211_ess_calculate_score(struct ieee80211com *ic, 451 struct ieee80211_node *ni) 452 { 453 int score = 0; 454 uint8_t min_5ghz_rssi; 455 456 if (ic->ic_max_rssi) 457 min_5ghz_rssi = IEEE80211_RSSI_THRES_RATIO_5GHZ; 458 else 459 min_5ghz_rssi = (uint8_t)IEEE80211_RSSI_THRES_5GHZ; 460 461 /* not using join any */ 462 if (ieee80211_get_ess(ic, ni->ni_essid, ni->ni_esslen)) 463 score += 32; 464 465 /* Calculate the crypto score */ 466 if (ni->ni_rsnprotos & IEEE80211_PROTO_RSN) 467 score += 16; 468 if (ni->ni_rsnprotos & IEEE80211_PROTO_WPA) 469 score += 8; 470 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 471 score += 4; 472 473 /* 5GHz with a good signal */ 474 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) && 475 ni->ni_rssi > min_5ghz_rssi) 476 score += 2; 477 478 /* Boost this AP if it had no auth/assoc failures in the past. */ 479 if (ni->ni_fails == 0) 480 score += 21; 481 482 return score; 483 } 484 485 /* 486 * Given two APs, determine the "better" one of the two. 487 * We compute a score based on the following attributes: 488 * 489 * crypto: wpa2 > wpa1 > wep > open 490 * band: 5 GHz > 2 GHz provided 5 GHz rssi is above threshold 491 * rssi: rssi1 > rssi2 as a numeric comparison with a slight 492 * disadvantage for 2 GHz APs 493 * 494 * Crypto carries most weight, followed by band, followed by rssi. 495 */ 496 int 497 ieee80211_ess_is_better(struct ieee80211com *ic, 498 struct ieee80211_node *nicur, struct ieee80211_node *nican) 499 { 500 struct ifnet *ifp = &ic->ic_if; 501 int score_cur = 0, score_can = 0; 502 int cur_rssi, can_rssi; 503 504 score_cur = ieee80211_ess_calculate_score(ic, nicur); 505 score_can = ieee80211_ess_calculate_score(ic, nican); 506 507 cur_rssi = ieee80211_ess_adjust_rssi(ic, nicur); 508 can_rssi = ieee80211_ess_adjust_rssi(ic, nican); 509 510 if (can_rssi > cur_rssi) 511 score_can++; 512 513 if ((ifp->if_flags & IFF_DEBUG) && (score_can <= score_cur)) { 514 printf("%s: AP %s ", ifp->if_xname, 515 ether_sprintf(nican->ni_bssid)); 516 ieee80211_print_essid(nican->ni_essid, nican->ni_esslen); 517 printf(" score %d\n", score_can); 518 } 519 520 return score_can > score_cur; 521 } 522 523 /* Determine whether a candidate AP belongs to a given ESS. */ 524 int 525 ieee80211_match_ess(struct ieee80211_ess *ess, struct ieee80211_node *ni) 526 { 527 if (ess->esslen != 0 && 528 (ess->esslen != ni->ni_esslen || 529 memcmp(ess->essid, ni->ni_essid, ess->esslen) != 0)) { 530 ni->ni_assoc_fail |= IEEE80211_NODE_ASSOCFAIL_ESSID; 531 return 0; 532 } 533 534 if (ess->flags & (IEEE80211_F_PSK | IEEE80211_F_RSNON)) { 535 /* Ensure same WPA version. */ 536 if ((ni->ni_rsnprotos & IEEE80211_PROTO_RSN) && 537 (ess->rsnprotos & IEEE80211_PROTO_RSN) == 0) { 538 ni->ni_assoc_fail |= IEEE80211_NODE_ASSOCFAIL_WPA_PROTO; 539 return 0; 540 } 541 if ((ni->ni_rsnprotos & IEEE80211_PROTO_WPA) && 542 (ess->rsnprotos & IEEE80211_PROTO_WPA) == 0) { 543 ni->ni_assoc_fail |= IEEE80211_NODE_ASSOCFAIL_WPA_PROTO; 544 return 0; 545 } 546 } else if (ess->flags & IEEE80211_F_WEPON) { 547 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) { 548 ni->ni_assoc_fail |= IEEE80211_NODE_ASSOCFAIL_PRIVACY; 549 return 0; 550 } 551 } else { 552 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) != 0) { 553 ni->ni_assoc_fail |= IEEE80211_NODE_ASSOCFAIL_PRIVACY; 554 return 0; 555 } 556 } 557 558 if (ess->esslen == 0 && 559 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) != 0) { 560 ni->ni_assoc_fail |= IEEE80211_NODE_ASSOCFAIL_PRIVACY; 561 return 0; 562 } 563 564 return 1; 565 } 566 567 void 568 ieee80211_switch_ess(struct ieee80211com *ic) 569 { 570 struct ifnet *ifp = &ic->ic_if; 571 struct ieee80211_ess *ess, *seless = NULL; 572 struct ieee80211_node *ni, *selni = NULL; 573 574 if (!ISSET(ifp->if_flags, IFF_RUNNING)) 575 return; 576 577 /* Find the best AP matching an entry on our ESS join list. */ 578 RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) { 579 if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 580 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 581 continue; 582 583 TAILQ_FOREACH(ess, &ic->ic_ess, ess_next) { 584 if (ieee80211_match_ess(ess, ni)) 585 break; 586 } 587 if (ess == NULL) 588 continue; 589 590 /* 591 * Operate only on ic_des_essid if auto-join is disabled. 592 * We might have a password stored for this network. 593 */ 594 if (!ISSET(ic->ic_flags, IEEE80211_F_AUTO_JOIN)) { 595 if (ic->ic_des_esslen == ni->ni_esslen && 596 memcmp(ic->ic_des_essid, ni->ni_essid, 597 ni->ni_esslen) == 0) { 598 ieee80211_set_ess(ic, ess, ni); 599 return; 600 } 601 continue; 602 } 603 604 if (selni == NULL) { 605 seless = ess; 606 selni = ni; 607 continue; 608 } 609 610 if (ieee80211_ess_is_better(ic, selni, ni)) { 611 seless = ess; 612 selni = ni; 613 } 614 } 615 616 if (selni && seless && !(selni->ni_esslen == ic->ic_des_esslen && 617 (memcmp(ic->ic_des_essid, selni->ni_essid, 618 IEEE80211_NWID_LEN) == 0))) { 619 if (ifp->if_flags & IFF_DEBUG) { 620 printf("%s: best AP %s ", ifp->if_xname, 621 ether_sprintf(selni->ni_bssid)); 622 ieee80211_print_essid(selni->ni_essid, 623 selni->ni_esslen); 624 printf(" score %d\n", 625 ieee80211_ess_calculate_score(ic, selni)); 626 printf("%s: switching to network ", ifp->if_xname); 627 ieee80211_print_essid(selni->ni_essid, 628 selni->ni_esslen); 629 if (seless->esslen == 0) 630 printf(" via join any"); 631 printf("\n"); 632 633 } 634 ieee80211_set_ess(ic, seless, selni); 635 } 636 } 637 638 void 639 ieee80211_set_ess(struct ieee80211com *ic, struct ieee80211_ess *ess, 640 struct ieee80211_node *ni) 641 { 642 memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN); 643 ic->ic_des_esslen = ni->ni_esslen; 644 memcpy(ic->ic_des_essid, ni->ni_essid, ic->ic_des_esslen); 645 646 ieee80211_disable_wep(ic); 647 ieee80211_disable_rsn(ic); 648 649 if (ess->flags & IEEE80211_F_RSNON) { 650 explicit_bzero(ic->ic_psk, sizeof(ic->ic_psk)); 651 memcpy(ic->ic_psk, ess->psk, sizeof(ic->ic_psk)); 652 653 ic->ic_rsnprotos = ess->rsnprotos; 654 ic->ic_rsnakms = ess->rsnakms; 655 ic->ic_rsngroupcipher = ess->rsngroupcipher; 656 ic->ic_rsnciphers = ess->rsnciphers; 657 ic->ic_flags |= IEEE80211_F_RSNON; 658 if (ess->flags & IEEE80211_F_PSK) 659 ic->ic_flags |= IEEE80211_F_PSK; 660 } else if (ess->flags & IEEE80211_F_WEPON) { 661 struct ieee80211_key *k; 662 int i; 663 664 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 665 k = &ic->ic_nw_keys[i]; 666 if (k->k_cipher != IEEE80211_CIPHER_NONE) 667 (*ic->ic_delete_key)(ic, NULL, k); 668 memcpy(&ic->ic_nw_keys[i], &ess->nw_keys[i], 669 sizeof(struct ieee80211_key)); 670 (*ic->ic_set_key)(ic, NULL, k); 671 } 672 ic->ic_def_txkey = ess->def_txkey; 673 ic->ic_flags |= IEEE80211_F_WEPON; 674 } 675 } 676 677 void 678 ieee80211_deselect_ess(struct ieee80211com *ic) 679 { 680 memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN); 681 ic->ic_des_esslen = 0; 682 ieee80211_disable_wep(ic); 683 ieee80211_disable_rsn(ic); 684 } 685 686 void 687 ieee80211_node_attach(struct ifnet *ifp) 688 { 689 struct ieee80211com *ic = (void *)ifp; 690 #ifndef IEEE80211_STA_ONLY 691 int size; 692 #endif 693 694 RBT_INIT(ieee80211_tree, &ic->ic_tree); 695 ic->ic_node_alloc = ieee80211_node_alloc; 696 ic->ic_node_free = ieee80211_node_free; 697 ic->ic_node_copy = ieee80211_node_copy; 698 ic->ic_node_getrssi = ieee80211_node_getrssi; 699 ic->ic_node_checkrssi = ieee80211_node_checkrssi; 700 ic->ic_scangen = 1; 701 ic->ic_max_nnodes = ieee80211_cache_size; 702 703 if (ic->ic_max_aid == 0) 704 ic->ic_max_aid = IEEE80211_AID_DEF; 705 else if (ic->ic_max_aid > IEEE80211_AID_MAX) 706 ic->ic_max_aid = IEEE80211_AID_MAX; 707 #ifndef IEEE80211_STA_ONLY 708 size = howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t); 709 ic->ic_aid_bitmap = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO); 710 if (ic->ic_aid_bitmap == NULL) { 711 /* XXX no way to recover */ 712 printf("%s: no memory for AID bitmap!\n", __func__); 713 ic->ic_max_aid = 0; 714 } 715 if (ic->ic_caps & (IEEE80211_C_HOSTAP | IEEE80211_C_IBSS)) { 716 ic->ic_tim_len = howmany(ic->ic_max_aid, 8); 717 ic->ic_tim_bitmap = malloc(ic->ic_tim_len, M_DEVBUF, 718 M_NOWAIT | M_ZERO); 719 if (ic->ic_tim_bitmap == NULL) { 720 printf("%s: no memory for TIM bitmap!\n", __func__); 721 ic->ic_tim_len = 0; 722 } else 723 ic->ic_set_tim = ieee80211_set_tim; 724 timeout_set(&ic->ic_rsn_timeout, 725 ieee80211_gtk_rekey_timeout, ic); 726 timeout_set(&ic->ic_inact_timeout, 727 ieee80211_inact_timeout, ic); 728 timeout_set(&ic->ic_node_cache_timeout, 729 ieee80211_node_cache_timeout, ic); 730 } 731 #endif 732 TAILQ_INIT(&ic->ic_ess); 733 } 734 735 struct ieee80211_node * 736 ieee80211_alloc_node_helper(struct ieee80211com *ic) 737 { 738 struct ieee80211_node *ni; 739 if (ic->ic_nnodes >= ic->ic_max_nnodes) 740 ieee80211_clean_nodes(ic, 0); 741 if (ic->ic_nnodes >= ic->ic_max_nnodes) 742 return NULL; 743 ni = (*ic->ic_node_alloc)(ic); 744 return ni; 745 } 746 747 void 748 ieee80211_node_lateattach(struct ifnet *ifp) 749 { 750 struct ieee80211com *ic = (void *)ifp; 751 struct ieee80211_node *ni; 752 753 ni = ieee80211_alloc_node_helper(ic); 754 if (ni == NULL) 755 panic("unable to setup inital BSS node"); 756 ni->ni_chan = IEEE80211_CHAN_ANYC; 757 ic->ic_bss = ieee80211_ref_node(ni); 758 ic->ic_txpower = IEEE80211_TXPOWER_MAX; 759 #ifndef IEEE80211_STA_ONLY 760 mq_init(&ni->ni_savedq, IEEE80211_PS_MAX_QUEUE, IPL_NET); 761 #endif 762 } 763 764 void 765 ieee80211_node_detach(struct ifnet *ifp) 766 { 767 struct ieee80211com *ic = (void *)ifp; 768 769 if (ic->ic_bss != NULL) { 770 (*ic->ic_node_free)(ic, ic->ic_bss); 771 ic->ic_bss = NULL; 772 } 773 ieee80211_del_ess(ic, NULL, 0, 1); 774 ieee80211_free_allnodes(ic, 1); 775 #ifndef IEEE80211_STA_ONLY 776 free(ic->ic_aid_bitmap, M_DEVBUF, 777 howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t)); 778 free(ic->ic_tim_bitmap, M_DEVBUF, ic->ic_tim_len); 779 timeout_del(&ic->ic_inact_timeout); 780 timeout_del(&ic->ic_node_cache_timeout); 781 timeout_del(&ic->ic_tkip_micfail_timeout); 782 #endif 783 timeout_del(&ic->ic_rsn_timeout); 784 } 785 786 /* 787 * AP scanning support. 788 */ 789 790 /* 791 * Initialize the active channel set based on the set 792 * of available channels and the current PHY mode. 793 */ 794 void 795 ieee80211_reset_scan(struct ifnet *ifp) 796 { 797 struct ieee80211com *ic = (void *)ifp; 798 799 memcpy(ic->ic_chan_scan, ic->ic_chan_active, 800 sizeof(ic->ic_chan_active)); 801 /* NB: hack, setup so next_scan starts with the first channel */ 802 if (ic->ic_bss != NULL && ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC) 803 ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX]; 804 } 805 806 /* 807 * Increase a node's inactivity counter. 808 * This counter get reset to zero if a frame is received. 809 * This function is intended for station mode only. 810 * See ieee80211_node_cache_timeout() for hostap mode. 811 */ 812 void 813 ieee80211_node_raise_inact(void *arg, struct ieee80211_node *ni) 814 { 815 if (ni->ni_refcnt == 0 && ni->ni_inact < IEEE80211_INACT_SCAN) 816 ni->ni_inact++; 817 } 818 819 /* 820 * Begin an active scan. 821 */ 822 void 823 ieee80211_begin_scan(struct ifnet *ifp) 824 { 825 struct ieee80211com *ic = (void *)ifp; 826 827 /* 828 * In all but hostap mode scanning starts off in 829 * an active mode before switching to passive. 830 */ 831 #ifndef IEEE80211_STA_ONLY 832 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 833 #endif 834 { 835 ic->ic_flags |= IEEE80211_F_ASCAN; 836 ic->ic_stats.is_scan_active++; 837 } 838 #ifndef IEEE80211_STA_ONLY 839 else 840 ic->ic_stats.is_scan_passive++; 841 #endif 842 if (ifp->if_flags & IFF_DEBUG) 843 printf("%s: begin %s scan\n", ifp->if_xname, 844 (ic->ic_flags & IEEE80211_F_ASCAN) ? 845 "active" : "passive"); 846 847 848 if (ic->ic_opmode == IEEE80211_M_STA) { 849 ieee80211_node_cleanup(ic, ic->ic_bss); 850 ieee80211_iterate_nodes(ic, ieee80211_node_raise_inact, NULL); 851 } 852 853 /* 854 * Reset the current mode. Setting the current mode will also 855 * reset scan state. 856 */ 857 if (IFM_MODE(ic->ic_media.ifm_cur->ifm_media) == IFM_AUTO) 858 ic->ic_curmode = IEEE80211_MODE_AUTO; 859 ieee80211_setmode(ic, ic->ic_curmode); 860 861 ic->ic_scan_count = 0; 862 863 /* Scan the next channel. */ 864 ieee80211_next_scan(ifp); 865 } 866 867 /* 868 * Switch to the next channel marked for scanning. 869 */ 870 void 871 ieee80211_next_scan(struct ifnet *ifp) 872 { 873 struct ieee80211com *ic = (void *)ifp; 874 struct ieee80211_channel *chan; 875 876 chan = ic->ic_bss->ni_chan; 877 for (;;) { 878 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 879 chan = &ic->ic_channels[0]; 880 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 881 /* 882 * Ignore channels marked passive-only 883 * during an active scan. 884 */ 885 if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 || 886 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) 887 break; 888 } 889 if (chan == ic->ic_bss->ni_chan) { 890 ieee80211_end_scan(ifp); 891 return; 892 } 893 } 894 clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 895 DPRINTF(("chan %d->%d\n", 896 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan), 897 ieee80211_chan2ieee(ic, chan))); 898 ic->ic_bss->ni_chan = chan; 899 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 900 } 901 902 #ifndef IEEE80211_STA_ONLY 903 void 904 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 905 { 906 struct ieee80211_node *ni; 907 struct ifnet *ifp = &ic->ic_if; 908 909 ni = ic->ic_bss; 910 if (ifp->if_flags & IFF_DEBUG) 911 printf("%s: creating ibss\n", ifp->if_xname); 912 ic->ic_flags |= IEEE80211_F_SIBSS; 913 ni->ni_chan = chan; 914 ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)]; 915 ni->ni_txrate = 0; 916 IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr); 917 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 918 if (ic->ic_opmode == IEEE80211_M_IBSS) { 919 if ((ic->ic_flags & IEEE80211_F_DESBSSID) != 0) 920 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 921 else 922 ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 923 } 924 ni->ni_esslen = ic->ic_des_esslen; 925 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 926 ni->ni_rssi = 0; 927 ni->ni_rstamp = 0; 928 memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp)); 929 ni->ni_intval = ic->ic_lintval; 930 ni->ni_capinfo = IEEE80211_CAPINFO_IBSS; 931 if (ic->ic_flags & IEEE80211_F_WEPON) 932 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 933 if (ic->ic_flags & IEEE80211_F_HTON) { 934 const struct ieee80211_edca_ac_params *ac_qap; 935 struct ieee80211_edca_ac_params *ac; 936 int aci; 937 938 /* 939 * Configure HT protection. This will be updated later 940 * based on the number of non-HT nodes in the node cache. 941 */ 942 ic->ic_protmode = IEEE80211_PROT_NONE; 943 ni->ni_htop1 = IEEE80211_HTPROT_NONE; 944 /* Disallow Greenfield mode. None of our drivers support it. */ 945 ni->ni_htop1 |= IEEE80211_HTOP1_NONGF_STA; 946 if (ic->ic_update_htprot) 947 ic->ic_update_htprot(ic, ni); 948 949 /* Configure QoS EDCA parameters. */ 950 for (aci = 0; aci < EDCA_NUM_AC; aci++) { 951 ac = &ic->ic_edca_ac[aci]; 952 ac_qap = &ieee80211_qap_edca_table[ic->ic_curmode][aci]; 953 ac->ac_acm = ac_qap->ac_acm; 954 ac->ac_aifsn = ac_qap->ac_aifsn; 955 ac->ac_ecwmin = ac_qap->ac_ecwmin; 956 ac->ac_ecwmax = ac_qap->ac_ecwmax; 957 ac->ac_txoplimit = ac_qap->ac_txoplimit; 958 } 959 if (ic->ic_updateedca) 960 (*ic->ic_updateedca)(ic); 961 } 962 if (ic->ic_flags & IEEE80211_F_RSNON) { 963 struct ieee80211_key *k; 964 965 /* initialize 256-bit global key counter to a random value */ 966 arc4random_buf(ic->ic_globalcnt, EAPOL_KEY_NONCE_LEN); 967 968 ni->ni_rsnprotos = ic->ic_rsnprotos; 969 ni->ni_rsnakms = ic->ic_rsnakms; 970 ni->ni_rsnciphers = ic->ic_rsnciphers; 971 ni->ni_rsngroupcipher = ic->ic_rsngroupcipher; 972 ni->ni_rsngroupmgmtcipher = ic->ic_rsngroupmgmtcipher; 973 ni->ni_rsncaps = 0; 974 if (ic->ic_caps & IEEE80211_C_MFP) { 975 ni->ni_rsncaps |= IEEE80211_RSNCAP_MFPC; 976 if (ic->ic_flags & IEEE80211_F_MFPR) 977 ni->ni_rsncaps |= IEEE80211_RSNCAP_MFPR; 978 } 979 980 ic->ic_def_txkey = 1; 981 ic->ic_flags &= ~IEEE80211_F_COUNTERM; 982 k = &ic->ic_nw_keys[ic->ic_def_txkey]; 983 memset(k, 0, sizeof(*k)); 984 k->k_id = ic->ic_def_txkey; 985 k->k_cipher = ni->ni_rsngroupcipher; 986 k->k_flags = IEEE80211_KEY_GROUP | IEEE80211_KEY_TX; 987 k->k_len = ieee80211_cipher_keylen(k->k_cipher); 988 arc4random_buf(k->k_key, k->k_len); 989 (*ic->ic_set_key)(ic, ni, k); /* XXX */ 990 991 if (ic->ic_caps & IEEE80211_C_MFP) { 992 ic->ic_igtk_kid = 4; 993 k = &ic->ic_nw_keys[ic->ic_igtk_kid]; 994 memset(k, 0, sizeof(*k)); 995 k->k_id = ic->ic_igtk_kid; 996 k->k_cipher = ni->ni_rsngroupmgmtcipher; 997 k->k_flags = IEEE80211_KEY_IGTK | IEEE80211_KEY_TX; 998 k->k_len = 16; 999 arc4random_buf(k->k_key, k->k_len); 1000 (*ic->ic_set_key)(ic, ni, k); /* XXX */ 1001 } 1002 /* 1003 * In HostAP mode, multicast traffic is sent using ic_bss 1004 * as the Tx node, so mark our node as valid so we can send 1005 * multicast frames using the group key we've just configured. 1006 */ 1007 ni->ni_port_valid = 1; 1008 ni->ni_flags |= IEEE80211_NODE_TXPROT; 1009 1010 /* schedule a GTK/IGTK rekeying after 3600s */ 1011 timeout_add_sec(&ic->ic_rsn_timeout, 3600); 1012 } 1013 timeout_add_sec(&ic->ic_inact_timeout, IEEE80211_INACT_WAIT); 1014 timeout_add_sec(&ic->ic_node_cache_timeout, IEEE80211_CACHE_WAIT); 1015 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1016 } 1017 #endif /* IEEE80211_STA_ONLY */ 1018 1019 int 1020 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni, 1021 int bgscan) 1022 { 1023 u_int8_t rate; 1024 int fail; 1025 1026 fail = 0; 1027 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 1028 fail |= IEEE80211_NODE_ASSOCFAIL_CHAN; 1029 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 1030 ni->ni_chan != ic->ic_des_chan) 1031 fail |= IEEE80211_NODE_ASSOCFAIL_CHAN; 1032 #ifndef IEEE80211_STA_ONLY 1033 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1034 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 1035 fail |= IEEE80211_NODE_ASSOCFAIL_IBSS; 1036 } else 1037 #endif 1038 { 1039 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 1040 fail |= IEEE80211_NODE_ASSOCFAIL_IBSS; 1041 } 1042 if (ic->ic_flags & (IEEE80211_F_WEPON | IEEE80211_F_RSNON)) { 1043 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 1044 fail |= IEEE80211_NODE_ASSOCFAIL_PRIVACY; 1045 } else { 1046 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 1047 fail |= IEEE80211_NODE_ASSOCFAIL_PRIVACY; 1048 } 1049 1050 rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO); 1051 if (rate & IEEE80211_RATE_BASIC) 1052 fail |= IEEE80211_NODE_ASSOCFAIL_BASIC_RATE; 1053 if (ic->ic_des_esslen == 0) 1054 fail |= IEEE80211_NODE_ASSOCFAIL_ESSID; 1055 if (ic->ic_des_esslen != 0 && 1056 (ni->ni_esslen != ic->ic_des_esslen || 1057 memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) 1058 fail |= IEEE80211_NODE_ASSOCFAIL_ESSID; 1059 if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 1060 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 1061 fail |= IEEE80211_NODE_ASSOCFAIL_BSSID; 1062 1063 if (ic->ic_flags & IEEE80211_F_RSNON) { 1064 /* 1065 * If at least one RSN IE field from the AP's RSN IE fails 1066 * to overlap with any value the STA supports, the STA shall 1067 * decline to associate with that AP. 1068 */ 1069 if ((ni->ni_rsnprotos & ic->ic_rsnprotos) == 0) 1070 fail |= IEEE80211_NODE_ASSOCFAIL_WPA_PROTO; 1071 if ((ni->ni_rsnakms & ic->ic_rsnakms) == 0) 1072 fail |= IEEE80211_NODE_ASSOCFAIL_WPA_PROTO; 1073 if ((ni->ni_rsnakms & ic->ic_rsnakms & 1074 ~(IEEE80211_AKM_PSK | IEEE80211_AKM_SHA256_PSK)) == 0) { 1075 /* AP only supports PSK AKMPs */ 1076 if (!(ic->ic_flags & IEEE80211_F_PSK)) 1077 fail |= IEEE80211_NODE_ASSOCFAIL_WPA_PROTO; 1078 } 1079 if (ni->ni_rsngroupcipher != IEEE80211_CIPHER_WEP40 && 1080 ni->ni_rsngroupcipher != IEEE80211_CIPHER_TKIP && 1081 ni->ni_rsngroupcipher != IEEE80211_CIPHER_CCMP && 1082 ni->ni_rsngroupcipher != IEEE80211_CIPHER_WEP104) 1083 fail |= IEEE80211_NODE_ASSOCFAIL_WPA_PROTO; 1084 if ((ni->ni_rsnciphers & ic->ic_rsnciphers) == 0) 1085 fail |= IEEE80211_NODE_ASSOCFAIL_WPA_PROTO; 1086 1087 /* we only support BIP as the IGTK cipher */ 1088 if ((ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC) && 1089 ni->ni_rsngroupmgmtcipher != IEEE80211_CIPHER_BIP) 1090 fail |= IEEE80211_NODE_ASSOCFAIL_WPA_PROTO; 1091 1092 /* we do not support MFP but AP requires it */ 1093 if (!(ic->ic_caps & IEEE80211_C_MFP) && 1094 (ni->ni_rsncaps & IEEE80211_RSNCAP_MFPR)) 1095 fail |= IEEE80211_NODE_ASSOCFAIL_WPA_PROTO; 1096 1097 /* we require MFP but AP does not support it */ 1098 if ((ic->ic_caps & IEEE80211_C_MFP) && 1099 (ic->ic_flags & IEEE80211_F_MFPR) && 1100 !(ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC)) 1101 fail |= IEEE80211_NODE_ASSOCFAIL_WPA_PROTO; 1102 } 1103 1104 if (ic->ic_if.if_flags & IFF_DEBUG) { 1105 printf("%s: %c %s%c", ic->ic_if.if_xname, fail ? '-' : '+', 1106 ether_sprintf(ni->ni_bssid), 1107 fail & IEEE80211_NODE_ASSOCFAIL_BSSID ? '!' : ' '); 1108 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 1109 fail & IEEE80211_NODE_ASSOCFAIL_CHAN ? '!' : ' '); 1110 printf(" %+4d", ni->ni_rssi); 1111 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 1112 fail & IEEE80211_NODE_ASSOCFAIL_BASIC_RATE ? '!' : ' '); 1113 printf(" %4s%c", 1114 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 1115 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 1116 "????", 1117 fail & IEEE80211_NODE_ASSOCFAIL_IBSS ? '!' : ' '); 1118 printf(" %7s%c ", 1119 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 1120 "privacy" : "no", 1121 fail & IEEE80211_NODE_ASSOCFAIL_PRIVACY ? '!' : ' '); 1122 printf(" %3s%c ", 1123 (ic->ic_flags & IEEE80211_F_RSNON) ? 1124 "rsn" : "no", 1125 fail & IEEE80211_NODE_ASSOCFAIL_WPA_PROTO ? '!' : ' '); 1126 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 1127 printf("%s\n", 1128 fail & IEEE80211_NODE_ASSOCFAIL_ESSID ? "!" : ""); 1129 } 1130 1131 /* We don't care about unrelated networks during background scans. */ 1132 if (bgscan) { 1133 if ((fail & IEEE80211_NODE_ASSOCFAIL_ESSID) == 0) 1134 ni->ni_assoc_fail = fail; 1135 } else 1136 ni->ni_assoc_fail = fail; 1137 if ((fail & IEEE80211_NODE_ASSOCFAIL_ESSID) == 0) 1138 ic->ic_bss->ni_assoc_fail = ni->ni_assoc_fail; 1139 1140 return fail; 1141 } 1142 1143 struct ieee80211_node_switch_bss_arg { 1144 u_int8_t cur_macaddr[IEEE80211_ADDR_LEN]; 1145 u_int8_t sel_macaddr[IEEE80211_ADDR_LEN]; 1146 }; 1147 1148 /* Implements ni->ni_unref_cb(). */ 1149 void 1150 ieee80211_node_switch_bss(struct ieee80211com *ic, struct ieee80211_node *ni) 1151 { 1152 struct ifnet *ifp = &ic->ic_if; 1153 struct ieee80211_node_switch_bss_arg *sba = ni->ni_unref_arg; 1154 struct ieee80211_node *curbs, *selbs; 1155 1156 splassert(IPL_NET); 1157 1158 if ((ic->ic_flags & IEEE80211_F_BGSCAN) == 0) { 1159 free(sba, M_DEVBUF, sizeof(*sba)); 1160 return; 1161 } 1162 1163 ic->ic_xflags &= ~IEEE80211_F_TX_MGMT_ONLY; 1164 1165 selbs = ieee80211_find_node(ic, sba->sel_macaddr); 1166 if (selbs == NULL) { 1167 free(sba, M_DEVBUF, sizeof(*sba)); 1168 ic->ic_flags &= ~IEEE80211_F_BGSCAN; 1169 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 1170 return; 1171 } 1172 1173 curbs = ieee80211_find_node(ic, sba->cur_macaddr); 1174 if (curbs == NULL) { 1175 free(sba, M_DEVBUF, sizeof(*sba)); 1176 ic->ic_flags &= ~IEEE80211_F_BGSCAN; 1177 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 1178 return; 1179 } 1180 1181 if (ifp->if_flags & IFF_DEBUG) { 1182 printf("%s: roaming from %s chan %d ", 1183 ifp->if_xname, ether_sprintf(curbs->ni_macaddr), 1184 ieee80211_chan2ieee(ic, curbs->ni_chan)); 1185 printf("to %s chan %d\n", ether_sprintf(selbs->ni_macaddr), 1186 ieee80211_chan2ieee(ic, selbs->ni_chan)); 1187 } 1188 ieee80211_node_newstate(curbs, IEEE80211_STA_CACHE); 1189 ieee80211_node_join_bss(ic, selbs); /* frees arg and ic->ic_bss */ 1190 } 1191 1192 void 1193 ieee80211_node_join_bss(struct ieee80211com *ic, struct ieee80211_node *selbs) 1194 { 1195 enum ieee80211_phymode mode; 1196 struct ieee80211_node *ni; 1197 uint32_t assoc_fail = 0; 1198 1199 /* Reinitialize media mode and channels if needed. */ 1200 mode = ieee80211_chan2mode(ic, selbs->ni_chan); 1201 if (mode != ic->ic_curmode) 1202 ieee80211_setmode(ic, mode); 1203 1204 /* Keep recorded association failures for this BSS/ESS intact. */ 1205 if (IEEE80211_ADDR_EQ(ic->ic_bss->ni_macaddr, selbs->ni_macaddr) || 1206 (ic->ic_des_esslen > 0 && ic->ic_des_esslen == selbs->ni_esslen && 1207 memcmp(ic->ic_des_essid, selbs->ni_essid, selbs->ni_esslen) == 0)) 1208 assoc_fail = ic->ic_bss->ni_assoc_fail; 1209 1210 (*ic->ic_node_copy)(ic, ic->ic_bss, selbs); 1211 ni = ic->ic_bss; 1212 ni->ni_assoc_fail |= assoc_fail; 1213 1214 ic->ic_curmode = ieee80211_chan2mode(ic, ni->ni_chan); 1215 1216 /* Make sure we send valid rates in an association request. */ 1217 if (ic->ic_opmode == IEEE80211_M_STA) 1218 ieee80211_fix_rate(ic, ni, 1219 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 1220 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1221 1222 if (ic->ic_flags & IEEE80211_F_RSNON) 1223 ieee80211_choose_rsnparams(ic); 1224 else if (ic->ic_flags & IEEE80211_F_WEPON) 1225 ni->ni_rsncipher = IEEE80211_CIPHER_USEGROUP; 1226 1227 ieee80211_node_newstate(selbs, IEEE80211_STA_BSS); 1228 #ifndef IEEE80211_STA_ONLY 1229 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1230 ieee80211_fix_rate(ic, ni, IEEE80211_F_DOFRATE | 1231 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1232 if (ni->ni_rates.rs_nrates == 0) { 1233 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 1234 return; 1235 } 1236 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1237 } else 1238 #endif 1239 { 1240 int bgscan = ((ic->ic_flags & IEEE80211_F_BGSCAN) && 1241 ic->ic_opmode == IEEE80211_M_STA && 1242 ic->ic_state == IEEE80211_S_RUN); 1243 int auth_next = (ic->ic_opmode == IEEE80211_M_STA && 1244 ic->ic_state == IEEE80211_S_AUTH); 1245 int mgt = -1; 1246 1247 timeout_del(&ic->ic_bgscan_timeout); 1248 ic->ic_flags &= ~IEEE80211_F_BGSCAN; 1249 1250 /* 1251 * After a background scan, we have now switched APs. 1252 * Pretend we were just de-authed, which makes 1253 * ieee80211_new_state() try to re-auth and thus send 1254 * an AUTH frame to our newly selected AP. 1255 */ 1256 if (bgscan) 1257 mgt = IEEE80211_FC0_SUBTYPE_DEAUTH; 1258 /* 1259 * If we are trying another AP after the previous one 1260 * failed (state transition AUTH->AUTH), ensure that 1261 * ieee80211_new_state() tries to send another auth frame. 1262 */ 1263 else if (auth_next) 1264 mgt = IEEE80211_FC0_SUBTYPE_AUTH; 1265 1266 ieee80211_new_state(ic, IEEE80211_S_AUTH, mgt); 1267 } 1268 } 1269 1270 struct ieee80211_node * 1271 ieee80211_node_choose_bss(struct ieee80211com *ic, int bgscan, 1272 struct ieee80211_node **curbs) 1273 { 1274 struct ieee80211_node *ni, *nextbs, *selbs = NULL, 1275 *selbs2 = NULL, *selbs5 = NULL; 1276 uint8_t min_5ghz_rssi; 1277 1278 ni = RBT_MIN(ieee80211_tree, &ic->ic_tree); 1279 1280 for (; ni != NULL; ni = nextbs) { 1281 nextbs = RBT_NEXT(ieee80211_tree, ni); 1282 if (ni->ni_fails) { 1283 /* 1284 * The configuration of the access points may change 1285 * during my scan. So delete the entry for the AP 1286 * and retry to associate if there is another beacon. 1287 */ 1288 if (ni->ni_fails++ > 2) 1289 ieee80211_free_node(ic, ni); 1290 continue; 1291 } 1292 1293 if (curbs && ieee80211_node_cmp(ic->ic_bss, ni) == 0) 1294 *curbs = ni; 1295 1296 if (ieee80211_match_bss(ic, ni, bgscan) != 0) 1297 continue; 1298 1299 if (ic->ic_caps & IEEE80211_C_SCANALLBAND) { 1300 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) && 1301 (selbs2 == NULL || ni->ni_rssi > selbs2->ni_rssi)) 1302 selbs2 = ni; 1303 else if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) && 1304 (selbs5 == NULL || ni->ni_rssi > selbs5->ni_rssi)) 1305 selbs5 = ni; 1306 } else if (selbs == NULL || ni->ni_rssi > selbs->ni_rssi) 1307 selbs = ni; 1308 } 1309 1310 if (ic->ic_max_rssi) 1311 min_5ghz_rssi = IEEE80211_RSSI_THRES_RATIO_5GHZ; 1312 else 1313 min_5ghz_rssi = (uint8_t)IEEE80211_RSSI_THRES_5GHZ; 1314 1315 /* 1316 * Prefer a 5Ghz AP even if its RSSI is weaker than the best 2Ghz AP 1317 * (as long as it meets the minimum RSSI threshold) since the 5Ghz band 1318 * is usually less saturated. 1319 */ 1320 if (selbs5 && selbs5->ni_rssi > min_5ghz_rssi) 1321 selbs = selbs5; 1322 else if (selbs5 && selbs2) 1323 selbs = (selbs5->ni_rssi >= selbs2->ni_rssi ? selbs5 : selbs2); 1324 else if (selbs2) 1325 selbs = selbs2; 1326 else if (selbs5) 1327 selbs = selbs5; 1328 1329 return selbs; 1330 } 1331 1332 /* 1333 * Complete a scan of potential channels. 1334 */ 1335 void 1336 ieee80211_end_scan(struct ifnet *ifp) 1337 { 1338 struct ieee80211com *ic = (void *)ifp; 1339 struct ieee80211_node *ni, *selbs = NULL, *curbs = NULL; 1340 int bgscan = ((ic->ic_flags & IEEE80211_F_BGSCAN) && 1341 ic->ic_opmode == IEEE80211_M_STA && 1342 ic->ic_state == IEEE80211_S_RUN); 1343 1344 if (ifp->if_flags & IFF_DEBUG) 1345 printf("%s: end %s scan\n", ifp->if_xname, 1346 bgscan ? "background" : 1347 ((ic->ic_flags & IEEE80211_F_ASCAN) ? 1348 "active" : "passive")); 1349 1350 if (ic->ic_scan_count) 1351 ic->ic_flags &= ~IEEE80211_F_ASCAN; 1352 1353 if (ic->ic_opmode == IEEE80211_M_STA) 1354 ieee80211_clean_inactive_nodes(ic, IEEE80211_INACT_SCAN); 1355 1356 ni = RBT_MIN(ieee80211_tree, &ic->ic_tree); 1357 1358 #ifndef IEEE80211_STA_ONLY 1359 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 1360 /* XXX off stack? */ 1361 u_char occupied[howmany(IEEE80211_CHAN_MAX, NBBY)]; 1362 int i, fail; 1363 1364 /* 1365 * The passive scan to look for existing AP's completed, 1366 * select a channel to camp on. Identify the channels 1367 * that already have one or more AP's and try to locate 1368 * an unoccupied one. If that fails, pick a random 1369 * channel from the active set. 1370 */ 1371 memset(occupied, 0, sizeof(occupied)); 1372 RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) 1373 setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan)); 1374 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 1375 if (isset(ic->ic_chan_active, i) && isclr(occupied, i)) 1376 break; 1377 if (i == IEEE80211_CHAN_MAX) { 1378 fail = arc4random() & 3; /* random 0-3 */ 1379 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 1380 if (isset(ic->ic_chan_active, i) && fail-- == 0) 1381 break; 1382 } 1383 ieee80211_create_ibss(ic, &ic->ic_channels[i]); 1384 return; 1385 } 1386 #endif 1387 if (ni == NULL) { 1388 DPRINTF(("no scan candidate\n")); 1389 notfound: 1390 1391 #ifndef IEEE80211_STA_ONLY 1392 if (ic->ic_opmode == IEEE80211_M_IBSS && 1393 (ic->ic_flags & IEEE80211_F_IBSSON) && 1394 ic->ic_des_esslen != 0) { 1395 ieee80211_create_ibss(ic, ic->ic_ibss_chan); 1396 return; 1397 } 1398 #endif 1399 /* 1400 * Reset the list of channels to scan and scan the next mode 1401 * if nothing has been found. 1402 * If the device scans all bands in one fell swoop, return 1403 * current scan results to userspace regardless of mode. 1404 * This will loop forever until an access point is found. 1405 */ 1406 ieee80211_reset_scan(ifp); 1407 if (ieee80211_next_mode(ifp) == IEEE80211_MODE_AUTO || 1408 (ic->ic_caps & IEEE80211_C_SCANALLBAND)) 1409 ic->ic_scan_count++; 1410 1411 ieee80211_next_scan(ifp); 1412 return; 1413 } 1414 1415 /* Possibly switch which ssid we are associated with */ 1416 if (!bgscan && ic->ic_opmode == IEEE80211_M_STA) 1417 ieee80211_switch_ess(ic); 1418 1419 selbs = ieee80211_node_choose_bss(ic, bgscan, &curbs); 1420 if (bgscan) { 1421 struct ieee80211_node_switch_bss_arg *arg; 1422 1423 /* AP disappeared? Should not happen. */ 1424 if (selbs == NULL || curbs == NULL) { 1425 ic->ic_flags &= ~IEEE80211_F_BGSCAN; 1426 goto notfound; 1427 } 1428 1429 /* 1430 * After a background scan we might end up choosing the 1431 * same AP again. Or the newly selected AP's RSSI level 1432 * might be low enough to trigger another background scan. 1433 * Do not change ic->ic_bss in these cases and make 1434 * background scans less frequent. 1435 */ 1436 if (selbs == curbs || !(*ic->ic_node_checkrssi)(ic, selbs)) { 1437 if (ic->ic_bgscan_fail < IEEE80211_BGSCAN_FAIL_MAX) { 1438 if (ic->ic_bgscan_fail <= 0) 1439 ic->ic_bgscan_fail = 1; 1440 else 1441 ic->ic_bgscan_fail *= 2; 1442 } 1443 ic->ic_flags &= ~IEEE80211_F_BGSCAN; 1444 return; 1445 } 1446 1447 arg = malloc(sizeof(*arg), M_DEVBUF, M_NOWAIT | M_ZERO); 1448 if (arg == NULL) { 1449 ic->ic_flags &= ~IEEE80211_F_BGSCAN; 1450 return; 1451 } 1452 1453 ic->ic_bgscan_fail = 0; 1454 1455 /* 1456 * We are going to switch APs. Stop A-MPDU Tx and 1457 * queue a de-auth frame addressed to our current AP. 1458 */ 1459 ieee80211_stop_ampdu_tx(ic, ic->ic_bss, 1460 IEEE80211_FC0_SUBTYPE_DEAUTH); 1461 if (IEEE80211_SEND_MGMT(ic, ic->ic_bss, 1462 IEEE80211_FC0_SUBTYPE_DEAUTH, 1463 IEEE80211_REASON_AUTH_LEAVE) != 0) { 1464 ic->ic_flags &= ~IEEE80211_F_BGSCAN; 1465 free(arg, M_DEVBUF, sizeof(*arg)); 1466 return; 1467 } 1468 1469 /* Prevent dispatch of additional data frames to hardware. */ 1470 ic->ic_xflags |= IEEE80211_F_TX_MGMT_ONLY; 1471 1472 /* 1473 * Install a callback which will switch us to the new AP once 1474 * all dispatched frames have been processed by hardware. 1475 */ 1476 IEEE80211_ADDR_COPY(arg->cur_macaddr, curbs->ni_macaddr); 1477 IEEE80211_ADDR_COPY(arg->sel_macaddr, selbs->ni_macaddr); 1478 ic->ic_bss->ni_unref_arg = arg; 1479 ic->ic_bss->ni_unref_arg_size = sizeof(*arg); 1480 ic->ic_bss->ni_unref_cb = ieee80211_node_switch_bss; 1481 /* F_BGSCAN flag gets cleared in ieee80211_node_join_bss(). */ 1482 return; 1483 } else if (selbs == NULL) 1484 goto notfound; 1485 1486 ieee80211_node_join_bss(ic, selbs); 1487 } 1488 1489 /* 1490 * Autoselect the best RSN parameters (protocol, AKMP, pairwise cipher...) 1491 * that are supported by both peers (STA mode only). 1492 */ 1493 void 1494 ieee80211_choose_rsnparams(struct ieee80211com *ic) 1495 { 1496 struct ieee80211_node *ni = ic->ic_bss; 1497 struct ieee80211_pmk *pmk; 1498 1499 /* filter out unsupported protocol versions */ 1500 ni->ni_rsnprotos &= ic->ic_rsnprotos; 1501 /* prefer RSN (aka WPA2) over WPA */ 1502 if (ni->ni_rsnprotos & IEEE80211_PROTO_RSN) 1503 ni->ni_rsnprotos = IEEE80211_PROTO_RSN; 1504 else 1505 ni->ni_rsnprotos = IEEE80211_PROTO_WPA; 1506 1507 /* filter out unsupported AKMPs */ 1508 ni->ni_rsnakms &= ic->ic_rsnakms; 1509 /* prefer SHA-256 based AKMPs */ 1510 if ((ic->ic_flags & IEEE80211_F_PSK) && (ni->ni_rsnakms & 1511 (IEEE80211_AKM_PSK | IEEE80211_AKM_SHA256_PSK))) { 1512 /* AP supports PSK AKMP and a PSK is configured */ 1513 if (ni->ni_rsnakms & IEEE80211_AKM_SHA256_PSK) 1514 ni->ni_rsnakms = IEEE80211_AKM_SHA256_PSK; 1515 else 1516 ni->ni_rsnakms = IEEE80211_AKM_PSK; 1517 } else { 1518 if (ni->ni_rsnakms & IEEE80211_AKM_SHA256_8021X) 1519 ni->ni_rsnakms = IEEE80211_AKM_SHA256_8021X; 1520 else 1521 ni->ni_rsnakms = IEEE80211_AKM_8021X; 1522 /* check if we have a cached PMK for this AP */ 1523 if (ni->ni_rsnprotos == IEEE80211_PROTO_RSN && 1524 (pmk = ieee80211_pmksa_find(ic, ni, NULL)) != NULL) { 1525 memcpy(ni->ni_pmkid, pmk->pmk_pmkid, 1526 IEEE80211_PMKID_LEN); 1527 ni->ni_flags |= IEEE80211_NODE_PMKID; 1528 } 1529 } 1530 1531 /* filter out unsupported pairwise ciphers */ 1532 ni->ni_rsnciphers &= ic->ic_rsnciphers; 1533 /* prefer CCMP over TKIP */ 1534 if (ni->ni_rsnciphers & IEEE80211_CIPHER_CCMP) 1535 ni->ni_rsnciphers = IEEE80211_CIPHER_CCMP; 1536 else 1537 ni->ni_rsnciphers = IEEE80211_CIPHER_TKIP; 1538 ni->ni_rsncipher = ni->ni_rsnciphers; 1539 1540 /* use MFP if we both support it */ 1541 if ((ic->ic_caps & IEEE80211_C_MFP) && 1542 (ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC)) 1543 ni->ni_flags |= IEEE80211_NODE_MFP; 1544 } 1545 1546 int 1547 ieee80211_get_rate(struct ieee80211com *ic) 1548 { 1549 u_int8_t (*rates)[IEEE80211_RATE_MAXSIZE]; 1550 int rate; 1551 1552 rates = &ic->ic_bss->ni_rates.rs_rates; 1553 1554 if (ic->ic_fixed_rate != -1) 1555 rate = (*rates)[ic->ic_fixed_rate]; 1556 else if (ic->ic_state == IEEE80211_S_RUN) 1557 rate = (*rates)[ic->ic_bss->ni_txrate]; 1558 else 1559 rate = 0; 1560 1561 return rate & IEEE80211_RATE_VAL; 1562 } 1563 1564 struct ieee80211_node * 1565 ieee80211_node_alloc(struct ieee80211com *ic) 1566 { 1567 return malloc(sizeof(struct ieee80211_node), M_DEVBUF, 1568 M_NOWAIT | M_ZERO); 1569 } 1570 1571 void 1572 ieee80211_node_cleanup(struct ieee80211com *ic, struct ieee80211_node *ni) 1573 { 1574 if (ni->ni_rsnie != NULL) { 1575 free(ni->ni_rsnie, M_DEVBUF, 2 + ni->ni_rsnie[1]); 1576 ni->ni_rsnie = NULL; 1577 } 1578 ieee80211_ba_del(ni); 1579 ni->ni_unref_cb = NULL; 1580 free(ni->ni_unref_arg, M_DEVBUF, ni->ni_unref_arg_size); 1581 ni->ni_unref_arg = NULL; 1582 ni->ni_unref_arg_size = 0; 1583 } 1584 1585 void 1586 ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni) 1587 { 1588 ieee80211_node_cleanup(ic, ni); 1589 free(ni, M_DEVBUF, 0); 1590 } 1591 1592 void 1593 ieee80211_node_copy(struct ieee80211com *ic, 1594 struct ieee80211_node *dst, const struct ieee80211_node *src) 1595 { 1596 ieee80211_node_cleanup(ic, dst); 1597 *dst = *src; 1598 dst->ni_rsnie = NULL; 1599 if (src->ni_rsnie != NULL) 1600 ieee80211_save_ie(src->ni_rsnie, &dst->ni_rsnie); 1601 ieee80211_node_set_timeouts(dst); 1602 #ifndef IEEE80211_STA_ONLY 1603 mq_init(&dst->ni_savedq, IEEE80211_PS_MAX_QUEUE, IPL_NET); 1604 #endif 1605 } 1606 1607 u_int8_t 1608 ieee80211_node_getrssi(struct ieee80211com *ic, 1609 const struct ieee80211_node *ni) 1610 { 1611 return ni->ni_rssi; 1612 } 1613 1614 int 1615 ieee80211_node_checkrssi(struct ieee80211com *ic, 1616 const struct ieee80211_node *ni) 1617 { 1618 uint8_t thres; 1619 1620 if (ni->ni_chan == IEEE80211_CHAN_ANYC) 1621 return 0; 1622 1623 if (ic->ic_max_rssi) { 1624 thres = (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) ? 1625 IEEE80211_RSSI_THRES_RATIO_2GHZ : 1626 IEEE80211_RSSI_THRES_RATIO_5GHZ; 1627 return ((ni->ni_rssi * 100) / ic->ic_max_rssi >= thres); 1628 } 1629 1630 thres = (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) ? 1631 IEEE80211_RSSI_THRES_2GHZ : 1632 IEEE80211_RSSI_THRES_5GHZ; 1633 return (ni->ni_rssi >= (u_int8_t)thres); 1634 } 1635 1636 void 1637 ieee80211_node_set_timeouts(struct ieee80211_node *ni) 1638 { 1639 int i; 1640 1641 #ifndef IEEE80211_STA_ONLY 1642 timeout_set(&ni->ni_eapol_to, ieee80211_eapol_timeout, ni); 1643 timeout_set(&ni->ni_sa_query_to, ieee80211_sa_query_timeout, ni); 1644 #endif 1645 timeout_set(&ni->ni_addba_req_to[EDCA_AC_BE], 1646 ieee80211_node_addba_request_ac_be_to, ni); 1647 timeout_set(&ni->ni_addba_req_to[EDCA_AC_BK], 1648 ieee80211_node_addba_request_ac_bk_to, ni); 1649 timeout_set(&ni->ni_addba_req_to[EDCA_AC_VI], 1650 ieee80211_node_addba_request_ac_vi_to, ni); 1651 timeout_set(&ni->ni_addba_req_to[EDCA_AC_VO], 1652 ieee80211_node_addba_request_ac_vo_to, ni); 1653 for (i = 0; i < nitems(ni->ni_addba_req_intval); i++) 1654 ni->ni_addba_req_intval[i] = 1; 1655 } 1656 1657 void 1658 ieee80211_setup_node(struct ieee80211com *ic, 1659 struct ieee80211_node *ni, const u_int8_t *macaddr) 1660 { 1661 int i, s; 1662 1663 DPRINTF(("%s\n", ether_sprintf((u_int8_t *)macaddr))); 1664 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 1665 ieee80211_node_newstate(ni, IEEE80211_STA_CACHE); 1666 1667 ni->ni_ic = ic; /* back-pointer */ 1668 /* Initialize cached last sequence numbers with invalid values. */ 1669 ni->ni_rxseq = 0xffffU; 1670 for (i=0; i < IEEE80211_NUM_TID; ++i) 1671 ni->ni_qos_rxseqs[i] = 0xffffU; 1672 #ifndef IEEE80211_STA_ONLY 1673 mq_init(&ni->ni_savedq, IEEE80211_PS_MAX_QUEUE, IPL_NET); 1674 #endif 1675 ieee80211_node_set_timeouts(ni); 1676 1677 s = splnet(); 1678 RBT_INSERT(ieee80211_tree, &ic->ic_tree, ni); 1679 ic->ic_nnodes++; 1680 splx(s); 1681 } 1682 1683 struct ieee80211_node * 1684 ieee80211_alloc_node(struct ieee80211com *ic, const u_int8_t *macaddr) 1685 { 1686 struct ieee80211_node *ni = ieee80211_alloc_node_helper(ic); 1687 if (ni != NULL) 1688 ieee80211_setup_node(ic, ni, macaddr); 1689 else 1690 ic->ic_stats.is_rx_nodealloc++; 1691 return ni; 1692 } 1693 1694 struct ieee80211_node * 1695 ieee80211_dup_bss(struct ieee80211com *ic, const u_int8_t *macaddr) 1696 { 1697 struct ieee80211_node *ni = ieee80211_alloc_node_helper(ic); 1698 if (ni != NULL) { 1699 ieee80211_setup_node(ic, ni, macaddr); 1700 /* 1701 * Inherit from ic_bss. 1702 */ 1703 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 1704 ni->ni_chan = ic->ic_bss->ni_chan; 1705 } else 1706 ic->ic_stats.is_rx_nodealloc++; 1707 return ni; 1708 } 1709 1710 struct ieee80211_node * 1711 ieee80211_find_node(struct ieee80211com *ic, const u_int8_t *macaddr) 1712 { 1713 struct ieee80211_node *ni; 1714 int cmp; 1715 1716 /* similar to RBT_FIND except we compare keys, not nodes */ 1717 ni = RBT_ROOT(ieee80211_tree, &ic->ic_tree); 1718 while (ni != NULL) { 1719 cmp = memcmp(macaddr, ni->ni_macaddr, IEEE80211_ADDR_LEN); 1720 if (cmp < 0) 1721 ni = RBT_LEFT(ieee80211_tree, ni); 1722 else if (cmp > 0) 1723 ni = RBT_RIGHT(ieee80211_tree, ni); 1724 else 1725 break; 1726 } 1727 return ni; 1728 } 1729 1730 /* 1731 * Return a reference to the appropriate node for sending 1732 * a data frame. This handles node discovery in adhoc networks. 1733 * 1734 * Drivers will call this, so increase the reference count before 1735 * returning the node. 1736 */ 1737 struct ieee80211_node * 1738 ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr) 1739 { 1740 #ifndef IEEE80211_STA_ONLY 1741 struct ieee80211_node *ni; 1742 int s; 1743 #endif 1744 1745 /* 1746 * The destination address should be in the node table 1747 * unless we are operating in station mode or this is a 1748 * multicast/broadcast frame. 1749 */ 1750 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr)) 1751 return ieee80211_ref_node(ic->ic_bss); 1752 1753 #ifndef IEEE80211_STA_ONLY 1754 s = splnet(); 1755 ni = ieee80211_find_node(ic, macaddr); 1756 splx(s); 1757 if (ni == NULL) { 1758 if (ic->ic_opmode != IEEE80211_M_IBSS && 1759 ic->ic_opmode != IEEE80211_M_AHDEMO) 1760 return NULL; 1761 1762 /* 1763 * Fake up a node; this handles node discovery in 1764 * adhoc mode. Note that for the driver's benefit 1765 * we we treat this like an association so the driver 1766 * has an opportunity to setup its private state. 1767 * 1768 * XXX need better way to handle this; issue probe 1769 * request so we can deduce rate set, etc. 1770 */ 1771 if ((ni = ieee80211_dup_bss(ic, macaddr)) == NULL) 1772 return NULL; 1773 /* XXX no rate negotiation; just dup */ 1774 ni->ni_rates = ic->ic_bss->ni_rates; 1775 ni->ni_txrate = 0; 1776 if (ic->ic_newassoc) 1777 (*ic->ic_newassoc)(ic, ni, 1); 1778 } 1779 return ieee80211_ref_node(ni); 1780 #else 1781 return NULL; /* can't get there */ 1782 #endif /* IEEE80211_STA_ONLY */ 1783 } 1784 1785 /* 1786 * It is usually desirable to process a Rx packet using its sender's 1787 * node-record instead of the BSS record. 1788 * 1789 * - AP mode: keep a node-record for every authenticated/associated 1790 * station *in the BSS*. For future use, we also track neighboring 1791 * APs, since they might belong to the same ESS. APs in the same 1792 * ESS may bridge packets to each other, forming a Wireless 1793 * Distribution System (WDS). 1794 * 1795 * - IBSS mode: keep a node-record for every station *in the BSS*. 1796 * Also track neighboring stations by their beacons/probe responses. 1797 * 1798 * - monitor mode: keep a node-record for every sender, regardless 1799 * of BSS. 1800 * 1801 * - STA mode: the only available node-record is the BSS record, 1802 * ic->ic_bss. 1803 * 1804 * Of all the 802.11 Control packets, only the node-records for 1805 * RTS packets node-record can be looked up. 1806 * 1807 * Return non-zero if the packet's node-record is kept, zero 1808 * otherwise. 1809 */ 1810 static __inline int 1811 ieee80211_needs_rxnode(struct ieee80211com *ic, 1812 const struct ieee80211_frame *wh, const u_int8_t **bssid) 1813 { 1814 int monitor, rc = 0; 1815 1816 monitor = (ic->ic_opmode == IEEE80211_M_MONITOR); 1817 1818 *bssid = NULL; 1819 1820 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 1821 case IEEE80211_FC0_TYPE_CTL: 1822 if (!monitor) 1823 break; 1824 return (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 1825 IEEE80211_FC0_SUBTYPE_RTS; 1826 case IEEE80211_FC0_TYPE_MGT: 1827 *bssid = wh->i_addr3; 1828 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) { 1829 case IEEE80211_FC0_SUBTYPE_BEACON: 1830 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 1831 break; 1832 default: 1833 #ifndef IEEE80211_STA_ONLY 1834 if (ic->ic_opmode == IEEE80211_M_STA) 1835 break; 1836 rc = IEEE80211_ADDR_EQ(*bssid, ic->ic_bss->ni_bssid) || 1837 IEEE80211_ADDR_EQ(*bssid, etherbroadcastaddr); 1838 #endif 1839 break; 1840 } 1841 break; 1842 case IEEE80211_FC0_TYPE_DATA: 1843 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 1844 case IEEE80211_FC1_DIR_NODS: 1845 *bssid = wh->i_addr3; 1846 #ifndef IEEE80211_STA_ONLY 1847 if (ic->ic_opmode == IEEE80211_M_IBSS || 1848 ic->ic_opmode == IEEE80211_M_AHDEMO) 1849 rc = IEEE80211_ADDR_EQ(*bssid, 1850 ic->ic_bss->ni_bssid); 1851 #endif 1852 break; 1853 case IEEE80211_FC1_DIR_TODS: 1854 *bssid = wh->i_addr1; 1855 #ifndef IEEE80211_STA_ONLY 1856 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1857 rc = IEEE80211_ADDR_EQ(*bssid, 1858 ic->ic_bss->ni_bssid); 1859 #endif 1860 break; 1861 case IEEE80211_FC1_DIR_FROMDS: 1862 case IEEE80211_FC1_DIR_DSTODS: 1863 *bssid = wh->i_addr2; 1864 #ifndef IEEE80211_STA_ONLY 1865 rc = (ic->ic_opmode == IEEE80211_M_HOSTAP); 1866 #endif 1867 break; 1868 } 1869 break; 1870 } 1871 return monitor || rc; 1872 } 1873 1874 /* 1875 * Drivers call this, so increase the reference count before returning 1876 * the node. 1877 */ 1878 struct ieee80211_node * 1879 ieee80211_find_rxnode(struct ieee80211com *ic, 1880 const struct ieee80211_frame *wh) 1881 { 1882 static const u_int8_t zero[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 1883 struct ieee80211_node *ni; 1884 const u_int8_t *bssid; 1885 int s; 1886 1887 if (!ieee80211_needs_rxnode(ic, wh, &bssid)) 1888 return ieee80211_ref_node(ic->ic_bss); 1889 1890 s = splnet(); 1891 ni = ieee80211_find_node(ic, wh->i_addr2); 1892 splx(s); 1893 1894 if (ni != NULL) 1895 return ieee80211_ref_node(ni); 1896 #ifndef IEEE80211_STA_ONLY 1897 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1898 return ieee80211_ref_node(ic->ic_bss); 1899 #endif 1900 /* XXX see remarks in ieee80211_find_txnode */ 1901 /* XXX no rate negotiation; just dup */ 1902 if ((ni = ieee80211_dup_bss(ic, wh->i_addr2)) == NULL) 1903 return ieee80211_ref_node(ic->ic_bss); 1904 1905 IEEE80211_ADDR_COPY(ni->ni_bssid, (bssid != NULL) ? bssid : zero); 1906 1907 ni->ni_rates = ic->ic_bss->ni_rates; 1908 ni->ni_txrate = 0; 1909 if (ic->ic_newassoc) 1910 (*ic->ic_newassoc)(ic, ni, 1); 1911 1912 DPRINTF(("faked-up node %p for %s\n", ni, 1913 ether_sprintf((u_int8_t *)wh->i_addr2))); 1914 1915 return ieee80211_ref_node(ni); 1916 } 1917 1918 struct ieee80211_node * 1919 ieee80211_find_node_for_beacon(struct ieee80211com *ic, 1920 const u_int8_t *macaddr, const struct ieee80211_channel *chan, 1921 const char *ssid, u_int8_t rssi) 1922 { 1923 struct ieee80211_node *ni, *keep = NULL; 1924 int s, score = 0; 1925 1926 if ((ni = ieee80211_find_node(ic, macaddr)) != NULL) { 1927 s = splnet(); 1928 1929 if (ni->ni_chan != chan && ni->ni_rssi >= rssi) 1930 score++; 1931 if (ssid[1] == 0 && ni->ni_esslen != 0) 1932 score++; 1933 if (score > 0) 1934 keep = ni; 1935 1936 splx(s); 1937 } 1938 1939 return (keep); 1940 } 1941 1942 void 1943 ieee80211_ba_del(struct ieee80211_node *ni) 1944 { 1945 int tid; 1946 1947 for (tid = 0; tid < nitems(ni->ni_rx_ba); tid++) { 1948 struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid]; 1949 if (ba->ba_state != IEEE80211_BA_INIT) { 1950 if (timeout_pending(&ba->ba_to)) 1951 timeout_del(&ba->ba_to); 1952 if (timeout_pending(&ba->ba_gap_to)) 1953 timeout_del(&ba->ba_gap_to); 1954 ba->ba_state = IEEE80211_BA_INIT; 1955 } 1956 } 1957 1958 for (tid = 0; tid < nitems(ni->ni_tx_ba); tid++) { 1959 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 1960 if (ba->ba_state != IEEE80211_BA_INIT) { 1961 if (timeout_pending(&ba->ba_to)) 1962 timeout_del(&ba->ba_to); 1963 ba->ba_state = IEEE80211_BA_INIT; 1964 } 1965 } 1966 1967 timeout_del(&ni->ni_addba_req_to[EDCA_AC_BE]); 1968 timeout_del(&ni->ni_addba_req_to[EDCA_AC_BK]); 1969 timeout_del(&ni->ni_addba_req_to[EDCA_AC_VI]); 1970 timeout_del(&ni->ni_addba_req_to[EDCA_AC_VO]); 1971 } 1972 1973 void 1974 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni) 1975 { 1976 if (ni == ic->ic_bss) 1977 panic("freeing bss node"); 1978 1979 splassert(IPL_NET); 1980 1981 DPRINTF(("%s\n", ether_sprintf(ni->ni_macaddr))); 1982 #ifndef IEEE80211_STA_ONLY 1983 timeout_del(&ni->ni_eapol_to); 1984 timeout_del(&ni->ni_sa_query_to); 1985 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1986 #endif 1987 ieee80211_ba_del(ni); 1988 RBT_REMOVE(ieee80211_tree, &ic->ic_tree, ni); 1989 ic->ic_nnodes--; 1990 #ifndef IEEE80211_STA_ONLY 1991 if (mq_purge(&ni->ni_savedq) > 0) { 1992 if (ic->ic_set_tim != NULL) 1993 (*ic->ic_set_tim)(ic, ni->ni_associd, 0); 1994 } 1995 #endif 1996 (*ic->ic_node_free)(ic, ni); 1997 /* TBD indicate to drivers that a new node can be allocated */ 1998 } 1999 2000 void 2001 ieee80211_release_node(struct ieee80211com *ic, struct ieee80211_node *ni) 2002 { 2003 int s; 2004 2005 DPRINTF(("%s refcnt %u\n", ether_sprintf(ni->ni_macaddr), 2006 ni->ni_refcnt)); 2007 s = splnet(); 2008 if (ieee80211_node_decref(ni) == 0) { 2009 if (ni->ni_unref_cb) { 2010 (*ni->ni_unref_cb)(ic, ni); 2011 ni->ni_unref_cb = NULL; 2012 /* Freed by callback if necessary: */ 2013 ni->ni_unref_arg = NULL; 2014 ni->ni_unref_arg_size = 0; 2015 } 2016 if (ni->ni_state == IEEE80211_STA_COLLECT) 2017 ieee80211_free_node(ic, ni); 2018 } 2019 splx(s); 2020 } 2021 2022 void 2023 ieee80211_free_allnodes(struct ieee80211com *ic, int clear_ic_bss) 2024 { 2025 struct ieee80211_node *ni; 2026 int s; 2027 2028 DPRINTF(("freeing all nodes\n")); 2029 s = splnet(); 2030 while ((ni = RBT_MIN(ieee80211_tree, &ic->ic_tree)) != NULL) 2031 ieee80211_free_node(ic, ni); 2032 splx(s); 2033 2034 if (clear_ic_bss && ic->ic_bss != NULL) 2035 ieee80211_node_cleanup(ic, ic->ic_bss); /* for station mode */ 2036 } 2037 2038 void 2039 ieee80211_clean_cached(struct ieee80211com *ic) 2040 { 2041 struct ieee80211_node *ni, *next_ni; 2042 int s; 2043 2044 s = splnet(); 2045 for (ni = RBT_MIN(ieee80211_tree, &ic->ic_tree); 2046 ni != NULL; ni = next_ni) { 2047 next_ni = RBT_NEXT(ieee80211_tree, ni); 2048 if (ni->ni_state == IEEE80211_STA_CACHE) 2049 ieee80211_free_node(ic, ni); 2050 } 2051 splx(s); 2052 } 2053 /* 2054 * Timeout inactive nodes. 2055 * 2056 * If called because of a cache timeout, which happens only in hostap and ibss 2057 * modes, clean all inactive cached or authenticated nodes but don't de-auth 2058 * any associated nodes. Also update HT protection settings. 2059 * 2060 * Else, this function is called because a new node must be allocated but the 2061 * node cache is full. In this case, return as soon as a free slot was made 2062 * available. If acting as hostap, clean cached nodes regardless of their 2063 * recent activity and also allow de-authing of authenticated nodes older 2064 * than one cache wait interval, and de-authing of inactive associated nodes. 2065 */ 2066 void 2067 ieee80211_clean_nodes(struct ieee80211com *ic, int cache_timeout) 2068 { 2069 struct ieee80211_node *ni, *next_ni; 2070 u_int gen = ic->ic_scangen++; /* NB: ok 'cuz single-threaded*/ 2071 int s; 2072 #ifndef IEEE80211_STA_ONLY 2073 int nnodes = 0, nonht = 0, nonhtassoc = 0; 2074 struct ifnet *ifp = &ic->ic_if; 2075 enum ieee80211_htprot htprot = IEEE80211_HTPROT_NONE; 2076 enum ieee80211_protmode protmode = IEEE80211_PROT_NONE; 2077 #endif 2078 2079 s = splnet(); 2080 for (ni = RBT_MIN(ieee80211_tree, &ic->ic_tree); 2081 ni != NULL; ni = next_ni) { 2082 next_ni = RBT_NEXT(ieee80211_tree, ni); 2083 if (!cache_timeout && ic->ic_nnodes < ic->ic_max_nnodes) 2084 break; 2085 if (ni->ni_scangen == gen) /* previously handled */ 2086 continue; 2087 #ifndef IEEE80211_STA_ONLY 2088 nnodes++; 2089 if ((ic->ic_flags & IEEE80211_F_HTON) && cache_timeout) { 2090 /* 2091 * Check if node supports 802.11n. 2092 * Only require HT capabilities IE for this check. 2093 * Nodes might never reveal their supported MCS to us 2094 * unless they go through a full association sequence. 2095 * ieee80211_node_supports_ht() could misclassify them. 2096 */ 2097 if ((ni->ni_flags & IEEE80211_NODE_HTCAP) == 0) { 2098 nonht++; 2099 if (ni->ni_state == IEEE80211_STA_ASSOC) 2100 nonhtassoc++; 2101 } 2102 } 2103 #endif 2104 ni->ni_scangen = gen; 2105 if (ni->ni_refcnt > 0) 2106 continue; 2107 #ifndef IEEE80211_STA_ONLY 2108 if ((ic->ic_opmode == IEEE80211_M_HOSTAP || 2109 ic->ic_opmode == IEEE80211_M_IBSS) && 2110 ic->ic_state == IEEE80211_S_RUN) { 2111 if (cache_timeout) { 2112 if (ni->ni_state != IEEE80211_STA_COLLECT && 2113 (ni->ni_state == IEEE80211_STA_ASSOC || 2114 ni->ni_inact < IEEE80211_INACT_MAX)) 2115 continue; 2116 } else { 2117 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 2118 ((ni->ni_state == IEEE80211_STA_ASSOC && 2119 ni->ni_inact < IEEE80211_INACT_MAX) || 2120 (ni->ni_state == IEEE80211_STA_AUTH && 2121 ni->ni_inact == 0))) 2122 continue; 2123 2124 if (ic->ic_opmode == IEEE80211_M_IBSS && 2125 ni->ni_state != IEEE80211_STA_COLLECT && 2126 ni->ni_state != IEEE80211_STA_CACHE && 2127 ni->ni_inact < IEEE80211_INACT_MAX) 2128 continue; 2129 } 2130 } 2131 if (ifp->if_flags & IFF_DEBUG) 2132 printf("%s: station %s purged from node cache\n", 2133 ifp->if_xname, ether_sprintf(ni->ni_macaddr)); 2134 #endif 2135 /* 2136 * If we're hostap and the node is authenticated, send 2137 * a deauthentication frame. The node will be freed when 2138 * the driver calls ieee80211_release_node(). 2139 */ 2140 #ifndef IEEE80211_STA_ONLY 2141 nnodes--; 2142 if ((ic->ic_flags & IEEE80211_F_HTON) && cache_timeout) { 2143 if ((ni->ni_flags & IEEE80211_NODE_HTCAP) == 0) { 2144 nonht--; 2145 if (ni->ni_state == IEEE80211_STA_ASSOC) 2146 nonhtassoc--; 2147 } 2148 } 2149 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 2150 ni->ni_state >= IEEE80211_STA_AUTH && 2151 ni->ni_state != IEEE80211_STA_COLLECT) { 2152 IEEE80211_SEND_MGMT(ic, ni, 2153 IEEE80211_FC0_SUBTYPE_DEAUTH, 2154 IEEE80211_REASON_AUTH_EXPIRE); 2155 ieee80211_node_leave(ic, ni); 2156 } else 2157 #endif 2158 ieee80211_free_node(ic, ni); 2159 ic->ic_stats.is_node_timeout++; 2160 } 2161 2162 #ifndef IEEE80211_STA_ONLY 2163 if ((ic->ic_flags & IEEE80211_F_HTON) && cache_timeout) { 2164 uint16_t htop1 = ic->ic_bss->ni_htop1; 2165 2166 /* Update HT protection settings. */ 2167 if (nonht) { 2168 protmode = IEEE80211_PROT_CTSONLY; 2169 if (nonhtassoc) 2170 htprot = IEEE80211_HTPROT_NONHT_MIXED; 2171 else 2172 htprot = IEEE80211_HTPROT_NONMEMBER; 2173 } 2174 if ((htop1 & IEEE80211_HTOP1_PROT_MASK) != htprot) { 2175 htop1 &= ~IEEE80211_HTOP1_PROT_MASK; 2176 htop1 |= htprot; 2177 ic->ic_bss->ni_htop1 = htop1; 2178 ic->ic_protmode = protmode; 2179 if (ic->ic_update_htprot) 2180 ic->ic_update_htprot(ic, ic->ic_bss); 2181 } 2182 } 2183 2184 /* 2185 * During a cache timeout we iterate over all nodes. 2186 * Check for node leaks by comparing the actual number of cached 2187 * nodes with the ic_nnodes count, which is maintained while adding 2188 * and removing nodes from the cache. 2189 */ 2190 if ((ifp->if_flags & IFF_DEBUG) && cache_timeout && 2191 nnodes != ic->ic_nnodes) 2192 printf("%s: number of cached nodes is %d, expected %d," 2193 "possible nodes leak\n", ifp->if_xname, nnodes, 2194 ic->ic_nnodes); 2195 #endif 2196 splx(s); 2197 } 2198 2199 void 2200 ieee80211_clean_inactive_nodes(struct ieee80211com *ic, int inact_max) 2201 { 2202 struct ieee80211_node *ni, *next_ni; 2203 u_int gen = ic->ic_scangen++; /* NB: ok 'cuz single-threaded*/ 2204 int s; 2205 2206 s = splnet(); 2207 for (ni = RBT_MIN(ieee80211_tree, &ic->ic_tree); 2208 ni != NULL; ni = next_ni) { 2209 next_ni = RBT_NEXT(ieee80211_tree, ni); 2210 if (ni->ni_scangen == gen) /* previously handled */ 2211 continue; 2212 ni->ni_scangen = gen; 2213 if (ni->ni_refcnt > 0 || ni->ni_inact < inact_max) 2214 continue; 2215 ieee80211_free_node(ic, ni); 2216 ic->ic_stats.is_node_timeout++; 2217 } 2218 2219 splx(s); 2220 } 2221 2222 void 2223 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, 2224 void *arg) 2225 { 2226 struct ieee80211_node *ni; 2227 int s; 2228 2229 s = splnet(); 2230 RBT_FOREACH(ni, ieee80211_tree, &ic->ic_tree) 2231 (*f)(arg, ni); 2232 splx(s); 2233 } 2234 2235 2236 /* 2237 * Install received HT caps information in the node's state block. 2238 */ 2239 void 2240 ieee80211_setup_htcaps(struct ieee80211_node *ni, const uint8_t *data, 2241 uint8_t len) 2242 { 2243 uint16_t rxrate; 2244 2245 if (len != 26) 2246 return; 2247 2248 ni->ni_htcaps = (data[0] | (data[1] << 8)); 2249 ni->ni_ampdu_param = data[2]; 2250 2251 memcpy(ni->ni_rxmcs, &data[3], sizeof(ni->ni_rxmcs)); 2252 /* clear reserved bits */ 2253 clrbit(ni->ni_rxmcs, 77); 2254 clrbit(ni->ni_rxmcs, 78); 2255 clrbit(ni->ni_rxmcs, 79); 2256 2257 /* Max MCS Rx rate in 1Mb/s units (0 means "not specified"). */ 2258 rxrate = ((data[13] | (data[14]) << 8) & IEEE80211_MCS_RX_RATE_HIGH); 2259 if (rxrate < 1024) 2260 ni->ni_max_rxrate = rxrate; 2261 2262 ni->ni_tx_mcs_set = data[15]; 2263 ni->ni_htxcaps = (data[19] | (data[20] << 8)); 2264 ni->ni_txbfcaps = (data[21] | (data[22] << 8) | (data[23] << 16) | 2265 (data[24] << 24)); 2266 ni->ni_aselcaps = data[25]; 2267 2268 ni->ni_flags |= IEEE80211_NODE_HTCAP; 2269 } 2270 2271 #ifndef IEEE80211_STA_ONLY 2272 /* 2273 * Handle nodes switching from 11n into legacy modes. 2274 */ 2275 void 2276 ieee80211_clear_htcaps(struct ieee80211_node *ni) 2277 { 2278 ni->ni_htcaps = 0; 2279 ni->ni_ampdu_param = 0; 2280 memset(ni->ni_rxmcs, 0, sizeof(ni->ni_rxmcs)); 2281 ni->ni_max_rxrate = 0; 2282 ni->ni_tx_mcs_set = 0; 2283 ni->ni_htxcaps = 0; 2284 ni->ni_txbfcaps = 0; 2285 ni->ni_aselcaps = 0; 2286 2287 ni->ni_flags &= ~(IEEE80211_NODE_HT | IEEE80211_NODE_HT_SGI20 | 2288 IEEE80211_NODE_HT_SGI40 | IEEE80211_NODE_HTCAP); 2289 2290 } 2291 #endif 2292 2293 /* 2294 * Install received HT op information in the node's state block. 2295 */ 2296 int 2297 ieee80211_setup_htop(struct ieee80211_node *ni, const uint8_t *data, 2298 uint8_t len, int isprobe) 2299 { 2300 if (len != 22) 2301 return 0; 2302 2303 ni->ni_primary_chan = data[0]; /* XXX corresponds to ni_chan */ 2304 2305 ni->ni_htop0 = data[1]; 2306 ni->ni_htop1 = (data[2] | (data[3] << 8)); 2307 ni->ni_htop2 = (data[3] | (data[4] << 8)); 2308 2309 /* 2310 * According to 802.11-2012 Table 8-130 the Basic MCS set is 2311 * only "present in Beacon, Probe Response, Mesh Peering Open 2312 * and Mesh Peering Confirm frames. Otherwise reserved." 2313 */ 2314 if (isprobe) 2315 memcpy(ni->ni_basic_mcs, &data[6], sizeof(ni->ni_basic_mcs)); 2316 2317 return 1; 2318 } 2319 2320 /* 2321 * Install received rate set information in the node's state block. 2322 */ 2323 int 2324 ieee80211_setup_rates(struct ieee80211com *ic, struct ieee80211_node *ni, 2325 const u_int8_t *rates, const u_int8_t *xrates, int flags) 2326 { 2327 struct ieee80211_rateset *rs = &ni->ni_rates; 2328 2329 memset(rs, 0, sizeof(*rs)); 2330 rs->rs_nrates = rates[1]; 2331 memcpy(rs->rs_rates, rates + 2, rs->rs_nrates); 2332 if (xrates != NULL) { 2333 u_int8_t nxrates; 2334 /* 2335 * Tack on 11g extended supported rate element. 2336 */ 2337 nxrates = xrates[1]; 2338 if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) { 2339 nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates; 2340 DPRINTF(("extended rate set too large; " 2341 "only using %u of %u rates\n", 2342 nxrates, xrates[1])); 2343 ic->ic_stats.is_rx_rstoobig++; 2344 } 2345 memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates); 2346 rs->rs_nrates += nxrates; 2347 } 2348 return ieee80211_fix_rate(ic, ni, flags); 2349 } 2350 2351 void 2352 ieee80211_node_trigger_addba_req(struct ieee80211_node *ni, int tid) 2353 { 2354 if (ni->ni_tx_ba[tid].ba_state == IEEE80211_BA_INIT && 2355 !timeout_pending(&ni->ni_addba_req_to[tid])) { 2356 timeout_add_sec(&ni->ni_addba_req_to[tid], 2357 ni->ni_addba_req_intval[tid]); 2358 } 2359 } 2360 2361 void 2362 ieee80211_node_addba_request(struct ieee80211_node *ni, int tid) 2363 { 2364 struct ieee80211com *ic = ni->ni_ic; 2365 uint16_t ssn = ni->ni_qos_txseqs[tid]; 2366 2367 ieee80211_addba_request(ic, ni, ssn, tid); 2368 } 2369 2370 void 2371 ieee80211_node_addba_request_ac_be_to(void *arg) 2372 { 2373 struct ieee80211_node *ni = arg; 2374 ieee80211_node_addba_request(ni, EDCA_AC_BE); 2375 } 2376 2377 void 2378 ieee80211_node_addba_request_ac_bk_to(void *arg) 2379 { 2380 struct ieee80211_node *ni = arg; 2381 ieee80211_node_addba_request(ni, EDCA_AC_BK); 2382 } 2383 2384 void 2385 ieee80211_node_addba_request_ac_vi_to(void *arg) 2386 { 2387 struct ieee80211_node *ni = arg; 2388 ieee80211_node_addba_request(ni, EDCA_AC_VI); 2389 } 2390 2391 void 2392 ieee80211_node_addba_request_ac_vo_to(void *arg) 2393 { 2394 struct ieee80211_node *ni = arg; 2395 ieee80211_node_addba_request(ni, EDCA_AC_VO); 2396 } 2397 2398 #ifndef IEEE80211_STA_ONLY 2399 /* 2400 * Check if the specified node supports ERP. 2401 */ 2402 int 2403 ieee80211_iserp_sta(const struct ieee80211_node *ni) 2404 { 2405 static const u_int8_t rates[] = { 2, 4, 11, 22, 12, 24, 48 }; 2406 const struct ieee80211_rateset *rs = &ni->ni_rates; 2407 int i, j; 2408 2409 /* 2410 * A STA supports ERP operation if it includes all the Clause 19 2411 * mandatory rates in its supported rate set. 2412 */ 2413 for (i = 0; i < nitems(rates); i++) { 2414 for (j = 0; j < rs->rs_nrates; j++) { 2415 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == rates[i]) 2416 break; 2417 } 2418 if (j == rs->rs_nrates) 2419 return 0; 2420 } 2421 return 1; 2422 } 2423 2424 /* 2425 * This function is called to notify the 802.1X PACP machine that a new 2426 * 802.1X port is enabled and must be authenticated. For 802.11, a port 2427 * becomes enabled whenever a STA successfully completes Open System 2428 * authentication with an AP. 2429 */ 2430 void 2431 ieee80211_needs_auth(struct ieee80211com *ic, struct ieee80211_node *ni) 2432 { 2433 /* 2434 * XXX this could be done via the route socket of via a dedicated 2435 * EAP socket or another kernel->userland notification mechanism. 2436 * The notification should include the MAC address (ni_macaddr). 2437 */ 2438 } 2439 2440 /* 2441 * Handle an HT STA joining an HT network. 2442 */ 2443 void 2444 ieee80211_node_join_ht(struct ieee80211com *ic, struct ieee80211_node *ni) 2445 { 2446 enum ieee80211_htprot; 2447 2448 /* Update HT protection setting. */ 2449 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) { 2450 uint16_t htop1 = ic->ic_bss->ni_htop1; 2451 htop1 &= ~IEEE80211_HTOP1_PROT_MASK; 2452 htop1 |= IEEE80211_HTPROT_NONHT_MIXED; 2453 ic->ic_bss->ni_htop1 = htop1; 2454 if (ic->ic_update_htprot) 2455 ic->ic_update_htprot(ic, ic->ic_bss); 2456 } 2457 } 2458 2459 /* 2460 * Handle a station joining an RSN network. 2461 */ 2462 void 2463 ieee80211_node_join_rsn(struct ieee80211com *ic, struct ieee80211_node *ni) 2464 { 2465 DPRINTF(("station %s associated using proto %d akm 0x%x " 2466 "cipher 0x%x groupcipher 0x%x\n", ether_sprintf(ni->ni_macaddr), 2467 ni->ni_rsnprotos, ni->ni_rsnakms, ni->ni_rsnciphers, 2468 ni->ni_rsngroupcipher)); 2469 2470 ni->ni_rsn_state = RSNA_AUTHENTICATION; 2471 2472 ni->ni_key_count = 0; 2473 ni->ni_port_valid = 0; 2474 ni->ni_flags &= ~IEEE80211_NODE_TXRXPROT; 2475 ni->ni_flags &= ~IEEE80211_NODE_RSN_NEW_PTK; 2476 ni->ni_replaycnt = -1; /* XXX */ 2477 ni->ni_rsn_retries = 0; 2478 ni->ni_rsncipher = ni->ni_rsnciphers; 2479 2480 ni->ni_rsn_state = RSNA_AUTHENTICATION_2; 2481 2482 /* generate a new authenticator nonce (ANonce) */ 2483 arc4random_buf(ni->ni_nonce, EAPOL_KEY_NONCE_LEN); 2484 2485 if (!ieee80211_is_8021x_akm(ni->ni_rsnakms)) { 2486 memcpy(ni->ni_pmk, ic->ic_psk, IEEE80211_PMK_LEN); 2487 ni->ni_flags |= IEEE80211_NODE_PMK; 2488 (void)ieee80211_send_4way_msg1(ic, ni); 2489 } else if (ni->ni_flags & IEEE80211_NODE_PMK) { 2490 /* skip 802.1X auth if a cached PMK was found */ 2491 (void)ieee80211_send_4way_msg1(ic, ni); 2492 } else { 2493 /* no cached PMK found, needs full 802.1X auth */ 2494 ieee80211_needs_auth(ic, ni); 2495 } 2496 } 2497 2498 void 2499 ieee80211_count_longslotsta(void *arg, struct ieee80211_node *ni) 2500 { 2501 int *longslotsta = arg; 2502 2503 if (ni->ni_associd == 0 || ni->ni_state == IEEE80211_STA_COLLECT) 2504 return; 2505 2506 if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)) 2507 (*longslotsta)++; 2508 } 2509 2510 void 2511 ieee80211_count_nonerpsta(void *arg, struct ieee80211_node *ni) 2512 { 2513 int *nonerpsta = arg; 2514 2515 if (ni->ni_associd == 0 || ni->ni_state == IEEE80211_STA_COLLECT) 2516 return; 2517 2518 if (!ieee80211_iserp_sta(ni)) 2519 (*nonerpsta)++; 2520 } 2521 2522 void 2523 ieee80211_count_pssta(void *arg, struct ieee80211_node *ni) 2524 { 2525 int *pssta = arg; 2526 2527 if (ni->ni_associd == 0 || ni->ni_state == IEEE80211_STA_COLLECT) 2528 return; 2529 2530 if (ni->ni_pwrsave == IEEE80211_PS_DOZE) 2531 (*pssta)++; 2532 } 2533 2534 void 2535 ieee80211_count_rekeysta(void *arg, struct ieee80211_node *ni) 2536 { 2537 int *rekeysta = arg; 2538 2539 if (ni->ni_associd == 0 || ni->ni_state == IEEE80211_STA_COLLECT) 2540 return; 2541 2542 if (ni->ni_flags & IEEE80211_NODE_REKEY) 2543 (*rekeysta)++; 2544 } 2545 2546 /* 2547 * Handle a station joining an 11g network. 2548 */ 2549 void 2550 ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 2551 { 2552 int longslotsta = 0, nonerpsta = 0; 2553 2554 if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)) { 2555 /* 2556 * Joining STA doesn't support short slot time. We must 2557 * disable the use of short slot time for all other associated 2558 * STAs and give the driver a chance to reconfigure the 2559 * hardware. 2560 */ 2561 ieee80211_iterate_nodes(ic, 2562 ieee80211_count_longslotsta, &longslotsta); 2563 if (longslotsta == 1) { 2564 if (ic->ic_caps & IEEE80211_C_SHSLOT) 2565 ieee80211_set_shortslottime(ic, 0); 2566 } 2567 DPRINTF(("[%s] station needs long slot time, count %d\n", 2568 ether_sprintf(ni->ni_macaddr), longslotsta)); 2569 } 2570 2571 if (!ieee80211_iserp_sta(ni)) { 2572 /* 2573 * Joining STA is non-ERP. 2574 */ 2575 ieee80211_iterate_nodes(ic, 2576 ieee80211_count_nonerpsta, &nonerpsta); 2577 DPRINTF(("[%s] station is non-ERP, %d non-ERP " 2578 "stations associated\n", ether_sprintf(ni->ni_macaddr), 2579 nonerpsta)); 2580 /* must enable the use of protection */ 2581 if (ic->ic_protmode != IEEE80211_PROT_NONE) { 2582 DPRINTF(("enable use of protection\n")); 2583 ic->ic_flags |= IEEE80211_F_USEPROT; 2584 } 2585 2586 if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) 2587 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 2588 } else 2589 ni->ni_flags |= IEEE80211_NODE_ERP; 2590 } 2591 2592 void 2593 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, 2594 int resp) 2595 { 2596 int newassoc = (ni->ni_state != IEEE80211_STA_ASSOC); 2597 2598 if (ni->ni_associd == 0) { 2599 u_int16_t aid; 2600 2601 /* 2602 * It would be clever to search the bitmap 2603 * more efficiently, but this will do for now. 2604 */ 2605 for (aid = 1; aid < ic->ic_max_aid; aid++) { 2606 if (!IEEE80211_AID_ISSET(aid, 2607 ic->ic_aid_bitmap)) 2608 break; 2609 } 2610 if (aid >= ic->ic_max_aid) { 2611 IEEE80211_SEND_MGMT(ic, ni, resp, 2612 IEEE80211_REASON_ASSOC_TOOMANY); 2613 ieee80211_node_leave(ic, ni); 2614 return; 2615 } 2616 ni->ni_associd = aid | 0xc000; 2617 IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); 2618 if (ic->ic_curmode == IEEE80211_MODE_11G || 2619 (ic->ic_curmode == IEEE80211_MODE_11N && 2620 IEEE80211_IS_CHAN_2GHZ(ic->ic_bss->ni_chan))) 2621 ieee80211_node_join_11g(ic, ni); 2622 } 2623 2624 DPRINTF(("station %s %s associated at aid %d\n", 2625 ether_sprintf(ni->ni_macaddr), newassoc ? "newly" : "already", 2626 ni->ni_associd & ~0xc000)); 2627 2628 ieee80211_ht_negotiate(ic, ni); 2629 if (ic->ic_flags & IEEE80211_F_HTON) 2630 ieee80211_node_join_ht(ic, ni); 2631 2632 /* give driver a chance to setup state like ni_txrate */ 2633 if (ic->ic_newassoc) 2634 (*ic->ic_newassoc)(ic, ni, newassoc); 2635 IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); 2636 ieee80211_node_newstate(ni, IEEE80211_STA_ASSOC); 2637 2638 if (!(ic->ic_flags & IEEE80211_F_RSNON)) { 2639 ni->ni_port_valid = 1; 2640 ni->ni_rsncipher = IEEE80211_CIPHER_USEGROUP; 2641 } else 2642 ieee80211_node_join_rsn(ic, ni); 2643 2644 #if NBRIDGE > 0 2645 /* 2646 * If the parent interface is a bridge port, learn 2647 * the node's address dynamically on this interface. 2648 */ 2649 if (ic->ic_if.if_bridgeidx != 0) 2650 bridge_update(&ic->ic_if, 2651 (struct ether_addr *)ni->ni_macaddr, 0); 2652 #endif 2653 } 2654 2655 /* 2656 * Handle an HT STA leaving an HT network. 2657 */ 2658 void 2659 ieee80211_node_leave_ht(struct ieee80211com *ic, struct ieee80211_node *ni) 2660 { 2661 struct ieee80211_rx_ba *ba; 2662 u_int8_t tid; 2663 int i; 2664 2665 /* free all Block Ack records */ 2666 ieee80211_ba_del(ni); 2667 for (tid = 0; tid < IEEE80211_NUM_TID; tid++) { 2668 ba = &ni->ni_rx_ba[tid]; 2669 if (ba->ba_buf != NULL) { 2670 for (i = 0; i < IEEE80211_BA_MAX_WINSZ; i++) 2671 m_freem(ba->ba_buf[i].m); 2672 free(ba->ba_buf, M_DEVBUF, 2673 IEEE80211_BA_MAX_WINSZ * sizeof(*ba->ba_buf)); 2674 ba->ba_buf = NULL; 2675 } 2676 } 2677 2678 ieee80211_clear_htcaps(ni); 2679 } 2680 2681 /* 2682 * Handle a station leaving an RSN network. 2683 */ 2684 void 2685 ieee80211_node_leave_rsn(struct ieee80211com *ic, struct ieee80211_node *ni) 2686 { 2687 int rekeysta = 0; 2688 2689 ni->ni_rsn_state = RSNA_DISCONNECTED; 2690 2691 ni->ni_rsn_state = RSNA_INITIALIZE; 2692 if (ni->ni_flags & IEEE80211_NODE_REKEY) { 2693 ni->ni_flags &= ~IEEE80211_NODE_REKEY; 2694 ieee80211_iterate_nodes(ic, 2695 ieee80211_count_rekeysta, &rekeysta); 2696 if (rekeysta == 0) 2697 ieee80211_setkeysdone(ic); 2698 } 2699 ni->ni_flags &= ~IEEE80211_NODE_PMK; 2700 ni->ni_rsn_gstate = RSNA_IDLE; 2701 2702 timeout_del(&ni->ni_eapol_to); 2703 timeout_del(&ni->ni_sa_query_to); 2704 2705 ni->ni_rsn_retries = 0; 2706 ni->ni_flags &= ~IEEE80211_NODE_TXRXPROT; 2707 ni->ni_port_valid = 0; 2708 (*ic->ic_delete_key)(ic, ni, &ni->ni_pairwise_key); 2709 } 2710 2711 /* 2712 * Handle a station leaving an 11g network. 2713 */ 2714 void 2715 ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 2716 { 2717 int longslotsta = 0, nonerpsta = 0; 2718 2719 if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)) { 2720 /* leaving STA did not support short slot time */ 2721 ieee80211_iterate_nodes(ic, 2722 ieee80211_count_longslotsta, &longslotsta); 2723 if (longslotsta == 1) { 2724 /* 2725 * All associated STAs now support short slot time, so 2726 * enable this feature and give the driver a chance to 2727 * reconfigure the hardware. Notice that IBSS always 2728 * use a long slot time. 2729 */ 2730 if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 2731 ic->ic_opmode != IEEE80211_M_IBSS) 2732 ieee80211_set_shortslottime(ic, 1); 2733 } 2734 DPRINTF(("[%s] long slot time station leaves, count %d\n", 2735 ether_sprintf(ni->ni_macaddr), longslotsta)); 2736 } 2737 2738 if (!(ni->ni_flags & IEEE80211_NODE_ERP)) { 2739 /* leaving STA was non-ERP */ 2740 ieee80211_iterate_nodes(ic, 2741 ieee80211_count_nonerpsta, &nonerpsta); 2742 if (nonerpsta == 1) { 2743 /* 2744 * All associated STAs are now ERP capable, disable use 2745 * of protection and re-enable short preamble support. 2746 */ 2747 ic->ic_flags &= ~IEEE80211_F_USEPROT; 2748 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) 2749 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 2750 } 2751 DPRINTF(("[%s] non-ERP station leaves, count %d\n", 2752 ether_sprintf(ni->ni_macaddr), nonerpsta)); 2753 } 2754 } 2755 2756 /* 2757 * Handle bookkeeping for station deauthentication/disassociation 2758 * when operating as an ap. 2759 */ 2760 void 2761 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 2762 { 2763 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2764 panic("not in ap mode, mode %u", ic->ic_opmode); 2765 2766 if (ni->ni_state == IEEE80211_STA_COLLECT) 2767 return; 2768 /* 2769 * If node wasn't previously associated all we need to do is 2770 * reclaim the reference. 2771 */ 2772 if (ni->ni_associd == 0) { 2773 ieee80211_node_newstate(ni, IEEE80211_STA_COLLECT); 2774 return; 2775 } 2776 2777 if (ni->ni_pwrsave == IEEE80211_PS_DOZE) 2778 ni->ni_pwrsave = IEEE80211_PS_AWAKE; 2779 2780 if (mq_purge(&ni->ni_savedq) > 0) { 2781 if (ic->ic_set_tim != NULL) 2782 (*ic->ic_set_tim)(ic, ni->ni_associd, 0); 2783 } 2784 2785 if (ic->ic_flags & IEEE80211_F_RSNON) 2786 ieee80211_node_leave_rsn(ic, ni); 2787 2788 if (ic->ic_curmode == IEEE80211_MODE_11G || 2789 (ic->ic_curmode == IEEE80211_MODE_11N && 2790 IEEE80211_IS_CHAN_2GHZ(ic->ic_bss->ni_chan))) 2791 ieee80211_node_leave_11g(ic, ni); 2792 2793 if (ni->ni_flags & IEEE80211_NODE_HT) 2794 ieee80211_node_leave_ht(ic, ni); 2795 2796 if (ic->ic_node_leave != NULL) 2797 (*ic->ic_node_leave)(ic, ni); 2798 2799 ieee80211_node_newstate(ni, IEEE80211_STA_COLLECT); 2800 2801 #if NBRIDGE > 0 2802 /* 2803 * If the parent interface is a bridge port, delete 2804 * any dynamically learned address for this node. 2805 */ 2806 if (ic->ic_if.if_bridgeidx != 0) 2807 bridge_update(&ic->ic_if, 2808 (struct ether_addr *)ni->ni_macaddr, 1); 2809 #endif 2810 } 2811 2812 static int 2813 ieee80211_do_slow_print(struct ieee80211com *ic, int *did_print) 2814 { 2815 static const struct timeval merge_print_intvl = { 2816 .tv_sec = 1, .tv_usec = 0 2817 }; 2818 if ((ic->ic_if.if_flags & IFF_LINK0) == 0) 2819 return 0; 2820 if (!*did_print && (ic->ic_if.if_flags & IFF_DEBUG) == 0 && 2821 !ratecheck(&ic->ic_last_merge_print, &merge_print_intvl)) 2822 return 0; 2823 2824 *did_print = 1; 2825 return 1; 2826 } 2827 2828 /* ieee80211_ibss_merge helps merge 802.11 ad hoc networks. The 2829 * convention, set by the Wireless Ethernet Compatibility Alliance 2830 * (WECA), is that an 802.11 station will change its BSSID to match 2831 * the "oldest" 802.11 ad hoc network, on the same channel, that 2832 * has the station's desired SSID. The "oldest" 802.11 network 2833 * sends beacons with the greatest TSF timestamp. 2834 * 2835 * Return ENETRESET if the BSSID changed, 0 otherwise. 2836 * 2837 * XXX Perhaps we should compensate for the time that elapses 2838 * between the MAC receiving the beacon and the host processing it 2839 * in ieee80211_ibss_merge. 2840 */ 2841 int 2842 ieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni, 2843 u_int64_t local_tsft) 2844 { 2845 u_int64_t beacon_tsft; 2846 int did_print = 0, sign; 2847 union { 2848 u_int64_t word; 2849 u_int8_t tstamp[8]; 2850 } u; 2851 2852 /* ensure alignment */ 2853 (void)memcpy(&u, &ni->ni_tstamp[0], sizeof(u)); 2854 beacon_tsft = letoh64(u.word); 2855 2856 /* we are faster, let the other guy catch up */ 2857 if (beacon_tsft < local_tsft) 2858 sign = -1; 2859 else 2860 sign = 1; 2861 2862 if (IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) { 2863 if (!ieee80211_do_slow_print(ic, &did_print)) 2864 return 0; 2865 printf("%s: tsft offset %s%llu\n", ic->ic_if.if_xname, 2866 (sign < 0) ? "-" : "", 2867 (sign < 0) 2868 ? (local_tsft - beacon_tsft) 2869 : (beacon_tsft - local_tsft)); 2870 return 0; 2871 } 2872 2873 if (sign < 0) 2874 return 0; 2875 2876 if (ieee80211_match_bss(ic, ni, 0) != 0) 2877 return 0; 2878 2879 if (ieee80211_do_slow_print(ic, &did_print)) { 2880 printf("%s: ieee80211_ibss_merge: bssid mismatch %s\n", 2881 ic->ic_if.if_xname, ether_sprintf(ni->ni_bssid)); 2882 printf("%s: my tsft %llu beacon tsft %llu\n", 2883 ic->ic_if.if_xname, local_tsft, beacon_tsft); 2884 printf("%s: sync TSF with %s\n", 2885 ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr)); 2886 } 2887 2888 ic->ic_flags &= ~IEEE80211_F_SIBSS; 2889 2890 /* negotiate rates with new IBSS */ 2891 ieee80211_fix_rate(ic, ni, IEEE80211_F_DOFRATE | 2892 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 2893 if (ni->ni_rates.rs_nrates == 0) { 2894 if (ieee80211_do_slow_print(ic, &did_print)) { 2895 printf("%s: rates mismatch, BSSID %s\n", 2896 ic->ic_if.if_xname, ether_sprintf(ni->ni_bssid)); 2897 } 2898 return 0; 2899 } 2900 2901 if (ieee80211_do_slow_print(ic, &did_print)) { 2902 printf("%s: sync BSSID %s -> ", 2903 ic->ic_if.if_xname, ether_sprintf(ic->ic_bss->ni_bssid)); 2904 printf("%s ", ether_sprintf(ni->ni_bssid)); 2905 printf("(from %s)\n", ether_sprintf(ni->ni_macaddr)); 2906 } 2907 2908 ieee80211_node_newstate(ni, IEEE80211_STA_BSS); 2909 (*ic->ic_node_copy)(ic, ic->ic_bss, ni); 2910 2911 return ENETRESET; 2912 } 2913 2914 void 2915 ieee80211_set_tim(struct ieee80211com *ic, int aid, int set) 2916 { 2917 if (set) 2918 setbit(ic->ic_tim_bitmap, aid & ~0xc000); 2919 else 2920 clrbit(ic->ic_tim_bitmap, aid & ~0xc000); 2921 } 2922 2923 /* 2924 * This function shall be called by drivers immediately after every DTIM. 2925 * Transmit all group addressed MSDUs buffered at the AP. 2926 */ 2927 void 2928 ieee80211_notify_dtim(struct ieee80211com *ic) 2929 { 2930 /* NB: group addressed MSDUs are buffered in ic_bss */ 2931 struct ieee80211_node *ni = ic->ic_bss; 2932 struct ifnet *ifp = &ic->ic_if; 2933 struct ieee80211_frame *wh; 2934 struct mbuf *m; 2935 2936 KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP); 2937 2938 while ((m = mq_dequeue(&ni->ni_savedq)) != NULL) { 2939 if (!mq_empty(&ni->ni_savedq)) { 2940 /* more queued frames, set the more data bit */ 2941 wh = mtod(m, struct ieee80211_frame *); 2942 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 2943 } 2944 mq_enqueue(&ic->ic_pwrsaveq, m); 2945 if_start(ifp); 2946 } 2947 /* XXX assumes everything has been sent */ 2948 ic->ic_tim_mcast_pending = 0; 2949 } 2950 #endif /* IEEE80211_STA_ONLY */ 2951 2952 /* 2953 * Compare nodes in the tree by lladdr 2954 */ 2955 int 2956 ieee80211_node_cmp(const struct ieee80211_node *b1, 2957 const struct ieee80211_node *b2) 2958 { 2959 return (memcmp(b1->ni_macaddr, b2->ni_macaddr, IEEE80211_ADDR_LEN)); 2960 } 2961 2962 /* 2963 * Compare nodes in the tree by essid 2964 */ 2965 int 2966 ieee80211_ess_cmp(const struct ieee80211_ess_rbt *b1, 2967 const struct ieee80211_ess_rbt *b2) 2968 { 2969 return (memcmp(b1->essid, b2->essid, IEEE80211_NWID_LEN)); 2970 } 2971 2972 /* 2973 * Generate red-black tree function logic 2974 */ 2975 RBT_GENERATE(ieee80211_tree, ieee80211_node, ni_node, ieee80211_node_cmp); 2976 RBT_GENERATE(ieee80211_ess_tree, ieee80211_ess_rbt, ess_rbt, ieee80211_ess_cmp); 2977