• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 
8 #include <drv_types.h>
9 #include <rtw_debug.h>
10 #include <asm/unaligned.h>
11 
init_mlme_ap_info(struct adapter * padapter)12 void init_mlme_ap_info(struct adapter *padapter)
13 {
14 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
15 	struct sta_priv *pstapriv = &padapter->stapriv;
16 	struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
17 
18 	spin_lock_init(&pmlmepriv->bcn_update_lock);
19 
20 	/* for ACL */
21 	INIT_LIST_HEAD(&pacl_list->acl_node_q.queue);
22 	spin_lock_init(&pacl_list->acl_node_q.lock);
23 
24 	/* pmlmeext->bstart_bss = false; */
25 
26 	start_ap_mode(padapter);
27 }
28 
free_mlme_ap_info(struct adapter * padapter)29 void free_mlme_ap_info(struct adapter *padapter)
30 {
31 	struct sta_info *psta = NULL;
32 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
33 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
34 	struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
35 
36 	/* stop_ap_mode(padapter); */
37 
38 	pmlmepriv->update_bcn = false;
39 	pmlmeext->bstart_bss = false;
40 
41 	rtw_sta_flush(padapter);
42 
43 	pmlmeinfo->state = _HW_STATE_NOLINK_;
44 
45 	/* free_assoc_sta_resources */
46 	rtw_free_all_stainfo(padapter);
47 
48 	/* free bc/mc sta_info */
49 	psta = rtw_get_bcmc_stainfo(padapter);
50 	rtw_free_stainfo(padapter, psta);
51 }
52 
update_BCNTIM(struct adapter * padapter)53 static void update_BCNTIM(struct adapter *padapter)
54 {
55 	struct sta_priv *pstapriv = &padapter->stapriv;
56 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
57 	struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
58 	struct wlan_bssid_ex *pnetwork_mlmeext = &pmlmeinfo->network;
59 	unsigned char *pie = pnetwork_mlmeext->ies;
60 
61 	/* update TIM IE */
62 	u8 *p, *dst_ie, *premainder_ie = NULL, *pbackup_remainder_ie = NULL;
63 	__le16 tim_bitmap_le;
64 	uint offset, tmp_len, tim_ielen, tim_ie_offset, remainder_ielen;
65 
66 	tim_bitmap_le = cpu_to_le16(pstapriv->tim_bitmap);
67 
68 	p = rtw_get_ie(pie + _FIXED_IE_LENGTH_,
69 		       WLAN_EID_TIM,
70 		       &tim_ielen,
71 		       pnetwork_mlmeext->ie_length - _FIXED_IE_LENGTH_
72 	);
73 	if (p && tim_ielen > 0) {
74 		tim_ielen += 2;
75 
76 		premainder_ie = p + tim_ielen;
77 
78 		tim_ie_offset = (signed int)(p - pie);
79 
80 		remainder_ielen = pnetwork_mlmeext->ie_length - tim_ie_offset - tim_ielen;
81 
82 		/* append TIM IE from dst_ie offset */
83 		dst_ie = p;
84 	} else {
85 		tim_ielen = 0;
86 
87 		/* calculate head_len */
88 		offset = _FIXED_IE_LENGTH_;
89 
90 		/* get ssid_ie len */
91 		p = rtw_get_ie(pie + _BEACON_IE_OFFSET_,
92 			       WLAN_EID_SSID,
93 			       &tmp_len,
94 			       (pnetwork_mlmeext->ie_length - _BEACON_IE_OFFSET_)
95 		);
96 		if (p)
97 			offset += tmp_len + 2;
98 
99 		/*  get supported rates len */
100 		p = rtw_get_ie(pie + _BEACON_IE_OFFSET_,
101 			       WLAN_EID_SUPP_RATES, &tmp_len,
102 			       (pnetwork_mlmeext->ie_length - _BEACON_IE_OFFSET_)
103 		);
104 		if (p)
105 			offset += tmp_len + 2;
106 
107 		/* DS Parameter Set IE, len =3 */
108 		offset += 3;
109 
110 		premainder_ie = pie + offset;
111 
112 		remainder_ielen = pnetwork_mlmeext->ie_length - offset - tim_ielen;
113 
114 		/* append TIM IE from offset */
115 		dst_ie = pie + offset;
116 	}
117 
118 	if (remainder_ielen > 0) {
119 		pbackup_remainder_ie = rtw_malloc(remainder_ielen);
120 		if (pbackup_remainder_ie && premainder_ie)
121 			memcpy(pbackup_remainder_ie, premainder_ie, remainder_ielen);
122 	}
123 
124 	*dst_ie++ = WLAN_EID_TIM;
125 
126 	if ((pstapriv->tim_bitmap & 0xff00) && (pstapriv->tim_bitmap & 0x00fe))
127 		tim_ielen = 5;
128 	else
129 		tim_ielen = 4;
130 
131 	*dst_ie++ = tim_ielen;
132 
133 	*dst_ie++ = 0;/* DTIM count */
134 	*dst_ie++ = 1;/* DTIM period */
135 
136 	if (pstapriv->tim_bitmap & BIT(0))/* for bc/mc frames */
137 		*dst_ie++ = BIT(0);/* bitmap ctrl */
138 	else
139 		*dst_ie++ = 0;
140 
141 	if (tim_ielen == 4) {
142 		__le16 pvb;
143 
144 		if (pstapriv->tim_bitmap & 0xff00)
145 			pvb = cpu_to_le16(pstapriv->tim_bitmap >> 8);
146 		else
147 			pvb = tim_bitmap_le;
148 
149 		*dst_ie++ = le16_to_cpu(pvb);
150 
151 	} else if (tim_ielen == 5) {
152 		memcpy(dst_ie, &tim_bitmap_le, 2);
153 		dst_ie += 2;
154 	}
155 
156 	/* copy remainder IE */
157 	if (pbackup_remainder_ie) {
158 		memcpy(dst_ie, pbackup_remainder_ie, remainder_ielen);
159 
160 		kfree(pbackup_remainder_ie);
161 	}
162 
163 	offset =  (uint)(dst_ie - pie);
164 	pnetwork_mlmeext->ie_length = offset + remainder_ielen;
165 }
166 
chk_sta_is_alive(struct sta_info * psta)167 static u8 chk_sta_is_alive(struct sta_info *psta)
168 {
169 	sta_update_last_rx_pkts(psta);
170 
171 	return true;
172 }
173 
expire_timeout_chk(struct adapter * padapter)174 void expire_timeout_chk(struct adapter *padapter)
175 {
176 	struct list_head *phead, *plist, *tmp;
177 	u8 updated = false;
178 	struct sta_info *psta = NULL;
179 	struct sta_priv *pstapriv = &padapter->stapriv;
180 	u8 chk_alive_num = 0;
181 	char chk_alive_list[NUM_STA];
182 	int i;
183 
184 	spin_lock_bh(&pstapriv->auth_list_lock);
185 
186 	phead = &pstapriv->auth_list;
187 	/* check auth_queue */
188 	list_for_each_safe(plist, tmp, phead) {
189 		psta = list_entry(plist, struct sta_info, auth_list);
190 
191 		if (psta->expire_to > 0) {
192 			psta->expire_to--;
193 			if (psta->expire_to == 0) {
194 				list_del_init(&psta->auth_list);
195 				pstapriv->auth_list_cnt--;
196 
197 				spin_unlock_bh(&pstapriv->auth_list_lock);
198 
199 				rtw_free_stainfo(padapter, psta);
200 
201 				spin_lock_bh(&pstapriv->auth_list_lock);
202 			}
203 		}
204 	}
205 
206 	spin_unlock_bh(&pstapriv->auth_list_lock);
207 	psta = NULL;
208 
209 	spin_lock_bh(&pstapriv->asoc_list_lock);
210 
211 	phead = &pstapriv->asoc_list;
212 	/* check asoc_queue */
213 	list_for_each_safe(plist, tmp, phead) {
214 		psta = list_entry(plist, struct sta_info, asoc_list);
215 		if (chk_sta_is_alive(psta) || !psta->expire_to) {
216 			psta->expire_to = pstapriv->expire_to;
217 			psta->keep_alive_trycnt = 0;
218 			psta->under_exist_checking = 0;
219 		} else {
220 			if (psta->expire_to > 0)
221 				psta->expire_to--;
222 		}
223 
224 		if (psta->expire_to == 0) {
225 			struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
226 
227 			if (padapter->registrypriv.wifi_spec == 1) {
228 				psta->expire_to = pstapriv->expire_to;
229 				continue;
230 			}
231 
232 			if (psta->state & WIFI_SLEEP_STATE) {
233 				if (!(psta->state & WIFI_STA_ALIVE_CHK_STATE)) {
234 					/* to check if alive by another methods */
235 					/* if station is at ps mode. */
236 					psta->expire_to = pstapriv->expire_to;
237 					psta->state |= WIFI_STA_ALIVE_CHK_STATE;
238 
239 					/* to update bcn with tim_bitmap for this station */
240 					pstapriv->tim_bitmap |= BIT(psta->aid);
241 					update_beacon(padapter, WLAN_EID_TIM, NULL, true);
242 
243 					if (!pmlmeext->active_keep_alive_check)
244 						continue;
245 				}
246 			}
247 			if (pmlmeext->active_keep_alive_check) {
248 				int stainfo_offset;
249 
250 				stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
251 				if (stainfo_offset_valid(stainfo_offset))
252 					chk_alive_list[chk_alive_num++] = stainfo_offset;
253 
254 				continue;
255 			}
256 			list_del_init(&psta->asoc_list);
257 			pstapriv->asoc_list_cnt--;
258 			updated = ap_free_sta(padapter, psta, false, WLAN_REASON_DEAUTH_LEAVING);
259 		} else {
260 			/* TODO: Aging mechanism to digest frames in sleep_q to */
261 			/* avoid running out of xmitframe */
262 			if (psta->sleepq_len > (NR_XMITFRAME / pstapriv->asoc_list_cnt)
263 				&& padapter->xmitpriv.free_xmitframe_cnt < ((
264 					NR_XMITFRAME / pstapriv->asoc_list_cnt
265 				) / 2)
266 			)
267 				wakeup_sta_to_xmit(padapter, psta);
268 		}
269 	}
270 
271 	spin_unlock_bh(&pstapriv->asoc_list_lock);
272 
273 	if (chk_alive_num) {
274 		u8 backup_oper_channel = 0;
275 		struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
276 
277 		/* switch to correct channel of current network  before issue keep-alive frames */
278 		if (rtw_get_oper_ch(padapter) != pmlmeext->cur_channel) {
279 			backup_oper_channel = rtw_get_oper_ch(padapter);
280 			SelectChannel(padapter, pmlmeext->cur_channel);
281 		}
282 
283 		/* issue null data to check sta alive*/
284 		for (i = 0; i < chk_alive_num; i++) {
285 			int ret = _FAIL;
286 
287 			psta = rtw_get_stainfo_by_offset(pstapriv, chk_alive_list[i]);
288 			if (!(psta->state & _FW_LINKED))
289 				continue;
290 
291 			if (psta->state & WIFI_SLEEP_STATE)
292 				ret = issue_nulldata(padapter, psta->hwaddr, 0, 1, 50);
293 			else
294 				ret = issue_nulldata(padapter, psta->hwaddr, 0, 3, 50);
295 
296 			psta->keep_alive_trycnt++;
297 			if (ret == _SUCCESS) {
298 				psta->expire_to = pstapriv->expire_to;
299 				psta->keep_alive_trycnt = 0;
300 				continue;
301 			} else if (psta->keep_alive_trycnt <= 3) {
302 				psta->expire_to = 1;
303 				continue;
304 			}
305 
306 			psta->keep_alive_trycnt = 0;
307 			spin_lock_bh(&pstapriv->asoc_list_lock);
308 			if (list_empty(&psta->asoc_list) == false) {
309 				list_del_init(&psta->asoc_list);
310 				pstapriv->asoc_list_cnt--;
311 				updated = ap_free_sta(padapter, psta, false,
312 						      WLAN_REASON_DEAUTH_LEAVING);
313 			}
314 			spin_unlock_bh(&pstapriv->asoc_list_lock);
315 		}
316 
317 		if (backup_oper_channel > 0) /* back to the original operation channel */
318 			SelectChannel(padapter, backup_oper_channel);
319 	}
320 
321 	associated_clients_update(padapter, updated);
322 }
323 
add_RATid(struct adapter * padapter,struct sta_info * psta,u8 rssi_level)324 void add_RATid(struct adapter *padapter, struct sta_info *psta, u8 rssi_level)
325 {
326 	unsigned char sta_band = 0, shortGIrate = false;
327 	unsigned int tx_ra_bitmap = 0;
328 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
329 	struct wlan_bssid_ex
330 		*pcur_network = (struct wlan_bssid_ex *)&pmlmepriv->cur_network.network;
331 
332 	if (!psta)
333 		return;
334 
335 	if (!(psta->state & _FW_LINKED))
336 		return;
337 
338 	rtw_hal_update_sta_rate_mask(padapter, psta);
339 	tx_ra_bitmap = psta->ra_mask;
340 
341 	shortGIrate = query_ra_short_GI(psta);
342 
343 	if (pcur_network->configuration.ds_config > 14) {
344 		sta_band |= WIRELESS_INVALID;
345 	} else {
346 		if (tx_ra_bitmap & 0xffff000)
347 			sta_band |= WIRELESS_11_24N;
348 
349 		if (tx_ra_bitmap & 0xff0)
350 			sta_band |= WIRELESS_11G;
351 
352 		if (tx_ra_bitmap & 0x0f)
353 			sta_band |= WIRELESS_11B;
354 	}
355 
356 	psta->wireless_mode = sta_band;
357 	psta->raid = networktype_to_raid_ex(padapter, psta);
358 
359 	if (psta->aid < NUM_STA) {
360 		u8 arg[4] = {0};
361 
362 		arg[0] = psta->mac_id;
363 		arg[1] = psta->raid;
364 		arg[2] = shortGIrate;
365 		arg[3] = psta->init_rate;
366 
367 		rtw_hal_add_ra_tid(padapter, tx_ra_bitmap, arg, rssi_level);
368 	}
369 }
370 
update_bmc_sta(struct adapter * padapter)371 void update_bmc_sta(struct adapter *padapter)
372 {
373 	unsigned char network_type;
374 	int supportRateNum = 0;
375 	unsigned int tx_ra_bitmap = 0;
376 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
377 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
378 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
379 	struct wlan_bssid_ex
380 		*pcur_network = (struct wlan_bssid_ex *)&pmlmepriv->cur_network.network;
381 	struct sta_info *psta = rtw_get_bcmc_stainfo(padapter);
382 
383 	if (psta) {
384 		psta->aid = 0;/* default set to 0 */
385 		/* psta->mac_id = psta->aid+4; */
386 		psta->mac_id = psta->aid + 1;/* mac_id = 1 for bc/mc stainfo */
387 
388 		pmlmeinfo->FW_sta_info[psta->mac_id].psta = psta;
389 
390 		psta->qos_option = 0;
391 		psta->htpriv.ht_option = false;
392 
393 		psta->ieee8021x_blocked = 0;
394 
395 		memset((void *)&psta->sta_stats, 0, sizeof(struct stainfo_stats));
396 
397 		/* psta->dot118021XPrivacy = _NO_PRIVACY_;//!!! remove it, because it has been set before this. */
398 
399 		/* prepare for add_RATid */
400 		supportRateNum = rtw_get_rateset_len((u8 *)&pcur_network->supported_rates);
401 		network_type = rtw_check_network_type((u8 *)&pcur_network->supported_rates,
402 						      supportRateNum,
403 						      pcur_network->configuration.ds_config
404 		);
405 		if (is_supported_tx_cck(network_type)) {
406 			network_type = WIRELESS_11B;
407 		} else if (network_type == WIRELESS_INVALID) { /*  error handling */
408 
409 			if (pcur_network->configuration.ds_config > 14)
410 				network_type = WIRELESS_INVALID;
411 			else
412 				network_type = WIRELESS_11B;
413 		}
414 		update_sta_basic_rate(psta, network_type);
415 		psta->wireless_mode = network_type;
416 
417 		rtw_hal_update_sta_rate_mask(padapter, psta);
418 		tx_ra_bitmap = psta->ra_mask;
419 
420 		psta->raid = networktype_to_raid_ex(padapter, psta);
421 
422 		/* ap mode */
423 		rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, true);
424 
425 		/* if (pHalData->fw_ractrl == true) */
426 		{
427 			u8 arg[4] = {0};
428 
429 			arg[0] = psta->mac_id;
430 			arg[1] = psta->raid;
431 			arg[2] = 0;
432 			arg[3] = psta->init_rate;
433 
434 			rtw_hal_add_ra_tid(padapter, tx_ra_bitmap, arg, 0);
435 		}
436 
437 		rtw_sta_media_status_rpt(padapter, psta, 1);
438 
439 		spin_lock_bh(&psta->lock);
440 		psta->state = _FW_LINKED;
441 		spin_unlock_bh(&psta->lock);
442 
443 	}
444 }
445 
446 /* notes: */
447 /* AID: 1~MAX for sta and 0 for bc/mc in ap/adhoc mode */
448 /* MAC_ID = AID+1 for sta in ap/adhoc mode */
449 /* MAC_ID = 1 for bc/mc for sta/ap/adhoc */
450 /* MAC_ID = 0 for bssid for sta/ap/adhoc */
451 /* CAM_ID = 0~3 for default key, cmd_id =macid + 3, macid =aid+1; */
452 
update_sta_info_apmode(struct adapter * padapter,struct sta_info * psta)453 void update_sta_info_apmode(struct adapter *padapter, struct sta_info *psta)
454 {
455 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
456 	struct security_priv *psecuritypriv = &padapter->securitypriv;
457 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
458 	struct ht_priv *phtpriv_ap = &pmlmepriv->htpriv;
459 	struct ht_priv *phtpriv_sta = &psta->htpriv;
460 	u8 cur_ldpc_cap = 0, cur_stbc_cap = 0, cur_beamform_cap = 0;
461 	/* set intf_tag to if1 */
462 	/* psta->intf_tag = 0; */
463 
464 	/* psta->mac_id = psta->aid+4; */
465 	/* psta->mac_id = psta->aid+1;//alloc macid when call rtw_alloc_stainfo(), */
466 	/* release macid when call rtw_free_stainfo() */
467 
468 	/* ap mode */
469 	rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, true);
470 
471 	if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X)
472 		psta->ieee8021x_blocked = true;
473 	else
474 		psta->ieee8021x_blocked = false;
475 
476 	/* update sta's cap */
477 
478 	/* ERP */
479 	VCS_update(padapter, psta);
480 
481 	/* HT related cap */
482 	if (phtpriv_sta->ht_option) {
483 		/* check if sta supports rx ampdu */
484 		phtpriv_sta->ampdu_enable = phtpriv_ap->ampdu_enable;
485 
486 		phtpriv_sta->rx_ampdu_min_spacing = (
487 			phtpriv_sta->ht_cap.ampdu_params_info & IEEE80211_HT_CAP_AMPDU_DENSITY
488 		) >> 2;
489 
490 		/*  bwmode */
491 		if ((
492 			phtpriv_sta->ht_cap.cap_info & phtpriv_ap->ht_cap.cap_info
493 		) & cpu_to_le16(IEEE80211_HT_CAP_SUP_WIDTH))
494 			psta->bw_mode = CHANNEL_WIDTH_40;
495 		else
496 			psta->bw_mode = CHANNEL_WIDTH_20;
497 
498 		if (pmlmeext->cur_bwmode < psta->bw_mode)
499 			psta->bw_mode = pmlmeext->cur_bwmode;
500 
501 		phtpriv_sta->ch_offset = pmlmeext->cur_ch_offset;
502 
503 		/* check if sta support s Short GI 20M */
504 		if ((
505 			phtpriv_sta->ht_cap.cap_info & phtpriv_ap->ht_cap.cap_info
506 		) & cpu_to_le16(IEEE80211_HT_CAP_SGI_20))
507 			phtpriv_sta->sgi_20m = true;
508 
509 		/* check if sta support s Short GI 40M */
510 		if ((
511 			phtpriv_sta->ht_cap.cap_info & phtpriv_ap->ht_cap.cap_info
512 		) & cpu_to_le16(IEEE80211_HT_CAP_SGI_40)) {
513 			if (psta->bw_mode == CHANNEL_WIDTH_40) /* according to psta->bw_mode */
514 				phtpriv_sta->sgi_40m = true;
515 			else
516 				phtpriv_sta->sgi_40m = false;
517 		}
518 
519 		psta->qos_option = true;
520 
521 		/*  B0 Config LDPC Coding Capability */
522 		if (TEST_FLAG(phtpriv_ap->ldpc_cap, LDPC_HT_ENABLE_TX) &&
523 		    GET_HT_CAPABILITY_ELE_LDPC_CAP((u8 *)(&phtpriv_sta->ht_cap)))
524 			SET_FLAG(cur_ldpc_cap, (LDPC_HT_ENABLE_TX | LDPC_HT_CAP_TX));
525 
526 		/*  B7 B8 B9 Config STBC setting */
527 		if (TEST_FLAG(phtpriv_ap->stbc_cap, STBC_HT_ENABLE_TX) &&
528 		    GET_HT_CAPABILITY_ELE_RX_STBC((u8 *)(&phtpriv_sta->ht_cap)))
529 			SET_FLAG(cur_stbc_cap, (STBC_HT_ENABLE_TX | STBC_HT_CAP_TX));
530 	} else {
531 		phtpriv_sta->ampdu_enable = false;
532 
533 		phtpriv_sta->sgi_20m = false;
534 		phtpriv_sta->sgi_40m = false;
535 		psta->bw_mode = CHANNEL_WIDTH_20;
536 		phtpriv_sta->ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
537 	}
538 
539 	phtpriv_sta->ldpc_cap = cur_ldpc_cap;
540 	phtpriv_sta->stbc_cap = cur_stbc_cap;
541 	phtpriv_sta->beamform_cap = cur_beamform_cap;
542 
543 	/* Rx AMPDU */
544 	send_delba(padapter, 0, psta->hwaddr);/*  recipient */
545 
546 	/* TX AMPDU */
547 	send_delba(padapter, 1, psta->hwaddr);/* originator */
548 	phtpriv_sta->agg_enable_bitmap = 0x0;/* reset */
549 	phtpriv_sta->candidate_tid_bitmap = 0x0;/* reset */
550 
551 	update_ldpc_stbc_cap(psta);
552 
553 	/* todo: init other variables */
554 
555 	memset((void *)&psta->sta_stats, 0, sizeof(struct stainfo_stats));
556 
557 	/* add ratid */
558 	/* add_RATid(padapter, psta);//move to ap_sta_info_defer_update() */
559 
560 	spin_lock_bh(&psta->lock);
561 	psta->state |= _FW_LINKED;
562 	spin_unlock_bh(&psta->lock);
563 }
564 
update_ap_info(struct adapter * padapter,struct sta_info * psta)565 static void update_ap_info(struct adapter *padapter, struct sta_info *psta)
566 {
567 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
568 	struct wlan_bssid_ex
569 		*pnetwork = (struct wlan_bssid_ex *)&pmlmepriv->cur_network.network;
570 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
571 	struct ht_priv *phtpriv_ap = &pmlmepriv->htpriv;
572 
573 	psta->wireless_mode = pmlmeext->cur_wireless_mode;
574 
575 	psta->bssratelen = rtw_get_rateset_len(pnetwork->supported_rates);
576 	memcpy(psta->bssrateset, pnetwork->supported_rates, psta->bssratelen);
577 
578 	/* HT related cap */
579 	if (phtpriv_ap->ht_option) {
580 		/* check if sta supports rx ampdu */
581 		/* phtpriv_ap->ampdu_enable = phtpriv_ap->ampdu_enable; */
582 
583 		/* check if sta support s Short GI 20M */
584 		if ((phtpriv_ap->ht_cap.cap_info) & cpu_to_le16(IEEE80211_HT_CAP_SGI_20))
585 			phtpriv_ap->sgi_20m = true;
586 
587 		/* check if sta support s Short GI 40M */
588 		if ((phtpriv_ap->ht_cap.cap_info) & cpu_to_le16(IEEE80211_HT_CAP_SGI_40))
589 			phtpriv_ap->sgi_40m = true;
590 
591 		psta->qos_option = true;
592 	} else {
593 		phtpriv_ap->ampdu_enable = false;
594 
595 		phtpriv_ap->sgi_20m = false;
596 		phtpriv_ap->sgi_40m = false;
597 	}
598 
599 	psta->bw_mode = pmlmeext->cur_bwmode;
600 	phtpriv_ap->ch_offset = pmlmeext->cur_ch_offset;
601 
602 	phtpriv_ap->agg_enable_bitmap = 0x0;/* reset */
603 	phtpriv_ap->candidate_tid_bitmap = 0x0;/* reset */
604 
605 	memcpy(&psta->htpriv, &pmlmepriv->htpriv, sizeof(struct ht_priv));
606 }
607 
update_hw_ht_param(struct adapter * padapter)608 static void update_hw_ht_param(struct adapter *padapter)
609 {
610 	unsigned char max_AMPDU_len;
611 	unsigned char min_MPDU_spacing;
612 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
613 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
614 
615 	/* handle A-MPDU parameter field
616 	 *
617 	 *	AMPDU_para [1:0]:Max AMPDU Len => 0:8k , 1:16k, 2:32k, 3:64k
618 	 *	AMPDU_para [4:2]:Min MPDU Start Spacing
619 	 */
620 	max_AMPDU_len = pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x03;
621 
622 	min_MPDU_spacing = (pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x1c) >> 2;
623 
624 	rtw_hal_set_hwreg(padapter, HW_VAR_AMPDU_MIN_SPACE, (u8 *)(&min_MPDU_spacing));
625 
626 	rtw_hal_set_hwreg(padapter, HW_VAR_AMPDU_FACTOR, (u8 *)(&max_AMPDU_len));
627 
628 	/*  */
629 	/*  Config SM Power Save setting */
630 	/*  */
631 	pmlmeinfo->SM_PS = (le16_to_cpu(
632 		pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info
633 	) & 0x0C) >> 2;
634 
635 	/*  */
636 	/*  Config current HT Protection mode. */
637 	/*  */
638 	/* pmlmeinfo->HT_protection = pmlmeinfo->HT_info.infos[1] & 0x3; */
639 }
640 
start_bss_network(struct adapter * padapter)641 void start_bss_network(struct adapter *padapter)
642 {
643 	u8 *p;
644 	u8 val8, cur_channel, cur_bwmode, cur_ch_offset;
645 	u16 bcn_interval;
646 	u32 acparm;
647 	int	ie_len;
648 	struct registry_priv  *pregpriv = &padapter->registrypriv;
649 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
650 	struct security_priv *psecuritypriv = &(padapter->securitypriv);
651 	struct wlan_bssid_ex
652 		*pnetwork = (struct wlan_bssid_ex *)&pmlmepriv->cur_network.network;
653 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
654 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
655 	struct wlan_bssid_ex *pnetwork_mlmeext = &(pmlmeinfo->network);
656 	struct HT_info_element *pht_info = NULL;
657 	u8 cbw40_enable = 0;
658 
659 	bcn_interval = (u16)pnetwork->configuration.beacon_period;
660 	cur_channel = pnetwork->configuration.ds_config;
661 	cur_bwmode = CHANNEL_WIDTH_20;
662 	cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
663 
664 	/* check if there is wps ie, */
665 	/* if there is wpsie in beacon, the hostapd will update beacon twice when stating hostapd, */
666 	/* and at first time the security ie (RSN/WPA IE) will not include in beacon. */
667 	if (!rtw_get_wps_ie(pnetwork->ies + _FIXED_IE_LENGTH_,
668 			    pnetwork->ie_length - _FIXED_IE_LENGTH_, NULL, NULL))
669 		pmlmeext->bstart_bss = true;
670 
671 	/* todo: update wmm, ht cap */
672 	/* pmlmeinfo->WMM_enable; */
673 	/* pmlmeinfo->HT_enable; */
674 	if (pmlmepriv->qospriv.qos_option)
675 		pmlmeinfo->WMM_enable = true;
676 	if (pmlmepriv->htpriv.ht_option) {
677 		pmlmeinfo->WMM_enable = true;
678 		pmlmeinfo->HT_enable = true;
679 		/* pmlmeinfo->HT_info_enable = true; */
680 		/* pmlmeinfo->HT_caps_enable = true; */
681 
682 		update_hw_ht_param(padapter);
683 	}
684 
685 	if (!pmlmepriv->cur_network.join_res) { /* setting only at  first time */
686 
687 		/* WEP Key will be set before this function, do not clear CAM. */
688 		if ((psecuritypriv->dot11PrivacyAlgrthm != _WEP40_) &&
689 		    (psecuritypriv->dot11PrivacyAlgrthm != _WEP104_))
690 			flush_all_cam_entry(padapter);	/* clear CAM */
691 	}
692 
693 	/* set MSR to AP_Mode */
694 	Set_MSR(padapter, _HW_STATE_AP_);
695 
696 	/* Set BSSID REG */
697 	rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, pnetwork->mac_address);
698 
699 	/* Set EDCA param reg */
700 	acparm = 0x002F3217; /*  VO */
701 	rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VO, (u8 *)(&acparm));
702 	acparm = 0x005E4317; /*  VI */
703 	rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VI, (u8 *)(&acparm));
704 	/* acparm = 0x00105320; // BE */
705 	acparm = 0x005ea42b;
706 	rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BE, (u8 *)(&acparm));
707 	acparm = 0x0000A444; /*  BK */
708 	rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BK, (u8 *)(&acparm));
709 
710 	/* Set Security */
711 	val8 = (
712 		psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X
713 	) ? 0xcc : 0xcf;
714 	rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
715 
716 	/* Beacon Control related register */
717 	rtw_hal_set_hwreg(padapter, HW_VAR_BEACON_INTERVAL, (u8 *)(&bcn_interval));
718 
719 	rtw_hal_set_hwreg(padapter, HW_VAR_DO_IQK, NULL);
720 
721 	if (!pmlmepriv->cur_network.join_res) { /* setting only at  first time */
722 		/* u32 initialgain; */
723 
724 		/* initialgain = 0x1e; */
725 
726 		/* disable dynamic functions, such as high power, DIG */
727 		/* Save_DM_Func_Flag(padapter); */
728 		/* Switch_DM_Func(padapter, DYNAMIC_FUNC_DISABLE, false); */
729 
730 		/* turn on all dynamic functions */
731 		Switch_DM_Func(padapter, DYNAMIC_ALL_FUNC_ENABLE, true);
732 
733 		/* rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain)); */
734 	}
735 
736 	/* set channel, bwmode */
737 	p = rtw_get_ie((pnetwork->ies + sizeof(struct ndis_802_11_fix_ie)),
738 		       WLAN_EID_HT_OPERATION,
739 		       &ie_len,
740 		       (pnetwork->ie_length - sizeof(struct ndis_802_11_fix_ie))
741 	);
742 	if (p && ie_len) {
743 		pht_info = (struct HT_info_element *)(p + 2);
744 
745 		if (cur_channel > 14) {
746 			if ((pregpriv->bw_mode & 0xf0) > 0)
747 				cbw40_enable = 1;
748 		} else {
749 			if ((pregpriv->bw_mode & 0x0f) > 0)
750 				cbw40_enable = 1;
751 		}
752 
753 		if ((cbw40_enable) &&	 (pht_info->infos[0] & BIT(2))) {
754 			/* switch to the 40M Hz mode */
755 			/* pmlmeext->cur_bwmode = CHANNEL_WIDTH_40; */
756 			cur_bwmode = CHANNEL_WIDTH_40;
757 			switch (pht_info->infos[0] & 0x3) {
758 			case 1:
759 				/* pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER; */
760 				cur_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER;
761 				break;
762 
763 			case 3:
764 				/* pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER; */
765 				cur_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER;
766 				break;
767 
768 			default:
769 				/* pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE; */
770 				cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
771 				break;
772 			}
773 		}
774 	}
775 
776 	set_channel_bwmode(padapter, cur_channel, cur_ch_offset, cur_bwmode);
777 	pmlmeext->cur_channel = cur_channel;
778 	pmlmeext->cur_bwmode = cur_bwmode;
779 	pmlmeext->cur_ch_offset = cur_ch_offset;
780 	pmlmeext->cur_wireless_mode = pmlmepriv->cur_network.network_type;
781 
782 	/* let pnetwork_mlmeext == pnetwork_mlme. */
783 	memcpy(pnetwork_mlmeext, pnetwork, pnetwork->length);
784 
785 	/* update cur_wireless_mode */
786 	update_wireless_mode(padapter);
787 
788 	/* update RRSR after set channel and bandwidth */
789 	UpdateBrateTbl(padapter, pnetwork->supported_rates);
790 	rtw_hal_set_hwreg(padapter, HW_VAR_BASIC_RATE, pnetwork->supported_rates);
791 
792 	/* update capability after cur_wireless_mode updated */
793 	update_capinfo(
794 		padapter,
795 		rtw_get_capability((struct wlan_bssid_ex *)pnetwork)
796 	);
797 
798 	if (pmlmeext->bstart_bss) {
799 		update_beacon(padapter, WLAN_EID_TIM, NULL, true);
800 
801 		/* issue beacon frame */
802 		send_beacon(padapter);
803 	}
804 
805 	/* update bc/mc sta_info */
806 	update_bmc_sta(padapter);
807 
808 	/* pmlmeext->bstart_bss = true; */
809 }
810 
rtw_check_beacon_data(struct adapter * padapter,u8 * pbuf,int len)811 int rtw_check_beacon_data(struct adapter *padapter, u8 *pbuf,  int len)
812 {
813 	int ret = _SUCCESS;
814 	u8 *p;
815 	u8 *pHT_caps_ie = NULL;
816 	u8 *pHT_info_ie = NULL;
817 	struct sta_info *psta = NULL;
818 	u16 cap, ht_cap = false;
819 	uint ie_len = 0;
820 	int group_cipher, pairwise_cipher;
821 	u8 channel, network_type, supportRate[NDIS_802_11_LENGTH_RATES_EX];
822 	int supportRateNum = 0;
823 	u8 OUI1[] = {0x00, 0x50, 0xf2, 0x01};
824 	u8 WMM_PARA_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x01, 0x01};
825 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
826 	struct security_priv *psecuritypriv = &padapter->securitypriv;
827 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
828 	struct wlan_bssid_ex
829 		*pbss_network = (struct wlan_bssid_ex *)&pmlmepriv->cur_network.network;
830 	u8 *ie = pbss_network->ies;
831 
832 	if (!check_fwstate(pmlmepriv, WIFI_AP_STATE))
833 		return _FAIL;
834 
835 	if (len < 0 || len > MAX_IE_SZ)
836 		return _FAIL;
837 
838 	pbss_network->ie_length = len;
839 
840 	memset(ie, 0, MAX_IE_SZ);
841 
842 	memcpy(ie, pbuf, pbss_network->ie_length);
843 
844 	if (pbss_network->infrastructure_mode != Ndis802_11APMode)
845 		return _FAIL;
846 
847 	pbss_network->rssi = 0;
848 
849 	memcpy(pbss_network->mac_address, myid(&(padapter->eeprompriv)), ETH_ALEN);
850 
851 	/* beacon interval */
852 	p = rtw_get_beacon_interval_from_ie(ie);/* ie + 8;	8: TimeStamp, 2: Beacon Interval 2:Capability */
853 	/* pbss_network->configuration.beacon_period = le16_to_cpu(*(unsigned short*)p); */
854 	pbss_network->configuration.beacon_period = get_unaligned_le16(p);
855 
856 	/* capability */
857 	/* cap = *(unsigned short *)rtw_get_capability_from_ie(ie); */
858 	/* cap = le16_to_cpu(cap); */
859 	cap = get_unaligned_le16(ie);
860 
861 	/* SSID */
862 	p = rtw_get_ie(
863 		ie + _BEACON_IE_OFFSET_,
864 		WLAN_EID_SSID,
865 		&ie_len,
866 		(pbss_network->ie_length - _BEACON_IE_OFFSET_)
867 	);
868 	if (p && ie_len > 0) {
869 		memset(&pbss_network->ssid, 0, sizeof(struct ndis_802_11_ssid));
870 		memcpy(pbss_network->ssid.ssid, (p + 2), ie_len);
871 		pbss_network->ssid.ssid_length = ie_len;
872 	}
873 
874 	/* channel */
875 	channel = 0;
876 	pbss_network->configuration.length = 0;
877 	p = rtw_get_ie(
878 		ie + _BEACON_IE_OFFSET_,
879 		WLAN_EID_DS_PARAMS, &ie_len,
880 		(pbss_network->ie_length - _BEACON_IE_OFFSET_)
881 	);
882 	if (p && ie_len > 0)
883 		channel = *(p + 2);
884 
885 	pbss_network->configuration.ds_config = channel;
886 
887 	memset(supportRate, 0, NDIS_802_11_LENGTH_RATES_EX);
888 	/*  get supported rates */
889 	p = rtw_get_ie(
890 		ie + _BEACON_IE_OFFSET_,
891 		WLAN_EID_SUPP_RATES,
892 		&ie_len,
893 		(pbss_network->ie_length - _BEACON_IE_OFFSET_)
894 	);
895 	if (p) {
896 		memcpy(supportRate, p + 2, ie_len);
897 		supportRateNum = ie_len;
898 	}
899 
900 	/* get ext_supported rates */
901 	p = rtw_get_ie(
902 		ie + _BEACON_IE_OFFSET_,
903 		WLAN_EID_EXT_SUPP_RATES,
904 		&ie_len,
905 		pbss_network->ie_length - _BEACON_IE_OFFSET_
906 	);
907 	if (p) {
908 		memcpy(supportRate + supportRateNum, p + 2, ie_len);
909 		supportRateNum += ie_len;
910 	}
911 
912 	network_type = rtw_check_network_type(supportRate, supportRateNum, channel);
913 
914 	rtw_set_supported_rate(pbss_network->supported_rates, network_type);
915 
916 	/* parsing ERP_IE */
917 	p = rtw_get_ie(
918 		ie + _BEACON_IE_OFFSET_,
919 		WLAN_EID_ERP_INFO,
920 		&ie_len,
921 		(pbss_network->ie_length - _BEACON_IE_OFFSET_)
922 	);
923 	if (p && ie_len > 0)
924 		ERP_IE_handler(padapter, (struct ndis_80211_var_ie *)p);
925 
926 	/* update privacy/security */
927 	if (cap & BIT(4))
928 		pbss_network->privacy = 1;
929 	else
930 		pbss_network->privacy = 0;
931 
932 	psecuritypriv->wpa_psk = 0;
933 
934 	/* wpa2 */
935 	group_cipher = 0; pairwise_cipher = 0;
936 	psecuritypriv->wpa2_group_cipher = _NO_PRIVACY_;
937 	psecuritypriv->wpa2_pairwise_cipher = _NO_PRIVACY_;
938 	p = rtw_get_ie(
939 		ie + _BEACON_IE_OFFSET_,
940 		WLAN_EID_RSN,
941 		&ie_len,
942 		(pbss_network->ie_length - _BEACON_IE_OFFSET_)
943 	);
944 	if (p && ie_len > 0) {
945 		if (rtw_parse_wpa2_ie(
946 			p,
947 			ie_len + 2,
948 			&group_cipher,
949 			&pairwise_cipher,
950 			NULL
951 		) == _SUCCESS) {
952 			psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
953 
954 			psecuritypriv->dot8021xalg = 1;/* psk,  todo:802.1x */
955 			psecuritypriv->wpa_psk |= BIT(1);
956 
957 			psecuritypriv->wpa2_group_cipher = group_cipher;
958 			psecuritypriv->wpa2_pairwise_cipher = pairwise_cipher;
959 		}
960 	}
961 
962 	/* wpa */
963 	ie_len = 0;
964 	group_cipher = 0; pairwise_cipher = 0;
965 	psecuritypriv->wpa_group_cipher = _NO_PRIVACY_;
966 	psecuritypriv->wpa_pairwise_cipher = _NO_PRIVACY_;
967 	for (p = ie + _BEACON_IE_OFFSET_; ; p += (ie_len + 2)) {
968 		p = rtw_get_ie(
969 			p,
970 			WLAN_EID_VENDOR_SPECIFIC,
971 			&ie_len,
972 			(pbss_network->ie_length - _BEACON_IE_OFFSET_ - (ie_len + 2))
973 		);
974 		if ((p) && (!memcmp(p + 2, OUI1, 4))) {
975 			if (rtw_parse_wpa_ie(
976 				p,
977 				ie_len + 2,
978 				&group_cipher,
979 				&pairwise_cipher,
980 				NULL
981 			) == _SUCCESS) {
982 				psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
983 
984 				psecuritypriv->dot8021xalg = 1;/* psk,  todo:802.1x */
985 
986 				psecuritypriv->wpa_psk |= BIT(0);
987 
988 				psecuritypriv->wpa_group_cipher = group_cipher;
989 				psecuritypriv->wpa_pairwise_cipher = pairwise_cipher;
990 			}
991 
992 			break;
993 		}
994 
995 		if (!p || ie_len == 0)
996 			break;
997 	}
998 
999 	/* wmm */
1000 	ie_len = 0;
1001 	pmlmepriv->qospriv.qos_option = 0;
1002 	if (pregistrypriv->wmm_enable) {
1003 		for (p = ie + _BEACON_IE_OFFSET_; ; p += (ie_len + 2)) {
1004 			p = rtw_get_ie(
1005 				p,
1006 				WLAN_EID_VENDOR_SPECIFIC,
1007 				&ie_len,
1008 				(pbss_network->ie_length - _BEACON_IE_OFFSET_ - (ie_len + 2))
1009 			);
1010 			if ((p) && !memcmp(p + 2, WMM_PARA_IE, 6)) {
1011 				pmlmepriv->qospriv.qos_option = 1;
1012 
1013 				*(p + 8) |= BIT(7);/* QoS Info, support U-APSD */
1014 
1015 				/* disable all ACM bits since the WMM admission */
1016 				/* control is not supported */
1017 				*(p + 10) &= ~BIT(4); /* BE */
1018 				*(p + 14) &= ~BIT(4); /* BK */
1019 				*(p + 18) &= ~BIT(4); /* VI */
1020 				*(p + 22) &= ~BIT(4); /* VO */
1021 
1022 				break;
1023 			}
1024 
1025 			if (!p || ie_len == 0)
1026 				break;
1027 		}
1028 	}
1029 
1030 	/* parsing HT_CAP_IE */
1031 	p = rtw_get_ie(
1032 		ie + _BEACON_IE_OFFSET_,
1033 		WLAN_EID_HT_CAPABILITY,
1034 		&ie_len,
1035 		(pbss_network->ie_length - _BEACON_IE_OFFSET_)
1036 	);
1037 	if (p && ie_len > 0) {
1038 		u8 max_rx_ampdu_factor = 0;
1039 		struct ieee80211_ht_cap *pht_cap = (struct ieee80211_ht_cap *)(p + 2);
1040 
1041 		pHT_caps_ie = p;
1042 
1043 		ht_cap = true;
1044 		network_type |= WIRELESS_11_24N;
1045 
1046 		rtw_ht_use_default_setting(padapter);
1047 
1048 		if (pmlmepriv->htpriv.sgi_20m == false)
1049 			pht_cap->cap_info &= cpu_to_le16(~(IEEE80211_HT_CAP_SGI_20));
1050 
1051 		if (pmlmepriv->htpriv.sgi_40m == false)
1052 			pht_cap->cap_info &= cpu_to_le16(~(IEEE80211_HT_CAP_SGI_40));
1053 
1054 		if (!TEST_FLAG(pmlmepriv->htpriv.ldpc_cap, LDPC_HT_ENABLE_RX))
1055 			pht_cap->cap_info &= cpu_to_le16(~(IEEE80211_HT_CAP_LDPC_CODING));
1056 
1057 		if (!TEST_FLAG(pmlmepriv->htpriv.stbc_cap, STBC_HT_ENABLE_TX))
1058 			pht_cap->cap_info &= cpu_to_le16(~(IEEE80211_HT_CAP_TX_STBC));
1059 
1060 		if (!TEST_FLAG(pmlmepriv->htpriv.stbc_cap, STBC_HT_ENABLE_RX))
1061 			pht_cap->cap_info &= cpu_to_le16(~(IEEE80211_HT_CAP_RX_STBC_3R));
1062 
1063 		pht_cap->ampdu_params_info &= ~(
1064 			IEEE80211_HT_CAP_AMPDU_FACTOR | IEEE80211_HT_CAP_AMPDU_DENSITY
1065 		);
1066 
1067 		if ((psecuritypriv->wpa_pairwise_cipher & WPA_CIPHER_CCMP) ||
1068 		    (psecuritypriv->wpa2_pairwise_cipher & WPA_CIPHER_CCMP)) {
1069 			pht_cap->ampdu_params_info |= (IEEE80211_HT_CAP_AMPDU_DENSITY &
1070 						       (0x07 << 2));
1071 		} else {
1072 			pht_cap->ampdu_params_info |= (IEEE80211_HT_CAP_AMPDU_DENSITY &
1073 						       0x00);
1074 		}
1075 
1076 		rtw_hal_get_def_var(
1077 			padapter,
1078 			HW_VAR_MAX_RX_AMPDU_FACTOR,
1079 			&max_rx_ampdu_factor
1080 		);
1081 		pht_cap->ampdu_params_info |= (
1082 			IEEE80211_HT_CAP_AMPDU_FACTOR & max_rx_ampdu_factor
1083 		); /* set  Max Rx AMPDU size  to 64K */
1084 
1085 		pht_cap->mcs.rx_mask[0] = 0xff;
1086 		pht_cap->mcs.rx_mask[1] = 0x0;
1087 
1088 		memcpy(&pmlmepriv->htpriv.ht_cap, p + 2, ie_len);
1089 	}
1090 
1091 	/* parsing HT_INFO_IE */
1092 	p = rtw_get_ie(
1093 		ie + _BEACON_IE_OFFSET_,
1094 		WLAN_EID_HT_OPERATION,
1095 		&ie_len,
1096 		(pbss_network->ie_length - _BEACON_IE_OFFSET_)
1097 	);
1098 	if (p && ie_len > 0)
1099 		pHT_info_ie = p;
1100 
1101 	switch (network_type) {
1102 	case WIRELESS_11B:
1103 		pbss_network->network_type_in_use = Ndis802_11DS;
1104 		break;
1105 	case WIRELESS_11G:
1106 	case WIRELESS_11BG:
1107 	case WIRELESS_11G_24N:
1108 	case WIRELESS_11BG_24N:
1109 		pbss_network->network_type_in_use = Ndis802_11OFDM24;
1110 		break;
1111 	default:
1112 		pbss_network->network_type_in_use = Ndis802_11OFDM24;
1113 		break;
1114 	}
1115 
1116 	pmlmepriv->cur_network.network_type = network_type;
1117 
1118 	pmlmepriv->htpriv.ht_option = false;
1119 
1120 	if ((psecuritypriv->wpa2_pairwise_cipher & WPA_CIPHER_TKIP) ||
1121 	    (psecuritypriv->wpa_pairwise_cipher & WPA_CIPHER_TKIP)) {
1122 		/* todo: */
1123 		/* ht_cap = false; */
1124 	}
1125 
1126 	/* ht_cap */
1127 	if (pregistrypriv->ht_enable && ht_cap) {
1128 		pmlmepriv->htpriv.ht_option = true;
1129 		pmlmepriv->qospriv.qos_option = 1;
1130 
1131 		if (pregistrypriv->ampdu_enable == 1)
1132 			pmlmepriv->htpriv.ampdu_enable = true;
1133 
1134 		HT_caps_handler(padapter, (struct ndis_80211_var_ie *)pHT_caps_ie);
1135 
1136 		HT_info_handler(padapter, (struct ndis_80211_var_ie *)pHT_info_ie);
1137 	}
1138 
1139 	pbss_network->length = get_wlan_bssid_ex_sz(
1140 		(struct wlan_bssid_ex  *)pbss_network
1141 	);
1142 
1143 	/* issue beacon to start bss network */
1144 	/* start_bss_network(padapter, (u8 *)pbss_network); */
1145 	rtw_startbss_cmd(padapter, RTW_CMDF_WAIT_ACK);
1146 
1147 	/* alloc sta_info for ap itself */
1148 	psta = rtw_get_stainfo(&padapter->stapriv, pbss_network->mac_address);
1149 	if (!psta) {
1150 		psta = rtw_alloc_stainfo(&padapter->stapriv, pbss_network->mac_address);
1151 		if (!psta)
1152 			return _FAIL;
1153 	}
1154 
1155 	/*  update AP's sta info */
1156 	update_ap_info(padapter, psta);
1157 
1158 	psta->state |= WIFI_AP_STATE;		/* Aries, add, fix bug of flush_cam_entry at STOP AP mode , 0724 */
1159 	rtw_indicate_connect(padapter);
1160 
1161 	pmlmepriv->cur_network.join_res = true;/* for check if already set beacon */
1162 
1163 	/* update bc/mc sta_info */
1164 	/* update_bmc_sta(padapter); */
1165 
1166 	return ret;
1167 }
1168 
rtw_set_macaddr_acl(struct adapter * padapter,int mode)1169 void rtw_set_macaddr_acl(struct adapter *padapter, int mode)
1170 {
1171 	struct sta_priv *pstapriv = &padapter->stapriv;
1172 	struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
1173 
1174 	pacl_list->mode = mode;
1175 }
1176 
rtw_acl_add_sta(struct adapter * padapter,u8 * addr)1177 int rtw_acl_add_sta(struct adapter *padapter, u8 *addr)
1178 {
1179 	struct list_head	*plist, *phead;
1180 	u8 added = false;
1181 	int i, ret = 0;
1182 	struct rtw_wlan_acl_node *paclnode;
1183 	struct sta_priv *pstapriv = &padapter->stapriv;
1184 	struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
1185 	struct __queue	*pacl_node_q = &pacl_list->acl_node_q;
1186 
1187 	if ((NUM_ACL - 1) < pacl_list->num)
1188 		return (-1);
1189 
1190 	spin_lock_bh(&(pacl_node_q->lock));
1191 
1192 	phead = get_list_head(pacl_node_q);
1193 	list_for_each(plist, phead) {
1194 		paclnode = list_entry(plist, struct rtw_wlan_acl_node, list);
1195 
1196 		if (!memcmp(paclnode->addr, addr, ETH_ALEN)) {
1197 			if (paclnode->valid == true) {
1198 				added = true;
1199 				break;
1200 			}
1201 		}
1202 	}
1203 
1204 	spin_unlock_bh(&(pacl_node_q->lock));
1205 
1206 	if (added)
1207 		return ret;
1208 
1209 	spin_lock_bh(&(pacl_node_q->lock));
1210 
1211 	for (i = 0; i < NUM_ACL; i++) {
1212 		paclnode = &pacl_list->aclnode[i];
1213 
1214 		if (!paclnode->valid) {
1215 			INIT_LIST_HEAD(&paclnode->list);
1216 
1217 			memcpy(paclnode->addr, addr, ETH_ALEN);
1218 
1219 			paclnode->valid = true;
1220 
1221 			list_add_tail(&paclnode->list, get_list_head(pacl_node_q));
1222 
1223 			pacl_list->num++;
1224 
1225 			break;
1226 		}
1227 	}
1228 
1229 	spin_unlock_bh(&(pacl_node_q->lock));
1230 
1231 	return ret;
1232 }
1233 
rtw_acl_remove_sta(struct adapter * padapter,u8 * addr)1234 void rtw_acl_remove_sta(struct adapter *padapter, u8 *addr)
1235 {
1236 	struct list_head *plist, *phead, *tmp;
1237 	struct rtw_wlan_acl_node *paclnode;
1238 	struct sta_priv *pstapriv = &padapter->stapriv;
1239 	struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
1240 	struct __queue	*pacl_node_q = &pacl_list->acl_node_q;
1241 	u8 baddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };	/* Baddr is used for clearing acl_list */
1242 
1243 	spin_lock_bh(&(pacl_node_q->lock));
1244 
1245 	phead = get_list_head(pacl_node_q);
1246 	list_for_each_safe(plist, tmp, phead) {
1247 		paclnode = list_entry(plist, struct rtw_wlan_acl_node, list);
1248 
1249 		if (
1250 			!memcmp(paclnode->addr, addr, ETH_ALEN) ||
1251 			!memcmp(baddr, addr, ETH_ALEN)
1252 		) {
1253 			if (paclnode->valid) {
1254 				paclnode->valid = false;
1255 
1256 				list_del_init(&paclnode->list);
1257 
1258 				pacl_list->num--;
1259 			}
1260 		}
1261 	}
1262 
1263 	spin_unlock_bh(&(pacl_node_q->lock));
1264 
1265 }
1266 
rtw_ap_set_pairwise_key(struct adapter * padapter,struct sta_info * psta)1267 u8 rtw_ap_set_pairwise_key(struct adapter *padapter, struct sta_info *psta)
1268 {
1269 	struct cmd_obj *ph2c;
1270 	struct set_stakey_parm	*psetstakey_para;
1271 	struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
1272 	u8 res = _SUCCESS;
1273 
1274 	ph2c = rtw_zmalloc(sizeof(struct cmd_obj));
1275 	if (!ph2c) {
1276 		res = _FAIL;
1277 		goto exit;
1278 	}
1279 
1280 	psetstakey_para = rtw_zmalloc(sizeof(struct set_stakey_parm));
1281 	if (!psetstakey_para) {
1282 		kfree(ph2c);
1283 		res = _FAIL;
1284 		goto exit;
1285 	}
1286 
1287 	init_h2fwcmd_w_parm_no_rsp(ph2c, psetstakey_para, _SetStaKey_CMD_);
1288 
1289 	psetstakey_para->algorithm = (u8)psta->dot118021XPrivacy;
1290 
1291 	memcpy(psetstakey_para->addr, psta->hwaddr, ETH_ALEN);
1292 
1293 	memcpy(psetstakey_para->key, &psta->dot118021x_UncstKey, 16);
1294 
1295 	res = rtw_enqueue_cmd(pcmdpriv, ph2c);
1296 
1297 exit:
1298 
1299 	return res;
1300 }
1301 
rtw_ap_set_key(struct adapter * padapter,u8 * key,u8 alg,int keyid,u8 set_tx)1302 static int rtw_ap_set_key(
1303 	struct adapter *padapter,
1304 	u8 *key,
1305 	u8 alg,
1306 	int keyid,
1307 	u8 set_tx
1308 )
1309 {
1310 	u8 keylen;
1311 	struct cmd_obj *pcmd;
1312 	struct setkey_parm *psetkeyparm;
1313 	struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
1314 	int res = _SUCCESS;
1315 
1316 	pcmd = rtw_zmalloc(sizeof(struct cmd_obj));
1317 	if (!pcmd) {
1318 		res = _FAIL;
1319 		goto exit;
1320 	}
1321 	psetkeyparm = rtw_zmalloc(sizeof(struct setkey_parm));
1322 	if (!psetkeyparm) {
1323 		kfree(pcmd);
1324 		res = _FAIL;
1325 		goto exit;
1326 	}
1327 
1328 	psetkeyparm->keyid = (u8)keyid;
1329 	if (is_wep_enc(alg))
1330 		padapter->securitypriv.key_mask |= BIT(psetkeyparm->keyid);
1331 
1332 	psetkeyparm->algorithm = alg;
1333 
1334 	psetkeyparm->set_tx = set_tx;
1335 
1336 	switch (alg) {
1337 	case _WEP40_:
1338 		keylen = 5;
1339 		break;
1340 	case _WEP104_:
1341 		keylen = 13;
1342 		break;
1343 	case _TKIP_:
1344 	case _TKIP_WTMIC_:
1345 	case _AES_:
1346 	default:
1347 		keylen = 16;
1348 	}
1349 
1350 	memcpy(&(psetkeyparm->key[0]), key, keylen);
1351 
1352 	pcmd->cmdcode = _SetKey_CMD_;
1353 	pcmd->parmbuf = (u8 *)psetkeyparm;
1354 	pcmd->cmdsz =  (sizeof(struct setkey_parm));
1355 	pcmd->rsp = NULL;
1356 	pcmd->rspsz = 0;
1357 
1358 	INIT_LIST_HEAD(&pcmd->list);
1359 
1360 	res = rtw_enqueue_cmd(pcmdpriv, pcmd);
1361 
1362 exit:
1363 
1364 	return res;
1365 }
1366 
rtw_ap_set_group_key(struct adapter * padapter,u8 * key,u8 alg,int keyid)1367 int rtw_ap_set_group_key(struct adapter *padapter, u8 *key, u8 alg, int keyid)
1368 {
1369 	return rtw_ap_set_key(padapter, key, alg, keyid, 1);
1370 }
1371 
rtw_ap_set_wep_key(struct adapter * padapter,u8 * key,u8 keylen,int keyid,u8 set_tx)1372 int rtw_ap_set_wep_key(
1373 	struct adapter *padapter,
1374 	u8 *key,
1375 	u8 keylen,
1376 	int keyid,
1377 	u8 set_tx
1378 )
1379 {
1380 	u8 alg;
1381 
1382 	switch (keylen) {
1383 	case 5:
1384 		alg = _WEP40_;
1385 		break;
1386 	case 13:
1387 		alg = _WEP104_;
1388 		break;
1389 	default:
1390 		alg = _NO_PRIVACY_;
1391 	}
1392 
1393 	return rtw_ap_set_key(padapter, key, alg, keyid, set_tx);
1394 }
1395 
update_bcn_fixed_ie(struct adapter * padapter)1396 static void update_bcn_fixed_ie(struct adapter *padapter)
1397 {
1398 }
1399 
update_bcn_erpinfo_ie(struct adapter * padapter)1400 static void update_bcn_erpinfo_ie(struct adapter *padapter)
1401 {
1402 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1403 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
1404 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1405 	struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
1406 	unsigned char *p, *ie = pnetwork->ies;
1407 	u32 len = 0;
1408 
1409 	if (!pmlmeinfo->ERP_enable)
1410 		return;
1411 
1412 	/* parsing ERP_IE */
1413 	p = rtw_get_ie(
1414 		ie + _BEACON_IE_OFFSET_,
1415 		WLAN_EID_ERP_INFO,
1416 		&len,
1417 		(pnetwork->ie_length - _BEACON_IE_OFFSET_)
1418 	);
1419 	if (p && len > 0) {
1420 		struct ndis_80211_var_ie *pIE = (struct ndis_80211_var_ie *)p;
1421 
1422 		if (pmlmepriv->num_sta_non_erp == 1)
1423 			pIE->data[0] |= RTW_ERP_INFO_NON_ERP_PRESENT | RTW_ERP_INFO_USE_PROTECTION;
1424 		else
1425 			pIE->data[0] &= ~(
1426 				RTW_ERP_INFO_NON_ERP_PRESENT | RTW_ERP_INFO_USE_PROTECTION
1427 			);
1428 
1429 		if (pmlmepriv->num_sta_no_short_preamble > 0)
1430 			pIE->data[0] |= RTW_ERP_INFO_BARKER_PREAMBLE_MODE;
1431 		else
1432 			pIE->data[0] &= ~(RTW_ERP_INFO_BARKER_PREAMBLE_MODE);
1433 
1434 		ERP_IE_handler(padapter, pIE);
1435 	}
1436 }
1437 
update_bcn_htcap_ie(struct adapter * padapter)1438 static void update_bcn_htcap_ie(struct adapter *padapter)
1439 {
1440 }
1441 
update_bcn_htinfo_ie(struct adapter * padapter)1442 static void update_bcn_htinfo_ie(struct adapter *padapter)
1443 {
1444 }
1445 
update_bcn_rsn_ie(struct adapter * padapter)1446 static void update_bcn_rsn_ie(struct adapter *padapter)
1447 {
1448 }
1449 
update_bcn_wpa_ie(struct adapter * padapter)1450 static void update_bcn_wpa_ie(struct adapter *padapter)
1451 {
1452 }
1453 
update_bcn_wmm_ie(struct adapter * padapter)1454 static void update_bcn_wmm_ie(struct adapter *padapter)
1455 {
1456 }
1457 
update_bcn_wps_ie(struct adapter * padapter)1458 static void update_bcn_wps_ie(struct adapter *padapter)
1459 {
1460 	u8 *pwps_ie = NULL;
1461 	u8 *pwps_ie_src;
1462 	u8 *premainder_ie;
1463 	u8 *pbackup_remainder_ie = NULL;
1464 
1465 	uint wps_ielen = 0, wps_offset, remainder_ielen;
1466 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1467 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
1468 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1469 	struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
1470 	unsigned char *ie = pnetwork->ies;
1471 	u32 ielen = pnetwork->ie_length;
1472 
1473 	pwps_ie = rtw_get_wps_ie(
1474 		ie + _FIXED_IE_LENGTH_,
1475 		ielen - _FIXED_IE_LENGTH_,
1476 		NULL,
1477 		&wps_ielen
1478 	);
1479 
1480 	if (!pwps_ie || wps_ielen == 0)
1481 		return;
1482 
1483 	pwps_ie_src = pmlmepriv->wps_beacon_ie;
1484 	if (!pwps_ie_src)
1485 		return;
1486 
1487 	wps_offset = (uint)(pwps_ie - ie);
1488 
1489 	premainder_ie = pwps_ie + wps_ielen;
1490 
1491 	remainder_ielen = ielen - wps_offset - wps_ielen;
1492 
1493 	if (remainder_ielen > 0) {
1494 		pbackup_remainder_ie = rtw_malloc(remainder_ielen);
1495 		if (pbackup_remainder_ie)
1496 			memcpy(pbackup_remainder_ie, premainder_ie, remainder_ielen);
1497 	}
1498 
1499 	wps_ielen = (uint)pwps_ie_src[1];/* to get ie data len */
1500 	if ((wps_offset + wps_ielen + 2 + remainder_ielen) <= MAX_IE_SZ) {
1501 		memcpy(pwps_ie, pwps_ie_src, wps_ielen + 2);
1502 		pwps_ie += (wps_ielen+2);
1503 
1504 		if (pbackup_remainder_ie)
1505 			memcpy(pwps_ie, pbackup_remainder_ie, remainder_ielen);
1506 
1507 		/* update ie_length */
1508 		pnetwork->ie_length = wps_offset + (wps_ielen + 2) + remainder_ielen;
1509 	}
1510 
1511 	kfree(pbackup_remainder_ie);
1512 }
1513 
update_bcn_p2p_ie(struct adapter * padapter)1514 static void update_bcn_p2p_ie(struct adapter *padapter)
1515 {
1516 }
1517 
update_bcn_vendor_spec_ie(struct adapter * padapter,u8 * oui)1518 static void update_bcn_vendor_spec_ie(struct adapter *padapter, u8 *oui)
1519 {
1520 	if (!memcmp(RTW_WPA_OUI, oui, 4))
1521 		update_bcn_wpa_ie(padapter);
1522 
1523 	else if (!memcmp(WMM_OUI, oui, 4))
1524 		update_bcn_wmm_ie(padapter);
1525 
1526 	else if (!memcmp(WPS_OUI, oui, 4))
1527 		update_bcn_wps_ie(padapter);
1528 
1529 	else if (!memcmp(P2P_OUI, oui, 4))
1530 		update_bcn_p2p_ie(padapter);
1531 }
1532 
update_beacon(struct adapter * padapter,u8 ie_id,u8 * oui,u8 tx)1533 void update_beacon(struct adapter *padapter, u8 ie_id, u8 *oui, u8 tx)
1534 {
1535 	struct mlme_priv *pmlmepriv;
1536 	struct mlme_ext_priv *pmlmeext;
1537 	/* struct mlme_ext_info *pmlmeinfo; */
1538 
1539 	if (!padapter)
1540 		return;
1541 
1542 	pmlmepriv = &(padapter->mlmepriv);
1543 	pmlmeext = &(padapter->mlmeextpriv);
1544 	/* pmlmeinfo = &(pmlmeext->mlmext_info); */
1545 
1546 	if (!pmlmeext->bstart_bss)
1547 		return;
1548 
1549 	spin_lock_bh(&pmlmepriv->bcn_update_lock);
1550 
1551 	switch (ie_id) {
1552 	case 0xFF:
1553 
1554 		update_bcn_fixed_ie(padapter);/* 8: TimeStamp, 2: Beacon Interval 2:Capability */
1555 
1556 		break;
1557 
1558 	case WLAN_EID_TIM:
1559 
1560 		update_BCNTIM(padapter);
1561 
1562 		break;
1563 
1564 	case WLAN_EID_ERP_INFO:
1565 
1566 		update_bcn_erpinfo_ie(padapter);
1567 
1568 		break;
1569 
1570 	case WLAN_EID_HT_CAPABILITY:
1571 
1572 		update_bcn_htcap_ie(padapter);
1573 
1574 		break;
1575 
1576 	case WLAN_EID_RSN:
1577 
1578 		update_bcn_rsn_ie(padapter);
1579 
1580 		break;
1581 
1582 	case WLAN_EID_HT_OPERATION:
1583 
1584 		update_bcn_htinfo_ie(padapter);
1585 
1586 		break;
1587 
1588 	case WLAN_EID_VENDOR_SPECIFIC:
1589 
1590 		update_bcn_vendor_spec_ie(padapter, oui);
1591 
1592 		break;
1593 
1594 	default:
1595 		break;
1596 	}
1597 
1598 	pmlmepriv->update_bcn = true;
1599 
1600 	spin_unlock_bh(&pmlmepriv->bcn_update_lock);
1601 
1602 	if (tx) {
1603 		/* send_beacon(padapter);//send_beacon must execute on TSR level */
1604 		set_tx_beacon_cmd(padapter);
1605 	}
1606 }
1607 
1608 /*
1609  * op_mode
1610  * Set to 0 (HT pure) under the following conditions
1611  *	  - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
1612  *	  - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
1613  * Set to 1 (HT non-member protection) if there may be non-HT STAs
1614  *	  in both the primary and the secondary channel
1615  * Set to 2 if only HT STAs are associated in BSS,
1616  *	  however and at least one 20 MHz HT STA is associated
1617  * Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
1618  *	  (currently non-GF HT station is considered as non-HT STA also)
1619  */
rtw_ht_operation_update(struct adapter * padapter)1620 static int rtw_ht_operation_update(struct adapter *padapter)
1621 {
1622 	u16 cur_op_mode, new_op_mode;
1623 	int op_mode_changes = 0;
1624 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1625 	struct ht_priv *phtpriv_ap = &pmlmepriv->htpriv;
1626 
1627 	if (pmlmepriv->htpriv.ht_option)
1628 		return 0;
1629 
1630 	if (!(pmlmepriv->ht_op_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT)
1631 	    && pmlmepriv->num_sta_ht_no_gf) {
1632 		pmlmepriv->ht_op_mode |=
1633 			IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT;
1634 		op_mode_changes++;
1635 	} else if ((pmlmepriv->ht_op_mode &
1636 		    IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT) &&
1637 		   pmlmepriv->num_sta_ht_no_gf == 0) {
1638 		pmlmepriv->ht_op_mode &=
1639 			~IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT;
1640 		op_mode_changes++;
1641 	}
1642 
1643 	if (!(pmlmepriv->ht_op_mode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT) &&
1644 	    (pmlmepriv->num_sta_no_ht || pmlmepriv->olbc_ht)) {
1645 		pmlmepriv->ht_op_mode |= IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
1646 		op_mode_changes++;
1647 	} else if ((pmlmepriv->ht_op_mode &
1648 		    IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT) &&
1649 		   (pmlmepriv->num_sta_no_ht == 0 && !pmlmepriv->olbc_ht)) {
1650 		pmlmepriv->ht_op_mode &=
1651 			~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
1652 		op_mode_changes++;
1653 	}
1654 
1655 	/* Note: currently we switch to the MIXED op mode if HT non-greenfield
1656 	 * station is associated. Probably it's a theoretical case, since
1657 	 * it looks like all known HT STAs support greenfield.
1658 	 */
1659 	new_op_mode = 0;
1660 	if (pmlmepriv->num_sta_no_ht ||
1661 	    (pmlmepriv->ht_op_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT))
1662 		new_op_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED;
1663 	else if (
1664 		(le16_to_cpu(phtpriv_ap->ht_cap.cap_info) & IEEE80211_HT_CAP_SUP_WIDTH)
1665 		&& pmlmepriv->num_sta_ht_20mhz)
1666 		new_op_mode = IEEE80211_HT_OP_MODE_PROTECTION_20MHZ;
1667 	else if (pmlmepriv->olbc_ht)
1668 		new_op_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER;
1669 	else
1670 		new_op_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
1671 
1672 	cur_op_mode = pmlmepriv->ht_op_mode & IEEE80211_HT_OP_MODE_PROTECTION;
1673 	if (cur_op_mode != new_op_mode) {
1674 		pmlmepriv->ht_op_mode &= ~IEEE80211_HT_OP_MODE_PROTECTION;
1675 		pmlmepriv->ht_op_mode |= new_op_mode;
1676 		op_mode_changes++;
1677 	}
1678 
1679 	return op_mode_changes;
1680 }
1681 
associated_clients_update(struct adapter * padapter,u8 updated)1682 void associated_clients_update(struct adapter *padapter, u8 updated)
1683 {
1684 	/* update associated stations cap. */
1685 	if (updated) {
1686 		struct list_head	*phead, *plist;
1687 		struct sta_info *psta = NULL;
1688 		struct sta_priv *pstapriv = &padapter->stapriv;
1689 
1690 		spin_lock_bh(&pstapriv->asoc_list_lock);
1691 
1692 		phead = &pstapriv->asoc_list;
1693 		/* check asoc_queue */
1694 		list_for_each(plist, phead) {
1695 			psta = list_entry(plist, struct sta_info, asoc_list);
1696 
1697 			VCS_update(padapter, psta);
1698 		}
1699 
1700 		spin_unlock_bh(&pstapriv->asoc_list_lock);
1701 	}
1702 }
1703 
1704 /* called > TSR LEVEL for USB or SDIO Interface*/
bss_cap_update_on_sta_join(struct adapter * padapter,struct sta_info * psta)1705 void bss_cap_update_on_sta_join(struct adapter *padapter, struct sta_info *psta)
1706 {
1707 	u8 beacon_updated = false;
1708 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1709 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
1710 
1711 	if (!(psta->flags & WLAN_STA_SHORT_PREAMBLE)) {
1712 		if (!psta->no_short_preamble_set) {
1713 			psta->no_short_preamble_set = 1;
1714 
1715 			pmlmepriv->num_sta_no_short_preamble++;
1716 
1717 			if ((pmlmeext->cur_wireless_mode > WIRELESS_11B) &&
1718 			    (pmlmepriv->num_sta_no_short_preamble == 1)) {
1719 				beacon_updated = true;
1720 				update_beacon(padapter, 0xFF, NULL, true);
1721 			}
1722 		}
1723 	} else {
1724 		if (psta->no_short_preamble_set) {
1725 			psta->no_short_preamble_set = 0;
1726 
1727 			pmlmepriv->num_sta_no_short_preamble--;
1728 
1729 			if ((pmlmeext->cur_wireless_mode > WIRELESS_11B) &&
1730 			    (pmlmepriv->num_sta_no_short_preamble == 0)) {
1731 				beacon_updated = true;
1732 				update_beacon(padapter, 0xFF, NULL, true);
1733 			}
1734 		}
1735 	}
1736 
1737 	if (psta->flags & WLAN_STA_NONERP) {
1738 		if (!psta->nonerp_set) {
1739 			psta->nonerp_set = 1;
1740 
1741 			pmlmepriv->num_sta_non_erp++;
1742 
1743 			if (pmlmepriv->num_sta_non_erp == 1) {
1744 				beacon_updated = true;
1745 				update_beacon(padapter, WLAN_EID_ERP_INFO, NULL, true);
1746 			}
1747 		}
1748 	} else {
1749 		if (psta->nonerp_set) {
1750 			psta->nonerp_set = 0;
1751 
1752 			pmlmepriv->num_sta_non_erp--;
1753 
1754 			if (pmlmepriv->num_sta_non_erp == 0) {
1755 				beacon_updated = true;
1756 				update_beacon(padapter, WLAN_EID_ERP_INFO, NULL, true);
1757 			}
1758 		}
1759 	}
1760 
1761 	if (!(psta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)) {
1762 		if (!psta->no_short_slot_time_set) {
1763 			psta->no_short_slot_time_set = 1;
1764 
1765 			pmlmepriv->num_sta_no_short_slot_time++;
1766 
1767 			if ((pmlmeext->cur_wireless_mode > WIRELESS_11B) &&
1768 			    (pmlmepriv->num_sta_no_short_slot_time == 1)) {
1769 				beacon_updated = true;
1770 				update_beacon(padapter, 0xFF, NULL, true);
1771 			}
1772 		}
1773 	} else {
1774 		if (psta->no_short_slot_time_set) {
1775 			psta->no_short_slot_time_set = 0;
1776 
1777 			pmlmepriv->num_sta_no_short_slot_time--;
1778 
1779 			if ((pmlmeext->cur_wireless_mode > WIRELESS_11B) &&
1780 			    (pmlmepriv->num_sta_no_short_slot_time == 0)) {
1781 				beacon_updated = true;
1782 				update_beacon(padapter, 0xFF, NULL, true);
1783 			}
1784 		}
1785 	}
1786 
1787 	if (psta->flags & WLAN_STA_HT) {
1788 		u16 ht_capab = le16_to_cpu(psta->htpriv.ht_cap.cap_info);
1789 
1790 		if (psta->no_ht_set) {
1791 			psta->no_ht_set = 0;
1792 			pmlmepriv->num_sta_no_ht--;
1793 		}
1794 
1795 		if ((ht_capab & IEEE80211_HT_CAP_GRN_FLD) == 0) {
1796 			if (!psta->no_ht_gf_set) {
1797 				psta->no_ht_gf_set = 1;
1798 				pmlmepriv->num_sta_ht_no_gf++;
1799 			}
1800 		}
1801 
1802 		if ((ht_capab & IEEE80211_HT_CAP_SUP_WIDTH) == 0) {
1803 			if (!psta->ht_20mhz_set) {
1804 				psta->ht_20mhz_set = 1;
1805 				pmlmepriv->num_sta_ht_20mhz++;
1806 			}
1807 		}
1808 
1809 	} else {
1810 		if (!psta->no_ht_set) {
1811 			psta->no_ht_set = 1;
1812 			pmlmepriv->num_sta_no_ht++;
1813 		}
1814 	}
1815 
1816 	if (rtw_ht_operation_update(padapter) > 0) {
1817 		update_beacon(padapter, WLAN_EID_HT_CAPABILITY, NULL, false);
1818 		update_beacon(padapter, WLAN_EID_HT_OPERATION, NULL, true);
1819 	}
1820 
1821 	/* update associated stations cap. */
1822 	associated_clients_update(padapter,  beacon_updated);
1823 }
1824 
bss_cap_update_on_sta_leave(struct adapter * padapter,struct sta_info * psta)1825 u8 bss_cap_update_on_sta_leave(struct adapter *padapter, struct sta_info *psta)
1826 {
1827 	u8 beacon_updated = false;
1828 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1829 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
1830 
1831 	if (!psta)
1832 		return beacon_updated;
1833 
1834 	if (psta->no_short_preamble_set) {
1835 		psta->no_short_preamble_set = 0;
1836 		pmlmepriv->num_sta_no_short_preamble--;
1837 		if (pmlmeext->cur_wireless_mode > WIRELESS_11B
1838 		    && pmlmepriv->num_sta_no_short_preamble == 0){
1839 			beacon_updated = true;
1840 			update_beacon(padapter, 0xFF, NULL, true);
1841 		}
1842 	}
1843 
1844 	if (psta->nonerp_set) {
1845 		psta->nonerp_set = 0;
1846 		pmlmepriv->num_sta_non_erp--;
1847 		if (pmlmepriv->num_sta_non_erp == 0) {
1848 			beacon_updated = true;
1849 			update_beacon(padapter, WLAN_EID_ERP_INFO, NULL, true);
1850 		}
1851 	}
1852 
1853 	if (psta->no_short_slot_time_set) {
1854 		psta->no_short_slot_time_set = 0;
1855 		pmlmepriv->num_sta_no_short_slot_time--;
1856 		if (pmlmeext->cur_wireless_mode > WIRELESS_11B
1857 		    && pmlmepriv->num_sta_no_short_slot_time == 0){
1858 			beacon_updated = true;
1859 			update_beacon(padapter, 0xFF, NULL, true);
1860 		}
1861 	}
1862 
1863 	if (psta->no_ht_gf_set) {
1864 		psta->no_ht_gf_set = 0;
1865 		pmlmepriv->num_sta_ht_no_gf--;
1866 	}
1867 
1868 	if (psta->no_ht_set) {
1869 		psta->no_ht_set = 0;
1870 		pmlmepriv->num_sta_no_ht--;
1871 	}
1872 
1873 	if (psta->ht_20mhz_set) {
1874 		psta->ht_20mhz_set = 0;
1875 		pmlmepriv->num_sta_ht_20mhz--;
1876 	}
1877 
1878 	if (rtw_ht_operation_update(padapter) > 0) {
1879 		update_beacon(padapter, WLAN_EID_HT_CAPABILITY, NULL, false);
1880 		update_beacon(padapter, WLAN_EID_HT_OPERATION, NULL, true);
1881 	}
1882 
1883 	return beacon_updated;
1884 }
1885 
ap_free_sta(struct adapter * padapter,struct sta_info * psta,bool active,u16 reason)1886 u8 ap_free_sta(
1887 	struct adapter *padapter,
1888 	struct sta_info *psta,
1889 	bool active,
1890 	u16 reason
1891 )
1892 {
1893 	u8 beacon_updated = false;
1894 
1895 	if (!psta)
1896 		return beacon_updated;
1897 
1898 	if (active) {
1899 		/* tear down Rx AMPDU */
1900 		send_delba(padapter, 0, psta->hwaddr);/*  recipient */
1901 
1902 		/* tear down TX AMPDU */
1903 		send_delba(padapter, 1, psta->hwaddr);/*  // originator */
1904 
1905 		issue_deauth(padapter, psta->hwaddr, reason);
1906 	}
1907 
1908 	psta->htpriv.agg_enable_bitmap = 0x0;/* reset */
1909 	psta->htpriv.candidate_tid_bitmap = 0x0;/* reset */
1910 
1911 	/* report_del_sta_event(padapter, psta->hwaddr, reason); */
1912 
1913 	/* clear cam entry / key */
1914 	rtw_clearstakey_cmd(padapter, psta, true);
1915 
1916 	spin_lock_bh(&psta->lock);
1917 	psta->state &= ~_FW_LINKED;
1918 	spin_unlock_bh(&psta->lock);
1919 
1920 	rtw_cfg80211_indicate_sta_disassoc(padapter, psta->hwaddr, reason);
1921 
1922 	report_del_sta_event(padapter, psta->hwaddr, reason);
1923 
1924 	beacon_updated = bss_cap_update_on_sta_leave(padapter, psta);
1925 
1926 	rtw_free_stainfo(padapter, psta);
1927 
1928 	return beacon_updated;
1929 }
1930 
rtw_sta_flush(struct adapter * padapter)1931 void rtw_sta_flush(struct adapter *padapter)
1932 {
1933 	struct list_head *phead, *plist, *tmp;
1934 	struct sta_info *psta = NULL;
1935 	struct sta_priv *pstapriv = &padapter->stapriv;
1936 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1937 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1938 	u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1939 
1940 	if ((pmlmeinfo->state & 0x03) != WIFI_FW_AP_STATE)
1941 		return;
1942 
1943 	spin_lock_bh(&pstapriv->asoc_list_lock);
1944 	phead = &pstapriv->asoc_list;
1945 	/* free sta asoc_queue */
1946 	list_for_each_safe(plist, tmp, phead) {
1947 		psta = list_entry(plist, struct sta_info, asoc_list);
1948 
1949 		list_del_init(&psta->asoc_list);
1950 		pstapriv->asoc_list_cnt--;
1951 
1952 		/* spin_unlock_bh(&pstapriv->asoc_list_lock); */
1953 		ap_free_sta(padapter, psta, true, WLAN_REASON_DEAUTH_LEAVING);
1954 		/* spin_lock_bh(&pstapriv->asoc_list_lock); */
1955 	}
1956 	spin_unlock_bh(&pstapriv->asoc_list_lock);
1957 
1958 	issue_deauth(padapter, bc_addr, WLAN_REASON_DEAUTH_LEAVING);
1959 
1960 	associated_clients_update(padapter, true);
1961 }
1962 
1963 /* called > TSR LEVEL for USB or SDIO Interface*/
sta_info_update(struct adapter * padapter,struct sta_info * psta)1964 void sta_info_update(struct adapter *padapter, struct sta_info *psta)
1965 {
1966 	int flags = psta->flags;
1967 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1968 
1969 	/* update wmm cap. */
1970 	if (WLAN_STA_WME & flags)
1971 		psta->qos_option = 1;
1972 	else
1973 		psta->qos_option = 0;
1974 
1975 	if (pmlmepriv->qospriv.qos_option == 0)
1976 		psta->qos_option = 0;
1977 
1978 	/* update 802.11n ht cap. */
1979 	if (WLAN_STA_HT & flags) {
1980 		psta->htpriv.ht_option = true;
1981 		psta->qos_option = 1;
1982 	} else {
1983 		psta->htpriv.ht_option = false;
1984 	}
1985 
1986 	if (!pmlmepriv->htpriv.ht_option)
1987 		psta->htpriv.ht_option = false;
1988 
1989 	update_sta_info_apmode(padapter, psta);
1990 }
1991 
1992 /* called >= TSR LEVEL for USB or SDIO Interface*/
ap_sta_info_defer_update(struct adapter * padapter,struct sta_info * psta)1993 void ap_sta_info_defer_update(struct adapter *padapter, struct sta_info *psta)
1994 {
1995 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1996 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1997 
1998 	if (psta->state & _FW_LINKED) {
1999 		pmlmeinfo->FW_sta_info[psta->mac_id].psta = psta;
2000 
2001 		/* add ratid */
2002 		add_RATid(padapter, psta, 0);/* DM_RATR_STA_INIT */
2003 	}
2004 }
2005 /* restore hw setting from sw data structures */
rtw_ap_restore_network(struct adapter * padapter)2006 void rtw_ap_restore_network(struct adapter *padapter)
2007 {
2008 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
2009 	struct sta_priv *pstapriv = &padapter->stapriv;
2010 	struct sta_info *psta;
2011 	struct security_priv *psecuritypriv = &(padapter->securitypriv);
2012 	struct list_head	*phead, *plist;
2013 	u8 chk_alive_num = 0;
2014 	char chk_alive_list[NUM_STA];
2015 	int i;
2016 
2017 	rtw_setopmode_cmd(padapter, Ndis802_11APMode, false);
2018 
2019 	set_channel_bwmode(
2020 		padapter,
2021 		pmlmeext->cur_channel,
2022 		pmlmeext->cur_ch_offset,
2023 		pmlmeext->cur_bwmode
2024 	);
2025 
2026 	start_bss_network(padapter);
2027 
2028 	if ((padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_) ||
2029 	    (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)) {
2030 		/* restore group key, WEP keys is restored in ips_leave() */
2031 		rtw_set_key(
2032 			padapter,
2033 			psecuritypriv,
2034 			psecuritypriv->dot118021XGrpKeyid,
2035 			0,
2036 			false
2037 		);
2038 	}
2039 
2040 	spin_lock_bh(&pstapriv->asoc_list_lock);
2041 
2042 	phead = &pstapriv->asoc_list;
2043 	list_for_each(plist, phead) {
2044 		int stainfo_offset;
2045 
2046 		psta = list_entry(plist, struct sta_info, asoc_list);
2047 
2048 		stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
2049 		if (stainfo_offset_valid(stainfo_offset))
2050 			chk_alive_list[chk_alive_num++] = stainfo_offset;
2051 	}
2052 
2053 	spin_unlock_bh(&pstapriv->asoc_list_lock);
2054 
2055 	for (i = 0; i < chk_alive_num; i++) {
2056 		psta = rtw_get_stainfo_by_offset(pstapriv, chk_alive_list[i]);
2057 
2058 		if (!psta)
2059 			continue;
2060 
2061 		if (psta->state & _FW_LINKED) {
2062 			rtw_sta_media_status_rpt(padapter, psta, 1);
2063 			Update_RA_Entry(padapter, psta);
2064 			/* pairwise key */
2065 			/* per sta pairwise key and settings */
2066 			if ((psecuritypriv->dot11PrivacyAlgrthm == _TKIP_) ||
2067 			    (psecuritypriv->dot11PrivacyAlgrthm == _AES_)) {
2068 				rtw_setstakey_cmd(padapter, psta, true, false);
2069 			}
2070 		}
2071 	}
2072 }
2073 
start_ap_mode(struct adapter * padapter)2074 void start_ap_mode(struct adapter *padapter)
2075 {
2076 	int i;
2077 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2078 	struct sta_priv *pstapriv = &padapter->stapriv;
2079 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
2080 	struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
2081 
2082 	pmlmepriv->update_bcn = false;
2083 
2084 	/* init_mlme_ap_info(padapter); */
2085 	pmlmeext->bstart_bss = false;
2086 
2087 	pmlmepriv->num_sta_non_erp = 0;
2088 
2089 	pmlmepriv->num_sta_no_short_slot_time = 0;
2090 
2091 	pmlmepriv->num_sta_no_short_preamble = 0;
2092 
2093 	pmlmepriv->num_sta_ht_no_gf = 0;
2094 	pmlmepriv->num_sta_no_ht = 0;
2095 	pmlmepriv->num_sta_ht_20mhz = 0;
2096 
2097 	pmlmepriv->olbc = false;
2098 
2099 	pmlmepriv->olbc_ht = false;
2100 
2101 	pmlmepriv->ht_op_mode = 0;
2102 
2103 	for (i = 0; i < NUM_STA; i++)
2104 		pstapriv->sta_aid[i] = NULL;
2105 
2106 	pmlmepriv->wps_beacon_ie = NULL;
2107 	pmlmepriv->wps_probe_resp_ie = NULL;
2108 	pmlmepriv->wps_assoc_resp_ie = NULL;
2109 
2110 	pmlmepriv->p2p_beacon_ie = NULL;
2111 	pmlmepriv->p2p_probe_resp_ie = NULL;
2112 
2113 	/* for ACL */
2114 	INIT_LIST_HEAD(&(pacl_list->acl_node_q.queue));
2115 	pacl_list->num = 0;
2116 	pacl_list->mode = 0;
2117 	for (i = 0; i < NUM_ACL; i++) {
2118 		INIT_LIST_HEAD(&pacl_list->aclnode[i].list);
2119 		pacl_list->aclnode[i].valid = false;
2120 	}
2121 }
2122 
stop_ap_mode(struct adapter * padapter)2123 void stop_ap_mode(struct adapter *padapter)
2124 {
2125 	struct list_head *phead, *plist, *tmp;
2126 	struct rtw_wlan_acl_node *paclnode;
2127 	struct sta_info *psta = NULL;
2128 	struct sta_priv *pstapriv = &padapter->stapriv;
2129 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2130 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
2131 	struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
2132 	struct __queue	*pacl_node_q = &pacl_list->acl_node_q;
2133 
2134 	pmlmepriv->update_bcn = false;
2135 	pmlmeext->bstart_bss = false;
2136 
2137 	/* reset and init security priv , this can refine with rtw_reset_securitypriv */
2138 	memset(
2139 		(unsigned char *)&padapter->securitypriv,
2140 		0,
2141 		sizeof(struct security_priv)
2142 	);
2143 	padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeOpen;
2144 	padapter->securitypriv.ndisencryptstatus = Ndis802_11WEPDisabled;
2145 
2146 	/* for ACL */
2147 	spin_lock_bh(&(pacl_node_q->lock));
2148 	phead = get_list_head(pacl_node_q);
2149 	list_for_each_safe(plist, tmp, phead) {
2150 		paclnode = list_entry(plist, struct rtw_wlan_acl_node, list);
2151 
2152 		if (paclnode->valid) {
2153 			paclnode->valid = false;
2154 
2155 			list_del_init(&paclnode->list);
2156 
2157 			pacl_list->num--;
2158 		}
2159 	}
2160 	spin_unlock_bh(&(pacl_node_q->lock));
2161 
2162 	rtw_sta_flush(padapter);
2163 
2164 	/* free_assoc_sta_resources */
2165 	rtw_free_all_stainfo(padapter);
2166 
2167 	psta = rtw_get_bcmc_stainfo(padapter);
2168 	rtw_free_stainfo(padapter, psta);
2169 
2170 	rtw_init_bcmc_stainfo(padapter);
2171 
2172 	rtw_free_mlme_priv_ie_data(pmlmepriv);
2173 
2174 	rtw_btcoex_MediaStatusNotify(padapter, 0); /* disconnect */
2175 }
2176