2 * Wi-Fi Direct - P2P group operations
3 * Copyright (c) 2009-2010, Atheros Communications
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "common/ieee802_11_defs.h"
19 #include "common/ieee802_11_common.h"
20 #include "wps/wps_defs.h"
21 #include "wps/wps_i.h"
26 struct p2p_group_member {
27 struct p2p_group_member *next;
28 u8 addr[ETH_ALEN]; /* P2P Interface Address */
29 u8 dev_addr[ETH_ALEN]; /* P2P Device Address */
30 struct wpabuf *p2p_ie;
31 struct wpabuf *client_info;
36 * struct p2p_group - Internal P2P module per-group data
40 struct p2p_group_config *cfg;
41 struct p2p_group_member *members;
48 static void p2p_group_update_ies(struct p2p_group *group);
51 struct p2p_group * p2p_group_init(struct p2p_data *p2p,
52 struct p2p_group_config *config)
54 struct p2p_group *group, **groups;
56 group = os_zalloc(sizeof(*group));
60 groups = os_realloc(p2p->groups, (p2p->num_groups + 1) *
61 sizeof(struct p2p_group *));
66 groups[p2p->num_groups++] = group;
71 group->group_formation = 1;
72 group->beacon_update = 1;
73 p2p_group_update_ies(group);
79 static void p2p_group_free_member(struct p2p_group_member *m)
81 wpabuf_free(m->p2p_ie);
82 wpabuf_free(m->client_info);
87 static void p2p_group_free_members(struct p2p_group *group)
89 struct p2p_group_member *m, *prev;
91 group->members = NULL;
95 p2p_group_free_member(prev);
100 void p2p_group_deinit(struct p2p_group *group)
103 struct p2p_data *p2p = group->p2p;
108 for (g = 0; g < p2p->num_groups; g++) {
109 if (p2p->groups[g] == group) {
110 while (g + 1 < p2p->num_groups) {
111 p2p->groups[g] = p2p->groups[g + 1];
119 p2p_group_free_members(group);
121 wpabuf_free(group->noa);
126 static void p2p_client_info(struct wpabuf *ie, struct p2p_group_member *m)
128 if (wpabuf_tailroom(ie) < wpabuf_len(m->client_info) + 1)
130 wpabuf_put_buf(ie, m->client_info);
134 static void p2p_group_add_common_ies(struct p2p_group *group,
137 u8 dev_capab = 0, group_capab = 0;
140 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
141 dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
142 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
143 if (group->cfg->persistent_group)
144 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
145 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
146 if (group->group_formation)
147 group_capab |= P2P_GROUP_CAPAB_GROUP_FORMATION;
148 p2p_buf_add_capability(ie, dev_capab, group_capab);
152 static void p2p_group_add_noa(struct wpabuf *ie, struct wpabuf *noa)
156 /* Notice of Absence */
157 wpabuf_put_u8(ie, P2P_ATTR_NOTICE_OF_ABSENCE);
158 wpabuf_put_le16(ie, wpabuf_len(noa));
159 wpabuf_put_buf(ie, noa);
163 static struct wpabuf * p2p_group_build_beacon_ie(struct p2p_group *group)
168 ie = wpabuf_alloc(257);
172 len = p2p_buf_add_ie_hdr(ie);
173 p2p_group_add_common_ies(group, ie);
174 p2p_buf_add_device_id(ie, group->p2p->cfg->dev_addr);
175 p2p_group_add_noa(ie, group->noa);
176 p2p_buf_update_ie_hdr(ie, len);
182 static struct wpabuf * p2p_group_build_probe_resp_ie(struct p2p_group *group)
186 struct p2p_group_member *m;
189 ie = wpabuf_alloc(257);
193 len = p2p_buf_add_ie_hdr(ie);
195 p2p_group_add_common_ies(group, ie);
196 p2p_group_add_noa(ie, group->noa);
198 /* P2P Device Info */
199 p2p_buf_add_device_info(ie, group->p2p, NULL);
201 if (group->members) {
203 group_info = wpabuf_put(ie, 0);
204 wpabuf_put_u8(ie, P2P_ATTR_GROUP_INFO);
205 wpabuf_put_le16(ie, 0); /* Length to be filled */
206 for (m = group->members; m; m = m->next)
207 p2p_client_info(ie, m);
208 WPA_PUT_LE16(group_info + 1,
209 (u8 *) wpabuf_put(ie, 0) - group_info - 3);
212 p2p_buf_update_ie_hdr(ie, len);
217 static void p2p_group_update_ies(struct p2p_group *group)
219 struct wpabuf *beacon_ie;
220 struct wpabuf *probe_resp_ie;
222 probe_resp_ie = p2p_group_build_probe_resp_ie(group);
223 if (probe_resp_ie == NULL)
225 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Probe Response P2P IE",
228 if (group->beacon_update) {
229 beacon_ie = p2p_group_build_beacon_ie(group);
231 group->beacon_update = 0;
232 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Beacon P2P IE",
237 group->cfg->ie_update(group->cfg->cb_ctx, beacon_ie, probe_resp_ie);
242 * p2p_build_client_info - Build P2P Client Info Descriptor
243 * @addr: MAC address of the peer device
244 * @p2p_ie: P2P IE from (Re)Association Request
245 * @dev_capab: Buffer for returning Device Capability
246 * @dev_addr: Buffer for returning P2P Device Address
247 * Returns: P2P Client Info Descriptor or %NULL on failure
249 * This function builds P2P Client Info Descriptor based on the information
250 * available from (Re)Association Request frame. Group owner can use this to
251 * build the P2P Group Info attribute for Probe Response frames.
253 static struct wpabuf * p2p_build_client_info(const u8 *addr,
254 struct wpabuf *p2p_ie,
255 u8 *dev_capab, u8 *dev_addr)
258 struct p2p_message msg;
265 os_memset(&msg, 0, sizeof(msg));
266 if (p2p_parse_p2p_ie(p2p_ie, &msg) ||
267 msg.capability == NULL || msg.p2p_device_info == NULL)
270 buf = wpabuf_alloc(ETH_ALEN + 1 + 1 + msg.p2p_device_info_len);
274 *dev_capab = msg.capability[0];
275 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
277 spos = msg.p2p_device_info; /* P2P Device address */
279 /* P2P Client Info Descriptor */
280 /* Length to be set */
281 len_pos = wpabuf_put(buf, 1);
282 /* P2P Device address */
283 wpabuf_put_data(buf, spos, ETH_ALEN);
284 /* P2P Interface address */
285 wpabuf_put_data(buf, addr, ETH_ALEN);
286 /* Device Capability Bitmap */
287 wpabuf_put_u8(buf, msg.capability[0]);
289 * Config Methods, Primary Device Type, Number of Secondary Device
290 * Types, Secondary Device Type List, Device Name copied from
293 wpabuf_put_data(buf, spos + ETH_ALEN,
294 msg.p2p_device_info_len - ETH_ALEN);
296 *len_pos = wpabuf_len(buf) - 1;
303 int p2p_group_notif_assoc(struct p2p_group *group, const u8 *addr,
304 const u8 *ie, size_t len)
306 struct p2p_group_member *m;
311 m = os_zalloc(sizeof(*m));
314 os_memcpy(m->addr, addr, ETH_ALEN);
315 m->p2p_ie = ieee802_11_vendor_ie_concat(ie, len, P2P_IE_VENDOR_TYPE);
316 if (m->p2p_ie == NULL) {
317 p2p_group_free_member(m);
321 m->client_info = p2p_build_client_info(addr, m->p2p_ie, &m->dev_capab,
323 if (m->client_info == NULL) {
324 p2p_group_free_member(m);
328 m->next = group->members;
331 p2p_group_update_ies(group);
337 struct wpabuf * p2p_group_assoc_resp_ie(struct p2p_group *group, u8 status)
343 * (Re)Association Response - P2P IE
344 * Status attribute (shall be present when association request is
346 * Extended Listen Timing (may be present)
348 resp = wpabuf_alloc(20);
351 rlen = p2p_buf_add_ie_hdr(resp);
352 if (status != P2P_SC_SUCCESS)
353 p2p_buf_add_status(resp, status);
354 p2p_buf_update_ie_hdr(resp, rlen);
360 void p2p_group_notif_disassoc(struct p2p_group *group, const u8 *addr)
362 struct p2p_group_member *m, *prev;
370 if (os_memcmp(m->addr, addr, ETH_ALEN) == 0)
378 prev->next = m->next;
380 group->members = m->next;
381 p2p_group_free_member(m);
382 p2p_group_update_ies(group);
388 * p2p_match_dev_type_member - Match client device type with requested type
390 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
391 * Returns: 1 on match, 0 on mismatch
393 * This function can be used to match the Requested Device Type attribute in
394 * WPS IE with the device types of a group member for deciding whether a GO
395 * should reply to a Probe Request frame.
397 static int p2p_match_dev_type_member(struct p2p_group_member *m,
401 struct wps_parse_attr attr;
404 if (m->client_info == NULL || wps == NULL)
407 pos = wpabuf_head(m->client_info);
408 end = pos + wpabuf_len(m->client_info);
410 pos += 1 + 2 * ETH_ALEN + 1 + 2;
411 if (end - pos < WPS_DEV_TYPE_LEN + 1)
414 if (wps_parse_msg(wps, &attr))
415 return 1; /* assume no Requested Device Type attributes */
417 if (attr.num_req_dev_type == 0)
418 return 1; /* no Requested Device Type attributes -> match */
420 if (dev_type_list_match(pos, attr.req_dev_type, attr.num_req_dev_type))
421 return 1; /* Match with client Primary Device Type */
423 pos += WPS_DEV_TYPE_LEN;
425 if (end - pos < num_sec * WPS_DEV_TYPE_LEN)
427 while (num_sec > 0) {
429 if (dev_type_list_match(pos, attr.req_dev_type,
430 attr.num_req_dev_type))
431 return 1; /* Match with client Secondary Device Type */
432 pos += WPS_DEV_TYPE_LEN;
435 /* No matching device type found */
440 int p2p_group_match_dev_type(struct p2p_group *group, struct wpabuf *wps)
442 struct p2p_group_member *m;
444 if (p2p_match_dev_type(group->p2p, wps))
445 return 1; /* Match with own device type */
447 for (m = group->members; m; m = m->next) {
448 if (p2p_match_dev_type_member(m, wps))
449 return 1; /* Match with group client device type */
452 /* No match with Requested Device Type */
457 void p2p_group_notif_formation_done(struct p2p_group *group)
461 group->group_formation = 0;
462 group->beacon_update = 1;
463 p2p_group_update_ies(group);
467 int p2p_group_notif_noa(struct p2p_group *group, const u8 *noa,
471 wpabuf_free(group->noa);
475 if (wpabuf_size(group->noa) >= noa_len) {
476 group->noa->size = 0;
477 wpabuf_put_data(group->noa, noa, noa_len);
479 wpabuf_free(group->noa);
485 group->noa = wpabuf_alloc_copy(noa, noa_len);
486 if (group->noa == NULL)
491 group->beacon_update = 1;
492 p2p_group_update_ies(group);
497 static struct p2p_group_member * p2p_group_get_client(struct p2p_group *group,
500 struct p2p_group_member *m;
502 for (m = group->members; m; m = m->next) {
503 if (os_memcmp(dev_id, m->dev_addr, ETH_ALEN) == 0)
511 static struct p2p_group_member * p2p_group_get_client_iface(
512 struct p2p_group *group, const u8 *interface_addr)
514 struct p2p_group_member *m;
516 for (m = group->members; m; m = m->next) {
517 if (os_memcmp(interface_addr, m->addr, ETH_ALEN) == 0)
525 static struct wpabuf * p2p_build_go_disc_req(void)
529 buf = wpabuf_alloc(100);
533 p2p_buf_add_action_hdr(buf, P2P_GO_DISC_REQ, 0);
539 int p2p_group_go_discover(struct p2p_group *group, const u8 *dev_id,
540 const u8 *searching_dev, int rx_freq)
542 struct p2p_group_member *m;
544 struct p2p_data *p2p = group->p2p;
547 m = p2p_group_get_client(group, dev_id);
549 wpa_printf(MSG_DEBUG, "P2P: Requested client was not in this "
551 MAC2STR(group->cfg->interface_addr));
555 if (!(m->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
556 wpa_printf(MSG_DEBUG, "P2P: Requested client does not support "
557 "client discoverability");
561 wpa_printf(MSG_DEBUG, "P2P: Schedule GO Discoverability Request to be "
562 "sent to " MACSTR, MAC2STR(dev_id));
564 req = p2p_build_go_disc_req();
568 /* TODO: Should really use group operating frequency here */
571 p2p->pending_action_state = P2P_PENDING_GO_DISC_REQ;
572 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, m->addr,
573 group->cfg->interface_addr,
574 group->cfg->interface_addr,
575 wpabuf_head(req), wpabuf_len(req), 200) < 0)
577 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
578 "P2P: Failed to send Action frame");
587 const u8 * p2p_group_get_interface_addr(struct p2p_group *group)
589 return group->cfg->interface_addr;
593 u8 p2p_group_presence_req(struct p2p_group *group,
594 const u8 *client_interface_addr,
595 const u8 *noa, size_t noa_len)
597 struct p2p_group_member *m;
601 m = p2p_group_get_client_iface(group, client_interface_addr);
603 wpa_printf(MSG_DEBUG, "P2P: Client was not in this group");
604 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
607 wpa_hexdump(MSG_DEBUG, "P2P: Presence Request NoA", noa, noa_len);
609 if (group->p2p->cfg->get_noa)
610 curr_noa_len = group->p2p->cfg->get_noa(
611 group->p2p->cfg->cb_ctx, group->cfg->interface_addr,
612 curr_noa, sizeof(curr_noa));
615 if (curr_noa_len < 0)
616 wpa_printf(MSG_DEBUG, "P2P: Failed to fetch current NoA");
617 else if (curr_noa_len == 0)
618 wpa_printf(MSG_DEBUG, "P2P: No NoA being advertized");
620 wpa_hexdump(MSG_DEBUG, "P2P: Current NoA", curr_noa,
623 /* TODO: properly process request and store copy */
624 if (curr_noa_len > 0)
625 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
627 return P2P_SC_SUCCESS;