gas_query.c 24 KB

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