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