1 //------------------------------------------------------------------------------
2 // <copyright file="wlan_node.c" company="Atheros">
3 // Copyright (c) 2004-2010 Atheros Corporation. All rights reserved.
6 // Permission to use, copy, modify, and/or distribute this software for any
7 // purpose with or without fee is hereby granted, provided that the above
8 // copyright notice and this permission notice appear in all copies.
10 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 //------------------------------------------------------------------------------
20 //==============================================================================
21 // IEEE 802.11 node handling support.
23 // Author(s): ="Atheros"
24 //==============================================================================
28 #define ATH_MODULE_NAME wlan
33 #include <ieee80211.h>
36 #include <ieee80211_node.h>
38 #define ATH_DEBUG_WLAN ATH_DEBUG_MAKE_MODULE_MASK(0)
40 #ifdef ATH_DEBUG_MODULE
42 static struct ath_debug_mask_description wlan_debug_desc[] = {
43 { ATH_DEBUG_WLAN , "General WLAN Node Tracing"},
46 ATH_DEBUG_INSTANTIATE_MODULE_VAR(wlan,
48 "WLAN Node Management",
49 ATH_DEBUG_MASK_DEFAULTS,
50 ATH_DEBUG_DESCRIPTION_COUNT(wlan_debug_desc),
56 static void wlan_node_timeout(unsigned long arg);
59 static bss_t * _ieee80211_find_node (struct ieee80211_node_table *nt,
63 wlan_node_alloc(struct ieee80211_node_table *nt, int wh_size)
67 ni = A_MALLOC_NOWAIT(sizeof(bss_t));
72 ni->ni_buf = A_MALLOC_NOWAIT(wh_size);
73 if (ni->ni_buf == NULL) {
83 /* Make sure our lists are clean */
84 ni->ni_list_next = NULL;
85 ni->ni_list_prev = NULL;
86 ni->ni_hash_next = NULL;
87 ni->ni_hash_prev = NULL;
90 // ni_scangen never initialized before and during suspend/resume of winmobile,
91 // that some junk has been stored in this, due to this scan list didn't properly updated
95 #ifdef OS_ROAM_MANAGEMENT
103 wlan_node_free(bss_t *ni)
105 if (ni->ni_buf != NULL) {
112 wlan_setup_node(struct ieee80211_node_table *nt, bss_t *ni,
116 u32 timeoutValue = 0;
118 memcpy(ni->ni_macaddr, macaddr, IEEE80211_ADDR_LEN);
119 hash = IEEE80211_NODE_HASH (macaddr);
120 ieee80211_node_initref (ni); /* mark referenced */
122 timeoutValue = nt->nt_nodeAge;
124 ni->ni_tstamp = A_GET_MS (0);
125 ni->ni_actcnt = WLAN_NODE_INACT_CNT;
127 IEEE80211_NODE_LOCK_BH(nt);
129 /* Insert at the end of the node list */
130 ni->ni_list_next = NULL;
131 ni->ni_list_prev = nt->nt_node_last;
132 if(nt->nt_node_last != NULL)
134 nt->nt_node_last->ni_list_next = ni;
136 nt->nt_node_last = ni;
137 if(nt->nt_node_first == NULL)
139 nt->nt_node_first = ni;
142 /* Insert into the hash list i.e. the bucket */
143 if((ni->ni_hash_next = nt->nt_hash[hash]) != NULL)
145 nt->nt_hash[hash]->ni_hash_prev = ni;
147 ni->ni_hash_prev = NULL;
148 nt->nt_hash[hash] = ni;
151 if (!nt->isTimerArmed) {
152 A_TIMEOUT_MS(&nt->nt_inact_timer, timeoutValue, 0);
153 nt->isTimerArmed = true;
157 IEEE80211_NODE_UNLOCK_BH(nt);
161 _ieee80211_find_node(struct ieee80211_node_table *nt,
167 IEEE80211_NODE_LOCK_ASSERT(nt);
169 hash = IEEE80211_NODE_HASH(macaddr);
170 for(ni = nt->nt_hash[hash]; ni; ni = ni->ni_hash_next) {
171 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
172 ieee80211_node_incref(ni); /* mark referenced */
180 wlan_find_node(struct ieee80211_node_table *nt, const u8 *macaddr)
184 IEEE80211_NODE_LOCK(nt);
185 ni = _ieee80211_find_node(nt, macaddr);
186 IEEE80211_NODE_UNLOCK(nt);
191 * Reclaim a node. If this is the last reference count then
192 * do the normal free work. Otherwise remove it from the node
193 * table and mark it gone by clearing the back-reference.
196 wlan_node_reclaim(struct ieee80211_node_table *nt, bss_t *ni)
198 IEEE80211_NODE_LOCK(nt);
200 if(ni->ni_list_prev == NULL)
202 /* First in list so fix the list head */
203 nt->nt_node_first = ni->ni_list_next;
207 ni->ni_list_prev->ni_list_next = ni->ni_list_next;
210 if(ni->ni_list_next == NULL)
212 /* Last in list so fix list tail */
213 nt->nt_node_last = ni->ni_list_prev;
217 ni->ni_list_next->ni_list_prev = ni->ni_list_prev;
220 if(ni->ni_hash_prev == NULL)
222 /* First in list so fix the list head */
224 hash = IEEE80211_NODE_HASH(ni->ni_macaddr);
225 nt->nt_hash[hash] = ni->ni_hash_next;
229 ni->ni_hash_prev->ni_hash_next = ni->ni_hash_next;
232 if(ni->ni_hash_next != NULL)
234 ni->ni_hash_next->ni_hash_prev = ni->ni_hash_prev;
238 IEEE80211_NODE_UNLOCK(nt);
242 wlan_node_dec_free(bss_t *ni)
244 if (ieee80211_node_dectestref(ni)) {
250 wlan_free_allnodes(struct ieee80211_node_table *nt)
254 while ((ni = nt->nt_node_first) != NULL) {
255 wlan_node_reclaim(nt, ni);
260 wlan_iterate_nodes(struct ieee80211_node_table *nt, wlan_node_iter_func *f,
266 gen = ++nt->nt_scangen;
268 IEEE80211_NODE_LOCK(nt);
269 for (ni = nt->nt_node_first; ni; ni = ni->ni_list_next) {
270 if (ni->ni_scangen != gen) {
271 ni->ni_scangen = gen;
272 (void) ieee80211_node_incref(ni);
274 wlan_node_dec_free(ni);
277 IEEE80211_NODE_UNLOCK(nt);
281 * Node table support.
284 wlan_node_table_init(void *wmip, struct ieee80211_node_table *nt)
288 AR_DEBUG_PRINTF(ATH_DEBUG_WLAN, ("node table = 0x%lx\n", (unsigned long)nt));
289 IEEE80211_NODE_LOCK_INIT(nt);
291 A_REGISTER_MODULE_DEBUG_INFO(wlan);
293 nt->nt_node_first = nt->nt_node_last = NULL;
294 for(i = 0; i < IEEE80211_NODE_HASHSIZE; i++)
296 nt->nt_hash[i] = NULL;
300 A_INIT_TIMER(&nt->nt_inact_timer, wlan_node_timeout, nt);
301 nt->isTimerArmed = false;
304 nt->nt_nodeAge = WLAN_NODE_INACT_TIMEOUT_MSEC;
307 // nt_scangen never initialized before and during suspend/resume of winmobile,
308 // that some junk has been stored in this, due to this scan list didn't properly updated
312 #ifdef OS_ROAM_MANAGEMENT
318 wlan_set_nodeage(struct ieee80211_node_table *nt, u32 nodeAge)
320 nt->nt_nodeAge = nodeAge;
324 wlan_refresh_inactive_nodes (struct ieee80211_node_table *nt)
327 bss_t *bss, *nextBss;
328 u8 myBssid[IEEE80211_ADDR_LEN], reArmTimer = false;
330 wmi_get_current_bssid(nt->nt_wmip, myBssid);
332 bss = nt->nt_node_first;
335 nextBss = bss->ni_list_next;
336 if (memcmp(myBssid, bss->ni_macaddr, sizeof(myBssid)) != 0)
339 * free up all but the current bss - if set
341 wlan_node_reclaim(nt, bss);
347 bss_t *bss, *nextBss;
348 u8 myBssid[IEEE80211_ADDR_LEN];
349 u32 timeoutValue = 0;
350 u32 now = A_GET_MS(0);
351 timeoutValue = nt->nt_nodeAge;
353 wmi_get_current_bssid(nt->nt_wmip, myBssid);
355 bss = nt->nt_node_first;
358 nextBss = bss->ni_list_next;
359 if (memcmp(myBssid, bss->ni_macaddr, sizeof(myBssid)) != 0)
362 if (((now - bss->ni_tstamp) > timeoutValue) || --bss->ni_actcnt == 0)
365 * free up all but the current bss - if set
367 wlan_node_reclaim(nt, bss);
377 wlan_node_timeout (unsigned long arg)
379 struct ieee80211_node_table *nt = (struct ieee80211_node_table *)arg;
380 bss_t *bss, *nextBss;
381 u8 myBssid[IEEE80211_ADDR_LEN], reArmTimer = false;
382 u32 timeoutValue = 0;
383 u32 now = A_GET_MS(0);
385 timeoutValue = nt->nt_nodeAge;
387 wmi_get_current_bssid(nt->nt_wmip, myBssid);
389 bss = nt->nt_node_first;
392 nextBss = bss->ni_list_next;
393 if (memcmp(myBssid, bss->ni_macaddr, sizeof(myBssid)) != 0)
396 if ((now - bss->ni_tstamp) > timeoutValue)
399 * free up all but the current bss - if set
401 wlan_node_reclaim(nt, bss);
406 * Re-arm timer, only when we have a bss other than
407 * current bss AND it is not aged-out.
416 A_TIMEOUT_MS (&nt->nt_inact_timer, timeoutValue, 0);
418 nt->isTimerArmed = reArmTimer;
423 wlan_node_table_cleanup(struct ieee80211_node_table *nt)
426 A_UNTIMEOUT(&nt->nt_inact_timer);
427 A_DELETE_TIMER(&nt->nt_inact_timer);
429 wlan_free_allnodes(nt);
430 IEEE80211_NODE_LOCK_DESTROY(nt);
434 wlan_find_Ssidnode (struct ieee80211_node_table *nt, u8 *pSsid,
435 u32 ssidLength, bool bIsWPA2, bool bMatchSSID)
440 IEEE80211_NODE_LOCK (nt);
442 for (ni = nt->nt_node_first; ni; ni = ni->ni_list_next) {
443 pIESsid = ni->ni_cie.ie_ssid;
444 if (pIESsid[1] <= 32) {
446 // Step 1 : Check SSID
447 if (0x00 == memcmp (pSsid, &pIESsid[2], ssidLength)) {
450 // Step 2.1 : Check MatchSSID is true, if so, return Matched SSID
451 // Profile, otherwise check whether WPA2 or WPA
453 if (true == bMatchSSID) {
454 ieee80211_node_incref (ni); /* mark referenced */
455 IEEE80211_NODE_UNLOCK (nt);
459 // Step 2 : if SSID matches, check WPA or WPA2
460 if (true == bIsWPA2 && NULL != ni->ni_cie.ie_rsn) {
461 ieee80211_node_incref (ni); /* mark referenced */
462 IEEE80211_NODE_UNLOCK (nt);
465 if (false == bIsWPA2 && NULL != ni->ni_cie.ie_wpa) {
466 ieee80211_node_incref(ni); /* mark referenced */
467 IEEE80211_NODE_UNLOCK (nt);
474 IEEE80211_NODE_UNLOCK (nt);
480 wlan_node_return (struct ieee80211_node_table *nt, bss_t *ni)
482 IEEE80211_NODE_LOCK (nt);
483 wlan_node_dec_free (ni);
484 IEEE80211_NODE_UNLOCK (nt);
488 wlan_node_remove_core (struct ieee80211_node_table *nt, bss_t *ni)
490 if(ni->ni_list_prev == NULL)
492 /* First in list so fix the list head */
493 nt->nt_node_first = ni->ni_list_next;
497 ni->ni_list_prev->ni_list_next = ni->ni_list_next;
500 if(ni->ni_list_next == NULL)
502 /* Last in list so fix list tail */
503 nt->nt_node_last = ni->ni_list_prev;
507 ni->ni_list_next->ni_list_prev = ni->ni_list_prev;
510 if(ni->ni_hash_prev == NULL)
512 /* First in list so fix the list head */
514 hash = IEEE80211_NODE_HASH(ni->ni_macaddr);
515 nt->nt_hash[hash] = ni->ni_hash_next;
519 ni->ni_hash_prev->ni_hash_next = ni->ni_hash_next;
522 if(ni->ni_hash_next != NULL)
524 ni->ni_hash_next->ni_hash_prev = ni->ni_hash_prev;
529 wlan_node_remove(struct ieee80211_node_table *nt, u8 *bssid)
531 bss_t *bss, *nextBss;
533 IEEE80211_NODE_LOCK(nt);
535 bss = nt->nt_node_first;
539 nextBss = bss->ni_list_next;
541 if (memcmp(bssid, bss->ni_macaddr, 6) == 0)
543 wlan_node_remove_core (nt, bss);
544 IEEE80211_NODE_UNLOCK(nt);
551 IEEE80211_NODE_UNLOCK(nt);
556 wlan_find_matching_Ssidnode (struct ieee80211_node_table *nt, u8 *pSsid,
557 u32 ssidLength, u32 dot11AuthMode, u32 authMode,
558 u32 pairwiseCryptoType, u32 grpwiseCryptoTyp)
561 bss_t *best_ni = NULL;
564 IEEE80211_NODE_LOCK (nt);
566 for (ni = nt->nt_node_first; ni; ni = ni->ni_list_next) {
567 pIESsid = ni->ni_cie.ie_ssid;
568 if (pIESsid[1] <= 32) {
570 // Step 1 : Check SSID
571 if (0x00 == memcmp (pSsid, &pIESsid[2], ssidLength)) {
573 if (ni->ni_cie.ie_capInfo & 0x10)
576 if ((NULL != ni->ni_cie.ie_rsn) && (WPA2_PSK_AUTH == authMode))
583 else if (ni->ni_rssi > best_ni->ni_rssi)
588 else if ((NULL != ni->ni_cie.ie_wpa) && (WPA_PSK_AUTH == authMode))
595 else if (ni->ni_rssi > best_ni->ni_rssi)
600 else if (WEP_CRYPT == pairwiseCryptoType)
607 else if (ni->ni_rssi > best_ni->ni_rssi)
616 if ((OPEN_AUTH == authMode) && (NONE_CRYPT == pairwiseCryptoType))
622 else if (ni->ni_rssi > best_ni->ni_rssi)
632 IEEE80211_NODE_UNLOCK (nt);