xref: /freebsd/contrib/wpa/src/p2p/p2p_parse.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1f05cddf9SRui Paulo /*
2f05cddf9SRui Paulo  * P2P - IE parser
3f05cddf9SRui Paulo  * Copyright (c) 2009-2010, Atheros Communications
4f05cddf9SRui Paulo  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
7f05cddf9SRui Paulo  */
8f05cddf9SRui Paulo 
9f05cddf9SRui Paulo #include "includes.h"
10f05cddf9SRui Paulo 
11f05cddf9SRui Paulo #include "common.h"
12f05cddf9SRui Paulo #include "common/ieee802_11_defs.h"
13f05cddf9SRui Paulo #include "common/ieee802_11_common.h"
14f05cddf9SRui Paulo #include "wps/wps_i.h"
15f05cddf9SRui Paulo #include "p2p_i.h"
16f05cddf9SRui Paulo 
17f05cddf9SRui Paulo 
p2p_copy_filter_devname(char * dst,size_t dst_len,const void * src,size_t src_len)18780fb4a2SCy Schubert void p2p_copy_filter_devname(char *dst, size_t dst_len,
19780fb4a2SCy Schubert 			     const void *src, size_t src_len)
20780fb4a2SCy Schubert {
21780fb4a2SCy Schubert 	size_t i;
22780fb4a2SCy Schubert 
23780fb4a2SCy Schubert 	if (src_len >= dst_len)
24780fb4a2SCy Schubert 		src_len = dst_len - 1;
25780fb4a2SCy Schubert 	os_memcpy(dst, src, src_len);
26780fb4a2SCy Schubert 	dst[src_len] = '\0';
27780fb4a2SCy Schubert 	for (i = 0; i < src_len; i++) {
28780fb4a2SCy Schubert 		if (dst[i] == '\0')
29780fb4a2SCy Schubert 			break;
30780fb4a2SCy Schubert 		if (is_ctrl_char(dst[i]))
31780fb4a2SCy Schubert 			dst[i] = '_';
32780fb4a2SCy Schubert 	}
33780fb4a2SCy Schubert }
34780fb4a2SCy Schubert 
35780fb4a2SCy Schubert 
p2p_parse_attribute(u8 id,const u8 * data,u16 len,struct p2p_message * msg)36f05cddf9SRui Paulo static int p2p_parse_attribute(u8 id, const u8 *data, u16 len,
37f05cddf9SRui Paulo 			       struct p2p_message *msg)
38f05cddf9SRui Paulo {
39f05cddf9SRui Paulo 	const u8 *pos;
40780fb4a2SCy Schubert 	u16 nlen;
41f05cddf9SRui Paulo 	char devtype[WPS_DEV_TYPE_BUFSIZE];
42f05cddf9SRui Paulo 
43f05cddf9SRui Paulo 	switch (id) {
44f05cddf9SRui Paulo 	case P2P_ATTR_CAPABILITY:
45f05cddf9SRui Paulo 		if (len < 2) {
46f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Capability "
47f05cddf9SRui Paulo 				   "attribute (length %d)", len);
48f05cddf9SRui Paulo 			return -1;
49f05cddf9SRui Paulo 		}
50f05cddf9SRui Paulo 		msg->capability = data;
51f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Device Capability %02x "
52f05cddf9SRui Paulo 			   "Group Capability %02x",
53f05cddf9SRui Paulo 			   data[0], data[1]);
54f05cddf9SRui Paulo 		break;
55f05cddf9SRui Paulo 	case P2P_ATTR_DEVICE_ID:
56f05cddf9SRui Paulo 		if (len < ETH_ALEN) {
57f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Device ID "
58f05cddf9SRui Paulo 				   "attribute (length %d)", len);
59f05cddf9SRui Paulo 			return -1;
60f05cddf9SRui Paulo 		}
61f05cddf9SRui Paulo 		msg->device_id = data;
62f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Device ID " MACSTR,
63f05cddf9SRui Paulo 			   MAC2STR(msg->device_id));
64f05cddf9SRui Paulo 		break;
65f05cddf9SRui Paulo 	case P2P_ATTR_GROUP_OWNER_INTENT:
66f05cddf9SRui Paulo 		if (len < 1) {
67f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short GO Intent "
68f05cddf9SRui Paulo 				   "attribute (length %d)", len);
69f05cddf9SRui Paulo 			return -1;
70f05cddf9SRui Paulo 		}
71f05cddf9SRui Paulo 		msg->go_intent = data;
72f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * GO Intent: Intent %u "
73f05cddf9SRui Paulo 			   "Tie breaker %u", data[0] >> 1, data[0] & 0x01);
74f05cddf9SRui Paulo 		break;
75f05cddf9SRui Paulo 	case P2P_ATTR_STATUS:
76f05cddf9SRui Paulo 		if (len < 1) {
77f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Status "
78f05cddf9SRui Paulo 				   "attribute (length %d)", len);
79f05cddf9SRui Paulo 			return -1;
80f05cddf9SRui Paulo 		}
81f05cddf9SRui Paulo 		msg->status = data;
82f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Status: %d", data[0]);
83f05cddf9SRui Paulo 		break;
84f05cddf9SRui Paulo 	case P2P_ATTR_LISTEN_CHANNEL:
85f05cddf9SRui Paulo 		if (len == 0) {
86f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: Ignore "
87f05cddf9SRui Paulo 				   "null channel");
88f05cddf9SRui Paulo 			break;
89f05cddf9SRui Paulo 		}
90f05cddf9SRui Paulo 		if (len < 5) {
91f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Listen Channel "
92f05cddf9SRui Paulo 				   "attribute (length %d)", len);
93f05cddf9SRui Paulo 			return -1;
94f05cddf9SRui Paulo 		}
95f05cddf9SRui Paulo 		msg->listen_channel = data;
96*a90b9d01SCy Schubert 		if (has_ctrl_char(data, 2)) {
97*a90b9d01SCy Schubert 			wpa_printf(MSG_DEBUG,
98*a90b9d01SCy Schubert 				   "P2P: * Listen Channel: Country(binary) %02x %02x (0x%02x) Regulatory Class %d Channel Number %d",
99*a90b9d01SCy Schubert 				   data[0], data[1], data[2], data[3], data[4]);
100*a90b9d01SCy Schubert 			break;
101*a90b9d01SCy Schubert 		}
102f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: "
103f05cddf9SRui Paulo 			   "Country %c%c(0x%02x) Regulatory "
104f05cddf9SRui Paulo 			   "Class %d Channel Number %d", data[0], data[1],
105f05cddf9SRui Paulo 			   data[2], data[3], data[4]);
106f05cddf9SRui Paulo 		break;
107f05cddf9SRui Paulo 	case P2P_ATTR_OPERATING_CHANNEL:
108f05cddf9SRui Paulo 		if (len == 0) {
109f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: "
110f05cddf9SRui Paulo 				   "Ignore null channel");
111f05cddf9SRui Paulo 			break;
112f05cddf9SRui Paulo 		}
113f05cddf9SRui Paulo 		if (len < 5) {
114f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Operating "
115f05cddf9SRui Paulo 				   "Channel attribute (length %d)", len);
116f05cddf9SRui Paulo 			return -1;
117f05cddf9SRui Paulo 		}
118f05cddf9SRui Paulo 		msg->operating_channel = data;
119*a90b9d01SCy Schubert 		if (has_ctrl_char(data, 2)) {
120*a90b9d01SCy Schubert 			wpa_printf(MSG_DEBUG,
121*a90b9d01SCy Schubert 				   "P2P: * Operating Channel: Country(binary) %02x %02x (0x%02x) Regulatory Class %d Channel Number %d",
122*a90b9d01SCy Schubert 				   data[0], data[1], data[2], data[3], data[4]);
123*a90b9d01SCy Schubert 			break;
124*a90b9d01SCy Schubert 		}
125f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: "
126f05cddf9SRui Paulo 			   "Country %c%c(0x%02x) Regulatory "
127f05cddf9SRui Paulo 			   "Class %d Channel Number %d", data[0], data[1],
128f05cddf9SRui Paulo 			   data[2], data[3], data[4]);
129f05cddf9SRui Paulo 		break;
130f05cddf9SRui Paulo 	case P2P_ATTR_CHANNEL_LIST:
131f05cddf9SRui Paulo 		if (len < 3) {
132f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Channel List "
133f05cddf9SRui Paulo 				   "attribute (length %d)", len);
134f05cddf9SRui Paulo 			return -1;
135f05cddf9SRui Paulo 		}
136f05cddf9SRui Paulo 		msg->channel_list = data;
137f05cddf9SRui Paulo 		msg->channel_list_len = len;
138*a90b9d01SCy Schubert 		if (has_ctrl_char(data, 2)) {
139*a90b9d01SCy Schubert 			wpa_printf(MSG_DEBUG,
140*a90b9d01SCy Schubert 				   "P2P: * Channel List: Country String (binary) %02x %02x (0x%02x)",
141*a90b9d01SCy Schubert 				   data[0], data[1], data[2]);
142*a90b9d01SCy Schubert 		} else {
143*a90b9d01SCy Schubert 			wpa_printf(MSG_DEBUG,
144*a90b9d01SCy Schubert 				   "P2P: * Channel List: Country String '%c%c(0x%02x)'",
145*a90b9d01SCy Schubert 				   data[0], data[1], data[2]);
146*a90b9d01SCy Schubert 		}
147f05cddf9SRui Paulo 		wpa_hexdump(MSG_MSGDUMP, "P2P: Channel List",
148f05cddf9SRui Paulo 			    msg->channel_list, msg->channel_list_len);
149f05cddf9SRui Paulo 		break;
150f05cddf9SRui Paulo 	case P2P_ATTR_GROUP_INFO:
151f05cddf9SRui Paulo 		msg->group_info = data;
152f05cddf9SRui Paulo 		msg->group_info_len = len;
153f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Group Info");
154f05cddf9SRui Paulo 		break;
155f05cddf9SRui Paulo 	case P2P_ATTR_DEVICE_INFO:
156f05cddf9SRui Paulo 		if (len < ETH_ALEN + 2 + 8 + 1) {
157f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Device Info "
158f05cddf9SRui Paulo 				   "attribute (length %d)", len);
159f05cddf9SRui Paulo 			return -1;
160f05cddf9SRui Paulo 		}
161f05cddf9SRui Paulo 		msg->p2p_device_info = data;
162f05cddf9SRui Paulo 		msg->p2p_device_info_len = len;
163f05cddf9SRui Paulo 		pos = data;
164f05cddf9SRui Paulo 		msg->p2p_device_addr = pos;
165f05cddf9SRui Paulo 		pos += ETH_ALEN;
166f05cddf9SRui Paulo 		msg->config_methods = WPA_GET_BE16(pos);
167f05cddf9SRui Paulo 		pos += 2;
168f05cddf9SRui Paulo 		msg->pri_dev_type = pos;
169f05cddf9SRui Paulo 		pos += 8;
170f05cddf9SRui Paulo 		msg->num_sec_dev_types = *pos++;
171f05cddf9SRui Paulo 		if (msg->num_sec_dev_types * 8 > data + len - pos) {
172f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Device Info underflow");
173f05cddf9SRui Paulo 			return -1;
174f05cddf9SRui Paulo 		}
175f05cddf9SRui Paulo 		pos += msg->num_sec_dev_types * 8;
176f05cddf9SRui Paulo 		if (data + len - pos < 4) {
177f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Invalid Device Name "
178f05cddf9SRui Paulo 				   "length %d", (int) (data + len - pos));
179f05cddf9SRui Paulo 			return -1;
180f05cddf9SRui Paulo 		}
181f05cddf9SRui Paulo 		if (WPA_GET_BE16(pos) != ATTR_DEV_NAME) {
182f05cddf9SRui Paulo 			wpa_hexdump(MSG_DEBUG, "P2P: Unexpected Device Name "
183f05cddf9SRui Paulo 				    "header", pos, 4);
184f05cddf9SRui Paulo 			return -1;
185f05cddf9SRui Paulo 		}
186f05cddf9SRui Paulo 		pos += 2;
187f05cddf9SRui Paulo 		nlen = WPA_GET_BE16(pos);
188f05cddf9SRui Paulo 		pos += 2;
189780fb4a2SCy Schubert 		if (nlen > data + len - pos || nlen > WPS_DEV_NAME_MAX_LEN) {
190f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Invalid Device Name "
191780fb4a2SCy Schubert 				   "length %u (buf len %d)", nlen,
192f05cddf9SRui Paulo 				   (int) (data + len - pos));
193f05cddf9SRui Paulo 			return -1;
194f05cddf9SRui Paulo 		}
195780fb4a2SCy Schubert 		p2p_copy_filter_devname(msg->device_name,
196780fb4a2SCy Schubert 					sizeof(msg->device_name), pos, nlen);
197f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Device Info: addr " MACSTR
198f05cddf9SRui Paulo 			   " primary device type %s device name '%s' "
199f05cddf9SRui Paulo 			   "config methods 0x%x",
200f05cddf9SRui Paulo 			   MAC2STR(msg->p2p_device_addr),
201f05cddf9SRui Paulo 			   wps_dev_type_bin2str(msg->pri_dev_type, devtype,
202f05cddf9SRui Paulo 						sizeof(devtype)),
203f05cddf9SRui Paulo 			   msg->device_name, msg->config_methods);
204f05cddf9SRui Paulo 		break;
205f05cddf9SRui Paulo 	case P2P_ATTR_CONFIGURATION_TIMEOUT:
206f05cddf9SRui Paulo 		if (len < 2) {
207f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Configuration "
208f05cddf9SRui Paulo 				   "Timeout attribute (length %d)", len);
209f05cddf9SRui Paulo 			return -1;
210f05cddf9SRui Paulo 		}
211f05cddf9SRui Paulo 		msg->config_timeout = data;
212f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout");
213f05cddf9SRui Paulo 		break;
214f05cddf9SRui Paulo 	case P2P_ATTR_INTENDED_INTERFACE_ADDR:
215f05cddf9SRui Paulo 		if (len < ETH_ALEN) {
216f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Intended P2P "
217f05cddf9SRui Paulo 				   "Interface Address attribute (length %d)",
218f05cddf9SRui Paulo 				   len);
219f05cddf9SRui Paulo 			return -1;
220f05cddf9SRui Paulo 		}
221f05cddf9SRui Paulo 		msg->intended_addr = data;
222f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address: "
223f05cddf9SRui Paulo 			   MACSTR, MAC2STR(msg->intended_addr));
224f05cddf9SRui Paulo 		break;
225f05cddf9SRui Paulo 	case P2P_ATTR_GROUP_BSSID:
226f05cddf9SRui Paulo 		if (len < ETH_ALEN) {
227f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short P2P Group BSSID "
228f05cddf9SRui Paulo 				   "attribute (length %d)", len);
229f05cddf9SRui Paulo 			return -1;
230f05cddf9SRui Paulo 		}
231f05cddf9SRui Paulo 		msg->group_bssid = data;
232f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID: " MACSTR,
233f05cddf9SRui Paulo 			   MAC2STR(msg->group_bssid));
234f05cddf9SRui Paulo 		break;
235f05cddf9SRui Paulo 	case P2P_ATTR_GROUP_ID:
236325151a3SRui Paulo 		if (len < ETH_ALEN || len > ETH_ALEN + SSID_MAX_LEN) {
237f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Invalid P2P Group ID "
238f05cddf9SRui Paulo 				   "attribute length %d", len);
239f05cddf9SRui Paulo 			return -1;
240f05cddf9SRui Paulo 		}
241f05cddf9SRui Paulo 		msg->group_id = data;
242f05cddf9SRui Paulo 		msg->group_id_len = len;
243f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * P2P Group ID: Device Address "
244f05cddf9SRui Paulo 			   MACSTR, MAC2STR(msg->group_id));
245f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "P2P: * P2P Group ID: SSID",
246f05cddf9SRui Paulo 				  msg->group_id + ETH_ALEN,
247f05cddf9SRui Paulo 				  msg->group_id_len - ETH_ALEN);
248f05cddf9SRui Paulo 		break;
249f05cddf9SRui Paulo 	case P2P_ATTR_INVITATION_FLAGS:
250f05cddf9SRui Paulo 		if (len < 1) {
251f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Invitation "
252f05cddf9SRui Paulo 				   "Flag attribute (length %d)", len);
253f05cddf9SRui Paulo 			return -1;
254f05cddf9SRui Paulo 		}
255f05cddf9SRui Paulo 		msg->invitation_flags = data;
256f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x",
257f05cddf9SRui Paulo 			   data[0]);
258f05cddf9SRui Paulo 		break;
259f05cddf9SRui Paulo 	case P2P_ATTR_MANAGEABILITY:
260f05cddf9SRui Paulo 		if (len < 1) {
261f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Manageability "
262f05cddf9SRui Paulo 				   "attribute (length %d)", len);
263f05cddf9SRui Paulo 			return -1;
264f05cddf9SRui Paulo 		}
265f05cddf9SRui Paulo 		msg->manageability = data;
266f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Manageability: bitmap 0x%x",
267f05cddf9SRui Paulo 			   data[0]);
268f05cddf9SRui Paulo 		break;
269f05cddf9SRui Paulo 	case P2P_ATTR_NOTICE_OF_ABSENCE:
270f05cddf9SRui Paulo 		if (len < 2) {
271f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Notice of "
272f05cddf9SRui Paulo 				   "Absence attribute (length %d)", len);
273f05cddf9SRui Paulo 			return -1;
274f05cddf9SRui Paulo 		}
275f05cddf9SRui Paulo 		msg->noa = data;
276f05cddf9SRui Paulo 		msg->noa_len = len;
277f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
278f05cddf9SRui Paulo 		break;
279f05cddf9SRui Paulo 	case P2P_ATTR_EXT_LISTEN_TIMING:
280f05cddf9SRui Paulo 		if (len < 4) {
281f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Extended Listen "
282f05cddf9SRui Paulo 				   "Timing attribute (length %d)", len);
283f05cddf9SRui Paulo 			return -1;
284f05cddf9SRui Paulo 		}
285f05cddf9SRui Paulo 		msg->ext_listen_timing = data;
286f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Extended Listen Timing "
287f05cddf9SRui Paulo 			   "(period %u msec  interval %u msec)",
288f05cddf9SRui Paulo 			   WPA_GET_LE16(msg->ext_listen_timing),
289f05cddf9SRui Paulo 			   WPA_GET_LE16(msg->ext_listen_timing + 2));
290f05cddf9SRui Paulo 		break;
291f05cddf9SRui Paulo 	case P2P_ATTR_MINOR_REASON_CODE:
292f05cddf9SRui Paulo 		if (len < 1) {
293f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short Minor Reason "
294f05cddf9SRui Paulo 				   "Code attribute (length %d)", len);
295f05cddf9SRui Paulo 			return -1;
296f05cddf9SRui Paulo 		}
297f05cddf9SRui Paulo 		msg->minor_reason_code = data;
298f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Minor Reason Code: %u",
299f05cddf9SRui Paulo 			   *msg->minor_reason_code);
300f05cddf9SRui Paulo 		break;
3015b9c547cSRui Paulo 	case P2P_ATTR_OOB_GO_NEG_CHANNEL:
3025b9c547cSRui Paulo 		if (len < 6) {
3035b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Too short OOB GO Neg "
3045b9c547cSRui Paulo 				   "Channel attribute (length %d)", len);
3055b9c547cSRui Paulo 			return -1;
3065b9c547cSRui Paulo 		}
3075b9c547cSRui Paulo 		msg->oob_go_neg_channel = data;
3085b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * OOB GO Neg Channel: "
3095b9c547cSRui Paulo 			   "Country %c%c(0x%02x) Operating Class %d "
3105b9c547cSRui Paulo 			   "Channel Number %d Role %d",
3115b9c547cSRui Paulo 			   data[0], data[1], data[2], data[3], data[4],
3125b9c547cSRui Paulo 			   data[5]);
3135b9c547cSRui Paulo 		break;
3145b9c547cSRui Paulo 	case P2P_ATTR_SERVICE_HASH:
3155b9c547cSRui Paulo 		if (len < P2PS_HASH_LEN) {
3165b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
3175b9c547cSRui Paulo 				   "P2P: Too short Service Hash (length %u)",
3185b9c547cSRui Paulo 				   len);
3195b9c547cSRui Paulo 			return -1;
3205b9c547cSRui Paulo 		}
3215b9c547cSRui Paulo 		msg->service_hash_count = len / P2PS_HASH_LEN;
3225b9c547cSRui Paulo 		msg->service_hash = data;
3235b9c547cSRui Paulo 		wpa_hexdump(MSG_DEBUG, "P2P: * Service Hash(s)", data, len);
3245b9c547cSRui Paulo 		break;
3255b9c547cSRui Paulo 	case P2P_ATTR_SESSION_INFORMATION_DATA:
3265b9c547cSRui Paulo 		msg->session_info = data;
3275b9c547cSRui Paulo 		msg->session_info_len = len;
3285b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Service Instance: %u bytes - %p",
3295b9c547cSRui Paulo 			   len, data);
3305b9c547cSRui Paulo 		break;
3315b9c547cSRui Paulo 	case P2P_ATTR_CONNECTION_CAPABILITY:
3325b9c547cSRui Paulo 		if (len < 1) {
3335b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
3345b9c547cSRui Paulo 				   "P2P: Too short Connection Capability (length %u)",
3355b9c547cSRui Paulo 				   len);
3365b9c547cSRui Paulo 			return -1;
3375b9c547cSRui Paulo 		}
3385b9c547cSRui Paulo 		msg->conn_cap = data;
3395b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Connection Capability: 0x%x",
3405b9c547cSRui Paulo 			   *msg->conn_cap);
3415b9c547cSRui Paulo 		break;
3425b9c547cSRui Paulo 	case P2P_ATTR_ADVERTISEMENT_ID:
3435b9c547cSRui Paulo 		if (len < 10) {
3445b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
3455b9c547cSRui Paulo 				   "P2P: Too short Advertisement ID (length %u)",
3465b9c547cSRui Paulo 				   len);
3475b9c547cSRui Paulo 			return -1;
3485b9c547cSRui Paulo 		}
3495b9c547cSRui Paulo 		msg->adv_id = data;
3505b9c547cSRui Paulo 		msg->adv_mac = &data[sizeof(u32)];
3515b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Advertisement ID %x",
3525b9c547cSRui Paulo 			   WPA_GET_LE32(data));
3535b9c547cSRui Paulo 		break;
3545b9c547cSRui Paulo 	case P2P_ATTR_ADVERTISED_SERVICE:
3555b9c547cSRui Paulo 		if (len < 8) {
3565b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
3575b9c547cSRui Paulo 				   "P2P: Too short Service Instance (length %u)",
3585b9c547cSRui Paulo 				   len);
3595b9c547cSRui Paulo 			return -1;
3605b9c547cSRui Paulo 		}
3615b9c547cSRui Paulo 		msg->adv_service_instance = data;
3625b9c547cSRui Paulo 		msg->adv_service_instance_len = len;
3635b9c547cSRui Paulo 		if (len <= 255 + 8) {
3645b9c547cSRui Paulo 			char str[256];
3655b9c547cSRui Paulo 			u8 namelen;
3665b9c547cSRui Paulo 
3675b9c547cSRui Paulo 			namelen = data[6];
3685b9c547cSRui Paulo 			if (namelen > len - 7)
3695b9c547cSRui Paulo 				break;
3705b9c547cSRui Paulo 			os_memcpy(str, &data[7], namelen);
3715b9c547cSRui Paulo 			str[namelen] = '\0';
3725b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: * Service Instance: %x-%s",
3735b9c547cSRui Paulo 				   WPA_GET_LE32(data), str);
3745b9c547cSRui Paulo 		} else {
3755b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: * Service Instance: %p",
3765b9c547cSRui Paulo 				   data);
3775b9c547cSRui Paulo 		}
3785b9c547cSRui Paulo 		break;
3795b9c547cSRui Paulo 	case P2P_ATTR_SESSION_ID:
3805b9c547cSRui Paulo 		if (len < sizeof(u32) + ETH_ALEN) {
3815b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
3825b9c547cSRui Paulo 				   "P2P: Too short Session ID Info (length %u)",
3835b9c547cSRui Paulo 				   len);
3845b9c547cSRui Paulo 			return -1;
3855b9c547cSRui Paulo 		}
3865b9c547cSRui Paulo 		msg->session_id = data;
3875b9c547cSRui Paulo 		msg->session_mac = &data[sizeof(u32)];
3885b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Session ID: %x " MACSTR,
3895b9c547cSRui Paulo 			   WPA_GET_LE32(data), MAC2STR(msg->session_mac));
3905b9c547cSRui Paulo 		break;
3915b9c547cSRui Paulo 	case P2P_ATTR_FEATURE_CAPABILITY:
3925b9c547cSRui Paulo 		if (!len) {
3935b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
3945b9c547cSRui Paulo 				   "P2P: Too short Feature Capability (length %u)",
3955b9c547cSRui Paulo 				   len);
3965b9c547cSRui Paulo 			return -1;
3975b9c547cSRui Paulo 		}
3985b9c547cSRui Paulo 		msg->feature_cap = data;
3995b9c547cSRui Paulo 		msg->feature_cap_len = len;
4005b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Feature Cap (length=%u)", len);
4015b9c547cSRui Paulo 		break;
4025b9c547cSRui Paulo 	case P2P_ATTR_PERSISTENT_GROUP:
4035b9c547cSRui Paulo 	{
404325151a3SRui Paulo 		if (len < ETH_ALEN || len > ETH_ALEN + SSID_MAX_LEN) {
4055b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
406325151a3SRui Paulo 				   "P2P: Invalid Persistent Group Info (length %u)",
4075b9c547cSRui Paulo 				   len);
4085b9c547cSRui Paulo 			return -1;
4095b9c547cSRui Paulo 		}
4105b9c547cSRui Paulo 
4115b9c547cSRui Paulo 		msg->persistent_dev = data;
4125b9c547cSRui Paulo 		msg->persistent_ssid_len = len - ETH_ALEN;
4135b9c547cSRui Paulo 		msg->persistent_ssid = &data[ETH_ALEN];
4145b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: * Persistent Group: " MACSTR " %s",
4155b9c547cSRui Paulo 			   MAC2STR(msg->persistent_dev),
4165b9c547cSRui Paulo 			   wpa_ssid_txt(msg->persistent_ssid,
4175b9c547cSRui Paulo 					msg->persistent_ssid_len));
4185b9c547cSRui Paulo 		break;
4195b9c547cSRui Paulo 	}
420f05cddf9SRui Paulo 	default:
421f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: Skipped unknown attribute %d "
422f05cddf9SRui Paulo 			   "(length %d)", id, len);
423f05cddf9SRui Paulo 		break;
424f05cddf9SRui Paulo 	}
425f05cddf9SRui Paulo 
426f05cddf9SRui Paulo 	return 0;
427f05cddf9SRui Paulo }
428f05cddf9SRui Paulo 
429f05cddf9SRui Paulo 
430f05cddf9SRui Paulo /**
431f05cddf9SRui Paulo  * p2p_parse_p2p_ie - Parse P2P IE
432f05cddf9SRui Paulo  * @buf: Concatenated P2P IE(s) payload
433f05cddf9SRui Paulo  * @msg: Buffer for returning parsed attributes
434f05cddf9SRui Paulo  * Returns: 0 on success, -1 on failure
435f05cddf9SRui Paulo  *
436f05cddf9SRui Paulo  * Note: Caller is responsible for clearing the msg data structure before
437f05cddf9SRui Paulo  * calling this function.
438f05cddf9SRui Paulo  */
p2p_parse_p2p_ie(const struct wpabuf * buf,struct p2p_message * msg)439f05cddf9SRui Paulo int p2p_parse_p2p_ie(const struct wpabuf *buf, struct p2p_message *msg)
440f05cddf9SRui Paulo {
441f05cddf9SRui Paulo 	const u8 *pos = wpabuf_head_u8(buf);
442f05cddf9SRui Paulo 	const u8 *end = pos + wpabuf_len(buf);
443f05cddf9SRui Paulo 
444f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "P2P: Parsing P2P IE");
445f05cddf9SRui Paulo 
446f05cddf9SRui Paulo 	while (pos < end) {
447f05cddf9SRui Paulo 		u16 attr_len;
4485b9c547cSRui Paulo 		u8 id;
4495b9c547cSRui Paulo 
4505b9c547cSRui Paulo 		if (end - pos < 3) {
451f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Invalid P2P attribute");
452f05cddf9SRui Paulo 			return -1;
453f05cddf9SRui Paulo 		}
4545b9c547cSRui Paulo 		id = *pos++;
4555b9c547cSRui Paulo 		attr_len = WPA_GET_LE16(pos);
4565b9c547cSRui Paulo 		pos += 2;
457f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: Attribute %d length %u",
4585b9c547cSRui Paulo 			   id, attr_len);
4595b9c547cSRui Paulo 		if (attr_len > end - pos) {
460f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Attribute underflow "
461f05cddf9SRui Paulo 				   "(len=%u left=%d)",
4625b9c547cSRui Paulo 				   attr_len, (int) (end - pos));
463f05cddf9SRui Paulo 			wpa_hexdump(MSG_MSGDUMP, "P2P: Data", pos, end - pos);
464f05cddf9SRui Paulo 			return -1;
465f05cddf9SRui Paulo 		}
4665b9c547cSRui Paulo 		if (p2p_parse_attribute(id, pos, attr_len, msg))
467f05cddf9SRui Paulo 			return -1;
4685b9c547cSRui Paulo 		pos += attr_len;
469f05cddf9SRui Paulo 	}
470f05cddf9SRui Paulo 
471f05cddf9SRui Paulo 	return 0;
472f05cddf9SRui Paulo }
473f05cddf9SRui Paulo 
474f05cddf9SRui Paulo 
p2p_parse_wps_ie(const struct wpabuf * buf,struct p2p_message * msg)475f05cddf9SRui Paulo static int p2p_parse_wps_ie(const struct wpabuf *buf, struct p2p_message *msg)
476f05cddf9SRui Paulo {
477f05cddf9SRui Paulo 	struct wps_parse_attr attr;
478f05cddf9SRui Paulo 	int i;
479f05cddf9SRui Paulo 
480f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "P2P: Parsing WPS IE");
481f05cddf9SRui Paulo 	if (wps_parse_msg(buf, &attr))
482f05cddf9SRui Paulo 		return -1;
483f05cddf9SRui Paulo 	if (attr.dev_name && attr.dev_name_len < sizeof(msg->device_name) &&
484f05cddf9SRui Paulo 	    !msg->device_name[0])
485f05cddf9SRui Paulo 		os_memcpy(msg->device_name, attr.dev_name, attr.dev_name_len);
486f05cddf9SRui Paulo 	if (attr.config_methods) {
487f05cddf9SRui Paulo 		msg->wps_config_methods =
488f05cddf9SRui Paulo 			WPA_GET_BE16(attr.config_methods);
489f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: Config Methods (WPS): 0x%x",
490f05cddf9SRui Paulo 			   msg->wps_config_methods);
491f05cddf9SRui Paulo 	}
492f05cddf9SRui Paulo 	if (attr.dev_password_id) {
493f05cddf9SRui Paulo 		msg->dev_password_id = WPA_GET_BE16(attr.dev_password_id);
494f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: Device Password ID: %d",
495f05cddf9SRui Paulo 			   msg->dev_password_id);
4965b9c547cSRui Paulo 		msg->dev_password_id_present = 1;
497f05cddf9SRui Paulo 	}
498f05cddf9SRui Paulo 	if (attr.primary_dev_type) {
499f05cddf9SRui Paulo 		char devtype[WPS_DEV_TYPE_BUFSIZE];
500f05cddf9SRui Paulo 		msg->wps_pri_dev_type = attr.primary_dev_type;
501f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: Primary Device Type (WPS): %s",
502f05cddf9SRui Paulo 			   wps_dev_type_bin2str(msg->wps_pri_dev_type, devtype,
503f05cddf9SRui Paulo 						sizeof(devtype)));
504f05cddf9SRui Paulo 	}
505f05cddf9SRui Paulo 	if (attr.sec_dev_type_list) {
506f05cddf9SRui Paulo 		msg->wps_sec_dev_type_list = attr.sec_dev_type_list;
507f05cddf9SRui Paulo 		msg->wps_sec_dev_type_list_len = attr.sec_dev_type_list_len;
508f05cddf9SRui Paulo 	}
509f05cddf9SRui Paulo 
510f05cddf9SRui Paulo 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
511f05cddf9SRui Paulo 		msg->wps_vendor_ext[i] = attr.vendor_ext[i];
512f05cddf9SRui Paulo 		msg->wps_vendor_ext_len[i] = attr.vendor_ext_len[i];
513f05cddf9SRui Paulo 	}
514f05cddf9SRui Paulo 
515f05cddf9SRui Paulo 	msg->manufacturer = attr.manufacturer;
516f05cddf9SRui Paulo 	msg->manufacturer_len = attr.manufacturer_len;
517f05cddf9SRui Paulo 	msg->model_name = attr.model_name;
518f05cddf9SRui Paulo 	msg->model_name_len = attr.model_name_len;
519f05cddf9SRui Paulo 	msg->model_number = attr.model_number;
520f05cddf9SRui Paulo 	msg->model_number_len = attr.model_number_len;
521f05cddf9SRui Paulo 	msg->serial_number = attr.serial_number;
522f05cddf9SRui Paulo 	msg->serial_number_len = attr.serial_number_len;
523f05cddf9SRui Paulo 
5245b9c547cSRui Paulo 	msg->oob_dev_password = attr.oob_dev_password;
5255b9c547cSRui Paulo 	msg->oob_dev_password_len = attr.oob_dev_password_len;
5265b9c547cSRui Paulo 
527f05cddf9SRui Paulo 	return 0;
528f05cddf9SRui Paulo }
529f05cddf9SRui Paulo 
530f05cddf9SRui Paulo 
531f05cddf9SRui Paulo /**
532f05cddf9SRui Paulo  * p2p_parse_ies - Parse P2P message IEs (both WPS and P2P IE)
533f05cddf9SRui Paulo  * @data: IEs from the message
534f05cddf9SRui Paulo  * @len: Length of data buffer in octets
535f05cddf9SRui Paulo  * @msg: Buffer for returning parsed attributes
536f05cddf9SRui Paulo  * Returns: 0 on success, -1 on failure
537f05cddf9SRui Paulo  *
538f05cddf9SRui Paulo  * Note: Caller is responsible for clearing the msg data structure before
539f05cddf9SRui Paulo  * calling this function.
540f05cddf9SRui Paulo  *
541f05cddf9SRui Paulo  * Note: Caller must free temporary memory allocations by calling
542f05cddf9SRui Paulo  * p2p_parse_free() when the parsed data is not needed anymore.
543f05cddf9SRui Paulo  */
p2p_parse_ies(const u8 * data,size_t len,struct p2p_message * msg)544f05cddf9SRui Paulo int p2p_parse_ies(const u8 *data, size_t len, struct p2p_message *msg)
545f05cddf9SRui Paulo {
546f05cddf9SRui Paulo 	struct ieee802_11_elems elems;
547f05cddf9SRui Paulo 
548*a90b9d01SCy Schubert 	if (ieee802_11_parse_elems(data, len, &elems, 0) == ParseFailed)
549*a90b9d01SCy Schubert 		return -1;
550*a90b9d01SCy Schubert 
551325151a3SRui Paulo 	if (elems.ds_params)
552f05cddf9SRui Paulo 		msg->ds_params = elems.ds_params;
553f05cddf9SRui Paulo 	if (elems.ssid)
554f05cddf9SRui Paulo 		msg->ssid = elems.ssid - 2;
555f05cddf9SRui Paulo 
556f05cddf9SRui Paulo 	msg->wps_attributes = ieee802_11_vendor_ie_concat(data, len,
557f05cddf9SRui Paulo 							  WPS_DEV_OUI_WFA);
558f05cddf9SRui Paulo 	if (msg->wps_attributes &&
559f05cddf9SRui Paulo 	    p2p_parse_wps_ie(msg->wps_attributes, msg)) {
560f05cddf9SRui Paulo 		p2p_parse_free(msg);
561f05cddf9SRui Paulo 		return -1;
562f05cddf9SRui Paulo 	}
563f05cddf9SRui Paulo 
564f05cddf9SRui Paulo 	msg->p2p_attributes = ieee802_11_vendor_ie_concat(data, len,
565f05cddf9SRui Paulo 							  P2P_IE_VENDOR_TYPE);
566f05cddf9SRui Paulo 	if (msg->p2p_attributes &&
567f05cddf9SRui Paulo 	    p2p_parse_p2p_ie(msg->p2p_attributes, msg)) {
568f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: Failed to parse P2P IE data");
569f05cddf9SRui Paulo 		if (msg->p2p_attributes)
570f05cddf9SRui Paulo 			wpa_hexdump_buf(MSG_MSGDUMP, "P2P: P2P IE data",
571f05cddf9SRui Paulo 					msg->p2p_attributes);
572f05cddf9SRui Paulo 		p2p_parse_free(msg);
573f05cddf9SRui Paulo 		return -1;
574f05cddf9SRui Paulo 	}
575f05cddf9SRui Paulo 
576f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
577f05cddf9SRui Paulo 	if (elems.wfd) {
578f05cddf9SRui Paulo 		msg->wfd_subelems = ieee802_11_vendor_ie_concat(
579f05cddf9SRui Paulo 			data, len, WFD_IE_VENDOR_TYPE);
580f05cddf9SRui Paulo 	}
581f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
582f05cddf9SRui Paulo 
583325151a3SRui Paulo 	msg->pref_freq_list = elems.pref_freq_list;
584325151a3SRui Paulo 	msg->pref_freq_list_len = elems.pref_freq_list_len;
585325151a3SRui Paulo 
586f05cddf9SRui Paulo 	return 0;
587f05cddf9SRui Paulo }
588f05cddf9SRui Paulo 
589f05cddf9SRui Paulo 
590f05cddf9SRui Paulo /**
591f05cddf9SRui Paulo  * p2p_parse - Parse a P2P Action frame contents
592f05cddf9SRui Paulo  * @data: Action frame payload after Category and Code fields
593f05cddf9SRui Paulo  * @len: Length of data buffer in octets
594f05cddf9SRui Paulo  * @msg: Buffer for returning parsed attributes
595f05cddf9SRui Paulo  * Returns: 0 on success, -1 on failure
596f05cddf9SRui Paulo  *
597f05cddf9SRui Paulo  * Note: Caller must free temporary memory allocations by calling
598f05cddf9SRui Paulo  * p2p_parse_free() when the parsed data is not needed anymore.
599f05cddf9SRui Paulo  */
p2p_parse(const u8 * data,size_t len,struct p2p_message * msg)600f05cddf9SRui Paulo int p2p_parse(const u8 *data, size_t len, struct p2p_message *msg)
601f05cddf9SRui Paulo {
602f05cddf9SRui Paulo 	os_memset(msg, 0, sizeof(*msg));
603f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "P2P: Parsing the received message");
604f05cddf9SRui Paulo 	if (len < 1) {
605f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: No Dialog Token in the message");
606f05cddf9SRui Paulo 		return -1;
607f05cddf9SRui Paulo 	}
608f05cddf9SRui Paulo 	msg->dialog_token = data[0];
609f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", msg->dialog_token);
610f05cddf9SRui Paulo 
611f05cddf9SRui Paulo 	return p2p_parse_ies(data + 1, len - 1, msg);
612f05cddf9SRui Paulo }
613f05cddf9SRui Paulo 
614f05cddf9SRui Paulo 
p2p_parse_ies_separate(const u8 * wsc,size_t wsc_len,const u8 * p2p,size_t p2p_len,struct p2p_message * msg)6155b9c547cSRui Paulo int p2p_parse_ies_separate(const u8 *wsc, size_t wsc_len, const u8 *p2p,
6165b9c547cSRui Paulo 			   size_t p2p_len, struct p2p_message *msg)
6175b9c547cSRui Paulo {
6185b9c547cSRui Paulo 	os_memset(msg, 0, sizeof(*msg));
6195b9c547cSRui Paulo 
6205b9c547cSRui Paulo 	msg->wps_attributes = wpabuf_alloc_copy(wsc, wsc_len);
6215b9c547cSRui Paulo 	if (msg->wps_attributes &&
6225b9c547cSRui Paulo 	    p2p_parse_wps_ie(msg->wps_attributes, msg)) {
6235b9c547cSRui Paulo 		p2p_parse_free(msg);
6245b9c547cSRui Paulo 		return -1;
6255b9c547cSRui Paulo 	}
6265b9c547cSRui Paulo 
6275b9c547cSRui Paulo 	msg->p2p_attributes = wpabuf_alloc_copy(p2p, p2p_len);
6285b9c547cSRui Paulo 	if (msg->p2p_attributes &&
6295b9c547cSRui Paulo 	    p2p_parse_p2p_ie(msg->p2p_attributes, msg)) {
6305b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: Failed to parse P2P IE data");
6315b9c547cSRui Paulo 		if (msg->p2p_attributes)
6325b9c547cSRui Paulo 			wpa_hexdump_buf(MSG_MSGDUMP, "P2P: P2P IE data",
6335b9c547cSRui Paulo 					msg->p2p_attributes);
6345b9c547cSRui Paulo 		p2p_parse_free(msg);
6355b9c547cSRui Paulo 		return -1;
6365b9c547cSRui Paulo 	}
6375b9c547cSRui Paulo 
6385b9c547cSRui Paulo 	return 0;
6395b9c547cSRui Paulo }
6405b9c547cSRui Paulo 
6415b9c547cSRui Paulo 
642f05cddf9SRui Paulo /**
643f05cddf9SRui Paulo  * p2p_parse_free - Free temporary data from P2P parsing
644f05cddf9SRui Paulo  * @msg: Parsed attributes
645f05cddf9SRui Paulo  */
p2p_parse_free(struct p2p_message * msg)646f05cddf9SRui Paulo void p2p_parse_free(struct p2p_message *msg)
647f05cddf9SRui Paulo {
648f05cddf9SRui Paulo 	wpabuf_free(msg->p2p_attributes);
649f05cddf9SRui Paulo 	msg->p2p_attributes = NULL;
650f05cddf9SRui Paulo 	wpabuf_free(msg->wps_attributes);
651f05cddf9SRui Paulo 	msg->wps_attributes = NULL;
652f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
653f05cddf9SRui Paulo 	wpabuf_free(msg->wfd_subelems);
654f05cddf9SRui Paulo 	msg->wfd_subelems = NULL;
655f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
656f05cddf9SRui Paulo }
657f05cddf9SRui Paulo 
658f05cddf9SRui Paulo 
p2p_group_info_parse(const u8 * gi,size_t gi_len,struct p2p_group_info * info)659f05cddf9SRui Paulo int p2p_group_info_parse(const u8 *gi, size_t gi_len,
660f05cddf9SRui Paulo 			 struct p2p_group_info *info)
661f05cddf9SRui Paulo {
662f05cddf9SRui Paulo 	const u8 *g, *gend;
663f05cddf9SRui Paulo 
664f05cddf9SRui Paulo 	os_memset(info, 0, sizeof(*info));
665f05cddf9SRui Paulo 	if (gi == NULL)
666f05cddf9SRui Paulo 		return 0;
667f05cddf9SRui Paulo 
668f05cddf9SRui Paulo 	g = gi;
669f05cddf9SRui Paulo 	gend = gi + gi_len;
670f05cddf9SRui Paulo 	while (g < gend) {
671f05cddf9SRui Paulo 		struct p2p_client_info *cli;
672780fb4a2SCy Schubert 		const u8 *cend;
673780fb4a2SCy Schubert 		u16 count;
674780fb4a2SCy Schubert 		u8 len;
675f05cddf9SRui Paulo 
676f05cddf9SRui Paulo 		cli = &info->client[info->num_clients];
677780fb4a2SCy Schubert 		len = *g++;
678780fb4a2SCy Schubert 		if (len > gend - g || len < 2 * ETH_ALEN + 1 + 2 + 8 + 1)
679f05cddf9SRui Paulo 			return -1; /* invalid data */
680780fb4a2SCy Schubert 		cend = g + len;
681f05cddf9SRui Paulo 		/* g at start of P2P Client Info Descriptor */
682780fb4a2SCy Schubert 		cli->p2p_device_addr = g;
683780fb4a2SCy Schubert 		g += ETH_ALEN;
684780fb4a2SCy Schubert 		cli->p2p_interface_addr = g;
685780fb4a2SCy Schubert 		g += ETH_ALEN;
686780fb4a2SCy Schubert 		cli->dev_capab = *g++;
687f05cddf9SRui Paulo 
688780fb4a2SCy Schubert 		cli->config_methods = WPA_GET_BE16(g);
689780fb4a2SCy Schubert 		g += 2;
690780fb4a2SCy Schubert 		cli->pri_dev_type = g;
691780fb4a2SCy Schubert 		g += 8;
692f05cddf9SRui Paulo 
693780fb4a2SCy Schubert 		/* g at Number of Secondary Device Types */
694780fb4a2SCy Schubert 		len = *g++;
695780fb4a2SCy Schubert 		if (8 * len > cend - g)
696f05cddf9SRui Paulo 			return -1; /* invalid data */
697780fb4a2SCy Schubert 		cli->num_sec_dev_types = len;
698780fb4a2SCy Schubert 		cli->sec_dev_types = g;
699780fb4a2SCy Schubert 		g += 8 * len;
700f05cddf9SRui Paulo 
701780fb4a2SCy Schubert 		/* g at Device Name in WPS TLV format */
702780fb4a2SCy Schubert 		if (cend - g < 2 + 2)
703f05cddf9SRui Paulo 			return -1; /* invalid data */
704780fb4a2SCy Schubert 		if (WPA_GET_BE16(g) != ATTR_DEV_NAME)
705f05cddf9SRui Paulo 			return -1; /* invalid Device Name TLV */
706780fb4a2SCy Schubert 		g += 2;
707780fb4a2SCy Schubert 		count = WPA_GET_BE16(g);
708780fb4a2SCy Schubert 		g += 2;
709780fb4a2SCy Schubert 		if (count > cend - g)
710f05cddf9SRui Paulo 			return -1; /* invalid Device Name TLV */
711325151a3SRui Paulo 		if (count >= WPS_DEV_NAME_MAX_LEN)
712325151a3SRui Paulo 			count = WPS_DEV_NAME_MAX_LEN;
713780fb4a2SCy Schubert 		cli->dev_name = (const char *) g;
714f05cddf9SRui Paulo 		cli->dev_name_len = count;
715f05cddf9SRui Paulo 
716f05cddf9SRui Paulo 		g = cend;
717f05cddf9SRui Paulo 
718f05cddf9SRui Paulo 		info->num_clients++;
719f05cddf9SRui Paulo 		if (info->num_clients == P2P_MAX_GROUP_ENTRIES)
720f05cddf9SRui Paulo 			return -1;
721f05cddf9SRui Paulo 	}
722f05cddf9SRui Paulo 
723f05cddf9SRui Paulo 	return 0;
724f05cddf9SRui Paulo }
725f05cddf9SRui Paulo 
726f05cddf9SRui Paulo 
p2p_group_info_text(const u8 * gi,size_t gi_len,char * buf,char * end)727f05cddf9SRui Paulo static int p2p_group_info_text(const u8 *gi, size_t gi_len, char *buf,
728f05cddf9SRui Paulo 			       char *end)
729f05cddf9SRui Paulo {
730f05cddf9SRui Paulo 	char *pos = buf;
731f05cddf9SRui Paulo 	int ret;
732f05cddf9SRui Paulo 	struct p2p_group_info info;
733f05cddf9SRui Paulo 	unsigned int i;
734f05cddf9SRui Paulo 
735f05cddf9SRui Paulo 	if (p2p_group_info_parse(gi, gi_len, &info) < 0)
736f05cddf9SRui Paulo 		return 0;
737f05cddf9SRui Paulo 
738f05cddf9SRui Paulo 	for (i = 0; i < info.num_clients; i++) {
739f05cddf9SRui Paulo 		struct p2p_client_info *cli;
740325151a3SRui Paulo 		char name[WPS_DEV_NAME_MAX_LEN + 1];
741f05cddf9SRui Paulo 		char devtype[WPS_DEV_TYPE_BUFSIZE];
742f05cddf9SRui Paulo 		u8 s;
743f05cddf9SRui Paulo 		int count;
744f05cddf9SRui Paulo 
745f05cddf9SRui Paulo 		cli = &info.client[i];
746f05cddf9SRui Paulo 		ret = os_snprintf(pos, end - pos, "p2p_group_client: "
747f05cddf9SRui Paulo 				  "dev=" MACSTR " iface=" MACSTR,
748f05cddf9SRui Paulo 				  MAC2STR(cli->p2p_device_addr),
749f05cddf9SRui Paulo 				  MAC2STR(cli->p2p_interface_addr));
7505b9c547cSRui Paulo 		if (os_snprintf_error(end - pos, ret))
751f05cddf9SRui Paulo 			return pos - buf;
752f05cddf9SRui Paulo 		pos += ret;
753f05cddf9SRui Paulo 
754f05cddf9SRui Paulo 		ret = os_snprintf(pos, end - pos,
755f05cddf9SRui Paulo 				  " dev_capab=0x%x config_methods=0x%x "
756f05cddf9SRui Paulo 				  "dev_type=%s",
757f05cddf9SRui Paulo 				  cli->dev_capab, cli->config_methods,
758f05cddf9SRui Paulo 				  wps_dev_type_bin2str(cli->pri_dev_type,
759f05cddf9SRui Paulo 						       devtype,
760f05cddf9SRui Paulo 						       sizeof(devtype)));
7615b9c547cSRui Paulo 		if (os_snprintf_error(end - pos, ret))
762f05cddf9SRui Paulo 			return pos - buf;
763f05cddf9SRui Paulo 		pos += ret;
764f05cddf9SRui Paulo 
765f05cddf9SRui Paulo 		for (s = 0; s < cli->num_sec_dev_types; s++) {
766f05cddf9SRui Paulo 			ret = os_snprintf(pos, end - pos, " dev_type=%s",
767f05cddf9SRui Paulo 					  wps_dev_type_bin2str(
768f05cddf9SRui Paulo 						  &cli->sec_dev_types[s * 8],
769f05cddf9SRui Paulo 						  devtype, sizeof(devtype)));
7705b9c547cSRui Paulo 			if (os_snprintf_error(end - pos, ret))
771f05cddf9SRui Paulo 				return pos - buf;
772f05cddf9SRui Paulo 			pos += ret;
773f05cddf9SRui Paulo 		}
774f05cddf9SRui Paulo 
775f05cddf9SRui Paulo 		os_memcpy(name, cli->dev_name, cli->dev_name_len);
776f05cddf9SRui Paulo 		name[cli->dev_name_len] = '\0';
777f05cddf9SRui Paulo 		count = (int) cli->dev_name_len - 1;
778f05cddf9SRui Paulo 		while (count >= 0) {
779325151a3SRui Paulo 			if (is_ctrl_char(name[count]))
780f05cddf9SRui Paulo 				name[count] = '_';
781f05cddf9SRui Paulo 			count--;
782f05cddf9SRui Paulo 		}
783f05cddf9SRui Paulo 
784f05cddf9SRui Paulo 		ret = os_snprintf(pos, end - pos, " dev_name='%s'\n", name);
7855b9c547cSRui Paulo 		if (os_snprintf_error(end - pos, ret))
786f05cddf9SRui Paulo 			return pos - buf;
787f05cddf9SRui Paulo 		pos += ret;
788f05cddf9SRui Paulo 	}
789f05cddf9SRui Paulo 
790f05cddf9SRui Paulo 	return pos - buf;
791f05cddf9SRui Paulo }
792f05cddf9SRui Paulo 
793f05cddf9SRui Paulo 
794f05cddf9SRui Paulo /**
795f05cddf9SRui Paulo  * p2p_attr_text - Build text format description of P2P IE attributes
796f05cddf9SRui Paulo  * @data: P2P IE contents
797f05cddf9SRui Paulo  * @buf: Buffer for returning text
798f05cddf9SRui Paulo  * @end: Pointer to the end of the buf area
799f05cddf9SRui Paulo  * Returns: Number of octets written to the buffer or -1 on faikure
800f05cddf9SRui Paulo  *
801f05cddf9SRui Paulo  * This function can be used to parse P2P IE contents into text format
802f05cddf9SRui Paulo  * field=value lines.
803f05cddf9SRui Paulo  */
p2p_attr_text(struct wpabuf * data,char * buf,char * end)804f05cddf9SRui Paulo int p2p_attr_text(struct wpabuf *data, char *buf, char *end)
805f05cddf9SRui Paulo {
806f05cddf9SRui Paulo 	struct p2p_message msg;
807f05cddf9SRui Paulo 	char *pos = buf;
808f05cddf9SRui Paulo 	int ret;
809f05cddf9SRui Paulo 
810f05cddf9SRui Paulo 	os_memset(&msg, 0, sizeof(msg));
811f05cddf9SRui Paulo 	if (p2p_parse_p2p_ie(data, &msg))
812f05cddf9SRui Paulo 		return -1;
813f05cddf9SRui Paulo 
814f05cddf9SRui Paulo 	if (msg.capability) {
815f05cddf9SRui Paulo 		ret = os_snprintf(pos, end - pos,
816f05cddf9SRui Paulo 				  "p2p_dev_capab=0x%x\n"
817f05cddf9SRui Paulo 				  "p2p_group_capab=0x%x\n",
818f05cddf9SRui Paulo 				  msg.capability[0], msg.capability[1]);
8195b9c547cSRui Paulo 		if (os_snprintf_error(end - pos, ret))
820f05cddf9SRui Paulo 			return pos - buf;
821f05cddf9SRui Paulo 		pos += ret;
822f05cddf9SRui Paulo 	}
823f05cddf9SRui Paulo 
824f05cddf9SRui Paulo 	if (msg.pri_dev_type) {
825f05cddf9SRui Paulo 		char devtype[WPS_DEV_TYPE_BUFSIZE];
826f05cddf9SRui Paulo 		ret = os_snprintf(pos, end - pos,
827f05cddf9SRui Paulo 				  "p2p_primary_device_type=%s\n",
828f05cddf9SRui Paulo 				  wps_dev_type_bin2str(msg.pri_dev_type,
829f05cddf9SRui Paulo 						       devtype,
830f05cddf9SRui Paulo 						       sizeof(devtype)));
8315b9c547cSRui Paulo 		if (os_snprintf_error(end - pos, ret))
832f05cddf9SRui Paulo 			return pos - buf;
833f05cddf9SRui Paulo 		pos += ret;
834f05cddf9SRui Paulo 	}
835f05cddf9SRui Paulo 
836f05cddf9SRui Paulo 	ret = os_snprintf(pos, end - pos, "p2p_device_name=%s\n",
837f05cddf9SRui Paulo 			  msg.device_name);
8385b9c547cSRui Paulo 	if (os_snprintf_error(end - pos, ret))
839f05cddf9SRui Paulo 		return pos - buf;
840f05cddf9SRui Paulo 	pos += ret;
841f05cddf9SRui Paulo 
842f05cddf9SRui Paulo 	if (msg.p2p_device_addr) {
843f05cddf9SRui Paulo 		ret = os_snprintf(pos, end - pos, "p2p_device_addr=" MACSTR
844f05cddf9SRui Paulo 				  "\n",
845f05cddf9SRui Paulo 				  MAC2STR(msg.p2p_device_addr));
8465b9c547cSRui Paulo 		if (os_snprintf_error(end - pos, ret))
847f05cddf9SRui Paulo 			return pos - buf;
848f05cddf9SRui Paulo 		pos += ret;
849f05cddf9SRui Paulo 	}
850f05cddf9SRui Paulo 
851f05cddf9SRui Paulo 	ret = os_snprintf(pos, end - pos, "p2p_config_methods=0x%x\n",
852f05cddf9SRui Paulo 			  msg.config_methods);
8535b9c547cSRui Paulo 	if (os_snprintf_error(end - pos, ret))
854f05cddf9SRui Paulo 		return pos - buf;
855f05cddf9SRui Paulo 	pos += ret;
856f05cddf9SRui Paulo 
857f05cddf9SRui Paulo 	ret = p2p_group_info_text(msg.group_info, msg.group_info_len,
858f05cddf9SRui Paulo 				  pos, end);
859f05cddf9SRui Paulo 	if (ret < 0)
860f05cddf9SRui Paulo 		return pos - buf;
861f05cddf9SRui Paulo 	pos += ret;
862f05cddf9SRui Paulo 
863f05cddf9SRui Paulo 	return pos - buf;
864f05cddf9SRui Paulo }
865f05cddf9SRui Paulo 
866f05cddf9SRui Paulo 
p2p_get_cross_connect_disallowed(const struct wpabuf * p2p_ie)867f05cddf9SRui Paulo int p2p_get_cross_connect_disallowed(const struct wpabuf *p2p_ie)
868f05cddf9SRui Paulo {
869f05cddf9SRui Paulo 	struct p2p_message msg;
870f05cddf9SRui Paulo 
871f05cddf9SRui Paulo 	os_memset(&msg, 0, sizeof(msg));
872f05cddf9SRui Paulo 	if (p2p_parse_p2p_ie(p2p_ie, &msg))
873f05cddf9SRui Paulo 		return 0;
874f05cddf9SRui Paulo 
875f05cddf9SRui Paulo 	if (!msg.manageability)
876f05cddf9SRui Paulo 		return 0;
877f05cddf9SRui Paulo 
878f05cddf9SRui Paulo 	return !(msg.manageability[0] & P2P_MAN_CROSS_CONNECTION_PERMITTED);
879f05cddf9SRui Paulo }
880f05cddf9SRui Paulo 
881f05cddf9SRui Paulo 
p2p_get_group_capab(const struct wpabuf * p2p_ie)882f05cddf9SRui Paulo u8 p2p_get_group_capab(const struct wpabuf *p2p_ie)
883f05cddf9SRui Paulo {
884f05cddf9SRui Paulo 	struct p2p_message msg;
885f05cddf9SRui Paulo 
886f05cddf9SRui Paulo 	os_memset(&msg, 0, sizeof(msg));
887f05cddf9SRui Paulo 	if (p2p_parse_p2p_ie(p2p_ie, &msg))
888f05cddf9SRui Paulo 		return 0;
889f05cddf9SRui Paulo 
890f05cddf9SRui Paulo 	if (!msg.capability)
891f05cddf9SRui Paulo 		return 0;
892f05cddf9SRui Paulo 
893f05cddf9SRui Paulo 	return msg.capability[1];
894f05cddf9SRui Paulo }
895f05cddf9SRui Paulo 
896f05cddf9SRui Paulo 
p2p_get_go_dev_addr(const struct wpabuf * p2p_ie)897f05cddf9SRui Paulo const u8 * p2p_get_go_dev_addr(const struct wpabuf *p2p_ie)
898f05cddf9SRui Paulo {
899f05cddf9SRui Paulo 	struct p2p_message msg;
900f05cddf9SRui Paulo 
901f05cddf9SRui Paulo 	os_memset(&msg, 0, sizeof(msg));
902f05cddf9SRui Paulo 	if (p2p_parse_p2p_ie(p2p_ie, &msg))
903f05cddf9SRui Paulo 		return NULL;
904f05cddf9SRui Paulo 
905f05cddf9SRui Paulo 	if (msg.p2p_device_addr)
906f05cddf9SRui Paulo 		return msg.p2p_device_addr;
907f05cddf9SRui Paulo 	if (msg.device_id)
908f05cddf9SRui Paulo 		return msg.device_id;
909f05cddf9SRui Paulo 
910f05cddf9SRui Paulo 	return NULL;
911f05cddf9SRui Paulo }
912