blob: eb18ed2bd716181dcde749994a1565496b3d87e4 [file] [log] [blame]
Dmitry Shmidt04949592012-07-19 12:16:46 -07001/*
2 * Copyright (c) 2009, Atheros Communications, Inc.
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
Dmitry Shmidt04949592012-07-19 12:16:46 -07004 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080010#include <sys/stat.h>
Dmitry Shmidt04949592012-07-19 12:16:46 -070011
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/gas.h"
17#include "common/wpa_ctrl.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080018#include "rsn_supp/wpa.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070019#include "wpa_supplicant_i.h"
20#include "driver_i.h"
21#include "config.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080022#include "scan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070023#include "bss.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080024#include "blacklist.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070025#include "gas_query.h"
26#include "interworking.h"
27#include "hs20_supplicant.h"
28
29
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080030#define OSU_MAX_ITEMS 10
31
32struct osu_lang_string {
33 char lang[4];
34 char text[253];
35};
36
37struct osu_icon {
38 u16 width;
39 u16 height;
40 char lang[4];
41 char icon_type[256];
42 char filename[256];
43 unsigned int id;
44 unsigned int failed:1;
45};
46
47struct osu_provider {
48 u8 bssid[ETH_ALEN];
49 u8 osu_ssid[32];
50 u8 osu_ssid_len;
51 char server_uri[256];
52 u32 osu_methods; /* bit 0 = OMA-DM, bit 1 = SOAP-XML SPP */
53 char osu_nai[256];
54 struct osu_lang_string friendly_name[OSU_MAX_ITEMS];
55 size_t friendly_name_count;
56 struct osu_lang_string serv_desc[OSU_MAX_ITEMS];
57 size_t serv_desc_count;
58 struct osu_icon icon[OSU_MAX_ITEMS];
59 size_t icon_count;
60};
61
62
63void wpas_hs20_add_indication(struct wpabuf *buf, int pps_mo_id)
Dmitry Shmidt04949592012-07-19 12:16:46 -070064{
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080065 u8 conf;
66
Dmitry Shmidt04949592012-07-19 12:16:46 -070067 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080068 wpabuf_put_u8(buf, pps_mo_id >= 0 ? 7 : 5);
Dmitry Shmidt04949592012-07-19 12:16:46 -070069 wpabuf_put_be24(buf, OUI_WFA);
70 wpabuf_put_u8(buf, HS20_INDICATION_OUI_TYPE);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080071 conf = HS20_VERSION;
72 if (pps_mo_id >= 0)
73 conf |= HS20_PPS_MO_ID_PRESENT;
74 wpabuf_put_u8(buf, conf);
75 if (pps_mo_id >= 0)
76 wpabuf_put_le16(buf, pps_mo_id);
Dmitry Shmidt04949592012-07-19 12:16:46 -070077}
78
79
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -070080int is_hs20_network(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
81 struct wpa_bss *bss)
82{
83 if (!wpa_s->conf->hs20 || !ssid)
84 return 0;
85
86 if (ssid->parent_cred)
87 return 1;
88
89 if (bss && !wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE))
90 return 0;
91
92 /*
93 * This may catch some non-Hotspot 2.0 cases, but it is safer to do that
94 * than cause Hotspot 2.0 connections without indication element getting
95 * added. Non-Hotspot 2.0 APs should ignore the unknown vendor element.
96 */
97
98 if (!(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X))
99 return 0;
100 if (!(ssid->pairwise_cipher & WPA_CIPHER_CCMP))
101 return 0;
102 if (ssid->proto != WPA_PROTO_RSN)
103 return 0;
104
105 return 1;
106}
107
108
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800109int hs20_get_pps_mo_id(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
110{
111 struct wpa_cred *cred;
112
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700113 if (ssid == NULL)
114 return 0;
115
116 if (ssid->update_identifier)
117 return ssid->update_identifier;
118
119 if (ssid->parent_cred == NULL)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800120 return 0;
121
122 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
123 if (ssid->parent_cred == cred)
124 return cred->update_identifier;
125 }
126
127 return 0;
128}
129
130
Dmitry Shmidt15907092014-03-25 10:42:57 -0700131void hs20_put_anqp_req(u32 stypes, const u8 *payload, size_t payload_len,
132 struct wpabuf *buf)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700133{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700134 u8 *len_pos;
135
Dmitry Shmidt04949592012-07-19 12:16:46 -0700136 if (buf == NULL)
Dmitry Shmidt15907092014-03-25 10:42:57 -0700137 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700138
139 len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
140 wpabuf_put_be24(buf, OUI_WFA);
141 wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
142 if (stypes == BIT(HS20_STYPE_NAI_HOME_REALM_QUERY)) {
143 wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
144 wpabuf_put_u8(buf, 0); /* Reserved */
145 if (payload)
146 wpabuf_put_data(buf, payload, payload_len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800147 } else if (stypes == BIT(HS20_STYPE_ICON_REQUEST)) {
148 wpabuf_put_u8(buf, HS20_STYPE_ICON_REQUEST);
149 wpabuf_put_u8(buf, 0); /* Reserved */
150 if (payload)
151 wpabuf_put_data(buf, payload, payload_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700152 } else {
153 u8 i;
154 wpabuf_put_u8(buf, HS20_STYPE_QUERY_LIST);
155 wpabuf_put_u8(buf, 0); /* Reserved */
156 for (i = 0; i < 32; i++) {
157 if (stypes & BIT(i))
158 wpabuf_put_u8(buf, i);
159 }
160 }
161 gas_anqp_set_element_len(buf, len_pos);
162
163 gas_anqp_set_len(buf);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700164}
165
166
167struct wpabuf * hs20_build_anqp_req(u32 stypes, const u8 *payload,
168 size_t payload_len)
169{
170 struct wpabuf *buf;
171
172 buf = gas_anqp_build_initial_req(0, 100 + payload_len);
173 if (buf == NULL)
174 return NULL;
175
176 hs20_put_anqp_req(stypes, payload, payload_len, buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700177
178 return buf;
179}
180
181
182int hs20_anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, u32 stypes,
183 const u8 *payload, size_t payload_len)
184{
185 struct wpabuf *buf;
186 int ret = 0;
187 int freq;
188 struct wpa_bss *bss;
189 int res;
190
191 freq = wpa_s->assoc_freq;
192 bss = wpa_bss_get_bssid(wpa_s, dst);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800193 if (bss) {
194 wpa_bss_anqp_unshare_alloc(bss);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700195 freq = bss->freq;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800196 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700197 if (freq <= 0)
198 return -1;
199
200 wpa_printf(MSG_DEBUG, "HS20: ANQP Query Request to " MACSTR " for "
201 "subtypes 0x%x", MAC2STR(dst), stypes);
202
203 buf = hs20_build_anqp_req(stypes, payload, payload_len);
204 if (buf == NULL)
205 return -1;
206
207 res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
208 if (res < 0) {
209 wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700210 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700211 ret = -1;
212 } else
213 wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
214 "%u", res);
215
Dmitry Shmidt04949592012-07-19 12:16:46 -0700216 return ret;
217}
218
219
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800220static void hs20_set_osu_access_permission(const char *osu_dir,
221 const char *fname)
222{
223 struct stat statbuf;
224
225 /* Get OSU directory information */
226 if (stat(osu_dir, &statbuf) < 0) {
227 wpa_printf(MSG_WARNING, "Cannot stat the OSU directory %s",
228 osu_dir);
229 return;
230 }
231
232 if (chmod(fname, statbuf.st_mode) < 0) {
233 wpa_printf(MSG_WARNING,
234 "Cannot change the permissions for %s", fname);
235 return;
236 }
237
238 if (chown(fname, statbuf.st_uid, statbuf.st_gid) < 0) {
239 wpa_printf(MSG_WARNING, "Cannot change the ownership for %s",
240 fname);
241 }
242}
243
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800244static int hs20_process_icon_binary_file(struct wpa_supplicant *wpa_s,
245 const u8 *sa, const u8 *pos,
246 size_t slen)
247{
248 char fname[256];
249 int png;
250 FILE *f;
251 u16 data_len;
252
253 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR " Icon Binary File",
254 MAC2STR(sa));
255
256 if (slen < 4) {
257 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
258 "value from " MACSTR, MAC2STR(sa));
259 return -1;
260 }
261
262 wpa_printf(MSG_DEBUG, "HS 2.0: Download Status Code %u", *pos);
263 if (*pos != 0)
264 return -1;
265 pos++;
266 slen--;
267
268 if ((size_t) 1 + pos[0] > slen) {
269 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
270 "value from " MACSTR, MAC2STR(sa));
271 return -1;
272 }
273 wpa_hexdump_ascii(MSG_DEBUG, "Icon Type", pos + 1, pos[0]);
274 png = os_strncasecmp((char *) pos + 1, "image/png", 9) == 0;
275 slen -= 1 + pos[0];
276 pos += 1 + pos[0];
277
278 if (slen < 2) {
279 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
280 "value from " MACSTR, MAC2STR(sa));
281 return -1;
282 }
283 data_len = WPA_GET_LE16(pos);
284 pos += 2;
285 slen -= 2;
286
287 if (data_len > slen) {
288 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon Binary File "
289 "value from " MACSTR, MAC2STR(sa));
290 return -1;
291 }
292
293 wpa_printf(MSG_DEBUG, "Icon Binary Data: %u bytes", data_len);
294 if (wpa_s->conf->osu_dir == NULL)
295 return -1;
296
297 wpa_s->osu_icon_id++;
298 if (wpa_s->osu_icon_id == 0)
299 wpa_s->osu_icon_id++;
300 snprintf(fname, sizeof(fname), "%s/osu-icon-%u.%s",
301 wpa_s->conf->osu_dir, wpa_s->osu_icon_id,
302 png ? "png" : "icon");
303 f = fopen(fname, "wb");
304 if (f == NULL)
305 return -1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800306
307 hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
308
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800309 if (fwrite(pos, slen, 1, f) != 1) {
310 fclose(f);
311 unlink(fname);
312 return -1;
313 }
314 fclose(f);
315
316 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP-ICON %s", fname);
317 return 0;
318}
319
320
321static void hs20_continue_icon_fetch(void *eloop_ctx, void *sock_ctx)
322{
323 struct wpa_supplicant *wpa_s = eloop_ctx;
324 if (wpa_s->fetch_osu_icon_in_progress)
325 hs20_next_osu_icon(wpa_s);
326}
327
328
329static void hs20_osu_icon_fetch_result(struct wpa_supplicant *wpa_s, int res)
330{
331 size_t i, j;
332 struct os_reltime now, tmp;
333 int dur;
334
335 os_get_reltime(&now);
336 os_reltime_sub(&now, &wpa_s->osu_icon_fetch_start, &tmp);
337 dur = tmp.sec * 1000 + tmp.usec / 1000;
338 wpa_printf(MSG_DEBUG, "HS 2.0: Icon fetch dur=%d ms res=%d",
339 dur, res);
340
341 for (i = 0; i < wpa_s->osu_prov_count; i++) {
342 struct osu_provider *osu = &wpa_s->osu_prov[i];
343 for (j = 0; j < osu->icon_count; j++) {
344 struct osu_icon *icon = &osu->icon[j];
345 if (icon->id || icon->failed)
346 continue;
347 if (res < 0)
348 icon->failed = 1;
349 else
350 icon->id = wpa_s->osu_icon_id;
351 return;
352 }
353 }
354}
355
356
Dmitry Shmidt04949592012-07-19 12:16:46 -0700357void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800358 struct wpa_bss *bss, const u8 *sa,
359 const u8 *data, size_t slen)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700360{
361 const u8 *pos = data;
362 u8 subtype;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700363 struct wpa_bss_anqp *anqp = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800364 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700365
366 if (slen < 2)
367 return;
368
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700369 if (bss)
370 anqp = bss->anqp;
371
Dmitry Shmidt04949592012-07-19 12:16:46 -0700372 subtype = *pos++;
373 slen--;
374
375 pos++; /* Reserved */
376 slen--;
377
378 switch (subtype) {
379 case HS20_STYPE_CAPABILITY_LIST:
380 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
381 " HS Capability List", MAC2STR(sa));
382 wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
383 break;
384 case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
385 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
386 " Operator Friendly Name", MAC2STR(sa));
387 wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700388 if (anqp) {
389 wpabuf_free(anqp->hs20_operator_friendly_name);
390 anqp->hs20_operator_friendly_name =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700391 wpabuf_alloc_copy(pos, slen);
392 }
393 break;
394 case HS20_STYPE_WAN_METRICS:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800395 wpa_hexdump(MSG_DEBUG, "WAN Metrics", pos, slen);
396 if (slen < 13) {
397 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short WAN "
398 "Metrics value from " MACSTR, MAC2STR(sa));
399 break;
400 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700401 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800402 " WAN Metrics %02x:%u:%u:%u:%u:%u", MAC2STR(sa),
403 pos[0], WPA_GET_LE32(pos + 1), WPA_GET_LE32(pos + 5),
404 pos[9], pos[10], WPA_GET_LE16(pos + 11));
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700405 if (anqp) {
406 wpabuf_free(anqp->hs20_wan_metrics);
407 anqp->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700408 }
409 break;
410 case HS20_STYPE_CONNECTION_CAPABILITY:
411 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
412 " Connection Capability", MAC2STR(sa));
413 wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700414 if (anqp) {
415 wpabuf_free(anqp->hs20_connection_capability);
416 anqp->hs20_connection_capability =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700417 wpabuf_alloc_copy(pos, slen);
418 }
419 break;
420 case HS20_STYPE_OPERATING_CLASS:
421 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
422 " Operating Class", MAC2STR(sa));
423 wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700424 if (anqp) {
425 wpabuf_free(anqp->hs20_operating_class);
426 anqp->hs20_operating_class =
Dmitry Shmidt04949592012-07-19 12:16:46 -0700427 wpabuf_alloc_copy(pos, slen);
428 }
429 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800430 case HS20_STYPE_OSU_PROVIDERS_LIST:
431 wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
432 " OSU Providers list", MAC2STR(sa));
433 wpa_s->num_prov_found++;
434 if (anqp) {
435 wpabuf_free(anqp->hs20_osu_providers_list);
436 anqp->hs20_osu_providers_list =
437 wpabuf_alloc_copy(pos, slen);
438 }
439 break;
440 case HS20_STYPE_ICON_BINARY_FILE:
441 ret = hs20_process_icon_binary_file(wpa_s, sa, pos, slen);
442 if (wpa_s->fetch_osu_icon_in_progress) {
443 hs20_osu_icon_fetch_result(wpa_s, ret);
444 eloop_cancel_timeout(hs20_continue_icon_fetch,
445 wpa_s, NULL);
446 eloop_register_timeout(0, 0, hs20_continue_icon_fetch,
447 wpa_s, NULL);
448 }
449 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700450 default:
451 wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
452 break;
453 }
454}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800455
456
457void hs20_notify_parse_done(struct wpa_supplicant *wpa_s)
458{
459 if (!wpa_s->fetch_osu_icon_in_progress)
460 return;
461 if (eloop_is_timeout_registered(hs20_continue_icon_fetch, wpa_s, NULL))
462 return;
463 /*
464 * We are going through icon fetch, but no icon response was received.
465 * Assume this means the current AP could not provide an answer to avoid
466 * getting stuck in fetch iteration.
467 */
468 hs20_icon_fetch_failed(wpa_s);
469}
470
471
472static void hs20_free_osu_prov_entry(struct osu_provider *prov)
473{
474}
475
476
477void hs20_free_osu_prov(struct wpa_supplicant *wpa_s)
478{
479 size_t i;
480 for (i = 0; i < wpa_s->osu_prov_count; i++)
481 hs20_free_osu_prov_entry(&wpa_s->osu_prov[i]);
482 os_free(wpa_s->osu_prov);
483 wpa_s->osu_prov = NULL;
484 wpa_s->osu_prov_count = 0;
485}
486
487
488static void hs20_osu_fetch_done(struct wpa_supplicant *wpa_s)
489{
490 char fname[256];
491 FILE *f;
492 size_t i, j;
493
494 wpa_s->fetch_osu_info = 0;
495 wpa_s->fetch_osu_icon_in_progress = 0;
496
497 if (wpa_s->conf->osu_dir == NULL) {
498 hs20_free_osu_prov(wpa_s);
499 wpa_s->fetch_anqp_in_progress = 0;
500 return;
501 }
502
503 snprintf(fname, sizeof(fname), "%s/osu-providers.txt",
504 wpa_s->conf->osu_dir);
505 f = fopen(fname, "w");
506 if (f == NULL) {
507 hs20_free_osu_prov(wpa_s);
508 return;
509 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800510
511 hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
512
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800513 for (i = 0; i < wpa_s->osu_prov_count; i++) {
514 struct osu_provider *osu = &wpa_s->osu_prov[i];
515 if (i > 0)
516 fprintf(f, "\n");
517 fprintf(f, "OSU-PROVIDER " MACSTR "\n"
518 "uri=%s\n"
519 "methods=%08x\n",
520 MAC2STR(osu->bssid), osu->server_uri, osu->osu_methods);
521 if (osu->osu_ssid_len) {
522 fprintf(f, "osu_ssid=%s\n",
523 wpa_ssid_txt(osu->osu_ssid,
524 osu->osu_ssid_len));
525 }
526 if (osu->osu_nai[0])
527 fprintf(f, "osu_nai=%s\n", osu->osu_nai);
528 for (j = 0; j < osu->friendly_name_count; j++) {
529 fprintf(f, "friendly_name=%s:%s\n",
530 osu->friendly_name[j].lang,
531 osu->friendly_name[j].text);
532 }
533 for (j = 0; j < osu->serv_desc_count; j++) {
534 fprintf(f, "desc=%s:%s\n",
535 osu->serv_desc[j].lang,
536 osu->serv_desc[j].text);
537 }
538 for (j = 0; j < osu->icon_count; j++) {
539 struct osu_icon *icon = &osu->icon[j];
540 if (icon->failed)
541 continue; /* could not fetch icon */
542 fprintf(f, "icon=%u:%u:%u:%s:%s:%s\n",
543 icon->id, icon->width, icon->height, icon->lang,
544 icon->icon_type, icon->filename);
545 }
546 }
547 fclose(f);
548 hs20_free_osu_prov(wpa_s);
549
550 wpa_msg(wpa_s, MSG_INFO, "OSU provider fetch completed");
551 wpa_s->fetch_anqp_in_progress = 0;
552}
553
554
555void hs20_next_osu_icon(struct wpa_supplicant *wpa_s)
556{
557 size_t i, j;
558
559 wpa_printf(MSG_DEBUG, "HS 2.0: Ready to fetch next icon");
560
561 for (i = 0; i < wpa_s->osu_prov_count; i++) {
562 struct osu_provider *osu = &wpa_s->osu_prov[i];
563 for (j = 0; j < osu->icon_count; j++) {
564 struct osu_icon *icon = &osu->icon[j];
565 if (icon->id || icon->failed)
566 continue;
567
568 wpa_printf(MSG_DEBUG, "HS 2.0: Try to fetch icon '%s' "
569 "from " MACSTR, icon->filename,
570 MAC2STR(osu->bssid));
571 os_get_reltime(&wpa_s->osu_icon_fetch_start);
572 if (hs20_anqp_send_req(wpa_s, osu->bssid,
573 BIT(HS20_STYPE_ICON_REQUEST),
574 (u8 *) icon->filename,
575 os_strlen(icon->filename)) < 0) {
576 icon->failed = 1;
577 continue;
578 }
579 return;
580 }
581 }
582
583 wpa_printf(MSG_DEBUG, "HS 2.0: No more icons to fetch");
584 hs20_osu_fetch_done(wpa_s);
585}
586
587
588static void hs20_osu_add_prov(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
589 const u8 *osu_ssid, u8 osu_ssid_len,
590 const u8 *pos, size_t len)
591{
592 struct osu_provider *prov;
593 const u8 *end = pos + len;
594 u16 len2;
595 const u8 *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800596 u8 uri_len, osu_method_len, osu_nai_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800597
598 wpa_hexdump(MSG_DEBUG, "HS 2.0: Parsing OSU Provider", pos, len);
599 prov = os_realloc_array(wpa_s->osu_prov,
600 wpa_s->osu_prov_count + 1,
601 sizeof(*prov));
602 if (prov == NULL)
603 return;
604 wpa_s->osu_prov = prov;
605 prov = &prov[wpa_s->osu_prov_count];
606 os_memset(prov, 0, sizeof(*prov));
607
608 os_memcpy(prov->bssid, bss->bssid, ETH_ALEN);
609 os_memcpy(prov->osu_ssid, osu_ssid, osu_ssid_len);
610 prov->osu_ssid_len = osu_ssid_len;
611
612 /* OSU Friendly Name Length */
613 if (pos + 2 > end) {
614 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
615 "Friendly Name Length");
616 return;
617 }
618 len2 = WPA_GET_LE16(pos);
619 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800620 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800621 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
622 "Friendly Name Duples");
623 return;
624 }
625 pos2 = pos;
626 pos += len2;
627
628 /* OSU Friendly Name Duples */
629 while (pos2 + 4 <= pos && prov->friendly_name_count < OSU_MAX_ITEMS) {
630 struct osu_lang_string *f;
631 if (pos2 + 1 + pos2[0] > pos || pos2[0] < 3) {
632 wpa_printf(MSG_DEBUG, "Invalid OSU Friendly Name");
633 break;
634 }
635 f = &prov->friendly_name[prov->friendly_name_count++];
636 os_memcpy(f->lang, pos2 + 1, 3);
637 os_memcpy(f->text, pos2 + 1 + 3, pos2[0] - 3);
638 pos2 += 1 + pos2[0];
639 }
640
641 /* OSU Server URI */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800642 if (pos + 1 > end) {
643 wpa_printf(MSG_DEBUG,
644 "HS 2.0: Not enough room for OSU Server URI length");
645 return;
646 }
647 uri_len = *pos++;
648 if (uri_len > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800649 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Server "
650 "URI");
651 return;
652 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800653 os_memcpy(prov->server_uri, pos, uri_len);
654 pos += uri_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800655
656 /* OSU Method list */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800657 if (pos + 1 > end) {
658 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
659 "list length");
660 return;
661 }
662 osu_method_len = pos[0];
663 if (osu_method_len > end - pos - 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800664 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
665 "list");
666 return;
667 }
668 pos2 = pos + 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800669 pos += 1 + osu_method_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800670 while (pos2 < pos) {
671 if (*pos2 < 32)
672 prov->osu_methods |= BIT(*pos2);
673 pos2++;
674 }
675
676 /* Icons Available Length */
677 if (pos + 2 > end) {
678 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
679 "Available Length");
680 return;
681 }
682 len2 = WPA_GET_LE16(pos);
683 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800684 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800685 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
686 "Available");
687 return;
688 }
689 pos2 = pos;
690 pos += len2;
691
692 /* Icons Available */
693 while (pos2 < pos) {
694 struct osu_icon *icon = &prov->icon[prov->icon_count];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800695 u8 flen;
696
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800697 if (pos2 + 2 + 2 + 3 + 1 + 1 > pos) {
698 wpa_printf(MSG_DEBUG, "HS 2.0: Invalid Icon Metadata");
699 break;
700 }
701
702 icon->width = WPA_GET_LE16(pos2);
703 pos2 += 2;
704 icon->height = WPA_GET_LE16(pos2);
705 pos2 += 2;
706 os_memcpy(icon->lang, pos2, 3);
707 pos2 += 3;
708
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800709 flen = pos2[0];
710 if (flen > pos - pos2 - 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800711 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon Type");
712 break;
713 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800714 os_memcpy(icon->icon_type, pos2 + 1, flen);
715 pos2 += 1 + flen;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800716
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800717 if (pos2 + 1 > pos) {
718 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
719 "Filename length");
720 break;
721 }
722 flen = pos2[0];
723 if (flen > pos - pos2 - 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800724 wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
725 "Filename");
726 break;
727 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800728 os_memcpy(icon->filename, pos2 + 1, flen);
729 pos2 += 1 + flen;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800730
731 prov->icon_count++;
732 }
733
734 /* OSU_NAI */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800735 if (pos + 1 > end) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800736 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
737 return;
738 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800739 osu_nai_len = pos[0];
740 if (osu_nai_len > end - pos - 1) {
741 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
742 return;
743 }
744 os_memcpy(prov->osu_nai, pos + 1, osu_nai_len);
745 pos += 1 + osu_nai_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800746
747 /* OSU Service Description Length */
748 if (pos + 2 > end) {
749 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
750 "Service Description Length");
751 return;
752 }
753 len2 = WPA_GET_LE16(pos);
754 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800755 if (len2 > end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800756 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
757 "Service Description Duples");
758 return;
759 }
760 pos2 = pos;
761 pos += len2;
762
763 /* OSU Service Description Duples */
764 while (pos2 + 4 <= pos && prov->serv_desc_count < OSU_MAX_ITEMS) {
765 struct osu_lang_string *f;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800766 u8 descr_len;
767
768 descr_len = pos2[0];
769 if (descr_len > pos - pos2 - 1 || descr_len < 3) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800770 wpa_printf(MSG_DEBUG, "Invalid OSU Service "
771 "Description");
772 break;
773 }
774 f = &prov->serv_desc[prov->serv_desc_count++];
775 os_memcpy(f->lang, pos2 + 1, 3);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800776 os_memcpy(f->text, pos2 + 1 + 3, descr_len - 3);
777 pos2 += 1 + descr_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800778 }
779
780 wpa_printf(MSG_DEBUG, "HS 2.0: Added OSU Provider through " MACSTR,
781 MAC2STR(bss->bssid));
782 wpa_s->osu_prov_count++;
783}
784
785
786void hs20_osu_icon_fetch(struct wpa_supplicant *wpa_s)
787{
788 struct wpa_bss *bss;
789 struct wpabuf *prov_anqp;
790 const u8 *pos, *end;
791 u16 len;
792 const u8 *osu_ssid;
793 u8 osu_ssid_len;
794 u8 num_providers;
795
796 hs20_free_osu_prov(wpa_s);
797
798 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
799 if (bss->anqp == NULL)
800 continue;
801 prov_anqp = bss->anqp->hs20_osu_providers_list;
802 if (prov_anqp == NULL)
803 continue;
804 wpa_printf(MSG_DEBUG, "HS 2.0: Parsing OSU Providers list from "
805 MACSTR, MAC2STR(bss->bssid));
806 wpa_hexdump_buf(MSG_DEBUG, "HS 2.0: OSU Providers list",
807 prov_anqp);
808 pos = wpabuf_head(prov_anqp);
809 end = pos + wpabuf_len(prov_anqp);
810
811 /* OSU SSID */
812 if (pos + 1 > end)
813 continue;
814 if (pos + 1 + pos[0] > end) {
815 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
816 "OSU SSID");
817 continue;
818 }
819 osu_ssid_len = *pos++;
820 if (osu_ssid_len > 32) {
821 wpa_printf(MSG_DEBUG, "HS 2.0: Invalid OSU SSID "
822 "Length %u", osu_ssid_len);
823 continue;
824 }
825 osu_ssid = pos;
826 pos += osu_ssid_len;
827
828 if (pos + 1 > end) {
829 wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
830 "Number of OSU Providers");
831 continue;
832 }
833 num_providers = *pos++;
834 wpa_printf(MSG_DEBUG, "HS 2.0: Number of OSU Providers: %u",
835 num_providers);
836
837 /* OSU Providers */
838 while (pos + 2 < end && num_providers > 0) {
839 num_providers--;
840 len = WPA_GET_LE16(pos);
841 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800842 if (len > (unsigned int) (end - pos))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800843 break;
844 hs20_osu_add_prov(wpa_s, bss, osu_ssid,
845 osu_ssid_len, pos, len);
846 pos += len;
847 }
848
849 if (pos != end) {
850 wpa_printf(MSG_DEBUG, "HS 2.0: Ignored %d bytes of "
851 "extra data after OSU Providers",
852 (int) (end - pos));
853 }
854 }
855
856 wpa_s->fetch_osu_icon_in_progress = 1;
857 hs20_next_osu_icon(wpa_s);
858}
859
860
861static void hs20_osu_scan_res_handler(struct wpa_supplicant *wpa_s,
862 struct wpa_scan_results *scan_res)
863{
864 wpa_printf(MSG_DEBUG, "OSU provisioning fetch scan completed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800865 if (!wpa_s->fetch_osu_waiting_scan) {
866 wpa_printf(MSG_DEBUG, "OSU fetch have been canceled");
867 return;
868 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800869 wpa_s->network_select = 0;
870 wpa_s->fetch_all_anqp = 1;
871 wpa_s->fetch_osu_info = 1;
872 wpa_s->fetch_osu_icon_in_progress = 0;
873
874 interworking_start_fetch_anqp(wpa_s);
875}
876
877
878int hs20_fetch_osu(struct wpa_supplicant *wpa_s)
879{
880 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
881 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
882 "interface disabled");
883 return -1;
884 }
885
886 if (wpa_s->scanning) {
887 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
888 "scanning");
889 return -1;
890 }
891
892 if (wpa_s->conf->osu_dir == NULL) {
893 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
894 "osu_dir not configured");
895 return -1;
896 }
897
898 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
899 wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
900 "fetch in progress (%d, %d)",
901 wpa_s->fetch_anqp_in_progress,
902 wpa_s->network_select);
903 return -1;
904 }
905
906 wpa_msg(wpa_s, MSG_INFO, "Starting OSU provisioning information fetch");
907 wpa_s->num_osu_scans = 0;
908 wpa_s->num_prov_found = 0;
909 hs20_start_osu_scan(wpa_s);
910
911 return 0;
912}
913
914
915void hs20_start_osu_scan(struct wpa_supplicant *wpa_s)
916{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800917 wpa_s->fetch_osu_waiting_scan = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800918 wpa_s->num_osu_scans++;
919 wpa_s->scan_req = MANUAL_SCAN_REQ;
920 wpa_s->scan_res_handler = hs20_osu_scan_res_handler;
921 wpa_supplicant_req_scan(wpa_s, 0, 0);
922}
923
924
925void hs20_cancel_fetch_osu(struct wpa_supplicant *wpa_s)
926{
927 wpa_printf(MSG_DEBUG, "Cancel OSU fetch");
928 interworking_stop_fetch_anqp(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800929 wpa_s->fetch_osu_waiting_scan = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800930 wpa_s->network_select = 0;
931 wpa_s->fetch_osu_info = 0;
932 wpa_s->fetch_osu_icon_in_progress = 0;
933}
934
935
936void hs20_icon_fetch_failed(struct wpa_supplicant *wpa_s)
937{
938 hs20_osu_icon_fetch_result(wpa_s, -1);
939 eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
940 eloop_register_timeout(0, 0, hs20_continue_icon_fetch, wpa_s, NULL);
941}
942
943
944void hs20_rx_subscription_remediation(struct wpa_supplicant *wpa_s,
945 const char *url, u8 osu_method)
946{
947 if (url)
948 wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION "%u %s",
949 osu_method, url);
950 else
951 wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION);
952}
953
954
955void hs20_rx_deauth_imminent_notice(struct wpa_supplicant *wpa_s, u8 code,
956 u16 reauth_delay, const char *url)
957{
958 if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
959 wpa_printf(MSG_DEBUG, "HS 2.0: Ignore deauthentication imminent notice since PMF was not enabled");
960 return;
961 }
962
963 wpa_msg(wpa_s, MSG_INFO, HS20_DEAUTH_IMMINENT_NOTICE "%u %u %s",
964 code, reauth_delay, url);
965
966 if (code == HS20_DEAUTH_REASON_CODE_BSS) {
967 wpa_printf(MSG_DEBUG, "HS 2.0: Add BSS to blacklist");
968 wpa_blacklist_add(wpa_s, wpa_s->bssid);
969 /* TODO: For now, disable full ESS since some drivers may not
970 * support disabling per BSS. */
971 if (wpa_s->current_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800972 struct os_reltime now;
973 os_get_reltime(&now);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800974 if (now.sec + reauth_delay <=
975 wpa_s->current_ssid->disabled_until.sec)
976 return;
977 wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds (BSS)",
978 reauth_delay);
979 wpa_s->current_ssid->disabled_until.sec =
980 now.sec + reauth_delay;
981 }
982 }
983
984 if (code == HS20_DEAUTH_REASON_CODE_ESS && wpa_s->current_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800985 struct os_reltime now;
986 os_get_reltime(&now);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800987 if (now.sec + reauth_delay <=
988 wpa_s->current_ssid->disabled_until.sec)
989 return;
990 wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds",
991 reauth_delay);
992 wpa_s->current_ssid->disabled_until.sec =
993 now.sec + reauth_delay;
994 }
995}
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700996
997
998void hs20_deinit(struct wpa_supplicant *wpa_s)
999{
1000 eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
1001 hs20_free_osu_prov(wpa_s);
1002}