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