interworking.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Interworking (IEEE 802.11u)
  3. * Copyright (c) 2011, Qualcomm Atheros
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "common/gas.h"
  18. #include "common/wpa_ctrl.h"
  19. #include "drivers/driver.h"
  20. #include "wpa_supplicant_i.h"
  21. #include "bss.h"
  22. #include "scan.h"
  23. #include "gas_query.h"
  24. #include "interworking.h"
  25. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
  26. static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
  27. struct wpabuf *extra)
  28. {
  29. struct wpabuf *buf;
  30. size_t i;
  31. u8 *len_pos;
  32. buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
  33. (extra ? wpabuf_len(extra) : 0));
  34. if (buf == NULL)
  35. return NULL;
  36. len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
  37. for (i = 0; i < num_ids; i++)
  38. wpabuf_put_le16(buf, info_ids[i]);
  39. gas_anqp_set_element_len(buf, len_pos);
  40. if (extra)
  41. wpabuf_put_buf(buf, extra);
  42. gas_anqp_set_len(buf);
  43. return buf;
  44. }
  45. static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
  46. u8 dialog_token,
  47. enum gas_query_result result,
  48. const struct wpabuf *adv_proto,
  49. const struct wpabuf *resp,
  50. u16 status_code)
  51. {
  52. struct wpa_supplicant *wpa_s = ctx;
  53. anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
  54. status_code);
  55. interworking_next_anqp_fetch(wpa_s);
  56. }
  57. static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
  58. struct wpa_bss *bss)
  59. {
  60. struct wpabuf *buf;
  61. int ret = 0;
  62. int res;
  63. u16 info_ids[] = {
  64. ANQP_CAPABILITY_LIST,
  65. ANQP_VENUE_NAME,
  66. ANQP_NETWORK_AUTH_TYPE,
  67. ANQP_ROAMING_CONSORTIUM,
  68. ANQP_IP_ADDR_TYPE_AVAILABILITY,
  69. ANQP_NAI_REALM,
  70. ANQP_3GPP_CELLULAR_NETWORK,
  71. ANQP_DOMAIN_NAME
  72. };
  73. struct wpabuf *extra = NULL;
  74. wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
  75. MAC2STR(bss->bssid));
  76. buf = anqp_build_req(info_ids, sizeof(info_ids) / sizeof(info_ids[0]),
  77. extra);
  78. wpabuf_free(extra);
  79. if (buf == NULL)
  80. return -1;
  81. res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
  82. interworking_anqp_resp_cb, wpa_s);
  83. if (res < 0) {
  84. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  85. ret = -1;
  86. } else
  87. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  88. "%u", res);
  89. wpabuf_free(buf);
  90. return ret;
  91. }
  92. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  93. {
  94. if (bss == NULL)
  95. return -1;
  96. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  97. MAC2STR(bss->bssid));
  98. /* TODO: create network block and connect */
  99. return 0;
  100. }
  101. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  102. {
  103. struct wpa_bss *bss, *selected = NULL;
  104. unsigned int count = 0;
  105. wpa_s->network_select = 0;
  106. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  107. if (bss->anqp_nai_realm == NULL)
  108. continue;
  109. /* TODO: verify that matching credentials are available */
  110. count++;
  111. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR,
  112. MAC2STR(bss->bssid));
  113. if (selected == NULL && wpa_s->auto_select)
  114. selected = bss;
  115. }
  116. if (count == 0) {
  117. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  118. "with matching credentials found");
  119. }
  120. if (selected)
  121. interworking_connect(wpa_s, selected);
  122. }
  123. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  124. {
  125. struct wpa_bss *bss;
  126. int found = 0;
  127. const u8 *ie;
  128. if (!wpa_s->fetch_anqp_in_progress)
  129. return;
  130. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  131. if (!(bss->caps & IEEE80211_CAP_ESS))
  132. continue;
  133. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  134. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  135. continue; /* AP does not support Interworking */
  136. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  137. found++;
  138. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  139. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  140. MACSTR, MAC2STR(bss->bssid));
  141. interworking_anqp_send_req(wpa_s, bss);
  142. break;
  143. }
  144. }
  145. if (found == 0) {
  146. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  147. wpa_s->fetch_anqp_in_progress = 0;
  148. if (wpa_s->network_select)
  149. interworking_select_network(wpa_s);
  150. }
  151. }
  152. static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  153. {
  154. struct wpa_bss *bss;
  155. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  156. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  157. wpa_s->fetch_anqp_in_progress = 1;
  158. interworking_next_anqp_fetch(wpa_s);
  159. }
  160. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  161. {
  162. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  163. return 0;
  164. wpa_s->network_select = 0;
  165. interworking_start_fetch_anqp(wpa_s);
  166. return 0;
  167. }
  168. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  169. {
  170. if (!wpa_s->fetch_anqp_in_progress)
  171. return;
  172. wpa_s->fetch_anqp_in_progress = 0;
  173. }
  174. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  175. u16 info_ids[], size_t num_ids)
  176. {
  177. struct wpabuf *buf;
  178. int ret = 0;
  179. int freq;
  180. struct wpa_bss *bss;
  181. int res;
  182. freq = wpa_s->assoc_freq;
  183. bss = wpa_bss_get_bssid(wpa_s, dst);
  184. if (bss)
  185. freq = bss->freq;
  186. if (freq <= 0)
  187. return -1;
  188. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  189. MAC2STR(dst), (unsigned int) num_ids);
  190. buf = anqp_build_req(info_ids, num_ids, NULL);
  191. if (buf == NULL)
  192. return -1;
  193. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  194. if (res < 0) {
  195. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  196. ret = -1;
  197. } else
  198. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  199. "%u", res);
  200. wpabuf_free(buf);
  201. return ret;
  202. }
  203. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  204. const u8 *sa, u16 info_id,
  205. const u8 *data, size_t slen)
  206. {
  207. const u8 *pos = data;
  208. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
  209. switch (info_id) {
  210. case ANQP_CAPABILITY_LIST:
  211. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  212. " ANQP Capability list", MAC2STR(sa));
  213. break;
  214. case ANQP_VENUE_NAME:
  215. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  216. " Venue Name", MAC2STR(sa));
  217. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  218. if (bss) {
  219. wpabuf_free(bss->anqp_venue_name);
  220. bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
  221. }
  222. break;
  223. case ANQP_NETWORK_AUTH_TYPE:
  224. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  225. " Network Authentication Type information",
  226. MAC2STR(sa));
  227. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  228. "Type", pos, slen);
  229. if (bss) {
  230. wpabuf_free(bss->anqp_network_auth_type);
  231. bss->anqp_network_auth_type =
  232. wpabuf_alloc_copy(pos, slen);
  233. }
  234. break;
  235. case ANQP_ROAMING_CONSORTIUM:
  236. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  237. " Roaming Consortium list", MAC2STR(sa));
  238. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  239. pos, slen);
  240. if (bss) {
  241. wpabuf_free(bss->anqp_roaming_consortium);
  242. bss->anqp_roaming_consortium =
  243. wpabuf_alloc_copy(pos, slen);
  244. }
  245. break;
  246. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  247. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  248. " IP Address Type Availability information",
  249. MAC2STR(sa));
  250. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  251. pos, slen);
  252. if (bss) {
  253. wpabuf_free(bss->anqp_ip_addr_type_availability);
  254. bss->anqp_ip_addr_type_availability =
  255. wpabuf_alloc_copy(pos, slen);
  256. }
  257. break;
  258. case ANQP_NAI_REALM:
  259. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  260. " NAI Realm list", MAC2STR(sa));
  261. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  262. if (bss) {
  263. wpabuf_free(bss->anqp_nai_realm);
  264. bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
  265. }
  266. break;
  267. case ANQP_3GPP_CELLULAR_NETWORK:
  268. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  269. " 3GPP Cellular Network information", MAC2STR(sa));
  270. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  271. pos, slen);
  272. if (bss) {
  273. wpabuf_free(bss->anqp_3gpp);
  274. bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  275. }
  276. break;
  277. case ANQP_DOMAIN_NAME:
  278. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  279. " Domain Name list", MAC2STR(sa));
  280. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  281. if (bss) {
  282. wpabuf_free(bss->anqp_domain_name);
  283. bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
  284. }
  285. break;
  286. case ANQP_VENDOR_SPECIFIC:
  287. if (slen < 3)
  288. return;
  289. switch (WPA_GET_BE24(pos)) {
  290. default:
  291. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  292. "vendor-specific ANQP OUI %06x",
  293. WPA_GET_BE24(pos));
  294. return;
  295. }
  296. break;
  297. default:
  298. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  299. "%u", info_id);
  300. break;
  301. }
  302. }
  303. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  304. enum gas_query_result result,
  305. const struct wpabuf *adv_proto,
  306. const struct wpabuf *resp, u16 status_code)
  307. {
  308. struct wpa_supplicant *wpa_s = ctx;
  309. const u8 *pos;
  310. const u8 *end;
  311. u16 info_id;
  312. u16 slen;
  313. if (result != GAS_QUERY_SUCCESS)
  314. return;
  315. pos = wpabuf_head(adv_proto);
  316. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  317. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  318. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  319. "Protocol in response");
  320. return;
  321. }
  322. pos = wpabuf_head(resp);
  323. end = pos + wpabuf_len(resp);
  324. while (pos < end) {
  325. if (pos + 4 > end) {
  326. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  327. break;
  328. }
  329. info_id = WPA_GET_LE16(pos);
  330. pos += 2;
  331. slen = WPA_GET_LE16(pos);
  332. pos += 2;
  333. if (pos + slen > end) {
  334. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  335. "for Info ID %u", info_id);
  336. break;
  337. }
  338. interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
  339. slen);
  340. pos += slen;
  341. }
  342. }
  343. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  344. struct wpa_scan_results *scan_res)
  345. {
  346. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  347. "ANQP fetch");
  348. interworking_start_fetch_anqp(wpa_s);
  349. }
  350. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
  351. {
  352. interworking_stop_fetch_anqp(wpa_s);
  353. wpa_s->network_select = 1;
  354. wpa_s->auto_select = !!auto_select;
  355. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  356. "selection");
  357. wpa_s->scan_res_handler = interworking_scan_res_handler;
  358. wpa_s->scan_req = 2;
  359. wpa_supplicant_req_scan(wpa_s, 0, 0);
  360. return 0;
  361. }