]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/dpp_supplicant.c
6d9427bb39b1c3213788b93c30b59a5fdff01252
[thirdparty/hostap.git] / wpa_supplicant / dpp_supplicant.c
1 /*
2 * wpa_supplicant - DPP
3 * Copyright (c) 2017, Qualcomm Atheros, Inc.
4 * Copyright (c) 2018-2020, The Linux Foundation
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10 #include "utils/includes.h"
11
12 #include "utils/common.h"
13 #include "utils/eloop.h"
14 #include "utils/ip_addr.h"
15 #include "common/dpp.h"
16 #include "common/gas.h"
17 #include "common/gas_server.h"
18 #include "rsn_supp/wpa.h"
19 #include "rsn_supp/pmksa_cache.h"
20 #include "wpa_supplicant_i.h"
21 #include "config.h"
22 #include "driver_i.h"
23 #include "offchannel.h"
24 #include "gas_query.h"
25 #include "bss.h"
26 #include "scan.h"
27 #include "notify.h"
28 #include "dpp_supplicant.h"
29
30
31 static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
32 unsigned int freq);
33 static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx);
34 static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator);
35 static void wpas_dpp_tx_status(struct wpa_supplicant *wpa_s,
36 unsigned int freq, const u8 *dst,
37 const u8 *src, const u8 *bssid,
38 const u8 *data, size_t data_len,
39 enum offchannel_send_action_result result);
40 static void wpas_dpp_init_timeout(void *eloop_ctx, void *timeout_ctx);
41 static int wpas_dpp_auth_init_next(struct wpa_supplicant *wpa_s);
42 static void
43 wpas_dpp_tx_pkex_status(struct wpa_supplicant *wpa_s,
44 unsigned int freq, const u8 *dst,
45 const u8 *src, const u8 *bssid,
46 const u8 *data, size_t data_len,
47 enum offchannel_send_action_result result);
48
49 static const u8 broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
50
51 /* Use a hardcoded Transaction ID 1 in Peer Discovery frames since there is only
52 * a single transaction in progress at any point in time. */
53 static const u8 TRANSACTION_ID = 1;
54
55
56 /**
57 * wpas_dpp_qr_code - Parse and add DPP bootstrapping info from a QR Code
58 * @wpa_s: Pointer to wpa_supplicant data
59 * @cmd: DPP URI read from a QR Code
60 * Returns: Identifier of the stored info or -1 on failure
61 */
62 int wpas_dpp_qr_code(struct wpa_supplicant *wpa_s, const char *cmd)
63 {
64 struct dpp_bootstrap_info *bi;
65 struct dpp_authentication *auth = wpa_s->dpp_auth;
66
67 bi = dpp_add_qr_code(wpa_s->dpp, cmd);
68 if (!bi)
69 return -1;
70
71 if (auth && auth->response_pending &&
72 dpp_notify_new_qr_code(auth, bi) == 1) {
73 wpa_printf(MSG_DEBUG,
74 "DPP: Sending out pending authentication response");
75 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
76 " freq=%u type=%d",
77 MAC2STR(auth->peer_mac_addr), auth->curr_freq,
78 DPP_PA_AUTHENTICATION_RESP);
79 offchannel_send_action(wpa_s, auth->curr_freq,
80 auth->peer_mac_addr, wpa_s->own_addr,
81 broadcast,
82 wpabuf_head(auth->resp_msg),
83 wpabuf_len(auth->resp_msg),
84 500, wpas_dpp_tx_status, 0);
85 }
86
87 return bi->id;
88 }
89
90
91 /**
92 * wpas_dpp_nfc_uri - Parse and add DPP bootstrapping info from NFC Tag (URI)
93 * @wpa_s: Pointer to wpa_supplicant data
94 * @cmd: DPP URI read from a NFC Tag (URI NDEF message)
95 * Returns: Identifier of the stored info or -1 on failure
96 */
97 int wpas_dpp_nfc_uri(struct wpa_supplicant *wpa_s, const char *cmd)
98 {
99 struct dpp_bootstrap_info *bi;
100
101 bi = dpp_add_nfc_uri(wpa_s->dpp, cmd);
102 if (!bi)
103 return -1;
104
105 return bi->id;
106 }
107
108
109 int wpas_dpp_nfc_handover_req(struct wpa_supplicant *wpa_s, const char *cmd)
110 {
111 const char *pos;
112 struct dpp_bootstrap_info *peer_bi, *own_bi;
113
114 pos = os_strstr(cmd, " own=");
115 if (!pos)
116 return -1;
117 pos += 5;
118 own_bi = dpp_bootstrap_get_id(wpa_s->dpp, atoi(pos));
119 if (!own_bi)
120 return -1;
121
122 pos = os_strstr(cmd, " uri=");
123 if (!pos)
124 return -1;
125 pos += 5;
126 peer_bi = dpp_add_nfc_uri(wpa_s->dpp, pos);
127 if (!peer_bi) {
128 wpa_printf(MSG_INFO,
129 "DPP: Failed to parse URI from NFC Handover Request");
130 return -1;
131 }
132
133 if (dpp_nfc_update_bi(own_bi, peer_bi) < 0)
134 return -1;
135
136 return peer_bi->id;
137 }
138
139
140 int wpas_dpp_nfc_handover_sel(struct wpa_supplicant *wpa_s, const char *cmd)
141 {
142 const char *pos;
143 struct dpp_bootstrap_info *peer_bi, *own_bi;
144
145 pos = os_strstr(cmd, " own=");
146 if (!pos)
147 return -1;
148 pos += 5;
149 own_bi = dpp_bootstrap_get_id(wpa_s->dpp, atoi(pos));
150 if (!own_bi)
151 return -1;
152
153 pos = os_strstr(cmd, " uri=");
154 if (!pos)
155 return -1;
156 pos += 5;
157 peer_bi = dpp_add_nfc_uri(wpa_s->dpp, pos);
158 if (!peer_bi) {
159 wpa_printf(MSG_INFO,
160 "DPP: Failed to parse URI from NFC Handover Select");
161 return -1;
162 }
163
164 if (peer_bi->curve != own_bi->curve) {
165 wpa_printf(MSG_INFO,
166 "DPP: Peer (NFC Handover Selector) used different curve");
167 return -1;
168 }
169
170 return peer_bi->id;
171 }
172
173
174 static void wpas_dpp_auth_resp_retry_timeout(void *eloop_ctx, void *timeout_ctx)
175 {
176 struct wpa_supplicant *wpa_s = eloop_ctx;
177 struct dpp_authentication *auth = wpa_s->dpp_auth;
178
179 if (!auth || !auth->resp_msg)
180 return;
181
182 wpa_printf(MSG_DEBUG,
183 "DPP: Retry Authentication Response after timeout");
184 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
185 " freq=%u type=%d",
186 MAC2STR(auth->peer_mac_addr), auth->curr_freq,
187 DPP_PA_AUTHENTICATION_RESP);
188 offchannel_send_action(wpa_s, auth->curr_freq, auth->peer_mac_addr,
189 wpa_s->own_addr, broadcast,
190 wpabuf_head(auth->resp_msg),
191 wpabuf_len(auth->resp_msg),
192 500, wpas_dpp_tx_status, 0);
193 }
194
195
196 static void wpas_dpp_auth_resp_retry(struct wpa_supplicant *wpa_s)
197 {
198 struct dpp_authentication *auth = wpa_s->dpp_auth;
199 unsigned int wait_time, max_tries;
200
201 if (!auth || !auth->resp_msg)
202 return;
203
204 if (wpa_s->dpp_resp_max_tries)
205 max_tries = wpa_s->dpp_resp_max_tries;
206 else
207 max_tries = 5;
208 auth->auth_resp_tries++;
209 if (auth->auth_resp_tries >= max_tries) {
210 wpa_printf(MSG_INFO, "DPP: No confirm received from initiator - stopping exchange");
211 offchannel_send_action_done(wpa_s);
212 dpp_auth_deinit(wpa_s->dpp_auth);
213 wpa_s->dpp_auth = NULL;
214 return;
215 }
216
217 if (wpa_s->dpp_resp_retry_time)
218 wait_time = wpa_s->dpp_resp_retry_time;
219 else
220 wait_time = 1000;
221 wpa_printf(MSG_DEBUG,
222 "DPP: Schedule retransmission of Authentication Response frame in %u ms",
223 wait_time);
224 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
225 eloop_register_timeout(wait_time / 1000,
226 (wait_time % 1000) * 1000,
227 wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
228 }
229
230
231 static void wpas_dpp_try_to_connect(struct wpa_supplicant *wpa_s)
232 {
233 wpa_printf(MSG_DEBUG, "DPP: Trying to connect to the new network");
234 wpa_s->suitable_network = 0;
235 wpa_s->no_suitable_network = 0;
236 wpa_s->disconnected = 0;
237 wpa_s->reassociate = 1;
238 wpa_s->scan_runs = 0;
239 wpa_s->normal_scans = 0;
240 wpa_supplicant_cancel_sched_scan(wpa_s);
241 wpa_supplicant_req_scan(wpa_s, 0, 0);
242 }
243
244
245 #ifdef CONFIG_DPP2
246
247 static void wpas_dpp_conn_status_result_timeout(void *eloop_ctx,
248 void *timeout_ctx)
249 {
250 struct wpa_supplicant *wpa_s = eloop_ctx;
251 struct dpp_authentication *auth = wpa_s->dpp_auth;
252 enum dpp_status_error result;
253
254 if (!auth || !auth->conn_status_requested)
255 return;
256
257 wpa_printf(MSG_DEBUG,
258 "DPP: Connection timeout - report Connection Status Result");
259 if (wpa_s->suitable_network)
260 result = DPP_STATUS_AUTH_FAILURE;
261 else if (wpa_s->no_suitable_network)
262 result = DPP_STATUS_NO_AP;
263 else
264 result = 255; /* What to report here for unexpected state? */
265 if (wpa_s->wpa_state == WPA_SCANNING)
266 wpas_abort_ongoing_scan(wpa_s);
267 wpas_dpp_send_conn_status_result(wpa_s, result);
268 }
269
270
271 static char * wpas_dpp_scan_channel_list(struct wpa_supplicant *wpa_s)
272 {
273 char *str, *end, *pos;
274 size_t len;
275 unsigned int i;
276 u8 last_op_class = 0;
277 int res;
278
279 if (!wpa_s->last_scan_freqs || !wpa_s->num_last_scan_freqs)
280 return NULL;
281
282 len = wpa_s->num_last_scan_freqs * 8;
283 str = os_zalloc(len);
284 if (!str)
285 return NULL;
286 end = str + len;
287 pos = str;
288
289 for (i = 0; i < wpa_s->num_last_scan_freqs; i++) {
290 enum hostapd_hw_mode mode;
291 u8 op_class, channel;
292
293 mode = ieee80211_freq_to_channel_ext(wpa_s->last_scan_freqs[i],
294 0, 0, &op_class, &channel);
295 if (mode == NUM_HOSTAPD_MODES)
296 continue;
297 if (op_class == last_op_class)
298 res = os_snprintf(pos, end - pos, ",%d", channel);
299 else
300 res = os_snprintf(pos, end - pos, "%s%d/%d",
301 pos == str ? "" : ",",
302 op_class, channel);
303 if (os_snprintf_error(end - pos, res)) {
304 *pos = '\0';
305 break;
306 }
307 pos += res;
308 last_op_class = op_class;
309 }
310
311 if (pos == str) {
312 os_free(str);
313 str = NULL;
314 }
315 return str;
316 }
317
318
319 void wpas_dpp_send_conn_status_result(struct wpa_supplicant *wpa_s,
320 enum dpp_status_error result)
321 {
322 struct wpabuf *msg;
323 const char *channel_list = NULL;
324 char *channel_list_buf = NULL;
325 struct wpa_ssid *ssid = wpa_s->current_ssid;
326 struct dpp_authentication *auth = wpa_s->dpp_auth;
327
328 eloop_cancel_timeout(wpas_dpp_conn_status_result_timeout, wpa_s, NULL);
329
330 if (!auth || !auth->conn_status_requested)
331 return;
332 auth->conn_status_requested = 0;
333 wpa_printf(MSG_DEBUG, "DPP: Report connection status result %d",
334 result);
335
336 if (result == DPP_STATUS_NO_AP) {
337 channel_list_buf = wpas_dpp_scan_channel_list(wpa_s);
338 channel_list = channel_list_buf;
339 }
340
341 msg = dpp_build_conn_status_result(auth, result,
342 ssid ? ssid->ssid :
343 wpa_s->dpp_last_ssid,
344 ssid ? ssid->ssid_len :
345 wpa_s->dpp_last_ssid_len,
346 channel_list);
347 os_free(channel_list_buf);
348 if (!msg) {
349 dpp_auth_deinit(wpa_s->dpp_auth);
350 wpa_s->dpp_auth = NULL;
351 return;
352 }
353
354 wpa_msg(wpa_s, MSG_INFO,
355 DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
356 MAC2STR(auth->peer_mac_addr), auth->curr_freq,
357 DPP_PA_CONNECTION_STATUS_RESULT);
358 offchannel_send_action(wpa_s, auth->curr_freq,
359 auth->peer_mac_addr, wpa_s->own_addr, broadcast,
360 wpabuf_head(msg), wpabuf_len(msg),
361 500, wpas_dpp_tx_status, 0);
362 wpabuf_free(msg);
363
364 /* This exchange will be terminated in the TX status handler */
365 auth->remove_on_tx_status = 1;
366
367 return;
368 }
369
370
371 void wpas_dpp_connected(struct wpa_supplicant *wpa_s)
372 {
373 struct dpp_authentication *auth = wpa_s->dpp_auth;
374
375 if (auth && auth->conn_status_requested)
376 wpas_dpp_send_conn_status_result(wpa_s, DPP_STATUS_OK);
377 }
378
379 #endif /* CONFIG_DPP2 */
380
381
382 static void wpas_dpp_tx_status(struct wpa_supplicant *wpa_s,
383 unsigned int freq, const u8 *dst,
384 const u8 *src, const u8 *bssid,
385 const u8 *data, size_t data_len,
386 enum offchannel_send_action_result result)
387 {
388 const char *res_txt;
389 struct dpp_authentication *auth = wpa_s->dpp_auth;
390
391 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
392 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
393 "FAILED");
394 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
395 " result=%s", freq, MAC2STR(dst), res_txt);
396 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
397 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
398
399 if (!wpa_s->dpp_auth) {
400 wpa_printf(MSG_DEBUG,
401 "DPP: Ignore TX status since there is no ongoing authentication exchange");
402 return;
403 }
404
405 #ifdef CONFIG_DPP2
406 if (auth->connect_on_tx_status) {
407 auth->connect_on_tx_status = 0;
408 wpa_printf(MSG_DEBUG,
409 "DPP: Try to connect after completed configuration result");
410 wpas_dpp_try_to_connect(wpa_s);
411 if (auth->conn_status_requested) {
412 wpa_printf(MSG_DEBUG,
413 "DPP: Start 15 second timeout for reporting connection status result");
414 eloop_cancel_timeout(
415 wpas_dpp_conn_status_result_timeout,
416 wpa_s, NULL);
417 eloop_register_timeout(
418 15, 0, wpas_dpp_conn_status_result_timeout,
419 wpa_s, NULL);
420 } else {
421 dpp_auth_deinit(wpa_s->dpp_auth);
422 wpa_s->dpp_auth = NULL;
423 }
424 return;
425 }
426 #endif /* CONFIG_DPP2 */
427
428 if (wpa_s->dpp_auth->remove_on_tx_status) {
429 wpa_printf(MSG_DEBUG,
430 "DPP: Terminate authentication exchange due to a request to do so on TX status");
431 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
432 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
433 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
434 NULL);
435 offchannel_send_action_done(wpa_s);
436 dpp_auth_deinit(wpa_s->dpp_auth);
437 wpa_s->dpp_auth = NULL;
438 return;
439 }
440
441 if (wpa_s->dpp_auth_ok_on_ack)
442 wpas_dpp_auth_success(wpa_s, 1);
443
444 if (!is_broadcast_ether_addr(dst) &&
445 result != OFFCHANNEL_SEND_ACTION_SUCCESS) {
446 wpa_printf(MSG_DEBUG,
447 "DPP: Unicast DPP Action frame was not ACKed");
448 if (auth->waiting_auth_resp) {
449 /* In case of DPP Authentication Request frame, move to
450 * the next channel immediately. */
451 offchannel_send_action_done(wpa_s);
452 wpas_dpp_auth_init_next(wpa_s);
453 return;
454 }
455 if (auth->waiting_auth_conf) {
456 wpas_dpp_auth_resp_retry(wpa_s);
457 return;
458 }
459 }
460
461 if (!is_broadcast_ether_addr(dst) && auth->waiting_auth_resp &&
462 result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
463 /* Allow timeout handling to stop iteration if no response is
464 * received from a peer that has ACKed a request. */
465 auth->auth_req_ack = 1;
466 }
467
468 if (!wpa_s->dpp_auth_ok_on_ack && wpa_s->dpp_auth->neg_freq > 0 &&
469 wpa_s->dpp_auth->curr_freq != wpa_s->dpp_auth->neg_freq) {
470 wpa_printf(MSG_DEBUG,
471 "DPP: Move from curr_freq %u MHz to neg_freq %u MHz for response",
472 wpa_s->dpp_auth->curr_freq,
473 wpa_s->dpp_auth->neg_freq);
474 offchannel_send_action_done(wpa_s);
475 wpas_dpp_listen_start(wpa_s, wpa_s->dpp_auth->neg_freq);
476 }
477
478 if (wpa_s->dpp_auth_ok_on_ack)
479 wpa_s->dpp_auth_ok_on_ack = 0;
480 }
481
482
483 static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx)
484 {
485 struct wpa_supplicant *wpa_s = eloop_ctx;
486 struct dpp_authentication *auth = wpa_s->dpp_auth;
487 unsigned int freq;
488 struct os_reltime now, diff;
489 unsigned int wait_time, diff_ms;
490
491 if (!auth || !auth->waiting_auth_resp)
492 return;
493
494 wait_time = wpa_s->dpp_resp_wait_time ?
495 wpa_s->dpp_resp_wait_time : 2000;
496 os_get_reltime(&now);
497 os_reltime_sub(&now, &wpa_s->dpp_last_init, &diff);
498 diff_ms = diff.sec * 1000 + diff.usec / 1000;
499 wpa_printf(MSG_DEBUG,
500 "DPP: Reply wait timeout - wait_time=%u diff_ms=%u",
501 wait_time, diff_ms);
502
503 if (auth->auth_req_ack && diff_ms >= wait_time) {
504 /* Peer ACK'ed Authentication Request frame, but did not reply
505 * with Authentication Response frame within two seconds. */
506 wpa_printf(MSG_INFO,
507 "DPP: No response received from responder - stopping initiation attempt");
508 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_INIT_FAILED);
509 offchannel_send_action_done(wpa_s);
510 wpas_dpp_listen_stop(wpa_s);
511 dpp_auth_deinit(auth);
512 wpa_s->dpp_auth = NULL;
513 return;
514 }
515
516 if (diff_ms >= wait_time) {
517 /* Authentication Request frame was not ACK'ed and no reply
518 * was receiving within two seconds. */
519 wpa_printf(MSG_DEBUG,
520 "DPP: Continue Initiator channel iteration");
521 offchannel_send_action_done(wpa_s);
522 wpas_dpp_listen_stop(wpa_s);
523 wpas_dpp_auth_init_next(wpa_s);
524 return;
525 }
526
527 /* Driver did not support 2000 ms long wait_time with TX command, so
528 * schedule listen operation to continue waiting for the response.
529 *
530 * DPP listen operations continue until stopped, so simply schedule a
531 * new call to this function at the point when the two second reply
532 * wait has expired. */
533 wait_time -= diff_ms;
534
535 freq = auth->curr_freq;
536 if (auth->neg_freq > 0)
537 freq = auth->neg_freq;
538 wpa_printf(MSG_DEBUG,
539 "DPP: Continue reply wait on channel %u MHz for %u ms",
540 freq, wait_time);
541 wpa_s->dpp_in_response_listen = 1;
542 wpas_dpp_listen_start(wpa_s, freq);
543
544 eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
545 wpas_dpp_reply_wait_timeout, wpa_s, NULL);
546 }
547
548
549 static void wpas_dpp_set_testing_options(struct wpa_supplicant *wpa_s,
550 struct dpp_authentication *auth)
551 {
552 #ifdef CONFIG_TESTING_OPTIONS
553 if (wpa_s->dpp_config_obj_override)
554 auth->config_obj_override =
555 os_strdup(wpa_s->dpp_config_obj_override);
556 if (wpa_s->dpp_discovery_override)
557 auth->discovery_override =
558 os_strdup(wpa_s->dpp_discovery_override);
559 if (wpa_s->dpp_groups_override)
560 auth->groups_override =
561 os_strdup(wpa_s->dpp_groups_override);
562 auth->ignore_netaccesskey_mismatch =
563 wpa_s->dpp_ignore_netaccesskey_mismatch;
564 #endif /* CONFIG_TESTING_OPTIONS */
565 }
566
567
568 static void wpas_dpp_init_timeout(void *eloop_ctx, void *timeout_ctx)
569 {
570 struct wpa_supplicant *wpa_s = eloop_ctx;
571
572 if (!wpa_s->dpp_auth)
573 return;
574 wpa_printf(MSG_DEBUG, "DPP: Retry initiation after timeout");
575 wpas_dpp_auth_init_next(wpa_s);
576 }
577
578
579 static int wpas_dpp_auth_init_next(struct wpa_supplicant *wpa_s)
580 {
581 struct dpp_authentication *auth = wpa_s->dpp_auth;
582 const u8 *dst;
583 unsigned int wait_time, max_wait_time, freq, max_tries, used;
584 struct os_reltime now, diff;
585
586 wpa_s->dpp_in_response_listen = 0;
587 if (!auth)
588 return -1;
589
590 if (auth->freq_idx == 0)
591 os_get_reltime(&wpa_s->dpp_init_iter_start);
592
593 if (auth->freq_idx >= auth->num_freq) {
594 auth->num_freq_iters++;
595 if (wpa_s->dpp_init_max_tries)
596 max_tries = wpa_s->dpp_init_max_tries;
597 else
598 max_tries = 5;
599 if (auth->num_freq_iters >= max_tries || auth->auth_req_ack) {
600 wpa_printf(MSG_INFO,
601 "DPP: No response received from responder - stopping initiation attempt");
602 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_INIT_FAILED);
603 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout,
604 wpa_s, NULL);
605 offchannel_send_action_done(wpa_s);
606 dpp_auth_deinit(wpa_s->dpp_auth);
607 wpa_s->dpp_auth = NULL;
608 return -1;
609 }
610 auth->freq_idx = 0;
611 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
612 if (wpa_s->dpp_init_retry_time)
613 wait_time = wpa_s->dpp_init_retry_time;
614 else
615 wait_time = 10000;
616 os_get_reltime(&now);
617 os_reltime_sub(&now, &wpa_s->dpp_init_iter_start, &diff);
618 used = diff.sec * 1000 + diff.usec / 1000;
619 if (used > wait_time)
620 wait_time = 0;
621 else
622 wait_time -= used;
623 wpa_printf(MSG_DEBUG, "DPP: Next init attempt in %u ms",
624 wait_time);
625 eloop_register_timeout(wait_time / 1000,
626 (wait_time % 1000) * 1000,
627 wpas_dpp_init_timeout, wpa_s,
628 NULL);
629 return 0;
630 }
631 freq = auth->freq[auth->freq_idx++];
632 auth->curr_freq = freq;
633
634 if (is_zero_ether_addr(auth->peer_bi->mac_addr))
635 dst = broadcast;
636 else
637 dst = auth->peer_bi->mac_addr;
638 wpa_s->dpp_auth_ok_on_ack = 0;
639 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
640 wait_time = wpa_s->max_remain_on_chan;
641 max_wait_time = wpa_s->dpp_resp_wait_time ?
642 wpa_s->dpp_resp_wait_time : 2000;
643 if (wait_time > max_wait_time)
644 wait_time = max_wait_time;
645 wait_time += 10; /* give the driver some extra time to complete */
646 eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
647 wpas_dpp_reply_wait_timeout,
648 wpa_s, NULL);
649 wait_time -= 10;
650 if (auth->neg_freq > 0 && freq != auth->neg_freq) {
651 wpa_printf(MSG_DEBUG,
652 "DPP: Initiate on %u MHz and move to neg_freq %u MHz for response",
653 freq, auth->neg_freq);
654 }
655 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
656 MAC2STR(dst), freq, DPP_PA_AUTHENTICATION_REQ);
657 auth->auth_req_ack = 0;
658 os_get_reltime(&wpa_s->dpp_last_init);
659 return offchannel_send_action(wpa_s, freq, dst,
660 wpa_s->own_addr, broadcast,
661 wpabuf_head(auth->req_msg),
662 wpabuf_len(auth->req_msg),
663 wait_time, wpas_dpp_tx_status, 0);
664 }
665
666
667 int wpas_dpp_auth_init(struct wpa_supplicant *wpa_s, const char *cmd)
668 {
669 const char *pos;
670 struct dpp_bootstrap_info *peer_bi, *own_bi = NULL;
671 struct dpp_authentication *auth;
672 u8 allowed_roles = DPP_CAPAB_CONFIGURATOR;
673 unsigned int neg_freq = 0;
674 int tcp = 0;
675 #ifdef CONFIG_DPP2
676 int tcp_port = DPP_TCP_PORT;
677 struct hostapd_ip_addr ipaddr;
678 char *addr;
679 #endif /* CONFIG_DPP2 */
680
681 wpa_s->dpp_gas_client = 0;
682
683 pos = os_strstr(cmd, " peer=");
684 if (!pos)
685 return -1;
686 pos += 6;
687 peer_bi = dpp_bootstrap_get_id(wpa_s->dpp, atoi(pos));
688 if (!peer_bi) {
689 wpa_printf(MSG_INFO,
690 "DPP: Could not find bootstrapping info for the identified peer");
691 return -1;
692 }
693
694 #ifdef CONFIG_DPP2
695 pos = os_strstr(cmd, " tcp_port=");
696 if (pos) {
697 pos += 10;
698 tcp_port = atoi(pos);
699 }
700
701 addr = get_param(cmd, " tcp_addr=");
702 if (addr) {
703 int res;
704
705 res = hostapd_parse_ip_addr(addr, &ipaddr);
706 os_free(addr);
707 if (res)
708 return -1;
709 tcp = 1;
710 }
711 #endif /* CONFIG_DPP2 */
712
713 pos = os_strstr(cmd, " own=");
714 if (pos) {
715 pos += 5;
716 own_bi = dpp_bootstrap_get_id(wpa_s->dpp, atoi(pos));
717 if (!own_bi) {
718 wpa_printf(MSG_INFO,
719 "DPP: Could not find bootstrapping info for the identified local entry");
720 return -1;
721 }
722
723 if (peer_bi->curve != own_bi->curve) {
724 wpa_printf(MSG_INFO,
725 "DPP: Mismatching curves in bootstrapping info (peer=%s own=%s)",
726 peer_bi->curve->name, own_bi->curve->name);
727 return -1;
728 }
729 }
730
731 pos = os_strstr(cmd, " role=");
732 if (pos) {
733 pos += 6;
734 if (os_strncmp(pos, "configurator", 12) == 0)
735 allowed_roles = DPP_CAPAB_CONFIGURATOR;
736 else if (os_strncmp(pos, "enrollee", 8) == 0)
737 allowed_roles = DPP_CAPAB_ENROLLEE;
738 else if (os_strncmp(pos, "either", 6) == 0)
739 allowed_roles = DPP_CAPAB_CONFIGURATOR |
740 DPP_CAPAB_ENROLLEE;
741 else
742 goto fail;
743 }
744
745 pos = os_strstr(cmd, " netrole=");
746 if (pos) {
747 pos += 9;
748 if (os_strncmp(pos, "ap", 2) == 0)
749 wpa_s->dpp_netrole = DPP_NETROLE_AP;
750 else if (os_strncmp(pos, "configurator", 12) == 0)
751 wpa_s->dpp_netrole = DPP_NETROLE_CONFIGURATOR;
752 else
753 wpa_s->dpp_netrole = DPP_NETROLE_STA;
754 } else {
755 wpa_s->dpp_netrole = DPP_NETROLE_STA;
756 }
757
758 pos = os_strstr(cmd, " neg_freq=");
759 if (pos)
760 neg_freq = atoi(pos + 10);
761
762 if (!tcp && wpa_s->dpp_auth) {
763 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
764 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
765 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
766 NULL);
767 offchannel_send_action_done(wpa_s);
768 dpp_auth_deinit(wpa_s->dpp_auth);
769 wpa_s->dpp_auth = NULL;
770 }
771
772 auth = dpp_auth_init(wpa_s, peer_bi, own_bi, allowed_roles, neg_freq,
773 wpa_s->hw.modes, wpa_s->hw.num_modes);
774 if (!auth)
775 goto fail;
776 wpas_dpp_set_testing_options(wpa_s, auth);
777 if (dpp_set_configurator(wpa_s->dpp, wpa_s, auth, cmd) < 0) {
778 dpp_auth_deinit(auth);
779 goto fail;
780 }
781
782 auth->neg_freq = neg_freq;
783
784 if (!is_zero_ether_addr(peer_bi->mac_addr))
785 os_memcpy(auth->peer_mac_addr, peer_bi->mac_addr, ETH_ALEN);
786
787 #ifdef CONFIG_DPP2
788 if (tcp)
789 return dpp_tcp_init(wpa_s->dpp, auth, &ipaddr, tcp_port);
790 #endif /* CONFIG_DPP2 */
791
792 wpa_s->dpp_auth = auth;
793 return wpas_dpp_auth_init_next(wpa_s);
794 fail:
795 return -1;
796 }
797
798
799 struct wpas_dpp_listen_work {
800 unsigned int freq;
801 unsigned int duration;
802 struct wpabuf *probe_resp_ie;
803 };
804
805
806 static void wpas_dpp_listen_work_free(struct wpas_dpp_listen_work *lwork)
807 {
808 if (!lwork)
809 return;
810 os_free(lwork);
811 }
812
813
814 static void wpas_dpp_listen_work_done(struct wpa_supplicant *wpa_s)
815 {
816 struct wpas_dpp_listen_work *lwork;
817
818 if (!wpa_s->dpp_listen_work)
819 return;
820
821 lwork = wpa_s->dpp_listen_work->ctx;
822 wpas_dpp_listen_work_free(lwork);
823 radio_work_done(wpa_s->dpp_listen_work);
824 wpa_s->dpp_listen_work = NULL;
825 }
826
827
828 static void dpp_start_listen_cb(struct wpa_radio_work *work, int deinit)
829 {
830 struct wpa_supplicant *wpa_s = work->wpa_s;
831 struct wpas_dpp_listen_work *lwork = work->ctx;
832
833 if (deinit) {
834 if (work->started) {
835 wpa_s->dpp_listen_work = NULL;
836 wpas_dpp_listen_stop(wpa_s);
837 }
838 wpas_dpp_listen_work_free(lwork);
839 return;
840 }
841
842 wpa_s->dpp_listen_work = work;
843
844 wpa_s->dpp_pending_listen_freq = lwork->freq;
845
846 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq,
847 wpa_s->max_remain_on_chan) < 0) {
848 wpa_printf(MSG_DEBUG,
849 "DPP: Failed to request the driver to remain on channel (%u MHz) for listen",
850 lwork->freq);
851 wpa_s->dpp_listen_freq = 0;
852 wpas_dpp_listen_work_done(wpa_s);
853 wpa_s->dpp_pending_listen_freq = 0;
854 return;
855 }
856 wpa_s->off_channel_freq = 0;
857 wpa_s->roc_waiting_drv_freq = lwork->freq;
858 }
859
860
861 static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
862 unsigned int freq)
863 {
864 struct wpas_dpp_listen_work *lwork;
865
866 if (wpa_s->dpp_listen_work) {
867 wpa_printf(MSG_DEBUG,
868 "DPP: Reject start_listen since dpp_listen_work already exists");
869 return -1;
870 }
871
872 if (wpa_s->dpp_listen_freq)
873 wpas_dpp_listen_stop(wpa_s);
874 wpa_s->dpp_listen_freq = freq;
875
876 lwork = os_zalloc(sizeof(*lwork));
877 if (!lwork)
878 return -1;
879 lwork->freq = freq;
880
881 if (radio_add_work(wpa_s, freq, "dpp-listen", 0, dpp_start_listen_cb,
882 lwork) < 0) {
883 wpas_dpp_listen_work_free(lwork);
884 return -1;
885 }
886
887 return 0;
888 }
889
890
891 int wpas_dpp_listen(struct wpa_supplicant *wpa_s, const char *cmd)
892 {
893 int freq;
894
895 freq = atoi(cmd);
896 if (freq <= 0)
897 return -1;
898
899 if (os_strstr(cmd, " role=configurator"))
900 wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR;
901 else if (os_strstr(cmd, " role=enrollee"))
902 wpa_s->dpp_allowed_roles = DPP_CAPAB_ENROLLEE;
903 else
904 wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR |
905 DPP_CAPAB_ENROLLEE;
906 wpa_s->dpp_qr_mutual = os_strstr(cmd, " qr=mutual") != NULL;
907 if (os_strstr(cmd, " netrole=ap"))
908 wpa_s->dpp_netrole = DPP_NETROLE_AP;
909 else if (os_strstr(cmd, " netrole=configurator"))
910 wpa_s->dpp_netrole = DPP_NETROLE_CONFIGURATOR;
911 else
912 wpa_s->dpp_netrole = DPP_NETROLE_STA;
913 if (wpa_s->dpp_listen_freq == (unsigned int) freq) {
914 wpa_printf(MSG_DEBUG, "DPP: Already listening on %u MHz",
915 freq);
916 return 0;
917 }
918
919 return wpas_dpp_listen_start(wpa_s, freq);
920 }
921
922
923 void wpas_dpp_listen_stop(struct wpa_supplicant *wpa_s)
924 {
925 wpa_s->dpp_in_response_listen = 0;
926 if (!wpa_s->dpp_listen_freq)
927 return;
928
929 wpa_printf(MSG_DEBUG, "DPP: Stop listen on %u MHz",
930 wpa_s->dpp_listen_freq);
931 wpa_drv_cancel_remain_on_channel(wpa_s);
932 wpa_s->dpp_listen_freq = 0;
933 wpas_dpp_listen_work_done(wpa_s);
934 }
935
936
937 void wpas_dpp_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
938 unsigned int freq)
939 {
940 wpas_dpp_listen_work_done(wpa_s);
941
942 if (wpa_s->dpp_auth && wpa_s->dpp_in_response_listen) {
943 unsigned int new_freq;
944
945 /* Continue listen with a new remain-on-channel */
946 if (wpa_s->dpp_auth->neg_freq > 0)
947 new_freq = wpa_s->dpp_auth->neg_freq;
948 else
949 new_freq = wpa_s->dpp_auth->curr_freq;
950 wpa_printf(MSG_DEBUG,
951 "DPP: Continue wait on %u MHz for the ongoing DPP provisioning session",
952 new_freq);
953 wpas_dpp_listen_start(wpa_s, new_freq);
954 return;
955 }
956
957 if (wpa_s->dpp_listen_freq) {
958 /* Continue listen with a new remain-on-channel */
959 wpas_dpp_listen_start(wpa_s, wpa_s->dpp_listen_freq);
960 }
961 }
962
963
964 static void wpas_dpp_rx_auth_req(struct wpa_supplicant *wpa_s, const u8 *src,
965 const u8 *hdr, const u8 *buf, size_t len,
966 unsigned int freq)
967 {
968 const u8 *r_bootstrap, *i_bootstrap;
969 u16 r_bootstrap_len, i_bootstrap_len;
970 struct dpp_bootstrap_info *own_bi = NULL, *peer_bi = NULL;
971
972 if (!wpa_s->dpp)
973 return;
974
975 wpa_printf(MSG_DEBUG, "DPP: Authentication Request from " MACSTR,
976 MAC2STR(src));
977
978 r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
979 &r_bootstrap_len);
980 if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
981 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
982 "Missing or invalid required Responder Bootstrapping Key Hash attribute");
983 return;
984 }
985 wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
986 r_bootstrap, r_bootstrap_len);
987
988 i_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
989 &i_bootstrap_len);
990 if (!i_bootstrap || i_bootstrap_len != SHA256_MAC_LEN) {
991 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
992 "Missing or invalid required Initiator Bootstrapping Key Hash attribute");
993 return;
994 }
995 wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Bootstrapping Key Hash",
996 i_bootstrap, i_bootstrap_len);
997
998 /* Try to find own and peer bootstrapping key matches based on the
999 * received hash values */
1000 dpp_bootstrap_find_pair(wpa_s->dpp, i_bootstrap, r_bootstrap,
1001 &own_bi, &peer_bi);
1002 if (!own_bi) {
1003 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1004 "No matching own bootstrapping key found - ignore message");
1005 return;
1006 }
1007
1008 if (wpa_s->dpp_auth) {
1009 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1010 "Already in DPP authentication exchange - ignore new one");
1011 return;
1012 }
1013
1014 wpa_s->dpp_gas_client = 0;
1015 wpa_s->dpp_auth_ok_on_ack = 0;
1016 wpa_s->dpp_auth = dpp_auth_req_rx(wpa_s, wpa_s->dpp_allowed_roles,
1017 wpa_s->dpp_qr_mutual,
1018 peer_bi, own_bi, freq, hdr, buf, len);
1019 if (!wpa_s->dpp_auth) {
1020 wpa_printf(MSG_DEBUG, "DPP: No response generated");
1021 return;
1022 }
1023 wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
1024 if (dpp_set_configurator(wpa_s->dpp, wpa_s, wpa_s->dpp_auth,
1025 wpa_s->dpp_configurator_params) < 0) {
1026 dpp_auth_deinit(wpa_s->dpp_auth);
1027 wpa_s->dpp_auth = NULL;
1028 return;
1029 }
1030 os_memcpy(wpa_s->dpp_auth->peer_mac_addr, src, ETH_ALEN);
1031
1032 if (wpa_s->dpp_listen_freq &&
1033 wpa_s->dpp_listen_freq != wpa_s->dpp_auth->curr_freq) {
1034 wpa_printf(MSG_DEBUG,
1035 "DPP: Stop listen on %u MHz to allow response on the request %u MHz",
1036 wpa_s->dpp_listen_freq, wpa_s->dpp_auth->curr_freq);
1037 wpas_dpp_listen_stop(wpa_s);
1038 }
1039
1040 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1041 MAC2STR(src), wpa_s->dpp_auth->curr_freq,
1042 DPP_PA_AUTHENTICATION_RESP);
1043 offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
1044 src, wpa_s->own_addr, broadcast,
1045 wpabuf_head(wpa_s->dpp_auth->resp_msg),
1046 wpabuf_len(wpa_s->dpp_auth->resp_msg),
1047 500, wpas_dpp_tx_status, 0);
1048 }
1049
1050
1051 static void wpas_dpp_start_gas_server(struct wpa_supplicant *wpa_s)
1052 {
1053 /* TODO: stop wait and start ROC */
1054 }
1055
1056
1057 static struct wpa_ssid * wpas_dpp_add_network(struct wpa_supplicant *wpa_s,
1058 struct dpp_authentication *auth,
1059 struct dpp_config_obj *conf)
1060 {
1061 struct wpa_ssid *ssid;
1062
1063 #ifdef CONFIG_DPP2
1064 if (conf->akm == DPP_AKM_SAE) {
1065 #ifdef CONFIG_SAE
1066 struct wpa_driver_capa capa;
1067 int res;
1068
1069 res = wpa_drv_get_capa(wpa_s, &capa);
1070 if (res == 0 &&
1071 !(capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE) &&
1072 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
1073 wpa_printf(MSG_DEBUG,
1074 "DPP: SAE not supported by the driver");
1075 return NULL;
1076 }
1077 #else /* CONFIG_SAE */
1078 wpa_printf(MSG_DEBUG, "DPP: SAE not supported in the build");
1079 return NULL;
1080 #endif /* CONFIG_SAE */
1081 }
1082 #endif /* CONFIG_DPP2 */
1083
1084 ssid = wpa_config_add_network(wpa_s->conf);
1085 if (!ssid)
1086 return NULL;
1087 wpas_notify_network_added(wpa_s, ssid);
1088 wpa_config_set_network_defaults(ssid);
1089 ssid->disabled = 1;
1090
1091 ssid->ssid = os_malloc(conf->ssid_len);
1092 if (!ssid->ssid)
1093 goto fail;
1094 os_memcpy(ssid->ssid, conf->ssid, conf->ssid_len);
1095 ssid->ssid_len = conf->ssid_len;
1096
1097 if (conf->connector) {
1098 ssid->key_mgmt = WPA_KEY_MGMT_DPP;
1099 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
1100 ssid->dpp_connector = os_strdup(conf->connector);
1101 if (!ssid->dpp_connector)
1102 goto fail;
1103 }
1104
1105 if (conf->c_sign_key) {
1106 ssid->dpp_csign = os_malloc(wpabuf_len(conf->c_sign_key));
1107 if (!ssid->dpp_csign)
1108 goto fail;
1109 os_memcpy(ssid->dpp_csign, wpabuf_head(conf->c_sign_key),
1110 wpabuf_len(conf->c_sign_key));
1111 ssid->dpp_csign_len = wpabuf_len(conf->c_sign_key);
1112 }
1113
1114 if (auth->net_access_key) {
1115 ssid->dpp_netaccesskey =
1116 os_malloc(wpabuf_len(auth->net_access_key));
1117 if (!ssid->dpp_netaccesskey)
1118 goto fail;
1119 os_memcpy(ssid->dpp_netaccesskey,
1120 wpabuf_head(auth->net_access_key),
1121 wpabuf_len(auth->net_access_key));
1122 ssid->dpp_netaccesskey_len = wpabuf_len(auth->net_access_key);
1123 ssid->dpp_netaccesskey_expiry = auth->net_access_key_expiry;
1124 }
1125
1126 if (!conf->connector || dpp_akm_psk(conf->akm) ||
1127 dpp_akm_sae(conf->akm)) {
1128 if (!conf->connector)
1129 ssid->key_mgmt = 0;
1130 if (dpp_akm_psk(conf->akm))
1131 ssid->key_mgmt |= WPA_KEY_MGMT_PSK |
1132 WPA_KEY_MGMT_PSK_SHA256 | WPA_KEY_MGMT_FT_PSK;
1133 if (dpp_akm_sae(conf->akm))
1134 ssid->key_mgmt |= WPA_KEY_MGMT_SAE |
1135 WPA_KEY_MGMT_FT_SAE;
1136 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
1137 if (conf->passphrase[0]) {
1138 if (wpa_config_set_quoted(ssid, "psk",
1139 conf->passphrase) < 0)
1140 goto fail;
1141 wpa_config_update_psk(ssid);
1142 ssid->export_keys = 1;
1143 } else {
1144 ssid->psk_set = conf->psk_set;
1145 os_memcpy(ssid->psk, conf->psk, PMK_LEN);
1146 }
1147 }
1148
1149 os_memcpy(wpa_s->dpp_last_ssid, conf->ssid, conf->ssid_len);
1150 wpa_s->dpp_last_ssid_len = conf->ssid_len;
1151
1152 return ssid;
1153 fail:
1154 wpas_notify_network_removed(wpa_s, ssid);
1155 wpa_config_remove_network(wpa_s->conf, ssid->id);
1156 return NULL;
1157 }
1158
1159
1160 static int wpas_dpp_process_config(struct wpa_supplicant *wpa_s,
1161 struct dpp_authentication *auth,
1162 struct dpp_config_obj *conf)
1163 {
1164 struct wpa_ssid *ssid;
1165
1166 if (wpa_s->conf->dpp_config_processing < 1)
1167 return 0;
1168
1169 ssid = wpas_dpp_add_network(wpa_s, auth, conf);
1170 if (!ssid)
1171 return -1;
1172
1173 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_NETWORK_ID "%d", ssid->id);
1174 if (wpa_s->conf->dpp_config_processing == 2)
1175 ssid->disabled = 0;
1176
1177 #ifndef CONFIG_NO_CONFIG_WRITE
1178 if (wpa_s->conf->update_config &&
1179 wpa_config_write(wpa_s->confname, wpa_s->conf))
1180 wpa_printf(MSG_DEBUG, "DPP: Failed to update configuration");
1181 #endif /* CONFIG_NO_CONFIG_WRITE */
1182
1183 return 0;
1184 }
1185
1186
1187 static void wpas_dpp_post_process_config(struct wpa_supplicant *wpa_s,
1188 struct dpp_authentication *auth)
1189 {
1190 if (wpa_s->conf->dpp_config_processing < 2)
1191 return;
1192
1193 #ifdef CONFIG_DPP2
1194 if (auth->peer_version >= 2) {
1195 wpa_printf(MSG_DEBUG,
1196 "DPP: Postpone connection attempt to wait for completion of DPP Configuration Result");
1197 auth->connect_on_tx_status = 1;
1198 return;
1199 }
1200 #endif /* CONFIG_DPP2 */
1201
1202 wpas_dpp_try_to_connect(wpa_s);
1203 }
1204
1205
1206 static int wpas_dpp_handle_config_obj(struct wpa_supplicant *wpa_s,
1207 struct dpp_authentication *auth,
1208 struct dpp_config_obj *conf)
1209 {
1210 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_RECEIVED);
1211 if (conf->ssid_len)
1212 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFOBJ_SSID "%s",
1213 wpa_ssid_txt(conf->ssid, conf->ssid_len));
1214 if (conf->ssid_charset)
1215 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFOBJ_SSID_CHARSET "%d",
1216 conf->ssid_charset);
1217 if (conf->connector) {
1218 /* TODO: Save the Connector and consider using a command
1219 * to fetch the value instead of sending an event with
1220 * it. The Connector could end up being larger than what
1221 * most clients are ready to receive as an event
1222 * message. */
1223 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONNECTOR "%s",
1224 conf->connector);
1225 }
1226 if (conf->c_sign_key) {
1227 char *hex;
1228 size_t hexlen;
1229
1230 hexlen = 2 * wpabuf_len(conf->c_sign_key) + 1;
1231 hex = os_malloc(hexlen);
1232 if (hex) {
1233 wpa_snprintf_hex(hex, hexlen,
1234 wpabuf_head(conf->c_sign_key),
1235 wpabuf_len(conf->c_sign_key));
1236 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_C_SIGN_KEY "%s",
1237 hex);
1238 os_free(hex);
1239 }
1240 }
1241 if (auth->net_access_key) {
1242 char *hex;
1243 size_t hexlen;
1244
1245 hexlen = 2 * wpabuf_len(auth->net_access_key) + 1;
1246 hex = os_malloc(hexlen);
1247 if (hex) {
1248 wpa_snprintf_hex(hex, hexlen,
1249 wpabuf_head(auth->net_access_key),
1250 wpabuf_len(auth->net_access_key));
1251 if (auth->net_access_key_expiry)
1252 wpa_msg(wpa_s, MSG_INFO,
1253 DPP_EVENT_NET_ACCESS_KEY "%s %lu", hex,
1254 (long unsigned)
1255 auth->net_access_key_expiry);
1256 else
1257 wpa_msg(wpa_s, MSG_INFO,
1258 DPP_EVENT_NET_ACCESS_KEY "%s", hex);
1259 os_free(hex);
1260 }
1261 }
1262
1263 return wpas_dpp_process_config(wpa_s, auth, conf);
1264 }
1265
1266
1267 static int wpas_dpp_handle_key_pkg(struct wpa_supplicant *wpa_s,
1268 struct dpp_asymmetric_key *key)
1269 {
1270 #ifdef CONFIG_DPP2
1271 int res;
1272
1273 if (!key)
1274 return 0;
1275
1276 wpa_printf(MSG_DEBUG, "DPP: Received Configurator backup");
1277 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_RECEIVED);
1278
1279 while (key) {
1280 res = dpp_configurator_from_backup(wpa_s->dpp, key);
1281 if (res < 0)
1282 return -1;
1283 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFIGURATOR_ID "%d",
1284 res);
1285 key = key->next;
1286 }
1287 #endif /* CONFIG_DPP2 */
1288
1289 return 0;
1290 }
1291
1292
1293 static void wpas_dpp_gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
1294 enum gas_query_result result,
1295 const struct wpabuf *adv_proto,
1296 const struct wpabuf *resp, u16 status_code)
1297 {
1298 struct wpa_supplicant *wpa_s = ctx;
1299 const u8 *pos;
1300 struct dpp_authentication *auth = wpa_s->dpp_auth;
1301 int res;
1302 enum dpp_status_error status = DPP_STATUS_CONFIG_REJECTED;
1303 unsigned int i;
1304
1305 wpa_s->dpp_gas_dialog_token = -1;
1306
1307 if (!auth || !auth->auth_success) {
1308 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
1309 return;
1310 }
1311 if (result != GAS_QUERY_SUCCESS ||
1312 !resp || status_code != WLAN_STATUS_SUCCESS) {
1313 wpa_printf(MSG_DEBUG, "DPP: GAS query did not succeed");
1314 goto fail;
1315 }
1316
1317 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response adv_proto",
1318 adv_proto);
1319 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response (GAS response)",
1320 resp);
1321
1322 if (wpabuf_len(adv_proto) != 10 ||
1323 !(pos = wpabuf_head(adv_proto)) ||
1324 pos[0] != WLAN_EID_ADV_PROTO ||
1325 pos[1] != 8 ||
1326 pos[3] != WLAN_EID_VENDOR_SPECIFIC ||
1327 pos[4] != 5 ||
1328 WPA_GET_BE24(&pos[5]) != OUI_WFA ||
1329 pos[8] != 0x1a ||
1330 pos[9] != 1) {
1331 wpa_printf(MSG_DEBUG,
1332 "DPP: Not a DPP Advertisement Protocol ID");
1333 goto fail;
1334 }
1335
1336 if (dpp_conf_resp_rx(auth, resp) < 0) {
1337 wpa_printf(MSG_DEBUG, "DPP: Configuration attempt failed");
1338 goto fail;
1339 }
1340
1341 for (i = 0; i < auth->num_conf_obj; i++) {
1342 res = wpas_dpp_handle_config_obj(wpa_s, auth,
1343 &auth->conf_obj[i]);
1344 if (res < 0)
1345 goto fail;
1346 }
1347 if (auth->num_conf_obj)
1348 wpas_dpp_post_process_config(wpa_s, auth);
1349 if (wpas_dpp_handle_key_pkg(wpa_s, auth->conf_key_pkg) < 0)
1350 goto fail;
1351
1352 status = DPP_STATUS_OK;
1353 #ifdef CONFIG_TESTING_OPTIONS
1354 if (dpp_test == DPP_TEST_REJECT_CONFIG) {
1355 wpa_printf(MSG_INFO, "DPP: TESTING - Reject Config Object");
1356 status = DPP_STATUS_CONFIG_REJECTED;
1357 }
1358 #endif /* CONFIG_TESTING_OPTIONS */
1359 fail:
1360 if (status != DPP_STATUS_OK)
1361 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1362 #ifdef CONFIG_DPP2
1363 if (auth->peer_version >= 2 &&
1364 auth->conf_resp_status == DPP_STATUS_OK) {
1365 struct wpabuf *msg;
1366
1367 wpa_printf(MSG_DEBUG, "DPP: Send DPP Configuration Result");
1368 msg = dpp_build_conf_result(auth, status);
1369 if (!msg)
1370 goto fail2;
1371
1372 wpa_msg(wpa_s, MSG_INFO,
1373 DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1374 MAC2STR(addr), auth->curr_freq,
1375 DPP_PA_CONFIGURATION_RESULT);
1376 offchannel_send_action(wpa_s, auth->curr_freq,
1377 addr, wpa_s->own_addr, broadcast,
1378 wpabuf_head(msg),
1379 wpabuf_len(msg),
1380 500, wpas_dpp_tx_status, 0);
1381 wpabuf_free(msg);
1382
1383 /* This exchange will be terminated in the TX status handler */
1384 return;
1385 }
1386 fail2:
1387 #endif /* CONFIG_DPP2 */
1388 dpp_auth_deinit(wpa_s->dpp_auth);
1389 wpa_s->dpp_auth = NULL;
1390 }
1391
1392
1393 static void wpas_dpp_start_gas_client(struct wpa_supplicant *wpa_s)
1394 {
1395 struct dpp_authentication *auth = wpa_s->dpp_auth;
1396 struct wpabuf *buf;
1397 int res;
1398 int *supp_op_classes;
1399
1400 wpa_s->dpp_gas_client = 1;
1401 offchannel_send_action_done(wpa_s);
1402 wpas_dpp_listen_stop(wpa_s);
1403
1404 supp_op_classes = wpas_supp_op_classes(wpa_s);
1405 buf = dpp_build_conf_req_helper(auth, wpa_s->conf->dpp_name,
1406 wpa_s->dpp_netrole,
1407 wpa_s->conf->dpp_mud_url,
1408 supp_op_classes);
1409 os_free(supp_op_classes);
1410 if (!buf) {
1411 wpa_printf(MSG_DEBUG,
1412 "DPP: No configuration request data available");
1413 return;
1414 }
1415
1416 wpa_printf(MSG_DEBUG, "DPP: GAS request to " MACSTR " (freq %u MHz)",
1417 MAC2STR(auth->peer_mac_addr), auth->curr_freq);
1418
1419 res = gas_query_req(wpa_s->gas, auth->peer_mac_addr, auth->curr_freq,
1420 1, buf, wpas_dpp_gas_resp_cb, wpa_s);
1421 if (res < 0) {
1422 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
1423 wpabuf_free(buf);
1424 } else {
1425 wpa_printf(MSG_DEBUG,
1426 "DPP: GAS query started with dialog token %u", res);
1427 wpa_s->dpp_gas_dialog_token = res;
1428 }
1429 }
1430
1431
1432 static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator)
1433 {
1434 wpa_printf(MSG_DEBUG, "DPP: Authentication succeeded");
1435 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=%d", initiator);
1436 #ifdef CONFIG_TESTING_OPTIONS
1437 if (dpp_test == DPP_TEST_STOP_AT_AUTH_CONF) {
1438 wpa_printf(MSG_INFO,
1439 "DPP: TESTING - stop at Authentication Confirm");
1440 if (wpa_s->dpp_auth->configurator) {
1441 /* Prevent GAS response */
1442 wpa_s->dpp_auth->auth_success = 0;
1443 }
1444 return;
1445 }
1446 #endif /* CONFIG_TESTING_OPTIONS */
1447
1448 if (wpa_s->dpp_auth->configurator)
1449 wpas_dpp_start_gas_server(wpa_s);
1450 else
1451 wpas_dpp_start_gas_client(wpa_s);
1452 }
1453
1454
1455 static void wpas_dpp_rx_auth_resp(struct wpa_supplicant *wpa_s, const u8 *src,
1456 const u8 *hdr, const u8 *buf, size_t len,
1457 unsigned int freq)
1458 {
1459 struct dpp_authentication *auth = wpa_s->dpp_auth;
1460 struct wpabuf *msg;
1461
1462 wpa_printf(MSG_DEBUG, "DPP: Authentication Response from " MACSTR
1463 " (freq %u MHz)", MAC2STR(src), freq);
1464
1465 if (!auth) {
1466 wpa_printf(MSG_DEBUG,
1467 "DPP: No DPP Authentication in progress - drop");
1468 return;
1469 }
1470
1471 if (!is_zero_ether_addr(auth->peer_mac_addr) &&
1472 os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1473 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1474 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1475 return;
1476 }
1477
1478 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
1479
1480 if (auth->curr_freq != freq && auth->neg_freq == freq) {
1481 wpa_printf(MSG_DEBUG,
1482 "DPP: Responder accepted request for different negotiation channel");
1483 auth->curr_freq = freq;
1484 }
1485
1486 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
1487 msg = dpp_auth_resp_rx(auth, hdr, buf, len);
1488 if (!msg) {
1489 if (auth->auth_resp_status == DPP_STATUS_RESPONSE_PENDING) {
1490 wpa_printf(MSG_DEBUG,
1491 "DPP: Start wait for full response");
1492 offchannel_send_action_done(wpa_s);
1493 wpas_dpp_listen_start(wpa_s, auth->curr_freq);
1494 return;
1495 }
1496 wpa_printf(MSG_DEBUG, "DPP: No confirm generated");
1497 return;
1498 }
1499 os_memcpy(auth->peer_mac_addr, src, ETH_ALEN);
1500
1501 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1502 MAC2STR(src), auth->curr_freq, DPP_PA_AUTHENTICATION_CONF);
1503 offchannel_send_action(wpa_s, auth->curr_freq,
1504 src, wpa_s->own_addr, broadcast,
1505 wpabuf_head(msg), wpabuf_len(msg),
1506 500, wpas_dpp_tx_status, 0);
1507 wpabuf_free(msg);
1508 wpa_s->dpp_auth_ok_on_ack = 1;
1509 }
1510
1511
1512 static void wpas_dpp_rx_auth_conf(struct wpa_supplicant *wpa_s, const u8 *src,
1513 const u8 *hdr, const u8 *buf, size_t len)
1514 {
1515 struct dpp_authentication *auth = wpa_s->dpp_auth;
1516
1517 wpa_printf(MSG_DEBUG, "DPP: Authentication Confirmation from " MACSTR,
1518 MAC2STR(src));
1519
1520 if (!auth) {
1521 wpa_printf(MSG_DEBUG,
1522 "DPP: No DPP Authentication in progress - drop");
1523 return;
1524 }
1525
1526 if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1527 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1528 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1529 return;
1530 }
1531
1532 if (dpp_auth_conf_rx(auth, hdr, buf, len) < 0) {
1533 wpa_printf(MSG_DEBUG, "DPP: Authentication failed");
1534 return;
1535 }
1536
1537 wpas_dpp_auth_success(wpa_s, 0);
1538 }
1539
1540
1541 #ifdef CONFIG_DPP2
1542
1543 static void wpas_dpp_config_result_wait_timeout(void *eloop_ctx,
1544 void *timeout_ctx)
1545 {
1546 struct wpa_supplicant *wpa_s = eloop_ctx;
1547 struct dpp_authentication *auth = wpa_s->dpp_auth;
1548
1549 if (!auth || !auth->waiting_conf_result)
1550 return;
1551
1552 wpa_printf(MSG_DEBUG,
1553 "DPP: Timeout while waiting for Configuration Result");
1554 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1555 dpp_auth_deinit(auth);
1556 wpa_s->dpp_auth = NULL;
1557 }
1558
1559
1560 static void wpas_dpp_conn_status_result_wait_timeout(void *eloop_ctx,
1561 void *timeout_ctx)
1562 {
1563 struct wpa_supplicant *wpa_s = eloop_ctx;
1564 struct dpp_authentication *auth = wpa_s->dpp_auth;
1565
1566 if (!auth || !auth->waiting_conn_status_result)
1567 return;
1568
1569 wpa_printf(MSG_DEBUG,
1570 "DPP: Timeout while waiting for Connection Status Result");
1571 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONN_STATUS_RESULT "timeout");
1572 wpas_dpp_listen_stop(wpa_s);
1573 dpp_auth_deinit(auth);
1574 wpa_s->dpp_auth = NULL;
1575 }
1576
1577
1578 static void wpas_dpp_rx_conf_result(struct wpa_supplicant *wpa_s, const u8 *src,
1579 const u8 *hdr, const u8 *buf, size_t len)
1580 {
1581 struct dpp_authentication *auth = wpa_s->dpp_auth;
1582 enum dpp_status_error status;
1583
1584 wpa_printf(MSG_DEBUG, "DPP: Configuration Result from " MACSTR,
1585 MAC2STR(src));
1586
1587 if (!auth || !auth->waiting_conf_result) {
1588 wpa_printf(MSG_DEBUG,
1589 "DPP: No DPP Configuration waiting for result - drop");
1590 return;
1591 }
1592
1593 if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1594 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1595 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1596 return;
1597 }
1598
1599 status = dpp_conf_result_rx(auth, hdr, buf, len);
1600
1601 if (status == DPP_STATUS_OK && auth->send_conn_status) {
1602 wpa_msg(wpa_s, MSG_INFO,
1603 DPP_EVENT_CONF_SENT "wait_conn_status=1");
1604 wpa_printf(MSG_DEBUG, "DPP: Wait for Connection Status Result");
1605 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout,
1606 wpa_s, NULL);
1607 auth->waiting_conn_status_result = 1;
1608 eloop_cancel_timeout(wpas_dpp_conn_status_result_wait_timeout,
1609 wpa_s, NULL);
1610 eloop_register_timeout(16, 0,
1611 wpas_dpp_conn_status_result_wait_timeout,
1612 wpa_s, NULL);
1613 offchannel_send_action_done(wpa_s);
1614 wpas_dpp_listen_start(wpa_s, auth->neg_freq ? auth->neg_freq :
1615 auth->curr_freq);
1616 return;
1617 }
1618 offchannel_send_action_done(wpa_s);
1619 wpas_dpp_listen_stop(wpa_s);
1620 if (status == DPP_STATUS_OK)
1621 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
1622 else
1623 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1624 dpp_auth_deinit(auth);
1625 wpa_s->dpp_auth = NULL;
1626 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout, wpa_s, NULL);
1627 }
1628
1629
1630 static void wpas_dpp_rx_conn_status_result(struct wpa_supplicant *wpa_s,
1631 const u8 *src, const u8 *hdr,
1632 const u8 *buf, size_t len)
1633 {
1634 struct dpp_authentication *auth = wpa_s->dpp_auth;
1635 enum dpp_status_error status;
1636 u8 ssid[SSID_MAX_LEN];
1637 size_t ssid_len = 0;
1638 char *channel_list = NULL;
1639
1640 wpa_printf(MSG_DEBUG, "DPP: Connection Status Result");
1641
1642 if (!auth || !auth->waiting_conn_status_result) {
1643 wpa_printf(MSG_DEBUG,
1644 "DPP: No DPP Configuration waiting for connection status result - drop");
1645 return;
1646 }
1647
1648 status = dpp_conn_status_result_rx(auth, hdr, buf, len,
1649 ssid, &ssid_len, &channel_list);
1650 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONN_STATUS_RESULT
1651 "result=%d ssid=%s channel_list=%s",
1652 status, wpa_ssid_txt(ssid, ssid_len),
1653 channel_list ? channel_list : "N/A");
1654 os_free(channel_list);
1655 offchannel_send_action_done(wpa_s);
1656 wpas_dpp_listen_stop(wpa_s);
1657 dpp_auth_deinit(auth);
1658 wpa_s->dpp_auth = NULL;
1659 eloop_cancel_timeout(wpas_dpp_conn_status_result_wait_timeout,
1660 wpa_s, NULL);
1661 }
1662
1663
1664 static int wpas_dpp_process_conf_obj(void *ctx,
1665 struct dpp_authentication *auth)
1666 {
1667 struct wpa_supplicant *wpa_s = ctx;
1668 unsigned int i;
1669 int res = -1;
1670
1671 for (i = 0; i < auth->num_conf_obj; i++) {
1672 res = wpas_dpp_handle_config_obj(wpa_s, auth,
1673 &auth->conf_obj[i]);
1674 if (res)
1675 break;
1676 }
1677 if (!res)
1678 wpas_dpp_post_process_config(wpa_s, auth);
1679
1680 return res;
1681 }
1682
1683 #endif /* CONFIG_DPP2 */
1684
1685
1686 static void wpas_dpp_rx_peer_disc_resp(struct wpa_supplicant *wpa_s,
1687 const u8 *src,
1688 const u8 *buf, size_t len)
1689 {
1690 struct wpa_ssid *ssid;
1691 const u8 *connector, *trans_id, *status;
1692 u16 connector_len, trans_id_len, status_len;
1693 struct dpp_introduction intro;
1694 struct rsn_pmksa_cache_entry *entry;
1695 struct os_time now;
1696 struct os_reltime rnow;
1697 os_time_t expiry;
1698 unsigned int seconds;
1699 enum dpp_status_error res;
1700
1701 wpa_printf(MSG_DEBUG, "DPP: Peer Discovery Response from " MACSTR,
1702 MAC2STR(src));
1703 if (is_zero_ether_addr(wpa_s->dpp_intro_bssid) ||
1704 os_memcmp(src, wpa_s->dpp_intro_bssid, ETH_ALEN) != 0) {
1705 wpa_printf(MSG_DEBUG, "DPP: Not waiting for response from "
1706 MACSTR " - drop", MAC2STR(src));
1707 return;
1708 }
1709 offchannel_send_action_done(wpa_s);
1710
1711 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1712 if (ssid == wpa_s->dpp_intro_network)
1713 break;
1714 }
1715 if (!ssid || !ssid->dpp_connector || !ssid->dpp_netaccesskey ||
1716 !ssid->dpp_csign) {
1717 wpa_printf(MSG_DEBUG,
1718 "DPP: Profile not found for network introduction");
1719 return;
1720 }
1721
1722 trans_id = dpp_get_attr(buf, len, DPP_ATTR_TRANSACTION_ID,
1723 &trans_id_len);
1724 if (!trans_id || trans_id_len != 1) {
1725 wpa_printf(MSG_DEBUG,
1726 "DPP: Peer did not include Transaction ID");
1727 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1728 " fail=missing_transaction_id", MAC2STR(src));
1729 goto fail;
1730 }
1731 if (trans_id[0] != TRANSACTION_ID) {
1732 wpa_printf(MSG_DEBUG,
1733 "DPP: Ignore frame with unexpected Transaction ID %u",
1734 trans_id[0]);
1735 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1736 " fail=transaction_id_mismatch", MAC2STR(src));
1737 goto fail;
1738 }
1739
1740 status = dpp_get_attr(buf, len, DPP_ATTR_STATUS, &status_len);
1741 if (!status || status_len != 1) {
1742 wpa_printf(MSG_DEBUG, "DPP: Peer did not include Status");
1743 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1744 " fail=missing_status", MAC2STR(src));
1745 goto fail;
1746 }
1747 if (status[0] != DPP_STATUS_OK) {
1748 wpa_printf(MSG_DEBUG,
1749 "DPP: Peer rejected network introduction: Status %u",
1750 status[0]);
1751 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1752 " status=%u", MAC2STR(src), status[0]);
1753 #ifdef CONFIG_DPP2
1754 wpas_dpp_send_conn_status_result(wpa_s, status[0]);
1755 #endif /* CONFIG_DPP2 */
1756 goto fail;
1757 }
1758
1759 connector = dpp_get_attr(buf, len, DPP_ATTR_CONNECTOR, &connector_len);
1760 if (!connector) {
1761 wpa_printf(MSG_DEBUG,
1762 "DPP: Peer did not include its Connector");
1763 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1764 " fail=missing_connector", MAC2STR(src));
1765 goto fail;
1766 }
1767
1768 res = dpp_peer_intro(&intro, ssid->dpp_connector,
1769 ssid->dpp_netaccesskey,
1770 ssid->dpp_netaccesskey_len,
1771 ssid->dpp_csign,
1772 ssid->dpp_csign_len,
1773 connector, connector_len, &expiry);
1774 if (res != DPP_STATUS_OK) {
1775 wpa_printf(MSG_INFO,
1776 "DPP: Network Introduction protocol resulted in failure");
1777 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1778 " fail=peer_connector_validation_failed", MAC2STR(src));
1779 #ifdef CONFIG_DPP2
1780 wpas_dpp_send_conn_status_result(wpa_s, res);
1781 #endif /* CONFIG_DPP2 */
1782 goto fail;
1783 }
1784
1785 entry = os_zalloc(sizeof(*entry));
1786 if (!entry)
1787 goto fail;
1788 os_memcpy(entry->aa, src, ETH_ALEN);
1789 os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN);
1790 os_memcpy(entry->pmk, intro.pmk, intro.pmk_len);
1791 entry->pmk_len = intro.pmk_len;
1792 entry->akmp = WPA_KEY_MGMT_DPP;
1793 if (expiry) {
1794 os_get_time(&now);
1795 seconds = expiry - now.sec;
1796 } else {
1797 seconds = 86400 * 7;
1798 }
1799 os_get_reltime(&rnow);
1800 entry->expiration = rnow.sec + seconds;
1801 entry->reauth_time = rnow.sec + seconds;
1802 entry->network_ctx = ssid;
1803 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
1804
1805 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1806 " status=%u", MAC2STR(src), status[0]);
1807
1808 wpa_printf(MSG_DEBUG,
1809 "DPP: Try connection again after successful network introduction");
1810 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
1811 wpa_supplicant_cancel_sched_scan(wpa_s);
1812 wpa_supplicant_req_scan(wpa_s, 0, 0);
1813 }
1814 fail:
1815 os_memset(&intro, 0, sizeof(intro));
1816 }
1817
1818
1819 static int wpas_dpp_allow_ir(struct wpa_supplicant *wpa_s, unsigned int freq)
1820 {
1821 int i, j;
1822
1823 if (!wpa_s->hw.modes)
1824 return -1;
1825
1826 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1827 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1828
1829 for (j = 0; j < mode->num_channels; j++) {
1830 struct hostapd_channel_data *chan = &mode->channels[j];
1831
1832 if (chan->freq != (int) freq)
1833 continue;
1834
1835 if (chan->flag & (HOSTAPD_CHAN_DISABLED |
1836 HOSTAPD_CHAN_NO_IR |
1837 HOSTAPD_CHAN_RADAR))
1838 continue;
1839
1840 return 1;
1841 }
1842 }
1843
1844 wpa_printf(MSG_DEBUG,
1845 "DPP: Frequency %u MHz not supported or does not allow PKEX initiation in the current channel list",
1846 freq);
1847
1848 return 0;
1849 }
1850
1851
1852 static int wpas_dpp_pkex_next_channel(struct wpa_supplicant *wpa_s,
1853 struct dpp_pkex *pkex)
1854 {
1855 if (pkex->freq == 2437)
1856 pkex->freq = 5745;
1857 else if (pkex->freq == 5745)
1858 pkex->freq = 5220;
1859 else if (pkex->freq == 5220)
1860 pkex->freq = 60480;
1861 else
1862 return -1; /* no more channels to try */
1863
1864 if (wpas_dpp_allow_ir(wpa_s, pkex->freq) == 1) {
1865 wpa_printf(MSG_DEBUG, "DPP: Try to initiate on %u MHz",
1866 pkex->freq);
1867 return 0;
1868 }
1869
1870 /* Could not use this channel - try the next one */
1871 return wpas_dpp_pkex_next_channel(wpa_s, pkex);
1872 }
1873
1874
1875 static void wpas_dpp_pkex_retry_timeout(void *eloop_ctx, void *timeout_ctx)
1876 {
1877 struct wpa_supplicant *wpa_s = eloop_ctx;
1878 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1879
1880 if (!pkex || !pkex->exchange_req)
1881 return;
1882 if (pkex->exch_req_tries >= 5) {
1883 if (wpas_dpp_pkex_next_channel(wpa_s, pkex) < 0) {
1884 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1885 "No response from PKEX peer");
1886 dpp_pkex_free(pkex);
1887 wpa_s->dpp_pkex = NULL;
1888 return;
1889 }
1890 pkex->exch_req_tries = 0;
1891 }
1892
1893 pkex->exch_req_tries++;
1894 wpa_printf(MSG_DEBUG, "DPP: Retransmit PKEX Exchange Request (try %u)",
1895 pkex->exch_req_tries);
1896 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1897 MAC2STR(broadcast), pkex->freq, DPP_PA_PKEX_EXCHANGE_REQ);
1898 offchannel_send_action(wpa_s, pkex->freq, broadcast,
1899 wpa_s->own_addr, broadcast,
1900 wpabuf_head(pkex->exchange_req),
1901 wpabuf_len(pkex->exchange_req),
1902 pkex->exch_req_wait_time,
1903 wpas_dpp_tx_pkex_status, 0);
1904 }
1905
1906
1907 static void
1908 wpas_dpp_tx_pkex_status(struct wpa_supplicant *wpa_s,
1909 unsigned int freq, const u8 *dst,
1910 const u8 *src, const u8 *bssid,
1911 const u8 *data, size_t data_len,
1912 enum offchannel_send_action_result result)
1913 {
1914 const char *res_txt;
1915 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1916
1917 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
1918 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
1919 "FAILED");
1920 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
1921 " result=%s (PKEX)",
1922 freq, MAC2STR(dst), res_txt);
1923 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
1924 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
1925
1926 if (!pkex) {
1927 wpa_printf(MSG_DEBUG,
1928 "DPP: Ignore TX status since there is no ongoing PKEX exchange");
1929 return;
1930 }
1931
1932 if (pkex->failed) {
1933 wpa_printf(MSG_DEBUG,
1934 "DPP: Terminate PKEX exchange due to an earlier error");
1935 if (pkex->t > pkex->own_bi->pkex_t)
1936 pkex->own_bi->pkex_t = pkex->t;
1937 dpp_pkex_free(pkex);
1938 wpa_s->dpp_pkex = NULL;
1939 return;
1940 }
1941
1942 if (pkex->exch_req_wait_time && pkex->exchange_req) {
1943 /* Wait for PKEX Exchange Response frame and retry request if
1944 * no response is seen. */
1945 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
1946 eloop_register_timeout(pkex->exch_req_wait_time / 1000,
1947 (pkex->exch_req_wait_time % 1000) * 1000,
1948 wpas_dpp_pkex_retry_timeout, wpa_s,
1949 NULL);
1950 }
1951 }
1952
1953
1954 static void
1955 wpas_dpp_rx_pkex_exchange_req(struct wpa_supplicant *wpa_s, const u8 *src,
1956 const u8 *buf, size_t len, unsigned int freq)
1957 {
1958 struct wpabuf *msg;
1959 unsigned int wait_time;
1960
1961 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Request from " MACSTR,
1962 MAC2STR(src));
1963
1964 /* TODO: Support multiple PKEX codes by iterating over all the enabled
1965 * values here */
1966
1967 if (!wpa_s->dpp_pkex_code || !wpa_s->dpp_pkex_bi) {
1968 wpa_printf(MSG_DEBUG,
1969 "DPP: No PKEX code configured - ignore request");
1970 return;
1971 }
1972
1973 if (wpa_s->dpp_pkex) {
1974 /* TODO: Support parallel operations */
1975 wpa_printf(MSG_DEBUG,
1976 "DPP: Already in PKEX session - ignore new request");
1977 return;
1978 }
1979
1980 wpa_s->dpp_pkex = dpp_pkex_rx_exchange_req(wpa_s, wpa_s->dpp_pkex_bi,
1981 wpa_s->own_addr, src,
1982 wpa_s->dpp_pkex_identifier,
1983 wpa_s->dpp_pkex_code,
1984 buf, len);
1985 if (!wpa_s->dpp_pkex) {
1986 wpa_printf(MSG_DEBUG,
1987 "DPP: Failed to process the request - ignore it");
1988 return;
1989 }
1990
1991 msg = wpa_s->dpp_pkex->exchange_resp;
1992 wait_time = wpa_s->max_remain_on_chan;
1993 if (wait_time > 2000)
1994 wait_time = 2000;
1995 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1996 MAC2STR(src), freq, DPP_PA_PKEX_EXCHANGE_RESP);
1997 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
1998 broadcast,
1999 wpabuf_head(msg), wpabuf_len(msg),
2000 wait_time, wpas_dpp_tx_pkex_status, 0);
2001 }
2002
2003
2004 static void
2005 wpas_dpp_rx_pkex_exchange_resp(struct wpa_supplicant *wpa_s, const u8 *src,
2006 const u8 *buf, size_t len, unsigned int freq)
2007 {
2008 struct wpabuf *msg;
2009 unsigned int wait_time;
2010
2011 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Response from " MACSTR,
2012 MAC2STR(src));
2013
2014 /* TODO: Support multiple PKEX codes by iterating over all the enabled
2015 * values here */
2016
2017 if (!wpa_s->dpp_pkex || !wpa_s->dpp_pkex->initiator ||
2018 wpa_s->dpp_pkex->exchange_done) {
2019 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2020 return;
2021 }
2022
2023 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
2024 wpa_s->dpp_pkex->exch_req_wait_time = 0;
2025
2026 msg = dpp_pkex_rx_exchange_resp(wpa_s->dpp_pkex, src, buf, len);
2027 if (!msg) {
2028 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
2029 return;
2030 }
2031
2032 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Request to " MACSTR,
2033 MAC2STR(src));
2034
2035 wait_time = wpa_s->max_remain_on_chan;
2036 if (wait_time > 2000)
2037 wait_time = 2000;
2038 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2039 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_REQ);
2040 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
2041 broadcast,
2042 wpabuf_head(msg), wpabuf_len(msg),
2043 wait_time, wpas_dpp_tx_pkex_status, 0);
2044 wpabuf_free(msg);
2045 }
2046
2047
2048 static struct dpp_bootstrap_info *
2049 wpas_dpp_pkex_finish(struct wpa_supplicant *wpa_s, const u8 *peer,
2050 unsigned int freq)
2051 {
2052 struct dpp_bootstrap_info *bi;
2053
2054 bi = dpp_pkex_finish(wpa_s->dpp, wpa_s->dpp_pkex, peer, freq);
2055 if (!bi)
2056 return NULL;
2057 wpa_s->dpp_pkex = NULL;
2058 return bi;
2059 }
2060
2061
2062 static void
2063 wpas_dpp_rx_pkex_commit_reveal_req(struct wpa_supplicant *wpa_s, const u8 *src,
2064 const u8 *hdr, const u8 *buf, size_t len,
2065 unsigned int freq)
2066 {
2067 struct wpabuf *msg;
2068 unsigned int wait_time;
2069 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
2070
2071 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Request from " MACSTR,
2072 MAC2STR(src));
2073
2074 if (!pkex || pkex->initiator || !pkex->exchange_done) {
2075 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2076 return;
2077 }
2078
2079 msg = dpp_pkex_rx_commit_reveal_req(pkex, hdr, buf, len);
2080 if (!msg) {
2081 wpa_printf(MSG_DEBUG, "DPP: Failed to process the request");
2082 if (pkex->failed) {
2083 wpa_printf(MSG_DEBUG, "DPP: Terminate PKEX exchange");
2084 if (pkex->t > pkex->own_bi->pkex_t)
2085 pkex->own_bi->pkex_t = pkex->t;
2086 dpp_pkex_free(wpa_s->dpp_pkex);
2087 wpa_s->dpp_pkex = NULL;
2088 }
2089 return;
2090 }
2091
2092 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Response to "
2093 MACSTR, MAC2STR(src));
2094
2095 wait_time = wpa_s->max_remain_on_chan;
2096 if (wait_time > 2000)
2097 wait_time = 2000;
2098 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2099 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_RESP);
2100 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
2101 broadcast,
2102 wpabuf_head(msg), wpabuf_len(msg),
2103 wait_time, wpas_dpp_tx_pkex_status, 0);
2104 wpabuf_free(msg);
2105
2106 wpas_dpp_pkex_finish(wpa_s, src, freq);
2107 }
2108
2109
2110 static void
2111 wpas_dpp_rx_pkex_commit_reveal_resp(struct wpa_supplicant *wpa_s, const u8 *src,
2112 const u8 *hdr, const u8 *buf, size_t len,
2113 unsigned int freq)
2114 {
2115 int res;
2116 struct dpp_bootstrap_info *bi;
2117 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
2118 char cmd[500];
2119
2120 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Response from " MACSTR,
2121 MAC2STR(src));
2122
2123 if (!pkex || !pkex->initiator || !pkex->exchange_done) {
2124 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2125 return;
2126 }
2127
2128 res = dpp_pkex_rx_commit_reveal_resp(pkex, hdr, buf, len);
2129 if (res < 0) {
2130 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
2131 return;
2132 }
2133
2134 bi = wpas_dpp_pkex_finish(wpa_s, src, freq);
2135 if (!bi)
2136 return;
2137
2138 os_snprintf(cmd, sizeof(cmd), " peer=%u %s",
2139 bi->id,
2140 wpa_s->dpp_pkex_auth_cmd ? wpa_s->dpp_pkex_auth_cmd : "");
2141 wpa_printf(MSG_DEBUG,
2142 "DPP: Start authentication after PKEX with parameters: %s",
2143 cmd);
2144 if (wpas_dpp_auth_init(wpa_s, cmd) < 0) {
2145 wpa_printf(MSG_DEBUG,
2146 "DPP: Authentication initialization failed");
2147 offchannel_send_action_done(wpa_s);
2148 return;
2149 }
2150 }
2151
2152
2153 void wpas_dpp_rx_action(struct wpa_supplicant *wpa_s, const u8 *src,
2154 const u8 *buf, size_t len, unsigned int freq)
2155 {
2156 u8 crypto_suite;
2157 enum dpp_public_action_frame_type type;
2158 const u8 *hdr;
2159 unsigned int pkex_t;
2160
2161 if (len < DPP_HDR_LEN)
2162 return;
2163 if (WPA_GET_BE24(buf) != OUI_WFA || buf[3] != DPP_OUI_TYPE)
2164 return;
2165 hdr = buf;
2166 buf += 4;
2167 len -= 4;
2168 crypto_suite = *buf++;
2169 type = *buf++;
2170 len -= 2;
2171
2172 wpa_printf(MSG_DEBUG,
2173 "DPP: Received DPP Public Action frame crypto suite %u type %d from "
2174 MACSTR " freq=%u",
2175 crypto_suite, type, MAC2STR(src), freq);
2176 if (crypto_suite != 1) {
2177 wpa_printf(MSG_DEBUG, "DPP: Unsupported crypto suite %u",
2178 crypto_suite);
2179 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2180 " freq=%u type=%d ignore=unsupported-crypto-suite",
2181 MAC2STR(src), freq, type);
2182 return;
2183 }
2184 wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes", buf, len);
2185 if (dpp_check_attrs(buf, len) < 0) {
2186 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2187 " freq=%u type=%d ignore=invalid-attributes",
2188 MAC2STR(src), freq, type);
2189 return;
2190 }
2191 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR " freq=%u type=%d",
2192 MAC2STR(src), freq, type);
2193
2194 switch (type) {
2195 case DPP_PA_AUTHENTICATION_REQ:
2196 wpas_dpp_rx_auth_req(wpa_s, src, hdr, buf, len, freq);
2197 break;
2198 case DPP_PA_AUTHENTICATION_RESP:
2199 wpas_dpp_rx_auth_resp(wpa_s, src, hdr, buf, len, freq);
2200 break;
2201 case DPP_PA_AUTHENTICATION_CONF:
2202 wpas_dpp_rx_auth_conf(wpa_s, src, hdr, buf, len);
2203 break;
2204 case DPP_PA_PEER_DISCOVERY_RESP:
2205 wpas_dpp_rx_peer_disc_resp(wpa_s, src, buf, len);
2206 break;
2207 case DPP_PA_PKEX_EXCHANGE_REQ:
2208 wpas_dpp_rx_pkex_exchange_req(wpa_s, src, buf, len, freq);
2209 break;
2210 case DPP_PA_PKEX_EXCHANGE_RESP:
2211 wpas_dpp_rx_pkex_exchange_resp(wpa_s, src, buf, len, freq);
2212 break;
2213 case DPP_PA_PKEX_COMMIT_REVEAL_REQ:
2214 wpas_dpp_rx_pkex_commit_reveal_req(wpa_s, src, hdr, buf, len,
2215 freq);
2216 break;
2217 case DPP_PA_PKEX_COMMIT_REVEAL_RESP:
2218 wpas_dpp_rx_pkex_commit_reveal_resp(wpa_s, src, hdr, buf, len,
2219 freq);
2220 break;
2221 #ifdef CONFIG_DPP2
2222 case DPP_PA_CONFIGURATION_RESULT:
2223 wpas_dpp_rx_conf_result(wpa_s, src, hdr, buf, len);
2224 break;
2225 case DPP_PA_CONNECTION_STATUS_RESULT:
2226 wpas_dpp_rx_conn_status_result(wpa_s, src, hdr, buf, len);
2227 break;
2228 #endif /* CONFIG_DPP2 */
2229 default:
2230 wpa_printf(MSG_DEBUG,
2231 "DPP: Ignored unsupported frame subtype %d", type);
2232 break;
2233 }
2234
2235 if (wpa_s->dpp_pkex)
2236 pkex_t = wpa_s->dpp_pkex->t;
2237 else if (wpa_s->dpp_pkex_bi)
2238 pkex_t = wpa_s->dpp_pkex_bi->pkex_t;
2239 else
2240 pkex_t = 0;
2241 if (pkex_t >= PKEX_COUNTER_T_LIMIT) {
2242 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_PKEX_T_LIMIT "id=0");
2243 wpas_dpp_pkex_remove(wpa_s, "*");
2244 }
2245 }
2246
2247
2248 static struct wpabuf *
2249 wpas_dpp_gas_req_handler(void *ctx, const u8 *sa, const u8 *query,
2250 size_t query_len)
2251 {
2252 struct wpa_supplicant *wpa_s = ctx;
2253 struct dpp_authentication *auth = wpa_s->dpp_auth;
2254 struct wpabuf *resp;
2255
2256 wpa_printf(MSG_DEBUG, "DPP: GAS request from " MACSTR,
2257 MAC2STR(sa));
2258 if (!auth || !auth->auth_success ||
2259 os_memcmp(sa, auth->peer_mac_addr, ETH_ALEN) != 0) {
2260 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
2261 return NULL;
2262 }
2263
2264 if (wpa_s->dpp_auth_ok_on_ack && auth->configurator) {
2265 wpa_printf(MSG_DEBUG,
2266 "DPP: Have not received ACK for Auth Confirm yet - assume it was received based on this GAS request");
2267 /* wpas_dpp_auth_success() would normally have been called from
2268 * TX status handler, but since there was no such handler call
2269 * yet, simply send out the event message and proceed with
2270 * exchange. */
2271 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=1");
2272 wpa_s->dpp_auth_ok_on_ack = 0;
2273 }
2274
2275 wpa_hexdump(MSG_DEBUG,
2276 "DPP: Received Configuration Request (GAS Query Request)",
2277 query, query_len);
2278 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_REQ_RX "src=" MACSTR,
2279 MAC2STR(sa));
2280 resp = dpp_conf_req_rx(auth, query, query_len);
2281 if (!resp)
2282 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
2283 auth->conf_resp = resp;
2284 return resp;
2285 }
2286
2287
2288 static void
2289 wpas_dpp_gas_status_handler(void *ctx, struct wpabuf *resp, int ok)
2290 {
2291 struct wpa_supplicant *wpa_s = ctx;
2292 struct dpp_authentication *auth = wpa_s->dpp_auth;
2293
2294 if (!auth) {
2295 wpabuf_free(resp);
2296 return;
2297 }
2298 if (auth->conf_resp != resp) {
2299 wpa_printf(MSG_DEBUG,
2300 "DPP: Ignore GAS status report (ok=%d) for unknown response",
2301 ok);
2302 wpabuf_free(resp);
2303 return;
2304 }
2305
2306 wpa_printf(MSG_DEBUG, "DPP: Configuration exchange completed (ok=%d)",
2307 ok);
2308 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
2309 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
2310 #ifdef CONFIG_DPP2
2311 if (ok && auth->peer_version >= 2 &&
2312 auth->conf_resp_status == DPP_STATUS_OK) {
2313 wpa_printf(MSG_DEBUG, "DPP: Wait for Configuration Result");
2314 auth->waiting_conf_result = 1;
2315 auth->conf_resp = NULL;
2316 wpabuf_free(resp);
2317 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout,
2318 wpa_s, NULL);
2319 eloop_register_timeout(2, 0,
2320 wpas_dpp_config_result_wait_timeout,
2321 wpa_s, NULL);
2322 return;
2323 }
2324 #endif /* CONFIG_DPP2 */
2325 offchannel_send_action_done(wpa_s);
2326 wpas_dpp_listen_stop(wpa_s);
2327 if (ok)
2328 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
2329 else
2330 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
2331 dpp_auth_deinit(wpa_s->dpp_auth);
2332 wpa_s->dpp_auth = NULL;
2333 wpabuf_free(resp);
2334 }
2335
2336
2337 int wpas_dpp_configurator_sign(struct wpa_supplicant *wpa_s, const char *cmd)
2338 {
2339 struct dpp_authentication *auth;
2340 int ret = -1;
2341 char *curve = NULL;
2342
2343 auth = os_zalloc(sizeof(*auth));
2344 if (!auth)
2345 return -1;
2346
2347 curve = get_param(cmd, " curve=");
2348 wpas_dpp_set_testing_options(wpa_s, auth);
2349 if (dpp_set_configurator(wpa_s->dpp, wpa_s, auth, cmd) == 0 &&
2350 dpp_configurator_own_config(auth, curve, 0) == 0)
2351 ret = wpas_dpp_handle_config_obj(wpa_s, auth,
2352 &auth->conf_obj[0]);
2353 if (!ret)
2354 wpas_dpp_post_process_config(wpa_s, auth);
2355
2356 dpp_auth_deinit(auth);
2357 os_free(curve);
2358
2359 return ret;
2360 }
2361
2362
2363 static void
2364 wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
2365 unsigned int freq, const u8 *dst,
2366 const u8 *src, const u8 *bssid,
2367 const u8 *data, size_t data_len,
2368 enum offchannel_send_action_result result)
2369 {
2370 const char *res_txt;
2371
2372 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
2373 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
2374 "FAILED");
2375 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
2376 " result=%s (DPP Peer Discovery Request)",
2377 freq, MAC2STR(dst), res_txt);
2378 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
2379 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
2380 /* TODO: Time out wait for response more quickly in error cases? */
2381 }
2382
2383
2384 int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2385 struct wpa_bss *bss)
2386 {
2387 struct os_time now;
2388 struct wpabuf *msg;
2389 unsigned int wait_time;
2390 const u8 *rsn;
2391 struct wpa_ie_data ied;
2392
2393 if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
2394 return 0; /* Not using DPP AKM - continue */
2395 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2396 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0 &&
2397 !(ied.key_mgmt & WPA_KEY_MGMT_DPP))
2398 return 0; /* AP does not support DPP AKM - continue */
2399 if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
2400 return 0; /* PMKSA exists for DPP AKM - continue */
2401
2402 if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
2403 !ssid->dpp_csign) {
2404 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2405 "missing %s",
2406 !ssid->dpp_connector ? "Connector" :
2407 (!ssid->dpp_netaccesskey ? "netAccessKey" :
2408 "C-sign-key"));
2409 return -1;
2410 }
2411
2412 os_get_time(&now);
2413
2414 if (ssid->dpp_netaccesskey_expiry &&
2415 (os_time_t) ssid->dpp_netaccesskey_expiry < now.sec) {
2416 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2417 "netAccessKey expired");
2418 return -1;
2419 }
2420
2421 wpa_printf(MSG_DEBUG,
2422 "DPP: Starting network introduction protocol to derive PMKSA for "
2423 MACSTR, MAC2STR(bss->bssid));
2424
2425 msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
2426 5 + 4 + os_strlen(ssid->dpp_connector));
2427 if (!msg)
2428 return -1;
2429
2430 #ifdef CONFIG_TESTING_OPTIONS
2431 if (dpp_test == DPP_TEST_NO_TRANSACTION_ID_PEER_DISC_REQ) {
2432 wpa_printf(MSG_INFO, "DPP: TESTING - no Transaction ID");
2433 goto skip_trans_id;
2434 }
2435 if (dpp_test == DPP_TEST_INVALID_TRANSACTION_ID_PEER_DISC_REQ) {
2436 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Transaction ID");
2437 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2438 wpabuf_put_le16(msg, 0);
2439 goto skip_trans_id;
2440 }
2441 #endif /* CONFIG_TESTING_OPTIONS */
2442
2443 /* Transaction ID */
2444 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2445 wpabuf_put_le16(msg, 1);
2446 wpabuf_put_u8(msg, TRANSACTION_ID);
2447
2448 #ifdef CONFIG_TESTING_OPTIONS
2449 skip_trans_id:
2450 if (dpp_test == DPP_TEST_NO_CONNECTOR_PEER_DISC_REQ) {
2451 wpa_printf(MSG_INFO, "DPP: TESTING - no Connector");
2452 goto skip_connector;
2453 }
2454 if (dpp_test == DPP_TEST_INVALID_CONNECTOR_PEER_DISC_REQ) {
2455 char *connector;
2456
2457 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Connector");
2458 connector = dpp_corrupt_connector_signature(
2459 ssid->dpp_connector);
2460 if (!connector) {
2461 wpabuf_free(msg);
2462 return -1;
2463 }
2464 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2465 wpabuf_put_le16(msg, os_strlen(connector));
2466 wpabuf_put_str(msg, connector);
2467 os_free(connector);
2468 goto skip_connector;
2469 }
2470 #endif /* CONFIG_TESTING_OPTIONS */
2471
2472 /* DPP Connector */
2473 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2474 wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
2475 wpabuf_put_str(msg, ssid->dpp_connector);
2476
2477 #ifdef CONFIG_TESTING_OPTIONS
2478 skip_connector:
2479 #endif /* CONFIG_TESTING_OPTIONS */
2480
2481 /* TODO: Timeout on AP response */
2482 wait_time = wpa_s->max_remain_on_chan;
2483 if (wait_time > 2000)
2484 wait_time = 2000;
2485 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2486 MAC2STR(bss->bssid), bss->freq, DPP_PA_PEER_DISCOVERY_REQ);
2487 offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
2488 broadcast,
2489 wpabuf_head(msg), wpabuf_len(msg),
2490 wait_time, wpas_dpp_tx_introduction_status, 0);
2491 wpabuf_free(msg);
2492
2493 /* Request this connection attempt to terminate - new one will be
2494 * started when network introduction protocol completes */
2495 os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
2496 wpa_s->dpp_intro_network = ssid;
2497 return 1;
2498 }
2499
2500
2501 int wpas_dpp_pkex_add(struct wpa_supplicant *wpa_s, const char *cmd)
2502 {
2503 struct dpp_bootstrap_info *own_bi;
2504 const char *pos, *end;
2505 unsigned int wait_time;
2506
2507 pos = os_strstr(cmd, " own=");
2508 if (!pos)
2509 return -1;
2510 pos += 5;
2511 own_bi = dpp_bootstrap_get_id(wpa_s->dpp, atoi(pos));
2512 if (!own_bi) {
2513 wpa_printf(MSG_DEBUG,
2514 "DPP: Identified bootstrap info not found");
2515 return -1;
2516 }
2517 if (own_bi->type != DPP_BOOTSTRAP_PKEX) {
2518 wpa_printf(MSG_DEBUG,
2519 "DPP: Identified bootstrap info not for PKEX");
2520 return -1;
2521 }
2522 wpa_s->dpp_pkex_bi = own_bi;
2523 own_bi->pkex_t = 0; /* clear pending errors on new code */
2524
2525 os_free(wpa_s->dpp_pkex_identifier);
2526 wpa_s->dpp_pkex_identifier = NULL;
2527 pos = os_strstr(cmd, " identifier=");
2528 if (pos) {
2529 pos += 12;
2530 end = os_strchr(pos, ' ');
2531 if (!end)
2532 return -1;
2533 wpa_s->dpp_pkex_identifier = os_malloc(end - pos + 1);
2534 if (!wpa_s->dpp_pkex_identifier)
2535 return -1;
2536 os_memcpy(wpa_s->dpp_pkex_identifier, pos, end - pos);
2537 wpa_s->dpp_pkex_identifier[end - pos] = '\0';
2538 }
2539
2540 pos = os_strstr(cmd, " code=");
2541 if (!pos)
2542 return -1;
2543 os_free(wpa_s->dpp_pkex_code);
2544 wpa_s->dpp_pkex_code = os_strdup(pos + 6);
2545 if (!wpa_s->dpp_pkex_code)
2546 return -1;
2547
2548 if (os_strstr(cmd, " init=1")) {
2549 struct dpp_pkex *pkex;
2550 struct wpabuf *msg;
2551
2552 wpa_printf(MSG_DEBUG, "DPP: Initiating PKEX");
2553 dpp_pkex_free(wpa_s->dpp_pkex);
2554 wpa_s->dpp_pkex = dpp_pkex_init(wpa_s, own_bi, wpa_s->own_addr,
2555 wpa_s->dpp_pkex_identifier,
2556 wpa_s->dpp_pkex_code);
2557 pkex = wpa_s->dpp_pkex;
2558 if (!pkex)
2559 return -1;
2560
2561 msg = pkex->exchange_req;
2562 wait_time = wpa_s->max_remain_on_chan;
2563 if (wait_time > 2000)
2564 wait_time = 2000;
2565 pkex->freq = 2437;
2566 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
2567 " freq=%u type=%d",
2568 MAC2STR(broadcast), pkex->freq,
2569 DPP_PA_PKEX_EXCHANGE_REQ);
2570 offchannel_send_action(wpa_s, pkex->freq, broadcast,
2571 wpa_s->own_addr, broadcast,
2572 wpabuf_head(msg), wpabuf_len(msg),
2573 wait_time, wpas_dpp_tx_pkex_status, 0);
2574 if (wait_time == 0)
2575 wait_time = 2000;
2576 pkex->exch_req_wait_time = wait_time;
2577 pkex->exch_req_tries = 1;
2578 }
2579
2580 /* TODO: Support multiple PKEX info entries */
2581
2582 os_free(wpa_s->dpp_pkex_auth_cmd);
2583 wpa_s->dpp_pkex_auth_cmd = os_strdup(cmd);
2584
2585 return 1;
2586 }
2587
2588
2589 int wpas_dpp_pkex_remove(struct wpa_supplicant *wpa_s, const char *id)
2590 {
2591 unsigned int id_val;
2592
2593 if (os_strcmp(id, "*") == 0) {
2594 id_val = 0;
2595 } else {
2596 id_val = atoi(id);
2597 if (id_val == 0)
2598 return -1;
2599 }
2600
2601 if ((id_val != 0 && id_val != 1) || !wpa_s->dpp_pkex_code)
2602 return -1;
2603
2604 /* TODO: Support multiple PKEX entries */
2605 os_free(wpa_s->dpp_pkex_code);
2606 wpa_s->dpp_pkex_code = NULL;
2607 os_free(wpa_s->dpp_pkex_identifier);
2608 wpa_s->dpp_pkex_identifier = NULL;
2609 os_free(wpa_s->dpp_pkex_auth_cmd);
2610 wpa_s->dpp_pkex_auth_cmd = NULL;
2611 wpa_s->dpp_pkex_bi = NULL;
2612 /* TODO: Remove dpp_pkex only if it is for the identified PKEX code */
2613 dpp_pkex_free(wpa_s->dpp_pkex);
2614 wpa_s->dpp_pkex = NULL;
2615 return 0;
2616 }
2617
2618
2619 void wpas_dpp_stop(struct wpa_supplicant *wpa_s)
2620 {
2621 if (wpa_s->dpp_auth || wpa_s->dpp_pkex)
2622 offchannel_send_action_done(wpa_s);
2623 dpp_auth_deinit(wpa_s->dpp_auth);
2624 wpa_s->dpp_auth = NULL;
2625 dpp_pkex_free(wpa_s->dpp_pkex);
2626 wpa_s->dpp_pkex = NULL;
2627 if (wpa_s->dpp_gas_client && wpa_s->dpp_gas_dialog_token >= 0)
2628 gas_query_stop(wpa_s->gas, wpa_s->dpp_gas_dialog_token);
2629 }
2630
2631
2632 int wpas_dpp_init(struct wpa_supplicant *wpa_s)
2633 {
2634 struct dpp_global_config config;
2635 u8 adv_proto_id[7];
2636
2637 adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
2638 adv_proto_id[1] = 5;
2639 WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
2640 adv_proto_id[5] = DPP_OUI_TYPE;
2641 adv_proto_id[6] = 0x01;
2642
2643 if (gas_server_register(wpa_s->gas_server, adv_proto_id,
2644 sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
2645 wpas_dpp_gas_status_handler, wpa_s) < 0)
2646 return -1;
2647
2648 os_memset(&config, 0, sizeof(config));
2649 config.msg_ctx = wpa_s;
2650 config.cb_ctx = wpa_s;
2651 #ifdef CONFIG_DPP2
2652 config.process_conf_obj = wpas_dpp_process_conf_obj;
2653 #endif /* CONFIG_DPP2 */
2654 wpa_s->dpp = dpp_global_init(&config);
2655 return wpa_s->dpp ? 0 : -1;
2656 }
2657
2658
2659 void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
2660 {
2661 #ifdef CONFIG_TESTING_OPTIONS
2662 os_free(wpa_s->dpp_config_obj_override);
2663 wpa_s->dpp_config_obj_override = NULL;
2664 os_free(wpa_s->dpp_discovery_override);
2665 wpa_s->dpp_discovery_override = NULL;
2666 os_free(wpa_s->dpp_groups_override);
2667 wpa_s->dpp_groups_override = NULL;
2668 wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
2669 #endif /* CONFIG_TESTING_OPTIONS */
2670 if (!wpa_s->dpp)
2671 return;
2672 dpp_global_clear(wpa_s->dpp);
2673 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
2674 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
2675 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
2676 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
2677 #ifdef CONFIG_DPP2
2678 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout, wpa_s, NULL);
2679 eloop_cancel_timeout(wpas_dpp_conn_status_result_wait_timeout,
2680 wpa_s, NULL);
2681 eloop_cancel_timeout(wpas_dpp_conn_status_result_timeout, wpa_s, NULL);
2682 dpp_pfs_free(wpa_s->dpp_pfs);
2683 wpa_s->dpp_pfs = NULL;
2684 #endif /* CONFIG_DPP2 */
2685 offchannel_send_action_done(wpa_s);
2686 wpas_dpp_listen_stop(wpa_s);
2687 wpas_dpp_stop(wpa_s);
2688 wpas_dpp_pkex_remove(wpa_s, "*");
2689 os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
2690 os_free(wpa_s->dpp_configurator_params);
2691 wpa_s->dpp_configurator_params = NULL;
2692 }
2693
2694
2695 #ifdef CONFIG_DPP2
2696 int wpas_dpp_controller_start(struct wpa_supplicant *wpa_s, const char *cmd)
2697 {
2698 struct dpp_controller_config config;
2699 const char *pos;
2700
2701 os_memset(&config, 0, sizeof(config));
2702 if (cmd) {
2703 pos = os_strstr(cmd, " tcp_port=");
2704 if (pos) {
2705 pos += 10;
2706 config.tcp_port = atoi(pos);
2707 }
2708 }
2709 config.configurator_params = wpa_s->dpp_configurator_params;
2710 return dpp_controller_start(wpa_s->dpp, &config);
2711 }
2712 #endif /* CONFIG_DPP2 */