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