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