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