blob: edb8d721b226e4a84af1157a2911b74353d50f6a [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Wi-Fi Direct - P2P group operations
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "common/ieee802_11_defs.h"
13#include "common/ieee802_11_common.h"
14#include "wps/wps_defs.h"
15#include "wps/wps_i.h"
16#include "p2p_i.h"
17#include "p2p.h"
18
19
20struct p2p_group_member {
21 struct p2p_group_member *next;
22 u8 addr[ETH_ALEN]; /* P2P Interface Address */
23 u8 dev_addr[ETH_ALEN]; /* P2P Device Address */
24 struct wpabuf *p2p_ie;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070025 struct wpabuf *wfd_ie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026 struct wpabuf *client_info;
27 u8 dev_capab;
28};
29
30/**
31 * struct p2p_group - Internal P2P module per-group data
32 */
33struct p2p_group {
34 struct p2p_data *p2p;
35 struct p2p_group_config *cfg;
36 struct p2p_group_member *members;
37 unsigned int num_members;
38 int group_formation;
39 int beacon_update;
40 struct wpabuf *noa;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070041 struct wpabuf *wfd_ie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042};
43
44
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045struct p2p_group * p2p_group_init(struct p2p_data *p2p,
46 struct p2p_group_config *config)
47{
48 struct p2p_group *group, **groups;
49
50 group = os_zalloc(sizeof(*group));
51 if (group == NULL)
52 return NULL;
53
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070054 groups = os_realloc_array(p2p->groups, p2p->num_groups + 1,
55 sizeof(struct p2p_group *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056 if (groups == NULL) {
57 os_free(group);
58 return NULL;
59 }
60 groups[p2p->num_groups++] = group;
61 p2p->groups = groups;
62
63 group->p2p = p2p;
64 group->cfg = config;
65 group->group_formation = 1;
66 group->beacon_update = 1;
67 p2p_group_update_ies(group);
68 group->cfg->idle_update(group->cfg->cb_ctx, 1);
69
70 return group;
71}
72
73
74static void p2p_group_free_member(struct p2p_group_member *m)
75{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070076 wpabuf_free(m->wfd_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077 wpabuf_free(m->p2p_ie);
78 wpabuf_free(m->client_info);
79 os_free(m);
80}
81
82
83static void p2p_group_free_members(struct p2p_group *group)
84{
85 struct p2p_group_member *m, *prev;
86 m = group->members;
87 group->members = NULL;
88 group->num_members = 0;
89 while (m) {
90 prev = m;
91 m = m->next;
92 p2p_group_free_member(prev);
93 }
94}
95
96
97void p2p_group_deinit(struct p2p_group *group)
98{
99 size_t g;
100 struct p2p_data *p2p;
101
102 if (group == NULL)
103 return;
104
105 p2p = group->p2p;
106
107 for (g = 0; g < p2p->num_groups; g++) {
108 if (p2p->groups[g] == group) {
109 while (g + 1 < p2p->num_groups) {
110 p2p->groups[g] = p2p->groups[g + 1];
111 g++;
112 }
113 p2p->num_groups--;
114 break;
115 }
116 }
117
118 p2p_group_free_members(group);
119 os_free(group->cfg);
120 wpabuf_free(group->noa);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121 wpabuf_free(group->wfd_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 os_free(group);
123}
124
125
126static void p2p_client_info(struct wpabuf *ie, struct p2p_group_member *m)
127{
128 if (m->client_info == NULL)
129 return;
130 if (wpabuf_tailroom(ie) < wpabuf_len(m->client_info) + 1)
131 return;
132 wpabuf_put_buf(ie, m->client_info);
133}
134
135
136static void p2p_group_add_common_ies(struct p2p_group *group,
137 struct wpabuf *ie)
138{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700139 u8 dev_capab = group->p2p->dev_capab, group_capab = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140
141 /* P2P Capability */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700142 dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800144 if (group->cfg->persistent_group) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800146 if (group->cfg->persistent_group == 2)
147 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
148 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149 if (group->p2p->cfg->p2p_intra_bss)
150 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
151 if (group->group_formation)
152 group_capab |= P2P_GROUP_CAPAB_GROUP_FORMATION;
153 if (group->p2p->cross_connect)
154 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
155 if (group->num_members >= group->cfg->max_clients)
156 group_capab |= P2P_GROUP_CAPAB_GROUP_LIMIT;
157 p2p_buf_add_capability(ie, dev_capab, group_capab);
158}
159
160
161static void p2p_group_add_noa(struct wpabuf *ie, struct wpabuf *noa)
162{
163 if (noa == NULL)
164 return;
165 /* Notice of Absence */
166 wpabuf_put_u8(ie, P2P_ATTR_NOTICE_OF_ABSENCE);
167 wpabuf_put_le16(ie, wpabuf_len(noa));
168 wpabuf_put_buf(ie, noa);
169}
170
171
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800172static struct wpabuf * p2p_group_encaps_probe_resp(struct wpabuf *subelems)
173{
174 struct wpabuf *ie;
175 const u8 *pos, *end;
176 size_t len;
177
178 if (subelems == NULL)
179 return NULL;
180
181 len = wpabuf_len(subelems) + 100;
182
183 ie = wpabuf_alloc(len);
184 if (ie == NULL)
185 return NULL;
186
187 pos = wpabuf_head(subelems);
188 end = pos + wpabuf_len(subelems);
189
190 while (end > pos) {
191 size_t frag_len = end - pos;
192 if (frag_len > 251)
193 frag_len = 251;
194 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
195 wpabuf_put_u8(ie, 4 + frag_len);
196 wpabuf_put_be32(ie, P2P_IE_VENDOR_TYPE);
197 wpabuf_put_data(ie, pos, frag_len);
198 pos += frag_len;
199 }
200
201 return ie;
202}
203
204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205static struct wpabuf * p2p_group_build_beacon_ie(struct p2p_group *group)
206{
207 struct wpabuf *ie;
208 u8 *len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700209 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700211#ifdef CONFIG_WIFI_DISPLAY
212 if (group->p2p->wfd_ie_beacon)
213 extra = wpabuf_len(group->p2p->wfd_ie_beacon);
214#endif /* CONFIG_WIFI_DISPLAY */
215
216 ie = wpabuf_alloc(257 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 if (ie == NULL)
218 return NULL;
219
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700220#ifdef CONFIG_WIFI_DISPLAY
221 if (group->p2p->wfd_ie_beacon)
222 wpabuf_put_buf(ie, group->p2p->wfd_ie_beacon);
223#endif /* CONFIG_WIFI_DISPLAY */
224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225 len = p2p_buf_add_ie_hdr(ie);
226 p2p_group_add_common_ies(group, ie);
227 p2p_buf_add_device_id(ie, group->p2p->cfg->dev_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 p2p_group_add_noa(ie, group->noa);
229 p2p_buf_update_ie_hdr(ie, len);
230
231 return ie;
232}
233
234
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700235#ifdef CONFIG_WIFI_DISPLAY
236
237struct wpabuf * p2p_group_get_wfd_ie(struct p2p_group *g)
238{
239 return g->wfd_ie;
240}
241
242
243struct wpabuf * wifi_display_encaps(struct wpabuf *subelems)
244{
245 struct wpabuf *ie;
246 const u8 *pos, *end;
247
248 if (subelems == NULL)
249 return NULL;
250
251 ie = wpabuf_alloc(wpabuf_len(subelems) + 100);
252 if (ie == NULL)
253 return NULL;
254
255 pos = wpabuf_head(subelems);
256 end = pos + wpabuf_len(subelems);
257
258 while (end > pos) {
259 size_t frag_len = end - pos;
260 if (frag_len > 251)
261 frag_len = 251;
262 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
263 wpabuf_put_u8(ie, 4 + frag_len);
264 wpabuf_put_be32(ie, WFD_IE_VENDOR_TYPE);
265 wpabuf_put_data(ie, pos, frag_len);
266 pos += frag_len;
267 }
268
269 return ie;
270}
271
272
273static int wifi_display_add_dev_info_descr(struct wpabuf *buf,
274 struct p2p_group_member *m)
275{
276 const u8 *pos, *end;
277 const u8 *dev_info = NULL;
278 const u8 *assoc_bssid = NULL;
279 const u8 *coupled_sink = NULL;
280 u8 zero_addr[ETH_ALEN];
281
282 if (m->wfd_ie == NULL)
283 return 0;
284
285 os_memset(zero_addr, 0, ETH_ALEN);
286 pos = wpabuf_head_u8(m->wfd_ie);
287 end = pos + wpabuf_len(m->wfd_ie);
288 while (pos + 1 < end) {
289 u8 id;
290 u16 len;
291
292 id = *pos++;
293 len = WPA_GET_BE16(pos);
294 pos += 2;
295 if (pos + len > end)
296 break;
297
298 switch (id) {
299 case WFD_SUBELEM_DEVICE_INFO:
300 if (len < 6)
301 break;
302 dev_info = pos;
303 break;
304 case WFD_SUBELEM_ASSOCIATED_BSSID:
305 if (len < ETH_ALEN)
306 break;
307 assoc_bssid = pos;
308 break;
309 case WFD_SUBELEM_COUPLED_SINK:
310 if (len < 1 + ETH_ALEN)
311 break;
312 coupled_sink = pos;
313 break;
314 }
315
316 pos += len;
317 }
318
319 if (dev_info == NULL)
320 return 0;
321
322 wpabuf_put_u8(buf, 23);
323 wpabuf_put_data(buf, m->dev_addr, ETH_ALEN);
324 if (assoc_bssid)
325 wpabuf_put_data(buf, assoc_bssid, ETH_ALEN);
326 else
327 wpabuf_put_data(buf, zero_addr, ETH_ALEN);
328 wpabuf_put_data(buf, dev_info, 2); /* WFD Device Info */
329 wpabuf_put_data(buf, dev_info + 4, 2); /* WFD Device Max Throughput */
330 if (coupled_sink) {
331 wpabuf_put_data(buf, coupled_sink, 1 + ETH_ALEN);
332 } else {
333 wpabuf_put_u8(buf, 0);
334 wpabuf_put_data(buf, zero_addr, ETH_ALEN);
335 }
336
337 return 1;
338}
339
340
341static struct wpabuf *
342wifi_display_build_go_ie(struct p2p_group *group)
343{
344 struct wpabuf *wfd_subelems, *wfd_ie;
345 struct p2p_group_member *m;
346 u8 *len;
347 unsigned int count = 0;
348
349 if (!group->p2p->wfd_ie_probe_resp)
350 return NULL;
351
352 wfd_subelems = wpabuf_alloc(wpabuf_len(group->p2p->wfd_ie_probe_resp) +
353 group->num_members * 24 + 100);
354 if (wfd_subelems == NULL)
355 return NULL;
356 if (group->p2p->wfd_dev_info)
357 wpabuf_put_buf(wfd_subelems, group->p2p->wfd_dev_info);
358 if (group->p2p->wfd_assoc_bssid)
359 wpabuf_put_buf(wfd_subelems,
360 group->p2p->wfd_assoc_bssid);
361 if (group->p2p->wfd_coupled_sink_info)
362 wpabuf_put_buf(wfd_subelems,
363 group->p2p->wfd_coupled_sink_info);
364
365 /* Build WFD Session Info */
366 wpabuf_put_u8(wfd_subelems, WFD_SUBELEM_SESSION_INFO);
367 len = wpabuf_put(wfd_subelems, 2);
368 m = group->members;
369 while (m) {
370 if (wifi_display_add_dev_info_descr(wfd_subelems, m))
371 count++;
372 m = m->next;
373 }
374
375 if (count == 0) {
376 /* No Wi-Fi Display clients - do not include subelement */
377 wfd_subelems->used -= 3;
378 } else {
379 WPA_PUT_BE16(len, (u8 *) wpabuf_put(wfd_subelems, 0) - len -
380 2);
381 wpa_printf(MSG_DEBUG, "WFD: WFD Session Info: %u descriptors",
382 count);
383 }
384
385 wfd_ie = wifi_display_encaps(wfd_subelems);
386 wpabuf_free(wfd_subelems);
387
388 return wfd_ie;
389}
390
391static void wifi_display_group_update(struct p2p_group *group)
392{
393 wpabuf_free(group->wfd_ie);
394 group->wfd_ie = wifi_display_build_go_ie(group);
395}
396
397#endif /* CONFIG_WIFI_DISPLAY */
398
399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400static struct wpabuf * p2p_group_build_probe_resp_ie(struct p2p_group *group)
401{
402 u8 *group_info;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800403 struct wpabuf *p2p_subelems, *ie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 struct p2p_group_member *m;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405
Jouni Malinen33077b12013-02-27 07:46:44 -0800406 p2p_subelems = wpabuf_alloc(500);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800407 if (p2p_subelems == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 return NULL;
409
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800410 p2p_group_add_common_ies(group, p2p_subelems);
411 p2p_group_add_noa(p2p_subelems, group->noa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412
413 /* P2P Device Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800414 p2p_buf_add_device_info(p2p_subelems, group->p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415
416 /* P2P Group Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800417 group_info = wpabuf_put(p2p_subelems, 0);
418 wpabuf_put_u8(p2p_subelems, P2P_ATTR_GROUP_INFO);
419 wpabuf_put_le16(p2p_subelems, 0); /* Length to be filled */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 for (m = group->members; m; m = m->next)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800421 p2p_client_info(p2p_subelems, m);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422 WPA_PUT_LE16(group_info + 1,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800423 (u8 *) wpabuf_put(p2p_subelems, 0) - group_info - 3);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800425 ie = p2p_group_encaps_probe_resp(p2p_subelems);
426 wpabuf_free(p2p_subelems);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700427
Jouni Malinen33077b12013-02-27 07:46:44 -0800428#ifdef CONFIG_WIFI_DISPLAY
429 if (group->wfd_ie) {
430 struct wpabuf *wfd = wpabuf_dup(group->wfd_ie);
431 ie = wpabuf_concat(wfd, ie);
432 }
433#endif /* CONFIG_WIFI_DISPLAY */
434
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 return ie;
436}
437
438
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700439void p2p_group_update_ies(struct p2p_group *group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440{
441 struct wpabuf *beacon_ie;
442 struct wpabuf *probe_resp_ie;
443
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700444#ifdef CONFIG_WIFI_DISPLAY
445 wifi_display_group_update(group);
446#endif /* CONFIG_WIFI_DISPLAY */
447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448 probe_resp_ie = p2p_group_build_probe_resp_ie(group);
449 if (probe_resp_ie == NULL)
450 return;
451 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Probe Response P2P IE",
452 probe_resp_ie);
453
454 if (group->beacon_update) {
455 beacon_ie = p2p_group_build_beacon_ie(group);
456 if (beacon_ie)
457 group->beacon_update = 0;
458 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Beacon P2P IE",
459 beacon_ie);
460 } else
461 beacon_ie = NULL;
462
463 group->cfg->ie_update(group->cfg->cb_ctx, beacon_ie, probe_resp_ie);
464}
465
466
467/**
468 * p2p_build_client_info - Build P2P Client Info Descriptor
469 * @addr: MAC address of the peer device
470 * @p2p_ie: P2P IE from (Re)Association Request
471 * @dev_capab: Buffer for returning Device Capability
472 * @dev_addr: Buffer for returning P2P Device Address
473 * Returns: P2P Client Info Descriptor or %NULL on failure
474 *
475 * This function builds P2P Client Info Descriptor based on the information
476 * available from (Re)Association Request frame. Group owner can use this to
477 * build the P2P Group Info attribute for Probe Response frames.
478 */
479static struct wpabuf * p2p_build_client_info(const u8 *addr,
480 struct wpabuf *p2p_ie,
481 u8 *dev_capab, u8 *dev_addr)
482{
483 const u8 *spos;
484 struct p2p_message msg;
485 u8 *len_pos;
486 struct wpabuf *buf;
487
488 if (p2p_ie == NULL)
489 return NULL;
490
491 os_memset(&msg, 0, sizeof(msg));
492 if (p2p_parse_p2p_ie(p2p_ie, &msg) ||
493 msg.capability == NULL || msg.p2p_device_info == NULL)
494 return NULL;
495
496 buf = wpabuf_alloc(ETH_ALEN + 1 + 1 + msg.p2p_device_info_len);
497 if (buf == NULL)
498 return NULL;
499
500 *dev_capab = msg.capability[0];
501 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
502
503 spos = msg.p2p_device_info; /* P2P Device address */
504
505 /* P2P Client Info Descriptor */
506 /* Length to be set */
507 len_pos = wpabuf_put(buf, 1);
508 /* P2P Device address */
509 wpabuf_put_data(buf, spos, ETH_ALEN);
510 /* P2P Interface address */
511 wpabuf_put_data(buf, addr, ETH_ALEN);
512 /* Device Capability Bitmap */
513 wpabuf_put_u8(buf, msg.capability[0]);
514 /*
515 * Config Methods, Primary Device Type, Number of Secondary Device
516 * Types, Secondary Device Type List, Device Name copied from
517 * Device Info
518 */
519 wpabuf_put_data(buf, spos + ETH_ALEN,
520 msg.p2p_device_info_len - ETH_ALEN);
521
522 *len_pos = wpabuf_len(buf) - 1;
523
524
525 return buf;
526}
527
528
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800529static int p2p_group_remove_member(struct p2p_group *group, const u8 *addr)
530{
531 struct p2p_group_member *m, *prev;
532
533 if (group == NULL)
534 return 0;
535
536 m = group->members;
537 prev = NULL;
538 while (m) {
539 if (os_memcmp(m->addr, addr, ETH_ALEN) == 0)
540 break;
541 prev = m;
542 m = m->next;
543 }
544
545 if (m == NULL)
546 return 0;
547
548 if (prev)
549 prev->next = m->next;
550 else
551 group->members = m->next;
552 p2p_group_free_member(m);
553 group->num_members--;
554
555 return 1;
556}
557
558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559int p2p_group_notif_assoc(struct p2p_group *group, const u8 *addr,
560 const u8 *ie, size_t len)
561{
562 struct p2p_group_member *m;
563
564 if (group == NULL)
565 return -1;
566
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700567 p2p_add_device(group->p2p, addr, 0, NULL, 0, ie, len, 0);
568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569 m = os_zalloc(sizeof(*m));
570 if (m == NULL)
571 return -1;
572 os_memcpy(m->addr, addr, ETH_ALEN);
573 m->p2p_ie = ieee802_11_vendor_ie_concat(ie, len, P2P_IE_VENDOR_TYPE);
574 if (m->p2p_ie) {
575 m->client_info = p2p_build_client_info(addr, m->p2p_ie,
576 &m->dev_capab,
577 m->dev_addr);
578 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700579#ifdef CONFIG_WIFI_DISPLAY
580 m->wfd_ie = ieee802_11_vendor_ie_concat(ie, len, WFD_IE_VENDOR_TYPE);
581#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800583 p2p_group_remove_member(group, addr);
584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585 m->next = group->members;
586 group->members = m;
587 group->num_members++;
588 wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Add client " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700589 " to group (p2p=%d wfd=%d client_info=%d); num_members=%u/%u",
590 MAC2STR(addr), m->p2p_ie ? 1 : 0, m->wfd_ie ? 1 : 0,
591 m->client_info ? 1 : 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592 group->num_members, group->cfg->max_clients);
593 if (group->num_members == group->cfg->max_clients)
594 group->beacon_update = 1;
595 p2p_group_update_ies(group);
596 if (group->num_members == 1)
597 group->cfg->idle_update(group->cfg->cb_ctx, 0);
598
599 return 0;
600}
601
602
603struct wpabuf * p2p_group_assoc_resp_ie(struct p2p_group *group, u8 status)
604{
605 struct wpabuf *resp;
606 u8 *rlen;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700607 size_t extra = 0;
608
609#ifdef CONFIG_WIFI_DISPLAY
610 if (group->wfd_ie)
611 extra = wpabuf_len(group->wfd_ie);
612#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613
614 /*
615 * (Re)Association Response - P2P IE
616 * Status attribute (shall be present when association request is
617 * denied)
618 * Extended Listen Timing (may be present)
619 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700620 resp = wpabuf_alloc(20 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621 if (resp == NULL)
622 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700623
624#ifdef CONFIG_WIFI_DISPLAY
625 if (group->wfd_ie)
626 wpabuf_put_buf(resp, group->wfd_ie);
627#endif /* CONFIG_WIFI_DISPLAY */
628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629 rlen = p2p_buf_add_ie_hdr(resp);
630 if (status != P2P_SC_SUCCESS)
631 p2p_buf_add_status(resp, status);
632 p2p_buf_update_ie_hdr(resp, rlen);
633
634 return resp;
635}
636
637
638void p2p_group_notif_disassoc(struct p2p_group *group, const u8 *addr)
639{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800640 if (p2p_group_remove_member(group, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Remove "
642 "client " MACSTR " from group; num_members=%u/%u",
643 MAC2STR(addr), group->num_members,
644 group->cfg->max_clients);
645 if (group->num_members == group->cfg->max_clients - 1)
646 group->beacon_update = 1;
647 p2p_group_update_ies(group);
648 if (group->num_members == 0)
649 group->cfg->idle_update(group->cfg->cb_ctx, 1);
650 }
651}
652
653
654/**
655 * p2p_match_dev_type_member - Match client device type with requested type
656 * @m: Group member
657 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
658 * Returns: 1 on match, 0 on mismatch
659 *
660 * This function can be used to match the Requested Device Type attribute in
661 * WPS IE with the device types of a group member for deciding whether a GO
662 * should reply to a Probe Request frame.
663 */
664static int p2p_match_dev_type_member(struct p2p_group_member *m,
665 struct wpabuf *wps)
666{
667 const u8 *pos, *end;
668 struct wps_parse_attr attr;
669 u8 num_sec;
670
671 if (m->client_info == NULL || wps == NULL)
672 return 0;
673
674 pos = wpabuf_head(m->client_info);
675 end = pos + wpabuf_len(m->client_info);
676
677 pos += 1 + 2 * ETH_ALEN + 1 + 2;
678 if (end - pos < WPS_DEV_TYPE_LEN + 1)
679 return 0;
680
681 if (wps_parse_msg(wps, &attr))
682 return 1; /* assume no Requested Device Type attributes */
683
684 if (attr.num_req_dev_type == 0)
685 return 1; /* no Requested Device Type attributes -> match */
686
687 if (dev_type_list_match(pos, attr.req_dev_type, attr.num_req_dev_type))
688 return 1; /* Match with client Primary Device Type */
689
690 pos += WPS_DEV_TYPE_LEN;
691 num_sec = *pos++;
692 if (end - pos < num_sec * WPS_DEV_TYPE_LEN)
693 return 0;
694 while (num_sec > 0) {
695 num_sec--;
696 if (dev_type_list_match(pos, attr.req_dev_type,
697 attr.num_req_dev_type))
698 return 1; /* Match with client Secondary Device Type */
699 pos += WPS_DEV_TYPE_LEN;
700 }
701
702 /* No matching device type found */
703 return 0;
704}
705
706
707int p2p_group_match_dev_type(struct p2p_group *group, struct wpabuf *wps)
708{
709 struct p2p_group_member *m;
710
711 if (p2p_match_dev_type(group->p2p, wps))
712 return 1; /* Match with own device type */
713
714 for (m = group->members; m; m = m->next) {
715 if (p2p_match_dev_type_member(m, wps))
716 return 1; /* Match with group client device type */
717 }
718
719 /* No match with Requested Device Type */
720 return 0;
721}
722
723
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800724int p2p_group_match_dev_id(struct p2p_group *group, struct wpabuf *p2p)
725{
726 struct p2p_group_member *m;
727 struct p2p_message msg;
728
729 os_memset(&msg, 0, sizeof(msg));
730 if (p2p_parse_p2p_ie(p2p, &msg))
731 return 1; /* Failed to parse - assume no filter on Device ID */
732
733 if (!msg.device_id)
734 return 1; /* No filter on Device ID */
735
736 if (os_memcmp(msg.device_id, group->p2p->cfg->dev_addr, ETH_ALEN) == 0)
737 return 1; /* Match with our P2P Device Address */
738
739 for (m = group->members; m; m = m->next) {
740 if (os_memcmp(msg.device_id, m->dev_addr, ETH_ALEN) == 0)
741 return 1; /* Match with group client P2P Device Address */
742 }
743
744 /* No match with Device ID */
745 return 0;
746}
747
748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749void p2p_group_notif_formation_done(struct p2p_group *group)
750{
751 if (group == NULL)
752 return;
753 group->group_formation = 0;
754 group->beacon_update = 1;
755 p2p_group_update_ies(group);
756}
757
758
759int p2p_group_notif_noa(struct p2p_group *group, const u8 *noa,
760 size_t noa_len)
761{
762 if (noa == NULL) {
763 wpabuf_free(group->noa);
764 group->noa = NULL;
765 } else {
766 if (group->noa) {
767 if (wpabuf_size(group->noa) >= noa_len) {
Dmitry Shmidtfc41cad2011-09-28 13:29:53 -0700768 group->noa->used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769 wpabuf_put_data(group->noa, noa, noa_len);
770 } else {
771 wpabuf_free(group->noa);
772 group->noa = NULL;
773 }
774 }
775
776 if (!group->noa) {
777 group->noa = wpabuf_alloc_copy(noa, noa_len);
778 if (group->noa == NULL)
779 return -1;
780 }
781 }
782
783 group->beacon_update = 1;
784 p2p_group_update_ies(group);
785 return 0;
786}
787
788
789static struct p2p_group_member * p2p_group_get_client(struct p2p_group *group,
790 const u8 *dev_id)
791{
792 struct p2p_group_member *m;
793
794 for (m = group->members; m; m = m->next) {
795 if (os_memcmp(dev_id, m->dev_addr, ETH_ALEN) == 0)
796 return m;
797 }
798
799 return NULL;
800}
801
802
803static struct p2p_group_member * p2p_group_get_client_iface(
804 struct p2p_group *group, const u8 *interface_addr)
805{
806 struct p2p_group_member *m;
807
808 for (m = group->members; m; m = m->next) {
809 if (os_memcmp(interface_addr, m->addr, ETH_ALEN) == 0)
810 return m;
811 }
812
813 return NULL;
814}
815
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800816
817const u8 * p2p_group_get_dev_addr(struct p2p_group *group, const u8 *addr)
Dmitry Shmidtdca39792011-09-06 11:17:33 -0700818{
819 struct p2p_group_member *m;
820
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800821 if (group == NULL)
Dmitry Shmidtdca39792011-09-06 11:17:33 -0700822 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800823 m = p2p_group_get_client_iface(group, addr);
824 if (m && !is_zero_ether_addr(m->dev_addr))
825 return m->dev_addr;
826 return NULL;
Dmitry Shmidtdca39792011-09-06 11:17:33 -0700827}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829
830static struct wpabuf * p2p_build_go_disc_req(void)
831{
832 struct wpabuf *buf;
833
834 buf = wpabuf_alloc(100);
835 if (buf == NULL)
836 return NULL;
837
838 p2p_buf_add_action_hdr(buf, P2P_GO_DISC_REQ, 0);
839
840 return buf;
841}
842
843
844int p2p_group_go_discover(struct p2p_group *group, const u8 *dev_id,
845 const u8 *searching_dev, int rx_freq)
846{
847 struct p2p_group_member *m;
848 struct wpabuf *req;
849 struct p2p_data *p2p = group->p2p;
850 int freq;
851
852 m = p2p_group_get_client(group, dev_id);
853 if (m == NULL || m->client_info == NULL) {
854 wpa_printf(MSG_DEBUG, "P2P: Requested client was not in this "
855 "group " MACSTR,
856 MAC2STR(group->cfg->interface_addr));
857 return -1;
858 }
859
860 if (!(m->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
861 wpa_printf(MSG_DEBUG, "P2P: Requested client does not support "
862 "client discoverability");
863 return -1;
864 }
865
866 wpa_printf(MSG_DEBUG, "P2P: Schedule GO Discoverability Request to be "
867 "sent to " MACSTR, MAC2STR(dev_id));
868
869 req = p2p_build_go_disc_req();
870 if (req == NULL)
871 return -1;
872
873 /* TODO: Should really use group operating frequency here */
874 freq = rx_freq;
875
876 p2p->pending_action_state = P2P_PENDING_GO_DISC_REQ;
877 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, m->addr,
878 group->cfg->interface_addr,
879 group->cfg->interface_addr,
880 wpabuf_head(req), wpabuf_len(req), 200) < 0)
881 {
882 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
883 "P2P: Failed to send Action frame");
884 }
885
886 wpabuf_free(req);
887
888 return 0;
889}
890
891
892const u8 * p2p_group_get_interface_addr(struct p2p_group *group)
893{
894 return group->cfg->interface_addr;
895}
896
897
898u8 p2p_group_presence_req(struct p2p_group *group,
899 const u8 *client_interface_addr,
900 const u8 *noa, size_t noa_len)
901{
902 struct p2p_group_member *m;
903 u8 curr_noa[50];
904 int curr_noa_len;
905
906 m = p2p_group_get_client_iface(group, client_interface_addr);
907 if (m == NULL || m->client_info == NULL) {
908 wpa_printf(MSG_DEBUG, "P2P: Client was not in this group");
909 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
910 }
911
912 wpa_hexdump(MSG_DEBUG, "P2P: Presence Request NoA", noa, noa_len);
913
914 if (group->p2p->cfg->get_noa)
915 curr_noa_len = group->p2p->cfg->get_noa(
916 group->p2p->cfg->cb_ctx, group->cfg->interface_addr,
917 curr_noa, sizeof(curr_noa));
918 else
919 curr_noa_len = -1;
920 if (curr_noa_len < 0)
921 wpa_printf(MSG_DEBUG, "P2P: Failed to fetch current NoA");
922 else if (curr_noa_len == 0)
923 wpa_printf(MSG_DEBUG, "P2P: No NoA being advertized");
924 else
925 wpa_hexdump(MSG_DEBUG, "P2P: Current NoA", curr_noa,
926 curr_noa_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 /* TODO: properly process request and store copy */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800929 if (curr_noa_len > 0 || curr_noa_len == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 return P2P_SC_SUCCESS;
933}
934
935
936unsigned int p2p_get_group_num_members(struct p2p_group *group)
937{
938 return group->num_members;
939}
940
941
942const u8 * p2p_iterate_group_members(struct p2p_group *group, void **next)
943{
944 struct p2p_group_member *iter = *next;
945
946 if (!iter)
947 iter = group->members;
948 else
949 iter = iter->next;
950
951 *next = iter;
952
953 if (!iter)
954 return NULL;
955
956 return iter->addr;
957}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800958
959
960int p2p_group_is_client_connected(struct p2p_group *group, const u8 *dev_addr)
961{
962 struct p2p_group_member *m;
963
964 for (m = group->members; m; m = m->next) {
965 if (os_memcmp(m->dev_addr, dev_addr, ETH_ALEN) == 0)
966 return 1;
967 }
968
969 return 0;
970}
Dmitry Shmidt04949592012-07-19 12:16:46 -0700971
972
973int p2p_group_is_group_id_match(struct p2p_group *group, const u8 *group_id,
974 size_t group_id_len)
975{
976 if (group_id_len != ETH_ALEN + group->cfg->ssid_len)
977 return 0;
978 if (os_memcmp(group_id, group->p2p->cfg->dev_addr, ETH_ALEN) != 0)
979 return 0;
980 return os_memcmp(group_id + ETH_ALEN, group->cfg->ssid,
981 group->cfg->ssid_len) == 0;
982}