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