gas_query.c 23 KB

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