forked from SberHome/bdk_freertos_old
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwpa_psk_cache.c
411 lines (327 loc) · 10.5 KB
/
wpa_psk_cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "includes.h"
#include "wpa_psk_cache.h"
#include "uart_pub.h"
#include "../wpa_supplicant/config.h"
#ifdef CONFIG_WPA_PSK_CACHE
void start_wpa_psk_cal_thread();
beken_thread_t wpa_pskcalc_thread_handle = NULL;
struct wpa_psk_cache *psk_cache;
void wpa_psk_cache_init()
{
psk_cache = os_zalloc(sizeof(*psk_cache));
if (psk_cache) {
#ifdef CONFIG_WPA_PSK_CACHE_MULTI
dl_list_init(&psk_cache->all);
dl_list_init(&psk_cache->pending);
#endif
rtos_init_semaphore(&psk_cache->sema, 1); //TODO: error check
rtos_set_semaphore(&psk_cache->sema);
}
}
#ifdef CONFIG_WPA_PSK_CACHE_MULTI
/* make cache entries less than WPA_PSK_ENTRIES */
static void wpa_psk_cache_expire(struct wpa_psk_cache *cache)
{
struct wpa_psk_cache_item *item;
while (dl_list_len(&cache->all) > WPA_PSK_ENTRIES) {
item = dl_list_first(&cache->all, struct wpa_psk_cache_item, node);
dl_list_del(&item->node); // delete first entry
os_free(item->passphrase);
os_free(item->ssid);
os_printf("vanish one\n");
}
}
static struct wpa_psk_cache_item *wpa_psk_cache_get_from_q(struct dl_list *list, u8 *ssid, size_t ssid_len, char *passphrase)
{
struct wpa_psk_cache_item *item = NULL;
dl_list_for_each(item, list, struct wpa_psk_cache_item, node) {
if (ssid_len != item->ssid_len)
continue;
if (os_memcmp(ssid, item->ssid, item->ssid_len))
continue;
/* ssid the same */
if (os_strcmp(passphrase, item->passphrase))
continue;
/* found */
return item;
}
return NULL;
}
/*
* caller must hold cache->sema.
*/
struct wpa_psk_cache_item *__wpa_psk_cache_get(struct wpa_psk_cache *cache,
u8 *ssid, size_t ssid_len, char *passphrase)
{
struct wpa_psk_cache_item *item = NULL;
item = wpa_psk_cache_get_from_q(&cache->all, ssid, ssid_len, passphrase);
if (item)
return item;
item = wpa_psk_cache_get_from_q(&cache->pending, ssid, ssid_len, passphrase);
return item;
}
int __wpa_get_psk_from_cache(u8 *ssid, size_t ssid_len, char *passphrase, u8 *psk, size_t psk_len)
{
struct wpa_psk_cache_item *item;
struct wpa_psk_cache *cache = psk_cache;
//os_printf("ssid: %s, passphase: %s\r\n", wpa_ssid_txt(ssid, ssid_len), passphrase);
rtos_get_semaphore(&cache->sema, BEKEN_WAIT_FOREVER);
item = __wpa_psk_cache_get(cache, ssid, ssid_len, passphrase);
if (item && (item->flags & WPA_PSK_CACHE_FLAG_COMPLETE)) {
/* copy psk to ssid->psk */
os_memcpy(psk, item->psk, sizeof(item->psk) > psk_len ? psk_len : sizeof(item->psk));
wpa_hexdump_key(MSG_MSGDUMP, "PSK (from cache)", psk, PMK_LEN);
rtos_set_semaphore(&cache->sema);
return 0;
} else if (item) {
rtos_set_semaphore(&cache->sema);
return 1;
}
rtos_set_semaphore(&cache->sema);
return -1;
}
int wpa_get_psk_from_cache(struct wpa_ssid *ssid)
{
return __wpa_get_psk_from_cache(ssid->ssid, ssid->ssid_len,
ssid->passphrase, ssid->psk, sizeof(ssid->psk));
}
/*
* Add psk to cache.
* caller must hold cache->sema.
*/
static int ___wpa_psk_cache_add(struct wpa_psk_cache *cache,
u8 *ssid, size_t ssid_len, char *passphrase, u8 *psk, size_t psk_len)
{
struct wpa_psk_cache_item *item;
/* add a new one */
item = (struct wpa_psk_cache_item *)os_zalloc(sizeof(*item));
if (item) {
item->ssid = dup_binstr(ssid, ssid_len);
item->ssid_len = ssid_len;
item->passphrase = os_strdup(passphrase);
if (!item->ssid || !item->passphrase) {
os_free(item->ssid);
os_free(item->passphrase);
os_free(item);
goto out;
}
if (psk) {
/* copy calculated psk */
os_memcpy(item->psk, psk, psk_len);
item->flags = WPA_PSK_CACHE_FLAG_COMPLETE;
} else {
item->flags = WPA_PSK_CACHE_FLAG_PENDING;
}
dl_list_add_tail(&cache->all, &item->node);
return 0;
}
out:
return -1;
}
/*
* Add psk to cache.
* caller must hold cache->sema.
*/
static int __wpa_psk_cache_add(struct wpa_psk_cache *cache,
u8 *ssid, size_t ssid_len, char *passphrase, u8 *psk, size_t psk_len)
{
struct wpa_psk_cache_item *item;
item = __wpa_psk_cache_get(cache, ssid, ssid_len, passphrase);
if (item) // already exist
return 0;
return ___wpa_psk_cache_add(cache, ssid, ssid_len, passphrase, psk, psk_len);
}
int wpa_psk_request(u8 *ssid, size_t ssid_len, char *passphrase, u8 *psk, size_t psk_len)
{
int ret;
struct wpa_psk_cache *cache = psk_cache;
rtos_get_semaphore(&cache->sema, BEKEN_WAIT_FOREVER);
ret = __wpa_psk_cache_add(cache, ssid, ssid_len, passphrase, psk, psk_len);
wpa_psk_cache_expire(cache);
rtos_set_semaphore(&cache->sema);
start_wpa_psk_cal_thread(); // run thread
return ret;
}
/* Update psk cache item */
static void wpa_psk_cache_update(struct wpa_psk_cache *cache,
u8 *ssid, size_t ssid_len, char *passphrase, u8 *psk, size_t psk_len)
{
struct wpa_psk_cache_item *item;
rtos_get_semaphore(&cache->sema, BEKEN_WAIT_FOREVER);
item = __wpa_psk_cache_get(cache, ssid, ssid_len, passphrase);
if (!item)
goto out;
/* copy calculated psk */
os_memcpy(item->psk, psk, psk_len);
item->flags = WPA_PSK_CACHE_FLAG_COMPLETE;
out:
rtos_set_semaphore(&cache->sema);
}
/* traverse all pending psk caculation */
void wpa_psk_cal_thread(void *arg)
{
struct wpa_psk_cache *cache = psk_cache;
struct wpa_psk_cache_item *item, *n;
int count;
while (1) {
count = 0;
// move all pending node to internal pending queue.
rtos_get_semaphore(&cache->sema, BEKEN_WAIT_FOREVER);
dl_list_for_each_safe(item, n, &cache->all, struct wpa_psk_cache_item, node) {
if (item->flags & WPA_PSK_CACHE_FLAG_COMPLETE)
continue;
dl_list_del(&item->node);
//dl_list_add_tail(&cache->pending, &item->node);
dl_list_add(&cache->pending, &item->node); // add to head, LIFO
count++;
#ifndef CONFIG_WPA_PSK_CALCU_MULTI
break; // always process one, comment out it to process more
#endif
}
rtos_set_semaphore(&cache->sema);
/* no more psk caculation pending */
if (!count)
break;
// caculate psk on pending list
dl_list_for_each_safe(item, n, &cache->pending, struct wpa_psk_cache_item, node) {
os_printf("PSKC: start\n");
pbkdf2_sha1(item->passphrase, (u8 *)item->ssid, item->ssid_len, 4096, item->psk, sizeof(item->psk));
os_printf("PSKC: end\n");
// requeue to complete list.
rtos_get_semaphore(&cache->sema, BEKEN_WAIT_FOREVER);
item->flags = WPA_PSK_CACHE_FLAG_COMPLETE;
dl_list_del(&item->node);
dl_list_add_tail(&cache->all, &item->node);
wpa_psk_cache_expire(cache);
rtos_set_semaphore(&cache->sema);
}
}
wpa_pskcalc_thread_handle = 0;
rtos_delete_thread(NULL); /* Delete thread */
}
#else /* !CONFIG_WPA_PSK_CACHE_MULTI */
int __wpa_get_psk_from_cache(u8 *ssid, size_t ssid_len, char *passphrase, u8 *psk, size_t psk_len)
{
struct wpa_psk_cache *cache = psk_cache;
//bk_printf("ssid: %s, passphase: %s\r\n", wpa_ssid_txt(ssid, ssid_len), passphrase);
rtos_get_semaphore(&cache->sema, BEKEN_WAIT_FOREVER);
if (cache->item.flags == WPA_PSK_CACHE_FLAG_COMPLETE) {
os_memcpy(psk, cache->item.psk, psk_len);
rtos_set_semaphore(&cache->sema);
return 0;
}
rtos_set_semaphore(&cache->sema);
// not available
return 1;
}
int wpa_get_psk_from_cache(struct wpa_ssid *ssid)
{
return __wpa_get_psk_from_cache(ssid->ssid, ssid->ssid_len,
ssid->passphrase, ssid->psk, sizeof(ssid->psk));
}
/**
* request a psk from cache, or starts a new psk calculation task
* @ssid: ssid
* @ssid_len: length of ssid
* @passphrase: passphrase or hex string of psk (in case of fast connect)
* @psk: not used
* @psk_len: not used
*
* returns: 1 if psk has already or in calculation,
* 0 if psk not found and request PSKC task to calculate it.
* -1 error.
*/
int wpa_psk_request(u8 *ssid, size_t ssid_len, char *passphrase, u8 *psk, size_t psk_len)
{
struct wpa_psk_cache *cache = psk_cache;
int complete = 0;
rtos_get_semaphore(&cache->sema, BEKEN_WAIT_FOREVER);
if (ssid_len == cache->item.ssid_len &&
os_memcmp(ssid, cache->item.ssid, ssid_len) == 0 &&
os_strcmp(passphrase, cache->item.passphrase) == 0) {
/* same, in calcuation process or have been calcuated */
rtos_set_semaphore(&cache->sema);
return 1;
}
/* a new request */
os_free(cache->item.ssid);
os_free(cache->item.passphrase);
if (os_strlen(passphrase) == 2 * PMK_LEN) {
/* passphrase is hex string of psk, convert it to binary */
if (!hexstr2bin(passphrase, cache->item.psk, 2 * PMK_LEN))
complete = 1;
}
cache->item.flags = complete ? WPA_PSK_CACHE_FLAG_COMPLETE : WPA_PSK_CACHE_FLAG_PENDING;
cache->item.ssid = dup_binstr(ssid, ssid_len);
cache->item.ssid_len = ssid_len;
cache->item.passphrase = os_strdup(passphrase);
ASSERT(cache->item.ssid && cache->item.passphrase);
rtos_set_semaphore(&cache->sema);
if (!complete)
start_wpa_psk_cal_thread();
return 0;
}
void wpa_psk_cal_thread(void *arg)
{
struct wpa_psk_cache *cache = psk_cache;
char *ssid;
size_t ssid_len;
char *passphrase;
u8 psk[PMK_LEN];
int done;
GLOBAL_INT_DECLARATION();
while (1) {
done = 0;
/* copy to temporay value */
rtos_get_semaphore(&cache->sema, BEKEN_WAIT_FOREVER);
ssid = dup_binstr(cache->item.ssid, cache->item.ssid_len);
ssid_len = cache->item.ssid_len;
passphrase = os_strdup(cache->item.passphrase);
cache->item.flags = WPA_PSK_CACHE_FLAG_PENDING;
rtos_set_semaphore(&cache->sema);
if (!ssid || !passphrase) {
os_free(ssid);
os_free(passphrase);
GLOBAL_INT_DISABLE();
wpa_pskcalc_thread_handle = 0;
GLOBAL_INT_RESTORE();
break;
}
os_printf("PSKC: ssid %s, passphrase %s\n", wpa_ssid_txt(ssid, ssid_len), passphrase);
pbkdf2_sha1(passphrase, (u8 *)ssid, ssid_len, 4096, psk, sizeof(psk));
os_printf("PSKC: end\n");
/* determine ssid & passphrase have been changed */
rtos_get_semaphore(&cache->sema, BEKEN_WAIT_FOREVER);
if (ssid_len == cache->item.ssid_len &&
os_memcmp(ssid, cache->item.ssid, ssid_len) == 0 &&
os_strcmp(passphrase, cache->item.passphrase) == 0) {
os_memcpy(cache->item.psk, psk, sizeof(cache->item.psk));
cache->item.flags = WPA_PSK_CACHE_FLAG_COMPLETE;
done = 1;
GLOBAL_INT_DISABLE();
wpa_pskcalc_thread_handle = 0;
GLOBAL_INT_RESTORE();
}
rtos_set_semaphore(&cache->sema);
os_free(ssid);
os_free(passphrase);
if (done)
break;
}
rtos_delete_thread(NULL); /* Delete thread */
}
#endif
void start_wpa_psk_cal_thread(void)
{
GLOBAL_INT_DECLARATION();
GLOBAL_INT_DISABLE();
if (!wpa_pskcalc_thread_handle) {
rtos_create_thread(&wpa_pskcalc_thread_handle, THD_WPAS_PRIORITY, /* lower or equal than wpas thread */
"pskc", /* psk calcuation task*/
(beken_thread_function_t)wpa_psk_cal_thread,
1024,
(beken_thread_arg_t)0);
}
GLOBAL_INT_RESTORE();
}
#endif //CONFIG_WPA_PSK_CACHE