hs20_supplicant.c 28 KB

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