]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/p2p/p2p_group.c
e25baaab059e6db0ac8d912b87caba5b7883e3d9
[thirdparty/hostap.git] / src / p2p / p2p_group.c
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
15 #include "includes.h"
16
17 #include "common.h"
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"
22 #include "p2p_i.h"
23 #include "p2p.h"
24
25
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;
32 u8 dev_capab;
33 };
34
35 /**
36 * struct p2p_group - Internal P2P module per-group data
37 */
38 struct p2p_group {
39 struct p2p_data *p2p;
40 struct p2p_group_config *cfg;
41 struct p2p_group_member *members;
42 unsigned int num_members;
43 int group_formation;
44 int beacon_update;
45 struct wpabuf *noa;
46 };
47
48
49 static void p2p_group_update_ies(struct p2p_group *group);
50
51
52 struct p2p_group * p2p_group_init(struct p2p_data *p2p,
53 struct p2p_group_config *config)
54 {
55 struct p2p_group *group, **groups;
56
57 group = os_zalloc(sizeof(*group));
58 if (group == NULL)
59 return NULL;
60
61 groups = os_realloc(p2p->groups, (p2p->num_groups + 1) *
62 sizeof(struct p2p_group *));
63 if (groups == NULL) {
64 os_free(group);
65 return NULL;
66 }
67 groups[p2p->num_groups++] = group;
68 p2p->groups = groups;
69
70 group->p2p = p2p;
71 group->cfg = config;
72 group->group_formation = 1;
73 group->beacon_update = 1;
74 p2p_group_update_ies(group);
75 group->cfg->idle_update(group->cfg->cb_ctx, 1);
76
77 return group;
78 }
79
80
81 static void p2p_group_free_member(struct p2p_group_member *m)
82 {
83 wpabuf_free(m->p2p_ie);
84 wpabuf_free(m->client_info);
85 os_free(m);
86 }
87
88
89 static void p2p_group_free_members(struct p2p_group *group)
90 {
91 struct p2p_group_member *m, *prev;
92 m = group->members;
93 group->members = NULL;
94 group->num_members = 0;
95 while (m) {
96 prev = m;
97 m = m->next;
98 p2p_group_free_member(prev);
99 }
100 }
101
102
103 void p2p_group_deinit(struct p2p_group *group)
104 {
105 size_t g;
106 struct p2p_data *p2p;
107
108 if (group == NULL)
109 return;
110
111 p2p = group->p2p;
112
113 for (g = 0; g < p2p->num_groups; g++) {
114 if (p2p->groups[g] == group) {
115 while (g + 1 < p2p->num_groups) {
116 p2p->groups[g] = p2p->groups[g + 1];
117 g++;
118 }
119 p2p->num_groups--;
120 break;
121 }
122 }
123
124 p2p_group_free_members(group);
125 os_free(group->cfg);
126 wpabuf_free(group->noa);
127 os_free(group);
128 }
129
130
131 static void p2p_client_info(struct wpabuf *ie, struct p2p_group_member *m)
132 {
133 if (m->client_info == NULL)
134 return;
135 if (wpabuf_tailroom(ie) < wpabuf_len(m->client_info) + 1)
136 return;
137 wpabuf_put_buf(ie, m->client_info);
138 }
139
140
141 static void p2p_group_add_common_ies(struct p2p_group *group,
142 struct wpabuf *ie)
143 {
144 u8 dev_capab = 0, group_capab = 0;
145
146 /* P2P Capability */
147 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
148 dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
149 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
150 if (group->cfg->persistent_group) {
151 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
152 if (group->cfg->persistent_group == 2)
153 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
154 }
155 if (group->p2p->cfg->p2p_intra_bss)
156 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
157 if (group->group_formation)
158 group_capab |= P2P_GROUP_CAPAB_GROUP_FORMATION;
159 if (group->p2p->cross_connect)
160 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
161 if (group->num_members >= group->cfg->max_clients)
162 group_capab |= P2P_GROUP_CAPAB_GROUP_LIMIT;
163 p2p_buf_add_capability(ie, dev_capab, group_capab);
164 }
165
166
167 static void p2p_group_add_noa(struct wpabuf *ie, struct wpabuf *noa)
168 {
169 if (noa == NULL)
170 return;
171 /* Notice of Absence */
172 wpabuf_put_u8(ie, P2P_ATTR_NOTICE_OF_ABSENCE);
173 wpabuf_put_le16(ie, wpabuf_len(noa));
174 wpabuf_put_buf(ie, noa);
175 }
176
177
178 static struct wpabuf * p2p_group_build_beacon_ie(struct p2p_group *group)
179 {
180 struct wpabuf *ie;
181 u8 *len;
182
183 ie = wpabuf_alloc(257);
184 if (ie == NULL)
185 return NULL;
186
187 len = p2p_buf_add_ie_hdr(ie);
188 p2p_group_add_common_ies(group, ie);
189 p2p_buf_add_device_id(ie, group->p2p->cfg->dev_addr);
190 p2p_group_add_noa(ie, group->noa);
191 p2p_buf_update_ie_hdr(ie, len);
192
193 return ie;
194 }
195
196
197 static struct wpabuf * p2p_group_build_probe_resp_ie(struct p2p_group *group)
198 {
199 u8 *group_info;
200 struct wpabuf *ie;
201 struct p2p_group_member *m;
202 u8 *len;
203
204 ie = wpabuf_alloc(257);
205 if (ie == NULL)
206 return NULL;
207
208 len = p2p_buf_add_ie_hdr(ie);
209
210 p2p_group_add_common_ies(group, ie);
211 p2p_group_add_noa(ie, group->noa);
212
213 /* P2P Device Info */
214 p2p_buf_add_device_info(ie, group->p2p, NULL);
215
216 /* P2P Group Info */
217 group_info = wpabuf_put(ie, 0);
218 wpabuf_put_u8(ie, P2P_ATTR_GROUP_INFO);
219 wpabuf_put_le16(ie, 0); /* Length to be filled */
220 for (m = group->members; m; m = m->next)
221 p2p_client_info(ie, m);
222 WPA_PUT_LE16(group_info + 1,
223 (u8 *) wpabuf_put(ie, 0) - group_info - 3);
224
225 p2p_buf_update_ie_hdr(ie, len);
226 return ie;
227 }
228
229
230 static void p2p_group_update_ies(struct p2p_group *group)
231 {
232 struct wpabuf *beacon_ie;
233 struct wpabuf *probe_resp_ie;
234
235 probe_resp_ie = p2p_group_build_probe_resp_ie(group);
236 if (probe_resp_ie == NULL)
237 return;
238 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Probe Response P2P IE",
239 probe_resp_ie);
240
241 if (group->beacon_update) {
242 beacon_ie = p2p_group_build_beacon_ie(group);
243 if (beacon_ie)
244 group->beacon_update = 0;
245 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Beacon P2P IE",
246 beacon_ie);
247 } else
248 beacon_ie = NULL;
249
250 group->cfg->ie_update(group->cfg->cb_ctx, beacon_ie, probe_resp_ie);
251 }
252
253
254 /**
255 * p2p_build_client_info - Build P2P Client Info Descriptor
256 * @addr: MAC address of the peer device
257 * @p2p_ie: P2P IE from (Re)Association Request
258 * @dev_capab: Buffer for returning Device Capability
259 * @dev_addr: Buffer for returning P2P Device Address
260 * Returns: P2P Client Info Descriptor or %NULL on failure
261 *
262 * This function builds P2P Client Info Descriptor based on the information
263 * available from (Re)Association Request frame. Group owner can use this to
264 * build the P2P Group Info attribute for Probe Response frames.
265 */
266 static struct wpabuf * p2p_build_client_info(const u8 *addr,
267 struct wpabuf *p2p_ie,
268 u8 *dev_capab, u8 *dev_addr)
269 {
270 const u8 *spos;
271 struct p2p_message msg;
272 u8 *len_pos;
273 struct wpabuf *buf;
274
275 if (p2p_ie == NULL)
276 return NULL;
277
278 os_memset(&msg, 0, sizeof(msg));
279 if (p2p_parse_p2p_ie(p2p_ie, &msg) ||
280 msg.capability == NULL || msg.p2p_device_info == NULL)
281 return NULL;
282
283 buf = wpabuf_alloc(ETH_ALEN + 1 + 1 + msg.p2p_device_info_len);
284 if (buf == NULL)
285 return NULL;
286
287 *dev_capab = msg.capability[0];
288 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
289
290 spos = msg.p2p_device_info; /* P2P Device address */
291
292 /* P2P Client Info Descriptor */
293 /* Length to be set */
294 len_pos = wpabuf_put(buf, 1);
295 /* P2P Device address */
296 wpabuf_put_data(buf, spos, ETH_ALEN);
297 /* P2P Interface address */
298 wpabuf_put_data(buf, addr, ETH_ALEN);
299 /* Device Capability Bitmap */
300 wpabuf_put_u8(buf, msg.capability[0]);
301 /*
302 * Config Methods, Primary Device Type, Number of Secondary Device
303 * Types, Secondary Device Type List, Device Name copied from
304 * Device Info
305 */
306 wpabuf_put_data(buf, spos + ETH_ALEN,
307 msg.p2p_device_info_len - ETH_ALEN);
308
309 *len_pos = wpabuf_len(buf) - 1;
310
311
312 return buf;
313 }
314
315
316 static int p2p_group_remove_member(struct p2p_group *group, const u8 *addr)
317 {
318 struct p2p_group_member *m, *prev;
319
320 if (group == NULL)
321 return 0;
322
323 m = group->members;
324 prev = NULL;
325 while (m) {
326 if (os_memcmp(m->addr, addr, ETH_ALEN) == 0)
327 break;
328 prev = m;
329 m = m->next;
330 }
331
332 if (m == NULL)
333 return 0;
334
335 if (prev)
336 prev->next = m->next;
337 else
338 group->members = m->next;
339 p2p_group_free_member(m);
340 group->num_members--;
341
342 return 1;
343 }
344
345
346 int p2p_group_notif_assoc(struct p2p_group *group, const u8 *addr,
347 const u8 *ie, size_t len)
348 {
349 struct p2p_group_member *m;
350
351 if (group == NULL)
352 return -1;
353
354 m = os_zalloc(sizeof(*m));
355 if (m == NULL)
356 return -1;
357 os_memcpy(m->addr, addr, ETH_ALEN);
358 m->p2p_ie = ieee802_11_vendor_ie_concat(ie, len, P2P_IE_VENDOR_TYPE);
359 if (m->p2p_ie) {
360 m->client_info = p2p_build_client_info(addr, m->p2p_ie,
361 &m->dev_capab,
362 m->dev_addr);
363 }
364
365 p2p_group_remove_member(group, addr);
366
367 m->next = group->members;
368 group->members = m;
369 group->num_members++;
370 wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Add client " MACSTR
371 " to group (p2p=%d client_info=%d); num_members=%u/%u",
372 MAC2STR(addr), m->p2p_ie ? 1 : 0, m->client_info ? 1 : 0,
373 group->num_members, group->cfg->max_clients);
374 if (group->num_members == group->cfg->max_clients)
375 group->beacon_update = 1;
376 p2p_group_update_ies(group);
377 if (group->num_members == 1)
378 group->cfg->idle_update(group->cfg->cb_ctx, 0);
379
380 return 0;
381 }
382
383
384 struct wpabuf * p2p_group_assoc_resp_ie(struct p2p_group *group, u8 status)
385 {
386 struct wpabuf *resp;
387 u8 *rlen;
388
389 /*
390 * (Re)Association Response - P2P IE
391 * Status attribute (shall be present when association request is
392 * denied)
393 * Extended Listen Timing (may be present)
394 */
395 resp = wpabuf_alloc(20);
396 if (resp == NULL)
397 return NULL;
398 rlen = p2p_buf_add_ie_hdr(resp);
399 if (status != P2P_SC_SUCCESS)
400 p2p_buf_add_status(resp, status);
401 p2p_buf_update_ie_hdr(resp, rlen);
402
403 return resp;
404 }
405
406
407 void p2p_group_notif_disassoc(struct p2p_group *group, const u8 *addr)
408 {
409 if (p2p_group_remove_member(group, addr)) {
410 wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Remove "
411 "client " MACSTR " from group; num_members=%u/%u",
412 MAC2STR(addr), group->num_members,
413 group->cfg->max_clients);
414 if (group->num_members == group->cfg->max_clients - 1)
415 group->beacon_update = 1;
416 p2p_group_update_ies(group);
417 if (group->num_members == 0)
418 group->cfg->idle_update(group->cfg->cb_ctx, 1);
419 }
420 }
421
422
423 /**
424 * p2p_match_dev_type_member - Match client device type with requested type
425 * @m: Group member
426 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
427 * Returns: 1 on match, 0 on mismatch
428 *
429 * This function can be used to match the Requested Device Type attribute in
430 * WPS IE with the device types of a group member for deciding whether a GO
431 * should reply to a Probe Request frame.
432 */
433 static int p2p_match_dev_type_member(struct p2p_group_member *m,
434 struct wpabuf *wps)
435 {
436 const u8 *pos, *end;
437 struct wps_parse_attr attr;
438 u8 num_sec;
439
440 if (m->client_info == NULL || wps == NULL)
441 return 0;
442
443 pos = wpabuf_head(m->client_info);
444 end = pos + wpabuf_len(m->client_info);
445
446 pos += 1 + 2 * ETH_ALEN + 1 + 2;
447 if (end - pos < WPS_DEV_TYPE_LEN + 1)
448 return 0;
449
450 if (wps_parse_msg(wps, &attr))
451 return 1; /* assume no Requested Device Type attributes */
452
453 if (attr.num_req_dev_type == 0)
454 return 1; /* no Requested Device Type attributes -> match */
455
456 if (dev_type_list_match(pos, attr.req_dev_type, attr.num_req_dev_type))
457 return 1; /* Match with client Primary Device Type */
458
459 pos += WPS_DEV_TYPE_LEN;
460 num_sec = *pos++;
461 if (end - pos < num_sec * WPS_DEV_TYPE_LEN)
462 return 0;
463 while (num_sec > 0) {
464 num_sec--;
465 if (dev_type_list_match(pos, attr.req_dev_type,
466 attr.num_req_dev_type))
467 return 1; /* Match with client Secondary Device Type */
468 pos += WPS_DEV_TYPE_LEN;
469 }
470
471 /* No matching device type found */
472 return 0;
473 }
474
475
476 int p2p_group_match_dev_type(struct p2p_group *group, struct wpabuf *wps)
477 {
478 struct p2p_group_member *m;
479
480 if (p2p_match_dev_type(group->p2p, wps))
481 return 1; /* Match with own device type */
482
483 for (m = group->members; m; m = m->next) {
484 if (p2p_match_dev_type_member(m, wps))
485 return 1; /* Match with group client device type */
486 }
487
488 /* No match with Requested Device Type */
489 return 0;
490 }
491
492
493 int p2p_group_match_dev_id(struct p2p_group *group, struct wpabuf *p2p)
494 {
495 struct p2p_group_member *m;
496 struct p2p_message msg;
497
498 os_memset(&msg, 0, sizeof(msg));
499 if (p2p_parse_p2p_ie(p2p, &msg))
500 return 1; /* Failed to parse - assume no filter on Device ID */
501
502 if (!msg.device_id)
503 return 1; /* No filter on Device ID */
504
505 if (os_memcmp(msg.device_id, group->p2p->cfg->dev_addr, ETH_ALEN) == 0)
506 return 1; /* Match with our P2P Device Address */
507
508 for (m = group->members; m; m = m->next) {
509 if (os_memcmp(msg.device_id, m->dev_addr, ETH_ALEN) == 0)
510 return 1; /* Match with group client P2P Device Address */
511 }
512
513 /* No match with Device ID */
514 return 0;
515 }
516
517
518 void p2p_group_notif_formation_done(struct p2p_group *group)
519 {
520 if (group == NULL)
521 return;
522 group->group_formation = 0;
523 group->beacon_update = 1;
524 p2p_group_update_ies(group);
525 }
526
527
528 int p2p_group_notif_noa(struct p2p_group *group, const u8 *noa,
529 size_t noa_len)
530 {
531 if (noa == NULL) {
532 wpabuf_free(group->noa);
533 group->noa = NULL;
534 } else {
535 if (group->noa) {
536 if (wpabuf_size(group->noa) >= noa_len) {
537 group->noa->used = 0;
538 wpabuf_put_data(group->noa, noa, noa_len);
539 } else {
540 wpabuf_free(group->noa);
541 group->noa = NULL;
542 }
543 }
544
545 if (!group->noa) {
546 group->noa = wpabuf_alloc_copy(noa, noa_len);
547 if (group->noa == NULL)
548 return -1;
549 }
550 }
551
552 group->beacon_update = 1;
553 p2p_group_update_ies(group);
554 return 0;
555 }
556
557
558 static struct p2p_group_member * p2p_group_get_client(struct p2p_group *group,
559 const u8 *dev_id)
560 {
561 struct p2p_group_member *m;
562
563 for (m = group->members; m; m = m->next) {
564 if (os_memcmp(dev_id, m->dev_addr, ETH_ALEN) == 0)
565 return m;
566 }
567
568 return NULL;
569 }
570
571
572 static struct p2p_group_member * p2p_group_get_client_iface(
573 struct p2p_group *group, const u8 *interface_addr)
574 {
575 struct p2p_group_member *m;
576
577 for (m = group->members; m; m = m->next) {
578 if (os_memcmp(interface_addr, m->addr, ETH_ALEN) == 0)
579 return m;
580 }
581
582 return NULL;
583 }
584
585
586 const u8 * p2p_group_get_dev_addr(struct p2p_group *group, const u8 *addr)
587 {
588 struct p2p_group_member *m;
589
590 if (group == NULL)
591 return NULL;
592 m = p2p_group_get_client_iface(group, addr);
593 if (m && !is_zero_ether_addr(m->dev_addr))
594 return m->dev_addr;
595 return NULL;
596 }
597
598
599 static struct wpabuf * p2p_build_go_disc_req(void)
600 {
601 struct wpabuf *buf;
602
603 buf = wpabuf_alloc(100);
604 if (buf == NULL)
605 return NULL;
606
607 p2p_buf_add_action_hdr(buf, P2P_GO_DISC_REQ, 0);
608
609 return buf;
610 }
611
612
613 int p2p_group_go_discover(struct p2p_group *group, const u8 *dev_id,
614 const u8 *searching_dev, int rx_freq)
615 {
616 struct p2p_group_member *m;
617 struct wpabuf *req;
618 struct p2p_data *p2p = group->p2p;
619 int freq;
620
621 m = p2p_group_get_client(group, dev_id);
622 if (m == NULL || m->client_info == NULL) {
623 wpa_printf(MSG_DEBUG, "P2P: Requested client was not in this "
624 "group " MACSTR,
625 MAC2STR(group->cfg->interface_addr));
626 return -1;
627 }
628
629 if (!(m->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
630 wpa_printf(MSG_DEBUG, "P2P: Requested client does not support "
631 "client discoverability");
632 return -1;
633 }
634
635 wpa_printf(MSG_DEBUG, "P2P: Schedule GO Discoverability Request to be "
636 "sent to " MACSTR, MAC2STR(dev_id));
637
638 req = p2p_build_go_disc_req();
639 if (req == NULL)
640 return -1;
641
642 /* TODO: Should really use group operating frequency here */
643 freq = rx_freq;
644
645 p2p->pending_action_state = P2P_PENDING_GO_DISC_REQ;
646 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, m->addr,
647 group->cfg->interface_addr,
648 group->cfg->interface_addr,
649 wpabuf_head(req), wpabuf_len(req), 200) < 0)
650 {
651 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
652 "P2P: Failed to send Action frame");
653 }
654
655 wpabuf_free(req);
656
657 return 0;
658 }
659
660
661 const u8 * p2p_group_get_interface_addr(struct p2p_group *group)
662 {
663 return group->cfg->interface_addr;
664 }
665
666
667 u8 p2p_group_presence_req(struct p2p_group *group,
668 const u8 *client_interface_addr,
669 const u8 *noa, size_t noa_len)
670 {
671 struct p2p_group_member *m;
672 u8 curr_noa[50];
673 int curr_noa_len;
674
675 m = p2p_group_get_client_iface(group, client_interface_addr);
676 if (m == NULL || m->client_info == NULL) {
677 wpa_printf(MSG_DEBUG, "P2P: Client was not in this group");
678 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
679 }
680
681 wpa_hexdump(MSG_DEBUG, "P2P: Presence Request NoA", noa, noa_len);
682
683 if (group->p2p->cfg->get_noa)
684 curr_noa_len = group->p2p->cfg->get_noa(
685 group->p2p->cfg->cb_ctx, group->cfg->interface_addr,
686 curr_noa, sizeof(curr_noa));
687 else
688 curr_noa_len = -1;
689 if (curr_noa_len < 0)
690 wpa_printf(MSG_DEBUG, "P2P: Failed to fetch current NoA");
691 else if (curr_noa_len == 0)
692 wpa_printf(MSG_DEBUG, "P2P: No NoA being advertized");
693 else
694 wpa_hexdump(MSG_DEBUG, "P2P: Current NoA", curr_noa,
695 curr_noa_len);
696
697 /* TODO: properly process request and store copy */
698 if (curr_noa_len > 0 || curr_noa_len == -1)
699 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
700
701 return P2P_SC_SUCCESS;
702 }
703
704
705 unsigned int p2p_get_group_num_members(struct p2p_group *group)
706 {
707 return group->num_members;
708 }
709
710
711 const u8 * p2p_iterate_group_members(struct p2p_group *group, void **next)
712 {
713 struct p2p_group_member *iter = *next;
714
715 if (!iter)
716 iter = group->members;
717 else
718 iter = iter->next;
719
720 *next = iter;
721
722 if (!iter)
723 return NULL;
724
725 return iter->addr;
726 }
727
728
729 int p2p_group_is_client_connected(struct p2p_group *group, const u8 *dev_addr)
730 {
731 struct p2p_group_member *m;
732
733 for (m = group->members; m; m = m->next) {
734 if (os_memcmp(m->dev_addr, dev_addr, ETH_ALEN) == 0)
735 return 1;
736 }
737
738 return 0;
739 }