HS 2.0: Convert icon storage to use dl_list
[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 static int hs20_process_icon_binary_file(struct wpa_supplicant *wpa_s,
361                                          const u8 *sa, const u8 *pos,
362                                          size_t slen, u8 dialog_token)
363 {
364         char fname[256];
365         int png;
366         FILE *f;
367         u16 data_len;
368         struct icon_entry *icon;
369
370         dl_list_for_each(icon, &wpa_s->icon_head, struct icon_entry, list) {
371                 if (icon->dialog_token == dialog_token && !icon->image &&
372                     os_memcmp(icon->bssid, sa, ETH_ALEN) == 0) {
373                         icon->image = os_malloc(slen);
374                         if (!icon->image)
375                                 return -1;
376                         os_memcpy(icon->image, pos, slen);
377                         icon->image_len = slen;
378                         wpa_msg(wpa_s, MSG_INFO,
379                                 "RX-HS20-ICON " MACSTR " %s %u",
380                                 MAC2STR(sa), icon->file_name,
381                                 (unsigned int) icon->image_len);
382                         return 0;
383                 }
384         }
385
386         wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR " Icon Binary File",
387                 MAC2STR(sa));
388
389         if (slen < 4) {
390                 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
391                         "value from " MACSTR, MAC2STR(sa));
392                 return -1;
393         }
394
395         wpa_printf(MSG_DEBUG, "HS 2.0: Download Status Code %u", *pos);
396         if (*pos != 0)
397                 return -1;
398         pos++;
399         slen--;
400
401         if ((size_t) 1 + pos[0] > slen) {
402                 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
403                         "value from " MACSTR, MAC2STR(sa));
404                 return -1;
405         }
406         wpa_hexdump_ascii(MSG_DEBUG, "Icon Type", pos + 1, pos[0]);
407         png = os_strncasecmp((char *) pos + 1, "image/png", 9) == 0;
408         slen -= 1 + pos[0];
409         pos += 1 + pos[0];
410
411         if (slen < 2) {
412                 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
413                         "value from " MACSTR, MAC2STR(sa));
414                 return -1;
415         }
416         data_len = WPA_GET_LE16(pos);
417         pos += 2;
418         slen -= 2;
419
420         if (data_len > slen) {
421                 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
422                         "value from " MACSTR, MAC2STR(sa));
423                 return -1;
424         }
425
426         wpa_printf(MSG_DEBUG, "Icon Binary Data: %u bytes", data_len);
427         if (wpa_s->conf->osu_dir == NULL)
428                 return -1;
429
430         wpa_s->osu_icon_id++;
431         if (wpa_s->osu_icon_id == 0)
432                 wpa_s->osu_icon_id++;
433         snprintf(fname, sizeof(fname), "%s/osu-icon-%u.%s",
434                  wpa_s->conf->osu_dir, wpa_s->osu_icon_id,
435                  png ? "png" : "icon");
436         f = fopen(fname, "wb");
437         if (f == NULL)
438                 return -1;
439
440         hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
441
442         if (fwrite(pos, slen, 1, f) != 1) {
443                 fclose(f);
444                 unlink(fname);
445                 return -1;
446         }
447         fclose(f);
448
449         wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP-ICON %s", fname);
450         return 0;
451 }
452
453
454 static void hs20_continue_icon_fetch(void *eloop_ctx, void *sock_ctx)
455 {
456         struct wpa_supplicant *wpa_s = eloop_ctx;
457         if (wpa_s->fetch_osu_icon_in_progress)
458                 hs20_next_osu_icon(wpa_s);
459 }
460
461
462 static void hs20_osu_icon_fetch_result(struct wpa_supplicant *wpa_s, int res)
463 {
464         size_t i, j;
465         struct os_reltime now, tmp;
466         int dur;
467
468         os_get_reltime(&now);
469         os_reltime_sub(&now, &wpa_s->osu_icon_fetch_start, &tmp);
470         dur = tmp.sec * 1000 + tmp.usec / 1000;
471         wpa_printf(MSG_DEBUG, "HS 2.0: Icon fetch dur=%d ms res=%d",
472                    dur, res);
473
474         for (i = 0; i < wpa_s->osu_prov_count; i++) {
475                 struct osu_provider *osu = &wpa_s->osu_prov[i];
476                 for (j = 0; j < osu->icon_count; j++) {
477                         struct osu_icon *icon = &osu->icon[j];
478                         if (icon->id || icon->failed)
479                                 continue;
480                         if (res < 0)
481                                 icon->failed = 1;
482                         else
483                                 icon->id = wpa_s->osu_icon_id;
484                         return;
485                 }
486         }
487 }
488
489
490 void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
491                                   struct wpa_bss *bss, const u8 *sa,
492                                   const u8 *data, size_t slen, u8 dialog_token)
493 {
494         const u8 *pos = data;
495         u8 subtype;
496         struct wpa_bss_anqp *anqp = NULL;
497         int ret;
498
499         if (slen < 2)
500                 return;
501
502         if (bss)
503                 anqp = bss->anqp;
504
505         subtype = *pos++;
506         slen--;
507
508         pos++; /* Reserved */
509         slen--;
510
511         switch (subtype) {
512         case HS20_STYPE_CAPABILITY_LIST:
513                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
514                         " HS Capability List", MAC2STR(sa));
515                 wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
516                 if (anqp) {
517                         wpabuf_free(anqp->hs20_capability_list);
518                         anqp->hs20_capability_list =
519                                 wpabuf_alloc_copy(pos, slen);
520                 }
521                 break;
522         case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
523                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
524                         " Operator Friendly Name", MAC2STR(sa));
525                 wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
526                 if (anqp) {
527                         wpabuf_free(anqp->hs20_operator_friendly_name);
528                         anqp->hs20_operator_friendly_name =
529                                 wpabuf_alloc_copy(pos, slen);
530                 }
531                 break;
532         case HS20_STYPE_WAN_METRICS:
533                 wpa_hexdump(MSG_DEBUG, "WAN Metrics", pos, slen);
534                 if (slen < 13) {
535                         wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short WAN "
536                                 "Metrics value from " MACSTR, MAC2STR(sa));
537                         break;
538                 }
539                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
540                         " WAN Metrics %02x:%u:%u:%u:%u:%u", MAC2STR(sa),
541                         pos[0], WPA_GET_LE32(pos + 1), WPA_GET_LE32(pos + 5),
542                         pos[9], pos[10], WPA_GET_LE16(pos + 11));
543                 if (anqp) {
544                         wpabuf_free(anqp->hs20_wan_metrics);
545                         anqp->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
546                 }
547                 break;
548         case HS20_STYPE_CONNECTION_CAPABILITY:
549                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
550                         " Connection Capability", MAC2STR(sa));
551                 wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
552                 if (anqp) {
553                         wpabuf_free(anqp->hs20_connection_capability);
554                         anqp->hs20_connection_capability =
555                                 wpabuf_alloc_copy(pos, slen);
556                 }
557                 break;
558         case HS20_STYPE_OPERATING_CLASS:
559                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
560                         " Operating Class", MAC2STR(sa));
561                 wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
562                 if (anqp) {
563                         wpabuf_free(anqp->hs20_operating_class);
564                         anqp->hs20_operating_class =
565                                 wpabuf_alloc_copy(pos, slen);
566                 }
567                 break;
568         case HS20_STYPE_OSU_PROVIDERS_LIST:
569                 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
570                         " OSU Providers list", MAC2STR(sa));
571                 wpa_s->num_prov_found++;
572                 if (anqp) {
573                         wpabuf_free(anqp->hs20_osu_providers_list);
574                         anqp->hs20_osu_providers_list =
575                                 wpabuf_alloc_copy(pos, slen);
576                 }
577                 break;
578         case HS20_STYPE_ICON_BINARY_FILE:
579                 ret = hs20_process_icon_binary_file(wpa_s, sa, pos, slen,
580                                                     dialog_token);
581                 if (wpa_s->fetch_osu_icon_in_progress) {
582                         hs20_osu_icon_fetch_result(wpa_s, ret);
583                         eloop_cancel_timeout(hs20_continue_icon_fetch,
584                                              wpa_s, NULL);
585                         eloop_register_timeout(0, 0, hs20_continue_icon_fetch,
586                                                wpa_s, NULL);
587                 }
588                 break;
589         default:
590                 wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
591                 break;
592         }
593 }
594
595
596 void hs20_notify_parse_done(struct wpa_supplicant *wpa_s)
597 {
598         if (!wpa_s->fetch_osu_icon_in_progress)
599                 return;
600         if (eloop_is_timeout_registered(hs20_continue_icon_fetch, wpa_s, NULL))
601                 return;
602         /*
603          * We are going through icon fetch, but no icon response was received.
604          * Assume this means the current AP could not provide an answer to avoid
605          * getting stuck in fetch iteration.
606          */
607         hs20_icon_fetch_failed(wpa_s);
608 }
609
610
611 static void hs20_free_osu_prov_entry(struct osu_provider *prov)
612 {
613 }
614
615
616 void hs20_free_osu_prov(struct wpa_supplicant *wpa_s)
617 {
618         size_t i;
619         for (i = 0; i < wpa_s->osu_prov_count; i++)
620                 hs20_free_osu_prov_entry(&wpa_s->osu_prov[i]);
621         os_free(wpa_s->osu_prov);
622         wpa_s->osu_prov = NULL;
623         wpa_s->osu_prov_count = 0;
624 }
625
626
627 static void hs20_osu_fetch_done(struct wpa_supplicant *wpa_s)
628 {
629         char fname[256];
630         FILE *f;
631         size_t i, j;
632
633         wpa_s->fetch_osu_info = 0;
634         wpa_s->fetch_osu_icon_in_progress = 0;
635
636         if (wpa_s->conf->osu_dir == NULL) {
637                 hs20_free_osu_prov(wpa_s);
638                 wpa_s->fetch_anqp_in_progress = 0;
639                 return;
640         }
641
642         snprintf(fname, sizeof(fname), "%s/osu-providers.txt",
643                  wpa_s->conf->osu_dir);
644         f = fopen(fname, "w");
645         if (f == NULL) {
646                 hs20_free_osu_prov(wpa_s);
647                 return;
648         }
649
650         hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
651
652         for (i = 0; i < wpa_s->osu_prov_count; i++) {
653                 struct osu_provider *osu = &wpa_s->osu_prov[i];
654                 if (i > 0)
655                         fprintf(f, "\n");
656                 fprintf(f, "OSU-PROVIDER " MACSTR "\n"
657                         "uri=%s\n"
658                         "methods=%08x\n",
659                         MAC2STR(osu->bssid), osu->server_uri, osu->osu_methods);
660                 if (osu->osu_ssid_len) {
661                         fprintf(f, "osu_ssid=%s\n",
662                                 wpa_ssid_txt(osu->osu_ssid,
663                                              osu->osu_ssid_len));
664                 }
665                 if (osu->osu_nai[0])
666                         fprintf(f, "osu_nai=%s\n", osu->osu_nai);
667                 for (j = 0; j < osu->friendly_name_count; j++) {
668                         fprintf(f, "friendly_name=%s:%s\n",
669                                 osu->friendly_name[j].lang,
670                                 osu->friendly_name[j].text);
671                 }
672                 for (j = 0; j < osu->serv_desc_count; j++) {
673                         fprintf(f, "desc=%s:%s\n",
674                                 osu->serv_desc[j].lang,
675                                 osu->serv_desc[j].text);
676                 }
677                 for (j = 0; j < osu->icon_count; j++) {
678                         struct osu_icon *icon = &osu->icon[j];
679                         if (icon->failed)
680                                 continue; /* could not fetch icon */
681                         fprintf(f, "icon=%u:%u:%u:%s:%s:%s\n",
682                                 icon->id, icon->width, icon->height, icon->lang,
683                                 icon->icon_type, icon->filename);
684                 }
685         }
686         fclose(f);
687         hs20_free_osu_prov(wpa_s);
688
689         wpa_msg(wpa_s, MSG_INFO, "OSU provider fetch completed");
690         wpa_s->fetch_anqp_in_progress = 0;
691 }
692
693
694 void hs20_next_osu_icon(struct wpa_supplicant *wpa_s)
695 {
696         size_t i, j;
697
698         wpa_printf(MSG_DEBUG, "HS 2.0: Ready to fetch next icon");
699
700         for (i = 0; i < wpa_s->osu_prov_count; i++) {
701                 struct osu_provider *osu = &wpa_s->osu_prov[i];
702                 for (j = 0; j < osu->icon_count; j++) {
703                         struct osu_icon *icon = &osu->icon[j];
704                         if (icon->id || icon->failed)
705                                 continue;
706
707                         wpa_printf(MSG_DEBUG, "HS 2.0: Try to fetch icon '%s' "
708                                    "from " MACSTR, icon->filename,
709                                    MAC2STR(osu->bssid));
710                         os_get_reltime(&wpa_s->osu_icon_fetch_start);
711                         if (hs20_anqp_send_req(wpa_s, osu->bssid,
712                                                BIT(HS20_STYPE_ICON_REQUEST),
713                                                (u8 *) icon->filename,
714                                                os_strlen(icon->filename),
715                                                0) < 0) {
716                                 icon->failed = 1;
717                                 continue;
718                         }
719                         return;
720                 }
721         }
722
723         wpa_printf(MSG_DEBUG, "HS 2.0: No more icons to fetch");
724         hs20_osu_fetch_done(wpa_s);
725 }
726
727
728 static void hs20_osu_add_prov(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
729                               const u8 *osu_ssid, u8 osu_ssid_len,
730                               const u8 *pos, size_t len)
731 {
732         struct osu_provider *prov;
733         const u8 *end = pos + len;
734         u16 len2;
735         const u8 *pos2;
736         u8 uri_len, osu_method_len, osu_nai_len;
737
738         wpa_hexdump(MSG_DEBUG, "HS 2.0: Parsing OSU Provider", pos, len);
739         prov = os_realloc_array(wpa_s->osu_prov,
740                                 wpa_s->osu_prov_count + 1,
741                                 sizeof(*prov));
742         if (prov == NULL)
743                 return;
744         wpa_s->osu_prov = prov;
745         prov = &prov[wpa_s->osu_prov_count];
746         os_memset(prov, 0, sizeof(*prov));
747
748         os_memcpy(prov->bssid, bss->bssid, ETH_ALEN);
749         os_memcpy(prov->osu_ssid, osu_ssid, osu_ssid_len);
750         prov->osu_ssid_len = osu_ssid_len;
751
752         /* OSU Friendly Name Length */
753         if (end - pos < 2) {
754                 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
755                            "Friendly Name Length");
756                 return;
757         }
758         len2 = WPA_GET_LE16(pos);
759         pos += 2;
760         if (len2 > end - pos) {
761                 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
762                            "Friendly Name Duples");
763                 return;
764         }
765         pos2 = pos;
766         pos += len2;
767
768         /* OSU Friendly Name Duples */
769         while (pos - pos2 >= 4 && prov->friendly_name_count < OSU_MAX_ITEMS) {
770                 struct osu_lang_string *f;
771                 if (1 + pos2[0] > pos - pos2 || pos2[0] < 3) {
772                         wpa_printf(MSG_DEBUG, "Invalid OSU Friendly Name");
773                         break;
774                 }
775                 f = &prov->friendly_name[prov->friendly_name_count++];
776                 os_memcpy(f->lang, pos2 + 1, 3);
777                 os_memcpy(f->text, pos2 + 1 + 3, pos2[0] - 3);
778                 pos2 += 1 + pos2[0];
779         }
780
781         /* OSU Server URI */
782         if (end - pos < 1) {
783                 wpa_printf(MSG_DEBUG,
784                            "HS 2.0: Not enough room for OSU Server URI length");
785                 return;
786         }
787         uri_len = *pos++;
788         if (uri_len > end - pos) {
789                 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Server "
790                            "URI");
791                 return;
792         }
793         os_memcpy(prov->server_uri, pos, uri_len);
794         pos += uri_len;
795
796         /* OSU Method list */
797         if (end - pos < 1) {
798                 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
799                            "list length");
800                 return;
801         }
802         osu_method_len = pos[0];
803         if (osu_method_len > end - pos - 1) {
804                 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
805                            "list");
806                 return;
807         }
808         pos2 = pos + 1;
809         pos += 1 + osu_method_len;
810         while (pos2 < pos) {
811                 if (*pos2 < 32)
812                         prov->osu_methods |= BIT(*pos2);
813                 pos2++;
814         }
815
816         /* Icons Available Length */
817         if (end - pos < 2) {
818                 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
819                            "Available Length");
820                 return;
821         }
822         len2 = WPA_GET_LE16(pos);
823         pos += 2;
824         if (len2 > end - pos) {
825                 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
826                            "Available");
827                 return;
828         }
829         pos2 = pos;
830         pos += len2;
831
832         /* Icons Available */
833         while (pos2 < pos) {
834                 struct osu_icon *icon = &prov->icon[prov->icon_count];
835                 u8 flen;
836
837                 if (2 + 2 + 3 + 1 + 1 > pos - pos2) {
838                         wpa_printf(MSG_DEBUG, "HS 2.0: Invalid Icon Metadata");
839                         break;
840                 }
841
842                 icon->width = WPA_GET_LE16(pos2);
843                 pos2 += 2;
844                 icon->height = WPA_GET_LE16(pos2);
845                 pos2 += 2;
846                 os_memcpy(icon->lang, pos2, 3);
847                 pos2 += 3;
848
849                 flen = *pos2++;
850                 if (flen > pos - pos2) {
851                         wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon Type");
852                         break;
853                 }
854                 os_memcpy(icon->icon_type, pos2, flen);
855                 pos2 += flen;
856
857                 if (pos - pos2 < 1) {
858                         wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
859                                    "Filename length");
860                         break;
861                 }
862                 flen = *pos2++;
863                 if (flen > pos - pos2) {
864                         wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
865                                    "Filename");
866                         break;
867                 }
868                 os_memcpy(icon->filename, pos2, flen);
869                 pos2 += flen;
870
871                 prov->icon_count++;
872         }
873
874         /* OSU_NAI */
875         if (end - pos < 1) {
876                 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
877                 return;
878         }
879         osu_nai_len = *pos++;
880         if (osu_nai_len > end - pos) {
881                 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
882                 return;
883         }
884         os_memcpy(prov->osu_nai, pos, osu_nai_len);
885         pos += osu_nai_len;
886
887         /* OSU Service Description Length */
888         if (end - pos < 2) {
889                 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
890                            "Service Description Length");
891                 return;
892         }
893         len2 = WPA_GET_LE16(pos);
894         pos += 2;
895         if (len2 > end - pos) {
896                 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
897                            "Service Description Duples");
898                 return;
899         }
900         pos2 = pos;
901         pos += len2;
902
903         /* OSU Service Description Duples */
904         while (pos - pos2 >= 4 && prov->serv_desc_count < OSU_MAX_ITEMS) {
905                 struct osu_lang_string *f;
906                 u8 descr_len;
907
908                 descr_len = *pos2++;
909                 if (descr_len > pos - pos2 || descr_len < 3) {
910                         wpa_printf(MSG_DEBUG, "Invalid OSU Service "
911                                    "Description");
912                         break;
913                 }
914                 f = &prov->serv_desc[prov->serv_desc_count++];
915                 os_memcpy(f->lang, pos2, 3);
916                 os_memcpy(f->text, pos2 + 3, descr_len - 3);
917                 pos2 += descr_len;
918         }
919
920         wpa_printf(MSG_DEBUG, "HS 2.0: Added OSU Provider through " MACSTR,
921                    MAC2STR(bss->bssid));
922         wpa_s->osu_prov_count++;
923 }
924
925
926 void hs20_osu_icon_fetch(struct wpa_supplicant *wpa_s)
927 {
928         struct wpa_bss *bss;
929         struct wpabuf *prov_anqp;
930         const u8 *pos, *end;
931         u16 len;
932         const u8 *osu_ssid;
933         u8 osu_ssid_len;
934         u8 num_providers;
935
936         hs20_free_osu_prov(wpa_s);
937
938         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
939                 if (bss->anqp == NULL)
940                         continue;
941                 prov_anqp = bss->anqp->hs20_osu_providers_list;
942                 if (prov_anqp == NULL)
943                         continue;
944                 wpa_printf(MSG_DEBUG, "HS 2.0: Parsing OSU Providers list from "
945                            MACSTR, MAC2STR(bss->bssid));
946                 wpa_hexdump_buf(MSG_DEBUG, "HS 2.0: OSU Providers list",
947                                 prov_anqp);
948                 pos = wpabuf_head(prov_anqp);
949                 end = pos + wpabuf_len(prov_anqp);
950
951                 /* OSU SSID */
952                 if (end - pos < 1)
953                         continue;
954                 if (1 + pos[0] > end - pos) {
955                         wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
956                                    "OSU SSID");
957                         continue;
958                 }
959                 osu_ssid_len = *pos++;
960                 if (osu_ssid_len > SSID_MAX_LEN) {
961                         wpa_printf(MSG_DEBUG, "HS 2.0: Invalid OSU SSID "
962                                    "Length %u", osu_ssid_len);
963                         continue;
964                 }
965                 osu_ssid = pos;
966                 pos += osu_ssid_len;
967
968                 if (end - pos < 1) {
969                         wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
970                                    "Number of OSU Providers");
971                         continue;
972                 }
973                 num_providers = *pos++;
974                 wpa_printf(MSG_DEBUG, "HS 2.0: Number of OSU Providers: %u",
975                            num_providers);
976
977                 /* OSU Providers */
978                 while (end - pos > 2 && num_providers > 0) {
979                         num_providers--;
980                         len = WPA_GET_LE16(pos);
981                         pos += 2;
982                         if (len > (unsigned int) (end - pos))
983                                 break;
984                         hs20_osu_add_prov(wpa_s, bss, osu_ssid,
985                                           osu_ssid_len, pos, len);
986                         pos += len;
987                 }
988
989                 if (pos != end) {
990                         wpa_printf(MSG_DEBUG, "HS 2.0: Ignored %d bytes of "
991                                    "extra data after OSU Providers",
992                                    (int) (end - pos));
993                 }
994         }
995
996         wpa_s->fetch_osu_icon_in_progress = 1;
997         hs20_next_osu_icon(wpa_s);
998 }
999
1000
1001 static void hs20_osu_scan_res_handler(struct wpa_supplicant *wpa_s,
1002                                       struct wpa_scan_results *scan_res)
1003 {
1004         wpa_printf(MSG_DEBUG, "OSU provisioning fetch scan completed");
1005         if (!wpa_s->fetch_osu_waiting_scan) {
1006                 wpa_printf(MSG_DEBUG, "OSU fetch have been canceled");
1007                 return;
1008         }
1009         wpa_s->network_select = 0;
1010         wpa_s->fetch_all_anqp = 1;
1011         wpa_s->fetch_osu_info = 1;
1012         wpa_s->fetch_osu_icon_in_progress = 0;
1013
1014         interworking_start_fetch_anqp(wpa_s);
1015 }
1016
1017
1018 int hs20_fetch_osu(struct wpa_supplicant *wpa_s)
1019 {
1020         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1021                 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1022                            "interface disabled");
1023                 return -1;
1024         }
1025
1026         if (wpa_s->scanning) {
1027                 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1028                            "scanning");
1029                 return -1;
1030         }
1031
1032         if (wpa_s->conf->osu_dir == NULL) {
1033                 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1034                            "osu_dir not configured");
1035                 return -1;
1036         }
1037
1038         if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
1039                 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
1040                            "fetch in progress (%d, %d)",
1041                            wpa_s->fetch_anqp_in_progress,
1042                            wpa_s->network_select);
1043                 return -1;
1044         }
1045
1046         wpa_msg(wpa_s, MSG_INFO, "Starting OSU provisioning information fetch");
1047         wpa_s->num_osu_scans = 0;
1048         wpa_s->num_prov_found = 0;
1049         hs20_start_osu_scan(wpa_s);
1050
1051         return 0;
1052 }
1053
1054
1055 void hs20_start_osu_scan(struct wpa_supplicant *wpa_s)
1056 {
1057         wpa_s->fetch_osu_waiting_scan = 1;
1058         wpa_s->num_osu_scans++;
1059         wpa_s->scan_req = MANUAL_SCAN_REQ;
1060         wpa_s->scan_res_handler = hs20_osu_scan_res_handler;
1061         wpa_supplicant_req_scan(wpa_s, 0, 0);
1062 }
1063
1064
1065 void hs20_cancel_fetch_osu(struct wpa_supplicant *wpa_s)
1066 {
1067         wpa_printf(MSG_DEBUG, "Cancel OSU fetch");
1068         interworking_stop_fetch_anqp(wpa_s);
1069         wpa_s->fetch_osu_waiting_scan = 0;
1070         wpa_s->network_select = 0;
1071         wpa_s->fetch_osu_info = 0;
1072         wpa_s->fetch_osu_icon_in_progress = 0;
1073 }
1074
1075
1076 void hs20_icon_fetch_failed(struct wpa_supplicant *wpa_s)
1077 {
1078         hs20_osu_icon_fetch_result(wpa_s, -1);
1079         eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
1080         eloop_register_timeout(0, 0, hs20_continue_icon_fetch, wpa_s, NULL);
1081 }
1082
1083
1084 void hs20_rx_subscription_remediation(struct wpa_supplicant *wpa_s,
1085                                       const char *url, u8 osu_method)
1086 {
1087         if (url)
1088                 wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION "%u %s",
1089                         osu_method, url);
1090         else
1091                 wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION);
1092 }
1093
1094
1095 void hs20_rx_deauth_imminent_notice(struct wpa_supplicant *wpa_s, u8 code,
1096                                     u16 reauth_delay, const char *url)
1097 {
1098         if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
1099                 wpa_printf(MSG_DEBUG, "HS 2.0: Ignore deauthentication imminent notice since PMF was not enabled");
1100                 return;
1101         }
1102
1103         wpa_msg(wpa_s, MSG_INFO, HS20_DEAUTH_IMMINENT_NOTICE "%u %u %s",
1104                 code, reauth_delay, url);
1105
1106         if (code == HS20_DEAUTH_REASON_CODE_BSS) {
1107                 wpa_printf(MSG_DEBUG, "HS 2.0: Add BSS to blacklist");
1108                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
1109                 /* TODO: For now, disable full ESS since some drivers may not
1110                  * support disabling per BSS. */
1111                 if (wpa_s->current_ssid) {
1112                         struct os_reltime now;
1113                         os_get_reltime(&now);
1114                         if (now.sec + reauth_delay <=
1115                             wpa_s->current_ssid->disabled_until.sec)
1116                                 return;
1117                         wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds (BSS)",
1118                                    reauth_delay);
1119                         wpa_s->current_ssid->disabled_until.sec =
1120                                 now.sec + reauth_delay;
1121                 }
1122         }
1123
1124         if (code == HS20_DEAUTH_REASON_CODE_ESS && wpa_s->current_ssid) {
1125                 struct os_reltime now;
1126                 os_get_reltime(&now);
1127                 if (now.sec + reauth_delay <=
1128                     wpa_s->current_ssid->disabled_until.sec)
1129                         return;
1130                 wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds",
1131                            reauth_delay);
1132                 wpa_s->current_ssid->disabled_until.sec =
1133                         now.sec + reauth_delay;
1134         }
1135 }
1136
1137
1138 void hs20_init(struct wpa_supplicant *wpa_s)
1139 {
1140         dl_list_init(&wpa_s->icon_head);
1141 }
1142
1143
1144 void hs20_deinit(struct wpa_supplicant *wpa_s)
1145 {
1146         eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
1147         hs20_free_osu_prov(wpa_s);
1148         if (wpa_s->icon_head.next)
1149                 hs20_del_icon(wpa_s, NULL, NULL);
1150 }