blob: 8aeb3b00dce5a80722aee9d571ef6fcccdd0049e [file] [log] [blame]
Dmitry Shmidt04949592012-07-19 12:16:46 -07001/*
2 * Copyright (c) 2009, Atheros Communications, Inc.
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
Dmitry Shmidt04949592012-07-19 12:16:46 -07004 *
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"
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080010#include <sys/stat.h>
Dmitry Shmidt04949592012-07-19 12:16:46 -070011
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"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080018#include "rsn_supp/wpa.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070019#include "wpa_supplicant_i.h"
20#include "driver_i.h"
21#include "config.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080022#include "scan.h"
Roshan Pius04a9d742016-12-12 12:40:46 -080023#include "notify.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070024#include "bss.h"
Hai Shalom60840252021-02-19 19:02:11 -080025#include "bssid_ignore.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070026#include "gas_query.h"
27#include "interworking.h"
28#include "hs20_supplicant.h"
Dmitry Shmidt7d56b752015-12-22 10:59:44 -080029#include "base64.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070030
31
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080032#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];
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -070051 u8 osu_ssid[SSID_MAX_LEN];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080052 u8 osu_ssid_len;
Hai Shalom39ba6fc2019-01-22 12:40:38 -080053 u8 osu_ssid2[SSID_MAX_LEN];
54 u8 osu_ssid2_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080055 char server_uri[256];
56 u32 osu_methods; /* bit 0 = OMA-DM, bit 1 = SOAP-XML SPP */
57 char osu_nai[256];
Hai Shalom39ba6fc2019-01-22 12:40:38 -080058 char osu_nai2[256];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080059 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
Dmitry Shmidt849734c2016-05-27 09:59:01 -070068void 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 wpa_printf(MSG_DEBUG,
78 "Not configuring frame filtering - BSS " MACSTR
79 " is not a Hotspot 2.0 network", MAC2STR(bssid));
80 return;
81 }
82
83 ie = wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE);
84
85 /* Check if DGAF disabled bit is zero (5th byte in the IE) */
86 if (!ie || ie[1] < 5)
87 wpa_printf(MSG_DEBUG,
88 "Not configuring frame filtering - Can't extract DGAF bit");
89 else if (!(ie[6] & HS20_DGAF_DISABLED))
90 filter |= WPA_DATA_FRAME_FILTER_FLAG_GTK;
91
92 ext_capa = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
93 if (!ext_capa || ext_capa[1] < 2) {
94 wpa_printf(MSG_DEBUG,
95 "Not configuring frame filtering - Can't extract Proxy ARP bit");
96 return;
97 }
98
Hai Shalom74f70d42019-02-11 14:42:39 -080099 if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_PROXY_ARP))
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700100 filter |= WPA_DATA_FRAME_FILTER_FLAG_ARP |
101 WPA_DATA_FRAME_FILTER_FLAG_NA;
102
103 wpa_drv_configure_frame_filters(wpa_s, filter);
104}
105
106
Hai Shalom74f70d42019-02-11 14:42:39 -0800107void wpas_hs20_add_indication(struct wpabuf *buf, int pps_mo_id, int ap_release)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700108{
Hai Shalom74f70d42019-02-11 14:42:39 -0800109 int release;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800110 u8 conf;
111
Hai Shalom74f70d42019-02-11 14:42:39 -0800112 release = (HS20_VERSION >> 4) + 1;
113 if (ap_release > 0 && release > ap_release)
114 release = ap_release;
115 if (release < 2)
116 pps_mo_id = -1;
117
Dmitry Shmidt04949592012-07-19 12:16:46 -0700118 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800119 wpabuf_put_u8(buf, pps_mo_id >= 0 ? 7 : 5);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120 wpabuf_put_be24(buf, OUI_WFA);
121 wpabuf_put_u8(buf, HS20_INDICATION_OUI_TYPE);
Hai Shalom74f70d42019-02-11 14:42:39 -0800122 conf = (release - 1) << 4;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800123 if (pps_mo_id >= 0)
124 conf |= HS20_PPS_MO_ID_PRESENT;
125 wpabuf_put_u8(buf, conf);
126 if (pps_mo_id >= 0)
127 wpabuf_put_le16(buf, pps_mo_id);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700128}
129
130
Roshan Pius3a1667e2018-07-03 15:17:14 -0700131void wpas_hs20_add_roam_cons_sel(struct wpabuf *buf,
132 const struct wpa_ssid *ssid)
133{
134 if (!ssid->roaming_consortium_selection ||
135 !ssid->roaming_consortium_selection_len)
136 return;
137
138 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
139 wpabuf_put_u8(buf, 4 + ssid->roaming_consortium_selection_len);
140 wpabuf_put_be24(buf, OUI_WFA);
141 wpabuf_put_u8(buf, HS20_ROAMING_CONS_SEL_OUI_TYPE);
142 wpabuf_put_data(buf, ssid->roaming_consortium_selection,
143 ssid->roaming_consortium_selection_len);
144}
145
146
Hai Shalom74f70d42019-02-11 14:42:39 -0800147int get_hs20_version(struct wpa_bss *bss)
148{
149 const u8 *ie;
150
151 if (!bss)
152 return 0;
153
154 ie = wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE);
155 if (!ie || ie[1] < 5)
156 return 0;
157
158 return ((ie[6] >> 4) & 0x0f) + 1;
159}
160
161
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700162int is_hs20_network(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
163 struct wpa_bss *bss)
164{
165 if (!wpa_s->conf->hs20 || !ssid)
166 return 0;
167
168 if (ssid->parent_cred)
169 return 1;
170
171 if (bss && !wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE))
172 return 0;
173
174 /*
175 * This may catch some non-Hotspot 2.0 cases, but it is safer to do that
176 * than cause Hotspot 2.0 connections without indication element getting
177 * added. Non-Hotspot 2.0 APs should ignore the unknown vendor element.
178 */
179
180 if (!(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X))
181 return 0;
182 if (!(ssid->pairwise_cipher & WPA_CIPHER_CCMP))
183 return 0;
184 if (ssid->proto != WPA_PROTO_RSN)
185 return 0;
186
187 return 1;
188}
189
190
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800191int hs20_get_pps_mo_id(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
192{
193 struct wpa_cred *cred;
194
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700195 if (ssid == NULL)
196 return 0;
197
198 if (ssid->update_identifier)
199 return ssid->update_identifier;
200
201 if (ssid->parent_cred == NULL)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800202 return 0;
203
204 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
205 if (ssid->parent_cred == cred)
206 return cred->update_identifier;
207 }
208
209 return 0;
210}
211
212
Dmitry Shmidt15907092014-03-25 10:42:57 -0700213void hs20_put_anqp_req(u32 stypes, const u8 *payload, size_t payload_len,
214 struct wpabuf *buf)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700215{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700216 u8 *len_pos;
217
Dmitry Shmidt04949592012-07-19 12:16:46 -0700218 if (buf == NULL)
Dmitry Shmidt15907092014-03-25 10:42:57 -0700219 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700220
221 len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
222 wpabuf_put_be24(buf, OUI_WFA);
223 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
224 if (stypes == BIT(HS20_STYPE_NAI_HOME_REALM_QUERY)) {
225 wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
226 wpabuf_put_u8(buf, 0); /* Reserved */
227 if (payload)
228 wpabuf_put_data(buf, payload, payload_len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800229 } else if (stypes == BIT(HS20_STYPE_ICON_REQUEST)) {
230 wpabuf_put_u8(buf, HS20_STYPE_ICON_REQUEST);
231 wpabuf_put_u8(buf, 0); /* Reserved */
232 if (payload)
233 wpabuf_put_data(buf, payload, payload_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700234 } else {
235 u8 i;
236 wpabuf_put_u8(buf, HS20_STYPE_QUERY_LIST);
237 wpabuf_put_u8(buf, 0); /* Reserved */
238 for (i = 0; i < 32; i++) {
239 if (stypes & BIT(i))
240 wpabuf_put_u8(buf, i);
241 }
242 }
243 gas_anqp_set_element_len(buf, len_pos);
244
245 gas_anqp_set_len(buf);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700246}
247
248
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700249static struct wpabuf * hs20_build_anqp_req(u32 stypes, const u8 *payload,
250 size_t payload_len)
Dmitry Shmidt15907092014-03-25 10:42:57 -0700251{
252 struct wpabuf *buf;
253
254 buf = gas_anqp_build_initial_req(0, 100 + payload_len);
255 if (buf == NULL)
256 return NULL;
257
258 hs20_put_anqp_req(stypes, payload, payload_len, buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700259
260 return buf;
261}
262
263
264int hs20_anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, u32 stypes,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800265 const u8 *payload, size_t payload_len, int inmem)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700266{
267 struct wpabuf *buf;
268 int ret = 0;
269 int freq;
270 struct wpa_bss *bss;
271 int res;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800272 struct icon_entry *icon_entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700273
Dmitry Shmidt04949592012-07-19 12:16:46 -0700274 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -0700275 if (!bss) {
276 wpa_printf(MSG_WARNING,
277 "ANQP: Cannot send query to unknown BSS "
278 MACSTR, MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700279 return -1;
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -0700280 }
281
282 wpa_bss_anqp_unshare_alloc(bss);
283 freq = bss->freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700284
285 wpa_printf(MSG_DEBUG, "HS20: ANQP Query Request to " MACSTR " for "
286 "subtypes 0x%x", MAC2STR(dst), stypes);
287
288 buf = hs20_build_anqp_req(stypes, payload, payload_len);
289 if (buf == NULL)
290 return -1;
291
Hai Shalomb755a2a2020-04-23 21:49:02 -0700292 res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, anqp_resp_cb,
293 wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700294 if (res < 0) {
295 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700296 wpabuf_free(buf);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800297 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700298 } else
299 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
300 "%u", res);
301
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800302 if (inmem) {
303 icon_entry = os_zalloc(sizeof(struct icon_entry));
304 if (!icon_entry)
305 return -1;
306 os_memcpy(icon_entry->bssid, dst, ETH_ALEN);
307 icon_entry->file_name = os_malloc(payload_len + 1);
308 if (!icon_entry->file_name) {
309 os_free(icon_entry);
310 return -1;
311 }
312 os_memcpy(icon_entry->file_name, payload, payload_len);
313 icon_entry->file_name[payload_len] = '\0';
314 icon_entry->dialog_token = res;
315
316 dl_list_add(&wpa_s->icon_head, &icon_entry->list);
317 }
318
Dmitry Shmidt04949592012-07-19 12:16:46 -0700319 return ret;
320}
321
322
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800323static struct icon_entry * hs20_find_icon(struct wpa_supplicant *wpa_s,
324 const u8 *bssid,
325 const char *file_name)
326{
327 struct icon_entry *icon;
328
329 dl_list_for_each(icon, &wpa_s->icon_head, struct icon_entry, list) {
330 if (os_memcmp(icon->bssid, bssid, ETH_ALEN) == 0 &&
331 os_strcmp(icon->file_name, file_name) == 0 && icon->image)
332 return icon;
333 }
334
335 return NULL;
336}
337
338
339int hs20_get_icon(struct wpa_supplicant *wpa_s, const u8 *bssid,
340 const char *file_name, size_t offset, size_t size,
341 char *reply, size_t buf_len)
342{
343 struct icon_entry *icon;
344 size_t out_size;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800345 char *b64;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800346 size_t b64_size;
347 int reply_size;
348
349 wpa_printf(MSG_DEBUG, "HS20: Get icon " MACSTR " %s @ %u +%u (%u)",
350 MAC2STR(bssid), file_name, (unsigned int) offset,
351 (unsigned int) size, (unsigned int) buf_len);
352
353 icon = hs20_find_icon(wpa_s, bssid, file_name);
354 if (!icon || !icon->image || offset >= icon->image_len)
355 return -1;
356 if (size > icon->image_len - offset)
357 size = icon->image_len - offset;
358 out_size = buf_len - 3 /* max base64 padding */;
359 if (size * 4 > out_size * 3)
360 size = out_size * 3 / 4;
361 if (size == 0)
362 return -1;
363
364 b64 = base64_encode(&icon->image[offset], size, &b64_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700365 if (b64 && buf_len >= b64_size) {
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800366 os_memcpy(reply, b64, b64_size);
367 reply_size = b64_size;
368 } else {
369 reply_size = -1;
370 }
371 os_free(b64);
372 return reply_size;
373}
374
375
376static void hs20_free_icon_entry(struct icon_entry *icon)
377{
378 wpa_printf(MSG_DEBUG, "HS20: Free stored icon from " MACSTR
379 " dialog_token=%u file_name=%s image_len=%u",
380 MAC2STR(icon->bssid), icon->dialog_token,
381 icon->file_name ? icon->file_name : "N/A",
382 (unsigned int) icon->image_len);
383 os_free(icon->file_name);
384 os_free(icon->image);
385 os_free(icon);
386}
387
388
389int hs20_del_icon(struct wpa_supplicant *wpa_s, const u8 *bssid,
390 const char *file_name)
391{
392 struct icon_entry *icon, *tmp;
393 int count = 0;
394
395 if (!bssid)
396 wpa_printf(MSG_DEBUG, "HS20: Delete all stored icons");
397 else if (!file_name)
398 wpa_printf(MSG_DEBUG, "HS20: Delete all stored icons for "
399 MACSTR, MAC2STR(bssid));
400 else
401 wpa_printf(MSG_DEBUG, "HS20: Delete stored icons for "
402 MACSTR " file name %s", MAC2STR(bssid), file_name);
403
404 dl_list_for_each_safe(icon, tmp, &wpa_s->icon_head, struct icon_entry,
405 list) {
406 if ((!bssid || os_memcmp(icon->bssid, bssid, ETH_ALEN) == 0) &&
407 (!file_name ||
408 os_strcmp(icon->file_name, file_name) == 0)) {
409 dl_list_del(&icon->list);
410 hs20_free_icon_entry(icon);
411 count++;
412 }
413 }
414 return count == 0 ? -1 : 0;
415}
416
417
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800418static void hs20_set_osu_access_permission(const char *osu_dir,
419 const char *fname)
420{
421 struct stat statbuf;
422
423 /* Get OSU directory information */
424 if (stat(osu_dir, &statbuf) < 0) {
425 wpa_printf(MSG_WARNING, "Cannot stat the OSU directory %s",
426 osu_dir);
427 return;
428 }
429
430 if (chmod(fname, statbuf.st_mode) < 0) {
431 wpa_printf(MSG_WARNING,
432 "Cannot change the permissions for %s", fname);
433 return;
434 }
435
Hai Shalom74f70d42019-02-11 14:42:39 -0800436 if (lchown(fname, statbuf.st_uid, statbuf.st_gid) < 0) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800437 wpa_printf(MSG_WARNING, "Cannot change the ownership for %s",
438 fname);
439 }
440}
441
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800442
443static void hs20_remove_duplicate_icons(struct wpa_supplicant *wpa_s,
444 struct icon_entry *new_icon)
445{
446 struct icon_entry *icon, *tmp;
447
448 dl_list_for_each_safe(icon, tmp, &wpa_s->icon_head, struct icon_entry,
449 list) {
450 if (icon == new_icon)
451 continue;
452 if (os_memcmp(icon->bssid, new_icon->bssid, ETH_ALEN) == 0 &&
453 os_strcmp(icon->file_name, new_icon->file_name) == 0) {
454 dl_list_del(&icon->list);
455 hs20_free_icon_entry(icon);
456 }
457 }
458}
459
460
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800461static int hs20_process_icon_binary_file(struct wpa_supplicant *wpa_s,
462 const u8 *sa, const u8 *pos,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800463 size_t slen, u8 dialog_token)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800464{
465 char fname[256];
466 int png;
467 FILE *f;
468 u16 data_len;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800469 struct icon_entry *icon;
470
471 dl_list_for_each(icon, &wpa_s->icon_head, struct icon_entry, list) {
472 if (icon->dialog_token == dialog_token && !icon->image &&
473 os_memcmp(icon->bssid, sa, ETH_ALEN) == 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700474 icon->image = os_memdup(pos, slen);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800475 if (!icon->image)
476 return -1;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800477 icon->image_len = slen;
478 hs20_remove_duplicate_icons(wpa_s, icon);
479 wpa_msg(wpa_s, MSG_INFO,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700480 RX_HS20_ICON MACSTR " %s %u",
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800481 MAC2STR(sa), icon->file_name,
482 (unsigned int) icon->image_len);
Roshan Pius04a9d742016-12-12 12:40:46 -0800483 wpas_notify_hs20_icon_query_done(wpa_s, sa,
484 icon->file_name,
485 icon->image,
486 icon->image_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800487 return 0;
488 }
489 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800490
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700491 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR " Icon Binary File",
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800492 MAC2STR(sa));
493
494 if (slen < 4) {
495 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
496 "value from " MACSTR, MAC2STR(sa));
497 return -1;
498 }
499
500 wpa_printf(MSG_DEBUG, "HS 2.0: Download Status Code %u", *pos);
501 if (*pos != 0)
502 return -1;
503 pos++;
504 slen--;
505
506 if ((size_t) 1 + pos[0] > slen) {
507 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
508 "value from " MACSTR, MAC2STR(sa));
509 return -1;
510 }
511 wpa_hexdump_ascii(MSG_DEBUG, "Icon Type", pos + 1, pos[0]);
512 png = os_strncasecmp((char *) pos + 1, "image/png", 9) == 0;
513 slen -= 1 + pos[0];
514 pos += 1 + pos[0];
515
516 if (slen < 2) {
517 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
518 "value from " MACSTR, MAC2STR(sa));
519 return -1;
520 }
521 data_len = WPA_GET_LE16(pos);
522 pos += 2;
523 slen -= 2;
524
525 if (data_len > slen) {
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
531 wpa_printf(MSG_DEBUG, "Icon Binary Data: %u bytes", data_len);
532 if (wpa_s->conf->osu_dir == NULL)
533 return -1;
534
535 wpa_s->osu_icon_id++;
536 if (wpa_s->osu_icon_id == 0)
537 wpa_s->osu_icon_id++;
538 snprintf(fname, sizeof(fname), "%s/osu-icon-%u.%s",
539 wpa_s->conf->osu_dir, wpa_s->osu_icon_id,
540 png ? "png" : "icon");
541 f = fopen(fname, "wb");
542 if (f == NULL)
543 return -1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800544
545 hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
546
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800547 if (fwrite(pos, slen, 1, f) != 1) {
548 fclose(f);
549 unlink(fname);
550 return -1;
551 }
552 fclose(f);
553
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700554 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP_ICON "%s", fname);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800555 return 0;
556}
557
558
559static void hs20_continue_icon_fetch(void *eloop_ctx, void *sock_ctx)
560{
561 struct wpa_supplicant *wpa_s = eloop_ctx;
562 if (wpa_s->fetch_osu_icon_in_progress)
563 hs20_next_osu_icon(wpa_s);
564}
565
566
567static void hs20_osu_icon_fetch_result(struct wpa_supplicant *wpa_s, int res)
568{
569 size_t i, j;
570 struct os_reltime now, tmp;
571 int dur;
572
573 os_get_reltime(&now);
574 os_reltime_sub(&now, &wpa_s->osu_icon_fetch_start, &tmp);
575 dur = tmp.sec * 1000 + tmp.usec / 1000;
576 wpa_printf(MSG_DEBUG, "HS 2.0: Icon fetch dur=%d ms res=%d",
577 dur, res);
578
579 for (i = 0; i < wpa_s->osu_prov_count; i++) {
580 struct osu_provider *osu = &wpa_s->osu_prov[i];
581 for (j = 0; j < osu->icon_count; j++) {
582 struct osu_icon *icon = &osu->icon[j];
583 if (icon->id || icon->failed)
584 continue;
585 if (res < 0)
586 icon->failed = 1;
587 else
588 icon->id = wpa_s->osu_icon_id;
589 return;
590 }
591 }
592}
593
594
Dmitry Shmidt04949592012-07-19 12:16:46 -0700595void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800596 struct wpa_bss *bss, const u8 *sa,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800597 const u8 *data, size_t slen, u8 dialog_token)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700598{
599 const u8 *pos = data;
600 u8 subtype;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700601 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800602 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700603
604 if (slen < 2)
605 return;
606
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700607 if (bss)
608 anqp = bss->anqp;
609
Dmitry Shmidt04949592012-07-19 12:16:46 -0700610 subtype = *pos++;
611 slen--;
612
613 pos++; /* Reserved */
614 slen--;
615
616 switch (subtype) {
617 case HS20_STYPE_CAPABILITY_LIST:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700618 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700619 " HS Capability List", MAC2STR(sa));
620 wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800621 if (anqp) {
622 wpabuf_free(anqp->hs20_capability_list);
623 anqp->hs20_capability_list =
624 wpabuf_alloc_copy(pos, slen);
625 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700626 break;
627 case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700628 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700629 " Operator Friendly Name", MAC2STR(sa));
630 wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700631 if (anqp) {
632 wpabuf_free(anqp->hs20_operator_friendly_name);
633 anqp->hs20_operator_friendly_name =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700634 wpabuf_alloc_copy(pos, slen);
635 }
636 break;
637 case HS20_STYPE_WAN_METRICS:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800638 wpa_hexdump(MSG_DEBUG, "WAN Metrics", pos, slen);
639 if (slen < 13) {
640 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short WAN "
641 "Metrics value from " MACSTR, MAC2STR(sa));
642 break;
643 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700644 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800645 " WAN Metrics %02x:%u:%u:%u:%u:%u", MAC2STR(sa),
646 pos[0], WPA_GET_LE32(pos + 1), WPA_GET_LE32(pos + 5),
647 pos[9], pos[10], WPA_GET_LE16(pos + 11));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700648 if (anqp) {
649 wpabuf_free(anqp->hs20_wan_metrics);
650 anqp->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700651 }
652 break;
653 case HS20_STYPE_CONNECTION_CAPABILITY:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700654 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700655 " Connection Capability", MAC2STR(sa));
656 wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700657 if (anqp) {
658 wpabuf_free(anqp->hs20_connection_capability);
659 anqp->hs20_connection_capability =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700660 wpabuf_alloc_copy(pos, slen);
661 }
662 break;
663 case HS20_STYPE_OPERATING_CLASS:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700664 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700665 " Operating Class", MAC2STR(sa));
666 wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700667 if (anqp) {
668 wpabuf_free(anqp->hs20_operating_class);
669 anqp->hs20_operating_class =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700670 wpabuf_alloc_copy(pos, slen);
671 }
672 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800673 case HS20_STYPE_OSU_PROVIDERS_LIST:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700674 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800675 " OSU Providers list", MAC2STR(sa));
676 wpa_s->num_prov_found++;
677 if (anqp) {
678 wpabuf_free(anqp->hs20_osu_providers_list);
679 anqp->hs20_osu_providers_list =
680 wpabuf_alloc_copy(pos, slen);
681 }
682 break;
683 case HS20_STYPE_ICON_BINARY_FILE:
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800684 ret = hs20_process_icon_binary_file(wpa_s, sa, pos, slen,
685 dialog_token);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800686 if (wpa_s->fetch_osu_icon_in_progress) {
687 hs20_osu_icon_fetch_result(wpa_s, ret);
688 eloop_cancel_timeout(hs20_continue_icon_fetch,
689 wpa_s, NULL);
690 eloop_register_timeout(0, 0, hs20_continue_icon_fetch,
691 wpa_s, NULL);
692 }
693 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700694 case HS20_STYPE_OPERATOR_ICON_METADATA:
695 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
696 " Operator Icon Metadata", MAC2STR(sa));
697 wpa_hexdump(MSG_DEBUG, "Operator Icon Metadata", pos, slen);
698 if (anqp) {
699 wpabuf_free(anqp->hs20_operator_icon_metadata);
700 anqp->hs20_operator_icon_metadata =
701 wpabuf_alloc_copy(pos, slen);
702 }
703 break;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800704 case HS20_STYPE_OSU_PROVIDERS_NAI_LIST:
705 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
706 " OSU Providers NAI List", MAC2STR(sa));
707 if (anqp) {
708 wpabuf_free(anqp->hs20_osu_providers_nai_list);
709 anqp->hs20_osu_providers_nai_list =
710 wpabuf_alloc_copy(pos, slen);
711 }
712 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700713 default:
714 wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
715 break;
716 }
717}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800718
719
720void hs20_notify_parse_done(struct wpa_supplicant *wpa_s)
721{
722 if (!wpa_s->fetch_osu_icon_in_progress)
723 return;
724 if (eloop_is_timeout_registered(hs20_continue_icon_fetch, wpa_s, NULL))
725 return;
726 /*
727 * We are going through icon fetch, but no icon response was received.
728 * Assume this means the current AP could not provide an answer to avoid
729 * getting stuck in fetch iteration.
730 */
731 hs20_icon_fetch_failed(wpa_s);
732}
733
734
735static void hs20_free_osu_prov_entry(struct osu_provider *prov)
736{
737}
738
739
740void hs20_free_osu_prov(struct wpa_supplicant *wpa_s)
741{
742 size_t i;
743 for (i = 0; i < wpa_s->osu_prov_count; i++)
744 hs20_free_osu_prov_entry(&wpa_s->osu_prov[i]);
745 os_free(wpa_s->osu_prov);
746 wpa_s->osu_prov = NULL;
747 wpa_s->osu_prov_count = 0;
748}
749
750
751static void hs20_osu_fetch_done(struct wpa_supplicant *wpa_s)
752{
753 char fname[256];
754 FILE *f;
755 size_t i, j;
756
757 wpa_s->fetch_osu_info = 0;
758 wpa_s->fetch_osu_icon_in_progress = 0;
759
760 if (wpa_s->conf->osu_dir == NULL) {
761 hs20_free_osu_prov(wpa_s);
762 wpa_s->fetch_anqp_in_progress = 0;
763 return;
764 }
765
766 snprintf(fname, sizeof(fname), "%s/osu-providers.txt",
767 wpa_s->conf->osu_dir);
768 f = fopen(fname, "w");
769 if (f == NULL) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700770 wpa_msg(wpa_s, MSG_INFO,
771 "Could not write OSU provider information");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800772 hs20_free_osu_prov(wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800773 wpa_s->fetch_anqp_in_progress = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800774 return;
775 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800776
777 hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
778
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800779 for (i = 0; i < wpa_s->osu_prov_count; i++) {
780 struct osu_provider *osu = &wpa_s->osu_prov[i];
781 if (i > 0)
782 fprintf(f, "\n");
783 fprintf(f, "OSU-PROVIDER " MACSTR "\n"
784 "uri=%s\n"
785 "methods=%08x\n",
786 MAC2STR(osu->bssid), osu->server_uri, osu->osu_methods);
787 if (osu->osu_ssid_len) {
788 fprintf(f, "osu_ssid=%s\n",
789 wpa_ssid_txt(osu->osu_ssid,
790 osu->osu_ssid_len));
791 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800792 if (osu->osu_ssid2_len) {
793 fprintf(f, "osu_ssid2=%s\n",
794 wpa_ssid_txt(osu->osu_ssid2,
795 osu->osu_ssid2_len));
796 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800797 if (osu->osu_nai[0])
798 fprintf(f, "osu_nai=%s\n", osu->osu_nai);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800799 if (osu->osu_nai2[0])
800 fprintf(f, "osu_nai2=%s\n", osu->osu_nai2);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800801 for (j = 0; j < osu->friendly_name_count; j++) {
802 fprintf(f, "friendly_name=%s:%s\n",
803 osu->friendly_name[j].lang,
804 osu->friendly_name[j].text);
805 }
806 for (j = 0; j < osu->serv_desc_count; j++) {
807 fprintf(f, "desc=%s:%s\n",
808 osu->serv_desc[j].lang,
809 osu->serv_desc[j].text);
810 }
811 for (j = 0; j < osu->icon_count; j++) {
812 struct osu_icon *icon = &osu->icon[j];
813 if (icon->failed)
814 continue; /* could not fetch icon */
815 fprintf(f, "icon=%u:%u:%u:%s:%s:%s\n",
816 icon->id, icon->width, icon->height, icon->lang,
817 icon->icon_type, icon->filename);
818 }
819 }
820 fclose(f);
821 hs20_free_osu_prov(wpa_s);
822
823 wpa_msg(wpa_s, MSG_INFO, "OSU provider fetch completed");
824 wpa_s->fetch_anqp_in_progress = 0;
825}
826
827
828void hs20_next_osu_icon(struct wpa_supplicant *wpa_s)
829{
830 size_t i, j;
831
832 wpa_printf(MSG_DEBUG, "HS 2.0: Ready to fetch next icon");
833
834 for (i = 0; i < wpa_s->osu_prov_count; i++) {
835 struct osu_provider *osu = &wpa_s->osu_prov[i];
836 for (j = 0; j < osu->icon_count; j++) {
837 struct osu_icon *icon = &osu->icon[j];
838 if (icon->id || icon->failed)
839 continue;
840
841 wpa_printf(MSG_DEBUG, "HS 2.0: Try to fetch icon '%s' "
842 "from " MACSTR, icon->filename,
843 MAC2STR(osu->bssid));
844 os_get_reltime(&wpa_s->osu_icon_fetch_start);
845 if (hs20_anqp_send_req(wpa_s, osu->bssid,
846 BIT(HS20_STYPE_ICON_REQUEST),
847 (u8 *) icon->filename,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800848 os_strlen(icon->filename),
849 0) < 0) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800850 icon->failed = 1;
851 continue;
852 }
853 return;
854 }
855 }
856
857 wpa_printf(MSG_DEBUG, "HS 2.0: No more icons to fetch");
858 hs20_osu_fetch_done(wpa_s);
859}
860
861
862static void hs20_osu_add_prov(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
863 const u8 *osu_ssid, u8 osu_ssid_len,
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800864 const u8 *osu_ssid2, u8 osu_ssid2_len,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800865 const u8 *pos, size_t len)
866{
867 struct osu_provider *prov;
868 const u8 *end = pos + len;
869 u16 len2;
870 const u8 *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800871 u8 uri_len, osu_method_len, osu_nai_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800872
873 wpa_hexdump(MSG_DEBUG, "HS 2.0: Parsing OSU Provider", pos, len);
874 prov = os_realloc_array(wpa_s->osu_prov,
875 wpa_s->osu_prov_count + 1,
876 sizeof(*prov));
877 if (prov == NULL)
878 return;
879 wpa_s->osu_prov = prov;
880 prov = &prov[wpa_s->osu_prov_count];
881 os_memset(prov, 0, sizeof(*prov));
882
883 os_memcpy(prov->bssid, bss->bssid, ETH_ALEN);
884 os_memcpy(prov->osu_ssid, osu_ssid, osu_ssid_len);
885 prov->osu_ssid_len = osu_ssid_len;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800886 if (osu_ssid2)
887 os_memcpy(prov->osu_ssid2, osu_ssid2, osu_ssid2_len);
888 prov->osu_ssid2_len = osu_ssid2_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800889
890 /* OSU Friendly Name Length */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800891 if (end - pos < 2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800892 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
893 "Friendly Name Length");
894 return;
895 }
896 len2 = WPA_GET_LE16(pos);
897 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800898 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800899 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
900 "Friendly Name Duples");
901 return;
902 }
903 pos2 = pos;
904 pos += len2;
905
906 /* OSU Friendly Name Duples */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800907 while (pos - pos2 >= 4 && prov->friendly_name_count < OSU_MAX_ITEMS) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800908 struct osu_lang_string *f;
Hai Shalom60840252021-02-19 19:02:11 -0800909 u8 slen;
910
911 slen = pos2[0];
912 if (1 + slen > pos - pos2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800913 wpa_printf(MSG_DEBUG, "Invalid OSU Friendly Name");
914 break;
915 }
Hai Shalom60840252021-02-19 19:02:11 -0800916 if (slen < 3) {
917 wpa_printf(MSG_DEBUG,
918 "Invalid OSU Friendly Name (no room for language)");
919 break;
920 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800921 f = &prov->friendly_name[prov->friendly_name_count++];
Hai Shalom60840252021-02-19 19:02:11 -0800922 pos2++;
923 os_memcpy(f->lang, pos2, 3);
924 pos2 += 3;
925 slen -= 3;
926 os_memcpy(f->text, pos2, slen);
927 pos2 += slen;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800928 }
929
930 /* OSU Server URI */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800931 if (end - pos < 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800932 wpa_printf(MSG_DEBUG,
933 "HS 2.0: Not enough room for OSU Server URI length");
934 return;
935 }
936 uri_len = *pos++;
937 if (uri_len > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800938 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Server "
939 "URI");
940 return;
941 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800942 os_memcpy(prov->server_uri, pos, uri_len);
943 pos += uri_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800944
945 /* OSU Method list */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800946 if (end - pos < 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800947 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
948 "list length");
949 return;
950 }
951 osu_method_len = pos[0];
952 if (osu_method_len > end - pos - 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800953 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
954 "list");
955 return;
956 }
957 pos2 = pos + 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800958 pos += 1 + osu_method_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800959 while (pos2 < pos) {
960 if (*pos2 < 32)
961 prov->osu_methods |= BIT(*pos2);
962 pos2++;
963 }
964
965 /* Icons Available Length */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800966 if (end - pos < 2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800967 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
968 "Available Length");
969 return;
970 }
971 len2 = WPA_GET_LE16(pos);
972 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800973 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800974 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
975 "Available");
976 return;
977 }
978 pos2 = pos;
979 pos += len2;
980
981 /* Icons Available */
982 while (pos2 < pos) {
983 struct osu_icon *icon = &prov->icon[prov->icon_count];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800984 u8 flen;
985
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800986 if (2 + 2 + 3 + 1 + 1 > pos - pos2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800987 wpa_printf(MSG_DEBUG, "HS 2.0: Invalid Icon Metadata");
988 break;
989 }
990
991 icon->width = WPA_GET_LE16(pos2);
992 pos2 += 2;
993 icon->height = WPA_GET_LE16(pos2);
994 pos2 += 2;
995 os_memcpy(icon->lang, pos2, 3);
996 pos2 += 3;
997
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800998 flen = *pos2++;
999 if (flen > pos - pos2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001000 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon Type");
1001 break;
1002 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001003 os_memcpy(icon->icon_type, pos2, flen);
1004 pos2 += flen;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001005
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001006 if (pos - pos2 < 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001007 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
1008 "Filename length");
1009 break;
1010 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001011 flen = *pos2++;
1012 if (flen > pos - pos2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001013 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
1014 "Filename");
1015 break;
1016 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001017 os_memcpy(icon->filename, pos2, flen);
1018 pos2 += flen;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001019
1020 prov->icon_count++;
1021 }
1022
1023 /* OSU_NAI */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001024 if (end - pos < 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001025 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
1026 return;
1027 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001028 osu_nai_len = *pos++;
1029 if (osu_nai_len > end - pos) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001030 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
1031 return;
1032 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001033 os_memcpy(prov->osu_nai, pos, osu_nai_len);
1034 pos += osu_nai_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001035
1036 /* OSU Service Description Length */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001037 if (end - pos < 2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001038 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
1039 "Service Description Length");
1040 return;
1041 }
1042 len2 = WPA_GET_LE16(pos);
1043 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001044 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001045 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
1046 "Service Description Duples");
1047 return;
1048 }
1049 pos2 = pos;
1050 pos += len2;
1051
1052 /* OSU Service Description Duples */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001053 while (pos - pos2 >= 4 && prov->serv_desc_count < OSU_MAX_ITEMS) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001054 struct osu_lang_string *f;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001055 u8 descr_len;
1056
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001057 descr_len = *pos2++;
1058 if (descr_len > pos - pos2 || descr_len < 3) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001059 wpa_printf(MSG_DEBUG, "Invalid OSU Service "
1060 "Description");
1061 break;
1062 }
1063 f = &prov->serv_desc[prov->serv_desc_count++];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001064 os_memcpy(f->lang, pos2, 3);
1065 os_memcpy(f->text, pos2 + 3, descr_len - 3);
1066 pos2 += descr_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001067 }
1068
1069 wpa_printf(MSG_DEBUG, "HS 2.0: Added OSU Provider through " MACSTR,
1070 MAC2STR(bss->bssid));
1071 wpa_s->osu_prov_count++;
1072}
1073
1074
1075void hs20_osu_icon_fetch(struct wpa_supplicant *wpa_s)
1076{
1077 struct wpa_bss *bss;
1078 struct wpabuf *prov_anqp;
1079 const u8 *pos, *end;
1080 u16 len;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001081 const u8 *osu_ssid, *osu_ssid2;
1082 u8 osu_ssid_len, osu_ssid2_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001083 u8 num_providers;
1084
1085 hs20_free_osu_prov(wpa_s);
1086
1087 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001088 struct wpa_ie_data data;
1089 const u8 *ie;
1090
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001091 if (bss->anqp == NULL)
1092 continue;
1093 prov_anqp = bss->anqp->hs20_osu_providers_list;
1094 if (prov_anqp == NULL)
1095 continue;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001096 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1097 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &data) == 0 &&
1098 (data.key_mgmt & WPA_KEY_MGMT_OSEN)) {
1099 osu_ssid2 = bss->ssid;
1100 osu_ssid2_len = bss->ssid_len;
1101 } else {
1102 osu_ssid2 = NULL;
1103 osu_ssid2_len = 0;
1104 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001105 wpa_printf(MSG_DEBUG, "HS 2.0: Parsing OSU Providers list from "
1106 MACSTR, MAC2STR(bss->bssid));
1107 wpa_hexdump_buf(MSG_DEBUG, "HS 2.0: OSU Providers list",
1108 prov_anqp);
1109 pos = wpabuf_head(prov_anqp);
1110 end = pos + wpabuf_len(prov_anqp);
1111
1112 /* OSU SSID */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001113 if (end - pos < 1)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001114 continue;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001115 if (1 + pos[0] > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001116 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
1117 "OSU SSID");
1118 continue;
1119 }
1120 osu_ssid_len = *pos++;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001121 if (osu_ssid_len > SSID_MAX_LEN) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001122 wpa_printf(MSG_DEBUG, "HS 2.0: Invalid OSU SSID "
1123 "Length %u", osu_ssid_len);
1124 continue;
1125 }
1126 osu_ssid = pos;
1127 pos += osu_ssid_len;
1128
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001129 if (end - pos < 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001130 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
1131 "Number of OSU Providers");
1132 continue;
1133 }
1134 num_providers = *pos++;
1135 wpa_printf(MSG_DEBUG, "HS 2.0: Number of OSU Providers: %u",
1136 num_providers);
1137
1138 /* OSU Providers */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001139 while (end - pos > 2 && num_providers > 0) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001140 num_providers--;
1141 len = WPA_GET_LE16(pos);
1142 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001143 if (len > (unsigned int) (end - pos))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001144 break;
1145 hs20_osu_add_prov(wpa_s, bss, osu_ssid,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001146 osu_ssid_len, osu_ssid2,
1147 osu_ssid2_len, pos, len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001148 pos += len;
1149 }
1150
1151 if (pos != end) {
1152 wpa_printf(MSG_DEBUG, "HS 2.0: Ignored %d bytes of "
1153 "extra data after OSU Providers",
1154 (int) (end - pos));
1155 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001156
1157 prov_anqp = bss->anqp->hs20_osu_providers_nai_list;
1158 if (!prov_anqp)
1159 continue;
1160 wpa_printf(MSG_DEBUG,
1161 "HS 2.0: Parsing OSU Providers NAI List from "
1162 MACSTR, MAC2STR(bss->bssid));
1163 wpa_hexdump_buf(MSG_DEBUG, "HS 2.0: OSU Providers NAI List",
1164 prov_anqp);
1165 pos = wpabuf_head(prov_anqp);
1166 end = pos + wpabuf_len(prov_anqp);
1167 num_providers = 0;
1168 while (end - pos > 0) {
1169 len = *pos++;
1170 if (end - pos < len) {
1171 wpa_printf(MSG_DEBUG,
1172 "HS 2.0: Not enough room for OSU_NAI");
1173 break;
1174 }
1175 if (num_providers >= wpa_s->osu_prov_count) {
1176 wpa_printf(MSG_DEBUG,
1177 "HS 2.0: Ignore unexpected OSU Provider NAI List entries");
1178 break;
1179 }
1180 os_memcpy(wpa_s->osu_prov[num_providers].osu_nai2,
1181 pos, len);
1182 pos += len;
1183 num_providers++;
1184 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001185 }
1186
1187 wpa_s->fetch_osu_icon_in_progress = 1;
1188 hs20_next_osu_icon(wpa_s);
1189}
1190
1191
1192static void hs20_osu_scan_res_handler(struct wpa_supplicant *wpa_s,
1193 struct wpa_scan_results *scan_res)
1194{
1195 wpa_printf(MSG_DEBUG, "OSU provisioning fetch scan completed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001196 if (!wpa_s->fetch_osu_waiting_scan) {
1197 wpa_printf(MSG_DEBUG, "OSU fetch have been canceled");
1198 return;
1199 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001200 wpa_s->network_select = 0;
1201 wpa_s->fetch_all_anqp = 1;
1202 wpa_s->fetch_osu_info = 1;
1203 wpa_s->fetch_osu_icon_in_progress = 0;
1204
1205 interworking_start_fetch_anqp(wpa_s);
1206}
1207
1208
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001209int hs20_fetch_osu(struct wpa_supplicant *wpa_s, int skip_scan)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001210{
1211 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1212 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1213 "interface disabled");
1214 return -1;
1215 }
1216
1217 if (wpa_s->scanning) {
1218 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1219 "scanning");
1220 return -1;
1221 }
1222
1223 if (wpa_s->conf->osu_dir == NULL) {
1224 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1225 "osu_dir not configured");
1226 return -1;
1227 }
1228
1229 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
1230 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1231 "fetch in progress (%d, %d)",
1232 wpa_s->fetch_anqp_in_progress,
1233 wpa_s->network_select);
1234 return -1;
1235 }
1236
1237 wpa_msg(wpa_s, MSG_INFO, "Starting OSU provisioning information fetch");
1238 wpa_s->num_osu_scans = 0;
1239 wpa_s->num_prov_found = 0;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001240 if (skip_scan) {
1241 wpa_s->network_select = 0;
1242 wpa_s->fetch_all_anqp = 1;
1243 wpa_s->fetch_osu_info = 1;
1244 wpa_s->fetch_osu_icon_in_progress = 0;
1245
1246 interworking_start_fetch_anqp(wpa_s);
1247 } else {
1248 hs20_start_osu_scan(wpa_s);
1249 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001250
1251 return 0;
1252}
1253
1254
1255void hs20_start_osu_scan(struct wpa_supplicant *wpa_s)
1256{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001257 wpa_s->fetch_osu_waiting_scan = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001258 wpa_s->num_osu_scans++;
1259 wpa_s->scan_req = MANUAL_SCAN_REQ;
1260 wpa_s->scan_res_handler = hs20_osu_scan_res_handler;
1261 wpa_supplicant_req_scan(wpa_s, 0, 0);
1262}
1263
1264
1265void hs20_cancel_fetch_osu(struct wpa_supplicant *wpa_s)
1266{
1267 wpa_printf(MSG_DEBUG, "Cancel OSU fetch");
1268 interworking_stop_fetch_anqp(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001269 wpa_s->fetch_osu_waiting_scan = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001270 wpa_s->network_select = 0;
1271 wpa_s->fetch_osu_info = 0;
1272 wpa_s->fetch_osu_icon_in_progress = 0;
1273}
1274
1275
1276void hs20_icon_fetch_failed(struct wpa_supplicant *wpa_s)
1277{
1278 hs20_osu_icon_fetch_result(wpa_s, -1);
1279 eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
1280 eloop_register_timeout(0, 0, hs20_continue_icon_fetch, wpa_s, NULL);
1281}
1282
1283
1284void hs20_rx_subscription_remediation(struct wpa_supplicant *wpa_s,
1285 const char *url, u8 osu_method)
1286{
1287 if (url)
1288 wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION "%u %s",
1289 osu_method, url);
1290 else
1291 wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION);
Roshan Pius04a9d742016-12-12 12:40:46 -08001292 wpas_notify_hs20_rx_subscription_remediation(wpa_s, url, osu_method);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001293}
1294
1295
1296void hs20_rx_deauth_imminent_notice(struct wpa_supplicant *wpa_s, u8 code,
1297 u16 reauth_delay, const char *url)
1298{
1299 if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
1300 wpa_printf(MSG_DEBUG, "HS 2.0: Ignore deauthentication imminent notice since PMF was not enabled");
1301 return;
1302 }
1303
1304 wpa_msg(wpa_s, MSG_INFO, HS20_DEAUTH_IMMINENT_NOTICE "%u %u %s",
1305 code, reauth_delay, url);
Roshan Pius04a9d742016-12-12 12:40:46 -08001306 wpas_notify_hs20_rx_deauth_imminent_notice(wpa_s, code, reauth_delay, url);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001307
1308 if (code == HS20_DEAUTH_REASON_CODE_BSS) {
Hai Shalom60840252021-02-19 19:02:11 -08001309 wpa_printf(MSG_DEBUG, "HS 2.0: Add BSS to ignore list");
1310 wpa_bssid_ignore_add(wpa_s, wpa_s->bssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001311 /* TODO: For now, disable full ESS since some drivers may not
1312 * support disabling per BSS. */
1313 if (wpa_s->current_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001314 struct os_reltime now;
1315 os_get_reltime(&now);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001316 if (now.sec + reauth_delay <=
1317 wpa_s->current_ssid->disabled_until.sec)
1318 return;
1319 wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds (BSS)",
1320 reauth_delay);
1321 wpa_s->current_ssid->disabled_until.sec =
1322 now.sec + reauth_delay;
1323 }
1324 }
1325
1326 if (code == HS20_DEAUTH_REASON_CODE_ESS && wpa_s->current_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001327 struct os_reltime now;
1328 os_get_reltime(&now);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001329 if (now.sec + reauth_delay <=
1330 wpa_s->current_ssid->disabled_until.sec)
1331 return;
1332 wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds",
1333 reauth_delay);
1334 wpa_s->current_ssid->disabled_until.sec =
1335 now.sec + reauth_delay;
1336 }
1337}
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001338
1339
Roshan Pius3a1667e2018-07-03 15:17:14 -07001340void hs20_rx_t_c_acceptance(struct wpa_supplicant *wpa_s, const char *url)
1341{
1342 if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
1343 wpa_printf(MSG_DEBUG,
1344 "HS 2.0: Ignore Terms and Conditions Acceptance since PMF was not enabled");
1345 return;
1346 }
1347
1348 wpa_msg(wpa_s, MSG_INFO, HS20_T_C_ACCEPTANCE "%s", url);
Hai Shalom04a4ca62020-10-28 19:04:47 -07001349 wpas_notify_hs20_rx_terms_and_conditions_acceptance(wpa_s, url);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001350}
1351
1352
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001353void hs20_init(struct wpa_supplicant *wpa_s)
1354{
1355 dl_list_init(&wpa_s->icon_head);
1356}
1357
1358
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001359void hs20_deinit(struct wpa_supplicant *wpa_s)
1360{
1361 eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
1362 hs20_free_osu_prov(wpa_s);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001363 if (wpa_s->icon_head.next)
1364 hs20_del_icon(wpa_s, NULL, NULL);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001365}