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