p2p_group.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * Wi-Fi Direct - P2P group operations
  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 "common/ieee802_11_common.h"
  18. #include "wps/wps_defs.h"
  19. #include "wps/wps_i.h"
  20. #include "p2p_i.h"
  21. #include "p2p.h"
  22. struct p2p_group_member {
  23. struct p2p_group_member *next;
  24. u8 addr[ETH_ALEN]; /* P2P Interface Address */
  25. u8 dev_addr[ETH_ALEN]; /* P2P Device Address */
  26. struct wpabuf *p2p_ie;
  27. struct wpabuf *client_info;
  28. u8 dev_capab;
  29. };
  30. /**
  31. * struct p2p_group - Internal P2P module per-group data
  32. */
  33. struct p2p_group {
  34. struct p2p_data *p2p;
  35. struct p2p_group_config *cfg;
  36. struct p2p_group_member *members;
  37. unsigned int num_members;
  38. int group_formation;
  39. int beacon_update;
  40. struct wpabuf *noa;
  41. };
  42. static void p2p_group_update_ies(struct p2p_group *group);
  43. struct p2p_group * p2p_group_init(struct p2p_data *p2p,
  44. struct p2p_group_config *config)
  45. {
  46. struct p2p_group *group, **groups;
  47. group = os_zalloc(sizeof(*group));
  48. if (group == NULL)
  49. return NULL;
  50. groups = os_realloc(p2p->groups, (p2p->num_groups + 1) *
  51. sizeof(struct p2p_group *));
  52. if (groups == NULL) {
  53. os_free(group);
  54. return NULL;
  55. }
  56. groups[p2p->num_groups++] = group;
  57. p2p->groups = groups;
  58. group->p2p = p2p;
  59. group->cfg = config;
  60. group->group_formation = 1;
  61. group->beacon_update = 1;
  62. p2p_group_update_ies(group);
  63. group->cfg->idle_update(group->cfg->cb_ctx, 1);
  64. return group;
  65. }
  66. static void p2p_group_free_member(struct p2p_group_member *m)
  67. {
  68. wpabuf_free(m->p2p_ie);
  69. wpabuf_free(m->client_info);
  70. os_free(m);
  71. }
  72. static void p2p_group_free_members(struct p2p_group *group)
  73. {
  74. struct p2p_group_member *m, *prev;
  75. m = group->members;
  76. group->members = NULL;
  77. group->num_members = 0;
  78. while (m) {
  79. prev = m;
  80. m = m->next;
  81. p2p_group_free_member(prev);
  82. }
  83. }
  84. void p2p_group_deinit(struct p2p_group *group)
  85. {
  86. size_t g;
  87. struct p2p_data *p2p;
  88. if (group == NULL)
  89. return;
  90. p2p = group->p2p;
  91. for (g = 0; g < p2p->num_groups; g++) {
  92. if (p2p->groups[g] == group) {
  93. while (g + 1 < p2p->num_groups) {
  94. p2p->groups[g] = p2p->groups[g + 1];
  95. g++;
  96. }
  97. p2p->num_groups--;
  98. break;
  99. }
  100. }
  101. p2p_group_free_members(group);
  102. os_free(group->cfg);
  103. wpabuf_free(group->noa);
  104. os_free(group);
  105. }
  106. static void p2p_client_info(struct wpabuf *ie, struct p2p_group_member *m)
  107. {
  108. if (m->client_info == NULL)
  109. return;
  110. if (wpabuf_tailroom(ie) < wpabuf_len(m->client_info) + 1)
  111. return;
  112. wpabuf_put_buf(ie, m->client_info);
  113. }
  114. static void p2p_group_add_common_ies(struct p2p_group *group,
  115. struct wpabuf *ie)
  116. {
  117. u8 dev_capab = 0, group_capab = 0;
  118. /* P2P Capability */
  119. dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
  120. dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
  121. group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
  122. if (group->cfg->persistent_group)
  123. group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  124. if (group->p2p->cfg->p2p_intra_bss)
  125. group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
  126. if (group->group_formation)
  127. group_capab |= P2P_GROUP_CAPAB_GROUP_FORMATION;
  128. if (group->p2p->cross_connect)
  129. group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
  130. if (group->num_members >= group->cfg->max_clients)
  131. group_capab |= P2P_GROUP_CAPAB_GROUP_LIMIT;
  132. p2p_buf_add_capability(ie, dev_capab, group_capab);
  133. }
  134. static void p2p_group_add_noa(struct wpabuf *ie, struct wpabuf *noa)
  135. {
  136. if (noa == NULL)
  137. return;
  138. /* Notice of Absence */
  139. wpabuf_put_u8(ie, P2P_ATTR_NOTICE_OF_ABSENCE);
  140. wpabuf_put_le16(ie, wpabuf_len(noa));
  141. wpabuf_put_buf(ie, noa);
  142. }
  143. static struct wpabuf * p2p_group_build_beacon_ie(struct p2p_group *group)
  144. {
  145. struct wpabuf *ie;
  146. u8 *len;
  147. ie = wpabuf_alloc(257);
  148. if (ie == NULL)
  149. return NULL;
  150. len = p2p_buf_add_ie_hdr(ie);
  151. p2p_group_add_common_ies(group, ie);
  152. p2p_buf_add_device_id(ie, group->p2p->cfg->dev_addr);
  153. p2p_group_add_noa(ie, group->noa);
  154. p2p_buf_update_ie_hdr(ie, len);
  155. return ie;
  156. }
  157. static struct wpabuf * p2p_group_build_probe_resp_ie(struct p2p_group *group)
  158. {
  159. u8 *group_info;
  160. struct wpabuf *ie;
  161. struct p2p_group_member *m;
  162. u8 *len;
  163. ie = wpabuf_alloc(257);
  164. if (ie == NULL)
  165. return NULL;
  166. len = p2p_buf_add_ie_hdr(ie);
  167. p2p_group_add_common_ies(group, ie);
  168. p2p_group_add_noa(ie, group->noa);
  169. /* P2P Device Info */
  170. p2p_buf_add_device_info(ie, group->p2p, NULL);
  171. /* P2P Group Info */
  172. group_info = wpabuf_put(ie, 0);
  173. wpabuf_put_u8(ie, P2P_ATTR_GROUP_INFO);
  174. wpabuf_put_le16(ie, 0); /* Length to be filled */
  175. for (m = group->members; m; m = m->next)
  176. p2p_client_info(ie, m);
  177. WPA_PUT_LE16(group_info + 1,
  178. (u8 *) wpabuf_put(ie, 0) - group_info - 3);
  179. p2p_buf_update_ie_hdr(ie, len);
  180. return ie;
  181. }
  182. static void p2p_group_update_ies(struct p2p_group *group)
  183. {
  184. struct wpabuf *beacon_ie;
  185. struct wpabuf *probe_resp_ie;
  186. probe_resp_ie = p2p_group_build_probe_resp_ie(group);
  187. if (probe_resp_ie == NULL)
  188. return;
  189. wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Probe Response P2P IE",
  190. probe_resp_ie);
  191. if (group->beacon_update) {
  192. beacon_ie = p2p_group_build_beacon_ie(group);
  193. if (beacon_ie)
  194. group->beacon_update = 0;
  195. wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Beacon P2P IE",
  196. beacon_ie);
  197. } else
  198. beacon_ie = NULL;
  199. group->cfg->ie_update(group->cfg->cb_ctx, beacon_ie, probe_resp_ie);
  200. }
  201. /**
  202. * p2p_build_client_info - Build P2P Client Info Descriptor
  203. * @addr: MAC address of the peer device
  204. * @p2p_ie: P2P IE from (Re)Association Request
  205. * @dev_capab: Buffer for returning Device Capability
  206. * @dev_addr: Buffer for returning P2P Device Address
  207. * Returns: P2P Client Info Descriptor or %NULL on failure
  208. *
  209. * This function builds P2P Client Info Descriptor based on the information
  210. * available from (Re)Association Request frame. Group owner can use this to
  211. * build the P2P Group Info attribute for Probe Response frames.
  212. */
  213. static struct wpabuf * p2p_build_client_info(const u8 *addr,
  214. struct wpabuf *p2p_ie,
  215. u8 *dev_capab, u8 *dev_addr)
  216. {
  217. const u8 *spos;
  218. struct p2p_message msg;
  219. u8 *len_pos;
  220. struct wpabuf *buf;
  221. if (p2p_ie == NULL)
  222. return NULL;
  223. os_memset(&msg, 0, sizeof(msg));
  224. if (p2p_parse_p2p_ie(p2p_ie, &msg) ||
  225. msg.capability == NULL || msg.p2p_device_info == NULL)
  226. return NULL;
  227. buf = wpabuf_alloc(ETH_ALEN + 1 + 1 + msg.p2p_device_info_len);
  228. if (buf == NULL)
  229. return NULL;
  230. *dev_capab = msg.capability[0];
  231. os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
  232. spos = msg.p2p_device_info; /* P2P Device address */
  233. /* P2P Client Info Descriptor */
  234. /* Length to be set */
  235. len_pos = wpabuf_put(buf, 1);
  236. /* P2P Device address */
  237. wpabuf_put_data(buf, spos, ETH_ALEN);
  238. /* P2P Interface address */
  239. wpabuf_put_data(buf, addr, ETH_ALEN);
  240. /* Device Capability Bitmap */
  241. wpabuf_put_u8(buf, msg.capability[0]);
  242. /*
  243. * Config Methods, Primary Device Type, Number of Secondary Device
  244. * Types, Secondary Device Type List, Device Name copied from
  245. * Device Info
  246. */
  247. wpabuf_put_data(buf, spos + ETH_ALEN,
  248. msg.p2p_device_info_len - ETH_ALEN);
  249. *len_pos = wpabuf_len(buf) - 1;
  250. return buf;
  251. }
  252. int p2p_group_notif_assoc(struct p2p_group *group, const u8 *addr,
  253. const u8 *ie, size_t len)
  254. {
  255. struct p2p_group_member *m;
  256. if (group == NULL)
  257. return -1;
  258. m = os_zalloc(sizeof(*m));
  259. if (m == NULL)
  260. return -1;
  261. os_memcpy(m->addr, addr, ETH_ALEN);
  262. m->p2p_ie = ieee802_11_vendor_ie_concat(ie, len, P2P_IE_VENDOR_TYPE);
  263. if (m->p2p_ie) {
  264. m->client_info = p2p_build_client_info(addr, m->p2p_ie,
  265. &m->dev_capab,
  266. m->dev_addr);
  267. }
  268. m->next = group->members;
  269. group->members = m;
  270. group->num_members++;
  271. wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Add client " MACSTR
  272. " to group (p2p=%d client_info=%d); num_members=%u/%u",
  273. MAC2STR(addr), m->p2p_ie ? 1 : 0, m->client_info ? 1 : 0,
  274. group->num_members, group->cfg->max_clients);
  275. if (group->num_members == group->cfg->max_clients)
  276. group->beacon_update = 1;
  277. p2p_group_update_ies(group);
  278. if (group->num_members == 1)
  279. group->cfg->idle_update(group->cfg->cb_ctx, 0);
  280. return 0;
  281. }
  282. struct wpabuf * p2p_group_assoc_resp_ie(struct p2p_group *group, u8 status)
  283. {
  284. struct wpabuf *resp;
  285. u8 *rlen;
  286. /*
  287. * (Re)Association Response - P2P IE
  288. * Status attribute (shall be present when association request is
  289. * denied)
  290. * Extended Listen Timing (may be present)
  291. */
  292. resp = wpabuf_alloc(20);
  293. if (resp == NULL)
  294. return NULL;
  295. rlen = p2p_buf_add_ie_hdr(resp);
  296. if (status != P2P_SC_SUCCESS)
  297. p2p_buf_add_status(resp, status);
  298. p2p_buf_update_ie_hdr(resp, rlen);
  299. return resp;
  300. }
  301. void p2p_group_notif_disassoc(struct p2p_group *group, const u8 *addr)
  302. {
  303. struct p2p_group_member *m, *prev;
  304. if (group == NULL)
  305. return;
  306. m = group->members;
  307. prev = NULL;
  308. while (m) {
  309. if (os_memcmp(m->addr, addr, ETH_ALEN) == 0)
  310. break;
  311. prev = m;
  312. m = m->next;
  313. }
  314. if (m) {
  315. if (prev)
  316. prev->next = m->next;
  317. else
  318. group->members = m->next;
  319. p2p_group_free_member(m);
  320. group->num_members--;
  321. wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Remove "
  322. "client " MACSTR " from group; num_members=%u/%u",
  323. MAC2STR(addr), group->num_members,
  324. group->cfg->max_clients);
  325. if (group->num_members == group->cfg->max_clients - 1)
  326. group->beacon_update = 1;
  327. p2p_group_update_ies(group);
  328. if (group->num_members == 0)
  329. group->cfg->idle_update(group->cfg->cb_ctx, 1);
  330. }
  331. }
  332. /**
  333. * p2p_match_dev_type_member - Match client device type with requested type
  334. * @m: Group member
  335. * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
  336. * Returns: 1 on match, 0 on mismatch
  337. *
  338. * This function can be used to match the Requested Device Type attribute in
  339. * WPS IE with the device types of a group member for deciding whether a GO
  340. * should reply to a Probe Request frame.
  341. */
  342. static int p2p_match_dev_type_member(struct p2p_group_member *m,
  343. struct wpabuf *wps)
  344. {
  345. const u8 *pos, *end;
  346. struct wps_parse_attr attr;
  347. u8 num_sec;
  348. if (m->client_info == NULL || wps == NULL)
  349. return 0;
  350. pos = wpabuf_head(m->client_info);
  351. end = pos + wpabuf_len(m->client_info);
  352. pos += 1 + 2 * ETH_ALEN + 1 + 2;
  353. if (end - pos < WPS_DEV_TYPE_LEN + 1)
  354. return 0;
  355. if (wps_parse_msg(wps, &attr))
  356. return 1; /* assume no Requested Device Type attributes */
  357. if (attr.num_req_dev_type == 0)
  358. return 1; /* no Requested Device Type attributes -> match */
  359. if (dev_type_list_match(pos, attr.req_dev_type, attr.num_req_dev_type))
  360. return 1; /* Match with client Primary Device Type */
  361. pos += WPS_DEV_TYPE_LEN;
  362. num_sec = *pos++;
  363. if (end - pos < num_sec * WPS_DEV_TYPE_LEN)
  364. return 0;
  365. while (num_sec > 0) {
  366. num_sec--;
  367. if (dev_type_list_match(pos, attr.req_dev_type,
  368. attr.num_req_dev_type))
  369. return 1; /* Match with client Secondary Device Type */
  370. pos += WPS_DEV_TYPE_LEN;
  371. }
  372. /* No matching device type found */
  373. return 0;
  374. }
  375. int p2p_group_match_dev_type(struct p2p_group *group, struct wpabuf *wps)
  376. {
  377. struct p2p_group_member *m;
  378. if (p2p_match_dev_type(group->p2p, wps))
  379. return 1; /* Match with own device type */
  380. for (m = group->members; m; m = m->next) {
  381. if (p2p_match_dev_type_member(m, wps))
  382. return 1; /* Match with group client device type */
  383. }
  384. /* No match with Requested Device Type */
  385. return 0;
  386. }
  387. void p2p_group_notif_formation_done(struct p2p_group *group)
  388. {
  389. if (group == NULL)
  390. return;
  391. group->group_formation = 0;
  392. group->beacon_update = 1;
  393. p2p_group_update_ies(group);
  394. }
  395. int p2p_group_notif_noa(struct p2p_group *group, const u8 *noa,
  396. size_t noa_len)
  397. {
  398. if (noa == NULL) {
  399. wpabuf_free(group->noa);
  400. group->noa = NULL;
  401. } else {
  402. if (group->noa) {
  403. if (wpabuf_size(group->noa) >= noa_len) {
  404. group->noa->size = 0;
  405. wpabuf_put_data(group->noa, noa, noa_len);
  406. } else {
  407. wpabuf_free(group->noa);
  408. group->noa = NULL;
  409. }
  410. }
  411. if (!group->noa) {
  412. group->noa = wpabuf_alloc_copy(noa, noa_len);
  413. if (group->noa == NULL)
  414. return -1;
  415. }
  416. }
  417. group->beacon_update = 1;
  418. p2p_group_update_ies(group);
  419. return 0;
  420. }
  421. static struct p2p_group_member * p2p_group_get_client(struct p2p_group *group,
  422. const u8 *dev_id)
  423. {
  424. struct p2p_group_member *m;
  425. for (m = group->members; m; m = m->next) {
  426. if (os_memcmp(dev_id, m->dev_addr, ETH_ALEN) == 0)
  427. return m;
  428. }
  429. return NULL;
  430. }
  431. static struct p2p_group_member * p2p_group_get_client_iface(
  432. struct p2p_group *group, const u8 *interface_addr)
  433. {
  434. struct p2p_group_member *m;
  435. for (m = group->members; m; m = m->next) {
  436. if (os_memcmp(interface_addr, m->addr, ETH_ALEN) == 0)
  437. return m;
  438. }
  439. return NULL;
  440. }
  441. static struct wpabuf * p2p_build_go_disc_req(void)
  442. {
  443. struct wpabuf *buf;
  444. buf = wpabuf_alloc(100);
  445. if (buf == NULL)
  446. return NULL;
  447. p2p_buf_add_action_hdr(buf, P2P_GO_DISC_REQ, 0);
  448. return buf;
  449. }
  450. int p2p_group_go_discover(struct p2p_group *group, const u8 *dev_id,
  451. const u8 *searching_dev, int rx_freq)
  452. {
  453. struct p2p_group_member *m;
  454. struct wpabuf *req;
  455. struct p2p_data *p2p = group->p2p;
  456. int freq;
  457. m = p2p_group_get_client(group, dev_id);
  458. if (m == NULL || m->client_info == NULL) {
  459. wpa_printf(MSG_DEBUG, "P2P: Requested client was not in this "
  460. "group " MACSTR,
  461. MAC2STR(group->cfg->interface_addr));
  462. return -1;
  463. }
  464. if (!(m->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
  465. wpa_printf(MSG_DEBUG, "P2P: Requested client does not support "
  466. "client discoverability");
  467. return -1;
  468. }
  469. wpa_printf(MSG_DEBUG, "P2P: Schedule GO Discoverability Request to be "
  470. "sent to " MACSTR, MAC2STR(dev_id));
  471. req = p2p_build_go_disc_req();
  472. if (req == NULL)
  473. return -1;
  474. /* TODO: Should really use group operating frequency here */
  475. freq = rx_freq;
  476. p2p->pending_action_state = P2P_PENDING_GO_DISC_REQ;
  477. if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, m->addr,
  478. group->cfg->interface_addr,
  479. group->cfg->interface_addr,
  480. wpabuf_head(req), wpabuf_len(req), 200) < 0)
  481. {
  482. wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
  483. "P2P: Failed to send Action frame");
  484. }
  485. wpabuf_free(req);
  486. return 0;
  487. }
  488. const u8 * p2p_group_get_interface_addr(struct p2p_group *group)
  489. {
  490. return group->cfg->interface_addr;
  491. }
  492. u8 p2p_group_presence_req(struct p2p_group *group,
  493. const u8 *client_interface_addr,
  494. const u8 *noa, size_t noa_len)
  495. {
  496. struct p2p_group_member *m;
  497. u8 curr_noa[50];
  498. int curr_noa_len;
  499. m = p2p_group_get_client_iface(group, client_interface_addr);
  500. if (m == NULL || m->client_info == NULL) {
  501. wpa_printf(MSG_DEBUG, "P2P: Client was not in this group");
  502. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  503. }
  504. wpa_hexdump(MSG_DEBUG, "P2P: Presence Request NoA", noa, noa_len);
  505. if (group->p2p->cfg->get_noa)
  506. curr_noa_len = group->p2p->cfg->get_noa(
  507. group->p2p->cfg->cb_ctx, group->cfg->interface_addr,
  508. curr_noa, sizeof(curr_noa));
  509. else
  510. curr_noa_len = -1;
  511. if (curr_noa_len < 0)
  512. wpa_printf(MSG_DEBUG, "P2P: Failed to fetch current NoA");
  513. else if (curr_noa_len == 0)
  514. wpa_printf(MSG_DEBUG, "P2P: No NoA being advertized");
  515. else
  516. wpa_hexdump(MSG_DEBUG, "P2P: Current NoA", curr_noa,
  517. curr_noa_len);
  518. /* TODO: properly process request and store copy */
  519. if (curr_noa_len > 0)
  520. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  521. return P2P_SC_SUCCESS;
  522. }