3 * Copyright (c) 2009-2010, Atheros Communications
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "common/ieee802_11_defs.h"
19 #include "common/ieee802_11_common.h"
20 #include "wps/wps_i.h"
24 static int p2p_parse_attribute(u8 id, const u8 *data, u16 len,
25 struct p2p_message *msg)
29 char devtype[WPS_DEV_TYPE_BUFSIZE];
32 case P2P_ATTR_CAPABILITY:
34 wpa_printf(MSG_DEBUG, "P2P: Too short Capability "
35 "attribute (length %d)", len);
38 msg->capability = data;
39 wpa_printf(MSG_DEBUG, "P2P: * Device Capability %02x "
40 "Group Capability %02x",
43 case P2P_ATTR_DEVICE_ID:
45 wpa_printf(MSG_DEBUG, "P2P: Too short Device ID "
46 "attribute (length %d)", len);
49 msg->device_id = data;
50 wpa_printf(MSG_DEBUG, "P2P: * Device ID " MACSTR,
51 MAC2STR(msg->device_id));
53 case P2P_ATTR_GROUP_OWNER_INTENT:
55 wpa_printf(MSG_DEBUG, "P2P: Too short GO Intent "
56 "attribute (length %d)", len);
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);
65 wpa_printf(MSG_DEBUG, "P2P: Too short Status "
66 "attribute (length %d)", len);
70 wpa_printf(MSG_DEBUG, "P2P: * Status: %d", data[0]);
72 case P2P_ATTR_LISTEN_CHANNEL:
74 wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: Ignore "
79 wpa_printf(MSG_DEBUG, "P2P: Too short Listen Channel "
80 "attribute (length %d)", len);
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]);
89 case P2P_ATTR_OPERATING_CHANNEL:
91 wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: "
92 "Ignore null channel");
96 wpa_printf(MSG_DEBUG, "P2P: Too short Operating "
97 "Channel attribute (length %d)", len);
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]);
106 case P2P_ATTR_CHANNEL_LIST:
108 wpa_printf(MSG_DEBUG, "P2P: Too short Channel List "
109 "attribute (length %d)", len);
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);
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");
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);
130 msg->p2p_device_info = data;
131 msg->p2p_device_info_len = len;
133 msg->p2p_device_addr = pos;
135 msg->config_methods = WPA_GET_BE16(pos);
137 msg->pri_dev_type = pos;
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");
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));
150 if (WPA_GET_BE16(pos) != ATTR_DEV_NAME) {
151 wpa_hexdump(MSG_DEBUG, "P2P: Unexpected Device Name "
156 nlen = WPA_GET_BE16(pos);
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));
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')
169 if (msg->device_name[i] < 32)
170 msg->device_name[i] = '_';
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,
178 msg->device_name, msg->config_methods);
180 case P2P_ATTR_CONFIGURATION_TIMEOUT:
182 wpa_printf(MSG_DEBUG, "P2P: Too short Configuration "
183 "Timeout attribute (length %d)", len);
186 msg->config_timeout = data;
187 wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout");
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)",
196 msg->intended_addr = data;
197 wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address: "
198 MACSTR, MAC2STR(msg->intended_addr));
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);
206 msg->group_bssid = data;
207 wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID: " MACSTR,
208 MAC2STR(msg->group_bssid));
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);
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);
224 case P2P_ATTR_INVITATION_FLAGS:
226 wpa_printf(MSG_DEBUG, "P2P: Too short Invitation "
227 "Flag attribute (length %d)", len);
230 msg->invitation_flags = data;
231 wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x",
234 case P2P_ATTR_MANAGEABILITY:
236 wpa_printf(MSG_DEBUG, "P2P: Too short Manageability "
237 "attribute (length %d)", len);
240 msg->manageability = data;
241 wpa_printf(MSG_DEBUG, "P2P: * Manageability: bitmap 0x%x",
244 case P2P_ATTR_NOTICE_OF_ABSENCE:
246 wpa_printf(MSG_DEBUG, "P2P: Too short Notice of "
247 "Absence attribute (length %d)", len);
252 wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
254 case P2P_ATTR_EXT_LISTEN_TIMING:
256 wpa_printf(MSG_DEBUG, "P2P: Too short Extended Listen "
257 "Timing attribute (length %d)", len);
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));
266 case P2P_ATTR_MINOR_REASON_CODE:
268 wpa_printf(MSG_DEBUG, "P2P: Too short Minor Reason "
269 "Code attribute (length %d)", len);
272 msg->minor_reason_code = data;
273 wpa_printf(MSG_DEBUG, "P2P: * Minor Reason Code: %u",
274 *msg->minor_reason_code);
277 wpa_printf(MSG_DEBUG, "P2P: Skipped unknown attribute %d "
278 "(length %d)", id, len);
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
292 * Note: Caller is responsible for clearing the msg data structure before
293 * calling this function.
295 int p2p_parse_p2p_ie(const struct wpabuf *buf, struct p2p_message *msg)
297 const u8 *pos = wpabuf_head_u8(buf);
298 const u8 *end = pos + wpabuf_len(buf);
300 wpa_printf(MSG_DEBUG, "P2P: Parsing P2P IE");
304 if (pos + 2 >= end) {
305 wpa_printf(MSG_DEBUG, "P2P: Invalid P2P attribute");
308 attr_len = WPA_GET_LE16(pos + 1);
309 wpa_printf(MSG_DEBUG, "P2P: Attribute %d length %u",
311 if (pos + 3 + attr_len > end) {
312 wpa_printf(MSG_DEBUG, "P2P: Attribute underflow "
314 attr_len, (int) (end - pos - 3));
315 wpa_hexdump(MSG_MSGDUMP, "P2P: Data", pos, end - pos);
318 if (p2p_parse_attribute(pos[0], pos + 3, attr_len, msg))
327 static int p2p_parse_wps_ie(const struct wpabuf *buf, struct p2p_message *msg)
329 struct wps_parse_attr attr;
331 wpa_printf(MSG_DEBUG, "P2P: Parsing WPS IE");
332 if (wps_parse_msg(buf, &attr))
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);
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);
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,
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
367 * Note: Caller is responsible for clearing the msg data structure before
368 * calling this function.
370 * Note: Caller must free temporary memory allocations by calling
371 * p2p_parse_free() when the parsed data is not needed anymore.
373 int p2p_parse_ies(const u8 *data, size_t len, struct p2p_message *msg)
375 struct ieee802_11_elems elems;
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;
381 msg->ssid = elems.ssid - 2;
383 msg->wps_attributes = ieee802_11_vendor_ie_concat(data, len,
385 if (msg->wps_attributes &&
386 p2p_parse_wps_ie(msg->wps_attributes, msg)) {
391 msg->p2p_attributes = ieee802_11_vendor_ie_concat(data, len,
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);
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
414 * Note: Caller must free temporary memory allocations by calling
415 * p2p_parse_free() when the parsed data is not needed anymore.
417 int p2p_parse(const u8 *data, size_t len, struct p2p_message *msg)
419 os_memset(msg, 0, sizeof(*msg));
420 wpa_printf(MSG_DEBUG, "P2P: Parsing the received message");
422 wpa_printf(MSG_DEBUG, "P2P: No Dialog Token in the message");
425 msg->dialog_token = data[0];
426 wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", msg->dialog_token);
428 return p2p_parse_ies(data + 1, len - 1, msg);
433 * p2p_parse_free - Free temporary data from P2P parsing
434 * @msg: Parsed attributes
436 void p2p_parse_free(struct p2p_message *msg)
438 wpabuf_free(msg->p2p_attributes);
439 msg->p2p_attributes = NULL;
440 wpabuf_free(msg->wps_attributes);
441 msg->wps_attributes = NULL;
445 int p2p_group_info_parse(const u8 *gi, size_t gi_len,
446 struct p2p_group_info *info)
450 os_memset(info, 0, sizeof(*info));
457 struct p2p_client_info *cli;
461 cli = &info->client[info->num_clients];
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;
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];
474 if (t + 1 + 2 + 8 + 1 > cend)
475 return -1; /* invalid data */
477 cli->config_methods = WPA_GET_BE16(&t[1]);
478 cli->pri_dev_type = &t[3];
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;
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 */
494 count = WPA_GET_BE16(t);
496 if (count > cend - t)
497 return -1; /* invalid Device Name TLV */
500 cli->dev_name = (const char *) t;
501 cli->dev_name_len = count;
506 if (info->num_clients == P2P_MAX_GROUP_ENTRIES)
514 static int p2p_group_info_text(const u8 *gi, size_t gi_len, char *buf,
519 struct p2p_group_info info;
522 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
525 for (i = 0; i < info.num_clients; i++) {
526 struct p2p_client_info *cli;
528 char devtype[WPS_DEV_TYPE_BUFSIZE];
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)
541 ret = os_snprintf(pos, end - pos,
542 " dev_capab=0x%x config_methods=0x%x "
544 cli->dev_capab, cli->config_methods,
545 wps_dev_type_bin2str(cli->pri_dev_type,
548 if (ret < 0 || ret >= end - pos)
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)
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;
566 if (name[count] < 32)
571 ret = os_snprintf(pos, end - pos, " dev_name='%s'\n", name);
572 if (ret < 0 || ret >= end - pos)
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
588 * This function can be used to parse P2P IE contents into text format
591 int p2p_attr_text(struct wpabuf *data, char *buf, char *end)
593 struct p2p_message msg;
597 os_memset(&msg, 0, sizeof(msg));
598 if (p2p_parse_p2p_ie(data, &msg))
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)
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,
618 if (ret < 0 || ret >= end - pos)
623 ret = os_snprintf(pos, end - pos, "p2p_device_name=%s\n",
625 if (ret < 0 || ret >= end - pos)
629 if (msg.p2p_device_addr) {
630 ret = os_snprintf(pos, end - pos, "p2p_device_addr=" MACSTR
632 MAC2STR(msg.p2p_device_addr));
633 if (ret < 0 || ret >= end - pos)
638 ret = os_snprintf(pos, end - pos, "p2p_config_methods=0x%x\n",
640 if (ret < 0 || ret >= end - pos)
644 ret = p2p_group_info_text(msg.group_info, msg.group_info_len,
654 int p2p_get_cross_connect_disallowed(const struct wpabuf *p2p_ie)
656 struct p2p_message msg;
658 os_memset(&msg, 0, sizeof(msg));
659 if (p2p_parse_p2p_ie(p2p_ie, &msg))
662 if (!msg.manageability)
665 return !(msg.manageability[0] & P2P_MAN_CROSS_CONNECTION_PERMITTED);
669 u8 p2p_get_group_capab(const struct wpabuf *p2p_ie)
671 struct p2p_message msg;
673 os_memset(&msg, 0, sizeof(msg));
674 if (p2p_parse_p2p_ie(p2p_ie, &msg))
680 return msg.capability[1];
684 const u8 * p2p_get_go_dev_addr(const struct wpabuf *p2p_ie)
686 struct p2p_message msg;
688 os_memset(&msg, 0, sizeof(msg));
689 if (p2p_parse_p2p_ie(p2p_ie, &msg))
692 if (msg.p2p_device_addr)
693 return msg.p2p_device_addr;
695 return msg.device_id;