11a1e1d21SSam Leffler /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3fe267a55SPedro F. Giffuni * 47535e66aSSam Leffler * Copyright (c) 2001 Atsushi Onoe 510ad9a77SSam Leffler * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 61a1e1d21SSam Leffler * All rights reserved. 71a1e1d21SSam Leffler * 81a1e1d21SSam Leffler * Redistribution and use in source and binary forms, with or without 91a1e1d21SSam Leffler * modification, are permitted provided that the following conditions 101a1e1d21SSam Leffler * are met: 111a1e1d21SSam Leffler * 1. Redistributions of source code must retain the above copyright 127535e66aSSam Leffler * notice, this list of conditions and the following disclaimer. 137535e66aSSam Leffler * 2. Redistributions in binary form must reproduce the above copyright 147535e66aSSam Leffler * notice, this list of conditions and the following disclaimer in the 157535e66aSSam Leffler * documentation and/or other materials provided with the distribution. 161a1e1d21SSam Leffler * 177535e66aSSam Leffler * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 187535e66aSSam Leffler * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 197535e66aSSam Leffler * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 207535e66aSSam Leffler * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 217535e66aSSam Leffler * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 227535e66aSSam Leffler * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 237535e66aSSam Leffler * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 247535e66aSSam Leffler * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 257535e66aSSam Leffler * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 267535e66aSSam Leffler * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 271a1e1d21SSam Leffler */ 281a1e1d21SSam Leffler 291a1e1d21SSam Leffler #include <sys/cdefs.h> 30b032f27cSSam Leffler #include "opt_wlan.h" 31b032f27cSSam Leffler 321a1e1d21SSam Leffler #include <sys/param.h> 331a1e1d21SSam Leffler #include <sys/systm.h> 341a1e1d21SSam Leffler #include <sys/mbuf.h> 351a1e1d21SSam Leffler #include <sys/malloc.h> 361a1e1d21SSam Leffler #include <sys/kernel.h> 371a1e1d21SSam Leffler 388a1b9b6aSSam Leffler #include <sys/socket.h> 391a1e1d21SSam Leffler 401a1e1d21SSam Leffler #include <net/if.h> 4176039bc8SGleb Smirnoff #include <net/if_var.h> 421a1e1d21SSam Leffler #include <net/if_media.h> 431a1e1d21SSam Leffler #include <net/ethernet.h> 441a1e1d21SSam Leffler 451a1e1d21SSam Leffler #include <net80211/ieee80211_var.h> 46b032f27cSSam Leffler #include <net80211/ieee80211_input.h> 47616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 48616190d0SSam Leffler #include <net80211/ieee80211_superg.h> 49616190d0SSam Leffler #endif 5010ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA 5110ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h> 5210ad9a77SSam Leffler #endif 53b032f27cSSam Leffler #include <net80211/ieee80211_wds.h> 5459aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h> 55b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h> 5651172f62SAdrian Chadd #include <net80211/ieee80211_vht.h> 571a1e1d21SSam Leffler 581a1e1d21SSam Leffler #include <net/bpf.h> 591a1e1d21SSam Leffler 604a8e4d15SBjoern A. Zeeb #ifdef IEEE80211_DEBUG_REFCNT 614a8e4d15SBjoern A. Zeeb #define __debrefcnt_used 624a8e4d15SBjoern A. Zeeb #else 634a8e4d15SBjoern A. Zeeb #define __debrefcnt_used __unused 644a8e4d15SBjoern A. Zeeb #endif 654a8e4d15SBjoern A. Zeeb 667268fa64SSam Leffler /* 6759aa14a9SRui Paulo * IEEE80211_NODE_HASHSIZE must be a power of 2. 6859aa14a9SRui Paulo */ 6959aa14a9SRui Paulo CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0); 7059aa14a9SRui Paulo 7159aa14a9SRui Paulo /* 727268fa64SSam Leffler * Association id's are managed with a bit vector. 737268fa64SSam Leffler */ 74b032f27cSSam Leffler #define IEEE80211_AID_SET(_vap, b) \ 75b032f27cSSam Leffler ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \ 76b032f27cSSam Leffler (1 << (IEEE80211_AID(b) % 32))) 77b032f27cSSam Leffler #define IEEE80211_AID_CLR(_vap, b) \ 78b032f27cSSam Leffler ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \ 79b032f27cSSam Leffler ~(1 << (IEEE80211_AID(b) % 32))) 80b032f27cSSam Leffler #define IEEE80211_AID_ISSET(_vap, b) \ 81b032f27cSSam Leffler ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32))) 827268fa64SSam Leffler 8368e8e04eSSam Leffler static int ieee80211_sta_join1(struct ieee80211_node *); 8468e8e04eSSam Leffler 85db195a52SBjoern A. Zeeb static struct ieee80211_node *ieee80211_alloc_node( 86db195a52SBjoern A. Zeeb struct ieee80211_node_table *, struct ieee80211vap *, 873a11944bSBjoern A. Zeeb const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *, int); 8838c208f8SSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211vap *, 8938c208f8SSam Leffler const uint8_t [IEEE80211_ADDR_LEN]); 90ea3d5fd9SAdrian Chadd static int node_init(struct ieee80211_node *); 918a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *); 928a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *); 93b032f27cSSam Leffler static void node_age(struct ieee80211_node *); 9468e8e04eSSam Leffler static int8_t node_getrssi(const struct ieee80211_node *); 9568e8e04eSSam Leffler static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *); 96b032f27cSSam Leffler static void node_getmimoinfo(const struct ieee80211_node *, 97b032f27cSSam Leffler struct ieee80211_mimo_info *); 981a1e1d21SSam Leffler 994a8e4d15SBjoern A. Zeeb static void __ieee80211_free_node(struct ieee80211_node *); 1008a1b9b6aSSam Leffler 1018a055831SAdrian Chadd static void node_reclaim(struct ieee80211_node_table *nt, 1028a055831SAdrian Chadd struct ieee80211_node *ni); 1038a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic, 104c1225b52SSam Leffler struct ieee80211_node_table *nt, const char *name, 10568e8e04eSSam Leffler int inact, int keymaxix); 106b032f27cSSam Leffler static void ieee80211_node_table_reset(struct ieee80211_node_table *, 107b032f27cSSam Leffler struct ieee80211vap *); 1088a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt); 109f1481c8dSAdrian Chadd static void ieee80211_vap_erp_timeout(struct ieee80211vap *); 1101a1e1d21SSam Leffler 11132346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state"); 112b032f27cSSam Leffler MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie"); 11337c150c4SSam Leffler 1141a1e1d21SSam Leffler void 1158a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic) 1161a1e1d21SSam Leffler { 1175b16c28cSSam Leffler /* XXX really want maxlen enforced per-sta */ 1185b16c28cSSam Leffler ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8, 1195b16c28cSSam Leffler "802.11 staging q"); 120b032f27cSSam Leffler ieee80211_node_table_init(ic, &ic->ic_sta, "station", 121b032f27cSSam Leffler IEEE80211_INACT_INIT, ic->ic_max_keyix); 122fd90e2edSJung-uk Kim callout_init(&ic->ic_inact, 1); 123b032f27cSSam Leffler callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz, 124b032f27cSSam Leffler ieee80211_node_timeout, ic); 1251a1e1d21SSam Leffler 1268a1b9b6aSSam Leffler ic->ic_node_alloc = node_alloc; 127ea3d5fd9SAdrian Chadd ic->ic_node_init = node_init; 1288a1b9b6aSSam Leffler ic->ic_node_free = node_free; 1298a1b9b6aSSam Leffler ic->ic_node_cleanup = node_cleanup; 130b032f27cSSam Leffler ic->ic_node_age = node_age; 131b032f27cSSam Leffler ic->ic_node_drain = node_age; /* NB: same as age */ 1328a1b9b6aSSam Leffler ic->ic_node_getrssi = node_getrssi; 13368e8e04eSSam Leffler ic->ic_node_getsignal = node_getsignal; 134b032f27cSSam Leffler ic->ic_node_getmimoinfo = node_getmimoinfo; 1358a1b9b6aSSam Leffler 136b032f27cSSam Leffler /* 137b032f27cSSam Leffler * Set flags to be propagated to all vap's; 138b032f27cSSam Leffler * these define default behaviour/configuration. 139b032f27cSSam Leffler */ 140c066143cSSam Leffler ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */ 141c1225b52SSam Leffler } 142c1225b52SSam Leffler 143c1225b52SSam Leffler void 1448a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic) 1451a1e1d21SSam Leffler { 1461a1e1d21SSam Leffler 147b032f27cSSam Leffler callout_drain(&ic->ic_inact); 148acc4f7f5SSam Leffler ieee80211_node_table_cleanup(&ic->ic_sta); 14958a7c4bfSKyle Evans ieee80211_ageq_drain(&ic->ic_stageq); 1505b16c28cSSam Leffler ieee80211_ageq_cleanup(&ic->ic_stageq); 151b032f27cSSam Leffler } 152b032f27cSSam Leffler 153b032f27cSSam Leffler void 154b032f27cSSam Leffler ieee80211_node_vattach(struct ieee80211vap *vap) 155b032f27cSSam Leffler { 156b032f27cSSam Leffler /* NB: driver can override */ 157b032f27cSSam Leffler vap->iv_max_aid = IEEE80211_AID_DEF; 158b032f27cSSam Leffler 1592ec4c3c7SGordon Bergling /* default station inactivity timer settings */ 160b032f27cSSam Leffler vap->iv_inact_init = IEEE80211_INACT_INIT; 161b032f27cSSam Leffler vap->iv_inact_auth = IEEE80211_INACT_AUTH; 162b032f27cSSam Leffler vap->iv_inact_run = IEEE80211_INACT_RUN; 163b032f27cSSam Leffler vap->iv_inact_probe = IEEE80211_INACT_PROBE; 164be1054edSSam Leffler 165be1054edSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT, 166be1054edSSam Leffler "%s: init %u auth %u run %u probe %u\n", __func__, 167be1054edSSam Leffler vap->iv_inact_init, vap->iv_inact_auth, 168be1054edSSam Leffler vap->iv_inact_run, vap->iv_inact_probe); 169b032f27cSSam Leffler } 170b032f27cSSam Leffler 171b032f27cSSam Leffler void 172b032f27cSSam Leffler ieee80211_node_latevattach(struct ieee80211vap *vap) 173b032f27cSSam Leffler { 17449619f73SBjoern A. Zeeb 17549619f73SBjoern A. Zeeb /* XXX should ieee80211_vap_attach(), our only caller hold the lock? */ 17649619f73SBjoern A. Zeeb IEEE80211_UNLOCK_ASSERT(vap->iv_ic); 17749619f73SBjoern A. Zeeb 178b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 179b032f27cSSam Leffler /* XXX should we allow max aid to be zero? */ 180b032f27cSSam Leffler if (vap->iv_max_aid < IEEE80211_AID_MIN) { 181b032f27cSSam Leffler vap->iv_max_aid = IEEE80211_AID_MIN; 182b032f27cSSam Leffler if_printf(vap->iv_ifp, 183b032f27cSSam Leffler "WARNING: max aid too small, changed to %d\n", 184b032f27cSSam Leffler vap->iv_max_aid); 185b032f27cSSam Leffler } 186b9b53389SAdrian Chadd vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC( 187c5abbba3SDag-Erling Smørgrav howmany(vap->iv_max_aid, 32) * sizeof(uint32_t), 188b9b53389SAdrian Chadd M_80211_NODE, 189b9b53389SAdrian Chadd IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 190b032f27cSSam Leffler if (vap->iv_aid_bitmap == NULL) { 191b032f27cSSam Leffler /* XXX no way to recover */ 192b032f27cSSam Leffler printf("%s: no memory for AID bitmap, max aid %d!\n", 193b032f27cSSam Leffler __func__, vap->iv_max_aid); 194b032f27cSSam Leffler vap->iv_max_aid = 0; 195b032f27cSSam Leffler } 196b032f27cSSam Leffler } 197b032f27cSSam Leffler 19849619f73SBjoern A. Zeeb IEEE80211_LOCK(vap->iv_ic); 199b032f27cSSam Leffler ieee80211_reset_bss(vap); 20049619f73SBjoern A. Zeeb IEEE80211_UNLOCK(vap->iv_ic); 201b032f27cSSam Leffler 202b032f27cSSam Leffler vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode); 203b032f27cSSam Leffler } 204b032f27cSSam Leffler 205b032f27cSSam Leffler void 206b032f27cSSam Leffler ieee80211_node_vdetach(struct ieee80211vap *vap) 207b032f27cSSam Leffler { 208b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 209b032f27cSSam Leffler 21049619f73SBjoern A. Zeeb /* XXX should ieee80211_vap_detach(), our only caller hold the lock? */ 21149619f73SBjoern A. Zeeb IEEE80211_UNLOCK_ASSERT(vap->iv_ic); 21249619f73SBjoern A. Zeeb 213b032f27cSSam Leffler ieee80211_node_table_reset(&ic->ic_sta, vap); 21449619f73SBjoern A. Zeeb IEEE80211_LOCK(ic); 215b032f27cSSam Leffler if (vap->iv_bss != NULL) { 216b032f27cSSam Leffler ieee80211_free_node(vap->iv_bss); 21791b4225aSBjoern A. Zeeb vap->iv_update_bss(vap, NULL); 218b032f27cSSam Leffler } 21949619f73SBjoern A. Zeeb IEEE80211_UNLOCK(ic); 220b032f27cSSam Leffler if (vap->iv_aid_bitmap != NULL) { 221b9b53389SAdrian Chadd IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE); 222b032f27cSSam Leffler vap->iv_aid_bitmap = NULL; 2238a1b9b6aSSam Leffler } 2248a1b9b6aSSam Leffler } 2258a1b9b6aSSam Leffler 2268a1b9b6aSSam Leffler /* 2278a1b9b6aSSam Leffler * Port authorize/unauthorize interfaces for use by an authenticator. 2288a1b9b6aSSam Leffler */ 2298a1b9b6aSSam Leffler 2308a1b9b6aSSam Leffler void 231e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni) 2328a1b9b6aSSam Leffler { 233be1054edSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 234be1054edSSam Leffler 2358a1b9b6aSSam Leffler ni->ni_flags |= IEEE80211_NODE_AUTH; 236be1054edSSam Leffler ni->ni_inact_reload = vap->iv_inact_run; 237c066143cSSam Leffler ni->ni_inact = ni->ni_inact_reload; 238be1054edSSam Leffler 239be1054edSSam Leffler IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 240be1054edSSam Leffler "%s: inact_reload %u", __func__, ni->ni_inact_reload); 2418a1b9b6aSSam Leffler } 2428a1b9b6aSSam Leffler 2438a1b9b6aSSam Leffler void 244e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni) 2458a1b9b6aSSam Leffler { 246be1054edSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 247be1054edSSam Leffler 2488a1b9b6aSSam Leffler ni->ni_flags &= ~IEEE80211_NODE_AUTH; 249be1054edSSam Leffler ni->ni_inact_reload = vap->iv_inact_auth; 250c066143cSSam Leffler if (ni->ni_inact > ni->ni_inact_reload) 251c066143cSSam Leffler ni->ni_inact = ni->ni_inact_reload; 252be1054edSSam Leffler 253be1054edSSam Leffler IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 254be1054edSSam Leffler "%s: inact_reload %u inact %u", __func__, 255be1054edSSam Leffler ni->ni_inact_reload, ni->ni_inact); 2568a1b9b6aSSam Leffler } 2578a1b9b6aSSam Leffler 2588a1b9b6aSSam Leffler /* 25901a03542SSam Leffler * Fix tx parameters for a node according to ``association state''. 26001a03542SSam Leffler */ 261d77148fbSSam Leffler void 262d77148fbSSam Leffler ieee80211_node_setuptxparms(struct ieee80211_node *ni) 26301a03542SSam Leffler { 26401a03542SSam Leffler struct ieee80211vap *vap = ni->ni_vap; 265f76cde95SSam Leffler enum ieee80211_phymode mode; 26601a03542SSam Leffler 267f6b98645SAndriy Voskoboinyk if (ni->ni_flags & IEEE80211_NODE_VHT) { 268f6b98645SAndriy Voskoboinyk if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)) 269f6b98645SAndriy Voskoboinyk mode = IEEE80211_MODE_VHT_5GHZ; 270f6b98645SAndriy Voskoboinyk else 271f6b98645SAndriy Voskoboinyk mode = IEEE80211_MODE_VHT_2GHZ; 272f6b98645SAndriy Voskoboinyk } else if (ni->ni_flags & IEEE80211_NODE_HT) { 27301a03542SSam Leffler if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)) 274f76cde95SSam Leffler mode = IEEE80211_MODE_11NA; 27501a03542SSam Leffler else 276f76cde95SSam Leffler mode = IEEE80211_MODE_11NG; 27701a03542SSam Leffler } else { /* legacy rate handling */ 278f76cde95SSam Leffler if (IEEE80211_IS_CHAN_ST(ni->ni_chan)) 279f76cde95SSam Leffler mode = IEEE80211_MODE_STURBO_A; 2806a76ae21SSam Leffler else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan)) 2816a76ae21SSam Leffler mode = IEEE80211_MODE_HALF; 2826a76ae21SSam Leffler else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan)) 2836a76ae21SSam Leffler mode = IEEE80211_MODE_QUARTER; 284c5262b82SSam Leffler /* NB: 108A should be handled as 11a */ 285f76cde95SSam Leffler else if (IEEE80211_IS_CHAN_A(ni->ni_chan)) 286f76cde95SSam Leffler mode = IEEE80211_MODE_11A; 287c5262b82SSam Leffler else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) || 288c5262b82SSam Leffler (ni->ni_flags & IEEE80211_NODE_ERP)) 289f76cde95SSam Leffler mode = IEEE80211_MODE_11G; 29001a03542SSam Leffler else 291f76cde95SSam Leffler mode = IEEE80211_MODE_11B; 29201a03542SSam Leffler } 293f76cde95SSam Leffler ni->ni_txparms = &vap->iv_txparms[mode]; 29401a03542SSam Leffler } 29501a03542SSam Leffler 29601a03542SSam Leffler /* 2978a1b9b6aSSam Leffler * Set/change the channel. The rate set is also updated as 2988a1b9b6aSSam Leffler * to insure a consistent view by drivers. 299b032f27cSSam Leffler * XXX should be private but hostap needs it to deal with CSA 3008a1b9b6aSSam Leffler */ 301b032f27cSSam Leffler void 302b032f27cSSam Leffler ieee80211_node_set_chan(struct ieee80211_node *ni, 303b032f27cSSam Leffler struct ieee80211_channel *chan) 3048a1b9b6aSSam Leffler { 305b032f27cSSam Leffler struct ieee80211com *ic = ni->ni_ic; 30601a03542SSam Leffler struct ieee80211vap *vap = ni->ni_vap; 30701a03542SSam Leffler enum ieee80211_phymode mode; 30868e8e04eSSam Leffler 309b032f27cSSam Leffler KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel")); 310b032f27cSSam Leffler 3118a1b9b6aSSam Leffler ni->ni_chan = chan; 31201a03542SSam Leffler mode = ieee80211_chan2mode(chan); 31368e8e04eSSam Leffler if (IEEE80211_IS_CHAN_HT(chan)) { 31468e8e04eSSam Leffler /* 315597029bfSBernhard Schmidt * We must install the legacy rate est in ni_rates and the 31668e8e04eSSam Leffler * HT rate set in ni_htrates. 31768e8e04eSSam Leffler */ 31868e8e04eSSam Leffler ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan); 31901a03542SSam Leffler /* 32001a03542SSam Leffler * Setup bss tx parameters based on operating mode. We 32101a03542SSam Leffler * use legacy rates when operating in a mixed HT+non-HT bss 32201a03542SSam Leffler * and non-ERP rates in 11g for mixed ERP+non-ERP bss. 32301a03542SSam Leffler */ 32401a03542SSam Leffler if (mode == IEEE80211_MODE_11NA && 3252bfc8a91SSam Leffler (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0) 32601a03542SSam Leffler mode = IEEE80211_MODE_11A; 32701a03542SSam Leffler else if (mode == IEEE80211_MODE_11NG && 3282bfc8a91SSam Leffler (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0) 32901a03542SSam Leffler mode = IEEE80211_MODE_11G; 33001a03542SSam Leffler if (mode == IEEE80211_MODE_11G && 33101a03542SSam Leffler (vap->iv_flags & IEEE80211_F_PUREG) == 0) 33201a03542SSam Leffler mode = IEEE80211_MODE_11B; 33368e8e04eSSam Leffler } 33401a03542SSam Leffler ni->ni_txparms = &vap->iv_txparms[mode]; 33541b3c790SSam Leffler ni->ni_rates = *ieee80211_get_suprates(ic, chan); 3361a1e1d21SSam Leffler } 3371a1e1d21SSam Leffler 338f9cd9174SSam Leffler static __inline void 339f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss) 340f9cd9174SSam Leffler { 341f9cd9174SSam Leffler /* propagate useful state */ 342f9cd9174SSam Leffler nbss->ni_authmode = obss->ni_authmode; 343f9cd9174SSam Leffler nbss->ni_txpower = obss->ni_txpower; 344f9cd9174SSam Leffler nbss->ni_vlan = obss->ni_vlan; 345f9cd9174SSam Leffler /* XXX statistics? */ 346b032f27cSSam Leffler /* XXX legacy WDS bssid? */ 347f9cd9174SSam Leffler } 348f9cd9174SSam Leffler 3491a1e1d21SSam Leffler void 350b032f27cSSam Leffler ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan) 3511a1e1d21SSam Leffler { 352b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 3531a1e1d21SSam Leffler struct ieee80211_node *ni; 3548a1b9b6aSSam Leffler 355b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 356869897d2SAdrian Chadd "%s: creating %s on channel %u%c flags 0x%08x\n", __func__, 35759aa14a9SRui Paulo ieee80211_opmode_name[vap->iv_opmode], 3582b8b8ae8SAdrian Chadd ieee80211_chan2ieee(ic, chan), 359869897d2SAdrian Chadd ieee80211_channel_type_char(chan), 360869897d2SAdrian Chadd chan->ic_flags); 3618a1b9b6aSSam Leffler 3623a11944bSBjoern A. Zeeb ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr, 3633a11944bSBjoern A. Zeeb __func__, __LINE__); 364acc4f7f5SSam Leffler if (ni == NULL) { 365acc4f7f5SSam Leffler /* XXX recovery? */ 3668a1b9b6aSSam Leffler return; 3678a1b9b6aSSam Leffler } 368b032f27cSSam Leffler IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr); 369b032f27cSSam Leffler ni->ni_esslen = vap->iv_des_ssid[0].len; 370b032f27cSSam Leffler memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen); 371b032f27cSSam Leffler if (vap->iv_bss != NULL) 372b032f27cSSam Leffler copy_bss(ni, vap->iv_bss); 373d365f9c7SSam Leffler ni->ni_intval = ic->ic_bintval; 374b032f27cSSam Leffler if (vap->iv_flags & IEEE80211_F_PRIVACY) 3751a1e1d21SSam Leffler ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 3761a1e1d21SSam Leffler if (ic->ic_phytype == IEEE80211_T_FH) { 3771a1e1d21SSam Leffler ni->ni_fhdwell = 200; /* XXX */ 3781a1e1d21SSam Leffler ni->ni_fhindex = 1; 3791a1e1d21SSam Leffler } 380b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_IBSS) { 3818a1b9b6aSSam Leffler ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */ 382b032f27cSSam Leffler if (vap->iv_flags & IEEE80211_F_DESBSSID) 383b032f27cSSam Leffler IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid); 384fe49f061SSam Leffler else { 385af7d9f8eSBjoern A. Zeeb net80211_get_random_bytes(ni->ni_bssid, 386af7d9f8eSBjoern A. Zeeb IEEE80211_ADDR_LEN); 387fe49f061SSam Leffler /* clear group bit, add local bit */ 388fe49f061SSam Leffler ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02; 389fe49f061SSam Leffler } 390b032f27cSSam Leffler } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) { 391b032f27cSSam Leffler if (vap->iv_flags & IEEE80211_F_DESBSSID) 392b032f27cSSam Leffler IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid); 39350d8b493SSam Leffler else 39410ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA 39510ad9a77SSam Leffler if ((vap->iv_caps & IEEE80211_C_TDMA) == 0) 39610ad9a77SSam Leffler #endif 39750d8b493SSam Leffler memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN); 39859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH 39959aa14a9SRui Paulo } else if (vap->iv_opmode == IEEE80211_M_MBSS) { 40059aa14a9SRui Paulo ni->ni_meshidlen = vap->iv_mesh->ms_idlen; 40159aa14a9SRui Paulo memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen); 40259aa14a9SRui Paulo #endif 4038a1b9b6aSSam Leffler } 4048a1b9b6aSSam Leffler /* 4058a1b9b6aSSam Leffler * Fix the channel and related attributes. 4068a1b9b6aSSam Leffler */ 407b032f27cSSam Leffler /* clear DFS CAC state on previous channel */ 408b032f27cSSam Leffler if (ic->ic_bsschan != IEEE80211_CHAN_ANYC && 409b032f27cSSam Leffler ic->ic_bsschan->ic_freq != chan->ic_freq && 410b032f27cSSam Leffler IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) 411b032f27cSSam Leffler ieee80211_dfs_cac_clear(ic, ic->ic_bsschan); 41268e8e04eSSam Leffler ic->ic_bsschan = chan; 413b032f27cSSam Leffler ieee80211_node_set_chan(ni, chan); 41468e8e04eSSam Leffler ic->ic_curmode = ieee80211_chan2mode(chan); 4158a1b9b6aSSam Leffler /* 416b032f27cSSam Leffler * Do mode-specific setup. 4178a1b9b6aSSam Leffler */ 41868e8e04eSSam Leffler if (IEEE80211_IS_CHAN_FULL(chan)) { 41968e8e04eSSam Leffler if (IEEE80211_IS_CHAN_ANYG(chan)) { 42068e8e04eSSam Leffler /* 421b032f27cSSam Leffler * Use a mixed 11b/11g basic rate set. 42268e8e04eSSam Leffler */ 423b032f27cSSam Leffler ieee80211_setbasicrates(&ni->ni_rates, 42468e8e04eSSam Leffler IEEE80211_MODE_11G); 425b032f27cSSam Leffler if (vap->iv_flags & IEEE80211_F_PUREG) { 426b032f27cSSam Leffler /* 427b032f27cSSam Leffler * Also mark OFDM rates basic so 11b 428b032f27cSSam Leffler * stations do not join (WiFi compliance). 429b032f27cSSam Leffler */ 430b032f27cSSam Leffler ieee80211_addbasicrates(&ni->ni_rates, 431b032f27cSSam Leffler IEEE80211_MODE_11A); 432b032f27cSSam Leffler } 43368e8e04eSSam Leffler } else if (IEEE80211_IS_CHAN_B(chan)) { 43468e8e04eSSam Leffler /* 43568e8e04eSSam Leffler * Force pure 11b rate set. 43668e8e04eSSam Leffler */ 437b032f27cSSam Leffler ieee80211_setbasicrates(&ni->ni_rates, 43868e8e04eSSam Leffler IEEE80211_MODE_11B); 43968e8e04eSSam Leffler } 4401a1e1d21SSam Leffler } 4411a1e1d21SSam Leffler 442869897d2SAdrian Chadd /* XXX TODO: other bits and pieces - eg fast-frames? */ 443869897d2SAdrian Chadd 444869897d2SAdrian Chadd /* If we're an 11n channel then initialise the 11n bits */ 44551172f62SAdrian Chadd if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) { 44651172f62SAdrian Chadd /* XXX what else? */ 44751172f62SAdrian Chadd ieee80211_ht_node_init(ni); 44851172f62SAdrian Chadd ieee80211_vht_node_init(ni); 44951172f62SAdrian Chadd } else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) { 450869897d2SAdrian Chadd /* XXX what else? */ 451869897d2SAdrian Chadd ieee80211_ht_node_init(ni); 452869897d2SAdrian Chadd } 453869897d2SAdrian Chadd 45468e8e04eSSam Leffler (void) ieee80211_sta_join1(ieee80211_ref_node(ni)); 45568e8e04eSSam Leffler } 45668e8e04eSSam Leffler 45768e8e04eSSam Leffler /* 45868e8e04eSSam Leffler * Reset bss state on transition to the INIT state. 45968e8e04eSSam Leffler * Clear any stations from the table (they have been 46068e8e04eSSam Leffler * deauth'd) and reset the bss node (clears key, rate 46168e8e04eSSam Leffler * etc. state). 46268e8e04eSSam Leffler */ 4638a1b9b6aSSam Leffler void 464b032f27cSSam Leffler ieee80211_reset_bss(struct ieee80211vap *vap) 465b4c5a90fSSam Leffler { 466b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 4678a1b9b6aSSam Leffler struct ieee80211_node *ni, *obss; 4688a1b9b6aSSam Leffler 46949619f73SBjoern A. Zeeb IEEE80211_LOCK_ASSERT(ic); 47049619f73SBjoern A. Zeeb 471b032f27cSSam Leffler ieee80211_node_table_reset(&ic->ic_sta, vap); 472b032f27cSSam Leffler /* XXX multi-bss: wrong */ 473d20ff6e6SAdrian Chadd ieee80211_vap_reset_erp(vap); 474acc4f7f5SSam Leffler 4753a11944bSBjoern A. Zeeb ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr, 4763a11944bSBjoern A. Zeeb __func__, __LINE__); 47748e1bda0SRui Paulo KASSERT(ni != NULL, ("unable to setup initial BSS node")); 47891b4225aSBjoern A. Zeeb obss = vap->iv_update_bss(vap, ieee80211_ref_node(ni)); 479f9cd9174SSam Leffler if (obss != NULL) { 480f9cd9174SSam Leffler copy_bss(ni, obss); 481d365f9c7SSam Leffler ni->ni_intval = ic->ic_bintval; 4828a1b9b6aSSam Leffler ieee80211_free_node(obss); 483b032f27cSSam Leffler } else 484b032f27cSSam Leffler IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr); 485f9cd9174SSam Leffler } 4868a1b9b6aSSam Leffler 4878a1b9b6aSSam Leffler static int 48868e8e04eSSam Leffler match_ssid(const struct ieee80211_node *ni, 48968e8e04eSSam Leffler int nssid, const struct ieee80211_scan_ssid ssids[]) 4908a1b9b6aSSam Leffler { 49168e8e04eSSam Leffler int i; 49268e8e04eSSam Leffler 49368e8e04eSSam Leffler for (i = 0; i < nssid; i++) { 49468e8e04eSSam Leffler if (ni->ni_esslen == ssids[i].len && 49568e8e04eSSam Leffler memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0) 49668e8e04eSSam Leffler return 1; 49768e8e04eSSam Leffler } 49868e8e04eSSam Leffler return 0; 49968e8e04eSSam Leffler } 50068e8e04eSSam Leffler 50168e8e04eSSam Leffler /* 50268e8e04eSSam Leffler * Test a node for suitability/compatibility. 50368e8e04eSSam Leffler */ 50468e8e04eSSam Leffler static int 505b032f27cSSam Leffler check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni) 50668e8e04eSSam Leffler { 507b032f27cSSam Leffler struct ieee80211com *ic = ni->ni_ic; 50868e8e04eSSam Leffler uint8_t rate; 50968e8e04eSSam Leffler 51068e8e04eSSam Leffler if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 51168e8e04eSSam Leffler return 0; 512b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_IBSS) { 51368e8e04eSSam Leffler if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 51468e8e04eSSam Leffler return 0; 51568e8e04eSSam Leffler } else { 51668e8e04eSSam Leffler if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 51768e8e04eSSam Leffler return 0; 51868e8e04eSSam Leffler } 519b032f27cSSam Leffler if (vap->iv_flags & IEEE80211_F_PRIVACY) { 52068e8e04eSSam Leffler if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 52168e8e04eSSam Leffler return 0; 52268e8e04eSSam Leffler } else { 52368e8e04eSSam Leffler /* XXX does this mean privacy is supported or required? */ 52468e8e04eSSam Leffler if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 52568e8e04eSSam Leffler return 0; 52668e8e04eSSam Leffler } 52768e8e04eSSam Leffler rate = ieee80211_fix_rate(ni, &ni->ni_rates, 52868e8e04eSSam Leffler IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 52968e8e04eSSam Leffler if (rate & IEEE80211_RATE_BASIC) 53068e8e04eSSam Leffler return 0; 531b032f27cSSam Leffler if (vap->iv_des_nssid != 0 && 532b032f27cSSam Leffler !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid)) 53368e8e04eSSam Leffler return 0; 534b032f27cSSam Leffler if ((vap->iv_flags & IEEE80211_F_DESBSSID) && 535b032f27cSSam Leffler !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid)) 53668e8e04eSSam Leffler return 0; 53768e8e04eSSam Leffler return 1; 53868e8e04eSSam Leffler } 53968e8e04eSSam Leffler 54068e8e04eSSam Leffler #ifdef IEEE80211_DEBUG 54168e8e04eSSam Leffler /* 54268e8e04eSSam Leffler * Display node suitability/compatibility. 54368e8e04eSSam Leffler */ 54468e8e04eSSam Leffler static void 545b032f27cSSam Leffler check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni) 54668e8e04eSSam Leffler { 547b032f27cSSam Leffler struct ieee80211com *ic = ni->ni_ic; 54868e8e04eSSam Leffler uint8_t rate; 549b4c5a90fSSam Leffler int fail; 550b4c5a90fSSam Leffler 551b4c5a90fSSam Leffler fail = 0; 552b4c5a90fSSam Leffler if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 553b4c5a90fSSam Leffler fail |= 0x01; 554b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_IBSS) { 555b4c5a90fSSam Leffler if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 556b4c5a90fSSam Leffler fail |= 0x02; 557b4c5a90fSSam Leffler } else { 558b4c5a90fSSam Leffler if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 559b4c5a90fSSam Leffler fail |= 0x02; 560b4c5a90fSSam Leffler } 561b032f27cSSam Leffler if (vap->iv_flags & IEEE80211_F_PRIVACY) { 562b4c5a90fSSam Leffler if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 563b4c5a90fSSam Leffler fail |= 0x04; 564b4c5a90fSSam Leffler } else { 565b4c5a90fSSam Leffler /* XXX does this mean privacy is supported or required? */ 566b4c5a90fSSam Leffler if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 567b4c5a90fSSam Leffler fail |= 0x04; 568b4c5a90fSSam Leffler } 56970e28b9aSSam Leffler rate = ieee80211_fix_rate(ni, &ni->ni_rates, 57079edaebfSSam Leffler IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 571b4c5a90fSSam Leffler if (rate & IEEE80211_RATE_BASIC) 572b4c5a90fSSam Leffler fail |= 0x08; 573b032f27cSSam Leffler if (vap->iv_des_nssid != 0 && 574b032f27cSSam Leffler !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid)) 575b4c5a90fSSam Leffler fail |= 0x10; 576b032f27cSSam Leffler if ((vap->iv_flags & IEEE80211_F_DESBSSID) && 577b032f27cSSam Leffler !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid)) 578b4c5a90fSSam Leffler fail |= 0x20; 57968e8e04eSSam Leffler 58068e8e04eSSam Leffler printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr)); 58168e8e04eSSam Leffler printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' '); 58268e8e04eSSam Leffler printf(" %3d%c", 58368e8e04eSSam Leffler ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' '); 584b4c5a90fSSam Leffler printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 585b4c5a90fSSam Leffler fail & 0x08 ? '!' : ' '); 586b4c5a90fSSam Leffler printf(" %4s%c", 587b4c5a90fSSam Leffler (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 588b4c5a90fSSam Leffler (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 589b4c5a90fSSam Leffler "????", 590b4c5a90fSSam Leffler fail & 0x02 ? '!' : ' '); 591b4c5a90fSSam Leffler printf(" %3s%c ", 59268e8e04eSSam Leffler (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no", 593b4c5a90fSSam Leffler fail & 0x04 ? '!' : ' '); 594b4c5a90fSSam Leffler ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 595b4c5a90fSSam Leffler printf("%s\n", fail & 0x10 ? "!" : ""); 596b4c5a90fSSam Leffler } 59768e8e04eSSam Leffler #endif /* IEEE80211_DEBUG */ 5988a1b9b6aSSam Leffler 599adad5b45SAdrian Chadd int 600adad5b45SAdrian Chadd ieee80211_ibss_merge_check(struct ieee80211_node *ni) 601adad5b45SAdrian Chadd { 602adad5b45SAdrian Chadd struct ieee80211vap *vap = ni->ni_vap; 603adad5b45SAdrian Chadd 604adad5b45SAdrian Chadd if (ni == vap->iv_bss || 605adad5b45SAdrian Chadd IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) { 606adad5b45SAdrian Chadd /* unchanged, nothing to do */ 607adad5b45SAdrian Chadd return 0; 608adad5b45SAdrian Chadd } 609adad5b45SAdrian Chadd 610adad5b45SAdrian Chadd if (!check_bss(vap, ni)) { 611adad5b45SAdrian Chadd /* capabilities mismatch */ 612adad5b45SAdrian Chadd IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC, 613adad5b45SAdrian Chadd "%s: merge failed, capabilities mismatch\n", __func__); 614adad5b45SAdrian Chadd #ifdef IEEE80211_DEBUG 615adad5b45SAdrian Chadd if (ieee80211_msg_assoc(vap)) 616adad5b45SAdrian Chadd check_bss_debug(vap, ni); 617adad5b45SAdrian Chadd #endif 618adad5b45SAdrian Chadd vap->iv_stats.is_ibss_capmismatch++; 619adad5b45SAdrian Chadd return 0; 620adad5b45SAdrian Chadd } 621adad5b45SAdrian Chadd 622adad5b45SAdrian Chadd return 1; 623adad5b45SAdrian Chadd } 624adad5b45SAdrian Chadd 625750d6d0cSSam Leffler /* 626172b009aSAdrian Chadd * Check if the given node should populate the node table. 627172b009aSAdrian Chadd * 628172b009aSAdrian Chadd * We need to be in "see all beacons for all ssids" mode in order 629172b009aSAdrian Chadd * to do IBSS merges, however this means we will populate nodes for 630172b009aSAdrian Chadd * /all/ IBSS SSIDs, versus just the one we care about. 631172b009aSAdrian Chadd * 632172b009aSAdrian Chadd * So this check ensures the node can actually belong to our IBSS 633172b009aSAdrian Chadd * configuration. For now it simply checks the SSID. 634172b009aSAdrian Chadd */ 635172b009aSAdrian Chadd int 636172b009aSAdrian Chadd ieee80211_ibss_node_check_new(struct ieee80211_node *ni, 637172b009aSAdrian Chadd const struct ieee80211_scanparams *scan) 638172b009aSAdrian Chadd { 639172b009aSAdrian Chadd struct ieee80211vap *vap = ni->ni_vap; 640172b009aSAdrian Chadd int i; 641172b009aSAdrian Chadd 642172b009aSAdrian Chadd /* 643172b009aSAdrian Chadd * If we have no SSID and no scan SSID, return OK. 644172b009aSAdrian Chadd */ 645172b009aSAdrian Chadd if (vap->iv_des_nssid == 0 && scan->ssid == NULL) 646172b009aSAdrian Chadd goto ok; 647172b009aSAdrian Chadd 648172b009aSAdrian Chadd /* 649172b009aSAdrian Chadd * If we have one of (SSID, scan SSID) then return error. 650172b009aSAdrian Chadd */ 651172b009aSAdrian Chadd if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL)) 652172b009aSAdrian Chadd goto mismatch; 653172b009aSAdrian Chadd 654172b009aSAdrian Chadd /* 655172b009aSAdrian Chadd * Double-check - we need scan SSID. 656172b009aSAdrian Chadd */ 657172b009aSAdrian Chadd if (scan->ssid == NULL) 658172b009aSAdrian Chadd goto mismatch; 659172b009aSAdrian Chadd 660172b009aSAdrian Chadd /* 661172b009aSAdrian Chadd * Check if the scan SSID matches the SSID list for the VAP. 662172b009aSAdrian Chadd */ 663172b009aSAdrian Chadd for (i = 0; i < vap->iv_des_nssid; i++) { 664172b009aSAdrian Chadd /* Sanity length check */ 665172b009aSAdrian Chadd if (vap->iv_des_ssid[i].len != scan->ssid[1]) 666172b009aSAdrian Chadd continue; 667172b009aSAdrian Chadd 668172b009aSAdrian Chadd /* Note: SSID in the scan entry is the IE format */ 669172b009aSAdrian Chadd if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2, 670172b009aSAdrian Chadd vap->iv_des_ssid[i].len) == 0) 671172b009aSAdrian Chadd goto ok; 672172b009aSAdrian Chadd } 673172b009aSAdrian Chadd 674172b009aSAdrian Chadd mismatch: 675172b009aSAdrian Chadd return (0); 676172b009aSAdrian Chadd ok: 677172b009aSAdrian Chadd return (1); 678172b009aSAdrian Chadd } 679172b009aSAdrian Chadd 680172b009aSAdrian Chadd /* 6818a1b9b6aSSam Leffler * Handle 802.11 ad hoc network merge. The 6828a1b9b6aSSam Leffler * convention, set by the Wireless Ethernet Compatibility Alliance 6838a1b9b6aSSam Leffler * (WECA), is that an 802.11 station will change its BSSID to match 6848a1b9b6aSSam Leffler * the "oldest" 802.11 ad hoc network, on the same channel, that 6858a1b9b6aSSam Leffler * has the station's desired SSID. The "oldest" 802.11 network 6868a1b9b6aSSam Leffler * sends beacons with the greatest TSF timestamp. 6878a1b9b6aSSam Leffler * 6888a1b9b6aSSam Leffler * The caller is assumed to validate TSF's before attempting a merge. 6898a1b9b6aSSam Leffler * 6908a1b9b6aSSam Leffler * Return !0 if the BSSID changed, 0 otherwise. 691750d6d0cSSam Leffler */ 6928a1b9b6aSSam Leffler int 693641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni) 6948a1b9b6aSSam Leffler { 695b032f27cSSam Leffler #ifdef IEEE80211_DEBUG 696c89e0d15SBjoern A. Zeeb struct ieee80211vap *vap = ni->ni_vap; 697b032f27cSSam Leffler #endif 6988a1b9b6aSSam Leffler 699adad5b45SAdrian Chadd if (! ieee80211_ibss_merge_check(ni)) 7008a1b9b6aSSam Leffler return 0; 701adad5b45SAdrian Chadd 702b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC, 7038a1b9b6aSSam Leffler "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__, 7048a1b9b6aSSam Leffler ether_sprintf(ni->ni_bssid), 705f1481c8dSAdrian Chadd vap->iv_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 706d20ff6e6SAdrian Chadd vap->iv_flags&IEEE80211_F_SHSLOT ? "short" : "long", 707f1481c8dSAdrian Chadd vap->iv_flags&IEEE80211_F_USEPROT ? ", protection" : "" 7088a1b9b6aSSam Leffler ); 70968e8e04eSSam Leffler return ieee80211_sta_join1(ieee80211_ref_node(ni)); 7108a1b9b6aSSam Leffler } 7118a1b9b6aSSam Leffler 7128a1b9b6aSSam Leffler /* 713b032f27cSSam Leffler * Calculate HT channel promotion flags for all vaps. 714b032f27cSSam Leffler * This assumes ni_chan have been setup for each vap. 715b032f27cSSam Leffler */ 716b032f27cSSam Leffler static int 717b032f27cSSam Leffler gethtadjustflags(struct ieee80211com *ic) 718b032f27cSSam Leffler { 719b032f27cSSam Leffler struct ieee80211vap *vap; 720b032f27cSSam Leffler int flags; 721b032f27cSSam Leffler 722b032f27cSSam Leffler flags = 0; 723b032f27cSSam Leffler /* XXX locking */ 724b032f27cSSam Leffler TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 725b032f27cSSam Leffler if (vap->iv_state < IEEE80211_S_RUN) 726b032f27cSSam Leffler continue; 727b032f27cSSam Leffler switch (vap->iv_opmode) { 728b032f27cSSam Leffler case IEEE80211_M_WDS: 729b032f27cSSam Leffler case IEEE80211_M_STA: 730b032f27cSSam Leffler case IEEE80211_M_AHDEMO: 731b032f27cSSam Leffler case IEEE80211_M_HOSTAP: 732b032f27cSSam Leffler case IEEE80211_M_IBSS: 73359aa14a9SRui Paulo case IEEE80211_M_MBSS: 734b032f27cSSam Leffler flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan); 735b032f27cSSam Leffler break; 736b032f27cSSam Leffler default: 737b032f27cSSam Leffler break; 738b032f27cSSam Leffler } 739b032f27cSSam Leffler } 740b032f27cSSam Leffler return flags; 741b032f27cSSam Leffler } 742b032f27cSSam Leffler 743b032f27cSSam Leffler /* 74451172f62SAdrian Chadd * Calculate VHT channel promotion flags for all vaps. 74551172f62SAdrian Chadd * This assumes ni_chan have been setup for each vap. 74651172f62SAdrian Chadd */ 74751172f62SAdrian Chadd static int 74851172f62SAdrian Chadd getvhtadjustflags(struct ieee80211com *ic) 74951172f62SAdrian Chadd { 75051172f62SAdrian Chadd struct ieee80211vap *vap; 75151172f62SAdrian Chadd int flags; 75251172f62SAdrian Chadd 75351172f62SAdrian Chadd flags = 0; 75451172f62SAdrian Chadd /* XXX locking */ 75551172f62SAdrian Chadd TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 75651172f62SAdrian Chadd if (vap->iv_state < IEEE80211_S_RUN) 75751172f62SAdrian Chadd continue; 75851172f62SAdrian Chadd switch (vap->iv_opmode) { 75951172f62SAdrian Chadd case IEEE80211_M_WDS: 76051172f62SAdrian Chadd case IEEE80211_M_STA: 76151172f62SAdrian Chadd case IEEE80211_M_AHDEMO: 76251172f62SAdrian Chadd case IEEE80211_M_HOSTAP: 76351172f62SAdrian Chadd case IEEE80211_M_IBSS: 76451172f62SAdrian Chadd case IEEE80211_M_MBSS: 76551172f62SAdrian Chadd flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan); 76651172f62SAdrian Chadd break; 76751172f62SAdrian Chadd default: 76851172f62SAdrian Chadd break; 76951172f62SAdrian Chadd } 77051172f62SAdrian Chadd } 77151172f62SAdrian Chadd return flags; 77251172f62SAdrian Chadd } 77351172f62SAdrian Chadd 77451172f62SAdrian Chadd /* 775b032f27cSSam Leffler * Check if the current channel needs to change based on whether 7765d44f8c0SSam Leffler * any vap's are using HT20/HT40. This is used to sync the state 7775d44f8c0SSam Leffler * of ic_curchan after a channel width change on a running vap. 77851172f62SAdrian Chadd * 77951172f62SAdrian Chadd * Same applies for VHT. 7801b6167d2SSam Leffler */ 7811b6167d2SSam Leffler void 782b032f27cSSam Leffler ieee80211_sync_curchan(struct ieee80211com *ic) 7831b6167d2SSam Leffler { 784b032f27cSSam Leffler struct ieee80211_channel *c; 785b032f27cSSam Leffler 786b032f27cSSam Leffler c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic)); 78751172f62SAdrian Chadd c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic)); 78851172f62SAdrian Chadd 789b032f27cSSam Leffler if (c != ic->ic_curchan) { 790b032f27cSSam Leffler ic->ic_curchan = c; 791b032f27cSSam Leffler ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan); 79226d39e2cSSam Leffler ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan); 7935efea30fSAndrew Thompson IEEE80211_UNLOCK(ic); 794b032f27cSSam Leffler ic->ic_set_channel(ic); 7955463c4a4SSam Leffler ieee80211_radiotap_chan_change(ic); 7965efea30fSAndrew Thompson IEEE80211_LOCK(ic); 797b032f27cSSam Leffler } 798b032f27cSSam Leffler } 799b032f27cSSam Leffler 800b032f27cSSam Leffler /* 8015efea30fSAndrew Thompson * Setup the current channel. The request channel may be 802b032f27cSSam Leffler * promoted if other vap's are operating with HT20/HT40. 803b032f27cSSam Leffler */ 804b032f27cSSam Leffler void 8055efea30fSAndrew Thompson ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c) 806b032f27cSSam Leffler { 807b032f27cSSam Leffler if (ic->ic_htcaps & IEEE80211_HTC_HT) { 808b032f27cSSam Leffler int flags = gethtadjustflags(ic); 809b032f27cSSam Leffler /* 810b032f27cSSam Leffler * Check for channel promotion required to support the 811b032f27cSSam Leffler * set of running vap's. This assumes we are called 812b032f27cSSam Leffler * after ni_chan is setup for each vap. 813b032f27cSSam Leffler */ 81451172f62SAdrian Chadd /* XXX VHT? */ 8152bfc8a91SSam Leffler /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */ 816b032f27cSSam Leffler if (flags > ieee80211_htchanflags(c)) 817b032f27cSSam Leffler c = ieee80211_ht_adjust_channel(ic, c, flags); 818b032f27cSSam Leffler } 81951172f62SAdrian Chadd 82051172f62SAdrian Chadd /* 82151172f62SAdrian Chadd * VHT promotion - this will at least promote to VHT20/40 82251172f62SAdrian Chadd * based on what HT has done; it may further promote the 82351172f62SAdrian Chadd * channel to VHT80 or above. 82451172f62SAdrian Chadd */ 825562adbe1SBjoern A. Zeeb if (ic->ic_vht_cap.vht_cap_info != 0) { 82651172f62SAdrian Chadd int flags = getvhtadjustflags(ic); 82751172f62SAdrian Chadd if (flags > ieee80211_vhtchanflags(c)) 82851172f62SAdrian Chadd c = ieee80211_vht_adjust_channel(ic, c, flags); 82951172f62SAdrian Chadd } 83051172f62SAdrian Chadd 831b032f27cSSam Leffler ic->ic_bsschan = ic->ic_curchan = c; 8321b6167d2SSam Leffler ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan); 83326d39e2cSSam Leffler ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan); 8345efea30fSAndrew Thompson } 8355efea30fSAndrew Thompson 8365efea30fSAndrew Thompson /* 8375efea30fSAndrew Thompson * Change the current channel. The channel change is guaranteed to have 8385efea30fSAndrew Thompson * happened before the next state change. 8395efea30fSAndrew Thompson */ 8405efea30fSAndrew Thompson void 8415efea30fSAndrew Thompson ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c) 8425efea30fSAndrew Thompson { 8435efea30fSAndrew Thompson ieee80211_setupcurchan(ic, c); 8445efea30fSAndrew Thompson ieee80211_runtask(ic, &ic->ic_chan_task); 8451b6167d2SSam Leffler } 8461b6167d2SSam Leffler 847b94299c4SAdrian Chadd void 848b94299c4SAdrian Chadd ieee80211_update_chw(struct ieee80211com *ic) 849b94299c4SAdrian Chadd { 850b94299c4SAdrian Chadd 851b94299c4SAdrian Chadd ieee80211_setupcurchan(ic, ic->ic_curchan); 852b94299c4SAdrian Chadd ieee80211_runtask(ic, &ic->ic_chw_task); 853b94299c4SAdrian Chadd } 854b94299c4SAdrian Chadd 8551b6167d2SSam Leffler /* 8568a1b9b6aSSam Leffler * Join the specified IBSS/BSS network. The node is assumed to 8578a1b9b6aSSam Leffler * be passed in with a held reference. 8588a1b9b6aSSam Leffler */ 85968e8e04eSSam Leffler static int 86068e8e04eSSam Leffler ieee80211_sta_join1(struct ieee80211_node *selbs) 8618a1b9b6aSSam Leffler { 862b032f27cSSam Leffler struct ieee80211vap *vap = selbs->ni_vap; 86368e8e04eSSam Leffler struct ieee80211com *ic = selbs->ni_ic; 8648a1b9b6aSSam Leffler struct ieee80211_node *obss; 86568e8e04eSSam Leffler int canreassoc; 8668a1b9b6aSSam Leffler 8678a1b9b6aSSam Leffler /* 8688a1b9b6aSSam Leffler * Committed to selbs, setup state. 8698a1b9b6aSSam Leffler */ 87049619f73SBjoern A. Zeeb IEEE80211_LOCK(ic); /* XXX may recurse here, check callers. */ 87191b4225aSBjoern A. Zeeb obss = vap->iv_update_bss(vap, selbs); /* NB: caller assumed to bump refcnt */ 87249619f73SBjoern A. Zeeb IEEE80211_UNLOCK(ic); 87368e8e04eSSam Leffler /* 87468e8e04eSSam Leffler * Check if old+new node have the same address in which 87568e8e04eSSam Leffler * case we can reassociate when operating in sta mode. 87668e8e04eSSam Leffler */ 877453d1a90SBjoern A. Zeeb /* XXX We'll not be in RUN anymore as iv_state got updated already? */ 87868e8e04eSSam Leffler canreassoc = (obss != NULL && 879b032f27cSSam Leffler vap->iv_state == IEEE80211_S_RUN && 88068e8e04eSSam Leffler IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr)); 8811fd2349dSSam Leffler if (obss != NULL) { 8828a055831SAdrian Chadd struct ieee80211_node_table *nt = obss->ni_table; 8838a055831SAdrian Chadd 8841fd2349dSSam Leffler copy_bss(selbs, obss); 8858a5a3e3dSBjoern A. Zeeb if (nt != NULL) { 88647a7b0faSSam Leffler ieee80211_node_decref(obss); /* iv_bss reference */ 8878a055831SAdrian Chadd IEEE80211_NODE_LOCK(nt); 8888a055831SAdrian Chadd node_reclaim(nt, obss); /* station table reference */ 8898a055831SAdrian Chadd IEEE80211_NODE_UNLOCK(nt); 8908a5a3e3dSBjoern A. Zeeb } else { 8918a5a3e3dSBjoern A. Zeeb ieee80211_free_node(obss); /* iv_bss reference */ 8928a5a3e3dSBjoern A. Zeeb } 8938a055831SAdrian Chadd 894b032f27cSSam Leffler obss = NULL; /* NB: guard against later use */ 8951fd2349dSSam Leffler } 89679edaebfSSam Leffler 89779edaebfSSam Leffler /* 89879edaebfSSam Leffler * Delete unusable rates; we've already checked 89979edaebfSSam Leffler * that the negotiated rate set is acceptable. 90079edaebfSSam Leffler */ 901b032f27cSSam Leffler ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates, 90270e28b9aSSam Leffler IEEE80211_F_DODEL | IEEE80211_F_JOIN); 90379edaebfSSam Leffler 904b032f27cSSam Leffler ieee80211_setcurchan(ic, selbs->ni_chan); 9058a1b9b6aSSam Leffler /* 9068a1b9b6aSSam Leffler * Set the erp state (mostly the slot time) to deal with 9078a1b9b6aSSam Leffler * the auto-select case; this should be redundant if the 9088a1b9b6aSSam Leffler * mode is locked. 9098a1b9b6aSSam Leffler */ 910d20ff6e6SAdrian Chadd ieee80211_vap_reset_erp(vap); 911b032f27cSSam Leffler ieee80211_wme_initparams(vap); 912acc4f7f5SSam Leffler 913b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_STA) { 91468e8e04eSSam Leffler if (canreassoc) { 91568e8e04eSSam Leffler /* Reassociate */ 916b032f27cSSam Leffler ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1); 91768e8e04eSSam Leffler } else { 91868e8e04eSSam Leffler /* 91968e8e04eSSam Leffler * Act as if we received a DEAUTH frame in case we 92068e8e04eSSam Leffler * are invoked from the RUN state. This will cause 92168e8e04eSSam Leffler * us to try to re-authenticate if we are operating 92268e8e04eSSam Leffler * as a station. 92368e8e04eSSam Leffler */ 92404efa18fSBjoern A. Zeeb IEEE80211_DPRINTF(vap, IEEE80211_MSG_AUTH, 925453d1a90SBjoern A. Zeeb "%s %p<%s> %s -> AUTH, FC0_SUBTYPE_DEAUTH\n", 926453d1a90SBjoern A. Zeeb __func__, selbs, ether_sprintf(selbs->ni_macaddr), 927453d1a90SBjoern A. Zeeb ieee80211_state_name[vap->iv_state]); 928b032f27cSSam Leffler ieee80211_new_state(vap, IEEE80211_S_AUTH, 92968e8e04eSSam Leffler IEEE80211_FC0_SUBTYPE_DEAUTH); 93068e8e04eSSam Leffler } 93168e8e04eSSam Leffler } else 932b032f27cSSam Leffler ieee80211_new_state(vap, IEEE80211_S_RUN, -1); 9338a1b9b6aSSam Leffler return 1; 9348a1b9b6aSSam Leffler } 9358a1b9b6aSSam Leffler 93668e8e04eSSam Leffler int 93710959256SSam Leffler ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan, 93868e8e04eSSam Leffler const struct ieee80211_scan_entry *se) 93968e8e04eSSam Leffler { 940b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 94168e8e04eSSam Leffler struct ieee80211_node *ni; 94251172f62SAdrian Chadd int do_ht = 0; 94368e8e04eSSam Leffler 9443a11944bSBjoern A. Zeeb ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr, 9453a11944bSBjoern A. Zeeb __func__, __LINE__); 94668e8e04eSSam Leffler if (ni == NULL) { 94768e8e04eSSam Leffler /* XXX msg */ 94868e8e04eSSam Leffler return 0; 94968e8e04eSSam Leffler } 950d71a1f7aSAdrian Chadd 95168e8e04eSSam Leffler /* 95268e8e04eSSam Leffler * Expand scan state into node's format. 95368e8e04eSSam Leffler * XXX may not need all this stuff 95468e8e04eSSam Leffler */ 95568e8e04eSSam Leffler IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid); 95668e8e04eSSam Leffler ni->ni_esslen = se->se_ssid[1]; 95768e8e04eSSam Leffler memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen); 95868e8e04eSSam Leffler ni->ni_tstamp.tsf = se->se_tstamp.tsf; 95968e8e04eSSam Leffler ni->ni_intval = se->se_intval; 96068e8e04eSSam Leffler ni->ni_capinfo = se->se_capinfo; 96110959256SSam Leffler ni->ni_chan = chan; 96268e8e04eSSam Leffler ni->ni_timoff = se->se_timoff; 96368e8e04eSSam Leffler ni->ni_fhdwell = se->se_fhdwell; 96468e8e04eSSam Leffler ni->ni_fhindex = se->se_fhindex; 96568e8e04eSSam Leffler ni->ni_erp = se->se_erp; 966b032f27cSSam Leffler IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi); 96768e8e04eSSam Leffler ni->ni_noise = se->se_noise; 9686ca74c40SSam Leffler if (vap->iv_opmode == IEEE80211_M_STA) { 9696ca74c40SSam Leffler /* NB: only infrastructure mode requires an associd */ 9701b999d64SSam Leffler ni->ni_flags |= IEEE80211_NODE_ASSOCID; 9716ca74c40SSam Leffler } 97268e8e04eSSam Leffler 973b032f27cSSam Leffler if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) { 974b032f27cSSam Leffler ieee80211_ies_expand(&ni->ni_ies); 975616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 976b032f27cSSam Leffler if (ni->ni_ies.ath_ie != NULL) 977b032f27cSSam Leffler ieee80211_parse_ath(ni, ni->ni_ies.ath_ie); 978616190d0SSam Leffler #endif 979b032f27cSSam Leffler if (ni->ni_ies.htcap_ie != NULL) 980b032f27cSSam Leffler ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie); 981b032f27cSSam Leffler if (ni->ni_ies.htinfo_ie != NULL) 982b032f27cSSam Leffler ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie); 98359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH 98459aa14a9SRui Paulo if (ni->ni_ies.meshid_ie != NULL) 98559aa14a9SRui Paulo ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie); 98659aa14a9SRui Paulo #endif 98710ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA 98810ad9a77SSam Leffler if (ni->ni_ies.tdma_ie != NULL) 98910ad9a77SSam Leffler ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie); 99010ad9a77SSam Leffler #endif 99151172f62SAdrian Chadd if (ni->ni_ies.vhtcap_ie != NULL) 99251172f62SAdrian Chadd ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie); 99351172f62SAdrian Chadd if (ni->ni_ies.vhtopmode_ie != NULL) 99451172f62SAdrian Chadd ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie); 99574a54be9SAdrian Chadd 99674a54be9SAdrian Chadd /* XXX parse BSSLOAD IE */ 99751172f62SAdrian Chadd /* XXX parse TXPWRENV IE */ 99874a54be9SAdrian Chadd /* XXX parse APCHANREP IE */ 999b032f27cSSam Leffler } 1000b032f27cSSam Leffler 1001b032f27cSSam Leffler vap->iv_dtim_period = se->se_dtimperiod; 1002b032f27cSSam Leffler vap->iv_dtim_count = 0; 100368e8e04eSSam Leffler 100468e8e04eSSam Leffler /* NB: must be after ni_chan is setup */ 100568e8e04eSSam Leffler ieee80211_setup_rates(ni, se->se_rates, se->se_xrates, 100668e8e04eSSam Leffler IEEE80211_F_DOSORT); 1007dfcd1f4dSSam Leffler if (ieee80211_iserp_rateset(&ni->ni_rates)) 1008dfcd1f4dSSam Leffler ni->ni_flags |= IEEE80211_NODE_ERP; 1009d71a1f7aSAdrian Chadd 1010d71a1f7aSAdrian Chadd /* 1011d71a1f7aSAdrian Chadd * Setup HT state for this node if it's available, otherwise 1012d71a1f7aSAdrian Chadd * non-STA modes won't pick this state up. 1013d71a1f7aSAdrian Chadd * 1014d71a1f7aSAdrian Chadd * For IBSS and related modes that don't go through an 1015d71a1f7aSAdrian Chadd * association request/response, the only appropriate place 1016d71a1f7aSAdrian Chadd * to setup the HT state is here. 1017d71a1f7aSAdrian Chadd */ 1018d71a1f7aSAdrian Chadd if (ni->ni_ies.htinfo_ie != NULL && 1019d71a1f7aSAdrian Chadd ni->ni_ies.htcap_ie != NULL && 1020d71a1f7aSAdrian Chadd vap->iv_flags_ht & IEEE80211_FHT_HT) { 1021d71a1f7aSAdrian Chadd ieee80211_ht_node_init(ni); 1022d71a1f7aSAdrian Chadd ieee80211_ht_updateparams(ni, 1023d71a1f7aSAdrian Chadd ni->ni_ies.htcap_ie, 1024d71a1f7aSAdrian Chadd ni->ni_ies.htinfo_ie); 102551172f62SAdrian Chadd do_ht = 1; 102651172f62SAdrian Chadd } 102751172f62SAdrian Chadd 102851172f62SAdrian Chadd /* 102951172f62SAdrian Chadd * Setup VHT state for this node if it's available. 103051172f62SAdrian Chadd * Same as the above. 103151172f62SAdrian Chadd * 103251172f62SAdrian Chadd * For now, don't allow 2GHz VHT operation. 103351172f62SAdrian Chadd */ 103451172f62SAdrian Chadd if (ni->ni_ies.vhtopmode_ie != NULL && 103551172f62SAdrian Chadd ni->ni_ies.vhtcap_ie != NULL && 1036ef48d4faSBjoern A. Zeeb vap->iv_vht_flags & IEEE80211_FVHT_VHT) { 103751172f62SAdrian Chadd if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) { 103851172f62SAdrian Chadd printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n", 103951172f62SAdrian Chadd __func__, 104051172f62SAdrian Chadd ni->ni_macaddr, 104151172f62SAdrian Chadd ":"); 104251172f62SAdrian Chadd } else { 104351172f62SAdrian Chadd ieee80211_vht_node_init(ni); 104451172f62SAdrian Chadd ieee80211_vht_updateparams(ni, 104551172f62SAdrian Chadd ni->ni_ies.vhtcap_ie, 104651172f62SAdrian Chadd ni->ni_ies.vhtopmode_ie); 104751172f62SAdrian Chadd ieee80211_setup_vht_rates(ni, ni->ni_ies.vhtcap_ie, 104851172f62SAdrian Chadd ni->ni_ies.vhtopmode_ie); 104951172f62SAdrian Chadd do_ht = 1; 105051172f62SAdrian Chadd } 105151172f62SAdrian Chadd } 105251172f62SAdrian Chadd 105351172f62SAdrian Chadd /* Finally do the node channel change */ 105451172f62SAdrian Chadd if (do_ht) { 105551172f62SAdrian Chadd ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie, 105651172f62SAdrian Chadd ni->ni_ies.htinfo_ie); 1057d71a1f7aSAdrian Chadd ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie, 1058d71a1f7aSAdrian Chadd IEEE80211_F_JOIN | IEEE80211_F_DOBRS); 1059d71a1f7aSAdrian Chadd ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie); 1060d71a1f7aSAdrian Chadd } 106151172f62SAdrian Chadd 1062d71a1f7aSAdrian Chadd /* XXX else check for ath FF? */ 1063d71a1f7aSAdrian Chadd /* XXX QoS? Difficult given that WME config is specific to a master */ 1064d71a1f7aSAdrian Chadd 1065d77148fbSSam Leffler ieee80211_node_setuptxparms(ni); 106649d2c137SBernhard Schmidt ieee80211_ratectl_node_init(ni); 106768e8e04eSSam Leffler 106868e8e04eSSam Leffler return ieee80211_sta_join1(ieee80211_ref_node(ni)); 106968e8e04eSSam Leffler } 107068e8e04eSSam Leffler 10718a1b9b6aSSam Leffler /* 10728a1b9b6aSSam Leffler * Leave the specified IBSS/BSS network. The node is assumed to 10738a1b9b6aSSam Leffler * be passed in with a held reference. 10748a1b9b6aSSam Leffler */ 10758a1b9b6aSSam Leffler void 1076b032f27cSSam Leffler ieee80211_sta_leave(struct ieee80211_node *ni) 10778a1b9b6aSSam Leffler { 1078b032f27cSSam Leffler struct ieee80211com *ic = ni->ni_ic; 1079b032f27cSSam Leffler 10808a1b9b6aSSam Leffler ic->ic_node_cleanup(ni); 1081b032f27cSSam Leffler ieee80211_notify_node_leave(ni); 1082b032f27cSSam Leffler } 1083b032f27cSSam Leffler 1084b032f27cSSam Leffler /* 1085b032f27cSSam Leffler * Send a deauthenticate frame and drop the station. 1086b032f27cSSam Leffler */ 1087b032f27cSSam Leffler void 1088b032f27cSSam Leffler ieee80211_node_deauth(struct ieee80211_node *ni, int reason) 1089b032f27cSSam Leffler { 1090eca3b4fcSAdrian Chadd /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */ 1091b032f27cSSam Leffler ieee80211_ref_node(ni); 1092b032f27cSSam Leffler if (ni->ni_associd != 0) 1093b032f27cSSam Leffler IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason); 1094b032f27cSSam Leffler ieee80211_node_leave(ni); 1095b032f27cSSam Leffler ieee80211_free_node(ni); 10968a1b9b6aSSam Leffler } 10978a1b9b6aSSam Leffler 10981a1e1d21SSam Leffler static struct ieee80211_node * 109938c208f8SSam Leffler node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 11001a1e1d21SSam Leffler { 1101410ca74bSSam Leffler struct ieee80211_node *ni; 11028a1b9b6aSSam Leffler 1103b9b53389SAdrian Chadd ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node), 1104b9b53389SAdrian Chadd M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 1105410ca74bSSam Leffler return ni; 11061a1e1d21SSam Leffler } 11071a1e1d21SSam Leffler 1108ea3d5fd9SAdrian Chadd static int 1109ea3d5fd9SAdrian Chadd node_init(struct ieee80211_node *ni) 1110ea3d5fd9SAdrian Chadd { 1111ea3d5fd9SAdrian Chadd return 0; 1112ea3d5fd9SAdrian Chadd } 1113ea3d5fd9SAdrian Chadd 11148a1b9b6aSSam Leffler /* 1115b032f27cSSam Leffler * Initialize an ie blob with the specified data. If previous 1116b032f27cSSam Leffler * data exists re-use the data block. As a side effect we clear 1117b032f27cSSam Leffler * all references to specific ie's; the caller is required to 1118b032f27cSSam Leffler * recalculate them. 1119b032f27cSSam Leffler */ 1120b032f27cSSam Leffler int 1121b032f27cSSam Leffler ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len) 1122b032f27cSSam Leffler { 1123b032f27cSSam Leffler /* NB: assumes data+len are the last fields */ 1124b032f27cSSam Leffler memset(ies, 0, offsetof(struct ieee80211_ies, data)); 1125b032f27cSSam Leffler if (ies->data != NULL && ies->len != len) { 1126b032f27cSSam Leffler /* data size changed */ 1127b9b53389SAdrian Chadd IEEE80211_FREE(ies->data, M_80211_NODE_IE); 1128b032f27cSSam Leffler ies->data = NULL; 1129b032f27cSSam Leffler } 1130b032f27cSSam Leffler if (ies->data == NULL) { 1131b9b53389SAdrian Chadd ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE, 1132b9b53389SAdrian Chadd IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 1133b032f27cSSam Leffler if (ies->data == NULL) { 1134b032f27cSSam Leffler ies->len = 0; 1135b032f27cSSam Leffler /* NB: pointers have already been zero'd above */ 1136b032f27cSSam Leffler return 0; 1137b032f27cSSam Leffler } 1138b032f27cSSam Leffler } 1139b032f27cSSam Leffler memcpy(ies->data, data, len); 1140b032f27cSSam Leffler ies->len = len; 1141b032f27cSSam Leffler return 1; 1142b032f27cSSam Leffler } 1143b032f27cSSam Leffler 1144b032f27cSSam Leffler /* 1145b032f27cSSam Leffler * Reclaim storage for an ie blob. 1146b032f27cSSam Leffler */ 1147b032f27cSSam Leffler void 1148b032f27cSSam Leffler ieee80211_ies_cleanup(struct ieee80211_ies *ies) 1149b032f27cSSam Leffler { 1150b032f27cSSam Leffler if (ies->data != NULL) 1151b9b53389SAdrian Chadd IEEE80211_FREE(ies->data, M_80211_NODE_IE); 1152b032f27cSSam Leffler } 1153b032f27cSSam Leffler 1154b032f27cSSam Leffler /* 1155b032f27cSSam Leffler * Expand an ie blob data contents and to fillin individual 1156b032f27cSSam Leffler * ie pointers. The data blob is assumed to be well-formed; 1157b032f27cSSam Leffler * we don't do any validity checking of ie lengths. 1158b032f27cSSam Leffler */ 1159b032f27cSSam Leffler void 1160b032f27cSSam Leffler ieee80211_ies_expand(struct ieee80211_ies *ies) 1161b032f27cSSam Leffler { 1162b032f27cSSam Leffler uint8_t *ie; 1163b032f27cSSam Leffler int ielen; 1164b032f27cSSam Leffler 1165b032f27cSSam Leffler ie = ies->data; 1166b032f27cSSam Leffler ielen = ies->len; 116709dd08f1SBjoern A. Zeeb while (ielen > 1) { 11689d2ba518SBjoern A. Zeeb /* Make sure the given IE length fits into the total length. */ 11699d2ba518SBjoern A. Zeeb if ((2 + ie[1]) > ielen) { 11709d2ba518SBjoern A. Zeeb printf("%s: malformed IEs! ies %p { data %p len %d }: " 11719d2ba518SBjoern A. Zeeb "ie %u len 2+%u > total len left %d\n", 11729d2ba518SBjoern A. Zeeb __func__, ies, ies->data, ies->len, 11739d2ba518SBjoern A. Zeeb ie[0], ie[1], ielen); 11749d2ba518SBjoern A. Zeeb return; 11759d2ba518SBjoern A. Zeeb } 1176b032f27cSSam Leffler switch (ie[0]) { 1177b032f27cSSam Leffler case IEEE80211_ELEMID_VENDOR: 1178b032f27cSSam Leffler if (iswpaoui(ie)) 1179b032f27cSSam Leffler ies->wpa_ie = ie; 1180b032f27cSSam Leffler else if (iswmeoui(ie)) 1181b032f27cSSam Leffler ies->wme_ie = ie; 1182616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 1183b032f27cSSam Leffler else if (isatherosoui(ie)) 1184b032f27cSSam Leffler ies->ath_ie = ie; 1185616190d0SSam Leffler #endif 118610ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA 118710ad9a77SSam Leffler else if (istdmaoui(ie)) 118810ad9a77SSam Leffler ies->tdma_ie = ie; 118910ad9a77SSam Leffler #endif 1190b032f27cSSam Leffler break; 1191b032f27cSSam Leffler case IEEE80211_ELEMID_RSN: 1192b032f27cSSam Leffler ies->rsn_ie = ie; 1193b032f27cSSam Leffler break; 1194b032f27cSSam Leffler case IEEE80211_ELEMID_HTCAP: 1195b032f27cSSam Leffler ies->htcap_ie = ie; 1196b032f27cSSam Leffler break; 1197d71a1f7aSAdrian Chadd case IEEE80211_ELEMID_HTINFO: 1198d71a1f7aSAdrian Chadd ies->htinfo_ie = ie; 1199d71a1f7aSAdrian Chadd break; 120059aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH 120159aa14a9SRui Paulo case IEEE80211_ELEMID_MESHID: 120259aa14a9SRui Paulo ies->meshid_ie = ie; 120359aa14a9SRui Paulo break; 120459aa14a9SRui Paulo #endif 120574a54be9SAdrian Chadd case IEEE80211_ELEMID_VHT_CAP: 120674a54be9SAdrian Chadd ies->vhtcap_ie = ie; 120774a54be9SAdrian Chadd break; 120874a54be9SAdrian Chadd case IEEE80211_ELEMID_VHT_OPMODE: 120974a54be9SAdrian Chadd ies->vhtopmode_ie = ie; 121074a54be9SAdrian Chadd break; 121174a54be9SAdrian Chadd case IEEE80211_ELEMID_VHT_PWR_ENV: 121274a54be9SAdrian Chadd ies->vhtpwrenv_ie = ie; 121374a54be9SAdrian Chadd break; 121474a54be9SAdrian Chadd case IEEE80211_ELEMID_BSSLOAD: 121574a54be9SAdrian Chadd ies->bssload_ie = ie; 121674a54be9SAdrian Chadd break; 121774a54be9SAdrian Chadd case IEEE80211_ELEMID_APCHANREP: 121874a54be9SAdrian Chadd ies->apchanrep_ie = ie; 121974a54be9SAdrian Chadd break; 1220b032f27cSSam Leffler } 1221b032f27cSSam Leffler ielen -= 2 + ie[1]; 1222b032f27cSSam Leffler ie += 2 + ie[1]; 1223b032f27cSSam Leffler } 1224b032f27cSSam Leffler } 1225b032f27cSSam Leffler 1226b032f27cSSam Leffler /* 12278a1b9b6aSSam Leffler * Reclaim any resources in a node and reset any critical 12288a1b9b6aSSam Leffler * state. Typically nodes are free'd immediately after, 12298a1b9b6aSSam Leffler * but in some cases the storage may be reused so we need 12308a1b9b6aSSam Leffler * to insure consistent state (should probably fix that). 12318a1b9b6aSSam Leffler */ 12321a1e1d21SSam Leffler static void 12338a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni) 12341a1e1d21SSam Leffler { 1235b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 12365b16c28cSSam Leffler struct ieee80211com *ic = ni->ni_ic; 123768e8e04eSSam Leffler int i; 12388a1b9b6aSSam Leffler 12398a1b9b6aSSam Leffler /* NB: preserve ni_table */ 12408a1b9b6aSSam Leffler if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 1241b032f27cSSam Leffler if (vap->iv_opmode != IEEE80211_M_STA) 1242b032f27cSSam Leffler vap->iv_ps_sta--; 12438a1b9b6aSSam Leffler ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 1244b032f27cSSam Leffler IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 1245b032f27cSSam Leffler "power save mode off, %u sta's in ps mode", vap->iv_ps_sta); 12468a1b9b6aSSam Leffler } 1247ebdda46cSSam Leffler /* 124851172f62SAdrian Chadd * Cleanup any VHT and HT-related state. 12491b6167d2SSam Leffler */ 125051172f62SAdrian Chadd if (ni->ni_flags & IEEE80211_NODE_VHT) 125151172f62SAdrian Chadd ieee80211_vht_node_cleanup(ni); 12521b6167d2SSam Leffler if (ni->ni_flags & IEEE80211_NODE_HT) 12531b6167d2SSam Leffler ieee80211_ht_node_cleanup(ni); 1254339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 12551c7b0c84SAdrian Chadd /* Always do FF node cleanup; for A-MSDU */ 1256339ccfb3SSam Leffler ieee80211_ff_node_cleanup(ni); 1257339ccfb3SSam Leffler #endif 125859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH 125959aa14a9SRui Paulo /* 126059aa14a9SRui Paulo * Cleanup any mesh-related state. 126159aa14a9SRui Paulo */ 126259aa14a9SRui Paulo if (vap->iv_opmode == IEEE80211_M_MBSS) 126359aa14a9SRui Paulo ieee80211_mesh_node_cleanup(ni); 126459aa14a9SRui Paulo #endif 12651b6167d2SSam Leffler /* 12665b16c28cSSam Leffler * Clear any staging queue entries. 12675b16c28cSSam Leffler */ 12685b16c28cSSam Leffler ieee80211_ageq_drain_node(&ic->ic_stageq, ni); 12695b16c28cSSam Leffler 12705b16c28cSSam Leffler /* 1271ebdda46cSSam Leffler * Clear AREF flag that marks the authorization refcnt bump 1272ebdda46cSSam Leffler * has happened. This is probably not needed as the node 1273ebdda46cSSam Leffler * should always be removed from the table so not found but 1274ebdda46cSSam Leffler * do it just in case. 12751b999d64SSam Leffler * Likewise clear the ASSOCID flag as these flags are intended 12761b999d64SSam Leffler * to be managed in tandem. 1277ebdda46cSSam Leffler */ 12781b999d64SSam Leffler ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID); 12798a1b9b6aSSam Leffler 12808a1b9b6aSSam Leffler /* 12818a1b9b6aSSam Leffler * Drain power save queue and, if needed, clear TIM. 12828a1b9b6aSSam Leffler */ 128363092fceSSam Leffler if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL) 1284b032f27cSSam Leffler vap->iv_set_tim(ni, 0); 12858a1b9b6aSSam Leffler 12868a1b9b6aSSam Leffler ni->ni_associd = 0; 12878a1b9b6aSSam Leffler if (ni->ni_challenge != NULL) { 1288b9b53389SAdrian Chadd IEEE80211_FREE(ni->ni_challenge, M_80211_NODE); 12898a1b9b6aSSam Leffler ni->ni_challenge = NULL; 12908a1b9b6aSSam Leffler } 12918a1b9b6aSSam Leffler /* 12928a1b9b6aSSam Leffler * Preserve SSID, WPA, and WME ie's so the bss node is 12938a1b9b6aSSam Leffler * reusable during a re-auth/re-assoc state transition. 12948a1b9b6aSSam Leffler * If we remove these data they will not be recreated 12958a1b9b6aSSam Leffler * because they come from a probe-response or beacon frame 12968a1b9b6aSSam Leffler * which cannot be expected prior to the association-response. 12978a1b9b6aSSam Leffler * This should not be an issue when operating in other modes 12988a1b9b6aSSam Leffler * as stations leaving always go through a full state transition 12998a1b9b6aSSam Leffler * which will rebuild this state. 13008a1b9b6aSSam Leffler * 13018a1b9b6aSSam Leffler * XXX does this leave us open to inheriting old state? 13028a1b9b6aSSam Leffler */ 1303a3e08d6fSRui Paulo for (i = 0; i < nitems(ni->ni_rxfrag); i++) 13048a1b9b6aSSam Leffler if (ni->ni_rxfrag[i] != NULL) { 13058a1b9b6aSSam Leffler m_freem(ni->ni_rxfrag[i]); 13068a1b9b6aSSam Leffler ni->ni_rxfrag[i] = NULL; 13078a1b9b6aSSam Leffler } 1308c1225b52SSam Leffler /* 1309c1225b52SSam Leffler * Must be careful here to remove any key map entry w/o a LOR. 1310c1225b52SSam Leffler */ 1311c1225b52SSam Leffler ieee80211_node_delucastkey(ni); 13128a1b9b6aSSam Leffler } 13138a1b9b6aSSam Leffler 13148a1b9b6aSSam Leffler static void 13158a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni) 13168a1b9b6aSSam Leffler { 13178a1b9b6aSSam Leffler struct ieee80211com *ic = ni->ni_ic; 13188a1b9b6aSSam Leffler 1319b6108616SRui Paulo ieee80211_ratectl_node_deinit(ni); 13208a1b9b6aSSam Leffler ic->ic_node_cleanup(ni); 1321b032f27cSSam Leffler ieee80211_ies_cleanup(&ni->ni_ies); 132263092fceSSam Leffler ieee80211_psq_cleanup(&ni->ni_psq); 1323b9b53389SAdrian Chadd IEEE80211_FREE(ni, M_80211_NODE); 13241a1e1d21SSam Leffler } 13251a1e1d21SSam Leffler 1326b032f27cSSam Leffler static void 1327b032f27cSSam Leffler node_age(struct ieee80211_node *ni) 1328b032f27cSSam Leffler { 1329b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 13305d44f8c0SSam Leffler 1331b032f27cSSam Leffler /* 1332b032f27cSSam Leffler * Age frames on the power save queue. 1333b032f27cSSam Leffler */ 133463092fceSSam Leffler if (ieee80211_node_psq_age(ni) != 0 && 133563092fceSSam Leffler ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL) 1336b032f27cSSam Leffler vap->iv_set_tim(ni, 0); 1337b032f27cSSam Leffler /* 1338b032f27cSSam Leffler * Age out HT resources (e.g. frames on the 1339b032f27cSSam Leffler * A-MPDU reorder queues). 1340b032f27cSSam Leffler */ 1341b032f27cSSam Leffler if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT)) 1342b032f27cSSam Leffler ieee80211_ht_node_age(ni); 1343b032f27cSSam Leffler } 1344b032f27cSSam Leffler 134568e8e04eSSam Leffler static int8_t 13468a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni) 1347d1e61976SSam Leffler { 1348b032f27cSSam Leffler uint32_t avgrssi = ni->ni_avgrssi; 1349b032f27cSSam Leffler int32_t rssi; 1350b032f27cSSam Leffler 1351b032f27cSSam Leffler if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) 1352b032f27cSSam Leffler return 0; 1353b032f27cSSam Leffler rssi = IEEE80211_RSSI_GET(avgrssi); 1354b032f27cSSam Leffler return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi; 1355d1e61976SSam Leffler } 1356d1e61976SSam Leffler 13571a1e1d21SSam Leffler static void 135868e8e04eSSam Leffler node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise) 135968e8e04eSSam Leffler { 1360b032f27cSSam Leffler *rssi = node_getrssi(ni); 136168e8e04eSSam Leffler *noise = ni->ni_noise; 136268e8e04eSSam Leffler } 136368e8e04eSSam Leffler 136468e8e04eSSam Leffler static void 1365b032f27cSSam Leffler node_getmimoinfo(const struct ieee80211_node *ni, 1366b032f27cSSam Leffler struct ieee80211_mimo_info *info) 1367b032f27cSSam Leffler { 1368864ab114SAdrian Chadd int i; 1369864ab114SAdrian Chadd uint32_t avgrssi; 1370864ab114SAdrian Chadd int32_t rssi; 1371864ab114SAdrian Chadd 1372864ab114SAdrian Chadd bzero(info, sizeof(*info)); 1373864ab114SAdrian Chadd 1374617f8b10SAdrian Chadd for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) { 1375617f8b10SAdrian Chadd /* Note: for now, just pri20 channel info */ 1376864ab114SAdrian Chadd avgrssi = ni->ni_mimo_rssi_ctl[i]; 1377864ab114SAdrian Chadd if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) { 1378617f8b10SAdrian Chadd info->ch[i].rssi[0] = 0; 1379864ab114SAdrian Chadd } else { 1380864ab114SAdrian Chadd rssi = IEEE80211_RSSI_GET(avgrssi); 1381617f8b10SAdrian Chadd info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi; 1382864ab114SAdrian Chadd } 1383617f8b10SAdrian Chadd info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i]; 138488e428c6SAdrian Chadd } 1385864ab114SAdrian Chadd 138688e428c6SAdrian Chadd /* XXX ext radios? */ 1387864ab114SAdrian Chadd 1388864ab114SAdrian Chadd /* XXX EVM? */ 1389b032f27cSSam Leffler } 1390b032f27cSSam Leffler 1391d2e877f0SAndriy Voskoboinyk static void 1392d2e877f0SAndriy Voskoboinyk ieee80211_add_node_nt(struct ieee80211_node_table *nt, 1393d2e877f0SAndriy Voskoboinyk struct ieee80211_node *ni) 1394d2e877f0SAndriy Voskoboinyk { 1395d2e877f0SAndriy Voskoboinyk struct ieee80211com *ic = nt->nt_ic; 1396d2e877f0SAndriy Voskoboinyk int hash; 1397d2e877f0SAndriy Voskoboinyk 1398d2e877f0SAndriy Voskoboinyk IEEE80211_NODE_LOCK_ASSERT(nt); 1399d2e877f0SAndriy Voskoboinyk 1400d2e877f0SAndriy Voskoboinyk hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr); 1401d2e877f0SAndriy Voskoboinyk (void) ic; /* XXX IEEE80211_NODE_HASH */ 1402d2e877f0SAndriy Voskoboinyk TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 1403d2e877f0SAndriy Voskoboinyk LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 1404d2e877f0SAndriy Voskoboinyk nt->nt_count++; 1405d2e877f0SAndriy Voskoboinyk ni->ni_table = nt; 1406d2e877f0SAndriy Voskoboinyk } 1407d2e877f0SAndriy Voskoboinyk 1408d2e877f0SAndriy Voskoboinyk static void 1409d2e877f0SAndriy Voskoboinyk ieee80211_del_node_nt(struct ieee80211_node_table *nt, 1410d2e877f0SAndriy Voskoboinyk struct ieee80211_node *ni) 1411d2e877f0SAndriy Voskoboinyk { 1412d2e877f0SAndriy Voskoboinyk 1413d2e877f0SAndriy Voskoboinyk IEEE80211_NODE_LOCK_ASSERT(nt); 1414d2e877f0SAndriy Voskoboinyk 1415d2e877f0SAndriy Voskoboinyk TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1416d2e877f0SAndriy Voskoboinyk LIST_REMOVE(ni, ni_hash); 1417d2e877f0SAndriy Voskoboinyk nt->nt_count--; 1418d2e877f0SAndriy Voskoboinyk KASSERT(nt->nt_count >= 0, 1419d2e877f0SAndriy Voskoboinyk ("nt_count is negative (%d)!\n", nt->nt_count)); 1420d2e877f0SAndriy Voskoboinyk ni->ni_table = NULL; 1421d2e877f0SAndriy Voskoboinyk } 1422d2e877f0SAndriy Voskoboinyk 1423db195a52SBjoern A. Zeeb static struct ieee80211_node * 1424b032f27cSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt, 14253a11944bSBjoern A. Zeeb struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN], 14263a11944bSBjoern A. Zeeb const char *func __debrefcnt_used, int line __debrefcnt_used) 14271a1e1d21SSam Leffler { 14288a1b9b6aSSam Leffler struct ieee80211com *ic = nt->nt_ic; 1429b032f27cSSam Leffler struct ieee80211_node *ni; 14301a1e1d21SSam Leffler 143138c208f8SSam Leffler ni = ic->ic_node_alloc(vap, macaddr); 1432b032f27cSSam Leffler if (ni == NULL) { 1433b032f27cSSam Leffler vap->iv_stats.is_rx_nodealloc++; 1434b032f27cSSam Leffler return NULL; 1435b032f27cSSam Leffler } 1436b032f27cSSam Leffler 1437b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 143849a15236SSam Leffler "%s %p<%s> in %s table\n", __func__, ni, 14398a1b9b6aSSam Leffler ether_sprintf(macaddr), nt->nt_name); 14408a1b9b6aSSam Leffler 14411a1e1d21SSam Leffler IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 14428a1b9b6aSSam Leffler ieee80211_node_initref(ni); /* mark referenced */ 14433a11944bSBjoern A. Zeeb #ifdef IEEE80211_DEBUG_REFCNT 14443a11944bSBjoern A. Zeeb IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 14453a11944bSBjoern A. Zeeb "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 14463a11944bSBjoern A. Zeeb ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 14473a11944bSBjoern A. Zeeb #endif 14488a1b9b6aSSam Leffler ni->ni_chan = IEEE80211_CHAN_ANYC; 14498a1b9b6aSSam Leffler ni->ni_authmode = IEEE80211_AUTH_OPEN; 14508a1b9b6aSSam Leffler ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 145101a03542SSam Leffler ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1452b032f27cSSam Leffler ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 1453b032f27cSSam Leffler ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER; 14542045f699SSam Leffler ni->ni_inact_reload = nt->nt_inact_init; 14552045f699SSam Leffler ni->ni_inact = ni->ni_inact_reload; 145668e8e04eSSam Leffler ni->ni_ath_defkeyix = 0x7fff; 145763092fceSSam Leffler ieee80211_psq_init(&ni->ni_psq, "unknown"); 145859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH 145959aa14a9SRui Paulo if (vap->iv_opmode == IEEE80211_M_MBSS) 146059aa14a9SRui Paulo ieee80211_mesh_node_init(vap, ni); 146159aa14a9SRui Paulo #endif 14628a1b9b6aSSam Leffler IEEE80211_NODE_LOCK(nt); 1463d2e877f0SAndriy Voskoboinyk ieee80211_add_node_nt(nt, ni); 1464b032f27cSSam Leffler ni->ni_vap = vap; 14658a1b9b6aSSam Leffler ni->ni_ic = ic; 14668a1b9b6aSSam Leffler IEEE80211_NODE_UNLOCK(nt); 14671a1e1d21SSam Leffler 1468ea3d5fd9SAdrian Chadd /* handle failure; free node state */ 1469ea3d5fd9SAdrian Chadd if (ic->ic_node_init(ni) != 0) { 1470ea3d5fd9SAdrian Chadd vap->iv_stats.is_rx_nodealloc++; 1471ea3d5fd9SAdrian Chadd ieee80211_psq_cleanup(&ni->ni_psq); 1472ea3d5fd9SAdrian Chadd ieee80211_ratectl_node_deinit(ni); 14734a8e4d15SBjoern A. Zeeb __ieee80211_free_node(ni); 1474ea3d5fd9SAdrian Chadd return NULL; 1475ea3d5fd9SAdrian Chadd } 1476ea3d5fd9SAdrian Chadd 1477be1054edSSam Leffler IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 1478be1054edSSam Leffler "%s: inact_reload %u", __func__, ni->ni_inact_reload); 1479be1054edSSam Leffler 14801a1e1d21SSam Leffler return ni; 14811a1e1d21SSam Leffler } 14821a1e1d21SSam Leffler 148397c973adSSam Leffler /* 148497c973adSSam Leffler * Craft a temporary node suitable for sending a management frame 148597c973adSSam Leffler * to the specified station. We craft only as much state as we 148697c973adSSam Leffler * need to do the work since the node will be immediately reclaimed 148797c973adSSam Leffler * once the send completes. 148897c973adSSam Leffler */ 148997c973adSSam Leffler struct ieee80211_node * 1490b032f27cSSam Leffler ieee80211_tmp_node(struct ieee80211vap *vap, 1491b032f27cSSam Leffler const uint8_t macaddr[IEEE80211_ADDR_LEN]) 149297c973adSSam Leffler { 1493b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 149497c973adSSam Leffler struct ieee80211_node *ni; 149597c973adSSam Leffler 149638c208f8SSam Leffler ni = ic->ic_node_alloc(vap, macaddr); 149797c973adSSam Leffler if (ni != NULL) { 1498b9b5f07dSSam Leffler struct ieee80211_node *bss = vap->iv_bss; 1499b9b5f07dSSam Leffler 1500b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 150197c973adSSam Leffler "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr)); 150297c973adSSam Leffler 1503b032f27cSSam Leffler ni->ni_table = NULL; /* NB: pedantic */ 1504b032f27cSSam Leffler ni->ni_ic = ic; /* NB: needed to set channel */ 1505b032f27cSSam Leffler ni->ni_vap = vap; 1506b032f27cSSam Leffler 150797c973adSSam Leffler IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 1508b9b5f07dSSam Leffler IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid); 150997c973adSSam Leffler ieee80211_node_initref(ni); /* mark referenced */ 15103a11944bSBjoern A. Zeeb #ifdef IEEE80211_DEBUG_REFCNT 15113a11944bSBjoern A. Zeeb /* Only one caller so we skip func/line passing into the func. */ 15123a11944bSBjoern A. Zeeb IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 15133a11944bSBjoern A. Zeeb "%s (%s:%u) %p<%s> refcnt %d\n", __func__, "", -1, ni, 15143a11944bSBjoern A. Zeeb ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 15153a11944bSBjoern A. Zeeb #endif 151697c973adSSam Leffler /* NB: required by ieee80211_fix_rate */ 1517b9b5f07dSSam Leffler ieee80211_node_set_chan(ni, bss->ni_chan); 1518b032f27cSSam Leffler ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, 151997c973adSSam Leffler IEEE80211_KEYIX_NONE); 1520b9b5f07dSSam Leffler ni->ni_txpower = bss->ni_txpower; 152197c973adSSam Leffler /* XXX optimize away */ 152263092fceSSam Leffler ieee80211_psq_init(&ni->ni_psq, "unknown"); 1523bd56e71bSBernhard Schmidt 1524bd56e71bSBernhard Schmidt ieee80211_ratectl_node_init(ni); 1525ea3d5fd9SAdrian Chadd 1526ea3d5fd9SAdrian Chadd /* handle failure; free node state */ 1527ea3d5fd9SAdrian Chadd if (ic->ic_node_init(ni) != 0) { 1528ea3d5fd9SAdrian Chadd vap->iv_stats.is_rx_nodealloc++; 1529ea3d5fd9SAdrian Chadd ieee80211_psq_cleanup(&ni->ni_psq); 1530ea3d5fd9SAdrian Chadd ieee80211_ratectl_node_deinit(ni); 15314a8e4d15SBjoern A. Zeeb __ieee80211_free_node(ni); 1532ea3d5fd9SAdrian Chadd return NULL; 1533ea3d5fd9SAdrian Chadd } 1534ea3d5fd9SAdrian Chadd 153597c973adSSam Leffler } else { 153697c973adSSam Leffler /* XXX msg */ 1537b032f27cSSam Leffler vap->iv_stats.is_rx_nodealloc++; 153897c973adSSam Leffler } 153997c973adSSam Leffler return ni; 154097c973adSSam Leffler } 154197c973adSSam Leffler 15421a1e1d21SSam Leffler struct ieee80211_node * 1543b032f27cSSam Leffler ieee80211_dup_bss(struct ieee80211vap *vap, 1544b032f27cSSam Leffler const uint8_t macaddr[IEEE80211_ADDR_LEN]) 15451a1e1d21SSam Leffler { 1546b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 15478a1b9b6aSSam Leffler struct ieee80211_node *ni; 15488a1b9b6aSSam Leffler 15493a11944bSBjoern A. Zeeb ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr, __func__, __LINE__); 15501a1e1d21SSam Leffler if (ni != NULL) { 1551b9b5f07dSSam Leffler struct ieee80211_node *bss = vap->iv_bss; 1552694dca64SSam Leffler /* 1553b032f27cSSam Leffler * Inherit from iv_bss. 1554694dca64SSam Leffler */ 1555b9b5f07dSSam Leffler copy_bss(ni, bss); 1556b9b5f07dSSam Leffler IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid); 1557b9b5f07dSSam Leffler ieee80211_node_set_chan(ni, bss->ni_chan); 1558b032f27cSSam Leffler } 15591a1e1d21SSam Leffler return ni; 15601a1e1d21SSam Leffler } 15611a1e1d21SSam Leffler 1562b032f27cSSam Leffler /* 1563b032f27cSSam Leffler * Create a bss node for a legacy WDS vap. The far end does 1564b032f27cSSam Leffler * not associate so we just create create a new node and 1565b032f27cSSam Leffler * simulate an association. The caller is responsible for 1566b032f27cSSam Leffler * installing the node as the bss node and handling any further 1567b032f27cSSam Leffler * setup work like authorizing the port. 1568b032f27cSSam Leffler */ 1569b032f27cSSam Leffler struct ieee80211_node * 1570b032f27cSSam Leffler ieee80211_node_create_wds(struct ieee80211vap *vap, 1571b032f27cSSam Leffler const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan) 1572b032f27cSSam Leffler { 1573b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 1574b032f27cSSam Leffler struct ieee80211_node *ni; 1575b032f27cSSam Leffler 1576b032f27cSSam Leffler /* XXX check if node already in sta table? */ 15773a11944bSBjoern A. Zeeb ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid, __func__, __LINE__); 1578b032f27cSSam Leffler if (ni != NULL) { 1579b032f27cSSam Leffler ni->ni_wdsvap = vap; 1580b032f27cSSam Leffler IEEE80211_ADDR_COPY(ni->ni_bssid, bssid); 1581b032f27cSSam Leffler /* 1582b032f27cSSam Leffler * Inherit any manually configured settings. 1583b032f27cSSam Leffler */ 1584b9b5f07dSSam Leffler copy_bss(ni, vap->iv_bss); 1585b032f27cSSam Leffler ieee80211_node_set_chan(ni, chan); 1586b032f27cSSam Leffler /* NB: propagate ssid so available to WPA supplicant */ 1587b032f27cSSam Leffler ni->ni_esslen = vap->iv_des_ssid[0].len; 1588b032f27cSSam Leffler memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen); 1589b032f27cSSam Leffler /* NB: no associd for peer */ 1590b032f27cSSam Leffler /* 1591b032f27cSSam Leffler * There are no management frames to use to 1592b032f27cSSam Leffler * discover neighbor capabilities, so blindly 1593b032f27cSSam Leffler * propagate the local configuration. 1594b032f27cSSam Leffler */ 1595b032f27cSSam Leffler if (vap->iv_flags & IEEE80211_F_WME) 1596b032f27cSSam Leffler ni->ni_flags |= IEEE80211_NODE_QOS; 1597616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 1598b032f27cSSam Leffler if (vap->iv_flags & IEEE80211_F_FF) 1599b032f27cSSam Leffler ni->ni_flags |= IEEE80211_NODE_FF; 1600616190d0SSam Leffler #endif 160151172f62SAdrian Chadd /* XXX VHT */ 1602b032f27cSSam Leffler if ((ic->ic_htcaps & IEEE80211_HTC_HT) && 16032bfc8a91SSam Leffler (vap->iv_flags_ht & IEEE80211_FHT_HT)) { 1604b032f27cSSam Leffler /* 1605b032f27cSSam Leffler * Device is HT-capable and HT is enabled for 1606b032f27cSSam Leffler * the vap; setup HT operation. On return 1607b032f27cSSam Leffler * ni_chan will be adjusted to an HT channel. 1608b032f27cSSam Leffler */ 1609b032f27cSSam Leffler ieee80211_ht_wds_init(ni); 1610ef48d4faSBjoern A. Zeeb if (vap->iv_vht_flags & IEEE80211_FVHT_VHT) { 161151172f62SAdrian Chadd printf("%s: TODO: vht_wds_init\n", __func__); 161251172f62SAdrian Chadd } 1613b032f27cSSam Leffler } else { 1614b032f27cSSam Leffler struct ieee80211_channel *c = ni->ni_chan; 1615b032f27cSSam Leffler /* 1616b032f27cSSam Leffler * Force a legacy channel to be used. 1617b032f27cSSam Leffler */ 1618b032f27cSSam Leffler c = ieee80211_find_channel(ic, 1619b032f27cSSam Leffler c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT); 1620b032f27cSSam Leffler KASSERT(c != NULL, ("no legacy channel, %u/%x", 1621b032f27cSSam Leffler ni->ni_chan->ic_freq, ni->ni_chan->ic_flags)); 1622b032f27cSSam Leffler ni->ni_chan = c; 1623b032f27cSSam Leffler } 1624b032f27cSSam Leffler } 1625b032f27cSSam Leffler return ni; 1626b032f27cSSam Leffler } 1627b032f27cSSam Leffler 1628b032f27cSSam Leffler struct ieee80211_node * 16294a8e4d15SBjoern A. Zeeb _ieee80211_find_node_locked(struct ieee80211_node_table *nt, 16304a8e4d15SBjoern A. Zeeb const uint8_t macaddr[IEEE80211_ADDR_LEN], 16314a8e4d15SBjoern A. Zeeb const char *func __debrefcnt_used, int line __debrefcnt_used) 16321a1e1d21SSam Leffler { 16331a1e1d21SSam Leffler struct ieee80211_node *ni; 16341a1e1d21SSam Leffler int hash; 16351a1e1d21SSam Leffler 16368a1b9b6aSSam Leffler IEEE80211_NODE_LOCK_ASSERT(nt); 1637750d6d0cSSam Leffler 163859aa14a9SRui Paulo hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr); 16398a1b9b6aSSam Leffler LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 16401a1e1d21SSam Leffler if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 16418a1b9b6aSSam Leffler ieee80211_ref_node(ni); /* mark referenced */ 16428a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT 1643b032f27cSSam Leffler IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 164449a15236SSam Leffler "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 164549a15236SSam Leffler func, line, 164649a15236SSam Leffler ni, ether_sprintf(ni->ni_macaddr), 16478a1b9b6aSSam Leffler ieee80211_node_refcnt(ni)); 16488a1b9b6aSSam Leffler #endif 1649750d6d0cSSam Leffler return ni; 16501a1e1d21SSam Leffler } 16511a1e1d21SSam Leffler } 1652750d6d0cSSam Leffler return NULL; 1653750d6d0cSSam Leffler } 1654750d6d0cSSam Leffler 1655750d6d0cSSam Leffler struct ieee80211_node * 16564a8e4d15SBjoern A. Zeeb _ieee80211_find_node(struct ieee80211_node_table *nt, 16574a8e4d15SBjoern A. Zeeb const uint8_t macaddr[IEEE80211_ADDR_LEN], 16584a8e4d15SBjoern A. Zeeb const char *func __debrefcnt_used, int line __debrefcnt_used) 1659750d6d0cSSam Leffler { 1660750d6d0cSSam Leffler struct ieee80211_node *ni; 1661750d6d0cSSam Leffler 16628a1b9b6aSSam Leffler IEEE80211_NODE_LOCK(nt); 16634a8e4d15SBjoern A. Zeeb ni = _ieee80211_find_node_locked(nt, macaddr, func, line); 1664b032f27cSSam Leffler IEEE80211_NODE_UNLOCK(nt); 1665b032f27cSSam Leffler return ni; 1666b032f27cSSam Leffler } 1667b032f27cSSam Leffler 1668b032f27cSSam Leffler struct ieee80211_node * 16694a8e4d15SBjoern A. Zeeb _ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt, 16704a8e4d15SBjoern A. Zeeb const struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN], 16714a8e4d15SBjoern A. Zeeb const char *func __debrefcnt_used, int line __debrefcnt_used) 1672b032f27cSSam Leffler { 1673b032f27cSSam Leffler struct ieee80211_node *ni; 1674b032f27cSSam Leffler int hash; 1675b032f27cSSam Leffler 1676b032f27cSSam Leffler IEEE80211_NODE_LOCK_ASSERT(nt); 1677b032f27cSSam Leffler 167859aa14a9SRui Paulo hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr); 1679b032f27cSSam Leffler LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1680b032f27cSSam Leffler if (ni->ni_vap == vap && 1681b032f27cSSam Leffler IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 1682b032f27cSSam Leffler ieee80211_ref_node(ni); /* mark referenced */ 1683b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT 1684b032f27cSSam Leffler IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 1685b032f27cSSam Leffler "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1686b032f27cSSam Leffler func, line, 1687b032f27cSSam Leffler ni, ether_sprintf(ni->ni_macaddr), 1688b032f27cSSam Leffler ieee80211_node_refcnt(ni)); 1689b032f27cSSam Leffler #endif 1690b032f27cSSam Leffler return ni; 1691b032f27cSSam Leffler } 1692b032f27cSSam Leffler } 1693b032f27cSSam Leffler return NULL; 1694b032f27cSSam Leffler } 1695b032f27cSSam Leffler 1696b032f27cSSam Leffler struct ieee80211_node * 16974a8e4d15SBjoern A. Zeeb _ieee80211_find_vap_node(struct ieee80211_node_table *nt, 16984a8e4d15SBjoern A. Zeeb const struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN], 16994a8e4d15SBjoern A. Zeeb const char *func __debrefcnt_used, int line __debrefcnt_used) 1700b032f27cSSam Leffler { 1701b032f27cSSam Leffler struct ieee80211_node *ni; 1702b032f27cSSam Leffler 1703b032f27cSSam Leffler IEEE80211_NODE_LOCK(nt); 17044a8e4d15SBjoern A. Zeeb ni = _ieee80211_find_vap_node_locked(nt, vap, macaddr, func, line); 17058a1b9b6aSSam Leffler IEEE80211_NODE_UNLOCK(nt); 17061a1e1d21SSam Leffler return ni; 17071a1e1d21SSam Leffler } 17081a1e1d21SSam Leffler 17091a1e1d21SSam Leffler /* 17108a1b9b6aSSam Leffler * Fake up a node; this handles node discovery in adhoc mode. 171193e49148SGordon Bergling * Note that for the driver's benefit we treat this like 17128a1b9b6aSSam Leffler * an association so the driver has an opportunity to setup 17138a1b9b6aSSam Leffler * it's private state. 17148a1b9b6aSSam Leffler */ 17158a1b9b6aSSam Leffler struct ieee80211_node * 1716b032f27cSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap, 171768e8e04eSSam Leffler const uint8_t macaddr[IEEE80211_ADDR_LEN]) 17188a1b9b6aSSam Leffler { 17198a1b9b6aSSam Leffler struct ieee80211_node *ni; 17208a1b9b6aSSam Leffler 1721d71a1f7aSAdrian Chadd IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC, 1722be425a0fSSam Leffler "%s: mac<%s>\n", __func__, ether_sprintf(macaddr)); 1723b032f27cSSam Leffler ni = ieee80211_dup_bss(vap, macaddr); 17248a1b9b6aSSam Leffler if (ni != NULL) { 1725b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 1726b032f27cSSam Leffler 17278a1b9b6aSSam Leffler /* XXX no rate negotiation; just dup */ 1728b032f27cSSam Leffler ni->ni_rates = vap->iv_bss->ni_rates; 1729aa68c24fSSam Leffler if (ieee80211_iserp_rateset(&ni->ni_rates)) 1730aa68c24fSSam Leffler ni->ni_flags |= IEEE80211_NODE_ERP; 1731b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_AHDEMO) { 17328e292e8eSSam Leffler /* 173368e8e04eSSam Leffler * In adhoc demo mode there are no management 173468e8e04eSSam Leffler * frames to use to discover neighbor capabilities, 173568e8e04eSSam Leffler * so blindly propagate the local configuration 173668e8e04eSSam Leffler * so we can do interesting things (e.g. use 173768e8e04eSSam Leffler * WME to disable ACK's). 17388e292e8eSSam Leffler */ 1739869897d2SAdrian Chadd /* 1740869897d2SAdrian Chadd * XXX TODO: 11n? 1741869897d2SAdrian Chadd */ 1742b032f27cSSam Leffler if (vap->iv_flags & IEEE80211_F_WME) 17438e292e8eSSam Leffler ni->ni_flags |= IEEE80211_NODE_QOS; 1744616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 1745b032f27cSSam Leffler if (vap->iv_flags & IEEE80211_F_FF) 174668e8e04eSSam Leffler ni->ni_flags |= IEEE80211_NODE_FF; 1747616190d0SSam Leffler #endif 17488e292e8eSSam Leffler } 1749d77148fbSSam Leffler ieee80211_node_setuptxparms(ni); 175049d2c137SBernhard Schmidt ieee80211_ratectl_node_init(ni); 1751869897d2SAdrian Chadd 1752869897d2SAdrian Chadd /* 1753869897d2SAdrian Chadd * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX, 1754869897d2SAdrian Chadd * not A-MPDU TX; not 11n rates, etc. We'll cycle 1755869897d2SAdrian Chadd * that after we hear that we can indeed do 11n 1756869897d2SAdrian Chadd * (either by a beacon frame or by a probe response.) 1757869897d2SAdrian Chadd */ 1758869897d2SAdrian Chadd 1759869897d2SAdrian Chadd /* 1760869897d2SAdrian Chadd * This is the first time we see the node. 1761869897d2SAdrian Chadd */ 1762b032f27cSSam Leffler if (ic->ic_newassoc != NULL) 1763b032f27cSSam Leffler ic->ic_newassoc(ni, 1); 1764869897d2SAdrian Chadd 1765869897d2SAdrian Chadd /* 1766869897d2SAdrian Chadd * Kick off a probe request to the given node; 1767869897d2SAdrian Chadd * we will then use the probe response to update 1768869897d2SAdrian Chadd * 11n/etc configuration state. 1769869897d2SAdrian Chadd * 1770869897d2SAdrian Chadd * XXX TODO: this isn't guaranteed, and until we get 1771869897d2SAdrian Chadd * a probe response, we won't be able to actually 1772869897d2SAdrian Chadd * do anything 802.11n related to the node. 1773869897d2SAdrian Chadd * So if this does indeed work, maybe we should hold 1774869897d2SAdrian Chadd * off on sending responses until we get the probe 1775869897d2SAdrian Chadd * response, or just default to some sensible subset 1776869897d2SAdrian Chadd * of 802.11n behaviour (eg always allow aggregation 1777869897d2SAdrian Chadd * negotiation TO us, but not FROM us, etc) so we 1778869897d2SAdrian Chadd * aren't entirely busted. 1779869897d2SAdrian Chadd */ 1780869897d2SAdrian Chadd if (vap->iv_opmode == IEEE80211_M_IBSS) { 1781869897d2SAdrian Chadd ieee80211_send_probereq(ni, /* node */ 1782869897d2SAdrian Chadd vap->iv_myaddr, /* SA */ 1783869897d2SAdrian Chadd ni->ni_macaddr, /* DA */ 1784869897d2SAdrian Chadd vap->iv_bss->ni_bssid, /* BSSID */ 1785869897d2SAdrian Chadd vap->iv_bss->ni_essid, 1786869897d2SAdrian Chadd vap->iv_bss->ni_esslen); /* SSID */ 1787869897d2SAdrian Chadd } 1788869897d2SAdrian Chadd 178968e8e04eSSam Leffler /* XXX not right for 802.1x/WPA */ 179068e8e04eSSam Leffler ieee80211_node_authorize(ni); 17918a1b9b6aSSam Leffler } 17928a1b9b6aSSam Leffler return ni; 17938a1b9b6aSSam Leffler } 17948a1b9b6aSSam Leffler 1795be425a0fSSam Leffler void 1796be425a0fSSam Leffler ieee80211_init_neighbor(struct ieee80211_node *ni, 1797be425a0fSSam Leffler const struct ieee80211_frame *wh, 1798be425a0fSSam Leffler const struct ieee80211_scanparams *sp) 1799be425a0fSSam Leffler { 180051172f62SAdrian Chadd int do_ht_setup = 0, do_vht_setup = 0; 1801d71a1f7aSAdrian Chadd 1802be425a0fSSam Leffler ni->ni_esslen = sp->ssid[1]; 1803be425a0fSSam Leffler memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 1804be425a0fSSam Leffler IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1805be425a0fSSam Leffler memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 1806be425a0fSSam Leffler ni->ni_intval = sp->bintval; 1807be425a0fSSam Leffler ni->ni_capinfo = sp->capinfo; 1808be425a0fSSam Leffler ni->ni_chan = ni->ni_ic->ic_curchan; 1809be425a0fSSam Leffler ni->ni_fhdwell = sp->fhdwell; 1810be425a0fSSam Leffler ni->ni_fhindex = sp->fhindex; 1811be425a0fSSam Leffler ni->ni_erp = sp->erp; 1812be425a0fSSam Leffler ni->ni_timoff = sp->timoff; 181359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH 181459aa14a9SRui Paulo if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS) 181559aa14a9SRui Paulo ieee80211_mesh_init_neighbor(ni, wh, sp); 181659aa14a9SRui Paulo #endif 1817b032f27cSSam Leffler if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) { 1818b032f27cSSam Leffler ieee80211_ies_expand(&ni->ni_ies); 18199094ffdfSSam Leffler if (ni->ni_ies.wme_ie != NULL) 18209094ffdfSSam Leffler ni->ni_flags |= IEEE80211_NODE_QOS; 18219094ffdfSSam Leffler else 18229094ffdfSSam Leffler ni->ni_flags &= ~IEEE80211_NODE_QOS; 1823616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 1824b032f27cSSam Leffler if (ni->ni_ies.ath_ie != NULL) 1825b032f27cSSam Leffler ieee80211_parse_ath(ni, ni->ni_ies.ath_ie); 1826616190d0SSam Leffler #endif 1827d71a1f7aSAdrian Chadd if (ni->ni_ies.htcap_ie != NULL) 1828d71a1f7aSAdrian Chadd ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie); 1829d71a1f7aSAdrian Chadd if (ni->ni_ies.htinfo_ie != NULL) 1830d71a1f7aSAdrian Chadd ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie); 1831d71a1f7aSAdrian Chadd 183251172f62SAdrian Chadd if (ni->ni_ies.vhtcap_ie != NULL) 183351172f62SAdrian Chadd ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie); 183451172f62SAdrian Chadd if (ni->ni_ies.vhtopmode_ie != NULL) 183551172f62SAdrian Chadd ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie); 183651172f62SAdrian Chadd 1837d71a1f7aSAdrian Chadd if ((ni->ni_ies.htcap_ie != NULL) && 1838d71a1f7aSAdrian Chadd (ni->ni_ies.htinfo_ie != NULL) && 1839d71a1f7aSAdrian Chadd (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) { 1840d71a1f7aSAdrian Chadd do_ht_setup = 1; 1841d71a1f7aSAdrian Chadd } 184251172f62SAdrian Chadd 184351172f62SAdrian Chadd if ((ni->ni_ies.vhtcap_ie != NULL) && 184451172f62SAdrian Chadd (ni->ni_ies.vhtopmode_ie != NULL) && 1845ef48d4faSBjoern A. Zeeb (ni->ni_vap->iv_vht_flags & IEEE80211_FVHT_VHT)) { 184651172f62SAdrian Chadd do_vht_setup = 1; 184751172f62SAdrian Chadd } 1848b032f27cSSam Leffler } 1849be425a0fSSam Leffler 1850be425a0fSSam Leffler /* NB: must be after ni_chan is setup */ 185179edaebfSSam Leffler ieee80211_setup_rates(ni, sp->rates, sp->xrates, 185279edaebfSSam Leffler IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 185379edaebfSSam Leffler IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1854d71a1f7aSAdrian Chadd 1855d71a1f7aSAdrian Chadd /* 1856d71a1f7aSAdrian Chadd * If the neighbor is HT compatible, flip that on. 1857d71a1f7aSAdrian Chadd */ 1858d71a1f7aSAdrian Chadd if (do_ht_setup) { 1859d71a1f7aSAdrian Chadd IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 1860d71a1f7aSAdrian Chadd "%s: doing HT setup\n", __func__); 1861d71a1f7aSAdrian Chadd ieee80211_ht_node_init(ni); 1862d71a1f7aSAdrian Chadd ieee80211_ht_updateparams(ni, 1863d71a1f7aSAdrian Chadd ni->ni_ies.htcap_ie, 1864d71a1f7aSAdrian Chadd ni->ni_ies.htinfo_ie); 186551172f62SAdrian Chadd 186651172f62SAdrian Chadd if (do_vht_setup) { 186751172f62SAdrian Chadd if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) { 186851172f62SAdrian Chadd printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n", 186951172f62SAdrian Chadd __func__, 187051172f62SAdrian Chadd ni->ni_macaddr, 187151172f62SAdrian Chadd ":"); 187251172f62SAdrian Chadd } else { 187351172f62SAdrian Chadd ieee80211_vht_node_init(ni); 187451172f62SAdrian Chadd ieee80211_vht_updateparams(ni, 187551172f62SAdrian Chadd ni->ni_ies.vhtcap_ie, 187651172f62SAdrian Chadd ni->ni_ies.vhtopmode_ie); 187751172f62SAdrian Chadd ieee80211_setup_vht_rates(ni, 187851172f62SAdrian Chadd ni->ni_ies.vhtcap_ie, 187951172f62SAdrian Chadd ni->ni_ies.vhtopmode_ie); 188051172f62SAdrian Chadd } 188151172f62SAdrian Chadd } 188251172f62SAdrian Chadd 188351172f62SAdrian Chadd /* 188451172f62SAdrian Chadd * Finally do the channel upgrade/change based 188551172f62SAdrian Chadd * on the HT/VHT configuration. 188651172f62SAdrian Chadd */ 188751172f62SAdrian Chadd ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie, 188851172f62SAdrian Chadd ni->ni_ies.htinfo_ie); 1889d71a1f7aSAdrian Chadd ieee80211_setup_htrates(ni, 1890d71a1f7aSAdrian Chadd ni->ni_ies.htcap_ie, 1891d71a1f7aSAdrian Chadd IEEE80211_F_JOIN | IEEE80211_F_DOBRS); 1892d71a1f7aSAdrian Chadd ieee80211_setup_basic_htrates(ni, 1893d71a1f7aSAdrian Chadd ni->ni_ies.htinfo_ie); 189451172f62SAdrian Chadd 1895d71a1f7aSAdrian Chadd ieee80211_node_setuptxparms(ni); 1896d71a1f7aSAdrian Chadd ieee80211_ratectl_node_init(ni); 1897869897d2SAdrian Chadd 189851172f62SAdrian Chadd /* Reassociate; we're now 11n/11ac */ 1899869897d2SAdrian Chadd /* 1900869897d2SAdrian Chadd * XXX TODO: this is the wrong thing to do - 1901869897d2SAdrian Chadd * we're calling it with isnew=1 so the ath(4) 1902869897d2SAdrian Chadd * driver reinitialises the rate tables. 1903869897d2SAdrian Chadd * This "mostly" works for ath(4), but it won't 1904869897d2SAdrian Chadd * be right for firmware devices which allocate 1905869897d2SAdrian Chadd * node states. 1906869897d2SAdrian Chadd * 1907869897d2SAdrian Chadd * So, do we just create a new node and delete 1908869897d2SAdrian Chadd * the old one? Or? 1909869897d2SAdrian Chadd */ 1910869897d2SAdrian Chadd if (ni->ni_ic->ic_newassoc) 1911869897d2SAdrian Chadd ni->ni_ic->ic_newassoc(ni, 1); 1912d71a1f7aSAdrian Chadd } 1913be425a0fSSam Leffler } 1914be425a0fSSam Leffler 1915b5c99415SSam Leffler /* 1916b5c99415SSam Leffler * Do node discovery in adhoc mode on receipt of a beacon 1917b5c99415SSam Leffler * or probe response frame. Note that for the driver's 191893e49148SGordon Bergling * benefit we treat this like an association so the 1919b5c99415SSam Leffler * driver has an opportunity to setup it's private state. 1920b5c99415SSam Leffler */ 1921b5c99415SSam Leffler struct ieee80211_node * 1922b032f27cSSam Leffler ieee80211_add_neighbor(struct ieee80211vap *vap, 1923b5c99415SSam Leffler const struct ieee80211_frame *wh, 1924b5c99415SSam Leffler const struct ieee80211_scanparams *sp) 1925b5c99415SSam Leffler { 1926b5c99415SSam Leffler struct ieee80211_node *ni; 1927b5c99415SSam Leffler 1928d71a1f7aSAdrian Chadd IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC, 1929be425a0fSSam Leffler "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2)); 1930b032f27cSSam Leffler ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */ 1931b5c99415SSam Leffler if (ni != NULL) { 1932b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 1933b032f27cSSam Leffler 1934be425a0fSSam Leffler ieee80211_init_neighbor(ni, wh, sp); 1935aa68c24fSSam Leffler if (ieee80211_iserp_rateset(&ni->ni_rates)) 1936aa68c24fSSam Leffler ni->ni_flags |= IEEE80211_NODE_ERP; 1937d77148fbSSam Leffler ieee80211_node_setuptxparms(ni); 193849d2c137SBernhard Schmidt ieee80211_ratectl_node_init(ni); 1939b5c99415SSam Leffler if (ic->ic_newassoc != NULL) 1940b5c99415SSam Leffler ic->ic_newassoc(ni, 1); 1941b5c99415SSam Leffler /* XXX not right for 802.1x/WPA */ 1942b5c99415SSam Leffler ieee80211_node_authorize(ni); 1943b5c99415SSam Leffler } 1944b5c99415SSam Leffler return ni; 1945b5c99415SSam Leffler } 1946b5c99415SSam Leffler 1947a2cfa5b7SSam Leffler #define IS_PROBEREQ(wh) \ 1948a2cfa5b7SSam Leffler ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \ 1949a2cfa5b7SSam Leffler == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ)) 1950a2cfa5b7SSam Leffler #define IS_BCAST_PROBEREQ(wh) \ 1951a2cfa5b7SSam Leffler (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \ 1952a2cfa5b7SSam Leffler ((const struct ieee80211_frame *)(wh))->i_addr3)) 1953a2cfa5b7SSam Leffler 1954a2cfa5b7SSam Leffler static __inline struct ieee80211_node * 1955a2cfa5b7SSam Leffler _find_rxnode(struct ieee80211_node_table *nt, 19564a8e4d15SBjoern A. Zeeb const struct ieee80211_frame_min *wh, 19574a8e4d15SBjoern A. Zeeb const char *func __debrefcnt_used, int line __debrefcnt_used) 1958a2cfa5b7SSam Leffler { 1959a2cfa5b7SSam Leffler if (IS_BCAST_PROBEREQ(wh)) 1960a2cfa5b7SSam Leffler return NULL; /* spam bcast probe req to all vap's */ 19614a8e4d15SBjoern A. Zeeb return _ieee80211_find_node_locked(nt, wh->i_addr2, func, line); 1962a2cfa5b7SSam Leffler } 196368e8e04eSSam Leffler 19648a1b9b6aSSam Leffler /* 19658a1b9b6aSSam Leffler * Locate the node for sender, track state, and then pass the 1966a2cfa5b7SSam Leffler * (referenced) node up to the 802.11 layer for its use. Note 1967a2cfa5b7SSam Leffler * we can return NULL if the sender is not in the table. 19688a1b9b6aSSam Leffler */ 19698a1b9b6aSSam Leffler struct ieee80211_node * 19704a8e4d15SBjoern A. Zeeb _ieee80211_find_rxnode(struct ieee80211com *ic, 19714a8e4d15SBjoern A. Zeeb const struct ieee80211_frame_min *wh, 19724a8e4d15SBjoern A. Zeeb const char *func __debrefcnt_used, int line __debrefcnt_used) 19738a1b9b6aSSam Leffler { 19748a1b9b6aSSam Leffler struct ieee80211_node_table *nt; 19758a1b9b6aSSam Leffler struct ieee80211_node *ni; 19768a1b9b6aSSam Leffler 197768e8e04eSSam Leffler nt = &ic->ic_sta; 19788a1b9b6aSSam Leffler IEEE80211_NODE_LOCK(nt); 19794a8e4d15SBjoern A. Zeeb ni = _find_rxnode(nt, wh, func, line); 19808a1b9b6aSSam Leffler IEEE80211_NODE_UNLOCK(nt); 19818a1b9b6aSSam Leffler 1982c1225b52SSam Leffler return ni; 1983c1225b52SSam Leffler } 1984c1225b52SSam Leffler 1985c1225b52SSam Leffler /* 1986c1225b52SSam Leffler * Like ieee80211_find_rxnode but use the supplied h/w 1987c1225b52SSam Leffler * key index as a hint to locate the node in the key 1988c1225b52SSam Leffler * mapping table. If an entry is present at the key 1989c1225b52SSam Leffler * index we return it; otherwise do a normal lookup and 1990c1225b52SSam Leffler * update the mapping table if the station has a unicast 1991c1225b52SSam Leffler * key assigned to it. 1992c1225b52SSam Leffler */ 1993c1225b52SSam Leffler struct ieee80211_node * 19944a8e4d15SBjoern A. Zeeb _ieee80211_find_rxnode_withkey(struct ieee80211com *ic, 1995c1225b52SSam Leffler const struct ieee80211_frame_min *wh, ieee80211_keyix keyix, 19964a8e4d15SBjoern A. Zeeb const char *func __debrefcnt_used, int line __debrefcnt_used) 1997c1225b52SSam Leffler { 1998c1225b52SSam Leffler struct ieee80211_node_table *nt; 1999c1225b52SSam Leffler struct ieee80211_node *ni; 2000c1225b52SSam Leffler 2001c1225b52SSam Leffler nt = &ic->ic_sta; 2002c1225b52SSam Leffler IEEE80211_NODE_LOCK(nt); 2003c1225b52SSam Leffler if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) 2004c1225b52SSam Leffler ni = nt->nt_keyixmap[keyix]; 2005c1225b52SSam Leffler else 2006c1225b52SSam Leffler ni = NULL; 2007c1225b52SSam Leffler if (ni == NULL) { 20084a8e4d15SBjoern A. Zeeb ni = _find_rxnode(nt, wh, func, line); 2009b032f27cSSam Leffler if (ni != NULL && nt->nt_keyixmap != NULL) { 2010c1225b52SSam Leffler /* 2011c1225b52SSam Leffler * If the station has a unicast key cache slot 2012c1225b52SSam Leffler * assigned update the key->node mapping table. 2013c1225b52SSam Leffler */ 2014c1225b52SSam Leffler keyix = ni->ni_ucastkey.wk_rxkeyix; 2015c1225b52SSam Leffler /* XXX can keyixmap[keyix] != NULL? */ 2016c1225b52SSam Leffler if (keyix < nt->nt_keyixmax && 2017c1225b52SSam Leffler nt->nt_keyixmap[keyix] == NULL) { 2018b032f27cSSam Leffler IEEE80211_DPRINTF(ni->ni_vap, 2019b032f27cSSam Leffler IEEE80211_MSG_NODE, 2020c1225b52SSam Leffler "%s: add key map entry %p<%s> refcnt %d\n", 2021c1225b52SSam Leffler __func__, ni, ether_sprintf(ni->ni_macaddr), 2022c1225b52SSam Leffler ieee80211_node_refcnt(ni)+1); 2023c1225b52SSam Leffler nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni); 2024c1225b52SSam Leffler } 2025c1225b52SSam Leffler } 2026a2cfa5b7SSam Leffler } else { 2027a2cfa5b7SSam Leffler if (IS_BCAST_PROBEREQ(wh)) 2028a2cfa5b7SSam Leffler ni = NULL; /* spam bcast probe req to all vap's */ 2029a2cfa5b7SSam Leffler else 2030c1225b52SSam Leffler ieee80211_ref_node(ni); 2031a2cfa5b7SSam Leffler } 2032c1225b52SSam Leffler IEEE80211_NODE_UNLOCK(nt); 2033c1225b52SSam Leffler 2034c1225b52SSam Leffler return ni; 2035c1225b52SSam Leffler } 2036a2cfa5b7SSam Leffler #undef IS_BCAST_PROBEREQ 2037a2cfa5b7SSam Leffler #undef IS_PROBEREQ 20388a1b9b6aSSam Leffler 20398a1b9b6aSSam Leffler /* 2040750d6d0cSSam Leffler * Return a reference to the appropriate node for sending 2041750d6d0cSSam Leffler * a data frame. This handles node discovery in adhoc networks. 2042750d6d0cSSam Leffler */ 2043750d6d0cSSam Leffler struct ieee80211_node * 20444a8e4d15SBjoern A. Zeeb _ieee80211_find_txnode(struct ieee80211vap *vap, 2045b032f27cSSam Leffler const uint8_t macaddr[IEEE80211_ADDR_LEN], 20464a8e4d15SBjoern A. Zeeb const char *func __debrefcnt_used, int line __debrefcnt_used) 2047750d6d0cSSam Leffler { 2048b032f27cSSam Leffler struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta; 2049750d6d0cSSam Leffler struct ieee80211_node *ni; 2050750d6d0cSSam Leffler 2051750d6d0cSSam Leffler /* 2052750d6d0cSSam Leffler * The destination address should be in the node table 2053c1225b52SSam Leffler * unless this is a multicast/broadcast frame. We can 2054c1225b52SSam Leffler * also optimize station mode operation, all frames go 2055c1225b52SSam Leffler * to the bss node. 2056750d6d0cSSam Leffler */ 2057750d6d0cSSam Leffler /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 20588a1b9b6aSSam Leffler IEEE80211_NODE_LOCK(nt); 2059b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_STA || 2060b032f27cSSam Leffler vap->iv_opmode == IEEE80211_M_WDS || 2061b032f27cSSam Leffler IEEE80211_IS_MULTICAST(macaddr)) 2062b032f27cSSam Leffler ni = ieee80211_ref_node(vap->iv_bss); 20631b999d64SSam Leffler else 20644a8e4d15SBjoern A. Zeeb ni = _ieee80211_find_node_locked(nt, macaddr, func, line); 20658a1b9b6aSSam Leffler IEEE80211_NODE_UNLOCK(nt); 20668a1b9b6aSSam Leffler 20678a1b9b6aSSam Leffler if (ni == NULL) { 2068b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_IBSS || 2069b032f27cSSam Leffler vap->iv_opmode == IEEE80211_M_AHDEMO) { 207090d0d036SSam Leffler /* 207190d0d036SSam Leffler * In adhoc mode cons up a node for the destination. 207290d0d036SSam Leffler * Note that we need an additional reference for the 2073b032f27cSSam Leffler * caller to be consistent with 2074b032f27cSSam Leffler * ieee80211_find_node_locked. 207590d0d036SSam Leffler */ 2076869897d2SAdrian Chadd /* 2077869897d2SAdrian Chadd * XXX TODO: this doesn't fake up 11n state; we need 2078869897d2SAdrian Chadd * to find another way to get it upgraded. 2079869897d2SAdrian Chadd */ 2080b032f27cSSam Leffler ni = ieee80211_fakeup_adhoc_node(vap, macaddr); 208190d0d036SSam Leffler if (ni != NULL) 208290d0d036SSam Leffler (void) ieee80211_ref_node(ni); 208390d0d036SSam Leffler } else { 2084b032f27cSSam Leffler IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr, 2085b032f27cSSam Leffler "no node, discard frame (%s)", __func__); 2086b032f27cSSam Leffler vap->iv_stats.is_tx_nonode++; 2087750d6d0cSSam Leffler } 2088750d6d0cSSam Leffler } 2089750d6d0cSSam Leffler return ni; 2090750d6d0cSSam Leffler } 2091750d6d0cSSam Leffler 209221888521SBjoern A. Zeeb struct ieee80211_node * 209321888521SBjoern A. Zeeb _ieee80211_ref_node(struct ieee80211_node *ni, 209421888521SBjoern A. Zeeb const char *func __debrefcnt_used, int line __debrefcnt_used) 209521888521SBjoern A. Zeeb { 209621888521SBjoern A. Zeeb 209721888521SBjoern A. Zeeb #ifdef IEEE80211_DEBUG_REFCNT 209821888521SBjoern A. Zeeb IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 209921888521SBjoern A. Zeeb "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 210021888521SBjoern A. Zeeb ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 210121888521SBjoern A. Zeeb #endif 210221888521SBjoern A. Zeeb ieee80211_node_incref(ni); 210321888521SBjoern A. Zeeb return (ni); 210421888521SBjoern A. Zeeb } 210521888521SBjoern A. Zeeb 21068a1b9b6aSSam Leffler static void 21074a8e4d15SBjoern A. Zeeb __ieee80211_free_node(struct ieee80211_node *ni) 21088a1b9b6aSSam Leffler { 21098a1b9b6aSSam Leffler struct ieee80211_node_table *nt = ni->ni_table; 21108a1b9b6aSSam Leffler 211137e9743aSSam Leffler /* 211237e9743aSSam Leffler * NB: careful about referencing the vap as it may be 211337e9743aSSam Leffler * gone if the last reference was held by a driver. 211437e9743aSSam Leffler * We know the com will always be present so it's safe 211537e9743aSSam Leffler * to use ni_ic below to reclaim resources. 211637e9743aSSam Leffler */ 211737e9743aSSam Leffler #if 0 2118b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 211949a15236SSam Leffler "%s %p<%s> in %s table\n", __func__, ni, 212049a15236SSam Leffler ether_sprintf(ni->ni_macaddr), 21218a1b9b6aSSam Leffler nt != NULL ? nt->nt_name : "<gone>"); 212237e9743aSSam Leffler #endif 212337e9743aSSam Leffler if (ni->ni_associd != 0) { 212437e9743aSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 2125b032f27cSSam Leffler if (vap->iv_aid_bitmap != NULL) 2126b032f27cSSam Leffler IEEE80211_AID_CLR(vap, ni->ni_associd); 212737e9743aSSam Leffler } 2128d2e877f0SAndriy Voskoboinyk if (nt != NULL) 2129d2e877f0SAndriy Voskoboinyk ieee80211_del_node_nt(nt, ni); 213037e9743aSSam Leffler ni->ni_ic->ic_node_free(ni); 21318a1b9b6aSSam Leffler } 21328a1b9b6aSSam Leffler 2133eca3b4fcSAdrian Chadd /* 2134eca3b4fcSAdrian Chadd * Clear any entry in the unicast key mapping table. 2135eca3b4fcSAdrian Chadd */ 2136eca3b4fcSAdrian Chadd static int 2137eca3b4fcSAdrian Chadd node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 2138eca3b4fcSAdrian Chadd { 2139eca3b4fcSAdrian Chadd ieee80211_keyix keyix; 2140eca3b4fcSAdrian Chadd 2141eca3b4fcSAdrian Chadd keyix = ni->ni_ucastkey.wk_rxkeyix; 2142eca3b4fcSAdrian Chadd if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax && 2143eca3b4fcSAdrian Chadd nt->nt_keyixmap[keyix] == ni) { 2144eca3b4fcSAdrian Chadd IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 2145eca3b4fcSAdrian Chadd "%s: %p<%s> clear key map entry %u\n", 2146eca3b4fcSAdrian Chadd __func__, ni, ether_sprintf(ni->ni_macaddr), keyix); 2147eca3b4fcSAdrian Chadd nt->nt_keyixmap[keyix] = NULL; 2148eca3b4fcSAdrian Chadd ieee80211_node_decref(ni); 2149eca3b4fcSAdrian Chadd return 1; 2150eca3b4fcSAdrian Chadd } 2151eca3b4fcSAdrian Chadd 2152eca3b4fcSAdrian Chadd return 0; 2153eca3b4fcSAdrian Chadd } 2154eca3b4fcSAdrian Chadd 21558a1b9b6aSSam Leffler void 21564a8e4d15SBjoern A. Zeeb _ieee80211_free_node(struct ieee80211_node *ni, 21574a8e4d15SBjoern A. Zeeb const char *func __debrefcnt_used, int line __debrefcnt_used) 21588a1b9b6aSSam Leffler { 21598a1b9b6aSSam Leffler struct ieee80211_node_table *nt = ni->ni_table; 21608a1b9b6aSSam Leffler 21618a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT 2162b032f27cSSam Leffler IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 216349a15236SSam Leffler "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 21648a1b9b6aSSam Leffler ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 21658a1b9b6aSSam Leffler #endif 2166c1225b52SSam Leffler if (nt != NULL) { 2167c1225b52SSam Leffler IEEE80211_NODE_LOCK(nt); 21688a1b9b6aSSam Leffler if (ieee80211_node_dectestref(ni)) { 21698a1b9b6aSSam Leffler /* 2170c1225b52SSam Leffler * Last reference, reclaim state. 21718a1b9b6aSSam Leffler */ 21724a8e4d15SBjoern A. Zeeb __ieee80211_free_node(ni); 2173eca3b4fcSAdrian Chadd } else if (ieee80211_node_refcnt(ni) == 1) 2174eca3b4fcSAdrian Chadd if (node_clear_keyixmap(nt, ni)) 21754a8e4d15SBjoern A. Zeeb __ieee80211_free_node(ni); 2176c1225b52SSam Leffler IEEE80211_NODE_UNLOCK(nt); 2177c1225b52SSam Leffler } else { 2178c1225b52SSam Leffler if (ieee80211_node_dectestref(ni)) 21794a8e4d15SBjoern A. Zeeb __ieee80211_free_node(ni); 2180c1225b52SSam Leffler } 2181c1225b52SSam Leffler } 2182c1225b52SSam Leffler 2183c1225b52SSam Leffler /* 2184c1225b52SSam Leffler * Reclaim a unicast key and clear any key cache state. 2185c1225b52SSam Leffler */ 2186c1225b52SSam Leffler int 2187c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni) 2188c1225b52SSam Leffler { 218937e9743aSSam Leffler struct ieee80211com *ic = ni->ni_ic; 219037e9743aSSam Leffler struct ieee80211_node_table *nt = &ic->ic_sta; 2191c1225b52SSam Leffler struct ieee80211_node *nikey; 2192c1225b52SSam Leffler ieee80211_keyix keyix; 2193c1225b52SSam Leffler int isowned, status; 2194c1225b52SSam Leffler 2195c1225b52SSam Leffler /* 2196c1225b52SSam Leffler * NB: We must beware of LOR here; deleting the key 2197c1225b52SSam Leffler * can cause the crypto layer to block traffic updates 2198c1225b52SSam Leffler * which can generate a LOR against the node table lock; 2199c1225b52SSam Leffler * grab it here and stash the key index for our use below. 2200c1225b52SSam Leffler * 2201c1225b52SSam Leffler * Must also beware of recursion on the node table lock. 2202c1225b52SSam Leffler * When called from node_cleanup we may already have 2203c1225b52SSam Leffler * the node table lock held. Unfortunately there's no 2204c1225b52SSam Leffler * way to separate out this path so we must do this 2205c1225b52SSam Leffler * conditionally. 2206c1225b52SSam Leffler */ 2207c1225b52SSam Leffler isowned = IEEE80211_NODE_IS_LOCKED(nt); 2208c1225b52SSam Leffler if (!isowned) 2209c1225b52SSam Leffler IEEE80211_NODE_LOCK(nt); 221037e9743aSSam Leffler nikey = NULL; 221137e9743aSSam Leffler status = 1; /* NB: success */ 2212ca92652aSSam Leffler if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) { 2213c1225b52SSam Leffler keyix = ni->ni_ucastkey.wk_rxkeyix; 221437e9743aSSam Leffler status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey); 2215c1225b52SSam Leffler if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) { 2216c1225b52SSam Leffler nikey = nt->nt_keyixmap[keyix]; 2217c2ede4b3SMartin Blapp nt->nt_keyixmap[keyix] = NULL; 221837e9743aSSam Leffler } 221937e9743aSSam Leffler } 2220c1225b52SSam Leffler if (!isowned) 2221b032f27cSSam Leffler IEEE80211_NODE_UNLOCK(nt); 2222c1225b52SSam Leffler 2223c1225b52SSam Leffler if (nikey != NULL) { 2224c1225b52SSam Leffler KASSERT(nikey == ni, 2225c1225b52SSam Leffler ("key map out of sync, ni %p nikey %p", ni, nikey)); 222637e9743aSSam Leffler IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 2227c1225b52SSam Leffler "%s: delete key map entry %p<%s> refcnt %d\n", 2228c1225b52SSam Leffler __func__, ni, ether_sprintf(ni->ni_macaddr), 2229c1225b52SSam Leffler ieee80211_node_refcnt(ni)-1); 2230c1225b52SSam Leffler ieee80211_free_node(ni); 2231c1225b52SSam Leffler } 2232c1225b52SSam Leffler return status; 2233c1225b52SSam Leffler } 22341a1e1d21SSam Leffler 2235303ebc3cSSam Leffler /* 22368a1b9b6aSSam Leffler * Reclaim a node. If this is the last reference count then 22378a1b9b6aSSam Leffler * do the normal free work. Otherwise remove it from the node 22388a1b9b6aSSam Leffler * table and mark it gone by clearing the back-reference. 22398a1b9b6aSSam Leffler */ 22408a1b9b6aSSam Leffler static void 22418a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 22428a1b9b6aSSam Leffler { 2243c1225b52SSam Leffler 2244c1225b52SSam Leffler IEEE80211_NODE_LOCK_ASSERT(nt); 22458a1b9b6aSSam Leffler 2246b032f27cSSam Leffler IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 224749a15236SSam Leffler "%s: remove %p<%s> from %s table, refcnt %d\n", 224849a15236SSam Leffler __func__, ni, ether_sprintf(ni->ni_macaddr), 224949a15236SSam Leffler nt->nt_name, ieee80211_node_refcnt(ni)-1); 2250c1225b52SSam Leffler /* 2251c1225b52SSam Leffler * Clear any entry in the unicast key mapping table. 2252c1225b52SSam Leffler * We need to do it here so rx lookups don't find it 2253c1225b52SSam Leffler * in the mapping table even if it's not in the hash 2254c1225b52SSam Leffler * table. We cannot depend on the mapping table entry 2255c1225b52SSam Leffler * being cleared because the node may not be free'd. 2256c1225b52SSam Leffler */ 2257eca3b4fcSAdrian Chadd (void)node_clear_keyixmap(nt, ni); 22588a1b9b6aSSam Leffler if (!ieee80211_node_dectestref(ni)) { 22598a1b9b6aSSam Leffler /* 22608a1b9b6aSSam Leffler * Other references are present, just remove the 22618a1b9b6aSSam Leffler * node from the table so it cannot be found. When 22628a1b9b6aSSam Leffler * the references are dropped storage will be 2263acc4f7f5SSam Leffler * reclaimed. 22648a1b9b6aSSam Leffler */ 2265d2e877f0SAndriy Voskoboinyk ieee80211_del_node_nt(nt, ni); 22668a1b9b6aSSam Leffler } else 22674a8e4d15SBjoern A. Zeeb __ieee80211_free_node(ni); 22688a1b9b6aSSam Leffler } 22698a1b9b6aSSam Leffler 2270b032f27cSSam Leffler /* 2271b032f27cSSam Leffler * Node table support. 2272b032f27cSSam Leffler */ 2273b032f27cSSam Leffler 2274b032f27cSSam Leffler static void 2275b032f27cSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic, 2276b032f27cSSam Leffler struct ieee80211_node_table *nt, 2277b032f27cSSam Leffler const char *name, int inact, int keyixmax) 2278b032f27cSSam Leffler { 2279b032f27cSSam Leffler 2280b032f27cSSam Leffler nt->nt_ic = ic; 2281c8f5794eSGleb Smirnoff IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name); 2282b032f27cSSam Leffler TAILQ_INIT(&nt->nt_node); 2283d2e877f0SAndriy Voskoboinyk nt->nt_count = 0; 2284b032f27cSSam Leffler nt->nt_name = name; 2285b032f27cSSam Leffler nt->nt_inact_init = inact; 2286b032f27cSSam Leffler nt->nt_keyixmax = keyixmax; 2287b032f27cSSam Leffler if (nt->nt_keyixmax > 0) { 2288b9b53389SAdrian Chadd nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC( 2289c5abbba3SDag-Erling Smørgrav keyixmax * sizeof(struct ieee80211_node *), 2290b9b53389SAdrian Chadd M_80211_NODE, 2291b9b53389SAdrian Chadd IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 2292b032f27cSSam Leffler if (nt->nt_keyixmap == NULL) 2293c8f5794eSGleb Smirnoff ic_printf(ic, 2294b032f27cSSam Leffler "Cannot allocate key index map with %u entries\n", 2295b032f27cSSam Leffler keyixmax); 2296b032f27cSSam Leffler } else 2297b032f27cSSam Leffler nt->nt_keyixmap = NULL; 2298b032f27cSSam Leffler } 2299b032f27cSSam Leffler 2300b032f27cSSam Leffler static void 2301b032f27cSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt, 2302b032f27cSSam Leffler struct ieee80211vap *match) 2303b032f27cSSam Leffler { 2304b032f27cSSam Leffler struct ieee80211_node *ni, *next; 2305b032f27cSSam Leffler 2306b032f27cSSam Leffler IEEE80211_NODE_LOCK(nt); 2307b032f27cSSam Leffler TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) { 2308b032f27cSSam Leffler if (match != NULL && ni->ni_vap != match) 2309b032f27cSSam Leffler continue; 2310b032f27cSSam Leffler /* XXX can this happen? if so need's work */ 2311b032f27cSSam Leffler if (ni->ni_associd != 0) { 2312b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 2313b032f27cSSam Leffler 2314b032f27cSSam Leffler if (vap->iv_auth->ia_node_leave != NULL) 2315b032f27cSSam Leffler vap->iv_auth->ia_node_leave(ni); 2316b032f27cSSam Leffler if (vap->iv_aid_bitmap != NULL) 2317b032f27cSSam Leffler IEEE80211_AID_CLR(vap, ni->ni_associd); 2318b032f27cSSam Leffler } 2319b032f27cSSam Leffler ni->ni_wdsvap = NULL; /* clear reference */ 23208a1b9b6aSSam Leffler node_reclaim(nt, ni); 23218a1b9b6aSSam Leffler } 2322b032f27cSSam Leffler if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) { 2323b032f27cSSam Leffler /* 2324b032f27cSSam Leffler * Make a separate pass to clear references to this vap 2325b032f27cSSam Leffler * held by DWDS entries. They will not be matched above 2326b032f27cSSam Leffler * because ni_vap will point to the ap vap but we still 2327b032f27cSSam Leffler * need to clear ni_wdsvap when the WDS vap is destroyed 2328b032f27cSSam Leffler * and/or reset. 2329b032f27cSSam Leffler */ 2330b032f27cSSam Leffler TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) 2331b032f27cSSam Leffler if (ni->ni_wdsvap == match) 2332b032f27cSSam Leffler ni->ni_wdsvap = NULL; 2333b032f27cSSam Leffler } 2334b032f27cSSam Leffler IEEE80211_NODE_UNLOCK(nt); 2335b032f27cSSam Leffler } 2336b032f27cSSam Leffler 2337b032f27cSSam Leffler static void 2338b032f27cSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 2339b032f27cSSam Leffler { 2340b032f27cSSam Leffler ieee80211_node_table_reset(nt, NULL); 2341b032f27cSSam Leffler if (nt->nt_keyixmap != NULL) { 2342b032f27cSSam Leffler #ifdef DIAGNOSTIC 2343b032f27cSSam Leffler /* XXX verify all entries are NULL */ 2344b032f27cSSam Leffler int i; 2345b032f27cSSam Leffler for (i = 0; i < nt->nt_keyixmax; i++) 2346b032f27cSSam Leffler if (nt->nt_keyixmap[i] != NULL) 2347b032f27cSSam Leffler printf("%s: %s[%u] still active\n", __func__, 2348b032f27cSSam Leffler nt->nt_name, i); 2349b032f27cSSam Leffler #endif 2350b9b53389SAdrian Chadd IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE); 2351b032f27cSSam Leffler nt->nt_keyixmap = NULL; 2352b032f27cSSam Leffler } 2353b032f27cSSam Leffler IEEE80211_NODE_LOCK_DESTROY(nt); 23548a1b9b6aSSam Leffler } 23558a1b9b6aSSam Leffler 23568a1b9b6aSSam Leffler static void 235703475bd0SAdrian Chadd timeout_stations(void *arg __unused, struct ieee80211_node *ni) 23581a1e1d21SSam Leffler { 235903475bd0SAdrian Chadd struct ieee80211com *ic = ni->ni_ic; 236003475bd0SAdrian Chadd struct ieee80211vap *vap = ni->ni_vap; 23611a1e1d21SSam Leffler 2362b032f27cSSam Leffler /* 2363b032f27cSSam Leffler * Only process stations when in RUN state. This 2364b032f27cSSam Leffler * insures, for example, that we don't timeout an 2365b032f27cSSam Leffler * inactive station during CAC. Note that CSA state 2366b032f27cSSam Leffler * is actually handled in ieee80211_node_timeout as 2367b032f27cSSam Leffler * it applies to more than timeout processing. 2368b032f27cSSam Leffler */ 2369b032f27cSSam Leffler if (vap->iv_state != IEEE80211_S_RUN) 237003475bd0SAdrian Chadd return; 237103475bd0SAdrian Chadd /* 237203475bd0SAdrian Chadd * Ignore entries for which have yet to receive an 237303475bd0SAdrian Chadd * authentication frame. These are transient and 237403475bd0SAdrian Chadd * will be reclaimed when the last reference to them 237503475bd0SAdrian Chadd * goes away (when frame xmits complete). 237603475bd0SAdrian Chadd */ 2377b032f27cSSam Leffler if ((vap->iv_opmode == IEEE80211_M_HOSTAP || 2378b032f27cSSam Leffler vap->iv_opmode == IEEE80211_M_STA) && 237944b666cdSSam Leffler (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 238003475bd0SAdrian Chadd return; 2381ebdda46cSSam Leffler /* 23828a1b9b6aSSam Leffler * Free fragment if not needed anymore 23838a1b9b6aSSam Leffler * (last fragment older than 1s). 2384b032f27cSSam Leffler * XXX doesn't belong here, move to node_age 23850a915fadSSam Leffler */ 23868a1b9b6aSSam Leffler if (ni->ni_rxfrag[0] != NULL && 23878a1b9b6aSSam Leffler ticks > ni->ni_rxfragstamp + hz) { 23888a1b9b6aSSam Leffler m_freem(ni->ni_rxfrag[0]); 23898a1b9b6aSSam Leffler ni->ni_rxfrag[0] = NULL; 23908a1b9b6aSSam Leffler } 2391be1054edSSam Leffler if (ni->ni_inact > 0) { 2392c066143cSSam Leffler ni->ni_inact--; 2393be1054edSSam Leffler IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 2394be1054edSSam Leffler "%s: inact %u inact_reload %u nrates %u", 2395be1054edSSam Leffler __func__, ni->ni_inact, ni->ni_inact_reload, 2396be1054edSSam Leffler ni->ni_rates.rs_nrates); 2397be1054edSSam Leffler } 2398ce647032SSam Leffler /* 2399ce647032SSam Leffler * Special case ourself; we may be idle for extended periods 2400ce647032SSam Leffler * of time and regardless reclaiming our state is wrong. 2401b032f27cSSam Leffler * XXX run ic_node_age 2402ce647032SSam Leffler */ 240303475bd0SAdrian Chadd /* XXX before inact decrement? */ 2404b032f27cSSam Leffler if (ni == vap->iv_bss) 240503475bd0SAdrian Chadd return; 2406b032f27cSSam Leffler if (ni->ni_associd != 0 || 2407b032f27cSSam Leffler (vap->iv_opmode == IEEE80211_M_IBSS || 2408b032f27cSSam Leffler vap->iv_opmode == IEEE80211_M_AHDEMO)) { 24098a1b9b6aSSam Leffler /* 2410b032f27cSSam Leffler * Age/drain resources held by the station. 24118a1b9b6aSSam Leffler */ 2412b032f27cSSam Leffler ic->ic_node_age(ni); 24138a1b9b6aSSam Leffler /* 24148a1b9b6aSSam Leffler * Probe the station before time it out. We 24158a1b9b6aSSam Leffler * send a null data frame which may not be 24168a1b9b6aSSam Leffler * universally supported by drivers (need it 24178a1b9b6aSSam Leffler * for ps-poll support so it should be...). 241868e8e04eSSam Leffler * 241968e8e04eSSam Leffler * XXX don't probe the station unless we've 242068e8e04eSSam Leffler * received a frame from them (and have 242168e8e04eSSam Leffler * some idea of the rates they are capable 242268e8e04eSSam Leffler * of); this will get fixed more properly 242368e8e04eSSam Leffler * soon with better handling of the rate set. 24248a1b9b6aSSam Leffler */ 2425b032f27cSSam Leffler if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) && 2426c066143cSSam Leffler (0 < ni->ni_inact && 2427b032f27cSSam Leffler ni->ni_inact <= vap->iv_inact_probe) && 242868e8e04eSSam Leffler ni->ni_rates.rs_nrates != 0) { 2429b032f27cSSam Leffler IEEE80211_NOTE(vap, 2430f66d97f6SSam Leffler IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, 2431f66d97f6SSam Leffler ni, "%s", 2432f66d97f6SSam Leffler "probe station due to inactivity"); 243319ad2dd7SSam Leffler /* 243403475bd0SAdrian Chadd * Grab a reference so the node cannot 243503475bd0SAdrian Chadd * be reclaimed before we send the frame. 243603475bd0SAdrian Chadd * ieee80211_send_nulldata understands 243703475bd0SAdrian Chadd * we've done this and reclaims the 243819ad2dd7SSam Leffler * ref for us as needed. 243919ad2dd7SSam Leffler */ 244003475bd0SAdrian Chadd /* XXX fix this (not required anymore). */ 244119ad2dd7SSam Leffler ieee80211_ref_node(ni); 244203475bd0SAdrian Chadd /* XXX useless */ 2443f62121ceSSam Leffler ieee80211_send_nulldata(ni); 24448a1b9b6aSSam Leffler /* XXX stat? */ 244503475bd0SAdrian Chadd return; 24468a1b9b6aSSam Leffler } 24478a1b9b6aSSam Leffler } 2448b032f27cSSam Leffler if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) && 2449c066143cSSam Leffler ni->ni_inact <= 0) { 2450b032f27cSSam Leffler IEEE80211_NOTE(vap, 2451f66d97f6SSam Leffler IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni, 2452f66d97f6SSam Leffler "station timed out due to inactivity " 2453f66d97f6SSam Leffler "(refcnt %u)", ieee80211_node_refcnt(ni)); 24548a1b9b6aSSam Leffler /* 24558a1b9b6aSSam Leffler * Send a deauthenticate frame and drop the station. 24568a1b9b6aSSam Leffler * This is somewhat complicated due to reference counts 24578a1b9b6aSSam Leffler * and locking. At this point a station will typically 245803475bd0SAdrian Chadd * have a reference count of 2. ieee80211_node_leave 24598a1b9b6aSSam Leffler * will do a "free" of the node which will drop the 24608a1b9b6aSSam Leffler * reference count. But in the meantime a reference 24618a1b9b6aSSam Leffler * wil be held by the deauth frame. The actual reclaim 24628a1b9b6aSSam Leffler * of the node will happen either after the tx is 24638a1b9b6aSSam Leffler * completed or by ieee80211_node_leave. 24648a1b9b6aSSam Leffler */ 24658a1b9b6aSSam Leffler if (ni->ni_associd != 0) { 2466b032f27cSSam Leffler IEEE80211_SEND_MGMT(ni, 24671a1e1d21SSam Leffler IEEE80211_FC0_SUBTYPE_DEAUTH, 24681a1e1d21SSam Leffler IEEE80211_REASON_AUTH_EXPIRE); 24698a1b9b6aSSam Leffler } 2470b032f27cSSam Leffler ieee80211_node_leave(ni); 2471b032f27cSSam Leffler vap->iv_stats.is_node_timeout++; 2472303ebc3cSSam Leffler } 24731a1e1d21SSam Leffler } 24748a1b9b6aSSam Leffler 247503475bd0SAdrian Chadd /* 247603475bd0SAdrian Chadd * Timeout inactive stations and do related housekeeping. 247703475bd0SAdrian Chadd */ 247803475bd0SAdrian Chadd static void 247903475bd0SAdrian Chadd ieee80211_timeout_stations(struct ieee80211com *ic) 248003475bd0SAdrian Chadd { 248103475bd0SAdrian Chadd struct ieee80211_node_table *nt = &ic->ic_sta; 248203475bd0SAdrian Chadd 248303475bd0SAdrian Chadd ieee80211_iterate_nodes(nt, timeout_stations, NULL); 248468e8e04eSSam Leffler } 24858a1b9b6aSSam Leffler 2486b032f27cSSam Leffler /* 2487b032f27cSSam Leffler * Aggressively reclaim resources. This should be used 2488b032f27cSSam Leffler * only in a critical situation to reclaim mbuf resources. 2489b032f27cSSam Leffler */ 2490b032f27cSSam Leffler void 2491b032f27cSSam Leffler ieee80211_drain(struct ieee80211com *ic) 2492b032f27cSSam Leffler { 2493b032f27cSSam Leffler struct ieee80211_node_table *nt = &ic->ic_sta; 2494b032f27cSSam Leffler struct ieee80211vap *vap; 2495b032f27cSSam Leffler struct ieee80211_node *ni; 2496b032f27cSSam Leffler 2497b032f27cSSam Leffler IEEE80211_NODE_LOCK(nt); 2498b032f27cSSam Leffler TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2499b032f27cSSam Leffler /* 2500b032f27cSSam Leffler * Ignore entries for which have yet to receive an 2501b032f27cSSam Leffler * authentication frame. These are transient and 2502b032f27cSSam Leffler * will be reclaimed when the last reference to them 2503b032f27cSSam Leffler * goes away (when frame xmits complete). 2504b032f27cSSam Leffler */ 2505b032f27cSSam Leffler vap = ni->ni_vap; 2506b032f27cSSam Leffler /* 2507b032f27cSSam Leffler * Only process stations when in RUN state. This 2508b032f27cSSam Leffler * insures, for example, that we don't timeout an 2509b032f27cSSam Leffler * inactive station during CAC. Note that CSA state 2510b032f27cSSam Leffler * is actually handled in ieee80211_node_timeout as 2511b032f27cSSam Leffler * it applies to more than timeout processing. 2512b032f27cSSam Leffler */ 2513b032f27cSSam Leffler if (vap->iv_state != IEEE80211_S_RUN) 2514b032f27cSSam Leffler continue; 2515b032f27cSSam Leffler /* XXX can vap be NULL? */ 2516b032f27cSSam Leffler if ((vap->iv_opmode == IEEE80211_M_HOSTAP || 2517b032f27cSSam Leffler vap->iv_opmode == IEEE80211_M_STA) && 2518b032f27cSSam Leffler (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 2519b032f27cSSam Leffler continue; 2520b032f27cSSam Leffler /* 2521b032f27cSSam Leffler * Free fragments. 2522b032f27cSSam Leffler * XXX doesn't belong here, move to node_drain 2523b032f27cSSam Leffler */ 2524b032f27cSSam Leffler if (ni->ni_rxfrag[0] != NULL) { 2525b032f27cSSam Leffler m_freem(ni->ni_rxfrag[0]); 2526b032f27cSSam Leffler ni->ni_rxfrag[0] = NULL; 2527b032f27cSSam Leffler } 2528b032f27cSSam Leffler /* 2529b032f27cSSam Leffler * Drain resources held by the station. 2530b032f27cSSam Leffler */ 2531b032f27cSSam Leffler ic->ic_node_drain(ni); 2532b032f27cSSam Leffler } 2533b032f27cSSam Leffler IEEE80211_NODE_UNLOCK(nt); 2534b032f27cSSam Leffler } 2535b032f27cSSam Leffler 2536b032f27cSSam Leffler /* 2537f1481c8dSAdrian Chadd * Per-ieee80211vap inactivity timer callback. 2538f1481c8dSAdrian Chadd */ 2539f1481c8dSAdrian Chadd static void 2540f1481c8dSAdrian Chadd ieee80211_vap_timeout(struct ieee80211vap *vap) 2541f1481c8dSAdrian Chadd { 2542f1481c8dSAdrian Chadd 2543f1481c8dSAdrian Chadd IEEE80211_LOCK_ASSERT(vap->iv_ic); 2544f1481c8dSAdrian Chadd 2545f1481c8dSAdrian Chadd ieee80211_vap_erp_timeout(vap); 2546f1481c8dSAdrian Chadd ieee80211_ht_timeout(vap); 2547f1481c8dSAdrian Chadd ieee80211_vht_timeout(vap); 2548f1481c8dSAdrian Chadd } 2549f1481c8dSAdrian Chadd 2550f1481c8dSAdrian Chadd /* 2551b032f27cSSam Leffler * Per-ieee80211com inactivity timer callback. 2552b032f27cSSam Leffler */ 255368e8e04eSSam Leffler void 255468e8e04eSSam Leffler ieee80211_node_timeout(void *arg) 255568e8e04eSSam Leffler { 255668e8e04eSSam Leffler struct ieee80211com *ic = arg; 2557f1481c8dSAdrian Chadd struct ieee80211vap *vap; 255868e8e04eSSam Leffler 2559b032f27cSSam Leffler /* 2560b032f27cSSam Leffler * Defer timeout processing if a channel switch is pending. 2561b032f27cSSam Leffler * We typically need to be mute so not doing things that 2562b032f27cSSam Leffler * might generate frames is good to handle in one place. 2563a4641f4eSPedro F. Giffuni * Suppressing the station timeout processing may extend the 2564b032f27cSSam Leffler * lifetime of inactive stations (by not decrementing their 2565b032f27cSSam Leffler * idle counters) but this should be ok unless the CSA is 2566b032f27cSSam Leffler * active for an unusually long time. 2567b032f27cSSam Leffler */ 2568b032f27cSSam Leffler if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) { 256968e8e04eSSam Leffler ieee80211_scan_timeout(ic); 2570b032f27cSSam Leffler ieee80211_timeout_stations(ic); 25715b16c28cSSam Leffler ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT); 257268e8e04eSSam Leffler 2573b105a069SSam Leffler IEEE80211_LOCK(ic); 2574f1481c8dSAdrian Chadd TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 2575f1481c8dSAdrian Chadd ieee80211_vap_timeout(vap); 2576b105a069SSam Leffler IEEE80211_UNLOCK(ic); 2577b032f27cSSam Leffler } 257868e8e04eSSam Leffler callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz, 257968e8e04eSSam Leffler ieee80211_node_timeout, ic); 25801a1e1d21SSam Leffler } 25811a1e1d21SSam Leffler 25827d684b4bSAdrian Chadd /* 2583d2e877f0SAndriy Voskoboinyk * The same as ieee80211_iterate_nodes(), but for one vap only. 2584d2e877f0SAndriy Voskoboinyk */ 2585d2e877f0SAndriy Voskoboinyk int 2586d2e877f0SAndriy Voskoboinyk ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt, 2587d2e877f0SAndriy Voskoboinyk struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg) 2588d2e877f0SAndriy Voskoboinyk { 2589d2e877f0SAndriy Voskoboinyk struct ieee80211_node **ni_arr; 2590d2e877f0SAndriy Voskoboinyk struct ieee80211_node *ni; 2591d2e877f0SAndriy Voskoboinyk size_t size; 2592d2e877f0SAndriy Voskoboinyk int count, i; 2593d2e877f0SAndriy Voskoboinyk 2594d2e877f0SAndriy Voskoboinyk /* 2595d2e877f0SAndriy Voskoboinyk * Iterate over the node table and save an array of ref'ed nodes. 25967d684b4bSAdrian Chadd * 25977d684b4bSAdrian Chadd * This is separated out from calling the actual node function so that 25987d684b4bSAdrian Chadd * no LORs will occur. 25997d684b4bSAdrian Chadd */ 26007d684b4bSAdrian Chadd IEEE80211_NODE_LOCK(nt); 2601d2e877f0SAndriy Voskoboinyk count = nt->nt_count; 2602d2e877f0SAndriy Voskoboinyk size = count * sizeof(struct ieee80211_node *); 2603d2e877f0SAndriy Voskoboinyk ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE, 2604d2e877f0SAndriy Voskoboinyk IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 2605d2e877f0SAndriy Voskoboinyk if (ni_arr == NULL) { 2606d2e877f0SAndriy Voskoboinyk IEEE80211_NODE_UNLOCK(nt); 2607d2e877f0SAndriy Voskoboinyk return (ENOMEM); 26087d684b4bSAdrian Chadd } 2609d2e877f0SAndriy Voskoboinyk 2610d2e877f0SAndriy Voskoboinyk i = 0; 2611d2e877f0SAndriy Voskoboinyk TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2612d2e877f0SAndriy Voskoboinyk if (vap != NULL && ni->ni_vap != vap) 2613d2e877f0SAndriy Voskoboinyk continue; 2614d2e877f0SAndriy Voskoboinyk KASSERT(i < count, 2615d2e877f0SAndriy Voskoboinyk ("node array overflow (vap %p, i %d, count %d)\n", 2616d2e877f0SAndriy Voskoboinyk vap, i, count)); 26177d684b4bSAdrian Chadd ni_arr[i] = ieee80211_ref_node(ni); 26187d684b4bSAdrian Chadd i++; 26197d684b4bSAdrian Chadd } 26207d684b4bSAdrian Chadd IEEE80211_NODE_UNLOCK(nt); 26217d684b4bSAdrian Chadd 2622d2e877f0SAndriy Voskoboinyk for (i = 0; i < count; i++) { 26237d684b4bSAdrian Chadd if (ni_arr[i] == NULL) /* end of the list */ 26247d684b4bSAdrian Chadd break; 26257d684b4bSAdrian Chadd (*f)(arg, ni_arr[i]); 26267d684b4bSAdrian Chadd /* ieee80211_free_node() locks by itself */ 26277d684b4bSAdrian Chadd ieee80211_free_node(ni_arr[i]); 26287d684b4bSAdrian Chadd } 26297d684b4bSAdrian Chadd 2630b9b53389SAdrian Chadd IEEE80211_FREE(ni_arr, M_80211_NODE); 2631d2e877f0SAndriy Voskoboinyk 2632d2e877f0SAndriy Voskoboinyk return (0); 2633d2e877f0SAndriy Voskoboinyk } 2634d2e877f0SAndriy Voskoboinyk 2635d2e877f0SAndriy Voskoboinyk /* 2636d2e877f0SAndriy Voskoboinyk * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes() 2637d2e877f0SAndriy Voskoboinyk * reference in the source. 2638d2e877f0SAndriy Voskoboinyk */ 2639d2e877f0SAndriy Voskoboinyk void 2640d2e877f0SAndriy Voskoboinyk ieee80211_iterate_nodes(struct ieee80211_node_table *nt, 2641d2e877f0SAndriy Voskoboinyk ieee80211_iter_func *f, void *arg) 2642d2e877f0SAndriy Voskoboinyk { 2643d2e877f0SAndriy Voskoboinyk /* XXX no way to pass error to the caller. */ 2644d2e877f0SAndriy Voskoboinyk (void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg); 26458a1b9b6aSSam Leffler } 26468a1b9b6aSSam Leffler 26478a1b9b6aSSam Leffler void 2648c3db9d4aSBjoern A. Zeeb ieee80211_dump_node(struct ieee80211_node_table *nt __unused, 2649c3db9d4aSBjoern A. Zeeb struct ieee80211_node *ni) 26508a1b9b6aSSam Leffler { 2651c3db9d4aSBjoern A. Zeeb printf("%p: mac %s refcnt %d\n", ni, 26528a1b9b6aSSam Leffler ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 265303475bd0SAdrian Chadd printf("\tauthmode %u flags 0x%x\n", 265403475bd0SAdrian Chadd ni->ni_authmode, ni->ni_flags); 26558a1b9b6aSSam Leffler printf("\tassocid 0x%x txpower %u vlan %u\n", 26568a1b9b6aSSam Leffler ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 26578a1b9b6aSSam Leffler printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 2658801df4a5SSam Leffler ni->ni_txseqs[IEEE80211_NONQOS_TID], 2659801df4a5SSam Leffler ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT, 2660801df4a5SSam Leffler ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK, 26618a1b9b6aSSam Leffler ni->ni_rxfragstamp); 26625463c4a4SSam Leffler printf("\trssi %d noise %d intval %u capinfo 0x%x\n", 26635463c4a4SSam Leffler node_getrssi(ni), ni->ni_noise, 266468e8e04eSSam Leffler ni->ni_intval, ni->ni_capinfo); 26658a1b9b6aSSam Leffler printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 26668a1b9b6aSSam Leffler ether_sprintf(ni->ni_bssid), 26678a1b9b6aSSam Leffler ni->ni_esslen, ni->ni_essid, 2668c93be307SBjoern A. Zeeb (ni->ni_chan != IEEE80211_CHAN_ANYC) ? ni->ni_chan->ic_freq : 0, 2669c93be307SBjoern A. Zeeb (ni->ni_chan != IEEE80211_CHAN_ANYC) ? ni->ni_chan->ic_flags : 0); 2670be1054edSSam Leffler printf("\tinact %u inact_reload %u txrate %u\n", 2671be1054edSSam Leffler ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate); 267268e8e04eSSam Leffler printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n", 267368e8e04eSSam Leffler ni->ni_htcap, ni->ni_htparam, 267468e8e04eSSam Leffler ni->ni_htctlchan, ni->ni_ht2ndchan); 2675*2c8b0d62SBjoern A. Zeeb printf("\thtopmode %x htstbc %x htchw %d (%s)\n", 2676ca389486SBjoern A. Zeeb ni->ni_htopmode, ni->ni_htstbc, 2677*2c8b0d62SBjoern A. Zeeb ni->ni_chw, ieee80211_ni_chw_to_str(ni->ni_chw)); 267851172f62SAdrian Chadd printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n", 267951172f62SAdrian Chadd ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2, 268051172f62SAdrian Chadd (int) ni->ni_vht_basicmcs); 268151172f62SAdrian Chadd /* XXX VHT state */ 26828a1b9b6aSSam Leffler } 26838a1b9b6aSSam Leffler 26848a1b9b6aSSam Leffler void 26858a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt) 26868a1b9b6aSSam Leffler { 26878a1b9b6aSSam Leffler ieee80211_iterate_nodes(nt, 26888a1b9b6aSSam Leffler (ieee80211_iter_func *) ieee80211_dump_node, nt); 26898a1b9b6aSSam Leffler } 26908a1b9b6aSSam Leffler 2691f1481c8dSAdrian Chadd /* 2692f1481c8dSAdrian Chadd * Iterate over the VAPs and update their ERP beacon IEs. 2693f1481c8dSAdrian Chadd * 2694f1481c8dSAdrian Chadd * Note this must be called from the deferred ERP update task paths. 2695f1481c8dSAdrian Chadd */ 2696f1481c8dSAdrian Chadd void 26972dc4d8dcSSam Leffler ieee80211_notify_erp_locked(struct ieee80211com *ic) 2698b105a069SSam Leffler { 2699b032f27cSSam Leffler struct ieee80211vap *vap; 2700b032f27cSSam Leffler 2701b032f27cSSam Leffler IEEE80211_LOCK_ASSERT(ic); 2702b032f27cSSam Leffler 2703b032f27cSSam Leffler TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 2704b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_HOSTAP) 2705b032f27cSSam Leffler ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP); 2706b105a069SSam Leffler } 2707b105a069SSam Leffler 27088a1b9b6aSSam Leffler /* 27098a1b9b6aSSam Leffler * Handle a station joining an 11g network. 27108a1b9b6aSSam Leffler */ 27118a1b9b6aSSam Leffler static void 2712b032f27cSSam Leffler ieee80211_node_join_11g(struct ieee80211_node *ni) 27138a1b9b6aSSam Leffler { 2714b032f27cSSam Leffler struct ieee80211com *ic = ni->ni_ic; 2715d20ff6e6SAdrian Chadd struct ieee80211vap *vap = ni->ni_vap; 27168a1b9b6aSSam Leffler 27171b6167d2SSam Leffler IEEE80211_LOCK_ASSERT(ic); 27181b6167d2SSam Leffler 27198a1b9b6aSSam Leffler /* 27208a1b9b6aSSam Leffler * Station isn't capable of short slot time. Bump 27218a1b9b6aSSam Leffler * the count of long slot time stations and disable 27228a1b9b6aSSam Leffler * use of short slot time. Note that the actual switch 27238a1b9b6aSSam Leffler * over to long slot time use may not occur until the 27248a1b9b6aSSam Leffler * next beacon transmission (per sec. 7.3.1.4 of 11g). 27258a1b9b6aSSam Leffler */ 27268a1b9b6aSSam Leffler if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2727f1481c8dSAdrian Chadd vap->iv_longslotsta++; 2728b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2729b105a069SSam Leffler "station needs long slot time, count %d", 2730f1481c8dSAdrian Chadd vap->iv_longslotsta); 2731f1481c8dSAdrian Chadd /* 2732f1481c8dSAdrian Chadd * XXX TODO: this may need all VAPs checked! 2733f1481c8dSAdrian Chadd */ 273468e8e04eSSam Leffler if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) { 273568e8e04eSSam Leffler /* 273668e8e04eSSam Leffler * Don't force slot time when switched to turbo 273768e8e04eSSam Leffler * mode as non-ERP stations won't be present; this 273868e8e04eSSam Leffler * need only be done when on the normal G channel. 273968e8e04eSSam Leffler */ 2740d20ff6e6SAdrian Chadd ieee80211_vap_set_shortslottime(vap, 0); 27418a1b9b6aSSam Leffler } 274268e8e04eSSam Leffler } 27438a1b9b6aSSam Leffler /* 27448a1b9b6aSSam Leffler * If the new station is not an ERP station 27458a1b9b6aSSam Leffler * then bump the counter and enable protection 27468a1b9b6aSSam Leffler * if configured. 27478a1b9b6aSSam Leffler */ 2748b032f27cSSam Leffler if (!ieee80211_iserp_rateset(&ni->ni_rates)) { 2749f1481c8dSAdrian Chadd vap->iv_nonerpsta++; 2750b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2751b105a069SSam Leffler "station is !ERP, %d non-ERP stations associated", 2752f1481c8dSAdrian Chadd vap->iv_nonerpsta); 27538a1b9b6aSSam Leffler /* 27548a1b9b6aSSam Leffler * If station does not support short preamble 27558a1b9b6aSSam Leffler * then we must enable use of Barker preamble. 27568a1b9b6aSSam Leffler */ 27578a1b9b6aSSam Leffler if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 2758b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2759b105a069SSam Leffler "%s", "station needs long preamble"); 2760f1481c8dSAdrian Chadd vap->iv_flags |= IEEE80211_F_USEBARKER; 2761f1481c8dSAdrian Chadd vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE; 2762f1481c8dSAdrian Chadd ieee80211_vap_update_preamble(vap); 27638a1b9b6aSSam Leffler } 2764b105a069SSam Leffler /* 2765b032f27cSSam Leffler * If protection is configured and this is the first 2766b032f27cSSam Leffler * indication we should use protection, enable it. 2767b105a069SSam Leffler */ 2768f1481c8dSAdrian Chadd if (vap->iv_protmode != IEEE80211_PROT_NONE && 2769f1481c8dSAdrian Chadd vap->iv_nonerpsta == 1 && 2770f1481c8dSAdrian Chadd (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) { 2771b032f27cSSam Leffler IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 2772b105a069SSam Leffler "%s: enable use of protection\n", __func__); 2773f1481c8dSAdrian Chadd vap->iv_flags |= IEEE80211_F_USEPROT; 2774f1481c8dSAdrian Chadd ieee80211_vap_update_erp_protmode(vap); 2775b105a069SSam Leffler } 27768a1b9b6aSSam Leffler } else 27778a1b9b6aSSam Leffler ni->ni_flags |= IEEE80211_NODE_ERP; 27788a1b9b6aSSam Leffler } 27798a1b9b6aSSam Leffler 27808a1b9b6aSSam Leffler void 2781b032f27cSSam Leffler ieee80211_node_join(struct ieee80211_node *ni, int resp) 27828a1b9b6aSSam Leffler { 2783b032f27cSSam Leffler struct ieee80211com *ic = ni->ni_ic; 2784b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 27858a1b9b6aSSam Leffler int newassoc; 27868a1b9b6aSSam Leffler 27878a1b9b6aSSam Leffler if (ni->ni_associd == 0) { 278868e8e04eSSam Leffler uint16_t aid; 27898a1b9b6aSSam Leffler 2790b032f27cSSam Leffler KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap")); 27918a1b9b6aSSam Leffler /* 27928a1b9b6aSSam Leffler * It would be good to search the bitmap 27938a1b9b6aSSam Leffler * more efficiently, but this will do for now. 27948a1b9b6aSSam Leffler */ 2795b032f27cSSam Leffler for (aid = 1; aid < vap->iv_max_aid; aid++) { 2796b032f27cSSam Leffler if (!IEEE80211_AID_ISSET(vap, aid)) 27978a1b9b6aSSam Leffler break; 27988a1b9b6aSSam Leffler } 2799b032f27cSSam Leffler if (aid >= vap->iv_max_aid) { 280013f91245SSam Leffler IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY); 2801b032f27cSSam Leffler ieee80211_node_leave(ni); 28028a1b9b6aSSam Leffler return; 28038a1b9b6aSSam Leffler } 28048a1b9b6aSSam Leffler ni->ni_associd = aid | 0xc000; 28051b6167d2SSam Leffler ni->ni_jointime = time_uptime; 2806b032f27cSSam Leffler IEEE80211_LOCK(ic); 2807b032f27cSSam Leffler IEEE80211_AID_SET(vap, ni->ni_associd); 2808b032f27cSSam Leffler vap->iv_sta_assoc++; 28091b6167d2SSam Leffler 28101b6167d2SSam Leffler if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan)) 28111b6167d2SSam Leffler ieee80211_ht_node_join(ni); 281251172f62SAdrian Chadd if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan)) 281351172f62SAdrian Chadd ieee80211_vht_node_join(ni); 281468e8e04eSSam Leffler if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 281568e8e04eSSam Leffler IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 2816b032f27cSSam Leffler ieee80211_node_join_11g(ni); 28171b6167d2SSam Leffler IEEE80211_UNLOCK(ic); 28181b6167d2SSam Leffler 28191b6167d2SSam Leffler newassoc = 1; 28208a1b9b6aSSam Leffler } else 28218a1b9b6aSSam Leffler newassoc = 0; 28228a1b9b6aSSam Leffler 282351172f62SAdrian Chadd /* 282451172f62SAdrian Chadd * XXX VHT - should log VHT channel width, etc 282551172f62SAdrian Chadd */ 2826b032f27cSSam Leffler IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni, 282748f25cc3SAdrian Chadd "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s", 2828b8fcf546SSam Leffler IEEE80211_NODE_AID(ni), 2829f1481c8dSAdrian Chadd vap->iv_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 2830d20ff6e6SAdrian Chadd vap->iv_flags & IEEE80211_F_SHSLOT ? "short" : "long", 2831f1481c8dSAdrian Chadd vap->iv_flags & IEEE80211_F_USEPROT ? ", protection" : "", 283268e8e04eSSam Leffler ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "", 283351172f62SAdrian Chadd /* XXX update for VHT string */ 283468e8e04eSSam Leffler ni->ni_flags & IEEE80211_NODE_HT ? 2835ca389486SBjoern A. Zeeb (ni->ni_chw == IEEE80211_STA_RX_BW_40 ? ", HT40" : ", HT20") : "", 28361b6167d2SSam Leffler ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "", 283747bf877bSAdrian Chadd ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "", 28388c070d69SSam Leffler ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" : 28398c070d69SSam Leffler ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "", 284044f7a6edSSam Leffler ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "", 2841b032f27cSSam Leffler IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ? 284268e8e04eSSam Leffler ", fast-frames" : "", 2843b032f27cSSam Leffler IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ? 284468e8e04eSSam Leffler ", turbo" : "" 2845b8fcf546SSam Leffler ); 28468a1b9b6aSSam Leffler 2847d77148fbSSam Leffler ieee80211_node_setuptxparms(ni); 284849d2c137SBernhard Schmidt ieee80211_ratectl_node_init(ni); 28498a1b9b6aSSam Leffler /* give driver a chance to setup state like ni_txrate */ 2850736b3dc3SSam Leffler if (ic->ic_newassoc != NULL) 2851e9962332SSam Leffler ic->ic_newassoc(ni, newassoc); 2852b032f27cSSam Leffler IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS); 28538a1b9b6aSSam Leffler /* tell the authenticator about new station */ 2854b032f27cSSam Leffler if (vap->iv_auth->ia_node_join != NULL) 2855b032f27cSSam Leffler vap->iv_auth->ia_node_join(ni); 2856b032f27cSSam Leffler ieee80211_notify_node_join(ni, 2857ce8977dfSSam Leffler resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 28588a1b9b6aSSam Leffler } 28598a1b9b6aSSam Leffler 2860b105a069SSam Leffler static void 2861f1481c8dSAdrian Chadd disable_protection(struct ieee80211vap *vap) 2862b105a069SSam Leffler { 2863f1481c8dSAdrian Chadd struct ieee80211com *ic = vap->iv_ic; 2864b105a069SSam Leffler 2865f1481c8dSAdrian Chadd KASSERT(vap->iv_nonerpsta == 0 && 2866f1481c8dSAdrian Chadd (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0, 2867f1481c8dSAdrian Chadd ("%d non ERP stations, flags 0x%x", vap->iv_nonerpsta, 2868f1481c8dSAdrian Chadd vap->iv_flags_ext)); 2869f1481c8dSAdrian Chadd 2870f1481c8dSAdrian Chadd vap->iv_flags &= ~IEEE80211_F_USEPROT; 2871b105a069SSam Leffler /* XXX verify mode? */ 2872b105a069SSam Leffler if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 2873f1481c8dSAdrian Chadd vap->iv_flags |= IEEE80211_F_SHPREAMBLE; 2874f1481c8dSAdrian Chadd vap->iv_flags &= ~IEEE80211_F_USEBARKER; 2875b105a069SSam Leffler } 2876f1481c8dSAdrian Chadd ieee80211_vap_update_erp_protmode(vap); 2877f1481c8dSAdrian Chadd ieee80211_vap_update_preamble(vap); 2878b105a069SSam Leffler } 2879b105a069SSam Leffler 28808a1b9b6aSSam Leffler /* 28818a1b9b6aSSam Leffler * Handle a station leaving an 11g network. 28828a1b9b6aSSam Leffler */ 28838a1b9b6aSSam Leffler static void 2884b032f27cSSam Leffler ieee80211_node_leave_11g(struct ieee80211_node *ni) 28858a1b9b6aSSam Leffler { 2886b032f27cSSam Leffler struct ieee80211com *ic = ni->ni_ic; 2887d20ff6e6SAdrian Chadd struct ieee80211vap *vap = ni->ni_vap; 28888a1b9b6aSSam Leffler 28891b6167d2SSam Leffler IEEE80211_LOCK_ASSERT(ic); 28901b6167d2SSam Leffler 289168e8e04eSSam Leffler KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan), 2892b032f27cSSam Leffler ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq, 2893b032f27cSSam Leffler ic->ic_bsschan->ic_flags)); 28948a1b9b6aSSam Leffler 28958a1b9b6aSSam Leffler /* 28968a1b9b6aSSam Leffler * If a long slot station do the slot time bookkeeping. 28978a1b9b6aSSam Leffler */ 28988a1b9b6aSSam Leffler if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2899f1481c8dSAdrian Chadd KASSERT(vap->iv_longslotsta > 0, 2900f1481c8dSAdrian Chadd ("bogus long slot station count %d", vap->iv_longslotsta)); 2901f1481c8dSAdrian Chadd vap->iv_longslotsta--; 2902b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2903b105a069SSam Leffler "long slot time station leaves, count now %d", 2904f1481c8dSAdrian Chadd vap->iv_longslotsta); 2905f1481c8dSAdrian Chadd /* 2906f1481c8dSAdrian Chadd * XXX TODO: this may need all VAPs checked! 2907f1481c8dSAdrian Chadd */ 2908f1481c8dSAdrian Chadd if (vap->iv_longslotsta == 0) { 29098a1b9b6aSSam Leffler /* 29108a1b9b6aSSam Leffler * Re-enable use of short slot time if supported 29118a1b9b6aSSam Leffler * and not operating in IBSS mode (per spec). 29128a1b9b6aSSam Leffler */ 29138a1b9b6aSSam Leffler if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 29148a1b9b6aSSam Leffler ic->ic_opmode != IEEE80211_M_IBSS) { 2915b032f27cSSam Leffler IEEE80211_DPRINTF(ni->ni_vap, 2916b032f27cSSam Leffler IEEE80211_MSG_ASSOC, 29178a1b9b6aSSam Leffler "%s: re-enable use of short slot time\n", 29188a1b9b6aSSam Leffler __func__); 2919d20ff6e6SAdrian Chadd ieee80211_vap_set_shortslottime(vap, 1); 29208a1b9b6aSSam Leffler } 29218a1b9b6aSSam Leffler } 29228a1b9b6aSSam Leffler } 29238a1b9b6aSSam Leffler /* 29248a1b9b6aSSam Leffler * If a non-ERP station do the protection-related bookkeeping. 29258a1b9b6aSSam Leffler */ 29268a1b9b6aSSam Leffler if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 2927f1481c8dSAdrian Chadd KASSERT(vap->iv_nonerpsta > 0, 2928f1481c8dSAdrian Chadd ("bogus non-ERP station count %d", vap->iv_nonerpsta)); 2929f1481c8dSAdrian Chadd vap->iv_nonerpsta--; 2930b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 2931f1481c8dSAdrian Chadd "non-ERP station leaves, count now %d%s", vap->iv_nonerpsta, 2932f1481c8dSAdrian Chadd (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) ? 2933b105a069SSam Leffler " (non-ERP sta present)" : ""); 2934f1481c8dSAdrian Chadd if (vap->iv_nonerpsta == 0 && 2935f1481c8dSAdrian Chadd (vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) { 2936b032f27cSSam Leffler IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 29378a1b9b6aSSam Leffler "%s: disable use of protection\n", __func__); 2938f1481c8dSAdrian Chadd disable_protection(vap); 2939b105a069SSam Leffler } 2940b105a069SSam Leffler } 2941b105a069SSam Leffler } 2942b105a069SSam Leffler 2943b105a069SSam Leffler /* 2944b105a069SSam Leffler * Time out presence of an overlapping bss with non-ERP 2945b105a069SSam Leffler * stations. When operating in hostap mode we listen for 2946b105a069SSam Leffler * beacons from other stations and if we identify a non-ERP 2947b105a069SSam Leffler * station is present we enable protection. To identify 2948b105a069SSam Leffler * when all non-ERP stations are gone we time out this 2949b105a069SSam Leffler * condition. 2950b105a069SSam Leffler */ 2951b105a069SSam Leffler static void 2952f1481c8dSAdrian Chadd ieee80211_vap_erp_timeout(struct ieee80211vap *vap) 2953b105a069SSam Leffler { 2954b105a069SSam Leffler 2955f1481c8dSAdrian Chadd IEEE80211_LOCK_ASSERT(vap->iv_ic); 2956b105a069SSam Leffler 2957f1481c8dSAdrian Chadd if ((vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) && 2958f1481c8dSAdrian Chadd ieee80211_time_after(ticks, vap->iv_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) { 2959f1481c8dSAdrian Chadd IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC, 2960b032f27cSSam Leffler "%s", "age out non-ERP sta present on channel"); 2961f1481c8dSAdrian Chadd vap->iv_flags_ext &= ~IEEE80211_FEXT_NONERP_PR; 2962f1481c8dSAdrian Chadd if (vap->iv_nonerpsta == 0) 2963f1481c8dSAdrian Chadd disable_protection(vap); 29648a1b9b6aSSam Leffler } 29658a1b9b6aSSam Leffler } 29668a1b9b6aSSam Leffler 29678a1b9b6aSSam Leffler /* 29688a1b9b6aSSam Leffler * Handle bookkeeping for station deauthentication/disassociation 29698a1b9b6aSSam Leffler * when operating as an ap. 29708a1b9b6aSSam Leffler */ 29718a1b9b6aSSam Leffler void 2972b032f27cSSam Leffler ieee80211_node_leave(struct ieee80211_node *ni) 29738a1b9b6aSSam Leffler { 2974b032f27cSSam Leffler struct ieee80211com *ic = ni->ni_ic; 2975b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 297644acc00dSSam Leffler struct ieee80211_node_table *nt = ni->ni_table; 29778a1b9b6aSSam Leffler 2978b032f27cSSam Leffler IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni, 2979b105a069SSam Leffler "station with aid %d leaves", IEEE80211_NODE_AID(ni)); 29808a1b9b6aSSam Leffler 2981b032f27cSSam Leffler KASSERT(vap->iv_opmode != IEEE80211_M_STA, 2982b032f27cSSam Leffler ("unexpected operating mode %u", vap->iv_opmode)); 29838a1b9b6aSSam Leffler /* 29848a1b9b6aSSam Leffler * If node wasn't previously associated all 29858a1b9b6aSSam Leffler * we need to do is reclaim the reference. 29868a1b9b6aSSam Leffler */ 29878a1b9b6aSSam Leffler /* XXX ibss mode bypasses 11g and notification */ 29888a1b9b6aSSam Leffler if (ni->ni_associd == 0) 29898a1b9b6aSSam Leffler goto done; 29908a1b9b6aSSam Leffler /* 29918a1b9b6aSSam Leffler * Tell the authenticator the station is leaving. 29928a1b9b6aSSam Leffler * Note that we must do this before yanking the 29938a1b9b6aSSam Leffler * association id as the authenticator uses the 29948a1b9b6aSSam Leffler * associd to locate it's state block. 29958a1b9b6aSSam Leffler */ 2996b032f27cSSam Leffler if (vap->iv_auth->ia_node_leave != NULL) 2997b032f27cSSam Leffler vap->iv_auth->ia_node_leave(ni); 29981b6167d2SSam Leffler 29991b6167d2SSam Leffler IEEE80211_LOCK(ic); 3000b032f27cSSam Leffler IEEE80211_AID_CLR(vap, ni->ni_associd); 3001b032f27cSSam Leffler vap->iv_sta_assoc--; 30028a1b9b6aSSam Leffler 300351172f62SAdrian Chadd if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan)) 300451172f62SAdrian Chadd ieee80211_vht_node_leave(ni); 30051b6167d2SSam Leffler if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan)) 30061b6167d2SSam Leffler ieee80211_ht_node_leave(ni); 300768e8e04eSSam Leffler if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 300868e8e04eSSam Leffler IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 3009b032f27cSSam Leffler ieee80211_node_leave_11g(ni); 30101b6167d2SSam Leffler IEEE80211_UNLOCK(ic); 30118a1b9b6aSSam Leffler /* 30128a1b9b6aSSam Leffler * Cleanup station state. In particular clear various 30138a1b9b6aSSam Leffler * state that might otherwise be reused if the node 30148a1b9b6aSSam Leffler * is reused before the reference count goes to zero 30158a1b9b6aSSam Leffler * (and memory is reclaimed). 30168a1b9b6aSSam Leffler */ 3017b032f27cSSam Leffler ieee80211_sta_leave(ni); 30188a1b9b6aSSam Leffler done: 301944acc00dSSam Leffler /* 302044acc00dSSam Leffler * Remove the node from any table it's recorded in and 302144acc00dSSam Leffler * drop the caller's reference. Removal from the table 302244acc00dSSam Leffler * is important to insure the node is not reprocessed 302344acc00dSSam Leffler * for inactivity. 302444acc00dSSam Leffler */ 302544acc00dSSam Leffler if (nt != NULL) { 302644acc00dSSam Leffler IEEE80211_NODE_LOCK(nt); 302744acc00dSSam Leffler node_reclaim(nt, ni); 302844acc00dSSam Leffler IEEE80211_NODE_UNLOCK(nt); 302944acc00dSSam Leffler } else 30308a1b9b6aSSam Leffler ieee80211_free_node(ni); 30318a1b9b6aSSam Leffler } 30328a1b9b6aSSam Leffler 3033b032f27cSSam Leffler struct rssiinfo { 3034b032f27cSSam Leffler int rssi_samples; 3035b032f27cSSam Leffler uint32_t rssi_total; 3036b032f27cSSam Leffler }; 3037b032f27cSSam Leffler 3038b032f27cSSam Leffler static void 3039b032f27cSSam Leffler get_hostap_rssi(void *arg, struct ieee80211_node *ni) 3040b032f27cSSam Leffler { 3041b032f27cSSam Leffler struct rssiinfo *info = arg; 3042b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 3043b032f27cSSam Leffler int8_t rssi; 3044b032f27cSSam Leffler 3045b032f27cSSam Leffler /* only associated stations */ 3046b032f27cSSam Leffler if (ni->ni_associd == 0) 3047b032f27cSSam Leffler return; 3048b032f27cSSam Leffler rssi = vap->iv_ic->ic_node_getrssi(ni); 3049b032f27cSSam Leffler if (rssi != 0) { 3050b032f27cSSam Leffler info->rssi_samples++; 3051b032f27cSSam Leffler info->rssi_total += rssi; 3052b032f27cSSam Leffler } 3053b032f27cSSam Leffler } 3054b032f27cSSam Leffler 3055b032f27cSSam Leffler static void 3056b032f27cSSam Leffler get_adhoc_rssi(void *arg, struct ieee80211_node *ni) 3057b032f27cSSam Leffler { 3058b032f27cSSam Leffler struct rssiinfo *info = arg; 3059b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 3060b032f27cSSam Leffler int8_t rssi; 3061b032f27cSSam Leffler 3062b032f27cSSam Leffler /* only neighbors */ 3063b032f27cSSam Leffler /* XXX check bssid */ 3064b032f27cSSam Leffler if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 3065b032f27cSSam Leffler return; 3066b032f27cSSam Leffler rssi = vap->iv_ic->ic_node_getrssi(ni); 3067b032f27cSSam Leffler if (rssi != 0) { 3068b032f27cSSam Leffler info->rssi_samples++; 3069b032f27cSSam Leffler info->rssi_total += rssi; 3070b032f27cSSam Leffler } 3071b032f27cSSam Leffler } 3072b032f27cSSam Leffler 307359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH 307459aa14a9SRui Paulo static void 307559aa14a9SRui Paulo get_mesh_rssi(void *arg, struct ieee80211_node *ni) 307659aa14a9SRui Paulo { 307759aa14a9SRui Paulo struct rssiinfo *info = arg; 307859aa14a9SRui Paulo struct ieee80211vap *vap = ni->ni_vap; 307959aa14a9SRui Paulo int8_t rssi; 308059aa14a9SRui Paulo 308159aa14a9SRui Paulo /* only neighbors that peered successfully */ 308259aa14a9SRui Paulo if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) 308359aa14a9SRui Paulo return; 308459aa14a9SRui Paulo rssi = vap->iv_ic->ic_node_getrssi(ni); 308559aa14a9SRui Paulo if (rssi != 0) { 308659aa14a9SRui Paulo info->rssi_samples++; 308759aa14a9SRui Paulo info->rssi_total += rssi; 308859aa14a9SRui Paulo } 308959aa14a9SRui Paulo } 309059aa14a9SRui Paulo #endif /* IEEE80211_SUPPORT_MESH */ 309159aa14a9SRui Paulo 309268e8e04eSSam Leffler int8_t 3093b032f27cSSam Leffler ieee80211_getrssi(struct ieee80211vap *vap) 30948a1b9b6aSSam Leffler { 30958a1b9b6aSSam Leffler #define NZ(x) ((x) == 0 ? 1 : (x)) 3096b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 3097b032f27cSSam Leffler struct rssiinfo info; 30988a1b9b6aSSam Leffler 3099b032f27cSSam Leffler info.rssi_total = 0; 3100b032f27cSSam Leffler info.rssi_samples = 0; 3101b032f27cSSam Leffler switch (vap->iv_opmode) { 31028a1b9b6aSSam Leffler case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 31038a1b9b6aSSam Leffler case IEEE80211_M_AHDEMO: /* average of all neighbors */ 31047db788c6SAndriy Voskoboinyk ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi, 31057db788c6SAndriy Voskoboinyk &info); 3106b032f27cSSam Leffler break; 31078a1b9b6aSSam Leffler case IEEE80211_M_HOSTAP: /* average of all associated stations */ 31087db788c6SAndriy Voskoboinyk ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi, 31097db788c6SAndriy Voskoboinyk &info); 31108a1b9b6aSSam Leffler break; 311159aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH 311259aa14a9SRui Paulo case IEEE80211_M_MBSS: /* average of all mesh neighbors */ 31137db788c6SAndriy Voskoboinyk ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi, 31147db788c6SAndriy Voskoboinyk &info); 311559aa14a9SRui Paulo break; 311659aa14a9SRui Paulo #endif 31178a1b9b6aSSam Leffler case IEEE80211_M_MONITOR: /* XXX */ 31188a1b9b6aSSam Leffler case IEEE80211_M_STA: /* use stats from associated ap */ 31198a1b9b6aSSam Leffler default: 3120b032f27cSSam Leffler if (vap->iv_bss != NULL) 3121b032f27cSSam Leffler info.rssi_total = ic->ic_node_getrssi(vap->iv_bss); 3122b032f27cSSam Leffler info.rssi_samples = 1; 31238a1b9b6aSSam Leffler break; 31248a1b9b6aSSam Leffler } 3125b032f27cSSam Leffler return info.rssi_total / NZ(info.rssi_samples); 31268a1b9b6aSSam Leffler #undef NZ 31278a1b9b6aSSam Leffler } 31288a1b9b6aSSam Leffler 312968e8e04eSSam Leffler void 3130b032f27cSSam Leffler ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise) 31318a1b9b6aSSam Leffler { 31328a1b9b6aSSam Leffler 3133b032f27cSSam Leffler if (vap->iv_bss == NULL) /* NB: shouldn't happen */ 313468e8e04eSSam Leffler return; 3135b032f27cSSam Leffler vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise); 313668e8e04eSSam Leffler /* for non-station mode return avg'd rssi accounting */ 3137b032f27cSSam Leffler if (vap->iv_opmode != IEEE80211_M_STA) 3138b032f27cSSam Leffler *rssi = ieee80211_getrssi(vap); 31398a1b9b6aSSam Leffler } 3140