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