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