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