gas_query.c 18 KB

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