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