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