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;
110 for (g = 0; g < p2p->num_groups; g++) {
111 if (p2p->groups[g] == group) {
112 while (g + 1 < p2p->num_groups) {
113 p2p->groups[g] = p2p->groups[g + 1];
121 p2p_group_free_members(group);
123 wpabuf_free(group->noa);
128 static void p2p_client_info(struct wpabuf *ie, struct p2p_group_member *m)
130 if (wpabuf_tailroom(ie) < wpabuf_len(m->client_info) + 1)
132 wpabuf_put_buf(ie, m->client_info);
136 static void p2p_group_add_common_ies(struct p2p_group *group,
139 u8 dev_capab = 0, group_capab = 0;
142 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
143 dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
144 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
145 if (group->cfg->persistent_group)
146 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
147 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
148 if (group->group_formation)
149 group_capab |= P2P_GROUP_CAPAB_GROUP_FORMATION;
150 if (group->p2p->cross_connect)
151 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
152 p2p_buf_add_capability(ie, dev_capab, group_capab);
156 static void p2p_group_add_noa(struct wpabuf *ie, struct wpabuf *noa)
160 /* Notice of Absence */
161 wpabuf_put_u8(ie, P2P_ATTR_NOTICE_OF_ABSENCE);
162 wpabuf_put_le16(ie, wpabuf_len(noa));
163 wpabuf_put_buf(ie, noa);
167 static struct wpabuf * p2p_group_build_beacon_ie(struct p2p_group *group)
172 ie = wpabuf_alloc(257);
176 len = p2p_buf_add_ie_hdr(ie);
177 p2p_group_add_common_ies(group, ie);
178 p2p_buf_add_device_id(ie, group->p2p->cfg->dev_addr);
179 p2p_group_add_noa(ie, group->noa);
180 p2p_buf_update_ie_hdr(ie, len);
186 static struct wpabuf * p2p_group_build_probe_resp_ie(struct p2p_group *group)
190 struct p2p_group_member *m;
193 ie = wpabuf_alloc(257);
197 len = p2p_buf_add_ie_hdr(ie);
199 p2p_group_add_common_ies(group, ie);
200 p2p_group_add_noa(ie, group->noa);
202 /* P2P Device Info */
203 p2p_buf_add_device_info(ie, group->p2p, NULL);
206 group_info = wpabuf_put(ie, 0);
207 wpabuf_put_u8(ie, P2P_ATTR_GROUP_INFO);
208 wpabuf_put_le16(ie, 0); /* Length to be filled */
209 for (m = group->members; m; m = m->next)
210 p2p_client_info(ie, m);
211 WPA_PUT_LE16(group_info + 1,
212 (u8 *) wpabuf_put(ie, 0) - group_info - 3);
214 p2p_buf_update_ie_hdr(ie, len);
219 static void p2p_group_update_ies(struct p2p_group *group)
221 struct wpabuf *beacon_ie;
222 struct wpabuf *probe_resp_ie;
224 probe_resp_ie = p2p_group_build_probe_resp_ie(group);
225 if (probe_resp_ie == NULL)
227 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Probe Response P2P IE",
230 if (group->beacon_update) {
231 beacon_ie = p2p_group_build_beacon_ie(group);
233 group->beacon_update = 0;
234 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Beacon P2P IE",
239 group->cfg->ie_update(group->cfg->cb_ctx, beacon_ie, probe_resp_ie);
244 * p2p_build_client_info - Build P2P Client Info Descriptor
245 * @addr: MAC address of the peer device
246 * @p2p_ie: P2P IE from (Re)Association Request
247 * @dev_capab: Buffer for returning Device Capability
248 * @dev_addr: Buffer for returning P2P Device Address
249 * Returns: P2P Client Info Descriptor or %NULL on failure
251 * This function builds P2P Client Info Descriptor based on the information
252 * available from (Re)Association Request frame. Group owner can use this to
253 * build the P2P Group Info attribute for Probe Response frames.
255 static struct wpabuf * p2p_build_client_info(const u8 *addr,
256 struct wpabuf *p2p_ie,
257 u8 *dev_capab, u8 *dev_addr)
260 struct p2p_message msg;
267 os_memset(&msg, 0, sizeof(msg));
268 if (p2p_parse_p2p_ie(p2p_ie, &msg) ||
269 msg.capability == NULL || msg.p2p_device_info == NULL)
272 buf = wpabuf_alloc(ETH_ALEN + 1 + 1 + msg.p2p_device_info_len);
276 *dev_capab = msg.capability[0];
277 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
279 spos = msg.p2p_device_info; /* P2P Device address */
281 /* P2P Client Info Descriptor */
282 /* Length to be set */
283 len_pos = wpabuf_put(buf, 1);
284 /* P2P Device address */
285 wpabuf_put_data(buf, spos, ETH_ALEN);
286 /* P2P Interface address */
287 wpabuf_put_data(buf, addr, ETH_ALEN);
288 /* Device Capability Bitmap */
289 wpabuf_put_u8(buf, msg.capability[0]);
291 * Config Methods, Primary Device Type, Number of Secondary Device
292 * Types, Secondary Device Type List, Device Name copied from
295 wpabuf_put_data(buf, spos + ETH_ALEN,
296 msg.p2p_device_info_len - ETH_ALEN);
298 *len_pos = wpabuf_len(buf) - 1;
305 int p2p_group_notif_assoc(struct p2p_group *group, const u8 *addr,
306 const u8 *ie, size_t len)
308 struct p2p_group_member *m;
313 m = os_zalloc(sizeof(*m));
316 os_memcpy(m->addr, addr, ETH_ALEN);
317 m->p2p_ie = ieee802_11_vendor_ie_concat(ie, len, P2P_IE_VENDOR_TYPE);
318 if (m->p2p_ie == NULL) {
319 p2p_group_free_member(m);
323 m->client_info = p2p_build_client_info(addr, m->p2p_ie, &m->dev_capab,
325 if (m->client_info == NULL) {
327 * This can happen, e.g., when a P2P client connects to a P2P
328 * group using the infrastructure WLAN interface instead of
329 * P2P group interface. In that case, the P2P client may behave
330 * as if the GO would be a P2P Manager WLAN AP.
332 wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG,
333 "P2P: Could not build Client Info from P2P IE - "
334 "assume " MACSTR " is not a P2P client",
336 p2p_group_free_member(m);
340 m->next = group->members;
343 p2p_group_update_ies(group);
349 struct wpabuf * p2p_group_assoc_resp_ie(struct p2p_group *group, u8 status)
355 * (Re)Association Response - P2P IE
356 * Status attribute (shall be present when association request is
358 * Extended Listen Timing (may be present)
360 resp = wpabuf_alloc(20);
363 rlen = p2p_buf_add_ie_hdr(resp);
364 if (status != P2P_SC_SUCCESS)
365 p2p_buf_add_status(resp, status);
366 p2p_buf_update_ie_hdr(resp, rlen);
372 void p2p_group_notif_disassoc(struct p2p_group *group, const u8 *addr)
374 struct p2p_group_member *m, *prev;
382 if (os_memcmp(m->addr, addr, ETH_ALEN) == 0)
390 prev->next = m->next;
392 group->members = m->next;
393 p2p_group_free_member(m);
394 p2p_group_update_ies(group);
400 * p2p_match_dev_type_member - Match client device type with requested type
402 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
403 * Returns: 1 on match, 0 on mismatch
405 * This function can be used to match the Requested Device Type attribute in
406 * WPS IE with the device types of a group member for deciding whether a GO
407 * should reply to a Probe Request frame.
409 static int p2p_match_dev_type_member(struct p2p_group_member *m,
413 struct wps_parse_attr attr;
416 if (m->client_info == NULL || wps == NULL)
419 pos = wpabuf_head(m->client_info);
420 end = pos + wpabuf_len(m->client_info);
422 pos += 1 + 2 * ETH_ALEN + 1 + 2;
423 if (end - pos < WPS_DEV_TYPE_LEN + 1)
426 if (wps_parse_msg(wps, &attr))
427 return 1; /* assume no Requested Device Type attributes */
429 if (attr.num_req_dev_type == 0)
430 return 1; /* no Requested Device Type attributes -> match */
432 if (dev_type_list_match(pos, attr.req_dev_type, attr.num_req_dev_type))
433 return 1; /* Match with client Primary Device Type */
435 pos += WPS_DEV_TYPE_LEN;
437 if (end - pos < num_sec * WPS_DEV_TYPE_LEN)
439 while (num_sec > 0) {
441 if (dev_type_list_match(pos, attr.req_dev_type,
442 attr.num_req_dev_type))
443 return 1; /* Match with client Secondary Device Type */
444 pos += WPS_DEV_TYPE_LEN;
447 /* No matching device type found */
452 int p2p_group_match_dev_type(struct p2p_group *group, struct wpabuf *wps)
454 struct p2p_group_member *m;
456 if (p2p_match_dev_type(group->p2p, wps))
457 return 1; /* Match with own device type */
459 for (m = group->members; m; m = m->next) {
460 if (p2p_match_dev_type_member(m, wps))
461 return 1; /* Match with group client device type */
464 /* No match with Requested Device Type */
469 void p2p_group_notif_formation_done(struct p2p_group *group)
473 group->group_formation = 0;
474 group->beacon_update = 1;
475 p2p_group_update_ies(group);
479 int p2p_group_notif_noa(struct p2p_group *group, const u8 *noa,
483 wpabuf_free(group->noa);
487 if (wpabuf_size(group->noa) >= noa_len) {
488 group->noa->size = 0;
489 wpabuf_put_data(group->noa, noa, noa_len);
491 wpabuf_free(group->noa);
497 group->noa = wpabuf_alloc_copy(noa, noa_len);
498 if (group->noa == NULL)
503 group->beacon_update = 1;
504 p2p_group_update_ies(group);
509 static struct p2p_group_member * p2p_group_get_client(struct p2p_group *group,
512 struct p2p_group_member *m;
514 for (m = group->members; m; m = m->next) {
515 if (os_memcmp(dev_id, m->dev_addr, ETH_ALEN) == 0)
523 static struct p2p_group_member * p2p_group_get_client_iface(
524 struct p2p_group *group, const u8 *interface_addr)
526 struct p2p_group_member *m;
528 for (m = group->members; m; m = m->next) {
529 if (os_memcmp(interface_addr, m->addr, ETH_ALEN) == 0)
537 static struct wpabuf * p2p_build_go_disc_req(void)
541 buf = wpabuf_alloc(100);
545 p2p_buf_add_action_hdr(buf, P2P_GO_DISC_REQ, 0);
551 int p2p_group_go_discover(struct p2p_group *group, const u8 *dev_id,
552 const u8 *searching_dev, int rx_freq)
554 struct p2p_group_member *m;
556 struct p2p_data *p2p = group->p2p;
559 m = p2p_group_get_client(group, dev_id);
561 wpa_printf(MSG_DEBUG, "P2P: Requested client was not in this "
563 MAC2STR(group->cfg->interface_addr));
567 if (!(m->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
568 wpa_printf(MSG_DEBUG, "P2P: Requested client does not support "
569 "client discoverability");
573 wpa_printf(MSG_DEBUG, "P2P: Schedule GO Discoverability Request to be "
574 "sent to " MACSTR, MAC2STR(dev_id));
576 req = p2p_build_go_disc_req();
580 /* TODO: Should really use group operating frequency here */
583 p2p->pending_action_state = P2P_PENDING_GO_DISC_REQ;
584 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, m->addr,
585 group->cfg->interface_addr,
586 group->cfg->interface_addr,
587 wpabuf_head(req), wpabuf_len(req), 200) < 0)
589 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
590 "P2P: Failed to send Action frame");
599 const u8 * p2p_group_get_interface_addr(struct p2p_group *group)
601 return group->cfg->interface_addr;
605 u8 p2p_group_presence_req(struct p2p_group *group,
606 const u8 *client_interface_addr,
607 const u8 *noa, size_t noa_len)
609 struct p2p_group_member *m;
613 m = p2p_group_get_client_iface(group, client_interface_addr);
615 wpa_printf(MSG_DEBUG, "P2P: Client was not in this group");
616 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
619 wpa_hexdump(MSG_DEBUG, "P2P: Presence Request NoA", noa, noa_len);
621 if (group->p2p->cfg->get_noa)
622 curr_noa_len = group->p2p->cfg->get_noa(
623 group->p2p->cfg->cb_ctx, group->cfg->interface_addr,
624 curr_noa, sizeof(curr_noa));
627 if (curr_noa_len < 0)
628 wpa_printf(MSG_DEBUG, "P2P: Failed to fetch current NoA");
629 else if (curr_noa_len == 0)
630 wpa_printf(MSG_DEBUG, "P2P: No NoA being advertized");
632 wpa_hexdump(MSG_DEBUG, "P2P: Current NoA", curr_noa,
635 /* TODO: properly process request and store copy */
636 if (curr_noa_len > 0)
637 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
639 return P2P_SC_SUCCESS;