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