ieee80211_node.c revision 1.48 1 /* $NetBSD: ieee80211_node.c,v 1.48 2005/12/29 22:13:40 dyoung 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.48 2005/12/29 22:13:40 dyoung 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 MALLOC(ic->ic_aid_bitmap, u_int32_t *,
134 howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
135 M_DEVBUF, M_NOWAIT | M_ZERO);
136 if (ic->ic_aid_bitmap == NULL) {
137 /* XXX no way to recover */
138 printf("%s: no memory for AID bitmap!\n", __func__);
139 ic->ic_max_aid = 0;
140 }
141
142 /* XXX defer until using hostap/ibss mode */
143 ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
144 MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
145 M_DEVBUF, M_NOWAIT | M_ZERO);
146 if (ic->ic_tim_bitmap == NULL) {
147 /* XXX no way to recover */
148 printf("%s: no memory for TIM bitmap!\n", __func__);
149 }
150
151 ieee80211_node_table_init(ic, &ic->ic_sta, "station",
152 IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix,
153 ieee80211_timeout_stations);
154 ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
155 IEEE80211_INACT_SCAN, 0,
156 ieee80211_timeout_scan_candidates);
157
158 ieee80211_reset_bss(ic);
159 /*
160 * Setup "global settings" in the bss node so that
161 * each new station automatically inherits them.
162 */
163 rsn = &ic->ic_bss->ni_rsn;
164 /* WEP, TKIP, and AES-CCM are always supported */
165 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
166 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
167 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
168 if (ic->ic_caps & IEEE80211_C_AES)
169 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
170 if (ic->ic_caps & IEEE80211_C_CKIP)
171 rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
172 /*
173 * Default unicast cipher to WEP for 802.1x use. If
174 * WPA is enabled the management code will set these
175 * values to reflect.
176 */
177 rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
178 rsn->rsn_ucastkeylen = 104 / NBBY;
179 /*
180 * WPA says the multicast cipher is the lowest unicast
181 * cipher supported. But we skip WEP which would
182 * otherwise be used based on this criteria.
183 */
184 rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
185 rsn->rsn_mcastkeylen = 128 / NBBY;
186
187 /*
188 * We support both WPA-PSK and 802.1x; the one used
189 * is determined by the authentication mode and the
190 * setting of the PSK state.
191 */
192 rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
193 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
194
195 ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
196 }
197
198 void
199 ieee80211_node_detach(struct ieee80211com *ic)
200 {
201
202 if (ic->ic_bss != NULL) {
203 ieee80211_free_node(ic->ic_bss);
204 ic->ic_bss = NULL;
205 }
206 ieee80211_node_table_cleanup(&ic->ic_scan);
207 ieee80211_node_table_cleanup(&ic->ic_sta);
208 if (ic->ic_aid_bitmap != NULL) {
209 FREE(ic->ic_aid_bitmap, M_DEVBUF);
210 ic->ic_aid_bitmap = NULL;
211 }
212 if (ic->ic_tim_bitmap != NULL) {
213 FREE(ic->ic_tim_bitmap, M_DEVBUF);
214 ic->ic_tim_bitmap = NULL;
215 }
216 }
217
218 /*
219 * Port authorize/unauthorize interfaces for use by an authenticator.
220 */
221
222 void
223 ieee80211_node_authorize(struct ieee80211_node *ni)
224 {
225 struct ieee80211com *ic = ni->ni_ic;
226
227 ni->ni_flags |= IEEE80211_NODE_AUTH;
228 ni->ni_inact_reload = ic->ic_inact_run;
229 }
230
231 void
232 ieee80211_node_unauthorize(struct ieee80211_node *ni)
233 {
234 ni->ni_flags &= ~IEEE80211_NODE_AUTH;
235 }
236
237 /*
238 * Set/change the channel. The rate set is also updated as
239 * to insure a consistent view by drivers.
240 */
241 static __inline void
242 ieee80211_set_chan(struct ieee80211com *ic,
243 struct ieee80211_node *ni, struct ieee80211_channel *chan)
244 {
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 5Ghz 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 ieee80211_free_node(obss);
816 /*
817 * Set the erp state (mostly the slot time) to deal with
818 * the auto-select case; this should be redundant if the
819 * mode is locked.
820 */
821 ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
822 ic->ic_curchan = selbs->ni_chan;
823 ieee80211_reset_erp(ic);
824 ieee80211_wme_initparams(ic);
825
826 if (ic->ic_opmode == IEEE80211_M_STA)
827 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
828 else
829 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
830 return 1;
831 }
832
833 /*
834 * Leave the specified IBSS/BSS network. The node is assumed to
835 * be passed in with a held reference.
836 */
837 void
838 ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
839 {
840 ic->ic_node_cleanup(ni);
841 ieee80211_notify_node_leave(ic, ni);
842 }
843
844 int
845 ieee80211_get_rate(struct ieee80211com *ic)
846 {
847 u_int8_t (*rates)[IEEE80211_RATE_MAXSIZE];
848 int rate;
849
850 rates = &ic->ic_bss->ni_rates.rs_rates;
851
852 if (ic->ic_fixed_rate != -1)
853 rate = (*rates)[ic->ic_fixed_rate];
854 else if (ic->ic_state == IEEE80211_S_RUN)
855 rate = (*rates)[ic->ic_bss->ni_txrate];
856 else
857 rate = 0;
858
859 return rate & IEEE80211_RATE_VAL;
860 }
861
862 static struct ieee80211_node *
863 node_alloc(struct ieee80211_node_table *nt)
864 {
865 struct ieee80211_node *ni;
866
867 MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
868 M_80211_NODE, M_NOWAIT | M_ZERO);
869 return ni;
870 }
871
872 /*
873 * Reclaim any resources in a node and reset any critical
874 * state. Typically nodes are free'd immediately after,
875 * but in some cases the storage may be reused so we need
876 * to insure consistent state (should probably fix that).
877 */
878 static void
879 node_cleanup(struct ieee80211_node *ni)
880 {
881 #define N(a) (sizeof(a)/sizeof(a[0]))
882 struct ieee80211com *ic = ni->ni_ic;
883 int i, qlen;
884
885 /* NB: preserve ni_table */
886 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
887 ic->ic_ps_sta--;
888 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
889 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
890 "[%s] power save mode off, %u sta's in ps mode\n",
891 ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
892 }
893 /*
894 * Clear AREF flag that marks the authorization refcnt bump
895 * has happened. This is probably not needed as the node
896 * should always be removed from the table so not found but
897 * do it just in case.
898 */
899 ni->ni_flags &= ~IEEE80211_NODE_AREF;
900
901 /*
902 * Drain power save queue and, if needed, clear TIM.
903 */
904 IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
905 if (qlen != 0 && ic->ic_set_tim != NULL)
906 ic->ic_set_tim(ni, 0);
907
908 ni->ni_associd = 0;
909 if (ni->ni_challenge != NULL) {
910 FREE(ni->ni_challenge, M_DEVBUF);
911 ni->ni_challenge = NULL;
912 }
913 /*
914 * Preserve SSID, WPA, and WME ie's so the bss node is
915 * reusable during a re-auth/re-assoc state transition.
916 * If we remove these data they will not be recreated
917 * because they come from a probe-response or beacon frame
918 * which cannot be expected prior to the association-response.
919 * This should not be an issue when operating in other modes
920 * as stations leaving always go through a full state transition
921 * which will rebuild this state.
922 *
923 * XXX does this leave us open to inheriting old state?
924 */
925 for (i = 0; i < N(ni->ni_rxfrag); i++)
926 if (ni->ni_rxfrag[i] != NULL) {
927 m_freem(ni->ni_rxfrag[i]);
928 ni->ni_rxfrag[i] = NULL;
929 }
930 /*
931 * Must be careful here to remove any key map entry w/o a LOR.
932 */
933 ieee80211_node_delucastkey(ni);
934 #undef N
935 }
936
937 static void
938 node_free(struct ieee80211_node *ni)
939 {
940 struct ieee80211com *ic = ni->ni_ic;
941
942 ic->ic_node_cleanup(ni);
943 if (ni->ni_wpa_ie != NULL)
944 FREE(ni->ni_wpa_ie, M_DEVBUF);
945 if (ni->ni_wme_ie != NULL)
946 FREE(ni->ni_wme_ie, M_DEVBUF);
947 IEEE80211_NODE_SAVEQ_DESTROY(ni);
948 FREE(ni, M_80211_NODE);
949 }
950
951 static u_int8_t
952 node_getrssi(const struct ieee80211_node *ni)
953 {
954 return ni->ni_rssi;
955 }
956
957 static void
958 ieee80211_setup_node(struct ieee80211_node_table *nt,
959 struct ieee80211_node *ni, const u_int8_t *macaddr)
960 {
961 struct ieee80211com *ic = nt->nt_ic;
962 int hash;
963
964 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
965 "%s %p<%s> in %s table\n", __func__, ni,
966 ether_sprintf(macaddr), nt->nt_name);
967
968 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
969 hash = IEEE80211_NODE_HASH(macaddr);
970 ieee80211_node_initref(ni); /* mark referenced */
971 ni->ni_chan = IEEE80211_CHAN_ANYC;
972 ni->ni_authmode = IEEE80211_AUTH_OPEN;
973 ni->ni_txpower = ic->ic_txpowlimit; /* max power */
974 ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
975 ni->ni_inact_reload = nt->nt_inact_init;
976 ni->ni_inact = ni->ni_inact_reload;
977 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
978
979 IEEE80211_NODE_LOCK(nt);
980 TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
981 LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
982 ni->ni_table = nt;
983 ni->ni_ic = ic;
984 IEEE80211_NODE_UNLOCK(nt);
985 }
986
987 struct ieee80211_node *
988 ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
989 {
990 struct ieee80211com *ic = nt->nt_ic;
991 struct ieee80211_node *ni;
992
993 ni = ic->ic_node_alloc(nt);
994 if (ni != NULL)
995 ieee80211_setup_node(nt, ni, macaddr);
996 else
997 ic->ic_stats.is_rx_nodealloc++;
998 return ni;
999 }
1000
1001 /*
1002 * Craft a temporary node suitable for sending a management frame
1003 * to the specified station. We craft only as much state as we
1004 * need to do the work since the node will be immediately reclaimed
1005 * once the send completes.
1006 */
1007 struct ieee80211_node *
1008 ieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr)
1009 {
1010 struct ieee80211_node *ni;
1011
1012 ni = ic->ic_node_alloc(&ic->ic_sta);
1013 if (ni != NULL) {
1014 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1015 "%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1016
1017 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1018 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
1019 ieee80211_node_initref(ni); /* mark referenced */
1020 ni->ni_txpower = ic->ic_bss->ni_txpower;
1021 /* NB: required by ieee80211_fix_rate */
1022 ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
1023 ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
1024 IEEE80211_KEYIX_NONE);
1025 /* XXX optimize away */
1026 IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1027
1028 ni->ni_table = NULL; /* NB: pedantic */
1029 ni->ni_ic = ic;
1030 } else {
1031 /* XXX msg */
1032 ic->ic_stats.is_rx_nodealloc++;
1033 }
1034 return ni;
1035 }
1036
1037 struct ieee80211_node *
1038 ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1039 {
1040 struct ieee80211com *ic = nt->nt_ic;
1041 struct ieee80211_node *ni;
1042
1043 ni = ic->ic_node_alloc(nt);
1044 if (ni != NULL) {
1045 ieee80211_setup_node(nt, ni, macaddr);
1046 /*
1047 * Inherit from ic_bss.
1048 */
1049 ni->ni_authmode = ic->ic_bss->ni_authmode;
1050 ni->ni_txpower = ic->ic_bss->ni_txpower;
1051 ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */
1052 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
1053 ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
1054 ni->ni_rsn = ic->ic_bss->ni_rsn;
1055 } else
1056 ic->ic_stats.is_rx_nodealloc++;
1057 return ni;
1058 }
1059
1060 static struct ieee80211_node *
1061 #ifdef IEEE80211_DEBUG_REFCNT
1062 _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1063 const u_int8_t *macaddr, const char *func, int line)
1064 #else
1065 _ieee80211_find_node(struct ieee80211_node_table *nt,
1066 const u_int8_t *macaddr)
1067 #endif
1068 {
1069 struct ieee80211_node *ni;
1070 int hash;
1071
1072 IEEE80211_NODE_LOCK_ASSERT(nt);
1073
1074 hash = IEEE80211_NODE_HASH(macaddr);
1075 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1076 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1077 ieee80211_ref_node(ni); /* mark referenced */
1078 #ifdef IEEE80211_DEBUG_REFCNT
1079 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1080 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1081 func, line,
1082 ni, ether_sprintf(ni->ni_macaddr),
1083 ieee80211_node_refcnt(ni));
1084 #endif
1085 return ni;
1086 }
1087 }
1088 return NULL;
1089 }
1090 #ifdef IEEE80211_DEBUG_REFCNT
1091 #define _ieee80211_find_node(nt, mac) \
1092 _ieee80211_find_node_debug(nt, mac, func, line)
1093 #endif
1094
1095 struct ieee80211_node *
1096 #ifdef IEEE80211_DEBUG_REFCNT
1097 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1098 const u_int8_t *macaddr, const char *func, int line)
1099 #else
1100 ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1101 #endif
1102 {
1103 struct ieee80211_node *ni;
1104
1105 IEEE80211_NODE_LOCK(nt);
1106 ni = _ieee80211_find_node(nt, macaddr);
1107 IEEE80211_NODE_UNLOCK(nt);
1108 return ni;
1109 }
1110
1111 /*
1112 * Fake up a node; this handles node discovery in adhoc mode.
1113 * Note that for the driver's benefit we we treat this like
1114 * an association so the driver has an opportunity to setup
1115 * it's private state.
1116 */
1117 struct ieee80211_node *
1118 ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
1119 const u_int8_t macaddr[IEEE80211_ADDR_LEN])
1120 {
1121 struct ieee80211com *ic = nt->nt_ic;
1122 struct ieee80211_node *ni;
1123
1124 ni = ieee80211_dup_bss(nt, macaddr);
1125 if (ni != NULL) {
1126 /* XXX no rate negotiation; just dup */
1127 ni->ni_rates = ic->ic_bss->ni_rates;
1128 if (ic->ic_newassoc != NULL)
1129 ic->ic_newassoc(ni, 1);
1130 /* XXX not right for 802.1x/WPA */
1131 ieee80211_node_authorize(ni);
1132 }
1133 return ni;
1134 }
1135
1136 #ifdef IEEE80211_DEBUG
1137 static void
1138 dump_probe_beacon(u_int8_t subtype, int isnew,
1139 const u_int8_t mac[IEEE80211_ADDR_LEN],
1140 const struct ieee80211_scanparams *sp)
1141 {
1142
1143 printf("[%s] %s%s on chan %u (bss chan %u) ",
1144 ether_sprintf(mac), isnew ? "new " : "",
1145 ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1146 sp->chan, sp->bchan);
1147 ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1148 printf("\n");
1149
1150 if (isnew) {
1151 printf("[%s] caps 0x%x bintval %u erp 0x%x",
1152 ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
1153 if (sp->country != NULL) {
1154 #ifdef __FreeBSD__
1155 printf(" country info %*D",
1156 sp->country[1], sp->country+2, " ");
1157 #else
1158 int i;
1159 printf(" country info");
1160 for (i = 0; i < sp->country[1]; i++)
1161 printf(" %02x", sp->country[i+2]);
1162 #endif
1163 }
1164 printf("\n");
1165 }
1166 }
1167 #endif /* IEEE80211_DEBUG */
1168
1169 static void
1170 saveie(u_int8_t **iep, const u_int8_t *ie)
1171 {
1172
1173 if (ie == NULL)
1174 *iep = NULL;
1175 else
1176 ieee80211_saveie(iep, ie);
1177 }
1178
1179 /*
1180 * Process a beacon or probe response frame.
1181 */
1182 void
1183 ieee80211_add_scan(struct ieee80211com *ic,
1184 const struct ieee80211_scanparams *sp,
1185 const struct ieee80211_frame *wh,
1186 int subtype, int rssi, int rstamp)
1187 {
1188 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1189 struct ieee80211_node_table *nt = &ic->ic_scan;
1190 struct ieee80211_node *ni;
1191 int newnode = 0;
1192
1193 ni = ieee80211_find_node(nt, wh->i_addr2);
1194 if (ni == NULL) {
1195 /*
1196 * Create a new entry.
1197 */
1198 ni = ic->ic_node_alloc(nt);
1199 if (ni == NULL) {
1200 ic->ic_stats.is_rx_nodealloc++;
1201 return;
1202 }
1203 ieee80211_setup_node(nt, ni, wh->i_addr2);
1204 /*
1205 * XXX inherit from ic_bss.
1206 */
1207 ni->ni_authmode = ic->ic_bss->ni_authmode;
1208 ni->ni_txpower = ic->ic_bss->ni_txpower;
1209 ni->ni_vlan = ic->ic_bss->ni_vlan; /* XXX?? */
1210 ieee80211_set_chan(ic, ni, ic->ic_curchan);
1211 ni->ni_rsn = ic->ic_bss->ni_rsn;
1212 newnode = 1;
1213 }
1214 #ifdef IEEE80211_DEBUG
1215 if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN))
1216 dump_probe_beacon(subtype, newnode, wh->i_addr2, sp);
1217 #endif
1218 /* XXX ap beaconing multiple ssid w/ same bssid */
1219 if (sp->ssid[1] != 0 &&
1220 (ISPROBE(subtype) || ni->ni_esslen == 0)) {
1221 ni->ni_esslen = sp->ssid[1];
1222 memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
1223 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1224 }
1225 ni->ni_scangen = ic->ic_scan.nt_scangen;
1226 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1227 ni->ni_rssi = rssi;
1228 ni->ni_rstamp = rstamp;
1229 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1230 ni->ni_intval = sp->bintval;
1231 ni->ni_capinfo = sp->capinfo;
1232 ni->ni_chan = &ic->ic_channels[sp->chan];
1233 ni->ni_fhdwell = sp->fhdwell;
1234 ni->ni_fhindex = sp->fhindex;
1235 ni->ni_erp = sp->erp;
1236 if (sp->tim != NULL) {
1237 struct ieee80211_tim_ie *ie =
1238 (struct ieee80211_tim_ie *) sp->tim;
1239
1240 ni->ni_dtim_count = ie->tim_count;
1241 ni->ni_dtim_period = ie->tim_period;
1242 }
1243 /*
1244 * Record the byte offset from the mac header to
1245 * the start of the TIM information element for
1246 * use by hardware and/or to speedup software
1247 * processing of beacon frames.
1248 */
1249 ni->ni_timoff = sp->timoff;
1250 /*
1251 * Record optional information elements that might be
1252 * used by applications or drivers.
1253 */
1254 saveie(&ni->ni_wme_ie, sp->wme);
1255 saveie(&ni->ni_wpa_ie, sp->wpa);
1256
1257 /* NB: must be after ni_chan is setup */
1258 ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT);
1259
1260 if (!newnode)
1261 ieee80211_free_node(ni);
1262 #undef ISPROBE
1263 }
1264
1265 void
1266 ieee80211_init_neighbor(struct ieee80211com *ic, struct ieee80211_node *ni,
1267 const struct ieee80211_frame *wh, const struct ieee80211_scanparams *sp,
1268 int isnew)
1269 {
1270 ni->ni_esslen = sp->ssid[1];
1271 memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1272 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1273 memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1274 ni->ni_intval = sp->bintval;
1275 ni->ni_capinfo = sp->capinfo;
1276 ni->ni_chan = ic->ic_bss->ni_chan;
1277 ni->ni_fhdwell = sp->fhdwell;
1278 ni->ni_fhindex = sp->fhindex;
1279 ni->ni_erp = sp->erp;
1280 ni->ni_timoff = sp->timoff;
1281 if (sp->wme != NULL)
1282 ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1283 if (sp->wpa != NULL)
1284 ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
1285
1286 /* NB: must be after ni_chan is setup */
1287 ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1288 IEEE80211_F_DODEL | IEEE80211_F_DONEGO | IEEE80211_F_DOSORT);
1289
1290 if (ic->ic_newassoc != NULL)
1291 ic->ic_newassoc(ni, isnew);
1292 }
1293
1294 /*
1295 * Do node discovery in adhoc mode on receipt of a beacon
1296 * or probe response frame. Note that for the driver's
1297 * benefit we we treat this like an association so the
1298 * driver has an opportunity to setup it's private state.
1299 */
1300 struct ieee80211_node *
1301 ieee80211_add_neighbor(struct ieee80211com *ic,
1302 const struct ieee80211_frame *wh,
1303 const struct ieee80211_scanparams *sp)
1304 {
1305 struct ieee80211_node *ni;
1306
1307 ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1308 if (ni != NULL) {
1309 ieee80211_init_neighbor(ic, ni, wh, sp, 1);
1310 /* XXX not right for 802.1x/WPA */
1311 ieee80211_node_authorize(ni);
1312 }
1313 return ni;
1314 }
1315
1316 #define IS_CTL(wh) \
1317 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1318 #define IS_PSPOLL(wh) \
1319 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1320 /*
1321 * Locate the node for sender, track state, and then pass the
1322 * (referenced) node up to the 802.11 layer for its use. We
1323 * are required to pass some node so we fall back to ic_bss
1324 * when this frame is from an unknown sender. The 802.11 layer
1325 * knows this means the sender wasn't in the node table and
1326 * acts accordingly.
1327 */
1328 struct ieee80211_node *
1329 #ifdef IEEE80211_DEBUG_REFCNT
1330 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1331 const struct ieee80211_frame_min *wh, const char *func, int line)
1332 #else
1333 ieee80211_find_rxnode(struct ieee80211com *ic,
1334 const struct ieee80211_frame_min *wh)
1335 #endif
1336 {
1337 struct ieee80211_node_table *nt;
1338 struct ieee80211_node *ni;
1339
1340 /* XXX may want scanned nodes in the neighbor table for adhoc */
1341 if (ic->ic_opmode == IEEE80211_M_STA ||
1342 ic->ic_opmode == IEEE80211_M_MONITOR ||
1343 (ic->ic_flags & IEEE80211_F_SCAN))
1344 nt = &ic->ic_scan;
1345 else
1346 nt = &ic->ic_sta;
1347 /* XXX check ic_bss first in station mode */
1348 /* XXX 4-address frames? */
1349 IEEE80211_NODE_LOCK(nt);
1350 if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1351 ni = _ieee80211_find_node(nt, wh->i_addr1);
1352 else
1353 ni = _ieee80211_find_node(nt, wh->i_addr2);
1354 if (ni == NULL)
1355 ni = ieee80211_ref_node(ic->ic_bss);
1356 IEEE80211_NODE_UNLOCK(nt);
1357
1358 return ni;
1359 }
1360
1361 /*
1362 * Like ieee80211_find_rxnode but use the supplied h/w
1363 * key index as a hint to locate the node in the key
1364 * mapping table. If an entry is present at the key
1365 * index we return it; otherwise do a normal lookup and
1366 * update the mapping table if the station has a unicast
1367 * key assigned to it.
1368 */
1369 struct ieee80211_node *
1370 #ifdef IEEE80211_DEBUG_REFCNT
1371 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1372 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1373 const char *func, int line)
1374 #else
1375 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1376 const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1377 #endif
1378 {
1379 struct ieee80211_node_table *nt;
1380 struct ieee80211_node *ni;
1381
1382 if (ic->ic_opmode == IEEE80211_M_STA ||
1383 ic->ic_opmode == IEEE80211_M_MONITOR ||
1384 (ic->ic_flags & IEEE80211_F_SCAN))
1385 nt = &ic->ic_scan;
1386 else
1387 nt = &ic->ic_sta;
1388 IEEE80211_NODE_LOCK(nt);
1389 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1390 ni = nt->nt_keyixmap[keyix];
1391 else
1392 ni = NULL;
1393 if (ni == NULL) {
1394 if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1395 ni = _ieee80211_find_node(nt, wh->i_addr1);
1396 else
1397 ni = _ieee80211_find_node(nt, wh->i_addr2);
1398 if (ni == NULL)
1399 ni = ieee80211_ref_node(ic->ic_bss);
1400 if (nt->nt_keyixmap != NULL) {
1401 /*
1402 * If the station has a unicast key cache slot
1403 * assigned update the key->node mapping table.
1404 */
1405 keyix = ni->ni_ucastkey.wk_rxkeyix;
1406 /* XXX can keyixmap[keyix] != NULL? */
1407 if (keyix < nt->nt_keyixmax &&
1408 nt->nt_keyixmap[keyix] == NULL) {
1409 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1410 "%s: add key map entry %p<%s> refcnt %d\n",
1411 __func__, ni, ether_sprintf(ni->ni_macaddr),
1412 ieee80211_node_refcnt(ni)+1);
1413 nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1414 }
1415 }
1416 } else {
1417 ieee80211_ref_node(ni);
1418 }
1419 IEEE80211_NODE_UNLOCK(nt);
1420
1421 return ni;
1422 }
1423 #undef IS_PSPOLL
1424 #undef IS_CTL
1425
1426 /*
1427 * Return a reference to the appropriate node for sending
1428 * a data frame. This handles node discovery in adhoc networks.
1429 */
1430 struct ieee80211_node *
1431 #ifdef IEEE80211_DEBUG_REFCNT
1432 ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
1433 const char *func, int line)
1434 #else
1435 ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
1436 #endif
1437 {
1438 struct ieee80211_node_table *nt = &ic->ic_sta;
1439 struct ieee80211_node *ni;
1440
1441 /*
1442 * The destination address should be in the node table
1443 * unless this is a multicast/broadcast frame. We can
1444 * also optimize station mode operation, all frames go
1445 * to the bss node.
1446 */
1447 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1448 IEEE80211_NODE_LOCK(nt);
1449 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1450 ni = ieee80211_ref_node(ic->ic_bss);
1451 else
1452 ni = _ieee80211_find_node(nt, macaddr);
1453 IEEE80211_NODE_UNLOCK(nt);
1454
1455 if (ni == NULL) {
1456 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1457 ic->ic_opmode == IEEE80211_M_AHDEMO) {
1458 /*
1459 * In adhoc mode cons up a node for the destination.
1460 * Note that we need an additional reference for the
1461 * caller to be consistent with _ieee80211_find_node.
1462 */
1463 ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1464 if (ni != NULL)
1465 (void) ieee80211_ref_node(ni);
1466 } else {
1467 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1468 "[%s] no node, discard frame (%s)\n",
1469 ether_sprintf(macaddr), __func__);
1470 ic->ic_stats.is_tx_nonode++;
1471 }
1472 }
1473 return ni;
1474 }
1475
1476 /*
1477 * Like find but search based on the channel too.
1478 */
1479 struct ieee80211_node *
1480 #ifdef IEEE80211_DEBUG_REFCNT
1481 ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
1482 const u_int8_t *macaddr, struct ieee80211_channel *chan,
1483 const char *func, int line)
1484 #else
1485 ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
1486 const u_int8_t *macaddr, struct ieee80211_channel *chan)
1487 #endif
1488 {
1489 struct ieee80211_node *ni;
1490 int hash;
1491
1492 hash = IEEE80211_NODE_HASH(macaddr);
1493 IEEE80211_NODE_LOCK(nt);
1494 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1495 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1496 ni->ni_chan == chan) {
1497 ieee80211_ref_node(ni); /* mark referenced */
1498 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1499 #ifdef IEEE80211_DEBUG_REFCNT
1500 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1501 func, line,
1502 #else
1503 "%s %p<%s> refcnt %d\n", __func__,
1504 #endif
1505 ni, ether_sprintf(ni->ni_macaddr),
1506 ieee80211_node_refcnt(ni));
1507 break;
1508 }
1509 }
1510 IEEE80211_NODE_UNLOCK(nt);
1511 return ni;
1512 }
1513
1514 struct ieee80211_node *
1515 ieee80211_refine_node_for_beacon(struct ieee80211com *ic,
1516 struct ieee80211_node *ni0, struct ieee80211_channel *chan,
1517 const u_int8_t *ssid)
1518 {
1519 struct ieee80211_node_table *nt = ni0->ni_table;
1520 struct ieee80211_node *best, *ni;
1521 int best_score = 0, score;
1522
1523 if (nt == NULL)
1524 return ni0;
1525
1526 best = ni0;
1527 if (ssid[1] == 0 || best->ni_esslen == 0)
1528 best_score = 1;
1529 else if (ssid[1] == best->ni_esslen &&
1530 memcmp(ssid + 2, best->ni_essid, ssid[1]) == 0)
1531 best_score = 2;
1532 else
1533 best_score = 0;
1534
1535 IEEE80211_NODE_LOCK(nt);
1536 for (ni = LIST_NEXT(ni0, ni_hash); ni != NULL;
1537 ni = LIST_NEXT(ni, ni_hash)) {
1538 if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, best->ni_macaddr) ||
1539 ni->ni_ic != best->ni_ic || ni->ni_chan != chan)
1540 continue;
1541
1542 if (ssid[1] == 0 || ni->ni_esslen == 0)
1543 score = 1;
1544 else if (ssid[1] == ni->ni_esslen &&
1545 memcmp(ssid + 2, ni->ni_essid, ssid[1]) == 0)
1546 score = 2;
1547 else
1548 continue;
1549
1550 if (score > best_score) {
1551 best = ni;
1552 best_score = score;
1553 }
1554 }
1555 IEEE80211_NODE_UNLOCK(nt);
1556 return best;
1557 }
1558
1559 /*
1560 * Like find but search based on the ssid too.
1561 */
1562 struct ieee80211_node *
1563 #ifdef IEEE80211_DEBUG_REFCNT
1564 ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1565 const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
1566 const char *func, int line)
1567 #else
1568 ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1569 const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
1570 #endif
1571 {
1572 #define MATCH_SSID(ni, ssid, ssidlen) \
1573 (ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1574 static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
1575 struct ieee80211com *ic = nt->nt_ic;
1576 struct ieee80211_node *ni;
1577 int hash;
1578
1579 IEEE80211_NODE_LOCK(nt);
1580 /*
1581 * A mac address that is all zero means match only the ssid;
1582 * otherwise we must match both.
1583 */
1584 if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1585 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1586 if (MATCH_SSID(ni, ssid, ssidlen))
1587 break;
1588 }
1589 } else {
1590 hash = IEEE80211_NODE_HASH(macaddr);
1591 LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1592 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1593 MATCH_SSID(ni, ssid, ssidlen))
1594 break;
1595 }
1596 }
1597 if (ni != NULL) {
1598 ieee80211_ref_node(ni); /* mark referenced */
1599 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1600 #ifdef IEEE80211_DEBUG_REFCNT
1601 "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1602 func, line,
1603 #else
1604 "%s %p<%s> refcnt %d\n", __func__,
1605 #endif
1606 ni, ether_sprintf(ni->ni_macaddr),
1607 ieee80211_node_refcnt(ni));
1608 }
1609 IEEE80211_NODE_UNLOCK(nt);
1610 return ni;
1611 #undef MATCH_SSID
1612 }
1613
1614 static void
1615 _ieee80211_free_node(struct ieee80211_node *ni)
1616 {
1617 struct ieee80211com *ic = ni->ni_ic;
1618 struct ieee80211_node_table *nt = ni->ni_table;
1619
1620 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1621 "%s %p<%s> in %s table\n", __func__, ni,
1622 ether_sprintf(ni->ni_macaddr),
1623 nt != NULL ? nt->nt_name : "<gone>");
1624
1625 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1626 if (nt != NULL) {
1627 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1628 LIST_REMOVE(ni, ni_hash);
1629 }
1630 ic->ic_node_free(ni);
1631 }
1632
1633 void
1634 #ifdef IEEE80211_DEBUG_REFCNT
1635 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1636 #else
1637 ieee80211_free_node(struct ieee80211_node *ni)
1638 #endif
1639 {
1640 struct ieee80211_node_table *nt = ni->ni_table;
1641
1642 #ifdef IEEE80211_DEBUG_REFCNT
1643 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1644 "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1645 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1646 #endif
1647 if (nt != NULL) {
1648 IEEE80211_NODE_LOCK(nt);
1649 if (ieee80211_node_dectestref(ni)) {
1650 /*
1651 * Last reference, reclaim state.
1652 */
1653 _ieee80211_free_node(ni);
1654 } else if (ieee80211_node_refcnt(ni) == 1 &&
1655 nt->nt_keyixmap != NULL) {
1656 ieee80211_keyix keyix;
1657 /*
1658 * Check for a last reference in the key mapping table.
1659 */
1660 keyix = ni->ni_ucastkey.wk_rxkeyix;
1661 if (keyix < nt->nt_keyixmax &&
1662 nt->nt_keyixmap[keyix] == ni) {
1663 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1664 "%s: %p<%s> clear key map entry", __func__,
1665 ni, ether_sprintf(ni->ni_macaddr));
1666 nt->nt_keyixmap[keyix] = NULL;
1667 ieee80211_node_decref(ni); /* XXX needed? */
1668 _ieee80211_free_node(ni);
1669 }
1670 }
1671 IEEE80211_NODE_UNLOCK(nt);
1672 } else {
1673 if (ieee80211_node_dectestref(ni))
1674 _ieee80211_free_node(ni);
1675 }
1676 }
1677
1678 /*
1679 * Reclaim a unicast key and clear any key cache state.
1680 */
1681 int
1682 ieee80211_node_delucastkey(struct ieee80211_node *ni)
1683 {
1684 struct ieee80211com *ic = ni->ni_ic;
1685 struct ieee80211_node_table *nt = &ic->ic_sta;
1686 struct ieee80211_node *nikey;
1687 ieee80211_keyix keyix;
1688 int isowned, status;
1689
1690 /*
1691 * NB: We must beware of LOR here; deleting the key
1692 * can cause the crypto layer to block traffic updates
1693 * which can generate a LOR against the node table lock;
1694 * grab it here and stash the key index for our use below.
1695 *
1696 * Must also beware of recursion on the node table lock.
1697 * When called from node_cleanup we may already have
1698 * the node table lock held. Unfortunately there's no
1699 * way to separate out this path so we must do this
1700 * conditionally.
1701 */
1702 isowned = IEEE80211_NODE_IS_LOCKED(nt);
1703 if (!isowned)
1704 IEEE80211_NODE_LOCK(nt);
1705 keyix = ni->ni_ucastkey.wk_rxkeyix;
1706 status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1707 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1708 nikey = nt->nt_keyixmap[keyix];
1709 nt->nt_keyixmap[keyix] = NULL;;
1710 } else
1711 nikey = NULL;
1712 if (!isowned)
1713 IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1714
1715 if (nikey != NULL) {
1716 IASSERT(nikey == ni,
1717 ("key map out of sync, ni %p nikey %p", ni, nikey));
1718 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1719 "%s: delete key map entry %p<%s> refcnt %d\n",
1720 __func__, ni, ether_sprintf(ni->ni_macaddr),
1721 ieee80211_node_refcnt(ni)-1);
1722 ieee80211_free_node(ni);
1723 }
1724 return status;
1725 }
1726
1727 /*
1728 * Reclaim a node. If this is the last reference count then
1729 * do the normal free work. Otherwise remove it from the node
1730 * table and mark it gone by clearing the back-reference.
1731 */
1732 static void
1733 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1734 {
1735 ieee80211_keyix keyix;
1736
1737 IEEE80211_NODE_LOCK_ASSERT(nt);
1738
1739 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1740 "%s: remove %p<%s> from %s table, refcnt %d\n",
1741 __func__, ni, ether_sprintf(ni->ni_macaddr),
1742 nt->nt_name, ieee80211_node_refcnt(ni)-1);
1743 /*
1744 * Clear any entry in the unicast key mapping table.
1745 * We need to do it here so rx lookups don't find it
1746 * in the mapping table even if it's not in the hash
1747 * table. We cannot depend on the mapping table entry
1748 * being cleared because the node may not be free'd.
1749 */
1750 keyix = ni->ni_ucastkey.wk_rxkeyix;
1751 if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1752 nt->nt_keyixmap[keyix] == ni) {
1753 IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1754 "%s: %p<%s> clear key map entry\n",
1755 __func__, ni, ether_sprintf(ni->ni_macaddr));
1756 nt->nt_keyixmap[keyix] = NULL;
1757 ieee80211_node_decref(ni); /* NB: don't need free */
1758 }
1759 if (!ieee80211_node_dectestref(ni)) {
1760 /*
1761 * Other references are present, just remove the
1762 * node from the table so it cannot be found. When
1763 * the references are dropped storage will be
1764 * reclaimed.
1765 */
1766 TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1767 LIST_REMOVE(ni, ni_hash);
1768 ni->ni_table = NULL; /* clear reference */
1769 } else
1770 _ieee80211_free_node(ni);
1771 }
1772
1773 static void
1774 ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1775 {
1776 struct ieee80211com *ic = nt->nt_ic;
1777 struct ieee80211_node *ni;
1778
1779 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1780 "%s: free all nodes in %s table\n", __func__, nt->nt_name);
1781
1782 while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1783 if (ni->ni_associd != 0) {
1784 if (ic->ic_auth->ia_node_leave != NULL)
1785 ic->ic_auth->ia_node_leave(ic, ni);
1786 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1787 }
1788 node_reclaim(nt, ni);
1789 }
1790 ieee80211_reset_erp(ic);
1791 }
1792
1793 static void
1794 ieee80211_free_allnodes(struct ieee80211_node_table *nt)
1795 {
1796
1797 IEEE80211_NODE_LOCK(nt);
1798 ieee80211_free_allnodes_locked(nt);
1799 IEEE80211_NODE_UNLOCK(nt);
1800 }
1801
1802 /*
1803 * Timeout entries in the scan cache.
1804 */
1805 static void
1806 ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
1807 {
1808 struct ieee80211com *ic = nt->nt_ic;
1809 struct ieee80211_node *ni, *tni;
1810
1811 IEEE80211_NODE_LOCK(nt);
1812 ni = ic->ic_bss;
1813 /* XXX belongs elsewhere */
1814 if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
1815 m_freem(ni->ni_rxfrag[0]);
1816 ni->ni_rxfrag[0] = NULL;
1817 }
1818 TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
1819 if (ni->ni_inact && --ni->ni_inact == 0) {
1820 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1821 "[%s] scan candidate purged from cache "
1822 "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1823 ieee80211_node_refcnt(ni));
1824 node_reclaim(nt, ni);
1825 }
1826 }
1827 IEEE80211_NODE_UNLOCK(nt);
1828
1829 nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1830 }
1831
1832 /*
1833 * Timeout inactive stations and do related housekeeping.
1834 * Note that we cannot hold the node lock while sending a
1835 * frame as this would lead to a LOR. Instead we use a
1836 * generation number to mark nodes that we've scanned and
1837 * drop the lock and restart a scan if we have to time out
1838 * a node. Since we are single-threaded by virtue of
1839 * controlling the inactivity timer we can be sure this will
1840 * process each node only once.
1841 */
1842 static void
1843 ieee80211_timeout_stations(struct ieee80211_node_table *nt)
1844 {
1845 struct ieee80211com *ic = nt->nt_ic;
1846 struct ieee80211_node *ni;
1847 u_int gen;
1848 int isadhoc;
1849
1850 isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1851 ic->ic_opmode == IEEE80211_M_AHDEMO);
1852 IEEE80211_SCAN_LOCK(nt);
1853 gen = nt->nt_scangen++;
1854 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1855 "%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1856 restart:
1857 IEEE80211_NODE_LOCK(nt);
1858 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1859 if (ni->ni_scangen == gen) /* previously handled */
1860 continue;
1861 ni->ni_scangen = gen;
1862 /*
1863 * Ignore entries for which have yet to receive an
1864 * authentication frame. These are transient and
1865 * will be reclaimed when the last reference to them
1866 * goes away (when frame xmits complete).
1867 */
1868 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1869 (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1870 continue;
1871 /*
1872 * Free fragment if not needed anymore
1873 * (last fragment older than 1s).
1874 * XXX doesn't belong here
1875 */
1876 if (ni->ni_rxfrag[0] != NULL &&
1877 ticks > ni->ni_rxfragstamp + hz) {
1878 m_freem(ni->ni_rxfrag[0]);
1879 ni->ni_rxfrag[0] = NULL;
1880 }
1881 /*
1882 * Special case ourself; we may be idle for extended periods
1883 * of time and regardless reclaiming our state is wrong.
1884 */
1885 if (ni == ic->ic_bss)
1886 continue;
1887 ni->ni_inact--;
1888 if (ni->ni_associd != 0 || isadhoc) {
1889 /*
1890 * Age frames on the power save queue. The
1891 * aging interval is 4 times the listen
1892 * interval specified by the station. This
1893 * number is factored into the age calculations
1894 * when the frame is placed on the queue. We
1895 * store ages as time differences we can check
1896 * and/or adjust only the head of the list.
1897 */
1898 if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
1899 struct mbuf *m;
1900 int discard = 0;
1901
1902 IEEE80211_NODE_SAVEQ_LOCK(ni);
1903 while (IF_POLL(&ni->ni_savedq, m) != NULL &&
1904 M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
1905 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
1906 _IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
1907 m_freem(m);
1908 discard++;
1909 }
1910 if (m != NULL)
1911 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
1912 IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1913
1914 if (discard != 0) {
1915 IEEE80211_DPRINTF(ic,
1916 IEEE80211_MSG_POWER,
1917 "[%s] discard %u frames for age\n",
1918 ether_sprintf(ni->ni_macaddr),
1919 discard);
1920 IEEE80211_NODE_STAT_ADD(ni,
1921 ps_discard, discard);
1922 if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1923 ic->ic_set_tim(ni, 0);
1924 }
1925 }
1926 /*
1927 * Probe the station before time it out. We
1928 * send a null data frame which may not be
1929 * universally supported by drivers (need it
1930 * for ps-poll support so it should be...).
1931 */
1932 if (0 < ni->ni_inact &&
1933 ni->ni_inact <= ic->ic_inact_probe) {
1934 IEEE80211_NOTE(ic,
1935 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1936 ni, "%s",
1937 "probe station due to inactivity");
1938 /*
1939 * Grab a reference before unlocking the table
1940 * so the node cannot be reclaimed before we
1941 * send the frame. ieee80211_send_nulldata
1942 * understands we've done this and reclaims the
1943 * ref for us as needed.
1944 */
1945 ieee80211_ref_node(ni);
1946 IEEE80211_NODE_UNLOCK(nt);
1947 ieee80211_send_nulldata(ni);
1948 /* XXX stat? */
1949 goto restart;
1950 }
1951 }
1952 if (ni->ni_inact <= 0) {
1953 IEEE80211_NOTE(ic,
1954 IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1955 "station timed out due to inactivity "
1956 "(refcnt %u)", ieee80211_node_refcnt(ni));
1957 /*
1958 * Send a deauthenticate frame and drop the station.
1959 * This is somewhat complicated due to reference counts
1960 * and locking. At this point a station will typically
1961 * have a reference count of 1. ieee80211_node_leave
1962 * will do a "free" of the node which will drop the
1963 * reference count. But in the meantime a reference
1964 * wil be held by the deauth frame. The actual reclaim
1965 * of the node will happen either after the tx is
1966 * completed or by ieee80211_node_leave.
1967 *
1968 * Separately we must drop the node lock before sending
1969 * in case the driver takes a lock, as this will result
1970 * in LOR between the node lock and the driver lock.
1971 */
1972 IEEE80211_NODE_UNLOCK(nt);
1973 if (ni->ni_associd != 0) {
1974 IEEE80211_SEND_MGMT(ic, ni,
1975 IEEE80211_FC0_SUBTYPE_DEAUTH,
1976 IEEE80211_REASON_AUTH_EXPIRE);
1977 }
1978 ieee80211_node_leave(ic, ni);
1979 ic->ic_stats.is_node_timeout++;
1980 goto restart;
1981 }
1982 }
1983 IEEE80211_NODE_UNLOCK(nt);
1984
1985 IEEE80211_SCAN_UNLOCK(nt);
1986
1987 nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1988 }
1989
1990 void
1991 ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1992 {
1993 struct ieee80211_node *ni;
1994 u_int gen;
1995
1996 IEEE80211_SCAN_LOCK(nt);
1997 gen = nt->nt_scangen++;
1998 restart:
1999 IEEE80211_NODE_LOCK(nt);
2000 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2001 if (ni->ni_scangen != gen) {
2002 ni->ni_scangen = gen;
2003 (void) ieee80211_ref_node(ni);
2004 IEEE80211_NODE_UNLOCK(nt);
2005 (*f)(arg, ni);
2006 ieee80211_free_node(ni);
2007 goto restart;
2008 }
2009 }
2010 IEEE80211_NODE_UNLOCK(nt);
2011
2012 IEEE80211_SCAN_UNLOCK(nt);
2013 }
2014
2015 void
2016 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2017 {
2018 printf("0x%p: mac %s refcnt %d\n", ni,
2019 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2020 printf("\tscangen %u authmode %u flags 0x%x\n",
2021 ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2022 printf("\tassocid 0x%x txpower %u vlan %u\n",
2023 ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2024 printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2025 ni->ni_txseqs[0],
2026 ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
2027 ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
2028 ni->ni_rxfragstamp);
2029 printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
2030 ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
2031 printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2032 ether_sprintf(ni->ni_bssid),
2033 ni->ni_esslen, ni->ni_essid,
2034 ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2035 printf("\tfails %u inact %u txrate %u\n",
2036 ni->ni_fails, ni->ni_inact, ni->ni_txrate);
2037 }
2038
2039 void
2040 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2041 {
2042 ieee80211_iterate_nodes(nt,
2043 (ieee80211_iter_func *) ieee80211_dump_node, nt);
2044 }
2045
2046 /*
2047 * Handle a station joining an 11g network.
2048 */
2049 static void
2050 ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
2051 {
2052
2053 /*
2054 * Station isn't capable of short slot time. Bump
2055 * the count of long slot time stations and disable
2056 * use of short slot time. Note that the actual switch
2057 * over to long slot time use may not occur until the
2058 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2059 */
2060 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2061 ic->ic_longslotsta++;
2062 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2063 "[%s] station needs long slot time, count %d\n",
2064 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
2065 /* XXX vap's w/ conflicting needs won't work */
2066 ieee80211_set_shortslottime(ic, 0);
2067 }
2068 /*
2069 * If the new station is not an ERP station
2070 * then bump the counter and enable protection
2071 * if configured.
2072 */
2073 if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
2074 ic->ic_nonerpsta++;
2075 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2076 "[%s] station is !ERP, %d non-ERP stations associated\n",
2077 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
2078 /*
2079 * If protection is configured, enable it.
2080 */
2081 if (ic->ic_protmode != IEEE80211_PROT_NONE) {
2082 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2083 "%s: enable use of protection\n", __func__);
2084 ic->ic_flags |= IEEE80211_F_USEPROT;
2085 }
2086 /*
2087 * If station does not support short preamble
2088 * then we must enable use of Barker preamble.
2089 */
2090 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2091 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2092 "[%s] station needs long preamble\n",
2093 ether_sprintf(ni->ni_macaddr));
2094 ic->ic_flags |= IEEE80211_F_USEBARKER;
2095 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2096 }
2097 } else
2098 ni->ni_flags |= IEEE80211_NODE_ERP;
2099 }
2100
2101 void
2102 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
2103 {
2104 int newassoc;
2105
2106 if (ni->ni_associd == 0) {
2107 u_int16_t aid;
2108
2109 /*
2110 * It would be good to search the bitmap
2111 * more efficiently, but this will do for now.
2112 */
2113 for (aid = 1; aid < ic->ic_max_aid; aid++) {
2114 if (!IEEE80211_AID_ISSET(aid,
2115 ic->ic_aid_bitmap))
2116 break;
2117 }
2118 if (aid >= ic->ic_max_aid) {
2119 IEEE80211_SEND_MGMT(ic, ni, resp,
2120 IEEE80211_REASON_ASSOC_TOOMANY);
2121 ieee80211_node_leave(ic, ni);
2122 return;
2123 }
2124 ni->ni_associd = aid | 0xc000;
2125 IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
2126 ic->ic_sta_assoc++;
2127 newassoc = 1;
2128 if (ic->ic_curmode == IEEE80211_MODE_11G)
2129 ieee80211_node_join_11g(ic, ni);
2130 } else
2131 newassoc = 0;
2132
2133 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2134 "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
2135 ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
2136 IEEE80211_NODE_AID(ni),
2137 ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2138 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2139 ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2140 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
2141 );
2142
2143 /* give driver a chance to setup state like ni_txrate */
2144 if (ic->ic_newassoc != NULL)
2145 ic->ic_newassoc(ni, newassoc);
2146 ni->ni_inact_reload = ic->ic_inact_auth;
2147 ni->ni_inact = ni->ni_inact_reload;
2148 IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
2149 /* tell the authenticator about new station */
2150 if (ic->ic_auth->ia_node_join != NULL)
2151 ic->ic_auth->ia_node_join(ic, ni);
2152 ieee80211_notify_node_join(ic, ni, newassoc);
2153 }
2154
2155 /*
2156 * Handle a station leaving an 11g network.
2157 */
2158 static void
2159 ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
2160 {
2161
2162 IASSERT(ic->ic_curmode == IEEE80211_MODE_11G,
2163 ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq,
2164 ni->ni_chan->ic_flags, ic->ic_curmode));
2165
2166 /*
2167 * If a long slot station do the slot time bookkeeping.
2168 */
2169 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2170 IASSERT(ic->ic_longslotsta > 0,
2171 ("bogus long slot station count %d", ic->ic_longslotsta));
2172 ic->ic_longslotsta--;
2173 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2174 "[%s] long slot time station leaves, count now %d\n",
2175 ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
2176 if (ic->ic_longslotsta == 0) {
2177 /*
2178 * Re-enable use of short slot time if supported
2179 * and not operating in IBSS mode (per spec).
2180 */
2181 if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2182 ic->ic_opmode != IEEE80211_M_IBSS) {
2183 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2184 "%s: re-enable use of short slot time\n",
2185 __func__);
2186 ieee80211_set_shortslottime(ic, 1);
2187 }
2188 }
2189 }
2190 /*
2191 * If a non-ERP station do the protection-related bookkeeping.
2192 */
2193 if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2194 IASSERT(ic->ic_nonerpsta > 0,
2195 ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2196 ic->ic_nonerpsta--;
2197 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2198 "[%s] non-ERP station leaves, count now %d\n",
2199 ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
2200 if (ic->ic_nonerpsta == 0) {
2201 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2202 "%s: disable use of protection\n", __func__);
2203 ic->ic_flags &= ~IEEE80211_F_USEPROT;
2204 /* XXX verify mode? */
2205 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2206 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2207 "%s: re-enable use of short preamble\n",
2208 __func__);
2209 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2210 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2211 }
2212 }
2213 }
2214 }
2215
2216 /*
2217 * Handle bookkeeping for station deauthentication/disassociation
2218 * when operating as an ap.
2219 */
2220 void
2221 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
2222 {
2223 struct ieee80211_node_table *nt = ni->ni_table;
2224
2225 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2226 "[%s] station with aid %d leaves\n",
2227 ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
2228
2229 IASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
2230 ic->ic_opmode == IEEE80211_M_IBSS ||
2231 ic->ic_opmode == IEEE80211_M_AHDEMO,
2232 ("unexpected operating mode %u", ic->ic_opmode));
2233 /*
2234 * If node wasn't previously associated all
2235 * we need to do is reclaim the reference.
2236 */
2237 /* XXX ibss mode bypasses 11g and notification */
2238 if (ni->ni_associd == 0)
2239 goto done;
2240 /*
2241 * Tell the authenticator the station is leaving.
2242 * Note that we must do this before yanking the
2243 * association id as the authenticator uses the
2244 * associd to locate it's state block.
2245 */
2246 if (ic->ic_auth->ia_node_leave != NULL)
2247 ic->ic_auth->ia_node_leave(ic, ni);
2248 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
2249 ni->ni_associd = 0;
2250 ic->ic_sta_assoc--;
2251
2252 if (ic->ic_curmode == IEEE80211_MODE_11G)
2253 ieee80211_node_leave_11g(ic, ni);
2254 /*
2255 * Cleanup station state. In particular clear various
2256 * state that might otherwise be reused if the node
2257 * is reused before the reference count goes to zero
2258 * (and memory is reclaimed).
2259 */
2260 ieee80211_sta_leave(ic, ni);
2261 done:
2262 /*
2263 * Remove the node from any table it's recorded in and
2264 * drop the caller's reference. Removal from the table
2265 * is important to insure the node is not reprocessed
2266 * for inactivity.
2267 */
2268 if (nt != NULL) {
2269 IEEE80211_NODE_LOCK(nt);
2270 node_reclaim(nt, ni);
2271 IEEE80211_NODE_UNLOCK(nt);
2272 } else
2273 ieee80211_free_node(ni);
2274 }
2275
2276 u_int8_t
2277 ieee80211_getrssi(struct ieee80211com *ic)
2278 {
2279 #define NZ(x) ((x) == 0 ? 1 : (x))
2280 struct ieee80211_node_table *nt = &ic->ic_sta;
2281 u_int32_t rssi_samples, rssi_total;
2282 struct ieee80211_node *ni;
2283
2284 rssi_total = 0;
2285 rssi_samples = 0;
2286 switch (ic->ic_opmode) {
2287 case IEEE80211_M_IBSS: /* average of all ibss neighbors */
2288 /* XXX locking */
2289 TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
2290 if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
2291 rssi_samples++;
2292 rssi_total += ic->ic_node_getrssi(ni);
2293 }
2294 break;
2295 case IEEE80211_M_AHDEMO: /* average of all neighbors */
2296 /* XXX locking */
2297 TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2298 rssi_samples++;
2299 rssi_total += ic->ic_node_getrssi(ni);
2300 }
2301 break;
2302 case IEEE80211_M_HOSTAP: /* average of all associated stations */
2303 #ifndef IEEE80211_NO_HOSTAP
2304 /* XXX locking */
2305 TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
2306 if (IEEE80211_AID(ni->ni_associd) != 0) {
2307 rssi_samples++;
2308 rssi_total += ic->ic_node_getrssi(ni);
2309 }
2310 #endif /* !IEEE80211_NO_HOSTAP */
2311 break;
2312 case IEEE80211_M_MONITOR: /* XXX */
2313 case IEEE80211_M_STA: /* use stats from associated ap */
2314 default:
2315 if (ic->ic_bss != NULL)
2316 rssi_total = ic->ic_node_getrssi(ic->ic_bss);
2317 rssi_samples = 1;
2318 break;
2319 }
2320 return rssi_total / NZ(rssi_samples);
2321 #undef NZ
2322 }
2323
2324 /*
2325 * Indicate whether there are frames queued for a station in power-save mode.
2326 */
2327 static void
2328 ieee80211_set_tim(struct ieee80211_node *ni, int set)
2329 {
2330 struct ieee80211com *ic = ni->ni_ic;
2331 u_int16_t aid;
2332
2333 IASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
2334 ic->ic_opmode == IEEE80211_M_IBSS,
2335 ("operating mode %u", ic->ic_opmode));
2336
2337 aid = IEEE80211_AID(ni->ni_associd);
2338 IASSERT(aid < ic->ic_max_aid,
2339 ("bogus aid %u, max %u", aid, ic->ic_max_aid));
2340
2341 IEEE80211_BEACON_LOCK(ic);
2342 if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
2343 if (set) {
2344 setbit(ic->ic_tim_bitmap, aid);
2345 ic->ic_ps_pending++;
2346 } else {
2347 clrbit(ic->ic_tim_bitmap, aid);
2348 ic->ic_ps_pending--;
2349 }
2350 ic->ic_flags |= IEEE80211_F_TIMUPDATE;
2351 }
2352 IEEE80211_BEACON_UNLOCK(ic);
2353 }
2354
2355 /*
2356 * Node table support.
2357 */
2358
2359 static void
2360 ieee80211_node_table_init(struct ieee80211com *ic,
2361 struct ieee80211_node_table *nt,
2362 const char *name, int inact, int keyixmax,
2363 void (*timeout)(struct ieee80211_node_table *))
2364 {
2365
2366 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
2367 "%s %s table, inact %u\n", __func__, name, inact);
2368
2369 nt->nt_ic = ic;
2370 /* XXX need unit */
2371 IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2372 IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2373 TAILQ_INIT(&nt->nt_node);
2374 nt->nt_name = name;
2375 nt->nt_scangen = 1;
2376 nt->nt_inact_init = inact;
2377 nt->nt_timeout = timeout;
2378 nt->nt_keyixmax = keyixmax;
2379 if (nt->nt_keyixmax > 0) {
2380 MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2381 keyixmax * sizeof(struct ieee80211_node *),
2382 M_80211_NODE, M_NOWAIT | M_ZERO);
2383 if (nt->nt_keyixmap == NULL)
2384 if_printf(ic->ic_ifp,
2385 "Cannot allocate key index map with %u entries\n",
2386 keyixmax);
2387 } else
2388 nt->nt_keyixmap = NULL;
2389 }
2390
2391 void
2392 ieee80211_node_table_reset(struct ieee80211_node_table *nt)
2393 {
2394
2395 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2396 "%s %s table\n", __func__, nt->nt_name);
2397
2398 IEEE80211_NODE_LOCK(nt);
2399 nt->nt_inact_timer = 0;
2400 ieee80211_free_allnodes_locked(nt);
2401 IEEE80211_NODE_UNLOCK(nt);
2402 }
2403
2404 static void
2405 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2406 {
2407
2408 IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2409 "%s %s table\n", __func__, nt->nt_name);
2410
2411 IEEE80211_NODE_LOCK(nt);
2412 ieee80211_free_allnodes_locked(nt);
2413 if (nt->nt_keyixmap != NULL) {
2414 /* XXX verify all entries are NULL */
2415 int i;
2416 for (i = 0; i < nt->nt_keyixmax; i++)
2417 if (nt->nt_keyixmap[i] != NULL)
2418 printf("%s: %s[%u] still active\n", __func__,
2419 nt->nt_name, i);
2420 FREE(nt->nt_keyixmap, M_80211_NODE);
2421 nt->nt_keyixmap = NULL;
2422 }
2423 IEEE80211_SCAN_LOCK_DESTROY(nt);
2424 IEEE80211_NODE_LOCK_DESTROY(nt);
2425 }
2426