gas_query.c 21 KB

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