p2p_pd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Wi-Fi Direct - P2P provision discovery
  3. * Copyright (c) 2009-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 "wps/wps_defs.h"
  18. #include "p2p_i.h"
  19. #include "p2p.h"
  20. /*
  21. * Number of retries to attempt for provision discovery requests during IDLE
  22. * state in case the peer is not listening.
  23. */
  24. #define MAX_PROV_DISC_REQ_RETRIES 10
  25. static void p2p_build_wps_ie_config_methods(struct wpabuf *buf,
  26. u16 config_methods)
  27. {
  28. u8 *len;
  29. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  30. len = wpabuf_put(buf, 1);
  31. wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
  32. /* Config Methods */
  33. wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
  34. wpabuf_put_be16(buf, 2);
  35. wpabuf_put_be16(buf, config_methods);
  36. p2p_buf_update_ie_hdr(buf, len);
  37. }
  38. static struct wpabuf * p2p_build_prov_disc_req(struct p2p_data *p2p,
  39. u8 dialog_token,
  40. u16 config_methods,
  41. struct p2p_device *go)
  42. {
  43. struct wpabuf *buf;
  44. u8 *len;
  45. buf = wpabuf_alloc(1000);
  46. if (buf == NULL)
  47. return NULL;
  48. p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_REQ, dialog_token);
  49. len = p2p_buf_add_ie_hdr(buf);
  50. p2p_buf_add_capability(buf, p2p->dev_capab, 0);
  51. p2p_buf_add_device_info(buf, p2p, NULL);
  52. if (go) {
  53. p2p_buf_add_group_id(buf, go->info.p2p_device_addr,
  54. go->oper_ssid, go->oper_ssid_len);
  55. }
  56. p2p_buf_update_ie_hdr(buf, len);
  57. /* WPS IE with Config Methods attribute */
  58. p2p_build_wps_ie_config_methods(buf, config_methods);
  59. return buf;
  60. }
  61. static struct wpabuf * p2p_build_prov_disc_resp(struct p2p_data *p2p,
  62. u8 dialog_token,
  63. u16 config_methods)
  64. {
  65. struct wpabuf *buf;
  66. buf = wpabuf_alloc(100);
  67. if (buf == NULL)
  68. return NULL;
  69. p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_RESP, dialog_token);
  70. /* WPS IE with Config Methods attribute */
  71. p2p_build_wps_ie_config_methods(buf, config_methods);
  72. return buf;
  73. }
  74. void p2p_process_prov_disc_req(struct p2p_data *p2p, const u8 *sa,
  75. const u8 *data, size_t len, int rx_freq)
  76. {
  77. struct p2p_message msg;
  78. struct p2p_device *dev;
  79. int freq;
  80. int reject = 1;
  81. struct wpabuf *resp;
  82. if (p2p_parse(data, len, &msg))
  83. return;
  84. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  85. "P2P: Received Provision Discovery Request from " MACSTR
  86. " with config methods 0x%x (freq=%d)",
  87. MAC2STR(sa), msg.wps_config_methods, rx_freq);
  88. dev = p2p_get_device(p2p, sa);
  89. if (dev == NULL || !(dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
  90. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  91. "P2P: Provision Discovery Request from "
  92. "unknown peer " MACSTR, MAC2STR(sa));
  93. if (p2p_add_device(p2p, sa, rx_freq, 0, data + 1, len - 1)) {
  94. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  95. "P2P: Provision Discovery Request add device "
  96. "failed " MACSTR, MAC2STR(sa));
  97. }
  98. }
  99. if (!(msg.wps_config_methods &
  100. (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD |
  101. WPS_CONFIG_PUSHBUTTON))) {
  102. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unsupported "
  103. "Config Methods in Provision Discovery Request");
  104. goto out;
  105. }
  106. if (dev)
  107. dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
  108. P2P_DEV_PD_PEER_KEYPAD);
  109. if (msg.wps_config_methods & WPS_CONFIG_DISPLAY) {
  110. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
  111. " requested us to show a PIN on display", MAC2STR(sa));
  112. if (dev)
  113. dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
  114. } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
  115. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
  116. " requested us to write its PIN using keypad",
  117. MAC2STR(sa));
  118. if (dev)
  119. dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
  120. }
  121. reject = 0;
  122. out:
  123. resp = p2p_build_prov_disc_resp(p2p, msg.dialog_token,
  124. reject ? 0 : msg.wps_config_methods);
  125. if (resp == NULL) {
  126. p2p_parse_free(&msg);
  127. return;
  128. }
  129. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  130. "P2P: Sending Provision Discovery Response");
  131. if (rx_freq > 0)
  132. freq = rx_freq;
  133. else
  134. freq = p2p_channel_to_freq(p2p->cfg->country,
  135. p2p->cfg->reg_class,
  136. p2p->cfg->channel);
  137. if (freq < 0) {
  138. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  139. "P2P: Unknown regulatory class/channel");
  140. wpabuf_free(resp);
  141. p2p_parse_free(&msg);
  142. return;
  143. }
  144. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  145. if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
  146. p2p->cfg->dev_addr,
  147. wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
  148. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  149. "P2P: Failed to send Action frame");
  150. }
  151. wpabuf_free(resp);
  152. if (!reject && p2p->cfg->prov_disc_req) {
  153. const u8 *dev_addr = sa;
  154. if (msg.p2p_device_addr)
  155. dev_addr = msg.p2p_device_addr;
  156. p2p->cfg->prov_disc_req(p2p->cfg->cb_ctx, sa,
  157. msg.wps_config_methods,
  158. dev_addr, msg.pri_dev_type,
  159. msg.device_name, msg.config_methods,
  160. msg.capability ? msg.capability[0] : 0,
  161. msg.capability ? msg.capability[1] :
  162. 0,
  163. msg.group_id, msg.group_id_len);
  164. }
  165. p2p_parse_free(&msg);
  166. }
  167. void p2p_process_prov_disc_resp(struct p2p_data *p2p, const u8 *sa,
  168. const u8 *data, size_t len)
  169. {
  170. struct p2p_message msg;
  171. struct p2p_device *dev;
  172. u16 report_config_methods = 0;
  173. if (p2p_parse(data, len, &msg))
  174. return;
  175. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  176. "P2P: Received Provisioning Discovery Response from " MACSTR
  177. " with config methods 0x%x",
  178. MAC2STR(sa), msg.wps_config_methods);
  179. dev = p2p_get_device(p2p, sa);
  180. if (dev == NULL || !dev->req_config_methods) {
  181. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  182. "P2P: Ignore Provisioning Discovery Response from "
  183. MACSTR " with no pending request", MAC2STR(sa));
  184. p2p_parse_free(&msg);
  185. return;
  186. }
  187. if (p2p->pending_action_state == P2P_PENDING_PD) {
  188. os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
  189. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  190. }
  191. if (dev->dialog_token != msg.dialog_token) {
  192. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  193. "P2P: Ignore Provisioning Discovery Response with "
  194. "unexpected Dialog Token %u (expected %u)",
  195. msg.dialog_token, dev->dialog_token);
  196. p2p_parse_free(&msg);
  197. return;
  198. }
  199. /*
  200. * If the response is from the peer to whom a user initiated request
  201. * was sent earlier, we reset that state info here.
  202. */
  203. if (p2p->user_initiated_pd &&
  204. os_memcmp(p2p->pending_pd_devaddr, sa, ETH_ALEN) == 0)
  205. p2p_reset_pending_pd(p2p);
  206. if (msg.wps_config_methods != dev->req_config_methods) {
  207. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer rejected "
  208. "our Provisioning Discovery Request");
  209. if (p2p->cfg->prov_disc_fail)
  210. p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, sa,
  211. P2P_PROV_DISC_REJECTED);
  212. p2p_parse_free(&msg);
  213. goto out;
  214. }
  215. report_config_methods = dev->req_config_methods;
  216. dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
  217. P2P_DEV_PD_PEER_KEYPAD);
  218. if (dev->req_config_methods & WPS_CONFIG_DISPLAY) {
  219. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
  220. " accepted to show a PIN on display", MAC2STR(sa));
  221. dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
  222. } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
  223. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
  224. " accepted to write our PIN using keypad",
  225. MAC2STR(sa));
  226. dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
  227. }
  228. /* Store the provisioning info */
  229. dev->wps_prov_info = msg.wps_config_methods;
  230. p2p_parse_free(&msg);
  231. out:
  232. dev->req_config_methods = 0;
  233. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  234. if (p2p->cfg->prov_disc_resp)
  235. p2p->cfg->prov_disc_resp(p2p->cfg->cb_ctx, sa,
  236. report_config_methods);
  237. }
  238. int p2p_send_prov_disc_req(struct p2p_data *p2p, struct p2p_device *dev,
  239. int join)
  240. {
  241. struct wpabuf *req;
  242. int freq;
  243. freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
  244. if (freq <= 0) {
  245. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  246. "P2P: No Listen/Operating frequency known for the "
  247. "peer " MACSTR " to send Provision Discovery Request",
  248. MAC2STR(dev->info.p2p_device_addr));
  249. return -1;
  250. }
  251. if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
  252. if (!(dev->info.dev_capab &
  253. P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
  254. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  255. "P2P: Cannot use PD with P2P Device " MACSTR
  256. " that is in a group and is not discoverable",
  257. MAC2STR(dev->info.p2p_device_addr));
  258. return -1;
  259. }
  260. /* TODO: use device discoverability request through GO */
  261. }
  262. dev->dialog_token++;
  263. if (dev->dialog_token == 0)
  264. dev->dialog_token = 1;
  265. req = p2p_build_prov_disc_req(p2p, dev->dialog_token,
  266. dev->req_config_methods,
  267. join ? dev : NULL);
  268. if (req == NULL)
  269. return -1;
  270. p2p->pending_action_state = P2P_PENDING_PD;
  271. if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
  272. p2p->cfg->dev_addr, dev->info.p2p_device_addr,
  273. wpabuf_head(req), wpabuf_len(req), 200) < 0) {
  274. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  275. "P2P: Failed to send Action frame");
  276. wpabuf_free(req);
  277. return -1;
  278. }
  279. os_memcpy(p2p->pending_pd_devaddr, dev->info.p2p_device_addr, ETH_ALEN);
  280. wpabuf_free(req);
  281. return 0;
  282. }
  283. int p2p_prov_disc_req(struct p2p_data *p2p, const u8 *peer_addr,
  284. u16 config_methods, int join)
  285. {
  286. struct p2p_device *dev;
  287. dev = p2p_get_device(p2p, peer_addr);
  288. if (dev == NULL)
  289. dev = p2p_get_device_interface(p2p, peer_addr);
  290. if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
  291. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision "
  292. "Discovery Request destination " MACSTR
  293. " not yet known", MAC2STR(peer_addr));
  294. return -1;
  295. }
  296. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision Discovery "
  297. "Request with " MACSTR " (config methods 0x%x)",
  298. MAC2STR(peer_addr), config_methods);
  299. if (config_methods == 0)
  300. return -1;
  301. /* Reset provisioning info */
  302. dev->wps_prov_info = 0;
  303. dev->req_config_methods = config_methods;
  304. if (join)
  305. dev->flags |= P2P_DEV_PD_FOR_JOIN;
  306. else
  307. dev->flags &= ~P2P_DEV_PD_FOR_JOIN;
  308. if (p2p->go_neg_peer ||
  309. (p2p->state != P2P_IDLE && p2p->state != P2P_SEARCH &&
  310. p2p->state != P2P_LISTEN_ONLY)) {
  311. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Busy with other "
  312. "operations; postpone Provision Discovery Request "
  313. "with " MACSTR " (config methods 0x%x)",
  314. MAC2STR(peer_addr), config_methods);
  315. return 0;
  316. }
  317. /*
  318. * We use the join param as a cue to differentiate between user
  319. * initiated PD request and one issued during finds (internal).
  320. */
  321. p2p->user_initiated_pd = !join;
  322. /* Also set some retries to attempt in case of IDLE state */
  323. if (p2p->user_initiated_pd && p2p->state == P2P_IDLE)
  324. p2p->pd_retries = MAX_PROV_DISC_REQ_RETRIES;
  325. return p2p_send_prov_disc_req(p2p, dev, join);
  326. }
  327. void p2p_reset_pending_pd(struct p2p_data *p2p)
  328. {
  329. p2p->user_initiated_pd = 0;
  330. os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
  331. p2p->pd_retries = 0;
  332. }