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