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