p2p_pd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Wi-Fi Direct - P2P provision discovery
  3. * Copyright (c) 2009-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 "wps/wps_defs.h"
  12. #include "p2p_i.h"
  13. #include "p2p.h"
  14. /*
  15. * Number of retries to attempt for provision discovery requests
  16. * in case the peer is not listening.
  17. */
  18. #define MAX_PROV_DISC_REQ_RETRIES 120
  19. static void p2p_build_wps_ie_config_methods(struct wpabuf *buf,
  20. u16 config_methods)
  21. {
  22. u8 *len;
  23. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  24. len = wpabuf_put(buf, 1);
  25. wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
  26. /* Config Methods */
  27. wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
  28. wpabuf_put_be16(buf, 2);
  29. wpabuf_put_be16(buf, config_methods);
  30. p2p_buf_update_ie_hdr(buf, len);
  31. }
  32. static struct wpabuf * p2p_build_prov_disc_req(struct p2p_data *p2p,
  33. u8 dialog_token,
  34. u16 config_methods,
  35. struct p2p_device *go)
  36. {
  37. struct wpabuf *buf;
  38. u8 *len;
  39. size_t extra = 0;
  40. #ifdef CONFIG_WIFI_DISPLAY
  41. if (p2p->wfd_ie_prov_disc_req)
  42. extra = wpabuf_len(p2p->wfd_ie_prov_disc_req);
  43. #endif /* CONFIG_WIFI_DISPLAY */
  44. buf = wpabuf_alloc(1000 + extra);
  45. if (buf == NULL)
  46. return NULL;
  47. p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_REQ, dialog_token);
  48. len = p2p_buf_add_ie_hdr(buf);
  49. p2p_buf_add_capability(buf, p2p->dev_capab &
  50. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 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. #ifdef CONFIG_WIFI_DISPLAY
  60. if (p2p->wfd_ie_prov_disc_req)
  61. wpabuf_put_buf(buf, p2p->wfd_ie_prov_disc_req);
  62. #endif /* CONFIG_WIFI_DISPLAY */
  63. return buf;
  64. }
  65. static struct wpabuf * p2p_build_prov_disc_resp(struct p2p_data *p2p,
  66. u8 dialog_token,
  67. u16 config_methods,
  68. const u8 *group_id,
  69. size_t group_id_len)
  70. {
  71. struct wpabuf *buf;
  72. size_t extra = 0;
  73. #ifdef CONFIG_WIFI_DISPLAY
  74. struct wpabuf *wfd_ie = p2p->wfd_ie_prov_disc_resp;
  75. if (wfd_ie && group_id) {
  76. size_t i;
  77. for (i = 0; i < p2p->num_groups; i++) {
  78. struct p2p_group *g = p2p->groups[i];
  79. struct wpabuf *ie;
  80. if (!p2p_group_is_group_id_match(g, group_id,
  81. group_id_len))
  82. continue;
  83. ie = p2p_group_get_wfd_ie(g);
  84. if (ie) {
  85. wfd_ie = ie;
  86. break;
  87. }
  88. }
  89. }
  90. if (wfd_ie)
  91. extra = wpabuf_len(wfd_ie);
  92. #endif /* CONFIG_WIFI_DISPLAY */
  93. buf = wpabuf_alloc(100 + extra);
  94. if (buf == NULL)
  95. return NULL;
  96. p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_RESP, dialog_token);
  97. /* WPS IE with Config Methods attribute */
  98. p2p_build_wps_ie_config_methods(buf, config_methods);
  99. #ifdef CONFIG_WIFI_DISPLAY
  100. if (wfd_ie)
  101. wpabuf_put_buf(buf, wfd_ie);
  102. #endif /* CONFIG_WIFI_DISPLAY */
  103. return buf;
  104. }
  105. void p2p_process_prov_disc_req(struct p2p_data *p2p, const u8 *sa,
  106. const u8 *data, size_t len, int rx_freq)
  107. {
  108. struct p2p_message msg;
  109. struct p2p_device *dev;
  110. int freq;
  111. int reject = 1;
  112. struct wpabuf *resp;
  113. if (p2p_parse(data, len, &msg))
  114. return;
  115. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  116. "P2P: Received Provision Discovery Request from " MACSTR
  117. " with config methods 0x%x (freq=%d)",
  118. MAC2STR(sa), msg.wps_config_methods, rx_freq);
  119. dev = p2p_get_device(p2p, sa);
  120. if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
  121. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  122. "P2P: Provision Discovery Request from "
  123. "unknown peer " MACSTR, MAC2STR(sa));
  124. if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
  125. 0)) {
  126. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  127. "P2P: Provision Discovery Request add device "
  128. "failed " MACSTR, MAC2STR(sa));
  129. }
  130. } else if (msg.wfd_subelems) {
  131. wpabuf_free(dev->info.wfd_subelems);
  132. dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
  133. }
  134. if (!(msg.wps_config_methods &
  135. (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD |
  136. WPS_CONFIG_PUSHBUTTON))) {
  137. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unsupported "
  138. "Config Methods in Provision Discovery Request");
  139. goto out;
  140. }
  141. if (msg.group_id) {
  142. size_t i;
  143. for (i = 0; i < p2p->num_groups; i++) {
  144. if (p2p_group_is_group_id_match(p2p->groups[i],
  145. msg.group_id,
  146. msg.group_id_len))
  147. break;
  148. }
  149. if (i == p2p->num_groups) {
  150. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: PD "
  151. "request for unknown P2P Group ID - reject");
  152. goto out;
  153. }
  154. }
  155. if (dev)
  156. dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
  157. P2P_DEV_PD_PEER_KEYPAD);
  158. if (msg.wps_config_methods & WPS_CONFIG_DISPLAY) {
  159. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
  160. " requested us to show a PIN on display", MAC2STR(sa));
  161. if (dev)
  162. dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
  163. } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
  164. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
  165. " requested us to write its PIN using keypad",
  166. MAC2STR(sa));
  167. if (dev)
  168. dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
  169. }
  170. reject = 0;
  171. out:
  172. resp = p2p_build_prov_disc_resp(p2p, msg.dialog_token,
  173. reject ? 0 : msg.wps_config_methods,
  174. msg.group_id, msg.group_id_len);
  175. if (resp == NULL) {
  176. p2p_parse_free(&msg);
  177. return;
  178. }
  179. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  180. "P2P: Sending Provision Discovery Response");
  181. if (rx_freq > 0)
  182. freq = rx_freq;
  183. else
  184. freq = p2p_channel_to_freq(p2p->cfg->country,
  185. p2p->cfg->reg_class,
  186. p2p->cfg->channel);
  187. if (freq < 0) {
  188. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  189. "P2P: Unknown regulatory class/channel");
  190. wpabuf_free(resp);
  191. p2p_parse_free(&msg);
  192. return;
  193. }
  194. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  195. if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
  196. p2p->cfg->dev_addr,
  197. wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
  198. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  199. "P2P: Failed to send Action frame");
  200. }
  201. wpabuf_free(resp);
  202. if (!reject && p2p->cfg->prov_disc_req) {
  203. const u8 *dev_addr = sa;
  204. if (msg.p2p_device_addr)
  205. dev_addr = msg.p2p_device_addr;
  206. p2p->cfg->prov_disc_req(p2p->cfg->cb_ctx, sa,
  207. msg.wps_config_methods,
  208. dev_addr, msg.pri_dev_type,
  209. msg.device_name, msg.config_methods,
  210. msg.capability ? msg.capability[0] : 0,
  211. msg.capability ? msg.capability[1] :
  212. 0,
  213. msg.group_id, msg.group_id_len);
  214. }
  215. p2p_parse_free(&msg);
  216. }
  217. void p2p_process_prov_disc_resp(struct p2p_data *p2p, const u8 *sa,
  218. const u8 *data, size_t len)
  219. {
  220. struct p2p_message msg;
  221. struct p2p_device *dev;
  222. u16 report_config_methods = 0, req_config_methods;
  223. int success = 0;
  224. if (p2p_parse(data, len, &msg))
  225. return;
  226. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  227. "P2P: Received Provision Discovery Response from " MACSTR
  228. " with config methods 0x%x",
  229. MAC2STR(sa), msg.wps_config_methods);
  230. dev = p2p_get_device(p2p, sa);
  231. if (dev == NULL || !dev->req_config_methods) {
  232. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  233. "P2P: Ignore Provision Discovery Response from "
  234. MACSTR " with no pending request", MAC2STR(sa));
  235. p2p_parse_free(&msg);
  236. return;
  237. }
  238. if (dev->dialog_token != msg.dialog_token) {
  239. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  240. "P2P: Ignore Provision Discovery Response with "
  241. "unexpected Dialog Token %u (expected %u)",
  242. msg.dialog_token, dev->dialog_token);
  243. p2p_parse_free(&msg);
  244. return;
  245. }
  246. if (p2p->pending_action_state == P2P_PENDING_PD) {
  247. os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
  248. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  249. }
  250. /*
  251. * Use a local copy of the requested config methods since
  252. * p2p_reset_pending_pd() can clear this in the peer entry.
  253. */
  254. req_config_methods = dev->req_config_methods;
  255. /*
  256. * If the response is from the peer to whom a user initiated request
  257. * was sent earlier, we reset that state info here.
  258. */
  259. if (p2p->user_initiated_pd &&
  260. os_memcmp(p2p->pending_pd_devaddr, sa, ETH_ALEN) == 0)
  261. p2p_reset_pending_pd(p2p);
  262. if (msg.wps_config_methods != req_config_methods) {
  263. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer rejected "
  264. "our Provision Discovery Request (received "
  265. "config_methods 0x%x expected 0x%x",
  266. msg.wps_config_methods, req_config_methods);
  267. if (p2p->cfg->prov_disc_fail)
  268. p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, sa,
  269. P2P_PROV_DISC_REJECTED);
  270. p2p_parse_free(&msg);
  271. goto out;
  272. }
  273. report_config_methods = req_config_methods;
  274. dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
  275. P2P_DEV_PD_PEER_KEYPAD);
  276. if (req_config_methods & WPS_CONFIG_DISPLAY) {
  277. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
  278. " accepted to show a PIN on display", MAC2STR(sa));
  279. dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
  280. } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
  281. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
  282. " accepted to write our PIN using keypad",
  283. MAC2STR(sa));
  284. dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
  285. }
  286. /* Store the provisioning info */
  287. dev->wps_prov_info = msg.wps_config_methods;
  288. p2p_parse_free(&msg);
  289. success = 1;
  290. out:
  291. dev->req_config_methods = 0;
  292. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  293. if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
  294. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  295. "P2P: Start GO Neg after the PD-before-GO-Neg "
  296. "workaround with " MACSTR,
  297. MAC2STR(dev->info.p2p_device_addr));
  298. dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
  299. p2p_connect_send(p2p, dev);
  300. return;
  301. }
  302. if (success && p2p->cfg->prov_disc_resp)
  303. p2p->cfg->prov_disc_resp(p2p->cfg->cb_ctx, sa,
  304. report_config_methods);
  305. if (p2p->state == P2P_PD_DURING_FIND) {
  306. p2p_clear_timeout(p2p);
  307. p2p_continue_find(p2p);
  308. }
  309. }
  310. int p2p_send_prov_disc_req(struct p2p_data *p2p, struct p2p_device *dev,
  311. int join, int force_freq)
  312. {
  313. struct wpabuf *req;
  314. int freq;
  315. if (force_freq > 0)
  316. freq = force_freq;
  317. else
  318. freq = dev->listen_freq > 0 ? dev->listen_freq :
  319. dev->oper_freq;
  320. if (freq <= 0) {
  321. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  322. "P2P: No Listen/Operating frequency known for the "
  323. "peer " MACSTR " to send Provision Discovery Request",
  324. MAC2STR(dev->info.p2p_device_addr));
  325. return -1;
  326. }
  327. if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
  328. if (!(dev->info.dev_capab &
  329. P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
  330. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  331. "P2P: Cannot use PD with P2P Device " MACSTR
  332. " that is in a group and is not discoverable",
  333. MAC2STR(dev->info.p2p_device_addr));
  334. return -1;
  335. }
  336. /* TODO: use device discoverability request through GO */
  337. }
  338. req = p2p_build_prov_disc_req(p2p, dev->dialog_token,
  339. dev->req_config_methods,
  340. join ? dev : NULL);
  341. if (req == NULL)
  342. return -1;
  343. if (p2p->state != P2P_IDLE)
  344. p2p_stop_listen_for_freq(p2p, freq);
  345. p2p->pending_action_state = P2P_PENDING_PD;
  346. if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
  347. p2p->cfg->dev_addr, dev->info.p2p_device_addr,
  348. wpabuf_head(req), wpabuf_len(req), 200) < 0) {
  349. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  350. "P2P: Failed to send Action frame");
  351. wpabuf_free(req);
  352. return -1;
  353. }
  354. os_memcpy(p2p->pending_pd_devaddr, dev->info.p2p_device_addr, ETH_ALEN);
  355. wpabuf_free(req);
  356. return 0;
  357. }
  358. int p2p_prov_disc_req(struct p2p_data *p2p, const u8 *peer_addr,
  359. u16 config_methods, int join, int force_freq,
  360. int user_initiated_pd)
  361. {
  362. struct p2p_device *dev;
  363. dev = p2p_get_device(p2p, peer_addr);
  364. if (dev == NULL)
  365. dev = p2p_get_device_interface(p2p, peer_addr);
  366. if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
  367. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision "
  368. "Discovery Request destination " MACSTR
  369. " not yet known", MAC2STR(peer_addr));
  370. return -1;
  371. }
  372. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision Discovery "
  373. "Request with " MACSTR " (config methods 0x%x)",
  374. MAC2STR(peer_addr), config_methods);
  375. if (config_methods == 0)
  376. return -1;
  377. /* Reset provisioning info */
  378. dev->wps_prov_info = 0;
  379. dev->req_config_methods = config_methods;
  380. if (join)
  381. dev->flags |= P2P_DEV_PD_FOR_JOIN;
  382. else
  383. dev->flags &= ~P2P_DEV_PD_FOR_JOIN;
  384. if (p2p->state != P2P_IDLE && p2p->state != P2P_SEARCH &&
  385. p2p->state != P2P_LISTEN_ONLY) {
  386. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Busy with other "
  387. "operations; postpone Provision Discovery Request "
  388. "with " MACSTR " (config methods 0x%x)",
  389. MAC2STR(peer_addr), config_methods);
  390. return 0;
  391. }
  392. p2p->user_initiated_pd = user_initiated_pd;
  393. if (p2p->user_initiated_pd)
  394. p2p->pd_retries = MAX_PROV_DISC_REQ_RETRIES;
  395. /*
  396. * Assign dialog token here to use the same value in each retry within
  397. * the same PD exchange.
  398. */
  399. dev->dialog_token++;
  400. if (dev->dialog_token == 0)
  401. dev->dialog_token = 1;
  402. return p2p_send_prov_disc_req(p2p, dev, join, force_freq);
  403. }
  404. void p2p_reset_pending_pd(struct p2p_data *p2p)
  405. {
  406. struct p2p_device *dev;
  407. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  408. if (os_memcmp(p2p->pending_pd_devaddr,
  409. dev->info.p2p_device_addr, ETH_ALEN))
  410. continue;
  411. if (!dev->req_config_methods)
  412. continue;
  413. if (dev->flags & P2P_DEV_PD_FOR_JOIN)
  414. continue;
  415. /* Reset the config methods of the device */
  416. dev->req_config_methods = 0;
  417. }
  418. p2p->user_initiated_pd = 0;
  419. os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
  420. p2p->pd_retries = 0;
  421. }