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