hs20_supplicant.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. wpa_s->fetch_anqp_in_progress = 0;
  563. return;
  564. }
  565. hs20_set_osu_access_permission(wpa_s->conf->osu_dir, fname);
  566. for (i = 0; i < wpa_s->osu_prov_count; i++) {
  567. struct osu_provider *osu = &wpa_s->osu_prov[i];
  568. if (i > 0)
  569. fprintf(f, "\n");
  570. fprintf(f, "OSU-PROVIDER " MACSTR "\n"
  571. "uri=%s\n"
  572. "methods=%08x\n",
  573. MAC2STR(osu->bssid), osu->server_uri, osu->osu_methods);
  574. if (osu->osu_ssid_len) {
  575. fprintf(f, "osu_ssid=%s\n",
  576. wpa_ssid_txt(osu->osu_ssid,
  577. osu->osu_ssid_len));
  578. }
  579. if (osu->osu_nai[0])
  580. fprintf(f, "osu_nai=%s\n", osu->osu_nai);
  581. for (j = 0; j < osu->friendly_name_count; j++) {
  582. fprintf(f, "friendly_name=%s:%s\n",
  583. osu->friendly_name[j].lang,
  584. osu->friendly_name[j].text);
  585. }
  586. for (j = 0; j < osu->serv_desc_count; j++) {
  587. fprintf(f, "desc=%s:%s\n",
  588. osu->serv_desc[j].lang,
  589. osu->serv_desc[j].text);
  590. }
  591. for (j = 0; j < osu->icon_count; j++) {
  592. struct osu_icon *icon = &osu->icon[j];
  593. if (icon->failed)
  594. continue; /* could not fetch icon */
  595. fprintf(f, "icon=%u:%u:%u:%s:%s:%s\n",
  596. icon->id, icon->width, icon->height, icon->lang,
  597. icon->icon_type, icon->filename);
  598. }
  599. }
  600. fclose(f);
  601. hs20_free_osu_prov(wpa_s);
  602. wpa_msg(wpa_s, MSG_INFO, "OSU provider fetch completed");
  603. wpa_s->fetch_anqp_in_progress = 0;
  604. }
  605. void hs20_next_osu_icon(struct wpa_supplicant *wpa_s)
  606. {
  607. size_t i, j;
  608. wpa_printf(MSG_DEBUG, "HS 2.0: Ready to fetch next icon");
  609. for (i = 0; i < wpa_s->osu_prov_count; i++) {
  610. struct osu_provider *osu = &wpa_s->osu_prov[i];
  611. for (j = 0; j < osu->icon_count; j++) {
  612. struct osu_icon *icon = &osu->icon[j];
  613. if (icon->id || icon->failed)
  614. continue;
  615. wpa_printf(MSG_DEBUG, "HS 2.0: Try to fetch icon '%s' "
  616. "from " MACSTR, icon->filename,
  617. MAC2STR(osu->bssid));
  618. os_get_reltime(&wpa_s->osu_icon_fetch_start);
  619. if (hs20_anqp_send_req(wpa_s, osu->bssid,
  620. BIT(HS20_STYPE_ICON_REQUEST),
  621. (u8 *) icon->filename,
  622. os_strlen(icon->filename),
  623. 0) < 0) {
  624. icon->failed = 1;
  625. continue;
  626. }
  627. return;
  628. }
  629. }
  630. wpa_printf(MSG_DEBUG, "HS 2.0: No more icons to fetch");
  631. hs20_osu_fetch_done(wpa_s);
  632. }
  633. static void hs20_osu_add_prov(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  634. const u8 *osu_ssid, u8 osu_ssid_len,
  635. const u8 *pos, size_t len)
  636. {
  637. struct osu_provider *prov;
  638. const u8 *end = pos + len;
  639. u16 len2;
  640. const u8 *pos2;
  641. u8 uri_len, osu_method_len, osu_nai_len;
  642. wpa_hexdump(MSG_DEBUG, "HS 2.0: Parsing OSU Provider", pos, len);
  643. prov = os_realloc_array(wpa_s->osu_prov,
  644. wpa_s->osu_prov_count + 1,
  645. sizeof(*prov));
  646. if (prov == NULL)
  647. return;
  648. wpa_s->osu_prov = prov;
  649. prov = &prov[wpa_s->osu_prov_count];
  650. os_memset(prov, 0, sizeof(*prov));
  651. os_memcpy(prov->bssid, bss->bssid, ETH_ALEN);
  652. os_memcpy(prov->osu_ssid, osu_ssid, osu_ssid_len);
  653. prov->osu_ssid_len = osu_ssid_len;
  654. /* OSU Friendly Name Length */
  655. if (end - pos < 2) {
  656. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
  657. "Friendly Name Length");
  658. return;
  659. }
  660. len2 = WPA_GET_LE16(pos);
  661. pos += 2;
  662. if (len2 > end - pos) {
  663. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
  664. "Friendly Name Duples");
  665. return;
  666. }
  667. pos2 = pos;
  668. pos += len2;
  669. /* OSU Friendly Name Duples */
  670. while (pos - pos2 >= 4 && prov->friendly_name_count < OSU_MAX_ITEMS) {
  671. struct osu_lang_string *f;
  672. if (1 + pos2[0] > pos - pos2 || pos2[0] < 3) {
  673. wpa_printf(MSG_DEBUG, "Invalid OSU Friendly Name");
  674. break;
  675. }
  676. f = &prov->friendly_name[prov->friendly_name_count++];
  677. os_memcpy(f->lang, pos2 + 1, 3);
  678. os_memcpy(f->text, pos2 + 1 + 3, pos2[0] - 3);
  679. pos2 += 1 + pos2[0];
  680. }
  681. /* OSU Server URI */
  682. if (end - pos < 1) {
  683. wpa_printf(MSG_DEBUG,
  684. "HS 2.0: Not enough room for OSU Server URI length");
  685. return;
  686. }
  687. uri_len = *pos++;
  688. if (uri_len > end - pos) {
  689. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Server "
  690. "URI");
  691. return;
  692. }
  693. os_memcpy(prov->server_uri, pos, uri_len);
  694. pos += uri_len;
  695. /* OSU Method list */
  696. if (end - pos < 1) {
  697. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
  698. "list length");
  699. return;
  700. }
  701. osu_method_len = pos[0];
  702. if (osu_method_len > end - pos - 1) {
  703. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU Method "
  704. "list");
  705. return;
  706. }
  707. pos2 = pos + 1;
  708. pos += 1 + osu_method_len;
  709. while (pos2 < pos) {
  710. if (*pos2 < 32)
  711. prov->osu_methods |= BIT(*pos2);
  712. pos2++;
  713. }
  714. /* Icons Available Length */
  715. if (end - pos < 2) {
  716. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
  717. "Available Length");
  718. return;
  719. }
  720. len2 = WPA_GET_LE16(pos);
  721. pos += 2;
  722. if (len2 > end - pos) {
  723. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for Icons "
  724. "Available");
  725. return;
  726. }
  727. pos2 = pos;
  728. pos += len2;
  729. /* Icons Available */
  730. while (pos2 < pos) {
  731. struct osu_icon *icon = &prov->icon[prov->icon_count];
  732. u8 flen;
  733. if (2 + 2 + 3 + 1 + 1 > pos - pos2) {
  734. wpa_printf(MSG_DEBUG, "HS 2.0: Invalid Icon Metadata");
  735. break;
  736. }
  737. icon->width = WPA_GET_LE16(pos2);
  738. pos2 += 2;
  739. icon->height = WPA_GET_LE16(pos2);
  740. pos2 += 2;
  741. os_memcpy(icon->lang, pos2, 3);
  742. pos2 += 3;
  743. flen = *pos2++;
  744. if (flen > pos - pos2) {
  745. wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon Type");
  746. break;
  747. }
  748. os_memcpy(icon->icon_type, pos2, flen);
  749. pos2 += flen;
  750. if (pos - pos2 < 1) {
  751. wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
  752. "Filename length");
  753. break;
  754. }
  755. flen = *pos2++;
  756. if (flen > pos - pos2) {
  757. wpa_printf(MSG_DEBUG, "HS 2.0: Not room for Icon "
  758. "Filename");
  759. break;
  760. }
  761. os_memcpy(icon->filename, pos2, flen);
  762. pos2 += flen;
  763. prov->icon_count++;
  764. }
  765. /* OSU_NAI */
  766. if (end - pos < 1) {
  767. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
  768. return;
  769. }
  770. osu_nai_len = *pos++;
  771. if (osu_nai_len > end - pos) {
  772. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU_NAI");
  773. return;
  774. }
  775. os_memcpy(prov->osu_nai, pos, osu_nai_len);
  776. pos += osu_nai_len;
  777. /* OSU Service Description Length */
  778. if (end - pos < 2) {
  779. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
  780. "Service Description Length");
  781. return;
  782. }
  783. len2 = WPA_GET_LE16(pos);
  784. pos += 2;
  785. if (len2 > end - pos) {
  786. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for OSU "
  787. "Service Description Duples");
  788. return;
  789. }
  790. pos2 = pos;
  791. pos += len2;
  792. /* OSU Service Description Duples */
  793. while (pos - pos2 >= 4 && prov->serv_desc_count < OSU_MAX_ITEMS) {
  794. struct osu_lang_string *f;
  795. u8 descr_len;
  796. descr_len = *pos2++;
  797. if (descr_len > pos - pos2 || descr_len < 3) {
  798. wpa_printf(MSG_DEBUG, "Invalid OSU Service "
  799. "Description");
  800. break;
  801. }
  802. f = &prov->serv_desc[prov->serv_desc_count++];
  803. os_memcpy(f->lang, pos2, 3);
  804. os_memcpy(f->text, pos2 + 3, descr_len - 3);
  805. pos2 += descr_len;
  806. }
  807. wpa_printf(MSG_DEBUG, "HS 2.0: Added OSU Provider through " MACSTR,
  808. MAC2STR(bss->bssid));
  809. wpa_s->osu_prov_count++;
  810. }
  811. void hs20_osu_icon_fetch(struct wpa_supplicant *wpa_s)
  812. {
  813. struct wpa_bss *bss;
  814. struct wpabuf *prov_anqp;
  815. const u8 *pos, *end;
  816. u16 len;
  817. const u8 *osu_ssid;
  818. u8 osu_ssid_len;
  819. u8 num_providers;
  820. hs20_free_osu_prov(wpa_s);
  821. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  822. if (bss->anqp == NULL)
  823. continue;
  824. prov_anqp = bss->anqp->hs20_osu_providers_list;
  825. if (prov_anqp == NULL)
  826. continue;
  827. wpa_printf(MSG_DEBUG, "HS 2.0: Parsing OSU Providers list from "
  828. MACSTR, MAC2STR(bss->bssid));
  829. wpa_hexdump_buf(MSG_DEBUG, "HS 2.0: OSU Providers list",
  830. prov_anqp);
  831. pos = wpabuf_head(prov_anqp);
  832. end = pos + wpabuf_len(prov_anqp);
  833. /* OSU SSID */
  834. if (end - pos < 1)
  835. continue;
  836. if (1 + pos[0] > end - pos) {
  837. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
  838. "OSU SSID");
  839. continue;
  840. }
  841. osu_ssid_len = *pos++;
  842. if (osu_ssid_len > SSID_MAX_LEN) {
  843. wpa_printf(MSG_DEBUG, "HS 2.0: Invalid OSU SSID "
  844. "Length %u", osu_ssid_len);
  845. continue;
  846. }
  847. osu_ssid = pos;
  848. pos += osu_ssid_len;
  849. if (end - pos < 1) {
  850. wpa_printf(MSG_DEBUG, "HS 2.0: Not enough room for "
  851. "Number of OSU Providers");
  852. continue;
  853. }
  854. num_providers = *pos++;
  855. wpa_printf(MSG_DEBUG, "HS 2.0: Number of OSU Providers: %u",
  856. num_providers);
  857. /* OSU Providers */
  858. while (end - pos > 2 && num_providers > 0) {
  859. num_providers--;
  860. len = WPA_GET_LE16(pos);
  861. pos += 2;
  862. if (len > (unsigned int) (end - pos))
  863. break;
  864. hs20_osu_add_prov(wpa_s, bss, osu_ssid,
  865. osu_ssid_len, pos, len);
  866. pos += len;
  867. }
  868. if (pos != end) {
  869. wpa_printf(MSG_DEBUG, "HS 2.0: Ignored %d bytes of "
  870. "extra data after OSU Providers",
  871. (int) (end - pos));
  872. }
  873. }
  874. wpa_s->fetch_osu_icon_in_progress = 1;
  875. hs20_next_osu_icon(wpa_s);
  876. }
  877. static void hs20_osu_scan_res_handler(struct wpa_supplicant *wpa_s,
  878. struct wpa_scan_results *scan_res)
  879. {
  880. wpa_printf(MSG_DEBUG, "OSU provisioning fetch scan completed");
  881. if (!wpa_s->fetch_osu_waiting_scan) {
  882. wpa_printf(MSG_DEBUG, "OSU fetch have been canceled");
  883. return;
  884. }
  885. wpa_s->network_select = 0;
  886. wpa_s->fetch_all_anqp = 1;
  887. wpa_s->fetch_osu_info = 1;
  888. wpa_s->fetch_osu_icon_in_progress = 0;
  889. interworking_start_fetch_anqp(wpa_s);
  890. }
  891. int hs20_fetch_osu(struct wpa_supplicant *wpa_s)
  892. {
  893. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  894. wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
  895. "interface disabled");
  896. return -1;
  897. }
  898. if (wpa_s->scanning) {
  899. wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
  900. "scanning");
  901. return -1;
  902. }
  903. if (wpa_s->conf->osu_dir == NULL) {
  904. wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
  905. "osu_dir not configured");
  906. return -1;
  907. }
  908. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
  909. wpa_printf(MSG_DEBUG, "HS 2.0: Cannot start fetch_osu - "
  910. "fetch in progress (%d, %d)",
  911. wpa_s->fetch_anqp_in_progress,
  912. wpa_s->network_select);
  913. return -1;
  914. }
  915. wpa_msg(wpa_s, MSG_INFO, "Starting OSU provisioning information fetch");
  916. wpa_s->num_osu_scans = 0;
  917. wpa_s->num_prov_found = 0;
  918. hs20_start_osu_scan(wpa_s);
  919. return 0;
  920. }
  921. void hs20_start_osu_scan(struct wpa_supplicant *wpa_s)
  922. {
  923. wpa_s->fetch_osu_waiting_scan = 1;
  924. wpa_s->num_osu_scans++;
  925. wpa_s->scan_req = MANUAL_SCAN_REQ;
  926. wpa_s->scan_res_handler = hs20_osu_scan_res_handler;
  927. wpa_supplicant_req_scan(wpa_s, 0, 0);
  928. }
  929. void hs20_cancel_fetch_osu(struct wpa_supplicant *wpa_s)
  930. {
  931. wpa_printf(MSG_DEBUG, "Cancel OSU fetch");
  932. interworking_stop_fetch_anqp(wpa_s);
  933. wpa_s->fetch_osu_waiting_scan = 0;
  934. wpa_s->network_select = 0;
  935. wpa_s->fetch_osu_info = 0;
  936. wpa_s->fetch_osu_icon_in_progress = 0;
  937. }
  938. void hs20_icon_fetch_failed(struct wpa_supplicant *wpa_s)
  939. {
  940. hs20_osu_icon_fetch_result(wpa_s, -1);
  941. eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
  942. eloop_register_timeout(0, 0, hs20_continue_icon_fetch, wpa_s, NULL);
  943. }
  944. void hs20_rx_subscription_remediation(struct wpa_supplicant *wpa_s,
  945. const char *url, u8 osu_method)
  946. {
  947. if (url)
  948. wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION "%u %s",
  949. osu_method, url);
  950. else
  951. wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION);
  952. }
  953. void hs20_rx_deauth_imminent_notice(struct wpa_supplicant *wpa_s, u8 code,
  954. u16 reauth_delay, const char *url)
  955. {
  956. if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
  957. wpa_printf(MSG_DEBUG, "HS 2.0: Ignore deauthentication imminent notice since PMF was not enabled");
  958. return;
  959. }
  960. wpa_msg(wpa_s, MSG_INFO, HS20_DEAUTH_IMMINENT_NOTICE "%u %u %s",
  961. code, reauth_delay, url);
  962. if (code == HS20_DEAUTH_REASON_CODE_BSS) {
  963. wpa_printf(MSG_DEBUG, "HS 2.0: Add BSS to blacklist");
  964. wpa_blacklist_add(wpa_s, wpa_s->bssid);
  965. /* TODO: For now, disable full ESS since some drivers may not
  966. * support disabling per BSS. */
  967. if (wpa_s->current_ssid) {
  968. struct os_reltime now;
  969. os_get_reltime(&now);
  970. if (now.sec + reauth_delay <=
  971. wpa_s->current_ssid->disabled_until.sec)
  972. return;
  973. wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds (BSS)",
  974. reauth_delay);
  975. wpa_s->current_ssid->disabled_until.sec =
  976. now.sec + reauth_delay;
  977. }
  978. }
  979. if (code == HS20_DEAUTH_REASON_CODE_ESS && wpa_s->current_ssid) {
  980. struct os_reltime now;
  981. os_get_reltime(&now);
  982. if (now.sec + reauth_delay <=
  983. wpa_s->current_ssid->disabled_until.sec)
  984. return;
  985. wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds",
  986. reauth_delay);
  987. wpa_s->current_ssid->disabled_until.sec =
  988. now.sec + reauth_delay;
  989. }
  990. }
  991. void hs20_init(struct wpa_supplicant *wpa_s)
  992. {
  993. dl_list_init(&wpa_s->icon_head);
  994. }
  995. void hs20_deinit(struct wpa_supplicant *wpa_s)
  996. {
  997. eloop_cancel_timeout(hs20_continue_icon_fetch, wpa_s, NULL);
  998. hs20_free_osu_prov(wpa_s);
  999. if (wpa_s->icon_head.next)
  1000. hs20_del_icon(wpa_s, NULL, NULL);
  1001. }