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