1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3 *
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5 *
6 ******************************************************************************/
7 #define _RTW_MLME_C_
8
9 #include <linux/etherdevice.h>
10 #include <drv_types.h>
11 #include <rtw_debug.h>
12 #include <hal_btcoex.h>
13 #include <linux/jiffies.h>
14
15 extern u8 rtw_do_join(struct adapter *padapter);
16
rtw_init_mlme_priv(struct adapter * padapter)17 int rtw_init_mlme_priv(struct adapter *padapter)
18 {
19 int i;
20 u8 *pbuf;
21 struct wlan_network *pnetwork;
22 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
23 int res = _SUCCESS;
24
25 pmlmepriv->nic_hdl = (u8 *)padapter;
26
27 pmlmepriv->pscanned = NULL;
28 pmlmepriv->fw_state = WIFI_STATION_STATE; /* Must sync with rtw_wdev_alloc() */
29 /* wdev->iftype = NL80211_IFTYPE_STATION */
30 pmlmepriv->cur_network.network.InfrastructureMode = Ndis802_11AutoUnknown;
31 pmlmepriv->scan_mode = SCAN_ACTIVE;/* 1: active, 0: pasive. Maybe someday we should rename this varable to "active_mode" (Jeff) */
32
33 spin_lock_init(&pmlmepriv->lock);
34 _rtw_init_queue(&pmlmepriv->free_bss_pool);
35 _rtw_init_queue(&pmlmepriv->scanned_queue);
36
37 set_scanned_network_val(pmlmepriv, 0);
38
39 memset(&pmlmepriv->assoc_ssid, 0, sizeof(struct ndis_802_11_ssid));
40
41 pbuf = vzalloc(array_size(MAX_BSS_CNT, sizeof(struct wlan_network)));
42
43 if (!pbuf) {
44 res = _FAIL;
45 goto exit;
46 }
47 pmlmepriv->free_bss_buf = pbuf;
48
49 pnetwork = (struct wlan_network *)pbuf;
50
51 for (i = 0; i < MAX_BSS_CNT; i++) {
52 INIT_LIST_HEAD(&pnetwork->list);
53
54 list_add_tail(&pnetwork->list, &pmlmepriv->free_bss_pool.queue);
55
56 pnetwork++;
57 }
58
59 /* allocate DMA-able/Non-Page memory for cmd_buf and rsp_buf */
60
61 rtw_clear_scan_deny(padapter);
62
63 #define RTW_ROAM_SCAN_RESULT_EXP_MS 5000
64 #define RTW_ROAM_RSSI_DIFF_TH 10
65 #define RTW_ROAM_SCAN_INTERVAL_MS 10000
66
67 pmlmepriv->roam_flags = 0
68 | RTW_ROAM_ON_EXPIRED
69 | RTW_ROAM_ON_RESUME
70 #ifdef CONFIG_LAYER2_ROAMING_ACTIVE /* FIXME */
71 | RTW_ROAM_ACTIVE
72 #endif
73 ;
74
75 pmlmepriv->roam_scanr_exp_ms = RTW_ROAM_SCAN_RESULT_EXP_MS;
76 pmlmepriv->roam_rssi_diff_th = RTW_ROAM_RSSI_DIFF_TH;
77 pmlmepriv->roam_scan_int_ms = RTW_ROAM_SCAN_INTERVAL_MS;
78
79 rtw_init_mlme_timer(padapter);
80
81 exit:
82
83 return res;
84 }
85
rtw_free_mlme_ie_data(u8 ** ppie,u32 * plen)86 static void rtw_free_mlme_ie_data(u8 **ppie, u32 *plen)
87 {
88 if (*ppie) {
89 kfree(*ppie);
90 *plen = 0;
91 *ppie = NULL;
92 }
93 }
94
rtw_free_mlme_priv_ie_data(struct mlme_priv * pmlmepriv)95 void rtw_free_mlme_priv_ie_data(struct mlme_priv *pmlmepriv)
96 {
97 rtw_buf_free(&pmlmepriv->assoc_req, &pmlmepriv->assoc_req_len);
98 rtw_buf_free(&pmlmepriv->assoc_rsp, &pmlmepriv->assoc_rsp_len);
99 rtw_free_mlme_ie_data(&pmlmepriv->wps_beacon_ie, &pmlmepriv->wps_beacon_ie_len);
100 rtw_free_mlme_ie_data(&pmlmepriv->wps_probe_req_ie, &pmlmepriv->wps_probe_req_ie_len);
101 rtw_free_mlme_ie_data(&pmlmepriv->wps_probe_resp_ie, &pmlmepriv->wps_probe_resp_ie_len);
102 rtw_free_mlme_ie_data(&pmlmepriv->wps_assoc_resp_ie, &pmlmepriv->wps_assoc_resp_ie_len);
103
104 rtw_free_mlme_ie_data(&pmlmepriv->p2p_beacon_ie, &pmlmepriv->p2p_beacon_ie_len);
105 rtw_free_mlme_ie_data(&pmlmepriv->p2p_probe_req_ie, &pmlmepriv->p2p_probe_req_ie_len);
106 rtw_free_mlme_ie_data(&pmlmepriv->p2p_probe_resp_ie, &pmlmepriv->p2p_probe_resp_ie_len);
107 rtw_free_mlme_ie_data(&pmlmepriv->p2p_go_probe_resp_ie, &pmlmepriv->p2p_go_probe_resp_ie_len);
108 rtw_free_mlme_ie_data(&pmlmepriv->p2p_assoc_req_ie, &pmlmepriv->p2p_assoc_req_ie_len);
109 }
110
_rtw_free_mlme_priv(struct mlme_priv * pmlmepriv)111 void _rtw_free_mlme_priv(struct mlme_priv *pmlmepriv)
112 {
113 if (pmlmepriv) {
114 rtw_free_mlme_priv_ie_data(pmlmepriv);
115 if (pmlmepriv->free_bss_buf)
116 vfree(pmlmepriv->free_bss_buf);
117 }
118 }
119
120 /*
121 struct wlan_network *_rtw_dequeue_network(struct __queue *queue)
122 {
123 _irqL irqL;
124
125 struct wlan_network *pnetwork;
126
127 spin_lock_bh(&queue->lock);
128
129 if (list_empty(&queue->queue))
130
131 pnetwork = NULL;
132
133 else
134 {
135 pnetwork = LIST_CONTAINOR(get_next(&queue->queue), struct wlan_network, list);
136
137 list_del_init(&(pnetwork->list));
138 }
139
140 spin_unlock_bh(&queue->lock);
141
142 return pnetwork;
143 }
144 */
145
rtw_alloc_network(struct mlme_priv * pmlmepriv)146 struct wlan_network *rtw_alloc_network(struct mlme_priv *pmlmepriv)
147 {
148 struct wlan_network *pnetwork;
149 struct __queue *free_queue = &pmlmepriv->free_bss_pool;
150 struct list_head *plist = NULL;
151
152 spin_lock_bh(&free_queue->lock);
153
154 if (list_empty(&free_queue->queue)) {
155 pnetwork = NULL;
156 goto exit;
157 }
158 plist = get_next(&(free_queue->queue));
159
160 pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list);
161
162 list_del_init(&pnetwork->list);
163
164 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
165 ("rtw_alloc_network: ptr =%p\n", plist));
166 pnetwork->network_type = 0;
167 pnetwork->fixed = false;
168 pnetwork->last_scanned = jiffies;
169 pnetwork->aid = 0;
170 pnetwork->join_res = 0;
171
172 pmlmepriv->num_of_scanned++;
173
174 exit:
175 spin_unlock_bh(&free_queue->lock);
176
177 return pnetwork;
178 }
179
_rtw_free_network(struct mlme_priv * pmlmepriv,struct wlan_network * pnetwork,u8 isfreeall)180 void _rtw_free_network(struct mlme_priv *pmlmepriv, struct wlan_network *pnetwork, u8 isfreeall)
181 {
182 unsigned int delta_time;
183 u32 lifetime = SCANQUEUE_LIFETIME;
184 /* _irqL irqL; */
185 struct __queue *free_queue = &(pmlmepriv->free_bss_pool);
186
187 if (!pnetwork)
188 return;
189
190 if (pnetwork->fixed)
191 return;
192
193 if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true) ||
194 (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true))
195 lifetime = 1;
196
197 if (!isfreeall) {
198 delta_time = jiffies_to_msecs(jiffies - pnetwork->last_scanned);
199 if (delta_time < lifetime)/* unit:msec */
200 return;
201 }
202
203 spin_lock_bh(&free_queue->lock);
204
205 list_del_init(&(pnetwork->list));
206
207 list_add_tail(&(pnetwork->list), &(free_queue->queue));
208
209 pmlmepriv->num_of_scanned--;
210
211 /* DBG_871X("_rtw_free_network:SSID =%s\n", pnetwork->network.Ssid.Ssid); */
212
213 spin_unlock_bh(&free_queue->lock);
214 }
215
_rtw_free_network_nolock(struct mlme_priv * pmlmepriv,struct wlan_network * pnetwork)216 void _rtw_free_network_nolock(struct mlme_priv *pmlmepriv, struct wlan_network *pnetwork)
217 {
218
219 struct __queue *free_queue = &(pmlmepriv->free_bss_pool);
220
221 if (!pnetwork)
222 return;
223
224 if (pnetwork->fixed)
225 return;
226
227 /* spin_lock_irqsave(&free_queue->lock, irqL); */
228
229 list_del_init(&(pnetwork->list));
230
231 list_add_tail(&(pnetwork->list), get_list_head(free_queue));
232
233 pmlmepriv->num_of_scanned--;
234
235 /* spin_unlock_irqrestore(&free_queue->lock, irqL); */
236 }
237
238 /*
239 return the wlan_network with the matching addr
240
241 Shall be called under atomic context... to avoid possible racing condition...
242 */
_rtw_find_network(struct __queue * scanned_queue,u8 * addr)243 struct wlan_network *_rtw_find_network(struct __queue *scanned_queue, u8 *addr)
244 {
245 struct list_head *phead, *plist;
246 struct wlan_network *pnetwork = NULL;
247 u8 zero_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
248
249 if (!memcmp(zero_addr, addr, ETH_ALEN)) {
250 pnetwork = NULL;
251 goto exit;
252 }
253
254 /* spin_lock_bh(&scanned_queue->lock); */
255
256 phead = get_list_head(scanned_queue);
257 plist = get_next(phead);
258
259 while (plist != phead) {
260 pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list);
261
262 if (!memcmp(addr, pnetwork->network.MacAddress, ETH_ALEN))
263 break;
264
265 plist = get_next(plist);
266 }
267
268 if (plist == phead)
269 pnetwork = NULL;
270
271 /* spin_unlock_bh(&scanned_queue->lock); */
272
273 exit:
274 return pnetwork;
275 }
276
rtw_free_network_queue(struct adapter * padapter,u8 isfreeall)277 void rtw_free_network_queue(struct adapter *padapter, u8 isfreeall)
278 {
279 struct list_head *phead, *plist;
280 struct wlan_network *pnetwork;
281 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
282 struct __queue *scanned_queue = &pmlmepriv->scanned_queue;
283
284 spin_lock_bh(&scanned_queue->lock);
285
286 phead = get_list_head(scanned_queue);
287 plist = get_next(phead);
288
289 while (phead != plist) {
290
291 pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list);
292
293 plist = get_next(plist);
294
295 _rtw_free_network(pmlmepriv, pnetwork, isfreeall);
296
297 }
298
299 spin_unlock_bh(&scanned_queue->lock);
300 }
301
rtw_if_up(struct adapter * padapter)302 sint rtw_if_up(struct adapter *padapter)
303 {
304 sint res;
305
306 if (padapter->bDriverStopped || padapter->bSurpriseRemoved ||
307 (check_fwstate(&padapter->mlmepriv, _FW_LINKED) == false)) {
308 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_if_up:bDriverStopped(%d) OR bSurpriseRemoved(%d)", padapter->bDriverStopped, padapter->bSurpriseRemoved));
309 res = false;
310 } else
311 res = true;
312 return res;
313 }
314
rtw_generate_random_ibss(u8 * pibss)315 void rtw_generate_random_ibss(u8 *pibss)
316 {
317 unsigned long curtime = jiffies;
318
319 pibss[0] = 0x02; /* in ad-hoc mode bit1 must set to 1 */
320 pibss[1] = 0x11;
321 pibss[2] = 0x87;
322 pibss[3] = (u8)(curtime & 0xff) ;/* p[0]; */
323 pibss[4] = (u8)((curtime>>8) & 0xff) ;/* p[1]; */
324 pibss[5] = (u8)((curtime>>16) & 0xff) ;/* p[2]; */
325 }
326
rtw_get_capability_from_ie(u8 * ie)327 u8 *rtw_get_capability_from_ie(u8 *ie)
328 {
329 return ie + 8 + 2;
330 }
331
rtw_get_capability(struct wlan_bssid_ex * bss)332 u16 rtw_get_capability(struct wlan_bssid_ex *bss)
333 {
334 __le16 val;
335
336 memcpy((u8 *)&val, rtw_get_capability_from_ie(bss->IEs), 2);
337
338 return le16_to_cpu(val);
339 }
340
rtw_get_beacon_interval_from_ie(u8 * ie)341 u8 *rtw_get_beacon_interval_from_ie(u8 *ie)
342 {
343 return ie + 8;
344 }
345
rtw_free_mlme_priv(struct mlme_priv * pmlmepriv)346 void rtw_free_mlme_priv(struct mlme_priv *pmlmepriv)
347 {
348 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("rtw_free_mlme_priv\n"));
349 _rtw_free_mlme_priv(pmlmepriv);
350 }
351
352 /*
353 static struct wlan_network *rtw_dequeue_network(struct __queue *queue)
354 {
355 struct wlan_network *pnetwork;
356
357 pnetwork = _rtw_dequeue_network(queue);
358 return pnetwork;
359 }
360 */
361
362 void rtw_free_network_nolock(struct adapter *padapter, struct wlan_network *pnetwork);
rtw_free_network_nolock(struct adapter * padapter,struct wlan_network * pnetwork)363 void rtw_free_network_nolock(struct adapter *padapter, struct wlan_network *pnetwork)
364 {
365 /* RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("rtw_free_network ==> ssid = %s\n\n" , pnetwork->network.Ssid.Ssid)); */
366 _rtw_free_network_nolock(&(padapter->mlmepriv), pnetwork);
367 rtw_cfg80211_unlink_bss(padapter, pnetwork);
368 }
369
370 /*
371 return the wlan_network with the matching addr
372
373 Shall be called under atomic context... to avoid possible racing condition...
374 */
rtw_find_network(struct __queue * scanned_queue,u8 * addr)375 struct wlan_network *rtw_find_network(struct __queue *scanned_queue, u8 *addr)
376 {
377 struct wlan_network *pnetwork = _rtw_find_network(scanned_queue, addr);
378
379 return pnetwork;
380 }
381
rtw_is_same_ibss(struct adapter * adapter,struct wlan_network * pnetwork)382 int rtw_is_same_ibss(struct adapter *adapter, struct wlan_network *pnetwork)
383 {
384 int ret = true;
385 struct security_priv *psecuritypriv = &adapter->securitypriv;
386
387 if ((psecuritypriv->dot11PrivacyAlgrthm != _NO_PRIVACY_) &&
388 (pnetwork->network.Privacy == 0))
389 ret = false;
390 else if ((psecuritypriv->dot11PrivacyAlgrthm == _NO_PRIVACY_) &&
391 (pnetwork->network.Privacy == 1))
392 ret = false;
393 else
394 ret = true;
395
396 return ret;
397
398 }
399
is_same_ess(struct wlan_bssid_ex * a,struct wlan_bssid_ex * b)400 inline int is_same_ess(struct wlan_bssid_ex *a, struct wlan_bssid_ex *b)
401 {
402 /* RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("(%s,%d)(%s,%d)\n", */
403 /* a->Ssid.Ssid, a->Ssid.SsidLength, b->Ssid.Ssid, b->Ssid.SsidLength)); */
404 return (a->Ssid.SsidLength == b->Ssid.SsidLength)
405 && !memcmp(a->Ssid.Ssid, b->Ssid.Ssid, a->Ssid.SsidLength);
406 }
407
is_same_network(struct wlan_bssid_ex * src,struct wlan_bssid_ex * dst,u8 feature)408 int is_same_network(struct wlan_bssid_ex *src, struct wlan_bssid_ex *dst, u8 feature)
409 {
410 u16 s_cap, d_cap;
411 __le16 tmps, tmpd;
412
413 if (rtw_bug_check(dst, src, &s_cap, &d_cap) == false)
414 return false;
415
416 memcpy((u8 *)&tmps, rtw_get_capability_from_ie(src->IEs), 2);
417 memcpy((u8 *)&tmpd, rtw_get_capability_from_ie(dst->IEs), 2);
418
419 s_cap = le16_to_cpu(tmps);
420 d_cap = le16_to_cpu(tmpd);
421
422 return (src->Ssid.SsidLength == dst->Ssid.SsidLength) &&
423 /* (src->Configuration.DSConfig == dst->Configuration.DSConfig) && */
424 ((!memcmp(src->MacAddress, dst->MacAddress, ETH_ALEN))) &&
425 ((!memcmp(src->Ssid.Ssid, dst->Ssid.Ssid, src->Ssid.SsidLength))) &&
426 ((s_cap & WLAN_CAPABILITY_IBSS) ==
427 (d_cap & WLAN_CAPABILITY_IBSS)) &&
428 ((s_cap & WLAN_CAPABILITY_BSS) ==
429 (d_cap & WLAN_CAPABILITY_BSS));
430
431 }
432
_rtw_find_same_network(struct __queue * scanned_queue,struct wlan_network * network)433 struct wlan_network *_rtw_find_same_network(struct __queue *scanned_queue, struct wlan_network *network)
434 {
435 struct list_head *phead, *plist;
436 struct wlan_network *found = NULL;
437
438 phead = get_list_head(scanned_queue);
439 plist = get_next(phead);
440
441 while (plist != phead) {
442 found = LIST_CONTAINOR(plist, struct wlan_network, list);
443
444 if (is_same_network(&network->network, &found->network, 0))
445 break;
446
447 plist = get_next(plist);
448 }
449
450 if (plist == phead)
451 found = NULL;
452
453 return found;
454 }
455
rtw_get_oldest_wlan_network(struct __queue * scanned_queue)456 struct wlan_network *rtw_get_oldest_wlan_network(struct __queue *scanned_queue)
457 {
458 struct list_head *plist, *phead;
459
460 struct wlan_network *pwlan = NULL;
461 struct wlan_network *oldest = NULL;
462
463 phead = get_list_head(scanned_queue);
464
465 plist = get_next(phead);
466
467 while (1) {
468
469 if (phead == plist)
470 break;
471
472 pwlan = LIST_CONTAINOR(plist, struct wlan_network, list);
473
474 if (!pwlan->fixed) {
475 if (oldest == NULL || time_after(oldest->last_scanned, pwlan->last_scanned))
476 oldest = pwlan;
477 }
478
479 plist = get_next(plist);
480 }
481 return oldest;
482
483 }
484
update_network(struct wlan_bssid_ex * dst,struct wlan_bssid_ex * src,struct adapter * padapter,bool update_ie)485 void update_network(struct wlan_bssid_ex *dst, struct wlan_bssid_ex *src,
486 struct adapter *padapter, bool update_ie)
487 {
488 long rssi_ori = dst->Rssi;
489
490 u8 sq_smp = src->PhyInfo.SignalQuality;
491
492 u8 ss_final;
493 u8 sq_final;
494 long rssi_final;
495
496 #if defined(DBG_RX_SIGNAL_DISPLAY_SSID_MONITORED) && 1
497 if (strcmp(dst->Ssid.Ssid, DBG_RX_SIGNAL_DISPLAY_SSID_MONITORED) == 0) {
498 DBG_871X(FUNC_ADPT_FMT" %s("MAC_FMT", ch%u) ss_ori:%3u, sq_ori:%3u, rssi_ori:%3ld, ss_smp:%3u, sq_smp:%3u, rssi_smp:%3ld\n"
499 , FUNC_ADPT_ARG(padapter)
500 , src->Ssid.Ssid, MAC_ARG(src->MacAddress), src->Configuration.DSConfig
501 , ss_ori, sq_ori, rssi_ori
502 , ss_smp, sq_smp, rssi_smp
503 );
504 }
505 #endif
506
507 /* The rule below is 1/5 for sample value, 4/5 for history value */
508 if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) && is_same_network(&(padapter->mlmepriv.cur_network.network), src, 0)) {
509 /* Take the recvpriv's value for the connected AP*/
510 ss_final = padapter->recvpriv.signal_strength;
511 sq_final = padapter->recvpriv.signal_qual;
512 /* the rssi value here is undecorated, and will be used for antenna diversity */
513 if (sq_smp != 101) /* from the right channel */
514 rssi_final = (src->Rssi+dst->Rssi*4)/5;
515 else
516 rssi_final = rssi_ori;
517 } else {
518 if (sq_smp != 101) { /* from the right channel */
519 ss_final = ((u32)(src->PhyInfo.SignalStrength)+(u32)(dst->PhyInfo.SignalStrength)*4)/5;
520 sq_final = ((u32)(src->PhyInfo.SignalQuality)+(u32)(dst->PhyInfo.SignalQuality)*4)/5;
521 rssi_final = (src->Rssi+dst->Rssi*4)/5;
522 } else {
523 /* bss info not receiving from the right channel, use the original RX signal infos */
524 ss_final = dst->PhyInfo.SignalStrength;
525 sq_final = dst->PhyInfo.SignalQuality;
526 rssi_final = dst->Rssi;
527 }
528
529 }
530
531 if (update_ie) {
532 dst->Reserved[0] = src->Reserved[0];
533 dst->Reserved[1] = src->Reserved[1];
534 memcpy((u8 *)dst, (u8 *)src, get_wlan_bssid_ex_sz(src));
535 }
536
537 dst->PhyInfo.SignalStrength = ss_final;
538 dst->PhyInfo.SignalQuality = sq_final;
539 dst->Rssi = rssi_final;
540
541 #if defined(DBG_RX_SIGNAL_DISPLAY_SSID_MONITORED) && 1
542 if (strcmp(dst->Ssid.Ssid, DBG_RX_SIGNAL_DISPLAY_SSID_MONITORED) == 0) {
543 DBG_871X(FUNC_ADPT_FMT" %s("MAC_FMT"), SignalStrength:%u, SignalQuality:%u, RawRSSI:%ld\n"
544 , FUNC_ADPT_ARG(padapter)
545 , dst->Ssid.Ssid, MAC_ARG(dst->MacAddress), dst->PhyInfo.SignalStrength, dst->PhyInfo.SignalQuality, dst->Rssi);
546 }
547 #endif
548 }
549
update_current_network(struct adapter * adapter,struct wlan_bssid_ex * pnetwork)550 static void update_current_network(struct adapter *adapter, struct wlan_bssid_ex *pnetwork)
551 {
552 struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
553
554 rtw_bug_check(&(pmlmepriv->cur_network.network),
555 &(pmlmepriv->cur_network.network),
556 &(pmlmepriv->cur_network.network),
557 &(pmlmepriv->cur_network.network));
558
559 if ((check_fwstate(pmlmepriv, _FW_LINKED) == true) && (is_same_network(&(pmlmepriv->cur_network.network), pnetwork, 0))) {
560 /* RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,"Same Network\n"); */
561
562 /* if (pmlmepriv->cur_network.network.IELength<= pnetwork->IELength) */
563 {
564 update_network(&(pmlmepriv->cur_network.network), pnetwork, adapter, true);
565 rtw_update_protection(adapter, (pmlmepriv->cur_network.network.IEs) + sizeof(struct ndis_802_11_fix_ie),
566 pmlmepriv->cur_network.network.IELength);
567 }
568 }
569 }
570
571 /*
572 Caller must hold pmlmepriv->lock first.
573 */
rtw_update_scanned_network(struct adapter * adapter,struct wlan_bssid_ex * target)574 void rtw_update_scanned_network(struct adapter *adapter, struct wlan_bssid_ex *target)
575 {
576 struct list_head *plist, *phead;
577 u32 bssid_ex_sz;
578 struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
579 struct __queue *queue = &(pmlmepriv->scanned_queue);
580 struct wlan_network *pnetwork = NULL;
581 struct wlan_network *oldest = NULL;
582 int target_find = 0;
583 u8 feature = 0;
584
585 spin_lock_bh(&queue->lock);
586 phead = get_list_head(queue);
587 plist = get_next(phead);
588
589 while (1) {
590 if (phead == plist)
591 break;
592
593 pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list);
594
595 rtw_bug_check(pnetwork, pnetwork, pnetwork, pnetwork);
596
597 if (is_same_network(&(pnetwork->network), target, feature)) {
598 target_find = 1;
599 break;
600 }
601
602 if (rtw_roam_flags(adapter)) {
603 /* TODO: don't select netowrk in the same ess as oldest if it's new enough*/
604 }
605
606 if (oldest == NULL || time_after(oldest->last_scanned, pnetwork->last_scanned))
607 oldest = pnetwork;
608
609 plist = get_next(plist);
610
611 }
612
613 /* If we didn't find a match, then get a new network slot to initialize
614 * with this beacon's information */
615 /* if (phead == plist) { */
616 if (!target_find) {
617 if (list_empty(&pmlmepriv->free_bss_pool.queue)) {
618 /* If there are no more slots, expire the oldest */
619 /* list_del_init(&oldest->list); */
620 pnetwork = oldest;
621 if (!pnetwork) {
622 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("\n\n\nsomething wrong here\n\n\n"));
623 goto exit;
624 }
625 memcpy(&(pnetwork->network), target, get_wlan_bssid_ex_sz(target));
626 /* variable initialize */
627 pnetwork->fixed = false;
628 pnetwork->last_scanned = jiffies;
629
630 pnetwork->network_type = 0;
631 pnetwork->aid = 0;
632 pnetwork->join_res = 0;
633
634 /* bss info not receiving from the right channel */
635 if (pnetwork->network.PhyInfo.SignalQuality == 101)
636 pnetwork->network.PhyInfo.SignalQuality = 0;
637 } else {
638 /* Otherwise just pull from the free list */
639
640 pnetwork = rtw_alloc_network(pmlmepriv); /* will update scan_time */
641
642 if (!pnetwork) {
643 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("\n\n\nsomething wrong here\n\n\n"));
644 goto exit;
645 }
646
647 bssid_ex_sz = get_wlan_bssid_ex_sz(target);
648 target->Length = bssid_ex_sz;
649 memcpy(&(pnetwork->network), target, bssid_ex_sz);
650
651 pnetwork->last_scanned = jiffies;
652
653 /* bss info not receiving from the right channel */
654 if (pnetwork->network.PhyInfo.SignalQuality == 101)
655 pnetwork->network.PhyInfo.SignalQuality = 0;
656
657 list_add_tail(&(pnetwork->list), &(queue->queue));
658
659 }
660 } else {
661 /* we have an entry and we are going to update it. But this entry may
662 * be already expired. In this case we do the same as we found a new
663 * net and call the new_net handler
664 */
665 bool update_ie = true;
666
667 pnetwork->last_scanned = jiffies;
668
669 /* target.Reserved[0]== 1, means that scanned network is a bcn frame. */
670 if ((pnetwork->network.IELength > target->IELength) && (target->Reserved[0] == 1))
671 update_ie = false;
672
673 /* probe resp(3) > beacon(1) > probe req(2) */
674 if ((target->Reserved[0] != 2) &&
675 (target->Reserved[0] >= pnetwork->network.Reserved[0])
676 ) {
677 update_ie = true;
678 } else {
679 update_ie = false;
680 }
681
682 update_network(&(pnetwork->network), target, adapter, update_ie);
683 }
684
685 exit:
686 spin_unlock_bh(&queue->lock);
687 }
688
689 void rtw_add_network(struct adapter *adapter, struct wlan_bssid_ex *pnetwork);
rtw_add_network(struct adapter * adapter,struct wlan_bssid_ex * pnetwork)690 void rtw_add_network(struct adapter *adapter, struct wlan_bssid_ex *pnetwork)
691 {
692 /* struct __queue *queue = &(pmlmepriv->scanned_queue); */
693
694 /* spin_lock_bh(&queue->lock); */
695
696 update_current_network(adapter, pnetwork);
697
698 rtw_update_scanned_network(adapter, pnetwork);
699
700 /* spin_unlock_bh(&queue->lock); */
701 }
702
703 /* select the desired network based on the capability of the (i)bss. */
704 /* check items: (1) security */
705 /* (2) network_type */
706 /* (3) WMM */
707 /* (4) HT */
708 /* (5) others */
709 int rtw_is_desired_network(struct adapter *adapter, struct wlan_network *pnetwork);
rtw_is_desired_network(struct adapter * adapter,struct wlan_network * pnetwork)710 int rtw_is_desired_network(struct adapter *adapter, struct wlan_network *pnetwork)
711 {
712 struct security_priv *psecuritypriv = &adapter->securitypriv;
713 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
714 u32 desired_encmode;
715 u32 privacy;
716
717 /* u8 wps_ie[512]; */
718 uint wps_ielen;
719
720 int bselected = true;
721
722 desired_encmode = psecuritypriv->ndisencryptstatus;
723 privacy = pnetwork->network.Privacy;
724
725 if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS)) {
726 if (rtw_get_wps_ie(pnetwork->network.IEs+_FIXED_IE_LENGTH_, pnetwork->network.IELength-_FIXED_IE_LENGTH_, NULL, &wps_ielen))
727 return true;
728 else
729 return false;
730
731 }
732 if (adapter->registrypriv.wifi_spec == 1) { /* for correct flow of 8021X to do.... */
733 u8 *p = NULL;
734 uint ie_len = 0;
735
736 if ((desired_encmode == Ndis802_11EncryptionDisabled) && (privacy != 0))
737 bselected = false;
738
739 if (psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPA2PSK) {
740 p = rtw_get_ie(pnetwork->network.IEs + _BEACON_IE_OFFSET_, _RSN_IE_2_, &ie_len, (pnetwork->network.IELength - _BEACON_IE_OFFSET_));
741 if (p && ie_len > 0)
742 bselected = true;
743 else
744 bselected = false;
745 }
746 }
747
748 if ((desired_encmode != Ndis802_11EncryptionDisabled) && (privacy == 0)) {
749 DBG_871X("desired_encmode: %d, privacy: %d\n", desired_encmode, privacy);
750 bselected = false;
751 }
752
753 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) {
754 if (pnetwork->network.InfrastructureMode != pmlmepriv->cur_network.network.InfrastructureMode)
755 bselected = false;
756 }
757
758 return bselected;
759 }
760
761 /* TODO: Perry : For Power Management */
rtw_atimdone_event_callback(struct adapter * adapter,u8 * pbuf)762 void rtw_atimdone_event_callback(struct adapter *adapter, u8 *pbuf)
763 {
764 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("receive atimdone_event\n"));
765 }
766
rtw_survey_event_callback(struct adapter * adapter,u8 * pbuf)767 void rtw_survey_event_callback(struct adapter *adapter, u8 *pbuf)
768 {
769 u32 len;
770 struct wlan_bssid_ex *pnetwork;
771 struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
772
773 pnetwork = (struct wlan_bssid_ex *)pbuf;
774
775 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_survey_event_callback, ssid =%s\n", pnetwork->Ssid.Ssid));
776
777 len = get_wlan_bssid_ex_sz(pnetwork);
778 if (len > (sizeof(struct wlan_bssid_ex))) {
779 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("\n ****rtw_survey_event_callback: return a wrong bss ***\n"));
780 return;
781 }
782
783 spin_lock_bh(&pmlmepriv->lock);
784
785 /* update IBSS_network 's timestamp */
786 if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) == true) {
787 /* RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,"rtw_survey_event_callback : WIFI_ADHOC_MASTER_STATE\n\n"); */
788 if (!memcmp(&(pmlmepriv->cur_network.network.MacAddress), pnetwork->MacAddress, ETH_ALEN)) {
789 struct wlan_network *ibss_wlan = NULL;
790
791 memcpy(pmlmepriv->cur_network.network.IEs, pnetwork->IEs, 8);
792 spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
793 ibss_wlan = rtw_find_network(&pmlmepriv->scanned_queue, pnetwork->MacAddress);
794 if (ibss_wlan) {
795 memcpy(ibss_wlan->network.IEs, pnetwork->IEs, 8);
796 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
797 goto exit;
798 }
799 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
800 }
801 }
802
803 /* lock pmlmepriv->lock when you accessing network_q */
804 if ((check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) == false) {
805 if (pnetwork->Ssid.Ssid[0] == 0)
806 pnetwork->Ssid.SsidLength = 0;
807 rtw_add_network(adapter, pnetwork);
808 }
809
810 exit:
811
812 spin_unlock_bh(&pmlmepriv->lock);
813 }
814
rtw_surveydone_event_callback(struct adapter * adapter,u8 * pbuf)815 void rtw_surveydone_event_callback(struct adapter *adapter, u8 *pbuf)
816 {
817 struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
818
819 spin_lock_bh(&pmlmepriv->lock);
820 if (pmlmepriv->wps_probe_req_ie) {
821 pmlmepriv->wps_probe_req_ie_len = 0;
822 kfree(pmlmepriv->wps_probe_req_ie);
823 pmlmepriv->wps_probe_req_ie = NULL;
824 }
825
826 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_surveydone_event_callback: fw_state:%x\n\n", get_fwstate(pmlmepriv)));
827
828 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY)) {
829 del_timer_sync(&pmlmepriv->scan_to_timer);
830 _clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
831 } else {
832
833 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("nic status =%x, survey done event comes too late!\n", get_fwstate(pmlmepriv)));
834 }
835
836 rtw_set_signal_stat_timer(&adapter->recvpriv);
837
838 if (pmlmepriv->to_join) {
839 if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true)) {
840 if (check_fwstate(pmlmepriv, _FW_LINKED) == false) {
841 set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
842
843 if (rtw_select_and_join_from_scanned_queue(pmlmepriv) == _SUCCESS) {
844 _set_timer(&pmlmepriv->assoc_timer, MAX_JOIN_TIMEOUT);
845 } else {
846 struct wlan_bssid_ex *pdev_network = &(adapter->registrypriv.dev_network);
847 u8 *pibss = adapter->registrypriv.dev_network.MacAddress;
848
849 /* pmlmepriv->fw_state ^= _FW_UNDER_SURVEY;because don't set assoc_timer */
850 _clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
851
852 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("switching to adhoc master\n"));
853
854 memcpy(&pdev_network->Ssid, &pmlmepriv->assoc_ssid, sizeof(struct ndis_802_11_ssid));
855
856 rtw_update_registrypriv_dev_network(adapter);
857 rtw_generate_random_ibss(pibss);
858
859 pmlmepriv->fw_state = WIFI_ADHOC_MASTER_STATE;
860
861 if (rtw_createbss_cmd(adapter) != _SUCCESS)
862 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Error =>rtw_createbss_cmd status FAIL\n"));
863
864 pmlmepriv->to_join = false;
865 }
866 }
867 } else {
868 int s_ret;
869 set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
870 pmlmepriv->to_join = false;
871 s_ret = rtw_select_and_join_from_scanned_queue(pmlmepriv);
872 if (_SUCCESS == s_ret) {
873 _set_timer(&pmlmepriv->assoc_timer, MAX_JOIN_TIMEOUT);
874 } else if (s_ret == 2) {/* there is no need to wait for join */
875 _clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
876 rtw_indicate_connect(adapter);
877 } else {
878 DBG_871X("try_to_join, but select scanning queue fail, to_roam:%d\n", rtw_to_roam(adapter));
879
880 if (rtw_to_roam(adapter) != 0) {
881 if (rtw_dec_to_roam(adapter) == 0
882 || _SUCCESS != rtw_sitesurvey_cmd(adapter, &pmlmepriv->assoc_ssid, 1, NULL, 0)
883 ) {
884 rtw_set_to_roam(adapter, 0);
885 rtw_free_assoc_resources(adapter, 1);
886 rtw_indicate_disconnect(adapter);
887 } else {
888 pmlmepriv->to_join = true;
889 }
890 } else
891 rtw_indicate_disconnect(adapter);
892
893 _clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
894 }
895 }
896 } else {
897 if (rtw_chk_roam_flags(adapter, RTW_ROAM_ACTIVE)) {
898 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)
899 && check_fwstate(pmlmepriv, _FW_LINKED)) {
900 if (rtw_select_roaming_candidate(pmlmepriv) == _SUCCESS) {
901 receive_disconnect(adapter, pmlmepriv->cur_network.network.MacAddress
902 , WLAN_REASON_ACTIVE_ROAM);
903 }
904 }
905 }
906 }
907
908 /* DBG_871X("scan complete in %dms\n", jiffies_to_msecs(jiffies - pmlmepriv->scan_start_time)); */
909
910 spin_unlock_bh(&pmlmepriv->lock);
911
912 rtw_os_xmit_schedule(adapter);
913
914 rtw_cfg80211_surveydone_event_callback(adapter);
915
916 rtw_indicate_scan_done(adapter, false);
917 }
918
rtw_dummy_event_callback(struct adapter * adapter,u8 * pbuf)919 void rtw_dummy_event_callback(struct adapter *adapter, u8 *pbuf)
920 {
921 }
922
rtw_fwdbg_event_callback(struct adapter * adapter,u8 * pbuf)923 void rtw_fwdbg_event_callback(struct adapter *adapter, u8 *pbuf)
924 {
925 }
926
free_scanqueue(struct mlme_priv * pmlmepriv)927 static void free_scanqueue(struct mlme_priv *pmlmepriv)
928 {
929 struct __queue *free_queue = &pmlmepriv->free_bss_pool;
930 struct __queue *scan_queue = &pmlmepriv->scanned_queue;
931 struct list_head *plist, *phead, *ptemp;
932
933 RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_, ("+free_scanqueue\n"));
934 spin_lock_bh(&scan_queue->lock);
935 spin_lock_bh(&free_queue->lock);
936
937 phead = get_list_head(scan_queue);
938 plist = get_next(phead);
939
940 while (plist != phead) {
941 ptemp = get_next(plist);
942 list_del_init(plist);
943 list_add_tail(plist, &free_queue->queue);
944 plist = ptemp;
945 pmlmepriv->num_of_scanned--;
946 }
947
948 spin_unlock_bh(&free_queue->lock);
949 spin_unlock_bh(&scan_queue->lock);
950 }
951
rtw_reset_rx_info(struct debug_priv * pdbgpriv)952 static void rtw_reset_rx_info(struct debug_priv *pdbgpriv)
953 {
954 pdbgpriv->dbg_rx_ampdu_drop_count = 0;
955 pdbgpriv->dbg_rx_ampdu_forced_indicate_count = 0;
956 pdbgpriv->dbg_rx_ampdu_loss_count = 0;
957 pdbgpriv->dbg_rx_dup_mgt_frame_drop_count = 0;
958 pdbgpriv->dbg_rx_ampdu_window_shift_cnt = 0;
959 }
960
find_network(struct adapter * adapter)961 static void find_network(struct adapter *adapter)
962 {
963 struct wlan_network *pwlan = NULL;
964 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
965 struct wlan_network *tgt_network = &pmlmepriv->cur_network;
966
967 pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.MacAddress);
968 if (pwlan)
969 pwlan->fixed = false;
970 else
971 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("rtw_free_assoc_resources : pwlan == NULL\n\n"));
972
973 if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) &&
974 (adapter->stapriv.asoc_sta_count == 1))
975 rtw_free_network_nolock(adapter, pwlan);
976 }
977
978 /*
979 *rtw_free_assoc_resources: the caller has to lock pmlmepriv->lock
980 */
rtw_free_assoc_resources(struct adapter * adapter,int lock_scanned_queue)981 void rtw_free_assoc_resources(struct adapter *adapter, int lock_scanned_queue)
982 {
983 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
984 struct wlan_network *tgt_network = &pmlmepriv->cur_network;
985 struct sta_priv *pstapriv = &adapter->stapriv;
986 struct dvobj_priv *psdpriv = adapter->dvobj;
987 struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
988
989 RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_, ("+rtw_free_assoc_resources\n"));
990 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("tgt_network->network.MacAddress ="MAC_FMT" ssid =%s\n",
991 MAC_ARG(tgt_network->network.MacAddress), tgt_network->network.Ssid.Ssid));
992
993 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE|WIFI_AP_STATE)) {
994 struct sta_info *psta;
995
996 psta = rtw_get_stainfo(&adapter->stapriv, tgt_network->network.MacAddress);
997 spin_lock_bh(&(pstapriv->sta_hash_lock));
998 rtw_free_stainfo(adapter, psta);
999
1000 spin_unlock_bh(&(pstapriv->sta_hash_lock));
1001
1002 }
1003
1004 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE|WIFI_ADHOC_MASTER_STATE|WIFI_AP_STATE)) {
1005 struct sta_info *psta;
1006
1007 rtw_free_all_stainfo(adapter);
1008
1009 psta = rtw_get_bcmc_stainfo(adapter);
1010 rtw_free_stainfo(adapter, psta);
1011
1012 rtw_init_bcmc_stainfo(adapter);
1013 }
1014
1015 find_network(adapter);
1016
1017 if (lock_scanned_queue)
1018 adapter->securitypriv.key_mask = 0;
1019
1020 rtw_reset_rx_info(pdbgpriv);
1021 }
1022
1023 /*
1024 *rtw_indicate_connect: the caller has to lock pmlmepriv->lock
1025 */
rtw_indicate_connect(struct adapter * padapter)1026 void rtw_indicate_connect(struct adapter *padapter)
1027 {
1028 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1029
1030 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("+rtw_indicate_connect\n"));
1031
1032 pmlmepriv->to_join = false;
1033
1034 if (!check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
1035
1036 set_fwstate(pmlmepriv, _FW_LINKED);
1037
1038 rtw_os_indicate_connect(padapter);
1039 }
1040
1041 rtw_set_to_roam(padapter, 0);
1042 rtw_set_scan_deny(padapter, 3000);
1043
1044 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("-rtw_indicate_connect: fw_state = 0x%08x\n", get_fwstate(pmlmepriv)));
1045 }
1046
1047 /*
1048 *rtw_indicate_disconnect: the caller has to lock pmlmepriv->lock
1049 */
rtw_indicate_disconnect(struct adapter * padapter)1050 void rtw_indicate_disconnect(struct adapter *padapter)
1051 {
1052 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1053
1054 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("+rtw_indicate_disconnect\n"));
1055
1056 _clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING|WIFI_UNDER_WPS);
1057
1058 /* DBG_871X("clear wps when %s\n", __func__); */
1059
1060 if (rtw_to_roam(padapter) > 0)
1061 _clr_fwstate_(pmlmepriv, _FW_LINKED);
1062
1063 if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)
1064 || (rtw_to_roam(padapter) <= 0)
1065 ) {
1066 rtw_os_indicate_disconnect(padapter);
1067
1068 /* set ips_deny_time to avoid enter IPS before LPS leave */
1069 rtw_set_ips_deny(padapter, 3000);
1070
1071 _clr_fwstate_(pmlmepriv, _FW_LINKED);
1072
1073 rtw_clear_scan_deny(padapter);
1074 }
1075
1076 rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_DISCONNECT, 1);
1077 }
1078
rtw_indicate_scan_done(struct adapter * padapter,bool aborted)1079 inline void rtw_indicate_scan_done(struct adapter *padapter, bool aborted)
1080 {
1081 DBG_871X(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter));
1082
1083 rtw_os_indicate_scan_done(padapter, aborted);
1084
1085 if (is_primary_adapter(padapter) &&
1086 (!adapter_to_pwrctl(padapter)->bInSuspend) &&
1087 (!check_fwstate(&padapter->mlmepriv,
1088 WIFI_ASOC_STATE|WIFI_UNDER_LINKING))) {
1089 rtw_set_ips_deny(padapter, 0);
1090 _set_timer(&padapter->mlmepriv.dynamic_chk_timer, 1);
1091 }
1092 }
1093
rtw_scan_abort(struct adapter * adapter)1094 void rtw_scan_abort(struct adapter *adapter)
1095 {
1096 unsigned long start;
1097 struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
1098 struct mlme_ext_priv *pmlmeext = &(adapter->mlmeextpriv);
1099
1100 start = jiffies;
1101 pmlmeext->scan_abort = true;
1102 while (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY)
1103 && jiffies_to_msecs(start) <= 200) {
1104
1105 if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
1106 break;
1107
1108 DBG_871X(FUNC_NDEV_FMT"fw_state = _FW_UNDER_SURVEY!\n", FUNC_NDEV_ARG(adapter->pnetdev));
1109 msleep(20);
1110 }
1111
1112 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY)) {
1113 if (!adapter->bDriverStopped && !adapter->bSurpriseRemoved)
1114 DBG_871X(FUNC_NDEV_FMT"waiting for scan_abort time out!\n", FUNC_NDEV_ARG(adapter->pnetdev));
1115 rtw_indicate_scan_done(adapter, true);
1116 }
1117 pmlmeext->scan_abort = false;
1118 }
1119
rtw_joinbss_update_stainfo(struct adapter * padapter,struct wlan_network * pnetwork)1120 static struct sta_info *rtw_joinbss_update_stainfo(struct adapter *padapter, struct wlan_network *pnetwork)
1121 {
1122 int i;
1123 struct sta_info *bmc_sta, *psta = NULL;
1124 struct recv_reorder_ctrl *preorder_ctrl;
1125 struct sta_priv *pstapriv = &padapter->stapriv;
1126 struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1127
1128 psta = rtw_get_stainfo(pstapriv, pnetwork->network.MacAddress);
1129 if (!psta)
1130 psta = rtw_alloc_stainfo(pstapriv, pnetwork->network.MacAddress);
1131
1132 if (psta) { /* update ptarget_sta */
1133
1134 DBG_871X("%s\n", __func__);
1135
1136 psta->aid = pnetwork->join_res;
1137
1138 update_sta_info(padapter, psta);
1139
1140 /* update station supportRate */
1141 psta->bssratelen = rtw_get_rateset_len(pnetwork->network.SupportedRates);
1142 memcpy(psta->bssrateset, pnetwork->network.SupportedRates, psta->bssratelen);
1143 rtw_hal_update_sta_rate_mask(padapter, psta);
1144
1145 psta->wireless_mode = pmlmeext->cur_wireless_mode;
1146 psta->raid = networktype_to_raid_ex(padapter, psta);
1147
1148 /* sta mode */
1149 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, true);
1150
1151 /* security related */
1152 if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) {
1153 padapter->securitypriv.binstallGrpkey = false;
1154 padapter->securitypriv.busetkipkey = false;
1155 padapter->securitypriv.bgrpkey_handshake = false;
1156
1157 psta->ieee8021x_blocked = true;
1158 psta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm;
1159
1160 memset((u8 *)&psta->dot118021x_UncstKey, 0, sizeof(union Keytype));
1161
1162 memset((u8 *)&psta->dot11tkiprxmickey, 0, sizeof(union Keytype));
1163 memset((u8 *)&psta->dot11tkiptxmickey, 0, sizeof(union Keytype));
1164
1165 memset((u8 *)&psta->dot11txpn, 0, sizeof(union pn48));
1166 psta->dot11txpn.val = psta->dot11txpn.val + 1;
1167 memset((u8 *)&psta->dot11wtxpn, 0, sizeof(union pn48));
1168 memset((u8 *)&psta->dot11rxpn, 0, sizeof(union pn48));
1169 }
1170
1171 /* Commented by Albert 2012/07/21 */
1172 /* When doing the WPS, the wps_ie_len won't equal to 0 */
1173 /* And the Wi-Fi driver shouldn't allow the data packet to be transmitted. */
1174 if (padapter->securitypriv.wps_ie_len != 0) {
1175 psta->ieee8021x_blocked = true;
1176 padapter->securitypriv.wps_ie_len = 0;
1177 }
1178
1179 /* for A-MPDU Rx reordering buffer control for bmc_sta & sta_info */
1180 /* if A-MPDU Rx is enabled, resetting rx_ordering_ctrl wstart_b(indicate_seq) to default value = 0xffff */
1181 /* todo: check if AP can send A-MPDU packets */
1182 for (i = 0; i < 16 ; i++) {
1183 /* preorder_ctrl = &precvpriv->recvreorder_ctrl[i]; */
1184 preorder_ctrl = &psta->recvreorder_ctrl[i];
1185 preorder_ctrl->enable = false;
1186 preorder_ctrl->indicate_seq = 0xffff;
1187 #ifdef DBG_RX_SEQ
1188 DBG_871X("DBG_RX_SEQ %s:%d indicate_seq:%u\n", __func__, __LINE__,
1189 preorder_ctrl->indicate_seq);
1190 #endif
1191 preorder_ctrl->wend_b = 0xffff;
1192 preorder_ctrl->wsize_b = 64;/* max_ampdu_sz;ex. 32(kbytes) -> wsize_b =32 */
1193 }
1194
1195 bmc_sta = rtw_get_bcmc_stainfo(padapter);
1196 if (bmc_sta) {
1197 for (i = 0; i < 16 ; i++) {
1198 /* preorder_ctrl = &precvpriv->recvreorder_ctrl[i]; */
1199 preorder_ctrl = &bmc_sta->recvreorder_ctrl[i];
1200 preorder_ctrl->enable = false;
1201 preorder_ctrl->indicate_seq = 0xffff;
1202 #ifdef DBG_RX_SEQ
1203 DBG_871X("DBG_RX_SEQ %s:%d indicate_seq:%u\n", __func__, __LINE__,
1204 preorder_ctrl->indicate_seq);
1205 #endif
1206 preorder_ctrl->wend_b = 0xffff;
1207 preorder_ctrl->wsize_b = 64;/* max_ampdu_sz;ex. 32(kbytes) -> wsize_b =32 */
1208 }
1209 }
1210 }
1211
1212 return psta;
1213
1214 }
1215
1216 /* pnetwork : returns from rtw_joinbss_event_callback */
1217 /* ptarget_wlan: found from scanned_queue */
rtw_joinbss_update_network(struct adapter * padapter,struct wlan_network * ptarget_wlan,struct wlan_network * pnetwork)1218 static void rtw_joinbss_update_network(struct adapter *padapter, struct wlan_network *ptarget_wlan, struct wlan_network *pnetwork)
1219 {
1220 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1221 struct wlan_network *cur_network = &(pmlmepriv->cur_network);
1222
1223 DBG_871X("%s\n", __func__);
1224
1225 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\nfw_state:%x, BSSID:"MAC_FMT"\n"
1226 , get_fwstate(pmlmepriv), MAC_ARG(pnetwork->network.MacAddress)));
1227
1228 /* why not use ptarget_wlan?? */
1229 memcpy(&cur_network->network, &pnetwork->network, pnetwork->network.Length);
1230 /* some IEs in pnetwork is wrong, so we should use ptarget_wlan IEs */
1231 cur_network->network.IELength = ptarget_wlan->network.IELength;
1232 memcpy(&cur_network->network.IEs[0], &ptarget_wlan->network.IEs[0], MAX_IE_SZ);
1233
1234 cur_network->aid = pnetwork->join_res;
1235
1236 rtw_set_signal_stat_timer(&padapter->recvpriv);
1237
1238 padapter->recvpriv.signal_strength = ptarget_wlan->network.PhyInfo.SignalStrength;
1239 padapter->recvpriv.signal_qual = ptarget_wlan->network.PhyInfo.SignalQuality;
1240 /* the ptarget_wlan->network.Rssi is raw data, we use ptarget_wlan->network.PhyInfo.SignalStrength instead (has scaled) */
1241 padapter->recvpriv.rssi = translate_percentage_to_dbm(ptarget_wlan->network.PhyInfo.SignalStrength);
1242 #if defined(DBG_RX_SIGNAL_DISPLAY_PROCESSING) && 1
1243 DBG_871X(FUNC_ADPT_FMT" signal_strength:%3u, rssi:%3d, signal_qual:%3u"
1244 "\n"
1245 , FUNC_ADPT_ARG(padapter)
1246 , padapter->recvpriv.signal_strength
1247 , padapter->recvpriv.rssi
1248 , padapter->recvpriv.signal_qual
1249 );
1250 #endif
1251
1252 rtw_set_signal_stat_timer(&padapter->recvpriv);
1253
1254 /* update fw_state will clr _FW_UNDER_LINKING here indirectly */
1255 switch (pnetwork->network.InfrastructureMode) {
1256 case Ndis802_11Infrastructure:
1257
1258 if (pmlmepriv->fw_state&WIFI_UNDER_WPS)
1259 pmlmepriv->fw_state = WIFI_STATION_STATE|WIFI_UNDER_WPS;
1260 else
1261 pmlmepriv->fw_state = WIFI_STATION_STATE;
1262
1263 break;
1264 case Ndis802_11IBSS:
1265 pmlmepriv->fw_state = WIFI_ADHOC_STATE;
1266 break;
1267 default:
1268 pmlmepriv->fw_state = WIFI_NULL_STATE;
1269 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Invalid network_mode\n"));
1270 break;
1271 }
1272
1273 rtw_update_protection(padapter, (cur_network->network.IEs) + sizeof(struct ndis_802_11_fix_ie),
1274 (cur_network->network.IELength));
1275
1276 rtw_update_ht_cap(padapter, cur_network->network.IEs, cur_network->network.IELength, (u8) cur_network->network.Configuration.DSConfig);
1277 }
1278
1279 /* Notes: the function could be > passive_level (the same context as Rx tasklet) */
1280 /* pnetwork : returns from rtw_joinbss_event_callback */
1281 /* ptarget_wlan: found from scanned_queue */
1282 /* if join_res > 0, for (fw_state ==WIFI_STATION_STATE), we check if "ptarget_sta" & "ptarget_wlan" exist. */
1283 /* if join_res > 0, for (fw_state ==WIFI_ADHOC_STATE), we only check if "ptarget_wlan" exist. */
1284 /* if join_res > 0, update "cur_network->network" from "pnetwork->network" if (ptarget_wlan != NULL). */
1285 /* */
1286 /* define REJOIN */
rtw_joinbss_event_prehandle(struct adapter * adapter,u8 * pbuf)1287 void rtw_joinbss_event_prehandle(struct adapter *adapter, u8 *pbuf)
1288 {
1289 static u8 retry;
1290 struct sta_info *ptarget_sta = NULL, *pcur_sta = NULL;
1291 struct sta_priv *pstapriv = &adapter->stapriv;
1292 struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
1293 struct wlan_network *pnetwork = (struct wlan_network *)pbuf;
1294 struct wlan_network *cur_network = &(pmlmepriv->cur_network);
1295 struct wlan_network *pcur_wlan = NULL, *ptarget_wlan = NULL;
1296 unsigned int the_same_macaddr = false;
1297
1298 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("joinbss event call back received with res =%d\n", pnetwork->join_res));
1299
1300 rtw_get_encrypt_decrypt_from_registrypriv(adapter);
1301
1302 if (pmlmepriv->assoc_ssid.SsidLength == 0)
1303 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("@@@@@ joinbss event call back for Any SSid\n"));
1304 else
1305 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("@@@@@ rtw_joinbss_event_callback for SSid:%s\n", pmlmepriv->assoc_ssid.Ssid));
1306
1307 the_same_macaddr = !memcmp(pnetwork->network.MacAddress, cur_network->network.MacAddress, ETH_ALEN);
1308
1309 pnetwork->network.Length = get_wlan_bssid_ex_sz(&pnetwork->network);
1310 if (pnetwork->network.Length > sizeof(struct wlan_bssid_ex)) {
1311 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("\n\n ***joinbss_evt_callback return a wrong bss ***\n\n"));
1312 return;
1313 }
1314
1315 spin_lock_bh(&pmlmepriv->lock);
1316
1317 pmlmepriv->LinkDetectInfo.TrafficTransitionCount = 0;
1318 pmlmepriv->LinkDetectInfo.LowPowerTransitionCount = 0;
1319
1320 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\n rtw_joinbss_event_callback !! spin_lock_irqsave\n"));
1321
1322 if (pnetwork->join_res > 0) {
1323 spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1324 retry = 0;
1325 if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) {
1326 /* s1. find ptarget_wlan */
1327 if (check_fwstate(pmlmepriv, _FW_LINKED)) {
1328 if (the_same_macaddr) {
1329 ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.MacAddress);
1330 } else {
1331 pcur_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.MacAddress);
1332 if (pcur_wlan)
1333 pcur_wlan->fixed = false;
1334
1335 pcur_sta = rtw_get_stainfo(pstapriv, cur_network->network.MacAddress);
1336 if (pcur_sta)
1337 rtw_free_stainfo(adapter, pcur_sta);
1338
1339 ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, pnetwork->network.MacAddress);
1340 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
1341 if (ptarget_wlan)
1342 ptarget_wlan->fixed = true;
1343 }
1344 }
1345
1346 } else {
1347 ptarget_wlan = _rtw_find_same_network(&pmlmepriv->scanned_queue, pnetwork);
1348 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
1349 if (ptarget_wlan)
1350 ptarget_wlan->fixed = true;
1351 }
1352 }
1353
1354 /* s2. update cur_network */
1355 if (ptarget_wlan) {
1356 rtw_joinbss_update_network(adapter, ptarget_wlan, pnetwork);
1357 } else {
1358 DBG_871X_LEVEL(_drv_always_, "Can't find ptarget_wlan when joinbss_event callback\n");
1359 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1360 goto ignore_joinbss_callback;
1361 }
1362
1363 /* s3. find ptarget_sta & update ptarget_sta after update cur_network only for station mode */
1364 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
1365 ptarget_sta = rtw_joinbss_update_stainfo(adapter, pnetwork);
1366 if (!ptarget_sta) {
1367 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Can't update stainfo when joinbss_event callback\n"));
1368 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1369 goto ignore_joinbss_callback;
1370 }
1371 }
1372
1373 /* s4. indicate connect */
1374 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
1375 pmlmepriv->cur_network_scanned = ptarget_wlan;
1376 rtw_indicate_connect(adapter);
1377 } else {
1378 /* adhoc mode will rtw_indicate_connect when rtw_stassoc_event_callback */
1379 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("adhoc mode, fw_state:%x", get_fwstate(pmlmepriv)));
1380 }
1381
1382 /* s5. Cancel assoc_timer */
1383 del_timer_sync(&pmlmepriv->assoc_timer);
1384
1385 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("Cancel assoc_timer\n"));
1386
1387 } else {
1388 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("rtw_joinbss_event_callback err: fw_state:%x", get_fwstate(pmlmepriv)));
1389 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1390 goto ignore_joinbss_callback;
1391 }
1392
1393 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1394
1395 } else if (pnetwork->join_res == -4) {
1396 rtw_reset_securitypriv(adapter);
1397 _set_timer(&pmlmepriv->assoc_timer, 1);
1398
1399 /* rtw_free_assoc_resources(adapter, 1); */
1400
1401 if ((check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) == true) {
1402 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("fail! clear _FW_UNDER_LINKING ^^^fw_state =%x\n", get_fwstate(pmlmepriv)));
1403 _clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
1404 }
1405
1406 } else {/* if join_res < 0 (join fails), then try again */
1407
1408 #ifdef REJOIN
1409 res = _FAIL;
1410 if (retry < 2) {
1411 res = rtw_select_and_join_from_scanned_queue(pmlmepriv);
1412 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("rtw_select_and_join_from_scanned_queue again! res:%d\n", res));
1413 }
1414
1415 if (res == _SUCCESS) {
1416 /* extend time of assoc_timer */
1417 _set_timer(&pmlmepriv->assoc_timer, MAX_JOIN_TIMEOUT);
1418 retry++;
1419 } else if (res == 2) {/* there is no need to wait for join */
1420 _clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
1421 rtw_indicate_connect(adapter);
1422 } else {
1423 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Set Assoc_Timer = 1; can't find match ssid in scanned_q\n"));
1424 #endif
1425
1426 _set_timer(&pmlmepriv->assoc_timer, 1);
1427 /* rtw_free_assoc_resources(adapter, 1); */
1428 _clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
1429
1430 #ifdef REJOIN
1431 retry = 0;
1432 }
1433 #endif
1434 }
1435
1436 ignore_joinbss_callback:
1437
1438 spin_unlock_bh(&pmlmepriv->lock);
1439 }
1440
rtw_joinbss_event_callback(struct adapter * adapter,u8 * pbuf)1441 void rtw_joinbss_event_callback(struct adapter *adapter, u8 *pbuf)
1442 {
1443 struct wlan_network *pnetwork = (struct wlan_network *)pbuf;
1444
1445 mlmeext_joinbss_event_callback(adapter, pnetwork->join_res);
1446
1447 rtw_os_xmit_schedule(adapter);
1448 }
1449
1450 /* FOR STA, AP , AD-HOC mode */
rtw_sta_media_status_rpt(struct adapter * adapter,struct sta_info * psta,u32 mstatus)1451 void rtw_sta_media_status_rpt(struct adapter *adapter, struct sta_info *psta, u32 mstatus)
1452 {
1453 u16 media_status_rpt;
1454
1455 if (!psta)
1456 return;
1457
1458 media_status_rpt = (u16)((psta->mac_id<<8)|mstatus); /* MACID|OPMODE:1 connect */
1459 rtw_hal_set_hwreg(adapter, HW_VAR_H2C_MEDIA_STATUS_RPT, (u8 *)&media_status_rpt);
1460 }
1461
rtw_stassoc_event_callback(struct adapter * adapter,u8 * pbuf)1462 void rtw_stassoc_event_callback(struct adapter *adapter, u8 *pbuf)
1463 {
1464 struct sta_info *psta;
1465 struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
1466 struct stassoc_event *pstassoc = (struct stassoc_event *)pbuf;
1467 struct wlan_network *cur_network = &(pmlmepriv->cur_network);
1468 struct wlan_network *ptarget_wlan = NULL;
1469
1470 if (rtw_access_ctrl(adapter, pstassoc->macaddr) == false)
1471 return;
1472
1473 if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1474 psta = rtw_get_stainfo(&adapter->stapriv, pstassoc->macaddr);
1475 if (psta) {
1476 u8 *passoc_req = NULL;
1477 u32 assoc_req_len = 0;
1478
1479 rtw_sta_media_status_rpt(adapter, psta, 1);
1480
1481 #ifndef CONFIG_AUTO_AP_MODE
1482
1483 ap_sta_info_defer_update(adapter, psta);
1484
1485 /* report to upper layer */
1486 DBG_871X("indicate_sta_assoc_event to upper layer - hostapd\n");
1487 spin_lock_bh(&psta->lock);
1488 if (psta->passoc_req && psta->assoc_req_len > 0) {
1489 passoc_req = rtw_zmalloc(psta->assoc_req_len);
1490 if (passoc_req) {
1491 assoc_req_len = psta->assoc_req_len;
1492 memcpy(passoc_req, psta->passoc_req, assoc_req_len);
1493
1494 kfree(psta->passoc_req);
1495 psta->passoc_req = NULL;
1496 psta->assoc_req_len = 0;
1497 }
1498 }
1499 spin_unlock_bh(&psta->lock);
1500
1501 if (passoc_req && assoc_req_len > 0) {
1502 rtw_cfg80211_indicate_sta_assoc(adapter, passoc_req, assoc_req_len);
1503
1504 kfree(passoc_req);
1505 }
1506 #endif /* CONFIG_AUTO_AP_MODE */
1507 }
1508 return;
1509 }
1510
1511 /* for AD-HOC mode */
1512 psta = rtw_get_stainfo(&adapter->stapriv, pstassoc->macaddr);
1513 if (psta) {
1514 /* the sta have been in sta_info_queue => do nothing */
1515
1516 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Error: rtw_stassoc_event_callback: sta has been in sta_hash_queue\n"));
1517
1518 return; /* between drv has received this event before and fw have not yet to set key to CAM_ENTRY) */
1519 }
1520
1521 psta = rtw_alloc_stainfo(&adapter->stapriv, pstassoc->macaddr);
1522 if (!psta) {
1523 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Can't alloc sta_info when rtw_stassoc_event_callback\n"));
1524 return;
1525 }
1526
1527 /* to do : init sta_info variable */
1528 psta->qos_option = 0;
1529 psta->mac_id = (uint)pstassoc->cam_id;
1530 /* psta->aid = (uint)pstassoc->cam_id; */
1531 DBG_871X("%s\n", __func__);
1532 /* for ad-hoc mode */
1533 rtw_hal_set_odm_var(adapter, HAL_ODM_STA_INFO, psta, true);
1534
1535 rtw_sta_media_status_rpt(adapter, psta, 1);
1536
1537 if (adapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X)
1538 psta->dot118021XPrivacy = adapter->securitypriv.dot11PrivacyAlgrthm;
1539
1540 psta->ieee8021x_blocked = false;
1541
1542 spin_lock_bh(&pmlmepriv->lock);
1543
1544 if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true) ||
1545 (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true)) {
1546 if (adapter->stapriv.asoc_sta_count == 2) {
1547 spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1548 ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.MacAddress);
1549 pmlmepriv->cur_network_scanned = ptarget_wlan;
1550 if (ptarget_wlan)
1551 ptarget_wlan->fixed = true;
1552 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1553 /* a sta + bc/mc_stainfo (not Ibss_stainfo) */
1554 rtw_indicate_connect(adapter);
1555 }
1556 }
1557
1558 spin_unlock_bh(&pmlmepriv->lock);
1559
1560 mlmeext_sta_add_event_callback(adapter, psta);
1561 }
1562
rtw_stadel_event_callback(struct adapter * adapter,u8 * pbuf)1563 void rtw_stadel_event_callback(struct adapter *adapter, u8 *pbuf)
1564 {
1565 int mac_id = (-1);
1566 struct sta_info *psta;
1567 struct wlan_network *pwlan = NULL;
1568 struct wlan_bssid_ex *pdev_network = NULL;
1569 u8 *pibss = NULL;
1570 struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
1571 struct stadel_event *pstadel = (struct stadel_event *)pbuf;
1572 struct wlan_network *tgt_network = &(pmlmepriv->cur_network);
1573 struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
1574 struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1575
1576 psta = rtw_get_stainfo(&adapter->stapriv, pstadel->macaddr);
1577 if (psta)
1578 mac_id = psta->mac_id;
1579 else
1580 mac_id = pstadel->mac_id;
1581
1582 DBG_871X("%s(mac_id =%d) =" MAC_FMT "\n", __func__, mac_id, MAC_ARG(pstadel->macaddr));
1583
1584 if (mac_id >= 0) {
1585 u16 media_status;
1586 media_status = (mac_id<<8)|0; /* MACID|OPMODE:0 means disconnect */
1587 /* for STA, AP, ADHOC mode, report disconnect stauts to FW */
1588 rtw_hal_set_hwreg(adapter, HW_VAR_H2C_MEDIA_STATUS_RPT, (u8 *)&media_status);
1589 }
1590
1591 /* if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) */
1592 if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)
1593 return;
1594
1595 mlmeext_sta_del_event_callback(adapter);
1596
1597 spin_lock_bh(&pmlmepriv->lock);
1598
1599 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1600 u16 reason = *((unsigned short *)(pstadel->rsvd));
1601 bool roam = false;
1602 struct wlan_network *roam_target = NULL;
1603
1604 if (adapter->registrypriv.wifi_spec == 1) {
1605 roam = false;
1606 } else if (reason == WLAN_REASON_EXPIRATION_CHK && rtw_chk_roam_flags(adapter, RTW_ROAM_ON_EXPIRED)) {
1607 roam = true;
1608 } else if (reason == WLAN_REASON_ACTIVE_ROAM && rtw_chk_roam_flags(adapter, RTW_ROAM_ACTIVE)) {
1609 roam = true;
1610 roam_target = pmlmepriv->roam_network;
1611 }
1612
1613 if (roam) {
1614 if (rtw_to_roam(adapter) > 0)
1615 rtw_dec_to_roam(adapter); /* this stadel_event is caused by roaming, decrease to_roam */
1616 else if (rtw_to_roam(adapter) == 0)
1617 rtw_set_to_roam(adapter, adapter->registrypriv.max_roaming_times);
1618 } else {
1619 rtw_set_to_roam(adapter, 0);
1620 }
1621
1622 rtw_free_uc_swdec_pending_queue(adapter);
1623
1624 rtw_free_assoc_resources(adapter, 1);
1625 rtw_indicate_disconnect(adapter);
1626
1627 spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1628 /* remove the network entry in scanned_queue */
1629 pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.MacAddress);
1630 if (pwlan) {
1631 pwlan->fixed = false;
1632 rtw_free_network_nolock(adapter, pwlan);
1633 }
1634 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1635
1636 _rtw_roaming(adapter, roam_target);
1637 }
1638
1639 if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) ||
1640 check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
1641
1642 rtw_free_stainfo(adapter, psta);
1643
1644 if (adapter->stapriv.asoc_sta_count == 1) {/* a sta + bc/mc_stainfo (not Ibss_stainfo) */
1645 /* rtw_indicate_disconnect(adapter);removed@20091105 */
1646 spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1647 /* free old ibss network */
1648 /* pwlan = rtw_find_network(&pmlmepriv->scanned_queue, pstadel->macaddr); */
1649 pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.MacAddress);
1650 if (pwlan) {
1651 pwlan->fixed = false;
1652 rtw_free_network_nolock(adapter, pwlan);
1653 }
1654 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1655 /* re-create ibss */
1656 pdev_network = &(adapter->registrypriv.dev_network);
1657 pibss = adapter->registrypriv.dev_network.MacAddress;
1658
1659 memcpy(pdev_network, &tgt_network->network, get_wlan_bssid_ex_sz(&tgt_network->network));
1660
1661 memcpy(&pdev_network->Ssid, &pmlmepriv->assoc_ssid, sizeof(struct ndis_802_11_ssid));
1662
1663 rtw_update_registrypriv_dev_network(adapter);
1664
1665 rtw_generate_random_ibss(pibss);
1666
1667 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
1668 set_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE);
1669 _clr_fwstate_(pmlmepriv, WIFI_ADHOC_STATE);
1670 }
1671
1672 if (rtw_createbss_cmd(adapter) != _SUCCESS)
1673 RT_TRACE(_module_rtl871x_ioctl_set_c_, _drv_err_, ("***Error =>stadel_event_callback: rtw_createbss_cmd status FAIL***\n "));
1674 }
1675
1676 }
1677
1678 spin_unlock_bh(&pmlmepriv->lock);
1679 }
1680
rtw_cpwm_event_callback(struct adapter * padapter,u8 * pbuf)1681 void rtw_cpwm_event_callback(struct adapter *padapter, u8 *pbuf)
1682 {
1683 struct reportpwrstate_parm *preportpwrstate;
1684
1685 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("+rtw_cpwm_event_callback !!!\n"));
1686 preportpwrstate = (struct reportpwrstate_parm *)pbuf;
1687 preportpwrstate->state |= (u8)(adapter_to_pwrctl(padapter)->cpwm_tog + 0x80);
1688 cpwm_int_hdl(padapter, preportpwrstate);
1689 }
1690
rtw_wmm_event_callback(struct adapter * padapter,u8 * pbuf)1691 void rtw_wmm_event_callback(struct adapter *padapter, u8 *pbuf)
1692 {
1693 WMMOnAssocRsp(padapter);
1694 }
1695
1696 /*
1697 * _rtw_join_timeout_handler - Timeout/failure handler for CMD JoinBss
1698 * @adapter: pointer to struct adapter structure
1699 */
_rtw_join_timeout_handler(struct timer_list * t)1700 void _rtw_join_timeout_handler(struct timer_list *t)
1701 {
1702 struct adapter *adapter = from_timer(adapter, t,
1703 mlmepriv.assoc_timer);
1704 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1705
1706 DBG_871X("%s, fw_state =%x\n", __func__, get_fwstate(pmlmepriv));
1707
1708 if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
1709 return;
1710
1711 spin_lock_bh(&pmlmepriv->lock);
1712
1713 if (rtw_to_roam(adapter) > 0) { /* join timeout caused by roaming */
1714 while (1) {
1715 rtw_dec_to_roam(adapter);
1716 if (rtw_to_roam(adapter) != 0) { /* try another */
1717 int do_join_r;
1718 DBG_871X("%s try another roaming\n", __func__);
1719 do_join_r = rtw_do_join(adapter);
1720 if (_SUCCESS != do_join_r) {
1721 DBG_871X("%s roaming do_join return %d\n", __func__, do_join_r);
1722 continue;
1723 }
1724 break;
1725 } else {
1726 DBG_871X("%s We've try roaming but fail\n", __func__);
1727 rtw_indicate_disconnect(adapter);
1728 break;
1729 }
1730 }
1731
1732 } else {
1733 rtw_indicate_disconnect(adapter);
1734 free_scanqueue(pmlmepriv);/* */
1735
1736 /* indicate disconnect for the case that join_timeout and check_fwstate != FW_LINKED */
1737 rtw_cfg80211_indicate_disconnect(adapter);
1738
1739 }
1740
1741 spin_unlock_bh(&pmlmepriv->lock);
1742 }
1743
1744 /*
1745 * rtw_scan_timeout_handler - Timeout/Failure handler for CMD SiteSurvey
1746 * @adapter: pointer to struct adapter structure
1747 */
rtw_scan_timeout_handler(struct timer_list * t)1748 void rtw_scan_timeout_handler(struct timer_list *t)
1749 {
1750 struct adapter *adapter = from_timer(adapter, t,
1751 mlmepriv.scan_to_timer);
1752 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1753
1754 DBG_871X(FUNC_ADPT_FMT" fw_state =%x\n", FUNC_ADPT_ARG(adapter), get_fwstate(pmlmepriv));
1755
1756 spin_lock_bh(&pmlmepriv->lock);
1757
1758 _clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
1759
1760 spin_unlock_bh(&pmlmepriv->lock);
1761
1762 rtw_indicate_scan_done(adapter, true);
1763 }
1764
rtw_mlme_reset_auto_scan_int(struct adapter * adapter)1765 void rtw_mlme_reset_auto_scan_int(struct adapter *adapter)
1766 {
1767 struct mlme_priv *mlme = &adapter->mlmepriv;
1768 struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
1769 struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1770
1771 if (pmlmeinfo->VHT_enable) /* disable auto scan when connect to 11AC AP */
1772 mlme->auto_scan_int_ms = 0;
1773 else if (adapter->registrypriv.wifi_spec && is_client_associated_to_ap(adapter) == true)
1774 mlme->auto_scan_int_ms = 60*1000;
1775 else if (rtw_chk_roam_flags(adapter, RTW_ROAM_ACTIVE)) {
1776 if (check_fwstate(mlme, WIFI_STATION_STATE) && check_fwstate(mlme, _FW_LINKED))
1777 mlme->auto_scan_int_ms = mlme->roam_scan_int_ms;
1778 } else
1779 mlme->auto_scan_int_ms = 0; /* disabled */
1780 }
1781
rtw_auto_scan_handler(struct adapter * padapter)1782 static void rtw_auto_scan_handler(struct adapter *padapter)
1783 {
1784 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1785
1786 rtw_mlme_reset_auto_scan_int(padapter);
1787
1788 if (pmlmepriv->auto_scan_int_ms != 0
1789 && jiffies_to_msecs(jiffies - pmlmepriv->scan_start_time) > pmlmepriv->auto_scan_int_ms) {
1790
1791 if (!padapter->registrypriv.wifi_spec) {
1792 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING) == true) {
1793 DBG_871X(FUNC_ADPT_FMT" _FW_UNDER_SURVEY|_FW_UNDER_LINKING\n", FUNC_ADPT_ARG(padapter));
1794 goto exit;
1795 }
1796
1797 if (pmlmepriv->LinkDetectInfo.bBusyTraffic) {
1798 DBG_871X(FUNC_ADPT_FMT" exit BusyTraffic\n", FUNC_ADPT_ARG(padapter));
1799 goto exit;
1800 }
1801 }
1802
1803 DBG_871X(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter));
1804
1805 rtw_set_802_11_bssid_list_scan(padapter, NULL, 0);
1806 }
1807
1808 exit:
1809 return;
1810 }
1811
rtw_dynamic_check_timer_handler(struct adapter * adapter)1812 void rtw_dynamic_check_timer_handler(struct adapter *adapter)
1813 {
1814 if (!adapter)
1815 return;
1816
1817 if (!adapter->hw_init_completed)
1818 return;
1819
1820 if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
1821 return;
1822
1823 if (adapter->net_closed)
1824 return;
1825
1826 if (is_primary_adapter(adapter))
1827 DBG_871X("IsBtDisabled =%d, IsBtControlLps =%d\n", hal_btcoex_IsBtDisabled(adapter), hal_btcoex_IsBtControlLps(adapter));
1828
1829 if ((adapter_to_pwrctl(adapter)->bFwCurrentInPSMode)
1830 && !(hal_btcoex_IsBtControlLps(adapter))
1831 ) {
1832 u8 bEnterPS;
1833
1834 linked_status_chk(adapter);
1835
1836 bEnterPS = traffic_status_watchdog(adapter, 1);
1837 if (bEnterPS) {
1838 /* rtw_lps_ctrl_wk_cmd(adapter, LPS_CTRL_ENTER, 1); */
1839 rtw_hal_dm_watchdog_in_lps(adapter);
1840 } else {
1841 /* call rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 1) in traffic_status_watchdog() */
1842 }
1843
1844 } else {
1845 if (is_primary_adapter(adapter))
1846 rtw_dynamic_chk_wk_cmd(adapter);
1847 }
1848
1849 /* auto site survey */
1850 rtw_auto_scan_handler(adapter);
1851 }
1852
rtw_is_scan_deny(struct adapter * adapter)1853 inline bool rtw_is_scan_deny(struct adapter *adapter)
1854 {
1855 struct mlme_priv *mlmepriv = &adapter->mlmepriv;
1856 return (atomic_read(&mlmepriv->set_scan_deny) != 0) ? true : false;
1857 }
1858
rtw_clear_scan_deny(struct adapter * adapter)1859 inline void rtw_clear_scan_deny(struct adapter *adapter)
1860 {
1861 struct mlme_priv *mlmepriv = &adapter->mlmepriv;
1862 atomic_set(&mlmepriv->set_scan_deny, 0);
1863
1864 DBG_871X(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(adapter));
1865 }
1866
rtw_set_scan_deny(struct adapter * adapter,u32 ms)1867 void rtw_set_scan_deny(struct adapter *adapter, u32 ms)
1868 {
1869 struct mlme_priv *mlmepriv = &adapter->mlmepriv;
1870
1871 DBG_871X(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(adapter));
1872 atomic_set(&mlmepriv->set_scan_deny, 1);
1873 _set_timer(&mlmepriv->set_scan_deny_timer, ms);
1874 }
1875
1876 /*
1877 * Select a new roaming candidate from the original @param candidate and @param competitor
1878 * @return true: candidate is updated
1879 * @return false: candidate is not updated
1880 */
rtw_check_roaming_candidate(struct mlme_priv * mlme,struct wlan_network ** candidate,struct wlan_network * competitor)1881 static int rtw_check_roaming_candidate(struct mlme_priv *mlme
1882 , struct wlan_network **candidate, struct wlan_network *competitor)
1883 {
1884 int updated = false;
1885 struct adapter *adapter = container_of(mlme, struct adapter, mlmepriv);
1886
1887 if (is_same_ess(&competitor->network, &mlme->cur_network.network) == false)
1888 goto exit;
1889
1890 if (rtw_is_desired_network(adapter, competitor) == false)
1891 goto exit;
1892
1893 DBG_871X("roam candidate:%s %s("MAC_FMT", ch%3u) rssi:%d, age:%5d\n",
1894 (competitor == mlme->cur_network_scanned)?"*":" ",
1895 competitor->network.Ssid.Ssid,
1896 MAC_ARG(competitor->network.MacAddress),
1897 competitor->network.Configuration.DSConfig,
1898 (int)competitor->network.Rssi,
1899 jiffies_to_msecs(jiffies - competitor->last_scanned)
1900 );
1901
1902 /* got specific addr to roam */
1903 if (!is_zero_mac_addr(mlme->roam_tgt_addr)) {
1904 if (!memcmp(mlme->roam_tgt_addr, competitor->network.MacAddress, ETH_ALEN))
1905 goto update;
1906 else
1907 goto exit;
1908 }
1909 if (jiffies_to_msecs(jiffies - competitor->last_scanned) >= mlme->roam_scanr_exp_ms)
1910 goto exit;
1911
1912 if (competitor->network.Rssi - mlme->cur_network_scanned->network.Rssi < mlme->roam_rssi_diff_th)
1913 goto exit;
1914
1915 if (*candidate != NULL && (*candidate)->network.Rssi >= competitor->network.Rssi)
1916 goto exit;
1917
1918 update:
1919 *candidate = competitor;
1920 updated = true;
1921
1922 exit:
1923 return updated;
1924 }
1925
rtw_select_roaming_candidate(struct mlme_priv * mlme)1926 int rtw_select_roaming_candidate(struct mlme_priv *mlme)
1927 {
1928 int ret = _FAIL;
1929 struct list_head *phead;
1930 struct __queue *queue = &(mlme->scanned_queue);
1931 struct wlan_network *pnetwork = NULL;
1932 struct wlan_network *candidate = NULL;
1933
1934 if (!mlme->cur_network_scanned) {
1935 rtw_warn_on(1);
1936 return ret;
1937 }
1938
1939 spin_lock_bh(&(mlme->scanned_queue.lock));
1940 phead = get_list_head(queue);
1941
1942 mlme->pscanned = get_next(phead);
1943
1944 while (phead != mlme->pscanned) {
1945
1946 pnetwork = LIST_CONTAINOR(mlme->pscanned, struct wlan_network, list);
1947 if (!pnetwork) {
1948 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s return _FAIL:(pnetwork == NULL)\n", __func__));
1949 ret = _FAIL;
1950 goto exit;
1951 }
1952
1953 mlme->pscanned = get_next(mlme->pscanned);
1954
1955 DBG_871X("%s("MAC_FMT", ch%u) rssi:%d\n"
1956 , pnetwork->network.Ssid.Ssid
1957 , MAC_ARG(pnetwork->network.MacAddress)
1958 , pnetwork->network.Configuration.DSConfig
1959 , (int)pnetwork->network.Rssi);
1960
1961 rtw_check_roaming_candidate(mlme, &candidate, pnetwork);
1962
1963 }
1964
1965 if (!candidate) {
1966 DBG_871X("%s: return _FAIL(candidate == NULL)\n", __func__);
1967 ret = _FAIL;
1968 goto exit;
1969 } else {
1970 DBG_871X("%s: candidate: %s("MAC_FMT", ch:%u)\n", __func__,
1971 candidate->network.Ssid.Ssid, MAC_ARG(candidate->network.MacAddress),
1972 candidate->network.Configuration.DSConfig);
1973
1974 mlme->roam_network = candidate;
1975
1976 if (!memcmp(candidate->network.MacAddress, mlme->roam_tgt_addr, ETH_ALEN))
1977 eth_zero_addr(mlme->roam_tgt_addr);
1978 }
1979
1980 ret = _SUCCESS;
1981 exit:
1982 spin_unlock_bh(&(mlme->scanned_queue.lock));
1983
1984 return ret;
1985 }
1986
1987 /*
1988 * Select a new join candidate from the original @param candidate and @param competitor
1989 * @return true: candidate is updated
1990 * @return false: candidate is not updated
1991 */
rtw_check_join_candidate(struct mlme_priv * mlme,struct wlan_network ** candidate,struct wlan_network * competitor)1992 static int rtw_check_join_candidate(struct mlme_priv *mlme
1993 , struct wlan_network **candidate, struct wlan_network *competitor)
1994 {
1995 int updated = false;
1996 struct adapter *adapter = container_of(mlme, struct adapter, mlmepriv);
1997
1998 /* check bssid, if needed */
1999 if (mlme->assoc_by_bssid) {
2000 if (memcmp(competitor->network.MacAddress, mlme->assoc_bssid, ETH_ALEN))
2001 goto exit;
2002 }
2003
2004 /* check ssid, if needed */
2005 if (mlme->assoc_ssid.Ssid[0] && mlme->assoc_ssid.SsidLength) {
2006 if (competitor->network.Ssid.SsidLength != mlme->assoc_ssid.SsidLength
2007 || memcmp(competitor->network.Ssid.Ssid, mlme->assoc_ssid.Ssid, mlme->assoc_ssid.SsidLength)
2008 )
2009 goto exit;
2010 }
2011
2012 if (rtw_is_desired_network(adapter, competitor) == false)
2013 goto exit;
2014
2015 if (rtw_to_roam(adapter) > 0) {
2016 if (jiffies_to_msecs(jiffies - competitor->last_scanned) >= mlme->roam_scanr_exp_ms
2017 || is_same_ess(&competitor->network, &mlme->cur_network.network) == false
2018 )
2019 goto exit;
2020 }
2021
2022 if (*candidate == NULL || (*candidate)->network.Rssi < competitor->network.Rssi) {
2023 *candidate = competitor;
2024 updated = true;
2025 }
2026
2027 if (updated) {
2028 DBG_871X("[by_bssid:%u][assoc_ssid:%s]"
2029 "[to_roam:%u] "
2030 "new candidate: %s("MAC_FMT", ch%u) rssi:%d\n",
2031 mlme->assoc_by_bssid,
2032 mlme->assoc_ssid.Ssid,
2033 rtw_to_roam(adapter),
2034 (*candidate)->network.Ssid.Ssid,
2035 MAC_ARG((*candidate)->network.MacAddress),
2036 (*candidate)->network.Configuration.DSConfig,
2037 (int)(*candidate)->network.Rssi
2038 );
2039 }
2040
2041 exit:
2042 return updated;
2043 }
2044
2045 /*
2046 Calling context:
2047 The caller of the sub-routine will be in critical section...
2048 The caller must hold the following spinlock
2049 pmlmepriv->lock
2050 */
2051
rtw_select_and_join_from_scanned_queue(struct mlme_priv * pmlmepriv)2052 int rtw_select_and_join_from_scanned_queue(struct mlme_priv *pmlmepriv)
2053 {
2054 int ret;
2055 struct list_head *phead;
2056 struct adapter *adapter;
2057 struct __queue *queue = &(pmlmepriv->scanned_queue);
2058 struct wlan_network *pnetwork = NULL;
2059 struct wlan_network *candidate = NULL;
2060
2061 adapter = (struct adapter *)pmlmepriv->nic_hdl;
2062
2063 spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
2064
2065 if (pmlmepriv->roam_network) {
2066 candidate = pmlmepriv->roam_network;
2067 pmlmepriv->roam_network = NULL;
2068 goto candidate_exist;
2069 }
2070
2071 phead = get_list_head(queue);
2072 pmlmepriv->pscanned = get_next(phead);
2073
2074 while (phead != pmlmepriv->pscanned) {
2075
2076 pnetwork = LIST_CONTAINOR(pmlmepriv->pscanned, struct wlan_network, list);
2077 if (!pnetwork) {
2078 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s return _FAIL:(pnetwork == NULL)\n", __func__));
2079 ret = _FAIL;
2080 goto exit;
2081 }
2082
2083 pmlmepriv->pscanned = get_next(pmlmepriv->pscanned);
2084
2085 DBG_871X("%s("MAC_FMT", ch%u) rssi:%d\n"
2086 , pnetwork->network.Ssid.Ssid
2087 , MAC_ARG(pnetwork->network.MacAddress)
2088 , pnetwork->network.Configuration.DSConfig
2089 , (int)pnetwork->network.Rssi);
2090
2091 rtw_check_join_candidate(pmlmepriv, &candidate, pnetwork);
2092
2093 }
2094
2095 if (!candidate) {
2096 DBG_871X("%s: return _FAIL(candidate == NULL)\n", __func__);
2097 #ifdef CONFIG_WOWLAN
2098 _clr_fwstate_(pmlmepriv, _FW_LINKED|_FW_UNDER_LINKING);
2099 #endif
2100 ret = _FAIL;
2101 goto exit;
2102 } else {
2103 DBG_871X("%s: candidate: %s("MAC_FMT", ch:%u)\n", __func__,
2104 candidate->network.Ssid.Ssid, MAC_ARG(candidate->network.MacAddress),
2105 candidate->network.Configuration.DSConfig);
2106 goto candidate_exist;
2107 }
2108
2109 candidate_exist:
2110
2111 /* check for situation of _FW_LINKED */
2112 if (check_fwstate(pmlmepriv, _FW_LINKED) == true) {
2113 DBG_871X("%s: _FW_LINKED while ask_for_joinbss!!!\n", __func__);
2114
2115 rtw_disassoc_cmd(adapter, 0, true);
2116 rtw_indicate_disconnect(adapter);
2117 rtw_free_assoc_resources(adapter, 0);
2118 }
2119
2120 set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
2121 ret = rtw_joinbss_cmd(adapter, candidate);
2122
2123 exit:
2124 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
2125 return ret;
2126 }
2127
rtw_set_auth(struct adapter * adapter,struct security_priv * psecuritypriv)2128 sint rtw_set_auth(struct adapter *adapter, struct security_priv *psecuritypriv)
2129 {
2130 struct cmd_obj *pcmd;
2131 struct setauth_parm *psetauthparm;
2132 struct cmd_priv *pcmdpriv = &(adapter->cmdpriv);
2133 sint res = _SUCCESS;
2134
2135 pcmd = rtw_zmalloc(sizeof(struct cmd_obj));
2136 if (!pcmd) {
2137 res = _FAIL; /* try again */
2138 goto exit;
2139 }
2140
2141 psetauthparm = rtw_zmalloc(sizeof(struct setauth_parm));
2142 if (!psetauthparm) {
2143 kfree(pcmd);
2144 res = _FAIL;
2145 goto exit;
2146 }
2147
2148 psetauthparm->mode = (unsigned char)psecuritypriv->dot11AuthAlgrthm;
2149
2150 pcmd->cmdcode = _SetAuth_CMD_;
2151 pcmd->parmbuf = (unsigned char *)psetauthparm;
2152 pcmd->cmdsz = (sizeof(struct setauth_parm));
2153 pcmd->rsp = NULL;
2154 pcmd->rspsz = 0;
2155
2156 INIT_LIST_HEAD(&pcmd->list);
2157
2158 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("after enqueue set_auth_cmd, auth_mode =%x\n", psecuritypriv->dot11AuthAlgrthm));
2159
2160 res = rtw_enqueue_cmd(pcmdpriv, pcmd);
2161
2162 exit:
2163 return res;
2164 }
2165
rtw_set_key(struct adapter * adapter,struct security_priv * psecuritypriv,sint keyid,u8 set_tx,bool enqueue)2166 sint rtw_set_key(struct adapter *adapter, struct security_priv *psecuritypriv, sint keyid, u8 set_tx, bool enqueue)
2167 {
2168 u8 keylen;
2169 struct cmd_obj *pcmd;
2170 struct setkey_parm *psetkeyparm;
2171 struct cmd_priv *pcmdpriv = &(adapter->cmdpriv);
2172 sint res = _SUCCESS;
2173
2174 psetkeyparm = rtw_zmalloc(sizeof(struct setkey_parm));
2175 if (!psetkeyparm) {
2176 res = _FAIL;
2177 goto exit;
2178 }
2179
2180 if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) {
2181 psetkeyparm->algorithm = (unsigned char)psecuritypriv->dot118021XGrpPrivacy;
2182 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("\n rtw_set_key: psetkeyparm->algorithm =(unsigned char)psecuritypriv->dot118021XGrpPrivacy =%d\n", psetkeyparm->algorithm));
2183 } else {
2184 psetkeyparm->algorithm = (u8)psecuritypriv->dot11PrivacyAlgrthm;
2185 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("\n rtw_set_key: psetkeyparm->algorithm =(u8)psecuritypriv->dot11PrivacyAlgrthm =%d\n", psetkeyparm->algorithm));
2186
2187 }
2188 psetkeyparm->keyid = (u8)keyid;/* 0~3 */
2189 psetkeyparm->set_tx = set_tx;
2190 if (is_wep_enc(psetkeyparm->algorithm))
2191 adapter->securitypriv.key_mask |= BIT(psetkeyparm->keyid);
2192
2193 DBG_871X("==> rtw_set_key algorithm(%x), keyid(%x), key_mask(%x)\n", psetkeyparm->algorithm, psetkeyparm->keyid, adapter->securitypriv.key_mask);
2194 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("\n rtw_set_key: psetkeyparm->algorithm =%d psetkeyparm->keyid =(u8)keyid =%d\n", psetkeyparm->algorithm, keyid));
2195
2196 switch (psetkeyparm->algorithm) {
2197
2198 case _WEP40_:
2199 keylen = 5;
2200 memcpy(&(psetkeyparm->key[0]), &(psecuritypriv->dot11DefKey[keyid].skey[0]), keylen);
2201 break;
2202 case _WEP104_:
2203 keylen = 13;
2204 memcpy(&(psetkeyparm->key[0]), &(psecuritypriv->dot11DefKey[keyid].skey[0]), keylen);
2205 break;
2206 case _TKIP_:
2207 keylen = 16;
2208 memcpy(&psetkeyparm->key, &psecuritypriv->dot118021XGrpKey[keyid], keylen);
2209 psetkeyparm->grpkey = 1;
2210 break;
2211 case _AES_:
2212 keylen = 16;
2213 memcpy(&psetkeyparm->key, &psecuritypriv->dot118021XGrpKey[keyid], keylen);
2214 psetkeyparm->grpkey = 1;
2215 break;
2216 default:
2217 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("\n rtw_set_key:psecuritypriv->dot11PrivacyAlgrthm = %x (must be 1 or 2 or 4 or 5)\n", psecuritypriv->dot11PrivacyAlgrthm));
2218 res = _FAIL;
2219 kfree(psetkeyparm);
2220 goto exit;
2221 }
2222
2223 if (enqueue) {
2224 pcmd = rtw_zmalloc(sizeof(struct cmd_obj));
2225 if (!pcmd) {
2226 kfree(psetkeyparm);
2227 res = _FAIL; /* try again */
2228 goto exit;
2229 }
2230
2231 pcmd->cmdcode = _SetKey_CMD_;
2232 pcmd->parmbuf = (u8 *)psetkeyparm;
2233 pcmd->cmdsz = (sizeof(struct setkey_parm));
2234 pcmd->rsp = NULL;
2235 pcmd->rspsz = 0;
2236
2237 INIT_LIST_HEAD(&pcmd->list);
2238
2239 res = rtw_enqueue_cmd(pcmdpriv, pcmd);
2240 } else {
2241 setkey_hdl(adapter, (u8 *)psetkeyparm);
2242 kfree(psetkeyparm);
2243 }
2244 exit:
2245 return res;
2246 }
2247
2248 /* adjust IEs for rtw_joinbss_cmd in WMM */
rtw_restruct_wmm_ie(struct adapter * adapter,u8 * in_ie,u8 * out_ie,uint in_len,uint initial_out_len)2249 int rtw_restruct_wmm_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie, uint in_len, uint initial_out_len)
2250 {
2251 unsigned int ielength = 0;
2252 unsigned int i, j;
2253
2254 i = 12; /* after the fixed IE */
2255 while (i < in_len) {
2256 ielength = initial_out_len;
2257
2258 if (in_ie[i] == 0xDD && in_ie[i+2] == 0x00 && in_ie[i+3] == 0x50 && in_ie[i+4] == 0xF2 && in_ie[i+5] == 0x02 && i+5 < in_len) { /* WMM element ID and OUI */
2259 for (j = i; j < i + 9; j++) {
2260 out_ie[ielength] = in_ie[j];
2261 ielength++;
2262 }
2263 out_ie[initial_out_len + 1] = 0x07;
2264 out_ie[initial_out_len + 6] = 0x00;
2265 out_ie[initial_out_len + 8] = 0x00;
2266
2267 break;
2268 }
2269
2270 i += (in_ie[i+1]+2); /* to the next IE element */
2271 }
2272
2273 return ielength;
2274
2275 }
2276
2277 /* */
2278 /* Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */
2279 /* Added by Annie, 2006-05-07. */
2280 /* */
2281 /* Search by BSSID, */
2282 /* Return Value: */
2283 /* -1 :if there is no pre-auth key in the table */
2284 /* >= 0 :if there is pre-auth key, and return the entry id */
2285 /* */
2286 /* */
2287
SecIsInPMKIDList(struct adapter * Adapter,u8 * bssid)2288 static int SecIsInPMKIDList(struct adapter *Adapter, u8 *bssid)
2289 {
2290 struct security_priv *psecuritypriv = &Adapter->securitypriv;
2291 int i = 0;
2292
2293 do {
2294 if ((psecuritypriv->PMKIDList[i].bUsed) &&
2295 (!memcmp(psecuritypriv->PMKIDList[i].Bssid, bssid, ETH_ALEN))) {
2296 break;
2297 } else {
2298 i++;
2299 /* continue; */
2300 }
2301
2302 } while (i < NUM_PMKID_CACHE);
2303
2304 if (i == NUM_PMKID_CACHE) {
2305 i = -1;/* Could not find. */
2306 } else {
2307 /* There is one Pre-Authentication Key for the specific BSSID. */
2308 }
2309
2310 return i;
2311
2312 }
2313
2314 /* */
2315 /* Check the RSN IE length */
2316 /* If the RSN IE length <= 20, the RSN IE didn't include the PMKID information */
2317 /* 0-11th element in the array are the fixed IE */
2318 /* 12th element in the array is the IE */
2319 /* 13th element in the array is the IE length */
2320 /* */
2321
rtw_append_pmkid(struct adapter * Adapter,int iEntry,u8 * ie,uint ie_len)2322 static int rtw_append_pmkid(struct adapter *Adapter, int iEntry, u8 *ie, uint ie_len)
2323 {
2324 struct security_priv *psecuritypriv = &Adapter->securitypriv;
2325
2326 if (ie[13] <= 20) {
2327 /* The RSN IE didn't include the PMK ID, append the PMK information */
2328 ie[ie_len] = 1;
2329 ie_len++;
2330 ie[ie_len] = 0; /* PMKID count = 0x0100 */
2331 ie_len++;
2332 memcpy(&ie[ie_len], &psecuritypriv->PMKIDList[iEntry].PMKID, 16);
2333
2334 ie_len += 16;
2335 ie[13] += 18;/* PMKID length = 2+16 */
2336
2337 }
2338 return ie_len;
2339 }
2340
rtw_restruct_sec_ie(struct adapter * adapter,u8 * in_ie,u8 * out_ie,uint in_len)2341 sint rtw_restruct_sec_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie, uint in_len)
2342 {
2343 u8 authmode = 0x0;
2344 uint ielength;
2345 int iEntry;
2346
2347 struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
2348 struct security_priv *psecuritypriv = &adapter->securitypriv;
2349 uint ndisauthmode = psecuritypriv->ndisauthtype;
2350
2351 RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_,
2352 ("+rtw_restruct_sec_ie: ndisauthmode =%d\n", ndisauthmode));
2353
2354 /* copy fixed ie only */
2355 memcpy(out_ie, in_ie, 12);
2356 ielength = 12;
2357 if ((ndisauthmode == Ndis802_11AuthModeWPA) || (ndisauthmode == Ndis802_11AuthModeWPAPSK))
2358 authmode = _WPA_IE_ID_;
2359 if ((ndisauthmode == Ndis802_11AuthModeWPA2) || (ndisauthmode == Ndis802_11AuthModeWPA2PSK))
2360 authmode = _WPA2_IE_ID_;
2361
2362 if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS)) {
2363 memcpy(out_ie+ielength, psecuritypriv->wps_ie, psecuritypriv->wps_ie_len);
2364
2365 ielength += psecuritypriv->wps_ie_len;
2366 } else if ((authmode == _WPA_IE_ID_) || (authmode == _WPA2_IE_ID_)) {
2367 /* copy RSN or SSN */
2368 memcpy(&out_ie[ielength], &psecuritypriv->supplicant_ie[0], psecuritypriv->supplicant_ie[1]+2);
2369 /* debug for CONFIG_IEEE80211W
2370 {
2371 int jj;
2372 printk("supplicant_ie_length =%d &&&&&&&&&&&&&&&&&&&\n", psecuritypriv->supplicant_ie[1]+2);
2373 for (jj = 0; jj < psecuritypriv->supplicant_ie[1]+2; jj++)
2374 printk(" %02x ", psecuritypriv->supplicant_ie[jj]);
2375 printk("\n");
2376 }*/
2377 ielength += psecuritypriv->supplicant_ie[1]+2;
2378 rtw_report_sec_ie(adapter, authmode, psecuritypriv->supplicant_ie);
2379 }
2380
2381 iEntry = SecIsInPMKIDList(adapter, pmlmepriv->assoc_bssid);
2382 if (iEntry < 0) {
2383 return ielength;
2384 } else {
2385 if (authmode == _WPA2_IE_ID_)
2386 ielength = rtw_append_pmkid(adapter, iEntry, out_ie, ielength);
2387 }
2388 return ielength;
2389 }
2390
rtw_init_registrypriv_dev_network(struct adapter * adapter)2391 void rtw_init_registrypriv_dev_network(struct adapter *adapter)
2392 {
2393 struct registry_priv *pregistrypriv = &adapter->registrypriv;
2394 struct eeprom_priv *peepriv = &adapter->eeprompriv;
2395 struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
2396 u8 *myhwaddr = myid(peepriv);
2397
2398 memcpy(pdev_network->MacAddress, myhwaddr, ETH_ALEN);
2399
2400 memcpy(&pdev_network->Ssid, &pregistrypriv->ssid, sizeof(struct ndis_802_11_ssid));
2401
2402 pdev_network->Configuration.Length = sizeof(struct ndis_802_11_conf);
2403 pdev_network->Configuration.BeaconPeriod = 100;
2404 pdev_network->Configuration.FHConfig.Length = 0;
2405 pdev_network->Configuration.FHConfig.HopPattern = 0;
2406 pdev_network->Configuration.FHConfig.HopSet = 0;
2407 pdev_network->Configuration.FHConfig.DwellTime = 0;
2408 }
2409
rtw_update_registrypriv_dev_network(struct adapter * adapter)2410 void rtw_update_registrypriv_dev_network(struct adapter *adapter)
2411 {
2412 int sz = 0;
2413 struct registry_priv *pregistrypriv = &adapter->registrypriv;
2414 struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
2415 struct security_priv *psecuritypriv = &adapter->securitypriv;
2416 struct wlan_network *cur_network = &adapter->mlmepriv.cur_network;
2417 /* struct xmit_priv *pxmitpriv = &adapter->xmitpriv; */
2418
2419 pdev_network->Privacy = (psecuritypriv->dot11PrivacyAlgrthm > 0 ? 1 : 0) ; /* adhoc no 802.1x */
2420
2421 pdev_network->Rssi = 0;
2422
2423 switch (pregistrypriv->wireless_mode) {
2424 case WIRELESS_11B:
2425 pdev_network->NetworkTypeInUse = (Ndis802_11DS);
2426 break;
2427 case WIRELESS_11G:
2428 case WIRELESS_11BG:
2429 case WIRELESS_11_24N:
2430 case WIRELESS_11G_24N:
2431 case WIRELESS_11BG_24N:
2432 pdev_network->NetworkTypeInUse = (Ndis802_11OFDM24);
2433 break;
2434 case WIRELESS_11A:
2435 case WIRELESS_11A_5N:
2436 pdev_network->NetworkTypeInUse = (Ndis802_11OFDM5);
2437 break;
2438 case WIRELESS_11ABGN:
2439 if (pregistrypriv->channel > 14)
2440 pdev_network->NetworkTypeInUse = (Ndis802_11OFDM5);
2441 else
2442 pdev_network->NetworkTypeInUse = (Ndis802_11OFDM24);
2443 break;
2444 default:
2445 /* TODO */
2446 break;
2447 }
2448
2449 pdev_network->Configuration.DSConfig = (pregistrypriv->channel);
2450 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("pregistrypriv->channel =%d, pdev_network->Configuration.DSConfig = 0x%x\n", pregistrypriv->channel, pdev_network->Configuration.DSConfig));
2451
2452 if (cur_network->network.InfrastructureMode == Ndis802_11IBSS)
2453 pdev_network->Configuration.ATIMWindow = (0);
2454
2455 pdev_network->InfrastructureMode = (cur_network->network.InfrastructureMode);
2456
2457 /* 1. Supported rates */
2458 /* 2. IE */
2459
2460 /* rtw_set_supported_rate(pdev_network->SupportedRates, pregistrypriv->wireless_mode) ; will be called in rtw_generate_ie */
2461 sz = rtw_generate_ie(pregistrypriv);
2462
2463 pdev_network->IELength = sz;
2464
2465 pdev_network->Length = get_wlan_bssid_ex_sz((struct wlan_bssid_ex *)pdev_network);
2466
2467 /* notes: translate IELength & Length after assign the Length to cmdsz in createbss_cmd(); */
2468 /* pdev_network->IELength = cpu_to_le32(sz); */
2469 }
2470
rtw_get_encrypt_decrypt_from_registrypriv(struct adapter * adapter)2471 void rtw_get_encrypt_decrypt_from_registrypriv(struct adapter *adapter)
2472 {
2473 }
2474
2475 /* the function is at passive_level */
rtw_joinbss_reset(struct adapter * padapter)2476 void rtw_joinbss_reset(struct adapter *padapter)
2477 {
2478 u8 threshold;
2479 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2480
2481 struct ht_priv *phtpriv = &pmlmepriv->htpriv;
2482
2483 /* todo: if you want to do something io/reg/hw setting before join_bss, please add code here */
2484
2485 pmlmepriv->num_FortyMHzIntolerant = 0;
2486
2487 pmlmepriv->num_sta_no_ht = 0;
2488
2489 phtpriv->ampdu_enable = false;/* reset to disabled */
2490
2491 /* TH = 1 => means that invalidate usb rx aggregation */
2492 /* TH = 0 => means that validate usb rx aggregation, use init value. */
2493 if (phtpriv->ht_option) {
2494 if (padapter->registrypriv.wifi_spec == 1)
2495 threshold = 1;
2496 else
2497 threshold = 0;
2498 rtw_hal_set_hwreg(padapter, HW_VAR_RXDMA_AGG_PG_TH, (u8 *)(&threshold));
2499 } else {
2500 threshold = 1;
2501 rtw_hal_set_hwreg(padapter, HW_VAR_RXDMA_AGG_PG_TH, (u8 *)(&threshold));
2502 }
2503 }
2504
rtw_ht_use_default_setting(struct adapter * padapter)2505 void rtw_ht_use_default_setting(struct adapter *padapter)
2506 {
2507 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2508 struct ht_priv *phtpriv = &pmlmepriv->htpriv;
2509 struct registry_priv *pregistrypriv = &padapter->registrypriv;
2510 bool bHwLDPCSupport = false, bHwSTBCSupport = false;
2511 bool bHwSupportBeamformer = false, bHwSupportBeamformee = false;
2512
2513 if (pregistrypriv->wifi_spec)
2514 phtpriv->bss_coexist = 1;
2515 else
2516 phtpriv->bss_coexist = 0;
2517
2518 phtpriv->sgi_40m = TEST_FLAG(pregistrypriv->short_gi, BIT1) ? true : false;
2519 phtpriv->sgi_20m = TEST_FLAG(pregistrypriv->short_gi, BIT0) ? true : false;
2520
2521 /* LDPC support */
2522 rtw_hal_get_def_var(padapter, HAL_DEF_RX_LDPC, (u8 *)&bHwLDPCSupport);
2523 CLEAR_FLAGS(phtpriv->ldpc_cap);
2524 if (bHwLDPCSupport) {
2525 if (TEST_FLAG(pregistrypriv->ldpc_cap, BIT4))
2526 SET_FLAG(phtpriv->ldpc_cap, LDPC_HT_ENABLE_RX);
2527 }
2528 rtw_hal_get_def_var(padapter, HAL_DEF_TX_LDPC, (u8 *)&bHwLDPCSupport);
2529 if (bHwLDPCSupport) {
2530 if (TEST_FLAG(pregistrypriv->ldpc_cap, BIT5))
2531 SET_FLAG(phtpriv->ldpc_cap, LDPC_HT_ENABLE_TX);
2532 }
2533 if (phtpriv->ldpc_cap)
2534 DBG_871X("[HT] Support LDPC = 0x%02X\n", phtpriv->ldpc_cap);
2535
2536 /* STBC */
2537 rtw_hal_get_def_var(padapter, HAL_DEF_TX_STBC, (u8 *)&bHwSTBCSupport);
2538 CLEAR_FLAGS(phtpriv->stbc_cap);
2539 if (bHwSTBCSupport) {
2540 if (TEST_FLAG(pregistrypriv->stbc_cap, BIT5))
2541 SET_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_TX);
2542 }
2543 rtw_hal_get_def_var(padapter, HAL_DEF_RX_STBC, (u8 *)&bHwSTBCSupport);
2544 if (bHwSTBCSupport) {
2545 if (TEST_FLAG(pregistrypriv->stbc_cap, BIT4))
2546 SET_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_RX);
2547 }
2548 if (phtpriv->stbc_cap)
2549 DBG_871X("[HT] Support STBC = 0x%02X\n", phtpriv->stbc_cap);
2550
2551 /* Beamforming setting */
2552 rtw_hal_get_def_var(padapter, HAL_DEF_EXPLICIT_BEAMFORMER, (u8 *)&bHwSupportBeamformer);
2553 rtw_hal_get_def_var(padapter, HAL_DEF_EXPLICIT_BEAMFORMEE, (u8 *)&bHwSupportBeamformee);
2554 CLEAR_FLAGS(phtpriv->beamform_cap);
2555 if (TEST_FLAG(pregistrypriv->beamform_cap, BIT4) && bHwSupportBeamformer) {
2556 SET_FLAG(phtpriv->beamform_cap, BEAMFORMING_HT_BEAMFORMER_ENABLE);
2557 DBG_871X("[HT] Support Beamformer\n");
2558 }
2559 if (TEST_FLAG(pregistrypriv->beamform_cap, BIT5) && bHwSupportBeamformee) {
2560 SET_FLAG(phtpriv->beamform_cap, BEAMFORMING_HT_BEAMFORMEE_ENABLE);
2561 DBG_871X("[HT] Support Beamformee\n");
2562 }
2563 }
2564
rtw_build_wmm_ie_ht(struct adapter * padapter,u8 * out_ie,uint * pout_len)2565 void rtw_build_wmm_ie_ht(struct adapter *padapter, u8 *out_ie, uint *pout_len)
2566 {
2567 unsigned char WMM_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01, 0x00};
2568 int out_len;
2569 u8 *pframe;
2570
2571 if (padapter->mlmepriv.qospriv.qos_option == 0) {
2572 out_len = *pout_len;
2573 pframe = rtw_set_ie(out_ie+out_len, _VENDOR_SPECIFIC_IE_,
2574 _WMM_IE_Length_, WMM_IE, pout_len);
2575
2576 padapter->mlmepriv.qospriv.qos_option = 1;
2577 }
2578 }
2579
2580 /* the function is >= passive_level */
rtw_restructure_ht_ie(struct adapter * padapter,u8 * in_ie,u8 * out_ie,uint in_len,uint * pout_len,u8 channel)2581 unsigned int rtw_restructure_ht_ie(struct adapter *padapter, u8 *in_ie, u8 *out_ie, uint in_len, uint *pout_len, u8 channel)
2582 {
2583 u32 ielen, out_len;
2584 enum HT_CAP_AMPDU_FACTOR max_rx_ampdu_factor;
2585 unsigned char *p, *pframe;
2586 struct rtw_ieee80211_ht_cap ht_capie;
2587 u8 cbw40_enable = 0, stbc_rx_enable = 0, rf_type = 0, operation_bw = 0;
2588 struct registry_priv *pregistrypriv = &padapter->registrypriv;
2589 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2590 struct ht_priv *phtpriv = &pmlmepriv->htpriv;
2591 struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
2592
2593 phtpriv->ht_option = false;
2594
2595 out_len = *pout_len;
2596
2597 memset(&ht_capie, 0, sizeof(struct rtw_ieee80211_ht_cap));
2598
2599 ht_capie.cap_info = cpu_to_le16(IEEE80211_HT_CAP_DSSSCCK40);
2600
2601 if (phtpriv->sgi_20m)
2602 ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_SGI_20);
2603
2604 /* Get HT BW */
2605 if (!in_ie) {
2606 /* TDLS: TODO 20/40 issue */
2607 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
2608 operation_bw = padapter->mlmeextpriv.cur_bwmode;
2609 if (operation_bw > CHANNEL_WIDTH_40)
2610 operation_bw = CHANNEL_WIDTH_40;
2611 } else
2612 /* TDLS: TODO 40? */
2613 operation_bw = CHANNEL_WIDTH_40;
2614 } else {
2615 p = rtw_get_ie(in_ie, _HT_ADD_INFO_IE_, &ielen, in_len);
2616 if (p && (ielen == sizeof(struct ieee80211_ht_addt_info))) {
2617 struct HT_info_element *pht_info = (struct HT_info_element *)(p+2);
2618 if (pht_info->infos[0] & BIT(2)) {
2619 switch (pht_info->infos[0] & 0x3) {
2620 case 1:
2621 case 3:
2622 operation_bw = CHANNEL_WIDTH_40;
2623 break;
2624 default:
2625 operation_bw = CHANNEL_WIDTH_20;
2626 break;
2627 }
2628 } else {
2629 operation_bw = CHANNEL_WIDTH_20;
2630 }
2631 }
2632 }
2633
2634 /* to disable 40M Hz support while gd_bw_40MHz_en = 0 */
2635 if (channel > 14) {
2636 if ((pregistrypriv->bw_mode & 0xf0) > 0)
2637 cbw40_enable = 1;
2638 } else {
2639 if ((pregistrypriv->bw_mode & 0x0f) > 0)
2640 cbw40_enable = 1;
2641 }
2642
2643 if ((cbw40_enable == 1) && (operation_bw == CHANNEL_WIDTH_40)) {
2644 ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_SUP_WIDTH);
2645 if (phtpriv->sgi_40m)
2646 ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_SGI_40);
2647 }
2648
2649 if (TEST_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_TX))
2650 ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_TX_STBC);
2651
2652 /* todo: disable SM power save mode */
2653 ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_SM_PS);
2654
2655 if (TEST_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_RX)) {
2656 if ((channel <= 14 && pregistrypriv->rx_stbc == 0x1) || /* enable for 2.4GHz */
2657 (pregistrypriv->wifi_spec == 1)) {
2658 stbc_rx_enable = 1;
2659 DBG_871X("declare supporting RX STBC\n");
2660 }
2661 }
2662
2663 /* fill default supported_mcs_set */
2664 memcpy(ht_capie.supp_mcs_set, pmlmeext->default_supported_mcs_set, 16);
2665
2666 /* update default supported_mcs_set */
2667 rtw_hal_get_hwreg(padapter, HW_VAR_RF_TYPE, (u8 *)(&rf_type));
2668
2669 switch (rf_type) {
2670 case RF_1T1R:
2671 if (stbc_rx_enable)
2672 ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_RX_STBC_1R);/* RX STBC One spatial stream */
2673
2674 set_mcs_rate_by_mask(ht_capie.supp_mcs_set, MCS_RATE_1R);
2675 break;
2676
2677 case RF_2T2R:
2678 case RF_1T2R:
2679 default:
2680 if (stbc_rx_enable)
2681 ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_RX_STBC_2R);/* RX STBC two spatial stream */
2682
2683 #ifdef CONFIG_DISABLE_MCS13TO15
2684 if (((cbw40_enable == 1) && (operation_bw == CHANNEL_WIDTH_40)) && (pregistrypriv->wifi_spec != 1))
2685 set_mcs_rate_by_mask(ht_capie.supp_mcs_set, MCS_RATE_2R_13TO15_OFF);
2686 else
2687 set_mcs_rate_by_mask(ht_capie.supp_mcs_set, MCS_RATE_2R);
2688 #else /* CONFIG_DISABLE_MCS13TO15 */
2689 set_mcs_rate_by_mask(ht_capie.supp_mcs_set, MCS_RATE_2R);
2690 #endif /* CONFIG_DISABLE_MCS13TO15 */
2691 break;
2692 }
2693
2694 {
2695 u32 rx_packet_offset, max_recvbuf_sz;
2696 rtw_hal_get_def_var(padapter, HAL_DEF_RX_PACKET_OFFSET, &rx_packet_offset);
2697 rtw_hal_get_def_var(padapter, HAL_DEF_MAX_RECVBUF_SZ, &max_recvbuf_sz);
2698 }
2699
2700 if (padapter->driver_rx_ampdu_factor != 0xFF)
2701 max_rx_ampdu_factor =
2702 (enum HT_CAP_AMPDU_FACTOR)padapter->driver_rx_ampdu_factor;
2703 else
2704 rtw_hal_get_def_var(padapter, HW_VAR_MAX_RX_AMPDU_FACTOR,
2705 &max_rx_ampdu_factor);
2706
2707 /* rtw_hal_get_def_var(padapter, HW_VAR_MAX_RX_AMPDU_FACTOR, &max_rx_ampdu_factor); */
2708 ht_capie.ampdu_params_info = (max_rx_ampdu_factor&0x03);
2709
2710 if (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)
2711 ht_capie.ampdu_params_info |= (IEEE80211_HT_CAP_AMPDU_DENSITY&(0x07<<2));
2712 else
2713 ht_capie.ampdu_params_info |= (IEEE80211_HT_CAP_AMPDU_DENSITY&0x00);
2714
2715 pframe = rtw_set_ie(out_ie+out_len, _HT_CAPABILITY_IE_,
2716 sizeof(struct rtw_ieee80211_ht_cap), (unsigned char *)&ht_capie, pout_len);
2717
2718 phtpriv->ht_option = true;
2719
2720 if (in_ie) {
2721 p = rtw_get_ie(in_ie, _HT_ADD_INFO_IE_, &ielen, in_len);
2722 if (p && (ielen == sizeof(struct ieee80211_ht_addt_info))) {
2723 out_len = *pout_len;
2724 pframe = rtw_set_ie(out_ie+out_len, _HT_ADD_INFO_IE_, ielen, p+2, pout_len);
2725 }
2726 }
2727
2728 return phtpriv->ht_option;
2729
2730 }
2731
2732 /* the function is > passive_level (in critical_section) */
rtw_update_ht_cap(struct adapter * padapter,u8 * pie,uint ie_len,u8 channel)2733 void rtw_update_ht_cap(struct adapter *padapter, u8 *pie, uint ie_len, u8 channel)
2734 {
2735 u8 *p, max_ampdu_sz;
2736 int len;
2737 /* struct sta_info *bmc_sta, *psta; */
2738 struct rtw_ieee80211_ht_cap *pht_capie;
2739 struct ieee80211_ht_addt_info *pht_addtinfo;
2740 /* struct recv_reorder_ctrl *preorder_ctrl; */
2741 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2742 struct ht_priv *phtpriv = &pmlmepriv->htpriv;
2743 /* struct recv_priv *precvpriv = &padapter->recvpriv; */
2744 struct registry_priv *pregistrypriv = &padapter->registrypriv;
2745 /* struct wlan_network *pcur_network = &(pmlmepriv->cur_network);; */
2746 struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
2747 struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
2748 u8 cbw40_enable = 0;
2749
2750 if (!phtpriv->ht_option)
2751 return;
2752
2753 if ((!pmlmeinfo->HT_info_enable) || (!pmlmeinfo->HT_caps_enable))
2754 return;
2755
2756 DBG_871X("+rtw_update_ht_cap()\n");
2757
2758 /* maybe needs check if ap supports rx ampdu. */
2759 if (!(phtpriv->ampdu_enable) && pregistrypriv->ampdu_enable == 1) {
2760 phtpriv->ampdu_enable = true;
2761 }
2762
2763 /* check Max Rx A-MPDU Size */
2764 len = 0;
2765 p = rtw_get_ie(pie+sizeof(struct ndis_802_11_fix_ie), _HT_CAPABILITY_IE_, &len, ie_len-sizeof(struct ndis_802_11_fix_ie));
2766 if (p && len > 0) {
2767 pht_capie = (struct rtw_ieee80211_ht_cap *)(p+2);
2768 max_ampdu_sz = (pht_capie->ampdu_params_info & IEEE80211_HT_CAP_AMPDU_FACTOR);
2769 max_ampdu_sz = 1 << (max_ampdu_sz+3); /* max_ampdu_sz (kbytes); */
2770
2771 /* DBG_871X("rtw_update_ht_cap(): max_ampdu_sz =%d\n", max_ampdu_sz); */
2772 phtpriv->rx_ampdu_maxlen = max_ampdu_sz;
2773
2774 }
2775
2776 len = 0;
2777 p = rtw_get_ie(pie+sizeof(struct ndis_802_11_fix_ie), _HT_ADD_INFO_IE_, &len, ie_len-sizeof(struct ndis_802_11_fix_ie));
2778 if (p && len > 0) {
2779 pht_addtinfo = (struct ieee80211_ht_addt_info *)(p+2);
2780 /* todo: */
2781 }
2782
2783 if (channel > 14) {
2784 if ((pregistrypriv->bw_mode & 0xf0) > 0)
2785 cbw40_enable = 1;
2786 } else {
2787 if ((pregistrypriv->bw_mode & 0x0f) > 0)
2788 cbw40_enable = 1;
2789 }
2790
2791 /* update cur_bwmode & cur_ch_offset */
2792 if ((cbw40_enable) &&
2793 (le16_to_cpu(pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info) &
2794 BIT(1)) && (pmlmeinfo->HT_info.infos[0] & BIT(2))) {
2795 int i;
2796 u8 rf_type;
2797
2798 rtw_hal_get_hwreg(padapter, HW_VAR_RF_TYPE, (u8 *)(&rf_type));
2799
2800 /* update the MCS set */
2801 for (i = 0; i < 16; i++)
2802 pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate[i] &= pmlmeext->default_supported_mcs_set[i];
2803
2804 /* update the MCS rates */
2805 switch (rf_type) {
2806 case RF_1T1R:
2807 case RF_1T2R:
2808 set_mcs_rate_by_mask(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_RATE_1R);
2809 break;
2810 case RF_2T2R:
2811 default:
2812 #ifdef CONFIG_DISABLE_MCS13TO15
2813 if (pmlmeext->cur_bwmode == CHANNEL_WIDTH_40 && pregistrypriv->wifi_spec != 1)
2814 set_mcs_rate_by_mask(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_RATE_2R_13TO15_OFF);
2815 else
2816 set_mcs_rate_by_mask(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_RATE_2R);
2817 #else /* CONFIG_DISABLE_MCS13TO15 */
2818 set_mcs_rate_by_mask(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_RATE_2R);
2819 #endif /* CONFIG_DISABLE_MCS13TO15 */
2820 }
2821
2822 /* switch to the 40M Hz mode according to the AP */
2823 /* pmlmeext->cur_bwmode = CHANNEL_WIDTH_40; */
2824 switch ((pmlmeinfo->HT_info.infos[0] & 0x3)) {
2825 case EXTCHNL_OFFSET_UPPER:
2826 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER;
2827 break;
2828
2829 case EXTCHNL_OFFSET_LOWER:
2830 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER;
2831 break;
2832
2833 default:
2834 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
2835 break;
2836 }
2837 }
2838
2839 /* */
2840 /* Config SM Power Save setting */
2841 /* */
2842 pmlmeinfo->SM_PS =
2843 (le16_to_cpu(pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info) &
2844 0x0C) >> 2;
2845 if (pmlmeinfo->SM_PS == WLAN_HT_CAP_SM_PS_STATIC)
2846 DBG_871X("%s(): WLAN_HT_CAP_SM_PS_STATIC\n", __func__);
2847
2848 /* */
2849 /* Config current HT Protection mode. */
2850 /* */
2851 pmlmeinfo->HT_protection = pmlmeinfo->HT_info.infos[1] & 0x3;
2852 }
2853
rtw_issue_addbareq_cmd(struct adapter * padapter,struct xmit_frame * pxmitframe)2854 void rtw_issue_addbareq_cmd(struct adapter *padapter, struct xmit_frame *pxmitframe)
2855 {
2856 u8 issued;
2857 int priority;
2858 struct sta_info *psta = NULL;
2859 struct ht_priv *phtpriv;
2860 struct pkt_attrib *pattrib = &pxmitframe->attrib;
2861 s32 bmcst = IS_MCAST(pattrib->ra);
2862
2863 /* if (bmcst || (padapter->mlmepriv.LinkDetectInfo.bTxBusyTraffic == false)) */
2864 if (bmcst || (padapter->mlmepriv.LinkDetectInfo.NumTxOkInPeriod < 100))
2865 return;
2866
2867 priority = pattrib->priority;
2868
2869 psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
2870 if (pattrib->psta != psta) {
2871 DBG_871X("%s, pattrib->psta(%p) != psta(%p)\n", __func__, pattrib->psta, psta);
2872 return;
2873 }
2874
2875 if (!psta) {
2876 DBG_871X("%s, psta ==NUL\n", __func__);
2877 return;
2878 }
2879
2880 if (!(psta->state & _FW_LINKED)) {
2881 DBG_871X("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, psta->state);
2882 return;
2883 }
2884
2885 phtpriv = &psta->htpriv;
2886
2887 if (phtpriv->ht_option && phtpriv->ampdu_enable) {
2888 issued = (phtpriv->agg_enable_bitmap>>priority)&0x1;
2889 issued |= (phtpriv->candidate_tid_bitmap>>priority)&0x1;
2890
2891 if (0 == issued) {
2892 DBG_871X("rtw_issue_addbareq_cmd, p =%d\n", priority);
2893 psta->htpriv.candidate_tid_bitmap |= BIT((u8)priority);
2894 rtw_addbareq_cmd(padapter, (u8) priority, pattrib->ra);
2895 }
2896 }
2897
2898 }
2899
rtw_append_exented_cap(struct adapter * padapter,u8 * out_ie,uint * pout_len)2900 void rtw_append_exented_cap(struct adapter *padapter, u8 *out_ie, uint *pout_len)
2901 {
2902 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2903 struct ht_priv *phtpriv = &pmlmepriv->htpriv;
2904 u8 cap_content[8] = {0};
2905
2906 if (phtpriv->bss_coexist)
2907 SET_EXT_CAPABILITY_ELE_BSS_COEXIST(cap_content, 1);
2908
2909 rtw_set_ie(out_ie + *pout_len, EID_EXTCapability, 8, cap_content, pout_len);
2910 }
2911
rtw_set_to_roam(struct adapter * adapter,u8 to_roam)2912 inline void rtw_set_to_roam(struct adapter *adapter, u8 to_roam)
2913 {
2914 if (to_roam == 0)
2915 adapter->mlmepriv.to_join = false;
2916 adapter->mlmepriv.to_roam = to_roam;
2917 }
2918
rtw_dec_to_roam(struct adapter * adapter)2919 inline u8 rtw_dec_to_roam(struct adapter *adapter)
2920 {
2921 adapter->mlmepriv.to_roam--;
2922 return adapter->mlmepriv.to_roam;
2923 }
2924
rtw_to_roam(struct adapter * adapter)2925 inline u8 rtw_to_roam(struct adapter *adapter)
2926 {
2927 return adapter->mlmepriv.to_roam;
2928 }
2929
rtw_roaming(struct adapter * padapter,struct wlan_network * tgt_network)2930 void rtw_roaming(struct adapter *padapter, struct wlan_network *tgt_network)
2931 {
2932 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2933
2934 spin_lock_bh(&pmlmepriv->lock);
2935 _rtw_roaming(padapter, tgt_network);
2936 spin_unlock_bh(&pmlmepriv->lock);
2937 }
_rtw_roaming(struct adapter * padapter,struct wlan_network * tgt_network)2938 void _rtw_roaming(struct adapter *padapter, struct wlan_network *tgt_network)
2939 {
2940 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2941 struct wlan_network *cur_network = &pmlmepriv->cur_network;
2942 int do_join_r;
2943
2944 if (0 < rtw_to_roam(padapter)) {
2945 DBG_871X("roaming from %s("MAC_FMT"), length:%d\n",
2946 cur_network->network.Ssid.Ssid, MAC_ARG(cur_network->network.MacAddress),
2947 cur_network->network.Ssid.SsidLength);
2948 memcpy(&pmlmepriv->assoc_ssid, &cur_network->network.Ssid, sizeof(struct ndis_802_11_ssid));
2949
2950 pmlmepriv->assoc_by_bssid = false;
2951
2952 while (1) {
2953 do_join_r = rtw_do_join(padapter);
2954 if (_SUCCESS == do_join_r) {
2955 break;
2956 } else {
2957 DBG_871X("roaming do_join return %d\n", do_join_r);
2958 rtw_dec_to_roam(padapter);
2959
2960 if (rtw_to_roam(padapter) > 0) {
2961 continue;
2962 } else {
2963 DBG_871X("%s(%d) -to roaming fail, indicate_disconnect\n", __func__, __LINE__);
2964 rtw_indicate_disconnect(padapter);
2965 break;
2966 }
2967 }
2968 }
2969 }
2970
2971 }
2972
rtw_linked_check(struct adapter * padapter)2973 sint rtw_linked_check(struct adapter *padapter)
2974 {
2975 if ((check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE) == true) ||
2976 (check_fwstate(&padapter->mlmepriv, WIFI_ADHOC_STATE|WIFI_ADHOC_MASTER_STATE) == true)) {
2977 if (padapter->stapriv.asoc_sta_count > 2)
2978 return true;
2979 } else { /* Station mode */
2980 if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) == true)
2981 return true;
2982 }
2983 return false;
2984 }
2985