gas_query.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. unsigned int wait_time;
  214. int res, prot = pmf_in_use(gas->wpa_s, query->addr);
  215. wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
  216. "freq=%d prot=%d", MAC2STR(query->addr),
  217. (unsigned int) wpabuf_len(req), query->freq, prot);
  218. if (prot) {
  219. u8 *categ = wpabuf_mhead_u8(req);
  220. *categ = WLAN_ACTION_PROTECTED_DUAL;
  221. }
  222. os_get_reltime(&query->last_oper);
  223. wait_time = 1000;
  224. if (gas->wpa_s->max_remain_on_chan &&
  225. wait_time > gas->wpa_s->max_remain_on_chan)
  226. wait_time = gas->wpa_s->max_remain_on_chan;
  227. res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
  228. gas->wpa_s->own_addr, query->addr,
  229. wpabuf_head(req), wpabuf_len(req),
  230. wait_time, gas_query_tx_status, 0);
  231. if (res == 0)
  232. query->offchannel_tx_started = 1;
  233. return res;
  234. }
  235. static void gas_query_tx_comeback_req(struct gas_query *gas,
  236. struct gas_query_pending *query)
  237. {
  238. struct wpabuf *req;
  239. req = gas_build_comeback_req(query->dialog_token);
  240. if (req == NULL) {
  241. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  242. return;
  243. }
  244. if (gas_query_tx(gas, query, req) < 0) {
  245. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  246. MACSTR, MAC2STR(query->addr));
  247. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  248. }
  249. wpabuf_free(req);
  250. }
  251. static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
  252. {
  253. struct gas_query *gas = eloop_data;
  254. struct gas_query_pending *query = user_ctx;
  255. wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
  256. MAC2STR(query->addr));
  257. gas_query_tx_comeback_req(gas, query);
  258. }
  259. static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
  260. struct gas_query_pending *query,
  261. u16 comeback_delay)
  262. {
  263. unsigned int secs, usecs;
  264. secs = (comeback_delay * 1024) / 1000000;
  265. usecs = comeback_delay * 1024 - secs * 1000000;
  266. wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
  267. " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
  268. eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
  269. eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
  270. gas, query);
  271. }
  272. static void gas_query_rx_initial(struct gas_query *gas,
  273. struct gas_query_pending *query,
  274. const u8 *adv_proto, const u8 *resp,
  275. size_t len, u16 comeback_delay)
  276. {
  277. wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
  278. MACSTR " (dialog_token=%u comeback_delay=%u)",
  279. MAC2STR(query->addr), query->dialog_token, comeback_delay);
  280. query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
  281. if (query->adv_proto == NULL) {
  282. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  283. return;
  284. }
  285. if (comeback_delay) {
  286. query->wait_comeback = 1;
  287. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  288. return;
  289. }
  290. /* Query was completed without comeback mechanism */
  291. if (gas_query_append(query, resp, len) < 0) {
  292. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  293. return;
  294. }
  295. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  296. }
  297. static void gas_query_rx_comeback(struct gas_query *gas,
  298. struct gas_query_pending *query,
  299. const u8 *adv_proto, const u8 *resp,
  300. size_t len, u8 frag_id, u8 more_frags,
  301. u16 comeback_delay)
  302. {
  303. wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
  304. MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
  305. "comeback_delay=%u)",
  306. MAC2STR(query->addr), query->dialog_token, frag_id,
  307. more_frags, comeback_delay);
  308. if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
  309. os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
  310. wpabuf_len(query->adv_proto)) != 0) {
  311. wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
  312. "between initial and comeback response from "
  313. MACSTR, MAC2STR(query->addr));
  314. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  315. return;
  316. }
  317. if (comeback_delay) {
  318. if (frag_id) {
  319. wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
  320. "with non-zero frag_id and comeback_delay "
  321. "from " MACSTR, MAC2STR(query->addr));
  322. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  323. return;
  324. }
  325. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  326. return;
  327. }
  328. if (frag_id != query->next_frag_id) {
  329. wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
  330. "from " MACSTR, MAC2STR(query->addr));
  331. if (frag_id + 1 == query->next_frag_id) {
  332. wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
  333. "retry of previous fragment");
  334. return;
  335. }
  336. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  337. return;
  338. }
  339. query->next_frag_id++;
  340. if (gas_query_append(query, resp, len) < 0) {
  341. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  342. return;
  343. }
  344. if (more_frags) {
  345. gas_query_tx_comeback_req(gas, query);
  346. return;
  347. }
  348. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  349. }
  350. /**
  351. * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
  352. * @gas: GAS query data from gas_query_init()
  353. * @da: Destination MAC address of the Action frame
  354. * @sa: Source MAC address of the Action frame
  355. * @bssid: BSSID of the Action frame
  356. * @categ: Category of the Action frame
  357. * @data: Payload of the Action frame
  358. * @len: Length of @data
  359. * @freq: Frequency (in MHz) on which the frame was received
  360. * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
  361. */
  362. int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
  363. const u8 *bssid, u8 categ, const u8 *data, size_t len,
  364. int freq)
  365. {
  366. struct gas_query_pending *query;
  367. u8 action, dialog_token, frag_id = 0, more_frags = 0;
  368. u16 comeback_delay, resp_len;
  369. const u8 *pos, *adv_proto;
  370. int prot, pmf;
  371. if (gas == NULL || len < 4)
  372. return -1;
  373. prot = categ == WLAN_ACTION_PROTECTED_DUAL;
  374. pmf = pmf_in_use(gas->wpa_s, bssid);
  375. if (prot && !pmf) {
  376. wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
  377. return 0;
  378. }
  379. if (!prot && pmf) {
  380. wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
  381. return 0;
  382. }
  383. pos = data;
  384. action = *pos++;
  385. dialog_token = *pos++;
  386. if (action != WLAN_PA_GAS_INITIAL_RESP &&
  387. action != WLAN_PA_GAS_COMEBACK_RESP)
  388. return -1; /* Not a GAS response */
  389. query = gas_query_get_pending(gas, sa, dialog_token);
  390. if (query == NULL) {
  391. wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
  392. " dialog token %u", MAC2STR(sa), dialog_token);
  393. return -1;
  394. }
  395. wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
  396. ms_from_time(&query->last_oper), MAC2STR(sa));
  397. if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
  398. wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
  399. MACSTR " dialog token %u when waiting for comeback "
  400. "response", MAC2STR(sa), dialog_token);
  401. return 0;
  402. }
  403. if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
  404. wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
  405. MACSTR " dialog token %u when waiting for initial "
  406. "response", MAC2STR(sa), dialog_token);
  407. return 0;
  408. }
  409. query->status_code = WPA_GET_LE16(pos);
  410. pos += 2;
  411. if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
  412. action == WLAN_PA_GAS_COMEBACK_RESP) {
  413. wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
  414. } else if (query->status_code != WLAN_STATUS_SUCCESS) {
  415. wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
  416. "%u failed - status code %u",
  417. MAC2STR(sa), dialog_token, query->status_code);
  418. gas_query_done(gas, query, GAS_QUERY_FAILURE);
  419. return 0;
  420. }
  421. if (action == WLAN_PA_GAS_COMEBACK_RESP) {
  422. if (pos + 1 > data + len)
  423. return 0;
  424. frag_id = *pos & 0x7f;
  425. more_frags = (*pos & 0x80) >> 7;
  426. pos++;
  427. }
  428. /* Comeback Delay */
  429. if (pos + 2 > data + len)
  430. return 0;
  431. comeback_delay = WPA_GET_LE16(pos);
  432. pos += 2;
  433. /* Advertisement Protocol element */
  434. if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
  435. wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
  436. "Protocol element in the response from " MACSTR,
  437. MAC2STR(sa));
  438. return 0;
  439. }
  440. if (*pos != WLAN_EID_ADV_PROTO) {
  441. wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
  442. "Protocol element ID %u in response from " MACSTR,
  443. *pos, MAC2STR(sa));
  444. return 0;
  445. }
  446. adv_proto = pos;
  447. pos += 2 + pos[1];
  448. /* Query Response Length */
  449. if (pos + 2 > data + len) {
  450. wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
  451. return 0;
  452. }
  453. resp_len = WPA_GET_LE16(pos);
  454. pos += 2;
  455. if (pos + resp_len > data + len) {
  456. wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
  457. "response from " MACSTR, MAC2STR(sa));
  458. return 0;
  459. }
  460. if (pos + resp_len < data + len) {
  461. wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
  462. "after Query Response from " MACSTR,
  463. (unsigned int) (data + len - pos - resp_len),
  464. MAC2STR(sa));
  465. }
  466. if (action == WLAN_PA_GAS_COMEBACK_RESP)
  467. gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
  468. frag_id, more_frags, comeback_delay);
  469. else
  470. gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
  471. comeback_delay);
  472. return 0;
  473. }
  474. static void gas_query_timeout(void *eloop_data, void *user_ctx)
  475. {
  476. struct gas_query *gas = eloop_data;
  477. struct gas_query_pending *query = user_ctx;
  478. wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
  479. " dialog token %u",
  480. MAC2STR(query->addr), query->dialog_token);
  481. gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
  482. }
  483. static int gas_query_dialog_token_available(struct gas_query *gas,
  484. const u8 *dst, u8 dialog_token)
  485. {
  486. struct gas_query_pending *q;
  487. dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
  488. if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
  489. dialog_token == q->dialog_token)
  490. return 0;
  491. }
  492. return 1;
  493. }
  494. static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
  495. {
  496. struct gas_query_pending *query = work->ctx;
  497. struct gas_query *gas = query->gas;
  498. struct wpa_supplicant *wpa_s = gas->wpa_s;
  499. if (deinit) {
  500. if (work->started) {
  501. gas->work = NULL;
  502. gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
  503. return;
  504. }
  505. gas_query_free(query, 1);
  506. return;
  507. }
  508. if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
  509. wpa_msg(wpa_s, MSG_INFO,
  510. "Failed to assign random MAC address for GAS");
  511. gas_query_free(query, 1);
  512. radio_work_done(work);
  513. return;
  514. }
  515. gas->work = work;
  516. if (gas_query_tx(gas, query, query->req) < 0) {
  517. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  518. MACSTR, MAC2STR(query->addr));
  519. gas_query_free(query, 1);
  520. return;
  521. }
  522. gas->current = query;
  523. wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
  524. query->dialog_token);
  525. eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
  526. gas_query_timeout, gas, query);
  527. }
  528. /**
  529. * gas_query_req - Request a GAS query
  530. * @gas: GAS query data from gas_query_init()
  531. * @dst: Destination MAC address for the query
  532. * @freq: Frequency (in MHz) for the channel on which to send the query
  533. * @req: GAS query payload (to be freed by gas_query module in case of success
  534. * return)
  535. * @cb: Callback function for reporting GAS query result and response
  536. * @ctx: Context pointer to use with the @cb call
  537. * Returns: dialog token (>= 0) on success or -1 on failure
  538. */
  539. int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
  540. struct wpabuf *req,
  541. void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
  542. enum gas_query_result result,
  543. const struct wpabuf *adv_proto,
  544. const struct wpabuf *resp, u16 status_code),
  545. void *ctx)
  546. {
  547. struct gas_query_pending *query;
  548. int dialog_token;
  549. static int next_start = 0;
  550. if (wpabuf_len(req) < 3)
  551. return -1;
  552. for (dialog_token = 0; dialog_token < 256; dialog_token++) {
  553. if (gas_query_dialog_token_available(
  554. gas, dst, (next_start + dialog_token) % 256))
  555. break;
  556. }
  557. if (dialog_token == 256)
  558. return -1; /* Too many pending queries */
  559. dialog_token = (next_start + dialog_token) % 256;
  560. next_start = (dialog_token + 1) % 256;
  561. query = os_zalloc(sizeof(*query));
  562. if (query == NULL)
  563. return -1;
  564. query->gas = gas;
  565. os_memcpy(query->addr, dst, ETH_ALEN);
  566. query->dialog_token = dialog_token;
  567. query->freq = freq;
  568. query->cb = cb;
  569. query->ctx = ctx;
  570. query->req = req;
  571. dl_list_add(&gas->pending, &query->list);
  572. *(wpabuf_mhead_u8(req) + 2) = dialog_token;
  573. wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
  574. " dialog_token=%u freq=%d",
  575. MAC2STR(query->addr), query->dialog_token, query->freq);
  576. if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
  577. query) < 0) {
  578. gas_query_free(query, 1);
  579. return -1;
  580. }
  581. return dialog_token;
  582. }
  583. /**
  584. * gas_query_cancel - Cancel a pending GAS query
  585. * @gas: GAS query data from gas_query_init()
  586. * @dst: Destination MAC address for the query
  587. * @dialog_token: Dialog token from gas_query_req()
  588. */
  589. void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
  590. {
  591. struct gas_query_pending *query;
  592. query = gas_query_get_pending(gas, dst, dialog_token);
  593. if (query)
  594. gas_query_done(gas, query, GAS_QUERY_CANCELLED);
  595. }