]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/dpp_supplicant.c
DPP2: Presence Announcement processing at Configurator
[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
06dd3290
JM
1697
1698static void
1699wpas_dpp_rx_presence_announcement(struct wpa_supplicant *wpa_s, const u8 *src,
1700 const u8 *hdr, const u8 *buf, size_t len,
1701 unsigned int freq)
1702{
1703 const u8 *r_bootstrap;
1704 u16 r_bootstrap_len;
1705 struct dpp_bootstrap_info *peer_bi;
1706 struct dpp_authentication *auth;
1707
1708 if (!wpa_s->dpp)
1709 return;
1710
1711 if (wpa_s->dpp_auth) {
1712 wpa_printf(MSG_DEBUG,
1713 "DPP: Ignore Presence Announcement during ongoing Authentication");
1714 return;
1715 }
1716
1717 wpa_printf(MSG_DEBUG, "DPP: Presence Announcement from " MACSTR,
1718 MAC2STR(src));
1719
1720 r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
1721 &r_bootstrap_len);
1722 if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
1723 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1724 "Missing or invalid required Responder Bootstrapping Key Hash attribute");
1725 return;
1726 }
1727 wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
1728 r_bootstrap, r_bootstrap_len);
1729 peer_bi = dpp_bootstrap_find_chirp(wpa_s->dpp, r_bootstrap);
1730 if (!peer_bi) {
1731 wpa_printf(MSG_DEBUG,
1732 "DPP: No matching bootstrapping information found");
1733 return;
1734 }
1735
1736 auth = dpp_auth_init(wpa_s->dpp, wpa_s, peer_bi, NULL,
1737 DPP_CAPAB_CONFIGURATOR, freq, NULL, 0);
1738 if (!auth)
1739 return;
1740 wpas_dpp_set_testing_options(wpa_s, auth);
1741 if (dpp_set_configurator(auth, wpa_s->dpp_configurator_params) < 0) {
1742 dpp_auth_deinit(auth);
1743 return;
1744 }
1745
1746 auth->neg_freq = freq;
1747
1748 if (!is_zero_ether_addr(peer_bi->mac_addr))
1749 os_memcpy(auth->peer_mac_addr, peer_bi->mac_addr, ETH_ALEN);
1750
1751 wpa_s->dpp_auth = auth;
1752 if (wpas_dpp_auth_init_next(wpa_s) < 0) {
1753 dpp_auth_deinit(wpa_s->dpp_auth);
1754 wpa_s->dpp_auth = NULL;
1755 }
1756}
1757
22f90b32
JM
1758#endif /* CONFIG_DPP2 */
1759
1760
a0d5c56f
JM
1761static void wpas_dpp_rx_peer_disc_resp(struct wpa_supplicant *wpa_s,
1762 const u8 *src,
1763 const u8 *buf, size_t len)
1764{
1765 struct wpa_ssid *ssid;
e85b6601
JM
1766 const u8 *connector, *trans_id, *status;
1767 u16 connector_len, trans_id_len, status_len;
a0d5c56f
JM
1768 struct dpp_introduction intro;
1769 struct rsn_pmksa_cache_entry *entry;
787615b3
JM
1770 struct os_time now;
1771 struct os_reltime rnow;
1772 os_time_t expiry;
1773 unsigned int seconds;
e85b6601 1774 enum dpp_status_error res;
a0d5c56f
JM
1775
1776 wpa_printf(MSG_DEBUG, "DPP: Peer Discovery Response from " MACSTR,
1777 MAC2STR(src));
1778 if (is_zero_ether_addr(wpa_s->dpp_intro_bssid) ||
1779 os_memcmp(src, wpa_s->dpp_intro_bssid, ETH_ALEN) != 0) {
1780 wpa_printf(MSG_DEBUG, "DPP: Not waiting for response from "
1781 MACSTR " - drop", MAC2STR(src));
1782 return;
1783 }
1784 offchannel_send_action_done(wpa_s);
1785
1786 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1787 if (ssid == wpa_s->dpp_intro_network)
1788 break;
1789 }
1790 if (!ssid || !ssid->dpp_connector || !ssid->dpp_netaccesskey ||
1791 !ssid->dpp_csign) {
1792 wpa_printf(MSG_DEBUG,
1793 "DPP: Profile not found for network introduction");
1794 return;
1795 }
1796
85fd8263
JM
1797 trans_id = dpp_get_attr(buf, len, DPP_ATTR_TRANSACTION_ID,
1798 &trans_id_len);
1799 if (!trans_id || trans_id_len != 1) {
1800 wpa_printf(MSG_DEBUG,
1801 "DPP: Peer did not include Transaction ID");
e85b6601
JM
1802 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1803 " fail=missing_transaction_id", MAC2STR(src));
85fd8263
JM
1804 goto fail;
1805 }
1806 if (trans_id[0] != TRANSACTION_ID) {
1807 wpa_printf(MSG_DEBUG,
1808 "DPP: Ignore frame with unexpected Transaction ID %u",
1809 trans_id[0]);
e85b6601
JM
1810 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1811 " fail=transaction_id_mismatch", MAC2STR(src));
1812 goto fail;
1813 }
1814
1815 status = dpp_get_attr(buf, len, DPP_ATTR_STATUS, &status_len);
1816 if (!status || status_len != 1) {
1817 wpa_printf(MSG_DEBUG, "DPP: Peer did not include Status");
1818 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1819 " fail=missing_status", MAC2STR(src));
1820 goto fail;
1821 }
1822 if (status[0] != DPP_STATUS_OK) {
1823 wpa_printf(MSG_DEBUG,
1824 "DPP: Peer rejected network introduction: Status %u",
1825 status[0]);
1826 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1827 " status=%u", MAC2STR(src), status[0]);
16ef233b
JM
1828#ifdef CONFIG_DPP2
1829 wpas_dpp_send_conn_status_result(wpa_s, status[0]);
1830#endif /* CONFIG_DPP2 */
85fd8263
JM
1831 goto fail;
1832 }
1833
a0d5c56f
JM
1834 connector = dpp_get_attr(buf, len, DPP_ATTR_CONNECTOR, &connector_len);
1835 if (!connector) {
1836 wpa_printf(MSG_DEBUG,
1837 "DPP: Peer did not include its Connector");
e85b6601
JM
1838 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1839 " fail=missing_connector", MAC2STR(src));
1840 goto fail;
a0d5c56f
JM
1841 }
1842
e85b6601
JM
1843 res = dpp_peer_intro(&intro, ssid->dpp_connector,
1844 ssid->dpp_netaccesskey,
1845 ssid->dpp_netaccesskey_len,
1846 ssid->dpp_csign,
1847 ssid->dpp_csign_len,
1848 connector, connector_len, &expiry);
1849 if (res != DPP_STATUS_OK) {
a0d5c56f
JM
1850 wpa_printf(MSG_INFO,
1851 "DPP: Network Introduction protocol resulted in failure");
e85b6601
JM
1852 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1853 " fail=peer_connector_validation_failed", MAC2STR(src));
16ef233b
JM
1854#ifdef CONFIG_DPP2
1855 wpas_dpp_send_conn_status_result(wpa_s, res);
1856#endif /* CONFIG_DPP2 */
a0d5c56f
JM
1857 goto fail;
1858 }
1859
a0d5c56f
JM
1860 entry = os_zalloc(sizeof(*entry));
1861 if (!entry)
1862 goto fail;
1863 os_memcpy(entry->aa, src, ETH_ALEN);
1864 os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN);
1865 os_memcpy(entry->pmk, intro.pmk, intro.pmk_len);
1866 entry->pmk_len = intro.pmk_len;
1867 entry->akmp = WPA_KEY_MGMT_DPP;
787615b3
JM
1868 if (expiry) {
1869 os_get_time(&now);
1870 seconds = expiry - now.sec;
1871 } else {
1872 seconds = 86400 * 7;
1873 }
1874 os_get_reltime(&rnow);
1875 entry->expiration = rnow.sec + seconds;
1876 entry->reauth_time = rnow.sec + seconds;
a0d5c56f
JM
1877 entry->network_ctx = ssid;
1878 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
1879
e85b6601
JM
1880 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1881 " status=%u", MAC2STR(src), status[0]);
1882
a0d5c56f
JM
1883 wpa_printf(MSG_DEBUG,
1884 "DPP: Try connection again after successful network introduction");
1885 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
1886 wpa_supplicant_cancel_sched_scan(wpa_s);
1887 wpa_supplicant_req_scan(wpa_s, 0, 0);
1888 }
1889fail:
1890 os_memset(&intro, 0, sizeof(intro));
1891}
1892
1893
3b50f8a4
JM
1894static int wpas_dpp_allow_ir(struct wpa_supplicant *wpa_s, unsigned int freq)
1895{
1896 int i, j;
1897
1898 if (!wpa_s->hw.modes)
1899 return -1;
1900
1901 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1902 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1903
1904 for (j = 0; j < mode->num_channels; j++) {
1905 struct hostapd_channel_data *chan = &mode->channels[j];
1906
1907 if (chan->freq != (int) freq)
1908 continue;
1909
1910 if (chan->flag & (HOSTAPD_CHAN_DISABLED |
1911 HOSTAPD_CHAN_NO_IR |
1912 HOSTAPD_CHAN_RADAR))
1913 continue;
1914
1915 return 1;
1916 }
1917 }
1918
1919 wpa_printf(MSG_DEBUG,
1920 "DPP: Frequency %u MHz not supported or does not allow PKEX initiation in the current channel list",
1921 freq);
1922
1923 return 0;
1924}
1925
1926
1927static int wpas_dpp_pkex_next_channel(struct wpa_supplicant *wpa_s,
1928 struct dpp_pkex *pkex)
1929{
1930 if (pkex->freq == 2437)
1931 pkex->freq = 5745;
1932 else if (pkex->freq == 5745)
1933 pkex->freq = 5220;
1934 else if (pkex->freq == 5220)
1935 pkex->freq = 60480;
1936 else
1937 return -1; /* no more channels to try */
1938
1939 if (wpas_dpp_allow_ir(wpa_s, pkex->freq) == 1) {
1940 wpa_printf(MSG_DEBUG, "DPP: Try to initiate on %u MHz",
1941 pkex->freq);
1942 return 0;
1943 }
1944
1945 /* Could not use this channel - try the next one */
1946 return wpas_dpp_pkex_next_channel(wpa_s, pkex);
1947}
1948
1949
00d2d13d
JM
1950static void wpas_dpp_pkex_retry_timeout(void *eloop_ctx, void *timeout_ctx)
1951{
1952 struct wpa_supplicant *wpa_s = eloop_ctx;
1953 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1954
1955 if (!pkex || !pkex->exchange_req)
1956 return;
1957 if (pkex->exch_req_tries >= 5) {
3b50f8a4
JM
1958 if (wpas_dpp_pkex_next_channel(wpa_s, pkex) < 0) {
1959 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1960 "No response from PKEX peer");
1961 dpp_pkex_free(pkex);
1962 wpa_s->dpp_pkex = NULL;
1963 return;
1964 }
1965 pkex->exch_req_tries = 0;
00d2d13d
JM
1966 }
1967
1968 pkex->exch_req_tries++;
1969 wpa_printf(MSG_DEBUG, "DPP: Retransmit PKEX Exchange Request (try %u)",
1970 pkex->exch_req_tries);
1971 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1972 MAC2STR(broadcast), pkex->freq, DPP_PA_PKEX_EXCHANGE_REQ);
1973 offchannel_send_action(wpa_s, pkex->freq, broadcast,
1974 wpa_s->own_addr, broadcast,
1975 wpabuf_head(pkex->exchange_req),
1976 wpabuf_len(pkex->exchange_req),
1977 pkex->exch_req_wait_time,
1978 wpas_dpp_tx_pkex_status, 0);
1979}
1980
1981
500ed7f0
JM
1982static void
1983wpas_dpp_tx_pkex_status(struct wpa_supplicant *wpa_s,
1984 unsigned int freq, const u8 *dst,
1985 const u8 *src, const u8 *bssid,
1986 const u8 *data, size_t data_len,
1987 enum offchannel_send_action_result result)
1988{
af48810b 1989 const char *res_txt;
00d2d13d 1990 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
af48810b
JM
1991
1992 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
1993 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
1994 "FAILED");
500ed7f0
JM
1995 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
1996 " result=%s (PKEX)",
af48810b
JM
1997 freq, MAC2STR(dst), res_txt);
1998 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
1999 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
e0247e79 2000
00d2d13d 2001 if (!pkex) {
e0247e79
JM
2002 wpa_printf(MSG_DEBUG,
2003 "DPP: Ignore TX status since there is no ongoing PKEX exchange");
2004 return;
2005 }
2006
00d2d13d 2007 if (pkex->failed) {
e0247e79
JM
2008 wpa_printf(MSG_DEBUG,
2009 "DPP: Terminate PKEX exchange due to an earlier error");
00d2d13d
JM
2010 if (pkex->t > pkex->own_bi->pkex_t)
2011 pkex->own_bi->pkex_t = pkex->t;
2012 dpp_pkex_free(pkex);
e0247e79 2013 wpa_s->dpp_pkex = NULL;
00d2d13d
JM
2014 return;
2015 }
2016
2017 if (pkex->exch_req_wait_time && pkex->exchange_req) {
2018 /* Wait for PKEX Exchange Response frame and retry request if
2019 * no response is seen. */
2020 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
2021 eloop_register_timeout(pkex->exch_req_wait_time / 1000,
2022 (pkex->exch_req_wait_time % 1000) * 1000,
2023 wpas_dpp_pkex_retry_timeout, wpa_s,
2024 NULL);
e0247e79 2025 }
500ed7f0
JM
2026}
2027
2028
2029static void
2030wpas_dpp_rx_pkex_exchange_req(struct wpa_supplicant *wpa_s, const u8 *src,
2031 const u8 *buf, size_t len, unsigned int freq)
2032{
2033 struct wpabuf *msg;
2034 unsigned int wait_time;
2035
2036 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Request from " MACSTR,
2037 MAC2STR(src));
2038
2039 /* TODO: Support multiple PKEX codes by iterating over all the enabled
2040 * values here */
2041
2042 if (!wpa_s->dpp_pkex_code || !wpa_s->dpp_pkex_bi) {
2043 wpa_printf(MSG_DEBUG,
2044 "DPP: No PKEX code configured - ignore request");
2045 return;
2046 }
2047
2048 if (wpa_s->dpp_pkex) {
2049 /* TODO: Support parallel operations */
2050 wpa_printf(MSG_DEBUG,
2051 "DPP: Already in PKEX session - ignore new request");
2052 return;
2053 }
2054
219d4c9f 2055 wpa_s->dpp_pkex = dpp_pkex_rx_exchange_req(wpa_s, wpa_s->dpp_pkex_bi,
500ed7f0
JM
2056 wpa_s->own_addr, src,
2057 wpa_s->dpp_pkex_identifier,
2058 wpa_s->dpp_pkex_code,
2059 buf, len);
2060 if (!wpa_s->dpp_pkex) {
2061 wpa_printf(MSG_DEBUG,
2062 "DPP: Failed to process the request - ignore it");
2063 return;
2064 }
2065
2066 msg = wpa_s->dpp_pkex->exchange_resp;
2067 wait_time = wpa_s->max_remain_on_chan;
2068 if (wait_time > 2000)
2069 wait_time = 2000;
af48810b
JM
2070 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2071 MAC2STR(src), freq, DPP_PA_PKEX_EXCHANGE_RESP);
500ed7f0
JM
2072 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
2073 broadcast,
2074 wpabuf_head(msg), wpabuf_len(msg),
2075 wait_time, wpas_dpp_tx_pkex_status, 0);
2076}
2077
2078
2079static void
2080wpas_dpp_rx_pkex_exchange_resp(struct wpa_supplicant *wpa_s, const u8 *src,
2081 const u8 *buf, size_t len, unsigned int freq)
2082{
2083 struct wpabuf *msg;
2084 unsigned int wait_time;
2085
2086 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Response from " MACSTR,
2087 MAC2STR(src));
2088
2089 /* TODO: Support multiple PKEX codes by iterating over all the enabled
2090 * values here */
2091
2092 if (!wpa_s->dpp_pkex || !wpa_s->dpp_pkex->initiator ||
2093 wpa_s->dpp_pkex->exchange_done) {
2094 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2095 return;
2096 }
2097
00d2d13d
JM
2098 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
2099 wpa_s->dpp_pkex->exch_req_wait_time = 0;
2100
af4103e5 2101 msg = dpp_pkex_rx_exchange_resp(wpa_s->dpp_pkex, src, buf, len);
500ed7f0
JM
2102 if (!msg) {
2103 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
2104 return;
2105 }
2106
2107 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Request to " MACSTR,
2108 MAC2STR(src));
2109
2110 wait_time = wpa_s->max_remain_on_chan;
2111 if (wait_time > 2000)
2112 wait_time = 2000;
af48810b
JM
2113 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2114 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_REQ);
500ed7f0
JM
2115 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
2116 broadcast,
2117 wpabuf_head(msg), wpabuf_len(msg),
2118 wait_time, wpas_dpp_tx_pkex_status, 0);
2119 wpabuf_free(msg);
2120}
2121
2122
de029861
JM
2123static struct dpp_bootstrap_info *
2124wpas_dpp_pkex_finish(struct wpa_supplicant *wpa_s, const u8 *peer,
2125 unsigned int freq)
2126{
de029861
JM
2127 struct dpp_bootstrap_info *bi;
2128
87d8435c 2129 bi = dpp_pkex_finish(wpa_s->dpp, wpa_s->dpp_pkex, peer, freq);
de029861
JM
2130 if (!bi)
2131 return NULL;
de029861 2132 wpa_s->dpp_pkex = NULL;
de029861
JM
2133 return bi;
2134}
2135
2136
500ed7f0
JM
2137static void
2138wpas_dpp_rx_pkex_commit_reveal_req(struct wpa_supplicant *wpa_s, const u8 *src,
4be5bc98
JM
2139 const u8 *hdr, const u8 *buf, size_t len,
2140 unsigned int freq)
500ed7f0
JM
2141{
2142 struct wpabuf *msg;
2143 unsigned int wait_time;
2144 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
500ed7f0
JM
2145
2146 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Request from " MACSTR,
2147 MAC2STR(src));
2148
2149 if (!pkex || pkex->initiator || !pkex->exchange_done) {
2150 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2151 return;
2152 }
2153
4be5bc98 2154 msg = dpp_pkex_rx_commit_reveal_req(pkex, hdr, buf, len);
500ed7f0
JM
2155 if (!msg) {
2156 wpa_printf(MSG_DEBUG, "DPP: Failed to process the request");
039b8e73
JM
2157 if (pkex->failed) {
2158 wpa_printf(MSG_DEBUG, "DPP: Terminate PKEX exchange");
29ab69e4
JM
2159 if (pkex->t > pkex->own_bi->pkex_t)
2160 pkex->own_bi->pkex_t = pkex->t;
039b8e73
JM
2161 dpp_pkex_free(wpa_s->dpp_pkex);
2162 wpa_s->dpp_pkex = NULL;
2163 }
500ed7f0
JM
2164 return;
2165 }
2166
2167 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Response to "
2168 MACSTR, MAC2STR(src));
2169
2170 wait_time = wpa_s->max_remain_on_chan;
2171 if (wait_time > 2000)
2172 wait_time = 2000;
af48810b
JM
2173 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2174 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_RESP);
500ed7f0
JM
2175 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
2176 broadcast,
2177 wpabuf_head(msg), wpabuf_len(msg),
2178 wait_time, wpas_dpp_tx_pkex_status, 0);
2179 wpabuf_free(msg);
2180
de029861 2181 wpas_dpp_pkex_finish(wpa_s, src, freq);
500ed7f0
JM
2182}
2183
2184
2185static void
2186wpas_dpp_rx_pkex_commit_reveal_resp(struct wpa_supplicant *wpa_s, const u8 *src,
4be5bc98 2187 const u8 *hdr, const u8 *buf, size_t len,
500ed7f0
JM
2188 unsigned int freq)
2189{
2190 int res;
de029861 2191 struct dpp_bootstrap_info *bi;
500ed7f0
JM
2192 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
2193 char cmd[500];
2194
2195 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Response from " MACSTR,
2196 MAC2STR(src));
2197
2198 if (!pkex || !pkex->initiator || !pkex->exchange_done) {
2199 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2200 return;
2201 }
2202
4be5bc98 2203 res = dpp_pkex_rx_commit_reveal_resp(pkex, hdr, buf, len);
500ed7f0
JM
2204 if (res < 0) {
2205 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
2206 return;
2207 }
2208
de029861 2209 bi = wpas_dpp_pkex_finish(wpa_s, src, freq);
500ed7f0
JM
2210 if (!bi)
2211 return;
500ed7f0
JM
2212
2213 os_snprintf(cmd, sizeof(cmd), " peer=%u %s",
2214 bi->id,
2215 wpa_s->dpp_pkex_auth_cmd ? wpa_s->dpp_pkex_auth_cmd : "");
2216 wpa_printf(MSG_DEBUG,
2217 "DPP: Start authentication after PKEX with parameters: %s",
2218 cmd);
2219 if (wpas_dpp_auth_init(wpa_s, cmd) < 0) {
2220 wpa_printf(MSG_DEBUG,
2221 "DPP: Authentication initialization failed");
c304bddc 2222 offchannel_send_action_done(wpa_s);
500ed7f0
JM
2223 return;
2224 }
2225}
2226
2227
30d27b04
JM
2228void wpas_dpp_rx_action(struct wpa_supplicant *wpa_s, const u8 *src,
2229 const u8 *buf, size_t len, unsigned int freq)
2230{
8c19ea3f 2231 u8 crypto_suite;
30d27b04 2232 enum dpp_public_action_frame_type type;
dc4d271c 2233 const u8 *hdr;
29ab69e4 2234 unsigned int pkex_t;
30d27b04 2235
dc4d271c
JM
2236 if (len < DPP_HDR_LEN)
2237 return;
2238 if (WPA_GET_BE24(buf) != OUI_WFA || buf[3] != DPP_OUI_TYPE)
30d27b04 2239 return;
dc4d271c
JM
2240 hdr = buf;
2241 buf += 4;
2242 len -= 4;
8c19ea3f
JM
2243 crypto_suite = *buf++;
2244 type = *buf++;
2245 len -= 2;
30d27b04
JM
2246
2247 wpa_printf(MSG_DEBUG,
8c19ea3f 2248 "DPP: Received DPP Public Action frame crypto suite %u type %d from "
30d27b04 2249 MACSTR " freq=%u",
8c19ea3f
JM
2250 crypto_suite, type, MAC2STR(src), freq);
2251 if (crypto_suite != 1) {
2252 wpa_printf(MSG_DEBUG, "DPP: Unsupported crypto suite %u",
2253 crypto_suite);
a7073934
JM
2254 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2255 " freq=%u type=%d ignore=unsupported-crypto-suite",
2256 MAC2STR(src), freq, type);
8c19ea3f
JM
2257 return;
2258 }
30d27b04 2259 wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes", buf, len);
a7073934
JM
2260 if (dpp_check_attrs(buf, len) < 0) {
2261 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2262 " freq=%u type=%d ignore=invalid-attributes",
2263 MAC2STR(src), freq, type);
30d27b04 2264 return;
a7073934
JM
2265 }
2266 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR " freq=%u type=%d",
2267 MAC2STR(src), freq, type);
30d27b04
JM
2268
2269 switch (type) {
2270 case DPP_PA_AUTHENTICATION_REQ:
dc4d271c 2271 wpas_dpp_rx_auth_req(wpa_s, src, hdr, buf, len, freq);
30d27b04
JM
2272 break;
2273 case DPP_PA_AUTHENTICATION_RESP:
d2709206 2274 wpas_dpp_rx_auth_resp(wpa_s, src, hdr, buf, len, freq);
30d27b04
JM
2275 break;
2276 case DPP_PA_AUTHENTICATION_CONF:
dc4d271c 2277 wpas_dpp_rx_auth_conf(wpa_s, src, hdr, buf, len);
30d27b04 2278 break;
a0d5c56f
JM
2279 case DPP_PA_PEER_DISCOVERY_RESP:
2280 wpas_dpp_rx_peer_disc_resp(wpa_s, src, buf, len);
2281 break;
500ed7f0
JM
2282 case DPP_PA_PKEX_EXCHANGE_REQ:
2283 wpas_dpp_rx_pkex_exchange_req(wpa_s, src, buf, len, freq);
2284 break;
2285 case DPP_PA_PKEX_EXCHANGE_RESP:
2286 wpas_dpp_rx_pkex_exchange_resp(wpa_s, src, buf, len, freq);
2287 break;
2288 case DPP_PA_PKEX_COMMIT_REVEAL_REQ:
4be5bc98
JM
2289 wpas_dpp_rx_pkex_commit_reveal_req(wpa_s, src, hdr, buf, len,
2290 freq);
500ed7f0
JM
2291 break;
2292 case DPP_PA_PKEX_COMMIT_REVEAL_RESP:
4be5bc98
JM
2293 wpas_dpp_rx_pkex_commit_reveal_resp(wpa_s, src, hdr, buf, len,
2294 freq);
500ed7f0 2295 break;
22f90b32
JM
2296#ifdef CONFIG_DPP2
2297 case DPP_PA_CONFIGURATION_RESULT:
2298 wpas_dpp_rx_conf_result(wpa_s, src, hdr, buf, len);
2299 break;
b10e01a7
JM
2300 case DPP_PA_CONNECTION_STATUS_RESULT:
2301 wpas_dpp_rx_conn_status_result(wpa_s, src, hdr, buf, len);
2302 break;
06dd3290
JM
2303 case DPP_PA_PRESENCE_ANNOUNCEMENT:
2304 wpas_dpp_rx_presence_announcement(wpa_s, src, hdr, buf, len,
2305 freq);
2306 break;
22f90b32 2307#endif /* CONFIG_DPP2 */
30d27b04
JM
2308 default:
2309 wpa_printf(MSG_DEBUG,
2310 "DPP: Ignored unsupported frame subtype %d", type);
2311 break;
2312 }
29ab69e4
JM
2313
2314 if (wpa_s->dpp_pkex)
2315 pkex_t = wpa_s->dpp_pkex->t;
2316 else if (wpa_s->dpp_pkex_bi)
2317 pkex_t = wpa_s->dpp_pkex_bi->pkex_t;
2318 else
2319 pkex_t = 0;
2320 if (pkex_t >= PKEX_COUNTER_T_LIMIT) {
2321 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_PKEX_T_LIMIT "id=0");
2322 wpas_dpp_pkex_remove(wpa_s, "*");
2323 }
30d27b04
JM
2324}
2325
2326
461d39af
JM
2327static struct wpabuf *
2328wpas_dpp_gas_req_handler(void *ctx, const u8 *sa, const u8 *query,
2329 size_t query_len)
2330{
2331 struct wpa_supplicant *wpa_s = ctx;
2332 struct dpp_authentication *auth = wpa_s->dpp_auth;
2333 struct wpabuf *resp;
2334
2335 wpa_printf(MSG_DEBUG, "DPP: GAS request from " MACSTR,
2336 MAC2STR(sa));
2337 if (!auth || !auth->auth_success ||
2338 os_memcmp(sa, auth->peer_mac_addr, ETH_ALEN) != 0) {
2339 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
2340 return NULL;
2341 }
422e73d6
JM
2342
2343 if (wpa_s->dpp_auth_ok_on_ack && auth->configurator) {
2344 wpa_printf(MSG_DEBUG,
2345 "DPP: Have not received ACK for Auth Confirm yet - assume it was received based on this GAS request");
2346 /* wpas_dpp_auth_success() would normally have been called from
2347 * TX status handler, but since there was no such handler call
2348 * yet, simply send out the event message and proceed with
2349 * exchange. */
2350 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=1");
2351 wpa_s->dpp_auth_ok_on_ack = 0;
2352 }
2353
461d39af
JM
2354 wpa_hexdump(MSG_DEBUG,
2355 "DPP: Received Configuration Request (GAS Query Request)",
2356 query, query_len);
fd920954
JM
2357 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_REQ_RX "src=" MACSTR,
2358 MAC2STR(sa));
461d39af
JM
2359 resp = dpp_conf_req_rx(auth, query, query_len);
2360 if (!resp)
2361 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
82feacce 2362 auth->conf_resp = resp;
461d39af
JM
2363 return resp;
2364}
2365
2366
2367static void
2368wpas_dpp_gas_status_handler(void *ctx, struct wpabuf *resp, int ok)
2369{
2370 struct wpa_supplicant *wpa_s = ctx;
2371 struct dpp_authentication *auth = wpa_s->dpp_auth;
2372
2373 if (!auth) {
2374 wpabuf_free(resp);
2375 return;
2376 }
82feacce
JM
2377 if (auth->conf_resp != resp) {
2378 wpa_printf(MSG_DEBUG,
2379 "DPP: Ignore GAS status report (ok=%d) for unknown response",
2380 ok);
2381 wpabuf_free(resp);
2382 return;
2383 }
461d39af
JM
2384
2385 wpa_printf(MSG_DEBUG, "DPP: Configuration exchange completed (ok=%d)",
2386 ok);
2387 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
95b0104a 2388 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
22f90b32
JM
2389#ifdef CONFIG_DPP2
2390 if (ok && auth->peer_version >= 2 &&
2391 auth->conf_resp_status == DPP_STATUS_OK) {
2392 wpa_printf(MSG_DEBUG, "DPP: Wait for Configuration Result");
2393 auth->waiting_conf_result = 1;
2394 auth->conf_resp = NULL;
2395 wpabuf_free(resp);
2396 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout,
2397 wpa_s, NULL);
2398 eloop_register_timeout(2, 0,
2399 wpas_dpp_config_result_wait_timeout,
2400 wpa_s, NULL);
2401 return;
2402 }
2403#endif /* CONFIG_DPP2 */
461d39af
JM
2404 offchannel_send_action_done(wpa_s);
2405 wpas_dpp_listen_stop(wpa_s);
2406 if (ok)
2407 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
2408 else
2409 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
2410 dpp_auth_deinit(wpa_s->dpp_auth);
2411 wpa_s->dpp_auth = NULL;
2412 wpabuf_free(resp);
2413}
2414
2415
f522bb23
JM
2416int wpas_dpp_configurator_sign(struct wpa_supplicant *wpa_s, const char *cmd)
2417{
2418 struct dpp_authentication *auth;
2419 int ret = -1;
2420 char *curve = NULL;
2421
514cc49b 2422 auth = dpp_alloc_auth(wpa_s->dpp, wpa_s);
f522bb23
JM
2423 if (!auth)
2424 return -1;
2425
2426 curve = get_param(cmd, " curve=");
e22c2338 2427 wpas_dpp_set_testing_options(wpa_s, auth);
514cc49b 2428 if (dpp_set_configurator(auth, cmd) == 0 &&
22f90b32 2429 dpp_configurator_own_config(auth, curve, 0) == 0)
52d469de
JM
2430 ret = wpas_dpp_handle_config_obj(wpa_s, auth,
2431 &auth->conf_obj[0]);
2432 if (!ret)
2433 wpas_dpp_post_process_config(wpa_s, auth);
f522bb23
JM
2434
2435 dpp_auth_deinit(auth);
2436 os_free(curve);
2437
2438 return ret;
2439}
2440
2441
a0d5c56f
JM
2442static void
2443wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
2444 unsigned int freq, const u8 *dst,
2445 const u8 *src, const u8 *bssid,
2446 const u8 *data, size_t data_len,
2447 enum offchannel_send_action_result result)
2448{
af48810b
JM
2449 const char *res_txt;
2450
2451 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
2452 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
2453 "FAILED");
a0d5c56f
JM
2454 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
2455 " result=%s (DPP Peer Discovery Request)",
af48810b
JM
2456 freq, MAC2STR(dst), res_txt);
2457 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
2458 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
a0d5c56f
JM
2459 /* TODO: Time out wait for response more quickly in error cases? */
2460}
2461
2462
2463int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2464 struct wpa_bss *bss)
2465{
2466 struct os_time now;
2467 struct wpabuf *msg;
2468 unsigned int wait_time;
dd6c5980
JM
2469 const u8 *rsn;
2470 struct wpa_ie_data ied;
a0d5c56f
JM
2471
2472 if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
2473 return 0; /* Not using DPP AKM - continue */
dd6c5980
JM
2474 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2475 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0 &&
2476 !(ied.key_mgmt & WPA_KEY_MGMT_DPP))
2477 return 0; /* AP does not support DPP AKM - continue */
a0d5c56f
JM
2478 if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
2479 return 0; /* PMKSA exists for DPP AKM - continue */
2480
2481 if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
2482 !ssid->dpp_csign) {
2483 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2484 "missing %s",
2485 !ssid->dpp_connector ? "Connector" :
2486 (!ssid->dpp_netaccesskey ? "netAccessKey" :
2487 "C-sign-key"));
2488 return -1;
2489 }
2490
2491 os_get_time(&now);
2492
a0d5c56f 2493 if (ssid->dpp_netaccesskey_expiry &&
3ca4be1e 2494 (os_time_t) ssid->dpp_netaccesskey_expiry < now.sec) {
a0d5c56f
JM
2495 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2496 "netAccessKey expired");
2497 return -1;
2498 }
2499
2500 wpa_printf(MSG_DEBUG,
2501 "DPP: Starting network introduction protocol to derive PMKSA for "
2502 MACSTR, MAC2STR(bss->bssid));
2503
2504 msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
85fd8263 2505 5 + 4 + os_strlen(ssid->dpp_connector));
a0d5c56f
JM
2506 if (!msg)
2507 return -1;
2508
a306ed5a
JM
2509#ifdef CONFIG_TESTING_OPTIONS
2510 if (dpp_test == DPP_TEST_NO_TRANSACTION_ID_PEER_DISC_REQ) {
2511 wpa_printf(MSG_INFO, "DPP: TESTING - no Transaction ID");
2512 goto skip_trans_id;
2513 }
55c6c858
JM
2514 if (dpp_test == DPP_TEST_INVALID_TRANSACTION_ID_PEER_DISC_REQ) {
2515 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Transaction ID");
2516 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2517 wpabuf_put_le16(msg, 0);
2518 goto skip_trans_id;
2519 }
a306ed5a
JM
2520#endif /* CONFIG_TESTING_OPTIONS */
2521
85fd8263
JM
2522 /* Transaction ID */
2523 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2524 wpabuf_put_le16(msg, 1);
2525 wpabuf_put_u8(msg, TRANSACTION_ID);
2526
a306ed5a
JM
2527#ifdef CONFIG_TESTING_OPTIONS
2528skip_trans_id:
2529 if (dpp_test == DPP_TEST_NO_CONNECTOR_PEER_DISC_REQ) {
2530 wpa_printf(MSG_INFO, "DPP: TESTING - no Connector");
2531 goto skip_connector;
2532 }
4b8de0c9
JM
2533 if (dpp_test == DPP_TEST_INVALID_CONNECTOR_PEER_DISC_REQ) {
2534 char *connector;
2535
2536 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Connector");
2537 connector = dpp_corrupt_connector_signature(
2538 ssid->dpp_connector);
2539 if (!connector) {
2540 wpabuf_free(msg);
2541 return -1;
2542 }
2543 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2544 wpabuf_put_le16(msg, os_strlen(connector));
2545 wpabuf_put_str(msg, connector);
2546 os_free(connector);
2547 goto skip_connector;
2548 }
a306ed5a
JM
2549#endif /* CONFIG_TESTING_OPTIONS */
2550
a0d5c56f
JM
2551 /* DPP Connector */
2552 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2553 wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
2554 wpabuf_put_str(msg, ssid->dpp_connector);
2555
a306ed5a
JM
2556#ifdef CONFIG_TESTING_OPTIONS
2557skip_connector:
2558#endif /* CONFIG_TESTING_OPTIONS */
2559
a0d5c56f
JM
2560 /* TODO: Timeout on AP response */
2561 wait_time = wpa_s->max_remain_on_chan;
2562 if (wait_time > 2000)
2563 wait_time = 2000;
af48810b
JM
2564 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2565 MAC2STR(bss->bssid), bss->freq, DPP_PA_PEER_DISCOVERY_REQ);
a0d5c56f
JM
2566 offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
2567 broadcast,
2568 wpabuf_head(msg), wpabuf_len(msg),
2569 wait_time, wpas_dpp_tx_introduction_status, 0);
2570 wpabuf_free(msg);
2571
2572 /* Request this connection attempt to terminate - new one will be
2573 * started when network introduction protocol completes */
2574 os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
2575 wpa_s->dpp_intro_network = ssid;
2576 return 1;
2577}
2578
2579
500ed7f0
JM
2580int wpas_dpp_pkex_add(struct wpa_supplicant *wpa_s, const char *cmd)
2581{
2582 struct dpp_bootstrap_info *own_bi;
2583 const char *pos, *end;
2584 unsigned int wait_time;
2585
2586 pos = os_strstr(cmd, " own=");
2587 if (!pos)
2588 return -1;
2589 pos += 5;
87d8435c 2590 own_bi = dpp_bootstrap_get_id(wpa_s->dpp, atoi(pos));
500ed7f0
JM
2591 if (!own_bi) {
2592 wpa_printf(MSG_DEBUG,
2593 "DPP: Identified bootstrap info not found");
2594 return -1;
2595 }
2596 if (own_bi->type != DPP_BOOTSTRAP_PKEX) {
2597 wpa_printf(MSG_DEBUG,
2598 "DPP: Identified bootstrap info not for PKEX");
2599 return -1;
2600 }
2601 wpa_s->dpp_pkex_bi = own_bi;
29ab69e4 2602 own_bi->pkex_t = 0; /* clear pending errors on new code */
500ed7f0
JM
2603
2604 os_free(wpa_s->dpp_pkex_identifier);
2605 wpa_s->dpp_pkex_identifier = NULL;
2606 pos = os_strstr(cmd, " identifier=");
2607 if (pos) {
2608 pos += 12;
2609 end = os_strchr(pos, ' ');
2610 if (!end)
2611 return -1;
2612 wpa_s->dpp_pkex_identifier = os_malloc(end - pos + 1);
2613 if (!wpa_s->dpp_pkex_identifier)
2614 return -1;
2615 os_memcpy(wpa_s->dpp_pkex_identifier, pos, end - pos);
2616 wpa_s->dpp_pkex_identifier[end - pos] = '\0';
2617 }
2618
2619 pos = os_strstr(cmd, " code=");
2620 if (!pos)
2621 return -1;
2622 os_free(wpa_s->dpp_pkex_code);
2623 wpa_s->dpp_pkex_code = os_strdup(pos + 6);
2624 if (!wpa_s->dpp_pkex_code)
2625 return -1;
2626
2627 if (os_strstr(cmd, " init=1")) {
00d2d13d 2628 struct dpp_pkex *pkex;
500ed7f0
JM
2629 struct wpabuf *msg;
2630
2631 wpa_printf(MSG_DEBUG, "DPP: Initiating PKEX");
2632 dpp_pkex_free(wpa_s->dpp_pkex);
219d4c9f 2633 wpa_s->dpp_pkex = dpp_pkex_init(wpa_s, own_bi, wpa_s->own_addr,
500ed7f0
JM
2634 wpa_s->dpp_pkex_identifier,
2635 wpa_s->dpp_pkex_code);
00d2d13d
JM
2636 pkex = wpa_s->dpp_pkex;
2637 if (!pkex)
500ed7f0
JM
2638 return -1;
2639
00d2d13d 2640 msg = pkex->exchange_req;
500ed7f0
JM
2641 wait_time = wpa_s->max_remain_on_chan;
2642 if (wait_time > 2000)
2643 wait_time = 2000;
00d2d13d 2644 pkex->freq = 2437;
af48810b
JM
2645 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
2646 " freq=%u type=%d",
00d2d13d
JM
2647 MAC2STR(broadcast), pkex->freq,
2648 DPP_PA_PKEX_EXCHANGE_REQ);
2649 offchannel_send_action(wpa_s, pkex->freq, broadcast,
2650 wpa_s->own_addr, broadcast,
500ed7f0
JM
2651 wpabuf_head(msg), wpabuf_len(msg),
2652 wait_time, wpas_dpp_tx_pkex_status, 0);
00d2d13d
JM
2653 if (wait_time == 0)
2654 wait_time = 2000;
2655 pkex->exch_req_wait_time = wait_time;
2656 pkex->exch_req_tries = 1;
500ed7f0
JM
2657 }
2658
2659 /* TODO: Support multiple PKEX info entries */
2660
2661 os_free(wpa_s->dpp_pkex_auth_cmd);
2662 wpa_s->dpp_pkex_auth_cmd = os_strdup(cmd);
2663
2664 return 1;
2665}
2666
2667
2668int wpas_dpp_pkex_remove(struct wpa_supplicant *wpa_s, const char *id)
2669{
2670 unsigned int id_val;
2671
2672 if (os_strcmp(id, "*") == 0) {
2673 id_val = 0;
2674 } else {
2675 id_val = atoi(id);
2676 if (id_val == 0)
2677 return -1;
2678 }
2679
2680 if ((id_val != 0 && id_val != 1) || !wpa_s->dpp_pkex_code)
2681 return -1;
2682
2683 /* TODO: Support multiple PKEX entries */
2684 os_free(wpa_s->dpp_pkex_code);
2685 wpa_s->dpp_pkex_code = NULL;
2686 os_free(wpa_s->dpp_pkex_identifier);
2687 wpa_s->dpp_pkex_identifier = NULL;
2688 os_free(wpa_s->dpp_pkex_auth_cmd);
2689 wpa_s->dpp_pkex_auth_cmd = NULL;
2690 wpa_s->dpp_pkex_bi = NULL;
2691 /* TODO: Remove dpp_pkex only if it is for the identified PKEX code */
2692 dpp_pkex_free(wpa_s->dpp_pkex);
2693 wpa_s->dpp_pkex = NULL;
2694 return 0;
2695}
2696
2697
c1d37739
JM
2698void wpas_dpp_stop(struct wpa_supplicant *wpa_s)
2699{
c304bddc
JM
2700 if (wpa_s->dpp_auth || wpa_s->dpp_pkex)
2701 offchannel_send_action_done(wpa_s);
c1d37739
JM
2702 dpp_auth_deinit(wpa_s->dpp_auth);
2703 wpa_s->dpp_auth = NULL;
ed62d401
JM
2704 dpp_pkex_free(wpa_s->dpp_pkex);
2705 wpa_s->dpp_pkex = NULL;
1866dfb5
JM
2706 if (wpa_s->dpp_gas_client && wpa_s->dpp_gas_dialog_token >= 0)
2707 gas_query_stop(wpa_s->gas, wpa_s->dpp_gas_dialog_token);
c1d37739
JM
2708}
2709
2710
be27e185
JM
2711int wpas_dpp_init(struct wpa_supplicant *wpa_s)
2712{
2ed2b52f 2713 struct dpp_global_config config;
461d39af
JM
2714 u8 adv_proto_id[7];
2715
2716 adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
2717 adv_proto_id[1] = 5;
2718 WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
2719 adv_proto_id[5] = DPP_OUI_TYPE;
2720 adv_proto_id[6] = 0x01;
2721
2722 if (gas_server_register(wpa_s->gas_server, adv_proto_id,
2723 sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
2724 wpas_dpp_gas_status_handler, wpa_s) < 0)
2725 return -1;
2ed2b52f
JM
2726
2727 os_memset(&config, 0, sizeof(config));
2728 config.msg_ctx = wpa_s;
2a5a0680
JM
2729 config.cb_ctx = wpa_s;
2730#ifdef CONFIG_DPP2
2731 config.process_conf_obj = wpas_dpp_process_conf_obj;
562f7714 2732 config.remove_bi = wpas_dpp_remove_bi;
2a5a0680 2733#endif /* CONFIG_DPP2 */
2ed2b52f 2734 wpa_s->dpp = dpp_global_init(&config);
87d8435c 2735 return wpa_s->dpp ? 0 : -1;
be27e185
JM
2736}
2737
2738
2739void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
2740{
461d39af
JM
2741#ifdef CONFIG_TESTING_OPTIONS
2742 os_free(wpa_s->dpp_config_obj_override);
2743 wpa_s->dpp_config_obj_override = NULL;
2744 os_free(wpa_s->dpp_discovery_override);
2745 wpa_s->dpp_discovery_override = NULL;
2746 os_free(wpa_s->dpp_groups_override);
2747 wpa_s->dpp_groups_override = NULL;
461d39af
JM
2748 wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
2749#endif /* CONFIG_TESTING_OPTIONS */
87d8435c 2750 if (!wpa_s->dpp)
be27e185 2751 return;
87d8435c 2752 dpp_global_clear(wpa_s->dpp);
00d2d13d 2753 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
30d27b04 2754 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
f97ace34 2755 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
95b0104a 2756 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
22f90b32
JM
2757#ifdef CONFIG_DPP2
2758 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout, wpa_s, NULL);
b10e01a7
JM
2759 eloop_cancel_timeout(wpas_dpp_conn_status_result_wait_timeout,
2760 wpa_s, NULL);
16ef233b 2761 eloop_cancel_timeout(wpas_dpp_conn_status_result_timeout, wpa_s, NULL);
10ec6a5f
JM
2762 dpp_pfs_free(wpa_s->dpp_pfs);
2763 wpa_s->dpp_pfs = NULL;
562f7714 2764 wpas_dpp_chirp_stop(wpa_s);
22f90b32 2765#endif /* CONFIG_DPP2 */
30d27b04
JM
2766 offchannel_send_action_done(wpa_s);
2767 wpas_dpp_listen_stop(wpa_s);
fa5c9074 2768 wpas_dpp_stop(wpa_s);
500ed7f0 2769 wpas_dpp_pkex_remove(wpa_s, "*");
a0d5c56f 2770 os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
b65b22d6
JM
2771 os_free(wpa_s->dpp_configurator_params);
2772 wpa_s->dpp_configurator_params = NULL;
be27e185 2773}
c02dd10d
JM
2774
2775
2776#ifdef CONFIG_DPP2
562f7714 2777
c02dd10d
JM
2778int wpas_dpp_controller_start(struct wpa_supplicant *wpa_s, const char *cmd)
2779{
2780 struct dpp_controller_config config;
2781 const char *pos;
2782
2783 os_memset(&config, 0, sizeof(config));
2784 if (cmd) {
2785 pos = os_strstr(cmd, " tcp_port=");
2786 if (pos) {
2787 pos += 10;
2788 config.tcp_port = atoi(pos);
2789 }
2790 }
2791 config.configurator_params = wpa_s->dpp_configurator_params;
2792 return dpp_controller_start(wpa_s->dpp, &config);
2793}
562f7714
JM
2794
2795
2796static void wpas_dpp_chirp_next(void *eloop_ctx, void *timeout_ctx);
2797
2798static void wpas_dpp_chirp_timeout(void *eloop_ctx, void *timeout_ctx)
2799{
2800 struct wpa_supplicant *wpa_s = eloop_ctx;
2801
2802 wpa_printf(MSG_DEBUG, "DPP: No chirp response received");
2803 offchannel_send_action_done(wpa_s);
2804 wpas_dpp_chirp_next(wpa_s, NULL);
2805}
2806
2807
2808static void wpas_dpp_chirp_tx_status(struct wpa_supplicant *wpa_s,
2809 unsigned int freq, const u8 *dst,
2810 const u8 *src, const u8 *bssid,
2811 const u8 *data, size_t data_len,
2812 enum offchannel_send_action_result result)
2813{
2814 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
2815 wpa_printf(MSG_DEBUG, "DPP: Failed to send chirp on %d MHz",
2816 wpa_s->dpp_chirp_freq);
2817 if (eloop_register_timeout(0, 0, wpas_dpp_chirp_next,
2818 wpa_s, NULL) < 0)
2819 wpas_dpp_chirp_stop(wpa_s);
2820 return;
2821 }
2822
2823 wpa_printf(MSG_DEBUG, "DPP: Chirp send completed - wait for response");
2824 if (eloop_register_timeout(2, 0, wpas_dpp_chirp_timeout,
2825 wpa_s, NULL) < 0)
2826 wpas_dpp_chirp_stop(wpa_s);
2827}
2828
2829
2830static void wpas_dpp_chirp_start(struct wpa_supplicant *wpa_s)
2831{
2832 wpa_printf(MSG_DEBUG, "DPP: Chirp on %d MHz", wpa_s->dpp_chirp_freq);
2833 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2834 MAC2STR(broadcast), wpa_s->dpp_chirp_freq,
2835 DPP_PA_PRESENCE_ANNOUNCEMENT);
2836 if (offchannel_send_action(
2837 wpa_s, wpa_s->dpp_chirp_freq, broadcast,
2838 wpa_s->own_addr, broadcast,
2839 wpabuf_head(wpa_s->dpp_presence_announcement),
2840 wpabuf_len(wpa_s->dpp_presence_announcement),
2841 2000, wpas_dpp_chirp_tx_status, 0) < 0)
2842 wpas_dpp_chirp_stop(wpa_s);
2843}
2844
2845
2846static void wpas_dpp_chirp_scan_res_handler(struct wpa_supplicant *wpa_s,
2847 struct wpa_scan_results *scan_res)
2848{
2849 struct dpp_bootstrap_info *bi = wpa_s->dpp_chirp_bi;
2850 unsigned int i;
2851 struct hostapd_hw_modes *mode;
2852 int c;
2853 struct wpa_bss *bss;
2854
2855 if (!bi)
2856 return;
2857
2858 wpa_s->dpp_chirp_scan_done = 1;
2859
2860 os_free(wpa_s->dpp_chirp_freqs);
2861 wpa_s->dpp_chirp_freqs = NULL;
2862
2863 /* Channels from own bootstrapping info */
2864 for (i = 0; i < bi->num_freq; i++)
2865 int_array_add_unique(&wpa_s->dpp_chirp_freqs, bi->freq[i]);
2866
2867 /* Preferred chirping channels */
2868 int_array_add_unique(&wpa_s->dpp_chirp_freqs, 2437);
2869
2870 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
2871 HOSTAPD_MODE_IEEE80211A, 0);
2872 if (mode) {
2873 int chan44 = 0, chan149 = 0;
2874
2875 for (c = 0; c < mode->num_channels; c++) {
2876 struct hostapd_channel_data *chan = &mode->channels[c];
2877
2878 if (chan->flag & (HOSTAPD_CHAN_DISABLED |
2879 HOSTAPD_CHAN_RADAR))
2880 continue;
2881 if (chan->freq == 5220)
2882 chan44 = 1;
2883 if (chan->freq == 5745)
2884 chan149 = 1;
2885 }
2886 if (chan149)
2887 int_array_add_unique(&wpa_s->dpp_chirp_freqs, 5745);
2888 else if (chan44)
2889 int_array_add_unique(&wpa_s->dpp_chirp_freqs, 5220);
2890 }
2891
2892 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
2893 HOSTAPD_MODE_IEEE80211AD, 0);
2894 if (mode) {
2895 for (c = 0; c < mode->num_channels; c++) {
2896 struct hostapd_channel_data *chan = &mode->channels[c];
2897
2898 if ((chan->flag & (HOSTAPD_CHAN_DISABLED |
2899 HOSTAPD_CHAN_RADAR)) ||
2900 chan->freq != 60480)
2901 continue;
2902 int_array_add_unique(&wpa_s->dpp_chirp_freqs, 60480);
2903 break;
2904 }
2905 }
2906
2907 /* Add channels from scan results for APs that advertise Configurator
2908 * Connectivity element */
2909 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2910 if (wpa_bss_get_vendor_ie(bss, DPP_CC_IE_VENDOR_TYPE))
2911 int_array_add_unique(&wpa_s->dpp_chirp_freqs,
2912 bss->freq);
2913 }
2914
2915 if (!wpa_s->dpp_chirp_freqs ||
2916 eloop_register_timeout(0, 0, wpas_dpp_chirp_next, wpa_s, NULL) < 0)
2917 wpas_dpp_chirp_stop(wpa_s);
2918}
2919
2920
2921static void wpas_dpp_chirp_next(void *eloop_ctx, void *timeout_ctx)
2922{
2923 struct wpa_supplicant *wpa_s = eloop_ctx;
2924 int i;
2925
2926 if (wpa_s->dpp_chirp_listen)
2927 wpas_dpp_listen_stop(wpa_s);
2928
2929 if (wpa_s->dpp_chirp_freq == 0) {
2930 if (wpa_s->dpp_chirp_round % 4 == 0 &&
2931 !wpa_s->dpp_chirp_scan_done) {
2932 wpa_printf(MSG_DEBUG,
2933 "DPP: Update channel list for chirping");
2934 wpa_s->scan_req = MANUAL_SCAN_REQ;
2935 wpa_s->scan_res_handler =
2936 wpas_dpp_chirp_scan_res_handler;
2937 wpa_supplicant_req_scan(wpa_s, 0, 0);
2938 return;
2939 }
2940 wpa_s->dpp_chirp_freq = wpa_s->dpp_chirp_freqs[0];
2941 wpa_s->dpp_chirp_round++;
2942 wpa_printf(MSG_DEBUG, "DPP: Start chirping round %d",
2943 wpa_s->dpp_chirp_round);
2944 } else {
2945 for (i = 0; wpa_s->dpp_chirp_freqs[i]; i++)
2946 if (wpa_s->dpp_chirp_freqs[i] == wpa_s->dpp_chirp_freq)
2947 break;
2948 if (!wpa_s->dpp_chirp_freqs[i]) {
2949 wpa_printf(MSG_DEBUG,
2950 "DPP: Previous chirp freq %d not found",
2951 wpa_s->dpp_chirp_freq);
2952 return;
2953 }
2954 i++;
2955 if (wpa_s->dpp_chirp_freqs[i]) {
2956 wpa_s->dpp_chirp_freq = wpa_s->dpp_chirp_freqs[i];
2957 } else {
2958 wpa_s->dpp_chirp_iter--;
2959 if (wpa_s->dpp_chirp_iter <= 0) {
2960 wpa_printf(MSG_DEBUG,
2961 "DPP: Chirping iterations completed");
2962 wpas_dpp_chirp_stop(wpa_s);
2963 return;
2964 }
2965 wpa_s->dpp_chirp_freq = 0;
2966 wpa_s->dpp_chirp_scan_done = 0;
2967 if (eloop_register_timeout(30, 0, wpas_dpp_chirp_next,
2968 wpa_s, NULL) < 0) {
2969 wpas_dpp_chirp_stop(wpa_s);
2970 return;
2971 }
2972 if (wpa_s->dpp_chirp_listen) {
2973 wpa_printf(MSG_DEBUG,
2974 "DPP: Listen on %d MHz during chirp 30 second wait",
2975 wpa_s->dpp_chirp_listen);
2976 wpas_dpp_listen_start(wpa_s,
2977 wpa_s->dpp_chirp_listen);
2978 } else {
2979 wpa_printf(MSG_DEBUG,
2980 "DPP: Wait 30 seconds before starting the next chirping round");
2981 }
2982 return;
2983 }
2984 }
2985
2986 wpas_dpp_chirp_start(wpa_s);
2987}
2988
2989
2990int wpas_dpp_chirp(struct wpa_supplicant *wpa_s, const char *cmd)
2991{
2992 const char *pos;
2993 int iter = 1, listen_freq = 0;
2994 struct dpp_bootstrap_info *bi;
2995
2996 pos = os_strstr(cmd, " own=");
2997 if (!pos)
2998 return -1;
2999 pos += 5;
3000 bi = dpp_bootstrap_get_id(wpa_s->dpp, atoi(pos));
3001 if (!bi) {
3002 wpa_printf(MSG_DEBUG,
3003 "DPP: Identified bootstrap info not found");
3004 return -1;
3005 }
3006
3007 pos = os_strstr(cmd, " iter=");
3008 if (pos) {
3009 iter = atoi(pos + 6);
3010 if (iter <= 0)
3011 return -1;
3012 }
3013
3014 pos = os_strstr(cmd, " listen=");
3015 if (pos) {
3016 listen_freq = atoi(pos + 8);
3017 if (iter <= 0)
3018 return -1;
3019 }
3020
3021 wpas_dpp_chirp_stop(wpa_s);
3022 wpa_s->dpp_allowed_roles = DPP_CAPAB_ENROLLEE;
3023 wpa_s->dpp_chirp_bi = bi;
3024 wpa_s->dpp_presence_announcement = dpp_build_presence_announcement(bi);
3025 if (!wpa_s->dpp_presence_announcement)
3026 return -1;
3027 wpa_s->dpp_chirp_iter = iter;
3028 wpa_s->dpp_chirp_round = 0;
3029 wpa_s->dpp_chirp_scan_done = 0;
3030 wpa_s->dpp_chirp_listen = listen_freq;
3031
3032 return eloop_register_timeout(0, 0, wpas_dpp_chirp_next, wpa_s, NULL);
3033}
3034
3035
3036void wpas_dpp_chirp_stop(struct wpa_supplicant *wpa_s)
3037{
3038 if (wpa_s->dpp_presence_announcement) {
3039 offchannel_send_action_done(wpa_s);
3040 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CHIRP_STOPPED);
3041 }
3042 wpa_s->dpp_chirp_bi = NULL;
3043 wpabuf_free(wpa_s->dpp_presence_announcement);
3044 wpa_s->dpp_presence_announcement = NULL;
3045 if (wpa_s->dpp_chirp_listen)
3046 wpas_dpp_listen_stop(wpa_s);
3047 wpa_s->dpp_chirp_listen = 0;
3048 wpa_s->dpp_chirp_freq = 0;
3049 os_free(wpa_s->dpp_chirp_freqs);
3050 wpa_s->dpp_chirp_freqs = NULL;
3051 eloop_cancel_timeout(wpas_dpp_chirp_next, wpa_s, NULL);
3052 eloop_cancel_timeout(wpas_dpp_chirp_timeout, wpa_s, NULL);
3053 if (wpa_s->scan_res_handler == wpas_dpp_chirp_scan_res_handler) {
3054 wpas_abort_ongoing_scan(wpa_s);
3055 wpa_s->scan_res_handler = NULL;
3056 }
3057}
3058
c02dd10d 3059#endif /* CONFIG_DPP2 */