2 * wpa_supplicant - P2P service discovery
3 * Copyright (c) 2009-2010, Atheros Communications
4 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
10 #include "utils/includes.h"
12 #include "utils/common.h"
14 #include "wpa_supplicant_i.h"
16 #include "p2p_supplicant.h"
20 * DNS Header section is used only to calculate compression pointers, so the
21 * contents of this data does not matter, but the length needs to be reserved
22 * in the virtual packet.
24 #define DNS_HEADER_LEN 12
27 * 27-octet in-memory packet from P2P specification containing two implied
28 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
30 #define P2P_SD_IN_MEMORY_LEN 27
32 static int p2p_sd_dns_uncompress_label(char **upos
, char *uend
, u8
*start
,
33 u8
**spos
, const u8
*end
)
36 u8 val
= ((*spos
)[0] & 0xc0) >> 6;
39 if (val
== 1 || val
== 2) {
40 /* These are reserved values in RFC 1035 */
41 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
42 "sequence starting with 0x%x", val
);
51 if (end
- *spos
< 2) {
52 wpa_printf(MSG_DEBUG
, "P2P: No room for full "
57 offset
= (((*spos
)[0] & 0x3f) << 8) | (*spos
)[1];
58 if (offset
>= *spos
- start
) {
59 wpa_printf(MSG_DEBUG
, "P2P: Invalid DNS "
60 "pointer offset %u", offset
);
65 spos_tmp
= start
+ offset
;
66 return p2p_sd_dns_uncompress_label(upos
, uend
, start
,
72 len
= (*spos
)[0] & 0x3f;
77 if (len
> end
- *spos
) {
78 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
79 "sequence - no room for label with length "
84 if (len
+ 2 > uend
- *upos
)
87 os_memcpy(*upos
, *spos
, len
);
99 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
100 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
101 * not large enough */
102 static int p2p_sd_dns_uncompress(char *buf
, size_t buf_len
, const u8
*msg
,
103 size_t msg_len
, size_t offset
)
105 /* 27-octet in-memory packet from P2P specification */
106 const char *prefix
= "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
107 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
108 u8
*tmp
, *end
, *spos
;
114 if (offset
> msg_len
)
117 tmp
= os_malloc(DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
+ msg_len
);
120 spos
= tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
;
121 end
= spos
+ msg_len
;
124 os_memset(tmp
, 0, DNS_HEADER_LEN
);
125 os_memcpy(tmp
+ DNS_HEADER_LEN
, prefix
, P2P_SD_IN_MEMORY_LEN
);
126 os_memcpy(tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
, msg
, msg_len
);
129 uend
= buf
+ buf_len
;
131 ret
= p2p_sd_dns_uncompress_label(&upos
, uend
, tmp
, &spos
, end
);
140 } else if (upos
[-1] == '.')
148 static struct p2p_srv_bonjour
*
149 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
150 const struct wpabuf
*query
)
152 struct p2p_srv_bonjour
*bsrv
;
155 len
= wpabuf_len(query
);
156 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
157 struct p2p_srv_bonjour
, list
) {
158 if (len
== wpabuf_len(bsrv
->query
) &&
159 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
167 static struct p2p_srv_upnp
*
168 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
171 struct p2p_srv_upnp
*usrv
;
173 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
174 struct p2p_srv_upnp
, list
) {
175 if (version
== usrv
->version
&&
176 os_strcmp(service
, usrv
->service
) == 0)
183 static void wpas_sd_add_empty(struct wpabuf
*resp
, u8 srv_proto
,
184 u8 srv_trans_id
, u8 status
)
188 if (wpabuf_tailroom(resp
) < 5)
191 /* Length (to be filled) */
192 len_pos
= wpabuf_put(resp
, 2);
193 wpabuf_put_u8(resp
, srv_proto
);
194 wpabuf_put_u8(resp
, srv_trans_id
);
196 wpabuf_put_u8(resp
, status
);
197 /* Response Data: empty */
198 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
202 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
205 wpas_sd_add_empty(resp
, srv_proto
, srv_trans_id
,
206 P2P_SD_PROTO_NOT_AVAILABLE
);
210 static void wpas_sd_add_bad_request(struct wpabuf
*resp
, u8 srv_proto
,
213 wpas_sd_add_empty(resp
, srv_proto
, srv_trans_id
, P2P_SD_BAD_REQUEST
);
217 static void wpas_sd_add_not_found(struct wpabuf
*resp
, u8 srv_proto
,
220 wpas_sd_add_empty(resp
, srv_proto
, srv_trans_id
,
221 P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
225 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
226 struct wpabuf
*resp
, u8 srv_trans_id
)
228 struct p2p_srv_bonjour
*bsrv
;
231 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
233 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
234 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
238 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
239 struct p2p_srv_bonjour
, list
) {
240 if (wpabuf_tailroom(resp
) <
241 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
243 /* Length (to be filled) */
244 len_pos
= wpabuf_put(resp
, 2);
245 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
246 wpabuf_put_u8(resp
, srv_trans_id
);
248 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
249 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
250 wpabuf_head(bsrv
->resp
),
251 wpabuf_len(bsrv
->resp
));
253 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
254 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
255 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
261 static int match_bonjour_query(struct p2p_srv_bonjour
*bsrv
, const u8
*query
,
264 char str_rx
[256], str_srv
[256];
266 if (query_len
< 3 || wpabuf_len(bsrv
->query
) < 3)
267 return 0; /* Too short to include DNS Type and Version */
268 if (os_memcmp(query
+ query_len
- 3,
269 wpabuf_head_u8(bsrv
->query
) + wpabuf_len(bsrv
->query
) - 3,
271 return 0; /* Mismatch in DNS Type or Version */
272 if (query_len
== wpabuf_len(bsrv
->query
) &&
273 os_memcmp(query
, wpabuf_head(bsrv
->query
), query_len
- 3) == 0)
274 return 1; /* Binary match */
276 if (p2p_sd_dns_uncompress(str_rx
, sizeof(str_rx
), query
, query_len
- 3,
278 return 0; /* Failed to uncompress query */
279 if (p2p_sd_dns_uncompress(str_srv
, sizeof(str_srv
),
280 wpabuf_head(bsrv
->query
),
281 wpabuf_len(bsrv
->query
) - 3, 0))
282 return 0; /* Failed to uncompress service */
284 return os_strcmp(str_rx
, str_srv
) == 0;
288 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
289 struct wpabuf
*resp
, u8 srv_trans_id
,
290 const u8
*query
, size_t query_len
)
292 struct p2p_srv_bonjour
*bsrv
;
296 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
298 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
299 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
300 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
305 if (query_len
== 0) {
306 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
310 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
311 struct p2p_srv_bonjour
, list
) {
312 if (!match_bonjour_query(bsrv
, query
, query_len
))
315 if (wpabuf_tailroom(resp
) <
316 5 + query_len
+ wpabuf_len(bsrv
->resp
))
321 /* Length (to be filled) */
322 len_pos
= wpabuf_put(resp
, 2);
323 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
324 wpabuf_put_u8(resp
, srv_trans_id
);
327 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
328 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
329 wpabuf_head(bsrv
->resp
),
330 wpabuf_len(bsrv
->resp
));
333 wpabuf_put_data(resp
, query
, query_len
); /* Key */
334 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
336 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
340 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
342 if (wpabuf_tailroom(resp
) < 5)
345 /* Length (to be filled) */
346 len_pos
= wpabuf_put(resp
, 2);
347 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
348 wpabuf_put_u8(resp
, srv_trans_id
);
351 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
352 /* Response Data: empty */
353 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
359 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
360 struct wpabuf
*resp
, u8 srv_trans_id
)
362 struct p2p_srv_upnp
*usrv
;
365 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
367 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
368 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
372 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
373 struct p2p_srv_upnp
, list
) {
374 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
377 /* Length (to be filled) */
378 len_pos
= wpabuf_put(resp
, 2);
379 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
380 wpabuf_put_u8(resp
, srv_trans_id
);
383 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
385 wpabuf_put_u8(resp
, usrv
->version
);
386 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
388 wpabuf_put_str(resp
, usrv
->service
);
389 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
395 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
396 struct wpabuf
*resp
, u8 srv_trans_id
,
397 const u8
*query
, size_t query_len
)
399 struct p2p_srv_upnp
*usrv
;
405 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
408 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
409 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
410 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
415 if (query_len
== 0) {
416 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
420 if (wpabuf_tailroom(resp
) < 5)
423 /* Length (to be filled) */
424 len_pos
= wpabuf_put(resp
, 2);
425 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
426 wpabuf_put_u8(resp
, srv_trans_id
);
429 str
= os_malloc(query_len
);
432 os_memcpy(str
, query
+ 1, query_len
- 1);
433 str
[query_len
- 1] = '\0';
435 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
436 struct p2p_srv_upnp
, list
) {
437 if (version
!= usrv
->version
)
440 if (os_strcmp(str
, "ssdp:all") != 0 &&
441 os_strstr(usrv
->service
, str
) == NULL
)
444 if (wpabuf_tailroom(resp
) < 2)
448 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
450 wpabuf_put_u8(resp
, version
);
452 wpabuf_put_u8(resp
, ',');
456 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
458 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
460 wpabuf_put_str(resp
, usrv
->service
);
465 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
468 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
469 /* Response Data: empty */
472 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
476 #ifdef CONFIG_WIFI_DISPLAY
477 static void wpas_sd_req_wfd(struct wpa_supplicant
*wpa_s
,
478 struct wpabuf
*resp
, u8 srv_trans_id
,
479 const u8
*query
, size_t query_len
)
485 wpa_hexdump(MSG_DEBUG
, "P2P: SD Request for WFD", query
, query_len
);
487 if (!wpa_s
->global
->wifi_display
) {
488 wpa_printf(MSG_DEBUG
, "P2P: WFD protocol not available");
489 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_WIFI_DISPLAY
,
495 wpa_printf(MSG_DEBUG
, "P2P: Missing WFD Requested Device "
500 if (wpabuf_tailroom(resp
) < 5)
505 wpa_printf(MSG_DEBUG
, "P2P: WSD for device role 0x%x", role
);
507 /* TODO: role specific handling */
509 /* Length (to be filled) */
510 len_pos
= wpabuf_put(resp
, 2);
511 wpabuf_put_u8(resp
, P2P_SERV_WIFI_DISPLAY
);
512 wpabuf_put_u8(resp
, srv_trans_id
);
513 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
); /* Status Code */
515 while (pos
< query
+ query_len
) {
516 if (*pos
< MAX_WFD_SUBELEMS
&&
517 wpa_s
->global
->wfd_subelem
[*pos
] &&
518 wpabuf_tailroom(resp
) >=
519 wpabuf_len(wpa_s
->global
->wfd_subelem
[*pos
])) {
520 wpa_printf(MSG_DEBUG
, "P2P: Add WSD response "
521 "subelement %u", *pos
);
522 wpabuf_put_buf(resp
, wpa_s
->global
->wfd_subelem
[*pos
]);
527 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
529 #endif /* CONFIG_WIFI_DISPLAY */
532 static int find_p2ps_substr(struct p2ps_advertisement
*adv_data
,
533 const u8
*needle
, size_t needle_len
)
535 const u8
*haystack
= (const u8
*) adv_data
->svc_info
;
536 size_t haystack_len
, i
;
538 /* Allow search term to be empty */
539 if (!needle
|| !needle_len
)
545 haystack_len
= os_strlen(adv_data
->svc_info
);
546 for (i
= 0; i
< haystack_len
; i
++) {
547 if (haystack_len
- i
< needle_len
)
549 if (os_memcmp(haystack
+ i
, needle
, needle_len
) == 0)
557 static void wpas_sd_req_asp(struct wpa_supplicant
*wpa_s
,
558 struct wpabuf
*resp
, u8 srv_trans_id
,
559 const u8
*query
, size_t query_len
)
561 struct p2ps_advertisement
*adv_data
;
562 const u8
*svc
= &query
[1];
563 const u8
*info
= NULL
;
564 size_t svc_len
= query
[0];
567 u8
*count_pos
= NULL
;
570 wpa_hexdump(MSG_DEBUG
, "P2P: SD Request for ASP", query
, query_len
);
572 if (!wpa_s
->global
->p2p
) {
573 wpa_printf(MSG_DEBUG
, "P2P: ASP protocol not available");
574 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_P2PS
, srv_trans_id
);
578 /* Info block is optional */
579 if (svc_len
+ 1 < query_len
) {
580 info
= &svc
[svc_len
];
584 /* Range check length of svc string and info block */
585 if (svc_len
+ (info_len
? info_len
+ 2 : 1) > query_len
) {
586 wpa_printf(MSG_DEBUG
, "P2P: ASP bad request");
587 wpas_sd_add_bad_request(resp
, P2P_SERV_P2PS
, srv_trans_id
);
591 /* Detect and correct for prefix search */
592 if (svc_len
&& svc
[svc_len
- 1] == '*') {
597 for (adv_data
= p2p_get_p2ps_adv_list(wpa_s
->global
->p2p
);
598 adv_data
; adv_data
= adv_data
->next
) {
599 /* If not a prefix match, reject length mismatches */
600 if (!prefix
&& svc_len
!= os_strlen(adv_data
->svc_name
))
603 /* Search each service for request */
604 if (os_memcmp(adv_data
->svc_name
, svc
, svc_len
) == 0 &&
605 find_p2ps_substr(adv_data
, info
, info_len
)) {
606 size_t len
= os_strlen(adv_data
->svc_name
);
607 size_t svc_info_len
= 0;
609 if (adv_data
->svc_info
)
610 svc_info_len
= os_strlen(adv_data
->svc_info
);
612 if (len
> 0xff || svc_info_len
> 0xffff)
615 /* Length & Count to be filled as we go */
616 if (!len_pos
&& !count_pos
) {
617 if (wpabuf_tailroom(resp
) <
618 len
+ svc_info_len
+ 16)
621 len_pos
= wpabuf_put(resp
, 2);
622 wpabuf_put_u8(resp
, P2P_SERV_P2PS
);
623 wpabuf_put_u8(resp
, srv_trans_id
);
625 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
626 count_pos
= wpabuf_put(resp
, 1);
628 } else if (wpabuf_tailroom(resp
) <
629 len
+ svc_info_len
+ 10)
633 wpa_printf(MSG_DEBUG
,
634 "P2P: Add Svc: %s info: %s",
638 wpa_printf(MSG_DEBUG
, "P2P: Add Svc: %s",
642 /* Advertisement ID */
643 wpabuf_put_le32(resp
, adv_data
->id
);
646 wpabuf_put_be16(resp
, adv_data
->config_methods
);
649 wpabuf_put_u8(resp
, (u8
) len
);
650 wpabuf_put_data(resp
, adv_data
->svc_name
, len
);
653 wpabuf_put_u8(resp
, adv_data
->state
);
655 /* Service Information */
656 wpabuf_put_le16(resp
, (u16
) svc_info_len
);
657 wpabuf_put_data(resp
, adv_data
->svc_info
, svc_info_len
);
659 /* Update length and count */
661 WPA_PUT_LE16(len_pos
,
662 (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
666 /* Return error if no matching svc found */
667 if (count_pos
== NULL
) {
668 wpa_printf(MSG_DEBUG
, "P2P: ASP service not found");
669 wpas_sd_add_not_found(resp
, P2P_SERV_P2PS
, srv_trans_id
);
674 static void wpas_sd_all_asp(struct wpa_supplicant
*wpa_s
,
675 struct wpabuf
*resp
, u8 srv_trans_id
)
677 /* Query data to add all P2PS advertisements:
678 * - Service name length: 1
679 * - Service name: '*'
680 * - Service Information Request Length: 0
682 const u8 q
[] = { 1, (const u8
) '*', 0 };
684 if (p2p_get_p2ps_adv_list(wpa_s
->global
->p2p
))
685 wpas_sd_req_asp(wpa_s
, resp
, srv_trans_id
, q
, sizeof(q
));
689 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
690 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
692 struct wpa_supplicant
*wpa_s
= ctx
;
693 const u8
*pos
= tlvs
;
694 const u8
*end
= tlvs
+ tlvs_len
;
698 u8 srv_proto
, srv_trans_id
;
702 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
704 buf_len
= 2 * tlvs_len
+ 1;
705 buf
= os_malloc(buf_len
);
707 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
708 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
710 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
715 if (wpa_s
->p2p_sd_over_ctrl_iface
) {
716 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
717 update_indic
, tlvs
, tlvs_len
);
718 return; /* to be processed by an external program */
721 resp
= wpabuf_alloc(10000);
725 while (end
- pos
> 1) {
726 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
727 slen
= WPA_GET_LE16(pos
);
729 if (slen
> end
- pos
|| slen
< 2) {
730 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
735 tlv_end
= pos
+ slen
;
738 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
740 srv_trans_id
= *pos
++;
741 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
744 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
748 if (wpa_s
->force_long_sd
) {
749 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
751 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
752 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
753 wpas_sd_all_asp(wpa_s
, resp
, srv_trans_id
);
758 case P2P_SERV_ALL_SERVICES
:
759 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
761 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
762 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
) &&
763 !p2p_get_p2ps_adv_list(wpa_s
->global
->p2p
)) {
764 wpa_printf(MSG_DEBUG
, "P2P: No service "
765 "discovery protocols available");
766 wpas_sd_add_proto_not_avail(
767 resp
, P2P_SERV_ALL_SERVICES
,
771 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
772 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
773 wpas_sd_all_asp(wpa_s
, resp
, srv_trans_id
);
775 case P2P_SERV_BONJOUR
:
776 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
780 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
783 #ifdef CONFIG_WIFI_DISPLAY
784 case P2P_SERV_WIFI_DISPLAY
:
785 wpas_sd_req_wfd(wpa_s
, resp
, srv_trans_id
,
788 #endif /* CONFIG_WIFI_DISPLAY */
790 wpas_sd_req_asp(wpa_s
, resp
, srv_trans_id
,
794 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
795 "protocol %u", srv_proto
);
796 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
805 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
806 update_indic
, tlvs
, tlvs_len
);
808 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
814 static void wpas_sd_p2ps_serv_response(struct wpa_supplicant
*wpa_s
,
815 const u8
*sa
, u8 srv_trans_id
,
816 const u8
*pos
, const u8
*tlv_end
)
824 while (left
-- && pos
< tlv_end
) {
829 /* Sanity check fixed length+svc_str */
830 if (6 >= tlv_end
- pos
)
833 if (svc_len
+ 10 > tlv_end
- pos
)
836 /* Advertisement ID */
837 adv_id
= WPA_GET_LE32(pos
);
841 config_methods
= WPA_GET_BE16(pos
);
846 os_memcpy(svc_str
, pos
, svc_len
);
847 svc_str
[svc_len
] = '\0';
853 /* Service Information Length */
854 buf_len
= WPA_GET_LE16(pos
);
857 /* Sanity check buffer length */
858 if (buf_len
> (unsigned int) (tlv_end
- pos
))
862 buf
= os_zalloc(2 * buf_len
+ 1);
864 utf8_escape((const char *) pos
, buf_len
, buf
,
872 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_ASP_RESP
873 MACSTR
" %x %x %x %x %s '%s'",
874 MAC2STR(sa
), srv_trans_id
, adv_id
,
875 svc_status
, config_methods
, svc_str
,
879 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_ASP_RESP
880 MACSTR
" %x %x %x %x %s",
881 MAC2STR(sa
), srv_trans_id
, adv_id
,
882 svc_status
, config_methods
, svc_str
);
888 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
889 const u8
*tlvs
, size_t tlvs_len
)
891 struct wpa_supplicant
*wpa_s
= ctx
;
892 const u8
*pos
= tlvs
;
893 const u8
*end
= tlvs
+ tlvs_len
;
899 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
901 if (tlvs_len
> 1500) {
902 /* TODO: better way for handling this */
903 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
904 P2P_EVENT_SERV_DISC_RESP MACSTR
905 " %u <long response: %u bytes>",
906 MAC2STR(sa
), update_indic
,
907 (unsigned int) tlvs_len
);
909 buf_len
= 2 * tlvs_len
+ 1;
910 buf
= os_malloc(buf_len
);
912 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
913 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
914 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
915 MAC2STR(sa
), update_indic
, buf
);
920 while (end
- pos
>= 2) {
921 u8 srv_proto
, srv_trans_id
, status
;
923 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
924 slen
= WPA_GET_LE16(pos
);
926 if (slen
> end
- pos
|| slen
< 3) {
927 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
931 tlv_end
= pos
+ slen
;
934 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
936 srv_trans_id
= *pos
++;
937 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
940 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
943 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
946 if (srv_proto
== P2P_SERV_P2PS
&& pos
< tlv_end
) {
947 wpas_sd_p2ps_serv_response(wpa_s
, sa
, srv_trans_id
,
954 wpas_notify_p2p_sd_response(wpa_s
, sa
, update_indic
, tlvs
, tlvs_len
);
958 u64
wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
959 const struct wpabuf
*tlvs
)
961 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
963 return (uintptr_t) p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
967 u64
wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
968 u8 version
, const char *query
)
973 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
976 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
977 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
978 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
979 wpabuf_put_u8(tlvs
, version
);
980 wpabuf_put_str(tlvs
, query
);
981 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
987 u64
wpas_p2p_sd_request_asp(struct wpa_supplicant
*wpa_s
, const u8
*dst
, u8 id
,
988 const char *svc_str
, const char *info_substr
)
991 size_t plen
, svc_len
, substr_len
= 0;
994 svc_len
= os_strlen(svc_str
);
996 substr_len
= os_strlen(info_substr
);
998 if (svc_len
> 0xff || substr_len
> 0xff)
1001 plen
= 1 + 1 + 1 + svc_len
+ 1 + substr_len
;
1002 tlvs
= wpabuf_alloc(2 + plen
);
1006 wpabuf_put_le16(tlvs
, plen
);
1007 wpabuf_put_u8(tlvs
, P2P_SERV_P2PS
);
1008 wpabuf_put_u8(tlvs
, id
); /* Service Transaction ID */
1009 wpabuf_put_u8(tlvs
, (u8
) svc_len
); /* Service String Length */
1010 wpabuf_put_data(tlvs
, svc_str
, svc_len
);
1011 wpabuf_put_u8(tlvs
, (u8
) substr_len
); /* Info Substring Length */
1012 wpabuf_put_data(tlvs
, info_substr
, substr_len
);
1013 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
1020 #ifdef CONFIG_WIFI_DISPLAY
1022 static u64
wpas_p2p_sd_request_wfd(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1023 const struct wpabuf
*tlvs
)
1025 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1027 return (uintptr_t) p2p_sd_request_wfd(wpa_s
->global
->p2p
, dst
, tlvs
);
1031 #define MAX_WFD_SD_SUBELEMS 20
1033 static void wfd_add_sd_req_role(struct wpabuf
*tlvs
, u8 id
, u8 role
,
1034 const char *subelems
)
1041 len
= wpabuf_put(tlvs
, 2);
1042 wpabuf_put_u8(tlvs
, P2P_SERV_WIFI_DISPLAY
); /* Service Protocol Type */
1043 wpabuf_put_u8(tlvs
, id
); /* Service Transaction ID */
1045 wpabuf_put_u8(tlvs
, role
);
1050 if (val
>= 0 && val
< 256) {
1051 wpabuf_put_u8(tlvs
, val
);
1053 if (count
== MAX_WFD_SD_SUBELEMS
)
1056 pos
= os_strchr(pos
+ 1, ',');
1062 WPA_PUT_LE16(len
, (u8
*) wpabuf_put(tlvs
, 0) - len
- 2);
1066 u64
wpas_p2p_sd_request_wifi_display(struct wpa_supplicant
*wpa_s
,
1067 const u8
*dst
, const char *role
)
1069 struct wpabuf
*tlvs
;
1071 const char *subelems
;
1074 subelems
= os_strchr(role
, ' ');
1075 if (subelems
== NULL
)
1079 tlvs
= wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS
));
1083 if (os_strstr(role
, "[source]"))
1084 wfd_add_sd_req_role(tlvs
, id
++, 0x00, subelems
);
1085 if (os_strstr(role
, "[pri-sink]"))
1086 wfd_add_sd_req_role(tlvs
, id
++, 0x01, subelems
);
1087 if (os_strstr(role
, "[sec-sink]"))
1088 wfd_add_sd_req_role(tlvs
, id
++, 0x02, subelems
);
1089 if (os_strstr(role
, "[source+sink]"))
1090 wfd_add_sd_req_role(tlvs
, id
++, 0x03, subelems
);
1092 ret
= wpas_p2p_sd_request_wfd(wpa_s
, dst
, tlvs
);
1097 #endif /* CONFIG_WIFI_DISPLAY */
1100 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, u64 req
)
1102 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1104 return p2p_sd_cancel_request(wpa_s
->global
->p2p
,
1105 (void *) (uintptr_t) req
);
1109 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
1110 const u8
*dst
, u8 dialog_token
,
1111 const struct wpabuf
*resp_tlvs
)
1113 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1115 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
1120 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
1122 if (wpa_s
->global
->p2p
)
1123 p2p_sd_service_update(wpa_s
->global
->p2p
);
1127 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
1129 dl_list_del(&bsrv
->list
);
1130 wpabuf_free(bsrv
->query
);
1131 wpabuf_free(bsrv
->resp
);
1136 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
1138 dl_list_del(&usrv
->list
);
1139 os_free(usrv
->service
);
1144 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
1146 struct p2p_srv_bonjour
*bsrv
, *bn
;
1147 struct p2p_srv_upnp
*usrv
, *un
;
1149 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
1150 struct p2p_srv_bonjour
, list
)
1151 wpas_p2p_srv_bonjour_free(bsrv
);
1153 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
1154 struct p2p_srv_upnp
, list
)
1155 wpas_p2p_srv_upnp_free(usrv
);
1157 wpas_p2p_service_flush_asp(wpa_s
);
1158 wpas_p2p_sd_service_update(wpa_s
);
1162 int wpas_p2p_service_p2ps_id_exists(struct wpa_supplicant
*wpa_s
, u32 adv_id
)
1167 if (p2p_service_p2ps_id(wpa_s
->global
->p2p
, adv_id
))
1174 int wpas_p2p_service_del_asp(struct wpa_supplicant
*wpa_s
, u32 adv_id
)
1178 ret
= p2p_service_del_asp(wpa_s
->global
->p2p
, adv_id
);
1180 wpas_p2p_sd_service_update(wpa_s
);
1185 int wpas_p2p_service_add_asp(struct wpa_supplicant
*wpa_s
,
1186 int auto_accept
, u32 adv_id
,
1187 const char *adv_str
, u8 svc_state
,
1188 u16 config_methods
, const char *svc_info
,
1189 const u8
*cpt_priority
)
1193 ret
= p2p_service_add_asp(wpa_s
->global
->p2p
, auto_accept
, adv_id
,
1194 adv_str
, svc_state
, config_methods
,
1195 svc_info
, cpt_priority
);
1197 wpas_p2p_sd_service_update(wpa_s
);
1202 void wpas_p2p_service_flush_asp(struct wpa_supplicant
*wpa_s
)
1204 p2p_service_flush_asp(wpa_s
->global
->p2p
);
1208 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
1209 struct wpabuf
*query
, struct wpabuf
*resp
)
1211 struct p2p_srv_bonjour
*bsrv
;
1213 bsrv
= os_zalloc(sizeof(*bsrv
));
1216 bsrv
->query
= query
;
1218 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
1220 wpas_p2p_sd_service_update(wpa_s
);
1225 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
1226 const struct wpabuf
*query
)
1228 struct p2p_srv_bonjour
*bsrv
;
1230 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
1233 wpas_p2p_srv_bonjour_free(bsrv
);
1234 wpas_p2p_sd_service_update(wpa_s
);
1239 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1240 const char *service
)
1242 struct p2p_srv_upnp
*usrv
;
1244 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
1245 return 0; /* Already listed */
1246 usrv
= os_zalloc(sizeof(*usrv
));
1249 usrv
->version
= version
;
1250 usrv
->service
= os_strdup(service
);
1251 if (usrv
->service
== NULL
) {
1255 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
1257 wpas_p2p_sd_service_update(wpa_s
);
1262 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1263 const char *service
)
1265 struct p2p_srv_upnp
*usrv
;
1267 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
1270 wpas_p2p_srv_upnp_free(usrv
);
1271 wpas_p2p_sd_service_update(wpa_s
);