/src/hostap/src/p2p/p2p.c
Line | Count | Source |
1 | | /* |
2 | | * Wi-Fi Direct - P2P module |
3 | | * Copyright (c) 2009-2010, Atheros Communications |
4 | | * |
5 | | * This software may be distributed under the terms of the BSD license. |
6 | | * See README for more details. |
7 | | */ |
8 | | |
9 | | #include "includes.h" |
10 | | |
11 | | #include "common.h" |
12 | | #include "eloop.h" |
13 | | #include "common/defs.h" |
14 | | #include "common/ieee802_11_defs.h" |
15 | | #include "common/ieee802_11_common.h" |
16 | | #include "common/wpa_common.h" |
17 | | #include "common/wpa_ctrl.h" |
18 | | #include "common/sae.h" |
19 | | #include "crypto/sha256.h" |
20 | | #include "crypto/sha384.h" |
21 | | #include "crypto/crypto.h" |
22 | | #include "pasn/pasn_common.h" |
23 | | #include "wps/wps_i.h" |
24 | | #include "p2p_i.h" |
25 | | #include "p2p.h" |
26 | | |
27 | | |
28 | | static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx); |
29 | | static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev); |
30 | | static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, |
31 | | const u8 *sa, const u8 *data, size_t len, |
32 | | int rx_freq); |
33 | | static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, |
34 | | const u8 *sa, const u8 *data, |
35 | | size_t len); |
36 | | static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx); |
37 | | static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx); |
38 | | |
39 | | |
40 | | /* |
41 | | * p2p_scan recovery timeout |
42 | | * |
43 | | * Many drivers are using 30 second timeout on scan results. Allow a bit larger |
44 | | * timeout for this to avoid hitting P2P timeout unnecessarily. |
45 | | */ |
46 | 0 | #define P2P_SCAN_TIMEOUT 35 |
47 | | |
48 | | /** |
49 | | * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer |
50 | | * entries will be removed |
51 | | */ |
52 | | #ifndef P2P_PEER_EXPIRATION_AGE |
53 | 0 | #define P2P_PEER_EXPIRATION_AGE 60 |
54 | | #endif /* P2P_PEER_EXPIRATION_AGE */ |
55 | | |
56 | | |
57 | | void p2p_expire_peers(struct p2p_data *p2p) |
58 | 0 | { |
59 | 0 | struct p2p_device *dev, *n; |
60 | 0 | struct os_reltime now; |
61 | 0 | size_t i; |
62 | |
|
63 | 0 | os_get_reltime(&now); |
64 | 0 | dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) { |
65 | 0 | if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec) |
66 | 0 | continue; |
67 | | |
68 | 0 | if (dev == p2p->go_neg_peer) { |
69 | | /* |
70 | | * GO Negotiation is in progress with the peer, so |
71 | | * don't expire the peer entry until GO Negotiation |
72 | | * fails or times out. |
73 | | */ |
74 | 0 | continue; |
75 | 0 | } |
76 | | |
77 | 0 | if (p2p->cfg->go_connected && |
78 | 0 | p2p->cfg->go_connected(p2p->cfg->cb_ctx, |
79 | 0 | dev->info.p2p_device_addr)) { |
80 | | /* |
81 | | * We are connected as a client to a group in which the |
82 | | * peer is the GO, so do not expire the peer entry. |
83 | | */ |
84 | 0 | os_get_reltime(&dev->last_seen); |
85 | 0 | continue; |
86 | 0 | } |
87 | | |
88 | 0 | for (i = 0; i < p2p->num_groups; i++) { |
89 | 0 | if (p2p_group_is_client_connected( |
90 | 0 | p2p->groups[i], dev->info.p2p_device_addr)) |
91 | 0 | break; |
92 | 0 | } |
93 | 0 | if (i < p2p->num_groups) { |
94 | | /* |
95 | | * The peer is connected as a client in a group where |
96 | | * we are the GO, so do not expire the peer entry. |
97 | | */ |
98 | 0 | os_get_reltime(&dev->last_seen); |
99 | 0 | continue; |
100 | 0 | } |
101 | | |
102 | 0 | p2p_dbg(p2p, "Expiring old peer entry " MACSTR, |
103 | 0 | MAC2STR(dev->info.p2p_device_addr)); |
104 | 0 | dl_list_del(&dev->list); |
105 | 0 | p2p_device_free(p2p, dev); |
106 | 0 | } |
107 | 0 | } |
108 | | |
109 | | |
110 | | static const char * p2p_state_txt(int state) |
111 | 19.7k | { |
112 | 19.7k | switch (state) { |
113 | 19.7k | case P2P_IDLE: |
114 | 19.7k | return "IDLE"; |
115 | 0 | case P2P_SEARCH: |
116 | 0 | return "SEARCH"; |
117 | 0 | case P2P_CONNECT: |
118 | 0 | return "CONNECT"; |
119 | 0 | case P2P_CONNECT_LISTEN: |
120 | 0 | return "CONNECT_LISTEN"; |
121 | 0 | case P2P_GO_NEG: |
122 | 0 | return "GO_NEG"; |
123 | 0 | case P2P_LISTEN_ONLY: |
124 | 0 | return "LISTEN_ONLY"; |
125 | 0 | case P2P_WAIT_PEER_CONNECT: |
126 | 0 | return "WAIT_PEER_CONNECT"; |
127 | 0 | case P2P_WAIT_PEER_IDLE: |
128 | 0 | return "WAIT_PEER_IDLE"; |
129 | 0 | case P2P_SD_DURING_FIND: |
130 | 0 | return "SD_DURING_FIND"; |
131 | 0 | case P2P_PROVISIONING: |
132 | 0 | return "PROVISIONING"; |
133 | 0 | case P2P_PD_DURING_FIND: |
134 | 0 | return "PD_DURING_FIND"; |
135 | 0 | case P2P_INVITE: |
136 | 0 | return "INVITE"; |
137 | 0 | case P2P_INVITE_LISTEN: |
138 | 0 | return "INVITE_LISTEN"; |
139 | 0 | default: |
140 | 0 | return "?"; |
141 | 19.7k | } |
142 | 19.7k | } |
143 | | |
144 | | |
145 | | const char * p2p_get_state_txt(struct p2p_data *p2p) |
146 | 0 | { |
147 | 0 | return p2p_state_txt(p2p->state); |
148 | 0 | } |
149 | | |
150 | | |
151 | | struct p2ps_advertisement * p2p_get_p2ps_adv_list(struct p2p_data *p2p) |
152 | 0 | { |
153 | 0 | return p2p ? p2p->p2ps_adv_list : NULL; |
154 | 0 | } |
155 | | |
156 | | |
157 | | void p2p_set_intended_addr(struct p2p_data *p2p, const u8 *intended_addr) |
158 | 0 | { |
159 | 0 | if (p2p && intended_addr) |
160 | 0 | os_memcpy(p2p->intended_addr, intended_addr, ETH_ALEN); |
161 | 0 | } |
162 | | |
163 | | |
164 | | u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr) |
165 | 0 | { |
166 | 0 | struct p2p_device *dev = NULL; |
167 | |
|
168 | 0 | if (!addr || !p2p) |
169 | 0 | return 0; |
170 | | |
171 | 0 | dev = p2p_get_device(p2p, addr); |
172 | 0 | if (dev) |
173 | 0 | return dev->wps_prov_info; |
174 | 0 | else |
175 | 0 | return 0; |
176 | 0 | } |
177 | | |
178 | | |
179 | | void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr) |
180 | 0 | { |
181 | 0 | struct p2p_device *dev = NULL; |
182 | |
|
183 | 0 | if (!addr || !p2p) |
184 | 0 | return; |
185 | | |
186 | 0 | dev = p2p_get_device(p2p, addr); |
187 | 0 | if (dev) |
188 | 0 | dev->wps_prov_info = 0; |
189 | 0 | } |
190 | | |
191 | | |
192 | | void p2p_set_state(struct p2p_data *p2p, int new_state) |
193 | 6.57k | { |
194 | 6.57k | p2p_dbg(p2p, "State %s -> %s", |
195 | 6.57k | p2p_state_txt(p2p->state), p2p_state_txt(new_state)); |
196 | 6.57k | p2p->state = new_state; |
197 | | |
198 | 6.57k | if (new_state == P2P_IDLE && p2p->pending_channel) { |
199 | 0 | p2p_dbg(p2p, "Apply change in listen channel"); |
200 | 0 | p2p->cfg->reg_class = p2p->pending_reg_class; |
201 | 0 | p2p->cfg->channel = p2p->pending_channel; |
202 | 0 | p2p->pending_reg_class = 0; |
203 | 0 | p2p->pending_channel = 0; |
204 | 0 | } |
205 | 6.57k | } |
206 | | |
207 | | |
208 | | void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec) |
209 | 0 | { |
210 | 0 | p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec", |
211 | 0 | p2p_state_txt(p2p->state), sec, usec); |
212 | 0 | eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); |
213 | 0 | eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL); |
214 | 0 | } |
215 | | |
216 | | |
217 | | void p2p_clear_timeout(struct p2p_data *p2p) |
218 | 6.57k | { |
219 | 6.57k | p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state)); |
220 | 6.57k | eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); |
221 | 6.57k | } |
222 | | |
223 | | |
224 | | void p2p_go_neg_failed(struct p2p_data *p2p, int status) |
225 | 0 | { |
226 | 0 | struct p2p_go_neg_results res; |
227 | 0 | struct p2p_device *peer = p2p->go_neg_peer; |
228 | |
|
229 | 0 | if (!peer) |
230 | 0 | return; |
231 | | |
232 | 0 | eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL); |
233 | 0 | if (p2p->state != P2P_SEARCH) { |
234 | | /* |
235 | | * Clear timeouts related to GO Negotiation if no new p2p_find |
236 | | * has been started. |
237 | | */ |
238 | 0 | p2p_clear_timeout(p2p); |
239 | 0 | p2p_set_state(p2p, P2P_IDLE); |
240 | 0 | } |
241 | |
|
242 | 0 | peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE; |
243 | 0 | peer->wps_method = WPS_NOT_READY; |
244 | 0 | peer->oob_pw_id = 0; |
245 | 0 | wpabuf_free(peer->go_neg_conf); |
246 | 0 | peer->go_neg_conf = NULL; |
247 | 0 | p2p->go_neg_peer = NULL; |
248 | |
|
249 | | #ifdef CONFIG_PASN |
250 | | if (peer->p2p2 && peer->pasn) |
251 | | wpa_pasn_reset(peer->pasn); |
252 | | os_memset(p2p->dev_sae_password, 0, sizeof(p2p->dev_sae_password)); |
253 | | os_memset(p2p->peer_sae_password, 0, sizeof(p2p->peer_sae_password)); |
254 | | #endif /* CONFIG_PASN */ |
255 | |
|
256 | 0 | os_memset(&res, 0, sizeof(res)); |
257 | 0 | res.status = status; |
258 | 0 | res.p2p2 = peer->p2p2; |
259 | 0 | os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN); |
260 | 0 | os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN); |
261 | 0 | p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); |
262 | 0 | } |
263 | | |
264 | | |
265 | | static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc) |
266 | 0 | { |
267 | 0 | unsigned int r, tu; |
268 | 0 | int freq; |
269 | 0 | struct wpabuf *ies; |
270 | |
|
271 | 0 | p2p_dbg(p2p, "Starting short listen state (state=%s)", |
272 | 0 | p2p_state_txt(p2p->state)); |
273 | |
|
274 | 0 | if (p2p->pending_listen_freq) { |
275 | | /* We have a pending p2p_listen request */ |
276 | 0 | p2p_dbg(p2p, "p2p_listen command pending already"); |
277 | 0 | return; |
278 | 0 | } |
279 | | |
280 | 0 | freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel); |
281 | 0 | if (freq < 0) { |
282 | 0 | p2p_dbg(p2p, "Unknown regulatory class/channel"); |
283 | 0 | return; |
284 | 0 | } |
285 | | |
286 | 0 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) |
287 | 0 | r = 0; |
288 | 0 | tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) + |
289 | 0 | p2p->min_disc_int) * 100; |
290 | 0 | if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu) |
291 | 0 | tu = p2p->max_disc_tu; |
292 | 0 | if (!dev_disc && tu < 100) |
293 | 0 | tu = 100; /* Need to wait in non-device discovery use cases */ |
294 | 0 | if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen) |
295 | 0 | tu = p2p->cfg->max_listen * 1000 / 1024; |
296 | |
|
297 | 0 | if (tu == 0) { |
298 | 0 | p2p_dbg(p2p, "Skip listen state since duration was 0 TU"); |
299 | 0 | p2p_set_timeout(p2p, 0, 0); |
300 | 0 | return; |
301 | 0 | } |
302 | | |
303 | 0 | ies = p2p_build_probe_resp_ies(p2p, NULL, 0); |
304 | 0 | if (ies == NULL) |
305 | 0 | return; |
306 | | |
307 | 0 | p2p->pending_listen_freq = freq; |
308 | 0 | p2p->pending_listen_sec = 0; |
309 | 0 | p2p->pending_listen_usec = 1024 * tu; |
310 | |
|
311 | 0 | if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000, |
312 | 0 | ies) < 0) { |
313 | 0 | p2p_dbg(p2p, "Failed to start listen mode"); |
314 | 0 | p2p->pending_listen_freq = 0; |
315 | 0 | } else { |
316 | 0 | p2p->pending_listen_wait_drv = true; |
317 | 0 | } |
318 | 0 | wpabuf_free(ies); |
319 | 0 | } |
320 | | |
321 | | |
322 | | int p2p_listen(struct p2p_data *p2p, unsigned int timeout) |
323 | 0 | { |
324 | 0 | int freq; |
325 | 0 | struct wpabuf *ies; |
326 | |
|
327 | 0 | p2p_dbg(p2p, "Going to listen(only) state"); |
328 | |
|
329 | 0 | if (p2p->pending_listen_freq) { |
330 | | /* We have a pending p2p_listen request */ |
331 | 0 | p2p_dbg(p2p, "p2p_listen command pending already"); |
332 | 0 | return -1; |
333 | 0 | } |
334 | | |
335 | 0 | freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel); |
336 | 0 | if (freq < 0) { |
337 | 0 | p2p_dbg(p2p, "Unknown regulatory class/channel"); |
338 | 0 | return -1; |
339 | 0 | } |
340 | | |
341 | 0 | p2p->pending_listen_sec = timeout / 1000; |
342 | 0 | p2p->pending_listen_usec = (timeout % 1000) * 1000; |
343 | |
|
344 | 0 | if (p2p->p2p_scan_running) { |
345 | 0 | if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) { |
346 | 0 | p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen"); |
347 | 0 | return 0; |
348 | 0 | } |
349 | 0 | p2p_dbg(p2p, "p2p_scan running - delay start of listen state"); |
350 | 0 | p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN; |
351 | 0 | return 0; |
352 | 0 | } |
353 | | |
354 | 0 | ies = p2p_build_probe_resp_ies(p2p, NULL, 0); |
355 | 0 | if (ies == NULL) |
356 | 0 | return -1; |
357 | | |
358 | 0 | p2p->pending_listen_freq = freq; |
359 | |
|
360 | 0 | if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) { |
361 | 0 | p2p_dbg(p2p, "Failed to start listen mode"); |
362 | 0 | p2p->pending_listen_freq = 0; |
363 | 0 | wpabuf_free(ies); |
364 | 0 | return -1; |
365 | 0 | } |
366 | 0 | p2p->pending_listen_wait_drv = true; |
367 | 0 | wpabuf_free(ies); |
368 | |
|
369 | 0 | p2p_set_state(p2p, P2P_LISTEN_ONLY); |
370 | |
|
371 | 0 | return 0; |
372 | 0 | } |
373 | | |
374 | | |
375 | | static void p2p_device_clear_reported(struct p2p_data *p2p) |
376 | 0 | { |
377 | 0 | struct p2p_device *dev; |
378 | 0 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
379 | 0 | dev->flags &= ~P2P_DEV_REPORTED; |
380 | 0 | dev->sd_reqs = 0; |
381 | 0 | } |
382 | 0 | } |
383 | | |
384 | | |
385 | | /** |
386 | | * p2p_get_device - Fetch a peer entry |
387 | | * @p2p: P2P module context from p2p_init() |
388 | | * @addr: P2P Device Address of the peer |
389 | | * Returns: Pointer to the device entry or %NULL if not found |
390 | | */ |
391 | | struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr) |
392 | 5.43k | { |
393 | 5.43k | struct p2p_device *dev; |
394 | 5.43k | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
395 | 3.76k | if (ether_addr_equal(dev->info.p2p_device_addr, addr)) |
396 | 1.01k | return dev; |
397 | 3.76k | } |
398 | 4.42k | return NULL; |
399 | 5.43k | } |
400 | | |
401 | | |
402 | | /** |
403 | | * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address |
404 | | * @p2p: P2P module context from p2p_init() |
405 | | * @addr: P2P Interface Address of the peer |
406 | | * Returns: Pointer to the device entry or %NULL if not found |
407 | | */ |
408 | | struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p, |
409 | | const u8 *addr) |
410 | 0 | { |
411 | 0 | struct p2p_device *dev; |
412 | 0 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
413 | 0 | if (ether_addr_equal(dev->interface_addr, addr)) |
414 | 0 | return dev; |
415 | 0 | } |
416 | 0 | return NULL; |
417 | 0 | } |
418 | | |
419 | | |
420 | | /** |
421 | | * p2p_create_device - Create a peer entry |
422 | | * @p2p: P2P module context from p2p_init() |
423 | | * @addr: P2P Device Address of the peer |
424 | | * Returns: Pointer to the device entry or %NULL on failure |
425 | | * |
426 | | * If there is already an entry for the peer, it will be returned instead of |
427 | | * creating a new one. |
428 | | */ |
429 | | static struct p2p_device * p2p_create_device(struct p2p_data *p2p, |
430 | | const u8 *addr) |
431 | 2.44k | { |
432 | 2.44k | struct p2p_device *dev, *oldest = NULL; |
433 | 2.44k | size_t count = 0; |
434 | | |
435 | 2.44k | dev = p2p_get_device(p2p, addr); |
436 | 2.44k | if (dev) |
437 | 452 | return dev; |
438 | | |
439 | 1.99k | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
440 | 736 | count++; |
441 | 736 | if (oldest == NULL || |
442 | 406 | os_reltime_before(&dev->last_seen, &oldest->last_seen)) |
443 | 340 | oldest = dev; |
444 | 736 | } |
445 | 1.99k | if (count + 1 > p2p->cfg->max_peers && oldest) { |
446 | 0 | p2p_dbg(p2p, |
447 | 0 | "Remove oldest peer entry to make room for a new peer " |
448 | 0 | MACSTR, MAC2STR(oldest->info.p2p_device_addr)); |
449 | 0 | dl_list_del(&oldest->list); |
450 | 0 | p2p_device_free(p2p, oldest); |
451 | 0 | } |
452 | | |
453 | 1.99k | dev = os_zalloc(sizeof(*dev)); |
454 | 1.99k | if (dev == NULL) |
455 | 0 | return NULL; |
456 | 1.99k | dl_list_add(&p2p->devices, &dev->list); |
457 | 1.99k | os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN); |
458 | 1.99k | dev->support_6ghz = false; |
459 | | |
460 | 1.99k | return dev; |
461 | 1.99k | } |
462 | | |
463 | | |
464 | | static void p2p_copy_client_info(struct p2p_device *dev, |
465 | | struct p2p_client_info *cli) |
466 | 193 | { |
467 | 193 | p2p_copy_filter_devname(dev->info.device_name, |
468 | 193 | sizeof(dev->info.device_name), |
469 | 193 | cli->dev_name, cli->dev_name_len); |
470 | 193 | dev->info.dev_capab = cli->dev_capab; |
471 | 193 | dev->info.config_methods = cli->config_methods; |
472 | 193 | os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8); |
473 | 193 | dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types; |
474 | 193 | if (dev->info.wps_sec_dev_type_list_len > WPS_SEC_DEV_TYPE_MAX_LEN) |
475 | 3 | dev->info.wps_sec_dev_type_list_len = WPS_SEC_DEV_TYPE_MAX_LEN; |
476 | 193 | os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types, |
477 | 193 | dev->info.wps_sec_dev_type_list_len); |
478 | 193 | } |
479 | | |
480 | | |
481 | | static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr, |
482 | | const u8 *go_interface_addr, int freq, |
483 | | const u8 *gi, size_t gi_len, |
484 | | struct os_reltime *rx_time) |
485 | 1.26k | { |
486 | 1.26k | struct p2p_group_info info; |
487 | 1.26k | size_t c; |
488 | 1.26k | struct p2p_device *dev; |
489 | | |
490 | 1.26k | if (gi == NULL) |
491 | 1.11k | return 0; |
492 | | |
493 | 148 | if (p2p_group_info_parse(gi, gi_len, &info) < 0) |
494 | 64 | return -1; |
495 | | |
496 | | /* |
497 | | * Clear old data for this group; if the devices are still in the |
498 | | * group, the information will be restored in the loop following this. |
499 | | */ |
500 | 84 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
501 | 84 | if (ether_addr_equal(dev->member_in_go_iface, |
502 | 84 | go_interface_addr)) { |
503 | 0 | os_memset(dev->member_in_go_iface, 0, ETH_ALEN); |
504 | 0 | os_memset(dev->member_in_go_dev, 0, ETH_ALEN); |
505 | 0 | } |
506 | 84 | } |
507 | | |
508 | 302 | for (c = 0; c < info.num_clients; c++) { |
509 | 218 | struct p2p_client_info *cli = &info.client[c]; |
510 | 218 | if (ether_addr_equal(cli->p2p_device_addr, p2p->cfg->dev_addr)) |
511 | 14 | continue; /* ignore our own entry */ |
512 | 204 | dev = p2p_get_device(p2p, cli->p2p_device_addr); |
513 | 204 | if (dev) { |
514 | 28 | if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY | |
515 | 28 | P2P_DEV_PROBE_REQ_ONLY)) { |
516 | | /* |
517 | | * Update information since we have not |
518 | | * received this directly from the client. |
519 | | */ |
520 | 17 | p2p_copy_client_info(dev, cli); |
521 | 17 | } else { |
522 | | /* |
523 | | * Need to update P2P Client Discoverability |
524 | | * flag since it is valid only in P2P Group |
525 | | * Info attribute. |
526 | | */ |
527 | 11 | dev->info.dev_capab &= |
528 | 11 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
529 | 11 | dev->info.dev_capab |= |
530 | 11 | cli->dev_capab & |
531 | 11 | P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
532 | 11 | } |
533 | 28 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { |
534 | 0 | dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; |
535 | 0 | } |
536 | 176 | } else { |
537 | 176 | dev = p2p_create_device(p2p, cli->p2p_device_addr); |
538 | 176 | if (dev == NULL) |
539 | 0 | continue; |
540 | 176 | dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY; |
541 | 176 | p2p_copy_client_info(dev, cli); |
542 | 176 | dev->oper_freq = freq; |
543 | 176 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, |
544 | 176 | dev->info.p2p_device_addr, |
545 | 176 | &dev->info, 1); |
546 | 176 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; |
547 | 176 | } |
548 | | |
549 | 204 | os_memcpy(dev->interface_addr, cli->p2p_interface_addr, |
550 | 204 | ETH_ALEN); |
551 | 204 | os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime)); |
552 | 204 | os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN); |
553 | 204 | os_memcpy(dev->member_in_go_iface, go_interface_addr, |
554 | 204 | ETH_ALEN); |
555 | 204 | dev->flags |= P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT; |
556 | 204 | } |
557 | | |
558 | 84 | return 0; |
559 | 148 | } |
560 | | |
561 | | |
562 | | static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev, |
563 | | int probe_req, const struct p2p_message *msg) |
564 | 2.27k | { |
565 | 2.27k | os_memcpy(dev->info.device_name, msg->device_name, |
566 | 2.27k | sizeof(dev->info.device_name)); |
567 | | |
568 | 2.27k | if (msg->manufacturer && |
569 | 23 | msg->manufacturer_len < sizeof(dev->info.manufacturer)) { |
570 | 23 | os_memset(dev->info.manufacturer, 0, |
571 | 23 | sizeof(dev->info.manufacturer)); |
572 | 23 | os_memcpy(dev->info.manufacturer, msg->manufacturer, |
573 | 23 | msg->manufacturer_len); |
574 | 23 | } |
575 | | |
576 | 2.27k | if (msg->model_name && |
577 | 17 | msg->model_name_len < sizeof(dev->info.model_name)) { |
578 | 17 | os_memset(dev->info.model_name, 0, |
579 | 17 | sizeof(dev->info.model_name)); |
580 | 17 | os_memcpy(dev->info.model_name, msg->model_name, |
581 | 17 | msg->model_name_len); |
582 | 17 | } |
583 | | |
584 | 2.27k | if (msg->model_number && |
585 | 25 | msg->model_number_len < sizeof(dev->info.model_number)) { |
586 | 25 | os_memset(dev->info.model_number, 0, |
587 | 25 | sizeof(dev->info.model_number)); |
588 | 25 | os_memcpy(dev->info.model_number, msg->model_number, |
589 | 25 | msg->model_number_len); |
590 | 25 | } |
591 | | |
592 | 2.27k | if (msg->serial_number && |
593 | 33 | msg->serial_number_len < sizeof(dev->info.serial_number)) { |
594 | 33 | os_memset(dev->info.serial_number, 0, |
595 | 33 | sizeof(dev->info.serial_number)); |
596 | 33 | os_memcpy(dev->info.serial_number, msg->serial_number, |
597 | 33 | msg->serial_number_len); |
598 | 33 | } |
599 | | |
600 | 2.27k | if (msg->pri_dev_type) |
601 | 522 | os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type, |
602 | 2.27k | sizeof(dev->info.pri_dev_type)); |
603 | 1.75k | else if (msg->wps_pri_dev_type) |
604 | 11 | os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type, |
605 | 2.27k | sizeof(dev->info.pri_dev_type)); |
606 | | |
607 | 2.27k | if (msg->wps_sec_dev_type_list) { |
608 | 6 | os_memcpy(dev->info.wps_sec_dev_type_list, |
609 | 6 | msg->wps_sec_dev_type_list, |
610 | 6 | msg->wps_sec_dev_type_list_len); |
611 | 6 | dev->info.wps_sec_dev_type_list_len = |
612 | 6 | msg->wps_sec_dev_type_list_len; |
613 | 6 | } |
614 | | |
615 | 2.27k | if (msg->capability) { |
616 | | /* |
617 | | * P2P Client Discoverability bit is reserved in all frames |
618 | | * that use this function, so do not change its value here. |
619 | | */ |
620 | 139 | dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
621 | 139 | dev->info.dev_capab |= msg->capability[0] & |
622 | 139 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
623 | 139 | dev->info.group_capab = msg->capability[1]; |
624 | 139 | } |
625 | | |
626 | 2.27k | p2p_update_peer_6ghz_capab(dev, msg); |
627 | | |
628 | 2.27k | if (msg->ext_listen_timing) { |
629 | 8 | dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing); |
630 | 8 | dev->ext_listen_interval = |
631 | 8 | WPA_GET_LE16(msg->ext_listen_timing + 2); |
632 | 8 | } |
633 | | |
634 | 2.27k | if (!probe_req) { |
635 | 1.87k | u16 new_config_methods; |
636 | 1.87k | new_config_methods = msg->config_methods ? |
637 | 1.38k | msg->config_methods : msg->wps_config_methods; |
638 | 1.87k | if (new_config_methods && |
639 | 573 | dev->info.config_methods != new_config_methods) { |
640 | 528 | p2p_dbg(p2p, "Update peer " MACSTR |
641 | 528 | " config_methods 0x%x -> 0x%x", |
642 | 528 | MAC2STR(dev->info.p2p_device_addr), |
643 | 528 | dev->info.config_methods, |
644 | 528 | new_config_methods); |
645 | 528 | dev->info.config_methods = new_config_methods; |
646 | 528 | } |
647 | 1.87k | } |
648 | 2.27k | } |
649 | | |
650 | | |
651 | | void p2p_update_peer_6ghz_capab(struct p2p_device *dev, |
652 | | const struct p2p_message *msg) |
653 | 2.61k | { |
654 | 2.61k | if (msg->capability && |
655 | 246 | (msg->capability[0] & P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE)) |
656 | 55 | dev->support_6ghz = true; |
657 | 2.61k | } |
658 | | |
659 | | |
660 | | static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies, |
661 | | size_t ies_len) |
662 | 1.84k | { |
663 | 1.84k | const u8 *pos, *end; |
664 | 1.84k | u8 id, len; |
665 | | |
666 | 1.84k | wpabuf_free(dev->info.vendor_elems); |
667 | 1.84k | dev->info.vendor_elems = NULL; |
668 | | |
669 | 1.84k | end = ies + ies_len; |
670 | | |
671 | 11.1M | for (pos = ies; end - pos > 1; pos += len) { |
672 | 11.1M | id = *pos++; |
673 | 11.1M | len = *pos++; |
674 | | |
675 | 11.1M | if (len > end - pos) |
676 | 0 | break; |
677 | | |
678 | 11.1M | if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3) |
679 | 11.1M | continue; |
680 | | |
681 | 54.5k | if (len >= 4) { |
682 | 53.6k | u32 type = WPA_GET_BE32(pos); |
683 | | |
684 | 53.6k | if (type == WPA_IE_VENDOR_TYPE || |
685 | 52.8k | type == WMM_IE_VENDOR_TYPE || |
686 | 52.6k | type == WPS_IE_VENDOR_TYPE || |
687 | 28.0k | type == P2P_IE_VENDOR_TYPE || |
688 | 15.7k | type == WFD_IE_VENDOR_TYPE) |
689 | 49.0k | continue; |
690 | 53.6k | } |
691 | | |
692 | | /* Unknown vendor element - make raw IE data available */ |
693 | 5.54k | if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0) |
694 | 0 | break; |
695 | 5.54k | wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len); |
696 | 5.54k | if (wpabuf_size(dev->info.vendor_elems) > 2000) |
697 | 29 | break; |
698 | 5.54k | } |
699 | 1.84k | } |
700 | | |
701 | | |
702 | | static int p2p_compare_wfd_info(struct p2p_device *dev, |
703 | | const struct p2p_message *msg) |
704 | 1.84k | { |
705 | 1.84k | if (dev->info.wfd_subelems && msg->wfd_subelems) { |
706 | 174 | if (dev->info.wfd_subelems->used != msg->wfd_subelems->used) |
707 | 31 | return 1; |
708 | | |
709 | 143 | return os_memcmp(dev->info.wfd_subelems->buf, |
710 | 174 | msg->wfd_subelems->buf, |
711 | 174 | dev->info.wfd_subelems->used); |
712 | 174 | } |
713 | 1.67k | if (dev->info.wfd_subelems || msg->wfd_subelems) |
714 | 225 | return 1; |
715 | | |
716 | 1.44k | return 0; |
717 | 1.67k | } |
718 | | |
719 | | |
720 | | /** |
721 | | * p2p_add_device - Add peer entries based on scan results or P2P frames |
722 | | * @p2p: P2P module context from p2p_init() |
723 | | * @addr: Source address of Beacon or Probe Response frame (may be either |
724 | | * P2P Device Address or P2P Interface Address) |
725 | | * @level: Signal level (signal strength of the received frame from the peer) |
726 | | * @freq: Frequency on which the Beacon or Probe Response frame was received |
727 | | * @rx_time: Time when the result was received |
728 | | * @ies: IEs from the Beacon or Probe Response frame |
729 | | * @ies_len: Length of ies buffer in octets |
730 | | * @scan_res: Whether this was based on scan results |
731 | | * Returns: 0 on success, -1 on failure |
732 | | * |
733 | | * If the scan result is for a GO, the clients in the group will also be added |
734 | | * to the peer table. This function can also be used with some other frames |
735 | | * like Provision Discovery Request that contains P2P Capability and P2P Device |
736 | | * Info attributes. |
737 | | */ |
738 | | int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, |
739 | | struct os_reltime *rx_time, int level, const u8 *ies, |
740 | | size_t ies_len, int scan_res) |
741 | 7.89k | { |
742 | 7.89k | struct p2p_device *dev; |
743 | 7.89k | struct p2p_message msg; |
744 | 7.89k | const u8 *p2p_dev_addr; |
745 | 7.89k | int wfd_changed; |
746 | 7.89k | int dev_name_changed; |
747 | 7.89k | int i; |
748 | 7.89k | struct os_reltime time_now; |
749 | | |
750 | 7.89k | os_memset(&msg, 0, sizeof(msg)); |
751 | 7.89k | if (p2p_parse_ies(ies, ies_len, &msg)) { |
752 | 2.42k | p2p_dbg(p2p, "Failed to parse P2P IE for a device entry"); |
753 | 2.42k | p2p_parse_free(&msg); |
754 | 2.42k | return -1; |
755 | 2.42k | } |
756 | | |
757 | 5.47k | if (msg.p2p_device_addr) |
758 | 490 | p2p_dev_addr = msg.p2p_device_addr; |
759 | 4.98k | else if (msg.device_id) |
760 | 1.35k | p2p_dev_addr = msg.device_id; |
761 | 3.62k | else { |
762 | 3.62k | p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id"); |
763 | 3.62k | p2p_parse_free(&msg); |
764 | 3.62k | return -1; |
765 | 3.62k | } |
766 | | |
767 | 1.84k | if (!is_zero_ether_addr(p2p->peer_filter) && |
768 | 0 | !ether_addr_equal(p2p_dev_addr, p2p->peer_filter)) { |
769 | 0 | p2p_dbg(p2p, "Do not add peer filter for " MACSTR |
770 | 0 | " due to peer filter", MAC2STR(p2p_dev_addr)); |
771 | 0 | p2p_parse_free(&msg); |
772 | 0 | return 0; |
773 | 0 | } |
774 | | |
775 | 1.84k | dev = p2p_create_device(p2p, p2p_dev_addr); |
776 | 1.84k | if (dev == NULL) { |
777 | 0 | p2p_parse_free(&msg); |
778 | 0 | return -1; |
779 | 0 | } |
780 | | |
781 | 1.84k | if (rx_time == NULL) { |
782 | 585 | os_get_reltime(&time_now); |
783 | 585 | rx_time = &time_now; |
784 | 585 | } |
785 | | |
786 | | /* |
787 | | * Update the device entry only if the new peer |
788 | | * entry is newer than the one previously stored, or if |
789 | | * the device was previously seen as a P2P Client in a group |
790 | | * and the new entry isn't older than a threshold. |
791 | | */ |
792 | 1.84k | if (dev->last_seen.sec > 0 && |
793 | 86 | os_reltime_before(rx_time, &dev->last_seen) && |
794 | 0 | (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) || |
795 | 0 | os_reltime_expired(&dev->last_seen, rx_time, |
796 | 0 | P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) { |
797 | 0 | p2p_dbg(p2p, |
798 | 0 | "Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u flags=0x%x)", |
799 | 0 | (unsigned int) rx_time->sec, |
800 | 0 | (unsigned int) rx_time->usec, |
801 | 0 | (unsigned int) dev->last_seen.sec, |
802 | 0 | (unsigned int) dev->last_seen.usec, |
803 | 0 | dev->flags); |
804 | 0 | p2p_parse_free(&msg); |
805 | 0 | return -1; |
806 | 0 | } |
807 | | |
808 | 1.84k | os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime)); |
809 | | |
810 | 1.84k | dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY | |
811 | 1.84k | P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT); |
812 | | |
813 | 1.84k | if (!ether_addr_equal(addr, p2p_dev_addr)) |
814 | 1.61k | os_memcpy(dev->interface_addr, addr, ETH_ALEN); |
815 | 1.84k | if (msg.ssid && |
816 | 770 | msg.ssid[1] <= sizeof(dev->oper_ssid) && |
817 | 770 | (msg.ssid[1] != P2P_WILDCARD_SSID_LEN || |
818 | 279 | os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) |
819 | 536 | != 0)) { |
820 | 536 | os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]); |
821 | 536 | dev->oper_ssid_len = msg.ssid[1]; |
822 | 536 | } |
823 | | |
824 | 1.84k | wpabuf_free(dev->info.p2ps_instance); |
825 | 1.84k | dev->info.p2ps_instance = NULL; |
826 | 1.84k | if (msg.adv_service_instance && msg.adv_service_instance_len) |
827 | 22 | dev->info.p2ps_instance = wpabuf_alloc_copy( |
828 | 22 | msg.adv_service_instance, msg.adv_service_instance_len); |
829 | | |
830 | 1.84k | if (freq >= 2412 && freq <= 2484 && msg.ds_params && |
831 | 198 | *msg.ds_params >= 1 && *msg.ds_params <= 14) { |
832 | 94 | int ds_freq; |
833 | 94 | if (*msg.ds_params == 14) |
834 | 7 | ds_freq = 2484; |
835 | 87 | else |
836 | 87 | ds_freq = 2407 + *msg.ds_params * 5; |
837 | 94 | if (freq != ds_freq) { |
838 | 75 | p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz", |
839 | 75 | freq, ds_freq); |
840 | 75 | freq = ds_freq; |
841 | 75 | } |
842 | 94 | } |
843 | | |
844 | 1.84k | if (dev->listen_freq && dev->listen_freq != freq && scan_res) { |
845 | 0 | p2p_dbg(p2p, "Update Listen frequency based on scan results (" |
846 | 0 | MACSTR " %d -> %d MHz (DS param %d)", |
847 | 0 | MAC2STR(dev->info.p2p_device_addr), dev->listen_freq, |
848 | 0 | freq, msg.ds_params ? *msg.ds_params : -1); |
849 | 0 | } |
850 | 1.84k | if (scan_res) { |
851 | 1.26k | dev->listen_freq = freq; |
852 | 1.26k | if (msg.group_info) |
853 | 148 | dev->oper_freq = freq; |
854 | 1.26k | } |
855 | 1.84k | dev->info.level = level; |
856 | | |
857 | 1.84k | dev_name_changed = os_strncmp(dev->info.device_name, msg.device_name, |
858 | 1.84k | WPS_DEV_NAME_MAX_LEN) != 0; |
859 | | |
860 | 1.84k | p2p_copy_wps_info(p2p, dev, 0, &msg); |
861 | | |
862 | 20.3k | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
863 | 18.4k | wpabuf_free(dev->info.wps_vendor_ext[i]); |
864 | 18.4k | dev->info.wps_vendor_ext[i] = NULL; |
865 | 18.4k | } |
866 | | |
867 | 2.00k | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
868 | 2.00k | if (msg.wps_vendor_ext[i] == NULL) |
869 | 1.84k | break; |
870 | 162 | dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy( |
871 | 162 | msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]); |
872 | 162 | if (dev->info.wps_vendor_ext[i] == NULL) |
873 | 0 | break; |
874 | 162 | } |
875 | | |
876 | 1.84k | wfd_changed = p2p_compare_wfd_info(dev, &msg); |
877 | | |
878 | 1.84k | if (wfd_changed) { |
879 | 369 | wpabuf_free(dev->info.wfd_subelems); |
880 | 369 | if (msg.wfd_subelems) |
881 | 360 | dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems); |
882 | 9 | else |
883 | 9 | dev->info.wfd_subelems = NULL; |
884 | 369 | } |
885 | | |
886 | 1.84k | if (scan_res) { |
887 | 1.26k | p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, |
888 | 1.26k | msg.group_info, msg.group_info_len, |
889 | 1.26k | rx_time); |
890 | 1.26k | } |
891 | | |
892 | 1.84k | p2p_parse_free(&msg); |
893 | | |
894 | 1.84k | p2p_update_peer_vendor_elems(dev, ies, ies_len); |
895 | | |
896 | 1.84k | if (dev->flags & P2P_DEV_REPORTED && !wfd_changed && |
897 | 184 | !dev_name_changed && |
898 | 39 | (!msg.adv_service_instance || |
899 | 3 | (dev->flags & P2P_DEV_P2PS_REPORTED))) |
900 | 38 | return 0; |
901 | | |
902 | 1.80k | p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)", |
903 | 1.80k | freq, (unsigned int) rx_time->sec, |
904 | 1.80k | (unsigned int) rx_time->usec); |
905 | 1.80k | if (dev->flags & P2P_DEV_USER_REJECTED) { |
906 | 0 | p2p_dbg(p2p, "Do not report rejected device"); |
907 | 0 | return 0; |
908 | 0 | } |
909 | | |
910 | 1.80k | if (dev->info.config_methods == 0 && |
911 | 1.27k | (freq == 2412 || freq == 2437 || freq == 2462)) { |
912 | | /* |
913 | | * If we have only seen a Beacon frame from a GO, we do not yet |
914 | | * know what WPS config methods it supports. Since some |
915 | | * applications use config_methods value from P2P-DEVICE-FOUND |
916 | | * events, postpone reporting this peer until we've fully |
917 | | * discovered its capabilities. |
918 | | * |
919 | | * At least for now, do this only if the peer was detected on |
920 | | * one of the social channels since that peer can be easily be |
921 | | * found again and there are no limitations of having to use |
922 | | * passive scan on this channels, so this can be done through |
923 | | * Probe Response frame that includes the config_methods |
924 | | * information. |
925 | | */ |
926 | 1.22k | p2p_dbg(p2p, "Do not report peer " MACSTR |
927 | 1.22k | " with unknown config methods", MAC2STR(addr)); |
928 | 1.22k | return 0; |
929 | 1.22k | } |
930 | | |
931 | 585 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info, |
932 | 585 | !(dev->flags & P2P_DEV_REPORTED_ONCE)); |
933 | 585 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; |
934 | | |
935 | 585 | if (msg.adv_service_instance) |
936 | 9 | dev->flags |= P2P_DEV_P2PS_REPORTED; |
937 | | |
938 | 585 | return 0; |
939 | 1.80k | } |
940 | | |
941 | | |
942 | | static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev) |
943 | 1.99k | { |
944 | 1.99k | int i; |
945 | | |
946 | 1.99k | if (p2p->go_neg_peer == dev) { |
947 | | /* |
948 | | * If GO Negotiation is in progress, report that it has failed. |
949 | | */ |
950 | 0 | p2p_go_neg_failed(p2p, -1); |
951 | 0 | } |
952 | 1.99k | if (p2p->invite_peer == dev) |
953 | 0 | p2p->invite_peer = NULL; |
954 | 1.99k | if (p2p->sd_peer == dev) |
955 | 0 | p2p->sd_peer = NULL; |
956 | 1.99k | if (p2p->pending_client_disc_go == dev) |
957 | 0 | p2p->pending_client_disc_go = NULL; |
958 | | |
959 | | /* dev_lost() device, but only if it was previously dev_found() */ |
960 | 1.99k | if (dev->flags & P2P_DEV_REPORTED_ONCE) |
961 | 626 | p2p->cfg->dev_lost(p2p->cfg->cb_ctx, |
962 | 626 | dev->info.p2p_device_addr); |
963 | | |
964 | 21.9k | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
965 | 19.9k | wpabuf_free(dev->info.wps_vendor_ext[i]); |
966 | 19.9k | dev->info.wps_vendor_ext[i] = NULL; |
967 | 19.9k | } |
968 | | |
969 | 1.99k | os_free(dev->bootstrap_params); |
970 | | |
971 | 1.99k | wpabuf_free(dev->action_frame_wrapper); |
972 | | |
973 | | #ifdef CONFIG_PASN |
974 | | if (dev->pasn) { |
975 | | wpa_pasn_reset(dev->pasn); |
976 | | pasn_data_deinit(dev->pasn); |
977 | | } |
978 | | #endif /* CONFIG_PASN */ |
979 | | |
980 | 1.99k | wpabuf_free(dev->info.wfd_subelems); |
981 | 1.99k | wpabuf_free(dev->info.vendor_elems); |
982 | 1.99k | wpabuf_free(dev->go_neg_conf); |
983 | 1.99k | wpabuf_free(dev->info.p2ps_instance); |
984 | | |
985 | 1.99k | os_free(dev); |
986 | 1.99k | } |
987 | | |
988 | | |
989 | | static int p2p_get_next_prog_freq(struct p2p_data *p2p) |
990 | 0 | { |
991 | 0 | struct p2p_channels *c; |
992 | 0 | struct p2p_reg_class *cla; |
993 | 0 | size_t cl, ch; |
994 | 0 | int found = 0; |
995 | 0 | u8 reg_class; |
996 | 0 | u8 channel; |
997 | 0 | int freq; |
998 | |
|
999 | 0 | c = &p2p->cfg->channels; |
1000 | 0 | for (cl = 0; cl < c->reg_classes; cl++) { |
1001 | 0 | cla = &c->reg_class[cl]; |
1002 | 0 | if (cla->reg_class != p2p->last_prog_scan_class) |
1003 | 0 | continue; |
1004 | 0 | for (ch = 0; ch < cla->channels; ch++) { |
1005 | 0 | if (cla->channel[ch] == p2p->last_prog_scan_chan) { |
1006 | 0 | found = 1; |
1007 | 0 | break; |
1008 | 0 | } |
1009 | 0 | } |
1010 | 0 | if (found) |
1011 | 0 | break; |
1012 | 0 | } |
1013 | |
|
1014 | 0 | if (!found) { |
1015 | | /* Start from beginning */ |
1016 | 0 | reg_class = c->reg_class[0].reg_class; |
1017 | 0 | channel = c->reg_class[0].channel[0]; |
1018 | 0 | } else { |
1019 | | /* Pick the next channel */ |
1020 | 0 | ch++; |
1021 | 0 | if (ch == cla->channels) { |
1022 | 0 | cl++; |
1023 | 0 | if (cl == c->reg_classes) |
1024 | 0 | cl = 0; |
1025 | 0 | ch = 0; |
1026 | 0 | } |
1027 | 0 | reg_class = c->reg_class[cl].reg_class; |
1028 | 0 | channel = c->reg_class[cl].channel[ch]; |
1029 | 0 | } |
1030 | |
|
1031 | 0 | freq = p2p_channel_to_freq(reg_class, channel); |
1032 | 0 | p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz", |
1033 | 0 | reg_class, channel, freq); |
1034 | 0 | p2p->last_prog_scan_class = reg_class; |
1035 | 0 | p2p->last_prog_scan_chan = channel; |
1036 | |
|
1037 | 0 | if (freq == 2412 || freq == 2437 || freq == 2462) |
1038 | 0 | return 0; /* No need to add social channels */ |
1039 | 0 | return freq; |
1040 | 0 | } |
1041 | | |
1042 | | |
1043 | | static void p2p_search(struct p2p_data *p2p) |
1044 | 0 | { |
1045 | 0 | int freq = 0; |
1046 | 0 | enum p2p_scan_type type; |
1047 | 0 | u16 pw_id = DEV_PW_DEFAULT; |
1048 | 0 | int res; |
1049 | |
|
1050 | 0 | if (p2p->drv_in_listen) { |
1051 | 0 | p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing"); |
1052 | 0 | return; |
1053 | 0 | } |
1054 | 0 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
1055 | 0 | p2p->pending_listen_wait_drv = false; |
1056 | |
|
1057 | 0 | if (p2p->find_pending_full && |
1058 | 0 | (p2p->find_type == P2P_FIND_PROGRESSIVE || |
1059 | 0 | p2p->find_type == P2P_FIND_START_WITH_FULL)) { |
1060 | 0 | type = P2P_SCAN_FULL; |
1061 | 0 | p2p_dbg(p2p, "Starting search (pending full scan)"); |
1062 | 0 | p2p->find_pending_full = 0; |
1063 | 0 | } else if ((p2p->find_type == P2P_FIND_PROGRESSIVE && |
1064 | 0 | (freq = p2p_get_next_prog_freq(p2p)) > 0) || |
1065 | 0 | (p2p->find_type == P2P_FIND_START_WITH_FULL && |
1066 | 0 | (freq = p2p->find_specified_freq) > 0)) { |
1067 | 0 | type = P2P_SCAN_SOCIAL_PLUS_ONE; |
1068 | 0 | p2p_dbg(p2p, "Starting search (+ freq %u)", freq); |
1069 | 0 | } else { |
1070 | 0 | type = P2P_SCAN_SOCIAL; |
1071 | 0 | p2p_dbg(p2p, "Starting search"); |
1072 | 0 | } |
1073 | |
|
1074 | 0 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq, |
1075 | 0 | p2p->num_req_dev_types, p2p->req_dev_types, |
1076 | 0 | p2p->find_dev_id, pw_id, p2p->include_6ghz); |
1077 | 0 | if (res < 0) { |
1078 | 0 | p2p_dbg(p2p, "Scan request schedule failed"); |
1079 | 0 | p2p_continue_find(p2p); |
1080 | 0 | } |
1081 | 0 | } |
1082 | | |
1083 | | |
1084 | | static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx) |
1085 | 0 | { |
1086 | 0 | struct p2p_data *p2p = eloop_ctx; |
1087 | 0 | p2p_dbg(p2p, "Find timeout -> stop"); |
1088 | 0 | p2p_stop_find(p2p); |
1089 | 0 | } |
1090 | | |
1091 | | |
1092 | | void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status) |
1093 | 0 | { |
1094 | 0 | if (status != 0) { |
1095 | 0 | p2p_dbg(p2p, "Scan request failed"); |
1096 | | /* Do continue find even for the first p2p_find_scan */ |
1097 | 0 | p2p_continue_find(p2p); |
1098 | 0 | } else { |
1099 | 0 | p2p_dbg(p2p, "Running p2p_scan"); |
1100 | 0 | p2p->p2p_scan_running = 1; |
1101 | 0 | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); |
1102 | 0 | eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout, |
1103 | 0 | p2p, NULL); |
1104 | 0 | } |
1105 | 0 | } |
1106 | | |
1107 | | |
1108 | | static int p2p_run_after_scan(struct p2p_data *p2p) |
1109 | 6.57k | { |
1110 | 6.57k | struct p2p_device *dev; |
1111 | 6.57k | enum p2p_after_scan op; |
1112 | | |
1113 | 6.57k | op = p2p->start_after_scan; |
1114 | 6.57k | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; |
1115 | 6.57k | switch (op) { |
1116 | 6.57k | case P2P_AFTER_SCAN_NOTHING: |
1117 | 6.57k | break; |
1118 | 0 | case P2P_AFTER_SCAN_LISTEN: |
1119 | 0 | p2p_dbg(p2p, "Start previously requested Listen state"); |
1120 | 0 | p2p_listen(p2p, p2p->pending_listen_sec * 1000 + |
1121 | 0 | p2p->pending_listen_usec / 1000); |
1122 | 0 | return 1; |
1123 | 0 | case P2P_AFTER_SCAN_CONNECT: |
1124 | 0 | p2p_dbg(p2p, "Start previously requested connect with " MACSTR, |
1125 | 0 | MAC2STR(p2p->after_scan_peer)); |
1126 | 0 | dev = p2p_get_device(p2p, p2p->after_scan_peer); |
1127 | 0 | if (dev == NULL) { |
1128 | 0 | p2p_dbg(p2p, "Peer not known anymore"); |
1129 | 0 | break; |
1130 | 0 | } |
1131 | 0 | p2p_connect_send(p2p, dev); |
1132 | 0 | return 1; |
1133 | 6.57k | } |
1134 | | |
1135 | 6.57k | return 0; |
1136 | 6.57k | } |
1137 | | |
1138 | | |
1139 | | static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx) |
1140 | 0 | { |
1141 | 0 | struct p2p_data *p2p = eloop_ctx; |
1142 | 0 | int running; |
1143 | 0 | p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running); |
1144 | 0 | running = p2p->p2p_scan_running; |
1145 | | /* Make sure we recover from missed scan results callback */ |
1146 | 0 | p2p->p2p_scan_running = 0; |
1147 | |
|
1148 | 0 | if (running) |
1149 | 0 | p2p_run_after_scan(p2p); |
1150 | 0 | } |
1151 | | |
1152 | | |
1153 | | static void p2p_free_req_dev_types(struct p2p_data *p2p) |
1154 | 13.1k | { |
1155 | 13.1k | p2p->num_req_dev_types = 0; |
1156 | 13.1k | os_free(p2p->req_dev_types); |
1157 | 13.1k | p2p->req_dev_types = NULL; |
1158 | 13.1k | } |
1159 | | |
1160 | | |
1161 | | static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash) |
1162 | 6.57k | { |
1163 | 6.57k | u8 buf[SHA256_MAC_LEN]; |
1164 | 6.57k | char str_buf[256]; |
1165 | 6.57k | const u8 *adv_array; |
1166 | 6.57k | size_t i, adv_len; |
1167 | | |
1168 | 6.57k | if (!str || !hash) |
1169 | 0 | return 0; |
1170 | | |
1171 | 6.57k | if (!str[0]) { |
1172 | 0 | os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN); |
1173 | 0 | return 1; |
1174 | 0 | } |
1175 | | |
1176 | 6.57k | adv_array = (u8 *) str_buf; |
1177 | 6.57k | adv_len = os_strlen(str); |
1178 | 6.57k | if (adv_len >= sizeof(str_buf)) |
1179 | 0 | return 0; |
1180 | | |
1181 | 98.6k | for (i = 0; i < adv_len; i++) { |
1182 | 92.0k | if (str[i] >= 'A' && str[i] <= 'Z') |
1183 | 0 | str_buf[i] = str[i] - 'A' + 'a'; |
1184 | 92.0k | else |
1185 | 92.0k | str_buf[i] = str[i]; |
1186 | 92.0k | } |
1187 | | |
1188 | 6.57k | if (sha256_vector(1, &adv_array, &adv_len, buf)) |
1189 | 0 | return 0; |
1190 | | |
1191 | 6.57k | os_memcpy(hash, buf, P2PS_HASH_LEN); |
1192 | 6.57k | return 1; |
1193 | 6.57k | } |
1194 | | |
1195 | | |
1196 | | int p2p_find(struct p2p_data *p2p, unsigned int timeout, |
1197 | | enum p2p_discovery_type type, |
1198 | | unsigned int num_req_dev_types, const u8 *req_dev_types, |
1199 | | const u8 *dev_id, unsigned int search_delay, |
1200 | | u8 seek_count, const char **seek, int freq, bool include_6ghz) |
1201 | 0 | { |
1202 | 0 | int res; |
1203 | 0 | struct os_reltime start; |
1204 | |
|
1205 | 0 | p2p_dbg(p2p, "Starting find (type=%d)", type); |
1206 | 0 | if (p2p->p2p_scan_running) { |
1207 | 0 | p2p_dbg(p2p, "p2p_scan is already running"); |
1208 | 0 | } |
1209 | |
|
1210 | 0 | p2p_free_req_dev_types(p2p); |
1211 | 0 | if (req_dev_types && num_req_dev_types) { |
1212 | 0 | p2p->req_dev_types = os_memdup(req_dev_types, |
1213 | 0 | num_req_dev_types * |
1214 | 0 | WPS_DEV_TYPE_LEN); |
1215 | 0 | if (p2p->req_dev_types == NULL) |
1216 | 0 | return -1; |
1217 | 0 | p2p->num_req_dev_types = num_req_dev_types; |
1218 | 0 | } |
1219 | | |
1220 | 0 | if (dev_id) { |
1221 | 0 | os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN); |
1222 | 0 | p2p->find_dev_id = p2p->find_dev_id_buf; |
1223 | 0 | } else |
1224 | 0 | p2p->find_dev_id = NULL; |
1225 | 0 | p2p->include_6ghz = p2p_wfd_enabled(p2p) && include_6ghz; |
1226 | 0 | if (seek_count == 0 || !seek) { |
1227 | | /* Not an ASP search */ |
1228 | 0 | p2p->p2ps_seek = 0; |
1229 | 0 | } else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) { |
1230 | | /* |
1231 | | * An empty seek string means no hash values, but still an ASP |
1232 | | * search. |
1233 | | */ |
1234 | 0 | p2p_dbg(p2p, "ASP search"); |
1235 | 0 | p2p->p2ps_seek_count = 0; |
1236 | 0 | p2p->p2ps_seek = 1; |
1237 | 0 | } else if (seek && seek_count <= P2P_MAX_QUERY_HASH) { |
1238 | 0 | u8 buf[P2PS_HASH_LEN]; |
1239 | 0 | int i, count = 0; |
1240 | |
|
1241 | 0 | for (i = 0; i < seek_count; i++) { |
1242 | 0 | if (!p2ps_gen_hash(p2p, seek[i], buf)) |
1243 | 0 | continue; |
1244 | | |
1245 | 0 | p2p_dbg(p2p, "Seek service %s hash " MACSTR, |
1246 | 0 | seek[i], MAC2STR(buf)); |
1247 | 0 | os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN], |
1248 | 0 | buf, P2PS_HASH_LEN); |
1249 | 0 | count++; |
1250 | 0 | } |
1251 | |
|
1252 | 0 | p2p->p2ps_seek_count = count; |
1253 | 0 | p2p->p2ps_seek = 1; |
1254 | 0 | } else { |
1255 | 0 | p2p->p2ps_seek_count = 0; |
1256 | 0 | p2p->p2ps_seek = 1; |
1257 | 0 | } |
1258 | | |
1259 | | /* Special case to perform wildcard search */ |
1260 | 0 | if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) { |
1261 | 0 | p2p->p2ps_seek_count = 1; |
1262 | 0 | os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash, |
1263 | 0 | P2PS_HASH_LEN); |
1264 | 0 | } |
1265 | |
|
1266 | 0 | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; |
1267 | 0 | p2p_clear_timeout(p2p); |
1268 | 0 | if (p2p->pending_listen_freq) { |
1269 | 0 | p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find"); |
1270 | 0 | p2p->pending_listen_freq = 0; |
1271 | 0 | } |
1272 | 0 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
1273 | 0 | p2p->pending_listen_wait_drv = false; |
1274 | 0 | p2p->find_pending_full = 0; |
1275 | 0 | p2p->find_type = type; |
1276 | 0 | if (freq != 2412 && freq != 2437 && freq != 2462 && freq != 60480) |
1277 | 0 | p2p->find_specified_freq = freq; |
1278 | 0 | else |
1279 | 0 | p2p->find_specified_freq = 0; |
1280 | 0 | p2p_device_clear_reported(p2p); |
1281 | 0 | os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN); |
1282 | 0 | p2p_set_state(p2p, P2P_SEARCH); |
1283 | 0 | p2p->search_delay = search_delay; |
1284 | 0 | p2p->in_search_delay = 0; |
1285 | 0 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); |
1286 | 0 | p2p->last_p2p_find_timeout = timeout; |
1287 | 0 | if (timeout) |
1288 | 0 | eloop_register_timeout(timeout, 0, p2p_find_timeout, |
1289 | 0 | p2p, NULL); |
1290 | 0 | os_get_reltime(&start); |
1291 | 0 | switch (type) { |
1292 | 0 | case P2P_FIND_START_WITH_FULL: |
1293 | 0 | if (freq > 0) { |
1294 | | /* |
1295 | | * Start with the specified channel and then move to |
1296 | | * scans for social channels and this specific channel. |
1297 | | */ |
1298 | 0 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, |
1299 | 0 | P2P_SCAN_SPECIFIC, freq, |
1300 | 0 | p2p->num_req_dev_types, |
1301 | 0 | p2p->req_dev_types, dev_id, |
1302 | 0 | DEV_PW_DEFAULT, |
1303 | 0 | p2p->include_6ghz); |
1304 | 0 | break; |
1305 | 0 | } |
1306 | | /* fall through */ |
1307 | 0 | case P2P_FIND_PROGRESSIVE: |
1308 | 0 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0, |
1309 | 0 | p2p->num_req_dev_types, |
1310 | 0 | p2p->req_dev_types, dev_id, |
1311 | 0 | DEV_PW_DEFAULT, p2p->include_6ghz); |
1312 | 0 | break; |
1313 | 0 | case P2P_FIND_ONLY_SOCIAL: |
1314 | 0 | res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0, |
1315 | 0 | p2p->num_req_dev_types, |
1316 | 0 | p2p->req_dev_types, dev_id, |
1317 | 0 | DEV_PW_DEFAULT, p2p->include_6ghz); |
1318 | 0 | break; |
1319 | 0 | default: |
1320 | 0 | return -1; |
1321 | 0 | } |
1322 | | |
1323 | 0 | if (!res) |
1324 | 0 | p2p->find_start = start; |
1325 | |
|
1326 | 0 | if (res != 0 && p2p->p2p_scan_running) { |
1327 | 0 | p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running"); |
1328 | | /* wait for the previous p2p_scan to complete */ |
1329 | 0 | if (type == P2P_FIND_PROGRESSIVE || |
1330 | 0 | (type == P2P_FIND_START_WITH_FULL && freq == 0)) |
1331 | 0 | p2p->find_pending_full = 1; |
1332 | 0 | res = 0; /* do not report failure */ |
1333 | 0 | } else if (res != 0) { |
1334 | 0 | p2p_dbg(p2p, "Failed to start p2p_scan"); |
1335 | 0 | p2p_set_state(p2p, P2P_IDLE); |
1336 | 0 | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); |
1337 | 0 | } |
1338 | |
|
1339 | 0 | return res; |
1340 | 0 | } |
1341 | | |
1342 | | |
1343 | | void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq) |
1344 | 6.57k | { |
1345 | 6.57k | p2p_dbg(p2p, "Stopping find"); |
1346 | 6.57k | eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); |
1347 | 6.57k | p2p_clear_timeout(p2p); |
1348 | 6.57k | if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND) |
1349 | 0 | p2p->cfg->find_stopped(p2p->cfg->cb_ctx); |
1350 | | |
1351 | 6.57k | p2p->p2ps_seek_count = 0; |
1352 | | |
1353 | 6.57k | p2p_set_state(p2p, P2P_IDLE); |
1354 | 6.57k | p2p_free_req_dev_types(p2p); |
1355 | 6.57k | p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; |
1356 | 6.57k | if (p2p->go_neg_peer) |
1357 | 0 | p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE; |
1358 | 6.57k | p2p->go_neg_peer = NULL; |
1359 | 6.57k | p2p->sd_peer = NULL; |
1360 | 6.57k | p2p->invite_peer = NULL; |
1361 | 6.57k | p2p_stop_listen_for_freq(p2p, freq); |
1362 | 6.57k | p2p->send_action_in_progress = 0; |
1363 | 6.57k | } |
1364 | | |
1365 | | |
1366 | | void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq) |
1367 | 6.57k | { |
1368 | 6.57k | p2p_dbg(p2p, |
1369 | 6.57k | "%s(freq=%d) pending_listen_freq=%d in_listen=%d drv_in_listen=%d", |
1370 | 6.57k | __func__, freq, p2p->pending_listen_freq, p2p->in_listen, |
1371 | 6.57k | p2p->drv_in_listen); |
1372 | 6.57k | if (freq > 0 && |
1373 | 0 | ((p2p->drv_in_listen == freq && p2p->in_listen) || |
1374 | 0 | p2p->pending_listen_freq == (unsigned int) freq)) { |
1375 | 0 | p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response"); |
1376 | 0 | return; |
1377 | 0 | } |
1378 | 6.57k | if (p2p->in_listen) { |
1379 | 0 | p2p->in_listen = 0; |
1380 | 0 | p2p_clear_timeout(p2p); |
1381 | 0 | } |
1382 | 6.57k | if (p2p->drv_in_listen) { |
1383 | | /* |
1384 | | * The driver may not deliver callback to p2p_listen_end() |
1385 | | * when the operation gets canceled, so clear the internal |
1386 | | * variable that is tracking driver state. |
1387 | | */ |
1388 | 0 | p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen); |
1389 | 0 | p2p->drv_in_listen = 0; |
1390 | 0 | } |
1391 | 6.57k | if (p2p->pending_listen_freq && |
1392 | 0 | p2p->pending_listen_freq != (unsigned int) freq && |
1393 | 0 | !p2p->drv_in_listen && p2p->pending_listen_wait_drv) { |
1394 | 0 | p2p_dbg(p2p, |
1395 | 0 | "Clear pending_listen_freq since the started listen did not complete before being stopped"); |
1396 | 0 | p2p->pending_listen_freq = 0; |
1397 | 0 | } |
1398 | 6.57k | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
1399 | 6.57k | p2p->pending_listen_wait_drv = false; |
1400 | 6.57k | } |
1401 | | |
1402 | | |
1403 | | void p2p_stop_listen(struct p2p_data *p2p) |
1404 | 0 | { |
1405 | 0 | if (p2p->state != P2P_LISTEN_ONLY) { |
1406 | 0 | p2p_dbg(p2p, "Skip stop_listen since not in listen_only state."); |
1407 | 0 | return; |
1408 | 0 | } |
1409 | | |
1410 | 0 | p2p_stop_listen_for_freq(p2p, 0); |
1411 | 0 | p2p_set_state(p2p, P2P_IDLE); |
1412 | 0 | } |
1413 | | |
1414 | | |
1415 | | void p2p_stop_find(struct p2p_data *p2p) |
1416 | 6.57k | { |
1417 | 6.57k | p2p->pending_listen_freq = 0; |
1418 | 6.57k | p2p_stop_find_for_freq(p2p, 0); |
1419 | 6.57k | } |
1420 | | |
1421 | | |
1422 | | static int p2p_prepare_channel_pref(struct p2p_data *p2p, |
1423 | | unsigned int force_freq, |
1424 | | unsigned int pref_freq, int go) |
1425 | 0 | { |
1426 | 0 | u8 op_class, op_channel; |
1427 | 0 | unsigned int freq = force_freq ? force_freq : pref_freq; |
1428 | |
|
1429 | 0 | p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d", |
1430 | 0 | force_freq, pref_freq, go); |
1431 | |
|
1432 | 0 | if (p2p->cfg->is_p2p_dfs_chan && |
1433 | 0 | p2p->cfg->is_p2p_dfs_chan(p2p->cfg->cb_ctx, freq, 0, 0) && |
1434 | 0 | p2p->dfs_ap_connected) { |
1435 | 0 | if (ieee80211_chaninfo_to_channel( |
1436 | 0 | freq, p2p->sta_connected_chan_width, 0, |
1437 | 0 | &op_class, &op_channel) < 0) { |
1438 | 0 | p2p_dbg(p2p, "Unsupported frequency %u MHz", freq); |
1439 | 0 | return -1; |
1440 | 0 | } |
1441 | 0 | } else if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) { |
1442 | 0 | p2p_dbg(p2p, "Unsupported frequency %u MHz", freq); |
1443 | 0 | return -1; |
1444 | 0 | } |
1445 | | |
1446 | 0 | if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) && |
1447 | 0 | (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class, |
1448 | 0 | op_channel))) { |
1449 | 0 | p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P", |
1450 | 0 | freq, op_class, op_channel); |
1451 | 0 | return -1; |
1452 | 0 | } |
1453 | | |
1454 | 0 | p2p->op_reg_class = op_class; |
1455 | 0 | p2p->op_channel = op_channel; |
1456 | |
|
1457 | 0 | if (force_freq) { |
1458 | 0 | p2p->channels.reg_classes = 1; |
1459 | 0 | p2p->channels.reg_class[0].channels = 1; |
1460 | 0 | p2p->channels.reg_class[0].reg_class = p2p->op_reg_class; |
1461 | 0 | p2p->channels.reg_class[0].channel[0] = p2p->op_channel; |
1462 | 0 | } else { |
1463 | 0 | p2p_copy_channels(&p2p->channels, &p2p->cfg->channels, |
1464 | 0 | p2p->allow_6ghz); |
1465 | 0 | } |
1466 | |
|
1467 | 0 | return 0; |
1468 | 0 | } |
1469 | | |
1470 | | |
1471 | | static void p2p_prepare_channel_best(struct p2p_data *p2p) |
1472 | 0 | { |
1473 | 0 | u8 op_class, op_channel; |
1474 | 0 | const int op_classes_5ghz[] = { 124, 125, 115, 0 }; |
1475 | 0 | const int op_classes_ht40[] = { 126, 127, 116, 117, 0 }; |
1476 | 0 | const int op_classes_vht[] = { 128, 0 }; |
1477 | 0 | const int op_classes_edmg[] = { 181, 182, 183, 0 }; |
1478 | 0 | const int op_classes_6ghz[] = { 131, 0 }; |
1479 | |
|
1480 | 0 | p2p_dbg(p2p, "Prepare channel best"); |
1481 | |
|
1482 | 0 | if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 && |
1483 | 0 | p2p_supported_freq(p2p, p2p->best_freq_overall) && |
1484 | 0 | p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel) |
1485 | 0 | == 0) { |
1486 | 0 | p2p_dbg(p2p, "Select best overall channel as operating channel preference"); |
1487 | 0 | p2p->op_reg_class = op_class; |
1488 | 0 | p2p->op_channel = op_channel; |
1489 | 0 | } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 && |
1490 | 0 | p2p_supported_freq(p2p, p2p->best_freq_5) && |
1491 | 0 | p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel) |
1492 | 0 | == 0) { |
1493 | 0 | p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference"); |
1494 | 0 | p2p->op_reg_class = op_class; |
1495 | 0 | p2p->op_channel = op_channel; |
1496 | 0 | } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 && |
1497 | 0 | p2p_supported_freq(p2p, p2p->best_freq_24) && |
1498 | 0 | p2p_freq_to_channel(p2p->best_freq_24, &op_class, |
1499 | 0 | &op_channel) == 0) { |
1500 | 0 | p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference"); |
1501 | 0 | p2p->op_reg_class = op_class; |
1502 | 0 | p2p->op_channel = op_channel; |
1503 | 0 | } else if (p2p->cfg->num_pref_chan > 0 && |
1504 | 0 | p2p_channels_includes(&p2p->cfg->channels, |
1505 | 0 | p2p->cfg->pref_chan[0].op_class, |
1506 | 0 | p2p->cfg->pref_chan[0].chan)) { |
1507 | 0 | p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference"); |
1508 | 0 | p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class; |
1509 | 0 | p2p->op_channel = p2p->cfg->pref_chan[0].chan; |
1510 | 0 | } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_edmg, |
1511 | 0 | &p2p->op_reg_class, &p2p->op_channel) == |
1512 | 0 | 0) { |
1513 | 0 | p2p_dbg(p2p, "Select possible EDMG channel (op_class %u channel %u) as operating channel preference", |
1514 | 0 | p2p->op_reg_class, p2p->op_channel); |
1515 | 0 | } else if (p2p->allow_6ghz && |
1516 | 0 | (p2p_channel_select(&p2p->cfg->channels, op_classes_6ghz, |
1517 | 0 | &p2p->op_reg_class, &p2p->op_channel) == |
1518 | 0 | 0)) { |
1519 | 0 | p2p_dbg(p2p, "Select possible 6 GHz channel (op_class %u channel %u) as operating channel preference", |
1520 | 0 | p2p->op_reg_class, p2p->op_channel); |
1521 | 0 | } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht, |
1522 | 0 | &p2p->op_reg_class, &p2p->op_channel) == |
1523 | 0 | 0) { |
1524 | 0 | p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference", |
1525 | 0 | p2p->op_reg_class, p2p->op_channel); |
1526 | 0 | } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40, |
1527 | 0 | &p2p->op_reg_class, &p2p->op_channel) == |
1528 | 0 | 0) { |
1529 | 0 | p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference", |
1530 | 0 | p2p->op_reg_class, p2p->op_channel); |
1531 | 0 | } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz, |
1532 | 0 | &p2p->op_reg_class, &p2p->op_channel) == |
1533 | 0 | 0) { |
1534 | 0 | p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference", |
1535 | 0 | p2p->op_reg_class, p2p->op_channel); |
1536 | 0 | } else if (p2p_channels_includes(&p2p->cfg->channels, |
1537 | 0 | p2p->cfg->op_reg_class, |
1538 | 0 | p2p->cfg->op_channel)) { |
1539 | 0 | p2p_dbg(p2p, "Select pre-configured channel as operating channel preference"); |
1540 | 0 | p2p->op_reg_class = p2p->cfg->op_reg_class; |
1541 | 0 | p2p->op_channel = p2p->cfg->op_channel; |
1542 | 0 | } else if (p2p_channel_random_social(&p2p->cfg->channels, |
1543 | 0 | &p2p->op_reg_class, |
1544 | 0 | &p2p->op_channel, |
1545 | 0 | NULL, NULL) == 0) { |
1546 | 0 | p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference", |
1547 | 0 | p2p->op_reg_class, p2p->op_channel); |
1548 | 0 | } else { |
1549 | | /* Select any random available channel from the first available |
1550 | | * operating class */ |
1551 | 0 | if (p2p_channel_select(&p2p->cfg->channels, NULL, |
1552 | 0 | &p2p->op_reg_class, |
1553 | 0 | &p2p->op_channel) == 0) |
1554 | 0 | p2p_dbg(p2p, |
1555 | 0 | "Select random available channel %d from operating class %d as operating channel preference", |
1556 | 0 | p2p->op_channel, p2p->op_reg_class); |
1557 | 0 | } |
1558 | |
|
1559 | 0 | p2p_copy_channels(&p2p->channels, &p2p->cfg->channels, p2p->allow_6ghz); |
1560 | 0 | } |
1561 | | |
1562 | | |
1563 | | /** |
1564 | | * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD |
1565 | | * @p2p: P2P module context from p2p_init() |
1566 | | * @dev: Selected peer device |
1567 | | * @force_freq: Forced frequency in MHz or 0 if not forced |
1568 | | * @pref_freq: Preferred frequency in MHz or 0 if no preference |
1569 | | * @go: Whether the local end will be forced to be GO |
1570 | | * Returns: 0 on success, -1 on failure (channel not supported for P2P) |
1571 | | * |
1572 | | * This function is used to do initial operating channel selection for GO |
1573 | | * Negotiation prior to having received peer information or for P2PS PD |
1574 | | * signalling. The selected channel may be further optimized in |
1575 | | * p2p_reselect_channel() once the peer information is available. |
1576 | | */ |
1577 | | int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev, |
1578 | | unsigned int force_freq, unsigned int pref_freq, int go) |
1579 | 0 | { |
1580 | 0 | p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d", |
1581 | 0 | force_freq, pref_freq, go); |
1582 | 0 | if (force_freq || pref_freq) { |
1583 | 0 | if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) < |
1584 | 0 | 0) |
1585 | 0 | return -1; |
1586 | 0 | } else { |
1587 | 0 | p2p_prepare_channel_best(p2p); |
1588 | 0 | } |
1589 | | |
1590 | 0 | if (p2p->cfg->is_p2p_dfs_chan && |
1591 | 0 | p2p->cfg->is_p2p_dfs_chan(p2p->cfg->cb_ctx, 0, |
1592 | 0 | p2p->op_reg_class, p2p->op_channel)) { |
1593 | 0 | struct p2p_channels p2p_chanlist; |
1594 | |
|
1595 | 0 | p2p_dfs_channel_filter(p2p, &p2p->channels, &p2p_chanlist, go); |
1596 | 0 | p2p_channels_dump(p2p, |
1597 | 0 | "Filtered channel list with allowed DFS channels", |
1598 | 0 | &p2p_chanlist); |
1599 | 0 | p2p_copy_channels(&p2p->channels, &p2p_chanlist, |
1600 | 0 | p2p->allow_6ghz); |
1601 | 0 | } |
1602 | |
|
1603 | 0 | p2p_channels_dump(p2p, "prepared channels", &p2p->channels); |
1604 | 0 | if (go) |
1605 | 0 | p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq); |
1606 | 0 | else if (!force_freq) |
1607 | 0 | p2p_channels_union_inplace(&p2p->channels, |
1608 | 0 | &p2p->cfg->cli_channels); |
1609 | 0 | p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels); |
1610 | |
|
1611 | 0 | p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s", |
1612 | 0 | p2p->op_reg_class, p2p->op_channel, |
1613 | 0 | force_freq ? " (forced)" : ""); |
1614 | |
|
1615 | 0 | if (force_freq) |
1616 | 0 | dev->flags |= P2P_DEV_FORCE_FREQ; |
1617 | 0 | else |
1618 | 0 | dev->flags &= ~P2P_DEV_FORCE_FREQ; |
1619 | |
|
1620 | 0 | return 0; |
1621 | 0 | } |
1622 | | |
1623 | | |
1624 | | static void p2p_set_dev_persistent(struct p2p_device *dev, |
1625 | | int persistent_group) |
1626 | 0 | { |
1627 | 0 | switch (persistent_group) { |
1628 | 0 | case 0: |
1629 | 0 | dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP | |
1630 | 0 | P2P_DEV_PREFER_PERSISTENT_RECONN); |
1631 | 0 | break; |
1632 | 0 | case 1: |
1633 | 0 | dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP; |
1634 | 0 | dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN; |
1635 | 0 | break; |
1636 | 0 | case 2: |
1637 | 0 | dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP | |
1638 | 0 | P2P_DEV_PREFER_PERSISTENT_RECONN; |
1639 | 0 | break; |
1640 | 0 | } |
1641 | 0 | } |
1642 | | |
1643 | | |
1644 | | int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr, |
1645 | | enum p2p_wps_method wps_method, |
1646 | | int go_intent, const u8 *own_interface_addr, |
1647 | | unsigned int force_freq, int persistent_group, |
1648 | | const u8 *force_ssid, size_t force_ssid_len, |
1649 | | int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id, |
1650 | | bool p2p2, u16 bootstrap, const char *password) |
1651 | 0 | { |
1652 | 0 | struct p2p_device *dev; |
1653 | |
|
1654 | 0 | p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR |
1655 | 0 | " GO Intent=%d Intended Interface Address=" MACSTR |
1656 | 0 | " wps_method=%d persistent_group=%d pd_before_go_neg=%d " |
1657 | 0 | "oob_pw_id=%u allow_6ghz=%d", |
1658 | 0 | MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), |
1659 | 0 | wps_method, persistent_group, pd_before_go_neg, oob_pw_id, |
1660 | 0 | p2p->allow_6ghz); |
1661 | |
|
1662 | 0 | dev = p2p_get_device(p2p, peer_addr); |
1663 | 0 | if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) { |
1664 | 0 | p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR, |
1665 | 0 | MAC2STR(peer_addr)); |
1666 | 0 | return -1; |
1667 | 0 | } |
1668 | | |
1669 | 0 | if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, |
1670 | 0 | go_intent == 15) < 0) |
1671 | 0 | return -1; |
1672 | | |
1673 | 0 | if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) { |
1674 | 0 | if (!(dev->info.dev_capab & |
1675 | 0 | P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) { |
1676 | 0 | p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR |
1677 | 0 | " that is in a group and is not discoverable", |
1678 | 0 | MAC2STR(peer_addr)); |
1679 | 0 | return -1; |
1680 | 0 | } |
1681 | 0 | if (dev->oper_freq <= 0) { |
1682 | 0 | p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR |
1683 | 0 | " with incomplete information", |
1684 | 0 | MAC2STR(peer_addr)); |
1685 | 0 | return -1; |
1686 | 0 | } |
1687 | | |
1688 | | /* |
1689 | | * First, try to connect directly. If the peer does not |
1690 | | * acknowledge frames, assume it is sleeping and use device |
1691 | | * discoverability via the GO at that point. |
1692 | | */ |
1693 | 0 | } |
1694 | | |
1695 | 0 | p2p->ssid_set = 0; |
1696 | 0 | if (force_ssid) { |
1697 | 0 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID", |
1698 | 0 | force_ssid, force_ssid_len); |
1699 | 0 | os_memcpy(p2p->ssid, force_ssid, force_ssid_len); |
1700 | 0 | p2p->ssid_len = force_ssid_len; |
1701 | 0 | p2p->ssid_set = 1; |
1702 | 0 | } |
1703 | |
|
1704 | 0 | dev->flags &= ~P2P_DEV_NOT_YET_READY; |
1705 | 0 | dev->flags &= ~P2P_DEV_USER_REJECTED; |
1706 | 0 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; |
1707 | 0 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; |
1708 | 0 | if (pd_before_go_neg) |
1709 | 0 | dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG; |
1710 | 0 | else { |
1711 | 0 | dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG; |
1712 | | /* |
1713 | | * Assign dialog token and tie breaker here to use the same |
1714 | | * values in each retry within the same GO Negotiation exchange. |
1715 | | */ |
1716 | 0 | dev->dialog_token++; |
1717 | 0 | if (dev->dialog_token == 0) |
1718 | 0 | dev->dialog_token = 1; |
1719 | 0 | dev->tie_breaker = p2p->next_tie_breaker; |
1720 | 0 | p2p->next_tie_breaker = !p2p->next_tie_breaker; |
1721 | 0 | } |
1722 | 0 | dev->connect_reqs = 0; |
1723 | 0 | dev->go_neg_req_sent = 0; |
1724 | 0 | dev->go_state = UNKNOWN_GO; |
1725 | 0 | p2p_set_dev_persistent(dev, persistent_group); |
1726 | 0 | p2p->go_intent = go_intent; |
1727 | 0 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); |
1728 | |
|
1729 | 0 | if (p2p->state != P2P_IDLE) |
1730 | 0 | p2p_stop_find(p2p); |
1731 | |
|
1732 | 0 | dev->wps_method = wps_method; |
1733 | 0 | dev->oob_pw_id = oob_pw_id; |
1734 | 0 | dev->p2p2 = p2p2; |
1735 | 0 | dev->req_bootstrap_method = bootstrap; |
1736 | 0 | if (password && os_strlen(password) < sizeof(dev->password)) |
1737 | 0 | os_strlcpy(dev->password, password, sizeof(dev->password)); |
1738 | 0 | dev->status = P2P_SC_SUCCESS; |
1739 | |
|
1740 | 0 | if (p2p->p2p_scan_running) { |
1741 | 0 | p2p_dbg(p2p, "p2p_scan running - delay connect send"); |
1742 | 0 | p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT; |
1743 | 0 | os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN); |
1744 | 0 | return 0; |
1745 | 0 | } |
1746 | | |
1747 | 0 | return p2p_connect_send(p2p, dev); |
1748 | 0 | } |
1749 | | |
1750 | | |
1751 | | int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr, |
1752 | | enum p2p_wps_method wps_method, |
1753 | | int go_intent, const u8 *own_interface_addr, |
1754 | | unsigned int force_freq, int persistent_group, |
1755 | | const u8 *force_ssid, size_t force_ssid_len, |
1756 | | unsigned int pref_freq, u16 oob_pw_id, u16 bootstrap, |
1757 | | const char *password) |
1758 | 0 | { |
1759 | 0 | struct p2p_device *dev; |
1760 | |
|
1761 | 0 | p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR |
1762 | 0 | " GO Intent=%d Intended Interface Address=" MACSTR |
1763 | 0 | " wps_method=%d persistent_group=%d oob_pw_id=%u allow_6ghz=%d", |
1764 | 0 | MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), |
1765 | 0 | wps_method, persistent_group, oob_pw_id, p2p->allow_6ghz); |
1766 | |
|
1767 | 0 | dev = p2p_get_device(p2p, peer_addr); |
1768 | 0 | if (dev == NULL) { |
1769 | 0 | p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR, |
1770 | 0 | MAC2STR(peer_addr)); |
1771 | 0 | return -1; |
1772 | 0 | } |
1773 | | |
1774 | 0 | if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent == |
1775 | 0 | 15) < 0) |
1776 | 0 | return -1; |
1777 | | |
1778 | 0 | p2p->ssid_set = 0; |
1779 | 0 | if (force_ssid) { |
1780 | 0 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID", |
1781 | 0 | force_ssid, force_ssid_len); |
1782 | 0 | os_memcpy(p2p->ssid, force_ssid, force_ssid_len); |
1783 | 0 | p2p->ssid_len = force_ssid_len; |
1784 | 0 | p2p->ssid_set = 1; |
1785 | 0 | } |
1786 | |
|
1787 | 0 | dev->flags &= ~P2P_DEV_NOT_YET_READY; |
1788 | 0 | dev->flags &= ~P2P_DEV_USER_REJECTED; |
1789 | 0 | dev->go_neg_req_sent = 0; |
1790 | 0 | dev->go_state = UNKNOWN_GO; |
1791 | 0 | dev->req_bootstrap_method = bootstrap; |
1792 | |
|
1793 | 0 | if (password && os_strlen(password) < sizeof(dev->password)) |
1794 | 0 | os_strlcpy(dev->password, password, sizeof(dev->password)); |
1795 | 0 | p2p_set_dev_persistent(dev, persistent_group); |
1796 | 0 | p2p->go_intent = go_intent; |
1797 | 0 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); |
1798 | |
|
1799 | 0 | dev->wps_method = wps_method; |
1800 | 0 | dev->oob_pw_id = oob_pw_id; |
1801 | 0 | dev->status = P2P_SC_SUCCESS; |
1802 | |
|
1803 | 0 | return 0; |
1804 | 0 | } |
1805 | | |
1806 | | |
1807 | | void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr, |
1808 | | struct p2p_device *dev, struct p2p_message *msg) |
1809 | 25 | { |
1810 | 25 | os_get_reltime(&dev->last_seen); |
1811 | | |
1812 | 25 | p2p_copy_wps_info(p2p, dev, 0, msg); |
1813 | | |
1814 | 25 | if (msg->listen_channel) { |
1815 | 25 | int freq; |
1816 | 25 | freq = p2p_channel_to_freq(msg->listen_channel[3], |
1817 | 25 | msg->listen_channel[4]); |
1818 | 25 | if (freq < 0) { |
1819 | 9 | p2p_dbg(p2p, "Unknown peer Listen channel: " |
1820 | 9 | "country=%c%c(0x%02x) reg_class=%u channel=%u", |
1821 | 9 | msg->listen_channel[0], |
1822 | 9 | msg->listen_channel[1], |
1823 | 9 | msg->listen_channel[2], |
1824 | 9 | msg->listen_channel[3], |
1825 | 9 | msg->listen_channel[4]); |
1826 | 16 | } else { |
1827 | 16 | p2p_dbg(p2p, "Update peer " MACSTR |
1828 | 16 | " Listen channel: %u -> %u MHz", |
1829 | 16 | MAC2STR(dev->info.p2p_device_addr), |
1830 | 16 | dev->listen_freq, freq); |
1831 | 16 | dev->listen_freq = freq; |
1832 | 16 | } |
1833 | 25 | } |
1834 | | |
1835 | 25 | if (msg->wfd_subelems) { |
1836 | 3 | wpabuf_free(dev->info.wfd_subelems); |
1837 | 3 | dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems); |
1838 | 3 | } |
1839 | | |
1840 | 25 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { |
1841 | 1 | dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; |
1842 | 1 | p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request"); |
1843 | 24 | } else { |
1844 | 24 | p2p_dbg(p2p, "Created device entry based on GO Neg Req: " |
1845 | 24 | MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' " |
1846 | 24 | "listen_freq=%d", |
1847 | 24 | MAC2STR(dev->info.p2p_device_addr), |
1848 | 24 | dev->info.dev_capab, dev->info.group_capab, |
1849 | 24 | dev->info.device_name, dev->listen_freq); |
1850 | 24 | } |
1851 | | |
1852 | 25 | dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY; |
1853 | | |
1854 | 25 | if (dev->flags & P2P_DEV_USER_REJECTED) { |
1855 | 0 | p2p_dbg(p2p, "Do not report rejected device"); |
1856 | 0 | return; |
1857 | 0 | } |
1858 | | |
1859 | 25 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info, |
1860 | 25 | !(dev->flags & P2P_DEV_REPORTED_ONCE)); |
1861 | 25 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; |
1862 | 25 | } |
1863 | | |
1864 | | |
1865 | | void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len) |
1866 | 0 | { |
1867 | 0 | os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); |
1868 | 0 | p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2); |
1869 | 0 | os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2], |
1870 | 0 | p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len); |
1871 | 0 | *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len; |
1872 | 0 | } |
1873 | | |
1874 | | |
1875 | | int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params) |
1876 | 0 | { |
1877 | 0 | if (p2p->ssid_set) { |
1878 | 0 | os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len); |
1879 | 0 | params->ssid_len = p2p->ssid_len; |
1880 | 0 | } else { |
1881 | 0 | p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len); |
1882 | 0 | } |
1883 | 0 | p2p->ssid_set = 0; |
1884 | |
|
1885 | 0 | params->cipher = WPA_CIPHER_CCMP; |
1886 | 0 | if (p2p->cfg->pairing_config.pasn_type & 0xc) |
1887 | 0 | params->cipher |= WPA_CIPHER_GCMP_256; |
1888 | |
|
1889 | 0 | p2p_random(params->passphrase, p2p->cfg->passphrase_len); |
1890 | 0 | params->passphrase[p2p->cfg->passphrase_len] = '\0'; |
1891 | |
|
1892 | 0 | if (params->p2p2) { |
1893 | 0 | os_strlcpy(p2p->dev_sae_password, params->passphrase, |
1894 | 0 | sizeof(p2p->dev_sae_password)); |
1895 | 0 | os_strlcpy(params->sae_password, p2p->dev_sae_password, |
1896 | 0 | sizeof(params->sae_password)); |
1897 | 0 | } |
1898 | |
|
1899 | 0 | return 0; |
1900 | 0 | } |
1901 | | |
1902 | | |
1903 | | void p2p_set_go_role(struct p2p_data *p2p, bool val) |
1904 | 0 | { |
1905 | 0 | p2p->go_role = val; |
1906 | 0 | } |
1907 | | |
1908 | | |
1909 | | void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer) |
1910 | 0 | { |
1911 | 0 | struct p2p_go_neg_results res; |
1912 | 0 | int go = peer->go_state == LOCAL_GO; |
1913 | 0 | struct p2p_channels intersection; |
1914 | |
|
1915 | 0 | p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)", |
1916 | 0 | MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer"); |
1917 | |
|
1918 | 0 | os_memset(&res, 0, sizeof(res)); |
1919 | 0 | res.role_go = go; |
1920 | 0 | os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN); |
1921 | 0 | os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN); |
1922 | 0 | res.wps_method = peer->wps_method; |
1923 | 0 | if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) { |
1924 | 0 | if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN) |
1925 | 0 | res.persistent_group = 2; |
1926 | 0 | else |
1927 | 0 | res.persistent_group = 1; |
1928 | 0 | } |
1929 | |
|
1930 | 0 | if (go) { |
1931 | | /* Setup AP mode for WPS provisioning */ |
1932 | 0 | res.freq = p2p_channel_to_freq(p2p->op_reg_class, |
1933 | 0 | p2p->op_channel); |
1934 | 0 | os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); |
1935 | 0 | res.ssid_len = p2p->ssid_len; |
1936 | 0 | p2p_random(res.passphrase, p2p->cfg->passphrase_len); |
1937 | 0 | res.passphrase[p2p->cfg->passphrase_len] = '\0'; |
1938 | 0 | } else { |
1939 | 0 | res.freq = peer->oper_freq; |
1940 | 0 | if (p2p->ssid_len) { |
1941 | 0 | os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); |
1942 | 0 | res.ssid_len = p2p->ssid_len; |
1943 | 0 | } |
1944 | 0 | } |
1945 | |
|
1946 | 0 | p2p_channels_dump(p2p, "own channels", &p2p->channels); |
1947 | 0 | p2p_channels_dump(p2p, "peer channels", &peer->channels); |
1948 | 0 | p2p_channels_intersect(&p2p->channels, &peer->channels, |
1949 | 0 | &intersection); |
1950 | 0 | if (go) { |
1951 | 0 | p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq); |
1952 | 0 | p2p_channels_dump(p2p, "intersection after no-GO removal", |
1953 | 0 | &intersection); |
1954 | 0 | } |
1955 | |
|
1956 | 0 | p2p_channels_to_freqs(&intersection, res.freq_list, |
1957 | 0 | P2P_MAX_CHANNELS); |
1958 | |
|
1959 | 0 | res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout; |
1960 | |
|
1961 | 0 | p2p_clear_timeout(p2p); |
1962 | 0 | p2p->ssid_set = 0; |
1963 | 0 | peer->go_neg_req_sent = 0; |
1964 | 0 | peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE; |
1965 | 0 | peer->wps_method = WPS_NOT_READY; |
1966 | 0 | peer->oob_pw_id = 0; |
1967 | 0 | wpabuf_free(peer->go_neg_conf); |
1968 | 0 | peer->go_neg_conf = NULL; |
1969 | |
|
1970 | | #ifdef CONFIG_PASN |
1971 | | if (peer->p2p2 && peer->pasn) { |
1972 | | res.p2p2 = peer->p2p2; |
1973 | | res.akmp = peer->pasn->akmp; |
1974 | | res.cipher = peer->pasn->cipher; |
1975 | | |
1976 | | if (res.akmp == WPA_KEY_MGMT_PASN) { |
1977 | | if (go) { |
1978 | | os_strlcpy(res.sae_password, |
1979 | | p2p->dev_sae_password, |
1980 | | sizeof(res.sae_password)); |
1981 | | } else { |
1982 | | if (!os_strlen(p2p->peer_sae_password)) { |
1983 | | p2p_dbg(p2p, "No password from peer GO for P2P2 group formation"); |
1984 | | return; |
1985 | | } |
1986 | | os_strlcpy(res.sae_password, |
1987 | | p2p->peer_sae_password, |
1988 | | sizeof(res.sae_password)); |
1989 | | } |
1990 | | } else if (res.akmp == WPA_KEY_MGMT_SAE) { |
1991 | | if (peer->role == P2P_ROLE_PAIRING_INITIATOR) { |
1992 | | pasn_initiator_pmksa_cache_get( |
1993 | | peer->pasn->pmksa, |
1994 | | peer->pasn->peer_addr, |
1995 | | res.pmkid, res.pmk, &res.pmk_len); |
1996 | | } else { |
1997 | | pasn_responder_pmksa_cache_get( |
1998 | | peer->pasn->pmksa, |
1999 | | peer->pasn->peer_addr, |
2000 | | res.pmkid, res.pmk, &res.pmk_len); |
2001 | | } |
2002 | | } |
2003 | | |
2004 | | os_memset(p2p->dev_sae_password, 0, |
2005 | | sizeof(p2p->dev_sae_password)); |
2006 | | os_memset(p2p->peer_sae_password, 0, |
2007 | | sizeof(p2p->peer_sae_password)); |
2008 | | wpa_pasn_reset(peer->pasn); |
2009 | | } |
2010 | | #endif /* CONFIG_PASN */ |
2011 | |
|
2012 | 0 | if (p2p->go_role && peer->p2p2) { |
2013 | 0 | p2p_set_state(p2p, P2P_IDLE); |
2014 | 0 | p2p->cfg->set_go_security_config(p2p->cfg->cb_ctx, &res); |
2015 | 0 | p2p->go_role = false; |
2016 | 0 | } else { |
2017 | 0 | p2p_set_state(p2p, P2P_PROVISIONING); |
2018 | 0 | p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); |
2019 | 0 | } |
2020 | |
|
2021 | 0 | forced_memzero(&res, sizeof(res)); |
2022 | 0 | } |
2023 | | |
2024 | | |
2025 | | static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa, |
2026 | | const u8 *data, size_t len, int rx_freq) |
2027 | 2.17k | { |
2028 | 2.17k | p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa)); |
2029 | 2.17k | wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len); |
2030 | | |
2031 | 2.17k | if (len < 1) |
2032 | 1 | return; |
2033 | | |
2034 | 2.16k | switch (data[0]) { |
2035 | 108 | case P2P_GO_NEG_REQ: |
2036 | 108 | p2p_handle_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq); |
2037 | 108 | break; |
2038 | 2 | case P2P_GO_NEG_RESP: |
2039 | 2 | p2p_handle_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq); |
2040 | 2 | break; |
2041 | 3 | case P2P_GO_NEG_CONF: |
2042 | 3 | p2p_handle_go_neg_conf(p2p, sa, data + 1, len - 1, false); |
2043 | 3 | break; |
2044 | 949 | case P2P_INVITATION_REQ: |
2045 | 949 | p2p_handle_invitation_req(p2p, sa, data + 1, len - 1, rx_freq); |
2046 | 949 | break; |
2047 | 3 | case P2P_INVITATION_RESP: |
2048 | 3 | p2p_process_invitation_resp(p2p, sa, data + 1, len - 1); |
2049 | 3 | break; |
2050 | 1.04k | case P2P_PROV_DISC_REQ: |
2051 | 1.04k | p2p_handle_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq); |
2052 | 1.04k | break; |
2053 | 28 | case P2P_PROV_DISC_RESP: |
2054 | 28 | p2p_handle_prov_disc_resp(p2p, sa, data + 1, len - 1, rx_freq); |
2055 | 28 | break; |
2056 | 31 | case P2P_DEV_DISC_REQ: |
2057 | 31 | p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq); |
2058 | 31 | break; |
2059 | 1 | case P2P_DEV_DISC_RESP: |
2060 | 1 | p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1); |
2061 | 1 | break; |
2062 | 3 | default: |
2063 | 3 | p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d", |
2064 | 3 | data[0]); |
2065 | 3 | break; |
2066 | 2.16k | } |
2067 | 2.16k | } |
2068 | | |
2069 | | |
2070 | | static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, |
2071 | | const u8 *sa, const u8 *bssid, const u8 *data, |
2072 | | size_t len, int freq) |
2073 | 2.26k | { |
2074 | 2.26k | if (len < 1) |
2075 | 5 | return; |
2076 | | |
2077 | 2.25k | switch (data[0]) { |
2078 | 2.19k | case WLAN_PA_VENDOR_SPECIFIC: |
2079 | 2.19k | data++; |
2080 | 2.19k | len--; |
2081 | 2.19k | if (len < 4) |
2082 | 4 | return; |
2083 | 2.19k | if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE) |
2084 | 24 | return; |
2085 | | |
2086 | 2.17k | data += 4; |
2087 | 2.17k | len -= 4; |
2088 | | |
2089 | 2.17k | p2p_rx_p2p_action(p2p, sa, data, len, freq); |
2090 | 2.17k | break; |
2091 | 1 | case WLAN_PA_GAS_INITIAL_REQ: |
2092 | 1 | p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq); |
2093 | 1 | break; |
2094 | 1 | case WLAN_PA_GAS_INITIAL_RESP: |
2095 | 1 | p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq); |
2096 | 1 | break; |
2097 | 33 | case WLAN_PA_GAS_COMEBACK_REQ: |
2098 | 33 | p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq); |
2099 | 33 | break; |
2100 | 1 | case WLAN_PA_GAS_COMEBACK_RESP: |
2101 | 1 | p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq); |
2102 | 1 | break; |
2103 | 2.25k | } |
2104 | 2.25k | } |
2105 | | |
2106 | | |
2107 | | void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa, |
2108 | | const u8 *bssid, u8 category, |
2109 | | const u8 *data, size_t len, int freq) |
2110 | 4.44k | { |
2111 | 4.44k | if (category == WLAN_ACTION_PUBLIC) { |
2112 | 2.26k | p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq); |
2113 | 2.26k | return; |
2114 | 2.26k | } |
2115 | | |
2116 | 2.17k | if (category != WLAN_ACTION_VENDOR_SPECIFIC) |
2117 | 1.52k | return; |
2118 | | |
2119 | 656 | if (len < 4) |
2120 | 3 | return; |
2121 | | |
2122 | 653 | if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE) |
2123 | 56 | return; |
2124 | 597 | data += 4; |
2125 | 597 | len -= 4; |
2126 | | |
2127 | | /* P2P action frame */ |
2128 | 597 | p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa)); |
2129 | 597 | wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len); |
2130 | | |
2131 | 597 | if (len < 1) |
2132 | 1 | return; |
2133 | 596 | switch (data[0]) { |
2134 | 2 | case P2P_NOA: |
2135 | 2 | p2p_dbg(p2p, "Received P2P Action - Notice of Absence"); |
2136 | | /* TODO */ |
2137 | 2 | break; |
2138 | 1 | case P2P_PRESENCE_REQ: |
2139 | 1 | p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq); |
2140 | 1 | break; |
2141 | 588 | case P2P_PRESENCE_RESP: |
2142 | 588 | p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1); |
2143 | 588 | break; |
2144 | 2 | case P2P_GO_DISC_REQ: |
2145 | 2 | p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq); |
2146 | 2 | break; |
2147 | 3 | default: |
2148 | 3 | p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]); |
2149 | 3 | break; |
2150 | 596 | } |
2151 | 596 | } |
2152 | | |
2153 | | |
2154 | | static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx) |
2155 | 0 | { |
2156 | 0 | struct p2p_data *p2p = eloop_ctx; |
2157 | 0 | if (p2p->go_neg_peer == NULL) |
2158 | 0 | return; |
2159 | 0 | if (p2p->pending_listen_freq) { |
2160 | 0 | p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start"); |
2161 | 0 | p2p->pending_listen_freq = 0; |
2162 | 0 | } |
2163 | 0 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
2164 | 0 | p2p->pending_listen_wait_drv = false; |
2165 | 0 | p2p->go_neg_peer->status = P2P_SC_SUCCESS; |
2166 | | /* |
2167 | | * Set new timeout to make sure a previously set one does not expire |
2168 | | * too quickly while waiting for the GO Negotiation to complete. |
2169 | | */ |
2170 | 0 | p2p_set_timeout(p2p, 0, 500000); |
2171 | 0 | p2p_connect_send(p2p, p2p->go_neg_peer); |
2172 | 0 | } |
2173 | | |
2174 | | |
2175 | | static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx) |
2176 | 0 | { |
2177 | 0 | struct p2p_data *p2p = eloop_ctx; |
2178 | 0 | if (p2p->invite_peer == NULL) |
2179 | 0 | return; |
2180 | 0 | if (p2p->pending_listen_freq) { |
2181 | 0 | p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start"); |
2182 | 0 | p2p->pending_listen_freq = 0; |
2183 | 0 | } |
2184 | 0 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
2185 | 0 | p2p->pending_listen_wait_drv = false; |
2186 | 0 | p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr, |
2187 | 0 | p2p->invite_dev_pw_id); |
2188 | 0 | } |
2189 | | |
2190 | | |
2191 | | static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr, |
2192 | | const u8 *ie, size_t ie_len) |
2193 | 6.57k | { |
2194 | 6.57k | struct p2p_message msg; |
2195 | 6.57k | struct p2p_device *dev; |
2196 | | |
2197 | 6.57k | os_memset(&msg, 0, sizeof(msg)); |
2198 | 6.57k | if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL) |
2199 | 4.68k | { |
2200 | 4.68k | p2p_parse_free(&msg); |
2201 | 4.68k | return; /* not a P2P probe */ |
2202 | 4.68k | } |
2203 | | |
2204 | 1.88k | if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN || |
2205 | 607 | os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) |
2206 | 1.37k | != 0) { |
2207 | | /* The Probe Request is not part of P2P Device Discovery. It is |
2208 | | * not known whether the source address of the frame is the P2P |
2209 | | * Device Address or P2P Interface Address. Do not add a new |
2210 | | * peer entry based on this frames. |
2211 | | */ |
2212 | 1.37k | p2p_parse_free(&msg); |
2213 | 1.37k | return; |
2214 | 1.37k | } |
2215 | | |
2216 | 511 | dev = p2p_get_device(p2p, addr); |
2217 | 511 | if (dev) { |
2218 | 104 | if (msg.listen_channel) { |
2219 | 70 | int freq; |
2220 | | |
2221 | 70 | if (dev->country[0] == 0) |
2222 | 70 | os_memcpy(dev->country, msg.listen_channel, 3); |
2223 | | |
2224 | 70 | freq = p2p_channel_to_freq(msg.listen_channel[3], |
2225 | 70 | msg.listen_channel[4]); |
2226 | | |
2227 | 70 | if (freq > 0 && dev->listen_freq != freq) { |
2228 | 47 | p2p_dbg(p2p, |
2229 | 47 | "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz", |
2230 | 47 | MAC2STR(addr), dev->listen_freq, freq); |
2231 | 47 | dev->listen_freq = freq; |
2232 | 47 | } |
2233 | 70 | } |
2234 | | |
2235 | 104 | p2p_update_peer_6ghz_capab(dev, &msg); |
2236 | 104 | os_get_reltime(&dev->last_seen); |
2237 | 104 | p2p_parse_free(&msg); |
2238 | 104 | return; /* already known */ |
2239 | 104 | } |
2240 | | |
2241 | 407 | dev = p2p_create_device(p2p, addr); |
2242 | 407 | if (dev == NULL) { |
2243 | 0 | p2p_parse_free(&msg); |
2244 | 0 | return; |
2245 | 0 | } |
2246 | | |
2247 | 407 | os_get_reltime(&dev->last_seen); |
2248 | 407 | dev->flags |= P2P_DEV_PROBE_REQ_ONLY; |
2249 | | |
2250 | 407 | if (msg.listen_channel) { |
2251 | 326 | os_memcpy(dev->country, msg.listen_channel, 3); |
2252 | 326 | dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3], |
2253 | 326 | msg.listen_channel[4]); |
2254 | 326 | } |
2255 | | |
2256 | 407 | p2p_copy_wps_info(p2p, dev, 1, &msg); |
2257 | | |
2258 | 407 | if (msg.wfd_subelems) { |
2259 | 32 | wpabuf_free(dev->info.wfd_subelems); |
2260 | 32 | dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems); |
2261 | 32 | } |
2262 | | |
2263 | 407 | p2p_parse_free(&msg); |
2264 | | |
2265 | 407 | p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR |
2266 | 407 | " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d", |
2267 | 407 | MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab, |
2268 | 407 | dev->info.group_capab, dev->info.device_name, |
2269 | 407 | dev->listen_freq); |
2270 | 407 | } |
2271 | | |
2272 | | |
2273 | | struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p, |
2274 | | const u8 *addr, |
2275 | | struct p2p_message *msg) |
2276 | 16 | { |
2277 | 16 | struct p2p_device *dev; |
2278 | | |
2279 | 16 | dev = p2p_get_device(p2p, addr); |
2280 | 16 | if (dev) { |
2281 | 0 | os_get_reltime(&dev->last_seen); |
2282 | 0 | return dev; /* already known */ |
2283 | 0 | } |
2284 | | |
2285 | 16 | dev = p2p_create_device(p2p, addr); |
2286 | 16 | if (dev == NULL) |
2287 | 0 | return NULL; |
2288 | | |
2289 | 16 | p2p_add_dev_info(p2p, addr, dev, msg); |
2290 | | |
2291 | 16 | return dev; |
2292 | 16 | } |
2293 | | |
2294 | | |
2295 | | static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type) |
2296 | 0 | { |
2297 | 0 | if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0) |
2298 | 0 | return 1; |
2299 | 0 | if (os_memcmp(dev_type, req_dev_type, 2) == 0 && |
2300 | 0 | WPA_GET_BE32(&req_dev_type[2]) == 0 && |
2301 | 0 | WPA_GET_BE16(&req_dev_type[6]) == 0) |
2302 | 0 | return 1; /* Category match with wildcard OUI/sub-category */ |
2303 | 0 | return 0; |
2304 | 0 | } |
2305 | | |
2306 | | |
2307 | | int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[], |
2308 | | size_t num_req_dev_type) |
2309 | 0 | { |
2310 | 0 | size_t i; |
2311 | 0 | for (i = 0; i < num_req_dev_type; i++) { |
2312 | 0 | if (dev_type_match(dev_type, req_dev_type[i])) |
2313 | 0 | return 1; |
2314 | 0 | } |
2315 | 0 | return 0; |
2316 | 0 | } |
2317 | | |
2318 | | |
2319 | | /** |
2320 | | * p2p_match_dev_type - Match local device type with requested type |
2321 | | * @p2p: P2P module context from p2p_init() |
2322 | | * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs) |
2323 | | * Returns: 1 on match, 0 on mismatch |
2324 | | * |
2325 | | * This function can be used to match the Requested Device Type attribute in |
2326 | | * WPS IE with the local device types for deciding whether to reply to a Probe |
2327 | | * Request frame. |
2328 | | */ |
2329 | | int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps) |
2330 | 0 | { |
2331 | 0 | struct wps_parse_attr attr; |
2332 | 0 | size_t i; |
2333 | |
|
2334 | 0 | if (wps_parse_msg(wps, &attr)) |
2335 | 0 | return 1; /* assume no Requested Device Type attributes */ |
2336 | | |
2337 | 0 | if (attr.num_req_dev_type == 0) |
2338 | 0 | return 1; /* no Requested Device Type attributes -> match */ |
2339 | | |
2340 | 0 | if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type, |
2341 | 0 | attr.num_req_dev_type)) |
2342 | 0 | return 1; /* Own Primary Device Type matches */ |
2343 | | |
2344 | 0 | for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) { |
2345 | 0 | if (dev_type_list_match(p2p->cfg->sec_dev_type[i], |
2346 | 0 | attr.req_dev_type, |
2347 | 0 | attr.num_req_dev_type)) |
2348 | 0 | return 1; /* Own Secondary Device Type matches */ |
2349 | 0 | } |
2350 | | |
2351 | | /* No matching device type found */ |
2352 | 0 | return 0; |
2353 | 0 | } |
2354 | | |
2355 | | |
2356 | | struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p, |
2357 | | const u8 *query_hash, |
2358 | | u8 query_count) |
2359 | 2 | { |
2360 | 2 | struct wpabuf *buf; |
2361 | 2 | u8 *len; |
2362 | 2 | int pw_id = -1; |
2363 | 2 | size_t extra = 0; |
2364 | | |
2365 | 2 | #ifdef CONFIG_WIFI_DISPLAY |
2366 | 2 | if (p2p->wfd_ie_probe_resp) |
2367 | 0 | extra = wpabuf_len(p2p->wfd_ie_probe_resp); |
2368 | 2 | #endif /* CONFIG_WIFI_DISPLAY */ |
2369 | | |
2370 | 2 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]) |
2371 | 0 | extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]); |
2372 | | |
2373 | 2 | if (query_count) |
2374 | 0 | extra += MAX_SVC_ADV_IE_LEN; |
2375 | | |
2376 | 2 | buf = wpabuf_alloc(1000 + extra); |
2377 | 2 | if (buf == NULL) |
2378 | 0 | return NULL; |
2379 | | |
2380 | 2 | if (p2p->go_neg_peer) { |
2381 | | /* Advertise immediate availability of WPS credential */ |
2382 | 0 | pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method); |
2383 | 0 | } |
2384 | | |
2385 | 2 | if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) { |
2386 | 0 | p2p_dbg(p2p, "Failed to build WPS IE for Probe Response"); |
2387 | 0 | wpabuf_free(buf); |
2388 | 0 | return NULL; |
2389 | 0 | } |
2390 | | |
2391 | 2 | #ifdef CONFIG_WIFI_DISPLAY |
2392 | 2 | if (p2p->wfd_ie_probe_resp) |
2393 | 0 | wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp); |
2394 | 2 | #endif /* CONFIG_WIFI_DISPLAY */ |
2395 | | |
2396 | 2 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]) |
2397 | 0 | wpabuf_put_buf(buf, |
2398 | 0 | p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]); |
2399 | | |
2400 | | /* P2P IE */ |
2401 | 2 | len = p2p_buf_add_ie_hdr(buf); |
2402 | 2 | p2p_buf_add_capability(buf, p2p->dev_capab & |
2403 | 2 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); |
2404 | 2 | if (p2p->ext_listen_interval) |
2405 | 0 | p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period, |
2406 | 0 | p2p->ext_listen_interval); |
2407 | 2 | p2p_buf_add_device_info(buf, p2p, NULL); |
2408 | 2 | p2p_buf_update_ie_hdr(buf, len); |
2409 | | |
2410 | 2 | if (query_count) { |
2411 | 0 | p2p_buf_add_service_instance(buf, p2p, query_count, query_hash, |
2412 | 0 | p2p->p2ps_adv_list); |
2413 | 0 | } |
2414 | | |
2415 | 2 | return buf; |
2416 | 2 | } |
2417 | | |
2418 | | static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf, |
2419 | | struct wpabuf *ies, |
2420 | | const u8 *addr, int rx_freq) |
2421 | 0 | { |
2422 | 0 | struct ieee80211_mgmt *resp; |
2423 | 0 | u8 channel, op_class; |
2424 | |
|
2425 | 0 | resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt, |
2426 | 0 | u.probe_resp.variable)); |
2427 | |
|
2428 | 0 | resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) | |
2429 | 0 | (WLAN_FC_STYPE_PROBE_RESP << 4)); |
2430 | 0 | os_memcpy(resp->da, addr, ETH_ALEN); |
2431 | 0 | os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN); |
2432 | 0 | os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN); |
2433 | 0 | resp->u.probe_resp.beacon_int = host_to_le16(100); |
2434 | | /* hardware or low-level driver will setup seq_ctrl and timestamp */ |
2435 | 0 | resp->u.probe_resp.capab_info = |
2436 | 0 | host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE | |
2437 | 0 | WLAN_CAPABILITY_PRIVACY | |
2438 | 0 | WLAN_CAPABILITY_SHORT_SLOT_TIME); |
2439 | |
|
2440 | 0 | wpabuf_put_u8(buf, WLAN_EID_SSID); |
2441 | 0 | wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN); |
2442 | 0 | wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); |
2443 | |
|
2444 | 0 | wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES); |
2445 | 0 | wpabuf_put_u8(buf, 8); |
2446 | 0 | wpabuf_put_u8(buf, (60 / 5) | 0x80); |
2447 | 0 | wpabuf_put_u8(buf, 90 / 5); |
2448 | 0 | wpabuf_put_u8(buf, (120 / 5) | 0x80); |
2449 | 0 | wpabuf_put_u8(buf, 180 / 5); |
2450 | 0 | wpabuf_put_u8(buf, (240 / 5) | 0x80); |
2451 | 0 | wpabuf_put_u8(buf, 360 / 5); |
2452 | 0 | wpabuf_put_u8(buf, 480 / 5); |
2453 | 0 | wpabuf_put_u8(buf, 540 / 5); |
2454 | |
|
2455 | 0 | if (!rx_freq) { |
2456 | 0 | channel = p2p->cfg->channel; |
2457 | 0 | } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) { |
2458 | 0 | p2p_err(p2p, "Failed to convert freq to channel"); |
2459 | 0 | return -1; |
2460 | 0 | } |
2461 | | |
2462 | 0 | wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS); |
2463 | 0 | wpabuf_put_u8(buf, 1); |
2464 | 0 | wpabuf_put_u8(buf, channel); |
2465 | |
|
2466 | 0 | wpabuf_put_buf(buf, ies); |
2467 | |
|
2468 | 0 | return 0; |
2469 | 0 | } |
2470 | | |
2471 | | static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash) |
2472 | 0 | { |
2473 | 0 | struct p2ps_advertisement *adv_data; |
2474 | 0 | int any_wfa; |
2475 | |
|
2476 | 0 | p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list); |
2477 | | |
2478 | | /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */ |
2479 | 0 | any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0; |
2480 | |
|
2481 | 0 | adv_data = p2p->p2ps_adv_list; |
2482 | 0 | while (adv_data) { |
2483 | 0 | if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0) |
2484 | 0 | return 1; /* exact hash match */ |
2485 | 0 | if (any_wfa && |
2486 | 0 | os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR, |
2487 | 0 | os_strlen(P2PS_WILD_HASH_STR)) == 0) |
2488 | 0 | return 1; /* WFA service match */ |
2489 | 0 | adv_data = adv_data->next; |
2490 | 0 | } |
2491 | | |
2492 | 0 | return 0; |
2493 | 0 | } |
2494 | | |
2495 | | |
2496 | | static enum p2p_probe_req_status |
2497 | | p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst, |
2498 | | const u8 *bssid, const u8 *ie, size_t ie_len, |
2499 | | unsigned int rx_freq) |
2500 | 6.57k | { |
2501 | 6.57k | struct ieee802_11_elems elems; |
2502 | 6.57k | struct wpabuf *buf; |
2503 | 6.57k | struct p2p_message msg; |
2504 | 6.57k | struct wpabuf *ies; |
2505 | | |
2506 | 6.57k | if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) == |
2507 | 6.57k | ParseFailed) { |
2508 | | /* Ignore invalid Probe Request frames */ |
2509 | 985 | p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it"); |
2510 | 985 | return P2P_PREQ_MALFORMED; |
2511 | 985 | } |
2512 | | |
2513 | 5.58k | if (elems.p2p == NULL) { |
2514 | | /* not a P2P probe - ignore it */ |
2515 | 3.05k | p2p_dbg(p2p, "Not a P2P probe - ignore it"); |
2516 | 3.05k | return P2P_PREQ_NOT_P2P; |
2517 | 3.05k | } |
2518 | | |
2519 | 2.53k | if (dst && !is_broadcast_ether_addr(dst) && |
2520 | 2.53k | !ether_addr_equal(dst, p2p->cfg->dev_addr)) { |
2521 | | /* Not sent to the broadcast address or our P2P Device Address |
2522 | | */ |
2523 | 2.53k | p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it", |
2524 | 2.53k | MAC2STR(dst)); |
2525 | 2.53k | return P2P_PREQ_NOT_PROCESSED; |
2526 | 2.53k | } |
2527 | | |
2528 | 0 | if (bssid && !is_broadcast_ether_addr(bssid)) { |
2529 | | /* Not sent to the Wildcard BSSID */ |
2530 | 0 | p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it", |
2531 | 0 | MAC2STR(bssid)); |
2532 | 0 | return P2P_PREQ_NOT_PROCESSED; |
2533 | 0 | } |
2534 | | |
2535 | 0 | if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN || |
2536 | 0 | os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) != |
2537 | 0 | 0) { |
2538 | | /* not using P2P Wildcard SSID - ignore */ |
2539 | 0 | p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it"); |
2540 | 0 | return P2P_PREQ_NOT_PROCESSED; |
2541 | 0 | } |
2542 | | |
2543 | 0 | if (supp_rates_11b_only(&elems)) { |
2544 | | /* Indicates support for 11b rates only */ |
2545 | 0 | p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it"); |
2546 | 0 | return P2P_PREQ_NOT_P2P; |
2547 | 0 | } |
2548 | | |
2549 | 0 | os_memset(&msg, 0, sizeof(msg)); |
2550 | 0 | if (p2p_parse_ies(ie, ie_len, &msg) < 0) { |
2551 | | /* Could not parse P2P attributes */ |
2552 | 0 | p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it"); |
2553 | 0 | return P2P_PREQ_NOT_P2P; |
2554 | 0 | } |
2555 | | |
2556 | 0 | if (msg.service_hash && msg.service_hash_count) { |
2557 | 0 | const u8 *hash = msg.service_hash; |
2558 | 0 | u8 i; |
2559 | 0 | int p2ps_svc_found = 0; |
2560 | |
|
2561 | 0 | p2p_dbg(p2p, "in_listen=%d drv_in_listen=%d when received P2PS Probe Request at %u MHz; own Listen channel %u, pending listen freq %u MHz", |
2562 | 0 | p2p->in_listen, p2p->drv_in_listen, rx_freq, |
2563 | 0 | p2p->cfg->channel, p2p->pending_listen_freq); |
2564 | |
|
2565 | 0 | if (!p2p->in_listen && !p2p->drv_in_listen && |
2566 | 0 | p2p->pending_listen_freq && rx_freq && |
2567 | 0 | rx_freq != p2p->pending_listen_freq) { |
2568 | 0 | p2p_dbg(p2p, "Do not reply to Probe Request frame that was received on %u MHz while waiting to start Listen state on %u MHz", |
2569 | 0 | rx_freq, p2p->pending_listen_freq); |
2570 | 0 | p2p_parse_free(&msg); |
2571 | 0 | return P2P_PREQ_NOT_LISTEN; |
2572 | 0 | } |
2573 | | |
2574 | 0 | for (i = 0; i < msg.service_hash_count; i++) { |
2575 | 0 | if (p2p_service_find_asp(p2p, hash)) { |
2576 | 0 | p2p_dbg(p2p, "Service Hash match found: " |
2577 | 0 | MACSTR, MAC2STR(hash)); |
2578 | 0 | p2ps_svc_found = 1; |
2579 | 0 | break; |
2580 | 0 | } |
2581 | 0 | hash += P2PS_HASH_LEN; |
2582 | 0 | } |
2583 | | |
2584 | | /* Probed hash unknown */ |
2585 | 0 | if (!p2ps_svc_found) { |
2586 | 0 | p2p_dbg(p2p, "No Service Hash match found"); |
2587 | 0 | p2p_parse_free(&msg); |
2588 | 0 | return P2P_PREQ_NOT_PROCESSED; |
2589 | 0 | } |
2590 | 0 | } else { |
2591 | | /* This is not a P2PS Probe Request */ |
2592 | 0 | p2p_dbg(p2p, "No P2PS Hash in Probe Request"); |
2593 | |
|
2594 | 0 | if (!p2p->in_listen || !p2p->drv_in_listen) { |
2595 | | /* not in Listen state - ignore Probe Request */ |
2596 | 0 | p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request", |
2597 | 0 | p2p->in_listen, p2p->drv_in_listen); |
2598 | 0 | p2p_parse_free(&msg); |
2599 | 0 | return P2P_PREQ_NOT_LISTEN; |
2600 | 0 | } |
2601 | 0 | } |
2602 | | |
2603 | 0 | if (msg.device_id && |
2604 | 0 | !ether_addr_equal(msg.device_id, p2p->cfg->dev_addr)) { |
2605 | | /* Device ID did not match */ |
2606 | 0 | p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it", |
2607 | 0 | MAC2STR(msg.device_id)); |
2608 | 0 | p2p_parse_free(&msg); |
2609 | 0 | return P2P_PREQ_NOT_PROCESSED; |
2610 | 0 | } |
2611 | | |
2612 | | /* Check Requested Device Type match */ |
2613 | 0 | if (msg.wps_attributes && |
2614 | 0 | !p2p_match_dev_type(p2p, msg.wps_attributes)) { |
2615 | | /* No match with Requested Device Type */ |
2616 | 0 | p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it"); |
2617 | 0 | p2p_parse_free(&msg); |
2618 | 0 | return P2P_PREQ_NOT_PROCESSED; |
2619 | 0 | } |
2620 | | |
2621 | 0 | if (!p2p->cfg->send_probe_resp) { |
2622 | | /* Response generated elsewhere */ |
2623 | 0 | p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response"); |
2624 | 0 | p2p_parse_free(&msg); |
2625 | 0 | return P2P_PREQ_NOT_PROCESSED; |
2626 | 0 | } |
2627 | | |
2628 | 0 | p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state"); |
2629 | | |
2630 | | /* |
2631 | | * We do not really have a specific BSS that this frame is advertising, |
2632 | | * so build a frame that has some information in valid format. This is |
2633 | | * really only used for discovery purposes, not to learn exact BSS |
2634 | | * parameters. |
2635 | | */ |
2636 | 0 | ies = p2p_build_probe_resp_ies(p2p, msg.service_hash, |
2637 | 0 | msg.service_hash_count); |
2638 | 0 | p2p_parse_free(&msg); |
2639 | 0 | if (ies == NULL) |
2640 | 0 | return P2P_PREQ_NOT_PROCESSED; |
2641 | | |
2642 | 0 | buf = wpabuf_alloc(200 + wpabuf_len(ies)); |
2643 | 0 | if (buf == NULL) { |
2644 | 0 | wpabuf_free(ies); |
2645 | 0 | return P2P_PREQ_NOT_PROCESSED; |
2646 | 0 | } |
2647 | | |
2648 | 0 | if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) { |
2649 | 0 | wpabuf_free(ies); |
2650 | 0 | wpabuf_free(buf); |
2651 | 0 | return P2P_PREQ_NOT_PROCESSED; |
2652 | 0 | } |
2653 | | |
2654 | 0 | wpabuf_free(ies); |
2655 | |
|
2656 | 0 | p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq); |
2657 | |
|
2658 | 0 | wpabuf_free(buf); |
2659 | |
|
2660 | 0 | return P2P_PREQ_PROCESSED; |
2661 | 0 | } |
2662 | | |
2663 | | |
2664 | | enum p2p_probe_req_status |
2665 | | p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst, |
2666 | | const u8 *bssid, const u8 *ie, size_t ie_len, |
2667 | | unsigned int rx_freq, int p2p_lo_started) |
2668 | 6.57k | { |
2669 | 6.57k | enum p2p_probe_req_status res; |
2670 | | |
2671 | 6.57k | p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len); |
2672 | | |
2673 | 6.57k | if (p2p_lo_started) { |
2674 | 0 | p2p_dbg(p2p, |
2675 | 0 | "Probe Response is offloaded, do not reply Probe Request"); |
2676 | 0 | return P2P_PREQ_PROCESSED; |
2677 | 0 | } |
2678 | | |
2679 | 6.57k | res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq); |
2680 | 6.57k | if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED) |
2681 | 4.04k | return res; |
2682 | | |
2683 | | /* |
2684 | | * Activate a pending GO Negotiation/Invite flow if a received Probe |
2685 | | * Request frame is from an expected peer. Some devices may share the |
2686 | | * same address for P2P and non-P2P STA running simultaneously. The |
2687 | | * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe() |
2688 | | * return values verified above ensure we are handling a Probe Request |
2689 | | * frame from a P2P peer. |
2690 | | */ |
2691 | 2.53k | if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) && |
2692 | 0 | p2p->go_neg_peer && |
2693 | 0 | ether_addr_equal(addr, p2p->go_neg_peer->info.p2p_device_addr) && |
2694 | 0 | !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) { |
2695 | | /* Received a Probe Request from GO Negotiation peer */ |
2696 | 0 | p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout"); |
2697 | 0 | eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL); |
2698 | 0 | eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL); |
2699 | 0 | return res; |
2700 | 0 | } |
2701 | | |
2702 | 2.53k | if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) && |
2703 | 0 | p2p->invite_peer && |
2704 | 0 | (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) && |
2705 | 0 | ether_addr_equal(addr, p2p->invite_peer->info.p2p_device_addr)) { |
2706 | | /* Received a Probe Request from Invite peer */ |
2707 | 0 | p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout"); |
2708 | 0 | eloop_cancel_timeout(p2p_invite_start, p2p, NULL); |
2709 | 0 | eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL); |
2710 | 0 | return res; |
2711 | 0 | } |
2712 | | |
2713 | 2.53k | return res; |
2714 | 2.53k | } |
2715 | | |
2716 | | |
2717 | | static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid, |
2718 | | u8 *buf, size_t len, struct wpabuf *p2p_ie) |
2719 | 0 | { |
2720 | 0 | struct wpabuf *tmp; |
2721 | 0 | u8 *lpos; |
2722 | 0 | size_t tmplen; |
2723 | 0 | int res; |
2724 | 0 | u8 group_capab; |
2725 | 0 | struct p2p_message msg; |
2726 | |
|
2727 | 0 | if (p2p_ie == NULL) |
2728 | 0 | return 0; /* WLAN AP is not a P2P manager */ |
2729 | | |
2730 | 0 | os_memset(&msg, 0, sizeof(msg)); |
2731 | 0 | if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0) |
2732 | 0 | return 0; |
2733 | | |
2734 | 0 | p2p_dbg(p2p, "BSS P2P manageability %s", |
2735 | 0 | msg.manageability ? "enabled" : "disabled"); |
2736 | |
|
2737 | 0 | if (!msg.manageability) |
2738 | 0 | return 0; |
2739 | | |
2740 | | /* |
2741 | | * (Re)Association Request - P2P IE |
2742 | | * P2P Capability attribute (shall be present) |
2743 | | * P2P Interface attribute (present if concurrent device and |
2744 | | * P2P Management is enabled) |
2745 | | */ |
2746 | 0 | tmp = wpabuf_alloc(200); |
2747 | 0 | if (tmp == NULL) |
2748 | 0 | return -1; |
2749 | | |
2750 | 0 | lpos = p2p_buf_add_ie_hdr(tmp); |
2751 | 0 | group_capab = 0; |
2752 | 0 | if (p2p->num_groups > 0) { |
2753 | 0 | group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER; |
2754 | 0 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && |
2755 | 0 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) && |
2756 | 0 | p2p->cross_connect) |
2757 | 0 | group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; |
2758 | 0 | } |
2759 | 0 | p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab); |
2760 | 0 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && |
2761 | 0 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED)) |
2762 | 0 | p2p_buf_add_p2p_interface(tmp, p2p); |
2763 | 0 | p2p_buf_update_ie_hdr(tmp, lpos); |
2764 | |
|
2765 | 0 | tmplen = wpabuf_len(tmp); |
2766 | 0 | if (tmplen > len) |
2767 | 0 | res = -1; |
2768 | 0 | else { |
2769 | 0 | os_memcpy(buf, wpabuf_head(tmp), tmplen); |
2770 | 0 | res = tmplen; |
2771 | 0 | } |
2772 | 0 | wpabuf_free(tmp); |
2773 | |
|
2774 | 0 | return res; |
2775 | 0 | } |
2776 | | |
2777 | | |
2778 | | int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf, |
2779 | | size_t len, int p2p_group, struct wpabuf *p2p_ie) |
2780 | 0 | { |
2781 | 0 | struct wpabuf *tmp; |
2782 | 0 | u8 *lpos; |
2783 | 0 | struct p2p_device *peer; |
2784 | 0 | size_t tmplen; |
2785 | 0 | int res; |
2786 | 0 | size_t extra = 0; |
2787 | |
|
2788 | 0 | if (!p2p_group) |
2789 | 0 | return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie); |
2790 | | |
2791 | 0 | #ifdef CONFIG_WIFI_DISPLAY |
2792 | 0 | if (p2p->wfd_ie_assoc_req) |
2793 | 0 | extra = wpabuf_len(p2p->wfd_ie_assoc_req); |
2794 | 0 | #endif /* CONFIG_WIFI_DISPLAY */ |
2795 | |
|
2796 | 0 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]) |
2797 | 0 | extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]); |
2798 | | |
2799 | | /* |
2800 | | * (Re)Association Request - P2P IE |
2801 | | * P2P Capability attribute (shall be present) |
2802 | | * Extended Listen Timing (may be present) |
2803 | | * P2P Device Info attribute (shall be present) |
2804 | | */ |
2805 | 0 | tmp = wpabuf_alloc(200 + extra); |
2806 | 0 | if (tmp == NULL) |
2807 | 0 | return -1; |
2808 | | |
2809 | 0 | #ifdef CONFIG_WIFI_DISPLAY |
2810 | 0 | if (p2p->wfd_ie_assoc_req) |
2811 | 0 | wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req); |
2812 | 0 | #endif /* CONFIG_WIFI_DISPLAY */ |
2813 | |
|
2814 | 0 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]) |
2815 | 0 | wpabuf_put_buf(tmp, |
2816 | 0 | p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]); |
2817 | |
|
2818 | 0 | peer = bssid ? p2p_get_device(p2p, bssid) : NULL; |
2819 | |
|
2820 | 0 | lpos = p2p_buf_add_ie_hdr(tmp); |
2821 | 0 | p2p_buf_add_capability(tmp, p2p->dev_capab, 0); |
2822 | 0 | if (p2p->ext_listen_interval) |
2823 | 0 | p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period, |
2824 | 0 | p2p->ext_listen_interval); |
2825 | 0 | p2p_buf_add_device_info(tmp, p2p, peer); |
2826 | 0 | p2p_buf_update_ie_hdr(tmp, lpos); |
2827 | |
|
2828 | 0 | tmplen = wpabuf_len(tmp); |
2829 | 0 | if (tmplen > len) |
2830 | 0 | res = -1; |
2831 | 0 | else { |
2832 | 0 | os_memcpy(buf, wpabuf_head(tmp), tmplen); |
2833 | 0 | res = tmplen; |
2834 | 0 | } |
2835 | 0 | wpabuf_free(tmp); |
2836 | |
|
2837 | 0 | return res; |
2838 | 0 | } |
2839 | | |
2840 | | |
2841 | | int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end) |
2842 | 0 | { |
2843 | 0 | struct wpabuf *p2p_ie; |
2844 | 0 | int ret; |
2845 | |
|
2846 | 0 | p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE); |
2847 | 0 | if (p2p_ie == NULL) |
2848 | 0 | return 0; |
2849 | | |
2850 | 0 | ret = p2p_attr_text(p2p_ie, buf, end); |
2851 | 0 | wpabuf_free(p2p_ie); |
2852 | 0 | return ret; |
2853 | 0 | } |
2854 | | |
2855 | | |
2856 | | struct p2ps_advertisement * |
2857 | | p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id) |
2858 | 1 | { |
2859 | 1 | struct p2ps_advertisement *adv_data; |
2860 | | |
2861 | 1 | if (!p2p) |
2862 | 0 | return NULL; |
2863 | | |
2864 | 1 | adv_data = p2p->p2ps_adv_list; |
2865 | 1 | while (adv_data) { |
2866 | 0 | if (adv_data->id == adv_id) |
2867 | 0 | return adv_data; |
2868 | 0 | adv_data = adv_data->next; |
2869 | 0 | } |
2870 | | |
2871 | 1 | return NULL; |
2872 | 1 | } |
2873 | | |
2874 | | |
2875 | | int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id) |
2876 | 0 | { |
2877 | 0 | struct p2ps_advertisement *adv_data; |
2878 | 0 | struct p2ps_advertisement **prior; |
2879 | |
|
2880 | 0 | if (!p2p) |
2881 | 0 | return -1; |
2882 | | |
2883 | 0 | adv_data = p2p->p2ps_adv_list; |
2884 | 0 | prior = &p2p->p2ps_adv_list; |
2885 | 0 | while (adv_data) { |
2886 | 0 | if (adv_data->id == adv_id) { |
2887 | 0 | p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id); |
2888 | 0 | *prior = adv_data->next; |
2889 | 0 | os_free(adv_data); |
2890 | 0 | return 0; |
2891 | 0 | } |
2892 | 0 | prior = &adv_data->next; |
2893 | 0 | adv_data = adv_data->next; |
2894 | 0 | } |
2895 | | |
2896 | 0 | return -1; |
2897 | 0 | } |
2898 | | |
2899 | | |
2900 | | int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id, |
2901 | | const char *adv_str, u8 svc_state, u16 config_methods, |
2902 | | const char *svc_info, const u8 *cpt_priority) |
2903 | 0 | { |
2904 | 0 | struct p2ps_advertisement *adv_data, *tmp, **prev; |
2905 | 0 | u8 buf[P2PS_HASH_LEN]; |
2906 | 0 | size_t adv_data_len, adv_len, info_len = 0; |
2907 | 0 | int i; |
2908 | |
|
2909 | 0 | if (!p2p || !adv_str || !adv_str[0] || !cpt_priority) |
2910 | 0 | return -1; |
2911 | | |
2912 | 0 | if (!(config_methods & p2p->cfg->config_methods)) { |
2913 | 0 | p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x", |
2914 | 0 | config_methods, p2p->cfg->config_methods); |
2915 | 0 | return -1; |
2916 | 0 | } |
2917 | | |
2918 | 0 | if (!p2ps_gen_hash(p2p, adv_str, buf)) |
2919 | 0 | return -1; |
2920 | | |
2921 | 0 | if (svc_info) |
2922 | 0 | info_len = os_strlen(svc_info); |
2923 | 0 | adv_len = os_strlen(adv_str); |
2924 | 0 | adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 + |
2925 | 0 | info_len + 1; |
2926 | |
|
2927 | 0 | adv_data = os_zalloc(adv_data_len); |
2928 | 0 | if (!adv_data) |
2929 | 0 | return -1; |
2930 | | |
2931 | 0 | os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN); |
2932 | 0 | adv_data->id = adv_id; |
2933 | 0 | adv_data->state = svc_state; |
2934 | 0 | adv_data->config_methods = config_methods & p2p->cfg->config_methods; |
2935 | 0 | adv_data->auto_accept = (u8) auto_accept; |
2936 | 0 | os_memcpy(adv_data->svc_name, adv_str, adv_len); |
2937 | |
|
2938 | 0 | for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) { |
2939 | 0 | adv_data->cpt_priority[i] = cpt_priority[i]; |
2940 | 0 | adv_data->cpt_mask |= cpt_priority[i]; |
2941 | 0 | } |
2942 | |
|
2943 | 0 | if (svc_info && info_len) { |
2944 | 0 | adv_data->svc_info = &adv_data->svc_name[adv_len + 1]; |
2945 | 0 | os_memcpy(adv_data->svc_info, svc_info, info_len); |
2946 | 0 | } |
2947 | | |
2948 | | /* |
2949 | | * Group Advertisements by service string. They do not need to be |
2950 | | * sorted, but groups allow easier Probe Response instance grouping |
2951 | | */ |
2952 | 0 | tmp = p2p->p2ps_adv_list; |
2953 | 0 | prev = &p2p->p2ps_adv_list; |
2954 | 0 | while (tmp) { |
2955 | 0 | if (tmp->id == adv_data->id) { |
2956 | 0 | if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) { |
2957 | 0 | os_free(adv_data); |
2958 | 0 | return -1; |
2959 | 0 | } |
2960 | 0 | adv_data->next = tmp->next; |
2961 | 0 | *prev = adv_data; |
2962 | 0 | os_free(tmp); |
2963 | 0 | goto inserted; |
2964 | 0 | } else { |
2965 | 0 | if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) { |
2966 | 0 | adv_data->next = tmp->next; |
2967 | 0 | tmp->next = adv_data; |
2968 | 0 | goto inserted; |
2969 | 0 | } |
2970 | 0 | } |
2971 | 0 | prev = &tmp->next; |
2972 | 0 | tmp = tmp->next; |
2973 | 0 | } |
2974 | | |
2975 | | /* No svc_name match found */ |
2976 | 0 | adv_data->next = p2p->p2ps_adv_list; |
2977 | 0 | p2p->p2ps_adv_list = adv_data; |
2978 | |
|
2979 | 0 | inserted: |
2980 | 0 | p2p_dbg(p2p, |
2981 | 0 | "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x", |
2982 | 0 | adv_id, adv_data->config_methods, svc_state, adv_str, |
2983 | 0 | adv_data->cpt_mask); |
2984 | |
|
2985 | 0 | return 0; |
2986 | 0 | } |
2987 | | |
2988 | | |
2989 | | void p2p_service_flush_asp(struct p2p_data *p2p) |
2990 | 6.57k | { |
2991 | 6.57k | struct p2ps_advertisement *adv, *prev; |
2992 | | |
2993 | 6.57k | if (!p2p) |
2994 | 0 | return; |
2995 | | |
2996 | 6.57k | adv = p2p->p2ps_adv_list; |
2997 | 6.57k | while (adv) { |
2998 | 0 | prev = adv; |
2999 | 0 | adv = adv->next; |
3000 | 0 | os_free(prev); |
3001 | 0 | } |
3002 | | |
3003 | 6.57k | p2p->p2ps_adv_list = NULL; |
3004 | 6.57k | p2ps_prov_free(p2p); |
3005 | 6.57k | p2p_dbg(p2p, "All ASP advertisements flushed"); |
3006 | 6.57k | } |
3007 | | |
3008 | | |
3009 | | int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr) |
3010 | 0 | { |
3011 | 0 | struct p2p_message msg; |
3012 | |
|
3013 | 0 | os_memset(&msg, 0, sizeof(msg)); |
3014 | 0 | if (p2p_parse_p2p_ie(p2p_ie, &msg)) |
3015 | 0 | return -1; |
3016 | | |
3017 | 0 | if (msg.p2p_device_addr) { |
3018 | 0 | os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN); |
3019 | 0 | return 0; |
3020 | 0 | } else if (msg.device_id) { |
3021 | 0 | os_memcpy(dev_addr, msg.device_id, ETH_ALEN); |
3022 | 0 | return 0; |
3023 | 0 | } |
3024 | 0 | return -1; |
3025 | 0 | } |
3026 | | |
3027 | | |
3028 | | int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr) |
3029 | 0 | { |
3030 | 0 | struct wpabuf *p2p_ie; |
3031 | 0 | int ret; |
3032 | |
|
3033 | 0 | p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, |
3034 | 0 | P2P_IE_VENDOR_TYPE); |
3035 | 0 | if (p2p_ie == NULL) |
3036 | 0 | return -1; |
3037 | 0 | ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr); |
3038 | 0 | wpabuf_free(p2p_ie); |
3039 | 0 | return ret; |
3040 | 0 | } |
3041 | | |
3042 | | |
3043 | | static void p2p_clear_go_neg(struct p2p_data *p2p) |
3044 | 0 | { |
3045 | 0 | p2p->go_neg_peer = NULL; |
3046 | 0 | p2p_clear_timeout(p2p); |
3047 | 0 | p2p_set_state(p2p, P2P_IDLE); |
3048 | 0 | } |
3049 | | |
3050 | | |
3051 | | void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr) |
3052 | 0 | { |
3053 | 0 | if (p2p->go_neg_peer == NULL) { |
3054 | 0 | p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification"); |
3055 | 0 | return; /* No pending Group Formation */ |
3056 | 0 | } |
3057 | | |
3058 | 0 | if (!ether_addr_equal(mac_addr, p2p->go_neg_peer->intended_addr)) { |
3059 | 0 | p2p_dbg(p2p, "Ignore WPS registration success notification for " |
3060 | 0 | MACSTR " (GO Negotiation peer " MACSTR ")", |
3061 | 0 | MAC2STR(mac_addr), |
3062 | 0 | MAC2STR(p2p->go_neg_peer->intended_addr)); |
3063 | 0 | return; /* Ignore unexpected peer address */ |
3064 | 0 | } |
3065 | | |
3066 | 0 | p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR, |
3067 | 0 | MAC2STR(mac_addr)); |
3068 | |
|
3069 | 0 | p2p_clear_go_neg(p2p); |
3070 | 0 | } |
3071 | | |
3072 | | |
3073 | | void p2p_group_formation_failed(struct p2p_data *p2p) |
3074 | 0 | { |
3075 | 0 | if (p2p->go_neg_peer == NULL) { |
3076 | 0 | p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification"); |
3077 | 0 | return; /* No pending Group Formation */ |
3078 | 0 | } |
3079 | | |
3080 | 0 | p2p_dbg(p2p, "Group Formation failed with " MACSTR, |
3081 | 0 | MAC2STR(p2p->go_neg_peer->intended_addr)); |
3082 | |
|
3083 | 0 | p2p_clear_go_neg(p2p); |
3084 | 0 | } |
3085 | | |
3086 | | |
3087 | | bool is_p2p_6ghz_disabled(struct p2p_data *p2p) |
3088 | 0 | { |
3089 | 0 | if (p2p) |
3090 | 0 | return p2p->cfg->p2p_6ghz_disable; |
3091 | 0 | return false; |
3092 | 0 | } |
3093 | | |
3094 | | |
3095 | | void p2p_set_dev_addr(struct p2p_data *p2p, const u8 *addr) |
3096 | 0 | { |
3097 | 0 | if (p2p && addr) |
3098 | 0 | os_memcpy(p2p->cfg->dev_addr, addr, ETH_ALEN); |
3099 | 0 | } |
3100 | | |
3101 | | |
3102 | | void p2p_update_dfs_ap_info(struct p2p_data *p2p, int freq, |
3103 | | enum chan_width ap_ch_width, bool disconnect_evt) |
3104 | 0 | { |
3105 | 0 | if (disconnect_evt) { |
3106 | 0 | p2p->dfs_ap_connected = false; |
3107 | 0 | p2p->sta_connected_freq = 0; |
3108 | 0 | p2p->sta_connected_chan_width = CHAN_WIDTH_UNKNOWN; |
3109 | 0 | return; |
3110 | 0 | } |
3111 | | |
3112 | 0 | p2p->sta_connected_freq = freq; |
3113 | 0 | p2p->sta_connected_chan_width = ap_ch_width; |
3114 | 0 | p2p->dfs_ap_connected = true; |
3115 | 0 | } |
3116 | | |
3117 | | |
3118 | | static void p2p_pairing_info_deinit(struct p2p_data *p2p) |
3119 | 13.1k | { |
3120 | | #ifdef CONFIG_PASN |
3121 | | pasn_initiator_pmksa_cache_deinit(p2p->initiator_pmksa); |
3122 | | pasn_responder_pmksa_cache_deinit(p2p->responder_pmksa); |
3123 | | #endif /* CONFIG_PASN */ |
3124 | 13.1k | os_free(p2p->pairing_info); |
3125 | 13.1k | } |
3126 | | |
3127 | | |
3128 | | static int p2p_pairing_info_init(struct p2p_data *p2p) |
3129 | 6.57k | { |
3130 | 6.57k | struct p2p_pairing_info *pairing_info; |
3131 | | |
3132 | 6.57k | if (p2p->cfg->pairing_config.dik_len > DEVICE_IDENTITY_KEY_MAX_LEN) |
3133 | 0 | return -1; |
3134 | | |
3135 | 6.57k | pairing_info = os_zalloc(sizeof(struct p2p_pairing_info)); |
3136 | 6.57k | if (!pairing_info) |
3137 | 0 | return -1; |
3138 | | |
3139 | 6.57k | pairing_info->enable_pairing_setup = |
3140 | 6.57k | p2p->cfg->pairing_config.enable_pairing_setup; |
3141 | 6.57k | pairing_info->enable_pairing_cache = |
3142 | 6.57k | p2p->cfg->pairing_config.enable_pairing_cache; |
3143 | 6.57k | pairing_info->supported_bootstrap = |
3144 | 6.57k | p2p->cfg->pairing_config.bootstrap_methods; |
3145 | | |
3146 | 6.57k | pairing_info->dev_ik.cipher_version = |
3147 | 6.57k | p2p->cfg->pairing_config.dik_cipher; |
3148 | 6.57k | pairing_info->dev_ik.dik_len = p2p->cfg->pairing_config.dik_len; |
3149 | 6.57k | os_memcpy(pairing_info->dev_ik.dik_data, |
3150 | 6.57k | p2p->cfg->pairing_config.dik_data, |
3151 | 6.57k | p2p->cfg->pairing_config.dik_len); |
3152 | 6.57k | pairing_info->dev_ik.expiration = 24; /* hours */ |
3153 | | |
3154 | 6.57k | p2p_pairing_info_deinit(p2p); |
3155 | | |
3156 | 6.57k | p2p->pairing_info = pairing_info; |
3157 | | #ifdef CONFIG_PASN |
3158 | | p2p->initiator_pmksa = pasn_initiator_pmksa_cache_init(); |
3159 | | p2p->responder_pmksa = pasn_responder_pmksa_cache_init(); |
3160 | | #endif /* CONFIG_PASN */ |
3161 | | |
3162 | 6.57k | return 0; |
3163 | 6.57k | } |
3164 | | |
3165 | | |
3166 | | struct p2p_data * p2p_init(const struct p2p_config *cfg) |
3167 | 6.57k | { |
3168 | 6.57k | struct p2p_data *p2p; |
3169 | | |
3170 | 6.57k | if (cfg->max_peers < 1 || |
3171 | 6.57k | cfg->passphrase_len < 8 || cfg->passphrase_len > 63) |
3172 | 0 | return NULL; |
3173 | | |
3174 | 6.57k | p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg)); |
3175 | 6.57k | if (p2p == NULL) |
3176 | 0 | return NULL; |
3177 | 6.57k | p2p->cfg = (struct p2p_config *) (p2p + 1); |
3178 | 6.57k | os_memcpy(p2p->cfg, cfg, sizeof(*cfg)); |
3179 | 6.57k | if (cfg->dev_name) |
3180 | 0 | p2p->cfg->dev_name = os_strdup(cfg->dev_name); |
3181 | 6.57k | if (cfg->manufacturer) |
3182 | 0 | p2p->cfg->manufacturer = os_strdup(cfg->manufacturer); |
3183 | 6.57k | if (cfg->model_name) |
3184 | 0 | p2p->cfg->model_name = os_strdup(cfg->model_name); |
3185 | 6.57k | if (cfg->model_number) |
3186 | 0 | p2p->cfg->model_number = os_strdup(cfg->model_number); |
3187 | 6.57k | if (cfg->serial_number) |
3188 | 0 | p2p->cfg->serial_number = os_strdup(cfg->serial_number); |
3189 | 6.57k | if (cfg->pref_chan) { |
3190 | 0 | p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan * |
3191 | 0 | sizeof(struct p2p_channel)); |
3192 | 0 | if (p2p->cfg->pref_chan) { |
3193 | 0 | os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan, |
3194 | 0 | cfg->num_pref_chan * |
3195 | 0 | sizeof(struct p2p_channel)); |
3196 | 0 | } else |
3197 | 0 | p2p->cfg->num_pref_chan = 0; |
3198 | 0 | } |
3199 | | |
3200 | 6.57k | p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash); |
3201 | | |
3202 | 6.57k | p2p->min_disc_int = 1; |
3203 | 6.57k | p2p->max_disc_int = 3; |
3204 | 6.57k | p2p->max_disc_tu = -1; |
3205 | | |
3206 | 6.57k | if (os_get_random(&p2p->next_tie_breaker, 1) < 0) |
3207 | 0 | p2p->next_tie_breaker = 0; |
3208 | 6.57k | p2p->next_tie_breaker &= 0x01; |
3209 | 6.57k | if (cfg->sd_request) |
3210 | 0 | p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY; |
3211 | 6.57k | p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE; |
3212 | 6.57k | if (cfg->concurrent_operations) |
3213 | 0 | p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER; |
3214 | 6.57k | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
3215 | | |
3216 | 6.57k | dl_list_init(&p2p->devices); |
3217 | | |
3218 | 6.57k | p2p->go_timeout = 100; |
3219 | 6.57k | p2p->client_timeout = 20; |
3220 | 6.57k | p2p->num_p2p_sd_queries = 0; |
3221 | | /* Default comeback after one second */ |
3222 | 6.57k | if (!p2p->cfg->comeback_after) |
3223 | 6.57k | p2p->cfg->comeback_after = 977; /* TUs */ |
3224 | 6.57k | p2p_pairing_info_init(p2p); |
3225 | | |
3226 | 6.57k | p2p_dbg(p2p, "initialized"); |
3227 | 6.57k | p2p_channels_dump(p2p, "channels", &p2p->cfg->channels); |
3228 | 6.57k | p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels); |
3229 | | |
3230 | 6.57k | return p2p; |
3231 | 6.57k | } |
3232 | | |
3233 | | |
3234 | | void p2p_deinit(struct p2p_data *p2p) |
3235 | 6.57k | { |
3236 | 6.57k | #ifdef CONFIG_WIFI_DISPLAY |
3237 | 6.57k | wpabuf_free(p2p->wfd_ie_beacon); |
3238 | 6.57k | wpabuf_free(p2p->wfd_ie_probe_req); |
3239 | 6.57k | wpabuf_free(p2p->wfd_ie_probe_resp); |
3240 | 6.57k | wpabuf_free(p2p->wfd_ie_assoc_req); |
3241 | 6.57k | wpabuf_free(p2p->wfd_ie_invitation); |
3242 | 6.57k | wpabuf_free(p2p->wfd_ie_prov_disc_req); |
3243 | 6.57k | wpabuf_free(p2p->wfd_ie_prov_disc_resp); |
3244 | 6.57k | wpabuf_free(p2p->wfd_ie_go_neg); |
3245 | 6.57k | wpabuf_free(p2p->wfd_dev_info); |
3246 | 6.57k | wpabuf_free(p2p->wfd_assoc_bssid); |
3247 | 6.57k | wpabuf_free(p2p->wfd_coupled_sink_info); |
3248 | 6.57k | wpabuf_free(p2p->wfd_r2_dev_info); |
3249 | 6.57k | #endif /* CONFIG_WIFI_DISPLAY */ |
3250 | | |
3251 | 6.57k | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); |
3252 | 6.57k | eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL); |
3253 | 6.57k | eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL); |
3254 | 6.57k | p2p_flush(p2p); |
3255 | 6.57k | p2p_free_req_dev_types(p2p); |
3256 | 6.57k | os_free(p2p->cfg->dev_name); |
3257 | 6.57k | os_free(p2p->cfg->manufacturer); |
3258 | 6.57k | os_free(p2p->cfg->model_name); |
3259 | 6.57k | os_free(p2p->cfg->model_number); |
3260 | 6.57k | os_free(p2p->cfg->serial_number); |
3261 | 6.57k | os_free(p2p->cfg->pref_chan); |
3262 | 6.57k | os_free(p2p->groups); |
3263 | 6.57k | p2ps_prov_free(p2p); |
3264 | 6.57k | wpabuf_free(p2p->sd_resp); |
3265 | 6.57k | p2p_remove_wps_vendor_extensions(p2p); |
3266 | 6.57k | os_free(p2p->no_go_freq.range); |
3267 | 6.57k | p2p_service_flush_asp(p2p); |
3268 | 6.57k | p2p_pairing_info_deinit(p2p); |
3269 | | |
3270 | 6.57k | os_free(p2p); |
3271 | 6.57k | } |
3272 | | |
3273 | | |
3274 | | void p2p_flush(struct p2p_data *p2p) |
3275 | 6.57k | { |
3276 | 6.57k | struct p2p_device *dev, *prev; |
3277 | | |
3278 | 6.57k | p2p_ext_listen(p2p, 0, 0); |
3279 | 6.57k | p2p_stop_find(p2p); |
3280 | 6.57k | dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device, |
3281 | 6.57k | list) { |
3282 | 1.99k | dl_list_del(&dev->list); |
3283 | 1.99k | p2p_device_free(p2p, dev); |
3284 | 1.99k | } |
3285 | 6.57k | p2p_free_sd_queries(p2p); |
3286 | 6.57k | p2p->ssid_set = 0; |
3287 | 6.57k | p2ps_prov_free(p2p); |
3288 | 6.57k | p2p_reset_pending_pd(p2p); |
3289 | 6.57k | p2p->override_pref_op_class = 0; |
3290 | 6.57k | p2p->override_pref_channel = 0; |
3291 | 6.57k | } |
3292 | | |
3293 | | |
3294 | | int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr) |
3295 | 0 | { |
3296 | 0 | struct p2p_device *dev; |
3297 | |
|
3298 | 0 | dev = p2p_get_device(p2p, addr); |
3299 | 0 | if (dev == NULL) |
3300 | 0 | return -1; |
3301 | | |
3302 | 0 | p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr)); |
3303 | |
|
3304 | 0 | if (p2p->go_neg_peer == dev) { |
3305 | 0 | eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL); |
3306 | 0 | p2p->go_neg_peer = NULL; |
3307 | 0 | } |
3308 | |
|
3309 | 0 | dev->wps_method = WPS_NOT_READY; |
3310 | 0 | dev->oob_pw_id = 0; |
3311 | 0 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; |
3312 | 0 | dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; |
3313 | |
|
3314 | 0 | return 0; |
3315 | 0 | } |
3316 | | |
3317 | | |
3318 | | int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name) |
3319 | 0 | { |
3320 | 0 | os_free(p2p->cfg->dev_name); |
3321 | 0 | if (dev_name) { |
3322 | 0 | p2p->cfg->dev_name = os_strdup(dev_name); |
3323 | 0 | if (p2p->cfg->dev_name == NULL) |
3324 | 0 | return -1; |
3325 | 0 | } else |
3326 | 0 | p2p->cfg->dev_name = NULL; |
3327 | 0 | return 0; |
3328 | 0 | } |
3329 | | |
3330 | | |
3331 | | int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer) |
3332 | 0 | { |
3333 | 0 | os_free(p2p->cfg->manufacturer); |
3334 | 0 | p2p->cfg->manufacturer = NULL; |
3335 | 0 | if (manufacturer) { |
3336 | 0 | p2p->cfg->manufacturer = os_strdup(manufacturer); |
3337 | 0 | if (p2p->cfg->manufacturer == NULL) |
3338 | 0 | return -1; |
3339 | 0 | } |
3340 | | |
3341 | 0 | return 0; |
3342 | 0 | } |
3343 | | |
3344 | | |
3345 | | int p2p_set_model_name(struct p2p_data *p2p, const char *model_name) |
3346 | 0 | { |
3347 | 0 | os_free(p2p->cfg->model_name); |
3348 | 0 | p2p->cfg->model_name = NULL; |
3349 | 0 | if (model_name) { |
3350 | 0 | p2p->cfg->model_name = os_strdup(model_name); |
3351 | 0 | if (p2p->cfg->model_name == NULL) |
3352 | 0 | return -1; |
3353 | 0 | } |
3354 | | |
3355 | 0 | return 0; |
3356 | 0 | } |
3357 | | |
3358 | | |
3359 | | int p2p_set_model_number(struct p2p_data *p2p, const char *model_number) |
3360 | 0 | { |
3361 | 0 | os_free(p2p->cfg->model_number); |
3362 | 0 | p2p->cfg->model_number = NULL; |
3363 | 0 | if (model_number) { |
3364 | 0 | p2p->cfg->model_number = os_strdup(model_number); |
3365 | 0 | if (p2p->cfg->model_number == NULL) |
3366 | 0 | return -1; |
3367 | 0 | } |
3368 | | |
3369 | 0 | return 0; |
3370 | 0 | } |
3371 | | |
3372 | | |
3373 | | int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number) |
3374 | 0 | { |
3375 | 0 | os_free(p2p->cfg->serial_number); |
3376 | 0 | p2p->cfg->serial_number = NULL; |
3377 | 0 | if (serial_number) { |
3378 | 0 | p2p->cfg->serial_number = os_strdup(serial_number); |
3379 | 0 | if (p2p->cfg->serial_number == NULL) |
3380 | 0 | return -1; |
3381 | 0 | } |
3382 | | |
3383 | 0 | return 0; |
3384 | 0 | } |
3385 | | |
3386 | | |
3387 | | void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods) |
3388 | 0 | { |
3389 | 0 | p2p->cfg->config_methods = config_methods; |
3390 | 0 | } |
3391 | | |
3392 | | |
3393 | | void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid) |
3394 | 0 | { |
3395 | 0 | os_memcpy(p2p->cfg->uuid, uuid, 16); |
3396 | 0 | } |
3397 | | |
3398 | | |
3399 | | int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type) |
3400 | 0 | { |
3401 | 0 | os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8); |
3402 | 0 | return 0; |
3403 | 0 | } |
3404 | | |
3405 | | |
3406 | | int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8], |
3407 | | size_t num_dev_types) |
3408 | 0 | { |
3409 | 0 | if (num_dev_types > P2P_SEC_DEVICE_TYPES) |
3410 | 0 | num_dev_types = P2P_SEC_DEVICE_TYPES; |
3411 | 0 | p2p->cfg->num_sec_dev_types = num_dev_types; |
3412 | 0 | os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8); |
3413 | 0 | return 0; |
3414 | 0 | } |
3415 | | |
3416 | | |
3417 | | void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p) |
3418 | 6.57k | { |
3419 | 6.57k | int i; |
3420 | | |
3421 | 72.3k | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
3422 | 65.7k | wpabuf_free(p2p->wps_vendor_ext[i]); |
3423 | 65.7k | p2p->wps_vendor_ext[i] = NULL; |
3424 | 65.7k | } |
3425 | 6.57k | } |
3426 | | |
3427 | | |
3428 | | int p2p_add_wps_vendor_extension(struct p2p_data *p2p, |
3429 | | const struct wpabuf *vendor_ext) |
3430 | 0 | { |
3431 | 0 | int i; |
3432 | |
|
3433 | 0 | if (vendor_ext == NULL) |
3434 | 0 | return -1; |
3435 | | |
3436 | 0 | for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { |
3437 | 0 | if (p2p->wps_vendor_ext[i] == NULL) |
3438 | 0 | break; |
3439 | 0 | } |
3440 | 0 | if (i >= P2P_MAX_WPS_VENDOR_EXT) |
3441 | 0 | return -1; |
3442 | | |
3443 | 0 | p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext); |
3444 | 0 | if (p2p->wps_vendor_ext[i] == NULL) |
3445 | 0 | return -1; |
3446 | | |
3447 | 0 | return 0; |
3448 | 0 | } |
3449 | | |
3450 | | |
3451 | | int p2p_set_country(struct p2p_data *p2p, const char *country) |
3452 | 0 | { |
3453 | 0 | os_memcpy(p2p->cfg->country, country, 3); |
3454 | 0 | return 0; |
3455 | 0 | } |
3456 | | |
3457 | | |
3458 | | static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev) |
3459 | 0 | { |
3460 | 0 | int res; |
3461 | |
|
3462 | 0 | if (dev->sd_pending_bcast_queries == 0) { |
3463 | | /* Initialize with total number of registered broadcast |
3464 | | * SD queries. */ |
3465 | 0 | dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries; |
3466 | 0 | } |
3467 | |
|
3468 | 0 | res = p2p_start_sd(p2p, dev); |
3469 | 0 | if (res == -2) |
3470 | 0 | return -2; |
3471 | 0 | if (res == 0) |
3472 | 0 | return 1; |
3473 | | |
3474 | 0 | if (dev->req_config_methods && |
3475 | 0 | !(dev->flags & P2P_DEV_PD_FOR_JOIN)) { |
3476 | 0 | p2p_dbg(p2p, "Send pending Provision Discovery Request to " |
3477 | 0 | MACSTR " (config methods 0x%x)", |
3478 | 0 | MAC2STR(dev->info.p2p_device_addr), |
3479 | 0 | dev->req_config_methods); |
3480 | 0 | if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0) |
3481 | 0 | return 1; |
3482 | 0 | } |
3483 | | |
3484 | 0 | return 0; |
3485 | 0 | } |
3486 | | |
3487 | | |
3488 | | void p2p_continue_find(struct p2p_data *p2p) |
3489 | 0 | { |
3490 | 0 | struct p2p_device *dev; |
3491 | 0 | int found, res; |
3492 | |
|
3493 | 0 | p2p_set_state(p2p, P2P_SEARCH); |
3494 | | |
3495 | | /* Continue from the device following the last iteration */ |
3496 | 0 | found = 0; |
3497 | 0 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
3498 | 0 | if (dev == p2p->last_p2p_find_oper) { |
3499 | 0 | found = 1; |
3500 | 0 | continue; |
3501 | 0 | } |
3502 | 0 | if (!found) |
3503 | 0 | continue; |
3504 | 0 | res = p2p_pre_find_operation(p2p, dev); |
3505 | 0 | if (res > 0) { |
3506 | 0 | p2p->last_p2p_find_oper = dev; |
3507 | 0 | return; |
3508 | 0 | } |
3509 | 0 | if (res == -2) |
3510 | 0 | goto skip_sd; |
3511 | 0 | } |
3512 | | |
3513 | | /* |
3514 | | * Wrap around to the beginning of the list and continue until the last |
3515 | | * iteration device. |
3516 | | */ |
3517 | 0 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
3518 | 0 | res = p2p_pre_find_operation(p2p, dev); |
3519 | 0 | if (res > 0) { |
3520 | 0 | p2p->last_p2p_find_oper = dev; |
3521 | 0 | return; |
3522 | 0 | } |
3523 | 0 | if (res == -2) |
3524 | 0 | goto skip_sd; |
3525 | 0 | if (dev == p2p->last_p2p_find_oper) |
3526 | 0 | break; |
3527 | 0 | } |
3528 | | |
3529 | 0 | skip_sd: |
3530 | 0 | os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN); |
3531 | 0 | p2p_listen_in_find(p2p, 1); |
3532 | 0 | } |
3533 | | |
3534 | | |
3535 | | void p2p_sd_query_cb(struct p2p_data *p2p, int success) |
3536 | 0 | { |
3537 | 0 | p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d", |
3538 | 0 | success); |
3539 | 0 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
3540 | |
|
3541 | 0 | if (!success) { |
3542 | 0 | if (p2p->sd_peer) { |
3543 | 0 | if (is_zero_ether_addr(p2p->sd_query_no_ack)) { |
3544 | 0 | os_memcpy(p2p->sd_query_no_ack, |
3545 | 0 | p2p->sd_peer->info.p2p_device_addr, |
3546 | 0 | ETH_ALEN); |
3547 | 0 | p2p_dbg(p2p, |
3548 | 0 | "First SD Query no-ACK in this search iteration: " |
3549 | 0 | MACSTR, MAC2STR(p2p->sd_query_no_ack)); |
3550 | 0 | } |
3551 | 0 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
3552 | 0 | } |
3553 | 0 | p2p->sd_peer = NULL; |
3554 | 0 | if (p2p->state != P2P_IDLE) |
3555 | 0 | p2p_continue_find(p2p); |
3556 | 0 | return; |
3557 | 0 | } |
3558 | | |
3559 | 0 | if (p2p->sd_peer == NULL) { |
3560 | 0 | p2p_dbg(p2p, "No SD peer entry known"); |
3561 | 0 | if (p2p->state != P2P_IDLE) |
3562 | 0 | p2p_continue_find(p2p); |
3563 | 0 | return; |
3564 | 0 | } |
3565 | | |
3566 | 0 | if (p2p->sd_query && p2p->sd_query->for_all_peers) { |
3567 | | /* Update the pending broadcast SD query count for this device |
3568 | | */ |
3569 | 0 | p2p->sd_peer->sd_pending_bcast_queries--; |
3570 | | |
3571 | | /* |
3572 | | * If there are no pending broadcast queries for this device, |
3573 | | * mark it as done (-1). |
3574 | | */ |
3575 | 0 | if (p2p->sd_peer->sd_pending_bcast_queries == 0) |
3576 | 0 | p2p->sd_peer->sd_pending_bcast_queries = -1; |
3577 | 0 | } |
3578 | | |
3579 | | /* Wait for response from the peer */ |
3580 | 0 | p2p_set_state(p2p, P2P_SD_DURING_FIND); |
3581 | 0 | p2p_set_timeout(p2p, 0, 200000); |
3582 | 0 | } |
3583 | | |
3584 | | |
3585 | | /** |
3586 | | * p2p_retry_pd - Retry any pending provision disc requests in IDLE state |
3587 | | * @p2p: P2P module context from p2p_init() |
3588 | | */ |
3589 | | static void p2p_retry_pd(struct p2p_data *p2p) |
3590 | 0 | { |
3591 | 0 | struct p2p_device *dev; |
3592 | | |
3593 | | /* |
3594 | | * Retry the prov disc req attempt only for the peer that the user had |
3595 | | * requested. |
3596 | | */ |
3597 | |
|
3598 | 0 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
3599 | 0 | if (!ether_addr_equal(p2p->pending_pd_devaddr, |
3600 | 0 | dev->info.p2p_device_addr)) |
3601 | 0 | continue; |
3602 | 0 | if (!dev->req_config_methods && !dev->req_bootstrap_method) |
3603 | 0 | continue; |
3604 | | |
3605 | 0 | p2p_dbg(p2p, "Send pending Provision Discovery Request to " |
3606 | 0 | MACSTR " (config methods 0x%x)", |
3607 | 0 | MAC2STR(dev->info.p2p_device_addr), |
3608 | 0 | dev->req_config_methods); |
3609 | 0 | p2p_send_prov_disc_req(p2p, dev, |
3610 | 0 | dev->flags & P2P_DEV_PD_FOR_JOIN, |
3611 | 0 | p2p->pd_force_freq); |
3612 | 0 | return; |
3613 | 0 | } |
3614 | 0 | } |
3615 | | |
3616 | | |
3617 | | static void p2p_prov_disc_cb(struct p2p_data *p2p, int success) |
3618 | 0 | { |
3619 | 0 | p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d", |
3620 | 0 | success); |
3621 | | |
3622 | | /* |
3623 | | * Postpone resetting the pending action state till after we actually |
3624 | | * time out. This allows us to take some action like notifying any |
3625 | | * interested parties about no response to the request. |
3626 | | * |
3627 | | * When the timer (below) goes off we check in IDLE, SEARCH, or |
3628 | | * LISTEN_ONLY state, which are the only allowed states to issue a PD |
3629 | | * requests in, if this was still pending and then raise notification. |
3630 | | */ |
3631 | |
|
3632 | 0 | if (!success) { |
3633 | 0 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
3634 | |
|
3635 | 0 | if (p2p->user_initiated_pd && |
3636 | 0 | (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY)) |
3637 | 0 | { |
3638 | | /* Retry request from timeout to avoid busy loops */ |
3639 | 0 | p2p->pending_action_state = P2P_PENDING_PD; |
3640 | 0 | p2p_set_timeout(p2p, 0, 50000); |
3641 | 0 | } else if (p2p->state != P2P_IDLE) |
3642 | 0 | p2p_continue_find(p2p); |
3643 | 0 | else if (p2p->user_initiated_pd) { |
3644 | 0 | p2p->pending_action_state = P2P_PENDING_PD; |
3645 | 0 | p2p_set_timeout(p2p, 0, 300000); |
3646 | 0 | } |
3647 | 0 | return; |
3648 | 0 | } |
3649 | | |
3650 | | /* |
3651 | | * If after PD Request the peer doesn't expect to receive PD Response |
3652 | | * the PD Request ACK indicates a completion of the current PD. This |
3653 | | * happens only on the advertiser side sending the follow-on PD Request |
3654 | | * with the status different than 12 (Success: accepted by user). |
3655 | | */ |
3656 | 0 | if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker && |
3657 | 0 | p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) { |
3658 | 0 | p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK"); |
3659 | |
|
3660 | 0 | if (p2p->send_action_in_progress) { |
3661 | 0 | p2p->send_action_in_progress = 0; |
3662 | 0 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
3663 | 0 | } |
3664 | |
|
3665 | 0 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
3666 | |
|
3667 | 0 | if (p2p->cfg->p2ps_prov_complete) { |
3668 | 0 | p2p->cfg->p2ps_prov_complete( |
3669 | 0 | p2p->cfg->cb_ctx, |
3670 | 0 | p2p->p2ps_prov->status, |
3671 | 0 | p2p->p2ps_prov->adv_mac, |
3672 | 0 | p2p->p2ps_prov->adv_mac, |
3673 | 0 | p2p->p2ps_prov->session_mac, |
3674 | 0 | NULL, p2p->p2ps_prov->adv_id, |
3675 | 0 | p2p->p2ps_prov->session_id, |
3676 | 0 | 0, 0, NULL, 0, 0, 0, |
3677 | 0 | NULL, NULL, 0, 0, NULL, 0); |
3678 | 0 | } |
3679 | |
|
3680 | 0 | if (p2p->user_initiated_pd) |
3681 | 0 | p2p_reset_pending_pd(p2p); |
3682 | |
|
3683 | 0 | p2ps_prov_free(p2p); |
3684 | 0 | return; |
3685 | 0 | } |
3686 | | |
3687 | | /* |
3688 | | * This postponing, of resetting pending_action_state, needs to be |
3689 | | * done only for user initiated PD requests and not internal ones. |
3690 | | */ |
3691 | 0 | if (p2p->user_initiated_pd) |
3692 | 0 | p2p->pending_action_state = P2P_PENDING_PD; |
3693 | 0 | else |
3694 | 0 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
3695 | | |
3696 | | /* Wait for response from the peer */ |
3697 | 0 | if (p2p->state == P2P_SEARCH) |
3698 | 0 | p2p_set_state(p2p, P2P_PD_DURING_FIND); |
3699 | 0 | p2p_set_timeout(p2p, 0, 200000); |
3700 | 0 | } |
3701 | | |
3702 | | |
3703 | | static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success) |
3704 | 0 | { |
3705 | 0 | p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d", |
3706 | 0 | success); |
3707 | |
|
3708 | 0 | if (p2p->send_action_in_progress) { |
3709 | 0 | p2p->send_action_in_progress = 0; |
3710 | 0 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
3711 | 0 | } |
3712 | |
|
3713 | 0 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
3714 | |
|
3715 | 0 | if (!success) { |
3716 | 0 | if (p2p->state == P2P_SEARCH) |
3717 | 0 | p2p_continue_find(p2p); |
3718 | 0 | return; |
3719 | 0 | } |
3720 | | |
3721 | 0 | if (!p2p->cfg->prov_disc_resp_cb || |
3722 | 0 | p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) { |
3723 | 0 | if (p2p->state == P2P_SEARCH) |
3724 | 0 | p2p_continue_find(p2p); |
3725 | 0 | return; |
3726 | 0 | } |
3727 | | |
3728 | 0 | p2p_dbg(p2p, |
3729 | 0 | "Post-Provision Discovery operations started - do not try to continue other P2P operations"); |
3730 | 0 | } |
3731 | | |
3732 | | |
3733 | | int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq, |
3734 | | struct os_reltime *rx_time, int level, const u8 *ies, |
3735 | | size_t ies_len) |
3736 | 6.57k | { |
3737 | 6.57k | if (os_reltime_before(rx_time, &p2p->find_start)) { |
3738 | | /* |
3739 | | * The driver may have cached (e.g., in cfg80211 BSS table) the |
3740 | | * scan results for relatively long time. To avoid reporting |
3741 | | * stale information, update P2P peers only based on results |
3742 | | * that have based on frames received after the last p2p_find |
3743 | | * operation was started. |
3744 | | */ |
3745 | 0 | p2p_dbg(p2p, "Ignore old scan result for " MACSTR |
3746 | 0 | " (rx_time=%u.%06u find_start=%u.%06u)", |
3747 | 0 | MAC2STR(bssid), (unsigned int) rx_time->sec, |
3748 | 0 | (unsigned int) rx_time->usec, |
3749 | 0 | (unsigned int) p2p->find_start.sec, |
3750 | 0 | (unsigned int) p2p->find_start.usec); |
3751 | 0 | return 0; |
3752 | 0 | } |
3753 | | |
3754 | 6.57k | p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1); |
3755 | | |
3756 | 6.57k | return 0; |
3757 | 6.57k | } |
3758 | | |
3759 | | |
3760 | | void p2p_scan_res_handled(struct p2p_data *p2p, unsigned int delay) |
3761 | 6.57k | { |
3762 | 6.57k | if (!p2p->p2p_scan_running) { |
3763 | 6.57k | p2p_dbg(p2p, "p2p_scan was not running, but scan results received"); |
3764 | 6.57k | } |
3765 | 6.57k | p2p->p2p_scan_running = 0; |
3766 | | |
3767 | | /* Use this delay only when p2p_find doesn't set it */ |
3768 | 6.57k | if (!p2p->search_delay) |
3769 | 6.57k | p2p->search_delay = delay; |
3770 | | |
3771 | 6.57k | eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); |
3772 | | |
3773 | 6.57k | if (p2p_run_after_scan(p2p)) |
3774 | 0 | return; |
3775 | 6.57k | if (p2p->state == P2P_SEARCH) |
3776 | 0 | p2p_continue_find(p2p); |
3777 | 6.57k | } |
3778 | | |
3779 | | |
3780 | | void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id, |
3781 | | unsigned int bands) |
3782 | 0 | { |
3783 | 0 | u8 dev_capab; |
3784 | 0 | u8 *len; |
3785 | |
|
3786 | 0 | #ifdef CONFIG_WIFI_DISPLAY |
3787 | 0 | if (p2p->wfd_ie_probe_req) |
3788 | 0 | wpabuf_put_buf(ies, p2p->wfd_ie_probe_req); |
3789 | 0 | #endif /* CONFIG_WIFI_DISPLAY */ |
3790 | |
|
3791 | 0 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]) |
3792 | 0 | wpabuf_put_buf(ies, |
3793 | 0 | p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]); |
3794 | |
|
3795 | 0 | len = p2p_buf_add_ie_hdr(ies); |
3796 | |
|
3797 | 0 | dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
3798 | | |
3799 | | /* P2PS requires Probe Request frames to include SD bit */ |
3800 | 0 | if (p2p->p2ps_seek && p2p->p2ps_seek_count) |
3801 | 0 | dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY; |
3802 | |
|
3803 | 0 | p2p_buf_add_capability(ies, dev_capab, 0); |
3804 | |
|
3805 | 0 | if (dev_id) |
3806 | 0 | p2p_buf_add_device_id(ies, dev_id); |
3807 | 0 | if (p2p->cfg->reg_class && p2p->cfg->channel) |
3808 | 0 | p2p_buf_add_listen_channel(ies, p2p->cfg->country, |
3809 | 0 | p2p->cfg->reg_class, |
3810 | 0 | p2p->cfg->channel); |
3811 | 0 | if (p2p->ext_listen_interval) |
3812 | 0 | p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period, |
3813 | 0 | p2p->ext_listen_interval); |
3814 | |
|
3815 | 0 | if (bands & BAND_60_GHZ) |
3816 | 0 | p2p_buf_add_device_info(ies, p2p, NULL); |
3817 | |
|
3818 | 0 | if (p2p->p2ps_seek && p2p->p2ps_seek_count) |
3819 | 0 | p2p_buf_add_service_hash(ies, p2p); |
3820 | | |
3821 | | /* TODO: p2p_buf_add_operating_channel() if GO */ |
3822 | 0 | p2p_buf_update_ie_hdr(ies, len); |
3823 | 0 | } |
3824 | | |
3825 | | |
3826 | | size_t p2p_scan_ie_buf_len(struct p2p_data *p2p) |
3827 | 0 | { |
3828 | 0 | size_t len = 100; |
3829 | |
|
3830 | 0 | #ifdef CONFIG_WIFI_DISPLAY |
3831 | 0 | if (p2p && p2p->wfd_ie_probe_req) |
3832 | 0 | len += wpabuf_len(p2p->wfd_ie_probe_req); |
3833 | 0 | #endif /* CONFIG_WIFI_DISPLAY */ |
3834 | |
|
3835 | 0 | if (p2p && p2p->vendor_elem && |
3836 | 0 | p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]) |
3837 | 0 | len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]); |
3838 | |
|
3839 | 0 | return len; |
3840 | 0 | } |
3841 | | |
3842 | | |
3843 | | int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end) |
3844 | 0 | { |
3845 | 0 | return p2p_attr_text(p2p_ie, buf, end); |
3846 | 0 | } |
3847 | | |
3848 | | |
3849 | | static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success) |
3850 | 0 | { |
3851 | 0 | struct p2p_device *dev = p2p->go_neg_peer; |
3852 | 0 | int timeout; |
3853 | |
|
3854 | 0 | p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success); |
3855 | |
|
3856 | 0 | if (dev == NULL) { |
3857 | 0 | p2p_dbg(p2p, "No pending GO Negotiation"); |
3858 | 0 | return; |
3859 | 0 | } |
3860 | | |
3861 | 0 | if (success) { |
3862 | 0 | if (dev->flags & P2P_DEV_USER_REJECTED) { |
3863 | 0 | p2p_set_state(p2p, P2P_IDLE); |
3864 | 0 | return; |
3865 | 0 | } |
3866 | 0 | } else if (dev->go_neg_req_sent) { |
3867 | | /* Cancel the increment from p2p_connect_send() on failure */ |
3868 | 0 | dev->go_neg_req_sent--; |
3869 | 0 | } |
3870 | | |
3871 | 0 | if (!success && |
3872 | 0 | (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) && |
3873 | 0 | !is_zero_ether_addr(dev->member_in_go_dev)) { |
3874 | 0 | p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO", |
3875 | 0 | MAC2STR(dev->info.p2p_device_addr)); |
3876 | 0 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
3877 | 0 | p2p_send_dev_disc_req(p2p, dev); |
3878 | 0 | return; |
3879 | 0 | } |
3880 | | |
3881 | | /* |
3882 | | * Use P2P find, if needed, to find the other device from its listen |
3883 | | * channel. |
3884 | | */ |
3885 | 0 | p2p_set_state(p2p, P2P_CONNECT); |
3886 | 0 | timeout = success ? 500000 : 100000; |
3887 | 0 | if (!success && p2p->go_neg_peer && |
3888 | 0 | (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) { |
3889 | 0 | unsigned int r; |
3890 | | /* |
3891 | | * Peer is expected to wait our response and we will skip the |
3892 | | * listen phase. Add some randomness to the wait time here to |
3893 | | * make it less likely to hit cases where we could end up in |
3894 | | * sync with peer not listening. |
3895 | | */ |
3896 | 0 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) |
3897 | 0 | r = 0; |
3898 | 0 | timeout += r % 100000; |
3899 | 0 | } |
3900 | 0 | p2p_set_timeout(p2p, 0, timeout); |
3901 | 0 | } |
3902 | | |
3903 | | |
3904 | | static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success) |
3905 | 0 | { |
3906 | 0 | p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d", |
3907 | 0 | success); |
3908 | 0 | if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) { |
3909 | 0 | p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore"); |
3910 | 0 | return; |
3911 | 0 | } |
3912 | 0 | p2p_set_state(p2p, P2P_CONNECT); |
3913 | 0 | p2p_set_timeout(p2p, 0, 500000); |
3914 | 0 | } |
3915 | | |
3916 | | |
3917 | | static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success, |
3918 | | const u8 *addr) |
3919 | 0 | { |
3920 | 0 | p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success); |
3921 | 0 | if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) { |
3922 | 0 | p2p_go_neg_failed(p2p, p2p->go_neg_peer->status); |
3923 | 0 | return; |
3924 | 0 | } |
3925 | | |
3926 | 0 | if (success) { |
3927 | 0 | struct p2p_device *dev; |
3928 | 0 | dev = p2p_get_device(p2p, addr); |
3929 | 0 | if (dev && |
3930 | 0 | dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) |
3931 | 0 | dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE; |
3932 | 0 | } |
3933 | |
|
3934 | 0 | if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND) |
3935 | 0 | p2p_continue_find(p2p); |
3936 | 0 | } |
3937 | | |
3938 | | |
3939 | | static void p2p_go_neg_conf_cb(struct p2p_data *p2p, |
3940 | | enum p2p_send_action_result result) |
3941 | 0 | { |
3942 | 0 | struct p2p_device *dev; |
3943 | |
|
3944 | 0 | p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result); |
3945 | 0 | if (result == P2P_SEND_ACTION_FAILED) { |
3946 | 0 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
3947 | 0 | p2p_go_neg_failed(p2p, -1); |
3948 | 0 | return; |
3949 | 0 | } |
3950 | | |
3951 | 0 | dev = p2p->go_neg_peer; |
3952 | |
|
3953 | 0 | if (result == P2P_SEND_ACTION_NO_ACK) { |
3954 | | /* |
3955 | | * Retry GO Negotiation Confirmation |
3956 | | * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive |
3957 | | * ACK for confirmation. |
3958 | | */ |
3959 | 0 | if (dev && dev->go_neg_conf && |
3960 | 0 | dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) { |
3961 | 0 | p2p_dbg(p2p, "GO Negotiation Confirm retry %d", |
3962 | 0 | dev->go_neg_conf_sent); |
3963 | 0 | p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM; |
3964 | 0 | if (p2p_send_action(p2p, dev->go_neg_conf_freq, |
3965 | 0 | dev->info.p2p_device_addr, |
3966 | 0 | p2p->cfg->dev_addr, |
3967 | 0 | dev->info.p2p_device_addr, |
3968 | 0 | wpabuf_head(dev->go_neg_conf), |
3969 | 0 | wpabuf_len(dev->go_neg_conf), 0) >= |
3970 | 0 | 0) { |
3971 | 0 | dev->go_neg_conf_sent++; |
3972 | 0 | return; |
3973 | 0 | } |
3974 | 0 | p2p_dbg(p2p, "Failed to re-send Action frame"); |
3975 | | |
3976 | | /* |
3977 | | * Continue with the assumption that the first attempt |
3978 | | * went through and just the ACK frame was lost. |
3979 | | */ |
3980 | 0 | } |
3981 | | |
3982 | | /* |
3983 | | * It looks like the TX status for GO Negotiation Confirm is |
3984 | | * often showing failure even when the peer has actually |
3985 | | * received the frame. Since the peer may change channels |
3986 | | * immediately after having received the frame, we may not see |
3987 | | * an Ack for retries, so just dropping a single frame may |
3988 | | * trigger this. To allow the group formation to succeed if the |
3989 | | * peer did indeed receive the frame, continue regardless of |
3990 | | * the TX status. |
3991 | | */ |
3992 | 0 | p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported"); |
3993 | 0 | } |
3994 | | |
3995 | 0 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
3996 | |
|
3997 | 0 | if (dev == NULL) |
3998 | 0 | return; |
3999 | | |
4000 | 0 | p2p_go_complete(p2p, dev); |
4001 | 0 | } |
4002 | | |
4003 | | |
4004 | | void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst, |
4005 | | const u8 *src, const u8 *bssid, |
4006 | | enum p2p_send_action_result result) |
4007 | 0 | { |
4008 | 0 | enum p2p_pending_action_state state; |
4009 | 0 | int success; |
4010 | |
|
4011 | 0 | p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR |
4012 | 0 | " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)", |
4013 | 0 | p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src), |
4014 | 0 | MAC2STR(bssid), result, p2p_state_txt(p2p->state)); |
4015 | 0 | success = result == P2P_SEND_ACTION_SUCCESS; |
4016 | 0 | state = p2p->pending_action_state; |
4017 | 0 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
4018 | 0 | switch (state) { |
4019 | 0 | case P2P_NO_PENDING_ACTION: |
4020 | 0 | if (p2p->send_action_in_progress) { |
4021 | 0 | p2p->send_action_in_progress = 0; |
4022 | 0 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
4023 | 0 | } |
4024 | 0 | break; |
4025 | 0 | case P2P_PENDING_GO_NEG_REQUEST: |
4026 | 0 | p2p_go_neg_req_cb(p2p, success); |
4027 | 0 | break; |
4028 | 0 | case P2P_PENDING_GO_NEG_RESPONSE: |
4029 | 0 | p2p_go_neg_resp_cb(p2p, success); |
4030 | 0 | break; |
4031 | 0 | case P2P_PENDING_GO_NEG_RESPONSE_FAILURE: |
4032 | 0 | p2p_go_neg_resp_failure_cb(p2p, success, dst); |
4033 | 0 | break; |
4034 | 0 | case P2P_PENDING_GO_NEG_CONFIRM: |
4035 | 0 | p2p_go_neg_conf_cb(p2p, result); |
4036 | 0 | break; |
4037 | 0 | case P2P_PENDING_SD: |
4038 | 0 | p2p_sd_query_cb(p2p, success); |
4039 | 0 | break; |
4040 | 0 | case P2P_PENDING_PD: |
4041 | 0 | p2p_prov_disc_cb(p2p, success); |
4042 | 0 | break; |
4043 | 0 | case P2P_PENDING_PD_RESPONSE: |
4044 | 0 | p2p_prov_disc_resp_cb(p2p, success); |
4045 | 0 | break; |
4046 | 0 | case P2P_PENDING_INVITATION_REQUEST: |
4047 | 0 | p2p_invitation_req_cb(p2p, success); |
4048 | 0 | break; |
4049 | 0 | case P2P_PENDING_INVITATION_RESPONSE: |
4050 | 0 | p2p_invitation_resp_cb(p2p, dst, success); |
4051 | 0 | break; |
4052 | 0 | case P2P_PENDING_DEV_DISC_REQUEST: |
4053 | 0 | p2p_dev_disc_req_cb(p2p, success); |
4054 | 0 | break; |
4055 | 0 | case P2P_PENDING_DEV_DISC_RESPONSE: |
4056 | 0 | p2p_dev_disc_resp_cb(p2p, success); |
4057 | 0 | break; |
4058 | 0 | case P2P_PENDING_GO_DISC_REQ: |
4059 | 0 | p2p_go_disc_req_cb(p2p, success); |
4060 | 0 | break; |
4061 | 0 | } |
4062 | 0 | } |
4063 | | |
4064 | | |
4065 | | void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq, |
4066 | | unsigned int duration) |
4067 | 0 | { |
4068 | 0 | if (freq == p2p->pending_client_disc_freq) { |
4069 | 0 | p2p_dbg(p2p, "Client discoverability remain-awake completed"); |
4070 | 0 | p2p->pending_client_disc_freq = 0; |
4071 | 0 | return; |
4072 | 0 | } |
4073 | | |
4074 | 0 | if (freq != p2p->pending_listen_freq) { |
4075 | 0 | p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)", |
4076 | 0 | freq, duration, p2p->pending_listen_freq); |
4077 | 0 | return; |
4078 | 0 | } |
4079 | | |
4080 | 0 | p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback", |
4081 | 0 | p2p->pending_listen_sec, p2p->pending_listen_usec, |
4082 | 0 | p2p->pending_listen_freq); |
4083 | 0 | p2p->pending_listen_wait_drv = false; |
4084 | 0 | p2p->in_listen = 1; |
4085 | 0 | p2p->drv_in_listen = freq; |
4086 | 0 | if (p2p->pending_listen_sec || p2p->pending_listen_usec) { |
4087 | | /* |
4088 | | * Add 20 msec extra wait to avoid race condition with driver |
4089 | | * remain-on-channel end event, i.e., give driver more time to |
4090 | | * complete the operation before our timeout expires. |
4091 | | */ |
4092 | 0 | p2p_set_timeout(p2p, p2p->pending_listen_sec, |
4093 | 0 | p2p->pending_listen_usec + 20000); |
4094 | 0 | } |
4095 | |
|
4096 | 0 | p2p->pending_listen_freq = 0; |
4097 | 0 | } |
4098 | | |
4099 | | |
4100 | | int p2p_listen_end(struct p2p_data *p2p, unsigned int freq) |
4101 | 0 | { |
4102 | 0 | p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq); |
4103 | 0 | p2p->drv_in_listen = 0; |
4104 | 0 | if (p2p->in_listen) |
4105 | 0 | return 0; /* Internal timeout will trigger the next step */ |
4106 | | |
4107 | 0 | if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer && |
4108 | 0 | p2p->pending_listen_freq) { |
4109 | | /* |
4110 | | * Better wait a bit if the driver is unable to start |
4111 | | * offchannel operation for some reason to continue with |
4112 | | * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions. |
4113 | | */ |
4114 | 0 | p2p_dbg(p2p, |
4115 | 0 | "Listen operation did not seem to start - delay idle phase to avoid busy loop"); |
4116 | 0 | p2p_set_timeout(p2p, 0, 100000); |
4117 | 0 | return 1; |
4118 | 0 | } |
4119 | | |
4120 | 0 | if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) { |
4121 | 0 | if (p2p->go_neg_peer->connect_reqs >= 120) { |
4122 | 0 | p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response"); |
4123 | 0 | p2p_go_neg_failed(p2p, -1); |
4124 | 0 | return 0; |
4125 | 0 | } |
4126 | | |
4127 | 0 | p2p_set_state(p2p, P2P_CONNECT); |
4128 | 0 | p2p_connect_send(p2p, p2p->go_neg_peer); |
4129 | 0 | return 1; |
4130 | 0 | } else if (p2p->state == P2P_SEARCH) { |
4131 | 0 | if (p2p->p2p_scan_running) { |
4132 | | /* |
4133 | | * Search is already in progress. This can happen if |
4134 | | * an Action frame RX is reported immediately after |
4135 | | * the end of a remain-on-channel operation and the |
4136 | | * response frame to that is sent using an offchannel |
4137 | | * operation while in p2p_find. Avoid an attempt to |
4138 | | * restart a scan here. |
4139 | | */ |
4140 | 0 | p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one"); |
4141 | 0 | return 1; |
4142 | 0 | } |
4143 | 0 | if (p2p->pending_listen_freq) { |
4144 | | /* |
4145 | | * Better wait a bit if the driver is unable to start |
4146 | | * offchannel operation for some reason. p2p_search() |
4147 | | * will be started from internal timeout. |
4148 | | */ |
4149 | 0 | p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop"); |
4150 | 0 | p2p_set_timeout(p2p, 0, 100000); |
4151 | 0 | return 1; |
4152 | 0 | } |
4153 | 0 | if (p2p->search_delay) { |
4154 | 0 | p2p_dbg(p2p, "Delay search operation by %u ms", |
4155 | 0 | p2p->search_delay); |
4156 | 0 | p2p_set_timeout(p2p, p2p->search_delay / 1000, |
4157 | 0 | (p2p->search_delay % 1000) * 1000); |
4158 | 0 | return 1; |
4159 | 0 | } |
4160 | 0 | p2p_search(p2p); |
4161 | 0 | return 1; |
4162 | 0 | } |
4163 | | |
4164 | 0 | return 0; |
4165 | 0 | } |
4166 | | |
4167 | | |
4168 | | void p2p_listen_failed(struct p2p_data *p2p, unsigned int freq) |
4169 | 0 | { |
4170 | 0 | if (freq != p2p->pending_listen_freq) { |
4171 | 0 | p2p_dbg(p2p, |
4172 | 0 | "Unexpected listen failed callback for freq=%u (pending_listen_freq=%u)", |
4173 | 0 | freq, p2p->pending_listen_freq); |
4174 | 0 | return; |
4175 | 0 | } |
4176 | | |
4177 | 0 | p2p_dbg(p2p, "Listen failed on freq=%u", freq); |
4178 | 0 | p2p->pending_listen_freq = 0; |
4179 | 0 | } |
4180 | | |
4181 | | |
4182 | | static void p2p_timeout_connect(struct p2p_data *p2p) |
4183 | 0 | { |
4184 | 0 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
4185 | 0 | if (p2p->go_neg_peer && |
4186 | 0 | (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) { |
4187 | 0 | p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed"); |
4188 | 0 | p2p_go_neg_failed(p2p, -1); |
4189 | 0 | return; |
4190 | 0 | } |
4191 | 0 | if (p2p->go_neg_peer && |
4192 | 0 | (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) && |
4193 | 0 | p2p->go_neg_peer->connect_reqs < 120) { |
4194 | 0 | p2p_dbg(p2p, "Peer expected to wait our response - skip listen"); |
4195 | 0 | p2p_connect_send(p2p, p2p->go_neg_peer); |
4196 | 0 | return; |
4197 | 0 | } |
4198 | 0 | if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) { |
4199 | 0 | p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)"); |
4200 | 0 | p2p_set_state(p2p, P2P_CONNECT_LISTEN); |
4201 | 0 | p2p_set_timeout(p2p, 0, 30000); |
4202 | 0 | return; |
4203 | 0 | } |
4204 | 0 | p2p_set_state(p2p, P2P_CONNECT_LISTEN); |
4205 | 0 | p2p_listen_in_find(p2p, 0); |
4206 | 0 | } |
4207 | | |
4208 | | |
4209 | | static void p2p_timeout_connect_listen(struct p2p_data *p2p) |
4210 | 0 | { |
4211 | 0 | if (p2p->go_neg_peer) { |
4212 | 0 | if (p2p->drv_in_listen) { |
4213 | 0 | p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete"); |
4214 | 0 | return; |
4215 | 0 | } |
4216 | | |
4217 | 0 | if (p2p->go_neg_peer->connect_reqs >= 120) { |
4218 | 0 | p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response"); |
4219 | 0 | p2p_go_neg_failed(p2p, -1); |
4220 | 0 | return; |
4221 | 0 | } |
4222 | | |
4223 | 0 | p2p_set_state(p2p, P2P_CONNECT); |
4224 | 0 | p2p_connect_send(p2p, p2p->go_neg_peer); |
4225 | 0 | } else |
4226 | 0 | p2p_set_state(p2p, P2P_IDLE); |
4227 | 0 | } |
4228 | | |
4229 | | |
4230 | | static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p) |
4231 | 0 | { |
4232 | 0 | p2p_set_state(p2p, P2P_WAIT_PEER_IDLE); |
4233 | |
|
4234 | 0 | if (p2p->cfg->is_concurrent_session_active && |
4235 | 0 | p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx)) |
4236 | 0 | p2p_set_timeout(p2p, 0, 500000); |
4237 | 0 | else |
4238 | 0 | p2p_set_timeout(p2p, 0, 200000); |
4239 | 0 | } |
4240 | | |
4241 | | |
4242 | | static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p) |
4243 | 0 | { |
4244 | 0 | struct p2p_device *dev = p2p->go_neg_peer; |
4245 | |
|
4246 | 0 | if (dev == NULL) { |
4247 | 0 | p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait"); |
4248 | 0 | return; |
4249 | 0 | } |
4250 | | |
4251 | 0 | p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation"); |
4252 | 0 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
4253 | 0 | p2p->pending_listen_wait_drv = false; |
4254 | 0 | if (p2p->pending_listen_freq) { |
4255 | 0 | p2p_dbg(p2p, "Clear pending_listen_freq for %s", __func__); |
4256 | 0 | p2p->pending_listen_freq = 0; |
4257 | 0 | } |
4258 | 0 | p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT); |
4259 | 0 | p2p_listen_in_find(p2p, 0); |
4260 | 0 | } |
4261 | | |
4262 | | |
4263 | | static void p2p_timeout_sd_during_find(struct p2p_data *p2p) |
4264 | 0 | { |
4265 | 0 | p2p_dbg(p2p, "Service Discovery Query timeout"); |
4266 | 0 | if (p2p->sd_peer) { |
4267 | 0 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
4268 | 0 | p2p->sd_peer = NULL; |
4269 | 0 | } |
4270 | 0 | p2p_continue_find(p2p); |
4271 | 0 | } |
4272 | | |
4273 | | |
4274 | | static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p) |
4275 | 0 | { |
4276 | 0 | p2p_dbg(p2p, "Provision Discovery Request timeout"); |
4277 | 0 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
4278 | 0 | p2p_continue_find(p2p); |
4279 | 0 | } |
4280 | | |
4281 | | |
4282 | | static void p2p_timeout_prov_disc_req(struct p2p_data *p2p) |
4283 | 0 | { |
4284 | 0 | u32 adv_id = 0; |
4285 | 0 | u8 *adv_mac = NULL; |
4286 | |
|
4287 | 0 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
4288 | | |
4289 | | /* |
4290 | | * For user initiated PD requests that we have not gotten any responses |
4291 | | * for while in IDLE state, we retry them a couple of times before |
4292 | | * giving up. |
4293 | | */ |
4294 | 0 | if (!p2p->user_initiated_pd) |
4295 | 0 | return; |
4296 | | |
4297 | 0 | p2p_dbg(p2p, "User initiated Provision Discovery Request timeout"); |
4298 | |
|
4299 | 0 | if (p2p->pd_retries) { |
4300 | 0 | p2p->pd_retries--; |
4301 | 0 | p2p_retry_pd(p2p); |
4302 | 0 | } else { |
4303 | 0 | struct p2p_device *dev; |
4304 | 0 | int for_join = 0; |
4305 | |
|
4306 | 0 | dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { |
4307 | 0 | if (!ether_addr_equal(p2p->pending_pd_devaddr, |
4308 | 0 | dev->info.p2p_device_addr)) |
4309 | 0 | continue; |
4310 | 0 | if (dev->req_config_methods && |
4311 | 0 | (dev->flags & P2P_DEV_PD_FOR_JOIN)) |
4312 | 0 | for_join = 1; |
4313 | 0 | } |
4314 | |
|
4315 | 0 | if (p2p->p2ps_prov) { |
4316 | 0 | adv_id = p2p->p2ps_prov->adv_id; |
4317 | 0 | adv_mac = p2p->p2ps_prov->adv_mac; |
4318 | 0 | } |
4319 | |
|
4320 | 0 | if (p2p->cfg->prov_disc_fail) |
4321 | 0 | p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, |
4322 | 0 | p2p->pending_pd_devaddr, |
4323 | 0 | for_join ? |
4324 | 0 | P2P_PROV_DISC_TIMEOUT_JOIN : |
4325 | 0 | P2P_PROV_DISC_TIMEOUT, |
4326 | 0 | adv_id, adv_mac, NULL); |
4327 | 0 | p2p_reset_pending_pd(p2p); |
4328 | 0 | } |
4329 | 0 | } |
4330 | | |
4331 | | |
4332 | | static void p2p_timeout_invite(struct p2p_data *p2p) |
4333 | 0 | { |
4334 | 0 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
4335 | 0 | p2p_set_state(p2p, P2P_INVITE_LISTEN); |
4336 | 0 | if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) { |
4337 | | /* |
4338 | | * Better remain on operating channel instead of listen channel |
4339 | | * when running a group. |
4340 | | * Wait 120 ms to let the P2P GO to send its beacon on the |
4341 | | * intended TBTT. |
4342 | | */ |
4343 | 0 | p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel"); |
4344 | 0 | p2p_set_timeout(p2p, 0, 120000); |
4345 | 0 | return; |
4346 | 0 | } |
4347 | 0 | p2p_listen_in_find(p2p, 0); |
4348 | 0 | } |
4349 | | |
4350 | | |
4351 | | static void p2p_timeout_invite_listen(struct p2p_data *p2p) |
4352 | 0 | { |
4353 | 0 | if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) { |
4354 | 0 | p2p_set_state(p2p, P2P_INVITE); |
4355 | 0 | p2p_invite_send(p2p, p2p->invite_peer, |
4356 | 0 | p2p->invite_go_dev_addr, p2p->invite_dev_pw_id); |
4357 | 0 | } else { |
4358 | 0 | if (p2p->invite_peer) { |
4359 | 0 | p2p_dbg(p2p, "Invitation Request retry limit reached"); |
4360 | 0 | if (p2p->cfg->invitation_result) |
4361 | 0 | p2p->cfg->invitation_result( |
4362 | 0 | p2p->cfg->cb_ctx, -1, NULL, 0, NULL, |
4363 | 0 | NULL, |
4364 | 0 | p2p->invite_peer->info.p2p_device_addr, |
4365 | 0 | 0, 0, NULL, NULL, 0, |
4366 | 0 | p2p->invite_go_dev_addr); |
4367 | 0 | } |
4368 | 0 | p2p_set_state(p2p, P2P_IDLE); |
4369 | 0 | } |
4370 | 0 | } |
4371 | | |
4372 | | |
4373 | | static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx) |
4374 | 0 | { |
4375 | 0 | struct p2p_data *p2p = eloop_ctx; |
4376 | |
|
4377 | 0 | p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state)); |
4378 | |
|
4379 | 0 | p2p->in_listen = 0; |
4380 | 0 | if (p2p->drv_in_listen) { |
4381 | 0 | p2p_dbg(p2p, "Driver is still in listen state - stop it"); |
4382 | 0 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
4383 | 0 | p2p->pending_listen_wait_drv = false; |
4384 | 0 | } |
4385 | |
|
4386 | 0 | switch (p2p->state) { |
4387 | 0 | case P2P_IDLE: |
4388 | | /* Check if we timed out waiting for PD req */ |
4389 | 0 | if (p2p->pending_action_state == P2P_PENDING_PD) |
4390 | 0 | p2p_timeout_prov_disc_req(p2p); |
4391 | 0 | break; |
4392 | 0 | case P2P_SEARCH: |
4393 | | /* Check if we timed out waiting for PD req */ |
4394 | 0 | if (p2p->pending_action_state == P2P_PENDING_PD) |
4395 | 0 | p2p_timeout_prov_disc_req(p2p); |
4396 | 0 | if (p2p->search_delay && !p2p->in_search_delay) { |
4397 | 0 | p2p_dbg(p2p, "Delay search operation by %u ms", |
4398 | 0 | p2p->search_delay); |
4399 | 0 | p2p->in_search_delay = 1; |
4400 | 0 | p2p_set_timeout(p2p, p2p->search_delay / 1000, |
4401 | 0 | (p2p->search_delay % 1000) * 1000); |
4402 | 0 | break; |
4403 | 0 | } |
4404 | 0 | p2p->in_search_delay = 0; |
4405 | 0 | p2p_search(p2p); |
4406 | 0 | break; |
4407 | 0 | case P2P_CONNECT: |
4408 | 0 | p2p_timeout_connect(p2p); |
4409 | 0 | break; |
4410 | 0 | case P2P_CONNECT_LISTEN: |
4411 | 0 | p2p_timeout_connect_listen(p2p); |
4412 | 0 | break; |
4413 | 0 | case P2P_GO_NEG: |
4414 | 0 | break; |
4415 | 0 | case P2P_LISTEN_ONLY: |
4416 | | /* Check if we timed out waiting for PD req */ |
4417 | 0 | if (p2p->pending_action_state == P2P_PENDING_PD) |
4418 | 0 | p2p_timeout_prov_disc_req(p2p); |
4419 | |
|
4420 | 0 | if (p2p->ext_listen_only) { |
4421 | 0 | p2p_dbg(p2p, "Extended Listen Timing - Listen State completed"); |
4422 | 0 | p2p->ext_listen_only = 0; |
4423 | 0 | p2p_set_state(p2p, P2P_IDLE); |
4424 | 0 | } |
4425 | 0 | break; |
4426 | 0 | case P2P_WAIT_PEER_CONNECT: |
4427 | 0 | p2p_timeout_wait_peer_connect(p2p); |
4428 | 0 | break; |
4429 | 0 | case P2P_WAIT_PEER_IDLE: |
4430 | 0 | p2p_timeout_wait_peer_idle(p2p); |
4431 | 0 | break; |
4432 | 0 | case P2P_SD_DURING_FIND: |
4433 | 0 | p2p_timeout_sd_during_find(p2p); |
4434 | 0 | break; |
4435 | 0 | case P2P_PROVISIONING: |
4436 | 0 | break; |
4437 | 0 | case P2P_PD_DURING_FIND: |
4438 | 0 | p2p_timeout_prov_disc_during_find(p2p); |
4439 | 0 | break; |
4440 | 0 | case P2P_INVITE: |
4441 | 0 | p2p_timeout_invite(p2p); |
4442 | 0 | break; |
4443 | 0 | case P2P_INVITE_LISTEN: |
4444 | 0 | p2p_timeout_invite_listen(p2p); |
4445 | 0 | break; |
4446 | 0 | } |
4447 | 0 | } |
4448 | | |
4449 | | |
4450 | | int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr) |
4451 | 0 | { |
4452 | 0 | struct p2p_device *dev; |
4453 | |
|
4454 | 0 | dev = p2p_get_device(p2p, peer_addr); |
4455 | 0 | p2p_dbg(p2p, "Local request to reject connection attempts by peer " |
4456 | 0 | MACSTR, MAC2STR(peer_addr)); |
4457 | 0 | if (dev == NULL) { |
4458 | 0 | p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr)); |
4459 | 0 | return -1; |
4460 | 0 | } |
4461 | 0 | dev->status = P2P_SC_FAIL_REJECTED_BY_USER; |
4462 | 0 | dev->flags |= P2P_DEV_USER_REJECTED; |
4463 | 0 | return 0; |
4464 | 0 | } |
4465 | | |
4466 | | |
4467 | | const char * p2p_wps_method_text(enum p2p_wps_method method) |
4468 | 0 | { |
4469 | 0 | switch (method) { |
4470 | 0 | case WPS_NOT_READY: |
4471 | 0 | return "not-ready"; |
4472 | 0 | case WPS_PIN_DISPLAY: |
4473 | 0 | return "Display"; |
4474 | 0 | case WPS_PIN_KEYPAD: |
4475 | 0 | return "Keypad"; |
4476 | 0 | case WPS_PBC: |
4477 | 0 | return "PBC"; |
4478 | 0 | case WPS_NFC: |
4479 | 0 | return "NFC"; |
4480 | 0 | case WPS_P2PS: |
4481 | 0 | return "P2PS"; |
4482 | 0 | } |
4483 | | |
4484 | 0 | return "??"; |
4485 | 0 | } |
4486 | | |
4487 | | |
4488 | | static const char * p2p_go_state_text(enum p2p_go_state go_state) |
4489 | 0 | { |
4490 | 0 | switch (go_state) { |
4491 | 0 | case UNKNOWN_GO: |
4492 | 0 | return "unknown"; |
4493 | 0 | case LOCAL_GO: |
4494 | 0 | return "local"; |
4495 | 0 | case REMOTE_GO: |
4496 | 0 | return "remote"; |
4497 | 0 | } |
4498 | | |
4499 | 0 | return "??"; |
4500 | 0 | } |
4501 | | |
4502 | | |
4503 | | const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p, |
4504 | | const u8 *addr, int next) |
4505 | 0 | { |
4506 | 0 | struct p2p_device *dev; |
4507 | |
|
4508 | 0 | if (addr) |
4509 | 0 | dev = p2p_get_device(p2p, addr); |
4510 | 0 | else |
4511 | 0 | dev = dl_list_first(&p2p->devices, struct p2p_device, list); |
4512 | |
|
4513 | 0 | if (dev && next) { |
4514 | 0 | dev = dl_list_first(&dev->list, struct p2p_device, list); |
4515 | 0 | if (&dev->list == &p2p->devices) |
4516 | 0 | dev = NULL; |
4517 | 0 | } |
4518 | |
|
4519 | 0 | if (dev == NULL) |
4520 | 0 | return NULL; |
4521 | | |
4522 | 0 | return &dev->info; |
4523 | 0 | } |
4524 | | |
4525 | | |
4526 | | int p2p_get_peer_info_txt(const struct p2p_peer_info *info, |
4527 | | char *buf, size_t buflen) |
4528 | 0 | { |
4529 | 0 | struct p2p_device *dev; |
4530 | 0 | int res; |
4531 | 0 | char *pos, *end; |
4532 | 0 | struct os_reltime now; |
4533 | |
|
4534 | 0 | if (info == NULL) |
4535 | 0 | return -1; |
4536 | | |
4537 | 0 | dev = (struct p2p_device *) (((u8 *) info) - |
4538 | 0 | offsetof(struct p2p_device, info)); |
4539 | |
|
4540 | 0 | pos = buf; |
4541 | 0 | end = buf + buflen; |
4542 | |
|
4543 | 0 | os_get_reltime(&now); |
4544 | 0 | res = os_snprintf(pos, end - pos, |
4545 | 0 | "age=%d\n" |
4546 | 0 | "listen_freq=%d\n" |
4547 | 0 | "wps_method=%s\n" |
4548 | 0 | "interface_addr=" MACSTR "\n" |
4549 | 0 | "member_in_go_dev=" MACSTR "\n" |
4550 | 0 | "member_in_go_iface=" MACSTR "\n" |
4551 | 0 | "go_neg_req_sent=%d\n" |
4552 | 0 | "go_state=%s\n" |
4553 | 0 | "dialog_token=%u\n" |
4554 | 0 | "intended_addr=" MACSTR "\n" |
4555 | 0 | "country=%c%c\n" |
4556 | 0 | "oper_freq=%d\n" |
4557 | 0 | "req_config_methods=0x%x\n" |
4558 | 0 | "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n" |
4559 | 0 | "status=%d\n" |
4560 | 0 | "invitation_reqs=%u\n", |
4561 | 0 | (int) (now.sec - dev->last_seen.sec), |
4562 | 0 | dev->listen_freq, |
4563 | 0 | p2p_wps_method_text(dev->wps_method), |
4564 | 0 | MAC2STR(dev->interface_addr), |
4565 | 0 | MAC2STR(dev->member_in_go_dev), |
4566 | 0 | MAC2STR(dev->member_in_go_iface), |
4567 | 0 | dev->go_neg_req_sent, |
4568 | 0 | p2p_go_state_text(dev->go_state), |
4569 | 0 | dev->dialog_token, |
4570 | 0 | MAC2STR(dev->intended_addr), |
4571 | 0 | dev->country[0] ? dev->country[0] : '_', |
4572 | 0 | dev->country[1] ? dev->country[1] : '_', |
4573 | 0 | dev->oper_freq, |
4574 | 0 | dev->req_config_methods, |
4575 | 0 | dev->flags & P2P_DEV_PROBE_REQ_ONLY ? |
4576 | 0 | "[PROBE_REQ_ONLY]" : "", |
4577 | 0 | dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "", |
4578 | 0 | dev->flags & P2P_DEV_NOT_YET_READY ? |
4579 | 0 | "[NOT_YET_READY]" : "", |
4580 | 0 | dev->flags & P2P_DEV_PD_PEER_DISPLAY ? |
4581 | 0 | "[PD_PEER_DISPLAY]" : "", |
4582 | 0 | dev->flags & P2P_DEV_PD_PEER_KEYPAD ? |
4583 | 0 | "[PD_PEER_KEYPAD]" : "", |
4584 | 0 | dev->flags & P2P_DEV_PD_PEER_P2PS ? |
4585 | 0 | "[PD_PEER_P2PS]" : "", |
4586 | 0 | dev->flags & P2P_DEV_USER_REJECTED ? |
4587 | 0 | "[USER_REJECTED]" : "", |
4588 | 0 | dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ? |
4589 | 0 | "[PEER_WAITING_RESPONSE]" : "", |
4590 | 0 | dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ? |
4591 | 0 | "[PREFER_PERSISTENT_GROUP]" : "", |
4592 | 0 | dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ? |
4593 | 0 | "[WAIT_GO_NEG_RESPONSE]" : "", |
4594 | 0 | dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ? |
4595 | 0 | "[WAIT_GO_NEG_CONFIRM]" : "", |
4596 | 0 | dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ? |
4597 | 0 | "[GROUP_CLIENT_ONLY]" : "", |
4598 | 0 | dev->flags & P2P_DEV_FORCE_FREQ ? |
4599 | 0 | "[FORCE_FREQ]" : "", |
4600 | 0 | dev->flags & P2P_DEV_PD_FOR_JOIN ? |
4601 | 0 | "[PD_FOR_JOIN]" : "", |
4602 | 0 | dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ? |
4603 | 0 | "[LAST_SEEN_AS_GROUP_CLIENT]" : "", |
4604 | 0 | dev->status, |
4605 | 0 | dev->invitation_reqs); |
4606 | 0 | if (os_snprintf_error(end - pos, res)) |
4607 | 0 | return pos - buf; |
4608 | 0 | pos += res; |
4609 | |
|
4610 | 0 | if (dev->ext_listen_period) { |
4611 | 0 | res = os_snprintf(pos, end - pos, |
4612 | 0 | "ext_listen_period=%u\n" |
4613 | 0 | "ext_listen_interval=%u\n", |
4614 | 0 | dev->ext_listen_period, |
4615 | 0 | dev->ext_listen_interval); |
4616 | 0 | if (os_snprintf_error(end - pos, res)) |
4617 | 0 | return pos - buf; |
4618 | 0 | pos += res; |
4619 | 0 | } |
4620 | | |
4621 | 0 | if (dev->oper_ssid_len) { |
4622 | 0 | res = os_snprintf(pos, end - pos, |
4623 | 0 | "oper_ssid=%s\n", |
4624 | 0 | wpa_ssid_txt(dev->oper_ssid, |
4625 | 0 | dev->oper_ssid_len)); |
4626 | 0 | if (os_snprintf_error(end - pos, res)) |
4627 | 0 | return pos - buf; |
4628 | 0 | pos += res; |
4629 | 0 | } |
4630 | | |
4631 | 0 | #ifdef CONFIG_WIFI_DISPLAY |
4632 | 0 | if (dev->info.wfd_subelems) { |
4633 | 0 | res = os_snprintf(pos, end - pos, "wfd_subelems="); |
4634 | 0 | if (os_snprintf_error(end - pos, res)) |
4635 | 0 | return pos - buf; |
4636 | 0 | pos += res; |
4637 | |
|
4638 | 0 | pos += wpa_snprintf_hex(pos, end - pos, |
4639 | 0 | wpabuf_head(dev->info.wfd_subelems), |
4640 | 0 | wpabuf_len(dev->info.wfd_subelems)); |
4641 | |
|
4642 | 0 | res = os_snprintf(pos, end - pos, "\n"); |
4643 | 0 | if (os_snprintf_error(end - pos, res)) |
4644 | 0 | return pos - buf; |
4645 | 0 | pos += res; |
4646 | 0 | } |
4647 | 0 | #endif /* CONFIG_WIFI_DISPLAY */ |
4648 | | |
4649 | 0 | return pos - buf; |
4650 | 0 | } |
4651 | | |
4652 | | |
4653 | | int p2p_peer_known(struct p2p_data *p2p, const u8 *addr) |
4654 | 0 | { |
4655 | 0 | return p2p_get_device(p2p, addr) != NULL; |
4656 | 0 | } |
4657 | | |
4658 | | |
4659 | | void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled) |
4660 | 0 | { |
4661 | 0 | if (enabled) { |
4662 | 0 | p2p_dbg(p2p, "Client discoverability enabled"); |
4663 | 0 | p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
4664 | 0 | } else { |
4665 | 0 | p2p_dbg(p2p, "Client discoverability disabled"); |
4666 | 0 | p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
4667 | 0 | } |
4668 | 0 | } |
4669 | | |
4670 | | |
4671 | | static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1, |
4672 | | u32 duration2, u32 interval2) |
4673 | 0 | { |
4674 | 0 | struct wpabuf *req; |
4675 | 0 | struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL; |
4676 | 0 | u8 *len; |
4677 | |
|
4678 | 0 | req = wpabuf_alloc(100); |
4679 | 0 | if (req == NULL) |
4680 | 0 | return NULL; |
4681 | | |
4682 | 0 | if (duration1 || interval1) { |
4683 | 0 | os_memset(&desc1, 0, sizeof(desc1)); |
4684 | 0 | desc1.count_type = 1; |
4685 | 0 | desc1.duration = duration1; |
4686 | 0 | desc1.interval = interval1; |
4687 | 0 | ptr1 = &desc1; |
4688 | |
|
4689 | 0 | if (duration2 || interval2) { |
4690 | 0 | os_memset(&desc2, 0, sizeof(desc2)); |
4691 | 0 | desc2.count_type = 2; |
4692 | 0 | desc2.duration = duration2; |
4693 | 0 | desc2.interval = interval2; |
4694 | 0 | ptr2 = &desc2; |
4695 | 0 | } |
4696 | 0 | } |
4697 | |
|
4698 | 0 | p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1); |
4699 | 0 | len = p2p_buf_add_ie_hdr(req); |
4700 | 0 | p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2); |
4701 | 0 | p2p_buf_update_ie_hdr(req, len); |
4702 | |
|
4703 | 0 | return req; |
4704 | 0 | } |
4705 | | |
4706 | | |
4707 | | int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr, |
4708 | | const u8 *own_interface_addr, unsigned int freq, |
4709 | | u32 duration1, u32 interval1, u32 duration2, |
4710 | | u32 interval2) |
4711 | 0 | { |
4712 | 0 | struct wpabuf *req; |
4713 | |
|
4714 | 0 | p2p_dbg(p2p, "Send Presence Request to GO " MACSTR |
4715 | 0 | " (own interface " MACSTR ") freq=%u dur1=%u int1=%u " |
4716 | 0 | "dur2=%u int2=%u", |
4717 | 0 | MAC2STR(go_interface_addr), MAC2STR(own_interface_addr), |
4718 | 0 | freq, duration1, interval1, duration2, interval2); |
4719 | |
|
4720 | 0 | req = p2p_build_presence_req(duration1, interval1, duration2, |
4721 | 0 | interval2); |
4722 | 0 | if (req == NULL) |
4723 | 0 | return -1; |
4724 | | |
4725 | 0 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
4726 | 0 | if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr, |
4727 | 0 | go_interface_addr, |
4728 | 0 | wpabuf_head(req), wpabuf_len(req), 200) < 0) { |
4729 | 0 | p2p_dbg(p2p, "Failed to send Action frame"); |
4730 | 0 | } |
4731 | 0 | wpabuf_free(req); |
4732 | |
|
4733 | 0 | return 0; |
4734 | 0 | } |
4735 | | |
4736 | | |
4737 | | static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa, |
4738 | | size_t noa_len, u8 dialog_token) |
4739 | 0 | { |
4740 | 0 | struct wpabuf *resp; |
4741 | 0 | u8 *len; |
4742 | |
|
4743 | 0 | resp = wpabuf_alloc(100 + noa_len); |
4744 | 0 | if (resp == NULL) |
4745 | 0 | return NULL; |
4746 | | |
4747 | 0 | p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token); |
4748 | 0 | len = p2p_buf_add_ie_hdr(resp); |
4749 | 0 | p2p_buf_add_status(resp, status); |
4750 | 0 | if (noa) { |
4751 | 0 | wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE); |
4752 | 0 | wpabuf_put_le16(resp, noa_len); |
4753 | 0 | wpabuf_put_data(resp, noa, noa_len); |
4754 | 0 | } else |
4755 | 0 | p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL); |
4756 | 0 | p2p_buf_update_ie_hdr(resp, len); |
4757 | |
|
4758 | 0 | return resp; |
4759 | 0 | } |
4760 | | |
4761 | | |
4762 | | static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, |
4763 | | const u8 *sa, const u8 *data, size_t len, |
4764 | | int rx_freq) |
4765 | 1 | { |
4766 | 1 | struct p2p_message msg; |
4767 | 1 | u8 status; |
4768 | 1 | struct wpabuf *resp; |
4769 | 1 | size_t g; |
4770 | 1 | struct p2p_group *group = NULL; |
4771 | 1 | int parsed = 0; |
4772 | 1 | u8 noa[50]; |
4773 | 1 | int noa_len; |
4774 | | |
4775 | 1 | p2p_dbg(p2p, "Received P2P Action - P2P Presence Request"); |
4776 | | |
4777 | 1 | for (g = 0; g < p2p->num_groups; g++) { |
4778 | 0 | if (ether_addr_equal( |
4779 | 0 | da, p2p_group_get_interface_addr(p2p->groups[g]))) { |
4780 | 0 | group = p2p->groups[g]; |
4781 | 0 | break; |
4782 | 0 | } |
4783 | 0 | } |
4784 | 1 | if (group == NULL) { |
4785 | 1 | p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group " |
4786 | 1 | MACSTR, MAC2STR(da)); |
4787 | 1 | return; |
4788 | 1 | } |
4789 | | |
4790 | 0 | if (p2p_parse(data, len, &msg) < 0) { |
4791 | 0 | p2p_dbg(p2p, "Failed to parse P2P Presence Request"); |
4792 | 0 | status = P2P_SC_FAIL_INVALID_PARAMS; |
4793 | 0 | goto fail; |
4794 | 0 | } |
4795 | 0 | parsed = 1; |
4796 | |
|
4797 | 0 | if (msg.noa == NULL) { |
4798 | 0 | p2p_dbg(p2p, "No NoA attribute in P2P Presence Request"); |
4799 | 0 | status = P2P_SC_FAIL_INVALID_PARAMS; |
4800 | 0 | goto fail; |
4801 | 0 | } |
4802 | | |
4803 | 0 | status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len); |
4804 | |
|
4805 | 0 | fail: |
4806 | 0 | if (p2p->cfg->get_noa) |
4807 | 0 | noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa, |
4808 | 0 | sizeof(noa)); |
4809 | 0 | else |
4810 | 0 | noa_len = -1; |
4811 | 0 | resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL, |
4812 | 0 | noa_len > 0 ? noa_len : 0, |
4813 | 0 | msg.dialog_token); |
4814 | 0 | if (parsed) |
4815 | 0 | p2p_parse_free(&msg); |
4816 | 0 | if (resp == NULL) |
4817 | 0 | return; |
4818 | | |
4819 | 0 | p2p->pending_action_state = P2P_NO_PENDING_ACTION; |
4820 | 0 | if (p2p_send_action(p2p, rx_freq, sa, da, da, |
4821 | 0 | wpabuf_head(resp), wpabuf_len(resp), 200) < 0) { |
4822 | 0 | p2p_dbg(p2p, "Failed to send Action frame"); |
4823 | 0 | } |
4824 | 0 | wpabuf_free(resp); |
4825 | 0 | } |
4826 | | |
4827 | | |
4828 | | static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, |
4829 | | const u8 *sa, const u8 *data, size_t len) |
4830 | 588 | { |
4831 | 588 | struct p2p_message msg; |
4832 | | |
4833 | 588 | p2p_dbg(p2p, "Received P2P Action - P2P Presence Response"); |
4834 | | |
4835 | 588 | if (p2p_parse(data, len, &msg) < 0) { |
4836 | 255 | p2p_dbg(p2p, "Failed to parse P2P Presence Response"); |
4837 | 255 | return; |
4838 | 255 | } |
4839 | | |
4840 | 333 | if (msg.status == NULL || msg.noa == NULL) { |
4841 | 327 | p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response"); |
4842 | 327 | p2p_parse_free(&msg); |
4843 | 327 | return; |
4844 | 327 | } |
4845 | | |
4846 | 6 | if (p2p->cfg->presence_resp) { |
4847 | 0 | p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status, |
4848 | 0 | msg.noa, msg.noa_len); |
4849 | 0 | } |
4850 | | |
4851 | 6 | if (*msg.status) { |
4852 | 5 | p2p_dbg(p2p, "P2P Presence Request was rejected: status %u", |
4853 | 5 | *msg.status); |
4854 | 5 | p2p_parse_free(&msg); |
4855 | 5 | return; |
4856 | 5 | } |
4857 | | |
4858 | 1 | p2p_dbg(p2p, "P2P Presence Request was accepted"); |
4859 | 1 | wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA", |
4860 | 1 | msg.noa, msg.noa_len); |
4861 | | /* TODO: process NoA */ |
4862 | 1 | p2p_parse_free(&msg); |
4863 | 1 | } |
4864 | | |
4865 | | |
4866 | | static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx) |
4867 | 0 | { |
4868 | 0 | struct p2p_data *p2p = eloop_ctx; |
4869 | |
|
4870 | 0 | if (p2p->ext_listen_interval) { |
4871 | | /* Schedule next extended listen timeout */ |
4872 | 0 | eloop_register_timeout(p2p->ext_listen_interval_sec, |
4873 | 0 | p2p->ext_listen_interval_usec, |
4874 | 0 | p2p_ext_listen_timeout, p2p, NULL); |
4875 | 0 | } |
4876 | |
|
4877 | 0 | if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) { |
4878 | | /* |
4879 | | * This should not really happen, but it looks like the Listen |
4880 | | * command may fail is something else (e.g., a scan) was |
4881 | | * running at an inconvenient time. As a workaround, allow new |
4882 | | * Extended Listen operation to be started. |
4883 | | */ |
4884 | 0 | p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again"); |
4885 | 0 | p2p->ext_listen_only = 0; |
4886 | 0 | p2p_set_state(p2p, P2P_IDLE); |
4887 | 0 | } |
4888 | |
|
4889 | 0 | if ((p2p->cfg->is_p2p_in_progress && |
4890 | 0 | p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) || |
4891 | 0 | (p2p->pending_action_state == P2P_PENDING_PD && |
4892 | 0 | p2p->pd_retries > 0)) { |
4893 | 0 | p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)", |
4894 | 0 | p2p_state_txt(p2p->state)); |
4895 | 0 | return; |
4896 | 0 | } |
4897 | | |
4898 | 0 | if (p2p->state != P2P_IDLE) { |
4899 | 0 | p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state)); |
4900 | 0 | return; |
4901 | 0 | } |
4902 | | |
4903 | 0 | p2p_dbg(p2p, "Extended Listen timeout"); |
4904 | 0 | p2p->ext_listen_only = 1; |
4905 | 0 | if (p2p_listen(p2p, p2p->ext_listen_period) < 0) { |
4906 | 0 | p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing"); |
4907 | 0 | p2p->ext_listen_only = 0; |
4908 | 0 | } |
4909 | 0 | } |
4910 | | |
4911 | | |
4912 | | int p2p_ext_listen(struct p2p_data *p2p, unsigned int period, |
4913 | | unsigned int interval) |
4914 | 6.57k | { |
4915 | 6.57k | if (period > 65535 || interval > 65535 || period > interval || |
4916 | 6.57k | (period == 0 && interval > 0) || (period > 0 && interval == 0)) { |
4917 | 0 | p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u", |
4918 | 0 | period, interval); |
4919 | 0 | return -1; |
4920 | 0 | } |
4921 | | |
4922 | 6.57k | eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); |
4923 | | |
4924 | 6.57k | if (interval == 0) { |
4925 | 6.57k | p2p_dbg(p2p, "Disabling Extended Listen Timing"); |
4926 | 6.57k | p2p->ext_listen_period = 0; |
4927 | 6.57k | p2p->ext_listen_interval = 0; |
4928 | 6.57k | return 0; |
4929 | 6.57k | } |
4930 | | |
4931 | 0 | p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec", |
4932 | 0 | period, interval); |
4933 | 0 | p2p->ext_listen_period = period; |
4934 | 0 | p2p->ext_listen_interval = interval; |
4935 | 0 | p2p->ext_listen_interval_sec = interval / 1000; |
4936 | 0 | p2p->ext_listen_interval_usec = (interval % 1000) * 1000; |
4937 | |
|
4938 | 0 | eloop_register_timeout(p2p->ext_listen_interval_sec, |
4939 | 0 | p2p->ext_listen_interval_usec, |
4940 | 0 | p2p_ext_listen_timeout, p2p, NULL); |
4941 | |
|
4942 | 0 | return 0; |
4943 | 6.57k | } |
4944 | | |
4945 | | |
4946 | | void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, |
4947 | | const u8 *ie, size_t ie_len) |
4948 | 0 | { |
4949 | 0 | struct p2p_message msg; |
4950 | |
|
4951 | 0 | if (bssid == NULL || ie == NULL) |
4952 | 0 | return; |
4953 | | |
4954 | 0 | os_memset(&msg, 0, sizeof(msg)); |
4955 | 0 | if (p2p_parse_ies(ie, ie_len, &msg)) |
4956 | 0 | return; |
4957 | 0 | if (msg.minor_reason_code == NULL) { |
4958 | 0 | p2p_parse_free(&msg); |
4959 | 0 | return; |
4960 | 0 | } |
4961 | | |
4962 | 0 | p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR |
4963 | 0 | " reason_code=%u minor_reason_code=%u", |
4964 | 0 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); |
4965 | |
|
4966 | 0 | p2p_parse_free(&msg); |
4967 | 0 | } |
4968 | | |
4969 | | |
4970 | | void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, |
4971 | | const u8 *ie, size_t ie_len) |
4972 | 0 | { |
4973 | 0 | struct p2p_message msg; |
4974 | |
|
4975 | 0 | if (bssid == NULL || ie == NULL) |
4976 | 0 | return; |
4977 | | |
4978 | 0 | os_memset(&msg, 0, sizeof(msg)); |
4979 | 0 | if (p2p_parse_ies(ie, ie_len, &msg)) |
4980 | 0 | return; |
4981 | 0 | if (msg.minor_reason_code == NULL) { |
4982 | 0 | p2p_parse_free(&msg); |
4983 | 0 | return; |
4984 | 0 | } |
4985 | | |
4986 | 0 | p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR |
4987 | 0 | " reason_code=%u minor_reason_code=%u", |
4988 | 0 | MAC2STR(bssid), reason_code, *msg.minor_reason_code); |
4989 | |
|
4990 | 0 | p2p_parse_free(&msg); |
4991 | 0 | } |
4992 | | |
4993 | | |
4994 | | void p2p_set_managed_oper(struct p2p_data *p2p, int enabled) |
4995 | 0 | { |
4996 | 0 | if (enabled) { |
4997 | 0 | p2p_dbg(p2p, "Managed P2P Device operations enabled"); |
4998 | 0 | p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED; |
4999 | 0 | } else { |
5000 | 0 | p2p_dbg(p2p, "Managed P2P Device operations disabled"); |
5001 | 0 | p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED; |
5002 | 0 | } |
5003 | 0 | } |
5004 | | |
5005 | | |
5006 | | void p2p_set_bootstrapmethods(struct p2p_data *p2p, int bootstrap_methods) |
5007 | 0 | { |
5008 | 0 | p2p_dbg(p2p, "Bootstraping methods: 0x%x", bootstrap_methods); |
5009 | 0 | p2p->cfg->pairing_config.bootstrap_methods = bootstrap_methods; |
5010 | 0 | if (p2p->pairing_info) |
5011 | 0 | p2p->pairing_info->supported_bootstrap = bootstrap_methods; |
5012 | 0 | } |
5013 | | |
5014 | | |
5015 | | void p2p_set_pasn_type(struct p2p_data *p2p, u8 pasn_type) |
5016 | 0 | { |
5017 | 0 | p2p_dbg(p2p, "PASN type: 0x%x", pasn_type); |
5018 | 0 | p2p->cfg->pairing_config.pasn_type = pasn_type; |
5019 | 0 | } |
5020 | | |
5021 | | |
5022 | | void p2p_set_comeback_after(struct p2p_data *p2p, int comeback_after) |
5023 | 0 | { |
5024 | 0 | p2p_dbg(p2p, "Comeback after: %d", comeback_after); |
5025 | 0 | p2p->cfg->comeback_after = comeback_after; |
5026 | 0 | } |
5027 | | |
5028 | | |
5029 | | void p2p_set_reg_info(struct p2p_data *p2p, u8 val) |
5030 | 0 | { |
5031 | 0 | p2p->cfg->reg_info = val; |
5032 | 0 | } |
5033 | | |
5034 | | |
5035 | | void p2p_set_twt_power_mgmt(struct p2p_data *p2p, int val) |
5036 | 0 | { |
5037 | 0 | p2p_dbg(p2p, "TWT-based P2P Power Mgmt: %s", |
5038 | 0 | val ? "Enabled" : "Disabled"); |
5039 | 0 | if (val) |
5040 | 0 | p2p->cfg->twt_power_mgmt = true; |
5041 | 0 | else |
5042 | 0 | p2p->cfg->twt_power_mgmt = false; |
5043 | 0 | } |
5044 | | |
5045 | | |
5046 | | void p2p_set_chan_switch_req_enable(struct p2p_data *p2p, bool val) |
5047 | 0 | { |
5048 | 0 | p2p->cfg->chan_switch_req_enable = val; |
5049 | 0 | } |
5050 | | |
5051 | | |
5052 | | void p2p_set_invitation_op_freq(struct p2p_data *p2p, int freq) |
5053 | 0 | { |
5054 | 0 | u8 op_class, channel; |
5055 | |
|
5056 | 0 | if (freq == -1) { |
5057 | 0 | p2p->cfg->inv_op_class = 0; |
5058 | 0 | p2p->cfg->inv_op_channel = 0; |
5059 | 0 | return; |
5060 | 0 | } |
5061 | | |
5062 | 0 | if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) |
5063 | 0 | return; |
5064 | | |
5065 | 0 | p2p->cfg->inv_op_class = op_class; |
5066 | 0 | p2p->cfg->inv_op_channel = channel; |
5067 | 0 | } |
5068 | | |
5069 | | |
5070 | | int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class, |
5071 | | u8 *op_channel, |
5072 | | struct wpa_freq_range_list *avoid_list, |
5073 | | struct wpa_freq_range_list *disallow_list) |
5074 | 0 | { |
5075 | 0 | return p2p_channel_random_social(&p2p->channels, op_class, op_channel, |
5076 | 0 | avoid_list, disallow_list); |
5077 | 0 | } |
5078 | | |
5079 | | |
5080 | | int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel, |
5081 | | u8 forced) |
5082 | 0 | { |
5083 | 0 | if (p2p_channel_to_freq(reg_class, channel) < 0) |
5084 | 0 | return -1; |
5085 | | |
5086 | | /* |
5087 | | * Listen channel was set in configuration or set by control interface; |
5088 | | * cannot override it. |
5089 | | */ |
5090 | 0 | if (p2p->cfg->channel_forced && forced == 0) { |
5091 | 0 | p2p_dbg(p2p, |
5092 | 0 | "Listen channel was previously configured - do not override based on optimization"); |
5093 | 0 | return -1; |
5094 | 0 | } |
5095 | | |
5096 | 0 | p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u", |
5097 | 0 | reg_class, channel); |
5098 | |
|
5099 | 0 | if (p2p->state == P2P_IDLE) { |
5100 | 0 | p2p->cfg->reg_class = reg_class; |
5101 | 0 | p2p->cfg->channel = channel; |
5102 | 0 | p2p->cfg->channel_forced = forced; |
5103 | 0 | } else { |
5104 | 0 | p2p_dbg(p2p, "Defer setting listen channel"); |
5105 | 0 | p2p->pending_reg_class = reg_class; |
5106 | 0 | p2p->pending_channel = channel; |
5107 | 0 | p2p->pending_channel_forced = forced; |
5108 | 0 | } |
5109 | |
|
5110 | 0 | return 0; |
5111 | 0 | } |
5112 | | |
5113 | | |
5114 | | u8 p2p_get_listen_channel(struct p2p_data *p2p) |
5115 | 0 | { |
5116 | 0 | return p2p->cfg->channel; |
5117 | 0 | } |
5118 | | |
5119 | | |
5120 | | int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len) |
5121 | 0 | { |
5122 | 0 | p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len)); |
5123 | 0 | if (postfix == NULL) { |
5124 | 0 | p2p->cfg->ssid_postfix_len = 0; |
5125 | 0 | return 0; |
5126 | 0 | } |
5127 | 0 | if (len > sizeof(p2p->cfg->ssid_postfix)) |
5128 | 0 | return -1; |
5129 | 0 | os_memcpy(p2p->cfg->ssid_postfix, postfix, len); |
5130 | 0 | p2p->cfg->ssid_postfix_len = len; |
5131 | 0 | return 0; |
5132 | 0 | } |
5133 | | |
5134 | | |
5135 | | int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel, |
5136 | | int cfg_op_channel) |
5137 | 0 | { |
5138 | 0 | if (p2p_channel_to_freq(op_reg_class, op_channel) < 0) |
5139 | 0 | return -1; |
5140 | | |
5141 | 0 | p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u", |
5142 | 0 | op_reg_class, op_channel); |
5143 | 0 | p2p->cfg->op_reg_class = op_reg_class; |
5144 | 0 | p2p->cfg->op_channel = op_channel; |
5145 | 0 | p2p->cfg->cfg_op_channel = cfg_op_channel; |
5146 | 0 | return 0; |
5147 | 0 | } |
5148 | | |
5149 | | |
5150 | | int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan, |
5151 | | const struct p2p_channel *pref_chan) |
5152 | 0 | { |
5153 | 0 | struct p2p_channel *n; |
5154 | |
|
5155 | 0 | if (pref_chan) { |
5156 | 0 | n = os_memdup(pref_chan, |
5157 | 0 | num_pref_chan * sizeof(struct p2p_channel)); |
5158 | 0 | if (n == NULL) |
5159 | 0 | return -1; |
5160 | 0 | } else |
5161 | 0 | n = NULL; |
5162 | | |
5163 | 0 | os_free(p2p->cfg->pref_chan); |
5164 | 0 | p2p->cfg->pref_chan = n; |
5165 | 0 | p2p->cfg->num_pref_chan = num_pref_chan; |
5166 | |
|
5167 | 0 | return 0; |
5168 | 0 | } |
5169 | | |
5170 | | |
5171 | | int p2p_set_no_go_freq(struct p2p_data *p2p, |
5172 | | const struct wpa_freq_range_list *list) |
5173 | 0 | { |
5174 | 0 | struct wpa_freq_range *tmp; |
5175 | |
|
5176 | 0 | if (list == NULL || list->num == 0) { |
5177 | 0 | os_free(p2p->no_go_freq.range); |
5178 | 0 | p2p->no_go_freq.range = NULL; |
5179 | 0 | p2p->no_go_freq.num = 0; |
5180 | 0 | return 0; |
5181 | 0 | } |
5182 | | |
5183 | 0 | tmp = os_calloc(list->num, sizeof(struct wpa_freq_range)); |
5184 | 0 | if (tmp == NULL) |
5185 | 0 | return -1; |
5186 | 0 | os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range)); |
5187 | 0 | os_free(p2p->no_go_freq.range); |
5188 | 0 | p2p->no_go_freq.range = tmp; |
5189 | 0 | p2p->no_go_freq.num = list->num; |
5190 | 0 | p2p_dbg(p2p, "Updated no GO chan list"); |
5191 | |
|
5192 | 0 | return 0; |
5193 | 0 | } |
5194 | | |
5195 | | |
5196 | | int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr, |
5197 | | u8 *iface_addr) |
5198 | 0 | { |
5199 | 0 | struct p2p_device *dev = p2p_get_device(p2p, dev_addr); |
5200 | |
|
5201 | 0 | if (!dev || is_zero_ether_addr(dev->interface_addr)) { |
5202 | 0 | p2p_dbg(p2p, |
5203 | 0 | "P2P: Failed to get interface address from device addr " |
5204 | 0 | MACSTR, MAC2STR(dev_addr)); |
5205 | 0 | return -1; |
5206 | 0 | } |
5207 | 0 | os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN); |
5208 | 0 | return 0; |
5209 | 0 | } |
5210 | | |
5211 | | |
5212 | | int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr, |
5213 | | u8 *dev_addr) |
5214 | 0 | { |
5215 | 0 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); |
5216 | |
|
5217 | 0 | if (!dev) { |
5218 | 0 | p2p_dbg(p2p, |
5219 | 0 | "P2P: Failed to get device address from interface address " |
5220 | 0 | MACSTR, MAC2STR(iface_addr)); |
5221 | 0 | return -1; |
5222 | 0 | } |
5223 | 0 | os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN); |
5224 | 0 | return 0; |
5225 | 0 | } |
5226 | | |
5227 | | |
5228 | | int p2p_get_dev_identity_key(struct p2p_data *p2p, const u8 *dev_addr, |
5229 | | const u8 **dik_data, size_t *dik_len, u8 *cipher) |
5230 | 0 | { |
5231 | 0 | if (!p2p || !p2p->peer_dik_len) { |
5232 | 0 | wpa_printf(MSG_DEBUG, |
5233 | 0 | "P2P2: Failed to get device identity key for " |
5234 | 0 | MACSTR, MAC2STR(dev_addr)); |
5235 | 0 | return -1; |
5236 | 0 | } |
5237 | | |
5238 | 0 | *dik_data = p2p->peer_dik_data; |
5239 | 0 | *dik_len = p2p->peer_dik_len; |
5240 | 0 | *cipher = p2p->dik_cipher_version; |
5241 | | |
5242 | | /* Reset DIK length to invalidate DIK for successive iteration of a new |
5243 | | * peer. */ |
5244 | 0 | p2p->peer_dik_len = 0; |
5245 | |
|
5246 | 0 | return 0; |
5247 | 0 | } |
5248 | | |
5249 | | |
5250 | | void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr) |
5251 | 0 | { |
5252 | 0 | os_memcpy(p2p->peer_filter, addr, ETH_ALEN); |
5253 | 0 | if (is_zero_ether_addr(p2p->peer_filter)) |
5254 | 0 | p2p_dbg(p2p, "Disable peer filter"); |
5255 | 0 | else |
5256 | 0 | p2p_dbg(p2p, "Enable peer filter for " MACSTR, |
5257 | 0 | MAC2STR(p2p->peer_filter)); |
5258 | 0 | } |
5259 | | |
5260 | | |
5261 | | void p2p_set_cross_connect(struct p2p_data *p2p, int enabled) |
5262 | 0 | { |
5263 | 0 | p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled"); |
5264 | 0 | if (p2p->cross_connect == enabled) |
5265 | 0 | return; |
5266 | 0 | p2p->cross_connect = enabled; |
5267 | | /* TODO: may need to tear down any action group where we are GO(?) */ |
5268 | 0 | } |
5269 | | |
5270 | | |
5271 | | int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr) |
5272 | 0 | { |
5273 | 0 | struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); |
5274 | 0 | if (dev == NULL) |
5275 | 0 | return -1; |
5276 | 0 | if (dev->oper_freq <= 0) |
5277 | 0 | return -1; |
5278 | 0 | return dev->oper_freq; |
5279 | 0 | } |
5280 | | |
5281 | | |
5282 | | void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled) |
5283 | 0 | { |
5284 | 0 | p2p_dbg(p2p, "Intra BSS distribution %s", |
5285 | 0 | enabled ? "enabled" : "disabled"); |
5286 | 0 | p2p->cfg->p2p_intra_bss = enabled; |
5287 | 0 | } |
5288 | | |
5289 | | |
5290 | | void p2p_update_channel_list(struct p2p_data *p2p, |
5291 | | const struct p2p_channels *chan, |
5292 | | const struct p2p_channels *cli_chan) |
5293 | 0 | { |
5294 | 0 | p2p_dbg(p2p, "Update channel list"); |
5295 | 0 | os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels)); |
5296 | 0 | p2p_channels_dump(p2p, "channels", &p2p->cfg->channels); |
5297 | 0 | os_memcpy(&p2p->cfg->cli_channels, cli_chan, |
5298 | 0 | sizeof(struct p2p_channels)); |
5299 | 0 | p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels); |
5300 | 0 | } |
5301 | | |
5302 | | |
5303 | | int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst, |
5304 | | const u8 *src, const u8 *bssid, const u8 *buf, |
5305 | | size_t len, unsigned int wait_time) |
5306 | 1.68k | { |
5307 | 1.68k | int res, scheduled; |
5308 | | |
5309 | 1.68k | res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid, |
5310 | 1.68k | buf, len, wait_time, &scheduled); |
5311 | 1.68k | if (res == 0 && scheduled && p2p->in_listen && freq > 0 && |
5312 | 0 | p2p->drv_in_listen > 0 && |
5313 | 0 | (unsigned int) p2p->drv_in_listen != freq) { |
5314 | 0 | p2p_dbg(p2p, |
5315 | 0 | "Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz", |
5316 | 0 | p2p->drv_in_listen, freq); |
5317 | 0 | p2p_stop_listen_for_freq(p2p, freq); |
5318 | 0 | } |
5319 | 1.68k | return res; |
5320 | 1.68k | } |
5321 | | |
5322 | | |
5323 | | void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5, |
5324 | | int freq_overall) |
5325 | 0 | { |
5326 | 0 | p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d", |
5327 | 0 | freq_24, freq_5, freq_overall); |
5328 | 0 | p2p->best_freq_24 = freq_24; |
5329 | 0 | p2p->best_freq_5 = freq_5; |
5330 | 0 | p2p->best_freq_overall = freq_overall; |
5331 | 0 | } |
5332 | | |
5333 | | |
5334 | | void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq) |
5335 | 0 | { |
5336 | 0 | p2p_dbg(p2p, "Own frequency preference: %d MHz", freq); |
5337 | 0 | p2p->own_freq_preference = freq; |
5338 | 0 | } |
5339 | | |
5340 | | |
5341 | | const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p) |
5342 | 0 | { |
5343 | 0 | if (p2p == NULL || p2p->go_neg_peer == NULL) |
5344 | 0 | return NULL; |
5345 | 0 | return p2p->go_neg_peer->info.p2p_device_addr; |
5346 | 0 | } |
5347 | | |
5348 | | |
5349 | | const struct p2p_peer_info * |
5350 | | p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next) |
5351 | 0 | { |
5352 | 0 | struct p2p_device *dev; |
5353 | |
|
5354 | 0 | if (addr) { |
5355 | 0 | dev = p2p_get_device(p2p, addr); |
5356 | 0 | if (!dev) |
5357 | 0 | return NULL; |
5358 | | |
5359 | 0 | if (!next) { |
5360 | 0 | if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) |
5361 | 0 | return NULL; |
5362 | | |
5363 | 0 | return &dev->info; |
5364 | 0 | } else { |
5365 | 0 | do { |
5366 | 0 | dev = dl_list_first(&dev->list, |
5367 | 0 | struct p2p_device, |
5368 | 0 | list); |
5369 | 0 | if (!dev || &dev->list == &p2p->devices) |
5370 | 0 | return NULL; |
5371 | 0 | } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY); |
5372 | 0 | } |
5373 | 0 | } else { |
5374 | 0 | dev = dl_list_first(&p2p->devices, struct p2p_device, list); |
5375 | 0 | if (!dev) |
5376 | 0 | return NULL; |
5377 | 0 | while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { |
5378 | 0 | dev = dl_list_first(&dev->list, |
5379 | 0 | struct p2p_device, |
5380 | 0 | list); |
5381 | 0 | if (!dev || &dev->list == &p2p->devices) |
5382 | 0 | return NULL; |
5383 | 0 | } |
5384 | 0 | } |
5385 | | |
5386 | 0 | return &dev->info; |
5387 | 0 | } |
5388 | | |
5389 | | |
5390 | | int p2p_in_progress(struct p2p_data *p2p) |
5391 | 0 | { |
5392 | 0 | if (p2p == NULL) |
5393 | 0 | return 0; |
5394 | 0 | if (p2p->state == P2P_SEARCH) |
5395 | 0 | return 2; |
5396 | 0 | return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING; |
5397 | 0 | } |
5398 | | |
5399 | | |
5400 | | void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout, |
5401 | | u8 client_timeout) |
5402 | 0 | { |
5403 | 0 | if (p2p) { |
5404 | 0 | p2p->go_timeout = go_timeout; |
5405 | 0 | p2p->client_timeout = client_timeout; |
5406 | 0 | } |
5407 | 0 | } |
5408 | | |
5409 | | |
5410 | | #ifdef CONFIG_WIFI_DISPLAY |
5411 | | |
5412 | | static void p2p_update_wfd_ie_groups(struct p2p_data *p2p) |
5413 | 0 | { |
5414 | 0 | size_t g; |
5415 | 0 | struct p2p_group *group; |
5416 | |
|
5417 | 0 | for (g = 0; g < p2p->num_groups; g++) { |
5418 | 0 | group = p2p->groups[g]; |
5419 | 0 | p2p_group_force_beacon_update_ies(group); |
5420 | 0 | } |
5421 | 0 | } |
5422 | | |
5423 | | |
5424 | | int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie) |
5425 | 0 | { |
5426 | 0 | wpabuf_free(p2p->wfd_ie_beacon); |
5427 | 0 | p2p->wfd_ie_beacon = ie; |
5428 | 0 | p2p_update_wfd_ie_groups(p2p); |
5429 | 0 | return 0; |
5430 | 0 | } |
5431 | | |
5432 | | |
5433 | | int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie) |
5434 | 0 | { |
5435 | 0 | wpabuf_free(p2p->wfd_ie_probe_req); |
5436 | 0 | p2p->wfd_ie_probe_req = ie; |
5437 | 0 | return 0; |
5438 | 0 | } |
5439 | | |
5440 | | |
5441 | | int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie) |
5442 | 0 | { |
5443 | 0 | wpabuf_free(p2p->wfd_ie_probe_resp); |
5444 | 0 | p2p->wfd_ie_probe_resp = ie; |
5445 | 0 | p2p_update_wfd_ie_groups(p2p); |
5446 | 0 | return 0; |
5447 | 0 | } |
5448 | | |
5449 | | |
5450 | | int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie) |
5451 | 0 | { |
5452 | 0 | wpabuf_free(p2p->wfd_ie_assoc_req); |
5453 | 0 | p2p->wfd_ie_assoc_req = ie; |
5454 | 0 | return 0; |
5455 | 0 | } |
5456 | | |
5457 | | |
5458 | | int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie) |
5459 | 0 | { |
5460 | 0 | wpabuf_free(p2p->wfd_ie_invitation); |
5461 | 0 | p2p->wfd_ie_invitation = ie; |
5462 | 0 | return 0; |
5463 | 0 | } |
5464 | | |
5465 | | |
5466 | | int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie) |
5467 | 0 | { |
5468 | 0 | wpabuf_free(p2p->wfd_ie_prov_disc_req); |
5469 | 0 | p2p->wfd_ie_prov_disc_req = ie; |
5470 | 0 | return 0; |
5471 | 0 | } |
5472 | | |
5473 | | |
5474 | | int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie) |
5475 | 0 | { |
5476 | 0 | wpabuf_free(p2p->wfd_ie_prov_disc_resp); |
5477 | 0 | p2p->wfd_ie_prov_disc_resp = ie; |
5478 | 0 | return 0; |
5479 | 0 | } |
5480 | | |
5481 | | |
5482 | | int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie) |
5483 | 0 | { |
5484 | 0 | wpabuf_free(p2p->wfd_ie_go_neg); |
5485 | 0 | p2p->wfd_ie_go_neg = ie; |
5486 | 0 | return 0; |
5487 | 0 | } |
5488 | | |
5489 | | |
5490 | | int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem) |
5491 | 0 | { |
5492 | 0 | wpabuf_free(p2p->wfd_dev_info); |
5493 | 0 | if (elem) { |
5494 | 0 | p2p->wfd_dev_info = wpabuf_dup(elem); |
5495 | 0 | if (p2p->wfd_dev_info == NULL) |
5496 | 0 | return -1; |
5497 | 0 | } else |
5498 | 0 | p2p->wfd_dev_info = NULL; |
5499 | | |
5500 | 0 | return 0; |
5501 | 0 | } |
5502 | | |
5503 | | |
5504 | | int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem) |
5505 | 0 | { |
5506 | 0 | wpabuf_free(p2p->wfd_r2_dev_info); |
5507 | 0 | if (elem) { |
5508 | 0 | p2p->wfd_r2_dev_info = wpabuf_dup(elem); |
5509 | 0 | if (p2p->wfd_r2_dev_info == NULL) |
5510 | 0 | return -1; |
5511 | 0 | } else |
5512 | 0 | p2p->wfd_r2_dev_info = NULL; |
5513 | | |
5514 | 0 | return 0; |
5515 | 0 | } |
5516 | | |
5517 | | |
5518 | | int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem) |
5519 | 0 | { |
5520 | 0 | wpabuf_free(p2p->wfd_assoc_bssid); |
5521 | 0 | if (elem) { |
5522 | 0 | p2p->wfd_assoc_bssid = wpabuf_dup(elem); |
5523 | 0 | if (p2p->wfd_assoc_bssid == NULL) |
5524 | 0 | return -1; |
5525 | 0 | } else |
5526 | 0 | p2p->wfd_assoc_bssid = NULL; |
5527 | | |
5528 | 0 | return 0; |
5529 | 0 | } |
5530 | | |
5531 | | |
5532 | | int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p, |
5533 | | const struct wpabuf *elem) |
5534 | 0 | { |
5535 | 0 | wpabuf_free(p2p->wfd_coupled_sink_info); |
5536 | 0 | if (elem) { |
5537 | 0 | p2p->wfd_coupled_sink_info = wpabuf_dup(elem); |
5538 | 0 | if (p2p->wfd_coupled_sink_info == NULL) |
5539 | 0 | return -1; |
5540 | 0 | } else |
5541 | 0 | p2p->wfd_coupled_sink_info = NULL; |
5542 | | |
5543 | 0 | return 0; |
5544 | 0 | } |
5545 | | |
5546 | | #endif /* CONFIG_WIFI_DISPLAY */ |
5547 | | |
5548 | | |
5549 | | int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int, |
5550 | | int max_disc_tu) |
5551 | 0 | { |
5552 | 0 | if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0) |
5553 | 0 | return -1; |
5554 | | |
5555 | 0 | p2p->min_disc_int = min_disc_int; |
5556 | 0 | p2p->max_disc_int = max_disc_int; |
5557 | 0 | p2p->max_disc_tu = max_disc_tu; |
5558 | 0 | p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d", |
5559 | 0 | min_disc_int, max_disc_int, max_disc_tu); |
5560 | |
|
5561 | 0 | return 0; |
5562 | 0 | } |
5563 | | |
5564 | | |
5565 | | void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...) |
5566 | 94.0k | { |
5567 | 94.0k | va_list ap; |
5568 | 94.0k | char buf[500]; |
5569 | | |
5570 | 94.0k | if (!p2p->cfg->debug_print) |
5571 | 0 | return; |
5572 | | |
5573 | 94.0k | va_start(ap, fmt); |
5574 | 94.0k | vsnprintf(buf, sizeof(buf), fmt, ap); |
5575 | 94.0k | buf[sizeof(buf) - 1] = '\0'; |
5576 | 94.0k | va_end(ap); |
5577 | 94.0k | p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf); |
5578 | 94.0k | } |
5579 | | |
5580 | | |
5581 | | void p2p_info(struct p2p_data *p2p, const char *fmt, ...) |
5582 | 51 | { |
5583 | 51 | va_list ap; |
5584 | 51 | char buf[500]; |
5585 | | |
5586 | 51 | if (!p2p->cfg->debug_print) |
5587 | 0 | return; |
5588 | | |
5589 | 51 | va_start(ap, fmt); |
5590 | 51 | vsnprintf(buf, sizeof(buf), fmt, ap); |
5591 | 51 | buf[sizeof(buf) - 1] = '\0'; |
5592 | 51 | va_end(ap); |
5593 | 51 | p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf); |
5594 | 51 | } |
5595 | | |
5596 | | |
5597 | | void p2p_err(struct p2p_data *p2p, const char *fmt, ...) |
5598 | 0 | { |
5599 | 0 | va_list ap; |
5600 | 0 | char buf[500]; |
5601 | |
|
5602 | 0 | if (!p2p->cfg->debug_print) |
5603 | 0 | return; |
5604 | | |
5605 | 0 | va_start(ap, fmt); |
5606 | 0 | vsnprintf(buf, sizeof(buf), fmt, ap); |
5607 | 0 | buf[sizeof(buf) - 1] = '\0'; |
5608 | 0 | va_end(ap); |
5609 | 0 | p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf); |
5610 | 0 | } |
5611 | | |
5612 | | |
5613 | | void p2p_loop_on_known_peers(struct p2p_data *p2p, |
5614 | | void (*peer_callback)(struct p2p_peer_info *peer, |
5615 | | void *user_data), |
5616 | | void *user_data) |
5617 | 0 | { |
5618 | 0 | struct p2p_device *dev, *n; |
5619 | |
|
5620 | 0 | dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) { |
5621 | 0 | peer_callback(&dev->info, user_data); |
5622 | 0 | } |
5623 | 0 | } |
5624 | | |
5625 | | |
5626 | | #ifdef CONFIG_WPS_NFC |
5627 | | |
5628 | | static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p, |
5629 | | int client_freq, |
5630 | | const u8 *go_dev_addr, |
5631 | | const u8 *ssid, size_t ssid_len) |
5632 | 0 | { |
5633 | 0 | struct wpabuf *buf; |
5634 | 0 | u8 op_class, channel; |
5635 | 0 | enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP; |
5636 | |
|
5637 | 0 | buf = wpabuf_alloc(1000); |
5638 | 0 | if (buf == NULL) |
5639 | 0 | return NULL; |
5640 | | |
5641 | 0 | op_class = p2p->cfg->reg_class; |
5642 | 0 | channel = p2p->cfg->channel; |
5643 | |
|
5644 | 0 | p2p_buf_add_capability(buf, p2p->dev_capab & |
5645 | 0 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); |
5646 | 0 | p2p_buf_add_device_info(buf, p2p, NULL); |
5647 | |
|
5648 | 0 | if (p2p->num_groups > 0) { |
5649 | 0 | int freq = p2p_group_get_freq(p2p->groups[0]); |
5650 | 0 | role = P2P_GO_IN_A_GROUP; |
5651 | 0 | if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) { |
5652 | 0 | p2p_dbg(p2p, |
5653 | 0 | "Unknown GO operating frequency %d MHz for NFC handover", |
5654 | 0 | freq); |
5655 | 0 | wpabuf_free(buf); |
5656 | 0 | return NULL; |
5657 | 0 | } |
5658 | 0 | } else if (client_freq > 0) { |
5659 | 0 | role = P2P_CLIENT_IN_A_GROUP; |
5660 | 0 | if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) { |
5661 | 0 | p2p_dbg(p2p, |
5662 | 0 | "Unknown client operating frequency %d MHz for NFC handover", |
5663 | 0 | client_freq); |
5664 | 0 | wpabuf_free(buf); |
5665 | 0 | return NULL; |
5666 | 0 | } |
5667 | 0 | } |
5668 | | |
5669 | 0 | p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class, |
5670 | 0 | channel, role); |
5671 | |
|
5672 | 0 | if (p2p->num_groups > 0) { |
5673 | | /* Limit number of clients to avoid very long message */ |
5674 | 0 | p2p_buf_add_group_info(p2p->groups[0], buf, 5); |
5675 | 0 | p2p_group_buf_add_id(p2p->groups[0], buf); |
5676 | 0 | } else if (client_freq > 0 && |
5677 | 0 | go_dev_addr && !is_zero_ether_addr(go_dev_addr) && |
5678 | 0 | ssid && ssid_len > 0) { |
5679 | | /* |
5680 | | * Add the optional P2P Group ID to indicate in which group this |
5681 | | * device is a P2P Client. |
5682 | | */ |
5683 | 0 | p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len); |
5684 | 0 | } |
5685 | |
|
5686 | 0 | return buf; |
5687 | 0 | } |
5688 | | |
5689 | | |
5690 | | struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p, |
5691 | | int client_freq, |
5692 | | const u8 *go_dev_addr, |
5693 | | const u8 *ssid, size_t ssid_len) |
5694 | 0 | { |
5695 | 0 | return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid, |
5696 | 0 | ssid_len); |
5697 | 0 | } |
5698 | | |
5699 | | |
5700 | | struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p, |
5701 | | int client_freq, |
5702 | | const u8 *go_dev_addr, |
5703 | | const u8 *ssid, size_t ssid_len) |
5704 | 0 | { |
5705 | 0 | return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid, |
5706 | 0 | ssid_len); |
5707 | 0 | } |
5708 | | |
5709 | | |
5710 | | int p2p_process_nfc_connection_handover(struct p2p_data *p2p, |
5711 | | struct p2p_nfc_params *params) |
5712 | 0 | { |
5713 | 0 | struct p2p_message msg; |
5714 | 0 | struct p2p_device *dev; |
5715 | 0 | const u8 *p2p_dev_addr; |
5716 | 0 | int freq; |
5717 | 0 | enum p2p_role_indication role; |
5718 | |
|
5719 | 0 | params->next_step = NO_ACTION; |
5720 | |
|
5721 | 0 | if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len, |
5722 | 0 | params->p2p_attr, params->p2p_len, &msg)) { |
5723 | 0 | p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC"); |
5724 | 0 | p2p_parse_free(&msg); |
5725 | 0 | return -1; |
5726 | 0 | } |
5727 | | |
5728 | 0 | if (msg.p2p_device_addr) |
5729 | 0 | p2p_dev_addr = msg.p2p_device_addr; |
5730 | 0 | else if (msg.device_id) |
5731 | 0 | p2p_dev_addr = msg.device_id; |
5732 | 0 | else { |
5733 | 0 | p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id"); |
5734 | 0 | p2p_parse_free(&msg); |
5735 | 0 | return -1; |
5736 | 0 | } |
5737 | | |
5738 | 0 | if (msg.oob_dev_password) { |
5739 | 0 | os_memcpy(params->oob_dev_pw, msg.oob_dev_password, |
5740 | 0 | msg.oob_dev_password_len); |
5741 | 0 | params->oob_dev_pw_len = msg.oob_dev_password_len; |
5742 | 0 | } |
5743 | |
|
5744 | 0 | dev = p2p_create_device(p2p, p2p_dev_addr); |
5745 | 0 | if (dev == NULL) { |
5746 | 0 | p2p_parse_free(&msg); |
5747 | 0 | return -1; |
5748 | 0 | } |
5749 | | |
5750 | 0 | params->peer = &dev->info; |
5751 | |
|
5752 | 0 | os_get_reltime(&dev->last_seen); |
5753 | 0 | dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY); |
5754 | 0 | p2p_copy_wps_info(p2p, dev, 0, &msg); |
5755 | |
|
5756 | 0 | if (!msg.oob_go_neg_channel) { |
5757 | 0 | p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included"); |
5758 | 0 | p2p_parse_free(&msg); |
5759 | 0 | return -1; |
5760 | 0 | } |
5761 | | |
5762 | 0 | if (msg.oob_go_neg_channel[3] == 0 && |
5763 | 0 | msg.oob_go_neg_channel[4] == 0) |
5764 | 0 | freq = 0; |
5765 | 0 | else |
5766 | 0 | freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3], |
5767 | 0 | msg.oob_go_neg_channel[4]); |
5768 | 0 | if (freq < 0) { |
5769 | 0 | p2p_dbg(p2p, "Unknown peer OOB GO Neg channel"); |
5770 | 0 | p2p_parse_free(&msg); |
5771 | 0 | return -1; |
5772 | 0 | } |
5773 | 0 | role = msg.oob_go_neg_channel[5]; |
5774 | |
|
5775 | 0 | if (role == P2P_GO_IN_A_GROUP) { |
5776 | 0 | p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq); |
5777 | 0 | params->go_freq = freq; |
5778 | 0 | } else if (role == P2P_CLIENT_IN_A_GROUP) { |
5779 | 0 | p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz", |
5780 | 0 | freq); |
5781 | 0 | params->go_freq = freq; |
5782 | 0 | } else |
5783 | 0 | p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq); |
5784 | 0 | dev->oob_go_neg_freq = freq; |
5785 | |
|
5786 | 0 | if (!params->sel && role != P2P_GO_IN_A_GROUP) { |
5787 | 0 | freq = p2p_channel_to_freq(p2p->cfg->reg_class, |
5788 | 0 | p2p->cfg->channel); |
5789 | 0 | if (freq < 0) { |
5790 | 0 | p2p_dbg(p2p, "Own listen channel not known"); |
5791 | 0 | p2p_parse_free(&msg); |
5792 | 0 | return -1; |
5793 | 0 | } |
5794 | 0 | p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq); |
5795 | 0 | dev->oob_go_neg_freq = freq; |
5796 | 0 | } |
5797 | | |
5798 | 0 | if (msg.group_id) { |
5799 | 0 | os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN); |
5800 | 0 | params->go_ssid_len = msg.group_id_len - ETH_ALEN; |
5801 | 0 | os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN, |
5802 | 0 | params->go_ssid_len); |
5803 | 0 | } |
5804 | |
|
5805 | 0 | if (dev->flags & P2P_DEV_USER_REJECTED) { |
5806 | 0 | p2p_dbg(p2p, "Do not report rejected device"); |
5807 | 0 | p2p_parse_free(&msg); |
5808 | 0 | return 0; |
5809 | 0 | } |
5810 | | |
5811 | 0 | if (!(dev->flags & P2P_DEV_REPORTED)) { |
5812 | 0 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info, |
5813 | 0 | !(dev->flags & P2P_DEV_REPORTED_ONCE)); |
5814 | 0 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; |
5815 | 0 | } |
5816 | 0 | p2p_parse_free(&msg); |
5817 | |
|
5818 | 0 | if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0) |
5819 | 0 | params->next_step = BOTH_GO; |
5820 | 0 | else if (role == P2P_GO_IN_A_GROUP) |
5821 | 0 | params->next_step = JOIN_GROUP; |
5822 | 0 | else if (role == P2P_CLIENT_IN_A_GROUP) { |
5823 | 0 | dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY; |
5824 | 0 | params->next_step = PEER_CLIENT; |
5825 | 0 | } else if (p2p->num_groups > 0) |
5826 | 0 | params->next_step = AUTH_JOIN; |
5827 | 0 | else if (params->sel) |
5828 | 0 | params->next_step = INIT_GO_NEG; |
5829 | 0 | else |
5830 | 0 | params->next_step = RESP_GO_NEG; |
5831 | |
|
5832 | 0 | return 0; |
5833 | 0 | } |
5834 | | |
5835 | | |
5836 | | void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id, |
5837 | | int go_intent, |
5838 | | const u8 *own_interface_addr) |
5839 | 0 | { |
5840 | |
|
5841 | 0 | p2p->authorized_oob_dev_pw_id = dev_pw_id; |
5842 | 0 | if (dev_pw_id == 0) { |
5843 | 0 | p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover"); |
5844 | 0 | return; |
5845 | 0 | } |
5846 | | |
5847 | 0 | p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover", |
5848 | 0 | dev_pw_id); |
5849 | |
|
5850 | 0 | p2p->go_intent = go_intent; |
5851 | 0 | os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); |
5852 | 0 | } |
5853 | | |
5854 | | #endif /* CONFIG_WPS_NFC */ |
5855 | | |
5856 | | |
5857 | | int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len) |
5858 | 0 | { |
5859 | 0 | if (len < 8 || len > 63) |
5860 | 0 | return -1; |
5861 | 0 | p2p->cfg->passphrase_len = len; |
5862 | 0 | return 0; |
5863 | 0 | } |
5864 | | |
5865 | | |
5866 | | void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem) |
5867 | 0 | { |
5868 | 0 | p2p->vendor_elem = vendor_elem; |
5869 | 0 | } |
5870 | | |
5871 | | |
5872 | | void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx) |
5873 | 0 | { |
5874 | 0 | struct p2p_data *p2p = eloop_ctx; |
5875 | |
|
5876 | 0 | p2p_dbg(p2p, |
5877 | 0 | "Timeout on waiting peer to become ready for GO Negotiation"); |
5878 | 0 | p2p_go_neg_failed(p2p, -1); |
5879 | 0 | } |
5880 | | |
5881 | | |
5882 | | void p2p_set_own_pref_freq_list(struct p2p_data *p2p, |
5883 | | const struct weighted_pcl *pref_freq_list, |
5884 | | unsigned int size) |
5885 | 0 | { |
5886 | 0 | unsigned int i; |
5887 | |
|
5888 | 0 | if (size > P2P_MAX_PREF_CHANNELS) |
5889 | 0 | size = P2P_MAX_PREF_CHANNELS; |
5890 | 0 | p2p->num_pref_freq = size; |
5891 | 0 | os_memcpy(p2p->pref_freq_list, pref_freq_list, |
5892 | 0 | size * sizeof(struct weighted_pcl)); |
5893 | 0 | for (i = 0; i < size; i++) { |
5894 | 0 | p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz", |
5895 | 0 | i, p2p->pref_freq_list[i].freq); |
5896 | 0 | } |
5897 | 0 | } |
5898 | | |
5899 | | |
5900 | | void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class, |
5901 | | u8 chan) |
5902 | 0 | { |
5903 | 0 | p2p->override_pref_op_class = op_class; |
5904 | 0 | p2p->override_pref_channel = chan; |
5905 | 0 | } |
5906 | | |
5907 | | |
5908 | | struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p, |
5909 | | unsigned int freq) |
5910 | 0 | { |
5911 | 0 | struct wpabuf *ies, *buf; |
5912 | 0 | u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
5913 | 0 | int ret; |
5914 | |
|
5915 | 0 | ies = p2p_build_probe_resp_ies(p2p, NULL, 0); |
5916 | 0 | if (!ies) { |
5917 | 0 | wpa_printf(MSG_ERROR, |
5918 | 0 | "CTRL: Failed to build Probe Response IEs"); |
5919 | 0 | return NULL; |
5920 | 0 | } |
5921 | | |
5922 | 0 | buf = wpabuf_alloc(200 + wpabuf_len(ies)); |
5923 | 0 | if (!buf) { |
5924 | 0 | wpabuf_free(ies); |
5925 | 0 | return NULL; |
5926 | 0 | } |
5927 | | |
5928 | 0 | ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq); |
5929 | 0 | wpabuf_free(ies); |
5930 | 0 | if (ret) { |
5931 | 0 | wpabuf_free(buf); |
5932 | 0 | return NULL; |
5933 | 0 | } |
5934 | | |
5935 | 0 | return buf; |
5936 | 0 | } |
5937 | | |
5938 | | |
5939 | | bool p2p_is_peer_6ghz_capab(struct p2p_data *p2p, const u8 *addr) |
5940 | 0 | { |
5941 | 0 | struct p2p_device *dev; |
5942 | |
|
5943 | 0 | dev = p2p_get_device(p2p, addr); |
5944 | 0 | if (!dev) |
5945 | 0 | return false; |
5946 | | |
5947 | 0 | return dev->support_6ghz; |
5948 | 0 | } |
5949 | | |
5950 | | |
5951 | | void p2p_set_6ghz_dev_capab(struct p2p_data *p2p, bool allow_6ghz) |
5952 | 0 | { |
5953 | 0 | p2p->p2p_6ghz_capable = allow_6ghz; |
5954 | 0 | p2p->allow_6ghz = allow_6ghz; |
5955 | 0 | p2p_dbg(p2p, "Set 6 GHz capability to %d", allow_6ghz); |
5956 | |
|
5957 | 0 | if (allow_6ghz) |
5958 | 0 | p2p->dev_capab |= P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE; |
5959 | 0 | else |
5960 | 0 | p2p->dev_capab &= ~P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE; |
5961 | 0 | } |
5962 | | |
5963 | | |
5964 | | bool is_p2p_6ghz_capable(struct p2p_data *p2p) |
5965 | 0 | { |
5966 | 0 | return p2p->p2p_6ghz_capable; |
5967 | 0 | } |
5968 | | |
5969 | | |
5970 | | bool p2p_wfd_enabled(struct p2p_data *p2p) |
5971 | 0 | { |
5972 | 0 | #ifdef CONFIG_WIFI_DISPLAY |
5973 | 0 | return p2p->wfd_ie_probe_req != NULL; |
5974 | | #else /* CONFIG_WIFI_DISPLAY */ |
5975 | | return false; |
5976 | | #endif /* CONFIG_WIFI_DISPLAY */ |
5977 | 0 | } |
5978 | | |
5979 | | |
5980 | | bool p2p_peer_wfd_enabled(struct p2p_data *p2p, const u8 *peer_addr) |
5981 | 0 | { |
5982 | 0 | #ifdef CONFIG_WIFI_DISPLAY |
5983 | 0 | struct p2p_device *dev; |
5984 | |
|
5985 | 0 | dev = p2p_get_device(p2p, peer_addr); |
5986 | 0 | return dev && dev->info.wfd_subelems != NULL; |
5987 | | #else /* CONFIG_WIFI_DISPLAY */ |
5988 | | return false; |
5989 | | #endif /* CONFIG_WIFI_DISPLAY */ |
5990 | 0 | } |
5991 | | |
5992 | | |
5993 | | bool is_p2p_allow_6ghz(struct p2p_data *p2p) |
5994 | 0 | { |
5995 | 0 | return p2p->allow_6ghz; |
5996 | 0 | } |
5997 | | |
5998 | | |
5999 | | void set_p2p_allow_6ghz(struct p2p_data *p2p, bool value) |
6000 | 0 | { |
6001 | 0 | p2p->allow_6ghz = value; |
6002 | 0 | } |
6003 | | |
6004 | | |
6005 | | static int p2p_derive_nonce_tag(struct p2p_data *p2p) |
6006 | 0 | { |
6007 | 0 | u8 dira_nonce[DEVICE_IDENTITY_NONCE_LEN]; |
6008 | 0 | u8 dira_tag[DEVICE_MAX_HASH_LEN]; |
6009 | 0 | u8 data[DIR_STR_LEN + DEVICE_IDENTITY_NONCE_LEN + ETH_ALEN]; |
6010 | 0 | struct p2p_id_key *dev_ik; |
6011 | |
|
6012 | 0 | dev_ik = &p2p->pairing_info->dev_ik; |
6013 | |
|
6014 | 0 | if (dev_ik->cipher_version != DIRA_CIPHER_VERSION_128) { |
6015 | 0 | wpa_printf(MSG_INFO, |
6016 | 0 | "P2P: Unsupported DIRA Cipher version = %d", |
6017 | 0 | dev_ik->cipher_version); |
6018 | 0 | return -1; |
6019 | 0 | } |
6020 | | |
6021 | 0 | if (dev_ik->dik_len != DEVICE_IDENTITY_KEY_LEN) { |
6022 | 0 | wpa_printf(MSG_INFO, "P2P: Invalid DIK length = %zu", |
6023 | 0 | dev_ik->dik_len); |
6024 | 0 | return -1; |
6025 | 0 | } |
6026 | | |
6027 | 0 | os_memset(data, 0, sizeof(data)); |
6028 | |
|
6029 | 0 | if (os_get_random(dira_nonce, DEVICE_IDENTITY_NONCE_LEN) < 0) { |
6030 | 0 | wpa_printf(MSG_ERROR, "P2P: Failed to generate DIRA nonce"); |
6031 | 0 | return -1; |
6032 | 0 | } |
6033 | | |
6034 | | /* Tag = Truncate-64(HMAC-SHA-256(DevIK, |
6035 | | * "DIR" || P2P Device Address || Nonce)) |
6036 | | */ |
6037 | 0 | os_memcpy(data, "DIR", DIR_STR_LEN); |
6038 | 0 | os_memcpy(&data[DIR_STR_LEN], p2p->cfg->dev_addr, ETH_ALEN); |
6039 | 0 | os_memcpy(&data[DIR_STR_LEN + ETH_ALEN], dira_nonce, |
6040 | 0 | DEVICE_IDENTITY_NONCE_LEN); |
6041 | |
|
6042 | 0 | if (hmac_sha256(dev_ik->dik_data, dev_ik->dik_len, data, sizeof(data), |
6043 | 0 | dira_tag) < 0) { |
6044 | 0 | wpa_printf(MSG_ERROR, "P2P: Could not derive DIRA tag"); |
6045 | 0 | return -1; |
6046 | 0 | } |
6047 | | |
6048 | 0 | dev_ik->dira_nonce_len = DEVICE_IDENTITY_NONCE_LEN; |
6049 | 0 | os_memcpy(dev_ik->dira_nonce, dira_nonce, DEVICE_IDENTITY_NONCE_LEN); |
6050 | 0 | dev_ik->dira_tag_len = DEVICE_IDENTITY_TAG_LEN; |
6051 | 0 | os_memcpy(dev_ik->dira_tag, dira_tag, DEVICE_IDENTITY_TAG_LEN); |
6052 | |
|
6053 | 0 | wpa_hexdump_key(MSG_DEBUG, "P2P: DIK", dev_ik->dik_data, |
6054 | 0 | dev_ik->dik_len); |
6055 | 0 | wpa_hexdump_key(MSG_DEBUG, "P2P: DIRA-NONCE", dev_ik->dira_nonce, |
6056 | 0 | dev_ik->dira_nonce_len); |
6057 | 0 | wpa_hexdump_key(MSG_DEBUG, "P2P: DIRA-TAG", dev_ik->dira_tag, |
6058 | 0 | dev_ik->dira_tag_len); |
6059 | 0 | return 0; |
6060 | 0 | } |
6061 | | |
6062 | | |
6063 | | static int p2p_validate_dira(struct p2p_data *p2p, struct p2p_device *dev, |
6064 | | const u8 *dira, u16 dira_len) |
6065 | 0 | { |
6066 | 0 | if (dira_len < 1 || dira[0] != DIRA_CIPHER_VERSION_128) { |
6067 | 0 | p2p_dbg(p2p, "Unsupported DIRA cipher version %d", |
6068 | 0 | dira[0]); |
6069 | 0 | return 0; |
6070 | 0 | } |
6071 | | |
6072 | 0 | if (dira_len < 1 + DEVICE_IDENTITY_NONCE_LEN + DEVICE_IDENTITY_TAG_LEN) |
6073 | 0 | { |
6074 | 0 | p2p_dbg(p2p, "Truncated DIRA (length %u)", dira_len); |
6075 | 0 | return 0; |
6076 | 0 | } |
6077 | | |
6078 | 0 | if (p2p->cfg->validate_dira) { |
6079 | 0 | const u8 *nonce = &dira[1]; |
6080 | 0 | const u8 *tag = &dira[1 + DEVICE_IDENTITY_NONCE_LEN]; |
6081 | |
|
6082 | 0 | return p2p->cfg->validate_dira(p2p->cfg->cb_ctx, |
6083 | 0 | dev->info.p2p_device_addr, |
6084 | 0 | nonce, tag); |
6085 | 0 | } |
6086 | | |
6087 | 0 | return 0; |
6088 | 0 | } |
6089 | | |
6090 | | |
6091 | | void p2p_usd_service_hash(struct p2p_data *p2p, const char *service_name) |
6092 | 0 | { |
6093 | 0 | u8 buf[P2PS_HASH_LEN]; |
6094 | |
|
6095 | 0 | p2p->usd_service = false; |
6096 | |
|
6097 | 0 | if (!service_name) |
6098 | 0 | return; |
6099 | | |
6100 | 0 | if (!p2ps_gen_hash(p2p, service_name, buf)) |
6101 | 0 | return; |
6102 | 0 | p2p_dbg(p2p, "USD service %s hash " MACSTR, |
6103 | 0 | service_name, MAC2STR(buf)); |
6104 | 0 | p2p->usd_service = true; |
6105 | 0 | os_memcpy(&p2p->p2p_service_hash, buf, P2PS_HASH_LEN); |
6106 | 0 | } |
6107 | | |
6108 | | |
6109 | | struct wpabuf * p2p_usd_elems(struct p2p_data *p2p) |
6110 | 0 | { |
6111 | 0 | struct wpabuf *buf; |
6112 | 0 | u8 *len; |
6113 | 0 | u8 group_capab; |
6114 | |
|
6115 | 0 | buf = wpabuf_alloc(1000); |
6116 | 0 | if (!buf) |
6117 | 0 | return NULL; |
6118 | | |
6119 | 0 | len = p2p_buf_add_ie_hdr(buf); |
6120 | | |
6121 | | /* P2P Capability attribute */ |
6122 | 0 | group_capab = 0; |
6123 | 0 | if (p2p->num_groups) { |
6124 | 0 | group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER; |
6125 | 0 | if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && |
6126 | 0 | (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) && |
6127 | 0 | p2p->cross_connect) |
6128 | 0 | group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; |
6129 | 0 | } |
6130 | 0 | if (p2p->cfg->p2p_intra_bss) |
6131 | 0 | group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST; |
6132 | 0 | p2p_buf_add_capability(buf, p2p->dev_capab & |
6133 | 0 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, |
6134 | 0 | group_capab); |
6135 | | |
6136 | | /* P2P Device Info attribute */ |
6137 | 0 | p2p_buf_add_device_info(buf, p2p, NULL); |
6138 | |
|
6139 | 0 | p2p_buf_update_ie_hdr(buf, len); |
6140 | |
|
6141 | 0 | len = p2p_buf_add_p2p2_ie_hdr(buf); |
6142 | | |
6143 | | /* P2P Capability Extension attribute */ |
6144 | 0 | p2p_buf_add_pcea(buf, p2p); |
6145 | | |
6146 | | /* P2P Pairing Bootstrapping Method attribute */ |
6147 | 0 | p2p_buf_add_pbma(buf, p2p->cfg->pairing_config.bootstrap_methods, NULL, |
6148 | 0 | 0, 0); |
6149 | | |
6150 | | /* P2P Device Identity Resolution attribute */ |
6151 | 0 | if (p2p->pairing_info && |
6152 | 0 | p2p->cfg->pairing_config.pairing_capable && |
6153 | 0 | p2p->cfg->pairing_config.enable_pairing_cache && |
6154 | 0 | p2p_derive_nonce_tag(p2p) == 0) |
6155 | 0 | p2p_buf_add_dira(buf, p2p); |
6156 | |
|
6157 | 0 | p2p_buf_update_ie_hdr(buf, len); |
6158 | |
|
6159 | 0 | return buf; |
6160 | 0 | } |
6161 | | |
6162 | | |
6163 | | void p2p_process_usd_elems(struct p2p_data *p2p, const u8 *ies, u16 ies_len, |
6164 | | const u8 *peer_addr, unsigned int freq) |
6165 | 0 | { |
6166 | 0 | struct p2p_device *dev; |
6167 | 0 | struct p2p_message msg; |
6168 | 0 | const u8 *p2p_dev_addr; |
6169 | |
|
6170 | 0 | os_memset(&msg, 0, sizeof(msg)); |
6171 | 0 | if (p2p_parse_ies(ies, ies_len, &msg)) { |
6172 | 0 | p2p_dbg(p2p, "Failed to parse P2P IE for a device entry"); |
6173 | 0 | p2p_parse_free(&msg); |
6174 | 0 | return; |
6175 | 0 | } |
6176 | 0 | if (msg.p2p_device_addr) |
6177 | 0 | p2p_dev_addr = msg.p2p_device_addr; |
6178 | 0 | else |
6179 | 0 | p2p_dev_addr = peer_addr; |
6180 | |
|
6181 | 0 | dev = p2p_create_device(p2p, p2p_dev_addr); |
6182 | 0 | if (!dev) { |
6183 | 0 | p2p_parse_free(&msg); |
6184 | 0 | p2p_dbg(p2p, "Failed to add a peer P2P Device"); |
6185 | 0 | return; |
6186 | 0 | } |
6187 | | |
6188 | 0 | if (msg.device_name[0]) |
6189 | 0 | os_memcpy(dev->info.device_name, msg.device_name, |
6190 | 0 | sizeof(dev->info.device_name)); |
6191 | |
|
6192 | 0 | dev->p2p2 = true; |
6193 | | /* Reset info from old IEs */ |
6194 | 0 | dev->info.reg_info = 0; |
6195 | 0 | os_memset(&dev->info.pairing_config, 0, |
6196 | 0 | sizeof(struct p2p_pairing_config)); |
6197 | |
|
6198 | 0 | os_get_reltime(&dev->last_seen); |
6199 | 0 | dev->listen_freq = freq; |
6200 | 0 | dev->oper_freq = freq; |
6201 | |
|
6202 | 0 | if (msg.capability) { |
6203 | | /* |
6204 | | * P2P Client Discoverability bit is reserved in all frames |
6205 | | * that use this function, so do not change its value here. |
6206 | | */ |
6207 | 0 | dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
6208 | 0 | dev->info.dev_capab |= msg.capability[0] & |
6209 | 0 | ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; |
6210 | 0 | dev->info.group_capab = msg.capability[1]; |
6211 | 0 | } |
6212 | |
|
6213 | 0 | if (msg.pcea_info && msg.pcea_info_len >= 2) |
6214 | 0 | p2p_process_pcea(p2p, &msg, dev); |
6215 | |
|
6216 | 0 | if (msg.pbma_info && msg.pbma_info_len == 2) |
6217 | 0 | dev->info.pairing_config.bootstrap_methods = |
6218 | 0 | WPA_GET_LE16(msg.pbma_info); |
6219 | |
|
6220 | 0 | if (!ether_addr_equal(peer_addr, p2p_dev_addr)) |
6221 | 0 | os_memcpy(dev->interface_addr, peer_addr, ETH_ALEN); |
6222 | |
|
6223 | 0 | if (msg.dira && msg.dira_len) { |
6224 | 0 | dev->info.nonce_tag_valid = false; |
6225 | 0 | dev->info.dik_id = p2p_validate_dira(p2p, dev, msg.dira, |
6226 | 0 | msg.dira_len); |
6227 | 0 | if (dev->info.dik_id) { |
6228 | 0 | os_memcpy(dev->info.nonce, &msg.dira[1], |
6229 | 0 | DEVICE_IDENTITY_NONCE_LEN); |
6230 | 0 | os_memcpy(dev->info.tag, |
6231 | 0 | &msg.dira[1 + DEVICE_IDENTITY_NONCE_LEN], |
6232 | 0 | DEVICE_IDENTITY_TAG_LEN); |
6233 | 0 | dev->info.pairing_config.dik_cipher = msg.dira[0]; |
6234 | 0 | dev->info.nonce_tag_valid = true; |
6235 | 0 | } |
6236 | 0 | } |
6237 | |
|
6238 | 0 | p2p_dbg(p2p, "Updated device entry based on USD frame: " MACSTR |
6239 | 0 | " dev_capab=0x%x group_capab=0x%x listen_freq=%d", |
6240 | 0 | MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab, |
6241 | 0 | dev->info.group_capab, dev->listen_freq); |
6242 | |
|
6243 | 0 | p2p->cfg->dev_found(p2p->cfg->cb_ctx, dev->info.p2p_device_addr, |
6244 | 0 | &dev->info, !(dev->flags & P2P_DEV_REPORTED_ONCE)); |
6245 | 0 | dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; |
6246 | |
|
6247 | 0 | p2p_parse_free(&msg); |
6248 | 0 | } |
6249 | | |
6250 | | |
6251 | | int p2p_get_dik_id(struct p2p_data *p2p, const u8 *peer) |
6252 | 0 | { |
6253 | 0 | struct p2p_device *dev; |
6254 | |
|
6255 | 0 | dev = p2p_get_device(p2p, peer); |
6256 | 0 | if (!dev) |
6257 | 0 | return 0; |
6258 | | |
6259 | 0 | return dev->info.dik_id; |
6260 | 0 | } |
6261 | | |
6262 | | |
6263 | | #ifdef CONFIG_PASN |
6264 | | |
6265 | | int p2p_config_sae_password(struct p2p_data *p2p, const char *pw) |
6266 | | { |
6267 | | os_memset(p2p->dev_sae_password, 0, sizeof(p2p->dev_sae_password)); |
6268 | | if (os_strlen(pw) >= sizeof(p2p->dev_sae_password)) |
6269 | | return -1; |
6270 | | |
6271 | | os_strlcpy(p2p->dev_sae_password, pw, sizeof(p2p->dev_sae_password)); |
6272 | | return 0; |
6273 | | } |
6274 | | |
6275 | | |
6276 | | static int p2p_prepare_pasn_extra_ie(struct p2p_data *p2p, |
6277 | | struct wpabuf *extra_ies, |
6278 | | const struct wpabuf *frame, bool add_dira) |
6279 | | { |
6280 | | struct wpabuf *buf, *buf2; |
6281 | | size_t len; |
6282 | | |
6283 | | len = 100; |
6284 | | if (frame) |
6285 | | len += wpabuf_len(frame); |
6286 | | buf = wpabuf_alloc(len); |
6287 | | if (!buf) |
6288 | | return -1; |
6289 | | |
6290 | | /* P2P Capability Extension attribute */ |
6291 | | p2p_buf_add_pcea(buf, p2p); |
6292 | | |
6293 | | if (add_dira) { |
6294 | | /* Device Identity Resolution attribute */ |
6295 | | p2p_buf_add_dira(buf, p2p); |
6296 | | } |
6297 | | |
6298 | | if (frame) { |
6299 | | p2p_dbg(p2p, "Add Action frame wrapper for PASN"); |
6300 | | wpabuf_put_u8(buf, P2P_ATTR_ACTION_FRAME_WRAPPER); |
6301 | | wpabuf_put_le16(buf, wpabuf_len(frame)); |
6302 | | wpabuf_put_buf(buf, frame); |
6303 | | } |
6304 | | |
6305 | | buf2 = p2p_encaps_ie(buf, P2P2_IE_VENDOR_TYPE); |
6306 | | wpabuf_free(buf); |
6307 | | |
6308 | | if (wpabuf_tailroom(extra_ies) < wpabuf_len(buf2)) { |
6309 | | p2p_err(p2p, "Not enough room for P2P2 IE in PASN extra IEs"); |
6310 | | wpabuf_free(buf2); |
6311 | | return -1; |
6312 | | } |
6313 | | wpabuf_put_buf(extra_ies, buf2); |
6314 | | wpabuf_free(buf2); |
6315 | | |
6316 | | return 0; |
6317 | | } |
6318 | | |
6319 | | |
6320 | | static struct wpabuf * p2p_pasn_service_hash(struct p2p_data *p2p, |
6321 | | struct wpabuf *extra_ies) |
6322 | | { |
6323 | | struct wpabuf *buf; |
6324 | | u8 *ie_len = NULL; |
6325 | | |
6326 | | if (!p2p->usd_service) |
6327 | | return extra_ies; |
6328 | | |
6329 | | p2p_dbg(p2p, "Add P2P2 USD service hash in extra IE"); |
6330 | | buf = wpabuf_alloc(100); |
6331 | | if (!buf) { |
6332 | | wpabuf_free(extra_ies); |
6333 | | return NULL; |
6334 | | } |
6335 | | |
6336 | | ie_len = p2p_buf_add_ie_hdr(buf); |
6337 | | p2p_buf_add_usd_service_hash(buf, p2p); |
6338 | | p2p_buf_update_ie_hdr(buf, ie_len); |
6339 | | |
6340 | | return wpabuf_concat(buf, extra_ies); |
6341 | | } |
6342 | | |
6343 | | |
6344 | | static struct wpabuf * p2p_pairing_generate_rsnxe(struct p2p_data *p2p, |
6345 | | int akmp) |
6346 | | { |
6347 | | u32 capab; |
6348 | | size_t flen = 0; |
6349 | | struct wpabuf *buf; |
6350 | | |
6351 | | capab = BIT(WLAN_RSNX_CAPAB_KEK_IN_PASN); |
6352 | | |
6353 | | if (wpa_key_mgmt_sae(akmp)) |
6354 | | capab |= BIT(WLAN_RSNX_CAPAB_SAE_H2E); |
6355 | | |
6356 | | while (capab >> flen * 8) |
6357 | | flen++; |
6358 | | |
6359 | | buf = wpabuf_alloc(2 + flen); |
6360 | | if (!buf) |
6361 | | return NULL; |
6362 | | |
6363 | | if (wpabuf_tailroom(buf) < 2 + flen) { |
6364 | | p2p_dbg(p2p, "wpabuf tail room too small"); |
6365 | | wpabuf_free(buf); |
6366 | | return NULL; |
6367 | | } |
6368 | | capab |= flen - 1; /* bit 0-3 = Field length (n - 1) */ |
6369 | | |
6370 | | p2p_dbg(p2p, "RSNXE capabilities: %04x", capab); |
6371 | | wpabuf_put_u8(buf, WLAN_EID_RSNX); |
6372 | | wpabuf_put_u8(buf, flen); |
6373 | | while (flen--) { |
6374 | | wpabuf_put_u8(buf, (capab & 0xff)); |
6375 | | capab = capab >> 8; |
6376 | | } |
6377 | | return buf; |
6378 | | } |
6379 | | |
6380 | | |
6381 | | /* SSID used for deriving SAE pt for pairing */ |
6382 | | #define P2P_PAIRING_SSID "516F9A020000" |
6383 | | |
6384 | | static void p2p_pairing_set_password(struct pasn_data *pasn, u8 pasn_type, |
6385 | | const char *passphrase) |
6386 | | { |
6387 | | int pasn_groups[4] = { 0 }; |
6388 | | size_t len; |
6389 | | |
6390 | | if (!passphrase) |
6391 | | return; |
6392 | | |
6393 | | len = os_strlen(passphrase); |
6394 | | |
6395 | | if (pasn_type & 0xc && pasn_type & 0x3) { |
6396 | | pasn_groups[0] = 20; |
6397 | | pasn_groups[1] = 19; |
6398 | | } else if (pasn_type & 0xc) { |
6399 | | pasn_groups[0] = 20; |
6400 | | } else { |
6401 | | pasn_groups[0] = 19; |
6402 | | } |
6403 | | pasn->pt = sae_derive_pt(pasn_groups, (const u8 *) P2P_PAIRING_SSID, |
6404 | | os_strlen(P2P_PAIRING_SSID), |
6405 | | (const u8 *) passphrase, len, NULL, 0); |
6406 | | } |
6407 | | |
6408 | | |
6409 | | void p2p_pasn_initialize(struct p2p_data *p2p, struct p2p_device *dev, |
6410 | | const u8 *addr, int freq, bool verify, bool derive_kek) |
6411 | | { |
6412 | | struct pasn_data *pasn; |
6413 | | struct wpabuf *rsnxe; |
6414 | | |
6415 | | if (!p2p || !dev) |
6416 | | return; |
6417 | | |
6418 | | if (dev->pasn) { |
6419 | | wpa_pasn_reset(dev->pasn); |
6420 | | } else { |
6421 | | dev->pasn = pasn_data_init(); |
6422 | | if (!dev->pasn) |
6423 | | return; |
6424 | | } |
6425 | | |
6426 | | pasn = dev->pasn; |
6427 | | |
6428 | | os_memcpy(pasn->own_addr, p2p->cfg->dev_addr, ETH_ALEN); |
6429 | | os_memcpy(pasn->peer_addr, addr, ETH_ALEN); |
6430 | | |
6431 | | os_memcpy(pasn->bssid, dev->role == P2P_ROLE_PAIRING_INITIATOR ? |
6432 | | pasn->peer_addr : pasn->own_addr, ETH_ALEN); |
6433 | | |
6434 | | pasn->noauth = 1; |
6435 | | |
6436 | | if ((p2p->cfg->pairing_config.pasn_type & 0xc) && |
6437 | | (dev->info.pairing_config.pasn_type & 0xc)) { |
6438 | | pasn->group = 20; |
6439 | | pasn->cipher = WPA_CIPHER_GCMP_256; |
6440 | | pasn->kek_len = 32; |
6441 | | pasn->derive_kek = true; |
6442 | | } else { |
6443 | | pasn->group = 19; |
6444 | | pasn->cipher = WPA_CIPHER_CCMP; |
6445 | | pasn->kek_len = 16; |
6446 | | pasn->derive_kek = true; |
6447 | | } |
6448 | | |
6449 | | if (!derive_kek) { |
6450 | | pasn->derive_kek = false; |
6451 | | pasn->kek_len = 0; |
6452 | | } |
6453 | | |
6454 | | if (dev->password[0]) { |
6455 | | pasn->akmp = WPA_KEY_MGMT_SAE; |
6456 | | p2p_pairing_set_password(pasn, |
6457 | | p2p->cfg->pairing_config.pasn_type, |
6458 | | dev->password); |
6459 | | } else if (verify) { |
6460 | | pasn->akmp = WPA_KEY_MGMT_SAE; |
6461 | | if (p2p->cfg->set_pmksa) |
6462 | | p2p->cfg->set_pmksa(p2p->cfg->cb_ctx, |
6463 | | dev->info.p2p_device_addr, |
6464 | | dev->info.dik_id); |
6465 | | } else { |
6466 | | pasn->akmp = WPA_KEY_MGMT_PASN; |
6467 | | } |
6468 | | |
6469 | | pasn->rsn_pairwise = pasn->cipher; |
6470 | | pasn->wpa_key_mgmt = pasn->akmp; |
6471 | | |
6472 | | rsnxe = p2p_pairing_generate_rsnxe(p2p, pasn->akmp); |
6473 | | if (rsnxe) { |
6474 | | os_free(pasn->rsnxe_ie); |
6475 | | pasn->rsnxe_ie = os_memdup(wpabuf_head_u8(rsnxe), |
6476 | | wpabuf_len(rsnxe)); |
6477 | | if (!pasn->rsnxe_ie) { |
6478 | | wpabuf_free(rsnxe); |
6479 | | return; |
6480 | | } |
6481 | | wpabuf_free(rsnxe); |
6482 | | } |
6483 | | |
6484 | | if (dev->role == P2P_ROLE_PAIRING_INITIATOR) |
6485 | | pasn->pmksa = p2p->initiator_pmksa; |
6486 | | else |
6487 | | pasn->pmksa = p2p->responder_pmksa; |
6488 | | |
6489 | | pasn->cb_ctx = p2p->cfg->cb_ctx; |
6490 | | pasn->send_mgmt = p2p->cfg->pasn_send_mgmt; |
6491 | | pasn->prepare_data_element = p2p->cfg->prepare_data_element; |
6492 | | pasn->parse_data_element = p2p->cfg->parse_data_element; |
6493 | | pasn->validate_custom_pmkid = p2p->cfg->pasn_validate_pmkid; |
6494 | | |
6495 | | pasn->freq = freq; |
6496 | | } |
6497 | | |
6498 | | |
6499 | | int p2p_get_listen_freq(struct p2p_data *p2p, const u8 *peer_addr) |
6500 | | { |
6501 | | int freq; |
6502 | | struct p2p_device *dev; |
6503 | | |
6504 | | if (!peer_addr) { |
6505 | | p2p_dbg(p2p, "Peer address NULL"); |
6506 | | return -1; |
6507 | | } |
6508 | | |
6509 | | dev = p2p_get_device(p2p, peer_addr); |
6510 | | if (!dev) { |
6511 | | p2p_dbg(p2p, "Peer not known"); |
6512 | | return -1; |
6513 | | } |
6514 | | |
6515 | | freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq; |
6516 | | if (freq <= 0) |
6517 | | freq = dev->oob_go_neg_freq; |
6518 | | if (freq <= 0) { |
6519 | | p2p_dbg(p2p, "No listen/operating frequency known for the peer " |
6520 | | MACSTR, MAC2STR(dev->info.p2p_device_addr)); |
6521 | | return -1; |
6522 | | } |
6523 | | return freq; |
6524 | | } |
6525 | | |
6526 | | |
6527 | | int p2p_initiate_pasn_verify(struct p2p_data *p2p, const u8 *peer_addr, |
6528 | | int freq, enum p2p_invite_role role, |
6529 | | const u8 *bssid, const u8 *ssid, size_t ssid_len, |
6530 | | unsigned int force_freq, const u8 *go_dev_addr, |
6531 | | unsigned int pref_freq) |
6532 | | { |
6533 | | struct pasn_data *pasn; |
6534 | | struct p2p_device *dev; |
6535 | | struct wpabuf *extra_ies, *req; |
6536 | | int ret = 0; |
6537 | | u8 *pasn_extra_ies = NULL; |
6538 | | u8 pmkid[PMKID_LEN]; |
6539 | | |
6540 | | if (!peer_addr) { |
6541 | | p2p_dbg(p2p, "Peer address NULL"); |
6542 | | return -1; |
6543 | | } |
6544 | | |
6545 | | dev = p2p_get_device(p2p, peer_addr); |
6546 | | if (!dev) { |
6547 | | p2p_dbg(p2p, "Peer not known"); |
6548 | | return -1; |
6549 | | } |
6550 | | |
6551 | | if (p2p_invite(p2p, peer_addr, role, bssid, ssid, ssid_len, force_freq, |
6552 | | go_dev_addr, 1, pref_freq, -1, 1)) { |
6553 | | p2p_dbg(p2p, "p2p_invite() failed"); |
6554 | | return -1; |
6555 | | } |
6556 | | |
6557 | | dev->role = P2P_ROLE_PAIRING_INITIATOR; |
6558 | | p2p_pasn_initialize(p2p, dev, peer_addr, freq, true, true); |
6559 | | pasn = dev->pasn; |
6560 | | |
6561 | | req = p2p_build_invitation_req(p2p, dev, go_dev_addr, -1); |
6562 | | if (!req) |
6563 | | return -1; |
6564 | | |
6565 | | p2p_set_state(p2p, P2P_INVITE); |
6566 | | p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST; |
6567 | | p2p->invite_peer = dev; |
6568 | | dev->invitation_reqs++; |
6569 | | |
6570 | | extra_ies = wpabuf_alloc(1500); |
6571 | | if (!extra_ies) { |
6572 | | wpabuf_free(req); |
6573 | | p2p_dbg(p2p, "Memory allocation failed for extra_ies"); |
6574 | | return -1; |
6575 | | } |
6576 | | |
6577 | | if (os_get_random(pmkid, PMKID_LEN) < 0) { |
6578 | | wpabuf_free(req); |
6579 | | wpabuf_free(extra_ies); |
6580 | | return -1; |
6581 | | } |
6582 | | wpa_hexdump(MSG_DEBUG, |
6583 | | "P2P2: Use new random PMKID for pairing verification", |
6584 | | pmkid, PMKID_LEN); |
6585 | | pasn_set_custom_pmkid(pasn, pmkid); |
6586 | | |
6587 | | if (p2p_prepare_pasn_extra_ie(p2p, extra_ies, req, true)) { |
6588 | | p2p_dbg(p2p, "Prepare PASN extra IEs failed"); |
6589 | | ret = -1; |
6590 | | goto out; |
6591 | | } |
6592 | | |
6593 | | extra_ies = p2p_pasn_service_hash(p2p, extra_ies); |
6594 | | if (!extra_ies) |
6595 | | goto out; |
6596 | | |
6597 | | pasn_extra_ies = os_memdup(wpabuf_head_u8(extra_ies), |
6598 | | wpabuf_len(extra_ies)); |
6599 | | if (!pasn_extra_ies) { |
6600 | | p2p_dbg(p2p, "Memory allocation failed for PASN extra IEs"); |
6601 | | ret = -1; |
6602 | | goto out; |
6603 | | } |
6604 | | |
6605 | | pasn->extra_ies = pasn_extra_ies; |
6606 | | pasn->extra_ies_len = wpabuf_len(extra_ies); |
6607 | | |
6608 | | /* Start PASN verify */ |
6609 | | if (wpa_pasn_verify(pasn, pasn->own_addr, pasn->peer_addr, pasn->bssid, |
6610 | | pasn->akmp, pasn->cipher, pasn->group, pasn->freq, |
6611 | | NULL, 0, NULL, 0, NULL)) { |
6612 | | p2p_dbg(p2p, "PASN verify failed"); |
6613 | | ret = -1; |
6614 | | } else { |
6615 | | dev->flags |= P2P_DEV_WAIT_INV_REQ_ACK; |
6616 | | } |
6617 | | out: |
6618 | | pasn->extra_ies = NULL; |
6619 | | pasn->extra_ies_len = 0; |
6620 | | os_free(pasn_extra_ies); |
6621 | | wpabuf_free(req); |
6622 | | wpabuf_free(extra_ies); |
6623 | | return ret; |
6624 | | } |
6625 | | |
6626 | | |
6627 | | int p2p_initiate_pasn_auth(struct p2p_data *p2p, const u8 *addr, int freq) |
6628 | | { |
6629 | | struct pasn_data *pasn; |
6630 | | struct p2p_device *dev; |
6631 | | struct wpabuf *extra_ies, *req; |
6632 | | u8 *ies = NULL; |
6633 | | int ret = 0; |
6634 | | size_t ies_len; |
6635 | | |
6636 | | if (!addr) { |
6637 | | p2p_dbg(p2p, "Peer address NULL"); |
6638 | | return -1; |
6639 | | } |
6640 | | |
6641 | | dev = p2p_get_device(p2p, addr); |
6642 | | if (!dev) { |
6643 | | p2p_dbg(p2p, "Peer not known"); |
6644 | | return -1; |
6645 | | } |
6646 | | |
6647 | | if (freq == 0) |
6648 | | freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq; |
6649 | | |
6650 | | dev->role = P2P_ROLE_PAIRING_INITIATOR; |
6651 | | p2p_pasn_initialize(p2p, dev, addr, freq, false, true); |
6652 | | pasn = dev->pasn; |
6653 | | |
6654 | | pasn_initiator_pmksa_cache_remove(pasn->pmksa, (u8 *)addr); |
6655 | | |
6656 | | req = p2p_build_go_neg_req(p2p, dev); |
6657 | | if (!req) |
6658 | | return -1; |
6659 | | |
6660 | | p2p->go_neg_peer = dev; |
6661 | | dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE; |
6662 | | |
6663 | | extra_ies = wpabuf_alloc(1500); |
6664 | | if (!extra_ies) { |
6665 | | wpabuf_free(req); |
6666 | | return -1; |
6667 | | } |
6668 | | |
6669 | | if (p2p_prepare_pasn_extra_ie(p2p, extra_ies, req, false)) { |
6670 | | p2p_dbg(p2p, "Failed to prepare PASN extra elements"); |
6671 | | ret = -1; |
6672 | | goto out; |
6673 | | } |
6674 | | |
6675 | | extra_ies = p2p_pasn_service_hash(p2p, extra_ies); |
6676 | | if (!extra_ies) |
6677 | | goto out; |
6678 | | |
6679 | | ies_len = wpabuf_len(extra_ies); |
6680 | | ies = os_memdup(wpabuf_head_u8(extra_ies), ies_len); |
6681 | | if (!ies) { |
6682 | | ret = -1; |
6683 | | goto out; |
6684 | | } |
6685 | | |
6686 | | pasn->extra_ies = ies; |
6687 | | pasn->extra_ies_len = ies_len; |
6688 | | |
6689 | | /* Start PASN authentication */ |
6690 | | if (wpas_pasn_start(pasn, pasn->own_addr, pasn->peer_addr, pasn->bssid, |
6691 | | pasn->akmp, pasn->cipher, pasn->group, pasn->freq, |
6692 | | NULL, 0, NULL, 0, NULL)) { |
6693 | | p2p_dbg(p2p, "Failed to start PASN"); |
6694 | | ret = -1; |
6695 | | } |
6696 | | out: |
6697 | | os_free(ies); |
6698 | | pasn->extra_ies = NULL; |
6699 | | pasn->extra_ies_len = 0; |
6700 | | wpabuf_free(req); |
6701 | | wpabuf_free(extra_ies); |
6702 | | return ret; |
6703 | | } |
6704 | | |
6705 | | |
6706 | | static int p2p_pasn_handle_action_wrapper(struct p2p_data *p2p, |
6707 | | struct p2p_device *dev, |
6708 | | const struct ieee80211_mgmt *mgmt, |
6709 | | size_t len, int freq, int trans_seq) |
6710 | | { |
6711 | | const u8 *ies; |
6712 | | size_t ies_len; |
6713 | | size_t data_len = 0; |
6714 | | bool derive_kek; |
6715 | | const u8 *data = NULL; |
6716 | | struct p2p_message msg; |
6717 | | struct ieee802_11_elems elems; |
6718 | | |
6719 | | ies = mgmt->u.auth.variable; |
6720 | | ies_len = len - offsetof(struct ieee80211_mgmt, u.auth.variable); |
6721 | | |
6722 | | os_memset(&msg, 0, sizeof(msg)); |
6723 | | if (p2p_parse_ies(ies, ies_len, &msg)) { |
6724 | | p2p_dbg(p2p, |
6725 | | "Failed to parse P2P IE from PASN Authentication frame"); |
6726 | | p2p_parse_free(&msg); |
6727 | | return -1; |
6728 | | } |
6729 | | |
6730 | | if (msg.action_frame_wrapper && msg.action_frame_wrapper_len) { |
6731 | | data = msg.action_frame_wrapper; |
6732 | | data_len = msg.action_frame_wrapper_len; |
6733 | | if (data_len >= 2 && |
6734 | | data[0] == WLAN_ACTION_PUBLIC && |
6735 | | data[1] == WLAN_PA_VENDOR_SPECIFIC) { |
6736 | | data += 2; |
6737 | | data_len -= 2; |
6738 | | if (data_len < 4 || |
6739 | | WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE) { |
6740 | | p2p_parse_free(&msg); |
6741 | | return -1; |
6742 | | } |
6743 | | data += 4; |
6744 | | data_len -= 4; |
6745 | | } else { |
6746 | | p2p_dbg(p2p, |
6747 | | "Invalid category in Action frame wrapper in Authentication frame seq %d", |
6748 | | trans_seq); |
6749 | | p2p_parse_free(&msg); |
6750 | | return -1; |
6751 | | } |
6752 | | } |
6753 | | |
6754 | | if (trans_seq == WLAN_AUTH_TR_SEQ_PASN_AUTH1) { |
6755 | | if (ieee802_11_parse_elems(mgmt->u.auth.variable, |
6756 | | len - offsetof(struct ieee80211_mgmt, |
6757 | | u.auth.variable), |
6758 | | &elems, 0) == ParseFailed) { |
6759 | | wpa_printf(MSG_DEBUG, |
6760 | | "PASN: Failed parsing Authentication frame"); |
6761 | | return -1; |
6762 | | } |
6763 | | derive_kek = ieee802_11_rsnx_capab_len( |
6764 | | elems.rsnxe, elems.rsnxe_len, |
6765 | | WLAN_RSNX_CAPAB_KEK_IN_PASN); |
6766 | | if (data && data_len >= 1 && data[0] == P2P_INVITATION_REQ) { |
6767 | | struct wpabuf *resp; |
6768 | | |
6769 | | resp = p2p_process_invitation_req(p2p, mgmt->sa, |
6770 | | data + 1, |
6771 | | data_len - 1, freq, |
6772 | | true); |
6773 | | if (!resp) |
6774 | | p2p_dbg(p2p, "No Invitation Response found"); |
6775 | | |
6776 | | dev->role = P2P_ROLE_PAIRING_RESPONDER; |
6777 | | p2p_pasn_initialize(p2p, dev, mgmt->sa, freq, true, |
6778 | | derive_kek); |
6779 | | wpabuf_free(dev->action_frame_wrapper); |
6780 | | dev->action_frame_wrapper = resp; |
6781 | | if (msg.dira && msg.dira_len && |
6782 | | p2p_validate_dira(p2p, dev, msg.dira, |
6783 | | msg.dira_len)) { |
6784 | | struct wpa_ie_data rsn_data; |
6785 | | |
6786 | | if (p2p->cfg->set_pmksa) |
6787 | | p2p->cfg->set_pmksa( |
6788 | | p2p->cfg->cb_ctx, |
6789 | | dev->info.p2p_device_addr, |
6790 | | dev->info.dik_id); |
6791 | | |
6792 | | if (wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, |
6793 | | elems.rsn_ie_len + 2, |
6794 | | &rsn_data) == 0 && |
6795 | | rsn_data.num_pmkid) |
6796 | | pasn_set_custom_pmkid(dev->pasn, |
6797 | | rsn_data.pmkid); |
6798 | | } |
6799 | | } else if (data && data_len >= 1 && data[0] == P2P_GO_NEG_REQ) { |
6800 | | struct wpabuf *resp; |
6801 | | |
6802 | | if (!derive_kek) { |
6803 | | p2p_dbg(p2p, "KEK-in-PASN not set in RSNXE"); |
6804 | | return -1; |
6805 | | } |
6806 | | resp = p2p_process_go_neg_req(p2p, mgmt->sa, data + 1, |
6807 | | data_len - 1, freq, true); |
6808 | | if (!resp) |
6809 | | p2p_dbg(p2p, |
6810 | | "No GO Negotiation Response found"); |
6811 | | wpabuf_free(dev->action_frame_wrapper); |
6812 | | dev->action_frame_wrapper = resp; |
6813 | | } else { |
6814 | | p2p_dbg(p2p, "Invalid action frame wrapper in Auth1"); |
6815 | | } |
6816 | | } else if (trans_seq == WLAN_AUTH_TR_SEQ_PASN_AUTH2) { |
6817 | | if (data && data_len >= 1 && data[0] == P2P_INVITATION_RESP) { |
6818 | | p2p_process_invitation_resp(p2p, mgmt->sa, data + 1, |
6819 | | data_len - 1); |
6820 | | wpabuf_free(dev->action_frame_wrapper); |
6821 | | dev->action_frame_wrapper = NULL; |
6822 | | } else if (data && data_len >= 1 && |
6823 | | data[0] == P2P_GO_NEG_RESP) { |
6824 | | struct wpabuf *conf; |
6825 | | |
6826 | | conf = p2p_process_go_neg_resp(p2p, mgmt->sa, data + 1, |
6827 | | data_len - 1, freq, |
6828 | | true); |
6829 | | if (!conf) |
6830 | | p2p_dbg(p2p, "No GO Negotiation Confirm found"); |
6831 | | wpabuf_free(dev->action_frame_wrapper); |
6832 | | dev->action_frame_wrapper = conf; |
6833 | | } else { |
6834 | | p2p_dbg(p2p, "Invalid action frame wrapper in Auth2"); |
6835 | | } |
6836 | | } else if (trans_seq == WLAN_AUTH_TR_SEQ_PASN_AUTH3) { |
6837 | | if (data && data_len >= 1 && data[0] == P2P_GO_NEG_CONF) |
6838 | | p2p_handle_go_neg_conf(p2p, mgmt->sa, data + 1, |
6839 | | data_len - 1, true); |
6840 | | else |
6841 | | p2p_invitation_resp_cb(p2p, mgmt->sa, |
6842 | | P2P_SEND_ACTION_SUCCESS); |
6843 | | } |
6844 | | p2p_parse_free(&msg); |
6845 | | return 0; |
6846 | | } |
6847 | | |
6848 | | |
6849 | | static int p2p_pasn_add_encrypted_data(struct p2p_data *p2p, |
6850 | | struct p2p_device *dev, |
6851 | | struct wpabuf *buf) |
6852 | | { |
6853 | | struct pasn_data *pasn; |
6854 | | struct wpabuf *p2p2_ie; |
6855 | | u8 *dika_len, *p2p2_ie_len; |
6856 | | int ret; |
6857 | | |
6858 | | if (!p2p || !dev || !dev->pasn) |
6859 | | return 0; |
6860 | | |
6861 | | pasn = dev->pasn; |
6862 | | |
6863 | | if (dev->req_bootstrap_method != P2P_PBMA_OPPORTUNISTIC && |
6864 | | !p2p->pairing_info->enable_pairing_cache) |
6865 | | return 0; |
6866 | | |
6867 | | p2p2_ie = wpabuf_alloc(100); |
6868 | | if (!p2p2_ie) |
6869 | | return -1; |
6870 | | |
6871 | | p2p2_ie_len = p2p_buf_add_p2p2_ie_hdr(p2p2_ie); |
6872 | | |
6873 | | if (p2p->pairing_info->enable_pairing_cache) { |
6874 | | wpabuf_put_u8(p2p2_ie, P2P_ATTR_DEVICE_IDENTITY_KEY); |
6875 | | dika_len = wpabuf_put(p2p2_ie, 2); |
6876 | | |
6877 | | wpabuf_put_u8(p2p2_ie, |
6878 | | p2p->pairing_info->dev_ik.cipher_version); |
6879 | | wpabuf_put_data(p2p2_ie, p2p->pairing_info->dev_ik.dik_data, |
6880 | | p2p->pairing_info->dev_ik.dik_len); |
6881 | | wpabuf_put_be32(p2p2_ie, p2p->pairing_info->dev_ik.expiration); |
6882 | | |
6883 | | WPA_PUT_LE16(dika_len, |
6884 | | (u8 *) wpabuf_put(p2p2_ie, 0) - dika_len - 2); |
6885 | | } |
6886 | | |
6887 | | if (dev->req_bootstrap_method == P2P_PBMA_OPPORTUNISTIC) { |
6888 | | if (!p2p->dev_sae_password[0]) { |
6889 | | int password_len; |
6890 | | |
6891 | | /* SAE password is not available as the request is not |
6892 | | * for an existing GO. Pick a random SAE password of |
6893 | | * length between 10 and 20. */ |
6894 | | password_len = 10 + os_random() % 10; |
6895 | | if (p2p_random(p2p->dev_sae_password, |
6896 | | password_len) < 0) { |
6897 | | wpabuf_free(p2p2_ie); |
6898 | | return -1; |
6899 | | } |
6900 | | p2p->dev_sae_password[password_len] = '\0'; |
6901 | | } |
6902 | | |
6903 | | wpabuf_put_u8(p2p2_ie, P2P_ATTR_PASSWORD); |
6904 | | wpabuf_put_le16(p2p2_ie, os_strlen(p2p->dev_sae_password)); |
6905 | | wpabuf_put_str(p2p2_ie, p2p->dev_sae_password); |
6906 | | } |
6907 | | |
6908 | | p2p_buf_update_ie_hdr(p2p2_ie, p2p2_ie_len); |
6909 | | |
6910 | | ret = pasn_add_encrypted_data(pasn, buf, wpabuf_mhead_u8(p2p2_ie), |
6911 | | wpabuf_len(p2p2_ie)); |
6912 | | wpabuf_free(p2p2_ie); |
6913 | | return ret; |
6914 | | } |
6915 | | |
6916 | | |
6917 | | int p2p_prepare_data_element(struct p2p_data *p2p, const u8 *peer_addr) |
6918 | | { |
6919 | | int ret = -1; |
6920 | | struct p2p_device *dev; |
6921 | | struct pasn_data *pasn; |
6922 | | struct wpabuf *extra_ies; |
6923 | | |
6924 | | if (!p2p) |
6925 | | return -1; |
6926 | | |
6927 | | dev = p2p_get_device(p2p, peer_addr); |
6928 | | if (!dev || !dev->pasn) { |
6929 | | p2p_dbg(p2p, "PASN: Peer not found " MACSTR, |
6930 | | MAC2STR(peer_addr)); |
6931 | | return -1; |
6932 | | } |
6933 | | pasn = dev->pasn; |
6934 | | |
6935 | | extra_ies = wpabuf_alloc(1500); |
6936 | | if (!extra_ies || |
6937 | | p2p_prepare_pasn_extra_ie(p2p, extra_ies, |
6938 | | dev->action_frame_wrapper, false)) { |
6939 | | p2p_dbg(p2p, "Failed to prepare PASN extra elements"); |
6940 | | goto out; |
6941 | | } |
6942 | | |
6943 | | if (p2p_pasn_add_encrypted_data(p2p, dev, extra_ies) < 0) |
6944 | | p2p_dbg(p2p, "Failed to add PASN encrypted elements"); |
6945 | | |
6946 | | ret = pasn_set_extra_ies(pasn, wpabuf_head_u8(extra_ies), |
6947 | | wpabuf_len(extra_ies)); |
6948 | | |
6949 | | out: |
6950 | | wpabuf_free(extra_ies); |
6951 | | wpabuf_free(dev->action_frame_wrapper); |
6952 | | dev->action_frame_wrapper = NULL; |
6953 | | |
6954 | | return ret; |
6955 | | } |
6956 | | |
6957 | | |
6958 | | int p2p_parse_data_element(struct p2p_data *p2p, const u8 *data, size_t len) |
6959 | | { |
6960 | | u8 attr_id; |
6961 | | const u8 *pos, *next; |
6962 | | u16 rem_len, attr_len; |
6963 | | |
6964 | | if (!p2p || !data || !len) |
6965 | | return -1; |
6966 | | |
6967 | | pos = data; |
6968 | | rem_len = len; |
6969 | | |
6970 | | if (rem_len < 6 || |
6971 | | pos[0] != WLAN_EID_VENDOR_SPECIFIC || |
6972 | | pos[1] < 4 || |
6973 | | rem_len < 2 + pos[1] || |
6974 | | WPA_GET_BE32(&pos[2]) != P2P2_IE_VENDOR_TYPE) { |
6975 | | p2p_dbg(p2p, |
6976 | | "P2P: P2P2 IE not present in PASN Encrypted Data element"); |
6977 | | return -1; |
6978 | | } |
6979 | | |
6980 | | pos += 6; |
6981 | | rem_len -= 6; |
6982 | | |
6983 | | while (rem_len >= 3) { |
6984 | | attr_id = *pos++; |
6985 | | attr_len = WPA_GET_LE16(pos); |
6986 | | pos += 2; |
6987 | | rem_len -= 3; |
6988 | | if (rem_len < attr_len) |
6989 | | return -1; |
6990 | | next = pos + attr_len; |
6991 | | rem_len -= attr_len; |
6992 | | |
6993 | | switch (attr_id) { |
6994 | | case P2P_ATTR_DEVICE_IDENTITY_KEY: |
6995 | | if (attr_len < 1) { |
6996 | | p2p_dbg(p2p, |
6997 | | "Too short Device Identity Key attribute"); |
6998 | | return -1; |
6999 | | } |
7000 | | p2p->dik_cipher_version = *pos++; |
7001 | | attr_len--; |
7002 | | if (p2p->dik_cipher_version == |
7003 | | DIRA_CIPHER_VERSION_128) { |
7004 | | if (attr_len < DEVICE_IDENTITY_KEY_LEN) { |
7005 | | p2p_dbg(p2p, "Too short DevIK"); |
7006 | | return -1; |
7007 | | } |
7008 | | os_memcpy(p2p->peer_dik_data, pos, |
7009 | | DEVICE_IDENTITY_KEY_LEN); |
7010 | | p2p->peer_dik_len = DEVICE_IDENTITY_KEY_LEN; |
7011 | | pos += DEVICE_IDENTITY_KEY_LEN; |
7012 | | attr_len -= DEVICE_IDENTITY_KEY_LEN; |
7013 | | } else { |
7014 | | p2p_dbg(p2p, |
7015 | | "Unsupported cipher version %u in Device Identity Key attribute", |
7016 | | p2p->dik_cipher_version); |
7017 | | return -1; |
7018 | | } |
7019 | | if (attr_len < 4) { |
7020 | | p2p_dbg(p2p, |
7021 | | "Not enough room for DevIK lifetime"); |
7022 | | return -1; |
7023 | | } |
7024 | | p2p->peer_dik_lifetime = WPA_GET_BE32(pos); |
7025 | | p2p_dbg(p2p, |
7026 | | "Received peer DevIK of length %zu octets and lifetime %u", |
7027 | | p2p->peer_dik_len, p2p->peer_dik_lifetime); |
7028 | | break; |
7029 | | case P2P_ATTR_PASSWORD: |
7030 | | if (attr_len < 1 || |
7031 | | attr_len > sizeof(p2p->peer_sae_password) - 1) { |
7032 | | p2p_dbg(p2p, |
7033 | | "P2P: Invalid password length %d", |
7034 | | attr_len); |
7035 | | return -1; |
7036 | | } |
7037 | | os_memset(p2p->peer_sae_password, 0, |
7038 | | sizeof(p2p->peer_sae_password)); |
7039 | | os_memcpy(p2p->peer_sae_password, pos, attr_len); |
7040 | | break; |
7041 | | default: |
7042 | | p2p_dbg(p2p, |
7043 | | "Unsupported Attribute ID %u in P2P2 IE in PASN Encrypted Data element", |
7044 | | attr_id); |
7045 | | break; |
7046 | | } |
7047 | | pos = next; |
7048 | | } |
7049 | | |
7050 | | return 0; |
7051 | | } |
7052 | | |
7053 | | |
7054 | | static int p2p_validate_custom_pmkid(struct p2p_data *p2p, |
7055 | | struct p2p_device *dev, const u8 *pmkid) |
7056 | | { |
7057 | | if (dev->pasn->custom_pmkid_valid && |
7058 | | os_memcmp(dev->pasn->custom_pmkid, pmkid, PMKID_LEN) == 0) { |
7059 | | p2p_dbg(p2p, "Customized PMKID valid"); |
7060 | | return 0; |
7061 | | } |
7062 | | return -1; |
7063 | | } |
7064 | | |
7065 | | |
7066 | | static int p2p_pasn_pmksa_get_pmk(struct p2p_data *p2p, const u8 *addr, |
7067 | | u8 *pmkid, u8 *pmk, size_t *pmk_len) |
7068 | | { |
7069 | | struct p2p_device *dev; |
7070 | | |
7071 | | dev = p2p_get_device(p2p, addr); |
7072 | | if (!dev) { |
7073 | | p2p_dbg(p2p, "PASN: Peer not found " MACSTR, MAC2STR(addr)); |
7074 | | return -1; |
7075 | | } |
7076 | | |
7077 | | if (dev->role == P2P_ROLE_PAIRING_INITIATOR) |
7078 | | return pasn_initiator_pmksa_cache_get(p2p->initiator_pmksa, |
7079 | | addr, pmkid, pmk, |
7080 | | pmk_len); |
7081 | | else |
7082 | | return pasn_responder_pmksa_cache_get(p2p->responder_pmksa, |
7083 | | addr, pmkid, pmk, |
7084 | | pmk_len); |
7085 | | } |
7086 | | |
7087 | | |
7088 | | int p2p_pasn_validate_and_update_pmkid(struct p2p_data *p2p, const u8 *addr, |
7089 | | const u8 *rsn_pmkid) |
7090 | | { |
7091 | | size_t pmk_len; |
7092 | | u8 pmkid[PMKID_LEN]; |
7093 | | u8 pmk[PMK_LEN_MAX]; |
7094 | | struct p2p_device *dev; |
7095 | | |
7096 | | if (!p2p) |
7097 | | return -1; |
7098 | | |
7099 | | dev = p2p_get_device(p2p, addr); |
7100 | | if (!dev || !dev->pasn) { |
7101 | | p2p_dbg(p2p, "P2P PASN: Peer not found " MACSTR, |
7102 | | MAC2STR(addr)); |
7103 | | return -1; |
7104 | | } |
7105 | | |
7106 | | if (p2p_validate_custom_pmkid(p2p, dev, rsn_pmkid)) |
7107 | | return -1; |
7108 | | |
7109 | | if (p2p_pasn_pmksa_get_pmk(p2p, addr, pmkid, pmk, &pmk_len)) { |
7110 | | p2p_dbg(p2p, "P2P PASN: Failed to get PMK from cache"); |
7111 | | return -1; |
7112 | | } |
7113 | | |
7114 | | p2p_pasn_pmksa_set_pmk(p2p, p2p->cfg->dev_addr, addr, pmk, pmk_len, |
7115 | | rsn_pmkid); |
7116 | | return 0; |
7117 | | } |
7118 | | |
7119 | | |
7120 | | int p2p_pasn_auth_tx_status(struct p2p_data *p2p, const u8 *data, |
7121 | | size_t data_len, bool acked, bool verify) |
7122 | | { |
7123 | | int ret = 0; |
7124 | | struct p2p_device *dev; |
7125 | | struct pasn_data *pasn; |
7126 | | const struct ieee80211_mgmt *mgmt = |
7127 | | (const struct ieee80211_mgmt *) data; |
7128 | | |
7129 | | if (!p2p) |
7130 | | return -1; |
7131 | | |
7132 | | dev = p2p_get_device(p2p, mgmt->da); |
7133 | | if (!dev || !dev->pasn) { |
7134 | | p2p_dbg(p2p, "P2P PASN: Peer not found " MACSTR, |
7135 | | MAC2STR(mgmt->da)); |
7136 | | return -1; |
7137 | | } |
7138 | | |
7139 | | pasn = dev->pasn; |
7140 | | |
7141 | | ret = wpa_pasn_auth_tx_status(pasn, data, data_len, acked); |
7142 | | if (ret != 1 && !acked && pasn->frame) |
7143 | | return pasn->send_mgmt(pasn->cb_ctx, wpabuf_head(pasn->frame), |
7144 | | wpabuf_len(pasn->frame), 0, pasn->freq, |
7145 | | 1000); |
7146 | | |
7147 | | wpabuf_free(pasn->frame); |
7148 | | pasn->frame = NULL; |
7149 | | |
7150 | | if (ret != 1) |
7151 | | return ret; |
7152 | | |
7153 | | if (verify && dev == p2p->invite_peer) |
7154 | | p2p_start_invitation_connect(p2p, dev); |
7155 | | else if (dev == p2p->go_neg_peer) |
7156 | | p2p_go_complete(p2p, dev); |
7157 | | |
7158 | | return 0; |
7159 | | } |
7160 | | |
7161 | | |
7162 | | static int p2p_handle_pasn_auth(struct p2p_data *p2p, struct p2p_device *dev, |
7163 | | const struct ieee80211_mgmt *mgmt, size_t len, |
7164 | | int freq) |
7165 | | { |
7166 | | struct pasn_data *pasn; |
7167 | | u8 pasn_type; |
7168 | | int pasn_groups[4] = { 0 }; |
7169 | | u16 auth_alg, auth_transaction, status_code; |
7170 | | |
7171 | | if (!p2p || !dev || !dev->pasn) |
7172 | | return -1; |
7173 | | |
7174 | | if (os_memcmp(mgmt->da, p2p->cfg->dev_addr, ETH_ALEN) != 0) { |
7175 | | p2p_dbg(p2p, "PASN Responder: Not our frame"); |
7176 | | return -1; |
7177 | | } |
7178 | | |
7179 | | if (len < offsetof(struct ieee80211_mgmt, u.auth.variable)) |
7180 | | return -1; |
7181 | | |
7182 | | pasn = dev->pasn; |
7183 | | auth_alg = le_to_host16(mgmt->u.auth.auth_alg); |
7184 | | status_code = le_to_host16(mgmt->u.auth.status_code); |
7185 | | |
7186 | | auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction); |
7187 | | |
7188 | | if (status_code != WLAN_STATUS_SUCCESS && |
7189 | | status_code != WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY) { |
7190 | | p2p_dbg(p2p, "PASN: Authentication rejected - status=%u", |
7191 | | status_code); |
7192 | | return -1; |
7193 | | } |
7194 | | |
7195 | | if (auth_alg != WLAN_AUTH_PASN || |
7196 | | auth_transaction == WLAN_AUTH_TR_SEQ_PASN_AUTH2) { |
7197 | | p2p_dbg(p2p, |
7198 | | "PASN Responder: Not a PASN frame or unexpected Authentication frame, auth_alg=%d", |
7199 | | auth_alg); |
7200 | | return -1; |
7201 | | } |
7202 | | if (auth_transaction == WLAN_AUTH_TR_SEQ_PASN_AUTH1) { |
7203 | | pasn_type = p2p->cfg->pairing_config.pasn_type; |
7204 | | if (pasn_type & 0xc && pasn_type & 0x3) { |
7205 | | pasn_groups[0] = 20; |
7206 | | pasn_groups[1] = 19; |
7207 | | } else if (pasn_type & 0xc) { |
7208 | | pasn_groups[0] = 20; |
7209 | | } else { |
7210 | | pasn_groups[0] = 19; |
7211 | | } |
7212 | | os_free(pasn->pasn_groups); |
7213 | | pasn->pasn_groups = int_array_dup(pasn_groups); |
7214 | | |
7215 | | if (p2p_pasn_handle_action_wrapper(p2p, dev, mgmt, len, freq, |
7216 | | auth_transaction)) { |
7217 | | p2p_dbg(p2p, |
7218 | | "PASN Responder: Handle Auth 1 action wrapper failed"); |
7219 | | return -1; |
7220 | | } |
7221 | | if (handle_auth_pasn_1(pasn, p2p->cfg->dev_addr, mgmt->sa, mgmt, |
7222 | | len, false) < 0) { |
7223 | | p2p_dbg(p2p, |
7224 | | "PASN Responder: Handle Auth 1 failed"); |
7225 | | return -1; |
7226 | | } |
7227 | | } else if (auth_transaction == WLAN_AUTH_TR_SEQ_PASN_AUTH3) { |
7228 | | if (handle_auth_pasn_3(pasn, p2p->cfg->dev_addr, mgmt->sa, mgmt, |
7229 | | len) < 0) { |
7230 | | p2p_dbg(p2p, |
7231 | | "PASN Responder: Handle Auth 3 failed"); |
7232 | | return -1; |
7233 | | } |
7234 | | #ifdef CONFIG_TESTING_OPTIONS |
7235 | | p2p_pasn_store_ptk(p2p, &pasn->ptk); |
7236 | | #endif /* CONFIG_TESTING_OPTIONS */ |
7237 | | if (p2p_pasn_handle_action_wrapper(p2p, dev, mgmt, len, freq, |
7238 | | auth_transaction)) { |
7239 | | p2p_dbg(p2p, |
7240 | | "PASN Responder: Handle Auth 3 action wrapper failed"); |
7241 | | /* Drop keying material from a failed pairing attempt */ |
7242 | | os_memset(p2p->peer_dik_data, 0, |
7243 | | sizeof(p2p->peer_dik_data)); |
7244 | | os_memset(p2p->peer_sae_password, 0, |
7245 | | sizeof(p2p->peer_sae_password)); |
7246 | | return -1; |
7247 | | } |
7248 | | forced_memzero(pasn_get_ptk(pasn), sizeof(pasn->ptk)); |
7249 | | } |
7250 | | return 0; |
7251 | | } |
7252 | | |
7253 | | |
7254 | | int p2p_pasn_auth_rx(struct p2p_data *p2p, const struct ieee80211_mgmt *mgmt, |
7255 | | size_t len, int freq) |
7256 | | { |
7257 | | int ret = 0; |
7258 | | u8 auth_transaction; |
7259 | | struct p2p_device *dev; |
7260 | | struct pasn_data *pasn; |
7261 | | struct wpa_pasn_params_data pasn_data; |
7262 | | |
7263 | | dev = p2p_get_device(p2p, mgmt->sa); |
7264 | | if (!dev) { |
7265 | | p2p_dbg(p2p, "PASN: Peer not found " MACSTR, |
7266 | | MAC2STR(mgmt->sa)); |
7267 | | return -1; |
7268 | | } |
7269 | | |
7270 | | if (!dev->pasn) { |
7271 | | dev->pasn = pasn_data_init(); |
7272 | | if (!dev->pasn) { |
7273 | | p2p_dbg(p2p, "PASN: Uninitialized"); |
7274 | | return -1; |
7275 | | } |
7276 | | } |
7277 | | |
7278 | | pasn = dev->pasn; |
7279 | | |
7280 | | wpabuf_free(pasn->frame); |
7281 | | pasn->frame = NULL; |
7282 | | |
7283 | | pasn_register_callbacks(pasn, p2p->cfg->cb_ctx, |
7284 | | p2p->cfg->pasn_send_mgmt, |
7285 | | p2p->cfg->pasn_validate_pmkid, |
7286 | | NULL, NULL); |
7287 | | auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction); |
7288 | | |
7289 | | if (dev->role == P2P_ROLE_PAIRING_INITIATOR && |
7290 | | auth_transaction == WLAN_AUTH_TR_SEQ_PASN_AUTH2) { |
7291 | | if (p2p_pasn_handle_action_wrapper(p2p, dev, mgmt, len, freq, |
7292 | | auth_transaction)) { |
7293 | | p2p_dbg(p2p, |
7294 | | "PASN Initiator: Handle Auth 2 action wrapper failed"); |
7295 | | return -1; |
7296 | | } |
7297 | | ret = wpa_pasn_auth_rx(pasn, (const u8 *) mgmt, len, |
7298 | | &pasn_data); |
7299 | | if (ret < 0) { |
7300 | | p2p_dbg(p2p, "PASN: wpa_pasn_auth_rx() failed"); |
7301 | | dev->role = P2P_ROLE_IDLE; |
7302 | | } |
7303 | | #ifdef CONFIG_TESTING_OPTIONS |
7304 | | p2p_pasn_store_ptk(p2p, &pasn->ptk); |
7305 | | #endif /* CONFIG_TESTING_OPTIONS */ |
7306 | | forced_memzero(pasn_get_ptk(pasn), sizeof(pasn->ptk)); |
7307 | | } else { |
7308 | | ret = p2p_handle_pasn_auth(p2p, dev, mgmt, len, freq); |
7309 | | } |
7310 | | return ret; |
7311 | | } |
7312 | | |
7313 | | |
7314 | | void p2p_pasn_pmksa_set_pmk(struct p2p_data *p2p, const u8 *src, const u8 *dst, |
7315 | | const u8 *pmk, size_t pmk_len, const u8 *pmkid) |
7316 | | { |
7317 | | pasn_initiator_pmksa_cache_add(p2p->initiator_pmksa, src, dst, pmk, |
7318 | | pmk_len, pmkid, WPA_KEY_MGMT_SAE); |
7319 | | pasn_responder_pmksa_cache_add(p2p->responder_pmksa, src, dst, pmk, |
7320 | | pmk_len, pmkid, WPA_KEY_MGMT_SAE); |
7321 | | } |
7322 | | |
7323 | | |
7324 | | #ifdef CONFIG_TESTING_OPTIONS |
7325 | | |
7326 | | void p2p_pasn_store_ptk(struct p2p_data *p2p, struct wpa_ptk *ptk) |
7327 | | { |
7328 | | u8 *pos; |
7329 | | |
7330 | | if (ptk->ptk_len > sizeof(p2p->pasn_ptk)) { |
7331 | | p2p_dbg(p2p, "P2P PASN PTK exceeds: (len=%ld)", ptk->ptk_len); |
7332 | | return; |
7333 | | } |
7334 | | |
7335 | | pos = p2p->pasn_ptk; |
7336 | | p2p->pasn_ptk_len = ptk->ptk_len; |
7337 | | if (ptk->kck_len) { |
7338 | | os_memcpy(pos, ptk->kck, ptk->kck_len); |
7339 | | pos += ptk->kck_len; |
7340 | | } |
7341 | | if (ptk->kek_len) { |
7342 | | os_memcpy(pos, ptk->kek, ptk->kek_len); |
7343 | | pos += ptk->kek_len; |
7344 | | } |
7345 | | if (ptk->tk_len) { |
7346 | | os_memcpy(pos, ptk->tk, ptk->tk_len); |
7347 | | pos += ptk->tk_len; |
7348 | | } |
7349 | | if (ptk->kdk_len) { |
7350 | | os_memcpy(pos, ptk->kdk, ptk->kdk_len); |
7351 | | pos += ptk->kdk_len; |
7352 | | } |
7353 | | } |
7354 | | |
7355 | | |
7356 | | int p2p_pasn_get_ptk(struct p2p_data *p2p, const u8 **buf, size_t *buf_len) |
7357 | | { |
7358 | | if (!p2p || !p2p->pasn_ptk_len) |
7359 | | return -1; |
7360 | | |
7361 | | *buf_len = p2p->pasn_ptk_len; |
7362 | | *buf = p2p->pasn_ptk; |
7363 | | return 0; |
7364 | | } |
7365 | | |
7366 | | #endif /* CONFIG_TESTING_OPTIONS */ |
7367 | | |
7368 | | #endif /* CONFIG_PASN */ |
7369 | | |
7370 | | |
7371 | | int p2p_get_dira_info(struct p2p_data *p2p, char *buf, size_t buflen) |
7372 | 0 | { |
7373 | 0 | int res; |
7374 | 0 | char *pos, *end; |
7375 | 0 | struct p2p_id_key *dev_ik; |
7376 | |
|
7377 | 0 | if (!p2p->pairing_info || |
7378 | 0 | !p2p->cfg->pairing_config.pairing_capable || |
7379 | 0 | !p2p->cfg->pairing_config.enable_pairing_cache) |
7380 | 0 | return 0; |
7381 | | |
7382 | 0 | if (p2p_derive_nonce_tag(p2p)) |
7383 | 0 | return 0; |
7384 | | |
7385 | 0 | pos = buf; |
7386 | 0 | end = buf + buflen; |
7387 | 0 | dev_ik = &p2p->pairing_info->dev_ik; |
7388 | |
|
7389 | 0 | res = os_snprintf(pos, end - pos, MACSTR, |
7390 | 0 | MAC2STR(p2p->cfg->dev_addr)); |
7391 | 0 | if (os_snprintf_error(end - pos, res)) |
7392 | 0 | return pos - buf; |
7393 | 0 | pos += res; |
7394 | |
|
7395 | 0 | res = os_snprintf(pos, end - pos, " "); |
7396 | 0 | if (os_snprintf_error(end - pos, res)) |
7397 | 0 | return pos - buf; |
7398 | 0 | pos += res; |
7399 | |
|
7400 | 0 | pos += wpa_snprintf_hex(pos, end - pos, dev_ik->dira_nonce, |
7401 | 0 | dev_ik->dira_nonce_len); |
7402 | |
|
7403 | 0 | res = os_snprintf(pos, end - pos, " "); |
7404 | 0 | if (os_snprintf_error(end - pos, res)) |
7405 | 0 | return pos - buf; |
7406 | 0 | pos += res; |
7407 | |
|
7408 | 0 | pos += wpa_snprintf_hex(pos, end - pos, dev_ik->dira_tag, |
7409 | 0 | dev_ik->dira_tag_len); |
7410 | |
|
7411 | 0 | res = os_snprintf(pos, end - pos, "\n"); |
7412 | 0 | if (os_snprintf_error(end - pos, res)) |
7413 | 0 | return pos - buf; |
7414 | 0 | pos += res; |
7415 | |
|
7416 | 0 | return pos - buf; |
7417 | 0 | } |