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