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