gas_query.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Generic advertisement service (GAS) query
  3. * Copyright (c) 2009, Atheros Communications
  4. * Copyright (c) 2011, Qualcomm Atheros
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #include "common.h"
  17. #include "utils/eloop.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "common/gas.h"
  20. #include "wpa_supplicant_i.h"
  21. #include "driver_i.h"
  22. #include "gas_query.h"
  23. #define GAS_QUERY_TIMEOUT 5
  24. struct gas_query_pending {
  25. struct dl_list list;
  26. u8 addr[ETH_ALEN];
  27. u8 dialog_token;
  28. u8 next_frag_id;
  29. int wait_comeback;
  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. struct gas_query {
  41. struct wpa_supplicant *wpa_s;
  42. struct dl_list pending; /* struct gas_query_pending */
  43. };
  44. static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
  45. static void gas_query_timeout(void *eloop_data, void *user_ctx);
  46. struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
  47. {
  48. struct gas_query *gas;
  49. gas = os_zalloc(sizeof(*gas));
  50. if (gas == NULL)
  51. return NULL;
  52. gas->wpa_s = wpa_s;
  53. dl_list_init(&gas->pending);
  54. return gas;
  55. }
  56. static void gas_query_done(struct gas_query *gas,
  57. struct gas_query_pending *query,
  58. enum gas_query_result result)
  59. {
  60. eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
  61. eloop_cancel_timeout(gas_query_timeout, gas, query);
  62. dl_list_del(&query->list);
  63. query->cb(query->ctx, query->addr, query->dialog_token, result,
  64. query->adv_proto, query->resp, query->status_code);
  65. wpabuf_free(query->adv_proto);
  66. wpabuf_free(query->resp);
  67. os_free(query);
  68. }
  69. void gas_query_deinit(struct gas_query *gas)
  70. {
  71. struct gas_query_pending *query, *next;
  72. if (gas == NULL)
  73. return;
  74. dl_list_for_each_safe(query, next, &gas->pending,
  75. struct gas_query_pending, list)
  76. gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
  77. os_free(gas);
  78. }
  79. static struct gas_query_pending *
  80. gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
  81. {
  82. struct gas_query_pending *q;
  83. dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
  84. if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
  85. q->dialog_token == dialog_token)
  86. return q;
  87. }
  88. return NULL;
  89. }
  90. static int gas_query_append(struct gas_query_pending *query, const u8 *data,
  91. size_t len)
  92. {
  93. if (wpabuf_resize(&query->resp, len) < 0) {
  94. wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
  95. return -1;
  96. }
  97. wpabuf_put_data(query->resp, data, len);
  98. return 0;
  99. }
  100. static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
  101. struct wpabuf *req)
  102. {
  103. wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
  104. "freq=%d", MAC2STR(query->addr),
  105. (unsigned int) wpabuf_len(req), query->freq);
  106. return wpa_drv_send_action(gas->wpa_s, query->freq, 0, query->addr,
  107. gas->wpa_s->own_addr, query->addr,
  108. wpabuf_head(req), wpabuf_len(req));
  109. }
  110. static void gas_query_tx_comeback_req(struct gas_query *gas,
  111. struct gas_query_pending *query)
  112. {
  113. struct wpabuf *req;
  114. req = gas_build_comeback_req(query->dialog_token);
  115. if (req == NULL) {
  116. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  117. return;
  118. }
  119. if (gas_query_tx(gas, query, req) < 0) {
  120. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  121. MACSTR, MAC2STR(query->addr));
  122. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  123. }
  124. wpabuf_free(req);
  125. }
  126. static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
  127. {
  128. struct gas_query *gas = eloop_data;
  129. struct gas_query_pending *query = user_ctx;
  130. wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
  131. MAC2STR(query->addr));
  132. gas_query_tx_comeback_req(gas, query);
  133. }
  134. static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
  135. struct gas_query_pending *query,
  136. u16 comeback_delay)
  137. {
  138. unsigned int secs, usecs;
  139. secs = (comeback_delay * 1024) / 1000000;
  140. usecs = comeback_delay * 1024 - secs * 1000000;
  141. wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
  142. " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
  143. eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
  144. eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
  145. gas, query);
  146. }
  147. static void gas_query_rx_initial(struct gas_query *gas,
  148. struct gas_query_pending *query,
  149. const u8 *adv_proto, const u8 *resp,
  150. size_t len, u16 comeback_delay)
  151. {
  152. wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
  153. MACSTR " (dialog_token=%u comeback_delay=%u)",
  154. MAC2STR(query->addr), query->dialog_token, comeback_delay);
  155. query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
  156. if (query->adv_proto == NULL) {
  157. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  158. return;
  159. }
  160. if (comeback_delay) {
  161. query->wait_comeback = 1;
  162. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  163. return;
  164. }
  165. /* Query was completed without comeback mechanism */
  166. if (gas_query_append(query, resp, len) < 0) {
  167. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  168. return;
  169. }
  170. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  171. }
  172. static void gas_query_rx_comeback(struct gas_query *gas,
  173. struct gas_query_pending *query,
  174. const u8 *adv_proto, const u8 *resp,
  175. size_t len, u8 frag_id, u8 more_frags,
  176. u16 comeback_delay)
  177. {
  178. wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
  179. MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
  180. "comeback_delay=%u)",
  181. MAC2STR(query->addr), query->dialog_token, frag_id,
  182. more_frags, comeback_delay);
  183. if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
  184. os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
  185. wpabuf_len(query->adv_proto)) != 0) {
  186. wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
  187. "between initial and comeback response from "
  188. MACSTR, MAC2STR(query->addr));
  189. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  190. return;
  191. }
  192. if (comeback_delay) {
  193. if (frag_id) {
  194. wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
  195. "with non-zero frag_id and comeback_delay "
  196. "from " MACSTR, MAC2STR(query->addr));
  197. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  198. return;
  199. }
  200. gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
  201. return;
  202. }
  203. if (frag_id != query->next_frag_id) {
  204. wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
  205. "from " MACSTR, MAC2STR(query->addr));
  206. gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
  207. return;
  208. }
  209. query->next_frag_id++;
  210. if (gas_query_append(query, resp, len) < 0) {
  211. gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
  212. return;
  213. }
  214. if (more_frags) {
  215. gas_query_tx_comeback_req(gas, query);
  216. return;
  217. }
  218. gas_query_done(gas, query, GAS_QUERY_SUCCESS);
  219. }
  220. int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
  221. const u8 *bssid, const u8 *data, size_t len, int freq)
  222. {
  223. struct gas_query_pending *query;
  224. u8 action, dialog_token, frag_id = 0, more_frags = 0;
  225. u16 comeback_delay, resp_len;
  226. const u8 *pos, *adv_proto;
  227. if (gas == NULL || len < 4)
  228. return -1;
  229. pos = data;
  230. action = *pos++;
  231. dialog_token = *pos++;
  232. if (action != WLAN_PA_GAS_INITIAL_RESP &&
  233. action != WLAN_PA_GAS_COMEBACK_RESP)
  234. return -1; /* Not a GAS response */
  235. query = gas_query_get_pending(gas, sa, dialog_token);
  236. if (query == NULL) {
  237. wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
  238. " dialog token %u", MAC2STR(sa), dialog_token);
  239. return -1;
  240. }
  241. if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
  242. wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
  243. MACSTR " dialog token %u when waiting for comeback "
  244. "response", MAC2STR(sa), dialog_token);
  245. return 0;
  246. }
  247. if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
  248. wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
  249. MACSTR " dialog token %u when waiting for initial "
  250. "response", MAC2STR(sa), dialog_token);
  251. return 0;
  252. }
  253. query->status_code = WPA_GET_LE16(pos);
  254. pos += 2;
  255. if (query->status_code != WLAN_STATUS_SUCCESS) {
  256. wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
  257. "%u failed - status code %u",
  258. MAC2STR(sa), dialog_token, query->status_code);
  259. gas_query_done(gas, query, GAS_QUERY_FAILURE);
  260. return 0;
  261. }
  262. if (action == WLAN_PA_GAS_COMEBACK_RESP) {
  263. if (pos + 1 > data + len)
  264. return 0;
  265. frag_id = *pos & 0x7f;
  266. more_frags = (*pos & 0x80) >> 7;
  267. pos++;
  268. }
  269. /* Comeback Delay */
  270. if (pos + 2 > data + len)
  271. return 0;
  272. comeback_delay = WPA_GET_LE16(pos);
  273. pos += 2;
  274. /* Advertisement Protocol element */
  275. if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
  276. wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
  277. "Protocol element in the response from " MACSTR,
  278. MAC2STR(sa));
  279. return 0;
  280. }
  281. if (*pos != WLAN_EID_ADV_PROTO) {
  282. wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
  283. "Protocol element ID %u in response from " MACSTR,
  284. *pos, MAC2STR(sa));
  285. return 0;
  286. }
  287. adv_proto = pos;
  288. pos += 2 + pos[1];
  289. /* Query Response Length */
  290. if (pos + 2 > data + len) {
  291. wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
  292. return 0;
  293. }
  294. resp_len = WPA_GET_LE16(pos);
  295. pos += 2;
  296. if (pos + resp_len > data + len) {
  297. wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
  298. "response from " MACSTR, MAC2STR(sa));
  299. return 0;
  300. }
  301. if (pos + resp_len < data + len) {
  302. wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
  303. "after Query Response from " MACSTR,
  304. (unsigned int) (data + len - pos - resp_len),
  305. MAC2STR(sa));
  306. }
  307. if (action == WLAN_PA_GAS_COMEBACK_RESP)
  308. gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
  309. frag_id, more_frags, comeback_delay);
  310. else
  311. gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
  312. comeback_delay);
  313. return 0;
  314. }
  315. static void gas_query_timeout(void *eloop_data, void *user_ctx)
  316. {
  317. struct gas_query *gas = eloop_data;
  318. struct gas_query_pending *query = user_ctx;
  319. wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR,
  320. MAC2STR(query->addr));
  321. gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
  322. }
  323. static int gas_query_dialog_token_available(struct gas_query *gas,
  324. const u8 *dst, u8 dialog_token)
  325. {
  326. struct gas_query_pending *q;
  327. dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
  328. if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
  329. dialog_token == q->dialog_token)
  330. return 0;
  331. }
  332. return 1;
  333. }
  334. int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
  335. struct wpabuf *req,
  336. void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
  337. enum gas_query_result result,
  338. const struct wpabuf *adv_proto,
  339. const struct wpabuf *resp, u16 status_code),
  340. void *ctx)
  341. {
  342. struct gas_query_pending *query;
  343. int dialog_token;
  344. if (wpabuf_len(req) < 3)
  345. return -1;
  346. for (dialog_token = 0; dialog_token < 256; dialog_token++) {
  347. if (gas_query_dialog_token_available(gas, dst, dialog_token))
  348. break;
  349. }
  350. if (dialog_token == 256)
  351. return -1; /* Too many pending queries */
  352. query = os_zalloc(sizeof(*query));
  353. if (query == NULL)
  354. return -1;
  355. os_memcpy(query->addr, dst, ETH_ALEN);
  356. query->dialog_token = dialog_token;
  357. query->freq = freq;
  358. query->cb = cb;
  359. query->ctx = ctx;
  360. dl_list_add(&gas->pending, &query->list);
  361. *(wpabuf_mhead_u8(req) + 2) = dialog_token;
  362. wpa_printf(MSG_DEBUG, "GAS: Starting request for " MACSTR
  363. " dialog_token %u", MAC2STR(dst), dialog_token);
  364. if (gas_query_tx(gas, query, req) < 0) {
  365. wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
  366. MACSTR, MAC2STR(query->addr));
  367. os_free(query);
  368. return -1;
  369. }
  370. eloop_register_timeout(GAS_QUERY_TIMEOUT, 0, gas_query_timeout,
  371. gas, query);
  372. return dialog_token;
  373. }
  374. void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
  375. {
  376. struct gas_query_pending *query;
  377. query = gas_query_get_pending(gas, dst, dialog_token);
  378. if (query)
  379. gas_query_done(gas, query, GAS_QUERY_CANCELLED);
  380. }