gas_query.c 18 KB

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