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