p2p_invitation.c 21 KB

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