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