1 /* $OpenBSD: ieee80211_node.c,v 1.46 2008/09/27 15:16:09 damien 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 "bpfilter.h" 34 #include "bridge.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/mbuf.h> 39 #include <sys/malloc.h> 40 #include <sys/kernel.h> 41 #include <sys/socket.h> 42 #include <sys/sockio.h> 43 #include <sys/endian.h> 44 #include <sys/errno.h> 45 #include <sys/proc.h> 46 #include <sys/sysctl.h> 47 #include <sys/tree.h> 48 49 #include <net/if.h> 50 #include <net/if_dl.h> 51 #include <net/if_media.h> 52 #include <net/if_arp.h> 53 #include <net/if_llc.h> 54 55 #if NBPFILTER > 0 56 #include <net/bpf.h> 57 #endif 58 59 #ifdef INET 60 #include <netinet/in.h> 61 #include <netinet/if_ether.h> 62 #endif 63 64 #if NBRIDGE > 0 65 #include <net/if_bridge.h> 66 #endif 67 68 #include <net80211/ieee80211_var.h> 69 #include <net80211/ieee80211_priv.h> 70 71 #include <dev/rndvar.h> 72 73 struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *); 74 void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *); 75 void ieee80211_node_copy(struct ieee80211com *, struct ieee80211_node *, 76 const struct ieee80211_node *); 77 void ieee80211_choose_rsnparams(struct ieee80211com *); 78 u_int8_t ieee80211_node_getrssi(struct ieee80211com *, 79 const struct ieee80211_node *); 80 void ieee80211_setup_node(struct ieee80211com *, struct ieee80211_node *, 81 const u_int8_t *); 82 void ieee80211_free_node(struct ieee80211com *, struct ieee80211_node *); 83 struct ieee80211_node *ieee80211_alloc_node_helper(struct ieee80211com *); 84 void ieee80211_node_cleanup(struct ieee80211com *, struct ieee80211_node *); 85 void ieee80211_needs_auth(struct ieee80211com *, struct ieee80211_node *); 86 #ifndef IEEE80211_STA_ONLY 87 void ieee80211_node_join_rsn(struct ieee80211com *, struct ieee80211_node *); 88 void ieee80211_node_join_11g(struct ieee80211com *, struct ieee80211_node *); 89 void ieee80211_node_leave_rsn(struct ieee80211com *, struct ieee80211_node *); 90 void ieee80211_node_leave_11g(struct ieee80211com *, struct ieee80211_node *); 91 void ieee80211_set_tim(struct ieee80211com *, int, int); 92 #endif 93 94 #define M_80211_NODE M_DEVBUF 95 96 void 97 ieee80211_node_attach(struct ifnet *ifp) 98 { 99 struct ieee80211com *ic = (void *)ifp; 100 #ifndef IEEE80211_STA_ONLY 101 int size; 102 #endif 103 104 RB_INIT(&ic->ic_tree); 105 ic->ic_node_alloc = ieee80211_node_alloc; 106 ic->ic_node_free = ieee80211_node_free; 107 ic->ic_node_copy = ieee80211_node_copy; 108 ic->ic_node_getrssi = ieee80211_node_getrssi; 109 ic->ic_scangen = 1; 110 ic->ic_max_nnodes = ieee80211_cache_size; 111 112 if (ic->ic_max_aid == 0) 113 ic->ic_max_aid = IEEE80211_AID_DEF; 114 else if (ic->ic_max_aid > IEEE80211_AID_MAX) 115 ic->ic_max_aid = IEEE80211_AID_MAX; 116 #ifndef IEEE80211_STA_ONLY 117 size = howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t); 118 ic->ic_aid_bitmap = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO); 119 if (ic->ic_aid_bitmap == NULL) { 120 /* XXX no way to recover */ 121 printf("%s: no memory for AID bitmap!\n", __func__); 122 ic->ic_max_aid = 0; 123 } 124 if (ic->ic_caps & (IEEE80211_C_HOSTAP | IEEE80211_C_IBSS)) { 125 ic->ic_tim_len = howmany(ic->ic_max_aid, 8); 126 ic->ic_tim_bitmap = malloc(ic->ic_tim_len, M_DEVBUF, 127 M_NOWAIT | M_ZERO); 128 if (ic->ic_tim_bitmap == NULL) { 129 printf("%s: no memory for TIM bitmap!\n", __func__); 130 ic->ic_tim_len = 0; 131 } else 132 ic->ic_set_tim = ieee80211_set_tim; 133 timeout_set(&ic->ic_rsn_timeout, 134 ieee80211_gtk_rekey_timeout, ic); 135 } 136 #endif 137 } 138 139 struct ieee80211_node * 140 ieee80211_alloc_node_helper(struct ieee80211com *ic) 141 { 142 struct ieee80211_node *ni; 143 if (ic->ic_nnodes >= ic->ic_max_nnodes) 144 ieee80211_clean_nodes(ic); 145 if (ic->ic_nnodes >= ic->ic_max_nnodes) 146 return NULL; 147 ni = (*ic->ic_node_alloc)(ic); 148 if (ni != NULL) 149 ic->ic_nnodes++; 150 return ni; 151 } 152 153 void 154 ieee80211_node_lateattach(struct ifnet *ifp) 155 { 156 struct ieee80211com *ic = (void *)ifp; 157 struct ieee80211_node *ni; 158 159 ni = ieee80211_alloc_node_helper(ic); 160 if (ni == NULL) 161 panic("unable to setup inital BSS node"); 162 ni->ni_chan = IEEE80211_CHAN_ANYC; 163 ic->ic_bss = ieee80211_ref_node(ni); 164 ic->ic_txpower = IEEE80211_TXPOWER_MAX; 165 } 166 167 void 168 ieee80211_node_detach(struct ifnet *ifp) 169 { 170 struct ieee80211com *ic = (void *)ifp; 171 172 if (ic->ic_bss != NULL) { 173 (*ic->ic_node_free)(ic, ic->ic_bss); 174 ic->ic_bss = NULL; 175 } 176 ieee80211_free_allnodes(ic); 177 if (ic->ic_aid_bitmap != NULL) 178 free(ic->ic_aid_bitmap, M_DEVBUF); 179 if (ic->ic_tim_bitmap != NULL) 180 free(ic->ic_tim_bitmap, M_DEVBUF); 181 } 182 183 /* 184 * AP scanning support. 185 */ 186 187 /* 188 * Initialize the active channel set based on the set 189 * of available channels and the current PHY mode. 190 */ 191 void 192 ieee80211_reset_scan(struct ifnet *ifp) 193 { 194 struct ieee80211com *ic = (void *)ifp; 195 196 memcpy(ic->ic_chan_scan, ic->ic_chan_active, 197 sizeof(ic->ic_chan_active)); 198 /* NB: hack, setup so next_scan starts with the first channel */ 199 if (ic->ic_bss != NULL && ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC) 200 ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX]; 201 } 202 203 /* 204 * Begin an active scan. 205 */ 206 void 207 ieee80211_begin_scan(struct ifnet *ifp) 208 { 209 struct ieee80211com *ic = (void *)ifp; 210 211 if (ic->ic_scan_lock & IEEE80211_SCAN_LOCKED) 212 return; 213 ic->ic_scan_lock |= IEEE80211_SCAN_LOCKED; 214 215 /* 216 * In all but hostap mode scanning starts off in 217 * an active mode before switching to passive. 218 */ 219 #ifndef IEEE80211_STA_ONLY 220 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 221 #endif 222 { 223 ic->ic_flags |= IEEE80211_F_ASCAN; 224 ic->ic_stats.is_scan_active++; 225 } 226 #ifndef IEEE80211_STA_ONLY 227 else 228 ic->ic_stats.is_scan_passive++; 229 #endif 230 if (ifp->if_flags & IFF_DEBUG) 231 printf("%s: begin %s scan\n", ifp->if_xname, 232 (ic->ic_flags & IEEE80211_F_ASCAN) ? 233 "active" : "passive"); 234 235 /* 236 * Flush any previously seen AP's. Note that the latter 237 * assumes we don't act as both an AP and a station, 238 * otherwise we'll potentially flush state of stations 239 * associated with us. 240 */ 241 ieee80211_free_allnodes(ic); 242 243 /* 244 * Reset the current mode. Setting the current mode will also 245 * reset scan state. 246 */ 247 if (IFM_MODE(ic->ic_media.ifm_cur->ifm_media) == IFM_AUTO) 248 ic->ic_curmode = IEEE80211_MODE_AUTO; 249 ieee80211_setmode(ic, ic->ic_curmode); 250 251 ic->ic_scan_count = 0; 252 253 /* Scan the next channel. */ 254 ieee80211_next_scan(ifp); 255 } 256 257 /* 258 * Switch to the next channel marked for scanning. 259 */ 260 void 261 ieee80211_next_scan(struct ifnet *ifp) 262 { 263 struct ieee80211com *ic = (void *)ifp; 264 struct ieee80211_channel *chan; 265 266 chan = ic->ic_bss->ni_chan; 267 for (;;) { 268 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX]) 269 chan = &ic->ic_channels[0]; 270 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) { 271 /* 272 * Ignore channels marked passive-only 273 * during an active scan. 274 */ 275 if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 || 276 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) 277 break; 278 } 279 if (chan == ic->ic_bss->ni_chan) { 280 ieee80211_end_scan(ifp); 281 return; 282 } 283 } 284 clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan)); 285 DPRINTF(("chan %d->%d\n", 286 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan), 287 ieee80211_chan2ieee(ic, chan))); 288 ic->ic_bss->ni_chan = chan; 289 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 290 } 291 292 #ifndef IEEE80211_STA_ONLY 293 void 294 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan) 295 { 296 struct ieee80211_node *ni; 297 struct ifnet *ifp = &ic->ic_if; 298 299 ni = ic->ic_bss; 300 if (ifp->if_flags & IFF_DEBUG) 301 printf("%s: creating ibss\n", ifp->if_xname); 302 ic->ic_flags |= IEEE80211_F_SIBSS; 303 ni->ni_chan = chan; 304 ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)]; 305 IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr); 306 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr); 307 if (ic->ic_opmode == IEEE80211_M_IBSS) { 308 if ((ic->ic_flags & IEEE80211_F_DESBSSID) != 0) 309 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid); 310 else 311 ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */ 312 } 313 ni->ni_esslen = ic->ic_des_esslen; 314 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 315 ni->ni_rssi = 0; 316 ni->ni_rstamp = 0; 317 memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp)); 318 ni->ni_intval = ic->ic_lintval; 319 ni->ni_capinfo = IEEE80211_CAPINFO_IBSS; 320 if (ic->ic_flags & IEEE80211_F_WEPON) 321 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 322 if (ic->ic_flags & IEEE80211_F_RSNON) { 323 struct ieee80211_key *k; 324 325 /* initialize 256-bit global key counter to a random value */ 326 arc4random_buf(ic->ic_globalcnt, EAPOL_KEY_NONCE_LEN); 327 328 ni->ni_rsnprotos = ic->ic_rsnprotos; 329 ni->ni_rsnakms = ic->ic_rsnakms; 330 ni->ni_rsnciphers = ic->ic_rsnciphers; 331 ni->ni_rsngroupcipher = ic->ic_rsngroupcipher; 332 ni->ni_rsngroupmgmtcipher = ic->ic_rsngroupmgmtcipher; 333 ni->ni_rsncaps = 0; 334 if (ic->ic_caps & IEEE80211_C_MFP) { 335 ni->ni_rsncaps |= IEEE80211_RSNCAP_MFPC; 336 if (ic->ic_flags & IEEE80211_F_MFPR) 337 ni->ni_rsncaps |= IEEE80211_RSNCAP_MFPR; 338 } 339 340 ic->ic_def_txkey = 1; 341 k = &ic->ic_nw_keys[ic->ic_def_txkey]; 342 memset(k, 0, sizeof(*k)); 343 k->k_id = ic->ic_def_txkey; 344 k->k_cipher = ni->ni_rsngroupcipher; 345 k->k_flags = IEEE80211_KEY_GROUP | IEEE80211_KEY_TX; 346 k->k_len = ieee80211_cipher_keylen(k->k_cipher); 347 arc4random_buf(k->k_key, k->k_len); 348 (*ic->ic_set_key)(ic, ni, k); /* XXX */ 349 350 if (ic->ic_caps & IEEE80211_C_MFP) { 351 ic->ic_igtk_kid = 4; 352 k = &ic->ic_nw_keys[ic->ic_igtk_kid]; 353 memset(k, 0, sizeof(*k)); 354 k->k_id = ic->ic_igtk_kid; 355 k->k_cipher = ni->ni_rsngroupmgmtcipher; 356 k->k_flags = IEEE80211_KEY_IGTK | IEEE80211_KEY_TX; 357 k->k_len = 16; 358 arc4random_buf(k->k_key, k->k_len); 359 (*ic->ic_set_key)(ic, ni, k); /* XXX */ 360 } 361 /* 362 * In HostAP mode, multicast traffic is sent using ic_bss 363 * as the Tx node, so mark our node as valid so we can send 364 * multicast frames using the group key we've just configured. 365 */ 366 ni->ni_port_valid = 1; 367 ni->ni_flags |= IEEE80211_NODE_TXPROT; 368 369 /* schedule a GTK/IGTK rekeying after 3600s */ 370 timeout_add(&ic->ic_rsn_timeout, 3600 * hz); 371 } 372 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 373 } 374 #endif /* IEEE80211_STA_ONLY */ 375 376 int 377 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni) 378 { 379 u_int8_t rate; 380 int fail; 381 382 fail = 0; 383 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 384 fail |= 0x01; 385 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 386 ni->ni_chan != ic->ic_des_chan) 387 fail |= 0x01; 388 #ifndef IEEE80211_STA_ONLY 389 if (ic->ic_opmode == IEEE80211_M_IBSS) { 390 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 391 fail |= 0x02; 392 } else 393 #endif 394 { 395 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 396 fail |= 0x02; 397 } 398 if (ic->ic_flags & (IEEE80211_F_WEPON | IEEE80211_F_RSNON)) { 399 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 400 fail |= 0x04; 401 } else { 402 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 403 fail |= 0x04; 404 } 405 406 rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO); 407 if (rate & IEEE80211_RATE_BASIC) 408 fail |= 0x08; 409 if (ic->ic_des_esslen != 0 && 410 (ni->ni_esslen != ic->ic_des_esslen || 411 memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) 412 fail |= 0x10; 413 if ((ic->ic_flags & IEEE80211_F_DESBSSID) && 414 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid)) 415 fail |= 0x20; 416 417 if (ic->ic_flags & IEEE80211_F_RSNON) { 418 /* 419 * If at least one RSN IE field from the AP's RSN IE fails 420 * to overlap with any value the STA supports, the STA shall 421 * decline to associate with that AP. 422 */ 423 if ((ni->ni_rsnprotos & ic->ic_rsnprotos) == 0) 424 fail |= 0x40; 425 if ((ni->ni_rsnakms & ic->ic_rsnakms) == 0) 426 fail |= 0x40; 427 if ((ni->ni_rsnakms & ic->ic_rsnakms & 428 ~(IEEE80211_AKM_PSK | IEEE80211_AKM_SHA256_PSK)) == 0) { 429 /* AP only supports PSK AKMPs */ 430 if (!(ic->ic_flags & IEEE80211_F_PSK)) 431 fail |= 0x40; 432 } 433 if ((ni->ni_rsnciphers & ic->ic_rsnciphers) == 0) 434 fail |= 0x40; 435 436 /* we only support AES-128-CMAC as the IGTK cipher */ 437 if ((ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC) && 438 ni->ni_rsngroupmgmtcipher != IEEE80211_CIPHER_AES128_CMAC) 439 fail |= 0x40; 440 441 /* we do not support MFP but AP requires it */ 442 if (!(ic->ic_caps & IEEE80211_C_MFP) && 443 (ni->ni_rsncaps & IEEE80211_RSNCAP_MFPR)) 444 fail |= 0x40; 445 446 /* we require MFP but AP does not support it */ 447 if ((ic->ic_caps & IEEE80211_C_MFP) && 448 (ic->ic_flags & IEEE80211_F_MFPR) && 449 !(ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC)) 450 fail |= 0x40; 451 } 452 453 #ifdef IEEE80211_DEBUG 454 if (ic->ic_if.if_flags & IFF_DEBUG) { 455 printf(" %c %s", fail ? '-' : '+', 456 ether_sprintf(ni->ni_macaddr)); 457 printf(" %s%c", ether_sprintf(ni->ni_bssid), 458 fail & 0x20 ? '!' : ' '); 459 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan), 460 fail & 0x01 ? '!' : ' '); 461 printf(" %+4d", ni->ni_rssi); 462 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 463 fail & 0x08 ? '!' : ' '); 464 printf(" %4s%c", 465 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 466 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 467 "????", 468 fail & 0x02 ? '!' : ' '); 469 printf(" %7s%c ", 470 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 471 "privacy" : "no", 472 fail & 0x04 ? '!' : ' '); 473 printf(" %3s%c ", 474 (ic->ic_flags & IEEE80211_F_RSNON) ? 475 "rsn" : "no", 476 fail & 0x40 ? '!' : ' '); 477 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 478 printf("%s\n", fail & 0x10 ? "!" : ""); 479 } 480 #endif 481 return fail; 482 } 483 484 /* 485 * Complete a scan of potential channels. 486 */ 487 void 488 ieee80211_end_scan(struct ifnet *ifp) 489 { 490 struct ieee80211com *ic = (void *)ifp; 491 struct ieee80211_node *ni, *nextbs, *selbs; 492 493 if (ifp->if_flags & IFF_DEBUG) 494 printf("%s: end %s scan\n", ifp->if_xname, 495 (ic->ic_flags & IEEE80211_F_ASCAN) ? 496 "active" : "passive"); 497 498 if (ic->ic_scan_count) 499 ic->ic_flags &= ~IEEE80211_F_ASCAN; 500 501 ni = RB_MIN(ieee80211_tree, &ic->ic_tree); 502 503 #ifndef IEEE80211_STA_ONLY 504 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 505 /* XXX off stack? */ 506 u_char occupied[howmany(IEEE80211_CHAN_MAX, NBBY)]; 507 int i, fail; 508 509 /* 510 * The passive scan to look for existing AP's completed, 511 * select a channel to camp on. Identify the channels 512 * that already have one or more AP's and try to locate 513 * an unnoccupied one. If that fails, pick a random 514 * channel from the active set. 515 */ 516 RB_FOREACH(ni, ieee80211_tree, &ic->ic_tree) 517 setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan)); 518 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 519 if (isset(ic->ic_chan_active, i) && isclr(occupied, i)) 520 break; 521 if (i == IEEE80211_CHAN_MAX) { 522 fail = arc4random() & 3; /* random 0-3 */ 523 for (i = 0; i < IEEE80211_CHAN_MAX; i++) 524 if (isset(ic->ic_chan_active, i) && fail-- == 0) 525 break; 526 } 527 ieee80211_create_ibss(ic, &ic->ic_channels[i]); 528 goto wakeup; 529 } 530 #endif 531 if (ni == NULL) { 532 DPRINTF(("no scan candidate\n")); 533 notfound: 534 535 #ifndef IEEE80211_STA_ONLY 536 if (ic->ic_opmode == IEEE80211_M_IBSS && 537 (ic->ic_flags & IEEE80211_F_IBSSON) && 538 ic->ic_des_esslen != 0) { 539 ieee80211_create_ibss(ic, ic->ic_ibss_chan); 540 goto wakeup; 541 } 542 #endif 543 /* 544 * Scan the next mode if nothing has been found. This 545 * is necessary if the device supports different 546 * incompatible modes in the same channel range, like 547 * like 11b and "pure" 11G mode. This will loop 548 * forever except for user-initiated scans. 549 */ 550 if (ieee80211_next_mode(ifp) == IEEE80211_MODE_AUTO) { 551 if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST && 552 ic->ic_scan_lock & IEEE80211_SCAN_RESUME) { 553 ic->ic_scan_lock = IEEE80211_SCAN_LOCKED; 554 /* Return from an user-initiated scan */ 555 wakeup(&ic->ic_scan_lock); 556 } else if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST) 557 goto wakeup; 558 ic->ic_scan_count++; 559 } 560 561 /* 562 * Reset the list of channels to scan and start again. 563 */ 564 ieee80211_next_scan(ifp); 565 return; 566 } 567 selbs = NULL; 568 569 for (; ni != NULL; ni = nextbs) { 570 nextbs = RB_NEXT(ieee80211_tree, &ic->ic_tree, ni); 571 if (ni->ni_fails) { 572 /* 573 * The configuration of the access points may change 574 * during my scan. So delete the entry for the AP 575 * and retry to associate if there is another beacon. 576 */ 577 if (ni->ni_fails++ > 2) 578 ieee80211_free_node(ic, ni); 579 continue; 580 } 581 if (ieee80211_match_bss(ic, ni) == 0) { 582 if (selbs == NULL) 583 selbs = ni; 584 else if (ni->ni_rssi > selbs->ni_rssi) 585 selbs = ni; 586 } 587 } 588 if (selbs == NULL) 589 goto notfound; 590 (*ic->ic_node_copy)(ic, ic->ic_bss, selbs); 591 ni = ic->ic_bss; 592 593 /* 594 * Set the erp state (mostly the slot time) to deal with 595 * the auto-select case; this should be redundant if the 596 * mode is locked. 597 */ 598 ic->ic_curmode = ieee80211_chan2mode(ic, ni->ni_chan); 599 ieee80211_reset_erp(ic); 600 601 if (ic->ic_flags & IEEE80211_F_RSNON) 602 ieee80211_choose_rsnparams(ic); 603 else if (ic->ic_flags & IEEE80211_F_WEPON) 604 ni->ni_rsncipher = IEEE80211_CIPHER_USEGROUP; 605 606 ieee80211_node_newstate(selbs, IEEE80211_STA_BSS); 607 #ifndef IEEE80211_STA_ONLY 608 if (ic->ic_opmode == IEEE80211_M_IBSS) { 609 ieee80211_fix_rate(ic, ni, IEEE80211_F_DOFRATE | 610 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 611 if (ni->ni_rates.rs_nrates == 0) 612 goto notfound; 613 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 614 } else 615 #endif 616 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 617 618 wakeup: 619 if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST) { 620 /* Return from an user-initiated scan */ 621 wakeup(&ic->ic_scan_lock); 622 } 623 624 ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED; 625 } 626 627 /* 628 * Autoselect the best RSN parameters (protocol, AKMP, pairwise cipher...) 629 * that are supported by both peers (STA mode only). 630 */ 631 void 632 ieee80211_choose_rsnparams(struct ieee80211com *ic) 633 { 634 struct ieee80211_node *ni = ic->ic_bss; 635 struct ieee80211_pmk *pmk; 636 637 /* filter out unsupported protocol versions */ 638 ni->ni_rsnprotos &= ic->ic_rsnprotos; 639 /* prefer RSN (aka WPA2) over WPA */ 640 if (ni->ni_rsnprotos & IEEE80211_PROTO_RSN) 641 ni->ni_rsnprotos = IEEE80211_PROTO_RSN; 642 else 643 ni->ni_rsnprotos = IEEE80211_PROTO_WPA; 644 645 /* filter out unsupported AKMPs */ 646 ni->ni_rsnakms &= ic->ic_rsnakms; 647 /* prefer SHA-256 based AKMPs */ 648 if ((ic->ic_flags & IEEE80211_F_PSK) && (ni->ni_rsnakms & 649 (IEEE80211_AKM_PSK | IEEE80211_AKM_SHA256_PSK))) { 650 /* AP supports PSK AKMP and a PSK is configured */ 651 if (ni->ni_rsnakms & IEEE80211_AKM_SHA256_PSK) 652 ni->ni_rsnakms = IEEE80211_AKM_SHA256_PSK; 653 else 654 ni->ni_rsnakms = IEEE80211_AKM_PSK; 655 } else { 656 if (ni->ni_rsnakms & IEEE80211_AKM_SHA256_8021X) 657 ni->ni_rsnakms = IEEE80211_AKM_SHA256_8021X; 658 else 659 ni->ni_rsnakms = IEEE80211_AKM_8021X; 660 /* check if we have a cached PMK for this AP */ 661 if (ni->ni_rsnprotos == IEEE80211_PROTO_RSN && 662 (pmk = ieee80211_pmksa_find(ic, ni, NULL)) != NULL) { 663 memcpy(ni->ni_pmkid, pmk->pmk_pmkid, 664 IEEE80211_PMKID_LEN); 665 ni->ni_flags |= IEEE80211_NODE_PMKID; 666 } 667 } 668 669 /* filter out unsupported pairwise ciphers */ 670 ni->ni_rsnciphers &= ic->ic_rsnciphers; 671 /* prefer CCMP over TKIP */ 672 if (ni->ni_rsnciphers & IEEE80211_CIPHER_CCMP) 673 ni->ni_rsnciphers = IEEE80211_CIPHER_CCMP; 674 else 675 ni->ni_rsnciphers = IEEE80211_CIPHER_TKIP; 676 ni->ni_rsncipher = ni->ni_rsnciphers; 677 678 /* use MFP if we both support it */ 679 if ((ic->ic_caps & IEEE80211_C_MFP) && 680 (ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC)) 681 ni->ni_flags |= IEEE80211_NODE_MFP; 682 } 683 684 int 685 ieee80211_get_rate(struct ieee80211com *ic) 686 { 687 u_int8_t (*rates)[IEEE80211_RATE_MAXSIZE]; 688 int rate; 689 690 rates = &ic->ic_bss->ni_rates.rs_rates; 691 692 if (ic->ic_fixed_rate != -1) 693 rate = (*rates)[ic->ic_fixed_rate]; 694 else if (ic->ic_state == IEEE80211_S_RUN) 695 rate = (*rates)[ic->ic_bss->ni_txrate]; 696 else 697 rate = 0; 698 699 return rate & IEEE80211_RATE_VAL; 700 } 701 702 struct ieee80211_node * 703 ieee80211_node_alloc(struct ieee80211com *ic) 704 { 705 return malloc(sizeof(struct ieee80211_node), M_80211_NODE, 706 M_NOWAIT | M_ZERO); 707 } 708 709 void 710 ieee80211_node_cleanup(struct ieee80211com *ic, struct ieee80211_node *ni) 711 { 712 if (ni->ni_rsnie != NULL) { 713 free(ni->ni_rsnie, M_DEVBUF); 714 ni->ni_rsnie = NULL; 715 } 716 } 717 718 void 719 ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni) 720 { 721 ieee80211_node_cleanup(ic, ni); 722 free(ni, M_80211_NODE); 723 } 724 725 void 726 ieee80211_node_copy(struct ieee80211com *ic, 727 struct ieee80211_node *dst, const struct ieee80211_node *src) 728 { 729 ieee80211_node_cleanup(ic, dst); 730 *dst = *src; 731 dst->ni_rsnie = NULL; 732 if (src->ni_rsnie != NULL) 733 ieee80211_save_ie(src->ni_rsnie, &dst->ni_rsnie); 734 } 735 736 u_int8_t 737 ieee80211_node_getrssi(struct ieee80211com *ic, 738 const struct ieee80211_node *ni) 739 { 740 return ni->ni_rssi; 741 } 742 743 void 744 ieee80211_setup_node(struct ieee80211com *ic, 745 struct ieee80211_node *ni, const u_int8_t *macaddr) 746 { 747 int s; 748 749 DPRINTF(("%s\n", ether_sprintf((u_int8_t *)macaddr))); 750 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 751 ieee80211_node_newstate(ni, IEEE80211_STA_CACHE); 752 753 ni->ni_ic = ic; /* back-pointer */ 754 755 /* 756 * Note we don't enable the inactive timer when acting 757 * as a station. Nodes created in this mode represent 758 * AP's identified while scanning. If we time them out 759 * then several things happen: we can't return the data 760 * to users to show the list of AP's we encountered, and 761 * more importantly, we'll incorrectly deauthenticate 762 * ourself because the inactivity timer will kick us off. 763 */ 764 s = splnet(); 765 if (ic->ic_opmode != IEEE80211_M_STA && 766 RB_EMPTY(&ic->ic_tree)) 767 ic->ic_inact_timer = IEEE80211_INACT_WAIT; 768 RB_INSERT(ieee80211_tree, &ic->ic_tree, ni); 769 splx(s); 770 } 771 772 struct ieee80211_node * 773 ieee80211_alloc_node(struct ieee80211com *ic, const u_int8_t *macaddr) 774 { 775 struct ieee80211_node *ni = ieee80211_alloc_node_helper(ic); 776 if (ni != NULL) 777 ieee80211_setup_node(ic, ni, macaddr); 778 else 779 ic->ic_stats.is_rx_nodealloc++; 780 return ni; 781 } 782 783 struct ieee80211_node * 784 ieee80211_dup_bss(struct ieee80211com *ic, const u_int8_t *macaddr) 785 { 786 struct ieee80211_node *ni = ieee80211_alloc_node_helper(ic); 787 if (ni != NULL) { 788 ieee80211_setup_node(ic, ni, macaddr); 789 /* 790 * Inherit from ic_bss. 791 */ 792 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid); 793 ni->ni_chan = ic->ic_bss->ni_chan; 794 } else 795 ic->ic_stats.is_rx_nodealloc++; 796 return ni; 797 } 798 799 struct ieee80211_node * 800 ieee80211_find_node(struct ieee80211com *ic, const u_int8_t *macaddr) 801 { 802 struct ieee80211_node ni; 803 804 IEEE80211_ADDR_COPY(ni.ni_macaddr, macaddr); 805 return (RB_FIND(ieee80211_tree, &ic->ic_tree, &ni)); 806 } 807 808 /* 809 * Return a reference to the appropriate node for sending 810 * a data frame. This handles node discovery in adhoc networks. 811 * 812 * Drivers will call this, so increase the reference count before 813 * returning the node. 814 */ 815 struct ieee80211_node * 816 ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr) 817 { 818 #ifndef IEEE80211_STA_ONLY 819 struct ieee80211_node *ni; 820 int s; 821 #endif 822 823 /* 824 * The destination address should be in the node table 825 * unless we are operating in station mode or this is a 826 * multicast/broadcast frame. 827 */ 828 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr)) 829 return ieee80211_ref_node(ic->ic_bss); 830 831 #ifndef IEEE80211_STA_ONLY 832 s = splnet(); 833 ni = ieee80211_find_node(ic, macaddr); 834 splx(s); 835 if (ni == NULL) { 836 if (ic->ic_opmode != IEEE80211_M_IBSS && 837 ic->ic_opmode != IEEE80211_M_AHDEMO) 838 return NULL; 839 840 /* 841 * Fake up a node; this handles node discovery in 842 * adhoc mode. Note that for the driver's benefit 843 * we we treat this like an association so the driver 844 * has an opportunity to setup its private state. 845 * 846 * XXX need better way to handle this; issue probe 847 * request so we can deduce rate set, etc. 848 */ 849 if ((ni = ieee80211_dup_bss(ic, macaddr)) == NULL) 850 return NULL; 851 /* XXX no rate negotiation; just dup */ 852 ni->ni_rates = ic->ic_bss->ni_rates; 853 if (ic->ic_newassoc) 854 (*ic->ic_newassoc)(ic, ni, 1); 855 } 856 return ieee80211_ref_node(ni); 857 #else 858 return NULL; /* can't get there */ 859 #endif /* IEEE80211_STA_ONLY */ 860 } 861 862 /* 863 * It is usually desirable to process a Rx packet using its sender's 864 * node-record instead of the BSS record. 865 * 866 * - AP mode: keep a node-record for every authenticated/associated 867 * station *in the BSS*. For future use, we also track neighboring 868 * APs, since they might belong to the same ESS. APs in the same 869 * ESS may bridge packets to each other, forming a Wireless 870 * Distribution System (WDS). 871 * 872 * - IBSS mode: keep a node-record for every station *in the BSS*. 873 * Also track neighboring stations by their beacons/probe responses. 874 * 875 * - monitor mode: keep a node-record for every sender, regardless 876 * of BSS. 877 * 878 * - STA mode: the only available node-record is the BSS record, 879 * ic->ic_bss. 880 * 881 * Of all the 802.11 Control packets, only the node-records for 882 * RTS packets node-record can be looked up. 883 * 884 * Return non-zero if the packet's node-record is kept, zero 885 * otherwise. 886 */ 887 static __inline int 888 ieee80211_needs_rxnode(struct ieee80211com *ic, 889 const struct ieee80211_frame *wh, const u_int8_t **bssid) 890 { 891 int monitor, rc = 0; 892 893 monitor = (ic->ic_opmode == IEEE80211_M_MONITOR); 894 895 *bssid = NULL; 896 897 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 898 case IEEE80211_FC0_TYPE_CTL: 899 if (!monitor) 900 break; 901 return (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 902 IEEE80211_FC0_SUBTYPE_RTS; 903 case IEEE80211_FC0_TYPE_MGT: 904 *bssid = wh->i_addr3; 905 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) { 906 case IEEE80211_FC0_SUBTYPE_BEACON: 907 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 908 rc = 1; 909 break; 910 default: 911 #ifndef IEEE80211_STA_ONLY 912 if (ic->ic_opmode == IEEE80211_M_STA) 913 break; 914 rc = IEEE80211_ADDR_EQ(*bssid, ic->ic_bss->ni_bssid) || 915 IEEE80211_ADDR_EQ(*bssid, etherbroadcastaddr); 916 #endif 917 break; 918 } 919 break; 920 case IEEE80211_FC0_TYPE_DATA: 921 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 922 case IEEE80211_FC1_DIR_NODS: 923 *bssid = wh->i_addr3; 924 #ifndef IEEE80211_STA_ONLY 925 if (ic->ic_opmode == IEEE80211_M_IBSS || 926 ic->ic_opmode == IEEE80211_M_AHDEMO) 927 rc = IEEE80211_ADDR_EQ(*bssid, 928 ic->ic_bss->ni_bssid); 929 #endif 930 break; 931 case IEEE80211_FC1_DIR_TODS: 932 *bssid = wh->i_addr1; 933 #ifndef IEEE80211_STA_ONLY 934 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 935 rc = IEEE80211_ADDR_EQ(*bssid, 936 ic->ic_bss->ni_bssid); 937 #endif 938 break; 939 case IEEE80211_FC1_DIR_FROMDS: 940 case IEEE80211_FC1_DIR_DSTODS: 941 *bssid = wh->i_addr2; 942 #ifndef IEEE80211_STA_ONLY 943 rc = (ic->ic_opmode == IEEE80211_M_HOSTAP); 944 #endif 945 break; 946 } 947 break; 948 } 949 return monitor || rc; 950 } 951 952 /* 953 * Drivers call this, so increase the reference count before returning 954 * the node. 955 */ 956 struct ieee80211_node * 957 ieee80211_find_rxnode(struct ieee80211com *ic, 958 const struct ieee80211_frame *wh) 959 { 960 static const u_int8_t zero[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 961 struct ieee80211_node *ni; 962 const u_int8_t *bssid; 963 int s; 964 965 if (!ieee80211_needs_rxnode(ic, wh, &bssid)) 966 return ieee80211_ref_node(ic->ic_bss); 967 968 s = splnet(); 969 ni = ieee80211_find_node(ic, wh->i_addr2); 970 splx(s); 971 972 if (ni != NULL) 973 return ieee80211_ref_node(ni); 974 #ifndef IEEE80211_STA_ONLY 975 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 976 return ieee80211_ref_node(ic->ic_bss); 977 #endif 978 /* XXX see remarks in ieee80211_find_txnode */ 979 /* XXX no rate negotiation; just dup */ 980 if ((ni = ieee80211_dup_bss(ic, wh->i_addr2)) == NULL) 981 return ieee80211_ref_node(ic->ic_bss); 982 983 IEEE80211_ADDR_COPY(ni->ni_bssid, (bssid != NULL) ? bssid : zero); 984 985 ni->ni_rates = ic->ic_bss->ni_rates; 986 if (ic->ic_newassoc) 987 (*ic->ic_newassoc)(ic, ni, 1); 988 989 DPRINTF(("faked-up node %p for %s\n", ni, 990 ether_sprintf((u_int8_t *)wh->i_addr2))); 991 992 return ieee80211_ref_node(ni); 993 } 994 995 struct ieee80211_node * 996 ieee80211_find_node_for_beacon(struct ieee80211com *ic, 997 const u_int8_t *macaddr, const struct ieee80211_channel *chan, 998 const char *ssid, u_int8_t rssi) 999 { 1000 struct ieee80211_node *ni, *keep = NULL; 1001 int s, score = 0; 1002 1003 if ((ni = ieee80211_find_node(ic, macaddr)) != NULL) { 1004 s = splnet(); 1005 1006 if (ni->ni_chan != chan && ni->ni_rssi >= rssi) 1007 score++; 1008 if (ssid[1] == 0 && ni->ni_esslen != 0) 1009 score++; 1010 if (score > 0) 1011 keep = ni; 1012 1013 splx(s); 1014 } 1015 1016 return (keep); 1017 } 1018 1019 void 1020 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni) 1021 { 1022 if (ni == ic->ic_bss) 1023 panic("freeing bss node"); 1024 1025 DPRINTF(("%s\n", ether_sprintf(ni->ni_macaddr))); 1026 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1027 RB_REMOVE(ieee80211_tree, &ic->ic_tree, ni); 1028 ic->ic_nnodes--; 1029 if (!IF_IS_EMPTY(&ni->ni_savedq)) { 1030 IF_PURGE(&ni->ni_savedq); 1031 if (ic->ic_set_tim != NULL) 1032 (*ic->ic_set_tim)(ic, ni->ni_associd, 0); 1033 } 1034 if (RB_EMPTY(&ic->ic_tree)) 1035 ic->ic_inact_timer = 0; 1036 (*ic->ic_node_free)(ic, ni); 1037 /* TBD indicate to drivers that a new node can be allocated */ 1038 } 1039 1040 void 1041 ieee80211_release_node(struct ieee80211com *ic, struct ieee80211_node *ni) 1042 { 1043 int s; 1044 1045 DPRINTF(("%s refcnt %d\n", ether_sprintf(ni->ni_macaddr), 1046 ni->ni_refcnt)); 1047 if (ieee80211_node_decref(ni) == 0 && 1048 ni->ni_state == IEEE80211_STA_COLLECT) { 1049 s = splnet(); 1050 ieee80211_free_node(ic, ni); 1051 splx(s); 1052 } 1053 } 1054 1055 void 1056 ieee80211_free_allnodes(struct ieee80211com *ic) 1057 { 1058 struct ieee80211_node *ni; 1059 int s; 1060 1061 DPRINTF(("freeing all nodes\n")); 1062 s = splnet(); 1063 while ((ni = RB_MIN(ieee80211_tree, &ic->ic_tree)) != NULL) 1064 ieee80211_free_node(ic, ni); 1065 splx(s); 1066 1067 if (ic->ic_bss != NULL) 1068 ieee80211_node_cleanup(ic, ic->ic_bss); /* for station mode */ 1069 } 1070 1071 /* 1072 * Timeout inactive nodes. 1073 */ 1074 void 1075 ieee80211_clean_nodes(struct ieee80211com *ic) 1076 { 1077 struct ieee80211_node *ni, *next_ni; 1078 u_int gen = ic->ic_scangen++; /* NB: ok 'cuz single-threaded*/ 1079 int s; 1080 1081 s = splnet(); 1082 for (ni = RB_MIN(ieee80211_tree, &ic->ic_tree); 1083 ni != NULL; ni = next_ni) { 1084 next_ni = RB_NEXT(ieee80211_tree, &ic->ic_tree, ni); 1085 if (ic->ic_nnodes <= ic->ic_max_nnodes) 1086 break; 1087 if (ni->ni_scangen == gen) /* previously handled */ 1088 continue; 1089 ni->ni_scangen = gen; 1090 if (ni->ni_refcnt > 0) 1091 continue; 1092 DPRINTF(("station %s purged from LRU cache\n", 1093 ether_sprintf(ni->ni_macaddr))); 1094 /* 1095 * Send a deauthenticate frame. 1096 */ 1097 #ifndef IEEE80211_STA_ONLY 1098 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 1099 splx(s); 1100 IEEE80211_SEND_MGMT(ic, ni, 1101 IEEE80211_FC0_SUBTYPE_DEAUTH, 1102 IEEE80211_REASON_AUTH_EXPIRE); 1103 s = splnet(); 1104 ieee80211_node_leave(ic, ni); 1105 } else 1106 #endif 1107 ieee80211_free_node(ic, ni); 1108 ic->ic_stats.is_node_timeout++; 1109 } 1110 splx(s); 1111 } 1112 1113 void 1114 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, 1115 void *arg) 1116 { 1117 struct ieee80211_node *ni; 1118 int s; 1119 1120 s = splnet(); 1121 RB_FOREACH(ni, ieee80211_tree, &ic->ic_tree) 1122 (*f)(arg, ni); 1123 splx(s); 1124 } 1125 1126 /* 1127 * Install received rate set information in the node's state block. 1128 */ 1129 int 1130 ieee80211_setup_rates(struct ieee80211com *ic, struct ieee80211_node *ni, 1131 const u_int8_t *rates, const u_int8_t *xrates, int flags) 1132 { 1133 struct ieee80211_rateset *rs = &ni->ni_rates; 1134 1135 memset(rs, 0, sizeof(*rs)); 1136 rs->rs_nrates = rates[1]; 1137 memcpy(rs->rs_rates, rates + 2, rs->rs_nrates); 1138 if (xrates != NULL) { 1139 u_int8_t nxrates; 1140 /* 1141 * Tack on 11g extended supported rate element. 1142 */ 1143 nxrates = xrates[1]; 1144 if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) { 1145 nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates; 1146 DPRINTF(("extended rate set too large; " 1147 "only using %u of %u rates\n", 1148 nxrates, xrates[1])); 1149 ic->ic_stats.is_rx_rstoobig++; 1150 } 1151 memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates); 1152 rs->rs_nrates += nxrates; 1153 } 1154 return ieee80211_fix_rate(ic, ni, flags); 1155 } 1156 1157 #ifndef IEEE80211_STA_ONLY 1158 /* 1159 * Check if the specified node supports ERP. 1160 */ 1161 int 1162 ieee80211_iserp_sta(const struct ieee80211_node *ni) 1163 { 1164 #define N(a) (sizeof (a) / sizeof (a)[0]) 1165 static const u_int8_t rates[] = { 2, 4, 11, 22, 12, 24, 48 }; 1166 const struct ieee80211_rateset *rs = &ni->ni_rates; 1167 int i, j; 1168 1169 /* 1170 * A STA supports ERP operation if it includes all the Clause 19 1171 * mandatory rates in its supported rate set. 1172 */ 1173 for (i = 0; i < N(rates); i++) { 1174 for (j = 0; j < rs->rs_nrates; j++) { 1175 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == rates[i]) 1176 break; 1177 } 1178 if (j == rs->rs_nrates) 1179 return 0; 1180 } 1181 return 1; 1182 #undef N 1183 } 1184 1185 /* 1186 * This function is called to notify the 802.1X PACP machine that a new 1187 * 802.1X port is enabled and must be authenticated. For 802.11, a port 1188 * becomes enabled whenever a STA successfully completes Open System 1189 * authentication with an AP. 1190 */ 1191 void 1192 ieee80211_needs_auth(struct ieee80211com *ic, struct ieee80211_node *ni) 1193 { 1194 /* 1195 * XXX this could be done via the route socket of via a dedicated 1196 * EAP socket or another kernel->userland notification mechanism. 1197 * The notification should include the MAC address (ni_macaddr). 1198 */ 1199 } 1200 1201 /* 1202 * Handle a station joining an RSN network. 1203 */ 1204 void 1205 ieee80211_node_join_rsn(struct ieee80211com *ic, struct ieee80211_node *ni) 1206 { 1207 DPRINTF(("station %s associated using proto %d akm 0x%x " 1208 "cipher 0x%x groupcipher 0x%x\n", ether_sprintf(ni->ni_macaddr), 1209 ni->ni_rsnprotos, ni->ni_rsnakms, ni->ni_rsnciphers, 1210 ni->ni_rsngroupcipher)); 1211 1212 ni->ni_rsn_state = RSNA_AUTHENTICATION; 1213 ic->ic_rsnsta++; 1214 1215 ni->ni_key_count = 0; 1216 ni->ni_port_valid = 0; 1217 ni->ni_flags &= ~IEEE80211_NODE_TXRXPROT; 1218 ni->ni_replaycnt = -1; /* XXX */ 1219 ni->ni_rsn_retries = 0; 1220 ni->ni_rsncipher = ni->ni_rsnciphers; 1221 timeout_set(&ni->ni_rsn_timeout, ieee80211_eapol_timeout, ni); 1222 1223 ni->ni_rsn_state = RSNA_AUTHENTICATION_2; 1224 1225 /* generate a new authenticator nonce (ANonce) */ 1226 arc4random_buf(ni->ni_nonce, EAPOL_KEY_NONCE_LEN); 1227 1228 if (!ieee80211_is_8021x_akm(ni->ni_rsnakms)) { 1229 memcpy(ni->ni_pmk, ic->ic_psk, IEEE80211_PMK_LEN); 1230 ni->ni_flags |= IEEE80211_NODE_PMK; 1231 (void)ieee80211_send_4way_msg1(ic, ni); 1232 } else if (ni->ni_flags & IEEE80211_NODE_PMK) { 1233 /* skip 802.1X auth if a cached PMK was found */ 1234 (void)ieee80211_send_4way_msg1(ic, ni); 1235 } else { 1236 /* no cached PMK found, needs full 802.1X auth */ 1237 ieee80211_needs_auth(ic, ni); 1238 } 1239 } 1240 1241 /* 1242 * Handle a station joining an 11g network. 1243 */ 1244 void 1245 ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1246 { 1247 if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)) { 1248 /* 1249 * Joining STA doesn't support short slot time. We must 1250 * disable the use of short slot time for all other associated 1251 * STAs and give the driver a chance to reconfigure the 1252 * hardware. 1253 */ 1254 if (++ic->ic_longslotsta == 1) { 1255 if (ic->ic_caps & IEEE80211_C_SHSLOT) 1256 ieee80211_set_shortslottime(ic, 0); 1257 } 1258 DPRINTF(("[%s] station needs long slot time, count %d\n", 1259 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta)); 1260 } 1261 1262 if (!ieee80211_iserp_sta(ni)) { 1263 /* 1264 * Joining STA is non-ERP. 1265 */ 1266 ic->ic_nonerpsta++; 1267 1268 DPRINTF(("[%s] station is non-ERP, %d non-ERP " 1269 "stations associated\n", ether_sprintf(ni->ni_macaddr), 1270 ic->ic_nonerpsta)); 1271 1272 /* must enable the use of protection */ 1273 if (ic->ic_protmode != IEEE80211_PROT_NONE) { 1274 DPRINTF(("enable use of protection\n")); 1275 ic->ic_flags |= IEEE80211_F_USEPROT; 1276 } 1277 1278 if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) 1279 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 1280 } else 1281 ni->ni_flags |= IEEE80211_NODE_ERP; 1282 } 1283 1284 void 1285 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, 1286 int resp) 1287 { 1288 int newassoc; 1289 1290 if (ni->ni_associd == 0) { 1291 u_int16_t aid; 1292 1293 /* 1294 * It would be clever to search the bitmap 1295 * more efficiently, but this will do for now. 1296 */ 1297 for (aid = 1; aid < ic->ic_max_aid; aid++) { 1298 if (!IEEE80211_AID_ISSET(aid, 1299 ic->ic_aid_bitmap)) 1300 break; 1301 } 1302 if (aid >= ic->ic_max_aid) { 1303 IEEE80211_SEND_MGMT(ic, ni, resp, 1304 IEEE80211_REASON_ASSOC_TOOMANY); 1305 ieee80211_node_leave(ic, ni); 1306 return; 1307 } 1308 ni->ni_associd = aid | 0xc000; 1309 IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap); 1310 newassoc = 1; 1311 if (ic->ic_curmode == IEEE80211_MODE_11G) 1312 ieee80211_node_join_11g(ic, ni); 1313 } else 1314 newassoc = 0; 1315 1316 DPRINTF(("station %s %s associated at aid %d\n", 1317 ether_sprintf(ni->ni_macaddr), newassoc ? "newly" : "already", 1318 ni->ni_associd & ~0xc000)); 1319 1320 /* give driver a chance to setup state like ni_txrate */ 1321 if (ic->ic_newassoc) 1322 (*ic->ic_newassoc)(ic, ni, newassoc); 1323 IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS); 1324 ieee80211_node_newstate(ni, IEEE80211_STA_ASSOC); 1325 1326 if (!(ic->ic_flags & IEEE80211_F_RSNON)) { 1327 ni->ni_port_valid = 1; 1328 ni->ni_rsncipher = IEEE80211_CIPHER_USEGROUP; 1329 } else 1330 ieee80211_node_join_rsn(ic, ni); 1331 1332 #if NBRIDGE > 0 1333 /* 1334 * If the parent interface belongs to a bridge, learn 1335 * the node's address dynamically on this interface. 1336 */ 1337 if (ic->ic_if.if_bridge != NULL) 1338 bridge_update(&ic->ic_if, 1339 (struct ether_addr *)ni->ni_macaddr, 0); 1340 #endif 1341 } 1342 1343 /* 1344 * Handle a station leaving an RSN network. 1345 */ 1346 void 1347 ieee80211_node_leave_rsn(struct ieee80211com *ic, struct ieee80211_node *ni) 1348 { 1349 ni->ni_rsn_state = RSNA_DISCONNECTED; 1350 ic->ic_rsnsta--; 1351 1352 ni->ni_rsn_state = RSNA_INITIALIZE; 1353 if ((ni->ni_flags & IEEE80211_NODE_REKEY) && 1354 --ic->ic_rsn_keydonesta == 0) 1355 ieee80211_setkeysdone(ic); 1356 ni->ni_flags &= ~IEEE80211_NODE_REKEY; 1357 1358 ni->ni_flags &= ~IEEE80211_NODE_PMK; 1359 ni->ni_rsn_gstate = RSNA_IDLE; 1360 1361 timeout_del(&ni->ni_rsn_timeout); 1362 ni->ni_rsn_retries = 0; 1363 ni->ni_flags &= ~IEEE80211_NODE_TXRXPROT; 1364 ni->ni_port_valid = 0; 1365 (*ic->ic_delete_key)(ic, ni, &ni->ni_pairwise_key); 1366 } 1367 1368 /* 1369 * Handle a station leaving an 11g network. 1370 */ 1371 void 1372 ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni) 1373 { 1374 if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)) { 1375 #ifdef DIAGNOSTIC 1376 if (ic->ic_longslotsta == 0) { 1377 panic("bogus long slot station count %d", 1378 ic->ic_longslotsta); 1379 } 1380 #endif 1381 /* leaving STA did not support short slot time */ 1382 if (--ic->ic_longslotsta == 0) { 1383 /* 1384 * All associated STAs now support short slot time, so 1385 * enable this feature and give the driver a chance to 1386 * reconfigure the hardware. Notice that IBSS always 1387 * use a long slot time. 1388 */ 1389 if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 1390 ic->ic_opmode != IEEE80211_M_IBSS) 1391 ieee80211_set_shortslottime(ic, 1); 1392 } 1393 DPRINTF(("[%s] long slot time station leaves, count %d\n", 1394 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta)); 1395 } 1396 1397 if (!(ni->ni_flags & IEEE80211_NODE_ERP)) { 1398 #ifdef DIAGNOSTIC 1399 if (ic->ic_nonerpsta == 0) { 1400 panic("bogus non-ERP station count %d\n", 1401 ic->ic_nonerpsta); 1402 } 1403 #endif 1404 /* leaving STA was non-ERP */ 1405 if (--ic->ic_nonerpsta == 0) { 1406 /* 1407 * All associated STAs are now ERP capable, disable use 1408 * of protection and re-enable short preamble support. 1409 */ 1410 ic->ic_flags &= ~IEEE80211_F_USEPROT; 1411 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) 1412 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 1413 } 1414 DPRINTF(("[%s] non-ERP station leaves, count %d\n", 1415 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta)); 1416 } 1417 } 1418 1419 /* 1420 * Handle bookkeeping for station deauthentication/disassociation 1421 * when operating as an ap. 1422 */ 1423 void 1424 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 1425 { 1426 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 1427 panic("not in ap mode, mode %u", ic->ic_opmode); 1428 /* 1429 * If node wasn't previously associated all 1430 * we need to do is reclaim the reference. 1431 */ 1432 if (ni->ni_associd == 0) 1433 return; 1434 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap); 1435 ni->ni_associd = 0; 1436 1437 if (ni->ni_pwrsave == IEEE80211_PS_DOZE) 1438 ic->ic_pssta--; 1439 1440 if (ic->ic_flags & IEEE80211_F_RSNON) 1441 ieee80211_node_leave_rsn(ic, ni); 1442 1443 if (ic->ic_curmode == IEEE80211_MODE_11G) 1444 ieee80211_node_leave_11g(ic, ni); 1445 1446 ieee80211_node_newstate(ni, IEEE80211_STA_COLLECT); 1447 1448 #if NBRIDGE > 0 1449 /* 1450 * If the parent interface belongs to a bridge, delete 1451 * any dynamically learned address for this node. 1452 */ 1453 if (ic->ic_if.if_bridge != NULL) 1454 bridge_update(&ic->ic_if, 1455 (struct ether_addr *)ni->ni_macaddr, 1); 1456 #endif 1457 } 1458 1459 static int 1460 ieee80211_do_slow_print(struct ieee80211com *ic, int *did_print) 1461 { 1462 static const struct timeval merge_print_intvl = { 1463 .tv_sec = 1, .tv_usec = 0 1464 }; 1465 if ((ic->ic_if.if_flags & IFF_LINK0) == 0) 1466 return 0; 1467 if (!*did_print && (ic->ic_if.if_flags & IFF_DEBUG) == 0 && 1468 !ratecheck(&ic->ic_last_merge_print, &merge_print_intvl)) 1469 return 0; 1470 1471 *did_print = 1; 1472 return 1; 1473 } 1474 1475 /* ieee80211_ibss_merge helps merge 802.11 ad hoc networks. The 1476 * convention, set by the Wireless Ethernet Compatibility Alliance 1477 * (WECA), is that an 802.11 station will change its BSSID to match 1478 * the "oldest" 802.11 ad hoc network, on the same channel, that 1479 * has the station's desired SSID. The "oldest" 802.11 network 1480 * sends beacons with the greatest TSF timestamp. 1481 * 1482 * Return ENETRESET if the BSSID changed, 0 otherwise. 1483 * 1484 * XXX Perhaps we should compensate for the time that elapses 1485 * between the MAC receiving the beacon and the host processing it 1486 * in ieee80211_ibss_merge. 1487 */ 1488 int 1489 ieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni, 1490 u_int64_t local_tsft) 1491 { 1492 u_int64_t beacon_tsft; 1493 int did_print = 0, sign; 1494 union { 1495 u_int64_t word; 1496 u_int8_t tstamp[8]; 1497 } u; 1498 1499 /* ensure alignment */ 1500 (void)memcpy(&u, &ni->ni_tstamp[0], sizeof(u)); 1501 beacon_tsft = letoh64(u.word); 1502 1503 /* we are faster, let the other guy catch up */ 1504 if (beacon_tsft < local_tsft) 1505 sign = -1; 1506 else 1507 sign = 1; 1508 1509 if (IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) { 1510 if (!ieee80211_do_slow_print(ic, &did_print)) 1511 return 0; 1512 printf("%s: tsft offset %s%llu\n", ic->ic_if.if_xname, 1513 (sign < 0) ? "-" : "", 1514 (sign < 0) 1515 ? (local_tsft - beacon_tsft) 1516 : (beacon_tsft - local_tsft)); 1517 return 0; 1518 } 1519 1520 if (sign < 0) 1521 return 0; 1522 1523 if (ieee80211_match_bss(ic, ni) != 0) 1524 return 0; 1525 1526 if (ieee80211_do_slow_print(ic, &did_print)) { 1527 printf("%s: ieee80211_ibss_merge: bssid mismatch %s\n", 1528 ic->ic_if.if_xname, ether_sprintf(ni->ni_bssid)); 1529 printf("%s: my tsft %llu beacon tsft %llu\n", 1530 ic->ic_if.if_xname, local_tsft, beacon_tsft); 1531 printf("%s: sync TSF with %s\n", 1532 ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr)); 1533 } 1534 1535 ic->ic_flags &= ~IEEE80211_F_SIBSS; 1536 1537 /* negotiate rates with new IBSS */ 1538 ieee80211_fix_rate(ic, ni, IEEE80211_F_DOFRATE | 1539 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1540 if (ni->ni_rates.rs_nrates == 0) { 1541 if (ieee80211_do_slow_print(ic, &did_print)) { 1542 printf("%s: rates mismatch, BSSID %s\n", 1543 ic->ic_if.if_xname, ether_sprintf(ni->ni_bssid)); 1544 } 1545 return 0; 1546 } 1547 1548 if (ieee80211_do_slow_print(ic, &did_print)) { 1549 printf("%s: sync BSSID %s -> ", 1550 ic->ic_if.if_xname, ether_sprintf(ic->ic_bss->ni_bssid)); 1551 printf("%s ", ether_sprintf(ni->ni_bssid)); 1552 printf("(from %s)\n", ether_sprintf(ni->ni_macaddr)); 1553 } 1554 1555 ieee80211_node_newstate(ni, IEEE80211_STA_BSS); 1556 (*ic->ic_node_copy)(ic, ic->ic_bss, ni); 1557 1558 return ENETRESET; 1559 } 1560 1561 void 1562 ieee80211_set_tim(struct ieee80211com *ic, int aid, int set) 1563 { 1564 if (set) 1565 setbit(ic->ic_tim_bitmap, aid & ~0xc000); 1566 else 1567 clrbit(ic->ic_tim_bitmap, aid & ~0xc000); 1568 } 1569 #endif /* IEEE80211_STA_ONLY */ 1570 1571 /* 1572 * Compare nodes in the tree by lladdr 1573 */ 1574 int 1575 ieee80211_node_cmp(const struct ieee80211_node *b1, 1576 const struct ieee80211_node *b2) 1577 { 1578 return (memcmp(b1->ni_macaddr, b2->ni_macaddr, IEEE80211_ADDR_LEN)); 1579 } 1580 1581 /* 1582 * Generate red-black tree function logic 1583 */ 1584 RB_GENERATE(ieee80211_tree, ieee80211_node, ni_node, ieee80211_node_cmp); 1585