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