Home | History | Annotate | Line # | Download | only in net80211
ieee80211_node.c revision 1.75.4.1
      1  1.75.4.1      phil /*-
      2  1.75.4.1      phil  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      3  1.75.4.1      phil  *
      4       1.1    dyoung  * Copyright (c) 2001 Atsushi Onoe
      5  1.75.4.1      phil  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
      6       1.1    dyoung  * All rights reserved.
      7       1.1    dyoung  *
      8       1.1    dyoung  * Redistribution and use in source and binary forms, with or without
      9       1.1    dyoung  * modification, are permitted provided that the following conditions
     10       1.1    dyoung  * are met:
     11       1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     12       1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     13       1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     15       1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     16       1.1    dyoung  *
     17       1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18       1.1    dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19       1.1    dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20       1.1    dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21       1.1    dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22       1.1    dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23       1.1    dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24       1.1    dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25       1.1    dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26       1.1    dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27       1.1    dyoung  */
     28       1.1    dyoung 
     29       1.1    dyoung #include <sys/cdefs.h>
     30  1.75.4.1      phil __FBSDID("$FreeBSD$");
     31       1.1    dyoung 
     32  1.75.4.1      phil #include "opt_wlan.h"
     33       1.1    dyoung 
     34       1.1    dyoung #include <sys/param.h>
     35  1.75.4.1      phil #include <sys/systm.h>
     36  1.75.4.1      phil #include <sys/mbuf.h>
     37       1.1    dyoung #include <sys/malloc.h>
     38       1.1    dyoung #include <sys/kernel.h>
     39      1.39    dyoung 
     40       1.1    dyoung #include <sys/socket.h>
     41  1.75.4.1      phil 
     42       1.1    dyoung #include <net/if.h>
     43  1.75.4.1      phil #include <net/if_var.h>
     44       1.1    dyoung #include <net/if_media.h>
     45  1.75.4.1      phil #include <net/ethernet.h>
     46       1.1    dyoung 
     47       1.1    dyoung #include <net80211/ieee80211_var.h>
     48  1.75.4.1      phil #include <net80211/ieee80211_input.h>
     49  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
     50  1.75.4.1      phil #include <net80211/ieee80211_superg.h>
     51  1.75.4.1      phil #endif
     52  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_TDMA
     53  1.75.4.1      phil #include <net80211/ieee80211_tdma.h>
     54  1.75.4.1      phil #endif
     55  1.75.4.1      phil #include <net80211/ieee80211_wds.h>
     56  1.75.4.1      phil #include <net80211/ieee80211_mesh.h>
     57  1.75.4.1      phil #include <net80211/ieee80211_ratectl.h>
     58  1.75.4.1      phil #include <net80211/ieee80211_vht.h>
     59       1.1    dyoung 
     60       1.1    dyoung #include <net/bpf.h>
     61       1.1    dyoung 
     62  1.75.4.1      phil /*
     63  1.75.4.1      phil  * IEEE80211_NODE_HASHSIZE must be a power of 2.
     64  1.75.4.1      phil  */
     65  1.75.4.1      phil CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
     66       1.1    dyoung 
     67      1.42    dyoung /*
     68      1.42    dyoung  * Association id's are managed with a bit vector.
     69      1.42    dyoung  */
     70  1.75.4.1      phil #define	IEEE80211_AID_SET(_vap, b) \
     71  1.75.4.1      phil 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
     72  1.75.4.1      phil 		(1 << (IEEE80211_AID(b) % 32)))
     73  1.75.4.1      phil #define	IEEE80211_AID_CLR(_vap, b) \
     74  1.75.4.1      phil 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
     75  1.75.4.1      phil 		~(1 << (IEEE80211_AID(b) % 32)))
     76  1.75.4.1      phil #define	IEEE80211_AID_ISSET(_vap, b) \
     77  1.75.4.1      phil 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
     78  1.75.4.1      phil 
     79  1.75.4.1      phil static int ieee80211_sta_join1(struct ieee80211_node *);
     80      1.42    dyoung 
     81  1.75.4.1      phil static struct ieee80211_node *node_alloc(struct ieee80211vap *,
     82  1.75.4.1      phil 	const uint8_t [IEEE80211_ADDR_LEN]);
     83      1.39    dyoung static void node_cleanup(struct ieee80211_node *);
     84      1.39    dyoung static void node_free(struct ieee80211_node *);
     85  1.75.4.1      phil static void node_age(struct ieee80211_node *);
     86  1.75.4.1      phil static int8_t node_getrssi(const struct ieee80211_node *);
     87  1.75.4.1      phil static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
     88  1.75.4.1      phil static void node_getmimoinfo(const struct ieee80211_node *,
     89  1.75.4.1      phil 	struct ieee80211_mimo_info *);
     90      1.39    dyoung 
     91      1.39    dyoung static void _ieee80211_free_node(struct ieee80211_node *);
     92      1.39    dyoung 
     93  1.75.4.1      phil static void node_reclaim(struct ieee80211_node_table *nt,
     94  1.75.4.1      phil 	struct ieee80211_node *ni);
     95      1.39    dyoung static void ieee80211_node_table_init(struct ieee80211com *ic,
     96      1.45     skrll 	struct ieee80211_node_table *nt, const char *name,
     97  1.75.4.1      phil 	int inact, int keymaxix);
     98  1.75.4.1      phil static void ieee80211_node_table_reset(struct ieee80211_node_table *,
     99  1.75.4.1      phil 	struct ieee80211vap *);
    100      1.39    dyoung static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
    101  1.75.4.1      phil static void ieee80211_erp_timeout(struct ieee80211com *);
    102       1.1    dyoung 
    103      1.11    dyoung MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
    104  1.75.4.1      phil MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
    105       1.9    dyoung 
    106       1.1    dyoung void
    107      1.23   mycroft ieee80211_node_attach(struct ieee80211com *ic)
    108       1.1    dyoung {
    109  1.75.4.1      phil 	/* XXX really want maxlen enforced per-sta */
    110  1.75.4.1      phil 	ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
    111  1.75.4.1      phil 	    "802.11 staging q");
    112  1.75.4.1      phil 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
    113  1.75.4.1      phil 		IEEE80211_INACT_INIT, ic->ic_max_keyix);
    114  1.75.4.1      phil 	callout_init(&ic->ic_inact, 1);
    115  1.75.4.1      phil 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
    116  1.75.4.1      phil 		ieee80211_node_timeout, ic);
    117       1.1    dyoung 
    118      1.39    dyoung 	ic->ic_node_alloc = node_alloc;
    119      1.39    dyoung 	ic->ic_node_free = node_free;
    120      1.39    dyoung 	ic->ic_node_cleanup = node_cleanup;
    121  1.75.4.1      phil 	ic->ic_node_age = node_age;
    122  1.75.4.1      phil 	ic->ic_node_drain = node_age;		/* NB: same as age */
    123      1.39    dyoung 	ic->ic_node_getrssi = node_getrssi;
    124  1.75.4.1      phil 	ic->ic_node_getsignal = node_getsignal;
    125  1.75.4.1      phil 	ic->ic_node_getmimoinfo = node_getmimoinfo;
    126      1.39    dyoung 
    127  1.75.4.1      phil 	/*
    128  1.75.4.1      phil 	 * Set flags to be propagated to all vap's;
    129  1.75.4.1      phil 	 * these define default behaviour/configuration.
    130  1.75.4.1      phil 	 */
    131  1.75.4.1      phil 	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
    132  1.75.4.1      phil }
    133  1.75.4.1      phil 
    134  1.75.4.1      phil void
    135  1.75.4.1      phil ieee80211_node_detach(struct ieee80211com *ic)
    136  1.75.4.1      phil {
    137      1.22   mycroft 
    138  1.75.4.1      phil 	callout_drain(&ic->ic_inact);
    139  1.75.4.1      phil 	ieee80211_node_table_cleanup(&ic->ic_sta);
    140  1.75.4.1      phil 	ieee80211_ageq_cleanup(&ic->ic_stageq);
    141      1.45     skrll }
    142      1.45     skrll 
    143      1.45     skrll void
    144  1.75.4.1      phil ieee80211_node_vattach(struct ieee80211vap *vap)
    145      1.45     skrll {
    146  1.75.4.1      phil 	/* NB: driver can override */
    147  1.75.4.1      phil 	vap->iv_max_aid = IEEE80211_AID_DEF;
    148      1.45     skrll 
    149  1.75.4.1      phil 	/* default station inactivity timer setings */
    150  1.75.4.1      phil 	vap->iv_inact_init = IEEE80211_INACT_INIT;
    151  1.75.4.1      phil 	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
    152  1.75.4.1      phil 	vap->iv_inact_run = IEEE80211_INACT_RUN;
    153  1.75.4.1      phil 	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
    154       1.1    dyoung 
    155  1.75.4.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
    156  1.75.4.1      phil 	    "%s: init %u auth %u run %u probe %u\n", __func__,
    157  1.75.4.1      phil 	    vap->iv_inact_init, vap->iv_inact_auth,
    158  1.75.4.1      phil 	    vap->iv_inact_run, vap->iv_inact_probe);
    159  1.75.4.1      phil }
    160  1.75.4.1      phil 
    161  1.75.4.1      phil void
    162  1.75.4.1      phil ieee80211_node_latevattach(struct ieee80211vap *vap)
    163  1.75.4.1      phil {
    164  1.75.4.1      phil 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
    165  1.75.4.1      phil 		/* XXX should we allow max aid to be zero? */
    166  1.75.4.1      phil 		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
    167  1.75.4.1      phil 			vap->iv_max_aid = IEEE80211_AID_MIN;
    168  1.75.4.1      phil 			if_printf(vap->iv_ifp,
    169  1.75.4.1      phil 			    "WARNING: max aid too small, changed to %d\n",
    170  1.75.4.1      phil 			    vap->iv_max_aid);
    171  1.75.4.1      phil 		}
    172  1.75.4.1      phil 		vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
    173  1.75.4.1      phil 			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
    174  1.75.4.1      phil 			M_80211_NODE,
    175  1.75.4.1      phil 			IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    176  1.75.4.1      phil 		if (vap->iv_aid_bitmap == NULL) {
    177  1.75.4.1      phil 			/* XXX no way to recover */
    178  1.75.4.1      phil 			printf("%s: no memory for AID bitmap, max aid %d!\n",
    179  1.75.4.1      phil 			    __func__, vap->iv_max_aid);
    180  1.75.4.1      phil 			vap->iv_max_aid = 0;
    181  1.75.4.1      phil 		}
    182      1.39    dyoung 	}
    183      1.33    dyoung 
    184  1.75.4.1      phil 	ieee80211_reset_bss(vap);
    185      1.39    dyoung 
    186  1.75.4.1      phil 	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
    187       1.1    dyoung }
    188       1.1    dyoung 
    189       1.1    dyoung void
    190  1.75.4.1      phil ieee80211_node_vdetach(struct ieee80211vap *vap)
    191       1.1    dyoung {
    192  1.75.4.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    193       1.1    dyoung 
    194  1.75.4.1      phil 	ieee80211_node_table_reset(&ic->ic_sta, vap);
    195  1.75.4.1      phil 	if (vap->iv_bss != NULL) {
    196  1.75.4.1      phil 		ieee80211_free_node(vap->iv_bss);
    197  1.75.4.1      phil 		vap->iv_bss = NULL;
    198      1.23   mycroft 	}
    199  1.75.4.1      phil 	if (vap->iv_aid_bitmap != NULL) {
    200  1.75.4.1      phil 		IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
    201  1.75.4.1      phil 		vap->iv_aid_bitmap = NULL;
    202      1.39    dyoung 	}
    203      1.39    dyoung }
    204      1.39    dyoung 
    205  1.75.4.1      phil /*
    206      1.39    dyoung  * Port authorize/unauthorize interfaces for use by an authenticator.
    207      1.39    dyoung  */
    208      1.39    dyoung 
    209      1.39    dyoung void
    210      1.45     skrll ieee80211_node_authorize(struct ieee80211_node *ni)
    211      1.39    dyoung {
    212  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    213      1.45     skrll 
    214      1.39    dyoung 	ni->ni_flags |= IEEE80211_NODE_AUTH;
    215  1.75.4.1      phil 	ni->ni_inact_reload = vap->iv_inact_run;
    216  1.75.4.1      phil 	ni->ni_inact = ni->ni_inact_reload;
    217  1.75.4.1      phil 
    218  1.75.4.1      phil 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
    219  1.75.4.1      phil 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
    220      1.39    dyoung }
    221      1.39    dyoung 
    222      1.39    dyoung void
    223      1.45     skrll ieee80211_node_unauthorize(struct ieee80211_node *ni)
    224      1.39    dyoung {
    225  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    226       1.1    dyoung 
    227  1.75.4.1      phil 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
    228  1.75.4.1      phil 	ni->ni_inact_reload = vap->iv_inact_auth;
    229  1.75.4.1      phil 	if (ni->ni_inact > ni->ni_inact_reload)
    230  1.75.4.1      phil 		ni->ni_inact = ni->ni_inact_reload;
    231      1.39    dyoung 
    232  1.75.4.1      phil 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
    233  1.75.4.1      phil 	    "%s: inact_reload %u inact %u", __func__,
    234  1.75.4.1      phil 	    ni->ni_inact_reload, ni->ni_inact);
    235      1.39    dyoung }
    236      1.39    dyoung 
    237       1.1    dyoung /*
    238  1.75.4.1      phil  * Fix tx parameters for a node according to ``association state''.
    239       1.1    dyoung  */
    240  1.75.4.1      phil void
    241  1.75.4.1      phil ieee80211_node_setuptxparms(struct ieee80211_node *ni)
    242       1.1    dyoung {
    243  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    244  1.75.4.1      phil 	enum ieee80211_phymode mode;
    245       1.1    dyoung 
    246  1.75.4.1      phil 	if (ni->ni_flags & IEEE80211_NODE_VHT) {
    247  1.75.4.1      phil 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
    248  1.75.4.1      phil 			mode = IEEE80211_MODE_VHT_5GHZ;
    249  1.75.4.1      phil 		else
    250  1.75.4.1      phil 			mode = IEEE80211_MODE_VHT_2GHZ;
    251  1.75.4.1      phil 	} else if (ni->ni_flags & IEEE80211_NODE_HT) {
    252  1.75.4.1      phil 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
    253  1.75.4.1      phil 			mode = IEEE80211_MODE_11NA;
    254  1.75.4.1      phil 		else
    255  1.75.4.1      phil 			mode = IEEE80211_MODE_11NG;
    256  1.75.4.1      phil 	} else {				/* legacy rate handling */
    257  1.75.4.1      phil 		if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
    258  1.75.4.1      phil 			mode = IEEE80211_MODE_STURBO_A;
    259  1.75.4.1      phil 		else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
    260  1.75.4.1      phil 			mode = IEEE80211_MODE_HALF;
    261  1.75.4.1      phil 		else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
    262  1.75.4.1      phil 			mode = IEEE80211_MODE_QUARTER;
    263  1.75.4.1      phil 		/* NB: 108A should be handled as 11a */
    264  1.75.4.1      phil 		else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
    265  1.75.4.1      phil 			mode = IEEE80211_MODE_11A;
    266  1.75.4.1      phil 		else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
    267  1.75.4.1      phil 		    (ni->ni_flags & IEEE80211_NODE_ERP))
    268  1.75.4.1      phil 			mode = IEEE80211_MODE_11G;
    269  1.75.4.1      phil 		else
    270  1.75.4.1      phil 			mode = IEEE80211_MODE_11B;
    271      1.39    dyoung 	}
    272  1.75.4.1      phil 	ni->ni_txparms = &vap->iv_txparms[mode];
    273       1.1    dyoung }
    274       1.1    dyoung 
    275       1.1    dyoung /*
    276  1.75.4.1      phil  * Set/change the channel.  The rate set is also updated as
    277  1.75.4.1      phil  * to insure a consistent view by drivers.
    278  1.75.4.1      phil  * XXX should be private but hostap needs it to deal with CSA
    279       1.1    dyoung  */
    280       1.1    dyoung void
    281  1.75.4.1      phil ieee80211_node_set_chan(struct ieee80211_node *ni,
    282  1.75.4.1      phil 	struct ieee80211_channel *chan)
    283       1.1    dyoung {
    284  1.75.4.1      phil 	struct ieee80211com *ic = ni->ni_ic;
    285  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    286  1.75.4.1      phil 	enum ieee80211_phymode mode;
    287      1.39    dyoung 
    288  1.75.4.1      phil 	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
    289      1.57     tacha 
    290  1.75.4.1      phil 	ni->ni_chan = chan;
    291  1.75.4.1      phil 	mode = ieee80211_chan2mode(chan);
    292  1.75.4.1      phil 	if (IEEE80211_IS_CHAN_HT(chan)) {
    293      1.57     tacha 		/*
    294  1.75.4.1      phil 		 * We must install the legacy rate est in ni_rates and the
    295  1.75.4.1      phil 		 * HT rate set in ni_htrates.
    296      1.57     tacha 		 */
    297  1.75.4.1      phil 		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
    298  1.75.4.1      phil 		/*
    299  1.75.4.1      phil 		 * Setup bss tx parameters based on operating mode.  We
    300  1.75.4.1      phil 		 * use legacy rates when operating in a mixed HT+non-HT bss
    301  1.75.4.1      phil 		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
    302  1.75.4.1      phil 		 */
    303  1.75.4.1      phil 		if (mode == IEEE80211_MODE_11NA &&
    304  1.75.4.1      phil 		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
    305  1.75.4.1      phil 			mode = IEEE80211_MODE_11A;
    306  1.75.4.1      phil 		else if (mode == IEEE80211_MODE_11NG &&
    307  1.75.4.1      phil 		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
    308  1.75.4.1      phil 			mode = IEEE80211_MODE_11G;
    309  1.75.4.1      phil 		if (mode == IEEE80211_MODE_11G &&
    310  1.75.4.1      phil 		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
    311  1.75.4.1      phil 			mode = IEEE80211_MODE_11B;
    312  1.75.4.1      phil 	}
    313  1.75.4.1      phil 	ni->ni_txparms = &vap->iv_txparms[mode];
    314  1.75.4.1      phil 	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
    315      1.57     tacha }
    316      1.57     tacha 
    317      1.39    dyoung static __inline void
    318      1.39    dyoung copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
    319      1.39    dyoung {
    320      1.39    dyoung 	/* propagate useful state */
    321      1.39    dyoung 	nbss->ni_authmode = obss->ni_authmode;
    322      1.39    dyoung 	nbss->ni_txpower = obss->ni_txpower;
    323      1.39    dyoung 	nbss->ni_vlan = obss->ni_vlan;
    324      1.39    dyoung 	/* XXX statistics? */
    325  1.75.4.1      phil 	/* XXX legacy WDS bssid? */
    326       1.1    dyoung }
    327       1.1    dyoung 
    328       1.1    dyoung void
    329  1.75.4.1      phil ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
    330       1.1    dyoung {
    331  1.75.4.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    332       1.1    dyoung 	struct ieee80211_node *ni;
    333       1.1    dyoung 
    334  1.75.4.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
    335  1.75.4.1      phil 		"%s: creating %s on channel %u%c flags 0x%08x\n", __func__,
    336  1.75.4.1      phil 		ieee80211_opmode_name[vap->iv_opmode],
    337  1.75.4.1      phil 		ieee80211_chan2ieee(ic, chan),
    338  1.75.4.1      phil 		ieee80211_channel_type_char(chan),
    339  1.75.4.1      phil 		chan->ic_flags);
    340      1.39    dyoung 
    341  1.75.4.1      phil 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
    342      1.39    dyoung 	if (ni == NULL) {
    343      1.39    dyoung 		/* XXX recovery? */
    344      1.39    dyoung 		return;
    345      1.16    dyoung 	}
    346  1.75.4.1      phil 	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
    347  1.75.4.1      phil 	ni->ni_esslen = vap->iv_des_ssid[0].len;
    348  1.75.4.1      phil 	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
    349  1.75.4.1      phil 	if (vap->iv_bss != NULL)
    350  1.75.4.1      phil 		copy_bss(ni, vap->iv_bss);
    351      1.45     skrll 	ni->ni_intval = ic->ic_bintval;
    352  1.75.4.1      phil 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
    353       1.1    dyoung 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
    354       1.1    dyoung 	if (ic->ic_phytype == IEEE80211_T_FH) {
    355       1.1    dyoung 		ni->ni_fhdwell = 200;	/* XXX */
    356       1.1    dyoung 		ni->ni_fhindex = 1;
    357       1.1    dyoung 	}
    358  1.75.4.1      phil 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
    359      1.39    dyoung 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
    360  1.75.4.1      phil 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
    361  1.75.4.1      phil 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
    362  1.75.4.1      phil 		else {
    363  1.75.4.1      phil 			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
    364  1.75.4.1      phil 			/* clear group bit, add local bit */
    365  1.75.4.1      phil 			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
    366  1.75.4.1      phil 		}
    367  1.75.4.1      phil 	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
    368  1.75.4.1      phil 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
    369  1.75.4.1      phil 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
    370      1.39    dyoung 		else
    371  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_TDMA
    372  1.75.4.1      phil 		if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
    373  1.75.4.1      phil #endif
    374  1.75.4.1      phil 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
    375  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_MESH
    376  1.75.4.1      phil 	} else if (vap->iv_opmode == IEEE80211_M_MBSS) {
    377  1.75.4.1      phil 		ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
    378  1.75.4.1      phil 		memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
    379  1.75.4.1      phil #endif
    380      1.39    dyoung 	}
    381  1.75.4.1      phil 	/*
    382      1.39    dyoung 	 * Fix the channel and related attributes.
    383      1.39    dyoung 	 */
    384  1.75.4.1      phil 	/* clear DFS CAC state on previous channel */
    385  1.75.4.1      phil 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
    386  1.75.4.1      phil 	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
    387  1.75.4.1      phil 	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
    388  1.75.4.1      phil 		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
    389  1.75.4.1      phil 	ic->ic_bsschan = chan;
    390  1.75.4.1      phil 	ieee80211_node_set_chan(ni, chan);
    391  1.75.4.1      phil 	ic->ic_curmode = ieee80211_chan2mode(chan);
    392      1.39    dyoung 	/*
    393  1.75.4.1      phil 	 * Do mode-specific setup.
    394      1.39    dyoung 	 */
    395  1.75.4.1      phil 	if (IEEE80211_IS_CHAN_FULL(chan)) {
    396  1.75.4.1      phil 		if (IEEE80211_IS_CHAN_ANYG(chan)) {
    397  1.75.4.1      phil 			/*
    398  1.75.4.1      phil 			 * Use a mixed 11b/11g basic rate set.
    399  1.75.4.1      phil 			 */
    400  1.75.4.1      phil 			ieee80211_setbasicrates(&ni->ni_rates,
    401  1.75.4.1      phil 			    IEEE80211_MODE_11G);
    402  1.75.4.1      phil 			if (vap->iv_flags & IEEE80211_F_PUREG) {
    403  1.75.4.1      phil 				/*
    404  1.75.4.1      phil 				 * Also mark OFDM rates basic so 11b
    405  1.75.4.1      phil 				 * stations do not join (WiFi compliance).
    406  1.75.4.1      phil 				 */
    407  1.75.4.1      phil 				ieee80211_addbasicrates(&ni->ni_rates,
    408  1.75.4.1      phil 				    IEEE80211_MODE_11A);
    409  1.75.4.1      phil 			}
    410  1.75.4.1      phil 		} else if (IEEE80211_IS_CHAN_B(chan)) {
    411  1.75.4.1      phil 			/*
    412  1.75.4.1      phil 			 * Force pure 11b rate set.
    413  1.75.4.1      phil 			 */
    414  1.75.4.1      phil 			ieee80211_setbasicrates(&ni->ni_rates,
    415  1.75.4.1      phil 				IEEE80211_MODE_11B);
    416  1.75.4.1      phil 		}
    417  1.75.4.1      phil 	}
    418  1.75.4.1      phil 
    419  1.75.4.1      phil 	/* XXX TODO: other bits and pieces - eg fast-frames? */
    420  1.75.4.1      phil 
    421  1.75.4.1      phil 	/* If we're an 11n channel then initialise the 11n bits */
    422  1.75.4.1      phil 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
    423  1.75.4.1      phil 		/* XXX what else? */
    424  1.75.4.1      phil 		ieee80211_ht_node_init(ni);
    425  1.75.4.1      phil 		ieee80211_vht_node_init(ni);
    426  1.75.4.1      phil 	} else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
    427  1.75.4.1      phil 		/* XXX what else? */
    428  1.75.4.1      phil 		ieee80211_ht_node_init(ni);
    429      1.39    dyoung 	}
    430      1.39    dyoung 
    431  1.75.4.1      phil 	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
    432       1.1    dyoung }
    433       1.1    dyoung 
    434  1.75.4.1      phil /*
    435  1.75.4.1      phil  * Reset bss state on transition to the INIT state.
    436  1.75.4.1      phil  * Clear any stations from the table (they have been
    437  1.75.4.1      phil  * deauth'd) and reset the bss node (clears key, rate
    438  1.75.4.1      phil  * etc. state).
    439  1.75.4.1      phil  */
    440      1.39    dyoung void
    441  1.75.4.1      phil ieee80211_reset_bss(struct ieee80211vap *vap)
    442      1.39    dyoung {
    443  1.75.4.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    444      1.39    dyoung 	struct ieee80211_node *ni, *obss;
    445      1.39    dyoung 
    446  1.75.4.1      phil 	ieee80211_node_table_reset(&ic->ic_sta, vap);
    447  1.75.4.1      phil 	/* XXX multi-bss: wrong */
    448  1.75.4.1      phil 	ieee80211_reset_erp(ic);
    449      1.39    dyoung 
    450  1.75.4.1      phil 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
    451  1.75.4.1      phil 	KASSERT(ni != NULL, ("unable to setup initial BSS node"));
    452  1.75.4.1      phil 	obss = vap->iv_bss;
    453  1.75.4.1      phil 	vap->iv_bss = ieee80211_ref_node(ni);
    454      1.39    dyoung 	if (obss != NULL) {
    455      1.39    dyoung 		copy_bss(ni, obss);
    456      1.45     skrll 		ni->ni_intval = ic->ic_bintval;
    457      1.39    dyoung 		ieee80211_free_node(obss);
    458  1.75.4.1      phil 	} else
    459  1.75.4.1      phil 		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
    460      1.39    dyoung }
    461      1.39    dyoung 
    462  1.75.4.1      phil static int
    463  1.75.4.1      phil match_ssid(const struct ieee80211_node *ni,
    464  1.75.4.1      phil 	int nssid, const struct ieee80211_scan_ssid ssids[])
    465  1.75.4.1      phil {
    466  1.75.4.1      phil 	int i;
    467  1.75.4.1      phil 
    468  1.75.4.1      phil 	for (i = 0; i < nssid; i++) {
    469  1.75.4.1      phil 		if (ni->ni_esslen == ssids[i].len &&
    470  1.75.4.1      phil 		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
    471  1.75.4.1      phil 			return 1;
    472  1.75.4.1      phil 	}
    473  1.75.4.1      phil 	return 0;
    474  1.75.4.1      phil }
    475      1.45     skrll 
    476  1.75.4.1      phil /*
    477  1.75.4.1      phil  * Test a node for suitability/compatibility.
    478  1.75.4.1      phil  */
    479      1.39    dyoung static int
    480  1.75.4.1      phil check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
    481  1.75.4.1      phil {
    482  1.75.4.1      phil 	struct ieee80211com *ic = ni->ni_ic;
    483  1.75.4.1      phil         uint8_t rate;
    484  1.75.4.1      phil 
    485  1.75.4.1      phil 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
    486  1.75.4.1      phil 		return 0;
    487  1.75.4.1      phil 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
    488  1.75.4.1      phil 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
    489  1.75.4.1      phil 			return 0;
    490  1.75.4.1      phil 	} else {
    491  1.75.4.1      phil 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
    492  1.75.4.1      phil 			return 0;
    493  1.75.4.1      phil 	}
    494  1.75.4.1      phil 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
    495  1.75.4.1      phil 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
    496  1.75.4.1      phil 			return 0;
    497  1.75.4.1      phil 	} else {
    498  1.75.4.1      phil 		/* XXX does this mean privacy is supported or required? */
    499  1.75.4.1      phil 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
    500  1.75.4.1      phil 			return 0;
    501  1.75.4.1      phil 	}
    502  1.75.4.1      phil 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
    503  1.75.4.1      phil 	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
    504  1.75.4.1      phil 	if (rate & IEEE80211_RATE_BASIC)
    505  1.75.4.1      phil 		return 0;
    506  1.75.4.1      phil 	if (vap->iv_des_nssid != 0 &&
    507  1.75.4.1      phil 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
    508  1.75.4.1      phil 		return 0;
    509  1.75.4.1      phil 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
    510  1.75.4.1      phil 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
    511  1.75.4.1      phil 		return 0;
    512  1.75.4.1      phil 	return 1;
    513  1.75.4.1      phil }
    514  1.75.4.1      phil 
    515  1.75.4.1      phil #ifdef IEEE80211_DEBUG
    516  1.75.4.1      phil /*
    517  1.75.4.1      phil  * Display node suitability/compatibility.
    518  1.75.4.1      phil  */
    519  1.75.4.1      phil static void
    520  1.75.4.1      phil check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
    521       1.5    dyoung {
    522  1.75.4.1      phil 	struct ieee80211com *ic = ni->ni_ic;
    523  1.75.4.1      phil         uint8_t rate;
    524  1.75.4.1      phil         int fail;
    525       1.5    dyoung 
    526       1.5    dyoung 	fail = 0;
    527       1.5    dyoung 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
    528       1.5    dyoung 		fail |= 0x01;
    529  1.75.4.1      phil 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
    530       1.5    dyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
    531       1.5    dyoung 			fail |= 0x02;
    532       1.5    dyoung 	} else {
    533       1.5    dyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
    534       1.5    dyoung 			fail |= 0x02;
    535       1.5    dyoung 	}
    536  1.75.4.1      phil 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
    537       1.5    dyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
    538       1.5    dyoung 			fail |= 0x04;
    539       1.5    dyoung 	} else {
    540      1.11    dyoung 		/* XXX does this mean privacy is supported or required? */
    541       1.5    dyoung 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
    542       1.5    dyoung 			fail |= 0x04;
    543       1.5    dyoung 	}
    544  1.75.4.1      phil 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
    545  1.75.4.1      phil 	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
    546       1.5    dyoung 	if (rate & IEEE80211_RATE_BASIC)
    547       1.5    dyoung 		fail |= 0x08;
    548  1.75.4.1      phil 	if (vap->iv_des_nssid != 0 &&
    549  1.75.4.1      phil 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
    550       1.5    dyoung 		fail |= 0x10;
    551  1.75.4.1      phil 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
    552  1.75.4.1      phil 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
    553       1.5    dyoung 		fail |= 0x20;
    554      1.75      maxv 
    555  1.75.4.1      phil 	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
    556  1.75.4.1      phil 	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
    557  1.75.4.1      phil 	printf(" %3d%c",
    558  1.75.4.1      phil 	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
    559  1.75.4.1      phil 	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
    560  1.75.4.1      phil 	    fail & 0x08 ? '!' : ' ');
    561  1.75.4.1      phil 	printf(" %4s%c",
    562  1.75.4.1      phil 	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
    563  1.75.4.1      phil 	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
    564  1.75.4.1      phil 	    "????",
    565  1.75.4.1      phil 	    fail & 0x02 ? '!' : ' ');
    566  1.75.4.1      phil 	printf(" %3s%c ",
    567  1.75.4.1      phil 	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
    568  1.75.4.1      phil 	    fail & 0x04 ? '!' : ' ');
    569  1.75.4.1      phil 	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
    570  1.75.4.1      phil 	printf("%s\n", fail & 0x10 ? "!" : "");
    571      1.39    dyoung }
    572  1.75.4.1      phil #endif /* IEEE80211_DEBUG */
    573  1.75.4.1      phil 
    574      1.39    dyoung 
    575  1.75.4.1      phil int
    576  1.75.4.1      phil ieee80211_ibss_merge_check(struct ieee80211_node *ni)
    577      1.39    dyoung {
    578  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    579      1.39    dyoung 
    580  1.75.4.1      phil 	if (ni == vap->iv_bss ||
    581  1.75.4.1      phil 	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
    582  1.75.4.1      phil 		/* unchanged, nothing to do */
    583  1.75.4.1      phil 		return 0;
    584  1.75.4.1      phil 	}
    585      1.39    dyoung 
    586  1.75.4.1      phil 	if (!check_bss(vap, ni)) {
    587  1.75.4.1      phil 		/* capabilities mismatch */
    588  1.75.4.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
    589  1.75.4.1      phil 		    "%s: merge failed, capabilities mismatch\n", __func__);
    590  1.75.4.1      phil #ifdef IEEE80211_DEBUG
    591  1.75.4.1      phil 		if (ieee80211_msg_assoc(vap))
    592  1.75.4.1      phil 			check_bss_debug(vap, ni);
    593  1.75.4.1      phil #endif
    594  1.75.4.1      phil 		vap->iv_stats.is_ibss_capmismatch++;
    595  1.75.4.1      phil 		return 0;
    596  1.75.4.1      phil 	}
    597      1.39    dyoung 
    598  1.75.4.1      phil 	return 1;
    599      1.39    dyoung }
    600      1.39    dyoung 
    601       1.1    dyoung /*
    602  1.75.4.1      phil  * Check if the given node should populate the node table.
    603  1.75.4.1      phil  *
    604  1.75.4.1      phil  * We need to be in "see all beacons for all ssids" mode in order
    605  1.75.4.1      phil  * to do IBSS merges, however this means we will populate nodes for
    606  1.75.4.1      phil  * /all/ IBSS SSIDs, versus just the one we care about.
    607  1.75.4.1      phil  *
    608  1.75.4.1      phil  * So this check ensures the node can actually belong to our IBSS
    609  1.75.4.1      phil  * configuration.  For now it simply checks the SSID.
    610       1.1    dyoung  */
    611  1.75.4.1      phil int
    612  1.75.4.1      phil ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
    613  1.75.4.1      phil     const struct ieee80211_scanparams *scan)
    614       1.1    dyoung {
    615  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    616  1.75.4.1      phil 	int i;
    617      1.39    dyoung 
    618      1.39    dyoung 	/*
    619  1.75.4.1      phil 	 * If we have no SSID and no scan SSID, return OK.
    620      1.39    dyoung 	 */
    621  1.75.4.1      phil 	if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
    622  1.75.4.1      phil 		goto ok;
    623      1.75      maxv 
    624      1.39    dyoung 	/*
    625  1.75.4.1      phil 	 * If we have one of (SSID, scan SSID) then return error.
    626      1.39    dyoung 	 */
    627  1.75.4.1      phil 	if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
    628  1.75.4.1      phil 		goto mismatch;
    629      1.75      maxv 
    630  1.75.4.1      phil 	/*
    631  1.75.4.1      phil 	 * Double-check - we need scan SSID.
    632  1.75.4.1      phil 	 */
    633  1.75.4.1      phil 	if (scan->ssid == NULL)
    634  1.75.4.1      phil 		goto mismatch;
    635      1.75      maxv 
    636  1.75.4.1      phil 	/*
    637  1.75.4.1      phil 	 * Check if the scan SSID matches the SSID list for the VAP.
    638  1.75.4.1      phil 	 */
    639  1.75.4.1      phil 	for (i = 0; i < vap->iv_des_nssid; i++) {
    640      1.75      maxv 
    641  1.75.4.1      phil 		/* Sanity length check */
    642  1.75.4.1      phil 		if (vap->iv_des_ssid[i].len != scan->ssid[1])
    643  1.75.4.1      phil 			continue;
    644      1.75      maxv 
    645  1.75.4.1      phil 		/* Note: SSID in the scan entry is the IE format */
    646  1.75.4.1      phil 		if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
    647  1.75.4.1      phil 		    vap->iv_des_ssid[i].len) == 0)
    648  1.75.4.1      phil 			goto ok;
    649       1.1    dyoung 	}
    650      1.75      maxv 
    651  1.75.4.1      phil mismatch:
    652  1.75.4.1      phil 	return (0);
    653  1.75.4.1      phil ok:
    654  1.75.4.1      phil 	return (1);
    655      1.39    dyoung }
    656      1.65  christos 
    657      1.39    dyoung /*
    658      1.39    dyoung  * Handle 802.11 ad hoc network merge.  The
    659      1.39    dyoung  * convention, set by the Wireless Ethernet Compatibility Alliance
    660      1.39    dyoung  * (WECA), is that an 802.11 station will change its BSSID to match
    661      1.39    dyoung  * the "oldest" 802.11 ad hoc network, on the same channel, that
    662      1.39    dyoung  * has the station's desired SSID.  The "oldest" 802.11 network
    663      1.39    dyoung  * sends beacons with the greatest TSF timestamp.
    664      1.39    dyoung  *
    665      1.39    dyoung  * The caller is assumed to validate TSF's before attempting a merge.
    666      1.39    dyoung  *
    667      1.39    dyoung  * Return !0 if the BSSID changed, 0 otherwise.
    668      1.39    dyoung  */
    669      1.39    dyoung int
    670      1.45     skrll ieee80211_ibss_merge(struct ieee80211_node *ni)
    671      1.39    dyoung {
    672  1.75.4.1      phil #ifdef IEEE80211_DEBUG
    673  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    674      1.45     skrll 	struct ieee80211com *ic = ni->ni_ic;
    675  1.75.4.1      phil #endif
    676      1.39    dyoung 
    677  1.75.4.1      phil 	if (! ieee80211_ibss_merge_check(ni))
    678      1.39    dyoung 		return 0;
    679  1.75.4.1      phil 
    680  1.75.4.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
    681      1.39    dyoung 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
    682      1.39    dyoung 		ether_sprintf(ni->ni_bssid),
    683      1.39    dyoung 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
    684      1.39    dyoung 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
    685      1.39    dyoung 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
    686      1.39    dyoung 	);
    687  1.75.4.1      phil 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
    688      1.39    dyoung }
    689      1.39    dyoung 
    690      1.39    dyoung /*
    691  1.75.4.1      phil  * Calculate HT channel promotion flags for all vaps.
    692  1.75.4.1      phil  * This assumes ni_chan have been setup for each vap.
    693      1.39    dyoung  */
    694  1.75.4.1      phil static int
    695  1.75.4.1      phil gethtadjustflags(struct ieee80211com *ic)
    696      1.39    dyoung {
    697  1.75.4.1      phil 	struct ieee80211vap *vap;
    698  1.75.4.1      phil 	int flags;
    699      1.39    dyoung 
    700  1.75.4.1      phil 	flags = 0;
    701  1.75.4.1      phil 	/* XXX locking */
    702  1.75.4.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
    703  1.75.4.1      phil 		if (vap->iv_state < IEEE80211_S_RUN)
    704  1.75.4.1      phil 			continue;
    705  1.75.4.1      phil 		switch (vap->iv_opmode) {
    706  1.75.4.1      phil 		case IEEE80211_M_WDS:
    707  1.75.4.1      phil 		case IEEE80211_M_STA:
    708  1.75.4.1      phil 		case IEEE80211_M_AHDEMO:
    709  1.75.4.1      phil 		case IEEE80211_M_HOSTAP:
    710  1.75.4.1      phil 		case IEEE80211_M_IBSS:
    711  1.75.4.1      phil 		case IEEE80211_M_MBSS:
    712  1.75.4.1      phil 			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
    713  1.75.4.1      phil 			break;
    714  1.75.4.1      phil 		default:
    715  1.75.4.1      phil 			break;
    716  1.75.4.1      phil 		}
    717  1.75.4.1      phil 	}
    718  1.75.4.1      phil 	return flags;
    719  1.75.4.1      phil }
    720      1.75      maxv 
    721  1.75.4.1      phil /*
    722  1.75.4.1      phil  * Calculate VHT channel promotion flags for all vaps.
    723  1.75.4.1      phil  * This assumes ni_chan have been setup for each vap.
    724  1.75.4.1      phil  */
    725  1.75.4.1      phil static int
    726  1.75.4.1      phil getvhtadjustflags(struct ieee80211com *ic)
    727  1.75.4.1      phil {
    728  1.75.4.1      phil 	struct ieee80211vap *vap;
    729  1.75.4.1      phil 	int flags;
    730  1.75.4.1      phil 
    731  1.75.4.1      phil 	flags = 0;
    732  1.75.4.1      phil 	/* XXX locking */
    733  1.75.4.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
    734  1.75.4.1      phil 		if (vap->iv_state < IEEE80211_S_RUN)
    735  1.75.4.1      phil 			continue;
    736  1.75.4.1      phil 		switch (vap->iv_opmode) {
    737  1.75.4.1      phil 		case IEEE80211_M_WDS:
    738  1.75.4.1      phil 		case IEEE80211_M_STA:
    739  1.75.4.1      phil 		case IEEE80211_M_AHDEMO:
    740  1.75.4.1      phil 		case IEEE80211_M_HOSTAP:
    741  1.75.4.1      phil 		case IEEE80211_M_IBSS:
    742  1.75.4.1      phil 		case IEEE80211_M_MBSS:
    743  1.75.4.1      phil 			flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan);
    744  1.75.4.1      phil 			break;
    745  1.75.4.1      phil 		default:
    746  1.75.4.1      phil 			break;
    747  1.75.4.1      phil 		}
    748  1.75.4.1      phil 	}
    749  1.75.4.1      phil 	return flags;
    750  1.75.4.1      phil }
    751  1.75.4.1      phil 
    752  1.75.4.1      phil /*
    753  1.75.4.1      phil  * Check if the current channel needs to change based on whether
    754  1.75.4.1      phil  * any vap's are using HT20/HT40.  This is used to sync the state
    755  1.75.4.1      phil  * of ic_curchan after a channel width change on a running vap.
    756  1.75.4.1      phil  *
    757  1.75.4.1      phil  * Same applies for VHT.
    758  1.75.4.1      phil  */
    759  1.75.4.1      phil void
    760  1.75.4.1      phil ieee80211_sync_curchan(struct ieee80211com *ic)
    761  1.75.4.1      phil {
    762  1.75.4.1      phil 	struct ieee80211_channel *c;
    763  1.75.4.1      phil 
    764  1.75.4.1      phil 	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
    765  1.75.4.1      phil 	c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
    766      1.75      maxv 
    767  1.75.4.1      phil 	if (c != ic->ic_curchan) {
    768  1.75.4.1      phil 		ic->ic_curchan = c;
    769  1.75.4.1      phil 		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
    770  1.75.4.1      phil 		ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
    771  1.75.4.1      phil 		IEEE80211_UNLOCK(ic);
    772  1.75.4.1      phil 		ic->ic_set_channel(ic);
    773  1.75.4.1      phil 		ieee80211_radiotap_chan_change(ic);
    774  1.75.4.1      phil 		IEEE80211_LOCK(ic);
    775  1.75.4.1      phil 	}
    776  1.75.4.1      phil }
    777  1.75.4.1      phil 
    778  1.75.4.1      phil /*
    779  1.75.4.1      phil  * Setup the current channel.  The request channel may be
    780  1.75.4.1      phil  * promoted if other vap's are operating with HT20/HT40.
    781  1.75.4.1      phil  */
    782  1.75.4.1      phil void
    783  1.75.4.1      phil ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
    784  1.75.4.1      phil {
    785  1.75.4.1      phil 	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
    786  1.75.4.1      phil 		int flags = gethtadjustflags(ic);
    787      1.39    dyoung 		/*
    788  1.75.4.1      phil 		 * Check for channel promotion required to support the
    789  1.75.4.1      phil 		 * set of running vap's.  This assumes we are called
    790  1.75.4.1      phil 		 * after ni_chan is setup for each vap.
    791      1.39    dyoung 		 */
    792  1.75.4.1      phil 		/* XXX VHT? */
    793  1.75.4.1      phil 		/* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
    794  1.75.4.1      phil 		if (flags > ieee80211_htchanflags(c))
    795  1.75.4.1      phil 			c = ieee80211_ht_adjust_channel(ic, c, flags);
    796      1.39    dyoung 	}
    797      1.39    dyoung 
    798      1.39    dyoung 	/*
    799  1.75.4.1      phil 	 * VHT promotion - this will at least promote to VHT20/40
    800  1.75.4.1      phil 	 * based on what HT has done; it may further promote the
    801  1.75.4.1      phil 	 * channel to VHT80 or above.
    802  1.75.4.1      phil 	 */
    803  1.75.4.1      phil 	if (ic->ic_vhtcaps != 0) {
    804  1.75.4.1      phil 		int flags = getvhtadjustflags(ic);
    805  1.75.4.1      phil 		if (flags > ieee80211_vhtchanflags(c))
    806  1.75.4.1      phil 			c = ieee80211_vht_adjust_channel(ic, c, flags);
    807  1.75.4.1      phil 	}
    808  1.75.4.1      phil 
    809  1.75.4.1      phil 	ic->ic_bsschan = ic->ic_curchan = c;
    810  1.75.4.1      phil 	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
    811  1.75.4.1      phil 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
    812  1.75.4.1      phil }
    813  1.75.4.1      phil 
    814  1.75.4.1      phil /*
    815  1.75.4.1      phil  * Change the current channel.  The channel change is guaranteed to have
    816  1.75.4.1      phil  * happened before the next state change.
    817  1.75.4.1      phil  */
    818  1.75.4.1      phil void
    819  1.75.4.1      phil ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
    820  1.75.4.1      phil {
    821  1.75.4.1      phil 	ieee80211_setupcurchan(ic, c);
    822  1.75.4.1      phil 	ieee80211_runtask(ic, &ic->ic_chan_task);
    823  1.75.4.1      phil }
    824  1.75.4.1      phil 
    825  1.75.4.1      phil void
    826  1.75.4.1      phil ieee80211_update_chw(struct ieee80211com *ic)
    827  1.75.4.1      phil {
    828  1.75.4.1      phil 
    829  1.75.4.1      phil 	ieee80211_setupcurchan(ic, ic->ic_curchan);
    830  1.75.4.1      phil 	ieee80211_runtask(ic, &ic->ic_chw_task);
    831  1.75.4.1      phil }
    832  1.75.4.1      phil 
    833  1.75.4.1      phil /*
    834  1.75.4.1      phil  * Join the specified IBSS/BSS network.  The node is assumed to
    835  1.75.4.1      phil  * be passed in with a held reference.
    836  1.75.4.1      phil  */
    837  1.75.4.1      phil static int
    838  1.75.4.1      phil ieee80211_sta_join1(struct ieee80211_node *selbs)
    839  1.75.4.1      phil {
    840  1.75.4.1      phil 	struct ieee80211vap *vap = selbs->ni_vap;
    841  1.75.4.1      phil 	struct ieee80211com *ic = selbs->ni_ic;
    842  1.75.4.1      phil 	struct ieee80211_node *obss;
    843  1.75.4.1      phil 	int canreassoc;
    844  1.75.4.1      phil 
    845  1.75.4.1      phil 	/*
    846      1.39    dyoung 	 * Committed to selbs, setup state.
    847      1.39    dyoung 	 */
    848  1.75.4.1      phil 	obss = vap->iv_bss;
    849  1.75.4.1      phil 	/*
    850  1.75.4.1      phil 	 * Check if old+new node have the same address in which
    851  1.75.4.1      phil 	 * case we can reassociate when operating in sta mode.
    852  1.75.4.1      phil 	 */
    853  1.75.4.1      phil 	canreassoc = (obss != NULL &&
    854  1.75.4.1      phil 		vap->iv_state == IEEE80211_S_RUN &&
    855  1.75.4.1      phil 		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
    856  1.75.4.1      phil 	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
    857      1.52    dyoung 	if (obss != NULL) {
    858  1.75.4.1      phil 		struct ieee80211_node_table *nt = obss->ni_table;
    859  1.75.4.1      phil 
    860      1.52    dyoung 		copy_bss(selbs, obss);
    861  1.75.4.1      phil 		ieee80211_node_decref(obss);	/* iv_bss reference */
    862  1.75.4.1      phil 
    863  1.75.4.1      phil 		IEEE80211_NODE_LOCK(nt);
    864  1.75.4.1      phil 		node_reclaim(nt, obss);		/* station table reference */
    865  1.75.4.1      phil 		IEEE80211_NODE_UNLOCK(nt);
    866  1.75.4.1      phil 
    867  1.75.4.1      phil 		obss = NULL;		/* NB: guard against later use */
    868      1.52    dyoung 	}
    869      1.75      maxv 
    870      1.39    dyoung 	/*
    871  1.75.4.1      phil 	 * Delete unusable rates; we've already checked
    872  1.75.4.1      phil 	 * that the negotiated rate set is acceptable.
    873  1.75.4.1      phil 	 */
    874  1.75.4.1      phil 	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
    875  1.75.4.1      phil 		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
    876  1.75.4.1      phil 
    877  1.75.4.1      phil 	ieee80211_setcurchan(ic, selbs->ni_chan);
    878  1.75.4.1      phil 	/*
    879      1.39    dyoung 	 * Set the erp state (mostly the slot time) to deal with
    880      1.39    dyoung 	 * the auto-select case; this should be redundant if the
    881      1.39    dyoung 	 * mode is locked.
    882  1.75.4.1      phil 	 */
    883      1.39    dyoung 	ieee80211_reset_erp(ic);
    884  1.75.4.1      phil 	ieee80211_wme_initparams(vap);
    885      1.39    dyoung 
    886  1.75.4.1      phil 	if (vap->iv_opmode == IEEE80211_M_STA) {
    887  1.75.4.1      phil 		if (canreassoc) {
    888  1.75.4.1      phil 			/* Reassociate */
    889  1.75.4.1      phil 			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
    890  1.75.4.1      phil 		} else {
    891  1.75.4.1      phil 			/*
    892  1.75.4.1      phil 			 * Act as if we received a DEAUTH frame in case we
    893  1.75.4.1      phil 			 * are invoked from the RUN state.  This will cause
    894  1.75.4.1      phil 			 * us to try to re-authenticate if we are operating
    895  1.75.4.1      phil 			 * as a station.
    896  1.75.4.1      phil 			 */
    897  1.75.4.1      phil 			ieee80211_new_state(vap, IEEE80211_S_AUTH,
    898  1.75.4.1      phil 				IEEE80211_FC0_SUBTYPE_DEAUTH);
    899  1.75.4.1      phil 		}
    900  1.75.4.1      phil 	} else
    901  1.75.4.1      phil 		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
    902      1.39    dyoung 	return 1;
    903      1.39    dyoung }
    904      1.39    dyoung 
    905  1.75.4.1      phil int
    906  1.75.4.1      phil ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
    907  1.75.4.1      phil 	const struct ieee80211_scan_entry *se)
    908  1.75.4.1      phil {
    909  1.75.4.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    910  1.75.4.1      phil 	struct ieee80211_node *ni;
    911  1.75.4.1      phil 	int do_ht = 0;
    912  1.75.4.1      phil 
    913  1.75.4.1      phil 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
    914  1.75.4.1      phil 	if (ni == NULL) {
    915  1.75.4.1      phil 		/* XXX msg */
    916  1.75.4.1      phil 		return 0;
    917  1.75.4.1      phil 	}
    918  1.75.4.1      phil 
    919  1.75.4.1      phil 	/*
    920  1.75.4.1      phil 	 * Expand scan state into node's format.
    921  1.75.4.1      phil 	 * XXX may not need all this stuff
    922  1.75.4.1      phil 	 */
    923  1.75.4.1      phil 	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
    924  1.75.4.1      phil 	ni->ni_esslen = se->se_ssid[1];
    925  1.75.4.1      phil 	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
    926  1.75.4.1      phil 	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
    927  1.75.4.1      phil 	ni->ni_intval = se->se_intval;
    928  1.75.4.1      phil 	ni->ni_capinfo = se->se_capinfo;
    929  1.75.4.1      phil 	ni->ni_chan = chan;
    930  1.75.4.1      phil 	ni->ni_timoff = se->se_timoff;
    931  1.75.4.1      phil 	ni->ni_fhdwell = se->se_fhdwell;
    932  1.75.4.1      phil 	ni->ni_fhindex = se->se_fhindex;
    933  1.75.4.1      phil 	ni->ni_erp = se->se_erp;
    934  1.75.4.1      phil 	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
    935  1.75.4.1      phil 	ni->ni_noise = se->se_noise;
    936  1.75.4.1      phil 	if (vap->iv_opmode == IEEE80211_M_STA) {
    937  1.75.4.1      phil 		/* NB: only infrastructure mode requires an associd */
    938  1.75.4.1      phil 		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
    939  1.75.4.1      phil 	}
    940  1.75.4.1      phil 
    941  1.75.4.1      phil 	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
    942  1.75.4.1      phil 		ieee80211_ies_expand(&ni->ni_ies);
    943  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
    944  1.75.4.1      phil 		if (ni->ni_ies.ath_ie != NULL)
    945  1.75.4.1      phil 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
    946  1.75.4.1      phil #endif
    947  1.75.4.1      phil 		if (ni->ni_ies.htcap_ie != NULL)
    948  1.75.4.1      phil 			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
    949  1.75.4.1      phil 		if (ni->ni_ies.htinfo_ie != NULL)
    950  1.75.4.1      phil 			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
    951  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_MESH
    952  1.75.4.1      phil 		if (ni->ni_ies.meshid_ie != NULL)
    953  1.75.4.1      phil 			ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
    954  1.75.4.1      phil #endif
    955  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_TDMA
    956  1.75.4.1      phil 		if (ni->ni_ies.tdma_ie != NULL)
    957  1.75.4.1      phil 			ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
    958  1.75.4.1      phil #endif
    959  1.75.4.1      phil 		if (ni->ni_ies.vhtcap_ie != NULL)
    960  1.75.4.1      phil 			ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
    961  1.75.4.1      phil 		if (ni->ni_ies.vhtopmode_ie != NULL)
    962  1.75.4.1      phil 			ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
    963  1.75.4.1      phil 
    964  1.75.4.1      phil 		/* XXX parse BSSLOAD IE */
    965  1.75.4.1      phil 		/* XXX parse TXPWRENV IE */
    966  1.75.4.1      phil 		/* XXX parse APCHANREP IE */
    967  1.75.4.1      phil 	}
    968  1.75.4.1      phil 
    969  1.75.4.1      phil 	vap->iv_dtim_period = se->se_dtimperiod;
    970  1.75.4.1      phil 	vap->iv_dtim_count = 0;
    971  1.75.4.1      phil 
    972  1.75.4.1      phil 	/* NB: must be after ni_chan is setup */
    973  1.75.4.1      phil 	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
    974  1.75.4.1      phil 		IEEE80211_F_DOSORT);
    975  1.75.4.1      phil 	if (ieee80211_iserp_rateset(&ni->ni_rates))
    976  1.75.4.1      phil 		ni->ni_flags |= IEEE80211_NODE_ERP;
    977  1.75.4.1      phil 
    978  1.75.4.1      phil 	/*
    979  1.75.4.1      phil 	 * Setup HT state for this node if it's available, otherwise
    980  1.75.4.1      phil 	 * non-STA modes won't pick this state up.
    981  1.75.4.1      phil 	 *
    982  1.75.4.1      phil 	 * For IBSS and related modes that don't go through an
    983  1.75.4.1      phil 	 * association request/response, the only appropriate place
    984  1.75.4.1      phil 	 * to setup the HT state is here.
    985  1.75.4.1      phil 	 */
    986  1.75.4.1      phil 	if (ni->ni_ies.htinfo_ie != NULL &&
    987  1.75.4.1      phil 	    ni->ni_ies.htcap_ie != NULL &&
    988  1.75.4.1      phil 	    vap->iv_flags_ht & IEEE80211_FHT_HT) {
    989  1.75.4.1      phil 		ieee80211_ht_node_init(ni);
    990  1.75.4.1      phil 		ieee80211_ht_updateparams(ni,
    991  1.75.4.1      phil 		    ni->ni_ies.htcap_ie,
    992  1.75.4.1      phil 		    ni->ni_ies.htinfo_ie);
    993  1.75.4.1      phil 		do_ht = 1;
    994  1.75.4.1      phil 	}
    995  1.75.4.1      phil 
    996  1.75.4.1      phil 	/*
    997  1.75.4.1      phil 	 * Setup VHT state for this node if it's available.
    998  1.75.4.1      phil 	 * Same as the above.
    999  1.75.4.1      phil 	 *
   1000  1.75.4.1      phil 	 * For now, don't allow 2GHz VHT operation.
   1001  1.75.4.1      phil 	 */
   1002  1.75.4.1      phil 	if (ni->ni_ies.vhtopmode_ie != NULL &&
   1003  1.75.4.1      phil 	    ni->ni_ies.vhtcap_ie != NULL &&
   1004  1.75.4.1      phil 	    vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
   1005  1.75.4.1      phil 		if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
   1006  1.75.4.1      phil 			printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
   1007  1.75.4.1      phil 			    __func__,
   1008  1.75.4.1      phil 			    ni->ni_macaddr,
   1009  1.75.4.1      phil 			    ":");
   1010  1.75.4.1      phil 		} else {
   1011  1.75.4.1      phil 			ieee80211_vht_node_init(ni);
   1012  1.75.4.1      phil 			ieee80211_vht_updateparams(ni,
   1013  1.75.4.1      phil 			    ni->ni_ies.vhtcap_ie,
   1014  1.75.4.1      phil 			    ni->ni_ies.vhtopmode_ie);
   1015  1.75.4.1      phil 			ieee80211_setup_vht_rates(ni, ni->ni_ies.vhtcap_ie,
   1016  1.75.4.1      phil 			    ni->ni_ies.vhtopmode_ie);
   1017  1.75.4.1      phil 			do_ht = 1;
   1018  1.75.4.1      phil 		}
   1019  1.75.4.1      phil 	}
   1020  1.75.4.1      phil 
   1021  1.75.4.1      phil 	/* Finally do the node channel change */
   1022  1.75.4.1      phil 	if (do_ht) {
   1023  1.75.4.1      phil 		ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
   1024  1.75.4.1      phil 		    ni->ni_ies.htinfo_ie);
   1025  1.75.4.1      phil 		ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
   1026  1.75.4.1      phil 		    IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
   1027  1.75.4.1      phil 		ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
   1028  1.75.4.1      phil 	}
   1029  1.75.4.1      phil 
   1030  1.75.4.1      phil 	/* XXX else check for ath FF? */
   1031  1.75.4.1      phil 	/* XXX QoS? Difficult given that WME config is specific to a master */
   1032  1.75.4.1      phil 
   1033  1.75.4.1      phil 	ieee80211_node_setuptxparms(ni);
   1034  1.75.4.1      phil 	ieee80211_ratectl_node_init(ni);
   1035  1.75.4.1      phil 
   1036  1.75.4.1      phil 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
   1037  1.75.4.1      phil }
   1038  1.75.4.1      phil 
   1039      1.39    dyoung /*
   1040      1.39    dyoung  * Leave the specified IBSS/BSS network.  The node is assumed to
   1041      1.39    dyoung  * be passed in with a held reference.
   1042      1.39    dyoung  */
   1043      1.39    dyoung void
   1044  1.75.4.1      phil ieee80211_sta_leave(struct ieee80211_node *ni)
   1045      1.39    dyoung {
   1046  1.75.4.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   1047  1.75.4.1      phil 
   1048      1.39    dyoung 	ic->ic_node_cleanup(ni);
   1049  1.75.4.1      phil 	ieee80211_notify_node_leave(ni);
   1050       1.1    dyoung }
   1051       1.1    dyoung 
   1052  1.75.4.1      phil /*
   1053  1.75.4.1      phil  * Send a deauthenticate frame and drop the station.
   1054  1.75.4.1      phil  */
   1055  1.75.4.1      phil void
   1056  1.75.4.1      phil ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
   1057       1.5    dyoung {
   1058  1.75.4.1      phil 	/* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
   1059  1.75.4.1      phil 	ieee80211_ref_node(ni);
   1060  1.75.4.1      phil 	if (ni->ni_associd != 0)
   1061  1.75.4.1      phil 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
   1062  1.75.4.1      phil 	ieee80211_node_leave(ni);
   1063  1.75.4.1      phil 	ieee80211_free_node(ni);
   1064  1.75.4.1      phil }
   1065       1.5    dyoung 
   1066  1.75.4.1      phil static struct ieee80211_node *
   1067  1.75.4.1      phil node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1068  1.75.4.1      phil {
   1069  1.75.4.1      phil 	struct ieee80211_node *ni;
   1070  1.75.4.1      phil 
   1071  1.75.4.1      phil 	ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
   1072  1.75.4.1      phil 		M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   1073  1.75.4.1      phil 	return ni;
   1074  1.75.4.1      phil }
   1075  1.75.4.1      phil 
   1076  1.75.4.1      phil /*
   1077  1.75.4.1      phil  * Initialize an ie blob with the specified data.  If previous
   1078  1.75.4.1      phil  * data exists re-use the data block.  As a side effect we clear
   1079  1.75.4.1      phil  * all references to specific ie's; the caller is required to
   1080  1.75.4.1      phil  * recalculate them.
   1081  1.75.4.1      phil  */
   1082  1.75.4.1      phil int
   1083  1.75.4.1      phil ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
   1084  1.75.4.1      phil {
   1085  1.75.4.1      phil 	/* NB: assumes data+len are the last fields */
   1086  1.75.4.1      phil 	memset(ies, 0, offsetof(struct ieee80211_ies, data));
   1087  1.75.4.1      phil 	if (ies->data != NULL && ies->len != len) {
   1088  1.75.4.1      phil 		/* data size changed */
   1089  1.75.4.1      phil 		IEEE80211_FREE(ies->data, M_80211_NODE_IE);
   1090  1.75.4.1      phil 		ies->data = NULL;
   1091  1.75.4.1      phil 	}
   1092  1.75.4.1      phil 	if (ies->data == NULL) {
   1093  1.75.4.1      phil 		ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
   1094  1.75.4.1      phil 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   1095  1.75.4.1      phil 		if (ies->data == NULL) {
   1096  1.75.4.1      phil 			ies->len = 0;
   1097  1.75.4.1      phil 			/* NB: pointers have already been zero'd above */
   1098  1.75.4.1      phil 			return 0;
   1099      1.55    dyoung 		}
   1100      1.55    dyoung 	}
   1101  1.75.4.1      phil 	memcpy(ies->data, data, len);
   1102  1.75.4.1      phil 	ies->len = len;
   1103  1.75.4.1      phil 	return 1;
   1104  1.75.4.1      phil }
   1105      1.75      maxv 
   1106  1.75.4.1      phil /*
   1107  1.75.4.1      phil  * Reclaim storage for an ie blob.
   1108  1.75.4.1      phil  */
   1109  1.75.4.1      phil void
   1110  1.75.4.1      phil ieee80211_ies_cleanup(struct ieee80211_ies *ies)
   1111  1.75.4.1      phil {
   1112  1.75.4.1      phil 	if (ies->data != NULL)
   1113  1.75.4.1      phil 		IEEE80211_FREE(ies->data, M_80211_NODE_IE);
   1114       1.5    dyoung }
   1115       1.5    dyoung 
   1116  1.75.4.1      phil /*
   1117  1.75.4.1      phil  * Expand an ie blob data contents and to fillin individual
   1118  1.75.4.1      phil  * ie pointers.  The data blob is assumed to be well-formed;
   1119  1.75.4.1      phil  * we don't do any validity checking of ie lengths.
   1120  1.75.4.1      phil  */
   1121  1.75.4.1      phil void
   1122  1.75.4.1      phil ieee80211_ies_expand(struct ieee80211_ies *ies)
   1123       1.1    dyoung {
   1124  1.75.4.1      phil 	uint8_t *ie;
   1125  1.75.4.1      phil 	int ielen;
   1126      1.39    dyoung 
   1127  1.75.4.1      phil 	ie = ies->data;
   1128  1.75.4.1      phil 	ielen = ies->len;
   1129  1.75.4.1      phil 	while (ielen > 0) {
   1130  1.75.4.1      phil 		switch (ie[0]) {
   1131  1.75.4.1      phil 		case IEEE80211_ELEMID_VENDOR:
   1132  1.75.4.1      phil 			if (iswpaoui(ie))
   1133  1.75.4.1      phil 				ies->wpa_ie = ie;
   1134  1.75.4.1      phil 			else if (iswmeoui(ie))
   1135  1.75.4.1      phil 				ies->wme_ie = ie;
   1136  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   1137  1.75.4.1      phil 			else if (isatherosoui(ie))
   1138  1.75.4.1      phil 				ies->ath_ie = ie;
   1139  1.75.4.1      phil #endif
   1140  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_TDMA
   1141  1.75.4.1      phil 			else if (istdmaoui(ie))
   1142  1.75.4.1      phil 				ies->tdma_ie = ie;
   1143  1.75.4.1      phil #endif
   1144  1.75.4.1      phil 			break;
   1145  1.75.4.1      phil 		case IEEE80211_ELEMID_RSN:
   1146  1.75.4.1      phil 			ies->rsn_ie = ie;
   1147  1.75.4.1      phil 			break;
   1148  1.75.4.1      phil 		case IEEE80211_ELEMID_HTCAP:
   1149  1.75.4.1      phil 			ies->htcap_ie = ie;
   1150  1.75.4.1      phil 			break;
   1151  1.75.4.1      phil 		case IEEE80211_ELEMID_HTINFO:
   1152  1.75.4.1      phil 			ies->htinfo_ie = ie;
   1153  1.75.4.1      phil 			break;
   1154  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_MESH
   1155  1.75.4.1      phil 		case IEEE80211_ELEMID_MESHID:
   1156  1.75.4.1      phil 			ies->meshid_ie = ie;
   1157  1.75.4.1      phil 			break;
   1158  1.75.4.1      phil #endif
   1159  1.75.4.1      phil 		case IEEE80211_ELEMID_VHT_CAP:
   1160  1.75.4.1      phil 			ies->vhtcap_ie = ie;
   1161  1.75.4.1      phil 			break;
   1162  1.75.4.1      phil 		case IEEE80211_ELEMID_VHT_OPMODE:
   1163  1.75.4.1      phil 			ies->vhtopmode_ie = ie;
   1164  1.75.4.1      phil 			break;
   1165  1.75.4.1      phil 		case IEEE80211_ELEMID_VHT_PWR_ENV:
   1166  1.75.4.1      phil 			ies->vhtpwrenv_ie = ie;
   1167  1.75.4.1      phil 			break;
   1168  1.75.4.1      phil 		case IEEE80211_ELEMID_BSSLOAD:
   1169  1.75.4.1      phil 			ies->bssload_ie = ie;
   1170  1.75.4.1      phil 			break;
   1171  1.75.4.1      phil 		case IEEE80211_ELEMID_APCHANREP:
   1172  1.75.4.1      phil 			ies->apchanrep_ie = ie;
   1173  1.75.4.1      phil 			break;
   1174  1.75.4.1      phil 		}
   1175  1.75.4.1      phil 		ielen -= 2 + ie[1];
   1176  1.75.4.1      phil 		ie += 2 + ie[1];
   1177  1.75.4.1      phil 	}
   1178       1.1    dyoung }
   1179       1.1    dyoung 
   1180      1.39    dyoung /*
   1181      1.39    dyoung  * Reclaim any resources in a node and reset any critical
   1182      1.39    dyoung  * state.  Typically nodes are free'd immediately after,
   1183      1.39    dyoung  * but in some cases the storage may be reused so we need
   1184      1.39    dyoung  * to insure consistent state (should probably fix that).
   1185      1.39    dyoung  */
   1186       1.1    dyoung static void
   1187      1.39    dyoung node_cleanup(struct ieee80211_node *ni)
   1188      1.22   mycroft {
   1189  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   1190      1.39    dyoung 	struct ieee80211com *ic = ni->ni_ic;
   1191  1.75.4.1      phil 	int i;
   1192      1.39    dyoung 
   1193      1.39    dyoung 	/* NB: preserve ni_table */
   1194      1.39    dyoung 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
   1195  1.75.4.1      phil 		if (vap->iv_opmode != IEEE80211_M_STA)
   1196  1.75.4.1      phil 			vap->iv_ps_sta--;
   1197      1.39    dyoung 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
   1198  1.75.4.1      phil 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
   1199  1.75.4.1      phil 		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
   1200      1.39    dyoung 	}
   1201  1.75.4.1      phil 	/*
   1202  1.75.4.1      phil 	 * Cleanup any VHT and HT-related state.
   1203  1.75.4.1      phil 	 */
   1204  1.75.4.1      phil 	if (ni->ni_flags & IEEE80211_NODE_VHT)
   1205  1.75.4.1      phil 		ieee80211_vht_node_cleanup(ni);
   1206  1.75.4.1      phil 	if (ni->ni_flags & IEEE80211_NODE_HT)
   1207  1.75.4.1      phil 		ieee80211_ht_node_cleanup(ni);
   1208  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   1209  1.75.4.1      phil 	/* Always do FF node cleanup; for A-MSDU */
   1210  1.75.4.1      phil 	ieee80211_ff_node_cleanup(ni);
   1211  1.75.4.1      phil #endif
   1212  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_MESH
   1213  1.75.4.1      phil 	/*
   1214  1.75.4.1      phil 	 * Cleanup any mesh-related state.
   1215  1.75.4.1      phil 	 */
   1216  1.75.4.1      phil 	if (vap->iv_opmode == IEEE80211_M_MBSS)
   1217  1.75.4.1      phil 		ieee80211_mesh_node_cleanup(ni);
   1218  1.75.4.1      phil #endif
   1219  1.75.4.1      phil 	/*
   1220  1.75.4.1      phil 	 * Clear any staging queue entries.
   1221  1.75.4.1      phil 	 */
   1222  1.75.4.1      phil 	ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
   1223      1.75      maxv 
   1224      1.42    dyoung 	/*
   1225      1.42    dyoung 	 * Clear AREF flag that marks the authorization refcnt bump
   1226      1.42    dyoung 	 * has happened.  This is probably not needed as the node
   1227      1.42    dyoung 	 * should always be removed from the table so not found but
   1228      1.42    dyoung 	 * do it just in case.
   1229  1.75.4.1      phil 	 * Likewise clear the ASSOCID flag as these flags are intended
   1230  1.75.4.1      phil 	 * to be managed in tandem.
   1231      1.42    dyoung 	 */
   1232  1.75.4.1      phil 	ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
   1233      1.39    dyoung 
   1234      1.39    dyoung 	/*
   1235      1.39    dyoung 	 * Drain power save queue and, if needed, clear TIM.
   1236      1.39    dyoung 	 */
   1237  1.75.4.1      phil 	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
   1238  1.75.4.1      phil 		vap->iv_set_tim(ni, 0);
   1239      1.39    dyoung 
   1240      1.39    dyoung 	ni->ni_associd = 0;
   1241      1.39    dyoung 	if (ni->ni_challenge != NULL) {
   1242  1.75.4.1      phil 		IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
   1243      1.39    dyoung 		ni->ni_challenge = NULL;
   1244      1.39    dyoung 	}
   1245      1.39    dyoung 	/*
   1246      1.39    dyoung 	 * Preserve SSID, WPA, and WME ie's so the bss node is
   1247      1.39    dyoung 	 * reusable during a re-auth/re-assoc state transition.
   1248      1.39    dyoung 	 * If we remove these data they will not be recreated
   1249      1.39    dyoung 	 * because they come from a probe-response or beacon frame
   1250      1.39    dyoung 	 * which cannot be expected prior to the association-response.
   1251      1.39    dyoung 	 * This should not be an issue when operating in other modes
   1252      1.39    dyoung 	 * as stations leaving always go through a full state transition
   1253      1.39    dyoung 	 * which will rebuild this state.
   1254      1.39    dyoung 	 *
   1255      1.39    dyoung 	 * XXX does this leave us open to inheriting old state?
   1256      1.39    dyoung 	 */
   1257  1.75.4.1      phil 	for (i = 0; i < nitems(ni->ni_rxfrag); i++)
   1258      1.39    dyoung 		if (ni->ni_rxfrag[i] != NULL) {
   1259      1.39    dyoung 			m_freem(ni->ni_rxfrag[i]);
   1260      1.39    dyoung 			ni->ni_rxfrag[i] = NULL;
   1261      1.39    dyoung 		}
   1262      1.45     skrll 	/*
   1263      1.45     skrll 	 * Must be careful here to remove any key map entry w/o a LOR.
   1264      1.45     skrll 	 */
   1265      1.45     skrll 	ieee80211_node_delucastkey(ni);
   1266      1.22   mycroft }
   1267      1.22   mycroft 
   1268      1.22   mycroft static void
   1269      1.39    dyoung node_free(struct ieee80211_node *ni)
   1270       1.1    dyoung {
   1271      1.39    dyoung 	struct ieee80211com *ic = ni->ni_ic;
   1272      1.39    dyoung 
   1273  1.75.4.1      phil 	ieee80211_ratectl_node_deinit(ni);
   1274      1.39    dyoung 	ic->ic_node_cleanup(ni);
   1275  1.75.4.1      phil 	ieee80211_ies_cleanup(&ni->ni_ies);
   1276  1.75.4.1      phil 	ieee80211_psq_cleanup(&ni->ni_psq);
   1277  1.75.4.1      phil 	IEEE80211_FREE(ni, M_80211_NODE);
   1278  1.75.4.1      phil }
   1279  1.75.4.1      phil 
   1280  1.75.4.1      phil static void
   1281  1.75.4.1      phil node_age(struct ieee80211_node *ni)
   1282  1.75.4.1      phil {
   1283  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   1284  1.75.4.1      phil 
   1285  1.75.4.1      phil 	/*
   1286  1.75.4.1      phil 	 * Age frames on the power save queue.
   1287  1.75.4.1      phil 	 */
   1288  1.75.4.1      phil 	if (ieee80211_node_psq_age(ni) != 0 &&
   1289  1.75.4.1      phil 	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
   1290  1.75.4.1      phil 		vap->iv_set_tim(ni, 0);
   1291  1.75.4.1      phil 	/*
   1292  1.75.4.1      phil 	 * Age out HT resources (e.g. frames on the
   1293  1.75.4.1      phil 	 * A-MPDU reorder queues).
   1294  1.75.4.1      phil 	 */
   1295  1.75.4.1      phil 	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
   1296  1.75.4.1      phil 		ieee80211_ht_node_age(ni);
   1297       1.1    dyoung }
   1298       1.1    dyoung 
   1299  1.75.4.1      phil static int8_t
   1300      1.39    dyoung node_getrssi(const struct ieee80211_node *ni)
   1301       1.9    dyoung {
   1302  1.75.4.1      phil 	uint32_t avgrssi = ni->ni_avgrssi;
   1303  1.75.4.1      phil 	int32_t rssi;
   1304  1.75.4.1      phil 
   1305  1.75.4.1      phil 	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
   1306  1.75.4.1      phil 		return 0;
   1307  1.75.4.1      phil 	rssi = IEEE80211_RSSI_GET(avgrssi);
   1308  1.75.4.1      phil 	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
   1309  1.75.4.1      phil }
   1310  1.75.4.1      phil 
   1311  1.75.4.1      phil static void
   1312  1.75.4.1      phil node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
   1313  1.75.4.1      phil {
   1314  1.75.4.1      phil 	*rssi = node_getrssi(ni);
   1315  1.75.4.1      phil 	*noise = ni->ni_noise;
   1316  1.75.4.1      phil }
   1317  1.75.4.1      phil 
   1318  1.75.4.1      phil static void
   1319  1.75.4.1      phil node_getmimoinfo(const struct ieee80211_node *ni,
   1320  1.75.4.1      phil 	struct ieee80211_mimo_info *info)
   1321  1.75.4.1      phil {
   1322  1.75.4.1      phil 	int i;
   1323  1.75.4.1      phil 	uint32_t avgrssi;
   1324  1.75.4.1      phil 	int32_t rssi;
   1325  1.75.4.1      phil 
   1326  1.75.4.1      phil 	bzero(info, sizeof(*info));
   1327  1.75.4.1      phil 
   1328  1.75.4.1      phil 	for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
   1329  1.75.4.1      phil 		/* Note: for now, just pri20 channel info */
   1330  1.75.4.1      phil 		avgrssi = ni->ni_mimo_rssi_ctl[i];
   1331  1.75.4.1      phil 		if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
   1332  1.75.4.1      phil 			info->ch[i].rssi[0] = 0;
   1333  1.75.4.1      phil 		} else {
   1334  1.75.4.1      phil 			rssi = IEEE80211_RSSI_GET(avgrssi);
   1335  1.75.4.1      phil 			info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
   1336  1.75.4.1      phil 		}
   1337  1.75.4.1      phil 		info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
   1338  1.75.4.1      phil 	}
   1339  1.75.4.1      phil 
   1340  1.75.4.1      phil 	/* XXX ext radios? */
   1341  1.75.4.1      phil 
   1342  1.75.4.1      phil 	/* XXX EVM? */
   1343       1.9    dyoung }
   1344       1.9    dyoung 
   1345       1.1    dyoung static void
   1346  1.75.4.1      phil ieee80211_add_node_nt(struct ieee80211_node_table *nt,
   1347  1.75.4.1      phil     struct ieee80211_node *ni)
   1348       1.1    dyoung {
   1349      1.39    dyoung 	struct ieee80211com *ic = nt->nt_ic;
   1350       1.1    dyoung 	int hash;
   1351       1.1    dyoung 
   1352  1.75.4.1      phil 	IEEE80211_NODE_LOCK_ASSERT(nt);
   1353  1.75.4.1      phil 
   1354  1.75.4.1      phil 	hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
   1355  1.75.4.1      phil 	(void) ic;	/* XXX IEEE80211_NODE_HASH */
   1356  1.75.4.1      phil 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
   1357  1.75.4.1      phil 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
   1358  1.75.4.1      phil 	nt->nt_count++;
   1359  1.75.4.1      phil 	ni->ni_table = nt;
   1360  1.75.4.1      phil }
   1361  1.75.4.1      phil 
   1362  1.75.4.1      phil static void
   1363  1.75.4.1      phil ieee80211_del_node_nt(struct ieee80211_node_table *nt,
   1364  1.75.4.1      phil     struct ieee80211_node *ni)
   1365  1.75.4.1      phil {
   1366  1.75.4.1      phil 
   1367  1.75.4.1      phil 	IEEE80211_NODE_LOCK_ASSERT(nt);
   1368  1.75.4.1      phil 
   1369  1.75.4.1      phil 	TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
   1370  1.75.4.1      phil 	LIST_REMOVE(ni, ni_hash);
   1371  1.75.4.1      phil 	nt->nt_count--;
   1372  1.75.4.1      phil 	KASSERT(nt->nt_count >= 0,
   1373  1.75.4.1      phil 	    ("nt_count is negative (%d)!\n", nt->nt_count));
   1374  1.75.4.1      phil 	ni->ni_table = NULL;
   1375  1.75.4.1      phil }
   1376  1.75.4.1      phil 
   1377  1.75.4.1      phil struct ieee80211_node *
   1378  1.75.4.1      phil ieee80211_alloc_node(struct ieee80211_node_table *nt,
   1379  1.75.4.1      phil 	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1380  1.75.4.1      phil {
   1381  1.75.4.1      phil 	struct ieee80211com *ic = nt->nt_ic;
   1382  1.75.4.1      phil 	struct ieee80211_node *ni;
   1383  1.75.4.1      phil 
   1384  1.75.4.1      phil 	ni = ic->ic_node_alloc(vap, macaddr);
   1385  1.75.4.1      phil 	if (ni == NULL) {
   1386  1.75.4.1      phil 		vap->iv_stats.is_rx_nodealloc++;
   1387  1.75.4.1      phil 		return NULL;
   1388  1.75.4.1      phil 	}
   1389  1.75.4.1      phil 
   1390  1.75.4.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
   1391      1.39    dyoung 		"%s %p<%s> in %s table\n", __func__, ni,
   1392      1.39    dyoung 		ether_sprintf(macaddr), nt->nt_name);
   1393      1.39    dyoung 
   1394       1.1    dyoung 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
   1395      1.39    dyoung 	ieee80211_node_initref(ni);		/* mark referenced */
   1396      1.39    dyoung 	ni->ni_chan = IEEE80211_CHAN_ANYC;
   1397      1.39    dyoung 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
   1398      1.39    dyoung 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
   1399  1.75.4.1      phil 	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
   1400  1.75.4.1      phil 	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
   1401  1.75.4.1      phil 	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
   1402      1.39    dyoung 	ni->ni_inact_reload = nt->nt_inact_init;
   1403      1.39    dyoung 	ni->ni_inact = ni->ni_inact_reload;
   1404  1.75.4.1      phil 	ni->ni_ath_defkeyix = 0x7fff;
   1405  1.75.4.1      phil 	ieee80211_psq_init(&ni->ni_psq, "unknown");
   1406  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_MESH
   1407  1.75.4.1      phil 	if (vap->iv_opmode == IEEE80211_M_MBSS)
   1408  1.75.4.1      phil 		ieee80211_mesh_node_init(vap, ni);
   1409  1.75.4.1      phil #endif
   1410      1.39    dyoung 	IEEE80211_NODE_LOCK(nt);
   1411  1.75.4.1      phil 	ieee80211_add_node_nt(nt, ni);
   1412  1.75.4.1      phil 	ni->ni_vap = vap;
   1413      1.39    dyoung 	ni->ni_ic = ic;
   1414      1.39    dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1415       1.1    dyoung 
   1416  1.75.4.1      phil 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
   1417  1.75.4.1      phil 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
   1418  1.75.4.1      phil 
   1419  1.75.4.1      phil 	ieee80211_ratectl_node_init(ni);
   1420      1.39    dyoung 
   1421       1.1    dyoung 	return ni;
   1422       1.1    dyoung }
   1423       1.1    dyoung 
   1424      1.45     skrll /*
   1425      1.45     skrll  * Craft a temporary node suitable for sending a management frame
   1426      1.45     skrll  * to the specified station.  We craft only as much state as we
   1427      1.45     skrll  * need to do the work since the node will be immediately reclaimed
   1428      1.45     skrll  * once the send completes.
   1429      1.45     skrll  */
   1430      1.45     skrll struct ieee80211_node *
   1431  1.75.4.1      phil ieee80211_tmp_node(struct ieee80211vap *vap,
   1432  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1433      1.45     skrll {
   1434  1.75.4.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1435      1.45     skrll 	struct ieee80211_node *ni;
   1436      1.45     skrll 
   1437  1.75.4.1      phil 	ni = ic->ic_node_alloc(vap, macaddr);
   1438      1.45     skrll 	if (ni != NULL) {
   1439  1.75.4.1      phil 		struct ieee80211_node *bss = vap->iv_bss;
   1440  1.75.4.1      phil 
   1441  1.75.4.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
   1442      1.45     skrll 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
   1443      1.45     skrll 
   1444  1.75.4.1      phil 		ni->ni_table = NULL;		/* NB: pedantic */
   1445  1.75.4.1      phil 		ni->ni_ic = ic;			/* NB: needed to set channel */
   1446  1.75.4.1      phil 		ni->ni_vap = vap;
   1447  1.75.4.1      phil 
   1448      1.45     skrll 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
   1449  1.75.4.1      phil 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
   1450      1.45     skrll 		ieee80211_node_initref(ni);		/* mark referenced */
   1451      1.45     skrll 		/* NB: required by ieee80211_fix_rate */
   1452  1.75.4.1      phil 		ieee80211_node_set_chan(ni, bss->ni_chan);
   1453  1.75.4.1      phil 		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
   1454      1.45     skrll 			IEEE80211_KEYIX_NONE);
   1455  1.75.4.1      phil 		ni->ni_txpower = bss->ni_txpower;
   1456      1.45     skrll 		/* XXX optimize away */
   1457  1.75.4.1      phil 		ieee80211_psq_init(&ni->ni_psq, "unknown");
   1458      1.45     skrll 
   1459  1.75.4.1      phil 		ieee80211_ratectl_node_init(ni);
   1460      1.45     skrll 	} else {
   1461      1.45     skrll 		/* XXX msg */
   1462  1.75.4.1      phil 		vap->iv_stats.is_rx_nodealloc++;
   1463      1.45     skrll 	}
   1464      1.45     skrll 	return ni;
   1465      1.45     skrll }
   1466      1.45     skrll 
   1467       1.1    dyoung struct ieee80211_node *
   1468  1.75.4.1      phil ieee80211_dup_bss(struct ieee80211vap *vap,
   1469  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1470       1.1    dyoung {
   1471  1.75.4.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1472      1.39    dyoung 	struct ieee80211_node *ni;
   1473      1.39    dyoung 
   1474  1.75.4.1      phil 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
   1475       1.1    dyoung 	if (ni != NULL) {
   1476  1.75.4.1      phil 		struct ieee80211_node *bss = vap->iv_bss;
   1477      1.11    dyoung 		/*
   1478  1.75.4.1      phil 		 * Inherit from iv_bss.
   1479      1.11    dyoung 		 */
   1480  1.75.4.1      phil 		copy_bss(ni, bss);
   1481  1.75.4.1      phil 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
   1482  1.75.4.1      phil 		ieee80211_node_set_chan(ni, bss->ni_chan);
   1483      1.75      maxv 	}
   1484       1.1    dyoung 	return ni;
   1485       1.1    dyoung }
   1486       1.1    dyoung 
   1487  1.75.4.1      phil /*
   1488  1.75.4.1      phil  * Create a bss node for a legacy WDS vap.  The far end does
   1489  1.75.4.1      phil  * not associate so we just create create a new node and
   1490  1.75.4.1      phil  * simulate an association.  The caller is responsible for
   1491  1.75.4.1      phil  * installing the node as the bss node and handling any further
   1492  1.75.4.1      phil  * setup work like authorizing the port.
   1493  1.75.4.1      phil  */
   1494  1.75.4.1      phil struct ieee80211_node *
   1495  1.75.4.1      phil ieee80211_node_create_wds(struct ieee80211vap *vap,
   1496  1.75.4.1      phil 	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
   1497  1.75.4.1      phil {
   1498  1.75.4.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1499  1.75.4.1      phil 	struct ieee80211_node *ni;
   1500  1.75.4.1      phil 
   1501  1.75.4.1      phil 	/* XXX check if node already in sta table? */
   1502  1.75.4.1      phil 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
   1503  1.75.4.1      phil 	if (ni != NULL) {
   1504  1.75.4.1      phil 		ni->ni_wdsvap = vap;
   1505  1.75.4.1      phil 		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
   1506  1.75.4.1      phil 		/*
   1507  1.75.4.1      phil 		 * Inherit any manually configured settings.
   1508  1.75.4.1      phil 		 */
   1509  1.75.4.1      phil 		copy_bss(ni, vap->iv_bss);
   1510  1.75.4.1      phil 		ieee80211_node_set_chan(ni, chan);
   1511  1.75.4.1      phil 		/* NB: propagate ssid so available to WPA supplicant */
   1512  1.75.4.1      phil 		ni->ni_esslen = vap->iv_des_ssid[0].len;
   1513  1.75.4.1      phil 		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
   1514  1.75.4.1      phil 		/* NB: no associd for peer */
   1515  1.75.4.1      phil 		/*
   1516  1.75.4.1      phil 		 * There are no management frames to use to
   1517  1.75.4.1      phil 		 * discover neighbor capabilities, so blindly
   1518  1.75.4.1      phil 		 * propagate the local configuration.
   1519  1.75.4.1      phil 		 */
   1520  1.75.4.1      phil 		if (vap->iv_flags & IEEE80211_F_WME)
   1521  1.75.4.1      phil 			ni->ni_flags |= IEEE80211_NODE_QOS;
   1522  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   1523  1.75.4.1      phil 		if (vap->iv_flags & IEEE80211_F_FF)
   1524  1.75.4.1      phil 			ni->ni_flags |= IEEE80211_NODE_FF;
   1525  1.75.4.1      phil #endif
   1526  1.75.4.1      phil 		/* XXX VHT */
   1527  1.75.4.1      phil 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
   1528  1.75.4.1      phil 		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
   1529  1.75.4.1      phil 			/*
   1530  1.75.4.1      phil 			 * Device is HT-capable and HT is enabled for
   1531  1.75.4.1      phil 			 * the vap; setup HT operation.  On return
   1532  1.75.4.1      phil 			 * ni_chan will be adjusted to an HT channel.
   1533  1.75.4.1      phil 			 */
   1534  1.75.4.1      phil 			ieee80211_ht_wds_init(ni);
   1535  1.75.4.1      phil 			if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
   1536  1.75.4.1      phil 				printf("%s: TODO: vht_wds_init\n", __func__);
   1537  1.75.4.1      phil 			}
   1538  1.75.4.1      phil 		} else {
   1539  1.75.4.1      phil 			struct ieee80211_channel *c = ni->ni_chan;
   1540  1.75.4.1      phil 			/*
   1541  1.75.4.1      phil 			 * Force a legacy channel to be used.
   1542  1.75.4.1      phil 			 */
   1543  1.75.4.1      phil 			c = ieee80211_find_channel(ic,
   1544  1.75.4.1      phil 			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
   1545  1.75.4.1      phil 			KASSERT(c != NULL, ("no legacy channel, %u/%x",
   1546  1.75.4.1      phil 			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
   1547  1.75.4.1      phil 			ni->ni_chan = c;
   1548  1.75.4.1      phil 		}
   1549  1.75.4.1      phil 	}
   1550  1.75.4.1      phil 	return ni;
   1551  1.75.4.1      phil }
   1552  1.75.4.1      phil 
   1553  1.75.4.1      phil struct ieee80211_node *
   1554      1.39    dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1555  1.75.4.1      phil ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
   1556  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
   1557      1.39    dyoung #else
   1558  1.75.4.1      phil ieee80211_find_node_locked(struct ieee80211_node_table *nt,
   1559  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1560      1.39    dyoung #endif
   1561       1.1    dyoung {
   1562       1.1    dyoung 	struct ieee80211_node *ni;
   1563       1.1    dyoung 	int hash;
   1564      1.11    dyoung 
   1565      1.39    dyoung 	IEEE80211_NODE_LOCK_ASSERT(nt);
   1566       1.1    dyoung 
   1567  1.75.4.1      phil 	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
   1568      1.39    dyoung 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
   1569       1.1    dyoung 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
   1570      1.39    dyoung 			ieee80211_ref_node(ni);	/* mark referenced */
   1571      1.39    dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1572  1.75.4.1      phil 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   1573      1.39    dyoung 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
   1574      1.39    dyoung 			    func, line,
   1575      1.39    dyoung 			    ni, ether_sprintf(ni->ni_macaddr),
   1576      1.39    dyoung 			    ieee80211_node_refcnt(ni));
   1577      1.39    dyoung #endif
   1578      1.11    dyoung 			return ni;
   1579       1.1    dyoung 		}
   1580       1.1    dyoung 	}
   1581      1.11    dyoung 	return NULL;
   1582      1.11    dyoung }
   1583      1.11    dyoung 
   1584      1.11    dyoung struct ieee80211_node *
   1585      1.39    dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1586      1.39    dyoung ieee80211_find_node_debug(struct ieee80211_node_table *nt,
   1587  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
   1588      1.39    dyoung #else
   1589  1.75.4.1      phil ieee80211_find_node(struct ieee80211_node_table *nt,
   1590  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1591      1.39    dyoung #endif
   1592      1.11    dyoung {
   1593      1.11    dyoung 	struct ieee80211_node *ni;
   1594      1.11    dyoung 
   1595      1.39    dyoung 	IEEE80211_NODE_LOCK(nt);
   1596  1.75.4.1      phil 	ni = ieee80211_find_node_locked(nt, macaddr);
   1597      1.39    dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1598       1.7    dyoung 	return ni;
   1599       1.7    dyoung }
   1600       1.7    dyoung 
   1601       1.7    dyoung struct ieee80211_node *
   1602  1.75.4.1      phil #ifdef IEEE80211_DEBUG_REFCNT
   1603  1.75.4.1      phil ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
   1604  1.75.4.1      phil 	const struct ieee80211vap *vap,
   1605  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
   1606  1.75.4.1      phil #else
   1607  1.75.4.1      phil ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
   1608  1.75.4.1      phil 	const struct ieee80211vap *vap,
   1609  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1610  1.75.4.1      phil #endif
   1611       1.7    dyoung {
   1612       1.7    dyoung 	struct ieee80211_node *ni;
   1613  1.75.4.1      phil 	int hash;
   1614       1.7    dyoung 
   1615  1.75.4.1      phil 	IEEE80211_NODE_LOCK_ASSERT(nt);
   1616      1.45     skrll 
   1617  1.75.4.1      phil 	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
   1618  1.75.4.1      phil 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
   1619  1.75.4.1      phil 		if (ni->ni_vap == vap &&
   1620  1.75.4.1      phil 		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
   1621  1.75.4.1      phil 			ieee80211_ref_node(ni);	/* mark referenced */
   1622  1.75.4.1      phil #ifdef IEEE80211_DEBUG_REFCNT
   1623  1.75.4.1      phil 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   1624  1.75.4.1      phil 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
   1625  1.75.4.1      phil 			    func, line,
   1626  1.75.4.1      phil 			    ni, ether_sprintf(ni->ni_macaddr),
   1627  1.75.4.1      phil 			    ieee80211_node_refcnt(ni));
   1628      1.45     skrll #endif
   1629  1.75.4.1      phil 			return ni;
   1630      1.45     skrll 		}
   1631      1.45     skrll 	}
   1632  1.75.4.1      phil 	return NULL;
   1633      1.45     skrll }
   1634      1.45     skrll 
   1635  1.75.4.1      phil struct ieee80211_node *
   1636  1.75.4.1      phil #ifdef IEEE80211_DEBUG_REFCNT
   1637  1.75.4.1      phil ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
   1638  1.75.4.1      phil 	const struct ieee80211vap *vap,
   1639  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
   1640  1.75.4.1      phil #else
   1641  1.75.4.1      phil ieee80211_find_vap_node(struct ieee80211_node_table *nt,
   1642  1.75.4.1      phil 	const struct ieee80211vap *vap,
   1643  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1644  1.75.4.1      phil #endif
   1645      1.45     skrll {
   1646  1.75.4.1      phil 	struct ieee80211_node *ni;
   1647      1.45     skrll 
   1648  1.75.4.1      phil 	IEEE80211_NODE_LOCK(nt);
   1649  1.75.4.1      phil 	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
   1650  1.75.4.1      phil 	IEEE80211_NODE_UNLOCK(nt);
   1651  1.75.4.1      phil 	return ni;
   1652      1.45     skrll }
   1653      1.45     skrll 
   1654      1.45     skrll /*
   1655  1.75.4.1      phil  * Fake up a node; this handles node discovery in adhoc mode.
   1656  1.75.4.1      phil  * Note that for the driver's benefit we we treat this like
   1657  1.75.4.1      phil  * an association so the driver has an opportunity to setup
   1658  1.75.4.1      phil  * it's private state.
   1659      1.45     skrll  */
   1660  1.75.4.1      phil struct ieee80211_node *
   1661  1.75.4.1      phil ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
   1662  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1663      1.45     skrll {
   1664      1.45     skrll 	struct ieee80211_node *ni;
   1665      1.45     skrll 
   1666  1.75.4.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
   1667  1.75.4.1      phil 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
   1668  1.75.4.1      phil 	ni = ieee80211_dup_bss(vap, macaddr);
   1669  1.75.4.1      phil 	if (ni != NULL) {
   1670  1.75.4.1      phil 		struct ieee80211com *ic = vap->iv_ic;
   1671  1.75.4.1      phil 
   1672  1.75.4.1      phil 		/* XXX no rate negotiation; just dup */
   1673  1.75.4.1      phil 		ni->ni_rates = vap->iv_bss->ni_rates;
   1674  1.75.4.1      phil 		if (ieee80211_iserp_rateset(&ni->ni_rates))
   1675  1.75.4.1      phil 			ni->ni_flags |= IEEE80211_NODE_ERP;
   1676  1.75.4.1      phil 		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
   1677  1.75.4.1      phil 			/*
   1678  1.75.4.1      phil 			 * In adhoc demo mode there are no management
   1679  1.75.4.1      phil 			 * frames to use to discover neighbor capabilities,
   1680  1.75.4.1      phil 			 * so blindly propagate the local configuration
   1681  1.75.4.1      phil 			 * so we can do interesting things (e.g. use
   1682  1.75.4.1      phil 			 * WME to disable ACK's).
   1683  1.75.4.1      phil 			 */
   1684  1.75.4.1      phil 			/*
   1685  1.75.4.1      phil 			 * XXX TODO: 11n?
   1686  1.75.4.1      phil 			 */
   1687  1.75.4.1      phil 			if (vap->iv_flags & IEEE80211_F_WME)
   1688  1.75.4.1      phil 				ni->ni_flags |= IEEE80211_NODE_QOS;
   1689  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   1690  1.75.4.1      phil 			if (vap->iv_flags & IEEE80211_F_FF)
   1691  1.75.4.1      phil 				ni->ni_flags |= IEEE80211_NODE_FF;
   1692  1.75.4.1      phil #endif
   1693  1.75.4.1      phil 		}
   1694  1.75.4.1      phil 		ieee80211_node_setuptxparms(ni);
   1695  1.75.4.1      phil 		ieee80211_ratectl_node_init(ni);
   1696  1.75.4.1      phil 
   1697  1.75.4.1      phil 		/*
   1698  1.75.4.1      phil 		 * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
   1699  1.75.4.1      phil 		 * not A-MPDU TX; not 11n rates, etc.  We'll cycle
   1700  1.75.4.1      phil 		 * that after we hear that we can indeed do 11n
   1701  1.75.4.1      phil 		 * (either by a beacon frame or by a probe response.)
   1702  1.75.4.1      phil 		 */
   1703  1.75.4.1      phil 
   1704  1.75.4.1      phil 		/*
   1705  1.75.4.1      phil 		 * This is the first time we see the node.
   1706  1.75.4.1      phil 		 */
   1707  1.75.4.1      phil 		if (ic->ic_newassoc != NULL)
   1708  1.75.4.1      phil 			ic->ic_newassoc(ni, 1);
   1709  1.75.4.1      phil 
   1710      1.45     skrll 		/*
   1711  1.75.4.1      phil 		 * Kick off a probe request to the given node;
   1712  1.75.4.1      phil 		 * we will then use the probe response to update
   1713  1.75.4.1      phil 		 * 11n/etc configuration state.
   1714  1.75.4.1      phil 		 *
   1715  1.75.4.1      phil 		 * XXX TODO: this isn't guaranteed, and until we get
   1716  1.75.4.1      phil 		 * a probe response, we won't be able to actually
   1717  1.75.4.1      phil 		 * do anything 802.11n related to the node.
   1718  1.75.4.1      phil 		 * So if this does indeed work, maybe we should hold
   1719  1.75.4.1      phil 		 * off on sending responses until we get the probe
   1720  1.75.4.1      phil 		 * response, or just default to some sensible subset
   1721  1.75.4.1      phil 		 * of 802.11n behaviour (eg always allow aggregation
   1722  1.75.4.1      phil 		 * negotiation TO us, but not FROM us, etc) so we
   1723  1.75.4.1      phil 		 * aren't entirely busted.
   1724      1.45     skrll 		 */
   1725  1.75.4.1      phil 		if (vap->iv_opmode == IEEE80211_M_IBSS) {
   1726  1.75.4.1      phil 			ieee80211_send_probereq(ni, /* node */
   1727  1.75.4.1      phil 				vap->iv_myaddr, /* SA */
   1728  1.75.4.1      phil 				ni->ni_macaddr, /* DA */
   1729  1.75.4.1      phil 				vap->iv_bss->ni_bssid, /* BSSID */
   1730  1.75.4.1      phil 				vap->iv_bss->ni_essid,
   1731  1.75.4.1      phil 				vap->iv_bss->ni_esslen); /* SSID */
   1732      1.45     skrll 		}
   1733      1.74      maxv 
   1734  1.75.4.1      phil 		/* XXX not right for 802.1x/WPA */
   1735  1.75.4.1      phil 		ieee80211_node_authorize(ni);
   1736      1.45     skrll 	}
   1737  1.75.4.1      phil 	return ni;
   1738  1.75.4.1      phil }
   1739      1.74      maxv 
   1740  1.75.4.1      phil void
   1741  1.75.4.1      phil ieee80211_init_neighbor(struct ieee80211_node *ni,
   1742  1.75.4.1      phil 	const struct ieee80211_frame *wh,
   1743  1.75.4.1      phil 	const struct ieee80211_scanparams *sp)
   1744  1.75.4.1      phil {
   1745  1.75.4.1      phil 	int do_ht_setup = 0, do_vht_setup = 0;
   1746      1.74      maxv 
   1747  1.75.4.1      phil 	ni->ni_esslen = sp->ssid[1];
   1748  1.75.4.1      phil 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
   1749      1.45     skrll 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
   1750  1.75.4.1      phil 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
   1751  1.75.4.1      phil 	ni->ni_intval = sp->bintval;
   1752  1.75.4.1      phil 	ni->ni_capinfo = sp->capinfo;
   1753  1.75.4.1      phil 	ni->ni_chan = ni->ni_ic->ic_curchan;
   1754  1.75.4.1      phil 	ni->ni_fhdwell = sp->fhdwell;
   1755  1.75.4.1      phil 	ni->ni_fhindex = sp->fhindex;
   1756  1.75.4.1      phil 	ni->ni_erp = sp->erp;
   1757  1.75.4.1      phil 	ni->ni_timoff = sp->timoff;
   1758  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_MESH
   1759  1.75.4.1      phil 	if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
   1760  1.75.4.1      phil 		ieee80211_mesh_init_neighbor(ni, wh, sp);
   1761  1.75.4.1      phil #endif
   1762  1.75.4.1      phil 	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
   1763  1.75.4.1      phil 		ieee80211_ies_expand(&ni->ni_ies);
   1764  1.75.4.1      phil 		if (ni->ni_ies.wme_ie != NULL)
   1765  1.75.4.1      phil 			ni->ni_flags |= IEEE80211_NODE_QOS;
   1766  1.75.4.1      phil 		else
   1767  1.75.4.1      phil 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
   1768  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   1769  1.75.4.1      phil 		if (ni->ni_ies.ath_ie != NULL)
   1770  1.75.4.1      phil 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
   1771  1.75.4.1      phil #endif
   1772  1.75.4.1      phil 		if (ni->ni_ies.htcap_ie != NULL)
   1773  1.75.4.1      phil 			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
   1774  1.75.4.1      phil 		if (ni->ni_ies.htinfo_ie != NULL)
   1775  1.75.4.1      phil 			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
   1776  1.75.4.1      phil 
   1777  1.75.4.1      phil 		if (ni->ni_ies.vhtcap_ie != NULL)
   1778  1.75.4.1      phil 			ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
   1779  1.75.4.1      phil 		if (ni->ni_ies.vhtopmode_ie != NULL)
   1780  1.75.4.1      phil 			ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
   1781  1.75.4.1      phil 
   1782  1.75.4.1      phil 		if ((ni->ni_ies.htcap_ie != NULL) &&
   1783  1.75.4.1      phil 		    (ni->ni_ies.htinfo_ie != NULL) &&
   1784  1.75.4.1      phil 		    (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
   1785  1.75.4.1      phil 			do_ht_setup = 1;
   1786  1.75.4.1      phil 		}
   1787  1.75.4.1      phil 
   1788  1.75.4.1      phil 		if ((ni->ni_ies.vhtcap_ie != NULL) &&
   1789  1.75.4.1      phil 		    (ni->ni_ies.vhtopmode_ie != NULL) &&
   1790  1.75.4.1      phil 		    (ni->ni_vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
   1791  1.75.4.1      phil 			do_vht_setup = 1;
   1792  1.75.4.1      phil 		}
   1793      1.45     skrll 
   1794      1.45     skrll 	}
   1795      1.74      maxv 
   1796      1.45     skrll 	/* NB: must be after ni_chan is setup */
   1797  1.75.4.1      phil 	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
   1798  1.75.4.1      phil 		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
   1799  1.75.4.1      phil 		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
   1800  1.75.4.1      phil 
   1801  1.75.4.1      phil 	/*
   1802  1.75.4.1      phil 	 * If the neighbor is HT compatible, flip that on.
   1803  1.75.4.1      phil 	 */
   1804  1.75.4.1      phil 	if (do_ht_setup) {
   1805  1.75.4.1      phil 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
   1806  1.75.4.1      phil 		    "%s: doing HT setup\n", __func__);
   1807  1.75.4.1      phil 		ieee80211_ht_node_init(ni);
   1808  1.75.4.1      phil 		ieee80211_ht_updateparams(ni,
   1809  1.75.4.1      phil 		    ni->ni_ies.htcap_ie,
   1810  1.75.4.1      phil 		    ni->ni_ies.htinfo_ie);
   1811  1.75.4.1      phil 
   1812  1.75.4.1      phil 		if (do_vht_setup) {
   1813  1.75.4.1      phil 			if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
   1814  1.75.4.1      phil 				printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
   1815  1.75.4.1      phil 				    __func__,
   1816  1.75.4.1      phil 				    ni->ni_macaddr,
   1817  1.75.4.1      phil 				    ":");
   1818  1.75.4.1      phil 			} else {
   1819  1.75.4.1      phil 				ieee80211_vht_node_init(ni);
   1820  1.75.4.1      phil 				ieee80211_vht_updateparams(ni,
   1821  1.75.4.1      phil 				    ni->ni_ies.vhtcap_ie,
   1822  1.75.4.1      phil 				    ni->ni_ies.vhtopmode_ie);
   1823  1.75.4.1      phil 				ieee80211_setup_vht_rates(ni,
   1824  1.75.4.1      phil 				    ni->ni_ies.vhtcap_ie,
   1825  1.75.4.1      phil 				    ni->ni_ies.vhtopmode_ie);
   1826  1.75.4.1      phil 			}
   1827  1.75.4.1      phil 		}
   1828      1.45     skrll 
   1829  1.75.4.1      phil 		/*
   1830  1.75.4.1      phil 		 * Finally do the channel upgrade/change based
   1831  1.75.4.1      phil 		 * on the HT/VHT configuration.
   1832  1.75.4.1      phil 		 */
   1833  1.75.4.1      phil 		ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
   1834  1.75.4.1      phil 		    ni->ni_ies.htinfo_ie);
   1835  1.75.4.1      phil 		ieee80211_setup_htrates(ni,
   1836  1.75.4.1      phil 		    ni->ni_ies.htcap_ie,
   1837  1.75.4.1      phil 		    IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
   1838  1.75.4.1      phil 		ieee80211_setup_basic_htrates(ni,
   1839  1.75.4.1      phil 		    ni->ni_ies.htinfo_ie);
   1840      1.46    dyoung 
   1841  1.75.4.1      phil 		ieee80211_node_setuptxparms(ni);
   1842  1.75.4.1      phil 		ieee80211_ratectl_node_init(ni);
   1843      1.46    dyoung 
   1844  1.75.4.1      phil 		/* Reassociate; we're now 11n/11ac */
   1845  1.75.4.1      phil 		/*
   1846  1.75.4.1      phil 		 * XXX TODO: this is the wrong thing to do -
   1847  1.75.4.1      phil 		 * we're calling it with isnew=1 so the ath(4)
   1848  1.75.4.1      phil 		 * driver reinitialises the rate tables.
   1849  1.75.4.1      phil 		 * This "mostly" works for ath(4), but it won't
   1850  1.75.4.1      phil 		 * be right for firmware devices which allocate
   1851  1.75.4.1      phil 		 * node states.
   1852  1.75.4.1      phil 		 *
   1853  1.75.4.1      phil 		 * So, do we just create a new node and delete
   1854  1.75.4.1      phil 		 * the old one? Or?
   1855  1.75.4.1      phil 		 */
   1856  1.75.4.1      phil 		if (ni->ni_ic->ic_newassoc)
   1857  1.75.4.1      phil 			ni->ni_ic->ic_newassoc(ni, 1);
   1858  1.75.4.1      phil 	}
   1859      1.46    dyoung }
   1860      1.46    dyoung 
   1861      1.45     skrll /*
   1862      1.45     skrll  * Do node discovery in adhoc mode on receipt of a beacon
   1863      1.45     skrll  * or probe response frame.  Note that for the driver's
   1864      1.45     skrll  * benefit we we treat this like an association so the
   1865  1.75.4.1      phil  * driver has an opportunity to setup it's private state.
   1866      1.45     skrll  */
   1867      1.45     skrll struct ieee80211_node *
   1868  1.75.4.1      phil ieee80211_add_neighbor(struct ieee80211vap *vap,
   1869      1.45     skrll 	const struct ieee80211_frame *wh,
   1870      1.45     skrll 	const struct ieee80211_scanparams *sp)
   1871      1.45     skrll {
   1872      1.45     skrll 	struct ieee80211_node *ni;
   1873      1.45     skrll 
   1874  1.75.4.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
   1875  1.75.4.1      phil 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
   1876  1.75.4.1      phil 	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
   1877      1.45     skrll 	if (ni != NULL) {
   1878  1.75.4.1      phil 		struct ieee80211com *ic = vap->iv_ic;
   1879  1.75.4.1      phil 
   1880  1.75.4.1      phil 		ieee80211_init_neighbor(ni, wh, sp);
   1881  1.75.4.1      phil 		if (ieee80211_iserp_rateset(&ni->ni_rates))
   1882  1.75.4.1      phil 			ni->ni_flags |= IEEE80211_NODE_ERP;
   1883  1.75.4.1      phil 		ieee80211_node_setuptxparms(ni);
   1884  1.75.4.1      phil 		ieee80211_ratectl_node_init(ni);
   1885  1.75.4.1      phil 		if (ic->ic_newassoc != NULL)
   1886  1.75.4.1      phil 			ic->ic_newassoc(ni, 1);
   1887      1.39    dyoung 		/* XXX not right for 802.1x/WPA */
   1888      1.45     skrll 		ieee80211_node_authorize(ni);
   1889       1.7    dyoung 	}
   1890      1.39    dyoung 	return ni;
   1891       1.7    dyoung }
   1892       1.7    dyoung 
   1893  1.75.4.1      phil #define	IS_PROBEREQ(wh) \
   1894  1.75.4.1      phil 	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
   1895  1.75.4.1      phil 	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
   1896  1.75.4.1      phil #define	IS_BCAST_PROBEREQ(wh) \
   1897  1.75.4.1      phil 	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
   1898  1.75.4.1      phil 	    ((const struct ieee80211_frame *)(wh))->i_addr3))
   1899  1.75.4.1      phil 
   1900  1.75.4.1      phil static __inline struct ieee80211_node *
   1901  1.75.4.1      phil _find_rxnode(struct ieee80211_node_table *nt,
   1902  1.75.4.1      phil     const struct ieee80211_frame_min *wh)
   1903  1.75.4.1      phil {
   1904  1.75.4.1      phil 	if (IS_BCAST_PROBEREQ(wh))
   1905  1.75.4.1      phil 		return NULL;		/* spam bcast probe req to all vap's */
   1906  1.75.4.1      phil 	return ieee80211_find_node_locked(nt, wh->i_addr2);
   1907  1.75.4.1      phil }
   1908  1.75.4.1      phil 
   1909       1.7    dyoung /*
   1910      1.39    dyoung  * Locate the node for sender, track state, and then pass the
   1911  1.75.4.1      phil  * (referenced) node up to the 802.11 layer for its use.  Note
   1912  1.75.4.1      phil  * we can return NULL if the sender is not in the table.
   1913       1.7    dyoung  */
   1914      1.39    dyoung struct ieee80211_node *
   1915      1.39    dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1916      1.39    dyoung ieee80211_find_rxnode_debug(struct ieee80211com *ic,
   1917      1.39    dyoung 	const struct ieee80211_frame_min *wh, const char *func, int line)
   1918      1.39    dyoung #else
   1919      1.39    dyoung ieee80211_find_rxnode(struct ieee80211com *ic,
   1920      1.39    dyoung 	const struct ieee80211_frame_min *wh)
   1921      1.39    dyoung #endif
   1922       1.7    dyoung {
   1923      1.39    dyoung 	struct ieee80211_node_table *nt;
   1924      1.39    dyoung 	struct ieee80211_node *ni;
   1925       1.7    dyoung 
   1926  1.75.4.1      phil 	nt = &ic->ic_sta;
   1927      1.39    dyoung 	IEEE80211_NODE_LOCK(nt);
   1928  1.75.4.1      phil 	ni = _find_rxnode(nt, wh);
   1929      1.39    dyoung 	IEEE80211_NODE_UNLOCK(nt);
   1930       1.7    dyoung 
   1931      1.45     skrll 	return ni;
   1932      1.45     skrll }
   1933      1.45     skrll 
   1934      1.45     skrll /*
   1935      1.45     skrll  * Like ieee80211_find_rxnode but use the supplied h/w
   1936      1.45     skrll  * key index as a hint to locate the node in the key
   1937      1.45     skrll  * mapping table.  If an entry is present at the key
   1938      1.45     skrll  * index we return it; otherwise do a normal lookup and
   1939      1.45     skrll  * update the mapping table if the station has a unicast
   1940      1.45     skrll  * key assigned to it.
   1941      1.45     skrll  */
   1942      1.45     skrll struct ieee80211_node *
   1943      1.45     skrll #ifdef IEEE80211_DEBUG_REFCNT
   1944      1.45     skrll ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
   1945      1.45     skrll 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
   1946      1.45     skrll 	const char *func, int line)
   1947      1.45     skrll #else
   1948      1.45     skrll ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
   1949      1.45     skrll 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
   1950      1.45     skrll #endif
   1951      1.45     skrll {
   1952      1.45     skrll 	struct ieee80211_node_table *nt;
   1953      1.45     skrll 	struct ieee80211_node *ni;
   1954      1.45     skrll 
   1955  1.75.4.1      phil 	nt = &ic->ic_sta;
   1956      1.45     skrll 	IEEE80211_NODE_LOCK(nt);
   1957  1.75.4.1      phil 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
   1958      1.45     skrll 		ni = nt->nt_keyixmap[keyix];
   1959  1.75.4.1      phil 	else
   1960      1.45     skrll 		ni = NULL;
   1961      1.45     skrll 	if (ni == NULL) {
   1962  1.75.4.1      phil 		ni = _find_rxnode(nt, wh);
   1963  1.75.4.1      phil 		if (ni != NULL && nt->nt_keyixmap != NULL) {
   1964      1.45     skrll 			/*
   1965      1.45     skrll 			 * If the station has a unicast key cache slot
   1966      1.45     skrll 			 * assigned update the key->node mapping table.
   1967      1.45     skrll 			 */
   1968      1.45     skrll 			keyix = ni->ni_ucastkey.wk_rxkeyix;
   1969      1.45     skrll 			/* XXX can keyixmap[keyix] != NULL? */
   1970      1.45     skrll 			if (keyix < nt->nt_keyixmax &&
   1971      1.45     skrll 			    nt->nt_keyixmap[keyix] == NULL) {
   1972  1.75.4.1      phil 				IEEE80211_DPRINTF(ni->ni_vap,
   1973  1.75.4.1      phil 				    IEEE80211_MSG_NODE,
   1974      1.45     skrll 				    "%s: add key map entry %p<%s> refcnt %d\n",
   1975      1.45     skrll 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
   1976      1.45     skrll 				    ieee80211_node_refcnt(ni)+1);
   1977      1.45     skrll 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
   1978      1.45     skrll 			}
   1979      1.45     skrll 		}
   1980      1.45     skrll 	} else {
   1981  1.75.4.1      phil 		if (IS_BCAST_PROBEREQ(wh))
   1982  1.75.4.1      phil 			ni = NULL;	/* spam bcast probe req to all vap's */
   1983  1.75.4.1      phil 		else
   1984  1.75.4.1      phil 			ieee80211_ref_node(ni);
   1985      1.45     skrll 	}
   1986      1.45     skrll 	IEEE80211_NODE_UNLOCK(nt);
   1987      1.45     skrll 
   1988      1.45     skrll 	return ni;
   1989      1.45     skrll }
   1990  1.75.4.1      phil #undef IS_BCAST_PROBEREQ
   1991  1.75.4.1      phil #undef IS_PROBEREQ
   1992       1.7    dyoung 
   1993      1.39    dyoung /*
   1994      1.39    dyoung  * Return a reference to the appropriate node for sending
   1995      1.39    dyoung  * a data frame.  This handles node discovery in adhoc networks.
   1996      1.33    dyoung  */
   1997       1.7    dyoung struct ieee80211_node *
   1998      1.39    dyoung #ifdef IEEE80211_DEBUG_REFCNT
   1999  1.75.4.1      phil ieee80211_find_txnode_debug(struct ieee80211vap *vap,
   2000  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN],
   2001      1.39    dyoung 	const char *func, int line)
   2002      1.39    dyoung #else
   2003  1.75.4.1      phil ieee80211_find_txnode(struct ieee80211vap *vap,
   2004  1.75.4.1      phil 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   2005      1.39    dyoung #endif
   2006       1.7    dyoung {
   2007  1.75.4.1      phil 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
   2008       1.7    dyoung 	struct ieee80211_node *ni;
   2009       1.7    dyoung 
   2010      1.39    dyoung 	/*
   2011      1.39    dyoung 	 * The destination address should be in the node table
   2012      1.45     skrll 	 * unless this is a multicast/broadcast frame.  We can
   2013      1.45     skrll 	 * also optimize station mode operation, all frames go
   2014      1.45     skrll 	 * to the bss node.
   2015      1.39    dyoung 	 */
   2016      1.39    dyoung 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
   2017      1.39    dyoung 	IEEE80211_NODE_LOCK(nt);
   2018  1.75.4.1      phil 	if (vap->iv_opmode == IEEE80211_M_STA ||
   2019  1.75.4.1      phil 	    vap->iv_opmode == IEEE80211_M_WDS ||
   2020  1.75.4.1      phil 	    IEEE80211_IS_MULTICAST(macaddr))
   2021  1.75.4.1      phil 		ni = ieee80211_ref_node(vap->iv_bss);
   2022      1.45     skrll 	else
   2023  1.75.4.1      phil 		ni = ieee80211_find_node_locked(nt, macaddr);
   2024      1.39    dyoung 	IEEE80211_NODE_UNLOCK(nt);
   2025      1.15    dyoung 
   2026      1.39    dyoung 	if (ni == NULL) {
   2027  1.75.4.1      phil 		if (vap->iv_opmode == IEEE80211_M_IBSS ||
   2028  1.75.4.1      phil 		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
   2029      1.39    dyoung 			/*
   2030      1.39    dyoung 			 * In adhoc mode cons up a node for the destination.
   2031      1.39    dyoung 			 * Note that we need an additional reference for the
   2032  1.75.4.1      phil 			 * caller to be consistent with
   2033  1.75.4.1      phil 			 * ieee80211_find_node_locked.
   2034  1.75.4.1      phil 			 */
   2035  1.75.4.1      phil 			/*
   2036  1.75.4.1      phil 			 * XXX TODO: this doesn't fake up 11n state; we need
   2037  1.75.4.1      phil 			 * to find another way to get it upgraded.
   2038      1.39    dyoung 			 */
   2039  1.75.4.1      phil 			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
   2040      1.39    dyoung 			if (ni != NULL)
   2041  1.75.4.1      phil 				(void) ieee80211_ref_node(ni);
   2042      1.39    dyoung 		} else {
   2043  1.75.4.1      phil 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
   2044  1.75.4.1      phil 			    "no node, discard frame (%s)", __func__);
   2045  1.75.4.1      phil 			vap->iv_stats.is_tx_nonode++;
   2046      1.39    dyoung 		}
   2047      1.39    dyoung 	}
   2048      1.39    dyoung 	return ni;
   2049       1.1    dyoung }
   2050       1.1    dyoung 
   2051  1.75.4.1      phil static void
   2052  1.75.4.1      phil _ieee80211_free_node(struct ieee80211_node *ni)
   2053       1.1    dyoung {
   2054  1.75.4.1      phil 	struct ieee80211_node_table *nt = ni->ni_table;
   2055      1.75      maxv 
   2056  1.75.4.1      phil 	/*
   2057  1.75.4.1      phil 	 * NB: careful about referencing the vap as it may be
   2058  1.75.4.1      phil 	 * gone if the last reference was held by a driver.
   2059  1.75.4.1      phil 	 * We know the com will always be present so it's safe
   2060  1.75.4.1      phil 	 * to use ni_ic below to reclaim resources.
   2061  1.75.4.1      phil 	 */
   2062  1.75.4.1      phil #if 0
   2063  1.75.4.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
   2064  1.75.4.1      phil 		"%s %p<%s> in %s table\n", __func__, ni,
   2065  1.75.4.1      phil 		ether_sprintf(ni->ni_macaddr),
   2066  1.75.4.1      phil 		nt != NULL ? nt->nt_name : "<gone>");
   2067      1.61  gmcgarry #endif
   2068  1.75.4.1      phil 	if (ni->ni_associd != 0) {
   2069  1.75.4.1      phil 		struct ieee80211vap *vap = ni->ni_vap;
   2070  1.75.4.1      phil 		if (vap->iv_aid_bitmap != NULL)
   2071  1.75.4.1      phil 			IEEE80211_AID_CLR(vap, ni->ni_associd);
   2072       1.1    dyoung 	}
   2073  1.75.4.1      phil 	if (nt != NULL)
   2074  1.75.4.1      phil 		ieee80211_del_node_nt(nt, ni);
   2075  1.75.4.1      phil 	ni->ni_ic->ic_node_free(ni);
   2076       1.1    dyoung }
   2077       1.1    dyoung 
   2078      1.39    dyoung /*
   2079  1.75.4.1      phil  * Clear any entry in the unicast key mapping table.
   2080      1.39    dyoung  */
   2081  1.75.4.1      phil static int
   2082  1.75.4.1      phil node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
   2083      1.39    dyoung {
   2084  1.75.4.1      phil 	ieee80211_keyix keyix;
   2085      1.72  christos 
   2086  1.75.4.1      phil 	keyix = ni->ni_ucastkey.wk_rxkeyix;
   2087  1.75.4.1      phil 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
   2088  1.75.4.1      phil 	    nt->nt_keyixmap[keyix] == ni) {
   2089  1.75.4.1      phil 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   2090  1.75.4.1      phil 			"%s: %p<%s> clear key map entry %u\n",
   2091  1.75.4.1      phil 			__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
   2092  1.75.4.1      phil 		nt->nt_keyixmap[keyix] = NULL;
   2093  1.75.4.1      phil 		ieee80211_node_decref(ni);
   2094  1.75.4.1      phil 		return 1;
   2095      1.39    dyoung 	}
   2096      1.75      maxv 
   2097  1.75.4.1      phil 	return 0;
   2098       1.1    dyoung }
   2099       1.1    dyoung 
   2100       1.1    dyoung void
   2101      1.39    dyoung #ifdef IEEE80211_DEBUG_REFCNT
   2102      1.39    dyoung ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
   2103      1.39    dyoung #else
   2104      1.39    dyoung ieee80211_free_node(struct ieee80211_node *ni)
   2105      1.39    dyoung #endif
   2106      1.39    dyoung {
   2107      1.39    dyoung 	struct ieee80211_node_table *nt = ni->ni_table;
   2108      1.39    dyoung 
   2109      1.39    dyoung #ifdef IEEE80211_DEBUG_REFCNT
   2110  1.75.4.1      phil 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   2111      1.39    dyoung 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
   2112      1.39    dyoung 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
   2113      1.39    dyoung #endif
   2114      1.45     skrll 	if (nt != NULL) {
   2115      1.45     skrll 		IEEE80211_NODE_LOCK(nt);
   2116      1.45     skrll 		if (ieee80211_node_dectestref(ni)) {
   2117      1.45     skrll 			/*
   2118      1.45     skrll 			 * Last reference, reclaim state.
   2119      1.45     skrll 			 */
   2120      1.39    dyoung 			_ieee80211_free_node(ni);
   2121  1.75.4.1      phil 		} else if (ieee80211_node_refcnt(ni) == 1)
   2122  1.75.4.1      phil 			if (node_clear_keyixmap(nt, ni))
   2123      1.45     skrll 				_ieee80211_free_node(ni);
   2124      1.45     skrll 		IEEE80211_NODE_UNLOCK(nt);
   2125      1.45     skrll 	} else {
   2126      1.45     skrll 		if (ieee80211_node_dectestref(ni))
   2127      1.39    dyoung 			_ieee80211_free_node(ni);
   2128      1.39    dyoung 	}
   2129      1.39    dyoung }
   2130      1.39    dyoung 
   2131      1.39    dyoung /*
   2132      1.45     skrll  * Reclaim a unicast key and clear any key cache state.
   2133      1.45     skrll  */
   2134      1.45     skrll int
   2135      1.45     skrll ieee80211_node_delucastkey(struct ieee80211_node *ni)
   2136      1.45     skrll {
   2137      1.45     skrll 	struct ieee80211com *ic = ni->ni_ic;
   2138      1.45     skrll 	struct ieee80211_node_table *nt = &ic->ic_sta;
   2139      1.45     skrll 	struct ieee80211_node *nikey;
   2140      1.45     skrll 	ieee80211_keyix keyix;
   2141      1.45     skrll 	int isowned, status;
   2142      1.45     skrll 
   2143      1.45     skrll 	/*
   2144      1.45     skrll 	 * NB: We must beware of LOR here; deleting the key
   2145      1.45     skrll 	 * can cause the crypto layer to block traffic updates
   2146      1.45     skrll 	 * which can generate a LOR against the node table lock;
   2147      1.45     skrll 	 * grab it here and stash the key index for our use below.
   2148      1.45     skrll 	 *
   2149      1.45     skrll 	 * Must also beware of recursion on the node table lock.
   2150      1.45     skrll 	 * When called from node_cleanup we may already have
   2151      1.45     skrll 	 * the node table lock held.  Unfortunately there's no
   2152      1.45     skrll 	 * way to separate out this path so we must do this
   2153      1.45     skrll 	 * conditionally.
   2154      1.45     skrll 	 */
   2155      1.45     skrll 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
   2156      1.45     skrll 	if (!isowned)
   2157      1.45     skrll 		IEEE80211_NODE_LOCK(nt);
   2158  1.75.4.1      phil 	nikey = NULL;
   2159  1.75.4.1      phil 	status = 1;		/* NB: success */
   2160  1.75.4.1      phil 	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
   2161  1.75.4.1      phil 		keyix = ni->ni_ucastkey.wk_rxkeyix;
   2162  1.75.4.1      phil 		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
   2163  1.75.4.1      phil 		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
   2164  1.75.4.1      phil 			nikey = nt->nt_keyixmap[keyix];
   2165  1.75.4.1      phil 			nt->nt_keyixmap[keyix] = NULL;
   2166  1.75.4.1      phil 		}
   2167  1.75.4.1      phil 	}
   2168      1.45     skrll 	if (!isowned)
   2169  1.75.4.1      phil 		IEEE80211_NODE_UNLOCK(nt);
   2170      1.45     skrll 
   2171      1.45     skrll 	if (nikey != NULL) {
   2172  1.75.4.1      phil 		KASSERT(nikey == ni,
   2173      1.45     skrll 			("key map out of sync, ni %p nikey %p", ni, nikey));
   2174  1.75.4.1      phil 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   2175      1.45     skrll 			"%s: delete key map entry %p<%s> refcnt %d\n",
   2176      1.45     skrll 			__func__, ni, ether_sprintf(ni->ni_macaddr),
   2177      1.45     skrll 			ieee80211_node_refcnt(ni)-1);
   2178      1.45     skrll 		ieee80211_free_node(ni);
   2179      1.45     skrll 	}
   2180      1.45     skrll 	return status;
   2181      1.45     skrll }
   2182      1.45     skrll 
   2183      1.45     skrll /*
   2184      1.39    dyoung  * Reclaim a node.  If this is the last reference count then
   2185      1.39    dyoung  * do the normal free work.  Otherwise remove it from the node
   2186      1.39    dyoung  * table and mark it gone by clearing the back-reference.
   2187      1.39    dyoung  */
   2188      1.39    dyoung static void
   2189      1.39    dyoung node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
   2190      1.39    dyoung {
   2191      1.45     skrll 
   2192      1.45     skrll 	IEEE80211_NODE_LOCK_ASSERT(nt);
   2193      1.39    dyoung 
   2194  1.75.4.1      phil 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   2195      1.39    dyoung 		"%s: remove %p<%s> from %s table, refcnt %d\n",
   2196      1.39    dyoung 		__func__, ni, ether_sprintf(ni->ni_macaddr),
   2197      1.39    dyoung 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
   2198      1.45     skrll 	/*
   2199      1.45     skrll 	 * Clear any entry in the unicast key mapping table.
   2200      1.45     skrll 	 * We need to do it here so rx lookups don't find it
   2201      1.45     skrll 	 * in the mapping table even if it's not in the hash
   2202      1.45     skrll 	 * table.  We cannot depend on the mapping table entry
   2203      1.45     skrll 	 * being cleared because the node may not be free'd.
   2204      1.45     skrll 	 */
   2205  1.75.4.1      phil 	(void)node_clear_keyixmap(nt, ni);
   2206  1.75.4.1      phil 	if (!ieee80211_node_dectestref(ni)) {
   2207  1.75.4.1      phil 		/*
   2208  1.75.4.1      phil 		 * Other references are present, just remove the
   2209  1.75.4.1      phil 		 * node from the table so it cannot be found.  When
   2210  1.75.4.1      phil 		 * the references are dropped storage will be
   2211  1.75.4.1      phil 		 * reclaimed.
   2212  1.75.4.1      phil 		 */
   2213  1.75.4.1      phil 		ieee80211_del_node_nt(nt, ni);
   2214  1.75.4.1      phil 	} else
   2215  1.75.4.1      phil 		_ieee80211_free_node(ni);
   2216  1.75.4.1      phil }
   2217  1.75.4.1      phil 
   2218  1.75.4.1      phil /*
   2219  1.75.4.1      phil  * Node table support.
   2220  1.75.4.1      phil  */
   2221  1.75.4.1      phil 
   2222  1.75.4.1      phil static void
   2223  1.75.4.1      phil ieee80211_node_table_init(struct ieee80211com *ic,
   2224  1.75.4.1      phil 	struct ieee80211_node_table *nt,
   2225  1.75.4.1      phil 	const char *name, int inact, int keyixmax)
   2226  1.75.4.1      phil {
   2227  1.75.4.1      phil 
   2228  1.75.4.1      phil 	nt->nt_ic = ic;
   2229  1.75.4.1      phil 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
   2230  1.75.4.1      phil 	TAILQ_INIT(&nt->nt_node);
   2231  1.75.4.1      phil 	nt->nt_count = 0;
   2232  1.75.4.1      phil 	nt->nt_name = name;
   2233  1.75.4.1      phil 	nt->nt_inact_init = inact;
   2234  1.75.4.1      phil 	nt->nt_keyixmax = keyixmax;
   2235  1.75.4.1      phil 	if (nt->nt_keyixmax > 0) {
   2236  1.75.4.1      phil 		nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
   2237  1.75.4.1      phil 			keyixmax * sizeof(struct ieee80211_node *),
   2238  1.75.4.1      phil 			M_80211_NODE,
   2239  1.75.4.1      phil 			IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   2240  1.75.4.1      phil 		if (nt->nt_keyixmap == NULL)
   2241  1.75.4.1      phil 			ic_printf(ic,
   2242  1.75.4.1      phil 			    "Cannot allocate key index map with %u entries\n",
   2243  1.75.4.1      phil 			    keyixmax);
   2244  1.75.4.1      phil 	} else
   2245  1.75.4.1      phil 		nt->nt_keyixmap = NULL;
   2246  1.75.4.1      phil }
   2247  1.75.4.1      phil 
   2248  1.75.4.1      phil static void
   2249  1.75.4.1      phil ieee80211_node_table_reset(struct ieee80211_node_table *nt,
   2250  1.75.4.1      phil 	struct ieee80211vap *match)
   2251  1.75.4.1      phil {
   2252  1.75.4.1      phil 	struct ieee80211_node *ni, *next;
   2253  1.75.4.1      phil 
   2254  1.75.4.1      phil 	IEEE80211_NODE_LOCK(nt);
   2255  1.75.4.1      phil 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
   2256  1.75.4.1      phil 		if (match != NULL && ni->ni_vap != match)
   2257  1.75.4.1      phil 			continue;
   2258  1.75.4.1      phil 		/* XXX can this happen?  if so need's work */
   2259  1.75.4.1      phil 		if (ni->ni_associd != 0) {
   2260  1.75.4.1      phil 			struct ieee80211vap *vap = ni->ni_vap;
   2261  1.75.4.1      phil 
   2262  1.75.4.1      phil 			if (vap->iv_auth->ia_node_leave != NULL)
   2263  1.75.4.1      phil 				vap->iv_auth->ia_node_leave(ni);
   2264  1.75.4.1      phil 			if (vap->iv_aid_bitmap != NULL)
   2265  1.75.4.1      phil 				IEEE80211_AID_CLR(vap, ni->ni_associd);
   2266  1.75.4.1      phil 		}
   2267  1.75.4.1      phil 		ni->ni_wdsvap = NULL;		/* clear reference */
   2268  1.75.4.1      phil 		node_reclaim(nt, ni);
   2269  1.75.4.1      phil 	}
   2270  1.75.4.1      phil 	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
   2271  1.75.4.1      phil 		/*
   2272  1.75.4.1      phil 		 * Make a separate pass to clear references to this vap
   2273  1.75.4.1      phil 		 * held by DWDS entries.  They will not be matched above
   2274  1.75.4.1      phil 		 * because ni_vap will point to the ap vap but we still
   2275  1.75.4.1      phil 		 * need to clear ni_wdsvap when the WDS vap is destroyed
   2276  1.75.4.1      phil 		 * and/or reset.
   2277  1.75.4.1      phil 		 */
   2278  1.75.4.1      phil 		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
   2279  1.75.4.1      phil 			if (ni->ni_wdsvap == match)
   2280  1.75.4.1      phil 				ni->ni_wdsvap = NULL;
   2281  1.75.4.1      phil 	}
   2282  1.75.4.1      phil 	IEEE80211_NODE_UNLOCK(nt);
   2283  1.75.4.1      phil }
   2284  1.75.4.1      phil 
   2285  1.75.4.1      phil static void
   2286  1.75.4.1      phil ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
   2287  1.75.4.1      phil {
   2288  1.75.4.1      phil 	ieee80211_node_table_reset(nt, NULL);
   2289  1.75.4.1      phil 	if (nt->nt_keyixmap != NULL) {
   2290  1.75.4.1      phil #ifdef DIAGNOSTIC
   2291  1.75.4.1      phil 		/* XXX verify all entries are NULL */
   2292  1.75.4.1      phil 		int i;
   2293  1.75.4.1      phil 		for (i = 0; i < nt->nt_keyixmax; i++)
   2294  1.75.4.1      phil 			if (nt->nt_keyixmap[i] != NULL)
   2295  1.75.4.1      phil 				printf("%s: %s[%u] still active\n", __func__,
   2296  1.75.4.1      phil 					nt->nt_name, i);
   2297  1.75.4.1      phil #endif
   2298  1.75.4.1      phil 		IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
   2299  1.75.4.1      phil 		nt->nt_keyixmap = NULL;
   2300  1.75.4.1      phil 	}
   2301  1.75.4.1      phil 	IEEE80211_NODE_LOCK_DESTROY(nt);
   2302  1.75.4.1      phil }
   2303  1.75.4.1      phil 
   2304  1.75.4.1      phil static void
   2305  1.75.4.1      phil timeout_stations(void *arg __unused, struct ieee80211_node *ni)
   2306  1.75.4.1      phil {
   2307  1.75.4.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   2308  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   2309  1.75.4.1      phil 
   2310  1.75.4.1      phil 	/*
   2311  1.75.4.1      phil 	 * Only process stations when in RUN state.  This
   2312  1.75.4.1      phil 	 * insures, for example, that we don't timeout an
   2313  1.75.4.1      phil 	 * inactive station during CAC.  Note that CSA state
   2314  1.75.4.1      phil 	 * is actually handled in ieee80211_node_timeout as
   2315  1.75.4.1      phil 	 * it applies to more than timeout processing.
   2316  1.75.4.1      phil 	 */
   2317  1.75.4.1      phil 	if (vap->iv_state != IEEE80211_S_RUN)
   2318  1.75.4.1      phil 		return;
   2319  1.75.4.1      phil 	/*
   2320  1.75.4.1      phil 	 * Ignore entries for which have yet to receive an
   2321  1.75.4.1      phil 	 * authentication frame.  These are transient and
   2322  1.75.4.1      phil 	 * will be reclaimed when the last reference to them
   2323  1.75.4.1      phil 	 * goes away (when frame xmits complete).
   2324  1.75.4.1      phil 	 */
   2325  1.75.4.1      phil 	if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
   2326  1.75.4.1      phil 	     vap->iv_opmode == IEEE80211_M_STA) &&
   2327  1.75.4.1      phil 	    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
   2328  1.75.4.1      phil 		return;
   2329  1.75.4.1      phil 	/*
   2330  1.75.4.1      phil 	 * Free fragment if not needed anymore
   2331  1.75.4.1      phil 	 * (last fragment older than 1s).
   2332  1.75.4.1      phil 	 * XXX doesn't belong here, move to node_age
   2333  1.75.4.1      phil 	 */
   2334  1.75.4.1      phil 	if (ni->ni_rxfrag[0] != NULL &&
   2335  1.75.4.1      phil 	    ticks > ni->ni_rxfragstamp + hz) {
   2336  1.75.4.1      phil 		m_freem(ni->ni_rxfrag[0]);
   2337  1.75.4.1      phil 		ni->ni_rxfrag[0] = NULL;
   2338  1.75.4.1      phil 	}
   2339  1.75.4.1      phil 	if (ni->ni_inact > 0) {
   2340  1.75.4.1      phil 		ni->ni_inact--;
   2341  1.75.4.1      phil 		IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
   2342  1.75.4.1      phil 		    "%s: inact %u inact_reload %u nrates %u",
   2343  1.75.4.1      phil 		    __func__, ni->ni_inact, ni->ni_inact_reload,
   2344  1.75.4.1      phil 		    ni->ni_rates.rs_nrates);
   2345  1.75.4.1      phil 	}
   2346  1.75.4.1      phil 	/*
   2347  1.75.4.1      phil 	 * Special case ourself; we may be idle for extended periods
   2348  1.75.4.1      phil 	 * of time and regardless reclaiming our state is wrong.
   2349  1.75.4.1      phil 	 * XXX run ic_node_age
   2350  1.75.4.1      phil 	 */
   2351  1.75.4.1      phil 	/* XXX before inact decrement? */
   2352  1.75.4.1      phil 	if (ni == vap->iv_bss)
   2353  1.75.4.1      phil 		return;
   2354  1.75.4.1      phil 	if (ni->ni_associd != 0 ||
   2355  1.75.4.1      phil 	    (vap->iv_opmode == IEEE80211_M_IBSS ||
   2356  1.75.4.1      phil 	     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
   2357  1.75.4.1      phil 		/*
   2358  1.75.4.1      phil 		 * Age/drain resources held by the station.
   2359  1.75.4.1      phil 		 */
   2360  1.75.4.1      phil 		ic->ic_node_age(ni);
   2361  1.75.4.1      phil 		/*
   2362  1.75.4.1      phil 		 * Probe the station before time it out.  We
   2363  1.75.4.1      phil 		 * send a null data frame which may not be
   2364  1.75.4.1      phil 		 * universally supported by drivers (need it
   2365  1.75.4.1      phil 		 * for ps-poll support so it should be...).
   2366  1.75.4.1      phil 		 *
   2367  1.75.4.1      phil 		 * XXX don't probe the station unless we've
   2368  1.75.4.1      phil 		 *     received a frame from them (and have
   2369  1.75.4.1      phil 		 *     some idea of the rates they are capable
   2370  1.75.4.1      phil 		 *     of); this will get fixed more properly
   2371  1.75.4.1      phil 		 *     soon with better handling of the rate set.
   2372  1.75.4.1      phil 		 */
   2373  1.75.4.1      phil 		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
   2374  1.75.4.1      phil 		    (0 < ni->ni_inact &&
   2375  1.75.4.1      phil 		     ni->ni_inact <= vap->iv_inact_probe) &&
   2376  1.75.4.1      phil 		    ni->ni_rates.rs_nrates != 0) {
   2377  1.75.4.1      phil 			IEEE80211_NOTE(vap,
   2378  1.75.4.1      phil 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
   2379  1.75.4.1      phil 			    ni, "%s",
   2380  1.75.4.1      phil 			    "probe station due to inactivity");
   2381  1.75.4.1      phil 			/*
   2382  1.75.4.1      phil 			 * Grab a reference so the node cannot
   2383  1.75.4.1      phil 			 * be reclaimed before we send the frame.
   2384  1.75.4.1      phil 			 * ieee80211_send_nulldata understands
   2385  1.75.4.1      phil 			 * we've done this and reclaims the
   2386  1.75.4.1      phil 			 * ref for us as needed.
   2387  1.75.4.1      phil 			 */
   2388  1.75.4.1      phil 			/* XXX fix this (not required anymore). */
   2389  1.75.4.1      phil 			ieee80211_ref_node(ni);
   2390  1.75.4.1      phil 			/* XXX useless */
   2391  1.75.4.1      phil 			ieee80211_send_nulldata(ni);
   2392  1.75.4.1      phil 			/* XXX stat? */
   2393  1.75.4.1      phil 			return;
   2394  1.75.4.1      phil 		}
   2395      1.45     skrll 	}
   2396  1.75.4.1      phil 	if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
   2397  1.75.4.1      phil 	    ni->ni_inact <= 0) {
   2398  1.75.4.1      phil 		IEEE80211_NOTE(vap,
   2399  1.75.4.1      phil 		    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
   2400  1.75.4.1      phil 		    "station timed out due to inactivity "
   2401  1.75.4.1      phil 		    "(refcnt %u)", ieee80211_node_refcnt(ni));
   2402      1.39    dyoung 		/*
   2403  1.75.4.1      phil 		 * Send a deauthenticate frame and drop the station.
   2404  1.75.4.1      phil 		 * This is somewhat complicated due to reference counts
   2405  1.75.4.1      phil 		 * and locking.  At this point a station will typically
   2406  1.75.4.1      phil 		 * have a reference count of 2.  ieee80211_node_leave
   2407  1.75.4.1      phil 		 * will do a "free" of the node which will drop the
   2408  1.75.4.1      phil 		 * reference count.  But in the meantime a reference
   2409  1.75.4.1      phil 		 * wil be held by the deauth frame.  The actual reclaim
   2410  1.75.4.1      phil 		 * of the node will happen either after the tx is
   2411  1.75.4.1      phil 		 * completed or by ieee80211_node_leave.
   2412      1.39    dyoung 		 */
   2413      1.39    dyoung 		if (ni->ni_associd != 0) {
   2414  1.75.4.1      phil 			IEEE80211_SEND_MGMT(ni,
   2415  1.75.4.1      phil 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
   2416  1.75.4.1      phil 			    IEEE80211_REASON_AUTH_EXPIRE);
   2417      1.39    dyoung 		}
   2418  1.75.4.1      phil 		ieee80211_node_leave(ni);
   2419  1.75.4.1      phil 		vap->iv_stats.is_node_timeout++;
   2420       1.1    dyoung 	}
   2421      1.39    dyoung }
   2422      1.39    dyoung 
   2423      1.39    dyoung /*
   2424  1.75.4.1      phil  * Timeout inactive stations and do related housekeeping.
   2425      1.39    dyoung  */
   2426      1.39    dyoung static void
   2427  1.75.4.1      phil ieee80211_timeout_stations(struct ieee80211com *ic)
   2428       1.1    dyoung {
   2429  1.75.4.1      phil 	struct ieee80211_node_table *nt = &ic->ic_sta;
   2430      1.22   mycroft 
   2431  1.75.4.1      phil 	ieee80211_iterate_nodes(nt, timeout_stations, NULL);
   2432       1.1    dyoung }
   2433       1.1    dyoung 
   2434       1.9    dyoung /*
   2435  1.75.4.1      phil  * Aggressively reclaim resources.  This should be used
   2436  1.75.4.1      phil  * only in a critical situation to reclaim mbuf resources.
   2437       1.9    dyoung  */
   2438  1.75.4.1      phil void
   2439  1.75.4.1      phil ieee80211_drain(struct ieee80211com *ic)
   2440       1.1    dyoung {
   2441  1.75.4.1      phil 	struct ieee80211_node_table *nt = &ic->ic_sta;
   2442  1.75.4.1      phil 	struct ieee80211vap *vap;
   2443       1.9    dyoung 	struct ieee80211_node *ni;
   2444      1.66  christos 
   2445      1.39    dyoung 	IEEE80211_NODE_LOCK(nt);
   2446      1.39    dyoung 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   2447      1.39    dyoung 		/*
   2448      1.42    dyoung 		 * Ignore entries for which have yet to receive an
   2449      1.42    dyoung 		 * authentication frame.  These are transient and
   2450      1.42    dyoung 		 * will be reclaimed when the last reference to them
   2451      1.42    dyoung 		 * goes away (when frame xmits complete).
   2452      1.42    dyoung 		 */
   2453  1.75.4.1      phil 		vap = ni->ni_vap;
   2454  1.75.4.1      phil 		/*
   2455  1.75.4.1      phil 		 * Only process stations when in RUN state.  This
   2456  1.75.4.1      phil 		 * insures, for example, that we don't timeout an
   2457  1.75.4.1      phil 		 * inactive station during CAC.  Note that CSA state
   2458  1.75.4.1      phil 		 * is actually handled in ieee80211_node_timeout as
   2459  1.75.4.1      phil 		 * it applies to more than timeout processing.
   2460  1.75.4.1      phil 		 */
   2461  1.75.4.1      phil 		if (vap->iv_state != IEEE80211_S_RUN)
   2462  1.75.4.1      phil 			continue;
   2463  1.75.4.1      phil 		/* XXX can vap be NULL? */
   2464  1.75.4.1      phil 		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
   2465  1.75.4.1      phil 		     vap->iv_opmode == IEEE80211_M_STA) &&
   2466      1.45     skrll 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
   2467      1.42    dyoung 			continue;
   2468      1.42    dyoung 		/*
   2469  1.75.4.1      phil 		 * Free fragments.
   2470  1.75.4.1      phil 		 * XXX doesn't belong here, move to node_drain
   2471      1.39    dyoung 		 */
   2472  1.75.4.1      phil 		if (ni->ni_rxfrag[0] != NULL) {
   2473      1.39    dyoung 			m_freem(ni->ni_rxfrag[0]);
   2474      1.39    dyoung 			ni->ni_rxfrag[0] = NULL;
   2475      1.39    dyoung 		}
   2476      1.39    dyoung 		/*
   2477  1.75.4.1      phil 		 * Drain resources held by the station.
   2478      1.39    dyoung 		 */
   2479  1.75.4.1      phil 		ic->ic_node_drain(ni);
   2480       1.1    dyoung 	}
   2481      1.39    dyoung 	IEEE80211_NODE_UNLOCK(nt);
   2482       1.1    dyoung }
   2483       1.1    dyoung 
   2484  1.75.4.1      phil /*
   2485  1.75.4.1      phil  * Per-ieee80211com inactivity timer callback.
   2486  1.75.4.1      phil  */
   2487       1.1    dyoung void
   2488  1.75.4.1      phil ieee80211_node_timeout(void *arg)
   2489       1.1    dyoung {
   2490  1.75.4.1      phil 	struct ieee80211com *ic = arg;
   2491      1.39    dyoung 
   2492  1.75.4.1      phil 	/*
   2493  1.75.4.1      phil 	 * Defer timeout processing if a channel switch is pending.
   2494  1.75.4.1      phil 	 * We typically need to be mute so not doing things that
   2495  1.75.4.1      phil 	 * might generate frames is good to handle in one place.
   2496  1.75.4.1      phil 	 * Suppressing the station timeout processing may extend the
   2497  1.75.4.1      phil 	 * lifetime of inactive stations (by not decrementing their
   2498  1.75.4.1      phil 	 * idle counters) but this should be ok unless the CSA is
   2499  1.75.4.1      phil 	 * active for an unusually long time.
   2500  1.75.4.1      phil 	 */
   2501  1.75.4.1      phil 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
   2502  1.75.4.1      phil 		ieee80211_scan_timeout(ic);
   2503  1.75.4.1      phil 		ieee80211_timeout_stations(ic);
   2504  1.75.4.1      phil 		ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
   2505  1.75.4.1      phil 
   2506  1.75.4.1      phil 		IEEE80211_LOCK(ic);
   2507  1.75.4.1      phil 		ieee80211_erp_timeout(ic);
   2508  1.75.4.1      phil 		ieee80211_ht_timeout(ic);
   2509  1.75.4.1      phil 		ieee80211_vht_timeout(ic);
   2510  1.75.4.1      phil 		IEEE80211_UNLOCK(ic);
   2511      1.66  christos 	}
   2512  1.75.4.1      phil 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
   2513  1.75.4.1      phil 		ieee80211_node_timeout, ic);
   2514  1.75.4.1      phil }
   2515  1.75.4.1      phil 
   2516  1.75.4.1      phil /*
   2517  1.75.4.1      phil  * The same as ieee80211_iterate_nodes(), but for one vap only.
   2518  1.75.4.1      phil  */
   2519  1.75.4.1      phil int
   2520  1.75.4.1      phil ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
   2521  1.75.4.1      phil     struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
   2522  1.75.4.1      phil {
   2523  1.75.4.1      phil 	struct ieee80211_node **ni_arr;
   2524  1.75.4.1      phil 	struct ieee80211_node *ni;
   2525  1.75.4.1      phil 	size_t size;
   2526  1.75.4.1      phil 	int count, i;
   2527      1.66  christos 
   2528  1.75.4.1      phil 	/*
   2529  1.75.4.1      phil 	 * Iterate over the node table and save an array of ref'ed nodes.
   2530  1.75.4.1      phil 	 *
   2531  1.75.4.1      phil 	 * This is separated out from calling the actual node function so that
   2532  1.75.4.1      phil 	 * no LORs will occur.
   2533  1.75.4.1      phil 	 */
   2534      1.39    dyoung 	IEEE80211_NODE_LOCK(nt);
   2535  1.75.4.1      phil 	count = nt->nt_count;
   2536  1.75.4.1      phil 	size = count * sizeof(struct ieee80211_node *);
   2537  1.75.4.1      phil 	ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
   2538  1.75.4.1      phil 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   2539  1.75.4.1      phil 	if (ni_arr == NULL) {
   2540  1.75.4.1      phil 		IEEE80211_NODE_UNLOCK(nt);
   2541  1.75.4.1      phil 		return (ENOMEM);
   2542  1.75.4.1      phil 	}
   2543  1.75.4.1      phil 
   2544  1.75.4.1      phil 	i = 0;
   2545      1.39    dyoung 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   2546  1.75.4.1      phil 		if (vap != NULL && ni->ni_vap != vap)
   2547  1.75.4.1      phil 			continue;
   2548  1.75.4.1      phil 		KASSERT(i < count,
   2549  1.75.4.1      phil 		    ("node array overflow (vap %p, i %d, count %d)\n",
   2550  1.75.4.1      phil 		    vap, i, count));
   2551  1.75.4.1      phil 		ni_arr[i] = ieee80211_ref_node(ni);
   2552  1.75.4.1      phil 		i++;
   2553      1.39    dyoung 	}
   2554      1.39    dyoung 	IEEE80211_NODE_UNLOCK(nt);
   2555  1.75.4.1      phil 
   2556  1.75.4.1      phil 	for (i = 0; i < count; i++) {
   2557  1.75.4.1      phil 		if (ni_arr[i] == NULL)	/* end of the list */
   2558  1.75.4.1      phil 			break;
   2559  1.75.4.1      phil 		(*f)(arg, ni_arr[i]);
   2560  1.75.4.1      phil 		/* ieee80211_free_node() locks by itself */
   2561  1.75.4.1      phil 		ieee80211_free_node(ni_arr[i]);
   2562  1.75.4.1      phil 	}
   2563  1.75.4.1      phil 
   2564  1.75.4.1      phil 	IEEE80211_FREE(ni_arr, M_80211_NODE);
   2565  1.75.4.1      phil 
   2566  1.75.4.1      phil 	return (0);
   2567      1.39    dyoung }
   2568      1.39    dyoung 
   2569  1.75.4.1      phil /*
   2570  1.75.4.1      phil  * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
   2571  1.75.4.1      phil  * reference in the source.
   2572  1.75.4.1      phil  */
   2573      1.39    dyoung void
   2574  1.75.4.1      phil ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
   2575  1.75.4.1      phil 	ieee80211_iter_func *f, void *arg)
   2576  1.75.4.1      phil {
   2577  1.75.4.1      phil 	/* XXX no way to pass error to the caller. */
   2578  1.75.4.1      phil 	(void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
   2579  1.75.4.1      phil }
   2580  1.75.4.1      phil 
   2581  1.75.4.1      phil void
   2582  1.75.4.1      phil ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
   2583      1.39    dyoung {
   2584      1.39    dyoung 	printf("0x%p: mac %s refcnt %d\n", ni,
   2585      1.39    dyoung 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
   2586  1.75.4.1      phil 	printf("\tauthmode %u flags 0x%x\n",
   2587  1.75.4.1      phil 		ni->ni_authmode, ni->ni_flags);
   2588      1.39    dyoung 	printf("\tassocid 0x%x txpower %u vlan %u\n",
   2589      1.39    dyoung 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
   2590      1.39    dyoung 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
   2591  1.75.4.1      phil 		ni->ni_txseqs[IEEE80211_NONQOS_TID],
   2592  1.75.4.1      phil 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
   2593  1.75.4.1      phil 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
   2594      1.39    dyoung 		ni->ni_rxfragstamp);
   2595  1.75.4.1      phil 	printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
   2596  1.75.4.1      phil 		node_getrssi(ni), ni->ni_noise,
   2597  1.75.4.1      phil 		ni->ni_intval, ni->ni_capinfo);
   2598      1.39    dyoung 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
   2599      1.39    dyoung 		ether_sprintf(ni->ni_bssid),
   2600      1.39    dyoung 		ni->ni_esslen, ni->ni_essid,
   2601      1.39    dyoung 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
   2602  1.75.4.1      phil 	printf("\tinact %u inact_reload %u txrate %u\n",
   2603  1.75.4.1      phil 		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
   2604  1.75.4.1      phil 	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
   2605  1.75.4.1      phil 		ni->ni_htcap, ni->ni_htparam,
   2606  1.75.4.1      phil 		ni->ni_htctlchan, ni->ni_ht2ndchan);
   2607  1.75.4.1      phil 	printf("\thtopmode %x htstbc %x htchw %u\n",
   2608  1.75.4.1      phil 		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
   2609  1.75.4.1      phil 	printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
   2610  1.75.4.1      phil 		ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
   2611  1.75.4.1      phil 		(int) ni->ni_vht_basicmcs);
   2612  1.75.4.1      phil 	/* XXX VHT state */
   2613      1.39    dyoung }
   2614      1.39    dyoung 
   2615      1.39    dyoung void
   2616      1.39    dyoung ieee80211_dump_nodes(struct ieee80211_node_table *nt)
   2617      1.39    dyoung {
   2618      1.39    dyoung 	ieee80211_iterate_nodes(nt,
   2619      1.39    dyoung 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
   2620      1.39    dyoung }
   2621      1.39    dyoung 
   2622  1.75.4.1      phil static void
   2623  1.75.4.1      phil ieee80211_notify_erp_locked(struct ieee80211com *ic)
   2624  1.75.4.1      phil {
   2625  1.75.4.1      phil 	struct ieee80211vap *vap;
   2626  1.75.4.1      phil 
   2627  1.75.4.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   2628  1.75.4.1      phil 
   2629  1.75.4.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
   2630  1.75.4.1      phil 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
   2631  1.75.4.1      phil 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
   2632  1.75.4.1      phil }
   2633  1.75.4.1      phil 
   2634  1.75.4.1      phil void
   2635  1.75.4.1      phil ieee80211_notify_erp(struct ieee80211com *ic)
   2636  1.75.4.1      phil {
   2637  1.75.4.1      phil 	IEEE80211_LOCK(ic);
   2638  1.75.4.1      phil 	ieee80211_notify_erp_locked(ic);
   2639  1.75.4.1      phil 	IEEE80211_UNLOCK(ic);
   2640  1.75.4.1      phil }
   2641  1.75.4.1      phil 
   2642      1.39    dyoung /*
   2643      1.39    dyoung  * Handle a station joining an 11g network.
   2644      1.39    dyoung  */
   2645      1.39    dyoung static void
   2646  1.75.4.1      phil ieee80211_node_join_11g(struct ieee80211_node *ni)
   2647      1.39    dyoung {
   2648  1.75.4.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   2649  1.75.4.1      phil 
   2650  1.75.4.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   2651       1.1    dyoung 
   2652      1.39    dyoung 	/*
   2653      1.39    dyoung 	 * Station isn't capable of short slot time.  Bump
   2654      1.39    dyoung 	 * the count of long slot time stations and disable
   2655      1.39    dyoung 	 * use of short slot time.  Note that the actual switch
   2656      1.39    dyoung 	 * over to long slot time use may not occur until the
   2657      1.39    dyoung 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
   2658      1.39    dyoung 	 */
   2659      1.39    dyoung 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
   2660      1.39    dyoung 		ic->ic_longslotsta++;
   2661  1.75.4.1      phil 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
   2662  1.75.4.1      phil 		    "station needs long slot time, count %d",
   2663  1.75.4.1      phil 		    ic->ic_longslotsta);
   2664      1.39    dyoung 		/* XXX vap's w/ conflicting needs won't work */
   2665  1.75.4.1      phil 		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
   2666  1.75.4.1      phil 			/*
   2667  1.75.4.1      phil 			 * Don't force slot time when switched to turbo
   2668  1.75.4.1      phil 			 * mode as non-ERP stations won't be present; this
   2669  1.75.4.1      phil 			 * need only be done when on the normal G channel.
   2670  1.75.4.1      phil 			 */
   2671  1.75.4.1      phil 			ieee80211_set_shortslottime(ic, 0);
   2672  1.75.4.1      phil 		}
   2673      1.39    dyoung 	}
   2674      1.39    dyoung 	/*
   2675      1.39    dyoung 	 * If the new station is not an ERP station
   2676      1.39    dyoung 	 * then bump the counter and enable protection
   2677      1.39    dyoung 	 * if configured.
   2678      1.39    dyoung 	 */
   2679  1.75.4.1      phil 	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
   2680      1.39    dyoung 		ic->ic_nonerpsta++;
   2681  1.75.4.1      phil 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
   2682  1.75.4.1      phil 		    "station is !ERP, %d non-ERP stations associated",
   2683  1.75.4.1      phil 		    ic->ic_nonerpsta);
   2684      1.39    dyoung 		/*
   2685      1.39    dyoung 		 * If station does not support short preamble
   2686      1.39    dyoung 		 * then we must enable use of Barker preamble.
   2687      1.39    dyoung 		 */
   2688      1.39    dyoung 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
   2689  1.75.4.1      phil 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
   2690  1.75.4.1      phil 			    "%s", "station needs long preamble");
   2691      1.39    dyoung 			ic->ic_flags |= IEEE80211_F_USEBARKER;
   2692      1.39    dyoung 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
   2693      1.39    dyoung 		}
   2694  1.75.4.1      phil 		/*
   2695  1.75.4.1      phil 		 * If protection is configured and this is the first
   2696  1.75.4.1      phil 		 * indication we should use protection, enable it.
   2697  1.75.4.1      phil 		 */
   2698  1.75.4.1      phil 		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
   2699  1.75.4.1      phil 		    ic->ic_nonerpsta == 1 &&
   2700  1.75.4.1      phil 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
   2701  1.75.4.1      phil 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
   2702  1.75.4.1      phil 			    "%s: enable use of protection\n", __func__);
   2703  1.75.4.1      phil 			ic->ic_flags |= IEEE80211_F_USEPROT;
   2704  1.75.4.1      phil 			ieee80211_notify_erp_locked(ic);
   2705  1.75.4.1      phil 		}
   2706  1.75.4.1      phil 	} else
   2707      1.39    dyoung 		ni->ni_flags |= IEEE80211_NODE_ERP;
   2708       1.1    dyoung }
   2709      1.22   mycroft 
   2710      1.22   mycroft void
   2711  1.75.4.1      phil ieee80211_node_join(struct ieee80211_node *ni, int resp)
   2712      1.22   mycroft {
   2713  1.75.4.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   2714  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   2715      1.22   mycroft 	int newassoc;
   2716      1.22   mycroft 
   2717      1.22   mycroft 	if (ni->ni_associd == 0) {
   2718  1.75.4.1      phil 		uint16_t aid;
   2719      1.22   mycroft 
   2720  1.75.4.1      phil 		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
   2721      1.22   mycroft 		/*
   2722      1.39    dyoung 		 * It would be good to search the bitmap
   2723      1.22   mycroft 		 * more efficiently, but this will do for now.
   2724      1.22   mycroft 		 */
   2725  1.75.4.1      phil 		for (aid = 1; aid < vap->iv_max_aid; aid++) {
   2726  1.75.4.1      phil 			if (!IEEE80211_AID_ISSET(vap, aid))
   2727      1.22   mycroft 				break;
   2728      1.22   mycroft 		}
   2729  1.75.4.1      phil 		if (aid >= vap->iv_max_aid) {
   2730  1.75.4.1      phil 			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
   2731  1.75.4.1      phil 			ieee80211_node_leave(ni);
   2732      1.22   mycroft 			return;
   2733      1.22   mycroft 		}
   2734      1.22   mycroft 		ni->ni_associd = aid | 0xc000;
   2735  1.75.4.1      phil 		ni->ni_jointime = time_uptime;
   2736  1.75.4.1      phil 		IEEE80211_LOCK(ic);
   2737  1.75.4.1      phil 		IEEE80211_AID_SET(vap, ni->ni_associd);
   2738  1.75.4.1      phil 		vap->iv_sta_assoc++;
   2739      1.39    dyoung 		ic->ic_sta_assoc++;
   2740  1.75.4.1      phil 
   2741  1.75.4.1      phil 		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
   2742  1.75.4.1      phil 			ieee80211_ht_node_join(ni);
   2743  1.75.4.1      phil 		if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
   2744  1.75.4.1      phil 			ieee80211_vht_node_join(ni);
   2745  1.75.4.1      phil 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
   2746  1.75.4.1      phil 		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
   2747  1.75.4.1      phil 			ieee80211_node_join_11g(ni);
   2748  1.75.4.1      phil 		IEEE80211_UNLOCK(ic);
   2749  1.75.4.1      phil 
   2750      1.22   mycroft 		newassoc = 1;
   2751      1.22   mycroft 	} else
   2752      1.22   mycroft 		newassoc = 0;
   2753      1.22   mycroft 
   2754  1.75.4.1      phil 	/*
   2755  1.75.4.1      phil 	 * XXX VHT - should log VHT channel width, etc
   2756  1.75.4.1      phil 	 */
   2757  1.75.4.1      phil 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
   2758  1.75.4.1      phil 	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
   2759      1.39    dyoung 	    IEEE80211_NODE_AID(ni),
   2760      1.39    dyoung 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
   2761      1.39    dyoung 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
   2762      1.39    dyoung 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
   2763  1.75.4.1      phil 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
   2764  1.75.4.1      phil 	    /* XXX update for VHT string */
   2765  1.75.4.1      phil 	    ni->ni_flags & IEEE80211_NODE_HT ?
   2766  1.75.4.1      phil 		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
   2767  1.75.4.1      phil 	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
   2768  1.75.4.1      phil 	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
   2769  1.75.4.1      phil 	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
   2770  1.75.4.1      phil 	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
   2771  1.75.4.1      phil 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
   2772  1.75.4.1      phil 		", fast-frames" : "",
   2773  1.75.4.1      phil 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
   2774  1.75.4.1      phil 		", turbo" : ""
   2775      1.39    dyoung 	);
   2776      1.22   mycroft 
   2777  1.75.4.1      phil 	ieee80211_node_setuptxparms(ni);
   2778  1.75.4.1      phil 	ieee80211_ratectl_node_init(ni);
   2779      1.22   mycroft 	/* give driver a chance to setup state like ni_txrate */
   2780      1.39    dyoung 	if (ic->ic_newassoc != NULL)
   2781      1.45     skrll 		ic->ic_newassoc(ni, newassoc);
   2782  1.75.4.1      phil 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
   2783      1.39    dyoung 	/* tell the authenticator about new station */
   2784  1.75.4.1      phil 	if (vap->iv_auth->ia_node_join != NULL)
   2785  1.75.4.1      phil 		vap->iv_auth->ia_node_join(ni);
   2786  1.75.4.1      phil 	ieee80211_notify_node_join(ni,
   2787  1.75.4.1      phil 	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
   2788  1.75.4.1      phil }
   2789  1.75.4.1      phil 
   2790  1.75.4.1      phil static void
   2791  1.75.4.1      phil disable_protection(struct ieee80211com *ic)
   2792  1.75.4.1      phil {
   2793  1.75.4.1      phil 	KASSERT(ic->ic_nonerpsta == 0 &&
   2794  1.75.4.1      phil 	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
   2795  1.75.4.1      phil 	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
   2796  1.75.4.1      phil 	   ic->ic_flags_ext));
   2797  1.75.4.1      phil 
   2798  1.75.4.1      phil 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
   2799  1.75.4.1      phil 	/* XXX verify mode? */
   2800  1.75.4.1      phil 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
   2801  1.75.4.1      phil 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
   2802  1.75.4.1      phil 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
   2803  1.75.4.1      phil 	}
   2804  1.75.4.1      phil 	ieee80211_notify_erp_locked(ic);
   2805      1.39    dyoung }
   2806      1.39    dyoung 
   2807      1.39    dyoung /*
   2808      1.39    dyoung  * Handle a station leaving an 11g network.
   2809      1.39    dyoung  */
   2810      1.39    dyoung static void
   2811  1.75.4.1      phil ieee80211_node_leave_11g(struct ieee80211_node *ni)
   2812      1.39    dyoung {
   2813  1.75.4.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   2814  1.75.4.1      phil 
   2815  1.75.4.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   2816      1.39    dyoung 
   2817  1.75.4.1      phil 	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
   2818  1.75.4.1      phil 	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
   2819  1.75.4.1      phil 	      ic->ic_bsschan->ic_flags));
   2820      1.39    dyoung 
   2821      1.39    dyoung 	/*
   2822      1.39    dyoung 	 * If a long slot station do the slot time bookkeeping.
   2823      1.39    dyoung 	 */
   2824      1.39    dyoung 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
   2825  1.75.4.1      phil 		KASSERT(ic->ic_longslotsta > 0,
   2826      1.39    dyoung 		    ("bogus long slot station count %d", ic->ic_longslotsta));
   2827      1.39    dyoung 		ic->ic_longslotsta--;
   2828  1.75.4.1      phil 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
   2829  1.75.4.1      phil 		    "long slot time station leaves, count now %d",
   2830  1.75.4.1      phil 		    ic->ic_longslotsta);
   2831      1.39    dyoung 		if (ic->ic_longslotsta == 0) {
   2832      1.39    dyoung 			/*
   2833      1.39    dyoung 			 * Re-enable use of short slot time if supported
   2834      1.39    dyoung 			 * and not operating in IBSS mode (per spec).
   2835      1.39    dyoung 			 */
   2836      1.39    dyoung 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
   2837      1.39    dyoung 			    ic->ic_opmode != IEEE80211_M_IBSS) {
   2838  1.75.4.1      phil 				IEEE80211_DPRINTF(ni->ni_vap,
   2839  1.75.4.1      phil 				    IEEE80211_MSG_ASSOC,
   2840      1.39    dyoung 				    "%s: re-enable use of short slot time\n",
   2841      1.39    dyoung 				    __func__);
   2842      1.39    dyoung 				ieee80211_set_shortslottime(ic, 1);
   2843      1.39    dyoung 			}
   2844      1.39    dyoung 		}
   2845      1.39    dyoung 	}
   2846      1.39    dyoung 	/*
   2847      1.39    dyoung 	 * If a non-ERP station do the protection-related bookkeeping.
   2848      1.39    dyoung 	 */
   2849      1.39    dyoung 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
   2850  1.75.4.1      phil 		KASSERT(ic->ic_nonerpsta > 0,
   2851      1.39    dyoung 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
   2852      1.39    dyoung 		ic->ic_nonerpsta--;
   2853  1.75.4.1      phil 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
   2854  1.75.4.1      phil 		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
   2855  1.75.4.1      phil 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
   2856  1.75.4.1      phil 			" (non-ERP sta present)" : "");
   2857  1.75.4.1      phil 		if (ic->ic_nonerpsta == 0 &&
   2858  1.75.4.1      phil 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
   2859  1.75.4.1      phil 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
   2860      1.39    dyoung 				"%s: disable use of protection\n", __func__);
   2861  1.75.4.1      phil 			disable_protection(ic);
   2862      1.39    dyoung 		}
   2863      1.39    dyoung 	}
   2864      1.22   mycroft }
   2865      1.22   mycroft 
   2866      1.22   mycroft /*
   2867  1.75.4.1      phil  * Time out presence of an overlapping bss with non-ERP
   2868  1.75.4.1      phil  * stations.  When operating in hostap mode we listen for
   2869  1.75.4.1      phil  * beacons from other stations and if we identify a non-ERP
   2870  1.75.4.1      phil  * station is present we enable protection.  To identify
   2871  1.75.4.1      phil  * when all non-ERP stations are gone we time out this
   2872  1.75.4.1      phil  * condition.
   2873  1.75.4.1      phil  */
   2874  1.75.4.1      phil static void
   2875  1.75.4.1      phil ieee80211_erp_timeout(struct ieee80211com *ic)
   2876  1.75.4.1      phil {
   2877  1.75.4.1      phil 
   2878  1.75.4.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   2879  1.75.4.1      phil 
   2880  1.75.4.1      phil 	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
   2881  1.75.4.1      phil 	    ieee80211_time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
   2882  1.75.4.1      phil #if 0
   2883  1.75.4.1      phil 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
   2884  1.75.4.1      phil 		    "%s", "age out non-ERP sta present on channel");
   2885  1.75.4.1      phil #endif
   2886  1.75.4.1      phil 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
   2887  1.75.4.1      phil 		if (ic->ic_nonerpsta == 0)
   2888  1.75.4.1      phil 			disable_protection(ic);
   2889  1.75.4.1      phil 	}
   2890  1.75.4.1      phil }
   2891  1.75.4.1      phil 
   2892  1.75.4.1      phil /*
   2893      1.26    dyoung  * Handle bookkeeping for station deauthentication/disassociation
   2894      1.26    dyoung  * when operating as an ap.
   2895      1.22   mycroft  */
   2896      1.22   mycroft void
   2897  1.75.4.1      phil ieee80211_node_leave(struct ieee80211_node *ni)
   2898      1.22   mycroft {
   2899  1.75.4.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   2900  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   2901      1.39    dyoung 	struct ieee80211_node_table *nt = ni->ni_table;
   2902      1.39    dyoung 
   2903  1.75.4.1      phil 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
   2904  1.75.4.1      phil 	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
   2905      1.75      maxv 
   2906  1.75.4.1      phil 	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
   2907  1.75.4.1      phil 		("unexpected operating mode %u", vap->iv_opmode));
   2908      1.22   mycroft 	/*
   2909      1.22   mycroft 	 * If node wasn't previously associated all
   2910      1.22   mycroft 	 * we need to do is reclaim the reference.
   2911      1.22   mycroft 	 */
   2912      1.39    dyoung 	/* XXX ibss mode bypasses 11g and notification */
   2913      1.22   mycroft 	if (ni->ni_associd == 0)
   2914      1.39    dyoung 		goto done;
   2915      1.39    dyoung 	/*
   2916      1.39    dyoung 	 * Tell the authenticator the station is leaving.
   2917      1.39    dyoung 	 * Note that we must do this before yanking the
   2918      1.39    dyoung 	 * association id as the authenticator uses the
   2919  1.75.4.1      phil 	 * associd to locate it's state block.
   2920      1.39    dyoung 	 */
   2921  1.75.4.1      phil 	if (vap->iv_auth->ia_node_leave != NULL)
   2922  1.75.4.1      phil 		vap->iv_auth->ia_node_leave(ni);
   2923      1.39    dyoung 
   2924  1.75.4.1      phil 	IEEE80211_LOCK(ic);
   2925  1.75.4.1      phil 	IEEE80211_AID_CLR(vap, ni->ni_associd);
   2926  1.75.4.1      phil 	vap->iv_sta_assoc--;
   2927  1.75.4.1      phil 	ic->ic_sta_assoc--;
   2928      1.75      maxv 
   2929  1.75.4.1      phil 	if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
   2930  1.75.4.1      phil 		ieee80211_vht_node_leave(ni);
   2931  1.75.4.1      phil 	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
   2932  1.75.4.1      phil 		ieee80211_ht_node_leave(ni);
   2933  1.75.4.1      phil 	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
   2934  1.75.4.1      phil 	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
   2935  1.75.4.1      phil 		ieee80211_node_leave_11g(ni);
   2936  1.75.4.1      phil 	IEEE80211_UNLOCK(ic);
   2937      1.39    dyoung 	/*
   2938      1.39    dyoung 	 * Cleanup station state.  In particular clear various
   2939      1.39    dyoung 	 * state that might otherwise be reused if the node
   2940      1.39    dyoung 	 * is reused before the reference count goes to zero
   2941      1.39    dyoung 	 * (and memory is reclaimed).
   2942      1.39    dyoung 	 */
   2943  1.75.4.1      phil 	ieee80211_sta_leave(ni);
   2944      1.39    dyoung done:
   2945      1.39    dyoung 	/*
   2946      1.39    dyoung 	 * Remove the node from any table it's recorded in and
   2947      1.39    dyoung 	 * drop the caller's reference.  Removal from the table
   2948      1.39    dyoung 	 * is important to insure the node is not reprocessed
   2949      1.39    dyoung 	 * for inactivity.
   2950      1.39    dyoung 	 */
   2951      1.39    dyoung 	if (nt != NULL) {
   2952      1.39    dyoung 		IEEE80211_NODE_LOCK(nt);
   2953      1.39    dyoung 		node_reclaim(nt, ni);
   2954      1.39    dyoung 		IEEE80211_NODE_UNLOCK(nt);
   2955      1.39    dyoung 	} else
   2956      1.39    dyoung 		ieee80211_free_node(ni);
   2957      1.39    dyoung }
   2958      1.39    dyoung 
   2959  1.75.4.1      phil struct rssiinfo {
   2960  1.75.4.1      phil 	int	rssi_samples;
   2961  1.75.4.1      phil 	uint32_t rssi_total;
   2962  1.75.4.1      phil };
   2963  1.75.4.1      phil 
   2964  1.75.4.1      phil static void
   2965  1.75.4.1      phil get_hostap_rssi(void *arg, struct ieee80211_node *ni)
   2966      1.39    dyoung {
   2967  1.75.4.1      phil 	struct rssiinfo *info = arg;
   2968  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   2969  1.75.4.1      phil 	int8_t rssi;
   2970      1.39    dyoung 
   2971  1.75.4.1      phil 	/* only associated stations */
   2972  1.75.4.1      phil 	if (ni->ni_associd == 0)
   2973  1.75.4.1      phil 		return;
   2974  1.75.4.1      phil 	rssi = vap->iv_ic->ic_node_getrssi(ni);
   2975  1.75.4.1      phil 	if (rssi != 0) {
   2976  1.75.4.1      phil 		info->rssi_samples++;
   2977  1.75.4.1      phil 		info->rssi_total += rssi;
   2978      1.39    dyoung 	}
   2979      1.39    dyoung }
   2980      1.39    dyoung 
   2981      1.39    dyoung static void
   2982  1.75.4.1      phil get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
   2983      1.39    dyoung {
   2984  1.75.4.1      phil 	struct rssiinfo *info = arg;
   2985  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   2986  1.75.4.1      phil 	int8_t rssi;
   2987  1.75.4.1      phil 
   2988  1.75.4.1      phil 	/* only neighbors */
   2989  1.75.4.1      phil 	/* XXX check bssid */
   2990  1.75.4.1      phil 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
   2991  1.75.4.1      phil 		return;
   2992  1.75.4.1      phil 	rssi = vap->iv_ic->ic_node_getrssi(ni);
   2993  1.75.4.1      phil 	if (rssi != 0) {
   2994  1.75.4.1      phil 		info->rssi_samples++;
   2995  1.75.4.1      phil 		info->rssi_total += rssi;
   2996      1.39    dyoung 	}
   2997      1.39    dyoung }
   2998      1.39    dyoung 
   2999  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_MESH
   3000      1.39    dyoung static void
   3001  1.75.4.1      phil get_mesh_rssi(void *arg, struct ieee80211_node *ni)
   3002      1.39    dyoung {
   3003  1.75.4.1      phil 	struct rssiinfo *info = arg;
   3004  1.75.4.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   3005  1.75.4.1      phil 	int8_t rssi;
   3006      1.39    dyoung 
   3007  1.75.4.1      phil 	/* only neighbors that peered successfully */
   3008  1.75.4.1      phil 	if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
   3009  1.75.4.1      phil 		return;
   3010  1.75.4.1      phil 	rssi = vap->iv_ic->ic_node_getrssi(ni);
   3011  1.75.4.1      phil 	if (rssi != 0) {
   3012  1.75.4.1      phil 		info->rssi_samples++;
   3013  1.75.4.1      phil 		info->rssi_total += rssi;
   3014  1.75.4.1      phil 	}
   3015      1.39    dyoung }
   3016  1.75.4.1      phil #endif /* IEEE80211_SUPPORT_MESH */
   3017      1.39    dyoung 
   3018  1.75.4.1      phil int8_t
   3019  1.75.4.1      phil ieee80211_getrssi(struct ieee80211vap *vap)
   3020      1.39    dyoung {
   3021  1.75.4.1      phil #define	NZ(x)	((x) == 0 ? 1 : (x))
   3022  1.75.4.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   3023  1.75.4.1      phil 	struct rssiinfo info;
   3024      1.39    dyoung 
   3025  1.75.4.1      phil 	info.rssi_total = 0;
   3026  1.75.4.1      phil 	info.rssi_samples = 0;
   3027  1.75.4.1      phil 	switch (vap->iv_opmode) {
   3028  1.75.4.1      phil 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
   3029  1.75.4.1      phil 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
   3030  1.75.4.1      phil 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
   3031  1.75.4.1      phil 		    &info);
   3032  1.75.4.1      phil 		break;
   3033  1.75.4.1      phil 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
   3034  1.75.4.1      phil 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
   3035  1.75.4.1      phil 		    &info);
   3036  1.75.4.1      phil 		break;
   3037  1.75.4.1      phil #ifdef IEEE80211_SUPPORT_MESH
   3038  1.75.4.1      phil 	case IEEE80211_M_MBSS:		/* average of all mesh neighbors */
   3039  1.75.4.1      phil 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
   3040  1.75.4.1      phil 		    &info);
   3041  1.75.4.1      phil 		break;
   3042  1.75.4.1      phil #endif
   3043  1.75.4.1      phil 	case IEEE80211_M_MONITOR:	/* XXX */
   3044  1.75.4.1      phil 	case IEEE80211_M_STA:		/* use stats from associated ap */
   3045  1.75.4.1      phil 	default:
   3046  1.75.4.1      phil 		if (vap->iv_bss != NULL)
   3047  1.75.4.1      phil 			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
   3048  1.75.4.1      phil 		info.rssi_samples = 1;
   3049  1.75.4.1      phil 		break;
   3050  1.75.4.1      phil 	}
   3051  1.75.4.1      phil 	return info.rssi_total / NZ(info.rssi_samples);
   3052  1.75.4.1      phil #undef NZ
   3053      1.39    dyoung }
   3054      1.39    dyoung 
   3055  1.75.4.1      phil void
   3056  1.75.4.1      phil ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
   3057      1.39    dyoung {
   3058      1.39    dyoung 
   3059  1.75.4.1      phil 	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
   3060  1.75.4.1      phil 		return;
   3061  1.75.4.1      phil 	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
   3062  1.75.4.1      phil 	/* for non-station mode return avg'd rssi accounting */
   3063  1.75.4.1      phil 	if (vap->iv_opmode != IEEE80211_M_STA)
   3064  1.75.4.1      phil 		*rssi = ieee80211_getrssi(vap);
   3065      1.22   mycroft }
   3066