gas_query.c 19 KB

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