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 if (group->p2p->cfg->p2p_intra_bss)
148 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
149 if (group->group_formation)
150 group_capab |= P2P_GROUP_CAPAB_GROUP_FORMATION;
151 if (group->p2p->cross_connect)
152 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
153 p2p_buf_add_capability(ie, dev_capab, group_capab);
157 static void p2p_group_add_noa(struct wpabuf *ie, struct wpabuf *noa)
161 /* Notice of Absence */
162 wpabuf_put_u8(ie, P2P_ATTR_NOTICE_OF_ABSENCE);
163 wpabuf_put_le16(ie, wpabuf_len(noa));
164 wpabuf_put_buf(ie, noa);
168 static struct wpabuf * p2p_group_build_beacon_ie(struct p2p_group *group)
173 ie = wpabuf_alloc(257);
177 len = p2p_buf_add_ie_hdr(ie);
178 p2p_group_add_common_ies(group, ie);
179 p2p_buf_add_device_id(ie, group->p2p->cfg->dev_addr);
180 p2p_group_add_noa(ie, group->noa);
181 p2p_buf_update_ie_hdr(ie, len);
187 static struct wpabuf * p2p_group_build_probe_resp_ie(struct p2p_group *group)
191 struct p2p_group_member *m;
194 ie = wpabuf_alloc(257);
198 len = p2p_buf_add_ie_hdr(ie);
200 p2p_group_add_common_ies(group, ie);
201 p2p_group_add_noa(ie, group->noa);
203 /* P2P Device Info */
204 p2p_buf_add_device_info(ie, group->p2p, NULL);
207 group_info = wpabuf_put(ie, 0);
208 wpabuf_put_u8(ie, P2P_ATTR_GROUP_INFO);
209 wpabuf_put_le16(ie, 0); /* Length to be filled */
210 for (m = group->members; m; m = m->next)
211 p2p_client_info(ie, m);
212 WPA_PUT_LE16(group_info + 1,
213 (u8 *) wpabuf_put(ie, 0) - group_info - 3);
215 p2p_buf_update_ie_hdr(ie, len);
220 static void p2p_group_update_ies(struct p2p_group *group)
222 struct wpabuf *beacon_ie;
223 struct wpabuf *probe_resp_ie;
225 probe_resp_ie = p2p_group_build_probe_resp_ie(group);
226 if (probe_resp_ie == NULL)
228 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Probe Response P2P IE",
231 if (group->beacon_update) {
232 beacon_ie = p2p_group_build_beacon_ie(group);
234 group->beacon_update = 0;
235 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Beacon P2P IE",
240 group->cfg->ie_update(group->cfg->cb_ctx, beacon_ie, probe_resp_ie);
245 * p2p_build_client_info - Build P2P Client Info Descriptor
246 * @addr: MAC address of the peer device
247 * @p2p_ie: P2P IE from (Re)Association Request
248 * @dev_capab: Buffer for returning Device Capability
249 * @dev_addr: Buffer for returning P2P Device Address
250 * Returns: P2P Client Info Descriptor or %NULL on failure
252 * This function builds P2P Client Info Descriptor based on the information
253 * available from (Re)Association Request frame. Group owner can use this to
254 * build the P2P Group Info attribute for Probe Response frames.
256 static struct wpabuf * p2p_build_client_info(const u8 *addr,
257 struct wpabuf *p2p_ie,
258 u8 *dev_capab, u8 *dev_addr)
261 struct p2p_message msg;
268 os_memset(&msg, 0, sizeof(msg));
269 if (p2p_parse_p2p_ie(p2p_ie, &msg) ||
270 msg.capability == NULL || msg.p2p_device_info == NULL)
273 buf = wpabuf_alloc(ETH_ALEN + 1 + 1 + msg.p2p_device_info_len);
277 *dev_capab = msg.capability[0];
278 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
280 spos = msg.p2p_device_info; /* P2P Device address */
282 /* P2P Client Info Descriptor */
283 /* Length to be set */
284 len_pos = wpabuf_put(buf, 1);
285 /* P2P Device address */
286 wpabuf_put_data(buf, spos, ETH_ALEN);
287 /* P2P Interface address */
288 wpabuf_put_data(buf, addr, ETH_ALEN);
289 /* Device Capability Bitmap */
290 wpabuf_put_u8(buf, msg.capability[0]);
292 * Config Methods, Primary Device Type, Number of Secondary Device
293 * Types, Secondary Device Type List, Device Name copied from
296 wpabuf_put_data(buf, spos + ETH_ALEN,
297 msg.p2p_device_info_len - ETH_ALEN);
299 *len_pos = wpabuf_len(buf) - 1;
306 int p2p_group_notif_assoc(struct p2p_group *group, const u8 *addr,
307 const u8 *ie, size_t len)
309 struct p2p_group_member *m;
314 m = os_zalloc(sizeof(*m));
317 os_memcpy(m->addr, addr, ETH_ALEN);
318 m->p2p_ie = ieee802_11_vendor_ie_concat(ie, len, P2P_IE_VENDOR_TYPE);
319 if (m->p2p_ie == NULL) {
320 p2p_group_free_member(m);
324 m->client_info = p2p_build_client_info(addr, m->p2p_ie, &m->dev_capab,
326 if (m->client_info == NULL) {
328 * This can happen, e.g., when a P2P client connects to a P2P
329 * group using the infrastructure WLAN interface instead of
330 * P2P group interface. In that case, the P2P client may behave
331 * as if the GO would be a P2P Manager WLAN AP.
333 wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG,
334 "P2P: Could not build Client Info from P2P IE - "
335 "assume " MACSTR " is not a P2P client",
337 p2p_group_free_member(m);
341 m->next = group->members;
344 p2p_group_update_ies(group);
350 struct wpabuf * p2p_group_assoc_resp_ie(struct p2p_group *group, u8 status)
356 * (Re)Association Response - P2P IE
357 * Status attribute (shall be present when association request is
359 * Extended Listen Timing (may be present)
361 resp = wpabuf_alloc(20);
364 rlen = p2p_buf_add_ie_hdr(resp);
365 if (status != P2P_SC_SUCCESS)
366 p2p_buf_add_status(resp, status);
367 p2p_buf_update_ie_hdr(resp, rlen);
373 void p2p_group_notif_disassoc(struct p2p_group *group, const u8 *addr)
375 struct p2p_group_member *m, *prev;
383 if (os_memcmp(m->addr, addr, ETH_ALEN) == 0)
391 prev->next = m->next;
393 group->members = m->next;
394 p2p_group_free_member(m);
395 p2p_group_update_ies(group);
401 * p2p_match_dev_type_member - Match client device type with requested type
403 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
404 * Returns: 1 on match, 0 on mismatch
406 * This function can be used to match the Requested Device Type attribute in
407 * WPS IE with the device types of a group member for deciding whether a GO
408 * should reply to a Probe Request frame.
410 static int p2p_match_dev_type_member(struct p2p_group_member *m,
414 struct wps_parse_attr attr;
417 if (m->client_info == NULL || wps == NULL)
420 pos = wpabuf_head(m->client_info);
421 end = pos + wpabuf_len(m->client_info);
423 pos += 1 + 2 * ETH_ALEN + 1 + 2;
424 if (end - pos < WPS_DEV_TYPE_LEN + 1)
427 if (wps_parse_msg(wps, &attr))
428 return 1; /* assume no Requested Device Type attributes */
430 if (attr.num_req_dev_type == 0)
431 return 1; /* no Requested Device Type attributes -> match */
433 if (dev_type_list_match(pos, attr.req_dev_type, attr.num_req_dev_type))
434 return 1; /* Match with client Primary Device Type */
436 pos += WPS_DEV_TYPE_LEN;
438 if (end - pos < num_sec * WPS_DEV_TYPE_LEN)
440 while (num_sec > 0) {
442 if (dev_type_list_match(pos, attr.req_dev_type,
443 attr.num_req_dev_type))
444 return 1; /* Match with client Secondary Device Type */
445 pos += WPS_DEV_TYPE_LEN;
448 /* No matching device type found */
453 int p2p_group_match_dev_type(struct p2p_group *group, struct wpabuf *wps)
455 struct p2p_group_member *m;
457 if (p2p_match_dev_type(group->p2p, wps))
458 return 1; /* Match with own device type */
460 for (m = group->members; m; m = m->next) {
461 if (p2p_match_dev_type_member(m, wps))
462 return 1; /* Match with group client device type */
465 /* No match with Requested Device Type */
470 void p2p_group_notif_formation_done(struct p2p_group *group)
474 group->group_formation = 0;
475 group->beacon_update = 1;
476 p2p_group_update_ies(group);
480 int p2p_group_notif_noa(struct p2p_group *group, const u8 *noa,
484 wpabuf_free(group->noa);
488 if (wpabuf_size(group->noa) >= noa_len) {
489 group->noa->size = 0;
490 wpabuf_put_data(group->noa, noa, noa_len);
492 wpabuf_free(group->noa);
498 group->noa = wpabuf_alloc_copy(noa, noa_len);
499 if (group->noa == NULL)
504 group->beacon_update = 1;
505 p2p_group_update_ies(group);
510 static struct p2p_group_member * p2p_group_get_client(struct p2p_group *group,
513 struct p2p_group_member *m;
515 for (m = group->members; m; m = m->next) {
516 if (os_memcmp(dev_id, m->dev_addr, ETH_ALEN) == 0)
524 static struct p2p_group_member * p2p_group_get_client_iface(
525 struct p2p_group *group, const u8 *interface_addr)
527 struct p2p_group_member *m;
529 for (m = group->members; m; m = m->next) {
530 if (os_memcmp(interface_addr, m->addr, ETH_ALEN) == 0)
538 static struct wpabuf * p2p_build_go_disc_req(void)
542 buf = wpabuf_alloc(100);
546 p2p_buf_add_action_hdr(buf, P2P_GO_DISC_REQ, 0);
552 int p2p_group_go_discover(struct p2p_group *group, const u8 *dev_id,
553 const u8 *searching_dev, int rx_freq)
555 struct p2p_group_member *m;
557 struct p2p_data *p2p = group->p2p;
560 m = p2p_group_get_client(group, dev_id);
562 wpa_printf(MSG_DEBUG, "P2P: Requested client was not in this "
564 MAC2STR(group->cfg->interface_addr));
568 if (!(m->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
569 wpa_printf(MSG_DEBUG, "P2P: Requested client does not support "
570 "client discoverability");
574 wpa_printf(MSG_DEBUG, "P2P: Schedule GO Discoverability Request to be "
575 "sent to " MACSTR, MAC2STR(dev_id));
577 req = p2p_build_go_disc_req();
581 /* TODO: Should really use group operating frequency here */
584 p2p->pending_action_state = P2P_PENDING_GO_DISC_REQ;
585 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, m->addr,
586 group->cfg->interface_addr,
587 group->cfg->interface_addr,
588 wpabuf_head(req), wpabuf_len(req), 200) < 0)
590 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
591 "P2P: Failed to send Action frame");
600 const u8 * p2p_group_get_interface_addr(struct p2p_group *group)
602 return group->cfg->interface_addr;
606 u8 p2p_group_presence_req(struct p2p_group *group,
607 const u8 *client_interface_addr,
608 const u8 *noa, size_t noa_len)
610 struct p2p_group_member *m;
614 m = p2p_group_get_client_iface(group, client_interface_addr);
616 wpa_printf(MSG_DEBUG, "P2P: Client was not in this group");
617 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
620 wpa_hexdump(MSG_DEBUG, "P2P: Presence Request NoA", noa, noa_len);
622 if (group->p2p->cfg->get_noa)
623 curr_noa_len = group->p2p->cfg->get_noa(
624 group->p2p->cfg->cb_ctx, group->cfg->interface_addr,
625 curr_noa, sizeof(curr_noa));
628 if (curr_noa_len < 0)
629 wpa_printf(MSG_DEBUG, "P2P: Failed to fetch current NoA");
630 else if (curr_noa_len == 0)
631 wpa_printf(MSG_DEBUG, "P2P: No NoA being advertized");
633 wpa_hexdump(MSG_DEBUG, "P2P: Current NoA", curr_noa,
636 /* TODO: properly process request and store copy */
637 if (curr_noa_len > 0)
638 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
640 return P2P_SC_SUCCESS;