gas_query.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. unsigned int left;
  372. if (gas == NULL || len < 4)
  373. return -1;
  374. prot = categ == WLAN_ACTION_PROTECTED_DUAL;
  375. pmf = pmf_in_use(gas->wpa_s, bssid);
  376. if (prot && !pmf) {
  377. wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
  378. return 0;
  379. }
  380. if (!prot && pmf) {
  381. wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
  382. return 0;
  383. }
  384. pos = data;
  385. action = *pos++;
  386. dialog_token = *pos++;
  387. if (action != WLAN_PA_GAS_INITIAL_RESP &&
  388. action != WLAN_PA_GAS_COMEBACK_RESP)
  389. return -1; /* Not a GAS response */
  390. query = gas_query_get_pending(gas, sa, dialog_token);
  391. if (query == NULL) {
  392. wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
  393. " dialog token %u", MAC2STR(sa), dialog_token);
  394. return -1;
  395. }
  396. wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
  397. ms_from_time(&query->last_oper), MAC2STR(sa));
  398. if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
  399. wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
  400. MACSTR " dialog token %u when waiting for comeback "
  401. "response", MAC2STR(sa), dialog_token);
  402. return 0;
  403. }
  404. if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
  405. wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
  406. MACSTR " dialog token %u when waiting for initial "
  407. "response", MAC2STR(sa), dialog_token);
  408. return 0;
  409. }
  410. query->status_code = WPA_GET_LE16(pos);
  411. pos += 2;
  412. if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
  413. action == WLAN_PA_GAS_COMEBACK_RESP) {
  414. wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
  415. } else if (query->status_code != WLAN_STATUS_SUCCESS) {
  416. wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
  417. "%u failed - status code %u",
  418. MAC2STR(sa), dialog_token, query->status_code);
  419. gas_query_done(gas, query, GAS_QUERY_FAILURE);
  420. return 0;
  421. }
  422. if (action == WLAN_PA_GAS_COMEBACK_RESP) {
  423. if (pos + 1 > data + len)
  424. return 0;
  425. frag_id = *pos & 0x7f;
  426. more_frags = (*pos & 0x80) >> 7;
  427. pos++;
  428. }
  429. /* Comeback Delay */
  430. if (pos + 2 > data + len)
  431. return 0;
  432. comeback_delay = WPA_GET_LE16(pos);
  433. pos += 2;
  434. /* Advertisement Protocol element */
  435. if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
  436. wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
  437. "Protocol element in the response from " MACSTR,
  438. MAC2STR(sa));
  439. return 0;
  440. }
  441. if (*pos != WLAN_EID_ADV_PROTO) {
  442. wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
  443. "Protocol element ID %u in response from " MACSTR,
  444. *pos, MAC2STR(sa));
  445. return 0;
  446. }
  447. adv_proto = pos;
  448. pos += 2 + pos[1];
  449. /* Query Response Length */
  450. if (pos + 2 > data + len) {
  451. wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
  452. return 0;
  453. }
  454. resp_len = WPA_GET_LE16(pos);
  455. pos += 2;
  456. left = data + len - pos;
  457. if (resp_len > left) {
  458. wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
  459. "response from " MACSTR, MAC2STR(sa));
  460. return 0;
  461. }
  462. if (resp_len < left) {
  463. wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
  464. "after Query Response from " MACSTR,
  465. left - resp_len, MAC2STR(sa));
  466. }
  467. if (action == WLAN_PA_GAS_COMEBACK_RESP)
  468. gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
  469. frag_id, more_frags, comeback_delay);
  470. else
  471. gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
  472. comeback_delay);
  473. return 0;
  474. }
  475. static void gas_query_timeout(void *eloop_data, void *user_ctx)
  476. {
  477. struct gas_query *gas = eloop_data;
  478. struct gas_query_pending *query = user_ctx;
  479. wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
  480. " dialog token %u",
  481. MAC2STR(query->addr), query->dialog_token);
  482. gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
  483. }
  484. static int gas_query_dialog_token_available(struct gas_query *gas,
  485. const u8 *dst, u8 dialog_token)
  486. {
  487. struct gas_query_pending *q;
  488. dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
  489. if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
  490. dialog_token == q->dialog_token)
  491. return 0;
  492. }
  493. return 1;
  494. }
  495. static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
  496. {
  497. struct gas_query_pending *query = work->ctx;
  498. struct gas_query *gas = query->gas;
  499. struct wpa_supplicant *wpa_s = gas->wpa_s;
  500. if (deinit) {
  501. if (work->started) {
  502. gas->work = NULL;
  503. gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
  504. return;
  505. }
  506. gas_query_free(query, 1);
  507. return;
  508. }
  509. if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
  510. wpa_msg(wpa_s, MSG_INFO,
  511. "Failed to assign random MAC address for GAS");
  512. gas_query_free(query, 1);
  513. radio_work_done(work);
  514. return;
  515. }
  516. gas->work = work;
  517. if (gas_query_tx(gas, query, query->req) < 0) {
  518. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  519. MACSTR, MAC2STR(query->addr));
  520. gas_query_free(query, 1);
  521. return;
  522. }
  523. gas->current = query;
  524. wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
  525. query->dialog_token);
  526. eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
  527. gas_query_timeout, gas, query);
  528. }
  529. /**
  530. * gas_query_req - Request a GAS query
  531. * @gas: GAS query data from gas_query_init()
  532. * @dst: Destination MAC address for the query
  533. * @freq: Frequency (in MHz) for the channel on which to send the query
  534. * @req: GAS query payload (to be freed by gas_query module in case of success
  535. * return)
  536. * @cb: Callback function for reporting GAS query result and response
  537. * @ctx: Context pointer to use with the @cb call
  538. * Returns: dialog token (>= 0) on success or -1 on failure
  539. */
  540. int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
  541. struct wpabuf *req,
  542. void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
  543. enum gas_query_result result,
  544. const struct wpabuf *adv_proto,
  545. const struct wpabuf *resp, u16 status_code),
  546. void *ctx)
  547. {
  548. struct gas_query_pending *query;
  549. int dialog_token;
  550. static int next_start = 0;
  551. if (wpabuf_len(req) < 3)
  552. return -1;
  553. for (dialog_token = 0; dialog_token < 256; dialog_token++) {
  554. if (gas_query_dialog_token_available(
  555. gas, dst, (next_start + dialog_token) % 256))
  556. break;
  557. }
  558. if (dialog_token == 256)
  559. return -1; /* Too many pending queries */
  560. dialog_token = (next_start + dialog_token) % 256;
  561. next_start = (dialog_token + 1) % 256;
  562. query = os_zalloc(sizeof(*query));
  563. if (query == NULL)
  564. return -1;
  565. query->gas = gas;
  566. os_memcpy(query->addr, dst, ETH_ALEN);
  567. query->dialog_token = dialog_token;
  568. query->freq = freq;
  569. query->cb = cb;
  570. query->ctx = ctx;
  571. query->req = req;
  572. dl_list_add(&gas->pending, &query->list);
  573. *(wpabuf_mhead_u8(req) + 2) = dialog_token;
  574. wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
  575. " dialog_token=%u freq=%d",
  576. MAC2STR(query->addr), query->dialog_token, query->freq);
  577. if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
  578. query) < 0) {
  579. gas_query_free(query, 1);
  580. return -1;
  581. }
  582. return dialog_token;
  583. }
  584. /**
  585. * gas_query_cancel - Cancel a pending GAS query
  586. * @gas: GAS query data from gas_query_init()
  587. * @dst: Destination MAC address for the query
  588. * @dialog_token: Dialog token from gas_query_req()
  589. */
  590. void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
  591. {
  592. struct gas_query_pending *query;
  593. query = gas_query_get_pending(gas, dst, dialog_token);
  594. if (query)
  595. gas_query_done(gas, query, GAS_QUERY_CANCELLED);
  596. }