p2p_invitation.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Wi-Fi Direct - P2P Invitation procedure
  3. * Copyright (c) 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 "p2p_i.h"
  18. #include "p2p.h"
  19. static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
  20. struct p2p_device *peer,
  21. const u8 *go_dev_addr)
  22. {
  23. struct wpabuf *buf;
  24. u8 *len;
  25. const u8 *dev_addr;
  26. buf = wpabuf_alloc(1000);
  27. if (buf == NULL)
  28. return NULL;
  29. peer->dialog_token++;
  30. if (peer->dialog_token == 0)
  31. peer->dialog_token = 1;
  32. p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
  33. peer->dialog_token);
  34. len = p2p_buf_add_ie_hdr(buf);
  35. if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
  36. p2p_buf_add_config_timeout(buf, 0, 0);
  37. else
  38. p2p_buf_add_config_timeout(buf, 100, 20);
  39. p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
  40. P2P_INVITATION_FLAGS_TYPE : 0);
  41. p2p_buf_add_operating_channel(buf, p2p->cfg->country,
  42. p2p->op_reg_class, p2p->op_channel);
  43. if (p2p->inv_bssid_set)
  44. p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
  45. p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
  46. if (go_dev_addr)
  47. dev_addr = go_dev_addr;
  48. else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
  49. dev_addr = peer->p2p_device_addr;
  50. else
  51. dev_addr = p2p->cfg->dev_addr;
  52. p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
  53. p2p_buf_add_device_info(buf, p2p, peer);
  54. p2p_buf_update_ie_hdr(buf, len);
  55. return buf;
  56. }
  57. static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
  58. struct p2p_device *peer,
  59. u8 dialog_token, u8 status,
  60. const u8 *group_bssid,
  61. u8 reg_class, u8 channel,
  62. struct p2p_channels *channels)
  63. {
  64. struct wpabuf *buf;
  65. u8 *len;
  66. buf = wpabuf_alloc(1000);
  67. if (buf == NULL)
  68. return NULL;
  69. p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
  70. dialog_token);
  71. len = p2p_buf_add_ie_hdr(buf);
  72. p2p_buf_add_status(buf, status);
  73. p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
  74. if (reg_class && channel)
  75. p2p_buf_add_operating_channel(buf, p2p->cfg->country,
  76. reg_class, channel);
  77. if (group_bssid)
  78. p2p_buf_add_group_bssid(buf, group_bssid);
  79. if (channels)
  80. p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
  81. p2p_buf_update_ie_hdr(buf, len);
  82. return buf;
  83. }
  84. void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
  85. const u8 *data, size_t len, int rx_freq)
  86. {
  87. struct p2p_device *dev;
  88. struct p2p_message msg;
  89. struct wpabuf *resp = NULL;
  90. u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  91. int freq;
  92. int go = 0;
  93. u8 group_bssid[ETH_ALEN], *bssid;
  94. int op_freq = 0;
  95. u8 reg_class = 0, channel = 0;
  96. struct p2p_channels intersection, *channels = NULL;
  97. int persistent;
  98. os_memset(group_bssid, 0, sizeof(group_bssid));
  99. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  100. "P2P: Received Invitation Request from " MACSTR " (freq=%d)",
  101. MAC2STR(sa), rx_freq);
  102. if (p2p_parse(data, len, &msg))
  103. return;
  104. dev = p2p_get_device(p2p, sa);
  105. if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
  106. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  107. "P2P: Invitation Request from unknown peer "
  108. MACSTR, MAC2STR(sa));
  109. if (p2p_add_device(p2p, sa, rx_freq, 0, data + 1, len - 1)) {
  110. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  111. "P2P: Invitation Request add device failed "
  112. MACSTR, MAC2STR(sa));
  113. status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  114. goto fail;
  115. }
  116. dev = p2p_get_device(p2p, sa);
  117. if (dev == NULL) {
  118. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  119. "P2P: Reject Invitation Request from unknown "
  120. "peer " MACSTR, MAC2STR(sa));
  121. status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  122. goto fail;
  123. }
  124. }
  125. if (!msg.group_id || !msg.channel_list) {
  126. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  127. "P2P: Mandatory attribute missing in Invitation "
  128. "Request from " MACSTR, MAC2STR(sa));
  129. status = P2P_SC_FAIL_INVALID_PARAMS;
  130. goto fail;
  131. }
  132. if (msg.invitation_flags)
  133. persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
  134. else {
  135. /* Invitation Flags is a mandatory attribute starting from P2P
  136. * spec 1.06. As a backwards compatibility mechanism, assume
  137. * the request was for a persistent group if the attribute is
  138. * missing.
  139. */
  140. wpa_printf(MSG_DEBUG, "P2P: Mandatory Invitation Flags "
  141. "attribute missing from Invitation Request");
  142. persistent = 1;
  143. }
  144. if (p2p_peer_channels_check(p2p, &p2p->cfg->channels, dev,
  145. msg.channel_list, msg.channel_list_len) <
  146. 0) {
  147. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  148. "P2P: No common channels found");
  149. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  150. goto fail;
  151. }
  152. if (p2p->cfg->invitation_process) {
  153. status = p2p->cfg->invitation_process(
  154. p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
  155. msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
  156. &go, group_bssid, &op_freq, persistent);
  157. }
  158. if (op_freq) {
  159. if (p2p_freq_to_channel(p2p->cfg->country, op_freq,
  160. &reg_class, &channel) < 0) {
  161. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  162. "P2P: Unknown forced freq %d MHz from "
  163. "invitation_process()", op_freq);
  164. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  165. goto fail;
  166. }
  167. p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
  168. &intersection);
  169. if (!p2p_channels_includes(&intersection, reg_class, channel))
  170. {
  171. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  172. "P2P: forced freq %d MHz not in the supported "
  173. "channels interaction", op_freq);
  174. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  175. goto fail;
  176. }
  177. if (status == P2P_SC_SUCCESS)
  178. channels = &intersection;
  179. } else {
  180. op_freq = p2p_channel_to_freq(p2p->cfg->country,
  181. p2p->cfg->op_reg_class,
  182. p2p->cfg->op_channel);
  183. if (op_freq < 0) {
  184. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  185. "P2P: Unknown operational channel "
  186. "(country=%c%c reg_class=%u channel=%u)",
  187. p2p->cfg->country[0], p2p->cfg->country[1],
  188. p2p->cfg->op_reg_class, p2p->cfg->op_channel);
  189. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  190. goto fail;
  191. }
  192. p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
  193. &intersection);
  194. if (status == P2P_SC_SUCCESS) {
  195. reg_class = p2p->cfg->op_reg_class;
  196. channel = p2p->cfg->op_channel;
  197. channels = &intersection;
  198. }
  199. }
  200. fail:
  201. if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
  202. bssid = group_bssid;
  203. else
  204. bssid = NULL;
  205. resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
  206. bssid, reg_class, channel, channels);
  207. if (resp == NULL)
  208. goto out;
  209. if (rx_freq > 0)
  210. freq = rx_freq;
  211. else
  212. freq = p2p_channel_to_freq(p2p->cfg->country,
  213. p2p->cfg->reg_class,
  214. p2p->cfg->channel);
  215. if (freq < 0) {
  216. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  217. "P2P: Unknown regulatory class/channel");
  218. goto out;
  219. }
  220. /*
  221. * Store copy of invitation data to be used when processing TX status
  222. * callback for the Acton frame.
  223. */
  224. os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
  225. if (msg.group_bssid) {
  226. os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
  227. p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
  228. } else
  229. p2p->inv_group_bssid_ptr = NULL;
  230. if (msg.group_id_len - ETH_ALEN <= 32) {
  231. os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
  232. msg.group_id_len - ETH_ALEN);
  233. p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
  234. }
  235. os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
  236. p2p->inv_status = status;
  237. p2p->inv_op_freq = op_freq;
  238. p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
  239. if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, sa,
  240. p2p->cfg->dev_addr, p2p->cfg->dev_addr,
  241. wpabuf_head(resp), wpabuf_len(resp), 200) <
  242. 0) {
  243. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  244. "P2P: Failed to send Action frame");
  245. }
  246. out:
  247. wpabuf_free(resp);
  248. p2p_parse_free(&msg);
  249. }
  250. void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
  251. const u8 *data, size_t len)
  252. {
  253. struct p2p_device *dev;
  254. struct p2p_message msg;
  255. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  256. "P2P: Received Invitation Response from " MACSTR,
  257. MAC2STR(sa));
  258. dev = p2p_get_device(p2p, sa);
  259. if (dev == NULL) {
  260. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  261. "P2P: Ignore Invitation Response from unknown peer "
  262. MACSTR, MAC2STR(sa));
  263. return;
  264. }
  265. if (dev != p2p->invite_peer) {
  266. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  267. "P2P: Ignore unexpected Invitation Response from peer "
  268. MACSTR, MAC2STR(sa));
  269. return;
  270. }
  271. if (p2p_parse(data, len, &msg))
  272. return;
  273. if (!msg.status) {
  274. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  275. "P2P: Mandatory Status attribute missing in "
  276. "Invitation Response from " MACSTR, MAC2STR(sa));
  277. p2p_parse_free(&msg);
  278. return;
  279. }
  280. if (p2p->cfg->invitation_result)
  281. p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
  282. msg.group_bssid);
  283. p2p_parse_free(&msg);
  284. p2p_clear_timeout(p2p);
  285. p2p_set_state(p2p, P2P_IDLE);
  286. p2p->invite_peer = NULL;
  287. }
  288. int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
  289. const u8 *go_dev_addr)
  290. {
  291. struct wpabuf *req;
  292. int freq;
  293. freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
  294. if (freq <= 0) {
  295. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  296. "P2P: No Listen/Operating frequency known for the "
  297. "peer " MACSTR " to send Invitation Request",
  298. MAC2STR(dev->p2p_device_addr));
  299. return -1;
  300. }
  301. req = p2p_build_invitation_req(p2p, dev, go_dev_addr);
  302. if (req == NULL)
  303. return -1;
  304. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  305. "P2P: Sending Invitation Request");
  306. p2p_set_state(p2p, P2P_INVITE);
  307. p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
  308. p2p->invite_peer = dev;
  309. dev->invitation_reqs++;
  310. if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq,
  311. dev->p2p_device_addr, p2p->cfg->dev_addr,
  312. dev->p2p_device_addr,
  313. wpabuf_head(req), wpabuf_len(req), 200) < 0)
  314. {
  315. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  316. "P2P: Failed to send Action frame");
  317. /* Use P2P find to recover and retry */
  318. p2p_set_timeout(p2p, 0, 0);
  319. }
  320. wpabuf_free(req);
  321. return 0;
  322. }
  323. void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
  324. {
  325. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  326. "P2P: Invitation Request TX callback: success=%d", success);
  327. if (p2p->invite_peer == NULL) {
  328. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  329. "P2P: No pending Invite");
  330. return;
  331. }
  332. /*
  333. * Use P2P find, if needed, to find the other device from its listen
  334. * channel.
  335. */
  336. p2p_set_state(p2p, P2P_INVITE);
  337. p2p_set_timeout(p2p, 0, 100000);
  338. }
  339. void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
  340. {
  341. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  342. "P2P: Invitation Response TX callback: success=%d", success);
  343. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  344. if (success && p2p->cfg->invitation_received) {
  345. p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
  346. p2p->inv_sa,
  347. p2p->inv_group_bssid,
  348. p2p->inv_ssid, p2p->inv_ssid_len,
  349. p2p->inv_go_dev_addr,
  350. p2p->inv_status,
  351. p2p->inv_op_freq);
  352. }
  353. }
  354. int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
  355. const u8 *bssid, const u8 *ssid, size_t ssid_len,
  356. unsigned int force_freq, const u8 *go_dev_addr,
  357. int persistent_group)
  358. {
  359. struct p2p_device *dev;
  360. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  361. "P2P: Request to invite peer " MACSTR " role=%d persistent=%d "
  362. "force_freq=%u",
  363. MAC2STR(peer), role, persistent_group, force_freq);
  364. if (bssid)
  365. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  366. "P2P: Invitation for BSSID " MACSTR, MAC2STR(bssid));
  367. if (go_dev_addr) {
  368. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  369. "P2P: Invitation for GO Device Address " MACSTR,
  370. MAC2STR(go_dev_addr));
  371. os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
  372. p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
  373. } else
  374. p2p->invite_go_dev_addr = NULL;
  375. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Invitation for SSID",
  376. ssid, ssid_len);
  377. dev = p2p_get_device(p2p, peer);
  378. if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0)) {
  379. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  380. "P2P: Cannot invite unknown P2P Device " MACSTR,
  381. MAC2STR(peer));
  382. return -1;
  383. }
  384. if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
  385. if (!(dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
  386. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  387. "P2P: Cannot invite a P2P Device " MACSTR
  388. " that is in a group and is not discoverable",
  389. MAC2STR(peer));
  390. }
  391. /* TODO: use device discoverability request through GO */
  392. }
  393. dev->invitation_reqs = 0;
  394. if (force_freq) {
  395. if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
  396. &p2p->op_reg_class, &p2p->op_channel) <
  397. 0) {
  398. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  399. "P2P: Unsupported frequency %u MHz",
  400. force_freq);
  401. return -1;
  402. }
  403. p2p->channels.reg_classes = 1;
  404. p2p->channels.reg_class[0].channels = 1;
  405. p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
  406. p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
  407. } else {
  408. p2p->op_reg_class = p2p->cfg->op_reg_class;
  409. p2p->op_channel = p2p->cfg->op_channel;
  410. os_memcpy(&p2p->channels, &p2p->cfg->channels,
  411. sizeof(struct p2p_channels));
  412. }
  413. if (p2p->state != P2P_IDLE)
  414. p2p_stop_find(p2p);
  415. p2p->inv_role = role;
  416. p2p->inv_bssid_set = bssid != NULL;
  417. if (bssid)
  418. os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
  419. os_memcpy(p2p->inv_ssid, ssid, ssid_len);
  420. p2p->inv_ssid_len = ssid_len;
  421. p2p->inv_persistent = persistent_group;
  422. return p2p_invite_send(p2p, dev, go_dev_addr);
  423. }