gas_query.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * Generic advertisement service (GAS) query
  3. * Copyright (c) 2009, Atheros Communications
  4. * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
  5. * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
  6. *
  7. * This software may be distributed under the terms of the BSD license.
  8. * See README for more details.
  9. */
  10. #include "includes.h"
  11. #include "common.h"
  12. #include "utils/eloop.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 "offchannel.h"
  20. #include "gas_query.h"
  21. /** GAS query timeout in seconds */
  22. #define GAS_QUERY_TIMEOUT_PERIOD 2
  23. /**
  24. * struct gas_query_pending - Pending GAS query
  25. */
  26. struct gas_query_pending {
  27. struct dl_list list;
  28. struct gas_query *gas;
  29. u8 addr[ETH_ALEN];
  30. u8 dialog_token;
  31. u8 next_frag_id;
  32. unsigned int wait_comeback:1;
  33. unsigned int offchannel_tx_started:1;
  34. int freq;
  35. u16 status_code;
  36. struct wpabuf *req;
  37. struct wpabuf *adv_proto;
  38. struct wpabuf *resp;
  39. struct os_reltime last_oper;
  40. void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
  41. enum gas_query_result result,
  42. const struct wpabuf *adv_proto,
  43. const struct wpabuf *resp, u16 status_code);
  44. void *ctx;
  45. };
  46. /**
  47. * struct gas_query - Internal GAS query data
  48. */
  49. struct gas_query {
  50. struct wpa_supplicant *wpa_s;
  51. struct dl_list pending; /* struct gas_query_pending */
  52. struct gas_query_pending *current;
  53. struct wpa_radio_work *work;
  54. };
  55. static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
  56. static void gas_query_timeout(void *eloop_data, void *user_ctx);
  57. static int ms_from_time(struct os_reltime *last)
  58. {
  59. struct os_reltime now, res;
  60. os_get_reltime(&now);
  61. os_reltime_sub(&now, last, &res);
  62. return res.sec * 1000 + res.usec / 1000;
  63. }
  64. /**
  65. * gas_query_init - Initialize GAS query component
  66. * @wpa_s: Pointer to wpa_supplicant data
  67. * Returns: Pointer to GAS query data or %NULL on failure
  68. */
  69. struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
  70. {
  71. struct gas_query *gas;
  72. gas = os_zalloc(sizeof(*gas));
  73. if (gas == NULL)
  74. return NULL;
  75. gas->wpa_s = wpa_s;
  76. dl_list_init(&gas->pending);
  77. return gas;
  78. }
  79. static const char * gas_result_txt(enum gas_query_result result)
  80. {
  81. switch (result) {
  82. case GAS_QUERY_SUCCESS:
  83. return "SUCCESS";
  84. case GAS_QUERY_FAILURE:
  85. return "FAILURE";
  86. case GAS_QUERY_TIMEOUT:
  87. return "TIMEOUT";
  88. case GAS_QUERY_PEER_ERROR:
  89. return "PEER_ERROR";
  90. case GAS_QUERY_INTERNAL_ERROR:
  91. return "INTERNAL_ERROR";
  92. case GAS_QUERY_CANCELLED:
  93. return "CANCELLED";
  94. case GAS_QUERY_DELETED_AT_DEINIT:
  95. return "DELETED_AT_DEINIT";
  96. }
  97. return "N/A";
  98. }
  99. static void gas_query_free(struct gas_query_pending *query, int del_list)
  100. {
  101. struct gas_query *gas = query->gas;
  102. if (del_list)
  103. dl_list_del(&query->list);
  104. if (gas->work && gas->work->ctx == query) {
  105. radio_work_done(gas->work);
  106. gas->work = NULL;
  107. }
  108. wpabuf_free(query->req);
  109. wpabuf_free(query->adv_proto);
  110. wpabuf_free(query->resp);
  111. os_free(query);
  112. }
  113. static void gas_query_done(struct gas_query *gas,
  114. struct gas_query_pending *query,
  115. enum gas_query_result result)
  116. {
  117. wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
  118. " dialog_token=%u freq=%d status_code=%u result=%s",
  119. MAC2STR(query->addr), query->dialog_token, query->freq,
  120. query->status_code, gas_result_txt(result));
  121. if (gas->current == query)
  122. gas->current = NULL;
  123. if (query->offchannel_tx_started)
  124. offchannel_send_action_done(gas->wpa_s);
  125. eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
  126. eloop_cancel_timeout(gas_query_timeout, gas, query);
  127. dl_list_del(&query->list);
  128. query->cb(query->ctx, query->addr, query->dialog_token, result,
  129. query->adv_proto, query->resp, query->status_code);
  130. gas_query_free(query, 0);
  131. }
  132. /**
  133. * gas_query_deinit - Deinitialize GAS query component
  134. * @gas: GAS query data from gas_query_init()
  135. */
  136. void gas_query_deinit(struct gas_query *gas)
  137. {
  138. struct gas_query_pending *query, *next;
  139. if (gas == NULL)
  140. return;
  141. dl_list_for_each_safe(query, next, &gas->pending,
  142. struct gas_query_pending, list)
  143. gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
  144. os_free(gas);
  145. }
  146. static struct gas_query_pending *
  147. gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
  148. {
  149. struct gas_query_pending *q;
  150. dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
  151. if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
  152. q->dialog_token == dialog_token)
  153. return q;
  154. }
  155. return NULL;
  156. }
  157. static int gas_query_append(struct gas_query_pending *query, const u8 *data,
  158. size_t len)
  159. {
  160. if (wpabuf_resize(&query->resp, len) < 0) {
  161. wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
  162. return -1;
  163. }
  164. wpabuf_put_data(query->resp, data, len);
  165. return 0;
  166. }
  167. static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
  168. unsigned int freq, const u8 *dst,
  169. const u8 *src, const u8 *bssid,
  170. const u8 *data, size_t data_len,
  171. enum offchannel_send_action_result result)
  172. {
  173. struct gas_query_pending *query;
  174. struct gas_query *gas = wpa_s->gas;
  175. int dur;
  176. if (gas->current == NULL) {
  177. wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
  178. MACSTR " result=%d - no query in progress",
  179. freq, MAC2STR(dst), result);
  180. return;
  181. }
  182. query = gas->current;
  183. dur = ms_from_time(&query->last_oper);
  184. wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
  185. " result=%d query=%p dialog_token=%u dur=%d ms",
  186. freq, MAC2STR(dst), result, query, query->dialog_token, dur);
  187. if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
  188. wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
  189. return;
  190. }
  191. os_get_reltime(&query->last_oper);
  192. if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
  193. eloop_cancel_timeout(gas_query_timeout, gas, query);
  194. eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
  195. gas_query_timeout, gas, query);
  196. }
  197. if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
  198. eloop_cancel_timeout(gas_query_timeout, gas, query);
  199. eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
  200. }
  201. }
  202. static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
  203. {
  204. if (wpa_s->current_ssid == NULL ||
  205. wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
  206. os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
  207. return 0;
  208. return wpa_sm_pmf_enabled(wpa_s->wpa);
  209. }
  210. static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
  211. struct wpabuf *req)
  212. {
  213. int res, prot = pmf_in_use(gas->wpa_s, query->addr);
  214. wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
  215. "freq=%d prot=%d", MAC2STR(query->addr),
  216. (unsigned int) wpabuf_len(req), query->freq, prot);
  217. if (prot) {
  218. u8 *categ = wpabuf_mhead_u8(req);
  219. *categ = WLAN_ACTION_PROTECTED_DUAL;
  220. }
  221. os_get_reltime(&query->last_oper);
  222. res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
  223. gas->wpa_s->own_addr, query->addr,
  224. wpabuf_head(req), wpabuf_len(req), 1000,
  225. gas_query_tx_status, 0);
  226. if (res == 0)
  227. query->offchannel_tx_started = 1;
  228. return res;
  229. }
  230. static void gas_query_tx_comeback_req(struct gas_query *gas,
  231. struct gas_query_pending *query)
  232. {
  233. struct wpabuf *req;
  234. req = gas_build_comeback_req(query->dialog_token);
  235. if (req == NULL) {
  236. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  237. return;
  238. }
  239. if (gas_query_tx(gas, query, req) < 0) {
  240. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  241. MACSTR, MAC2STR(query->addr));
  242. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  243. }
  244. wpabuf_free(req);
  245. }
  246. static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
  247. {
  248. struct gas_query *gas = eloop_data;
  249. struct gas_query_pending *query = user_ctx;
  250. wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
  251. MAC2STR(query->addr));
  252. gas_query_tx_comeback_req(gas, query);
  253. }
  254. static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
  255. struct gas_query_pending *query,
  256. u16 comeback_delay)
  257. {
  258. unsigned int secs, usecs;
  259. secs = (comeback_delay * 1024) / 1000000;
  260. usecs = comeback_delay * 1024 - secs * 1000000;
  261. wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
  262. " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
  263. eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
  264. eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
  265. gas, query);
  266. }
  267. static void gas_query_rx_initial(struct gas_query *gas,
  268. struct gas_query_pending *query,
  269. const u8 *adv_proto, const u8 *resp,
  270. size_t len, u16 comeback_delay)
  271. {
  272. wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
  273. MACSTR " (dialog_token=%u comeback_delay=%u)",
  274. MAC2STR(query->addr), query->dialog_token, comeback_delay);
  275. query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
  276. if (query->adv_proto == NULL) {
  277. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  278. return;
  279. }
  280. if (comeback_delay) {
  281. query->wait_comeback = 1;
  282. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  283. return;
  284. }
  285. /* Query was completed without comeback mechanism */
  286. if (gas_query_append(query, resp, len) < 0) {
  287. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  288. return;
  289. }
  290. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  291. }
  292. static void gas_query_rx_comeback(struct gas_query *gas,
  293. struct gas_query_pending *query,
  294. const u8 *adv_proto, const u8 *resp,
  295. size_t len, u8 frag_id, u8 more_frags,
  296. u16 comeback_delay)
  297. {
  298. wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
  299. MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
  300. "comeback_delay=%u)",
  301. MAC2STR(query->addr), query->dialog_token, frag_id,
  302. more_frags, comeback_delay);
  303. if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
  304. os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
  305. wpabuf_len(query->adv_proto)) != 0) {
  306. wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
  307. "between initial and comeback response from "
  308. MACSTR, MAC2STR(query->addr));
  309. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  310. return;
  311. }
  312. if (comeback_delay) {
  313. if (frag_id) {
  314. wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
  315. "with non-zero frag_id and comeback_delay "
  316. "from " MACSTR, MAC2STR(query->addr));
  317. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  318. return;
  319. }
  320. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  321. return;
  322. }
  323. if (frag_id != query->next_frag_id) {
  324. wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
  325. "from " MACSTR, MAC2STR(query->addr));
  326. if (frag_id + 1 == query->next_frag_id) {
  327. wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
  328. "retry of previous fragment");
  329. return;
  330. }
  331. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  332. return;
  333. }
  334. query->next_frag_id++;
  335. if (gas_query_append(query, resp, len) < 0) {
  336. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  337. return;
  338. }
  339. if (more_frags) {
  340. gas_query_tx_comeback_req(gas, query);
  341. return;
  342. }
  343. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  344. }
  345. /**
  346. * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
  347. * @gas: GAS query data from gas_query_init()
  348. * @da: Destination MAC address of the Action frame
  349. * @sa: Source MAC address of the Action frame
  350. * @bssid: BSSID of the Action frame
  351. * @categ: Category of the Action frame
  352. * @data: Payload of the Action frame
  353. * @len: Length of @data
  354. * @freq: Frequency (in MHz) on which the frame was received
  355. * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
  356. */
  357. int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
  358. const u8 *bssid, u8 categ, const u8 *data, size_t len,
  359. int freq)
  360. {
  361. struct gas_query_pending *query;
  362. u8 action, dialog_token, frag_id = 0, more_frags = 0;
  363. u16 comeback_delay, resp_len;
  364. const u8 *pos, *adv_proto;
  365. int prot, pmf;
  366. if (gas == NULL || len < 4)
  367. return -1;
  368. prot = categ == WLAN_ACTION_PROTECTED_DUAL;
  369. pmf = pmf_in_use(gas->wpa_s, bssid);
  370. if (prot && !pmf) {
  371. wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
  372. return 0;
  373. }
  374. if (!prot && pmf) {
  375. wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
  376. return 0;
  377. }
  378. pos = data;
  379. action = *pos++;
  380. dialog_token = *pos++;
  381. if (action != WLAN_PA_GAS_INITIAL_RESP &&
  382. action != WLAN_PA_GAS_COMEBACK_RESP)
  383. return -1; /* Not a GAS response */
  384. query = gas_query_get_pending(gas, sa, dialog_token);
  385. if (query == NULL) {
  386. wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
  387. " dialog token %u", MAC2STR(sa), dialog_token);
  388. return -1;
  389. }
  390. wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
  391. ms_from_time(&query->last_oper), MAC2STR(sa));
  392. if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
  393. wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
  394. MACSTR " dialog token %u when waiting for comeback "
  395. "response", MAC2STR(sa), dialog_token);
  396. return 0;
  397. }
  398. if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
  399. wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
  400. MACSTR " dialog token %u when waiting for initial "
  401. "response", MAC2STR(sa), dialog_token);
  402. return 0;
  403. }
  404. query->status_code = WPA_GET_LE16(pos);
  405. pos += 2;
  406. if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
  407. action == WLAN_PA_GAS_COMEBACK_RESP) {
  408. wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
  409. } else if (query->status_code != WLAN_STATUS_SUCCESS) {
  410. wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
  411. "%u failed - status code %u",
  412. MAC2STR(sa), dialog_token, query->status_code);
  413. gas_query_done(gas, query, GAS_QUERY_FAILURE);
  414. return 0;
  415. }
  416. if (action == WLAN_PA_GAS_COMEBACK_RESP) {
  417. if (pos + 1 > data + len)
  418. return 0;
  419. frag_id = *pos & 0x7f;
  420. more_frags = (*pos & 0x80) >> 7;
  421. pos++;
  422. }
  423. /* Comeback Delay */
  424. if (pos + 2 > data + len)
  425. return 0;
  426. comeback_delay = WPA_GET_LE16(pos);
  427. pos += 2;
  428. /* Advertisement Protocol element */
  429. if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
  430. wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
  431. "Protocol element in the response from " MACSTR,
  432. MAC2STR(sa));
  433. return 0;
  434. }
  435. if (*pos != WLAN_EID_ADV_PROTO) {
  436. wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
  437. "Protocol element ID %u in response from " MACSTR,
  438. *pos, MAC2STR(sa));
  439. return 0;
  440. }
  441. adv_proto = pos;
  442. pos += 2 + pos[1];
  443. /* Query Response Length */
  444. if (pos + 2 > data + len) {
  445. wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
  446. return 0;
  447. }
  448. resp_len = WPA_GET_LE16(pos);
  449. pos += 2;
  450. if (pos + resp_len > data + len) {
  451. wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
  452. "response from " MACSTR, MAC2STR(sa));
  453. return 0;
  454. }
  455. if (pos + resp_len < data + len) {
  456. wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
  457. "after Query Response from " MACSTR,
  458. (unsigned int) (data + len - pos - resp_len),
  459. MAC2STR(sa));
  460. }
  461. if (action == WLAN_PA_GAS_COMEBACK_RESP)
  462. gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
  463. frag_id, more_frags, comeback_delay);
  464. else
  465. gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
  466. comeback_delay);
  467. return 0;
  468. }
  469. static void gas_query_timeout(void *eloop_data, void *user_ctx)
  470. {
  471. struct gas_query *gas = eloop_data;
  472. struct gas_query_pending *query = user_ctx;
  473. wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
  474. " dialog token %u",
  475. MAC2STR(query->addr), query->dialog_token);
  476. gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
  477. }
  478. static int gas_query_dialog_token_available(struct gas_query *gas,
  479. const u8 *dst, u8 dialog_token)
  480. {
  481. struct gas_query_pending *q;
  482. dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
  483. if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
  484. dialog_token == q->dialog_token)
  485. return 0;
  486. }
  487. return 1;
  488. }
  489. static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
  490. {
  491. struct gas_query_pending *query = work->ctx;
  492. struct gas_query *gas = query->gas;
  493. if (deinit) {
  494. if (work->started) {
  495. gas->work = NULL;
  496. gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
  497. return;
  498. }
  499. gas_query_free(query, 1);
  500. return;
  501. }
  502. gas->work = work;
  503. if (gas_query_tx(gas, query, query->req) < 0) {
  504. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  505. MACSTR, MAC2STR(query->addr));
  506. gas_query_free(query, 1);
  507. return;
  508. }
  509. gas->current = query;
  510. wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
  511. query->dialog_token);
  512. eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
  513. gas_query_timeout, gas, query);
  514. }
  515. /**
  516. * gas_query_req - Request a GAS query
  517. * @gas: GAS query data from gas_query_init()
  518. * @dst: Destination MAC address for the query
  519. * @freq: Frequency (in MHz) for the channel on which to send the query
  520. * @req: GAS query payload (to be freed by gas_query module in case of success
  521. * return)
  522. * @cb: Callback function for reporting GAS query result and response
  523. * @ctx: Context pointer to use with the @cb call
  524. * Returns: dialog token (>= 0) on success or -1 on failure
  525. */
  526. int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
  527. struct wpabuf *req,
  528. void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
  529. enum gas_query_result result,
  530. const struct wpabuf *adv_proto,
  531. const struct wpabuf *resp, u16 status_code),
  532. void *ctx)
  533. {
  534. struct gas_query_pending *query;
  535. int dialog_token;
  536. static int next_start = 0;
  537. if (wpabuf_len(req) < 3)
  538. return -1;
  539. for (dialog_token = 0; dialog_token < 256; dialog_token++) {
  540. if (gas_query_dialog_token_available(
  541. gas, dst, (next_start + dialog_token) % 256))
  542. break;
  543. }
  544. if (dialog_token == 256)
  545. return -1; /* Too many pending queries */
  546. dialog_token = (next_start + dialog_token) % 256;
  547. next_start = (dialog_token + 1) % 256;
  548. query = os_zalloc(sizeof(*query));
  549. if (query == NULL)
  550. return -1;
  551. query->gas = gas;
  552. os_memcpy(query->addr, dst, ETH_ALEN);
  553. query->dialog_token = dialog_token;
  554. query->freq = freq;
  555. query->cb = cb;
  556. query->ctx = ctx;
  557. query->req = req;
  558. dl_list_add(&gas->pending, &query->list);
  559. *(wpabuf_mhead_u8(req) + 2) = dialog_token;
  560. wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
  561. " dialog_token=%u freq=%d",
  562. MAC2STR(query->addr), query->dialog_token, query->freq);
  563. if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
  564. query) < 0) {
  565. gas_query_free(query, 1);
  566. return -1;
  567. }
  568. return dialog_token;
  569. }
  570. /**
  571. * gas_query_cancel - Cancel a pending GAS query
  572. * @gas: GAS query data from gas_query_init()
  573. * @dst: Destination MAC address for the query
  574. * @dialog_token: Dialog token from gas_query_req()
  575. */
  576. void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
  577. {
  578. struct gas_query_pending *query;
  579. query = gas_query_get_pending(gas, dst, dialog_token);
  580. if (query)
  581. gas_query_done(gas, query, GAS_QUERY_CANCELLED);
  582. }