gas_query.c 21 KB

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