p2p_invitation.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Wi-Fi Direct - P2P Invitation 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 "common/wpa_ctrl.h"
  12. #include "p2p_i.h"
  13. #include "p2p.h"
  14. static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
  15. struct p2p_device *peer,
  16. const u8 *go_dev_addr,
  17. int dev_pw_id)
  18. {
  19. struct wpabuf *buf;
  20. u8 *len;
  21. const u8 *dev_addr;
  22. size_t extra = 0;
  23. #ifdef CONFIG_WIFI_DISPLAY
  24. struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
  25. if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
  26. size_t i;
  27. for (i = 0; i < p2p->num_groups; i++) {
  28. struct p2p_group *g = p2p->groups[i];
  29. struct wpabuf *ie;
  30. if (os_memcmp(p2p_group_get_interface_addr(g),
  31. p2p->inv_bssid, ETH_ALEN) != 0)
  32. continue;
  33. ie = p2p_group_get_wfd_ie(g);
  34. if (ie) {
  35. wfd_ie = ie;
  36. break;
  37. }
  38. }
  39. }
  40. if (wfd_ie)
  41. extra = wpabuf_len(wfd_ie);
  42. #endif /* CONFIG_WIFI_DISPLAY */
  43. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ])
  44. extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]);
  45. buf = wpabuf_alloc(1000 + extra);
  46. if (buf == NULL)
  47. return NULL;
  48. peer->dialog_token++;
  49. if (peer->dialog_token == 0)
  50. peer->dialog_token = 1;
  51. p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
  52. peer->dialog_token);
  53. len = p2p_buf_add_ie_hdr(buf);
  54. if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
  55. p2p_buf_add_config_timeout(buf, 0, 0);
  56. else
  57. p2p_buf_add_config_timeout(buf, p2p->go_timeout,
  58. p2p->client_timeout);
  59. p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
  60. P2P_INVITATION_FLAGS_TYPE : 0);
  61. if (p2p->inv_role != P2P_INVITE_ROLE_CLIENT ||
  62. !(peer->flags & P2P_DEV_NO_PREF_CHAN))
  63. p2p_buf_add_operating_channel(buf, p2p->cfg->country,
  64. p2p->op_reg_class,
  65. p2p->op_channel);
  66. if (p2p->inv_bssid_set)
  67. p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
  68. p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
  69. if (go_dev_addr)
  70. dev_addr = go_dev_addr;
  71. else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
  72. dev_addr = peer->info.p2p_device_addr;
  73. else
  74. dev_addr = p2p->cfg->dev_addr;
  75. p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
  76. p2p_buf_add_device_info(buf, p2p, peer);
  77. p2p_buf_update_ie_hdr(buf, len);
  78. #ifdef CONFIG_WIFI_DISPLAY
  79. if (wfd_ie)
  80. wpabuf_put_buf(buf, wfd_ie);
  81. #endif /* CONFIG_WIFI_DISPLAY */
  82. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ])
  83. wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]);
  84. if (dev_pw_id >= 0) {
  85. /* WSC IE in Invitation Request for NFC static handover */
  86. p2p_build_wps_ie(p2p, buf, dev_pw_id, 0);
  87. }
  88. return buf;
  89. }
  90. static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
  91. struct p2p_device *peer,
  92. u8 dialog_token, u8 status,
  93. const u8 *group_bssid,
  94. u8 reg_class, u8 channel,
  95. struct p2p_channels *channels)
  96. {
  97. struct wpabuf *buf;
  98. u8 *len;
  99. size_t extra = 0;
  100. #ifdef CONFIG_WIFI_DISPLAY
  101. struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
  102. if (wfd_ie && group_bssid) {
  103. size_t i;
  104. for (i = 0; i < p2p->num_groups; i++) {
  105. struct p2p_group *g = p2p->groups[i];
  106. struct wpabuf *ie;
  107. if (os_memcmp(p2p_group_get_interface_addr(g),
  108. group_bssid, ETH_ALEN) != 0)
  109. continue;
  110. ie = p2p_group_get_wfd_ie(g);
  111. if (ie) {
  112. wfd_ie = ie;
  113. break;
  114. }
  115. }
  116. }
  117. if (wfd_ie)
  118. extra = wpabuf_len(wfd_ie);
  119. #endif /* CONFIG_WIFI_DISPLAY */
  120. buf = wpabuf_alloc(1000 + extra);
  121. if (buf == NULL)
  122. return NULL;
  123. p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
  124. dialog_token);
  125. len = p2p_buf_add_ie_hdr(buf);
  126. p2p_buf_add_status(buf, status);
  127. p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
  128. if (reg_class && channel)
  129. p2p_buf_add_operating_channel(buf, p2p->cfg->country,
  130. reg_class, channel);
  131. if (group_bssid)
  132. p2p_buf_add_group_bssid(buf, group_bssid);
  133. if (channels)
  134. p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
  135. p2p_buf_update_ie_hdr(buf, len);
  136. #ifdef CONFIG_WIFI_DISPLAY
  137. if (wfd_ie)
  138. wpabuf_put_buf(buf, wfd_ie);
  139. #endif /* CONFIG_WIFI_DISPLAY */
  140. return buf;
  141. }
  142. void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
  143. const u8 *data, size_t len, int rx_freq)
  144. {
  145. struct p2p_device *dev;
  146. struct p2p_message msg;
  147. struct wpabuf *resp = NULL;
  148. u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  149. int freq;
  150. int go = 0;
  151. u8 group_bssid[ETH_ALEN], *bssid;
  152. int op_freq = 0;
  153. u8 reg_class = 0, channel = 0;
  154. struct p2p_channels all_channels, intersection, *channels = NULL;
  155. int persistent;
  156. os_memset(group_bssid, 0, sizeof(group_bssid));
  157. p2p_dbg(p2p, "Received Invitation Request from " MACSTR " (freq=%d)",
  158. MAC2STR(sa), rx_freq);
  159. if (p2p_parse(data, len, &msg))
  160. return;
  161. dev = p2p_get_device(p2p, sa);
  162. if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
  163. p2p_dbg(p2p, "Invitation Request from unknown peer " MACSTR,
  164. MAC2STR(sa));
  165. if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
  166. 0)) {
  167. p2p_dbg(p2p, "Invitation Request add device failed "
  168. MACSTR, MAC2STR(sa));
  169. status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  170. goto fail;
  171. }
  172. dev = p2p_get_device(p2p, sa);
  173. if (dev == NULL) {
  174. p2p_dbg(p2p, "Reject Invitation Request from unknown peer "
  175. MACSTR, MAC2STR(sa));
  176. status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  177. goto fail;
  178. }
  179. }
  180. if (!msg.group_id || !msg.channel_list) {
  181. p2p_dbg(p2p, "Mandatory attribute missing in Invitation Request from "
  182. MACSTR, MAC2STR(sa));
  183. status = P2P_SC_FAIL_INVALID_PARAMS;
  184. goto fail;
  185. }
  186. if (msg.invitation_flags)
  187. persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
  188. else {
  189. /* Invitation Flags is a mandatory attribute starting from P2P
  190. * spec 1.06. As a backwards compatibility mechanism, assume
  191. * the request was for a persistent group if the attribute is
  192. * missing.
  193. */
  194. p2p_dbg(p2p, "Mandatory Invitation Flags attribute missing from Invitation Request");
  195. persistent = 1;
  196. }
  197. p2p_channels_union(&p2p->cfg->channels, &p2p->cfg->cli_channels,
  198. &all_channels);
  199. if (p2p_peer_channels_check(p2p, &all_channels, dev,
  200. msg.channel_list, msg.channel_list_len) <
  201. 0) {
  202. p2p_dbg(p2p, "No common channels found");
  203. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  204. goto fail;
  205. }
  206. p2p_channels_dump(p2p, "own channels", &p2p->cfg->channels);
  207. p2p_channels_dump(p2p, "own client channels", &all_channels);
  208. p2p_channels_dump(p2p, "peer channels", &dev->channels);
  209. p2p_channels_intersect(&all_channels, &dev->channels,
  210. &intersection);
  211. p2p_channels_dump(p2p, "intersection", &intersection);
  212. if (p2p->cfg->invitation_process) {
  213. status = p2p->cfg->invitation_process(
  214. p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
  215. msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
  216. &go, group_bssid, &op_freq, persistent, &intersection,
  217. msg.dev_password_id_present ? msg.dev_password_id : -1);
  218. }
  219. if (go) {
  220. p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
  221. &intersection);
  222. p2p_channels_dump(p2p, "intersection(GO)", &intersection);
  223. if (intersection.reg_classes == 0) {
  224. p2p_dbg(p2p, "No common channels found (GO)");
  225. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  226. goto fail;
  227. }
  228. }
  229. if (op_freq) {
  230. p2p_dbg(p2p, "Invitation processing forced frequency %d MHz",
  231. op_freq);
  232. if (p2p_freq_to_channel(op_freq, &reg_class, &channel) < 0) {
  233. p2p_dbg(p2p, "Unknown forced freq %d MHz from invitation_process()",
  234. op_freq);
  235. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  236. goto fail;
  237. }
  238. if (!p2p_channels_includes(&intersection, reg_class, channel))
  239. {
  240. p2p_dbg(p2p, "forced freq %d MHz not in the supported channels interaction",
  241. op_freq);
  242. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  243. goto fail;
  244. }
  245. if (status == P2P_SC_SUCCESS)
  246. channels = &intersection;
  247. } else {
  248. p2p_dbg(p2p, "No forced channel from invitation processing - figure out best one to use");
  249. /* Default to own configuration as a starting point */
  250. p2p->op_reg_class = p2p->cfg->op_reg_class;
  251. p2p->op_channel = p2p->cfg->op_channel;
  252. p2p_dbg(p2p, "Own default op_class %d channel %d",
  253. p2p->op_reg_class, p2p->op_channel);
  254. /* Use peer preference if specified and compatible */
  255. if (msg.operating_channel) {
  256. int req_freq;
  257. req_freq = p2p_channel_to_freq(
  258. msg.operating_channel[3],
  259. msg.operating_channel[4]);
  260. p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
  261. req_freq);
  262. if (req_freq > 0 &&
  263. p2p_channels_includes(&intersection,
  264. msg.operating_channel[3],
  265. msg.operating_channel[4])) {
  266. p2p->op_reg_class = msg.operating_channel[3];
  267. p2p->op_channel = msg.operating_channel[4];
  268. p2p_dbg(p2p, "Use peer preference op_class %d channel %d",
  269. p2p->op_reg_class, p2p->op_channel);
  270. } else {
  271. p2p_dbg(p2p, "Cannot use peer channel preference");
  272. }
  273. }
  274. /* Reselect the channel only for the case of the GO */
  275. if (go &&
  276. !p2p_channels_includes(&intersection, p2p->op_reg_class,
  277. p2p->op_channel)) {
  278. p2p_dbg(p2p, "Initially selected channel (op_class %d channel %d) not in channel intersection - try to reselect",
  279. p2p->op_reg_class, p2p->op_channel);
  280. p2p_reselect_channel(p2p, &intersection);
  281. p2p_dbg(p2p, "Re-selection result: op_class %d channel %d",
  282. p2p->op_reg_class, p2p->op_channel);
  283. if (!p2p_channels_includes(&intersection,
  284. p2p->op_reg_class,
  285. p2p->op_channel)) {
  286. p2p_dbg(p2p, "Peer does not support selected operating channel (reg_class=%u channel=%u)",
  287. p2p->op_reg_class, p2p->op_channel);
  288. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  289. goto fail;
  290. }
  291. } else if (go && !(dev->flags & P2P_DEV_FORCE_FREQ) &&
  292. !p2p->cfg->cfg_op_channel) {
  293. p2p_dbg(p2p, "Try to reselect channel selection with peer information received; previously selected op_class %u channel %u",
  294. p2p->op_reg_class, p2p->op_channel);
  295. p2p_reselect_channel(p2p, &intersection);
  296. }
  297. op_freq = p2p_channel_to_freq(p2p->op_reg_class,
  298. p2p->op_channel);
  299. if (op_freq < 0) {
  300. p2p_dbg(p2p, "Unknown operational channel (country=%c%c reg_class=%u channel=%u)",
  301. p2p->cfg->country[0], p2p->cfg->country[1],
  302. p2p->op_reg_class, p2p->op_channel);
  303. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  304. goto fail;
  305. }
  306. p2p_dbg(p2p, "Selected operating channel - %d MHz", op_freq);
  307. if (status == P2P_SC_SUCCESS) {
  308. reg_class = p2p->op_reg_class;
  309. channel = p2p->op_channel;
  310. channels = &intersection;
  311. }
  312. }
  313. fail:
  314. if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
  315. bssid = group_bssid;
  316. else
  317. bssid = NULL;
  318. resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
  319. bssid, reg_class, channel, channels);
  320. if (resp == NULL)
  321. goto out;
  322. if (rx_freq > 0)
  323. freq = rx_freq;
  324. else
  325. freq = p2p_channel_to_freq(p2p->cfg->reg_class,
  326. p2p->cfg->channel);
  327. if (freq < 0) {
  328. p2p_dbg(p2p, "Unknown regulatory class/channel");
  329. goto out;
  330. }
  331. /*
  332. * Store copy of invitation data to be used when processing TX status
  333. * callback for the Acton frame.
  334. */
  335. os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
  336. if (msg.group_bssid) {
  337. os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
  338. p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
  339. } else
  340. p2p->inv_group_bssid_ptr = NULL;
  341. if (msg.group_id) {
  342. if (msg.group_id_len - ETH_ALEN <= 32) {
  343. os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
  344. msg.group_id_len - ETH_ALEN);
  345. p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
  346. }
  347. os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
  348. } else {
  349. p2p->inv_ssid_len = 0;
  350. os_memset(p2p->inv_go_dev_addr, 0, ETH_ALEN);
  351. }
  352. p2p->inv_status = status;
  353. p2p->inv_op_freq = op_freq;
  354. p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
  355. if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
  356. p2p->cfg->dev_addr,
  357. wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
  358. p2p_dbg(p2p, "Failed to send Action frame");
  359. }
  360. out:
  361. wpabuf_free(resp);
  362. p2p_parse_free(&msg);
  363. }
  364. void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
  365. const u8 *data, size_t len)
  366. {
  367. struct p2p_device *dev;
  368. struct p2p_message msg;
  369. struct p2p_channels intersection, *channels = NULL;
  370. p2p_dbg(p2p, "Received Invitation Response from " MACSTR,
  371. MAC2STR(sa));
  372. dev = p2p_get_device(p2p, sa);
  373. if (dev == NULL) {
  374. p2p_dbg(p2p, "Ignore Invitation Response from unknown peer "
  375. MACSTR, MAC2STR(sa));
  376. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  377. return;
  378. }
  379. if (dev != p2p->invite_peer) {
  380. p2p_dbg(p2p, "Ignore unexpected Invitation Response from peer "
  381. MACSTR, MAC2STR(sa));
  382. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  383. return;
  384. }
  385. if (p2p_parse(data, len, &msg)) {
  386. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  387. return;
  388. }
  389. if (!msg.status) {
  390. p2p_dbg(p2p, "Mandatory Status attribute missing in Invitation Response from "
  391. MACSTR, MAC2STR(sa));
  392. p2p_parse_free(&msg);
  393. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  394. return;
  395. }
  396. /*
  397. * We should not really receive a replayed response twice since
  398. * duplicate frames are supposed to be dropped. However, not all drivers
  399. * do that for pre-association frames. We did not use to verify dialog
  400. * token matches for invitation response frames, but that check can be
  401. * safely used to drop a replayed response to the previous Invitation
  402. * Request in case the suggested operating channel was changed. This
  403. * allows a duplicated reject frame to be dropped with the assumption
  404. * that the real response follows after it.
  405. */
  406. if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS &&
  407. p2p->retry_invite_req_sent &&
  408. msg.dialog_token != dev->dialog_token) {
  409. p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
  410. msg.dialog_token, dev->dialog_token);
  411. p2p_parse_free(&msg);
  412. return;
  413. }
  414. if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS &&
  415. p2p->retry_invite_req &&
  416. p2p_channel_random_social(&p2p->cfg->channels, &p2p->op_reg_class,
  417. &p2p->op_channel) == 0) {
  418. p2p->retry_invite_req = 0;
  419. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  420. p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
  421. p2p_set_state(p2p, P2P_INVITE);
  422. p2p_dbg(p2p, "Resend Invitation Request setting op_class %u channel %u as operating channel",
  423. p2p->op_reg_class, p2p->op_channel);
  424. p2p->retry_invite_req_sent = 1;
  425. p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
  426. p2p->invite_dev_pw_id);
  427. p2p_parse_free(&msg);
  428. return;
  429. }
  430. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  431. p2p->retry_invite_req = 0;
  432. if (!msg.channel_list && *msg.status == P2P_SC_SUCCESS) {
  433. p2p_dbg(p2p, "Mandatory Channel List attribute missing in Invitation Response from "
  434. MACSTR, MAC2STR(sa));
  435. #ifdef CONFIG_P2P_STRICT
  436. p2p_parse_free(&msg);
  437. return;
  438. #endif /* CONFIG_P2P_STRICT */
  439. /* Try to survive without peer channel list */
  440. channels = &p2p->channels;
  441. } else if (!msg.channel_list) {
  442. /* Non-success cases are not required to include Channel List */
  443. channels = &p2p->channels;
  444. } else if (p2p_peer_channels_check(p2p, &p2p->channels, dev,
  445. msg.channel_list,
  446. msg.channel_list_len) < 0) {
  447. p2p_dbg(p2p, "No common channels found");
  448. p2p_parse_free(&msg);
  449. return;
  450. } else {
  451. p2p_channels_intersect(&p2p->channels, &dev->channels,
  452. &intersection);
  453. channels = &intersection;
  454. }
  455. if (p2p->cfg->invitation_result) {
  456. int peer_oper_freq = 0;
  457. int freq = p2p_channel_to_freq(p2p->op_reg_class,
  458. p2p->op_channel);
  459. if (freq < 0)
  460. freq = 0;
  461. if (msg.operating_channel) {
  462. peer_oper_freq = p2p_channel_to_freq(
  463. msg.operating_channel[3],
  464. msg.operating_channel[4]);
  465. if (peer_oper_freq < 0)
  466. peer_oper_freq = 0;
  467. }
  468. p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
  469. msg.group_bssid, channels, sa,
  470. freq, peer_oper_freq);
  471. }
  472. p2p_parse_free(&msg);
  473. p2p_clear_timeout(p2p);
  474. p2p_set_state(p2p, P2P_IDLE);
  475. p2p->invite_peer = NULL;
  476. }
  477. int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
  478. const u8 *go_dev_addr, int dev_pw_id)
  479. {
  480. struct wpabuf *req;
  481. int freq;
  482. freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
  483. if (freq <= 0)
  484. freq = dev->oob_go_neg_freq;
  485. if (freq <= 0) {
  486. p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
  487. MACSTR " to send Invitation Request",
  488. MAC2STR(dev->info.p2p_device_addr));
  489. return -1;
  490. }
  491. req = p2p_build_invitation_req(p2p, dev, go_dev_addr, dev_pw_id);
  492. if (req == NULL)
  493. return -1;
  494. if (p2p->state != P2P_IDLE)
  495. p2p_stop_listen_for_freq(p2p, freq);
  496. p2p_dbg(p2p, "Sending Invitation Request");
  497. p2p_set_state(p2p, P2P_INVITE);
  498. p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
  499. p2p->invite_peer = dev;
  500. dev->invitation_reqs++;
  501. if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
  502. p2p->cfg->dev_addr, dev->info.p2p_device_addr,
  503. wpabuf_head(req), wpabuf_len(req), 500) < 0) {
  504. p2p_dbg(p2p, "Failed to send Action frame");
  505. /* Use P2P find to recover and retry */
  506. p2p_set_timeout(p2p, 0, 0);
  507. } else {
  508. dev->flags |= P2P_DEV_WAIT_INV_REQ_ACK;
  509. }
  510. wpabuf_free(req);
  511. return 0;
  512. }
  513. void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
  514. {
  515. p2p_dbg(p2p, "Invitation Request TX callback: success=%d", success);
  516. if (p2p->invite_peer == NULL) {
  517. p2p_dbg(p2p, "No pending Invite");
  518. return;
  519. }
  520. if (success)
  521. p2p->invite_peer->flags &= ~P2P_DEV_WAIT_INV_REQ_ACK;
  522. /*
  523. * Use P2P find, if needed, to find the other device from its listen
  524. * channel.
  525. */
  526. p2p_set_state(p2p, P2P_INVITE);
  527. p2p_set_timeout(p2p, 0, success ? 500000 : 100000);
  528. }
  529. void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
  530. {
  531. p2p_dbg(p2p, "Invitation Response TX callback: success=%d", success);
  532. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  533. if (!success)
  534. p2p_dbg(p2p, "Assume Invitation Response was actually received by the peer even though Ack was not reported");
  535. if (p2p->cfg->invitation_received) {
  536. p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
  537. p2p->inv_sa,
  538. p2p->inv_group_bssid_ptr,
  539. p2p->inv_ssid, p2p->inv_ssid_len,
  540. p2p->inv_go_dev_addr,
  541. p2p->inv_status,
  542. p2p->inv_op_freq);
  543. }
  544. }
  545. int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
  546. const u8 *bssid, const u8 *ssid, size_t ssid_len,
  547. unsigned int force_freq, const u8 *go_dev_addr,
  548. int persistent_group, unsigned int pref_freq, int dev_pw_id)
  549. {
  550. struct p2p_device *dev;
  551. p2p_dbg(p2p, "Request to invite peer " MACSTR " role=%d persistent=%d "
  552. "force_freq=%u",
  553. MAC2STR(peer), role, persistent_group, force_freq);
  554. if (bssid)
  555. p2p_dbg(p2p, "Invitation for BSSID " MACSTR, MAC2STR(bssid));
  556. if (go_dev_addr) {
  557. p2p_dbg(p2p, "Invitation for GO Device Address " MACSTR,
  558. MAC2STR(go_dev_addr));
  559. os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
  560. p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
  561. } else
  562. p2p->invite_go_dev_addr = NULL;
  563. wpa_hexdump_ascii(MSG_DEBUG, "Invitation for SSID",
  564. ssid, ssid_len);
  565. if (dev_pw_id >= 0) {
  566. p2p_dbg(p2p, "Invitation to use Device Password ID %d",
  567. dev_pw_id);
  568. }
  569. p2p->invite_dev_pw_id = dev_pw_id;
  570. p2p->retry_invite_req = role == P2P_INVITE_ROLE_GO &&
  571. persistent_group && !force_freq;
  572. p2p->retry_invite_req_sent = 0;
  573. dev = p2p_get_device(p2p, peer);
  574. if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0 &&
  575. dev->oob_go_neg_freq <= 0)) {
  576. p2p_dbg(p2p, "Cannot invite unknown P2P Device " MACSTR,
  577. MAC2STR(peer));
  578. return -1;
  579. }
  580. if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
  581. role != P2P_INVITE_ROLE_CLIENT) < 0)
  582. return -1;
  583. if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq &&
  584. !pref_freq)
  585. dev->flags |= P2P_DEV_NO_PREF_CHAN;
  586. else
  587. dev->flags &= ~P2P_DEV_NO_PREF_CHAN;
  588. if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
  589. if (!(dev->info.dev_capab &
  590. P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
  591. p2p_dbg(p2p, "Cannot invite a P2P Device " MACSTR
  592. " that is in a group and is not discoverable",
  593. MAC2STR(peer));
  594. }
  595. /* TODO: use device discoverability request through GO */
  596. }
  597. dev->invitation_reqs = 0;
  598. if (p2p->state != P2P_IDLE)
  599. p2p_stop_find(p2p);
  600. p2p->inv_role = role;
  601. p2p->inv_bssid_set = bssid != NULL;
  602. if (bssid)
  603. os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
  604. os_memcpy(p2p->inv_ssid, ssid, ssid_len);
  605. p2p->inv_ssid_len = ssid_len;
  606. p2p->inv_persistent = persistent_group;
  607. return p2p_invite_send(p2p, dev, go_dev_addr, dev_pw_id);
  608. }