132176cfdSRui Paulo /*- 2f186073cSJoerg Sonnenberger * Copyright (c) 2001 Atsushi Onoe 332176cfdSRui Paulo * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 4f186073cSJoerg Sonnenberger * All rights reserved. 5f186073cSJoerg Sonnenberger * 6f186073cSJoerg Sonnenberger * Redistribution and use in source and binary forms, with or without 7f186073cSJoerg Sonnenberger * modification, are permitted provided that the following conditions 8f186073cSJoerg Sonnenberger * are met: 9f186073cSJoerg Sonnenberger * 1. Redistributions of source code must retain the above copyright 10f186073cSJoerg Sonnenberger * notice, this list of conditions and the following disclaimer. 11f186073cSJoerg Sonnenberger * 2. Redistributions in binary form must reproduce the above copyright 12f186073cSJoerg Sonnenberger * notice, this list of conditions and the following disclaimer in the 13f186073cSJoerg Sonnenberger * documentation and/or other materials provided with the distribution. 14f186073cSJoerg Sonnenberger * 15f186073cSJoerg Sonnenberger * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16f186073cSJoerg Sonnenberger * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17f186073cSJoerg Sonnenberger * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18f186073cSJoerg Sonnenberger * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19f186073cSJoerg Sonnenberger * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20f186073cSJoerg Sonnenberger * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21f186073cSJoerg Sonnenberger * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22f186073cSJoerg Sonnenberger * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23f186073cSJoerg Sonnenberger * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24f186073cSJoerg Sonnenberger * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25f186073cSJoerg Sonnenberger */ 26f186073cSJoerg Sonnenberger 27085ff963SMatthew Dillon #include <sys/cdefs.h> 28085ff963SMatthew Dillon __FBSDID("$FreeBSD$"); 29085ff963SMatthew Dillon 3032176cfdSRui Paulo #include "opt_wlan.h" 3132176cfdSRui Paulo 32f186073cSJoerg Sonnenberger #include <sys/param.h> 33f186073cSJoerg Sonnenberger #include <sys/systm.h> 34f186073cSJoerg Sonnenberger #include <sys/mbuf.h> 35f186073cSJoerg Sonnenberger #include <sys/malloc.h> 36f186073cSJoerg Sonnenberger #include <sys/kernel.h> 37f186073cSJoerg Sonnenberger 38841ab66cSSepherosa Ziehau #include <sys/socket.h> 39f186073cSJoerg Sonnenberger 40f186073cSJoerg Sonnenberger #include <net/if.h> 41085ff963SMatthew Dillon #include <net/if_var.h> 42841ab66cSSepherosa Ziehau #include <net/if_media.h> 43f186073cSJoerg Sonnenberger #include <net/ethernet.h> 44f186073cSJoerg Sonnenberger 45f186073cSJoerg Sonnenberger #include <netproto/802_11/ieee80211_var.h> 4632176cfdSRui Paulo #include <netproto/802_11/ieee80211_input.h> 4732176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG 4832176cfdSRui Paulo #include <netproto/802_11/ieee80211_superg.h> 4932176cfdSRui Paulo #endif 5032176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_TDMA 5132176cfdSRui Paulo #include <netproto/802_11/ieee80211_tdma.h> 5232176cfdSRui Paulo #endif 5332176cfdSRui Paulo #include <netproto/802_11/ieee80211_wds.h> 5432176cfdSRui Paulo #include <netproto/802_11/ieee80211_mesh.h> 554028af95SRui Paulo #include <netproto/802_11/ieee80211_ratectl.h> 56f186073cSJoerg Sonnenberger 57f186073cSJoerg Sonnenberger #include <net/bpf.h> 58f186073cSJoerg Sonnenberger 59841ab66cSSepherosa Ziehau /* 6032176cfdSRui Paulo * IEEE80211_NODE_HASHSIZE must be a power of 2. 6132176cfdSRui Paulo */ 6232176cfdSRui Paulo CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0); 6332176cfdSRui Paulo 6432176cfdSRui Paulo /* 65841ab66cSSepherosa Ziehau * Association id's are managed with a bit vector. 66841ab66cSSepherosa Ziehau */ 6732176cfdSRui Paulo #define IEEE80211_AID_SET(_vap, b) \ 6832176cfdSRui Paulo ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \ 6932176cfdSRui Paulo (1 << (IEEE80211_AID(b) % 32))) 7032176cfdSRui Paulo #define IEEE80211_AID_CLR(_vap, b) \ 7132176cfdSRui Paulo ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \ 7232176cfdSRui Paulo ~(1 << (IEEE80211_AID(b) % 32))) 7332176cfdSRui Paulo #define IEEE80211_AID_ISSET(_vap, b) \ 7432176cfdSRui Paulo ((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32))) 75f186073cSJoerg Sonnenberger 765d004897SSepherosa Ziehau #ifdef IEEE80211_DEBUG_REFCNT 775d004897SSepherosa Ziehau #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line 785d004897SSepherosa Ziehau #else 795d004897SSepherosa Ziehau #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__ 805d004897SSepherosa Ziehau #endif 815d004897SSepherosa Ziehau 8232176cfdSRui Paulo static int ieee80211_sta_join1(struct ieee80211_node *); 8332176cfdSRui Paulo 8432176cfdSRui Paulo static struct ieee80211_node *node_alloc(struct ieee80211vap *, 8532176cfdSRui Paulo const uint8_t [IEEE80211_ADDR_LEN]); 86841ab66cSSepherosa Ziehau static void node_cleanup(struct ieee80211_node *); 87841ab66cSSepherosa Ziehau static void node_free(struct ieee80211_node *); 8832176cfdSRui Paulo static void node_age(struct ieee80211_node *); 8932176cfdSRui Paulo static int8_t node_getrssi(const struct ieee80211_node *); 9032176cfdSRui Paulo static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *); 9132176cfdSRui Paulo static void node_getmimoinfo(const struct ieee80211_node *, 9232176cfdSRui Paulo struct ieee80211_mimo_info *); 93f186073cSJoerg Sonnenberger 94841ab66cSSepherosa Ziehau static void _ieee80211_free_node(struct ieee80211_node *); 95841ab66cSSepherosa Ziehau 964f898719SImre Vadász static void node_reclaim(struct ieee80211_node_table *nt, 974f898719SImre Vadász struct ieee80211_node *ni); 98841ab66cSSepherosa Ziehau static void ieee80211_node_table_init(struct ieee80211com *ic, 99841ab66cSSepherosa Ziehau struct ieee80211_node_table *nt, const char *name, 10032176cfdSRui Paulo int inact, int keymaxix); 10132176cfdSRui Paulo static void ieee80211_node_table_reset(struct ieee80211_node_table *, 10232176cfdSRui Paulo struct ieee80211vap *); 103841ab66cSSepherosa Ziehau static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt); 10432176cfdSRui Paulo static void ieee80211_erp_timeout(struct ieee80211com *); 105f186073cSJoerg Sonnenberger 106f186073cSJoerg Sonnenberger MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state"); 10732176cfdSRui Paulo MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie"); 108f186073cSJoerg Sonnenberger 109f186073cSJoerg Sonnenberger void 110841ab66cSSepherosa Ziehau ieee80211_node_attach(struct ieee80211com *ic) 111f186073cSJoerg Sonnenberger { 11232176cfdSRui Paulo /* XXX really want maxlen enforced per-sta */ 11332176cfdSRui Paulo ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8, 11432176cfdSRui Paulo "802.11 staging q"); 11532176cfdSRui Paulo ieee80211_node_table_init(ic, &ic->ic_sta, "station", 11632176cfdSRui Paulo IEEE80211_INACT_INIT, ic->ic_max_keyix); 117*4f655ef5SMatthew Dillon #if defined(__DragonFly__) 11834a60cf6SRui Paulo callout_init_mp(&ic->ic_inact); 119*4f655ef5SMatthew Dillon #else 120*4f655ef5SMatthew Dillon callout_init(&ic->ic_inact, 1); 121*4f655ef5SMatthew Dillon #endif 12232176cfdSRui Paulo callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz, 123085ff963SMatthew Dillon ieee80211_node_timeout, ic); 12432176cfdSRui Paulo 125841ab66cSSepherosa Ziehau ic->ic_node_alloc = node_alloc; 126841ab66cSSepherosa Ziehau ic->ic_node_free = node_free; 127841ab66cSSepherosa Ziehau ic->ic_node_cleanup = node_cleanup; 12832176cfdSRui Paulo ic->ic_node_age = node_age; 12932176cfdSRui Paulo ic->ic_node_drain = node_age; /* NB: same as age */ 130841ab66cSSepherosa Ziehau ic->ic_node_getrssi = node_getrssi; 13132176cfdSRui Paulo ic->ic_node_getsignal = node_getsignal; 13232176cfdSRui Paulo ic->ic_node_getmimoinfo = node_getmimoinfo; 133841ab66cSSepherosa Ziehau 134841ab66cSSepherosa Ziehau /* 13532176cfdSRui Paulo * Set flags to be propagated to all vap's; 13632176cfdSRui Paulo * these define default behaviour/configuration. 137841ab66cSSepherosa Ziehau */ 13832176cfdSRui Paulo ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */ 139f186073cSJoerg Sonnenberger } 140f186073cSJoerg Sonnenberger 141f186073cSJoerg Sonnenberger void 142841ab66cSSepherosa Ziehau ieee80211_node_detach(struct ieee80211com *ic) 143f186073cSJoerg Sonnenberger { 14432176cfdSRui Paulo 145085ff963SMatthew Dillon callout_drain(&ic->ic_inact); 146841ab66cSSepherosa Ziehau ieee80211_node_table_cleanup(&ic->ic_sta); 14732176cfdSRui Paulo ieee80211_ageq_cleanup(&ic->ic_stageq); 148841ab66cSSepherosa Ziehau } 14932176cfdSRui Paulo 15032176cfdSRui Paulo void 15132176cfdSRui Paulo ieee80211_node_vattach(struct ieee80211vap *vap) 15232176cfdSRui Paulo { 15332176cfdSRui Paulo /* NB: driver can override */ 15432176cfdSRui Paulo vap->iv_max_aid = IEEE80211_AID_DEF; 15532176cfdSRui Paulo 15632176cfdSRui Paulo /* default station inactivity timer setings */ 15732176cfdSRui Paulo vap->iv_inact_init = IEEE80211_INACT_INIT; 15832176cfdSRui Paulo vap->iv_inact_auth = IEEE80211_INACT_AUTH; 15932176cfdSRui Paulo vap->iv_inact_run = IEEE80211_INACT_RUN; 16032176cfdSRui Paulo vap->iv_inact_probe = IEEE80211_INACT_PROBE; 16132176cfdSRui Paulo 16232176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT, 16332176cfdSRui Paulo "%s: init %u auth %u run %u probe %u\n", __func__, 16432176cfdSRui Paulo vap->iv_inact_init, vap->iv_inact_auth, 16532176cfdSRui Paulo vap->iv_inact_run, vap->iv_inact_probe); 16632176cfdSRui Paulo } 16732176cfdSRui Paulo 16832176cfdSRui Paulo void 16932176cfdSRui Paulo ieee80211_node_latevattach(struct ieee80211vap *vap) 17032176cfdSRui Paulo { 17132176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 17232176cfdSRui Paulo /* XXX should we allow max aid to be zero? */ 17332176cfdSRui Paulo if (vap->iv_max_aid < IEEE80211_AID_MIN) { 17432176cfdSRui Paulo vap->iv_max_aid = IEEE80211_AID_MIN; 17532176cfdSRui Paulo if_printf(vap->iv_ifp, 17632176cfdSRui Paulo "WARNING: max aid too small, changed to %d\n", 17732176cfdSRui Paulo vap->iv_max_aid); 17832176cfdSRui Paulo } 179*4f655ef5SMatthew Dillon #if defined(__DragonFly__) 18032176cfdSRui Paulo vap->iv_aid_bitmap = (uint32_t *) kmalloc( 18132176cfdSRui Paulo howmany(vap->iv_max_aid, 32) * sizeof(uint32_t), 182fcaa651dSRui Paulo M_80211_NODE, M_INTWAIT | M_ZERO); 183*4f655ef5SMatthew Dillon #else 184*4f655ef5SMatthew Dillon vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC( 185*4f655ef5SMatthew Dillon howmany(vap->iv_max_aid, 32) * sizeof(uint32_t), 186*4f655ef5SMatthew Dillon M_80211_NODE, 187*4f655ef5SMatthew Dillon IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 188*4f655ef5SMatthew Dillon #endif 18932176cfdSRui Paulo if (vap->iv_aid_bitmap == NULL) { 19032176cfdSRui Paulo /* XXX no way to recover */ 19132176cfdSRui Paulo kprintf("%s: no memory for AID bitmap, max aid %d!\n", 19232176cfdSRui Paulo __func__, vap->iv_max_aid); 19332176cfdSRui Paulo vap->iv_max_aid = 0; 19432176cfdSRui Paulo } 19532176cfdSRui Paulo } 19632176cfdSRui Paulo 19732176cfdSRui Paulo ieee80211_reset_bss(vap); 19832176cfdSRui Paulo 19932176cfdSRui Paulo vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode); 20032176cfdSRui Paulo } 20132176cfdSRui Paulo 20232176cfdSRui Paulo void 20332176cfdSRui Paulo ieee80211_node_vdetach(struct ieee80211vap *vap) 20432176cfdSRui Paulo { 20532176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic; 20632176cfdSRui Paulo 20732176cfdSRui Paulo ieee80211_node_table_reset(&ic->ic_sta, vap); 20832176cfdSRui Paulo if (vap->iv_bss != NULL) { 20932176cfdSRui Paulo ieee80211_free_node(vap->iv_bss); 21032176cfdSRui Paulo vap->iv_bss = NULL; 21132176cfdSRui Paulo } 21232176cfdSRui Paulo if (vap->iv_aid_bitmap != NULL) { 213*4f655ef5SMatthew Dillon IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE); 21432176cfdSRui Paulo vap->iv_aid_bitmap = NULL; 215841ab66cSSepherosa Ziehau } 216841ab66cSSepherosa Ziehau } 217f186073cSJoerg Sonnenberger 218841ab66cSSepherosa Ziehau /* 219841ab66cSSepherosa Ziehau * Port authorize/unauthorize interfaces for use by an authenticator. 220841ab66cSSepherosa Ziehau */ 221841ab66cSSepherosa Ziehau 222841ab66cSSepherosa Ziehau void 223841ab66cSSepherosa Ziehau ieee80211_node_authorize(struct ieee80211_node *ni) 224841ab66cSSepherosa Ziehau { 22532176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 226841ab66cSSepherosa Ziehau 227841ab66cSSepherosa Ziehau ni->ni_flags |= IEEE80211_NODE_AUTH; 22832176cfdSRui Paulo ni->ni_inact_reload = vap->iv_inact_run; 22932176cfdSRui Paulo ni->ni_inact = ni->ni_inact_reload; 23032176cfdSRui Paulo 23132176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 23232176cfdSRui Paulo "%s: inact_reload %u", __func__, ni->ni_inact_reload); 233841ab66cSSepherosa Ziehau } 234841ab66cSSepherosa Ziehau 235841ab66cSSepherosa Ziehau void 236841ab66cSSepherosa Ziehau ieee80211_node_unauthorize(struct ieee80211_node *ni) 237841ab66cSSepherosa Ziehau { 23832176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 23932176cfdSRui Paulo 240841ab66cSSepherosa Ziehau ni->ni_flags &= ~IEEE80211_NODE_AUTH; 24132176cfdSRui Paulo ni->ni_inact_reload = vap->iv_inact_auth; 24232176cfdSRui Paulo if (ni->ni_inact > ni->ni_inact_reload) 24332176cfdSRui Paulo ni->ni_inact = ni->ni_inact_reload; 24432176cfdSRui Paulo 24532176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 24632176cfdSRui Paulo "%s: inact_reload %u inact %u", __func__, 24732176cfdSRui Paulo ni->ni_inact_reload, ni->ni_inact); 24832176cfdSRui Paulo } 24932176cfdSRui Paulo 25032176cfdSRui Paulo /* 25132176cfdSRui Paulo * Fix tx parameters for a node according to ``association state''. 25232176cfdSRui Paulo */ 25332176cfdSRui Paulo void 25432176cfdSRui Paulo ieee80211_node_setuptxparms(struct ieee80211_node *ni) 25532176cfdSRui Paulo { 25632176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 25732176cfdSRui Paulo enum ieee80211_phymode mode; 25832176cfdSRui Paulo 25932176cfdSRui Paulo if (ni->ni_flags & IEEE80211_NODE_HT) { 26032176cfdSRui Paulo if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)) 26132176cfdSRui Paulo mode = IEEE80211_MODE_11NA; 26232176cfdSRui Paulo else 26332176cfdSRui Paulo mode = IEEE80211_MODE_11NG; 26432176cfdSRui Paulo } else { /* legacy rate handling */ 26532176cfdSRui Paulo if (IEEE80211_IS_CHAN_ST(ni->ni_chan)) 26632176cfdSRui Paulo mode = IEEE80211_MODE_STURBO_A; 26732176cfdSRui Paulo else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan)) 26832176cfdSRui Paulo mode = IEEE80211_MODE_HALF; 26932176cfdSRui Paulo else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan)) 27032176cfdSRui Paulo mode = IEEE80211_MODE_QUARTER; 27132176cfdSRui Paulo /* NB: 108A should be handled as 11a */ 27232176cfdSRui Paulo else if (IEEE80211_IS_CHAN_A(ni->ni_chan)) 27332176cfdSRui Paulo mode = IEEE80211_MODE_11A; 27432176cfdSRui Paulo else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) || 27532176cfdSRui Paulo (ni->ni_flags & IEEE80211_NODE_ERP)) 27632176cfdSRui Paulo mode = IEEE80211_MODE_11G; 27732176cfdSRui Paulo else 27832176cfdSRui Paulo mode = IEEE80211_MODE_11B; 27932176cfdSRui Paulo } 28032176cfdSRui Paulo ni->ni_txparms = &vap->iv_txparms[mode]; 281841ab66cSSepherosa Ziehau } 282841ab66cSSepherosa Ziehau 283841ab66cSSepherosa Ziehau /* 284841ab66cSSepherosa Ziehau * Set/change the channel. The rate set is also updated as 285841ab66cSSepherosa Ziehau * to insure a consistent view by drivers. 28632176cfdSRui Paulo * XXX should be private but hostap needs it to deal with CSA 287841ab66cSSepherosa Ziehau */ 28832176cfdSRui Paulo void 28932176cfdSRui Paulo ieee80211_node_set_chan(struct ieee80211_node *ni, 29032176cfdSRui Paulo struct ieee80211_channel *chan) 291841ab66cSSepherosa Ziehau { 29232176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic; 29332176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 29432176cfdSRui Paulo enum ieee80211_phymode mode; 29532176cfdSRui Paulo 29632176cfdSRui Paulo KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel")); 29732176cfdSRui Paulo 298841ab66cSSepherosa Ziehau ni->ni_chan = chan; 29932176cfdSRui Paulo mode = ieee80211_chan2mode(chan); 30032176cfdSRui Paulo if (IEEE80211_IS_CHAN_HT(chan)) { 30132176cfdSRui Paulo /* 302085ff963SMatthew Dillon * We must install the legacy rate est in ni_rates and the 30332176cfdSRui Paulo * HT rate set in ni_htrates. 30432176cfdSRui Paulo */ 30532176cfdSRui Paulo ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan); 30632176cfdSRui Paulo /* 30732176cfdSRui Paulo * Setup bss tx parameters based on operating mode. We 30832176cfdSRui Paulo * use legacy rates when operating in a mixed HT+non-HT bss 30932176cfdSRui Paulo * and non-ERP rates in 11g for mixed ERP+non-ERP bss. 31032176cfdSRui Paulo */ 31132176cfdSRui Paulo if (mode == IEEE80211_MODE_11NA && 31232176cfdSRui Paulo (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0) 31332176cfdSRui Paulo mode = IEEE80211_MODE_11A; 31432176cfdSRui Paulo else if (mode == IEEE80211_MODE_11NG && 31532176cfdSRui Paulo (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0) 31632176cfdSRui Paulo mode = IEEE80211_MODE_11G; 31732176cfdSRui Paulo if (mode == IEEE80211_MODE_11G && 31832176cfdSRui Paulo (vap->iv_flags & IEEE80211_F_PUREG) == 0) 31932176cfdSRui Paulo mode = IEEE80211_MODE_11B; 320f186073cSJoerg Sonnenberger } 32132176cfdSRui Paulo ni->ni_txparms = &vap->iv_txparms[mode]; 32232176cfdSRui Paulo ni->ni_rates = *ieee80211_get_suprates(ic, chan); 323841ab66cSSepherosa Ziehau } 324841ab66cSSepherosa Ziehau 325841ab66cSSepherosa Ziehau static __inline void 326841ab66cSSepherosa Ziehau copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss) 327841ab66cSSepherosa Ziehau { 328841ab66cSSepherosa Ziehau /* propagate useful state */ 329841ab66cSSepherosa Ziehau nbss->ni_authmode = obss->ni_authmode; 330841ab66cSSepherosa Ziehau nbss->ni_txpower = obss->ni_txpower; 331841ab66cSSepherosa Ziehau nbss->ni_vlan = obss->ni_vlan; 332841ab66cSSepherosa Ziehau /* XXX statistics? */ 33332176cfdSRui Paulo /* XXX legacy WDS bssid? */ 334f186073cSJoerg Sonnenberger } 335f186073cSJoerg Sonnenberger 336f186073cSJoerg Sonnenberger void 33732176cfdSRui Paulo ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan) 338f186073cSJoerg Sonnenberger { 33932176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic; 340f186073cSJoerg Sonnenberger struct ieee80211_node *ni; 341f186073cSJoerg Sonnenberger 34232176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 343*4f655ef5SMatthew Dillon "%s: creating %s on channel %u%c\n", __func__, 34432176cfdSRui Paulo ieee80211_opmode_name[vap->iv_opmode], 345*4f655ef5SMatthew Dillon ieee80211_chan2ieee(ic, chan), 346*4f655ef5SMatthew Dillon ieee80211_channel_type_char(chan)); 347841ab66cSSepherosa Ziehau 34832176cfdSRui Paulo ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr); 349841ab66cSSepherosa Ziehau if (ni == NULL) { 350841ab66cSSepherosa Ziehau /* XXX recovery? */ 351841ab66cSSepherosa Ziehau return; 352841ab66cSSepherosa Ziehau } 35332176cfdSRui Paulo IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr); 35432176cfdSRui Paulo ni->ni_esslen = vap->iv_des_ssid[0].len; 35532176cfdSRui Paulo memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen); 35632176cfdSRui Paulo if (vap->iv_bss != NULL) 35732176cfdSRui Paulo copy_bss(ni, vap->iv_bss); 358841ab66cSSepherosa Ziehau ni->ni_intval = ic->ic_bintval; 35932176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_PRIVACY) 360f186073cSJoerg Sonnenberger ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 361f186073cSJoerg Sonnenberger if (ic->ic_phytype == IEEE80211_T_FH) { 362f186073cSJoerg Sonnenberger ni->ni_fhdwell = 200; /* XXX */ 363f186073cSJoerg Sonnenberger ni->ni_fhindex = 1; 364f186073cSJoerg Sonnenberger } 36532176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_IBSS) { 36632176cfdSRui Paulo vap->iv_flags |= IEEE80211_F_SIBSS; 367841ab66cSSepherosa Ziehau ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS; /* XXX */ 36832176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_DESBSSID) 36932176cfdSRui Paulo IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid); 37032176cfdSRui Paulo else { 371215bf50cSSepherosa Ziehau get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN); 37232176cfdSRui Paulo /* clear group bit, add local bit */ 373215bf50cSSepherosa Ziehau ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02; 374215bf50cSSepherosa Ziehau } 37532176cfdSRui Paulo } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) { 37632176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_DESBSSID) 37732176cfdSRui Paulo IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid); 378841ab66cSSepherosa Ziehau else 37932176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_TDMA 38032176cfdSRui Paulo if ((vap->iv_caps & IEEE80211_C_TDMA) == 0) 38132176cfdSRui Paulo #endif 382841ab66cSSepherosa Ziehau memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN); 38332176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH 38432176cfdSRui Paulo } else if (vap->iv_opmode == IEEE80211_M_MBSS) { 38532176cfdSRui Paulo ni->ni_meshidlen = vap->iv_mesh->ms_idlen; 38632176cfdSRui Paulo memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen); 38732176cfdSRui Paulo #endif 388841ab66cSSepherosa Ziehau } 389841ab66cSSepherosa Ziehau /* 390841ab66cSSepherosa Ziehau * Fix the channel and related attributes. 391841ab66cSSepherosa Ziehau */ 39232176cfdSRui Paulo /* clear DFS CAC state on previous channel */ 39332176cfdSRui Paulo if (ic->ic_bsschan != IEEE80211_CHAN_ANYC && 39432176cfdSRui Paulo ic->ic_bsschan->ic_freq != chan->ic_freq && 39532176cfdSRui Paulo IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) 39632176cfdSRui Paulo ieee80211_dfs_cac_clear(ic, ic->ic_bsschan); 39732176cfdSRui Paulo ic->ic_bsschan = chan; 39832176cfdSRui Paulo ieee80211_node_set_chan(ni, chan); 39932176cfdSRui Paulo ic->ic_curmode = ieee80211_chan2mode(chan); 400841ab66cSSepherosa Ziehau /* 40132176cfdSRui Paulo * Do mode-specific setup. 402841ab66cSSepherosa Ziehau */ 40332176cfdSRui Paulo if (IEEE80211_IS_CHAN_FULL(chan)) { 40432176cfdSRui Paulo if (IEEE80211_IS_CHAN_ANYG(chan)) { 40532176cfdSRui Paulo /* 40632176cfdSRui Paulo * Use a mixed 11b/11g basic rate set. 40732176cfdSRui Paulo */ 40832176cfdSRui Paulo ieee80211_setbasicrates(&ni->ni_rates, 40932176cfdSRui Paulo IEEE80211_MODE_11G); 41032176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_PUREG) { 41132176cfdSRui Paulo /* 41232176cfdSRui Paulo * Also mark OFDM rates basic so 11b 41332176cfdSRui Paulo * stations do not join (WiFi compliance). 41432176cfdSRui Paulo */ 41532176cfdSRui Paulo ieee80211_addbasicrates(&ni->ni_rates, 41632176cfdSRui Paulo IEEE80211_MODE_11A); 41732176cfdSRui Paulo } 41832176cfdSRui Paulo } else if (IEEE80211_IS_CHAN_B(chan)) { 41932176cfdSRui Paulo /* 42032176cfdSRui Paulo * Force pure 11b rate set. 42132176cfdSRui Paulo */ 42232176cfdSRui Paulo ieee80211_setbasicrates(&ni->ni_rates, 42332176cfdSRui Paulo IEEE80211_MODE_11B); 42432176cfdSRui Paulo } 425841ab66cSSepherosa Ziehau } 426841ab66cSSepherosa Ziehau 42732176cfdSRui Paulo (void) ieee80211_sta_join1(ieee80211_ref_node(ni)); 42832176cfdSRui Paulo } 42932176cfdSRui Paulo 43032176cfdSRui Paulo /* 43132176cfdSRui Paulo * Reset bss state on transition to the INIT state. 43232176cfdSRui Paulo * Clear any stations from the table (they have been 43332176cfdSRui Paulo * deauth'd) and reset the bss node (clears key, rate 43432176cfdSRui Paulo * etc. state). 43532176cfdSRui Paulo */ 436841ab66cSSepherosa Ziehau void 43732176cfdSRui Paulo ieee80211_reset_bss(struct ieee80211vap *vap) 438f186073cSJoerg Sonnenberger { 43932176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic; 440841ab66cSSepherosa Ziehau struct ieee80211_node *ni, *obss; 441841ab66cSSepherosa Ziehau 44232176cfdSRui Paulo ieee80211_node_table_reset(&ic->ic_sta, vap); 44332176cfdSRui Paulo /* XXX multi-bss: wrong */ 44432176cfdSRui Paulo ieee80211_reset_erp(ic); 445841ab66cSSepherosa Ziehau 44632176cfdSRui Paulo ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr); 4474d770dcfSSascha Wildner KASSERT(ni != NULL, ("unable to setup initial BSS node")); 44832176cfdSRui Paulo obss = vap->iv_bss; 44932176cfdSRui Paulo vap->iv_bss = ieee80211_ref_node(ni); 450841ab66cSSepherosa Ziehau if (obss != NULL) { 451841ab66cSSepherosa Ziehau copy_bss(ni, obss); 452841ab66cSSepherosa Ziehau ni->ni_intval = ic->ic_bintval; 453841ab66cSSepherosa Ziehau ieee80211_free_node(obss); 45432176cfdSRui Paulo } else 45532176cfdSRui Paulo IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr); 456841ab66cSSepherosa Ziehau } 457841ab66cSSepherosa Ziehau 458841ab66cSSepherosa Ziehau static int 45932176cfdSRui Paulo match_ssid(const struct ieee80211_node *ni, 46032176cfdSRui Paulo int nssid, const struct ieee80211_scan_ssid ssids[]) 461841ab66cSSepherosa Ziehau { 46232176cfdSRui Paulo int i; 46332176cfdSRui Paulo 46432176cfdSRui Paulo for (i = 0; i < nssid; i++) { 46532176cfdSRui Paulo if (ni->ni_esslen == ssids[i].len && 46632176cfdSRui Paulo memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0) 46732176cfdSRui Paulo return 1; 46832176cfdSRui Paulo } 46932176cfdSRui Paulo return 0; 47032176cfdSRui Paulo } 47132176cfdSRui Paulo 47232176cfdSRui Paulo /* 47332176cfdSRui Paulo * Test a node for suitability/compatibility. 47432176cfdSRui Paulo */ 47532176cfdSRui Paulo static int 47632176cfdSRui Paulo check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni) 47732176cfdSRui Paulo { 47832176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic; 47932176cfdSRui Paulo uint8_t rate; 48032176cfdSRui Paulo 48132176cfdSRui Paulo if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 48232176cfdSRui Paulo return 0; 48332176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_IBSS) { 48432176cfdSRui Paulo if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 48532176cfdSRui Paulo return 0; 48632176cfdSRui Paulo } else { 48732176cfdSRui Paulo if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 48832176cfdSRui Paulo return 0; 48932176cfdSRui Paulo } 49032176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_PRIVACY) { 49132176cfdSRui Paulo if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 49232176cfdSRui Paulo return 0; 49332176cfdSRui Paulo } else { 49432176cfdSRui Paulo /* XXX does this mean privacy is supported or required? */ 49532176cfdSRui Paulo if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 49632176cfdSRui Paulo return 0; 49732176cfdSRui Paulo } 49832176cfdSRui Paulo rate = ieee80211_fix_rate(ni, &ni->ni_rates, 49932176cfdSRui Paulo IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 50032176cfdSRui Paulo if (rate & IEEE80211_RATE_BASIC) 50132176cfdSRui Paulo return 0; 50232176cfdSRui Paulo if (vap->iv_des_nssid != 0 && 50332176cfdSRui Paulo !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid)) 50432176cfdSRui Paulo return 0; 50532176cfdSRui Paulo if ((vap->iv_flags & IEEE80211_F_DESBSSID) && 50632176cfdSRui Paulo !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid)) 50732176cfdSRui Paulo return 0; 50832176cfdSRui Paulo return 1; 50932176cfdSRui Paulo } 51032176cfdSRui Paulo 51132176cfdSRui Paulo #ifdef IEEE80211_DEBUG 51232176cfdSRui Paulo /* 51332176cfdSRui Paulo * Display node suitability/compatibility. 51432176cfdSRui Paulo */ 51532176cfdSRui Paulo static void 51632176cfdSRui Paulo check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni) 51732176cfdSRui Paulo { 51832176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic; 519f186073cSJoerg Sonnenberger uint8_t rate; 520f186073cSJoerg Sonnenberger int fail; 521f186073cSJoerg Sonnenberger 522f186073cSJoerg Sonnenberger fail = 0; 523f186073cSJoerg Sonnenberger if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan))) 524f186073cSJoerg Sonnenberger fail |= 0x01; 52532176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_IBSS) { 526f186073cSJoerg Sonnenberger if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 527f186073cSJoerg Sonnenberger fail |= 0x02; 528f186073cSJoerg Sonnenberger } else { 529f186073cSJoerg Sonnenberger if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0) 530f186073cSJoerg Sonnenberger fail |= 0x02; 531f186073cSJoerg Sonnenberger } 53232176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_PRIVACY) { 533f186073cSJoerg Sonnenberger if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 534f186073cSJoerg Sonnenberger fail |= 0x04; 535f186073cSJoerg Sonnenberger } else { 536f186073cSJoerg Sonnenberger /* XXX does this mean privacy is supported or required? */ 537f186073cSJoerg Sonnenberger if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) 538f186073cSJoerg Sonnenberger fail |= 0x04; 539f186073cSJoerg Sonnenberger } 54032176cfdSRui Paulo rate = ieee80211_fix_rate(ni, &ni->ni_rates, 54132176cfdSRui Paulo IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE); 542f186073cSJoerg Sonnenberger if (rate & IEEE80211_RATE_BASIC) 543f186073cSJoerg Sonnenberger fail |= 0x08; 54432176cfdSRui Paulo if (vap->iv_des_nssid != 0 && 54532176cfdSRui Paulo !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid)) 546f186073cSJoerg Sonnenberger fail |= 0x10; 54732176cfdSRui Paulo if ((vap->iv_flags & IEEE80211_F_DESBSSID) && 54832176cfdSRui Paulo !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid)) 549f186073cSJoerg Sonnenberger fail |= 0x20; 55032176cfdSRui Paulo 551085ff963SMatthew Dillon kprintf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr)); 552085ff963SMatthew Dillon kprintf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' '); 55332176cfdSRui Paulo kprintf(" %3d%c", 55432176cfdSRui Paulo ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' '); 555a6ec04bcSSascha Wildner kprintf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 556f186073cSJoerg Sonnenberger fail & 0x08 ? '!' : ' '); 557a6ec04bcSSascha Wildner kprintf(" %4s%c", 558f186073cSJoerg Sonnenberger (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 559f186073cSJoerg Sonnenberger (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 560f186073cSJoerg Sonnenberger "????", 561f186073cSJoerg Sonnenberger fail & 0x02 ? '!' : ' '); 562a6ec04bcSSascha Wildner kprintf(" %3s%c ", 56332176cfdSRui Paulo (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no", 564f186073cSJoerg Sonnenberger fail & 0x04 ? '!' : ' '); 565f186073cSJoerg Sonnenberger ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 566a6ec04bcSSascha Wildner kprintf("%s\n", fail & 0x10 ? "!" : ""); 567f186073cSJoerg Sonnenberger } 56832176cfdSRui Paulo #endif /* IEEE80211_DEBUG */ 569841ab66cSSepherosa Ziehau 570*4f655ef5SMatthew Dillon 571*4f655ef5SMatthew Dillon int 572*4f655ef5SMatthew Dillon ieee80211_ibss_merge_check(struct ieee80211_node *ni) 573*4f655ef5SMatthew Dillon { 574*4f655ef5SMatthew Dillon struct ieee80211vap *vap = ni->ni_vap; 575*4f655ef5SMatthew Dillon 576*4f655ef5SMatthew Dillon if (ni == vap->iv_bss || 577*4f655ef5SMatthew Dillon IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) { 578*4f655ef5SMatthew Dillon /* unchanged, nothing to do */ 579*4f655ef5SMatthew Dillon return 0; 580*4f655ef5SMatthew Dillon } 581*4f655ef5SMatthew Dillon 582*4f655ef5SMatthew Dillon if (!check_bss(vap, ni)) { 583*4f655ef5SMatthew Dillon /* capabilities mismatch */ 584*4f655ef5SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC, 585*4f655ef5SMatthew Dillon "%s: merge failed, capabilities mismatch\n", __func__); 586*4f655ef5SMatthew Dillon #ifdef IEEE80211_DEBUG 587*4f655ef5SMatthew Dillon if (ieee80211_msg_assoc(vap)) 588*4f655ef5SMatthew Dillon check_bss_debug(vap, ni); 589*4f655ef5SMatthew Dillon #endif 590*4f655ef5SMatthew Dillon vap->iv_stats.is_ibss_capmismatch++; 591*4f655ef5SMatthew Dillon return 0; 592*4f655ef5SMatthew Dillon } 593*4f655ef5SMatthew Dillon 594*4f655ef5SMatthew Dillon return 1; 595*4f655ef5SMatthew Dillon } 596*4f655ef5SMatthew Dillon 597841ab66cSSepherosa Ziehau /* 598841ab66cSSepherosa Ziehau * Handle 802.11 ad hoc network merge. The 599841ab66cSSepherosa Ziehau * convention, set by the Wireless Ethernet Compatibility Alliance 600841ab66cSSepherosa Ziehau * (WECA), is that an 802.11 station will change its BSSID to match 601841ab66cSSepherosa Ziehau * the "oldest" 802.11 ad hoc network, on the same channel, that 602841ab66cSSepherosa Ziehau * has the station's desired SSID. The "oldest" 802.11 network 603841ab66cSSepherosa Ziehau * sends beacons with the greatest TSF timestamp. 604841ab66cSSepherosa Ziehau * 605841ab66cSSepherosa Ziehau * The caller is assumed to validate TSF's before attempting a merge. 606841ab66cSSepherosa Ziehau * 607841ab66cSSepherosa Ziehau * Return !0 if the BSSID changed, 0 otherwise. 608841ab66cSSepherosa Ziehau */ 609841ab66cSSepherosa Ziehau int 610841ab66cSSepherosa Ziehau ieee80211_ibss_merge(struct ieee80211_node *ni) 611841ab66cSSepherosa Ziehau { 61232176cfdSRui Paulo #ifdef IEEE80211_DEBUG 613*4f655ef5SMatthew Dillon struct ieee80211vap *vap = ni->ni_vap; 614841ab66cSSepherosa Ziehau struct ieee80211com *ic = ni->ni_ic; 61533e5766fSSascha Wildner #endif 616841ab66cSSepherosa Ziehau 617*4f655ef5SMatthew Dillon if (! ieee80211_ibss_merge_check(ni)) 618841ab66cSSepherosa Ziehau return 0; 619*4f655ef5SMatthew Dillon 62032176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC, 6211e290df3SAntonio Huete Jimenez "%s: new bssid %s: %s preamble, %s slot time%s\n", __func__, 622085ff963SMatthew Dillon ether_sprintf(ni->ni_bssid), 623841ab66cSSepherosa Ziehau ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 624841ab66cSSepherosa Ziehau ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 625841ab66cSSepherosa Ziehau ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "" 626841ab66cSSepherosa Ziehau ); 62732176cfdSRui Paulo return ieee80211_sta_join1(ieee80211_ref_node(ni)); 62832176cfdSRui Paulo } 62932176cfdSRui Paulo 63032176cfdSRui Paulo /* 63132176cfdSRui Paulo * Calculate HT channel promotion flags for all vaps. 63232176cfdSRui Paulo * This assumes ni_chan have been setup for each vap. 63332176cfdSRui Paulo */ 63432176cfdSRui Paulo static int 63532176cfdSRui Paulo gethtadjustflags(struct ieee80211com *ic) 63632176cfdSRui Paulo { 63732176cfdSRui Paulo struct ieee80211vap *vap; 63832176cfdSRui Paulo int flags; 63932176cfdSRui Paulo 64032176cfdSRui Paulo flags = 0; 64132176cfdSRui Paulo /* XXX locking */ 64232176cfdSRui Paulo TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 64332176cfdSRui Paulo if (vap->iv_state < IEEE80211_S_RUN) 64432176cfdSRui Paulo continue; 64532176cfdSRui Paulo switch (vap->iv_opmode) { 64632176cfdSRui Paulo case IEEE80211_M_WDS: 64732176cfdSRui Paulo case IEEE80211_M_STA: 64832176cfdSRui Paulo case IEEE80211_M_AHDEMO: 64932176cfdSRui Paulo case IEEE80211_M_HOSTAP: 65032176cfdSRui Paulo case IEEE80211_M_IBSS: 65132176cfdSRui Paulo case IEEE80211_M_MBSS: 65232176cfdSRui Paulo flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan); 65332176cfdSRui Paulo break; 65432176cfdSRui Paulo default: 65532176cfdSRui Paulo break; 65632176cfdSRui Paulo } 65732176cfdSRui Paulo } 65832176cfdSRui Paulo return flags; 65932176cfdSRui Paulo } 66032176cfdSRui Paulo 66132176cfdSRui Paulo /* 66232176cfdSRui Paulo * Check if the current channel needs to change based on whether 66332176cfdSRui Paulo * any vap's are using HT20/HT40. This is used to sync the state 66432176cfdSRui Paulo * of ic_curchan after a channel width change on a running vap. 66532176cfdSRui Paulo */ 66632176cfdSRui Paulo void 66732176cfdSRui Paulo ieee80211_sync_curchan(struct ieee80211com *ic) 66832176cfdSRui Paulo { 66932176cfdSRui Paulo struct ieee80211_channel *c; 67032176cfdSRui Paulo 67132176cfdSRui Paulo c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic)); 67232176cfdSRui Paulo if (c != ic->ic_curchan) { 67332176cfdSRui Paulo ic->ic_curchan = c; 67432176cfdSRui Paulo ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan); 67532176cfdSRui Paulo ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan); 676085ff963SMatthew Dillon IEEE80211_UNLOCK(ic); 67732176cfdSRui Paulo ic->ic_set_channel(ic); 67832176cfdSRui Paulo ieee80211_radiotap_chan_change(ic); 679085ff963SMatthew Dillon IEEE80211_LOCK(ic); 68032176cfdSRui Paulo } 68132176cfdSRui Paulo } 68232176cfdSRui Paulo 68332176cfdSRui Paulo /* 68432176cfdSRui Paulo * Setup the current channel. The request channel may be 68532176cfdSRui Paulo * promoted if other vap's are operating with HT20/HT40. 68632176cfdSRui Paulo */ 68732176cfdSRui Paulo void 68832176cfdSRui Paulo ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c) 68932176cfdSRui Paulo { 69032176cfdSRui Paulo if (ic->ic_htcaps & IEEE80211_HTC_HT) { 69132176cfdSRui Paulo int flags = gethtadjustflags(ic); 69232176cfdSRui Paulo /* 69332176cfdSRui Paulo * Check for channel promotion required to support the 69432176cfdSRui Paulo * set of running vap's. This assumes we are called 69532176cfdSRui Paulo * after ni_chan is setup for each vap. 69632176cfdSRui Paulo */ 69732176cfdSRui Paulo /* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */ 69832176cfdSRui Paulo if (flags > ieee80211_htchanflags(c)) 69932176cfdSRui Paulo c = ieee80211_ht_adjust_channel(ic, c, flags); 70032176cfdSRui Paulo } 70132176cfdSRui Paulo ic->ic_bsschan = ic->ic_curchan = c; 70232176cfdSRui Paulo ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan); 70332176cfdSRui Paulo ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan); 70432176cfdSRui Paulo } 70532176cfdSRui Paulo 70632176cfdSRui Paulo /* 70732176cfdSRui Paulo * Change the current channel. The channel change is guaranteed to have 70832176cfdSRui Paulo * happened before the next state change. 70932176cfdSRui Paulo */ 71032176cfdSRui Paulo void 71132176cfdSRui Paulo ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c) 71232176cfdSRui Paulo { 71332176cfdSRui Paulo ieee80211_setupcurchan(ic, c); 71432176cfdSRui Paulo ieee80211_runtask(ic, &ic->ic_chan_task); 715841ab66cSSepherosa Ziehau } 716841ab66cSSepherosa Ziehau 717085ff963SMatthew Dillon void 718085ff963SMatthew Dillon ieee80211_update_chw(struct ieee80211com *ic) 719085ff963SMatthew Dillon { 720085ff963SMatthew Dillon 721085ff963SMatthew Dillon ieee80211_setupcurchan(ic, ic->ic_curchan); 722085ff963SMatthew Dillon ieee80211_runtask(ic, &ic->ic_chw_task); 723085ff963SMatthew Dillon } 724085ff963SMatthew Dillon 725841ab66cSSepherosa Ziehau /* 726841ab66cSSepherosa Ziehau * Join the specified IBSS/BSS network. The node is assumed to 727841ab66cSSepherosa Ziehau * be passed in with a held reference. 728841ab66cSSepherosa Ziehau */ 72932176cfdSRui Paulo static int 73032176cfdSRui Paulo ieee80211_sta_join1(struct ieee80211_node *selbs) 731841ab66cSSepherosa Ziehau { 73232176cfdSRui Paulo struct ieee80211vap *vap = selbs->ni_vap; 73332176cfdSRui Paulo struct ieee80211com *ic = selbs->ni_ic; 734841ab66cSSepherosa Ziehau struct ieee80211_node *obss; 73532176cfdSRui Paulo int canreassoc; 736841ab66cSSepherosa Ziehau 737841ab66cSSepherosa Ziehau /* 738841ab66cSSepherosa Ziehau * Committed to selbs, setup state. 739841ab66cSSepherosa Ziehau */ 74032176cfdSRui Paulo obss = vap->iv_bss; 74132176cfdSRui Paulo /* 74232176cfdSRui Paulo * Check if old+new node have the same address in which 74332176cfdSRui Paulo * case we can reassociate when operating in sta mode. 74432176cfdSRui Paulo */ 74532176cfdSRui Paulo canreassoc = (obss != NULL && 74632176cfdSRui Paulo vap->iv_state == IEEE80211_S_RUN && 74732176cfdSRui Paulo IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr)); 74832176cfdSRui Paulo vap->iv_bss = selbs; /* NB: caller assumed to bump refcnt */ 749841ab66cSSepherosa Ziehau if (obss != NULL) { 7504f898719SImre Vadász struct ieee80211_node_table *nt = obss->ni_table; 7514f898719SImre Vadász 752841ab66cSSepherosa Ziehau copy_bss(selbs, obss); 75332176cfdSRui Paulo ieee80211_node_decref(obss); /* iv_bss reference */ 7544f898719SImre Vadász 7554f898719SImre Vadász IEEE80211_NODE_LOCK(nt); 7564f898719SImre Vadász node_reclaim(nt, obss); /* station table reference */ 7574f898719SImre Vadász IEEE80211_NODE_UNLOCK(nt); 7584f898719SImre Vadász 75932176cfdSRui Paulo obss = NULL; /* NB: guard against later use */ 760841ab66cSSepherosa Ziehau } 76132176cfdSRui Paulo 76232176cfdSRui Paulo /* 76332176cfdSRui Paulo * Delete unusable rates; we've already checked 76432176cfdSRui Paulo * that the negotiated rate set is acceptable. 76532176cfdSRui Paulo */ 76632176cfdSRui Paulo ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates, 76732176cfdSRui Paulo IEEE80211_F_DODEL | IEEE80211_F_JOIN); 76832176cfdSRui Paulo 76932176cfdSRui Paulo ieee80211_setcurchan(ic, selbs->ni_chan); 770841ab66cSSepherosa Ziehau /* 771841ab66cSSepherosa Ziehau * Set the erp state (mostly the slot time) to deal with 772841ab66cSSepherosa Ziehau * the auto-select case; this should be redundant if the 773841ab66cSSepherosa Ziehau * mode is locked. 774841ab66cSSepherosa Ziehau */ 775841ab66cSSepherosa Ziehau ieee80211_reset_erp(ic); 77632176cfdSRui Paulo ieee80211_wme_initparams(vap); 777841ab66cSSepherosa Ziehau 77832176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_STA) { 77932176cfdSRui Paulo if (canreassoc) { 78032176cfdSRui Paulo /* Reassociate */ 78132176cfdSRui Paulo ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1); 78232176cfdSRui Paulo } else { 783208a1285SSepherosa Ziehau /* 78432176cfdSRui Paulo * Act as if we received a DEAUTH frame in case we 78532176cfdSRui Paulo * are invoked from the RUN state. This will cause 78632176cfdSRui Paulo * us to try to re-authenticate if we are operating 78732176cfdSRui Paulo * as a station. 788208a1285SSepherosa Ziehau */ 78932176cfdSRui Paulo ieee80211_new_state(vap, IEEE80211_S_AUTH, 79032176cfdSRui Paulo IEEE80211_FC0_SUBTYPE_DEAUTH); 791208a1285SSepherosa Ziehau } 79232176cfdSRui Paulo } else 79332176cfdSRui Paulo ieee80211_new_state(vap, IEEE80211_S_RUN, -1); 794841ab66cSSepherosa Ziehau return 1; 795841ab66cSSepherosa Ziehau } 796841ab66cSSepherosa Ziehau 79732176cfdSRui Paulo int 79832176cfdSRui Paulo ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan, 79932176cfdSRui Paulo const struct ieee80211_scan_entry *se) 80032176cfdSRui Paulo { 80132176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic; 80232176cfdSRui Paulo struct ieee80211_node *ni; 80332176cfdSRui Paulo 80432176cfdSRui Paulo ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr); 80532176cfdSRui Paulo if (ni == NULL) { 80632176cfdSRui Paulo /* XXX msg */ 80732176cfdSRui Paulo return 0; 80832176cfdSRui Paulo } 809085ff963SMatthew Dillon 81032176cfdSRui Paulo /* 81132176cfdSRui Paulo * Expand scan state into node's format. 81232176cfdSRui Paulo * XXX may not need all this stuff 81332176cfdSRui Paulo */ 81432176cfdSRui Paulo IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid); 81532176cfdSRui Paulo ni->ni_esslen = se->se_ssid[1]; 81632176cfdSRui Paulo memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen); 81732176cfdSRui Paulo ni->ni_tstamp.tsf = se->se_tstamp.tsf; 81832176cfdSRui Paulo ni->ni_intval = se->se_intval; 81932176cfdSRui Paulo ni->ni_capinfo = se->se_capinfo; 82032176cfdSRui Paulo ni->ni_chan = chan; 82132176cfdSRui Paulo ni->ni_timoff = se->se_timoff; 82232176cfdSRui Paulo ni->ni_fhdwell = se->se_fhdwell; 82332176cfdSRui Paulo ni->ni_fhindex = se->se_fhindex; 82432176cfdSRui Paulo ni->ni_erp = se->se_erp; 82532176cfdSRui Paulo IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi); 82632176cfdSRui Paulo ni->ni_noise = se->se_noise; 82732176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_STA) { 82832176cfdSRui Paulo /* NB: only infrastructure mode requires an associd */ 82932176cfdSRui Paulo ni->ni_flags |= IEEE80211_NODE_ASSOCID; 83032176cfdSRui Paulo } 83132176cfdSRui Paulo 83232176cfdSRui Paulo if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) { 83332176cfdSRui Paulo ieee80211_ies_expand(&ni->ni_ies); 83432176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG 83532176cfdSRui Paulo if (ni->ni_ies.ath_ie != NULL) 83632176cfdSRui Paulo ieee80211_parse_ath(ni, ni->ni_ies.ath_ie); 83732176cfdSRui Paulo #endif 83832176cfdSRui Paulo if (ni->ni_ies.htcap_ie != NULL) 83932176cfdSRui Paulo ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie); 84032176cfdSRui Paulo if (ni->ni_ies.htinfo_ie != NULL) 84132176cfdSRui Paulo ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie); 84232176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH 84332176cfdSRui Paulo if (ni->ni_ies.meshid_ie != NULL) 84432176cfdSRui Paulo ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie); 84532176cfdSRui Paulo #endif 84632176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_TDMA 84732176cfdSRui Paulo if (ni->ni_ies.tdma_ie != NULL) 84832176cfdSRui Paulo ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie); 84932176cfdSRui Paulo #endif 85032176cfdSRui Paulo } 85132176cfdSRui Paulo 85232176cfdSRui Paulo vap->iv_dtim_period = se->se_dtimperiod; 85332176cfdSRui Paulo vap->iv_dtim_count = 0; 85432176cfdSRui Paulo 85532176cfdSRui Paulo /* NB: must be after ni_chan is setup */ 85632176cfdSRui Paulo ieee80211_setup_rates(ni, se->se_rates, se->se_xrates, 85732176cfdSRui Paulo IEEE80211_F_DOSORT); 85832176cfdSRui Paulo if (ieee80211_iserp_rateset(&ni->ni_rates)) 85932176cfdSRui Paulo ni->ni_flags |= IEEE80211_NODE_ERP; 860085ff963SMatthew Dillon 861085ff963SMatthew Dillon /* 862085ff963SMatthew Dillon * Setup HT state for this node if it's available, otherwise 863085ff963SMatthew Dillon * non-STA modes won't pick this state up. 864085ff963SMatthew Dillon * 865085ff963SMatthew Dillon * For IBSS and related modes that don't go through an 866085ff963SMatthew Dillon * association request/response, the only appropriate place 867085ff963SMatthew Dillon * to setup the HT state is here. 868085ff963SMatthew Dillon */ 869085ff963SMatthew Dillon if (ni->ni_ies.htinfo_ie != NULL && 870085ff963SMatthew Dillon ni->ni_ies.htcap_ie != NULL && 871085ff963SMatthew Dillon vap->iv_flags_ht & IEEE80211_FHT_HT) { 872085ff963SMatthew Dillon ieee80211_ht_node_init(ni); 873085ff963SMatthew Dillon ieee80211_ht_updateparams(ni, 874085ff963SMatthew Dillon ni->ni_ies.htcap_ie, 875085ff963SMatthew Dillon ni->ni_ies.htinfo_ie); 876085ff963SMatthew Dillon ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie, 877085ff963SMatthew Dillon IEEE80211_F_JOIN | IEEE80211_F_DOBRS); 878085ff963SMatthew Dillon ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie); 879085ff963SMatthew Dillon } 880085ff963SMatthew Dillon /* XXX else check for ath FF? */ 881085ff963SMatthew Dillon /* XXX QoS? Difficult given that WME config is specific to a master */ 882085ff963SMatthew Dillon 88332176cfdSRui Paulo ieee80211_node_setuptxparms(ni); 8844fbce6bdSSascha Wildner ieee80211_ratectl_node_init(ni); 88532176cfdSRui Paulo 88632176cfdSRui Paulo return ieee80211_sta_join1(ieee80211_ref_node(ni)); 88732176cfdSRui Paulo } 88832176cfdSRui Paulo 889841ab66cSSepherosa Ziehau /* 890841ab66cSSepherosa Ziehau * Leave the specified IBSS/BSS network. The node is assumed to 891841ab66cSSepherosa Ziehau * be passed in with a held reference. 892841ab66cSSepherosa Ziehau */ 893841ab66cSSepherosa Ziehau void 89432176cfdSRui Paulo ieee80211_sta_leave(struct ieee80211_node *ni) 895841ab66cSSepherosa Ziehau { 89632176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic; 89732176cfdSRui Paulo 898841ab66cSSepherosa Ziehau ic->ic_node_cleanup(ni); 89932176cfdSRui Paulo ieee80211_notify_node_leave(ni); 90032176cfdSRui Paulo } 90132176cfdSRui Paulo 90232176cfdSRui Paulo /* 90332176cfdSRui Paulo * Send a deauthenticate frame and drop the station. 90432176cfdSRui Paulo */ 90532176cfdSRui Paulo void 90632176cfdSRui Paulo ieee80211_node_deauth(struct ieee80211_node *ni, int reason) 90732176cfdSRui Paulo { 9084f898719SImre Vadász /* NB: bump the refcnt to be sure temporary nodes are not reclaimed */ 90932176cfdSRui Paulo ieee80211_ref_node(ni); 91032176cfdSRui Paulo if (ni->ni_associd != 0) 91132176cfdSRui Paulo IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason); 91232176cfdSRui Paulo ieee80211_node_leave(ni); 91332176cfdSRui Paulo ieee80211_free_node(ni); 914f186073cSJoerg Sonnenberger } 915f186073cSJoerg Sonnenberger 916f186073cSJoerg Sonnenberger static struct ieee80211_node * 91732176cfdSRui Paulo node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 918f186073cSJoerg Sonnenberger { 919f186073cSJoerg Sonnenberger struct ieee80211_node *ni; 920841ab66cSSepherosa Ziehau 921*4f655ef5SMatthew Dillon #if defined(__DragonFly__) 92232176cfdSRui Paulo ni = (struct ieee80211_node *) kmalloc(sizeof(struct ieee80211_node), 923fcaa651dSRui Paulo M_80211_NODE, M_INTWAIT | M_ZERO); 924*4f655ef5SMatthew Dillon #else 925*4f655ef5SMatthew Dillon ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node), 926*4f655ef5SMatthew Dillon M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 927*4f655ef5SMatthew Dillon #endif 928f186073cSJoerg Sonnenberger return ni; 929f186073cSJoerg Sonnenberger } 930f186073cSJoerg Sonnenberger 931841ab66cSSepherosa Ziehau /* 93232176cfdSRui Paulo * Initialize an ie blob with the specified data. If previous 93332176cfdSRui Paulo * data exists re-use the data block. As a side effect we clear 93432176cfdSRui Paulo * all references to specific ie's; the caller is required to 93532176cfdSRui Paulo * recalculate them. 93632176cfdSRui Paulo */ 93732176cfdSRui Paulo int 93832176cfdSRui Paulo ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len) 93932176cfdSRui Paulo { 94032176cfdSRui Paulo /* NB: assumes data+len are the last fields */ 94132176cfdSRui Paulo memset(ies, 0, offsetof(struct ieee80211_ies, data)); 94232176cfdSRui Paulo if (ies->data != NULL && ies->len != len) { 94332176cfdSRui Paulo /* data size changed */ 944*4f655ef5SMatthew Dillon IEEE80211_FREE(ies->data, M_80211_NODE_IE); 94532176cfdSRui Paulo ies->data = NULL; 94632176cfdSRui Paulo } 94732176cfdSRui Paulo if (ies->data == NULL) { 948*4f655ef5SMatthew Dillon #if defined(__DragonFly__) 949*4f655ef5SMatthew Dillon ies->data = (uint8_t *) kmalloc(len, 950*4f655ef5SMatthew Dillon M_80211_NODE_IE, M_INTWAIT | M_ZERO); 951*4f655ef5SMatthew Dillon #else 952*4f655ef5SMatthew Dillon ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE, 953*4f655ef5SMatthew Dillon IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 954*4f655ef5SMatthew Dillon 955*4f655ef5SMatthew Dillon #endif 95632176cfdSRui Paulo if (ies->data == NULL) { 95732176cfdSRui Paulo ies->len = 0; 95832176cfdSRui Paulo /* NB: pointers have already been zero'd above */ 95932176cfdSRui Paulo return 0; 96032176cfdSRui Paulo } 96132176cfdSRui Paulo } 96232176cfdSRui Paulo memcpy(ies->data, data, len); 96332176cfdSRui Paulo ies->len = len; 96432176cfdSRui Paulo return 1; 96532176cfdSRui Paulo } 96632176cfdSRui Paulo 96732176cfdSRui Paulo /* 96832176cfdSRui Paulo * Reclaim storage for an ie blob. 96932176cfdSRui Paulo */ 97032176cfdSRui Paulo void 97132176cfdSRui Paulo ieee80211_ies_cleanup(struct ieee80211_ies *ies) 97232176cfdSRui Paulo { 97332176cfdSRui Paulo if (ies->data != NULL) 974*4f655ef5SMatthew Dillon IEEE80211_FREE(ies->data, M_80211_NODE_IE); 97532176cfdSRui Paulo } 97632176cfdSRui Paulo 97732176cfdSRui Paulo /* 97832176cfdSRui Paulo * Expand an ie blob data contents and to fillin individual 97932176cfdSRui Paulo * ie pointers. The data blob is assumed to be well-formed; 98032176cfdSRui Paulo * we don't do any validity checking of ie lengths. 98132176cfdSRui Paulo */ 98232176cfdSRui Paulo void 98332176cfdSRui Paulo ieee80211_ies_expand(struct ieee80211_ies *ies) 98432176cfdSRui Paulo { 98532176cfdSRui Paulo uint8_t *ie; 98632176cfdSRui Paulo int ielen; 98732176cfdSRui Paulo 98832176cfdSRui Paulo ie = ies->data; 98932176cfdSRui Paulo ielen = ies->len; 99032176cfdSRui Paulo while (ielen > 0) { 99132176cfdSRui Paulo switch (ie[0]) { 99232176cfdSRui Paulo case IEEE80211_ELEMID_VENDOR: 99332176cfdSRui Paulo if (iswpaoui(ie)) 99432176cfdSRui Paulo ies->wpa_ie = ie; 99532176cfdSRui Paulo else if (iswmeoui(ie)) 99632176cfdSRui Paulo ies->wme_ie = ie; 99732176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG 99832176cfdSRui Paulo else if (isatherosoui(ie)) 99932176cfdSRui Paulo ies->ath_ie = ie; 100032176cfdSRui Paulo #endif 100132176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_TDMA 100232176cfdSRui Paulo else if (istdmaoui(ie)) 100332176cfdSRui Paulo ies->tdma_ie = ie; 100432176cfdSRui Paulo #endif 100532176cfdSRui Paulo break; 100632176cfdSRui Paulo case IEEE80211_ELEMID_RSN: 100732176cfdSRui Paulo ies->rsn_ie = ie; 100832176cfdSRui Paulo break; 100932176cfdSRui Paulo case IEEE80211_ELEMID_HTCAP: 101032176cfdSRui Paulo ies->htcap_ie = ie; 101132176cfdSRui Paulo break; 1012085ff963SMatthew Dillon case IEEE80211_ELEMID_HTINFO: 1013085ff963SMatthew Dillon ies->htinfo_ie = ie; 1014085ff963SMatthew Dillon break; 101532176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH 101632176cfdSRui Paulo case IEEE80211_ELEMID_MESHID: 101732176cfdSRui Paulo ies->meshid_ie = ie; 101832176cfdSRui Paulo break; 101932176cfdSRui Paulo #endif 102032176cfdSRui Paulo } 102132176cfdSRui Paulo ielen -= 2 + ie[1]; 102232176cfdSRui Paulo ie += 2 + ie[1]; 102332176cfdSRui Paulo } 102432176cfdSRui Paulo } 102532176cfdSRui Paulo 102632176cfdSRui Paulo /* 1027841ab66cSSepherosa Ziehau * Reclaim any resources in a node and reset any critical 1028841ab66cSSepherosa Ziehau * state. Typically nodes are free'd immediately after, 1029841ab66cSSepherosa Ziehau * but in some cases the storage may be reused so we need 1030841ab66cSSepherosa Ziehau * to insure consistent state (should probably fix that). 1031841ab66cSSepherosa Ziehau */ 1032f186073cSJoerg Sonnenberger static void 1033841ab66cSSepherosa Ziehau node_cleanup(struct ieee80211_node *ni) 1034f186073cSJoerg Sonnenberger { 103532176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 1036841ab66cSSepherosa Ziehau struct ieee80211com *ic = ni->ni_ic; 103732176cfdSRui Paulo int i; 1038841ab66cSSepherosa Ziehau 1039841ab66cSSepherosa Ziehau /* NB: preserve ni_table */ 1040841ab66cSSepherosa Ziehau if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 104132176cfdSRui Paulo if (vap->iv_opmode != IEEE80211_M_STA) 104232176cfdSRui Paulo vap->iv_ps_sta--; 1043841ab66cSSepherosa Ziehau ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 104432176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 104532176cfdSRui Paulo "power save mode off, %u sta's in ps mode", vap->iv_ps_sta); 1046841ab66cSSepherosa Ziehau } 1047841ab66cSSepherosa Ziehau /* 104832176cfdSRui Paulo * Cleanup any HT-related state. 104932176cfdSRui Paulo */ 105032176cfdSRui Paulo if (ni->ni_flags & IEEE80211_NODE_HT) 105132176cfdSRui Paulo ieee80211_ht_node_cleanup(ni); 105232176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG 1053*4f655ef5SMatthew Dillon /* Always do FF node cleanup; for A-MSDU */ 105432176cfdSRui Paulo ieee80211_ff_node_cleanup(ni); 105532176cfdSRui Paulo #endif 105632176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH 105732176cfdSRui Paulo /* 105832176cfdSRui Paulo * Cleanup any mesh-related state. 105932176cfdSRui Paulo */ 106032176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_MBSS) 106132176cfdSRui Paulo ieee80211_mesh_node_cleanup(ni); 106232176cfdSRui Paulo #endif 106332176cfdSRui Paulo /* 106432176cfdSRui Paulo * Clear any staging queue entries. 106532176cfdSRui Paulo */ 106632176cfdSRui Paulo ieee80211_ageq_drain_node(&ic->ic_stageq, ni); 106732176cfdSRui Paulo 106832176cfdSRui Paulo /* 1069841ab66cSSepherosa Ziehau * Clear AREF flag that marks the authorization refcnt bump 1070841ab66cSSepherosa Ziehau * has happened. This is probably not needed as the node 1071841ab66cSSepherosa Ziehau * should always be removed from the table so not found but 1072841ab66cSSepherosa Ziehau * do it just in case. 107332176cfdSRui Paulo * Likewise clear the ASSOCID flag as these flags are intended 107432176cfdSRui Paulo * to be managed in tandem. 1075841ab66cSSepherosa Ziehau */ 107632176cfdSRui Paulo ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID); 1077841ab66cSSepherosa Ziehau 1078841ab66cSSepherosa Ziehau /* 1079841ab66cSSepherosa Ziehau * Drain power save queue and, if needed, clear TIM. 1080841ab66cSSepherosa Ziehau */ 108132176cfdSRui Paulo if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL) 108232176cfdSRui Paulo vap->iv_set_tim(ni, 0); 1083841ab66cSSepherosa Ziehau 1084841ab66cSSepherosa Ziehau ni->ni_associd = 0; 1085841ab66cSSepherosa Ziehau if (ni->ni_challenge != NULL) { 1086*4f655ef5SMatthew Dillon IEEE80211_FREE(ni->ni_challenge, M_80211_NODE); 1087841ab66cSSepherosa Ziehau ni->ni_challenge = NULL; 1088841ab66cSSepherosa Ziehau } 1089841ab66cSSepherosa Ziehau /* 1090841ab66cSSepherosa Ziehau * Preserve SSID, WPA, and WME ie's so the bss node is 1091841ab66cSSepherosa Ziehau * reusable during a re-auth/re-assoc state transition. 1092841ab66cSSepherosa Ziehau * If we remove these data they will not be recreated 1093841ab66cSSepherosa Ziehau * because they come from a probe-response or beacon frame 1094841ab66cSSepherosa Ziehau * which cannot be expected prior to the association-response. 1095841ab66cSSepherosa Ziehau * This should not be an issue when operating in other modes 1096841ab66cSSepherosa Ziehau * as stations leaving always go through a full state transition 1097841ab66cSSepherosa Ziehau * which will rebuild this state. 1098841ab66cSSepherosa Ziehau * 1099841ab66cSSepherosa Ziehau * XXX does this leave us open to inheriting old state? 1100841ab66cSSepherosa Ziehau */ 1101085ff963SMatthew Dillon for (i = 0; i < nitems(ni->ni_rxfrag); i++) 1102841ab66cSSepherosa Ziehau if (ni->ni_rxfrag[i] != NULL) { 1103841ab66cSSepherosa Ziehau m_freem(ni->ni_rxfrag[i]); 1104841ab66cSSepherosa Ziehau ni->ni_rxfrag[i] = NULL; 1105841ab66cSSepherosa Ziehau } 1106841ab66cSSepherosa Ziehau /* 1107841ab66cSSepherosa Ziehau * Must be careful here to remove any key map entry w/o a LOR. 1108841ab66cSSepherosa Ziehau */ 1109841ab66cSSepherosa Ziehau ieee80211_node_delucastkey(ni); 1110f186073cSJoerg Sonnenberger } 1111f186073cSJoerg Sonnenberger 1112f186073cSJoerg Sonnenberger static void 1113841ab66cSSepherosa Ziehau node_free(struct ieee80211_node *ni) 1114f186073cSJoerg Sonnenberger { 1115841ab66cSSepherosa Ziehau struct ieee80211com *ic = ni->ni_ic; 1116841ab66cSSepherosa Ziehau 11174028af95SRui Paulo ieee80211_ratectl_node_deinit(ni); 1118841ab66cSSepherosa Ziehau ic->ic_node_cleanup(ni); 111932176cfdSRui Paulo ieee80211_ies_cleanup(&ni->ni_ies); 112032176cfdSRui Paulo ieee80211_psq_cleanup(&ni->ni_psq); 1121*4f655ef5SMatthew Dillon IEEE80211_FREE(ni, M_80211_NODE); 1122f186073cSJoerg Sonnenberger } 1123f186073cSJoerg Sonnenberger 112432176cfdSRui Paulo static void 112532176cfdSRui Paulo node_age(struct ieee80211_node *ni) 112632176cfdSRui Paulo { 112732176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 112832176cfdSRui Paulo 1129085ff963SMatthew Dillon IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta); 1130085ff963SMatthew Dillon 113132176cfdSRui Paulo /* 113232176cfdSRui Paulo * Age frames on the power save queue. 113332176cfdSRui Paulo */ 113432176cfdSRui Paulo if (ieee80211_node_psq_age(ni) != 0 && 113532176cfdSRui Paulo ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL) 113632176cfdSRui Paulo vap->iv_set_tim(ni, 0); 113732176cfdSRui Paulo /* 113832176cfdSRui Paulo * Age out HT resources (e.g. frames on the 113932176cfdSRui Paulo * A-MPDU reorder queues). 114032176cfdSRui Paulo */ 114132176cfdSRui Paulo if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT)) 114232176cfdSRui Paulo ieee80211_ht_node_age(ni); 114332176cfdSRui Paulo } 114432176cfdSRui Paulo 114532176cfdSRui Paulo static int8_t 1146841ab66cSSepherosa Ziehau node_getrssi(const struct ieee80211_node *ni) 1147f186073cSJoerg Sonnenberger { 114832176cfdSRui Paulo uint32_t avgrssi = ni->ni_avgrssi; 114932176cfdSRui Paulo int32_t rssi; 115032176cfdSRui Paulo 115132176cfdSRui Paulo if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) 115232176cfdSRui Paulo return 0; 115332176cfdSRui Paulo rssi = IEEE80211_RSSI_GET(avgrssi); 115432176cfdSRui Paulo return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi; 1155f186073cSJoerg Sonnenberger } 1156f186073cSJoerg Sonnenberger 1157f186073cSJoerg Sonnenberger static void 115832176cfdSRui Paulo node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise) 115932176cfdSRui Paulo { 116032176cfdSRui Paulo *rssi = node_getrssi(ni); 116132176cfdSRui Paulo *noise = ni->ni_noise; 116232176cfdSRui Paulo } 116332176cfdSRui Paulo 116432176cfdSRui Paulo static void 116532176cfdSRui Paulo node_getmimoinfo(const struct ieee80211_node *ni, 116632176cfdSRui Paulo struct ieee80211_mimo_info *info) 116732176cfdSRui Paulo { 1168085ff963SMatthew Dillon int i; 1169085ff963SMatthew Dillon uint32_t avgrssi; 1170085ff963SMatthew Dillon int32_t rssi; 1171085ff963SMatthew Dillon 1172085ff963SMatthew Dillon bzero(info, sizeof(*info)); 1173085ff963SMatthew Dillon 1174085ff963SMatthew Dillon for (i = 0; i < ni->ni_mimo_chains; i++) { 1175085ff963SMatthew Dillon avgrssi = ni->ni_mimo_rssi_ctl[i]; 1176085ff963SMatthew Dillon if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) { 1177085ff963SMatthew Dillon info->rssi[i] = 0; 1178085ff963SMatthew Dillon } else { 1179085ff963SMatthew Dillon rssi = IEEE80211_RSSI_GET(avgrssi); 1180085ff963SMatthew Dillon info->rssi[i] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi; 1181085ff963SMatthew Dillon } 1182085ff963SMatthew Dillon info->noise[i] = ni->ni_mimo_noise_ctl[i]; 1183085ff963SMatthew Dillon } 1184085ff963SMatthew Dillon 1185085ff963SMatthew Dillon /* XXX ext radios? */ 1186085ff963SMatthew Dillon 1187085ff963SMatthew Dillon /* XXX EVM? */ 118832176cfdSRui Paulo } 118932176cfdSRui Paulo 119032176cfdSRui Paulo struct ieee80211_node * 119132176cfdSRui Paulo ieee80211_alloc_node(struct ieee80211_node_table *nt, 119232176cfdSRui Paulo struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1193f186073cSJoerg Sonnenberger { 1194841ab66cSSepherosa Ziehau struct ieee80211com *ic = nt->nt_ic; 119532176cfdSRui Paulo struct ieee80211_node *ni; 1196f186073cSJoerg Sonnenberger int hash; 1197f186073cSJoerg Sonnenberger 119832176cfdSRui Paulo ni = ic->ic_node_alloc(vap, macaddr); 119932176cfdSRui Paulo if (ni == NULL) { 120032176cfdSRui Paulo vap->iv_stats.is_rx_nodealloc++; 120132176cfdSRui Paulo return NULL; 120232176cfdSRui Paulo } 1203841ab66cSSepherosa Ziehau 120432176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 12051e290df3SAntonio Huete Jimenez "%s %p<%s> in %s table\n", __func__, ni, 1206085ff963SMatthew Dillon ether_sprintf(macaddr), nt->nt_name); 1207841ab66cSSepherosa Ziehau 1208f186073cSJoerg Sonnenberger IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 120932176cfdSRui Paulo hash = IEEE80211_NODE_HASH(ic, macaddr); 1210841ab66cSSepherosa Ziehau ieee80211_node_initref(ni); /* mark referenced */ 1211841ab66cSSepherosa Ziehau ni->ni_chan = IEEE80211_CHAN_ANYC; 1212841ab66cSSepherosa Ziehau ni->ni_authmode = IEEE80211_AUTH_OPEN; 1213841ab66cSSepherosa Ziehau ni->ni_txpower = ic->ic_txpowlimit; /* max power */ 121432176cfdSRui Paulo ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 121532176cfdSRui Paulo ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE); 121632176cfdSRui Paulo ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER; 1217841ab66cSSepherosa Ziehau ni->ni_inact_reload = nt->nt_inact_init; 1218841ab66cSSepherosa Ziehau ni->ni_inact = ni->ni_inact_reload; 121932176cfdSRui Paulo ni->ni_ath_defkeyix = 0x7fff; 122032176cfdSRui Paulo ieee80211_psq_init(&ni->ni_psq, "unknown"); 122132176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH 122232176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_MBSS) 122332176cfdSRui Paulo ieee80211_mesh_node_init(vap, ni); 122432176cfdSRui Paulo #endif 1225085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 1226841ab66cSSepherosa Ziehau TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list); 1227841ab66cSSepherosa Ziehau LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash); 1228841ab66cSSepherosa Ziehau ni->ni_table = nt; 122932176cfdSRui Paulo ni->ni_vap = vap; 1230841ab66cSSepherosa Ziehau ni->ni_ic = ic; 1231085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 1232b437bb69SSascha Wildner 123332176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 123432176cfdSRui Paulo "%s: inact_reload %u", __func__, ni->ni_inact_reload); 1235f186073cSJoerg Sonnenberger 1236085ff963SMatthew Dillon ieee80211_ratectl_node_init(ni); 1237085ff963SMatthew Dillon 1238f186073cSJoerg Sonnenberger return ni; 1239f186073cSJoerg Sonnenberger } 1240f186073cSJoerg Sonnenberger 1241841ab66cSSepherosa Ziehau /* 1242841ab66cSSepherosa Ziehau * Craft a temporary node suitable for sending a management frame 1243841ab66cSSepherosa Ziehau * to the specified station. We craft only as much state as we 1244841ab66cSSepherosa Ziehau * need to do the work since the node will be immediately reclaimed 1245841ab66cSSepherosa Ziehau * once the send completes. 1246841ab66cSSepherosa Ziehau */ 1247f186073cSJoerg Sonnenberger struct ieee80211_node * 124832176cfdSRui Paulo ieee80211_tmp_node(struct ieee80211vap *vap, 124932176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1250f186073cSJoerg Sonnenberger { 125132176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic; 1252841ab66cSSepherosa Ziehau struct ieee80211_node *ni; 1253841ab66cSSepherosa Ziehau 125432176cfdSRui Paulo ni = ic->ic_node_alloc(vap, macaddr); 1255f186073cSJoerg Sonnenberger if (ni != NULL) { 125632176cfdSRui Paulo struct ieee80211_node *bss = vap->iv_bss; 1257841ab66cSSepherosa Ziehau 125832176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1259085ff963SMatthew Dillon "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr)); 1260841ab66cSSepherosa Ziehau 1261841ab66cSSepherosa Ziehau ni->ni_table = NULL; /* NB: pedantic */ 126232176cfdSRui Paulo ni->ni_ic = ic; /* NB: needed to set channel */ 126332176cfdSRui Paulo ni->ni_vap = vap; 126432176cfdSRui Paulo 126532176cfdSRui Paulo IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr); 126632176cfdSRui Paulo IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid); 126732176cfdSRui Paulo ieee80211_node_initref(ni); /* mark referenced */ 126832176cfdSRui Paulo /* NB: required by ieee80211_fix_rate */ 126932176cfdSRui Paulo ieee80211_node_set_chan(ni, bss->ni_chan); 127032176cfdSRui Paulo ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, 127132176cfdSRui Paulo IEEE80211_KEYIX_NONE); 127232176cfdSRui Paulo ni->ni_txpower = bss->ni_txpower; 127332176cfdSRui Paulo /* XXX optimize away */ 127432176cfdSRui Paulo ieee80211_psq_init(&ni->ni_psq, "unknown"); 1275b437bb69SSascha Wildner 1276b437bb69SSascha Wildner ieee80211_ratectl_node_init(ni); 1277841ab66cSSepherosa Ziehau } else { 1278841ab66cSSepherosa Ziehau /* XXX msg */ 127932176cfdSRui Paulo vap->iv_stats.is_rx_nodealloc++; 1280841ab66cSSepherosa Ziehau } 1281841ab66cSSepherosa Ziehau return ni; 1282841ab66cSSepherosa Ziehau } 1283841ab66cSSepherosa Ziehau 1284841ab66cSSepherosa Ziehau struct ieee80211_node * 128532176cfdSRui Paulo ieee80211_dup_bss(struct ieee80211vap *vap, 128632176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1287841ab66cSSepherosa Ziehau { 128832176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic; 1289841ab66cSSepherosa Ziehau struct ieee80211_node *ni; 1290841ab66cSSepherosa Ziehau 129132176cfdSRui Paulo ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr); 1292841ab66cSSepherosa Ziehau if (ni != NULL) { 129332176cfdSRui Paulo struct ieee80211_node *bss = vap->iv_bss; 1294f186073cSJoerg Sonnenberger /* 129532176cfdSRui Paulo * Inherit from iv_bss. 1296f186073cSJoerg Sonnenberger */ 129732176cfdSRui Paulo copy_bss(ni, bss); 129832176cfdSRui Paulo IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid); 129932176cfdSRui Paulo ieee80211_node_set_chan(ni, bss->ni_chan); 130032176cfdSRui Paulo } 1301f186073cSJoerg Sonnenberger return ni; 1302f186073cSJoerg Sonnenberger } 1303f186073cSJoerg Sonnenberger 130432176cfdSRui Paulo /* 130532176cfdSRui Paulo * Create a bss node for a legacy WDS vap. The far end does 130632176cfdSRui Paulo * not associate so we just create create a new node and 130732176cfdSRui Paulo * simulate an association. The caller is responsible for 130832176cfdSRui Paulo * installing the node as the bss node and handling any further 130932176cfdSRui Paulo * setup work like authorizing the port. 131032176cfdSRui Paulo */ 131132176cfdSRui Paulo struct ieee80211_node * 131232176cfdSRui Paulo ieee80211_node_create_wds(struct ieee80211vap *vap, 131332176cfdSRui Paulo const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan) 131432176cfdSRui Paulo { 131532176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic; 131632176cfdSRui Paulo struct ieee80211_node *ni; 131732176cfdSRui Paulo 131832176cfdSRui Paulo /* XXX check if node already in sta table? */ 131932176cfdSRui Paulo ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid); 132032176cfdSRui Paulo if (ni != NULL) { 132132176cfdSRui Paulo ni->ni_wdsvap = vap; 132232176cfdSRui Paulo IEEE80211_ADDR_COPY(ni->ni_bssid, bssid); 132332176cfdSRui Paulo /* 132432176cfdSRui Paulo * Inherit any manually configured settings. 132532176cfdSRui Paulo */ 132632176cfdSRui Paulo copy_bss(ni, vap->iv_bss); 132732176cfdSRui Paulo ieee80211_node_set_chan(ni, chan); 132832176cfdSRui Paulo /* NB: propagate ssid so available to WPA supplicant */ 132932176cfdSRui Paulo ni->ni_esslen = vap->iv_des_ssid[0].len; 133032176cfdSRui Paulo memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen); 133132176cfdSRui Paulo /* NB: no associd for peer */ 133232176cfdSRui Paulo /* 133332176cfdSRui Paulo * There are no management frames to use to 133432176cfdSRui Paulo * discover neighbor capabilities, so blindly 133532176cfdSRui Paulo * propagate the local configuration. 133632176cfdSRui Paulo */ 133732176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_WME) 133832176cfdSRui Paulo ni->ni_flags |= IEEE80211_NODE_QOS; 133932176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG 134032176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_FF) 134132176cfdSRui Paulo ni->ni_flags |= IEEE80211_NODE_FF; 134232176cfdSRui Paulo #endif 134332176cfdSRui Paulo if ((ic->ic_htcaps & IEEE80211_HTC_HT) && 134432176cfdSRui Paulo (vap->iv_flags_ht & IEEE80211_FHT_HT)) { 134532176cfdSRui Paulo /* 134632176cfdSRui Paulo * Device is HT-capable and HT is enabled for 134732176cfdSRui Paulo * the vap; setup HT operation. On return 134832176cfdSRui Paulo * ni_chan will be adjusted to an HT channel. 134932176cfdSRui Paulo */ 135032176cfdSRui Paulo ieee80211_ht_wds_init(ni); 135132176cfdSRui Paulo } else { 135232176cfdSRui Paulo struct ieee80211_channel *c = ni->ni_chan; 135332176cfdSRui Paulo /* 135432176cfdSRui Paulo * Force a legacy channel to be used. 135532176cfdSRui Paulo */ 135632176cfdSRui Paulo c = ieee80211_find_channel(ic, 135732176cfdSRui Paulo c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT); 135832176cfdSRui Paulo KASSERT(c != NULL, ("no legacy channel, %u/%x", 135932176cfdSRui Paulo ni->ni_chan->ic_freq, ni->ni_chan->ic_flags)); 136032176cfdSRui Paulo ni->ni_chan = c; 136132176cfdSRui Paulo } 136232176cfdSRui Paulo } 136332176cfdSRui Paulo return ni; 136432176cfdSRui Paulo } 136532176cfdSRui Paulo 136632176cfdSRui Paulo struct ieee80211_node * 1367841ab66cSSepherosa Ziehau #ifdef IEEE80211_DEBUG_REFCNT 136832176cfdSRui Paulo ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt, 136932176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1370841ab66cSSepherosa Ziehau #else 137132176cfdSRui Paulo ieee80211_find_node_locked(struct ieee80211_node_table *nt, 137232176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1373841ab66cSSepherosa Ziehau #endif 1374f186073cSJoerg Sonnenberger { 1375f186073cSJoerg Sonnenberger struct ieee80211_node *ni; 1376f186073cSJoerg Sonnenberger int hash; 1377085ff963SMatthew Dillon 1378085ff963SMatthew Dillon IEEE80211_NODE_LOCK_ASSERT(nt); 1379085ff963SMatthew Dillon 138032176cfdSRui Paulo hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr); 1381841ab66cSSepherosa Ziehau LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 1382f186073cSJoerg Sonnenberger if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 1383841ab66cSSepherosa Ziehau ieee80211_ref_node(ni); /* mark referenced */ 1384841ab66cSSepherosa Ziehau #ifdef IEEE80211_DEBUG_REFCNT 138532176cfdSRui Paulo IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 13861e290df3SAntonio Huete Jimenez "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 1387841ab66cSSepherosa Ziehau func, line, 1388085ff963SMatthew Dillon ni, ether_sprintf(ni->ni_macaddr), 1389841ab66cSSepherosa Ziehau ieee80211_node_refcnt(ni)); 1390841ab66cSSepherosa Ziehau #endif 1391f186073cSJoerg Sonnenberger return ni; 1392f186073cSJoerg Sonnenberger } 1393f186073cSJoerg Sonnenberger } 1394f186073cSJoerg Sonnenberger return NULL; 1395f186073cSJoerg Sonnenberger } 1396f186073cSJoerg Sonnenberger 1397f186073cSJoerg Sonnenberger struct ieee80211_node * 1398841ab66cSSepherosa Ziehau #ifdef IEEE80211_DEBUG_REFCNT 1399841ab66cSSepherosa Ziehau ieee80211_find_node_debug(struct ieee80211_node_table *nt, 140032176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 1401841ab66cSSepherosa Ziehau #else 140232176cfdSRui Paulo ieee80211_find_node(struct ieee80211_node_table *nt, 140332176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1404841ab66cSSepherosa Ziehau #endif 1405f186073cSJoerg Sonnenberger { 1406f186073cSJoerg Sonnenberger struct ieee80211_node *ni; 1407f186073cSJoerg Sonnenberger 1408085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 140932176cfdSRui Paulo ni = ieee80211_find_node_locked(nt, macaddr); 1410085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 141132176cfdSRui Paulo return ni; 141232176cfdSRui Paulo } 1413841ab66cSSepherosa Ziehau 141432176cfdSRui Paulo struct ieee80211_node * 141532176cfdSRui Paulo #ifdef IEEE80211_DEBUG_REFCNT 141632176cfdSRui Paulo ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt, 141732176cfdSRui Paulo const struct ieee80211vap *vap, 141832176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 141932176cfdSRui Paulo #else 142032176cfdSRui Paulo ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt, 142132176cfdSRui Paulo const struct ieee80211vap *vap, 142232176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN]) 142332176cfdSRui Paulo #endif 142432176cfdSRui Paulo { 142532176cfdSRui Paulo struct ieee80211_node *ni; 142632176cfdSRui Paulo int hash; 1427085ff963SMatthew Dillon 1428085ff963SMatthew Dillon IEEE80211_NODE_LOCK_ASSERT(nt); 1429085ff963SMatthew Dillon 143032176cfdSRui Paulo hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr); 143132176cfdSRui Paulo LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) { 143232176cfdSRui Paulo if (ni->ni_vap == vap && 143332176cfdSRui Paulo IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) { 143432176cfdSRui Paulo ieee80211_ref_node(ni); /* mark referenced */ 143532176cfdSRui Paulo #ifdef IEEE80211_DEBUG_REFCNT 143632176cfdSRui Paulo IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 14371e290df3SAntonio Huete Jimenez "%s (%s:%u) %p<%s> refcnt %d\n", __func__, 143832176cfdSRui Paulo func, line, 1439085ff963SMatthew Dillon ni, ether_sprintf(ni->ni_macaddr), 144032176cfdSRui Paulo ieee80211_node_refcnt(ni)); 144132176cfdSRui Paulo #endif 144232176cfdSRui Paulo return ni; 144332176cfdSRui Paulo } 144432176cfdSRui Paulo } 144532176cfdSRui Paulo return NULL; 144632176cfdSRui Paulo } 144732176cfdSRui Paulo 144832176cfdSRui Paulo struct ieee80211_node * 144932176cfdSRui Paulo #ifdef IEEE80211_DEBUG_REFCNT 145032176cfdSRui Paulo ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt, 145132176cfdSRui Paulo const struct ieee80211vap *vap, 145232176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line) 145332176cfdSRui Paulo #else 145432176cfdSRui Paulo ieee80211_find_vap_node(struct ieee80211_node_table *nt, 145532176cfdSRui Paulo const struct ieee80211vap *vap, 145632176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN]) 145732176cfdSRui Paulo #endif 145832176cfdSRui Paulo { 145932176cfdSRui Paulo struct ieee80211_node *ni; 146032176cfdSRui Paulo 1461085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 146232176cfdSRui Paulo ni = ieee80211_find_vap_node_locked(nt, vap, macaddr); 1463085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 1464f186073cSJoerg Sonnenberger return ni; 1465f186073cSJoerg Sonnenberger } 1466f186073cSJoerg Sonnenberger 1467f186073cSJoerg Sonnenberger /* 1468841ab66cSSepherosa Ziehau * Fake up a node; this handles node discovery in adhoc mode. 1469841ab66cSSepherosa Ziehau * Note that for the driver's benefit we we treat this like 1470841ab66cSSepherosa Ziehau * an association so the driver has an opportunity to setup 1471841ab66cSSepherosa Ziehau * it's private state. 1472841ab66cSSepherosa Ziehau */ 1473841ab66cSSepherosa Ziehau struct ieee80211_node * 147432176cfdSRui Paulo ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap, 1475841ab66cSSepherosa Ziehau const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1476841ab66cSSepherosa Ziehau { 1477841ab66cSSepherosa Ziehau struct ieee80211_node *ni; 1478841ab66cSSepherosa Ziehau 1479085ff963SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC, 1480085ff963SMatthew Dillon "%s: mac<%s>\n", __func__, ether_sprintf(macaddr)); 148132176cfdSRui Paulo ni = ieee80211_dup_bss(vap, macaddr); 1482841ab66cSSepherosa Ziehau if (ni != NULL) { 148332176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic; 148432176cfdSRui Paulo 1485841ab66cSSepherosa Ziehau /* XXX no rate negotiation; just dup */ 148632176cfdSRui Paulo ni->ni_rates = vap->iv_bss->ni_rates; 148732176cfdSRui Paulo if (ieee80211_iserp_rateset(&ni->ni_rates)) 148832176cfdSRui Paulo ni->ni_flags |= IEEE80211_NODE_ERP; 148932176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_AHDEMO) { 149032176cfdSRui Paulo /* 149132176cfdSRui Paulo * In adhoc demo mode there are no management 149232176cfdSRui Paulo * frames to use to discover neighbor capabilities, 149332176cfdSRui Paulo * so blindly propagate the local configuration 149432176cfdSRui Paulo * so we can do interesting things (e.g. use 149532176cfdSRui Paulo * WME to disable ACK's). 149632176cfdSRui Paulo */ 149732176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_WME) 149832176cfdSRui Paulo ni->ni_flags |= IEEE80211_NODE_QOS; 149932176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG 150032176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_FF) 150132176cfdSRui Paulo ni->ni_flags |= IEEE80211_NODE_FF; 150232176cfdSRui Paulo #endif 150332176cfdSRui Paulo } 150432176cfdSRui Paulo ieee80211_node_setuptxparms(ni); 15054fbce6bdSSascha Wildner ieee80211_ratectl_node_init(ni); 1506841ab66cSSepherosa Ziehau if (ic->ic_newassoc != NULL) 1507841ab66cSSepherosa Ziehau ic->ic_newassoc(ni, 1); 1508841ab66cSSepherosa Ziehau /* XXX not right for 802.1x/WPA */ 1509841ab66cSSepherosa Ziehau ieee80211_node_authorize(ni); 1510841ab66cSSepherosa Ziehau } 1511841ab66cSSepherosa Ziehau return ni; 1512841ab66cSSepherosa Ziehau } 1513841ab66cSSepherosa Ziehau 1514841ab66cSSepherosa Ziehau void 1515841ab66cSSepherosa Ziehau ieee80211_init_neighbor(struct ieee80211_node *ni, 1516841ab66cSSepherosa Ziehau const struct ieee80211_frame *wh, 1517841ab66cSSepherosa Ziehau const struct ieee80211_scanparams *sp) 1518841ab66cSSepherosa Ziehau { 1519085ff963SMatthew Dillon int do_ht_setup = 0; 1520085ff963SMatthew Dillon 1521841ab66cSSepherosa Ziehau ni->ni_esslen = sp->ssid[1]; 1522841ab66cSSepherosa Ziehau memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]); 1523841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1524841ab66cSSepherosa Ziehau memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp)); 1525841ab66cSSepherosa Ziehau ni->ni_intval = sp->bintval; 1526841ab66cSSepherosa Ziehau ni->ni_capinfo = sp->capinfo; 1527841ab66cSSepherosa Ziehau ni->ni_chan = ni->ni_ic->ic_curchan; 1528841ab66cSSepherosa Ziehau ni->ni_fhdwell = sp->fhdwell; 1529841ab66cSSepherosa Ziehau ni->ni_fhindex = sp->fhindex; 1530841ab66cSSepherosa Ziehau ni->ni_erp = sp->erp; 1531841ab66cSSepherosa Ziehau ni->ni_timoff = sp->timoff; 153232176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH 153332176cfdSRui Paulo if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS) 153432176cfdSRui Paulo ieee80211_mesh_init_neighbor(ni, wh, sp); 153532176cfdSRui Paulo #endif 153632176cfdSRui Paulo if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) { 153732176cfdSRui Paulo ieee80211_ies_expand(&ni->ni_ies); 153832176cfdSRui Paulo if (ni->ni_ies.wme_ie != NULL) 153932176cfdSRui Paulo ni->ni_flags |= IEEE80211_NODE_QOS; 154032176cfdSRui Paulo else 154132176cfdSRui Paulo ni->ni_flags &= ~IEEE80211_NODE_QOS; 154232176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG 154332176cfdSRui Paulo if (ni->ni_ies.ath_ie != NULL) 154432176cfdSRui Paulo ieee80211_parse_ath(ni, ni->ni_ies.ath_ie); 154532176cfdSRui Paulo #endif 1546085ff963SMatthew Dillon if (ni->ni_ies.htcap_ie != NULL) 1547085ff963SMatthew Dillon ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie); 1548085ff963SMatthew Dillon if (ni->ni_ies.htinfo_ie != NULL) 1549085ff963SMatthew Dillon ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie); 1550085ff963SMatthew Dillon 1551085ff963SMatthew Dillon if ((ni->ni_ies.htcap_ie != NULL) && 1552085ff963SMatthew Dillon (ni->ni_ies.htinfo_ie != NULL) && 1553085ff963SMatthew Dillon (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) { 1554085ff963SMatthew Dillon do_ht_setup = 1; 1555085ff963SMatthew Dillon } 155632176cfdSRui Paulo } 1557841ab66cSSepherosa Ziehau 1558841ab66cSSepherosa Ziehau /* NB: must be after ni_chan is setup */ 1559208a1285SSepherosa Ziehau ieee80211_setup_rates(ni, sp->rates, sp->xrates, 1560208a1285SSepherosa Ziehau IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 156132176cfdSRui Paulo IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1562085ff963SMatthew Dillon 1563085ff963SMatthew Dillon /* 1564085ff963SMatthew Dillon * If the neighbor is HT compatible, flip that on. 1565085ff963SMatthew Dillon */ 1566085ff963SMatthew Dillon if (do_ht_setup) { 1567085ff963SMatthew Dillon IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 1568085ff963SMatthew Dillon "%s: doing HT setup\n", __func__); 1569085ff963SMatthew Dillon ieee80211_ht_node_init(ni); 1570085ff963SMatthew Dillon ieee80211_ht_updateparams(ni, 1571085ff963SMatthew Dillon ni->ni_ies.htcap_ie, 1572085ff963SMatthew Dillon ni->ni_ies.htinfo_ie); 1573085ff963SMatthew Dillon ieee80211_setup_htrates(ni, 1574085ff963SMatthew Dillon ni->ni_ies.htcap_ie, 1575085ff963SMatthew Dillon IEEE80211_F_JOIN | IEEE80211_F_DOBRS); 1576085ff963SMatthew Dillon ieee80211_setup_basic_htrates(ni, 1577085ff963SMatthew Dillon ni->ni_ies.htinfo_ie); 1578085ff963SMatthew Dillon ieee80211_node_setuptxparms(ni); 1579085ff963SMatthew Dillon ieee80211_ratectl_node_init(ni); 1580085ff963SMatthew Dillon } 1581841ab66cSSepherosa Ziehau } 1582841ab66cSSepherosa Ziehau 1583841ab66cSSepherosa Ziehau /* 1584841ab66cSSepherosa Ziehau * Do node discovery in adhoc mode on receipt of a beacon 1585841ab66cSSepherosa Ziehau * or probe response frame. Note that for the driver's 1586841ab66cSSepherosa Ziehau * benefit we we treat this like an association so the 1587841ab66cSSepherosa Ziehau * driver has an opportunity to setup it's private state. 1588841ab66cSSepherosa Ziehau */ 1589841ab66cSSepherosa Ziehau struct ieee80211_node * 159032176cfdSRui Paulo ieee80211_add_neighbor(struct ieee80211vap *vap, 1591841ab66cSSepherosa Ziehau const struct ieee80211_frame *wh, 1592841ab66cSSepherosa Ziehau const struct ieee80211_scanparams *sp) 1593841ab66cSSepherosa Ziehau { 1594841ab66cSSepherosa Ziehau struct ieee80211_node *ni; 1595841ab66cSSepherosa Ziehau 1596085ff963SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC, 1597085ff963SMatthew Dillon "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2)); 159832176cfdSRui Paulo ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */ 1599841ab66cSSepherosa Ziehau if (ni != NULL) { 160032176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic; 160132176cfdSRui Paulo 1602841ab66cSSepherosa Ziehau ieee80211_init_neighbor(ni, wh, sp); 160332176cfdSRui Paulo if (ieee80211_iserp_rateset(&ni->ni_rates)) 160432176cfdSRui Paulo ni->ni_flags |= IEEE80211_NODE_ERP; 160532176cfdSRui Paulo ieee80211_node_setuptxparms(ni); 16064fbce6bdSSascha Wildner ieee80211_ratectl_node_init(ni); 1607841ab66cSSepherosa Ziehau if (ic->ic_newassoc != NULL) 1608841ab66cSSepherosa Ziehau ic->ic_newassoc(ni, 1); 1609841ab66cSSepherosa Ziehau /* XXX not right for 802.1x/WPA */ 1610841ab66cSSepherosa Ziehau ieee80211_node_authorize(ni); 1611841ab66cSSepherosa Ziehau } 1612841ab66cSSepherosa Ziehau return ni; 1613841ab66cSSepherosa Ziehau } 1614841ab66cSSepherosa Ziehau 161532176cfdSRui Paulo #define IS_PROBEREQ(wh) \ 161632176cfdSRui Paulo ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \ 161732176cfdSRui Paulo == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ)) 161832176cfdSRui Paulo #define IS_BCAST_PROBEREQ(wh) \ 161932176cfdSRui Paulo (IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \ 162032176cfdSRui Paulo ((const struct ieee80211_frame *)(wh))->i_addr3)) 162132176cfdSRui Paulo 162232176cfdSRui Paulo static __inline struct ieee80211_node * 162332176cfdSRui Paulo _find_rxnode(struct ieee80211_node_table *nt, 162432176cfdSRui Paulo const struct ieee80211_frame_min *wh) 162532176cfdSRui Paulo { 162632176cfdSRui Paulo if (IS_BCAST_PROBEREQ(wh)) 162732176cfdSRui Paulo return NULL; /* spam bcast probe req to all vap's */ 162832176cfdSRui Paulo return ieee80211_find_node_locked(nt, wh->i_addr2); 162932176cfdSRui Paulo } 163032176cfdSRui Paulo 1631841ab66cSSepherosa Ziehau /* 1632841ab66cSSepherosa Ziehau * Locate the node for sender, track state, and then pass the 163332176cfdSRui Paulo * (referenced) node up to the 802.11 layer for its use. Note 163432176cfdSRui Paulo * we can return NULL if the sender is not in the table. 1635841ab66cSSepherosa Ziehau */ 1636841ab66cSSepherosa Ziehau struct ieee80211_node * 1637841ab66cSSepherosa Ziehau #ifdef IEEE80211_DEBUG_REFCNT 1638841ab66cSSepherosa Ziehau ieee80211_find_rxnode_debug(struct ieee80211com *ic, 1639841ab66cSSepherosa Ziehau const struct ieee80211_frame_min *wh, const char *func, int line) 1640841ab66cSSepherosa Ziehau #else 1641841ab66cSSepherosa Ziehau ieee80211_find_rxnode(struct ieee80211com *ic, 1642841ab66cSSepherosa Ziehau const struct ieee80211_frame_min *wh) 1643841ab66cSSepherosa Ziehau #endif 1644841ab66cSSepherosa Ziehau { 1645841ab66cSSepherosa Ziehau struct ieee80211_node_table *nt; 1646841ab66cSSepherosa Ziehau struct ieee80211_node *ni; 1647841ab66cSSepherosa Ziehau 1648841ab66cSSepherosa Ziehau nt = &ic->ic_sta; 1649085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 165032176cfdSRui Paulo ni = _find_rxnode(nt, wh); 1651085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 1652841ab66cSSepherosa Ziehau 1653841ab66cSSepherosa Ziehau return ni; 1654841ab66cSSepherosa Ziehau } 1655841ab66cSSepherosa Ziehau 1656841ab66cSSepherosa Ziehau /* 1657841ab66cSSepherosa Ziehau * Like ieee80211_find_rxnode but use the supplied h/w 1658841ab66cSSepherosa Ziehau * key index as a hint to locate the node in the key 1659841ab66cSSepherosa Ziehau * mapping table. If an entry is present at the key 1660841ab66cSSepherosa Ziehau * index we return it; otherwise do a normal lookup and 1661841ab66cSSepherosa Ziehau * update the mapping table if the station has a unicast 1662841ab66cSSepherosa Ziehau * key assigned to it. 1663841ab66cSSepherosa Ziehau */ 1664841ab66cSSepherosa Ziehau struct ieee80211_node * 1665841ab66cSSepherosa Ziehau #ifdef IEEE80211_DEBUG_REFCNT 1666841ab66cSSepherosa Ziehau ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic, 1667841ab66cSSepherosa Ziehau const struct ieee80211_frame_min *wh, ieee80211_keyix keyix, 1668841ab66cSSepherosa Ziehau const char *func, int line) 1669841ab66cSSepherosa Ziehau #else 1670841ab66cSSepherosa Ziehau ieee80211_find_rxnode_withkey(struct ieee80211com *ic, 1671841ab66cSSepherosa Ziehau const struct ieee80211_frame_min *wh, ieee80211_keyix keyix) 1672841ab66cSSepherosa Ziehau #endif 1673841ab66cSSepherosa Ziehau { 1674841ab66cSSepherosa Ziehau struct ieee80211_node_table *nt; 1675841ab66cSSepherosa Ziehau struct ieee80211_node *ni; 1676841ab66cSSepherosa Ziehau 1677841ab66cSSepherosa Ziehau nt = &ic->ic_sta; 1678085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 1679841ab66cSSepherosa Ziehau if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) 1680841ab66cSSepherosa Ziehau ni = nt->nt_keyixmap[keyix]; 1681841ab66cSSepherosa Ziehau else 1682841ab66cSSepherosa Ziehau ni = NULL; 1683841ab66cSSepherosa Ziehau if (ni == NULL) { 168432176cfdSRui Paulo ni = _find_rxnode(nt, wh); 168532176cfdSRui Paulo if (ni != NULL && nt->nt_keyixmap != NULL) { 1686841ab66cSSepherosa Ziehau /* 1687841ab66cSSepherosa Ziehau * If the station has a unicast key cache slot 1688841ab66cSSepherosa Ziehau * assigned update the key->node mapping table. 1689841ab66cSSepherosa Ziehau */ 1690841ab66cSSepherosa Ziehau keyix = ni->ni_ucastkey.wk_rxkeyix; 1691841ab66cSSepherosa Ziehau /* XXX can keyixmap[keyix] != NULL? */ 1692841ab66cSSepherosa Ziehau if (keyix < nt->nt_keyixmax && 1693841ab66cSSepherosa Ziehau nt->nt_keyixmap[keyix] == NULL) { 169432176cfdSRui Paulo IEEE80211_DPRINTF(ni->ni_vap, 169532176cfdSRui Paulo IEEE80211_MSG_NODE, 16961e290df3SAntonio Huete Jimenez "%s: add key map entry %p<%s> refcnt %d\n", 1697085ff963SMatthew Dillon __func__, ni, ether_sprintf(ni->ni_macaddr), 1698841ab66cSSepherosa Ziehau ieee80211_node_refcnt(ni)+1); 1699841ab66cSSepherosa Ziehau nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni); 1700841ab66cSSepherosa Ziehau } 1701841ab66cSSepherosa Ziehau } 1702841ab66cSSepherosa Ziehau } else { 170332176cfdSRui Paulo if (IS_BCAST_PROBEREQ(wh)) 170432176cfdSRui Paulo ni = NULL; /* spam bcast probe req to all vap's */ 170532176cfdSRui Paulo else 1706841ab66cSSepherosa Ziehau ieee80211_ref_node(ni); 1707841ab66cSSepherosa Ziehau } 1708085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 1709085ff963SMatthew Dillon 1710841ab66cSSepherosa Ziehau return ni; 1711841ab66cSSepherosa Ziehau } 171232176cfdSRui Paulo #undef IS_BCAST_PROBEREQ 171332176cfdSRui Paulo #undef IS_PROBEREQ 1714841ab66cSSepherosa Ziehau 1715841ab66cSSepherosa Ziehau /* 1716f186073cSJoerg Sonnenberger * Return a reference to the appropriate node for sending 1717f186073cSJoerg Sonnenberger * a data frame. This handles node discovery in adhoc networks. 1718f186073cSJoerg Sonnenberger */ 1719f186073cSJoerg Sonnenberger struct ieee80211_node * 1720841ab66cSSepherosa Ziehau #ifdef IEEE80211_DEBUG_REFCNT 172132176cfdSRui Paulo ieee80211_find_txnode_debug(struct ieee80211vap *vap, 172232176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN], 1723841ab66cSSepherosa Ziehau const char *func, int line) 1724841ab66cSSepherosa Ziehau #else 172532176cfdSRui Paulo ieee80211_find_txnode(struct ieee80211vap *vap, 172632176cfdSRui Paulo const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1727841ab66cSSepherosa Ziehau #endif 1728f186073cSJoerg Sonnenberger { 172932176cfdSRui Paulo struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta; 1730f186073cSJoerg Sonnenberger struct ieee80211_node *ni; 1731841ab66cSSepherosa Ziehau 1732f186073cSJoerg Sonnenberger /* 1733f186073cSJoerg Sonnenberger * The destination address should be in the node table 1734841ab66cSSepherosa Ziehau * unless this is a multicast/broadcast frame. We can 1735841ab66cSSepherosa Ziehau * also optimize station mode operation, all frames go 1736841ab66cSSepherosa Ziehau * to the bss node. 1737f186073cSJoerg Sonnenberger */ 173832176cfdSRui Paulo /* XXX can't hold lock across dup_bss 'cuz of recursive locking */ 1739085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 174032176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_STA || 174132176cfdSRui Paulo vap->iv_opmode == IEEE80211_M_WDS || 174232176cfdSRui Paulo IEEE80211_IS_MULTICAST(macaddr)) 174332176cfdSRui Paulo ni = ieee80211_ref_node(vap->iv_bss); 174432176cfdSRui Paulo else 174532176cfdSRui Paulo ni = ieee80211_find_node_locked(nt, macaddr); 1746085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 1747f186073cSJoerg Sonnenberger 1748841ab66cSSepherosa Ziehau if (ni == NULL) { 174932176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_IBSS || 175032176cfdSRui Paulo vap->iv_opmode == IEEE80211_M_AHDEMO) { 1751f186073cSJoerg Sonnenberger /* 1752841ab66cSSepherosa Ziehau * In adhoc mode cons up a node for the destination. 1753841ab66cSSepherosa Ziehau * Note that we need an additional reference for the 175432176cfdSRui Paulo * caller to be consistent with 175532176cfdSRui Paulo * ieee80211_find_node_locked. 1756f186073cSJoerg Sonnenberger */ 175732176cfdSRui Paulo ni = ieee80211_fakeup_adhoc_node(vap, macaddr); 1758841ab66cSSepherosa Ziehau if (ni != NULL) 175932176cfdSRui Paulo (void) ieee80211_ref_node(ni); 1760841ab66cSSepherosa Ziehau } else { 176132176cfdSRui Paulo IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr, 176232176cfdSRui Paulo "no node, discard frame (%s)", __func__); 176332176cfdSRui Paulo vap->iv_stats.is_tx_nonode++; 1764f186073cSJoerg Sonnenberger } 1765f186073cSJoerg Sonnenberger } 1766f186073cSJoerg Sonnenberger return ni; 1767f186073cSJoerg Sonnenberger } 1768f186073cSJoerg Sonnenberger 1769841ab66cSSepherosa Ziehau static void 1770841ab66cSSepherosa Ziehau _ieee80211_free_node(struct ieee80211_node *ni) 1771841ab66cSSepherosa Ziehau { 1772841ab66cSSepherosa Ziehau struct ieee80211_node_table *nt = ni->ni_table; 1773841ab66cSSepherosa Ziehau 177432176cfdSRui Paulo /* 177532176cfdSRui Paulo * NB: careful about referencing the vap as it may be 177632176cfdSRui Paulo * gone if the last reference was held by a driver. 177732176cfdSRui Paulo * We know the com will always be present so it's safe 177832176cfdSRui Paulo * to use ni_ic below to reclaim resources. 177932176cfdSRui Paulo */ 178032176cfdSRui Paulo #if 0 178132176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 17821e290df3SAntonio Huete Jimenez "%s %p<%s> in %s table\n", __func__, ni, 1783085ff963SMatthew Dillon ether_sprintf(ni->ni_macaddr), 1784841ab66cSSepherosa Ziehau nt != NULL ? nt->nt_name : "<gone>"); 178532176cfdSRui Paulo #endif 178632176cfdSRui Paulo if (ni->ni_associd != 0) { 178732176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 178832176cfdSRui Paulo if (vap->iv_aid_bitmap != NULL) 178932176cfdSRui Paulo IEEE80211_AID_CLR(vap, ni->ni_associd); 179032176cfdSRui Paulo } 1791841ab66cSSepherosa Ziehau if (nt != NULL) { 1792841ab66cSSepherosa Ziehau TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1793841ab66cSSepherosa Ziehau LIST_REMOVE(ni, ni_hash); 1794841ab66cSSepherosa Ziehau } 179532176cfdSRui Paulo ni->ni_ic->ic_node_free(ni); 1796841ab66cSSepherosa Ziehau } 1797841ab66cSSepherosa Ziehau 17984f898719SImre Vadász /* 17994f898719SImre Vadász * Clear any entry in the unicast key mapping table. 18004f898719SImre Vadász */ 18014f898719SImre Vadász static int 18024f898719SImre Vadász node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 18034f898719SImre Vadász { 18044f898719SImre Vadász ieee80211_keyix keyix; 18054f898719SImre Vadász 18064f898719SImre Vadász keyix = ni->ni_ucastkey.wk_rxkeyix; 18074f898719SImre Vadász if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax && 18084f898719SImre Vadász nt->nt_keyixmap[keyix] == ni) { 18094f898719SImre Vadász IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 18104f898719SImre Vadász "%s: %p<%s> clear key map entry %u\n", 18114f898719SImre Vadász __func__, ni, ether_sprintf(ni->ni_macaddr), keyix); 18124f898719SImre Vadász nt->nt_keyixmap[keyix] = NULL; 18134f898719SImre Vadász ieee80211_node_decref(ni); 18144f898719SImre Vadász return 1; 18154f898719SImre Vadász } 18164f898719SImre Vadász 18174f898719SImre Vadász return 0; 18184f898719SImre Vadász } 18194f898719SImre Vadász 1820841ab66cSSepherosa Ziehau void 1821841ab66cSSepherosa Ziehau #ifdef IEEE80211_DEBUG_REFCNT 1822841ab66cSSepherosa Ziehau ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line) 1823841ab66cSSepherosa Ziehau #else 1824841ab66cSSepherosa Ziehau ieee80211_free_node(struct ieee80211_node *ni) 1825841ab66cSSepherosa Ziehau #endif 1826841ab66cSSepherosa Ziehau { 1827841ab66cSSepherosa Ziehau struct ieee80211_node_table *nt = ni->ni_table; 1828841ab66cSSepherosa Ziehau 1829841ab66cSSepherosa Ziehau #ifdef IEEE80211_DEBUG_REFCNT 183032176cfdSRui Paulo IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 18311e290df3SAntonio Huete Jimenez "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni, 1832085ff963SMatthew Dillon ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1); 1833841ab66cSSepherosa Ziehau #endif 1834841ab66cSSepherosa Ziehau if (nt != NULL) { 1835085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 1836841ab66cSSepherosa Ziehau if (ieee80211_node_dectestref(ni)) { 1837841ab66cSSepherosa Ziehau /* 1838841ab66cSSepherosa Ziehau * Last reference, reclaim state. 1839841ab66cSSepherosa Ziehau */ 1840841ab66cSSepherosa Ziehau _ieee80211_free_node(ni); 18414f898719SImre Vadász } else if (ieee80211_node_refcnt(ni) == 1) 18424f898719SImre Vadász if (node_clear_keyixmap(nt, ni)) 1843841ab66cSSepherosa Ziehau _ieee80211_free_node(ni); 1844085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 1845841ab66cSSepherosa Ziehau } else { 1846841ab66cSSepherosa Ziehau if (ieee80211_node_dectestref(ni)) 1847841ab66cSSepherosa Ziehau _ieee80211_free_node(ni); 1848841ab66cSSepherosa Ziehau } 1849f186073cSJoerg Sonnenberger } 1850f186073cSJoerg Sonnenberger 1851f186073cSJoerg Sonnenberger /* 1852841ab66cSSepherosa Ziehau * Reclaim a unicast key and clear any key cache state. 1853841ab66cSSepherosa Ziehau */ 1854841ab66cSSepherosa Ziehau int 1855841ab66cSSepherosa Ziehau ieee80211_node_delucastkey(struct ieee80211_node *ni) 1856841ab66cSSepherosa Ziehau { 1857841ab66cSSepherosa Ziehau struct ieee80211com *ic = ni->ni_ic; 1858841ab66cSSepherosa Ziehau struct ieee80211_node_table *nt = &ic->ic_sta; 1859841ab66cSSepherosa Ziehau struct ieee80211_node *nikey; 1860841ab66cSSepherosa Ziehau ieee80211_keyix keyix; 1861085ff963SMatthew Dillon int isowned, status; 1862841ab66cSSepherosa Ziehau 186332176cfdSRui Paulo /* 186432176cfdSRui Paulo * NB: We must beware of LOR here; deleting the key 186532176cfdSRui Paulo * can cause the crypto layer to block traffic updates 186632176cfdSRui Paulo * which can generate a LOR against the node table lock; 186732176cfdSRui Paulo * grab it here and stash the key index for our use below. 186832176cfdSRui Paulo * 186932176cfdSRui Paulo * Must also beware of recursion on the node table lock. 187032176cfdSRui Paulo * When called from node_cleanup we may already have 187132176cfdSRui Paulo * the node table lock held. Unfortunately there's no 187232176cfdSRui Paulo * way to separate out this path so we must do this 187332176cfdSRui Paulo * conditionally. 187432176cfdSRui Paulo */ 1875085ff963SMatthew Dillon isowned = IEEE80211_NODE_IS_LOCKED(nt); 1876085ff963SMatthew Dillon if (!isowned) 1877085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 187832176cfdSRui Paulo nikey = NULL; 187932176cfdSRui Paulo status = 1; /* NB: success */ 188032176cfdSRui Paulo if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) { 1881841ab66cSSepherosa Ziehau keyix = ni->ni_ucastkey.wk_rxkeyix; 188232176cfdSRui Paulo status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey); 1883841ab66cSSepherosa Ziehau if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) { 1884841ab66cSSepherosa Ziehau nikey = nt->nt_keyixmap[keyix]; 1885fc6d0222SSascha Wildner nt->nt_keyixmap[keyix] = NULL; 188632176cfdSRui Paulo } 188732176cfdSRui Paulo } 1888085ff963SMatthew Dillon if (!isowned) 1889085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 1890841ab66cSSepherosa Ziehau 1891841ab66cSSepherosa Ziehau if (nikey != NULL) { 1892841ab66cSSepherosa Ziehau KASSERT(nikey == ni, 1893841ab66cSSepherosa Ziehau ("key map out of sync, ni %p nikey %p", ni, nikey)); 189432176cfdSRui Paulo IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 18951e290df3SAntonio Huete Jimenez "%s: delete key map entry %p<%s> refcnt %d\n", 1896085ff963SMatthew Dillon __func__, ni, ether_sprintf(ni->ni_macaddr), 1897841ab66cSSepherosa Ziehau ieee80211_node_refcnt(ni)-1); 1898841ab66cSSepherosa Ziehau ieee80211_free_node(ni); 1899841ab66cSSepherosa Ziehau } 1900841ab66cSSepherosa Ziehau return status; 1901841ab66cSSepherosa Ziehau } 1902841ab66cSSepherosa Ziehau 1903841ab66cSSepherosa Ziehau /* 1904841ab66cSSepherosa Ziehau * Reclaim a node. If this is the last reference count then 1905841ab66cSSepherosa Ziehau * do the normal free work. Otherwise remove it from the node 1906841ab66cSSepherosa Ziehau * table and mark it gone by clearing the back-reference. 1907841ab66cSSepherosa Ziehau */ 1908841ab66cSSepherosa Ziehau static void 1909841ab66cSSepherosa Ziehau node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 1910841ab66cSSepherosa Ziehau { 1911085ff963SMatthew Dillon 1912085ff963SMatthew Dillon IEEE80211_NODE_LOCK_ASSERT(nt); 1913841ab66cSSepherosa Ziehau 191432176cfdSRui Paulo IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE, 19151e290df3SAntonio Huete Jimenez "%s: remove %p<%s> from %s table, refcnt %d\n", 1916085ff963SMatthew Dillon __func__, ni, ether_sprintf(ni->ni_macaddr), 1917841ab66cSSepherosa Ziehau nt->nt_name, ieee80211_node_refcnt(ni)-1); 1918841ab66cSSepherosa Ziehau /* 1919841ab66cSSepherosa Ziehau * Clear any entry in the unicast key mapping table. 1920841ab66cSSepherosa Ziehau * We need to do it here so rx lookups don't find it 1921841ab66cSSepherosa Ziehau * in the mapping table even if it's not in the hash 1922841ab66cSSepherosa Ziehau * table. We cannot depend on the mapping table entry 1923841ab66cSSepherosa Ziehau * being cleared because the node may not be free'd. 1924841ab66cSSepherosa Ziehau */ 19254f898719SImre Vadász (void)node_clear_keyixmap(nt, ni); 1926841ab66cSSepherosa Ziehau if (!ieee80211_node_dectestref(ni)) { 1927841ab66cSSepherosa Ziehau /* 1928841ab66cSSepherosa Ziehau * Other references are present, just remove the 1929841ab66cSSepherosa Ziehau * node from the table so it cannot be found. When 1930841ab66cSSepherosa Ziehau * the references are dropped storage will be 1931841ab66cSSepherosa Ziehau * reclaimed. 1932841ab66cSSepherosa Ziehau */ 1933841ab66cSSepherosa Ziehau TAILQ_REMOVE(&nt->nt_node, ni, ni_list); 1934841ab66cSSepherosa Ziehau LIST_REMOVE(ni, ni_hash); 1935841ab66cSSepherosa Ziehau ni->ni_table = NULL; /* clear reference */ 1936841ab66cSSepherosa Ziehau } else 1937841ab66cSSepherosa Ziehau _ieee80211_free_node(ni); 1938841ab66cSSepherosa Ziehau } 1939841ab66cSSepherosa Ziehau 1940841ab66cSSepherosa Ziehau /* 194132176cfdSRui Paulo * Node table support. 1942841ab66cSSepherosa Ziehau */ 194332176cfdSRui Paulo 1944841ab66cSSepherosa Ziehau static void 194532176cfdSRui Paulo ieee80211_node_table_init(struct ieee80211com *ic, 194632176cfdSRui Paulo struct ieee80211_node_table *nt, 194732176cfdSRui Paulo const char *name, int inact, int keyixmax) 1948841ab66cSSepherosa Ziehau { 1949085ff963SMatthew Dillon 195032176cfdSRui Paulo nt->nt_ic = ic; 19514f898719SImre Vadász IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name); 19524f898719SImre Vadász IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ic->ic_name); 195332176cfdSRui Paulo TAILQ_INIT(&nt->nt_node); 195432176cfdSRui Paulo nt->nt_name = name; 195532176cfdSRui Paulo nt->nt_scangen = 1; 195632176cfdSRui Paulo nt->nt_inact_init = inact; 195732176cfdSRui Paulo nt->nt_keyixmax = keyixmax; 195832176cfdSRui Paulo if (nt->nt_keyixmax > 0) { 1959*4f655ef5SMatthew Dillon #if defined(__DragonFly__) 196032176cfdSRui Paulo nt->nt_keyixmap = (struct ieee80211_node **) kmalloc( 196132176cfdSRui Paulo keyixmax * sizeof(struct ieee80211_node *), 1962fcaa651dSRui Paulo M_80211_NODE, M_INTWAIT | M_ZERO); 1963*4f655ef5SMatthew Dillon #else 1964*4f655ef5SMatthew Dillon nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC( 1965*4f655ef5SMatthew Dillon keyixmax * sizeof(struct ieee80211_node *), 1966*4f655ef5SMatthew Dillon M_80211_NODE, 1967*4f655ef5SMatthew Dillon IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 1968*4f655ef5SMatthew Dillon #endif 196932176cfdSRui Paulo if (nt->nt_keyixmap == NULL) 19704f898719SImre Vadász ic_printf(ic, 197132176cfdSRui Paulo "Cannot allocate key index map with %u entries\n", 197232176cfdSRui Paulo keyixmax); 197332176cfdSRui Paulo } else 197432176cfdSRui Paulo nt->nt_keyixmap = NULL; 1975841ab66cSSepherosa Ziehau } 197632176cfdSRui Paulo 197732176cfdSRui Paulo static void 197832176cfdSRui Paulo ieee80211_node_table_reset(struct ieee80211_node_table *nt, 197932176cfdSRui Paulo struct ieee80211vap *match) 198032176cfdSRui Paulo { 198132176cfdSRui Paulo struct ieee80211_node *ni, *next; 198232176cfdSRui Paulo 1983085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 1984085ff963SMatthew Dillon TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) { 198532176cfdSRui Paulo if (match != NULL && ni->ni_vap != match) 198632176cfdSRui Paulo continue; 198732176cfdSRui Paulo /* XXX can this happen? if so need's work */ 198832176cfdSRui Paulo if (ni->ni_associd != 0) { 198932176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 199032176cfdSRui Paulo 199132176cfdSRui Paulo if (vap->iv_auth->ia_node_leave != NULL) 199232176cfdSRui Paulo vap->iv_auth->ia_node_leave(ni); 199332176cfdSRui Paulo if (vap->iv_aid_bitmap != NULL) 199432176cfdSRui Paulo IEEE80211_AID_CLR(vap, ni->ni_associd); 199532176cfdSRui Paulo } 199632176cfdSRui Paulo ni->ni_wdsvap = NULL; /* clear reference */ 1997841ab66cSSepherosa Ziehau node_reclaim(nt, ni); 1998841ab66cSSepherosa Ziehau } 199932176cfdSRui Paulo if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) { 200032176cfdSRui Paulo /* 200132176cfdSRui Paulo * Make a separate pass to clear references to this vap 200232176cfdSRui Paulo * held by DWDS entries. They will not be matched above 200332176cfdSRui Paulo * because ni_vap will point to the ap vap but we still 200432176cfdSRui Paulo * need to clear ni_wdsvap when the WDS vap is destroyed 200532176cfdSRui Paulo * and/or reset. 200632176cfdSRui Paulo */ 2007085ff963SMatthew Dillon TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) 200832176cfdSRui Paulo if (ni->ni_wdsvap == match) 200932176cfdSRui Paulo ni->ni_wdsvap = NULL; 201032176cfdSRui Paulo } 2011085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 2012841ab66cSSepherosa Ziehau } 2013841ab66cSSepherosa Ziehau 201432176cfdSRui Paulo static void 201532176cfdSRui Paulo ieee80211_node_table_cleanup(struct ieee80211_node_table *nt) 201632176cfdSRui Paulo { 201732176cfdSRui Paulo ieee80211_node_table_reset(nt, NULL); 201832176cfdSRui Paulo if (nt->nt_keyixmap != NULL) { 201932176cfdSRui Paulo #ifdef DIAGNOSTIC 202032176cfdSRui Paulo /* XXX verify all entries are NULL */ 202132176cfdSRui Paulo int i; 202232176cfdSRui Paulo for (i = 0; i < nt->nt_keyixmax; i++) 202332176cfdSRui Paulo if (nt->nt_keyixmap[i] != NULL) 20242fafbbf5SRui Paulo kprintf("%s: %s[%u] still active\n", __func__, 202532176cfdSRui Paulo nt->nt_name, i); 202632176cfdSRui Paulo #endif 2027*4f655ef5SMatthew Dillon IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE); 202832176cfdSRui Paulo nt->nt_keyixmap = NULL; 202932176cfdSRui Paulo } 2030085ff963SMatthew Dillon IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt); 2031085ff963SMatthew Dillon IEEE80211_NODE_LOCK_DESTROY(nt); 2032841ab66cSSepherosa Ziehau } 2033841ab66cSSepherosa Ziehau 2034841ab66cSSepherosa Ziehau /* 2035841ab66cSSepherosa Ziehau * Timeout inactive stations and do related housekeeping. 2036841ab66cSSepherosa Ziehau * Note that we cannot hold the node lock while sending a 2037841ab66cSSepherosa Ziehau * frame as this would lead to a LOR. Instead we use a 2038841ab66cSSepherosa Ziehau * generation number to mark nodes that we've scanned and 2039841ab66cSSepherosa Ziehau * drop the lock and restart a scan if we have to time out 2040841ab66cSSepherosa Ziehau * a node. Since we are single-threaded by virtue of 2041f186073cSJoerg Sonnenberger * controlling the inactivity timer we can be sure this will 2042f186073cSJoerg Sonnenberger * process each node only once. 2043f186073cSJoerg Sonnenberger */ 2044841ab66cSSepherosa Ziehau static void 204532176cfdSRui Paulo ieee80211_timeout_stations(struct ieee80211com *ic) 2046f186073cSJoerg Sonnenberger { 204732176cfdSRui Paulo struct ieee80211_node_table *nt = &ic->ic_sta; 204832176cfdSRui Paulo struct ieee80211vap *vap; 204932176cfdSRui Paulo struct ieee80211_node *ni; 205032176cfdSRui Paulo int gen = 0; 2051f186073cSJoerg Sonnenberger 2052085ff963SMatthew Dillon IEEE80211_NODE_ITERATE_LOCK(nt); 205332176cfdSRui Paulo gen = ++nt->nt_scangen; 205432176cfdSRui Paulo restart: 2055085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 205632176cfdSRui Paulo TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 205732176cfdSRui Paulo if (ni->ni_scangen == gen) /* previously handled */ 205832176cfdSRui Paulo continue; 205932176cfdSRui Paulo ni->ni_scangen = gen; 2060f186073cSJoerg Sonnenberger /* 2061841ab66cSSepherosa Ziehau * Ignore entries for which have yet to receive an 2062841ab66cSSepherosa Ziehau * authentication frame. These are transient and 2063841ab66cSSepherosa Ziehau * will be reclaimed when the last reference to them 2064841ab66cSSepherosa Ziehau * goes away (when frame xmits complete). 2065f186073cSJoerg Sonnenberger */ 206632176cfdSRui Paulo vap = ni->ni_vap; 206732176cfdSRui Paulo /* 206832176cfdSRui Paulo * Only process stations when in RUN state. This 206932176cfdSRui Paulo * insures, for example, that we don't timeout an 207032176cfdSRui Paulo * inactive station during CAC. Note that CSA state 207132176cfdSRui Paulo * is actually handled in ieee80211_node_timeout as 207232176cfdSRui Paulo * it applies to more than timeout processing. 207332176cfdSRui Paulo */ 207432176cfdSRui Paulo if (vap->iv_state != IEEE80211_S_RUN) 207532176cfdSRui Paulo continue; 207632176cfdSRui Paulo /* XXX can vap be NULL? */ 207732176cfdSRui Paulo if ((vap->iv_opmode == IEEE80211_M_HOSTAP || 207832176cfdSRui Paulo vap->iv_opmode == IEEE80211_M_STA) && 2079841ab66cSSepherosa Ziehau (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 2080841ab66cSSepherosa Ziehau continue; 2081841ab66cSSepherosa Ziehau /* 2082841ab66cSSepherosa Ziehau * Free fragment if not needed anymore 2083841ab66cSSepherosa Ziehau * (last fragment older than 1s). 208432176cfdSRui Paulo * XXX doesn't belong here, move to node_age 2085841ab66cSSepherosa Ziehau */ 2086841ab66cSSepherosa Ziehau if (ni->ni_rxfrag[0] != NULL && 2087841ab66cSSepherosa Ziehau ticks > ni->ni_rxfragstamp + hz) { 2088841ab66cSSepherosa Ziehau m_freem(ni->ni_rxfrag[0]); 2089841ab66cSSepherosa Ziehau ni->ni_rxfrag[0] = NULL; 2090841ab66cSSepherosa Ziehau } 209132176cfdSRui Paulo if (ni->ni_inact > 0) { 209232176cfdSRui Paulo ni->ni_inact--; 209332176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni, 209432176cfdSRui Paulo "%s: inact %u inact_reload %u nrates %u", 209532176cfdSRui Paulo __func__, ni->ni_inact, ni->ni_inact_reload, 209632176cfdSRui Paulo ni->ni_rates.rs_nrates); 209732176cfdSRui Paulo } 2098841ab66cSSepherosa Ziehau /* 2099841ab66cSSepherosa Ziehau * Special case ourself; we may be idle for extended periods 2100841ab66cSSepherosa Ziehau * of time and regardless reclaiming our state is wrong. 210132176cfdSRui Paulo * XXX run ic_node_age 2102841ab66cSSepherosa Ziehau */ 210332176cfdSRui Paulo if (ni == vap->iv_bss) 2104841ab66cSSepherosa Ziehau continue; 210532176cfdSRui Paulo if (ni->ni_associd != 0 || 210632176cfdSRui Paulo (vap->iv_opmode == IEEE80211_M_IBSS || 210732176cfdSRui Paulo vap->iv_opmode == IEEE80211_M_AHDEMO)) { 2108841ab66cSSepherosa Ziehau /* 210932176cfdSRui Paulo * Age/drain resources held by the station. 2110841ab66cSSepherosa Ziehau */ 211132176cfdSRui Paulo ic->ic_node_age(ni); 2112841ab66cSSepherosa Ziehau /* 2113841ab66cSSepherosa Ziehau * Probe the station before time it out. We 2114841ab66cSSepherosa Ziehau * send a null data frame which may not be 2115841ab66cSSepherosa Ziehau * universally supported by drivers (need it 2116841ab66cSSepherosa Ziehau * for ps-poll support so it should be...). 211732176cfdSRui Paulo * 211832176cfdSRui Paulo * XXX don't probe the station unless we've 211932176cfdSRui Paulo * received a frame from them (and have 212032176cfdSRui Paulo * some idea of the rates they are capable 212132176cfdSRui Paulo * of); this will get fixed more properly 212232176cfdSRui Paulo * soon with better handling of the rate set. 2123841ab66cSSepherosa Ziehau */ 212432176cfdSRui Paulo if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) && 212532176cfdSRui Paulo (0 < ni->ni_inact && 212632176cfdSRui Paulo ni->ni_inact <= vap->iv_inact_probe) && 212732176cfdSRui Paulo ni->ni_rates.rs_nrates != 0) { 212832176cfdSRui Paulo IEEE80211_NOTE(vap, 2129841ab66cSSepherosa Ziehau IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, 2130841ab66cSSepherosa Ziehau ni, "%s", 2131841ab66cSSepherosa Ziehau "probe station due to inactivity"); 2132841ab66cSSepherosa Ziehau /* 2133841ab66cSSepherosa Ziehau * Grab a reference before unlocking the table 2134841ab66cSSepherosa Ziehau * so the node cannot be reclaimed before we 2135841ab66cSSepherosa Ziehau * send the frame. ieee80211_send_nulldata 2136841ab66cSSepherosa Ziehau * understands we've done this and reclaims the 2137841ab66cSSepherosa Ziehau * ref for us as needed. 2138841ab66cSSepherosa Ziehau */ 2139841ab66cSSepherosa Ziehau ieee80211_ref_node(ni); 2140085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 2141841ab66cSSepherosa Ziehau ieee80211_send_nulldata(ni); 2142841ab66cSSepherosa Ziehau /* XXX stat? */ 214332176cfdSRui Paulo goto restart; 2144841ab66cSSepherosa Ziehau } 2145841ab66cSSepherosa Ziehau } 214632176cfdSRui Paulo if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) && 214732176cfdSRui Paulo ni->ni_inact <= 0) { 214832176cfdSRui Paulo IEEE80211_NOTE(vap, 2149841ab66cSSepherosa Ziehau IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni, 2150841ab66cSSepherosa Ziehau "station timed out due to inactivity " 2151841ab66cSSepherosa Ziehau "(refcnt %u)", ieee80211_node_refcnt(ni)); 2152841ab66cSSepherosa Ziehau /* 2153841ab66cSSepherosa Ziehau * Send a deauthenticate frame and drop the station. 2154841ab66cSSepherosa Ziehau * This is somewhat complicated due to reference counts 2155841ab66cSSepherosa Ziehau * and locking. At this point a station will typically 2156841ab66cSSepherosa Ziehau * have a reference count of 1. ieee80211_node_leave 2157841ab66cSSepherosa Ziehau * will do a "free" of the node which will drop the 2158841ab66cSSepherosa Ziehau * reference count. But in the meantime a reference 2159841ab66cSSepherosa Ziehau * wil be held by the deauth frame. The actual reclaim 2160841ab66cSSepherosa Ziehau * of the node will happen either after the tx is 2161841ab66cSSepherosa Ziehau * completed or by ieee80211_node_leave. 216232176cfdSRui Paulo * 216332176cfdSRui Paulo * Separately we must drop the node lock before sending 216432176cfdSRui Paulo * in case the driver takes a lock, as this can result 216532176cfdSRui Paulo * in a LOR between the node lock and the driver lock. 2166841ab66cSSepherosa Ziehau */ 216732176cfdSRui Paulo ieee80211_ref_node(ni); 2168085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 2169841ab66cSSepherosa Ziehau if (ni->ni_associd != 0) { 217032176cfdSRui Paulo IEEE80211_SEND_MGMT(ni, 2171f186073cSJoerg Sonnenberger IEEE80211_FC0_SUBTYPE_DEAUTH, 2172f186073cSJoerg Sonnenberger IEEE80211_REASON_AUTH_EXPIRE); 2173841ab66cSSepherosa Ziehau } 217432176cfdSRui Paulo ieee80211_node_leave(ni); 217532176cfdSRui Paulo ieee80211_free_node(ni); 217632176cfdSRui Paulo vap->iv_stats.is_node_timeout++; 217732176cfdSRui Paulo goto restart; 2178f186073cSJoerg Sonnenberger } 2179f186073cSJoerg Sonnenberger } 2180085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 2181085ff963SMatthew Dillon 2182085ff963SMatthew Dillon IEEE80211_NODE_ITERATE_UNLOCK(nt); 218332176cfdSRui Paulo } 2184841ab66cSSepherosa Ziehau 218532176cfdSRui Paulo /* 218632176cfdSRui Paulo * Aggressively reclaim resources. This should be used 218732176cfdSRui Paulo * only in a critical situation to reclaim mbuf resources. 218832176cfdSRui Paulo */ 218932176cfdSRui Paulo void 219032176cfdSRui Paulo ieee80211_drain(struct ieee80211com *ic) 219132176cfdSRui Paulo { 219232176cfdSRui Paulo struct ieee80211_node_table *nt = &ic->ic_sta; 219332176cfdSRui Paulo struct ieee80211vap *vap; 219432176cfdSRui Paulo struct ieee80211_node *ni; 219532176cfdSRui Paulo 2196085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 219732176cfdSRui Paulo TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 219832176cfdSRui Paulo /* 219932176cfdSRui Paulo * Ignore entries for which have yet to receive an 220032176cfdSRui Paulo * authentication frame. These are transient and 220132176cfdSRui Paulo * will be reclaimed when the last reference to them 220232176cfdSRui Paulo * goes away (when frame xmits complete). 220332176cfdSRui Paulo */ 220432176cfdSRui Paulo vap = ni->ni_vap; 220532176cfdSRui Paulo /* 220632176cfdSRui Paulo * Only process stations when in RUN state. This 220732176cfdSRui Paulo * insures, for example, that we don't timeout an 220832176cfdSRui Paulo * inactive station during CAC. Note that CSA state 220932176cfdSRui Paulo * is actually handled in ieee80211_node_timeout as 221032176cfdSRui Paulo * it applies to more than timeout processing. 221132176cfdSRui Paulo */ 221232176cfdSRui Paulo if (vap->iv_state != IEEE80211_S_RUN) 221332176cfdSRui Paulo continue; 221432176cfdSRui Paulo /* XXX can vap be NULL? */ 221532176cfdSRui Paulo if ((vap->iv_opmode == IEEE80211_M_HOSTAP || 221632176cfdSRui Paulo vap->iv_opmode == IEEE80211_M_STA) && 221732176cfdSRui Paulo (ni->ni_flags & IEEE80211_NODE_AREF) == 0) 221832176cfdSRui Paulo continue; 221932176cfdSRui Paulo /* 222032176cfdSRui Paulo * Free fragments. 222132176cfdSRui Paulo * XXX doesn't belong here, move to node_drain 222232176cfdSRui Paulo */ 222332176cfdSRui Paulo if (ni->ni_rxfrag[0] != NULL) { 222432176cfdSRui Paulo m_freem(ni->ni_rxfrag[0]); 222532176cfdSRui Paulo ni->ni_rxfrag[0] = NULL; 222632176cfdSRui Paulo } 222732176cfdSRui Paulo /* 222832176cfdSRui Paulo * Drain resources held by the station. 222932176cfdSRui Paulo */ 223032176cfdSRui Paulo ic->ic_node_drain(ni); 223132176cfdSRui Paulo } 2232085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 223332176cfdSRui Paulo } 223432176cfdSRui Paulo 223532176cfdSRui Paulo /* 223632176cfdSRui Paulo * Per-ieee80211com inactivity timer callback. 223732176cfdSRui Paulo */ 2238085ff963SMatthew Dillon void 2239085ff963SMatthew Dillon ieee80211_node_timeout(void *arg) 224032176cfdSRui Paulo { 224132176cfdSRui Paulo struct ieee80211com *ic = arg; 224232176cfdSRui Paulo 224332176cfdSRui Paulo /* 224432176cfdSRui Paulo * Defer timeout processing if a channel switch is pending. 224532176cfdSRui Paulo * We typically need to be mute so not doing things that 224632176cfdSRui Paulo * might generate frames is good to handle in one place. 2247*4f655ef5SMatthew Dillon * Suppressing the station timeout processing may extend the 224832176cfdSRui Paulo * lifetime of inactive stations (by not decrementing their 224932176cfdSRui Paulo * idle counters) but this should be ok unless the CSA is 225032176cfdSRui Paulo * active for an unusually long time. 225132176cfdSRui Paulo */ 225232176cfdSRui Paulo if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) { 225332176cfdSRui Paulo ieee80211_scan_timeout(ic); 225432176cfdSRui Paulo ieee80211_timeout_stations(ic); 225532176cfdSRui Paulo ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT); 225632176cfdSRui Paulo 2257085ff963SMatthew Dillon IEEE80211_LOCK(ic); 225832176cfdSRui Paulo ieee80211_erp_timeout(ic); 225932176cfdSRui Paulo ieee80211_ht_timeout(ic); 2260085ff963SMatthew Dillon IEEE80211_UNLOCK(ic); 226132176cfdSRui Paulo } 226232176cfdSRui Paulo callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz, 2263085ff963SMatthew Dillon ieee80211_node_timeout, ic); 2264f186073cSJoerg Sonnenberger } 2265f186073cSJoerg Sonnenberger 2266085ff963SMatthew Dillon /* 2267085ff963SMatthew Dillon * Iterate over the node table and return an array of ref'ed nodes. 2268085ff963SMatthew Dillon * 2269085ff963SMatthew Dillon * This is separated out from calling the actual node function so that 2270085ff963SMatthew Dillon * no LORs will occur. 2271085ff963SMatthew Dillon * 2272085ff963SMatthew Dillon * If there are too many nodes (ie, the number of nodes doesn't fit 2273085ff963SMatthew Dillon * within 'max_aid' entries) then the node references will be freed 2274085ff963SMatthew Dillon * and an error will be returned. 2275085ff963SMatthew Dillon * 2276085ff963SMatthew Dillon * The responsibility of allocating and freeing "ni_arr" is up to 2277085ff963SMatthew Dillon * the caller. 2278085ff963SMatthew Dillon */ 2279085ff963SMatthew Dillon int 2280085ff963SMatthew Dillon ieee80211_iterate_nt(struct ieee80211_node_table *nt, 2281085ff963SMatthew Dillon struct ieee80211_node **ni_arr, uint16_t max_aid) 2282085ff963SMatthew Dillon { 2283085ff963SMatthew Dillon u_int gen; 2284085ff963SMatthew Dillon int i, j, ret; 2285085ff963SMatthew Dillon struct ieee80211_node *ni; 2286085ff963SMatthew Dillon 2287085ff963SMatthew Dillon IEEE80211_NODE_ITERATE_LOCK(nt); 2288085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 2289085ff963SMatthew Dillon 2290085ff963SMatthew Dillon gen = ++nt->nt_scangen; 2291085ff963SMatthew Dillon i = ret = 0; 2292085ff963SMatthew Dillon 2293085ff963SMatthew Dillon /* 2294085ff963SMatthew Dillon * We simply assume here that since the node 2295085ff963SMatthew Dillon * scan generation doesn't change (as 2296085ff963SMatthew Dillon * we are holding both the node table and 2297085ff963SMatthew Dillon * node table iteration locks), we can simply 2298085ff963SMatthew Dillon * assign it to the node here. 2299085ff963SMatthew Dillon */ 2300085ff963SMatthew Dillon TAILQ_FOREACH(ni, &nt->nt_node, ni_list) { 2301085ff963SMatthew Dillon if (i >= max_aid) { 2302085ff963SMatthew Dillon ret = E2BIG; 23034f898719SImre Vadász ic_printf(nt->nt_ic, "Node array overflow: max=%u", 23044f898719SImre Vadász max_aid); 2305085ff963SMatthew Dillon break; 2306085ff963SMatthew Dillon } 2307085ff963SMatthew Dillon ni_arr[i] = ieee80211_ref_node(ni); 2308085ff963SMatthew Dillon ni_arr[i]->ni_scangen = gen; 2309085ff963SMatthew Dillon i++; 2310085ff963SMatthew Dillon } 2311085ff963SMatthew Dillon 2312085ff963SMatthew Dillon /* 2313085ff963SMatthew Dillon * It's safe to unlock here. 2314085ff963SMatthew Dillon * 2315085ff963SMatthew Dillon * If we're successful, the list is returned. 2316085ff963SMatthew Dillon * If we're unsuccessful, the list is ignored 2317085ff963SMatthew Dillon * and we remove our references. 2318085ff963SMatthew Dillon * 2319085ff963SMatthew Dillon * This avoids any potential LOR with 2320085ff963SMatthew Dillon * ieee80211_free_node(). 2321085ff963SMatthew Dillon */ 2322085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 2323085ff963SMatthew Dillon IEEE80211_NODE_ITERATE_UNLOCK(nt); 2324085ff963SMatthew Dillon 2325085ff963SMatthew Dillon /* 2326085ff963SMatthew Dillon * If ret is non-zero, we hit some kind of error. 2327085ff963SMatthew Dillon * Rather than walking some nodes, we'll walk none 2328085ff963SMatthew Dillon * of them. 2329085ff963SMatthew Dillon */ 2330085ff963SMatthew Dillon if (ret) { 2331085ff963SMatthew Dillon for (j = 0; j < i; j++) { 2332085ff963SMatthew Dillon /* ieee80211_free_node() locks by itself */ 2333085ff963SMatthew Dillon ieee80211_free_node(ni_arr[j]); 2334085ff963SMatthew Dillon } 2335085ff963SMatthew Dillon } 2336085ff963SMatthew Dillon 2337085ff963SMatthew Dillon return (ret); 2338085ff963SMatthew Dillon } 2339085ff963SMatthew Dillon 2340085ff963SMatthew Dillon /* 2341085ff963SMatthew Dillon * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes() 2342085ff963SMatthew Dillon * reference in the source. 2343085ff963SMatthew Dillon * 2344085ff963SMatthew Dillon * Note that this fetches 'max_aid' from the first VAP, rather than finding 2345085ff963SMatthew Dillon * the largest max_aid from all VAPs. 2346085ff963SMatthew Dillon */ 2347f186073cSJoerg Sonnenberger void 234832176cfdSRui Paulo ieee80211_iterate_nodes(struct ieee80211_node_table *nt, 234932176cfdSRui Paulo ieee80211_iter_func *f, void *arg) 2350f186073cSJoerg Sonnenberger { 2351085ff963SMatthew Dillon struct ieee80211_node **ni_arr; 2352085ff963SMatthew Dillon size_t size; 2353085ff963SMatthew Dillon int i; 2354085ff963SMatthew Dillon uint16_t max_aid; 2355085ff963SMatthew Dillon struct ieee80211vap *vap; 2356f186073cSJoerg Sonnenberger 2357085ff963SMatthew Dillon /* Overdoing it default */ 2358085ff963SMatthew Dillon max_aid = IEEE80211_AID_MAX; 2359085ff963SMatthew Dillon 2360085ff963SMatthew Dillon /* Handle the case of there being no vaps just yet */ 2361085ff963SMatthew Dillon vap = TAILQ_FIRST(&nt->nt_ic->ic_vaps); 2362085ff963SMatthew Dillon if (vap != NULL) 2363085ff963SMatthew Dillon max_aid = vap->iv_max_aid; 2364085ff963SMatthew Dillon 2365085ff963SMatthew Dillon size = max_aid * sizeof(struct ieee80211_node *); 2366*4f655ef5SMatthew Dillon #if defined(__DragonFly__) 2367085ff963SMatthew Dillon ni_arr = (struct ieee80211_node **) kmalloc(size, M_80211_NODE, 2368085ff963SMatthew Dillon M_INTWAIT | M_ZERO); 2369*4f655ef5SMatthew Dillon #else 2370*4f655ef5SMatthew Dillon ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE, 2371*4f655ef5SMatthew Dillon IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 2372*4f655ef5SMatthew Dillon #endif 2373085ff963SMatthew Dillon if (ni_arr == NULL) 2374085ff963SMatthew Dillon return; 2375085ff963SMatthew Dillon 2376085ff963SMatthew Dillon /* 2377085ff963SMatthew Dillon * If this fails, the node table won't have any 2378085ff963SMatthew Dillon * valid entries - ieee80211_iterate_nt() frees 2379085ff963SMatthew Dillon * the references to them. So don't try walking 2380085ff963SMatthew Dillon * the table; just skip to the end and free the 2381085ff963SMatthew Dillon * temporary memory. 2382085ff963SMatthew Dillon */ 2383085ff963SMatthew Dillon if (ieee80211_iterate_nt(nt, ni_arr, max_aid) != 0) 2384085ff963SMatthew Dillon goto done; 2385085ff963SMatthew Dillon 2386085ff963SMatthew Dillon for (i = 0; i < max_aid; i++) { 2387085ff963SMatthew Dillon if (ni_arr[i] == NULL) /* end of the list */ 2388085ff963SMatthew Dillon break; 2389085ff963SMatthew Dillon (*f)(arg, ni_arr[i]); 2390085ff963SMatthew Dillon /* ieee80211_free_node() locks by itself */ 2391085ff963SMatthew Dillon ieee80211_free_node(ni_arr[i]); 239232176cfdSRui Paulo } 2393085ff963SMatthew Dillon 2394085ff963SMatthew Dillon done: 2395*4f655ef5SMatthew Dillon IEEE80211_FREE(ni_arr, M_80211_NODE); 2396841ab66cSSepherosa Ziehau } 2397841ab66cSSepherosa Ziehau 2398841ab66cSSepherosa Ziehau void 2399841ab66cSSepherosa Ziehau ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni) 2400841ab66cSSepherosa Ziehau { 2401085ff963SMatthew Dillon kprintf("0x%p: mac %s refcnt %d\n", ni, 2402085ff963SMatthew Dillon ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)); 240332176cfdSRui Paulo kprintf("\tscangen %u authmode %u flags 0x%x\n", 240432176cfdSRui Paulo ni->ni_scangen, ni->ni_authmode, ni->ni_flags); 2405a6ec04bcSSascha Wildner kprintf("\tassocid 0x%x txpower %u vlan %u\n", 2406841ab66cSSepherosa Ziehau ni->ni_associd, ni->ni_txpower, ni->ni_vlan); 2407a6ec04bcSSascha Wildner kprintf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n", 240832176cfdSRui Paulo ni->ni_txseqs[IEEE80211_NONQOS_TID], 240932176cfdSRui Paulo ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT, 241032176cfdSRui Paulo ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK, 2411841ab66cSSepherosa Ziehau ni->ni_rxfragstamp); 241232176cfdSRui Paulo kprintf("\trssi %d noise %d intval %u capinfo 0x%x\n", 241332176cfdSRui Paulo node_getrssi(ni), ni->ni_noise, 241432176cfdSRui Paulo ni->ni_intval, ni->ni_capinfo); 24151e290df3SAntonio Huete Jimenez kprintf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n", 2416085ff963SMatthew Dillon ether_sprintf(ni->ni_bssid), 2417841ab66cSSepherosa Ziehau ni->ni_esslen, ni->ni_essid, 2418841ab66cSSepherosa Ziehau ni->ni_chan->ic_freq, ni->ni_chan->ic_flags); 241932176cfdSRui Paulo kprintf("\tinact %u inact_reload %u txrate %u\n", 242032176cfdSRui Paulo ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate); 242132176cfdSRui Paulo kprintf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n", 242232176cfdSRui Paulo ni->ni_htcap, ni->ni_htparam, 242332176cfdSRui Paulo ni->ni_htctlchan, ni->ni_ht2ndchan); 242432176cfdSRui Paulo kprintf("\thtopmode %x htstbc %x chw %u\n", 242532176cfdSRui Paulo ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw); 2426841ab66cSSepherosa Ziehau } 2427841ab66cSSepherosa Ziehau 2428841ab66cSSepherosa Ziehau void 2429841ab66cSSepherosa Ziehau ieee80211_dump_nodes(struct ieee80211_node_table *nt) 2430841ab66cSSepherosa Ziehau { 2431841ab66cSSepherosa Ziehau ieee80211_iterate_nodes(nt, 2432841ab66cSSepherosa Ziehau (ieee80211_iter_func *) ieee80211_dump_node, nt); 2433841ab66cSSepherosa Ziehau } 2434841ab66cSSepherosa Ziehau 243532176cfdSRui Paulo static void 243632176cfdSRui Paulo ieee80211_notify_erp_locked(struct ieee80211com *ic) 243732176cfdSRui Paulo { 243832176cfdSRui Paulo struct ieee80211vap *vap; 243932176cfdSRui Paulo 2440085ff963SMatthew Dillon IEEE80211_LOCK_ASSERT(ic); 2441085ff963SMatthew Dillon 244232176cfdSRui Paulo TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 244332176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_HOSTAP) 244432176cfdSRui Paulo ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP); 244532176cfdSRui Paulo } 244632176cfdSRui Paulo 244732176cfdSRui Paulo void 244832176cfdSRui Paulo ieee80211_notify_erp(struct ieee80211com *ic) 244932176cfdSRui Paulo { 2450085ff963SMatthew Dillon IEEE80211_LOCK(ic); 245132176cfdSRui Paulo ieee80211_notify_erp_locked(ic); 2452085ff963SMatthew Dillon IEEE80211_UNLOCK(ic); 245332176cfdSRui Paulo } 245432176cfdSRui Paulo 2455841ab66cSSepherosa Ziehau /* 2456841ab66cSSepherosa Ziehau * Handle a station joining an 11g network. 2457841ab66cSSepherosa Ziehau */ 2458841ab66cSSepherosa Ziehau static void 245932176cfdSRui Paulo ieee80211_node_join_11g(struct ieee80211_node *ni) 2460841ab66cSSepherosa Ziehau { 246132176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic; 246232176cfdSRui Paulo 2463085ff963SMatthew Dillon IEEE80211_LOCK_ASSERT(ic); 2464085ff963SMatthew Dillon 2465841ab66cSSepherosa Ziehau /* 2466841ab66cSSepherosa Ziehau * Station isn't capable of short slot time. Bump 2467841ab66cSSepherosa Ziehau * the count of long slot time stations and disable 2468841ab66cSSepherosa Ziehau * use of short slot time. Note that the actual switch 2469841ab66cSSepherosa Ziehau * over to long slot time use may not occur until the 2470841ab66cSSepherosa Ziehau * next beacon transmission (per sec. 7.3.1.4 of 11g). 2471841ab66cSSepherosa Ziehau */ 2472841ab66cSSepherosa Ziehau if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2473841ab66cSSepherosa Ziehau ic->ic_longslotsta++; 247432176cfdSRui Paulo IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 247532176cfdSRui Paulo "station needs long slot time, count %d", 247632176cfdSRui Paulo ic->ic_longslotsta); 2477841ab66cSSepherosa Ziehau /* XXX vap's w/ conflicting needs won't work */ 247832176cfdSRui Paulo if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) { 247932176cfdSRui Paulo /* 248032176cfdSRui Paulo * Don't force slot time when switched to turbo 248132176cfdSRui Paulo * mode as non-ERP stations won't be present; this 248232176cfdSRui Paulo * need only be done when on the normal G channel. 248332176cfdSRui Paulo */ 2484841ab66cSSepherosa Ziehau ieee80211_set_shortslottime(ic, 0); 2485841ab66cSSepherosa Ziehau } 248632176cfdSRui Paulo } 2487841ab66cSSepherosa Ziehau /* 2488841ab66cSSepherosa Ziehau * If the new station is not an ERP station 2489841ab66cSSepherosa Ziehau * then bump the counter and enable protection 2490841ab66cSSepherosa Ziehau * if configured. 2491841ab66cSSepherosa Ziehau */ 249232176cfdSRui Paulo if (!ieee80211_iserp_rateset(&ni->ni_rates)) { 2493841ab66cSSepherosa Ziehau ic->ic_nonerpsta++; 249432176cfdSRui Paulo IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 249532176cfdSRui Paulo "station is !ERP, %d non-ERP stations associated", 249632176cfdSRui Paulo ic->ic_nonerpsta); 2497841ab66cSSepherosa Ziehau /* 2498841ab66cSSepherosa Ziehau * If station does not support short preamble 2499841ab66cSSepherosa Ziehau * then we must enable use of Barker preamble. 2500841ab66cSSepherosa Ziehau */ 2501841ab66cSSepherosa Ziehau if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) { 250232176cfdSRui Paulo IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 250332176cfdSRui Paulo "%s", "station needs long preamble"); 250432176cfdSRui Paulo ic->ic_flags |= IEEE80211_F_USEBARKER; 250532176cfdSRui Paulo ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 2506841ab66cSSepherosa Ziehau } 250732176cfdSRui Paulo /* 250832176cfdSRui Paulo * If protection is configured and this is the first 250932176cfdSRui Paulo * indication we should use protection, enable it. 251032176cfdSRui Paulo */ 251132176cfdSRui Paulo if (ic->ic_protmode != IEEE80211_PROT_NONE && 251232176cfdSRui Paulo ic->ic_nonerpsta == 1 && 251332176cfdSRui Paulo (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) { 251432176cfdSRui Paulo IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 251532176cfdSRui Paulo "%s: enable use of protection\n", __func__); 251632176cfdSRui Paulo ic->ic_flags |= IEEE80211_F_USEPROT; 251732176cfdSRui Paulo ieee80211_notify_erp_locked(ic); 251832176cfdSRui Paulo } 2519841ab66cSSepherosa Ziehau } else 2520841ab66cSSepherosa Ziehau ni->ni_flags |= IEEE80211_NODE_ERP; 2521841ab66cSSepherosa Ziehau } 2522841ab66cSSepherosa Ziehau 2523841ab66cSSepherosa Ziehau void 252432176cfdSRui Paulo ieee80211_node_join(struct ieee80211_node *ni, int resp) 2525841ab66cSSepherosa Ziehau { 252632176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic; 252732176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 2528841ab66cSSepherosa Ziehau int newassoc; 2529841ab66cSSepherosa Ziehau 2530841ab66cSSepherosa Ziehau if (ni->ni_associd == 0) { 2531841ab66cSSepherosa Ziehau uint16_t aid; 2532841ab66cSSepherosa Ziehau 253332176cfdSRui Paulo KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap")); 2534841ab66cSSepherosa Ziehau /* 2535841ab66cSSepherosa Ziehau * It would be good to search the bitmap 2536841ab66cSSepherosa Ziehau * more efficiently, but this will do for now. 2537841ab66cSSepherosa Ziehau */ 253832176cfdSRui Paulo for (aid = 1; aid < vap->iv_max_aid; aid++) { 253932176cfdSRui Paulo if (!IEEE80211_AID_ISSET(vap, aid)) 2540841ab66cSSepherosa Ziehau break; 2541841ab66cSSepherosa Ziehau } 254232176cfdSRui Paulo if (aid >= vap->iv_max_aid) { 254332176cfdSRui Paulo IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY); 254432176cfdSRui Paulo ieee80211_node_leave(ni); 2545841ab66cSSepherosa Ziehau return; 2546841ab66cSSepherosa Ziehau } 2547841ab66cSSepherosa Ziehau ni->ni_associd = aid | 0xc000; 2548cec73927SMatthew Dillon ni->ni_jointime = time_uptime; 2549085ff963SMatthew Dillon IEEE80211_LOCK(ic); 255032176cfdSRui Paulo IEEE80211_AID_SET(vap, ni->ni_associd); 255132176cfdSRui Paulo vap->iv_sta_assoc++; 2552841ab66cSSepherosa Ziehau ic->ic_sta_assoc++; 255332176cfdSRui Paulo 255432176cfdSRui Paulo if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan)) 255532176cfdSRui Paulo ieee80211_ht_node_join(ni); 255632176cfdSRui Paulo if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 255732176cfdSRui Paulo IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 255832176cfdSRui Paulo ieee80211_node_join_11g(ni); 2559085ff963SMatthew Dillon IEEE80211_UNLOCK(ic); 256032176cfdSRui Paulo 2561841ab66cSSepherosa Ziehau newassoc = 1; 2562841ab66cSSepherosa Ziehau } else 2563841ab66cSSepherosa Ziehau newassoc = 0; 2564841ab66cSSepherosa Ziehau 256532176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni, 256632176cfdSRui Paulo "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s", 2567841ab66cSSepherosa Ziehau IEEE80211_NODE_AID(ni), 2568841ab66cSSepherosa Ziehau ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long", 2569841ab66cSSepherosa Ziehau ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", 2570841ab66cSSepherosa Ziehau ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "", 257132176cfdSRui Paulo ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "", 257232176cfdSRui Paulo ni->ni_flags & IEEE80211_NODE_HT ? 257332176cfdSRui Paulo (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "", 257432176cfdSRui Paulo ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "", 257532176cfdSRui Paulo ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" : 257632176cfdSRui Paulo ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "", 257732176cfdSRui Paulo ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "", 257832176cfdSRui Paulo IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ? 257932176cfdSRui Paulo ", fast-frames" : "", 258032176cfdSRui Paulo IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ? 258132176cfdSRui Paulo ", turbo" : "" 2582841ab66cSSepherosa Ziehau ); 2583841ab66cSSepherosa Ziehau 258432176cfdSRui Paulo ieee80211_node_setuptxparms(ni); 25854fbce6bdSSascha Wildner ieee80211_ratectl_node_init(ni); 2586841ab66cSSepherosa Ziehau /* give driver a chance to setup state like ni_txrate */ 2587841ab66cSSepherosa Ziehau if (ic->ic_newassoc != NULL) 2588841ab66cSSepherosa Ziehau ic->ic_newassoc(ni, newassoc); 258932176cfdSRui Paulo IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS); 2590841ab66cSSepherosa Ziehau /* tell the authenticator about new station */ 259132176cfdSRui Paulo if (vap->iv_auth->ia_node_join != NULL) 259232176cfdSRui Paulo vap->iv_auth->ia_node_join(ni); 259332176cfdSRui Paulo ieee80211_notify_node_join(ni, 259432176cfdSRui Paulo resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 259532176cfdSRui Paulo } 259632176cfdSRui Paulo 259732176cfdSRui Paulo static void 259832176cfdSRui Paulo disable_protection(struct ieee80211com *ic) 259932176cfdSRui Paulo { 260032176cfdSRui Paulo KASSERT(ic->ic_nonerpsta == 0 && 260132176cfdSRui Paulo (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0, 260232176cfdSRui Paulo ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta, 260332176cfdSRui Paulo ic->ic_flags_ext)); 260432176cfdSRui Paulo 260532176cfdSRui Paulo ic->ic_flags &= ~IEEE80211_F_USEPROT; 260632176cfdSRui Paulo /* XXX verify mode? */ 260732176cfdSRui Paulo if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) { 260832176cfdSRui Paulo ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 260932176cfdSRui Paulo ic->ic_flags &= ~IEEE80211_F_USEBARKER; 261032176cfdSRui Paulo } 261132176cfdSRui Paulo ieee80211_notify_erp_locked(ic); 2612841ab66cSSepherosa Ziehau } 2613841ab66cSSepherosa Ziehau 2614841ab66cSSepherosa Ziehau /* 2615841ab66cSSepherosa Ziehau * Handle a station leaving an 11g network. 2616841ab66cSSepherosa Ziehau */ 2617841ab66cSSepherosa Ziehau static void 261832176cfdSRui Paulo ieee80211_node_leave_11g(struct ieee80211_node *ni) 2619841ab66cSSepherosa Ziehau { 262032176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic; 2621841ab66cSSepherosa Ziehau 2622085ff963SMatthew Dillon IEEE80211_LOCK_ASSERT(ic); 2623085ff963SMatthew Dillon 262432176cfdSRui Paulo KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan), 262532176cfdSRui Paulo ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq, 262632176cfdSRui Paulo ic->ic_bsschan->ic_flags)); 2627841ab66cSSepherosa Ziehau 2628841ab66cSSepherosa Ziehau /* 2629841ab66cSSepherosa Ziehau * If a long slot station do the slot time bookkeeping. 2630841ab66cSSepherosa Ziehau */ 2631841ab66cSSepherosa Ziehau if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) { 2632841ab66cSSepherosa Ziehau KASSERT(ic->ic_longslotsta > 0, 2633841ab66cSSepherosa Ziehau ("bogus long slot station count %d", ic->ic_longslotsta)); 2634841ab66cSSepherosa Ziehau ic->ic_longslotsta--; 263532176cfdSRui Paulo IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 263632176cfdSRui Paulo "long slot time station leaves, count now %d", 263732176cfdSRui Paulo ic->ic_longslotsta); 2638841ab66cSSepherosa Ziehau if (ic->ic_longslotsta == 0) { 2639841ab66cSSepherosa Ziehau /* 2640841ab66cSSepherosa Ziehau * Re-enable use of short slot time if supported 2641841ab66cSSepherosa Ziehau * and not operating in IBSS mode (per spec). 2642841ab66cSSepherosa Ziehau */ 2643841ab66cSSepherosa Ziehau if ((ic->ic_caps & IEEE80211_C_SHSLOT) && 2644841ab66cSSepherosa Ziehau ic->ic_opmode != IEEE80211_M_IBSS) { 264532176cfdSRui Paulo IEEE80211_DPRINTF(ni->ni_vap, 264632176cfdSRui Paulo IEEE80211_MSG_ASSOC, 2647841ab66cSSepherosa Ziehau "%s: re-enable use of short slot time\n", 2648841ab66cSSepherosa Ziehau __func__); 2649841ab66cSSepherosa Ziehau ieee80211_set_shortslottime(ic, 1); 2650841ab66cSSepherosa Ziehau } 2651841ab66cSSepherosa Ziehau } 2652841ab66cSSepherosa Ziehau } 2653841ab66cSSepherosa Ziehau /* 2654841ab66cSSepherosa Ziehau * If a non-ERP station do the protection-related bookkeeping. 2655841ab66cSSepherosa Ziehau */ 2656841ab66cSSepherosa Ziehau if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) { 2657841ab66cSSepherosa Ziehau KASSERT(ic->ic_nonerpsta > 0, 2658841ab66cSSepherosa Ziehau ("bogus non-ERP station count %d", ic->ic_nonerpsta)); 2659841ab66cSSepherosa Ziehau ic->ic_nonerpsta--; 266032176cfdSRui Paulo IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni, 266132176cfdSRui Paulo "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta, 266232176cfdSRui Paulo (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ? 266332176cfdSRui Paulo " (non-ERP sta present)" : ""); 266432176cfdSRui Paulo if (ic->ic_nonerpsta == 0 && 266532176cfdSRui Paulo (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) { 266632176cfdSRui Paulo IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC, 2667841ab66cSSepherosa Ziehau "%s: disable use of protection\n", __func__); 266832176cfdSRui Paulo disable_protection(ic); 2669841ab66cSSepherosa Ziehau } 2670841ab66cSSepherosa Ziehau } 2671841ab66cSSepherosa Ziehau } 267232176cfdSRui Paulo 267332176cfdSRui Paulo /* 267432176cfdSRui Paulo * Time out presence of an overlapping bss with non-ERP 267532176cfdSRui Paulo * stations. When operating in hostap mode we listen for 267632176cfdSRui Paulo * beacons from other stations and if we identify a non-ERP 267732176cfdSRui Paulo * station is present we enable protection. To identify 267832176cfdSRui Paulo * when all non-ERP stations are gone we time out this 267932176cfdSRui Paulo * condition. 268032176cfdSRui Paulo */ 268132176cfdSRui Paulo static void 268232176cfdSRui Paulo ieee80211_erp_timeout(struct ieee80211com *ic) 268332176cfdSRui Paulo { 2684085ff963SMatthew Dillon 2685085ff963SMatthew Dillon IEEE80211_LOCK_ASSERT(ic); 2686085ff963SMatthew Dillon 268732176cfdSRui Paulo if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) && 2688*4f655ef5SMatthew Dillon ieee80211_time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) { 268932176cfdSRui Paulo #if 0 269032176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni, 269132176cfdSRui Paulo "%s", "age out non-ERP sta present on channel"); 269232176cfdSRui Paulo #endif 269332176cfdSRui Paulo ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR; 269432176cfdSRui Paulo if (ic->ic_nonerpsta == 0) 269532176cfdSRui Paulo disable_protection(ic); 269632176cfdSRui Paulo } 2697841ab66cSSepherosa Ziehau } 2698841ab66cSSepherosa Ziehau 2699841ab66cSSepherosa Ziehau /* 2700841ab66cSSepherosa Ziehau * Handle bookkeeping for station deauthentication/disassociation 2701841ab66cSSepherosa Ziehau * when operating as an ap. 2702841ab66cSSepherosa Ziehau */ 2703841ab66cSSepherosa Ziehau void 270432176cfdSRui Paulo ieee80211_node_leave(struct ieee80211_node *ni) 2705841ab66cSSepherosa Ziehau { 270632176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic; 270732176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 2708841ab66cSSepherosa Ziehau struct ieee80211_node_table *nt = ni->ni_table; 2709841ab66cSSepherosa Ziehau 271032176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni, 271132176cfdSRui Paulo "station with aid %d leaves", IEEE80211_NODE_AID(ni)); 2712841ab66cSSepherosa Ziehau 271332176cfdSRui Paulo KASSERT(vap->iv_opmode != IEEE80211_M_STA, 271432176cfdSRui Paulo ("unexpected operating mode %u", vap->iv_opmode)); 2715841ab66cSSepherosa Ziehau /* 2716841ab66cSSepherosa Ziehau * If node wasn't previously associated all 2717841ab66cSSepherosa Ziehau * we need to do is reclaim the reference. 2718841ab66cSSepherosa Ziehau */ 2719841ab66cSSepherosa Ziehau /* XXX ibss mode bypasses 11g and notification */ 2720841ab66cSSepherosa Ziehau if (ni->ni_associd == 0) 2721841ab66cSSepherosa Ziehau goto done; 2722841ab66cSSepherosa Ziehau /* 2723841ab66cSSepherosa Ziehau * Tell the authenticator the station is leaving. 2724841ab66cSSepherosa Ziehau * Note that we must do this before yanking the 2725841ab66cSSepherosa Ziehau * association id as the authenticator uses the 2726841ab66cSSepherosa Ziehau * associd to locate it's state block. 2727841ab66cSSepherosa Ziehau */ 272832176cfdSRui Paulo if (vap->iv_auth->ia_node_leave != NULL) 272932176cfdSRui Paulo vap->iv_auth->ia_node_leave(ni); 273032176cfdSRui Paulo 2731085ff963SMatthew Dillon IEEE80211_LOCK(ic); 273232176cfdSRui Paulo IEEE80211_AID_CLR(vap, ni->ni_associd); 273332176cfdSRui Paulo vap->iv_sta_assoc--; 2734841ab66cSSepherosa Ziehau ic->ic_sta_assoc--; 2735841ab66cSSepherosa Ziehau 273632176cfdSRui Paulo if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan)) 273732176cfdSRui Paulo ieee80211_ht_node_leave(ni); 273832176cfdSRui Paulo if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) && 273932176cfdSRui Paulo IEEE80211_IS_CHAN_FULL(ic->ic_bsschan)) 274032176cfdSRui Paulo ieee80211_node_leave_11g(ni); 2741085ff963SMatthew Dillon IEEE80211_UNLOCK(ic); 2742841ab66cSSepherosa Ziehau /* 2743841ab66cSSepherosa Ziehau * Cleanup station state. In particular clear various 2744841ab66cSSepherosa Ziehau * state that might otherwise be reused if the node 2745841ab66cSSepherosa Ziehau * is reused before the reference count goes to zero 2746841ab66cSSepherosa Ziehau * (and memory is reclaimed). 2747841ab66cSSepherosa Ziehau */ 274832176cfdSRui Paulo ieee80211_sta_leave(ni); 2749841ab66cSSepherosa Ziehau done: 2750841ab66cSSepherosa Ziehau /* 2751841ab66cSSepherosa Ziehau * Remove the node from any table it's recorded in and 2752841ab66cSSepherosa Ziehau * drop the caller's reference. Removal from the table 2753841ab66cSSepherosa Ziehau * is important to insure the node is not reprocessed 2754841ab66cSSepherosa Ziehau * for inactivity. 2755841ab66cSSepherosa Ziehau */ 275632176cfdSRui Paulo if (nt != NULL) { 2757085ff963SMatthew Dillon IEEE80211_NODE_LOCK(nt); 2758841ab66cSSepherosa Ziehau node_reclaim(nt, ni); 2759085ff963SMatthew Dillon IEEE80211_NODE_UNLOCK(nt); 276032176cfdSRui Paulo } else 2761841ab66cSSepherosa Ziehau ieee80211_free_node(ni); 2762841ab66cSSepherosa Ziehau } 2763841ab66cSSepherosa Ziehau 276432176cfdSRui Paulo struct rssiinfo { 276532176cfdSRui Paulo struct ieee80211vap *vap; 276632176cfdSRui Paulo int rssi_samples; 276732176cfdSRui Paulo uint32_t rssi_total; 276832176cfdSRui Paulo }; 276932176cfdSRui Paulo 277032176cfdSRui Paulo static void 277132176cfdSRui Paulo get_hostap_rssi(void *arg, struct ieee80211_node *ni) 277232176cfdSRui Paulo { 277332176cfdSRui Paulo struct rssiinfo *info = arg; 277432176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 277532176cfdSRui Paulo int8_t rssi; 277632176cfdSRui Paulo 277732176cfdSRui Paulo if (info->vap != vap) 277832176cfdSRui Paulo return; 277932176cfdSRui Paulo /* only associated stations */ 278032176cfdSRui Paulo if (ni->ni_associd == 0) 278132176cfdSRui Paulo return; 278232176cfdSRui Paulo rssi = vap->iv_ic->ic_node_getrssi(ni); 278332176cfdSRui Paulo if (rssi != 0) { 278432176cfdSRui Paulo info->rssi_samples++; 278532176cfdSRui Paulo info->rssi_total += rssi; 278632176cfdSRui Paulo } 278732176cfdSRui Paulo } 278832176cfdSRui Paulo 278932176cfdSRui Paulo static void 279032176cfdSRui Paulo get_adhoc_rssi(void *arg, struct ieee80211_node *ni) 279132176cfdSRui Paulo { 279232176cfdSRui Paulo struct rssiinfo *info = arg; 279332176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 279432176cfdSRui Paulo int8_t rssi; 279532176cfdSRui Paulo 279632176cfdSRui Paulo if (info->vap != vap) 279732176cfdSRui Paulo return; 279832176cfdSRui Paulo /* only neighbors */ 279932176cfdSRui Paulo /* XXX check bssid */ 280032176cfdSRui Paulo if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 280132176cfdSRui Paulo return; 280232176cfdSRui Paulo rssi = vap->iv_ic->ic_node_getrssi(ni); 280332176cfdSRui Paulo if (rssi != 0) { 280432176cfdSRui Paulo info->rssi_samples++; 280532176cfdSRui Paulo info->rssi_total += rssi; 280632176cfdSRui Paulo } 280732176cfdSRui Paulo } 280832176cfdSRui Paulo 280932176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH 281032176cfdSRui Paulo static void 281132176cfdSRui Paulo get_mesh_rssi(void *arg, struct ieee80211_node *ni) 281232176cfdSRui Paulo { 281332176cfdSRui Paulo struct rssiinfo *info = arg; 281432176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap; 281532176cfdSRui Paulo int8_t rssi; 281632176cfdSRui Paulo 281732176cfdSRui Paulo if (info->vap != vap) 281832176cfdSRui Paulo return; 281932176cfdSRui Paulo /* only neighbors that peered successfully */ 282032176cfdSRui Paulo if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) 282132176cfdSRui Paulo return; 282232176cfdSRui Paulo rssi = vap->iv_ic->ic_node_getrssi(ni); 282332176cfdSRui Paulo if (rssi != 0) { 282432176cfdSRui Paulo info->rssi_samples++; 282532176cfdSRui Paulo info->rssi_total += rssi; 282632176cfdSRui Paulo } 282732176cfdSRui Paulo } 282832176cfdSRui Paulo #endif /* IEEE80211_SUPPORT_MESH */ 282932176cfdSRui Paulo 283032176cfdSRui Paulo int8_t 283132176cfdSRui Paulo ieee80211_getrssi(struct ieee80211vap *vap) 2832841ab66cSSepherosa Ziehau { 2833841ab66cSSepherosa Ziehau #define NZ(x) ((x) == 0 ? 1 : (x)) 283432176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic; 283532176cfdSRui Paulo struct rssiinfo info; 2836841ab66cSSepherosa Ziehau 283732176cfdSRui Paulo info.rssi_total = 0; 283832176cfdSRui Paulo info.rssi_samples = 0; 283932176cfdSRui Paulo info.vap = vap; 284032176cfdSRui Paulo switch (vap->iv_opmode) { 2841841ab66cSSepherosa Ziehau case IEEE80211_M_IBSS: /* average of all ibss neighbors */ 2842841ab66cSSepherosa Ziehau case IEEE80211_M_AHDEMO: /* average of all neighbors */ 284332176cfdSRui Paulo ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info); 2844841ab66cSSepherosa Ziehau break; 2845841ab66cSSepherosa Ziehau case IEEE80211_M_HOSTAP: /* average of all associated stations */ 284632176cfdSRui Paulo ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info); 2847841ab66cSSepherosa Ziehau break; 284832176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH 284932176cfdSRui Paulo case IEEE80211_M_MBSS: /* average of all mesh neighbors */ 285032176cfdSRui Paulo ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info); 285132176cfdSRui Paulo break; 285232176cfdSRui Paulo #endif 2853841ab66cSSepherosa Ziehau case IEEE80211_M_MONITOR: /* XXX */ 2854841ab66cSSepherosa Ziehau case IEEE80211_M_STA: /* use stats from associated ap */ 2855841ab66cSSepherosa Ziehau default: 285632176cfdSRui Paulo if (vap->iv_bss != NULL) 285732176cfdSRui Paulo info.rssi_total = ic->ic_node_getrssi(vap->iv_bss); 285832176cfdSRui Paulo info.rssi_samples = 1; 2859841ab66cSSepherosa Ziehau break; 2860841ab66cSSepherosa Ziehau } 286132176cfdSRui Paulo return info.rssi_total / NZ(info.rssi_samples); 2862841ab66cSSepherosa Ziehau #undef NZ 2863841ab66cSSepherosa Ziehau } 2864841ab66cSSepherosa Ziehau 2865841ab66cSSepherosa Ziehau void 286632176cfdSRui Paulo ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise) 2867841ab66cSSepherosa Ziehau { 2868841ab66cSSepherosa Ziehau 286932176cfdSRui Paulo if (vap->iv_bss == NULL) /* NB: shouldn't happen */ 287032176cfdSRui Paulo return; 287132176cfdSRui Paulo vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise); 287232176cfdSRui Paulo /* for non-station mode return avg'd rssi accounting */ 287332176cfdSRui Paulo if (vap->iv_opmode != IEEE80211_M_STA) 287432176cfdSRui Paulo *rssi = ieee80211_getrssi(vap); 2875f39a365aSSepherosa Ziehau } 2876