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_memdup(pos, slen);
434			if (!icon->image)
435				return -1;
436			icon->image_len = slen;
437			hs20_remove_duplicate_icons(wpa_s, icon);
438			wpa_msg(wpa_s, MSG_INFO,
439				RX_HS20_ICON MACSTR " %s %u",
440				MAC2STR(sa), icon->file_name,
441				(unsigned int) icon->image_len);
442			wpas_notify_hs20_icon_query_done(wpa_s, sa,
443							 icon->file_name,
444							 icon->image,
445							 icon->image_len);
446			return 0;
447		}
448	}
449
450	wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR " Icon Binary File",
451		MAC2STR(sa));
452
453	if (slen < 4) {
454		wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
455			"value from " MACSTR, MAC2STR(sa));
456		return -1;
457	}
458
459	wpa_printf(MSG_DEBUG, "HS 2.0: Download Status Code %u", *pos);
460	if (*pos != 0)
461		return -1;
462	pos++;
463	slen--;
464
465	if ((size_t) 1 + pos[0] > slen) {
466		wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
467			"value from " MACSTR, MAC2STR(sa));
468		return -1;
469	}
470	wpa_hexdump_ascii(MSG_DEBUG, "Icon Type", pos + 1, pos[0]);
471	png = os_strncasecmp((char *) pos + 1, "image/png", 9) == 0;
472	slen -= 1 + pos[0];
473	pos += 1 + pos[0];
474
475	if (slen < 2) {
476		wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
477			"value from " MACSTR, MAC2STR(sa));
478		return -1;
479	}
480	data_len = WPA_GET_LE16(pos);
481	pos += 2;
482	slen -= 2;
483
484	if (data_len > slen) {
485		wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
486			"value from " MACSTR, MAC2STR(sa));
487		return -1;
488	}
489
490	wpa_printf(MSG_DEBUG, "Icon Binary Data: %u bytes", data_len);
491	if (wpa_s->conf->osu_dir == NULL)
492		return -1;
493
494	wpa_s->osu_icon_id++;
495	if (wpa_s->osu_icon_id == 0)
496		wpa_s->osu_icon_id++;
497	snprintf(fname, sizeof(fname), "%s/osu-icon-%u.%s",
498		 wpa_s->conf->osu_dir, wpa_s->osu_icon_id,
499		 png ? "png" : "icon");
500	f = fopen(fname, "wb");
501	if (f == NULL)
502		return -1;
503
504	hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
505
506	if (fwrite(pos, slen, 1, f) != 1) {
507		fclose(f);
508		unlink(fname);
509		return -1;
510	}
511	fclose(f);
512
513	wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP_ICON "%s", fname);
514	return 0;
515}
516
517
518static void hs20_continue_icon_fetch(void *eloop_ctx, void *sock_ctx)
519{
520	struct wpa_supplicant *wpa_s = eloop_ctx;
521	if (wpa_s->fetch_osu_icon_in_progress)
522		hs20_next_osu_icon(wpa_s);
523}
524
525
526static void hs20_osu_icon_fetch_result(struct wpa_supplicant *wpa_s, int res)
527{
528	size_t i, j;
529	struct os_reltime now, tmp;
530	int dur;
531
532	os_get_reltime(&now);
533	os_reltime_sub(&now, &wpa_s->osu_icon_fetch_start, &tmp);
534	dur = tmp.sec * 1000 + tmp.usec / 1000;
535	wpa_printf(MSG_DEBUG, "HS 2.0: Icon fetch dur=%d ms res=%d",
536		   dur, res);
537
538	for (i = 0; i < wpa_s->osu_prov_count; i++) {
539		struct osu_provider *osu = &wpa_s->osu_prov[i];
540		for (j = 0; j < osu->icon_count; j++) {
541			struct osu_icon *icon = &osu->icon[j];
542			if (icon->id || icon->failed)
543				continue;
544			if (res < 0)
545				icon->failed = 1;
546			else
547				icon->id = wpa_s->osu_icon_id;
548			return;
549		}
550	}
551}
552
553
554void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
555				  struct wpa_bss *bss, const u8 *sa,
556				  const u8 *data, size_t slen, u8 dialog_token)
557{
558	const u8 *pos = data;
559	u8 subtype;
560	struct wpa_bss_anqp *anqp = NULL;
561	int ret;
562
563	if (slen < 2)
564		return;
565
566	if (bss)
567		anqp = bss->anqp;
568
569	subtype = *pos++;
570	slen--;
571
572	pos++; /* Reserved */
573	slen--;
574
575	switch (subtype) {
576	case HS20_STYPE_CAPABILITY_LIST:
577		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
578			" HS Capability List", MAC2STR(sa));
579		wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
580		if (anqp) {
581			wpabuf_free(anqp->hs20_capability_list);
582			anqp->hs20_capability_list =
583				wpabuf_alloc_copy(pos, slen);
584		}
585		break;
586	case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
587		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
588			" Operator Friendly Name", MAC2STR(sa));
589		wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
590		if (anqp) {
591			wpabuf_free(anqp->hs20_operator_friendly_name);
592			anqp->hs20_operator_friendly_name =
593				wpabuf_alloc_copy(pos, slen);
594		}
595		break;
596	case HS20_STYPE_WAN_METRICS:
597		wpa_hexdump(MSG_DEBUG, "WAN Metrics", pos, slen);
598		if (slen < 13) {
599			wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short WAN "
600				"Metrics value from " MACSTR, MAC2STR(sa));
601			break;
602		}
603		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
604			" WAN Metrics %02x:%u:%u:%u:%u:%u", MAC2STR(sa),
605			pos[0], WPA_GET_LE32(pos + 1), WPA_GET_LE32(pos + 5),
606			pos[9], pos[10], WPA_GET_LE16(pos + 11));
607		if (anqp) {
608			wpabuf_free(anqp->hs20_wan_metrics);
609			anqp->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
610		}
611		break;
612	case HS20_STYPE_CONNECTION_CAPABILITY:
613		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
614			" Connection Capability", MAC2STR(sa));
615		wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
616		if (anqp) {
617			wpabuf_free(anqp->hs20_connection_capability);
618			anqp->hs20_connection_capability =
619				wpabuf_alloc_copy(pos, slen);
620		}
621		break;
622	case HS20_STYPE_OPERATING_CLASS:
623		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
624			" Operating Class", MAC2STR(sa));
625		wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
626		if (anqp) {
627			wpabuf_free(anqp->hs20_operating_class);
628			anqp->hs20_operating_class =
629				wpabuf_alloc_copy(pos, slen);
630		}
631		break;
632	case HS20_STYPE_OSU_PROVIDERS_LIST:
633		wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
634			" OSU Providers list", MAC2STR(sa));
635		wpa_s->num_prov_found++;
636		if (anqp) {
637			wpabuf_free(anqp->hs20_osu_providers_list);
638			anqp->hs20_osu_providers_list =
639				wpabuf_alloc_copy(pos, slen);
640		}
641		break;
642	case HS20_STYPE_ICON_BINARY_FILE:
643		ret = hs20_process_icon_binary_file(wpa_s, sa, pos, slen,
644						    dialog_token);
645		if (wpa_s->fetch_osu_icon_in_progress) {
646			hs20_osu_icon_fetch_result(wpa_s, ret);
647			eloop_cancel_timeout(hs20_continue_icon_fetch,
648					     wpa_s, NULL);
649			eloop_register_timeout(0, 0, hs20_continue_icon_fetch,
650					       wpa_s, NULL);
651		}
652		break;
653	default:
654		wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
655		break;
656	}
657}
658
659
660void hs20_notify_parse_done(struct wpa_supplicant *wpa_s)
661{
662	if (!wpa_s->fetch_osu_icon_in_progress)
663		return;
664	if (eloop_is_timeout_registered(hs20_continue_icon_fetch, wpa_s, NULL))
665		return;
666	/*
667	 * We are going through icon fetch, but no icon response was received.
668	 * Assume this means the current AP could not provide an answer to avoid
669	 * getting stuck in fetch iteration.
670	 */
671	hs20_icon_fetch_failed(wpa_s);
672}
673
674
675static void hs20_free_osu_prov_entry(struct osu_provider *prov)
676{
677}
678
679
680void hs20_free_osu_prov(struct wpa_supplicant *wpa_s)
681{
682	size_t i;
683	for (i = 0; i < wpa_s->osu_prov_count; i++)
684		hs20_free_osu_prov_entry(&wpa_s->osu_prov[i]);
685	os_free(wpa_s->osu_prov);
686	wpa_s->osu_prov = NULL;
687	wpa_s->osu_prov_count = 0;
688}
689
690
691static void hs20_osu_fetch_done(struct wpa_supplicant *wpa_s)
692{
693	char fname[256];
694	FILE *f;
695	size_t i, j;
696
697	wpa_s->fetch_osu_info = 0;
698	wpa_s->fetch_osu_icon_in_progress = 0;
699
700	if (wpa_s->conf->osu_dir == NULL) {
701		hs20_free_osu_prov(wpa_s);
702		wpa_s->fetch_anqp_in_progress = 0;
703		return;
704	}
705
706	snprintf(fname, sizeof(fname), "%s/osu-providers.txt",
707		 wpa_s->conf->osu_dir);
708	f = fopen(fname, "w");
709	if (f == NULL) {
710		wpa_msg(wpa_s, MSG_INFO,
711			"Could not write OSU provider information");
712		hs20_free_osu_prov(wpa_s);
713		wpa_s->fetch_anqp_in_progress = 0;
714		return;
715	}
716
717	hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
718
719	for (i = 0; i < wpa_s->osu_prov_count; i++) {
720		struct osu_provider *osu = &wpa_s->osu_prov[i];
721		if (i > 0)
722			fprintf(f, "\n");
723		fprintf(f, "OSU-PROVIDER " MACSTR "\n"
724			"uri=%s\n"
725			"methods=%08x\n",
726			MAC2STR(osu->bssid), osu->server_uri, osu->osu_methods);
727		if (osu->osu_ssid_len) {
728			fprintf(f, "osu_ssid=%s\n",
729				wpa_ssid_txt(osu->osu_ssid,
730					     osu->osu_ssid_len));
731		}
732		if (osu->osu_nai[0])
733			fprintf(f, "osu_nai=%s\n", osu->osu_nai);
734		for (j = 0; j < osu->friendly_name_count; j++) {
735			fprintf(f, "friendly_name=%s:%s\n",
736				osu->friendly_name[j].lang,
737				osu->friendly_name[j].text);
738		}
739		for (j = 0; j < osu->serv_desc_count; j++) {
740			fprintf(f, "desc=%s:%s\n",
741				osu->serv_desc[j].lang,
742				osu->serv_desc[j].text);
743		}
744		for (j = 0; j < osu->icon_count; j++) {
745			struct osu_icon *icon = &osu->icon[j];
746			if (icon->failed)
747				continue; /* could not fetch icon */
748			fprintf(f, "icon=%u:%u:%u:%s:%s:%s\n",
749				icon->id, icon->width, icon->height, icon->lang,
750				icon->icon_type, icon->filename);
751		}
752	}
753	fclose(f);
754	hs20_free_osu_prov(wpa_s);
755
756	wpa_msg(wpa_s, MSG_INFO, "OSU provider fetch completed");
757	wpa_s->fetch_anqp_in_progress = 0;
758}
759
760
761void hs20_next_osu_icon(struct wpa_supplicant *wpa_s)
762{
763	size_t i, j;
764
765	wpa_printf(MSG_DEBUG, "HS 2.0: Ready to fetch next icon");
766
767	for (i = 0; i < wpa_s->osu_prov_count; i++) {
768		struct osu_provider *osu = &wpa_s->osu_prov[i];
769		for (j = 0; j < osu->icon_count; j++) {
770			struct osu_icon *icon = &osu->icon[j];
771			if (icon->id || icon->failed)
772				continue;
773
774			wpa_printf(MSG_DEBUG, "HS 2.0: Try to fetch icon '%s' "
775				   "from " MACSTR, icon->filename,
776				   MAC2STR(osu->bssid));
777			os_get_reltime(&wpa_s->osu_icon_fetch_start);
778			if (hs20_anqp_send_req(wpa_s, osu->bssid,
779					       BIT(HS20_STYPE_ICON_REQUEST),
780					       (u8 *) icon->filename,
781					       os_strlen(icon->filename),
782					       0) < 0) {
783				icon->failed = 1;
784				continue;
785			}
786			return;
787		}
788	}
789
790	wpa_printf(MSG_DEBUG, "HS 2.0: No more icons to fetch");
791	hs20_osu_fetch_done(wpa_s);
792}
793
794
795static void hs20_osu_add_prov(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
796			      const u8 *osu_ssid, u8 osu_ssid_len,
797			      const u8 *pos, size_t len)
798{
799	struct osu_provider *prov;
800	const u8 *end = pos + len;
801	u16 len2;
802	const u8 *pos2;
803	u8 uri_len, osu_method_len, osu_nai_len;
804
805	wpa_hexdump(MSG_DEBUG, "HS 2.0: Parsing OSU Provider", pos, len);
806	prov = os_realloc_array(wpa_s->osu_prov,
807				wpa_s->osu_prov_count + 1,
808				sizeof(*prov));
809	if (prov == NULL)
810		return;
811	wpa_s->osu_prov = prov;
812	prov = &prov[wpa_s->osu_prov_count];
813	os_memset(prov, 0, sizeof(*prov));
814
815	os_memcpy(prov->bssid, bss->bssid, ETH_ALEN);
816	os_memcpy(prov->osu_ssid, osu_ssid, osu_ssid_len);
817	prov->osu_ssid_len = osu_ssid_len;
818
819	/* OSU Friendly Name Length */
820	if (end - pos < 2) {
821		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
822			   "Friendly Name Length");
823		return;
824	}
825	len2 = WPA_GET_LE16(pos);
826	pos += 2;
827	if (len2 > end - pos) {
828		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
829			   "Friendly Name Duples");
830		return;
831	}
832	pos2 = pos;
833	pos += len2;
834
835	/* OSU Friendly Name Duples */
836	while (pos - pos2 >= 4 && prov->friendly_name_count < OSU_MAX_ITEMS) {
837		struct osu_lang_string *f;
838		if (1 + pos2[0] > pos - pos2 || pos2[0] < 3) {
839			wpa_printf(MSG_DEBUG, "Invalid OSU Friendly Name");
840			break;
841		}
842		f = &prov->friendly_name[prov->friendly_name_count++];
843		os_memcpy(f->lang, pos2 + 1, 3);
844		os_memcpy(f->text, pos2 + 1 + 3, pos2[0] - 3);
845		pos2 += 1 + pos2[0];
846	}
847
848	/* OSU Server URI */
849	if (end - pos < 1) {
850		wpa_printf(MSG_DEBUG,
851			   "HS 2.0: Not enough room for OSU Server URI length");
852		return;
853	}
854	uri_len = *pos++;
855	if (uri_len > end - pos) {
856		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Server "
857			   "URI");
858		return;
859	}
860	os_memcpy(prov->server_uri, pos, uri_len);
861	pos += uri_len;
862
863	/* OSU Method list */
864	if (end - pos < 1) {
865		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
866			   "list length");
867		return;
868	}
869	osu_method_len = pos[0];
870	if (osu_method_len > end - pos - 1) {
871		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
872			   "list");
873		return;
874	}
875	pos2 = pos + 1;
876	pos += 1 + osu_method_len;
877	while (pos2 < pos) {
878		if (*pos2 < 32)
879			prov->osu_methods |= BIT(*pos2);
880		pos2++;
881	}
882
883	/* Icons Available Length */
884	if (end - pos < 2) {
885		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
886			   "Available Length");
887		return;
888	}
889	len2 = WPA_GET_LE16(pos);
890	pos += 2;
891	if (len2 > end - pos) {
892		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
893			   "Available");
894		return;
895	}
896	pos2 = pos;
897	pos += len2;
898
899	/* Icons Available */
900	while (pos2 < pos) {
901		struct osu_icon *icon = &prov->icon[prov->icon_count];
902		u8 flen;
903
904		if (2 + 2 + 3 + 1 + 1 > pos - pos2) {
905			wpa_printf(MSG_DEBUG, "HS 2.0: Invalid Icon Metadata");
906			break;
907		}
908
909		icon->width = WPA_GET_LE16(pos2);
910		pos2 += 2;
911		icon->height = WPA_GET_LE16(pos2);
912		pos2 += 2;
913		os_memcpy(icon->lang, pos2, 3);
914		pos2 += 3;
915
916		flen = *pos2++;
917		if (flen > pos - pos2) {
918			wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon Type");
919			break;
920		}
921		os_memcpy(icon->icon_type, pos2, flen);
922		pos2 += flen;
923
924		if (pos - pos2 < 1) {
925			wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
926				   "Filename length");
927			break;
928		}
929		flen = *pos2++;
930		if (flen > pos - pos2) {
931			wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
932				   "Filename");
933			break;
934		}
935		os_memcpy(icon->filename, pos2, flen);
936		pos2 += flen;
937
938		prov->icon_count++;
939	}
940
941	/* OSU_NAI */
942	if (end - pos < 1) {
943		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
944		return;
945	}
946	osu_nai_len = *pos++;
947	if (osu_nai_len > end - pos) {
948		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
949		return;
950	}
951	os_memcpy(prov->osu_nai, pos, osu_nai_len);
952	pos += osu_nai_len;
953
954	/* OSU Service Description Length */
955	if (end - pos < 2) {
956		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
957			   "Service Description Length");
958		return;
959	}
960	len2 = WPA_GET_LE16(pos);
961	pos += 2;
962	if (len2 > end - pos) {
963		wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
964			   "Service Description Duples");
965		return;
966	}
967	pos2 = pos;
968	pos += len2;
969
970	/* OSU Service Description Duples */
971	while (pos - pos2 >= 4 && prov->serv_desc_count < OSU_MAX_ITEMS) {
972		struct osu_lang_string *f;
973		u8 descr_len;
974
975		descr_len = *pos2++;
976		if (descr_len > pos - pos2 || descr_len < 3) {
977			wpa_printf(MSG_DEBUG, "Invalid OSU Service "
978				   "Description");
979			break;
980		}
981		f = &prov->serv_desc[prov->serv_desc_count++];
982		os_memcpy(f->lang, pos2, 3);
983		os_memcpy(f->text, pos2 + 3, descr_len - 3);
984		pos2 += descr_len;
985	}
986
987	wpa_printf(MSG_DEBUG, "HS 2.0: Added OSU Provider through " MACSTR,
988		   MAC2STR(bss->bssid));
989	wpa_s->osu_prov_count++;
990}
991
992
993void hs20_osu_icon_fetch(struct wpa_supplicant *wpa_s)
994{
995	struct wpa_bss *bss;
996	struct wpabuf *prov_anqp;
997	const u8 *pos, *end;
998	u16 len;
999	const u8 *osu_ssid;
1000	u8 osu_ssid_len;
1001	u8 num_providers;
1002
1003	hs20_free_osu_prov(wpa_s);
1004
1005	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1006		if (bss->anqp == NULL)
1007			continue;
1008		prov_anqp = bss->anqp->hs20_osu_providers_list;
1009		if (prov_anqp == NULL)
1010			continue;
1011		wpa_printf(MSG_DEBUG, "HS 2.0: Parsing OSU Providers list from "
1012			   MACSTR, MAC2STR(bss->bssid));
1013		wpa_hexdump_buf(MSG_DEBUG, "HS 2.0: OSU Providers list",
1014				prov_anqp);
1015		pos = wpabuf_head(prov_anqp);
1016		end = pos + wpabuf_len(prov_anqp);
1017
1018		/* OSU SSID */
1019		if (end - pos < 1)
1020			continue;
1021		if (1 + pos[0] > end - pos) {
1022			wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
1023				   "OSU SSID");
1024			continue;
1025		}
1026		osu_ssid_len = *pos++;
1027		if (osu_ssid_len > SSID_MAX_LEN) {
1028			wpa_printf(MSG_DEBUG, "HS 2.0: Invalid OSU SSID "
1029				   "Length %u", osu_ssid_len);
1030			continue;
1031		}
1032		osu_ssid = pos;
1033		pos += osu_ssid_len;
1034
1035		if (end - pos < 1) {
1036			wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
1037				   "Number of OSU Providers");
1038			continue;
1039		}
1040		num_providers = *pos++;
1041		wpa_printf(MSG_DEBUG, "HS 2.0: Number of OSU Providers: %u",
1042			   num_providers);
1043
1044		/* OSU Providers */
1045		while (end - pos > 2 && num_providers > 0) {
1046			num_providers--;
1047			len = WPA_GET_LE16(pos);
1048			pos += 2;
1049			if (len > (unsigned int) (end - pos))
1050				break;
1051			hs20_osu_add_prov(wpa_s, bss, osu_ssid,
1052					  osu_ssid_len, pos, len);
1053			pos += len;
1054		}
1055
1056		if (pos != end) {
1057			wpa_printf(MSG_DEBUG, "HS 2.0: Ignored %d bytes of "
1058				   "extra data after OSU Providers",
1059				   (int) (end - pos));
1060		}
1061	}
1062
1063	wpa_s->fetch_osu_icon_in_progress = 1;
1064	hs20_next_osu_icon(wpa_s);
1065}
1066
1067
1068static void hs20_osu_scan_res_handler(struct wpa_supplicant *wpa_s,
1069				      struct wpa_scan_results *scan_res)
1070{
1071	wpa_printf(MSG_DEBUG, "OSU provisioning fetch scan completed");
1072	if (!wpa_s->fetch_osu_waiting_scan) {
1073		wpa_printf(MSG_DEBUG, "OSU fetch have been canceled");
1074		return;
1075	}
1076	wpa_s->network_select = 0;
1077	wpa_s->fetch_all_anqp = 1;
1078	wpa_s->fetch_osu_info = 1;
1079	wpa_s->fetch_osu_icon_in_progress = 0;
1080
1081	interworking_start_fetch_anqp(wpa_s);
1082}
1083
1084
1085int hs20_fetch_osu(struct wpa_supplicant *wpa_s, int skip_scan)
1086{
1087	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1088		wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1089			   "interface disabled");
1090		return -1;
1091	}
1092
1093	if (wpa_s->scanning) {
1094		wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1095			   "scanning");
1096		return -1;
1097	}
1098
1099	if (wpa_s->conf->osu_dir == NULL) {
1100		wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1101			   "osu_dir not configured");
1102		return -1;
1103	}
1104
1105	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
1106		wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1107			   "fetch in progress (%d, %d)",
1108			   wpa_s->fetch_anqp_in_progress,
1109			   wpa_s->network_select);
1110		return -1;
1111	}
1112
1113	wpa_msg(wpa_s, MSG_INFO, "Starting OSU provisioning information fetch");
1114	wpa_s->num_osu_scans = 0;
1115	wpa_s->num_prov_found = 0;
1116	if (skip_scan) {
1117		wpa_s->network_select = 0;
1118		wpa_s->fetch_all_anqp = 1;
1119		wpa_s->fetch_osu_info = 1;
1120		wpa_s->fetch_osu_icon_in_progress = 0;
1121
1122		interworking_start_fetch_anqp(wpa_s);
1123	} else {
1124		hs20_start_osu_scan(wpa_s);
1125	}
1126
1127	return 0;
1128}
1129
1130
1131void hs20_start_osu_scan(struct wpa_supplicant *wpa_s)
1132{
1133	wpa_s->fetch_osu_waiting_scan = 1;
1134	wpa_s->num_osu_scans++;
1135	wpa_s->scan_req = MANUAL_SCAN_REQ;
1136	wpa_s->scan_res_handler = hs20_osu_scan_res_handler;
1137	wpa_supplicant_req_scan(wpa_s, 0, 0);
1138}
1139
1140
1141void hs20_cancel_fetch_osu(struct wpa_supplicant *wpa_s)
1142{
1143	wpa_printf(MSG_DEBUG, "Cancel OSU fetch");
1144	interworking_stop_fetch_anqp(wpa_s);
1145	wpa_s->fetch_osu_waiting_scan = 0;
1146	wpa_s->network_select = 0;
1147	wpa_s->fetch_osu_info = 0;
1148	wpa_s->fetch_osu_icon_in_progress = 0;
1149}
1150
1151
1152void hs20_icon_fetch_failed(struct wpa_supplicant *wpa_s)
1153{
1154	hs20_osu_icon_fetch_result(wpa_s, -1);
1155	eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
1156	eloop_register_timeout(0, 0, hs20_continue_icon_fetch, wpa_s, NULL);
1157}
1158
1159
1160void hs20_rx_subscription_remediation(struct wpa_supplicant *wpa_s,
1161				      const char *url, u8 osu_method)
1162{
1163	if (url)
1164		wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION "%u %s",
1165			osu_method, url);
1166	else
1167		wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION);
1168	wpas_notify_hs20_rx_subscription_remediation(wpa_s, url, osu_method);
1169}
1170
1171
1172void hs20_rx_deauth_imminent_notice(struct wpa_supplicant *wpa_s, u8 code,
1173				    u16 reauth_delay, const char *url)
1174{
1175	if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
1176		wpa_printf(MSG_DEBUG, "HS 2.0: Ignore deauthentication imminent notice since PMF was not enabled");
1177		return;
1178	}
1179
1180	wpa_msg(wpa_s, MSG_INFO, HS20_DEAUTH_IMMINENT_NOTICE "%u %u %s",
1181		code, reauth_delay, url);
1182	wpas_notify_hs20_rx_deauth_imminent_notice(wpa_s, code, reauth_delay, url);
1183
1184	if (code == HS20_DEAUTH_REASON_CODE_BSS) {
1185		wpa_printf(MSG_DEBUG, "HS 2.0: Add BSS to blacklist");
1186		wpa_blacklist_add(wpa_s, wpa_s->bssid);
1187		/* TODO: For now, disable full ESS since some drivers may not
1188		 * support disabling per BSS. */
1189		if (wpa_s->current_ssid) {
1190			struct os_reltime now;
1191			os_get_reltime(&now);
1192			if (now.sec + reauth_delay <=
1193			    wpa_s->current_ssid->disabled_until.sec)
1194				return;
1195			wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds (BSS)",
1196				   reauth_delay);
1197			wpa_s->current_ssid->disabled_until.sec =
1198				now.sec + reauth_delay;
1199		}
1200	}
1201
1202	if (code == HS20_DEAUTH_REASON_CODE_ESS && wpa_s->current_ssid) {
1203		struct os_reltime now;
1204		os_get_reltime(&now);
1205		if (now.sec + reauth_delay <=
1206		    wpa_s->current_ssid->disabled_until.sec)
1207			return;
1208		wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds",
1209			   reauth_delay);
1210		wpa_s->current_ssid->disabled_until.sec =
1211			now.sec + reauth_delay;
1212	}
1213}
1214
1215
1216void hs20_init(struct wpa_supplicant *wpa_s)
1217{
1218	dl_list_init(&wpa_s->icon_head);
1219}
1220
1221
1222void hs20_deinit(struct wpa_supplicant *wpa_s)
1223{
1224	eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
1225	hs20_free_osu_prov(wpa_s);
1226	if (wpa_s->icon_head.next)
1227		hs20_del_icon(wpa_s, NULL, NULL);
1228}
1229