hs20_supplicant.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "wpa_supplicant_i.h"
  16. #include "driver_i.h"
  17. #include "config.h"
  18. #include "bss.h"
  19. #include "gas_query.h"
  20. #include "interworking.h"
  21. #include "hs20_supplicant.h"
  22. void wpas_hs20_add_indication(struct wpabuf *buf, int pps_mo_id)
  23. {
  24. u8 conf;
  25. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  26. wpabuf_put_u8(buf, pps_mo_id >= 0 ? 7 : 5);
  27. wpabuf_put_be24(buf, OUI_WFA);
  28. wpabuf_put_u8(buf, HS20_INDICATION_OUI_TYPE);
  29. conf = HS20_VERSION;
  30. if (pps_mo_id >= 0)
  31. conf |= HS20_PPS_MO_ID_PRESENT;
  32. wpabuf_put_u8(buf, conf);
  33. if (pps_mo_id >= 0)
  34. wpabuf_put_le16(buf, pps_mo_id);
  35. }
  36. int is_hs20_network(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  37. struct wpa_bss *bss)
  38. {
  39. if (!wpa_s->conf->hs20 || !ssid)
  40. return 0;
  41. if (ssid->parent_cred)
  42. return 1;
  43. if (bss && !wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE))
  44. return 0;
  45. /*
  46. * This may catch some non-Hotspot 2.0 cases, but it is safer to do that
  47. * than cause Hotspot 2.0 connections without indication element getting
  48. * added. Non-Hotspot 2.0 APs should ignore the unknown vendor element.
  49. */
  50. if (!(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X))
  51. return 0;
  52. if (!(ssid->pairwise_cipher & WPA_CIPHER_CCMP))
  53. return 0;
  54. if (ssid->proto != WPA_PROTO_RSN)
  55. return 0;
  56. return 1;
  57. }
  58. int hs20_get_pps_mo_id(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  59. {
  60. struct wpa_cred *cred;
  61. if (ssid == NULL || ssid->parent_cred == NULL)
  62. return 0;
  63. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  64. if (ssid->parent_cred == cred)
  65. return cred->update_identifier;
  66. }
  67. return 0;
  68. }
  69. struct wpabuf * hs20_build_anqp_req(u32 stypes, const u8 *payload,
  70. size_t payload_len)
  71. {
  72. struct wpabuf *buf;
  73. u8 *len_pos;
  74. buf = gas_anqp_build_initial_req(0, 100 + payload_len);
  75. if (buf == NULL)
  76. return NULL;
  77. len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  78. wpabuf_put_be24(buf, OUI_WFA);
  79. wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
  80. if (stypes == BIT(HS20_STYPE_NAI_HOME_REALM_QUERY)) {
  81. wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
  82. wpabuf_put_u8(buf, 0); /* Reserved */
  83. if (payload)
  84. wpabuf_put_data(buf, payload, payload_len);
  85. } else {
  86. u8 i;
  87. wpabuf_put_u8(buf, HS20_STYPE_QUERY_LIST);
  88. wpabuf_put_u8(buf, 0); /* Reserved */
  89. for (i = 0; i < 32; i++) {
  90. if (stypes & BIT(i))
  91. wpabuf_put_u8(buf, i);
  92. }
  93. }
  94. gas_anqp_set_element_len(buf, len_pos);
  95. gas_anqp_set_len(buf);
  96. return buf;
  97. }
  98. int hs20_anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst, u32 stypes,
  99. const u8 *payload, size_t payload_len)
  100. {
  101. struct wpabuf *buf;
  102. int ret = 0;
  103. int freq;
  104. struct wpa_bss *bss;
  105. int res;
  106. freq = wpa_s->assoc_freq;
  107. bss = wpa_bss_get_bssid(wpa_s, dst);
  108. if (bss) {
  109. wpa_bss_anqp_unshare_alloc(bss);
  110. freq = bss->freq;
  111. }
  112. if (freq <= 0)
  113. return -1;
  114. wpa_printf(MSG_DEBUG, "HS20: ANQP Query Request to " MACSTR " for "
  115. "subtypes 0x%x", MAC2STR(dst), stypes);
  116. buf = hs20_build_anqp_req(stypes, payload, payload_len);
  117. if (buf == NULL)
  118. return -1;
  119. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  120. if (res < 0) {
  121. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  122. wpabuf_free(buf);
  123. ret = -1;
  124. } else
  125. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  126. "%u", res);
  127. return ret;
  128. }
  129. void hs20_parse_rx_hs20_anqp_resp(struct wpa_supplicant *wpa_s,
  130. const u8 *sa, const u8 *data, size_t slen)
  131. {
  132. const u8 *pos = data;
  133. u8 subtype;
  134. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
  135. struct wpa_bss_anqp *anqp = NULL;
  136. if (slen < 2)
  137. return;
  138. if (bss)
  139. anqp = bss->anqp;
  140. subtype = *pos++;
  141. slen--;
  142. pos++; /* Reserved */
  143. slen--;
  144. switch (subtype) {
  145. case HS20_STYPE_CAPABILITY_LIST:
  146. wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
  147. " HS Capability List", MAC2STR(sa));
  148. wpa_hexdump_ascii(MSG_DEBUG, "HS Capability List", pos, slen);
  149. break;
  150. case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
  151. wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
  152. " Operator Friendly Name", MAC2STR(sa));
  153. wpa_hexdump_ascii(MSG_DEBUG, "oper friendly name", pos, slen);
  154. if (anqp) {
  155. wpabuf_free(anqp->hs20_operator_friendly_name);
  156. anqp->hs20_operator_friendly_name =
  157. wpabuf_alloc_copy(pos, slen);
  158. }
  159. break;
  160. case HS20_STYPE_WAN_METRICS:
  161. wpa_hexdump(MSG_DEBUG, "WAN Metrics", pos, slen);
  162. if (slen < 13) {
  163. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: Too short WAN "
  164. "Metrics value from " MACSTR, MAC2STR(sa));
  165. break;
  166. }
  167. wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
  168. " WAN Metrics %02x:%u:%u:%u:%u:%u", MAC2STR(sa),
  169. pos[0], WPA_GET_LE32(pos + 1), WPA_GET_LE32(pos + 5),
  170. pos[9], pos[10], WPA_GET_LE16(pos + 11));
  171. if (anqp) {
  172. wpabuf_free(anqp->hs20_wan_metrics);
  173. anqp->hs20_wan_metrics = wpabuf_alloc_copy(pos, slen);
  174. }
  175. break;
  176. case HS20_STYPE_CONNECTION_CAPABILITY:
  177. wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
  178. " Connection Capability", MAC2STR(sa));
  179. wpa_hexdump_ascii(MSG_DEBUG, "conn capability", pos, slen);
  180. if (anqp) {
  181. wpabuf_free(anqp->hs20_connection_capability);
  182. anqp->hs20_connection_capability =
  183. wpabuf_alloc_copy(pos, slen);
  184. }
  185. break;
  186. case HS20_STYPE_OPERATING_CLASS:
  187. wpa_msg(wpa_s, MSG_INFO, "RX-HS20-ANQP " MACSTR
  188. " Operating Class", MAC2STR(sa));
  189. wpa_hexdump_ascii(MSG_DEBUG, "Operating Class", pos, slen);
  190. if (anqp) {
  191. wpabuf_free(anqp->hs20_operating_class);
  192. anqp->hs20_operating_class =
  193. wpabuf_alloc_copy(pos, slen);
  194. }
  195. break;
  196. default:
  197. wpa_printf(MSG_DEBUG, "HS20: Unsupported subtype %u", subtype);
  198. break;
  199. }
  200. }