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