p2p_dev_disc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. unsigned int wait_time;
  58. go = p2p_get_device(p2p, dev->member_in_go_dev);
  59. if (go == NULL || dev->oper_freq <= 0) {
  60. p2p_dbg(p2p, "Could not find peer entry for GO and frequency to send Device Discoverability Request");
  61. return -1;
  62. }
  63. req = p2p_build_dev_disc_req(p2p, go, dev->info.p2p_device_addr);
  64. if (req == NULL)
  65. return -1;
  66. p2p_dbg(p2p, "Sending Device Discoverability Request to GO " MACSTR
  67. " for client " MACSTR,
  68. MAC2STR(go->info.p2p_device_addr),
  69. MAC2STR(dev->info.p2p_device_addr));
  70. p2p->pending_client_disc_go = go;
  71. os_memcpy(p2p->pending_client_disc_addr, dev->info.p2p_device_addr,
  72. ETH_ALEN);
  73. p2p->pending_action_state = P2P_PENDING_DEV_DISC_REQUEST;
  74. wait_time = 1000;
  75. if (p2p->cfg->max_listen && wait_time > p2p->cfg->max_listen)
  76. wait_time = p2p->cfg->max_listen;
  77. if (p2p_send_action(p2p, dev->oper_freq, go->info.p2p_device_addr,
  78. p2p->cfg->dev_addr, go->info.p2p_device_addr,
  79. wpabuf_head(req), wpabuf_len(req), wait_time) < 0) {
  80. p2p_dbg(p2p, "Failed to send Action frame");
  81. wpabuf_free(req);
  82. /* TODO: how to recover from failure? */
  83. return -1;
  84. }
  85. wpabuf_free(req);
  86. return 0;
  87. }
  88. static struct wpabuf * p2p_build_dev_disc_resp(u8 dialog_token, u8 status)
  89. {
  90. struct wpabuf *buf;
  91. u8 *len;
  92. buf = wpabuf_alloc(100);
  93. if (buf == NULL)
  94. return NULL;
  95. p2p_buf_add_public_action_hdr(buf, P2P_DEV_DISC_RESP, dialog_token);
  96. len = p2p_buf_add_ie_hdr(buf);
  97. p2p_buf_add_status(buf, status);
  98. p2p_buf_update_ie_hdr(buf, len);
  99. return buf;
  100. }
  101. void p2p_dev_disc_resp_cb(struct p2p_data *p2p, int success)
  102. {
  103. p2p_dbg(p2p, "Device Discoverability Response TX callback: success=%d",
  104. success);
  105. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  106. }
  107. static void p2p_send_dev_disc_resp(struct p2p_data *p2p, u8 dialog_token,
  108. const u8 *addr, int freq, u8 status)
  109. {
  110. struct wpabuf *resp;
  111. resp = p2p_build_dev_disc_resp(dialog_token, status);
  112. if (resp == NULL)
  113. return;
  114. p2p_dbg(p2p, "Sending Device Discoverability Response to " MACSTR
  115. " (status %u freq %d)",
  116. MAC2STR(addr), status, freq);
  117. p2p->pending_action_state = P2P_PENDING_DEV_DISC_RESPONSE;
  118. if (p2p_send_action(p2p, freq, addr, p2p->cfg->dev_addr,
  119. p2p->cfg->dev_addr,
  120. wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
  121. p2p_dbg(p2p, "Failed to send Action frame");
  122. }
  123. wpabuf_free(resp);
  124. }
  125. void p2p_process_dev_disc_req(struct p2p_data *p2p, const u8 *sa,
  126. const u8 *data, size_t len, int rx_freq)
  127. {
  128. struct p2p_message msg;
  129. size_t g;
  130. p2p_dbg(p2p, "Received Device Discoverability Request from " MACSTR
  131. " (freq=%d)", MAC2STR(sa), rx_freq);
  132. if (p2p_parse(data, len, &msg))
  133. return;
  134. if (msg.dialog_token == 0) {
  135. p2p_dbg(p2p, "Invalid Dialog Token 0 (must be nonzero) in Device Discoverability Request");
  136. p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
  137. P2P_SC_FAIL_INVALID_PARAMS);
  138. p2p_parse_free(&msg);
  139. return;
  140. }
  141. if (msg.device_id == NULL) {
  142. p2p_dbg(p2p, "P2P Device ID attribute missing from Device Discoverability Request");
  143. p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
  144. P2P_SC_FAIL_INVALID_PARAMS);
  145. p2p_parse_free(&msg);
  146. return;
  147. }
  148. for (g = 0; g < p2p->num_groups; g++) {
  149. if (p2p_group_go_discover(p2p->groups[g], msg.device_id, sa,
  150. rx_freq) == 0) {
  151. p2p_dbg(p2p, "Scheduled GO Discoverability Request for the target device");
  152. /*
  153. * P2P group code will use a callback to indicate TX
  154. * status, so that we can reply to the request once the
  155. * target client has acknowledged the request or it has
  156. * timed out.
  157. */
  158. p2p->pending_dev_disc_dialog_token = msg.dialog_token;
  159. os_memcpy(p2p->pending_dev_disc_addr, sa, ETH_ALEN);
  160. p2p->pending_dev_disc_freq = rx_freq;
  161. p2p_parse_free(&msg);
  162. return;
  163. }
  164. }
  165. p2p_dbg(p2p, "Requested client was not found in any group or did not support client discoverability");
  166. p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
  167. P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE);
  168. p2p_parse_free(&msg);
  169. }
  170. void p2p_process_dev_disc_resp(struct p2p_data *p2p, const u8 *sa,
  171. const u8 *data, size_t len)
  172. {
  173. struct p2p_message msg;
  174. struct p2p_device *go;
  175. u8 status;
  176. p2p_dbg(p2p, "Received Device Discoverability Response from " MACSTR,
  177. MAC2STR(sa));
  178. go = p2p->pending_client_disc_go;
  179. if (go == NULL ||
  180. os_memcmp(sa, go->info.p2p_device_addr, ETH_ALEN) != 0) {
  181. p2p_dbg(p2p, "Ignore unexpected Device Discoverability Response");
  182. return;
  183. }
  184. if (p2p_parse(data, len, &msg))
  185. return;
  186. if (msg.status == NULL) {
  187. p2p_parse_free(&msg);
  188. return;
  189. }
  190. if (msg.dialog_token != go->dialog_token) {
  191. p2p_dbg(p2p, "Ignore Device Discoverability Response with unexpected dialog token %u (expected %u)",
  192. msg.dialog_token, go->dialog_token);
  193. p2p_parse_free(&msg);
  194. return;
  195. }
  196. status = *msg.status;
  197. p2p_parse_free(&msg);
  198. p2p_dbg(p2p, "Device Discoverability Response status %u", status);
  199. if (p2p->go_neg_peer == NULL ||
  200. os_memcmp(p2p->pending_client_disc_addr,
  201. p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN) != 0 ||
  202. os_memcmp(p2p->go_neg_peer->member_in_go_dev,
  203. go->info.p2p_device_addr, ETH_ALEN) != 0) {
  204. p2p_dbg(p2p, "No pending operation with the client discoverability peer anymore");
  205. return;
  206. }
  207. if (status == 0) {
  208. /*
  209. * Peer is expected to be awake for at least 100 TU; try to
  210. * connect immediately.
  211. */
  212. p2p_dbg(p2p, "Client discoverability request succeeded");
  213. if (p2p->state == P2P_CONNECT) {
  214. /*
  215. * Change state to force the timeout to start in
  216. * P2P_CONNECT again without going through the short
  217. * Listen state.
  218. */
  219. p2p_set_state(p2p, P2P_CONNECT_LISTEN);
  220. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  221. }
  222. p2p_set_timeout(p2p, 0, 0);
  223. } else {
  224. /*
  225. * Client discoverability request failed; try to connect from
  226. * timeout.
  227. */
  228. p2p_dbg(p2p, "Client discoverability request failed");
  229. p2p_set_timeout(p2p, 0, 500000);
  230. }
  231. }
  232. void p2p_go_disc_req_cb(struct p2p_data *p2p, int success)
  233. {
  234. p2p_dbg(p2p, "GO Discoverability Request TX callback: success=%d",
  235. success);
  236. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  237. if (p2p->pending_dev_disc_dialog_token == 0) {
  238. p2p_dbg(p2p, "No pending Device Discoverability Request");
  239. return;
  240. }
  241. p2p_send_dev_disc_resp(p2p, p2p->pending_dev_disc_dialog_token,
  242. p2p->pending_dev_disc_addr,
  243. p2p->pending_dev_disc_freq,
  244. success ? P2P_SC_SUCCESS :
  245. P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE);
  246. p2p->pending_dev_disc_dialog_token = 0;
  247. }
  248. void p2p_process_go_disc_req(struct p2p_data *p2p, const u8 *da, const u8 *sa,
  249. const u8 *data, size_t len, int rx_freq)
  250. {
  251. unsigned int tu;
  252. struct wpabuf *ies;
  253. p2p_dbg(p2p, "Received GO Discoverability Request - remain awake for 100 TU");
  254. ies = p2p_build_probe_resp_ies(p2p);
  255. if (ies == NULL)
  256. return;
  257. /* Remain awake 100 TU on operating channel */
  258. p2p->pending_client_disc_freq = rx_freq;
  259. tu = 100;
  260. if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, rx_freq, 1024 * tu / 1000,
  261. ies) < 0) {
  262. p2p_dbg(p2p, "Failed to start listen mode for client discoverability");
  263. }
  264. wpabuf_free(ies);
  265. }