blob: 633dd5c9b0d161cf642ad2e7c30bd80fe25e3636 [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 Shmidt61d9df32012-08-29 16:22:06 -0700405 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700407#ifdef CONFIG_WIFI_DISPLAY
408 if (group->wfd_ie)
409 extra += wpabuf_len(group->wfd_ie);
410#endif /* CONFIG_WIFI_DISPLAY */
411
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800412 p2p_subelems = wpabuf_alloc(500 + extra);
413 if (p2p_subelems == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 return NULL;
415
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700416#ifdef CONFIG_WIFI_DISPLAY
417 if (group->wfd_ie)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800418 wpabuf_put_buf(p2p_subelems, group->wfd_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700419#endif /* CONFIG_WIFI_DISPLAY */
420
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800421 p2p_group_add_common_ies(group, p2p_subelems);
422 p2p_group_add_noa(p2p_subelems, group->noa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423
424 /* P2P Device Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800425 p2p_buf_add_device_info(p2p_subelems, group->p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426
427 /* P2P Group Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800428 group_info = wpabuf_put(p2p_subelems, 0);
429 wpabuf_put_u8(p2p_subelems, P2P_ATTR_GROUP_INFO);
430 wpabuf_put_le16(p2p_subelems, 0); /* Length to be filled */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 for (m = group->members; m; m = m->next)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800432 p2p_client_info(p2p_subelems, m);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 WPA_PUT_LE16(group_info + 1,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800434 (u8 *) wpabuf_put(p2p_subelems, 0) - group_info - 3);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800436 ie = p2p_group_encaps_probe_resp(p2p_subelems);
437 wpabuf_free(p2p_subelems);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 return ie;
440}
441
442
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700443void p2p_group_update_ies(struct p2p_group *group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444{
445 struct wpabuf *beacon_ie;
446 struct wpabuf *probe_resp_ie;
447
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700448#ifdef CONFIG_WIFI_DISPLAY
449 wifi_display_group_update(group);
450#endif /* CONFIG_WIFI_DISPLAY */
451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 probe_resp_ie = p2p_group_build_probe_resp_ie(group);
453 if (probe_resp_ie == NULL)
454 return;
455 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Probe Response P2P IE",
456 probe_resp_ie);
457
458 if (group->beacon_update) {
459 beacon_ie = p2p_group_build_beacon_ie(group);
460 if (beacon_ie)
461 group->beacon_update = 0;
462 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: Update GO Beacon P2P IE",
463 beacon_ie);
464 } else
465 beacon_ie = NULL;
466
467 group->cfg->ie_update(group->cfg->cb_ctx, beacon_ie, probe_resp_ie);
468}
469
470
471/**
472 * p2p_build_client_info - Build P2P Client Info Descriptor
473 * @addr: MAC address of the peer device
474 * @p2p_ie: P2P IE from (Re)Association Request
475 * @dev_capab: Buffer for returning Device Capability
476 * @dev_addr: Buffer for returning P2P Device Address
477 * Returns: P2P Client Info Descriptor or %NULL on failure
478 *
479 * This function builds P2P Client Info Descriptor based on the information
480 * available from (Re)Association Request frame. Group owner can use this to
481 * build the P2P Group Info attribute for Probe Response frames.
482 */
483static struct wpabuf * p2p_build_client_info(const u8 *addr,
484 struct wpabuf *p2p_ie,
485 u8 *dev_capab, u8 *dev_addr)
486{
487 const u8 *spos;
488 struct p2p_message msg;
489 u8 *len_pos;
490 struct wpabuf *buf;
491
492 if (p2p_ie == NULL)
493 return NULL;
494
495 os_memset(&msg, 0, sizeof(msg));
496 if (p2p_parse_p2p_ie(p2p_ie, &msg) ||
497 msg.capability == NULL || msg.p2p_device_info == NULL)
498 return NULL;
499
500 buf = wpabuf_alloc(ETH_ALEN + 1 + 1 + msg.p2p_device_info_len);
501 if (buf == NULL)
502 return NULL;
503
504 *dev_capab = msg.capability[0];
505 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
506
507 spos = msg.p2p_device_info; /* P2P Device address */
508
509 /* P2P Client Info Descriptor */
510 /* Length to be set */
511 len_pos = wpabuf_put(buf, 1);
512 /* P2P Device address */
513 wpabuf_put_data(buf, spos, ETH_ALEN);
514 /* P2P Interface address */
515 wpabuf_put_data(buf, addr, ETH_ALEN);
516 /* Device Capability Bitmap */
517 wpabuf_put_u8(buf, msg.capability[0]);
518 /*
519 * Config Methods, Primary Device Type, Number of Secondary Device
520 * Types, Secondary Device Type List, Device Name copied from
521 * Device Info
522 */
523 wpabuf_put_data(buf, spos + ETH_ALEN,
524 msg.p2p_device_info_len - ETH_ALEN);
525
526 *len_pos = wpabuf_len(buf) - 1;
527
528
529 return buf;
530}
531
532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800533static int p2p_group_remove_member(struct p2p_group *group, const u8 *addr)
534{
535 struct p2p_group_member *m, *prev;
536
537 if (group == NULL)
538 return 0;
539
540 m = group->members;
541 prev = NULL;
542 while (m) {
543 if (os_memcmp(m->addr, addr, ETH_ALEN) == 0)
544 break;
545 prev = m;
546 m = m->next;
547 }
548
549 if (m == NULL)
550 return 0;
551
552 if (prev)
553 prev->next = m->next;
554 else
555 group->members = m->next;
556 p2p_group_free_member(m);
557 group->num_members--;
558
559 return 1;
560}
561
562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563int p2p_group_notif_assoc(struct p2p_group *group, const u8 *addr,
564 const u8 *ie, size_t len)
565{
566 struct p2p_group_member *m;
567
568 if (group == NULL)
569 return -1;
570
571 m = os_zalloc(sizeof(*m));
572 if (m == NULL)
573 return -1;
574 os_memcpy(m->addr, addr, ETH_ALEN);
575 m->p2p_ie = ieee802_11_vendor_ie_concat(ie, len, P2P_IE_VENDOR_TYPE);
576 if (m->p2p_ie) {
577 m->client_info = p2p_build_client_info(addr, m->p2p_ie,
578 &m->dev_capab,
579 m->dev_addr);
580 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700581#ifdef CONFIG_WIFI_DISPLAY
582 m->wfd_ie = ieee802_11_vendor_ie_concat(ie, len, WFD_IE_VENDOR_TYPE);
583#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800585 p2p_group_remove_member(group, addr);
586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587 m->next = group->members;
588 group->members = m;
589 group->num_members++;
590 wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Add client " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700591 " to group (p2p=%d wfd=%d client_info=%d); num_members=%u/%u",
592 MAC2STR(addr), m->p2p_ie ? 1 : 0, m->wfd_ie ? 1 : 0,
593 m->client_info ? 1 : 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594 group->num_members, group->cfg->max_clients);
595 if (group->num_members == group->cfg->max_clients)
596 group->beacon_update = 1;
597 p2p_group_update_ies(group);
598 if (group->num_members == 1)
599 group->cfg->idle_update(group->cfg->cb_ctx, 0);
600
601 return 0;
602}
603
604
605struct wpabuf * p2p_group_assoc_resp_ie(struct p2p_group *group, u8 status)
606{
607 struct wpabuf *resp;
608 u8 *rlen;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700609 size_t extra = 0;
610
611#ifdef CONFIG_WIFI_DISPLAY
612 if (group->wfd_ie)
613 extra = wpabuf_len(group->wfd_ie);
614#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615
616 /*
617 * (Re)Association Response - P2P IE
618 * Status attribute (shall be present when association request is
619 * denied)
620 * Extended Listen Timing (may be present)
621 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700622 resp = wpabuf_alloc(20 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623 if (resp == NULL)
624 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700625
626#ifdef CONFIG_WIFI_DISPLAY
627 if (group->wfd_ie)
628 wpabuf_put_buf(resp, group->wfd_ie);
629#endif /* CONFIG_WIFI_DISPLAY */
630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631 rlen = p2p_buf_add_ie_hdr(resp);
632 if (status != P2P_SC_SUCCESS)
633 p2p_buf_add_status(resp, status);
634 p2p_buf_update_ie_hdr(resp, rlen);
635
636 return resp;
637}
638
639
640void p2p_group_notif_disassoc(struct p2p_group *group, const u8 *addr)
641{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800642 if (p2p_group_remove_member(group, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643 wpa_msg(group->p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Remove "
644 "client " MACSTR " from group; num_members=%u/%u",
645 MAC2STR(addr), group->num_members,
646 group->cfg->max_clients);
647 if (group->num_members == group->cfg->max_clients - 1)
648 group->beacon_update = 1;
649 p2p_group_update_ies(group);
650 if (group->num_members == 0)
651 group->cfg->idle_update(group->cfg->cb_ctx, 1);
652 }
653}
654
655
656/**
657 * p2p_match_dev_type_member - Match client device type with requested type
658 * @m: Group member
659 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
660 * Returns: 1 on match, 0 on mismatch
661 *
662 * This function can be used to match the Requested Device Type attribute in
663 * WPS IE with the device types of a group member for deciding whether a GO
664 * should reply to a Probe Request frame.
665 */
666static int p2p_match_dev_type_member(struct p2p_group_member *m,
667 struct wpabuf *wps)
668{
669 const u8 *pos, *end;
670 struct wps_parse_attr attr;
671 u8 num_sec;
672
673 if (m->client_info == NULL || wps == NULL)
674 return 0;
675
676 pos = wpabuf_head(m->client_info);
677 end = pos + wpabuf_len(m->client_info);
678
679 pos += 1 + 2 * ETH_ALEN + 1 + 2;
680 if (end - pos < WPS_DEV_TYPE_LEN + 1)
681 return 0;
682
683 if (wps_parse_msg(wps, &attr))
684 return 1; /* assume no Requested Device Type attributes */
685
686 if (attr.num_req_dev_type == 0)
687 return 1; /* no Requested Device Type attributes -> match */
688
689 if (dev_type_list_match(pos, attr.req_dev_type, attr.num_req_dev_type))
690 return 1; /* Match with client Primary Device Type */
691
692 pos += WPS_DEV_TYPE_LEN;
693 num_sec = *pos++;
694 if (end - pos < num_sec * WPS_DEV_TYPE_LEN)
695 return 0;
696 while (num_sec > 0) {
697 num_sec--;
698 if (dev_type_list_match(pos, attr.req_dev_type,
699 attr.num_req_dev_type))
700 return 1; /* Match with client Secondary Device Type */
701 pos += WPS_DEV_TYPE_LEN;
702 }
703
704 /* No matching device type found */
705 return 0;
706}
707
708
709int p2p_group_match_dev_type(struct p2p_group *group, struct wpabuf *wps)
710{
711 struct p2p_group_member *m;
712
713 if (p2p_match_dev_type(group->p2p, wps))
714 return 1; /* Match with own device type */
715
716 for (m = group->members; m; m = m->next) {
717 if (p2p_match_dev_type_member(m, wps))
718 return 1; /* Match with group client device type */
719 }
720
721 /* No match with Requested Device Type */
722 return 0;
723}
724
725
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800726int p2p_group_match_dev_id(struct p2p_group *group, struct wpabuf *p2p)
727{
728 struct p2p_group_member *m;
729 struct p2p_message msg;
730
731 os_memset(&msg, 0, sizeof(msg));
732 if (p2p_parse_p2p_ie(p2p, &msg))
733 return 1; /* Failed to parse - assume no filter on Device ID */
734
735 if (!msg.device_id)
736 return 1; /* No filter on Device ID */
737
738 if (os_memcmp(msg.device_id, group->p2p->cfg->dev_addr, ETH_ALEN) == 0)
739 return 1; /* Match with our P2P Device Address */
740
741 for (m = group->members; m; m = m->next) {
742 if (os_memcmp(msg.device_id, m->dev_addr, ETH_ALEN) == 0)
743 return 1; /* Match with group client P2P Device Address */
744 }
745
746 /* No match with Device ID */
747 return 0;
748}
749
750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751void p2p_group_notif_formation_done(struct p2p_group *group)
752{
753 if (group == NULL)
754 return;
755 group->group_formation = 0;
756 group->beacon_update = 1;
757 p2p_group_update_ies(group);
758}
759
760
761int p2p_group_notif_noa(struct p2p_group *group, const u8 *noa,
762 size_t noa_len)
763{
764 if (noa == NULL) {
765 wpabuf_free(group->noa);
766 group->noa = NULL;
767 } else {
768 if (group->noa) {
769 if (wpabuf_size(group->noa) >= noa_len) {
Dmitry Shmidtfc41cad2011-09-28 13:29:53 -0700770 group->noa->used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 wpabuf_put_data(group->noa, noa, noa_len);
772 } else {
773 wpabuf_free(group->noa);
774 group->noa = NULL;
775 }
776 }
777
778 if (!group->noa) {
779 group->noa = wpabuf_alloc_copy(noa, noa_len);
780 if (group->noa == NULL)
781 return -1;
782 }
783 }
784
785 group->beacon_update = 1;
786 p2p_group_update_ies(group);
787 return 0;
788}
789
790
791static struct p2p_group_member * p2p_group_get_client(struct p2p_group *group,
792 const u8 *dev_id)
793{
794 struct p2p_group_member *m;
795
796 for (m = group->members; m; m = m->next) {
797 if (os_memcmp(dev_id, m->dev_addr, ETH_ALEN) == 0)
798 return m;
799 }
800
801 return NULL;
802}
803
804
805static struct p2p_group_member * p2p_group_get_client_iface(
806 struct p2p_group *group, const u8 *interface_addr)
807{
808 struct p2p_group_member *m;
809
810 for (m = group->members; m; m = m->next) {
811 if (os_memcmp(interface_addr, m->addr, ETH_ALEN) == 0)
812 return m;
813 }
814
815 return NULL;
816}
817
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800818
819const u8 * p2p_group_get_dev_addr(struct p2p_group *group, const u8 *addr)
Dmitry Shmidtdca39792011-09-06 11:17:33 -0700820{
821 struct p2p_group_member *m;
822
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800823 if (group == NULL)
Dmitry Shmidtdca39792011-09-06 11:17:33 -0700824 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800825 m = p2p_group_get_client_iface(group, addr);
826 if (m && !is_zero_ether_addr(m->dev_addr))
827 return m->dev_addr;
828 return NULL;
Dmitry Shmidtdca39792011-09-06 11:17:33 -0700829}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831
832static struct wpabuf * p2p_build_go_disc_req(void)
833{
834 struct wpabuf *buf;
835
836 buf = wpabuf_alloc(100);
837 if (buf == NULL)
838 return NULL;
839
840 p2p_buf_add_action_hdr(buf, P2P_GO_DISC_REQ, 0);
841
842 return buf;
843}
844
845
846int p2p_group_go_discover(struct p2p_group *group, const u8 *dev_id,
847 const u8 *searching_dev, int rx_freq)
848{
849 struct p2p_group_member *m;
850 struct wpabuf *req;
851 struct p2p_data *p2p = group->p2p;
852 int freq;
853
854 m = p2p_group_get_client(group, dev_id);
855 if (m == NULL || m->client_info == NULL) {
856 wpa_printf(MSG_DEBUG, "P2P: Requested client was not in this "
857 "group " MACSTR,
858 MAC2STR(group->cfg->interface_addr));
859 return -1;
860 }
861
862 if (!(m->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
863 wpa_printf(MSG_DEBUG, "P2P: Requested client does not support "
864 "client discoverability");
865 return -1;
866 }
867
868 wpa_printf(MSG_DEBUG, "P2P: Schedule GO Discoverability Request to be "
869 "sent to " MACSTR, MAC2STR(dev_id));
870
871 req = p2p_build_go_disc_req();
872 if (req == NULL)
873 return -1;
874
875 /* TODO: Should really use group operating frequency here */
876 freq = rx_freq;
877
878 p2p->pending_action_state = P2P_PENDING_GO_DISC_REQ;
879 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, m->addr,
880 group->cfg->interface_addr,
881 group->cfg->interface_addr,
882 wpabuf_head(req), wpabuf_len(req), 200) < 0)
883 {
884 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
885 "P2P: Failed to send Action frame");
886 }
887
888 wpabuf_free(req);
889
890 return 0;
891}
892
893
894const u8 * p2p_group_get_interface_addr(struct p2p_group *group)
895{
896 return group->cfg->interface_addr;
897}
898
899
900u8 p2p_group_presence_req(struct p2p_group *group,
901 const u8 *client_interface_addr,
902 const u8 *noa, size_t noa_len)
903{
904 struct p2p_group_member *m;
905 u8 curr_noa[50];
906 int curr_noa_len;
907
908 m = p2p_group_get_client_iface(group, client_interface_addr);
909 if (m == NULL || m->client_info == NULL) {
910 wpa_printf(MSG_DEBUG, "P2P: Client was not in this group");
911 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
912 }
913
914 wpa_hexdump(MSG_DEBUG, "P2P: Presence Request NoA", noa, noa_len);
915
916 if (group->p2p->cfg->get_noa)
917 curr_noa_len = group->p2p->cfg->get_noa(
918 group->p2p->cfg->cb_ctx, group->cfg->interface_addr,
919 curr_noa, sizeof(curr_noa));
920 else
921 curr_noa_len = -1;
922 if (curr_noa_len < 0)
923 wpa_printf(MSG_DEBUG, "P2P: Failed to fetch current NoA");
924 else if (curr_noa_len == 0)
925 wpa_printf(MSG_DEBUG, "P2P: No NoA being advertized");
926 else
927 wpa_hexdump(MSG_DEBUG, "P2P: Current NoA", curr_noa,
928 curr_noa_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 /* TODO: properly process request and store copy */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800931 if (curr_noa_len > 0 || curr_noa_len == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 return P2P_SC_SUCCESS;
935}
936
937
938unsigned int p2p_get_group_num_members(struct p2p_group *group)
939{
940 return group->num_members;
941}
942
943
944const u8 * p2p_iterate_group_members(struct p2p_group *group, void **next)
945{
946 struct p2p_group_member *iter = *next;
947
948 if (!iter)
949 iter = group->members;
950 else
951 iter = iter->next;
952
953 *next = iter;
954
955 if (!iter)
956 return NULL;
957
958 return iter->addr;
959}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800960
961
962int p2p_group_is_client_connected(struct p2p_group *group, const u8 *dev_addr)
963{
964 struct p2p_group_member *m;
965
966 for (m = group->members; m; m = m->next) {
967 if (os_memcmp(m->dev_addr, dev_addr, ETH_ALEN) == 0)
968 return 1;
969 }
970
971 return 0;
972}
Dmitry Shmidt04949592012-07-19 12:16:46 -0700973
974
975int p2p_group_is_group_id_match(struct p2p_group *group, const u8 *group_id,
976 size_t group_id_len)
977{
978 if (group_id_len != ETH_ALEN + group->cfg->ssid_len)
979 return 0;
980 if (os_memcmp(group_id, group->p2p->cfg->dev_addr, ETH_ALEN) != 0)
981 return 0;
982 return os_memcmp(group_id + ETH_ALEN, group->cfg->ssid,
983 group->cfg->ssid_len) == 0;
984}