1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
7 #include <osdep_service.h>
8 #include <mlme_osdep.h>
10 #include <wlan_bssdef.h>
12 #define MAX_BSS_CNT 128
13 #define MAX_JOIN_TIMEOUT 6500
15 /* Increase the scanning timeout because of increasing the SURVEY_TO value. */
17 #define SCANNING_TIMEOUT 8000
19 #define SCAN_INTERVAL (30) /* unit:2sec, 30*2=60sec */
21 #define SCANQUEUE_LIFETIME 20 /* unit:sec */
23 #define WIFI_NULL_STATE 0x00000000
25 #define WIFI_ASOC_STATE 0x00000001 /* Under Linked state */
26 #define WIFI_REASOC_STATE 0x00000002
27 #define WIFI_SLEEP_STATE 0x00000004
28 #define WIFI_STATION_STATE 0x00000008
30 #define WIFI_AP_STATE 0x00000010
31 #define WIFI_ADHOC_STATE 0x00000020
32 #define WIFI_ADHOC_MASTER_STATE 0x00000040
33 #define WIFI_UNDER_LINKING 0x00000080
35 #define WIFI_UNDER_WPS 0x00000100
36 #define WIFI_STA_ALIVE_CHK_STATE 0x00000400
37 #define WIFI_SITE_MONITOR 0x00000800 /* to indicate the station is under site surveying */
39 #define WIFI_MP_STATE 0x00010000
40 #define WIFI_MP_CTX_BACKGROUND 0x00020000 /* in continuous tx background */
41 #define WIFI_MP_CTX_ST 0x00040000 /* in continuous tx with single-tone */
42 #define WIFI_MP_CTX_BACKGROUND_PENDING 0x00080000 /* pending in continuous tx background due to out of skb */
43 #define WIFI_MP_CTX_CCK_HW 0x00100000 /* in continuous tx */
44 #define WIFI_MP_CTX_CCK_CS 0x00200000 /* in continuous tx with carrier suppression */
45 #define WIFI_MP_LPBK_STATE 0x00400000
47 #define _FW_UNDER_LINKING WIFI_UNDER_LINKING
48 #define _FW_LINKED WIFI_ASOC_STATE
49 #define _FW_UNDER_SURVEY WIFI_SITE_MONITOR
51 enum dot11AuthAlgrthmNum {
52 dot11AuthAlgrthm_Open = 0,
53 dot11AuthAlgrthm_Shared,
54 dot11AuthAlgrthm_8021X,
55 dot11AuthAlgrthm_Auto,
56 dot11AuthAlgrthm_WAPI,
57 dot11AuthAlgrthm_MaxNum
60 /* Scan type including active and passive scan. */
67 enum SCAN_RESULT_TYPE {
68 SCAN_RESULT_P2P_ONLY = 0, /* Will return all the P2P devices. */
69 SCAN_RESULT_ALL = 1, /* Will return all the scanned device,
71 SCAN_RESULT_WFD_TYPE = 2 /* Will just return the correct WFD
73 /* If this device is Miracast sink
74 * device, it will just return all the
75 * Miracast source devices. */
79 there are several "locks" in mlme_priv,
80 since mlme_priv is a shared resource between many threads,
81 like ISR/Call-Back functions, the OID handlers, and even timer functions.
83 Each _queue has its own locks, already.
84 Other items are protected by mlme_priv.lock.
86 To avoid possible dead lock, any thread trying to modifiying mlme_priv
87 SHALL not lock up more than one lock at a time!
90 #define traffic_threshold 10
91 #define traffic_scan_period 500
93 struct sitesurvey_ctrl {
97 struct timer_list sitesurvey_ctrl_timer;
100 struct rt_link_detect {
103 u32 NumRxUnicastOkInPeriod;
107 bool bHigherBusyTraffic; /* For interrupt migration purpose. */
108 bool bHigherBusyRxTraffic; /* We may disable Tx interrupt according
110 bool bHigherBusyTxTraffic; /* We may disable Tx interrupt according
114 struct profile_info {
116 u8 ssid[ WLAN_SSID_MAXLEN ];
117 u8 peermac[ ETH_ALEN ];
120 struct tx_invite_req_info {
123 u8 go_ssid[ WLAN_SSID_MAXLEN ];
125 u8 go_bssid[ ETH_ALEN ];
126 u8 peer_macaddr[ ETH_ALEN ];
127 u8 operating_ch; /* This information will be set by using the
129 u8 peer_ch; /* The listen channel for peer P2P device */
132 struct tx_invite_resp_info {
133 u8 token; /* Used to record the dialog token of p2p invitation
137 struct tx_provdisc_req_info {
138 u16 wps_config_method_request; /* Used when sending the
139 * provisioning request frame*/
140 u16 peer_channel_num[2]; /* The channel number which the
141 * receiver stands. */
142 struct ndis_802_11_ssid ssid;
143 u8 peerDevAddr[ETH_ALEN]; /* Peer device address */
144 u8 peerIFAddr[ETH_ALEN]; /* Peer interface address */
145 u8 benable; /* This provision discovery
146 * request frame is trigger
150 /* When peer device issue prov_disc_req first, we should store the following
152 /* The UI must know this information to know which config method the
153 * remote p2p device needs. */
154 struct rx_provdisc_req_info {
155 u8 peerDevAddr[ETH_ALEN]; /* Peer device address */
156 u8 strconfig_method_desc_of_prov_disc_req[4]; /* description
157 * for the config method located in the provisioning
158 * discovery request frame. */
161 struct tx_nego_req_info {
162 u16 peer_channel_num[2]; /* The channel number. */
163 u8 peerDevAddr[ETH_ALEN]; /* Peer device address */
164 u8 benable; /* This negotiation request frame is
165 * trigger to send or not */
168 struct group_id_info {
169 u8 go_device_addr[ ETH_ALEN ]; /* The GO's device address of
171 u8 ssid[ WLAN_SSID_MAXLEN ]; /* The SSID of this P2P group */
174 struct scan_limit_info {
175 u8 scan_op_ch_only; /* When this flag is set, the driver
176 * should only scan the op. channel */
177 u8 operation_ch[2]; /* Store the op. chan of invitation */
180 struct wifidirect_info {
181 struct adapter *padapter;
182 struct timer_list find_phase_timer;
183 struct timer_list restore_p2p_state_timer;
185 /* Used to do the scanning. After confirming the peer is availalble,
186 * the driver transmits the P2P frame to peer. */
187 struct timer_list pre_tx_scan_timer;
188 struct timer_list reset_ch_sitesurvey;
189 struct timer_list reset_ch_sitesurvey2; /* Just for resetting the scan
190 * limit function by using p2p nego */
191 struct tx_provdisc_req_info tx_prov_disc_info;
192 struct rx_provdisc_req_info rx_prov_disc_info;
193 struct tx_invite_req_info invitereq_info;
194 /* Store the profile information of persistent group */
195 struct profile_info profileinfo[P2P_MAX_PERSISTENT_GROUP_NUM];
196 struct tx_invite_resp_info inviteresp_info;
197 struct tx_nego_req_info nego_req_info;
198 /* Store the group id info when doing the group negot handshake. */
199 struct group_id_info groupid_info;
200 /* Used for get the limit scan channel from the Invitation procedure */
201 struct scan_limit_info rx_invitereq_info;
202 /* Used for get the limit scan chan from the P2P negotiation handshake*/
203 struct scan_limit_info p2p_info;
205 enum P2P_STATE pre_p2p_state;
206 enum P2P_STATE p2p_state;
207 /* The device address should be the mac address of this device. */
208 u8 device_addr[ETH_ALEN];
209 u8 interface_addr[ETH_ALEN];
212 u8 operating_channel;
213 u8 listen_dwell; /* This value should be between 1 and 3 */
215 u8 p2p_wildcard_ssid[P2P_WILDCARD_SSID_LEN];
216 u8 intent; /* should only include the intent value. */
217 u8 p2p_peer_interface_addr[ETH_ALEN];
218 u8 p2p_peer_device_addr[ETH_ALEN];
219 u8 peer_intent; /* Included the intent value and tie breaker value. */
220 /* Device name for displaying on searching device screen */
221 u8 device_name[WPS_MAX_DEVICE_NAME_LEN];
223 u8 profileindex; /* Used to point to the index of profileinfo array */
224 u8 peer_operating_ch;
225 u8 find_phase_state_exchange_cnt;
226 /* The device password ID for group negotiation */
227 u16 device_password_id_for_nego;
228 u8 negotiation_dialog_token;
229 /* SSID information for group negotitation */
230 u8 nego_ssid[WLAN_SSID_MAXLEN];
232 u8 p2p_group_ssid[WLAN_SSID_MAXLEN];
233 u8 p2p_group_ssid_len;
234 /* Flag to know if the persistent function should be supported or not.*/
235 u8 persistent_supported;
236 /* In the Sigma test, the Sigma will provide this enable from the
237 * sta_set_p2p CAPI. */
240 u8 session_available; /* Flag to set the WFD session available to
241 * enable or disable "by Sigma" */
242 /* In the Sigma test, the Sigma will disable the session available
243 * by using the sta_preset CAPI. */
246 u8 wfd_tdls_enable; /* Flag to enable or disable the TDLS by WFD Sigma*/
249 u8 wfd_tdls_weaksec; /* Flag to enable or disable the weak security
250 * function for TDLS by WFD Sigma */
252 /* In this case, the driver can't issue the tdsl
253 * setup request frame. */
255 /* In this case, the driver can issue the tdls
256 * setup request frame */
257 /* even the current security is weak security. */
259 /* This field will store the WPS value (PIN value or PBC) that UI had
260 * got from the user. */
261 enum P2P_WPSINFO ui_got_wps_info;
262 u16 supported_wps_cm; /* This field describes the WPS config method
263 * which this driver supported. */
264 /* The value should be the combination of config
265 * method defined in page104 of WPS v2.0 spec.*/
266 /* This field will contain the length of body of P2P Channel List
267 * attribute of group negotiation response frame. */
268 uint channel_list_attr_len;
269 /* This field will contain the body of P2P Channel List attribute of
270 * group negotitation response frame. */
271 /* We will use the channel_cnt and channel_list fields when constructing
272 * the group negotiation confirm frame. */
273 u8 channel_list_attr[100];
274 enum P2P_PS_MODE p2p_ps_mode; /* indicate p2p ps mode */
275 enum P2P_PS_STATE p2p_ps_state; /* indicate p2p ps state */
276 u8 noa_index; /* Identifies and instance of Notice of Absence timing. */
277 u8 ctwindow; /* Client traffic window. A period of time in TU after TBTT. */
278 u8 opp_ps; /* opportunistic power save. */
279 u8 noa_num; /* number of NoA descriptor in P2P IE. */
280 u8 noa_count[P2P_MAX_NOA_NUM]; /* Count for owner, Type of client. */
281 /* Max duration for owner, preferred or min acceptable duration for
283 u32 noa_duration[P2P_MAX_NOA_NUM];
284 /* Length of interval for owner, preferred or max acceptable interval
286 u32 noa_interval[P2P_MAX_NOA_NUM];
287 /* schedule expressed in terms of the lower 4 bytes of the TSF timer. */
288 u32 noa_start_time[P2P_MAX_NOA_NUM];
291 struct tdls_ss_record { /* signal strength record */
292 u8 macaddr[ETH_ALEN];
294 u8 is_tdls_sta; /* true: direct link sta, false: else */
301 u8 sta_maximum; /* 1:tdls sta is equal (NUM_STA-1), reach max direct link number; 0: else; */
302 struct tdls_ss_record ss_record;
303 u8 macid_index; /* macid entry that is ready to write */
304 u8 clear_cam; /* cam entry that is trying to clear, using it in direct link teardown */
308 u8 collect_pkt_num[MAX_CHANNEL_NUM];
312 u8 dev_discovered; /* WFD_TDLS: for sigma test */
318 int fw_state; /* shall we protect this variable? maybe not necessarily... */
320 u8 to_join; /* flag */
321 u8 to_roaming; /* roaming trying times */
326 struct list_head *pscanned;
327 struct __queue free_bss_pool;
328 struct __queue scanned_queue;
330 u8 key_mask; /* use to restore wep key after hal_init */
333 struct ndis_802_11_ssid assoc_ssid;
336 struct wlan_network cur_network;
337 struct wlan_network *cur_network_scanned;
341 struct timer_list assoc_timer;
346 struct timer_list scan_to_timer; /* driver itself handles scan_timeout status. */
347 u32 scan_start_time; /* used to evaluate the time spent in scanning */
349 struct qos_priv qospriv;
351 /* Number of non-HT AP/stations */
354 /* Number of HT AP/stations 20 MHz */
355 /* int num_sta_ht_20mhz; */
357 int num_FortyMHzIntolerant;
358 struct ht_priv htpriv;
359 struct rt_link_detect LinkDetectInfo;
360 struct timer_list dynamic_chk_timer; /* dynamic/periodic check timer */
362 u8 acm_mask; /* for wmm acm mask */
364 enum rt_scan_type scan_mode; /* active: 1, passive: 0 */
366 /* u8 probereq_wpsie[MAX_WPS_IE_LEN];added in probe req */
367 /* int probereq_wpsie_len; */
368 u8 *wps_probe_req_ie;
369 u32 wps_probe_req_ie_len;
376 #if defined (CONFIG_88EU_AP_MODE)
377 /* Number of associated Non-ERP stations (i.e., stations using 802.11b
381 /* Number of associated stations that do not support Short Slot Time */
382 int num_sta_no_short_slot_time;
384 /* Number of associated stations that do not support Short Preamble */
385 int num_sta_no_short_preamble;
387 int olbc; /* Overlapping Legacy BSS Condition */
389 /* Number of HT assoc sta that do not support greenfield */
390 int num_sta_ht_no_gf;
392 /* Number of associated non-HT stations */
393 /* int num_sta_no_ht; */
395 /* Number of HT associated stations 20 MHz */
396 int num_sta_ht_20mhz;
398 /* Overlapping BSS information */
404 /* u8 *wps_probe_req_ie; */
405 u8 *wps_probe_resp_ie;
406 u8 *wps_assoc_resp_ie;
408 u32 wps_beacon_ie_len;
409 u32 wps_probe_resp_ie_len;
410 u32 wps_assoc_resp_ie_len;
413 u8 *p2p_probe_req_ie;
414 u8 *p2p_probe_resp_ie;
415 u8 *p2p_go_probe_resp_ie; /* for GO */
416 u8 *p2p_assoc_req_ie;
418 u32 p2p_beacon_ie_len;
419 u32 p2p_probe_req_ie_len;
420 u32 p2p_probe_resp_ie_len;
421 u32 p2p_go_probe_resp_ie_len; /* for GO */
422 u32 p2p_assoc_req_ie_len;
423 spinlock_t bcn_update_lock;
425 #endif /* if defined (CONFIG_88EU_AP_MODE) */
428 #ifdef CONFIG_88EU_AP_MODE
430 struct hostapd_priv {
431 struct adapter *padapter;
434 int hostapd_mode_init(struct adapter *padapter);
435 void hostapd_mode_unload(struct adapter *padapter);
438 extern unsigned char WPA_TKIP_CIPHER[4];
439 extern unsigned char RSN_TKIP_CIPHER[4];
440 extern unsigned char REALTEK_96B_IE[];
441 extern unsigned char MCS_rate_2R[16];
442 extern unsigned char MCS_rate_1R[16];
444 void rtw_joinbss_event_prehandle(struct adapter *adapter, u8 *pbuf);
445 void rtw_survey_event_callback(struct adapter *adapter, u8 *pbuf);
446 void rtw_surveydone_event_callback(struct adapter *adapter, u8 *pbuf);
447 void rtw_joinbss_event_callback(struct adapter *adapter, u8 *pbuf);
448 void rtw_stassoc_event_callback(struct adapter *adapter, u8 *pbuf);
449 void rtw_stadel_event_callback(struct adapter *adapter, u8 *pbuf);
450 void rtw_atimdone_event_callback(struct adapter *adapter, u8 *pbuf);
451 void rtw_cpwm_event_callback(struct adapter *adapter, u8 *pbuf);
452 void indicate_wx_scan_complete_event(struct adapter *padapter);
453 void rtw_indicate_wx_assoc_event(struct adapter *padapter);
454 void rtw_indicate_wx_disassoc_event(struct adapter *padapter);
455 int event_thread(void *context);
456 void rtw_join_timeout_handler (struct timer_list *t);
457 void _rtw_scan_timeout_handler (struct timer_list *t);
458 void rtw_free_network_queue(struct adapter *adapter, u8 isfreeall);
459 int rtw_init_mlme_priv(struct adapter *adapter);
460 void rtw_free_mlme_priv (struct mlme_priv *pmlmepriv);
461 int rtw_select_and_join_from_scanned_queue(struct mlme_priv *pmlmepriv);
462 int rtw_set_key(struct adapter *adapter, struct security_priv *psecuritypriv,
463 int keyid, u8 set_tx);
464 int rtw_set_auth(struct adapter *adapter, struct security_priv *psecuritypriv);
466 static inline u8 *get_bssid(struct mlme_priv *pmlmepriv)
467 { /* if sta_mode:pmlmepriv->cur_network.network.MacAddress=> bssid */
468 /* if adhoc_mode:pmlmepriv->cur_network.network.MacAddress=> ibss mac address */
469 return pmlmepriv->cur_network.network.MacAddress;
472 static inline int check_fwstate(struct mlme_priv *pmlmepriv, int state)
474 if (pmlmepriv->fw_state & state)
480 static inline int get_fwstate(struct mlme_priv *pmlmepriv)
482 return pmlmepriv->fw_state;
486 * No Limit on the calling context,
487 * therefore set it to be the critical section...
489 * ### NOTE:#### (!!!!)
490 * MUST TAKE CARE THAT BEFORE CALLING THIS FUNC, YOU SHOULD HAVE LOCKED pmlmepriv->lock
492 static inline void set_fwstate(struct mlme_priv *pmlmepriv, int state)
494 pmlmepriv->fw_state |= state;
495 /* FOR HW integration */
496 if (_FW_UNDER_SURVEY==state)
497 pmlmepriv->bScanInProcess = true;
500 static inline void _clr_fwstate_(struct mlme_priv *pmlmepriv, int state)
502 pmlmepriv->fw_state &= ~state;
503 /* FOR HW integration */
504 if (_FW_UNDER_SURVEY==state)
505 pmlmepriv->bScanInProcess = false;
509 * No Limit on the calling context,
510 * therefore set it to be the critical section...
512 static inline void clr_fwstate(struct mlme_priv *pmlmepriv, int state)
514 spin_lock_bh(&pmlmepriv->lock);
515 if (check_fwstate(pmlmepriv, state) == true)
516 pmlmepriv->fw_state ^= state;
517 spin_unlock_bh(&pmlmepriv->lock);
520 static inline void clr_fwstate_ex(struct mlme_priv *pmlmepriv, int state)
522 spin_lock_bh(&pmlmepriv->lock);
523 _clr_fwstate_(pmlmepriv, state);
524 spin_unlock_bh(&pmlmepriv->lock);
527 static inline void up_scanned_network(struct mlme_priv *pmlmepriv)
529 spin_lock_bh(&pmlmepriv->lock);
530 pmlmepriv->num_of_scanned++;
531 spin_unlock_bh(&pmlmepriv->lock);
534 static inline void down_scanned_network(struct mlme_priv *pmlmepriv)
536 spin_lock_bh(&pmlmepriv->lock);
537 pmlmepriv->num_of_scanned--;
538 spin_unlock_bh(&pmlmepriv->lock);
541 static inline void set_scanned_network_val(struct mlme_priv *pmlmepriv, int val)
543 spin_lock_bh(&pmlmepriv->lock);
544 pmlmepriv->num_of_scanned = val;
545 spin_unlock_bh(&pmlmepriv->lock);
548 u16 rtw_get_capability(struct wlan_bssid_ex *bss);
549 void rtw_update_scanned_network(struct adapter *adapter,
550 struct wlan_bssid_ex *target);
551 void rtw_disconnect_hdl_under_linked(struct adapter *adapter,
552 struct sta_info *psta, u8 free_assoc);
553 void rtw_generate_random_ibss(u8 *pibss);
554 struct wlan_network *rtw_find_network(struct __queue *scanned_queue, u8 *addr);
555 struct wlan_network *rtw_get_oldest_wlan_network(struct __queue *scanned_queue);
557 void rtw_free_assoc_resources(struct adapter *adapter, int lock_scanned_queue);
558 void rtw_indicate_disconnect(struct adapter *adapter);
559 void rtw_indicate_connect(struct adapter *adapter);
560 void rtw_indicate_scan_done( struct adapter *padapter, bool aborted);
561 void rtw_scan_abort(struct adapter *adapter);
563 int rtw_restruct_sec_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie,
565 int rtw_restruct_wmm_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie,
566 uint in_len, uint initial_out_len);
567 void rtw_init_registrypriv_dev_network(struct adapter *adapter);
569 void rtw_update_registrypriv_dev_network(struct adapter *adapter);
571 void rtw_get_encrypt_decrypt_from_registrypriv(struct adapter *adapter);
573 void _rtw_join_timeout_handler(struct adapter *adapter);
574 void rtw_scan_timeout_handler(struct adapter *adapter);
576 void rtw_dynamic_check_timer_handlder(struct adapter *adapter);
577 #define rtw_is_scan_deny(adapter) false
578 #define rtw_clear_scan_deny(adapter) do {} while (0)
579 #define rtw_set_scan_deny_timer_hdl(adapter) do {} while (0)
580 #define rtw_set_scan_deny(adapter, ms) do {} while (0)
582 int _rtw_init_mlme_priv(struct adapter *padapter);
584 void rtw_free_mlme_priv_ie_data(struct mlme_priv *pmlmepriv);
586 void _rtw_free_mlme_priv(struct mlme_priv *pmlmepriv);
588 int _rtw_enqueue_network(struct __queue *queue, struct wlan_network *pnetwork);
590 struct wlan_network *_rtw_dequeue_network(struct __queue *queue);
592 struct wlan_network *_rtw_alloc_network(struct mlme_priv *pmlmepriv);
594 void _rtw_free_network(struct mlme_priv *pmlmepriv,
595 struct wlan_network *pnetwork, u8 isfreeall);
596 void _rtw_free_network_nolock(struct mlme_priv *pmlmepriv,
597 struct wlan_network *pnetwork);
599 struct wlan_network* _rtw_find_network(struct __queue *scanned_queue, u8 *addr);
601 void _rtw_free_network_queue(struct adapter *padapter, u8 isfreeall);
603 int rtw_if_up(struct adapter *padapter);
605 u8 *rtw_get_capability_from_ie(u8 *ie);
606 u8 *rtw_get_timestampe_from_ie(u8 *ie);
607 u8 *rtw_get_beacon_interval_from_ie(u8 *ie);
609 void rtw_joinbss_reset(struct adapter *padapter);
611 unsigned int rtw_restructure_ht_ie(struct adapter *padapter, u8 *in_ie,
612 u8 *out_ie, uint in_len, uint *pout_len);
613 void rtw_update_ht_cap(struct adapter *padapter, u8 *pie, uint ie_len);
614 void rtw_issue_addbareq_cmd(struct adapter *padapter,
615 struct xmit_frame *pxmitframe);
617 int rtw_is_same_ibss(struct adapter *adapter, struct wlan_network *pnetwork);
618 int is_same_network(struct wlan_bssid_ex *src, struct wlan_bssid_ex *dst);
620 void rtw_roaming(struct adapter *padapter, struct wlan_network *tgt_network);
621 void _rtw_roaming(struct adapter *padapter, struct wlan_network *tgt_network);
622 void rtw_set_roaming(struct adapter *adapter, u8 to_roaming);
623 u8 rtw_to_roaming(struct adapter *adapter);
625 void rtw_sta_media_status_rpt(struct adapter *adapter, struct sta_info *psta,
628 #endif /* __RTL871X_MLME_H_ */