p2p_dev_disc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Wi-Fi Direct - P2P Device Discoverability procedure
  3. * Copyright (c) 2010, Atheros Communications
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "p2p_i.h"
  18. #include "p2p.h"
  19. static struct wpabuf * p2p_build_dev_disc_req(struct p2p_data *p2p,
  20. struct p2p_device *go,
  21. const u8 *dev_id)
  22. {
  23. struct wpabuf *buf;
  24. u8 *len;
  25. buf = wpabuf_alloc(100);
  26. if (buf == NULL)
  27. return NULL;
  28. go->dialog_token++;
  29. if (go->dialog_token == 0)
  30. go->dialog_token = 1;
  31. p2p_buf_add_public_action_hdr(buf, P2P_DEV_DISC_REQ, go->dialog_token);
  32. len = p2p_buf_add_ie_hdr(buf);
  33. p2p_buf_add_device_id(buf, dev_id);
  34. p2p_buf_add_group_id(buf, go->info.p2p_device_addr, go->oper_ssid,
  35. go->oper_ssid_len);
  36. p2p_buf_update_ie_hdr(buf, len);
  37. return buf;
  38. }
  39. void p2p_dev_disc_req_cb(struct p2p_data *p2p, int success)
  40. {
  41. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  42. "P2P: Device Discoverability Request TX callback: success=%d",
  43. success);
  44. if (!success) {
  45. /*
  46. * Use P2P find, if needed, to find the other device or to
  47. * retry device discoverability.
  48. */
  49. p2p_set_state(p2p, P2P_CONNECT);
  50. p2p_set_timeout(p2p, 0, 100000);
  51. return;
  52. }
  53. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  54. "P2P: GO acknowledged Device Discoverability Request - wait "
  55. "for response");
  56. /*
  57. * TODO: is the remain-on-channel from Action frame TX long enough for
  58. * most cases or should we try to increase its duration and/or start
  59. * another remain-on-channel if needed once the previous one expires?
  60. */
  61. }
  62. int p2p_send_dev_disc_req(struct p2p_data *p2p, struct p2p_device *dev)
  63. {
  64. struct p2p_device *go;
  65. struct wpabuf *req;
  66. go = p2p_get_device(p2p, dev->member_in_go_dev);
  67. if (go == NULL || dev->oper_freq <= 0) {
  68. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  69. "P2P: Could not find peer entry for GO and frequency "
  70. "to send Device Discoverability Request");
  71. return -1;
  72. }
  73. req = p2p_build_dev_disc_req(p2p, go, dev->info.p2p_device_addr);
  74. if (req == NULL)
  75. return -1;
  76. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  77. "P2P: Sending Device Discoverability Request to GO " MACSTR
  78. " for client " MACSTR,
  79. MAC2STR(go->info.p2p_device_addr),
  80. MAC2STR(dev->info.p2p_device_addr));
  81. p2p->pending_client_disc_go = go;
  82. os_memcpy(p2p->pending_client_disc_addr, dev->info.p2p_device_addr,
  83. ETH_ALEN);
  84. p2p->pending_action_state = P2P_PENDING_DEV_DISC_REQUEST;
  85. if (p2p_send_action(p2p, dev->oper_freq, go->info.p2p_device_addr,
  86. p2p->cfg->dev_addr, go->info.p2p_device_addr,
  87. wpabuf_head(req), wpabuf_len(req), 1000) < 0) {
  88. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  89. "P2P: Failed to send Action frame");
  90. wpabuf_free(req);
  91. /* TODO: how to recover from failure? */
  92. return -1;
  93. }
  94. wpabuf_free(req);
  95. return 0;
  96. }
  97. static struct wpabuf * p2p_build_dev_disc_resp(u8 dialog_token, u8 status)
  98. {
  99. struct wpabuf *buf;
  100. u8 *len;
  101. buf = wpabuf_alloc(100);
  102. if (buf == NULL)
  103. return NULL;
  104. p2p_buf_add_public_action_hdr(buf, P2P_DEV_DISC_RESP, dialog_token);
  105. len = p2p_buf_add_ie_hdr(buf);
  106. p2p_buf_add_status(buf, status);
  107. p2p_buf_update_ie_hdr(buf, len);
  108. return buf;
  109. }
  110. void p2p_dev_disc_resp_cb(struct p2p_data *p2p, int success)
  111. {
  112. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  113. "P2P: Device Discoverability Response TX callback: success=%d",
  114. success);
  115. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  116. }
  117. static void p2p_send_dev_disc_resp(struct p2p_data *p2p, u8 dialog_token,
  118. const u8 *addr, int freq, u8 status)
  119. {
  120. struct wpabuf *resp;
  121. resp = p2p_build_dev_disc_resp(dialog_token, status);
  122. if (resp == NULL)
  123. return;
  124. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  125. "P2P: Sending Device Discoverability Response to " MACSTR
  126. " (status %u freq %d)",
  127. MAC2STR(addr), status, freq);
  128. p2p->pending_action_state = P2P_PENDING_DEV_DISC_RESPONSE;
  129. if (p2p_send_action(p2p, freq, addr, p2p->cfg->dev_addr,
  130. p2p->cfg->dev_addr,
  131. wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
  132. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  133. "P2P: Failed to send Action frame");
  134. }
  135. wpabuf_free(resp);
  136. }
  137. void p2p_process_dev_disc_req(struct p2p_data *p2p, const u8 *sa,
  138. const u8 *data, size_t len, int rx_freq)
  139. {
  140. struct p2p_message msg;
  141. size_t g;
  142. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  143. "P2P: Received Device Discoverability Request from " MACSTR
  144. " (freq=%d)", MAC2STR(sa), rx_freq);
  145. if (p2p_parse(data, len, &msg))
  146. return;
  147. if (msg.dialog_token == 0) {
  148. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  149. "P2P: Invalid Dialog Token 0 (must be nonzero) in "
  150. "Device Discoverability Request");
  151. p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
  152. P2P_SC_FAIL_INVALID_PARAMS);
  153. p2p_parse_free(&msg);
  154. return;
  155. }
  156. if (msg.device_id == NULL) {
  157. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  158. "P2P: P2P Device ID attribute missing from Device "
  159. "Discoverability Request");
  160. p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
  161. P2P_SC_FAIL_INVALID_PARAMS);
  162. p2p_parse_free(&msg);
  163. return;
  164. }
  165. for (g = 0; g < p2p->num_groups; g++) {
  166. if (p2p_group_go_discover(p2p->groups[g], msg.device_id, sa,
  167. rx_freq) == 0) {
  168. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Scheduled "
  169. "GO Discoverability Request for the target "
  170. "device");
  171. /*
  172. * P2P group code will use a callback to indicate TX
  173. * status, so that we can reply to the request once the
  174. * target client has acknowledged the request or it has
  175. * timed out.
  176. */
  177. p2p->pending_dev_disc_dialog_token = msg.dialog_token;
  178. os_memcpy(p2p->pending_dev_disc_addr, sa, ETH_ALEN);
  179. p2p->pending_dev_disc_freq = rx_freq;
  180. p2p_parse_free(&msg);
  181. return;
  182. }
  183. }
  184. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Requested client "
  185. "was not found in any group or did not support client "
  186. "discoverability");
  187. p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
  188. P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE);
  189. p2p_parse_free(&msg);
  190. }
  191. void p2p_process_dev_disc_resp(struct p2p_data *p2p, const u8 *sa,
  192. const u8 *data, size_t len)
  193. {
  194. struct p2p_message msg;
  195. struct p2p_device *go;
  196. u8 status;
  197. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  198. "P2P: Received Device Discoverability Response from " MACSTR,
  199. MAC2STR(sa));
  200. go = p2p->pending_client_disc_go;
  201. if (go == NULL ||
  202. os_memcmp(sa, go->info.p2p_device_addr, ETH_ALEN) != 0) {
  203. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Ignore unexpected "
  204. "Device Discoverability Response");
  205. return;
  206. }
  207. if (p2p_parse(data, len, &msg))
  208. return;
  209. if (msg.status == NULL) {
  210. p2p_parse_free(&msg);
  211. return;
  212. }
  213. if (msg.dialog_token != go->dialog_token) {
  214. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Ignore Device "
  215. "Discoverability Response with unexpected dialog "
  216. "token %u (expected %u)",
  217. msg.dialog_token, go->dialog_token);
  218. p2p_parse_free(&msg);
  219. return;
  220. }
  221. status = *msg.status;
  222. p2p_parse_free(&msg);
  223. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  224. "P2P: Device Discoverability Response status %u", status);
  225. if (p2p->go_neg_peer == NULL ||
  226. os_memcmp(p2p->pending_client_disc_addr,
  227. p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN) != 0 ||
  228. os_memcmp(p2p->go_neg_peer->member_in_go_dev,
  229. go->info.p2p_device_addr, ETH_ALEN) != 0) {
  230. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: No pending "
  231. "operation with the client discoverability peer "
  232. "anymore");
  233. return;
  234. }
  235. if (status == 0) {
  236. /*
  237. * Peer is expected to be awake for at least 100 TU; try to
  238. * connect immediately.
  239. */
  240. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  241. "P2P: Client discoverability request succeeded");
  242. if (p2p->state == P2P_CONNECT) {
  243. /*
  244. * Change state to force the timeout to start in
  245. * P2P_CONNECT again without going through the short
  246. * Listen state.
  247. */
  248. p2p_set_state(p2p, P2P_CONNECT_LISTEN);
  249. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  250. }
  251. p2p_set_timeout(p2p, 0, 0);
  252. } else {
  253. /*
  254. * Client discoverability request failed; try to connect from
  255. * timeout.
  256. */
  257. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  258. "P2P: Client discoverability request failed");
  259. p2p_set_timeout(p2p, 0, 500000);
  260. }
  261. }
  262. void p2p_go_disc_req_cb(struct p2p_data *p2p, int success)
  263. {
  264. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  265. "P2P: GO Discoverability Request TX callback: success=%d",
  266. success);
  267. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  268. if (p2p->pending_dev_disc_dialog_token == 0) {
  269. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: No pending Device "
  270. "Discoverability Request");
  271. return;
  272. }
  273. p2p_send_dev_disc_resp(p2p, p2p->pending_dev_disc_dialog_token,
  274. p2p->pending_dev_disc_addr,
  275. p2p->pending_dev_disc_freq,
  276. success ? P2P_SC_SUCCESS :
  277. P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE);
  278. p2p->pending_dev_disc_dialog_token = 0;
  279. }
  280. void p2p_process_go_disc_req(struct p2p_data *p2p, const u8 *da, const u8 *sa,
  281. const u8 *data, size_t len, int rx_freq)
  282. {
  283. unsigned int tu;
  284. struct wpabuf *ies;
  285. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  286. "P2P: Received GO Discoverability Request - remain awake for "
  287. "100 TU");
  288. ies = p2p_build_probe_resp_ies(p2p);
  289. if (ies == NULL)
  290. return;
  291. /* Remain awake 100 TU on operating channel */
  292. p2p->pending_client_disc_freq = rx_freq;
  293. tu = 100;
  294. if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, rx_freq, 1024 * tu / 1000,
  295. ies) < 0) {
  296. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  297. "P2P: Failed to start listen mode for client "
  298. "discoverability");
  299. }
  300. wpabuf_free(ies);
  301. }