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