#include <sys/cdefs.h>
#include "opt_wlan.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_media.h>
#include <net/ethernet.h>
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_input.h>
#ifdef IEEE80211_SUPPORT_SUPERG
#include <net80211/ieee80211_superg.h>
#endif
#ifdef IEEE80211_SUPPORT_TDMA
#include <net80211/ieee80211_tdma.h>
#endif
#include <net80211/ieee80211_wds.h>
#include <net80211/ieee80211_mesh.h>
#include <net80211/ieee80211_ratectl.h>
#include <net80211/ieee80211_vht.h>
#include <net/bpf.h>
#ifdef IEEE80211_DEBUG_REFCNT
#define __debrefcnt_used
#else
#define __debrefcnt_used __unused
#endif
CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
#define IEEE80211_AID_SET(_vap, b) \
((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
(1 << (IEEE80211_AID(b) % 32)))
#define IEEE80211_AID_CLR(_vap, b) \
((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
~(1 << (IEEE80211_AID(b) % 32)))
#define IEEE80211_AID_ISSET(_vap, b) \
((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
static int ieee80211_sta_join1(struct ieee80211_node *);
static struct ieee80211_node *ieee80211_alloc_node(
struct ieee80211_node_table *, struct ieee80211vap *,
const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *, int);
static struct ieee80211_node *node_alloc(struct ieee80211vap *,
const uint8_t [IEEE80211_ADDR_LEN]);
static int node_init(struct ieee80211_node *);
static void node_cleanup(struct ieee80211_node *);
static void node_free(struct ieee80211_node *);
static void node_age(struct ieee80211_node *);
static int8_t node_getrssi(const struct ieee80211_node *);
static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
static void node_getmimoinfo(const struct ieee80211_node *,
struct ieee80211_mimo_info *);
static void __ieee80211_free_node(struct ieee80211_node *);
static void node_reclaim(struct ieee80211_node_table *nt,
struct ieee80211_node *ni);
static void ieee80211_node_table_init(struct ieee80211com *ic,
struct ieee80211_node_table *nt, const char *name,
int inact, int keymaxix);
static void ieee80211_node_table_reset(struct ieee80211_node_table *,
struct ieee80211vap *);
static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
static void ieee80211_vap_erp_timeout(struct ieee80211vap *);
MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
void
ieee80211_node_attach(struct ieee80211com *ic)
{
ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
"802.11 staging q");
ieee80211_node_table_init(ic, &ic->ic_sta, "station",
IEEE80211_INACT_INIT, ic->ic_max_keyix);
callout_init(&ic->ic_inact, 1);
callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
ieee80211_node_timeout, ic);
ic->ic_node_alloc = node_alloc;
ic->ic_node_init = node_init;
ic->ic_node_free = node_free;
ic->ic_node_cleanup = node_cleanup;
ic->ic_node_age = node_age;
ic->ic_node_drain = node_age;
ic->ic_node_getrssi = node_getrssi;
ic->ic_node_getsignal = node_getsignal;
ic->ic_node_getmimoinfo = node_getmimoinfo;
ic->ic_flags_ext |= IEEE80211_FEXT_INACT;
}
void
ieee80211_node_detach(struct ieee80211com *ic)
{
callout_drain(&ic->ic_inact);
ieee80211_node_table_cleanup(&ic->ic_sta);
ieee80211_ageq_drain(&ic->ic_stageq);
ieee80211_ageq_cleanup(&ic->ic_stageq);
}
void
ieee80211_node_vattach(struct ieee80211vap *vap)
{
vap->iv_max_aid = IEEE80211_AID_DEF;
vap->iv_inact_init = IEEE80211_INACT_INIT;
vap->iv_inact_auth = IEEE80211_INACT_AUTH;
vap->iv_inact_run = IEEE80211_INACT_RUN;
vap->iv_inact_probe = IEEE80211_INACT_PROBE;
IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
"%s: init %u auth %u run %u probe %u\n", __func__,
vap->iv_inact_init, vap->iv_inact_auth,
vap->iv_inact_run, vap->iv_inact_probe);
}
void
ieee80211_node_latevattach(struct ieee80211vap *vap)
{
IEEE80211_UNLOCK_ASSERT(vap->iv_ic);
if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
if (vap->iv_max_aid < IEEE80211_AID_MIN) {
vap->iv_max_aid = IEEE80211_AID_MIN;
net80211_vap_printf(vap,
"WARNING: max aid too small, changed to %d\n",
vap->iv_max_aid);
}
vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
M_80211_NODE,
IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
if (vap->iv_aid_bitmap == NULL) {
net80211_vap_printf(vap,
"%s: no memory for AID bitmap, max aid %d!\n",
__func__, vap->iv_max_aid);
vap->iv_max_aid = 0;
}
}
IEEE80211_LOCK(vap->iv_ic);
ieee80211_reset_bss(vap);
IEEE80211_UNLOCK(vap->iv_ic);
vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
}
void
ieee80211_node_vdetach(struct ieee80211vap *vap)
{
struct ieee80211com *ic = vap->iv_ic;
IEEE80211_UNLOCK_ASSERT(vap->iv_ic);
ieee80211_node_table_reset(&ic->ic_sta, vap);
IEEE80211_LOCK(ic);
if (vap->iv_bss != NULL) {
ieee80211_free_node(vap->iv_bss);
vap->iv_update_bss(vap, NULL);
}
IEEE80211_UNLOCK(ic);
if (vap->iv_aid_bitmap != NULL) {
IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
vap->iv_aid_bitmap = NULL;
}
}
void
ieee80211_node_authorize(struct ieee80211_node *ni)
{
struct ieee80211vap *vap = ni->ni_vap;
ni->ni_flags |= IEEE80211_NODE_AUTH;
ni->ni_inact_reload = vap->iv_inact_run;
ni->ni_inact = ni->ni_inact_reload;
IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
"%s: inact_reload %u", __func__, ni->ni_inact_reload);
}
void
ieee80211_node_unauthorize(struct ieee80211_node *ni)
{
struct ieee80211vap *vap = ni->ni_vap;
ni->ni_flags &= ~IEEE80211_NODE_AUTH;
ni->ni_inact_reload = vap->iv_inact_auth;
if (ni->ni_inact > ni->ni_inact_reload)
ni->ni_inact = ni->ni_inact_reload;
IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
"%s: inact_reload %u inact %u", __func__,
ni->ni_inact_reload, ni->ni_inact);
}
void
ieee80211_node_setuptxparms(struct ieee80211_node *ni)
{
struct ieee80211vap *vap = ni->ni_vap;
enum ieee80211_phymode mode;
if (ni->ni_flags & IEEE80211_NODE_VHT) {
if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
mode = IEEE80211_MODE_VHT_5GHZ;
else
mode = IEEE80211_MODE_VHT_2GHZ;
} else if (ni->ni_flags & IEEE80211_NODE_HT) {
if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
mode = IEEE80211_MODE_11NA;
else
mode = IEEE80211_MODE_11NG;
} else {
if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
mode = IEEE80211_MODE_STURBO_A;
else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
mode = IEEE80211_MODE_HALF;
else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
mode = IEEE80211_MODE_QUARTER;
else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
mode = IEEE80211_MODE_11A;
else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
(ni->ni_flags & IEEE80211_NODE_ERP))
mode = IEEE80211_MODE_11G;
else
mode = IEEE80211_MODE_11B;
}
ni->ni_txparms = &vap->iv_txparms[mode];
}
void
ieee80211_node_set_chan(struct ieee80211_node *ni,
struct ieee80211_channel *chan)
{
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211vap *vap = ni->ni_vap;
enum ieee80211_phymode mode;
KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
ni->ni_chan = chan;
mode = ieee80211_chan2mode(chan);
if (IEEE80211_IS_CHAN_HT(chan)) {
ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
if (mode == IEEE80211_MODE_11NA &&
(vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
mode = IEEE80211_MODE_11A;
else if (mode == IEEE80211_MODE_11NG &&
(vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
mode = IEEE80211_MODE_11G;
if (mode == IEEE80211_MODE_11G &&
(vap->iv_flags & IEEE80211_F_PUREG) == 0)
mode = IEEE80211_MODE_11B;
}
ni->ni_txparms = &vap->iv_txparms[mode];
ni->ni_rates = *ieee80211_get_suprates(ic, chan);
}
static __inline void
copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
{
nbss->ni_authmode = obss->ni_authmode;
nbss->ni_txpower = obss->ni_txpower;
nbss->ni_vlan = obss->ni_vlan;
}
void
ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
{
struct ieee80211com *ic = vap->iv_ic;
struct ieee80211_node *ni;
IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
"%s: creating %s on channel %u%c flags 0x%08x\n", __func__,
ieee80211_opmode_name[vap->iv_opmode],
ieee80211_chan2ieee(ic, chan),
ieee80211_channel_type_char(chan),
chan->ic_flags);
ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr,
__func__, __LINE__);
if (ni == NULL) {
return;
}
IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
ni->ni_esslen = vap->iv_des_ssid[0].len;
memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
if (vap->iv_bss != NULL)
copy_bss(ni, vap->iv_bss);
ni->ni_intval = ic->ic_bintval;
if (vap->iv_flags & IEEE80211_F_PRIVACY)
ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
if (ic->ic_phytype == IEEE80211_T_FH) {
ni->ni_fhdwell = 200;
ni->ni_fhindex = 1;
}
if (vap->iv_opmode == IEEE80211_M_IBSS) {
ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;
if (vap->iv_flags & IEEE80211_F_DESBSSID)
IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
else {
net80211_get_random_bytes(ni->ni_bssid,
IEEE80211_ADDR_LEN);
ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
}
} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
if (vap->iv_flags & IEEE80211_F_DESBSSID)
IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
else
#ifdef IEEE80211_SUPPORT_TDMA
if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
#endif
memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
#ifdef IEEE80211_SUPPORT_MESH
} else if (vap->iv_opmode == IEEE80211_M_MBSS) {
ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
#endif
}
if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
ic->ic_bsschan->ic_freq != chan->ic_freq &&
IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
ic->ic_bsschan = chan;
ieee80211_node_set_chan(ni, chan);
ic->ic_curmode = ieee80211_chan2mode(chan);
if (IEEE80211_IS_CHAN_FULL(chan)) {
if (IEEE80211_IS_CHAN_ANYG(chan)) {
ieee80211_setbasicrates(&ni->ni_rates,
IEEE80211_MODE_11G);
if (vap->iv_flags & IEEE80211_F_PUREG) {
ieee80211_addbasicrates(&ni->ni_rates,
IEEE80211_MODE_11A);
}
} else if (IEEE80211_IS_CHAN_B(chan)) {
ieee80211_setbasicrates(&ni->ni_rates,
IEEE80211_MODE_11B);
}
}
if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
ieee80211_ht_node_init(ni);
ieee80211_vht_node_init(ni);
} else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
ieee80211_ht_node_init(ni);
}
(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
}
void
ieee80211_reset_bss(struct ieee80211vap *vap)
{
struct ieee80211com *ic = vap->iv_ic;
struct ieee80211_node *ni, *obss;
IEEE80211_LOCK_ASSERT(ic);
ieee80211_node_table_reset(&ic->ic_sta, vap);
ieee80211_vap_reset_erp(vap);
ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr,
__func__, __LINE__);
KASSERT(ni != NULL, ("unable to setup initial BSS node"));
obss = vap->iv_update_bss(vap, ieee80211_ref_node(ni));
if (obss != NULL) {
copy_bss(ni, obss);
ni->ni_intval = ic->ic_bintval;
ieee80211_free_node(obss);
} else
IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
}
static int
match_ssid(const struct ieee80211_node *ni,
int nssid, const struct ieee80211_scan_ssid ssids[])
{
int i;
for (i = 0; i < nssid; i++) {
if (ni->ni_esslen == ssids[i].len &&
memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
return 1;
}
return 0;
}
static int
check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
uint8_t rate;
if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
return 0;
if (vap->iv_opmode == IEEE80211_M_IBSS) {
if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
return 0;
} else {
if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
return 0;
}
if (vap->iv_flags & IEEE80211_F_PRIVACY) {
if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
return 0;
} else {
if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
return 0;
}
rate = ieee80211_fix_rate(ni, &ni->ni_rates,
IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
if (rate & IEEE80211_RATE_BASIC)
return 0;
if (vap->iv_des_nssid != 0 &&
!match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
return 0;
if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
!IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
return 0;
return 1;
}
#ifdef IEEE80211_DEBUG
static void
check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
uint8_t rate;
int fail;
fail = 0;
if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
fail |= 0x01;
if (vap->iv_opmode == IEEE80211_M_IBSS) {
if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
fail |= 0x02;
} else {
if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
fail |= 0x02;
}
if (vap->iv_flags & IEEE80211_F_PRIVACY) {
if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
fail |= 0x04;
} else {
if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
fail |= 0x04;
}
rate = ieee80211_fix_rate(ni, &ni->ni_rates,
IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
if (rate & IEEE80211_RATE_BASIC)
fail |= 0x08;
if (vap->iv_des_nssid != 0 &&
!match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
fail |= 0x10;
if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
!IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
fail |= 0x20;
net80211_printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
net80211_printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
net80211_printf(" %3d%c",
ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
net80211_printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
fail & 0x08 ? '!' : ' ');
net80211_printf(" %4s%c",
(ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
(ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
"????",
fail & 0x02 ? '!' : ' ');
net80211_printf(" %3s%c ",
(ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" : "no",
fail & 0x04 ? '!' : ' ');
ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
net80211_printf("%s\n", fail & 0x10 ? "!" : "");
}
#endif
int
ieee80211_ibss_merge_check(struct ieee80211_node *ni)
{
struct ieee80211vap *vap = ni->ni_vap;
if (ni == vap->iv_bss ||
IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
return 0;
}
if (!check_bss(vap, ni)) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
"%s: merge failed, capabilities mismatch\n", __func__);
#ifdef IEEE80211_DEBUG
if (ieee80211_msg_assoc(vap))
check_bss_debug(vap, ni);
#endif
vap->iv_stats.is_ibss_capmismatch++;
return 0;
}
return 1;
}
int
ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
const struct ieee80211_scanparams *scan)
{
struct ieee80211vap *vap = ni->ni_vap;
int i;
if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
goto ok;
if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
goto mismatch;
if (scan->ssid == NULL)
goto mismatch;
for (i = 0; i < vap->iv_des_nssid; i++) {
if (vap->iv_des_ssid[i].len != scan->ssid[1])
continue;
if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
vap->iv_des_ssid[i].len) == 0)
goto ok;
}
mismatch:
return (0);
ok:
return (1);
}
int
ieee80211_ibss_merge(struct ieee80211_node *ni)
{
#ifdef IEEE80211_DEBUG
struct ieee80211vap *vap = ni->ni_vap;
#endif
if (! ieee80211_ibss_merge_check(ni))
return 0;
IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
ether_sprintf(ni->ni_bssid),
vap->iv_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
vap->iv_flags&IEEE80211_F_SHSLOT ? "short" : "long",
vap->iv_flags&IEEE80211_F_USEPROT ? ", protection" : ""
);
return ieee80211_sta_join1(ieee80211_ref_node(ni));
}
static int
gethtadjustflags(struct ieee80211com *ic)
{
struct ieee80211vap *vap;
int flags;
flags = 0;
TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
if (vap->iv_state < IEEE80211_S_RUN)
continue;
switch (vap->iv_opmode) {
case IEEE80211_M_WDS:
case IEEE80211_M_STA:
case IEEE80211_M_AHDEMO:
case IEEE80211_M_HOSTAP:
case IEEE80211_M_IBSS:
case IEEE80211_M_MBSS:
flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
break;
default:
break;
}
}
return flags;
}
static int
getvhtadjustflags(struct ieee80211com *ic)
{
struct ieee80211vap *vap;
int flags;
flags = 0;
TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
if (vap->iv_state < IEEE80211_S_RUN)
continue;
switch (vap->iv_opmode) {
case IEEE80211_M_WDS:
case IEEE80211_M_STA:
case IEEE80211_M_AHDEMO:
case IEEE80211_M_HOSTAP:
case IEEE80211_M_IBSS:
case IEEE80211_M_MBSS:
flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan);
break;
default:
break;
}
}
return flags;
}
void
ieee80211_sync_curchan(struct ieee80211com *ic)
{
struct ieee80211_channel *c;
c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
if (c != ic->ic_curchan) {
ic->ic_curchan = c;
ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
IEEE80211_UNLOCK(ic);
ic->ic_set_channel(ic);
ieee80211_radiotap_chan_change(ic);
IEEE80211_LOCK(ic);
}
}
void
ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
{
if (ic->ic_htcaps & IEEE80211_HTC_HT) {
int flags = gethtadjustflags(ic);
if (flags > ieee80211_htchanflags(c))
c = ieee80211_ht_adjust_channel(ic, c, flags);
}
if (ic->ic_vht_cap.vht_cap_info != 0) {
int flags = getvhtadjustflags(ic);
if (flags > ieee80211_vhtchanflags(c))
c = ieee80211_vht_adjust_channel(ic, c, flags);
}
ic->ic_bsschan = ic->ic_curchan = c;
ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
}
void
ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
{
ieee80211_setupcurchan(ic, c);
ieee80211_runtask(ic, &ic->ic_chan_task);
}
void
ieee80211_update_chw(struct ieee80211com *ic)
{
ieee80211_setupcurchan(ic, ic->ic_curchan);
ieee80211_runtask(ic, &ic->ic_chw_task);
}
static int
ieee80211_sta_join1(struct ieee80211_node *selbs)
{
struct ieee80211vap *vap = selbs->ni_vap;
struct ieee80211com *ic = selbs->ni_ic;
struct ieee80211_node *obss;
int canreassoc;
IEEE80211_LOCK(ic);
obss = vap->iv_update_bss(vap, selbs);
IEEE80211_UNLOCK(ic);
canreassoc = (obss != NULL &&
vap->iv_state == IEEE80211_S_RUN &&
IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
if (obss != NULL) {
struct ieee80211_node_table *nt = obss->ni_table;
copy_bss(selbs, obss);
if (nt != NULL) {
ieee80211_node_decref(obss);
IEEE80211_NODE_LOCK(nt);
node_reclaim(nt, obss);
IEEE80211_NODE_UNLOCK(nt);
} else {
ieee80211_free_node(obss);
}
obss = NULL;
}
ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
IEEE80211_F_DODEL | IEEE80211_F_JOIN);
ieee80211_setcurchan(ic, selbs->ni_chan);
ieee80211_vap_reset_erp(vap);
ieee80211_wme_initparams(vap);
if (vap->iv_opmode == IEEE80211_M_STA) {
if (canreassoc) {
ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
} else {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_AUTH,
"%s %p<%s> %s -> AUTH, FC0_SUBTYPE_DEAUTH\n",
__func__, selbs, ether_sprintf(selbs->ni_macaddr),
ieee80211_state_name[vap->iv_state]);
ieee80211_new_state(vap, IEEE80211_S_AUTH,
IEEE80211_FC0_SUBTYPE_DEAUTH);
}
} else
ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
return 1;
}
int
ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
const struct ieee80211_scan_entry *se)
{
struct ieee80211com *ic = vap->iv_ic;
struct ieee80211_node *ni;
bool do_ht;
ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr,
__func__, __LINE__);
if (ni == NULL) {
return 0;
}
IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
ni->ni_esslen = se->se_ssid[1];
memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
ni->ni_tstamp.tsf = se->se_tstamp.tsf;
ni->ni_intval = se->se_intval;
ni->ni_capinfo = se->se_capinfo;
ni->ni_chan = chan;
ni->ni_timoff = se->se_timoff;
ni->ni_fhdwell = se->se_fhdwell;
ni->ni_fhindex = se->se_fhindex;
ni->ni_erp = se->se_erp;
IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
ni->ni_noise = se->se_noise;
if (vap->iv_opmode == IEEE80211_M_STA) {
ni->ni_flags |= IEEE80211_NODE_ASSOCID;
}
if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
ieee80211_ies_expand(&ni->ni_ies);
#ifdef IEEE80211_SUPPORT_SUPERG
if (ni->ni_ies.ath_ie != NULL)
ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
#endif
if (ni->ni_ies.htcap_ie != NULL)
ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
if (ni->ni_ies.htinfo_ie != NULL)
ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
#ifdef IEEE80211_SUPPORT_MESH
if (ni->ni_ies.meshid_ie != NULL)
ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
#endif
#ifdef IEEE80211_SUPPORT_TDMA
if (ni->ni_ies.tdma_ie != NULL)
ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
#endif
if (ni->ni_ies.vhtcap_ie != NULL)
ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
if (ni->ni_ies.vhtopmode_ie != NULL)
ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
}
vap->iv_dtim_period = se->se_dtimperiod;
vap->iv_dtim_count = 0;
ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
IEEE80211_F_DOSORT);
if (ieee80211_iserp_rateset(&ni->ni_rates))
ni->ni_flags |= IEEE80211_NODE_ERP;
do_ht = false;
if (ni->ni_ies.htinfo_ie != NULL &&
ni->ni_ies.htcap_ie != NULL &&
vap->iv_flags_ht & IEEE80211_FHT_HT) {
ieee80211_ht_node_init(ni);
ieee80211_ht_updateparams(ni,
ni->ni_ies.htcap_ie,
ni->ni_ies.htinfo_ie);
do_ht = true;
}
if (do_ht && ni->ni_ies.vhtopmode_ie != NULL &&
ni->ni_ies.vhtcap_ie != NULL &&
vap->iv_vht_flags & IEEE80211_FVHT_VHT) {
if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
net80211_vap_printf(ni->ni_vap,
"%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
__func__, ni->ni_macaddr, ":");
} else {
ieee80211_vht_node_init(ni);
ieee80211_vht_updateparams(ni,
ni->ni_ies.vhtcap_ie,
ni->ni_ies.vhtopmode_ie);
ieee80211_setup_vht_rates(ni);
}
}
if (do_ht) {
ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
ni->ni_ies.htinfo_ie);
ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
}
ieee80211_node_setuptxparms(ni);
ieee80211_ratectl_node_init(ni);
return ieee80211_sta_join1(ieee80211_ref_node(ni));
}
void
ieee80211_sta_leave(struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
ic->ic_node_cleanup(ni);
ieee80211_notify_node_leave(ni);
}
void
ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
{
ieee80211_ref_node(ni);
if (ni->ni_associd != 0)
IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
ieee80211_node_leave(ni);
ieee80211_free_node(ni);
}
static struct ieee80211_node *
node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
{
struct ieee80211_node *ni;
ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
return ni;
}
static int
node_init(struct ieee80211_node *ni)
{
return 0;
}
int
ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
{
memset(ies, 0, offsetof(struct ieee80211_ies, data));
if (ies->data != NULL && ies->len != len) {
IEEE80211_FREE(ies->data, M_80211_NODE_IE);
ies->data = NULL;
}
if (ies->data == NULL) {
ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
if (ies->data == NULL) {
ies->len = 0;
return 0;
}
}
memcpy(ies->data, data, len);
ies->len = len;
return 1;
}
void
ieee80211_ies_cleanup(struct ieee80211_ies *ies)
{
if (ies->data != NULL)
IEEE80211_FREE(ies->data, M_80211_NODE_IE);
}
void
ieee80211_ies_expand(struct ieee80211_ies *ies)
{
uint8_t *ie;
int ielen;
ie = ies->data;
ielen = ies->len;
while (ielen > 1) {
if ((2 + ie[1]) > ielen) {
net80211_printf("%s: malformed IEs! ies %p { data %p len %d }: "
"ie %u len 2+%u > total len left %d\n",
__func__, ies, ies->data, ies->len,
ie[0], ie[1], ielen);
return;
}
switch (ie[0]) {
case IEEE80211_ELEMID_VENDOR:
if (iswpaoui(ie))
ies->wpa_ie = ie;
else if (iswmeoui(ie))
ies->wme_ie = ie;
#ifdef IEEE80211_SUPPORT_SUPERG
else if (isatherosoui(ie))
ies->ath_ie = ie;
#endif
#ifdef IEEE80211_SUPPORT_TDMA
else if (istdmaoui(ie))
ies->tdma_ie = ie;
#endif
break;
case IEEE80211_ELEMID_RSN:
ies->rsn_ie = ie;
break;
case IEEE80211_ELEMID_HTCAP:
ies->htcap_ie = ie;
break;
case IEEE80211_ELEMID_HTINFO:
ies->htinfo_ie = ie;
break;
#ifdef IEEE80211_SUPPORT_MESH
case IEEE80211_ELEMID_MESHID:
ies->meshid_ie = ie;
break;
#endif
case IEEE80211_ELEMID_VHT_CAP:
ies->vhtcap_ie = ie;
break;
case IEEE80211_ELEMID_VHT_OPMODE:
ies->vhtopmode_ie = ie;
break;
case IEEE80211_ELEMID_VHT_PWR_ENV:
ies->vhtpwrenv_ie = ie;
break;
case IEEE80211_ELEMID_BSSLOAD:
ies->bssload_ie = ie;
break;
case IEEE80211_ELEMID_APCHANREP:
ies->apchanrep_ie = ie;
break;
}
ielen -= 2 + ie[1];
ie += 2 + ie[1];
}
}
static void
node_cleanup(struct ieee80211_node *ni)
{
struct ieee80211vap *vap = ni->ni_vap;
struct ieee80211com *ic = ni->ni_ic;
int i;
if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
if (vap->iv_opmode != IEEE80211_M_STA)
vap->iv_ps_sta--;
ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
}
if (ni->ni_flags & IEEE80211_NODE_VHT)
ieee80211_vht_node_cleanup(ni);
if (ni->ni_flags & IEEE80211_NODE_HT)
ieee80211_ht_node_cleanup(ni);
#ifdef IEEE80211_SUPPORT_SUPERG
ieee80211_ff_node_cleanup(ni);
#endif
#ifdef IEEE80211_SUPPORT_MESH
if (vap->iv_opmode == IEEE80211_M_MBSS)
ieee80211_mesh_node_cleanup(ni);
#endif
ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
vap->iv_set_tim(ni, 0);
ni->ni_associd = 0;
if (ni->ni_challenge != NULL) {
IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
ni->ni_challenge = NULL;
}
for (i = 0; i < nitems(ni->ni_rxfrag); i++)
if (ni->ni_rxfrag[i] != NULL) {
m_freem(ni->ni_rxfrag[i]);
ni->ni_rxfrag[i] = NULL;
}
ieee80211_node_delucastkey(ni);
}
static void
node_free(struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
ieee80211_ratectl_node_deinit(ni);
ic->ic_node_cleanup(ni);
ieee80211_ies_cleanup(&ni->ni_ies);
ieee80211_psq_cleanup(&ni->ni_psq);
IEEE80211_FREE(ni, M_80211_NODE);
}
static void
node_age(struct ieee80211_node *ni)
{
struct ieee80211vap *vap = ni->ni_vap;
if (ieee80211_node_psq_age(ni) != 0 &&
ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
vap->iv_set_tim(ni, 0);
if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
ieee80211_ht_node_age(ni);
}
static int8_t
node_getrssi(const struct ieee80211_node *ni)
{
uint32_t avgrssi = ni->ni_avgrssi;
int32_t rssi;
if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
return 0;
rssi = IEEE80211_RSSI_GET(avgrssi);
return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
}
static void
node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
{
*rssi = node_getrssi(ni);
*noise = ni->ni_noise;
}
static void
node_getmimoinfo(const struct ieee80211_node *ni,
struct ieee80211_mimo_info *info)
{
int i;
uint32_t avgrssi;
int32_t rssi;
bzero(info, sizeof(*info));
for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
avgrssi = ni->ni_mimo_rssi_ctl[i];
if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
info->ch[i].rssi[0] = 0;
} else {
rssi = IEEE80211_RSSI_GET(avgrssi);
info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
}
info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
}
}
static void
ieee80211_add_node_nt(struct ieee80211_node_table *nt,
struct ieee80211_node *ni)
{
struct ieee80211com *ic = nt->nt_ic;
int hash;
IEEE80211_NODE_LOCK_ASSERT(nt);
hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
(void) ic;
TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
nt->nt_count++;
ni->ni_table = nt;
}
static void
ieee80211_del_node_nt(struct ieee80211_node_table *nt,
struct ieee80211_node *ni)
{
IEEE80211_NODE_LOCK_ASSERT(nt);
TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
LIST_REMOVE(ni, ni_hash);
nt->nt_count--;
KASSERT(nt->nt_count >= 0,
("nt_count is negative (%d)!\n", nt->nt_count));
ni->ni_table = NULL;
}
static struct ieee80211_node *
ieee80211_alloc_node(struct ieee80211_node_table *nt,
struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN],
const char *func __debrefcnt_used, int line __debrefcnt_used)
{
struct ieee80211com *ic = nt->nt_ic;
struct ieee80211_node *ni;
ni = ic->ic_node_alloc(vap, macaddr);
if (ni == NULL) {
vap->iv_stats.is_rx_nodealloc++;
return NULL;
}
IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
"%s %p<%s> in %s table\n", __func__, ni,
ether_sprintf(macaddr), nt->nt_name);
IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
ieee80211_node_initref(ni);
#ifdef IEEE80211_DEBUG_REFCNT
IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
#endif
ni->ni_chan = IEEE80211_CHAN_ANYC;
ni->ni_authmode = IEEE80211_AUTH_OPEN;
ni->ni_txpower = ic->ic_txpowlimit;
ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
ni->ni_inact_reload = nt->nt_inact_init;
ni->ni_inact = ni->ni_inact_reload;
ni->ni_ath_defkeyix = 0x7fff;
ieee80211_psq_init(&ni->ni_psq, "unknown");
#ifdef IEEE80211_SUPPORT_MESH
if (vap->iv_opmode == IEEE80211_M_MBSS)
ieee80211_mesh_node_init(vap, ni);
#endif
IEEE80211_NODE_LOCK(nt);
ieee80211_add_node_nt(nt, ni);
ni->ni_vap = vap;
ni->ni_ic = ic;
IEEE80211_NODE_UNLOCK(nt);
if (ic->ic_node_init(ni) != 0) {
vap->iv_stats.is_rx_nodealloc++;
ieee80211_psq_cleanup(&ni->ni_psq);
ieee80211_ratectl_node_deinit(ni);
__ieee80211_free_node(ni);
return NULL;
}
IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
"%s: inact_reload %u", __func__, ni->ni_inact_reload);
return ni;
}
struct ieee80211_node *
ieee80211_tmp_node(struct ieee80211vap *vap,
const uint8_t macaddr[IEEE80211_ADDR_LEN])
{
struct ieee80211com *ic = vap->iv_ic;
struct ieee80211_node *ni;
ni = ic->ic_node_alloc(vap, macaddr);
if (ni != NULL) {
struct ieee80211_node *bss = vap->iv_bss;
IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
ni->ni_table = NULL;
ni->ni_ic = ic;
ni->ni_vap = vap;
IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
ieee80211_node_initref(ni);
#ifdef IEEE80211_DEBUG_REFCNT
IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
"%s (%s:%u) %p<%s> refcnt %d\n", __func__, "", -1, ni,
ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
#endif
ieee80211_node_set_chan(ni, bss->ni_chan);
ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
IEEE80211_KEYIX_NONE);
ni->ni_txpower = bss->ni_txpower;
ieee80211_psq_init(&ni->ni_psq, "unknown");
ieee80211_ratectl_node_init(ni);
if (ic->ic_node_init(ni) != 0) {
vap->iv_stats.is_rx_nodealloc++;
ieee80211_psq_cleanup(&ni->ni_psq);
ieee80211_ratectl_node_deinit(ni);
__ieee80211_free_node(ni);
return NULL;
}
} else {
vap->iv_stats.is_rx_nodealloc++;
}
return ni;
}
struct ieee80211_node *
ieee80211_dup_bss(struct ieee80211vap *vap,
const uint8_t macaddr[IEEE80211_ADDR_LEN])
{
struct ieee80211com *ic = vap->iv_ic;
struct ieee80211_node *ni;
ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr, __func__, __LINE__);
if (ni != NULL) {
struct ieee80211_node *bss = vap->iv_bss;
copy_bss(ni, bss);
IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
ieee80211_node_set_chan(ni, bss->ni_chan);
}
return ni;
}
struct ieee80211_node *
ieee80211_node_create_wds(struct ieee80211vap *vap,
const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
{
struct ieee80211com *ic = vap->iv_ic;
struct ieee80211_node *ni;
ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid, __func__, __LINE__);
if (ni != NULL) {
ni->ni_wdsvap = vap;
IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
copy_bss(ni, vap->iv_bss);
ieee80211_node_set_chan(ni, chan);
ni->ni_esslen = vap->iv_des_ssid[0].len;
memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
if (vap->iv_flags & IEEE80211_F_WME)
ni->ni_flags |= IEEE80211_NODE_QOS;
#ifdef IEEE80211_SUPPORT_SUPERG
if (vap->iv_flags & IEEE80211_F_FF)
ni->ni_flags |= IEEE80211_NODE_FF;
#endif
if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
(vap->iv_flags_ht & IEEE80211_FHT_HT)) {
ieee80211_ht_wds_init(ni);
if (vap->iv_vht_flags & IEEE80211_FVHT_VHT) {
net80211_vap_printf(vap,
"%s: TODO: vht_wds_init\n", __func__);
}
} else {
struct ieee80211_channel *c = ni->ni_chan;
c = ieee80211_find_channel(ic,
c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
KASSERT(c != NULL, ("no legacy channel, %u/%x",
ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
ni->ni_chan = c;
}
}
return ni;
}
struct ieee80211_node *
_ieee80211_find_node_locked(struct ieee80211_node_table *nt,
const uint8_t macaddr[IEEE80211_ADDR_LEN],
const char *func __debrefcnt_used, int line __debrefcnt_used)
{
struct ieee80211_node *ni;
int hash;
IEEE80211_NODE_LOCK_ASSERT(nt);
hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
ieee80211_ref_node(ni);
#ifdef IEEE80211_DEBUG_REFCNT
IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
"%s (%s:%u) %p<%s> refcnt %d\n", __func__,
func, line,
ni, ether_sprintf(ni->ni_macaddr),
ieee80211_node_refcnt(ni));
#endif
return ni;
}
}
return NULL;
}
struct ieee80211_node *
_ieee80211_find_node(struct ieee80211_node_table *nt,
const uint8_t macaddr[IEEE80211_ADDR_LEN],
const char *func __debrefcnt_used, int line __debrefcnt_used)
{
struct ieee80211_node *ni;
IEEE80211_NODE_LOCK(nt);
ni = _ieee80211_find_node_locked(nt, macaddr, func, line);
IEEE80211_NODE_UNLOCK(nt);
return ni;
}
struct ieee80211_node *
_ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
const struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN],
const char *func __debrefcnt_used, int line __debrefcnt_used)
{
struct ieee80211_node *ni;
int hash;
IEEE80211_NODE_LOCK_ASSERT(nt);
hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
if (ni->ni_vap == vap &&
IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
ieee80211_ref_node(ni);
#ifdef IEEE80211_DEBUG_REFCNT
IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
"%s (%s:%u) %p<%s> refcnt %d\n", __func__,
func, line,
ni, ether_sprintf(ni->ni_macaddr),
ieee80211_node_refcnt(ni));
#endif
return ni;
}
}
return NULL;
}
struct ieee80211_node *
_ieee80211_find_vap_node(struct ieee80211_node_table *nt,
const struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN],
const char *func __debrefcnt_used, int line __debrefcnt_used)
{
struct ieee80211_node *ni;
IEEE80211_NODE_LOCK(nt);
ni = _ieee80211_find_vap_node_locked(nt, vap, macaddr, func, line);
IEEE80211_NODE_UNLOCK(nt);
return ni;
}
struct ieee80211_node *
ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
const uint8_t macaddr[IEEE80211_ADDR_LEN])
{
struct ieee80211_node *ni;
IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
"%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
ni = ieee80211_dup_bss(vap, macaddr);
if (ni != NULL) {
struct ieee80211com *ic = vap->iv_ic;
ni->ni_rates = vap->iv_bss->ni_rates;
if (ieee80211_iserp_rateset(&ni->ni_rates))
ni->ni_flags |= IEEE80211_NODE_ERP;
if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
if (vap->iv_flags & IEEE80211_F_WME)
ni->ni_flags |= IEEE80211_NODE_QOS;
#ifdef IEEE80211_SUPPORT_SUPERG
if (vap->iv_flags & IEEE80211_F_FF)
ni->ni_flags |= IEEE80211_NODE_FF;
#endif
}
ieee80211_node_setuptxparms(ni);
ieee80211_ratectl_node_init(ni);
if (ic->ic_newassoc != NULL)
ic->ic_newassoc(ni, 1);
if (vap->iv_opmode == IEEE80211_M_IBSS) {
ieee80211_send_probereq(ni,
vap->iv_myaddr,
ni->ni_macaddr,
vap->iv_bss->ni_bssid,
vap->iv_bss->ni_essid,
vap->iv_bss->ni_esslen);
}
ieee80211_node_authorize(ni);
}
return ni;
}
void
ieee80211_init_neighbor(struct ieee80211_node *ni,
const struct ieee80211_frame *wh,
const struct ieee80211_scanparams *sp)
{
int do_ht_setup = 0, do_vht_setup = 0;
ni->ni_esslen = sp->ssid[1];
memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
ni->ni_intval = sp->bintval;
ni->ni_capinfo = sp->capinfo;
ni->ni_chan = ni->ni_ic->ic_curchan;
ni->ni_fhdwell = sp->fhdwell;
ni->ni_fhindex = sp->fhindex;
ni->ni_erp = sp->erp;
ni->ni_timoff = sp->timoff;
#ifdef IEEE80211_SUPPORT_MESH
if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
ieee80211_mesh_init_neighbor(ni, wh, sp);
#endif
if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
ieee80211_ies_expand(&ni->ni_ies);
if (ni->ni_ies.wme_ie != NULL)
ni->ni_flags |= IEEE80211_NODE_QOS;
else
ni->ni_flags &= ~IEEE80211_NODE_QOS;
#ifdef IEEE80211_SUPPORT_SUPERG
if (ni->ni_ies.ath_ie != NULL)
ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
#endif
if (ni->ni_ies.htcap_ie != NULL)
ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
if (ni->ni_ies.htinfo_ie != NULL)
ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
if (ni->ni_ies.vhtcap_ie != NULL)
ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
if (ni->ni_ies.vhtopmode_ie != NULL)
ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
if ((ni->ni_ies.htcap_ie != NULL) &&
(ni->ni_ies.htinfo_ie != NULL) &&
(ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
do_ht_setup = 1;
}
if ((ni->ni_ies.vhtcap_ie != NULL) &&
(ni->ni_ies.vhtopmode_ie != NULL) &&
(ni->ni_vap->iv_vht_flags & IEEE80211_FVHT_VHT)) {
do_vht_setup = 1;
}
}
ieee80211_setup_rates(ni, sp->rates, sp->xrates,
IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
if (do_ht_setup) {
IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
"%s: doing HT setup\n", __func__);
ieee80211_ht_node_init(ni);
ieee80211_ht_updateparams(ni,
ni->ni_ies.htcap_ie,
ni->ni_ies.htinfo_ie);
if (do_vht_setup) {
if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
net80211_vap_printf(ni->ni_vap,
"%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
__func__, ni->ni_macaddr, ":");
} else {
ieee80211_vht_node_init(ni);
ieee80211_vht_updateparams(ni,
ni->ni_ies.vhtcap_ie,
ni->ni_ies.vhtopmode_ie);
ieee80211_setup_vht_rates(ni);
}
}
ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
ni->ni_ies.htinfo_ie);
ieee80211_setup_htrates(ni,
ni->ni_ies.htcap_ie,
IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
ieee80211_setup_basic_htrates(ni,
ni->ni_ies.htinfo_ie);
ieee80211_node_setuptxparms(ni);
ieee80211_ratectl_node_init(ni);
if (ni->ni_ic->ic_newassoc)
ni->ni_ic->ic_newassoc(ni, 1);
}
}
struct ieee80211_node *
ieee80211_add_neighbor(struct ieee80211vap *vap,
const struct ieee80211_frame *wh,
const struct ieee80211_scanparams *sp)
{
struct ieee80211_node *ni;
IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
"%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
ni = ieee80211_dup_bss(vap, wh->i_addr2);
if (ni != NULL) {
struct ieee80211com *ic = vap->iv_ic;
ieee80211_init_neighbor(ni, wh, sp);
if (ieee80211_iserp_rateset(&ni->ni_rates))
ni->ni_flags |= IEEE80211_NODE_ERP;
ieee80211_node_setuptxparms(ni);
ieee80211_ratectl_node_init(ni);
if (ic->ic_newassoc != NULL)
ic->ic_newassoc(ni, 1);
ieee80211_node_authorize(ni);
}
return ni;
}
#define IS_PROBEREQ(wh) \
((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
== (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
#define IS_BCAST_PROBEREQ(wh) \
(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
((const struct ieee80211_frame *)(wh))->i_addr3))
static __inline struct ieee80211_node *
_find_rxnode(struct ieee80211_node_table *nt,
const struct ieee80211_frame_min *wh,
const char *func __debrefcnt_used, int line __debrefcnt_used)
{
if (IS_BCAST_PROBEREQ(wh))
return NULL;
return _ieee80211_find_node_locked(nt, wh->i_addr2, func, line);
}
struct ieee80211_node *
_ieee80211_find_rxnode(struct ieee80211com *ic,
const struct ieee80211_frame_min *wh,
const char *func __debrefcnt_used, int line __debrefcnt_used)
{
struct ieee80211_node_table *nt;
struct ieee80211_node *ni;
nt = &ic->ic_sta;
IEEE80211_NODE_LOCK(nt);
ni = _find_rxnode(nt, wh, func, line);
IEEE80211_NODE_UNLOCK(nt);
return ni;
}
struct ieee80211_node *
_ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
const char *func __debrefcnt_used, int line __debrefcnt_used)
{
struct ieee80211_node_table *nt;
struct ieee80211_node *ni;
nt = &ic->ic_sta;
IEEE80211_NODE_LOCK(nt);
if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
ni = nt->nt_keyixmap[keyix];
else
ni = NULL;
if (ni == NULL) {
ni = _find_rxnode(nt, wh, func, line);
if (ni != NULL && nt->nt_keyixmap != NULL) {
keyix = ni->ni_ucastkey.wk_rxkeyix;
if (keyix < nt->nt_keyixmax &&
nt->nt_keyixmap[keyix] == NULL) {
IEEE80211_DPRINTF(ni->ni_vap,
IEEE80211_MSG_NODE,
"%s: add key map entry %p<%s> refcnt %d\n",
__func__, ni, ether_sprintf(ni->ni_macaddr),
ieee80211_node_refcnt(ni)+1);
nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
}
}
} else {
if (IS_BCAST_PROBEREQ(wh))
ni = NULL;
else
ieee80211_ref_node(ni);
}
IEEE80211_NODE_UNLOCK(nt);
return ni;
}
#undef IS_BCAST_PROBEREQ
#undef IS_PROBEREQ
struct ieee80211_node *
_ieee80211_find_txnode(struct ieee80211vap *vap,
const uint8_t macaddr[IEEE80211_ADDR_LEN],
const char *func __debrefcnt_used, int line __debrefcnt_used)
{
struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
struct ieee80211_node *ni;
IEEE80211_NODE_LOCK(nt);
if (vap->iv_opmode == IEEE80211_M_STA ||
vap->iv_opmode == IEEE80211_M_WDS ||
IEEE80211_IS_MULTICAST(macaddr))
ni = ieee80211_ref_node(vap->iv_bss);
else
ni = _ieee80211_find_node_locked(nt, macaddr, func, line);
IEEE80211_NODE_UNLOCK(nt);
if (ni == NULL) {
if (vap->iv_opmode == IEEE80211_M_IBSS ||
vap->iv_opmode == IEEE80211_M_AHDEMO) {
ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
if (ni != NULL)
(void) ieee80211_ref_node(ni);
} else {
IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
"no node, discard frame (%s)", __func__);
vap->iv_stats.is_tx_nonode++;
}
}
return ni;
}
struct ieee80211_node *
_ieee80211_ref_node(struct ieee80211_node *ni,
const char *func __debrefcnt_used, int line __debrefcnt_used)
{
#ifdef IEEE80211_DEBUG_REFCNT
IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
#endif
ieee80211_node_incref(ni);
return (ni);
}
static void
__ieee80211_free_node(struct ieee80211_node *ni)
{
struct ieee80211_node_table *nt = ni->ni_table;
#if 0
IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
"%s %p<%s> in %s table\n", __func__, ni,
ether_sprintf(ni->ni_macaddr),
nt != NULL ? nt->nt_name : "<gone>");
#endif
if (ni->ni_associd != 0) {
struct ieee80211vap *vap = ni->ni_vap;
if (vap->iv_aid_bitmap != NULL)
IEEE80211_AID_CLR(vap, ni->ni_associd);
}
if (nt != NULL)
ieee80211_del_node_nt(nt, ni);
ni->ni_ic->ic_node_free(ni);
}
static int
node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
{
ieee80211_keyix keyix;
keyix = ni->ni_ucastkey.wk_rxkeyix;
if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
nt->nt_keyixmap[keyix] == ni) {
IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
"%s: %p<%s> clear key map entry %u\n",
__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
nt->nt_keyixmap[keyix] = NULL;
ieee80211_node_decref(ni);
return 1;
}
return 0;
}
void
_ieee80211_free_node(struct ieee80211_node *ni,
const char *func __debrefcnt_used, int line __debrefcnt_used)
{
struct ieee80211_node_table *nt = ni->ni_table;
#ifdef IEEE80211_DEBUG_REFCNT
IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
#endif
if (nt != NULL) {
IEEE80211_NODE_LOCK(nt);
if (ieee80211_node_dectestref(ni)) {
__ieee80211_free_node(ni);
} else if (ieee80211_node_refcnt(ni) == 1)
if (node_clear_keyixmap(nt, ni))
__ieee80211_free_node(ni);
IEEE80211_NODE_UNLOCK(nt);
} else {
if (ieee80211_node_dectestref(ni))
__ieee80211_free_node(ni);
}
}
int
ieee80211_node_delucastkey(struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211_node_table *nt = &ic->ic_sta;
struct ieee80211_node *nikey;
ieee80211_keyix keyix;
int isowned, status;
isowned = IEEE80211_NODE_IS_LOCKED(nt);
if (!isowned)
IEEE80211_NODE_LOCK(nt);
nikey = NULL;
status = 1;
if (!IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
keyix = ni->ni_ucastkey.wk_rxkeyix;
status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
nikey = nt->nt_keyixmap[keyix];
nt->nt_keyixmap[keyix] = NULL;
}
}
if (!isowned)
IEEE80211_NODE_UNLOCK(nt);
if (nikey != NULL) {
KASSERT(nikey == ni,
("key map out of sync, ni %p nikey %p", ni, nikey));
IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
"%s: delete key map entry %p<%s> refcnt %d\n",
__func__, ni, ether_sprintf(ni->ni_macaddr),
ieee80211_node_refcnt(ni)-1);
ieee80211_free_node(ni);
}
return status;
}
static void
node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
{
IEEE80211_NODE_LOCK_ASSERT(nt);
IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
"%s: remove %p<%s> from %s table, refcnt %d\n",
__func__, ni, ether_sprintf(ni->ni_macaddr),
nt->nt_name, ieee80211_node_refcnt(ni)-1);
(void)node_clear_keyixmap(nt, ni);
if (!ieee80211_node_dectestref(ni)) {
ieee80211_del_node_nt(nt, ni);
} else
__ieee80211_free_node(ni);
}
static void
ieee80211_node_table_init(struct ieee80211com *ic,
struct ieee80211_node_table *nt,
const char *name, int inact, int keyixmax)
{
nt->nt_ic = ic;
IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
TAILQ_INIT(&nt->nt_node);
nt->nt_count = 0;
nt->nt_name = name;
nt->nt_inact_init = inact;
nt->nt_keyixmax = keyixmax;
if (nt->nt_keyixmax > 0) {
nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
keyixmax * sizeof(struct ieee80211_node *),
M_80211_NODE,
IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
if (nt->nt_keyixmap == NULL)
ic_printf(ic,
"Cannot allocate key index map with %u entries\n",
keyixmax);
} else
nt->nt_keyixmap = NULL;
}
static void
ieee80211_node_table_reset(struct ieee80211_node_table *nt,
struct ieee80211vap *match)
{
struct ieee80211_node *ni, *next;
IEEE80211_NODE_LOCK(nt);
TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
if (match != NULL && ni->ni_vap != match)
continue;
if (ni->ni_associd != 0) {
struct ieee80211vap *vap = ni->ni_vap;
if (vap->iv_auth->ia_node_leave != NULL)
vap->iv_auth->ia_node_leave(ni);
if (vap->iv_aid_bitmap != NULL)
IEEE80211_AID_CLR(vap, ni->ni_associd);
}
ni->ni_wdsvap = NULL;
node_reclaim(nt, ni);
}
if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
if (ni->ni_wdsvap == match)
ni->ni_wdsvap = NULL;
}
IEEE80211_NODE_UNLOCK(nt);
}
static void
ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
{
ieee80211_node_table_reset(nt, NULL);
if (nt->nt_keyixmap != NULL) {
#ifdef DIAGNOSTIC
int i;
for (i = 0; i < nt->nt_keyixmax; i++)
if (nt->nt_keyixmap[i] != NULL)
net80211_printf("%s: %s[%u] still active\n", __func__,
nt->nt_name, i);
#endif
IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
nt->nt_keyixmap = NULL;
}
IEEE80211_NODE_LOCK_DESTROY(nt);
}
static void
timeout_stations(void *arg __unused, struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211vap *vap = ni->ni_vap;
if (vap->iv_state != IEEE80211_S_RUN)
return;
if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
vap->iv_opmode == IEEE80211_M_STA) &&
(ni->ni_flags & IEEE80211_NODE_AREF) == 0)
return;
if (ni->ni_rxfrag[0] != NULL &&
ticks > ni->ni_rxfragstamp + hz) {
m_freem(ni->ni_rxfrag[0]);
ni->ni_rxfrag[0] = NULL;
}
if (ni->ni_inact > 0) {
ni->ni_inact--;
IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
"%s: inact %u inact_reload %u nrates %u",
__func__, ni->ni_inact, ni->ni_inact_reload,
ni->ni_rates.rs_nrates);
}
if (ni == vap->iv_bss)
return;
if (ni->ni_associd != 0 ||
(vap->iv_opmode == IEEE80211_M_IBSS ||
vap->iv_opmode == IEEE80211_M_AHDEMO)) {
ic->ic_node_age(ni);
if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
(0 < ni->ni_inact &&
ni->ni_inact <= vap->iv_inact_probe) &&
ni->ni_rates.rs_nrates != 0) {
IEEE80211_NOTE(vap,
IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
ni, "%s",
"probe station due to inactivity");
ieee80211_ref_node(ni);
ieee80211_send_nulldata(ni);
return;
}
}
if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
ni->ni_inact <= 0) {
IEEE80211_NOTE(vap,
IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
"station timed out due to inactivity "
"(refcnt %u)", ieee80211_node_refcnt(ni));
if (ni->ni_associd != 0) {
IEEE80211_SEND_MGMT(ni,
IEEE80211_FC0_SUBTYPE_DEAUTH,
IEEE80211_REASON_AUTH_EXPIRE);
}
ieee80211_node_leave(ni);
vap->iv_stats.is_node_timeout++;
}
}
static void
ieee80211_timeout_stations(struct ieee80211com *ic)
{
struct ieee80211_node_table *nt = &ic->ic_sta;
ieee80211_iterate_nodes(nt, timeout_stations, NULL);
}
void
ieee80211_drain(struct ieee80211com *ic)
{
struct ieee80211_node_table *nt = &ic->ic_sta;
struct ieee80211vap *vap;
struct ieee80211_node *ni;
IEEE80211_NODE_LOCK(nt);
TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
vap = ni->ni_vap;
if (vap->iv_state != IEEE80211_S_RUN)
continue;
if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
vap->iv_opmode == IEEE80211_M_STA) &&
(ni->ni_flags & IEEE80211_NODE_AREF) == 0)
continue;
if (ni->ni_rxfrag[0] != NULL) {
m_freem(ni->ni_rxfrag[0]);
ni->ni_rxfrag[0] = NULL;
}
ic->ic_node_drain(ni);
}
IEEE80211_NODE_UNLOCK(nt);
}
static void
ieee80211_vap_timeout(struct ieee80211vap *vap)
{
IEEE80211_LOCK_ASSERT(vap->iv_ic);
ieee80211_vap_erp_timeout(vap);
ieee80211_ht_timeout(vap);
ieee80211_vht_timeout(vap);
}
void
ieee80211_node_timeout(void *arg)
{
struct ieee80211com *ic = arg;
struct ieee80211vap *vap;
if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
ieee80211_scan_timeout(ic);
ieee80211_timeout_stations(ic);
ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
IEEE80211_LOCK(ic);
TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
ieee80211_vap_timeout(vap);
IEEE80211_UNLOCK(ic);
}
callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
ieee80211_node_timeout, ic);
}
int
ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
{
struct ieee80211_node **ni_arr;
struct ieee80211_node *ni;
size_t size;
int count, i;
IEEE80211_NODE_LOCK(nt);
count = nt->nt_count;
size = count * sizeof(struct ieee80211_node *);
ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
if (ni_arr == NULL) {
IEEE80211_NODE_UNLOCK(nt);
return (ENOMEM);
}
i = 0;
TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
if (vap != NULL && ni->ni_vap != vap)
continue;
KASSERT(i < count,
("node array overflow (vap %p, i %d, count %d)\n",
vap, i, count));
ni_arr[i] = ieee80211_ref_node(ni);
i++;
}
IEEE80211_NODE_UNLOCK(nt);
for (i = 0; i < count; i++) {
if (ni_arr[i] == NULL)
break;
(*f)(arg, ni_arr[i]);
ieee80211_free_node(ni_arr[i]);
}
IEEE80211_FREE(ni_arr, M_80211_NODE);
return (0);
}
void
ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
ieee80211_iter_func *f, void *arg)
{
(void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
}
void
ieee80211_dump_node(struct ieee80211_node_table *nt __unused,
struct ieee80211_node *ni)
{
net80211_printf("%p: mac %s refcnt %d\n", ni,
ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
net80211_printf("\tauthmode %u flags 0x%x\n",
ni->ni_authmode, ni->ni_flags);
net80211_printf("\tassocid 0x%x txpower %u vlan %u\n",
ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
net80211_printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
ni->ni_txseqs[IEEE80211_NONQOS_TID],
ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
ni->ni_rxfragstamp);
net80211_printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
node_getrssi(ni), ni->ni_noise,
ni->ni_intval, ni->ni_capinfo);
net80211_printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
ether_sprintf(ni->ni_bssid),
ni->ni_esslen, ni->ni_essid,
(ni->ni_chan != IEEE80211_CHAN_ANYC) ? ni->ni_chan->ic_freq : 0,
(ni->ni_chan != IEEE80211_CHAN_ANYC) ? ni->ni_chan->ic_flags : 0);
net80211_printf("\tinact %u inact_reload %u txrate type %d dot11rate %u\n",
ni->ni_inact, ni->ni_inact_reload,
ni->ni_txrate.type,
ni->ni_txrate.dot11rate);
net80211_printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
ni->ni_htcap, ni->ni_htparam,
ni->ni_htctlchan, ni->ni_ht2ndchan);
net80211_printf("\thtopmode %x htstbc %x htchw %d (%s)\n",
ni->ni_htopmode, ni->ni_htstbc,
ni->ni_chw, net80211_ni_chw_to_str(ni->ni_chw));
net80211_printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
(int) ni->ni_vht_basicmcs);
}
void
ieee80211_dump_nodes(struct ieee80211_node_table *nt)
{
ieee80211_iterate_nodes(nt,
(ieee80211_iter_func *) ieee80211_dump_node, nt);
}
void
ieee80211_notify_erp_locked(struct ieee80211com *ic)
{
struct ieee80211vap *vap;
IEEE80211_LOCK_ASSERT(ic);
TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
if (vap->iv_opmode == IEEE80211_M_HOSTAP)
ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
}
static void
ieee80211_node_join_11g(struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211vap *vap = ni->ni_vap;
IEEE80211_LOCK_ASSERT(ic);
if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
vap->iv_longslotsta++;
IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
"station needs long slot time, count %d",
vap->iv_longslotsta);
if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
ieee80211_vap_set_shortslottime(vap, 0);
}
}
if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
vap->iv_nonerpsta++;
IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
"station is !ERP, %d non-ERP stations associated",
vap->iv_nonerpsta);
if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
"%s", "station needs long preamble");
vap->iv_flags |= IEEE80211_F_USEBARKER;
vap->iv_flags &= ~IEEE80211_F_SHPREAMBLE;
ieee80211_vap_update_preamble(vap);
}
if (vap->iv_protmode != IEEE80211_PROT_NONE &&
vap->iv_nonerpsta == 1 &&
(vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
"%s: enable use of protection\n", __func__);
vap->iv_flags |= IEEE80211_F_USEPROT;
ieee80211_vap_update_erp_protmode(vap);
}
} else
ni->ni_flags |= IEEE80211_NODE_ERP;
}
void
ieee80211_node_join(struct ieee80211_node *ni, int resp)
{
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211vap *vap = ni->ni_vap;
int newassoc;
if (ni->ni_associd == 0) {
uint16_t aid;
KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
for (aid = 1; aid < vap->iv_max_aid; aid++) {
if (!IEEE80211_AID_ISSET(vap, aid))
break;
}
if (aid >= vap->iv_max_aid) {
IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
ieee80211_node_leave(ni);
return;
}
ni->ni_associd = aid | 0xc000;
ni->ni_jointime = time_uptime;
IEEE80211_LOCK(ic);
IEEE80211_AID_SET(vap, ni->ni_associd);
vap->iv_sta_assoc++;
if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
ieee80211_ht_node_join(ni);
if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
ieee80211_vht_node_join(ni);
if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
ieee80211_node_join_11g(ni);
IEEE80211_UNLOCK(ic);
newassoc = 1;
} else
newassoc = 0;
IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
"station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s",
IEEE80211_NODE_AID(ni),
vap->iv_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
vap->iv_flags & IEEE80211_F_SHSLOT ? "short" : "long",
vap->iv_flags & IEEE80211_F_USEPROT ? ", protection" : "",
ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
ni->ni_flags & IEEE80211_NODE_HT ?
(ni->ni_chw == NET80211_STA_RX_BW_40 ? ", HT40" : ", HT20") : "",
ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "",
ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
", fast-frames" : "",
IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
", turbo" : ""
);
ieee80211_node_setuptxparms(ni);
ieee80211_ratectl_node_init(ni);
if (ic->ic_newassoc != NULL)
ic->ic_newassoc(ni, newassoc);
IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
if (vap->iv_auth->ia_node_join != NULL)
vap->iv_auth->ia_node_join(ni);
ieee80211_notify_node_join(ni,
resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
}
static void
disable_protection(struct ieee80211vap *vap)
{
struct ieee80211com *ic = vap->iv_ic;
KASSERT(vap->iv_nonerpsta == 0 &&
(vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
("%d non ERP stations, flags 0x%x", vap->iv_nonerpsta,
vap->iv_flags_ext));
vap->iv_flags &= ~IEEE80211_F_USEPROT;
if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
vap->iv_flags |= IEEE80211_F_SHPREAMBLE;
vap->iv_flags &= ~IEEE80211_F_USEBARKER;
}
ieee80211_vap_update_erp_protmode(vap);
ieee80211_vap_update_preamble(vap);
}
static void
ieee80211_node_leave_11g(struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211vap *vap = ni->ni_vap;
IEEE80211_LOCK_ASSERT(ic);
KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
ic->ic_bsschan->ic_flags));
if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
KASSERT(vap->iv_longslotsta > 0,
("bogus long slot station count %d", vap->iv_longslotsta));
vap->iv_longslotsta--;
IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
"long slot time station leaves, count now %d",
vap->iv_longslotsta);
if (vap->iv_longslotsta == 0) {
if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
ic->ic_opmode != IEEE80211_M_IBSS) {
IEEE80211_DPRINTF(ni->ni_vap,
IEEE80211_MSG_ASSOC,
"%s: re-enable use of short slot time\n",
__func__);
ieee80211_vap_set_shortslottime(vap, 1);
}
}
}
if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
KASSERT(vap->iv_nonerpsta > 0,
("bogus non-ERP station count %d", vap->iv_nonerpsta));
vap->iv_nonerpsta--;
IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
"non-ERP station leaves, count now %d%s", vap->iv_nonerpsta,
(vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
" (non-ERP sta present)" : "");
if (vap->iv_nonerpsta == 0 &&
(vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
"%s: disable use of protection\n", __func__);
disable_protection(vap);
}
}
}
static void
ieee80211_vap_erp_timeout(struct ieee80211vap *vap)
{
IEEE80211_LOCK_ASSERT(vap->iv_ic);
if ((vap->iv_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
ieee80211_time_after(ticks, vap->iv_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
"%s", "age out non-ERP sta present on channel");
vap->iv_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
if (vap->iv_nonerpsta == 0)
disable_protection(vap);
}
}
void
ieee80211_node_leave(struct ieee80211_node *ni)
{
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211vap *vap = ni->ni_vap;
struct ieee80211_node_table *nt = ni->ni_table;
IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
"station with aid %d leaves", IEEE80211_NODE_AID(ni));
KASSERT(vap->iv_opmode != IEEE80211_M_STA,
("unexpected operating mode %u", vap->iv_opmode));
if (ni->ni_associd == 0)
goto done;
if (vap->iv_auth->ia_node_leave != NULL)
vap->iv_auth->ia_node_leave(ni);
IEEE80211_LOCK(ic);
IEEE80211_AID_CLR(vap, ni->ni_associd);
vap->iv_sta_assoc--;
if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
ieee80211_vht_node_leave(ni);
if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
ieee80211_ht_node_leave(ni);
if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
ieee80211_node_leave_11g(ni);
IEEE80211_UNLOCK(ic);
ieee80211_sta_leave(ni);
done:
if (nt != NULL) {
IEEE80211_NODE_LOCK(nt);
node_reclaim(nt, ni);
IEEE80211_NODE_UNLOCK(nt);
} else
ieee80211_free_node(ni);
}
struct rssiinfo {
int rssi_samples;
uint32_t rssi_total;
};
static void
get_hostap_rssi(void *arg, struct ieee80211_node *ni)
{
struct rssiinfo *info = arg;
struct ieee80211vap *vap = ni->ni_vap;
int8_t rssi;
if (ni->ni_associd == 0)
return;
rssi = vap->iv_ic->ic_node_getrssi(ni);
if (rssi != 0) {
info->rssi_samples++;
info->rssi_total += rssi;
}
}
static void
get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
{
struct rssiinfo *info = arg;
struct ieee80211vap *vap = ni->ni_vap;
int8_t rssi;
if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
return;
rssi = vap->iv_ic->ic_node_getrssi(ni);
if (rssi != 0) {
info->rssi_samples++;
info->rssi_total += rssi;
}
}
#ifdef IEEE80211_SUPPORT_MESH
static void
get_mesh_rssi(void *arg, struct ieee80211_node *ni)
{
struct rssiinfo *info = arg;
struct ieee80211vap *vap = ni->ni_vap;
int8_t rssi;
if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
return;
rssi = vap->iv_ic->ic_node_getrssi(ni);
if (rssi != 0) {
info->rssi_samples++;
info->rssi_total += rssi;
}
}
#endif
int8_t
ieee80211_getrssi(struct ieee80211vap *vap)
{
#define NZ(x) ((x) == 0 ? 1 : (x))
struct ieee80211com *ic = vap->iv_ic;
struct rssiinfo info;
info.rssi_total = 0;
info.rssi_samples = 0;
switch (vap->iv_opmode) {
case IEEE80211_M_IBSS:
case IEEE80211_M_AHDEMO:
ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
&info);
break;
case IEEE80211_M_HOSTAP:
ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
&info);
break;
#ifdef IEEE80211_SUPPORT_MESH
case IEEE80211_M_MBSS:
ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
&info);
break;
#endif
case IEEE80211_M_MONITOR:
case IEEE80211_M_STA:
default:
if (vap->iv_bss != NULL)
info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
info.rssi_samples = 1;
break;
}
return info.rssi_total / NZ(info.rssi_samples);
#undef NZ
}
void
ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
{
if (vap->iv_bss == NULL)
return;
vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
if (vap->iv_opmode != IEEE80211_M_STA)
*rssi = ieee80211_getrssi(vap);
}
ieee80211_seq ieee80211_tx_seqno_fetch_incr(struct ieee80211_node *ni,
uint8_t tid)
{
ieee80211_seq seq;
seq = ni->ni_txseqs[tid];
ni->ni_txseqs[tid] = (ni->ni_txseqs[tid] + 1) % IEEE80211_SEQ_RANGE;
return (seq);
}
ieee80211_seq ieee80211_tx_seqno_fetch(const struct ieee80211_node *ni,
uint8_t tid)
{
return (ni->ni_txseqs[tid]);
}
uint8_t
ieee80211_node_get_txrate_dot11rate(struct ieee80211_node *ni)
{
switch (ni->ni_txrate.type) {
case IEEE80211_NODE_TXRATE_LEGACY:
case IEEE80211_NODE_TXRATE_HT:
return (ni->ni_txrate.dot11rate);
break;
case IEEE80211_NODE_TXRATE_VHT:
default:
net80211_vap_printf(ni->ni_vap,
"%s: called for VHT / unknown rate (type %d)!\n",
__func__, ni->ni_txrate.type);
return (12);
}
}
void
ieee80211_node_get_txrate(struct ieee80211_node *ni,
struct ieee80211_node_txrate *txr)
{
MPASS(ni != NULL);
MPASS(txr != NULL);
*txr = ni->ni_txrate;
}
void
ieee80211_node_set_txrate(struct ieee80211_node *ni,
const struct ieee80211_node_txrate *txr)
{
MPASS(ni != NULL);
MPASS(txr != NULL);
ni->ni_txrate = *txr;
}
void
ieee80211_node_set_txrate_dot11rate(struct ieee80211_node *ni,
uint8_t dot11Rate)
{
if (dot11Rate & IEEE80211_RATE_MCS) {
ni->ni_txrate.type = IEEE80211_NODE_TXRATE_HT;
ni->ni_txrate.mcs = dot11Rate & IEEE80211_RATE_VAL;
ni->ni_txrate.nss = 0;
ni->ni_txrate.dot11rate = dot11Rate;
} else {
ni->ni_txrate.type = IEEE80211_NODE_TXRATE_LEGACY;
ni->ni_txrate.mcs = ni->ni_txrate.nss = 0;
ni->ni_txrate.dot11rate = dot11Rate;
}
}
void
ieee80211_node_set_txrate_ht_mcsrate(struct ieee80211_node *ni,
uint8_t mcs)
{
KASSERT(mcs <= 76, ("%s: MCS is not 0..76 (%d)", __func__, mcs));
if (mcs > 76) {
ic_printf(ni->ni_ic, "%s: invalid MCS (%d)\n", __func__, mcs);
return;
}
ni->ni_txrate.type = IEEE80211_NODE_TXRATE_HT;
ni->ni_txrate.mcs = mcs;
ni->ni_txrate.nss = 0;
ni->ni_txrate.dot11rate = IEEE80211_RATE_MCS | mcs;
}
void
ieee80211_node_set_txrate_vht_rate(struct ieee80211_node *ni,
uint8_t nss, uint8_t mcs)
{
MPASS(ni != NULL);
ni->ni_txrate.type = IEEE80211_NODE_TXRATE_VHT;
ni->ni_txrate.mcs = mcs;
ni->ni_txrate.nss = nss;
ni->ni_txrate.dot11rate = 0;
}
uint32_t
ieee80211_node_get_txrate_kbit(struct ieee80211_node *ni)
{
uint32_t kbps;
switch (ni->ni_txrate.type) {
case IEEE80211_NODE_TXRATE_LEGACY:
kbps = ni->ni_txrate.dot11rate * 500;
break;
case IEEE80211_NODE_TXRATE_HT:
{
const struct ieee80211_mcs_rates *mcs =
&ieee80211_htrates[ni->ni_txrate.dot11rate &
~IEEE80211_RATE_MCS];
if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
if (ni->ni_flags & IEEE80211_NODE_SGI40)
kbps = mcs->ht40_rate_800ns * 500;
else
kbps = mcs->ht40_rate_400ns * 500;
} else {
if (ni->ni_flags & IEEE80211_NODE_SGI20)
kbps = mcs->ht20_rate_800ns * 500;
else
kbps = mcs->ht20_rate_400ns * 500;
}
}
break;
case IEEE80211_NODE_TXRATE_VHT:
kbps = ieee80211_phy_vht_get_mcs_kbit(ni->ni_chw,
ni->ni_txrate.nss, ni->ni_txrate.mcs, false);
break;
default:
net80211_vap_printf(ni->ni_vap,
"%s: called for unknown rate (type %d)!\n", __func__,
ni->ni_txrate.type);
return (0);
}
return (kbps);
}