gas_query.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
  115. struct wpabuf *req)
  116. {
  117. int res;
  118. wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
  119. "freq=%d", MAC2STR(query->addr),
  120. (unsigned int) wpabuf_len(req), query->freq);
  121. res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
  122. gas->wpa_s->own_addr, query->addr,
  123. wpabuf_head(req), wpabuf_len(req), 1000,
  124. NULL, 0);
  125. if (res == 0)
  126. query->offchannel_tx_started = 1;
  127. return res;
  128. }
  129. static void gas_query_tx_comeback_req(struct gas_query *gas,
  130. struct gas_query_pending *query)
  131. {
  132. struct wpabuf *req;
  133. req = gas_build_comeback_req(query->dialog_token);
  134. if (req == NULL) {
  135. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  136. return;
  137. }
  138. if (gas_query_tx(gas, query, req) < 0) {
  139. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  140. MACSTR, MAC2STR(query->addr));
  141. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  142. }
  143. wpabuf_free(req);
  144. }
  145. static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
  146. {
  147. struct gas_query *gas = eloop_data;
  148. struct gas_query_pending *query = user_ctx;
  149. wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
  150. MAC2STR(query->addr));
  151. gas_query_tx_comeback_req(gas, query);
  152. }
  153. static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
  154. struct gas_query_pending *query,
  155. u16 comeback_delay)
  156. {
  157. unsigned int secs, usecs;
  158. secs = (comeback_delay * 1024) / 1000000;
  159. usecs = comeback_delay * 1024 - secs * 1000000;
  160. wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
  161. " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
  162. eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
  163. eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
  164. gas, query);
  165. }
  166. static void gas_query_rx_initial(struct gas_query *gas,
  167. struct gas_query_pending *query,
  168. const u8 *adv_proto, const u8 *resp,
  169. size_t len, u16 comeback_delay)
  170. {
  171. wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
  172. MACSTR " (dialog_token=%u comeback_delay=%u)",
  173. MAC2STR(query->addr), query->dialog_token, comeback_delay);
  174. query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
  175. if (query->adv_proto == NULL) {
  176. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  177. return;
  178. }
  179. if (comeback_delay) {
  180. query->wait_comeback = 1;
  181. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  182. return;
  183. }
  184. /* Query was completed without comeback mechanism */
  185. if (gas_query_append(query, resp, len) < 0) {
  186. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  187. return;
  188. }
  189. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  190. }
  191. static void gas_query_rx_comeback(struct gas_query *gas,
  192. struct gas_query_pending *query,
  193. const u8 *adv_proto, const u8 *resp,
  194. size_t len, u8 frag_id, u8 more_frags,
  195. u16 comeback_delay)
  196. {
  197. wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
  198. MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
  199. "comeback_delay=%u)",
  200. MAC2STR(query->addr), query->dialog_token, frag_id,
  201. more_frags, comeback_delay);
  202. if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
  203. os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
  204. wpabuf_len(query->adv_proto)) != 0) {
  205. wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
  206. "between initial and comeback response from "
  207. MACSTR, MAC2STR(query->addr));
  208. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  209. return;
  210. }
  211. if (comeback_delay) {
  212. if (frag_id) {
  213. wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
  214. "with non-zero frag_id and comeback_delay "
  215. "from " MACSTR, MAC2STR(query->addr));
  216. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  217. return;
  218. }
  219. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  220. return;
  221. }
  222. if (frag_id != query->next_frag_id) {
  223. wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
  224. "from " MACSTR, MAC2STR(query->addr));
  225. if (frag_id + 1 == query->next_frag_id) {
  226. wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
  227. "retry of previous fragment");
  228. return;
  229. }
  230. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  231. return;
  232. }
  233. query->next_frag_id++;
  234. if (gas_query_append(query, resp, len) < 0) {
  235. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  236. return;
  237. }
  238. if (more_frags) {
  239. gas_query_tx_comeback_req(gas, query);
  240. return;
  241. }
  242. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  243. }
  244. /**
  245. * gas_query_rx - Indicate reception of a Public Action frame
  246. * @gas: GAS query data from gas_query_init()
  247. * @da: Destination MAC address of the Action frame
  248. * @sa: Source MAC address of the Action frame
  249. * @bssid: BSSID of the Action frame
  250. * @data: Payload of the Action frame
  251. * @len: Length of @data
  252. * @freq: Frequency (in MHz) on which the frame was received
  253. * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
  254. */
  255. int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
  256. const u8 *bssid, const u8 *data, size_t len, int freq)
  257. {
  258. struct gas_query_pending *query;
  259. u8 action, dialog_token, frag_id = 0, more_frags = 0;
  260. u16 comeback_delay, resp_len;
  261. const u8 *pos, *adv_proto;
  262. if (gas == NULL || len < 4)
  263. return -1;
  264. pos = data;
  265. action = *pos++;
  266. dialog_token = *pos++;
  267. if (action != WLAN_PA_GAS_INITIAL_RESP &&
  268. action != WLAN_PA_GAS_COMEBACK_RESP)
  269. return -1; /* Not a GAS response */
  270. query = gas_query_get_pending(gas, sa, dialog_token);
  271. if (query == NULL) {
  272. wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
  273. " dialog token %u", MAC2STR(sa), dialog_token);
  274. return -1;
  275. }
  276. if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
  277. wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
  278. MACSTR " dialog token %u when waiting for comeback "
  279. "response", MAC2STR(sa), dialog_token);
  280. return 0;
  281. }
  282. if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
  283. wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
  284. MACSTR " dialog token %u when waiting for initial "
  285. "response", MAC2STR(sa), dialog_token);
  286. return 0;
  287. }
  288. query->status_code = WPA_GET_LE16(pos);
  289. pos += 2;
  290. if (query->status_code != WLAN_STATUS_SUCCESS) {
  291. wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
  292. "%u failed - status code %u",
  293. MAC2STR(sa), dialog_token, query->status_code);
  294. gas_query_done(gas, query, GAS_QUERY_FAILURE);
  295. return 0;
  296. }
  297. if (action == WLAN_PA_GAS_COMEBACK_RESP) {
  298. if (pos + 1 > data + len)
  299. return 0;
  300. frag_id = *pos & 0x7f;
  301. more_frags = (*pos & 0x80) >> 7;
  302. pos++;
  303. }
  304. /* Comeback Delay */
  305. if (pos + 2 > data + len)
  306. return 0;
  307. comeback_delay = WPA_GET_LE16(pos);
  308. pos += 2;
  309. /* Advertisement Protocol element */
  310. if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
  311. wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
  312. "Protocol element in the response from " MACSTR,
  313. MAC2STR(sa));
  314. return 0;
  315. }
  316. if (*pos != WLAN_EID_ADV_PROTO) {
  317. wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
  318. "Protocol element ID %u in response from " MACSTR,
  319. *pos, MAC2STR(sa));
  320. return 0;
  321. }
  322. adv_proto = pos;
  323. pos += 2 + pos[1];
  324. /* Query Response Length */
  325. if (pos + 2 > data + len) {
  326. wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
  327. return 0;
  328. }
  329. resp_len = WPA_GET_LE16(pos);
  330. pos += 2;
  331. if (pos + resp_len > data + len) {
  332. wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
  333. "response from " MACSTR, MAC2STR(sa));
  334. return 0;
  335. }
  336. if (pos + resp_len < data + len) {
  337. wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
  338. "after Query Response from " MACSTR,
  339. (unsigned int) (data + len - pos - resp_len),
  340. MAC2STR(sa));
  341. }
  342. if (action == WLAN_PA_GAS_COMEBACK_RESP)
  343. gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
  344. frag_id, more_frags, comeback_delay);
  345. else
  346. gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
  347. comeback_delay);
  348. return 0;
  349. }
  350. static void gas_query_timeout(void *eloop_data, void *user_ctx)
  351. {
  352. struct gas_query *gas = eloop_data;
  353. struct gas_query_pending *query = user_ctx;
  354. wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR,
  355. MAC2STR(query->addr));
  356. gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
  357. }
  358. static int gas_query_dialog_token_available(struct gas_query *gas,
  359. const u8 *dst, u8 dialog_token)
  360. {
  361. struct gas_query_pending *q;
  362. dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
  363. if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
  364. dialog_token == q->dialog_token)
  365. return 0;
  366. }
  367. return 1;
  368. }
  369. /**
  370. * gas_query_req - Request a GAS query
  371. * @gas: GAS query data from gas_query_init()
  372. * @dst: Destination MAC address for the query
  373. * @freq: Frequency (in MHz) for the channel on which to send the query
  374. * @req: GAS query payload
  375. * @cb: Callback function for reporting GAS query result and response
  376. * @ctx: Context pointer to use with the @cb call
  377. * Returns: dialog token (>= 0) on success or -1 on failure
  378. */
  379. int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
  380. struct wpabuf *req,
  381. void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
  382. enum gas_query_result result,
  383. const struct wpabuf *adv_proto,
  384. const struct wpabuf *resp, u16 status_code),
  385. void *ctx)
  386. {
  387. struct gas_query_pending *query;
  388. int dialog_token;
  389. static int next_start = 0;
  390. if (wpabuf_len(req) < 3)
  391. return -1;
  392. for (dialog_token = 0; dialog_token < 256; dialog_token++) {
  393. if (gas_query_dialog_token_available(
  394. gas, dst, (next_start + dialog_token) % 256))
  395. break;
  396. }
  397. if (dialog_token == 256)
  398. return -1; /* Too many pending queries */
  399. dialog_token = (next_start + dialog_token) % 256;
  400. next_start = (dialog_token + 1) % 256;
  401. query = os_zalloc(sizeof(*query));
  402. if (query == NULL)
  403. return -1;
  404. os_memcpy(query->addr, dst, ETH_ALEN);
  405. query->dialog_token = dialog_token;
  406. query->freq = freq;
  407. query->cb = cb;
  408. query->ctx = ctx;
  409. dl_list_add(&gas->pending, &query->list);
  410. *(wpabuf_mhead_u8(req) + 2) = dialog_token;
  411. wpa_printf(MSG_DEBUG, "GAS: Starting request for " MACSTR
  412. " dialog_token %u", MAC2STR(dst), dialog_token);
  413. if (gas_query_tx(gas, query, req) < 0) {
  414. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  415. MACSTR, MAC2STR(query->addr));
  416. dl_list_del(&query->list);
  417. os_free(query);
  418. return -1;
  419. }
  420. eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, gas_query_timeout,
  421. gas, query);
  422. return dialog_token;
  423. }
  424. /**
  425. * gas_query_cancel - Cancel a pending GAS query
  426. * @gas: GAS query data from gas_query_init()
  427. * @dst: Destination MAC address for the query
  428. * @dialog_token: Dialog token from gas_query_req()
  429. */
  430. void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
  431. {
  432. struct gas_query_pending *query;
  433. query = gas_query_get_pending(gas, dst, dialog_token);
  434. if (query)
  435. gas_query_done(gas, query, GAS_QUERY_CANCELLED);
  436. }