]>
Commit | Line | Data |
---|---|---|
b22128ef JM |
1 | /* |
2 | * Wi-Fi Direct - P2P module | |
3 | * Copyright (c) 2009-2010, Atheros Communications | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Alternatively, this software may be distributed under the terms of BSD | |
10 | * license. | |
11 | * | |
12 | * See README and COPYING for more details. | |
13 | */ | |
14 | ||
15 | #include "includes.h" | |
16 | ||
17 | #include "common.h" | |
18 | #include "eloop.h" | |
19 | #include "common/ieee802_11_defs.h" | |
20 | #include "common/ieee802_11_common.h" | |
8aebb0e4 | 21 | #include "common/wpa_ctrl.h" |
b22128ef JM |
22 | #include "wps/wps_i.h" |
23 | #include "p2p_i.h" | |
24 | #include "p2p.h" | |
25 | ||
26 | ||
27 | static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx); | |
28 | static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev); | |
29 | static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, | |
30 | const u8 *sa, const u8 *data, size_t len, | |
31 | int rx_freq); | |
32 | static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, | |
33 | const u8 *sa, const u8 *data, | |
34 | size_t len); | |
35 | static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx); | |
40c03fd4 | 36 | static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx); |
b22128ef JM |
37 | |
38 | ||
40c03fd4 JM |
39 | /* |
40 | * p2p_scan recovery timeout | |
41 | * | |
42 | * Many drivers are using 30 second timeout on scan results. Allow a bit larger | |
43 | * timeout for this to avoid hitting P2P timeout unnecessarily. | |
44 | */ | |
45 | #define P2P_SCAN_TIMEOUT 35 | |
46 | ||
b22128ef JM |
47 | /** |
48 | * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer | |
49 | * entries will be removed | |
50 | */ | |
51 | #define P2P_PEER_EXPIRATION_AGE 300 | |
52 | ||
53 | #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2) | |
54 | ||
55 | static void p2p_expire_peers(struct p2p_data *p2p) | |
56 | { | |
57 | struct p2p_device *dev, *n; | |
58 | struct os_time now; | |
1d277f02 | 59 | size_t i; |
b22128ef JM |
60 | |
61 | os_get_time(&now); | |
62 | dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) { | |
63 | if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec) | |
64 | continue; | |
b1aebbc4 JM |
65 | |
66 | if (p2p->cfg->go_connected && | |
67 | p2p->cfg->go_connected(p2p->cfg->cb_ctx, | |
68 | dev->info.p2p_device_addr)) { | |
69 | /* | |
70 | * We are connected as a client to a group in which the | |
71 | * peer is the GO, so do not expire the peer entry. | |
72 | */ | |
73 | os_get_time(&dev->last_seen); | |
74 | continue; | |
75 | } | |
76 | ||
1d277f02 JM |
77 | for (i = 0; i < p2p->num_groups; i++) { |
78 | if (p2p_group_is_client_connected( | |
79 | p2p->groups[i], dev->info.p2p_device_addr)) | |
80 | break; | |
81 | } | |
82 | if (i < p2p->num_groups) { | |
83 | /* | |
84 | * The peer is connected as a client in a group where | |
85 | * we are the GO, so do not expire the peer entry. | |
86 | */ | |
87 | os_get_time(&dev->last_seen); | |
88 | continue; | |
89 | } | |
90 | ||
b22128ef | 91 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer " |
c5db8e51 | 92 | "entry " MACSTR, MAC2STR(dev->info.p2p_device_addr)); |
b22128ef JM |
93 | dl_list_del(&dev->list); |
94 | p2p_device_free(p2p, dev); | |
95 | } | |
96 | } | |
97 | ||
98 | ||
99 | static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx) | |
100 | { | |
101 | struct p2p_data *p2p = eloop_ctx; | |
102 | p2p_expire_peers(p2p); | |
103 | eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0, | |
104 | p2p_expiration_timeout, p2p, NULL); | |
105 | } | |
106 | ||
107 | ||
108 | static const char * p2p_state_txt(int state) | |
109 | { | |
110 | switch (state) { | |
111 | case P2P_IDLE: | |
112 | return "IDLE"; | |
113 | case P2P_SEARCH: | |
114 | return "SEARCH"; | |
115 | case P2P_CONNECT: | |
116 | return "CONNECT"; | |
117 | case P2P_CONNECT_LISTEN: | |
118 | return "CONNECT_LISTEN"; | |
119 | case P2P_GO_NEG: | |
120 | return "GO_NEG"; | |
121 | case P2P_LISTEN_ONLY: | |
122 | return "LISTEN_ONLY"; | |
123 | case P2P_WAIT_PEER_CONNECT: | |
124 | return "WAIT_PEER_CONNECT"; | |
125 | case P2P_WAIT_PEER_IDLE: | |
126 | return "WAIT_PEER_IDLE"; | |
127 | case P2P_SD_DURING_FIND: | |
128 | return "SD_DURING_FIND"; | |
129 | case P2P_PROVISIONING: | |
130 | return "PROVISIONING"; | |
131 | case P2P_PD_DURING_FIND: | |
132 | return "PD_DURING_FIND"; | |
133 | case P2P_INVITE: | |
134 | return "INVITE"; | |
135 | case P2P_INVITE_LISTEN: | |
136 | return "INVITE_LISTEN"; | |
39185dfa JM |
137 | case P2P_SEARCH_WHEN_READY: |
138 | return "SEARCH_WHEN_READY"; | |
b22128ef JM |
139 | default: |
140 | return "?"; | |
141 | } | |
142 | } | |
143 | ||
144 | ||
ec437d9e JJ |
145 | u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr) |
146 | { | |
147 | struct p2p_device *dev = NULL; | |
148 | ||
149 | if (!addr || !p2p) | |
150 | return 0; | |
151 | ||
152 | dev = p2p_get_device(p2p, addr); | |
153 | if (dev) | |
154 | return dev->wps_prov_info; | |
155 | else | |
156 | return 0; | |
157 | } | |
158 | ||
159 | ||
160 | void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *iface_addr) | |
161 | { | |
162 | struct p2p_device *dev = NULL; | |
163 | ||
164 | if (!iface_addr || !p2p) | |
165 | return; | |
166 | ||
167 | dev = p2p_get_device_interface(p2p, iface_addr); | |
168 | if (dev) | |
169 | dev->wps_prov_info = 0; | |
170 | } | |
171 | ||
172 | ||
b22128ef JM |
173 | void p2p_set_state(struct p2p_data *p2p, int new_state) |
174 | { | |
175 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s", | |
176 | p2p_state_txt(p2p->state), p2p_state_txt(new_state)); | |
177 | p2p->state = new_state; | |
178 | } | |
179 | ||
180 | ||
181 | void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec) | |
182 | { | |
183 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
184 | "P2P: Set timeout (state=%s): %u.%06u sec", | |
185 | p2p_state_txt(p2p->state), sec, usec); | |
186 | eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); | |
187 | eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL); | |
188 | } | |
189 | ||
190 | ||
191 | void p2p_clear_timeout(struct p2p_data *p2p) | |
192 | { | |
193 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)", | |
194 | p2p_state_txt(p2p->state)); | |
195 | eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); | |
196 | } | |
197 | ||
198 | ||
199 | void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer, | |
200 | int status) | |
201 | { | |
202 | struct p2p_go_neg_results res; | |
203 | p2p_clear_timeout(p2p); | |
204 | p2p_set_state(p2p, P2P_IDLE); | |
eb916eb8 JB |
205 | if (p2p->go_neg_peer) |
206 | p2p->go_neg_peer->wps_method = WPS_NOT_READY; | |
b22128ef JM |
207 | p2p->go_neg_peer = NULL; |
208 | ||
209 | os_memset(&res, 0, sizeof(res)); | |
210 | res.status = status; | |
211 | if (peer) { | |
c5db8e51 | 212 | os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, |
b22128ef JM |
213 | ETH_ALEN); |
214 | os_memcpy(res.peer_interface_addr, peer->intended_addr, | |
215 | ETH_ALEN); | |
216 | } | |
217 | p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); | |
218 | } | |
219 | ||
220 | ||
221 | static void p2p_listen_in_find(struct p2p_data *p2p) | |
222 | { | |
223 | unsigned int r, tu; | |
224 | int freq; | |
225 | struct wpabuf *ies; | |
226 | ||
227 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
228 | "P2P: Starting short listen state (state=%s)", | |
229 | p2p_state_txt(p2p->state)); | |
230 | ||
231 | freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class, | |
232 | p2p->cfg->channel); | |
233 | if (freq < 0) { | |
234 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
235 | "P2P: Unknown regulatory class/channel"); | |
236 | return; | |
237 | } | |
238 | ||
239 | os_get_random((u8 *) &r, sizeof(r)); | |
240 | tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) + | |
241 | p2p->min_disc_int) * 100; | |
242 | ||
243 | p2p->pending_listen_freq = freq; | |
244 | p2p->pending_listen_sec = 0; | |
245 | p2p->pending_listen_usec = 1024 * tu; | |
246 | ||
247 | ies = p2p_build_probe_resp_ies(p2p); | |
248 | if (ies == NULL) | |
249 | return; | |
250 | ||
251 | if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000, | |
252 | ies) < 0) { | |
253 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
254 | "P2P: Failed to start listen mode"); | |
255 | p2p->pending_listen_freq = 0; | |
256 | } | |
257 | wpabuf_free(ies); | |
258 | } | |
259 | ||
260 | ||
261 | int p2p_listen(struct p2p_data *p2p, unsigned int timeout) | |
262 | { | |
263 | int freq; | |
264 | struct wpabuf *ies; | |
265 | ||
266 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
267 | "P2P: Going to listen(only) state"); | |
268 | ||
269 | freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class, | |
270 | p2p->cfg->channel); | |
271 | if (freq < 0) { | |
272 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
273 | "P2P: Unknown regulatory class/channel"); | |
274 | return -1; | |
275 | } | |
276 | ||
277 | p2p->pending_listen_freq = freq; | |
278 | p2p->pending_listen_sec = timeout / 1000; | |
279 | p2p->pending_listen_usec = (timeout % 1000) * 1000; | |
280 | ||
281 | if (p2p->p2p_scan_running) { | |
d9bdba9f JM |
282 | if (p2p->start_after_scan == P2P_AFTER_SCAN_NOTHING) { |
283 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
284 | "P2P: p2p_scan running - connect is already " | |
285 | "pending - skip listen"); | |
286 | return 0; | |
287 | } | |
b22128ef JM |
288 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
289 | "P2P: p2p_scan running - delay start of listen state"); | |
290 | p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN; | |
291 | return 0; | |
292 | } | |
293 | ||
294 | ies = p2p_build_probe_resp_ies(p2p); | |
295 | if (ies == NULL) | |
296 | return -1; | |
297 | ||
298 | if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) { | |
299 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
300 | "P2P: Failed to start listen mode"); | |
301 | p2p->pending_listen_freq = 0; | |
302 | wpabuf_free(ies); | |
303 | return -1; | |
304 | } | |
305 | wpabuf_free(ies); | |
306 | ||
307 | p2p_set_state(p2p, P2P_LISTEN_ONLY); | |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
312 | ||
313 | static void p2p_device_clear_reported(struct p2p_data *p2p) | |
314 | { | |
315 | struct p2p_device *dev; | |
316 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) | |
317 | dev->flags &= ~P2P_DEV_REPORTED; | |
318 | } | |
319 | ||
320 | ||
321 | /** | |
322 | * p2p_get_device - Fetch a peer entry | |
323 | * @p2p: P2P module context from p2p_init() | |
324 | * @addr: P2P Device Address of the peer | |
325 | * Returns: Pointer to the device entry or %NULL if not found | |
326 | */ | |
327 | struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr) | |
328 | { | |
329 | struct p2p_device *dev; | |
330 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
c5db8e51 | 331 | if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0) |
b22128ef JM |
332 | return dev; |
333 | } | |
334 | return NULL; | |
335 | } | |
336 | ||
337 | ||
338 | /** | |
339 | * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address | |
340 | * @p2p: P2P module context from p2p_init() | |
341 | * @addr: P2P Interface Address of the peer | |
342 | * Returns: Pointer to the device entry or %NULL if not found | |
343 | */ | |
344 | struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p, | |
345 | const u8 *addr) | |
346 | { | |
347 | struct p2p_device *dev; | |
348 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
349 | if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0) | |
350 | return dev; | |
351 | } | |
352 | return NULL; | |
353 | } | |
354 | ||
355 | ||
356 | /** | |
357 | * p2p_create_device - Create a peer entry | |
358 | * @p2p: P2P module context from p2p_init() | |
359 | * @addr: P2P Device Address of the peer | |
360 | * Returns: Pointer to the device entry or %NULL on failure | |
361 | * | |
362 | * If there is already an entry for the peer, it will be returned instead of | |
363 | * creating a new one. | |
364 | */ | |
365 | static struct p2p_device * p2p_create_device(struct p2p_data *p2p, | |
366 | const u8 *addr) | |
367 | { | |
368 | struct p2p_device *dev, *oldest = NULL; | |
369 | size_t count = 0; | |
370 | ||
371 | dev = p2p_get_device(p2p, addr); | |
372 | if (dev) | |
373 | return dev; | |
374 | ||
375 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
376 | count++; | |
377 | if (oldest == NULL || | |
378 | os_time_before(&dev->last_seen, &oldest->last_seen)) | |
379 | oldest = dev; | |
380 | } | |
381 | if (count + 1 > p2p->cfg->max_peers && oldest) { | |
382 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
383 | "P2P: Remove oldest peer entry to make room for a new " | |
384 | "peer"); | |
385 | dl_list_del(&oldest->list); | |
386 | p2p_device_free(p2p, oldest); | |
387 | } | |
388 | ||
389 | dev = os_zalloc(sizeof(*dev)); | |
390 | if (dev == NULL) | |
391 | return NULL; | |
392 | dl_list_add(&p2p->devices, &dev->list); | |
c5db8e51 | 393 | os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN); |
b22128ef JM |
394 | |
395 | return dev; | |
396 | } | |
397 | ||
398 | ||
399 | static void p2p_copy_client_info(struct p2p_device *dev, | |
400 | struct p2p_client_info *cli) | |
401 | { | |
c5db8e51 KRK |
402 | os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len); |
403 | dev->info.device_name[cli->dev_name_len] = '\0'; | |
404 | dev->info.dev_capab = cli->dev_capab; | |
405 | dev->info.config_methods = cli->config_methods; | |
406 | os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8); | |
e57ae6e1 JMB |
407 | dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types; |
408 | os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types, | |
409 | dev->info.wps_sec_dev_type_list_len); | |
b22128ef JM |
410 | } |
411 | ||
412 | ||
413 | static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr, | |
414 | const u8 *go_interface_addr, int freq, | |
415 | const u8 *gi, size_t gi_len) | |
416 | { | |
417 | struct p2p_group_info info; | |
418 | size_t c; | |
419 | struct p2p_device *dev; | |
420 | ||
421 | if (gi == NULL) | |
422 | return 0; | |
423 | ||
424 | if (p2p_group_info_parse(gi, gi_len, &info) < 0) | |
425 | return -1; | |
426 | ||
427 | /* | |
428 | * Clear old data for this group; if the devices are still in the | |
429 | * group, the information will be restored in the loop following this. | |
430 | */ | |
431 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
b5472a45 | 432 | if (os_memcmp(dev->member_in_go_iface, go_interface_addr, |
b22128ef JM |
433 | ETH_ALEN) == 0) { |
434 | os_memset(dev->member_in_go_iface, 0, ETH_ALEN); | |
435 | os_memset(dev->member_in_go_dev, 0, ETH_ALEN); | |
436 | } | |
437 | } | |
438 | ||
439 | for (c = 0; c < info.num_clients; c++) { | |
440 | struct p2p_client_info *cli = &info.client[c]; | |
2f0c8936 MK |
441 | if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr, |
442 | ETH_ALEN) == 0) | |
443 | continue; /* ignore our own entry */ | |
b22128ef JM |
444 | dev = p2p_get_device(p2p, cli->p2p_device_addr); |
445 | if (dev) { | |
446 | /* | |
447 | * Update information only if we have not received this | |
448 | * directly from the client. | |
449 | */ | |
450 | if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY | | |
451 | P2P_DEV_PROBE_REQ_ONLY)) | |
452 | p2p_copy_client_info(dev, cli); | |
453 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { | |
454 | dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; | |
455 | } | |
456 | } else { | |
457 | dev = p2p_create_device(p2p, cli->p2p_device_addr); | |
458 | if (dev == NULL) | |
459 | continue; | |
460 | dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY; | |
461 | p2p_copy_client_info(dev, cli); | |
462 | dev->oper_freq = freq; | |
c5db8e51 KRK |
463 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, |
464 | dev->info.p2p_device_addr, | |
8fd7dc1b JB |
465 | &dev->info, 1); |
466 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; | |
b22128ef JM |
467 | } |
468 | ||
469 | os_memcpy(dev->interface_addr, cli->p2p_interface_addr, | |
470 | ETH_ALEN); | |
471 | os_get_time(&dev->last_seen); | |
472 | os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN); | |
473 | os_memcpy(dev->member_in_go_iface, go_interface_addr, | |
474 | ETH_ALEN); | |
475 | } | |
476 | ||
477 | return 0; | |
478 | } | |
479 | ||
480 | ||
b67d0d9e JM |
481 | static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req, |
482 | const struct p2p_message *msg) | |
483 | { | |
484 | os_memcpy(dev->info.device_name, msg->device_name, | |
485 | sizeof(dev->info.device_name)); | |
486 | ||
487 | if (msg->manufacturer && | |
488 | msg->manufacturer_len < sizeof(dev->info.manufacturer)) { | |
489 | os_memset(dev->info.manufacturer, 0, | |
490 | sizeof(dev->info.manufacturer)); | |
491 | os_memcpy(dev->info.manufacturer, msg->manufacturer, | |
492 | msg->manufacturer_len); | |
493 | } | |
494 | ||
495 | if (msg->model_name && | |
496 | msg->model_name_len < sizeof(dev->info.model_name)) { | |
497 | os_memset(dev->info.model_name, 0, | |
498 | sizeof(dev->info.model_name)); | |
499 | os_memcpy(dev->info.model_name, msg->model_name, | |
500 | msg->model_name_len); | |
501 | } | |
502 | ||
503 | if (msg->model_number && | |
504 | msg->model_number_len < sizeof(dev->info.model_number)) { | |
505 | os_memset(dev->info.model_number, 0, | |
506 | sizeof(dev->info.model_number)); | |
507 | os_memcpy(dev->info.model_number, msg->model_number, | |
508 | msg->model_number_len); | |
509 | } | |
510 | ||
511 | if (msg->serial_number && | |
512 | msg->serial_number_len < sizeof(dev->info.serial_number)) { | |
513 | os_memset(dev->info.serial_number, 0, | |
514 | sizeof(dev->info.serial_number)); | |
515 | os_memcpy(dev->info.serial_number, msg->serial_number, | |
516 | msg->serial_number_len); | |
517 | } | |
518 | ||
519 | if (msg->pri_dev_type) | |
520 | os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type, | |
521 | sizeof(dev->info.pri_dev_type)); | |
522 | else if (msg->wps_pri_dev_type) | |
523 | os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type, | |
524 | sizeof(dev->info.pri_dev_type)); | |
525 | ||
526 | if (msg->wps_sec_dev_type_list) { | |
527 | os_memcpy(dev->info.wps_sec_dev_type_list, | |
528 | msg->wps_sec_dev_type_list, | |
529 | msg->wps_sec_dev_type_list_len); | |
530 | dev->info.wps_sec_dev_type_list_len = | |
531 | msg->wps_sec_dev_type_list_len; | |
532 | } | |
533 | ||
534 | if (msg->capability) { | |
535 | dev->info.dev_capab = msg->capability[0]; | |
536 | dev->info.group_capab = msg->capability[1]; | |
537 | } | |
538 | ||
539 | if (msg->ext_listen_timing) { | |
540 | dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing); | |
541 | dev->ext_listen_interval = | |
542 | WPA_GET_LE16(msg->ext_listen_timing + 2); | |
543 | } | |
544 | ||
545 | if (!probe_req) { | |
546 | dev->info.config_methods = msg->config_methods ? | |
547 | msg->config_methods : msg->wps_config_methods; | |
548 | } | |
549 | } | |
550 | ||
551 | ||
b22128ef JM |
552 | /** |
553 | * p2p_add_device - Add peer entries based on scan results | |
554 | * @p2p: P2P module context from p2p_init() | |
555 | * @addr: Source address of Beacon or Probe Response frame (may be either | |
556 | * P2P Device Address or P2P Interface Address) | |
557 | * @level: Signal level (signal strength of the received frame from the peer) | |
558 | * @freq: Frequency on which the Beacon or Probe Response frame was received | |
559 | * @ies: IEs from the Beacon or Probe Response frame | |
560 | * @ies_len: Length of ies buffer in octets | |
561 | * Returns: 0 on success, -1 on failure | |
562 | * | |
563 | * If the scan result is for a GO, the clients in the group will also be added | |
17bef1e9 AC |
564 | * to the peer table. This function can also be used with some other frames |
565 | * like Provision Discovery Request that contains P2P Capability and P2P Device | |
566 | * Info attributes. | |
b22128ef | 567 | */ |
17bef1e9 AC |
568 | int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level, |
569 | const u8 *ies, size_t ies_len) | |
b22128ef JM |
570 | { |
571 | struct p2p_device *dev; | |
572 | struct p2p_message msg; | |
573 | const u8 *p2p_dev_addr; | |
6f2c0607 | 574 | int i; |
b22128ef JM |
575 | |
576 | os_memset(&msg, 0, sizeof(msg)); | |
577 | if (p2p_parse_ies(ies, ies_len, &msg)) { | |
578 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
579 | "P2P: Failed to parse P2P IE for a device entry"); | |
580 | p2p_parse_free(&msg); | |
581 | return -1; | |
582 | } | |
583 | ||
584 | if (msg.p2p_device_addr) | |
585 | p2p_dev_addr = msg.p2p_device_addr; | |
586 | else if (msg.device_id) | |
587 | p2p_dev_addr = msg.device_id; | |
588 | else { | |
589 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
590 | "P2P: Ignore scan data without P2P Device Info or " | |
591 | "P2P Device Id"); | |
592 | p2p_parse_free(&msg); | |
593 | return -1; | |
594 | } | |
595 | ||
80c9582a JM |
596 | if (!is_zero_ether_addr(p2p->peer_filter) && |
597 | os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) { | |
598 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer " | |
599 | "filter for " MACSTR " due to peer filter", | |
600 | MAC2STR(p2p_dev_addr)); | |
601 | return 0; | |
602 | } | |
603 | ||
b22128ef JM |
604 | dev = p2p_create_device(p2p, p2p_dev_addr); |
605 | if (dev == NULL) { | |
606 | p2p_parse_free(&msg); | |
607 | return -1; | |
608 | } | |
609 | os_get_time(&dev->last_seen); | |
610 | dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY); | |
611 | ||
612 | if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0) | |
613 | os_memcpy(dev->interface_addr, addr, ETH_ALEN); | |
614 | if (msg.ssid && | |
615 | (msg.ssid[1] != P2P_WILDCARD_SSID_LEN || | |
616 | os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) | |
617 | != 0)) { | |
618 | os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]); | |
619 | dev->oper_ssid_len = msg.ssid[1]; | |
620 | } | |
621 | ||
622 | if (freq >= 2412 && freq <= 2484 && msg.ds_params && | |
623 | *msg.ds_params >= 1 && *msg.ds_params <= 14) { | |
624 | int ds_freq; | |
625 | if (*msg.ds_params == 14) | |
626 | ds_freq = 2484; | |
627 | else | |
628 | ds_freq = 2407 + *msg.ds_params * 5; | |
629 | if (freq != ds_freq) { | |
630 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
631 | "P2P: Update Listen frequency based on DS " | |
632 | "Parameter Set IE: %d -> %d MHz", | |
633 | freq, ds_freq); | |
634 | freq = ds_freq; | |
635 | } | |
636 | } | |
637 | ||
638 | if (dev->listen_freq && dev->listen_freq != freq) { | |
639 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
640 | "P2P: Update Listen frequency based on scan " | |
641 | "results (" MACSTR " %d -> %d MHz (DS param %d)", | |
c5db8e51 KRK |
642 | MAC2STR(dev->info.p2p_device_addr), dev->listen_freq, |
643 | freq, msg.ds_params ? *msg.ds_params : -1); | |
b22128ef JM |
644 | } |
645 | dev->listen_freq = freq; | |
f8d0131a JM |
646 | if (msg.group_info) |
647 | dev->oper_freq = freq; | |
6402fc43 | 648 | dev->info.level = level; |
b22128ef | 649 | |
b67d0d9e | 650 | p2p_copy_wps_info(dev, 0, &msg); |
e57ae6e1 | 651 | |
10c5d2a5 | 652 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
6f2c0607 JMB |
653 | wpabuf_free(dev->info.wps_vendor_ext[i]); |
654 | dev->info.wps_vendor_ext[i] = NULL; | |
655 | } | |
656 | ||
10c5d2a5 | 657 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
6f2c0607 JMB |
658 | if (msg.wps_vendor_ext[i] == NULL) |
659 | break; | |
660 | dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy( | |
661 | msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]); | |
662 | if (dev->info.wps_vendor_ext[i] == NULL) | |
663 | break; | |
664 | } | |
665 | ||
b22128ef JM |
666 | p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, msg.group_info, |
667 | msg.group_info_len); | |
668 | ||
669 | p2p_parse_free(&msg); | |
670 | ||
671 | if (p2p_pending_sd_req(p2p, dev)) | |
672 | dev->flags |= P2P_DEV_SD_SCHEDULE; | |
673 | ||
674 | if (dev->flags & P2P_DEV_REPORTED) | |
675 | return 0; | |
676 | ||
677 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
678 | "P2P: Peer found with Listen frequency %d MHz", freq); | |
679 | if (dev->flags & P2P_DEV_USER_REJECTED) { | |
680 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
681 | "P2P: Do not report rejected device"); | |
682 | return 0; | |
683 | } | |
8fd7dc1b JB |
684 | |
685 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info, | |
686 | !(dev->flags & P2P_DEV_REPORTED_ONCE)); | |
687 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; | |
b22128ef JM |
688 | |
689 | return 0; | |
690 | } | |
691 | ||
692 | ||
693 | static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev) | |
694 | { | |
6f2c0607 JMB |
695 | int i; |
696 | ||
5cfda25e JJ |
697 | if (p2p->go_neg_peer == dev) { |
698 | /* | |
699 | * If GO Negotiation is in progress, report that it has failed. | |
700 | */ | |
701 | p2p_go_neg_failed(p2p, dev, -1); | |
b22128ef | 702 | p2p->go_neg_peer = NULL; |
5cfda25e | 703 | } |
b22128ef JM |
704 | if (p2p->invite_peer == dev) |
705 | p2p->invite_peer = NULL; | |
706 | if (p2p->sd_peer == dev) | |
707 | p2p->sd_peer = NULL; | |
708 | if (p2p->pending_client_disc_go == dev) | |
709 | p2p->pending_client_disc_go = NULL; | |
710 | ||
f5fc6032 AC |
711 | /* dev_lost() device, but only if it was previously dev_found() */ |
712 | if (dev->flags & P2P_DEV_REPORTED_ONCE) | |
713 | p2p->cfg->dev_lost(p2p->cfg->cb_ctx, | |
714 | dev->info.p2p_device_addr); | |
56eeb8f2 | 715 | |
10c5d2a5 | 716 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
6f2c0607 JMB |
717 | wpabuf_free(dev->info.wps_vendor_ext[i]); |
718 | dev->info.wps_vendor_ext[i] = NULL; | |
719 | } | |
720 | ||
b22128ef JM |
721 | os_free(dev); |
722 | } | |
723 | ||
724 | ||
725 | static int p2p_get_next_prog_freq(struct p2p_data *p2p) | |
726 | { | |
727 | struct p2p_channels *c; | |
728 | struct p2p_reg_class *cla; | |
729 | size_t cl, ch; | |
730 | int found = 0; | |
731 | u8 reg_class; | |
732 | u8 channel; | |
733 | int freq; | |
734 | ||
735 | c = &p2p->cfg->channels; | |
736 | for (cl = 0; cl < c->reg_classes; cl++) { | |
737 | cla = &c->reg_class[cl]; | |
738 | if (cla->reg_class != p2p->last_prog_scan_class) | |
739 | continue; | |
740 | for (ch = 0; ch < cla->channels; ch++) { | |
741 | if (cla->channel[ch] == p2p->last_prog_scan_chan) { | |
742 | found = 1; | |
743 | break; | |
744 | } | |
745 | } | |
746 | if (found) | |
747 | break; | |
748 | } | |
749 | ||
750 | if (!found) { | |
751 | /* Start from beginning */ | |
752 | reg_class = c->reg_class[0].reg_class; | |
753 | channel = c->reg_class[0].channel[0]; | |
754 | } else { | |
755 | /* Pick the next channel */ | |
756 | ch++; | |
757 | if (ch == cla->channels) { | |
758 | cl++; | |
759 | if (cl == c->reg_classes) | |
760 | cl = 0; | |
761 | ch = 0; | |
762 | } | |
763 | reg_class = c->reg_class[cl].reg_class; | |
764 | channel = c->reg_class[cl].channel[ch]; | |
765 | } | |
766 | ||
767 | freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel); | |
768 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search " | |
769 | "channel: reg_class %u channel %u -> %d MHz", | |
770 | reg_class, channel, freq); | |
771 | p2p->last_prog_scan_class = reg_class; | |
772 | p2p->last_prog_scan_chan = channel; | |
773 | ||
774 | if (freq == 2412 || freq == 2437 || freq == 2462) | |
775 | return 0; /* No need to add social channels */ | |
776 | return freq; | |
777 | } | |
778 | ||
779 | ||
780 | static void p2p_search(struct p2p_data *p2p) | |
781 | { | |
782 | int freq = 0; | |
783 | enum p2p_scan_type type; | |
784 | ||
785 | if (p2p->drv_in_listen) { | |
786 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still " | |
787 | "in Listen state - wait for it to end before " | |
788 | "continuing"); | |
789 | return; | |
790 | } | |
791 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); | |
792 | ||
793 | if (p2p->go_neg_peer) { | |
794 | /* | |
795 | * Only scan the known listen frequency of the peer | |
796 | * during GO Negotiation start. | |
797 | */ | |
798 | freq = p2p->go_neg_peer->listen_freq; | |
799 | if (freq <= 0) | |
800 | freq = p2p->go_neg_peer->oper_freq; | |
801 | type = P2P_SCAN_SPECIFIC; | |
802 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search " | |
803 | "for freq %u (GO Neg)", freq); | |
804 | } else if (p2p->invite_peer) { | |
805 | /* | |
806 | * Only scan the known listen frequency of the peer | |
807 | * during Invite start. | |
808 | */ | |
809 | freq = p2p->invite_peer->listen_freq; | |
810 | if (freq <= 0) | |
811 | freq = p2p->invite_peer->oper_freq; | |
812 | type = P2P_SCAN_SPECIFIC; | |
813 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search " | |
814 | "for freq %u (Invite)", freq); | |
815 | } else if (p2p->find_type == P2P_FIND_PROGRESSIVE && | |
816 | (freq = p2p_get_next_prog_freq(p2p)) > 0) { | |
817 | type = P2P_SCAN_SOCIAL_PLUS_ONE; | |
818 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search " | |
819 | "(+ freq %u)", freq); | |
820 | } else { | |
821 | type = P2P_SCAN_SOCIAL; | |
822 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search"); | |
823 | } | |
824 | ||
046ef4aa | 825 | if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq, |
6d92fa6e JM |
826 | p2p->num_req_dev_types, p2p->req_dev_types, |
827 | p2p->find_dev_id) < 0) { | |
b22128ef JM |
828 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
829 | "P2P: Scan request failed"); | |
830 | p2p_continue_find(p2p); | |
40c03fd4 JM |
831 | } else { |
832 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan"); | |
833 | p2p->p2p_scan_running = 1; | |
834 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
835 | eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout, | |
836 | p2p, NULL); | |
b22128ef JM |
837 | } |
838 | } | |
839 | ||
840 | ||
841 | static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx) | |
842 | { | |
843 | struct p2p_data *p2p = eloop_ctx; | |
844 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop"); | |
845 | p2p_stop_find(p2p); | |
846 | } | |
847 | ||
848 | ||
849 | static int p2p_run_after_scan(struct p2p_data *p2p) | |
850 | { | |
851 | struct p2p_device *dev; | |
852 | enum p2p_after_scan op; | |
853 | ||
3f9285ff | 854 | if (p2p->after_scan_tx) { |
3f9285ff JM |
855 | /* TODO: schedule p2p_run_after_scan to be called from TX |
856 | * status callback(?) */ | |
857 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending " | |
858 | "Action frame at p2p_scan completion"); | |
46eeedac JM |
859 | p2p->cfg->send_action(p2p->cfg->cb_ctx, |
860 | p2p->after_scan_tx->freq, | |
861 | p2p->after_scan_tx->dst, | |
862 | p2p->after_scan_tx->src, | |
863 | p2p->after_scan_tx->bssid, | |
864 | (u8 *) (p2p->after_scan_tx + 1), | |
865 | p2p->after_scan_tx->len, | |
866 | p2p->after_scan_tx->wait_time); | |
3f9285ff JM |
867 | os_free(p2p->after_scan_tx); |
868 | p2p->after_scan_tx = NULL; | |
869 | return 1; | |
870 | } | |
871 | ||
b22128ef JM |
872 | op = p2p->start_after_scan; |
873 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; | |
874 | switch (op) { | |
875 | case P2P_AFTER_SCAN_NOTHING: | |
876 | break; | |
877 | case P2P_AFTER_SCAN_LISTEN: | |
878 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously " | |
879 | "requested Listen state"); | |
880 | p2p_listen(p2p, p2p->pending_listen_sec * 1000 + | |
881 | p2p->pending_listen_usec / 1000); | |
882 | return 1; | |
883 | case P2P_AFTER_SCAN_CONNECT: | |
884 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously " | |
885 | "requested connect with " MACSTR, | |
886 | MAC2STR(p2p->after_scan_peer)); | |
887 | dev = p2p_get_device(p2p, p2p->after_scan_peer); | |
888 | if (dev == NULL) { | |
889 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not " | |
890 | "known anymore"); | |
891 | break; | |
892 | } | |
893 | p2p_connect_send(p2p, dev); | |
894 | return 1; | |
895 | } | |
896 | ||
897 | return 0; | |
898 | } | |
899 | ||
900 | ||
b22128ef JM |
901 | static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx) |
902 | { | |
903 | struct p2p_data *p2p = eloop_ctx; | |
904 | int running; | |
905 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout " | |
906 | "(running=%d)", p2p->p2p_scan_running); | |
907 | running = p2p->p2p_scan_running; | |
908 | /* Make sure we recover from missed scan results callback */ | |
909 | p2p->p2p_scan_running = 0; | |
910 | ||
911 | if (running) | |
912 | p2p_run_after_scan(p2p); | |
913 | } | |
914 | ||
915 | ||
046ef4aa JMB |
916 | static void p2p_free_req_dev_types(struct p2p_data *p2p) |
917 | { | |
918 | p2p->num_req_dev_types = 0; | |
919 | os_free(p2p->req_dev_types); | |
920 | p2p->req_dev_types = NULL; | |
921 | } | |
922 | ||
923 | ||
b22128ef | 924 | int p2p_find(struct p2p_data *p2p, unsigned int timeout, |
046ef4aa | 925 | enum p2p_discovery_type type, |
6d92fa6e JM |
926 | unsigned int num_req_dev_types, const u8 *req_dev_types, |
927 | const u8 *dev_id) | |
b22128ef JM |
928 | { |
929 | int res; | |
930 | ||
931 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)", | |
932 | type); | |
933 | if (p2p->p2p_scan_running) { | |
934 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is " | |
935 | "already running"); | |
936 | } | |
046ef4aa JMB |
937 | |
938 | p2p_free_req_dev_types(p2p); | |
939 | if (req_dev_types && num_req_dev_types) { | |
940 | p2p->req_dev_types = os_malloc(num_req_dev_types * | |
941 | WPS_DEV_TYPE_LEN); | |
942 | if (p2p->req_dev_types == NULL) | |
943 | return -1; | |
944 | os_memcpy(p2p->req_dev_types, req_dev_types, | |
945 | num_req_dev_types * WPS_DEV_TYPE_LEN); | |
946 | p2p->num_req_dev_types = num_req_dev_types; | |
947 | } | |
948 | ||
6d92fa6e JM |
949 | if (dev_id) { |
950 | os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN); | |
951 | p2p->find_dev_id = p2p->find_dev_id_buf; | |
952 | } else | |
953 | p2p->find_dev_id = NULL; | |
954 | ||
b22128ef JM |
955 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; |
956 | p2p_clear_timeout(p2p); | |
957 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); | |
958 | p2p->find_type = type; | |
959 | p2p_device_clear_reported(p2p); | |
960 | p2p_set_state(p2p, P2P_SEARCH); | |
961 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); | |
39185dfa | 962 | p2p->last_p2p_find_timeout = timeout; |
b22128ef JM |
963 | if (timeout) |
964 | eloop_register_timeout(timeout, 0, p2p_find_timeout, | |
965 | p2p, NULL); | |
966 | switch (type) { | |
967 | case P2P_FIND_START_WITH_FULL: | |
968 | case P2P_FIND_PROGRESSIVE: | |
046ef4aa JMB |
969 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0, |
970 | p2p->num_req_dev_types, | |
6d92fa6e | 971 | p2p->req_dev_types, dev_id); |
b22128ef JM |
972 | break; |
973 | case P2P_FIND_ONLY_SOCIAL: | |
046ef4aa JMB |
974 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0, |
975 | p2p->num_req_dev_types, | |
6d92fa6e | 976 | p2p->req_dev_types, dev_id); |
b22128ef JM |
977 | break; |
978 | default: | |
979 | return -1; | |
980 | } | |
981 | ||
982 | if (res == 0) { | |
983 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan"); | |
984 | p2p->p2p_scan_running = 1; | |
985 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
986 | eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout, | |
987 | p2p, NULL); | |
39185dfa JM |
988 | } else if (res == 1) { |
989 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start " | |
990 | "p2p_scan at this point - will try again after " | |
991 | "previous scan completes"); | |
992 | res = 0; | |
993 | p2p_set_state(p2p, P2P_SEARCH_WHEN_READY); | |
994 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); | |
b22128ef JM |
995 | } else { |
996 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start " | |
997 | "p2p_scan"); | |
0c96fd6d JM |
998 | p2p_set_state(p2p, P2P_IDLE); |
999 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); | |
b22128ef JM |
1000 | } |
1001 | ||
1002 | return res; | |
1003 | } | |
1004 | ||
1005 | ||
39185dfa JM |
1006 | int p2p_other_scan_completed(struct p2p_data *p2p) |
1007 | { | |
1008 | if (p2p->state != P2P_SEARCH_WHEN_READY) | |
1009 | return 0; | |
1010 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find " | |
1011 | "now that previous scan was completed"); | |
1012 | if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type, | |
6d92fa6e JM |
1013 | p2p->num_req_dev_types, p2p->req_dev_types, |
1014 | p2p->find_dev_id) < 0) | |
39185dfa JM |
1015 | return 0; |
1016 | return 1; | |
1017 | } | |
1018 | ||
1019 | ||
0b8889d8 | 1020 | void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq) |
b22128ef JM |
1021 | { |
1022 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find"); | |
1023 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); | |
1024 | p2p_clear_timeout(p2p); | |
8aebb0e4 JJ |
1025 | if (p2p->state == P2P_SEARCH) |
1026 | wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED); | |
b22128ef | 1027 | p2p_set_state(p2p, P2P_IDLE); |
046ef4aa | 1028 | p2p_free_req_dev_types(p2p); |
b22128ef JM |
1029 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; |
1030 | p2p->go_neg_peer = NULL; | |
1031 | p2p->sd_peer = NULL; | |
1032 | p2p->invite_peer = NULL; | |
0b8889d8 JM |
1033 | if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) { |
1034 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen " | |
1035 | "since we are on correct channel for response"); | |
1036 | return; | |
1037 | } | |
54b8f994 JM |
1038 | if (p2p->drv_in_listen) { |
1039 | /* | |
1040 | * The driver may not deliver callback to p2p_listen_end() | |
1041 | * when the operation gets canceled, so clear the internal | |
1042 | * variable that is tracking driver state. | |
1043 | */ | |
1044 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear " | |
1045 | "drv_in_listen (%d)", p2p->drv_in_listen); | |
1046 | p2p->drv_in_listen = 0; | |
1047 | } | |
b22128ef JM |
1048 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
1049 | } | |
1050 | ||
1051 | ||
0b8889d8 JM |
1052 | void p2p_stop_find(struct p2p_data *p2p) |
1053 | { | |
1054 | p2p_stop_find_for_freq(p2p, 0); | |
1055 | } | |
1056 | ||
1057 | ||
7861cb08 | 1058 | static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq) |
b22128ef | 1059 | { |
b22128ef | 1060 | if (force_freq) { |
1e19f734 | 1061 | u8 op_reg_class, op_channel; |
b22128ef | 1062 | if (p2p_freq_to_channel(p2p->cfg->country, force_freq, |
1e19f734 | 1063 | &op_reg_class, &op_channel) < 0) { |
b22128ef JM |
1064 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
1065 | "P2P: Unsupported frequency %u MHz", | |
1066 | force_freq); | |
1067 | return -1; | |
1068 | } | |
1e19f734 JM |
1069 | if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class, |
1070 | op_channel)) { | |
1071 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1072 | "P2P: Frequency %u MHz (oper_class %u " | |
1073 | "channel %u) not allowed for P2P", | |
1074 | force_freq, op_reg_class, op_channel); | |
1075 | return -1; | |
1076 | } | |
1077 | p2p->op_reg_class = op_reg_class; | |
1078 | p2p->op_channel = op_channel; | |
b22128ef JM |
1079 | p2p->channels.reg_classes = 1; |
1080 | p2p->channels.reg_class[0].channels = 1; | |
1081 | p2p->channels.reg_class[0].reg_class = p2p->op_reg_class; | |
1082 | p2p->channels.reg_class[0].channel[0] = p2p->op_channel; | |
1083 | } else { | |
7cfc4ac3 AGS |
1084 | u8 op_reg_class, op_channel; |
1085 | ||
1086 | if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 && | |
1087 | p2p_supported_freq(p2p, p2p->best_freq_overall) && | |
1088 | p2p_freq_to_channel(p2p->cfg->country, | |
1089 | p2p->best_freq_overall, | |
1090 | &op_reg_class, &op_channel) == 0) { | |
1091 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1092 | "P2P: Select best overall channel as " | |
1093 | "operating channel preference"); | |
1094 | p2p->op_reg_class = op_reg_class; | |
1095 | p2p->op_channel = op_channel; | |
1096 | } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 && | |
1097 | p2p_supported_freq(p2p, p2p->best_freq_5) && | |
1098 | p2p_freq_to_channel(p2p->cfg->country, | |
1099 | p2p->best_freq_5, | |
1100 | &op_reg_class, &op_channel) == | |
1101 | 0) { | |
1102 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1103 | "P2P: Select best 5 GHz channel as " | |
1104 | "operating channel preference"); | |
1105 | p2p->op_reg_class = op_reg_class; | |
1106 | p2p->op_channel = op_channel; | |
1107 | } else if (!p2p->cfg->cfg_op_channel && | |
1108 | p2p->best_freq_24 > 0 && | |
1109 | p2p_supported_freq(p2p, p2p->best_freq_24) && | |
1110 | p2p_freq_to_channel(p2p->cfg->country, | |
1111 | p2p->best_freq_24, | |
1112 | &op_reg_class, &op_channel) == | |
1113 | 0) { | |
1114 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1115 | "P2P: Select best 2.4 GHz channel as " | |
1116 | "operating channel preference"); | |
1117 | p2p->op_reg_class = op_reg_class; | |
1118 | p2p->op_channel = op_channel; | |
1119 | } else { | |
1120 | p2p->op_reg_class = p2p->cfg->op_reg_class; | |
1121 | p2p->op_channel = p2p->cfg->op_channel; | |
1122 | } | |
1123 | ||
b22128ef JM |
1124 | os_memcpy(&p2p->channels, &p2p->cfg->channels, |
1125 | sizeof(struct p2p_channels)); | |
1126 | } | |
1127 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1128 | "P2P: Own preference for operation channel: " | |
7cfc4ac3 | 1129 | "Operating Class %u Channel %u%s", |
b22128ef JM |
1130 | p2p->op_reg_class, p2p->op_channel, |
1131 | force_freq ? " (forced)" : ""); | |
1132 | ||
7861cb08 JM |
1133 | return 0; |
1134 | } | |
1135 | ||
1136 | ||
acc247b2 JM |
1137 | static void p2p_set_dev_persistent(struct p2p_device *dev, |
1138 | int persistent_group) | |
1139 | { | |
1140 | switch (persistent_group) { | |
1141 | case 0: | |
1142 | dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP | | |
1143 | P2P_DEV_PREFER_PERSISTENT_RECONN); | |
1144 | break; | |
1145 | case 1: | |
1146 | dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP; | |
1147 | dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN; | |
1148 | break; | |
1149 | case 2: | |
1150 | dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP | | |
1151 | P2P_DEV_PREFER_PERSISTENT_RECONN; | |
1152 | break; | |
1153 | } | |
1154 | } | |
1155 | ||
1156 | ||
7861cb08 JM |
1157 | int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr, |
1158 | enum p2p_wps_method wps_method, | |
1159 | int go_intent, const u8 *own_interface_addr, | |
1160 | unsigned int force_freq, int persistent_group) | |
1161 | { | |
1162 | struct p2p_device *dev; | |
1163 | ||
1164 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1165 | "P2P: Request to start group negotiation - peer=" MACSTR | |
1166 | " GO Intent=%d Intended Interface Address=" MACSTR | |
1167 | " wps_method=%d persistent_group=%d", | |
1168 | MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), | |
1169 | wps_method, persistent_group); | |
1170 | ||
1171 | if (p2p_prepare_channel(p2p, force_freq) < 0) | |
1172 | return -1; | |
1173 | ||
4458d915 | 1174 | p2p->ssid_set = 0; |
b22128ef JM |
1175 | dev = p2p_get_device(p2p, peer_addr); |
1176 | if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) { | |
1177 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1178 | "P2P: Cannot connect to unknown P2P Device " MACSTR, | |
1179 | MAC2STR(peer_addr)); | |
1180 | return -1; | |
1181 | } | |
1182 | ||
1183 | if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) { | |
c5db8e51 KRK |
1184 | if (!(dev->info.dev_capab & |
1185 | P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) { | |
b22128ef JM |
1186 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
1187 | "P2P: Cannot connect to P2P Device " MACSTR | |
1188 | " that is in a group and is not discoverable", | |
1189 | MAC2STR(peer_addr)); | |
1190 | return -1; | |
1191 | } | |
1192 | if (dev->oper_freq <= 0) { | |
1193 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1194 | "P2P: Cannot connect to P2P Device " MACSTR | |
1195 | " with incomplete information", | |
1196 | MAC2STR(peer_addr)); | |
1197 | return -1; | |
1198 | } | |
1199 | ||
1200 | /* | |
1201 | * First, try to connect directly. If the peer does not | |
1202 | * acknowledge frames, assume it is sleeping and use device | |
1203 | * discoverability via the GO at that point. | |
1204 | */ | |
1205 | } | |
1206 | ||
1207 | dev->flags &= ~P2P_DEV_NOT_YET_READY; | |
1208 | dev->flags &= ~P2P_DEV_USER_REJECTED; | |
1209 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; | |
1210 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; | |
9dac8c3e | 1211 | dev->connect_reqs = 0; |
b22128ef JM |
1212 | dev->go_neg_req_sent = 0; |
1213 | dev->go_state = UNKNOWN_GO; | |
acc247b2 | 1214 | p2p_set_dev_persistent(dev, persistent_group); |
b22128ef JM |
1215 | p2p->go_intent = go_intent; |
1216 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); | |
1217 | ||
1218 | if (p2p->state != P2P_IDLE) | |
1219 | p2p_stop_find(p2p); | |
1220 | ||
f44ae207 JM |
1221 | if (p2p->after_scan_tx) { |
1222 | /* | |
1223 | * We need to drop the pending frame to avoid issues with the | |
1224 | * new GO Negotiation, e.g., when the pending frame was from a | |
1225 | * previous attempt at starting a GO Negotiation. | |
1226 | */ | |
1227 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped " | |
1228 | "previous pending Action frame TX that was waiting " | |
1229 | "for p2p_scan completion"); | |
1230 | os_free(p2p->after_scan_tx); | |
1231 | p2p->after_scan_tx = NULL; | |
1232 | } | |
1233 | ||
b22128ef JM |
1234 | dev->wps_method = wps_method; |
1235 | dev->status = P2P_SC_SUCCESS; | |
d5b20a73 JM |
1236 | |
1237 | if (force_freq) | |
1238 | dev->flags |= P2P_DEV_FORCE_FREQ; | |
1239 | else | |
1240 | dev->flags &= ~P2P_DEV_FORCE_FREQ; | |
1241 | ||
b22128ef JM |
1242 | if (p2p->p2p_scan_running) { |
1243 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1244 | "P2P: p2p_scan running - delay connect send"); | |
1245 | p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT; | |
1246 | os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN); | |
1247 | return 0; | |
1248 | } | |
1249 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; | |
1250 | ||
1251 | return p2p_connect_send(p2p, dev); | |
1252 | } | |
1253 | ||
1254 | ||
1255 | int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr, | |
1256 | enum p2p_wps_method wps_method, | |
1257 | int go_intent, const u8 *own_interface_addr, | |
1258 | unsigned int force_freq, int persistent_group) | |
1259 | { | |
1260 | struct p2p_device *dev; | |
1261 | ||
1262 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1263 | "P2P: Request to authorize group negotiation - peer=" MACSTR | |
1264 | " GO Intent=%d Intended Interface Address=" MACSTR | |
1265 | " wps_method=%d persistent_group=%d", | |
1266 | MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), | |
1267 | wps_method, persistent_group); | |
1268 | ||
7861cb08 JM |
1269 | if (p2p_prepare_channel(p2p, force_freq) < 0) |
1270 | return -1; | |
b22128ef JM |
1271 | |
1272 | dev = p2p_get_device(p2p, peer_addr); | |
1273 | if (dev == NULL) { | |
1274 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1275 | "P2P: Cannot authorize unknown P2P Device " MACSTR, | |
1276 | MAC2STR(peer_addr)); | |
1277 | return -1; | |
1278 | } | |
1279 | ||
1280 | dev->flags &= ~P2P_DEV_NOT_YET_READY; | |
1281 | dev->flags &= ~P2P_DEV_USER_REJECTED; | |
1282 | dev->go_neg_req_sent = 0; | |
1283 | dev->go_state = UNKNOWN_GO; | |
acc247b2 | 1284 | p2p_set_dev_persistent(dev, persistent_group); |
b22128ef JM |
1285 | p2p->go_intent = go_intent; |
1286 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); | |
1287 | ||
1288 | dev->wps_method = wps_method; | |
1289 | dev->status = P2P_SC_SUCCESS; | |
1290 | ||
d5b20a73 JM |
1291 | if (force_freq) |
1292 | dev->flags |= P2P_DEV_FORCE_FREQ; | |
1293 | else | |
1294 | dev->flags &= ~P2P_DEV_FORCE_FREQ; | |
1295 | ||
b22128ef JM |
1296 | return 0; |
1297 | } | |
1298 | ||
1299 | ||
1300 | void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr, | |
1301 | struct p2p_device *dev, struct p2p_message *msg) | |
1302 | { | |
1303 | os_get_time(&dev->last_seen); | |
1304 | ||
b67d0d9e | 1305 | p2p_copy_wps_info(dev, 0, msg); |
e57ae6e1 | 1306 | |
b22128ef JM |
1307 | if (msg->listen_channel) { |
1308 | int freq; | |
1309 | freq = p2p_channel_to_freq((char *) msg->listen_channel, | |
1310 | msg->listen_channel[3], | |
1311 | msg->listen_channel[4]); | |
1312 | if (freq < 0) { | |
1313 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1314 | "P2P: Unknown peer Listen channel: " | |
1315 | "country=%c%c(0x%02x) reg_class=%u channel=%u", | |
1316 | msg->listen_channel[0], | |
1317 | msg->listen_channel[1], | |
1318 | msg->listen_channel[2], | |
1319 | msg->listen_channel[3], | |
1320 | msg->listen_channel[4]); | |
1321 | } else { | |
1322 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update " | |
1323 | "peer " MACSTR " Listen channel: %u -> %u MHz", | |
c5db8e51 | 1324 | MAC2STR(dev->info.p2p_device_addr), |
b22128ef JM |
1325 | dev->listen_freq, freq); |
1326 | dev->listen_freq = freq; | |
1327 | } | |
1328 | } | |
b22128ef JM |
1329 | |
1330 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { | |
1331 | dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; | |
1332 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1333 | "P2P: Completed device entry based on data from " | |
1334 | "GO Negotiation Request"); | |
1335 | } else { | |
1336 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1337 | "P2P: Created device entry based on GO Neg Req: " | |
1338 | MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' " | |
1339 | "listen_freq=%d", | |
c5db8e51 KRK |
1340 | MAC2STR(dev->info.p2p_device_addr), |
1341 | dev->info.dev_capab, dev->info.group_capab, | |
1342 | dev->info.device_name, dev->listen_freq); | |
b22128ef JM |
1343 | } |
1344 | ||
1345 | dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY; | |
1346 | ||
1347 | if (dev->flags & P2P_DEV_USER_REJECTED) { | |
1348 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1349 | "P2P: Do not report rejected device"); | |
1350 | return; | |
1351 | } | |
1352 | ||
8fd7dc1b JB |
1353 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info, |
1354 | !(dev->flags & P2P_DEV_REPORTED_ONCE)); | |
1355 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; | |
b22128ef JM |
1356 | } |
1357 | ||
1358 | ||
1359 | void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len) | |
1360 | { | |
1361 | os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); | |
1362 | p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2); | |
1363 | os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2], | |
1364 | p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len); | |
1365 | *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len; | |
1366 | } | |
1367 | ||
1368 | ||
1369 | int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params) | |
1370 | { | |
1371 | p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len); | |
1372 | p2p_random(params->passphrase, 8); | |
1373 | return 0; | |
1374 | } | |
1375 | ||
1376 | ||
1377 | void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer) | |
1378 | { | |
1379 | struct p2p_go_neg_results res; | |
1380 | int go = peer->go_state == LOCAL_GO; | |
1381 | struct p2p_channels intersection; | |
1382 | int freqs; | |
1383 | size_t i, j; | |
1384 | ||
1385 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1386 | "P2P: GO Negotiation with " MACSTR " completed (%s will be " | |
c5db8e51 | 1387 | "GO)", MAC2STR(peer->info.p2p_device_addr), |
b22128ef JM |
1388 | go ? "local end" : "peer"); |
1389 | ||
1390 | os_memset(&res, 0, sizeof(res)); | |
1391 | res.role_go = go; | |
c5db8e51 | 1392 | os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN); |
b22128ef JM |
1393 | os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN); |
1394 | res.wps_method = peer->wps_method; | |
acc247b2 JM |
1395 | if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) { |
1396 | if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN) | |
1397 | res.persistent_group = 2; | |
1398 | else | |
1399 | res.persistent_group = 1; | |
1400 | } | |
b22128ef JM |
1401 | |
1402 | if (go) { | |
1403 | /* Setup AP mode for WPS provisioning */ | |
1404 | res.freq = p2p_channel_to_freq(p2p->cfg->country, | |
1405 | p2p->op_reg_class, | |
1406 | p2p->op_channel); | |
1407 | os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); | |
1408 | res.ssid_len = p2p->ssid_len; | |
1409 | p2p_random(res.passphrase, 8); | |
e9a7ae41 | 1410 | } else { |
b22128ef | 1411 | res.freq = peer->oper_freq; |
e9a7ae41 JM |
1412 | if (p2p->ssid_len) { |
1413 | os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); | |
1414 | res.ssid_len = p2p->ssid_len; | |
1415 | } | |
1416 | } | |
b22128ef JM |
1417 | |
1418 | p2p_channels_intersect(&p2p->channels, &peer->channels, | |
1419 | &intersection); | |
1420 | freqs = 0; | |
1421 | for (i = 0; i < intersection.reg_classes; i++) { | |
1422 | struct p2p_reg_class *c = &intersection.reg_class[i]; | |
1423 | if (freqs + 1 == P2P_MAX_CHANNELS) | |
1424 | break; | |
1425 | for (j = 0; j < c->channels; j++) { | |
1426 | int freq; | |
1427 | if (freqs + 1 == P2P_MAX_CHANNELS) | |
1428 | break; | |
1429 | freq = p2p_channel_to_freq(peer->country, c->reg_class, | |
1430 | c->channel[j]); | |
1431 | if (freq < 0) | |
1432 | continue; | |
1433 | res.freq_list[freqs++] = freq; | |
1434 | } | |
1435 | } | |
1436 | ||
ae3e3421 JM |
1437 | res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout; |
1438 | ||
b22128ef | 1439 | p2p_clear_timeout(p2p); |
4458d915 | 1440 | p2p->ssid_set = 0; |
b22128ef JM |
1441 | peer->go_neg_req_sent = 0; |
1442 | peer->wps_method = WPS_NOT_READY; | |
1443 | ||
1444 | p2p_set_state(p2p, P2P_PROVISIONING); | |
1445 | p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); | |
1446 | } | |
1447 | ||
1448 | ||
1449 | static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa, | |
1450 | const u8 *data, size_t len, int rx_freq) | |
1451 | { | |
1452 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1453 | "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa)); | |
1454 | wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len); | |
1455 | ||
1456 | if (len < 1) | |
1457 | return; | |
1458 | ||
1459 | switch (data[0]) { | |
1460 | case P2P_GO_NEG_REQ: | |
1461 | p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq); | |
1462 | break; | |
1463 | case P2P_GO_NEG_RESP: | |
1464 | p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq); | |
1465 | break; | |
1466 | case P2P_GO_NEG_CONF: | |
1467 | p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1); | |
1468 | break; | |
1469 | case P2P_INVITATION_REQ: | |
1470 | p2p_process_invitation_req(p2p, sa, data + 1, len - 1, | |
1471 | rx_freq); | |
1472 | break; | |
1473 | case P2P_INVITATION_RESP: | |
1474 | p2p_process_invitation_resp(p2p, sa, data + 1, len - 1); | |
1475 | break; | |
1476 | case P2P_PROV_DISC_REQ: | |
1477 | p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq); | |
1478 | break; | |
1479 | case P2P_PROV_DISC_RESP: | |
1480 | p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1); | |
1481 | break; | |
1482 | case P2P_DEV_DISC_REQ: | |
1483 | p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq); | |
1484 | break; | |
1485 | case P2P_DEV_DISC_RESP: | |
1486 | p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1); | |
1487 | break; | |
1488 | default: | |
1489 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1490 | "P2P: Unsupported P2P Public Action frame type %d", | |
1491 | data[0]); | |
1492 | break; | |
1493 | } | |
1494 | } | |
1495 | ||
1496 | ||
19df9b07 JM |
1497 | static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, |
1498 | const u8 *sa, const u8 *bssid, const u8 *data, | |
1499 | size_t len, int freq) | |
b22128ef JM |
1500 | { |
1501 | if (len < 1) | |
1502 | return; | |
1503 | ||
1504 | switch (data[0]) { | |
1505 | case WLAN_PA_VENDOR_SPECIFIC: | |
1506 | data++; | |
1507 | len--; | |
1508 | if (len < 3) | |
1509 | return; | |
1510 | if (WPA_GET_BE24(data) != OUI_WFA) | |
1511 | return; | |
1512 | ||
1513 | data += 3; | |
1514 | len -= 3; | |
1515 | if (len < 1) | |
1516 | return; | |
1517 | ||
1518 | if (*data != P2P_OUI_TYPE) | |
1519 | return; | |
1520 | ||
1521 | p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq); | |
1522 | break; | |
1523 | case WLAN_PA_GAS_INITIAL_REQ: | |
1524 | p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq); | |
1525 | break; | |
1526 | case WLAN_PA_GAS_INITIAL_RESP: | |
18708aad JM |
1527 | p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq); |
1528 | break; | |
1529 | case WLAN_PA_GAS_COMEBACK_REQ: | |
1530 | p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq); | |
1531 | break; | |
1532 | case WLAN_PA_GAS_COMEBACK_RESP: | |
1533 | p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq); | |
b22128ef JM |
1534 | break; |
1535 | } | |
1536 | } | |
1537 | ||
1538 | ||
1539 | void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa, | |
1540 | const u8 *bssid, u8 category, | |
1541 | const u8 *data, size_t len, int freq) | |
1542 | { | |
1543 | if (category == WLAN_ACTION_PUBLIC) { | |
1544 | p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq); | |
1545 | return; | |
1546 | } | |
1547 | ||
1548 | if (category != WLAN_ACTION_VENDOR_SPECIFIC) | |
1549 | return; | |
1550 | ||
1551 | if (len < 4) | |
1552 | return; | |
1553 | ||
1554 | if (WPA_GET_BE24(data) != OUI_WFA) | |
1555 | return; | |
1556 | data += 3; | |
1557 | len -= 3; | |
1558 | ||
1559 | if (*data != P2P_OUI_TYPE) | |
1560 | return; | |
1561 | data++; | |
1562 | len--; | |
1563 | ||
1564 | /* P2P action frame */ | |
1565 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1566 | "P2P: RX P2P Action from " MACSTR, MAC2STR(sa)); | |
1567 | wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len); | |
1568 | ||
1569 | if (len < 1) | |
1570 | return; | |
1571 | switch (data[0]) { | |
1572 | case P2P_NOA: | |
1573 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1574 | "P2P: Received P2P Action - Notice of Absence"); | |
1575 | /* TODO */ | |
1576 | break; | |
1577 | case P2P_PRESENCE_REQ: | |
1578 | p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq); | |
1579 | break; | |
1580 | case P2P_PRESENCE_RESP: | |
1581 | p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1); | |
1582 | break; | |
1583 | case P2P_GO_DISC_REQ: | |
1584 | p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq); | |
1585 | break; | |
1586 | default: | |
1587 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1588 | "P2P: Received P2P Action - unknown type %u", data[0]); | |
1589 | break; | |
1590 | } | |
1591 | } | |
1592 | ||
1593 | ||
1594 | static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx) | |
1595 | { | |
1596 | struct p2p_data *p2p = eloop_ctx; | |
1597 | if (p2p->go_neg_peer == NULL) | |
1598 | return; | |
1599 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); | |
1600 | p2p->go_neg_peer->status = P2P_SC_SUCCESS; | |
1601 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
1602 | } | |
1603 | ||
1604 | ||
1605 | static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx) | |
1606 | { | |
1607 | struct p2p_data *p2p = eloop_ctx; | |
1608 | if (p2p->invite_peer == NULL) | |
1609 | return; | |
1610 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); | |
1611 | p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr); | |
1612 | } | |
1613 | ||
1614 | ||
1615 | static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr, | |
1616 | const u8 *ie, size_t ie_len) | |
1617 | { | |
1618 | struct p2p_message msg; | |
1619 | struct p2p_device *dev; | |
1620 | ||
1621 | os_memset(&msg, 0, sizeof(msg)); | |
1622 | if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL) | |
1623 | { | |
1624 | p2p_parse_free(&msg); | |
1625 | return; /* not a P2P probe */ | |
1626 | } | |
1627 | ||
1628 | if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN || | |
1629 | os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) | |
1630 | != 0) { | |
1631 | /* The Probe Request is not part of P2P Device Discovery. It is | |
1632 | * not known whether the source address of the frame is the P2P | |
1633 | * Device Address or P2P Interface Address. Do not add a new | |
1634 | * peer entry based on this frames. | |
1635 | */ | |
1636 | p2p_parse_free(&msg); | |
1637 | return; | |
1638 | } | |
1639 | ||
1640 | dev = p2p_get_device(p2p, addr); | |
1641 | if (dev) { | |
1642 | if (dev->country[0] == 0 && msg.listen_channel) | |
1643 | os_memcpy(dev->country, msg.listen_channel, 3); | |
ed908a55 | 1644 | os_get_time(&dev->last_seen); |
b22128ef JM |
1645 | p2p_parse_free(&msg); |
1646 | return; /* already known */ | |
1647 | } | |
1648 | ||
1649 | dev = p2p_create_device(p2p, addr); | |
1650 | if (dev == NULL) { | |
1651 | p2p_parse_free(&msg); | |
1652 | return; | |
1653 | } | |
1654 | ||
1655 | os_get_time(&dev->last_seen); | |
1656 | dev->flags |= P2P_DEV_PROBE_REQ_ONLY; | |
1657 | ||
b22128ef JM |
1658 | if (msg.listen_channel) { |
1659 | os_memcpy(dev->country, msg.listen_channel, 3); | |
1660 | dev->listen_freq = p2p_channel_to_freq(dev->country, | |
1661 | msg.listen_channel[3], | |
1662 | msg.listen_channel[4]); | |
1663 | } | |
1664 | ||
b67d0d9e | 1665 | p2p_copy_wps_info(dev, 1, &msg); |
e57ae6e1 | 1666 | |
b22128ef JM |
1667 | p2p_parse_free(&msg); |
1668 | ||
1669 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1670 | "P2P: Created device entry based on Probe Req: " MACSTR | |
1671 | " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d", | |
c5db8e51 KRK |
1672 | MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab, |
1673 | dev->info.group_capab, dev->info.device_name, | |
1674 | dev->listen_freq); | |
b22128ef JM |
1675 | } |
1676 | ||
1677 | ||
1678 | struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p, | |
1679 | const u8 *addr, | |
1680 | struct p2p_message *msg) | |
1681 | { | |
1682 | struct p2p_device *dev; | |
1683 | ||
1684 | dev = p2p_get_device(p2p, addr); | |
1685 | if (dev) { | |
1686 | os_get_time(&dev->last_seen); | |
1687 | return dev; /* already known */ | |
1688 | } | |
1689 | ||
1690 | dev = p2p_create_device(p2p, addr); | |
1691 | if (dev == NULL) | |
1692 | return NULL; | |
1693 | ||
1694 | p2p_add_dev_info(p2p, addr, dev, msg); | |
1695 | ||
1696 | return dev; | |
1697 | } | |
1698 | ||
1699 | ||
1700 | static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type) | |
1701 | { | |
1702 | if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0) | |
1703 | return 1; | |
1704 | if (os_memcmp(dev_type, req_dev_type, 2) == 0 && | |
1705 | WPA_GET_BE32(&req_dev_type[2]) == 0 && | |
1706 | WPA_GET_BE16(&req_dev_type[6]) == 0) | |
1707 | return 1; /* Category match with wildcard OUI/sub-category */ | |
1708 | return 0; | |
1709 | } | |
1710 | ||
1711 | ||
1712 | int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[], | |
1713 | size_t num_req_dev_type) | |
1714 | { | |
1715 | size_t i; | |
1716 | for (i = 0; i < num_req_dev_type; i++) { | |
1717 | if (dev_type_match(dev_type, req_dev_type[i])) | |
1718 | return 1; | |
1719 | } | |
1720 | return 0; | |
1721 | } | |
1722 | ||
1723 | ||
1724 | /** | |
1725 | * p2p_match_dev_type - Match local device type with requested type | |
1726 | * @p2p: P2P module context from p2p_init() | |
1727 | * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs) | |
1728 | * Returns: 1 on match, 0 on mismatch | |
1729 | * | |
1730 | * This function can be used to match the Requested Device Type attribute in | |
1731 | * WPS IE with the local device types for deciding whether to reply to a Probe | |
1732 | * Request frame. | |
1733 | */ | |
1734 | int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps) | |
1735 | { | |
1736 | struct wps_parse_attr attr; | |
1737 | size_t i; | |
1738 | ||
1739 | if (wps_parse_msg(wps, &attr)) | |
1740 | return 1; /* assume no Requested Device Type attributes */ | |
1741 | ||
1742 | if (attr.num_req_dev_type == 0) | |
1743 | return 1; /* no Requested Device Type attributes -> match */ | |
1744 | ||
1745 | if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type, | |
1746 | attr.num_req_dev_type)) | |
1747 | return 1; /* Own Primary Device Type matches */ | |
1748 | ||
1749 | for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) | |
1750 | if (dev_type_list_match(p2p->cfg->sec_dev_type[i], | |
1751 | attr.req_dev_type, | |
1752 | attr.num_req_dev_type)) | |
1753 | return 1; /* Own Secondary Device Type matches */ | |
1754 | ||
1755 | /* No matching device type found */ | |
1756 | return 0; | |
1757 | } | |
1758 | ||
1759 | ||
1760 | struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p) | |
1761 | { | |
1762 | struct wpabuf *buf; | |
1763 | u8 *len; | |
1764 | ||
1765 | buf = wpabuf_alloc(1000); | |
1766 | if (buf == NULL) | |
1767 | return NULL; | |
1768 | ||
b22128ef JM |
1769 | p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1); |
1770 | ||
1771 | /* P2P IE */ | |
1772 | len = p2p_buf_add_ie_hdr(buf); | |
1773 | p2p_buf_add_capability(buf, p2p->dev_capab, 0); | |
1774 | if (p2p->ext_listen_interval) | |
1775 | p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period, | |
1776 | p2p->ext_listen_interval); | |
1777 | p2p_buf_add_device_info(buf, p2p, NULL); | |
1778 | p2p_buf_update_ie_hdr(buf, len); | |
1779 | ||
1780 | return buf; | |
1781 | } | |
1782 | ||
1783 | ||
e1d52629 JM |
1784 | static int is_11b(u8 rate) |
1785 | { | |
1786 | return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16; | |
1787 | } | |
1788 | ||
1789 | ||
1790 | static int supp_rates_11b_only(struct ieee802_11_elems *elems) | |
1791 | { | |
1792 | int num_11b = 0, num_others = 0; | |
1793 | int i; | |
1794 | ||
1795 | if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL) | |
1796 | return 0; | |
1797 | ||
1798 | for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) { | |
1799 | if (is_11b(elems->supp_rates[i])) | |
1800 | num_11b++; | |
1801 | else | |
1802 | num_others++; | |
1803 | } | |
1804 | ||
70dbe3b6 JM |
1805 | for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len; |
1806 | i++) { | |
1807 | if (is_11b(elems->ext_supp_rates[i])) | |
1808 | num_11b++; | |
1809 | else | |
1810 | num_others++; | |
1811 | } | |
1812 | ||
e1d52629 JM |
1813 | return num_11b > 0 && num_others == 0; |
1814 | } | |
1815 | ||
1816 | ||
04a85e44 JM |
1817 | static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, |
1818 | const u8 *dst, const u8 *bssid, const u8 *ie, | |
b22128ef JM |
1819 | size_t ie_len) |
1820 | { | |
1821 | struct ieee802_11_elems elems; | |
1822 | struct wpabuf *buf; | |
1823 | struct ieee80211_mgmt *resp; | |
97c5b3c4 | 1824 | struct p2p_message msg; |
b22128ef JM |
1825 | struct wpabuf *ies; |
1826 | ||
1827 | if (!p2p->in_listen || !p2p->drv_in_listen) { | |
1828 | /* not in Listen state - ignore Probe Request */ | |
1829 | return; | |
1830 | } | |
1831 | ||
1832 | if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) == | |
1833 | ParseFailed) { | |
1834 | /* Ignore invalid Probe Request frames */ | |
1835 | return; | |
1836 | } | |
1837 | ||
1838 | if (elems.p2p == NULL) { | |
1839 | /* not a P2P probe - ignore it */ | |
1840 | return; | |
1841 | } | |
1842 | ||
04a85e44 JM |
1843 | if (dst && !is_broadcast_ether_addr(dst) && |
1844 | os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) { | |
1845 | /* Not sent to the broadcast address or our P2P Device Address | |
1846 | */ | |
1847 | return; | |
1848 | } | |
1849 | ||
1850 | if (bssid && !is_broadcast_ether_addr(bssid)) { | |
1851 | /* Not sent to the Wildcard BSSID */ | |
1852 | return; | |
1853 | } | |
1854 | ||
b22128ef JM |
1855 | if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN || |
1856 | os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) != | |
1857 | 0) { | |
1858 | /* not using P2P Wildcard SSID - ignore */ | |
1859 | return; | |
1860 | } | |
1861 | ||
e1d52629 JM |
1862 | if (supp_rates_11b_only(&elems)) { |
1863 | /* Indicates support for 11b rates only */ | |
1864 | return; | |
1865 | } | |
1866 | ||
97c5b3c4 JM |
1867 | os_memset(&msg, 0, sizeof(msg)); |
1868 | if (p2p_parse_ies(ie, ie_len, &msg) < 0) { | |
1869 | /* Could not parse P2P attributes */ | |
1870 | return; | |
1871 | } | |
1872 | ||
1873 | if (msg.device_id && | |
1874 | os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN != 0)) { | |
1875 | /* Device ID did not match */ | |
1876 | p2p_parse_free(&msg); | |
1877 | return; | |
1878 | } | |
1879 | ||
b22128ef | 1880 | /* Check Requested Device Type match */ |
97c5b3c4 JM |
1881 | if (msg.wps_attributes && |
1882 | !p2p_match_dev_type(p2p, msg.wps_attributes)) { | |
b22128ef | 1883 | /* No match with Requested Device Type */ |
97c5b3c4 | 1884 | p2p_parse_free(&msg); |
b22128ef JM |
1885 | return; |
1886 | } | |
97c5b3c4 | 1887 | p2p_parse_free(&msg); |
b22128ef JM |
1888 | |
1889 | if (!p2p->cfg->send_probe_resp) | |
1890 | return; /* Response generated elsewhere */ | |
1891 | ||
1892 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1893 | "P2P: Reply to P2P Probe Request in Listen state"); | |
1894 | ||
1895 | /* | |
1896 | * We do not really have a specific BSS that this frame is advertising, | |
1897 | * so build a frame that has some information in valid format. This is | |
1898 | * really only used for discovery purposes, not to learn exact BSS | |
1899 | * parameters. | |
1900 | */ | |
1901 | ies = p2p_build_probe_resp_ies(p2p); | |
1902 | if (ies == NULL) | |
1903 | return; | |
1904 | ||
1905 | buf = wpabuf_alloc(200 + wpabuf_len(ies)); | |
1906 | if (buf == NULL) { | |
1907 | wpabuf_free(ies); | |
1908 | return; | |
1909 | } | |
1910 | ||
1911 | resp = NULL; | |
1912 | resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp); | |
1913 | ||
1914 | resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) | | |
1915 | (WLAN_FC_STYPE_PROBE_RESP << 4)); | |
1916 | os_memcpy(resp->da, addr, ETH_ALEN); | |
1917 | os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN); | |
1918 | os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN); | |
1919 | resp->u.probe_resp.beacon_int = host_to_le16(100); | |
1920 | /* hardware or low-level driver will setup seq_ctrl and timestamp */ | |
1921 | resp->u.probe_resp.capab_info = | |
1922 | host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE | | |
1923 | WLAN_CAPABILITY_PRIVACY | | |
1924 | WLAN_CAPABILITY_SHORT_SLOT_TIME); | |
1925 | ||
1926 | wpabuf_put_u8(buf, WLAN_EID_SSID); | |
1927 | wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN); | |
1928 | wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); | |
1929 | ||
1930 | wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES); | |
1931 | wpabuf_put_u8(buf, 8); | |
1932 | wpabuf_put_u8(buf, (60 / 5) | 0x80); | |
1933 | wpabuf_put_u8(buf, 90 / 5); | |
1934 | wpabuf_put_u8(buf, (120 / 5) | 0x80); | |
1935 | wpabuf_put_u8(buf, 180 / 5); | |
1936 | wpabuf_put_u8(buf, (240 / 5) | 0x80); | |
1937 | wpabuf_put_u8(buf, 360 / 5); | |
1938 | wpabuf_put_u8(buf, 480 / 5); | |
1939 | wpabuf_put_u8(buf, 540 / 5); | |
1940 | ||
1941 | wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS); | |
1942 | wpabuf_put_u8(buf, 1); | |
1943 | wpabuf_put_u8(buf, p2p->cfg->channel); | |
1944 | ||
1945 | wpabuf_put_buf(buf, ies); | |
1946 | wpabuf_free(ies); | |
1947 | ||
1948 | p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf); | |
1949 | ||
1950 | wpabuf_free(buf); | |
1951 | } | |
1952 | ||
1953 | ||
04a85e44 JM |
1954 | int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst, |
1955 | const u8 *bssid, const u8 *ie, size_t ie_len) | |
b22128ef JM |
1956 | { |
1957 | p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len); | |
1958 | ||
04a85e44 | 1959 | p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len); |
b22128ef JM |
1960 | |
1961 | if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) && | |
1962 | p2p->go_neg_peer && | |
c5db8e51 KRK |
1963 | os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN) |
1964 | == 0) { | |
b22128ef JM |
1965 | /* Received a Probe Request from GO Negotiation peer */ |
1966 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1967 | "P2P: Found GO Negotiation peer - try to start GO " | |
1968 | "negotiation from timeout"); | |
1969 | eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL); | |
1970 | return 1; | |
1971 | } | |
1972 | ||
1973 | if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) && | |
1974 | p2p->invite_peer && | |
c5db8e51 KRK |
1975 | os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN) |
1976 | == 0) { | |
b22128ef JM |
1977 | /* Received a Probe Request from Invite peer */ |
1978 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
1979 | "P2P: Found Invite peer - try to start Invite from " | |
1980 | "timeout"); | |
1981 | eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL); | |
1982 | return 1; | |
1983 | } | |
1984 | ||
1985 | return 0; | |
1986 | } | |
1987 | ||
1988 | ||
1989 | static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid, | |
4c08c0bd | 1990 | u8 *buf, size_t len, struct wpabuf *p2p_ie) |
b22128ef JM |
1991 | { |
1992 | struct wpabuf *tmp; | |
1993 | u8 *lpos; | |
1994 | size_t tmplen; | |
1995 | int res; | |
72044390 | 1996 | u8 group_capab; |
b22128ef | 1997 | |
4c08c0bd JM |
1998 | if (p2p_ie == NULL) |
1999 | return 0; /* WLAN AP is not a P2P manager */ | |
b22128ef JM |
2000 | |
2001 | /* | |
2002 | * (Re)Association Request - P2P IE | |
2003 | * P2P Capability attribute (shall be present) | |
4c08c0bd JM |
2004 | * P2P Interface attribute (present if concurrent device and |
2005 | * P2P Management is enabled) | |
b22128ef JM |
2006 | */ |
2007 | tmp = wpabuf_alloc(200); | |
2008 | if (tmp == NULL) | |
2009 | return -1; | |
2010 | ||
2011 | lpos = p2p_buf_add_ie_hdr(tmp); | |
72044390 JM |
2012 | group_capab = 0; |
2013 | if (p2p->num_groups > 0) { | |
2014 | group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER; | |
2015 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && | |
2016 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) && | |
2017 | p2p->cross_connect) | |
2018 | group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; | |
2019 | } | |
2020 | p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab); | |
4c08c0bd JM |
2021 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && |
2022 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED)) | |
b22128ef JM |
2023 | p2p_buf_add_p2p_interface(tmp, p2p); |
2024 | p2p_buf_update_ie_hdr(tmp, lpos); | |
2025 | ||
2026 | tmplen = wpabuf_len(tmp); | |
2027 | if (tmplen > len) | |
2028 | res = -1; | |
2029 | else { | |
2030 | os_memcpy(buf, wpabuf_head(tmp), tmplen); | |
2031 | res = tmplen; | |
2032 | } | |
2033 | wpabuf_free(tmp); | |
2034 | ||
2035 | return res; | |
2036 | } | |
2037 | ||
2038 | ||
2039 | int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf, | |
4c08c0bd | 2040 | size_t len, int p2p_group, struct wpabuf *p2p_ie) |
b22128ef JM |
2041 | { |
2042 | struct wpabuf *tmp; | |
2043 | u8 *lpos; | |
2044 | struct p2p_device *peer; | |
2045 | size_t tmplen; | |
2046 | int res; | |
2047 | ||
2048 | if (!p2p_group) | |
4c08c0bd | 2049 | return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie); |
b22128ef JM |
2050 | |
2051 | /* | |
2052 | * (Re)Association Request - P2P IE | |
2053 | * P2P Capability attribute (shall be present) | |
2054 | * Extended Listen Timing (may be present) | |
2055 | * P2P Device Info attribute (shall be present) | |
2056 | */ | |
2057 | tmp = wpabuf_alloc(200); | |
2058 | if (tmp == NULL) | |
2059 | return -1; | |
2060 | ||
2061 | peer = bssid ? p2p_get_device(p2p, bssid) : NULL; | |
2062 | ||
2063 | lpos = p2p_buf_add_ie_hdr(tmp); | |
2064 | p2p_buf_add_capability(tmp, p2p->dev_capab, 0); | |
5be5305b JM |
2065 | if (p2p->ext_listen_interval) |
2066 | p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period, | |
2067 | p2p->ext_listen_interval); | |
b22128ef JM |
2068 | p2p_buf_add_device_info(tmp, p2p, peer); |
2069 | p2p_buf_update_ie_hdr(tmp, lpos); | |
2070 | ||
2071 | tmplen = wpabuf_len(tmp); | |
2072 | if (tmplen > len) | |
2073 | res = -1; | |
2074 | else { | |
2075 | os_memcpy(buf, wpabuf_head(tmp), tmplen); | |
2076 | res = tmplen; | |
2077 | } | |
2078 | wpabuf_free(tmp); | |
2079 | ||
2080 | return res; | |
2081 | } | |
2082 | ||
2083 | ||
2084 | int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end) | |
2085 | { | |
2086 | struct wpabuf *p2p_ie; | |
2087 | int ret; | |
2088 | ||
2089 | p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE); | |
2090 | if (p2p_ie == NULL) | |
2091 | return 0; | |
2092 | ||
2093 | ret = p2p_attr_text(p2p_ie, buf, end); | |
2094 | wpabuf_free(p2p_ie); | |
2095 | return ret; | |
2096 | } | |
2097 | ||
2098 | ||
2099 | static void p2p_clear_go_neg(struct p2p_data *p2p) | |
2100 | { | |
2101 | p2p->go_neg_peer = NULL; | |
2102 | p2p_clear_timeout(p2p); | |
2103 | p2p_set_state(p2p, P2P_IDLE); | |
2104 | } | |
2105 | ||
2106 | ||
2107 | void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr) | |
2108 | { | |
2109 | if (p2p->go_neg_peer == NULL) { | |
2110 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2111 | "P2P: No pending Group Formation - " | |
2112 | "ignore WPS registration success notification"); | |
2113 | return; /* No pending Group Formation */ | |
2114 | } | |
2115 | ||
2116 | if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) != | |
2117 | 0) { | |
2118 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2119 | "P2P: Ignore WPS registration success notification " | |
2120 | "for " MACSTR " (GO Negotiation peer " MACSTR ")", | |
2121 | MAC2STR(mac_addr), | |
2122 | MAC2STR(p2p->go_neg_peer->intended_addr)); | |
2123 | return; /* Ignore unexpected peer address */ | |
2124 | } | |
2125 | ||
2126 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2127 | "P2P: Group Formation completed successfully with " MACSTR, | |
2128 | MAC2STR(mac_addr)); | |
2129 | ||
2130 | p2p_clear_go_neg(p2p); | |
2131 | } | |
2132 | ||
2133 | ||
2134 | void p2p_group_formation_failed(struct p2p_data *p2p) | |
2135 | { | |
2136 | if (p2p->go_neg_peer == NULL) { | |
2137 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2138 | "P2P: No pending Group Formation - " | |
2139 | "ignore group formation failure notification"); | |
2140 | return; /* No pending Group Formation */ | |
2141 | } | |
2142 | ||
2143 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2144 | "P2P: Group Formation failed with " MACSTR, | |
2145 | MAC2STR(p2p->go_neg_peer->intended_addr)); | |
2146 | ||
2147 | p2p_clear_go_neg(p2p); | |
2148 | } | |
2149 | ||
2150 | ||
2151 | struct p2p_data * p2p_init(const struct p2p_config *cfg) | |
2152 | { | |
2153 | struct p2p_data *p2p; | |
2154 | ||
2155 | if (cfg->max_peers < 1) | |
2156 | return NULL; | |
2157 | ||
2158 | p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg)); | |
2159 | if (p2p == NULL) | |
2160 | return NULL; | |
2161 | p2p->cfg = (struct p2p_config *) (p2p + 1); | |
2162 | os_memcpy(p2p->cfg, cfg, sizeof(*cfg)); | |
2163 | if (cfg->dev_name) | |
2164 | p2p->cfg->dev_name = os_strdup(cfg->dev_name); | |
b6e01800 JM |
2165 | if (cfg->manufacturer) |
2166 | p2p->cfg->manufacturer = os_strdup(cfg->manufacturer); | |
2167 | if (cfg->model_name) | |
2168 | p2p->cfg->model_name = os_strdup(cfg->model_name); | |
2169 | if (cfg->model_number) | |
2170 | p2p->cfg->model_number = os_strdup(cfg->model_number); | |
2171 | if (cfg->serial_number) | |
2172 | p2p->cfg->serial_number = os_strdup(cfg->serial_number); | |
b22128ef JM |
2173 | |
2174 | p2p->min_disc_int = 1; | |
2175 | p2p->max_disc_int = 3; | |
2176 | ||
2177 | os_get_random(&p2p->next_tie_breaker, 1); | |
2178 | p2p->next_tie_breaker &= 0x01; | |
2179 | if (cfg->sd_request) | |
2180 | p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY; | |
2181 | p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE; | |
2182 | if (cfg->concurrent_operations) | |
2183 | p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER; | |
2184 | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
2185 | ||
2186 | dl_list_init(&p2p->devices); | |
2187 | ||
2188 | eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0, | |
2189 | p2p_expiration_timeout, p2p, NULL); | |
2190 | ||
2191 | return p2p; | |
2192 | } | |
2193 | ||
2194 | ||
2195 | void p2p_deinit(struct p2p_data *p2p) | |
2196 | { | |
2197 | eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL); | |
2198 | eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); | |
2199 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
2200 | p2p_flush(p2p); | |
046ef4aa | 2201 | p2p_free_req_dev_types(p2p); |
b22128ef | 2202 | os_free(p2p->cfg->dev_name); |
b6e01800 JM |
2203 | os_free(p2p->cfg->manufacturer); |
2204 | os_free(p2p->cfg->model_name); | |
2205 | os_free(p2p->cfg->model_number); | |
2206 | os_free(p2p->cfg->serial_number); | |
b22128ef | 2207 | os_free(p2p->groups); |
18708aad | 2208 | wpabuf_free(p2p->sd_resp); |
3f9285ff | 2209 | os_free(p2p->after_scan_tx); |
f95cac27 | 2210 | p2p_remove_wps_vendor_extensions(p2p); |
b22128ef JM |
2211 | os_free(p2p); |
2212 | } | |
2213 | ||
2214 | ||
2215 | void p2p_flush(struct p2p_data *p2p) | |
2216 | { | |
2217 | struct p2p_device *dev, *prev; | |
2218 | p2p_clear_timeout(p2p); | |
2219 | p2p_set_state(p2p, P2P_IDLE); | |
2220 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; | |
2221 | p2p->go_neg_peer = NULL; | |
2222 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); | |
2223 | dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device, | |
2224 | list) { | |
2225 | dl_list_del(&dev->list); | |
2226 | p2p_device_free(p2p, dev); | |
2227 | } | |
2228 | p2p_free_sd_queries(p2p); | |
f44ae207 JM |
2229 | os_free(p2p->after_scan_tx); |
2230 | p2p->after_scan_tx = NULL; | |
b22128ef JM |
2231 | } |
2232 | ||
2233 | ||
9d562b79 SS |
2234 | int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr) |
2235 | { | |
2236 | struct p2p_device *dev; | |
2237 | ||
2238 | dev = p2p_get_device(p2p, addr); | |
2239 | if (dev == NULL) | |
2240 | return -1; | |
2241 | ||
2242 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR, | |
2243 | MAC2STR(addr)); | |
2244 | ||
2245 | if (p2p->go_neg_peer == dev) | |
2246 | p2p->go_neg_peer = NULL; | |
2247 | ||
2248 | dev->wps_method = WPS_NOT_READY; | |
2249 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; | |
2250 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; | |
2251 | ||
2252 | /* Check if after_scan_tx is for this peer. If so free it */ | |
2253 | if (p2p->after_scan_tx && | |
2254 | os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) { | |
2255 | os_free(p2p->after_scan_tx); | |
2256 | p2p->after_scan_tx = NULL; | |
2257 | } | |
2258 | ||
2259 | return 0; | |
2260 | } | |
2261 | ||
2262 | ||
b22128ef JM |
2263 | int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name) |
2264 | { | |
2265 | os_free(p2p->cfg->dev_name); | |
2266 | if (dev_name) { | |
2267 | p2p->cfg->dev_name = os_strdup(dev_name); | |
2268 | if (p2p->cfg->dev_name == NULL) | |
2269 | return -1; | |
2270 | } else | |
2271 | p2p->cfg->dev_name = NULL; | |
2272 | return 0; | |
2273 | } | |
2274 | ||
2275 | ||
b6e01800 JM |
2276 | int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer) |
2277 | { | |
2278 | os_free(p2p->cfg->manufacturer); | |
2279 | p2p->cfg->manufacturer = NULL; | |
2280 | if (manufacturer) { | |
2281 | p2p->cfg->manufacturer = os_strdup(manufacturer); | |
2282 | if (p2p->cfg->manufacturer == NULL) | |
2283 | return -1; | |
2284 | } | |
2285 | ||
2286 | return 0; | |
2287 | } | |
2288 | ||
2289 | ||
2290 | int p2p_set_model_name(struct p2p_data *p2p, const char *model_name) | |
2291 | { | |
2292 | os_free(p2p->cfg->model_name); | |
2293 | p2p->cfg->model_name = NULL; | |
2294 | if (model_name) { | |
2295 | p2p->cfg->model_name = os_strdup(model_name); | |
2296 | if (p2p->cfg->model_name == NULL) | |
2297 | return -1; | |
2298 | } | |
2299 | ||
2300 | return 0; | |
2301 | } | |
2302 | ||
2303 | ||
2304 | int p2p_set_model_number(struct p2p_data *p2p, const char *model_number) | |
2305 | { | |
2306 | os_free(p2p->cfg->model_number); | |
2307 | p2p->cfg->model_number = NULL; | |
2308 | if (model_number) { | |
2309 | p2p->cfg->model_number = os_strdup(model_number); | |
2310 | if (p2p->cfg->model_number == NULL) | |
2311 | return -1; | |
2312 | } | |
2313 | ||
2314 | return 0; | |
2315 | } | |
2316 | ||
2317 | ||
2318 | int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number) | |
2319 | { | |
2320 | os_free(p2p->cfg->serial_number); | |
2321 | p2p->cfg->serial_number = NULL; | |
2322 | if (serial_number) { | |
2323 | p2p->cfg->serial_number = os_strdup(serial_number); | |
2324 | if (p2p->cfg->serial_number == NULL) | |
2325 | return -1; | |
2326 | } | |
2327 | ||
2328 | return 0; | |
2329 | } | |
2330 | ||
2331 | ||
2332 | void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods) | |
2333 | { | |
2334 | p2p->cfg->config_methods = config_methods; | |
2335 | } | |
2336 | ||
2337 | ||
2338 | void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid) | |
2339 | { | |
2340 | os_memcpy(p2p->cfg->uuid, uuid, 16); | |
2341 | } | |
2342 | ||
2343 | ||
b22128ef JM |
2344 | int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type) |
2345 | { | |
2346 | os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8); | |
2347 | return 0; | |
2348 | } | |
2349 | ||
2350 | ||
2351 | int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8], | |
2352 | size_t num_dev_types) | |
2353 | { | |
2354 | if (num_dev_types > P2P_SEC_DEVICE_TYPES) | |
2355 | num_dev_types = P2P_SEC_DEVICE_TYPES; | |
2356 | p2p->cfg->num_sec_dev_types = num_dev_types; | |
2357 | os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8); | |
2358 | return 0; | |
2359 | } | |
2360 | ||
2361 | ||
f95cac27 JMB |
2362 | void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p) |
2363 | { | |
2364 | int i; | |
2365 | ||
10c5d2a5 | 2366 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
f95cac27 JMB |
2367 | wpabuf_free(p2p->wps_vendor_ext[i]); |
2368 | p2p->wps_vendor_ext[i] = NULL; | |
2369 | } | |
2370 | } | |
2371 | ||
2372 | ||
2373 | int p2p_add_wps_vendor_extension(struct p2p_data *p2p, | |
2374 | const struct wpabuf *vendor_ext) | |
2375 | { | |
2376 | int i; | |
2377 | ||
2378 | if (vendor_ext == NULL) | |
2379 | return -1; | |
2380 | ||
10c5d2a5 | 2381 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
f95cac27 JMB |
2382 | if (p2p->wps_vendor_ext[i] == NULL) |
2383 | break; | |
2384 | } | |
10c5d2a5 | 2385 | if (i >= P2P_MAX_WPS_VENDOR_EXT) |
f95cac27 JMB |
2386 | return -1; |
2387 | ||
2388 | p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext); | |
2389 | if (p2p->wps_vendor_ext[i] == NULL) | |
2390 | return -1; | |
2391 | ||
2392 | return 0; | |
2393 | } | |
2394 | ||
2395 | ||
b22128ef JM |
2396 | int p2p_set_country(struct p2p_data *p2p, const char *country) |
2397 | { | |
2398 | os_memcpy(p2p->cfg->country, country, 3); | |
2399 | return 0; | |
2400 | } | |
2401 | ||
2402 | ||
2403 | void p2p_continue_find(struct p2p_data *p2p) | |
2404 | { | |
2405 | struct p2p_device *dev; | |
2406 | p2p_set_state(p2p, P2P_SEARCH); | |
2407 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
2408 | if (dev->flags & P2P_DEV_SD_SCHEDULE) { | |
2409 | if (p2p_start_sd(p2p, dev) == 0) | |
2410 | return; | |
2411 | else | |
2412 | break; | |
10c4edde JM |
2413 | } else if (dev->req_config_methods && |
2414 | !(dev->flags & P2P_DEV_PD_FOR_JOIN)) { | |
2415 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send " | |
e3a0706b | 2416 | "pending Provision Discovery Request to " |
10c4edde | 2417 | MACSTR " (config methods 0x%x)", |
c5db8e51 | 2418 | MAC2STR(dev->info.p2p_device_addr), |
10c4edde | 2419 | dev->req_config_methods); |
1ef2f7ff | 2420 | if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0) |
b22128ef JM |
2421 | return; |
2422 | } | |
2423 | } | |
2424 | ||
2425 | p2p_listen_in_find(p2p); | |
2426 | } | |
2427 | ||
2428 | ||
2429 | static void p2p_sd_cb(struct p2p_data *p2p, int success) | |
2430 | { | |
2431 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2432 | "P2P: Service Discovery Query TX callback: success=%d", | |
2433 | success); | |
2434 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
2435 | ||
2436 | if (!success) { | |
2437 | if (p2p->sd_peer) { | |
2438 | p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE; | |
2439 | p2p->sd_peer = NULL; | |
2440 | } | |
2441 | p2p_continue_find(p2p); | |
2442 | return; | |
2443 | } | |
2444 | ||
2445 | if (p2p->sd_peer == NULL) { | |
2446 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2447 | "P2P: No SD peer entry known"); | |
2448 | p2p_continue_find(p2p); | |
2449 | return; | |
2450 | } | |
2451 | ||
2452 | /* Wait for response from the peer */ | |
2453 | p2p_set_state(p2p, P2P_SD_DURING_FIND); | |
2454 | p2p_set_timeout(p2p, 0, 200000); | |
2455 | } | |
2456 | ||
6b56cc2d JS |
2457 | |
2458 | /** | |
2459 | * p2p_retry_pd - Retry any pending provision disc requests in IDLE state | |
2460 | * @p2p: P2P module context from p2p_init() | |
2461 | */ | |
19df9b07 | 2462 | static void p2p_retry_pd(struct p2p_data *p2p) |
6b56cc2d JS |
2463 | { |
2464 | struct p2p_device *dev; | |
2465 | ||
2466 | if (p2p->state != P2P_IDLE) | |
2467 | return; | |
2468 | ||
2469 | /* | |
2470 | * Retry the prov disc req attempt only for the peer that the user had | |
2471 | * requested for and provided a join has not been initiated on it | |
2472 | * in the meantime. | |
2473 | */ | |
2474 | ||
2475 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
2476 | if (os_memcmp(p2p->pending_pd_devaddr, | |
2477 | dev->info.p2p_device_addr, ETH_ALEN) != 0) | |
2478 | continue; | |
2479 | if (!dev->req_config_methods) | |
2480 | continue; | |
2481 | if (dev->flags & P2P_DEV_PD_FOR_JOIN) | |
2482 | continue; | |
2483 | ||
2484 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send " | |
e3a0706b | 2485 | "pending Provision Discovery Request to " |
6b56cc2d JS |
2486 | MACSTR " (config methods 0x%x)", |
2487 | MAC2STR(dev->info.p2p_device_addr), | |
2488 | dev->req_config_methods); | |
1ef2f7ff | 2489 | p2p_send_prov_disc_req(p2p, dev, 0, 0); |
6b56cc2d JS |
2490 | return; |
2491 | } | |
2492 | } | |
2493 | ||
2494 | ||
b22128ef JM |
2495 | static void p2p_prov_disc_cb(struct p2p_data *p2p, int success) |
2496 | { | |
2497 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2498 | "P2P: Provision Discovery Request TX callback: success=%d", | |
2499 | success); | |
6b56cc2d JS |
2500 | |
2501 | /* | |
2502 | * Postpone resetting the pending action state till after we actually | |
2503 | * time out. This allows us to take some action like notifying any | |
2504 | * interested parties about no response to the request. | |
2505 | * | |
2506 | * When the timer (below) goes off we check in IDLE, SEARCH, or | |
2507 | * LISTEN_ONLY state, which are the only allowed states to issue a PD | |
2508 | * requests in, if this was still pending and then raise notification. | |
2509 | */ | |
b22128ef JM |
2510 | |
2511 | if (!success) { | |
6b56cc2d JS |
2512 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
2513 | ||
b22128ef JM |
2514 | if (p2p->state != P2P_IDLE) |
2515 | p2p_continue_find(p2p); | |
6b56cc2d JS |
2516 | else if (p2p->user_initiated_pd) { |
2517 | p2p->pending_action_state = P2P_PENDING_PD; | |
2518 | p2p_set_timeout(p2p, 0, 300000); | |
2519 | } | |
b22128ef JM |
2520 | return; |
2521 | } | |
2522 | ||
6b56cc2d JS |
2523 | /* |
2524 | * This postponing, of resetting pending_action_state, needs to be | |
2525 | * done only for user initiated PD requests and not internal ones. | |
2526 | */ | |
2527 | if (p2p->user_initiated_pd) | |
2528 | p2p->pending_action_state = P2P_PENDING_PD; | |
2529 | else | |
2530 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
2531 | ||
b22128ef JM |
2532 | /* Wait for response from the peer */ |
2533 | if (p2p->state == P2P_SEARCH) | |
2534 | p2p_set_state(p2p, P2P_PD_DURING_FIND); | |
2535 | p2p_set_timeout(p2p, 0, 200000); | |
2536 | } | |
2537 | ||
2538 | ||
2539 | int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq, | |
2540 | int level, const u8 *ies, size_t ies_len) | |
2541 | { | |
2542 | p2p_add_device(p2p, bssid, freq, level, ies, ies_len); | |
2543 | ||
2544 | if (p2p->go_neg_peer && p2p->state == P2P_SEARCH && | |
c5db8e51 KRK |
2545 | os_memcmp(p2p->go_neg_peer->info.p2p_device_addr, bssid, ETH_ALEN) |
2546 | == 0) { | |
b22128ef JM |
2547 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
2548 | "P2P: Found GO Negotiation peer - try to start GO " | |
2549 | "negotiation"); | |
2550 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
2551 | return 1; | |
2552 | } | |
2553 | ||
2554 | return 0; | |
2555 | } | |
2556 | ||
2557 | ||
2558 | void p2p_scan_res_handled(struct p2p_data *p2p) | |
2559 | { | |
2560 | if (!p2p->p2p_scan_running) { | |
2561 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not " | |
2562 | "running, but scan results received"); | |
2563 | } | |
2564 | p2p->p2p_scan_running = 0; | |
2565 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
2566 | ||
2567 | if (p2p_run_after_scan(p2p)) | |
2568 | return; | |
2569 | if (p2p->state == P2P_SEARCH) | |
2570 | p2p_continue_find(p2p); | |
2571 | } | |
2572 | ||
2573 | ||
6d92fa6e | 2574 | void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id) |
b22128ef JM |
2575 | { |
2576 | u8 *len = p2p_buf_add_ie_hdr(ies); | |
2577 | p2p_buf_add_capability(ies, p2p->dev_capab, 0); | |
6d92fa6e JM |
2578 | if (dev_id) |
2579 | p2p_buf_add_device_id(ies, dev_id); | |
b22128ef JM |
2580 | if (p2p->cfg->reg_class && p2p->cfg->channel) |
2581 | p2p_buf_add_listen_channel(ies, p2p->cfg->country, | |
2582 | p2p->cfg->reg_class, | |
2583 | p2p->cfg->channel); | |
2584 | if (p2p->ext_listen_interval) | |
2585 | p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period, | |
2586 | p2p->ext_listen_interval); | |
2587 | /* TODO: p2p_buf_add_operating_channel() if GO */ | |
2588 | p2p_buf_update_ie_hdr(ies, len); | |
2589 | } | |
2590 | ||
2591 | ||
206e1f42 JM |
2592 | size_t p2p_scan_ie_buf_len(struct p2p_data *p2p) |
2593 | { | |
2594 | return 100; | |
2595 | } | |
2596 | ||
2597 | ||
b22128ef JM |
2598 | int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end) |
2599 | { | |
2600 | return p2p_attr_text(p2p_ie, buf, end); | |
2601 | } | |
2602 | ||
2603 | ||
2604 | static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success) | |
2605 | { | |
2606 | struct p2p_device *dev = p2p->go_neg_peer; | |
2607 | ||
2608 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2609 | "P2P: GO Negotiation Request TX callback: success=%d", | |
2610 | success); | |
2611 | ||
2612 | if (dev == NULL) { | |
2613 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2614 | "P2P: No pending GO Negotiation"); | |
2615 | return; | |
2616 | } | |
2617 | ||
2618 | if (success) { | |
2619 | dev->go_neg_req_sent++; | |
2620 | if (dev->flags & P2P_DEV_USER_REJECTED) { | |
2621 | p2p_set_state(p2p, P2P_IDLE); | |
2622 | return; | |
2623 | } | |
2624 | } | |
2625 | ||
2626 | if (!success && | |
c5db8e51 | 2627 | (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) && |
b22128ef JM |
2628 | !is_zero_ether_addr(dev->member_in_go_dev)) { |
2629 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2630 | "P2P: Peer " MACSTR " did not acknowledge request - " | |
2631 | "try to use device discoverability through its GO", | |
c5db8e51 | 2632 | MAC2STR(dev->info.p2p_device_addr)); |
b22128ef JM |
2633 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
2634 | p2p_send_dev_disc_req(p2p, dev); | |
2635 | return; | |
2636 | } | |
2637 | ||
2638 | /* | |
2639 | * Use P2P find, if needed, to find the other device from its listen | |
2640 | * channel. | |
2641 | */ | |
2642 | p2p_set_state(p2p, P2P_CONNECT); | |
2643 | p2p_set_timeout(p2p, 0, 100000); | |
2644 | } | |
2645 | ||
2646 | ||
2647 | static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success) | |
2648 | { | |
2649 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2650 | "P2P: GO Negotiation Response TX callback: success=%d", | |
2651 | success); | |
2652 | if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) { | |
2653 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2654 | "P2P: Ignore TX callback event - GO Negotiation is " | |
2655 | "not running anymore"); | |
2656 | return; | |
2657 | } | |
2658 | p2p_set_state(p2p, P2P_CONNECT); | |
2659 | p2p_set_timeout(p2p, 0, 100000); | |
2660 | } | |
2661 | ||
2662 | ||
2663 | static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success) | |
2664 | { | |
2665 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2666 | "P2P: GO Negotiation Response (failure) TX callback: " | |
2667 | "success=%d", success); | |
fbe70272 JM |
2668 | if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) { |
2669 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, | |
2670 | p2p->go_neg_peer->status); | |
2671 | } | |
b22128ef JM |
2672 | } |
2673 | ||
2674 | ||
93b7ddd0 JM |
2675 | static void p2p_go_neg_conf_cb(struct p2p_data *p2p, |
2676 | enum p2p_send_action_result result) | |
b22128ef JM |
2677 | { |
2678 | struct p2p_device *dev; | |
2679 | ||
2680 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
93b7ddd0 JM |
2681 | "P2P: GO Negotiation Confirm TX callback: result=%d", |
2682 | result); | |
b22128ef | 2683 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
93b7ddd0 JM |
2684 | if (result == P2P_SEND_ACTION_FAILED) { |
2685 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); | |
2686 | return; | |
2687 | } | |
2688 | if (result == P2P_SEND_ACTION_NO_ACK) { | |
b22128ef JM |
2689 | /* |
2690 | * It looks like the TX status for GO Negotiation Confirm is | |
2691 | * often showing failure even when the peer has actually | |
2692 | * received the frame. Since the peer may change channels | |
2693 | * immediately after having received the frame, we may not see | |
2694 | * an Ack for retries, so just dropping a single frame may | |
2695 | * trigger this. To allow the group formation to succeed if the | |
2696 | * peer did indeed receive the frame, continue regardless of | |
2697 | * the TX status. | |
2698 | */ | |
2699 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2700 | "P2P: Assume GO Negotiation Confirm TX was actually " | |
2701 | "received by the peer even though Ack was not " | |
2702 | "reported"); | |
2703 | } | |
2704 | ||
2705 | dev = p2p->go_neg_peer; | |
2706 | if (dev == NULL) | |
2707 | return; | |
2708 | ||
2709 | p2p_go_complete(p2p, dev); | |
2710 | } | |
2711 | ||
2712 | ||
2713 | void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst, | |
93b7ddd0 JM |
2714 | const u8 *src, const u8 *bssid, |
2715 | enum p2p_send_action_result result) | |
b22128ef JM |
2716 | { |
2717 | enum p2p_pending_action_state state; | |
93b7ddd0 | 2718 | int success; |
b22128ef JM |
2719 | |
2720 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2721 | "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR | |
93b7ddd0 | 2722 | " src=" MACSTR " bssid=" MACSTR " result=%d", |
b22128ef | 2723 | p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src), |
93b7ddd0 JM |
2724 | MAC2STR(bssid), result); |
2725 | success = result == P2P_SEND_ACTION_SUCCESS; | |
b22128ef JM |
2726 | state = p2p->pending_action_state; |
2727 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
2728 | switch (state) { | |
2729 | case P2P_NO_PENDING_ACTION: | |
2730 | break; | |
2731 | case P2P_PENDING_GO_NEG_REQUEST: | |
2732 | p2p_go_neg_req_cb(p2p, success); | |
2733 | break; | |
2734 | case P2P_PENDING_GO_NEG_RESPONSE: | |
2735 | p2p_go_neg_resp_cb(p2p, success); | |
2736 | break; | |
2737 | case P2P_PENDING_GO_NEG_RESPONSE_FAILURE: | |
2738 | p2p_go_neg_resp_failure_cb(p2p, success); | |
2739 | break; | |
2740 | case P2P_PENDING_GO_NEG_CONFIRM: | |
93b7ddd0 | 2741 | p2p_go_neg_conf_cb(p2p, result); |
b22128ef JM |
2742 | break; |
2743 | case P2P_PENDING_SD: | |
2744 | p2p_sd_cb(p2p, success); | |
2745 | break; | |
2746 | case P2P_PENDING_PD: | |
2747 | p2p_prov_disc_cb(p2p, success); | |
2748 | break; | |
2749 | case P2P_PENDING_INVITATION_REQUEST: | |
2750 | p2p_invitation_req_cb(p2p, success); | |
2751 | break; | |
2752 | case P2P_PENDING_INVITATION_RESPONSE: | |
2753 | p2p_invitation_resp_cb(p2p, success); | |
2754 | break; | |
2755 | case P2P_PENDING_DEV_DISC_REQUEST: | |
2756 | p2p_dev_disc_req_cb(p2p, success); | |
2757 | break; | |
2758 | case P2P_PENDING_DEV_DISC_RESPONSE: | |
2759 | p2p_dev_disc_resp_cb(p2p, success); | |
2760 | break; | |
2761 | case P2P_PENDING_GO_DISC_REQ: | |
2762 | p2p_go_disc_req_cb(p2p, success); | |
2763 | break; | |
2764 | } | |
2765 | } | |
2766 | ||
2767 | ||
2768 | void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq, | |
2769 | unsigned int duration) | |
2770 | { | |
2771 | if (freq == p2p->pending_client_disc_freq) { | |
2772 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2773 | "P2P: Client discoverability remain-awake completed"); | |
2774 | p2p->pending_client_disc_freq = 0; | |
2775 | return; | |
2776 | } | |
2777 | ||
2778 | if (freq != p2p->pending_listen_freq) { | |
2779 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2780 | "P2P: Unexpected listen callback for freq=%u " | |
2781 | "duration=%u (pending_listen_freq=%u)", | |
2782 | freq, duration, p2p->pending_listen_freq); | |
2783 | return; | |
2784 | } | |
2785 | ||
2786 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2787 | "P2P: Starting Listen timeout(%u,%u) on freq=%u based on " | |
2788 | "callback", | |
2789 | p2p->pending_listen_sec, p2p->pending_listen_usec, | |
2790 | p2p->pending_listen_freq); | |
2791 | p2p->in_listen = 1; | |
0b8889d8 | 2792 | p2p->drv_in_listen = freq; |
b22128ef JM |
2793 | if (p2p->pending_listen_sec || p2p->pending_listen_usec) { |
2794 | /* | |
2795 | * Add 20 msec extra wait to avoid race condition with driver | |
2796 | * remain-on-channel end event, i.e., give driver more time to | |
2797 | * complete the operation before our timeout expires. | |
2798 | */ | |
2799 | p2p_set_timeout(p2p, p2p->pending_listen_sec, | |
2800 | p2p->pending_listen_usec + 20000); | |
2801 | } | |
2802 | ||
2803 | p2p->pending_listen_freq = 0; | |
2804 | } | |
2805 | ||
2806 | ||
2807 | int p2p_listen_end(struct p2p_data *p2p, unsigned int freq) | |
2808 | { | |
2809 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen " | |
2810 | "state (freq=%u)", freq); | |
2811 | p2p->drv_in_listen = 0; | |
2812 | if (p2p->in_listen) | |
2813 | return 0; /* Internal timeout will trigger the next step */ | |
2814 | ||
2815 | if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) { | |
e24cf97c JM |
2816 | if (p2p->go_neg_peer->connect_reqs >= 120) { |
2817 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2818 | "P2P: Timeout on sending GO Negotiation " | |
2819 | "Request without getting response"); | |
2820 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); | |
2821 | return 0; | |
2822 | } | |
2823 | ||
b22128ef JM |
2824 | p2p_set_state(p2p, P2P_CONNECT); |
2825 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
2826 | return 1; | |
2827 | } else if (p2p->state == P2P_SEARCH) { | |
2828 | p2p_search(p2p); | |
2829 | return 1; | |
2830 | } | |
2831 | ||
2832 | return 0; | |
2833 | } | |
2834 | ||
2835 | ||
2836 | static void p2p_timeout_connect(struct p2p_data *p2p) | |
2837 | { | |
2838 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
2839 | p2p_set_state(p2p, P2P_CONNECT_LISTEN); | |
2840 | p2p_listen_in_find(p2p); | |
2841 | } | |
2842 | ||
2843 | ||
2844 | static void p2p_timeout_connect_listen(struct p2p_data *p2p) | |
2845 | { | |
2846 | if (p2p->go_neg_peer) { | |
2847 | if (p2p->drv_in_listen) { | |
2848 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is " | |
2849 | "still in Listen state; wait for it to " | |
2850 | "complete"); | |
2851 | return; | |
2852 | } | |
9dac8c3e FM |
2853 | |
2854 | if (p2p->go_neg_peer->connect_reqs >= 120) { | |
2855 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2856 | "P2P: Timeout on sending GO Negotiation " | |
2857 | "Request without getting response"); | |
2858 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); | |
2859 | return; | |
2860 | } | |
2861 | ||
b22128ef JM |
2862 | p2p_set_state(p2p, P2P_CONNECT); |
2863 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
2864 | } else | |
2865 | p2p_set_state(p2p, P2P_IDLE); | |
2866 | } | |
2867 | ||
2868 | ||
2869 | static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p) | |
2870 | { | |
2871 | /* | |
2872 | * TODO: could remain constantly in Listen state for some time if there | |
2873 | * are no other concurrent uses for the radio. For now, go to listen | |
2874 | * state once per second to give other uses a chance to use the radio. | |
2875 | */ | |
2876 | p2p_set_state(p2p, P2P_WAIT_PEER_IDLE); | |
d58ed4e3 | 2877 | p2p_set_timeout(p2p, 0, 500000); |
b22128ef JM |
2878 | } |
2879 | ||
2880 | ||
2881 | static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p) | |
2882 | { | |
2883 | struct p2p_device *dev = p2p->go_neg_peer; | |
2884 | ||
2885 | if (dev == NULL) { | |
2886 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2887 | "P2P: Unknown GO Neg peer - stop GO Neg wait"); | |
2888 | return; | |
2889 | } | |
2890 | ||
2891 | dev->wait_count++; | |
2892 | if (dev->wait_count >= 120) { | |
2893 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2894 | "P2P: Timeout on waiting peer to become ready for GO " | |
2895 | "Negotiation"); | |
2896 | p2p_go_neg_failed(p2p, dev, -1); | |
2897 | return; | |
2898 | } | |
2899 | ||
2900 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2901 | "P2P: Go to Listen state while waiting for the peer to become " | |
2902 | "ready for GO Negotiation"); | |
2903 | p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT); | |
2904 | p2p_listen_in_find(p2p); | |
2905 | } | |
2906 | ||
2907 | ||
2908 | static void p2p_timeout_sd_during_find(struct p2p_data *p2p) | |
2909 | { | |
2910 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2911 | "P2P: Service Discovery Query timeout"); | |
2912 | if (p2p->sd_peer) { | |
2913 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
2914 | p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE; | |
2915 | p2p->sd_peer = NULL; | |
2916 | } | |
2917 | p2p_continue_find(p2p); | |
2918 | } | |
2919 | ||
2920 | ||
2921 | static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p) | |
2922 | { | |
2923 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2924 | "P2P: Provision Discovery Request timeout"); | |
2925 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
2926 | p2p_continue_find(p2p); | |
2927 | } | |
2928 | ||
2929 | ||
6b56cc2d JS |
2930 | static void p2p_timeout_prov_disc_req(struct p2p_data *p2p) |
2931 | { | |
2932 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
2933 | ||
2934 | /* | |
2935 | * For user initiated PD requests that we have not gotten any responses | |
2936 | * for while in IDLE state, we retry them a couple of times before | |
2937 | * giving up. | |
2938 | */ | |
2939 | if (!p2p->user_initiated_pd) | |
2940 | return; | |
2941 | ||
2942 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2943 | "P2P: User initiated Provision Discovery Request timeout"); | |
2944 | ||
2945 | if (p2p->pd_retries) { | |
2946 | p2p->pd_retries--; | |
2947 | p2p_retry_pd(p2p); | |
2948 | } else { | |
349b213c JS |
2949 | if (p2p->cfg->prov_disc_fail) |
2950 | p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, | |
2951 | p2p->pending_pd_devaddr, | |
2952 | P2P_PROV_DISC_TIMEOUT); | |
6b56cc2d JS |
2953 | p2p_reset_pending_pd(p2p); |
2954 | } | |
2955 | } | |
2956 | ||
2957 | ||
b22128ef JM |
2958 | static void p2p_timeout_invite(struct p2p_data *p2p) |
2959 | { | |
2960 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
2961 | p2p_set_state(p2p, P2P_INVITE_LISTEN); | |
04d8dad5 JM |
2962 | if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) { |
2963 | /* | |
2964 | * Better remain on operating channel instead of listen channel | |
2965 | * when running a group. | |
2966 | */ | |
2967 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in " | |
2968 | "active GO role - wait on operating channel"); | |
2969 | p2p_set_timeout(p2p, 0, 100000); | |
2970 | return; | |
2971 | } | |
b22128ef JM |
2972 | p2p_listen_in_find(p2p); |
2973 | } | |
2974 | ||
2975 | ||
2976 | static void p2p_timeout_invite_listen(struct p2p_data *p2p) | |
2977 | { | |
2978 | if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) { | |
2979 | p2p_set_state(p2p, P2P_INVITE); | |
2980 | p2p_invite_send(p2p, p2p->invite_peer, | |
2981 | p2p->invite_go_dev_addr); | |
2982 | } else { | |
2983 | if (p2p->invite_peer) { | |
2984 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
2985 | "P2P: Invitation Request retry limit reached"); | |
2986 | if (p2p->cfg->invitation_result) | |
2987 | p2p->cfg->invitation_result( | |
2988 | p2p->cfg->cb_ctx, -1, NULL); | |
2989 | } | |
2990 | p2p_set_state(p2p, P2P_IDLE); | |
2991 | } | |
2992 | } | |
2993 | ||
2994 | ||
2995 | static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx) | |
2996 | { | |
2997 | struct p2p_data *p2p = eloop_ctx; | |
2998 | ||
2999 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)", | |
3000 | p2p_state_txt(p2p->state)); | |
3001 | ||
3002 | p2p->in_listen = 0; | |
3003 | ||
3004 | switch (p2p->state) { | |
3005 | case P2P_IDLE: | |
6b56cc2d JS |
3006 | /* Check if we timed out waiting for PD req */ |
3007 | if (p2p->pending_action_state == P2P_PENDING_PD) | |
3008 | p2p_timeout_prov_disc_req(p2p); | |
b22128ef JM |
3009 | break; |
3010 | case P2P_SEARCH: | |
6b56cc2d JS |
3011 | /* Check if we timed out waiting for PD req */ |
3012 | if (p2p->pending_action_state == P2P_PENDING_PD) | |
3013 | p2p_timeout_prov_disc_req(p2p); | |
b22128ef JM |
3014 | p2p_search(p2p); |
3015 | break; | |
3016 | case P2P_CONNECT: | |
3017 | p2p_timeout_connect(p2p); | |
3018 | break; | |
3019 | case P2P_CONNECT_LISTEN: | |
3020 | p2p_timeout_connect_listen(p2p); | |
3021 | break; | |
3022 | case P2P_GO_NEG: | |
3023 | break; | |
3024 | case P2P_LISTEN_ONLY: | |
6b56cc2d JS |
3025 | /* Check if we timed out waiting for PD req */ |
3026 | if (p2p->pending_action_state == P2P_PENDING_PD) | |
3027 | p2p_timeout_prov_disc_req(p2p); | |
3028 | ||
b22128ef JM |
3029 | if (p2p->ext_listen_only) { |
3030 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3031 | "P2P: Extended Listen Timing - Listen State " | |
3032 | "completed"); | |
3033 | p2p->ext_listen_only = 0; | |
3034 | p2p_set_state(p2p, P2P_IDLE); | |
3035 | } | |
3036 | break; | |
3037 | case P2P_WAIT_PEER_CONNECT: | |
3038 | p2p_timeout_wait_peer_connect(p2p); | |
3039 | break; | |
3040 | case P2P_WAIT_PEER_IDLE: | |
3041 | p2p_timeout_wait_peer_idle(p2p); | |
3042 | break; | |
3043 | case P2P_SD_DURING_FIND: | |
3044 | p2p_timeout_sd_during_find(p2p); | |
3045 | break; | |
3046 | case P2P_PROVISIONING: | |
3047 | break; | |
3048 | case P2P_PD_DURING_FIND: | |
3049 | p2p_timeout_prov_disc_during_find(p2p); | |
3050 | break; | |
3051 | case P2P_INVITE: | |
3052 | p2p_timeout_invite(p2p); | |
3053 | break; | |
3054 | case P2P_INVITE_LISTEN: | |
3055 | p2p_timeout_invite_listen(p2p); | |
3056 | break; | |
39185dfa JM |
3057 | case P2P_SEARCH_WHEN_READY: |
3058 | break; | |
b22128ef JM |
3059 | } |
3060 | } | |
3061 | ||
3062 | ||
3063 | int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr) | |
3064 | { | |
3065 | struct p2p_device *dev; | |
3066 | ||
3067 | dev = p2p_get_device(p2p, peer_addr); | |
3068 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject " | |
3069 | "connection attempts by peer " MACSTR, MAC2STR(peer_addr)); | |
3070 | if (dev == NULL) { | |
3071 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR | |
3072 | " unknown", MAC2STR(peer_addr)); | |
3073 | return -1; | |
3074 | } | |
3075 | dev->status = P2P_SC_FAIL_REJECTED_BY_USER; | |
3076 | dev->flags |= P2P_DEV_USER_REJECTED; | |
3077 | return 0; | |
3078 | } | |
3079 | ||
3080 | ||
e5a359cf | 3081 | const char * p2p_wps_method_text(enum p2p_wps_method method) |
b22128ef JM |
3082 | { |
3083 | switch (method) { | |
3084 | case WPS_NOT_READY: | |
3085 | return "not-ready"; | |
b22128ef JM |
3086 | case WPS_PIN_DISPLAY: |
3087 | return "Display"; | |
3088 | case WPS_PIN_KEYPAD: | |
3089 | return "Keypad"; | |
3090 | case WPS_PBC: | |
3091 | return "PBC"; | |
3092 | } | |
3093 | ||
3094 | return "??"; | |
3095 | } | |
3096 | ||
3097 | ||
3098 | static const char * p2p_go_state_text(enum p2p_go_state go_state) | |
3099 | { | |
3100 | switch (go_state) { | |
3101 | case UNKNOWN_GO: | |
3102 | return "unknown"; | |
3103 | case LOCAL_GO: | |
3104 | return "local"; | |
3105 | case REMOTE_GO: | |
3106 | return "remote"; | |
3107 | } | |
3108 | ||
3109 | return "??"; | |
3110 | } | |
3111 | ||
3112 | ||
b3ffc80b JM |
3113 | const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p, |
3114 | const u8 *addr, int next) | |
b22128ef JM |
3115 | { |
3116 | struct p2p_device *dev; | |
b22128ef JM |
3117 | |
3118 | if (addr) | |
3119 | dev = p2p_get_device(p2p, addr); | |
3120 | else | |
3121 | dev = dl_list_first(&p2p->devices, struct p2p_device, list); | |
3122 | ||
3123 | if (dev && next) { | |
3124 | dev = dl_list_first(&dev->list, struct p2p_device, list); | |
3125 | if (&dev->list == &p2p->devices) | |
3126 | dev = NULL; | |
3127 | } | |
3128 | ||
3129 | if (dev == NULL) | |
b3ffc80b JM |
3130 | return NULL; |
3131 | ||
3132 | return &dev->info; | |
3133 | } | |
3134 | ||
3135 | ||
3136 | int p2p_get_peer_info_txt(const struct p2p_peer_info *info, | |
3137 | char *buf, size_t buflen) | |
3138 | { | |
3139 | struct p2p_device *dev; | |
3140 | int res; | |
3141 | char *pos, *end; | |
3142 | struct os_time now; | |
3143 | ||
3144 | if (info == NULL) | |
b22128ef JM |
3145 | return -1; |
3146 | ||
b3ffc80b JM |
3147 | dev = (struct p2p_device *) (((u8 *) info) - |
3148 | offsetof(struct p2p_device, info)); | |
3149 | ||
b22128ef JM |
3150 | pos = buf; |
3151 | end = buf + buflen; | |
3152 | ||
b22128ef JM |
3153 | os_get_time(&now); |
3154 | res = os_snprintf(pos, end - pos, | |
3155 | "age=%d\n" | |
3156 | "listen_freq=%d\n" | |
b22128ef JM |
3157 | "wps_method=%s\n" |
3158 | "interface_addr=" MACSTR "\n" | |
3159 | "member_in_go_dev=" MACSTR "\n" | |
3160 | "member_in_go_iface=" MACSTR "\n" | |
b22128ef JM |
3161 | "go_neg_req_sent=%d\n" |
3162 | "go_state=%s\n" | |
3163 | "dialog_token=%u\n" | |
3164 | "intended_addr=" MACSTR "\n" | |
3165 | "country=%c%c\n" | |
3166 | "oper_freq=%d\n" | |
3167 | "req_config_methods=0x%x\n" | |
10c4edde | 3168 | "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n" |
b22128ef JM |
3169 | "status=%d\n" |
3170 | "wait_count=%u\n" | |
3171 | "invitation_reqs=%u\n", | |
3172 | (int) (now.sec - dev->last_seen.sec), | |
3173 | dev->listen_freq, | |
b22128ef JM |
3174 | p2p_wps_method_text(dev->wps_method), |
3175 | MAC2STR(dev->interface_addr), | |
3176 | MAC2STR(dev->member_in_go_dev), | |
3177 | MAC2STR(dev->member_in_go_iface), | |
b22128ef JM |
3178 | dev->go_neg_req_sent, |
3179 | p2p_go_state_text(dev->go_state), | |
3180 | dev->dialog_token, | |
3181 | MAC2STR(dev->intended_addr), | |
3182 | dev->country[0] ? dev->country[0] : '_', | |
3183 | dev->country[1] ? dev->country[1] : '_', | |
3184 | dev->oper_freq, | |
3185 | dev->req_config_methods, | |
3186 | dev->flags & P2P_DEV_PROBE_REQ_ONLY ? | |
3187 | "[PROBE_REQ_ONLY]" : "", | |
3188 | dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "", | |
3189 | dev->flags & P2P_DEV_NOT_YET_READY ? | |
3190 | "[NOT_YET_READY]" : "", | |
3191 | dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "", | |
3192 | dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" : | |
3193 | "", | |
3194 | dev->flags & P2P_DEV_PD_PEER_DISPLAY ? | |
3195 | "[PD_PEER_DISPLAY]" : "", | |
3196 | dev->flags & P2P_DEV_PD_PEER_KEYPAD ? | |
3197 | "[PD_PEER_KEYPAD]" : "", | |
3198 | dev->flags & P2P_DEV_USER_REJECTED ? | |
3199 | "[USER_REJECTED]" : "", | |
3200 | dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ? | |
3201 | "[PEER_WAITING_RESPONSE]" : "", | |
3202 | dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ? | |
3203 | "[PREFER_PERSISTENT_GROUP]" : "", | |
3204 | dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ? | |
3205 | "[WAIT_GO_NEG_RESPONSE]" : "", | |
3206 | dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ? | |
3207 | "[WAIT_GO_NEG_CONFIRM]" : "", | |
3208 | dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ? | |
3209 | "[GROUP_CLIENT_ONLY]" : "", | |
d5b20a73 | 3210 | dev->flags & P2P_DEV_FORCE_FREQ ? |
10c4edde JM |
3211 | "[FORCE_FREQ]" : "", |
3212 | dev->flags & P2P_DEV_PD_FOR_JOIN ? | |
3213 | "[PD_FOR_JOIN]" : "", | |
b22128ef JM |
3214 | dev->status, |
3215 | dev->wait_count, | |
3216 | dev->invitation_reqs); | |
3217 | if (res < 0 || res >= end - pos) | |
3218 | return pos - buf; | |
3219 | pos += res; | |
3220 | ||
3221 | if (dev->ext_listen_period) { | |
3222 | res = os_snprintf(pos, end - pos, | |
3223 | "ext_listen_period=%u\n" | |
3224 | "ext_listen_interval=%u\n", | |
3225 | dev->ext_listen_period, | |
3226 | dev->ext_listen_interval); | |
3227 | if (res < 0 || res >= end - pos) | |
3228 | return pos - buf; | |
3229 | pos += res; | |
3230 | } | |
3231 | ||
3232 | if (dev->oper_ssid_len) { | |
3233 | res = os_snprintf(pos, end - pos, | |
3234 | "oper_ssid=%s\n", | |
3235 | wpa_ssid_txt(dev->oper_ssid, | |
3236 | dev->oper_ssid_len)); | |
3237 | if (res < 0 || res >= end - pos) | |
3238 | return pos - buf; | |
3239 | pos += res; | |
3240 | } | |
3241 | ||
3242 | return pos - buf; | |
3243 | } | |
3244 | ||
3245 | ||
b3bcc0f5 JM |
3246 | int p2p_peer_known(struct p2p_data *p2p, const u8 *addr) |
3247 | { | |
3248 | return p2p_get_device(p2p, addr) != NULL; | |
3249 | } | |
3250 | ||
3251 | ||
b22128ef JM |
3252 | void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled) |
3253 | { | |
3254 | if (enabled) { | |
3255 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client " | |
3256 | "discoverability enabled"); | |
3257 | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
3258 | } else { | |
3259 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client " | |
3260 | "discoverability disabled"); | |
3261 | p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
3262 | } | |
3263 | } | |
3264 | ||
3265 | ||
3266 | static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1, | |
3267 | u32 duration2, u32 interval2) | |
3268 | { | |
3269 | struct wpabuf *req; | |
3270 | struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL; | |
3271 | u8 *len; | |
3272 | ||
3273 | req = wpabuf_alloc(100); | |
3274 | if (req == NULL) | |
3275 | return NULL; | |
3276 | ||
3277 | if (duration1 || interval1) { | |
3278 | os_memset(&desc1, 0, sizeof(desc1)); | |
3279 | desc1.count_type = 1; | |
3280 | desc1.duration = duration1; | |
3281 | desc1.interval = interval1; | |
3282 | ptr1 = &desc1; | |
3283 | ||
3284 | if (duration2 || interval2) { | |
3285 | os_memset(&desc2, 0, sizeof(desc2)); | |
3286 | desc2.count_type = 2; | |
3287 | desc2.duration = duration2; | |
3288 | desc2.interval = interval2; | |
3289 | ptr2 = &desc2; | |
3290 | } | |
3291 | } | |
3292 | ||
3293 | p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1); | |
3294 | len = p2p_buf_add_ie_hdr(req); | |
3295 | p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2); | |
3296 | p2p_buf_update_ie_hdr(req, len); | |
3297 | ||
3298 | return req; | |
3299 | } | |
3300 | ||
3301 | ||
3302 | int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr, | |
3303 | const u8 *own_interface_addr, unsigned int freq, | |
3304 | u32 duration1, u32 interval1, u32 duration2, | |
3305 | u32 interval2) | |
3306 | { | |
3307 | struct wpabuf *req; | |
3308 | ||
3309 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to " | |
3310 | "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u " | |
3311 | "int1=%u dur2=%u int2=%u", | |
3312 | MAC2STR(go_interface_addr), MAC2STR(own_interface_addr), | |
3313 | freq, duration1, interval1, duration2, interval2); | |
3314 | ||
3315 | req = p2p_build_presence_req(duration1, interval1, duration2, | |
3316 | interval2); | |
3317 | if (req == NULL) | |
3318 | return -1; | |
3319 | ||
3320 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3f9285ff JM |
3321 | if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr, |
3322 | go_interface_addr, | |
3323 | wpabuf_head(req), wpabuf_len(req), 200) < 0) { | |
b22128ef JM |
3324 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
3325 | "P2P: Failed to send Action frame"); | |
3326 | } | |
3327 | wpabuf_free(req); | |
3328 | ||
3329 | return 0; | |
3330 | } | |
3331 | ||
3332 | ||
3333 | static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa, | |
3334 | size_t noa_len, u8 dialog_token) | |
3335 | { | |
3336 | struct wpabuf *resp; | |
3337 | u8 *len; | |
3338 | ||
3339 | resp = wpabuf_alloc(100 + noa_len); | |
3340 | if (resp == NULL) | |
3341 | return NULL; | |
3342 | ||
3343 | p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token); | |
3344 | len = p2p_buf_add_ie_hdr(resp); | |
3345 | p2p_buf_add_status(resp, status); | |
3346 | if (noa) { | |
3347 | wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE); | |
3348 | wpabuf_put_le16(resp, noa_len); | |
3349 | wpabuf_put_data(resp, noa, noa_len); | |
3350 | } else | |
3351 | p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL); | |
3352 | p2p_buf_update_ie_hdr(resp, len); | |
3353 | ||
3354 | return resp; | |
3355 | } | |
3356 | ||
3357 | ||
3358 | static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, | |
3359 | const u8 *sa, const u8 *data, size_t len, | |
3360 | int rx_freq) | |
3361 | { | |
3362 | struct p2p_message msg; | |
3363 | u8 status; | |
3364 | struct wpabuf *resp; | |
3365 | size_t g; | |
3366 | struct p2p_group *group = NULL; | |
3367 | int parsed = 0; | |
3368 | u8 noa[50]; | |
3369 | int noa_len; | |
3370 | ||
3371 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3372 | "P2P: Received P2P Action - P2P Presence Request"); | |
3373 | ||
3374 | for (g = 0; g < p2p->num_groups; g++) { | |
3375 | if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]), | |
3376 | ETH_ALEN) == 0) { | |
3377 | group = p2p->groups[g]; | |
3378 | break; | |
3379 | } | |
3380 | } | |
3381 | if (group == NULL) { | |
3382 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3383 | "P2P: Ignore P2P Presence Request for unknown group " | |
3384 | MACSTR, MAC2STR(da)); | |
3385 | return; | |
3386 | } | |
3387 | ||
3388 | if (p2p_parse(data, len, &msg) < 0) { | |
3389 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3390 | "P2P: Failed to parse P2P Presence Request"); | |
3391 | status = P2P_SC_FAIL_INVALID_PARAMS; | |
3392 | goto fail; | |
3393 | } | |
3394 | parsed = 1; | |
3395 | ||
3396 | if (msg.noa == NULL) { | |
3397 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3398 | "P2P: No NoA attribute in P2P Presence Request"); | |
3399 | status = P2P_SC_FAIL_INVALID_PARAMS; | |
3400 | goto fail; | |
3401 | } | |
3402 | ||
3403 | status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len); | |
3404 | ||
3405 | fail: | |
3406 | if (p2p->cfg->get_noa) | |
3407 | noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa, | |
3408 | sizeof(noa)); | |
3409 | else | |
3410 | noa_len = -1; | |
3411 | resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL, | |
3412 | noa_len > 0 ? noa_len : 0, | |
3413 | msg.dialog_token); | |
3414 | if (parsed) | |
3415 | p2p_parse_free(&msg); | |
3416 | if (resp == NULL) | |
3417 | return; | |
3418 | ||
3419 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3f9285ff JM |
3420 | if (p2p_send_action(p2p, rx_freq, sa, da, da, |
3421 | wpabuf_head(resp), wpabuf_len(resp), 200) < 0) { | |
b22128ef JM |
3422 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, |
3423 | "P2P: Failed to send Action frame"); | |
3424 | } | |
3425 | wpabuf_free(resp); | |
3426 | } | |
3427 | ||
3428 | ||
3429 | static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, | |
3430 | const u8 *sa, const u8 *data, size_t len) | |
3431 | { | |
3432 | struct p2p_message msg; | |
3433 | ||
3434 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3435 | "P2P: Received P2P Action - P2P Presence Response"); | |
3436 | ||
3437 | if (p2p_parse(data, len, &msg) < 0) { | |
3438 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3439 | "P2P: Failed to parse P2P Presence Response"); | |
3440 | return; | |
3441 | } | |
3442 | ||
3443 | if (msg.status == NULL || msg.noa == NULL) { | |
3444 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3445 | "P2P: No Status or NoA attribute in P2P Presence " | |
3446 | "Response"); | |
3447 | p2p_parse_free(&msg); | |
3448 | return; | |
3449 | } | |
3450 | ||
3451 | if (*msg.status) { | |
3452 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3453 | "P2P: P2P Presence Request was rejected: status %u", | |
3454 | *msg.status); | |
3455 | p2p_parse_free(&msg); | |
3456 | return; | |
3457 | } | |
3458 | ||
3459 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3460 | "P2P: P2P Presence Request was accepted"); | |
3461 | wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA", | |
3462 | msg.noa, msg.noa_len); | |
3463 | /* TODO: process NoA */ | |
3464 | p2p_parse_free(&msg); | |
3465 | } | |
3466 | ||
3467 | ||
3468 | static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx) | |
3469 | { | |
3470 | struct p2p_data *p2p = eloop_ctx; | |
3471 | ||
3472 | if (p2p->ext_listen_interval) { | |
3473 | /* Schedule next extended listen timeout */ | |
3474 | eloop_register_timeout(p2p->ext_listen_interval_sec, | |
3475 | p2p->ext_listen_interval_usec, | |
3476 | p2p_ext_listen_timeout, p2p, NULL); | |
3477 | } | |
3478 | ||
f7a69057 JM |
3479 | if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) { |
3480 | /* | |
3481 | * This should not really happen, but it looks like the Listen | |
3482 | * command may fail is something else (e.g., a scan) was | |
3483 | * running at an inconvenient time. As a workaround, allow new | |
3484 | * Extended Listen operation to be started. | |
3485 | */ | |
3486 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous " | |
3487 | "Extended Listen operation had not been completed - " | |
3488 | "try again"); | |
3489 | p2p->ext_listen_only = 0; | |
3490 | p2p_set_state(p2p, P2P_IDLE); | |
3491 | } | |
3492 | ||
b22128ef JM |
3493 | if (p2p->state != P2P_IDLE) { |
3494 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended " | |
3495 | "Listen timeout in active state (%s)", | |
3496 | p2p_state_txt(p2p->state)); | |
3497 | return; | |
3498 | } | |
3499 | ||
3500 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout"); | |
3501 | p2p->ext_listen_only = 1; | |
3502 | if (p2p_listen(p2p, p2p->ext_listen_period) < 0) { | |
3503 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start " | |
3504 | "Listen state for Extended Listen Timing"); | |
3505 | p2p->ext_listen_only = 0; | |
3506 | } | |
3507 | } | |
3508 | ||
3509 | ||
3510 | int p2p_ext_listen(struct p2p_data *p2p, unsigned int period, | |
3511 | unsigned int interval) | |
3512 | { | |
3513 | if (period > 65535 || interval > 65535 || period > interval || | |
3514 | (period == 0 && interval > 0) || (period > 0 && interval == 0)) { | |
3515 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3516 | "P2P: Invalid Extended Listen Timing request: " | |
3517 | "period=%u interval=%u", period, interval); | |
3518 | return -1; | |
3519 | } | |
3520 | ||
3521 | eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); | |
3522 | ||
3523 | if (interval == 0) { | |
3524 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3525 | "P2P: Disabling Extended Listen Timing"); | |
3526 | p2p->ext_listen_period = 0; | |
3527 | p2p->ext_listen_interval = 0; | |
3528 | return 0; | |
3529 | } | |
3530 | ||
3531 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, | |
3532 | "P2P: Enabling Extended Listen Timing: period %u msec, " | |
3533 | "interval %u msec", period, interval); | |
3534 | p2p->ext_listen_period = period; | |
3535 | p2p->ext_listen_interval = interval; | |
3536 | p2p->ext_listen_interval_sec = interval / 1000; | |
3537 | p2p->ext_listen_interval_usec = (interval % 1000) * 1000; | |
3538 | ||
3539 | eloop_register_timeout(p2p->ext_listen_interval_sec, | |
3540 | p2p->ext_listen_interval_usec, | |
3541 | p2p_ext_listen_timeout, p2p, NULL); | |
3542 | ||
3543 | return 0; | |
3544 | } | |
3545 | ||
3546 | ||
3547 | void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, | |
3548 | const u8 *ie, size_t ie_len) | |
3549 | { | |
3550 | struct p2p_message msg; | |
3551 | ||
3552 | if (bssid == NULL || ie == NULL) | |
3553 | return; | |
3554 | ||
3555 | os_memset(&msg, 0, sizeof(msg)); | |
3556 | if (p2p_parse_ies(ie, ie_len, &msg)) | |
3557 | return; | |
3558 | if (msg.minor_reason_code == NULL) | |
3559 | return; | |
3560 | ||
3561 | wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, | |
3562 | "P2P: Deauthentication notification BSSID " MACSTR | |
3563 | " reason_code=%u minor_reason_code=%u", | |
3564 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); | |
3565 | ||
3566 | p2p_parse_free(&msg); | |
3567 | } | |
3568 | ||
3569 | ||
3570 | void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, | |
3571 | const u8 *ie, size_t ie_len) | |
3572 | { | |
3573 | struct p2p_message msg; | |
3574 | ||
3575 | if (bssid == NULL || ie == NULL) | |
3576 | return; | |
3577 | ||
3578 | os_memset(&msg, 0, sizeof(msg)); | |
3579 | if (p2p_parse_ies(ie, ie_len, &msg)) | |
3580 | return; | |
3581 | if (msg.minor_reason_code == NULL) | |
3582 | return; | |
3583 | ||
3584 | wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, | |
3585 | "P2P: Disassociation notification BSSID " MACSTR | |
3586 | " reason_code=%u minor_reason_code=%u", | |
3587 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); | |
3588 | ||
3589 | p2p_parse_free(&msg); | |
3590 | } | |
3591 | ||
3592 | ||
3593 | void p2p_set_managed_oper(struct p2p_data *p2p, int enabled) | |
3594 | { | |
3595 | if (enabled) { | |
3596 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P " | |
3597 | "Device operations enabled"); | |
3598 | p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED; | |
3599 | } else { | |
3600 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P " | |
3601 | "Device operations disabled"); | |
3602 | p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED; | |
3603 | } | |
3604 | } | |
3605 | ||
3606 | ||
3607 | int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel) | |
3608 | { | |
3609 | if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0) | |
3610 | return -1; | |
3611 | ||
3612 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: " | |
3613 | "reg_class %u channel %u", reg_class, channel); | |
3614 | p2p->cfg->reg_class = reg_class; | |
3615 | p2p->cfg->channel = channel; | |
3616 | ||
3617 | return 0; | |
3618 | } | |
3619 | ||
3620 | ||
3621 | int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len) | |
3622 | { | |
3623 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len); | |
3624 | if (postfix == NULL) { | |
3625 | p2p->cfg->ssid_postfix_len = 0; | |
3626 | return 0; | |
3627 | } | |
3628 | if (len > sizeof(p2p->cfg->ssid_postfix)) | |
3629 | return -1; | |
3630 | os_memcpy(p2p->cfg->ssid_postfix, postfix, len); | |
3631 | p2p->cfg->ssid_postfix_len = len; | |
3632 | return 0; | |
3633 | } | |
3634 | ||
3635 | ||
2463ba70 JS |
3636 | int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel, |
3637 | int cfg_op_channel) | |
3638 | { | |
3639 | if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel) | |
3640 | < 0) | |
3641 | return -1; | |
3642 | ||
3643 | wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: " | |
3644 | "reg_class %u channel %u", op_reg_class, op_channel); | |
3645 | p2p->cfg->op_reg_class = op_reg_class; | |
3646 | p2p->cfg->op_channel = op_channel; | |
3647 | p2p->cfg->cfg_op_channel = cfg_op_channel; | |
3648 | return 0; | |
3649 | } | |
3650 | ||
3651 | ||
b22128ef JM |
3652 | int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr, |
3653 | u8 *iface_addr) | |
3654 | { | |
3655 | struct p2p_device *dev = p2p_get_device(p2p, dev_addr); | |
3656 | if (dev == NULL || is_zero_ether_addr(dev->interface_addr)) | |
3657 | return -1; | |
3658 | os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN); | |
3659 | return 0; | |
3660 | } | |
80c9582a JM |
3661 | |
3662 | ||
4147a2cc JM |
3663 | int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr, |
3664 | u8 *dev_addr) | |
3665 | { | |
3666 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); | |
3667 | if (dev == NULL) | |
3668 | return -1; | |
c5db8e51 | 3669 | os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN); |
4147a2cc JM |
3670 | return 0; |
3671 | } | |
3672 | ||
3673 | ||
80c9582a JM |
3674 | void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr) |
3675 | { | |
3676 | os_memcpy(p2p->peer_filter, addr, ETH_ALEN); | |
3677 | if (is_zero_ether_addr(p2p->peer_filter)) | |
3678 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer " | |
3679 | "filter"); | |
3680 | else | |
3681 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer " | |
3682 | "filter for " MACSTR, MAC2STR(p2p->peer_filter)); | |
3683 | } | |
72044390 JM |
3684 | |
3685 | ||
3686 | void p2p_set_cross_connect(struct p2p_data *p2p, int enabled) | |
3687 | { | |
3688 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s", | |
3689 | enabled ? "enabled" : "disabled"); | |
3690 | if (p2p->cross_connect == enabled) | |
3691 | return; | |
3692 | p2p->cross_connect = enabled; | |
3693 | /* TODO: may need to tear down any action group where we are GO(?) */ | |
3694 | } | |
f8d0131a JM |
3695 | |
3696 | ||
3697 | int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr) | |
3698 | { | |
3699 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); | |
3700 | if (dev == NULL) | |
3701 | return -1; | |
3702 | if (dev->oper_freq <= 0) | |
3703 | return -1; | |
3704 | return dev->oper_freq; | |
3705 | } | |
0f66abd2 SS |
3706 | |
3707 | ||
3708 | void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled) | |
3709 | { | |
3710 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s", | |
3711 | enabled ? "enabled" : "disabled"); | |
3712 | p2p->cfg->p2p_intra_bss = enabled; | |
3713 | } | |
b5c9da8d JM |
3714 | |
3715 | ||
3716 | void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan) | |
3717 | { | |
3718 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list"); | |
3719 | os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels)); | |
3720 | } | |
3f9285ff JM |
3721 | |
3722 | ||
3723 | int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst, | |
3724 | const u8 *src, const u8 *bssid, const u8 *buf, | |
3725 | size_t len, unsigned int wait_time) | |
3726 | { | |
3727 | if (p2p->p2p_scan_running) { | |
3728 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action " | |
3729 | "frame TX until p2p_scan completes"); | |
3730 | if (p2p->after_scan_tx) { | |
3731 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped " | |
3732 | "previous pending Action frame TX"); | |
3733 | os_free(p2p->after_scan_tx); | |
3734 | } | |
3735 | p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) + | |
3736 | len); | |
3737 | if (p2p->after_scan_tx == NULL) | |
3738 | return -1; | |
3739 | p2p->after_scan_tx->freq = freq; | |
3740 | os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN); | |
3741 | os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN); | |
3742 | os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN); | |
3743 | p2p->after_scan_tx->len = len; | |
3744 | p2p->after_scan_tx->wait_time = wait_time; | |
3745 | os_memcpy(p2p->after_scan_tx + 1, buf, len); | |
3746 | return 0; | |
3747 | } | |
3748 | ||
3749 | return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid, | |
3750 | buf, len, wait_time); | |
3751 | } | |
7cfc4ac3 AGS |
3752 | |
3753 | ||
3754 | void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5, | |
3755 | int freq_overall) | |
3756 | { | |
3757 | wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d," | |
3758 | " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall); | |
3759 | p2p->best_freq_24 = freq_24; | |
3760 | p2p->best_freq_5 = freq_5; | |
3761 | p2p->best_freq_overall = freq_overall; | |
3762 | } | |
231bbd03 SS |
3763 | |
3764 | ||
3765 | const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p) | |
3766 | { | |
3767 | if (p2p == NULL || p2p->go_neg_peer == NULL) | |
3768 | return NULL; | |
c5db8e51 | 3769 | return p2p->go_neg_peer->info.p2p_device_addr; |
231bbd03 | 3770 | } |
c165d81e JB |
3771 | |
3772 | ||
3773 | const struct p2p_peer_info * | |
3774 | p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next) | |
3775 | { | |
3776 | struct p2p_device *dev; | |
3777 | ||
3778 | if (addr) { | |
3779 | dev = p2p_get_device(p2p, addr); | |
3780 | if (!dev) | |
3781 | return NULL; | |
3782 | ||
3783 | if (!next) { | |
3784 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) | |
3785 | return NULL; | |
3786 | ||
3787 | return &dev->info; | |
3788 | } else { | |
3789 | do { | |
3790 | dev = dl_list_first(&dev->list, | |
3791 | struct p2p_device, | |
3792 | list); | |
3793 | if (&dev->list == &p2p->devices) | |
3794 | return NULL; | |
3795 | } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY); | |
3796 | } | |
3797 | } else { | |
3798 | dev = dl_list_first(&p2p->devices, struct p2p_device, list); | |
3799 | if (!dev) | |
3800 | return NULL; | |
3801 | while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { | |
3802 | dev = dl_list_first(&dev->list, | |
3803 | struct p2p_device, | |
3804 | list); | |
3805 | if (&dev->list == &p2p->devices) | |
3806 | return NULL; | |
3807 | } | |
3808 | } | |
3809 | ||
3810 | return &dev->info; | |
3811 | } | |
303f60d3 JM |
3812 | |
3813 | ||
3814 | int p2p_in_progress(struct p2p_data *p2p) | |
3815 | { | |
3816 | if (p2p == NULL) | |
3817 | return 0; | |
fc6997b3 | 3818 | return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING; |
303f60d3 | 3819 | } |