hs20_supplicant.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2009, Atheros Communications, Inc.
  3. * Copyright (c) 2011-2012, 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 "bss.h"
  20. #include "blacklist.h"
  21. #include "gas_query.h"
  22. #include "interworking.h"
  23. #include "hs20_supplicant.h"
  24. void wpas_hs20_add_indication(struct wpabuf *buf, int pps_mo_id)
  25. {
  26. u8 conf;
  27. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  28. wpabuf_put_u8(buf, pps_mo_id >= 0 ? 7 : 5);
  29. wpabuf_put_be24(buf, OUI_WFA);
  30. wpabuf_put_u8(buf, HS20_INDICATION_OUI_TYPE);
  31. conf = HS20_VERSION;
  32. if (pps_mo_id >= 0)
  33. conf |= HS20_PPS_MO_ID_PRESENT;
  34. wpabuf_put_u8(buf, conf);
  35. if (pps_mo_id >= 0)
  36. wpabuf_put_le16(buf, pps_mo_id);
  37. }
  38. int is_hs20_network(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  39. struct wpa_bss *bss)
  40. {
  41. if (!wpa_s->conf->hs20 || !ssid)
  42. return 0;
  43. if (ssid->parent_cred)
  44. return 1;
  45. if (bss && !wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE))
  46. return 0;
  47. /*
  48. * This may catch some non-Hotspot 2.0 cases, but it is safer to do that
  49. * than cause Hotspot 2.0 connections without indication element getting
  50. * added. Non-Hotspot 2.0 APs should ignore the unknown vendor element.
  51. */
  52. if (!(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X))
  53. return 0;
  54. if (!(ssid->pairwise_cipher & WPA_CIPHER_CCMP))
  55. return 0;
  56. if (ssid->proto != WPA_PROTO_RSN)
  57. return 0;
  58. return 1;
  59. }
  60. int hs20_get_pps_mo_id(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  61. {
  62. struct wpa_cred *cred;
  63. if (ssid == NULL || ssid->parent_cred == NULL)
  64. return 0;
  65. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  66. if (ssid->parent_cred == cred)
  67. return cred->update_identifier;
  68. }
  69. return 0;
  70. }
  71. struct wpabuf * hs20_build_anqp_req(u32 stypes, const u8 *payload,
  72. size_t payload_len)
  73. {
  74. struct wpabuf *buf;
  75. u8 *len_pos;
  76. buf = gas_anqp_build_initial_req(0, 100 + payload_len);
  77. if (buf == NULL)
  78. return NULL;
  79. len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  80. wpabuf_put_be24(buf, OUI_WFA);
  81. wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
  82. if (stypes == BIT(HS20_STYPE_NAI_HOME_REALM_QUERY)) {
  83. wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
  84. wpabuf_put_u8(buf, 0); /* Reserved */
  85. if (payload)
  86. wpabuf_put_data(buf, payload, payload_len);
  87. } else if (stypes == BIT(HS20_STYPE_ICON_REQUEST)) {
  88. wpabuf_put_u8(buf, HS20_STYPE_ICON_REQUEST);
  89. wpabuf_put_u8(buf, 0); /* Reserved */
  90. if (payload)
  91. wpabuf_put_data(buf, payload, payload_len);
  92. } else {
  93. u8 i;
  94. wpabuf_put_u8(buf, HS20_STYPE_QUERY_LIST);
  95. wpabuf_put_u8(buf, 0); /* Reserved */
  96. for (i = 0; i < 32; i++) {
  97. if (stypes & BIT(i))
  98. wpabuf_put_u8(buf, i);
  99. }
  100. }
  101. gas_anqp_set_element_len(buf, len_pos);
  102. gas_anqp_set_len(buf);
  103. return buf;
  104. }
  105. int hs20_anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, u32 stypes,
  106. const u8 *payload, size_t payload_len)
  107. {
  108. struct wpabuf *buf;
  109. int ret = 0;
  110. int freq;
  111. struct wpa_bss *bss;
  112. int res;
  113. freq = wpa_s->assoc_freq;
  114. bss = wpa_bss_get_bssid(wpa_s, dst);
  115. if (bss) {
  116. wpa_bss_anqp_unshare_alloc(bss);
  117. freq = bss->freq;
  118. }
  119. if (freq <= 0)
  120. return -1;
  121. wpa_printf(MSG_DEBUG, "HS20: ANQP Query Request to " MACSTR " for "
  122. "subtypes 0x%x", MAC2STR(dst), stypes);
  123. buf = hs20_build_anqp_req(stypes, payload, payload_len);
  124. if (buf == NULL)
  125. return -1;
  126. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  127. if (res < 0) {
  128. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  129. wpabuf_free(buf);
  130. ret = -1;
  131. } else
  132. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  133. "%u", res);
  134. return ret;
  135. }
  136. void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
  137. const u8 *sa, const u8 *data, size_t slen)
  138. {
  139. const u8 *pos = data;
  140. u8 subtype;
  141. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
  142. struct wpa_bss_anqp *anqp = NULL;
  143. u16 data_len;
  144. if (slen < 2)
  145. return;
  146. if (bss)
  147. anqp = bss->anqp;
  148. subtype = *pos++;
  149. slen--;
  150. pos++; /* Reserved */
  151. slen--;
  152. switch (subtype) {
  153. case HS20_STYPE_CAPABILITY_LIST:
  154. wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
  155. " HS Capability List", MAC2STR(sa));
  156. wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
  157. break;
  158. case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
  159. wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
  160. " Operator Friendly Name", MAC2STR(sa));
  161. wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
  162. if (anqp) {
  163. wpabuf_free(anqp->hs20_operator_friendly_name);
  164. anqp->hs20_operator_friendly_name =
  165. wpabuf_alloc_copy(pos, slen);
  166. }
  167. break;
  168. case HS20_STYPE_WAN_METRICS:
  169. wpa_hexdump(MSG_DEBUG, "WAN Metrics", pos, slen);
  170. if (slen < 13) {
  171. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short WAN "
  172. "Metrics value from " MACSTR, MAC2STR(sa));
  173. break;
  174. }
  175. wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
  176. " WAN Metrics %02x:%u:%u:%u:%u:%u", MAC2STR(sa),
  177. pos[0], WPA_GET_LE32(pos + 1), WPA_GET_LE32(pos + 5),
  178. pos[9], pos[10], WPA_GET_LE16(pos + 11));
  179. if (anqp) {
  180. wpabuf_free(anqp->hs20_wan_metrics);
  181. anqp->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
  182. }
  183. break;
  184. case HS20_STYPE_CONNECTION_CAPABILITY:
  185. wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
  186. " Connection Capability", MAC2STR(sa));
  187. wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
  188. if (anqp) {
  189. wpabuf_free(anqp->hs20_connection_capability);
  190. anqp->hs20_connection_capability =
  191. wpabuf_alloc_copy(pos, slen);
  192. }
  193. break;
  194. case HS20_STYPE_OPERATING_CLASS:
  195. wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
  196. " Operating Class", MAC2STR(sa));
  197. wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
  198. if (anqp) {
  199. wpabuf_free(anqp->hs20_operating_class);
  200. anqp->hs20_operating_class =
  201. wpabuf_alloc_copy(pos, slen);
  202. }
  203. break;
  204. case HS20_STYPE_ICON_BINARY_FILE:
  205. wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
  206. " Icon Binary File", MAC2STR(sa));
  207. if (slen < 4) {
  208. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon "
  209. "Binary File value from " MACSTR, MAC2STR(sa));
  210. break;
  211. }
  212. wpa_printf(MSG_DEBUG, "HS 2.0: Download Status Code %u", *pos);
  213. pos++;
  214. slen--;
  215. if ((size_t) 1 + pos[0] > slen) {
  216. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon "
  217. "Binary File value from " MACSTR, MAC2STR(sa));
  218. break;
  219. }
  220. wpa_hexdump_ascii(MSG_DEBUG, "Icon Type", pos + 1, pos[0]);
  221. slen -= 1 + pos[0];
  222. pos += 1 + pos[0];
  223. if (slen < 2) {
  224. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon "
  225. "Binary File value from " MACSTR, MAC2STR(sa));
  226. break;
  227. }
  228. data_len = WPA_GET_BE16(pos);
  229. pos += 2;
  230. slen -= 2;
  231. if (data_len > slen) {
  232. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short Icon "
  233. "Binary File value from " MACSTR, MAC2STR(sa));
  234. break;
  235. }
  236. wpa_printf(MSG_DEBUG, "Icon Binary Data: %u bytes", data_len);
  237. break;
  238. default:
  239. wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
  240. break;
  241. }
  242. }
  243. void hs20_rx_subscription_remediation(struct wpa_supplicant *wpa_s,
  244. const char *url, u8 osu_method)
  245. {
  246. if (url)
  247. wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION "%u %s",
  248. osu_method, url);
  249. else
  250. wpa_msg(wpa_s, MSG_INFO, HS20_SUBSCRIPTION_REMEDIATION);
  251. }
  252. void hs20_rx_deauth_imminent_notice(struct wpa_supplicant *wpa_s, u8 code,
  253. u16 reauth_delay, const char *url)
  254. {
  255. if (!wpa_sm_pmf_enabled(wpa_s->wpa)) {
  256. wpa_printf(MSG_DEBUG, "HS 2.0: Ignore deauthentication imminent notice since PMF was not enabled");
  257. return;
  258. }
  259. wpa_msg(wpa_s, MSG_INFO, HS20_DEAUTH_IMMINENT_NOTICE "%u %u %s",
  260. code, reauth_delay, url);
  261. if (code == HS20_DEAUTH_REASON_CODE_BSS) {
  262. wpa_printf(MSG_DEBUG, "HS 2.0: Add BSS to blacklist");
  263. wpa_blacklist_add(wpa_s, wpa_s->bssid);
  264. }
  265. if (code == HS20_DEAUTH_REASON_CODE_ESS && wpa_s->current_ssid) {
  266. struct os_time now;
  267. os_get_time(&now);
  268. if (now.sec + reauth_delay <=
  269. wpa_s->current_ssid->disabled_until.sec)
  270. return;
  271. wpa_printf(MSG_DEBUG, "HS 2.0: Disable network for %u seconds",
  272. reauth_delay);
  273. wpa_s->current_ssid->disabled_until.sec =
  274. now.sec + reauth_delay;
  275. }
  276. }