blob: a8bc5ba7f344c663fa0604cdab770d3a5e3203d4 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Wi-Fi Direct - P2P service discovery
3 * Copyright (c) 2009, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080013#include "common/gas.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "p2p_i.h"
15#include "p2p.h"
16
17
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070018#ifdef CONFIG_WIFI_DISPLAY
19static int wfd_wsd_supported(struct wpabuf *wfd)
20{
21 const u8 *pos, *end;
22 u8 subelem;
23 u16 len;
24
25 if (wfd == NULL)
26 return 0;
27
28 pos = wpabuf_head(wfd);
29 end = pos + wpabuf_len(wfd);
30
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080031 while (end - pos >= 3) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070032 subelem = *pos++;
33 len = WPA_GET_BE16(pos);
34 pos += 2;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080035 if (len > end - pos)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070036 break;
37
38 if (subelem == WFD_SUBELEM_DEVICE_INFO && len >= 6) {
39 u16 info = WPA_GET_BE16(pos);
40 return !!(info & 0x0040);
41 }
42
43 pos += len;
44 }
45
46 return 0;
47}
48#endif /* CONFIG_WIFI_DISPLAY */
49
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050struct p2p_sd_query * p2p_pending_sd_req(struct p2p_data *p2p,
51 struct p2p_device *dev)
52{
53 struct p2p_sd_query *q;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070054 int wsd = 0;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -080055 int count = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056
57 if (!(dev->info.dev_capab & P2P_DEV_CAPAB_SERVICE_DISCOVERY))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080058 return NULL; /* peer does not support SD */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070059#ifdef CONFIG_WIFI_DISPLAY
60 if (wfd_wsd_supported(dev->info.wfd_subelems))
61 wsd = 1;
62#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063
64 for (q = p2p->sd_queries; q; q = q->next) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070065 /* Use WSD only if the peer indicates support or it */
66 if (q->wsd && !wsd)
67 continue;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -080068 /* if the query is a broadcast query */
69 if (q->for_all_peers) {
70 /*
71 * check if there are any broadcast queries pending for
72 * this device
73 */
74 if (dev->sd_pending_bcast_queries <= 0)
75 return NULL;
76 /* query number that needs to be send to the device */
77 if (count == dev->sd_pending_bcast_queries - 1)
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080078 goto found;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -080079 count++;
80 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081 if (!q->for_all_peers &&
82 os_memcmp(q->peer, dev->info.p2p_device_addr, ETH_ALEN) ==
83 0)
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080084 goto found;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070085 }
86
87 return NULL;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080088
89found:
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));
94 return NULL;
95 }
96 return q;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097}
98
99
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800100static void p2p_decrease_sd_bc_queries(struct p2p_data *p2p, int query_number)
101{
102 struct p2p_device *dev;
103
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) {
107 /*
108 * Query not yet sent to the device and it is to be
109 * removed, so update the pending count.
110 */
111 dev->sd_pending_bcast_queries--;
112 }
113 }
114}
115
116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117static int p2p_unlink_sd_query(struct p2p_data *p2p,
118 struct p2p_sd_query *query)
119{
120 struct p2p_sd_query *q, *prev;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800121 int query_number = 0;
122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123 q = p2p->sd_queries;
124 prev = NULL;
125 while (q) {
126 if (q == query) {
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800127 /* If the query is a broadcast query, decrease one from
128 * all the devices */
129 if (query->for_all_peers)
130 p2p_decrease_sd_bc_queries(p2p, query_number);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 if (prev)
132 prev->next = q->next;
133 else
134 p2p->sd_queries = q->next;
135 if (p2p->sd_query == query)
136 p2p->sd_query = NULL;
137 return 1;
138 }
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800139 if (q->for_all_peers)
140 query_number++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141 prev = q;
142 q = q->next;
143 }
144 return 0;
145}
146
147
148static void p2p_free_sd_query(struct p2p_sd_query *q)
149{
150 if (q == NULL)
151 return;
152 wpabuf_free(q->tlvs);
153 os_free(q);
154}
155
156
157void p2p_free_sd_queries(struct p2p_data *p2p)
158{
159 struct p2p_sd_query *q, *prev;
160 q = p2p->sd_queries;
161 p2p->sd_queries = NULL;
162 while (q) {
163 prev = q;
164 q = q->next;
165 p2p_free_sd_query(prev);
166 }
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800167 p2p->num_p2p_sd_queries = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168}
169
170
171static struct wpabuf * p2p_build_sd_query(u16 update_indic,
172 struct wpabuf *tlvs)
173{
174 struct wpabuf *buf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800175 u8 *len_pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800177 buf = gas_anqp_build_initial_req(0, 100 + wpabuf_len(tlvs));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178 if (buf == NULL)
179 return NULL;
180
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800181 /* ANQP Query Request Frame */
182 len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800183 wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 wpabuf_put_le16(buf, update_indic); /* Service Update Indicator */
185 wpabuf_put_buf(buf, tlvs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800186 gas_anqp_set_element_len(buf, len_pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800188 gas_anqp_set_len(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189
190 return buf;
191}
192
193
194static void p2p_send_gas_comeback_req(struct p2p_data *p2p, const u8 *dst,
195 u8 dialog_token, int freq)
196{
197 struct wpabuf *req;
198
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800199 req = gas_build_comeback_req(dialog_token);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 if (req == NULL)
201 return;
202
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)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700206 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207
208 wpabuf_free(req);
209}
210
211
212static struct wpabuf * p2p_build_sd_response(u8 dialog_token, u16 status_code,
213 u16 comeback_delay,
214 u16 update_indic,
215 const struct wpabuf *tlvs)
216{
217 struct wpabuf *buf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800218 u8 *len_pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800220 buf = gas_anqp_build_initial_resp(dialog_token, status_code,
221 comeback_delay,
222 100 + (tlvs ? wpabuf_len(tlvs) : 0));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223 if (buf == NULL)
224 return NULL;
225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 if (tlvs) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800227 /* ANQP Query Response Frame */
228 len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800229 wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 /* Service Update Indicator */
231 wpabuf_put_le16(buf, update_indic);
232 wpabuf_put_buf(buf, tlvs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233 gas_anqp_set_element_len(buf, len_pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 }
235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800236 gas_anqp_set_len(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237
238 return buf;
239}
240
241
242static struct wpabuf * p2p_build_gas_comeback_resp(u8 dialog_token,
243 u16 status_code,
244 u16 update_indic,
245 const u8 *data, size_t len,
246 u8 frag_id, u8 more,
247 u16 total_len)
248{
249 struct wpabuf *buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800251 buf = gas_anqp_build_comeback_resp(dialog_token, status_code, frag_id,
252 more, 0, 100 + len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700253 if (buf == NULL)
254 return NULL;
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 if (frag_id == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800257 /* ANQP Query Response Frame */
258 wpabuf_put_le16(buf, ANQP_VENDOR_SPECIFIC); /* Info ID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259 wpabuf_put_le16(buf, 3 + 1 + 2 + total_len);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800260 wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 /* Service Update Indicator */
262 wpabuf_put_le16(buf, update_indic);
263 }
264
265 wpabuf_put_data(buf, data, len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800266 gas_anqp_set_len(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267
268 return buf;
269}
270
271
272int p2p_start_sd(struct p2p_data *p2p, struct p2p_device *dev)
273{
274 struct wpabuf *req;
275 int ret = 0;
276 struct p2p_sd_query *query;
277 int freq;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700278 unsigned int wait_time;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279
280 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
281 if (freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700282 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
283 MACSTR " to send SD Request",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 MAC2STR(dev->info.p2p_device_addr));
285 return -1;
286 }
287
288 query = p2p_pending_sd_req(p2p, dev);
289 if (query == NULL)
290 return -1;
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800291 if (p2p->state == P2P_SEARCH &&
292 os_memcmp(p2p->sd_query_no_ack, dev->info.p2p_device_addr,
293 ETH_ALEN) == 0) {
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));
297 return -2;
298 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700300 p2p_dbg(p2p, "Start Service Discovery with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301 MAC2STR(dev->info.p2p_device_addr));
302
303 req = p2p_build_sd_query(p2p->srv_update_indic, query->tlvs);
304 if (req == NULL)
305 return -1;
306
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800307 dev->sd_reqs++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 p2p->sd_peer = dev;
309 p2p->sd_query = query;
310 p2p->pending_action_state = P2P_PENDING_SD;
311
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700312 wait_time = 5000;
313 if (p2p->cfg->max_listen && wait_time > p2p->cfg->max_listen)
314 wait_time = p2p->cfg->max_listen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
316 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700317 wpabuf_head(req), wpabuf_len(req), wait_time) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700318 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 ret = -1;
320 }
321
322 wpabuf_free(req);
323
324 return ret;
325}
326
327
328void p2p_rx_gas_initial_req(struct p2p_data *p2p, const u8 *sa,
329 const u8 *data, size_t len, int rx_freq)
330{
331 const u8 *pos = data;
332 const u8 *end = data + len;
333 const u8 *next;
334 u8 dialog_token;
335 u16 slen;
336 int freq;
337 u16 update_indic;
338
339
340 if (p2p->cfg->sd_request == NULL)
341 return;
342
343 if (rx_freq > 0)
344 freq = rx_freq;
345 else
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700346 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 p2p->cfg->channel);
348 if (freq < 0)
349 return;
350
351 if (len < 1 + 2)
352 return;
353
354 dialog_token = *pos++;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700355 p2p_dbg(p2p, "GAS Initial Request from " MACSTR
356 " (dialog token %u, freq %d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 MAC2STR(sa), dialog_token, rx_freq);
358
359 if (*pos != WLAN_EID_ADV_PROTO) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700360 p2p_dbg(p2p, "Unexpected IE in GAS Initial Request: %u", *pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361 return;
362 }
363 pos++;
364
365 slen = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800366 if (slen > end - pos || slen < 2) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700367 p2p_dbg(p2p, "Invalid IE in GAS Initial Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 return;
369 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800370 next = pos + slen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371 pos++; /* skip QueryRespLenLimit and PAME-BI */
372
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800373 if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700374 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 *pos);
376 return;
377 }
378
379 pos = next;
380 /* Query Request */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800381 if (end - pos < 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 return;
383 slen = WPA_GET_LE16(pos);
384 pos += 2;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800385 if (slen > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 return;
387 end = pos + slen;
388
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800389 /* ANQP Query Request */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800390 if (end - pos < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800392 if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700393 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 return;
395 }
396 pos += 2;
397
398 slen = WPA_GET_LE16(pos);
399 pos += 2;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800400 if (slen > end - pos || slen < 3 + 1) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700401 p2p_dbg(p2p, "Invalid ANQP Query Request length");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402 return;
403 }
404
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800405 if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
406 p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
407 WPA_GET_BE32(pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 return;
409 }
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800410 pos += 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700411
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800412 if (end - pos < 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413 return;
414 update_indic = WPA_GET_LE16(pos);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700415 p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 pos += 2;
417
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() */
421}
422
423
424void p2p_sd_response(struct p2p_data *p2p, int freq, const u8 *dst,
425 u8 dialog_token, const struct wpabuf *resp_tlvs)
426{
427 struct wpabuf *resp;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800428 size_t max_len;
429
430 /*
431 * In the 60 GHz, we have a smaller maximum frame length for management
432 * frames.
433 */
434 max_len = (freq > 56160) ? 928 : 1400;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435
436 /* TODO: fix the length limit to match with the maximum frame length */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800437 if (wpabuf_len(resp_tlvs) > max_len) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700438 p2p_dbg(p2p, "SD response long enough to require fragmentation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 if (p2p->sd_resp) {
440 /*
441 * TODO: Could consider storing the fragmented response
442 * separately for each peer to avoid having to drop old
443 * one if there is more than one pending SD query.
444 * Though, that would eat more memory, so there are
445 * also benefits to just using a single buffer.
446 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700447 p2p_dbg(p2p, "Drop previous SD response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448 wpabuf_free(p2p->sd_resp);
449 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700450 p2p->sd_resp = wpabuf_dup(resp_tlvs);
451 if (p2p->sd_resp == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700452 p2p_err(p2p, "Failed to allocate SD response fragmentation area");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700453 return;
454 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455 os_memcpy(p2p->sd_resp_addr, dst, ETH_ALEN);
456 p2p->sd_resp_dialog_token = dialog_token;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 p2p->sd_resp_pos = 0;
458 p2p->sd_frag_id = 0;
459 resp = p2p_build_sd_response(dialog_token, WLAN_STATUS_SUCCESS,
460 1, p2p->srv_update_indic, NULL);
461 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700462 p2p_dbg(p2p, "SD response fits in initial response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463 resp = p2p_build_sd_response(dialog_token,
464 WLAN_STATUS_SUCCESS, 0,
465 p2p->srv_update_indic, resp_tlvs);
466 }
467 if (resp == NULL)
468 return;
469
470 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
471 if (p2p_send_action(p2p, freq, dst, p2p->cfg->dev_addr,
472 p2p->cfg->dev_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473 wpabuf_head(resp), wpabuf_len(resp), 200) < 0)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700474 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475
476 wpabuf_free(resp);
477}
478
479
480void p2p_rx_gas_initial_resp(struct p2p_data *p2p, const u8 *sa,
481 const u8 *data, size_t len, int rx_freq)
482{
483 const u8 *pos = data;
484 const u8 *end = data + len;
485 const u8 *next;
486 u8 dialog_token;
487 u16 status_code;
488 u16 comeback_delay;
489 u16 slen;
490 u16 update_indic;
491
492 if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
493 os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700494 p2p_dbg(p2p, "Ignore unexpected GAS Initial Response from "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 MACSTR, MAC2STR(sa));
496 return;
497 }
498 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
499 p2p_clear_timeout(p2p);
500
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700501 p2p_dbg(p2p, "Received GAS Initial Response from " MACSTR " (len=%d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 MAC2STR(sa), (int) len);
503
504 if (len < 5 + 2) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700505 p2p_dbg(p2p, "Too short GAS Initial Response frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506 return;
507 }
508
509 dialog_token = *pos++;
510 /* TODO: check dialog_token match */
511 status_code = WPA_GET_LE16(pos);
512 pos += 2;
513 comeback_delay = WPA_GET_LE16(pos);
514 pos += 2;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700515 p2p_dbg(p2p, "dialog_token=%u status_code=%u comeback_delay=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700516 dialog_token, status_code, comeback_delay);
517 if (status_code) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700518 p2p_dbg(p2p, "Service Discovery failed: status code %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700519 status_code);
520 return;
521 }
522
523 if (*pos != WLAN_EID_ADV_PROTO) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700524 p2p_dbg(p2p, "Unexpected IE in GAS Initial Response: %u", *pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 return;
526 }
527 pos++;
528
529 slen = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800530 if (slen > end - pos || slen < 2) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700531 p2p_dbg(p2p, "Invalid IE in GAS Initial Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 return;
533 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800534 next = pos + slen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 pos++; /* skip QueryRespLenLimit and PAME-BI */
536
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800537 if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700538 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 *pos);
540 return;
541 }
542
543 pos = next;
544 /* Query Response */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800545 if (end - pos < 2) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700546 p2p_dbg(p2p, "Too short Query Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547 return;
548 }
549 slen = WPA_GET_LE16(pos);
550 pos += 2;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700551 p2p_dbg(p2p, "Query Response Length: %d", slen);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800552 if (slen > end - pos) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700553 p2p_dbg(p2p, "Not enough Query Response data");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 return;
555 }
556 end = pos + slen;
557
558 if (comeback_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700559 p2p_dbg(p2p, "Fragmented response - request fragments");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560 if (p2p->sd_rx_resp) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700561 p2p_dbg(p2p, "Drop old SD reassembly buffer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562 wpabuf_free(p2p->sd_rx_resp);
563 p2p->sd_rx_resp = NULL;
564 }
565 p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
566 return;
567 }
568
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800569 /* ANQP Query Response */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800570 if (end - pos < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700571 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800572 if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700573 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574 return;
575 }
576 pos += 2;
577
578 slen = WPA_GET_LE16(pos);
579 pos += 2;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800580 if (slen > end - pos || slen < 3 + 1) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700581 p2p_dbg(p2p, "Invalid ANQP Query Response length");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 return;
583 }
584
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800585 if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
586 p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
587 WPA_GET_BE32(pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588 return;
589 }
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800590 pos += 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700591
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800592 if (end - pos < 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 return;
594 update_indic = WPA_GET_LE16(pos);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700595 p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 pos += 2;
597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598 p2p->sd_peer = NULL;
599
600 if (p2p->sd_query) {
601 if (!p2p->sd_query->for_all_peers) {
602 struct p2p_sd_query *q;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700603 p2p_dbg(p2p, "Remove completed SD query %p",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 p2p->sd_query);
605 q = p2p->sd_query;
606 p2p_unlink_sd_query(p2p, p2p->sd_query);
607 p2p_free_sd_query(q);
608 }
609 p2p->sd_query = NULL;
610 }
611
612 if (p2p->cfg->sd_response)
613 p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa, update_indic,
614 pos, end - pos);
615 p2p_continue_find(p2p);
616}
617
618
619void p2p_rx_gas_comeback_req(struct p2p_data *p2p, const u8 *sa,
620 const u8 *data, size_t len, int rx_freq)
621{
622 struct wpabuf *resp;
623 u8 dialog_token;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800624 size_t frag_len, max_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625 int more = 0;
626
627 wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Request", data, len);
628 if (len < 1)
629 return;
630 dialog_token = *data;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700631 p2p_dbg(p2p, "Dialog Token: %u", dialog_token);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632 if (dialog_token != p2p->sd_resp_dialog_token) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700633 p2p_dbg(p2p, "No pending SD response fragment for dialog token %u",
634 dialog_token);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 return;
636 }
637
638 if (p2p->sd_resp == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700639 p2p_dbg(p2p, "No pending SD response fragment available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 return;
641 }
642 if (os_memcmp(sa, p2p->sd_resp_addr, ETH_ALEN) != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700643 p2p_dbg(p2p, "No pending SD response fragment for " MACSTR,
644 MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 return;
646 }
647
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800648 /*
649 * In the 60 GHz, we have a smaller maximum frame length for management
650 * frames.
651 */
652 max_len = (rx_freq > 56160) ? 928 : 1400;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700653 frag_len = wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800654 if (frag_len > max_len) {
655 frag_len = max_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656 more = 1;
657 }
658 resp = p2p_build_gas_comeback_resp(dialog_token, WLAN_STATUS_SUCCESS,
659 p2p->srv_update_indic,
660 wpabuf_head_u8(p2p->sd_resp) +
661 p2p->sd_resp_pos, frag_len,
662 p2p->sd_frag_id, more,
663 wpabuf_len(p2p->sd_resp));
664 if (resp == NULL)
665 return;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700666 p2p_dbg(p2p, "Send GAS Comeback Response (frag_id %d more=%d frag_len=%d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667 p2p->sd_frag_id, more, (int) frag_len);
668 p2p->sd_frag_id++;
669 p2p->sd_resp_pos += frag_len;
670
671 if (more) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700672 p2p_dbg(p2p, "%d more bytes remain to be sent",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673 (int) (wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos));
674 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700675 p2p_dbg(p2p, "All fragments of SD response sent");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 wpabuf_free(p2p->sd_resp);
677 p2p->sd_resp = NULL;
678 }
679
680 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
681 if (p2p_send_action(p2p, rx_freq, sa, p2p->cfg->dev_addr,
682 p2p->cfg->dev_addr,
683 wpabuf_head(resp), wpabuf_len(resp), 200) < 0)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700684 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685
686 wpabuf_free(resp);
687}
688
689
690void p2p_rx_gas_comeback_resp(struct p2p_data *p2p, const u8 *sa,
691 const u8 *data, size_t len, int rx_freq)
692{
693 const u8 *pos = data;
694 const u8 *end = data + len;
695 const u8 *next;
696 u8 dialog_token;
697 u16 status_code;
698 u8 frag_id;
699 u8 more_frags;
700 u16 comeback_delay;
701 u16 slen;
702
703 wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Response", data, len);
704
Dmitry Shmidt04949592012-07-19 12:16:46 -0700705 if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700706 os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700707 p2p_dbg(p2p, "Ignore unexpected GAS Comeback Response from "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 MACSTR, MAC2STR(sa));
709 return;
710 }
711 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
712 p2p_clear_timeout(p2p);
713
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700714 p2p_dbg(p2p, "Received GAS Comeback Response from " MACSTR " (len=%d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715 MAC2STR(sa), (int) len);
716
717 if (len < 6 + 2) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700718 p2p_dbg(p2p, "Too short GAS Comeback Response frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719 return;
720 }
721
722 dialog_token = *pos++;
723 /* TODO: check dialog_token match */
724 status_code = WPA_GET_LE16(pos);
725 pos += 2;
726 frag_id = *pos & 0x7f;
727 more_frags = (*pos & 0x80) >> 7;
728 pos++;
729 comeback_delay = WPA_GET_LE16(pos);
730 pos += 2;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700731 p2p_dbg(p2p, "dialog_token=%u status_code=%u frag_id=%d more_frags=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 "comeback_delay=%u",
733 dialog_token, status_code, frag_id, more_frags,
734 comeback_delay);
735 /* TODO: check frag_id match */
736 if (status_code) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700737 p2p_dbg(p2p, "Service Discovery failed: status code %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 status_code);
739 return;
740 }
741
742 if (*pos != WLAN_EID_ADV_PROTO) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700743 p2p_dbg(p2p, "Unexpected IE in GAS Comeback Response: %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 *pos);
745 return;
746 }
747 pos++;
748
749 slen = *pos++;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800750 if (slen > end - pos || slen < 2) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700751 p2p_dbg(p2p, "Invalid IE in GAS Comeback Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752 return;
753 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800754 next = pos + slen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 pos++; /* skip QueryRespLenLimit and PAME-BI */
756
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800757 if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700758 p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 *pos);
760 return;
761 }
762
763 pos = next;
764 /* Query Response */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800765 if (end - pos < 2) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700766 p2p_dbg(p2p, "Too short Query Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 return;
768 }
769 slen = WPA_GET_LE16(pos);
770 pos += 2;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700771 p2p_dbg(p2p, "Query Response Length: %d", slen);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800772 if (slen > end - pos) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700773 p2p_dbg(p2p, "Not enough Query Response data");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 return;
775 }
776 if (slen == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700777 p2p_dbg(p2p, "No Query Response data");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778 return;
779 }
780 end = pos + slen;
781
782 if (p2p->sd_rx_resp) {
783 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800784 * ANQP header is only included in the first fragment; rest of
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785 * the fragments start with continue TLVs.
786 */
787 goto skip_nqp_header;
788 }
789
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800790 /* ANQP Query Response */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800791 if (end - pos < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800793 if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700794 p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795 return;
796 }
797 pos += 2;
798
799 slen = WPA_GET_LE16(pos);
800 pos += 2;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700801 p2p_dbg(p2p, "ANQP Query Response length: %u", slen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802 if (slen < 3 + 1) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700803 p2p_dbg(p2p, "Invalid ANQP Query Response length");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804 return;
805 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800806 if (end - pos < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807 return;
808
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800809 if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
810 p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
811 WPA_GET_BE32(pos));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812 return;
813 }
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800814 pos += 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800816 if (end - pos < 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817 return;
818 p2p->sd_rx_update_indic = WPA_GET_LE16(pos);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700819 p2p_dbg(p2p, "Service Update Indicator: %u", p2p->sd_rx_update_indic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 pos += 2;
821
822skip_nqp_header:
823 if (wpabuf_resize(&p2p->sd_rx_resp, end - pos) < 0)
824 return;
825 wpabuf_put_data(p2p->sd_rx_resp, pos, end - pos);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700826 p2p_dbg(p2p, "Current SD reassembly buffer length: %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 (unsigned int) wpabuf_len(p2p->sd_rx_resp));
828
829 if (more_frags) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700830 p2p_dbg(p2p, "More fragments remains");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 /* TODO: what would be a good size limit? */
832 if (wpabuf_len(p2p->sd_rx_resp) > 64000) {
833 wpabuf_free(p2p->sd_rx_resp);
834 p2p->sd_rx_resp = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700835 p2p_dbg(p2p, "Too long SD response - drop it");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 return;
837 }
838 p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
839 return;
840 }
841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842 p2p->sd_peer = NULL;
843
844 if (p2p->sd_query) {
845 if (!p2p->sd_query->for_all_peers) {
846 struct p2p_sd_query *q;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700847 p2p_dbg(p2p, "Remove completed SD query %p",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700848 p2p->sd_query);
849 q = p2p->sd_query;
850 p2p_unlink_sd_query(p2p, p2p->sd_query);
851 p2p_free_sd_query(q);
852 }
853 p2p->sd_query = NULL;
854 }
855
856 if (p2p->cfg->sd_response)
857 p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa,
858 p2p->sd_rx_update_indic,
859 wpabuf_head(p2p->sd_rx_resp),
860 wpabuf_len(p2p->sd_rx_resp));
861 wpabuf_free(p2p->sd_rx_resp);
862 p2p->sd_rx_resp = NULL;
863
864 p2p_continue_find(p2p);
865}
866
867
868void * p2p_sd_request(struct p2p_data *p2p, const u8 *dst,
869 const struct wpabuf *tlvs)
870{
871 struct p2p_sd_query *q;
872
873 q = os_zalloc(sizeof(*q));
874 if (q == NULL)
875 return NULL;
876
877 if (dst)
878 os_memcpy(q->peer, dst, ETH_ALEN);
879 else
880 q->for_all_peers = 1;
881
882 q->tlvs = wpabuf_dup(tlvs);
883 if (q->tlvs == NULL) {
884 p2p_free_sd_query(q);
885 return NULL;
886 }
887
888 q->next = p2p->sd_queries;
889 p2p->sd_queries = q;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700890 p2p_dbg(p2p, "Added SD Query %p", q);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891
Irfan Sheriff96161872012-04-11 18:07:13 -0700892 if (dst == NULL) {
893 struct p2p_device *dev;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800894
895 p2p->num_p2p_sd_queries++;
896
897 /* Update all the devices for the newly added broadcast query */
898 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
899 if (dev->sd_pending_bcast_queries <= 0)
900 dev->sd_pending_bcast_queries = 1;
901 else
902 dev->sd_pending_bcast_queries++;
903 }
Irfan Sheriff96161872012-04-11 18:07:13 -0700904 }
905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 return q;
907}
908
909
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700910#ifdef CONFIG_WIFI_DISPLAY
911void * p2p_sd_request_wfd(struct p2p_data *p2p, const u8 *dst,
912 const struct wpabuf *tlvs)
913{
914 struct p2p_sd_query *q;
915 q = p2p_sd_request(p2p, dst, tlvs);
916 if (q)
917 q->wsd = 1;
918 return q;
919}
920#endif /* CONFIG_WIFI_DISPLAY */
921
922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923void p2p_sd_service_update(struct p2p_data *p2p)
924{
925 p2p->srv_update_indic++;
926}
927
928
929int p2p_sd_cancel_request(struct p2p_data *p2p, void *req)
930{
931 if (p2p_unlink_sd_query(p2p, req)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700932 p2p_dbg(p2p, "Cancel pending SD query %p", req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 p2p_free_sd_query(req);
934 return 0;
935 }
936 return -1;
937}