gas_query.c 12 KB

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