gas_query.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. query->wait_comeback = 1;
  344. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  345. return;
  346. }
  347. /* Query was completed without comeback mechanism */
  348. if (gas_query_append(query, resp, len) < 0) {
  349. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  350. return;
  351. }
  352. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  353. }
  354. static void gas_query_rx_comeback(struct gas_query *gas,
  355. struct gas_query_pending *query,
  356. const u8 *adv_proto, const u8 *resp,
  357. size_t len, u8 frag_id, u8 more_frags,
  358. u16 comeback_delay)
  359. {
  360. wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
  361. MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
  362. "comeback_delay=%u)",
  363. MAC2STR(query->addr), query->dialog_token, frag_id,
  364. more_frags, comeback_delay);
  365. eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
  366. if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
  367. os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
  368. wpabuf_len(query->adv_proto)) != 0) {
  369. wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
  370. "between initial and comeback response from "
  371. MACSTR, MAC2STR(query->addr));
  372. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  373. return;
  374. }
  375. if (comeback_delay) {
  376. if (frag_id) {
  377. wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
  378. "with non-zero frag_id and comeback_delay "
  379. "from " MACSTR, MAC2STR(query->addr));
  380. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  381. return;
  382. }
  383. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  384. return;
  385. }
  386. if (frag_id != query->next_frag_id) {
  387. wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
  388. "from " MACSTR, MAC2STR(query->addr));
  389. if (frag_id + 1 == query->next_frag_id) {
  390. wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
  391. "retry of previous fragment");
  392. return;
  393. }
  394. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  395. return;
  396. }
  397. query->next_frag_id++;
  398. if (gas_query_append(query, resp, len) < 0) {
  399. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  400. return;
  401. }
  402. if (more_frags) {
  403. gas_query_tx_comeback_req(gas, query);
  404. return;
  405. }
  406. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  407. }
  408. /**
  409. * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
  410. * @gas: GAS query data from gas_query_init()
  411. * @da: Destination MAC address of the Action frame
  412. * @sa: Source MAC address of the Action frame
  413. * @bssid: BSSID of the Action frame
  414. * @categ: Category of the Action frame
  415. * @data: Payload of the Action frame
  416. * @len: Length of @data
  417. * @freq: Frequency (in MHz) on which the frame was received
  418. * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
  419. */
  420. int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
  421. const u8 *bssid, u8 categ, const u8 *data, size_t len,
  422. int freq)
  423. {
  424. struct gas_query_pending *query;
  425. u8 action, dialog_token, frag_id = 0, more_frags = 0;
  426. u16 comeback_delay, resp_len;
  427. const u8 *pos, *adv_proto;
  428. int prot, pmf;
  429. unsigned int left;
  430. if (gas == NULL || len < 4)
  431. return -1;
  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. prot = categ == WLAN_ACTION_PROTECTED_DUAL;
  439. pmf = pmf_in_use(gas->wpa_s, sa);
  440. if (prot && !pmf) {
  441. wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
  442. return 0;
  443. }
  444. if (!prot && pmf) {
  445. wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
  446. return 0;
  447. }
  448. query = gas_query_get_pending(gas, sa, dialog_token);
  449. if (query == NULL) {
  450. wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
  451. " dialog token %u", MAC2STR(sa), dialog_token);
  452. return -1;
  453. }
  454. wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
  455. ms_from_time(&query->last_oper), MAC2STR(sa));
  456. if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
  457. wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
  458. MACSTR " dialog token %u when waiting for comeback "
  459. "response", MAC2STR(sa), dialog_token);
  460. return 0;
  461. }
  462. if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
  463. wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
  464. MACSTR " dialog token %u when waiting for initial "
  465. "response", MAC2STR(sa), dialog_token);
  466. return 0;
  467. }
  468. query->status_code = WPA_GET_LE16(pos);
  469. pos += 2;
  470. if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
  471. action == WLAN_PA_GAS_COMEBACK_RESP) {
  472. wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
  473. } else if (query->status_code != WLAN_STATUS_SUCCESS) {
  474. wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
  475. "%u failed - status code %u",
  476. MAC2STR(sa), dialog_token, query->status_code);
  477. gas_query_done(gas, query, GAS_QUERY_FAILURE);
  478. return 0;
  479. }
  480. if (action == WLAN_PA_GAS_COMEBACK_RESP) {
  481. if (pos + 1 > data + len)
  482. return 0;
  483. frag_id = *pos & 0x7f;
  484. more_frags = (*pos & 0x80) >> 7;
  485. pos++;
  486. }
  487. /* Comeback Delay */
  488. if (pos + 2 > data + len)
  489. return 0;
  490. comeback_delay = WPA_GET_LE16(pos);
  491. pos += 2;
  492. /* Advertisement Protocol element */
  493. if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
  494. wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
  495. "Protocol element in the response from " MACSTR,
  496. MAC2STR(sa));
  497. return 0;
  498. }
  499. if (*pos != WLAN_EID_ADV_PROTO) {
  500. wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
  501. "Protocol element ID %u in response from " MACSTR,
  502. *pos, MAC2STR(sa));
  503. return 0;
  504. }
  505. adv_proto = pos;
  506. pos += 2 + pos[1];
  507. /* Query Response Length */
  508. if (pos + 2 > data + len) {
  509. wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
  510. return 0;
  511. }
  512. resp_len = WPA_GET_LE16(pos);
  513. pos += 2;
  514. left = data + len - pos;
  515. if (resp_len > left) {
  516. wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
  517. "response from " MACSTR, MAC2STR(sa));
  518. return 0;
  519. }
  520. if (resp_len < left) {
  521. wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
  522. "after Query Response from " MACSTR,
  523. left - resp_len, MAC2STR(sa));
  524. }
  525. if (action == WLAN_PA_GAS_COMEBACK_RESP)
  526. gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
  527. frag_id, more_frags, comeback_delay);
  528. else
  529. gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
  530. comeback_delay);
  531. return 0;
  532. }
  533. static void gas_query_timeout(void *eloop_data, void *user_ctx)
  534. {
  535. struct gas_query *gas = eloop_data;
  536. struct gas_query_pending *query = user_ctx;
  537. wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
  538. " dialog token %u",
  539. MAC2STR(query->addr), query->dialog_token);
  540. gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
  541. }
  542. static int gas_query_dialog_token_available(struct gas_query *gas,
  543. const u8 *dst, u8 dialog_token)
  544. {
  545. struct gas_query_pending *q;
  546. dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
  547. if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
  548. dialog_token == q->dialog_token)
  549. return 0;
  550. }
  551. return 1;
  552. }
  553. static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
  554. {
  555. struct gas_query_pending *query = work->ctx;
  556. struct gas_query *gas = query->gas;
  557. struct wpa_supplicant *wpa_s = gas->wpa_s;
  558. if (deinit) {
  559. if (work->started) {
  560. gas->work = NULL;
  561. gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
  562. return;
  563. }
  564. gas_query_free(query, 1);
  565. return;
  566. }
  567. if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
  568. wpa_msg(wpa_s, MSG_INFO,
  569. "Failed to assign random MAC address for GAS");
  570. gas_query_free(query, 1);
  571. radio_work_done(work);
  572. return;
  573. }
  574. gas->work = work;
  575. gas_query_tx_initial_req(gas, query);
  576. }
  577. static void gas_query_tx_initial_req(struct gas_query *gas,
  578. struct gas_query_pending *query)
  579. {
  580. if (gas_query_tx(gas, query, query->req,
  581. GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
  582. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  583. MACSTR, MAC2STR(query->addr));
  584. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  585. return;
  586. }
  587. gas->current = query;
  588. wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
  589. query->dialog_token);
  590. eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
  591. gas_query_timeout, gas, query);
  592. }
  593. static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
  594. {
  595. static int next_start = 0;
  596. int dialog_token;
  597. for (dialog_token = 0; dialog_token < 256; dialog_token++) {
  598. if (gas_query_dialog_token_available(
  599. gas, dst, (next_start + dialog_token) % 256))
  600. break;
  601. }
  602. if (dialog_token == 256)
  603. return -1; /* Too many pending queries */
  604. dialog_token = (next_start + dialog_token) % 256;
  605. next_start = (dialog_token + 1) % 256;
  606. return dialog_token;
  607. }
  608. /**
  609. * gas_query_req - Request a GAS query
  610. * @gas: GAS query data from gas_query_init()
  611. * @dst: Destination MAC address for the query
  612. * @freq: Frequency (in MHz) for the channel on which to send the query
  613. * @req: GAS query payload (to be freed by gas_query module in case of success
  614. * return)
  615. * @cb: Callback function for reporting GAS query result and response
  616. * @ctx: Context pointer to use with the @cb call
  617. * Returns: dialog token (>= 0) on success or -1 on failure
  618. */
  619. int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
  620. struct wpabuf *req,
  621. void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
  622. enum gas_query_result result,
  623. const struct wpabuf *adv_proto,
  624. const struct wpabuf *resp, u16 status_code),
  625. void *ctx)
  626. {
  627. struct gas_query_pending *query;
  628. int dialog_token;
  629. if (wpabuf_len(req) < 3)
  630. return -1;
  631. dialog_token = gas_query_new_dialog_token(gas, dst);
  632. if (dialog_token < 0)
  633. return -1;
  634. query = os_zalloc(sizeof(*query));
  635. if (query == NULL)
  636. return -1;
  637. query->gas = gas;
  638. os_memcpy(query->addr, dst, ETH_ALEN);
  639. query->dialog_token = dialog_token;
  640. query->freq = freq;
  641. query->cb = cb;
  642. query->ctx = ctx;
  643. query->req = req;
  644. dl_list_add(&gas->pending, &query->list);
  645. *(wpabuf_mhead_u8(req) + 2) = dialog_token;
  646. wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
  647. " dialog_token=%u freq=%d",
  648. MAC2STR(query->addr), query->dialog_token, query->freq);
  649. if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
  650. query) < 0) {
  651. query->req = NULL; /* caller will free this in error case */
  652. gas_query_free(query, 1);
  653. return -1;
  654. }
  655. return dialog_token;
  656. }