gas_query.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 5
  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. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  226. return;
  227. }
  228. query->next_frag_id++;
  229. if (gas_query_append(query, resp, len) < 0) {
  230. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  231. return;
  232. }
  233. if (more_frags) {
  234. gas_query_tx_comeback_req(gas, query);
  235. return;
  236. }
  237. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  238. }
  239. /**
  240. * gas_query_rx - Indicate reception of a Public Action frame
  241. * @gas: GAS query data from gas_query_init()
  242. * @da: Destination MAC address of the Action frame
  243. * @sa: Source MAC address of the Action frame
  244. * @bssid: BSSID of the Action frame
  245. * @data: Payload of the Action frame
  246. * @len: Length of @data
  247. * @freq: Frequency (in MHz) on which the frame was received
  248. * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
  249. */
  250. int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
  251. const u8 *bssid, const u8 *data, size_t len, int freq)
  252. {
  253. struct gas_query_pending *query;
  254. u8 action, dialog_token, frag_id = 0, more_frags = 0;
  255. u16 comeback_delay, resp_len;
  256. const u8 *pos, *adv_proto;
  257. if (gas == NULL || len < 4)
  258. return -1;
  259. pos = data;
  260. action = *pos++;
  261. dialog_token = *pos++;
  262. if (action != WLAN_PA_GAS_INITIAL_RESP &&
  263. action != WLAN_PA_GAS_COMEBACK_RESP)
  264. return -1; /* Not a GAS response */
  265. query = gas_query_get_pending(gas, sa, dialog_token);
  266. if (query == NULL) {
  267. wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
  268. " dialog token %u", MAC2STR(sa), dialog_token);
  269. return -1;
  270. }
  271. if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
  272. wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
  273. MACSTR " dialog token %u when waiting for comeback "
  274. "response", MAC2STR(sa), dialog_token);
  275. return 0;
  276. }
  277. if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
  278. wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
  279. MACSTR " dialog token %u when waiting for initial "
  280. "response", MAC2STR(sa), dialog_token);
  281. return 0;
  282. }
  283. query->status_code = WPA_GET_LE16(pos);
  284. pos += 2;
  285. if (query->status_code != WLAN_STATUS_SUCCESS) {
  286. wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
  287. "%u failed - status code %u",
  288. MAC2STR(sa), dialog_token, query->status_code);
  289. gas_query_done(gas, query, GAS_QUERY_FAILURE);
  290. return 0;
  291. }
  292. if (action == WLAN_PA_GAS_COMEBACK_RESP) {
  293. if (pos + 1 > data + len)
  294. return 0;
  295. frag_id = *pos & 0x7f;
  296. more_frags = (*pos & 0x80) >> 7;
  297. pos++;
  298. }
  299. /* Comeback Delay */
  300. if (pos + 2 > data + len)
  301. return 0;
  302. comeback_delay = WPA_GET_LE16(pos);
  303. pos += 2;
  304. /* Advertisement Protocol element */
  305. if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
  306. wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
  307. "Protocol element in the response from " MACSTR,
  308. MAC2STR(sa));
  309. return 0;
  310. }
  311. if (*pos != WLAN_EID_ADV_PROTO) {
  312. wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
  313. "Protocol element ID %u in response from " MACSTR,
  314. *pos, MAC2STR(sa));
  315. return 0;
  316. }
  317. adv_proto = pos;
  318. pos += 2 + pos[1];
  319. /* Query Response Length */
  320. if (pos + 2 > data + len) {
  321. wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
  322. return 0;
  323. }
  324. resp_len = WPA_GET_LE16(pos);
  325. pos += 2;
  326. if (pos + resp_len > data + len) {
  327. wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
  328. "response from " MACSTR, MAC2STR(sa));
  329. return 0;
  330. }
  331. if (pos + resp_len < data + len) {
  332. wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
  333. "after Query Response from " MACSTR,
  334. (unsigned int) (data + len - pos - resp_len),
  335. MAC2STR(sa));
  336. }
  337. if (action == WLAN_PA_GAS_COMEBACK_RESP)
  338. gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
  339. frag_id, more_frags, comeback_delay);
  340. else
  341. gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
  342. comeback_delay);
  343. return 0;
  344. }
  345. static void gas_query_timeout(void *eloop_data, void *user_ctx)
  346. {
  347. struct gas_query *gas = eloop_data;
  348. struct gas_query_pending *query = user_ctx;
  349. wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR,
  350. MAC2STR(query->addr));
  351. gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
  352. }
  353. static int gas_query_dialog_token_available(struct gas_query *gas,
  354. const u8 *dst, u8 dialog_token)
  355. {
  356. struct gas_query_pending *q;
  357. dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
  358. if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
  359. dialog_token == q->dialog_token)
  360. return 0;
  361. }
  362. return 1;
  363. }
  364. /**
  365. * gas_query_req - Request a GAS query
  366. * @gas: GAS query data from gas_query_init()
  367. * @dst: Destination MAC address for the query
  368. * @freq: Frequency (in MHz) for the channel on which to send the query
  369. * @req: GAS query payload
  370. * @cb: Callback function for reporting GAS query result and response
  371. * @ctx: Context pointer to use with the @cb call
  372. * Returns: dialog token (>= 0) on success or -1 on failure
  373. */
  374. int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
  375. struct wpabuf *req,
  376. void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
  377. enum gas_query_result result,
  378. const struct wpabuf *adv_proto,
  379. const struct wpabuf *resp, u16 status_code),
  380. void *ctx)
  381. {
  382. struct gas_query_pending *query;
  383. int dialog_token;
  384. if (wpabuf_len(req) < 3)
  385. return -1;
  386. for (dialog_token = 0; dialog_token < 256; dialog_token++) {
  387. if (gas_query_dialog_token_available(gas, dst, dialog_token))
  388. break;
  389. }
  390. if (dialog_token == 256)
  391. return -1; /* Too many pending queries */
  392. query = os_zalloc(sizeof(*query));
  393. if (query == NULL)
  394. return -1;
  395. os_memcpy(query->addr, dst, ETH_ALEN);
  396. query->dialog_token = dialog_token;
  397. query->freq = freq;
  398. query->cb = cb;
  399. query->ctx = ctx;
  400. dl_list_add(&gas->pending, &query->list);
  401. *(wpabuf_mhead_u8(req) + 2) = dialog_token;
  402. wpa_printf(MSG_DEBUG, "GAS: Starting request for " MACSTR
  403. " dialog_token %u", MAC2STR(dst), dialog_token);
  404. if (gas_query_tx(gas, query, req) < 0) {
  405. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  406. MACSTR, MAC2STR(query->addr));
  407. dl_list_del(&query->list);
  408. os_free(query);
  409. return -1;
  410. }
  411. eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, gas_query_timeout,
  412. gas, query);
  413. return dialog_token;
  414. }
  415. /**
  416. * gas_query_cancel - Cancel a pending GAS query
  417. * @gas: GAS query data from gas_query_init()
  418. * @dst: Destination MAC address for the query
  419. * @dialog_token: Dialog token from gas_query_req()
  420. */
  421. void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
  422. {
  423. struct gas_query_pending *query;
  424. query = gas_query_get_pending(gas, dst, dialog_token);
  425. if (query)
  426. gas_query_done(gas, query, GAS_QUERY_CANCELLED);
  427. }