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