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