2 * Wi-Fi Direct - P2P service discovery
3 * Copyright (c) 2009, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/ieee802_11_defs.h"
13 #include "common/gas.h"
18 #ifdef CONFIG_WIFI_DISPLAY
19 static int wfd_wsd_supported(struct wpabuf *wfd)
28 pos = wpabuf_head(wfd);
29 end = pos + wpabuf_len(wfd);
31 while (end - pos >= 3) {
33 len = WPA_GET_BE16(pos);
38 if (subelem == WFD_SUBELEM_DEVICE_INFO && len >= 6) {
39 u16 info = WPA_GET_BE16(pos);
40 return !!(info & 0x0040);
48 #endif /* CONFIG_WIFI_DISPLAY */
50 struct p2p_sd_query * p2p_pending_sd_req(struct p2p_data *p2p,
51 struct p2p_device *dev)
53 struct p2p_sd_query *q;
57 if (!(dev->info.dev_capab & P2P_DEV_CAPAB_SERVICE_DISCOVERY))
58 return NULL; /* peer does not support SD */
59 #ifdef CONFIG_WIFI_DISPLAY
60 if (wfd_wsd_supported(dev->info.wfd_subelems))
62 #endif /* CONFIG_WIFI_DISPLAY */
64 for (q = p2p->sd_queries; q; q = q->next) {
65 /* Use WSD only if the peer indicates support or it */
68 /* if the query is a broadcast query */
69 if (q->for_all_peers) {
71 * check if there are any broadcast queries pending for
74 if (dev->sd_pending_bcast_queries <= 0)
76 /* query number that needs to be send to the device */
77 if (count == dev->sd_pending_bcast_queries - 1)
81 if (!q->for_all_peers &&
82 os_memcmp(q->peer, dev->info.p2p_device_addr, ETH_ALEN) ==
90 if (dev->sd_reqs > 100) {
91 p2p_dbg(p2p, "Too many SD request attempts to " MACSTR
92 " - skip remaining queries",
93 MAC2STR(dev->info.p2p_device_addr));
100 static void p2p_decrease_sd_bc_queries(struct p2p_data *p2p, int query_number)
102 struct p2p_device *dev;
104 p2p->num_p2p_sd_queries--;
105 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
106 if (query_number <= dev->sd_pending_bcast_queries - 1) {
108 * Query not yet sent to the device and it is to be
109 * removed, so update the pending count.
111 dev->sd_pending_bcast_queries--;
117 static int p2p_unlink_sd_query(struct p2p_data *p2p,
118 struct p2p_sd_query *query)
120 struct p2p_sd_query *q, *prev;
121 int query_number = 0;
127 /* If the query is a broadcast query, decrease one from
129 if (query->for_all_peers)
130 p2p_decrease_sd_bc_queries(p2p, query_number);
132 prev->next = q->next;
134 p2p->sd_queries = q->next;
135 if (p2p->sd_query == query)
136 p2p->sd_query = NULL;
139 if (q->for_all_peers)
148 static void p2p_free_sd_query(struct p2p_sd_query *q)
152 wpabuf_free(q->tlvs);
157 void p2p_free_sd_queries(struct p2p_data *p2p)
159 struct p2p_sd_query *q, *prev;
161 p2p->sd_queries = NULL;
165 p2p_free_sd_query(prev);
167 p2p->num_p2p_sd_queries = 0;
171 static struct wpabuf * p2p_build_sd_query(u16 update_indic,
177 buf = gas_anqp_build_initial_req(0, 100 + wpabuf_len(tlvs));
181 /* ANQP Query Request Frame */
182 len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
183 wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
184 wpabuf_put_le16(buf, update_indic); /* Service Update Indicator */
185 wpabuf_put_buf(buf, tlvs);
186 gas_anqp_set_element_len(buf, len_pos);
188 gas_anqp_set_len(buf);
194 static void p2p_send_gas_comeback_req(struct p2p_data *p2p, const u8 *dst,
195 u8 dialog_token, int freq)
199 req = gas_build_comeback_req(dialog_token);
203 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
204 if (p2p_send_action(p2p, freq, dst, p2p->cfg->dev_addr, dst,
205 wpabuf_head(req), wpabuf_len(req), 200) < 0)
206 p2p_dbg(p2p, "Failed to send Action frame");
212 static struct wpabuf * p2p_build_sd_response(u8 dialog_token, u16 status_code,
215 const struct wpabuf *tlvs)
220 buf = gas_anqp_build_initial_resp(dialog_token, status_code,
222 100 + (tlvs ? wpabuf_len(tlvs) : 0));
227 /* ANQP Query Response Frame */
228 len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
229 wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
230 /* Service Update Indicator */
231 wpabuf_put_le16(buf, update_indic);
232 wpabuf_put_buf(buf, tlvs);
233 gas_anqp_set_element_len(buf, len_pos);
236 gas_anqp_set_len(buf);
242 static struct wpabuf * p2p_build_gas_comeback_resp(u8 dialog_token,
245 const u8 *data, size_t len,
251 buf = gas_anqp_build_comeback_resp(dialog_token, status_code, frag_id,
257 /* ANQP Query Response Frame */
258 wpabuf_put_le16(buf, ANQP_VENDOR_SPECIFIC); /* Info ID */
259 wpabuf_put_le16(buf, 3 + 1 + 2 + total_len);
260 wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
261 /* Service Update Indicator */
262 wpabuf_put_le16(buf, update_indic);
265 wpabuf_put_data(buf, data, len);
266 gas_anqp_set_len(buf);
272 int p2p_start_sd(struct p2p_data *p2p, struct p2p_device *dev)
276 struct p2p_sd_query *query;
278 unsigned int wait_time;
280 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
282 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
283 MACSTR " to send SD Request",
284 MAC2STR(dev->info.p2p_device_addr));
288 query = p2p_pending_sd_req(p2p, dev);
291 if (p2p->state == P2P_SEARCH &&
292 os_memcmp(p2p->sd_query_no_ack, dev->info.p2p_device_addr,
294 p2p_dbg(p2p, "Do not start Service Discovery with " MACSTR
295 " due to it being the first no-ACK peer in this search iteration",
296 MAC2STR(dev->info.p2p_device_addr));
300 p2p_dbg(p2p, "Start Service Discovery with " MACSTR,
301 MAC2STR(dev->info.p2p_device_addr));
303 req = p2p_build_sd_query(p2p->srv_update_indic, query->tlvs);
309 p2p->sd_query = query;
310 p2p->pending_action_state = P2P_PENDING_SD;
313 if (p2p->cfg->max_listen && wait_time > p2p->cfg->max_listen)
314 wait_time = p2p->cfg->max_listen;
315 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
316 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
317 wpabuf_head(req), wpabuf_len(req), wait_time) < 0) {
318 p2p_dbg(p2p, "Failed to send Action frame");
328 void p2p_rx_gas_initial_req(struct p2p_data *p2p, const u8 *sa,
329 const u8 *data, size_t len, int rx_freq)
331 const u8 *pos = data;
332 const u8 *end = data + len;
340 if (p2p->cfg->sd_request == NULL)
346 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
354 dialog_token = *pos++;
355 p2p_dbg(p2p, "GAS Initial Request from " MACSTR
356 " (dialog token %u, freq %d)",
357 MAC2STR(sa), dialog_token, rx_freq);
359 if (*pos != WLAN_EID_ADV_PROTO) {
360 p2p_dbg(p2p, "Unexpected IE in GAS Initial Request: %u", *pos);
366 if (slen > end - pos || slen < 2) {
367 p2p_dbg(p2p, "Invalid IE in GAS Initial Request");
371 pos++; /* skip QueryRespLenLimit and PAME-BI */
373 if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
374 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
383 slen = WPA_GET_LE16(pos);
385 if (slen > end - pos)
389 /* ANQP Query Request */
392 if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
393 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
398 slen = WPA_GET_LE16(pos);
400 if (slen > end - pos || slen < 3 + 1) {
401 p2p_dbg(p2p, "Invalid ANQP Query Request length");
405 if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
406 p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
414 update_indic = WPA_GET_LE16(pos);
415 p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
418 p2p->cfg->sd_request(p2p->cfg->cb_ctx, freq, sa, dialog_token,
419 update_indic, pos, end - pos);
420 /* the response will be indicated with a call to p2p_sd_response() */
424 void p2p_sd_response(struct p2p_data *p2p, int freq, const u8 *dst,
425 u8 dialog_token, const struct wpabuf *resp_tlvs)
429 unsigned int wait_time = 200;
432 * In the 60 GHz, we have a smaller maximum frame length for management
435 max_len = (freq > 56160) ? 928 : 1400;
437 /* TODO: fix the length limit to match with the maximum frame length */
438 if (wpabuf_len(resp_tlvs) > max_len) {
439 p2p_dbg(p2p, "SD response long enough to require fragmentation");
442 * TODO: Could consider storing the fragmented response
443 * separately for each peer to avoid having to drop old
444 * one if there is more than one pending SD query.
445 * Though, that would eat more memory, so there are
446 * also benefits to just using a single buffer.
448 p2p_dbg(p2p, "Drop previous SD response");
449 wpabuf_free(p2p->sd_resp);
451 p2p->sd_resp = wpabuf_dup(resp_tlvs);
452 if (p2p->sd_resp == NULL) {
453 p2p_err(p2p, "Failed to allocate SD response fragmentation area");
456 os_memcpy(p2p->sd_resp_addr, dst, ETH_ALEN);
457 p2p->sd_resp_dialog_token = dialog_token;
458 p2p->sd_resp_pos = 0;
460 resp = p2p_build_sd_response(dialog_token, WLAN_STATUS_SUCCESS,
461 1, p2p->srv_update_indic, NULL);
463 p2p_dbg(p2p, "SD response fits in initial response");
464 wait_time = 0; /* no more SD frames in the sequence */
465 resp = p2p_build_sd_response(dialog_token,
466 WLAN_STATUS_SUCCESS, 0,
467 p2p->srv_update_indic, resp_tlvs);
472 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
473 if (p2p_send_action(p2p, freq, dst, p2p->cfg->dev_addr,
475 wpabuf_head(resp), wpabuf_len(resp), wait_time) < 0)
476 p2p_dbg(p2p, "Failed to send Action frame");
482 void p2p_rx_gas_initial_resp(struct p2p_data *p2p, const u8 *sa,
483 const u8 *data, size_t len, int rx_freq)
485 const u8 *pos = data;
486 const u8 *end = data + len;
494 if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
495 os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
496 p2p_dbg(p2p, "Ignore unexpected GAS Initial Response from "
497 MACSTR, MAC2STR(sa));
500 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
501 p2p_clear_timeout(p2p);
503 p2p_dbg(p2p, "Received GAS Initial Response from " MACSTR " (len=%d)",
504 MAC2STR(sa), (int) len);
507 p2p_dbg(p2p, "Too short GAS Initial Response frame");
511 dialog_token = *pos++;
512 /* TODO: check dialog_token match */
513 status_code = WPA_GET_LE16(pos);
515 comeback_delay = WPA_GET_LE16(pos);
517 p2p_dbg(p2p, "dialog_token=%u status_code=%u comeback_delay=%u",
518 dialog_token, status_code, comeback_delay);
520 p2p_dbg(p2p, "Service Discovery failed: status code %u",
525 if (*pos != WLAN_EID_ADV_PROTO) {
526 p2p_dbg(p2p, "Unexpected IE in GAS Initial Response: %u", *pos);
532 if (slen > end - pos || slen < 2) {
533 p2p_dbg(p2p, "Invalid IE in GAS Initial Response");
537 pos++; /* skip QueryRespLenLimit and PAME-BI */
539 if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
540 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
548 p2p_dbg(p2p, "Too short Query Response");
551 slen = WPA_GET_LE16(pos);
553 p2p_dbg(p2p, "Query Response Length: %d", slen);
554 if (slen > end - pos) {
555 p2p_dbg(p2p, "Not enough Query Response data");
560 if (comeback_delay) {
561 p2p_dbg(p2p, "Fragmented response - request fragments");
562 if (p2p->sd_rx_resp) {
563 p2p_dbg(p2p, "Drop old SD reassembly buffer");
564 wpabuf_free(p2p->sd_rx_resp);
565 p2p->sd_rx_resp = NULL;
567 p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
571 /* ANQP Query Response */
574 if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
575 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
580 slen = WPA_GET_LE16(pos);
582 if (slen > end - pos || slen < 3 + 1) {
583 p2p_dbg(p2p, "Invalid ANQP Query Response length");
587 if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
588 p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
596 update_indic = WPA_GET_LE16(pos);
597 p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
603 if (!p2p->sd_query->for_all_peers) {
604 struct p2p_sd_query *q;
605 p2p_dbg(p2p, "Remove completed SD query %p",
608 p2p_unlink_sd_query(p2p, p2p->sd_query);
609 p2p_free_sd_query(q);
611 p2p->sd_query = NULL;
614 if (p2p->cfg->sd_response)
615 p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa, update_indic,
617 p2p_continue_find(p2p);
621 void p2p_rx_gas_comeback_req(struct p2p_data *p2p, const u8 *sa,
622 const u8 *data, size_t len, int rx_freq)
626 size_t frag_len, max_len;
628 unsigned int wait_time = 200;
630 wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Request", data, len);
633 dialog_token = *data;
634 p2p_dbg(p2p, "Dialog Token: %u", dialog_token);
635 if (dialog_token != p2p->sd_resp_dialog_token) {
636 p2p_dbg(p2p, "No pending SD response fragment for dialog token %u",
641 if (p2p->sd_resp == NULL) {
642 p2p_dbg(p2p, "No pending SD response fragment available");
645 if (os_memcmp(sa, p2p->sd_resp_addr, ETH_ALEN) != 0) {
646 p2p_dbg(p2p, "No pending SD response fragment for " MACSTR,
652 * In the 60 GHz, we have a smaller maximum frame length for management
655 max_len = (rx_freq > 56160) ? 928 : 1400;
656 frag_len = wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos;
657 if (frag_len > max_len) {
661 resp = p2p_build_gas_comeback_resp(dialog_token, WLAN_STATUS_SUCCESS,
662 p2p->srv_update_indic,
663 wpabuf_head_u8(p2p->sd_resp) +
664 p2p->sd_resp_pos, frag_len,
665 p2p->sd_frag_id, more,
666 wpabuf_len(p2p->sd_resp));
669 p2p_dbg(p2p, "Send GAS Comeback Response (frag_id %d more=%d frag_len=%d)",
670 p2p->sd_frag_id, more, (int) frag_len);
672 p2p->sd_resp_pos += frag_len;
675 p2p_dbg(p2p, "%d more bytes remain to be sent",
676 (int) (wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos));
678 p2p_dbg(p2p, "All fragments of SD response sent");
679 wpabuf_free(p2p->sd_resp);
681 wait_time = 0; /* no more SD frames in the sequence */
684 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
685 if (p2p_send_action(p2p, rx_freq, sa, p2p->cfg->dev_addr,
687 wpabuf_head(resp), wpabuf_len(resp), wait_time) < 0)
688 p2p_dbg(p2p, "Failed to send Action frame");
694 void p2p_rx_gas_comeback_resp(struct p2p_data *p2p, const u8 *sa,
695 const u8 *data, size_t len, int rx_freq)
697 const u8 *pos = data;
698 const u8 *end = data + len;
707 wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Response", data, len);
709 if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
710 os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
711 p2p_dbg(p2p, "Ignore unexpected GAS Comeback Response from "
712 MACSTR, MAC2STR(sa));
715 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
716 p2p_clear_timeout(p2p);
718 p2p_dbg(p2p, "Received GAS Comeback Response from " MACSTR " (len=%d)",
719 MAC2STR(sa), (int) len);
722 p2p_dbg(p2p, "Too short GAS Comeback Response frame");
726 dialog_token = *pos++;
727 /* TODO: check dialog_token match */
728 status_code = WPA_GET_LE16(pos);
730 frag_id = *pos & 0x7f;
731 more_frags = (*pos & 0x80) >> 7;
733 comeback_delay = WPA_GET_LE16(pos);
735 p2p_dbg(p2p, "dialog_token=%u status_code=%u frag_id=%d more_frags=%d "
737 dialog_token, status_code, frag_id, more_frags,
739 /* TODO: check frag_id match */
741 p2p_dbg(p2p, "Service Discovery failed: status code %u",
746 if (*pos != WLAN_EID_ADV_PROTO) {
747 p2p_dbg(p2p, "Unexpected IE in GAS Comeback Response: %u",
754 if (slen > end - pos || slen < 2) {
755 p2p_dbg(p2p, "Invalid IE in GAS Comeback Response");
759 pos++; /* skip QueryRespLenLimit and PAME-BI */
761 if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
762 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
770 p2p_dbg(p2p, "Too short Query Response");
773 slen = WPA_GET_LE16(pos);
775 p2p_dbg(p2p, "Query Response Length: %d", slen);
776 if (slen > end - pos) {
777 p2p_dbg(p2p, "Not enough Query Response data");
781 p2p_dbg(p2p, "No Query Response data");
786 if (p2p->sd_rx_resp) {
788 * ANQP header is only included in the first fragment; rest of
789 * the fragments start with continue TLVs.
791 goto skip_nqp_header;
794 /* ANQP Query Response */
797 if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
798 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
803 slen = WPA_GET_LE16(pos);
805 p2p_dbg(p2p, "ANQP Query Response length: %u", slen);
807 p2p_dbg(p2p, "Invalid ANQP Query Response length");
813 if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
814 p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
822 p2p->sd_rx_update_indic = WPA_GET_LE16(pos);
823 p2p_dbg(p2p, "Service Update Indicator: %u", p2p->sd_rx_update_indic);
827 if (wpabuf_resize(&p2p->sd_rx_resp, end - pos) < 0)
829 wpabuf_put_data(p2p->sd_rx_resp, pos, end - pos);
830 p2p_dbg(p2p, "Current SD reassembly buffer length: %u",
831 (unsigned int) wpabuf_len(p2p->sd_rx_resp));
834 p2p_dbg(p2p, "More fragments remains");
835 /* TODO: what would be a good size limit? */
836 if (wpabuf_len(p2p->sd_rx_resp) > 64000) {
837 wpabuf_free(p2p->sd_rx_resp);
838 p2p->sd_rx_resp = NULL;
839 p2p_dbg(p2p, "Too long SD response - drop it");
842 p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
849 if (!p2p->sd_query->for_all_peers) {
850 struct p2p_sd_query *q;
851 p2p_dbg(p2p, "Remove completed SD query %p",
854 p2p_unlink_sd_query(p2p, p2p->sd_query);
855 p2p_free_sd_query(q);
857 p2p->sd_query = NULL;
860 if (p2p->cfg->sd_response)
861 p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa,
862 p2p->sd_rx_update_indic,
863 wpabuf_head(p2p->sd_rx_resp),
864 wpabuf_len(p2p->sd_rx_resp));
865 wpabuf_free(p2p->sd_rx_resp);
866 p2p->sd_rx_resp = NULL;
868 p2p_continue_find(p2p);
872 void * p2p_sd_request(struct p2p_data *p2p, const u8 *dst,
873 const struct wpabuf *tlvs)
875 struct p2p_sd_query *q;
877 q = os_zalloc(sizeof(*q));
882 os_memcpy(q->peer, dst, ETH_ALEN);
884 q->for_all_peers = 1;
886 q->tlvs = wpabuf_dup(tlvs);
887 if (q->tlvs == NULL) {
888 p2p_free_sd_query(q);
892 q->next = p2p->sd_queries;
894 p2p_dbg(p2p, "Added SD Query %p", q);
897 struct p2p_device *dev;
899 p2p->num_p2p_sd_queries++;
901 /* Update all the devices for the newly added broadcast query */
902 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
903 if (dev->sd_pending_bcast_queries <= 0)
904 dev->sd_pending_bcast_queries = 1;
906 dev->sd_pending_bcast_queries++;
914 #ifdef CONFIG_WIFI_DISPLAY
915 void * p2p_sd_request_wfd(struct p2p_data *p2p, const u8 *dst,
916 const struct wpabuf *tlvs)
918 struct p2p_sd_query *q;
919 q = p2p_sd_request(p2p, dst, tlvs);
924 #endif /* CONFIG_WIFI_DISPLAY */
927 void p2p_sd_service_update(struct p2p_data *p2p)
929 p2p->srv_update_indic++;
933 int p2p_sd_cancel_request(struct p2p_data *p2p, void *req)
935 if (p2p_unlink_sd_query(p2p, req)) {
936 p2p_dbg(p2p, "Cancel pending SD query %p", req);
937 p2p_free_sd_query(req);