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