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