]>
Commit | Line | Data |
---|---|---|
b22128ef JM |
1 | /* |
2 | * Wi-Fi Direct - P2P module | |
3 | * Copyright (c) 2009-2010, Atheros Communications | |
4 | * | |
e22d4d95 JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
b22128ef JM |
7 | */ |
8 | ||
9 | #include "includes.h" | |
10 | ||
11 | #include "common.h" | |
12 | #include "eloop.h" | |
c58eed6d | 13 | #include "common/defs.h" |
b22128ef JM |
14 | #include "common/ieee802_11_defs.h" |
15 | #include "common/ieee802_11_common.h" | |
86bd36f0 | 16 | #include "common/wpa_ctrl.h" |
51775096 BG |
17 | #include "crypto/sha256.h" |
18 | #include "crypto/crypto.h" | |
b22128ef JM |
19 | #include "wps/wps_i.h" |
20 | #include "p2p_i.h" | |
21 | #include "p2p.h" | |
22 | ||
23 | ||
24 | static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx); | |
25 | static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev); | |
26 | static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, | |
27 | const u8 *sa, const u8 *data, size_t len, | |
28 | int rx_freq); | |
29 | static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, | |
30 | const u8 *sa, const u8 *data, | |
31 | size_t len); | |
32 | static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx); | |
40c03fd4 | 33 | static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx); |
b22128ef JM |
34 | |
35 | ||
40c03fd4 JM |
36 | /* |
37 | * p2p_scan recovery timeout | |
38 | * | |
39 | * Many drivers are using 30 second timeout on scan results. Allow a bit larger | |
40 | * timeout for this to avoid hitting P2P timeout unnecessarily. | |
41 | */ | |
42 | #define P2P_SCAN_TIMEOUT 35 | |
43 | ||
b22128ef JM |
44 | /** |
45 | * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer | |
46 | * entries will be removed | |
47 | */ | |
dcdd3838 DS |
48 | #ifndef P2P_PEER_EXPIRATION_AGE |
49 | #define P2P_PEER_EXPIRATION_AGE 60 | |
50 | #endif /* P2P_PEER_EXPIRATION_AGE */ | |
b22128ef | 51 | |
b22128ef | 52 | |
8c0d0ff2 | 53 | void p2p_expire_peers(struct p2p_data *p2p) |
b22128ef JM |
54 | { |
55 | struct p2p_device *dev, *n; | |
acb69cec | 56 | struct os_reltime now; |
1d277f02 | 57 | size_t i; |
b22128ef | 58 | |
acb69cec | 59 | os_get_reltime(&now); |
b22128ef JM |
60 | dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) { |
61 | if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec) | |
62 | continue; | |
b1aebbc4 | 63 | |
a691d99f JJ |
64 | if (dev == p2p->go_neg_peer) { |
65 | /* | |
66 | * GO Negotiation is in progress with the peer, so | |
67 | * don't expire the peer entry until GO Negotiation | |
68 | * fails or times out. | |
69 | */ | |
70 | continue; | |
71 | } | |
72 | ||
b1aebbc4 JM |
73 | if (p2p->cfg->go_connected && |
74 | p2p->cfg->go_connected(p2p->cfg->cb_ctx, | |
75 | dev->info.p2p_device_addr)) { | |
76 | /* | |
77 | * We are connected as a client to a group in which the | |
78 | * peer is the GO, so do not expire the peer entry. | |
79 | */ | |
acb69cec | 80 | os_get_reltime(&dev->last_seen); |
b1aebbc4 JM |
81 | continue; |
82 | } | |
83 | ||
1d277f02 JM |
84 | for (i = 0; i < p2p->num_groups; i++) { |
85 | if (p2p_group_is_client_connected( | |
86 | p2p->groups[i], dev->info.p2p_device_addr)) | |
87 | break; | |
88 | } | |
89 | if (i < p2p->num_groups) { | |
90 | /* | |
91 | * The peer is connected as a client in a group where | |
92 | * we are the GO, so do not expire the peer entry. | |
93 | */ | |
acb69cec | 94 | os_get_reltime(&dev->last_seen); |
1d277f02 JM |
95 | continue; |
96 | } | |
97 | ||
ed496f13 JM |
98 | p2p_dbg(p2p, "Expiring old peer entry " MACSTR, |
99 | MAC2STR(dev->info.p2p_device_addr)); | |
b22128ef JM |
100 | dl_list_del(&dev->list); |
101 | p2p_device_free(p2p, dev); | |
102 | } | |
103 | } | |
104 | ||
105 | ||
b22128ef JM |
106 | static const char * p2p_state_txt(int state) |
107 | { | |
108 | switch (state) { | |
109 | case P2P_IDLE: | |
110 | return "IDLE"; | |
111 | case P2P_SEARCH: | |
112 | return "SEARCH"; | |
113 | case P2P_CONNECT: | |
114 | return "CONNECT"; | |
115 | case P2P_CONNECT_LISTEN: | |
116 | return "CONNECT_LISTEN"; | |
117 | case P2P_GO_NEG: | |
118 | return "GO_NEG"; | |
119 | case P2P_LISTEN_ONLY: | |
120 | return "LISTEN_ONLY"; | |
121 | case P2P_WAIT_PEER_CONNECT: | |
122 | return "WAIT_PEER_CONNECT"; | |
123 | case P2P_WAIT_PEER_IDLE: | |
124 | return "WAIT_PEER_IDLE"; | |
125 | case P2P_SD_DURING_FIND: | |
126 | return "SD_DURING_FIND"; | |
127 | case P2P_PROVISIONING: | |
128 | return "PROVISIONING"; | |
129 | case P2P_PD_DURING_FIND: | |
130 | return "PD_DURING_FIND"; | |
131 | case P2P_INVITE: | |
132 | return "INVITE"; | |
133 | case P2P_INVITE_LISTEN: | |
134 | return "INVITE_LISTEN"; | |
135 | default: | |
136 | return "?"; | |
137 | } | |
138 | } | |
139 | ||
140 | ||
4c559019 JM |
141 | const char * p2p_get_state_txt(struct p2p_data *p2p) |
142 | { | |
143 | return p2p_state_txt(p2p->state); | |
144 | } | |
145 | ||
146 | ||
6df08d03 KV |
147 | struct p2ps_advertisement * p2p_get_p2ps_adv_list(struct p2p_data *p2p) |
148 | { | |
149 | return p2p ? p2p->p2ps_adv_list : NULL; | |
150 | } | |
151 | ||
152 | ||
6d908514 KV |
153 | void p2p_set_intended_addr(struct p2p_data *p2p, const u8 *intended_addr) |
154 | { | |
155 | if (p2p && intended_addr) | |
156 | os_memcpy(p2p->intended_addr, intended_addr, ETH_ALEN); | |
157 | } | |
158 | ||
159 | ||
ec437d9e JJ |
160 | u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr) |
161 | { | |
162 | struct p2p_device *dev = NULL; | |
163 | ||
164 | if (!addr || !p2p) | |
165 | return 0; | |
166 | ||
167 | dev = p2p_get_device(p2p, addr); | |
168 | if (dev) | |
169 | return dev->wps_prov_info; | |
170 | else | |
171 | return 0; | |
172 | } | |
173 | ||
174 | ||
10531d21 | 175 | void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr) |
ec437d9e JJ |
176 | { |
177 | struct p2p_device *dev = NULL; | |
178 | ||
10531d21 | 179 | if (!addr || !p2p) |
ec437d9e JJ |
180 | return; |
181 | ||
10531d21 | 182 | dev = p2p_get_device(p2p, addr); |
ec437d9e JJ |
183 | if (dev) |
184 | dev->wps_prov_info = 0; | |
185 | } | |
186 | ||
187 | ||
b22128ef JM |
188 | void p2p_set_state(struct p2p_data *p2p, int new_state) |
189 | { | |
ed496f13 | 190 | p2p_dbg(p2p, "State %s -> %s", |
b22128ef JM |
191 | p2p_state_txt(p2p->state), p2p_state_txt(new_state)); |
192 | p2p->state = new_state; | |
e3bd6e9d IP |
193 | |
194 | if (new_state == P2P_IDLE && p2p->pending_channel) { | |
195 | p2p_dbg(p2p, "Apply change in listen channel"); | |
196 | p2p->cfg->reg_class = p2p->pending_reg_class; | |
197 | p2p->cfg->channel = p2p->pending_channel; | |
198 | p2p->pending_reg_class = 0; | |
199 | p2p->pending_channel = 0; | |
200 | } | |
b22128ef JM |
201 | } |
202 | ||
203 | ||
204 | void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec) | |
205 | { | |
ed496f13 | 206 | p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec", |
b22128ef JM |
207 | p2p_state_txt(p2p->state), sec, usec); |
208 | eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); | |
209 | eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL); | |
210 | } | |
211 | ||
212 | ||
213 | void p2p_clear_timeout(struct p2p_data *p2p) | |
214 | { | |
ed496f13 | 215 | p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state)); |
b22128ef JM |
216 | eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); |
217 | } | |
218 | ||
219 | ||
8c00fd00 | 220 | void p2p_go_neg_failed(struct p2p_data *p2p, int status) |
b22128ef JM |
221 | { |
222 | struct p2p_go_neg_results res; | |
8c00fd00 JM |
223 | struct p2p_device *peer = p2p->go_neg_peer; |
224 | ||
225 | if (!peer) | |
226 | return; | |
227 | ||
0c6eee8b | 228 | eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL); |
9d118320 RR |
229 | if (p2p->state != P2P_SEARCH) { |
230 | /* | |
231 | * Clear timeouts related to GO Negotiation if no new p2p_find | |
232 | * has been started. | |
233 | */ | |
234 | p2p_clear_timeout(p2p); | |
235 | p2p_set_state(p2p, P2P_IDLE); | |
236 | } | |
8c00fd00 JM |
237 | |
238 | peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE; | |
239 | peer->wps_method = WPS_NOT_READY; | |
240 | peer->oob_pw_id = 0; | |
241 | wpabuf_free(peer->go_neg_conf); | |
242 | peer->go_neg_conf = NULL; | |
b22128ef JM |
243 | p2p->go_neg_peer = NULL; |
244 | ||
245 | os_memset(&res, 0, sizeof(res)); | |
246 | res.status = status; | |
8c00fd00 JM |
247 | os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN); |
248 | os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN); | |
b22128ef JM |
249 | p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); |
250 | } | |
251 | ||
252 | ||
96beff11 | 253 | static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc) |
b22128ef JM |
254 | { |
255 | unsigned int r, tu; | |
256 | int freq; | |
257 | struct wpabuf *ies; | |
258 | ||
ed496f13 | 259 | p2p_dbg(p2p, "Starting short listen state (state=%s)", |
b22128ef JM |
260 | p2p_state_txt(p2p->state)); |
261 | ||
5661bd0f JJ |
262 | if (p2p->pending_listen_freq) { |
263 | /* We have a pending p2p_listen request */ | |
264 | p2p_dbg(p2p, "p2p_listen command pending already"); | |
265 | return; | |
266 | } | |
267 | ||
9ccd9165 | 268 | freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel); |
b22128ef | 269 | if (freq < 0) { |
ed496f13 | 270 | p2p_dbg(p2p, "Unknown regulatory class/channel"); |
b22128ef JM |
271 | return; |
272 | } | |
273 | ||
df2508d7 JM |
274 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) |
275 | r = 0; | |
b22128ef JM |
276 | tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) + |
277 | p2p->min_disc_int) * 100; | |
96beff11 JM |
278 | if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu) |
279 | tu = p2p->max_disc_tu; | |
280 | if (!dev_disc && tu < 100) | |
281 | tu = 100; /* Need to wait in non-device discovery use cases */ | |
282 | if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen) | |
283 | tu = p2p->cfg->max_listen * 1000 / 1024; | |
284 | ||
285 | if (tu == 0) { | |
ed496f13 | 286 | p2p_dbg(p2p, "Skip listen state since duration was 0 TU"); |
96beff11 JM |
287 | p2p_set_timeout(p2p, 0, 0); |
288 | return; | |
289 | } | |
b22128ef | 290 | |
e12c4004 | 291 | ies = p2p_build_probe_resp_ies(p2p, NULL, 0); |
b22128ef JM |
292 | if (ies == NULL) |
293 | return; | |
294 | ||
5661bd0f JJ |
295 | p2p->pending_listen_freq = freq; |
296 | p2p->pending_listen_sec = 0; | |
297 | p2p->pending_listen_usec = 1024 * tu; | |
298 | ||
b22128ef JM |
299 | if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000, |
300 | ies) < 0) { | |
ed496f13 | 301 | p2p_dbg(p2p, "Failed to start listen mode"); |
b22128ef JM |
302 | p2p->pending_listen_freq = 0; |
303 | } | |
304 | wpabuf_free(ies); | |
305 | } | |
306 | ||
307 | ||
308 | int p2p_listen(struct p2p_data *p2p, unsigned int timeout) | |
309 | { | |
310 | int freq; | |
311 | struct wpabuf *ies; | |
312 | ||
ed496f13 | 313 | p2p_dbg(p2p, "Going to listen(only) state"); |
b22128ef | 314 | |
5661bd0f JJ |
315 | if (p2p->pending_listen_freq) { |
316 | /* We have a pending p2p_listen request */ | |
317 | p2p_dbg(p2p, "p2p_listen command pending already"); | |
318 | return -1; | |
319 | } | |
320 | ||
9ccd9165 | 321 | freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel); |
b22128ef | 322 | if (freq < 0) { |
ed496f13 | 323 | p2p_dbg(p2p, "Unknown regulatory class/channel"); |
b22128ef JM |
324 | return -1; |
325 | } | |
326 | ||
b22128ef JM |
327 | p2p->pending_listen_sec = timeout / 1000; |
328 | p2p->pending_listen_usec = (timeout % 1000) * 1000; | |
329 | ||
330 | if (p2p->p2p_scan_running) { | |
5b9cecaf | 331 | if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) { |
ed496f13 | 332 | p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen"); |
d9bdba9f JM |
333 | return 0; |
334 | } | |
ed496f13 | 335 | p2p_dbg(p2p, "p2p_scan running - delay start of listen state"); |
b22128ef JM |
336 | p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN; |
337 | return 0; | |
338 | } | |
339 | ||
e12c4004 | 340 | ies = p2p_build_probe_resp_ies(p2p, NULL, 0); |
b22128ef JM |
341 | if (ies == NULL) |
342 | return -1; | |
343 | ||
5661bd0f JJ |
344 | p2p->pending_listen_freq = freq; |
345 | ||
b22128ef | 346 | if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) { |
ed496f13 | 347 | p2p_dbg(p2p, "Failed to start listen mode"); |
b22128ef JM |
348 | p2p->pending_listen_freq = 0; |
349 | wpabuf_free(ies); | |
350 | return -1; | |
351 | } | |
352 | wpabuf_free(ies); | |
353 | ||
354 | p2p_set_state(p2p, P2P_LISTEN_ONLY); | |
355 | ||
356 | return 0; | |
357 | } | |
358 | ||
359 | ||
360 | static void p2p_device_clear_reported(struct p2p_data *p2p) | |
361 | { | |
362 | struct p2p_device *dev; | |
44abecbf | 363 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
b22128ef | 364 | dev->flags &= ~P2P_DEV_REPORTED; |
44abecbf JM |
365 | dev->sd_reqs = 0; |
366 | } | |
b22128ef JM |
367 | } |
368 | ||
369 | ||
370 | /** | |
371 | * p2p_get_device - Fetch a peer entry | |
372 | * @p2p: P2P module context from p2p_init() | |
373 | * @addr: P2P Device Address of the peer | |
374 | * Returns: Pointer to the device entry or %NULL if not found | |
375 | */ | |
376 | struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr) | |
377 | { | |
378 | struct p2p_device *dev; | |
379 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
c5db8e51 | 380 | if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0) |
b22128ef JM |
381 | return dev; |
382 | } | |
383 | return NULL; | |
384 | } | |
385 | ||
386 | ||
387 | /** | |
388 | * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address | |
389 | * @p2p: P2P module context from p2p_init() | |
390 | * @addr: P2P Interface Address of the peer | |
391 | * Returns: Pointer to the device entry or %NULL if not found | |
392 | */ | |
393 | struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p, | |
394 | const u8 *addr) | |
395 | { | |
396 | struct p2p_device *dev; | |
397 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
398 | if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0) | |
399 | return dev; | |
400 | } | |
401 | return NULL; | |
402 | } | |
403 | ||
404 | ||
405 | /** | |
406 | * p2p_create_device - Create a peer entry | |
407 | * @p2p: P2P module context from p2p_init() | |
408 | * @addr: P2P Device Address of the peer | |
409 | * Returns: Pointer to the device entry or %NULL on failure | |
410 | * | |
411 | * If there is already an entry for the peer, it will be returned instead of | |
412 | * creating a new one. | |
413 | */ | |
414 | static struct p2p_device * p2p_create_device(struct p2p_data *p2p, | |
415 | const u8 *addr) | |
416 | { | |
417 | struct p2p_device *dev, *oldest = NULL; | |
418 | size_t count = 0; | |
419 | ||
420 | dev = p2p_get_device(p2p, addr); | |
421 | if (dev) | |
422 | return dev; | |
423 | ||
424 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
425 | count++; | |
426 | if (oldest == NULL || | |
acb69cec | 427 | os_reltime_before(&dev->last_seen, &oldest->last_seen)) |
b22128ef JM |
428 | oldest = dev; |
429 | } | |
430 | if (count + 1 > p2p->cfg->max_peers && oldest) { | |
ed496f13 | 431 | p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer"); |
b22128ef JM |
432 | dl_list_del(&oldest->list); |
433 | p2p_device_free(p2p, oldest); | |
434 | } | |
435 | ||
436 | dev = os_zalloc(sizeof(*dev)); | |
437 | if (dev == NULL) | |
438 | return NULL; | |
439 | dl_list_add(&p2p->devices, &dev->list); | |
c5db8e51 | 440 | os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN); |
b22128ef JM |
441 | |
442 | return dev; | |
443 | } | |
444 | ||
445 | ||
446 | static void p2p_copy_client_info(struct p2p_device *dev, | |
447 | struct p2p_client_info *cli) | |
448 | { | |
5d1d69a1 HW |
449 | p2p_copy_filter_devname(dev->info.device_name, |
450 | sizeof(dev->info.device_name), | |
451 | cli->dev_name, cli->dev_name_len); | |
c5db8e51 KRK |
452 | dev->info.dev_capab = cli->dev_capab; |
453 | dev->info.config_methods = cli->config_methods; | |
454 | os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8); | |
e57ae6e1 JMB |
455 | dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types; |
456 | os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types, | |
457 | dev->info.wps_sec_dev_type_list_len); | |
b22128ef JM |
458 | } |
459 | ||
460 | ||
461 | static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr, | |
462 | const u8 *go_interface_addr, int freq, | |
4e8817f7 AO |
463 | const u8 *gi, size_t gi_len, |
464 | struct os_reltime *rx_time) | |
b22128ef JM |
465 | { |
466 | struct p2p_group_info info; | |
467 | size_t c; | |
468 | struct p2p_device *dev; | |
469 | ||
470 | if (gi == NULL) | |
471 | return 0; | |
472 | ||
473 | if (p2p_group_info_parse(gi, gi_len, &info) < 0) | |
474 | return -1; | |
475 | ||
476 | /* | |
477 | * Clear old data for this group; if the devices are still in the | |
478 | * group, the information will be restored in the loop following this. | |
479 | */ | |
480 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
b5472a45 | 481 | if (os_memcmp(dev->member_in_go_iface, go_interface_addr, |
b22128ef JM |
482 | ETH_ALEN) == 0) { |
483 | os_memset(dev->member_in_go_iface, 0, ETH_ALEN); | |
484 | os_memset(dev->member_in_go_dev, 0, ETH_ALEN); | |
485 | } | |
486 | } | |
487 | ||
488 | for (c = 0; c < info.num_clients; c++) { | |
489 | struct p2p_client_info *cli = &info.client[c]; | |
2f0c8936 MK |
490 | if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr, |
491 | ETH_ALEN) == 0) | |
492 | continue; /* ignore our own entry */ | |
b22128ef JM |
493 | dev = p2p_get_device(p2p, cli->p2p_device_addr); |
494 | if (dev) { | |
b22128ef | 495 | if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY | |
f33bc035 JM |
496 | P2P_DEV_PROBE_REQ_ONLY)) { |
497 | /* | |
498 | * Update information since we have not | |
499 | * received this directly from the client. | |
500 | */ | |
b22128ef | 501 | p2p_copy_client_info(dev, cli); |
f33bc035 JM |
502 | } else { |
503 | /* | |
504 | * Need to update P2P Client Discoverability | |
505 | * flag since it is valid only in P2P Group | |
506 | * Info attribute. | |
507 | */ | |
508 | dev->info.dev_capab &= | |
509 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
510 | dev->info.dev_capab |= | |
511 | cli->dev_capab & | |
512 | P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
513 | } | |
b22128ef JM |
514 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { |
515 | dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; | |
516 | } | |
517 | } else { | |
518 | dev = p2p_create_device(p2p, cli->p2p_device_addr); | |
519 | if (dev == NULL) | |
520 | continue; | |
521 | dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY; | |
522 | p2p_copy_client_info(dev, cli); | |
523 | dev->oper_freq = freq; | |
c5db8e51 KRK |
524 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, |
525 | dev->info.p2p_device_addr, | |
8fd7dc1b JB |
526 | &dev->info, 1); |
527 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; | |
b22128ef JM |
528 | } |
529 | ||
530 | os_memcpy(dev->interface_addr, cli->p2p_interface_addr, | |
531 | ETH_ALEN); | |
4e8817f7 | 532 | os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime)); |
b22128ef JM |
533 | os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN); |
534 | os_memcpy(dev->member_in_go_iface, go_interface_addr, | |
535 | ETH_ALEN); | |
8f524099 | 536 | dev->flags |= P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT; |
b22128ef JM |
537 | } |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
542 | ||
ed496f13 JM |
543 | static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev, |
544 | int probe_req, const struct p2p_message *msg) | |
b67d0d9e JM |
545 | { |
546 | os_memcpy(dev->info.device_name, msg->device_name, | |
547 | sizeof(dev->info.device_name)); | |
548 | ||
549 | if (msg->manufacturer && | |
550 | msg->manufacturer_len < sizeof(dev->info.manufacturer)) { | |
551 | os_memset(dev->info.manufacturer, 0, | |
552 | sizeof(dev->info.manufacturer)); | |
553 | os_memcpy(dev->info.manufacturer, msg->manufacturer, | |
554 | msg->manufacturer_len); | |
555 | } | |
556 | ||
557 | if (msg->model_name && | |
558 | msg->model_name_len < sizeof(dev->info.model_name)) { | |
559 | os_memset(dev->info.model_name, 0, | |
560 | sizeof(dev->info.model_name)); | |
561 | os_memcpy(dev->info.model_name, msg->model_name, | |
562 | msg->model_name_len); | |
563 | } | |
564 | ||
565 | if (msg->model_number && | |
566 | msg->model_number_len < sizeof(dev->info.model_number)) { | |
567 | os_memset(dev->info.model_number, 0, | |
568 | sizeof(dev->info.model_number)); | |
569 | os_memcpy(dev->info.model_number, msg->model_number, | |
570 | msg->model_number_len); | |
571 | } | |
572 | ||
573 | if (msg->serial_number && | |
574 | msg->serial_number_len < sizeof(dev->info.serial_number)) { | |
575 | os_memset(dev->info.serial_number, 0, | |
576 | sizeof(dev->info.serial_number)); | |
577 | os_memcpy(dev->info.serial_number, msg->serial_number, | |
578 | msg->serial_number_len); | |
579 | } | |
580 | ||
581 | if (msg->pri_dev_type) | |
582 | os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type, | |
583 | sizeof(dev->info.pri_dev_type)); | |
584 | else if (msg->wps_pri_dev_type) | |
585 | os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type, | |
586 | sizeof(dev->info.pri_dev_type)); | |
587 | ||
588 | if (msg->wps_sec_dev_type_list) { | |
589 | os_memcpy(dev->info.wps_sec_dev_type_list, | |
590 | msg->wps_sec_dev_type_list, | |
591 | msg->wps_sec_dev_type_list_len); | |
592 | dev->info.wps_sec_dev_type_list_len = | |
593 | msg->wps_sec_dev_type_list_len; | |
594 | } | |
595 | ||
596 | if (msg->capability) { | |
f33bc035 JM |
597 | /* |
598 | * P2P Client Discoverability bit is reserved in all frames | |
599 | * that use this function, so do not change its value here. | |
600 | */ | |
601 | dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
602 | dev->info.dev_capab |= msg->capability[0] & | |
603 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
b67d0d9e JM |
604 | dev->info.group_capab = msg->capability[1]; |
605 | } | |
606 | ||
607 | if (msg->ext_listen_timing) { | |
608 | dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing); | |
609 | dev->ext_listen_interval = | |
610 | WPA_GET_LE16(msg->ext_listen_timing + 2); | |
611 | } | |
612 | ||
613 | if (!probe_req) { | |
954ee628 JM |
614 | u16 new_config_methods; |
615 | new_config_methods = msg->config_methods ? | |
b67d0d9e | 616 | msg->config_methods : msg->wps_config_methods; |
954ee628 JM |
617 | if (new_config_methods && |
618 | dev->info.config_methods != new_config_methods) { | |
ed496f13 JM |
619 | p2p_dbg(p2p, "Update peer " MACSTR |
620 | " config_methods 0x%x -> 0x%x", | |
621 | MAC2STR(dev->info.p2p_device_addr), | |
622 | dev->info.config_methods, | |
623 | new_config_methods); | |
954ee628 JM |
624 | dev->info.config_methods = new_config_methods; |
625 | } | |
b67d0d9e JM |
626 | } |
627 | } | |
628 | ||
629 | ||
71a0e395 JM |
630 | static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies, |
631 | size_t ies_len) | |
632 | { | |
633 | const u8 *pos, *end; | |
634 | u8 id, len; | |
635 | ||
636 | wpabuf_free(dev->info.vendor_elems); | |
637 | dev->info.vendor_elems = NULL; | |
638 | ||
639 | end = ies + ies_len; | |
640 | ||
d6ee858c | 641 | for (pos = ies; end - pos > 1; pos += len) { |
71a0e395 JM |
642 | id = *pos++; |
643 | len = *pos++; | |
644 | ||
d6ee858c | 645 | if (len > end - pos) |
71a0e395 JM |
646 | break; |
647 | ||
648 | if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3) | |
649 | continue; | |
650 | ||
651 | if (len >= 4) { | |
652 | u32 type = WPA_GET_BE32(pos); | |
653 | ||
654 | if (type == WPA_IE_VENDOR_TYPE || | |
655 | type == WMM_IE_VENDOR_TYPE || | |
656 | type == WPS_IE_VENDOR_TYPE || | |
657 | type == P2P_IE_VENDOR_TYPE || | |
658 | type == WFD_IE_VENDOR_TYPE) | |
659 | continue; | |
660 | } | |
661 | ||
662 | /* Unknown vendor element - make raw IE data available */ | |
663 | if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0) | |
664 | break; | |
665 | wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len); | |
666 | } | |
667 | } | |
668 | ||
669 | ||
4b32f6a5 JT |
670 | static int p2p_compare_wfd_info(struct p2p_device *dev, |
671 | const struct p2p_message *msg) | |
672 | { | |
673 | if (dev->info.wfd_subelems && msg->wfd_subelems) { | |
674 | if (dev->info.wfd_subelems->used != msg->wfd_subelems->used) | |
675 | return 1; | |
676 | ||
677 | return os_memcmp(dev->info.wfd_subelems->buf, | |
678 | msg->wfd_subelems->buf, | |
679 | dev->info.wfd_subelems->used); | |
680 | } | |
681 | if (dev->info.wfd_subelems || msg->wfd_subelems) | |
682 | return 1; | |
683 | ||
684 | return 0; | |
685 | } | |
686 | ||
687 | ||
b22128ef | 688 | /** |
c98b83f2 | 689 | * p2p_add_device - Add peer entries based on scan results or P2P frames |
b22128ef JM |
690 | * @p2p: P2P module context from p2p_init() |
691 | * @addr: Source address of Beacon or Probe Response frame (may be either | |
692 | * P2P Device Address or P2P Interface Address) | |
693 | * @level: Signal level (signal strength of the received frame from the peer) | |
694 | * @freq: Frequency on which the Beacon or Probe Response frame was received | |
c5f10e80 | 695 | * @rx_time: Time when the result was received |
b22128ef JM |
696 | * @ies: IEs from the Beacon or Probe Response frame |
697 | * @ies_len: Length of ies buffer in octets | |
c98b83f2 | 698 | * @scan_res: Whether this was based on scan results |
b22128ef JM |
699 | * Returns: 0 on success, -1 on failure |
700 | * | |
701 | * If the scan result is for a GO, the clients in the group will also be added | |
17bef1e9 AC |
702 | * to the peer table. This function can also be used with some other frames |
703 | * like Provision Discovery Request that contains P2P Capability and P2P Device | |
704 | * Info attributes. | |
b22128ef | 705 | */ |
3dfd0484 | 706 | int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, |
acb69cec | 707 | struct os_reltime *rx_time, int level, const u8 *ies, |
3dfd0484 | 708 | size_t ies_len, int scan_res) |
b22128ef JM |
709 | { |
710 | struct p2p_device *dev; | |
711 | struct p2p_message msg; | |
712 | const u8 *p2p_dev_addr; | |
4b32f6a5 | 713 | int wfd_changed; |
0a6c9dc7 | 714 | int dev_name_changed; |
6f2c0607 | 715 | int i; |
acb69cec | 716 | struct os_reltime time_now; |
b22128ef JM |
717 | |
718 | os_memset(&msg, 0, sizeof(msg)); | |
719 | if (p2p_parse_ies(ies, ies_len, &msg)) { | |
ed496f13 | 720 | p2p_dbg(p2p, "Failed to parse P2P IE for a device entry"); |
b22128ef JM |
721 | p2p_parse_free(&msg); |
722 | return -1; | |
723 | } | |
724 | ||
725 | if (msg.p2p_device_addr) | |
726 | p2p_dev_addr = msg.p2p_device_addr; | |
727 | else if (msg.device_id) | |
728 | p2p_dev_addr = msg.device_id; | |
729 | else { | |
ed496f13 | 730 | p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id"); |
b22128ef JM |
731 | p2p_parse_free(&msg); |
732 | return -1; | |
733 | } | |
734 | ||
80c9582a JM |
735 | if (!is_zero_ether_addr(p2p->peer_filter) && |
736 | os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) { | |
ed496f13 JM |
737 | p2p_dbg(p2p, "Do not add peer filter for " MACSTR |
738 | " due to peer filter", MAC2STR(p2p_dev_addr)); | |
f96c1d76 | 739 | p2p_parse_free(&msg); |
80c9582a JM |
740 | return 0; |
741 | } | |
742 | ||
b22128ef JM |
743 | dev = p2p_create_device(p2p, p2p_dev_addr); |
744 | if (dev == NULL) { | |
745 | p2p_parse_free(&msg); | |
746 | return -1; | |
747 | } | |
3dfd0484 | 748 | |
c5f10e80 | 749 | if (rx_time == NULL) { |
acb69cec | 750 | os_get_reltime(&time_now); |
c5f10e80 JM |
751 | rx_time = &time_now; |
752 | } | |
3dfd0484 YD |
753 | |
754 | /* | |
755 | * Update the device entry only if the new peer | |
8f524099 AO |
756 | * entry is newer than the one previously stored, or if |
757 | * the device was previously seen as a P2P Client in a group | |
758 | * and the new entry isn't older than a threshold. | |
3dfd0484 | 759 | */ |
c5f10e80 | 760 | if (dev->last_seen.sec > 0 && |
8f524099 AO |
761 | os_reltime_before(rx_time, &dev->last_seen) && |
762 | (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) || | |
763 | os_reltime_expired(&dev->last_seen, rx_time, | |
764 | P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) { | |
765 | p2p_dbg(p2p, | |
766 | "Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u flags=0x%x)", | |
c5f10e80 JM |
767 | (unsigned int) rx_time->sec, |
768 | (unsigned int) rx_time->usec, | |
769 | (unsigned int) dev->last_seen.sec, | |
8f524099 AO |
770 | (unsigned int) dev->last_seen.usec, |
771 | dev->flags); | |
f96c1d76 | 772 | p2p_parse_free(&msg); |
3dfd0484 | 773 | return -1; |
f96c1d76 | 774 | } |
3dfd0484 | 775 | |
acb69cec | 776 | os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime)); |
3dfd0484 | 777 | |
8f524099 AO |
778 | dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY | |
779 | P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT); | |
b22128ef JM |
780 | |
781 | if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0) | |
782 | os_memcpy(dev->interface_addr, addr, ETH_ALEN); | |
783 | if (msg.ssid && | |
9ed4eee3 | 784 | msg.ssid[1] <= sizeof(dev->oper_ssid) && |
b22128ef JM |
785 | (msg.ssid[1] != P2P_WILDCARD_SSID_LEN || |
786 | os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) | |
787 | != 0)) { | |
788 | os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]); | |
789 | dev->oper_ssid_len = msg.ssid[1]; | |
790 | } | |
791 | ||
0f282c76 NC |
792 | wpabuf_free(dev->info.p2ps_instance); |
793 | dev->info.p2ps_instance = NULL; | |
794 | if (msg.adv_service_instance && msg.adv_service_instance_len) | |
095b3c40 BG |
795 | dev->info.p2ps_instance = wpabuf_alloc_copy( |
796 | msg.adv_service_instance, msg.adv_service_instance_len); | |
095b3c40 | 797 | |
b22128ef JM |
798 | if (freq >= 2412 && freq <= 2484 && msg.ds_params && |
799 | *msg.ds_params >= 1 && *msg.ds_params <= 14) { | |
800 | int ds_freq; | |
801 | if (*msg.ds_params == 14) | |
802 | ds_freq = 2484; | |
803 | else | |
804 | ds_freq = 2407 + *msg.ds_params * 5; | |
805 | if (freq != ds_freq) { | |
ed496f13 | 806 | p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz", |
b22128ef JM |
807 | freq, ds_freq); |
808 | freq = ds_freq; | |
809 | } | |
810 | } | |
811 | ||
c98b83f2 | 812 | if (dev->listen_freq && dev->listen_freq != freq && scan_res) { |
ed496f13 JM |
813 | p2p_dbg(p2p, "Update Listen frequency based on scan results (" |
814 | MACSTR " %d -> %d MHz (DS param %d)", | |
c5db8e51 KRK |
815 | MAC2STR(dev->info.p2p_device_addr), dev->listen_freq, |
816 | freq, msg.ds_params ? *msg.ds_params : -1); | |
b22128ef | 817 | } |
c98b83f2 JM |
818 | if (scan_res) { |
819 | dev->listen_freq = freq; | |
820 | if (msg.group_info) | |
821 | dev->oper_freq = freq; | |
822 | } | |
6402fc43 | 823 | dev->info.level = level; |
b22128ef | 824 | |
bbad185c AO |
825 | dev_name_changed = os_strncmp(dev->info.device_name, msg.device_name, |
826 | WPS_DEV_NAME_MAX_LEN) != 0; | |
0a6c9dc7 | 827 | |
ed496f13 | 828 | p2p_copy_wps_info(p2p, dev, 0, &msg); |
e57ae6e1 | 829 | |
10c5d2a5 | 830 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
6f2c0607 JMB |
831 | wpabuf_free(dev->info.wps_vendor_ext[i]); |
832 | dev->info.wps_vendor_ext[i] = NULL; | |
833 | } | |
834 | ||
10c5d2a5 | 835 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
6f2c0607 JMB |
836 | if (msg.wps_vendor_ext[i] == NULL) |
837 | break; | |
838 | dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy( | |
839 | msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]); | |
840 | if (dev->info.wps_vendor_ext[i] == NULL) | |
841 | break; | |
842 | } | |
843 | ||
4b32f6a5 JT |
844 | wfd_changed = p2p_compare_wfd_info(dev, &msg); |
845 | ||
9a431d49 | 846 | if (wfd_changed) { |
9675ce35 | 847 | wpabuf_free(dev->info.wfd_subelems); |
9a431d49 MH |
848 | if (msg.wfd_subelems) |
849 | dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems); | |
850 | else | |
851 | dev->info.wfd_subelems = NULL; | |
9675ce35 JM |
852 | } |
853 | ||
c98b83f2 JM |
854 | if (scan_res) { |
855 | p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, | |
4e8817f7 AO |
856 | msg.group_info, msg.group_info_len, |
857 | rx_time); | |
c98b83f2 | 858 | } |
b22128ef JM |
859 | |
860 | p2p_parse_free(&msg); | |
861 | ||
71a0e395 JM |
862 | p2p_update_peer_vendor_elems(dev, ies, ies_len); |
863 | ||
095b3c40 | 864 | if (dev->flags & P2P_DEV_REPORTED && !wfd_changed && |
0a6c9dc7 | 865 | !dev_name_changed && |
095b3c40 BG |
866 | (!msg.adv_service_instance || |
867 | (dev->flags & P2P_DEV_P2PS_REPORTED))) | |
b22128ef JM |
868 | return 0; |
869 | ||
ed496f13 JM |
870 | p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)", |
871 | freq, (unsigned int) rx_time->sec, | |
c5f10e80 | 872 | (unsigned int) rx_time->usec); |
b22128ef | 873 | if (dev->flags & P2P_DEV_USER_REJECTED) { |
ed496f13 | 874 | p2p_dbg(p2p, "Do not report rejected device"); |
b22128ef JM |
875 | return 0; |
876 | } | |
8fd7dc1b | 877 | |
8b2b2a70 JM |
878 | if (dev->info.config_methods == 0 && |
879 | (freq == 2412 || freq == 2437 || freq == 2462)) { | |
880 | /* | |
881 | * If we have only seen a Beacon frame from a GO, we do not yet | |
882 | * know what WPS config methods it supports. Since some | |
883 | * applications use config_methods value from P2P-DEVICE-FOUND | |
884 | * events, postpone reporting this peer until we've fully | |
885 | * discovered its capabilities. | |
886 | * | |
887 | * At least for now, do this only if the peer was detected on | |
888 | * one of the social channels since that peer can be easily be | |
889 | * found again and there are no limitations of having to use | |
890 | * passive scan on this channels, so this can be done through | |
891 | * Probe Response frame that includes the config_methods | |
892 | * information. | |
893 | */ | |
ed496f13 JM |
894 | p2p_dbg(p2p, "Do not report peer " MACSTR |
895 | " with unknown config methods", MAC2STR(addr)); | |
8b2b2a70 JM |
896 | return 0; |
897 | } | |
898 | ||
8fd7dc1b JB |
899 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info, |
900 | !(dev->flags & P2P_DEV_REPORTED_ONCE)); | |
901 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; | |
b22128ef | 902 | |
095b3c40 BG |
903 | if (msg.adv_service_instance) |
904 | dev->flags |= P2P_DEV_P2PS_REPORTED; | |
905 | ||
b22128ef JM |
906 | return 0; |
907 | } | |
908 | ||
909 | ||
910 | static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev) | |
911 | { | |
6f2c0607 JMB |
912 | int i; |
913 | ||
5cfda25e JJ |
914 | if (p2p->go_neg_peer == dev) { |
915 | /* | |
916 | * If GO Negotiation is in progress, report that it has failed. | |
917 | */ | |
8c00fd00 | 918 | p2p_go_neg_failed(p2p, -1); |
5cfda25e | 919 | } |
b22128ef JM |
920 | if (p2p->invite_peer == dev) |
921 | p2p->invite_peer = NULL; | |
922 | if (p2p->sd_peer == dev) | |
923 | p2p->sd_peer = NULL; | |
924 | if (p2p->pending_client_disc_go == dev) | |
925 | p2p->pending_client_disc_go = NULL; | |
926 | ||
f5fc6032 AC |
927 | /* dev_lost() device, but only if it was previously dev_found() */ |
928 | if (dev->flags & P2P_DEV_REPORTED_ONCE) | |
929 | p2p->cfg->dev_lost(p2p->cfg->cb_ctx, | |
930 | dev->info.p2p_device_addr); | |
56eeb8f2 | 931 | |
10c5d2a5 | 932 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
6f2c0607 JMB |
933 | wpabuf_free(dev->info.wps_vendor_ext[i]); |
934 | dev->info.wps_vendor_ext[i] = NULL; | |
935 | } | |
936 | ||
9675ce35 | 937 | wpabuf_free(dev->info.wfd_subelems); |
71a0e395 | 938 | wpabuf_free(dev->info.vendor_elems); |
0e0e1e56 | 939 | wpabuf_free(dev->go_neg_conf); |
095b3c40 | 940 | wpabuf_free(dev->info.p2ps_instance); |
9675ce35 | 941 | |
b22128ef JM |
942 | os_free(dev); |
943 | } | |
944 | ||
945 | ||
946 | static int p2p_get_next_prog_freq(struct p2p_data *p2p) | |
947 | { | |
948 | struct p2p_channels *c; | |
949 | struct p2p_reg_class *cla; | |
950 | size_t cl, ch; | |
951 | int found = 0; | |
952 | u8 reg_class; | |
953 | u8 channel; | |
954 | int freq; | |
955 | ||
956 | c = &p2p->cfg->channels; | |
957 | for (cl = 0; cl < c->reg_classes; cl++) { | |
958 | cla = &c->reg_class[cl]; | |
959 | if (cla->reg_class != p2p->last_prog_scan_class) | |
960 | continue; | |
961 | for (ch = 0; ch < cla->channels; ch++) { | |
962 | if (cla->channel[ch] == p2p->last_prog_scan_chan) { | |
963 | found = 1; | |
964 | break; | |
965 | } | |
966 | } | |
967 | if (found) | |
968 | break; | |
969 | } | |
970 | ||
971 | if (!found) { | |
972 | /* Start from beginning */ | |
973 | reg_class = c->reg_class[0].reg_class; | |
974 | channel = c->reg_class[0].channel[0]; | |
975 | } else { | |
976 | /* Pick the next channel */ | |
977 | ch++; | |
978 | if (ch == cla->channels) { | |
979 | cl++; | |
980 | if (cl == c->reg_classes) | |
981 | cl = 0; | |
982 | ch = 0; | |
983 | } | |
984 | reg_class = c->reg_class[cl].reg_class; | |
985 | channel = c->reg_class[cl].channel[ch]; | |
986 | } | |
987 | ||
9ccd9165 | 988 | freq = p2p_channel_to_freq(reg_class, channel); |
ed496f13 | 989 | p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz", |
b22128ef JM |
990 | reg_class, channel, freq); |
991 | p2p->last_prog_scan_class = reg_class; | |
992 | p2p->last_prog_scan_chan = channel; | |
993 | ||
994 | if (freq == 2412 || freq == 2437 || freq == 2462) | |
995 | return 0; /* No need to add social channels */ | |
996 | return freq; | |
997 | } | |
998 | ||
999 | ||
1000 | static void p2p_search(struct p2p_data *p2p) | |
1001 | { | |
1002 | int freq = 0; | |
1003 | enum p2p_scan_type type; | |
360182ed | 1004 | u16 pw_id = DEV_PW_DEFAULT; |
99fcd404 | 1005 | int res; |
b22128ef JM |
1006 | |
1007 | if (p2p->drv_in_listen) { | |
ed496f13 | 1008 | p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing"); |
b22128ef JM |
1009 | return; |
1010 | } | |
1011 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); | |
1012 | ||
2c0ac6d6 JM |
1013 | if (p2p->find_pending_full && |
1014 | (p2p->find_type == P2P_FIND_PROGRESSIVE || | |
1015 | p2p->find_type == P2P_FIND_START_WITH_FULL)) { | |
1016 | type = P2P_SCAN_FULL; | |
1017 | p2p_dbg(p2p, "Starting search (pending full scan)"); | |
1018 | p2p->find_pending_full = 0; | |
1019 | } else if ((p2p->find_type == P2P_FIND_PROGRESSIVE && | |
d3bb082a JM |
1020 | (freq = p2p_get_next_prog_freq(p2p)) > 0) || |
1021 | (p2p->find_type == P2P_FIND_START_WITH_FULL && | |
1022 | (freq = p2p->find_specified_freq) > 0)) { | |
b22128ef | 1023 | type = P2P_SCAN_SOCIAL_PLUS_ONE; |
ed496f13 | 1024 | p2p_dbg(p2p, "Starting search (+ freq %u)", freq); |
b22128ef JM |
1025 | } else { |
1026 | type = P2P_SCAN_SOCIAL; | |
ed496f13 | 1027 | p2p_dbg(p2p, "Starting search"); |
b22128ef JM |
1028 | } |
1029 | ||
99fcd404 JM |
1030 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq, |
1031 | p2p->num_req_dev_types, p2p->req_dev_types, | |
1032 | p2p->find_dev_id, pw_id); | |
1033 | if (res < 0) { | |
b951a974 | 1034 | p2p_dbg(p2p, "Scan request schedule failed"); |
b22128ef JM |
1035 | p2p_continue_find(p2p); |
1036 | } | |
1037 | } | |
1038 | ||
1039 | ||
1040 | static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx) | |
1041 | { | |
1042 | struct p2p_data *p2p = eloop_ctx; | |
ed496f13 | 1043 | p2p_dbg(p2p, "Find timeout -> stop"); |
b22128ef JM |
1044 | p2p_stop_find(p2p); |
1045 | } | |
1046 | ||
1047 | ||
b951a974 SD |
1048 | void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status) |
1049 | { | |
1050 | if (status != 0) { | |
1051 | p2p_dbg(p2p, "Scan request failed"); | |
1052 | /* Do continue find even for the first p2p_find_scan */ | |
1053 | p2p_continue_find(p2p); | |
1054 | } else { | |
1055 | p2p_dbg(p2p, "Running p2p_scan"); | |
1056 | p2p->p2p_scan_running = 1; | |
1057 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
1058 | eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout, | |
1059 | p2p, NULL); | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | ||
b22128ef JM |
1064 | static int p2p_run_after_scan(struct p2p_data *p2p) |
1065 | { | |
1066 | struct p2p_device *dev; | |
1067 | enum p2p_after_scan op; | |
1068 | ||
1069 | op = p2p->start_after_scan; | |
1070 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; | |
1071 | switch (op) { | |
1072 | case P2P_AFTER_SCAN_NOTHING: | |
1073 | break; | |
1074 | case P2P_AFTER_SCAN_LISTEN: | |
ed496f13 | 1075 | p2p_dbg(p2p, "Start previously requested Listen state"); |
b22128ef JM |
1076 | p2p_listen(p2p, p2p->pending_listen_sec * 1000 + |
1077 | p2p->pending_listen_usec / 1000); | |
1078 | return 1; | |
1079 | case P2P_AFTER_SCAN_CONNECT: | |
ed496f13 | 1080 | p2p_dbg(p2p, "Start previously requested connect with " MACSTR, |
b22128ef JM |
1081 | MAC2STR(p2p->after_scan_peer)); |
1082 | dev = p2p_get_device(p2p, p2p->after_scan_peer); | |
1083 | if (dev == NULL) { | |
ed496f13 | 1084 | p2p_dbg(p2p, "Peer not known anymore"); |
b22128ef JM |
1085 | break; |
1086 | } | |
1087 | p2p_connect_send(p2p, dev); | |
1088 | return 1; | |
1089 | } | |
1090 | ||
1091 | return 0; | |
1092 | } | |
1093 | ||
1094 | ||
b22128ef JM |
1095 | static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx) |
1096 | { | |
1097 | struct p2p_data *p2p = eloop_ctx; | |
1098 | int running; | |
ed496f13 | 1099 | p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running); |
b22128ef JM |
1100 | running = p2p->p2p_scan_running; |
1101 | /* Make sure we recover from missed scan results callback */ | |
1102 | p2p->p2p_scan_running = 0; | |
1103 | ||
1104 | if (running) | |
1105 | p2p_run_after_scan(p2p); | |
1106 | } | |
1107 | ||
1108 | ||
046ef4aa JMB |
1109 | static void p2p_free_req_dev_types(struct p2p_data *p2p) |
1110 | { | |
1111 | p2p->num_req_dev_types = 0; | |
1112 | os_free(p2p->req_dev_types); | |
1113 | p2p->req_dev_types = NULL; | |
1114 | } | |
1115 | ||
1116 | ||
51775096 BG |
1117 | static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash) |
1118 | { | |
1119 | u8 buf[SHA256_MAC_LEN]; | |
1120 | char str_buf[256]; | |
1121 | const u8 *adv_array; | |
1122 | size_t i, adv_len; | |
1123 | ||
1124 | if (!str || !hash) | |
1125 | return 0; | |
1126 | ||
1127 | if (!str[0]) { | |
1128 | os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN); | |
1129 | return 1; | |
1130 | } | |
1131 | ||
1132 | adv_array = (u8 *) str_buf; | |
1133 | adv_len = os_strlen(str); | |
f33a31b0 JM |
1134 | if (adv_len >= sizeof(str_buf)) |
1135 | return 0; | |
51775096 | 1136 | |
65866555 | 1137 | for (i = 0; i < adv_len; i++) { |
51775096 BG |
1138 | if (str[i] >= 'A' && str[i] <= 'Z') |
1139 | str_buf[i] = str[i] - 'A' + 'a'; | |
1140 | else | |
1141 | str_buf[i] = str[i]; | |
1142 | } | |
1143 | ||
1144 | if (sha256_vector(1, &adv_array, &adv_len, buf)) | |
1145 | return 0; | |
1146 | ||
1147 | os_memcpy(hash, buf, P2PS_HASH_LEN); | |
1148 | return 1; | |
1149 | } | |
1150 | ||
1151 | ||
b22128ef | 1152 | int p2p_find(struct p2p_data *p2p, unsigned int timeout, |
046ef4aa | 1153 | enum p2p_discovery_type type, |
6d92fa6e | 1154 | unsigned int num_req_dev_types, const u8 *req_dev_types, |
51775096 | 1155 | const u8 *dev_id, unsigned int search_delay, |
fa9f381f | 1156 | u8 seek_count, const char **seek, int freq) |
b22128ef JM |
1157 | { |
1158 | int res; | |
f2a6ac63 | 1159 | struct os_reltime start; |
b22128ef | 1160 | |
ed496f13 | 1161 | p2p_dbg(p2p, "Starting find (type=%d)", type); |
b22128ef | 1162 | if (p2p->p2p_scan_running) { |
ed496f13 | 1163 | p2p_dbg(p2p, "p2p_scan is already running"); |
b22128ef | 1164 | } |
046ef4aa JMB |
1165 | |
1166 | p2p_free_req_dev_types(p2p); | |
1167 | if (req_dev_types && num_req_dev_types) { | |
a1f11e34 JB |
1168 | p2p->req_dev_types = os_memdup(req_dev_types, |
1169 | num_req_dev_types * | |
046ef4aa JMB |
1170 | WPS_DEV_TYPE_LEN); |
1171 | if (p2p->req_dev_types == NULL) | |
1172 | return -1; | |
046ef4aa JMB |
1173 | p2p->num_req_dev_types = num_req_dev_types; |
1174 | } | |
1175 | ||
6d92fa6e JM |
1176 | if (dev_id) { |
1177 | os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN); | |
1178 | p2p->find_dev_id = p2p->find_dev_id_buf; | |
1179 | } else | |
1180 | p2p->find_dev_id = NULL; | |
1181 | ||
51775096 BG |
1182 | if (seek_count == 0 || !seek) { |
1183 | /* Not an ASP search */ | |
1184 | p2p->p2ps_seek = 0; | |
1185 | } else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) { | |
1186 | /* | |
1187 | * An empty seek string means no hash values, but still an ASP | |
1188 | * search. | |
1189 | */ | |
24533f7e | 1190 | p2p_dbg(p2p, "ASP search"); |
51775096 BG |
1191 | p2p->p2ps_seek_count = 0; |
1192 | p2p->p2ps_seek = 1; | |
1193 | } else if (seek && seek_count <= P2P_MAX_QUERY_HASH) { | |
1194 | u8 buf[P2PS_HASH_LEN]; | |
e12c4004 | 1195 | int i, count = 0; |
51775096 | 1196 | |
51775096 BG |
1197 | for (i = 0; i < seek_count; i++) { |
1198 | if (!p2ps_gen_hash(p2p, seek[i], buf)) | |
1199 | continue; | |
1200 | ||
24533f7e JM |
1201 | p2p_dbg(p2p, "Seek service %s hash " MACSTR, |
1202 | seek[i], MAC2STR(buf)); | |
e12c4004 MS |
1203 | os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN], |
1204 | buf, P2PS_HASH_LEN); | |
1205 | count++; | |
51775096 | 1206 | } |
e12c4004 MS |
1207 | |
1208 | p2p->p2ps_seek_count = count; | |
51775096 BG |
1209 | p2p->p2ps_seek = 1; |
1210 | } else { | |
1211 | p2p->p2ps_seek_count = 0; | |
1212 | p2p->p2ps_seek = 1; | |
1213 | } | |
1214 | ||
1215 | /* Special case to perform wildcard search */ | |
1216 | if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) { | |
1217 | p2p->p2ps_seek_count = 1; | |
e12c4004 MS |
1218 | os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash, |
1219 | P2PS_HASH_LEN); | |
51775096 BG |
1220 | } |
1221 | ||
b22128ef JM |
1222 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; |
1223 | p2p_clear_timeout(p2p); | |
e79eb0c6 JM |
1224 | if (p2p->pending_listen_freq) { |
1225 | p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find"); | |
1226 | p2p->pending_listen_freq = 0; | |
1227 | } | |
b22128ef | 1228 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
2c0ac6d6 | 1229 | p2p->find_pending_full = 0; |
b22128ef | 1230 | p2p->find_type = type; |
d3bb082a JM |
1231 | if (freq != 2412 && freq != 2437 && freq != 2462 && freq != 60480) |
1232 | p2p->find_specified_freq = freq; | |
1233 | else | |
1234 | p2p->find_specified_freq = 0; | |
b22128ef | 1235 | p2p_device_clear_reported(p2p); |
cba9ebfd | 1236 | os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN); |
b22128ef | 1237 | p2p_set_state(p2p, P2P_SEARCH); |
37448ede JM |
1238 | p2p->search_delay = search_delay; |
1239 | p2p->in_search_delay = 0; | |
b22128ef | 1240 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); |
39185dfa | 1241 | p2p->last_p2p_find_timeout = timeout; |
b22128ef JM |
1242 | if (timeout) |
1243 | eloop_register_timeout(timeout, 0, p2p_find_timeout, | |
1244 | p2p, NULL); | |
f2a6ac63 | 1245 | os_get_reltime(&start); |
b22128ef JM |
1246 | switch (type) { |
1247 | case P2P_FIND_START_WITH_FULL: | |
fa9f381f DN |
1248 | if (freq > 0) { |
1249 | /* | |
1250 | * Start with the specified channel and then move to | |
d3bb082a | 1251 | * scans for social channels and this specific channel. |
fa9f381f DN |
1252 | */ |
1253 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, | |
1254 | P2P_SCAN_SPECIFIC, freq, | |
1255 | p2p->num_req_dev_types, | |
1256 | p2p->req_dev_types, dev_id, | |
1257 | DEV_PW_DEFAULT); | |
1258 | break; | |
1259 | } | |
1260 | /* fall through */ | |
b22128ef | 1261 | case P2P_FIND_PROGRESSIVE: |
046ef4aa JMB |
1262 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0, |
1263 | p2p->num_req_dev_types, | |
360182ed JM |
1264 | p2p->req_dev_types, dev_id, |
1265 | DEV_PW_DEFAULT); | |
b22128ef JM |
1266 | break; |
1267 | case P2P_FIND_ONLY_SOCIAL: | |
046ef4aa JMB |
1268 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0, |
1269 | p2p->num_req_dev_types, | |
360182ed JM |
1270 | p2p->req_dev_types, dev_id, |
1271 | DEV_PW_DEFAULT); | |
b22128ef JM |
1272 | break; |
1273 | default: | |
1274 | return -1; | |
1275 | } | |
1276 | ||
f2a6ac63 PK |
1277 | if (!res) |
1278 | p2p->find_start = start; | |
1279 | ||
b951a974 | 1280 | if (res != 0 && p2p->p2p_scan_running) { |
1b5d4714 JM |
1281 | p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running"); |
1282 | /* wait for the previous p2p_scan to complete */ | |
2c0ac6d6 JM |
1283 | if (type == P2P_FIND_PROGRESSIVE || |
1284 | (type == P2P_FIND_START_WITH_FULL && freq == 0)) | |
1285 | p2p->find_pending_full = 1; | |
825fb6b2 | 1286 | res = 0; /* do not report failure */ |
b951a974 | 1287 | } else if (res != 0) { |
ed496f13 | 1288 | p2p_dbg(p2p, "Failed to start p2p_scan"); |
0c96fd6d JM |
1289 | p2p_set_state(p2p, P2P_IDLE); |
1290 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); | |
b22128ef JM |
1291 | } |
1292 | ||
1293 | return res; | |
1294 | } | |
1295 | ||
1296 | ||
0b8889d8 | 1297 | void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq) |
b22128ef | 1298 | { |
ed496f13 | 1299 | p2p_dbg(p2p, "Stopping find"); |
b22128ef JM |
1300 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); |
1301 | p2p_clear_timeout(p2p); | |
0cf12b32 | 1302 | if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND) |
710ae9ac | 1303 | p2p->cfg->find_stopped(p2p->cfg->cb_ctx); |
51775096 BG |
1304 | |
1305 | p2p->p2ps_seek_count = 0; | |
1306 | ||
b22128ef | 1307 | p2p_set_state(p2p, P2P_IDLE); |
046ef4aa | 1308 | p2p_free_req_dev_types(p2p); |
b22128ef | 1309 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; |
3a2a7c3d | 1310 | if (p2p->go_neg_peer) |
fb8984fd | 1311 | p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE; |
b22128ef JM |
1312 | p2p->go_neg_peer = NULL; |
1313 | p2p->sd_peer = NULL; | |
1314 | p2p->invite_peer = NULL; | |
1a9c618d | 1315 | p2p_stop_listen_for_freq(p2p, freq); |
0d2f324d | 1316 | p2p->send_action_in_progress = 0; |
1a9c618d JM |
1317 | } |
1318 | ||
1319 | ||
1320 | void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq) | |
1321 | { | |
0b8889d8 | 1322 | if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) { |
ed496f13 | 1323 | p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response"); |
0b8889d8 JM |
1324 | return; |
1325 | } | |
1a9c618d JM |
1326 | if (p2p->in_listen) { |
1327 | p2p->in_listen = 0; | |
1328 | p2p_clear_timeout(p2p); | |
1329 | } | |
54b8f994 JM |
1330 | if (p2p->drv_in_listen) { |
1331 | /* | |
1332 | * The driver may not deliver callback to p2p_listen_end() | |
1333 | * when the operation gets canceled, so clear the internal | |
1334 | * variable that is tracking driver state. | |
1335 | */ | |
ed496f13 | 1336 | p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen); |
54b8f994 JM |
1337 | p2p->drv_in_listen = 0; |
1338 | } | |
b22128ef JM |
1339 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
1340 | } | |
1341 | ||
1342 | ||
0b5fb86a SADR |
1343 | void p2p_stop_listen(struct p2p_data *p2p) |
1344 | { | |
1345 | if (p2p->state != P2P_LISTEN_ONLY) { | |
1346 | p2p_dbg(p2p, "Skip stop_listen since not in listen_only state."); | |
1347 | return; | |
1348 | } | |
1349 | ||
1350 | p2p_stop_listen_for_freq(p2p, 0); | |
1351 | p2p_set_state(p2p, P2P_IDLE); | |
1352 | } | |
1353 | ||
1354 | ||
0b8889d8 JM |
1355 | void p2p_stop_find(struct p2p_data *p2p) |
1356 | { | |
5661bd0f | 1357 | p2p->pending_listen_freq = 0; |
0b8889d8 JM |
1358 | p2p_stop_find_for_freq(p2p, 0); |
1359 | } | |
1360 | ||
1361 | ||
e28c226d JM |
1362 | static int p2p_prepare_channel_pref(struct p2p_data *p2p, |
1363 | unsigned int force_freq, | |
51e9f228 | 1364 | unsigned int pref_freq, int go) |
e28c226d JM |
1365 | { |
1366 | u8 op_class, op_channel; | |
1367 | unsigned int freq = force_freq ? force_freq : pref_freq; | |
1368 | ||
51e9f228 JM |
1369 | p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d", |
1370 | force_freq, pref_freq, go); | |
9ccd9165 | 1371 | if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) { |
ed496f13 | 1372 | p2p_dbg(p2p, "Unsupported frequency %u MHz", freq); |
e28c226d JM |
1373 | return -1; |
1374 | } | |
1375 | ||
51e9f228 JM |
1376 | if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) && |
1377 | (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class, | |
1378 | op_channel))) { | |
ed496f13 JM |
1379 | p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P", |
1380 | freq, op_class, op_channel); | |
e28c226d JM |
1381 | return -1; |
1382 | } | |
1383 | ||
1384 | p2p->op_reg_class = op_class; | |
1385 | p2p->op_channel = op_channel; | |
1386 | ||
1387 | if (force_freq) { | |
1388 | p2p->channels.reg_classes = 1; | |
1389 | p2p->channels.reg_class[0].channels = 1; | |
1390 | p2p->channels.reg_class[0].reg_class = p2p->op_reg_class; | |
1391 | p2p->channels.reg_class[0].channel[0] = p2p->op_channel; | |
1392 | } else { | |
1393 | os_memcpy(&p2p->channels, &p2p->cfg->channels, | |
1394 | sizeof(struct p2p_channels)); | |
1395 | } | |
1396 | ||
1397 | return 0; | |
1398 | } | |
1399 | ||
1400 | ||
1401 | static void p2p_prepare_channel_best(struct p2p_data *p2p) | |
1402 | { | |
1403 | u8 op_class, op_channel; | |
0c9fb14e | 1404 | const int op_classes_5ghz[] = { 124, 125, 115, 0 }; |
00043740 | 1405 | const int op_classes_ht40[] = { 126, 127, 116, 117, 0 }; |
384bdd02 | 1406 | const int op_classes_vht[] = { 128, 0 }; |
e28c226d | 1407 | |
99d7c762 JM |
1408 | p2p_dbg(p2p, "Prepare channel best"); |
1409 | ||
e28c226d JM |
1410 | if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 && |
1411 | p2p_supported_freq(p2p, p2p->best_freq_overall) && | |
9ccd9165 JM |
1412 | p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel) |
1413 | == 0) { | |
ed496f13 | 1414 | p2p_dbg(p2p, "Select best overall channel as operating channel preference"); |
e28c226d JM |
1415 | p2p->op_reg_class = op_class; |
1416 | p2p->op_channel = op_channel; | |
1417 | } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 && | |
1418 | p2p_supported_freq(p2p, p2p->best_freq_5) && | |
9ccd9165 JM |
1419 | p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel) |
1420 | == 0) { | |
ed496f13 | 1421 | p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference"); |
e28c226d JM |
1422 | p2p->op_reg_class = op_class; |
1423 | p2p->op_channel = op_channel; | |
1424 | } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 && | |
1425 | p2p_supported_freq(p2p, p2p->best_freq_24) && | |
9ccd9165 JM |
1426 | p2p_freq_to_channel(p2p->best_freq_24, &op_class, |
1427 | &op_channel) == 0) { | |
ed496f13 | 1428 | p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference"); |
e28c226d JM |
1429 | p2p->op_reg_class = op_class; |
1430 | p2p->op_channel = op_channel; | |
6701fdc3 JM |
1431 | } else if (p2p->cfg->num_pref_chan > 0 && |
1432 | p2p_channels_includes(&p2p->cfg->channels, | |
1433 | p2p->cfg->pref_chan[0].op_class, | |
1434 | p2p->cfg->pref_chan[0].chan)) { | |
1435 | p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference"); | |
1436 | p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class; | |
1437 | p2p->op_channel = p2p->cfg->pref_chan[0].chan; | |
384bdd02 JM |
1438 | } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht, |
1439 | &p2p->op_reg_class, &p2p->op_channel) == | |
1440 | 0) { | |
1441 | p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference", | |
1442 | p2p->op_reg_class, p2p->op_channel); | |
a30d50b3 JM |
1443 | } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40, |
1444 | &p2p->op_reg_class, &p2p->op_channel) == | |
1445 | 0) { | |
1446 | p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference", | |
1447 | p2p->op_reg_class, p2p->op_channel); | |
f2407107 JM |
1448 | } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz, |
1449 | &p2p->op_reg_class, &p2p->op_channel) == | |
1450 | 0) { | |
1451 | p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference", | |
1452 | p2p->op_reg_class, p2p->op_channel); | |
94b84bc7 AH |
1453 | } else if (p2p_channels_includes(&p2p->cfg->channels, |
1454 | p2p->cfg->op_reg_class, | |
1455 | p2p->cfg->op_channel)) { | |
99d7c762 | 1456 | p2p_dbg(p2p, "Select pre-configured channel as operating channel preference"); |
e28c226d JM |
1457 | p2p->op_reg_class = p2p->cfg->op_reg_class; |
1458 | p2p->op_channel = p2p->cfg->op_channel; | |
6ace13a9 JM |
1459 | } else if (p2p_channel_random_social(&p2p->cfg->channels, |
1460 | &p2p->op_reg_class, | |
59fa2053 PK |
1461 | &p2p->op_channel, |
1462 | NULL, NULL) == 0) { | |
1595eb93 AM |
1463 | p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference", |
1464 | p2p->op_reg_class, p2p->op_channel); | |
94b84bc7 | 1465 | } else { |
6ace13a9 JM |
1466 | /* Select any random available channel from the first available |
1467 | * operating class */ | |
1468 | p2p_channel_select(&p2p->cfg->channels, NULL, | |
1469 | &p2p->op_reg_class, | |
1470 | &p2p->op_channel); | |
1471 | p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference", | |
1472 | p2p->op_channel, p2p->op_reg_class); | |
e28c226d JM |
1473 | } |
1474 | ||
1475 | os_memcpy(&p2p->channels, &p2p->cfg->channels, | |
1476 | sizeof(struct p2p_channels)); | |
1477 | } | |
1478 | ||
1479 | ||
a5830ede | 1480 | /** |
ebd32943 | 1481 | * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD |
a5830ede JM |
1482 | * @p2p: P2P module context from p2p_init() |
1483 | * @dev: Selected peer device | |
1484 | * @force_freq: Forced frequency in MHz or 0 if not forced | |
1485 | * @pref_freq: Preferred frequency in MHz or 0 if no preference | |
51e9f228 | 1486 | * @go: Whether the local end will be forced to be GO |
a5830ede JM |
1487 | * Returns: 0 on success, -1 on failure (channel not supported for P2P) |
1488 | * | |
1489 | * This function is used to do initial operating channel selection for GO | |
ebd32943 IP |
1490 | * Negotiation prior to having received peer information or for P2PS PD |
1491 | * signalling. The selected channel may be further optimized in | |
1492 | * p2p_reselect_channel() once the peer information is available. | |
a5830ede | 1493 | */ |
79879f4a | 1494 | int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev, |
51e9f228 | 1495 | unsigned int force_freq, unsigned int pref_freq, int go) |
b22128ef | 1496 | { |
51e9f228 JM |
1497 | p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d", |
1498 | force_freq, pref_freq, go); | |
04a3e69d | 1499 | if (force_freq || pref_freq) { |
51e9f228 JM |
1500 | if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) < |
1501 | 0) | |
1e19f734 | 1502 | return -1; |
b22128ef | 1503 | } else { |
e28c226d | 1504 | p2p_prepare_channel_best(p2p); |
b22128ef | 1505 | } |
51e9f228 JM |
1506 | p2p_channels_dump(p2p, "prepared channels", &p2p->channels); |
1507 | if (go) | |
1508 | p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq); | |
1509 | else if (!force_freq) | |
a6306bcc JM |
1510 | p2p_channels_union_inplace(&p2p->channels, |
1511 | &p2p->cfg->cli_channels); | |
51e9f228 JM |
1512 | p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels); |
1513 | ||
ed496f13 | 1514 | p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s", |
b22128ef JM |
1515 | p2p->op_reg_class, p2p->op_channel, |
1516 | force_freq ? " (forced)" : ""); | |
1517 | ||
92ac756c JM |
1518 | if (force_freq) |
1519 | dev->flags |= P2P_DEV_FORCE_FREQ; | |
1520 | else | |
1521 | dev->flags &= ~P2P_DEV_FORCE_FREQ; | |
1522 | ||
7861cb08 JM |
1523 | return 0; |
1524 | } | |
1525 | ||
1526 | ||
acc247b2 JM |
1527 | static void p2p_set_dev_persistent(struct p2p_device *dev, |
1528 | int persistent_group) | |
1529 | { | |
1530 | switch (persistent_group) { | |
1531 | case 0: | |
1532 | dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP | | |
1533 | P2P_DEV_PREFER_PERSISTENT_RECONN); | |
1534 | break; | |
1535 | case 1: | |
1536 | dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP; | |
1537 | dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN; | |
1538 | break; | |
1539 | case 2: | |
1540 | dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP | | |
1541 | P2P_DEV_PREFER_PERSISTENT_RECONN; | |
1542 | break; | |
1543 | } | |
1544 | } | |
1545 | ||
1546 | ||
7861cb08 JM |
1547 | int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr, |
1548 | enum p2p_wps_method wps_method, | |
1549 | int go_intent, const u8 *own_interface_addr, | |
23c84252 | 1550 | unsigned int force_freq, int persistent_group, |
3bc462cb | 1551 | const u8 *force_ssid, size_t force_ssid_len, |
ab9e3442 | 1552 | int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id) |
7861cb08 JM |
1553 | { |
1554 | struct p2p_device *dev; | |
1555 | ||
ed496f13 | 1556 | p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR |
7861cb08 | 1557 | " GO Intent=%d Intended Interface Address=" MACSTR |
ab9e3442 JM |
1558 | " wps_method=%d persistent_group=%d pd_before_go_neg=%d " |
1559 | "oob_pw_id=%u", | |
7861cb08 | 1560 | MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), |
ab9e3442 | 1561 | wps_method, persistent_group, pd_before_go_neg, oob_pw_id); |
7861cb08 | 1562 | |
b22128ef JM |
1563 | dev = p2p_get_device(p2p, peer_addr); |
1564 | if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) { | |
ed496f13 | 1565 | p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR, |
b22128ef JM |
1566 | MAC2STR(peer_addr)); |
1567 | return -1; | |
1568 | } | |
1569 | ||
51e9f228 JM |
1570 | if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, |
1571 | go_intent == 15) < 0) | |
92ac756c JM |
1572 | return -1; |
1573 | ||
b22128ef | 1574 | if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) { |
c5db8e51 KRK |
1575 | if (!(dev->info.dev_capab & |
1576 | P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) { | |
ed496f13 | 1577 | p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR |
b22128ef JM |
1578 | " that is in a group and is not discoverable", |
1579 | MAC2STR(peer_addr)); | |
1580 | return -1; | |
1581 | } | |
1582 | if (dev->oper_freq <= 0) { | |
ed496f13 | 1583 | p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR |
b22128ef JM |
1584 | " with incomplete information", |
1585 | MAC2STR(peer_addr)); | |
1586 | return -1; | |
1587 | } | |
1588 | ||
1589 | /* | |
1590 | * First, try to connect directly. If the peer does not | |
1591 | * acknowledge frames, assume it is sleeping and use device | |
1592 | * discoverability via the GO at that point. | |
1593 | */ | |
1594 | } | |
1595 | ||
23c84252 JM |
1596 | p2p->ssid_set = 0; |
1597 | if (force_ssid) { | |
1598 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID", | |
1599 | force_ssid, force_ssid_len); | |
1600 | os_memcpy(p2p->ssid, force_ssid, force_ssid_len); | |
1601 | p2p->ssid_len = force_ssid_len; | |
1602 | p2p->ssid_set = 1; | |
1603 | } | |
1604 | ||
b22128ef JM |
1605 | dev->flags &= ~P2P_DEV_NOT_YET_READY; |
1606 | dev->flags &= ~P2P_DEV_USER_REJECTED; | |
1607 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; | |
1608 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; | |
3bc462cb JM |
1609 | if (pd_before_go_neg) |
1610 | dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG; | |
624b4d5a | 1611 | else { |
3bc462cb | 1612 | dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG; |
624b4d5a | 1613 | /* |
003c4580 JM |
1614 | * Assign dialog token and tie breaker here to use the same |
1615 | * values in each retry within the same GO Negotiation exchange. | |
624b4d5a SD |
1616 | */ |
1617 | dev->dialog_token++; | |
1618 | if (dev->dialog_token == 0) | |
1619 | dev->dialog_token = 1; | |
003c4580 JM |
1620 | dev->tie_breaker = p2p->next_tie_breaker; |
1621 | p2p->next_tie_breaker = !p2p->next_tie_breaker; | |
624b4d5a | 1622 | } |
9dac8c3e | 1623 | dev->connect_reqs = 0; |
b22128ef JM |
1624 | dev->go_neg_req_sent = 0; |
1625 | dev->go_state = UNKNOWN_GO; | |
acc247b2 | 1626 | p2p_set_dev_persistent(dev, persistent_group); |
b22128ef JM |
1627 | p2p->go_intent = go_intent; |
1628 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); | |
1629 | ||
1630 | if (p2p->state != P2P_IDLE) | |
1631 | p2p_stop_find(p2p); | |
1632 | ||
1633 | dev->wps_method = wps_method; | |
ab9e3442 | 1634 | dev->oob_pw_id = oob_pw_id; |
b22128ef | 1635 | dev->status = P2P_SC_SUCCESS; |
d5b20a73 | 1636 | |
b22128ef | 1637 | if (p2p->p2p_scan_running) { |
ed496f13 | 1638 | p2p_dbg(p2p, "p2p_scan running - delay connect send"); |
b22128ef JM |
1639 | p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT; |
1640 | os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN); | |
1641 | return 0; | |
1642 | } | |
b22128ef JM |
1643 | |
1644 | return p2p_connect_send(p2p, dev); | |
1645 | } | |
1646 | ||
1647 | ||
1648 | int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr, | |
1649 | enum p2p_wps_method wps_method, | |
1650 | int go_intent, const u8 *own_interface_addr, | |
23c84252 | 1651 | unsigned int force_freq, int persistent_group, |
04a3e69d | 1652 | const u8 *force_ssid, size_t force_ssid_len, |
ab9e3442 | 1653 | unsigned int pref_freq, u16 oob_pw_id) |
b22128ef JM |
1654 | { |
1655 | struct p2p_device *dev; | |
1656 | ||
ed496f13 | 1657 | p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR |
b22128ef | 1658 | " GO Intent=%d Intended Interface Address=" MACSTR |
ab9e3442 | 1659 | " wps_method=%d persistent_group=%d oob_pw_id=%u", |
b22128ef | 1660 | MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), |
ab9e3442 | 1661 | wps_method, persistent_group, oob_pw_id); |
b22128ef | 1662 | |
b22128ef JM |
1663 | dev = p2p_get_device(p2p, peer_addr); |
1664 | if (dev == NULL) { | |
ed496f13 | 1665 | p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR, |
b22128ef JM |
1666 | MAC2STR(peer_addr)); |
1667 | return -1; | |
1668 | } | |
1669 | ||
51e9f228 JM |
1670 | if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent == |
1671 | 15) < 0) | |
92ac756c JM |
1672 | return -1; |
1673 | ||
23c84252 JM |
1674 | p2p->ssid_set = 0; |
1675 | if (force_ssid) { | |
1676 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID", | |
1677 | force_ssid, force_ssid_len); | |
1678 | os_memcpy(p2p->ssid, force_ssid, force_ssid_len); | |
1679 | p2p->ssid_len = force_ssid_len; | |
1680 | p2p->ssid_set = 1; | |
1681 | } | |
1682 | ||
b22128ef JM |
1683 | dev->flags &= ~P2P_DEV_NOT_YET_READY; |
1684 | dev->flags &= ~P2P_DEV_USER_REJECTED; | |
1685 | dev->go_neg_req_sent = 0; | |
1686 | dev->go_state = UNKNOWN_GO; | |
acc247b2 | 1687 | p2p_set_dev_persistent(dev, persistent_group); |
b22128ef JM |
1688 | p2p->go_intent = go_intent; |
1689 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); | |
1690 | ||
1691 | dev->wps_method = wps_method; | |
ab9e3442 | 1692 | dev->oob_pw_id = oob_pw_id; |
b22128ef JM |
1693 | dev->status = P2P_SC_SUCCESS; |
1694 | ||
1695 | return 0; | |
1696 | } | |
1697 | ||
1698 | ||
1699 | void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr, | |
1700 | struct p2p_device *dev, struct p2p_message *msg) | |
1701 | { | |
acb69cec | 1702 | os_get_reltime(&dev->last_seen); |
b22128ef | 1703 | |
ed496f13 | 1704 | p2p_copy_wps_info(p2p, dev, 0, msg); |
e57ae6e1 | 1705 | |
b22128ef JM |
1706 | if (msg->listen_channel) { |
1707 | int freq; | |
9ccd9165 | 1708 | freq = p2p_channel_to_freq(msg->listen_channel[3], |
b22128ef JM |
1709 | msg->listen_channel[4]); |
1710 | if (freq < 0) { | |
ed496f13 | 1711 | p2p_dbg(p2p, "Unknown peer Listen channel: " |
b22128ef JM |
1712 | "country=%c%c(0x%02x) reg_class=%u channel=%u", |
1713 | msg->listen_channel[0], | |
1714 | msg->listen_channel[1], | |
1715 | msg->listen_channel[2], | |
1716 | msg->listen_channel[3], | |
1717 | msg->listen_channel[4]); | |
1718 | } else { | |
ed496f13 JM |
1719 | p2p_dbg(p2p, "Update peer " MACSTR |
1720 | " Listen channel: %u -> %u MHz", | |
c5db8e51 | 1721 | MAC2STR(dev->info.p2p_device_addr), |
b22128ef JM |
1722 | dev->listen_freq, freq); |
1723 | dev->listen_freq = freq; | |
1724 | } | |
1725 | } | |
b22128ef | 1726 | |
9675ce35 JM |
1727 | if (msg->wfd_subelems) { |
1728 | wpabuf_free(dev->info.wfd_subelems); | |
1729 | dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems); | |
1730 | } | |
1731 | ||
b22128ef JM |
1732 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { |
1733 | dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; | |
ed496f13 | 1734 | p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request"); |
b22128ef | 1735 | } else { |
ed496f13 | 1736 | p2p_dbg(p2p, "Created device entry based on GO Neg Req: " |
b22128ef JM |
1737 | MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' " |
1738 | "listen_freq=%d", | |
c5db8e51 KRK |
1739 | MAC2STR(dev->info.p2p_device_addr), |
1740 | dev->info.dev_capab, dev->info.group_capab, | |
1741 | dev->info.device_name, dev->listen_freq); | |
b22128ef JM |
1742 | } |
1743 | ||
1744 | dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY; | |
1745 | ||
1746 | if (dev->flags & P2P_DEV_USER_REJECTED) { | |
ed496f13 | 1747 | p2p_dbg(p2p, "Do not report rejected device"); |
b22128ef JM |
1748 | return; |
1749 | } | |
1750 | ||
8fd7dc1b JB |
1751 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info, |
1752 | !(dev->flags & P2P_DEV_REPORTED_ONCE)); | |
1753 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; | |
b22128ef JM |
1754 | } |
1755 | ||
1756 | ||
1757 | void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len) | |
1758 | { | |
1759 | os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); | |
1760 | p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2); | |
1761 | os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2], | |
1762 | p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len); | |
1763 | *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len; | |
1764 | } | |
1765 | ||
1766 | ||
1767 | int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params) | |
1768 | { | |
59fec34d KV |
1769 | if (p2p->ssid_set) { |
1770 | os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len); | |
1771 | params->ssid_len = p2p->ssid_len; | |
1772 | } else { | |
1773 | p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len); | |
1774 | } | |
1775 | p2p->ssid_set = 0; | |
1776 | ||
1b928f96 | 1777 | p2p_random(params->passphrase, p2p->cfg->passphrase_len); |
b22128ef JM |
1778 | return 0; |
1779 | } | |
1780 | ||
1781 | ||
1782 | void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer) | |
1783 | { | |
1784 | struct p2p_go_neg_results res; | |
1785 | int go = peer->go_state == LOCAL_GO; | |
1786 | struct p2p_channels intersection; | |
b22128ef | 1787 | |
ed496f13 JM |
1788 | p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)", |
1789 | MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer"); | |
b22128ef JM |
1790 | |
1791 | os_memset(&res, 0, sizeof(res)); | |
1792 | res.role_go = go; | |
c5db8e51 | 1793 | os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN); |
b22128ef JM |
1794 | os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN); |
1795 | res.wps_method = peer->wps_method; | |
acc247b2 JM |
1796 | if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) { |
1797 | if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN) | |
1798 | res.persistent_group = 2; | |
1799 | else | |
1800 | res.persistent_group = 1; | |
1801 | } | |
b22128ef JM |
1802 | |
1803 | if (go) { | |
1804 | /* Setup AP mode for WPS provisioning */ | |
9ccd9165 | 1805 | res.freq = p2p_channel_to_freq(p2p->op_reg_class, |
b22128ef JM |
1806 | p2p->op_channel); |
1807 | os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); | |
1808 | res.ssid_len = p2p->ssid_len; | |
1b928f96 | 1809 | p2p_random(res.passphrase, p2p->cfg->passphrase_len); |
e9a7ae41 | 1810 | } else { |
b22128ef | 1811 | res.freq = peer->oper_freq; |
e9a7ae41 JM |
1812 | if (p2p->ssid_len) { |
1813 | os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); | |
1814 | res.ssid_len = p2p->ssid_len; | |
1815 | } | |
1816 | } | |
b22128ef | 1817 | |
556b30da JM |
1818 | p2p_channels_dump(p2p, "own channels", &p2p->channels); |
1819 | p2p_channels_dump(p2p, "peer channels", &peer->channels); | |
b22128ef JM |
1820 | p2p_channels_intersect(&p2p->channels, &peer->channels, |
1821 | &intersection); | |
556b30da JM |
1822 | if (go) { |
1823 | p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq); | |
1824 | p2p_channels_dump(p2p, "intersection after no-GO removal", | |
1825 | &intersection); | |
1826 | } | |
1170239e IP |
1827 | |
1828 | p2p_channels_to_freqs(&intersection, res.freq_list, | |
1829 | P2P_MAX_CHANNELS); | |
b22128ef | 1830 | |
ae3e3421 JM |
1831 | res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout; |
1832 | ||
b22128ef | 1833 | p2p_clear_timeout(p2p); |
4458d915 | 1834 | p2p->ssid_set = 0; |
b22128ef | 1835 | peer->go_neg_req_sent = 0; |
49aa88bb | 1836 | peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE; |
b22128ef | 1837 | peer->wps_method = WPS_NOT_READY; |
ab9e3442 | 1838 | peer->oob_pw_id = 0; |
0e0e1e56 NS |
1839 | wpabuf_free(peer->go_neg_conf); |
1840 | peer->go_neg_conf = NULL; | |
b22128ef JM |
1841 | |
1842 | p2p_set_state(p2p, P2P_PROVISIONING); | |
1843 | p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); | |
1844 | } | |
1845 | ||
1846 | ||
1847 | static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa, | |
1848 | const u8 *data, size_t len, int rx_freq) | |
1849 | { | |
ed496f13 | 1850 | p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa)); |
b22128ef JM |
1851 | wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len); |
1852 | ||
1853 | if (len < 1) | |
1854 | return; | |
1855 | ||
1856 | switch (data[0]) { | |
1857 | case P2P_GO_NEG_REQ: | |
1858 | p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq); | |
1859 | break; | |
1860 | case P2P_GO_NEG_RESP: | |
1861 | p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq); | |
1862 | break; | |
1863 | case P2P_GO_NEG_CONF: | |
1864 | p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1); | |
1865 | break; | |
1866 | case P2P_INVITATION_REQ: | |
1867 | p2p_process_invitation_req(p2p, sa, data + 1, len - 1, | |
1868 | rx_freq); | |
1869 | break; | |
1870 | case P2P_INVITATION_RESP: | |
1871 | p2p_process_invitation_resp(p2p, sa, data + 1, len - 1); | |
1872 | break; | |
1873 | case P2P_PROV_DISC_REQ: | |
1874 | p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq); | |
1875 | break; | |
1876 | case P2P_PROV_DISC_RESP: | |
1877 | p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1); | |
1878 | break; | |
1879 | case P2P_DEV_DISC_REQ: | |
1880 | p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq); | |
1881 | break; | |
1882 | case P2P_DEV_DISC_RESP: | |
1883 | p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1); | |
1884 | break; | |
1885 | default: | |
ed496f13 | 1886 | p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d", |
b22128ef JM |
1887 | data[0]); |
1888 | break; | |
1889 | } | |
1890 | } | |
1891 | ||
1892 | ||
19df9b07 JM |
1893 | static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, |
1894 | const u8 *sa, const u8 *bssid, const u8 *data, | |
1895 | size_t len, int freq) | |
b22128ef JM |
1896 | { |
1897 | if (len < 1) | |
1898 | return; | |
1899 | ||
1900 | switch (data[0]) { | |
1901 | case WLAN_PA_VENDOR_SPECIFIC: | |
1902 | data++; | |
1903 | len--; | |
8bc4372f | 1904 | if (len < 4) |
b22128ef | 1905 | return; |
8bc4372f | 1906 | if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE) |
b22128ef JM |
1907 | return; |
1908 | ||
8bc4372f RJ |
1909 | data += 4; |
1910 | len -= 4; | |
b22128ef | 1911 | |
8bc4372f | 1912 | p2p_rx_p2p_action(p2p, sa, data, len, freq); |
b22128ef JM |
1913 | break; |
1914 | case WLAN_PA_GAS_INITIAL_REQ: | |
1915 | p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq); | |
1916 | break; | |
1917 | case WLAN_PA_GAS_INITIAL_RESP: | |
18708aad JM |
1918 | p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq); |
1919 | break; | |
1920 | case WLAN_PA_GAS_COMEBACK_REQ: | |
1921 | p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq); | |
1922 | break; | |
1923 | case WLAN_PA_GAS_COMEBACK_RESP: | |
1924 | p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq); | |
b22128ef JM |
1925 | break; |
1926 | } | |
1927 | } | |
1928 | ||
1929 | ||
1930 | void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa, | |
1931 | const u8 *bssid, u8 category, | |
1932 | const u8 *data, size_t len, int freq) | |
1933 | { | |
1934 | if (category == WLAN_ACTION_PUBLIC) { | |
1935 | p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq); | |
1936 | return; | |
1937 | } | |
1938 | ||
1939 | if (category != WLAN_ACTION_VENDOR_SPECIFIC) | |
1940 | return; | |
1941 | ||
1942 | if (len < 4) | |
1943 | return; | |
1944 | ||
8bc4372f | 1945 | if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE) |
b22128ef | 1946 | return; |
8bc4372f RJ |
1947 | data += 4; |
1948 | len -= 4; | |
b22128ef JM |
1949 | |
1950 | /* P2P action frame */ | |
ed496f13 | 1951 | p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa)); |
b22128ef JM |
1952 | wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len); |
1953 | ||
1954 | if (len < 1) | |
1955 | return; | |
1956 | switch (data[0]) { | |
1957 | case P2P_NOA: | |
ed496f13 | 1958 | p2p_dbg(p2p, "Received P2P Action - Notice of Absence"); |
b22128ef JM |
1959 | /* TODO */ |
1960 | break; | |
1961 | case P2P_PRESENCE_REQ: | |
1962 | p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq); | |
1963 | break; | |
1964 | case P2P_PRESENCE_RESP: | |
1965 | p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1); | |
1966 | break; | |
1967 | case P2P_GO_DISC_REQ: | |
1968 | p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq); | |
1969 | break; | |
1970 | default: | |
ed496f13 | 1971 | p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]); |
b22128ef JM |
1972 | break; |
1973 | } | |
1974 | } | |
1975 | ||
1976 | ||
1977 | static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx) | |
1978 | { | |
1979 | struct p2p_data *p2p = eloop_ctx; | |
1980 | if (p2p->go_neg_peer == NULL) | |
1981 | return; | |
7549c178 JM |
1982 | if (p2p->pending_listen_freq) { |
1983 | p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start"); | |
1984 | p2p->pending_listen_freq = 0; | |
1985 | } | |
b22128ef JM |
1986 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
1987 | p2p->go_neg_peer->status = P2P_SC_SUCCESS; | |
e49cabcf JM |
1988 | /* |
1989 | * Set new timeout to make sure a previously set one does not expire | |
1990 | * too quickly while waiting for the GO Negotiation to complete. | |
1991 | */ | |
1992 | p2p_set_timeout(p2p, 0, 500000); | |
b22128ef JM |
1993 | p2p_connect_send(p2p, p2p->go_neg_peer); |
1994 | } | |
1995 | ||
1996 | ||
1997 | static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx) | |
1998 | { | |
1999 | struct p2p_data *p2p = eloop_ctx; | |
2000 | if (p2p->invite_peer == NULL) | |
2001 | return; | |
7549c178 JM |
2002 | if (p2p->pending_listen_freq) { |
2003 | p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start"); | |
2004 | p2p->pending_listen_freq = 0; | |
2005 | } | |
b22128ef | 2006 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
8e9f53c3 JM |
2007 | p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr, |
2008 | p2p->invite_dev_pw_id); | |
b22128ef JM |
2009 | } |
2010 | ||
2011 | ||
2012 | static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr, | |
2013 | const u8 *ie, size_t ie_len) | |
2014 | { | |
2015 | struct p2p_message msg; | |
2016 | struct p2p_device *dev; | |
2017 | ||
2018 | os_memset(&msg, 0, sizeof(msg)); | |
2019 | if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL) | |
2020 | { | |
2021 | p2p_parse_free(&msg); | |
2022 | return; /* not a P2P probe */ | |
2023 | } | |
2024 | ||
2025 | if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN || | |
2026 | os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) | |
2027 | != 0) { | |
2028 | /* The Probe Request is not part of P2P Device Discovery. It is | |
2029 | * not known whether the source address of the frame is the P2P | |
2030 | * Device Address or P2P Interface Address. Do not add a new | |
2031 | * peer entry based on this frames. | |
2032 | */ | |
2033 | p2p_parse_free(&msg); | |
2034 | return; | |
2035 | } | |
2036 | ||
2037 | dev = p2p_get_device(p2p, addr); | |
2038 | if (dev) { | |
12c1fdf1 IP |
2039 | if (msg.listen_channel) { |
2040 | int freq; | |
2041 | ||
2042 | if (dev->country[0] == 0) | |
2043 | os_memcpy(dev->country, msg.listen_channel, 3); | |
2044 | ||
2045 | freq = p2p_channel_to_freq(msg.listen_channel[3], | |
2046 | msg.listen_channel[4]); | |
2047 | ||
2048 | if (freq > 0 && dev->listen_freq != freq) { | |
2049 | p2p_dbg(p2p, | |
2050 | "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz", | |
2051 | MAC2STR(addr), dev->listen_freq, freq); | |
2052 | dev->listen_freq = freq; | |
2053 | } | |
2054 | } | |
2055 | ||
acb69cec | 2056 | os_get_reltime(&dev->last_seen); |
b22128ef JM |
2057 | p2p_parse_free(&msg); |
2058 | return; /* already known */ | |
2059 | } | |
2060 | ||
2061 | dev = p2p_create_device(p2p, addr); | |
2062 | if (dev == NULL) { | |
2063 | p2p_parse_free(&msg); | |
2064 | return; | |
2065 | } | |
2066 | ||
acb69cec | 2067 | os_get_reltime(&dev->last_seen); |
b22128ef JM |
2068 | dev->flags |= P2P_DEV_PROBE_REQ_ONLY; |
2069 | ||
b22128ef JM |
2070 | if (msg.listen_channel) { |
2071 | os_memcpy(dev->country, msg.listen_channel, 3); | |
9ccd9165 | 2072 | dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3], |
b22128ef JM |
2073 | msg.listen_channel[4]); |
2074 | } | |
2075 | ||
ed496f13 | 2076 | p2p_copy_wps_info(p2p, dev, 1, &msg); |
e57ae6e1 | 2077 | |
9675ce35 JM |
2078 | if (msg.wfd_subelems) { |
2079 | wpabuf_free(dev->info.wfd_subelems); | |
2080 | dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems); | |
2081 | } | |
2082 | ||
b22128ef JM |
2083 | p2p_parse_free(&msg); |
2084 | ||
ed496f13 | 2085 | p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR |
b22128ef | 2086 | " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d", |
c5db8e51 KRK |
2087 | MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab, |
2088 | dev->info.group_capab, dev->info.device_name, | |
2089 | dev->listen_freq); | |
b22128ef JM |
2090 | } |
2091 | ||
2092 | ||
2093 | struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p, | |
2094 | const u8 *addr, | |
2095 | struct p2p_message *msg) | |
2096 | { | |
2097 | struct p2p_device *dev; | |
2098 | ||
2099 | dev = p2p_get_device(p2p, addr); | |
2100 | if (dev) { | |
acb69cec | 2101 | os_get_reltime(&dev->last_seen); |
b22128ef JM |
2102 | return dev; /* already known */ |
2103 | } | |
2104 | ||
2105 | dev = p2p_create_device(p2p, addr); | |
2106 | if (dev == NULL) | |
2107 | return NULL; | |
2108 | ||
2109 | p2p_add_dev_info(p2p, addr, dev, msg); | |
2110 | ||
2111 | return dev; | |
2112 | } | |
2113 | ||
2114 | ||
2115 | static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type) | |
2116 | { | |
2117 | if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0) | |
2118 | return 1; | |
2119 | if (os_memcmp(dev_type, req_dev_type, 2) == 0 && | |
2120 | WPA_GET_BE32(&req_dev_type[2]) == 0 && | |
2121 | WPA_GET_BE16(&req_dev_type[6]) == 0) | |
2122 | return 1; /* Category match with wildcard OUI/sub-category */ | |
2123 | return 0; | |
2124 | } | |
2125 | ||
2126 | ||
2127 | int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[], | |
2128 | size_t num_req_dev_type) | |
2129 | { | |
2130 | size_t i; | |
2131 | for (i = 0; i < num_req_dev_type; i++) { | |
2132 | if (dev_type_match(dev_type, req_dev_type[i])) | |
2133 | return 1; | |
2134 | } | |
2135 | return 0; | |
2136 | } | |
2137 | ||
2138 | ||
2139 | /** | |
2140 | * p2p_match_dev_type - Match local device type with requested type | |
2141 | * @p2p: P2P module context from p2p_init() | |
2142 | * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs) | |
2143 | * Returns: 1 on match, 0 on mismatch | |
2144 | * | |
2145 | * This function can be used to match the Requested Device Type attribute in | |
2146 | * WPS IE with the local device types for deciding whether to reply to a Probe | |
2147 | * Request frame. | |
2148 | */ | |
2149 | int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps) | |
2150 | { | |
2151 | struct wps_parse_attr attr; | |
2152 | size_t i; | |
2153 | ||
2154 | if (wps_parse_msg(wps, &attr)) | |
2155 | return 1; /* assume no Requested Device Type attributes */ | |
2156 | ||
2157 | if (attr.num_req_dev_type == 0) | |
2158 | return 1; /* no Requested Device Type attributes -> match */ | |
2159 | ||
2160 | if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type, | |
2161 | attr.num_req_dev_type)) | |
2162 | return 1; /* Own Primary Device Type matches */ | |
2163 | ||
e635efb4 | 2164 | for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) { |
b22128ef JM |
2165 | if (dev_type_list_match(p2p->cfg->sec_dev_type[i], |
2166 | attr.req_dev_type, | |
2167 | attr.num_req_dev_type)) | |
e635efb4 JM |
2168 | return 1; /* Own Secondary Device Type matches */ |
2169 | } | |
b22128ef JM |
2170 | |
2171 | /* No matching device type found */ | |
2172 | return 0; | |
2173 | } | |
2174 | ||
2175 | ||
e12c4004 MS |
2176 | struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p, |
2177 | const u8 *query_hash, | |
2178 | u8 query_count) | |
b22128ef JM |
2179 | { |
2180 | struct wpabuf *buf; | |
2181 | u8 *len; | |
360182ed | 2182 | int pw_id = -1; |
9675ce35 | 2183 | size_t extra = 0; |
b22128ef | 2184 | |
9675ce35 JM |
2185 | #ifdef CONFIG_WIFI_DISPLAY |
2186 | if (p2p->wfd_ie_probe_resp) | |
2187 | extra = wpabuf_len(p2p->wfd_ie_probe_resp); | |
2188 | #endif /* CONFIG_WIFI_DISPLAY */ | |
2189 | ||
86bd36f0 JM |
2190 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]) |
2191 | extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]); | |
2192 | ||
e12c4004 | 2193 | if (query_count) |
4660e732 BG |
2194 | extra += MAX_SVC_ADV_IE_LEN; |
2195 | ||
9675ce35 | 2196 | buf = wpabuf_alloc(1000 + extra); |
b22128ef JM |
2197 | if (buf == NULL) |
2198 | return NULL; | |
2199 | ||
360182ed JM |
2200 | if (p2p->go_neg_peer) { |
2201 | /* Advertise immediate availability of WPS credential */ | |
2202 | pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method); | |
2203 | } | |
2204 | ||
33183761 JM |
2205 | if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) { |
2206 | p2p_dbg(p2p, "Failed to build WPS IE for Probe Response"); | |
2207 | wpabuf_free(buf); | |
2208 | return NULL; | |
2209 | } | |
b22128ef | 2210 | |
9675ce35 JM |
2211 | #ifdef CONFIG_WIFI_DISPLAY |
2212 | if (p2p->wfd_ie_probe_resp) | |
2213 | wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp); | |
2214 | #endif /* CONFIG_WIFI_DISPLAY */ | |
2215 | ||
86bd36f0 JM |
2216 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]) |
2217 | wpabuf_put_buf(buf, | |
2218 | p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]); | |
2219 | ||
b22128ef JM |
2220 | /* P2P IE */ |
2221 | len = p2p_buf_add_ie_hdr(buf); | |
18485b54 MH |
2222 | p2p_buf_add_capability(buf, p2p->dev_capab & |
2223 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); | |
b22128ef JM |
2224 | if (p2p->ext_listen_interval) |
2225 | p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period, | |
2226 | p2p->ext_listen_interval); | |
2227 | p2p_buf_add_device_info(buf, p2p, NULL); | |
2228 | p2p_buf_update_ie_hdr(buf, len); | |
2229 | ||
e12c4004 MS |
2230 | if (query_count) { |
2231 | p2p_buf_add_service_instance(buf, p2p, query_count, query_hash, | |
4660e732 BG |
2232 | p2p->p2ps_adv_list); |
2233 | } | |
2234 | ||
b22128ef JM |
2235 | return buf; |
2236 | } | |
2237 | ||
a6f5b193 PX |
2238 | static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf, |
2239 | struct wpabuf *ies, | |
2240 | const u8 *addr, int rx_freq) | |
2241 | { | |
2242 | struct ieee80211_mgmt *resp; | |
2243 | u8 channel, op_class; | |
2244 | ||
2245 | resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt, | |
2246 | u.probe_resp.variable)); | |
2247 | ||
2248 | resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) | | |
2249 | (WLAN_FC_STYPE_PROBE_RESP << 4)); | |
2250 | os_memcpy(resp->da, addr, ETH_ALEN); | |
2251 | os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN); | |
2252 | os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN); | |
2253 | resp->u.probe_resp.beacon_int = host_to_le16(100); | |
2254 | /* hardware or low-level driver will setup seq_ctrl and timestamp */ | |
2255 | resp->u.probe_resp.capab_info = | |
2256 | host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE | | |
2257 | WLAN_CAPABILITY_PRIVACY | | |
2258 | WLAN_CAPABILITY_SHORT_SLOT_TIME); | |
2259 | ||
2260 | wpabuf_put_u8(buf, WLAN_EID_SSID); | |
2261 | wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN); | |
2262 | wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); | |
2263 | ||
2264 | wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES); | |
2265 | wpabuf_put_u8(buf, 8); | |
2266 | wpabuf_put_u8(buf, (60 / 5) | 0x80); | |
2267 | wpabuf_put_u8(buf, 90 / 5); | |
2268 | wpabuf_put_u8(buf, (120 / 5) | 0x80); | |
2269 | wpabuf_put_u8(buf, 180 / 5); | |
2270 | wpabuf_put_u8(buf, (240 / 5) | 0x80); | |
2271 | wpabuf_put_u8(buf, 360 / 5); | |
2272 | wpabuf_put_u8(buf, 480 / 5); | |
2273 | wpabuf_put_u8(buf, 540 / 5); | |
2274 | ||
2275 | if (!rx_freq) { | |
2276 | channel = p2p->cfg->channel; | |
2277 | } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) { | |
2278 | p2p_err(p2p, "Failed to convert freq to channel"); | |
2279 | return -1; | |
2280 | } | |
2281 | ||
2282 | wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS); | |
2283 | wpabuf_put_u8(buf, 1); | |
2284 | wpabuf_put_u8(buf, channel); | |
2285 | ||
2286 | wpabuf_put_buf(buf, ies); | |
2287 | ||
2288 | return 0; | |
2289 | } | |
b22128ef | 2290 | |
9e7321ee KV |
2291 | static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash) |
2292 | { | |
2293 | struct p2ps_advertisement *adv_data; | |
ebdc32f3 | 2294 | int any_wfa; |
9e7321ee KV |
2295 | |
2296 | p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list); | |
2297 | ||
ebdc32f3 JM |
2298 | /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */ |
2299 | any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0; | |
9e7321ee KV |
2300 | |
2301 | adv_data = p2p->p2ps_adv_list; | |
2302 | while (adv_data) { | |
9e7321ee | 2303 | if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0) |
ebdc32f3 JM |
2304 | return 1; /* exact hash match */ |
2305 | if (any_wfa && | |
2306 | os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR, | |
2307 | os_strlen(P2PS_WILD_HASH_STR)) == 0) | |
2308 | return 1; /* WFA service match */ | |
9e7321ee KV |
2309 | adv_data = adv_data->next; |
2310 | } | |
2311 | ||
2312 | return 0; | |
2313 | } | |
2314 | ||
2315 | ||
2d43d37f JB |
2316 | static enum p2p_probe_req_status |
2317 | p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst, | |
734ddf61 MS |
2318 | const u8 *bssid, const u8 *ie, size_t ie_len, |
2319 | unsigned int rx_freq) | |
b22128ef JM |
2320 | { |
2321 | struct ieee802_11_elems elems; | |
2322 | struct wpabuf *buf; | |
97c5b3c4 | 2323 | struct p2p_message msg; |
b22128ef JM |
2324 | struct wpabuf *ies; |
2325 | ||
b22128ef JM |
2326 | if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) == |
2327 | ParseFailed) { | |
2328 | /* Ignore invalid Probe Request frames */ | |
a5527a49 | 2329 | p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it"); |
2d43d37f | 2330 | return P2P_PREQ_MALFORMED; |
b22128ef JM |
2331 | } |
2332 | ||
2333 | if (elems.p2p == NULL) { | |
2334 | /* not a P2P probe - ignore it */ | |
a5527a49 | 2335 | p2p_dbg(p2p, "Not a P2P probe - ignore it"); |
2d43d37f | 2336 | return P2P_PREQ_NOT_P2P; |
b22128ef JM |
2337 | } |
2338 | ||
04a85e44 JM |
2339 | if (dst && !is_broadcast_ether_addr(dst) && |
2340 | os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) { | |
2341 | /* Not sent to the broadcast address or our P2P Device Address | |
2342 | */ | |
a5527a49 JM |
2343 | p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it", |
2344 | MAC2STR(dst)); | |
2d43d37f | 2345 | return P2P_PREQ_NOT_PROCESSED; |
04a85e44 JM |
2346 | } |
2347 | ||
2348 | if (bssid && !is_broadcast_ether_addr(bssid)) { | |
2349 | /* Not sent to the Wildcard BSSID */ | |
a5527a49 JM |
2350 | p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it", |
2351 | MAC2STR(bssid)); | |
2d43d37f | 2352 | return P2P_PREQ_NOT_PROCESSED; |
04a85e44 JM |
2353 | } |
2354 | ||
b22128ef JM |
2355 | if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN || |
2356 | os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) != | |
2357 | 0) { | |
2358 | /* not using P2P Wildcard SSID - ignore */ | |
a5527a49 | 2359 | p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it"); |
2d43d37f | 2360 | return P2P_PREQ_NOT_PROCESSED; |
b22128ef JM |
2361 | } |
2362 | ||
e1d52629 JM |
2363 | if (supp_rates_11b_only(&elems)) { |
2364 | /* Indicates support for 11b rates only */ | |
a5527a49 | 2365 | p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it"); |
2d43d37f | 2366 | return P2P_PREQ_NOT_P2P; |
e1d52629 JM |
2367 | } |
2368 | ||
97c5b3c4 JM |
2369 | os_memset(&msg, 0, sizeof(msg)); |
2370 | if (p2p_parse_ies(ie, ie_len, &msg) < 0) { | |
2371 | /* Could not parse P2P attributes */ | |
a5527a49 | 2372 | p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it"); |
2d43d37f | 2373 | return P2P_PREQ_NOT_P2P; |
97c5b3c4 JM |
2374 | } |
2375 | ||
9e7321ee KV |
2376 | if (msg.service_hash && msg.service_hash_count) { |
2377 | const u8 *hash = msg.service_hash; | |
9e7321ee | 2378 | u8 i; |
886f583d | 2379 | int p2ps_svc_found = 0; |
9e7321ee | 2380 | |
a4062443 JM |
2381 | p2p_dbg(p2p, "in_listen=%d drv_in_listen=%d when received P2PS Probe Request at %u MHz; own Listen channel %u, pending listen freq %u MHz", |
2382 | p2p->in_listen, p2p->drv_in_listen, rx_freq, | |
2383 | p2p->cfg->channel, p2p->pending_listen_freq); | |
2384 | ||
2385 | if (!p2p->in_listen && !p2p->drv_in_listen && | |
2386 | p2p->pending_listen_freq && rx_freq && | |
2387 | rx_freq != p2p->pending_listen_freq) { | |
2388 | p2p_dbg(p2p, "Do not reply to Probe Request frame that was received on %u MHz while waiting to start Listen state on %u MHz", | |
2389 | rx_freq, p2p->pending_listen_freq); | |
2390 | p2p_parse_free(&msg); | |
2391 | return P2P_PREQ_NOT_LISTEN; | |
2392 | } | |
2393 | ||
9e7321ee KV |
2394 | for (i = 0; i < msg.service_hash_count; i++) { |
2395 | if (p2p_service_find_asp(p2p, hash)) { | |
fdde3db6 JM |
2396 | p2p_dbg(p2p, "Service Hash match found: " |
2397 | MACSTR, MAC2STR(hash)); | |
886f583d | 2398 | p2ps_svc_found = 1; |
fdde3db6 | 2399 | break; |
9e7321ee KV |
2400 | } |
2401 | hash += P2PS_HASH_LEN; | |
2402 | } | |
2403 | ||
9e7321ee | 2404 | /* Probed hash unknown */ |
886f583d | 2405 | if (!p2ps_svc_found) { |
fdde3db6 | 2406 | p2p_dbg(p2p, "No Service Hash match found"); |
9e7321ee KV |
2407 | p2p_parse_free(&msg); |
2408 | return P2P_PREQ_NOT_PROCESSED; | |
2409 | } | |
2410 | } else { | |
2411 | /* This is not a P2PS Probe Request */ | |
9e7321ee KV |
2412 | p2p_dbg(p2p, "No P2PS Hash in Probe Request"); |
2413 | ||
2414 | if (!p2p->in_listen || !p2p->drv_in_listen) { | |
2415 | /* not in Listen state - ignore Probe Request */ | |
2416 | p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request", | |
2417 | p2p->in_listen, p2p->drv_in_listen); | |
2418 | p2p_parse_free(&msg); | |
2419 | return P2P_PREQ_NOT_LISTEN; | |
2420 | } | |
2421 | } | |
2422 | ||
97c5b3c4 | 2423 | if (msg.device_id && |
1c7447d0 | 2424 | os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) { |
97c5b3c4 | 2425 | /* Device ID did not match */ |
a5527a49 JM |
2426 | p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it", |
2427 | MAC2STR(msg.device_id)); | |
97c5b3c4 | 2428 | p2p_parse_free(&msg); |
2d43d37f | 2429 | return P2P_PREQ_NOT_PROCESSED; |
97c5b3c4 JM |
2430 | } |
2431 | ||
b22128ef | 2432 | /* Check Requested Device Type match */ |
97c5b3c4 JM |
2433 | if (msg.wps_attributes && |
2434 | !p2p_match_dev_type(p2p, msg.wps_attributes)) { | |
b22128ef | 2435 | /* No match with Requested Device Type */ |
b3e8ca65 | 2436 | p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it"); |
97c5b3c4 | 2437 | p2p_parse_free(&msg); |
2d43d37f | 2438 | return P2P_PREQ_NOT_PROCESSED; |
b22128ef | 2439 | } |
b22128ef | 2440 | |
2d43d37f JB |
2441 | if (!p2p->cfg->send_probe_resp) { |
2442 | /* Response generated elsewhere */ | |
a5527a49 | 2443 | p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response"); |
fdde3db6 | 2444 | p2p_parse_free(&msg); |
2d43d37f JB |
2445 | return P2P_PREQ_NOT_PROCESSED; |
2446 | } | |
b22128ef | 2447 | |
ed496f13 | 2448 | p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state"); |
b22128ef JM |
2449 | |
2450 | /* | |
2451 | * We do not really have a specific BSS that this frame is advertising, | |
2452 | * so build a frame that has some information in valid format. This is | |
2453 | * really only used for discovery purposes, not to learn exact BSS | |
2454 | * parameters. | |
2455 | */ | |
fdde3db6 JM |
2456 | ies = p2p_build_probe_resp_ies(p2p, msg.service_hash, |
2457 | msg.service_hash_count); | |
2458 | p2p_parse_free(&msg); | |
b22128ef | 2459 | if (ies == NULL) |
2d43d37f | 2460 | return P2P_PREQ_NOT_PROCESSED; |
b22128ef JM |
2461 | |
2462 | buf = wpabuf_alloc(200 + wpabuf_len(ies)); | |
2463 | if (buf == NULL) { | |
2464 | wpabuf_free(ies); | |
2d43d37f | 2465 | return P2P_PREQ_NOT_PROCESSED; |
b22128ef JM |
2466 | } |
2467 | ||
a6f5b193 | 2468 | if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) { |
734ddf61 MS |
2469 | wpabuf_free(ies); |
2470 | wpabuf_free(buf); | |
2471 | return P2P_PREQ_NOT_PROCESSED; | |
2472 | } | |
2473 | ||
b22128ef JM |
2474 | wpabuf_free(ies); |
2475 | ||
0799b3f8 | 2476 | p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq); |
b22128ef JM |
2477 | |
2478 | wpabuf_free(buf); | |
2d43d37f | 2479 | |
5143e7ee | 2480 | return P2P_PREQ_PROCESSED; |
b22128ef JM |
2481 | } |
2482 | ||
2483 | ||
2d43d37f JB |
2484 | enum p2p_probe_req_status |
2485 | p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst, | |
734ddf61 | 2486 | const u8 *bssid, const u8 *ie, size_t ie_len, |
a6f5b193 | 2487 | unsigned int rx_freq, int p2p_lo_started) |
b22128ef | 2488 | { |
2d43d37f JB |
2489 | enum p2p_probe_req_status res; |
2490 | ||
b22128ef JM |
2491 | p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len); |
2492 | ||
a6f5b193 PX |
2493 | if (p2p_lo_started) { |
2494 | p2p_dbg(p2p, | |
2495 | "Probe Response is offloaded, do not reply Probe Request"); | |
2496 | return P2P_PREQ_PROCESSED; | |
2497 | } | |
2498 | ||
734ddf61 | 2499 | res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq); |
5143e7ee MS |
2500 | if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED) |
2501 | return res; | |
2502 | ||
2503 | /* | |
2504 | * Activate a pending GO Negotiation/Invite flow if a received Probe | |
2505 | * Request frame is from an expected peer. Some devices may share the | |
2506 | * same address for P2P and non-P2P STA running simultaneously. The | |
2507 | * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe() | |
2508 | * return values verified above ensure we are handling a Probe Request | |
2509 | * frame from a P2P peer. | |
2510 | */ | |
b22128ef JM |
2511 | if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) && |
2512 | p2p->go_neg_peer && | |
c5db8e51 | 2513 | os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN) |
c03e2113 JM |
2514 | == 0 && |
2515 | !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) { | |
b22128ef | 2516 | /* Received a Probe Request from GO Negotiation peer */ |
ed496f13 | 2517 | p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout"); |
8cee87ab | 2518 | eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL); |
b22128ef | 2519 | eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL); |
5143e7ee | 2520 | return res; |
b22128ef JM |
2521 | } |
2522 | ||
2523 | if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) && | |
2524 | p2p->invite_peer && | |
a05e236b | 2525 | (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) && |
c5db8e51 KRK |
2526 | os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN) |
2527 | == 0) { | |
b22128ef | 2528 | /* Received a Probe Request from Invite peer */ |
ed496f13 | 2529 | p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout"); |
aef5047a | 2530 | eloop_cancel_timeout(p2p_invite_start, p2p, NULL); |
b22128ef | 2531 | eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL); |
5143e7ee | 2532 | return res; |
b22128ef JM |
2533 | } |
2534 | ||
2d43d37f | 2535 | return res; |
b22128ef JM |
2536 | } |
2537 | ||
2538 | ||
2539 | static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid, | |
4c08c0bd | 2540 | u8 *buf, size_t len, struct wpabuf *p2p_ie) |
b22128ef JM |
2541 | { |
2542 | struct wpabuf *tmp; | |
2543 | u8 *lpos; | |
2544 | size_t tmplen; | |
2545 | int res; | |
72044390 | 2546 | u8 group_capab; |
9b05135a | 2547 | struct p2p_message msg; |
b22128ef | 2548 | |
4c08c0bd JM |
2549 | if (p2p_ie == NULL) |
2550 | return 0; /* WLAN AP is not a P2P manager */ | |
b22128ef | 2551 | |
9b05135a IP |
2552 | os_memset(&msg, 0, sizeof(msg)); |
2553 | if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0) | |
2554 | return 0; | |
2555 | ||
2556 | p2p_dbg(p2p, "BSS P2P manageability %s", | |
2557 | msg.manageability ? "enabled" : "disabled"); | |
2558 | ||
2559 | if (!msg.manageability) | |
2560 | return 0; | |
2561 | ||
b22128ef JM |
2562 | /* |
2563 | * (Re)Association Request - P2P IE | |
2564 | * P2P Capability attribute (shall be present) | |
4c08c0bd JM |
2565 | * P2P Interface attribute (present if concurrent device and |
2566 | * P2P Management is enabled) | |
b22128ef JM |
2567 | */ |
2568 | tmp = wpabuf_alloc(200); | |
2569 | if (tmp == NULL) | |
2570 | return -1; | |
2571 | ||
2572 | lpos = p2p_buf_add_ie_hdr(tmp); | |
72044390 JM |
2573 | group_capab = 0; |
2574 | if (p2p->num_groups > 0) { | |
2575 | group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER; | |
2576 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && | |
2577 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) && | |
2578 | p2p->cross_connect) | |
2579 | group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; | |
2580 | } | |
2581 | p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab); | |
4c08c0bd JM |
2582 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && |
2583 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED)) | |
b22128ef JM |
2584 | p2p_buf_add_p2p_interface(tmp, p2p); |
2585 | p2p_buf_update_ie_hdr(tmp, lpos); | |
2586 | ||
2587 | tmplen = wpabuf_len(tmp); | |
2588 | if (tmplen > len) | |
2589 | res = -1; | |
2590 | else { | |
2591 | os_memcpy(buf, wpabuf_head(tmp), tmplen); | |
2592 | res = tmplen; | |
2593 | } | |
2594 | wpabuf_free(tmp); | |
2595 | ||
2596 | return res; | |
2597 | } | |
2598 | ||
2599 | ||
2600 | int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf, | |
4c08c0bd | 2601 | size_t len, int p2p_group, struct wpabuf *p2p_ie) |
b22128ef JM |
2602 | { |
2603 | struct wpabuf *tmp; | |
2604 | u8 *lpos; | |
2605 | struct p2p_device *peer; | |
2606 | size_t tmplen; | |
2607 | int res; | |
9675ce35 | 2608 | size_t extra = 0; |
b22128ef JM |
2609 | |
2610 | if (!p2p_group) | |
4c08c0bd | 2611 | return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie); |
b22128ef | 2612 | |
9675ce35 JM |
2613 | #ifdef CONFIG_WIFI_DISPLAY |
2614 | if (p2p->wfd_ie_assoc_req) | |
2615 | extra = wpabuf_len(p2p->wfd_ie_assoc_req); | |
2616 | #endif /* CONFIG_WIFI_DISPLAY */ | |
2617 | ||
86bd36f0 JM |
2618 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]) |
2619 | extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]); | |
2620 | ||
b22128ef JM |
2621 | /* |
2622 | * (Re)Association Request - P2P IE | |
2623 | * P2P Capability attribute (shall be present) | |
2624 | * Extended Listen Timing (may be present) | |
2625 | * P2P Device Info attribute (shall be present) | |
2626 | */ | |
9675ce35 | 2627 | tmp = wpabuf_alloc(200 + extra); |
b22128ef JM |
2628 | if (tmp == NULL) |
2629 | return -1; | |
2630 | ||
9675ce35 JM |
2631 | #ifdef CONFIG_WIFI_DISPLAY |
2632 | if (p2p->wfd_ie_assoc_req) | |
2633 | wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req); | |
2634 | #endif /* CONFIG_WIFI_DISPLAY */ | |
2635 | ||
86bd36f0 JM |
2636 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]) |
2637 | wpabuf_put_buf(tmp, | |
2638 | p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]); | |
2639 | ||
b22128ef JM |
2640 | peer = bssid ? p2p_get_device(p2p, bssid) : NULL; |
2641 | ||
2642 | lpos = p2p_buf_add_ie_hdr(tmp); | |
2643 | p2p_buf_add_capability(tmp, p2p->dev_capab, 0); | |
5be5305b JM |
2644 | if (p2p->ext_listen_interval) |
2645 | p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period, | |
2646 | p2p->ext_listen_interval); | |
b22128ef JM |
2647 | p2p_buf_add_device_info(tmp, p2p, peer); |
2648 | p2p_buf_update_ie_hdr(tmp, lpos); | |
2649 | ||
2650 | tmplen = wpabuf_len(tmp); | |
2651 | if (tmplen > len) | |
2652 | res = -1; | |
2653 | else { | |
2654 | os_memcpy(buf, wpabuf_head(tmp), tmplen); | |
2655 | res = tmplen; | |
2656 | } | |
2657 | wpabuf_free(tmp); | |
2658 | ||
2659 | return res; | |
2660 | } | |
2661 | ||
2662 | ||
2663 | int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end) | |
2664 | { | |
2665 | struct wpabuf *p2p_ie; | |
2666 | int ret; | |
2667 | ||
2668 | p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE); | |
2669 | if (p2p_ie == NULL) | |
2670 | return 0; | |
2671 | ||
2672 | ret = p2p_attr_text(p2p_ie, buf, end); | |
2673 | wpabuf_free(p2p_ie); | |
2674 | return ret; | |
2675 | } | |
2676 | ||
2677 | ||
ae9d45f3 KV |
2678 | struct p2ps_advertisement * |
2679 | p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id) | |
2680 | { | |
2681 | struct p2ps_advertisement *adv_data; | |
2682 | ||
2683 | if (!p2p) | |
2684 | return NULL; | |
2685 | ||
2686 | adv_data = p2p->p2ps_adv_list; | |
2687 | while (adv_data) { | |
2688 | if (adv_data->id == adv_id) | |
2689 | return adv_data; | |
2690 | adv_data = adv_data->next; | |
2691 | } | |
2692 | ||
2693 | return NULL; | |
2694 | } | |
2695 | ||
2696 | ||
2697 | int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id) | |
2698 | { | |
2699 | struct p2ps_advertisement *adv_data; | |
2700 | struct p2ps_advertisement **prior; | |
2701 | ||
2702 | if (!p2p) | |
2703 | return -1; | |
2704 | ||
2705 | adv_data = p2p->p2ps_adv_list; | |
2706 | prior = &p2p->p2ps_adv_list; | |
2707 | while (adv_data) { | |
2708 | if (adv_data->id == adv_id) { | |
2709 | p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id); | |
2710 | *prior = adv_data->next; | |
2711 | os_free(adv_data); | |
2712 | return 0; | |
2713 | } | |
2714 | prior = &adv_data->next; | |
2715 | adv_data = adv_data->next; | |
2716 | } | |
2717 | ||
2718 | return -1; | |
2719 | } | |
2720 | ||
2721 | ||
2722 | int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id, | |
2723 | const char *adv_str, u8 svc_state, u16 config_methods, | |
e2b7fbf2 | 2724 | const char *svc_info, const u8 *cpt_priority) |
ae9d45f3 KV |
2725 | { |
2726 | struct p2ps_advertisement *adv_data, *tmp, **prev; | |
2727 | u8 buf[P2PS_HASH_LEN]; | |
2728 | size_t adv_data_len, adv_len, info_len = 0; | |
e2b7fbf2 | 2729 | int i; |
ae9d45f3 | 2730 | |
e2b7fbf2 | 2731 | if (!p2p || !adv_str || !adv_str[0] || !cpt_priority) |
ae9d45f3 KV |
2732 | return -1; |
2733 | ||
2734 | if (!(config_methods & p2p->cfg->config_methods)) { | |
2735 | p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x", | |
2736 | config_methods, p2p->cfg->config_methods); | |
2737 | return -1; | |
2738 | } | |
2739 | ||
2740 | if (!p2ps_gen_hash(p2p, adv_str, buf)) | |
2741 | return -1; | |
2742 | ||
2743 | if (svc_info) | |
2744 | info_len = os_strlen(svc_info); | |
2745 | adv_len = os_strlen(adv_str); | |
2746 | adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 + | |
2747 | info_len + 1; | |
2748 | ||
2749 | adv_data = os_zalloc(adv_data_len); | |
2750 | if (!adv_data) | |
2751 | return -1; | |
2752 | ||
2753 | os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN); | |
2754 | adv_data->id = adv_id; | |
2755 | adv_data->state = svc_state; | |
2756 | adv_data->config_methods = config_methods & p2p->cfg->config_methods; | |
2757 | adv_data->auto_accept = (u8) auto_accept; | |
2758 | os_memcpy(adv_data->svc_name, adv_str, adv_len); | |
2759 | ||
e2b7fbf2 MS |
2760 | for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) { |
2761 | adv_data->cpt_priority[i] = cpt_priority[i]; | |
2762 | adv_data->cpt_mask |= cpt_priority[i]; | |
2763 | } | |
2764 | ||
ae9d45f3 KV |
2765 | if (svc_info && info_len) { |
2766 | adv_data->svc_info = &adv_data->svc_name[adv_len + 1]; | |
2767 | os_memcpy(adv_data->svc_info, svc_info, info_len); | |
2768 | } | |
2769 | ||
2770 | /* | |
2771 | * Group Advertisements by service string. They do not need to be | |
2772 | * sorted, but groups allow easier Probe Response instance grouping | |
2773 | */ | |
2774 | tmp = p2p->p2ps_adv_list; | |
2775 | prev = &p2p->p2ps_adv_list; | |
2776 | while (tmp) { | |
2777 | if (tmp->id == adv_data->id) { | |
2778 | if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) { | |
2779 | os_free(adv_data); | |
2780 | return -1; | |
2781 | } | |
2782 | adv_data->next = tmp->next; | |
2783 | *prev = adv_data; | |
2784 | os_free(tmp); | |
2785 | goto inserted; | |
2786 | } else { | |
2787 | if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) { | |
2788 | adv_data->next = tmp->next; | |
2789 | tmp->next = adv_data; | |
2790 | goto inserted; | |
2791 | } | |
2792 | } | |
2793 | prev = &tmp->next; | |
2794 | tmp = tmp->next; | |
2795 | } | |
2796 | ||
2797 | /* No svc_name match found */ | |
2798 | adv_data->next = p2p->p2ps_adv_list; | |
2799 | p2p->p2ps_adv_list = adv_data; | |
2800 | ||
2801 | inserted: | |
2802 | p2p_dbg(p2p, | |
e2b7fbf2 MS |
2803 | "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x", |
2804 | adv_id, adv_data->config_methods, svc_state, adv_str, | |
2805 | adv_data->cpt_mask); | |
ae9d45f3 KV |
2806 | |
2807 | return 0; | |
2808 | } | |
2809 | ||
2810 | ||
c40a8918 MS |
2811 | void p2p_service_flush_asp(struct p2p_data *p2p) |
2812 | { | |
2813 | struct p2ps_advertisement *adv, *prev; | |
2814 | ||
2815 | if (!p2p) | |
2816 | return; | |
2817 | ||
2818 | adv = p2p->p2ps_adv_list; | |
2819 | while (adv) { | |
2820 | prev = adv; | |
2821 | adv = adv->next; | |
2822 | os_free(prev); | |
2823 | } | |
2824 | ||
2825 | p2p->p2ps_adv_list = NULL; | |
78a3b230 | 2826 | p2ps_prov_free(p2p); |
c40a8918 MS |
2827 | p2p_dbg(p2p, "All ASP advertisements flushed"); |
2828 | } | |
2829 | ||
2830 | ||
c2d76aa6 | 2831 | int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr) |
0a70f34f | 2832 | { |
0a70f34f JM |
2833 | struct p2p_message msg; |
2834 | ||
0a70f34f | 2835 | os_memset(&msg, 0, sizeof(msg)); |
c2d76aa6 | 2836 | if (p2p_parse_p2p_ie(p2p_ie, &msg)) |
0a70f34f | 2837 | return -1; |
0a70f34f | 2838 | |
526ec4ae JM |
2839 | if (msg.p2p_device_addr) { |
2840 | os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN); | |
c2d76aa6 | 2841 | return 0; |
526ec4ae JM |
2842 | } else if (msg.device_id) { |
2843 | os_memcpy(dev_addr, msg.device_id, ETH_ALEN); | |
c2d76aa6 | 2844 | return 0; |
0a70f34f | 2845 | } |
c2d76aa6 MH |
2846 | return -1; |
2847 | } | |
0a70f34f | 2848 | |
c2d76aa6 MH |
2849 | |
2850 | int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr) | |
2851 | { | |
2852 | struct wpabuf *p2p_ie; | |
2853 | int ret; | |
2854 | ||
2855 | p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, | |
2856 | P2P_IE_VENDOR_TYPE); | |
2857 | if (p2p_ie == NULL) | |
2858 | return -1; | |
2859 | ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr); | |
0a70f34f | 2860 | wpabuf_free(p2p_ie); |
526ec4ae | 2861 | return ret; |
0a70f34f JM |
2862 | } |
2863 | ||
2864 | ||
b22128ef JM |
2865 | static void p2p_clear_go_neg(struct p2p_data *p2p) |
2866 | { | |
2867 | p2p->go_neg_peer = NULL; | |
2868 | p2p_clear_timeout(p2p); | |
2869 | p2p_set_state(p2p, P2P_IDLE); | |
2870 | } | |
2871 | ||
2872 | ||
2873 | void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr) | |
2874 | { | |
2875 | if (p2p->go_neg_peer == NULL) { | |
ed496f13 | 2876 | p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification"); |
b22128ef JM |
2877 | return; /* No pending Group Formation */ |
2878 | } | |
2879 | ||
2880 | if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) != | |
2881 | 0) { | |
ed496f13 JM |
2882 | p2p_dbg(p2p, "Ignore WPS registration success notification for " |
2883 | MACSTR " (GO Negotiation peer " MACSTR ")", | |
b22128ef JM |
2884 | MAC2STR(mac_addr), |
2885 | MAC2STR(p2p->go_neg_peer->intended_addr)); | |
2886 | return; /* Ignore unexpected peer address */ | |
2887 | } | |
2888 | ||
ed496f13 | 2889 | p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR, |
b22128ef JM |
2890 | MAC2STR(mac_addr)); |
2891 | ||
2892 | p2p_clear_go_neg(p2p); | |
2893 | } | |
2894 | ||
2895 | ||
2896 | void p2p_group_formation_failed(struct p2p_data *p2p) | |
2897 | { | |
2898 | if (p2p->go_neg_peer == NULL) { | |
ed496f13 | 2899 | p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification"); |
b22128ef JM |
2900 | return; /* No pending Group Formation */ |
2901 | } | |
2902 | ||
ed496f13 | 2903 | p2p_dbg(p2p, "Group Formation failed with " MACSTR, |
b22128ef JM |
2904 | MAC2STR(p2p->go_neg_peer->intended_addr)); |
2905 | ||
2906 | p2p_clear_go_neg(p2p); | |
2907 | } | |
2908 | ||
2909 | ||
2910 | struct p2p_data * p2p_init(const struct p2p_config *cfg) | |
2911 | { | |
2912 | struct p2p_data *p2p; | |
2913 | ||
1b928f96 JM |
2914 | if (cfg->max_peers < 1 || |
2915 | cfg->passphrase_len < 8 || cfg->passphrase_len > 63) | |
b22128ef JM |
2916 | return NULL; |
2917 | ||
2918 | p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg)); | |
2919 | if (p2p == NULL) | |
2920 | return NULL; | |
2921 | p2p->cfg = (struct p2p_config *) (p2p + 1); | |
2922 | os_memcpy(p2p->cfg, cfg, sizeof(*cfg)); | |
2923 | if (cfg->dev_name) | |
2924 | p2p->cfg->dev_name = os_strdup(cfg->dev_name); | |
b6e01800 JM |
2925 | if (cfg->manufacturer) |
2926 | p2p->cfg->manufacturer = os_strdup(cfg->manufacturer); | |
2927 | if (cfg->model_name) | |
2928 | p2p->cfg->model_name = os_strdup(cfg->model_name); | |
2929 | if (cfg->model_number) | |
2930 | p2p->cfg->model_number = os_strdup(cfg->model_number); | |
2931 | if (cfg->serial_number) | |
2932 | p2p->cfg->serial_number = os_strdup(cfg->serial_number); | |
21d996f7 JM |
2933 | if (cfg->pref_chan) { |
2934 | p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan * | |
2935 | sizeof(struct p2p_channel)); | |
2936 | if (p2p->cfg->pref_chan) { | |
2937 | os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan, | |
2938 | cfg->num_pref_chan * | |
2939 | sizeof(struct p2p_channel)); | |
2940 | } else | |
2941 | p2p->cfg->num_pref_chan = 0; | |
2942 | } | |
b22128ef | 2943 | |
51775096 BG |
2944 | p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash); |
2945 | ||
b22128ef JM |
2946 | p2p->min_disc_int = 1; |
2947 | p2p->max_disc_int = 3; | |
96beff11 | 2948 | p2p->max_disc_tu = -1; |
b22128ef | 2949 | |
df2508d7 JM |
2950 | if (os_get_random(&p2p->next_tie_breaker, 1) < 0) |
2951 | p2p->next_tie_breaker = 0; | |
b22128ef JM |
2952 | p2p->next_tie_breaker &= 0x01; |
2953 | if (cfg->sd_request) | |
2954 | p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY; | |
2955 | p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE; | |
2956 | if (cfg->concurrent_operations) | |
2957 | p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER; | |
2958 | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
2959 | ||
2960 | dl_list_init(&p2p->devices); | |
2961 | ||
4f219667 JM |
2962 | p2p->go_timeout = 100; |
2963 | p2p->client_timeout = 20; | |
f667e031 | 2964 | p2p->num_p2p_sd_queries = 0; |
4f219667 | 2965 | |
51e9f228 JM |
2966 | p2p_dbg(p2p, "initialized"); |
2967 | p2p_channels_dump(p2p, "channels", &p2p->cfg->channels); | |
2968 | p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels); | |
2969 | ||
b22128ef JM |
2970 | return p2p; |
2971 | } | |
2972 | ||
2973 | ||
2974 | void p2p_deinit(struct p2p_data *p2p) | |
2975 | { | |
9675ce35 JM |
2976 | #ifdef CONFIG_WIFI_DISPLAY |
2977 | wpabuf_free(p2p->wfd_ie_beacon); | |
2978 | wpabuf_free(p2p->wfd_ie_probe_req); | |
2979 | wpabuf_free(p2p->wfd_ie_probe_resp); | |
2980 | wpabuf_free(p2p->wfd_ie_assoc_req); | |
2981 | wpabuf_free(p2p->wfd_ie_invitation); | |
2982 | wpabuf_free(p2p->wfd_ie_prov_disc_req); | |
2983 | wpabuf_free(p2p->wfd_ie_prov_disc_resp); | |
2984 | wpabuf_free(p2p->wfd_ie_go_neg); | |
2985 | wpabuf_free(p2p->wfd_dev_info); | |
2986 | wpabuf_free(p2p->wfd_assoc_bssid); | |
2987 | wpabuf_free(p2p->wfd_coupled_sink_info); | |
e9518ae7 | 2988 | wpabuf_free(p2p->wfd_r2_dev_info); |
9675ce35 JM |
2989 | #endif /* CONFIG_WIFI_DISPLAY */ |
2990 | ||
b22128ef | 2991 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); |
8cee87ab | 2992 | eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL); |
0c6eee8b | 2993 | eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL); |
b22128ef | 2994 | p2p_flush(p2p); |
046ef4aa | 2995 | p2p_free_req_dev_types(p2p); |
b22128ef | 2996 | os_free(p2p->cfg->dev_name); |
b6e01800 JM |
2997 | os_free(p2p->cfg->manufacturer); |
2998 | os_free(p2p->cfg->model_name); | |
2999 | os_free(p2p->cfg->model_number); | |
3000 | os_free(p2p->cfg->serial_number); | |
21d996f7 | 3001 | os_free(p2p->cfg->pref_chan); |
b22128ef | 3002 | os_free(p2p->groups); |
3f048aa8 | 3003 | p2ps_prov_free(p2p); |
18708aad | 3004 | wpabuf_free(p2p->sd_resp); |
f95cac27 | 3005 | p2p_remove_wps_vendor_extensions(p2p); |
556b30da | 3006 | os_free(p2p->no_go_freq.range); |
c40a8918 | 3007 | p2p_service_flush_asp(p2p); |
ae9d45f3 | 3008 | |
b22128ef JM |
3009 | os_free(p2p); |
3010 | } | |
3011 | ||
3012 | ||
3013 | void p2p_flush(struct p2p_data *p2p) | |
3014 | { | |
3015 | struct p2p_device *dev, *prev; | |
a3de1676 AO |
3016 | |
3017 | p2p_ext_listen(p2p, 0, 0); | |
78db55b8 | 3018 | p2p_stop_find(p2p); |
b22128ef JM |
3019 | dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device, |
3020 | list) { | |
3021 | dl_list_del(&dev->list); | |
3022 | p2p_device_free(p2p, dev); | |
3023 | } | |
3024 | p2p_free_sd_queries(p2p); | |
85b563f7 | 3025 | p2p->ssid_set = 0; |
4cc0f909 AN |
3026 | p2ps_prov_free(p2p); |
3027 | p2p_reset_pending_pd(p2p); | |
3a7819f0 JM |
3028 | p2p->override_pref_op_class = 0; |
3029 | p2p->override_pref_channel = 0; | |
b22128ef JM |
3030 | } |
3031 | ||
3032 | ||
9d562b79 SS |
3033 | int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr) |
3034 | { | |
3035 | struct p2p_device *dev; | |
3036 | ||
3037 | dev = p2p_get_device(p2p, addr); | |
3038 | if (dev == NULL) | |
3039 | return -1; | |
3040 | ||
ed496f13 | 3041 | p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr)); |
9d562b79 | 3042 | |
0c6eee8b RR |
3043 | if (p2p->go_neg_peer == dev) { |
3044 | eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL); | |
9d562b79 | 3045 | p2p->go_neg_peer = NULL; |
0c6eee8b | 3046 | } |
9d562b79 SS |
3047 | |
3048 | dev->wps_method = WPS_NOT_READY; | |
ab9e3442 | 3049 | dev->oob_pw_id = 0; |
9d562b79 SS |
3050 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; |
3051 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; | |
3052 | ||
9d562b79 SS |
3053 | return 0; |
3054 | } | |
3055 | ||
3056 | ||
b22128ef JM |
3057 | int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name) |
3058 | { | |
3059 | os_free(p2p->cfg->dev_name); | |
3060 | if (dev_name) { | |
3061 | p2p->cfg->dev_name = os_strdup(dev_name); | |
3062 | if (p2p->cfg->dev_name == NULL) | |
3063 | return -1; | |
3064 | } else | |
3065 | p2p->cfg->dev_name = NULL; | |
3066 | return 0; | |
3067 | } | |
3068 | ||
3069 | ||
b6e01800 JM |
3070 | int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer) |
3071 | { | |
3072 | os_free(p2p->cfg->manufacturer); | |
3073 | p2p->cfg->manufacturer = NULL; | |
3074 | if (manufacturer) { | |
3075 | p2p->cfg->manufacturer = os_strdup(manufacturer); | |
3076 | if (p2p->cfg->manufacturer == NULL) | |
3077 | return -1; | |
3078 | } | |
3079 | ||
3080 | return 0; | |
3081 | } | |
3082 | ||
3083 | ||
3084 | int p2p_set_model_name(struct p2p_data *p2p, const char *model_name) | |
3085 | { | |
3086 | os_free(p2p->cfg->model_name); | |
3087 | p2p->cfg->model_name = NULL; | |
3088 | if (model_name) { | |
3089 | p2p->cfg->model_name = os_strdup(model_name); | |
3090 | if (p2p->cfg->model_name == NULL) | |
3091 | return -1; | |
3092 | } | |
3093 | ||
3094 | return 0; | |
3095 | } | |
3096 | ||
3097 | ||
3098 | int p2p_set_model_number(struct p2p_data *p2p, const char *model_number) | |
3099 | { | |
3100 | os_free(p2p->cfg->model_number); | |
3101 | p2p->cfg->model_number = NULL; | |
3102 | if (model_number) { | |
3103 | p2p->cfg->model_number = os_strdup(model_number); | |
3104 | if (p2p->cfg->model_number == NULL) | |
3105 | return -1; | |
3106 | } | |
3107 | ||
3108 | return 0; | |
3109 | } | |
3110 | ||
3111 | ||
3112 | int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number) | |
3113 | { | |
3114 | os_free(p2p->cfg->serial_number); | |
3115 | p2p->cfg->serial_number = NULL; | |
3116 | if (serial_number) { | |
3117 | p2p->cfg->serial_number = os_strdup(serial_number); | |
3118 | if (p2p->cfg->serial_number == NULL) | |
3119 | return -1; | |
3120 | } | |
3121 | ||
3122 | return 0; | |
3123 | } | |
3124 | ||
3125 | ||
3126 | void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods) | |
3127 | { | |
3128 | p2p->cfg->config_methods = config_methods; | |
3129 | } | |
3130 | ||
3131 | ||
3132 | void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid) | |
3133 | { | |
3134 | os_memcpy(p2p->cfg->uuid, uuid, 16); | |
3135 | } | |
3136 | ||
3137 | ||
b22128ef JM |
3138 | int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type) |
3139 | { | |
3140 | os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8); | |
3141 | return 0; | |
3142 | } | |
3143 | ||
3144 | ||
3145 | int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8], | |
3146 | size_t num_dev_types) | |
3147 | { | |
3148 | if (num_dev_types > P2P_SEC_DEVICE_TYPES) | |
3149 | num_dev_types = P2P_SEC_DEVICE_TYPES; | |
3150 | p2p->cfg->num_sec_dev_types = num_dev_types; | |
3151 | os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8); | |
3152 | return 0; | |
3153 | } | |
3154 | ||
3155 | ||
f95cac27 JMB |
3156 | void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p) |
3157 | { | |
3158 | int i; | |
3159 | ||
10c5d2a5 | 3160 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
f95cac27 JMB |
3161 | wpabuf_free(p2p->wps_vendor_ext[i]); |
3162 | p2p->wps_vendor_ext[i] = NULL; | |
3163 | } | |
3164 | } | |
3165 | ||
3166 | ||
3167 | int p2p_add_wps_vendor_extension(struct p2p_data *p2p, | |
3168 | const struct wpabuf *vendor_ext) | |
3169 | { | |
3170 | int i; | |
3171 | ||
3172 | if (vendor_ext == NULL) | |
3173 | return -1; | |
3174 | ||
10c5d2a5 | 3175 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
f95cac27 JMB |
3176 | if (p2p->wps_vendor_ext[i] == NULL) |
3177 | break; | |
3178 | } | |
10c5d2a5 | 3179 | if (i >= P2P_MAX_WPS_VENDOR_EXT) |
f95cac27 JMB |
3180 | return -1; |
3181 | ||
3182 | p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext); | |
3183 | if (p2p->wps_vendor_ext[i] == NULL) | |
3184 | return -1; | |
3185 | ||
3186 | return 0; | |
3187 | } | |
3188 | ||
3189 | ||
b22128ef JM |
3190 | int p2p_set_country(struct p2p_data *p2p, const char *country) |
3191 | { | |
3192 | os_memcpy(p2p->cfg->country, country, 3); | |
3193 | return 0; | |
3194 | } | |
3195 | ||
3196 | ||
e706b8c8 JM |
3197 | static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev) |
3198 | { | |
cba9ebfd JM |
3199 | int res; |
3200 | ||
e706b8c8 JM |
3201 | if (dev->sd_pending_bcast_queries == 0) { |
3202 | /* Initialize with total number of registered broadcast | |
3203 | * SD queries. */ | |
3204 | dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries; | |
3205 | } | |
3206 | ||
cba9ebfd JM |
3207 | res = p2p_start_sd(p2p, dev); |
3208 | if (res == -2) | |
3209 | return -2; | |
3210 | if (res == 0) | |
e706b8c8 JM |
3211 | return 1; |
3212 | ||
3213 | if (dev->req_config_methods && | |
3214 | !(dev->flags & P2P_DEV_PD_FOR_JOIN)) { | |
3215 | p2p_dbg(p2p, "Send pending Provision Discovery Request to " | |
3216 | MACSTR " (config methods 0x%x)", | |
3217 | MAC2STR(dev->info.p2p_device_addr), | |
3218 | dev->req_config_methods); | |
3219 | if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0) | |
3220 | return 1; | |
3221 | } | |
3222 | ||
3223 | return 0; | |
3224 | } | |
3225 | ||
3226 | ||
b22128ef JM |
3227 | void p2p_continue_find(struct p2p_data *p2p) |
3228 | { | |
3229 | struct p2p_device *dev; | |
cba9ebfd | 3230 | int found, res; |
e706b8c8 | 3231 | |
b22128ef | 3232 | p2p_set_state(p2p, P2P_SEARCH); |
e706b8c8 JM |
3233 | |
3234 | /* Continue from the device following the last iteration */ | |
3235 | found = 0; | |
b22128ef | 3236 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
e706b8c8 JM |
3237 | if (dev == p2p->last_p2p_find_oper) { |
3238 | found = 1; | |
3239 | continue; | |
3240 | } | |
3241 | if (!found) | |
3242 | continue; | |
cba9ebfd JM |
3243 | res = p2p_pre_find_operation(p2p, dev); |
3244 | if (res > 0) { | |
e706b8c8 JM |
3245 | p2p->last_p2p_find_oper = dev; |
3246 | return; | |
f667e031 | 3247 | } |
cba9ebfd JM |
3248 | if (res == -2) |
3249 | goto skip_sd; | |
e706b8c8 | 3250 | } |
f667e031 | 3251 | |
e706b8c8 JM |
3252 | /* |
3253 | * Wrap around to the beginning of the list and continue until the last | |
3254 | * iteration device. | |
3255 | */ | |
3256 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
cba9ebfd JM |
3257 | res = p2p_pre_find_operation(p2p, dev); |
3258 | if (res > 0) { | |
e706b8c8 | 3259 | p2p->last_p2p_find_oper = dev; |
f667e031 | 3260 | return; |
b22128ef | 3261 | } |
cba9ebfd JM |
3262 | if (res == -2) |
3263 | goto skip_sd; | |
e706b8c8 JM |
3264 | if (dev == p2p->last_p2p_find_oper) |
3265 | break; | |
b22128ef JM |
3266 | } |
3267 | ||
cba9ebfd JM |
3268 | skip_sd: |
3269 | os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN); | |
96beff11 | 3270 | p2p_listen_in_find(p2p, 1); |
b22128ef JM |
3271 | } |
3272 | ||
3273 | ||
3274 | static void p2p_sd_cb(struct p2p_data *p2p, int success) | |
3275 | { | |
ed496f13 | 3276 | p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d", |
b22128ef JM |
3277 | success); |
3278 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3279 | ||
3280 | if (!success) { | |
cba9ebfd JM |
3281 | if (p2p->sd_peer) { |
3282 | if (is_zero_ether_addr(p2p->sd_query_no_ack)) { | |
3283 | os_memcpy(p2p->sd_query_no_ack, | |
3284 | p2p->sd_peer->info.p2p_device_addr, | |
3285 | ETH_ALEN); | |
3286 | p2p_dbg(p2p, | |
3287 | "First SD Query no-ACK in this search iteration: " | |
3288 | MACSTR, MAC2STR(p2p->sd_query_no_ack)); | |
3289 | } | |
4db20751 | 3290 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
cba9ebfd | 3291 | } |
f667e031 | 3292 | p2p->sd_peer = NULL; |
2c0efd9e KV |
3293 | if (p2p->state != P2P_IDLE) |
3294 | p2p_continue_find(p2p); | |
b22128ef JM |
3295 | return; |
3296 | } | |
3297 | ||
3298 | if (p2p->sd_peer == NULL) { | |
ed496f13 | 3299 | p2p_dbg(p2p, "No SD peer entry known"); |
2c0efd9e KV |
3300 | if (p2p->state != P2P_IDLE) |
3301 | p2p_continue_find(p2p); | |
b22128ef JM |
3302 | return; |
3303 | } | |
3304 | ||
bff162ac | 3305 | if (p2p->sd_query && p2p->sd_query->for_all_peers) { |
7139cf4a CM |
3306 | /* Update the pending broadcast SD query count for this device |
3307 | */ | |
3308 | p2p->sd_peer->sd_pending_bcast_queries--; | |
3309 | ||
3310 | /* | |
3311 | * If there are no pending broadcast queries for this device, | |
3312 | * mark it as done (-1). | |
3313 | */ | |
3314 | if (p2p->sd_peer->sd_pending_bcast_queries == 0) | |
3315 | p2p->sd_peer->sd_pending_bcast_queries = -1; | |
3316 | } | |
3317 | ||
b22128ef JM |
3318 | /* Wait for response from the peer */ |
3319 | p2p_set_state(p2p, P2P_SD_DURING_FIND); | |
3320 | p2p_set_timeout(p2p, 0, 200000); | |
3321 | } | |
3322 | ||
6b56cc2d JS |
3323 | |
3324 | /** | |
3325 | * p2p_retry_pd - Retry any pending provision disc requests in IDLE state | |
3326 | * @p2p: P2P module context from p2p_init() | |
3327 | */ | |
19df9b07 | 3328 | static void p2p_retry_pd(struct p2p_data *p2p) |
6b56cc2d JS |
3329 | { |
3330 | struct p2p_device *dev; | |
3331 | ||
6b56cc2d JS |
3332 | /* |
3333 | * Retry the prov disc req attempt only for the peer that the user had | |
175171ac | 3334 | * requested. |
6b56cc2d JS |
3335 | */ |
3336 | ||
3337 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
3338 | if (os_memcmp(p2p->pending_pd_devaddr, | |
3339 | dev->info.p2p_device_addr, ETH_ALEN) != 0) | |
3340 | continue; | |
3341 | if (!dev->req_config_methods) | |
3342 | continue; | |
6b56cc2d | 3343 | |
ed496f13 | 3344 | p2p_dbg(p2p, "Send pending Provision Discovery Request to " |
6b56cc2d JS |
3345 | MACSTR " (config methods 0x%x)", |
3346 | MAC2STR(dev->info.p2p_device_addr), | |
3347 | dev->req_config_methods); | |
175171ac | 3348 | p2p_send_prov_disc_req(p2p, dev, |
8d82c210 JM |
3349 | dev->flags & P2P_DEV_PD_FOR_JOIN, |
3350 | p2p->pd_force_freq); | |
6b56cc2d JS |
3351 | return; |
3352 | } | |
3353 | } | |
3354 | ||
3355 | ||
b22128ef JM |
3356 | static void p2p_prov_disc_cb(struct p2p_data *p2p, int success) |
3357 | { | |
ed496f13 | 3358 | p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d", |
b22128ef | 3359 | success); |
6b56cc2d JS |
3360 | |
3361 | /* | |
3362 | * Postpone resetting the pending action state till after we actually | |
3363 | * time out. This allows us to take some action like notifying any | |
3364 | * interested parties about no response to the request. | |
3365 | * | |
3366 | * When the timer (below) goes off we check in IDLE, SEARCH, or | |
3367 | * LISTEN_ONLY state, which are the only allowed states to issue a PD | |
3368 | * requests in, if this was still pending and then raise notification. | |
3369 | */ | |
b22128ef JM |
3370 | |
3371 | if (!success) { | |
6b56cc2d JS |
3372 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
3373 | ||
488f4a71 JM |
3374 | if (p2p->user_initiated_pd && |
3375 | (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY)) | |
3376 | { | |
3377 | /* Retry request from timeout to avoid busy loops */ | |
3378 | p2p->pending_action_state = P2P_PENDING_PD; | |
3379 | p2p_set_timeout(p2p, 0, 50000); | |
3380 | } else if (p2p->state != P2P_IDLE) | |
b22128ef | 3381 | p2p_continue_find(p2p); |
6b56cc2d JS |
3382 | else if (p2p->user_initiated_pd) { |
3383 | p2p->pending_action_state = P2P_PENDING_PD; | |
3384 | p2p_set_timeout(p2p, 0, 300000); | |
3385 | } | |
b22128ef JM |
3386 | return; |
3387 | } | |
3388 | ||
f94e4c20 MS |
3389 | /* |
3390 | * If after PD Request the peer doesn't expect to receive PD Response | |
3391 | * the PD Request ACK indicates a completion of the current PD. This | |
3392 | * happens only on the advertiser side sending the follow-on PD Request | |
3393 | * with the status different than 12 (Success: accepted by user). | |
3394 | */ | |
3395 | if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker && | |
3396 | p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) { | |
3397 | p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK"); | |
3398 | ||
3399 | if (p2p->send_action_in_progress) { | |
3400 | p2p->send_action_in_progress = 0; | |
3401 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
3402 | } | |
3403 | ||
3404 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3405 | ||
3406 | if (p2p->cfg->p2ps_prov_complete) { | |
3407 | p2p->cfg->p2ps_prov_complete( | |
3408 | p2p->cfg->cb_ctx, | |
3409 | p2p->p2ps_prov->status, | |
3410 | p2p->p2ps_prov->adv_mac, | |
3411 | p2p->p2ps_prov->adv_mac, | |
3412 | p2p->p2ps_prov->session_mac, | |
3413 | NULL, p2p->p2ps_prov->adv_id, | |
3414 | p2p->p2ps_prov->session_id, | |
3415 | 0, 0, NULL, 0, 0, 0, | |
aa256cb3 | 3416 | NULL, NULL, 0, 0, NULL, 0); |
f94e4c20 MS |
3417 | } |
3418 | ||
3419 | if (p2p->user_initiated_pd) | |
3420 | p2p_reset_pending_pd(p2p); | |
3421 | ||
3422 | p2ps_prov_free(p2p); | |
3423 | return; | |
3424 | } | |
3425 | ||
6b56cc2d JS |
3426 | /* |
3427 | * This postponing, of resetting pending_action_state, needs to be | |
3428 | * done only for user initiated PD requests and not internal ones. | |
3429 | */ | |
3430 | if (p2p->user_initiated_pd) | |
3431 | p2p->pending_action_state = P2P_PENDING_PD; | |
3432 | else | |
3433 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3434 | ||
b22128ef JM |
3435 | /* Wait for response from the peer */ |
3436 | if (p2p->state == P2P_SEARCH) | |
3437 | p2p_set_state(p2p, P2P_PD_DURING_FIND); | |
3438 | p2p_set_timeout(p2p, 0, 200000); | |
3439 | } | |
3440 | ||
3441 | ||
9a58e521 KV |
3442 | static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success) |
3443 | { | |
3444 | p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d", | |
3445 | success); | |
3446 | ||
3447 | if (p2p->send_action_in_progress) { | |
3448 | p2p->send_action_in_progress = 0; | |
3449 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
3450 | } | |
3451 | ||
3452 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3453 | ||
3454 | if (!success) | |
683f8677 | 3455 | return; |
9a58e521 KV |
3456 | |
3457 | if (!p2p->cfg->prov_disc_resp_cb || | |
3458 | p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) | |
683f8677 | 3459 | return; |
9a58e521 KV |
3460 | |
3461 | p2p_dbg(p2p, | |
3462 | "Post-Provision Discovery operations started - do not try to continue other P2P operations"); | |
9a58e521 KV |
3463 | } |
3464 | ||
3465 | ||
b22128ef | 3466 | int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq, |
acb69cec | 3467 | struct os_reltime *rx_time, int level, const u8 *ies, |
3dfd0484 | 3468 | size_t ies_len) |
b22128ef | 3469 | { |
acb69cec | 3470 | if (os_reltime_before(rx_time, &p2p->find_start)) { |
a5b5e830 JM |
3471 | /* |
3472 | * The driver may have cached (e.g., in cfg80211 BSS table) the | |
3473 | * scan results for relatively long time. To avoid reporting | |
3474 | * stale information, update P2P peers only based on results | |
3475 | * that have based on frames received after the last p2p_find | |
3476 | * operation was started. | |
3477 | */ | |
ed496f13 | 3478 | p2p_dbg(p2p, "Ignore old scan result for " MACSTR |
6014e59c | 3479 | " (rx_time=%u.%06u find_start=%u.%06u)", |
a5b5e830 | 3480 | MAC2STR(bssid), (unsigned int) rx_time->sec, |
6014e59c JM |
3481 | (unsigned int) rx_time->usec, |
3482 | (unsigned int) p2p->find_start.sec, | |
3483 | (unsigned int) p2p->find_start.usec); | |
a5b5e830 JM |
3484 | return 0; |
3485 | } | |
3486 | ||
c5f10e80 | 3487 | p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1); |
b22128ef | 3488 | |
b22128ef JM |
3489 | return 0; |
3490 | } | |
3491 | ||
3492 | ||
3493 | void p2p_scan_res_handled(struct p2p_data *p2p) | |
3494 | { | |
3495 | if (!p2p->p2p_scan_running) { | |
ed496f13 | 3496 | p2p_dbg(p2p, "p2p_scan was not running, but scan results received"); |
b22128ef JM |
3497 | } |
3498 | p2p->p2p_scan_running = 0; | |
3499 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
3500 | ||
3501 | if (p2p_run_after_scan(p2p)) | |
3502 | return; | |
3503 | if (p2p->state == P2P_SEARCH) | |
3504 | p2p_continue_find(p2p); | |
3505 | } | |
3506 | ||
3507 | ||
c58eed6d LD |
3508 | void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id, |
3509 | unsigned int bands) | |
b22128ef | 3510 | { |
1a94b0ad | 3511 | u8 dev_capab; |
9675ce35 JM |
3512 | u8 *len; |
3513 | ||
3514 | #ifdef CONFIG_WIFI_DISPLAY | |
3515 | if (p2p->wfd_ie_probe_req) | |
3516 | wpabuf_put_buf(ies, p2p->wfd_ie_probe_req); | |
3517 | #endif /* CONFIG_WIFI_DISPLAY */ | |
3518 | ||
86bd36f0 JM |
3519 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]) |
3520 | wpabuf_put_buf(ies, | |
3521 | p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]); | |
3522 | ||
9675ce35 | 3523 | len = p2p_buf_add_ie_hdr(ies); |
1a94b0ad BG |
3524 | |
3525 | dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
3526 | ||
3527 | /* P2PS requires Probe Request frames to include SD bit */ | |
3528 | if (p2p->p2ps_seek && p2p->p2ps_seek_count) | |
3529 | dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY; | |
3530 | ||
3531 | p2p_buf_add_capability(ies, dev_capab, 0); | |
3532 | ||
6d92fa6e JM |
3533 | if (dev_id) |
3534 | p2p_buf_add_device_id(ies, dev_id); | |
b22128ef JM |
3535 | if (p2p->cfg->reg_class && p2p->cfg->channel) |
3536 | p2p_buf_add_listen_channel(ies, p2p->cfg->country, | |
3537 | p2p->cfg->reg_class, | |
3538 | p2p->cfg->channel); | |
3539 | if (p2p->ext_listen_interval) | |
3540 | p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period, | |
3541 | p2p->ext_listen_interval); | |
1a94b0ad | 3542 | |
c58eed6d LD |
3543 | if (bands & BAND_60_GHZ) |
3544 | p2p_buf_add_device_info(ies, p2p, NULL); | |
3545 | ||
1a94b0ad BG |
3546 | if (p2p->p2ps_seek && p2p->p2ps_seek_count) |
3547 | p2p_buf_add_service_hash(ies, p2p); | |
3548 | ||
b22128ef JM |
3549 | /* TODO: p2p_buf_add_operating_channel() if GO */ |
3550 | p2p_buf_update_ie_hdr(ies, len); | |
3551 | } | |
3552 | ||
3553 | ||
206e1f42 JM |
3554 | size_t p2p_scan_ie_buf_len(struct p2p_data *p2p) |
3555 | { | |
9675ce35 JM |
3556 | size_t len = 100; |
3557 | ||
3558 | #ifdef CONFIG_WIFI_DISPLAY | |
3559 | if (p2p && p2p->wfd_ie_probe_req) | |
3560 | len += wpabuf_len(p2p->wfd_ie_probe_req); | |
3561 | #endif /* CONFIG_WIFI_DISPLAY */ | |
3562 | ||
86bd36f0 JM |
3563 | if (p2p && p2p->vendor_elem && |
3564 | p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]) | |
3565 | len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]); | |
3566 | ||
9675ce35 | 3567 | return len; |
206e1f42 JM |
3568 | } |
3569 | ||
3570 | ||
b22128ef JM |
3571 | int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end) |
3572 | { | |
3573 | return p2p_attr_text(p2p_ie, buf, end); | |
3574 | } | |
3575 | ||
3576 | ||
3577 | static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success) | |
3578 | { | |
3579 | struct p2p_device *dev = p2p->go_neg_peer; | |
fb8984fd | 3580 | int timeout; |
b22128ef | 3581 | |
ed496f13 | 3582 | p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success); |
b22128ef JM |
3583 | |
3584 | if (dev == NULL) { | |
ed496f13 | 3585 | p2p_dbg(p2p, "No pending GO Negotiation"); |
b22128ef JM |
3586 | return; |
3587 | } | |
3588 | ||
3589 | if (success) { | |
b22128ef JM |
3590 | if (dev->flags & P2P_DEV_USER_REJECTED) { |
3591 | p2p_set_state(p2p, P2P_IDLE); | |
3592 | return; | |
3593 | } | |
a1d2ab32 NKG |
3594 | } else if (dev->go_neg_req_sent) { |
3595 | /* Cancel the increment from p2p_connect_send() on failure */ | |
3596 | dev->go_neg_req_sent--; | |
b22128ef JM |
3597 | } |
3598 | ||
3599 | if (!success && | |
c5db8e51 | 3600 | (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) && |
b22128ef | 3601 | !is_zero_ether_addr(dev->member_in_go_dev)) { |
ed496f13 | 3602 | p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO", |
c5db8e51 | 3603 | MAC2STR(dev->info.p2p_device_addr)); |
b22128ef JM |
3604 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
3605 | p2p_send_dev_disc_req(p2p, dev); | |
3606 | return; | |
3607 | } | |
3608 | ||
3609 | /* | |
3610 | * Use P2P find, if needed, to find the other device from its listen | |
3611 | * channel. | |
3612 | */ | |
3613 | p2p_set_state(p2p, P2P_CONNECT); | |
fb8984fd JM |
3614 | timeout = success ? 500000 : 100000; |
3615 | if (!success && p2p->go_neg_peer && | |
3616 | (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) { | |
3617 | unsigned int r; | |
3618 | /* | |
3619 | * Peer is expected to wait our response and we will skip the | |
3620 | * listen phase. Add some randomness to the wait time here to | |
3621 | * make it less likely to hit cases where we could end up in | |
3622 | * sync with peer not listening. | |
3623 | */ | |
df2508d7 JM |
3624 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) |
3625 | r = 0; | |
fb8984fd JM |
3626 | timeout += r % 100000; |
3627 | } | |
3628 | p2p_set_timeout(p2p, 0, timeout); | |
b22128ef JM |
3629 | } |
3630 | ||
3631 | ||
3632 | static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success) | |
3633 | { | |
ed496f13 | 3634 | p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d", |
b22128ef JM |
3635 | success); |
3636 | if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) { | |
ed496f13 | 3637 | p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore"); |
b22128ef JM |
3638 | return; |
3639 | } | |
3640 | p2p_set_state(p2p, P2P_CONNECT); | |
8e4839ce | 3641 | p2p_set_timeout(p2p, 0, 500000); |
b22128ef JM |
3642 | } |
3643 | ||
3644 | ||
7800d45c JM |
3645 | static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success, |
3646 | const u8 *addr) | |
b22128ef | 3647 | { |
ed496f13 | 3648 | p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success); |
fbe70272 | 3649 | if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) { |
8c00fd00 | 3650 | p2p_go_neg_failed(p2p, p2p->go_neg_peer->status); |
de7c06ee JM |
3651 | return; |
3652 | } | |
3653 | ||
3654 | if (success) { | |
7800d45c JM |
3655 | struct p2p_device *dev; |
3656 | dev = p2p_get_device(p2p, addr); | |
3657 | if (dev && | |
3658 | dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) | |
3659 | dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE; | |
fbe70272 | 3660 | } |
de7c06ee JM |
3661 | |
3662 | if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND) | |
3663 | p2p_continue_find(p2p); | |
b22128ef JM |
3664 | } |
3665 | ||
3666 | ||
93b7ddd0 JM |
3667 | static void p2p_go_neg_conf_cb(struct p2p_data *p2p, |
3668 | enum p2p_send_action_result result) | |
b22128ef JM |
3669 | { |
3670 | struct p2p_device *dev; | |
3671 | ||
ed496f13 | 3672 | p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result); |
93b7ddd0 | 3673 | if (result == P2P_SEND_ACTION_FAILED) { |
0e0e1e56 | 3674 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
8c00fd00 | 3675 | p2p_go_neg_failed(p2p, -1); |
93b7ddd0 JM |
3676 | return; |
3677 | } | |
0e0e1e56 NS |
3678 | |
3679 | dev = p2p->go_neg_peer; | |
3680 | ||
93b7ddd0 | 3681 | if (result == P2P_SEND_ACTION_NO_ACK) { |
0e0e1e56 NS |
3682 | /* |
3683 | * Retry GO Negotiation Confirmation | |
3684 | * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive | |
3685 | * ACK for confirmation. | |
3686 | */ | |
3687 | if (dev && dev->go_neg_conf && | |
3688 | dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) { | |
3689 | p2p_dbg(p2p, "GO Negotiation Confirm retry %d", | |
3690 | dev->go_neg_conf_sent); | |
3691 | p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM; | |
3692 | if (p2p_send_action(p2p, dev->go_neg_conf_freq, | |
3693 | dev->info.p2p_device_addr, | |
3694 | p2p->cfg->dev_addr, | |
3695 | dev->info.p2p_device_addr, | |
3696 | wpabuf_head(dev->go_neg_conf), | |
3697 | wpabuf_len(dev->go_neg_conf), 0) >= | |
3698 | 0) { | |
3699 | dev->go_neg_conf_sent++; | |
3700 | return; | |
3701 | } | |
3702 | p2p_dbg(p2p, "Failed to re-send Action frame"); | |
3703 | ||
3704 | /* | |
3705 | * Continue with the assumption that the first attempt | |
3706 | * went through and just the ACK frame was lost. | |
3707 | */ | |
3708 | } | |
3709 | ||
b22128ef JM |
3710 | /* |
3711 | * It looks like the TX status for GO Negotiation Confirm is | |
3712 | * often showing failure even when the peer has actually | |
3713 | * received the frame. Since the peer may change channels | |
3714 | * immediately after having received the frame, we may not see | |
3715 | * an Ack for retries, so just dropping a single frame may | |
3716 | * trigger this. To allow the group formation to succeed if the | |
3717 | * peer did indeed receive the frame, continue regardless of | |
3718 | * the TX status. | |
3719 | */ | |
ed496f13 | 3720 | p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported"); |
b22128ef JM |
3721 | } |
3722 | ||
0e0e1e56 NS |
3723 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
3724 | ||
b22128ef JM |
3725 | if (dev == NULL) |
3726 | return; | |
3727 | ||
3728 | p2p_go_complete(p2p, dev); | |
3729 | } | |
3730 | ||
3731 | ||
3732 | void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst, | |
93b7ddd0 JM |
3733 | const u8 *src, const u8 *bssid, |
3734 | enum p2p_send_action_result result) | |
b22128ef JM |
3735 | { |
3736 | enum p2p_pending_action_state state; | |
93b7ddd0 | 3737 | int success; |
b22128ef | 3738 | |
ed496f13 | 3739 | p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR |
c5e154c0 | 3740 | " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)", |
b22128ef | 3741 | p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src), |
c5e154c0 | 3742 | MAC2STR(bssid), result, p2p_state_txt(p2p->state)); |
93b7ddd0 | 3743 | success = result == P2P_SEND_ACTION_SUCCESS; |
b22128ef JM |
3744 | state = p2p->pending_action_state; |
3745 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3746 | switch (state) { | |
3747 | case P2P_NO_PENDING_ACTION: | |
c5c2d942 JM |
3748 | if (p2p->send_action_in_progress) { |
3749 | p2p->send_action_in_progress = 0; | |
3750 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
3751 | } | |
b22128ef JM |
3752 | break; |
3753 | case P2P_PENDING_GO_NEG_REQUEST: | |
3754 | p2p_go_neg_req_cb(p2p, success); | |
3755 | break; | |
3756 | case P2P_PENDING_GO_NEG_RESPONSE: | |
3757 | p2p_go_neg_resp_cb(p2p, success); | |
3758 | break; | |
3759 | case P2P_PENDING_GO_NEG_RESPONSE_FAILURE: | |
7800d45c | 3760 | p2p_go_neg_resp_failure_cb(p2p, success, dst); |
b22128ef JM |
3761 | break; |
3762 | case P2P_PENDING_GO_NEG_CONFIRM: | |
93b7ddd0 | 3763 | p2p_go_neg_conf_cb(p2p, result); |
b22128ef JM |
3764 | break; |
3765 | case P2P_PENDING_SD: | |
3766 | p2p_sd_cb(p2p, success); | |
3767 | break; | |
3768 | case P2P_PENDING_PD: | |
3769 | p2p_prov_disc_cb(p2p, success); | |
3770 | break; | |
9a58e521 KV |
3771 | case P2P_PENDING_PD_RESPONSE: |
3772 | p2p_prov_disc_resp_cb(p2p, success); | |
3773 | break; | |
b22128ef JM |
3774 | case P2P_PENDING_INVITATION_REQUEST: |
3775 | p2p_invitation_req_cb(p2p, success); | |
3776 | break; | |
3777 | case P2P_PENDING_INVITATION_RESPONSE: | |
3778 | p2p_invitation_resp_cb(p2p, success); | |
3779 | break; | |
3780 | case P2P_PENDING_DEV_DISC_REQUEST: | |
3781 | p2p_dev_disc_req_cb(p2p, success); | |
3782 | break; | |
3783 | case P2P_PENDING_DEV_DISC_RESPONSE: | |
3784 | p2p_dev_disc_resp_cb(p2p, success); | |
3785 | break; | |
3786 | case P2P_PENDING_GO_DISC_REQ: | |
3787 | p2p_go_disc_req_cb(p2p, success); | |
3788 | break; | |
3789 | } | |
3790 | } | |
3791 | ||
3792 | ||
3793 | void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq, | |
3794 | unsigned int duration) | |
3795 | { | |
3796 | if (freq == p2p->pending_client_disc_freq) { | |
ed496f13 | 3797 | p2p_dbg(p2p, "Client discoverability remain-awake completed"); |
b22128ef JM |
3798 | p2p->pending_client_disc_freq = 0; |
3799 | return; | |
3800 | } | |
3801 | ||
3802 | if (freq != p2p->pending_listen_freq) { | |
ed496f13 | 3803 | p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)", |
b22128ef JM |
3804 | freq, duration, p2p->pending_listen_freq); |
3805 | return; | |
3806 | } | |
3807 | ||
ed496f13 | 3808 | p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback", |
b22128ef JM |
3809 | p2p->pending_listen_sec, p2p->pending_listen_usec, |
3810 | p2p->pending_listen_freq); | |
3811 | p2p->in_listen = 1; | |
0b8889d8 | 3812 | p2p->drv_in_listen = freq; |
b22128ef JM |
3813 | if (p2p->pending_listen_sec || p2p->pending_listen_usec) { |
3814 | /* | |
3815 | * Add 20 msec extra wait to avoid race condition with driver | |
3816 | * remain-on-channel end event, i.e., give driver more time to | |
3817 | * complete the operation before our timeout expires. | |
3818 | */ | |
3819 | p2p_set_timeout(p2p, p2p->pending_listen_sec, | |
3820 | p2p->pending_listen_usec + 20000); | |
3821 | } | |
3822 | ||
3823 | p2p->pending_listen_freq = 0; | |
3824 | } | |
3825 | ||
3826 | ||
3827 | int p2p_listen_end(struct p2p_data *p2p, unsigned int freq) | |
3828 | { | |
ed496f13 | 3829 | p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq); |
b22128ef JM |
3830 | p2p->drv_in_listen = 0; |
3831 | if (p2p->in_listen) | |
3832 | return 0; /* Internal timeout will trigger the next step */ | |
3833 | ||
bb9808fa SD |
3834 | if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer && |
3835 | p2p->pending_listen_freq) { | |
3836 | /* | |
3837 | * Better wait a bit if the driver is unable to start | |
3838 | * offchannel operation for some reason to continue with | |
3839 | * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions. | |
3840 | */ | |
3841 | p2p_dbg(p2p, | |
3842 | "Listen operation did not seem to start - delay idle phase to avoid busy loop"); | |
3843 | p2p_set_timeout(p2p, 0, 100000); | |
3844 | return 1; | |
3845 | } | |
3846 | ||
b22128ef | 3847 | if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) { |
e24cf97c | 3848 | if (p2p->go_neg_peer->connect_reqs >= 120) { |
ed496f13 | 3849 | p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response"); |
8c00fd00 | 3850 | p2p_go_neg_failed(p2p, -1); |
e24cf97c JM |
3851 | return 0; |
3852 | } | |
3853 | ||
b22128ef JM |
3854 | p2p_set_state(p2p, P2P_CONNECT); |
3855 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
3856 | return 1; | |
3857 | } else if (p2p->state == P2P_SEARCH) { | |
59acfe87 JM |
3858 | if (p2p->p2p_scan_running) { |
3859 | /* | |
3860 | * Search is already in progress. This can happen if | |
3861 | * an Action frame RX is reported immediately after | |
3862 | * the end of a remain-on-channel operation and the | |
3863 | * response frame to that is sent using an offchannel | |
3864 | * operation while in p2p_find. Avoid an attempt to | |
3865 | * restart a scan here. | |
3866 | */ | |
ed496f13 | 3867 | p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one"); |
59acfe87 JM |
3868 | return 1; |
3869 | } | |
3fe8b68d JM |
3870 | if (p2p->pending_listen_freq) { |
3871 | /* | |
3872 | * Better wait a bit if the driver is unable to start | |
3873 | * offchannel operation for some reason. p2p_search() | |
3874 | * will be started from internal timeout. | |
3875 | */ | |
ed496f13 | 3876 | p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop"); |
3fe8b68d JM |
3877 | p2p_set_timeout(p2p, 0, 100000); |
3878 | return 1; | |
3879 | } | |
37448ede | 3880 | if (p2p->search_delay) { |
ed496f13 | 3881 | p2p_dbg(p2p, "Delay search operation by %u ms", |
37448ede JM |
3882 | p2p->search_delay); |
3883 | p2p_set_timeout(p2p, p2p->search_delay / 1000, | |
3884 | (p2p->search_delay % 1000) * 1000); | |
3885 | return 1; | |
3886 | } | |
b22128ef JM |
3887 | p2p_search(p2p); |
3888 | return 1; | |
3889 | } | |
3890 | ||
3891 | return 0; | |
3892 | } | |
3893 | ||
3894 | ||
3895 | static void p2p_timeout_connect(struct p2p_data *p2p) | |
3896 | { | |
3897 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
579a8098 JM |
3898 | if (p2p->go_neg_peer && |
3899 | (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) { | |
ed496f13 | 3900 | p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed"); |
8c00fd00 | 3901 | p2p_go_neg_failed(p2p, -1); |
579a8098 JM |
3902 | return; |
3903 | } | |
fb8984fd JM |
3904 | if (p2p->go_neg_peer && |
3905 | (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) && | |
3906 | p2p->go_neg_peer->connect_reqs < 120) { | |
ed496f13 | 3907 | p2p_dbg(p2p, "Peer expected to wait our response - skip listen"); |
fb8984fd JM |
3908 | p2p_connect_send(p2p, p2p->go_neg_peer); |
3909 | return; | |
3910 | } | |
fdd48ff6 JM |
3911 | if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) { |
3912 | p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)"); | |
3913 | p2p_set_state(p2p, P2P_CONNECT_LISTEN); | |
3914 | p2p_set_timeout(p2p, 0, 30000); | |
3915 | return; | |
3916 | } | |
b22128ef | 3917 | p2p_set_state(p2p, P2P_CONNECT_LISTEN); |
96beff11 | 3918 | p2p_listen_in_find(p2p, 0); |
b22128ef JM |
3919 | } |
3920 | ||
3921 | ||
3922 | static void p2p_timeout_connect_listen(struct p2p_data *p2p) | |
3923 | { | |
3924 | if (p2p->go_neg_peer) { | |
3925 | if (p2p->drv_in_listen) { | |
ed496f13 | 3926 | p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete"); |
b22128ef JM |
3927 | return; |
3928 | } | |
9dac8c3e FM |
3929 | |
3930 | if (p2p->go_neg_peer->connect_reqs >= 120) { | |
ed496f13 | 3931 | p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response"); |
8c00fd00 | 3932 | p2p_go_neg_failed(p2p, -1); |
9dac8c3e FM |
3933 | return; |
3934 | } | |
3935 | ||
b22128ef JM |
3936 | p2p_set_state(p2p, P2P_CONNECT); |
3937 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
3938 | } else | |
3939 | p2p_set_state(p2p, P2P_IDLE); | |
3940 | } | |
3941 | ||
3942 | ||
3943 | static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p) | |
3944 | { | |
b22128ef | 3945 | p2p_set_state(p2p, P2P_WAIT_PEER_IDLE); |
a2d63657 RR |
3946 | |
3947 | if (p2p->cfg->is_concurrent_session_active && | |
3948 | p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx)) | |
3949 | p2p_set_timeout(p2p, 0, 500000); | |
3950 | else | |
3951 | p2p_set_timeout(p2p, 0, 200000); | |
b22128ef JM |
3952 | } |
3953 | ||
3954 | ||
3955 | static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p) | |
3956 | { | |
3957 | struct p2p_device *dev = p2p->go_neg_peer; | |
3958 | ||
3959 | if (dev == NULL) { | |
ed496f13 | 3960 | p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait"); |
b22128ef JM |
3961 | return; |
3962 | } | |
3963 | ||
ed496f13 | 3964 | p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation"); |
b22128ef | 3965 | p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT); |
96beff11 | 3966 | p2p_listen_in_find(p2p, 0); |
b22128ef JM |
3967 | } |
3968 | ||
3969 | ||
3970 | static void p2p_timeout_sd_during_find(struct p2p_data *p2p) | |
3971 | { | |
ed496f13 | 3972 | p2p_dbg(p2p, "Service Discovery Query timeout"); |
b22128ef JM |
3973 | if (p2p->sd_peer) { |
3974 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
b22128ef JM |
3975 | p2p->sd_peer = NULL; |
3976 | } | |
3977 | p2p_continue_find(p2p); | |
3978 | } | |
3979 | ||
3980 | ||
3981 | static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p) | |
3982 | { | |
ed496f13 | 3983 | p2p_dbg(p2p, "Provision Discovery Request timeout"); |
b22128ef JM |
3984 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
3985 | p2p_continue_find(p2p); | |
3986 | } | |
3987 | ||
3988 | ||
6b56cc2d JS |
3989 | static void p2p_timeout_prov_disc_req(struct p2p_data *p2p) |
3990 | { | |
ab8ee776 KV |
3991 | u32 adv_id = 0; |
3992 | u8 *adv_mac = NULL; | |
3993 | ||
6b56cc2d JS |
3994 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
3995 | ||
3996 | /* | |
3997 | * For user initiated PD requests that we have not gotten any responses | |
3998 | * for while in IDLE state, we retry them a couple of times before | |
3999 | * giving up. | |
4000 | */ | |
4001 | if (!p2p->user_initiated_pd) | |
4002 | return; | |
4003 | ||
ed496f13 | 4004 | p2p_dbg(p2p, "User initiated Provision Discovery Request timeout"); |
6b56cc2d JS |
4005 | |
4006 | if (p2p->pd_retries) { | |
4007 | p2p->pd_retries--; | |
4008 | p2p_retry_pd(p2p); | |
4009 | } else { | |
175171ac JM |
4010 | struct p2p_device *dev; |
4011 | int for_join = 0; | |
4012 | ||
4013 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
4014 | if (os_memcmp(p2p->pending_pd_devaddr, | |
4015 | dev->info.p2p_device_addr, ETH_ALEN) != 0) | |
4016 | continue; | |
4017 | if (dev->req_config_methods && | |
4018 | (dev->flags & P2P_DEV_PD_FOR_JOIN)) | |
4019 | for_join = 1; | |
4020 | } | |
4021 | ||
ab8ee776 KV |
4022 | if (p2p->p2ps_prov) { |
4023 | adv_id = p2p->p2ps_prov->adv_id; | |
4024 | adv_mac = p2p->p2ps_prov->adv_mac; | |
4025 | } | |
4026 | ||
349b213c JS |
4027 | if (p2p->cfg->prov_disc_fail) |
4028 | p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, | |
4029 | p2p->pending_pd_devaddr, | |
175171ac JM |
4030 | for_join ? |
4031 | P2P_PROV_DISC_TIMEOUT_JOIN : | |
ab8ee776 KV |
4032 | P2P_PROV_DISC_TIMEOUT, |
4033 | adv_id, adv_mac, NULL); | |
6b56cc2d JS |
4034 | p2p_reset_pending_pd(p2p); |
4035 | } | |
4036 | } | |
4037 | ||
4038 | ||
b22128ef JM |
4039 | static void p2p_timeout_invite(struct p2p_data *p2p) |
4040 | { | |
4041 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
4042 | p2p_set_state(p2p, P2P_INVITE_LISTEN); | |
04d8dad5 JM |
4043 | if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) { |
4044 | /* | |
4045 | * Better remain on operating channel instead of listen channel | |
4046 | * when running a group. | |
4047 | */ | |
ed496f13 | 4048 | p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel"); |
04d8dad5 JM |
4049 | p2p_set_timeout(p2p, 0, 100000); |
4050 | return; | |
4051 | } | |
96beff11 | 4052 | p2p_listen_in_find(p2p, 0); |
b22128ef JM |
4053 | } |
4054 | ||
4055 | ||
4056 | static void p2p_timeout_invite_listen(struct p2p_data *p2p) | |
4057 | { | |
4058 | if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) { | |
4059 | p2p_set_state(p2p, P2P_INVITE); | |
4060 | p2p_invite_send(p2p, p2p->invite_peer, | |
8e9f53c3 | 4061 | p2p->invite_go_dev_addr, p2p->invite_dev_pw_id); |
b22128ef JM |
4062 | } else { |
4063 | if (p2p->invite_peer) { | |
ed496f13 | 4064 | p2p_dbg(p2p, "Invitation Request retry limit reached"); |
b22128ef JM |
4065 | if (p2p->cfg->invitation_result) |
4066 | p2p->cfg->invitation_result( | |
dbca75f8 | 4067 | p2p->cfg->cb_ctx, -1, NULL, NULL, |
dcd25d4c | 4068 | p2p->invite_peer->info.p2p_device_addr, |
41d5ce9e | 4069 | 0, 0); |
b22128ef JM |
4070 | } |
4071 | p2p_set_state(p2p, P2P_IDLE); | |
4072 | } | |
4073 | } | |
4074 | ||
4075 | ||
4076 | static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx) | |
4077 | { | |
4078 | struct p2p_data *p2p = eloop_ctx; | |
4079 | ||
ed496f13 | 4080 | p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state)); |
b22128ef JM |
4081 | |
4082 | p2p->in_listen = 0; | |
a902d5a6 CM |
4083 | if (p2p->drv_in_listen) { |
4084 | p2p_dbg(p2p, "Driver is still in listen state - stop it"); | |
4085 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); | |
4086 | } | |
b22128ef JM |
4087 | |
4088 | switch (p2p->state) { | |
4089 | case P2P_IDLE: | |
6b56cc2d JS |
4090 | /* Check if we timed out waiting for PD req */ |
4091 | if (p2p->pending_action_state == P2P_PENDING_PD) | |
4092 | p2p_timeout_prov_disc_req(p2p); | |
b22128ef JM |
4093 | break; |
4094 | case P2P_SEARCH: | |
6b56cc2d JS |
4095 | /* Check if we timed out waiting for PD req */ |
4096 | if (p2p->pending_action_state == P2P_PENDING_PD) | |
4097 | p2p_timeout_prov_disc_req(p2p); | |
37448ede | 4098 | if (p2p->search_delay && !p2p->in_search_delay) { |
ed496f13 | 4099 | p2p_dbg(p2p, "Delay search operation by %u ms", |
37448ede JM |
4100 | p2p->search_delay); |
4101 | p2p->in_search_delay = 1; | |
4102 | p2p_set_timeout(p2p, p2p->search_delay / 1000, | |
4103 | (p2p->search_delay % 1000) * 1000); | |
4104 | break; | |
4105 | } | |
4106 | p2p->in_search_delay = 0; | |
b22128ef JM |
4107 | p2p_search(p2p); |
4108 | break; | |
4109 | case P2P_CONNECT: | |
4110 | p2p_timeout_connect(p2p); | |
4111 | break; | |
4112 | case P2P_CONNECT_LISTEN: | |
4113 | p2p_timeout_connect_listen(p2p); | |
4114 | break; | |
4115 | case P2P_GO_NEG: | |
4116 | break; | |
4117 | case P2P_LISTEN_ONLY: | |
6b56cc2d JS |
4118 | /* Check if we timed out waiting for PD req */ |
4119 | if (p2p->pending_action_state == P2P_PENDING_PD) | |
4120 | p2p_timeout_prov_disc_req(p2p); | |
4121 | ||
b22128ef | 4122 | if (p2p->ext_listen_only) { |
ed496f13 | 4123 | p2p_dbg(p2p, "Extended Listen Timing - Listen State completed"); |
b22128ef JM |
4124 | p2p->ext_listen_only = 0; |
4125 | p2p_set_state(p2p, P2P_IDLE); | |
4126 | } | |
4127 | break; | |
4128 | case P2P_WAIT_PEER_CONNECT: | |
4129 | p2p_timeout_wait_peer_connect(p2p); | |
4130 | break; | |
4131 | case P2P_WAIT_PEER_IDLE: | |
4132 | p2p_timeout_wait_peer_idle(p2p); | |
4133 | break; | |
4134 | case P2P_SD_DURING_FIND: | |
4135 | p2p_timeout_sd_during_find(p2p); | |
4136 | break; | |
4137 | case P2P_PROVISIONING: | |
4138 | break; | |
4139 | case P2P_PD_DURING_FIND: | |
4140 | p2p_timeout_prov_disc_during_find(p2p); | |
4141 | break; | |
4142 | case P2P_INVITE: | |
4143 | p2p_timeout_invite(p2p); | |
4144 | break; | |
4145 | case P2P_INVITE_LISTEN: | |
4146 | p2p_timeout_invite_listen(p2p); | |
4147 | break; | |
4148 | } | |
4149 | } | |
4150 | ||
4151 | ||
4152 | int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr) | |
4153 | { | |
4154 | struct p2p_device *dev; | |
4155 | ||
4156 | dev = p2p_get_device(p2p, peer_addr); | |
ed496f13 JM |
4157 | p2p_dbg(p2p, "Local request to reject connection attempts by peer " |
4158 | MACSTR, MAC2STR(peer_addr)); | |
b22128ef | 4159 | if (dev == NULL) { |
ed496f13 | 4160 | p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr)); |
b22128ef JM |
4161 | return -1; |
4162 | } | |
4163 | dev->status = P2P_SC_FAIL_REJECTED_BY_USER; | |
4164 | dev->flags |= P2P_DEV_USER_REJECTED; | |
4165 | return 0; | |
4166 | } | |
4167 | ||
4168 | ||
e5a359cf | 4169 | const char * p2p_wps_method_text(enum p2p_wps_method method) |
b22128ef JM |
4170 | { |
4171 | switch (method) { | |
4172 | case WPS_NOT_READY: | |
4173 | return "not-ready"; | |
b22128ef JM |
4174 | case WPS_PIN_DISPLAY: |
4175 | return "Display"; | |
4176 | case WPS_PIN_KEYPAD: | |
4177 | return "Keypad"; | |
4178 | case WPS_PBC: | |
4179 | return "PBC"; | |
c00ab85f JM |
4180 | case WPS_NFC: |
4181 | return "NFC"; | |
4f88fc04 BG |
4182 | case WPS_P2PS: |
4183 | return "P2PS"; | |
b22128ef JM |
4184 | } |
4185 | ||
4186 | return "??"; | |
4187 | } | |
4188 | ||
4189 | ||
4190 | static const char * p2p_go_state_text(enum p2p_go_state go_state) | |
4191 | { | |
4192 | switch (go_state) { | |
4193 | case UNKNOWN_GO: | |
4194 | return "unknown"; | |
4195 | case LOCAL_GO: | |
4196 | return "local"; | |
4197 | case REMOTE_GO: | |
4198 | return "remote"; | |
4199 | } | |
4200 | ||
4201 | return "??"; | |
4202 | } | |
4203 | ||
4204 | ||
b3ffc80b JM |
4205 | const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p, |
4206 | const u8 *addr, int next) | |
b22128ef JM |
4207 | { |
4208 | struct p2p_device *dev; | |
b22128ef JM |
4209 | |
4210 | if (addr) | |
4211 | dev = p2p_get_device(p2p, addr); | |
4212 | else | |
4213 | dev = dl_list_first(&p2p->devices, struct p2p_device, list); | |
4214 | ||
4215 | if (dev && next) { | |
4216 | dev = dl_list_first(&dev->list, struct p2p_device, list); | |
4217 | if (&dev->list == &p2p->devices) | |
4218 | dev = NULL; | |
4219 | } | |
4220 | ||
4221 | if (dev == NULL) | |
b3ffc80b JM |
4222 | return NULL; |
4223 | ||
4224 | return &dev->info; | |
4225 | } | |
4226 | ||
4227 | ||
4228 | int p2p_get_peer_info_txt(const struct p2p_peer_info *info, | |
4229 | char *buf, size_t buflen) | |
4230 | { | |
4231 | struct p2p_device *dev; | |
4232 | int res; | |
4233 | char *pos, *end; | |
acb69cec | 4234 | struct os_reltime now; |
b3ffc80b JM |
4235 | |
4236 | if (info == NULL) | |
b22128ef JM |
4237 | return -1; |
4238 | ||
b3ffc80b JM |
4239 | dev = (struct p2p_device *) (((u8 *) info) - |
4240 | offsetof(struct p2p_device, info)); | |
4241 | ||
b22128ef JM |
4242 | pos = buf; |
4243 | end = buf + buflen; | |
4244 | ||
acb69cec | 4245 | os_get_reltime(&now); |
b22128ef JM |
4246 | res = os_snprintf(pos, end - pos, |
4247 | "age=%d\n" | |
4248 | "listen_freq=%d\n" | |
b22128ef JM |
4249 | "wps_method=%s\n" |
4250 | "interface_addr=" MACSTR "\n" | |
4251 | "member_in_go_dev=" MACSTR "\n" | |
4252 | "member_in_go_iface=" MACSTR "\n" | |
b22128ef JM |
4253 | "go_neg_req_sent=%d\n" |
4254 | "go_state=%s\n" | |
4255 | "dialog_token=%u\n" | |
4256 | "intended_addr=" MACSTR "\n" | |
4257 | "country=%c%c\n" | |
4258 | "oper_freq=%d\n" | |
4259 | "req_config_methods=0x%x\n" | |
8f524099 | 4260 | "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n" |
b22128ef | 4261 | "status=%d\n" |
b22128ef JM |
4262 | "invitation_reqs=%u\n", |
4263 | (int) (now.sec - dev->last_seen.sec), | |
4264 | dev->listen_freq, | |
b22128ef JM |
4265 | p2p_wps_method_text(dev->wps_method), |
4266 | MAC2STR(dev->interface_addr), | |
4267 | MAC2STR(dev->member_in_go_dev), | |
4268 | MAC2STR(dev->member_in_go_iface), | |
b22128ef JM |
4269 | dev->go_neg_req_sent, |
4270 | p2p_go_state_text(dev->go_state), | |
4271 | dev->dialog_token, | |
4272 | MAC2STR(dev->intended_addr), | |
4273 | dev->country[0] ? dev->country[0] : '_', | |
4274 | dev->country[1] ? dev->country[1] : '_', | |
4275 | dev->oper_freq, | |
4276 | dev->req_config_methods, | |
4277 | dev->flags & P2P_DEV_PROBE_REQ_ONLY ? | |
4278 | "[PROBE_REQ_ONLY]" : "", | |
4279 | dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "", | |
4280 | dev->flags & P2P_DEV_NOT_YET_READY ? | |
4281 | "[NOT_YET_READY]" : "", | |
b22128ef JM |
4282 | dev->flags & P2P_DEV_PD_PEER_DISPLAY ? |
4283 | "[PD_PEER_DISPLAY]" : "", | |
4284 | dev->flags & P2P_DEV_PD_PEER_KEYPAD ? | |
4285 | "[PD_PEER_KEYPAD]" : "", | |
1300cc8e KV |
4286 | dev->flags & P2P_DEV_PD_PEER_P2PS ? |
4287 | "[PD_PEER_P2PS]" : "", | |
b22128ef JM |
4288 | dev->flags & P2P_DEV_USER_REJECTED ? |
4289 | "[USER_REJECTED]" : "", | |
4290 | dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ? | |
4291 | "[PEER_WAITING_RESPONSE]" : "", | |
4292 | dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ? | |
4293 | "[PREFER_PERSISTENT_GROUP]" : "", | |
4294 | dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ? | |
4295 | "[WAIT_GO_NEG_RESPONSE]" : "", | |
4296 | dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ? | |
4297 | "[WAIT_GO_NEG_CONFIRM]" : "", | |
4298 | dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ? | |
4299 | "[GROUP_CLIENT_ONLY]" : "", | |
d5b20a73 | 4300 | dev->flags & P2P_DEV_FORCE_FREQ ? |
10c4edde JM |
4301 | "[FORCE_FREQ]" : "", |
4302 | dev->flags & P2P_DEV_PD_FOR_JOIN ? | |
4303 | "[PD_FOR_JOIN]" : "", | |
8f524099 AO |
4304 | dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ? |
4305 | "[LAST_SEEN_AS_GROUP_CLIENT]" : "", | |
b22128ef | 4306 | dev->status, |
b22128ef | 4307 | dev->invitation_reqs); |
d85e1fc8 | 4308 | if (os_snprintf_error(end - pos, res)) |
b22128ef JM |
4309 | return pos - buf; |
4310 | pos += res; | |
4311 | ||
4312 | if (dev->ext_listen_period) { | |
4313 | res = os_snprintf(pos, end - pos, | |
4314 | "ext_listen_period=%u\n" | |
4315 | "ext_listen_interval=%u\n", | |
4316 | dev->ext_listen_period, | |
4317 | dev->ext_listen_interval); | |
d85e1fc8 | 4318 | if (os_snprintf_error(end - pos, res)) |
b22128ef JM |
4319 | return pos - buf; |
4320 | pos += res; | |
4321 | } | |
4322 | ||
4323 | if (dev->oper_ssid_len) { | |
4324 | res = os_snprintf(pos, end - pos, | |
4325 | "oper_ssid=%s\n", | |
4326 | wpa_ssid_txt(dev->oper_ssid, | |
4327 | dev->oper_ssid_len)); | |
d85e1fc8 | 4328 | if (os_snprintf_error(end - pos, res)) |
b22128ef JM |
4329 | return pos - buf; |
4330 | pos += res; | |
4331 | } | |
4332 | ||
9675ce35 JM |
4333 | #ifdef CONFIG_WIFI_DISPLAY |
4334 | if (dev->info.wfd_subelems) { | |
4335 | res = os_snprintf(pos, end - pos, "wfd_subelems="); | |
d85e1fc8 | 4336 | if (os_snprintf_error(end - pos, res)) |
9675ce35 JM |
4337 | return pos - buf; |
4338 | pos += res; | |
4339 | ||
4340 | pos += wpa_snprintf_hex(pos, end - pos, | |
4341 | wpabuf_head(dev->info.wfd_subelems), | |
4342 | wpabuf_len(dev->info.wfd_subelems)); | |
4343 | ||
4344 | res = os_snprintf(pos, end - pos, "\n"); | |
d85e1fc8 | 4345 | if (os_snprintf_error(end - pos, res)) |
9675ce35 JM |
4346 | return pos - buf; |
4347 | pos += res; | |
4348 | } | |
4349 | #endif /* CONFIG_WIFI_DISPLAY */ | |
4350 | ||
b22128ef JM |
4351 | return pos - buf; |
4352 | } | |
4353 | ||
4354 | ||
b3bcc0f5 JM |
4355 | int p2p_peer_known(struct p2p_data *p2p, const u8 *addr) |
4356 | { | |
4357 | return p2p_get_device(p2p, addr) != NULL; | |
4358 | } | |
4359 | ||
4360 | ||
b22128ef JM |
4361 | void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled) |
4362 | { | |
4363 | if (enabled) { | |
ed496f13 | 4364 | p2p_dbg(p2p, "Client discoverability enabled"); |
b22128ef JM |
4365 | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
4366 | } else { | |
ed496f13 | 4367 | p2p_dbg(p2p, "Client discoverability disabled"); |
b22128ef JM |
4368 | p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
4369 | } | |
4370 | } | |
4371 | ||
4372 | ||
4373 | static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1, | |
4374 | u32 duration2, u32 interval2) | |
4375 | { | |
4376 | struct wpabuf *req; | |
4377 | struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL; | |
4378 | u8 *len; | |
4379 | ||
4380 | req = wpabuf_alloc(100); | |
4381 | if (req == NULL) | |
4382 | return NULL; | |
4383 | ||
4384 | if (duration1 || interval1) { | |
4385 | os_memset(&desc1, 0, sizeof(desc1)); | |
4386 | desc1.count_type = 1; | |
4387 | desc1.duration = duration1; | |
4388 | desc1.interval = interval1; | |
4389 | ptr1 = &desc1; | |
4390 | ||
4391 | if (duration2 || interval2) { | |
4392 | os_memset(&desc2, 0, sizeof(desc2)); | |
4393 | desc2.count_type = 2; | |
4394 | desc2.duration = duration2; | |
4395 | desc2.interval = interval2; | |
4396 | ptr2 = &desc2; | |
4397 | } | |
4398 | } | |
4399 | ||
4400 | p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1); | |
4401 | len = p2p_buf_add_ie_hdr(req); | |
4402 | p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2); | |
4403 | p2p_buf_update_ie_hdr(req, len); | |
4404 | ||
4405 | return req; | |
4406 | } | |
4407 | ||
4408 | ||
4409 | int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr, | |
4410 | const u8 *own_interface_addr, unsigned int freq, | |
4411 | u32 duration1, u32 interval1, u32 duration2, | |
4412 | u32 interval2) | |
4413 | { | |
4414 | struct wpabuf *req; | |
4415 | ||
ed496f13 JM |
4416 | p2p_dbg(p2p, "Send Presence Request to GO " MACSTR |
4417 | " (own interface " MACSTR ") freq=%u dur1=%u int1=%u " | |
4418 | "dur2=%u int2=%u", | |
b22128ef JM |
4419 | MAC2STR(go_interface_addr), MAC2STR(own_interface_addr), |
4420 | freq, duration1, interval1, duration2, interval2); | |
4421 | ||
4422 | req = p2p_build_presence_req(duration1, interval1, duration2, | |
4423 | interval2); | |
4424 | if (req == NULL) | |
4425 | return -1; | |
4426 | ||
4427 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3f9285ff JM |
4428 | if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr, |
4429 | go_interface_addr, | |
4430 | wpabuf_head(req), wpabuf_len(req), 200) < 0) { | |
ed496f13 | 4431 | p2p_dbg(p2p, "Failed to send Action frame"); |
b22128ef JM |
4432 | } |
4433 | wpabuf_free(req); | |
4434 | ||
4435 | return 0; | |
4436 | } | |
4437 | ||
4438 | ||
4439 | static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa, | |
4440 | size_t noa_len, u8 dialog_token) | |
4441 | { | |
4442 | struct wpabuf *resp; | |
4443 | u8 *len; | |
4444 | ||
4445 | resp = wpabuf_alloc(100 + noa_len); | |
4446 | if (resp == NULL) | |
4447 | return NULL; | |
4448 | ||
4449 | p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token); | |
4450 | len = p2p_buf_add_ie_hdr(resp); | |
4451 | p2p_buf_add_status(resp, status); | |
4452 | if (noa) { | |
4453 | wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE); | |
4454 | wpabuf_put_le16(resp, noa_len); | |
4455 | wpabuf_put_data(resp, noa, noa_len); | |
4456 | } else | |
4457 | p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL); | |
4458 | p2p_buf_update_ie_hdr(resp, len); | |
4459 | ||
4460 | return resp; | |
4461 | } | |
4462 | ||
4463 | ||
4464 | static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, | |
4465 | const u8 *sa, const u8 *data, size_t len, | |
4466 | int rx_freq) | |
4467 | { | |
4468 | struct p2p_message msg; | |
4469 | u8 status; | |
4470 | struct wpabuf *resp; | |
4471 | size_t g; | |
4472 | struct p2p_group *group = NULL; | |
4473 | int parsed = 0; | |
4474 | u8 noa[50]; | |
4475 | int noa_len; | |
4476 | ||
ed496f13 | 4477 | p2p_dbg(p2p, "Received P2P Action - P2P Presence Request"); |
b22128ef JM |
4478 | |
4479 | for (g = 0; g < p2p->num_groups; g++) { | |
4480 | if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]), | |
4481 | ETH_ALEN) == 0) { | |
4482 | group = p2p->groups[g]; | |
4483 | break; | |
4484 | } | |
4485 | } | |
4486 | if (group == NULL) { | |
ed496f13 | 4487 | p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group " |
b22128ef JM |
4488 | MACSTR, MAC2STR(da)); |
4489 | return; | |
4490 | } | |
4491 | ||
4492 | if (p2p_parse(data, len, &msg) < 0) { | |
ed496f13 | 4493 | p2p_dbg(p2p, "Failed to parse P2P Presence Request"); |
b22128ef JM |
4494 | status = P2P_SC_FAIL_INVALID_PARAMS; |
4495 | goto fail; | |
4496 | } | |
4497 | parsed = 1; | |
4498 | ||
4499 | if (msg.noa == NULL) { | |
ed496f13 | 4500 | p2p_dbg(p2p, "No NoA attribute in P2P Presence Request"); |
b22128ef JM |
4501 | status = P2P_SC_FAIL_INVALID_PARAMS; |
4502 | goto fail; | |
4503 | } | |
4504 | ||
4505 | status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len); | |
4506 | ||
4507 | fail: | |
4508 | if (p2p->cfg->get_noa) | |
4509 | noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa, | |
4510 | sizeof(noa)); | |
4511 | else | |
4512 | noa_len = -1; | |
4513 | resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL, | |
4514 | noa_len > 0 ? noa_len : 0, | |
4515 | msg.dialog_token); | |
4516 | if (parsed) | |
4517 | p2p_parse_free(&msg); | |
4518 | if (resp == NULL) | |
4519 | return; | |
4520 | ||
4521 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3f9285ff JM |
4522 | if (p2p_send_action(p2p, rx_freq, sa, da, da, |
4523 | wpabuf_head(resp), wpabuf_len(resp), 200) < 0) { | |
ed496f13 | 4524 | p2p_dbg(p2p, "Failed to send Action frame"); |
b22128ef JM |
4525 | } |
4526 | wpabuf_free(resp); | |
4527 | } | |
4528 | ||
4529 | ||
4530 | static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, | |
4531 | const u8 *sa, const u8 *data, size_t len) | |
4532 | { | |
4533 | struct p2p_message msg; | |
4534 | ||
ed496f13 | 4535 | p2p_dbg(p2p, "Received P2P Action - P2P Presence Response"); |
b22128ef JM |
4536 | |
4537 | if (p2p_parse(data, len, &msg) < 0) { | |
ed496f13 | 4538 | p2p_dbg(p2p, "Failed to parse P2P Presence Response"); |
b22128ef JM |
4539 | return; |
4540 | } | |
4541 | ||
4542 | if (msg.status == NULL || msg.noa == NULL) { | |
ed496f13 | 4543 | p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response"); |
b22128ef JM |
4544 | p2p_parse_free(&msg); |
4545 | return; | |
4546 | } | |
4547 | ||
c64e3a08 JM |
4548 | if (p2p->cfg->presence_resp) { |
4549 | p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status, | |
4550 | msg.noa, msg.noa_len); | |
4551 | } | |
4552 | ||
b22128ef | 4553 | if (*msg.status) { |
ed496f13 | 4554 | p2p_dbg(p2p, "P2P Presence Request was rejected: status %u", |
b22128ef JM |
4555 | *msg.status); |
4556 | p2p_parse_free(&msg); | |
4557 | return; | |
4558 | } | |
4559 | ||
ed496f13 | 4560 | p2p_dbg(p2p, "P2P Presence Request was accepted"); |
b22128ef JM |
4561 | wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA", |
4562 | msg.noa, msg.noa_len); | |
4563 | /* TODO: process NoA */ | |
4564 | p2p_parse_free(&msg); | |
4565 | } | |
4566 | ||
4567 | ||
4568 | static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx) | |
4569 | { | |
4570 | struct p2p_data *p2p = eloop_ctx; | |
4571 | ||
4572 | if (p2p->ext_listen_interval) { | |
4573 | /* Schedule next extended listen timeout */ | |
4574 | eloop_register_timeout(p2p->ext_listen_interval_sec, | |
4575 | p2p->ext_listen_interval_usec, | |
4576 | p2p_ext_listen_timeout, p2p, NULL); | |
4577 | } | |
4578 | ||
7e68be38 JM |
4579 | if ((p2p->cfg->is_p2p_in_progress && |
4580 | p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) || | |
4581 | (p2p->pending_action_state == P2P_PENDING_PD && | |
4582 | p2p->pd_retries > 0)) { | |
0f1034e3 SD |
4583 | p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)", |
4584 | p2p_state_txt(p2p->state)); | |
4585 | return; | |
4586 | } | |
4587 | ||
f7a69057 JM |
4588 | if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) { |
4589 | /* | |
4590 | * This should not really happen, but it looks like the Listen | |
4591 | * command may fail is something else (e.g., a scan) was | |
4592 | * running at an inconvenient time. As a workaround, allow new | |
4593 | * Extended Listen operation to be started. | |
4594 | */ | |
ed496f13 | 4595 | p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again"); |
f7a69057 JM |
4596 | p2p->ext_listen_only = 0; |
4597 | p2p_set_state(p2p, P2P_IDLE); | |
4598 | } | |
4599 | ||
b22128ef | 4600 | if (p2p->state != P2P_IDLE) { |
ed496f13 | 4601 | p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state)); |
b22128ef JM |
4602 | return; |
4603 | } | |
4604 | ||
ed496f13 | 4605 | p2p_dbg(p2p, "Extended Listen timeout"); |
b22128ef JM |
4606 | p2p->ext_listen_only = 1; |
4607 | if (p2p_listen(p2p, p2p->ext_listen_period) < 0) { | |
ed496f13 | 4608 | p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing"); |
b22128ef JM |
4609 | p2p->ext_listen_only = 0; |
4610 | } | |
4611 | } | |
4612 | ||
4613 | ||
4614 | int p2p_ext_listen(struct p2p_data *p2p, unsigned int period, | |
4615 | unsigned int interval) | |
4616 | { | |
4617 | if (period > 65535 || interval > 65535 || period > interval || | |
4618 | (period == 0 && interval > 0) || (period > 0 && interval == 0)) { | |
ed496f13 JM |
4619 | p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u", |
4620 | period, interval); | |
b22128ef JM |
4621 | return -1; |
4622 | } | |
4623 | ||
4624 | eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); | |
4625 | ||
4626 | if (interval == 0) { | |
ed496f13 | 4627 | p2p_dbg(p2p, "Disabling Extended Listen Timing"); |
b22128ef JM |
4628 | p2p->ext_listen_period = 0; |
4629 | p2p->ext_listen_interval = 0; | |
4630 | return 0; | |
4631 | } | |
4632 | ||
ed496f13 JM |
4633 | p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec", |
4634 | period, interval); | |
b22128ef JM |
4635 | p2p->ext_listen_period = period; |
4636 | p2p->ext_listen_interval = interval; | |
4637 | p2p->ext_listen_interval_sec = interval / 1000; | |
4638 | p2p->ext_listen_interval_usec = (interval % 1000) * 1000; | |
4639 | ||
4640 | eloop_register_timeout(p2p->ext_listen_interval_sec, | |
4641 | p2p->ext_listen_interval_usec, | |
4642 | p2p_ext_listen_timeout, p2p, NULL); | |
4643 | ||
4644 | return 0; | |
4645 | } | |
4646 | ||
4647 | ||
4648 | void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, | |
4649 | const u8 *ie, size_t ie_len) | |
4650 | { | |
4651 | struct p2p_message msg; | |
4652 | ||
4653 | if (bssid == NULL || ie == NULL) | |
4654 | return; | |
4655 | ||
4656 | os_memset(&msg, 0, sizeof(msg)); | |
4657 | if (p2p_parse_ies(ie, ie_len, &msg)) | |
4658 | return; | |
04c366cb EL |
4659 | if (msg.minor_reason_code == NULL) { |
4660 | p2p_parse_free(&msg); | |
b22128ef | 4661 | return; |
04c366cb | 4662 | } |
b22128ef | 4663 | |
ed496f13 | 4664 | p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR |
b22128ef JM |
4665 | " reason_code=%u minor_reason_code=%u", |
4666 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); | |
4667 | ||
4668 | p2p_parse_free(&msg); | |
4669 | } | |
4670 | ||
4671 | ||
4672 | void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, | |
4673 | const u8 *ie, size_t ie_len) | |
4674 | { | |
4675 | struct p2p_message msg; | |
4676 | ||
4677 | if (bssid == NULL || ie == NULL) | |
4678 | return; | |
4679 | ||
4680 | os_memset(&msg, 0, sizeof(msg)); | |
4681 | if (p2p_parse_ies(ie, ie_len, &msg)) | |
4682 | return; | |
04c366cb EL |
4683 | if (msg.minor_reason_code == NULL) { |
4684 | p2p_parse_free(&msg); | |
b22128ef | 4685 | return; |
04c366cb | 4686 | } |
b22128ef | 4687 | |
ed496f13 | 4688 | p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR |
b22128ef JM |
4689 | " reason_code=%u minor_reason_code=%u", |
4690 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); | |
4691 | ||
4692 | p2p_parse_free(&msg); | |
4693 | } | |
4694 | ||
4695 | ||
4696 | void p2p_set_managed_oper(struct p2p_data *p2p, int enabled) | |
4697 | { | |
4698 | if (enabled) { | |
ed496f13 | 4699 | p2p_dbg(p2p, "Managed P2P Device operations enabled"); |
b22128ef JM |
4700 | p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED; |
4701 | } else { | |
ed496f13 | 4702 | p2p_dbg(p2p, "Managed P2P Device operations disabled"); |
b22128ef JM |
4703 | p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED; |
4704 | } | |
4705 | } | |
4706 | ||
4707 | ||
1595eb93 | 4708 | int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class, |
59fa2053 PK |
4709 | u8 *op_channel, |
4710 | struct wpa_freq_range_list *avoid_list, | |
4711 | struct wpa_freq_range_list *disallow_list) | |
1595eb93 | 4712 | { |
59fa2053 PK |
4713 | return p2p_channel_random_social(&p2p->channels, op_class, op_channel, |
4714 | avoid_list, disallow_list); | |
1595eb93 AM |
4715 | } |
4716 | ||
4717 | ||
e3bd6e9d IP |
4718 | int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel, |
4719 | u8 forced) | |
b22128ef | 4720 | { |
9ccd9165 | 4721 | if (p2p_channel_to_freq(reg_class, channel) < 0) |
b22128ef JM |
4722 | return -1; |
4723 | ||
e3bd6e9d IP |
4724 | /* |
4725 | * Listen channel was set in configuration or set by control interface; | |
4726 | * cannot override it. | |
4727 | */ | |
abe96d06 JM |
4728 | if (p2p->cfg->channel_forced && forced == 0) { |
4729 | p2p_dbg(p2p, | |
4730 | "Listen channel was previously configured - do not override based on optimization"); | |
e3bd6e9d | 4731 | return -1; |
abe96d06 JM |
4732 | } |
4733 | ||
4734 | p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u", | |
4735 | reg_class, channel); | |
e3bd6e9d IP |
4736 | |
4737 | if (p2p->state == P2P_IDLE) { | |
4738 | p2p->cfg->reg_class = reg_class; | |
4739 | p2p->cfg->channel = channel; | |
4740 | p2p->cfg->channel_forced = forced; | |
4741 | } else { | |
4742 | p2p_dbg(p2p, "Defer setting listen channel"); | |
4743 | p2p->pending_reg_class = reg_class; | |
4744 | p2p->pending_channel = channel; | |
4745 | p2p->pending_channel_forced = forced; | |
4746 | } | |
b22128ef JM |
4747 | |
4748 | return 0; | |
4749 | } | |
4750 | ||
4751 | ||
e3bd6e9d IP |
4752 | u8 p2p_get_listen_channel(struct p2p_data *p2p) |
4753 | { | |
4754 | return p2p->cfg->channel; | |
4755 | } | |
4756 | ||
4757 | ||
b22128ef JM |
4758 | int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len) |
4759 | { | |
ed496f13 | 4760 | p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len)); |
b22128ef JM |
4761 | if (postfix == NULL) { |
4762 | p2p->cfg->ssid_postfix_len = 0; | |
4763 | return 0; | |
4764 | } | |
4765 | if (len > sizeof(p2p->cfg->ssid_postfix)) | |
4766 | return -1; | |
4767 | os_memcpy(p2p->cfg->ssid_postfix, postfix, len); | |
4768 | p2p->cfg->ssid_postfix_len = len; | |
4769 | return 0; | |
4770 | } | |
4771 | ||
4772 | ||
2463ba70 JS |
4773 | int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel, |
4774 | int cfg_op_channel) | |
4775 | { | |
9ccd9165 | 4776 | if (p2p_channel_to_freq(op_reg_class, op_channel) < 0) |
2463ba70 JS |
4777 | return -1; |
4778 | ||
ed496f13 JM |
4779 | p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u", |
4780 | op_reg_class, op_channel); | |
2463ba70 JS |
4781 | p2p->cfg->op_reg_class = op_reg_class; |
4782 | p2p->cfg->op_channel = op_channel; | |
4783 | p2p->cfg->cfg_op_channel = cfg_op_channel; | |
4784 | return 0; | |
4785 | } | |
4786 | ||
4787 | ||
21d996f7 JM |
4788 | int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan, |
4789 | const struct p2p_channel *pref_chan) | |
4790 | { | |
4791 | struct p2p_channel *n; | |
4792 | ||
4793 | if (pref_chan) { | |
a1f11e34 JB |
4794 | n = os_memdup(pref_chan, |
4795 | num_pref_chan * sizeof(struct p2p_channel)); | |
21d996f7 JM |
4796 | if (n == NULL) |
4797 | return -1; | |
21d996f7 JM |
4798 | } else |
4799 | n = NULL; | |
4800 | ||
4801 | os_free(p2p->cfg->pref_chan); | |
4802 | p2p->cfg->pref_chan = n; | |
4803 | p2p->cfg->num_pref_chan = num_pref_chan; | |
4804 | ||
4805 | return 0; | |
4806 | } | |
4807 | ||
4808 | ||
556b30da JM |
4809 | int p2p_set_no_go_freq(struct p2p_data *p2p, |
4810 | const struct wpa_freq_range_list *list) | |
4811 | { | |
4812 | struct wpa_freq_range *tmp; | |
4813 | ||
4814 | if (list == NULL || list->num == 0) { | |
4815 | os_free(p2p->no_go_freq.range); | |
4816 | p2p->no_go_freq.range = NULL; | |
4817 | p2p->no_go_freq.num = 0; | |
4818 | return 0; | |
4819 | } | |
4820 | ||
4821 | tmp = os_calloc(list->num, sizeof(struct wpa_freq_range)); | |
4822 | if (tmp == NULL) | |
4823 | return -1; | |
4824 | os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range)); | |
4825 | os_free(p2p->no_go_freq.range); | |
4826 | p2p->no_go_freq.range = tmp; | |
4827 | p2p->no_go_freq.num = list->num; | |
4828 | p2p_dbg(p2p, "Updated no GO chan list"); | |
4829 | ||
4830 | return 0; | |
4831 | } | |
4832 | ||
4833 | ||
b22128ef JM |
4834 | int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr, |
4835 | u8 *iface_addr) | |
4836 | { | |
4837 | struct p2p_device *dev = p2p_get_device(p2p, dev_addr); | |
4838 | if (dev == NULL || is_zero_ether_addr(dev->interface_addr)) | |
4839 | return -1; | |
4840 | os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN); | |
4841 | return 0; | |
4842 | } | |
80c9582a JM |
4843 | |
4844 | ||
4147a2cc JM |
4845 | int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr, |
4846 | u8 *dev_addr) | |
4847 | { | |
4848 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); | |
4849 | if (dev == NULL) | |
4850 | return -1; | |
c5db8e51 | 4851 | os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN); |
4147a2cc JM |
4852 | return 0; |
4853 | } | |
4854 | ||
4855 | ||
80c9582a JM |
4856 | void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr) |
4857 | { | |
4858 | os_memcpy(p2p->peer_filter, addr, ETH_ALEN); | |
4859 | if (is_zero_ether_addr(p2p->peer_filter)) | |
ed496f13 | 4860 | p2p_dbg(p2p, "Disable peer filter"); |
80c9582a | 4861 | else |
ed496f13 JM |
4862 | p2p_dbg(p2p, "Enable peer filter for " MACSTR, |
4863 | MAC2STR(p2p->peer_filter)); | |
80c9582a | 4864 | } |
72044390 JM |
4865 | |
4866 | ||
4867 | void p2p_set_cross_connect(struct p2p_data *p2p, int enabled) | |
4868 | { | |
ed496f13 | 4869 | p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled"); |
72044390 JM |
4870 | if (p2p->cross_connect == enabled) |
4871 | return; | |
4872 | p2p->cross_connect = enabled; | |
4873 | /* TODO: may need to tear down any action group where we are GO(?) */ | |
4874 | } | |
f8d0131a JM |
4875 | |
4876 | ||
4877 | int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr) | |
4878 | { | |
4879 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); | |
4880 | if (dev == NULL) | |
4881 | return -1; | |
4882 | if (dev->oper_freq <= 0) | |
4883 | return -1; | |
4884 | return dev->oper_freq; | |
4885 | } | |
0f66abd2 SS |
4886 | |
4887 | ||
4888 | void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled) | |
4889 | { | |
ed496f13 | 4890 | p2p_dbg(p2p, "Intra BSS distribution %s", |
0f66abd2 SS |
4891 | enabled ? "enabled" : "disabled"); |
4892 | p2p->cfg->p2p_intra_bss = enabled; | |
4893 | } | |
b5c9da8d JM |
4894 | |
4895 | ||
51e9f228 JM |
4896 | void p2p_update_channel_list(struct p2p_data *p2p, |
4897 | const struct p2p_channels *chan, | |
4898 | const struct p2p_channels *cli_chan) | |
b5c9da8d | 4899 | { |
ed496f13 | 4900 | p2p_dbg(p2p, "Update channel list"); |
b5c9da8d | 4901 | os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels)); |
51e9f228 JM |
4902 | p2p_channels_dump(p2p, "channels", &p2p->cfg->channels); |
4903 | os_memcpy(&p2p->cfg->cli_channels, cli_chan, | |
4904 | sizeof(struct p2p_channels)); | |
4905 | p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels); | |
b5c9da8d | 4906 | } |
3f9285ff JM |
4907 | |
4908 | ||
4909 | int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst, | |
4910 | const u8 *src, const u8 *bssid, const u8 *buf, | |
4911 | size_t len, unsigned int wait_time) | |
4912 | { | |
947b5a15 JM |
4913 | int res, scheduled; |
4914 | ||
947b5a15 JM |
4915 | res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid, |
4916 | buf, len, wait_time, &scheduled); | |
4917 | if (res == 0 && scheduled && p2p->in_listen && freq > 0 && | |
4918 | (unsigned int) p2p->drv_in_listen != freq) { | |
4919 | p2p_dbg(p2p, | |
4920 | "Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz", | |
4921 | p2p->drv_in_listen, freq); | |
4922 | p2p_stop_listen_for_freq(p2p, freq); | |
4923 | } | |
4924 | return res; | |
3f9285ff | 4925 | } |
7cfc4ac3 AGS |
4926 | |
4927 | ||
4928 | void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5, | |
4929 | int freq_overall) | |
4930 | { | |
ed496f13 JM |
4931 | p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d", |
4932 | freq_24, freq_5, freq_overall); | |
7cfc4ac3 AGS |
4933 | p2p->best_freq_24 = freq_24; |
4934 | p2p->best_freq_5 = freq_5; | |
4935 | p2p->best_freq_overall = freq_overall; | |
4936 | } | |
231bbd03 SS |
4937 | |
4938 | ||
6cb27aa8 JM |
4939 | void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq) |
4940 | { | |
ed496f13 | 4941 | p2p_dbg(p2p, "Own frequency preference: %d MHz", freq); |
6cb27aa8 JM |
4942 | p2p->own_freq_preference = freq; |
4943 | } | |
4944 | ||
4945 | ||
231bbd03 SS |
4946 | const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p) |
4947 | { | |
4948 | if (p2p == NULL || p2p->go_neg_peer == NULL) | |
4949 | return NULL; | |
c5db8e51 | 4950 | return p2p->go_neg_peer->info.p2p_device_addr; |
231bbd03 | 4951 | } |
c165d81e JB |
4952 | |
4953 | ||
4954 | const struct p2p_peer_info * | |
4955 | p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next) | |
4956 | { | |
4957 | struct p2p_device *dev; | |
4958 | ||
4959 | if (addr) { | |
4960 | dev = p2p_get_device(p2p, addr); | |
4961 | if (!dev) | |
4962 | return NULL; | |
4963 | ||
4964 | if (!next) { | |
4965 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) | |
4966 | return NULL; | |
4967 | ||
4968 | return &dev->info; | |
4969 | } else { | |
4970 | do { | |
4971 | dev = dl_list_first(&dev->list, | |
4972 | struct p2p_device, | |
4973 | list); | |
0bceb8d6 | 4974 | if (!dev || &dev->list == &p2p->devices) |
c165d81e JB |
4975 | return NULL; |
4976 | } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY); | |
4977 | } | |
4978 | } else { | |
4979 | dev = dl_list_first(&p2p->devices, struct p2p_device, list); | |
4980 | if (!dev) | |
4981 | return NULL; | |
4982 | while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { | |
4983 | dev = dl_list_first(&dev->list, | |
4984 | struct p2p_device, | |
4985 | list); | |
0bceb8d6 | 4986 | if (!dev || &dev->list == &p2p->devices) |
c165d81e JB |
4987 | return NULL; |
4988 | } | |
4989 | } | |
4990 | ||
4991 | return &dev->info; | |
4992 | } | |
303f60d3 JM |
4993 | |
4994 | ||
4995 | int p2p_in_progress(struct p2p_data *p2p) | |
4996 | { | |
4997 | if (p2p == NULL) | |
4998 | return 0; | |
1b5d4714 | 4999 | if (p2p->state == P2P_SEARCH) |
99fcd404 | 5000 | return 2; |
fc6997b3 | 5001 | return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING; |
303f60d3 | 5002 | } |
4f219667 JM |
5003 | |
5004 | ||
5005 | void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout, | |
5006 | u8 client_timeout) | |
5007 | { | |
5008 | if (p2p) { | |
5009 | p2p->go_timeout = go_timeout; | |
5010 | p2p->client_timeout = client_timeout; | |
5011 | } | |
5012 | } | |
99fcd404 JM |
5013 | |
5014 | ||
9675ce35 JM |
5015 | #ifdef CONFIG_WIFI_DISPLAY |
5016 | ||
5017 | static void p2p_update_wfd_ie_groups(struct p2p_data *p2p) | |
5018 | { | |
5019 | size_t g; | |
5020 | struct p2p_group *group; | |
5021 | ||
5022 | for (g = 0; g < p2p->num_groups; g++) { | |
5023 | group = p2p->groups[g]; | |
3342c263 | 5024 | p2p_group_force_beacon_update_ies(group); |
9675ce35 JM |
5025 | } |
5026 | } | |
5027 | ||
5028 | ||
5029 | int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie) | |
5030 | { | |
5031 | wpabuf_free(p2p->wfd_ie_beacon); | |
5032 | p2p->wfd_ie_beacon = ie; | |
5033 | p2p_update_wfd_ie_groups(p2p); | |
5034 | return 0; | |
5035 | } | |
5036 | ||
5037 | ||
5038 | int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie) | |
5039 | { | |
5040 | wpabuf_free(p2p->wfd_ie_probe_req); | |
5041 | p2p->wfd_ie_probe_req = ie; | |
5042 | return 0; | |
5043 | } | |
5044 | ||
5045 | ||
5046 | int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie) | |
5047 | { | |
5048 | wpabuf_free(p2p->wfd_ie_probe_resp); | |
5049 | p2p->wfd_ie_probe_resp = ie; | |
5050 | p2p_update_wfd_ie_groups(p2p); | |
5051 | return 0; | |
5052 | } | |
5053 | ||
5054 | ||
5055 | int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie) | |
5056 | { | |
5057 | wpabuf_free(p2p->wfd_ie_assoc_req); | |
5058 | p2p->wfd_ie_assoc_req = ie; | |
5059 | return 0; | |
5060 | } | |
5061 | ||
5062 | ||
5063 | int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie) | |
5064 | { | |
5065 | wpabuf_free(p2p->wfd_ie_invitation); | |
5066 | p2p->wfd_ie_invitation = ie; | |
5067 | return 0; | |
5068 | } | |
5069 | ||
5070 | ||
5071 | int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie) | |
5072 | { | |
5073 | wpabuf_free(p2p->wfd_ie_prov_disc_req); | |
5074 | p2p->wfd_ie_prov_disc_req = ie; | |
5075 | return 0; | |
5076 | } | |
5077 | ||
5078 | ||
5079 | int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie) | |
5080 | { | |
5081 | wpabuf_free(p2p->wfd_ie_prov_disc_resp); | |
5082 | p2p->wfd_ie_prov_disc_resp = ie; | |
5083 | return 0; | |
5084 | } | |
5085 | ||
5086 | ||
5087 | int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie) | |
5088 | { | |
5089 | wpabuf_free(p2p->wfd_ie_go_neg); | |
5090 | p2p->wfd_ie_go_neg = ie; | |
5091 | return 0; | |
5092 | } | |
5093 | ||
5094 | ||
5095 | int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem) | |
5096 | { | |
5097 | wpabuf_free(p2p->wfd_dev_info); | |
5098 | if (elem) { | |
5099 | p2p->wfd_dev_info = wpabuf_dup(elem); | |
5100 | if (p2p->wfd_dev_info == NULL) | |
5101 | return -1; | |
5102 | } else | |
5103 | p2p->wfd_dev_info = NULL; | |
5104 | ||
5105 | return 0; | |
5106 | } | |
5107 | ||
5108 | ||
e9518ae7 AHS |
5109 | int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem) |
5110 | { | |
5111 | wpabuf_free(p2p->wfd_r2_dev_info); | |
5112 | if (elem) { | |
5113 | p2p->wfd_r2_dev_info = wpabuf_dup(elem); | |
5114 | if (p2p->wfd_r2_dev_info == NULL) | |
5115 | return -1; | |
5116 | } else | |
5117 | p2p->wfd_r2_dev_info = NULL; | |
5118 | ||
5119 | return 0; | |
5120 | } | |
5121 | ||
5122 | ||
9675ce35 JM |
5123 | int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem) |
5124 | { | |
5125 | wpabuf_free(p2p->wfd_assoc_bssid); | |
5126 | if (elem) { | |
5127 | p2p->wfd_assoc_bssid = wpabuf_dup(elem); | |
5128 | if (p2p->wfd_assoc_bssid == NULL) | |
5129 | return -1; | |
5130 | } else | |
5131 | p2p->wfd_assoc_bssid = NULL; | |
5132 | ||
5133 | return 0; | |
5134 | } | |
5135 | ||
5136 | ||
5137 | int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p, | |
5138 | const struct wpabuf *elem) | |
5139 | { | |
5140 | wpabuf_free(p2p->wfd_coupled_sink_info); | |
5141 | if (elem) { | |
5142 | p2p->wfd_coupled_sink_info = wpabuf_dup(elem); | |
5143 | if (p2p->wfd_coupled_sink_info == NULL) | |
5144 | return -1; | |
5145 | } else | |
5146 | p2p->wfd_coupled_sink_info = NULL; | |
5147 | ||
5148 | return 0; | |
5149 | } | |
5150 | ||
5151 | #endif /* CONFIG_WIFI_DISPLAY */ | |
96beff11 JM |
5152 | |
5153 | ||
5154 | int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int, | |
5155 | int max_disc_tu) | |
5156 | { | |
5157 | if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0) | |
5158 | return -1; | |
5159 | ||
5160 | p2p->min_disc_int = min_disc_int; | |
5161 | p2p->max_disc_int = max_disc_int; | |
5162 | p2p->max_disc_tu = max_disc_tu; | |
ed496f13 JM |
5163 | p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d", |
5164 | min_disc_int, max_disc_int, max_disc_tu); | |
96beff11 JM |
5165 | |
5166 | return 0; | |
5167 | } | |
ed496f13 JM |
5168 | |
5169 | ||
5170 | void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...) | |
5171 | { | |
5172 | va_list ap; | |
5173 | char buf[500]; | |
5174 | ||
5175 | if (!p2p->cfg->debug_print) | |
5176 | return; | |
5177 | ||
5178 | va_start(ap, fmt); | |
5179 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
5180 | buf[sizeof(buf) - 1] = '\0'; | |
5181 | va_end(ap); | |
5182 | p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf); | |
5183 | } | |
5184 | ||
5185 | ||
5186 | void p2p_info(struct p2p_data *p2p, const char *fmt, ...) | |
5187 | { | |
5188 | va_list ap; | |
5189 | char buf[500]; | |
5190 | ||
5191 | if (!p2p->cfg->debug_print) | |
5192 | return; | |
5193 | ||
5194 | va_start(ap, fmt); | |
5195 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
5196 | buf[sizeof(buf) - 1] = '\0'; | |
5197 | va_end(ap); | |
5198 | p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf); | |
5199 | } | |
5200 | ||
5201 | ||
5202 | void p2p_err(struct p2p_data *p2p, const char *fmt, ...) | |
5203 | { | |
5204 | va_list ap; | |
5205 | char buf[500]; | |
5206 | ||
5207 | if (!p2p->cfg->debug_print) | |
5208 | return; | |
5209 | ||
5210 | va_start(ap, fmt); | |
5211 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
5212 | buf[sizeof(buf) - 1] = '\0'; | |
5213 | va_end(ap); | |
5214 | p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf); | |
5215 | } | |
93588780 JM |
5216 | |
5217 | ||
36716eef TB |
5218 | void p2p_loop_on_known_peers(struct p2p_data *p2p, |
5219 | void (*peer_callback)(struct p2p_peer_info *peer, | |
5220 | void *user_data), | |
5221 | void *user_data) | |
5222 | { | |
5223 | struct p2p_device *dev, *n; | |
5224 | ||
5225 | dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) { | |
5226 | peer_callback(&dev->info, user_data); | |
5227 | } | |
5228 | } | |
5229 | ||
5230 | ||
93588780 JM |
5231 | #ifdef CONFIG_WPS_NFC |
5232 | ||
59b45d1a | 5233 | static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p, |
abe44e3c JM |
5234 | int client_freq, |
5235 | const u8 *go_dev_addr, | |
5236 | const u8 *ssid, size_t ssid_len) | |
93588780 JM |
5237 | { |
5238 | struct wpabuf *buf; | |
5239 | u8 op_class, channel; | |
5240 | enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP; | |
5241 | ||
5242 | buf = wpabuf_alloc(1000); | |
5243 | if (buf == NULL) | |
5244 | return NULL; | |
5245 | ||
5246 | op_class = p2p->cfg->reg_class; | |
5247 | channel = p2p->cfg->channel; | |
5248 | ||
5249 | p2p_buf_add_capability(buf, p2p->dev_capab & | |
5250 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); | |
5251 | p2p_buf_add_device_info(buf, p2p, NULL); | |
5252 | ||
5253 | if (p2p->num_groups > 0) { | |
d06e9ac5 | 5254 | int freq = p2p_group_get_freq(p2p->groups[0]); |
93588780 | 5255 | role = P2P_GO_IN_A_GROUP; |
d06e9ac5 JM |
5256 | if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) { |
5257 | p2p_dbg(p2p, | |
5258 | "Unknown GO operating frequency %d MHz for NFC handover", | |
5259 | freq); | |
5260 | wpabuf_free(buf); | |
5261 | return NULL; | |
5262 | } | |
59b45d1a JM |
5263 | } else if (client_freq > 0) { |
5264 | role = P2P_CLIENT_IN_A_GROUP; | |
d06e9ac5 JM |
5265 | if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) { |
5266 | p2p_dbg(p2p, | |
5267 | "Unknown client operating frequency %d MHz for NFC handover", | |
5268 | client_freq); | |
5269 | wpabuf_free(buf); | |
5270 | return NULL; | |
5271 | } | |
93588780 JM |
5272 | } |
5273 | ||
5274 | p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class, | |
5275 | channel, role); | |
5276 | ||
5277 | if (p2p->num_groups > 0) { | |
5278 | /* Limit number of clients to avoid very long message */ | |
5279 | p2p_buf_add_group_info(p2p->groups[0], buf, 5); | |
5280 | p2p_group_buf_add_id(p2p->groups[0], buf); | |
abe44e3c JM |
5281 | } else if (client_freq > 0 && |
5282 | go_dev_addr && !is_zero_ether_addr(go_dev_addr) && | |
5283 | ssid && ssid_len > 0) { | |
5284 | /* | |
5285 | * Add the optional P2P Group ID to indicate in which group this | |
5286 | * device is a P2P Client. | |
5287 | */ | |
5288 | p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len); | |
93588780 JM |
5289 | } |
5290 | ||
5291 | return buf; | |
5292 | } | |
5293 | ||
5294 | ||
59b45d1a | 5295 | struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p, |
abe44e3c JM |
5296 | int client_freq, |
5297 | const u8 *go_dev_addr, | |
5298 | const u8 *ssid, size_t ssid_len) | |
93588780 | 5299 | { |
abe44e3c JM |
5300 | return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid, |
5301 | ssid_len); | |
93588780 JM |
5302 | } |
5303 | ||
5304 | ||
59b45d1a | 5305 | struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p, |
abe44e3c JM |
5306 | int client_freq, |
5307 | const u8 *go_dev_addr, | |
5308 | const u8 *ssid, size_t ssid_len) | |
93588780 | 5309 | { |
abe44e3c JM |
5310 | return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid, |
5311 | ssid_len); | |
93588780 JM |
5312 | } |
5313 | ||
db6ae69e JM |
5314 | |
5315 | int p2p_process_nfc_connection_handover(struct p2p_data *p2p, | |
5316 | struct p2p_nfc_params *params) | |
5317 | { | |
5318 | struct p2p_message msg; | |
5319 | struct p2p_device *dev; | |
5320 | const u8 *p2p_dev_addr; | |
59b45d1a JM |
5321 | int freq; |
5322 | enum p2p_role_indication role; | |
db6ae69e JM |
5323 | |
5324 | params->next_step = NO_ACTION; | |
5325 | ||
5326 | if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len, | |
5327 | params->p2p_attr, params->p2p_len, &msg)) { | |
5328 | p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC"); | |
5329 | p2p_parse_free(&msg); | |
5330 | return -1; | |
5331 | } | |
5332 | ||
5333 | if (msg.p2p_device_addr) | |
5334 | p2p_dev_addr = msg.p2p_device_addr; | |
5335 | else if (msg.device_id) | |
5336 | p2p_dev_addr = msg.device_id; | |
5337 | else { | |
5338 | p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id"); | |
5339 | p2p_parse_free(&msg); | |
5340 | return -1; | |
5341 | } | |
5342 | ||
5343 | if (msg.oob_dev_password) { | |
5344 | os_memcpy(params->oob_dev_pw, msg.oob_dev_password, | |
5345 | msg.oob_dev_password_len); | |
5346 | params->oob_dev_pw_len = msg.oob_dev_password_len; | |
5347 | } | |
5348 | ||
5349 | dev = p2p_create_device(p2p, p2p_dev_addr); | |
5350 | if (dev == NULL) { | |
5351 | p2p_parse_free(&msg); | |
5352 | return -1; | |
5353 | } | |
5354 | ||
5355 | params->peer = &dev->info; | |
5356 | ||
5357 | os_get_reltime(&dev->last_seen); | |
5358 | dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY); | |
5359 | p2p_copy_wps_info(p2p, dev, 0, &msg); | |
5360 | ||
59b45d1a JM |
5361 | if (!msg.oob_go_neg_channel) { |
5362 | p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included"); | |
6b5147af | 5363 | p2p_parse_free(&msg); |
59b45d1a JM |
5364 | return -1; |
5365 | } | |
db6ae69e | 5366 | |
59b45d1a JM |
5367 | if (msg.oob_go_neg_channel[3] == 0 && |
5368 | msg.oob_go_neg_channel[4] == 0) | |
5369 | freq = 0; | |
5370 | else | |
5371 | freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3], | |
5372 | msg.oob_go_neg_channel[4]); | |
5373 | if (freq < 0) { | |
5374 | p2p_dbg(p2p, "Unknown peer OOB GO Neg channel"); | |
6b5147af | 5375 | p2p_parse_free(&msg); |
59b45d1a | 5376 | return -1; |
59b45d1a | 5377 | } |
23318bea | 5378 | role = msg.oob_go_neg_channel[5]; |
db6ae69e | 5379 | |
23318bea JM |
5380 | if (role == P2P_GO_IN_A_GROUP) { |
5381 | p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq); | |
5382 | params->go_freq = freq; | |
abe44e3c JM |
5383 | } else if (role == P2P_CLIENT_IN_A_GROUP) { |
5384 | p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz", | |
5385 | freq); | |
5386 | params->go_freq = freq; | |
23318bea JM |
5387 | } else |
5388 | p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq); | |
5389 | dev->oob_go_neg_freq = freq; | |
5390 | ||
5391 | if (!params->sel && role != P2P_GO_IN_A_GROUP) { | |
59b45d1a JM |
5392 | freq = p2p_channel_to_freq(p2p->cfg->reg_class, |
5393 | p2p->cfg->channel); | |
5394 | if (freq < 0) { | |
5395 | p2p_dbg(p2p, "Own listen channel not known"); | |
6b5147af | 5396 | p2p_parse_free(&msg); |
59b45d1a JM |
5397 | return -1; |
5398 | } | |
5399 | p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq); | |
5400 | dev->oob_go_neg_freq = freq; | |
db6ae69e JM |
5401 | } |
5402 | ||
23318bea JM |
5403 | if (msg.group_id) { |
5404 | os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN); | |
5405 | params->go_ssid_len = msg.group_id_len - ETH_ALEN; | |
5406 | os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN, | |
5407 | params->go_ssid_len); | |
5408 | } | |
59b45d1a | 5409 | |
db6ae69e JM |
5410 | if (dev->flags & P2P_DEV_USER_REJECTED) { |
5411 | p2p_dbg(p2p, "Do not report rejected device"); | |
8a387a26 | 5412 | p2p_parse_free(&msg); |
db6ae69e JM |
5413 | return 0; |
5414 | } | |
5415 | ||
5416 | if (!(dev->flags & P2P_DEV_REPORTED)) { | |
5417 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info, | |
5418 | !(dev->flags & P2P_DEV_REPORTED_ONCE)); | |
5419 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; | |
5420 | } | |
8a387a26 | 5421 | p2p_parse_free(&msg); |
db6ae69e | 5422 | |
59b45d1a | 5423 | if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0) |
74df9ecd | 5424 | params->next_step = BOTH_GO; |
59b45d1a | 5425 | else if (role == P2P_GO_IN_A_GROUP) |
db6ae69e | 5426 | params->next_step = JOIN_GROUP; |
59b45d1a JM |
5427 | else if (role == P2P_CLIENT_IN_A_GROUP) { |
5428 | dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY; | |
5429 | params->next_step = PEER_CLIENT; | |
5430 | } else if (p2p->num_groups > 0) | |
db6ae69e JM |
5431 | params->next_step = AUTH_JOIN; |
5432 | else if (params->sel) | |
5433 | params->next_step = INIT_GO_NEG; | |
5434 | else | |
5435 | params->next_step = RESP_GO_NEG; | |
5436 | ||
5437 | return 0; | |
5438 | } | |
5439 | ||
c4f87a70 JM |
5440 | |
5441 | void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id, | |
5442 | int go_intent, | |
5443 | const u8 *own_interface_addr) | |
5444 | { | |
5445 | ||
5446 | p2p->authorized_oob_dev_pw_id = dev_pw_id; | |
5447 | if (dev_pw_id == 0) { | |
5448 | p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover"); | |
5449 | return; | |
5450 | } | |
5451 | ||
5452 | p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover", | |
5453 | dev_pw_id); | |
5454 | ||
5455 | p2p->go_intent = go_intent; | |
5456 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); | |
5457 | } | |
5458 | ||
93588780 | 5459 | #endif /* CONFIG_WPS_NFC */ |
1b928f96 JM |
5460 | |
5461 | ||
5462 | int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len) | |
5463 | { | |
5464 | if (len < 8 || len > 63) | |
5465 | return -1; | |
5466 | p2p->cfg->passphrase_len = len; | |
5467 | return 0; | |
5468 | } | |
86bd36f0 JM |
5469 | |
5470 | ||
5471 | void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem) | |
5472 | { | |
5473 | p2p->vendor_elem = vendor_elem; | |
5474 | } | |
0c6eee8b RR |
5475 | |
5476 | ||
5477 | void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx) | |
5478 | { | |
5479 | struct p2p_data *p2p = eloop_ctx; | |
5480 | ||
5481 | p2p_dbg(p2p, | |
5482 | "Timeout on waiting peer to become ready for GO Negotiation"); | |
8c00fd00 | 5483 | p2p_go_neg_failed(p2p, -1); |
0c6eee8b | 5484 | } |
370017d9 AK |
5485 | |
5486 | ||
5487 | void p2p_set_own_pref_freq_list(struct p2p_data *p2p, | |
5488 | const unsigned int *pref_freq_list, | |
5489 | unsigned int size) | |
5490 | { | |
5491 | unsigned int i; | |
5492 | ||
5493 | if (size > P2P_MAX_PREF_CHANNELS) | |
5494 | size = P2P_MAX_PREF_CHANNELS; | |
5495 | p2p->num_pref_freq = size; | |
5496 | for (i = 0; i < size; i++) { | |
5497 | p2p->pref_freq_list[i] = pref_freq_list[i]; | |
5498 | p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz", | |
5499 | i, p2p->pref_freq_list[i]); | |
5500 | } | |
5501 | } | |
a6f5b193 PX |
5502 | |
5503 | ||
3a7819f0 JM |
5504 | void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class, |
5505 | u8 chan) | |
5506 | { | |
5507 | p2p->override_pref_op_class = op_class; | |
5508 | p2p->override_pref_channel = chan; | |
5509 | } | |
5510 | ||
5511 | ||
a6f5b193 PX |
5512 | struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p, |
5513 | unsigned int freq) | |
5514 | { | |
5515 | struct wpabuf *ies, *buf; | |
5516 | u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; | |
5517 | int ret; | |
5518 | ||
5519 | ies = p2p_build_probe_resp_ies(p2p, NULL, 0); | |
5520 | if (!ies) { | |
5521 | wpa_printf(MSG_ERROR, | |
5522 | "CTRL: Failed to build Probe Response IEs"); | |
5523 | return NULL; | |
5524 | } | |
5525 | ||
5526 | buf = wpabuf_alloc(200 + wpabuf_len(ies)); | |
5527 | if (!buf) { | |
5528 | wpabuf_free(ies); | |
5529 | return NULL; | |
5530 | } | |
5531 | ||
5532 | ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq); | |
5533 | wpabuf_free(ies); | |
5534 | if (ret) { | |
5535 | wpabuf_free(buf); | |
5536 | return NULL; | |
5537 | } | |
5538 | ||
5539 | return buf; | |
5540 | } |