xref: /freebsd/contrib/wpa/wpa_supplicant/hs20_supplicant.c (revision 87b759f0fa1f7554d50ce640c40138512bbded44)
1 /*
2  * Copyright (c) 2009, Atheros Communications, Inc.
3  * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 #include <sys/stat.h>
11 
12 #include "common.h"
13 #include "eloop.h"
14 #include "common/ieee802_11_common.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/gas.h"
17 #include "common/wpa_ctrl.h"
18 #include "rsn_supp/wpa.h"
19 #include "wpa_supplicant_i.h"
20 #include "driver_i.h"
21 #include "config.h"
22 #include "scan.h"
23 #include "bss.h"
24 #include "bssid_ignore.h"
25 #include "gas_query.h"
26 #include "interworking.h"
27 #include "hs20_supplicant.h"
28 #include "base64.h"
29 #include "notify.h"
30 
31 
32 #define OSU_MAX_ITEMS 10
33 
34 struct osu_lang_string {
35 	char lang[4];
36 	char text[253];
37 };
38 
39 struct osu_icon {
40 	u16 width;
41 	u16 height;
42 	char lang[4];
43 	char icon_type[256];
44 	char filename[256];
45 	unsigned int id;
46 	unsigned int failed:1;
47 };
48 
49 struct osu_provider {
50 	u8 bssid[ETH_ALEN];
51 	u8 osu_ssid[SSID_MAX_LEN];
52 	u8 osu_ssid_len;
53 	u8 osu_ssid2[SSID_MAX_LEN];
54 	u8 osu_ssid2_len;
55 	char server_uri[256];
56 	u32 osu_methods; /* bit 0 = OMA-DM, bit 1 = SOAP-XML SPP */
57 	char osu_nai[256];
58 	char osu_nai2[256];
59 	struct osu_lang_string friendly_name[OSU_MAX_ITEMS];
60 	size_t friendly_name_count;
61 	struct osu_lang_string serv_desc[OSU_MAX_ITEMS];
62 	size_t serv_desc_count;
63 	struct osu_icon icon[OSU_MAX_ITEMS];
64 	size_t icon_count;
65 };
66 
67 
68 void hs20_configure_frame_filters(struct wpa_supplicant *wpa_s)
69 {
70 	struct wpa_bss *bss = wpa_s->current_bss;
71 	const u8 *ie;
72 	const u8 *ext_capa;
73 	u32 filter = 0;
74 
75 	if (!bss || !is_hs20_network(wpa_s, wpa_s->current_ssid, bss)) {
76 		/* Not configuring frame filtering - BSS is not a Hotspot 2.0
77 		 * network */
78 		return;
79 	}
80 
81 	ie = wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE);
82 
83 	/* Check if DGAF disabled bit is zero (5th byte in the IE) */
84 	if (!ie || ie[1] < 5)
85 		wpa_printf(MSG_DEBUG,
86 			   "Not configuring frame filtering - Can't extract DGAF bit");
87 	else if (!(ie[6] & HS20_DGAF_DISABLED))
88 		filter |= WPA_DATA_FRAME_FILTER_FLAG_GTK;
89 
90 	ext_capa = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
91 	if (!ext_capa || ext_capa[1] < 2) {
92 		wpa_printf(MSG_DEBUG,
93 			   "Not configuring frame filtering - Can't extract Proxy ARP bit");
94 		return;
95 	}
96 
97 	if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_PROXY_ARP))
98 		filter |= WPA_DATA_FRAME_FILTER_FLAG_ARP |
99 			WPA_DATA_FRAME_FILTER_FLAG_NA;
100 
101 	wpa_drv_configure_frame_filters(wpa_s, filter);
102 }
103 
104 
105 void wpas_hs20_add_indication(struct wpabuf *buf, int pps_mo_id, int ap_release)
106 {
107 	int release;
108 	u8 conf;
109 
110 	release = (HS20_VERSION >> 4) + 1;
111 	if (ap_release > 0 && release > ap_release)
112 		release = ap_release;
113 	if (release < 2)
114 		pps_mo_id = -1;
115 
116 	wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
117 	wpabuf_put_u8(buf, pps_mo_id >= 0 ? 7 : 5);
118 	wpabuf_put_be24(buf, OUI_WFA);
119 	wpabuf_put_u8(buf, HS20_INDICATION_OUI_TYPE);
120 	conf = (release - 1) << 4;
121 	if (pps_mo_id >= 0)
122 		conf |= HS20_PPS_MO_ID_PRESENT;
123 	wpabuf_put_u8(buf, conf);
124 	if (pps_mo_id >= 0)
125 		wpabuf_put_le16(buf, pps_mo_id);
126 }
127 
128 
129 void wpas_hs20_add_roam_cons_sel(struct wpabuf *buf,
130 				 const struct wpa_ssid *ssid)
131 {
132 	if (!ssid->roaming_consortium_selection ||
133 	    !ssid->roaming_consortium_selection_len)
134 		return;
135 
136 	wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
137 	wpabuf_put_u8(buf, 4 + ssid->roaming_consortium_selection_len);
138 	wpabuf_put_be24(buf, OUI_WFA);
139 	wpabuf_put_u8(buf, HS20_ROAMING_CONS_SEL_OUI_TYPE);
140 	wpabuf_put_data(buf, ssid->roaming_consortium_selection,
141 			ssid->roaming_consortium_selection_len);
142 }
143 
144 
145 int get_hs20_version(struct wpa_bss *bss)
146 {
147 	const u8 *ie;
148 
149 	if (!bss)
150 		return 0;
151 
152 	ie = wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE);
153 	if (!ie || ie[1] < 5)
154 		return 0;
155 
156 	return ((ie[6] >> 4) & 0x0f) + 1;
157 }
158 
159 
160 int is_hs20_network(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
161 		    struct wpa_bss *bss)
162 {
163 	if (!wpa_s->conf->hs20 || !ssid)
164 		return 0;
165 
166 	if (ssid->parent_cred)
167 		return 1;
168 
169 	if (bss && !wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE))
170 		return 0;
171 
172 	/*
173 	 * This may catch some non-Hotspot 2.0 cases, but it is safer to do that
174 	 * than cause Hotspot 2.0 connections without indication element getting
175 	 * added. Non-Hotspot 2.0 APs should ignore the unknown vendor element.
176 	 */
177 
178 	if (!(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X))
179 		return 0;
180 	if (!(ssid->pairwise_cipher & WPA_CIPHER_CCMP))
181 		return 0;
182 	if (ssid->proto != WPA_PROTO_RSN)
183 		return 0;
184 
185 	return 1;
186 }
187 
188 
189 int hs20_get_pps_mo_id(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
190 {
191 	struct wpa_cred *cred;
192 
193 	if (ssid == NULL)
194 		return 0;
195 
196 	if (ssid->update_identifier)
197 		return ssid->update_identifier;
198 
199 	if (ssid->parent_cred == NULL)
200 		return 0;
201 
202 	for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
203 		if (ssid->parent_cred == cred)
204 			return cred->update_identifier;
205 	}
206 
207 	return 0;
208 }
209 
210 
211 void hs20_put_anqp_req(u32 stypes, const u8 *payload, size_t payload_len,
212 		       struct wpabuf *buf)
213 {
214 	u8 *len_pos;
215 
216 	if (buf == NULL)
217 		return;
218 
219 	len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
220 	wpabuf_put_be24(buf, OUI_WFA);
221 	wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
222 	if (stypes == BIT(HS20_STYPE_NAI_HOME_REALM_QUERY)) {
223 		wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
224 		wpabuf_put_u8(buf, 0); /* Reserved */
225 		if (payload)
226 			wpabuf_put_data(buf, payload, payload_len);
227 	} else if (stypes == BIT(HS20_STYPE_ICON_REQUEST)) {
228 		wpabuf_put_u8(buf, HS20_STYPE_ICON_REQUEST);
229 		wpabuf_put_u8(buf, 0); /* Reserved */
230 		if (payload)
231 			wpabuf_put_data(buf, payload, payload_len);
232 	} else {
233 		u8 i;
234 		wpabuf_put_u8(buf, HS20_STYPE_QUERY_LIST);
235 		wpabuf_put_u8(buf, 0); /* Reserved */
236 		for (i = 0; i < 32; i++) {
237 			if (stypes & BIT(i))
238 				wpabuf_put_u8(buf, i);
239 		}
240 	}
241 	gas_anqp_set_element_len(buf, len_pos);
242 
243 	gas_anqp_set_len(buf);
244 }
245 
246 
247 static struct wpabuf * hs20_build_anqp_req(u32 stypes, const u8 *payload,
248 					   size_t payload_len)
249 {
250 	struct wpabuf *buf;
251 
252 	buf = gas_anqp_build_initial_req(0, 100 + payload_len);
253 	if (buf == NULL)
254 		return NULL;
255 
256 	hs20_put_anqp_req(stypes, payload, payload_len, buf);
257 
258 	return buf;
259 }
260 
261 
262 int hs20_anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, u32 stypes,
263 		       const u8 *payload, size_t payload_len, int inmem)
264 {
265 	struct wpabuf *buf;
266 	int ret = 0;
267 	int freq;
268 	struct wpa_bss *bss;
269 	int res;
270 	struct icon_entry *icon_entry;
271 
272 	bss = wpa_bss_get_bssid(wpa_s, dst);
273 	if (!bss) {
274 		wpa_printf(MSG_WARNING,
275 			   "ANQP: Cannot send query to unknown BSS "
276 			   MACSTR, MAC2STR(dst));
277 		return -1;
278 	}
279 
280 	wpa_bss_anqp_unshare_alloc(bss);
281 	freq = bss->freq;
282 
283 	wpa_printf(MSG_DEBUG, "HS20: ANQP Query Request to " MACSTR " for "
284 		   "subtypes 0x%x", MAC2STR(dst), stypes);
285 
286 	buf = hs20_build_anqp_req(stypes, payload, payload_len);
287 	if (buf == NULL)
288 		return -1;
289 
290 	res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, anqp_resp_cb,
291 			    wpa_s);
292 	if (res < 0) {
293 		wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
294 		wpabuf_free(buf);
295 		return -1;
296 	} else
297 		wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
298 			   "%u", res);
299 
300 	if (inmem) {
301 		icon_entry = os_zalloc(sizeof(struct icon_entry));
302 		if (!icon_entry)
303 			return -1;
304 		os_memcpy(icon_entry->bssid, dst, ETH_ALEN);
305 		icon_entry->file_name = os_malloc(payload_len + 1);
306 		if (!icon_entry->file_name) {
307 			os_free(icon_entry);
308 			return -1;
309 		}
310 		os_memcpy(icon_entry->file_name, payload, payload_len);
311 		icon_entry->file_name[payload_len] = '\0';
312 		icon_entry->dialog_token = res;
313 
314 		dl_list_add(&wpa_s->icon_head, &icon_entry->list);
315 	}
316 
317 	return ret;
318 }
319 
320 
321 static struct icon_entry * hs20_find_icon(struct wpa_supplicant *wpa_s,
322 					  const u8 *bssid,
323 					  const char *file_name)
324 {
325 	struct icon_entry *icon;
326 
327 	dl_list_for_each(icon, &wpa_s->icon_head, struct icon_entry, list) {
328 		if (ether_addr_equal(icon->bssid, bssid) &&
329 		    os_strcmp(icon->file_name, file_name) == 0 && icon->image)
330 			return icon;
331 	}
332 
333 	return NULL;
334 }
335 
336 
337 int hs20_get_icon(struct wpa_supplicant *wpa_s, const u8 *bssid,
338 		  const char *file_name, size_t offset, size_t size,
339 		  char *reply, size_t buf_len)
340 {
341 	struct icon_entry *icon;
342 	size_t out_size;
343 	char *b64;
344 	size_t b64_size;
345 	int reply_size;
346 
347 	wpa_printf(MSG_DEBUG, "HS20: Get icon " MACSTR " %s @ %u +%u (%u)",
348 		   MAC2STR(bssid), file_name, (unsigned int) offset,
349 		   (unsigned int) size, (unsigned int) buf_len);
350 
351 	icon = hs20_find_icon(wpa_s, bssid, file_name);
352 	if (!icon || !icon->image || offset >= icon->image_len)
353 		return -1;
354 	if (size > icon->image_len - offset)
355 		size = icon->image_len - offset;
356 	out_size = buf_len - 3 /* max base64 padding */;
357 	if (size * 4 > out_size * 3)
358 		size = out_size * 3 / 4;
359 	if (size == 0)
360 		return -1;
361 
362 	b64 = base64_encode(&icon->image[offset], size, &b64_size);
363 	if (b64 && buf_len >= b64_size) {
364 		os_memcpy(reply, b64, b64_size);
365 		reply_size = b64_size;
366 	} else {
367 		reply_size = -1;
368 	}
369 	os_free(b64);
370 	return reply_size;
371 }
372 
373 
374 static void hs20_free_icon_entry(struct icon_entry *icon)
375 {
376 	wpa_printf(MSG_DEBUG, "HS20: Free stored icon from " MACSTR
377 		   " dialog_token=%u file_name=%s image_len=%u",
378 		   MAC2STR(icon->bssid), icon->dialog_token,
379 		   icon->file_name ? icon->file_name : "N/A",
380 		   (unsigned int) icon->image_len);
381 	os_free(icon->file_name);
382 	os_free(icon->image);
383 	os_free(icon);
384 }
385 
386 
387 int hs20_del_icon(struct wpa_supplicant *wpa_s, const u8 *bssid,
388 		  const char *file_name)
389 {
390 	struct icon_entry *icon, *tmp;
391 	int count = 0;
392 
393 	if (!bssid)
394 		wpa_printf(MSG_DEBUG, "HS20: Delete all stored icons");
395 	else if (!file_name)
396 		wpa_printf(MSG_DEBUG, "HS20: Delete all stored icons for "
397 			   MACSTR, MAC2STR(bssid));
398 	else
399 		wpa_printf(MSG_DEBUG, "HS20: Delete stored icons for "
400 			   MACSTR " file name %s", MAC2STR(bssid), file_name);
401 
402 	dl_list_for_each_safe(icon, tmp, &wpa_s->icon_head, struct icon_entry,
403 			      list) {
404 		if ((!bssid || ether_addr_equal(icon->bssid, bssid)) &&
405 		    (!file_name ||
406 		     os_strcmp(icon->file_name, file_name) == 0)) {
407 			dl_list_del(&icon->list);
408 			hs20_free_icon_entry(icon);
409 			count++;
410 		}
411 	}
412 	return count == 0 ? -1 : 0;
413 }
414 
415 
416 static void hs20_set_osu_access_permission(const char *osu_dir,
417 					   const char *fname)
418 {
419 	struct stat statbuf;
420 
421 	/* Get OSU directory information */
422 	if (stat(osu_dir, &statbuf) < 0) {
423 		wpa_printf(MSG_WARNING, "Cannot stat the OSU directory %s",
424 			   osu_dir);
425 		return;
426 	}
427 
428 	if (chmod(fname, statbuf.st_mode) < 0) {
429 		wpa_printf(MSG_WARNING,
430 			   "Cannot change the permissions for %s", fname);
431 		return;
432 	}
433 
434 	if (lchown(fname, statbuf.st_uid, statbuf.st_gid) < 0) {
435 		wpa_printf(MSG_WARNING, "Cannot change the ownership for %s",
436 			   fname);
437 	}
438 }
439 
440 
441 static void hs20_remove_duplicate_icons(struct wpa_supplicant *wpa_s,
442 					struct icon_entry *new_icon)
443 {
444 	struct icon_entry *icon, *tmp;
445 
446 	dl_list_for_each_safe(icon, tmp, &wpa_s->icon_head, struct icon_entry,
447 			      list) {
448 		if (icon == new_icon)
449 			continue;
450 		if (ether_addr_equal(icon->bssid, new_icon->bssid) &&
451 		    os_strcmp(icon->file_name, new_icon->file_name) == 0) {
452 			dl_list_del(&icon->list);
453 			hs20_free_icon_entry(icon);
454 		}
455 	}
456 }
457 
458 
459 static int hs20_process_icon_binary_file(struct wpa_supplicant *wpa_s,
460 					 const u8 *sa, const u8 *pos,
461 					 size_t slen, u8 dialog_token)
462 {
463 	char fname[256];
464 	int png;
465 	FILE *f;
466 	u16 data_len;
467 	struct icon_entry *icon;
468 
469 	dl_list_for_each(icon, &wpa_s->icon_head, struct icon_entry, list) {
470 		if (icon->dialog_token == dialog_token && !icon->image &&
471 		    ether_addr_equal(icon->bssid, sa)) {
472 			icon->image = os_memdup(pos, slen);
473 			if (!icon->image)
474 				return -1;
475 			icon->image_len = slen;
476 			hs20_remove_duplicate_icons(wpa_s, icon);
477 			wpa_msg(wpa_s, MSG_INFO,
478 				RX_HS20_ICON MACSTR " %s %u",
479 				MAC2STR(sa), icon->file_name,
480 				(unsigned int) icon->image_len);
481 			return 0;
482 		}
483 	}
484 
485 	wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR " Icon Binary File",
486 		MAC2STR(sa));
487 
488 	if (slen < 4) {
489 		wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
490 			"value from " MACSTR, MAC2STR(sa));
491 		return -1;
492 	}
493 
494 	wpa_printf(MSG_DEBUG, "HS 2.0: Download Status Code %u", *pos);
495 	if (*pos != 0)
496 		return -1;
497 	pos++;
498 	slen--;
499 
500 	if ((size_t) 1 + pos[0] > slen) {
501 		wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
502 			"value from " MACSTR, MAC2STR(sa));
503 		return -1;
504 	}
505 	wpa_hexdump_ascii(MSG_DEBUG, "Icon Type", pos + 1, pos[0]);
506 	png = os_strncasecmp((char *) pos + 1, "image/png", 9) == 0;
507 	slen -= 1 + pos[0];
508 	pos += 1 + pos[0];
509 
510 	if (slen < 2) {
511 		wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
512 			"value from " MACSTR, MAC2STR(sa));
513 		return -1;
514 	}
515 	data_len = WPA_GET_LE16(pos);
516 	pos += 2;
517 	slen -= 2;
518 
519 	if (data_len > slen) {
520 		wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
521 			"value from " MACSTR, MAC2STR(sa));
522 		return -1;
523 	}
524 
525 	wpa_printf(MSG_DEBUG, "Icon Binary Data: %u bytes", data_len);
526 	if (wpa_s->conf->osu_dir == NULL)
527 		return -1;
528 
529 	wpa_s->osu_icon_id++;
530 	if (wpa_s->osu_icon_id == 0)
531 		wpa_s->osu_icon_id++;
532 	snprintf(fname, sizeof(fname), "%s/osu-icon-%u.%s",
533 		 wpa_s->conf->osu_dir, wpa_s->osu_icon_id,
534 		 png ? "png" : "icon");
535 	f = fopen(fname, "wb");
536 	if (f == NULL)
537 		return -1;
538 
539 	hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
540 
541 	if (fwrite(pos, slen, 1, f) != 1) {
542 		fclose(f);
543 		unlink(fname);
544 		return -1;
545 	}
546 	fclose(f);
547 
548 	wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP_ICON "%s", fname);
549 	return 0;
550 }
551 
552 
553 static void hs20_continue_icon_fetch(void *eloop_ctx, void *sock_ctx)
554 {
555 	struct wpa_supplicant *wpa_s = eloop_ctx;
556 	if (wpa_s->fetch_osu_icon_in_progress)
557 		hs20_next_osu_icon(wpa_s);
558 }
559 
560 
561 static void hs20_osu_icon_fetch_result(struct wpa_supplicant *wpa_s, int res)
562 {
563 	size_t i, j;
564 	struct os_reltime now, tmp;
565 	int dur;
566 
567 	os_get_reltime(&now);
568 	os_reltime_sub(&now, &wpa_s->osu_icon_fetch_start, &tmp);
569 	dur = tmp.sec * 1000 + tmp.usec / 1000;
570 	wpa_printf(MSG_DEBUG, "HS 2.0: Icon fetch dur=%d ms res=%d",
571 		   dur, res);
572 
573 	for (i = 0; i < wpa_s->osu_prov_count; i++) {
574 		struct osu_provider *osu = &wpa_s->osu_prov[i];
575 		for (j = 0; j < osu->icon_count; j++) {
576 			struct osu_icon *icon = &osu->icon[j];
577 			if (icon->id || icon->failed)
578 				continue;
579 			if (res < 0)
580 				icon->failed = 1;
581 			else
582 				icon->id = wpa_s->osu_icon_id;
583 			return;
584 		}
585 	}
586 }
587 
588 
589 void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
590 				  struct wpa_bss *bss, const u8 *sa,
591 				  const u8 *data, size_t slen, u8 dialog_token)
592 {
593 	const u8 *pos = data;
594 	u8 subtype;
595 	struct wpa_bss_anqp *anqp = NULL;
596 	int ret;
597 
598 	if (slen < 2)
599 		return;
600 
601 	if (bss)
602 		anqp = bss->anqp;
603 
604 	subtype = *pos++;
605 	slen--;
606 
607 	pos++; /* Reserved */
608 	slen--;
609 
610 	switch (subtype) {
611 	case HS20_STYPE_CAPABILITY_LIST:
612 		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
613 			" HS Capability List", MAC2STR(sa));
614 		wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
615 		if (anqp) {
616 			wpabuf_free(anqp->hs20_capability_list);
617 			anqp->hs20_capability_list =
618 				wpabuf_alloc_copy(pos, slen);
619 		}
620 		break;
621 	case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
622 		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
623 			" Operator Friendly Name", MAC2STR(sa));
624 		wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
625 		if (anqp) {
626 			wpabuf_free(anqp->hs20_operator_friendly_name);
627 			anqp->hs20_operator_friendly_name =
628 				wpabuf_alloc_copy(pos, slen);
629 		}
630 		break;
631 	case HS20_STYPE_WAN_METRICS:
632 		wpa_hexdump(MSG_DEBUG, "WAN Metrics", pos, slen);
633 		if (slen < 13) {
634 			wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short WAN "
635 				"Metrics value from " MACSTR, MAC2STR(sa));
636 			break;
637 		}
638 		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
639 			" WAN Metrics %02x:%u:%u:%u:%u:%u", MAC2STR(sa),
640 			pos[0], WPA_GET_LE32(pos + 1), WPA_GET_LE32(pos + 5),
641 			pos[9], pos[10], WPA_GET_LE16(pos + 11));
642 		if (anqp) {
643 			wpabuf_free(anqp->hs20_wan_metrics);
644 			anqp->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
645 		}
646 		break;
647 	case HS20_STYPE_CONNECTION_CAPABILITY:
648 		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
649 			" Connection Capability", MAC2STR(sa));
650 		wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
651 		if (anqp) {
652 			wpabuf_free(anqp->hs20_connection_capability);
653 			anqp->hs20_connection_capability =
654 				wpabuf_alloc_copy(pos, slen);
655 		}
656 		break;
657 	case HS20_STYPE_OPERATING_CLASS:
658 		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
659 			" Operating Class", MAC2STR(sa));
660 		wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
661 		if (anqp) {
662 			wpabuf_free(anqp->hs20_operating_class);
663 			anqp->hs20_operating_class =
664 				wpabuf_alloc_copy(pos, slen);
665 		}
666 		break;
667 	case HS20_STYPE_OSU_PROVIDERS_LIST:
668 		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
669 			" OSU Providers list", MAC2STR(sa));
670 		wpa_s->num_prov_found++;
671 		if (anqp) {
672 			wpabuf_free(anqp->hs20_osu_providers_list);
673 			anqp->hs20_osu_providers_list =
674 				wpabuf_alloc_copy(pos, slen);
675 		}
676 		break;
677 	case HS20_STYPE_ICON_BINARY_FILE:
678 		ret = hs20_process_icon_binary_file(wpa_s, sa, pos, slen,
679 						    dialog_token);
680 		if (wpa_s->fetch_osu_icon_in_progress) {
681 			hs20_osu_icon_fetch_result(wpa_s, ret);
682 			eloop_cancel_timeout(hs20_continue_icon_fetch,
683 					     wpa_s, NULL);
684 			eloop_register_timeout(0, 0, hs20_continue_icon_fetch,
685 					       wpa_s, NULL);
686 		}
687 		break;
688 	case HS20_STYPE_OPERATOR_ICON_METADATA:
689 		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
690 			" Operator Icon Metadata", MAC2STR(sa));
691 		wpa_hexdump(MSG_DEBUG, "Operator Icon Metadata", pos, slen);
692 		if (anqp) {
693 			wpabuf_free(anqp->hs20_operator_icon_metadata);
694 			anqp->hs20_operator_icon_metadata =
695 				wpabuf_alloc_copy(pos, slen);
696 		}
697 		break;
698 	case HS20_STYPE_OSU_PROVIDERS_NAI_LIST:
699 		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
700 			" OSU Providers NAI List", MAC2STR(sa));
701 		if (anqp) {
702 			wpabuf_free(anqp->hs20_osu_providers_nai_list);
703 			anqp->hs20_osu_providers_nai_list =
704 				wpabuf_alloc_copy(pos, slen);
705 		}
706 		break;
707 	default:
708 		wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
709 		break;
710 	}
711 }
712 
713 
714 void hs20_notify_parse_done(struct wpa_supplicant *wpa_s)
715 {
716 	if (!wpa_s->fetch_osu_icon_in_progress)
717 		return;
718 	if (eloop_is_timeout_registered(hs20_continue_icon_fetch, wpa_s, NULL))
719 		return;
720 	/*
721 	 * We are going through icon fetch, but no icon response was received.
722 	 * Assume this means the current AP could not provide an answer to avoid
723 	 * getting stuck in fetch iteration.
724 	 */
725 	hs20_icon_fetch_failed(wpa_s);
726 }
727 
728 
729 static void hs20_free_osu_prov_entry(struct osu_provider *prov)
730 {
731 }
732 
733 
734 void hs20_free_osu_prov(struct wpa_supplicant *wpa_s)
735 {
736 	size_t i;
737 	for (i = 0; i < wpa_s->osu_prov_count; i++)
738 		hs20_free_osu_prov_entry(&wpa_s->osu_prov[i]);
739 	os_free(wpa_s->osu_prov);
740 	wpa_s->osu_prov = NULL;
741 	wpa_s->osu_prov_count = 0;
742 }
743 
744 
745 static void hs20_osu_fetch_done(struct wpa_supplicant *wpa_s)
746 {
747 	char fname[256];
748 	FILE *f;
749 	size_t i, j;
750 
751 	wpa_s->fetch_osu_info = 0;
752 	wpa_s->fetch_osu_icon_in_progress = 0;
753 
754 	if (wpa_s->conf->osu_dir == NULL) {
755 		hs20_free_osu_prov(wpa_s);
756 		wpa_s->fetch_anqp_in_progress = 0;
757 		return;
758 	}
759 
760 	snprintf(fname, sizeof(fname), "%s/osu-providers.txt",
761 		 wpa_s->conf->osu_dir);
762 	f = fopen(fname, "w");
763 	if (f == NULL) {
764 		wpa_msg(wpa_s, MSG_INFO,
765 			"Could not write OSU provider information");
766 		hs20_free_osu_prov(wpa_s);
767 		wpa_s->fetch_anqp_in_progress = 0;
768 		return;
769 	}
770 
771 	hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
772 
773 	for (i = 0; i < wpa_s->osu_prov_count; i++) {
774 		struct osu_provider *osu = &wpa_s->osu_prov[i];
775 		if (i > 0)
776 			fprintf(f, "\n");
777 		fprintf(f, "OSU-PROVIDER " MACSTR "\n"
778 			"uri=%s\n"
779 			"methods=%08x\n",
780 			MAC2STR(osu->bssid), osu->server_uri, osu->osu_methods);
781 		if (osu->osu_ssid_len) {
782 			fprintf(f, "osu_ssid=%s\n",
783 				wpa_ssid_txt(osu->osu_ssid,
784 					     osu->osu_ssid_len));
785 		}
786 		if (osu->osu_ssid2_len) {
787 			fprintf(f, "osu_ssid2=%s\n",
788 				wpa_ssid_txt(osu->osu_ssid2,
789 					     osu->osu_ssid2_len));
790 		}
791 		if (osu->osu_nai[0])
792 			fprintf(f, "osu_nai=%s\n", osu->osu_nai);
793 		if (osu->osu_nai2[0])
794 			fprintf(f, "osu_nai2=%s\n", osu->osu_nai2);
795 		for (j = 0; j < osu->friendly_name_count; j++) {
796 			fprintf(f, "friendly_name=%s:%s\n",
797 				osu->friendly_name[j].lang,
798 				osu->friendly_name[j].text);
799 		}
800 		for (j = 0; j < osu->serv_desc_count; j++) {
801 			fprintf(f, "desc=%s:%s\n",
802 				osu->serv_desc[j].lang,
803 				osu->serv_desc[j].text);
804 		}
805 		for (j = 0; j < osu->icon_count; j++) {
806 			struct osu_icon *icon = &osu->icon[j];
807 			if (icon->failed)
808 				continue; /* could not fetch icon */
809 			fprintf(f, "icon=%u:%u:%u:%s:%s:%s\n",
810 				icon->id, icon->width, icon->height, icon->lang,
811 				icon->icon_type, icon->filename);
812 		}
813 	}
814 	fclose(f);
815 	hs20_free_osu_prov(wpa_s);
816 
817 	wpa_msg(wpa_s, MSG_INFO, "OSU provider fetch completed");
818 	wpa_s->fetch_anqp_in_progress = 0;
819 }
820 
821 
822 void hs20_next_osu_icon(struct wpa_supplicant *wpa_s)
823 {
824 	size_t i, j;
825 
826 	wpa_printf(MSG_DEBUG, "HS 2.0: Ready to fetch next icon");
827 
828 	for (i = 0; i < wpa_s->osu_prov_count; i++) {
829 		struct osu_provider *osu = &wpa_s->osu_prov[i];
830 		for (j = 0; j < osu->icon_count; j++) {
831 			struct osu_icon *icon = &osu->icon[j];
832 			if (icon->id || icon->failed)
833 				continue;
834 
835 			wpa_printf(MSG_DEBUG, "HS 2.0: Try to fetch icon '%s' "
836 				   "from " MACSTR, icon->filename,
837 				   MAC2STR(osu->bssid));
838 			os_get_reltime(&wpa_s->osu_icon_fetch_start);
839 			if (hs20_anqp_send_req(wpa_s, osu->bssid,
840 					       BIT(HS20_STYPE_ICON_REQUEST),
841 					       (u8 *) icon->filename,
842 					       os_strlen(icon->filename),
843 					       0) < 0) {
844 				icon->failed = 1;
845 				continue;
846 			}
847 			return;
848 		}
849 	}
850 
851 	wpa_printf(MSG_DEBUG, "HS 2.0: No more icons to fetch");
852 	hs20_osu_fetch_done(wpa_s);
853 }
854 
855 
856 static void hs20_osu_add_prov(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
857 			      const u8 *osu_ssid, u8 osu_ssid_len,
858 			      const u8 *osu_ssid2, u8 osu_ssid2_len,
859 			      const u8 *pos, size_t len)
860 {
861 	struct osu_provider *prov;
862 	const u8 *end = pos + len;
863 	u16 len2;
864 	const u8 *pos2;
865 	u8 uri_len, osu_method_len, osu_nai_len;
866 
867 	wpa_hexdump(MSG_DEBUG, "HS 2.0: Parsing OSU Provider", pos, len);
868 	prov = os_realloc_array(wpa_s->osu_prov,
869 				wpa_s->osu_prov_count + 1,
870 				sizeof(*prov));
871 	if (prov == NULL)
872 		return;
873 	wpa_s->osu_prov = prov;
874 	prov = &prov[wpa_s->osu_prov_count];
875 	os_memset(prov, 0, sizeof(*prov));
876 
877 	os_memcpy(prov->bssid, bss->bssid, ETH_ALEN);
878 	os_memcpy(prov->osu_ssid, osu_ssid, osu_ssid_len);
879 	prov->osu_ssid_len = osu_ssid_len;
880 	if (osu_ssid2)
881 		os_memcpy(prov->osu_ssid2, osu_ssid2, osu_ssid2_len);
882 	prov->osu_ssid2_len = osu_ssid2_len;
883 
884 	/* OSU Friendly Name Length */
885 	if (end - pos < 2) {
886 		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
887 			   "Friendly Name Length");
888 		return;
889 	}
890 	len2 = WPA_GET_LE16(pos);
891 	pos += 2;
892 	if (len2 > end - pos) {
893 		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
894 			   "Friendly Name Duples");
895 		return;
896 	}
897 	pos2 = pos;
898 	pos += len2;
899 
900 	/* OSU Friendly Name Duples */
901 	while (pos - pos2 >= 4 && prov->friendly_name_count < OSU_MAX_ITEMS) {
902 		struct osu_lang_string *f;
903 		u8 slen;
904 
905 		slen = pos2[0];
906 		if (1 + slen > pos - pos2) {
907 			wpa_printf(MSG_DEBUG, "Invalid OSU Friendly Name");
908 			break;
909 		}
910 		if (slen < 3) {
911 			wpa_printf(MSG_DEBUG,
912 				   "Invalid OSU Friendly Name (no room for language)");
913 			break;
914 		}
915 		f = &prov->friendly_name[prov->friendly_name_count++];
916 		pos2++;
917 		os_memcpy(f->lang, pos2, 3);
918 		pos2 += 3;
919 		slen -= 3;
920 		os_memcpy(f->text, pos2, slen);
921 		pos2 += slen;
922 	}
923 
924 	/* OSU Server URI */
925 	if (end - pos < 1) {
926 		wpa_printf(MSG_DEBUG,
927 			   "HS 2.0: Not enough room for OSU Server URI length");
928 		return;
929 	}
930 	uri_len = *pos++;
931 	if (uri_len > end - pos) {
932 		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Server "
933 			   "URI");
934 		return;
935 	}
936 	os_memcpy(prov->server_uri, pos, uri_len);
937 	pos += uri_len;
938 
939 	/* OSU Method list */
940 	if (end - pos < 1) {
941 		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
942 			   "list length");
943 		return;
944 	}
945 	osu_method_len = pos[0];
946 	if (osu_method_len > end - pos - 1) {
947 		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
948 			   "list");
949 		return;
950 	}
951 	pos2 = pos + 1;
952 	pos += 1 + osu_method_len;
953 	while (pos2 < pos) {
954 		if (*pos2 < 32)
955 			prov->osu_methods |= BIT(*pos2);
956 		pos2++;
957 	}
958 
959 	/* Icons Available Length */
960 	if (end - pos < 2) {
961 		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
962 			   "Available Length");
963 		return;
964 	}
965 	len2 = WPA_GET_LE16(pos);
966 	pos += 2;
967 	if (len2 > end - pos) {
968 		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
969 			   "Available");
970 		return;
971 	}
972 	pos2 = pos;
973 	pos += len2;
974 
975 	/* Icons Available */
976 	while (pos2 < pos) {
977 		struct osu_icon *icon = &prov->icon[prov->icon_count];
978 		u8 flen;
979 
980 		if (2 + 2 + 3 + 1 + 1 > pos - pos2) {
981 			wpa_printf(MSG_DEBUG, "HS 2.0: Invalid Icon Metadata");
982 			break;
983 		}
984 
985 		icon->width = WPA_GET_LE16(pos2);
986 		pos2 += 2;
987 		icon->height = WPA_GET_LE16(pos2);
988 		pos2 += 2;
989 		os_memcpy(icon->lang, pos2, 3);
990 		pos2 += 3;
991 
992 		flen = *pos2++;
993 		if (flen > pos - pos2) {
994 			wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon Type");
995 			break;
996 		}
997 		os_memcpy(icon->icon_type, pos2, flen);
998 		pos2 += flen;
999 
1000 		if (pos - pos2 < 1) {
1001 			wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
1002 				   "Filename length");
1003 			break;
1004 		}
1005 		flen = *pos2++;
1006 		if (flen > pos - pos2) {
1007 			wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
1008 				   "Filename");
1009 			break;
1010 		}
1011 		os_memcpy(icon->filename, pos2, flen);
1012 		pos2 += flen;
1013 
1014 		prov->icon_count++;
1015 	}
1016 
1017 	/* OSU_NAI */
1018 	if (end - pos < 1) {
1019 		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
1020 		return;
1021 	}
1022 	osu_nai_len = *pos++;
1023 	if (osu_nai_len > end - pos) {
1024 		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
1025 		return;
1026 	}
1027 	os_memcpy(prov->osu_nai, pos, osu_nai_len);
1028 	pos += osu_nai_len;
1029 
1030 	/* OSU Service Description Length */
1031 	if (end - pos < 2) {
1032 		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
1033 			   "Service Description Length");
1034 		return;
1035 	}
1036 	len2 = WPA_GET_LE16(pos);
1037 	pos += 2;
1038 	if (len2 > end - pos) {
1039 		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
1040 			   "Service Description Duples");
1041 		return;
1042 	}
1043 	pos2 = pos;
1044 	pos += len2;
1045 
1046 	/* OSU Service Description Duples */
1047 	while (pos - pos2 >= 4 && prov->serv_desc_count < OSU_MAX_ITEMS) {
1048 		struct osu_lang_string *f;
1049 		u8 descr_len;
1050 
1051 		descr_len = *pos2++;
1052 		if (descr_len > pos - pos2 || descr_len < 3) {
1053 			wpa_printf(MSG_DEBUG, "Invalid OSU Service "
1054 				   "Description");
1055 			break;
1056 		}
1057 		f = &prov->serv_desc[prov->serv_desc_count++];
1058 		os_memcpy(f->lang, pos2, 3);
1059 		os_memcpy(f->text, pos2 + 3, descr_len - 3);
1060 		pos2 += descr_len;
1061 	}
1062 
1063 	wpa_printf(MSG_DEBUG, "HS 2.0: Added OSU Provider through " MACSTR,
1064 		   MAC2STR(bss->bssid));
1065 	wpa_s->osu_prov_count++;
1066 }
1067 
1068 
1069 void hs20_osu_icon_fetch(struct wpa_supplicant *wpa_s)
1070 {
1071 	struct wpa_bss *bss;
1072 	struct wpabuf *prov_anqp;
1073 	const u8 *pos, *end;
1074 	u16 len;
1075 	const u8 *osu_ssid, *osu_ssid2;
1076 	u8 osu_ssid_len, osu_ssid2_len;
1077 	u8 num_providers;
1078 
1079 	hs20_free_osu_prov(wpa_s);
1080 
1081 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1082 		struct wpa_ie_data data;
1083 		const u8 *ie;
1084 
1085 		if (bss->anqp == NULL)
1086 			continue;
1087 		prov_anqp = bss->anqp->hs20_osu_providers_list;
1088 		if (prov_anqp == NULL)
1089 			continue;
1090 		ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1091 		if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &data) == 0 &&
1092 		    (data.key_mgmt & WPA_KEY_MGMT_OSEN)) {
1093 			osu_ssid2 = bss->ssid;
1094 			osu_ssid2_len = bss->ssid_len;
1095 		} else {
1096 			osu_ssid2 = NULL;
1097 			osu_ssid2_len = 0;
1098 		}
1099 		wpa_printf(MSG_DEBUG, "HS 2.0: Parsing OSU Providers list from "
1100 			   MACSTR, MAC2STR(bss->bssid));
1101 		wpa_hexdump_buf(MSG_DEBUG, "HS 2.0: OSU Providers list",
1102 				prov_anqp);
1103 		pos = wpabuf_head(prov_anqp);
1104 		end = pos + wpabuf_len(prov_anqp);
1105 
1106 		/* OSU SSID */
1107 		if (end - pos < 1)
1108 			continue;
1109 		if (1 + pos[0] > end - pos) {
1110 			wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
1111 				   "OSU SSID");
1112 			continue;
1113 		}
1114 		osu_ssid_len = *pos++;
1115 		if (osu_ssid_len > SSID_MAX_LEN) {
1116 			wpa_printf(MSG_DEBUG, "HS 2.0: Invalid OSU SSID "
1117 				   "Length %u", osu_ssid_len);
1118 			continue;
1119 		}
1120 		osu_ssid = pos;
1121 		pos += osu_ssid_len;
1122 
1123 		if (end - pos < 1) {
1124 			wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
1125 				   "Number of OSU Providers");
1126 			continue;
1127 		}
1128 		num_providers = *pos++;
1129 		wpa_printf(MSG_DEBUG, "HS 2.0: Number of OSU Providers: %u",
1130 			   num_providers);
1131 
1132 		/* OSU Providers */
1133 		while (end - pos > 2 && num_providers > 0) {
1134 			num_providers--;
1135 			len = WPA_GET_LE16(pos);
1136 			pos += 2;
1137 			if (len > (unsigned int) (end - pos))
1138 				break;
1139 			hs20_osu_add_prov(wpa_s, bss, osu_ssid,
1140 					  osu_ssid_len, osu_ssid2,
1141 					  osu_ssid2_len, pos, len);
1142 			pos += len;
1143 		}
1144 
1145 		if (pos != end) {
1146 			wpa_printf(MSG_DEBUG, "HS 2.0: Ignored %d bytes of "
1147 				   "extra data after OSU Providers",
1148 				   (int) (end - pos));
1149 		}
1150 
1151 		prov_anqp = bss->anqp->hs20_osu_providers_nai_list;
1152 		if (!prov_anqp)
1153 			continue;
1154 		wpa_printf(MSG_DEBUG,
1155 			   "HS 2.0: Parsing OSU Providers NAI List from "
1156 			   MACSTR, MAC2STR(bss->bssid));
1157 		wpa_hexdump_buf(MSG_DEBUG, "HS 2.0: OSU Providers NAI List",
1158 				prov_anqp);
1159 		pos = wpabuf_head(prov_anqp);
1160 		end = pos + wpabuf_len(prov_anqp);
1161 		num_providers = 0;
1162 		while (end - pos > 0) {
1163 			len = *pos++;
1164 			if (end - pos < len) {
1165 				wpa_printf(MSG_DEBUG,
1166 					   "HS 2.0: Not enough room for OSU_NAI");
1167 				break;
1168 			}
1169 			if (num_providers >= wpa_s->osu_prov_count) {
1170 				wpa_printf(MSG_DEBUG,
1171 					   "HS 2.0: Ignore unexpected OSU Provider NAI List entries");
1172 				break;
1173 			}
1174 			os_memcpy(wpa_s->osu_prov[num_providers].osu_nai2,
1175 				  pos, len);
1176 			pos += len;
1177 			num_providers++;
1178 		}
1179 	}
1180 
1181 	wpa_s->fetch_osu_icon_in_progress = 1;
1182 	hs20_next_osu_icon(wpa_s);
1183 }
1184 
1185 
1186 static void hs20_osu_scan_res_handler(struct wpa_supplicant *wpa_s,
1187 				      struct wpa_scan_results *scan_res)
1188 {
1189 	wpa_printf(MSG_DEBUG, "OSU provisioning fetch scan completed");
1190 	if (!wpa_s->fetch_osu_waiting_scan) {
1191 		wpa_printf(MSG_DEBUG, "OSU fetch have been canceled");
1192 		return;
1193 	}
1194 	wpa_s->network_select = 0;
1195 	wpa_s->fetch_all_anqp = 1;
1196 	wpa_s->fetch_osu_info = 1;
1197 	wpa_s->fetch_osu_icon_in_progress = 0;
1198 
1199 	interworking_start_fetch_anqp(wpa_s);
1200 }
1201 
1202 
1203 int hs20_fetch_osu(struct wpa_supplicant *wpa_s, int skip_scan)
1204 {
1205 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1206 		wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1207 			   "interface disabled");
1208 		return -1;
1209 	}
1210 
1211 	if (wpa_s->scanning) {
1212 		wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1213 			   "scanning");
1214 		return -1;
1215 	}
1216 
1217 	if (wpa_s->conf->osu_dir == NULL) {
1218 		wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1219 			   "osu_dir not configured");
1220 		return -1;
1221 	}
1222 
1223 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
1224 		wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1225 			   "fetch in progress (%d, %d)",
1226 			   wpa_s->fetch_anqp_in_progress,
1227 			   wpa_s->network_select);
1228 		return -1;
1229 	}
1230 
1231 	wpa_msg(wpa_s, MSG_INFO, "Starting OSU provisioning information fetch");
1232 	wpa_s->num_osu_scans = 0;
1233 	wpa_s->num_prov_found = 0;
1234 	if (skip_scan) {
1235 		wpa_s->network_select = 0;
1236 		wpa_s->fetch_all_anqp = 1;
1237 		wpa_s->fetch_osu_info = 1;
1238 		wpa_s->fetch_osu_icon_in_progress = 0;
1239 
1240 		interworking_start_fetch_anqp(wpa_s);
1241 	} else {
1242 		hs20_start_osu_scan(wpa_s);
1243 	}
1244 
1245 	return 0;
1246 }
1247 
1248 
1249 void hs20_start_osu_scan(struct wpa_supplicant *wpa_s)
1250 {
1251 	wpa_s->fetch_osu_waiting_scan = 1;
1252 	wpa_s->num_osu_scans++;
1253 	wpa_s->scan_req = MANUAL_SCAN_REQ;
1254 	wpa_s->scan_res_handler = hs20_osu_scan_res_handler;
1255 	wpa_supplicant_req_scan(wpa_s, 0, 0);
1256 }
1257 
1258 
1259 void hs20_cancel_fetch_osu(struct wpa_supplicant *wpa_s)
1260 {
1261 	wpa_printf(MSG_DEBUG, "Cancel OSU fetch");
1262 	interworking_stop_fetch_anqp(wpa_s);
1263 	wpa_s->fetch_osu_waiting_scan = 0;
1264 	wpa_s->network_select = 0;
1265 	wpa_s->fetch_osu_info = 0;
1266 	wpa_s->fetch_osu_icon_in_progress = 0;
1267 }
1268 
1269 
1270 void hs20_icon_fetch_failed(struct wpa_supplicant *wpa_s)
1271 {
1272 	hs20_osu_icon_fetch_result(wpa_s, -1);
1273 	eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
1274 	eloop_register_timeout(0, 0, hs20_continue_icon_fetch, wpa_s, NULL);
1275 }
1276 
1277 
1278 void hs20_rx_subscription_remediation(struct wpa_supplicant *wpa_s,
1279 				      const char *url, u8 osu_method)
1280 {
1281 	if (url)
1282 		wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION "%u %s",
1283 			osu_method, url);
1284 	else
1285 		wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION);
1286 }
1287 
1288 
1289 void hs20_rx_deauth_imminent_notice(struct wpa_supplicant *wpa_s, u8 code,
1290 				    u16 reauth_delay, const char *url)
1291 {
1292 	if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
1293 		wpa_printf(MSG_DEBUG, "HS 2.0: Ignore deauthentication imminent notice since PMF was not enabled");
1294 		return;
1295 	}
1296 
1297 	wpa_msg(wpa_s, MSG_INFO, HS20_DEAUTH_IMMINENT_NOTICE "%u %u %s",
1298 		code, reauth_delay, url);
1299 
1300 	if (code == HS20_DEAUTH_REASON_CODE_BSS) {
1301 		wpa_printf(MSG_DEBUG, "HS 2.0: Add BSS to ignore list");
1302 		wpa_bssid_ignore_add(wpa_s, wpa_s->bssid);
1303 		/* TODO: For now, disable full ESS since some drivers may not
1304 		 * support disabling per BSS. */
1305 		if (wpa_s->current_ssid) {
1306 			struct os_reltime now;
1307 			os_get_reltime(&now);
1308 			if (now.sec + reauth_delay <=
1309 			    wpa_s->current_ssid->disabled_until.sec)
1310 				return;
1311 			wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds (BSS)",
1312 				   reauth_delay);
1313 			wpa_s->current_ssid->disabled_until.sec =
1314 				now.sec + reauth_delay;
1315 		}
1316 	}
1317 
1318 	if (code == HS20_DEAUTH_REASON_CODE_ESS && wpa_s->current_ssid) {
1319 		struct os_reltime now;
1320 		os_get_reltime(&now);
1321 		if (now.sec + reauth_delay <=
1322 		    wpa_s->current_ssid->disabled_until.sec)
1323 			return;
1324 		wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds",
1325 			   reauth_delay);
1326 		wpa_s->current_ssid->disabled_until.sec =
1327 			now.sec + reauth_delay;
1328 	}
1329 }
1330 
1331 
1332 void hs20_rx_t_c_acceptance(struct wpa_supplicant *wpa_s, const char *url)
1333 {
1334 	if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
1335 		wpa_printf(MSG_DEBUG,
1336 			   "HS 2.0: Ignore Terms and Conditions Acceptance since PMF was not enabled");
1337 		return;
1338 	}
1339 
1340 	wpas_notify_hs20_t_c_acceptance(wpa_s, url);
1341 }
1342 
1343 
1344 void hs20_init(struct wpa_supplicant *wpa_s)
1345 {
1346 	dl_list_init(&wpa_s->icon_head);
1347 }
1348 
1349 
1350 void hs20_deinit(struct wpa_supplicant *wpa_s)
1351 {
1352 	eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
1353 	hs20_free_osu_prov(wpa_s);
1354 	if (wpa_s->icon_head.next)
1355 		hs20_del_icon(wpa_s, NULL, NULL);
1356 }
1357