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 (pos + 3 <= end) {
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;
56 if (!(dev->info.dev_capab & P2P_DEV_CAPAB_SERVICE_DISCOVERY))
57 return NULL; /* peer does not support SD */
58 #ifdef CONFIG_WIFI_DISPLAY
59 if (wfd_wsd_supported(dev->info.wfd_subelems))
61 #endif /* CONFIG_WIFI_DISPLAY */
63 for (q = p2p->sd_queries; q; q = q->next) {
64 /* Use WSD only if the peer indicates support or it */
67 if (q->for_all_peers && !(dev->flags & P2P_DEV_SD_INFO))
69 if (!q->for_all_peers &&
70 os_memcmp(q->peer, dev->info.p2p_device_addr, ETH_ALEN) ==
79 static int p2p_unlink_sd_query(struct p2p_data *p2p,
80 struct p2p_sd_query *query)
82 struct p2p_sd_query *q, *prev;
90 p2p->sd_queries = q->next;
91 if (p2p->sd_query == query)
102 static void p2p_free_sd_query(struct p2p_sd_query *q)
106 wpabuf_free(q->tlvs);
111 void p2p_free_sd_queries(struct p2p_data *p2p)
113 struct p2p_sd_query *q, *prev;
115 p2p->sd_queries = NULL;
119 p2p_free_sd_query(prev);
124 static struct wpabuf * p2p_build_sd_query(u16 update_indic,
130 buf = gas_anqp_build_initial_req(0, 100 + wpabuf_len(tlvs));
134 /* ANQP Query Request Frame */
135 len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
136 wpabuf_put_be24(buf, OUI_WFA);
137 wpabuf_put_u8(buf, P2P_OUI_TYPE);
138 wpabuf_put_le16(buf, update_indic); /* Service Update Indicator */
139 wpabuf_put_buf(buf, tlvs);
140 gas_anqp_set_element_len(buf, len_pos);
142 gas_anqp_set_len(buf);
148 static void p2p_send_gas_comeback_req(struct p2p_data *p2p, const u8 *dst,
149 u8 dialog_token, int freq)
153 req = gas_build_comeback_req(dialog_token);
157 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
158 if (p2p_send_action(p2p, freq, dst, p2p->cfg->dev_addr, dst,
159 wpabuf_head(req), wpabuf_len(req), 200) < 0)
160 p2p_dbg(p2p, "Failed to send Action frame");
166 static struct wpabuf * p2p_build_sd_response(u8 dialog_token, u16 status_code,
169 const struct wpabuf *tlvs)
174 buf = gas_anqp_build_initial_resp(dialog_token, status_code,
176 100 + (tlvs ? wpabuf_len(tlvs) : 0));
181 /* ANQP Query Response Frame */
182 len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
183 wpabuf_put_be24(buf, OUI_WFA);
184 wpabuf_put_u8(buf, P2P_OUI_TYPE);
185 /* Service Update Indicator */
186 wpabuf_put_le16(buf, update_indic);
187 wpabuf_put_buf(buf, tlvs);
188 gas_anqp_set_element_len(buf, len_pos);
191 gas_anqp_set_len(buf);
197 static struct wpabuf * p2p_build_gas_comeback_resp(u8 dialog_token,
200 const u8 *data, size_t len,
206 buf = gas_anqp_build_comeback_resp(dialog_token, status_code, frag_id,
212 /* ANQP Query Response Frame */
213 wpabuf_put_le16(buf, ANQP_VENDOR_SPECIFIC); /* Info ID */
214 wpabuf_put_le16(buf, 3 + 1 + 2 + total_len);
215 wpabuf_put_be24(buf, OUI_WFA);
216 wpabuf_put_u8(buf, P2P_OUI_TYPE);
217 /* Service Update Indicator */
218 wpabuf_put_le16(buf, update_indic);
221 wpabuf_put_data(buf, data, len);
222 gas_anqp_set_len(buf);
228 int p2p_start_sd(struct p2p_data *p2p, struct p2p_device *dev)
232 struct p2p_sd_query *query;
235 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
237 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
238 MACSTR " to send SD Request",
239 MAC2STR(dev->info.p2p_device_addr));
243 query = p2p_pending_sd_req(p2p, dev);
247 p2p_dbg(p2p, "Start Service Discovery with " MACSTR,
248 MAC2STR(dev->info.p2p_device_addr));
250 req = p2p_build_sd_query(p2p->srv_update_indic, query->tlvs);
255 p2p->sd_query = query;
256 p2p->pending_action_state = P2P_PENDING_SD;
258 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
259 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
260 wpabuf_head(req), wpabuf_len(req), 5000) < 0) {
261 p2p_dbg(p2p, "Failed to send Action frame");
271 void p2p_rx_gas_initial_req(struct p2p_data *p2p, const u8 *sa,
272 const u8 *data, size_t len, int rx_freq)
274 const u8 *pos = data;
275 const u8 *end = data + len;
283 if (p2p->cfg->sd_request == NULL)
289 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
297 dialog_token = *pos++;
298 p2p_dbg(p2p, "GAS Initial Request from " MACSTR
299 " (dialog token %u, freq %d)",
300 MAC2STR(sa), dialog_token, rx_freq);
302 if (*pos != WLAN_EID_ADV_PROTO) {
303 p2p_dbg(p2p, "Unexpected IE in GAS Initial Request: %u", *pos);
310 if (next > end || slen < 2) {
311 p2p_dbg(p2p, "Invalid IE in GAS Initial Request");
314 pos++; /* skip QueryRespLenLimit and PAME-BI */
316 if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
317 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
326 slen = WPA_GET_LE16(pos);
328 if (pos + slen > end)
332 /* ANQP Query Request */
335 if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
336 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
341 slen = WPA_GET_LE16(pos);
343 if (pos + slen > end || slen < 3 + 1) {
344 p2p_dbg(p2p, "Invalid ANQP Query Request length");
348 if (WPA_GET_BE24(pos) != OUI_WFA) {
349 p2p_dbg(p2p, "Unsupported ANQP OUI %06x", WPA_GET_BE24(pos));
354 if (*pos != P2P_OUI_TYPE) {
355 p2p_dbg(p2p, "Unsupported ANQP vendor type %u", *pos);
362 update_indic = WPA_GET_LE16(pos);
363 p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
366 p2p->cfg->sd_request(p2p->cfg->cb_ctx, freq, sa, dialog_token,
367 update_indic, pos, end - pos);
368 /* the response will be indicated with a call to p2p_sd_response() */
372 void p2p_sd_response(struct p2p_data *p2p, int freq, const u8 *dst,
373 u8 dialog_token, const struct wpabuf *resp_tlvs)
377 /* TODO: fix the length limit to match with the maximum frame length */
378 if (wpabuf_len(resp_tlvs) > 1400) {
379 p2p_dbg(p2p, "SD response long enough to require fragmentation");
382 * TODO: Could consider storing the fragmented response
383 * separately for each peer to avoid having to drop old
384 * one if there is more than one pending SD query.
385 * Though, that would eat more memory, so there are
386 * also benefits to just using a single buffer.
388 p2p_dbg(p2p, "Drop previous SD response");
389 wpabuf_free(p2p->sd_resp);
391 p2p->sd_resp = wpabuf_dup(resp_tlvs);
392 if (p2p->sd_resp == NULL) {
393 p2p_err(p2p, "Failed to allocate SD response fragmentation area");
396 os_memcpy(p2p->sd_resp_addr, dst, ETH_ALEN);
397 p2p->sd_resp_dialog_token = dialog_token;
398 p2p->sd_resp_pos = 0;
400 resp = p2p_build_sd_response(dialog_token, WLAN_STATUS_SUCCESS,
401 1, p2p->srv_update_indic, NULL);
403 p2p_dbg(p2p, "SD response fits in initial response");
404 resp = p2p_build_sd_response(dialog_token,
405 WLAN_STATUS_SUCCESS, 0,
406 p2p->srv_update_indic, resp_tlvs);
411 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
412 if (p2p_send_action(p2p, freq, dst, p2p->cfg->dev_addr,
414 wpabuf_head(resp), wpabuf_len(resp), 200) < 0)
415 p2p_dbg(p2p, "Failed to send Action frame");
421 void p2p_rx_gas_initial_resp(struct p2p_data *p2p, const u8 *sa,
422 const u8 *data, size_t len, int rx_freq)
424 const u8 *pos = data;
425 const u8 *end = data + len;
433 if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
434 os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
435 p2p_dbg(p2p, "Ignore unexpected GAS Initial Response from "
436 MACSTR, MAC2STR(sa));
439 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
440 p2p_clear_timeout(p2p);
442 p2p_dbg(p2p, "Received GAS Initial Response from " MACSTR " (len=%d)",
443 MAC2STR(sa), (int) len);
446 p2p_dbg(p2p, "Too short GAS Initial Response frame");
450 dialog_token = *pos++;
451 /* TODO: check dialog_token match */
452 status_code = WPA_GET_LE16(pos);
454 comeback_delay = WPA_GET_LE16(pos);
456 p2p_dbg(p2p, "dialog_token=%u status_code=%u comeback_delay=%u",
457 dialog_token, status_code, comeback_delay);
459 p2p_dbg(p2p, "Service Discovery failed: status code %u",
464 if (*pos != WLAN_EID_ADV_PROTO) {
465 p2p_dbg(p2p, "Unexpected IE in GAS Initial Response: %u", *pos);
472 if (next > end || slen < 2) {
473 p2p_dbg(p2p, "Invalid IE in GAS Initial Response");
476 pos++; /* skip QueryRespLenLimit and PAME-BI */
478 if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
479 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
487 p2p_dbg(p2p, "Too short Query Response");
490 slen = WPA_GET_LE16(pos);
492 p2p_dbg(p2p, "Query Response Length: %d", slen);
493 if (pos + slen > end) {
494 p2p_dbg(p2p, "Not enough Query Response data");
499 if (comeback_delay) {
500 p2p_dbg(p2p, "Fragmented response - request fragments");
501 if (p2p->sd_rx_resp) {
502 p2p_dbg(p2p, "Drop old SD reassembly buffer");
503 wpabuf_free(p2p->sd_rx_resp);
504 p2p->sd_rx_resp = NULL;
506 p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
510 /* ANQP Query Response */
513 if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
514 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
519 slen = WPA_GET_LE16(pos);
521 if (pos + slen > end || slen < 3 + 1) {
522 p2p_dbg(p2p, "Invalid ANQP Query Response length");
526 if (WPA_GET_BE24(pos) != OUI_WFA) {
527 p2p_dbg(p2p, "Unsupported ANQP OUI %06x", WPA_GET_BE24(pos));
532 if (*pos != P2P_OUI_TYPE) {
533 p2p_dbg(p2p, "Unsupported ANQP vendor type %u", *pos);
540 update_indic = WPA_GET_LE16(pos);
541 p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
544 p2p->sd_peer->flags |= P2P_DEV_SD_INFO;
545 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
549 if (!p2p->sd_query->for_all_peers) {
550 struct p2p_sd_query *q;
551 p2p_dbg(p2p, "Remove completed SD query %p",
554 p2p_unlink_sd_query(p2p, p2p->sd_query);
555 p2p_free_sd_query(q);
557 p2p->sd_query = NULL;
560 if (p2p->cfg->sd_response)
561 p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa, update_indic,
563 p2p_continue_find(p2p);
567 void p2p_rx_gas_comeback_req(struct p2p_data *p2p, const u8 *sa,
568 const u8 *data, size_t len, int rx_freq)
575 wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Request", data, len);
578 dialog_token = *data;
579 p2p_dbg(p2p, "Dialog Token: %u", dialog_token);
580 if (dialog_token != p2p->sd_resp_dialog_token) {
581 p2p_dbg(p2p, "No pending SD response fragment for dialog token %u",
586 if (p2p->sd_resp == NULL) {
587 p2p_dbg(p2p, "No pending SD response fragment available");
590 if (os_memcmp(sa, p2p->sd_resp_addr, ETH_ALEN) != 0) {
591 p2p_dbg(p2p, "No pending SD response fragment for " MACSTR,
596 frag_len = wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos;
597 if (frag_len > 1400) {
601 resp = p2p_build_gas_comeback_resp(dialog_token, WLAN_STATUS_SUCCESS,
602 p2p->srv_update_indic,
603 wpabuf_head_u8(p2p->sd_resp) +
604 p2p->sd_resp_pos, frag_len,
605 p2p->sd_frag_id, more,
606 wpabuf_len(p2p->sd_resp));
609 p2p_dbg(p2p, "Send GAS Comeback Response (frag_id %d more=%d frag_len=%d)",
610 p2p->sd_frag_id, more, (int) frag_len);
612 p2p->sd_resp_pos += frag_len;
615 p2p_dbg(p2p, "%d more bytes remain to be sent",
616 (int) (wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos));
618 p2p_dbg(p2p, "All fragments of SD response sent");
619 wpabuf_free(p2p->sd_resp);
623 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
624 if (p2p_send_action(p2p, rx_freq, sa, p2p->cfg->dev_addr,
626 wpabuf_head(resp), wpabuf_len(resp), 200) < 0)
627 p2p_dbg(p2p, "Failed to send Action frame");
633 void p2p_rx_gas_comeback_resp(struct p2p_data *p2p, const u8 *sa,
634 const u8 *data, size_t len, int rx_freq)
636 const u8 *pos = data;
637 const u8 *end = data + len;
646 wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Response", data, len);
648 if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
649 os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
650 p2p_dbg(p2p, "Ignore unexpected GAS Comeback Response from "
651 MACSTR, MAC2STR(sa));
654 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
655 p2p_clear_timeout(p2p);
657 p2p_dbg(p2p, "Received GAS Comeback Response from " MACSTR " (len=%d)",
658 MAC2STR(sa), (int) len);
661 p2p_dbg(p2p, "Too short GAS Comeback Response frame");
665 dialog_token = *pos++;
666 /* TODO: check dialog_token match */
667 status_code = WPA_GET_LE16(pos);
669 frag_id = *pos & 0x7f;
670 more_frags = (*pos & 0x80) >> 7;
672 comeback_delay = WPA_GET_LE16(pos);
674 p2p_dbg(p2p, "dialog_token=%u status_code=%u frag_id=%d more_frags=%d "
676 dialog_token, status_code, frag_id, more_frags,
678 /* TODO: check frag_id match */
680 p2p_dbg(p2p, "Service Discovery failed: status code %u",
685 if (*pos != WLAN_EID_ADV_PROTO) {
686 p2p_dbg(p2p, "Unexpected IE in GAS Comeback Response: %u",
694 if (next > end || slen < 2) {
695 p2p_dbg(p2p, "Invalid IE in GAS Comeback Response");
698 pos++; /* skip QueryRespLenLimit and PAME-BI */
700 if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
701 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
709 p2p_dbg(p2p, "Too short Query Response");
712 slen = WPA_GET_LE16(pos);
714 p2p_dbg(p2p, "Query Response Length: %d", slen);
715 if (pos + slen > end) {
716 p2p_dbg(p2p, "Not enough Query Response data");
720 p2p_dbg(p2p, "No Query Response data");
725 if (p2p->sd_rx_resp) {
727 * ANQP header is only included in the first fragment; rest of
728 * the fragments start with continue TLVs.
730 goto skip_nqp_header;
733 /* ANQP Query Response */
736 if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
737 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
742 slen = WPA_GET_LE16(pos);
744 p2p_dbg(p2p, "ANQP Query Response length: %u", slen);
746 p2p_dbg(p2p, "Invalid ANQP Query Response length");
752 if (WPA_GET_BE24(pos) != OUI_WFA) {
753 p2p_dbg(p2p, "Unsupported ANQP OUI %06x", WPA_GET_BE24(pos));
758 if (*pos != P2P_OUI_TYPE) {
759 p2p_dbg(p2p, "Unsupported ANQP vendor type %u", *pos);
766 p2p->sd_rx_update_indic = WPA_GET_LE16(pos);
767 p2p_dbg(p2p, "Service Update Indicator: %u", p2p->sd_rx_update_indic);
771 if (wpabuf_resize(&p2p->sd_rx_resp, end - pos) < 0)
773 wpabuf_put_data(p2p->sd_rx_resp, pos, end - pos);
774 p2p_dbg(p2p, "Current SD reassembly buffer length: %u",
775 (unsigned int) wpabuf_len(p2p->sd_rx_resp));
778 p2p_dbg(p2p, "More fragments remains");
779 /* TODO: what would be a good size limit? */
780 if (wpabuf_len(p2p->sd_rx_resp) > 64000) {
781 wpabuf_free(p2p->sd_rx_resp);
782 p2p->sd_rx_resp = NULL;
783 p2p_dbg(p2p, "Too long SD response - drop it");
786 p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
790 p2p->sd_peer->flags |= P2P_DEV_SD_INFO;
791 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
795 if (!p2p->sd_query->for_all_peers) {
796 struct p2p_sd_query *q;
797 p2p_dbg(p2p, "Remove completed SD query %p",
800 p2p_unlink_sd_query(p2p, p2p->sd_query);
801 p2p_free_sd_query(q);
803 p2p->sd_query = NULL;
806 if (p2p->cfg->sd_response)
807 p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa,
808 p2p->sd_rx_update_indic,
809 wpabuf_head(p2p->sd_rx_resp),
810 wpabuf_len(p2p->sd_rx_resp));
811 wpabuf_free(p2p->sd_rx_resp);
812 p2p->sd_rx_resp = NULL;
814 p2p_continue_find(p2p);
818 void * p2p_sd_request(struct p2p_data *p2p, const u8 *dst,
819 const struct wpabuf *tlvs)
821 struct p2p_sd_query *q;
823 q = os_zalloc(sizeof(*q));
828 os_memcpy(q->peer, dst, ETH_ALEN);
830 q->for_all_peers = 1;
832 q->tlvs = wpabuf_dup(tlvs);
833 if (q->tlvs == NULL) {
834 p2p_free_sd_query(q);
838 q->next = p2p->sd_queries;
840 p2p_dbg(p2p, "Added SD Query %p", q);
843 struct p2p_device *dev;
844 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
845 dev->flags &= ~P2P_DEV_SD_INFO;
852 #ifdef CONFIG_WIFI_DISPLAY
853 void * p2p_sd_request_wfd(struct p2p_data *p2p, const u8 *dst,
854 const struct wpabuf *tlvs)
856 struct p2p_sd_query *q;
857 q = p2p_sd_request(p2p, dst, tlvs);
862 #endif /* CONFIG_WIFI_DISPLAY */
865 void p2p_sd_service_update(struct p2p_data *p2p)
867 p2p->srv_update_indic++;
871 int p2p_sd_cancel_request(struct p2p_data *p2p, void *req)
873 if (p2p_unlink_sd_query(p2p, req)) {
874 p2p_dbg(p2p, "Cancel pending SD query %p", req);
875 p2p_free_sd_query(req);