hs20_supplicant.c 23 KB

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