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