1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifdef ESP_SUPPLICANT
16 
17 #include "utils/includes.h"
18 #include "utils/common.h"
19 #include "common/eapol_common.h"
20 #include "rsn_supp/wpa.h"
21 #include "rsn_supp/pmksa_cache.h"
22 
wpa_sm_alloc_eapol(struct wpa_sm * sm,u8 type,const void * data,u16 data_len,size_t * msg_len,void ** data_pos)23 u8   *wpa_sm_alloc_eapol(struct wpa_sm *sm, u8 type,
24                          const void *data, u16 data_len,
25                          size_t *msg_len, void **data_pos)
26 {
27     void *buffer;
28     struct ieee802_1x_hdr *hdr;
29 
30     *msg_len = sizeof(struct ieee802_1x_hdr) + data_len;
31 
32     buffer = os_malloc(*msg_len + sizeof(struct l2_ethhdr));
33 
34     if (buffer == NULL) {
35         return NULL;
36     }
37 
38     /* XXX: reserve l2_ethhdr is enough */
39     hdr = (struct ieee802_1x_hdr *)((char *)buffer + sizeof(struct l2_ethhdr));
40 
41     hdr->version = sm->eapol_version;
42     hdr->type = type;
43     hdr->length = host_to_be16(data_len);
44 
45     if (data) {
46         memcpy(hdr + 1, data, data_len);
47     } else {
48         memset(hdr + 1, 0, data_len);
49     }
50 
51     if (data_pos) {
52         *data_pos = hdr + 1;
53     }
54 
55     return (u8 *) hdr;
56 }
57 
wpa_sm_free_eapol(u8 * buffer)58 void  wpa_sm_free_eapol(u8 *buffer)
59 {
60     buffer = buffer - sizeof(struct l2_ethhdr);
61     os_free(buffer);
62 }
63 
wpa_sm_deauthenticate(struct wpa_sm * sm,u8 reason_code)64 void  wpa_sm_deauthenticate(struct wpa_sm *sm, u8 reason_code)
65 {
66 
67     /*only need send deauth frame when associated*/
68     if (WPA_SM_STATE(sm) >= WPA_ASSOCIATED) {
69         pmksa_cache_clear_current(sm);
70         sm->wpa_deauthenticate(reason_code);
71     }
72 }
73 
74 /**
75  * mlme_setprotection - MLME-SETPROTECTION.request primitive
76  * @priv: Private driver interface data
77  * @addr: Address of the station for which to set protection (may be
78  * %NULL for group keys)
79  * @protect_type: MLME_SETPROTECTION_PROTECT_TYPE_*
80  * @key_type: MLME_SETPROTECTION_KEY_TYPE_*
81  * Returns: 0 on success, -1 on failure
82  *
83  * This is an optional function that can be used to set the driver to
84  * require protection for Tx and/or Rx frames. This uses the layer
85  * interface defined in IEEE 802.11i-2004 clause 10.3.22.1
86  * (MLME-SETPROTECTION.request). Many drivers do not use explicit
87  * set protection operation; instead, they set protection implicitly
88  * based on configured keys.
89  */
wpa_sm_mlme_setprotection(struct wpa_sm * sm,const u8 * addr,int protect_type,int key_type)90 int  wpa_sm_mlme_setprotection(struct wpa_sm *sm, const u8 *addr,
91                                int protect_type, int key_type)
92 {
93     return 0;
94 }
95 
96 /*
97  *use above two functions to get wpa_ie and rsn_ie, then don't need wpa_sm_get_beacon_ie function
98 */
wpa_sm_get_beacon_ie(struct wpa_sm * sm)99 int  wpa_sm_get_beacon_ie(struct wpa_sm *sm)
100 {
101     return 0;
102 }
103 
104 /**
105  * wpa_supplicant_disassociate - Disassociate the current connection
106  * @wpa_s: Pointer to wpa_supplicant data
107  * @reason_code: IEEE 802.11 reason code for the disassociate frame
108  *
109  * This function is used to request %wpa_supplicant to disassociate with the
110  * current AP.
111  */
wpa_sm_disassociate(struct wpa_sm * sm,int reason_code)112 void  wpa_sm_disassociate(struct wpa_sm *sm, int reason_code)
113 {
114     /*check if need clear internal state and data value*/
115 }
116 #endif
117