blob: 814d18e129cb6d37892d176bc159217b026afbcb [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"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080025#include "blacklist.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 Shalombf6e0ba2019-02-11 12:01:50 -080099 /* Check if Proxy ARP is enabled (2nd byte in the IE) */
100 if (ext_capa[3] & BIT(4))
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700101 filter |= WPA_DATA_FRAME_FILTER_FLAG_ARP |
102 WPA_DATA_FRAME_FILTER_FLAG_NA;
103
104 wpa_drv_configure_frame_filters(wpa_s, filter);
105}
106
107
Hai Shalombf6e0ba2019-02-11 12:01:50 -0800108void wpas_hs20_add_indication(struct wpabuf *buf, int pps_mo_id)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700109{
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800110 u8 conf;
111
Dmitry Shmidt04949592012-07-19 12:16:46 -0700112 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800113 wpabuf_put_u8(buf, pps_mo_id >= 0 ? 7 : 5);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700114 wpabuf_put_be24(buf, OUI_WFA);
115 wpabuf_put_u8(buf, HS20_INDICATION_OUI_TYPE);
Hai Shalombf6e0ba2019-02-11 12:01:50 -0800116 conf = HS20_VERSION;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800117 if (pps_mo_id >= 0)
118 conf |= HS20_PPS_MO_ID_PRESENT;
119 wpabuf_put_u8(buf, conf);
120 if (pps_mo_id >= 0)
121 wpabuf_put_le16(buf, pps_mo_id);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700122}
123
124
Roshan Pius3a1667e2018-07-03 15:17:14 -0700125void wpas_hs20_add_roam_cons_sel(struct wpabuf *buf,
126 const struct wpa_ssid *ssid)
127{
128 if (!ssid->roaming_consortium_selection ||
129 !ssid->roaming_consortium_selection_len)
130 return;
131
132 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
133 wpabuf_put_u8(buf, 4 + ssid->roaming_consortium_selection_len);
134 wpabuf_put_be24(buf, OUI_WFA);
135 wpabuf_put_u8(buf, HS20_ROAMING_CONS_SEL_OUI_TYPE);
136 wpabuf_put_data(buf, ssid->roaming_consortium_selection,
137 ssid->roaming_consortium_selection_len);
138}
139
140
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700141int is_hs20_network(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
142 struct wpa_bss *bss)
143{
144 if (!wpa_s->conf->hs20 || !ssid)
145 return 0;
146
147 if (ssid->parent_cred)
148 return 1;
149
150 if (bss && !wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE))
151 return 0;
152
153 /*
154 * This may catch some non-Hotspot 2.0 cases, but it is safer to do that
155 * than cause Hotspot 2.0 connections without indication element getting
156 * added. Non-Hotspot 2.0 APs should ignore the unknown vendor element.
157 */
158
159 if (!(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X))
160 return 0;
161 if (!(ssid->pairwise_cipher & WPA_CIPHER_CCMP))
162 return 0;
163 if (ssid->proto != WPA_PROTO_RSN)
164 return 0;
165
166 return 1;
167}
168
169
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800170int hs20_get_pps_mo_id(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
171{
172 struct wpa_cred *cred;
173
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700174 if (ssid == NULL)
175 return 0;
176
177 if (ssid->update_identifier)
178 return ssid->update_identifier;
179
180 if (ssid->parent_cred == NULL)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800181 return 0;
182
183 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
184 if (ssid->parent_cred == cred)
185 return cred->update_identifier;
186 }
187
188 return 0;
189}
190
191
Dmitry Shmidt15907092014-03-25 10:42:57 -0700192void hs20_put_anqp_req(u32 stypes, const u8 *payload, size_t payload_len,
193 struct wpabuf *buf)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700194{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700195 u8 *len_pos;
196
Dmitry Shmidt04949592012-07-19 12:16:46 -0700197 if (buf == NULL)
Dmitry Shmidt15907092014-03-25 10:42:57 -0700198 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700199
200 len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
201 wpabuf_put_be24(buf, OUI_WFA);
202 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
203 if (stypes == BIT(HS20_STYPE_NAI_HOME_REALM_QUERY)) {
204 wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
205 wpabuf_put_u8(buf, 0); /* Reserved */
206 if (payload)
207 wpabuf_put_data(buf, payload, payload_len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800208 } else if (stypes == BIT(HS20_STYPE_ICON_REQUEST)) {
209 wpabuf_put_u8(buf, HS20_STYPE_ICON_REQUEST);
210 wpabuf_put_u8(buf, 0); /* Reserved */
211 if (payload)
212 wpabuf_put_data(buf, payload, payload_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700213 } else {
214 u8 i;
215 wpabuf_put_u8(buf, HS20_STYPE_QUERY_LIST);
216 wpabuf_put_u8(buf, 0); /* Reserved */
217 for (i = 0; i < 32; i++) {
218 if (stypes & BIT(i))
219 wpabuf_put_u8(buf, i);
220 }
221 }
222 gas_anqp_set_element_len(buf, len_pos);
223
224 gas_anqp_set_len(buf);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700225}
226
227
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700228static struct wpabuf * hs20_build_anqp_req(u32 stypes, const u8 *payload,
229 size_t payload_len)
Dmitry Shmidt15907092014-03-25 10:42:57 -0700230{
231 struct wpabuf *buf;
232
233 buf = gas_anqp_build_initial_req(0, 100 + payload_len);
234 if (buf == NULL)
235 return NULL;
236
237 hs20_put_anqp_req(stypes, payload, payload_len, buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700238
239 return buf;
240}
241
242
243int hs20_anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, u32 stypes,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800244 const u8 *payload, size_t payload_len, int inmem)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700245{
246 struct wpabuf *buf;
247 int ret = 0;
248 int freq;
249 struct wpa_bss *bss;
250 int res;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800251 struct icon_entry *icon_entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700252
Dmitry Shmidt04949592012-07-19 12:16:46 -0700253 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -0700254 if (!bss) {
255 wpa_printf(MSG_WARNING,
256 "ANQP: Cannot send query to unknown BSS "
257 MACSTR, MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700258 return -1;
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -0700259 }
260
261 wpa_bss_anqp_unshare_alloc(bss);
262 freq = bss->freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700263
264 wpa_printf(MSG_DEBUG, "HS20: ANQP Query Request to " MACSTR " for "
265 "subtypes 0x%x", MAC2STR(dst), stypes);
266
267 buf = hs20_build_anqp_req(stypes, payload, payload_len);
268 if (buf == NULL)
269 return -1;
270
Roshan Pius3a1667e2018-07-03 15:17:14 -0700271 res = gas_query_req(wpa_s->gas, dst, freq, 0, buf, anqp_resp_cb, wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700272 if (res < 0) {
273 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700274 wpabuf_free(buf);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800275 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700276 } else
277 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
278 "%u", res);
279
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800280 if (inmem) {
281 icon_entry = os_zalloc(sizeof(struct icon_entry));
282 if (!icon_entry)
283 return -1;
284 os_memcpy(icon_entry->bssid, dst, ETH_ALEN);
285 icon_entry->file_name = os_malloc(payload_len + 1);
286 if (!icon_entry->file_name) {
287 os_free(icon_entry);
288 return -1;
289 }
290 os_memcpy(icon_entry->file_name, payload, payload_len);
291 icon_entry->file_name[payload_len] = '\0';
292 icon_entry->dialog_token = res;
293
294 dl_list_add(&wpa_s->icon_head, &icon_entry->list);
295 }
296
Dmitry Shmidt04949592012-07-19 12:16:46 -0700297 return ret;
298}
299
300
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800301static struct icon_entry * hs20_find_icon(struct wpa_supplicant *wpa_s,
302 const u8 *bssid,
303 const char *file_name)
304{
305 struct icon_entry *icon;
306
307 dl_list_for_each(icon, &wpa_s->icon_head, struct icon_entry, list) {
308 if (os_memcmp(icon->bssid, bssid, ETH_ALEN) == 0 &&
309 os_strcmp(icon->file_name, file_name) == 0 && icon->image)
310 return icon;
311 }
312
313 return NULL;
314}
315
316
317int hs20_get_icon(struct wpa_supplicant *wpa_s, const u8 *bssid,
318 const char *file_name, size_t offset, size_t size,
319 char *reply, size_t buf_len)
320{
321 struct icon_entry *icon;
322 size_t out_size;
323 unsigned char *b64;
324 size_t b64_size;
325 int reply_size;
326
327 wpa_printf(MSG_DEBUG, "HS20: Get icon " MACSTR " %s @ %u +%u (%u)",
328 MAC2STR(bssid), file_name, (unsigned int) offset,
329 (unsigned int) size, (unsigned int) buf_len);
330
331 icon = hs20_find_icon(wpa_s, bssid, file_name);
332 if (!icon || !icon->image || offset >= icon->image_len)
333 return -1;
334 if (size > icon->image_len - offset)
335 size = icon->image_len - offset;
336 out_size = buf_len - 3 /* max base64 padding */;
337 if (size * 4 > out_size * 3)
338 size = out_size * 3 / 4;
339 if (size == 0)
340 return -1;
341
342 b64 = base64_encode(&icon->image[offset], size, &b64_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700343 if (b64 && buf_len >= b64_size) {
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800344 os_memcpy(reply, b64, b64_size);
345 reply_size = b64_size;
346 } else {
347 reply_size = -1;
348 }
349 os_free(b64);
350 return reply_size;
351}
352
353
354static void hs20_free_icon_entry(struct icon_entry *icon)
355{
356 wpa_printf(MSG_DEBUG, "HS20: Free stored icon from " MACSTR
357 " dialog_token=%u file_name=%s image_len=%u",
358 MAC2STR(icon->bssid), icon->dialog_token,
359 icon->file_name ? icon->file_name : "N/A",
360 (unsigned int) icon->image_len);
361 os_free(icon->file_name);
362 os_free(icon->image);
363 os_free(icon);
364}
365
366
367int hs20_del_icon(struct wpa_supplicant *wpa_s, const u8 *bssid,
368 const char *file_name)
369{
370 struct icon_entry *icon, *tmp;
371 int count = 0;
372
373 if (!bssid)
374 wpa_printf(MSG_DEBUG, "HS20: Delete all stored icons");
375 else if (!file_name)
376 wpa_printf(MSG_DEBUG, "HS20: Delete all stored icons for "
377 MACSTR, MAC2STR(bssid));
378 else
379 wpa_printf(MSG_DEBUG, "HS20: Delete stored icons for "
380 MACSTR " file name %s", MAC2STR(bssid), file_name);
381
382 dl_list_for_each_safe(icon, tmp, &wpa_s->icon_head, struct icon_entry,
383 list) {
384 if ((!bssid || os_memcmp(icon->bssid, bssid, ETH_ALEN) == 0) &&
385 (!file_name ||
386 os_strcmp(icon->file_name, file_name) == 0)) {
387 dl_list_del(&icon->list);
388 hs20_free_icon_entry(icon);
389 count++;
390 }
391 }
392 return count == 0 ? -1 : 0;
393}
394
395
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800396static void hs20_set_osu_access_permission(const char *osu_dir,
397 const char *fname)
398{
399 struct stat statbuf;
400
401 /* Get OSU directory information */
402 if (stat(osu_dir, &statbuf) < 0) {
403 wpa_printf(MSG_WARNING, "Cannot stat the OSU directory %s",
404 osu_dir);
405 return;
406 }
407
408 if (chmod(fname, statbuf.st_mode) < 0) {
409 wpa_printf(MSG_WARNING,
410 "Cannot change the permissions for %s", fname);
411 return;
412 }
413
Hai Shalombf6e0ba2019-02-11 12:01:50 -0800414 if (chown(fname, statbuf.st_uid, statbuf.st_gid) < 0) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800415 wpa_printf(MSG_WARNING, "Cannot change the ownership for %s",
416 fname);
417 }
418}
419
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800420
421static void hs20_remove_duplicate_icons(struct wpa_supplicant *wpa_s,
422 struct icon_entry *new_icon)
423{
424 struct icon_entry *icon, *tmp;
425
426 dl_list_for_each_safe(icon, tmp, &wpa_s->icon_head, struct icon_entry,
427 list) {
428 if (icon == new_icon)
429 continue;
430 if (os_memcmp(icon->bssid, new_icon->bssid, ETH_ALEN) == 0 &&
431 os_strcmp(icon->file_name, new_icon->file_name) == 0) {
432 dl_list_del(&icon->list);
433 hs20_free_icon_entry(icon);
434 }
435 }
436}
437
438
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800439static int hs20_process_icon_binary_file(struct wpa_supplicant *wpa_s,
440 const u8 *sa, const u8 *pos,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800441 size_t slen, u8 dialog_token)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800442{
443 char fname[256];
444 int png;
445 FILE *f;
446 u16 data_len;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800447 struct icon_entry *icon;
448
449 dl_list_for_each(icon, &wpa_s->icon_head, struct icon_entry, list) {
450 if (icon->dialog_token == dialog_token && !icon->image &&
451 os_memcmp(icon->bssid, sa, ETH_ALEN) == 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700452 icon->image = os_memdup(pos, slen);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800453 if (!icon->image)
454 return -1;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800455 icon->image_len = slen;
456 hs20_remove_duplicate_icons(wpa_s, icon);
457 wpa_msg(wpa_s, MSG_INFO,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700458 RX_HS20_ICON MACSTR " %s %u",
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800459 MAC2STR(sa), icon->file_name,
460 (unsigned int) icon->image_len);
Roshan Pius04a9d742016-12-12 12:40:46 -0800461 wpas_notify_hs20_icon_query_done(wpa_s, sa,
462 icon->file_name,
463 icon->image,
464 icon->image_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800465 return 0;
466 }
467 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800468
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700469 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR " Icon Binary File",
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800470 MAC2STR(sa));
471
472 if (slen < 4) {
473 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
474 "value from " MACSTR, MAC2STR(sa));
475 return -1;
476 }
477
478 wpa_printf(MSG_DEBUG, "HS 2.0: Download Status Code %u", *pos);
479 if (*pos != 0)
480 return -1;
481 pos++;
482 slen--;
483
484 if ((size_t) 1 + pos[0] > slen) {
485 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
486 "value from " MACSTR, MAC2STR(sa));
487 return -1;
488 }
489 wpa_hexdump_ascii(MSG_DEBUG, "Icon Type", pos + 1, pos[0]);
490 png = os_strncasecmp((char *) pos + 1, "image/png", 9) == 0;
491 slen -= 1 + pos[0];
492 pos += 1 + pos[0];
493
494 if (slen < 2) {
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 data_len = WPA_GET_LE16(pos);
500 pos += 2;
501 slen -= 2;
502
503 if (data_len > slen) {
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, "Icon Binary Data: %u bytes", data_len);
510 if (wpa_s->conf->osu_dir == NULL)
511 return -1;
512
513 wpa_s->osu_icon_id++;
514 if (wpa_s->osu_icon_id == 0)
515 wpa_s->osu_icon_id++;
516 snprintf(fname, sizeof(fname), "%s/osu-icon-%u.%s",
517 wpa_s->conf->osu_dir, wpa_s->osu_icon_id,
518 png ? "png" : "icon");
519 f = fopen(fname, "wb");
520 if (f == NULL)
521 return -1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800522
523 hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
524
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800525 if (fwrite(pos, slen, 1, f) != 1) {
526 fclose(f);
527 unlink(fname);
528 return -1;
529 }
530 fclose(f);
531
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700532 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP_ICON "%s", fname);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800533 return 0;
534}
535
536
537static void hs20_continue_icon_fetch(void *eloop_ctx, void *sock_ctx)
538{
539 struct wpa_supplicant *wpa_s = eloop_ctx;
540 if (wpa_s->fetch_osu_icon_in_progress)
541 hs20_next_osu_icon(wpa_s);
542}
543
544
545static void hs20_osu_icon_fetch_result(struct wpa_supplicant *wpa_s, int res)
546{
547 size_t i, j;
548 struct os_reltime now, tmp;
549 int dur;
550
551 os_get_reltime(&now);
552 os_reltime_sub(&now, &wpa_s->osu_icon_fetch_start, &tmp);
553 dur = tmp.sec * 1000 + tmp.usec / 1000;
554 wpa_printf(MSG_DEBUG, "HS 2.0: Icon fetch dur=%d ms res=%d",
555 dur, res);
556
557 for (i = 0; i < wpa_s->osu_prov_count; i++) {
558 struct osu_provider *osu = &wpa_s->osu_prov[i];
559 for (j = 0; j < osu->icon_count; j++) {
560 struct osu_icon *icon = &osu->icon[j];
561 if (icon->id || icon->failed)
562 continue;
563 if (res < 0)
564 icon->failed = 1;
565 else
566 icon->id = wpa_s->osu_icon_id;
567 return;
568 }
569 }
570}
571
572
Dmitry Shmidt04949592012-07-19 12:16:46 -0700573void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800574 struct wpa_bss *bss, const u8 *sa,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800575 const u8 *data, size_t slen, u8 dialog_token)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700576{
577 const u8 *pos = data;
578 u8 subtype;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700579 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800580 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700581
582 if (slen < 2)
583 return;
584
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700585 if (bss)
586 anqp = bss->anqp;
587
Dmitry Shmidt04949592012-07-19 12:16:46 -0700588 subtype = *pos++;
589 slen--;
590
591 pos++; /* Reserved */
592 slen--;
593
594 switch (subtype) {
595 case HS20_STYPE_CAPABILITY_LIST:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700596 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700597 " HS Capability List", MAC2STR(sa));
598 wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800599 if (anqp) {
600 wpabuf_free(anqp->hs20_capability_list);
601 anqp->hs20_capability_list =
602 wpabuf_alloc_copy(pos, slen);
603 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700604 break;
605 case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700606 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700607 " Operator Friendly Name", MAC2STR(sa));
608 wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700609 if (anqp) {
610 wpabuf_free(anqp->hs20_operator_friendly_name);
611 anqp->hs20_operator_friendly_name =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700612 wpabuf_alloc_copy(pos, slen);
613 }
614 break;
615 case HS20_STYPE_WAN_METRICS:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800616 wpa_hexdump(MSG_DEBUG, "WAN Metrics", pos, slen);
617 if (slen < 13) {
618 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short WAN "
619 "Metrics value from " MACSTR, MAC2STR(sa));
620 break;
621 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700622 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800623 " WAN Metrics %02x:%u:%u:%u:%u:%u", MAC2STR(sa),
624 pos[0], WPA_GET_LE32(pos + 1), WPA_GET_LE32(pos + 5),
625 pos[9], pos[10], WPA_GET_LE16(pos + 11));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700626 if (anqp) {
627 wpabuf_free(anqp->hs20_wan_metrics);
628 anqp->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700629 }
630 break;
631 case HS20_STYPE_CONNECTION_CAPABILITY:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700632 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700633 " Connection Capability", MAC2STR(sa));
634 wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700635 if (anqp) {
636 wpabuf_free(anqp->hs20_connection_capability);
637 anqp->hs20_connection_capability =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700638 wpabuf_alloc_copy(pos, slen);
639 }
640 break;
641 case HS20_STYPE_OPERATING_CLASS:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700642 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700643 " Operating Class", MAC2STR(sa));
644 wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700645 if (anqp) {
646 wpabuf_free(anqp->hs20_operating_class);
647 anqp->hs20_operating_class =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700648 wpabuf_alloc_copy(pos, slen);
649 }
650 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800651 case HS20_STYPE_OSU_PROVIDERS_LIST:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700652 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800653 " OSU Providers list", MAC2STR(sa));
654 wpa_s->num_prov_found++;
655 if (anqp) {
656 wpabuf_free(anqp->hs20_osu_providers_list);
657 anqp->hs20_osu_providers_list =
658 wpabuf_alloc_copy(pos, slen);
659 }
660 break;
661 case HS20_STYPE_ICON_BINARY_FILE:
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800662 ret = hs20_process_icon_binary_file(wpa_s, sa, pos, slen,
663 dialog_token);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800664 if (wpa_s->fetch_osu_icon_in_progress) {
665 hs20_osu_icon_fetch_result(wpa_s, ret);
666 eloop_cancel_timeout(hs20_continue_icon_fetch,
667 wpa_s, NULL);
668 eloop_register_timeout(0, 0, hs20_continue_icon_fetch,
669 wpa_s, NULL);
670 }
671 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700672 case HS20_STYPE_OPERATOR_ICON_METADATA:
673 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
674 " Operator Icon Metadata", MAC2STR(sa));
675 wpa_hexdump(MSG_DEBUG, "Operator Icon Metadata", pos, slen);
676 if (anqp) {
677 wpabuf_free(anqp->hs20_operator_icon_metadata);
678 anqp->hs20_operator_icon_metadata =
679 wpabuf_alloc_copy(pos, slen);
680 }
681 break;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800682 case HS20_STYPE_OSU_PROVIDERS_NAI_LIST:
683 wpa_msg(wpa_s, MSG_INFO, RX_HS20_ANQP MACSTR
684 " OSU Providers NAI List", MAC2STR(sa));
685 if (anqp) {
686 wpabuf_free(anqp->hs20_osu_providers_nai_list);
687 anqp->hs20_osu_providers_nai_list =
688 wpabuf_alloc_copy(pos, slen);
689 }
690 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700691 default:
692 wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
693 break;
694 }
695}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800696
697
698void hs20_notify_parse_done(struct wpa_supplicant *wpa_s)
699{
700 if (!wpa_s->fetch_osu_icon_in_progress)
701 return;
702 if (eloop_is_timeout_registered(hs20_continue_icon_fetch, wpa_s, NULL))
703 return;
704 /*
705 * We are going through icon fetch, but no icon response was received.
706 * Assume this means the current AP could not provide an answer to avoid
707 * getting stuck in fetch iteration.
708 */
709 hs20_icon_fetch_failed(wpa_s);
710}
711
712
713static void hs20_free_osu_prov_entry(struct osu_provider *prov)
714{
715}
716
717
718void hs20_free_osu_prov(struct wpa_supplicant *wpa_s)
719{
720 size_t i;
721 for (i = 0; i < wpa_s->osu_prov_count; i++)
722 hs20_free_osu_prov_entry(&wpa_s->osu_prov[i]);
723 os_free(wpa_s->osu_prov);
724 wpa_s->osu_prov = NULL;
725 wpa_s->osu_prov_count = 0;
726}
727
728
729static void hs20_osu_fetch_done(struct wpa_supplicant *wpa_s)
730{
731 char fname[256];
732 FILE *f;
733 size_t i, j;
734
735 wpa_s->fetch_osu_info = 0;
736 wpa_s->fetch_osu_icon_in_progress = 0;
737
738 if (wpa_s->conf->osu_dir == NULL) {
739 hs20_free_osu_prov(wpa_s);
740 wpa_s->fetch_anqp_in_progress = 0;
741 return;
742 }
743
744 snprintf(fname, sizeof(fname), "%s/osu-providers.txt",
745 wpa_s->conf->osu_dir);
746 f = fopen(fname, "w");
747 if (f == NULL) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700748 wpa_msg(wpa_s, MSG_INFO,
749 "Could not write OSU provider information");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800750 hs20_free_osu_prov(wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800751 wpa_s->fetch_anqp_in_progress = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800752 return;
753 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800754
755 hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
756
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800757 for (i = 0; i < wpa_s->osu_prov_count; i++) {
758 struct osu_provider *osu = &wpa_s->osu_prov[i];
759 if (i > 0)
760 fprintf(f, "\n");
761 fprintf(f, "OSU-PROVIDER " MACSTR "\n"
762 "uri=%s\n"
763 "methods=%08x\n",
764 MAC2STR(osu->bssid), osu->server_uri, osu->osu_methods);
765 if (osu->osu_ssid_len) {
766 fprintf(f, "osu_ssid=%s\n",
767 wpa_ssid_txt(osu->osu_ssid,
768 osu->osu_ssid_len));
769 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800770 if (osu->osu_ssid2_len) {
771 fprintf(f, "osu_ssid2=%s\n",
772 wpa_ssid_txt(osu->osu_ssid2,
773 osu->osu_ssid2_len));
774 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800775 if (osu->osu_nai[0])
776 fprintf(f, "osu_nai=%s\n", osu->osu_nai);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800777 if (osu->osu_nai2[0])
778 fprintf(f, "osu_nai2=%s\n", osu->osu_nai2);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800779 for (j = 0; j < osu->friendly_name_count; j++) {
780 fprintf(f, "friendly_name=%s:%s\n",
781 osu->friendly_name[j].lang,
782 osu->friendly_name[j].text);
783 }
784 for (j = 0; j < osu->serv_desc_count; j++) {
785 fprintf(f, "desc=%s:%s\n",
786 osu->serv_desc[j].lang,
787 osu->serv_desc[j].text);
788 }
789 for (j = 0; j < osu->icon_count; j++) {
790 struct osu_icon *icon = &osu->icon[j];
791 if (icon->failed)
792 continue; /* could not fetch icon */
793 fprintf(f, "icon=%u:%u:%u:%s:%s:%s\n",
794 icon->id, icon->width, icon->height, icon->lang,
795 icon->icon_type, icon->filename);
796 }
797 }
798 fclose(f);
799 hs20_free_osu_prov(wpa_s);
800
801 wpa_msg(wpa_s, MSG_INFO, "OSU provider fetch completed");
802 wpa_s->fetch_anqp_in_progress = 0;
803}
804
805
806void hs20_next_osu_icon(struct wpa_supplicant *wpa_s)
807{
808 size_t i, j;
809
810 wpa_printf(MSG_DEBUG, "HS 2.0: Ready to fetch next icon");
811
812 for (i = 0; i < wpa_s->osu_prov_count; i++) {
813 struct osu_provider *osu = &wpa_s->osu_prov[i];
814 for (j = 0; j < osu->icon_count; j++) {
815 struct osu_icon *icon = &osu->icon[j];
816 if (icon->id || icon->failed)
817 continue;
818
819 wpa_printf(MSG_DEBUG, "HS 2.0: Try to fetch icon '%s' "
820 "from " MACSTR, icon->filename,
821 MAC2STR(osu->bssid));
822 os_get_reltime(&wpa_s->osu_icon_fetch_start);
823 if (hs20_anqp_send_req(wpa_s, osu->bssid,
824 BIT(HS20_STYPE_ICON_REQUEST),
825 (u8 *) icon->filename,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800826 os_strlen(icon->filename),
827 0) < 0) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800828 icon->failed = 1;
829 continue;
830 }
831 return;
832 }
833 }
834
835 wpa_printf(MSG_DEBUG, "HS 2.0: No more icons to fetch");
836 hs20_osu_fetch_done(wpa_s);
837}
838
839
840static void hs20_osu_add_prov(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
841 const u8 *osu_ssid, u8 osu_ssid_len,
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800842 const u8 *osu_ssid2, u8 osu_ssid2_len,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800843 const u8 *pos, size_t len)
844{
845 struct osu_provider *prov;
846 const u8 *end = pos + len;
847 u16 len2;
848 const u8 *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800849 u8 uri_len, osu_method_len, osu_nai_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800850
851 wpa_hexdump(MSG_DEBUG, "HS 2.0: Parsing OSU Provider", pos, len);
852 prov = os_realloc_array(wpa_s->osu_prov,
853 wpa_s->osu_prov_count + 1,
854 sizeof(*prov));
855 if (prov == NULL)
856 return;
857 wpa_s->osu_prov = prov;
858 prov = &prov[wpa_s->osu_prov_count];
859 os_memset(prov, 0, sizeof(*prov));
860
861 os_memcpy(prov->bssid, bss->bssid, ETH_ALEN);
862 os_memcpy(prov->osu_ssid, osu_ssid, osu_ssid_len);
863 prov->osu_ssid_len = osu_ssid_len;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800864 if (osu_ssid2)
865 os_memcpy(prov->osu_ssid2, osu_ssid2, osu_ssid2_len);
866 prov->osu_ssid2_len = osu_ssid2_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800867
868 /* OSU Friendly Name Length */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800869 if (end - pos < 2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800870 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
871 "Friendly Name Length");
872 return;
873 }
874 len2 = WPA_GET_LE16(pos);
875 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800876 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800877 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
878 "Friendly Name Duples");
879 return;
880 }
881 pos2 = pos;
882 pos += len2;
883
884 /* OSU Friendly Name Duples */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800885 while (pos - pos2 >= 4 && prov->friendly_name_count < OSU_MAX_ITEMS) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800886 struct osu_lang_string *f;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800887 if (1 + pos2[0] > pos - pos2 || pos2[0] < 3) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800888 wpa_printf(MSG_DEBUG, "Invalid OSU Friendly Name");
889 break;
890 }
891 f = &prov->friendly_name[prov->friendly_name_count++];
892 os_memcpy(f->lang, pos2 + 1, 3);
893 os_memcpy(f->text, pos2 + 1 + 3, pos2[0] - 3);
894 pos2 += 1 + pos2[0];
895 }
896
897 /* OSU Server URI */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800898 if (end - pos < 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800899 wpa_printf(MSG_DEBUG,
900 "HS 2.0: Not enough room for OSU Server URI length");
901 return;
902 }
903 uri_len = *pos++;
904 if (uri_len > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800905 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Server "
906 "URI");
907 return;
908 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800909 os_memcpy(prov->server_uri, pos, uri_len);
910 pos += uri_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800911
912 /* OSU Method list */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800913 if (end - pos < 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800914 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
915 "list length");
916 return;
917 }
918 osu_method_len = pos[0];
919 if (osu_method_len > end - pos - 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800920 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
921 "list");
922 return;
923 }
924 pos2 = pos + 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800925 pos += 1 + osu_method_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800926 while (pos2 < pos) {
927 if (*pos2 < 32)
928 prov->osu_methods |= BIT(*pos2);
929 pos2++;
930 }
931
932 /* Icons Available Length */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800933 if (end - pos < 2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800934 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
935 "Available Length");
936 return;
937 }
938 len2 = WPA_GET_LE16(pos);
939 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800940 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800941 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
942 "Available");
943 return;
944 }
945 pos2 = pos;
946 pos += len2;
947
948 /* Icons Available */
949 while (pos2 < pos) {
950 struct osu_icon *icon = &prov->icon[prov->icon_count];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800951 u8 flen;
952
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800953 if (2 + 2 + 3 + 1 + 1 > pos - pos2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800954 wpa_printf(MSG_DEBUG, "HS 2.0: Invalid Icon Metadata");
955 break;
956 }
957
958 icon->width = WPA_GET_LE16(pos2);
959 pos2 += 2;
960 icon->height = WPA_GET_LE16(pos2);
961 pos2 += 2;
962 os_memcpy(icon->lang, pos2, 3);
963 pos2 += 3;
964
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800965 flen = *pos2++;
966 if (flen > pos - pos2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800967 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon Type");
968 break;
969 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800970 os_memcpy(icon->icon_type, pos2, flen);
971 pos2 += flen;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800972
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800973 if (pos - pos2 < 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800974 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
975 "Filename length");
976 break;
977 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800978 flen = *pos2++;
979 if (flen > pos - pos2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800980 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
981 "Filename");
982 break;
983 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800984 os_memcpy(icon->filename, pos2, flen);
985 pos2 += flen;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800986
987 prov->icon_count++;
988 }
989
990 /* OSU_NAI */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800991 if (end - pos < 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800992 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
993 return;
994 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800995 osu_nai_len = *pos++;
996 if (osu_nai_len > end - pos) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800997 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
998 return;
999 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001000 os_memcpy(prov->osu_nai, pos, osu_nai_len);
1001 pos += osu_nai_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001002
1003 /* OSU Service Description Length */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001004 if (end - pos < 2) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001005 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
1006 "Service Description Length");
1007 return;
1008 }
1009 len2 = WPA_GET_LE16(pos);
1010 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001011 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001012 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
1013 "Service Description Duples");
1014 return;
1015 }
1016 pos2 = pos;
1017 pos += len2;
1018
1019 /* OSU Service Description Duples */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001020 while (pos - pos2 >= 4 && prov->serv_desc_count < OSU_MAX_ITEMS) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001021 struct osu_lang_string *f;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001022 u8 descr_len;
1023
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001024 descr_len = *pos2++;
1025 if (descr_len > pos - pos2 || descr_len < 3) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001026 wpa_printf(MSG_DEBUG, "Invalid OSU Service "
1027 "Description");
1028 break;
1029 }
1030 f = &prov->serv_desc[prov->serv_desc_count++];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001031 os_memcpy(f->lang, pos2, 3);
1032 os_memcpy(f->text, pos2 + 3, descr_len - 3);
1033 pos2 += descr_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001034 }
1035
1036 wpa_printf(MSG_DEBUG, "HS 2.0: Added OSU Provider through " MACSTR,
1037 MAC2STR(bss->bssid));
1038 wpa_s->osu_prov_count++;
1039}
1040
1041
1042void hs20_osu_icon_fetch(struct wpa_supplicant *wpa_s)
1043{
1044 struct wpa_bss *bss;
1045 struct wpabuf *prov_anqp;
1046 const u8 *pos, *end;
1047 u16 len;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001048 const u8 *osu_ssid, *osu_ssid2;
1049 u8 osu_ssid_len, osu_ssid2_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001050 u8 num_providers;
1051
1052 hs20_free_osu_prov(wpa_s);
1053
1054 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001055 struct wpa_ie_data data;
1056 const u8 *ie;
1057
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001058 if (bss->anqp == NULL)
1059 continue;
1060 prov_anqp = bss->anqp->hs20_osu_providers_list;
1061 if (prov_anqp == NULL)
1062 continue;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001063 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1064 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &data) == 0 &&
1065 (data.key_mgmt & WPA_KEY_MGMT_OSEN)) {
1066 osu_ssid2 = bss->ssid;
1067 osu_ssid2_len = bss->ssid_len;
1068 } else {
1069 osu_ssid2 = NULL;
1070 osu_ssid2_len = 0;
1071 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001072 wpa_printf(MSG_DEBUG, "HS 2.0: Parsing OSU Providers list from "
1073 MACSTR, MAC2STR(bss->bssid));
1074 wpa_hexdump_buf(MSG_DEBUG, "HS 2.0: OSU Providers list",
1075 prov_anqp);
1076 pos = wpabuf_head(prov_anqp);
1077 end = pos + wpabuf_len(prov_anqp);
1078
1079 /* OSU SSID */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001080 if (end - pos < 1)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001081 continue;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001082 if (1 + pos[0] > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001083 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
1084 "OSU SSID");
1085 continue;
1086 }
1087 osu_ssid_len = *pos++;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001088 if (osu_ssid_len > SSID_MAX_LEN) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001089 wpa_printf(MSG_DEBUG, "HS 2.0: Invalid OSU SSID "
1090 "Length %u", osu_ssid_len);
1091 continue;
1092 }
1093 osu_ssid = pos;
1094 pos += osu_ssid_len;
1095
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001096 if (end - pos < 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001097 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
1098 "Number of OSU Providers");
1099 continue;
1100 }
1101 num_providers = *pos++;
1102 wpa_printf(MSG_DEBUG, "HS 2.0: Number of OSU Providers: %u",
1103 num_providers);
1104
1105 /* OSU Providers */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001106 while (end - pos > 2 && num_providers > 0) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001107 num_providers--;
1108 len = WPA_GET_LE16(pos);
1109 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001110 if (len > (unsigned int) (end - pos))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001111 break;
1112 hs20_osu_add_prov(wpa_s, bss, osu_ssid,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001113 osu_ssid_len, osu_ssid2,
1114 osu_ssid2_len, pos, len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001115 pos += len;
1116 }
1117
1118 if (pos != end) {
1119 wpa_printf(MSG_DEBUG, "HS 2.0: Ignored %d bytes of "
1120 "extra data after OSU Providers",
1121 (int) (end - pos));
1122 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001123
1124 prov_anqp = bss->anqp->hs20_osu_providers_nai_list;
1125 if (!prov_anqp)
1126 continue;
1127 wpa_printf(MSG_DEBUG,
1128 "HS 2.0: Parsing OSU Providers NAI List from "
1129 MACSTR, MAC2STR(bss->bssid));
1130 wpa_hexdump_buf(MSG_DEBUG, "HS 2.0: OSU Providers NAI List",
1131 prov_anqp);
1132 pos = wpabuf_head(prov_anqp);
1133 end = pos + wpabuf_len(prov_anqp);
1134 num_providers = 0;
1135 while (end - pos > 0) {
1136 len = *pos++;
1137 if (end - pos < len) {
1138 wpa_printf(MSG_DEBUG,
1139 "HS 2.0: Not enough room for OSU_NAI");
1140 break;
1141 }
1142 if (num_providers >= wpa_s->osu_prov_count) {
1143 wpa_printf(MSG_DEBUG,
1144 "HS 2.0: Ignore unexpected OSU Provider NAI List entries");
1145 break;
1146 }
1147 os_memcpy(wpa_s->osu_prov[num_providers].osu_nai2,
1148 pos, len);
1149 pos += len;
1150 num_providers++;
1151 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001152 }
1153
1154 wpa_s->fetch_osu_icon_in_progress = 1;
1155 hs20_next_osu_icon(wpa_s);
1156}
1157
1158
1159static void hs20_osu_scan_res_handler(struct wpa_supplicant *wpa_s,
1160 struct wpa_scan_results *scan_res)
1161{
1162 wpa_printf(MSG_DEBUG, "OSU provisioning fetch scan completed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001163 if (!wpa_s->fetch_osu_waiting_scan) {
1164 wpa_printf(MSG_DEBUG, "OSU fetch have been canceled");
1165 return;
1166 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001167 wpa_s->network_select = 0;
1168 wpa_s->fetch_all_anqp = 1;
1169 wpa_s->fetch_osu_info = 1;
1170 wpa_s->fetch_osu_icon_in_progress = 0;
1171
1172 interworking_start_fetch_anqp(wpa_s);
1173}
1174
1175
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001176int hs20_fetch_osu(struct wpa_supplicant *wpa_s, int skip_scan)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001177{
1178 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1179 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1180 "interface disabled");
1181 return -1;
1182 }
1183
1184 if (wpa_s->scanning) {
1185 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1186 "scanning");
1187 return -1;
1188 }
1189
1190 if (wpa_s->conf->osu_dir == NULL) {
1191 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1192 "osu_dir not configured");
1193 return -1;
1194 }
1195
1196 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
1197 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1198 "fetch in progress (%d, %d)",
1199 wpa_s->fetch_anqp_in_progress,
1200 wpa_s->network_select);
1201 return -1;
1202 }
1203
1204 wpa_msg(wpa_s, MSG_INFO, "Starting OSU provisioning information fetch");
1205 wpa_s->num_osu_scans = 0;
1206 wpa_s->num_prov_found = 0;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001207 if (skip_scan) {
1208 wpa_s->network_select = 0;
1209 wpa_s->fetch_all_anqp = 1;
1210 wpa_s->fetch_osu_info = 1;
1211 wpa_s->fetch_osu_icon_in_progress = 0;
1212
1213 interworking_start_fetch_anqp(wpa_s);
1214 } else {
1215 hs20_start_osu_scan(wpa_s);
1216 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001217
1218 return 0;
1219}
1220
1221
1222void hs20_start_osu_scan(struct wpa_supplicant *wpa_s)
1223{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001224 wpa_s->fetch_osu_waiting_scan = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001225 wpa_s->num_osu_scans++;
1226 wpa_s->scan_req = MANUAL_SCAN_REQ;
1227 wpa_s->scan_res_handler = hs20_osu_scan_res_handler;
1228 wpa_supplicant_req_scan(wpa_s, 0, 0);
1229}
1230
1231
1232void hs20_cancel_fetch_osu(struct wpa_supplicant *wpa_s)
1233{
1234 wpa_printf(MSG_DEBUG, "Cancel OSU fetch");
1235 interworking_stop_fetch_anqp(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001236 wpa_s->fetch_osu_waiting_scan = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001237 wpa_s->network_select = 0;
1238 wpa_s->fetch_osu_info = 0;
1239 wpa_s->fetch_osu_icon_in_progress = 0;
1240}
1241
1242
1243void hs20_icon_fetch_failed(struct wpa_supplicant *wpa_s)
1244{
1245 hs20_osu_icon_fetch_result(wpa_s, -1);
1246 eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
1247 eloop_register_timeout(0, 0, hs20_continue_icon_fetch, wpa_s, NULL);
1248}
1249
1250
1251void hs20_rx_subscription_remediation(struct wpa_supplicant *wpa_s,
1252 const char *url, u8 osu_method)
1253{
1254 if (url)
1255 wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION "%u %s",
1256 osu_method, url);
1257 else
1258 wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION);
Roshan Pius04a9d742016-12-12 12:40:46 -08001259 wpas_notify_hs20_rx_subscription_remediation(wpa_s, url, osu_method);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001260}
1261
1262
1263void hs20_rx_deauth_imminent_notice(struct wpa_supplicant *wpa_s, u8 code,
1264 u16 reauth_delay, const char *url)
1265{
1266 if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
1267 wpa_printf(MSG_DEBUG, "HS 2.0: Ignore deauthentication imminent notice since PMF was not enabled");
1268 return;
1269 }
1270
1271 wpa_msg(wpa_s, MSG_INFO, HS20_DEAUTH_IMMINENT_NOTICE "%u %u %s",
1272 code, reauth_delay, url);
Roshan Pius04a9d742016-12-12 12:40:46 -08001273 wpas_notify_hs20_rx_deauth_imminent_notice(wpa_s, code, reauth_delay, url);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001274
1275 if (code == HS20_DEAUTH_REASON_CODE_BSS) {
1276 wpa_printf(MSG_DEBUG, "HS 2.0: Add BSS to blacklist");
1277 wpa_blacklist_add(wpa_s, wpa_s->bssid);
1278 /* TODO: For now, disable full ESS since some drivers may not
1279 * support disabling per BSS. */
1280 if (wpa_s->current_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001281 struct os_reltime now;
1282 os_get_reltime(&now);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001283 if (now.sec + reauth_delay <=
1284 wpa_s->current_ssid->disabled_until.sec)
1285 return;
1286 wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds (BSS)",
1287 reauth_delay);
1288 wpa_s->current_ssid->disabled_until.sec =
1289 now.sec + reauth_delay;
1290 }
1291 }
1292
1293 if (code == HS20_DEAUTH_REASON_CODE_ESS && wpa_s->current_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001294 struct os_reltime now;
1295 os_get_reltime(&now);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001296 if (now.sec + reauth_delay <=
1297 wpa_s->current_ssid->disabled_until.sec)
1298 return;
1299 wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds",
1300 reauth_delay);
1301 wpa_s->current_ssid->disabled_until.sec =
1302 now.sec + reauth_delay;
1303 }
1304}
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001305
1306
Roshan Pius3a1667e2018-07-03 15:17:14 -07001307void hs20_rx_t_c_acceptance(struct wpa_supplicant *wpa_s, const char *url)
1308{
1309 if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
1310 wpa_printf(MSG_DEBUG,
1311 "HS 2.0: Ignore Terms and Conditions Acceptance since PMF was not enabled");
1312 return;
1313 }
1314
1315 wpa_msg(wpa_s, MSG_INFO, HS20_T_C_ACCEPTANCE "%s", url);
1316}
1317
1318
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001319void hs20_init(struct wpa_supplicant *wpa_s)
1320{
1321 dl_list_init(&wpa_s->icon_head);
1322}
1323
1324
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001325void hs20_deinit(struct wpa_supplicant *wpa_s)
1326{
1327 eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
1328 hs20_free_osu_prov(wpa_s);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001329 if (wpa_s->icon_head.next)
1330 hs20_del_icon(wpa_s, NULL, NULL);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07001331}