p2p_dev_disc.c 8.5 KB

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