2 * Wi-Fi Direct - P2P group operations
3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/ieee802_11_defs.h"
13 #include "common/ieee802_11_common.h"
14 #include "common/wpa_ctrl.h"
15 #include "wps/wps_defs.h"
16 #include "wps/wps_i.h"
21 struct p2p_group_member
{
22 struct p2p_group_member
*next
;
23 u8 addr
[ETH_ALEN
]; /* P2P Interface Address */
24 u8 dev_addr
[ETH_ALEN
]; /* P2P Device Address */
25 struct wpabuf
*p2p_ie
;
26 struct wpabuf
*wfd_ie
;
27 struct wpabuf
*client_info
;
32 * struct p2p_group - Internal P2P module per-group data
36 struct p2p_group_config
*cfg
;
37 struct p2p_group_member
*members
;
38 unsigned int num_members
;
42 struct wpabuf
*wfd_ie
;
46 struct p2p_group
* p2p_group_init(struct p2p_data
*p2p
,
47 struct p2p_group_config
*config
)
49 struct p2p_group
*group
, **groups
;
51 group
= os_zalloc(sizeof(*group
));
55 groups
= os_realloc_array(p2p
->groups
, p2p
->num_groups
+ 1,
56 sizeof(struct p2p_group
*));
61 groups
[p2p
->num_groups
++] = group
;
66 group
->group_formation
= 1;
67 group
->beacon_update
= 1;
68 p2p_group_update_ies(group
);
69 group
->cfg
->idle_update(group
->cfg
->cb_ctx
, 1);
75 static void p2p_group_free_member(struct p2p_group_member
*m
)
77 wpabuf_free(m
->wfd_ie
);
78 wpabuf_free(m
->p2p_ie
);
79 wpabuf_free(m
->client_info
);
84 static void p2p_group_free_members(struct p2p_group
*group
)
86 struct p2p_group_member
*m
, *prev
;
88 group
->members
= NULL
;
89 group
->num_members
= 0;
93 p2p_group_free_member(prev
);
98 void p2p_group_deinit(struct p2p_group
*group
)
101 struct p2p_data
*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
);
122 wpabuf_free(group
->wfd_ie
);
127 static void p2p_client_info(struct wpabuf
*ie
, struct p2p_group_member
*m
)
129 if (m
->client_info
== NULL
)
131 if (wpabuf_tailroom(ie
) < wpabuf_len(m
->client_info
) + 1)
133 wpabuf_put_buf(ie
, m
->client_info
);
137 static void p2p_group_add_common_ies(struct p2p_group
*group
,
140 u8 dev_capab
= group
->p2p
->dev_capab
, group_capab
= 0;
143 dev_capab
&= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
;
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
->cfg
->persistent_group
== 2)
148 group_capab
|= P2P_GROUP_CAPAB_PERSISTENT_RECONN
;
150 if (group
->p2p
->cfg
->p2p_intra_bss
)
151 group_capab
|= P2P_GROUP_CAPAB_INTRA_BSS_DIST
;
152 if (group
->group_formation
)
153 group_capab
|= P2P_GROUP_CAPAB_GROUP_FORMATION
;
154 if (group
->p2p
->cross_connect
)
155 group_capab
|= P2P_GROUP_CAPAB_CROSS_CONN
;
156 if (group
->num_members
>= group
->cfg
->max_clients
)
157 group_capab
|= P2P_GROUP_CAPAB_GROUP_LIMIT
;
158 if (group
->cfg
->ip_addr_alloc
)
159 group_capab
|= P2P_GROUP_CAPAB_IP_ADDR_ALLOCATION
;
160 p2p_buf_add_capability(ie
, dev_capab
, group_capab
);
164 static void p2p_group_add_noa(struct wpabuf
*ie
, struct wpabuf
*noa
)
168 /* Notice of Absence */
169 wpabuf_put_u8(ie
, P2P_ATTR_NOTICE_OF_ABSENCE
);
170 wpabuf_put_le16(ie
, wpabuf_len(noa
));
171 wpabuf_put_buf(ie
, noa
);
175 static struct wpabuf
* p2p_group_encaps_probe_resp(struct wpabuf
*subelems
)
181 if (subelems
== NULL
)
184 len
= wpabuf_len(subelems
) + 100;
186 ie
= wpabuf_alloc(len
);
190 pos
= wpabuf_head(subelems
);
191 end
= pos
+ wpabuf_len(subelems
);
194 size_t frag_len
= end
- pos
;
197 wpabuf_put_u8(ie
, WLAN_EID_VENDOR_SPECIFIC
);
198 wpabuf_put_u8(ie
, 4 + frag_len
);
199 wpabuf_put_be32(ie
, P2P_IE_VENDOR_TYPE
);
200 wpabuf_put_data(ie
, pos
, frag_len
);
208 static struct wpabuf
* p2p_group_build_beacon_ie(struct p2p_group
*group
)
214 #ifdef CONFIG_WIFI_DISPLAY
215 if (group
->p2p
->wfd_ie_beacon
)
216 extra
= wpabuf_len(group
->p2p
->wfd_ie_beacon
);
217 #endif /* CONFIG_WIFI_DISPLAY */
219 if (group
->p2p
->vendor_elem
&&
220 group
->p2p
->vendor_elem
[VENDOR_ELEM_BEACON_P2P_GO
])
221 extra
+= wpabuf_len(group
->p2p
->vendor_elem
[VENDOR_ELEM_BEACON_P2P_GO
]);
223 ie
= wpabuf_alloc(257 + extra
);
227 #ifdef CONFIG_WIFI_DISPLAY
228 if (group
->p2p
->wfd_ie_beacon
)
229 wpabuf_put_buf(ie
, group
->p2p
->wfd_ie_beacon
);
230 #endif /* CONFIG_WIFI_DISPLAY */
232 if (group
->p2p
->vendor_elem
&&
233 group
->p2p
->vendor_elem
[VENDOR_ELEM_BEACON_P2P_GO
])
235 group
->p2p
->vendor_elem
[VENDOR_ELEM_BEACON_P2P_GO
]);
237 len
= p2p_buf_add_ie_hdr(ie
);
238 p2p_group_add_common_ies(group
, ie
);
239 p2p_buf_add_device_id(ie
, group
->p2p
->cfg
->dev_addr
);
240 p2p_group_add_noa(ie
, group
->noa
);
241 p2p_buf_update_ie_hdr(ie
, len
);
247 #ifdef CONFIG_WIFI_DISPLAY
249 struct wpabuf
* p2p_group_get_wfd_ie(struct p2p_group
*g
)
255 struct wpabuf
* wifi_display_encaps(struct wpabuf
*subelems
)
260 if (subelems
== NULL
)
263 ie
= wpabuf_alloc(wpabuf_len(subelems
) + 100);
267 pos
= wpabuf_head(subelems
);
268 end
= pos
+ wpabuf_len(subelems
);
271 size_t frag_len
= end
- pos
;
274 wpabuf_put_u8(ie
, WLAN_EID_VENDOR_SPECIFIC
);
275 wpabuf_put_u8(ie
, 4 + frag_len
);
276 wpabuf_put_be32(ie
, WFD_IE_VENDOR_TYPE
);
277 wpabuf_put_data(ie
, pos
, frag_len
);
285 static int wifi_display_add_dev_info_descr(struct wpabuf
*buf
,
286 struct p2p_group_member
*m
)
289 const u8
*dev_info
= NULL
;
290 const u8
*assoc_bssid
= NULL
;
291 const u8
*coupled_sink
= NULL
;
292 u8 zero_addr
[ETH_ALEN
];
294 if (m
->wfd_ie
== NULL
)
297 os_memset(zero_addr
, 0, ETH_ALEN
);
298 pos
= wpabuf_head_u8(m
->wfd_ie
);
299 end
= pos
+ wpabuf_len(m
->wfd_ie
);
300 while (end
- pos
>= 3) {
305 len
= WPA_GET_BE16(pos
);
311 case WFD_SUBELEM_DEVICE_INFO
:
316 case WFD_SUBELEM_ASSOCIATED_BSSID
:
321 case WFD_SUBELEM_COUPLED_SINK
:
322 if (len
< 1 + ETH_ALEN
)
331 if (dev_info
== NULL
)
334 wpabuf_put_u8(buf
, 23);
335 wpabuf_put_data(buf
, m
->dev_addr
, ETH_ALEN
);
337 wpabuf_put_data(buf
, assoc_bssid
, ETH_ALEN
);
339 wpabuf_put_data(buf
, zero_addr
, ETH_ALEN
);
340 wpabuf_put_data(buf
, dev_info
, 2); /* WFD Device Info */
341 wpabuf_put_data(buf
, dev_info
+ 4, 2); /* WFD Device Max Throughput */
343 wpabuf_put_data(buf
, coupled_sink
, 1 + ETH_ALEN
);
345 wpabuf_put_u8(buf
, 0);
346 wpabuf_put_data(buf
, zero_addr
, ETH_ALEN
);
353 static struct wpabuf
*
354 wifi_display_build_go_ie(struct p2p_group
*group
)
356 struct wpabuf
*wfd_subelems
, *wfd_ie
;
357 struct p2p_group_member
*m
;
359 unsigned int count
= 0;
361 if (!group
->p2p
->wfd_ie_probe_resp
)
364 wfd_subelems
= wpabuf_alloc(wpabuf_len(group
->p2p
->wfd_ie_probe_resp
) +
365 group
->num_members
* 24 + 100);
366 if (wfd_subelems
== NULL
)
368 if (group
->p2p
->wfd_dev_info
)
369 wpabuf_put_buf(wfd_subelems
, group
->p2p
->wfd_dev_info
);
370 if (group
->p2p
->wfd_r2_dev_info
)
371 wpabuf_put_buf(wfd_subelems
, group
->p2p
->wfd_r2_dev_info
);
372 if (group
->p2p
->wfd_assoc_bssid
)
373 wpabuf_put_buf(wfd_subelems
,
374 group
->p2p
->wfd_assoc_bssid
);
375 if (group
->p2p
->wfd_coupled_sink_info
)
376 wpabuf_put_buf(wfd_subelems
,
377 group
->p2p
->wfd_coupled_sink_info
);
379 /* Build WFD Session Info */
380 wpabuf_put_u8(wfd_subelems
, WFD_SUBELEM_SESSION_INFO
);
381 len
= wpabuf_put(wfd_subelems
, 2);
384 if (wifi_display_add_dev_info_descr(wfd_subelems
, m
))
390 /* No Wi-Fi Display clients - do not include subelement */
391 wfd_subelems
->used
-= 3;
393 WPA_PUT_BE16(len
, (u8
*) wpabuf_put(wfd_subelems
, 0) - len
-
395 p2p_dbg(group
->p2p
, "WFD: WFD Session Info: %u descriptors",
399 wfd_ie
= wifi_display_encaps(wfd_subelems
);
400 wpabuf_free(wfd_subelems
);
405 static void wifi_display_group_update(struct p2p_group
*group
)
407 wpabuf_free(group
->wfd_ie
);
408 group
->wfd_ie
= wifi_display_build_go_ie(group
);
411 #endif /* CONFIG_WIFI_DISPLAY */
414 void p2p_buf_add_group_info(struct p2p_group
*group
, struct wpabuf
*buf
,
419 struct p2p_group_member
*m
;
421 p2p_dbg(group
->p2p
, "* P2P Group Info");
422 group_info
= wpabuf_put(buf
, 0);
423 wpabuf_put_u8(buf
, P2P_ATTR_GROUP_INFO
);
424 wpabuf_put_le16(buf
, 0); /* Length to be filled */
425 for (m
= group
->members
; m
; m
= m
->next
) {
426 p2p_client_info(buf
, m
);
428 if (max_clients
>= 0 && count
>= max_clients
)
431 WPA_PUT_LE16(group_info
+ 1,
432 (u8
*) wpabuf_put(buf
, 0) - group_info
- 3);
436 void p2p_group_buf_add_id(struct p2p_group
*group
, struct wpabuf
*buf
)
438 p2p_buf_add_group_id(buf
, group
->p2p
->cfg
->dev_addr
, group
->cfg
->ssid
,
439 group
->cfg
->ssid_len
);
443 static struct wpabuf
* p2p_group_build_probe_resp_ie(struct p2p_group
*group
)
445 struct wpabuf
*p2p_subelems
, *ie
;
447 p2p_subelems
= wpabuf_alloc(500);
448 if (p2p_subelems
== NULL
)
451 p2p_group_add_common_ies(group
, p2p_subelems
);
452 p2p_group_add_noa(p2p_subelems
, group
->noa
);
454 /* P2P Device Info */
455 p2p_buf_add_device_info(p2p_subelems
, group
->p2p
, NULL
);
457 /* P2P Group Info: Only when at least one P2P Client is connected */
459 p2p_buf_add_group_info(group
, p2p_subelems
, -1);
461 ie
= p2p_group_encaps_probe_resp(p2p_subelems
);
462 wpabuf_free(p2p_subelems
);
464 if (group
->p2p
->vendor_elem
&&
465 group
->p2p
->vendor_elem
[VENDOR_ELEM_PROBE_RESP_P2P_GO
]) {
466 struct wpabuf
*extra
;
467 extra
= wpabuf_dup(group
->p2p
->vendor_elem
[VENDOR_ELEM_PROBE_RESP_P2P_GO
]);
468 ie
= wpabuf_concat(extra
, ie
);
471 #ifdef CONFIG_WIFI_DISPLAY
473 struct wpabuf
*wfd
= wpabuf_dup(group
->wfd_ie
);
474 ie
= wpabuf_concat(wfd
, ie
);
476 #endif /* CONFIG_WIFI_DISPLAY */
482 void p2p_group_update_ies(struct p2p_group
*group
)
484 struct wpabuf
*beacon_ie
;
485 struct wpabuf
*probe_resp_ie
;
487 #ifdef CONFIG_WIFI_DISPLAY
488 wifi_display_group_update(group
);
489 #endif /* CONFIG_WIFI_DISPLAY */
491 probe_resp_ie
= p2p_group_build_probe_resp_ie(group
);
492 if (probe_resp_ie
== NULL
)
494 wpa_hexdump_buf(MSG_MSGDUMP
, "P2P: Update GO Probe Response P2P IE",
497 if (group
->beacon_update
) {
498 beacon_ie
= p2p_group_build_beacon_ie(group
);
500 group
->beacon_update
= 0;
501 wpa_hexdump_buf(MSG_MSGDUMP
, "P2P: Update GO Beacon P2P IE",
506 group
->cfg
->ie_update(group
->cfg
->cb_ctx
, beacon_ie
, probe_resp_ie
);
511 * p2p_build_client_info - Build P2P Client Info Descriptor
512 * @addr: MAC address of the peer device
513 * @p2p_ie: P2P IE from (Re)Association Request
514 * @dev_capab: Buffer for returning Device Capability
515 * @dev_addr: Buffer for returning P2P Device Address
516 * Returns: P2P Client Info Descriptor or %NULL on failure
518 * This function builds P2P Client Info Descriptor based on the information
519 * available from (Re)Association Request frame. Group owner can use this to
520 * build the P2P Group Info attribute for Probe Response frames.
522 static struct wpabuf
* p2p_build_client_info(const u8
*addr
,
523 struct wpabuf
*p2p_ie
,
524 u8
*dev_capab
, u8
*dev_addr
)
527 struct p2p_message msg
;
534 os_memset(&msg
, 0, sizeof(msg
));
535 if (p2p_parse_p2p_ie(p2p_ie
, &msg
) ||
536 msg
.capability
== NULL
|| msg
.p2p_device_info
== NULL
)
539 buf
= wpabuf_alloc(ETH_ALEN
+ 1 + 1 + msg
.p2p_device_info_len
);
543 *dev_capab
= msg
.capability
[0];
544 os_memcpy(dev_addr
, msg
.p2p_device_addr
, ETH_ALEN
);
546 spos
= msg
.p2p_device_info
; /* P2P Device address */
548 /* P2P Client Info Descriptor */
549 /* Length to be set */
550 len_pos
= wpabuf_put(buf
, 1);
551 /* P2P Device address */
552 wpabuf_put_data(buf
, spos
, ETH_ALEN
);
553 /* P2P Interface address */
554 wpabuf_put_data(buf
, addr
, ETH_ALEN
);
555 /* Device Capability Bitmap */
556 wpabuf_put_u8(buf
, msg
.capability
[0]);
558 * Config Methods, Primary Device Type, Number of Secondary Device
559 * Types, Secondary Device Type List, Device Name copied from
562 wpabuf_put_data(buf
, spos
+ ETH_ALEN
,
563 msg
.p2p_device_info_len
- ETH_ALEN
);
565 *len_pos
= wpabuf_len(buf
) - 1;
572 static int p2p_group_remove_member(struct p2p_group
*group
, const u8
*addr
)
574 struct p2p_group_member
*m
, *prev
;
582 if (os_memcmp(m
->addr
, addr
, ETH_ALEN
) == 0)
592 prev
->next
= m
->next
;
594 group
->members
= m
->next
;
595 p2p_group_free_member(m
);
596 group
->num_members
--;
602 int p2p_group_notif_assoc(struct p2p_group
*group
, const u8
*addr
,
603 const u8
*ie
, size_t len
)
605 struct p2p_group_member
*m
;
610 p2p_add_device(group
->p2p
, addr
, 0, NULL
, 0, ie
, len
, 0);
612 m
= os_zalloc(sizeof(*m
));
615 os_memcpy(m
->addr
, addr
, ETH_ALEN
);
616 m
->p2p_ie
= ieee802_11_vendor_ie_concat(ie
, len
, P2P_IE_VENDOR_TYPE
);
618 m
->client_info
= p2p_build_client_info(addr
, m
->p2p_ie
,
622 #ifdef CONFIG_WIFI_DISPLAY
623 m
->wfd_ie
= ieee802_11_vendor_ie_concat(ie
, len
, WFD_IE_VENDOR_TYPE
);
624 #endif /* CONFIG_WIFI_DISPLAY */
626 p2p_group_remove_member(group
, addr
);
628 m
->next
= group
->members
;
630 group
->num_members
++;
631 p2p_dbg(group
->p2p
, "Add client " MACSTR
632 " to group (p2p=%d wfd=%d client_info=%d); num_members=%u/%u",
633 MAC2STR(addr
), m
->p2p_ie
? 1 : 0, m
->wfd_ie
? 1 : 0,
634 m
->client_info
? 1 : 0,
635 group
->num_members
, group
->cfg
->max_clients
);
636 if (group
->num_members
== group
->cfg
->max_clients
)
637 group
->beacon_update
= 1;
638 p2p_group_update_ies(group
);
639 if (group
->num_members
== 1)
640 group
->cfg
->idle_update(group
->cfg
->cb_ctx
, 0);
646 struct wpabuf
* p2p_group_assoc_resp_ie(struct p2p_group
*group
, u8 status
)
652 #ifdef CONFIG_WIFI_DISPLAY
654 extra
= wpabuf_len(group
->wfd_ie
);
655 #endif /* CONFIG_WIFI_DISPLAY */
657 if (group
->p2p
->vendor_elem
&&
658 group
->p2p
->vendor_elem
[VENDOR_ELEM_P2P_ASSOC_RESP
])
659 extra
+= wpabuf_len(group
->p2p
->vendor_elem
[VENDOR_ELEM_P2P_ASSOC_RESP
]);
662 * (Re)Association Response - P2P IE
663 * Status attribute (shall be present when association request is
665 * Extended Listen Timing (may be present)
667 resp
= wpabuf_alloc(20 + extra
);
671 #ifdef CONFIG_WIFI_DISPLAY
673 wpabuf_put_buf(resp
, group
->wfd_ie
);
674 #endif /* CONFIG_WIFI_DISPLAY */
676 if (group
->p2p
->vendor_elem
&&
677 group
->p2p
->vendor_elem
[VENDOR_ELEM_P2P_ASSOC_RESP
])
679 group
->p2p
->vendor_elem
[VENDOR_ELEM_P2P_ASSOC_RESP
]);
681 rlen
= p2p_buf_add_ie_hdr(resp
);
682 if (status
!= P2P_SC_SUCCESS
)
683 p2p_buf_add_status(resp
, status
);
684 p2p_buf_update_ie_hdr(resp
, rlen
);
690 void p2p_group_notif_disassoc(struct p2p_group
*group
, const u8
*addr
)
692 if (p2p_group_remove_member(group
, addr
)) {
693 p2p_dbg(group
->p2p
, "Remove client " MACSTR
694 " from group; num_members=%u/%u",
695 MAC2STR(addr
), group
->num_members
,
696 group
->cfg
->max_clients
);
697 if (group
->num_members
== group
->cfg
->max_clients
- 1)
698 group
->beacon_update
= 1;
699 p2p_group_update_ies(group
);
700 if (group
->num_members
== 0)
701 group
->cfg
->idle_update(group
->cfg
->cb_ctx
, 1);
707 * p2p_match_dev_type_member - Match client device type with requested type
709 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
710 * Returns: 1 on match, 0 on mismatch
712 * This function can be used to match the Requested Device Type attribute in
713 * WPS IE with the device types of a group member for deciding whether a GO
714 * should reply to a Probe Request frame.
716 static int p2p_match_dev_type_member(struct p2p_group_member
*m
,
720 struct wps_parse_attr attr
;
723 if (m
->client_info
== NULL
|| wps
== NULL
)
726 pos
= wpabuf_head(m
->client_info
);
727 end
= pos
+ wpabuf_len(m
->client_info
);
729 pos
+= 1 + 2 * ETH_ALEN
+ 1 + 2;
730 if (end
- pos
< WPS_DEV_TYPE_LEN
+ 1)
733 if (wps_parse_msg(wps
, &attr
))
734 return 1; /* assume no Requested Device Type attributes */
736 if (attr
.num_req_dev_type
== 0)
737 return 1; /* no Requested Device Type attributes -> match */
739 if (dev_type_list_match(pos
, attr
.req_dev_type
, attr
.num_req_dev_type
))
740 return 1; /* Match with client Primary Device Type */
742 pos
+= WPS_DEV_TYPE_LEN
;
744 if (end
- pos
< num_sec
* WPS_DEV_TYPE_LEN
)
746 while (num_sec
> 0) {
748 if (dev_type_list_match(pos
, attr
.req_dev_type
,
749 attr
.num_req_dev_type
))
750 return 1; /* Match with client Secondary Device Type */
751 pos
+= WPS_DEV_TYPE_LEN
;
754 /* No matching device type found */
759 int p2p_group_match_dev_type(struct p2p_group
*group
, struct wpabuf
*wps
)
761 struct p2p_group_member
*m
;
763 if (p2p_match_dev_type(group
->p2p
, wps
))
764 return 1; /* Match with own device type */
766 for (m
= group
->members
; m
; m
= m
->next
) {
767 if (p2p_match_dev_type_member(m
, wps
))
768 return 1; /* Match with group client device type */
771 /* No match with Requested Device Type */
776 int p2p_group_match_dev_id(struct p2p_group
*group
, struct wpabuf
*p2p
)
778 struct p2p_group_member
*m
;
779 struct p2p_message msg
;
781 os_memset(&msg
, 0, sizeof(msg
));
782 if (p2p_parse_p2p_ie(p2p
, &msg
))
783 return 1; /* Failed to parse - assume no filter on Device ID */
786 return 1; /* No filter on Device ID */
788 if (os_memcmp(msg
.device_id
, group
->p2p
->cfg
->dev_addr
, ETH_ALEN
) == 0)
789 return 1; /* Match with our P2P Device Address */
791 for (m
= group
->members
; m
; m
= m
->next
) {
792 if (os_memcmp(msg
.device_id
, m
->dev_addr
, ETH_ALEN
) == 0)
793 return 1; /* Match with group client P2P Device Address */
796 /* No match with Device ID */
801 void p2p_group_notif_formation_done(struct p2p_group
*group
)
805 group
->group_formation
= 0;
806 group
->beacon_update
= 1;
807 p2p_group_update_ies(group
);
811 int p2p_group_notif_noa(struct p2p_group
*group
, const u8
*noa
,
815 wpabuf_free(group
->noa
);
819 if (wpabuf_size(group
->noa
) >= noa_len
) {
820 group
->noa
->used
= 0;
821 wpabuf_put_data(group
->noa
, noa
, noa_len
);
823 wpabuf_free(group
->noa
);
829 group
->noa
= wpabuf_alloc_copy(noa
, noa_len
);
830 if (group
->noa
== NULL
)
835 group
->beacon_update
= 1;
836 p2p_group_update_ies(group
);
841 static struct p2p_group_member
* p2p_group_get_client(struct p2p_group
*group
,
844 struct p2p_group_member
*m
;
846 for (m
= group
->members
; m
; m
= m
->next
) {
847 if (os_memcmp(dev_id
, m
->dev_addr
, ETH_ALEN
) == 0)
855 const u8
* p2p_group_get_client_interface_addr(struct p2p_group
*group
,
858 struct p2p_group_member
*m
;
862 m
= p2p_group_get_client(group
, dev_addr
);
869 static struct p2p_group_member
* p2p_group_get_client_iface(
870 struct p2p_group
*group
, const u8
*interface_addr
)
872 struct p2p_group_member
*m
;
874 for (m
= group
->members
; m
; m
= m
->next
) {
875 if (os_memcmp(interface_addr
, m
->addr
, ETH_ALEN
) == 0)
883 const u8
* p2p_group_get_dev_addr(struct p2p_group
*group
, const u8
*addr
)
885 struct p2p_group_member
*m
;
889 m
= p2p_group_get_client_iface(group
, addr
);
890 if (m
&& !is_zero_ether_addr(m
->dev_addr
))
896 static struct wpabuf
* p2p_build_go_disc_req(void)
900 buf
= wpabuf_alloc(100);
904 p2p_buf_add_action_hdr(buf
, P2P_GO_DISC_REQ
, 0);
910 int p2p_group_go_discover(struct p2p_group
*group
, const u8
*dev_id
,
911 const u8
*searching_dev
, int rx_freq
)
913 struct p2p_group_member
*m
;
915 struct p2p_data
*p2p
= group
->p2p
;
918 m
= p2p_group_get_client(group
, dev_id
);
919 if (m
== NULL
|| m
->client_info
== NULL
) {
920 p2p_dbg(group
->p2p
, "Requested client was not in this group "
921 MACSTR
, MAC2STR(group
->cfg
->interface_addr
));
925 if (!(m
->dev_capab
& P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
)) {
926 p2p_dbg(group
->p2p
, "Requested client does not support client discoverability");
930 p2p_dbg(group
->p2p
, "Schedule GO Discoverability Request to be sent to "
931 MACSTR
, MAC2STR(dev_id
));
933 req
= p2p_build_go_disc_req();
937 /* TODO: Should really use group operating frequency here */
940 p2p
->pending_action_state
= P2P_PENDING_GO_DISC_REQ
;
941 if (p2p
->cfg
->send_action(p2p
->cfg
->cb_ctx
, freq
, m
->addr
,
942 group
->cfg
->interface_addr
,
943 group
->cfg
->interface_addr
,
944 wpabuf_head(req
), wpabuf_len(req
), 200) < 0)
946 p2p_dbg(p2p
, "Failed to send Action frame");
955 const u8
* p2p_group_get_interface_addr(struct p2p_group
*group
)
957 return group
->cfg
->interface_addr
;
961 u8
p2p_group_presence_req(struct p2p_group
*group
,
962 const u8
*client_interface_addr
,
963 const u8
*noa
, size_t noa_len
)
965 struct p2p_group_member
*m
;
969 m
= p2p_group_get_client_iface(group
, client_interface_addr
);
970 if (m
== NULL
|| m
->client_info
== NULL
) {
971 p2p_dbg(group
->p2p
, "Client was not in this group");
972 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
975 wpa_hexdump(MSG_DEBUG
, "P2P: Presence Request NoA", noa
, noa_len
);
977 if (group
->p2p
->cfg
->get_noa
)
978 curr_noa_len
= group
->p2p
->cfg
->get_noa(
979 group
->p2p
->cfg
->cb_ctx
, group
->cfg
->interface_addr
,
980 curr_noa
, sizeof(curr_noa
));
983 if (curr_noa_len
< 0)
984 p2p_dbg(group
->p2p
, "Failed to fetch current NoA");
985 else if (curr_noa_len
== 0)
986 p2p_dbg(group
->p2p
, "No NoA being advertized");
988 wpa_hexdump(MSG_DEBUG
, "P2P: Current NoA", curr_noa
,
991 /* TODO: properly process request and store copy */
992 if (curr_noa_len
> 0 || curr_noa_len
== -1)
993 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
995 return P2P_SC_SUCCESS
;
999 unsigned int p2p_get_group_num_members(struct p2p_group
*group
)
1004 return group
->num_members
;
1008 int p2p_client_limit_reached(struct p2p_group
*group
)
1010 if (!group
|| !group
->cfg
)
1013 return group
->num_members
>= group
->cfg
->max_clients
;
1017 const u8
* p2p_iterate_group_members(struct p2p_group
*group
, void **next
)
1019 struct p2p_group_member
*iter
= *next
;
1022 iter
= group
->members
;
1031 return iter
->dev_addr
;
1035 int p2p_group_is_client_connected(struct p2p_group
*group
, const u8
*dev_addr
)
1037 struct p2p_group_member
*m
;
1039 for (m
= group
->members
; m
; m
= m
->next
) {
1040 if (os_memcmp(m
->dev_addr
, dev_addr
, ETH_ALEN
) == 0)
1048 int p2p_group_is_group_id_match(struct p2p_group
*group
, const u8
*group_id
,
1049 size_t group_id_len
)
1051 if (group_id_len
!= ETH_ALEN
+ group
->cfg
->ssid_len
)
1053 if (os_memcmp(group_id
, group
->p2p
->cfg
->dev_addr
, ETH_ALEN
) != 0)
1055 return os_memcmp(group_id
+ ETH_ALEN
, group
->cfg
->ssid
,
1056 group
->cfg
->ssid_len
) == 0;
1060 void p2p_group_force_beacon_update_ies(struct p2p_group
*group
)
1062 group
->beacon_update
= 1;
1063 p2p_group_update_ies(group
);
1067 int p2p_group_get_freq(struct p2p_group
*group
)
1069 return group
->cfg
->freq
;
1073 const struct p2p_group_config
* p2p_group_get_config(struct p2p_group
*group
)
1079 void p2p_loop_on_all_groups(struct p2p_data
*p2p
,
1080 int (*group_callback
)(struct p2p_group
*group
,
1086 for (i
= 0; i
< p2p
->num_groups
; i
++) {
1087 if (!group_callback(p2p
->groups
[i
], user_data
))
1093 int p2p_group_get_common_freqs(struct p2p_group
*group
, int *common_freqs
,
1097 struct p2p_channels intersect
, res
;
1098 struct p2p_group_member
*m
;
1100 if (!group
|| !common_freqs
|| !num
)
1103 os_memset(&intersect
, 0, sizeof(intersect
));
1104 os_memset(&res
, 0, sizeof(res
));
1106 p2p_channels_union(&intersect
, &group
->p2p
->cfg
->channels
,
1109 p2p_channels_dump(group
->p2p
,
1110 "Group common freqs before iterating members",
1113 for (m
= group
->members
; m
; m
= m
->next
) {
1114 struct p2p_device
*dev
;
1116 dev
= p2p_get_device(group
->p2p
, m
->dev_addr
);
1117 if (!dev
|| dev
->channels
.reg_classes
== 0)
1120 p2p_channels_intersect(&intersect
, &dev
->channels
, &res
);
1124 p2p_channels_dump(group
->p2p
, "Group common channels", &intersect
);
1126 os_memset(common_freqs
, 0, *num
* sizeof(int));
1127 *num
= p2p_channels_to_freqs(&intersect
, common_freqs
, *num
);