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