gas_query.c 20 KB

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