gas_query.c 15 KB

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