1 /*
2  * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "esp_wifi_driver.h"
8 #include "esp_wps.h"
9 #include "wps/wps.h"
10 #include "wps/wps_attr_parse.h"
11 
12 /* WPS message flag */
13 enum wps_msg_flag {
14     WPS_MSG_FLAG_MORE = 0x01,
15     WPS_MSG_FLAG_LEN = 0x02
16 };
17 
18 #ifdef USE_WPS_TASK
19 enum wps_sig_type {
20     SIG_WPS_ENABLE = 1,         //1
21     SIG_WPS_DISABLE,            //2
22     SIG_WPS_START,              //3
23     SIG_WPS_RX,                 //4
24     SIG_WPS_TIMER_TIMEOUT,      //5
25     SIG_WPS_TIMER_MSG_TIMEOUT,  //6
26     SIG_WPS_TIMER_SUCCESS_CB,   //7
27     SIG_WPS_TIMER_SCAN,         //8
28     SIG_WPS_TIMER_EAPOL_START,  //9
29     SIG_WPS_NUM,                //10
30 };
31 #endif
32 
33 enum wps_reg_sig_type {
34     SIG_WPS_REG_ENABLE = 1,         //1
35     SIG_WPS_REG_DISABLE,            //2
36     SIG_WPS_REG_START,              //3
37     SIG_WPS_REG_MAX,                //4
38 };
39 
40 typedef struct {
41     void *arg;
42     int ret; /* return value */
43 } wps_ioctl_param_t;
44 
45 #ifdef ESP_SUPPLICANT
46 enum wps_sm_state{
47      WAIT_START,
48      WPA_MESG,
49      WPA_FAIL
50 };
51 #endif /* ESP_SUPPLICANT */
52 
53 #define WPS_IGNORE_SEL_REG_MAX_CNT	4
54 
55 #define WPS_MAX_DIS_AP_NUM	10
56 
57 /* Bssid of the discard AP which is discarded for not select reg or other reason */
58 struct discard_ap_list_t{
59 	u8 bssid[6];
60 };
61 
62 struct wps_sm {
63     u8 state;
64     struct wps_config *wps_cfg;
65     struct wps_context *wps_ctx;
66     struct wps_data *wps;
67     char identity[32];
68     u8 identity_len;
69     u8 ownaddr[ETH_ALEN];
70     u8 bssid[ETH_ALEN];
71     struct wps_credential creds[MAX_CRED_COUNT];
72     u8 ap_cred_cnt;
73     struct wps_device_data *dev;
74     u8 uuid[16];
75     u8 current_identifier;
76     bool is_wps_scan;
77     u8 channel;
78     u8 scan_cnt;
79 #ifdef USE_WPS_TASK
80     u8 wps_sig_cnt[SIG_WPS_NUM];
81 #endif
82     u8 discover_ssid_cnt;
83     bool ignore_sel_reg;
84     struct discard_ap_list_t dis_ap_list[WPS_MAX_DIS_AP_NUM];
85     u8 discard_ap_cnt;
86 };
87 
88 #define API_MUTEX_TAKE() do {\
89     if (!s_wps_api_lock) {\
90         s_wps_api_lock = os_recursive_mutex_create();\
91         if (!s_wps_api_lock) {\
92             wpa_printf(MSG_ERROR, "wps api lock create failed");\
93             return ESP_ERR_NO_MEM;\
94         }\
95     }\
96     os_mutex_lock(s_wps_api_lock);\
97 } while(0)
98 
99 #define API_MUTEX_GIVE() os_mutex_unlock(s_wps_api_lock)
100 #define DATA_MUTEX_TAKE() os_mutex_lock(s_wps_data_lock)
101 #define DATA_MUTEX_GIVE() os_mutex_unlock(s_wps_data_lock)
102 
103 struct wps_sm *wps_sm_get(void);
104 int wps_station_wps_unregister_cb(void);
105 int wps_start_pending(void);
106 int wps_sm_rx_eapol(u8 *src_addr, u8 *buf, u32 len);
107 
108 int wps_dev_deinit(struct wps_device_data *dev);
109 int wps_dev_init(void);
110 int wps_set_factory_info(const esp_wps_config_t *config);
111 
112 struct wps_sm_funcs {
113     void (*wps_sm_notify_deauth)(void);
114 };
115 
116 struct wps_sm_funcs* wps_get_wps_sm_cb(void);
wps_get_type(void)117 static inline int wps_get_type(void)
118 {
119     return esp_wifi_get_wps_type_internal();
120 }
121 
wps_set_type(uint32_t type)122 static inline int wps_set_type(uint32_t type)
123 {
124     return esp_wifi_set_wps_type_internal(type);
125 }
126 
wps_get_status(void)127 static inline int wps_get_status(void)
128 {
129     return esp_wifi_get_wps_status_internal();
130 }
131 
wps_set_status(uint32_t status)132 static inline int wps_set_status(uint32_t status)
133 {
134     return esp_wifi_set_wps_status_internal(status);
135 }
136 
137 bool is_wps_enabled(void);
138 int wps_init_cfg_pin(struct wps_config *cfg);
139 void wifi_station_wps_eapol_start_handle(void *data, void *user_ctx);
140 int wifi_ap_wps_disable_internal(void);
141