]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/dpp_supplicant.c
DPP2: Chirping in wpa_supplicant Enrollee
[thirdparty/hostap.git] / wpa_supplicant / dpp_supplicant.c
CommitLineData
be27e185
JM
1/*
2 * wpa_supplicant - DPP
3 * Copyright (c) 2017, Qualcomm Atheros, Inc.
5e287724 4 * Copyright (c) 2018-2020, The Linux Foundation
be27e185
JM
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"
30d27b04 13#include "utils/eloop.h"
c02dd10d 14#include "utils/ip_addr.h"
be27e185 15#include "common/dpp.h"
461d39af
JM
16#include "common/gas.h"
17#include "common/gas_server.h"
a0d5c56f
JM
18#include "rsn_supp/wpa.h"
19#include "rsn_supp/pmksa_cache.h"
be27e185 20#include "wpa_supplicant_i.h"
a0d5c56f 21#include "config.h"
30d27b04
JM
22#include "driver_i.h"
23#include "offchannel.h"
461d39af 24#include "gas_query.h"
a0d5c56f
JM
25#include "bss.h"
26#include "scan.h"
8528994e 27#include "notify.h"
be27e185
JM
28#include "dpp_supplicant.h"
29
30
30d27b04
JM
31static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
32 unsigned int freq);
461d39af
JM
33static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx);
34static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator);
30d27b04
JM
35static 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);
f97ace34
JM
40static void wpas_dpp_init_timeout(void *eloop_ctx, void *timeout_ctx);
41static int wpas_dpp_auth_init_next(struct wpa_supplicant *wpa_s);
00d2d13d
JM
42static void
43wpas_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);
30d27b04
JM
48
49static const u8 broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
50
85fd8263
JM
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. */
53static const u8 TRANSACTION_ID = 1;
54
30d27b04 55
be27e185
JM
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 */
62int wpas_dpp_qr_code(struct wpa_supplicant *wpa_s, const char *cmd)
63{
64 struct dpp_bootstrap_info *bi;
30d27b04 65 struct dpp_authentication *auth = wpa_s->dpp_auth;
be27e185 66
87d8435c 67 bi = dpp_add_qr_code(wpa_s->dpp, cmd);
be27e185
JM
68 if (!bi)
69 return -1;
70
30d27b04
JM
71 if (auth && auth->response_pending &&
72 dpp_notify_new_qr_code(auth, bi) == 1) {
30d27b04
JM
73 wpa_printf(MSG_DEBUG,
74 "DPP: Sending out pending authentication response");
af48810b
JM
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);
30d27b04
JM
79 offchannel_send_action(wpa_s, auth->curr_freq,
80 auth->peer_mac_addr, wpa_s->own_addr,
81 broadcast,
dc4d271c
JM
82 wpabuf_head(auth->resp_msg),
83 wpabuf_len(auth->resp_msg),
30d27b04 84 500, wpas_dpp_tx_status, 0);
30d27b04
JM
85 }
86
be27e185
JM
87 return bi->id;
88}
89
90
e780b4bf
JM
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 */
97int 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
5e287724
JM
109int 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
140int 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
95b0104a
JM
174static 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
196static 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
ed2c493e 220 wait_time = 1000;
95b0104a
JM
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
22f90b32
JM
231static 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");
16ef233b
JM
234 wpa_s->suitable_network = 0;
235 wpa_s->no_suitable_network = 0;
22f90b32
JM
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
16ef233b
JM
245#ifdef CONFIG_DPP2
246
247static 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? */
9be30ffc
JM
265 if (wpa_s->wpa_state == WPA_SCANNING)
266 wpas_abort_ongoing_scan(wpa_s);
16ef233b
JM
267 wpas_dpp_send_conn_status_result(wpa_s, result);
268}
269
270
271static 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
319void 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
371void 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
30d27b04
JM
382static 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{
af48810b 388 const char *res_txt;
f97ace34 389 struct dpp_authentication *auth = wpa_s->dpp_auth;
af48810b
JM
390
391 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
392 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
393 "FAILED");
30d27b04 394 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
af48810b
JM
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);
30d27b04
JM
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
22f90b32
JM
405#ifdef CONFIG_DPP2
406 if (auth->connect_on_tx_status) {
16ef233b 407 auth->connect_on_tx_status = 0;
22f90b32
JM
408 wpa_printf(MSG_DEBUG,
409 "DPP: Try to connect after completed configuration result");
410 wpas_dpp_try_to_connect(wpa_s);
16ef233b
JM
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 }
22f90b32
JM
424 return;
425 }
426#endif /* CONFIG_DPP2 */
427
30d27b04
JM
428 if (wpa_s->dpp_auth->remove_on_tx_status) {
429 wpa_printf(MSG_DEBUG,
16ef233b 430 "DPP: Terminate authentication exchange due to a request to do so on TX status");
f97ace34 431 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
461d39af 432 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
95b0104a
JM
433 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
434 NULL);
461d39af 435 offchannel_send_action_done(wpa_s);
30d27b04
JM
436 dpp_auth_deinit(wpa_s->dpp_auth);
437 wpa_s->dpp_auth = NULL;
438 return;
439 }
440
461d39af
JM
441 if (wpa_s->dpp_auth_ok_on_ack)
442 wpas_dpp_auth_success(wpa_s, 1);
443
30d27b04
JM
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");
f97ace34
JM
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 }
95b0104a
JM
455 if (auth->waiting_auth_conf) {
456 wpas_dpp_auth_resp_retry(wpa_s);
457 return;
458 }
30d27b04 459 }
d2709206 460
248264c6
JM
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
d2709206
JM
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 }
d3cb7ebe
JM
477
478 if (wpa_s->dpp_auth_ok_on_ack)
479 wpa_s->dpp_auth_ok_on_ack = 0;
30d27b04
JM
480}
481
482
483static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx)
484{
485 struct wpa_supplicant *wpa_s = eloop_ctx;
248264c6 486 struct dpp_authentication *auth = wpa_s->dpp_auth;
d2709206 487 unsigned int freq;
0db637ca
JM
488 struct os_reltime now, diff;
489 unsigned int wait_time, diff_ms;
30d27b04 490
0db637ca 491 if (!auth || !auth->waiting_auth_resp)
30d27b04 492 return;
f97ace34 493
0db637ca
JM
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. */
248264c6
JM
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);
0db637ca 510 wpas_dpp_listen_stop(wpa_s);
248264c6
JM
511 dpp_auth_deinit(auth);
512 wpa_s->dpp_auth = NULL;
513 return;
514 }
515
0db637ca
JM
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;
f97ace34
JM
525 }
526
0db637ca
JM
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
248264c6
JM
535 freq = auth->curr_freq;
536 if (auth->neg_freq > 0)
537 freq = auth->neg_freq;
0db637ca
JM
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;
d2709206 542 wpas_dpp_listen_start(wpa_s, freq);
0db637ca
JM
543
544 eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
545 wpas_dpp_reply_wait_timeout, wpa_s, NULL);
30d27b04
JM
546}
547
548
461d39af
JM
549static 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);
461d39af
JM
562 auth->ignore_netaccesskey_mismatch =
563 wpa_s->dpp_ignore_netaccesskey_mismatch;
564#endif /* CONFIG_TESTING_OPTIONS */
565}
566
567
f97ace34
JM
568static 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
579static 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;
0db637ca 583 unsigned int wait_time, max_wait_time, freq, max_tries, used;
921f5acd 584 struct os_reltime now, diff;
f97ace34 585
0db637ca 586 wpa_s->dpp_in_response_listen = 0;
f97ace34
JM
587 if (!auth)
588 return -1;
0db637ca
JM
589
590 if (auth->freq_idx == 0)
591 os_get_reltime(&wpa_s->dpp_init_iter_start);
592
f97ace34
JM
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;
248264c6 599 if (auth->num_freq_iters >= max_tries || auth->auth_req_ack) {
f97ace34
JM
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;
921f5acd 616 os_get_reltime(&now);
0db637ca 617 os_reltime_sub(&now, &wpa_s->dpp_init_iter_start, &diff);
921f5acd
JM
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);
f97ace34
JM
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;
0db637ca
JM
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 */
f97ace34
JM
646 eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
647 wpas_dpp_reply_wait_timeout,
648 wpa_s, NULL);
0db637ca 649 wait_time -= 10;
f97ace34
JM
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);
248264c6 657 auth->auth_req_ack = 0;
f97ace34
JM
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
b65b22d6
JM
667int 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;
2a5a0680 671 struct dpp_authentication *auth;
d1f08264 672 u8 allowed_roles = DPP_CAPAB_CONFIGURATOR;
d2709206 673 unsigned int neg_freq = 0;
2a5a0680
JM
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 */
b65b22d6
JM
680
681 wpa_s->dpp_gas_client = 0;
682
683 pos = os_strstr(cmd, " peer=");
684 if (!pos)
685 return -1;
686 pos += 6;
87d8435c 687 peer_bi = dpp_bootstrap_get_id(wpa_s->dpp, atoi(pos));
b65b22d6
JM
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
2a5a0680
JM
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
b65b22d6
JM
713 pos = os_strstr(cmd, " own=");
714 if (pos) {
715 pos += 5;
87d8435c 716 own_bi = dpp_bootstrap_get_id(wpa_s->dpp, atoi(pos));
b65b22d6
JM
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)
d1f08264 735 allowed_roles = DPP_CAPAB_CONFIGURATOR;
b65b22d6 736 else if (os_strncmp(pos, "enrollee", 8) == 0)
d1f08264
JM
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;
b65b22d6
JM
741 else
742 goto fail;
743 }
744
745 pos = os_strstr(cmd, " netrole=");
746 if (pos) {
747 pos += 9;
74045744
JM
748 if (os_strncmp(pos, "ap", 2) == 0)
749 wpa_s->dpp_netrole = DPP_NETROLE_AP;
5661ebd7
JM
750 else if (os_strncmp(pos, "configurator", 12) == 0)
751 wpa_s->dpp_netrole = DPP_NETROLE_CONFIGURATOR;
74045744
JM
752 else
753 wpa_s->dpp_netrole = DPP_NETROLE_STA;
c7cc80fb
JM
754 } else {
755 wpa_s->dpp_netrole = DPP_NETROLE_STA;
b65b22d6 756 }
30d27b04 757
d2709206
JM
758 pos = os_strstr(cmd, " neg_freq=");
759 if (pos)
760 neg_freq = atoi(pos + 10);
761
2a5a0680 762 if (!tcp && wpa_s->dpp_auth) {
f97ace34 763 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
30d27b04 764 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
95b0104a
JM
765 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
766 NULL);
30d27b04
JM
767 offchannel_send_action_done(wpa_s);
768 dpp_auth_deinit(wpa_s->dpp_auth);
2a5a0680 769 wpa_s->dpp_auth = NULL;
30d27b04 770 }
2a5a0680 771
514cc49b
JM
772 auth = dpp_auth_init(wpa_s->dpp, wpa_s, peer_bi, own_bi, allowed_roles,
773 neg_freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
2a5a0680 774 if (!auth)
461d39af 775 goto fail;
2a5a0680 776 wpas_dpp_set_testing_options(wpa_s, auth);
514cc49b 777 if (dpp_set_configurator(auth, cmd) < 0) {
2a5a0680 778 dpp_auth_deinit(auth);
a00d65e7
JM
779 goto fail;
780 }
30d27b04 781
2a5a0680 782 auth->neg_freq = neg_freq;
30d27b04 783
f97ace34 784 if (!is_zero_ether_addr(peer_bi->mac_addr))
2a5a0680
JM
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 */
f97ace34 791
2a5a0680 792 wpa_s->dpp_auth = auth;
f97ace34 793 return wpas_dpp_auth_init_next(wpa_s);
461d39af 794fail:
461d39af 795 return -1;
30d27b04
JM
796}
797
798
799struct wpas_dpp_listen_work {
800 unsigned int freq;
801 unsigned int duration;
802 struct wpabuf *probe_resp_ie;
803};
804
805
806static 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
814static 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
828static 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);
d8c20ec5 851 wpa_s->dpp_listen_freq = 0;
30d27b04
JM
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
861static 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
891int 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;
74045744
JM
907 if (os_strstr(cmd, " netrole=ap"))
908 wpa_s->dpp_netrole = DPP_NETROLE_AP;
5661ebd7
JM
909 else if (os_strstr(cmd, " netrole=configurator"))
910 wpa_s->dpp_netrole = DPP_NETROLE_CONFIGURATOR;
74045744
JM
911 else
912 wpa_s->dpp_netrole = DPP_NETROLE_STA;
30d27b04
JM
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
923void wpas_dpp_listen_stop(struct wpa_supplicant *wpa_s)
924{
0db637ca 925 wpa_s->dpp_in_response_listen = 0;
30d27b04
JM
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
30d27b04
JM
937void 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
0db637ca
JM
942 if (wpa_s->dpp_auth && wpa_s->dpp_in_response_listen) {
943 unsigned int new_freq;
944
30d27b04 945 /* Continue listen with a new remain-on-channel */
0db637ca
JM
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;
30d27b04
JM
950 wpa_printf(MSG_DEBUG,
951 "DPP: Continue wait on %u MHz for the ongoing DPP provisioning session",
0db637ca
JM
952 new_freq);
953 wpas_dpp_listen_start(wpa_s, new_freq);
30d27b04
JM
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
964static void wpas_dpp_rx_auth_req(struct wpa_supplicant *wpa_s, const u8 *src,
dc4d271c
JM
965 const u8 *hdr, const u8 *buf, size_t len,
966 unsigned int freq)
30d27b04 967{
27fefbbb
JM
968 const u8 *r_bootstrap, *i_bootstrap;
969 u16 r_bootstrap_len, i_bootstrap_len;
87d8435c
JM
970 struct dpp_bootstrap_info *own_bi = NULL, *peer_bi = NULL;
971
972 if (!wpa_s->dpp)
973 return;
30d27b04
JM
974
975 wpa_printf(MSG_DEBUG, "DPP: Authentication Request from " MACSTR,
976 MAC2STR(src));
977
562f7714
JM
978#ifdef CONFIG_DPP2
979 wpas_dpp_chirp_stop(wpa_s);
980#endif /* CONFIG_DPP2 */
981
30d27b04
JM
982 r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
983 &r_bootstrap_len);
27fefbbb 984 if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
26806abe
JM
985 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
986 "Missing or invalid required Responder Bootstrapping Key Hash attribute");
30d27b04
JM
987 return;
988 }
989 wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
990 r_bootstrap, r_bootstrap_len);
991
992 i_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
993 &i_bootstrap_len);
27fefbbb 994 if (!i_bootstrap || i_bootstrap_len != SHA256_MAC_LEN) {
26806abe
JM
995 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
996 "Missing or invalid required Initiator Bootstrapping Key Hash attribute");
30d27b04
JM
997 return;
998 }
999 wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Bootstrapping Key Hash",
1000 i_bootstrap, i_bootstrap_len);
1001
1002 /* Try to find own and peer bootstrapping key matches based on the
1003 * received hash values */
87d8435c
JM
1004 dpp_bootstrap_find_pair(wpa_s->dpp, i_bootstrap, r_bootstrap,
1005 &own_bi, &peer_bi);
30d27b04 1006 if (!own_bi) {
26806abe
JM
1007 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1008 "No matching own bootstrapping key found - ignore message");
30d27b04
JM
1009 return;
1010 }
1011
1012 if (wpa_s->dpp_auth) {
26806abe
JM
1013 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1014 "Already in DPP authentication exchange - ignore new one");
30d27b04
JM
1015 return;
1016 }
1017
461d39af
JM
1018 wpa_s->dpp_gas_client = 0;
1019 wpa_s->dpp_auth_ok_on_ack = 0;
514cc49b
JM
1020 wpa_s->dpp_auth = dpp_auth_req_rx(wpa_s->dpp, wpa_s,
1021 wpa_s->dpp_allowed_roles,
30d27b04 1022 wpa_s->dpp_qr_mutual,
27fefbbb 1023 peer_bi, own_bi, freq, hdr, buf, len);
30d27b04
JM
1024 if (!wpa_s->dpp_auth) {
1025 wpa_printf(MSG_DEBUG, "DPP: No response generated");
1026 return;
1027 }
461d39af 1028 wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
514cc49b 1029 if (dpp_set_configurator(wpa_s->dpp_auth,
87d8435c 1030 wpa_s->dpp_configurator_params) < 0) {
a00d65e7
JM
1031 dpp_auth_deinit(wpa_s->dpp_auth);
1032 wpa_s->dpp_auth = NULL;
1033 return;
1034 }
30d27b04
JM
1035 os_memcpy(wpa_s->dpp_auth->peer_mac_addr, src, ETH_ALEN);
1036
d2709206
JM
1037 if (wpa_s->dpp_listen_freq &&
1038 wpa_s->dpp_listen_freq != wpa_s->dpp_auth->curr_freq) {
1039 wpa_printf(MSG_DEBUG,
1040 "DPP: Stop listen on %u MHz to allow response on the request %u MHz",
1041 wpa_s->dpp_listen_freq, wpa_s->dpp_auth->curr_freq);
1042 wpas_dpp_listen_stop(wpa_s);
1043 }
1044
af48810b
JM
1045 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1046 MAC2STR(src), wpa_s->dpp_auth->curr_freq,
1047 DPP_PA_AUTHENTICATION_RESP);
30d27b04
JM
1048 offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
1049 src, wpa_s->own_addr, broadcast,
dc4d271c
JM
1050 wpabuf_head(wpa_s->dpp_auth->resp_msg),
1051 wpabuf_len(wpa_s->dpp_auth->resp_msg),
30d27b04 1052 500, wpas_dpp_tx_status, 0);
30d27b04
JM
1053}
1054
1055
461d39af
JM
1056static void wpas_dpp_start_gas_server(struct wpa_supplicant *wpa_s)
1057{
1058 /* TODO: stop wait and start ROC */
1059}
1060
1061
8528994e 1062static struct wpa_ssid * wpas_dpp_add_network(struct wpa_supplicant *wpa_s,
52d469de
JM
1063 struct dpp_authentication *auth,
1064 struct dpp_config_obj *conf)
8528994e
JM
1065{
1066 struct wpa_ssid *ssid;
1067
22f90b32 1068#ifdef CONFIG_DPP2
52d469de 1069 if (conf->akm == DPP_AKM_SAE) {
22f90b32
JM
1070#ifdef CONFIG_SAE
1071 struct wpa_driver_capa capa;
1072 int res;
1073
1074 res = wpa_drv_get_capa(wpa_s, &capa);
1075 if (res == 0 &&
1076 !(capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE) &&
1077 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
1078 wpa_printf(MSG_DEBUG,
1079 "DPP: SAE not supported by the driver");
1080 return NULL;
1081 }
1082#else /* CONFIG_SAE */
1083 wpa_printf(MSG_DEBUG, "DPP: SAE not supported in the build");
1084 return NULL;
1085#endif /* CONFIG_SAE */
1086 }
1087#endif /* CONFIG_DPP2 */
1088
8528994e
JM
1089 ssid = wpa_config_add_network(wpa_s->conf);
1090 if (!ssid)
1091 return NULL;
1092 wpas_notify_network_added(wpa_s, ssid);
1093 wpa_config_set_network_defaults(ssid);
1094 ssid->disabled = 1;
1095
52d469de 1096 ssid->ssid = os_malloc(conf->ssid_len);
8528994e
JM
1097 if (!ssid->ssid)
1098 goto fail;
52d469de
JM
1099 os_memcpy(ssid->ssid, conf->ssid, conf->ssid_len);
1100 ssid->ssid_len = conf->ssid_len;
8528994e 1101
52d469de 1102 if (conf->connector) {
8528994e 1103 ssid->key_mgmt = WPA_KEY_MGMT_DPP;
70e19013 1104 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
52d469de 1105 ssid->dpp_connector = os_strdup(conf->connector);
8528994e
JM
1106 if (!ssid->dpp_connector)
1107 goto fail;
1108 }
1109
52d469de
JM
1110 if (conf->c_sign_key) {
1111 ssid->dpp_csign = os_malloc(wpabuf_len(conf->c_sign_key));
8528994e
JM
1112 if (!ssid->dpp_csign)
1113 goto fail;
52d469de
JM
1114 os_memcpy(ssid->dpp_csign, wpabuf_head(conf->c_sign_key),
1115 wpabuf_len(conf->c_sign_key));
1116 ssid->dpp_csign_len = wpabuf_len(conf->c_sign_key);
8528994e
JM
1117 }
1118
1119 if (auth->net_access_key) {
1120 ssid->dpp_netaccesskey =
1121 os_malloc(wpabuf_len(auth->net_access_key));
1122 if (!ssid->dpp_netaccesskey)
1123 goto fail;
1124 os_memcpy(ssid->dpp_netaccesskey,
1125 wpabuf_head(auth->net_access_key),
1126 wpabuf_len(auth->net_access_key));
1127 ssid->dpp_netaccesskey_len = wpabuf_len(auth->net_access_key);
1128 ssid->dpp_netaccesskey_expiry = auth->net_access_key_expiry;
1129 }
1130
52d469de
JM
1131 if (!conf->connector || dpp_akm_psk(conf->akm) ||
1132 dpp_akm_sae(conf->akm)) {
1133 if (!conf->connector)
18015fc8 1134 ssid->key_mgmt = 0;
52d469de 1135 if (dpp_akm_psk(conf->akm))
5dd745b7
JM
1136 ssid->key_mgmt |= WPA_KEY_MGMT_PSK |
1137 WPA_KEY_MGMT_PSK_SHA256 | WPA_KEY_MGMT_FT_PSK;
52d469de 1138 if (dpp_akm_sae(conf->akm))
5dd745b7
JM
1139 ssid->key_mgmt |= WPA_KEY_MGMT_SAE |
1140 WPA_KEY_MGMT_FT_SAE;
70e19013 1141 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
52d469de 1142 if (conf->passphrase[0]) {
8528994e 1143 if (wpa_config_set_quoted(ssid, "psk",
52d469de 1144 conf->passphrase) < 0)
8528994e
JM
1145 goto fail;
1146 wpa_config_update_psk(ssid);
1147 ssid->export_keys = 1;
1148 } else {
52d469de
JM
1149 ssid->psk_set = conf->psk_set;
1150 os_memcpy(ssid->psk, conf->psk, PMK_LEN);
8528994e
JM
1151 }
1152 }
1153
52d469de
JM
1154 os_memcpy(wpa_s->dpp_last_ssid, conf->ssid, conf->ssid_len);
1155 wpa_s->dpp_last_ssid_len = conf->ssid_len;
16ef233b 1156
8528994e
JM
1157 return ssid;
1158fail:
1159 wpas_notify_network_removed(wpa_s, ssid);
1160 wpa_config_remove_network(wpa_s->conf, ssid->id);
1161 return NULL;
1162}
1163
1164
22f90b32 1165static int wpas_dpp_process_config(struct wpa_supplicant *wpa_s,
52d469de
JM
1166 struct dpp_authentication *auth,
1167 struct dpp_config_obj *conf)
8528994e
JM
1168{
1169 struct wpa_ssid *ssid;
1170
1171 if (wpa_s->conf->dpp_config_processing < 1)
22f90b32 1172 return 0;
8528994e 1173
52d469de 1174 ssid = wpas_dpp_add_network(wpa_s, auth, conf);
8528994e 1175 if (!ssid)
22f90b32 1176 return -1;
8528994e
JM
1177
1178 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_NETWORK_ID "%d", ssid->id);
954c535a
JM
1179 if (wpa_s->conf->dpp_config_processing == 2)
1180 ssid->disabled = 0;
1181
1182#ifndef CONFIG_NO_CONFIG_WRITE
1183 if (wpa_s->conf->update_config &&
1184 wpa_config_write(wpa_s->confname, wpa_s->conf))
1185 wpa_printf(MSG_DEBUG, "DPP: Failed to update configuration");
1186#endif /* CONFIG_NO_CONFIG_WRITE */
1187
52d469de
JM
1188 return 0;
1189}
1190
1191
1192static void wpas_dpp_post_process_config(struct wpa_supplicant *wpa_s,
1193 struct dpp_authentication *auth)
1194{
8528994e 1195 if (wpa_s->conf->dpp_config_processing < 2)
52d469de 1196 return;
8528994e 1197
22f90b32
JM
1198#ifdef CONFIG_DPP2
1199 if (auth->peer_version >= 2) {
1200 wpa_printf(MSG_DEBUG,
1201 "DPP: Postpone connection attempt to wait for completion of DPP Configuration Result");
1202 auth->connect_on_tx_status = 1;
52d469de 1203 return;
22f90b32
JM
1204 }
1205#endif /* CONFIG_DPP2 */
1206
1207 wpas_dpp_try_to_connect(wpa_s);
8528994e
JM
1208}
1209
1210
22f90b32 1211static int wpas_dpp_handle_config_obj(struct wpa_supplicant *wpa_s,
52d469de
JM
1212 struct dpp_authentication *auth,
1213 struct dpp_config_obj *conf)
461d39af 1214{
461d39af 1215 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_RECEIVED);
52d469de 1216 if (conf->ssid_len)
461d39af 1217 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFOBJ_SSID "%s",
52d469de 1218 wpa_ssid_txt(conf->ssid, conf->ssid_len));
181bf933
JM
1219 if (conf->ssid_charset)
1220 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFOBJ_SSID_CHARSET "%d",
1221 conf->ssid_charset);
52d469de 1222 if (conf->connector) {
461d39af
JM
1223 /* TODO: Save the Connector and consider using a command
1224 * to fetch the value instead of sending an event with
1225 * it. The Connector could end up being larger than what
1226 * most clients are ready to receive as an event
1227 * message. */
1228 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONNECTOR "%s",
52d469de 1229 conf->connector);
461d39af 1230 }
52d469de 1231 if (conf->c_sign_key) {
461d39af
JM
1232 char *hex;
1233 size_t hexlen;
1234
52d469de 1235 hexlen = 2 * wpabuf_len(conf->c_sign_key) + 1;
461d39af
JM
1236 hex = os_malloc(hexlen);
1237 if (hex) {
1238 wpa_snprintf_hex(hex, hexlen,
52d469de
JM
1239 wpabuf_head(conf->c_sign_key),
1240 wpabuf_len(conf->c_sign_key));
c77e2ff0
JM
1241 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_C_SIGN_KEY "%s",
1242 hex);
461d39af
JM
1243 os_free(hex);
1244 }
1245 }
1246 if (auth->net_access_key) {
1247 char *hex;
1248 size_t hexlen;
1249
1250 hexlen = 2 * wpabuf_len(auth->net_access_key) + 1;
1251 hex = os_malloc(hexlen);
1252 if (hex) {
1253 wpa_snprintf_hex(hex, hexlen,
1254 wpabuf_head(auth->net_access_key),
1255 wpabuf_len(auth->net_access_key));
1256 if (auth->net_access_key_expiry)
1257 wpa_msg(wpa_s, MSG_INFO,
1258 DPP_EVENT_NET_ACCESS_KEY "%s %lu", hex,
1259 (long unsigned)
1260 auth->net_access_key_expiry);
1261 else
1262 wpa_msg(wpa_s, MSG_INFO,
1263 DPP_EVENT_NET_ACCESS_KEY "%s", hex);
1264 os_free(hex);
1265 }
1266 }
8528994e 1267
52d469de 1268 return wpas_dpp_process_config(wpa_s, auth, conf);
f522bb23
JM
1269}
1270
1271
7d9e3200
JM
1272static int wpas_dpp_handle_key_pkg(struct wpa_supplicant *wpa_s,
1273 struct dpp_asymmetric_key *key)
1274{
1275#ifdef CONFIG_DPP2
1276 int res;
1277
1278 if (!key)
1279 return 0;
1280
1281 wpa_printf(MSG_DEBUG, "DPP: Received Configurator backup");
1282 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_RECEIVED);
1283
1284 while (key) {
1285 res = dpp_configurator_from_backup(wpa_s->dpp, key);
1286 if (res < 0)
1287 return -1;
1288 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFIGURATOR_ID "%d",
1289 res);
1290 key = key->next;
1291 }
1292#endif /* CONFIG_DPP2 */
1293
1294 return 0;
1295}
1296
1297
f522bb23
JM
1298static void wpas_dpp_gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
1299 enum gas_query_result result,
1300 const struct wpabuf *adv_proto,
1301 const struct wpabuf *resp, u16 status_code)
1302{
1303 struct wpa_supplicant *wpa_s = ctx;
1304 const u8 *pos;
1305 struct dpp_authentication *auth = wpa_s->dpp_auth;
22f90b32
JM
1306 int res;
1307 enum dpp_status_error status = DPP_STATUS_CONFIG_REJECTED;
52d469de 1308 unsigned int i;
f522bb23 1309
1866dfb5
JM
1310 wpa_s->dpp_gas_dialog_token = -1;
1311
f522bb23
JM
1312 if (!auth || !auth->auth_success) {
1313 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
1314 return;
1315 }
931f7ff6
JM
1316 if (result != GAS_QUERY_SUCCESS ||
1317 !resp || status_code != WLAN_STATUS_SUCCESS) {
f522bb23
JM
1318 wpa_printf(MSG_DEBUG, "DPP: GAS query did not succeed");
1319 goto fail;
1320 }
1321
1322 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response adv_proto",
1323 adv_proto);
1324 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response (GAS response)",
1325 resp);
1326
1327 if (wpabuf_len(adv_proto) != 10 ||
1328 !(pos = wpabuf_head(adv_proto)) ||
1329 pos[0] != WLAN_EID_ADV_PROTO ||
1330 pos[1] != 8 ||
1331 pos[3] != WLAN_EID_VENDOR_SPECIFIC ||
1332 pos[4] != 5 ||
1333 WPA_GET_BE24(&pos[5]) != OUI_WFA ||
1334 pos[8] != 0x1a ||
1335 pos[9] != 1) {
1336 wpa_printf(MSG_DEBUG,
1337 "DPP: Not a DPP Advertisement Protocol ID");
1338 goto fail;
1339 }
8528994e 1340
f522bb23
JM
1341 if (dpp_conf_resp_rx(auth, resp) < 0) {
1342 wpa_printf(MSG_DEBUG, "DPP: Configuration attempt failed");
1343 goto fail;
1344 }
1345
52d469de
JM
1346 for (i = 0; i < auth->num_conf_obj; i++) {
1347 res = wpas_dpp_handle_config_obj(wpa_s, auth,
1348 &auth->conf_obj[i]);
1349 if (res < 0)
1350 goto fail;
1351 }
1352 if (auth->num_conf_obj)
1353 wpas_dpp_post_process_config(wpa_s, auth);
7d9e3200
JM
1354 if (wpas_dpp_handle_key_pkg(wpa_s, auth->conf_key_pkg) < 0)
1355 goto fail;
461d39af 1356
22f90b32 1357 status = DPP_STATUS_OK;
67b3bcc9
JM
1358#ifdef CONFIG_TESTING_OPTIONS
1359 if (dpp_test == DPP_TEST_REJECT_CONFIG) {
1360 wpa_printf(MSG_INFO, "DPP: TESTING - Reject Config Object");
1361 status = DPP_STATUS_CONFIG_REJECTED;
1362 }
1363#endif /* CONFIG_TESTING_OPTIONS */
461d39af 1364fail:
22f90b32
JM
1365 if (status != DPP_STATUS_OK)
1366 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1367#ifdef CONFIG_DPP2
1368 if (auth->peer_version >= 2 &&
1369 auth->conf_resp_status == DPP_STATUS_OK) {
1370 struct wpabuf *msg;
1371
1372 wpa_printf(MSG_DEBUG, "DPP: Send DPP Configuration Result");
1373 msg = dpp_build_conf_result(auth, status);
1374 if (!msg)
1375 goto fail2;
1376
1377 wpa_msg(wpa_s, MSG_INFO,
1378 DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1379 MAC2STR(addr), auth->curr_freq,
1380 DPP_PA_CONFIGURATION_RESULT);
1381 offchannel_send_action(wpa_s, auth->curr_freq,
1382 addr, wpa_s->own_addr, broadcast,
1383 wpabuf_head(msg),
1384 wpabuf_len(msg),
1385 500, wpas_dpp_tx_status, 0);
1386 wpabuf_free(msg);
1387
1388 /* This exchange will be terminated in the TX status handler */
1389 return;
1390 }
1391fail2:
1392#endif /* CONFIG_DPP2 */
461d39af
JM
1393 dpp_auth_deinit(wpa_s->dpp_auth);
1394 wpa_s->dpp_auth = NULL;
1395}
1396
1397
1398static void wpas_dpp_start_gas_client(struct wpa_supplicant *wpa_s)
1399{
1400 struct dpp_authentication *auth = wpa_s->dpp_auth;
bd23daa8 1401 struct wpabuf *buf;
461d39af 1402 int res;
8f8c423a 1403 int *supp_op_classes;
461d39af
JM
1404
1405 wpa_s->dpp_gas_client = 1;
461d39af
JM
1406 offchannel_send_action_done(wpa_s);
1407 wpas_dpp_listen_stop(wpa_s);
1408
8f8c423a 1409 supp_op_classes = wpas_supp_op_classes(wpa_s);
5a5639b0 1410 buf = dpp_build_conf_req_helper(auth, wpa_s->conf->dpp_name,
74045744 1411 wpa_s->dpp_netrole,
8f8c423a
JM
1412 wpa_s->conf->dpp_mud_url,
1413 supp_op_classes);
1414 os_free(supp_op_classes);
bd23daa8 1415 if (!buf) {
461d39af
JM
1416 wpa_printf(MSG_DEBUG,
1417 "DPP: No configuration request data available");
1418 return;
1419 }
1420
461d39af
JM
1421 wpa_printf(MSG_DEBUG, "DPP: GAS request to " MACSTR " (freq %u MHz)",
1422 MAC2STR(auth->peer_mac_addr), auth->curr_freq);
1423
1424 res = gas_query_req(wpa_s->gas, auth->peer_mac_addr, auth->curr_freq,
aca4d84e 1425 1, buf, wpas_dpp_gas_resp_cb, wpa_s);
461d39af
JM
1426 if (res < 0) {
1427 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
1428 wpabuf_free(buf);
1429 } else {
1430 wpa_printf(MSG_DEBUG,
1431 "DPP: GAS query started with dialog token %u", res);
1866dfb5 1432 wpa_s->dpp_gas_dialog_token = res;
461d39af
JM
1433 }
1434}
1435
1436
1437static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator)
1438{
1439 wpa_printf(MSG_DEBUG, "DPP: Authentication succeeded");
1440 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=%d", initiator);
d74963d4
SD
1441#ifdef CONFIG_TESTING_OPTIONS
1442 if (dpp_test == DPP_TEST_STOP_AT_AUTH_CONF) {
1443 wpa_printf(MSG_INFO,
1444 "DPP: TESTING - stop at Authentication Confirm");
1445 if (wpa_s->dpp_auth->configurator) {
1446 /* Prevent GAS response */
1447 wpa_s->dpp_auth->auth_success = 0;
1448 }
1449 return;
1450 }
1451#endif /* CONFIG_TESTING_OPTIONS */
461d39af
JM
1452
1453 if (wpa_s->dpp_auth->configurator)
1454 wpas_dpp_start_gas_server(wpa_s);
1455 else
1456 wpas_dpp_start_gas_client(wpa_s);
1457}
1458
1459
30d27b04 1460static void wpas_dpp_rx_auth_resp(struct wpa_supplicant *wpa_s, const u8 *src,
d2709206
JM
1461 const u8 *hdr, const u8 *buf, size_t len,
1462 unsigned int freq)
30d27b04
JM
1463{
1464 struct dpp_authentication *auth = wpa_s->dpp_auth;
dc4d271c 1465 struct wpabuf *msg;
30d27b04 1466
d2709206
JM
1467 wpa_printf(MSG_DEBUG, "DPP: Authentication Response from " MACSTR
1468 " (freq %u MHz)", MAC2STR(src), freq);
30d27b04
JM
1469
1470 if (!auth) {
1471 wpa_printf(MSG_DEBUG,
1472 "DPP: No DPP Authentication in progress - drop");
1473 return;
1474 }
1475
1476 if (!is_zero_ether_addr(auth->peer_mac_addr) &&
1477 os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1478 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1479 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1480 return;
1481 }
1482
1483 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
1484
d2709206
JM
1485 if (auth->curr_freq != freq && auth->neg_freq == freq) {
1486 wpa_printf(MSG_DEBUG,
1487 "DPP: Responder accepted request for different negotiation channel");
1488 auth->curr_freq = freq;
1489 }
1490
f97ace34 1491 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
dc4d271c
JM
1492 msg = dpp_auth_resp_rx(auth, hdr, buf, len);
1493 if (!msg) {
30d27b04
JM
1494 if (auth->auth_resp_status == DPP_STATUS_RESPONSE_PENDING) {
1495 wpa_printf(MSG_DEBUG,
1496 "DPP: Start wait for full response");
1497 offchannel_send_action_done(wpa_s);
1498 wpas_dpp_listen_start(wpa_s, auth->curr_freq);
1499 return;
1500 }
1501 wpa_printf(MSG_DEBUG, "DPP: No confirm generated");
1502 return;
1503 }
1504 os_memcpy(auth->peer_mac_addr, src, ETH_ALEN);
1505
af48810b
JM
1506 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1507 MAC2STR(src), auth->curr_freq, DPP_PA_AUTHENTICATION_CONF);
30d27b04
JM
1508 offchannel_send_action(wpa_s, auth->curr_freq,
1509 src, wpa_s->own_addr, broadcast,
1510 wpabuf_head(msg), wpabuf_len(msg),
1511 500, wpas_dpp_tx_status, 0);
1512 wpabuf_free(msg);
461d39af 1513 wpa_s->dpp_auth_ok_on_ack = 1;
30d27b04
JM
1514}
1515
1516
1517static void wpas_dpp_rx_auth_conf(struct wpa_supplicant *wpa_s, const u8 *src,
dc4d271c 1518 const u8 *hdr, const u8 *buf, size_t len)
30d27b04
JM
1519{
1520 struct dpp_authentication *auth = wpa_s->dpp_auth;
1521
1522 wpa_printf(MSG_DEBUG, "DPP: Authentication Confirmation from " MACSTR,
1523 MAC2STR(src));
1524
1525 if (!auth) {
1526 wpa_printf(MSG_DEBUG,
1527 "DPP: No DPP Authentication in progress - drop");
1528 return;
1529 }
1530
1531 if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1532 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1533 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1534 return;
1535 }
1536
dc4d271c 1537 if (dpp_auth_conf_rx(auth, hdr, buf, len) < 0) {
30d27b04
JM
1538 wpa_printf(MSG_DEBUG, "DPP: Authentication failed");
1539 return;
1540 }
1541
461d39af 1542 wpas_dpp_auth_success(wpa_s, 0);
30d27b04
JM
1543}
1544
1545
22f90b32
JM
1546#ifdef CONFIG_DPP2
1547
1548static void wpas_dpp_config_result_wait_timeout(void *eloop_ctx,
1549 void *timeout_ctx)
1550{
1551 struct wpa_supplicant *wpa_s = eloop_ctx;
1552 struct dpp_authentication *auth = wpa_s->dpp_auth;
1553
1554 if (!auth || !auth->waiting_conf_result)
1555 return;
1556
1557 wpa_printf(MSG_DEBUG,
1558 "DPP: Timeout while waiting for Configuration Result");
1559 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1560 dpp_auth_deinit(auth);
1561 wpa_s->dpp_auth = NULL;
1562}
1563
1564
b10e01a7
JM
1565static void wpas_dpp_conn_status_result_wait_timeout(void *eloop_ctx,
1566 void *timeout_ctx)
1567{
1568 struct wpa_supplicant *wpa_s = eloop_ctx;
1569 struct dpp_authentication *auth = wpa_s->dpp_auth;
1570
1571 if (!auth || !auth->waiting_conn_status_result)
1572 return;
1573
1574 wpa_printf(MSG_DEBUG,
1575 "DPP: Timeout while waiting for Connection Status Result");
1576 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONN_STATUS_RESULT "timeout");
1577 wpas_dpp_listen_stop(wpa_s);
1578 dpp_auth_deinit(auth);
1579 wpa_s->dpp_auth = NULL;
1580}
1581
1582
22f90b32
JM
1583static void wpas_dpp_rx_conf_result(struct wpa_supplicant *wpa_s, const u8 *src,
1584 const u8 *hdr, const u8 *buf, size_t len)
1585{
1586 struct dpp_authentication *auth = wpa_s->dpp_auth;
1587 enum dpp_status_error status;
1588
1589 wpa_printf(MSG_DEBUG, "DPP: Configuration Result from " MACSTR,
1590 MAC2STR(src));
1591
1592 if (!auth || !auth->waiting_conf_result) {
1593 wpa_printf(MSG_DEBUG,
1594 "DPP: No DPP Configuration waiting for result - drop");
1595 return;
1596 }
1597
1598 if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1599 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1600 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1601 return;
1602 }
1603
1604 status = dpp_conf_result_rx(auth, hdr, buf, len);
1605
b10e01a7
JM
1606 if (status == DPP_STATUS_OK && auth->send_conn_status) {
1607 wpa_msg(wpa_s, MSG_INFO,
1608 DPP_EVENT_CONF_SENT "wait_conn_status=1");
1609 wpa_printf(MSG_DEBUG, "DPP: Wait for Connection Status Result");
1610 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout,
1611 wpa_s, NULL);
1612 auth->waiting_conn_status_result = 1;
1613 eloop_cancel_timeout(wpas_dpp_conn_status_result_wait_timeout,
1614 wpa_s, NULL);
1615 eloop_register_timeout(16, 0,
1616 wpas_dpp_conn_status_result_wait_timeout,
1617 wpa_s, NULL);
1618 offchannel_send_action_done(wpa_s);
1619 wpas_dpp_listen_start(wpa_s, auth->neg_freq ? auth->neg_freq :
1620 auth->curr_freq);
1621 return;
1622 }
22f90b32
JM
1623 offchannel_send_action_done(wpa_s);
1624 wpas_dpp_listen_stop(wpa_s);
1625 if (status == DPP_STATUS_OK)
1626 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
1627 else
1628 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1629 dpp_auth_deinit(auth);
1630 wpa_s->dpp_auth = NULL;
1631 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout, wpa_s, NULL);
1632}
1633
2a5a0680 1634
b10e01a7
JM
1635static void wpas_dpp_rx_conn_status_result(struct wpa_supplicant *wpa_s,
1636 const u8 *src, const u8 *hdr,
1637 const u8 *buf, size_t len)
1638{
1639 struct dpp_authentication *auth = wpa_s->dpp_auth;
1640 enum dpp_status_error status;
1641 u8 ssid[SSID_MAX_LEN];
1642 size_t ssid_len = 0;
1643 char *channel_list = NULL;
1644
1645 wpa_printf(MSG_DEBUG, "DPP: Connection Status Result");
1646
1647 if (!auth || !auth->waiting_conn_status_result) {
1648 wpa_printf(MSG_DEBUG,
1649 "DPP: No DPP Configuration waiting for connection status result - drop");
1650 return;
1651 }
1652
1653 status = dpp_conn_status_result_rx(auth, hdr, buf, len,
1654 ssid, &ssid_len, &channel_list);
1655 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONN_STATUS_RESULT
1656 "result=%d ssid=%s channel_list=%s",
1657 status, wpa_ssid_txt(ssid, ssid_len),
1658 channel_list ? channel_list : "N/A");
1659 os_free(channel_list);
1660 offchannel_send_action_done(wpa_s);
1661 wpas_dpp_listen_stop(wpa_s);
1662 dpp_auth_deinit(auth);
1663 wpa_s->dpp_auth = NULL;
1664 eloop_cancel_timeout(wpas_dpp_conn_status_result_wait_timeout,
1665 wpa_s, NULL);
1666}
1667
1668
2a5a0680
JM
1669static int wpas_dpp_process_conf_obj(void *ctx,
1670 struct dpp_authentication *auth)
1671{
1672 struct wpa_supplicant *wpa_s = ctx;
52d469de
JM
1673 unsigned int i;
1674 int res = -1;
1675
1676 for (i = 0; i < auth->num_conf_obj; i++) {
1677 res = wpas_dpp_handle_config_obj(wpa_s, auth,
1678 &auth->conf_obj[i]);
1679 if (res)
1680 break;
1681 }
1682 if (!res)
1683 wpas_dpp_post_process_config(wpa_s, auth);
2a5a0680 1684
52d469de 1685 return res;
2a5a0680
JM
1686}
1687
562f7714
JM
1688
1689static void wpas_dpp_remove_bi(void *ctx, struct dpp_bootstrap_info *bi)
1690{
1691 struct wpa_supplicant *wpa_s = ctx;
1692
1693 if (bi == wpa_s->dpp_chirp_bi)
1694 wpas_dpp_chirp_stop(wpa_s);
1695}
1696
22f90b32
JM
1697#endif /* CONFIG_DPP2 */
1698
1699
a0d5c56f
JM
1700static void wpas_dpp_rx_peer_disc_resp(struct wpa_supplicant *wpa_s,
1701 const u8 *src,
1702 const u8 *buf, size_t len)
1703{
1704 struct wpa_ssid *ssid;
e85b6601
JM
1705 const u8 *connector, *trans_id, *status;
1706 u16 connector_len, trans_id_len, status_len;
a0d5c56f
JM
1707 struct dpp_introduction intro;
1708 struct rsn_pmksa_cache_entry *entry;
787615b3
JM
1709 struct os_time now;
1710 struct os_reltime rnow;
1711 os_time_t expiry;
1712 unsigned int seconds;
e85b6601 1713 enum dpp_status_error res;
a0d5c56f
JM
1714
1715 wpa_printf(MSG_DEBUG, "DPP: Peer Discovery Response from " MACSTR,
1716 MAC2STR(src));
1717 if (is_zero_ether_addr(wpa_s->dpp_intro_bssid) ||
1718 os_memcmp(src, wpa_s->dpp_intro_bssid, ETH_ALEN) != 0) {
1719 wpa_printf(MSG_DEBUG, "DPP: Not waiting for response from "
1720 MACSTR " - drop", MAC2STR(src));
1721 return;
1722 }
1723 offchannel_send_action_done(wpa_s);
1724
1725 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1726 if (ssid == wpa_s->dpp_intro_network)
1727 break;
1728 }
1729 if (!ssid || !ssid->dpp_connector || !ssid->dpp_netaccesskey ||
1730 !ssid->dpp_csign) {
1731 wpa_printf(MSG_DEBUG,
1732 "DPP: Profile not found for network introduction");
1733 return;
1734 }
1735
85fd8263
JM
1736 trans_id = dpp_get_attr(buf, len, DPP_ATTR_TRANSACTION_ID,
1737 &trans_id_len);
1738 if (!trans_id || trans_id_len != 1) {
1739 wpa_printf(MSG_DEBUG,
1740 "DPP: Peer did not include Transaction ID");
e85b6601
JM
1741 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1742 " fail=missing_transaction_id", MAC2STR(src));
85fd8263
JM
1743 goto fail;
1744 }
1745 if (trans_id[0] != TRANSACTION_ID) {
1746 wpa_printf(MSG_DEBUG,
1747 "DPP: Ignore frame with unexpected Transaction ID %u",
1748 trans_id[0]);
e85b6601
JM
1749 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1750 " fail=transaction_id_mismatch", MAC2STR(src));
1751 goto fail;
1752 }
1753
1754 status = dpp_get_attr(buf, len, DPP_ATTR_STATUS, &status_len);
1755 if (!status || status_len != 1) {
1756 wpa_printf(MSG_DEBUG, "DPP: Peer did not include Status");
1757 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1758 " fail=missing_status", MAC2STR(src));
1759 goto fail;
1760 }
1761 if (status[0] != DPP_STATUS_OK) {
1762 wpa_printf(MSG_DEBUG,
1763 "DPP: Peer rejected network introduction: Status %u",
1764 status[0]);
1765 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1766 " status=%u", MAC2STR(src), status[0]);
16ef233b
JM
1767#ifdef CONFIG_DPP2
1768 wpas_dpp_send_conn_status_result(wpa_s, status[0]);
1769#endif /* CONFIG_DPP2 */
85fd8263
JM
1770 goto fail;
1771 }
1772
a0d5c56f
JM
1773 connector = dpp_get_attr(buf, len, DPP_ATTR_CONNECTOR, &connector_len);
1774 if (!connector) {
1775 wpa_printf(MSG_DEBUG,
1776 "DPP: Peer did not include its Connector");
e85b6601
JM
1777 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1778 " fail=missing_connector", MAC2STR(src));
1779 goto fail;
a0d5c56f
JM
1780 }
1781
e85b6601
JM
1782 res = dpp_peer_intro(&intro, ssid->dpp_connector,
1783 ssid->dpp_netaccesskey,
1784 ssid->dpp_netaccesskey_len,
1785 ssid->dpp_csign,
1786 ssid->dpp_csign_len,
1787 connector, connector_len, &expiry);
1788 if (res != DPP_STATUS_OK) {
a0d5c56f
JM
1789 wpa_printf(MSG_INFO,
1790 "DPP: Network Introduction protocol resulted in failure");
e85b6601
JM
1791 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1792 " fail=peer_connector_validation_failed", MAC2STR(src));
16ef233b
JM
1793#ifdef CONFIG_DPP2
1794 wpas_dpp_send_conn_status_result(wpa_s, res);
1795#endif /* CONFIG_DPP2 */
a0d5c56f
JM
1796 goto fail;
1797 }
1798
a0d5c56f
JM
1799 entry = os_zalloc(sizeof(*entry));
1800 if (!entry)
1801 goto fail;
1802 os_memcpy(entry->aa, src, ETH_ALEN);
1803 os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN);
1804 os_memcpy(entry->pmk, intro.pmk, intro.pmk_len);
1805 entry->pmk_len = intro.pmk_len;
1806 entry->akmp = WPA_KEY_MGMT_DPP;
787615b3
JM
1807 if (expiry) {
1808 os_get_time(&now);
1809 seconds = expiry - now.sec;
1810 } else {
1811 seconds = 86400 * 7;
1812 }
1813 os_get_reltime(&rnow);
1814 entry->expiration = rnow.sec + seconds;
1815 entry->reauth_time = rnow.sec + seconds;
a0d5c56f
JM
1816 entry->network_ctx = ssid;
1817 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
1818
e85b6601
JM
1819 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1820 " status=%u", MAC2STR(src), status[0]);
1821
a0d5c56f
JM
1822 wpa_printf(MSG_DEBUG,
1823 "DPP: Try connection again after successful network introduction");
1824 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
1825 wpa_supplicant_cancel_sched_scan(wpa_s);
1826 wpa_supplicant_req_scan(wpa_s, 0, 0);
1827 }
1828fail:
1829 os_memset(&intro, 0, sizeof(intro));
1830}
1831
1832
3b50f8a4
JM
1833static int wpas_dpp_allow_ir(struct wpa_supplicant *wpa_s, unsigned int freq)
1834{
1835 int i, j;
1836
1837 if (!wpa_s->hw.modes)
1838 return -1;
1839
1840 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1841 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1842
1843 for (j = 0; j < mode->num_channels; j++) {
1844 struct hostapd_channel_data *chan = &mode->channels[j];
1845
1846 if (chan->freq != (int) freq)
1847 continue;
1848
1849 if (chan->flag & (HOSTAPD_CHAN_DISABLED |
1850 HOSTAPD_CHAN_NO_IR |
1851 HOSTAPD_CHAN_RADAR))
1852 continue;
1853
1854 return 1;
1855 }
1856 }
1857
1858 wpa_printf(MSG_DEBUG,
1859 "DPP: Frequency %u MHz not supported or does not allow PKEX initiation in the current channel list",
1860 freq);
1861
1862 return 0;
1863}
1864
1865
1866static int wpas_dpp_pkex_next_channel(struct wpa_supplicant *wpa_s,
1867 struct dpp_pkex *pkex)
1868{
1869 if (pkex->freq == 2437)
1870 pkex->freq = 5745;
1871 else if (pkex->freq == 5745)
1872 pkex->freq = 5220;
1873 else if (pkex->freq == 5220)
1874 pkex->freq = 60480;
1875 else
1876 return -1; /* no more channels to try */
1877
1878 if (wpas_dpp_allow_ir(wpa_s, pkex->freq) == 1) {
1879 wpa_printf(MSG_DEBUG, "DPP: Try to initiate on %u MHz",
1880 pkex->freq);
1881 return 0;
1882 }
1883
1884 /* Could not use this channel - try the next one */
1885 return wpas_dpp_pkex_next_channel(wpa_s, pkex);
1886}
1887
1888
00d2d13d
JM
1889static void wpas_dpp_pkex_retry_timeout(void *eloop_ctx, void *timeout_ctx)
1890{
1891 struct wpa_supplicant *wpa_s = eloop_ctx;
1892 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1893
1894 if (!pkex || !pkex->exchange_req)
1895 return;
1896 if (pkex->exch_req_tries >= 5) {
3b50f8a4
JM
1897 if (wpas_dpp_pkex_next_channel(wpa_s, pkex) < 0) {
1898 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1899 "No response from PKEX peer");
1900 dpp_pkex_free(pkex);
1901 wpa_s->dpp_pkex = NULL;
1902 return;
1903 }
1904 pkex->exch_req_tries = 0;
00d2d13d
JM
1905 }
1906
1907 pkex->exch_req_tries++;
1908 wpa_printf(MSG_DEBUG, "DPP: Retransmit PKEX Exchange Request (try %u)",
1909 pkex->exch_req_tries);
1910 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1911 MAC2STR(broadcast), pkex->freq, DPP_PA_PKEX_EXCHANGE_REQ);
1912 offchannel_send_action(wpa_s, pkex->freq, broadcast,
1913 wpa_s->own_addr, broadcast,
1914 wpabuf_head(pkex->exchange_req),
1915 wpabuf_len(pkex->exchange_req),
1916 pkex->exch_req_wait_time,
1917 wpas_dpp_tx_pkex_status, 0);
1918}
1919
1920
500ed7f0
JM
1921static void
1922wpas_dpp_tx_pkex_status(struct wpa_supplicant *wpa_s,
1923 unsigned int freq, const u8 *dst,
1924 const u8 *src, const u8 *bssid,
1925 const u8 *data, size_t data_len,
1926 enum offchannel_send_action_result result)
1927{
af48810b 1928 const char *res_txt;
00d2d13d 1929 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
af48810b
JM
1930
1931 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
1932 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
1933 "FAILED");
500ed7f0
JM
1934 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
1935 " result=%s (PKEX)",
af48810b
JM
1936 freq, MAC2STR(dst), res_txt);
1937 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
1938 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
e0247e79 1939
00d2d13d 1940 if (!pkex) {
e0247e79
JM
1941 wpa_printf(MSG_DEBUG,
1942 "DPP: Ignore TX status since there is no ongoing PKEX exchange");
1943 return;
1944 }
1945
00d2d13d 1946 if (pkex->failed) {
e0247e79
JM
1947 wpa_printf(MSG_DEBUG,
1948 "DPP: Terminate PKEX exchange due to an earlier error");
00d2d13d
JM
1949 if (pkex->t > pkex->own_bi->pkex_t)
1950 pkex->own_bi->pkex_t = pkex->t;
1951 dpp_pkex_free(pkex);
e0247e79 1952 wpa_s->dpp_pkex = NULL;
00d2d13d
JM
1953 return;
1954 }
1955
1956 if (pkex->exch_req_wait_time && pkex->exchange_req) {
1957 /* Wait for PKEX Exchange Response frame and retry request if
1958 * no response is seen. */
1959 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
1960 eloop_register_timeout(pkex->exch_req_wait_time / 1000,
1961 (pkex->exch_req_wait_time % 1000) * 1000,
1962 wpas_dpp_pkex_retry_timeout, wpa_s,
1963 NULL);
e0247e79 1964 }
500ed7f0
JM
1965}
1966
1967
1968static void
1969wpas_dpp_rx_pkex_exchange_req(struct wpa_supplicant *wpa_s, const u8 *src,
1970 const u8 *buf, size_t len, unsigned int freq)
1971{
1972 struct wpabuf *msg;
1973 unsigned int wait_time;
1974
1975 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Request from " MACSTR,
1976 MAC2STR(src));
1977
1978 /* TODO: Support multiple PKEX codes by iterating over all the enabled
1979 * values here */
1980
1981 if (!wpa_s->dpp_pkex_code || !wpa_s->dpp_pkex_bi) {
1982 wpa_printf(MSG_DEBUG,
1983 "DPP: No PKEX code configured - ignore request");
1984 return;
1985 }
1986
1987 if (wpa_s->dpp_pkex) {
1988 /* TODO: Support parallel operations */
1989 wpa_printf(MSG_DEBUG,
1990 "DPP: Already in PKEX session - ignore new request");
1991 return;
1992 }
1993
219d4c9f 1994 wpa_s->dpp_pkex = dpp_pkex_rx_exchange_req(wpa_s, wpa_s->dpp_pkex_bi,
500ed7f0
JM
1995 wpa_s->own_addr, src,
1996 wpa_s->dpp_pkex_identifier,
1997 wpa_s->dpp_pkex_code,
1998 buf, len);
1999 if (!wpa_s->dpp_pkex) {
2000 wpa_printf(MSG_DEBUG,
2001 "DPP: Failed to process the request - ignore it");
2002 return;
2003 }
2004
2005 msg = wpa_s->dpp_pkex->exchange_resp;
2006 wait_time = wpa_s->max_remain_on_chan;
2007 if (wait_time > 2000)
2008 wait_time = 2000;
af48810b
JM
2009 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2010 MAC2STR(src), freq, DPP_PA_PKEX_EXCHANGE_RESP);
500ed7f0
JM
2011 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
2012 broadcast,
2013 wpabuf_head(msg), wpabuf_len(msg),
2014 wait_time, wpas_dpp_tx_pkex_status, 0);
2015}
2016
2017
2018static void
2019wpas_dpp_rx_pkex_exchange_resp(struct wpa_supplicant *wpa_s, const u8 *src,
2020 const u8 *buf, size_t len, unsigned int freq)
2021{
2022 struct wpabuf *msg;
2023 unsigned int wait_time;
2024
2025 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Response from " MACSTR,
2026 MAC2STR(src));
2027
2028 /* TODO: Support multiple PKEX codes by iterating over all the enabled
2029 * values here */
2030
2031 if (!wpa_s->dpp_pkex || !wpa_s->dpp_pkex->initiator ||
2032 wpa_s->dpp_pkex->exchange_done) {
2033 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2034 return;
2035 }
2036
00d2d13d
JM
2037 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
2038 wpa_s->dpp_pkex->exch_req_wait_time = 0;
2039
af4103e5 2040 msg = dpp_pkex_rx_exchange_resp(wpa_s->dpp_pkex, src, buf, len);
500ed7f0
JM
2041 if (!msg) {
2042 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
2043 return;
2044 }
2045
2046 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Request to " MACSTR,
2047 MAC2STR(src));
2048
2049 wait_time = wpa_s->max_remain_on_chan;
2050 if (wait_time > 2000)
2051 wait_time = 2000;
af48810b
JM
2052 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2053 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_REQ);
500ed7f0
JM
2054 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
2055 broadcast,
2056 wpabuf_head(msg), wpabuf_len(msg),
2057 wait_time, wpas_dpp_tx_pkex_status, 0);
2058 wpabuf_free(msg);
2059}
2060
2061
de029861
JM
2062static struct dpp_bootstrap_info *
2063wpas_dpp_pkex_finish(struct wpa_supplicant *wpa_s, const u8 *peer,
2064 unsigned int freq)
2065{
de029861
JM
2066 struct dpp_bootstrap_info *bi;
2067
87d8435c 2068 bi = dpp_pkex_finish(wpa_s->dpp, wpa_s->dpp_pkex, peer, freq);
de029861
JM
2069 if (!bi)
2070 return NULL;
de029861 2071 wpa_s->dpp_pkex = NULL;
de029861
JM
2072 return bi;
2073}
2074
2075
500ed7f0
JM
2076static void
2077wpas_dpp_rx_pkex_commit_reveal_req(struct wpa_supplicant *wpa_s, const u8 *src,
4be5bc98
JM
2078 const u8 *hdr, const u8 *buf, size_t len,
2079 unsigned int freq)
500ed7f0
JM
2080{
2081 struct wpabuf *msg;
2082 unsigned int wait_time;
2083 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
500ed7f0
JM
2084
2085 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Request from " MACSTR,
2086 MAC2STR(src));
2087
2088 if (!pkex || pkex->initiator || !pkex->exchange_done) {
2089 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2090 return;
2091 }
2092
4be5bc98 2093 msg = dpp_pkex_rx_commit_reveal_req(pkex, hdr, buf, len);
500ed7f0
JM
2094 if (!msg) {
2095 wpa_printf(MSG_DEBUG, "DPP: Failed to process the request");
039b8e73
JM
2096 if (pkex->failed) {
2097 wpa_printf(MSG_DEBUG, "DPP: Terminate PKEX exchange");
29ab69e4
JM
2098 if (pkex->t > pkex->own_bi->pkex_t)
2099 pkex->own_bi->pkex_t = pkex->t;
039b8e73
JM
2100 dpp_pkex_free(wpa_s->dpp_pkex);
2101 wpa_s->dpp_pkex = NULL;
2102 }
500ed7f0
JM
2103 return;
2104 }
2105
2106 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Response to "
2107 MACSTR, MAC2STR(src));
2108
2109 wait_time = wpa_s->max_remain_on_chan;
2110 if (wait_time > 2000)
2111 wait_time = 2000;
af48810b
JM
2112 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2113 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_RESP);
500ed7f0
JM
2114 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
2115 broadcast,
2116 wpabuf_head(msg), wpabuf_len(msg),
2117 wait_time, wpas_dpp_tx_pkex_status, 0);
2118 wpabuf_free(msg);
2119
de029861 2120 wpas_dpp_pkex_finish(wpa_s, src, freq);
500ed7f0
JM
2121}
2122
2123
2124static void
2125wpas_dpp_rx_pkex_commit_reveal_resp(struct wpa_supplicant *wpa_s, const u8 *src,
4be5bc98 2126 const u8 *hdr, const u8 *buf, size_t len,
500ed7f0
JM
2127 unsigned int freq)
2128{
2129 int res;
de029861 2130 struct dpp_bootstrap_info *bi;
500ed7f0
JM
2131 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
2132 char cmd[500];
2133
2134 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Response from " MACSTR,
2135 MAC2STR(src));
2136
2137 if (!pkex || !pkex->initiator || !pkex->exchange_done) {
2138 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2139 return;
2140 }
2141
4be5bc98 2142 res = dpp_pkex_rx_commit_reveal_resp(pkex, hdr, buf, len);
500ed7f0
JM
2143 if (res < 0) {
2144 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
2145 return;
2146 }
2147
de029861 2148 bi = wpas_dpp_pkex_finish(wpa_s, src, freq);
500ed7f0
JM
2149 if (!bi)
2150 return;
500ed7f0
JM
2151
2152 os_snprintf(cmd, sizeof(cmd), " peer=%u %s",
2153 bi->id,
2154 wpa_s->dpp_pkex_auth_cmd ? wpa_s->dpp_pkex_auth_cmd : "");
2155 wpa_printf(MSG_DEBUG,
2156 "DPP: Start authentication after PKEX with parameters: %s",
2157 cmd);
2158 if (wpas_dpp_auth_init(wpa_s, cmd) < 0) {
2159 wpa_printf(MSG_DEBUG,
2160 "DPP: Authentication initialization failed");
c304bddc 2161 offchannel_send_action_done(wpa_s);
500ed7f0
JM
2162 return;
2163 }
2164}
2165
2166
30d27b04
JM
2167void wpas_dpp_rx_action(struct wpa_supplicant *wpa_s, const u8 *src,
2168 const u8 *buf, size_t len, unsigned int freq)
2169{
8c19ea3f 2170 u8 crypto_suite;
30d27b04 2171 enum dpp_public_action_frame_type type;
dc4d271c 2172 const u8 *hdr;
29ab69e4 2173 unsigned int pkex_t;
30d27b04 2174
dc4d271c
JM
2175 if (len < DPP_HDR_LEN)
2176 return;
2177 if (WPA_GET_BE24(buf) != OUI_WFA || buf[3] != DPP_OUI_TYPE)
30d27b04 2178 return;
dc4d271c
JM
2179 hdr = buf;
2180 buf += 4;
2181 len -= 4;
8c19ea3f
JM
2182 crypto_suite = *buf++;
2183 type = *buf++;
2184 len -= 2;
30d27b04
JM
2185
2186 wpa_printf(MSG_DEBUG,
8c19ea3f 2187 "DPP: Received DPP Public Action frame crypto suite %u type %d from "
30d27b04 2188 MACSTR " freq=%u",
8c19ea3f
JM
2189 crypto_suite, type, MAC2STR(src), freq);
2190 if (crypto_suite != 1) {
2191 wpa_printf(MSG_DEBUG, "DPP: Unsupported crypto suite %u",
2192 crypto_suite);
a7073934
JM
2193 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2194 " freq=%u type=%d ignore=unsupported-crypto-suite",
2195 MAC2STR(src), freq, type);
8c19ea3f
JM
2196 return;
2197 }
30d27b04 2198 wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes", buf, len);
a7073934
JM
2199 if (dpp_check_attrs(buf, len) < 0) {
2200 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2201 " freq=%u type=%d ignore=invalid-attributes",
2202 MAC2STR(src), freq, type);
30d27b04 2203 return;
a7073934
JM
2204 }
2205 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR " freq=%u type=%d",
2206 MAC2STR(src), freq, type);
30d27b04
JM
2207
2208 switch (type) {
2209 case DPP_PA_AUTHENTICATION_REQ:
dc4d271c 2210 wpas_dpp_rx_auth_req(wpa_s, src, hdr, buf, len, freq);
30d27b04
JM
2211 break;
2212 case DPP_PA_AUTHENTICATION_RESP:
d2709206 2213 wpas_dpp_rx_auth_resp(wpa_s, src, hdr, buf, len, freq);
30d27b04
JM
2214 break;
2215 case DPP_PA_AUTHENTICATION_CONF:
dc4d271c 2216 wpas_dpp_rx_auth_conf(wpa_s, src, hdr, buf, len);
30d27b04 2217 break;
a0d5c56f
JM
2218 case DPP_PA_PEER_DISCOVERY_RESP:
2219 wpas_dpp_rx_peer_disc_resp(wpa_s, src, buf, len);
2220 break;
500ed7f0
JM
2221 case DPP_PA_PKEX_EXCHANGE_REQ:
2222 wpas_dpp_rx_pkex_exchange_req(wpa_s, src, buf, len, freq);
2223 break;
2224 case DPP_PA_PKEX_EXCHANGE_RESP:
2225 wpas_dpp_rx_pkex_exchange_resp(wpa_s, src, buf, len, freq);
2226 break;
2227 case DPP_PA_PKEX_COMMIT_REVEAL_REQ:
4be5bc98
JM
2228 wpas_dpp_rx_pkex_commit_reveal_req(wpa_s, src, hdr, buf, len,
2229 freq);
500ed7f0
JM
2230 break;
2231 case DPP_PA_PKEX_COMMIT_REVEAL_RESP:
4be5bc98
JM
2232 wpas_dpp_rx_pkex_commit_reveal_resp(wpa_s, src, hdr, buf, len,
2233 freq);
500ed7f0 2234 break;
22f90b32
JM
2235#ifdef CONFIG_DPP2
2236 case DPP_PA_CONFIGURATION_RESULT:
2237 wpas_dpp_rx_conf_result(wpa_s, src, hdr, buf, len);
2238 break;
b10e01a7
JM
2239 case DPP_PA_CONNECTION_STATUS_RESULT:
2240 wpas_dpp_rx_conn_status_result(wpa_s, src, hdr, buf, len);
2241 break;
22f90b32 2242#endif /* CONFIG_DPP2 */
30d27b04
JM
2243 default:
2244 wpa_printf(MSG_DEBUG,
2245 "DPP: Ignored unsupported frame subtype %d", type);
2246 break;
2247 }
29ab69e4
JM
2248
2249 if (wpa_s->dpp_pkex)
2250 pkex_t = wpa_s->dpp_pkex->t;
2251 else if (wpa_s->dpp_pkex_bi)
2252 pkex_t = wpa_s->dpp_pkex_bi->pkex_t;
2253 else
2254 pkex_t = 0;
2255 if (pkex_t >= PKEX_COUNTER_T_LIMIT) {
2256 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_PKEX_T_LIMIT "id=0");
2257 wpas_dpp_pkex_remove(wpa_s, "*");
2258 }
30d27b04
JM
2259}
2260
2261
461d39af
JM
2262static struct wpabuf *
2263wpas_dpp_gas_req_handler(void *ctx, const u8 *sa, const u8 *query,
2264 size_t query_len)
2265{
2266 struct wpa_supplicant *wpa_s = ctx;
2267 struct dpp_authentication *auth = wpa_s->dpp_auth;
2268 struct wpabuf *resp;
2269
2270 wpa_printf(MSG_DEBUG, "DPP: GAS request from " MACSTR,
2271 MAC2STR(sa));
2272 if (!auth || !auth->auth_success ||
2273 os_memcmp(sa, auth->peer_mac_addr, ETH_ALEN) != 0) {
2274 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
2275 return NULL;
2276 }
422e73d6
JM
2277
2278 if (wpa_s->dpp_auth_ok_on_ack && auth->configurator) {
2279 wpa_printf(MSG_DEBUG,
2280 "DPP: Have not received ACK for Auth Confirm yet - assume it was received based on this GAS request");
2281 /* wpas_dpp_auth_success() would normally have been called from
2282 * TX status handler, but since there was no such handler call
2283 * yet, simply send out the event message and proceed with
2284 * exchange. */
2285 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=1");
2286 wpa_s->dpp_auth_ok_on_ack = 0;
2287 }
2288
461d39af
JM
2289 wpa_hexdump(MSG_DEBUG,
2290 "DPP: Received Configuration Request (GAS Query Request)",
2291 query, query_len);
fd920954
JM
2292 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_REQ_RX "src=" MACSTR,
2293 MAC2STR(sa));
461d39af
JM
2294 resp = dpp_conf_req_rx(auth, query, query_len);
2295 if (!resp)
2296 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
82feacce 2297 auth->conf_resp = resp;
461d39af
JM
2298 return resp;
2299}
2300
2301
2302static void
2303wpas_dpp_gas_status_handler(void *ctx, struct wpabuf *resp, int ok)
2304{
2305 struct wpa_supplicant *wpa_s = ctx;
2306 struct dpp_authentication *auth = wpa_s->dpp_auth;
2307
2308 if (!auth) {
2309 wpabuf_free(resp);
2310 return;
2311 }
82feacce
JM
2312 if (auth->conf_resp != resp) {
2313 wpa_printf(MSG_DEBUG,
2314 "DPP: Ignore GAS status report (ok=%d) for unknown response",
2315 ok);
2316 wpabuf_free(resp);
2317 return;
2318 }
461d39af
JM
2319
2320 wpa_printf(MSG_DEBUG, "DPP: Configuration exchange completed (ok=%d)",
2321 ok);
2322 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
95b0104a 2323 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
22f90b32
JM
2324#ifdef CONFIG_DPP2
2325 if (ok && auth->peer_version >= 2 &&
2326 auth->conf_resp_status == DPP_STATUS_OK) {
2327 wpa_printf(MSG_DEBUG, "DPP: Wait for Configuration Result");
2328 auth->waiting_conf_result = 1;
2329 auth->conf_resp = NULL;
2330 wpabuf_free(resp);
2331 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout,
2332 wpa_s, NULL);
2333 eloop_register_timeout(2, 0,
2334 wpas_dpp_config_result_wait_timeout,
2335 wpa_s, NULL);
2336 return;
2337 }
2338#endif /* CONFIG_DPP2 */
461d39af
JM
2339 offchannel_send_action_done(wpa_s);
2340 wpas_dpp_listen_stop(wpa_s);
2341 if (ok)
2342 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
2343 else
2344 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
2345 dpp_auth_deinit(wpa_s->dpp_auth);
2346 wpa_s->dpp_auth = NULL;
2347 wpabuf_free(resp);
2348}
2349
2350
f522bb23
JM
2351int wpas_dpp_configurator_sign(struct wpa_supplicant *wpa_s, const char *cmd)
2352{
2353 struct dpp_authentication *auth;
2354 int ret = -1;
2355 char *curve = NULL;
2356
514cc49b 2357 auth = dpp_alloc_auth(wpa_s->dpp, wpa_s);
f522bb23
JM
2358 if (!auth)
2359 return -1;
2360
2361 curve = get_param(cmd, " curve=");
e22c2338 2362 wpas_dpp_set_testing_options(wpa_s, auth);
514cc49b 2363 if (dpp_set_configurator(auth, cmd) == 0 &&
22f90b32 2364 dpp_configurator_own_config(auth, curve, 0) == 0)
52d469de
JM
2365 ret = wpas_dpp_handle_config_obj(wpa_s, auth,
2366 &auth->conf_obj[0]);
2367 if (!ret)
2368 wpas_dpp_post_process_config(wpa_s, auth);
f522bb23
JM
2369
2370 dpp_auth_deinit(auth);
2371 os_free(curve);
2372
2373 return ret;
2374}
2375
2376
a0d5c56f
JM
2377static void
2378wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
2379 unsigned int freq, const u8 *dst,
2380 const u8 *src, const u8 *bssid,
2381 const u8 *data, size_t data_len,
2382 enum offchannel_send_action_result result)
2383{
af48810b
JM
2384 const char *res_txt;
2385
2386 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
2387 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
2388 "FAILED");
a0d5c56f
JM
2389 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
2390 " result=%s (DPP Peer Discovery Request)",
af48810b
JM
2391 freq, MAC2STR(dst), res_txt);
2392 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
2393 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
a0d5c56f
JM
2394 /* TODO: Time out wait for response more quickly in error cases? */
2395}
2396
2397
2398int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2399 struct wpa_bss *bss)
2400{
2401 struct os_time now;
2402 struct wpabuf *msg;
2403 unsigned int wait_time;
dd6c5980
JM
2404 const u8 *rsn;
2405 struct wpa_ie_data ied;
a0d5c56f
JM
2406
2407 if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
2408 return 0; /* Not using DPP AKM - continue */
dd6c5980
JM
2409 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2410 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0 &&
2411 !(ied.key_mgmt & WPA_KEY_MGMT_DPP))
2412 return 0; /* AP does not support DPP AKM - continue */
a0d5c56f
JM
2413 if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
2414 return 0; /* PMKSA exists for DPP AKM - continue */
2415
2416 if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
2417 !ssid->dpp_csign) {
2418 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2419 "missing %s",
2420 !ssid->dpp_connector ? "Connector" :
2421 (!ssid->dpp_netaccesskey ? "netAccessKey" :
2422 "C-sign-key"));
2423 return -1;
2424 }
2425
2426 os_get_time(&now);
2427
a0d5c56f 2428 if (ssid->dpp_netaccesskey_expiry &&
3ca4be1e 2429 (os_time_t) ssid->dpp_netaccesskey_expiry < now.sec) {
a0d5c56f
JM
2430 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2431 "netAccessKey expired");
2432 return -1;
2433 }
2434
2435 wpa_printf(MSG_DEBUG,
2436 "DPP: Starting network introduction protocol to derive PMKSA for "
2437 MACSTR, MAC2STR(bss->bssid));
2438
2439 msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
85fd8263 2440 5 + 4 + os_strlen(ssid->dpp_connector));
a0d5c56f
JM
2441 if (!msg)
2442 return -1;
2443
a306ed5a
JM
2444#ifdef CONFIG_TESTING_OPTIONS
2445 if (dpp_test == DPP_TEST_NO_TRANSACTION_ID_PEER_DISC_REQ) {
2446 wpa_printf(MSG_INFO, "DPP: TESTING - no Transaction ID");
2447 goto skip_trans_id;
2448 }
55c6c858
JM
2449 if (dpp_test == DPP_TEST_INVALID_TRANSACTION_ID_PEER_DISC_REQ) {
2450 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Transaction ID");
2451 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2452 wpabuf_put_le16(msg, 0);
2453 goto skip_trans_id;
2454 }
a306ed5a
JM
2455#endif /* CONFIG_TESTING_OPTIONS */
2456
85fd8263
JM
2457 /* Transaction ID */
2458 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2459 wpabuf_put_le16(msg, 1);
2460 wpabuf_put_u8(msg, TRANSACTION_ID);
2461
a306ed5a
JM
2462#ifdef CONFIG_TESTING_OPTIONS
2463skip_trans_id:
2464 if (dpp_test == DPP_TEST_NO_CONNECTOR_PEER_DISC_REQ) {
2465 wpa_printf(MSG_INFO, "DPP: TESTING - no Connector");
2466 goto skip_connector;
2467 }
4b8de0c9
JM
2468 if (dpp_test == DPP_TEST_INVALID_CONNECTOR_PEER_DISC_REQ) {
2469 char *connector;
2470
2471 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Connector");
2472 connector = dpp_corrupt_connector_signature(
2473 ssid->dpp_connector);
2474 if (!connector) {
2475 wpabuf_free(msg);
2476 return -1;
2477 }
2478 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2479 wpabuf_put_le16(msg, os_strlen(connector));
2480 wpabuf_put_str(msg, connector);
2481 os_free(connector);
2482 goto skip_connector;
2483 }
a306ed5a
JM
2484#endif /* CONFIG_TESTING_OPTIONS */
2485
a0d5c56f
JM
2486 /* DPP Connector */
2487 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2488 wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
2489 wpabuf_put_str(msg, ssid->dpp_connector);
2490
a306ed5a
JM
2491#ifdef CONFIG_TESTING_OPTIONS
2492skip_connector:
2493#endif /* CONFIG_TESTING_OPTIONS */
2494
a0d5c56f
JM
2495 /* TODO: Timeout on AP response */
2496 wait_time = wpa_s->max_remain_on_chan;
2497 if (wait_time > 2000)
2498 wait_time = 2000;
af48810b
JM
2499 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2500 MAC2STR(bss->bssid), bss->freq, DPP_PA_PEER_DISCOVERY_REQ);
a0d5c56f
JM
2501 offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
2502 broadcast,
2503 wpabuf_head(msg), wpabuf_len(msg),
2504 wait_time, wpas_dpp_tx_introduction_status, 0);
2505 wpabuf_free(msg);
2506
2507 /* Request this connection attempt to terminate - new one will be
2508 * started when network introduction protocol completes */
2509 os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
2510 wpa_s->dpp_intro_network = ssid;
2511 return 1;
2512}
2513
2514
500ed7f0
JM
2515int wpas_dpp_pkex_add(struct wpa_supplicant *wpa_s, const char *cmd)
2516{
2517 struct dpp_bootstrap_info *own_bi;
2518 const char *pos, *end;
2519 unsigned int wait_time;
2520
2521 pos = os_strstr(cmd, " own=");
2522 if (!pos)
2523 return -1;
2524 pos += 5;
87d8435c 2525 own_bi = dpp_bootstrap_get_id(wpa_s->dpp, atoi(pos));
500ed7f0
JM
2526 if (!own_bi) {
2527 wpa_printf(MSG_DEBUG,
2528 "DPP: Identified bootstrap info not found");
2529 return -1;
2530 }
2531 if (own_bi->type != DPP_BOOTSTRAP_PKEX) {
2532 wpa_printf(MSG_DEBUG,
2533 "DPP: Identified bootstrap info not for PKEX");
2534 return -1;
2535 }
2536 wpa_s->dpp_pkex_bi = own_bi;
29ab69e4 2537 own_bi->pkex_t = 0; /* clear pending errors on new code */
500ed7f0
JM
2538
2539 os_free(wpa_s->dpp_pkex_identifier);
2540 wpa_s->dpp_pkex_identifier = NULL;
2541 pos = os_strstr(cmd, " identifier=");
2542 if (pos) {
2543 pos += 12;
2544 end = os_strchr(pos, ' ');
2545 if (!end)
2546 return -1;
2547 wpa_s->dpp_pkex_identifier = os_malloc(end - pos + 1);
2548 if (!wpa_s->dpp_pkex_identifier)
2549 return -1;
2550 os_memcpy(wpa_s->dpp_pkex_identifier, pos, end - pos);
2551 wpa_s->dpp_pkex_identifier[end - pos] = '\0';
2552 }
2553
2554 pos = os_strstr(cmd, " code=");
2555 if (!pos)
2556 return -1;
2557 os_free(wpa_s->dpp_pkex_code);
2558 wpa_s->dpp_pkex_code = os_strdup(pos + 6);
2559 if (!wpa_s->dpp_pkex_code)
2560 return -1;
2561
2562 if (os_strstr(cmd, " init=1")) {
00d2d13d 2563 struct dpp_pkex *pkex;
500ed7f0
JM
2564 struct wpabuf *msg;
2565
2566 wpa_printf(MSG_DEBUG, "DPP: Initiating PKEX");
2567 dpp_pkex_free(wpa_s->dpp_pkex);
219d4c9f 2568 wpa_s->dpp_pkex = dpp_pkex_init(wpa_s, own_bi, wpa_s->own_addr,
500ed7f0
JM
2569 wpa_s->dpp_pkex_identifier,
2570 wpa_s->dpp_pkex_code);
00d2d13d
JM
2571 pkex = wpa_s->dpp_pkex;
2572 if (!pkex)
500ed7f0
JM
2573 return -1;
2574
00d2d13d 2575 msg = pkex->exchange_req;
500ed7f0
JM
2576 wait_time = wpa_s->max_remain_on_chan;
2577 if (wait_time > 2000)
2578 wait_time = 2000;
00d2d13d 2579 pkex->freq = 2437;
af48810b
JM
2580 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
2581 " freq=%u type=%d",
00d2d13d
JM
2582 MAC2STR(broadcast), pkex->freq,
2583 DPP_PA_PKEX_EXCHANGE_REQ);
2584 offchannel_send_action(wpa_s, pkex->freq, broadcast,
2585 wpa_s->own_addr, broadcast,
500ed7f0
JM
2586 wpabuf_head(msg), wpabuf_len(msg),
2587 wait_time, wpas_dpp_tx_pkex_status, 0);
00d2d13d
JM
2588 if (wait_time == 0)
2589 wait_time = 2000;
2590 pkex->exch_req_wait_time = wait_time;
2591 pkex->exch_req_tries = 1;
500ed7f0
JM
2592 }
2593
2594 /* TODO: Support multiple PKEX info entries */
2595
2596 os_free(wpa_s->dpp_pkex_auth_cmd);
2597 wpa_s->dpp_pkex_auth_cmd = os_strdup(cmd);
2598
2599 return 1;
2600}
2601
2602
2603int wpas_dpp_pkex_remove(struct wpa_supplicant *wpa_s, const char *id)
2604{
2605 unsigned int id_val;
2606
2607 if (os_strcmp(id, "*") == 0) {
2608 id_val = 0;
2609 } else {
2610 id_val = atoi(id);
2611 if (id_val == 0)
2612 return -1;
2613 }
2614
2615 if ((id_val != 0 && id_val != 1) || !wpa_s->dpp_pkex_code)
2616 return -1;
2617
2618 /* TODO: Support multiple PKEX entries */
2619 os_free(wpa_s->dpp_pkex_code);
2620 wpa_s->dpp_pkex_code = NULL;
2621 os_free(wpa_s->dpp_pkex_identifier);
2622 wpa_s->dpp_pkex_identifier = NULL;
2623 os_free(wpa_s->dpp_pkex_auth_cmd);
2624 wpa_s->dpp_pkex_auth_cmd = NULL;
2625 wpa_s->dpp_pkex_bi = NULL;
2626 /* TODO: Remove dpp_pkex only if it is for the identified PKEX code */
2627 dpp_pkex_free(wpa_s->dpp_pkex);
2628 wpa_s->dpp_pkex = NULL;
2629 return 0;
2630}
2631
2632
c1d37739
JM
2633void wpas_dpp_stop(struct wpa_supplicant *wpa_s)
2634{
c304bddc
JM
2635 if (wpa_s->dpp_auth || wpa_s->dpp_pkex)
2636 offchannel_send_action_done(wpa_s);
c1d37739
JM
2637 dpp_auth_deinit(wpa_s->dpp_auth);
2638 wpa_s->dpp_auth = NULL;
ed62d401
JM
2639 dpp_pkex_free(wpa_s->dpp_pkex);
2640 wpa_s->dpp_pkex = NULL;
1866dfb5
JM
2641 if (wpa_s->dpp_gas_client && wpa_s->dpp_gas_dialog_token >= 0)
2642 gas_query_stop(wpa_s->gas, wpa_s->dpp_gas_dialog_token);
c1d37739
JM
2643}
2644
2645
be27e185
JM
2646int wpas_dpp_init(struct wpa_supplicant *wpa_s)
2647{
2ed2b52f 2648 struct dpp_global_config config;
461d39af
JM
2649 u8 adv_proto_id[7];
2650
2651 adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
2652 adv_proto_id[1] = 5;
2653 WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
2654 adv_proto_id[5] = DPP_OUI_TYPE;
2655 adv_proto_id[6] = 0x01;
2656
2657 if (gas_server_register(wpa_s->gas_server, adv_proto_id,
2658 sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
2659 wpas_dpp_gas_status_handler, wpa_s) < 0)
2660 return -1;
2ed2b52f
JM
2661
2662 os_memset(&config, 0, sizeof(config));
2663 config.msg_ctx = wpa_s;
2a5a0680
JM
2664 config.cb_ctx = wpa_s;
2665#ifdef CONFIG_DPP2
2666 config.process_conf_obj = wpas_dpp_process_conf_obj;
562f7714 2667 config.remove_bi = wpas_dpp_remove_bi;
2a5a0680 2668#endif /* CONFIG_DPP2 */
2ed2b52f 2669 wpa_s->dpp = dpp_global_init(&config);
87d8435c 2670 return wpa_s->dpp ? 0 : -1;
be27e185
JM
2671}
2672
2673
2674void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
2675{
461d39af
JM
2676#ifdef CONFIG_TESTING_OPTIONS
2677 os_free(wpa_s->dpp_config_obj_override);
2678 wpa_s->dpp_config_obj_override = NULL;
2679 os_free(wpa_s->dpp_discovery_override);
2680 wpa_s->dpp_discovery_override = NULL;
2681 os_free(wpa_s->dpp_groups_override);
2682 wpa_s->dpp_groups_override = NULL;
461d39af
JM
2683 wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
2684#endif /* CONFIG_TESTING_OPTIONS */
87d8435c 2685 if (!wpa_s->dpp)
be27e185 2686 return;
87d8435c 2687 dpp_global_clear(wpa_s->dpp);
00d2d13d 2688 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
30d27b04 2689 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
f97ace34 2690 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
95b0104a 2691 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
22f90b32
JM
2692#ifdef CONFIG_DPP2
2693 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout, wpa_s, NULL);
b10e01a7
JM
2694 eloop_cancel_timeout(wpas_dpp_conn_status_result_wait_timeout,
2695 wpa_s, NULL);
16ef233b 2696 eloop_cancel_timeout(wpas_dpp_conn_status_result_timeout, wpa_s, NULL);
10ec6a5f
JM
2697 dpp_pfs_free(wpa_s->dpp_pfs);
2698 wpa_s->dpp_pfs = NULL;
562f7714 2699 wpas_dpp_chirp_stop(wpa_s);
22f90b32 2700#endif /* CONFIG_DPP2 */
30d27b04
JM
2701 offchannel_send_action_done(wpa_s);
2702 wpas_dpp_listen_stop(wpa_s);
fa5c9074 2703 wpas_dpp_stop(wpa_s);
500ed7f0 2704 wpas_dpp_pkex_remove(wpa_s, "*");
a0d5c56f 2705 os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
b65b22d6
JM
2706 os_free(wpa_s->dpp_configurator_params);
2707 wpa_s->dpp_configurator_params = NULL;
be27e185 2708}
c02dd10d
JM
2709
2710
2711#ifdef CONFIG_DPP2
562f7714 2712
c02dd10d
JM
2713int wpas_dpp_controller_start(struct wpa_supplicant *wpa_s, const char *cmd)
2714{
2715 struct dpp_controller_config config;
2716 const char *pos;
2717
2718 os_memset(&config, 0, sizeof(config));
2719 if (cmd) {
2720 pos = os_strstr(cmd, " tcp_port=");
2721 if (pos) {
2722 pos += 10;
2723 config.tcp_port = atoi(pos);
2724 }
2725 }
2726 config.configurator_params = wpa_s->dpp_configurator_params;
2727 return dpp_controller_start(wpa_s->dpp, &config);
2728}
562f7714
JM
2729
2730
2731static void wpas_dpp_chirp_next(void *eloop_ctx, void *timeout_ctx);
2732
2733static void wpas_dpp_chirp_timeout(void *eloop_ctx, void *timeout_ctx)
2734{
2735 struct wpa_supplicant *wpa_s = eloop_ctx;
2736
2737 wpa_printf(MSG_DEBUG, "DPP: No chirp response received");
2738 offchannel_send_action_done(wpa_s);
2739 wpas_dpp_chirp_next(wpa_s, NULL);
2740}
2741
2742
2743static void wpas_dpp_chirp_tx_status(struct wpa_supplicant *wpa_s,
2744 unsigned int freq, const u8 *dst,
2745 const u8 *src, const u8 *bssid,
2746 const u8 *data, size_t data_len,
2747 enum offchannel_send_action_result result)
2748{
2749 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
2750 wpa_printf(MSG_DEBUG, "DPP: Failed to send chirp on %d MHz",
2751 wpa_s->dpp_chirp_freq);
2752 if (eloop_register_timeout(0, 0, wpas_dpp_chirp_next,
2753 wpa_s, NULL) < 0)
2754 wpas_dpp_chirp_stop(wpa_s);
2755 return;
2756 }
2757
2758 wpa_printf(MSG_DEBUG, "DPP: Chirp send completed - wait for response");
2759 if (eloop_register_timeout(2, 0, wpas_dpp_chirp_timeout,
2760 wpa_s, NULL) < 0)
2761 wpas_dpp_chirp_stop(wpa_s);
2762}
2763
2764
2765static void wpas_dpp_chirp_start(struct wpa_supplicant *wpa_s)
2766{
2767 wpa_printf(MSG_DEBUG, "DPP: Chirp on %d MHz", wpa_s->dpp_chirp_freq);
2768 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2769 MAC2STR(broadcast), wpa_s->dpp_chirp_freq,
2770 DPP_PA_PRESENCE_ANNOUNCEMENT);
2771 if (offchannel_send_action(
2772 wpa_s, wpa_s->dpp_chirp_freq, broadcast,
2773 wpa_s->own_addr, broadcast,
2774 wpabuf_head(wpa_s->dpp_presence_announcement),
2775 wpabuf_len(wpa_s->dpp_presence_announcement),
2776 2000, wpas_dpp_chirp_tx_status, 0) < 0)
2777 wpas_dpp_chirp_stop(wpa_s);
2778}
2779
2780
2781static void wpas_dpp_chirp_scan_res_handler(struct wpa_supplicant *wpa_s,
2782 struct wpa_scan_results *scan_res)
2783{
2784 struct dpp_bootstrap_info *bi = wpa_s->dpp_chirp_bi;
2785 unsigned int i;
2786 struct hostapd_hw_modes *mode;
2787 int c;
2788 struct wpa_bss *bss;
2789
2790 if (!bi)
2791 return;
2792
2793 wpa_s->dpp_chirp_scan_done = 1;
2794
2795 os_free(wpa_s->dpp_chirp_freqs);
2796 wpa_s->dpp_chirp_freqs = NULL;
2797
2798 /* Channels from own bootstrapping info */
2799 for (i = 0; i < bi->num_freq; i++)
2800 int_array_add_unique(&wpa_s->dpp_chirp_freqs, bi->freq[i]);
2801
2802 /* Preferred chirping channels */
2803 int_array_add_unique(&wpa_s->dpp_chirp_freqs, 2437);
2804
2805 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
2806 HOSTAPD_MODE_IEEE80211A, 0);
2807 if (mode) {
2808 int chan44 = 0, chan149 = 0;
2809
2810 for (c = 0; c < mode->num_channels; c++) {
2811 struct hostapd_channel_data *chan = &mode->channels[c];
2812
2813 if (chan->flag & (HOSTAPD_CHAN_DISABLED |
2814 HOSTAPD_CHAN_RADAR))
2815 continue;
2816 if (chan->freq == 5220)
2817 chan44 = 1;
2818 if (chan->freq == 5745)
2819 chan149 = 1;
2820 }
2821 if (chan149)
2822 int_array_add_unique(&wpa_s->dpp_chirp_freqs, 5745);
2823 else if (chan44)
2824 int_array_add_unique(&wpa_s->dpp_chirp_freqs, 5220);
2825 }
2826
2827 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
2828 HOSTAPD_MODE_IEEE80211AD, 0);
2829 if (mode) {
2830 for (c = 0; c < mode->num_channels; c++) {
2831 struct hostapd_channel_data *chan = &mode->channels[c];
2832
2833 if ((chan->flag & (HOSTAPD_CHAN_DISABLED |
2834 HOSTAPD_CHAN_RADAR)) ||
2835 chan->freq != 60480)
2836 continue;
2837 int_array_add_unique(&wpa_s->dpp_chirp_freqs, 60480);
2838 break;
2839 }
2840 }
2841
2842 /* Add channels from scan results for APs that advertise Configurator
2843 * Connectivity element */
2844 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2845 if (wpa_bss_get_vendor_ie(bss, DPP_CC_IE_VENDOR_TYPE))
2846 int_array_add_unique(&wpa_s->dpp_chirp_freqs,
2847 bss->freq);
2848 }
2849
2850 if (!wpa_s->dpp_chirp_freqs ||
2851 eloop_register_timeout(0, 0, wpas_dpp_chirp_next, wpa_s, NULL) < 0)
2852 wpas_dpp_chirp_stop(wpa_s);
2853}
2854
2855
2856static void wpas_dpp_chirp_next(void *eloop_ctx, void *timeout_ctx)
2857{
2858 struct wpa_supplicant *wpa_s = eloop_ctx;
2859 int i;
2860
2861 if (wpa_s->dpp_chirp_listen)
2862 wpas_dpp_listen_stop(wpa_s);
2863
2864 if (wpa_s->dpp_chirp_freq == 0) {
2865 if (wpa_s->dpp_chirp_round % 4 == 0 &&
2866 !wpa_s->dpp_chirp_scan_done) {
2867 wpa_printf(MSG_DEBUG,
2868 "DPP: Update channel list for chirping");
2869 wpa_s->scan_req = MANUAL_SCAN_REQ;
2870 wpa_s->scan_res_handler =
2871 wpas_dpp_chirp_scan_res_handler;
2872 wpa_supplicant_req_scan(wpa_s, 0, 0);
2873 return;
2874 }
2875 wpa_s->dpp_chirp_freq = wpa_s->dpp_chirp_freqs[0];
2876 wpa_s->dpp_chirp_round++;
2877 wpa_printf(MSG_DEBUG, "DPP: Start chirping round %d",
2878 wpa_s->dpp_chirp_round);
2879 } else {
2880 for (i = 0; wpa_s->dpp_chirp_freqs[i]; i++)
2881 if (wpa_s->dpp_chirp_freqs[i] == wpa_s->dpp_chirp_freq)
2882 break;
2883 if (!wpa_s->dpp_chirp_freqs[i]) {
2884 wpa_printf(MSG_DEBUG,
2885 "DPP: Previous chirp freq %d not found",
2886 wpa_s->dpp_chirp_freq);
2887 return;
2888 }
2889 i++;
2890 if (wpa_s->dpp_chirp_freqs[i]) {
2891 wpa_s->dpp_chirp_freq = wpa_s->dpp_chirp_freqs[i];
2892 } else {
2893 wpa_s->dpp_chirp_iter--;
2894 if (wpa_s->dpp_chirp_iter <= 0) {
2895 wpa_printf(MSG_DEBUG,
2896 "DPP: Chirping iterations completed");
2897 wpas_dpp_chirp_stop(wpa_s);
2898 return;
2899 }
2900 wpa_s->dpp_chirp_freq = 0;
2901 wpa_s->dpp_chirp_scan_done = 0;
2902 if (eloop_register_timeout(30, 0, wpas_dpp_chirp_next,
2903 wpa_s, NULL) < 0) {
2904 wpas_dpp_chirp_stop(wpa_s);
2905 return;
2906 }
2907 if (wpa_s->dpp_chirp_listen) {
2908 wpa_printf(MSG_DEBUG,
2909 "DPP: Listen on %d MHz during chirp 30 second wait",
2910 wpa_s->dpp_chirp_listen);
2911 wpas_dpp_listen_start(wpa_s,
2912 wpa_s->dpp_chirp_listen);
2913 } else {
2914 wpa_printf(MSG_DEBUG,
2915 "DPP: Wait 30 seconds before starting the next chirping round");
2916 }
2917 return;
2918 }
2919 }
2920
2921 wpas_dpp_chirp_start(wpa_s);
2922}
2923
2924
2925int wpas_dpp_chirp(struct wpa_supplicant *wpa_s, const char *cmd)
2926{
2927 const char *pos;
2928 int iter = 1, listen_freq = 0;
2929 struct dpp_bootstrap_info *bi;
2930
2931 pos = os_strstr(cmd, " own=");
2932 if (!pos)
2933 return -1;
2934 pos += 5;
2935 bi = dpp_bootstrap_get_id(wpa_s->dpp, atoi(pos));
2936 if (!bi) {
2937 wpa_printf(MSG_DEBUG,
2938 "DPP: Identified bootstrap info not found");
2939 return -1;
2940 }
2941
2942 pos = os_strstr(cmd, " iter=");
2943 if (pos) {
2944 iter = atoi(pos + 6);
2945 if (iter <= 0)
2946 return -1;
2947 }
2948
2949 pos = os_strstr(cmd, " listen=");
2950 if (pos) {
2951 listen_freq = atoi(pos + 8);
2952 if (iter <= 0)
2953 return -1;
2954 }
2955
2956 wpas_dpp_chirp_stop(wpa_s);
2957 wpa_s->dpp_allowed_roles = DPP_CAPAB_ENROLLEE;
2958 wpa_s->dpp_chirp_bi = bi;
2959 wpa_s->dpp_presence_announcement = dpp_build_presence_announcement(bi);
2960 if (!wpa_s->dpp_presence_announcement)
2961 return -1;
2962 wpa_s->dpp_chirp_iter = iter;
2963 wpa_s->dpp_chirp_round = 0;
2964 wpa_s->dpp_chirp_scan_done = 0;
2965 wpa_s->dpp_chirp_listen = listen_freq;
2966
2967 return eloop_register_timeout(0, 0, wpas_dpp_chirp_next, wpa_s, NULL);
2968}
2969
2970
2971void wpas_dpp_chirp_stop(struct wpa_supplicant *wpa_s)
2972{
2973 if (wpa_s->dpp_presence_announcement) {
2974 offchannel_send_action_done(wpa_s);
2975 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CHIRP_STOPPED);
2976 }
2977 wpa_s->dpp_chirp_bi = NULL;
2978 wpabuf_free(wpa_s->dpp_presence_announcement);
2979 wpa_s->dpp_presence_announcement = NULL;
2980 if (wpa_s->dpp_chirp_listen)
2981 wpas_dpp_listen_stop(wpa_s);
2982 wpa_s->dpp_chirp_listen = 0;
2983 wpa_s->dpp_chirp_freq = 0;
2984 os_free(wpa_s->dpp_chirp_freqs);
2985 wpa_s->dpp_chirp_freqs = NULL;
2986 eloop_cancel_timeout(wpas_dpp_chirp_next, wpa_s, NULL);
2987 eloop_cancel_timeout(wpas_dpp_chirp_timeout, wpa_s, NULL);
2988 if (wpa_s->scan_res_handler == wpas_dpp_chirp_scan_res_handler) {
2989 wpas_abort_ongoing_scan(wpa_s);
2990 wpa_s->scan_res_handler = NULL;
2991 }
2992}
2993
c02dd10d 2994#endif /* CONFIG_DPP2 */