blob: eaf0803d38d36ab32b63d95868c7329be64785df [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
Hai Shalom2cbbcd12021-03-08 18:33:38 -080076 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 ) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -070082 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
Hai Shalom74f70d42019-02-11 14:42:39 -0800104 if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_PROXY_ARP))
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700105 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
Hai Shalom74f70d42019-02-11 14:42:39 -0800112void wpas_hs20_add_indication(struct wpabuf *buf, int pps_mo_id, int ap_release)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700113{
Hai Shalom74f70d42019-02-11 14:42:39 -0800114 int release;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800115 u8 conf;
116
Hai Shalom74f70d42019-02-11 14:42:39 -0800117 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
Dmitry Shmidt04949592012-07-19 12:16:46 -0700123 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800124 wpabuf_put_u8(buf, pps_mo_id >= 0 ? 7 : 5);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700125 wpabuf_put_be24(buf, OUI_WFA);
126 wpabuf_put_u8(buf, HS20_INDICATION_OUI_TYPE);
Hai Shalom74f70d42019-02-11 14:42:39 -0800127 conf = (release - 1) << 4;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800128 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);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700133}
134
135
Roshan Pius3a1667e2018-07-03 15:17:14 -0700136void 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
Hai Shalom74f70d42019-02-11 14:42:39 -0800152int 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
Hai Shalom2cbbcd12021-03-08 18:33:38 -0800166int is_hs20_config(struct wpa_supplicant *wpa_s)
167{
168 return wpa_s->conf->hs20;
169}
Hai Shalom74f70d42019-02-11 14:42:39 -0800170
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700171int is_hs20_network(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
172 struct wpa_bss *bss)
173{
Hai Shalom2cbbcd12021-03-08 18:33:38 -0800174 if (!ssid)
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700175 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
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800200int hs20_get_pps_mo_id(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
201{
202 struct wpa_cred *cred;
203
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700204 if (ssid == NULL)
205 return 0;
206
207 if (ssid->update_identifier)
208 return ssid->update_identifier;
209
210 if (ssid->parent_cred == NULL)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800211 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
Dmitry Shmidt15907092014-03-25 10:42:57 -0700222void hs20_put_anqp_req(u32 stypes, const u8 *payload, size_t payload_len,
223 struct wpabuf *buf)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700224{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700225 u8 *len_pos;
226
Dmitry Shmidt04949592012-07-19 12:16:46 -0700227 if (buf == NULL)
Dmitry Shmidt15907092014-03-25 10:42:57 -0700228 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700229
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);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800238 } 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);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700243 } 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);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700255}
256
257
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700258static struct wpabuf * hs20_build_anqp_req(u32 stypes, const u8 *payload,
259 size_t payload_len)
Dmitry Shmidt15907092014-03-25 10:42:57 -0700260{
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);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700268
269 return buf;
270}
271
272
273int hs20_anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, u32 stypes,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800274 const u8 *payload, size_t payload_len, int inmem)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700275{
276 struct wpabuf *buf;
277 int ret = 0;
278 int freq;
279 struct wpa_bss *bss;
280 int res;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800281 struct icon_entry *icon_entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700282
Dmitry Shmidt04949592012-07-19 12:16:46 -0700283 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -0700284 if (!bss) {
285 wpa_printf(MSG_WARNING,
286 "ANQP: Cannot send query to unknown BSS "
287 MACSTR, MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700288 return -1;
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -0700289 }
290
291 wpa_bss_anqp_unshare_alloc(bss);
292 freq = bss->freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700293
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
Hai Shalomb755a2a2020-04-23 21:49:02 -0700301 res = gas_query_req(wpa_s->gas, dst, freq, 0, 0, buf, anqp_resp_cb,
302 wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700303 if (res < 0) {
304 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700305 wpabuf_free(buf);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800306 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700307 } else
308 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
309 "%u", res);
310
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800311 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
Dmitry Shmidt04949592012-07-19 12:16:46 -0700328 return ret;
329}
330
331
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800332static 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
348int 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;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800354 char *b64;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800355 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);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700374 if (b64 && buf_len >= b64_size) {
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800375 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
385static 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
398int 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
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800427static 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
Hai Shalom74f70d42019-02-11 14:42:39 -0800445 if (lchown(fname, statbuf.st_uid, statbuf.st_gid) < 0) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800446 wpa_printf(MSG_WARNING, "Cannot change the ownership for %s",
447 fname);
448 }
449}
450
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800451
452static 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
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800470static int hs20_process_icon_binary_file(struct wpa_supplicant *wpa_s,
471 const u8 *sa, const u8 *pos,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800472 size_t slen, u8 dialog_token)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800473{
474 char fname[256];
475 int png;
476 FILE *f;
477 u16 data_len;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800478 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) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700483 icon->image = os_memdup(pos, slen);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800484 if (!icon->image)
485 return -1;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800486 icon->image_len = slen;
487 hs20_remove_duplicate_icons(wpa_s, icon);
488 wpa_msg(wpa_s, MSG_INFO,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700489 RX_HS20_ICON MACSTR " %s %u",
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800490 MAC2STR(sa), icon->file_name,
491 (unsigned int) icon->image_len);
Roshan Pius04a9d742016-12-12 12:40:46 -0800492 wpas_notify_hs20_icon_query_done(wpa_s, sa,
493 icon->file_name,
494 icon->image,
495 icon->image_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800496 return 0;
497 }
498 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800499
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700500 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR " Icon Binary File",
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800501 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;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800553
554 hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
555
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800556 if (fwrite(pos, slen, 1, f) != 1) {
557 fclose(f);
558 unlink(fname);
559 return -1;
560 }
561 fclose(f);
562
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700563 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP_ICON "%s", fname);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800564 return 0;
565}
566
567
568static 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
576static 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
Dmitry Shmidt04949592012-07-19 12:16:46 -0700604void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800605 struct wpa_bss *bss, const u8 *sa,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800606 const u8 *data, size_t slen, u8 dialog_token)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700607{
608 const u8 *pos = data;
609 u8 subtype;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700610 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800611 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700612
613 if (slen < 2)
614 return;
615
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700616 if (bss)
617 anqp = bss->anqp;
618
Dmitry Shmidt04949592012-07-19 12:16:46 -0700619 subtype = *pos++;
620 slen--;
621
622 pos++; /* Reserved */
623 slen--;
624
625 switch (subtype) {
626 case HS20_STYPE_CAPABILITY_LIST:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700627 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700628 " HS Capability List", MAC2STR(sa));
629 wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800630 if (anqp) {
631 wpabuf_free(anqp->hs20_capability_list);
632 anqp->hs20_capability_list =
633 wpabuf_alloc_copy(pos, slen);
634 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700635 break;
636 case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700637 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700638 " Operator Friendly Name", MAC2STR(sa));
639 wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700640 if (anqp) {
641 wpabuf_free(anqp->hs20_operator_friendly_name);
642 anqp->hs20_operator_friendly_name =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700643 wpabuf_alloc_copy(pos, slen);
644 }
645 break;
646 case HS20_STYPE_WAN_METRICS:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800647 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 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700653 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800654 " 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));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700657 if (anqp) {
658 wpabuf_free(anqp->hs20_wan_metrics);
659 anqp->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700660 }
661 break;
662 case HS20_STYPE_CONNECTION_CAPABILITY:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700663 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700664 " Connection Capability", MAC2STR(sa));
665 wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700666 if (anqp) {
667 wpabuf_free(anqp->hs20_connection_capability);
668 anqp->hs20_connection_capability =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700669 wpabuf_alloc_copy(pos, slen);
670 }
671 break;
672 case HS20_STYPE_OPERATING_CLASS:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700673 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700674 " Operating Class", MAC2STR(sa));
675 wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700676 if (anqp) {
677 wpabuf_free(anqp->hs20_operating_class);
678 anqp->hs20_operating_class =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700679 wpabuf_alloc_copy(pos, slen);
680 }
681 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800682 case HS20_STYPE_OSU_PROVIDERS_LIST:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700683 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800684 " 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:
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800693 ret = hs20_process_icon_binary_file(wpa_s, sa, pos, slen,
694 dialog_token);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800695 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;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700703 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;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800713 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;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700722 default:
723 wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
724 break;
725 }
726}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800727
728
729void 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
744static void hs20_free_osu_prov_entry(struct osu_provider *prov)
745{
746}
747
748
749void 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
760static 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) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700779 wpa_msg(wpa_s, MSG_INFO,
780 "Could not write OSU provider information");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800781 hs20_free_osu_prov(wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800782 wpa_s->fetch_anqp_in_progress = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800783 return;
784 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800785
786 hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
787
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800788 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 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800801 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 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800806 if (osu->osu_nai[0])
807 fprintf(f, "osu_nai=%s\n", osu->osu_nai);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800808 if (osu->osu_nai2[0])
809 fprintf(f, "osu_nai2=%s\n", osu->osu_nai2);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800810 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
837void 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,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800857 os_strlen(icon->filename),
858 0) < 0) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800859 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
871static void hs20_osu_add_prov(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
872 const u8 *osu_ssid, u8 osu_ssid_len,
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800873 const u8 *osu_ssid2, u8 osu_ssid2_len,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800874 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;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800880 u8 uri_len, osu_method_len, osu_nai_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800881
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;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800895 if (osu_ssid2)
896 os_memcpy(prov->osu_ssid2, osu_ssid2, osu_ssid2_len);
897 prov->osu_ssid2_len = osu_ssid2_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800898
899 /* OSU Friendly Name Length */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800900 if (end - pos < 2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800901 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;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800907 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800908 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 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800916 while (pos - pos2 >= 4 && prov->friendly_name_count < OSU_MAX_ITEMS) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800917 struct osu_lang_string *f;
Hai Shalom60840252021-02-19 19:02:11 -0800918 u8 slen;
919
920 slen = pos2[0];
921 if (1 + slen > pos - pos2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800922 wpa_printf(MSG_DEBUG, "Invalid OSU Friendly Name");
923 break;
924 }
Hai Shalom60840252021-02-19 19:02:11 -0800925 if (slen < 3) {
926 wpa_printf(MSG_DEBUG,
927 "Invalid OSU Friendly Name (no room for language)");
928 break;
929 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800930 f = &prov->friendly_name[prov->friendly_name_count++];
Hai Shalom60840252021-02-19 19:02:11 -0800931 pos2++;
932 os_memcpy(f->lang, pos2, 3);
933 pos2 += 3;
934 slen -= 3;
935 os_memcpy(f->text, pos2, slen);
936 pos2 += slen;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800937 }
938
939 /* OSU Server URI */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800940 if (end - pos < 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800941 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) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800947 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Server "
948 "URI");
949 return;
950 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800951 os_memcpy(prov->server_uri, pos, uri_len);
952 pos += uri_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800953
954 /* OSU Method list */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800955 if (end - pos < 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800956 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) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800962 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
963 "list");
964 return;
965 }
966 pos2 = pos + 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800967 pos += 1 + osu_method_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800968 while (pos2 < pos) {
969 if (*pos2 < 32)
970 prov->osu_methods |= BIT(*pos2);
971 pos2++;
972 }
973
974 /* Icons Available Length */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800975 if (end - pos < 2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800976 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;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800982 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800983 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];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800993 u8 flen;
994
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800995 if (2 + 2 + 3 + 1 + 1 > pos - pos2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800996 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
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001007 flen = *pos2++;
1008 if (flen > pos - pos2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001009 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon Type");
1010 break;
1011 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001012 os_memcpy(icon->icon_type, pos2, flen);
1013 pos2 += flen;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001014
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001015 if (pos - pos2 < 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001016 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
1017 "Filename length");
1018 break;
1019 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001020 flen = *pos2++;
1021 if (flen > pos - pos2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001022 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
1023 "Filename");
1024 break;
1025 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001026 os_memcpy(icon->filename, pos2, flen);
1027 pos2 += flen;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001028
1029 prov->icon_count++;
1030 }
1031
1032 /* OSU_NAI */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001033 if (end - pos < 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001034 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
1035 return;
1036 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001037 osu_nai_len = *pos++;
1038 if (osu_nai_len > end - pos) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001039 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
1040 return;
1041 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001042 os_memcpy(prov->osu_nai, pos, osu_nai_len);
1043 pos += osu_nai_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001044
1045 /* OSU Service Description Length */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001046 if (end - pos < 2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001047 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;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001053 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001054 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 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001062 while (pos - pos2 >= 4 && prov->serv_desc_count < OSU_MAX_ITEMS) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001063 struct osu_lang_string *f;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001064 u8 descr_len;
1065
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001066 descr_len = *pos2++;
1067 if (descr_len > pos - pos2 || descr_len < 3) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001068 wpa_printf(MSG_DEBUG, "Invalid OSU Service "
1069 "Description");
1070 break;
1071 }
1072 f = &prov->serv_desc[prov->serv_desc_count++];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001073 os_memcpy(f->lang, pos2, 3);
1074 os_memcpy(f->text, pos2 + 3, descr_len - 3);
1075 pos2 += descr_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001076 }
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
1084void 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;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001090 const u8 *osu_ssid, *osu_ssid2;
1091 u8 osu_ssid_len, osu_ssid2_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001092 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) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001097 struct wpa_ie_data data;
1098 const u8 *ie;
1099
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001100 if (bss->anqp == NULL)
1101 continue;
1102 prov_anqp = bss->anqp->hs20_osu_providers_list;
1103 if (prov_anqp == NULL)
1104 continue;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001105 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 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001114 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 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001122 if (end - pos < 1)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001123 continue;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001124 if (1 + pos[0] > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001125 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
1126 "OSU SSID");
1127 continue;
1128 }
1129 osu_ssid_len = *pos++;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001130 if (osu_ssid_len > SSID_MAX_LEN) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001131 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
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001138 if (end - pos < 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001139 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 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001148 while (end - pos > 2 && num_providers > 0) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001149 num_providers--;
1150 len = WPA_GET_LE16(pos);
1151 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001152 if (len > (unsigned int) (end - pos))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001153 break;
1154 hs20_osu_add_prov(wpa_s, bss, osu_ssid,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001155 osu_ssid_len, osu_ssid2,
1156 osu_ssid2_len, pos, len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001157 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 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001165
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 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001194 }
1195
1196 wpa_s->fetch_osu_icon_in_progress = 1;
1197 hs20_next_osu_icon(wpa_s);
1198}
1199
1200
1201static 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");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001205 if (!wpa_s->fetch_osu_waiting_scan) {
1206 wpa_printf(MSG_DEBUG, "OSU fetch have been canceled");
1207 return;
1208 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001209 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
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001218int hs20_fetch_osu(struct wpa_supplicant *wpa_s, int skip_scan)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001219{
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;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001249 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 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001259
1260 return 0;
1261}
1262
1263
1264void hs20_start_osu_scan(struct wpa_supplicant *wpa_s)
1265{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001266 wpa_s->fetch_osu_waiting_scan = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001267 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
1274void 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);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001278 wpa_s->fetch_osu_waiting_scan = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001279 wpa_s->network_select = 0;
1280 wpa_s->fetch_osu_info = 0;
1281 wpa_s->fetch_osu_icon_in_progress = 0;
1282}
1283
1284
1285void 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
1293void 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);
Roshan Pius04a9d742016-12-12 12:40:46 -08001301 wpas_notify_hs20_rx_subscription_remediation(wpa_s, url, osu_method);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001302}
1303
1304
1305void 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);
Roshan Pius04a9d742016-12-12 12:40:46 -08001315 wpas_notify_hs20_rx_deauth_imminent_notice(wpa_s, code, reauth_delay, url);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001316
1317 if (code == HS20_DEAUTH_REASON_CODE_BSS) {
Hai Shalom60840252021-02-19 19:02:11 -08001318 wpa_printf(MSG_DEBUG, "HS 2.0: Add BSS to ignore list");
1319 wpa_bssid_ignore_add(wpa_s, wpa_s->bssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001320 /* TODO: For now, disable full ESS since some drivers may not
1321 * support disabling per BSS. */
1322 if (wpa_s->current_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001323 struct os_reltime now;
1324 os_get_reltime(&now);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001325 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) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001336 struct os_reltime now;
1337 os_get_reltime(&now);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001338 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}
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001347
1348
Roshan Pius3a1667e2018-07-03 15:17:14 -07001349void 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);
Hai Shalom04a4ca62020-10-28 19:04:47 -07001358 wpas_notify_hs20_rx_terms_and_conditions_acceptance(wpa_s, url);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001359}
1360
1361
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001362void hs20_init(struct wpa_supplicant *wpa_s)
1363{
1364 dl_list_init(&wpa_s->icon_head);
1365}
1366
1367
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001368void 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);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001372 if (wpa_s->icon_head.next)
1373 hs20_del_icon(wpa_s, NULL, NULL);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001374}