gas_query.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Generic advertisement service (GAS) query
  3. * Copyright (c) 2009, Atheros Communications
  4. * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "utils/eloop.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/gas.h"
  14. #include "wpa_supplicant_i.h"
  15. #include "driver_i.h"
  16. #include "offchannel.h"
  17. #include "gas_query.h"
  18. /** GAS query timeout in seconds */
  19. #define GAS_QUERY_TIMEOUT_PERIOD 2
  20. /** Retry period for GAS query requests in milliseconds */
  21. #define GAS_SERVICE_RETRY_PERIOD_MS 500
  22. /**
  23. * struct gas_query_pending - Pending GAS query
  24. */
  25. struct gas_query_pending {
  26. struct dl_list list;
  27. u8 addr[ETH_ALEN];
  28. u8 dialog_token;
  29. u8 next_frag_id;
  30. unsigned int wait_comeback:1;
  31. unsigned int offchannel_tx_started:1;
  32. int freq;
  33. u16 status_code;
  34. struct wpabuf *req;
  35. struct wpabuf *adv_proto;
  36. struct wpabuf *resp;
  37. void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
  38. enum gas_query_result result,
  39. const struct wpabuf *adv_proto,
  40. const struct wpabuf *resp, u16 status_code);
  41. void *ctx;
  42. };
  43. /**
  44. * struct gas_query - Internal GAS query data
  45. */
  46. struct gas_query {
  47. struct wpa_supplicant *wpa_s;
  48. struct dl_list pending; /* struct gas_query_pending */
  49. struct gas_query_pending *current;
  50. };
  51. static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
  52. static void gas_query_timeout(void *eloop_data, void *user_ctx);
  53. static void gas_service_timeout(void *eloop_data, void *user_ctx);
  54. /**
  55. * gas_query_init - Initialize GAS query component
  56. * @wpa_s: Pointer to wpa_supplicant data
  57. * Returns: Pointer to GAS query data or %NULL on failure
  58. */
  59. struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
  60. {
  61. struct gas_query *gas;
  62. gas = os_zalloc(sizeof(*gas));
  63. if (gas == NULL)
  64. return NULL;
  65. gas->wpa_s = wpa_s;
  66. dl_list_init(&gas->pending);
  67. return gas;
  68. }
  69. static void gas_query_done(struct gas_query *gas,
  70. struct gas_query_pending *query,
  71. enum gas_query_result result)
  72. {
  73. if (gas->current == query)
  74. gas->current = NULL;
  75. if (query->offchannel_tx_started)
  76. offchannel_send_action_done(gas->wpa_s);
  77. eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
  78. eloop_cancel_timeout(gas_query_timeout, gas, query);
  79. eloop_cancel_timeout(gas_service_timeout, gas, query);
  80. dl_list_del(&query->list);
  81. query->cb(query->ctx, query->addr, query->dialog_token, result,
  82. query->adv_proto, query->resp, query->status_code);
  83. wpabuf_free(query->req);
  84. wpabuf_free(query->adv_proto);
  85. wpabuf_free(query->resp);
  86. os_free(query);
  87. }
  88. /**
  89. * gas_query_deinit - Deinitialize GAS query component
  90. * @gas: GAS query data from gas_query_init()
  91. */
  92. void gas_query_deinit(struct gas_query *gas)
  93. {
  94. struct gas_query_pending *query, *next;
  95. if (gas == NULL)
  96. return;
  97. dl_list_for_each_safe(query, next, &gas->pending,
  98. struct gas_query_pending, list)
  99. gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
  100. os_free(gas);
  101. }
  102. static struct gas_query_pending *
  103. gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
  104. {
  105. struct gas_query_pending *q;
  106. dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
  107. if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
  108. q->dialog_token == dialog_token)
  109. return q;
  110. }
  111. return NULL;
  112. }
  113. static int gas_query_append(struct gas_query_pending *query, const u8 *data,
  114. size_t len)
  115. {
  116. if (wpabuf_resize(&query->resp, len) < 0) {
  117. wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
  118. return -1;
  119. }
  120. wpabuf_put_data(query->resp, data, len);
  121. return 0;
  122. }
  123. static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
  124. unsigned int freq, const u8 *dst,
  125. const u8 *src, const u8 *bssid,
  126. const u8 *data, size_t data_len,
  127. enum offchannel_send_action_result result)
  128. {
  129. struct gas_query_pending *query;
  130. struct gas_query *gas = wpa_s->gas;
  131. if (gas->current == NULL) {
  132. wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
  133. MACSTR " result=%d - no query in progress",
  134. freq, MAC2STR(dst), result);
  135. return;
  136. }
  137. query = gas->current;
  138. wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
  139. " result=%d query=%p dialog_token=%u",
  140. freq, MAC2STR(dst), result, query, query->dialog_token);
  141. if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
  142. wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
  143. return;
  144. }
  145. if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
  146. eloop_cancel_timeout(gas_query_timeout, gas, query);
  147. eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
  148. gas_query_timeout, gas, query);
  149. }
  150. if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
  151. eloop_cancel_timeout(gas_query_timeout, gas, query);
  152. eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
  153. }
  154. }
  155. static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
  156. struct wpabuf *req)
  157. {
  158. int res;
  159. wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
  160. "freq=%d", MAC2STR(query->addr),
  161. (unsigned int) wpabuf_len(req), query->freq);
  162. res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
  163. gas->wpa_s->own_addr, query->addr,
  164. wpabuf_head(req), wpabuf_len(req), 1000,
  165. gas_query_tx_status, 0);
  166. if (res == 0)
  167. query->offchannel_tx_started = 1;
  168. return res;
  169. }
  170. static void gas_query_tx_comeback_req(struct gas_query *gas,
  171. struct gas_query_pending *query)
  172. {
  173. struct wpabuf *req;
  174. req = gas_build_comeback_req(query->dialog_token);
  175. if (req == NULL) {
  176. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  177. return;
  178. }
  179. if (gas_query_tx(gas, query, req) < 0) {
  180. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  181. MACSTR, MAC2STR(query->addr));
  182. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  183. }
  184. wpabuf_free(req);
  185. }
  186. static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
  187. {
  188. struct gas_query *gas = eloop_data;
  189. struct gas_query_pending *query = user_ctx;
  190. wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
  191. MAC2STR(query->addr));
  192. gas_query_tx_comeback_req(gas, query);
  193. }
  194. static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
  195. struct gas_query_pending *query,
  196. u16 comeback_delay)
  197. {
  198. unsigned int secs, usecs;
  199. secs = (comeback_delay * 1024) / 1000000;
  200. usecs = comeback_delay * 1024 - secs * 1000000;
  201. wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
  202. " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
  203. eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
  204. eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
  205. gas, query);
  206. }
  207. static void gas_query_rx_initial(struct gas_query *gas,
  208. struct gas_query_pending *query,
  209. const u8 *adv_proto, const u8 *resp,
  210. size_t len, u16 comeback_delay)
  211. {
  212. wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
  213. MACSTR " (dialog_token=%u comeback_delay=%u)",
  214. MAC2STR(query->addr), query->dialog_token, comeback_delay);
  215. query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
  216. if (query->adv_proto == NULL) {
  217. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  218. return;
  219. }
  220. if (comeback_delay) {
  221. query->wait_comeback = 1;
  222. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  223. return;
  224. }
  225. /* Query was completed without comeback mechanism */
  226. if (gas_query_append(query, resp, len) < 0) {
  227. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  228. return;
  229. }
  230. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  231. }
  232. static void gas_query_rx_comeback(struct gas_query *gas,
  233. struct gas_query_pending *query,
  234. const u8 *adv_proto, const u8 *resp,
  235. size_t len, u8 frag_id, u8 more_frags,
  236. u16 comeback_delay)
  237. {
  238. wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
  239. MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
  240. "comeback_delay=%u)",
  241. MAC2STR(query->addr), query->dialog_token, frag_id,
  242. more_frags, comeback_delay);
  243. if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
  244. os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
  245. wpabuf_len(query->adv_proto)) != 0) {
  246. wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
  247. "between initial and comeback response from "
  248. MACSTR, MAC2STR(query->addr));
  249. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  250. return;
  251. }
  252. if (comeback_delay) {
  253. if (frag_id) {
  254. wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
  255. "with non-zero frag_id and comeback_delay "
  256. "from " MACSTR, MAC2STR(query->addr));
  257. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  258. return;
  259. }
  260. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  261. return;
  262. }
  263. if (frag_id != query->next_frag_id) {
  264. wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
  265. "from " MACSTR, MAC2STR(query->addr));
  266. if (frag_id + 1 == query->next_frag_id) {
  267. wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
  268. "retry of previous fragment");
  269. return;
  270. }
  271. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  272. return;
  273. }
  274. query->next_frag_id++;
  275. if (gas_query_append(query, resp, len) < 0) {
  276. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  277. return;
  278. }
  279. if (more_frags) {
  280. gas_query_tx_comeback_req(gas, query);
  281. return;
  282. }
  283. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  284. }
  285. /**
  286. * gas_query_rx - Indicate reception of a Public Action frame
  287. * @gas: GAS query data from gas_query_init()
  288. * @da: Destination MAC address of the Action frame
  289. * @sa: Source MAC address of the Action frame
  290. * @bssid: BSSID of the Action frame
  291. * @data: Payload of the Action frame
  292. * @len: Length of @data
  293. * @freq: Frequency (in MHz) on which the frame was received
  294. * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
  295. */
  296. int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
  297. const u8 *bssid, const u8 *data, size_t len, int freq)
  298. {
  299. struct gas_query_pending *query;
  300. u8 action, dialog_token, frag_id = 0, more_frags = 0;
  301. u16 comeback_delay, resp_len;
  302. const u8 *pos, *adv_proto;
  303. if (gas == NULL || len < 4)
  304. return -1;
  305. pos = data;
  306. action = *pos++;
  307. dialog_token = *pos++;
  308. if (action != WLAN_PA_GAS_INITIAL_RESP &&
  309. action != WLAN_PA_GAS_COMEBACK_RESP)
  310. return -1; /* Not a GAS response */
  311. query = gas_query_get_pending(gas, sa, dialog_token);
  312. if (query == NULL) {
  313. wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
  314. " dialog token %u", MAC2STR(sa), dialog_token);
  315. return -1;
  316. }
  317. if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
  318. wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
  319. MACSTR " dialog token %u when waiting for comeback "
  320. "response", MAC2STR(sa), dialog_token);
  321. return 0;
  322. }
  323. if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
  324. wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
  325. MACSTR " dialog token %u when waiting for initial "
  326. "response", MAC2STR(sa), dialog_token);
  327. return 0;
  328. }
  329. query->status_code = WPA_GET_LE16(pos);
  330. pos += 2;
  331. if (query->status_code != WLAN_STATUS_SUCCESS) {
  332. wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
  333. "%u failed - status code %u",
  334. MAC2STR(sa), dialog_token, query->status_code);
  335. gas_query_done(gas, query, GAS_QUERY_FAILURE);
  336. return 0;
  337. }
  338. if (action == WLAN_PA_GAS_COMEBACK_RESP) {
  339. if (pos + 1 > data + len)
  340. return 0;
  341. frag_id = *pos & 0x7f;
  342. more_frags = (*pos & 0x80) >> 7;
  343. pos++;
  344. }
  345. /* Comeback Delay */
  346. if (pos + 2 > data + len)
  347. return 0;
  348. comeback_delay = WPA_GET_LE16(pos);
  349. pos += 2;
  350. /* Advertisement Protocol element */
  351. if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
  352. wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
  353. "Protocol element in the response from " MACSTR,
  354. MAC2STR(sa));
  355. return 0;
  356. }
  357. if (*pos != WLAN_EID_ADV_PROTO) {
  358. wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
  359. "Protocol element ID %u in response from " MACSTR,
  360. *pos, MAC2STR(sa));
  361. return 0;
  362. }
  363. adv_proto = pos;
  364. pos += 2 + pos[1];
  365. /* Query Response Length */
  366. if (pos + 2 > data + len) {
  367. wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
  368. return 0;
  369. }
  370. resp_len = WPA_GET_LE16(pos);
  371. pos += 2;
  372. if (pos + resp_len > data + len) {
  373. wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
  374. "response from " MACSTR, MAC2STR(sa));
  375. return 0;
  376. }
  377. if (pos + resp_len < data + len) {
  378. wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
  379. "after Query Response from " MACSTR,
  380. (unsigned int) (data + len - pos - resp_len),
  381. MAC2STR(sa));
  382. }
  383. if (action == WLAN_PA_GAS_COMEBACK_RESP)
  384. gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
  385. frag_id, more_frags, comeback_delay);
  386. else
  387. gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
  388. comeback_delay);
  389. return 0;
  390. }
  391. static void gas_query_timeout(void *eloop_data, void *user_ctx)
  392. {
  393. struct gas_query *gas = eloop_data;
  394. struct gas_query_pending *query = user_ctx;
  395. wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
  396. " dialog token %u",
  397. MAC2STR(query->addr), query->dialog_token);
  398. gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
  399. }
  400. static void gas_service_timeout(void *eloop_data, void *user_ctx)
  401. {
  402. struct gas_query *gas = eloop_data;
  403. struct wpa_supplicant *wpa_s = gas->wpa_s;
  404. struct gas_query_pending *query = user_ctx;
  405. int conn;
  406. conn = wpas_wpa_is_in_progress(wpa_s, 1);
  407. if (conn || wpa_s->scanning || gas->current) {
  408. wpa_printf(MSG_DEBUG, "GAS: Delaying GAS query Tx while another operation is in progress:%s%s%s",
  409. conn ? " connection" : "",
  410. wpa_s->scanning ? " scanning" : "",
  411. gas->current ? " gas_query" : "");
  412. eloop_register_timeout(
  413. GAS_SERVICE_RETRY_PERIOD_MS / 1000,
  414. (GAS_SERVICE_RETRY_PERIOD_MS % 1000) * 1000,
  415. gas_service_timeout, gas, query);
  416. return;
  417. }
  418. if (gas_query_tx(gas, query, query->req) < 0) {
  419. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  420. MACSTR, MAC2STR(query->addr));
  421. dl_list_del(&query->list);
  422. wpabuf_free(query->req);
  423. os_free(query);
  424. return;
  425. }
  426. gas->current = query;
  427. wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
  428. query->dialog_token);
  429. eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
  430. gas_query_timeout, gas, query);
  431. }
  432. static int gas_query_dialog_token_available(struct gas_query *gas,
  433. const u8 *dst, u8 dialog_token)
  434. {
  435. struct gas_query_pending *q;
  436. dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
  437. if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
  438. dialog_token == q->dialog_token)
  439. return 0;
  440. }
  441. return 1;
  442. }
  443. /**
  444. * gas_query_req - Request a GAS query
  445. * @gas: GAS query data from gas_query_init()
  446. * @dst: Destination MAC address for the query
  447. * @freq: Frequency (in MHz) for the channel on which to send the query
  448. * @req: GAS query payload (to be freed by gas_query module in case of success
  449. * return)
  450. * @cb: Callback function for reporting GAS query result and response
  451. * @ctx: Context pointer to use with the @cb call
  452. * Returns: dialog token (>= 0) on success or -1 on failure
  453. */
  454. int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
  455. struct wpabuf *req,
  456. void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
  457. enum gas_query_result result,
  458. const struct wpabuf *adv_proto,
  459. const struct wpabuf *resp, u16 status_code),
  460. void *ctx)
  461. {
  462. struct gas_query_pending *query;
  463. int dialog_token;
  464. static int next_start = 0;
  465. if (wpabuf_len(req) < 3)
  466. return -1;
  467. for (dialog_token = 0; dialog_token < 256; dialog_token++) {
  468. if (gas_query_dialog_token_available(
  469. gas, dst, (next_start + dialog_token) % 256))
  470. break;
  471. }
  472. if (dialog_token == 256)
  473. return -1; /* Too many pending queries */
  474. dialog_token = (next_start + dialog_token) % 256;
  475. next_start = (dialog_token + 1) % 256;
  476. query = os_zalloc(sizeof(*query));
  477. if (query == NULL)
  478. return -1;
  479. os_memcpy(query->addr, dst, ETH_ALEN);
  480. query->dialog_token = dialog_token;
  481. query->freq = freq;
  482. query->cb = cb;
  483. query->ctx = ctx;
  484. query->req = req;
  485. dl_list_add(&gas->pending, &query->list);
  486. *(wpabuf_mhead_u8(req) + 2) = dialog_token;
  487. wpa_printf(MSG_DEBUG, "GAS: Starting request for " MACSTR
  488. " dialog_token %u", MAC2STR(dst), dialog_token);
  489. eloop_register_timeout(0, 0, gas_service_timeout, gas, query);
  490. return dialog_token;
  491. }
  492. /**
  493. * gas_query_cancel - Cancel a pending GAS query
  494. * @gas: GAS query data from gas_query_init()
  495. * @dst: Destination MAC address for the query
  496. * @dialog_token: Dialog token from gas_query_req()
  497. */
  498. void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
  499. {
  500. struct gas_query_pending *query;
  501. query = gas_query_get_pending(gas, dst, dialog_token);
  502. if (query)
  503. gas_query_done(gas, query, GAS_QUERY_CANCELLED);
  504. }
  505. int gas_query_in_progress(struct gas_query *gas)
  506. {
  507. return gas->current != NULL;
  508. }