]>
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); | |
8e9f53c3 JM |
1765 | p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr, |
1766 | p2p->invite_dev_pw_id); | |
b22128ef JM |
1767 | } |
1768 | ||
1769 | ||
1770 | static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr, | |
1771 | const u8 *ie, size_t ie_len) | |
1772 | { | |
1773 | struct p2p_message msg; | |
1774 | struct p2p_device *dev; | |
1775 | ||
1776 | os_memset(&msg, 0, sizeof(msg)); | |
1777 | if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL) | |
1778 | { | |
1779 | p2p_parse_free(&msg); | |
1780 | return; /* not a P2P probe */ | |
1781 | } | |
1782 | ||
1783 | if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN || | |
1784 | os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) | |
1785 | != 0) { | |
1786 | /* The Probe Request is not part of P2P Device Discovery. It is | |
1787 | * not known whether the source address of the frame is the P2P | |
1788 | * Device Address or P2P Interface Address. Do not add a new | |
1789 | * peer entry based on this frames. | |
1790 | */ | |
1791 | p2p_parse_free(&msg); | |
1792 | return; | |
1793 | } | |
1794 | ||
1795 | dev = p2p_get_device(p2p, addr); | |
1796 | if (dev) { | |
1797 | if (dev->country[0] == 0 && msg.listen_channel) | |
1798 | os_memcpy(dev->country, msg.listen_channel, 3); | |
acb69cec | 1799 | os_get_reltime(&dev->last_seen); |
b22128ef JM |
1800 | p2p_parse_free(&msg); |
1801 | return; /* already known */ | |
1802 | } | |
1803 | ||
1804 | dev = p2p_create_device(p2p, addr); | |
1805 | if (dev == NULL) { | |
1806 | p2p_parse_free(&msg); | |
1807 | return; | |
1808 | } | |
1809 | ||
acb69cec | 1810 | os_get_reltime(&dev->last_seen); |
b22128ef JM |
1811 | dev->flags |= P2P_DEV_PROBE_REQ_ONLY; |
1812 | ||
b22128ef JM |
1813 | if (msg.listen_channel) { |
1814 | os_memcpy(dev->country, msg.listen_channel, 3); | |
9ccd9165 | 1815 | dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3], |
b22128ef JM |
1816 | msg.listen_channel[4]); |
1817 | } | |
1818 | ||
ed496f13 | 1819 | p2p_copy_wps_info(p2p, dev, 1, &msg); |
e57ae6e1 | 1820 | |
9675ce35 JM |
1821 | if (msg.wfd_subelems) { |
1822 | wpabuf_free(dev->info.wfd_subelems); | |
1823 | dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems); | |
1824 | } | |
1825 | ||
b22128ef JM |
1826 | p2p_parse_free(&msg); |
1827 | ||
ed496f13 | 1828 | p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR |
b22128ef | 1829 | " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d", |
c5db8e51 KRK |
1830 | MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab, |
1831 | dev->info.group_capab, dev->info.device_name, | |
1832 | dev->listen_freq); | |
b22128ef JM |
1833 | } |
1834 | ||
1835 | ||
1836 | struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p, | |
1837 | const u8 *addr, | |
1838 | struct p2p_message *msg) | |
1839 | { | |
1840 | struct p2p_device *dev; | |
1841 | ||
1842 | dev = p2p_get_device(p2p, addr); | |
1843 | if (dev) { | |
acb69cec | 1844 | os_get_reltime(&dev->last_seen); |
b22128ef JM |
1845 | return dev; /* already known */ |
1846 | } | |
1847 | ||
1848 | dev = p2p_create_device(p2p, addr); | |
1849 | if (dev == NULL) | |
1850 | return NULL; | |
1851 | ||
1852 | p2p_add_dev_info(p2p, addr, dev, msg); | |
1853 | ||
1854 | return dev; | |
1855 | } | |
1856 | ||
1857 | ||
1858 | static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type) | |
1859 | { | |
1860 | if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0) | |
1861 | return 1; | |
1862 | if (os_memcmp(dev_type, req_dev_type, 2) == 0 && | |
1863 | WPA_GET_BE32(&req_dev_type[2]) == 0 && | |
1864 | WPA_GET_BE16(&req_dev_type[6]) == 0) | |
1865 | return 1; /* Category match with wildcard OUI/sub-category */ | |
1866 | return 0; | |
1867 | } | |
1868 | ||
1869 | ||
1870 | int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[], | |
1871 | size_t num_req_dev_type) | |
1872 | { | |
1873 | size_t i; | |
1874 | for (i = 0; i < num_req_dev_type; i++) { | |
1875 | if (dev_type_match(dev_type, req_dev_type[i])) | |
1876 | return 1; | |
1877 | } | |
1878 | return 0; | |
1879 | } | |
1880 | ||
1881 | ||
1882 | /** | |
1883 | * p2p_match_dev_type - Match local device type with requested type | |
1884 | * @p2p: P2P module context from p2p_init() | |
1885 | * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs) | |
1886 | * Returns: 1 on match, 0 on mismatch | |
1887 | * | |
1888 | * This function can be used to match the Requested Device Type attribute in | |
1889 | * WPS IE with the local device types for deciding whether to reply to a Probe | |
1890 | * Request frame. | |
1891 | */ | |
1892 | int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps) | |
1893 | { | |
1894 | struct wps_parse_attr attr; | |
1895 | size_t i; | |
1896 | ||
1897 | if (wps_parse_msg(wps, &attr)) | |
1898 | return 1; /* assume no Requested Device Type attributes */ | |
1899 | ||
1900 | if (attr.num_req_dev_type == 0) | |
1901 | return 1; /* no Requested Device Type attributes -> match */ | |
1902 | ||
1903 | if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type, | |
1904 | attr.num_req_dev_type)) | |
1905 | return 1; /* Own Primary Device Type matches */ | |
1906 | ||
1907 | for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) | |
1908 | if (dev_type_list_match(p2p->cfg->sec_dev_type[i], | |
1909 | attr.req_dev_type, | |
1910 | attr.num_req_dev_type)) | |
1911 | return 1; /* Own Secondary Device Type matches */ | |
1912 | ||
1913 | /* No matching device type found */ | |
1914 | return 0; | |
1915 | } | |
1916 | ||
1917 | ||
1918 | struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p) | |
1919 | { | |
1920 | struct wpabuf *buf; | |
1921 | u8 *len; | |
360182ed | 1922 | int pw_id = -1; |
9675ce35 | 1923 | size_t extra = 0; |
b22128ef | 1924 | |
9675ce35 JM |
1925 | #ifdef CONFIG_WIFI_DISPLAY |
1926 | if (p2p->wfd_ie_probe_resp) | |
1927 | extra = wpabuf_len(p2p->wfd_ie_probe_resp); | |
1928 | #endif /* CONFIG_WIFI_DISPLAY */ | |
1929 | ||
1930 | buf = wpabuf_alloc(1000 + extra); | |
b22128ef JM |
1931 | if (buf == NULL) |
1932 | return NULL; | |
1933 | ||
360182ed JM |
1934 | if (p2p->go_neg_peer) { |
1935 | /* Advertise immediate availability of WPS credential */ | |
1936 | pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method); | |
1937 | } | |
1938 | ||
33183761 JM |
1939 | if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) { |
1940 | p2p_dbg(p2p, "Failed to build WPS IE for Probe Response"); | |
1941 | wpabuf_free(buf); | |
1942 | return NULL; | |
1943 | } | |
b22128ef | 1944 | |
9675ce35 JM |
1945 | #ifdef CONFIG_WIFI_DISPLAY |
1946 | if (p2p->wfd_ie_probe_resp) | |
1947 | wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp); | |
1948 | #endif /* CONFIG_WIFI_DISPLAY */ | |
1949 | ||
b22128ef JM |
1950 | /* P2P IE */ |
1951 | len = p2p_buf_add_ie_hdr(buf); | |
18485b54 MH |
1952 | p2p_buf_add_capability(buf, p2p->dev_capab & |
1953 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); | |
b22128ef JM |
1954 | if (p2p->ext_listen_interval) |
1955 | p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period, | |
1956 | p2p->ext_listen_interval); | |
1957 | p2p_buf_add_device_info(buf, p2p, NULL); | |
1958 | p2p_buf_update_ie_hdr(buf, len); | |
1959 | ||
1960 | return buf; | |
1961 | } | |
1962 | ||
1963 | ||
2d43d37f JB |
1964 | static enum p2p_probe_req_status |
1965 | p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst, | |
1966 | const u8 *bssid, const u8 *ie, size_t ie_len) | |
b22128ef JM |
1967 | { |
1968 | struct ieee802_11_elems elems; | |
1969 | struct wpabuf *buf; | |
1970 | struct ieee80211_mgmt *resp; | |
97c5b3c4 | 1971 | struct p2p_message msg; |
b22128ef JM |
1972 | struct wpabuf *ies; |
1973 | ||
1974 | if (!p2p->in_listen || !p2p->drv_in_listen) { | |
1975 | /* not in Listen state - ignore Probe Request */ | |
2d43d37f | 1976 | return P2P_PREQ_NOT_LISTEN; |
b22128ef JM |
1977 | } |
1978 | ||
1979 | if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) == | |
1980 | ParseFailed) { | |
1981 | /* Ignore invalid Probe Request frames */ | |
2d43d37f | 1982 | return P2P_PREQ_MALFORMED; |
b22128ef JM |
1983 | } |
1984 | ||
1985 | if (elems.p2p == NULL) { | |
1986 | /* not a P2P probe - ignore it */ | |
2d43d37f | 1987 | return P2P_PREQ_NOT_P2P; |
b22128ef JM |
1988 | } |
1989 | ||
04a85e44 JM |
1990 | if (dst && !is_broadcast_ether_addr(dst) && |
1991 | os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) { | |
1992 | /* Not sent to the broadcast address or our P2P Device Address | |
1993 | */ | |
2d43d37f | 1994 | return P2P_PREQ_NOT_PROCESSED; |
04a85e44 JM |
1995 | } |
1996 | ||
1997 | if (bssid && !is_broadcast_ether_addr(bssid)) { | |
1998 | /* Not sent to the Wildcard BSSID */ | |
2d43d37f | 1999 | return P2P_PREQ_NOT_PROCESSED; |
04a85e44 JM |
2000 | } |
2001 | ||
b22128ef JM |
2002 | if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN || |
2003 | os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) != | |
2004 | 0) { | |
2005 | /* not using P2P Wildcard SSID - ignore */ | |
2d43d37f | 2006 | return P2P_PREQ_NOT_PROCESSED; |
b22128ef JM |
2007 | } |
2008 | ||
e1d52629 JM |
2009 | if (supp_rates_11b_only(&elems)) { |
2010 | /* Indicates support for 11b rates only */ | |
2d43d37f | 2011 | return P2P_PREQ_NOT_P2P; |
e1d52629 JM |
2012 | } |
2013 | ||
97c5b3c4 JM |
2014 | os_memset(&msg, 0, sizeof(msg)); |
2015 | if (p2p_parse_ies(ie, ie_len, &msg) < 0) { | |
2016 | /* Could not parse P2P attributes */ | |
2d43d37f | 2017 | return P2P_PREQ_NOT_P2P; |
97c5b3c4 JM |
2018 | } |
2019 | ||
2020 | if (msg.device_id && | |
1c7447d0 | 2021 | os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) { |
97c5b3c4 JM |
2022 | /* Device ID did not match */ |
2023 | p2p_parse_free(&msg); | |
2d43d37f | 2024 | return P2P_PREQ_NOT_PROCESSED; |
97c5b3c4 JM |
2025 | } |
2026 | ||
b22128ef | 2027 | /* Check Requested Device Type match */ |
97c5b3c4 JM |
2028 | if (msg.wps_attributes && |
2029 | !p2p_match_dev_type(p2p, msg.wps_attributes)) { | |
b22128ef | 2030 | /* No match with Requested Device Type */ |
97c5b3c4 | 2031 | p2p_parse_free(&msg); |
2d43d37f | 2032 | return P2P_PREQ_NOT_PROCESSED; |
b22128ef | 2033 | } |
97c5b3c4 | 2034 | p2p_parse_free(&msg); |
b22128ef | 2035 | |
2d43d37f JB |
2036 | if (!p2p->cfg->send_probe_resp) { |
2037 | /* Response generated elsewhere */ | |
2038 | return P2P_PREQ_NOT_PROCESSED; | |
2039 | } | |
b22128ef | 2040 | |
ed496f13 | 2041 | p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state"); |
b22128ef JM |
2042 | |
2043 | /* | |
2044 | * We do not really have a specific BSS that this frame is advertising, | |
2045 | * so build a frame that has some information in valid format. This is | |
2046 | * really only used for discovery purposes, not to learn exact BSS | |
2047 | * parameters. | |
2048 | */ | |
2049 | ies = p2p_build_probe_resp_ies(p2p); | |
2050 | if (ies == NULL) | |
2d43d37f | 2051 | return P2P_PREQ_NOT_PROCESSED; |
b22128ef JM |
2052 | |
2053 | buf = wpabuf_alloc(200 + wpabuf_len(ies)); | |
2054 | if (buf == NULL) { | |
2055 | wpabuf_free(ies); | |
2d43d37f | 2056 | return P2P_PREQ_NOT_PROCESSED; |
b22128ef JM |
2057 | } |
2058 | ||
2059 | resp = NULL; | |
2060 | resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp); | |
2061 | ||
2062 | resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) | | |
2063 | (WLAN_FC_STYPE_PROBE_RESP << 4)); | |
2064 | os_memcpy(resp->da, addr, ETH_ALEN); | |
2065 | os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN); | |
2066 | os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN); | |
2067 | resp->u.probe_resp.beacon_int = host_to_le16(100); | |
2068 | /* hardware or low-level driver will setup seq_ctrl and timestamp */ | |
2069 | resp->u.probe_resp.capab_info = | |
2070 | host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE | | |
2071 | WLAN_CAPABILITY_PRIVACY | | |
2072 | WLAN_CAPABILITY_SHORT_SLOT_TIME); | |
2073 | ||
2074 | wpabuf_put_u8(buf, WLAN_EID_SSID); | |
2075 | wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN); | |
2076 | wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); | |
2077 | ||
2078 | wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES); | |
2079 | wpabuf_put_u8(buf, 8); | |
2080 | wpabuf_put_u8(buf, (60 / 5) | 0x80); | |
2081 | wpabuf_put_u8(buf, 90 / 5); | |
2082 | wpabuf_put_u8(buf, (120 / 5) | 0x80); | |
2083 | wpabuf_put_u8(buf, 180 / 5); | |
2084 | wpabuf_put_u8(buf, (240 / 5) | 0x80); | |
2085 | wpabuf_put_u8(buf, 360 / 5); | |
2086 | wpabuf_put_u8(buf, 480 / 5); | |
2087 | wpabuf_put_u8(buf, 540 / 5); | |
2088 | ||
2089 | wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS); | |
2090 | wpabuf_put_u8(buf, 1); | |
2091 | wpabuf_put_u8(buf, p2p->cfg->channel); | |
2092 | ||
2093 | wpabuf_put_buf(buf, ies); | |
2094 | wpabuf_free(ies); | |
2095 | ||
2096 | p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf); | |
2097 | ||
2098 | wpabuf_free(buf); | |
2d43d37f JB |
2099 | |
2100 | return P2P_PREQ_NOT_PROCESSED; | |
b22128ef JM |
2101 | } |
2102 | ||
2103 | ||
2d43d37f JB |
2104 | enum p2p_probe_req_status |
2105 | p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst, | |
2106 | const u8 *bssid, const u8 *ie, size_t ie_len) | |
b22128ef | 2107 | { |
2d43d37f JB |
2108 | enum p2p_probe_req_status res; |
2109 | ||
b22128ef JM |
2110 | p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len); |
2111 | ||
2d43d37f | 2112 | res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len); |
b22128ef JM |
2113 | |
2114 | if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) && | |
2115 | p2p->go_neg_peer && | |
c5db8e51 | 2116 | os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN) |
c03e2113 JM |
2117 | == 0 && |
2118 | !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) { | |
b22128ef | 2119 | /* Received a Probe Request from GO Negotiation peer */ |
ed496f13 | 2120 | p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout"); |
8cee87ab | 2121 | eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL); |
b22128ef | 2122 | eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL); |
2d43d37f | 2123 | return P2P_PREQ_PROCESSED; |
b22128ef JM |
2124 | } |
2125 | ||
2126 | if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) && | |
2127 | p2p->invite_peer && | |
a05e236b | 2128 | (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) && |
c5db8e51 KRK |
2129 | os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN) |
2130 | == 0) { | |
b22128ef | 2131 | /* Received a Probe Request from Invite peer */ |
ed496f13 | 2132 | p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout"); |
b22128ef | 2133 | eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL); |
2d43d37f | 2134 | return P2P_PREQ_PROCESSED; |
b22128ef JM |
2135 | } |
2136 | ||
2d43d37f | 2137 | return res; |
b22128ef JM |
2138 | } |
2139 | ||
2140 | ||
2141 | static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid, | |
4c08c0bd | 2142 | u8 *buf, size_t len, struct wpabuf *p2p_ie) |
b22128ef JM |
2143 | { |
2144 | struct wpabuf *tmp; | |
2145 | u8 *lpos; | |
2146 | size_t tmplen; | |
2147 | int res; | |
72044390 | 2148 | u8 group_capab; |
b22128ef | 2149 | |
4c08c0bd JM |
2150 | if (p2p_ie == NULL) |
2151 | return 0; /* WLAN AP is not a P2P manager */ | |
b22128ef JM |
2152 | |
2153 | /* | |
2154 | * (Re)Association Request - P2P IE | |
2155 | * P2P Capability attribute (shall be present) | |
4c08c0bd JM |
2156 | * P2P Interface attribute (present if concurrent device and |
2157 | * P2P Management is enabled) | |
b22128ef JM |
2158 | */ |
2159 | tmp = wpabuf_alloc(200); | |
2160 | if (tmp == NULL) | |
2161 | return -1; | |
2162 | ||
2163 | lpos = p2p_buf_add_ie_hdr(tmp); | |
72044390 JM |
2164 | group_capab = 0; |
2165 | if (p2p->num_groups > 0) { | |
2166 | group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER; | |
2167 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && | |
2168 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) && | |
2169 | p2p->cross_connect) | |
2170 | group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; | |
2171 | } | |
2172 | p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab); | |
4c08c0bd JM |
2173 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && |
2174 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED)) | |
b22128ef JM |
2175 | p2p_buf_add_p2p_interface(tmp, p2p); |
2176 | p2p_buf_update_ie_hdr(tmp, lpos); | |
2177 | ||
2178 | tmplen = wpabuf_len(tmp); | |
2179 | if (tmplen > len) | |
2180 | res = -1; | |
2181 | else { | |
2182 | os_memcpy(buf, wpabuf_head(tmp), tmplen); | |
2183 | res = tmplen; | |
2184 | } | |
2185 | wpabuf_free(tmp); | |
2186 | ||
2187 | return res; | |
2188 | } | |
2189 | ||
2190 | ||
2191 | int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf, | |
4c08c0bd | 2192 | size_t len, int p2p_group, struct wpabuf *p2p_ie) |
b22128ef JM |
2193 | { |
2194 | struct wpabuf *tmp; | |
2195 | u8 *lpos; | |
2196 | struct p2p_device *peer; | |
2197 | size_t tmplen; | |
2198 | int res; | |
9675ce35 | 2199 | size_t extra = 0; |
b22128ef JM |
2200 | |
2201 | if (!p2p_group) | |
4c08c0bd | 2202 | return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie); |
b22128ef | 2203 | |
9675ce35 JM |
2204 | #ifdef CONFIG_WIFI_DISPLAY |
2205 | if (p2p->wfd_ie_assoc_req) | |
2206 | extra = wpabuf_len(p2p->wfd_ie_assoc_req); | |
2207 | #endif /* CONFIG_WIFI_DISPLAY */ | |
2208 | ||
b22128ef JM |
2209 | /* |
2210 | * (Re)Association Request - P2P IE | |
2211 | * P2P Capability attribute (shall be present) | |
2212 | * Extended Listen Timing (may be present) | |
2213 | * P2P Device Info attribute (shall be present) | |
2214 | */ | |
9675ce35 | 2215 | tmp = wpabuf_alloc(200 + extra); |
b22128ef JM |
2216 | if (tmp == NULL) |
2217 | return -1; | |
2218 | ||
9675ce35 JM |
2219 | #ifdef CONFIG_WIFI_DISPLAY |
2220 | if (p2p->wfd_ie_assoc_req) | |
2221 | wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req); | |
2222 | #endif /* CONFIG_WIFI_DISPLAY */ | |
2223 | ||
b22128ef JM |
2224 | peer = bssid ? p2p_get_device(p2p, bssid) : NULL; |
2225 | ||
2226 | lpos = p2p_buf_add_ie_hdr(tmp); | |
2227 | p2p_buf_add_capability(tmp, p2p->dev_capab, 0); | |
5be5305b JM |
2228 | if (p2p->ext_listen_interval) |
2229 | p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period, | |
2230 | p2p->ext_listen_interval); | |
b22128ef JM |
2231 | p2p_buf_add_device_info(tmp, p2p, peer); |
2232 | p2p_buf_update_ie_hdr(tmp, lpos); | |
2233 | ||
2234 | tmplen = wpabuf_len(tmp); | |
2235 | if (tmplen > len) | |
2236 | res = -1; | |
2237 | else { | |
2238 | os_memcpy(buf, wpabuf_head(tmp), tmplen); | |
2239 | res = tmplen; | |
2240 | } | |
2241 | wpabuf_free(tmp); | |
2242 | ||
2243 | return res; | |
2244 | } | |
2245 | ||
2246 | ||
2247 | int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end) | |
2248 | { | |
2249 | struct wpabuf *p2p_ie; | |
2250 | int ret; | |
2251 | ||
2252 | p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE); | |
2253 | if (p2p_ie == NULL) | |
2254 | return 0; | |
2255 | ||
2256 | ret = p2p_attr_text(p2p_ie, buf, end); | |
2257 | wpabuf_free(p2p_ie); | |
2258 | return ret; | |
2259 | } | |
2260 | ||
2261 | ||
c2d76aa6 | 2262 | int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr) |
0a70f34f | 2263 | { |
0a70f34f JM |
2264 | struct p2p_message msg; |
2265 | ||
0a70f34f | 2266 | os_memset(&msg, 0, sizeof(msg)); |
c2d76aa6 | 2267 | if (p2p_parse_p2p_ie(p2p_ie, &msg)) |
0a70f34f | 2268 | return -1; |
0a70f34f | 2269 | |
526ec4ae JM |
2270 | if (msg.p2p_device_addr) { |
2271 | os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN); | |
c2d76aa6 | 2272 | return 0; |
526ec4ae JM |
2273 | } else if (msg.device_id) { |
2274 | os_memcpy(dev_addr, msg.device_id, ETH_ALEN); | |
c2d76aa6 | 2275 | return 0; |
0a70f34f | 2276 | } |
c2d76aa6 MH |
2277 | return -1; |
2278 | } | |
0a70f34f | 2279 | |
c2d76aa6 MH |
2280 | |
2281 | int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr) | |
2282 | { | |
2283 | struct wpabuf *p2p_ie; | |
2284 | int ret; | |
2285 | ||
2286 | p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, | |
2287 | P2P_IE_VENDOR_TYPE); | |
2288 | if (p2p_ie == NULL) | |
2289 | return -1; | |
2290 | ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr); | |
0a70f34f | 2291 | wpabuf_free(p2p_ie); |
526ec4ae | 2292 | return ret; |
0a70f34f JM |
2293 | } |
2294 | ||
2295 | ||
b22128ef JM |
2296 | static void p2p_clear_go_neg(struct p2p_data *p2p) |
2297 | { | |
2298 | p2p->go_neg_peer = NULL; | |
2299 | p2p_clear_timeout(p2p); | |
2300 | p2p_set_state(p2p, P2P_IDLE); | |
2301 | } | |
2302 | ||
2303 | ||
2304 | void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr) | |
2305 | { | |
2306 | if (p2p->go_neg_peer == NULL) { | |
ed496f13 | 2307 | p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification"); |
b22128ef JM |
2308 | return; /* No pending Group Formation */ |
2309 | } | |
2310 | ||
2311 | if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) != | |
2312 | 0) { | |
ed496f13 JM |
2313 | p2p_dbg(p2p, "Ignore WPS registration success notification for " |
2314 | MACSTR " (GO Negotiation peer " MACSTR ")", | |
b22128ef JM |
2315 | MAC2STR(mac_addr), |
2316 | MAC2STR(p2p->go_neg_peer->intended_addr)); | |
2317 | return; /* Ignore unexpected peer address */ | |
2318 | } | |
2319 | ||
ed496f13 | 2320 | p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR, |
b22128ef JM |
2321 | MAC2STR(mac_addr)); |
2322 | ||
2323 | p2p_clear_go_neg(p2p); | |
2324 | } | |
2325 | ||
2326 | ||
2327 | void p2p_group_formation_failed(struct p2p_data *p2p) | |
2328 | { | |
2329 | if (p2p->go_neg_peer == NULL) { | |
ed496f13 | 2330 | p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification"); |
b22128ef JM |
2331 | return; /* No pending Group Formation */ |
2332 | } | |
2333 | ||
ed496f13 | 2334 | p2p_dbg(p2p, "Group Formation failed with " MACSTR, |
b22128ef JM |
2335 | MAC2STR(p2p->go_neg_peer->intended_addr)); |
2336 | ||
2337 | p2p_clear_go_neg(p2p); | |
2338 | } | |
2339 | ||
2340 | ||
2341 | struct p2p_data * p2p_init(const struct p2p_config *cfg) | |
2342 | { | |
2343 | struct p2p_data *p2p; | |
2344 | ||
2345 | if (cfg->max_peers < 1) | |
2346 | return NULL; | |
2347 | ||
2348 | p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg)); | |
2349 | if (p2p == NULL) | |
2350 | return NULL; | |
2351 | p2p->cfg = (struct p2p_config *) (p2p + 1); | |
2352 | os_memcpy(p2p->cfg, cfg, sizeof(*cfg)); | |
2353 | if (cfg->dev_name) | |
2354 | p2p->cfg->dev_name = os_strdup(cfg->dev_name); | |
b6e01800 JM |
2355 | if (cfg->manufacturer) |
2356 | p2p->cfg->manufacturer = os_strdup(cfg->manufacturer); | |
2357 | if (cfg->model_name) | |
2358 | p2p->cfg->model_name = os_strdup(cfg->model_name); | |
2359 | if (cfg->model_number) | |
2360 | p2p->cfg->model_number = os_strdup(cfg->model_number); | |
2361 | if (cfg->serial_number) | |
2362 | p2p->cfg->serial_number = os_strdup(cfg->serial_number); | |
21d996f7 JM |
2363 | if (cfg->pref_chan) { |
2364 | p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan * | |
2365 | sizeof(struct p2p_channel)); | |
2366 | if (p2p->cfg->pref_chan) { | |
2367 | os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan, | |
2368 | cfg->num_pref_chan * | |
2369 | sizeof(struct p2p_channel)); | |
2370 | } else | |
2371 | p2p->cfg->num_pref_chan = 0; | |
2372 | } | |
b22128ef JM |
2373 | |
2374 | p2p->min_disc_int = 1; | |
2375 | p2p->max_disc_int = 3; | |
96beff11 | 2376 | p2p->max_disc_tu = -1; |
b22128ef JM |
2377 | |
2378 | os_get_random(&p2p->next_tie_breaker, 1); | |
2379 | p2p->next_tie_breaker &= 0x01; | |
2380 | if (cfg->sd_request) | |
2381 | p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY; | |
2382 | p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE; | |
2383 | if (cfg->concurrent_operations) | |
2384 | p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER; | |
2385 | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; | |
2386 | ||
2387 | dl_list_init(&p2p->devices); | |
2388 | ||
2389 | eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0, | |
2390 | p2p_expiration_timeout, p2p, NULL); | |
2391 | ||
4f219667 JM |
2392 | p2p->go_timeout = 100; |
2393 | p2p->client_timeout = 20; | |
2394 | ||
51e9f228 JM |
2395 | p2p_dbg(p2p, "initialized"); |
2396 | p2p_channels_dump(p2p, "channels", &p2p->cfg->channels); | |
2397 | p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels); | |
2398 | ||
b22128ef JM |
2399 | return p2p; |
2400 | } | |
2401 | ||
2402 | ||
2403 | void p2p_deinit(struct p2p_data *p2p) | |
2404 | { | |
9675ce35 JM |
2405 | #ifdef CONFIG_WIFI_DISPLAY |
2406 | wpabuf_free(p2p->wfd_ie_beacon); | |
2407 | wpabuf_free(p2p->wfd_ie_probe_req); | |
2408 | wpabuf_free(p2p->wfd_ie_probe_resp); | |
2409 | wpabuf_free(p2p->wfd_ie_assoc_req); | |
2410 | wpabuf_free(p2p->wfd_ie_invitation); | |
2411 | wpabuf_free(p2p->wfd_ie_prov_disc_req); | |
2412 | wpabuf_free(p2p->wfd_ie_prov_disc_resp); | |
2413 | wpabuf_free(p2p->wfd_ie_go_neg); | |
2414 | wpabuf_free(p2p->wfd_dev_info); | |
2415 | wpabuf_free(p2p->wfd_assoc_bssid); | |
2416 | wpabuf_free(p2p->wfd_coupled_sink_info); | |
2417 | #endif /* CONFIG_WIFI_DISPLAY */ | |
2418 | ||
b22128ef JM |
2419 | eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL); |
2420 | eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); | |
2421 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
8cee87ab | 2422 | eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL); |
b22128ef | 2423 | p2p_flush(p2p); |
046ef4aa | 2424 | p2p_free_req_dev_types(p2p); |
b22128ef | 2425 | os_free(p2p->cfg->dev_name); |
b6e01800 JM |
2426 | os_free(p2p->cfg->manufacturer); |
2427 | os_free(p2p->cfg->model_name); | |
2428 | os_free(p2p->cfg->model_number); | |
2429 | os_free(p2p->cfg->serial_number); | |
21d996f7 | 2430 | os_free(p2p->cfg->pref_chan); |
b22128ef | 2431 | os_free(p2p->groups); |
18708aad | 2432 | wpabuf_free(p2p->sd_resp); |
3f9285ff | 2433 | os_free(p2p->after_scan_tx); |
f95cac27 | 2434 | p2p_remove_wps_vendor_extensions(p2p); |
556b30da | 2435 | os_free(p2p->no_go_freq.range); |
b22128ef JM |
2436 | os_free(p2p); |
2437 | } | |
2438 | ||
2439 | ||
2440 | void p2p_flush(struct p2p_data *p2p) | |
2441 | { | |
2442 | struct p2p_device *dev, *prev; | |
78db55b8 | 2443 | p2p_stop_find(p2p); |
b22128ef JM |
2444 | dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device, |
2445 | list) { | |
2446 | dl_list_del(&dev->list); | |
2447 | p2p_device_free(p2p, dev); | |
2448 | } | |
2449 | p2p_free_sd_queries(p2p); | |
f44ae207 JM |
2450 | os_free(p2p->after_scan_tx); |
2451 | p2p->after_scan_tx = NULL; | |
b22128ef JM |
2452 | } |
2453 | ||
2454 | ||
9d562b79 SS |
2455 | int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr) |
2456 | { | |
2457 | struct p2p_device *dev; | |
2458 | ||
2459 | dev = p2p_get_device(p2p, addr); | |
2460 | if (dev == NULL) | |
2461 | return -1; | |
2462 | ||
ed496f13 | 2463 | p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr)); |
9d562b79 SS |
2464 | |
2465 | if (p2p->go_neg_peer == dev) | |
2466 | p2p->go_neg_peer = NULL; | |
2467 | ||
2468 | dev->wps_method = WPS_NOT_READY; | |
ab9e3442 | 2469 | dev->oob_pw_id = 0; |
9d562b79 SS |
2470 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; |
2471 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; | |
2472 | ||
2473 | /* Check if after_scan_tx is for this peer. If so free it */ | |
2474 | if (p2p->after_scan_tx && | |
2475 | os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) { | |
2476 | os_free(p2p->after_scan_tx); | |
2477 | p2p->after_scan_tx = NULL; | |
2478 | } | |
2479 | ||
2480 | return 0; | |
2481 | } | |
2482 | ||
2483 | ||
b22128ef JM |
2484 | int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name) |
2485 | { | |
2486 | os_free(p2p->cfg->dev_name); | |
2487 | if (dev_name) { | |
2488 | p2p->cfg->dev_name = os_strdup(dev_name); | |
2489 | if (p2p->cfg->dev_name == NULL) | |
2490 | return -1; | |
2491 | } else | |
2492 | p2p->cfg->dev_name = NULL; | |
2493 | return 0; | |
2494 | } | |
2495 | ||
2496 | ||
b6e01800 JM |
2497 | int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer) |
2498 | { | |
2499 | os_free(p2p->cfg->manufacturer); | |
2500 | p2p->cfg->manufacturer = NULL; | |
2501 | if (manufacturer) { | |
2502 | p2p->cfg->manufacturer = os_strdup(manufacturer); | |
2503 | if (p2p->cfg->manufacturer == NULL) | |
2504 | return -1; | |
2505 | } | |
2506 | ||
2507 | return 0; | |
2508 | } | |
2509 | ||
2510 | ||
2511 | int p2p_set_model_name(struct p2p_data *p2p, const char *model_name) | |
2512 | { | |
2513 | os_free(p2p->cfg->model_name); | |
2514 | p2p->cfg->model_name = NULL; | |
2515 | if (model_name) { | |
2516 | p2p->cfg->model_name = os_strdup(model_name); | |
2517 | if (p2p->cfg->model_name == NULL) | |
2518 | return -1; | |
2519 | } | |
2520 | ||
2521 | return 0; | |
2522 | } | |
2523 | ||
2524 | ||
2525 | int p2p_set_model_number(struct p2p_data *p2p, const char *model_number) | |
2526 | { | |
2527 | os_free(p2p->cfg->model_number); | |
2528 | p2p->cfg->model_number = NULL; | |
2529 | if (model_number) { | |
2530 | p2p->cfg->model_number = os_strdup(model_number); | |
2531 | if (p2p->cfg->model_number == NULL) | |
2532 | return -1; | |
2533 | } | |
2534 | ||
2535 | return 0; | |
2536 | } | |
2537 | ||
2538 | ||
2539 | int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number) | |
2540 | { | |
2541 | os_free(p2p->cfg->serial_number); | |
2542 | p2p->cfg->serial_number = NULL; | |
2543 | if (serial_number) { | |
2544 | p2p->cfg->serial_number = os_strdup(serial_number); | |
2545 | if (p2p->cfg->serial_number == NULL) | |
2546 | return -1; | |
2547 | } | |
2548 | ||
2549 | return 0; | |
2550 | } | |
2551 | ||
2552 | ||
2553 | void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods) | |
2554 | { | |
2555 | p2p->cfg->config_methods = config_methods; | |
2556 | } | |
2557 | ||
2558 | ||
2559 | void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid) | |
2560 | { | |
2561 | os_memcpy(p2p->cfg->uuid, uuid, 16); | |
2562 | } | |
2563 | ||
2564 | ||
b22128ef JM |
2565 | int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type) |
2566 | { | |
2567 | os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8); | |
2568 | return 0; | |
2569 | } | |
2570 | ||
2571 | ||
2572 | int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8], | |
2573 | size_t num_dev_types) | |
2574 | { | |
2575 | if (num_dev_types > P2P_SEC_DEVICE_TYPES) | |
2576 | num_dev_types = P2P_SEC_DEVICE_TYPES; | |
2577 | p2p->cfg->num_sec_dev_types = num_dev_types; | |
2578 | os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8); | |
2579 | return 0; | |
2580 | } | |
2581 | ||
2582 | ||
f95cac27 JMB |
2583 | void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p) |
2584 | { | |
2585 | int i; | |
2586 | ||
10c5d2a5 | 2587 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
f95cac27 JMB |
2588 | wpabuf_free(p2p->wps_vendor_ext[i]); |
2589 | p2p->wps_vendor_ext[i] = NULL; | |
2590 | } | |
2591 | } | |
2592 | ||
2593 | ||
2594 | int p2p_add_wps_vendor_extension(struct p2p_data *p2p, | |
2595 | const struct wpabuf *vendor_ext) | |
2596 | { | |
2597 | int i; | |
2598 | ||
2599 | if (vendor_ext == NULL) | |
2600 | return -1; | |
2601 | ||
10c5d2a5 | 2602 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
f95cac27 JMB |
2603 | if (p2p->wps_vendor_ext[i] == NULL) |
2604 | break; | |
2605 | } | |
10c5d2a5 | 2606 | if (i >= P2P_MAX_WPS_VENDOR_EXT) |
f95cac27 JMB |
2607 | return -1; |
2608 | ||
2609 | p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext); | |
2610 | if (p2p->wps_vendor_ext[i] == NULL) | |
2611 | return -1; | |
2612 | ||
2613 | return 0; | |
2614 | } | |
2615 | ||
2616 | ||
b22128ef JM |
2617 | int p2p_set_country(struct p2p_data *p2p, const char *country) |
2618 | { | |
2619 | os_memcpy(p2p->cfg->country, country, 3); | |
2620 | return 0; | |
2621 | } | |
2622 | ||
2623 | ||
2624 | void p2p_continue_find(struct p2p_data *p2p) | |
2625 | { | |
2626 | struct p2p_device *dev; | |
2627 | p2p_set_state(p2p, P2P_SEARCH); | |
2628 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
2629 | if (dev->flags & P2P_DEV_SD_SCHEDULE) { | |
2630 | if (p2p_start_sd(p2p, dev) == 0) | |
2631 | return; | |
2632 | else | |
2633 | break; | |
10c4edde JM |
2634 | } else if (dev->req_config_methods && |
2635 | !(dev->flags & P2P_DEV_PD_FOR_JOIN)) { | |
ed496f13 | 2636 | p2p_dbg(p2p, "Send pending Provision Discovery Request to " |
10c4edde | 2637 | MACSTR " (config methods 0x%x)", |
c5db8e51 | 2638 | MAC2STR(dev->info.p2p_device_addr), |
10c4edde | 2639 | dev->req_config_methods); |
1ef2f7ff | 2640 | if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0) |
b22128ef JM |
2641 | return; |
2642 | } | |
2643 | } | |
2644 | ||
96beff11 | 2645 | p2p_listen_in_find(p2p, 1); |
b22128ef JM |
2646 | } |
2647 | ||
2648 | ||
2649 | static void p2p_sd_cb(struct p2p_data *p2p, int success) | |
2650 | { | |
ed496f13 | 2651 | p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d", |
b22128ef JM |
2652 | success); |
2653 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
2654 | ||
2655 | if (!success) { | |
2656 | if (p2p->sd_peer) { | |
2657 | p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE; | |
2658 | p2p->sd_peer = NULL; | |
2659 | } | |
2660 | p2p_continue_find(p2p); | |
2661 | return; | |
2662 | } | |
2663 | ||
2664 | if (p2p->sd_peer == NULL) { | |
ed496f13 | 2665 | p2p_dbg(p2p, "No SD peer entry known"); |
b22128ef JM |
2666 | p2p_continue_find(p2p); |
2667 | return; | |
2668 | } | |
2669 | ||
2670 | /* Wait for response from the peer */ | |
2671 | p2p_set_state(p2p, P2P_SD_DURING_FIND); | |
2672 | p2p_set_timeout(p2p, 0, 200000); | |
2673 | } | |
2674 | ||
6b56cc2d JS |
2675 | |
2676 | /** | |
2677 | * p2p_retry_pd - Retry any pending provision disc requests in IDLE state | |
2678 | * @p2p: P2P module context from p2p_init() | |
2679 | */ | |
19df9b07 | 2680 | static void p2p_retry_pd(struct p2p_data *p2p) |
6b56cc2d JS |
2681 | { |
2682 | struct p2p_device *dev; | |
2683 | ||
2684 | if (p2p->state != P2P_IDLE) | |
2685 | return; | |
2686 | ||
2687 | /* | |
2688 | * Retry the prov disc req attempt only for the peer that the user had | |
175171ac | 2689 | * requested. |
6b56cc2d JS |
2690 | */ |
2691 | ||
2692 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
2693 | if (os_memcmp(p2p->pending_pd_devaddr, | |
2694 | dev->info.p2p_device_addr, ETH_ALEN) != 0) | |
2695 | continue; | |
2696 | if (!dev->req_config_methods) | |
2697 | continue; | |
6b56cc2d | 2698 | |
ed496f13 | 2699 | p2p_dbg(p2p, "Send pending Provision Discovery Request to " |
6b56cc2d JS |
2700 | MACSTR " (config methods 0x%x)", |
2701 | MAC2STR(dev->info.p2p_device_addr), | |
2702 | dev->req_config_methods); | |
175171ac | 2703 | p2p_send_prov_disc_req(p2p, dev, |
8d82c210 JM |
2704 | dev->flags & P2P_DEV_PD_FOR_JOIN, |
2705 | p2p->pd_force_freq); | |
6b56cc2d JS |
2706 | return; |
2707 | } | |
2708 | } | |
2709 | ||
2710 | ||
b22128ef JM |
2711 | static void p2p_prov_disc_cb(struct p2p_data *p2p, int success) |
2712 | { | |
ed496f13 | 2713 | p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d", |
b22128ef | 2714 | success); |
6b56cc2d JS |
2715 | |
2716 | /* | |
2717 | * Postpone resetting the pending action state till after we actually | |
2718 | * time out. This allows us to take some action like notifying any | |
2719 | * interested parties about no response to the request. | |
2720 | * | |
2721 | * When the timer (below) goes off we check in IDLE, SEARCH, or | |
2722 | * LISTEN_ONLY state, which are the only allowed states to issue a PD | |
2723 | * requests in, if this was still pending and then raise notification. | |
2724 | */ | |
b22128ef JM |
2725 | |
2726 | if (!success) { | |
6b56cc2d JS |
2727 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
2728 | ||
488f4a71 JM |
2729 | if (p2p->user_initiated_pd && |
2730 | (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY)) | |
2731 | { | |
2732 | /* Retry request from timeout to avoid busy loops */ | |
2733 | p2p->pending_action_state = P2P_PENDING_PD; | |
2734 | p2p_set_timeout(p2p, 0, 50000); | |
2735 | } else if (p2p->state != P2P_IDLE) | |
b22128ef | 2736 | p2p_continue_find(p2p); |
6b56cc2d JS |
2737 | else if (p2p->user_initiated_pd) { |
2738 | p2p->pending_action_state = P2P_PENDING_PD; | |
2739 | p2p_set_timeout(p2p, 0, 300000); | |
2740 | } | |
b22128ef JM |
2741 | return; |
2742 | } | |
2743 | ||
6b56cc2d JS |
2744 | /* |
2745 | * This postponing, of resetting pending_action_state, needs to be | |
2746 | * done only for user initiated PD requests and not internal ones. | |
2747 | */ | |
2748 | if (p2p->user_initiated_pd) | |
2749 | p2p->pending_action_state = P2P_PENDING_PD; | |
2750 | else | |
2751 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
2752 | ||
b22128ef JM |
2753 | /* Wait for response from the peer */ |
2754 | if (p2p->state == P2P_SEARCH) | |
2755 | p2p_set_state(p2p, P2P_PD_DURING_FIND); | |
2756 | p2p_set_timeout(p2p, 0, 200000); | |
2757 | } | |
2758 | ||
2759 | ||
2760 | int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq, | |
acb69cec | 2761 | struct os_reltime *rx_time, int level, const u8 *ies, |
3dfd0484 | 2762 | size_t ies_len) |
b22128ef | 2763 | { |
acb69cec | 2764 | if (os_reltime_before(rx_time, &p2p->find_start)) { |
a5b5e830 JM |
2765 | /* |
2766 | * The driver may have cached (e.g., in cfg80211 BSS table) the | |
2767 | * scan results for relatively long time. To avoid reporting | |
2768 | * stale information, update P2P peers only based on results | |
2769 | * that have based on frames received after the last p2p_find | |
2770 | * operation was started. | |
2771 | */ | |
ed496f13 JM |
2772 | p2p_dbg(p2p, "Ignore old scan result for " MACSTR |
2773 | " (rx_time=%u.%06u)", | |
a5b5e830 JM |
2774 | MAC2STR(bssid), (unsigned int) rx_time->sec, |
2775 | (unsigned int) rx_time->usec); | |
2776 | return 0; | |
2777 | } | |
2778 | ||
c5f10e80 | 2779 | p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1); |
b22128ef | 2780 | |
b22128ef JM |
2781 | return 0; |
2782 | } | |
2783 | ||
2784 | ||
2785 | void p2p_scan_res_handled(struct p2p_data *p2p) | |
2786 | { | |
2787 | if (!p2p->p2p_scan_running) { | |
ed496f13 | 2788 | p2p_dbg(p2p, "p2p_scan was not running, but scan results received"); |
b22128ef JM |
2789 | } |
2790 | p2p->p2p_scan_running = 0; | |
2791 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); | |
2792 | ||
2793 | if (p2p_run_after_scan(p2p)) | |
2794 | return; | |
2795 | if (p2p->state == P2P_SEARCH) | |
2796 | p2p_continue_find(p2p); | |
2797 | } | |
2798 | ||
2799 | ||
6d92fa6e | 2800 | void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id) |
b22128ef | 2801 | { |
9675ce35 JM |
2802 | u8 *len; |
2803 | ||
2804 | #ifdef CONFIG_WIFI_DISPLAY | |
2805 | if (p2p->wfd_ie_probe_req) | |
2806 | wpabuf_put_buf(ies, p2p->wfd_ie_probe_req); | |
2807 | #endif /* CONFIG_WIFI_DISPLAY */ | |
2808 | ||
2809 | len = p2p_buf_add_ie_hdr(ies); | |
18485b54 MH |
2810 | p2p_buf_add_capability(ies, p2p->dev_capab & |
2811 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); | |
6d92fa6e JM |
2812 | if (dev_id) |
2813 | p2p_buf_add_device_id(ies, dev_id); | |
b22128ef JM |
2814 | if (p2p->cfg->reg_class && p2p->cfg->channel) |
2815 | p2p_buf_add_listen_channel(ies, p2p->cfg->country, | |
2816 | p2p->cfg->reg_class, | |
2817 | p2p->cfg->channel); | |
2818 | if (p2p->ext_listen_interval) | |
2819 | p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period, | |
2820 | p2p->ext_listen_interval); | |
2821 | /* TODO: p2p_buf_add_operating_channel() if GO */ | |
2822 | p2p_buf_update_ie_hdr(ies, len); | |
2823 | } | |
2824 | ||
2825 | ||
206e1f42 JM |
2826 | size_t p2p_scan_ie_buf_len(struct p2p_data *p2p) |
2827 | { | |
9675ce35 JM |
2828 | size_t len = 100; |
2829 | ||
2830 | #ifdef CONFIG_WIFI_DISPLAY | |
2831 | if (p2p && p2p->wfd_ie_probe_req) | |
2832 | len += wpabuf_len(p2p->wfd_ie_probe_req); | |
2833 | #endif /* CONFIG_WIFI_DISPLAY */ | |
2834 | ||
2835 | return len; | |
206e1f42 JM |
2836 | } |
2837 | ||
2838 | ||
b22128ef JM |
2839 | int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end) |
2840 | { | |
2841 | return p2p_attr_text(p2p_ie, buf, end); | |
2842 | } | |
2843 | ||
2844 | ||
2845 | static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success) | |
2846 | { | |
2847 | struct p2p_device *dev = p2p->go_neg_peer; | |
fb8984fd | 2848 | int timeout; |
b22128ef | 2849 | |
ed496f13 | 2850 | p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success); |
b22128ef JM |
2851 | |
2852 | if (dev == NULL) { | |
ed496f13 | 2853 | p2p_dbg(p2p, "No pending GO Negotiation"); |
b22128ef JM |
2854 | return; |
2855 | } | |
2856 | ||
2857 | if (success) { | |
b22128ef JM |
2858 | if (dev->flags & P2P_DEV_USER_REJECTED) { |
2859 | p2p_set_state(p2p, P2P_IDLE); | |
2860 | return; | |
2861 | } | |
a1d2ab32 NKG |
2862 | } else if (dev->go_neg_req_sent) { |
2863 | /* Cancel the increment from p2p_connect_send() on failure */ | |
2864 | dev->go_neg_req_sent--; | |
b22128ef JM |
2865 | } |
2866 | ||
2867 | if (!success && | |
c5db8e51 | 2868 | (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) && |
b22128ef | 2869 | !is_zero_ether_addr(dev->member_in_go_dev)) { |
ed496f13 | 2870 | p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO", |
c5db8e51 | 2871 | MAC2STR(dev->info.p2p_device_addr)); |
b22128ef JM |
2872 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
2873 | p2p_send_dev_disc_req(p2p, dev); | |
2874 | return; | |
2875 | } | |
2876 | ||
2877 | /* | |
2878 | * Use P2P find, if needed, to find the other device from its listen | |
2879 | * channel. | |
2880 | */ | |
2881 | p2p_set_state(p2p, P2P_CONNECT); | |
fb8984fd JM |
2882 | timeout = success ? 500000 : 100000; |
2883 | if (!success && p2p->go_neg_peer && | |
2884 | (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) { | |
2885 | unsigned int r; | |
2886 | /* | |
2887 | * Peer is expected to wait our response and we will skip the | |
2888 | * listen phase. Add some randomness to the wait time here to | |
2889 | * make it less likely to hit cases where we could end up in | |
2890 | * sync with peer not listening. | |
2891 | */ | |
2892 | os_get_random((u8 *) &r, sizeof(r)); | |
2893 | timeout += r % 100000; | |
2894 | } | |
2895 | p2p_set_timeout(p2p, 0, timeout); | |
b22128ef JM |
2896 | } |
2897 | ||
2898 | ||
2899 | static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success) | |
2900 | { | |
ed496f13 | 2901 | p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d", |
b22128ef JM |
2902 | success); |
2903 | if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) { | |
ed496f13 | 2904 | p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore"); |
b22128ef JM |
2905 | return; |
2906 | } | |
2907 | p2p_set_state(p2p, P2P_CONNECT); | |
8e4839ce | 2908 | p2p_set_timeout(p2p, 0, 500000); |
b22128ef JM |
2909 | } |
2910 | ||
2911 | ||
7800d45c JM |
2912 | static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success, |
2913 | const u8 *addr) | |
b22128ef | 2914 | { |
ed496f13 | 2915 | p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success); |
fbe70272 JM |
2916 | if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) { |
2917 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, | |
2918 | p2p->go_neg_peer->status); | |
7800d45c JM |
2919 | } else if (success) { |
2920 | struct p2p_device *dev; | |
2921 | dev = p2p_get_device(p2p, addr); | |
2922 | if (dev && | |
2923 | dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) | |
2924 | dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE; | |
fbe70272 | 2925 | } |
b22128ef JM |
2926 | } |
2927 | ||
2928 | ||
93b7ddd0 JM |
2929 | static void p2p_go_neg_conf_cb(struct p2p_data *p2p, |
2930 | enum p2p_send_action_result result) | |
b22128ef JM |
2931 | { |
2932 | struct p2p_device *dev; | |
2933 | ||
ed496f13 | 2934 | p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result); |
b22128ef | 2935 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
93b7ddd0 JM |
2936 | if (result == P2P_SEND_ACTION_FAILED) { |
2937 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); | |
2938 | return; | |
2939 | } | |
2940 | if (result == P2P_SEND_ACTION_NO_ACK) { | |
b22128ef JM |
2941 | /* |
2942 | * It looks like the TX status for GO Negotiation Confirm is | |
2943 | * often showing failure even when the peer has actually | |
2944 | * received the frame. Since the peer may change channels | |
2945 | * immediately after having received the frame, we may not see | |
2946 | * an Ack for retries, so just dropping a single frame may | |
2947 | * trigger this. To allow the group formation to succeed if the | |
2948 | * peer did indeed receive the frame, continue regardless of | |
2949 | * the TX status. | |
2950 | */ | |
ed496f13 | 2951 | p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported"); |
b22128ef JM |
2952 | } |
2953 | ||
2954 | dev = p2p->go_neg_peer; | |
2955 | if (dev == NULL) | |
2956 | return; | |
2957 | ||
2958 | p2p_go_complete(p2p, dev); | |
2959 | } | |
2960 | ||
2961 | ||
2962 | void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst, | |
93b7ddd0 JM |
2963 | const u8 *src, const u8 *bssid, |
2964 | enum p2p_send_action_result result) | |
b22128ef JM |
2965 | { |
2966 | enum p2p_pending_action_state state; | |
93b7ddd0 | 2967 | int success; |
b22128ef | 2968 | |
ed496f13 | 2969 | p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR |
93b7ddd0 | 2970 | " src=" MACSTR " bssid=" MACSTR " result=%d", |
b22128ef | 2971 | p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src), |
93b7ddd0 JM |
2972 | MAC2STR(bssid), result); |
2973 | success = result == P2P_SEND_ACTION_SUCCESS; | |
b22128ef JM |
2974 | state = p2p->pending_action_state; |
2975 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
2976 | switch (state) { | |
2977 | case P2P_NO_PENDING_ACTION: | |
63a965c3 JM |
2978 | if (p2p->after_scan_tx_in_progress) { |
2979 | p2p->after_scan_tx_in_progress = 0; | |
2980 | if (p2p->start_after_scan != P2P_AFTER_SCAN_NOTHING && | |
2981 | p2p_run_after_scan(p2p)) | |
2982 | break; | |
2983 | if (p2p->state == P2P_SEARCH) { | |
ed496f13 | 2984 | p2p_dbg(p2p, "Continue find after after_scan_tx completion"); |
63a965c3 JM |
2985 | p2p_continue_find(p2p); |
2986 | } | |
2987 | } | |
b22128ef JM |
2988 | break; |
2989 | case P2P_PENDING_GO_NEG_REQUEST: | |
2990 | p2p_go_neg_req_cb(p2p, success); | |
2991 | break; | |
2992 | case P2P_PENDING_GO_NEG_RESPONSE: | |
2993 | p2p_go_neg_resp_cb(p2p, success); | |
2994 | break; | |
2995 | case P2P_PENDING_GO_NEG_RESPONSE_FAILURE: | |
7800d45c | 2996 | p2p_go_neg_resp_failure_cb(p2p, success, dst); |
b22128ef JM |
2997 | break; |
2998 | case P2P_PENDING_GO_NEG_CONFIRM: | |
93b7ddd0 | 2999 | p2p_go_neg_conf_cb(p2p, result); |
b22128ef JM |
3000 | break; |
3001 | case P2P_PENDING_SD: | |
3002 | p2p_sd_cb(p2p, success); | |
3003 | break; | |
3004 | case P2P_PENDING_PD: | |
3005 | p2p_prov_disc_cb(p2p, success); | |
3006 | break; | |
3007 | case P2P_PENDING_INVITATION_REQUEST: | |
3008 | p2p_invitation_req_cb(p2p, success); | |
3009 | break; | |
3010 | case P2P_PENDING_INVITATION_RESPONSE: | |
3011 | p2p_invitation_resp_cb(p2p, success); | |
3012 | break; | |
3013 | case P2P_PENDING_DEV_DISC_REQUEST: | |
3014 | p2p_dev_disc_req_cb(p2p, success); | |
3015 | break; | |
3016 | case P2P_PENDING_DEV_DISC_RESPONSE: | |
3017 | p2p_dev_disc_resp_cb(p2p, success); | |
3018 | break; | |
3019 | case P2P_PENDING_GO_DISC_REQ: | |
3020 | p2p_go_disc_req_cb(p2p, success); | |
3021 | break; | |
3022 | } | |
63a965c3 JM |
3023 | |
3024 | p2p->after_scan_tx_in_progress = 0; | |
b22128ef JM |
3025 | } |
3026 | ||
3027 | ||
3028 | void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq, | |
3029 | unsigned int duration) | |
3030 | { | |
3031 | if (freq == p2p->pending_client_disc_freq) { | |
ed496f13 | 3032 | p2p_dbg(p2p, "Client discoverability remain-awake completed"); |
b22128ef JM |
3033 | p2p->pending_client_disc_freq = 0; |
3034 | return; | |
3035 | } | |
3036 | ||
3037 | if (freq != p2p->pending_listen_freq) { | |
ed496f13 | 3038 | p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)", |
b22128ef JM |
3039 | freq, duration, p2p->pending_listen_freq); |
3040 | return; | |
3041 | } | |
3042 | ||
ed496f13 | 3043 | p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback", |
b22128ef JM |
3044 | p2p->pending_listen_sec, p2p->pending_listen_usec, |
3045 | p2p->pending_listen_freq); | |
3046 | p2p->in_listen = 1; | |
0b8889d8 | 3047 | p2p->drv_in_listen = freq; |
b22128ef JM |
3048 | if (p2p->pending_listen_sec || p2p->pending_listen_usec) { |
3049 | /* | |
3050 | * Add 20 msec extra wait to avoid race condition with driver | |
3051 | * remain-on-channel end event, i.e., give driver more time to | |
3052 | * complete the operation before our timeout expires. | |
3053 | */ | |
3054 | p2p_set_timeout(p2p, p2p->pending_listen_sec, | |
3055 | p2p->pending_listen_usec + 20000); | |
3056 | } | |
3057 | ||
3058 | p2p->pending_listen_freq = 0; | |
3059 | } | |
3060 | ||
3061 | ||
3062 | int p2p_listen_end(struct p2p_data *p2p, unsigned int freq) | |
3063 | { | |
ed496f13 | 3064 | p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq); |
b22128ef JM |
3065 | p2p->drv_in_listen = 0; |
3066 | if (p2p->in_listen) | |
3067 | return 0; /* Internal timeout will trigger the next step */ | |
3068 | ||
3069 | if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) { | |
e24cf97c | 3070 | if (p2p->go_neg_peer->connect_reqs >= 120) { |
ed496f13 | 3071 | p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response"); |
e24cf97c JM |
3072 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); |
3073 | return 0; | |
3074 | } | |
3075 | ||
b22128ef JM |
3076 | p2p_set_state(p2p, P2P_CONNECT); |
3077 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
3078 | return 1; | |
3079 | } else if (p2p->state == P2P_SEARCH) { | |
59acfe87 JM |
3080 | if (p2p->p2p_scan_running) { |
3081 | /* | |
3082 | * Search is already in progress. This can happen if | |
3083 | * an Action frame RX is reported immediately after | |
3084 | * the end of a remain-on-channel operation and the | |
3085 | * response frame to that is sent using an offchannel | |
3086 | * operation while in p2p_find. Avoid an attempt to | |
3087 | * restart a scan here. | |
3088 | */ | |
ed496f13 | 3089 | p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one"); |
59acfe87 JM |
3090 | return 1; |
3091 | } | |
3fe8b68d JM |
3092 | if (p2p->pending_listen_freq) { |
3093 | /* | |
3094 | * Better wait a bit if the driver is unable to start | |
3095 | * offchannel operation for some reason. p2p_search() | |
3096 | * will be started from internal timeout. | |
3097 | */ | |
ed496f13 | 3098 | p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop"); |
3fe8b68d JM |
3099 | p2p_set_timeout(p2p, 0, 100000); |
3100 | return 1; | |
3101 | } | |
37448ede | 3102 | if (p2p->search_delay) { |
ed496f13 | 3103 | p2p_dbg(p2p, "Delay search operation by %u ms", |
37448ede JM |
3104 | p2p->search_delay); |
3105 | p2p_set_timeout(p2p, p2p->search_delay / 1000, | |
3106 | (p2p->search_delay % 1000) * 1000); | |
3107 | return 1; | |
3108 | } | |
b22128ef JM |
3109 | p2p_search(p2p); |
3110 | return 1; | |
3111 | } | |
3112 | ||
3113 | return 0; | |
3114 | } | |
3115 | ||
3116 | ||
3117 | static void p2p_timeout_connect(struct p2p_data *p2p) | |
3118 | { | |
3119 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
579a8098 JM |
3120 | if (p2p->go_neg_peer && |
3121 | (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) { | |
ed496f13 | 3122 | p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed"); |
579a8098 JM |
3123 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); |
3124 | return; | |
3125 | } | |
fb8984fd JM |
3126 | if (p2p->go_neg_peer && |
3127 | (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) && | |
3128 | p2p->go_neg_peer->connect_reqs < 120) { | |
ed496f13 | 3129 | p2p_dbg(p2p, "Peer expected to wait our response - skip listen"); |
fb8984fd JM |
3130 | p2p_connect_send(p2p, p2p->go_neg_peer); |
3131 | return; | |
3132 | } | |
fdd48ff6 JM |
3133 | if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) { |
3134 | p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)"); | |
3135 | p2p_set_state(p2p, P2P_CONNECT_LISTEN); | |
3136 | p2p_set_timeout(p2p, 0, 30000); | |
3137 | return; | |
3138 | } | |
b22128ef | 3139 | p2p_set_state(p2p, P2P_CONNECT_LISTEN); |
96beff11 | 3140 | p2p_listen_in_find(p2p, 0); |
b22128ef JM |
3141 | } |
3142 | ||
3143 | ||
3144 | static void p2p_timeout_connect_listen(struct p2p_data *p2p) | |
3145 | { | |
3146 | if (p2p->go_neg_peer) { | |
3147 | if (p2p->drv_in_listen) { | |
ed496f13 | 3148 | p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete"); |
b22128ef JM |
3149 | return; |
3150 | } | |
9dac8c3e FM |
3151 | |
3152 | if (p2p->go_neg_peer->connect_reqs >= 120) { | |
ed496f13 | 3153 | p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response"); |
9dac8c3e FM |
3154 | p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); |
3155 | return; | |
3156 | } | |
3157 | ||
b22128ef JM |
3158 | p2p_set_state(p2p, P2P_CONNECT); |
3159 | p2p_connect_send(p2p, p2p->go_neg_peer); | |
3160 | } else | |
3161 | p2p_set_state(p2p, P2P_IDLE); | |
3162 | } | |
3163 | ||
3164 | ||
3165 | static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p) | |
3166 | { | |
b22128ef | 3167 | p2p_set_state(p2p, P2P_WAIT_PEER_IDLE); |
a2d63657 RR |
3168 | |
3169 | if (p2p->cfg->is_concurrent_session_active && | |
3170 | p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx)) | |
3171 | p2p_set_timeout(p2p, 0, 500000); | |
3172 | else | |
3173 | p2p_set_timeout(p2p, 0, 200000); | |
b22128ef JM |
3174 | } |
3175 | ||
3176 | ||
3177 | static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p) | |
3178 | { | |
3179 | struct p2p_device *dev = p2p->go_neg_peer; | |
3180 | ||
3181 | if (dev == NULL) { | |
ed496f13 | 3182 | p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait"); |
b22128ef JM |
3183 | return; |
3184 | } | |
3185 | ||
3186 | dev->wait_count++; | |
3187 | if (dev->wait_count >= 120) { | |
ed496f13 | 3188 | p2p_dbg(p2p, "Timeout on waiting peer to become ready for GO Negotiation"); |
b22128ef JM |
3189 | p2p_go_neg_failed(p2p, dev, -1); |
3190 | return; | |
3191 | } | |
3192 | ||
ed496f13 | 3193 | p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation"); |
b22128ef | 3194 | p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT); |
96beff11 | 3195 | p2p_listen_in_find(p2p, 0); |
b22128ef JM |
3196 | } |
3197 | ||
3198 | ||
3199 | static void p2p_timeout_sd_during_find(struct p2p_data *p2p) | |
3200 | { | |
ed496f13 | 3201 | p2p_dbg(p2p, "Service Discovery Query timeout"); |
b22128ef JM |
3202 | if (p2p->sd_peer) { |
3203 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
3204 | p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE; | |
3205 | p2p->sd_peer = NULL; | |
3206 | } | |
3207 | p2p_continue_find(p2p); | |
3208 | } | |
3209 | ||
3210 | ||
3211 | static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p) | |
3212 | { | |
ed496f13 | 3213 | p2p_dbg(p2p, "Provision Discovery Request timeout"); |
b22128ef JM |
3214 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
3215 | p2p_continue_find(p2p); | |
3216 | } | |
3217 | ||
3218 | ||
6b56cc2d JS |
3219 | static void p2p_timeout_prov_disc_req(struct p2p_data *p2p) |
3220 | { | |
3221 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3222 | ||
3223 | /* | |
3224 | * For user initiated PD requests that we have not gotten any responses | |
3225 | * for while in IDLE state, we retry them a couple of times before | |
3226 | * giving up. | |
3227 | */ | |
3228 | if (!p2p->user_initiated_pd) | |
3229 | return; | |
3230 | ||
ed496f13 | 3231 | p2p_dbg(p2p, "User initiated Provision Discovery Request timeout"); |
6b56cc2d JS |
3232 | |
3233 | if (p2p->pd_retries) { | |
3234 | p2p->pd_retries--; | |
3235 | p2p_retry_pd(p2p); | |
3236 | } else { | |
175171ac JM |
3237 | struct p2p_device *dev; |
3238 | int for_join = 0; | |
3239 | ||
3240 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { | |
3241 | if (os_memcmp(p2p->pending_pd_devaddr, | |
3242 | dev->info.p2p_device_addr, ETH_ALEN) != 0) | |
3243 | continue; | |
3244 | if (dev->req_config_methods && | |
3245 | (dev->flags & P2P_DEV_PD_FOR_JOIN)) | |
3246 | for_join = 1; | |
3247 | } | |
3248 | ||
349b213c JS |
3249 | if (p2p->cfg->prov_disc_fail) |
3250 | p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, | |
3251 | p2p->pending_pd_devaddr, | |
175171ac JM |
3252 | for_join ? |
3253 | P2P_PROV_DISC_TIMEOUT_JOIN : | |
349b213c | 3254 | P2P_PROV_DISC_TIMEOUT); |
6b56cc2d JS |
3255 | p2p_reset_pending_pd(p2p); |
3256 | } | |
3257 | } | |
3258 | ||
3259 | ||
b22128ef JM |
3260 | static void p2p_timeout_invite(struct p2p_data *p2p) |
3261 | { | |
3262 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); | |
3263 | p2p_set_state(p2p, P2P_INVITE_LISTEN); | |
04d8dad5 JM |
3264 | if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) { |
3265 | /* | |
3266 | * Better remain on operating channel instead of listen channel | |
3267 | * when running a group. | |
3268 | */ | |
ed496f13 | 3269 | p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel"); |
04d8dad5 JM |
3270 | p2p_set_timeout(p2p, 0, 100000); |
3271 | return; | |
3272 | } | |
96beff11 | 3273 | p2p_listen_in_find(p2p, 0); |
b22128ef JM |
3274 | } |
3275 | ||
3276 | ||
3277 | static void p2p_timeout_invite_listen(struct p2p_data *p2p) | |
3278 | { | |
3279 | if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) { | |
3280 | p2p_set_state(p2p, P2P_INVITE); | |
3281 | p2p_invite_send(p2p, p2p->invite_peer, | |
8e9f53c3 | 3282 | p2p->invite_go_dev_addr, p2p->invite_dev_pw_id); |
b22128ef JM |
3283 | } else { |
3284 | if (p2p->invite_peer) { | |
ed496f13 | 3285 | p2p_dbg(p2p, "Invitation Request retry limit reached"); |
b22128ef JM |
3286 | if (p2p->cfg->invitation_result) |
3287 | p2p->cfg->invitation_result( | |
dbca75f8 | 3288 | p2p->cfg->cb_ctx, -1, NULL, NULL, |
dcd25d4c VKE |
3289 | p2p->invite_peer->info.p2p_device_addr, |
3290 | 0); | |
b22128ef JM |
3291 | } |
3292 | p2p_set_state(p2p, P2P_IDLE); | |
3293 | } | |
3294 | } | |
3295 | ||
3296 | ||
3297 | static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx) | |
3298 | { | |
3299 | struct p2p_data *p2p = eloop_ctx; | |
3300 | ||
ed496f13 | 3301 | p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state)); |
b22128ef JM |
3302 | |
3303 | p2p->in_listen = 0; | |
3304 | ||
3305 | switch (p2p->state) { | |
3306 | case P2P_IDLE: | |
6b56cc2d JS |
3307 | /* Check if we timed out waiting for PD req */ |
3308 | if (p2p->pending_action_state == P2P_PENDING_PD) | |
3309 | p2p_timeout_prov_disc_req(p2p); | |
b22128ef JM |
3310 | break; |
3311 | case P2P_SEARCH: | |
6b56cc2d JS |
3312 | /* Check if we timed out waiting for PD req */ |
3313 | if (p2p->pending_action_state == P2P_PENDING_PD) | |
3314 | p2p_timeout_prov_disc_req(p2p); | |
37448ede | 3315 | if (p2p->search_delay && !p2p->in_search_delay) { |
ed496f13 | 3316 | p2p_dbg(p2p, "Delay search operation by %u ms", |
37448ede JM |
3317 | p2p->search_delay); |
3318 | p2p->in_search_delay = 1; | |
3319 | p2p_set_timeout(p2p, p2p->search_delay / 1000, | |
3320 | (p2p->search_delay % 1000) * 1000); | |
3321 | break; | |
3322 | } | |
3323 | p2p->in_search_delay = 0; | |
b22128ef JM |
3324 | p2p_search(p2p); |
3325 | break; | |
3326 | case P2P_CONNECT: | |
3327 | p2p_timeout_connect(p2p); | |
3328 | break; | |
3329 | case P2P_CONNECT_LISTEN: | |
3330 | p2p_timeout_connect_listen(p2p); | |
3331 | break; | |
3332 | case P2P_GO_NEG: | |
3333 | break; | |
3334 | case P2P_LISTEN_ONLY: | |
6b56cc2d JS |
3335 | /* Check if we timed out waiting for PD req */ |
3336 | if (p2p->pending_action_state == P2P_PENDING_PD) | |
3337 | p2p_timeout_prov_disc_req(p2p); | |
3338 | ||
b22128ef | 3339 | if (p2p->ext_listen_only) { |
ed496f13 | 3340 | p2p_dbg(p2p, "Extended Listen Timing - Listen State completed"); |
b22128ef JM |
3341 | p2p->ext_listen_only = 0; |
3342 | p2p_set_state(p2p, P2P_IDLE); | |
3343 | } | |
3344 | break; | |
3345 | case P2P_WAIT_PEER_CONNECT: | |
3346 | p2p_timeout_wait_peer_connect(p2p); | |
3347 | break; | |
3348 | case P2P_WAIT_PEER_IDLE: | |
3349 | p2p_timeout_wait_peer_idle(p2p); | |
3350 | break; | |
3351 | case P2P_SD_DURING_FIND: | |
3352 | p2p_timeout_sd_during_find(p2p); | |
3353 | break; | |
3354 | case P2P_PROVISIONING: | |
3355 | break; | |
3356 | case P2P_PD_DURING_FIND: | |
3357 | p2p_timeout_prov_disc_during_find(p2p); | |
3358 | break; | |
3359 | case P2P_INVITE: | |
3360 | p2p_timeout_invite(p2p); | |
3361 | break; | |
3362 | case P2P_INVITE_LISTEN: | |
3363 | p2p_timeout_invite_listen(p2p); | |
3364 | break; | |
3365 | } | |
3366 | } | |
3367 | ||
3368 | ||
3369 | int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr) | |
3370 | { | |
3371 | struct p2p_device *dev; | |
3372 | ||
3373 | dev = p2p_get_device(p2p, peer_addr); | |
ed496f13 JM |
3374 | p2p_dbg(p2p, "Local request to reject connection attempts by peer " |
3375 | MACSTR, MAC2STR(peer_addr)); | |
b22128ef | 3376 | if (dev == NULL) { |
ed496f13 | 3377 | p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr)); |
b22128ef JM |
3378 | return -1; |
3379 | } | |
3380 | dev->status = P2P_SC_FAIL_REJECTED_BY_USER; | |
3381 | dev->flags |= P2P_DEV_USER_REJECTED; | |
3382 | return 0; | |
3383 | } | |
3384 | ||
3385 | ||
e5a359cf | 3386 | const char * p2p_wps_method_text(enum p2p_wps_method method) |
b22128ef JM |
3387 | { |
3388 | switch (method) { | |
3389 | case WPS_NOT_READY: | |
3390 | return "not-ready"; | |
b22128ef JM |
3391 | case WPS_PIN_DISPLAY: |
3392 | return "Display"; | |
3393 | case WPS_PIN_KEYPAD: | |
3394 | return "Keypad"; | |
3395 | case WPS_PBC: | |
3396 | return "PBC"; | |
c00ab85f JM |
3397 | case WPS_NFC: |
3398 | return "NFC"; | |
b22128ef JM |
3399 | } |
3400 | ||
3401 | return "??"; | |
3402 | } | |
3403 | ||
3404 | ||
3405 | static const char * p2p_go_state_text(enum p2p_go_state go_state) | |
3406 | { | |
3407 | switch (go_state) { | |
3408 | case UNKNOWN_GO: | |
3409 | return "unknown"; | |
3410 | case LOCAL_GO: | |
3411 | return "local"; | |
3412 | case REMOTE_GO: | |
3413 | return "remote"; | |
3414 | } | |
3415 | ||
3416 | return "??"; | |
3417 | } | |
3418 | ||
3419 | ||
b3ffc80b JM |
3420 | const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p, |
3421 | const u8 *addr, int next) | |
b22128ef JM |
3422 | { |
3423 | struct p2p_device *dev; | |
b22128ef JM |
3424 | |
3425 | if (addr) | |
3426 | dev = p2p_get_device(p2p, addr); | |
3427 | else | |
3428 | dev = dl_list_first(&p2p->devices, struct p2p_device, list); | |
3429 | ||
3430 | if (dev && next) { | |
3431 | dev = dl_list_first(&dev->list, struct p2p_device, list); | |
3432 | if (&dev->list == &p2p->devices) | |
3433 | dev = NULL; | |
3434 | } | |
3435 | ||
3436 | if (dev == NULL) | |
b3ffc80b JM |
3437 | return NULL; |
3438 | ||
3439 | return &dev->info; | |
3440 | } | |
3441 | ||
3442 | ||
3443 | int p2p_get_peer_info_txt(const struct p2p_peer_info *info, | |
3444 | char *buf, size_t buflen) | |
3445 | { | |
3446 | struct p2p_device *dev; | |
3447 | int res; | |
3448 | char *pos, *end; | |
acb69cec | 3449 | struct os_reltime now; |
b3ffc80b JM |
3450 | |
3451 | if (info == NULL) | |
b22128ef JM |
3452 | return -1; |
3453 | ||
b3ffc80b JM |
3454 | dev = (struct p2p_device *) (((u8 *) info) - |
3455 | offsetof(struct p2p_device, info)); | |
3456 | ||
b22128ef JM |
3457 | pos = buf; |
3458 | end = buf + buflen; | |
3459 | ||
acb69cec | 3460 | os_get_reltime(&now); |
b22128ef JM |
3461 | res = os_snprintf(pos, end - pos, |
3462 | "age=%d\n" | |
3463 | "listen_freq=%d\n" | |
b22128ef JM |
3464 | "wps_method=%s\n" |
3465 | "interface_addr=" MACSTR "\n" | |
3466 | "member_in_go_dev=" MACSTR "\n" | |
3467 | "member_in_go_iface=" MACSTR "\n" | |
b22128ef JM |
3468 | "go_neg_req_sent=%d\n" |
3469 | "go_state=%s\n" | |
3470 | "dialog_token=%u\n" | |
3471 | "intended_addr=" MACSTR "\n" | |
3472 | "country=%c%c\n" | |
3473 | "oper_freq=%d\n" | |
3474 | "req_config_methods=0x%x\n" | |
10c4edde | 3475 | "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n" |
b22128ef JM |
3476 | "status=%d\n" |
3477 | "wait_count=%u\n" | |
3478 | "invitation_reqs=%u\n", | |
3479 | (int) (now.sec - dev->last_seen.sec), | |
3480 | dev->listen_freq, | |
b22128ef JM |
3481 | p2p_wps_method_text(dev->wps_method), |
3482 | MAC2STR(dev->interface_addr), | |
3483 | MAC2STR(dev->member_in_go_dev), | |
3484 | MAC2STR(dev->member_in_go_iface), | |
b22128ef JM |
3485 | dev->go_neg_req_sent, |
3486 | p2p_go_state_text(dev->go_state), | |
3487 | dev->dialog_token, | |
3488 | MAC2STR(dev->intended_addr), | |
3489 | dev->country[0] ? dev->country[0] : '_', | |
3490 | dev->country[1] ? dev->country[1] : '_', | |
3491 | dev->oper_freq, | |
3492 | dev->req_config_methods, | |
3493 | dev->flags & P2P_DEV_PROBE_REQ_ONLY ? | |
3494 | "[PROBE_REQ_ONLY]" : "", | |
3495 | dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "", | |
3496 | dev->flags & P2P_DEV_NOT_YET_READY ? | |
3497 | "[NOT_YET_READY]" : "", | |
3498 | dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "", | |
3499 | dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" : | |
3500 | "", | |
3501 | dev->flags & P2P_DEV_PD_PEER_DISPLAY ? | |
3502 | "[PD_PEER_DISPLAY]" : "", | |
3503 | dev->flags & P2P_DEV_PD_PEER_KEYPAD ? | |
3504 | "[PD_PEER_KEYPAD]" : "", | |
3505 | dev->flags & P2P_DEV_USER_REJECTED ? | |
3506 | "[USER_REJECTED]" : "", | |
3507 | dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ? | |
3508 | "[PEER_WAITING_RESPONSE]" : "", | |
3509 | dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ? | |
3510 | "[PREFER_PERSISTENT_GROUP]" : "", | |
3511 | dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ? | |
3512 | "[WAIT_GO_NEG_RESPONSE]" : "", | |
3513 | dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ? | |
3514 | "[WAIT_GO_NEG_CONFIRM]" : "", | |
3515 | dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ? | |
3516 | "[GROUP_CLIENT_ONLY]" : "", | |
d5b20a73 | 3517 | dev->flags & P2P_DEV_FORCE_FREQ ? |
10c4edde JM |
3518 | "[FORCE_FREQ]" : "", |
3519 | dev->flags & P2P_DEV_PD_FOR_JOIN ? | |
3520 | "[PD_FOR_JOIN]" : "", | |
b22128ef JM |
3521 | dev->status, |
3522 | dev->wait_count, | |
3523 | dev->invitation_reqs); | |
3524 | if (res < 0 || res >= end - pos) | |
3525 | return pos - buf; | |
3526 | pos += res; | |
3527 | ||
3528 | if (dev->ext_listen_period) { | |
3529 | res = os_snprintf(pos, end - pos, | |
3530 | "ext_listen_period=%u\n" | |
3531 | "ext_listen_interval=%u\n", | |
3532 | dev->ext_listen_period, | |
3533 | dev->ext_listen_interval); | |
3534 | if (res < 0 || res >= end - pos) | |
3535 | return pos - buf; | |
3536 | pos += res; | |
3537 | } | |
3538 | ||
3539 | if (dev->oper_ssid_len) { | |
3540 | res = os_snprintf(pos, end - pos, | |
3541 | "oper_ssid=%s\n", | |
3542 | wpa_ssid_txt(dev->oper_ssid, | |
3543 | dev->oper_ssid_len)); | |
3544 | if (res < 0 || res >= end - pos) | |
3545 | return pos - buf; | |
3546 | pos += res; | |
3547 | } | |
3548 | ||
9675ce35 JM |
3549 | #ifdef CONFIG_WIFI_DISPLAY |
3550 | if (dev->info.wfd_subelems) { | |
3551 | res = os_snprintf(pos, end - pos, "wfd_subelems="); | |
3552 | if (res < 0 || res >= end - pos) | |
3553 | return pos - buf; | |
3554 | pos += res; | |
3555 | ||
3556 | pos += wpa_snprintf_hex(pos, end - pos, | |
3557 | wpabuf_head(dev->info.wfd_subelems), | |
3558 | wpabuf_len(dev->info.wfd_subelems)); | |
3559 | ||
3560 | res = os_snprintf(pos, end - pos, "\n"); | |
3561 | if (res < 0 || res >= end - pos) | |
3562 | return pos - buf; | |
3563 | pos += res; | |
3564 | } | |
3565 | #endif /* CONFIG_WIFI_DISPLAY */ | |
3566 | ||
b22128ef JM |
3567 | return pos - buf; |
3568 | } | |
3569 | ||
3570 | ||
b3bcc0f5 JM |
3571 | int p2p_peer_known(struct p2p_data *p2p, const u8 *addr) |
3572 | { | |
3573 | return p2p_get_device(p2p, addr) != NULL; | |
3574 | } | |
3575 | ||
3576 | ||
b22128ef JM |
3577 | void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled) |
3578 | { | |
3579 | if (enabled) { | |
ed496f13 | 3580 | p2p_dbg(p2p, "Client discoverability enabled"); |
b22128ef JM |
3581 | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
3582 | } else { | |
ed496f13 | 3583 | p2p_dbg(p2p, "Client discoverability disabled"); |
b22128ef JM |
3584 | p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
3585 | } | |
3586 | } | |
3587 | ||
3588 | ||
3589 | static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1, | |
3590 | u32 duration2, u32 interval2) | |
3591 | { | |
3592 | struct wpabuf *req; | |
3593 | struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL; | |
3594 | u8 *len; | |
3595 | ||
3596 | req = wpabuf_alloc(100); | |
3597 | if (req == NULL) | |
3598 | return NULL; | |
3599 | ||
3600 | if (duration1 || interval1) { | |
3601 | os_memset(&desc1, 0, sizeof(desc1)); | |
3602 | desc1.count_type = 1; | |
3603 | desc1.duration = duration1; | |
3604 | desc1.interval = interval1; | |
3605 | ptr1 = &desc1; | |
3606 | ||
3607 | if (duration2 || interval2) { | |
3608 | os_memset(&desc2, 0, sizeof(desc2)); | |
3609 | desc2.count_type = 2; | |
3610 | desc2.duration = duration2; | |
3611 | desc2.interval = interval2; | |
3612 | ptr2 = &desc2; | |
3613 | } | |
3614 | } | |
3615 | ||
3616 | p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1); | |
3617 | len = p2p_buf_add_ie_hdr(req); | |
3618 | p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2); | |
3619 | p2p_buf_update_ie_hdr(req, len); | |
3620 | ||
3621 | return req; | |
3622 | } | |
3623 | ||
3624 | ||
3625 | int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr, | |
3626 | const u8 *own_interface_addr, unsigned int freq, | |
3627 | u32 duration1, u32 interval1, u32 duration2, | |
3628 | u32 interval2) | |
3629 | { | |
3630 | struct wpabuf *req; | |
3631 | ||
ed496f13 JM |
3632 | p2p_dbg(p2p, "Send Presence Request to GO " MACSTR |
3633 | " (own interface " MACSTR ") freq=%u dur1=%u int1=%u " | |
3634 | "dur2=%u int2=%u", | |
b22128ef JM |
3635 | MAC2STR(go_interface_addr), MAC2STR(own_interface_addr), |
3636 | freq, duration1, interval1, duration2, interval2); | |
3637 | ||
3638 | req = p2p_build_presence_req(duration1, interval1, duration2, | |
3639 | interval2); | |
3640 | if (req == NULL) | |
3641 | return -1; | |
3642 | ||
3643 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3f9285ff JM |
3644 | if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr, |
3645 | go_interface_addr, | |
3646 | wpabuf_head(req), wpabuf_len(req), 200) < 0) { | |
ed496f13 | 3647 | p2p_dbg(p2p, "Failed to send Action frame"); |
b22128ef JM |
3648 | } |
3649 | wpabuf_free(req); | |
3650 | ||
3651 | return 0; | |
3652 | } | |
3653 | ||
3654 | ||
3655 | static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa, | |
3656 | size_t noa_len, u8 dialog_token) | |
3657 | { | |
3658 | struct wpabuf *resp; | |
3659 | u8 *len; | |
3660 | ||
3661 | resp = wpabuf_alloc(100 + noa_len); | |
3662 | if (resp == NULL) | |
3663 | return NULL; | |
3664 | ||
3665 | p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token); | |
3666 | len = p2p_buf_add_ie_hdr(resp); | |
3667 | p2p_buf_add_status(resp, status); | |
3668 | if (noa) { | |
3669 | wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE); | |
3670 | wpabuf_put_le16(resp, noa_len); | |
3671 | wpabuf_put_data(resp, noa, noa_len); | |
3672 | } else | |
3673 | p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL); | |
3674 | p2p_buf_update_ie_hdr(resp, len); | |
3675 | ||
3676 | return resp; | |
3677 | } | |
3678 | ||
3679 | ||
3680 | static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, | |
3681 | const u8 *sa, const u8 *data, size_t len, | |
3682 | int rx_freq) | |
3683 | { | |
3684 | struct p2p_message msg; | |
3685 | u8 status; | |
3686 | struct wpabuf *resp; | |
3687 | size_t g; | |
3688 | struct p2p_group *group = NULL; | |
3689 | int parsed = 0; | |
3690 | u8 noa[50]; | |
3691 | int noa_len; | |
3692 | ||
ed496f13 | 3693 | p2p_dbg(p2p, "Received P2P Action - P2P Presence Request"); |
b22128ef JM |
3694 | |
3695 | for (g = 0; g < p2p->num_groups; g++) { | |
3696 | if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]), | |
3697 | ETH_ALEN) == 0) { | |
3698 | group = p2p->groups[g]; | |
3699 | break; | |
3700 | } | |
3701 | } | |
3702 | if (group == NULL) { | |
ed496f13 | 3703 | p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group " |
b22128ef JM |
3704 | MACSTR, MAC2STR(da)); |
3705 | return; | |
3706 | } | |
3707 | ||
3708 | if (p2p_parse(data, len, &msg) < 0) { | |
ed496f13 | 3709 | p2p_dbg(p2p, "Failed to parse P2P Presence Request"); |
b22128ef JM |
3710 | status = P2P_SC_FAIL_INVALID_PARAMS; |
3711 | goto fail; | |
3712 | } | |
3713 | parsed = 1; | |
3714 | ||
3715 | if (msg.noa == NULL) { | |
ed496f13 | 3716 | p2p_dbg(p2p, "No NoA attribute in P2P Presence Request"); |
b22128ef JM |
3717 | status = P2P_SC_FAIL_INVALID_PARAMS; |
3718 | goto fail; | |
3719 | } | |
3720 | ||
3721 | status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len); | |
3722 | ||
3723 | fail: | |
3724 | if (p2p->cfg->get_noa) | |
3725 | noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa, | |
3726 | sizeof(noa)); | |
3727 | else | |
3728 | noa_len = -1; | |
3729 | resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL, | |
3730 | noa_len > 0 ? noa_len : 0, | |
3731 | msg.dialog_token); | |
3732 | if (parsed) | |
3733 | p2p_parse_free(&msg); | |
3734 | if (resp == NULL) | |
3735 | return; | |
3736 | ||
3737 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; | |
3f9285ff JM |
3738 | if (p2p_send_action(p2p, rx_freq, sa, da, da, |
3739 | wpabuf_head(resp), wpabuf_len(resp), 200) < 0) { | |
ed496f13 | 3740 | p2p_dbg(p2p, "Failed to send Action frame"); |
b22128ef JM |
3741 | } |
3742 | wpabuf_free(resp); | |
3743 | } | |
3744 | ||
3745 | ||
3746 | static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, | |
3747 | const u8 *sa, const u8 *data, size_t len) | |
3748 | { | |
3749 | struct p2p_message msg; | |
3750 | ||
ed496f13 | 3751 | p2p_dbg(p2p, "Received P2P Action - P2P Presence Response"); |
b22128ef JM |
3752 | |
3753 | if (p2p_parse(data, len, &msg) < 0) { | |
ed496f13 | 3754 | p2p_dbg(p2p, "Failed to parse P2P Presence Response"); |
b22128ef JM |
3755 | return; |
3756 | } | |
3757 | ||
3758 | if (msg.status == NULL || msg.noa == NULL) { | |
ed496f13 | 3759 | p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response"); |
b22128ef JM |
3760 | p2p_parse_free(&msg); |
3761 | return; | |
3762 | } | |
3763 | ||
c64e3a08 JM |
3764 | if (p2p->cfg->presence_resp) { |
3765 | p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status, | |
3766 | msg.noa, msg.noa_len); | |
3767 | } | |
3768 | ||
b22128ef | 3769 | if (*msg.status) { |
ed496f13 | 3770 | p2p_dbg(p2p, "P2P Presence Request was rejected: status %u", |
b22128ef JM |
3771 | *msg.status); |
3772 | p2p_parse_free(&msg); | |
3773 | return; | |
3774 | } | |
3775 | ||
ed496f13 | 3776 | p2p_dbg(p2p, "P2P Presence Request was accepted"); |
b22128ef JM |
3777 | wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA", |
3778 | msg.noa, msg.noa_len); | |
3779 | /* TODO: process NoA */ | |
3780 | p2p_parse_free(&msg); | |
3781 | } | |
3782 | ||
3783 | ||
3784 | static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx) | |
3785 | { | |
3786 | struct p2p_data *p2p = eloop_ctx; | |
3787 | ||
3788 | if (p2p->ext_listen_interval) { | |
3789 | /* Schedule next extended listen timeout */ | |
3790 | eloop_register_timeout(p2p->ext_listen_interval_sec, | |
3791 | p2p->ext_listen_interval_usec, | |
3792 | p2p_ext_listen_timeout, p2p, NULL); | |
3793 | } | |
3794 | ||
f7a69057 JM |
3795 | if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) { |
3796 | /* | |
3797 | * This should not really happen, but it looks like the Listen | |
3798 | * command may fail is something else (e.g., a scan) was | |
3799 | * running at an inconvenient time. As a workaround, allow new | |
3800 | * Extended Listen operation to be started. | |
3801 | */ | |
ed496f13 | 3802 | p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again"); |
f7a69057 JM |
3803 | p2p->ext_listen_only = 0; |
3804 | p2p_set_state(p2p, P2P_IDLE); | |
3805 | } | |
3806 | ||
b22128ef | 3807 | if (p2p->state != P2P_IDLE) { |
ed496f13 | 3808 | p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state)); |
b22128ef JM |
3809 | return; |
3810 | } | |
3811 | ||
ed496f13 | 3812 | p2p_dbg(p2p, "Extended Listen timeout"); |
b22128ef JM |
3813 | p2p->ext_listen_only = 1; |
3814 | if (p2p_listen(p2p, p2p->ext_listen_period) < 0) { | |
ed496f13 | 3815 | p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing"); |
b22128ef JM |
3816 | p2p->ext_listen_only = 0; |
3817 | } | |
3818 | } | |
3819 | ||
3820 | ||
3821 | int p2p_ext_listen(struct p2p_data *p2p, unsigned int period, | |
3822 | unsigned int interval) | |
3823 | { | |
3824 | if (period > 65535 || interval > 65535 || period > interval || | |
3825 | (period == 0 && interval > 0) || (period > 0 && interval == 0)) { | |
ed496f13 JM |
3826 | p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u", |
3827 | period, interval); | |
b22128ef JM |
3828 | return -1; |
3829 | } | |
3830 | ||
3831 | eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); | |
3832 | ||
3833 | if (interval == 0) { | |
ed496f13 | 3834 | p2p_dbg(p2p, "Disabling Extended Listen Timing"); |
b22128ef JM |
3835 | p2p->ext_listen_period = 0; |
3836 | p2p->ext_listen_interval = 0; | |
3837 | return 0; | |
3838 | } | |
3839 | ||
ed496f13 JM |
3840 | p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec", |
3841 | period, interval); | |
b22128ef JM |
3842 | p2p->ext_listen_period = period; |
3843 | p2p->ext_listen_interval = interval; | |
3844 | p2p->ext_listen_interval_sec = interval / 1000; | |
3845 | p2p->ext_listen_interval_usec = (interval % 1000) * 1000; | |
3846 | ||
3847 | eloop_register_timeout(p2p->ext_listen_interval_sec, | |
3848 | p2p->ext_listen_interval_usec, | |
3849 | p2p_ext_listen_timeout, p2p, NULL); | |
3850 | ||
3851 | return 0; | |
3852 | } | |
3853 | ||
3854 | ||
3855 | void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, | |
3856 | const u8 *ie, size_t ie_len) | |
3857 | { | |
3858 | struct p2p_message msg; | |
3859 | ||
3860 | if (bssid == NULL || ie == NULL) | |
3861 | return; | |
3862 | ||
3863 | os_memset(&msg, 0, sizeof(msg)); | |
3864 | if (p2p_parse_ies(ie, ie_len, &msg)) | |
3865 | return; | |
04c366cb EL |
3866 | if (msg.minor_reason_code == NULL) { |
3867 | p2p_parse_free(&msg); | |
b22128ef | 3868 | return; |
04c366cb | 3869 | } |
b22128ef | 3870 | |
ed496f13 | 3871 | p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR |
b22128ef JM |
3872 | " reason_code=%u minor_reason_code=%u", |
3873 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); | |
3874 | ||
3875 | p2p_parse_free(&msg); | |
3876 | } | |
3877 | ||
3878 | ||
3879 | void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, | |
3880 | const u8 *ie, size_t ie_len) | |
3881 | { | |
3882 | struct p2p_message msg; | |
3883 | ||
3884 | if (bssid == NULL || ie == NULL) | |
3885 | return; | |
3886 | ||
3887 | os_memset(&msg, 0, sizeof(msg)); | |
3888 | if (p2p_parse_ies(ie, ie_len, &msg)) | |
3889 | return; | |
04c366cb EL |
3890 | if (msg.minor_reason_code == NULL) { |
3891 | p2p_parse_free(&msg); | |
b22128ef | 3892 | return; |
04c366cb | 3893 | } |
b22128ef | 3894 | |
ed496f13 | 3895 | p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR |
b22128ef JM |
3896 | " reason_code=%u minor_reason_code=%u", |
3897 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); | |
3898 | ||
3899 | p2p_parse_free(&msg); | |
3900 | } | |
3901 | ||
3902 | ||
3903 | void p2p_set_managed_oper(struct p2p_data *p2p, int enabled) | |
3904 | { | |
3905 | if (enabled) { | |
ed496f13 | 3906 | p2p_dbg(p2p, "Managed P2P Device operations enabled"); |
b22128ef JM |
3907 | p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED; |
3908 | } else { | |
ed496f13 | 3909 | p2p_dbg(p2p, "Managed P2P Device operations disabled"); |
b22128ef JM |
3910 | p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED; |
3911 | } | |
3912 | } | |
3913 | ||
3914 | ||
3915 | int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel) | |
3916 | { | |
9ccd9165 | 3917 | if (p2p_channel_to_freq(reg_class, channel) < 0) |
b22128ef JM |
3918 | return -1; |
3919 | ||
ed496f13 JM |
3920 | p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u", |
3921 | reg_class, channel); | |
b22128ef JM |
3922 | p2p->cfg->reg_class = reg_class; |
3923 | p2p->cfg->channel = channel; | |
3924 | ||
3925 | return 0; | |
3926 | } | |
3927 | ||
3928 | ||
3929 | int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len) | |
3930 | { | |
ed496f13 | 3931 | p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len)); |
b22128ef JM |
3932 | if (postfix == NULL) { |
3933 | p2p->cfg->ssid_postfix_len = 0; | |
3934 | return 0; | |
3935 | } | |
3936 | if (len > sizeof(p2p->cfg->ssid_postfix)) | |
3937 | return -1; | |
3938 | os_memcpy(p2p->cfg->ssid_postfix, postfix, len); | |
3939 | p2p->cfg->ssid_postfix_len = len; | |
3940 | return 0; | |
3941 | } | |
3942 | ||
3943 | ||
2463ba70 JS |
3944 | int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel, |
3945 | int cfg_op_channel) | |
3946 | { | |
9ccd9165 | 3947 | if (p2p_channel_to_freq(op_reg_class, op_channel) < 0) |
2463ba70 JS |
3948 | return -1; |
3949 | ||
ed496f13 JM |
3950 | p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u", |
3951 | op_reg_class, op_channel); | |
2463ba70 JS |
3952 | p2p->cfg->op_reg_class = op_reg_class; |
3953 | p2p->cfg->op_channel = op_channel; | |
3954 | p2p->cfg->cfg_op_channel = cfg_op_channel; | |
3955 | return 0; | |
3956 | } | |
3957 | ||
3958 | ||
21d996f7 JM |
3959 | int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan, |
3960 | const struct p2p_channel *pref_chan) | |
3961 | { | |
3962 | struct p2p_channel *n; | |
3963 | ||
3964 | if (pref_chan) { | |
3965 | n = os_malloc(num_pref_chan * sizeof(struct p2p_channel)); | |
3966 | if (n == NULL) | |
3967 | return -1; | |
3968 | os_memcpy(n, pref_chan, | |
3969 | num_pref_chan * sizeof(struct p2p_channel)); | |
3970 | } else | |
3971 | n = NULL; | |
3972 | ||
3973 | os_free(p2p->cfg->pref_chan); | |
3974 | p2p->cfg->pref_chan = n; | |
3975 | p2p->cfg->num_pref_chan = num_pref_chan; | |
3976 | ||
3977 | return 0; | |
3978 | } | |
3979 | ||
3980 | ||
556b30da JM |
3981 | int p2p_set_no_go_freq(struct p2p_data *p2p, |
3982 | const struct wpa_freq_range_list *list) | |
3983 | { | |
3984 | struct wpa_freq_range *tmp; | |
3985 | ||
3986 | if (list == NULL || list->num == 0) { | |
3987 | os_free(p2p->no_go_freq.range); | |
3988 | p2p->no_go_freq.range = NULL; | |
3989 | p2p->no_go_freq.num = 0; | |
3990 | return 0; | |
3991 | } | |
3992 | ||
3993 | tmp = os_calloc(list->num, sizeof(struct wpa_freq_range)); | |
3994 | if (tmp == NULL) | |
3995 | return -1; | |
3996 | os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range)); | |
3997 | os_free(p2p->no_go_freq.range); | |
3998 | p2p->no_go_freq.range = tmp; | |
3999 | p2p->no_go_freq.num = list->num; | |
4000 | p2p_dbg(p2p, "Updated no GO chan list"); | |
4001 | ||
4002 | return 0; | |
4003 | } | |
4004 | ||
4005 | ||
b22128ef JM |
4006 | int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr, |
4007 | u8 *iface_addr) | |
4008 | { | |
4009 | struct p2p_device *dev = p2p_get_device(p2p, dev_addr); | |
4010 | if (dev == NULL || is_zero_ether_addr(dev->interface_addr)) | |
4011 | return -1; | |
4012 | os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN); | |
4013 | return 0; | |
4014 | } | |
80c9582a JM |
4015 | |
4016 | ||
4147a2cc JM |
4017 | int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr, |
4018 | u8 *dev_addr) | |
4019 | { | |
4020 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); | |
4021 | if (dev == NULL) | |
4022 | return -1; | |
c5db8e51 | 4023 | os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN); |
4147a2cc JM |
4024 | return 0; |
4025 | } | |
4026 | ||
4027 | ||
80c9582a JM |
4028 | void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr) |
4029 | { | |
4030 | os_memcpy(p2p->peer_filter, addr, ETH_ALEN); | |
4031 | if (is_zero_ether_addr(p2p->peer_filter)) | |
ed496f13 | 4032 | p2p_dbg(p2p, "Disable peer filter"); |
80c9582a | 4033 | else |
ed496f13 JM |
4034 | p2p_dbg(p2p, "Enable peer filter for " MACSTR, |
4035 | MAC2STR(p2p->peer_filter)); | |
80c9582a | 4036 | } |
72044390 JM |
4037 | |
4038 | ||
4039 | void p2p_set_cross_connect(struct p2p_data *p2p, int enabled) | |
4040 | { | |
ed496f13 | 4041 | p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled"); |
72044390 JM |
4042 | if (p2p->cross_connect == enabled) |
4043 | return; | |
4044 | p2p->cross_connect = enabled; | |
4045 | /* TODO: may need to tear down any action group where we are GO(?) */ | |
4046 | } | |
f8d0131a JM |
4047 | |
4048 | ||
4049 | int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr) | |
4050 | { | |
4051 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); | |
4052 | if (dev == NULL) | |
4053 | return -1; | |
4054 | if (dev->oper_freq <= 0) | |
4055 | return -1; | |
4056 | return dev->oper_freq; | |
4057 | } | |
0f66abd2 SS |
4058 | |
4059 | ||
4060 | void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled) | |
4061 | { | |
ed496f13 | 4062 | p2p_dbg(p2p, "Intra BSS distribution %s", |
0f66abd2 SS |
4063 | enabled ? "enabled" : "disabled"); |
4064 | p2p->cfg->p2p_intra_bss = enabled; | |
4065 | } | |
b5c9da8d JM |
4066 | |
4067 | ||
51e9f228 JM |
4068 | void p2p_update_channel_list(struct p2p_data *p2p, |
4069 | const struct p2p_channels *chan, | |
4070 | const struct p2p_channels *cli_chan) | |
b5c9da8d | 4071 | { |
ed496f13 | 4072 | p2p_dbg(p2p, "Update channel list"); |
b5c9da8d | 4073 | os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels)); |
51e9f228 JM |
4074 | p2p_channels_dump(p2p, "channels", &p2p->cfg->channels); |
4075 | os_memcpy(&p2p->cfg->cli_channels, cli_chan, | |
4076 | sizeof(struct p2p_channels)); | |
4077 | p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels); | |
b5c9da8d | 4078 | } |
3f9285ff JM |
4079 | |
4080 | ||
4081 | int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst, | |
4082 | const u8 *src, const u8 *bssid, const u8 *buf, | |
4083 | size_t len, unsigned int wait_time) | |
4084 | { | |
4085 | if (p2p->p2p_scan_running) { | |
ed496f13 | 4086 | p2p_dbg(p2p, "Delay Action frame TX until p2p_scan completes"); |
3f9285ff | 4087 | if (p2p->after_scan_tx) { |
ed496f13 | 4088 | p2p_dbg(p2p, "Dropped previous pending Action frame TX"); |
3f9285ff JM |
4089 | os_free(p2p->after_scan_tx); |
4090 | } | |
4091 | p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) + | |
4092 | len); | |
4093 | if (p2p->after_scan_tx == NULL) | |
4094 | return -1; | |
4095 | p2p->after_scan_tx->freq = freq; | |
4096 | os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN); | |
4097 | os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN); | |
4098 | os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN); | |
4099 | p2p->after_scan_tx->len = len; | |
4100 | p2p->after_scan_tx->wait_time = wait_time; | |
4101 | os_memcpy(p2p->after_scan_tx + 1, buf, len); | |
4102 | return 0; | |
4103 | } | |
4104 | ||
4105 | return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid, | |
4106 | buf, len, wait_time); | |
4107 | } | |
7cfc4ac3 AGS |
4108 | |
4109 | ||
4110 | void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5, | |
4111 | int freq_overall) | |
4112 | { | |
ed496f13 JM |
4113 | p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d", |
4114 | freq_24, freq_5, freq_overall); | |
7cfc4ac3 AGS |
4115 | p2p->best_freq_24 = freq_24; |
4116 | p2p->best_freq_5 = freq_5; | |
4117 | p2p->best_freq_overall = freq_overall; | |
4118 | } | |
231bbd03 SS |
4119 | |
4120 | ||
6cb27aa8 JM |
4121 | void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq) |
4122 | { | |
ed496f13 | 4123 | p2p_dbg(p2p, "Own frequency preference: %d MHz", freq); |
6cb27aa8 JM |
4124 | p2p->own_freq_preference = freq; |
4125 | } | |
4126 | ||
4127 | ||
231bbd03 SS |
4128 | const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p) |
4129 | { | |
4130 | if (p2p == NULL || p2p->go_neg_peer == NULL) | |
4131 | return NULL; | |
c5db8e51 | 4132 | return p2p->go_neg_peer->info.p2p_device_addr; |
231bbd03 | 4133 | } |
c165d81e JB |
4134 | |
4135 | ||
4136 | const struct p2p_peer_info * | |
4137 | p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next) | |
4138 | { | |
4139 | struct p2p_device *dev; | |
4140 | ||
4141 | if (addr) { | |
4142 | dev = p2p_get_device(p2p, addr); | |
4143 | if (!dev) | |
4144 | return NULL; | |
4145 | ||
4146 | if (!next) { | |
4147 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) | |
4148 | return NULL; | |
4149 | ||
4150 | return &dev->info; | |
4151 | } else { | |
4152 | do { | |
4153 | dev = dl_list_first(&dev->list, | |
4154 | struct p2p_device, | |
4155 | list); | |
4156 | if (&dev->list == &p2p->devices) | |
4157 | return NULL; | |
4158 | } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY); | |
4159 | } | |
4160 | } else { | |
4161 | dev = dl_list_first(&p2p->devices, struct p2p_device, list); | |
4162 | if (!dev) | |
4163 | return NULL; | |
4164 | while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { | |
4165 | dev = dl_list_first(&dev->list, | |
4166 | struct p2p_device, | |
4167 | list); | |
4168 | if (&dev->list == &p2p->devices) | |
4169 | return NULL; | |
4170 | } | |
4171 | } | |
4172 | ||
4173 | return &dev->info; | |
4174 | } | |
303f60d3 JM |
4175 | |
4176 | ||
4177 | int p2p_in_progress(struct p2p_data *p2p) | |
4178 | { | |
4179 | if (p2p == NULL) | |
4180 | return 0; | |
1b5d4714 | 4181 | if (p2p->state == P2P_SEARCH) |
99fcd404 | 4182 | return 2; |
fc6997b3 | 4183 | return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING; |
303f60d3 | 4184 | } |
4f219667 JM |
4185 | |
4186 | ||
4187 | void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout, | |
4188 | u8 client_timeout) | |
4189 | { | |
4190 | if (p2p) { | |
4191 | p2p->go_timeout = go_timeout; | |
4192 | p2p->client_timeout = client_timeout; | |
4193 | } | |
4194 | } | |
99fcd404 JM |
4195 | |
4196 | ||
9675ce35 JM |
4197 | #ifdef CONFIG_WIFI_DISPLAY |
4198 | ||
4199 | static void p2p_update_wfd_ie_groups(struct p2p_data *p2p) | |
4200 | { | |
4201 | size_t g; | |
4202 | struct p2p_group *group; | |
4203 | ||
4204 | for (g = 0; g < p2p->num_groups; g++) { | |
4205 | group = p2p->groups[g]; | |
3342c263 | 4206 | p2p_group_force_beacon_update_ies(group); |
9675ce35 JM |
4207 | } |
4208 | } | |
4209 | ||
4210 | ||
4211 | int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie) | |
4212 | { | |
4213 | wpabuf_free(p2p->wfd_ie_beacon); | |
4214 | p2p->wfd_ie_beacon = ie; | |
4215 | p2p_update_wfd_ie_groups(p2p); | |
4216 | return 0; | |
4217 | } | |
4218 | ||
4219 | ||
4220 | int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie) | |
4221 | { | |
4222 | wpabuf_free(p2p->wfd_ie_probe_req); | |
4223 | p2p->wfd_ie_probe_req = ie; | |
4224 | return 0; | |
4225 | } | |
4226 | ||
4227 | ||
4228 | int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie) | |
4229 | { | |
4230 | wpabuf_free(p2p->wfd_ie_probe_resp); | |
4231 | p2p->wfd_ie_probe_resp = ie; | |
4232 | p2p_update_wfd_ie_groups(p2p); | |
4233 | return 0; | |
4234 | } | |
4235 | ||
4236 | ||
4237 | int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie) | |
4238 | { | |
4239 | wpabuf_free(p2p->wfd_ie_assoc_req); | |
4240 | p2p->wfd_ie_assoc_req = ie; | |
4241 | return 0; | |
4242 | } | |
4243 | ||
4244 | ||
4245 | int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie) | |
4246 | { | |
4247 | wpabuf_free(p2p->wfd_ie_invitation); | |
4248 | p2p->wfd_ie_invitation = ie; | |
4249 | return 0; | |
4250 | } | |
4251 | ||
4252 | ||
4253 | int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie) | |
4254 | { | |
4255 | wpabuf_free(p2p->wfd_ie_prov_disc_req); | |
4256 | p2p->wfd_ie_prov_disc_req = ie; | |
4257 | return 0; | |
4258 | } | |
4259 | ||
4260 | ||
4261 | int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie) | |
4262 | { | |
4263 | wpabuf_free(p2p->wfd_ie_prov_disc_resp); | |
4264 | p2p->wfd_ie_prov_disc_resp = ie; | |
4265 | return 0; | |
4266 | } | |
4267 | ||
4268 | ||
4269 | int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie) | |
4270 | { | |
4271 | wpabuf_free(p2p->wfd_ie_go_neg); | |
4272 | p2p->wfd_ie_go_neg = ie; | |
4273 | return 0; | |
4274 | } | |
4275 | ||
4276 | ||
4277 | int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem) | |
4278 | { | |
4279 | wpabuf_free(p2p->wfd_dev_info); | |
4280 | if (elem) { | |
4281 | p2p->wfd_dev_info = wpabuf_dup(elem); | |
4282 | if (p2p->wfd_dev_info == NULL) | |
4283 | return -1; | |
4284 | } else | |
4285 | p2p->wfd_dev_info = NULL; | |
4286 | ||
4287 | return 0; | |
4288 | } | |
4289 | ||
4290 | ||
4291 | int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem) | |
4292 | { | |
4293 | wpabuf_free(p2p->wfd_assoc_bssid); | |
4294 | if (elem) { | |
4295 | p2p->wfd_assoc_bssid = wpabuf_dup(elem); | |
4296 | if (p2p->wfd_assoc_bssid == NULL) | |
4297 | return -1; | |
4298 | } else | |
4299 | p2p->wfd_assoc_bssid = NULL; | |
4300 | ||
4301 | return 0; | |
4302 | } | |
4303 | ||
4304 | ||
4305 | int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p, | |
4306 | const struct wpabuf *elem) | |
4307 | { | |
4308 | wpabuf_free(p2p->wfd_coupled_sink_info); | |
4309 | if (elem) { | |
4310 | p2p->wfd_coupled_sink_info = wpabuf_dup(elem); | |
4311 | if (p2p->wfd_coupled_sink_info == NULL) | |
4312 | return -1; | |
4313 | } else | |
4314 | p2p->wfd_coupled_sink_info = NULL; | |
4315 | ||
4316 | return 0; | |
4317 | } | |
4318 | ||
4319 | #endif /* CONFIG_WIFI_DISPLAY */ | |
96beff11 JM |
4320 | |
4321 | ||
4322 | int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int, | |
4323 | int max_disc_tu) | |
4324 | { | |
4325 | if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0) | |
4326 | return -1; | |
4327 | ||
4328 | p2p->min_disc_int = min_disc_int; | |
4329 | p2p->max_disc_int = max_disc_int; | |
4330 | p2p->max_disc_tu = max_disc_tu; | |
ed496f13 JM |
4331 | p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d", |
4332 | min_disc_int, max_disc_int, max_disc_tu); | |
96beff11 JM |
4333 | |
4334 | return 0; | |
4335 | } | |
ed496f13 JM |
4336 | |
4337 | ||
4338 | void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...) | |
4339 | { | |
4340 | va_list ap; | |
4341 | char buf[500]; | |
4342 | ||
4343 | if (!p2p->cfg->debug_print) | |
4344 | return; | |
4345 | ||
4346 | va_start(ap, fmt); | |
4347 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
4348 | buf[sizeof(buf) - 1] = '\0'; | |
4349 | va_end(ap); | |
4350 | p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf); | |
4351 | } | |
4352 | ||
4353 | ||
4354 | void p2p_info(struct p2p_data *p2p, const char *fmt, ...) | |
4355 | { | |
4356 | va_list ap; | |
4357 | char buf[500]; | |
4358 | ||
4359 | if (!p2p->cfg->debug_print) | |
4360 | return; | |
4361 | ||
4362 | va_start(ap, fmt); | |
4363 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
4364 | buf[sizeof(buf) - 1] = '\0'; | |
4365 | va_end(ap); | |
4366 | p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf); | |
4367 | } | |
4368 | ||
4369 | ||
4370 | void p2p_err(struct p2p_data *p2p, const char *fmt, ...) | |
4371 | { | |
4372 | va_list ap; | |
4373 | char buf[500]; | |
4374 | ||
4375 | if (!p2p->cfg->debug_print) | |
4376 | return; | |
4377 | ||
4378 | va_start(ap, fmt); | |
4379 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
4380 | buf[sizeof(buf) - 1] = '\0'; | |
4381 | va_end(ap); | |
4382 | p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf); | |
4383 | } | |
93588780 JM |
4384 | |
4385 | ||
4386 | #ifdef CONFIG_WPS_NFC | |
4387 | ||
59b45d1a | 4388 | static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p, |
abe44e3c JM |
4389 | int client_freq, |
4390 | const u8 *go_dev_addr, | |
4391 | const u8 *ssid, size_t ssid_len) | |
93588780 JM |
4392 | { |
4393 | struct wpabuf *buf; | |
4394 | u8 op_class, channel; | |
4395 | enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP; | |
4396 | ||
4397 | buf = wpabuf_alloc(1000); | |
4398 | if (buf == NULL) | |
4399 | return NULL; | |
4400 | ||
4401 | op_class = p2p->cfg->reg_class; | |
4402 | channel = p2p->cfg->channel; | |
4403 | ||
4404 | p2p_buf_add_capability(buf, p2p->dev_capab & | |
4405 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); | |
4406 | p2p_buf_add_device_info(buf, p2p, NULL); | |
4407 | ||
4408 | if (p2p->num_groups > 0) { | |
4409 | role = P2P_GO_IN_A_GROUP; | |
4410 | p2p_freq_to_channel(p2p_group_get_freq(p2p->groups[0]), | |
4411 | &op_class, &channel); | |
59b45d1a JM |
4412 | } else if (client_freq > 0) { |
4413 | role = P2P_CLIENT_IN_A_GROUP; | |
4414 | p2p_freq_to_channel(client_freq, &op_class, &channel); | |
93588780 JM |
4415 | } |
4416 | ||
4417 | p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class, | |
4418 | channel, role); | |
4419 | ||
4420 | if (p2p->num_groups > 0) { | |
4421 | /* Limit number of clients to avoid very long message */ | |
4422 | p2p_buf_add_group_info(p2p->groups[0], buf, 5); | |
4423 | p2p_group_buf_add_id(p2p->groups[0], buf); | |
abe44e3c JM |
4424 | } else if (client_freq > 0 && |
4425 | go_dev_addr && !is_zero_ether_addr(go_dev_addr) && | |
4426 | ssid && ssid_len > 0) { | |
4427 | /* | |
4428 | * Add the optional P2P Group ID to indicate in which group this | |
4429 | * device is a P2P Client. | |
4430 | */ | |
4431 | p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len); | |
93588780 JM |
4432 | } |
4433 | ||
4434 | return buf; | |
4435 | } | |
4436 | ||
4437 | ||
59b45d1a | 4438 | struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p, |
abe44e3c JM |
4439 | int client_freq, |
4440 | const u8 *go_dev_addr, | |
4441 | const u8 *ssid, size_t ssid_len) | |
93588780 | 4442 | { |
abe44e3c JM |
4443 | return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid, |
4444 | ssid_len); | |
93588780 JM |
4445 | } |
4446 | ||
4447 | ||
59b45d1a | 4448 | struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p, |
abe44e3c JM |
4449 | int client_freq, |
4450 | const u8 *go_dev_addr, | |
4451 | const u8 *ssid, size_t ssid_len) | |
93588780 | 4452 | { |
abe44e3c JM |
4453 | return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid, |
4454 | ssid_len); | |
93588780 JM |
4455 | } |
4456 | ||
db6ae69e JM |
4457 | |
4458 | int p2p_process_nfc_connection_handover(struct p2p_data *p2p, | |
4459 | struct p2p_nfc_params *params) | |
4460 | { | |
4461 | struct p2p_message msg; | |
4462 | struct p2p_device *dev; | |
4463 | const u8 *p2p_dev_addr; | |
59b45d1a JM |
4464 | int freq; |
4465 | enum p2p_role_indication role; | |
db6ae69e JM |
4466 | |
4467 | params->next_step = NO_ACTION; | |
4468 | ||
4469 | if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len, | |
4470 | params->p2p_attr, params->p2p_len, &msg)) { | |
4471 | p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC"); | |
4472 | p2p_parse_free(&msg); | |
4473 | return -1; | |
4474 | } | |
4475 | ||
4476 | if (msg.p2p_device_addr) | |
4477 | p2p_dev_addr = msg.p2p_device_addr; | |
4478 | else if (msg.device_id) | |
4479 | p2p_dev_addr = msg.device_id; | |
4480 | else { | |
4481 | p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id"); | |
4482 | p2p_parse_free(&msg); | |
4483 | return -1; | |
4484 | } | |
4485 | ||
4486 | if (msg.oob_dev_password) { | |
4487 | os_memcpy(params->oob_dev_pw, msg.oob_dev_password, | |
4488 | msg.oob_dev_password_len); | |
4489 | params->oob_dev_pw_len = msg.oob_dev_password_len; | |
4490 | } | |
4491 | ||
4492 | dev = p2p_create_device(p2p, p2p_dev_addr); | |
4493 | if (dev == NULL) { | |
4494 | p2p_parse_free(&msg); | |
4495 | return -1; | |
4496 | } | |
4497 | ||
4498 | params->peer = &dev->info; | |
4499 | ||
4500 | os_get_reltime(&dev->last_seen); | |
4501 | dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY); | |
4502 | p2p_copy_wps_info(p2p, dev, 0, &msg); | |
4503 | ||
59b45d1a JM |
4504 | if (!msg.oob_go_neg_channel) { |
4505 | p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included"); | |
4506 | return -1; | |
4507 | } | |
db6ae69e | 4508 | |
59b45d1a JM |
4509 | if (msg.oob_go_neg_channel[3] == 0 && |
4510 | msg.oob_go_neg_channel[4] == 0) | |
4511 | freq = 0; | |
4512 | else | |
4513 | freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3], | |
4514 | msg.oob_go_neg_channel[4]); | |
4515 | if (freq < 0) { | |
4516 | p2p_dbg(p2p, "Unknown peer OOB GO Neg channel"); | |
4517 | return -1; | |
59b45d1a | 4518 | } |
23318bea | 4519 | role = msg.oob_go_neg_channel[5]; |
db6ae69e | 4520 | |
23318bea JM |
4521 | if (role == P2P_GO_IN_A_GROUP) { |
4522 | p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq); | |
4523 | params->go_freq = freq; | |
abe44e3c JM |
4524 | } else if (role == P2P_CLIENT_IN_A_GROUP) { |
4525 | p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz", | |
4526 | freq); | |
4527 | params->go_freq = freq; | |
23318bea JM |
4528 | } else |
4529 | p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq); | |
4530 | dev->oob_go_neg_freq = freq; | |
4531 | ||
4532 | if (!params->sel && role != P2P_GO_IN_A_GROUP) { | |
59b45d1a JM |
4533 | freq = p2p_channel_to_freq(p2p->cfg->reg_class, |
4534 | p2p->cfg->channel); | |
4535 | if (freq < 0) { | |
4536 | p2p_dbg(p2p, "Own listen channel not known"); | |
4537 | return -1; | |
4538 | } | |
4539 | p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq); | |
4540 | dev->oob_go_neg_freq = freq; | |
db6ae69e JM |
4541 | } |
4542 | ||
23318bea JM |
4543 | if (msg.group_id) { |
4544 | os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN); | |
4545 | params->go_ssid_len = msg.group_id_len - ETH_ALEN; | |
4546 | os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN, | |
4547 | params->go_ssid_len); | |
4548 | } | |
59b45d1a | 4549 | |
db6ae69e JM |
4550 | p2p_parse_free(&msg); |
4551 | ||
4552 | if (dev->flags & P2P_DEV_USER_REJECTED) { | |
4553 | p2p_dbg(p2p, "Do not report rejected device"); | |
4554 | return 0; | |
4555 | } | |
4556 | ||
4557 | if (!(dev->flags & P2P_DEV_REPORTED)) { | |
4558 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info, | |
4559 | !(dev->flags & P2P_DEV_REPORTED_ONCE)); | |
4560 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; | |
4561 | } | |
4562 | ||
59b45d1a | 4563 | if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0) |
74df9ecd | 4564 | params->next_step = BOTH_GO; |
59b45d1a | 4565 | else if (role == P2P_GO_IN_A_GROUP) |
db6ae69e | 4566 | params->next_step = JOIN_GROUP; |
59b45d1a JM |
4567 | else if (role == P2P_CLIENT_IN_A_GROUP) { |
4568 | dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY; | |
4569 | params->next_step = PEER_CLIENT; | |
4570 | } else if (p2p->num_groups > 0) | |
db6ae69e JM |
4571 | params->next_step = AUTH_JOIN; |
4572 | else if (params->sel) | |
4573 | params->next_step = INIT_GO_NEG; | |
4574 | else | |
4575 | params->next_step = RESP_GO_NEG; | |
4576 | ||
4577 | return 0; | |
4578 | } | |
4579 | ||
c4f87a70 JM |
4580 | |
4581 | void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id, | |
4582 | int go_intent, | |
4583 | const u8 *own_interface_addr) | |
4584 | { | |
4585 | ||
4586 | p2p->authorized_oob_dev_pw_id = dev_pw_id; | |
4587 | if (dev_pw_id == 0) { | |
4588 | p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover"); | |
4589 | return; | |
4590 | } | |
4591 | ||
4592 | p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover", | |
4593 | dev_pw_id); | |
4594 | ||
4595 | p2p->go_intent = go_intent; | |
4596 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); | |
4597 | } | |
4598 | ||
93588780 | 4599 | #endif /* CONFIG_WPS_NFC */ |