automake build system
[mech_eap.orig] / src / p2p / p2p_parse.c
1 /*
2  * P2P - IE parser
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_i.h"
21 #include "p2p_i.h"
22
23
24 static int p2p_parse_attribute(u8 id, const u8 *data, u16 len,
25                                struct p2p_message *msg)
26 {
27         const u8 *pos;
28         size_t i, nlen;
29         char devtype[WPS_DEV_TYPE_BUFSIZE];
30
31         switch (id) {
32         case P2P_ATTR_CAPABILITY:
33                 if (len < 2) {
34                         wpa_printf(MSG_DEBUG, "P2P: Too short Capability "
35                                    "attribute (length %d)", len);
36                         return -1;
37                 }
38                 msg->capability = data;
39                 wpa_printf(MSG_DEBUG, "P2P: * Device Capability %02x "
40                            "Group Capability %02x",
41                            data[0], data[1]);
42                 break;
43         case P2P_ATTR_DEVICE_ID:
44                 if (len < ETH_ALEN) {
45                         wpa_printf(MSG_DEBUG, "P2P: Too short Device ID "
46                                    "attribute (length %d)", len);
47                         return -1;
48                 }
49                 msg->device_id = data;
50                 wpa_printf(MSG_DEBUG, "P2P: * Device ID " MACSTR,
51                            MAC2STR(msg->device_id));
52                 break;
53         case P2P_ATTR_GROUP_OWNER_INTENT:
54                 if (len < 1) {
55                         wpa_printf(MSG_DEBUG, "P2P: Too short GO Intent "
56                                    "attribute (length %d)", len);
57                         return -1;
58                 }
59                 msg->go_intent = data;
60                 wpa_printf(MSG_DEBUG, "P2P: * GO Intent: Intent %u "
61                            "Tie breaker %u", data[0] >> 1, data[0] & 0x01);
62                 break;
63         case P2P_ATTR_STATUS:
64                 if (len < 1) {
65                         wpa_printf(MSG_DEBUG, "P2P: Too short Status "
66                                    "attribute (length %d)", len);
67                         return -1;
68                 }
69                 msg->status = data;
70                 wpa_printf(MSG_DEBUG, "P2P: * Status: %d", data[0]);
71                 break;
72         case P2P_ATTR_LISTEN_CHANNEL:
73                 if (len == 0) {
74                         wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: Ignore "
75                                    "null channel");
76                         break;
77                 }
78                 if (len < 5) {
79                         wpa_printf(MSG_DEBUG, "P2P: Too short Listen Channel "
80                                    "attribute (length %d)", len);
81                         return -1;
82                 }
83                 msg->listen_channel = data;
84                 wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: "
85                            "Country %c%c(0x%02x) Regulatory "
86                            "Class %d Channel Number %d", data[0], data[1],
87                            data[2], data[3], data[4]);
88                 break;
89         case P2P_ATTR_OPERATING_CHANNEL:
90                 if (len == 0) {
91                         wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: "
92                                    "Ignore null channel");
93                         break;
94                 }
95                 if (len < 5) {
96                         wpa_printf(MSG_DEBUG, "P2P: Too short Operating "
97                                    "Channel attribute (length %d)", len);
98                         return -1;
99                 }
100                 msg->operating_channel = data;
101                 wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: "
102                            "Country %c%c(0x%02x) Regulatory "
103                            "Class %d Channel Number %d", data[0], data[1],
104                            data[2], data[3], data[4]);
105                 break;
106         case P2P_ATTR_CHANNEL_LIST:
107                 if (len < 3) {
108                         wpa_printf(MSG_DEBUG, "P2P: Too short Channel List "
109                                    "attribute (length %d)", len);
110                         return -1;
111                 }
112                 msg->channel_list = data;
113                 msg->channel_list_len = len;
114                 wpa_printf(MSG_DEBUG, "P2P: * Channel List: Country String "
115                            "'%c%c(0x%02x)'", data[0], data[1], data[2]);
116                 wpa_hexdump(MSG_MSGDUMP, "P2P: Channel List",
117                             msg->channel_list, msg->channel_list_len);
118                 break;
119         case P2P_ATTR_GROUP_INFO:
120                 msg->group_info = data;
121                 msg->group_info_len = len;
122                 wpa_printf(MSG_DEBUG, "P2P: * Group Info");
123                 break;
124         case P2P_ATTR_DEVICE_INFO:
125                 if (len < ETH_ALEN + 2 + 8 + 1) {
126                         wpa_printf(MSG_DEBUG, "P2P: Too short Device Info "
127                                    "attribute (length %d)", len);
128                         return -1;
129                 }
130                 msg->p2p_device_info = data;
131                 msg->p2p_device_info_len = len;
132                 pos = data;
133                 msg->p2p_device_addr = pos;
134                 pos += ETH_ALEN;
135                 msg->config_methods = WPA_GET_BE16(pos);
136                 pos += 2;
137                 msg->pri_dev_type = pos;
138                 pos += 8;
139                 msg->num_sec_dev_types = *pos++;
140                 if (msg->num_sec_dev_types * 8 > data + len - pos) {
141                         wpa_printf(MSG_DEBUG, "P2P: Device Info underflow");
142                         return -1;
143                 }
144                 pos += msg->num_sec_dev_types * 8;
145                 if (data + len - pos < 4) {
146                         wpa_printf(MSG_DEBUG, "P2P: Invalid Device Name "
147                                    "length %d", (int) (data + len - pos));
148                         return -1;
149                 }
150                 if (WPA_GET_BE16(pos) != ATTR_DEV_NAME) {
151                         wpa_hexdump(MSG_DEBUG, "P2P: Unexpected Device Name "
152                                     "header", pos, 4);
153                         return -1;
154                 }
155                 pos += 2;
156                 nlen = WPA_GET_BE16(pos);
157                 pos += 2;
158                 if (data + len - pos < (int) nlen || nlen > 32) {
159                         wpa_printf(MSG_DEBUG, "P2P: Invalid Device Name "
160                                    "length %d (buf len %d)", (int) nlen,
161                                    (int) (data + len - pos));
162                         return -1;
163                 }
164                 os_memcpy(msg->device_name, pos, nlen);
165                 msg->device_name[nlen] = '\0';
166                 for (i = 0; i < nlen; i++) {
167                         if (msg->device_name[i] == '\0')
168                                 break;
169                         if (msg->device_name[i] < 32)
170                                 msg->device_name[i] = '_';
171                 }
172                 wpa_printf(MSG_DEBUG, "P2P: * Device Info: addr " MACSTR
173                            " primary device type %s device name '%s' "
174                            "config methods 0x%x",
175                            MAC2STR(msg->p2p_device_addr),
176                            wps_dev_type_bin2str(msg->pri_dev_type, devtype,
177                                                 sizeof(devtype)),
178                            msg->device_name, msg->config_methods);
179                 break;
180         case P2P_ATTR_CONFIGURATION_TIMEOUT:
181                 if (len < 2) {
182                         wpa_printf(MSG_DEBUG, "P2P: Too short Configuration "
183                                    "Timeout attribute (length %d)", len);
184                         return -1;
185                 }
186                 msg->config_timeout = data;
187                 wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout");
188                 break;
189         case P2P_ATTR_INTENDED_INTERFACE_ADDR:
190                 if (len < ETH_ALEN) {
191                         wpa_printf(MSG_DEBUG, "P2P: Too short Intended P2P "
192                                    "Interface Address attribute (length %d)",
193                                    len);
194                         return -1;
195                 }
196                 msg->intended_addr = data;
197                 wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address: "
198                            MACSTR, MAC2STR(msg->intended_addr));
199                 break;
200         case P2P_ATTR_GROUP_BSSID:
201                 if (len < ETH_ALEN) {
202                         wpa_printf(MSG_DEBUG, "P2P: Too short P2P Group BSSID "
203                                    "attribute (length %d)", len);
204                         return -1;
205                 }
206                 msg->group_bssid = data;
207                 wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID: " MACSTR,
208                            MAC2STR(msg->group_bssid));
209                 break;
210         case P2P_ATTR_GROUP_ID:
211                 if (len < ETH_ALEN || len > ETH_ALEN + 32) {
212                         wpa_printf(MSG_DEBUG, "P2P: Invalid P2P Group ID "
213                                    "attribute length %d", len);
214                         return -1;
215                 }
216                 msg->group_id = data;
217                 msg->group_id_len = len;
218                 wpa_printf(MSG_DEBUG, "P2P: * P2P Group ID: Device Address "
219                            MACSTR, MAC2STR(msg->group_id));
220                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: * P2P Group ID: SSID",
221                                   msg->group_id + ETH_ALEN,
222                                   msg->group_id_len - ETH_ALEN);
223                 break;
224         case P2P_ATTR_INVITATION_FLAGS:
225                 if (len < 1) {
226                         wpa_printf(MSG_DEBUG, "P2P: Too short Invitation "
227                                    "Flag attribute (length %d)", len);
228                         return -1;
229                 }
230                 msg->invitation_flags = data;
231                 wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x",
232                            data[0]);
233                 break;
234         case P2P_ATTR_MANAGEABILITY:
235                 if (len < 1) {
236                         wpa_printf(MSG_DEBUG, "P2P: Too short Manageability "
237                                    "attribute (length %d)", len);
238                         return -1;
239                 }
240                 msg->manageability = data;
241                 wpa_printf(MSG_DEBUG, "P2P: * Manageability: bitmap 0x%x",
242                            data[0]);
243                 break;
244         case P2P_ATTR_NOTICE_OF_ABSENCE:
245                 if (len < 2) {
246                         wpa_printf(MSG_DEBUG, "P2P: Too short Notice of "
247                                    "Absence attribute (length %d)", len);
248                         return -1;
249                 }
250                 msg->noa = data;
251                 msg->noa_len = len;
252                 wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
253                 break;
254         case P2P_ATTR_EXT_LISTEN_TIMING:
255                 if (len < 4) {
256                         wpa_printf(MSG_DEBUG, "P2P: Too short Extended Listen "
257                                    "Timing attribute (length %d)", len);
258                         return -1;
259                 }
260                 msg->ext_listen_timing = data;
261                 wpa_printf(MSG_DEBUG, "P2P: * Extended Listen Timing "
262                            "(period %u msec  interval %u msec)",
263                            WPA_GET_LE16(msg->ext_listen_timing),
264                            WPA_GET_LE16(msg->ext_listen_timing + 2));
265                 break;
266         case P2P_ATTR_MINOR_REASON_CODE:
267                 if (len < 1) {
268                         wpa_printf(MSG_DEBUG, "P2P: Too short Minor Reason "
269                                    "Code attribute (length %d)", len);
270                         return -1;
271                 }
272                 msg->minor_reason_code = data;
273                 wpa_printf(MSG_DEBUG, "P2P: * Minor Reason Code: %u",
274                            *msg->minor_reason_code);
275                 break;
276         default:
277                 wpa_printf(MSG_DEBUG, "P2P: Skipped unknown attribute %d "
278                            "(length %d)", id, len);
279                 break;
280         }
281
282         return 0;
283 }
284
285
286 /**
287  * p2p_parse_p2p_ie - Parse P2P IE
288  * @buf: Concatenated P2P IE(s) payload
289  * @msg: Buffer for returning parsed attributes
290  * Returns: 0 on success, -1 on failure
291  *
292  * Note: Caller is responsible for clearing the msg data structure before
293  * calling this function.
294  */
295 int p2p_parse_p2p_ie(const struct wpabuf *buf, struct p2p_message *msg)
296 {
297         const u8 *pos = wpabuf_head_u8(buf);
298         const u8 *end = pos + wpabuf_len(buf);
299
300         wpa_printf(MSG_DEBUG, "P2P: Parsing P2P IE");
301
302         while (pos < end) {
303                 u16 attr_len;
304                 if (pos + 2 >= end) {
305                         wpa_printf(MSG_DEBUG, "P2P: Invalid P2P attribute");
306                         return -1;
307                 }
308                 attr_len = WPA_GET_LE16(pos + 1);
309                 wpa_printf(MSG_DEBUG, "P2P: Attribute %d length %u",
310                            pos[0], attr_len);
311                 if (pos + 3 + attr_len > end) {
312                         wpa_printf(MSG_DEBUG, "P2P: Attribute underflow "
313                                    "(len=%u left=%d)",
314                                    attr_len, (int) (end - pos - 3));
315                         wpa_hexdump(MSG_MSGDUMP, "P2P: Data", pos, end - pos);
316                         return -1;
317                 }
318                 if (p2p_parse_attribute(pos[0], pos + 3, attr_len, msg))
319                         return -1;
320                 pos += 3 + attr_len;
321         }
322
323         return 0;
324 }
325
326
327 static int p2p_parse_wps_ie(const struct wpabuf *buf, struct p2p_message *msg)
328 {
329         struct wps_parse_attr attr;
330
331         wpa_printf(MSG_DEBUG, "P2P: Parsing WPS IE");
332         if (wps_parse_msg(buf, &attr))
333                 return -1;
334         if (attr.dev_name && attr.dev_name_len < sizeof(msg->device_name) &&
335             !msg->device_name[0])
336                 os_memcpy(msg->device_name, attr.dev_name, attr.dev_name_len);
337         if (attr.config_methods) {
338                 msg->wps_config_methods =
339                         WPA_GET_BE16(attr.config_methods);
340                 wpa_printf(MSG_DEBUG, "P2P: Config Methods (WPS): 0x%x",
341                            msg->wps_config_methods);
342         }
343         if (attr.dev_password_id) {
344                 msg->dev_password_id = WPA_GET_BE16(attr.dev_password_id);
345                 wpa_printf(MSG_DEBUG, "P2P: Device Password ID: %d",
346                            msg->dev_password_id);
347         }
348         if (attr.primary_dev_type) {
349                 char devtype[WPS_DEV_TYPE_BUFSIZE];
350                 msg->wps_pri_dev_type = attr.primary_dev_type;
351                 wpa_printf(MSG_DEBUG, "P2P: Primary Device Type (WPS): %s",
352                            wps_dev_type_bin2str(msg->wps_pri_dev_type, devtype,
353                                                 sizeof(devtype)));
354         }
355
356         return 0;
357 }
358
359
360 /**
361  * p2p_parse_ies - Parse P2P message IEs (both WPS and P2P IE)
362  * @data: IEs from the message
363  * @len: Length of data buffer in octets
364  * @msg: Buffer for returning parsed attributes
365  * Returns: 0 on success, -1 on failure
366  *
367  * Note: Caller is responsible for clearing the msg data structure before
368  * calling this function.
369  *
370  * Note: Caller must free temporary memory allocations by calling
371  * p2p_parse_free() when the parsed data is not needed anymore.
372  */
373 int p2p_parse_ies(const u8 *data, size_t len, struct p2p_message *msg)
374 {
375         struct ieee802_11_elems elems;
376
377         ieee802_11_parse_elems(data, len, &elems, 0);
378         if (elems.ds_params && elems.ds_params_len >= 1)
379                 msg->ds_params = elems.ds_params;
380         if (elems.ssid)
381                 msg->ssid = elems.ssid - 2;
382
383         msg->wps_attributes = ieee802_11_vendor_ie_concat(data, len,
384                                                           WPS_DEV_OUI_WFA);
385         if (msg->wps_attributes &&
386             p2p_parse_wps_ie(msg->wps_attributes, msg)) {
387                 p2p_parse_free(msg);
388                 return -1;
389         }
390
391         msg->p2p_attributes = ieee802_11_vendor_ie_concat(data, len,
392                                                           P2P_IE_VENDOR_TYPE);
393         if (msg->p2p_attributes &&
394             p2p_parse_p2p_ie(msg->p2p_attributes, msg)) {
395                 wpa_printf(MSG_DEBUG, "P2P: Failed to parse P2P IE data");
396                 if (msg->p2p_attributes)
397                         wpa_hexdump_buf(MSG_MSGDUMP, "P2P: P2P IE data",
398                                         msg->p2p_attributes);
399                 p2p_parse_free(msg);
400                 return -1;
401         }
402
403         return 0;
404 }
405
406
407 /**
408  * p2p_parse - Parse a P2P Action frame contents
409  * @data: Action frame payload after Category and Code fields
410  * @len: Length of data buffer in octets
411  * @msg: Buffer for returning parsed attributes
412  * Returns: 0 on success, -1 on failure
413  *
414  * Note: Caller must free temporary memory allocations by calling
415  * p2p_parse_free() when the parsed data is not needed anymore.
416  */
417 int p2p_parse(const u8 *data, size_t len, struct p2p_message *msg)
418 {
419         os_memset(msg, 0, sizeof(*msg));
420         wpa_printf(MSG_DEBUG, "P2P: Parsing the received message");
421         if (len < 1) {
422                 wpa_printf(MSG_DEBUG, "P2P: No Dialog Token in the message");
423                 return -1;
424         }
425         msg->dialog_token = data[0];
426         wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", msg->dialog_token);
427
428         return p2p_parse_ies(data + 1, len - 1, msg);
429 }
430
431
432 /**
433  * p2p_parse_free - Free temporary data from P2P parsing
434  * @msg: Parsed attributes
435  */
436 void p2p_parse_free(struct p2p_message *msg)
437 {
438         wpabuf_free(msg->p2p_attributes);
439         msg->p2p_attributes = NULL;
440         wpabuf_free(msg->wps_attributes);
441         msg->wps_attributes = NULL;
442 }
443
444
445 int p2p_group_info_parse(const u8 *gi, size_t gi_len,
446                          struct p2p_group_info *info)
447 {
448         const u8 *g, *gend;
449
450         os_memset(info, 0, sizeof(*info));
451         if (gi == NULL)
452                 return 0;
453
454         g = gi;
455         gend = gi + gi_len;
456         while (g < gend) {
457                 struct p2p_client_info *cli;
458                 const u8 *t, *cend;
459                 int count;
460
461                 cli = &info->client[info->num_clients];
462                 cend = g + 1 + g[0];
463                 if (cend > gend)
464                         return -1; /* invalid data */
465                 /* g at start of P2P Client Info Descriptor */
466                 /* t at Device Capability Bitmap */
467                 t = g + 1 + 2 * ETH_ALEN;
468                 if (t > cend)
469                         return -1; /* invalid data */
470                 cli->p2p_device_addr = g + 1;
471                 cli->p2p_interface_addr = g + 1 + ETH_ALEN;
472                 cli->dev_capab = t[0];
473
474                 if (t + 1 + 2 + 8 + 1 > cend)
475                         return -1; /* invalid data */
476
477                 cli->config_methods = WPA_GET_BE16(&t[1]);
478                 cli->pri_dev_type = &t[3];
479
480                 t += 1 + 2 + 8;
481                 /* t at Number of Secondary Device Types */
482                 cli->num_sec_dev_types = *t++;
483                 if (t + 8 * cli->num_sec_dev_types > cend)
484                         return -1; /* invalid data */
485                 cli->sec_dev_types = t;
486                 t += 8 * cli->num_sec_dev_types;
487
488                 /* t at Device Name in WPS TLV format */
489                 if (t + 2 + 2 > cend)
490                         return -1; /* invalid data */
491                 if (WPA_GET_BE16(t) != ATTR_DEV_NAME)
492                         return -1; /* invalid Device Name TLV */
493                 t += 2;
494                 count = WPA_GET_BE16(t);
495                 t += 2;
496                 if (count > cend - t)
497                         return -1; /* invalid Device Name TLV */
498                 if (count >= 32)
499                         count = 32;
500                 cli->dev_name = (const char *) t;
501                 cli->dev_name_len = count;
502
503                 g = cend;
504
505                 info->num_clients++;
506                 if (info->num_clients == P2P_MAX_GROUP_ENTRIES)
507                         return -1;
508         }
509
510         return 0;
511 }
512
513
514 static int p2p_group_info_text(const u8 *gi, size_t gi_len, char *buf,
515                                char *end)
516 {
517         char *pos = buf;
518         int ret;
519         struct p2p_group_info info;
520         unsigned int i;
521
522         if (p2p_group_info_parse(gi, gi_len, &info) < 0)
523                 return 0;
524
525         for (i = 0; i < info.num_clients; i++) {
526                 struct p2p_client_info *cli;
527                 char name[33];
528                 char devtype[WPS_DEV_TYPE_BUFSIZE];
529                 u8 s;
530                 int count;
531
532                 cli = &info.client[i];
533                 ret = os_snprintf(pos, end - pos, "p2p_group_client: "
534                                   "dev=" MACSTR " iface=" MACSTR,
535                                   MAC2STR(cli->p2p_device_addr),
536                                   MAC2STR(cli->p2p_interface_addr));
537                 if (ret < 0 || ret >= end - pos)
538                         return pos - buf;
539                 pos += ret;
540
541                 ret = os_snprintf(pos, end - pos,
542                                   " dev_capab=0x%x config_methods=0x%x "
543                                   "dev_type=%s",
544                                   cli->dev_capab, cli->config_methods,
545                                   wps_dev_type_bin2str(cli->pri_dev_type,
546                                                        devtype,
547                                                        sizeof(devtype)));
548                 if (ret < 0 || ret >= end - pos)
549                         return pos - buf;
550                 pos += ret;
551
552                 for (s = 0; s < cli->num_sec_dev_types; s++) {
553                         ret = os_snprintf(pos, end - pos, " dev_type=%s",
554                                           wps_dev_type_bin2str(
555                                                   &cli->sec_dev_types[s * 8],
556                                                   devtype, sizeof(devtype)));
557                         if (ret < 0 || ret >= end - pos)
558                                 return pos - buf;
559                         pos += ret;
560                 }
561
562                 os_memcpy(name, cli->dev_name, cli->dev_name_len);
563                 name[cli->dev_name_len] = '\0';
564                 count = (int) cli->dev_name_len - 1;
565                 while (count >= 0) {
566                         if (name[count] < 32)
567                                 name[count] = '_';
568                         count--;
569                 }
570
571                 ret = os_snprintf(pos, end - pos, " dev_name='%s'\n", name);
572                 if (ret < 0 || ret >= end - pos)
573                         return pos - buf;
574                 pos += ret;
575         }
576
577         return pos - buf;
578 }
579
580
581 /**
582  * p2p_attr_text - Build text format description of P2P IE attributes
583  * @data: P2P IE contents
584  * @buf: Buffer for returning text
585  * @end: Pointer to the end of the buf area
586  * Returns: Number of octets written to the buffer or -1 on faikure
587  *
588  * This function can be used to parse P2P IE contents into text format
589  * field=value lines.
590  */
591 int p2p_attr_text(struct wpabuf *data, char *buf, char *end)
592 {
593         struct p2p_message msg;
594         char *pos = buf;
595         int ret;
596
597         os_memset(&msg, 0, sizeof(msg));
598         if (p2p_parse_p2p_ie(data, &msg))
599                 return -1;
600
601         if (msg.capability) {
602                 ret = os_snprintf(pos, end - pos,
603                                   "p2p_dev_capab=0x%x\n"
604                                   "p2p_group_capab=0x%x\n",
605                                   msg.capability[0], msg.capability[1]);
606                 if (ret < 0 || ret >= end - pos)
607                         return pos - buf;
608                 pos += ret;
609         }
610
611         if (msg.pri_dev_type) {
612                 char devtype[WPS_DEV_TYPE_BUFSIZE];
613                 ret = os_snprintf(pos, end - pos,
614                                   "p2p_primary_device_type=%s\n",
615                                   wps_dev_type_bin2str(msg.pri_dev_type,
616                                                        devtype,
617                                                        sizeof(devtype)));
618                 if (ret < 0 || ret >= end - pos)
619                         return pos - buf;
620                 pos += ret;
621         }
622
623         ret = os_snprintf(pos, end - pos, "p2p_device_name=%s\n",
624                           msg.device_name);
625         if (ret < 0 || ret >= end - pos)
626                 return pos - buf;
627         pos += ret;
628
629         if (msg.p2p_device_addr) {
630                 ret = os_snprintf(pos, end - pos, "p2p_device_addr=" MACSTR
631                                   "\n",
632                                   MAC2STR(msg.p2p_device_addr));
633                 if (ret < 0 || ret >= end - pos)
634                         return pos - buf;
635                 pos += ret;
636         }
637
638         ret = os_snprintf(pos, end - pos, "p2p_config_methods=0x%x\n",
639                           msg.config_methods);
640         if (ret < 0 || ret >= end - pos)
641                 return pos - buf;
642         pos += ret;
643
644         ret = p2p_group_info_text(msg.group_info, msg.group_info_len,
645                                   pos, end);
646         if (ret < 0)
647                 return pos - buf;
648         pos += ret;
649
650         return pos - buf;
651 }
652
653
654 int p2p_get_cross_connect_disallowed(const struct wpabuf *p2p_ie)
655 {
656         struct p2p_message msg;
657
658         os_memset(&msg, 0, sizeof(msg));
659         if (p2p_parse_p2p_ie(p2p_ie, &msg))
660                 return 0;
661
662         if (!msg.manageability)
663                 return 0;
664
665         return !(msg.manageability[0] & P2P_MAN_CROSS_CONNECTION_PERMITTED);
666 }
667
668
669 u8 p2p_get_group_capab(const struct wpabuf *p2p_ie)
670 {
671         struct p2p_message msg;
672
673         os_memset(&msg, 0, sizeof(msg));
674         if (p2p_parse_p2p_ie(p2p_ie, &msg))
675                 return 0;
676
677         if (!msg.capability)
678                 return 0;
679
680         return msg.capability[1];
681 }
682
683
684 const u8 * p2p_get_go_dev_addr(const struct wpabuf *p2p_ie)
685 {
686         struct p2p_message msg;
687
688         os_memset(&msg, 0, sizeof(msg));
689         if (p2p_parse_p2p_ie(p2p_ie, &msg))
690                 return NULL;
691
692         if (msg.p2p_device_addr)
693                 return msg.p2p_device_addr;
694         if (msg.device_id)
695                 return msg.device_id;
696
697         return NULL;
698 }