3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/ieee802_11_defs.h"
13 #include "common/ieee802_11_common.h"
14 #include "wps/wps_i.h"
18 static int p2p_parse_attribute(u8 id, const u8 *data, u16 len,
19 struct p2p_message *msg)
23 char devtype[WPS_DEV_TYPE_BUFSIZE];
26 case P2P_ATTR_CAPABILITY:
28 wpa_printf(MSG_DEBUG, "P2P: Too short Capability "
29 "attribute (length %d)", len);
32 msg->capability = data;
33 wpa_printf(MSG_DEBUG, "P2P: * Device Capability %02x "
34 "Group Capability %02x",
37 case P2P_ATTR_DEVICE_ID:
39 wpa_printf(MSG_DEBUG, "P2P: Too short Device ID "
40 "attribute (length %d)", len);
43 msg->device_id = data;
44 wpa_printf(MSG_DEBUG, "P2P: * Device ID " MACSTR,
45 MAC2STR(msg->device_id));
47 case P2P_ATTR_GROUP_OWNER_INTENT:
49 wpa_printf(MSG_DEBUG, "P2P: Too short GO Intent "
50 "attribute (length %d)", len);
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);
59 wpa_printf(MSG_DEBUG, "P2P: Too short Status "
60 "attribute (length %d)", len);
64 wpa_printf(MSG_DEBUG, "P2P: * Status: %d", data[0]);
66 case P2P_ATTR_LISTEN_CHANNEL:
68 wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: Ignore "
73 wpa_printf(MSG_DEBUG, "P2P: Too short Listen Channel "
74 "attribute (length %d)", len);
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]);
83 case P2P_ATTR_OPERATING_CHANNEL:
85 wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: "
86 "Ignore null channel");
90 wpa_printf(MSG_DEBUG, "P2P: Too short Operating "
91 "Channel attribute (length %d)", len);
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]);
100 case P2P_ATTR_CHANNEL_LIST:
102 wpa_printf(MSG_DEBUG, "P2P: Too short Channel List "
103 "attribute (length %d)", len);
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);
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");
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);
124 msg->p2p_device_info = data;
125 msg->p2p_device_info_len = len;
127 msg->p2p_device_addr = pos;
129 msg->config_methods = WPA_GET_BE16(pos);
131 msg->pri_dev_type = pos;
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");
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));
144 if (WPA_GET_BE16(pos) != ATTR_DEV_NAME) {
145 wpa_hexdump(MSG_DEBUG, "P2P: Unexpected Device Name "
150 nlen = WPA_GET_BE16(pos);
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));
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')
163 if (msg->device_name[i] > 0 &&
164 msg->device_name[i] < 32)
165 msg->device_name[i] = '_';
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,
173 msg->device_name, msg->config_methods);
175 case P2P_ATTR_CONFIGURATION_TIMEOUT:
177 wpa_printf(MSG_DEBUG, "P2P: Too short Configuration "
178 "Timeout attribute (length %d)", len);
181 msg->config_timeout = data;
182 wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout");
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)",
191 msg->intended_addr = data;
192 wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address: "
193 MACSTR, MAC2STR(msg->intended_addr));
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);
201 msg->group_bssid = data;
202 wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID: " MACSTR,
203 MAC2STR(msg->group_bssid));
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);
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);
219 case P2P_ATTR_INVITATION_FLAGS:
221 wpa_printf(MSG_DEBUG, "P2P: Too short Invitation "
222 "Flag attribute (length %d)", len);
225 msg->invitation_flags = data;
226 wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x",
229 case P2P_ATTR_MANAGEABILITY:
231 wpa_printf(MSG_DEBUG, "P2P: Too short Manageability "
232 "attribute (length %d)", len);
235 msg->manageability = data;
236 wpa_printf(MSG_DEBUG, "P2P: * Manageability: bitmap 0x%x",
239 case P2P_ATTR_NOTICE_OF_ABSENCE:
241 wpa_printf(MSG_DEBUG, "P2P: Too short Notice of "
242 "Absence attribute (length %d)", len);
247 wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
249 case P2P_ATTR_EXT_LISTEN_TIMING:
251 wpa_printf(MSG_DEBUG, "P2P: Too short Extended Listen "
252 "Timing attribute (length %d)", len);
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));
261 case P2P_ATTR_MINOR_REASON_CODE:
263 wpa_printf(MSG_DEBUG, "P2P: Too short Minor Reason "
264 "Code attribute (length %d)", len);
267 msg->minor_reason_code = data;
268 wpa_printf(MSG_DEBUG, "P2P: * Minor Reason Code: %u",
269 *msg->minor_reason_code);
272 wpa_printf(MSG_DEBUG, "P2P: Skipped unknown attribute %d "
273 "(length %d)", id, len);
282 * p2p_parse_p2p_ie - Parse P2P IE
283 * @buf: Concatenated P2P IE(s) payload
284 * @msg: Buffer for returning parsed attributes
285 * Returns: 0 on success, -1 on failure
287 * Note: Caller is responsible for clearing the msg data structure before
288 * calling this function.
290 int p2p_parse_p2p_ie(const struct wpabuf *buf, struct p2p_message *msg)
292 const u8 *pos = wpabuf_head_u8(buf);
293 const u8 *end = pos + wpabuf_len(buf);
295 wpa_printf(MSG_DEBUG, "P2P: Parsing P2P IE");
299 if (pos + 2 >= end) {
300 wpa_printf(MSG_DEBUG, "P2P: Invalid P2P attribute");
303 attr_len = WPA_GET_LE16(pos + 1);
304 wpa_printf(MSG_DEBUG, "P2P: Attribute %d length %u",
306 if (pos + 3 + attr_len > end) {
307 wpa_printf(MSG_DEBUG, "P2P: Attribute underflow "
309 attr_len, (int) (end - pos - 3));
310 wpa_hexdump(MSG_MSGDUMP, "P2P: Data", pos, end - pos);
313 if (p2p_parse_attribute(pos[0], pos + 3, attr_len, msg))
322 static int p2p_parse_wps_ie(const struct wpabuf *buf, struct p2p_message *msg)
324 struct wps_parse_attr attr;
327 wpa_printf(MSG_DEBUG, "P2P: Parsing WPS IE");
328 if (wps_parse_msg(buf, &attr))
330 if (attr.dev_name && attr.dev_name_len < sizeof(msg->device_name) &&
331 !msg->device_name[0])
332 os_memcpy(msg->device_name, attr.dev_name, attr.dev_name_len);
333 if (attr.config_methods) {
334 msg->wps_config_methods =
335 WPA_GET_BE16(attr.config_methods);
336 wpa_printf(MSG_DEBUG, "P2P: Config Methods (WPS): 0x%x",
337 msg->wps_config_methods);
339 if (attr.dev_password_id) {
340 msg->dev_password_id = WPA_GET_BE16(attr.dev_password_id);
341 wpa_printf(MSG_DEBUG, "P2P: Device Password ID: %d",
342 msg->dev_password_id);
344 if (attr.primary_dev_type) {
345 char devtype[WPS_DEV_TYPE_BUFSIZE];
346 msg->wps_pri_dev_type = attr.primary_dev_type;
347 wpa_printf(MSG_DEBUG, "P2P: Primary Device Type (WPS): %s",
348 wps_dev_type_bin2str(msg->wps_pri_dev_type, devtype,
351 if (attr.sec_dev_type_list) {
352 msg->wps_sec_dev_type_list = attr.sec_dev_type_list;
353 msg->wps_sec_dev_type_list_len = attr.sec_dev_type_list_len;
356 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
357 msg->wps_vendor_ext[i] = attr.vendor_ext[i];
358 msg->wps_vendor_ext_len[i] = attr.vendor_ext_len[i];
361 msg->manufacturer = attr.manufacturer;
362 msg->manufacturer_len = attr.manufacturer_len;
363 msg->model_name = attr.model_name;
364 msg->model_name_len = attr.model_name_len;
365 msg->model_number = attr.model_number;
366 msg->model_number_len = attr.model_number_len;
367 msg->serial_number = attr.serial_number;
368 msg->serial_number_len = attr.serial_number_len;
375 * p2p_parse_ies - Parse P2P message IEs (both WPS and P2P IE)
376 * @data: IEs from the message
377 * @len: Length of data buffer in octets
378 * @msg: Buffer for returning parsed attributes
379 * Returns: 0 on success, -1 on failure
381 * Note: Caller is responsible for clearing the msg data structure before
382 * calling this function.
384 * Note: Caller must free temporary memory allocations by calling
385 * p2p_parse_free() when the parsed data is not needed anymore.
387 int p2p_parse_ies(const u8 *data, size_t len, struct p2p_message *msg)
389 struct ieee802_11_elems elems;
391 ieee802_11_parse_elems(data, len, &elems, 0);
392 if (elems.ds_params && elems.ds_params_len >= 1)
393 msg->ds_params = elems.ds_params;
395 msg->ssid = elems.ssid - 2;
397 msg->wps_attributes = ieee802_11_vendor_ie_concat(data, len,
399 if (msg->wps_attributes &&
400 p2p_parse_wps_ie(msg->wps_attributes, msg)) {
405 msg->p2p_attributes = ieee802_11_vendor_ie_concat(data, len,
407 if (msg->p2p_attributes &&
408 p2p_parse_p2p_ie(msg->p2p_attributes, msg)) {
409 wpa_printf(MSG_DEBUG, "P2P: Failed to parse P2P IE data");
410 if (msg->p2p_attributes)
411 wpa_hexdump_buf(MSG_MSGDUMP, "P2P: P2P IE data",
412 msg->p2p_attributes);
417 #ifdef CONFIG_WIFI_DISPLAY
419 msg->wfd_subelems = ieee802_11_vendor_ie_concat(
420 data, len, WFD_IE_VENDOR_TYPE);
422 #endif /* CONFIG_WIFI_DISPLAY */
429 * p2p_parse - Parse a P2P Action frame contents
430 * @data: Action frame payload after Category and Code fields
431 * @len: Length of data buffer in octets
432 * @msg: Buffer for returning parsed attributes
433 * Returns: 0 on success, -1 on failure
435 * Note: Caller must free temporary memory allocations by calling
436 * p2p_parse_free() when the parsed data is not needed anymore.
438 int p2p_parse(const u8 *data, size_t len, struct p2p_message *msg)
440 os_memset(msg, 0, sizeof(*msg));
441 wpa_printf(MSG_DEBUG, "P2P: Parsing the received message");
443 wpa_printf(MSG_DEBUG, "P2P: No Dialog Token in the message");
446 msg->dialog_token = data[0];
447 wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", msg->dialog_token);
449 return p2p_parse_ies(data + 1, len - 1, msg);
454 * p2p_parse_free - Free temporary data from P2P parsing
455 * @msg: Parsed attributes
457 void p2p_parse_free(struct p2p_message *msg)
459 wpabuf_free(msg->p2p_attributes);
460 msg->p2p_attributes = NULL;
461 wpabuf_free(msg->wps_attributes);
462 msg->wps_attributes = NULL;
463 #ifdef CONFIG_WIFI_DISPLAY
464 wpabuf_free(msg->wfd_subelems);
465 msg->wfd_subelems = NULL;
466 #endif /* CONFIG_WIFI_DISPLAY */
470 int p2p_group_info_parse(const u8 *gi, size_t gi_len,
471 struct p2p_group_info *info)
475 os_memset(info, 0, sizeof(*info));
482 struct p2p_client_info *cli;
486 cli = &info->client[info->num_clients];
489 return -1; /* invalid data */
490 /* g at start of P2P Client Info Descriptor */
491 /* t at Device Capability Bitmap */
492 t = g + 1 + 2 * ETH_ALEN;
494 return -1; /* invalid data */
495 cli->p2p_device_addr = g + 1;
496 cli->p2p_interface_addr = g + 1 + ETH_ALEN;
497 cli->dev_capab = t[0];
499 if (t + 1 + 2 + 8 + 1 > cend)
500 return -1; /* invalid data */
502 cli->config_methods = WPA_GET_BE16(&t[1]);
503 cli->pri_dev_type = &t[3];
506 /* t at Number of Secondary Device Types */
507 cli->num_sec_dev_types = *t++;
508 if (t + 8 * cli->num_sec_dev_types > cend)
509 return -1; /* invalid data */
510 cli->sec_dev_types = t;
511 t += 8 * cli->num_sec_dev_types;
513 /* t at Device Name in WPS TLV format */
514 if (t + 2 + 2 > cend)
515 return -1; /* invalid data */
516 if (WPA_GET_BE16(t) != ATTR_DEV_NAME)
517 return -1; /* invalid Device Name TLV */
519 count = WPA_GET_BE16(t);
521 if (count > cend - t)
522 return -1; /* invalid Device Name TLV */
525 cli->dev_name = (const char *) t;
526 cli->dev_name_len = count;
531 if (info->num_clients == P2P_MAX_GROUP_ENTRIES)
539 static int p2p_group_info_text(const u8 *gi, size_t gi_len, char *buf,
544 struct p2p_group_info info;
547 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
550 for (i = 0; i < info.num_clients; i++) {
551 struct p2p_client_info *cli;
553 char devtype[WPS_DEV_TYPE_BUFSIZE];
557 cli = &info.client[i];
558 ret = os_snprintf(pos, end - pos, "p2p_group_client: "
559 "dev=" MACSTR " iface=" MACSTR,
560 MAC2STR(cli->p2p_device_addr),
561 MAC2STR(cli->p2p_interface_addr));
562 if (ret < 0 || ret >= end - pos)
566 ret = os_snprintf(pos, end - pos,
567 " dev_capab=0x%x config_methods=0x%x "
569 cli->dev_capab, cli->config_methods,
570 wps_dev_type_bin2str(cli->pri_dev_type,
573 if (ret < 0 || ret >= end - pos)
577 for (s = 0; s < cli->num_sec_dev_types; s++) {
578 ret = os_snprintf(pos, end - pos, " dev_type=%s",
579 wps_dev_type_bin2str(
580 &cli->sec_dev_types[s * 8],
581 devtype, sizeof(devtype)));
582 if (ret < 0 || ret >= end - pos)
587 os_memcpy(name, cli->dev_name, cli->dev_name_len);
588 name[cli->dev_name_len] = '\0';
589 count = (int) cli->dev_name_len - 1;
591 if (name[count] > 0 && name[count] < 32)
596 ret = os_snprintf(pos, end - pos, " dev_name='%s'\n", name);
597 if (ret < 0 || ret >= end - pos)
607 * p2p_attr_text - Build text format description of P2P IE attributes
608 * @data: P2P IE contents
609 * @buf: Buffer for returning text
610 * @end: Pointer to the end of the buf area
611 * Returns: Number of octets written to the buffer or -1 on faikure
613 * This function can be used to parse P2P IE contents into text format
616 int p2p_attr_text(struct wpabuf *data, char *buf, char *end)
618 struct p2p_message msg;
622 os_memset(&msg, 0, sizeof(msg));
623 if (p2p_parse_p2p_ie(data, &msg))
626 if (msg.capability) {
627 ret = os_snprintf(pos, end - pos,
628 "p2p_dev_capab=0x%x\n"
629 "p2p_group_capab=0x%x\n",
630 msg.capability[0], msg.capability[1]);
631 if (ret < 0 || ret >= end - pos)
636 if (msg.pri_dev_type) {
637 char devtype[WPS_DEV_TYPE_BUFSIZE];
638 ret = os_snprintf(pos, end - pos,
639 "p2p_primary_device_type=%s\n",
640 wps_dev_type_bin2str(msg.pri_dev_type,
643 if (ret < 0 || ret >= end - pos)
648 ret = os_snprintf(pos, end - pos, "p2p_device_name=%s\n",
650 if (ret < 0 || ret >= end - pos)
654 if (msg.p2p_device_addr) {
655 ret = os_snprintf(pos, end - pos, "p2p_device_addr=" MACSTR
657 MAC2STR(msg.p2p_device_addr));
658 if (ret < 0 || ret >= end - pos)
663 ret = os_snprintf(pos, end - pos, "p2p_config_methods=0x%x\n",
665 if (ret < 0 || ret >= end - pos)
669 ret = p2p_group_info_text(msg.group_info, msg.group_info_len,
679 int p2p_get_cross_connect_disallowed(const struct wpabuf *p2p_ie)
681 struct p2p_message msg;
683 os_memset(&msg, 0, sizeof(msg));
684 if (p2p_parse_p2p_ie(p2p_ie, &msg))
687 if (!msg.manageability)
690 return !(msg.manageability[0] & P2P_MAN_CROSS_CONNECTION_PERMITTED);
694 u8 p2p_get_group_capab(const struct wpabuf *p2p_ie)
696 struct p2p_message msg;
698 os_memset(&msg, 0, sizeof(msg));
699 if (p2p_parse_p2p_ie(p2p_ie, &msg))
705 return msg.capability[1];
709 const u8 * p2p_get_go_dev_addr(const struct wpabuf *p2p_ie)
711 struct p2p_message msg;
713 os_memset(&msg, 0, sizeof(msg));
714 if (p2p_parse_p2p_ie(p2p_ie, &msg))
717 if (msg.p2p_device_addr)
718 return msg.p2p_device_addr;
720 return msg.device_id;